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 EB3D6C4338F for ; Thu, 29 Jul 2021 01:56:52 +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 A37A46056C for ; Thu, 29 Jul 2021 01:56:52 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org A37A46056C 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 1m8vI3-00062D-7W; Wed, 28 Jul 2021 21:56:51 -0400 Received: from mail-ot1-x32e.google.com ([2607:f8b0:4864:20::32e]) 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-0002iG-3T for kernelnewbies@kernelnewbies.org; Wed, 28 Jul 2021 21:54:31 -0400 Received: by mail-ot1-x32e.google.com with SMTP id z6-20020a9d24860000b02904d14e47202cso4213797ota.4 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=e4Cil8UyFveunyfhdg9prQXTKI60qw+NHlxrLHZPnMc=; b=W6tx1rbDfbctaNTfj5z+Y97j2ApUcJJ3DCnHPQo+HTvNvZCewBeIobpazgn/Vm5APW GQg1bntSSJRkojml4beEpbasjH6BVCAOc4u2TyrRMGRfnL64xx6b1/jyACpYEYmBnXsg dJ388clXsqSNOdMBnzcW12Uvd2RQJdXcrEbgR41G+WSAOkp8ZZ7OWtikHF9vuYt0RAqU 6lKZZQQukCiccsH6JUxOzQF7oGmimTRlEwA5WhgyOg0vA4pZJWmzhyKFS7gybrF4RTSt JMLwE/qpB/B/j0LGERe+yruohj7FOr1V2j235PewAp5j3W/DELX9EeSG5UpfzzzC/fu6 knhg== 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=e4Cil8UyFveunyfhdg9prQXTKI60qw+NHlxrLHZPnMc=; b=MLZ9oJmEJ6ssW026r8aGW3ishCqav6IfKqh7nv5BR6eWOaFwNwioSohTl5rHvS+BfX I302LVq3wonKX0KQIGTrGkChWeKVy/iw2KefSHcf4png8LqyPuhJR7iYYoI+hph4TeFi 8aYvmwm+/lQ1F07l20m+ZHfNedHq/8E0/P04GuEkGc1I0AeMaKT4tUe/2u3gDKKe3z+b iTLLezFCqwGCPZexZi7/NiiCwRpEC+Uc6N6rqYUaSrQyFkP4yAVHhFyZPxYGDjTxXuJy jhzj4nnHibEXv6yN3G7r3dqasNrnZVyk/DEGdQbpV7CDCM6tNgsZhPagDkXbNQqQoelF ++Ig== X-Gm-Message-State: AOAM533ovT8x0mp2z0xMRyqisVVNeoh9eJegIkrjU27xvGw3NbAYObS5 AVU3GKk+PP/DrKoDOhdiP+s= X-Google-Smtp-Source: ABdhPJw82QLWhuo6vRkom8k9uuRZcO4NPiIluShNkHW3TZtfVFNkZAPiQuXfM+DVHaAQsc7SOZRBCw== X-Received: by 2002:a9d:8ac:: with SMTP id 41mr1901869otf.170.1627523670254; Wed, 28 Jul 2021 18:54:30 -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.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jul 2021 18:54:29 -0700 (PDT) From: Ian Pilcher To: linux-block@vger.kernel.org, linux-leds@vger.kernel.org Subject: [RFC PATCH 3/8] block: Add kernel APIs to create & delete block device LED triggers Date: Wed, 28 Jul 2021 20:53:39 -0500 Message-Id: <20210729015344.3366750-4-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 file - include/linux/blk-ledtrig.h Signed-off-by: Ian Pilcher --- block/blk-ledtrig.c | 152 ++++++++++++++++++++++++++++++++++++ include/linux/blk-ledtrig.h | 19 +++++ 2 files changed, 171 insertions(+) create mode 100644 include/linux/blk-ledtrig.h diff --git a/block/blk-ledtrig.c b/block/blk-ledtrig.c index 345a3b6bdbc6..c69ea1539336 100644 --- a/block/blk-ledtrig.c +++ b/block/blk-ledtrig.c @@ -6,9 +6,11 @@ * Copyright 2021 Ian Pilcher */ +#include #include #include #include +#include /* @@ -49,3 +51,153 @@ static struct blk_ledtrig *blk_ledtrig_find(const char *const name, return NULL; } + + +/* + * + * Create a new trigger + * + */ + +static int __blk_ledtrig_create(const char *const name, const size_t len) +{ + struct blk_ledtrig *t; + int ret; + + if (len == 0) { + pr_warn("empty name specified for blockdev LED trigger\n"); + ret = -EINVAL; + goto create_exit_return; + } + + ret = mutex_lock_interruptible(&blk_ledtrig_list_mutex); + if (unlikely(ret != 0)) + goto create_exit_return; + + if (blk_ledtrig_find(name, len) != NULL) { + pr_warn("blockdev LED trigger named %.*s already exists\n", + (int)len, name); + ret = -EEXIST; + goto create_exit_unlock_list; + } + + t = kzalloc(sizeof(*t) + len + 1, GFP_KERNEL); + if (unlikely(t == NULL)) { + ret = -ENOMEM; + goto create_exit_unlock_list; + } + + memcpy(t->name, name, len); + t->trigger.name = t->name; + mutex_init(&t->refcount_mutex); + + ret = led_trigger_register(&t->trigger); + if (ret != 0) { + if (likely(ret == -EEXIST)) { + pr_warn("LED trigger named %.*s already exists\n", + (int)len, name); + } + goto create_exit_free; + } + + list_add(&t->list_node, &blk_ledtrig_list); + ret = 0; + +create_exit_free: + if (ret != 0) + kfree(t); +create_exit_unlock_list: + mutex_unlock(&blk_ledtrig_list_mutex); +create_exit_return: + return ret; +} + +/** + * blk_ledtrig_create() - creates a new block device LED trigger + * @name: the name of the new trigger + * + * Context: Process context (can sleep). Takes and releases + * @blk_ledtrig_list_mutex. + * + * Return: 0 on success; -@errno on error + */ +int blk_ledtrig_create(const char *const name) +{ + return __blk_ledtrig_create(name, strlen(name)); +} +EXPORT_SYMBOL_GPL(blk_ledtrig_create); + + +/* + * + * Delete a trigger + * + */ + +static int __blk_ledtrig_delete(const char *const name, const size_t len) +{ + struct blk_ledtrig *t; + int ret; + + if (len == 0) { + pr_warn("empty name specified for blockdev LED trigger\n"); + ret = -EINVAL; + goto delete_exit_return; + } + + ret = mutex_lock_interruptible(&blk_ledtrig_list_mutex); + if (unlikely(ret != 0)) + goto delete_exit_return; + + t = blk_ledtrig_find(name, len); + if (t == NULL) { + pr_warn("blockdev LED trigger named %.*s doesn't exist\n", + (int)len, name); + ret = -ENODEV; + goto delete_exit_unlock_list; + } + + ret = mutex_lock_interruptible(&t->refcount_mutex); + if (unlikely(ret != 0)) + goto delete_exit_unlock_list; + + if (WARN_ON(t->refcount < 0)) { + ret = -EBADFD; + goto delete_exit_unlock_refcount; + } + + if (t->refcount > 0) { + pr_warn("blockdev LED trigger %s still in use\n", t->name); + ret = -EBUSY; + goto delete_exit_unlock_refcount; + } + + led_trigger_unregister(&t->trigger); + list_del(&t->list_node); + + ret = 0; + +delete_exit_unlock_refcount: + mutex_unlock(&t->refcount_mutex); + if (ret == 0) + kfree(t); +delete_exit_unlock_list: + mutex_unlock(&blk_ledtrig_list_mutex); +delete_exit_return: + return ret; +} + +/** + * blk_ledtrig_delete() - deletes a block device LED trigger + * @name: the name of the trigger to be deleted + * + * Context: Process context (can sleep). Takes and releases + * @blk_ledtrig_list_mutex and trigger's @refcount_mutex. + * + * Return: 0 on success; -@errno on error + */ +int blk_ledtrig_delete(const char *const name) +{ + return __blk_ledtrig_delete(name, strlen(name)); +} +EXPORT_SYMBOL_GPL(blk_ledtrig_delete); diff --git a/include/linux/blk-ledtrig.h b/include/linux/blk-ledtrig.h new file mode 100644 index 000000000000..6f73635f65ec --- /dev/null +++ b/include/linux/blk-ledtrig.h @@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +/* + * Block device LED triggers + * + * Copyright 2021 Ian Pilcher + */ + +#ifndef _LINUX_BLK_LEDTRIG_H +#define _LINUX_BLK_LEDTRIG_H + +#ifdef CONFIG_BLK_LED_TRIGGERS + +int blk_ledtrig_create(const char *name); +int blk_ledtrig_delete(const char *name); + +#endif // CONFIG_BLK_LED_TRIGGERS + +#endif // _LINUX_BLK_LEDTRIG_H -- 2.31.1 _______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies