Skip to content

Commit 100ccd1

Browse files
committed
netfs: Optimise away reads above the point at which there can be no data
Track the file position above which the server is not expected to have any data (the "zero point") and preemptively assume that we can satisfy requests by filling them with zeroes locally rather than attempting to download them if they're over that line - even if we've written data back to the server. Assume that any data that was written back above that position is held in the local cache. Note that we have to split requests that straddle the line. Make use of this to optimise away some reads from the server. We need to set the zero point in the following circumstances: (1) When we see an extant remote inode and have no cache for it, we set the zero_point to i_size. (2) On local inode creation, we set zero_point to 0. (3) On local truncation down, we reduce zero_point to the new i_size if the new i_size is lower. (4) On local truncation up, we don't change zero_point. (5) On local modification, we don't change zero_point. (6) On remote invalidation, we set zero_point to the new i_size. (7) If stored data is discarded from the pagecache or culled from fscache, we must set zero_point above that if the data also got written to the server. (8) If dirty data is written back to the server, but not fscache, we must set zero_point above that. (9) If a direct I/O write is made, set zero_point above that. Assuming the above, any read from the server at or above the zero_point position will return all zeroes. The zero_point value can be stored in the cache, provided the above rules are applied to it by any code that culls part of the local cache. Signed-off-by: David Howells <dhowells@redhat.com> cc: Jeff Layton <jlayton@kernel.org> cc: linux-cachefs@redhat.com cc: linux-fsdevel@vger.kernel.org cc: linux-mm@kvack.org
1 parent 41d8e76 commit 100ccd1

File tree

11 files changed

+58
-20
lines changed

11 files changed

+58
-20
lines changed

fs/9p/vfs_inode.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ void v9fs_free_inode(struct inode *inode)
249249
static void v9fs_set_netfs_context(struct inode *inode)
250250
{
251251
struct v9fs_inode *v9inode = V9FS_I(inode);
252-
netfs_inode_init(&v9inode->netfs, &v9fs_req_ops);
252+
netfs_inode_init(&v9inode->netfs, &v9fs_req_ops, true);
253253
}
254254

