From: Sascha Hauer <s.hauer@pengutronix.de>
To: BAREBOX <barebox@lists.infradead.org>
Subject: [PATCH 1/2] fs: devfs: make cdev_find_free_index() return 0 instead of an error
Date: Tue, 30 Jun 2026 14:39:55 +0200 [thread overview]
Message-ID: <20260630-usb-disk-alias-v1-1-d23318bcb684@pengutronix.de> (raw)
In-Reply-To: <20260630-usb-disk-alias-v1-0-d23318bcb684@pengutronix.de>
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
next prev parent reply other threads:[~2026-06-30 12:41 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
2026-06-30 12:39 ` [PATCH 2/2] usb: " Sascha Hauer
2026-07-07 6:47 ` [PATCH 0/2] USB: " Sascha Hauer
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=20260630-usb-disk-alias-v1-1-d23318bcb684@pengutronix.de \
--to=s.hauer@pengutronix.de \
--cc=barebox@lists.infradead.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