Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp5728332pxv; Wed, 28 Jul 2021 18:56:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyQZKBj0MXlmf7OJ9MnVFbO+/Ym+xy1myL0dIWKHgU0DzwDSoGMCWvA2STStV/nPIhKLAUs X-Received: by 2002:a17:906:3888:: with SMTP id q8mr2233108ejd.269.1627523784074; Wed, 28 Jul 2021 18:56:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627523784; cv=none; d=google.com; s=arc-20160816; b=N+y97Dih2afKDRkCmKyN42Sjcbs9PVJdjZLD40l11EmTcmqUp1PBGJXERy3nhDOmlm S/YV+GzjID+6N4e45QYwBSMXA2jnLfYwGc4ILIrrvca5RWShvGxSIOxKvt27Kq5P9idR 2qyaWpxBJEq+/bI7khXwPMUWOwJ/MI+fRzTTOuqugCoqGcBz3AHOqiYcdrND25fbkvH2 dPY4sKvMlOwMpjW/I3sUuSQb0JtyELldxZcUOfvrlaQqvjgKYdrtH2VbBKLTbXAgPLut tlT+GeE9ZAucnP788ZYLhooP4viEYftPI3gpizcXu2e3AHrh4o7Mspti/kCWWTS3gWKx bgGg== 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=e4Cil8UyFveunyfhdg9prQXTKI60qw+NHlxrLHZPnMc=; b=YlVqjQQtfKKZsMUoMbL6fS1W7LuwxNoWV/2PtKRNSUPqz7B5R/u53nys1Yxg1yBE9i 25f3xUDMao5UgKp4qaIxmodHynqT87LuLhygQeL4Cxkmvh0esAq3Y6n/OviTio2uHA5R r/wrq3mg1kX1iFhn7Q0Rj/4XCdYc4zYOLuDvqhJyfyrRkoc93iiajqh3XBzYN7AAJpsD wD+NoOaf4VaN+WXGCpDRvOREGkG8+DBmO2O97aPEE2JA4DXJUdYEHMVjKGTxWEgIuLfh GsgujvO9VutS/GnDIo5UXkS/4ucS6U6Ud/2AjYr1fcXlXzaOZKBd+9Ln2L6mqnYwYFdI iKmw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=W6tx1rbD; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l1si1239476edw.438.2021.07.28.18.56.01; Wed, 28 Jul 2021 18:56:24 -0700 (PDT) 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=@gmail.com header.s=20161025 header.b=W6tx1rbD; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233481AbhG2Byj (ORCPT + 99 others); Wed, 28 Jul 2021 21:54:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54004 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233300AbhG2Bye (ORCPT ); Wed, 28 Jul 2021 21:54:34 -0400 Received: from mail-ot1-x32d.google.com (mail-ot1-x32d.google.com [IPv6:2607:f8b0:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D6307C061765; Wed, 28 Jul 2021 18:54:30 -0700 (PDT) Received: by mail-ot1-x32d.google.com with SMTP id 48-20020a9d0bb30000b02904cd671b911bso4219981oth.1; Wed, 28 Jul 2021 18:54:30 -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=MnIk0mJNnqa+nLLCN1b5qa4dm8MzS49X290ZiARqX4LdNySRzvZRFVOOIQGQkqAF5N 481XwCGiHRDJJomgoDXE9HgEkbS4sVrB77S/7PRkPnAvCK7Wa+LIHOPL9ZFz/yt/63oS YfD9w0ltVf1q+viJe43NXon8e35cxoLAiEYWDP6LVLooohIRyhdzm/wNAWJHgSrJrCbF p0/PRcrlp5GEMnZeqPIkiRp8kQBIxVB3EozqQtRLX7kds2g9IsfOOcPXJp+oeTSw0W/K ScBYU1QbYNWd59Z947bugFzi/s5uX4pKrqqF7H9wI35yc1y4v/SfrdaSzsx3ZihLyNts ZqnQ== X-Gm-Message-State: AOAM533+CK6xZ7tviVionNlceqwRJZGr0kEHUeG1l8HvmBp3AhQ8aNLg beqYG67TgLy4Kk8Ft/W86MnM02YN39fliZF5 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 Cc: axboe@kernel.dk, pavel@ucw.cz, linux-kernel@vger.kernel.org, kernelnewbies@kernelnewbies.org, Ian Pilcher 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 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.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