From patchwork Fri Oct 8 08:17:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1538256 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20210112.gappssmtp.com header.i=@bgdev-pl.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=2LnsEmQd; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by bilbo.ozlabs.org (Postfix) with ESMTP id 4HQgzl6MZ7z9sP7 for ; Fri, 8 Oct 2021 19:17:55 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233932AbhJHITr (ORCPT ); Fri, 8 Oct 2021 04:19:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46220 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233716AbhJHITo (ORCPT ); Fri, 8 Oct 2021 04:19:44 -0400 Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 62BCFC061764 for ; Fri, 8 Oct 2021 01:17:48 -0700 (PDT) Received: by mail-wr1-x436.google.com with SMTP id v25so27077293wra.2 for ; Fri, 08 Oct 2021 01:17:48 -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=4StP4zG1fEI9tjzWgHEnJNQemAlTt1GkDMlHKHuvYzA=; b=2LnsEmQd+THylAdhLg4go2sLdEBbDFvg3G4CDWZjhJKALT9HG+Bfz8weDwGFUAIGgb /rC22nlJJP9bqckAPzDpHFqzHkrU4oUWbwx1MhmuRaC5alaS4eaSx+lyf6rZNXzjXkvF gSThu6GkIAx0/c/WmRcHrl2dIw2FHIMUoEdqoyNhJK8Ch76kKWHpXbj81GbHf5/XFtY8 3/uhBC/xKLFdc2hFw1hZBb8QuHEEAWcRYJERRsAqDa/719zWleLcyuceCJJjfMC2a9p7 TAi2h6mv5C04evyta9a7tju1r1cBphXGFIKntxIHQYSnnp4QbsWKRYIkNaWlvnMFSUCl 3InA== 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=4StP4zG1fEI9tjzWgHEnJNQemAlTt1GkDMlHKHuvYzA=; b=1ST6qwj8jXdjos6aKPxyi/JvATKQwVhwhjZ/0wSekMJ838MHwlnCR9iq0xG+/7Z1/e m43afhlozXYjj5v3ccfUIL6GFN1gGkKFtuKJW0NjQ/1h4OyU3Qhi954JbAgiPed7h2nQ Rjwq2ZxPyhHa2kAx92Mc71PH+xLp9oYJcfx6T+wVZH+KCatI1yVA/yBejZ4K6wycAJxW tEKfBLZq0doL38dpLhvSthN61teMYsBpqrSYplZFsLGBRuS6h6xkXu+L/g4gXbrG0Gcw ANBzIU5Hn6In6gJr7bMYbHmsQEPetPyWycewmTt355JvzqgF7e3jSDi3VNWWzaKDO4v+ suuw== X-Gm-Message-State: AOAM531QTqIMfNrqeH6QJc2E1QiyInCcwnS0+BElFqmzoHhiCvWjeY2Y rXxQZDRQmZzbhEbljLc8oSNfgw== X-Google-Smtp-Source: ABdhPJy8j/Bag6TeZLPEVgNrkt6F+Q8SlQN5pr2pANzFWXeQZnqrf5dENflqTDTLorY+PZBMllWSGA== X-Received: by 2002:adf:a550:: with SMTP id j16mr2227261wrb.384.1633681066942; Fri, 08 Oct 2021 01:17:46 -0700 (PDT) Received: from debian-brgl.home ([2a01:cb1d:334:ac00:7d50:ff5:f5c1:e225]) by smtp.gmail.com with ESMTPSA id i3sm1759530wrn.34.2021.10.08.01.17.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Oct 2021 01:17:46 -0700 (PDT) From: Bartosz Golaszewski To: Joel Becker , Christoph Hellwig , Shuah Khan , Linus Walleij , Andy Shevchenko , =?utf-8?q?Uwe_Kleine-K?= =?utf-8?q?=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 v7 4/8] samples: configfs: add a committable group Date: Fri, 8 Oct 2021 10:17:35 +0200 Message-Id: <20211008081739.26807-5-brgl@bgdev.pl> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20211008081739.26807-1-brgl@bgdev.pl> References: <20211008081739.26807-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org 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 Acked-by: Andy Shevchenko --- 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 37a657b25d58..f0a1c4d847b1 100644 --- a/samples/configfs/configfs_sample.c +++ b/samples/configfs/configfs_sample.c @@ -313,6 +313,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 @@ -324,6 +476,7 @@ static struct configfs_subsystem *example_subsys[] = { &childless_subsys.subsys, &simple_children_subsys, &group_children_subsys, + &committable_children_subsys, NULL, };