From: Ian Pilcher <arequipeno@gmail.com>
To: Greg KH <gregkh@linuxfoundation.org>
Cc: axboe@kernel.dk, pavel@ucw.cz, linux-leds@vger.kernel.org,
linux-block@vger.kernel.org, kabel@kernel.org
Subject: Re: [PATCH 15/18] ledtrig-blkdev: Add sysfs attributes to [un]link LEDs & devices
Date: Sat, 4 Sep 2021 17:35:29 -0500 [thread overview]
Message-ID: <2791db8c-9049-adbe-604e-ff010aef03fa@gmail.com> (raw)
In-Reply-To: <YTMLVrCokErmYxEU@kroah.com>
On 9/4/21 12:59 AM, Greg KH wrote:
> DEVICE_ATTR_RO()? Or something like that? Do not use __ATTR() for
> device attributes if at all possible, worst case, use DEVICE_ATTR()
> here.
For some reason, it didn't click until now that these are device
attributes (because I was focused on the fact that I was working on the
LED trigger).
DEVICE_ATTR*() it is.
> And the mode settings are odd, are you sure you want that?
Yes. These are write-only attributes.
>> + if (name_len == 0) {
>> + pr_info("blkdev LED: empty block device name\n");
>
> Looks like debugging code, please remove.
It's really more of an error message for the system administrator. So
as with my earlier note, dev_info() would be my preference.
> And how can this ever happen?
The blkdev_skip_space() and blkdev_find_space() calls effectively find
the first non-whitespace token in the buffer (disk_name) and its length
(name_len). If the buffer only contains whitespace (e.g. echo > $ATTR),
then name_len will be 0.
>
>> + return -EINVAL;
>> + }
>> +
>> + if (attr == &ledtrig_blkdev_attr_del) {
>> + blkdev_disk_delete(led, disk_name, name_len);
>> + } else { /* attr == &ledtrig_blkdev_attr_add */
>> + ret = blkdev_disk_add(led, disk_name, name_len);
>
> Why do you have a single attribute callback that does two totally
> different things? Just have 2 different callback functions please, it
> makes things much easier to review and maintain over time.
Hmmm. All of the "real work" is done in blkdev_disk_delete() and
blkdev_disk_add(). The store function's only purpose is to parse the
token(s) from the buffer, and that logic is exactly the same for the
two different attributes.
So it's a choice between:
> static ssize_t blkdev_add_or_del(struct device *const dev,
> struct device_attribute *const attr,
> const char *const buf, const size_t count)
> {
> struct ledtrig_blkdev_led *const led = led_trigger_get_drvdata(dev);
> const char *const disk_name = blkdev_skip_space(buf);
> const char *const endp = blkdev_find_space(disk_name);
> const ptrdiff_t name_len = endp - disk_name; /* always >= 0 */
> int ret;
>
> if (name_len == 0) {
> pr_info("blkdev LED: empty block device name\n");
> return -EINVAL;
> }
>
> if (attr == &ledtrig_blkdev_attr_del) {
> blkdev_disk_delete(led, disk_name, name_len);
> } else { /* attr == &ledtrig_blkdev_attr_add */
> ret = blkdev_disk_add(led, disk_name, name_len);
> if (ret != 0)
> return ret;
> }
>
> /*
> * Consume everything up to the next non-whitespace token (or the end
> * of the input). Avoids "empty block device name" error if there is
> * whitespace (such as a newline) after the last token.
> */
> return blkdev_skip_space(endp) - buf;
> }
Or:
> static ssize_t blkdev_add(struct device *const dev,
> struct device_attribute *const attr,
> const char *const buf, const size_t count)
> {
> struct ledtrig_blkdev_led *const led = led_trigger_get_drvdata(dev);
> const char *const disk_name = blkdev_skip_space(buf);
> const char *const endp = blkdev_find_space(disk_name);
> const ptrdiff_t name_len = endp - disk_name; /* always >= 0 */
> int ret;
>
> if (name_len == 0) {
> pr_info("blkdev LED: empty block device name\n");
> return -EINVAL;
> }
>
> ret = blkdev_disk_add(led, disk_name, name_len);
> if (ret != 0)
> return ret;
>
> /*
> * Consume everything up to the next non-whitespace token (or the end
> * of the input). Avoids "empty block device name" error if there is
> * whitespace (such as a newline) after the last token.
> */
> return blkdev_skip_space(endp) - buf;
> }
>
>
> static ssize_t blkdev_del(struct device *const dev,
> struct device_attribute *const attr,
> const char *const buf, const size_t count)
> {
> struct ledtrig_blkdev_led *const led = led_trigger_get_drvdata(dev);
> const char *const disk_name = blkdev_skip_space(buf);
> const char *const endp = blkdev_find_space(disk_name);
> const ptrdiff_t name_len = endp - disk_name; /* always >= 0 */
> int ret;
>
> if (name_len == 0) {
> pr_info("blkdev LED: empty block device name\n");
> return -EINVAL;
> }
>
> blkdev_disk_delete(led, disk_name, name_len);
>
> /* See comment in blkdev_add() */
> return blkdev_skip_space(endp) - buf;
> }
Maybe the right thing to do is to simply add a comment to clarify the
separation (and maybe rename the function as well)?
Thanks!
--
========================================================================
In Soviet Russia, Google searches you!
========================================================================
next prev parent reply other threads:[~2021-09-04 22:35 UTC|newest]
Thread overview: 43+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-09-03 20:45 [PATCH 00/18] Introduce block device LED trigger Ian Pilcher
2021-09-03 20:45 ` [PATCH 01/18] docs: Add block device (blkdev) LED trigger documentation Ian Pilcher
2021-09-04 6:29 ` Pavel Machek
2021-09-05 14:49 ` Ian Pilcher
2021-09-05 18:42 ` Pavel Machek
2021-09-05 23:13 ` Ian Pilcher
2021-09-03 20:45 ` [PATCH 02/18] ledtrig-blkdev: Add build infra for block device LED trigger Ian Pilcher
2021-09-03 20:45 ` [PATCH 03/18] ledtrig-blkdev: Add function placeholders needed by block changes Ian Pilcher
2021-09-04 16:57 ` kernel test robot
2021-09-03 20:45 ` [PATCH 04/18] block: Add block device LED trigger integrations Ian Pilcher
2021-09-03 20:45 ` [PATCH 05/18] ledtrig-blkdev: Implement functions called from block subsystem Ian Pilcher
2021-09-03 20:45 ` [PATCH 06/18] ledtrig-blkdev: Add function to get gendisk by name Ian Pilcher
2021-09-03 20:45 ` [PATCH 07/18] ledtrig-blkdev: Add constants, data types, and global variables Ian Pilcher
2021-09-03 20:45 ` [PATCH 08/18] ledtrig-blkdev: Add miscellaneous helper functions Ian Pilcher
2021-09-04 6:00 ` Greg KH
2021-09-04 22:43 ` Ian Pilcher
2021-09-03 20:45 ` [PATCH 09/18] ledtrig-blkdev: Periodically check devices for activity & blink LEDs Ian Pilcher
2021-09-04 6:01 ` Greg KH
2021-09-05 14:39 ` Ian Pilcher
2021-09-05 14:51 ` Greg KH
2021-09-05 14:56 ` Ian Pilcher
2021-09-05 15:12 ` Greg KH
2021-09-05 16:55 ` Eric Biggers
2021-09-03 20:45 ` [PATCH 10/18] ledtrig-blkdev: Add function to associate the trigger with an LED Ian Pilcher
2021-09-03 20:45 ` [PATCH 11/18] ledtrig-blkdev: Add function to associate a device " Ian Pilcher
2021-09-03 20:45 ` [PATCH 12/18] ledtrig-blkdev: Add function to remove LED/device association Ian Pilcher
2021-09-03 20:45 ` [PATCH 13/18] ledtrig-blkdev: Add function to disassociate a device from all LEDs Ian Pilcher
2021-09-03 20:45 ` [PATCH 14/18] ledtrig-blkdev: Add function to disassociate an LED from the trigger Ian Pilcher
2021-09-03 20:45 ` [PATCH 15/18] ledtrig-blkdev: Add sysfs attributes to [un]link LEDs & devices Ian Pilcher
2021-09-04 5:57 ` Greg KH
2021-09-04 21:28 ` Ian Pilcher
2021-09-04 5:59 ` Greg KH
2021-09-04 22:35 ` Ian Pilcher [this message]
2021-09-05 14:51 ` Greg KH
2021-09-05 15:33 ` Ian Pilcher
2021-09-05 16:43 ` Greg KH
2021-09-03 20:45 ` [PATCH 16/18] ledtrig-blkdev: Add blink_time & interval sysfs attributes Ian Pilcher
2021-09-03 20:45 ` [PATCH 17/18] ledtrig-blkdev: Add mode (read/write/rw) sysfs attributue Ian Pilcher
2021-09-04 5:57 ` Greg KH
2021-09-04 21:01 ` Ian Pilcher
2021-09-05 14:50 ` Greg KH
2021-09-03 20:45 ` [PATCH 18/18] ledtrig-blkdev: Add initialization & exit functions Ian Pilcher
2021-09-04 6:35 ` [PATCH 00/18] Introduce block device LED trigger Pavel Machek
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=2791db8c-9049-adbe-604e-ff010aef03fa@gmail.com \
--to=arequipeno@gmail.com \
--cc=axboe@kernel.dk \
--cc=gregkh@linuxfoundation.org \
--cc=kabel@kernel.org \
--cc=linux-block@vger.kernel.org \
--cc=linux-leds@vger.kernel.org \
--cc=pavel@ucw.cz \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).