255255
int v9fs_init_inode(struct v9fs_session_info *v9ses,

fs/afs/dynroot.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ struct inode *afs_iget_pseudo_dir(struct super_block *sb, bool root)
7676
/* there shouldn't be an existing inode */
7777
BUG_ON(!(inode->i_state & I_NEW));
7878

79-
netfs_inode_init(&vnode->netfs, NULL);
79+
netfs_inode_init(&vnode->netfs, NULL, false);
8080
inode->i_size = 0;
8181
inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
8282
if (root) {

fs/afs/inode.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static noinline void dump_vnode(struct afs_vnode *vnode, struct afs_vnode *paren
5858
*/
5959
static void afs_set_netfs_context(struct afs_vnode *vnode)
6060
{
61-
netfs_inode_init(&vnode->netfs, &afs_req_ops);
61+
netfs_inode_init(&vnode->netfs, &afs_req_ops, true);
6262
}
6363

6464
/*
@@ -168,6 +168,7 @@ static void afs_apply_status(struct afs_operation *op,
168168
struct inode *inode = &vnode->netfs.inode;
169169
struct timespec64 t;
170170
umode_t mode;
171+
bool unexpected_jump = false;
171172
bool data_changed = false;
172173
bool change_size = vp->set_size;
173174

@@ -231,6 +232,7 @@ static void afs_apply_status(struct afs_operation *op,
231232
}
232233
change_size = true;
233234
data_changed = true;
235+
unexpected_jump = true;
234236
} else if (vnode->status.type == AFS_FTYPE_DIR) {
235237
/* Expected directory change is handled elsewhere so
236238
* that we can locally edit the directory and save on a
@@ -252,6 +254,8 @@ static void afs_apply_status(struct afs_operation *op,
252254
vnode->netfs.remote_i_size = status->size;
253255
if (change_size || status->size > i_size_read(inode)) {
254256
afs_set_i_size(vnode, status->size);
257+
if (unexpected_jump)
258+
vnode->netfs.zero_point = status->size;
255259
inode_set_ctime_to_ts(inode, t);
256260
inode_set_atime_to_ts(inode, t);
257261
}
@@ -865,17 +869,17 @@ static void afs_setattr_success(struct afs_operation *op)
865869
static void afs_setattr_edit_file(struct afs_operation *op)
866870
{
867871
struct afs_vnode_param *vp = &op->file[0];
868-
struct inode *inode = &vp->vnode->netfs.inode;
872+
struct afs_vnode *vnode = vp->vnode;
869873

870874
if (op->setattr.attr->ia_valid & ATTR_SIZE) {
871875
loff_t size = op->setattr.attr->ia_size;
872876
loff_t i_size = op->setattr.old_i_size;
873877

874-
if (size < i_size)
875-
truncate_pagecache(inode, size);
876-
if (size != i_size)
877-
fscache_resize_cookie(afs_vnode_cache(vp->vnode),
878-
vp->scb.status.size);
878+
if (size != i_size) {
879+
truncate_setsize(&vnode->netfs.inode, size);
880+
netfs_resize_file(&vnode->netfs, size, true);
881+
fscache_resize_cookie(afs_vnode_cache(vnode), size);
882+
}
879883
}
880884
}
881885

@@ -943,11 +947,11 @@ int afs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
943947
*/
944948
if (!(attr->ia_valid & (supported & ~ATTR_SIZE & ~ATTR_MTIME)) &&
945949
attr->ia_size < i_size &&
946-
attr->ia_size > vnode->status.size) {
947-
truncate_pagecache(inode, attr->ia_size);
950+
attr->ia_size > vnode->netfs.remote_i_size) {
951+
truncate_setsize(inode, attr->ia_size);
952+
netfs_resize_file(&vnode->netfs, size, false);
948953
fscache_resize_cookie(afs_vnode_cache(vnode),
949954
attr->ia_size);
950-
i_size_write(inode, attr->ia_size);
951955
ret = 0;
952956
goto out_unlock;
953957
}

fs/ceph/inode.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ struct inode *ceph_alloc_inode(struct super_block *sb)
574574
doutc(fsc->client, "%p\n", &ci->netfs.inode);
575575

576576
/* Set parameters for the netfs library */
577-
netfs_inode_init(&ci->netfs, &ceph_netfs_ops);
577+
netfs_inode_init(&ci->netfs, &ceph_netfs_ops, false);
578578

579579
spin_lock_init(&ci->i_ceph_lock);
580580

fs/netfs/buffered_write.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static enum netfs_how_to_modify netfs_how_to_modify(struct netfs_inode *ctx,
7373
if (folio_test_uptodate(folio))
7474
return NETFS_FOLIO_IS_UPTODATE;
7575

76-
if (pos >= ctx->remote_i_size)
76+
if (pos >= ctx->zero_point)
7777
return NETFS_MODIFY_AND_CLEAR;
7878

7979
if (!maybe_trouble && offset == 0 && len >= flen)

fs/netfs/direct_write.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ ssize_t netfs_unbuffered_write_iter(struct kiocb *iocb, struct iov_iter *from)
134134
struct file *file = iocb->ki_filp;
135135
struct inode *inode = file->f_mapping->host;
136136
struct netfs_inode *ictx = netfs_inode(inode);
137+
unsigned long long end;
137138
ssize_t ret;
138139

139140
_enter("%llx,%zx,%llx", iocb->ki_pos, iov_iter_count(from), i_size_read(inode));
@@ -155,6 +156,9 @@ ssize_t netfs_unbuffered_write_iter(struct kiocb *iocb, struct iov_iter *from)
155156
ret = kiocb_invalidate_pages(iocb, iov_iter_count(from));
156157
if (ret < 0)
157158
goto out;
159+
end = iocb->ki_pos + iov_iter_count(from);
160+
if (end > ictx->zero_point)
161+
ictx->zero_point = end;
158162

159163
fscache_invalidate(netfs_i_cookie(ictx), NULL, i_size_read(inode),
160164
FSCACHE_INVAL_DIO_WRITE);

fs/netfs/io.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ netfs_rreq_prepare_read(struct netfs_io_request *rreq,
569569
struct iov_iter *io_iter)
570570
{
571571
enum netfs_io_source source = NETFS_DOWNLOAD_FROM_SERVER;
572+
struct netfs_inode *ictx = netfs_inode(rreq->inode);
572573
size_t lsize;
573574

574575
_enter("%llx-%llx,%llx", subreq->start, subreq->start + subreq->len, rreq->i_size);
@@ -586,6 +587,14 @@ netfs_rreq_prepare_read(struct netfs_io_request *rreq,
586587
* to make serial calls, it can indicate a short read and then
587588
* we will call it again.
588589
*/
590+
if (rreq->origin != NETFS_DIO_READ) {
591+
if (subreq->start >= ictx->zero_point) {
592+
source = NETFS_FILL_WITH_ZEROES;
593+
goto set;
594+
}
595+
if (subreq->len > ictx->zero_point - subreq->start)
596+
subreq->len = ictx->zero_point - subreq->start;
597+
}
589598
if (subreq->len > rreq->i_size - subreq->start)
590599
subreq->len = rreq->i_size - subreq->start;
591600
if (rreq->rsize && subreq->len > rreq->rsize)
@@ -607,6 +616,7 @@ netfs_rreq_prepare_read(struct netfs_io_request *rreq,
607616
}
608617
}
609618

619+
set:
610620
if (subreq->len > rreq->len)
611621
pr_warn("R=%08x[%u] SREQ>RREQ %zx > %zx\n",
612622
rreq->debug_id, subreq->debug_index,

fs/netfs/misc.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,11 @@ EXPORT_SYMBOL(netfs_invalidate_folio);
240240
bool netfs_release_folio(struct folio *folio, gfp_t gfp)
241241
{
242242
struct netfs_inode *ctx = netfs_inode(folio_inode(folio));
243+
unsigned long long end;
244+
245+
end = folio_pos(folio) + folio_size(folio);
246+
if (end > ctx->zero_point)
247+
ctx->zero_point = end;
243248

244249
if (folio_test_private(folio))
245250
return false;

fs/nfs/fscache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ static inline void nfs_netfs_put(struct nfs_netfs_io_data *netfs)
8080
}
8181
static inline void nfs_netfs_inode_init(struct nfs_inode *nfsi)
8282
{
83-
netfs_inode_init(&nfsi->netfs, &nfs_netfs_ops);
83+
netfs_inode_init(&nfsi->netfs, &nfs_netfs_ops, false);
8484
}
8585
extern void nfs_netfs_initiate_read(struct nfs_pgio_header *hdr);
8686
extern void nfs_netfs_read_completion(struct nfs_pgio_header *hdr);

fs/smb/client/cifsfs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,7 +1220,7 @@ static int cifs_precopy_set_eof(struct inode *src_inode, struct cifsInodeInfo *s
12201220
if (rc < 0)
12211221
goto set_failed;
12221222

1223-
netfs_resize_file(&src_cifsi->netfs, src_end);
1223+
netfs_resize_file(&src_cifsi->netfs, src_end, true);
12241224
fscache_resize_cookie(cifs_inode_cookie(src_inode), src_end);
12251225
return 0;
12261226

@@ -1351,7 +1351,7 @@ static loff_t cifs_remap_file_range(struct file *src_file, loff_t off,
13511351
smb_file_src, smb_file_target, off, len, destoff);
13521352
if (rc == 0 && new_size > i_size_read(target_inode)) {
13531353
truncate_setsize(target_inode, new_size);
1354-
netfs_resize_file(&target_cifsi->netfs, new_size);
1354+
netfs_resize_file(&target_cifsi->netfs, new_size, true);
13551355
fscache_resize_cookie(cifs_inode_cookie(target_inode),
13561356
new_size);
13571357
}

0 commit comments

Comments
 (0)