From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E7187C433F5 for ; Tue, 19 Oct 2021 14:49:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CA9976135E for ; Tue, 19 Oct 2021 14:49:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232371AbhJSOvt (ORCPT ); Tue, 19 Oct 2021 10:51:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41036 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232740AbhJSOvf (ORCPT ); Tue, 19 Oct 2021 10:51:35 -0400 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DBA66C061768 for ; Tue, 19 Oct 2021 07:49:21 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id a140-20020a1c7f92000000b0030d8315b593so2971082wmd.5 for ; Tue, 19 Oct 2021 07:49:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=pSX6xpIY0vqw/EiCB6+AarjCoI5aNC4wN0qJcbQixSM=; b=YYmlPMhJJDlZ/38LnbayB6iBr5X5i1WJlFYXBm+Pt8MuTGT152BRx3IckMoWkugIGR Xlv+6GHdso/+WhRuhBCSBe0wCw4AAgrAEaMY8RzCxMPVUdg66Muo3Mdvlm5axoqYOh6N ggrUS71J/xQ1/3wizU+DiI39a6hnuQMOqeIX3GUT4YZ9RvefgjRN9hBdGOZSUdmrKgSs hhckLEsmhleDQydMZUBqL01fkniVwJymYD8rm1RGwG3j6LDy3g4frgr3FXiALnzNryZ/ uwkgkBpLhCjj2PjUBFYdQAbNnwMwIUzSDZ5CO4CMjrrOn/s8ibsZneg+J5eAPHpJzHP2 ke4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=pSX6xpIY0vqw/EiCB6+AarjCoI5aNC4wN0qJcbQixSM=; b=KKYDX7TJ5vzi+lw/nEy1zCFqQtvZ4zVlVz93Ew0ja8XNUz0NfdBx4qVBjff9ybD/te 2Aow4T90h0m7T6VYEgtm/E6XXVljQHWANiRHim5IJZNXt9QQn+pmj26VdkZnKCVJTbsB A96nVVBLf9AdNtdhGt7pO+aCFY+YtmyFdKcSYNCwwS7HEDCrxbTVym9lCPfHcCfAytrt v0G4eQMha0LKcCU2OoEhmaR4OGzjMlIJRGUyEeP2vk/MqzsFUBdSxTy0zFisa0+ND85T 3Pv5fC/rV4JrMwED8LWzRLQWLZCU7NyXxuxgqBzMX1pO89WoPSQzU9Y3zMUqT4J3B33V JF/w== X-Gm-Message-State: AOAM533nzIO4pAS6otFI9x6YGmKKRjWURfv6fSartRWDU/Bfj7EmM5T2 kgRg3PvvTQnQSQYfiXFMJhUoMw== X-Google-Smtp-Source: ABdhPJwes/n7z4pPQy17twnD1a6tRq+b6GnGmOe6cFGOozkmNC4uhBWPpV++M4jLLLOAJ1qdISxqUQ== X-Received: by 2002:a5d:47c6:: with SMTP id o6mr44931270wrc.416.1634654960349; Tue, 19 Oct 2021 07:49:20 -0700 (PDT) Received: from debian-brgl.home ([2a01:cb1d:334:ac00:7d50:ff5:f5c1:e225]) by smtp.gmail.com with ESMTPSA id j1sm16212752wrd.28.2021.10.19.07.49.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Oct 2021 07:49:19 -0700 (PDT) From: Bartosz Golaszewski To: Joel Becker , Christoph Hellwig , Shuah Khan , Linus Walleij , Andy Shevchenko , =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= , Geert Uytterhoeven , Kent Gibson , Jonathan Corbet , Greg Kroah-Hartman , Al Viro , Jack Winch , Viresh Kumar Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v8 3/8] configfs: implement committable items Date: Tue, 19 Oct 2021 16:49:04 +0200 Message-Id: <20211019144909.21959-4-brgl@bgdev.pl> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20211019144909.21959-1-brgl@bgdev.pl> References: <20211019144909.21959-1-brgl@bgdev.pl> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-doc@vger.kernel.org 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. Signed-off-by: Bartosz Golaszewski Acked-by: Linus Walleij Acked-by: Andy Shevchenko --- Documentation/filesystems/configfs.rst | 6 +- fs/configfs/configfs_internal.h | 2 + fs/configfs/dir.c | 276 ++++++++++++++++++++++++- include/linux/configfs.h | 1 + 4 files changed, 275 insertions(+), 10 deletions(-) diff --git a/Documentation/filesystems/configfs.rst b/Documentation/filesystems/configfs.rst index 1d3d6f4a82a9..7e0e7c356450 100644 --- a/Documentation/filesystems/configfs.rst +++ b/Documentation/filesystems/configfs.rst @@ -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. diff --git a/fs/configfs/configfs_internal.h b/fs/configfs/configfs_internal.h index 0c7a3d857fde..7dc0f2754c7e 100644 --- a/fs/configfs/configfs_internal.h +++ b/fs/configfs/configfs_internal.h @@ -54,6 +54,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; diff --git a/fs/configfs/dir.c b/fs/configfs/dir.c index 1466b5d01cbb..3251fb2ec010 100644 --- a/fs/configfs/dir.c +++ b/fs/configfs/dir.c @@ -42,6 +42,14 @@ static void configfs_d_iput(struct dentry * dentry, if (sd) { /* Coordinate with configfs_readdir */ spin_lock(&configfs_dirent_lock); + + /* + * Free memory allocated for the pending and live directories + * of committable groups. + */ + if (sd->s_type & (CONFIGFS_GROUP_PENDING | CONFIGFS_GROUP_LIVE)) + kfree(sd->s_element); + /* * Set sd->s_dentry to null only when this dentry is the one * that is going to be killed. Otherwise configfs_d_iput may @@ -833,6 +841,134 @@ static void configfs_detach_item(struct config_item *item) configfs_remove_dir(item); } +static bool is_committable_group(struct config_item *item) +{ + const struct config_item_type *type = item->ci_type; + + if (type && type->ct_group_ops && + type->ct_group_ops->commit_item && + type->ct_group_ops->uncommit_item) + return true; + + return false; +} + +struct pending_group_data { + struct config_group group; + struct config_item_type type; + struct configfs_group_operations group_ops; +}; + +struct live_group_data { + struct config_group group; + struct config_item_type type; +}; + +static int create_pending_group(struct config_item *parent_item, + struct configfs_fragment *frag) +{ + const struct config_item_type *parent_type = parent_item->ci_type; + struct pending_group_data *pending; + struct configfs_dirent *sd; + int ret; + + pending = kzalloc(sizeof(*pending), GFP_KERNEL); + if (!pending) + return -ENOMEM; + + /* + * Let's inherit the group_ops from the parent except for item + * committing and uncommitting. + */ + memcpy(&pending->group_ops, parent_type->ct_group_ops, + sizeof(struct configfs_group_operations)); + pending->type.ct_group_ops = &pending->group_ops; + pending->type.ct_group_ops->commit_item = NULL; + pending->type.ct_group_ops->uncommit_item = NULL; + + /* Let's directly reuse item_ops. */ + pending->type.ct_item_ops = parent_type->ct_item_ops; + pending->type.ct_owner = parent_type->ct_owner; + + config_group_init_type_name(&pending->group, "pending", &pending->type); + + ret = create_default_group(to_config_group(parent_item), + &pending->group, frag); + if (ret) { + kfree(pending); + return ret; + } + + link_group(to_config_group(parent_item), &pending->group); + + sd = pending->group.cg_item.ci_dentry->d_fsdata; + /* Allow creating config_items in 'pending' group. */ + sd->s_type |= (CONFIGFS_GROUP_PENDING | CONFIGFS_USET_DIR); + + return 0; +} + +static int create_live_group(struct config_item *parent_item, + struct configfs_fragment *frag) +{ + struct live_group_data *live; + struct configfs_dirent *sd; + int ret; + + live = kzalloc(sizeof(*live), GFP_KERNEL); + if (!live) + return -ENOMEM; + + live->type.ct_owner = parent_item->ci_type->ct_owner; + + config_group_init_type_name(&live->group, "live", &live->type); + + ret = create_default_group(to_config_group(parent_item), + &live->group, frag); + if (ret) { + kfree(live); + return ret; + } + + link_group(to_config_group(parent_item), &live->group); + + sd = live->group.cg_item.ci_dentry->d_fsdata; + sd->s_type |= CONFIGFS_GROUP_LIVE; + sd->s_type &= ~CONFIGFS_USET_DIR; + + return 0; +} + +static int create_committable_groups(struct config_item *parent_item, + struct configfs_fragment *frag) +{ + struct configfs_dirent *sd; + int ret; + + ret = create_pending_group(parent_item, frag); + if (ret) + return ret; + + ret = create_live_group(parent_item, frag); + if (ret) { + detach_groups(to_config_group(parent_item)); + return ret; + } + + /* Disallow creating items directly in the committable group. */ + sd = parent_item->ci_dentry->d_fsdata; + sd->s_type &= ~CONFIGFS_USET_DIR; + + return 0; +} + +static void dentry_mark_dead(struct config_item *item, struct dentry *dentry) +{ + configfs_detach_item(item); + d_inode(dentry)->i_flags |= S_DEAD; + dont_mount(dentry); +} + static int configfs_attach_group(struct config_item *parent_item, struct config_item *item, struct dentry *dentry, @@ -858,11 +994,15 @@ static int configfs_attach_group(struct config_item *parent_item, inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD); configfs_adjust_dir_dirent_depth_before_populate(sd); ret = populate_groups(to_config_group(item), frag); - if (ret) { - configfs_detach_item(item); - d_inode(dentry)->i_flags |= S_DEAD; - dont_mount(dentry); + if (ret) + dentry_mark_dead(item, dentry); + + if (is_committable_group(item)) { + ret = create_committable_groups(item, frag); + if (ret) + dentry_mark_dead(item, dentry); } + configfs_adjust_dir_dirent_depth_after_populate(sd); inode_unlock(d_inode(dentry)); if (ret) @@ -939,6 +1079,8 @@ static void configfs_dump_one(struct configfs_dirent *sd, int level) type_print(CONFIGFS_USET_DIR); type_print(CONFIGFS_USET_DEFAULT); type_print(CONFIGFS_USET_DROPPING); + type_print(CONFIGFS_GROUP_PENDING); + type_print(CONFIGFS_GROUP_LIVE); #undef type_print } @@ -1241,6 +1383,27 @@ int configfs_depend_item_unlocked(struct configfs_subsystem *caller_subsys, } EXPORT_SYMBOL(configfs_depend_item_unlocked); +static int committable_item_check_live_group(const char *new_item, + struct configfs_dirent *parent_sd) +{ + struct configfs_dirent *sd, *live = NULL; + + list_for_each_entry(sd, &parent_sd->s_children, s_sibling) { + if (strcmp(configfs_get_name(sd), "live") == 0) + live = sd; + } + + if (WARN_ON(!live)) + return -ENOENT; /* Something's wrong in the configfs code. */ + + list_for_each_entry(sd, &live->s_children, s_sibling) { + if (configfs_dirent_exists(live, new_item)) + return -EEXIST; + } + + return 0; +} + static int configfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, struct dentry *dentry, umode_t mode) { @@ -1250,7 +1413,7 @@ static int configfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, struct config_item *item = NULL; struct config_item *parent_item; struct configfs_subsystem *subsys; - struct configfs_dirent *sd; + struct configfs_dirent *sd, *parent_sd; const struct config_item_type *type; struct module *subsys_owner = NULL, *new_item_owner = NULL; struct configfs_fragment *frag; @@ -1272,6 +1435,14 @@ static int configfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, goto out; } + if (sd->s_type & CONFIGFS_GROUP_PENDING) { + parent_sd = dentry->d_parent->d_parent->d_fsdata; + ret = committable_item_check_live_group(dentry->d_name.name, + parent_sd); + if (ret) + goto out; + } + frag = new_fragment(); if (!frag) { ret = -ENOMEM; @@ -1430,7 +1601,7 @@ static int configfs_rmdir(struct inode *dir, struct dentry *dentry) struct config_item *parent_item; struct config_item *item; struct configfs_subsystem *subsys; - struct configfs_dirent *sd; + struct configfs_dirent *sd, *parent_sd; struct configfs_fragment *frag; struct module *subsys_owner = NULL, *dead_item_owner = NULL; int ret; @@ -1449,6 +1620,12 @@ static int configfs_rmdir(struct inode *dir, struct dentry *dentry) return -EINVAL; } + parent_sd = dentry->d_parent->d_fsdata; + if (parent_sd->s_type & CONFIGFS_GROUP_LIVE) { + config_item_put(parent_item); + return -EPERM; + } + /* configfs_mkdir() shouldn't have allowed this */ BUG_ON(!subsys->su_group.cg_item.ci_type); subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner; @@ -1535,9 +1712,96 @@ static int configfs_rmdir(struct inode *dir, struct dentry *dentry) return 0; } +static int configfs_rename(struct user_namespace *mnt_userns, + struct inode *old_dir, struct dentry *old_dentry, + struct inode *new_dir, struct dentry *new_dentry, + unsigned int flags) +{ + struct configfs_dirent *sd, *old_parent_sd, *new_parent_sd; + struct dentry *old_parent_dentry, *new_parent_dentry; + struct dentry *committable_group_dentry; + struct config_item *committable_group_item, *item, *new_parent_item; + struct configfs_subsystem *committable_group_subsys; + struct configfs_group_operations *committable_group_ops; + int ret = 0; + + if (flags) + return -EINVAL; + + old_parent_dentry = old_dentry->d_parent; + new_parent_dentry = new_dentry->d_parent; + + sd = old_dentry->d_fsdata; + old_parent_sd = old_dentry->d_parent->d_fsdata; + new_parent_sd = new_dentry->d_parent->d_fsdata; + + if (!old_parent_sd || !new_parent_sd) + return -EPERM; + + /* + * Renaming must always be between a 'pending' and a 'live' group and + * both need to have the same parent. Changing the directory name is + * not allowed. + */ + if (!((old_parent_sd->s_type & CONFIGFS_GROUP_PENDING) && + (new_parent_sd->s_type & CONFIGFS_GROUP_LIVE)) && + !((old_parent_sd->s_type & CONFIGFS_GROUP_LIVE) && + (new_parent_sd->s_type & CONFIGFS_GROUP_PENDING))) + return -EPERM; + + if (old_parent_dentry->d_parent != new_parent_dentry->d_parent) + return -EPERM; + + if (strcmp(old_dentry->d_name.name, new_dentry->d_name.name)) + return -EPERM; + + committable_group_dentry = old_parent_dentry->d_parent; + /* + * Grab a reference to the committable group for the duration of + * this function. + */ + committable_group_item = + configfs_get_config_item(committable_group_dentry); + committable_group_subsys = + to_config_group(committable_group_item)->cg_subsys; + committable_group_ops = committable_group_item->ci_type->ct_group_ops; + + item = sd->s_element; + new_parent_item = new_parent_sd->s_element; + + if (WARN_ON(!is_committable_group(committable_group_item))) { + /* This would be a result of a programming error in configfs. */ + config_item_put(committable_group_item); + return -EPERM; + } + + mutex_lock(&committable_group_subsys->su_mutex); + + if ((old_parent_sd->s_type & CONFIGFS_GROUP_PENDING) && + (new_parent_sd->s_type & CONFIGFS_GROUP_LIVE)) + ret = committable_group_ops->commit_item(item); + else + ret = committable_group_ops->uncommit_item(item); + if (ret) + goto out; + + spin_lock(&configfs_dirent_lock); + new_dentry->d_fsdata = configfs_get(sd); + item->ci_parent = new_parent_item; + list_move(&sd->s_sibling, &new_parent_sd->s_children); + spin_unlock(&configfs_dirent_lock); + +out: + mutex_unlock(&committable_group_subsys->su_mutex); + config_item_put(committable_group_item); + + return ret; +} + const struct inode_operations configfs_dir_inode_operations = { .mkdir = configfs_mkdir, .rmdir = configfs_rmdir, + .rename = configfs_rename, .symlink = configfs_symlink, .unlink = configfs_unlink, .lookup = configfs_lookup, diff --git a/include/linux/configfs.h b/include/linux/configfs.h index e398fb2e83b2..3c3978258a1d 100644 --- a/include/linux/configfs.h +++ b/include/linux/configfs.h @@ -217,6 +217,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); }; -- 2.30.1