Skip to content

Commit 8fb1dcb

Browse files
adam900710kdave
authored andcommitted
Revert "btrfs: canonicalize the device path before adding it"
This reverts commit 7e06de7. Commit 7e06de7 ("btrfs: canonicalize the device path before adding it") tries to make btrfs to use "/dev/mapper/*" name first, then any filename inside "/dev/" as the device path. This is mostly fine when there is only the root namespace involved, but when multiple namespace are involved, things can easily go wrong for the d_path() usage. As d_path() returns a file path that is namespace dependent, the resulted string may not make any sense in another namespace. Furthermore, the "/dev/" prefix checks itself is not reliable, one can still make a valid initramfs without devtmpfs, and fill all needed device nodes manually. Overall the userspace has all its might to pass whatever device path for mount, and we are not going to win the war trying to cover every corner case. So just revert that commit, and do no extra d_path() based file path sanity check. CC: [email protected] # 6.12+ Link: https://lore.kernel.org/linux-fsdevel/[email protected]/ Reviewed-by: Boris Burkov <[email protected]> Signed-off-by: Qu Wenruo <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent f95d186 commit 8fb1dcb

File tree

1 file changed

+1
-90
lines changed

1 file changed

+1
-90
lines changed

fs/btrfs/volumes.c

Lines changed: 1 addition & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -733,82 +733,6 @@ const u8 *btrfs_sb_fsid_ptr(const struct btrfs_super_block *sb)
733733
return has_metadata_uuid ? sb->metadata_uuid : sb->fsid;
734734
}
735735

736-
/*
737-
* We can have very weird soft links passed in.
738-
* One example is "/proc/self/fd/<fd>", which can be a soft link to
739-
* a block device.
740-
*
741-
* But it's never a good idea to use those weird names.
742-
* Here we check if the path (not following symlinks) is a good one inside
743-
* "/dev/".
744-
*/
745-
static bool is_good_dev_path(const char *dev_path)
746-
{
747-
struct path path = { .mnt = NULL, .dentry = NULL };
748-
char *path_buf = NULL;
749-
char *resolved_path;
750-
bool is_good = false;
751-
int ret;
752-
753-
if (!dev_path)
754-
goto out;
755-
756-
path_buf = kmalloc(PATH_MAX, GFP_KERNEL);
757-
if (!path_buf)
758-
goto out;
759-
760-
/*
761-
* Do not follow soft link, just check if the original path is inside
762-
* "/dev/".
763-
*/
764-
ret = kern_path(dev_path, 0, &path);
765-
if (ret)
766-
goto out;
767-
resolved_path = d_path(&path, path_buf, PATH_MAX);
768-
if (IS_ERR(resolved_path))
769-
goto out;
770-
if (strncmp(resolved_path, "/dev/", strlen("/dev/")))
771-
goto out;
772-
is_good = true;
773-
out:
774-
kfree(path_buf);
775-
path_put(&path);
776-
return is_good;
777-
}
778-
779-
static int get_canonical_dev_path(const char *dev_path, char *canonical)
780-
{
781-
struct path path = { .mnt = NULL, .dentry = NULL };
782-
char *path_buf = NULL;
783-
char *resolved_path;
784-
int ret;
785-
786-
if (!dev_path) {
787-
ret = -EINVAL;
788-
goto out;
789-
}
790-
791-
path_buf = kmalloc(PATH_MAX, GFP_KERNEL);
792-
if (!path_buf) {
793-
ret = -ENOMEM;
794-
goto out;
795-
}
796-
797-
ret = kern_path(dev_path, LOOKUP_FOLLOW, &path);
798-
if (ret)
799-
goto out;
800-
resolved_path = d_path(&path, path_buf, PATH_MAX);
801-
if (IS_ERR(resolved_path)) {
802-
ret = PTR_ERR(resolved_path);
803-
goto out;
804-
}
805-
ret = strscpy(canonical, resolved_path, PATH_MAX);
806-
out:
807-
kfree(path_buf);
808-
path_put(&path);
809-
return ret;
810-
}
811-
812736
static bool is_same_device(struct btrfs_device *device, const char *new_path)
813737
{
814738
struct path old = { .mnt = NULL, .dentry = NULL };
@@ -1513,23 +1437,12 @@ struct btrfs_device *btrfs_scan_one_device(const char *path, blk_mode_t flags,
15131437
bool new_device_added = false;
15141438
struct btrfs_device *device = NULL;
15151439
struct file *bdev_file;
1516-
char *canonical_path = NULL;
15171440
u64 bytenr;
15181441
dev_t devt;
15191442
int ret;
15201443

15211444
lockdep_assert_held(&uuid_mutex);
15221445

1523-
if (!is_good_dev_path(path)) {
1524-
canonical_path = kmalloc(PATH_MAX, GFP_KERNEL);
1525-
if (canonical_path) {
1526-
ret = get_canonical_dev_path(path, canonical_path);
1527-
if (ret < 0) {
1528-
kfree(canonical_path);
1529-
canonical_path = NULL;
1530-
}
1531-
}
1532-
}
15331446
/*
15341447
* Avoid an exclusive open here, as the systemd-udev may initiate the
15351448
* device scan which may race with the user's mount or mkfs command,
@@ -1574,8 +1487,7 @@ struct btrfs_device *btrfs_scan_one_device(const char *path, blk_mode_t flags,
15741487
goto free_disk_super;
15751488
}
15761489

1577-
device = device_list_add(canonical_path ? : path, disk_super,
1578-
&new_device_added);
1490+
device = device_list_add(path, disk_super, &new_device_added);
15791491
if (!IS_ERR(device) && new_device_added)
15801492
btrfs_free_stale_devices(device->devt, device);
15811493

@@ -1584,7 +1496,6 @@ struct btrfs_device *btrfs_scan_one_device(const char *path, blk_mode_t flags,
15841496

15851497
error_bdev_put:
15861498
fput(bdev_file);
1587-
kfree(canonical_path);
15881499

15891500
return device;
15901501
}

0 commit comments

Comments
 (0)