btrfs-progs: handle orphan directories properly

When implementing the GC tree I started getting btrfsck errors when a
test rm -rf <directory> with files inside of it and immediately unmount,
leaving behind orphaned directory items that have GC items for them.

This made me realize that we don't actually handle this case currently
for our normal orphan path.  If we fail to clean everything up and leave
behind the orphan items we'll fail fsck.

Fix this by not processing any backrefs we find if we found an inode
item and its nlink is 0.  This allows us to pass the test case I've
provided to validate this patch.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Josef Bacik 2022-02-22 17:24:30 -05:00 committed by David Sterba
parent d1235141ab
commit c9703d8726
2 changed files with 18 additions and 3 deletions

View file

@ -1018,6 +1018,16 @@ static int merge_inode_recs(struct inode_record *src, struct inode_record *dst,
int ret = 0;
dst->merging = 1;
/*
* If we wandered into a shared node while we were processing an inode
* we may have added backrefs for a directory that had nlink == 0, so
* skip adding these backrefs to our src inode if we have nlink == 0 and
* we actually found the inode item.
*/
if (src->found_inode_item && src->nlink == 0)
goto skip_backrefs;
list_for_each_entry(backref, &src->backrefs, list) {
if (backref->found_dir_index) {
add_inode_backref(dst_cache, dst->ino, backref->dir,
@ -1039,7 +1049,7 @@ static int merge_inode_recs(struct inode_record *src, struct inode_record *dst,
backref->ref_type, backref->errors);
}
}
skip_backrefs:
if (src->found_dir_item)
dst->found_dir_item = 1;
if (src->found_file_extent)
@ -1394,6 +1404,9 @@ static int process_dir_item(struct extent_buffer *eb,
rec = active_node->current;
rec->found_dir_item = 1;
if (rec->found_inode_item && rec->nlink == 0)
return 0;
di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
total = btrfs_item_size_nr(eb, slot);
while (cur < total) {

View file

@ -2727,6 +2727,8 @@ static int check_inode_item(struct btrfs_root *root, struct btrfs_path *path)
imode_to_type(mode), key.objectid,
key.offset);
}
if (is_orphan && key.type == BTRFS_DIR_INDEX_KEY)
break;
ret = check_dir_item(root, &key, path, &size);
err |= ret;
break;
@ -2769,7 +2771,7 @@ out:
&nlink);
}
if (nlink != 1) {
if (nlink > 1) {
err |= LINK_COUNT_ERROR;
error("root %llu DIR INODE[%llu] shouldn't have more than one link(%llu)",
root->objectid, inode_id, nlink);
@ -2785,7 +2787,7 @@ out:
gfs_info->nodesize);
}
if (isize != size) {
if (isize != size && !is_orphan) {
if (repair)
ret = repair_dir_isize_lowmem(root, path,
inode_id, size);