Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1606355pxb; Thu, 4 Mar 2021 16:09:51 -0800 (PST) X-Google-Smtp-Source: ABdhPJyGFPBZx//xVFTrI7mJorMAEEgEPbM/lm8HzihqHBv7oQDlnMlvNcw68kbSc/03SvqfNmJ8 X-Received: by 2002:a5d:9386:: with SMTP id c6mr5601757iol.203.1614902991424; Thu, 04 Mar 2021 16:09:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614902991; cv=none; d=google.com; s=arc-20160816; b=QWUq1XU+LG1CjsLZuUNOxWIgp443NdITQDc2qN1MQtR9m1IqCikkaM+OjI8HnUdZvs 69omNRQMEFVlkYUe7AeyTFKTOkuxehoHSAFvoDGTz4U7rJjhYvCiNtyT7H96QM0EAkq7 3tEYFYPNanCYNVLjX5z43n9+C4ZIguVef5IDa9Yljx+vgCtcOFm9PbrNHdmBHPRqXL+w ecXn7+WTiOqkCIwYhzi0cNuFwMiDtzuJsRe8B2Rz9fkmFAyxUvJsd+L9knXmSZzbLKBz ABqb+1f74mjNHcWLc3Lp36dI80PjfWyeFigvxKzsYZjCLkWKhvxrULMUwhFF3+nVlOfp wpmA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Bs0N+b7IbNFBoZCqiL+fVI6wVQVsEVA2VSnx/V3f4QI=; b=mcF/ETrUo81o0xxfIaqmOkj3s+OjEQkMOCrCbTnuvbfc/MW1HJJpS884qS+D3LCOCZ 0Iu7ORU4Li2QrXH0A+qYEpHkc9JmaMIXfhbwpealyPH/V0EGJqipr6Gc2ZAdNSiRUNm3 mRLvKtvCj1PFirHdUUCbeig2Bm2sOUxXNfuMXLV3qeb7bpyAfbZUWhcKJvv/2leorQW3 TYbuXHCZAFE2c8MNZFIGQ4EnM+zkMu26ZqEmu4pEtkTpn9gyHwu8bWnHyAQE+U5SL4uN Cipen9CvWzh5CGDLo9xtEuTC8LTupkD7SX50+qoLlnVyo8hfL9BQ1T8mNhlUpXi7ed4q 0XwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bgdev-pl.20150623.gappssmtp.com header.s=20150623 header.b=i+Zvg25x; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a5si636469ilp.121.2021.03.04.16.09.37; Thu, 04 Mar 2021 16:09:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@bgdev-pl.20150623.gappssmtp.com header.s=20150623 header.b=i+Zvg25x; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238576AbhCDKcB (ORCPT + 99 others); Thu, 4 Mar 2021 05:32:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39602 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238544AbhCDKb3 (ORCPT ); Thu, 4 Mar 2021 05:31:29 -0500 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F1C75C0613DB for ; Thu, 4 Mar 2021 02:30:10 -0800 (PST) Received: by mail-wr1-x42c.google.com with SMTP id j2so14170993wrx.9 for ; Thu, 04 Mar 2021 02:30:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Bs0N+b7IbNFBoZCqiL+fVI6wVQVsEVA2VSnx/V3f4QI=; b=i+Zvg25xWRRu3Q+3OHqNie0KuSOiMHg6Had86XZY/5pU4nj64JjnzVPBvlXb100DOY 55fH5/xUvZDflCQt+qb9eJwDxrAMVcc7VTXf6LvuhVwMt1UI4r2jr3FJBwU3JDkwv7Wj HHaBp+Fcl37uh7LYBMPHYBrlascVC/XfrppRxQlYnItNZNgWlwOr6g4cybI0E7r+GYzS +3o5/Tdy1Xuc+yp9ld87CGZnogyIBpZpBLyYcUQQQ4UKcKjieo7ocJtNtt4mVUjHveiP 2bV+S5MkgpN53kZtGLSaMR+WRRpxvc3iOwYVGB0xQNFRR04gtt+9fr5q+r/0HBpEtkmV lAwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Bs0N+b7IbNFBoZCqiL+fVI6wVQVsEVA2VSnx/V3f4QI=; b=FWYzD9oDrpqmfnDBlb76VFV5+ZeN5nmx9OoNyDbKnd+0674CNIaEe6G/kCljFXsOUB 0f7HgdxBBO1Ga0KTGaZsyCuD68taY+A9dQWnOyMe2zG2Z4n4WzuyaaRBAZyPjx+Pi4lN BI+BB3XcY3z1HxMvOfIeK1C89Be34VrZXqjvrKMPzz/nB+X9/yJ7wTlyOYRHKI4wf5CR Hg0sRXid9BTSNMfGcx1b/9OJanj/QXiz53SKDMvsE2xgRTmUBQVEJ0NZdx0Tzq/cIWH5 rejk49EV0w0C+XradS70xXWKlq7SC+2Tr/WN1IdroGhyRUVz1c3Oz0Em99lT6DYBausB njdA== X-Gm-Message-State: AOAM531tTL42YzfAoENyulw8+GjJOMv8g8B2jzK1u3c9kRU7tcRMAG26 824QyZ2/lP+CGDQFHFFYaPrwu0AgcFvSoA== X-Received: by 2002:adf:d236:: with SMTP id k22mr3317611wrh.144.1614853809697; Thu, 04 Mar 2021 02:30:09 -0800 (PST) Received: from debian-brgl.home (amarseille-656-1-4-167.w90-8.abo.wanadoo.fr. [90.8.158.167]) by smtp.gmail.com with ESMTPSA id f7sm35501854wre.78.2021.03.04.02.30.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Mar 2021 02:30:09 -0800 (PST) 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 Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 04/12] samples: configfs: add a committable group Date: Thu, 4 Mar 2021 11:24:44 +0100 Message-Id: <20210304102452.21726-5-brgl@bgdev.pl> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20210304102452.21726-1-brgl@bgdev.pl> References: <20210304102452.21726-1-brgl@bgdev.pl> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Bartosz Golaszewski 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. Signed-off-by: Bartosz Golaszewski Acked-by: Linus Walleij --- samples/configfs/configfs_sample.c | 153 +++++++++++++++++++++++++++++ 1 file changed, 153 insertions(+) diff --git a/samples/configfs/configfs_sample.c b/samples/configfs/configfs_sample.c index f9008be7a8a1..9bef74e4369d 100644 --- a/samples/configfs/configfs_sample.c +++ b/samples/configfs/configfs_sample.c @@ -315,6 +315,158 @@ 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; + + if (child->committed) + return -EPERM; + + 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. In this example, the storeme attribute\n" +"becomes 'read-only' once committed.\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 +478,7 @@ static struct configfs_subsystem *example_subsys[] = { &childless_subsys.subsys, &simple_children_subsys, &group_children_subsys, + &committable_children_subsys, NULL, }; -- 2.29.1