Skip to contentSkip to conversation

udev causing stale device nodes

PATCH Arnd Bergmann 4 participants 5 messages Feb 23, 2004

lore
Share & tools
lore
#0 lore raw

I got a report about symlinks in /udev pointing to the wrong disks. It turned out that the system had crashed and during the next reboot the disks were detected in a different order. The symlinks were created with the disk label but pointing to the device nodes from the previous boot:

/udev/dasda
/udev/dasda1
/udev/dasdb
/udev/dasdb1
/udev/dasd/label/0X1000/disc -> ../../dasda
/udev/dasd/label/0X1000/part1 -> ../../dasda1
/udev/dasd/label/0X1001/disc -> ../../dasdb
/udev/dasd/label/0X1001/part1 -> ../../dasdb1
/udev/dasd/label/0X1002/disc -> ../../dasdb
/udev/dasd/label/0X1002/part1 -> ../../dasdb1

When the 0X1002 device appeared, the 0X1001 symlink still pointed to the old device node.
The worst thing that can happen is that two devices are added in reverse order so both symlinks point to a wrong device. The two solutions I could come up with are
a) Always remove all udev-created nodes before udev starts
b) When a device appears, remove everything previously associated with that kernel name.
The patch below implements b) in a simple way, but I'm not sure if there are some races left where a device node might be on disc but not yet in udev's database.

	Arnd <><
udev.c +4 -3
diff -u udev-018/udev.c udev-018-patched/udev.c
@@ -163,13 +163,14 @@
 	/* initialize the naming deamon */
 	namedev_init();
 
-	if (strcmp(action, "add") = 0)
+	if (strcmp(action, "add") = 0) {
+		udev_remove_device(devpath, subsystem);
 		retval = udev_add_device(devpath, subsystem, 0);
 
-	else if (strcmp(action, "remove") = 0)
+	} else if (strcmp(action, "remove") = 0) {
 		retval = udev_remove_device(devpath, subsystem);
 
-	else {
+	} else {
 		dbg("unknown action '%s'", action);
 		retval = -EINVAL;
 	}

-------------------------------------------------------
SF.Net is sponsored by: Speed Start Your Linux Apps Now. Build and deploy apps & Web services for Linux with
a free DVD software kit from IBM. Click Now!
http://ads.osdn.com/?ad_id56&alloc_id438&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

#1 lore raw
Quoted 1 line · Arnd Bergmann

a) Always remove all udev-created nodes before udev starts

Do you know a good reason to not rm the node and symlink before creating them? I do it that way.

udev-add.c renamed from udev-add.c.orig +2 -7
@@ -103,6 +103,7 @@ static int make_node(char *filename, int
 {
 	int retval;
 
+	unlink(filename);
 	retval = mknod(filename, mode, makedev(major, minor));
 	if (retval != 0) {
 		dbg("mknod(%s, %#o, %u, %u) failed with error '%s'",
@@ -133,7 +134,6 @@ static int make_node(char *filename, int
 
 static int create_node(struct udevice *dev, int fake)
 {
-	struct stat stats;
 	char filename[255];
 	char linktarget[255];
 	char partitionname[255];
@@ -247,18 +247,13 @@ static int create_node(struct udevice *d
 				i++;
 			}
 
-			if (linktarget[0] = '\0')
-				strcpy(linktarget, "./");
 			strcat(linktarget, &dev->name[tail]);
 
 			/* unlink existing files to ensure that our symlink is created */
-			if (!fake && (lstat(filename, &stats) = 0)) {
-				if ((stats.st_mode & S_IFMT) != S_IFDIR) {
+			if (!fake)
 					if (unlink(filename))
 						dbg("unlink(%s) failed with error '%s'",
 						    filename, strerror(errno));
-				}
-			}
 
 			dbg("symlink(%s, %s)", linktarget, filename);
 			if (!fake) {
Signature · 14 lines
USB is for mice, FireWire is for men!

sUse lINUX ag, nÜRNBERG


-------------------------------------------------------
SF.Net is sponsored by: Speed Start Your Linux Apps Now.
Build and deploy apps & Web services for Linux with
a free DVD software kit from IBM. Click Now!
http://ads.osdn.com/?ad_id56&alloc_id438&op=click
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel
#2 lore raw
In reply to Arnd Bergmann
Quoted 5 lines · Arnd Bergmann

I got a report about symlinks in /udev pointing to the wrong disks. It turned out that the system had crashed and during the next reboot the disks were detected in a different order. The symlinks were created with the disk label but pointing to the device nodes from the previous boot:

Why not just put /dev on a ramfs and then this problem would not even happen?

thanks,

greg k-h

-------------------------------------------------------
SF.Net is sponsored by: Speed Start Your Linux Apps Now. Build and deploy apps & Web services for Linux with
a free DVD software kit from IBM. Click Now!
http://ads.osdn.com/?ad_id56&alloc_id438&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

#3 lore raw
In reply to Arnd Bergmann
Quoted 9 lines · Greg KH
Quoted 5 lines · Arnd Bergmann

I got a report about symlinks in /udev pointing to the wrong disks. It turned out that the system had crashed and during the next reboot the disks were detected in a different order. The symlinks were created with the disk label but pointing to the device nodes from the previous boot:

Why not just put /dev on a ramfs and then this problem would not even happen?

And you get a diskless udev-db for free with ramfs. :)

Kay

-------------------------------------------------------
SF.Net is sponsored by: Speed Start Your Linux Apps Now. Build and deploy apps & Web services for Linux with
a free DVD software kit from IBM. Click Now!
http://ads.osdn.com/?ad_id56&alloc_id438&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

#4 lore raw
In reply to Arnd Bergmann

Greg KH <[email protected]> schrieb am 23.02.2004, 23:35:25:

Quoted 2 lines

Why not just put /dev on a ramfs and then this problem would not even happen?

Of course, that would fix it. However, I'm not comfortable with requiring /dev to be on ramfs. If users wants to have /dev on a writable root file system, they should be able to do this without risking data corruption by accessing the wrong devices.

After all, having /dev as part of / is the traditional setup.

       Arnd <><

-------------------------------------------------------
SF.Net is sponsored by: Speed Start Your Linux Apps Now. Build and deploy apps & Web services for Linux with
a free DVD software kit from IBM. Click Now!
http://ads.osdn.com/?ad_id56&alloc_id438&op=click
_______________________________________________
Linux-hotplug-devel mailing list http://linux-hotplug.sourceforge.net
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel