configfs: implement committable items and add sample code
Subsystems (2)
v3 cover configfs: implement committable items and add sample code
From: Bartosz Golaszewski <[email protected]>
Committable items in configfs are well defined and documented but unfortunately so far never implemented.
The use-case we have over at the GPIO subsystem is using configfs in conjunction with sysfs to replace our current gpio-mockup testing module with one that will be much more flexible and will allow complete coverage of the GPIO uAPI.
The current gpio-mockup module is controlled using module parameters which forces the user to reload it everytime they need to change the chip configuration or layout and makes it difficult to extend its functionality.
Testing module based on configfs would allow fine-grained control over dummy GPIO chips but since GPIO devices must be configured before they are instantiated, we need committable items.
This implements them and adds code examples to configfs_sample module. The first two patches are just cosmetic.
v1 -> v2:
- fix a 'set but not used' build warning reported by kernel test robot
v2 -> v3:
- use (1UL << bit) instead of BIT() in patch 2/4
- extend configfs_dump_one() to make it print the new flags
- clear the CONFIGFS_USET_DIR bit on the live group dirent
Rebased on top of v5.11-rc1.
Bartosz Golaszewski (4):
configfs: increase the item name length
configfs: use (1UL << bit) for internal flags
configfs: implement committable items
samples: configfs: add a committable group
Documentation/filesystems/configfs.rst | 6 +- fs/configfs/configfs_internal.h | 22 +-- fs/configfs/dir.c | 240 ++++++++++++++++++++++++- fs/configfs/file.c | 8 + include/linux/configfs.h | 3 +- samples/configfs/configfs_sample.c | 150 ++++++++++++++++ 6 files changed, 409 insertions(+), 20 deletions(-)
Signature · 1 line
2.29.1
PATCH RESEND v3 1/4 configfs: increase the item name length
From: Bartosz Golaszewski <[email protected]>
20 characters limit for item name is relatively small. Let's increase it to 32 to fit '04-committable-children' - a name we'll use in the sample code for committable items.
--- include/linux/configfs.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/configfs.h +1 -1
@@ -27,7 +27,7 @@#include <linux/kref.h> /* struct kref */ #include <linux/mutex.h> /* struct mutex */ -#define CONFIGFS_ITEM_NAME_LEN 20 +#define CONFIGFS_ITEM_NAME_LEN 32struct module ;
Signature · 1 line
2.29.1
PATCH RESEND v3 2/4 configfs: use (1UL << bit) for internal flags
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
For better readability and maintenance: use the (1UL << bit) for flag definitions.
--- fs/configfs/configfs_internal.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
fs/configfs/configfs_internal.h +10 -10
@@ -46,16 +46,16 @@ struct configfs_dirent {struct configfs_fragment *s_frag; }; -#define CONFIGFS_ROOT 0x0001 -#define CONFIGFS_DIR 0x0002 -#define CONFIGFS_ITEM_ATTR 0x0004 -#define CONFIGFS_ITEM_BIN_ATTR 0x0008 -#define CONFIGFS_ITEM_LINK 0x0020 -#define CONFIGFS_USET_DIR 0x0040 -#define CONFIGFS_USET_DEFAULT 0x0080 -#define CONFIGFS_USET_DROPPING 0x0100 -#define CONFIGFS_USET_IN_MKDIR 0x0200 -#define CONFIGFS_USET_CREATING 0x0400 +#define CONFIGFS_ROOT (1UL << 0) +#define CONFIGFS_DIR (1UL << 1) +#define CONFIGFS_ITEM_ATTR (1UL << 2) +#define CONFIGFS_ITEM_BIN_ATTR (1UL << 3) +#define CONFIGFS_ITEM_LINK (1UL << 5) +#define CONFIGFS_USET_DIR (1UL << 6) +#define CONFIGFS_USET_DEFAULT (1UL << 7) +#define CONFIGFS_USET_DROPPING (1UL << 8) +#define CONFIGFS_USET_IN_MKDIR (1UL << 9) +#define CONFIGFS_USET_CREATING (1UL << 10)#define CONFIGFS_NOT_PINNED (CONFIGFS_ITEM_ATTR | CONFIGFS_ITEM_BIN_ATTR)extern struct mutex configfs_symlink_mutex;
Signature · 1 line
2.29.1
PATCH RESEND v3 3/4 configfs: implement committable items
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
This implements configfs committable items. We mostly follow the documentation except that we extend config_group_ops with uncommit_item() callback for reverting the changes made by commit_item().
Each committable group has two sub-directories: pending and live. New items can only be created in pending/. Attributes can only be modified while the item is in pending/. Once it's ready to be committed, it must be moved over to live/ using the rename() system call. This is when the commit_item() function will be called.
Implementation-wise: we reuse the default group mechanism to elegantly plug the new pseude-groups into configfs. The pending group inherits the parent group's operations so that config_items can be seamlesly created in it using the callbacks supplied by the user as part of the committable group itself.
--- Documentation/filesystems/configfs.rst | 6 +- fs/configfs/configfs_internal.h | 2 + fs/configfs/dir.c | 240 ++++++++++++++++++++++++- fs/configfs/file.c | 8 + include/linux/configfs.h | 1 + 5 files changed, 248 insertions(+), 9 deletions(-)
Documentation/filesystems/configfs.rst +2 -4
@@ -290,6 +290,7 @@ config_item_type:: struct config_group *(*make_group)(struct config_group *group, const char *name); int (*commit_item)(struct config_item *item); + int (*uncommit_item)(struct config_item *item); void (*disconnect_notify)(struct config_group *group, struct config_item *item); void (*drop_item)(struct config_group *group,
@@ -490,9 +491,6 @@ pass up an error. Committable Items ================= -Note: - Committable items are currently unimplemented. - Some config_items cannot have a valid initial state. That is, no default values can be specified for the item's attributes such that the item can do its work. Userspace must configure one or more attributes,
@@ -532,4 +530,4 @@ method returns zero and the item is moved to the "live" directory. As rmdir(2) does not work in the "live" directory, an item must be shutdown, or "uncommitted". Again, this is done via rename(2), this time from the "live" directory back to the "pending" one. The subsystem -is notified by the ct_group_ops->uncommit_object() method. +is notified by the ct_group_ops->uncommit_item() method.
fs/configfs/configfs_internal.h +2 -0
@@ -56,6 +56,8 @@ struct configfs_dirent {#define CONFIGFS_USET_DROPPING (1UL << 8)#define CONFIGFS_USET_IN_MKDIR (1UL << 9)#define CONFIGFS_USET_CREATING (1UL << 10) +#define CONFIGFS_GROUP_PENDING (1UL << 11) +#define CONFIGFS_GROUP_LIVE (1UL << 12)#define CONFIGFS_NOT_PINNED (CONFIGFS_ITEM_ATTR | CONFIGFS_ITEM_BIN_ATTR)extern struct mutex configfs_symlink_mutex;
fs/configfs/dir.c +235 -5
286 lines not on this page · raw message
fs/configfs/file.c +8 -0
@@ -243,9 +243,17 @@ fill_write_buffer(struct configfs_buffer * buffer, const char __user * buf, sizestatic int flush_write_buffer (struct file *file,struct configfs_buffer *buffer,size_t count) { +struct config_item *parent_item = buffer->item->ci_parent;struct configfs_fragment *frag =to_frag (file); +struct configfs_dirent *sd;int res = -ENOENT ; +if (parent_item && parent_item->ci_dentry) { + sd = parent_item->ci_dentry->d_fsdata; +if (sd->s_type &CONFIGFS_GROUP_LIVE ) +return -EPERM ; + } +down_read (&frag->frag_sem);if (!frag->frag_dead) res = buffer->attr->store (buffer->item, buffer->page, count);
include/linux/configfs.h +1 -0
@@ -219,6 +219,7 @@ struct configfs_group_operations {struct config_item *(*make_item)(struct config_group *group,const char *name);struct config_group *(*make_group)(struct config_group *group,const char *name);int (*commit_item)(struct config_item *item); +int (*uncommit_item)(struct config_item *item);void (*disconnect_notify)(struct config_group *group,struct config_item *item);void (*drop_item)(struct config_group *group,struct config_item *item); };
Signature · 1 line
2.29.1
PATCH RESEND v3 4/4 samples: configfs: add a committable group
In reply to Bartosz GolaszewskiFrom: Bartosz Golaszewski <[email protected]>
Add an example of using committable items to configfs samples. Each config item has two attributes: read-write 'storeme' which works similarly to other examples in this file and a read-only 'committed' attribute which changes its value between false and true depending on whether it's committed or not at the moment.
--- samples/configfs/configfs_sample.c | 150 +++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+)
samples/configfs/configfs_sample.c +150 -0
@@ -315,6 +315,155 @@ static struct configfs_subsystem group_children_subsys = {/* ----------------------------------------------------------------- */ +/* +* 04-committable-children +* +* This is an example of a committable group. It's similar to the simple +* children example but each config_item has an additional 'committed' +* attribute which is read-only and is only modified when the config_item +* is moved from the 'pending' to the 'live' directory. +*/ + +struct committable_child { +struct config_item item; +int storeme; +bool committed; +}; + +static inline struct committable_child * +to_committable_child (struct config_item *item) +{ + return container_of(item, struct committable_child, item); +} + +static ssize_t +committable_child_storeme_show (struct config_item *item,char *page) +{ +return sprintf (page,"%d\n" ,to_committable_child (item)->storeme); +} + +static ssize_t committable_child_storeme_store (struct config_item *item, +const char *page,size_t count) +{ +struct committable_child *child =to_committable_child (item); +int ret; + + ret =kstrtoint (page,10 , &child->storeme); +if (ret) +return ret; + +return count; +} + +CONFIGFS_ATTR (committable_child_, storeme); + +static ssize_t +committable_child_committed_show (struct config_item *item,char *page) +{ +return sprintf (page,"%s\n" , +to_committable_child (item)->committed ?"true" :"false" ); +} + +CONFIGFS_ATTR_RO (committable_child_, committed); + +static struct configfs_attribute *committable_child_attrs[] = { + &committable_child_attr_storeme, + &committable_child_attr_committed, +NULL , +}; + +static void committable_child_release (struct config_item *item) +{ +kfree (to_committable_child (item)); +} + +static struct configfs_item_operations committable_child_item_ops = { + .release = committable_child_release, +}; + +static const struct config_item_type committable_child_type = { + .ct_item_ops = &committable_child_item_ops, + .ct_attrs = committable_child_attrs, + .ct_owner =THIS_MODULE , +}; + +struct committable_children { +struct config_group group; +}; + +static struct config_item * +committable_children_make_item (struct config_group *group,const char *name) +{ +struct committable_child *child; + + child =kzalloc (sizeof (*child),GFP_KERNEL ); +if (!child) +return ERR_PTR (-ENOMEM ); + +config_item_init_type_name (&child->item, name, &committable_child_type); + +return &child->item; +} + +static ssize_t +committable_children_description_show (struct config_item *item,char *page) +{ +return sprintf (page, +"[04-committable-children]\n" +"\n" +"This subsystem allows creation of committable config_items. The subsystem\n" +"has two subdirectories: pending and live. New config_items can only be\n" +"created in pending/ and they have one writable and readable attribute as\n" +"well as a single read-only attribute. The latter is only changed once the\n" +"item is 'committed'. This is done by moving the config_item (using\n" +"rename()) to the live/ directory. At that point even the read-write\n" +"attributes can no longer be written to.\n" ); +} + +CONFIGFS_ATTR_RO (committable_children_, description); + +static struct configfs_attribute *committable_children_attrs[] = { + &committable_children_attr_description, +NULL , +}; + +static int committable_children_commit_item (struct config_item *item) +{ +to_committable_child (item)->committed = true; + +return 0 ; +} + +static int committable_children_uncommit_item (struct config_item *item) +{ +to_committable_child (item)->committed = false; + +return 0 ; +} + +static struct configfs_group_operations committable_children_group_ops = { + .make_item = committable_children_make_item, + .commit_item = committable_children_commit_item, + .uncommit_item = committable_children_uncommit_item, +}; + +static const struct config_item_type committable_children_type = { + .ct_group_ops = &committable_children_group_ops, + .ct_attrs = committable_children_attrs, + .ct_owner =THIS_MODULE , +}; + +static struct configfs_subsystem committable_children_subsys = { + .su_group = { + .cg_item = { + .ci_namebuf ="04-committable-children" , + .ci_type = &committable_children_type, + }, + }, +}; + +/* ----------------------------------------------------------------- */ +/* * We're now done with our subsystem definitions. * For convenience in this module, here's a list of them all. It
@@ -326,6 +475,7 @@ static struct configfs_subsystem *example_subsys[] = { &childless_subsys.subsys, &simple_children_subsys, &group_children_subsys, + &committable_children_subsys, NULL, };
Signature · 1 line
2.29.1
On Tue, Dec 29, 2020 at 11:22 AM Bartosz Golaszewski <[email protected]> wrote:
Committable items in configfs are well defined and documented but unfortunately
Quoted 21 lines
so far never implemented.
The use-case we have over at the GPIO subsystem is using configfs in conjunction with sysfs to replace our current gpio-mockup testing module with one that will be much more flexible and will allow complete coverage of the GPIO uAPI.
The current gpio-mockup module is controlled using module parameters which forces the user to reload it everytime they need to change the chip configuration or layout and makes it difficult to extend its functionality.
Testing module based on configfs would allow fine-grained control over dummy GPIO chips but since GPIO devices must be configured before they are instantiated, we need committable items.
This implements them and adds code examples to
configfs_samplemodule. The first two patches are just cosmetic.v1 -> v2:
- fix a 'set but not used' build warning reported by kernel test robot
Quoted 6 lines
v2 -> v3:
- use (1UL << bit) instead ofBIT()in patch 2/4
- extendconfigfs_dump_one()to make it print the new flags
- clear theCONFIGFS_USET_DIRbit on the live group direntRebased on top of v5.11-rc1.
This patch set makes a lot of sense to me.
Yours,
Linus Walleij
Quoted 46 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
Committable items in configfs are well defined and documented but unfortunately so far never implemented.
The use-case we have over at the GPIO subsystem is using configfs in conjunction with sysfs to replace our current gpio-mockup testing module with one that will be much more flexible and will allow complete coverage of the GPIO uAPI.
The current gpio-mockup module is controlled using module parameters which forces the user to reload it everytime they need to change the chip configuration or layout and makes it difficult to extend its functionality.
Testing module based on configfs would allow fine-grained control over dummy GPIO chips but since GPIO devices must be configured before they are instantiated, we need committable items.
This implements them and adds code examples to
configfs_samplemodule. The first two patches are just cosmetic.v1 -> v2:
- fix a 'set but not used' build warning reported by kernel test robotv2 -> v3:
- use (1UL << bit) instead ofBIT()in patch 2/4
- extendconfigfs_dump_one()to make it print the new flags
- clear theCONFIGFS_USET_DIRbit on the live group direntRebased on top of v5.11-rc1.
Bartosz Golaszewski (4):
configfs: increase the item name length
configfs: use (1UL << bit) for internal flags
configfs: implement committable items
samples: configfs: add a committable groupDocumentation/filesystems/configfs.rst | 6 +- fs/configfs/configfs_internal.h | 22 +-- fs/configfs/dir.c | 240 ++++++++++++++++++++++++- fs/configfs/file.c | 8 + include/linux/configfs.h | 3 +- samples/configfs/configfs_sample.c | 150 ++++++++++++++++ 6 files changed, 409 insertions(+), 20 deletions(-)
Quoted 3 lines
--
2.29.1
Joel, Christoph,
This series in its current form has been on the list for many weeks. Are there any objections from your side against merging it for v5.12?
Best regards,
Bartosz Golaszewski
Quoted 47 lines · Bartosz Golaszewski
Quoted 46 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
Committable items in configfs are well defined and documented but unfortunately so far never implemented.
The use-case we have over at the GPIO subsystem is using configfs in conjunction with sysfs to replace our current gpio-mockup testing module with one that will be much more flexible and will allow complete coverage of the GPIO uAPI.
The current gpio-mockup module is controlled using module parameters which forces the user to reload it everytime they need to change the chip configuration or layout and makes it difficult to extend its functionality.
Testing module based on configfs would allow fine-grained control over dummy GPIO chips but since GPIO devices must be configured before they are instantiated, we need committable items.
This implements them and adds code examples to
configfs_samplemodule. The first two patches are just cosmetic.v1 -> v2:
- fix a 'set but not used' build warning reported by kernel test robotv2 -> v3:
- use (1UL << bit) instead ofBIT()in patch 2/4
- extendconfigfs_dump_one()to make it print the new flags
- clear theCONFIGFS_USET_DIRbit on the live group direntRebased on top of v5.11-rc1.
Bartosz Golaszewski (4):
configfs: increase the item name length
configfs: use (1UL << bit) for internal flags
configfs: implement committable items
samples: configfs: add a committable groupDocumentation/filesystems/configfs.rst | 6 +- fs/configfs/configfs_internal.h | 22 +-- fs/configfs/dir.c | 240 ++++++++++++++++++++++++- fs/configfs/file.c | 8 + include/linux/configfs.h | 3 +- samples/configfs/configfs_sample.c | 150 ++++++++++++++++ 6 files changed, 409 insertions(+), 20 deletions(-)
Quoted 8 lines
Quoted 3 lines
--
2.29.1Joel, Christoph,
This series in its current form has been on the list for many weeks. Are there any objections from your side against merging it for v5.12?
Hey Bartosz,
I don't have much time for code reviews these days (you can see my lack of mailing list stats), so I only saw your series today. I accidentally reviewed the first version, but my comments there still stand. Overall, I'm very happy to see this implemented.
Thanks,
Joel
Signature · 5 lines
"Get right to the heart of matters. It's the heart that matters more." http://www.jlbec.org/ [email protected]