Skip to content

Commit 3eefc0c

Browse files
author
Darrick J. Wong
committed
xfs: fix freemap adjustments when adding xattrs to leaf blocks
xfs/592 and xfs/794 both trip this assertion in the leaf block freemap adjustment code after ~20 minutes of running on my test VMs: ASSERT(ichdr->firstused >= ichdr->count * sizeof(xfs_attr_leaf_entry_t) + xfs_attr3_leaf_hdr_size(leaf)); Upon enabling quite a lot more debugging code, I narrowed this down to fsstress trying to set a local extended attribute with namelen=3 and valuelen=71. This results in an entry size of 80 bytes. At the start of xfs_attr3_leaf_add_work, the freemap looks like this: i 0 base 448 size 0 rhs 448 count 46 i 1 base 388 size 132 rhs 448 count 46 i 2 base 2120 size 4 rhs 448 count 46 firstused = 520 where "rhs" is the first byte past the end of the leaf entry array. This is inconsistent -- the entries array ends at byte 448, but freemap[1] says there's free space starting at byte 388! By the end of the function, the freemap is in worse shape: i 0 base 456 size 0 rhs 456 count 47 i 1 base 388 size 52 rhs 456 count 47 i 2 base 2120 size 4 rhs 456 count 47 firstused = 440 Important note: 388 is not aligned with the entries array element size of 8 bytes. Based on the incorrect freemap, the name area starts at byte 440, which is below the end of the entries array! That's why the assertion triggers and the filesystem shuts down. How did we end up here? First, recall from the previous patch that the freemap array in an xattr leaf block is not intended to be a comprehensive map of all free space in the leaf block. In other words, it's perfectly legal to have a leaf block with: * 376 bytes in use by the entries array * freemap[0] has [base = 376, size = 8] * freemap[1] has [base = 388, size = 1500] * the space between 376 and 388 is free, but the freemap stopped tracking that some time ago If we add one xattr, the entries array grows to 384 bytes, and freemap[0] becomes [base = 384, size = 0]. So far, so good. But if we add a second xattr, the entries array grows to 392 bytes, and freemap[0] gets pushed up to [base = 392, size = 0]. This is bad, because freemap[1] hasn't been updated, and now the entries array and the free space claim the same space. The fix here is to adjust all freemap entries so that none of them collide with the entries array. Note that this fix relies on commit 2a2b593 ("xfs: fix attr leaf header freemap.size underflow") and the previous patch that resets zero length freemap entries to have base = 0. Cc: <stable@vger.kernel.org> # v2.6.12 Fixes: 1da177e ("Linux-2.6.12-rc2") Signed-off-by: "Darrick J. Wong" <djwong@kernel.org> Reviewed-by: Christoph Hellwig <hch@lst.de>
1 parent 6f13c1d commit 3eefc0c

1 file changed

Lines changed: 28 additions & 8 deletions

File tree

fs/xfs/libxfs/xfs_attr_leaf.c

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,6 +1476,7 @@ xfs_attr3_leaf_add_work(
14761476
struct xfs_attr_leaf_name_local *name_loc;
14771477
struct xfs_attr_leaf_name_remote *name_rmt;
14781478
struct xfs_mount *mp;
1479+
int old_end, new_end;
14791480
int tmp;
14801481
int i;
14811482

@@ -1568,17 +1569,36 @@ xfs_attr3_leaf_add_work(
15681569
if (be16_to_cpu(entry->nameidx) < ichdr->firstused)
15691570
ichdr->firstused = be16_to_cpu(entry->nameidx);
15701571

1571-
ASSERT(ichdr->firstused >= ichdr->count * sizeof(xfs_attr_leaf_entry_t)
1572-
+ xfs_attr3_leaf_hdr_size(leaf));
1573-
tmp = (ichdr->count - 1) * sizeof(xfs_attr_leaf_entry_t)
1574-
+ xfs_attr3_leaf_hdr_size(leaf);
1572+
new_end = ichdr->count * sizeof(struct xfs_attr_leaf_entry) +
1573+
xfs_attr3_leaf_hdr_size(leaf);
1574+
old_end = new_end - sizeof(struct xfs_attr_leaf_entry);
1575+
1576+
ASSERT(ichdr->firstused >= new_end);
15751577

15761578
for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
1577-
if (ichdr->freemap[i].base == tmp) {
1578-
ichdr->freemap[i].base += sizeof(xfs_attr_leaf_entry_t);
1579+
int diff = 0;
1580+
1581+
if (ichdr->freemap[i].base == old_end) {
1582+
/*
1583+
* This freemap entry starts at the old end of the
1584+
* leaf entry array, so we need to adjust its base
1585+
* upward to accomodate the larger array.
1586+
*/
1587+
diff = sizeof(struct xfs_attr_leaf_entry);
1588+
} else if (ichdr->freemap[i].size > 0 &&
1589+
ichdr->freemap[i].base < new_end) {
1590+
/*
1591+
* This freemap entry starts in the space claimed by
1592+
* the new leaf entry. Adjust its base upward to
1593+
* reflect that.
1594+
*/
1595+
diff = new_end - ichdr->freemap[i].base;
1596+
}
1597+
1598+
if (diff) {
1599+
ichdr->freemap[i].base += diff;
15791600
ichdr->freemap[i].size -=
1580-
min_t(uint16_t, ichdr->freemap[i].size,
1581-
sizeof(xfs_attr_leaf_entry_t));
1601+
min_t(uint16_t, ichdr->freemap[i].size, diff);
15821602
}
15831603

15841604
/*

0 commit comments

Comments
 (0)