btrfs-progs: make sure "mkfs --rootdir" copies all the attributes for the rootdir
PATCH v3 1/2 btrfs-progs: mkfs/rootdir: copy missing attributes for the rootdir inode
In reply to Qu Wenruo[BUG]
When using "mkfs.btrfs" with "--rootdir" option, the top level inode (rootdir) will not get the same xattr from the source dir:
mkdir -p source_dir/ touch source_dir/file setfattr -n user.rootdir_xattr source_dir/ setfattr -n user.regular_xattr source_dir/file mkfs.btrfs -f --rootdir source_dir $dev mount $dev $mnt getfattr $mnt # Nothing <<< getfattr $mnt/file # file: $mnt/file user.regular_xattr <<<
[CAUSE]
In function traverse_directory(), we only call add_xattr_item() for all the child inodes, not really for the rootdir inode itself, leading to the missing xattr items.
Not only xattr, in fact we also miss the uid/gid/timestamps/mode for the rootdir inode.
[FIX]
Extract a dedicated function, copy_rootdir_inode(), to handle every needed attributes for the rootdir inode, including:
- xattr
- uid
- gid
- mode
- timestamps
Issue: #688
--- mkfs/rootdir.c | 88 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 67 insertions(+), 21 deletions(-)
mkfs/rootdir.c +67 -21
@@ -429,6 +429,69 @@ end:return ret; } +static int copy_rootdir_inode (struct btrfs_trans_handle *trans, +struct btrfs_root *root,const char *dir_name) +{ +u64 root_dir_inode_size; +struct btrfs_inode_item *inode_item; +struct btrfs_path path = {0 }; +struct btrfs_key key; +struct extent_buffer *leaf; +struct stat st; +int ret; + + ret =stat (dir_name, &st); +if (ret <0 ) { + ret = -errno; +error ("lstat failed for direcotry %s: $m" , dir_name); +return ret; + } + + ret =add_xattr_item (trans, root,btrfs_root_dirid (&root->root_item), + dir_name); +if (ret <0 ) { + errno = -ret; +error ("failed to add xattr item for the top level inode: %m" ); +return ret; + } + + key.objectid =btrfs_root_dirid (&root->root_item); + key.offset =0 ; + key.type =BTRFS_INODE_ITEM_KEY ; + ret =btrfs_lookup_inode (trans, root, &path, &key,1 ); +if (ret >0 ) + ret = -ENOENT ; +if (ret) { +error ("failed to lookup root dir: %d" , ret); + goto error; + } + + leaf = path.nodes[0 ]; + inode_item =btrfs_item_ptr (leaf, path.slots[0 ], + struct btrfs_inode_item); + + root_dir_inode_size =calculate_dir_inode_size (dir_name); +btrfs_set_inode_size (leaf, inode_item, root_dir_inode_size); + +/* Unlike fill_inode_item, we only need to copy part of the attributes. */ +btrfs_set_inode_uid (leaf, inode_item, st.st_uid); +btrfs_set_inode_gid (leaf, inode_item, st.st_gid); +btrfs_set_inode_mode (leaf, inode_item, st.st_mode); +btrfs_set_timespec_sec (leaf, &inode_item->atime, st.st_atime); +btrfs_set_timespec_nsec (leaf, &inode_item->atime,0 ); +btrfs_set_timespec_sec (leaf, &inode_item->ctime, st.st_ctime); +btrfs_set_timespec_nsec (leaf, &inode_item->ctime,0 ); +btrfs_set_timespec_sec (leaf, &inode_item->mtime, st.st_mtime); +btrfs_set_timespec_nsec (leaf, &inode_item->mtime,0 ); +btrfs_set_timespec_sec (leaf, &inode_item->otime,0 ); +btrfs_set_timespec_nsec (leaf, &inode_item->otime,0 ); +btrfs_mark_buffer_dirty (leaf); + +error: +btrfs_release_path (&path); +return ret; +} +static int traverse_directory (struct btrfs_trans_handle *trans,struct btrfs_root *root,const char *dir_name,struct directory_name_entry *dir_head)
@@ -436,7 +499,6 @@ static int traverse_directory(struct btrfs_trans_handle *trans,int ret =0 ;struct btrfs_inode_item cur_inode; -struct btrfs_inode_item *inode_item;int count, i, dir_index_cnt;struct dirent **files;struct stat st;
@@ -445,10 +507,6 @@ static int traverse_directory(struct btrfs_trans_handle *trans,ino_t parent_inum, cur_inum;ino_t highest_inum =0 ;const char *parent_dir_name; -struct btrfs_path path = {0 }; -struct extent_buffer *leaf; -struct btrfs_key root_dir_key; -u64 root_dir_inode_size =0 ;/* Add list for source directory */ dir_entry =malloc (sizeof (struct directory_name_entry ));
@@ -466,25 +524,13 @@ static int traverse_directory(struct btrfs_trans_handle *trans, dir_entry->inum = parent_inum;list_add_tail (&dir_entry->list, &dir_head->list); - root_dir_key.objectid =btrfs_root_dirid (&root->root_item); - root_dir_key.offset =0 ; - root_dir_key.type =BTRFS_INODE_ITEM_KEY ; - ret =btrfs_lookup_inode (trans, root, &path, &root_dir_key,1 ); -if (ret) { -error ("failed to lookup root dir: %d" , ret); + ret =copy_rootdir_inode (trans, root, dir_name); +if (ret <0 ) { + errno = -ret; +error ("failed to copy rootdir inode: %m" ); goto fail_no_dir; } - leaf = path.nodes[0 ]; - inode_item =btrfs_item_ptr (leaf, path.slots[0 ], - struct btrfs_inode_item); - - root_dir_inode_size =calculate_dir_inode_size (dir_name); -btrfs_set_inode_size (leaf, inode_item, root_dir_inode_size); -btrfs_mark_buffer_dirty (leaf); - -btrfs_release_path (&path); - do { parent_dir_entry = list_entry(dir_head->list.next, struct directory_name_entry,
Signature · 1 line
2.42.0
v3 cover btrfs-progs: make sure "mkfs --rootdir" copies all the attributes for the rootdir
[CHANGELOG]
v3:
- Use 'stat()' instead pf 'lstate()' for the rootdir This would follow a softlink for the rootdir.
v2:
- Add more attributes to be copied for rootdir, including:
* mode
* uid
* gid
* timestamps
- Update the test case
* Use two loopback devices
One to store the source directory, so that we're ensured to have xattr support.
* Add checks for all the uid/gid/mode attributes
We got a bug report that "mkfs.btrfs --rootdir" copies all the xattr but the xattr of the source directory.
It turns out that we only do the regular xattr/gid/uid/mode/timestamps copy for all the child inodes, not the source directory itself.
Fix it and create a test case for it.
Qu Wenruo (2):
btrfs-progs: mkfs/rootdir: copy missing attributes for the rootdir
inode
btrfs-progs: tests/mkfs: make sure rootdir inode got its attributes
copied
mkfs/rootdir.c | 88 ++++++++++++++++------ tests/mkfs-tests/027-rootdir-inode/test.sh | 60 +++++++++++++++ 2 files changed, 127 insertions(+), 21 deletions(-) create mode 100755 tests/mkfs-tests/027-rootdir-inode/test.sh
--
2.42.0
PATCH v3 2/2 btrfs-progs: tests/mkfs: make sure rootdir inode got its attributes copied
The new test case would:
- Prepare two loopback devices
One is for storing the source directory (on a btrfs). This is to ensure we can set xattr for the directory, as filesystems like tmpfs (mostly utilized by mktemp) is not supporting xattr.
The other one is the real target fs where we call "mkfs.btrfs --rootdir" on.
- Create the source directory with the following contents:
* rootdir inode attributs:
# mode (750)
# uid (1000)
# gid (1000)
# xattr (user.rootdir)
* one regular file, with attributes:
# xattr (user.foorbar)- Execute "mkfs.btrfs --rootdir" and mount the new fs
- Verify the above attributes
The target fs should have the same attributes, especially for the rootdir inode.
--- tests/mkfs-tests/027-rootdir-inode/test.sh | 60 ++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100755 tests/mkfs-tests/027-rootdir-inode/test.sh
tests/mkfs-tests/027-rootdir-inode/test.sh new file +60 -0
@@ -0,0 +1,60 @@ +#!/bin/bash +# Test if "mkfs.btrfs --rootdir" would properly copy all the attributes of the +# source directory. + +source "$TEST_TOP/common" || exit + +setup_root_helper +# Here we need two devices, one as a temporaray btrfs, storing the source +# directory. As we want to setup xattr, which is not supported by tmpfs +# (most modern distros go tmpfs for /tmp). +# So we have to put the source directory on a fs that supports xattr. +# +# Then the second fs is the real one we mkfs on. +setup_loopdevs 2 +prepare_loopdevs + +tmp_dev=${loopdevs[1]} +real_dev=${loopdevs[2]} + +check_global_prereq setfattr +check_global_prereq getfattr + +# Here we don't want to use /tmp, as it's pretty common /tmp is tmpfs, which +# doesn't support xattr. +# Instead we go $TEST_TOP/btrfs-progs-mkfs-tests-027.XXXXXX/ instead. +run_check $SUDO_HELPER "$TOP/mkfs.btrfs" -f "$tmp_dev" +run_check $SUDO_HELPER mount -t btrfs "$tmp_dev" "$TEST_MNT" + +run_check $SUDO_HELPER mkdir "$TEST_MNT/source_dir/" +run_check $SUDO_HELPER chmod 750 "$TEST_MNT/source_dir/" +run_check $SUDO_HELPER chown 1000:1000 "$TEST_MNT/source_dir/" +run_check $SUDO_HELPER setfattr -n user.rootdir "$TEST_MNT/source_dir/" + +old_mode=$(run_check_stdout $SUDO_HELPER stat "$TEST_MNT/source_dir/" | grep "Uid:") +run_check $SUDO_HELPER touch "$TEST_MNT/source_dir/foobar" +run_check $SUDO_HELPER setfattr -n user.foobar "$TEST_MNT/source_dir/foobar" + +run_check $SUDO_HELPER "$TOP/mkfs.btrfs" --rootdir "$TEST_MNT/source_dir" -f "$real_dev" +run_check $SUDO_HELPER umount "$TEST_MNT" + +run_check $SUDO_HELPER mount -t btrfs "$real_dev" "$TEST_MNT" + +new_mode=$(run_check_stdout $SUDO_HELPER stat "$TEST_MNT/" | grep "Uid:") +new_rootdir_attr=$(run_check_stdout $SUDO_HELPER getfattr -n user.rootdir --absolute-names "$src_dir") +new_foobar_attr=$(run_check_stdout getfattr -n user.foobar --absolute-names "$src_dir/foobar") + +run_check_umount_test_dev "$TEST_MNT" +cleanup_loopdevs + +if ! echo "$new_rootdir_attr" | grep -q "user.rootdir" ; then + _fail "no rootdir xattr found" +fi + +if ! echo "$new_foobar_attr"| grep -q "user.foobar" ; then + _fail "no regular file xattr found" +fi + +if [ "$new_mode" != "$old_mode" ]; then + _fail "mode/uid/gid mismatch" +fi
Signature · 1 line
2.42.0
Re: [PATCH v3 1/2] btrfs-progs: mkfs/rootdir: copy missing attributes for the rootdir inode
In reply to Qu WenruoQuoted 50 lines · Qu Wenruo
[BUG]
When using "mkfs.btrfs" with "--rootdir" option, the top level inode (rootdir) will not get the same xattr from the source dir:mkdir -p source_dir/ touch source_dir/file setfattr -n user.rootdir_xattr source_dir/ setfattr -n user.regular_xattr source_dir/file mkfs.btrfs -f --rootdir source_dir $dev mount $dev $mnt getfattr $mnt # Nothing <<< getfattr $mnt/file # file: $mnt/file user.regular_xattr <<<[CAUSE]
In functiontraverse_directory(), we only calladd_xattr_item()for all the child inodes, not really for the rootdir inode itself, leading to the missing xattr items.Not only xattr, in fact we also miss the uid/gid/timestamps/mode for the rootdir inode.
[FIX]
Extract a dedicated function,copy_rootdir_inode(), to handle every needed attributes for the rootdir inode, including:- xattr
- uid
- gid
- mode
- timestampsIssue: #688
Signed-off-by Qu Wenruo <[email protected]>--- mkfs/rootdir.c | 88 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 67 insertions(+), 21 deletions(-)mkfs/rootdir.c +2
-0@@ -429,6 +429,69 @@ end:return ret; } +static int copy_rootdir_inode (struct btrfs_trans_handle *trans, +struct btrfs_root *root,const char *dir_name)
mkfs/rootdir.c end: · line 443
Quoted 10 lines
mkfs/rootdir.c +10
-0+{ +u64 root_dir_inode_size; +struct btrfs_inode_item *inode_item; +struct btrfs_path path = {0 }; +struct btrfs_key key; +struct extent_buffer *leaf; +struct stat st; +int ret; + + ret =stat (dir_name, &st);
According to the v3 changelog this should be lstat(), right?
mkfs/rootdir.c end: · line 446
Quoted 3 lines
mkfs/rootdir.c +3
-0+if (ret <0 ) { + ret = -errno; +error ("lstat failed for direcotry %s: $m" , dir_name);
^^^^^
like here.
Quoted 2 lines
mkfs/rootdir.c +2
-0+return ret; + }
Re: [PATCH v3 1/2] btrfs-progs: mkfs/rootdir: copy missing attributes for the rootdir inode
In reply to Qu WenruoQuoted 94 lines · Qu Wenruo
[BUG]
When using "mkfs.btrfs" with "--rootdir" option, the top level inode (rootdir) will not get the same xattr from the source dir:mkdir -p source_dir/ touch source_dir/file setfattr -n user.rootdir_xattr source_dir/ setfattr -n user.regular_xattr source_dir/file mkfs.btrfs -f --rootdir source_dir $dev mount $dev $mnt getfattr $mnt # Nothing <<< getfattr $mnt/file # file: $mnt/file user.regular_xattr <<<[CAUSE]
In functiontraverse_directory(), we only calladd_xattr_item()for all the child inodes, not really for the rootdir inode itself, leading to the missing xattr items.Not only xattr, in fact we also miss the uid/gid/timestamps/mode for the rootdir inode.
[FIX]
Extract a dedicated function,copy_rootdir_inode(), to handle every needed attributes for the rootdir inode, including:- xattr
- uid
- gid
- mode
- timestampsIssue: #688
Signed-off-by Qu Wenruo <[email protected]>--- mkfs/rootdir.c | 88 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 67 insertions(+), 21 deletions(-)mkfs/rootdir.c +46
-0@@ -429,6 +429,69 @@ end:return ret; } +static int copy_rootdir_inode (struct btrfs_trans_handle *trans, +struct btrfs_root *root,const char *dir_name) +{ +u64 root_dir_inode_size; +struct btrfs_inode_item *inode_item; +struct btrfs_path path = {0 }; +struct btrfs_key key; +struct extent_buffer *leaf; +struct stat st; +int ret; + + ret =stat (dir_name, &st); +if (ret <0 ) { + ret = -errno; +error ("lstat failed for direcotry %s: $m" , dir_name); +return ret; + } + + ret =add_xattr_item (trans, root,btrfs_root_dirid (&root->root_item), + dir_name); +if (ret <0 ) { + errno = -ret; +error ("failed to add xattr item for the top level inode: %m" ); +return ret; + } + + key.objectid =btrfs_root_dirid (&root->root_item); + key.offset =0 ; + key.type =BTRFS_INODE_ITEM_KEY ; + ret =btrfs_lookup_inode (trans, root, &path, &key,1 ); +if (ret >0 ) + ret = -ENOENT ; +if (ret) { +error ("failed to lookup root dir: %d" , ret); + goto error; + } + + leaf = path.nodes[0 ]; + inode_item =btrfs_item_ptr (leaf, path.slots[0 ], + struct btrfs_inode_item); + + root_dir_inode_size =calculate_dir_inode_size (dir_name); +btrfs_set_inode_size (leaf, inode_item, root_dir_inode_size); + +/* Unlike fill_inode_item, we only need to copy part of the attributes. */ +btrfs_set_inode_uid (leaf, inode_item, st.st_uid);
mkfs/rootdir.c end: · line 487
Quoted 10 lines
mkfs/rootdir.c +10
-0+btrfs_set_inode_gid (leaf, inode_item, st.st_gid); +btrfs_set_inode_mode (leaf, inode_item, st.st_mode); +btrfs_set_timespec_sec (leaf, &inode_item->atime, st.st_atime); +btrfs_set_timespec_nsec (leaf, &inode_item->atime,0 ); +btrfs_set_timespec_sec (leaf, &inode_item->ctime, st.st_ctime); +btrfs_set_timespec_nsec (leaf, &inode_item->ctime,0 ); +btrfs_set_timespec_sec (leaf, &inode_item->mtime, st.st_mtime); +btrfs_set_timespec_nsec (leaf, &inode_item->mtime,0 ); +btrfs_set_timespec_sec (leaf, &inode_item->otime,0 ); +btrfs_set_timespec_nsec (leaf, &inode_item->otime,0 );
We should put something sensible to the otime, either current time, real otime or at least mtime/ctime as a fallback.
Quoted 24 lines · Qu Wenruo
[CHANGELOG]
v3:
- Use 'stat()' instead pf 'lstate()' for the rootdir This would follow a softlink for the rootdir.v2:
- Add more attributes to be copied for rootdir, including:
* mode
* uid
* gid
* timestamps- Update the test case
* Use two loopback devices
One to store the source directory, so that we're ensured to have xattr support.
* Add checks for all the uid/gid/mode attributesWe got a bug report that "mkfs.btrfs --rootdir" copies all the xattr but the xattr of the source directory.
It turns out that we only do the regular xattr/gid/uid/mode/timestamps copy for all the child inodes, not the source directory itself.
Quoted 7 lines
Fix it and create a test case for it.
Qu Wenruo (2):
btrfs-progs: mkfs/rootdir: copy missing attributes for the rootdir
inode
btrfs-progs: tests/mkfs: make sure rootdir inode got its attributes
copied
Added to devel with some fixups, thanks.
Re: [PATCH v3 1/2] btrfs-progs: mkfs/rootdir: copy missing attributes for the rootdir inode
In reply to David SterbaQuoted 49 lines · David Sterba
Quoted 48 lines · Qu Wenruo
[BUG]
When using "mkfs.btrfs" with "--rootdir" option, the top level inode (rootdir) will not get the same xattr from the source dir:mkdir -p source_dir/ touch source_dir/file setfattr -n user.rootdir_xattr source_dir/ setfattr -n user.regular_xattr source_dir/file mkfs.btrfs -f --rootdir source_dir $dev mount $dev $mnt getfattr $mnt # Nothing <<< getfattr $mnt/file # file: $mnt/file user.regular_xattr <<<[CAUSE]
In functiontraverse_directory(), we only calladd_xattr_item()for all the child inodes, not really for the rootdir inode itself, leading to the missing xattr items.Not only xattr, in fact we also miss the uid/gid/timestamps/mode for the rootdir inode.
[FIX]
Extract a dedicated function,copy_rootdir_inode(), to handle every needed attributes for the rootdir inode, including:- xattr
- uid
- gid
- mode
- timestampsIssue: #688
Signed-off-by Qu Wenruo <[email protected]>--- mkfs/rootdir.c | 88 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 67 insertions(+), 21 deletions(-)mkfs/rootdir.c +0
-0@@ -429,6 +429,69 @@ end:return ret; }
Quoted 14 lines
mkfs/rootdir.c end: · line 443
Quoted 12 lines
mkfs/rootdir.c +12
-0+static int copy_rootdir_inode (struct btrfs_trans_handle *trans, +struct btrfs_root *root,const char *dir_name) +{ +u64 root_dir_inode_size; +struct btrfs_inode_item *inode_item; +struct btrfs_path path = {0 }; +struct btrfs_key key; +struct extent_buffer *leaf; +struct stat st; +int ret; + + ret =stat (dir_name, &st);According to the v3 changelog this should be
lstat(), right?
It should be stat(), not lstat() of v2.
The reason is, the end user may pass a softlink pointing to a directory. In that case, lstat() would not follow the softlink.
Quoted 7 lines
mkfs/rootdir.c end: · line 446
Quoted 4 lines
mkfs/rootdir.c +3
-0+if (ret <0 ) { + ret = -errno; +error ("lstat failed for direcotry %s: $m" , dir_name);^^^^^like here.
I'll update it along with other fixes to address the comments.
Thanks,
Qu
Quoted 3 lines
Quoted 3 lines
mkfs/rootdir.c +2
-0+return ret; + }
Re: [PATCH v3 1/2] btrfs-progs: mkfs/rootdir: copy missing attributes for the rootdir inode
Quoted 67 lines · Qu Wenruo
Quoted 49 lines · David Sterba
Quoted 48 lines · Qu Wenruo
[BUG]
When using "mkfs.btrfs" with "--rootdir" option, the top level inode (rootdir) will not get the same xattr from the source dir:mkdir -p source_dir/ touch source_dir/file setfattr -n user.rootdir_xattr source_dir/ setfattr -n user.regular_xattr source_dir/file mkfs.btrfs -f --rootdir source_dir $dev mount $dev $mnt getfattr $mnt # Nothing <<< getfattr $mnt/file # file: $mnt/file user.regular_xattr <<<[CAUSE]
In functiontraverse_directory(), we only calladd_xattr_item()for all the child inodes, not really for the rootdir inode itself, leading to the missing xattr items.Not only xattr, in fact we also miss the uid/gid/timestamps/mode for the rootdir inode.
[FIX]
Extract a dedicated function,copy_rootdir_inode(), to handle every needed attributes for the rootdir inode, including:- xattr
- uid
- gid
- mode
- timestampsIssue: #688
Signed-off-by Qu Wenruo <[email protected]>--- mkfs/rootdir.c | 88 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 67 insertions(+), 21 deletions(-)mkfs/rootdir.c +0
-0@@ -429,6 +429,69 @@ end:return ret; }Quoted 14 lines
mkfs/rootdir.c end: · line 443
Quoted 12 lines
mkfs/rootdir.c +12
-0+static int copy_rootdir_inode (struct btrfs_trans_handle *trans, +struct btrfs_root *root,const char *dir_name) +{ +u64 root_dir_inode_size; +struct btrfs_inode_item *inode_item; +struct btrfs_path path = {0 }; +struct btrfs_key key; +struct extent_buffer *leaf; +struct stat st; +int ret; + + ret =stat (dir_name, &st);According to the v3 changelog this should be
lstat(), right?
Quoted 4 lines
It should be
stat(), notlstat()of v2.The reason is, the end user may pass a softlink pointing to a directory. In that case,
lstat()would not follow the softlink.
Ok, I've changed it to stat().
Quoted 10 lines
Quoted 8 lines
mkfs/rootdir.c end: · line 446
Quoted 5 lines
mkfs/rootdir.c +3
-0+if (ret <0 ) { + ret = -errno; +error ("lstat failed for direcotry %s: $m" , dir_name);^^^^^like here.
I'll update it along with other fixes to address the comments.
Unless it's just the l/stat references no need to resend, I've updated the commit.