Skip to contentSkip to conversation

btrfs-progs: mkfs/rootdir: Don't follow symbolic link when calcuating size

PATCH In review Qu Wenruo 2 participants 3 messages Mar 28, 2018 – Mar 29, 2018 Btrfs file system

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

PATCH 1/1 btrfs-progs: mkfs/rootdir: Don't follow symbolic link when calcuating size

[BUG]
If we have a symbolic link in rootdir pointing to non-existing location, mkfs.btrfs --rootdir will just fail:
------

$ mkfs.btrfs  -f --rootdir /tmp/rootdir/ /dev/data/btrfs
btrfs-progs v4.15.1
See http://btrfs.wiki.kernel.org for more information.

ERROR: ftw subdir walk of /tmp/rootdir/ failed: No such file or directory
------

[CAUSE]
Commit 599a0abed564 ("btrfs-progs: mkfs/rootdir: Use over-reserve method to make size estimate easier") add extra ftw walk to estimate the filesystem size.

Such default ftw walk will follow symbolic link and gives ENOENT error.

[FIX]
Use nftw() to specify FTW_PHYS so we won't follow symbolic link for size calculation.

Fixes 599a0abed564 ("btrfs-progs: mkfs/rootdir: Use over-reserve method to make size estimate easier")
---
 mkfs/rootdir.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
mkfs/rootdir.c +7 -2
index a1d223a2408a..33c3ff1e18cf 100644
@@ -696,7 +696,7 @@ out:
 }
 
 static int ftw_add_entry_size(const char *fpath, const struct stat *st,
-			      int type)
+			      int type, struct FTW *ftwbuf)
 {
 	/*
 	 * Failed to read the directory, mostly due to EPERM.  Abort ASAP, so
@@ -731,7 +731,12 @@ u64 btrfs_mkfs_size_dir(const char *dir_name, u32 sectorsize, u64 min_dev_size,
 	fs_block_size = sectorsize;
 	ftw_data_size = 0;
 	ftw_meta_nr_inode = 0;
-	ret = ftw(dir_name, ftw_add_entry_size, 10);
+
+	/*
+	 * Symbolic link is not followed when creating files, so no need to
+	 * follow them here.
+	 */
+	ret = nftw(dir_name, ftw_add_entry_size, 10, FTW_PHYS);
 	if (ret < 0) {
 		error("ftw subdir walk of %s failed: %s", dir_name,
 			strerror(errno));
Signature · 1 line
2.16.2
#1 lore raw
Quoted 18 lines · Qu Wenruo

[BUG]
If we have a symbolic link in rootdir pointing to non-existing location, mkfs.btrfs --rootdir will just fail:
------

$ mkfs.btrfs  -f --rootdir /tmp/rootdir/ /dev/data/btrfs
btrfs-progs v4.15.1
See http://btrfs.wiki.kernel.org for more information.

ERROR: ftw subdir walk of /tmp/rootdir/ failed: No such file or directory
------

[CAUSE]
Commit 599a0abed564 ("btrfs-progs: mkfs/rootdir: Use over-reserve method to make size estimate easier") add extra ftw walk to estimate the filesystem size.

Such default ftw walk will follow symbolic link and gives ENOENT error.

Quoted 7 lines

[FIX]
Use nftw() to specify FTW_PHYS so we won't follow symbolic link for size calculation.

Fixes 599a0abed564 ("btrfs-progs: mkfs/rootdir: Use over-reserve method to make size estimate easier")

Applied, thanks. Can you please write a testcase for that?

author
#2 lore raw
Quoted 23 lines · David Sterba
Quoted 22 lines · Qu Wenruo

[BUG]
If we have a symbolic link in rootdir pointing to non-existing location, mkfs.btrfs --rootdir will just fail:
------

$ mkfs.btrfs  -f --rootdir /tmp/rootdir/ /dev/data/btrfs
btrfs-progs v4.15.1
See http://btrfs.wiki.kernel.org for more information.

ERROR: ftw subdir walk of /tmp/rootdir/ failed: No such file or directory
------

[CAUSE]
Commit 599a0abed564 ("btrfs-progs: mkfs/rootdir: Use over-reserve method to make size estimate easier") add extra ftw walk to estimate the filesystem size.

Such default ftw walk will follow symbolic link and gives ENOENT error.

[FIX]
Use nftw() to specify FTW_PHYS so we won't follow symbolic link for size calculation.

Quoted 6 lines
Quoted 3 lines
Fixes 599a0abed564 ("btrfs-progs: mkfs/rootdir: Use over-reserve method to make size estimate easier")

Applied, thanks. Can you please write a testcase for that?

Sorry, I planned but forgot that.

Will come soon.

Thanks,
Qu