[PATCH 1/2] fs: devfs: make cdev_find_free_index() return 0 instead of an error

Sascha Hauer s.hauer at pengutronix.de
Tue Jun 30 05:39:55 PDT 2026


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 at 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




More information about the barebox mailing list