* patch sysfs-add-sys-dev-char-block-to-lookup-sysfs-path-by-major-minor.patch added to gregkh-2.6 tree [not found] <1208800267.26479.3.camel@dwillia2-linux.ch.intel.com> @ 2008-04-28 23:51 ` gregkh 2008-04-29 7:48 ` SL Baur 0 siblings, 1 reply; 4+ messages in thread From: gregkh @ 2008-04-28 23:51 UTC (permalink / raw) To: dan.j.williams, akpm, gregkh, greg, hpa, htejun, kay.sievers, linux-kernel, lkml, neilb, steve This is a note to let you know that I've just added the patch titled Subject: sysfs: add /sys/dev/{char,block} to lookup sysfs path by major:minor to my gregkh-2.6 tree. Its filename is sysfs-add-sys-dev-char-block-to-lookup-sysfs-path-by-major-minor.patch This tree can be found at http://www.kernel.org/pub/linux/kernel/people/gregkh/gregkh-2.6/patches/ >From dan.j.williams@intel.com Mon Apr 21 10:51:11 2008 From: Dan Williams <dan.j.williams@intel.com> Date: Mon, 21 Apr 2008 10:51:07 -0700 Subject: sysfs: add /sys/dev/{char,block} to lookup sysfs path by major:minor To: Greg KH <greg@kroah.com>, Andrew Morton <akpm@linux-foundation.org> Cc: linux-kernel <linux-kernel@vger.kernel.org> Message-ID: <1208800267.26479.3.camel@dwillia2-linux.ch.intel.com> From: Dan Williams <dan.j.williams@intel.com> Why?: There are occasions where userspace would like to access sysfs attributes for a device but it may not know how sysfs has named the device or the path. For example what is the sysfs path for /dev/disk/by-id/ata-ST3160827AS_5MT004CK? With this change a call to stat(2) returns the major:minor then userspace can see that /sys/dev/block/8:32 links to /sys/block/sdc. What are the alternatives?: 1/ Add an ioctl to return the path: Doable, but sysfs is meant to reduce the need to proliferate ioctl interfaces into the kernel, so this seems counter productive. 2/ Use udev to create these symlinks: Also doable, but it adds a udev dependency to utilities that might be running in a limited environment like an initramfs. 3/ Do a full-tree search of sysfs. [kay.sievers@vrfy.org: fix duplicate registrations] [kay.sievers@vrfy.org: cleanup suggestions] Cc: Neil Brown <neilb@suse.de> Cc: Tejun Heo <htejun@gmail.com> Cc: Kay Sievers <kay.sievers@vrfy.org> Cc: SL Baur <steve@xemacs.org> Acked-by: Kay Sievers <kay.sievers@vrfy.org> Acked-by: Mark Lord <lkml@rtr.ca> Acked-by: H. Peter Anvin <hpa@zytor.com> Signed-off-by: Dan Williams <dan.j.williams@intel.com> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de> --- Documentation/ABI/testing/sysfs-dev | 20 ++++++++ Documentation/filesystems/sysfs.txt | 6 ++ block/genhd.c | 5 +- drivers/base/class.c | 4 + drivers/base/core.c | 83 +++++++++++++++++++++++++++++++++++- drivers/usb/core/devio.c | 5 ++ include/linux/device.h | 3 + 7 files changed, 124 insertions(+), 2 deletions(-) --- a/block/genhd.c +++ b/block/genhd.c @@ -360,7 +360,10 @@ static struct kobject *base_probe(dev_t static int __init genhd_device_init(void) { - int error = class_register(&block_class); + int error; + + block_class.dev_kobj = sysfs_dev_block_kobj; + error = class_register(&block_class); if (unlikely(error)) return error; bdev_map = kobj_map_init(base_probe, &block_class_lock); --- /dev/null +++ b/Documentation/ABI/testing/sysfs-dev @@ -0,0 +1,20 @@ +What: /sys/dev +Date: April 2008 +KernelVersion: 2.6.26 +Contact: Dan Williams <dan.j.williams@intel.com> +Description: The /sys/dev tree provides a method to look up the sysfs + path for a device using the information returned from + stat(2). There are two directories, 'block' and 'char', + beneath /sys/dev containing symbolic links with names of + the form "<major>:<minor>". These links point to the + corresponding sysfs path for the given device. + + Example: + $ readlink /sys/dev/block/8:32 + ../../block/sdc + + Entries in /sys/dev/char and /sys/dev/block will be + dynamically created and destroyed as devices enter and + leave the system. + +Users: mdadm <linux-raid@vger.kernel.org> --- a/Documentation/filesystems/sysfs.txt +++ b/Documentation/filesystems/sysfs.txt @@ -248,6 +248,7 @@ The top level sysfs directory looks like block/ bus/ class/ +dev/ devices/ firmware/ net/ @@ -274,6 +275,11 @@ fs/ contains a directory for some filesy filesystem wanting to export attributes must create its own hierarchy below fs/ (see ./fuse.txt for an example). +dev/ contains two directories char/ and block/. Inside these two +directories there are symlinks named <major>:<minor>. These symlinks +point to the sysfs directory for the given device. /sys/dev provides a +quick way to lookup the sysfs interface for a device from the result of +a stat(2) operation. More information can driver-model specific features can be found in Documentation/driver-model/. --- a/drivers/base/class.c +++ b/drivers/base/class.c @@ -149,6 +149,10 @@ int class_register(struct class *cls) if (error) return error; + /* set the default /sys/dev directory for devices of this class */ + if (!cls->dev_kobj) + cls->dev_kobj = sysfs_dev_char_kobj; + #if defined(CONFIG_SYSFS_DEPRECATED) && defined(CONFIG_BLOCK) /* let the block class directory show up in the root of sysfs */ if (cls != &block_class) --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -27,6 +27,9 @@ int (*platform_notify)(struct device *dev) = NULL; int (*platform_notify_remove)(struct device *dev) = NULL; +static struct kobject *dev_kobj; +struct kobject *sysfs_dev_char_kobj; +struct kobject *sysfs_dev_block_kobj; #ifdef CONFIG_BLOCK static inline int device_is_not_partition(struct device *dev) @@ -761,6 +764,54 @@ static void device_remove_class_symlinks } /** + * device_to_dev_kobj - select a /sys/dev/ directory for the device + * @dev: device + * + * By default we select char/ for new entries. Setting class->dev_obj + * to NULL prevents an entry from being created. class->dev_kobj must + * be set (or cleared) before any devices are registered to the class + * otherwise device_create_sys_dev_entry() and + * device_remove_sys_dev_entry() will disagree about the the presence + * of the link. + */ +static struct kobject *device_to_dev_kobj(struct device *dev) +{ + struct kobject *kobj; + + if (dev->class) + kobj = dev->class->dev_kobj; + else + kobj = sysfs_dev_char_kobj; + + return kobj; +} + +static int device_create_sys_dev_entry(struct device *dev) +{ + struct kobject *kobj = device_to_dev_kobj(dev); + int error = 0; + char devt_str[15]; + + if (kobj) { + format_dev_t(devt_str, dev->devt); + error = sysfs_create_link(kobj, &dev->kobj, devt_str); + } + + return error; +} + +static void device_remove_sys_dev_entry(struct device *dev) +{ + struct kobject *kobj = device_to_dev_kobj(dev); + char devt_str[15]; + + if (kobj) { + format_dev_t(devt_str, dev->devt); + sysfs_remove_link(kobj, devt_str); + } +} + +/** * device_add - add device to device hierarchy. * @dev: device. * @@ -810,6 +861,10 @@ int device_add(struct device *dev) error = device_create_file(dev, &devt_attr); if (error) goto ueventattrError; + + error = device_create_sys_dev_entry(dev); + if (error) + goto devtattrError; } error = device_add_class_symlinks(dev); @@ -854,6 +909,9 @@ int device_add(struct device *dev) device_remove_class_symlinks(dev); SymlinkError: if (MAJOR(dev->devt)) + device_remove_sys_dev_entry(dev); + devtattrError: + if (MAJOR(dev->devt)) device_remove_file(dev, &devt_attr); ueventattrError: device_remove_file(dev, &uevent_attr); @@ -929,8 +987,10 @@ void device_del(struct device *dev) device_pm_remove(dev); if (parent) klist_del(&dev->knode_parent); - if (MAJOR(dev->devt)) + if (MAJOR(dev->devt)) { + device_remove_sys_dev_entry(dev); device_remove_file(dev, &devt_attr); + } if (dev->class) { device_remove_class_symlinks(dev); @@ -1055,7 +1115,25 @@ int __init devices_init(void) devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL); if (!devices_kset) return -ENOMEM; + dev_kobj = kobject_create_and_add("dev", NULL); + if (!dev_kobj) + goto dev_kobj_err; + sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj); + if (!sysfs_dev_block_kobj) + goto block_kobj_err; + sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj); + if (!sysfs_dev_char_kobj) + goto char_kobj_err; + return 0; + + char_kobj_err: + kobject_put(sysfs_dev_block_kobj); + block_kobj_err: + kobject_put(dev_kobj); + dev_kobj_err: + kset_unregister(devices_kset); + return -ENOMEM; } EXPORT_SYMBOL_GPL(device_for_each_child); @@ -1351,4 +1429,7 @@ void device_shutdown(void) dev->driver->shutdown(dev); } } + kobject_put(sysfs_dev_char_kobj); + kobject_put(sysfs_dev_block_kobj); + kobject_put(dev_kobj); } --- a/drivers/usb/core/devio.c +++ b/drivers/usb/core/devio.c @@ -1748,6 +1748,11 @@ int __init usb_devio_init(void) usb_classdev_class = NULL; goto out; } + /* devices of this class shadow the major:minor of their parent + * device, so clear ->dev_kobj to prevent adding duplicate entries + * to /sys/dev + */ + usb_classdev_class->dev_kobj = NULL; usb_register_notify(&usbdev_nb); #endif --- a/include/linux/device.h +++ b/include/linux/device.h @@ -194,6 +194,7 @@ struct class { struct semaphore sem; /* locks children, devices, interfaces */ struct class_attribute *class_attrs; struct device_attribute *dev_attrs; + struct kobject *dev_kobj; int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env); @@ -206,6 +207,8 @@ struct class { struct pm_ops *pm; }; +extern struct kobject *sysfs_dev_block_kobj; +extern struct kobject *sysfs_dev_char_kobj; extern int __must_check class_register(struct class *class); extern void class_unregister(struct class *class); extern int class_for_each_device(struct class *class, void *data, Patches currently in gregkh-2.6 which might be from dan.j.williams@intel.com are driver-core/sysfs-add-sys-dev-char-block-to-lookup-sysfs-path-by-major-minor.patch ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: patch sysfs-add-sys-dev-char-block-to-lookup-sysfs-path-by-major-minor.patch added to gregkh-2.6 tree 2008-04-28 23:51 ` patch sysfs-add-sys-dev-char-block-to-lookup-sysfs-path-by-major-minor.patch added to gregkh-2.6 tree gregkh @ 2008-04-29 7:48 ` SL Baur 2008-04-29 15:22 ` patch sysfs-add-sys-dev-char-block-to-lookup-sysfs-path-by-major-minor.pat ch " Greg KH 0 siblings, 1 reply; 4+ messages in thread From: SL Baur @ 2008-04-29 7:48 UTC (permalink / raw) To: gregkh@suse.de Cc: dan.j.williams, akpm, greg, hpa, htejun, kay.sievers, linux-kernel, lkml, neilb On 4/28/08, gregkh@suse.de <gregkh@suse.de> wrote: > > This is a note to let you know that I've just added the patch titled > > Subject: sysfs: add /sys/dev/{char,block} to lookup sysfs path by major:minor > > to my gregkh-2.6 tree. Its filename is > > sysfs-add-sys-dev-char-block-to-lookup-sysfs-path-by-major-minor.patch > + char devt_str[15]; > + > + if (kobj) { > + format_dev_t(devt_str, dev->devt); This part is ugly, but is not the fault of the patch. The ill- considered interface for `format_dev_t' is something that probably noone wants to fix. Otherwise, still OK with me. Reviewed-by: SL Baur <steve@xemacs.org> (Is Reviewed-by not sticky?) -sb ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: patch sysfs-add-sys-dev-char-block-to-lookup-sysfs-path-by-major-minor.pat ch added to gregkh-2.6 tree 2008-04-29 7:48 ` SL Baur @ 2008-04-29 15:22 ` Greg KH 0 siblings, 0 replies; 4+ messages in thread From: Greg KH @ 2008-04-29 15:22 UTC (permalink / raw) To: SL Baur Cc: gregkh@suse.de, dan.j.williams, akpm, hpa, htejun, kay.sievers, linux-kernel, lkml, neilb On Tue, Apr 29, 2008 at 12:48:11AM -0700, SL Baur wrote: > On 4/28/08, gregkh@suse.de <gregkh@suse.de> wrote: > > > > This is a note to let you know that I've just added the patch titled > > > > Subject: sysfs: add /sys/dev/{char,block} to lookup sysfs path by major:minor > > > > to my gregkh-2.6 tree. Its filename is > > > > sysfs-add-sys-dev-char-block-to-lookup-sysfs-path-by-major-minor.patch > > > + char devt_str[15]; > > + > > + if (kobj) { > > + format_dev_t(devt_str, dev->devt); > > This part is ugly, but is not the fault of the patch. The ill- > considered interface for `format_dev_t' is something that probably > noone wants to fix. Why not? Patches are always welcome, it's not like that function is used in that many different places, should be a simple change... > Otherwise, still OK with me. > > Reviewed-by: SL Baur <steve@xemacs.org> > > (Is Reviewed-by not sticky?) Yes, it is, I'll add it to my version of the patch. thanks, greg k-h ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: What to do about the 2TB limit on HDIO_GETGEO ?
@ 2008-04-15 23:43 Dan Williams
2008-04-16 20:55 ` patch sysfs-add-sys-dev-char-block-to-lookup-sysfs-path-by-major-minor.patch added to gregkh-2.6 tree gregkh
0 siblings, 1 reply; 4+ messages in thread
From: Dan Williams @ 2008-04-15 23:43 UTC (permalink / raw)
To: Andrew Morton
Cc: James Bottomley, Kay Sievers, Tejun Heo, Mark Lord, Greg KH,
Jens Axboe, Jeff Garzik, Linus Torvalds, Linux Kernel,
IDE/ATA development list, linux-scsi, H. Peter Anvin
Subject: sysfs: add /sys/dev/{char,block} to lookup sysfs path by major:minor
From: Dan Williams <dan.j.williams@intel.com>
Why?:
There are occasions where userspace would like to access sysfs
attributes for a device but it may not know how sysfs has named the
device or the path. For example what is the sysfs path for
/dev/disk/by-id/ata-ST3160827AS_5MT004CK? With this change a call to
stat(2) returns the major:minor then userspace can see that
/sys/dev/block/8:32 links to /sys/block/sdc.
What are the alternatives?:
1/ Add an ioctl to return the path: Doable, but sysfs is meant to reduce
the need to proliferate ioctl interfaces into the kernel, so this
seems counter productive.
2/ Use udev to create these symlinks: Also doable, but it adds a
udev dependency to utilities that might be running in a limited
environment like an initramfs.
3/ Do a full-tree search of sysfs.
Cc: NeilBrown <neilb@suse.de>
Cc: Tejun Heo <htejun@gmail.com>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Cc: Greg KH <gregkh@suse.de>
Acked-by: Mark Lord <lkml@rtr.ca>
Acked-by: H. Peter Anvin <hpa@zytor.com>
Reviewed-by: SL Baur <steve@xemacs.org>
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
---
Andrew here is an updated patch with some presumptive acked-by's from Mark and hpa.
* fixed up ENOMEM handling in devices_init()
* added a short blurb in Documentation/filesystems/sysfs.txt
* dropped the size of the buffer passed to format_dev_t a bit
Documentation/filesystems/sysfs.txt | 6 +++++
drivers/base/core.c | 46 ++++++++++++++++++++++++++++++++++-
2 files changed, 51 insertions(+), 1 deletions(-)
diff --git a/Documentation/filesystems/sysfs.txt b/Documentation/filesystems/sysfs.txt
index 7f27b8f..9e9c348 100644
--- a/Documentation/filesystems/sysfs.txt
+++ b/Documentation/filesystems/sysfs.txt
@@ -248,6 +248,7 @@ The top level sysfs directory looks like:
block/
bus/
class/
+dev/
devices/
firmware/
net/
@@ -274,6 +275,11 @@ fs/ contains a directory for some filesystems. Currently each
filesystem wanting to export attributes must create its own hierarchy
below fs/ (see ./fuse.txt for an example).
+dev/ contains two directories char/ and block/. Inside these two
+directories there are symlinks named <major>:<minor>. These symlinks
+point to the sysfs directory for the given device. /sys/dev provides a
+quick way to lookup the sysfs interface for a device from the result of
+a stat(2) operation.
More information can driver-model specific features can be found in
Documentation/driver-model/.
diff --git a/drivers/base/core.c b/drivers/base/core.c
index 24198ad..ba21118 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -27,6 +27,9 @@
int (*platform_notify)(struct device *dev) = NULL;
int (*platform_notify_remove)(struct device *dev) = NULL;
+static struct kobject *dev_kobj;
+static struct kobject *char_kobj;
+static struct kobject *block_kobj;
#ifdef CONFIG_BLOCK
static inline int device_is_not_partition(struct device *dev)
@@ -759,6 +762,11 @@ static void device_remove_class_symlinks(struct device *dev)
sysfs_remove_link(&dev->kobj, "subsystem");
}
+static struct kobject *device_to_dev_kobj(struct device *dev)
+{
+ return dev->class == &block_class ? block_kobj : char_kobj;
+}
+
/**
* device_add - add device to device hierarchy.
* @dev: device.
@@ -775,6 +783,7 @@ int device_add(struct device *dev)
struct device *parent = NULL;
struct class_interface *class_intf;
int error;
+ char devt_str[15];
dev = get_device(dev);
if (!dev || !strlen(dev->bus_id)) {
@@ -806,9 +815,16 @@ int device_add(struct device *dev)
goto attrError;
if (MAJOR(dev->devt)) {
+ struct kobject *kobj = device_to_dev_kobj(dev);
+
error = device_create_file(dev, &devt_attr);
if (error)
goto ueventattrError;
+
+ format_dev_t(devt_str, dev->devt);
+ error = sysfs_create_link(kobj, &dev->kobj, devt_str);
+ if (error)
+ goto devtattrError;
}
error = device_add_class_symlinks(dev);
@@ -854,6 +870,9 @@ int device_add(struct device *dev)
device_remove_class_symlinks(dev);
SymlinkError:
if (MAJOR(dev->devt))
+ sysfs_remove_link(device_to_dev_kobj(dev), devt_str);
+ devtattrError:
+ if (MAJOR(dev->devt))
device_remove_file(dev, &devt_attr);
ueventattrError:
device_remove_file(dev, &uevent_attr);
@@ -925,12 +944,16 @@ void device_del(struct device *dev)
{
struct device *parent = dev->parent;
struct class_interface *class_intf;
+ char devt_str[15];
device_pm_remove(dev);
if (parent)
klist_del(&dev->knode_parent);
- if (MAJOR(dev->devt))
+ if (MAJOR(dev->devt)) {
+ format_dev_t(devt_str, dev->devt);
+ sysfs_remove_link(device_to_dev_kobj(dev), devt_str);
device_remove_file(dev, &devt_attr);
+ }
if (dev->class) {
device_remove_class_symlinks(dev);
@@ -1055,7 +1078,25 @@ int __init devices_init(void)
devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
if (!devices_kset)
return -ENOMEM;
+ dev_kobj = kobject_create_and_add("dev", NULL);
+ if (!dev_kobj)
+ goto dev_kobj_err;
+ block_kobj = kobject_create_and_add("block", dev_kobj);
+ if (!block_kobj)
+ goto block_kobj_err;
+ char_kobj = kobject_create_and_add("char", dev_kobj);
+ if (!char_kobj)
+ goto char_kobj_err;
+
return 0;
+
+ char_kobj_err:
+ kobject_put(block_kobj);
+ block_kobj_err:
+ kobject_put(dev_kobj);
+ dev_kobj_err:
+ kset_unregister(devices_kset);
+ return -ENOMEM;
}
EXPORT_SYMBOL_GPL(device_for_each_child);
@@ -1380,4 +1421,7 @@ void device_shutdown(void)
dev->driver->shutdown(dev);
}
}
+ kobject_put(char_kobj);
+ kobject_put(block_kobj);
+ kobject_put(dev_kobj);
}
^ permalink raw reply related [flat|nested] 4+ messages in thread* patch sysfs-add-sys-dev-char-block-to-lookup-sysfs-path-by-major-minor.patch added to gregkh-2.6 tree 2008-04-15 23:43 What to do about the 2TB limit on HDIO_GETGEO ? Dan Williams @ 2008-04-16 20:55 ` gregkh 0 siblings, 0 replies; 4+ messages in thread From: gregkh @ 2008-04-16 20:55 UTC (permalink / raw) To: dan.j.williams, James.Bottomley, akpm, axboe, gregkh, hpa, htejun, jgarzik, kay.sievers, linux-ide, linux-kernel, linux-scsi, lkml, neilb, steve, torvalds This is a note to let you know that I've just added the patch titled Subject: sysfs: add /sys/dev/{char,block} to lookup sysfs path by major:minor to my gregkh-2.6 tree. Its filename is sysfs-add-sys-dev-char-block-to-lookup-sysfs-path-by-major-minor.patch This tree can be found at http://www.kernel.org/pub/linux/kernel/people/gregkh/gregkh-2.6/patches/ >From dan.j.williams@intel.com Wed Apr 16 13:49:38 2008 From: Dan Williams <dan.j.williams@intel.com> Date: Tue, 15 Apr 2008 16:43:15 -0700 Subject: sysfs: add /sys/dev/{char,block} to lookup sysfs path by major:minor To: Andrew Morton <akpm@linux-foundation.org> Cc: James Bottomley <James.Bottomley@HansenPartnership.com>, Kay Sievers <kay.sievers@vrfy.org>, Tejun Heo <htejun@gmail.com>, Mark Lord <lkml@rtr.ca>, Greg KH <gregkh@suse.de>, Jens Axboe <axboe@kernel.dk>, Jeff Garzik <jgarzik@pobox.com>, Linus Torvalds <torvalds@linux-foundation.org>, Linux Kernel <linux-kernel@vger.kernel.org>, IDE/ATA development list <linux-ide@vger.kernel.org>, linux-scsi <linux-scsi@vger.kernel.org>, "H. Peter Anvin" <hpa@zytor.com> Message-ID: <1208302995.21877.12.camel@dwillia2-linux.ch.intel.com> From: Dan Williams <dan.j.williams@intel.com> Why?: There are occasions where userspace would like to access sysfs attributes for a device but it may not know how sysfs has named the device or the path. For example what is the sysfs path for /dev/disk/by-id/ata-ST3160827AS_5MT004CK? With this change a call to stat(2) returns the major:minor then userspace can see that /sys/dev/block/8:32 links to /sys/block/sdc. What are the alternatives?: 1/ Add an ioctl to return the path: Doable, but sysfs is meant to reduce the need to proliferate ioctl interfaces into the kernel, so this seems counter productive. 2/ Use udev to create these symlinks: Also doable, but it adds a udev dependency to utilities that might be running in a limited environment like an initramfs. 3/ Do a full-tree search of sysfs. Cc: Neil Brown <neilb@suse.de> Cc: Tejun Heo <htejun@gmail.com> Cc: Kay Sievers <kay.sievers@vrfy.org> Cc: Greg KH <gregkh@suse.de> Acked-by: Mark Lord <lkml@rtr.ca> Acked-by: H. Peter Anvin <hpa@zytor.com> Reviewed-by: SL Baur <steve@xemacs.org> Signed-off-by: Dan Williams <dan.j.williams@intel.com> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de> --- Documentation/filesystems/sysfs.txt | 6 ++++ drivers/base/core.c | 46 +++++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) --- a/Documentation/filesystems/sysfs.txt +++ b/Documentation/filesystems/sysfs.txt @@ -248,6 +248,7 @@ The top level sysfs directory looks like block/ bus/ class/ +dev/ devices/ firmware/ net/ @@ -274,6 +275,11 @@ fs/ contains a directory for some filesy filesystem wanting to export attributes must create its own hierarchy below fs/ (see ./fuse.txt for an example). +dev/ contains two directories char/ and block/. Inside these two +directories there are symlinks named <major>:<minor>. These symlinks +point to the sysfs directory for the given device. /sys/dev provides a +quick way to lookup the sysfs interface for a device from the result of +a stat(2) operation. More information can driver-model specific features can be found in Documentation/driver-model/. --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -27,6 +27,9 @@ int (*platform_notify)(struct device *dev) = NULL; int (*platform_notify_remove)(struct device *dev) = NULL; +static struct kobject *dev_kobj; +static struct kobject *char_kobj; +static struct kobject *block_kobj; #ifdef CONFIG_BLOCK static inline int device_is_not_partition(struct device *dev) @@ -760,6 +763,11 @@ static void device_remove_class_symlinks sysfs_remove_link(&dev->kobj, "subsystem"); } +static struct kobject *device_to_dev_kobj(struct device *dev) +{ + return dev->class == &block_class ? block_kobj : char_kobj; +} + /** * device_add - add device to device hierarchy. * @dev: device. @@ -776,6 +784,7 @@ int device_add(struct device *dev) struct device *parent = NULL; struct class_interface *class_intf; int error; + char devt_str[15]; dev = get_device(dev); if (!dev || !strlen(dev->bus_id)) { @@ -807,9 +816,16 @@ int device_add(struct device *dev) goto attrError; if (MAJOR(dev->devt)) { + struct kobject *kobj = device_to_dev_kobj(dev); + error = device_create_file(dev, &devt_attr); if (error) goto ueventattrError; + + format_dev_t(devt_str, dev->devt); + error = sysfs_create_link(kobj, &dev->kobj, devt_str); + if (error) + goto devtattrError; } error = device_add_class_symlinks(dev); @@ -854,6 +870,9 @@ int device_add(struct device *dev) device_remove_class_symlinks(dev); SymlinkError: if (MAJOR(dev->devt)) + sysfs_remove_link(device_to_dev_kobj(dev), devt_str); + devtattrError: + if (MAJOR(dev->devt)) device_remove_file(dev, &devt_attr); ueventattrError: device_remove_file(dev, &uevent_attr); @@ -925,12 +944,16 @@ void device_del(struct device *dev) { struct device *parent = dev->parent; struct class_interface *class_intf; + char devt_str[15]; device_pm_remove(dev); if (parent) klist_del(&dev->knode_parent); - if (MAJOR(dev->devt)) + if (MAJOR(dev->devt)) { + format_dev_t(devt_str, dev->devt); + sysfs_remove_link(device_to_dev_kobj(dev), devt_str); device_remove_file(dev, &devt_attr); + } if (dev->class) { device_remove_class_symlinks(dev); @@ -1055,7 +1078,25 @@ int __init devices_init(void) devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL); if (!devices_kset) return -ENOMEM; + dev_kobj = kobject_create_and_add("dev", NULL); + if (!dev_kobj) + goto dev_kobj_err; + block_kobj = kobject_create_and_add("block", dev_kobj); + if (!block_kobj) + goto block_kobj_err; + char_kobj = kobject_create_and_add("char", dev_kobj); + if (!char_kobj) + goto char_kobj_err; + return 0; + + char_kobj_err: + kobject_put(block_kobj); + block_kobj_err: + kobject_put(dev_kobj); + dev_kobj_err: + kset_unregister(devices_kset); + return -ENOMEM; } EXPORT_SYMBOL_GPL(device_for_each_child); @@ -1351,4 +1392,7 @@ void device_shutdown(void) dev->driver->shutdown(dev); } } + kobject_put(char_kobj); + kobject_put(block_kobj); + kobject_put(dev_kobj); } Patches currently in gregkh-2.6 which might be from dan.j.williams@intel.com are driver-core/sysfs-refill-attribute-buffer-when-reading-from-offset-0.patch driver-core/sysfs-add-sys-dev-char-block-to-lookup-sysfs-path-by-major-minor.patch ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2008-04-29 15:26 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <1208800267.26479.3.camel@dwillia2-linux.ch.intel.com>
2008-04-28 23:51 ` patch sysfs-add-sys-dev-char-block-to-lookup-sysfs-path-by-major-minor.patch added to gregkh-2.6 tree gregkh
2008-04-29 7:48 ` SL Baur
2008-04-29 15:22 ` patch sysfs-add-sys-dev-char-block-to-lookup-sysfs-path-by-major-minor.pat ch " Greg KH
2008-04-15 23:43 What to do about the 2TB limit on HDIO_GETGEO ? Dan Williams
2008-04-16 20:55 ` patch sysfs-add-sys-dev-char-block-to-lookup-sysfs-path-by-major-minor.patch added to gregkh-2.6 tree gregkh
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox