mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ismael Luceno <ismael@iodev.co.uk>
To: linux-kernel@vger.kernel.org
Cc: Ismael Luceno <ismael@iodev.co.uk>,
	Ismael Luceno <iluceno@suse.de>, David Sterba <dsterba@suse.com>,
	Chris Mason <mason@kernel.org>,
	linux-btrfs@vger.kernel.org (open list:BTRFS FILE SYSTEM)
Subject: [PATCH 4/8] btrfs: add a helper to scan all block devices
Date: Mon, 16 Nov 2026 23:43:20 +0100	[thread overview]
Message-ID: <20261116224316.11237-14-ismael@iodev.co.uk> (raw)
In-Reply-To: <20261116224316.11237-10-ismael@iodev.co.uk>

btrfs learns about the devices of a filesystem from the mount source,
from the device= mount options, or from userspace running the device
scanner through the BTRFS_IOC_SCAN_DEV ioctl.  When btrfs is the root
filesystem and there is no initrd, none of those is there: the root
mount names one device and the rest of the array is left out.

Add btrfs_scan_all_devices(), which walks the block class and hands
every candidate to btrfs_scan_one_device(), the same path the ioctl
takes.  It is the kernel side of the scanner userspace would normally
run, so that the devices can be found without userspace.

A whole disk carrying a partition table is skipped.  btrfs reads its
superblock from a fixed offset near the start of the device, which on
a partitioned disk can fall inside the first partition, and picking the
disk up as a btrfs device when the partition is the real one would be
wrong.  Partitions themselves, and disks with no partition table, are
scanned as before.

The device is opened by its /dev name, which is what the rest of btrfs
records in device->name.

This adds no behaviour on its own; a caller is added separately.

Partitions are registered with part_type and disks with disk_type, so
iterating with a type filter for either skips the other.  Iterate
everything and let btrfs_can_scan_disk() decide, so a partition is
scanned and a disk that only holds partitions is not.

Signed-off-by: Ismael Luceno <ismael@iodev.co.uk>
Signed-off-by: Ismael Luceno <iluceno@suse.de>
---
 fs/btrfs/volumes.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++
 fs/btrfs/volumes.h |  3 ++
 2 files changed, 82 insertions(+)

diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index f1305f68b1f8..55f11c9fe24c 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -1531,6 +1531,85 @@ btrfs_get_bdev_and_sb(const char *device_path, blk_mode_t flags, void *holder,
 	return device;
 }
 
+#ifdef CONFIG_BTRFS_ROOT_SCAN
+/*
+ * Return true if @disk is worth handing to btrfs_scan_one_device().
+ *
+ * A partition always is.  A whole disk only is when it carries no partition
+ * table: if it does, the first partition may well have a signature in the
+ * area btrfs reads from, and picking the disk up as a btrfs device would be
+ * wrong.
+ */
+static bool btrfs_can_scan_disk(struct gendisk *disk, struct block_device *bdev)
+{
+	struct block_device *part;
+	unsigned long idx;
+
+	if (!get_capacity(disk) || (disk->flags & GENHD_FL_HIDDEN))
+		return false;
+	if (bdev_is_partition(bdev))
+		return true;
+
+	rcu_read_lock();
+	xa_for_each_start(&disk->part_tbl, idx, part, 1) {
+		if (bdev_nr_sectors(part)) {
+			rcu_read_unlock();
+			return false;
+		}
+	}
+	rcu_read_unlock();
+
+	return true;
+}
+
+/*
+ * Register every block device that holds a btrfs superblock.
+ *
+ * This is the kernel side of the device scanner userspace normally drives with
+ * the BTRFS_IOC_SCAN_DEV ioctl.  It exists for the case where there is no
+ * userspace around to do it, e.g. a multi-device root filesystem mounted
+ * without an initrd.
+ *
+ * Devices are opened by their /dev name, which is what the rest of btrfs
+ * records in device->name.
+ */
+void btrfs_scan_all_devices(void)
+{
+	struct class_dev_iter iter;
+	struct device *dev;
+
+	lockdep_assert_held(&uuid_mutex);
+
+	/*
+	 * Every block device, not just whole disks: partitions are registered
+	 * with part_type rather than disk_type, and a btrfs filesystem usually
+	 * lives on one, so a type filter here would skip exactly the devices
+	 * we are looking for.  A disk that has partitions is filtered out by
+	 * btrfs_can_scan_disk() below, which can say why.
+	 */
+	class_dev_iter_init(&iter, &block_class, NULL, NULL);
+	while ((dev = class_dev_iter_next(&iter))) {
+		struct gendisk *disk = dev_to_disk(dev);
+		struct block_device *bdev = dev_to_bdev(dev);
+
+		if (btrfs_can_scan_disk(disk, bdev)) {
+			/* Longest is "/dev/" plus a BDEVNAME_SIZE name. */
+			char path[sizeof("/dev/") + BDEVNAME_SIZE];
+			struct btrfs_device *device;
+
+			snprintf(path, sizeof(path), "/dev/%s", dev_name(dev));
+
+			device = btrfs_scan_one_device(path, false);
+			if (IS_ERR(device))
+				btrfs_debug(NULL, "skipping %s: %ld\n", path,
+					    PTR_ERR(device));
+		}
+	}
+	class_dev_iter_exit(&iter);
+}
+
+#endif /* CONFIG_BTRFS_ROOT_SCAN */
+
 /*
  * Find the first pending extent intersecting a range.
  *
diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
index 1c4cfb21ad12..814d801db2e8 100644
--- a/fs/btrfs/volumes.h
+++ b/fs/btrfs/volumes.h
@@ -744,6 +744,9 @@ #define BTRFS_DEV_LOOKUP_ARGS(name) \
 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
 		       blk_mode_t flags, void *holder);
 struct btrfs_device *btrfs_scan_one_device(const char *path, bool mount_arg_dev);
+#ifdef CONFIG_BTRFS_ROOT_SCAN
+void btrfs_scan_all_devices(void);
+#endif
 int btrfs_forget_devices(dev_t devt);
 void btrfs_close_devices(struct btrfs_fs_devices *fs_devices);
 void btrfs_release_device_allow_freeze(struct file *bdev_file);

  parent reply	other threads:[~2026-11-16 22:43 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16 22:43 [PATCH 0/8] btrfs: assemble a multi-device root filesystem without an initrd Ismael Luceno
2026-11-16 22:43 ` [PATCH 1/8] btrfs: add config options for the root device scan Ismael Luceno
2026-11-16 22:43 ` [PATCH 2/8] init: mount devtmpfs before the root filesystem Ismael Luceno
2026-11-16 22:43 ` [PATCH 3/8] init: let filesystems ask whether they are mounting the root device Ismael Luceno
2026-11-17  7:41   ` kernel test robot
2026-11-16 22:43 ` Ismael Luceno [this message]
2026-11-17 13:10   ` [PATCH 4/8] btrfs: add a helper to scan all block devices kernel test robot
2026-11-16 22:43 ` [PATCH 5/8] btrfs: wait for the root filesystem to have all its devices Ismael Luceno
2026-11-18  1:19   ` kernel test robot
2026-11-16 22:43 ` [PATCH 6/8] Documentation: btrfs: document waiting for the root filesystem devices Ismael Luceno
2026-11-16 22:43 ` [PATCH 7/8] init: let a filesystem resolve a root device by UUID or label Ismael Luceno
2026-11-16 22:43 ` [PATCH 8/8] btrfs: resolve root=UUID= and root=LABEL= Ismael Luceno
2026-11-16 23:41 ` [PATCH 0/8] btrfs: assemble a multi-device root filesystem without an initrd Qu Wenruo
2026-11-17 17:36   ` Ismael Luceno
2026-11-17 20:00     ` Qu Wenruo
2026-11-18 12:07       ` Ismael Luceno
2026-11-18 20:33         ` Qu Wenruo
2026-11-20 12:13 ` Christian Brauner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261116224316.11237-14-ismael@iodev.co.uk \
    --to=ismael@iodev.co.uk \
    --cc=dsterba@suse.com \
    --cc=iluceno@suse.de \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mason@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®