Kay Sievers | 4633600 | 2007-06-08 13:36:37 -0700 | [diff] [blame] | 1 | Rules on how to access information in the Linux kernel sysfs |
| 2 | |
Randy Dunlap | 30b1b28 | 2007-07-23 21:05:02 -0700 | [diff] [blame] | 3 | The kernel-exported sysfs exports internal kernel implementation details |
Kay Sievers | 4633600 | 2007-06-08 13:36:37 -0700 | [diff] [blame] | 4 | and depends on internal kernel structures and layout. It is agreed upon |
| 5 | by the kernel developers that the Linux kernel does not provide a stable |
Nathan Lynch | 83c79b5 | 2008-07-02 10:21:30 -0700 | [diff] [blame] | 6 | internal API. Therefore, there are aspects of the sysfs interface that |
| 7 | may not be stable across kernel releases. |
Kay Sievers | 4633600 | 2007-06-08 13:36:37 -0700 | [diff] [blame] | 8 | |
| 9 | To minimize the risk of breaking users of sysfs, which are in most cases |
| 10 | low-level userspace applications, with a new kernel release, the users |
Randy Dunlap | 30b1b28 | 2007-07-23 21:05:02 -0700 | [diff] [blame] | 11 | of sysfs must follow some rules to use an as-abstract-as-possible way to |
Kay Sievers | 4633600 | 2007-06-08 13:36:37 -0700 | [diff] [blame] | 12 | access this filesystem. The current udev and HAL programs already |
| 13 | implement this and users are encouraged to plug, if possible, into the |
Randy Dunlap | 30b1b28 | 2007-07-23 21:05:02 -0700 | [diff] [blame] | 14 | abstractions these programs provide instead of accessing sysfs directly. |
Kay Sievers | 4633600 | 2007-06-08 13:36:37 -0700 | [diff] [blame] | 15 | |
| 16 | But if you really do want or need to access sysfs directly, please follow |
| 17 | the following rules and then your programs should work with future |
| 18 | versions of the sysfs interface. |
| 19 | |
| 20 | - Do not use libsysfs |
| 21 | It makes assumptions about sysfs which are not true. Its API does not |
| 22 | offer any abstraction, it exposes all the kernel driver-core |
| 23 | implementation details in its own API. Therefore it is not better than |
| 24 | reading directories and opening the files yourself. |
| 25 | Also, it is not actively maintained, in the sense of reflecting the |
Randy Dunlap | 30b1b28 | 2007-07-23 21:05:02 -0700 | [diff] [blame] | 26 | current kernel development. The goal of providing a stable interface |
| 27 | to sysfs has failed; it causes more problems than it solves. It |
Kay Sievers | 4633600 | 2007-06-08 13:36:37 -0700 | [diff] [blame] | 28 | violates many of the rules in this document. |
| 29 | |
| 30 | - sysfs is always at /sys |
| 31 | Parsing /proc/mounts is a waste of time. Other mount points are a |
| 32 | system configuration bug you should not try to solve. For test cases, |
| 33 | possibly support a SYSFS_PATH environment variable to overwrite the |
Randy Dunlap | 30b1b28 | 2007-07-23 21:05:02 -0700 | [diff] [blame] | 34 | application's behavior, but never try to search for sysfs. Never try |
Kay Sievers | 4633600 | 2007-06-08 13:36:37 -0700 | [diff] [blame] | 35 | to mount it, if you are not an early boot script. |
| 36 | |
| 37 | - devices are only "devices" |
| 38 | There is no such thing like class-, bus-, physical devices, |
| 39 | interfaces, and such that you can rely on in userspace. Everything is |
| 40 | just simply a "device". Class-, bus-, physical, ... types are just |
Randy Dunlap | 30b1b28 | 2007-07-23 21:05:02 -0700 | [diff] [blame] | 41 | kernel implementation details which should not be expected by |
Kay Sievers | 4633600 | 2007-06-08 13:36:37 -0700 | [diff] [blame] | 42 | applications that look for devices in sysfs. |
| 43 | |
| 44 | The properties of a device are: |
| 45 | o devpath (/devices/pci0000:00/0000:00:1d.1/usb2/2-2/2-2:1.0) |
| 46 | - identical to the DEVPATH value in the event sent from the kernel |
| 47 | at device creation and removal |
| 48 | - the unique key to the device at that point in time |
Randy Dunlap | 30b1b28 | 2007-07-23 21:05:02 -0700 | [diff] [blame] | 49 | - the kernel's path to the device directory without the leading |
Kay Sievers | 4633600 | 2007-06-08 13:36:37 -0700 | [diff] [blame] | 50 | /sys, and always starting with with a slash |
| 51 | - all elements of a devpath must be real directories. Symlinks |
| 52 | pointing to /sys/devices must always be resolved to their real |
Randy Dunlap | 30b1b28 | 2007-07-23 21:05:02 -0700 | [diff] [blame] | 53 | target and the target path must be used to access the device. |
Kay Sievers | 4633600 | 2007-06-08 13:36:37 -0700 | [diff] [blame] | 54 | That way the devpath to the device matches the devpath of the |
| 55 | kernel used at event time. |
| 56 | - using or exposing symlink values as elements in a devpath string |
| 57 | is a bug in the application |
| 58 | |
| 59 | o kernel name (sda, tty, 0000:00:1f.2, ...) |
| 60 | - a directory name, identical to the last element of the devpath |
| 61 | - applications need to handle spaces and characters like '!' in |
| 62 | the name |
| 63 | |
| 64 | o subsystem (block, tty, pci, ...) |
| 65 | - simple string, never a path or a link |
| 66 | - retrieved by reading the "subsystem"-link and using only the |
| 67 | last element of the target path |
| 68 | |
| 69 | o driver (tg3, ata_piix, uhci_hcd) |
| 70 | - a simple string, which may contain spaces, never a path or a |
| 71 | link |
| 72 | - it is retrieved by reading the "driver"-link and using only the |
| 73 | last element of the target path |
Randy Dunlap | 30b1b28 | 2007-07-23 21:05:02 -0700 | [diff] [blame] | 74 | - devices which do not have "driver"-link just do not have a |
| 75 | driver; copying the driver value in a child device context is a |
Kay Sievers | 4633600 | 2007-06-08 13:36:37 -0700 | [diff] [blame] | 76 | bug in the application |
| 77 | |
| 78 | o attributes |
Randy Dunlap | 30b1b28 | 2007-07-23 21:05:02 -0700 | [diff] [blame] | 79 | - the files in the device directory or files below subdirectories |
Kay Sievers | 4633600 | 2007-06-08 13:36:37 -0700 | [diff] [blame] | 80 | of the same device directory |
| 81 | - accessing attributes reached by a symlink pointing to another device, |
| 82 | like the "device"-link, is a bug in the application |
| 83 | |
Randy Dunlap | 30b1b28 | 2007-07-23 21:05:02 -0700 | [diff] [blame] | 84 | Everything else is just a kernel driver-core implementation detail |
Kay Sievers | 4633600 | 2007-06-08 13:36:37 -0700 | [diff] [blame] | 85 | that should not be assumed to be stable across kernel releases. |
| 86 | |
| 87 | - Properties of parent devices never belong into a child device. |
| 88 | Always look at the parent devices themselves for determining device |
| 89 | context properties. If the device 'eth0' or 'sda' does not have a |
| 90 | "driver"-link, then this device does not have a driver. Its value is empty. |
| 91 | Never copy any property of the parent-device into a child-device. Parent |
Randy Dunlap | 30b1b28 | 2007-07-23 21:05:02 -0700 | [diff] [blame] | 92 | device properties may change dynamically without any notice to the |
Kay Sievers | 4633600 | 2007-06-08 13:36:37 -0700 | [diff] [blame] | 93 | child device. |
| 94 | |
Randy Dunlap | 30b1b28 | 2007-07-23 21:05:02 -0700 | [diff] [blame] | 95 | - Hierarchy in a single device tree |
Kay Sievers | 4633600 | 2007-06-08 13:36:37 -0700 | [diff] [blame] | 96 | There is only one valid place in sysfs where hierarchy can be examined |
| 97 | and this is below: /sys/devices. |
Randy Dunlap | 30b1b28 | 2007-07-23 21:05:02 -0700 | [diff] [blame] | 98 | It is planned that all device directories will end up in the tree |
Kay Sievers | 4633600 | 2007-06-08 13:36:37 -0700 | [diff] [blame] | 99 | below this directory. |
| 100 | |
| 101 | - Classification by subsystem |
| 102 | There are currently three places for classification of devices: |
| 103 | /sys/block, /sys/class and /sys/bus. It is planned that these will |
Randy Dunlap | 30b1b28 | 2007-07-23 21:05:02 -0700 | [diff] [blame] | 104 | not contain any device directories themselves, but only flat lists of |
Kay Sievers | 4633600 | 2007-06-08 13:36:37 -0700 | [diff] [blame] | 105 | symlinks pointing to the unified /sys/devices tree. |
| 106 | All three places have completely different rules on how to access |
| 107 | device information. It is planned to merge all three |
Randy Dunlap | 30b1b28 | 2007-07-23 21:05:02 -0700 | [diff] [blame] | 108 | classification directories into one place at /sys/subsystem, |
| 109 | following the layout of the bus directories. All buses and |
| 110 | classes, including the converted block subsystem, will show up |
Kay Sievers | 4633600 | 2007-06-08 13:36:37 -0700 | [diff] [blame] | 111 | there. |
| 112 | The devices belonging to a subsystem will create a symlink in the |
| 113 | "devices" directory at /sys/subsystem/<name>/devices. |
| 114 | |
| 115 | If /sys/subsystem exists, /sys/bus, /sys/class and /sys/block can be |
| 116 | ignored. If it does not exist, you have always to scan all three |
| 117 | places, as the kernel is free to move a subsystem from one place to |
| 118 | the other, as long as the devices are still reachable by the same |
| 119 | subsystem name. |
| 120 | |
| 121 | Assuming /sys/class/<subsystem> and /sys/bus/<subsystem>, or |
Randy Dunlap | 30b1b28 | 2007-07-23 21:05:02 -0700 | [diff] [blame] | 122 | /sys/block and /sys/class/block are not interchangeable is a bug in |
Kay Sievers | 4633600 | 2007-06-08 13:36:37 -0700 | [diff] [blame] | 123 | the application. |
| 124 | |
| 125 | - Block |
Randy Dunlap | 30b1b28 | 2007-07-23 21:05:02 -0700 | [diff] [blame] | 126 | The converted block subsystem at /sys/class/block or |
Kay Sievers | 4633600 | 2007-06-08 13:36:37 -0700 | [diff] [blame] | 127 | /sys/subsystem/block will contain the links for disks and partitions |
Randy Dunlap | 30b1b28 | 2007-07-23 21:05:02 -0700 | [diff] [blame] | 128 | at the same level, never in a hierarchy. Assuming the block subsytem to |
| 129 | contain only disks and not partition devices in the same flat list is |
Kay Sievers | 4633600 | 2007-06-08 13:36:37 -0700 | [diff] [blame] | 130 | a bug in the application. |
| 131 | |
| 132 | - "device"-link and <subsystem>:<kernel name>-links |
| 133 | Never depend on the "device"-link. The "device"-link is a workaround |
Randy Dunlap | 30b1b28 | 2007-07-23 21:05:02 -0700 | [diff] [blame] | 134 | for the old layout, where class devices are not created in |
| 135 | /sys/devices/ like the bus devices. If the link-resolving of a |
| 136 | device directory does not end in /sys/devices/, you can use the |
Kay Sievers | 4633600 | 2007-06-08 13:36:37 -0700 | [diff] [blame] | 137 | "device"-link to find the parent devices in /sys/devices/. That is the |
Randy Dunlap | 30b1b28 | 2007-07-23 21:05:02 -0700 | [diff] [blame] | 138 | single valid use of the "device"-link; it must never appear in any |
Kay Sievers | 4633600 | 2007-06-08 13:36:37 -0700 | [diff] [blame] | 139 | path as an element. Assuming the existence of the "device"-link for |
| 140 | a device in /sys/devices/ is a bug in the application. |
| 141 | Accessing /sys/class/net/eth0/device is a bug in the application. |
| 142 | |
| 143 | Never depend on the class-specific links back to the /sys/class |
| 144 | directory. These links are also a workaround for the design mistake |
Randy Dunlap | 30b1b28 | 2007-07-23 21:05:02 -0700 | [diff] [blame] | 145 | that class devices are not created in /sys/devices. If a device |
Kay Sievers | 4633600 | 2007-06-08 13:36:37 -0700 | [diff] [blame] | 146 | directory does not contain directories for child devices, these links |
| 147 | may be used to find the child devices in /sys/class. That is the single |
Randy Dunlap | 30b1b28 | 2007-07-23 21:05:02 -0700 | [diff] [blame] | 148 | valid use of these links; they must never appear in any path as an |
Kay Sievers | 4633600 | 2007-06-08 13:36:37 -0700 | [diff] [blame] | 149 | element. Assuming the existence of these links for devices which are |
Randy Dunlap | 30b1b28 | 2007-07-23 21:05:02 -0700 | [diff] [blame] | 150 | real child device directories in the /sys/devices tree is a bug in |
Kay Sievers | 4633600 | 2007-06-08 13:36:37 -0700 | [diff] [blame] | 151 | the application. |
| 152 | |
Randy Dunlap | 30b1b28 | 2007-07-23 21:05:02 -0700 | [diff] [blame] | 153 | It is planned to remove all these links when all class device |
Kay Sievers | 4633600 | 2007-06-08 13:36:37 -0700 | [diff] [blame] | 154 | directories live in /sys/devices. |
| 155 | |
| 156 | - Position of devices along device chain can change. |
| 157 | Never depend on a specific parent device position in the devpath, |
| 158 | or the chain of parent devices. The kernel is free to insert devices into |
| 159 | the chain. You must always request the parent device you are looking for |
| 160 | by its subsystem value. You need to walk up the chain until you find |
| 161 | the device that matches the expected subsystem. Depending on a specific |
Randy Dunlap | 30b1b28 | 2007-07-23 21:05:02 -0700 | [diff] [blame] | 162 | position of a parent device or exposing relative paths using "../" to |
| 163 | access the chain of parents is a bug in the application. |