Change netfs_writeback_single() so that the function is now given an explicit content length in addition to the buffer iterator. The buffer must be rounded out to the cache DIO block size and netfslib will then draw on this excess space to write padding into the cache beyond the content length specified. The caller must also now clear the padding space. AFS directory code is modified to comply with this, including allocating pre-zeroed buffer space and clearing excess buffer space if the content is shrunk by a third party. It's not done during writeback as that may happen many times for a directory. AFS symlink code, however, is modified such that the clearing is done when the content is written to the cache (expected to be a one-off per symlink, if it happens). A further patch will make better use of the two different lengths. Signed-off-by: David Howells cc: Paulo Alcantara cc: Matthew Wilcox cc: Marc Dionne cc: Christoph Hellwig cc: linux-afs@lists.infradead.org cc: netfs@lists.linux.dev cc: linux-fsdevel@vger.kernel.org --- Documentation/filesystems/netfs_library.rst | 47 +++++++++++++++------ fs/afs/dir.c | 17 ++++++-- fs/afs/dir_edit.c | 2 +- fs/afs/symlink.c | 16 ++++++- fs/netfs/write_issue.c | 13 ++++-- include/linux/netfs.h | 2 +- 6 files changed, 73 insertions(+), 24 deletions(-) diff --git a/Documentation/filesystems/netfs_library.rst b/Documentation/filesystems/netfs_library.rst index 215a6621f3d3..1fe110ceb3f1 100644 --- a/Documentation/filesystems/netfs_library.rst +++ b/Documentation/filesystems/netfs_library.rst @@ -437,22 +437,43 @@ Monolithic Files API There is also a special API set for files for which the content must be read in a single RPC (and not written back) and is maintained as a monolithic blob -(e.g. an AFS directory), though it can be stored and updated in the local cache:: +(e.g. an AFS directory), though it can be stored and updated in the local +cache. + +To start with, there's a function to synchronously read the contents of such a +file into a buffer in a single operation:: + + ssize_t netfs_read_single(struct inode *inode, + struct file *file, + struct iov_iter *iter); + +If such a file is modified locally, it can be marked dirty with:: - ssize_t netfs_read_single(struct inode *inode, struct file *file, struct iov_iter *iter); void netfs_single_mark_inode_dirty(struct inode *inode); + +which will schedule it for writeback later. When the writeback eventually +happens, the filesystem should call:: + int netfs_writeback_single(struct address_space *mapping, struct writeback_control *wbc, - struct iov_iter *iter); + struct iov_iter *iter, + size_t len); + +to write the data to the cache from its writepages method. -The first function reads from a file into the given buffer, reading from the -cache in preference if the data is cached there; the second function allows the -inode to be marked dirty, causing a later writeback; and the third function can -be called from the writeback code to write the data to the cache, if there is -one. +In both the read and writeback functions, the buffer needs to be rounded up to +a multiple of the cache DIO size, with ``iter->count`` indicating the size of +the buffer. For the write function, ``len`` should indicate the size of the +content which may be less than the buffer size. The gap between ``len`` and the +buffer size should be cleared by the filesystem as this will be used as padding +if the write size has to be increased (e.g. for the cache DIO). + +Note that, for the moment, rounding the buffer up to a multiple of PAGE_SIZE +instead of the cache DIO size should suffice. DIO size > PAGE_SIZE is not +currently supported. The inode should be marked ``NETFS_ICTX_SINGLE_NO_UPLOAD`` if this API is to be -used. The writeback function requires the buffer to be of ITER_FOLIOQ type. +used. Clearing Stale Post-EOF Pagecache --------------------------------- @@ -859,12 +880,12 @@ The methods defined in the table are: paths to ask the filesystem to update its idea of the file size. If not given, netfslib will set i_size and i_blocks and update the local cache cookie. - + * ``post_modify()`` [Optional] This is called after netfslib writes to the pagecache or when it allows an mmap'd page to be marked as writable. - + * ``begin_writeback()`` [Optional] Netfslib calls this when processing a writeback request if it @@ -872,7 +893,7 @@ The methods defined in the table are: indicating it must be written to the server. This allows the filesystem to only set up writeback resources when it knows it's going to have to perform a write. - + * ``prepare_write()`` [Optional] This is called to allow the filesystem to limit the size of a @@ -911,7 +932,7 @@ The methods defined in the table are: allows the filesystem to examine the state of the request, the subrequests in the indicated stream and of its own data and make adjustments or renegotiate resources. - + * ``invalidate_cache()`` [Optional] This is called by netfslib to invalidate data stored in the local diff --git a/fs/afs/dir.c b/fs/afs/dir.c index 75f8f70cfeff..51c158d8f723 100644 --- a/fs/afs/dir.c +++ b/fs/afs/dir.c @@ -252,7 +252,8 @@ static ssize_t afs_do_read_single(struct afs_vnode *dvnode, struct file *file) ret = netfs_alloc_folioq_buffer(NULL, &dvnode->directory, &cur_size, i_size, - mapping_gfp_mask(dvnode->netfs.inode.i_mapping)); + mapping_gfp_mask(dvnode->netfs.inode.i_mapping) | + __GFP_ZERO); dvnode->directory_size = cur_size; if (ret < 0) return ret; @@ -278,6 +279,13 @@ static ssize_t afs_do_read_single(struct afs_vnode *dvnode, struct file *file) if (ret2 < 0) ret = ret2; } + + if (ret >= 0 && ret < dvnode->directory_size) { + iov_iter_folio_queue(&iter, ITER_DEST, dvnode->directory, + 0, 0, dvnode->directory_size); + iov_iter_advance(&iter, ret); + iov_iter_zero(dvnode->directory_size - ret, &iter); + } } return ret; @@ -2228,9 +2236,10 @@ static int afs_dir_writepages(struct address_space *mapping, } if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) { - iov_iter_folio_queue(&iter, ITER_SOURCE, dvnode->directory, 0, 0, - i_size_read(&dvnode->netfs.inode)); - ret = netfs_writeback_single(mapping, wbc, &iter); + iov_iter_folio_queue(&iter, ITER_SOURCE, dvnode->directory, + 0, 0, dvnode->directory_size); + ret = netfs_writeback_single(mapping, wbc, &iter, + i_size_read(&dvnode->netfs.inode)); if (ret == 1) ret = 0; /* Skipped write due to lock conflict. */ } diff --git a/fs/afs/dir_edit.c b/fs/afs/dir_edit.c index c31303059444..185e8819e424 100644 --- a/fs/afs/dir_edit.c +++ b/fs/afs/dir_edit.c @@ -122,7 +122,7 @@ static union afs_xdr_dir_block *afs_dir_get_block(struct afs_dir_iter *iter, siz ret = netfs_alloc_folioq_buffer( NULL, &dvnode->directory, &cur_size, blend, - mapping_gfp_mask(dvnode->netfs.inode.i_mapping)); + mapping_gfp_mask(dvnode->netfs.inode.i_mapping) | __GFP_ZERO); dvnode->directory_size = cur_size; if (ret < 0) goto fail; diff --git a/fs/afs/symlink.c b/fs/afs/symlink.c index a1dbd552ddff..c18e4f806423 100644 --- a/fs/afs/symlink.c +++ b/fs/afs/symlink.c @@ -228,6 +228,10 @@ int afs_readlink(struct dentry *dentry, char __user *buffer, int buflen) /* * Write the symlink contents to the cache as a single blob. We then throw * away the page we used to receive it. + * + * Note that the buffer tail is cleared under a shared lock on ->validate_lock + * - though hopefully there won't be racing writers since the only + * 'modification' is to be either read from the server or locally created. */ int afs_symlink_writepages(struct address_space *mapping, struct writeback_control *wbc) @@ -247,9 +251,17 @@ int afs_symlink_writepages(struct address_space *mapping, if (vnode->directory && atomic64_read(&vnode->cb_expires_at) != AFS_NO_CB_PROMISE) { + size_t i_size = i_size_read(&vnode->netfs.inode); + + if (i_size < vnode->directory_size) { + iov_iter_folio_queue(&iter, ITER_DEST, vnode->directory, 0, 0, + vnode->directory_size); + iov_iter_advance(&iter, i_size); + iov_iter_zero(vnode->directory_size - i_size, &iter); + } iov_iter_folio_queue(&iter, ITER_SOURCE, vnode->directory, 0, 0, - i_size_read(&vnode->netfs.inode)); - ret = netfs_writeback_single(mapping, wbc, &iter); + vnode->directory_size); + ret = netfs_writeback_single(mapping, wbc, &iter, i_size); } if (ret == 0) { diff --git a/fs/netfs/write_issue.c b/fs/netfs/write_issue.c index f23364116421..63b0e2599c3c 100644 --- a/fs/netfs/write_issue.c +++ b/fs/netfs/write_issue.c @@ -749,26 +749,33 @@ static int netfs_write_folio_single(struct netfs_io_request *wreq, * netfs_writeback_single - Write back a monolithic payload * @mapping: The mapping to write from * @wbc: Hints from the VM - * @iter: Data to write, must be ITER_FOLIOQ. + * @iter: Data buffer to write from, must be ITER_FOLIOQ. + * @len: Amount of data to write. * * Write a monolithic, non-pagecache object back to the server and/or * the cache. * + * The data buffer may be bigger than @len (as set in @iter's count), and if + * so, this function will use the extra as needed to provide padding into the + * cache to handle DIO rounding, so the caller should clear it. + * * Return: 0 if successful; 1 if skipped due to lock conflict and WB_SYNC_NONE; * or a negative error code. */ int netfs_writeback_single(struct address_space *mapping, struct writeback_control *wbc, - struct iov_iter *iter) + struct iov_iter *iter, size_t len) { struct netfs_io_request *wreq; struct netfs_inode *ictx = netfs_inode(mapping->host); struct folio_queue *fq; - size_t size = iov_iter_count(iter); + size_t size = len; int ret; if (WARN_ON_ONCE(!iov_iter_is_folioq(iter))) return -EIO; + if (WARN_ON_ONCE(iov_iter_count(iter) < len)) + return -EIO; if (!netfs_wb_begin(ictx, wbc->sync_mode == WB_SYNC_NONE)) { /* The VFS will have undirtied the inode. */ diff --git a/include/linux/netfs.h b/include/linux/netfs.h index 2cf1163aafa6..375fd415b4e5 100644 --- a/include/linux/netfs.h +++ b/include/linux/netfs.h @@ -430,7 +430,7 @@ void netfs_single_mark_inode_dirty(struct inode *inode); ssize_t netfs_read_single(struct inode *inode, struct file *file, struct iov_iter *iter); int netfs_writeback_single(struct address_space *mapping, struct writeback_control *wbc, - struct iov_iter *iter); + struct iov_iter *iter, size_t len); /* Address operations API */ struct readahead_control;