2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
sysfs - _The_ filesystem for exporting kernel objects.
|
|
|
|
|
|
|
|
Patrick Mochel <mochel@osdl.org>
|
2009-02-22 14:19:23 +08:00
|
|
|
Mike Murphy <mamurph@cs.clemson.edu>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-08-17 00:42:22 +08:00
|
|
|
Revised: 16 August 2011
|
2009-02-22 14:19:23 +08:00
|
|
|
Original: 10 January 2003
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
What it is:
|
|
|
|
~~~~~~~~~~~
|
|
|
|
|
|
|
|
sysfs is a ram-based filesystem initially based on ramfs. It provides
|
|
|
|
a means to export kernel data structures, their attributes, and the
|
|
|
|
linkages between them to userspace.
|
|
|
|
|
|
|
|
sysfs is tied inherently to the kobject infrastructure. Please read
|
|
|
|
Documentation/kobject.txt for more information concerning the kobject
|
|
|
|
interface.
|
|
|
|
|
|
|
|
|
|
|
|
Using sysfs
|
|
|
|
~~~~~~~~~~~
|
|
|
|
|
2009-07-28 00:06:42 +08:00
|
|
|
sysfs is always compiled in if CONFIG_SYSFS is defined. You can access
|
|
|
|
it by doing:
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
mount -t sysfs sysfs /sys
|
|
|
|
|
|
|
|
|
|
|
|
Directory Creation
|
|
|
|
~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
For every kobject that is registered with the system, a directory is
|
|
|
|
created for it in sysfs. That directory is created as a subdirectory
|
|
|
|
of the kobject's parent, expressing internal object hierarchies to
|
|
|
|
userspace. Top-level directories in sysfs represent the common
|
|
|
|
ancestors of object hierarchies; i.e. the subsystems the objects
|
|
|
|
belong to.
|
|
|
|
|
2010-12-21 20:09:23 +08:00
|
|
|
Sysfs internally stores a pointer to the kobject that implements a
|
2015-09-02 20:34:52 +08:00
|
|
|
directory in the kernfs_node object associated with the directory. In
|
2010-12-21 20:09:23 +08:00
|
|
|
the past this kobject pointer has been used by sysfs to do reference
|
|
|
|
counting directly on the kobject whenever the file is opened or closed.
|
|
|
|
With the current sysfs implementation the kobject reference count is
|
|
|
|
only modified directly by the function sysfs_schedule_callback().
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
Attributes
|
|
|
|
~~~~~~~~~~
|
|
|
|
|
|
|
|
Attributes can be exported for kobjects in the form of regular files in
|
|
|
|
the filesystem. Sysfs forwards file I/O operations to methods defined
|
|
|
|
for the attributes, providing a means to read and write kernel
|
|
|
|
attributes.
|
|
|
|
|
|
|
|
Attributes should be ASCII text files, preferably with only one value
|
2007-10-20 08:39:43 +08:00
|
|
|
per file. It is noted that it may not be efficient to contain only one
|
2005-04-17 06:20:36 +08:00
|
|
|
value per file, so it is socially acceptable to express an array of
|
|
|
|
values of the same type.
|
|
|
|
|
|
|
|
Mixing types, expressing multiple lines of data, and doing fancy
|
|
|
|
formatting of data is heavily frowned upon. Doing these things may get
|
2011-03-31 09:57:33 +08:00
|
|
|
you publicly humiliated and your code rewritten without notice.
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
An attribute definition is simply:
|
|
|
|
|
|
|
|
struct attribute {
|
|
|
|
char * name;
|
2009-02-22 14:19:23 +08:00
|
|
|
struct module *owner;
|
2011-07-25 11:44:53 +08:00
|
|
|
umode_t mode;
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-02-22 14:19:23 +08:00
|
|
|
int sysfs_create_file(struct kobject * kobj, const struct attribute * attr);
|
|
|
|
void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
A bare attribute contains no means to read or write the value of the
|
|
|
|
attribute. Subsystems are encouraged to define their own attribute
|
|
|
|
structure and wrapper functions for adding and removing attributes for
|
|
|
|
a specific object type.
|
|
|
|
|
|
|
|
For example, the driver model defines struct device_attribute like:
|
|
|
|
|
|
|
|
struct device_attribute {
|
2009-02-22 14:19:23 +08:00
|
|
|
struct attribute attr;
|
|
|
|
ssize_t (*show)(struct device *dev, struct device_attribute *attr,
|
|
|
|
char *buf);
|
|
|
|
ssize_t (*store)(struct device *dev, struct device_attribute *attr,
|
|
|
|
const char *buf, size_t count);
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
2009-12-18 21:34:19 +08:00
|
|
|
int device_create_file(struct device *, const struct device_attribute *);
|
|
|
|
void device_remove_file(struct device *, const struct device_attribute *);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
It also defines this helper for defining device attributes:
|
|
|
|
|
2009-02-22 14:19:23 +08:00
|
|
|
#define DEVICE_ATTR(_name, _mode, _show, _store) \
|
|
|
|
struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
For example, declaring
|
|
|
|
|
2005-07-31 19:12:10 +08:00
|
|
|
static DEVICE_ATTR(foo, S_IWUSR | S_IRUGO, show_foo, store_foo);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
is equivalent to doing:
|
|
|
|
|
|
|
|
static struct device_attribute dev_attr_foo = {
|
2014-01-24 07:55:26 +08:00
|
|
|
.attr = {
|
2005-04-17 06:20:36 +08:00
|
|
|
.name = "foo",
|
2005-07-31 19:12:10 +08:00
|
|
|
.mode = S_IWUSR | S_IRUGO,
|
2005-04-17 06:20:36 +08:00
|
|
|
},
|
2014-01-24 07:55:26 +08:00
|
|
|
.show = show_foo,
|
|
|
|
.store = store_foo,
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Subsystem-Specific Callbacks
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
When a subsystem defines a new attribute type, it must implement a
|
|
|
|
set of sysfs operations for forwarding read and write calls to the
|
|
|
|
show and store methods of the attribute owners.
|
|
|
|
|
|
|
|
struct sysfs_ops {
|
2005-07-31 19:12:09 +08:00
|
|
|
ssize_t (*show)(struct kobject *, struct attribute *, char *);
|
2010-07-21 06:22:05 +08:00
|
|
|
ssize_t (*store)(struct kobject *, struct attribute *, const char *, size_t);
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
[ Subsystems should have already defined a struct kobj_type as a
|
|
|
|
descriptor for this type, which is where the sysfs_ops pointer is
|
|
|
|
stored. See the kobject documentation for more information. ]
|
|
|
|
|
|
|
|
When a file is read or written, sysfs calls the appropriate method
|
|
|
|
for the type. The method then translates the generic struct kobject
|
|
|
|
and struct attribute pointers to the appropriate pointer types, and
|
|
|
|
calls the associated methods.
|
|
|
|
|
|
|
|
|
|
|
|
To illustrate:
|
|
|
|
|
2010-07-21 06:22:05 +08:00
|
|
|
#define to_dev(obj) container_of(obj, struct device, kobj)
|
2005-07-31 19:12:09 +08:00
|
|
|
#define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-07-21 06:22:05 +08:00
|
|
|
static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
|
|
|
|
char *buf)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2010-07-21 06:22:05 +08:00
|
|
|
struct device_attribute *dev_attr = to_dev_attr(attr);
|
|
|
|
struct device *dev = to_dev(kobj);
|
|
|
|
ssize_t ret = -EIO;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (dev_attr->show)
|
2010-07-21 06:22:05 +08:00
|
|
|
ret = dev_attr->show(dev, dev_attr, buf);
|
|
|
|
if (ret >= (ssize_t)PAGE_SIZE) {
|
2018-01-05 18:25:38 +08:00
|
|
|
printk("dev_attr_show: %pS returned bad count\n",
|
|
|
|
dev_attr->show);
|
2010-07-21 06:22:05 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Reading/Writing Attribute Data
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
To read or write attributes, show() or store() methods must be
|
|
|
|
specified when declaring the attribute. The method types should be as
|
|
|
|
simple as those defined for device attributes:
|
|
|
|
|
2010-07-21 06:22:05 +08:00
|
|
|
ssize_t (*show)(struct device *dev, struct device_attribute *attr, char *buf);
|
|
|
|
ssize_t (*store)(struct device *dev, struct device_attribute *attr,
|
|
|
|
const char *buf, size_t count);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2009-02-22 14:19:23 +08:00
|
|
|
IOW, they should take only an object, an attribute, and a buffer as parameters.
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
sysfs allocates a buffer of size (PAGE_SIZE) and passes it to the
|
|
|
|
method. Sysfs will call the method exactly once for each read or
|
|
|
|
write. This forces the following behavior on the method
|
|
|
|
implementations:
|
|
|
|
|
|
|
|
- On read(2), the show() method should fill the entire buffer.
|
|
|
|
Recall that an attribute should only be exporting one value, or an
|
|
|
|
array of similar values, so this shouldn't be that expensive.
|
|
|
|
|
2008-04-08 06:35:01 +08:00
|
|
|
This allows userspace to do partial reads and forward seeks
|
|
|
|
arbitrarily over the entire file at will. If userspace seeks back to
|
|
|
|
zero or does a pread(2) with an offset of '0' the show() method will
|
|
|
|
be called again, rearmed, to fill the buffer.
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
- On write(2), sysfs expects the entire buffer to be passed during the
|
2015-09-08 01:06:14 +08:00
|
|
|
first write. Sysfs then passes the entire buffer to the store() method.
|
|
|
|
A terminating null is added after the data on stores. This makes
|
|
|
|
functions like sysfs_streq() safe to use.
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
When writing sysfs files, userspace processes should first read the
|
|
|
|
entire file, modify the values it wishes to change, then write the
|
|
|
|
entire buffer back.
|
|
|
|
|
|
|
|
Attribute method implementations should operate on an identical
|
|
|
|
buffer when reading and writing values.
|
|
|
|
|
|
|
|
Other notes:
|
|
|
|
|
2008-04-08 06:35:01 +08:00
|
|
|
- Writing causes the show() method to be rearmed regardless of current
|
|
|
|
file position.
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
- The buffer will always be PAGE_SIZE bytes in length. On i386, this
|
|
|
|
is 4096.
|
|
|
|
|
|
|
|
- show() methods should return the number of bytes printed into the
|
2010-12-21 20:09:47 +08:00
|
|
|
buffer. This is the return value of scnprintf().
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2015-06-25 10:33:08 +08:00
|
|
|
- show() must not use snprintf() when formatting the value to be
|
|
|
|
returned to user space. If you can guarantee that an overflow
|
|
|
|
will never happen you can use sprintf() otherwise you must use
|
|
|
|
scnprintf().
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-07-21 06:22:05 +08:00
|
|
|
- store() should return the number of bytes used from the buffer. If the
|
|
|
|
entire buffer has been used, just return the count argument.
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
- show() or store() can always return errors. If a bad value comes
|
|
|
|
through, be sure to return an error.
|
|
|
|
|
|
|
|
- The object passed to the methods will be pinned in memory via sysfs
|
|
|
|
referencing counting its embedded object. However, the physical
|
|
|
|
entity (e.g. device) the object represents may not be present. Be
|
|
|
|
sure to have a way to check this, if necessary.
|
|
|
|
|
|
|
|
|
|
|
|
A very simple (and naive) implementation of a device attribute is:
|
|
|
|
|
2010-07-21 06:22:05 +08:00
|
|
|
static ssize_t show_name(struct device *dev, struct device_attribute *attr,
|
|
|
|
char *buf)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2010-12-21 20:09:47 +08:00
|
|
|
return scnprintf(buf, PAGE_SIZE, "%s\n", dev->name);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2010-07-21 06:22:05 +08:00
|
|
|
static ssize_t store_name(struct device *dev, struct device_attribute *attr,
|
|
|
|
const char *buf, size_t count)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2010-07-21 06:22:05 +08:00
|
|
|
snprintf(dev->name, sizeof(dev->name), "%.*s",
|
|
|
|
(int)min(count, sizeof(dev->name) - 1), buf);
|
|
|
|
return count;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2005-07-31 19:12:09 +08:00
|
|
|
static DEVICE_ATTR(name, S_IRUGO, show_name, store_name);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
(Note that the real implementation doesn't allow userspace to set the
|
|
|
|
name for a device.)
|
|
|
|
|
|
|
|
|
|
|
|
Top Level Directory Layout
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
The sysfs directory arrangement exposes the relationship of kernel
|
|
|
|
data structures.
|
|
|
|
|
2006-10-04 04:47:42 +08:00
|
|
|
The top level sysfs directory looks like:
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
block/
|
|
|
|
bus/
|
|
|
|
class/
|
2008-04-22 01:51:07 +08:00
|
|
|
dev/
|
2005-04-17 06:20:36 +08:00
|
|
|
devices/
|
|
|
|
firmware/
|
|
|
|
net/
|
2006-04-26 16:49:26 +08:00
|
|
|
fs/
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
devices/ contains a filesystem representation of the device tree. It maps
|
|
|
|
directly to the internal kernel device tree, which is a hierarchy of
|
|
|
|
struct device.
|
|
|
|
|
|
|
|
bus/ contains flat directory layout of the various bus types in the
|
|
|
|
kernel. Each bus's directory contains two subdirectories:
|
|
|
|
|
|
|
|
devices/
|
|
|
|
drivers/
|
|
|
|
|
|
|
|
devices/ contains symlinks for each device discovered in the system
|
|
|
|
that point to the device's directory under root/.
|
|
|
|
|
|
|
|
drivers/ contains a directory for each device driver that is loaded
|
|
|
|
for devices on that particular bus (this assumes that drivers do not
|
|
|
|
span multiple bus types).
|
|
|
|
|
2006-04-26 16:49:26 +08:00
|
|
|
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).
|
|
|
|
|
2008-04-22 01:51:07 +08:00
|
|
|
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.
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
More information can driver-model specific features can be found in
|
|
|
|
Documentation/driver-model/.
|
|
|
|
|
|
|
|
|
|
|
|
TODO: Finish this section.
|
|
|
|
|
|
|
|
|
|
|
|
Current Interfaces
|
|
|
|
~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
The following interface layers currently exist in sysfs:
|
|
|
|
|
|
|
|
|
|
|
|
- devices (include/linux/device.h)
|
|
|
|
----------------------------------
|
|
|
|
Structure:
|
|
|
|
|
|
|
|
struct device_attribute {
|
2009-02-22 14:19:23 +08:00
|
|
|
struct attribute attr;
|
|
|
|
ssize_t (*show)(struct device *dev, struct device_attribute *attr,
|
|
|
|
char *buf);
|
|
|
|
ssize_t (*store)(struct device *dev, struct device_attribute *attr,
|
|
|
|
const char *buf, size_t count);
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Declaring:
|
|
|
|
|
2009-02-22 14:19:23 +08:00
|
|
|
DEVICE_ATTR(_name, _mode, _show, _store);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
Creation/Removal:
|
|
|
|
|
2009-12-18 21:34:19 +08:00
|
|
|
int device_create_file(struct device *dev, const struct device_attribute * attr);
|
|
|
|
void device_remove_file(struct device *dev, const struct device_attribute * attr);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
- bus drivers (include/linux/device.h)
|
|
|
|
--------------------------------------
|
|
|
|
Structure:
|
|
|
|
|
|
|
|
struct bus_attribute {
|
|
|
|
struct attribute attr;
|
|
|
|
ssize_t (*show)(struct bus_type *, char * buf);
|
2010-07-16 02:34:44 +08:00
|
|
|
ssize_t (*store)(struct bus_type *, const char * buf, size_t count);
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Declaring:
|
|
|
|
|
2005-07-31 19:12:09 +08:00
|
|
|
BUS_ATTR(_name, _mode, _show, _store)
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
Creation/Removal:
|
|
|
|
|
|
|
|
int bus_create_file(struct bus_type *, struct bus_attribute *);
|
|
|
|
void bus_remove_file(struct bus_type *, struct bus_attribute *);
|
|
|
|
|
|
|
|
|
|
|
|
- device drivers (include/linux/device.h)
|
|
|
|
-----------------------------------------
|
|
|
|
|
|
|
|
Structure:
|
|
|
|
|
|
|
|
struct driver_attribute {
|
|
|
|
struct attribute attr;
|
|
|
|
ssize_t (*show)(struct device_driver *, char * buf);
|
2009-02-22 14:19:23 +08:00
|
|
|
ssize_t (*store)(struct device_driver *, const char * buf,
|
|
|
|
size_t count);
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Declaring:
|
|
|
|
|
2017-09-18 18:17:57 +08:00
|
|
|
DRIVER_ATTR_RO(_name)
|
|
|
|
DRIVER_ATTR_RW(_name)
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
Creation/Removal:
|
|
|
|
|
2009-12-18 21:34:21 +08:00
|
|
|
int driver_create_file(struct device_driver *, const struct driver_attribute *);
|
|
|
|
void driver_remove_file(struct device_driver *, const struct driver_attribute *);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
|
2011-08-17 00:42:22 +08:00
|
|
|
Documentation
|
|
|
|
~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
The sysfs directory structure and the attributes in each directory define an
|
|
|
|
ABI between the kernel and user space. As for any ABI, it is important that
|
|
|
|
this ABI is stable and properly documented. All new sysfs attributes must be
|
|
|
|
documented in Documentation/ABI. See also Documentation/ABI/README for more
|
|
|
|
information.
|