Skip to content

Commit 6d7b4bc

Browse files
author
Darrick J. Wong
committed
xfs: update btree keys correctly when _insrec splits an inode root block
In commit 2c813ad, I partially fixed a bug wherein xfs_btree_insrec would erroneously try to update the parent's key for a block that had been split if we decided to insert the new record into the new block. The solution was to detect this situation and update the in-core key value that we pass up to the caller so that the caller will (eventually) add the new block to the parent level of the tree with the correct key. However, I missed a subtlety about the way inode-rooted btrees work. If the full block was a maximally sized inode root block, we'll solve that fullness by moving the root block's records to a new block, resizing the root block, and updating the root to point to the new block. We don't pass a pointer to the new block to the caller because that work has already been done. The new record will /always/ land in the new block, so in this case we need to use xfs_btree_update_keys to update the keys. This bug can theoretically manifest itself in the very rare case that we split a bmbt root block and the new record lands in the very first slot of the new block, though I've never managed to trigger it in practice. However, it is very easy to reproduce by running generic/522 with the realtime rmapbt patchset if rtinherit=1. Cc: <stable@vger.kernel.org> # v4.8 Fixes: 2c813ad ("xfs: support btrees with overlapping intervals for keys") Signed-off-by: "Darrick J. Wong" <djwong@kernel.org> Reviewed-by: Christoph Hellwig <hch@lst.de>
1 parent 23bee6f commit 6d7b4bc

1 file changed

Lines changed: 23 additions & 6 deletions

File tree

fs/xfs/libxfs/xfs_btree.c

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3557,14 +3557,31 @@ xfs_btree_insrec(
35573557
xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
35583558

35593559
/*
3560-
* If we just inserted into a new tree block, we have to
3561-
* recalculate nkey here because nkey is out of date.
3560+
* Update btree keys to reflect the newly added record or keyptr.
3561+
* There are three cases here to be aware of. Normally, all we have to
3562+
* do is walk towards the root, updating keys as necessary.
35623563
*
3563-
* Otherwise we're just updating an existing block (having shoved
3564-
* some records into the new tree block), so use the regular key
3565-
* update mechanism.
3564+
* If the caller had us target a full block for the insertion, we dealt
3565+
* with that by calling the _make_block_unfull function. If the
3566+
* "make unfull" function splits the block, it'll hand us back the key
3567+
* and pointer of the new block. We haven't yet added the new block to
3568+
* the next level up, so if we decide to add the new record to the new
3569+
* block (bp->b_bn != old_bn), we have to update the caller's pointer
3570+
* so that the caller adds the new block with the correct key.
3571+
*
3572+
* However, there is a third possibility-- if the selected block is the
3573+
* root block of an inode-rooted btree and cannot be expanded further,
3574+
* the "make unfull" function moves the root block contents to a new
3575+
* block and updates the root block to point to the new block. In this
3576+
* case, no block pointer is passed back because the block has already
3577+
* been added to the btree. In this case, we need to use the regular
3578+
* key update function, just like the first case. This is critical for
3579+
* overlapping btrees, because the high key must be updated to reflect
3580+
* the entire tree, not just the subtree accessible through the first
3581+
* child of the root (which is now two levels down from the root).
35663582
*/
3567-
if (bp && xfs_buf_daddr(bp) != old_bn) {
3583+
if (!xfs_btree_ptr_is_null(cur, &nptr) &&
3584+
bp && xfs_buf_daddr(bp) != old_bn) {
35683585
xfs_btree_get_keys(cur, block, lkey);
35693586
} else if (xfs_btree_needs_key_update(cur, optr)) {
35703587
error = xfs_btree_update_keys(cur, level);

0 commit comments

Comments
 (0)