mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: BAREBOX <barebox@lists.infradead.org>
Subject: [PATCH 09/10] fs: replace cdev links with aliases
Date: Thu, 27 Nov 2025 10:19:31 +0100	[thread overview]
Message-ID: <20251127-devfs-v1-9-4aff12818757@pengutronix.de> (raw)
In-Reply-To: <20251127-devfs-v1-0-4aff12818757@pengutronix.de>

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 commands/devinfo.c         |   7 ++-
 common/partitions.c        |   2 +-
 drivers/block/dm/dm-core.c |   1 -
 fs/devfs-core.c            | 148 +++++++++++++++++++--------------------------
 include/block.h            |   2 -
 include/driver.h           |   8 +--
 6 files changed, 71 insertions(+), 97 deletions(-)

diff --git a/commands/devinfo.c b/commands/devinfo.c
index c87b30e84307e5053bc35e8bd70ac7af63f688b8..d898e81b3278808a7eed5116d210020d8715a09f 100644
--- a/commands/devinfo.c
+++ b/commands/devinfo.c
@@ -9,7 +9,8 @@
 static int do_devinfo_subtree(struct device *dev, int depth)
 {
 	struct device *child;
-	struct cdev *cdev, *cdevl;
+	struct cdev *cdev;
+	struct string_list *sl;
 	int i;
 
 	for (i = 0; i < depth; i++)
@@ -26,8 +27,8 @@ static int do_devinfo_subtree(struct device *dev, int depth)
 					cdev->offset + cdev->size - 1,
 					size_human_readable(cdev->size),
 					cdev->name);
-			list_for_each_entry(cdevl, &cdev->links, link_entry)
-				printf(", %s", cdevl->name);
+			string_list_for_each_entry(sl, &cdev->aliases)
+				printf(", %s", sl->str);
 			printf("\n");
 		}
 	} else {
diff --git a/common/partitions.c b/common/partitions.c
index 7563cb0e6767891ee7f1fe264ce86703408f790a..1a4e046c5f55314ccb2abf5094b0966def8c5b55 100644
--- a/common/partitions.c
+++ b/common/partitions.c
@@ -63,7 +63,7 @@ static int register_one_partition(struct block_device *blk, struct partition *pa
 		return 0;
 
 	partition_name = xasprintf("%s.%s", blk->cdev.name, part->name);
-	ret = devfs_create_link(cdev, partition_name);
+	ret = devfs_add_alias(cdev, partition_name);
 	if (ret)
 		dev_warn(blk->dev, "Failed to create link from %s to %s\n",
 			 partition_name, cdev->name);
diff --git a/drivers/block/dm/dm-core.c b/drivers/block/dm/dm-core.c
index fd7ed0d84ed7388f8c1cd4aa19e0466191459fd6..2f48cfeb06f7b2da6d26850820f228a6f522fc2f 100644
--- a/drivers/block/dm/dm-core.c
+++ b/drivers/block/dm/dm-core.c
@@ -77,7 +77,6 @@ int dm_cdev_open(struct dm_cdev *dmcdev, const char *path, ulong flags,
 			return -ENODEV;
 		}
 
-		dmcdev->cdev = cdev_readlink(dmcdev->cdev);
 		break;
 	default:
 		*errmsg = xstrdup("Only regular files and device specials are supported");
diff --git a/fs/devfs-core.c b/fs/devfs-core.c
index c4ff5fe5718ea2c0236fdbf124e56a13e17702d1..32e9b5f867ed1d1696a4c16fb154474f933de342 100644
--- a/fs/devfs-core.c
+++ b/fs/devfs-core.c
@@ -50,42 +50,21 @@ int devfs_partition_complete(struct string_list *sl, char *instr)
 }
 #endif
 
-struct cdev *cdev_readlink(const struct cdev *cdev)
-{
-	if (!cdev)
-		return NULL;
-
-	if (cdev->link)
-		cdev = cdev->link;
-
-	/* links to links are not allowed */
-	BUG_ON(cdev->link);
-
-	return (void *)cdev;
-}
-
-struct cdev *lcdev_by_name(const char *filename)
+struct cdev *cdev_by_name(const char *filename)
 {
 	struct cdev *cdev;
+	struct string_list *entry;
 
 	for_each_cdev(cdev) {
 		if (!strcmp(cdev->name, filename))
 			return cdev;
+		string_list_for_each_entry(entry, &cdev->aliases)
+			if (!strcmp(entry->str, filename))
+				return cdev;
 	}
 	return NULL;
 }
 
-struct cdev *cdev_by_name(const char *filename)
-{
-	struct cdev *cdev;
-
-	cdev = lcdev_by_name(filename);
-	if (!cdev)
-		return NULL;
-
-	return cdev_readlink(cdev);
-}
-
 struct cdev *cdev_by_device_node(struct device_node *node)
 {
 	struct cdev *cdev;
@@ -95,7 +74,7 @@ struct cdev *cdev_by_device_node(struct device_node *node)
 
 	for_each_cdev(cdev) {
 		if (cdev_of_node(cdev) == node)
-			return cdev_readlink(cdev);
+			return cdev;
 	}
 	return NULL;
 }
@@ -133,9 +112,6 @@ cdev_find_child_by_gpt_typeuuid(struct cdev *cdev, const guid_t *typeuuid)
 {
 	struct cdev *partcdev;
 
-        /* Follow links to support storage-by-alias */
-        cdev = cdev_readlink(cdev);
-
 	if (!cdev_is_gpt_partitioned(cdev))
 		return ERR_PTR(-EINVAL);
 
@@ -153,6 +129,29 @@ cdev_find_child_by_gpt_typeuuid(struct cdev *cdev, const guid_t *typeuuid)
 	return ERR_PTR(-ENOENT);
 }
 
+static bool cdev_has_partname_alias(struct cdev *cdev, const char *partname)
+{
+	char *fullname;
+	struct string_list *sl;
+	bool ret = false;
+
+	if (!cdev->master)
+		return false;
+
+	fullname = xasprintf("%s.%s", cdev->master->name, partname);
+
+	string_list_for_each_entry(sl, &cdev->aliases) {
+		if (streq_ptr(sl->str, fullname)) {
+			ret = true;
+			break;
+		}
+	}
+
+	free(fullname);
+
+	return ret;
+}
+
 /**
  * cdev_find_partition - find a partition belonging to a physical device
  *
@@ -164,15 +163,10 @@ struct cdev *cdev_find_partition(struct cdev *cdevm, const char *name)
 	struct cdev *partcdev;
 
 	for_each_cdev_partition(partcdev, cdevm) {
-		struct cdev *cdevl;
-
 		if (streq_ptr(partcdev->partname, name))
 			return partcdev;
-
-		list_for_each_entry(cdevl, &partcdev->links, link_entry) {
-			if (streq_ptr(cdevl->partname, name))
-				return cdevl;
-		}
+		if (cdev_has_partname_alias(partcdev, name))
+			return partcdev;
 	}
 
 	return NULL;
@@ -190,15 +184,11 @@ struct cdev *device_find_partition(struct device *dev, const char *name)
 	struct device *child;
 
 	list_for_each_entry(cdev, &dev->cdevs, devices_list) {
-		struct cdev *cdevl;
-
 		if (streq_ptr(cdev->partname, name))
 			return cdev;
 
-		list_for_each_entry(cdevl, &cdev->links, link_entry) {
-			if (streq_ptr(cdevl->partname, name))
-				return cdev_readlink(cdevl);
-		}
+		if (cdev_has_partname_alias(cdev, name))
+			return cdev;
 	}
 
 	device_for_each_child(dev, child) {
@@ -460,20 +450,36 @@ static void cdev_free(struct cdev *cdev)
 
 static bool devfs_initialized;
 
+static int cdev_symlink(struct cdev *cdev, const char *linkname)
+{
+	char *path;
+	int ret;
+
+	if (!devfs_initialized)
+		return 0;
+
+	path = xasprintf("/dev/%s", linkname);
+	ret = symlink(cdev->name, path);
+	free(path);
+
+	return ret;
+}
+
 static void devfs_mknod(struct cdev *cdev)
 {
 	char *path;
 	int ret;
+	struct string_list *sl;
 
 	if (!devfs_initialized)
 		return;
 
 	path = xasprintf("/dev/%s", cdev->name);
 
-	if (cdev->link)
-		ret = symlink(cdev->link->name, path);
-	else
-		ret = mknod(path, S_IFCHR | 0600, cdev->name);
+	string_list_for_each_entry(sl, &cdev->aliases)
+		cdev_symlink(cdev, sl->str);
+
+	ret = mknod(path, S_IFCHR | 0600, cdev->name);
 
 	free(path);
 
@@ -499,8 +505,8 @@ int devfs_create(struct cdev *new)
 	if (cdev)
 		return -EEXIST;
 
-	INIT_LIST_HEAD(&new->links);
 	INIT_LIST_HEAD(&new->partitions);
+	string_list_init(&new->aliases);
 
 	list_add_tail(&new->list, &cdev_list);
 	if (new->dev) {
@@ -509,46 +515,22 @@ int devfs_create(struct cdev *new)
 			new->device_node = new->dev->of_node;
 	}
 
-	if (new->link)
-		list_add_tail(&new->link_entry, &new->link->links);
-
 	devfs_mknod(new);
 
 	return 0;
 }
 
-int devfs_create_link(struct cdev *cdev, const char *name)
+int devfs_add_alias(struct cdev *cdev, const char *name)
 {
-	struct cdev *new;
-	int ret;
-
-	/*
-	 * Create a link to the real cdev instead of creating
-	 * a link to a link.
-	 */
-	cdev = cdev_readlink(cdev);
-
-	new = cdev_alloc(name);
-	new->link = cdev;
+	struct cdev *conflict;
 
-	ret = devfs_create(new);
-	if (ret) {
-		cdev_free(new);
-		return ret;
-	}
-
-	if (cdev->partname) {
-		size_t partnameoff = 0;
-
-		if (cdev->master) {
-			size_t masterlen = strlen(cdev->master->name);
+	conflict = cdev_by_name(name);
+	if (conflict)
+		return -EEXIST;
 
-			if (!strncmp(name, cdev->master->name, masterlen))
-				partnameoff += masterlen + 1;
-		}
+	string_list_add(&cdev->aliases, name);
 
-		new->partname = xstrdup(name + partnameoff);
-	}
+	cdev_symlink(cdev, name);
 
 	return 0;
 }
@@ -565,8 +547,7 @@ int devfs_remove(struct cdev *cdev)
 	if (cdev->dev)
 		list_del(&cdev->devices_list);
 
-	list_for_each_entry_safe(c, tmp, &cdev->links, link_entry)
-		devfs_remove(c);
+	string_list_free(&cdev->aliases);
 
 	list_for_each_entry_safe(c, tmp, &cdev->partitions, partition_entry)
 		cdevfs_del_partition(c);
@@ -574,9 +555,6 @@ int devfs_remove(struct cdev *cdev)
 	if (cdev_is_partition(cdev))
 		list_del(&cdev->partition_entry);
 
-	if (cdev->link)
-		cdev_free(cdev);
-
 	return 0;
 }
 
@@ -681,7 +659,7 @@ static struct cdev *__devfs_add_partition(struct cdev *cdev,
 	if (overlap) {
 		if (!IS_ERR(overlap)) {
 			/* only fails with -EEXIST, which is fine */
-			(void)devfs_create_link(overlap, partinfo->name);
+			(void)devfs_add_alias(overlap, partinfo->name);
 		}
 
 		return overlap;
diff --git a/include/block.h b/include/block.h
index fc7a0a32fb2d6bc579aa6305433fb2a9e39f336a..a8ebe202288fff83677948f3d67bc12cae5dc107 100644
--- a/include/block.h
+++ b/include/block.h
@@ -104,13 +104,11 @@ static inline bool cdev_is_block_device(const struct cdev *cdev)
 
 static inline bool cdev_is_block_partition(const struct cdev *cdev)
 {
-	cdev = cdev_readlink(cdev);
 	return cdev_is_block_device(cdev) && cdev_is_partition(cdev);
 }
 
 static inline bool cdev_is_block_disk(const struct cdev *cdev)
 {
-	cdev = cdev_readlink(cdev);
 	return cdev_is_block_device(cdev) && !cdev_is_partition(cdev);
 }
 
diff --git a/include/driver.h b/include/driver.h
index 5a67d20ccff3fca339e7b40c32ecfbc244f358f5..afd22b88e5116c73bd79fc650fee9cbf6acfe247 100644
--- a/include/driver.h
+++ b/include/driver.h
@@ -17,6 +17,7 @@
 #include <init.h>
 #include <errno.h>
 #include <filetype.h>
+#include <stringlist.h>
 
 #define FORMAT_DRIVER_NAME_ID	"%s%d"
 
@@ -432,8 +433,7 @@ struct cdev {
 	u16 typeflags; /* GPT type-specific attributes */
 	int open;
 	struct mtd_info *mtd;
-	struct cdev *link;
-	struct list_head link_entry, links;
+	struct string_list aliases;
 	struct list_head partition_entry, partitions;
 	struct cdev *master;
 	enum filetype filetype;
@@ -461,13 +461,11 @@ static inline const char *cdev_name(struct cdev *cdev)
 
 void devfs_init(void);
 int devfs_create(struct cdev *);
-int devfs_create_link(struct cdev *, const char *name);
+int devfs_add_alias(struct cdev *, const char *name);
 int devfs_remove(struct cdev *);
 int cdev_find_free_index(const char *);
 struct cdev *cdev_find_partition(struct cdev *cdevm, const char *name);
 struct cdev *device_find_partition(struct device *dev, const char *name);
-struct cdev *lcdev_by_name(const char *filename);
-struct cdev *cdev_readlink(const struct cdev *cdev);
 struct cdev *cdev_by_device_node(struct device_node *node);
 struct cdev *cdev_by_partuuid(const char *partuuid);
 struct cdev *cdev_by_diskuuid(const char *partuuid);

-- 
2.47.3




  parent reply	other threads:[~2025-11-27  9:20 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-27  9:19 [PATCH 00/10] fs: Use device special nodes for devfs Sascha Hauer
2025-11-27  9:19 ` [PATCH 01/10] fs: implement mknod Sascha Hauer
2025-11-27  9:19 ` [PATCH 02/10] commands: add mknod command Sascha Hauer
2025-11-27  9:19 ` [PATCH 03/10] fs: ramfs: add device file support Sascha Hauer
2025-11-27  9:19 ` [PATCH 04/10] cdev: add cdev_size() helper Sascha Hauer
2025-11-27  9:19 ` [PATCH 05/10] fs: fix st_size for device files Sascha Hauer
2025-11-27  9:19 ` [PATCH 06/10] fs: retire devfs as filesystem Sascha Hauer
2025-11-27  9:19 ` [PATCH 07/10] fs: include cdevname in struct stat Sascha Hauer
2025-11-27  9:19 ` [PATCH 08/10] fs: stat_print: get cdevname from stat Sascha Hauer
2025-11-27  9:19 ` Sascha Hauer [this message]
2025-11-27  9:19 ` [PATCH 10/10] ls: use ~0 for FILE_SIZE_STREAM 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=20251127-devfs-v1-9-4aff12818757@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