]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
pNFS: Fix extent encoding in block/scsi layout
authorSergey Bashirov <sergeybashirov@gmail.com>
Mon, 30 Jun 2025 18:35:27 +0000 (21:35 +0300)
committerTrond Myklebust <trond.myklebust@hammerspace.com>
Mon, 14 Jul 2025 22:20:28 +0000 (15:20 -0700)
The 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.

Co-developed-by: Konstantin Evtushenko <koevtushenko@yandex.com>
Signed-off-by: Konstantin Evtushenko <koevtushenko@yandex.com>
Signed-off-by: Sergey Bashirov <sergeybashirov@gmail.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Link: https://lore.kernel.org/r/20250630183537.196479-3-sergeybashirov@gmail.com
Signed-off-by: Trond Myklebust <trond.myklebust@hammerspace.com>
fs/nfs/blocklayout/extent_tree.c

index 0add0f329816b01602feb405260d52117dda5374..faccd5caa1492f65d6b9556bf79dad56b6bcbea9 100644 (file)
@@ -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);