pNFS: Fix layoutcommit handling in block/scsi driver
Subsystems (2)
cover pNFS: Fix layoutcommit handling in block/scsi driver
Depending on the allocation scheme and size, some files may have a large number of extents. This series fixes the handling of large extent arrays and allows the pNFS client to split large layoutcommits that exceed the maximum RPC size accepted by the server into multiple requests.
Manually tested for the block layout driver:
sudo mount.nfs4 -v -o minorversion=2,sync,hard,noatime, \
rsize=16384,wsize=16384,timeo=600, \
retrans=2 192.168.1.1:/mnt/export /mnt/pnfs
sudo fio --name=test --filename=/mnt/pnfs/test6.raw --size=10M \
--rw=randwrite --ioengine=libaio --direct=1 --bs=4k \
--iodepth=128 --fallocate=none --verify=sha1
Trace:
fio-1580 [012] 1258.595866: bl_ext_tree_prepare_commit:
ret=-28, found 372 ranges, lwb=1531903, not all ranges encoded
fio-1580 [012] 1258.609534: bl_ext_tree_prepare_commit:
ret=-28, found 372 ranges, lwb=3104767, not all ranges encoded
fio-1580 [012] 1258.655556: bl_ext_tree_prepare_commit:
ret=-28, found 372 ranges, lwb=4644863, not all ranges encoded
fio-1580 [012] 1258.699603: bl_ext_tree_prepare_commit:
ret=-28, found 372 ranges, lwb=6176767, not all ranges encoded
fio-1580 [012] 1258.743650: bl_ext_tree_prepare_commit:
ret=-28, found 372 ranges, lwb=7708671, not all ranges encoded
fio-1580 [012] 1258.787605: bl_ext_tree_prepare_commit:
ret=-28, found 372 ranges, lwb=9236479, not all ranges encoded
fio-1580 [012] 1258.817439: bl_ext_tree_prepare_commit:
ret=0, found 295 ranges, lwb=10485759---
Sergey Bashirov (4):
pNFS: Fix uninited ptr deref in block/scsi layout
pNFS: Fix extent encoding in block/scsi layout
pNFS: Add prepare commit trace to block/scsi layout
pNFS: Handle RPC size limit for layoutcommits
fs/nfs/blocklayout/extent_tree.c | 104 +++++++++++++++++++++++++++---- fs/nfs/nfs4trace.c | 1 + fs/nfs/nfs4trace.h | 34 ++++++++++ fs/nfs/pnfs.c | 11 +++- 4 files changed, 134 insertions(+), 16 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/4 pNFS: Fix uninited ptr deref in block/scsi layout
The error occurs on the third attempt to encode extents. When function ext_tree_prepare_commit() reallocates a larger buffer to retry encoding extents, the "layoutupdate_pages" page array is initialized only after the retry loop. But ext_tree_free_commitdata() is called on every iteration and tries to put pages in the array, thus dereferencing uninitialized pointers.
An additional problem is that there is no limit on the maximum possible buffer_size. When there are too many extents, the client may create a layoutcommit that is larger than the maximum possible RPC size accepted by the server.
During testing, we observed two typical scenarios. First, one memory page for extents is enough when we work with small files, append data to the end of the file, or preallocate extents before writing. But when we fill a new large file without preallocating, the number of extents can be huge, and counting the number of written extents in ext_tree_encode_commit() does not help much. Since this number increases even more between unlocking and locking of ext_tree, the reallocated buffer may not be large enough again and again.
--- fs/nfs/blocklayout/extent_tree.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-)
fs/nfs/blocklayout/extent_tree.c +15 -5
@@ -552,6 +552,15 @@ static int ext_tree_encode_commit(struct pnfs_block_layout *bl, __be32 *p,return ret; } +/** +* ext_tree_prepare_commit - encode extents that need to be committed +* @arg: layout commit data +* +* Return values: +* %0: Success, all required extents are encoded +* %-ENOSPC: Some extents are encoded, but not all, due to RPC size limit +* %-ENOMEM: Out of memory, extents not encoded +*/ int ext_tree_prepare_commit (struct nfs4_layoutcommit_args *arg) {
@@ -568,12 +577,12 @@ ext_tree_prepare_commit(struct nfs4_layoutcommit_args *arg) start_p =page_address (arg->layoutupdate_page); arg->layoutupdate_pages = &arg->layoutupdate_page; -retry: - ret =ext_tree_encode_commit (bl, start_p +1 , buffer_size, &count, &arg->lastbytewritten); + ret =ext_tree_encode_commit (bl, start_p +1 , buffer_size, + &count, &arg->lastbytewritten);if (unlikely (ret)) {ext_tree_free_commitdata (arg, buffer_size); - buffer_size =ext_tree_layoutupdate_size (bl, count); + buffer_size =NFS_SERVER (arg->inode)->wsize; count =0 ; arg->layoutupdate_pages =
@@ -588,7 +597,8 @@ ext_tree_prepare_commit(struct nfs4_layoutcommit_args *arg)return -ENOMEM ; } - goto retry; + ret =ext_tree_encode_commit (bl, start_p +1 , buffer_size, + &count, &arg->lastbytewritten); } *start_p =cpu_to_be32 (count);
@@ -608,7 +618,7 @@ ext_tree_prepare_commit(struct nfs4_layoutcommit_args *arg) }dprintk ("%s found %zu ranges\n" , __func__, count); -return 0 ; +return ret; }void
Signature · 1 line
2.43.0
Re: [PATCH 1/4] pNFS: Fix uninited ptr deref in block/scsi layout
Looks good:
PATCH 2/4 pNFS: Fix extent encoding in block/scsi layout
In reply to Sergey BashirovThe ext_tree_encode_commit() function may be called multiple times for the same file, layout, and last written byte if the provided buffer is not large enough to encode all extents in it.
The first problem is that the last written byte field must be zeroed only on a successful call, otherwise we will lose its actual value and get an integer overflow on the next encoding attempt.
The second problem is that we can't count and encode in one pass. The extent state changes during encoding, so if we return -ENOSPC but have already encoded some extents into a small buffer, they will not be re-encoded into a new larger buffer on the next try. As a result, the client never commits these extents to the server.
--- fs/nfs/blocklayout/extent_tree.c | 80 +++++++++++++++++++++++++++++--- 1 file changed, 74 insertions(+), 6 deletions(-)
fs/nfs/blocklayout/extent_tree.c +74 -6
@@ -520,10 +520,71 @@ static __be32 *encode_scsi_range(struct pnfs_block_extent *be, __be32 *p)return xdr_encode_hyper (p, be->be_length <<SECTOR_SHIFT ); } -static int ext_tree_encode_commit (struct pnfs_block_layout *bl,__be32 *p, +/** +* ext_tree_try_encode_commit - try to encode all extents into the buffer +* @bl: pointer to the layout +* @p: pointer to the output buffer +* @buffer_size: size of the output buffer +* @count: output pointer to the number of encoded extents +* @lastbyte: output pointer to the last written byte +* +* Return values: +* %0: Success, all required extents encoded, outputs are valid +* %-ENOSPC: Buffer too small, nothing encoded, outputs are invalid +*/ +static int +ext_tree_try_encode_commit (struct pnfs_block_layout *bl,__be32 *p,size_t buffer_size,size_t *count,__u64 *lastbyte) {struct pnfs_block_extent *be; + +spin_lock (&bl->bl_ext_lock); +for (be =ext_tree_first (&bl->bl_ext_rw); be; be =ext_tree_next (be)) { +if (be->be_state !=PNFS_BLOCK_INVALID_DATA || + be->be_tag !=EXTENT_WRITTEN ) +continue ; + + (*count)++; +if (ext_tree_layoutupdate_size (bl, *count) > buffer_size) { +spin_unlock (&bl->bl_ext_lock); +return -ENOSPC ; + } + } +for (be =ext_tree_first (&bl->bl_ext_rw); be; be =ext_tree_next (be)) { +if (be->be_state !=PNFS_BLOCK_INVALID_DATA || + be->be_tag !=EXTENT_WRITTEN ) +continue ; + +if (bl->bl_scsi_layout) + p =encode_scsi_range (be, p); +else + p =encode_block_extent (be, p); + be->be_tag =EXTENT_COMMITTING ; + } + *lastbyte = (bl->bl_lwb !=0 ) ? bl->bl_lwb -1 :U64_MAX ; + bl->bl_lwb =0 ; +spin_unlock (&bl->bl_ext_lock); + +return 0 ; +} + +/** +* ext_tree_encode_commit - encode as much as possible extents into the buffer +* @bl: pointer to the layout +* @p: pointer to the output buffer +* @buffer_size: size of the output buffer +* @count: output pointer to the number of encoded extents +* @lastbyte: output pointer to the last written byte +* +* Return values: +* %0: Success, all required extents encoded, outputs are valid +* %-ENOSPC: Buffer too small, some extents are encoded, outputs are valid +*/ +static int +ext_tree_encode_commit (struct pnfs_block_layout *bl,__be32 *p, +size_t buffer_size,size_t *count,__u64 *lastbyte) +{ +struct pnfs_block_extent *be, *be_prev;int ret =0 ;spin_lock (&bl->bl_ext_lock);
@@ -534,9 +595,9 @@ static int ext_tree_encode_commit(struct pnfs_block_layout *bl, __be32 *p, (*count)++;if (ext_tree_layoutupdate_size (bl, *count) > buffer_size) { -/* keep counting.. */ + (*count)--; ret = -ENOSPC ; -continue ; +break ; }if (bl->bl_scsi_layout)
@@ -544,9 +605,16 @@ static int ext_tree_encode_commit(struct pnfs_block_layout *bl, __be32 *p,else p =encode_block_extent (be, p); be->be_tag =EXTENT_COMMITTING ; + be_prev = be; + } +if (!ret) { + *lastbyte = (bl->bl_lwb !=0 ) ? bl->bl_lwb -1 :U64_MAX ; + bl->bl_lwb =0 ; + }else { + *lastbyte = be_prev->be_f_offset + be_prev->be_length; + *lastbyte <<=SECTOR_SHIFT ; + *lastbyte -=1 ; } - *lastbyte = bl->bl_lwb -1 ; - bl->bl_lwb =0 ;spin_unlock (&bl->bl_ext_lock);return ret;
@@ -577,7 +645,7 @@ ext_tree_prepare_commit(struct nfs4_layoutcommit_args *arg) start_p =page_address (arg->layoutupdate_page); arg->layoutupdate_pages = &arg->layoutupdate_page; - ret =ext_tree_encode_commit (bl, start_p +1 , buffer_size, + ret =ext_tree_try_encode_commit (bl, start_p +1 , buffer_size, &count, &arg->lastbytewritten);if (unlikely (ret)) {ext_tree_free_commitdata (arg, buffer_size);
Signature · 1 line
2.43.0
Re: [PATCH 2/4] pNFS: Fix extent encoding in block/scsi layout
Looks good:
PATCH 3/4 pNFS: Add prepare commit trace to block/scsi layout
In reply to Sergey BashirovReplace dprintk with trace event in ext_tree_prepare_commit() function.
--- fs/nfs/blocklayout/extent_tree.c | 6 +++--- fs/nfs/nfs4trace.c | 1 + fs/nfs/nfs4trace.h | 34 ++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-)
fs/nfs/blocklayout/extent_tree.c +3 -3
@@ -6,6 +6,7 @@#include <linux/vmalloc.h> #include "blocklayout.h" +#include "../nfs4trace.h" #define NFSDBG_FACILITY NFSDBG_PNFS_LD
@@ -637,8 +638,6 @@ ext_tree_prepare_commit(struct nfs4_layoutcommit_args *arg)__be32 *start_p;int ret; -dprintk ("%s enter\n" , __func__); - arg->layoutupdate_page =alloc_page (GFP_NOFS );if (!arg->layoutupdate_page)return -ENOMEM ;
@@ -685,7 +684,8 @@ ext_tree_prepare_commit(struct nfs4_layoutcommit_args *arg) } } -dprintk ("%s found %zu ranges\n" , __func__, count); +trace_bl_ext_tree_prepare_commit (ret, count, + arg->lastbytewritten, !!ret);return ret; }
fs/nfs/nfs4trace.c +1 -0
@@ -31,6 +31,7 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(ff_layout_read_error);EXPORT_TRACEPOINT_SYMBOL_GPL (ff_layout_write_error);EXPORT_TRACEPOINT_SYMBOL_GPL (ff_layout_commit_error); +EXPORT_TRACEPOINT_SYMBOL_GPL (bl_ext_tree_prepare_commit);EXPORT_TRACEPOINT_SYMBOL_GPL (bl_pr_key_reg);EXPORT_TRACEPOINT_SYMBOL_GPL (bl_pr_key_reg_err);EXPORT_TRACEPOINT_SYMBOL_GPL (bl_pr_key_unreg);
fs/nfs/nfs4trace.h +34 -0
@@ -2163,6 +2163,40 @@ TRACE_EVENT(ff_layout_commit_error, ) ); +TRACE_EVENT (bl_ext_tree_prepare_commit, +TP_PROTO ( + int ret, + size_t count, + u64 lwb, + bool not_all_ranges + ), + +TP_ARGS (ret, count, lwb, not_all_ranges), + +TP_STRUCT__entry ( + __field(int, ret) + __field(size_t, count) + __field(u64, lwb) +__field (bool, not_all_ranges) + ), + +TP_fast_assign ( + __entry->ret = ret; + __entry->count = count; + __entry->lwb = lwb; + __entry->not_all_ranges = not_all_ranges; + ), + +TP_printk ( +"ret=%d, found %zu ranges, lwb=%llu%s" , + __entry->ret, + __entry->count, + __entry->lwb, + __entry->not_all_ranges ?", not all ranges encoded" : +"" + ) +); + DECLARE_EVENT_CLASS(pnfs_bl_pr_key_class, TP_PROTO( const struct block_device *bdev,
Signature · 1 line
2.43.0
Re: [PATCH 3/4] pNFS: Add prepare commit trace to block/scsi layout
Looks good:
PATCH 4/4 pNFS: Handle RPC size limit for layoutcommits
In reply to Sergey BashirovWhen there are too many block extents for a layoutcommit, they may not all fit into the maximum-sized RPC. This patch allows the generic pnfs code to properly handle -ENOSPC returned by the block/scsi layout driver and trigger additional layoutcommits if necessary.
--- fs/nfs/pnfs.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
fs/nfs/pnfs.c +8 -3
@@ -3338,6 +3338,7 @@ pnfs_layoutcommit_inode(struct inode *inode, bool sync)struct nfs_inode *nfsi =NFS_I (inode);loff_t end_pos;int status; +bool mark_as_dirty = false;if (!pnfs_layoutcommit_outstanding (inode))return 0 ;
@@ -3389,19 +3390,23 @@ pnfs_layoutcommit_inode(struct inode *inode, bool sync)if (ld->prepare_layoutcommit) { status = ld->prepare_layoutcommit (&data->args);if (status) { -put_cred (data->cred); +if (status != -ENOSPC ) +put_cred (data->cred);spin_lock (&inode->i_lock);set_bit (NFS_INO_LAYOUTCOMMIT , &nfsi->flags);if (end_pos > nfsi->layout->plh_lwb) nfsi->layout->plh_lwb = end_pos; - goto out_unlock; +if (status != -ENOSPC ) + goto out_unlock; +spin_unlock (&inode->i_lock); + mark_as_dirty = true; } } status =nfs4_proc_layoutcommit (data, sync); out: -if (status) +if (status || mark_as_dirty)mark_inode_dirty_sync (inode);dprintk ("<-- %s status %d\n" , __func__, status);return status;
Signature · 1 line
2.43.0
Re: [PATCH 4/4] pNFS: Handle RPC size limit for layoutcommits
Looks good: