brintos

brintos / linux-shallow public Read only

0
0
Text · 20.2 KiB · ac22138 Raw
488 lines · plain
1=======================================================2Configfs - Userspace-driven Kernel Object Configuration3=======================================================4 5Joel Becker <joel.becker@oracle.com>6 7Updated: 31 March 20058 9Copyright (c) 2005 Oracle Corporation,10	Joel Becker <joel.becker@oracle.com>11 12 13What is configfs?14=================15 16configfs is a ram-based filesystem that provides the converse of17sysfs's functionality.  Where sysfs is a filesystem-based view of18kernel objects, configfs is a filesystem-based manager of kernel19objects, or config_items.20 21With sysfs, an object is created in kernel (for example, when a device22is discovered) and it is registered with sysfs.  Its attributes then23appear in sysfs, allowing userspace to read the attributes via24readdir(3)/read(2).  It may allow some attributes to be modified via25write(2).  The important point is that the object is created and26destroyed in kernel, the kernel controls the lifecycle of the sysfs27representation, and sysfs is merely a window on all this.28 29A configfs config_item is created via an explicit userspace operation:30mkdir(2).  It is destroyed via rmdir(2).  The attributes appear at31mkdir(2) time, and can be read or modified via read(2) and write(2).32As with sysfs, readdir(3) queries the list of items and/or attributes.33symlink(2) can be used to group items together.  Unlike sysfs, the34lifetime of the representation is completely driven by userspace.  The35kernel modules backing the items must respond to this.36 37Both sysfs and configfs can and should exist together on the same38system.  One is not a replacement for the other.39 40Using configfs41==============42 43configfs can be compiled as a module or into the kernel.  You can access44it by doing::45 46	mount -t configfs none /config47 48The configfs tree will be empty unless client modules are also loaded.49These are modules that register their item types with configfs as50subsystems.  Once a client subsystem is loaded, it will appear as a51subdirectory (or more than one) under /config.  Like sysfs, the52configfs tree is always there, whether mounted on /config or not.53 54An item is created via mkdir(2).  The item's attributes will also55appear at this time.  readdir(3) can determine what the attributes are,56read(2) can query their default values, and write(2) can store new57values.  Don't mix more than one attribute in one attribute file.58 59There are two types of configfs attributes:60 61* Normal attributes, which similar to sysfs attributes, are small ASCII text62  files, with a maximum size of one page (PAGE_SIZE, 4096 on i386).  Preferably63  only one value per file should be used, and the same caveats from sysfs apply.64  Configfs expects write(2) to store the entire buffer at once.  When writing to65  normal configfs attributes, userspace processes should first read the entire66  file, modify the portions they wish to change, and then write the entire67  buffer back.68 69* Binary attributes, which are somewhat similar to sysfs binary attributes,70  but with a few slight changes to semantics.  The PAGE_SIZE limitation does not71  apply, but the whole binary item must fit in single kernel vmalloc'ed buffer.72  The write(2) calls from user space are buffered, and the attributes'73  write_bin_attribute method will be invoked on the final close, therefore it is74  imperative for user-space to check the return code of close(2) in order to75  verify that the operation finished successfully.76  To avoid a malicious user OOMing the kernel, there's a per-binary attribute77  maximum buffer value.78 79When an item needs to be destroyed, remove it with rmdir(2).  An80item cannot be destroyed if any other item has a link to it (via81symlink(2)).  Links can be removed via unlink(2).82 83Configuring FakeNBD: an Example84===============================85 86Imagine there's a Network Block Device (NBD) driver that allows you to87access remote block devices.  Call it FakeNBD.  FakeNBD uses configfs88for its configuration.  Obviously, there will be a nice program that89sysadmins use to configure FakeNBD, but somehow that program has to tell90the driver about it.  Here's where configfs comes in.91 92When the FakeNBD driver is loaded, it registers itself with configfs.93readdir(3) sees this just fine::94 95	# ls /config96	fakenbd97 98A fakenbd connection can be created with mkdir(2).  The name is99arbitrary, but likely the tool will make some use of the name.  Perhaps100it is a uuid or a disk name::101 102	# mkdir /config/fakenbd/disk1103	# ls /config/fakenbd/disk1104	target device rw105 106The target attribute contains the IP address of the server FakeNBD will107connect to.  The device attribute is the device on the server.108Predictably, the rw attribute determines whether the connection is109read-only or read-write::110 111	# echo 10.0.0.1 > /config/fakenbd/disk1/target112	# echo /dev/sda1 > /config/fakenbd/disk1/device113	# echo 1 > /config/fakenbd/disk1/rw114 115That's it.  That's all there is.  Now the device is configured, via the116shell no less.117 118Coding With configfs119====================120 121Every object in configfs is a config_item.  A config_item reflects an122object in the subsystem.  It has attributes that match values on that123object.  configfs handles the filesystem representation of that object124and its attributes, allowing the subsystem to ignore all but the125basic show/store interaction.126 127Items are created and destroyed inside a config_group.  A group is a128collection of items that share the same attributes and operations.129Items are created by mkdir(2) and removed by rmdir(2), but configfs130handles that.  The group has a set of operations to perform these tasks131 132A subsystem is the top level of a client module.  During initialization,133the client module registers the subsystem with configfs, the subsystem134appears as a directory at the top of the configfs filesystem.  A135subsystem is also a config_group, and can do everything a config_group136can.137 138struct config_item139==================140 141::142 143	struct config_item {144		char                    *ci_name;145		char                    ci_namebuf[UOBJ_NAME_LEN];146		struct kref             ci_kref;147		struct list_head        ci_entry;148		struct config_item      *ci_parent;149		struct config_group     *ci_group;150		struct config_item_type *ci_type;151		struct dentry           *ci_dentry;152	};153 154	void config_item_init(struct config_item *);155	void config_item_init_type_name(struct config_item *,156					const char *name,157					struct config_item_type *type);158	struct config_item *config_item_get(struct config_item *);159	void config_item_put(struct config_item *);160 161Generally, struct config_item is embedded in a container structure, a162structure that actually represents what the subsystem is doing.  The163config_item portion of that structure is how the object interacts with164configfs.165 166Whether statically defined in a source file or created by a parent167config_group, a config_item must have one of the _init() functions168called on it.  This initializes the reference count and sets up the169appropriate fields.170 171All users of a config_item should have a reference on it via172config_item_get(), and drop the reference when they are done via173config_item_put().174 175By itself, a config_item cannot do much more than appear in configfs.176Usually a subsystem wants the item to display and/or store attributes,177among other things.  For that, it needs a type.178 179struct config_item_type180=======================181 182::183 184	struct configfs_item_operations {185		void (*release)(struct config_item *);186		int (*allow_link)(struct config_item *src,187				  struct config_item *target);188		void (*drop_link)(struct config_item *src,189				 struct config_item *target);190	};191 192	struct config_item_type {193		struct module                           *ct_owner;194		struct configfs_item_operations         *ct_item_ops;195		struct configfs_group_operations        *ct_group_ops;196		struct configfs_attribute               **ct_attrs;197		struct configfs_bin_attribute		**ct_bin_attrs;198	};199 200The most basic function of a config_item_type is to define what201operations can be performed on a config_item.  All items that have been202allocated dynamically will need to provide the ct_item_ops->release()203method.  This method is called when the config_item's reference count204reaches zero.205 206struct configfs_attribute207=========================208 209::210 211	struct configfs_attribute {212		char                    *ca_name;213		struct module           *ca_owner;214		umode_t                  ca_mode;215		ssize_t (*show)(struct config_item *, char *);216		ssize_t (*store)(struct config_item *, const char *, size_t);217	};218 219When a config_item wants an attribute to appear as a file in the item's220configfs directory, it must define a configfs_attribute describing it.221It then adds the attribute to the NULL-terminated array222config_item_type->ct_attrs.  When the item appears in configfs, the223attribute file will appear with the configfs_attribute->ca_name224filename.  configfs_attribute->ca_mode specifies the file permissions.225 226If an attribute is readable and provides a ->show method, that method will227be called whenever userspace asks for a read(2) on the attribute.  If an228attribute is writable and provides a ->store  method, that method will be229called whenever userspace asks for a write(2) on the attribute.230 231struct configfs_bin_attribute232=============================233 234::235 236	struct configfs_bin_attribute {237		struct configfs_attribute	cb_attr;238		void				*cb_private;239		size_t				cb_max_size;240	};241 242The binary attribute is used when the one needs to use binary blob to243appear as the contents of a file in the item's configfs directory.244To do so add the binary attribute to the NULL-terminated array245config_item_type->ct_bin_attrs, and the item appears in configfs, the246attribute file will appear with the configfs_bin_attribute->cb_attr.ca_name247filename.  configfs_bin_attribute->cb_attr.ca_mode specifies the file248permissions.249The cb_private member is provided for use by the driver, while the250cb_max_size member specifies the maximum amount of vmalloc buffer251to be used.252 253If binary attribute is readable and the config_item provides a254ct_item_ops->read_bin_attribute() method, that method will be called255whenever userspace asks for a read(2) on the attribute.  The converse256will happen for write(2). The reads/writes are buffered so only a257single read/write will occur; the attributes' need not concern itself258with it.259 260struct config_group261===================262 263A config_item cannot live in a vacuum.  The only way one can be created264is via mkdir(2) on a config_group.  This will trigger creation of a265child item::266 267	struct config_group {268		struct config_item		cg_item;269		struct list_head		cg_children;270		struct configfs_subsystem 	*cg_subsys;271		struct list_head		default_groups;272		struct list_head		group_entry;273	};274 275	void config_group_init(struct config_group *group);276	void config_group_init_type_name(struct config_group *group,277					 const char *name,278					 struct config_item_type *type);279 280 281The config_group structure contains a config_item.  Properly configuring282that item means that a group can behave as an item in its own right.283However, it can do more: it can create child items or groups.  This is284accomplished via the group operations specified on the group's285config_item_type::286 287	struct configfs_group_operations {288		struct config_item *(*make_item)(struct config_group *group,289						 const char *name);290		struct config_group *(*make_group)(struct config_group *group,291						   const char *name);292		void (*disconnect_notify)(struct config_group *group,293					  struct config_item *item);294		void (*drop_item)(struct config_group *group,295				  struct config_item *item);296	};297 298A group creates child items by providing the299ct_group_ops->make_item() method.  If provided, this method is called from300mkdir(2) in the group's directory.  The subsystem allocates a new301config_item (or more likely, its container structure), initializes it,302and returns it to configfs.  Configfs will then populate the filesystem303tree to reflect the new item.304 305If the subsystem wants the child to be a group itself, the subsystem306provides ct_group_ops->make_group().  Everything else behaves the same,307using the group _init() functions on the group.308 309Finally, when userspace calls rmdir(2) on the item or group,310ct_group_ops->drop_item() is called.  As a config_group is also a311config_item, it is not necessary for a separate drop_group() method.312The subsystem must config_item_put() the reference that was initialized313upon item allocation.  If a subsystem has no work to do, it may omit314the ct_group_ops->drop_item() method, and configfs will call315config_item_put() on the item on behalf of the subsystem.316 317Important:318   drop_item() is void, and as such cannot fail.  When rmdir(2)319   is called, configfs WILL remove the item from the filesystem tree320   (assuming that it has no children to keep it busy).  The subsystem is321   responsible for responding to this.  If the subsystem has references to322   the item in other threads, the memory is safe.  It may take some time323   for the item to actually disappear from the subsystem's usage.  But it324   is gone from configfs.325 326When drop_item() is called, the item's linkage has already been torn327down.  It no longer has a reference on its parent and has no place in328the item hierarchy.  If a client needs to do some cleanup before this329teardown happens, the subsystem can implement the330ct_group_ops->disconnect_notify() method.  The method is called after331configfs has removed the item from the filesystem view but before the332item is removed from its parent group.  Like drop_item(),333disconnect_notify() is void and cannot fail.  Client subsystems should334not drop any references here, as they still must do it in drop_item().335 336A config_group cannot be removed while it still has child items.  This337is implemented in the configfs rmdir(2) code.  ->drop_item() will not be338called, as the item has not been dropped.  rmdir(2) will fail, as the339directory is not empty.340 341struct configfs_subsystem342=========================343 344A subsystem must register itself, usually at module_init time.  This345tells configfs to make the subsystem appear in the file tree::346 347	struct configfs_subsystem {348		struct config_group	su_group;349		struct mutex		su_mutex;350	};351 352	int configfs_register_subsystem(struct configfs_subsystem *subsys);353	void configfs_unregister_subsystem(struct configfs_subsystem *subsys);354 355A subsystem consists of a toplevel config_group and a mutex.356The group is where child config_items are created.  For a subsystem,357this group is usually defined statically.  Before calling358configfs_register_subsystem(), the subsystem must have initialized the359group via the usual group _init() functions, and it must also have360initialized the mutex.361 362When the register call returns, the subsystem is live, and it363will be visible via configfs.  At that point, mkdir(2) can be called and364the subsystem must be ready for it.365 366An Example367==========368 369The best example of these basic concepts is the simple_children370subsystem/group and the simple_child item in371samples/configfs/configfs_sample.c. It shows a trivial object displaying372and storing an attribute, and a simple group creating and destroying373these children.374 375Hierarchy Navigation and the Subsystem Mutex376============================================377 378There is an extra bonus that configfs provides.  The config_groups and379config_items are arranged in a hierarchy due to the fact that they380appear in a filesystem.  A subsystem is NEVER to touch the filesystem381parts, but the subsystem might be interested in this hierarchy.  For382this reason, the hierarchy is mirrored via the config_group->cg_children383and config_item->ci_parent structure members.384 385A subsystem can navigate the cg_children list and the ci_parent pointer386to see the tree created by the subsystem.  This can race with configfs'387management of the hierarchy, so configfs uses the subsystem mutex to388protect modifications.  Whenever a subsystem wants to navigate the389hierarchy, it must do so under the protection of the subsystem390mutex.391 392A subsystem will be prevented from acquiring the mutex while a newly393allocated item has not been linked into this hierarchy.   Similarly, it394will not be able to acquire the mutex while a dropping item has not395yet been unlinked.  This means that an item's ci_parent pointer will396never be NULL while the item is in configfs, and that an item will only397be in its parent's cg_children list for the same duration.  This allows398a subsystem to trust ci_parent and cg_children while they hold the399mutex.400 401Item Aggregation Via symlink(2)402===============================403 404configfs provides a simple group via the group->item parent/child405relationship.  Often, however, a larger environment requires aggregation406outside of the parent/child connection.  This is implemented via407symlink(2).408 409A config_item may provide the ct_item_ops->allow_link() and410ct_item_ops->drop_link() methods.  If the ->allow_link() method exists,411symlink(2) may be called with the config_item as the source of the link.412These links are only allowed between configfs config_items.  Any413symlink(2) attempt outside the configfs filesystem will be denied.414 415When symlink(2) is called, the source config_item's ->allow_link()416method is called with itself and a target item.  If the source item417allows linking to target item, it returns 0.  A source item may wish to418reject a link if it only wants links to a certain type of object (say,419in its own subsystem).420 421When unlink(2) is called on the symbolic link, the source item is422notified via the ->drop_link() method.  Like the ->drop_item() method,423this is a void function and cannot return failure.  The subsystem is424responsible for responding to the change.425 426A config_item cannot be removed while it links to any other item, nor427can it be removed while an item links to it.  Dangling symlinks are not428allowed in configfs.429 430Automatically Created Subgroups431===============================432 433A new config_group may want to have two types of child config_items.434While this could be codified by magic names in ->make_item(), it is much435more explicit to have a method whereby userspace sees this divergence.436 437Rather than have a group where some items behave differently than438others, configfs provides a method whereby one or many subgroups are439automatically created inside the parent at its creation.  Thus,440mkdir("parent") results in "parent", "parent/subgroup1", up through441"parent/subgroupN".  Items of type 1 can now be created in442"parent/subgroup1", and items of type N can be created in443"parent/subgroupN".444 445These automatic subgroups, or default groups, do not preclude other446children of the parent group.  If ct_group_ops->make_group() exists,447other child groups can be created on the parent group directly.448 449A configfs subsystem specifies default groups by adding them using the450configfs_add_default_group() function to the parent config_group451structure.  Each added group is populated in the configfs tree at the same452time as the parent group.  Similarly, they are removed at the same time453as the parent.  No extra notification is provided.  When a ->drop_item()454method call notifies the subsystem the parent group is going away, it455also means every default group child associated with that parent group.456 457As a consequence of this, default groups cannot be removed directly via458rmdir(2).  They also are not considered when rmdir(2) on the parent459group is checking for children.460 461Dependent Subsystems462====================463 464Sometimes other drivers depend on particular configfs items.  For465example, ocfs2 mounts depend on a heartbeat region item.  If that466region item is removed with rmdir(2), the ocfs2 mount must BUG or go467readonly.  Not happy.468 469configfs provides two additional API calls: configfs_depend_item() and470configfs_undepend_item().  A client driver can call471configfs_depend_item() on an existing item to tell configfs that it is472depended on.  configfs will then return -EBUSY from rmdir(2) for that473item.  When the item is no longer depended on, the client driver calls474configfs_undepend_item() on it.475 476These API cannot be called underneath any configfs callbacks, as477they will conflict.  They can block and allocate.  A client driver478probably shouldn't calling them of its own gumption.  Rather it should479be providing an API that external subsystems call.480 481How does this work?  Imagine the ocfs2 mount process.  When it mounts,482it asks for a heartbeat region item.  This is done via a call into the483heartbeat code.  Inside the heartbeat code, the region item is looked484up.  Here, the heartbeat code calls configfs_depend_item().  If it485succeeds, then heartbeat knows the region is safe to give to ocfs2.486If it fails, it was being torn down anyway, and heartbeat can gracefully487pass up an error.488