[PATCH 2/2] cdev: add cdev_alloc helper for creating cdevs

Ahmad Fatoum a.fatoum at pengutronix.de
Wed Jan 3 02:20:35 PST 2024


We have different helpers for creating cdevs, depending on whether they
are loop devices, partitions or links.

Create a common cdev_alloc function, so it's easier to instrument cdev
creation during debugging.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 fs/devfs-core.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/fs/devfs-core.c b/fs/devfs-core.c
index c79b092a112e..9f5b41761f52 100644
--- a/fs/devfs-core.c
+++ b/fs/devfs-core.c
@@ -324,6 +324,16 @@ int cdev_truncate(struct cdev *cdev, size_t size)
 	return -EPERM;
 }
 
+static struct cdev *cdev_alloc(const char *name)
+{
+	struct cdev *new;
+
+	new = xzalloc(sizeof(*new));
+	new->name = xstrdup(name);
+
+	return new;
+}
+
 int devfs_create(struct cdev *new)
 {
 	struct cdev *cdev;
@@ -358,8 +368,7 @@ int devfs_create_link(struct cdev *cdev, const char *name)
 	 */
 	cdev = cdev_readlink(cdev);
 
-	new = xzalloc(sizeof(*new));
-	new->name = xstrdup(name);
+	new = cdev_alloc(name);
 	new->link = cdev;
 
 	if (cdev->partname) {
@@ -540,8 +549,7 @@ struct cdev *cdevfs_add_partition(struct cdev *cdev,
 		return &mtd->cdev;
 	}
 
-	new = xzalloc(sizeof(*new));
-	new->name = strdup(partinfo->name);
+	new = cdev_alloc(partinfo->name);
 	if (!strncmp(cdev->name, partinfo->name, strlen(cdev->name)))
 		new->partname = xstrdup(partinfo->name + strlen(cdev->name) + 1);
 
@@ -679,6 +687,7 @@ static const struct cdev_operations loop_ops = {
 
 struct cdev *cdev_create_loop(const char *path, ulong flags, loff_t offset)
 {
+	char str[16];
 	struct cdev *new;
 	struct loop_priv *priv;
 	static int loopno;
@@ -692,10 +701,10 @@ struct cdev *cdev_create_loop(const char *path, ulong flags, loff_t offset)
 		return NULL;
 	}
 
-	new = xzalloc(sizeof(*new));
+	snprintf(str, sizeof(str), "loop%u", loopno++);
 
+	new = cdev_alloc(str);
 	new->ops = &loop_ops;
-	new->name = basprintf("loop%u", loopno++);
 	new->priv = priv;
 
 	ofs = lseek(priv->fd, 0, SEEK_END);
-- 
2.39.2




More information about the barebox mailing list