Skip to contentSkip to conversation

Fix /dev to /sys node name translation

PATCH Applied Stanislav Brabec 2 participants 3 messages May 27, 2015 – May 28, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix /dev to /sys node name translation

d0dc6c1 introduced translation of /sys names to /dev names, as required by the kernel linux/drivers/base/core.c: device_get_devnode(). But there are other places of code that use /dev names in /sys. They need reverse translation from '/' to '!'.

For example, fdisk -l returns empty list since a22c6eb for device nodes in subdirectories (used e. g. by cciss driver).

Introduce yet another helper sysfs_dev_name_to_devname() and use it where appropriate.

---
 include/sysfs.h | 20 +++++++++++++++++++-
 lib/sysfs.c     |  8 ++++++--
 2 files changed, 25 insertions(+), 3 deletions(-)
include/sysfs.h +19 -1
index 4564124..6b08bbe 100644
@@ -98,7 +98,7 @@ extern int sysfs_scsi_path_contains(struct sysfs_cxt *cxt, const char *pattern);
  * Linux kernel linux/drivers/base/core.c: device_get_devnode()
  * defines a replacement of '!' in the /sys device name by '/' in the
  * /dev device name. This helper replaces all ocurrences of '!' in
- * @name by '/'.
+ * @name by '/' to convert from /sys to /dev.
  */
 static inline void sysfs_devname_to_dev_name (char *name)
 {
@@ -109,4 +109,22 @@ static inline void sysfs_devname_to_dev_name (char *name)
 			c[0] = '/';
 }
 
+/**
+ * sysfs_dev_name_to_devname:
+ * @name: devname to be converted in place
+ *
+ * Linux kernel linux/drivers/base/core.c: device_get_devnode()
+ * defines a replacement of '!' in the /sys device name by '/' in the
+ * /dev device name. This helper replaces all ocurrences of '/' in
+ * @name by '!' to convert from /dev to /sys.
+ */
+static inline void sysfs_dev_name_to_devname (char *name)
+{
+	char *c;
+
+	if (name)
+		while ((c = strchr(name, '/')))
+			c[0] = '!';
+}
+
 #endif /* UTIL_LINUX_SYSFS_H */
lib/sysfs.c +6 -2
index 8417d2d..34a5207 100644
@@ -74,10 +74,14 @@ dev_t sysfs_devname_to_devno(const char *name, const char *parent)
 
 	} else if (!dev) {
 		/*
-		 * Create path to /sys/block/<name>/dev
+		 * Create path to /sys/block/<sysname>/dev
 		 */
+		char sysname[PATH_MAX];
+
+		strncpy(sysname, name, sizeof(sysname));
+		sysfs_dev_name_to_devname(sysname);
 		int len = snprintf(buf, sizeof(buf),
-				_PATH_SYS_BLOCK "/%s/dev", name);
+				_PATH_SYS_BLOCK "/%s/dev", sysname);
 		if (len < 0 || (size_t) len + 1 > sizeof(buf))
 			return 0;
 		path = buf;
Signature · 13 lines
2.4.1

-- 
Best Regards / S pozdravem,

Stanislav Brabec
software developer
---------------------------------------------------------------------
SUSE LINUX, s. r. o.                          e-mail: [email protected]
Lihovarská 1060/12                            tel: +49 911 7405384547
190 00 Praha 9                                 fax:  +420 284 084 001
Czech Republic                                    http://www.suse.cz/
PGP: 830B 40D5 9E05 35D8 5E27 6FA3 717C 209F A04F CD76
maintainer Applied
#1 lore raw
Quoted 3 lines · Stanislav Brabec
 include/sysfs.h | 20 +++++++++++++++++++-
 lib/sysfs.c     |  8 ++++++--
 2 files changed, 25 insertions(+), 3 deletions(-)

Applied, thanks.

Quoted 2 lines

static inline void sysfs_devname_to_dev_name (char *name)
+static inline void sysfs_dev_name_to_devname (char *name)

but I have renamed to

sysfs_devname_sys_to_dev()
sysfs_devname_dev_to_sys()

because the original names have been too tricky :-)

I have also changed some thing in sysfs.c, it would be nice if we can keep the crazy sysfs devnames (with '!') really only in sysfs specific code and externally provide regular device names.

Please, test it -- especially lsblk.

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#2 lore raw
Quoted 1 line · Karel Zak

because the original names have been too tricky :-)

When I created the first one, I did not think that reverse will be needed.

Quoted 5 lines

I have also changed some thing in sysfs.c, it would be nice if we can keep the crazy sysfs devnames (with '!') really only in sysfs specific code and externally provide regular device names.

Please, test it -- especially lsblk.

fdisk -l works, lsblk -f is broken again. lsblk worked before, but it used '!'.

Before starting to play with '!' (687cc5d):

cciss-test:~/util-linux # ./lsblk -f
NAME FSTYPE LABEL MOUNTPOINT
cciss!c0d0

├─cciss!c0d0p1              
├─cciss!c0d0p2              
└─cciss!c0d0p3              

cciss-test:~/util-linux # ./fdisk -l
cciss-test:~/util-linux #

After 759b120: lib/sysfs: Fix /dev to /sys node name translation:
cciss-test:~/util-linux # ./lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
cciss!c0d0

├─cciss!c0d0p1 ext3   Boot  126055ae-08fd-4da3-a8ad-7e150d268edb /boot
├─cciss!c0d0p2 swap         e4e0d080-2b69-4ece-aeb5-e2190ad2934b [SWAP]
└─cciss!c0d0p3 btrfs        6b184ec0-512c-4620-8dc1-856a0b85969f /

cciss-test:~/util-linux # ./fdisk -l
Disk /dev/cciss/c0d0: 136.7 GiB, 146778685440 bytes, 286677120 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x0002fc74

Device Boot Start End Sectors Size Id Type
/dev/cciss/c0d0p1 * 2048 321535 319488 156M 83 Linux
/dev/cciss/c0d0p2 321536 4530175 4208640 2G 82 Linux swap / Solaris
/dev/cciss/c0d0p3 4530176 286676991 282146816 134.6G 83 Linux
cciss-test:~/util-linux #

HEAD d5dbd57:
cciss-test:~/util-linux # ./lsblk -f cciss-test:~/util-linux # ./fdisk -l
Disk /dev/cciss/c0d0: 136.7 GiB, 146778685440 bytes, 286677120 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x0002fc74

Device Boot Start End Sectors Size Id Type
/dev/cciss/c0d0p1 * 2048 321535 319488 156M 83 Linux
/dev/cciss/c0d0p2 321536 4530175 4208640 2G 82 Linux swap / Solaris
/dev/cciss/c0d0p3 4530176 286676991 282146816 134.6G 83 Linux
cciss-test:~/util-linux #

Signature · 10 lines
Best Regards / S pozdravem,

Stanislav Brabec
software developer
---------------------------------------------------------------------
SUSE LINUX, s. r. o.                          e-mail: [email protected]
Lihovarská 1060/12                            tel: +49 911 7405384547
190 00 Praha 9                                 fax:  +420 284 084 001
Czech Republic                                    http://www.suse.cz/
PGP: 830B 40D5 9E05 35D8 5E27 6FA3 717C 209F A04F CD76