Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

docs: filesystems: convert sysfs.txt to ReST

- Add a SPDX header;
- Add a document title;
- Adjust document and section titles;
- use :field: markup;
- Some whitespace fixes and new line breaks;
- Mark literal blocks as such;
- Add it to filesystems/index.rst.

Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
Link: https://lore.kernel.org/r/5c480dcb467315b5df6e25372a65e473b585c36d.1581955849.git.mchehab+huawei@kernel.org
Signed-off-by: Jonathan Corbet <corbet@lwn.net>

authored by

Mauro Carvalho Chehab and committed by
Jonathan Corbet
86beb976 31771f45

+175 -164
+1
Documentation/filesystems/index.rst
··· 87 87 relay 88 88 romfs 89 89 squashfs 90 + sysfs 90 91 virtiofs 91 92 vfat
+174 -164
Documentation/filesystems/sysfs.txt Documentation/filesystems/sysfs.rst
··· 1 + .. SPDX-License-Identifier: GPL-2.0 1 2 2 - sysfs - _The_ filesystem for exporting kernel objects. 3 + ===================================================== 4 + sysfs - _The_ filesystem for exporting kernel objects 5 + ===================================================== 3 6 4 7 Patrick Mochel <mochel@osdl.org> 8 + 5 9 Mike Murphy <mamurph@cs.clemson.edu> 6 10 7 - Revised: 16 August 2011 8 - Original: 10 January 2003 11 + :Revised: 16 August 2011 12 + :Original: 10 January 2003 9 13 10 14 11 15 What it is: 12 16 ~~~~~~~~~~~ 13 17 14 18 sysfs is a ram-based filesystem initially based on ramfs. It provides 15 - a means to export kernel data structures, their attributes, and the 16 - linkages between them to userspace. 19 + a means to export kernel data structures, their attributes, and the 20 + linkages between them to userspace. 17 21 18 22 sysfs is tied inherently to the kobject infrastructure. Please read 19 23 Documentation/kobject.txt for more information concerning the kobject 20 - interface. 24 + interface. 21 25 22 26 23 27 Using sysfs 24 28 ~~~~~~~~~~~ 25 29 26 30 sysfs is always compiled in if CONFIG_SYSFS is defined. You can access 27 - it by doing: 31 + it by doing:: 28 32 29 - mount -t sysfs sysfs /sys 33 + mount -t sysfs sysfs /sys 30 34 31 35 32 36 Directory Creation ··· 41 37 of the kobject's parent, expressing internal object hierarchies to 42 38 userspace. Top-level directories in sysfs represent the common 43 39 ancestors of object hierarchies; i.e. the subsystems the objects 44 - belong to. 40 + belong to. 45 41 46 42 Sysfs internally stores a pointer to the kobject that implements a 47 43 directory in the kernfs_node object associated with the directory. In ··· 62 58 Attributes should be ASCII text files, preferably with only one value 63 59 per file. It is noted that it may not be efficient to contain only one 64 60 value per file, so it is socially acceptable to express an array of 65 - values of the same type. 61 + values of the same type. 66 62 67 63 Mixing types, expressing multiple lines of data, and doing fancy 68 64 formatting of data is heavily frowned upon. Doing these things may get 69 - you publicly humiliated and your code rewritten without notice. 65 + you publicly humiliated and your code rewritten without notice. 70 66 71 67 72 - An attribute definition is simply: 68 + An attribute definition is simply:: 73 69 74 - struct attribute { 75 - char * name; 76 - struct module *owner; 77 - umode_t mode; 78 - }; 70 + struct attribute { 71 + char * name; 72 + struct module *owner; 73 + umode_t mode; 74 + }; 79 75 80 76 81 - int sysfs_create_file(struct kobject * kobj, const struct attribute * attr); 82 - void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr); 77 + int sysfs_create_file(struct kobject * kobj, const struct attribute * attr); 78 + void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr); 83 79 84 80 85 81 A bare attribute contains no means to read or write the value of the 86 82 attribute. Subsystems are encouraged to define their own attribute 87 83 structure and wrapper functions for adding and removing attributes for 88 - a specific object type. 84 + a specific object type. 89 85 90 - For example, the driver model defines struct device_attribute like: 86 + For example, the driver model defines struct device_attribute like:: 91 87 92 - struct device_attribute { 93 - struct attribute attr; 94 - ssize_t (*show)(struct device *dev, struct device_attribute *attr, 95 - char *buf); 96 - ssize_t (*store)(struct device *dev, struct device_attribute *attr, 97 - const char *buf, size_t count); 98 - }; 88 + struct device_attribute { 89 + struct attribute attr; 90 + ssize_t (*show)(struct device *dev, struct device_attribute *attr, 91 + char *buf); 92 + ssize_t (*store)(struct device *dev, struct device_attribute *attr, 93 + const char *buf, size_t count); 94 + }; 99 95 100 - int device_create_file(struct device *, const struct device_attribute *); 101 - void device_remove_file(struct device *, const struct device_attribute *); 96 + int device_create_file(struct device *, const struct device_attribute *); 97 + void device_remove_file(struct device *, const struct device_attribute *); 102 98 103 - It also defines this helper for defining device attributes: 99 + It also defines this helper for defining device attributes:: 104 100 105 - #define DEVICE_ATTR(_name, _mode, _show, _store) \ 106 - struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store) 101 + #define DEVICE_ATTR(_name, _mode, _show, _store) \ 102 + struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store) 107 103 108 - For example, declaring 104 + For example, declaring:: 109 105 110 - static DEVICE_ATTR(foo, S_IWUSR | S_IRUGO, show_foo, store_foo); 106 + static DEVICE_ATTR(foo, S_IWUSR | S_IRUGO, show_foo, store_foo); 111 107 112 - is equivalent to doing: 108 + is equivalent to doing:: 113 109 114 - static struct device_attribute dev_attr_foo = { 115 - .attr = { 116 - .name = "foo", 117 - .mode = S_IWUSR | S_IRUGO, 118 - }, 119 - .show = show_foo, 120 - .store = store_foo, 121 - }; 110 + static struct device_attribute dev_attr_foo = { 111 + .attr = { 112 + .name = "foo", 113 + .mode = S_IWUSR | S_IRUGO, 114 + }, 115 + .show = show_foo, 116 + .store = store_foo, 117 + }; 122 118 123 119 Note as stated in include/linux/kernel.h "OTHER_WRITABLE? Generally 124 120 considered a bad idea." so trying to set a sysfs file writable for ··· 131 127 static struct device_attribute dev_attr_foo = __ATTR_RW(foo); 132 128 133 129 the list of helpers available to define your wrapper function is: 134 - __ATTR_RO(name): assumes default name_show and mode 0444 135 - __ATTR_WO(name): assumes a name_store only and is restricted to mode 130 + 131 + __ATTR_RO(name): 132 + assumes default name_show and mode 0444 133 + __ATTR_WO(name): 134 + assumes a name_store only and is restricted to mode 136 135 0200 that is root write access only. 137 - __ATTR_RO_MODE(name, mode): fore more restrictive RO access currently 136 + __ATTR_RO_MODE(name, mode): 137 + fore more restrictive RO access currently 138 138 only use case is the EFI System Resource Table 139 139 (see drivers/firmware/efi/esrt.c) 140 - __ATTR_RW(name): assumes default name_show, name_store and setting 140 + __ATTR_RW(name): 141 + assumes default name_show, name_store and setting 141 142 mode to 0644. 142 - __ATTR_NULL: which sets the name to NULL and is used as end of list 143 + __ATTR_NULL: 144 + which sets the name to NULL and is used as end of list 143 145 indicator (see: kernel/workqueue.c) 144 146 145 147 Subsystem-Specific Callbacks ··· 153 143 154 144 When a subsystem defines a new attribute type, it must implement a 155 145 set of sysfs operations for forwarding read and write calls to the 156 - show and store methods of the attribute owners. 146 + show and store methods of the attribute owners:: 157 147 158 - struct sysfs_ops { 159 - ssize_t (*show)(struct kobject *, struct attribute *, char *); 160 - ssize_t (*store)(struct kobject *, struct attribute *, const char *, size_t); 161 - }; 148 + struct sysfs_ops { 149 + ssize_t (*show)(struct kobject *, struct attribute *, char *); 150 + ssize_t (*store)(struct kobject *, struct attribute *, const char *, size_t); 151 + }; 162 152 163 153 [ Subsystems should have already defined a struct kobj_type as a 164 154 descriptor for this type, which is where the sysfs_ops pointer is ··· 167 157 When a file is read or written, sysfs calls the appropriate method 168 158 for the type. The method then translates the generic struct kobject 169 159 and struct attribute pointers to the appropriate pointer types, and 170 - calls the associated methods. 160 + calls the associated methods. 171 161 172 162 173 - To illustrate: 163 + To illustrate:: 174 164 175 - #define to_dev(obj) container_of(obj, struct device, kobj) 176 - #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) 165 + #define to_dev(obj) container_of(obj, struct device, kobj) 166 + #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr) 177 167 178 - static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr, 179 - char *buf) 180 - { 181 - struct device_attribute *dev_attr = to_dev_attr(attr); 182 - struct device *dev = to_dev(kobj); 183 - ssize_t ret = -EIO; 168 + static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr, 169 + char *buf) 170 + { 171 + struct device_attribute *dev_attr = to_dev_attr(attr); 172 + struct device *dev = to_dev(kobj); 173 + ssize_t ret = -EIO; 184 174 185 - if (dev_attr->show) 186 - ret = dev_attr->show(dev, dev_attr, buf); 187 - if (ret >= (ssize_t)PAGE_SIZE) { 188 - printk("dev_attr_show: %pS returned bad count\n", 189 - dev_attr->show); 190 - } 191 - return ret; 192 - } 175 + if (dev_attr->show) 176 + ret = dev_attr->show(dev, dev_attr, buf); 177 + if (ret >= (ssize_t)PAGE_SIZE) { 178 + printk("dev_attr_show: %pS returned bad count\n", 179 + dev_attr->show); 180 + } 181 + return ret; 182 + } 193 183 194 184 195 185 ··· 198 188 199 189 To read or write attributes, show() or store() methods must be 200 190 specified when declaring the attribute. The method types should be as 201 - simple as those defined for device attributes: 191 + simple as those defined for device attributes:: 202 192 203 - ssize_t (*show)(struct device *dev, struct device_attribute *attr, char *buf); 204 - ssize_t (*store)(struct device *dev, struct device_attribute *attr, 205 - const char *buf, size_t count); 193 + ssize_t (*show)(struct device *dev, struct device_attribute *attr, char *buf); 194 + ssize_t (*store)(struct device *dev, struct device_attribute *attr, 195 + const char *buf, size_t count); 206 196 207 197 IOW, they should take only an object, an attribute, and a buffer as parameters. 208 198 ··· 210 200 sysfs allocates a buffer of size (PAGE_SIZE) and passes it to the 211 201 method. Sysfs will call the method exactly once for each read or 212 202 write. This forces the following behavior on the method 213 - implementations: 203 + implementations: 214 204 215 - - On read(2), the show() method should fill the entire buffer. 205 + - On read(2), the show() method should fill the entire buffer. 216 206 Recall that an attribute should only be exporting one value, or an 217 - array of similar values, so this shouldn't be that expensive. 207 + array of similar values, so this shouldn't be that expensive. 218 208 219 209 This allows userspace to do partial reads and forward seeks 220 210 arbitrarily over the entire file at will. If userspace seeks back to ··· 228 218 229 219 When writing sysfs files, userspace processes should first read the 230 220 entire file, modify the values it wishes to change, then write the 231 - entire buffer back. 221 + entire buffer back. 232 222 233 223 Attribute method implementations should operate on an identical 234 - buffer when reading and writing values. 224 + buffer when reading and writing values. 235 225 236 226 Other notes: 237 227 ··· 239 229 file position. 240 230 241 231 - The buffer will always be PAGE_SIZE bytes in length. On i386, this 242 - is 4096. 232 + is 4096. 243 233 244 234 - show() methods should return the number of bytes printed into the 245 235 buffer. This is the return value of scnprintf(). ··· 256 246 through, be sure to return an error. 257 247 258 248 - The object passed to the methods will be pinned in memory via sysfs 259 - referencing counting its embedded object. However, the physical 260 - entity (e.g. device) the object represents may not be present. Be 261 - sure to have a way to check this, if necessary. 249 + referencing counting its embedded object. However, the physical 250 + entity (e.g. device) the object represents may not be present. Be 251 + sure to have a way to check this, if necessary. 262 252 263 253 264 - A very simple (and naive) implementation of a device attribute is: 254 + A very simple (and naive) implementation of a device attribute is:: 265 255 266 - static ssize_t show_name(struct device *dev, struct device_attribute *attr, 267 - char *buf) 268 - { 269 - return scnprintf(buf, PAGE_SIZE, "%s\n", dev->name); 270 - } 256 + static ssize_t show_name(struct device *dev, struct device_attribute *attr, 257 + char *buf) 258 + { 259 + return scnprintf(buf, PAGE_SIZE, "%s\n", dev->name); 260 + } 271 261 272 - static ssize_t store_name(struct device *dev, struct device_attribute *attr, 273 - const char *buf, size_t count) 274 - { 275 - snprintf(dev->name, sizeof(dev->name), "%.*s", 276 - (int)min(count, sizeof(dev->name) - 1), buf); 277 - return count; 278 - } 262 + static ssize_t store_name(struct device *dev, struct device_attribute *attr, 263 + const char *buf, size_t count) 264 + { 265 + snprintf(dev->name, sizeof(dev->name), "%.*s", 266 + (int)min(count, sizeof(dev->name) - 1), buf); 267 + return count; 268 + } 279 269 280 - static DEVICE_ATTR(name, S_IRUGO, show_name, store_name); 270 + static DEVICE_ATTR(name, S_IRUGO, show_name, store_name); 281 271 282 272 283 - (Note that the real implementation doesn't allow userspace to set the 273 + (Note that the real implementation doesn't allow userspace to set the 284 274 name for a device.) 285 275 286 276 ··· 288 278 ~~~~~~~~~~~~~~~~~~~~~~~~~~ 289 279 290 280 The sysfs directory arrangement exposes the relationship of kernel 291 - data structures. 281 + data structures. 292 282 293 - The top level sysfs directory looks like: 283 + The top level sysfs directory looks like:: 294 284 295 - block/ 296 - bus/ 297 - class/ 298 - dev/ 299 - devices/ 300 - firmware/ 301 - net/ 302 - fs/ 285 + block/ 286 + bus/ 287 + class/ 288 + dev/ 289 + devices/ 290 + firmware/ 291 + net/ 292 + fs/ 303 293 304 294 devices/ contains a filesystem representation of the device tree. It maps 305 295 directly to the internal kernel device tree, which is a hierarchy of 306 - struct device. 296 + struct device. 307 297 308 298 bus/ contains flat directory layout of the various bus types in the 309 - kernel. Each bus's directory contains two subdirectories: 299 + kernel. Each bus's directory contains two subdirectories:: 310 300 311 301 devices/ 312 302 drivers/ ··· 341 331 The following interface layers currently exist in sysfs: 342 332 343 333 344 - - devices (include/linux/device.h) 345 - ---------------------------------- 346 - Structure: 334 + devices (include/linux/device.h) 335 + -------------------------------- 336 + Structure:: 347 337 348 - struct device_attribute { 349 - struct attribute attr; 350 - ssize_t (*show)(struct device *dev, struct device_attribute *attr, 351 - char *buf); 352 - ssize_t (*store)(struct device *dev, struct device_attribute *attr, 353 - const char *buf, size_t count); 354 - }; 338 + struct device_attribute { 339 + struct attribute attr; 340 + ssize_t (*show)(struct device *dev, struct device_attribute *attr, 341 + char *buf); 342 + ssize_t (*store)(struct device *dev, struct device_attribute *attr, 343 + const char *buf, size_t count); 344 + }; 355 345 356 - Declaring: 346 + Declaring:: 357 347 358 - DEVICE_ATTR(_name, _mode, _show, _store); 348 + DEVICE_ATTR(_name, _mode, _show, _store); 359 349 360 - Creation/Removal: 350 + Creation/Removal:: 361 351 362 - int device_create_file(struct device *dev, const struct device_attribute * attr); 363 - void device_remove_file(struct device *dev, const struct device_attribute * attr); 364 - 365 - 366 - - bus drivers (include/linux/device.h) 367 - -------------------------------------- 368 - Structure: 369 - 370 - struct bus_attribute { 371 - struct attribute attr; 372 - ssize_t (*show)(struct bus_type *, char * buf); 373 - ssize_t (*store)(struct bus_type *, const char * buf, size_t count); 374 - }; 375 - 376 - Declaring: 377 - 378 - static BUS_ATTR_RW(name); 379 - static BUS_ATTR_RO(name); 380 - static BUS_ATTR_WO(name); 381 - 382 - Creation/Removal: 383 - 384 - int bus_create_file(struct bus_type *, struct bus_attribute *); 385 - void bus_remove_file(struct bus_type *, struct bus_attribute *); 352 + int device_create_file(struct device *dev, const struct device_attribute * attr); 353 + void device_remove_file(struct device *dev, const struct device_attribute * attr); 386 354 387 355 388 - - device drivers (include/linux/device.h) 389 - ----------------------------------------- 356 + bus drivers (include/linux/device.h) 357 + ------------------------------------ 358 + Structure:: 390 359 391 - Structure: 360 + struct bus_attribute { 361 + struct attribute attr; 362 + ssize_t (*show)(struct bus_type *, char * buf); 363 + ssize_t (*store)(struct bus_type *, const char * buf, size_t count); 364 + }; 392 365 393 - struct driver_attribute { 394 - struct attribute attr; 395 - ssize_t (*show)(struct device_driver *, char * buf); 396 - ssize_t (*store)(struct device_driver *, const char * buf, 397 - size_t count); 398 - }; 366 + Declaring:: 399 367 400 - Declaring: 368 + static BUS_ATTR_RW(name); 369 + static BUS_ATTR_RO(name); 370 + static BUS_ATTR_WO(name); 401 371 402 - DRIVER_ATTR_RO(_name) 403 - DRIVER_ATTR_RW(_name) 372 + Creation/Removal:: 404 373 405 - Creation/Removal: 374 + int bus_create_file(struct bus_type *, struct bus_attribute *); 375 + void bus_remove_file(struct bus_type *, struct bus_attribute *); 406 376 407 - int driver_create_file(struct device_driver *, const struct driver_attribute *); 408 - void driver_remove_file(struct device_driver *, const struct driver_attribute *); 377 + 378 + device drivers (include/linux/device.h) 379 + --------------------------------------- 380 + 381 + Structure:: 382 + 383 + struct driver_attribute { 384 + struct attribute attr; 385 + ssize_t (*show)(struct device_driver *, char * buf); 386 + ssize_t (*store)(struct device_driver *, const char * buf, 387 + size_t count); 388 + }; 389 + 390 + Declaring:: 391 + 392 + DRIVER_ATTR_RO(_name) 393 + DRIVER_ATTR_RW(_name) 394 + 395 + Creation/Removal:: 396 + 397 + int driver_create_file(struct device_driver *, const struct driver_attribute *); 398 + void driver_remove_file(struct device_driver *, const struct driver_attribute *); 409 399 410 400 411 401 Documentation