Skip to content

Commit 9ebff83

Browse files
committed
netfs: Prep to use folio->private for write grouping and streaming write
Prepare to use folio->private to hold information write grouping and streaming write. These are implemented in the same commit as they both make use of folio->private and will be both checked at the same time in several places. "Write grouping" involves ordering the writeback of groups of writes, such as is needed for ceph snaps. A group is represented by a filesystem-supplied object which must contain a netfs_group struct. This contains just a refcount and a pointer to a destructor. "Streaming write" is the storage of data in folios that are marked dirty, but not uptodate, to avoid unnecessary reads of data. This is represented by a netfs_folio struct. This contains the offset and length of the modified region plus the otherwise displaced write grouping pointer. The way folio->private is multiplexed is: (1) If private is NULL then neither is in operation on a dirty folio. (2) If private is set, with bit 0 clear, then this points to a group. (3) If private is set, with bit 0 set, then this points to a netfs_folio struct (with bit 0 AND'ed out). Signed-off-by: David Howells <[email protected]> Reviewed-by: Jeff Layton <[email protected]> cc: [email protected] cc: [email protected] cc: [email protected]
1 parent 4fcccc3 commit 9ebff83

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

fs/netfs/internal.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,34 @@ static inline bool netfs_is_cache_enabled(struct netfs_inode *ctx)
149149
#endif
150150
}
151151

152+
/*
153+
* Get a ref on a netfs group attached to a dirty page (e.g. a ceph snap).
154+
*/
155+
static inline struct netfs_group *netfs_get_group(struct netfs_group *netfs_group)
156+
{
157+
if (netfs_group)
158+
refcount_inc(&netfs_group->ref);
159+
return netfs_group;
160+
}
161+
162+
/*
163+
* Dispose of a netfs group attached to a dirty page (e.g. a ceph snap).
164+
*/
165+
static inline void netfs_put_group(struct netfs_group *netfs_group)
166+
{
167+
if (netfs_group && refcount_dec_and_test(&netfs_group->ref))
168+
netfs_group->free(netfs_group);
169+
}
170+
171+
/*
172+
* Dispose of a netfs group attached to a dirty page (e.g. a ceph snap).
173+
*/
174+
static inline void netfs_put_group_many(struct netfs_group *netfs_group, int nr)
175+
{
176+
if (netfs_group && refcount_sub_and_test(nr, &netfs_group->ref))
177+
netfs_group->free(netfs_group);
178+
}
179+
152180
/*
153181
* fscache-cache.c
154182
*/

fs/netfs/misc.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,55 @@ EXPORT_SYMBOL(netfs_clear_inode_writeback);
177177
*/
178178
void netfs_invalidate_folio(struct folio *folio, size_t offset, size_t length)
179179
{
180+
struct netfs_folio *finfo = NULL;
181+
size_t flen = folio_size(folio);
182+
180183
_enter("{%lx},%zx,%zx", folio_index(folio), offset, length);
181184

182185
folio_wait_fscache(folio);
186+
187+
if (!folio_test_private(folio))
188+
return;
189+
190+
finfo = netfs_folio_info(folio);
191+
192+
if (offset == 0 && length >= flen)
193+
goto erase_completely;
194+
195+
if (finfo) {
196+
/* We have a partially uptodate page from a streaming write. */
197+
unsigned int fstart = finfo->dirty_offset;
198+
unsigned int fend = fstart + finfo->dirty_len;
199+
unsigned int end = offset + length;
200+
201+
if (offset >= fend)
202+
return;
203+
if (end <= fstart)
204+
return;
205+
if (offset <= fstart && end >= fend)
206+
goto erase_completely;
207+
if (offset <= fstart && end > fstart)
208+
goto reduce_len;
209+
if (offset > fstart && end >= fend)
210+
goto move_start;
211+
/* A partial write was split. The caller has already zeroed
212+
* it, so just absorb the hole.
213+
*/
214+
}
215+
return;
216+
217+
erase_completely:
218+
netfs_put_group(netfs_folio_group(folio));
219+
folio_detach_private(folio);
220+
folio_clear_uptodate(folio);
221+
kfree(finfo);
222+
return;
223+
reduce_len:
224+
finfo->dirty_len = offset + length - finfo->dirty_offset;
225+
return;
226+
move_start:
227+
finfo->dirty_len -= offset - finfo->dirty_offset;
228+
finfo->dirty_offset = offset;
183229
}
184230
EXPORT_SYMBOL(netfs_invalidate_folio);
185231

include/linux/netfs.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,47 @@ struct netfs_inode {
140140
#define NETFS_ICTX_ODIRECT 0 /* The file has DIO in progress */
141141
};
142142

143+
/*
144+
* A netfs group - for instance a ceph snap. This is marked on dirty pages and
145+
* pages marked with a group must be flushed before they can be written under
146+
* the domain of another group.
147+
*/
148+
struct netfs_group {
149+
refcount_t ref;
150+
void (*free)(struct netfs_group *netfs_group);
151+
};
152+
153+
/*
154+
* Information about a dirty page (attached only if necessary).
155+
* folio->private
156+
*/
157+
struct netfs_folio {
158+
struct netfs_group *netfs_group; /* Filesystem's grouping marker (or NULL). */
159+
unsigned int dirty_offset; /* Write-streaming dirty data offset */
160+
unsigned int dirty_len; /* Write-streaming dirty data length */
161+
};
162+
#define NETFS_FOLIO_INFO 0x1UL /* OR'd with folio->private. */
163+
164+
static inline struct netfs_folio *netfs_folio_info(struct folio *folio)
165+
{
166+
void *priv = folio_get_private(folio);
167+
168+
if ((unsigned long)priv & NETFS_FOLIO_INFO)
169+
return (struct netfs_folio *)((unsigned long)priv & ~NETFS_FOLIO_INFO);
170+
return NULL;
171+
}
172+
173+
static inline struct netfs_group *netfs_folio_group(struct folio *folio)
174+
{
175+
struct netfs_folio *finfo;
176+
void *priv = folio_get_private(folio);
177+
178+
finfo = netfs_folio_info(folio);
179+
if (finfo)
180+
return finfo->netfs_group;
181+
return priv;
182+
}
183+
143184
/*
144185
* Resources required to do operations on a cache.
145186
*/

0 commit comments

Comments
 (0)