[PATCH] Symlinks from devices to classes

From: Matthew Wilcox
Date: Thu Mar 03 2005 - 09:43:38 EST



When looking through sysfs, I find myself frequently wanting to see
the class attributes associated with a device. At the moment, that's
inconvenient to do -- you have to go back up to /sys/class/<classname>,
find the device and then look at its attributes. Additionally, you may
not realise that a device already has attributes available through one
of its classes.

This patch adds links from the device back to the classes that it has.
Things will go wrong if a device has attributes with the same name as
the classes it belongs to, but that's a really confusing thing for the
device to have anyway, so I think people should be forced to rename
their attributes if they've done that.

There is one nasty I've discovered with this patch:

# ls -l /sys/devices/parisc/0/0:0/pci0000:00/0000:00:04.0 |grep tty
tty -> ../../../../../../class/tty/ttyS2
tty -> ../../../../../../class/tty/ttyS2
tty -> ../../../../../../class/tty/ttyS2

This happens because this is a PCI device with three serial ports:

# ls -l /sys/class/tty/ttyS[0-2] |grep device
device -> ../../../devices/parisc/0/0:0/pci0000:00/0000:00:04.0
device -> ../../../devices/parisc/0/0:0/pci0000:00/0000:00:04.0
device -> ../../../devices/parisc/0/0:0/pci0000:00/0000:00:04.0

so we create the symlink three times. What's the right way to solve
this? Have sysfs decline to create a symlink if an entry of the same
name already exists? Should we create 'ttyS0', 'ttyS1' and 'ttyS2'
directories in the parent's directory? We'll have to do that anyway if
we want ttyS* to have sysfs attributes (and presumably we do, eventually)

Signed-off-by: Matthew Wilcox <matthew@xxxxxx>

Index: ./drivers/base/class.c
===================================================================
RCS file: /var/lib/cvs/linux-2.6/drivers/base/class.c,v
retrieving revision 1.15
diff -u -p -r1.15 class.c
--- ./drivers/base/class.c 12 Jan 2005 20:16:13 -0000 1.15
+++ ./drivers/base/class.c 20 Feb 2005 22:01:07 -0000
@@ -197,15 +197,33 @@ void class_device_remove_bin_file(struct

static int class_device_dev_link(struct class_device * class_dev)
{
- if (class_dev->dev)
- return sysfs_create_link(&class_dev->kobj,
- &class_dev->dev->kobj, "device");
+ int retval = 0;
+ if (!class_dev->dev)
+ goto out;
+
+ retval = sysfs_create_link(&class_dev->kobj, &class_dev->dev->kobj,
+ "device");
+ if (retval)
+ goto out;
+
+ retval = sysfs_create_link(&class_dev->dev->kobj, &class_dev->kobj,
+ class_dev->class->name);
+ if (retval)
+ goto unlink;
+
return 0;
+
+ unlink:
+ sysfs_remove_link(&class_dev->kobj, "device");
+ out:
+ return retval;
}

static void class_device_dev_unlink(struct class_device * class_dev)
{
sysfs_remove_link(&class_dev->kobj, "device");
+ if (class_dev->dev)
+ sysfs_remove_link(&class_dev->dev->kobj, class_dev->class->name);
}

static int class_device_driver_link(struct class_device * class_dev)

--
"Next the statesmen will invent cheap lies, putting the blame upon
the nation that is attacked, and every man will be glad of those
conscience-soothing falsities, and will diligently study them, and refuse
to examine any refutations of them; and thus he will by and by convince
himself that the war is just, and will thank God for the better sleep
he enjoys after this process of grotesque self-deception." -- Mark Twain
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/