* [PATCH 1/2] fs: devfs: make cdev_find_free_index() return 0 instead of an error
2026-06-30 12:39 [PATCH 0/2] USB: storage: add usbdisk alias for USB storage devices Sascha Hauer
@ 2026-06-30 12:39 ` Sascha Hauer
2026-06-30 12:39 ` [PATCH 2/2] usb: storage: add usbdisk alias for USB storage devices Sascha Hauer
2026-07-07 6:47 ` [PATCH 0/2] USB: " Sascha Hauer
2 siblings, 0 replies; 4+ messages in thread
From: Sascha Hauer @ 2026-06-30 12:39 UTC (permalink / raw)
To: BAREBOX
We should never run out of free indexes in realistic scenarios. When we
do, returning 0 ensures that the caller's cdev registration just fails
with -EEXIST, which is a normal error path. Add an error message so the
user realizes that something is wrong here and not in the devfs
registration itself.
With this the callers no longer need to handle a negative return value,
so drop their (incomplete) error handling.
Assisted-by: Claude Opus 4.8
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
drivers/ata/disk_ata_drive.c | 2 --
drivers/hw_random/core.c | 13 +++----------
drivers/usb/storage/usb.c | 2 --
fs/devfs-core.c | 10 +++++++++-
4 files changed, 12 insertions(+), 15 deletions(-)
diff --git a/drivers/ata/disk_ata_drive.c b/drivers/ata/disk_ata_drive.c
index c5bf17d863..0509b7dc78 100644
--- a/drivers/ata/disk_ata_drive.c
+++ b/drivers/ata/disk_ata_drive.c
@@ -238,8 +238,6 @@ static int ata_port_init(struct ata_port *port)
port->blk.cdev.name = xstrdup(port->devname);
} else {
rc = cdev_find_free_index("ata");
- if (rc == -1)
- pr_err("Cannot find a free index for the disk node\n");
port->blk.cdev.name = basprintf("ata%d", rc);
}
diff --git a/drivers/hw_random/core.c b/drivers/hw_random/core.c
index 9f73aa45bc..ef960b787c 100644
--- a/drivers/hw_random/core.c
+++ b/drivers/hw_random/core.c
@@ -67,19 +67,12 @@ static int hwrng_register_cdev(struct hwrng *rng)
struct device *dev = rng->dev;
const char *alias;
char *devname;
- int err;
alias = of_alias_get(dev->of_node);
- if (alias) {
+ if (alias)
devname = xstrdup(alias);
- } else {
- err = cdev_find_free_index("hwrng");
- if (err < 0) {
- dev_err(dev, "no index found to name device\n");
- return err;
- }
- devname = xasprintf("hwrng%d", err);
- }
+ else
+ devname = xasprintf("hwrng%d", cdev_find_free_index("hwrng"));
rng->cdev.name = devname;
rng->cdev.flags = DEVFS_IS_CHARACTER_DEV;
diff --git a/drivers/usb/storage/usb.c b/drivers/usb/storage/usb.c
index b3116dc6e6..2ad08183f6 100644
--- a/drivers/usb/storage/usb.c
+++ b/drivers/usb/storage/usb.c
@@ -415,8 +415,6 @@ static int usb_stor_add_blkdev(struct us_data *us, unsigned char lun)
goto BadDevice;
result = cdev_find_free_index("disk");
- if (result == -1)
- pr_err("Cannot find a free number for the disk node\n");
dev_info(dev, "registering as disk%d\n", result);
pblk_dev->blk.cdev.name = basprintf("disk%d", result);
diff --git a/fs/devfs-core.c b/fs/devfs-core.c
index 67ce678517..522d883e1c 100644
--- a/fs/devfs-core.c
+++ b/fs/devfs-core.c
@@ -223,7 +223,15 @@ int cdev_find_free_index(const char *basename)
return i;
}
- return -EBUSY; /* all indexes are used */
+ /*
+ * We should never run out of free indexes in realistic scenarios.
+ * Return 0 so that the caller's cdev registration fails with -EEXIST,
+ * which is a normal error path. The message tells the user that
+ * something is wrong here and not in the devfs registration itself.
+ */
+ pr_err("Cannot find a free index for '%s'\n", basename);
+
+ return 0;
}
static struct cdev *cdev_get_master(struct cdev *cdev)
--
2.47.3
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH 2/2] usb: storage: add usbdisk alias for USB storage devices
2026-06-30 12:39 [PATCH 0/2] USB: storage: add usbdisk alias for USB storage devices Sascha Hauer
2026-06-30 12:39 ` [PATCH 1/2] fs: devfs: make cdev_find_free_index() return 0 instead of an error Sascha Hauer
@ 2026-06-30 12:39 ` Sascha Hauer
2026-07-07 6:47 ` [PATCH 0/2] USB: " Sascha Hauer
2 siblings, 0 replies; 4+ messages in thread
From: Sascha Hauer @ 2026-06-30 12:39 UTC (permalink / raw)
To: BAREBOX
Both MMC/SD and USB storage devices can be registered as "disk0" so
there is no way to access the first USB storage device. Add an "usbdisk"
alias so a user can easily detect the presence of a USB storage device
and acces it.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
drivers/usb/storage/usb.c | 15 ++++++++++++---
1 file changed, 12 insertions(+), 3 deletions(-)
diff --git a/drivers/usb/storage/usb.c b/drivers/usb/storage/usb.c
index 2ad08183f6..6251a6e5a5 100644
--- a/drivers/usb/storage/usb.c
+++ b/drivers/usb/storage/usb.c
@@ -399,6 +399,7 @@ static int usb_stor_add_blkdev(struct us_data *us, unsigned char lun)
struct device *dev = &us->pusb_dev->dev;
struct us_blk_dev *pblk_dev;
int result;
+ char *diskname = NULL, *alias = NULL;
/* allocate a new USB block device */
pblk_dev = xzalloc(sizeof(struct us_blk_dev));
@@ -414,10 +415,12 @@ static int usb_stor_add_blkdev(struct us_data *us, unsigned char lun)
if (result < 0)
goto BadDevice;
- result = cdev_find_free_index("disk");
- dev_info(dev, "registering as disk%d\n", result);
+ diskname = xasprintf("disk%d", cdev_find_free_index("disk"));
+ alias = xasprintf("usbdisk%d", cdev_find_free_index("usbdisk"));
- pblk_dev->blk.cdev.name = basprintf("disk%d", result);
+ dev_info(dev, "registering as %s (%s)\n", diskname, alias);
+
+ pblk_dev->blk.cdev.name = diskname;
pblk_dev->blk.blockbits = SECTOR_SHIFT;
pblk_dev->blk.type = BLK_TYPE_USB;
pblk_dev->blk.removable = true;
@@ -429,6 +432,9 @@ static int usb_stor_add_blkdev(struct us_data *us, unsigned char lun)
goto BadDevice;
}
+ devfs_add_alias(&pblk_dev->blk.cdev, alias);
+ free(alias);
+
list_add_tail(&pblk_dev->list, &us->blk_dev_list);
dev_dbg(dev, "USB disk device successfully added\n");
@@ -437,6 +443,9 @@ static int usb_stor_add_blkdev(struct us_data *us, unsigned char lun)
BadDevice:
dev_dbg(dev, "%s failed with %d\n", __func__, result);
free(pblk_dev);
+ free(diskname);
+ free(alias);
+
return result;
}
--
2.47.3
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH 0/2] USB: storage: add usbdisk alias for USB storage devices
2026-06-30 12:39 [PATCH 0/2] USB: storage: add usbdisk alias for USB storage devices Sascha Hauer
2026-06-30 12:39 ` [PATCH 1/2] fs: devfs: make cdev_find_free_index() return 0 instead of an error Sascha Hauer
2026-06-30 12:39 ` [PATCH 2/2] usb: storage: add usbdisk alias for USB storage devices Sascha Hauer
@ 2026-07-07 6:47 ` Sascha Hauer
2 siblings, 0 replies; 4+ messages in thread
From: Sascha Hauer @ 2026-07-07 6:47 UTC (permalink / raw)
To: BAREBOX, Sascha Hauer
On Tue, 30 Jun 2026 14:39:54 +0200, Sascha Hauer wrote:
> This is an alternative approach for:
>
> https://lore.kernel.org/p/20260623142646.87173-1-chalianis1@gmail.com
>
> Instead of adding a new Kconfig variable we add a /dev/usbdiskx alias
> for each USB storage device. This allows us to easily use the first USB
> storage device instead of stumbling upon other devices which are also
> registered as /dev/diskx.
>
> [...]
Applied, thanks!
[1/2] fs: devfs: make cdev_find_free_index() return 0 instead of an error
https://git.pengutronix.de/cgit/barebox/commit/?id=3555017d7539 (link may not be stable)
[2/2] usb: storage: add usbdisk alias for USB storage devices
https://git.pengutronix.de/cgit/barebox/commit/?id=9b7c7f7b18d7 (link may not be stable)
Best regards,
--
Sascha Hauer <s.hauer@pengutronix.de>
^ permalink raw reply [flat|nested] 4+ messages in thread