btrfs-progs/kernel-shared
Qu Wenruo 2cdc8dddbf btrfs-progs: mkfs: offset inode numbers of the source filesystem
[BUG]
When running mkfs tests on a newly rebooted minimal system, it can cause
mkfs/009 to fail.

The reproduce steps requires /tmp to has minimal files in the first
place.

  # mkdir /tmp/rootdir
  # xfs_io -f -c "pwrite 0 16k" /tmp/rootdir
  # mkfs.btrfs --rootdir /tmp/rootdir -f $dev
  # btrfs check $dev
  Opening filesystem to check...
  Checking filesystem on /dev/test/scratch1
  UUID: 6821b3db-f056-4c18-b797-32679dcd4272
  [1/7] checking root items
  [2/7] checking extents
  data backref 13631488 root 5 owner 170 offset 0 num_refs 0 not found in extent tree
  incorrect local backref count on 13631488 root 5 owner 170 offset 0 found 1 wanted 0 back 0x55ff6cd72260
  backref 13631488 root 5 not referenced back 0x55ff6cd4c1f0
  incorrect global backref count on 13631488 found 2 wanted 1
  backpointer mismatch on [13631488 16384]
  ERROR: errors found in extent allocation tree or chunk allocation

[CAUSE]
The extent tree has the following weird item:

	item 0 key (13631488 EXTENT_ITEM 16384) itemoff 16250 itemsize 33
		refs 1 gen 0 flags DATA
		tree block backref root FS_TREE

This is an extent item for data, thus it should not have an inline tree
backref.

Then checking the fs tree:

	item 0 key (170 INODE_ITEM 0) itemoff 16123 itemsize 160
		generation 7 transid 0 size 16384 nbytes 16384
		block group 0 mode 100600 links 1 uid 1000 gid 1000 rdev 0
		sequence 0 flags 0x0(none)
		atime 1664866393.0 (2022-10-04 14:53:13)
		ctime 1664863510.0 (2022-10-04 14:05:10)
		mtime 1664863455.0 (2022-10-04 14:04:15)
		otime 0.0 (1970-01-01 08:00:00)

There is an inode item before the root dir inode.

And that inode number 170 is causing the problem.

In traverse_directory(), we use the inode number reported from stat()
directly as btrfs inode number, and pass it to
btrfs_record_file_extent(), which finally calls btrfs_inc_extent_ref(),
with above 170 passed as @owner parameter.

But inside btrfs_inc_extent_ref() we use that @owner value to determine
if it's a data backref.
Since we got a smaller than BTRFS_FIRST_FREE_OBJECTID, btrfs treats it
as tree block, and cause the above problem.

[FIX]
As a quick fix, always add BTRFS_FIRST_FREE_OBJECTID to all inode number
directly grabbed from stat().

And add an ASSERT() in __btrfs_record_file_extent() to catch unexpected
objectid.

This is not a perfect solution, as the resulted fs will has a huge gap
in its inodes:

	item 0 key (256 INODE_ITEM 0) itemoff 16123 itemsize 160
	item 4 key (426 INODE_ITEM 0) itemoff 15883 itemsize 160

For a proper fix, we should allocate new btrfs inode numbers in a
sequential order, but that would be another series of patches.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2022-10-11 09:08:10 +02:00
..
backref.c btrfs-progs: remove the _nr from the item helpers 2022-03-09 15:13:13 +01:00
backref.h
ctree.c btrfs-progs: move repair.c from common/ to check/ 2022-10-11 09:08:09 +02:00
ctree.h btrfs-progs: kernel-lib: remove radix-tree 2022-10-11 09:08:07 +02:00
delayed-ref.c btrfs-progs: don't include btrfs-list.h unless necessary 2021-10-08 20:47:03 +02:00
delayed-ref.h btrfs-progs: unify GPL header comments 2021-09-07 13:58:44 +02:00
dir-item.c btrfs-progs: remove the _nr from the item helpers 2022-03-09 15:13:13 +01:00
disk-io.c btrfs-progs: use template for out of memory error messages 2022-10-11 09:08:09 +02:00
disk-io.h btrfs-progs: btrfstune: add the ability to convert to block group tree feature 2022-09-12 18:25:32 +02:00
extent-tree.c btrfs-progs: mkfs: offset inode numbers of the source filesystem 2022-10-11 09:08:10 +02:00
extent_io.c btrfs-progs: use template for out of memory error messages 2022-10-11 09:08:09 +02:00
extent_io.h btrfs-progs: remove unused function extent_io_tree_init_cache_max() 2022-10-11 09:08:08 +02:00
file-item.c btrfs-progs: kernel-lib: remove radix-tree 2022-10-11 09:08:07 +02:00
file.c btrfs-progs: use read_data_from_disk() to replace read_extent_from_disk() and replace read_extent_data() 2022-04-25 19:08:30 +02:00
free-space-cache.c btrfs-progs: use read_data_from_disk() to replace read_extent_from_disk() and replace read_extent_data() 2022-04-25 19:08:30 +02:00
free-space-cache.h
free-space-tree.c btrfs-progs: fix may be unused warning in load_free_space_extents 2022-10-11 09:06:11 +02:00
free-space-tree.h btrfs-progs: properly initialize block group thresholds 2022-05-20 15:54:20 +02:00
inode-item.c btrfs-progs: remove the _nr from the item helpers 2022-03-09 15:13:13 +01:00
inode.c btrfs-progs: use template for transaction commit error messages 2022-10-11 09:08:10 +02:00
print-tree.c btrfs-progs: use template for out of memory error messages 2022-10-11 09:08:09 +02:00
print-tree.h
root-tree.c btrfs-progs: remove the _nr from the item helpers 2022-03-09 15:13:13 +01:00
send.h btrfs-progs: receive: add support for fs-verity 2022-10-11 09:08:08 +02:00
transaction.c btrfs-progs: properly handle write error when writing back tree blocks 2022-10-11 09:08:08 +02:00
transaction.h
ulist.c btrfs-progs: unify GPL header comments 2021-09-07 13:58:44 +02:00
ulist.h btrfs-progs: unify GPL header comments 2021-09-07 13:58:44 +02:00
uuid-tree.c btrfs-progs: remove the _nr from the item helpers 2022-03-09 15:13:13 +01:00
volumes.c btrfs-progs: device-utils: rename btrfs_device_size 2022-10-11 09:08:10 +02:00
volumes.h btrfs-progs: remove the unused btrfs_fs_info::seeding member 2022-04-29 22:13:22 +02:00
zoned.c btrfs-progs: device-utils: rename btrfs_device_size 2022-10-11 09:08:10 +02:00
zoned.h btrfs-progs: zoned: export sb_zone_number() and related constants 2022-04-08 23:17:35 +02:00