mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 2/2] cdev: add cdev_alloc helper for creating cdevs
Date: Wed,  3 Jan 2024 11:20:35 +0100	[thread overview]
Message-ID: <20240103102035.2630735-2-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20240103102035.2630735-1-a.fatoum@pengutronix.de>

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




  reply	other threads:[~2024-01-03 10:21 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-03 10:20 [PATCH 1/2] cdev: simplify loop in cdev_by_device_node Ahmad Fatoum
2024-01-03 10:20 ` Ahmad Fatoum [this message]
2024-01-04 11:17 ` 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=20240103102035.2630735-2-a.fatoum@pengutronix.de \
    --to=a.fatoum@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