[PATCH v3 7/9] ocfs2: copy all xattr names and values when converting a block
From: Siddh Raman Pant
Date: Sat Dec 05 2026 - 03:10:07 EST
ocfs2_cp_xattr_block_to_bucket() starts copying names and values at
the offset in the last entry. Resizing an earlier xattr can move its
name and value to a lower offset without changing its position in the
entry array. If the block then fills up, conversion skips those bytes
and the resized attribute can no longer be found.
Find the lowest name offset across all entries, as the block allocator
already does, and copy from there to the end of the block.
Fixes: 012255961c9e ("ocfs2: Enable xattr set in index btree")
Signed-off-by: Siddh Raman Pant <siddh.raman.pant@xxxxxxxxxx>
---
fs/ocfs2/xattr.c | 27 +++++++++++++--------------
1 file changed, 13 insertions(+), 14 deletions(-)
diff --git a/fs/ocfs2/xattr.c b/fs/ocfs2/xattr.c
index 75036d0fd9b6..2ea5a3c036bf 100644
--- a/fs/ocfs2/xattr.c
+++ b/fs/ocfs2/xattr.c
@@ -1983,11 +1983,10 @@ static int ocfs2_xa_block_can_reuse(struct ocfs2_xa_loc *loc,
namevalue_size_xi(xi);
}
-static int ocfs2_xa_block_get_free_start(struct ocfs2_xa_loc *loc)
+static int ocfs2_xattr_min_offset(struct ocfs2_xattr_header *xh, int free_start)
{
- struct ocfs2_xattr_header *xh = loc->xl_header;
int i, count = le16_to_cpu(xh->xh_count);
- int offset, free_start = loc->xl_size;
+ int offset;
for (i = 0; i < count; i++) {
offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
@@ -1998,6 +1997,11 @@ static int ocfs2_xa_block_get_free_start(struct ocfs2_xa_loc *loc)
return free_start;
}
+static int ocfs2_xa_block_get_free_start(struct ocfs2_xa_loc *loc)
+{
+ return ocfs2_xattr_min_offset(loc->xl_header, loc->xl_size);
+}
+
static int ocfs2_xa_block_check_space(struct ocfs2_xa_loc *loc,
struct ocfs2_xattr_info *xi)
{
@@ -4710,7 +4714,6 @@ static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
int i, blocksize = inode->i_sb->s_blocksize;
int blks = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
u16 offset, size, off_change;
- struct ocfs2_xattr_entry *xe;
struct ocfs2_xattr_block *xb =
(struct ocfs2_xattr_block *)xb_bh->b_data;
struct ocfs2_xattr_header *xb_xh = &xb->xb_attrs.xb_header;
@@ -4727,11 +4730,8 @@ static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
memset(bucket_block(bucket, i), 0, blocksize);
/*
- * An empty block has no names or values to carry over, and the
- * xh_entries[count - 1] below would be xh_entries[-1]. Leave the
- * bucket empty, but carry over the hash ocfs2_xa_remove_entry() left
- * in the block's xh_entries[0] so that the bucket still holds the key
- * it is looked up by.
+ * An empty bucket still needs the hash ocfs2_xa_remove_entry()
+ * left in the block's first entry.
*/
if (!count) {
xh->xh_num_buckets = cpu_to_le16(1);
@@ -4742,13 +4742,12 @@ static void ocfs2_cp_xattr_block_to_bucket(struct inode *inode,
}
/*
- * Since the xe_name_offset is based on ocfs2_xattr_header,
- * there is a offset change corresponding to the change of
- * ocfs2_xattr_header's position.
+ * Resizing an xattr can move its name/value pair below the last
+ * entry's pair. Copy from the lowest name offset in the block.
*/
off_change = offsetof(struct ocfs2_xattr_block, xb_attrs.xb_header);
- xe = &xb_xh->xh_entries[count - 1];
- offset = le16_to_cpu(xe->xe_name_offset) + off_change;
+ offset = ocfs2_xattr_min_offset(xb_xh, blocksize - off_change);
+ offset += off_change;
size = blocksize - offset;
/* copy all the names and values. */
--
2.53.0