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 X-Spam-Level: X-Spam-Status: No, score=-13.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A843AC4338F for ; Thu, 29 Jul 2021 01:57:38 +0000 (UTC) Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 3FA066101C for ; Thu, 29 Jul 2021 01:57:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 3FA066101C Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kernelnewbies.org Received: from localhost ([::1] helo=shelob.surriel.com) by shelob.surriel.com with esmtp (Exim 4.94.2) (envelope-from ) id 1m8vIk-0007qq-Oq; Wed, 28 Jul 2021 21:57:34 -0400 Received: from mail-ot1-x32d.google.com ([2607:f8b0:4864:20::32d]) by shelob.surriel.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1m8vFn-0002iS-Se for kernelnewbies@kernelnewbies.org; Wed, 28 Jul 2021 21:54:31 -0400 Received: by mail-ot1-x32d.google.com with SMTP id o2-20020a9d22020000b0290462f0ab0800so4173624ota.11 for ; Wed, 28 Jul 2021 18:54:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=iwbk+n8V82DYCRSAvUIXW0Vdrx9nBNIpoXbvhprm1aU=; b=tPAeipBo6QTlClkDwJ3yEjd1cMxq2ZmPpYnmp44H4/gUBSAxf/yrYO46DG0WkVbQhb nJGB7ygsfUTSaieb9OtgRb3vR0Vr74gv2dgLRcvRA2XWcHNK5f3KxKL4ipP/Lu+eCcwO Myp4cTMrYFJHLqv8aW9RpC1nj9hIB3N0pkQ7BD/CZVSXs7FmSVsV8KCNCSnxec6uVf1G OtlEAqu5q6HUW9/7skhJi3N3pcpjUU1PhxoR1XaXdrRtPVo/WPtUbIFNBdQZJFWqWfPM V2No7psrjQTFOddQ+uLiCoNbAdT7p6DE7wflddqHsCVJl2p7nzMv8iY0Fv4OAMe1Edhp v/3Q== 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=iwbk+n8V82DYCRSAvUIXW0Vdrx9nBNIpoXbvhprm1aU=; b=iOfPvN28nvHhsKgbT/WZDIZV+6x6m+VFmIqHcxt+AA9ZKZBxFY3MsmxOPZhIHRX7jW w0JZCl1fB5Z7FREJNOPkyh4QSXLpvAJnOU9mKIWBdLPXyPUcvLwUYJkxijwV4AGD9bHK 9/Qhyit5UalOlbEvmqdTpryvL6w/iPmXN2LujRy9yRyPRF+KsH5oWuY9NNsipzKu9ill CQcq780RXA8XHqmsbSktGgA3EQ8Tkqqa5v0Xi3s66PzKiYNYyiQpS0RD+qmKM6tSQbLm ZoxpIfqsqAu0MIeyYECNNP6dqeZQqgQ4qrW6z8rU5SiA2XzTh5ThESTcZhY83c+Z/kbv HKpQ== X-Gm-Message-State: AOAM532AHzwdIDzzSbtvY8hv0kL5NjKwc7gTadmAK/6fM131/rmtl2GW +kgdci4JT4dkrOWvzlWYfb0= X-Google-Smtp-Source: ABdhPJy4WA8YQHAm65LnVLGVEyhm3WD7rnl+XeLhSw5+oD0I9pdM+KIFG97DgxlRuxCZlCL7DIhMEA== X-Received: by 2002:a05:6830:40c4:: with SMTP id h4mr1928304otu.75.1627523671009; Wed, 28 Jul 2021 18:54:31 -0700 (PDT) Received: from ian.penurio.us ([47.184.51.90]) by smtp.gmail.com with ESMTPSA id c11sm311424otm.37.2021.07.28.18.54.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jul 2021 18:54:30 -0700 (PDT) From: Ian Pilcher To: linux-block@vger.kernel.org, linux-leds@vger.kernel.org Subject: [RFC PATCH 4/8] block: Add block class attributes to manage LED trigger list Date: Wed, 28 Jul 2021 20:53:40 -0500 Message-Id: <20210729015344.3366750-5-arequipeno@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210729015344.3366750-1-arequipeno@gmail.com> References: <20210729015344.3366750-1-arequipeno@gmail.com> MIME-Version: 1.0 Cc: axboe@kernel.dk, Ian Pilcher , linux-kernel@vger.kernel.org, pavel@ucw.cz, kernelnewbies@kernelnewbies.org X-BeenThere: kernelnewbies@kernelnewbies.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: Learn about the Linux kernel List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: kernelnewbies-bounces@kernelnewbies.org * New class attributes - /sys/class/block/led_trigger_{new,list,del} * Add init function - blk_ledtrig_init() - to create the attributes in sysfs. Call blk_ledtrig_init() from genhd_device_init() (in block/genhd.c). * New file - block/blk-ledtrig.h Signed-off-by: Ian Pilcher --- block/blk-ledtrig.c | 147 ++++++++++++++++++++++++++++++++++++++++++++ block/blk-ledtrig.h | 22 +++++++ block/genhd.c | 2 + 3 files changed, 171 insertions(+) create mode 100644 block/blk-ledtrig.h diff --git a/block/blk-ledtrig.c b/block/blk-ledtrig.c index c69ea1539336..6392ab4169f9 100644 --- a/block/blk-ledtrig.c +++ b/block/blk-ledtrig.c @@ -7,11 +7,15 @@ */ #include +#include +#include #include #include #include #include +#include "blk-ledtrig.h" + /* * @@ -201,3 +205,146 @@ int blk_ledtrig_delete(const char *const name) return __blk_ledtrig_delete(name, strlen(name)); } EXPORT_SYMBOL_GPL(blk_ledtrig_delete); + + +/* + * + * Class attributes to manage the trigger list + * + */ + +static ssize_t blk_ledtrig_attr_store(struct class *, struct class_attribute *, + const char *, const size_t); +static ssize_t blk_ledtrig_list_show(struct class *, struct class_attribute *, + char *); + +static struct class_attribute blk_ledtrig_attr_new = + __ATTR(led_trigger_new, 0200, 0, blk_ledtrig_attr_store); + +static struct class_attribute blk_ledtrig_attr_del = + __ATTR(led_trigger_del, 0200, 0, blk_ledtrig_attr_store); + +static struct class_attribute blk_ledtrig_attr_list = + __ATTR(led_trigger_list, 0444, blk_ledtrig_list_show, 0); + +// Returns a pointer to the first non-whitespace character in s (or a pointer +// to the terminating nul). +static const char *blk_ledtrig_skip_whitespace(const char *s) +{ + while (*s != 0 && isspace(*s)) + ++s; + + return s; +} + +// Returns a pointer to the first whitespace character in s (or a pointer to +// the terminating nul), which is effectively a pointer to the position *after* +// the last character in the non-whitespace token at the beginning of s. (s is +// expected to be the result of a previous call to blk_ledtrig_skip_whitespace.) +static const char *blk_ledtrig_find_whitespace(const char *s) +{ + while (*s != 0 && !isspace(*s)) + ++s; + + return s; +} + +static ssize_t blk_ledtrig_attr_store(struct class *const class, + struct class_attribute *const attr, + const char *const buf, + const size_t count) +{ + const char *const name = blk_ledtrig_skip_whitespace(buf); + const char *const endp = blk_ledtrig_find_whitespace(name); + const ptrdiff_t name_len = endp - name; // always >= 0 + int ret; + + if (attr == &blk_ledtrig_attr_new) + ret = __blk_ledtrig_create(name, name_len); + else // attr == &blk_ledtrig_attr_del + ret = __blk_ledtrig_delete(name, name_len); + + if (ret < 0) + return ret; + + // Avoid potential "empty name" error by skipping whitespace + // to next token or terminating nul + return blk_ledtrig_skip_whitespace(endp) - buf; +} + +static ssize_t blk_ledtrig_list_show(struct class *const class, + struct class_attribute *const attr, + char *const buf) +{ + struct list_head *n; + int ret, c = 0; + + ret = mutex_lock_interruptible(&blk_ledtrig_list_mutex); + if (unlikely(ret != 0)) + goto list_exit_return; + + list_for_each(n, &blk_ledtrig_list) { + + struct blk_ledtrig *const t = blk_ledtrig_from_node(n); + int refcount; + + ret = mutex_lock_interruptible(&t->refcount_mutex); + if (unlikely(ret != 0)) + goto list_exit_unlock_list; + + refcount = t->refcount; + + mutex_unlock(&t->refcount_mutex); + + ret = snprintf(buf + c, PAGE_SIZE - c, "%s: %d\n", + t->name, refcount); + if (unlikely(ret < 0)) + goto list_exit_unlock_list; + + c += ret; + if (unlikely(c >= PAGE_SIZE)) { + ret = -EOVERFLOW; + goto list_exit_unlock_list; + } + } + + ret = c; + +list_exit_unlock_list: + mutex_unlock(&blk_ledtrig_list_mutex); +list_exit_return: + return ret; +} + + +/* + * + * Initialization - create the class attributes + * + */ + +void __init blk_ledtrig_init(void) +{ + int ret; + + ret = class_create_file(&block_class, &blk_ledtrig_attr_new); + if (unlikely(ret != 0)) + goto init_error_new; + + ret = class_create_file(&block_class, &blk_ledtrig_attr_del); + if (unlikely(ret != 0)) + goto init_error_del; + + ret = class_create_file(&block_class, &blk_ledtrig_attr_list); + if (unlikely(ret != 0)) + goto init_error_list; + + return; + +init_error_list: + class_remove_file(&block_class, &blk_ledtrig_attr_del); +init_error_del: + class_remove_file(&block_class, &blk_ledtrig_attr_new); +init_error_new: + pr_err("failed to initialize blkdev LED triggers (%d)\n", ret); +} diff --git a/block/blk-ledtrig.h b/block/blk-ledtrig.h new file mode 100644 index 000000000000..894843249deb --- /dev/null +++ b/block/blk-ledtrig.h @@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +/* + * Block device LED triggers + * + * Copyright 2021 Ian Pilcher + */ + +#ifndef _BLOCK_BLK_LEDTRIG_H +#define _BLOCK_BLK_LEDTRIG_H + +#ifdef CONFIG_BLK_LED_TRIGGERS + +void blk_ledtrig_init(void); + +#else // CONFIG_BLK_LED_TRIGGERS + +static inline void blk_ledtrig_init(void) {} + +#endif // CONFIG_BLK_LED_TRIGGERS + +#endif // _BLOCK_BLK_LEDTRIG_H diff --git a/block/genhd.c b/block/genhd.c index af4d2ab4a633..d0b1d8f743ae 100644 --- a/block/genhd.c +++ b/block/genhd.c @@ -26,6 +26,7 @@ #include #include "blk.h" +#include "blk-ledtrig.h" static struct kobject *block_depr; @@ -824,6 +825,7 @@ static int __init genhd_device_init(void) if (unlikely(error)) return error; blk_dev_init(); + blk_ledtrig_init(); register_blkdev(BLOCK_EXT_MAJOR, "blkext"); -- 2.31.1 _______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies