Message ID | 20210916202127.1216994-1-arequipeno@gmail.com |
---|---|
Headers | show |
Series | Introduce block device LED trigger | expand |
On Thu, Sep 16, 2021 at 03:21:26PM -0500, Ian Pilcher wrote: > +What: /sys/class/leds/<led>/link_device > +Date: September 2021 > +Contact: Ian Pilcher <arequipeno@gmail.com> > +Description: > + Associate a block device with this LED by writing the path to > + the device special file (e.g. /dev/sda) to this attribute. > + Symbolic links are followed. Optionally, the leading "/dev/" > + may be omitted. No, please don't follow symlinks, stick with kernel names here, otherwise you have a mismatch between that and the list of devices in this file: > +What: /sys/class/leds/<led>/linked_devices thanks, greg k-h
Combining 2 related threads ... On 9/17/21 01:19, Greg KH wrote: > On Thu, Sep 16, 2021 at 03:21:26PM -0500, Ian Pilcher wrote: >> +What: /sys/class/leds/<led>/link_device >> +Date: September 2021 >> +Contact: Ian Pilcher <arequipeno@gmail.com> >> +Description: >> + Associate a block device with this LED by writing the path to >> + the device special file (e.g. /dev/sda) to this attribute. >> + Symbolic links are followed. Optionally, the leading "/dev/" >> + may be omitted. > > No, please don't follow symlinks, stick with kernel names here, > otherwise you have a mismatch between that and the list of devices in > this file: > >> +What: /sys/class/leds/<led>/linked_devices I did update the documentation to mention that fact. Following symlinks is the behavior of blkdev_get_by_path(), not some- thing that my code is doing. As far as using kernel names, that would be my preference, but I simply don't know of any way to do so with the existing block API. To my knowledge, there simply isn't anything like a blkdev_get_by_name() API. This the reason that I added the "retry" logic to led_bdev_get(). It doesn't prevent the system administrator from using a symbolic link (or an oddly named special file), but it does make an unqualified name like "sda" work if the expected special file exists in /dev. However ... On 9/17/21 00:53, Christoph Hellwig wrote: > On Thu, Sep 16, 2021 at 03:21:27PM -0500, Ian Pilcher wrote: >> +static struct block_device *led_bdev_get(const char *const buf, >> + const size_t count, fmode_t mode) >> +{ >> + static const char dev[] = "/dev/"; >> + struct block_device *bdev; >> + char *dev_path, *path; >> + >> + /* sizeof(dev) includes terminating null */ >> + dev_path = kmalloc(sizeof(dev) + count, GFP_KERNEL); >> + if (dev_path == NULL) >> + return ERR_PTR(-ENOMEM); >> + >> + /* sizeof(dev) - 1 is compile-time equivalent of strlen(dev) */ >> + memcpy(dev_path, dev, sizeof(dev) - 1); >> + path = dev_path + sizeof(dev) - 1; >> + memcpy(path, buf, count + 1); /* include terminating null */ >> + strim(path); >> + >> +try_blkdev_get: >> + bdev = blkdev_get_by_path(path, mode, THIS_MODULE); >> + if (IS_ERR(bdev) && PTR_ERR(bdev) == -ENOENT && path != dev_path) { >> + path = dev_path; >> + goto try_blkdev_get; >> + } >> + >> + kfree(dev_path); >> + return bdev; > > Please just required the user to put in the whole path and remove all > this garbage. There is no need to build your own broken wrappers around > the VFS path resolution. Please be specific about what is broken. If you see an actual bug in the code, please tell me what it is. If (as I suspect) you disagree with the basic idea of retrying with "/dev/" prepended to the supplied path, please say that. Honestly, I wasn't particularly enthusiastic about it in the first place; it feels like something that should be done in user space. I wouldn't have included it if I didn't have to make a writable copy of the buffer anyway, in order to trim a trailing newline. I can certainly remove the re-check logic. The end result will be an API that is slightly less "user friendly" in return for saving a bit of pointer arithmetic and a 5-byte memcpy(). -- ======================================================================== In Soviet Russia, Google searches you! ========================================================================
On Fri, Sep 17, 2021 at 03:46:55PM -0500, Ian Pilcher wrote: > Combining 2 related threads ... > > On 9/17/21 01:19, Greg KH wrote: > > On Thu, Sep 16, 2021 at 03:21:26PM -0500, Ian Pilcher wrote: > > > +What: /sys/class/leds/<led>/link_device > > > +Date: September 2021 > > > +Contact: Ian Pilcher <arequipeno@gmail.com> > > > +Description: > > > + Associate a block device with this LED by writing the path to > > > + the device special file (e.g. /dev/sda) to this attribute. > > > + Symbolic links are followed. Optionally, the leading "/dev/" > > > + may be omitted. > > > > No, please don't follow symlinks, stick with kernel names here, > > otherwise you have a mismatch between that and the list of devices in > > this file: > > > > > +What: /sys/class/leds/<led>/linked_devices > > I did update the documentation to mention that fact. > > Following symlinks is the behavior of blkdev_get_by_path(), not some- > thing that my code is doing. > > As far as using kernel names, that would be my preference, but I simply > don't know of any way to do so with the existing block API. To my > knowledge, there simply isn't anything like a blkdev_get_by_name() API. > > This the reason that I added the "retry" logic to led_bdev_get(). It > doesn't prevent the system administrator from using a symbolic link (or > an oddly named special file), but it does make an unqualified name like > "sda" work if the expected special file exists in /dev. > > However ... > > On 9/17/21 00:53, Christoph Hellwig wrote: > > On Thu, Sep 16, 2021 at 03:21:27PM -0500, Ian Pilcher wrote: > > > +static struct block_device *led_bdev_get(const char *const buf, > > > + const size_t count, fmode_t mode) > > > +{ > > > + static const char dev[] = "/dev/"; > > > + struct block_device *bdev; > > > + char *dev_path, *path; > > > + > > > + /* sizeof(dev) includes terminating null */ > > > + dev_path = kmalloc(sizeof(dev) + count, GFP_KERNEL); > > > + if (dev_path == NULL) > > > + return ERR_PTR(-ENOMEM); > > > + > > > + /* sizeof(dev) - 1 is compile-time equivalent of strlen(dev) */ > > > + memcpy(dev_path, dev, sizeof(dev) - 1); > > > + path = dev_path + sizeof(dev) - 1; > > > + memcpy(path, buf, count + 1); /* include terminating null */ > > > + strim(path); > > > + > > > +try_blkdev_get: > > > + bdev = blkdev_get_by_path(path, mode, THIS_MODULE); > > > + if (IS_ERR(bdev) && PTR_ERR(bdev) == -ENOENT && path != dev_path) { > > > + path = dev_path; > > > + goto try_blkdev_get; > > > + } > > > + > > > + kfree(dev_path); > > > + return bdev; > > > > Please just required the user to put in the whole path and remove all > > this garbage. There is no need to build your own broken wrappers around > > the VFS path resolution. > > Please be specific about what is broken. > > If you see an actual bug in the code, please tell me what it is. > > If (as I suspect) you disagree with the basic idea of retrying with > "/dev/" prepended to the supplied path, please say that. > > Honestly, I wasn't particularly enthusiastic about it in the first > place; it feels like something that should be done in user space. I > wouldn't have included it if I didn't have to make a writable copy of > the buffer anyway, in order to trim a trailing newline. > > I can certainly remove the re-check logic. The end result will be an > API that is slightly less "user friendly" in return for saving a bit of > pointer arithmetic and a 5-byte memcpy(). Just use the kernel block device name and that way you do not have to parse anything as it is unique and no paths are having to be followed. That's the way that other LED apis are working, right? thanks, greg k-h
On 9/18/21 02:07, Greg KH wrote: > On Fri, Sep 17, 2021 at 03:46:55PM -0500, Ian Pilcher wrote: >> As far as using kernel names, that would be my preference, but I simply >> don't know of any way to do so with the existing block API. To my >> knowledge, there simply isn't anything like a blkdev_get_by_name() API. ... > Just use the kernel block device name and that way you do not have to > parse anything as it is unique and no paths are having to be followed. > > That's the way that other LED apis are working, right? Greg - There are 2 existing LED triggers that have similar functionality (i.e. they allow LEDs to be "associated" with devices by name), and they both use subsystem-specific APIs to "resolve" those names to the actual kernel objects on which they operate. * The *netdev* trigger uses dev_get_by_name(), which is specific to network devices (despite its name). * The *tty* trigger uses tty_dev_name_to_number() and tty_kopen_shared(). As I've been saying, I simply don't know of any similar API for block devices. The block API provides blkdev_get_by_path(), which I am using, and blkdev_get_by_dev(), which takes the device number (dev_t). If you know of an API that will allow me to resolve a block device (or dev_t) by its kernel name, please share that information. Thanks! -- ======================================================================== In Soviet Russia, Google searches you! ========================================================================
On Sat, Sep 18, 2021 at 09:07:54AM +0200, Greg KH wrote: > > Honestly, I wasn't particularly enthusiastic about it in the first > > place; it feels like something that should be done in user space. I > > wouldn't have included it if I didn't have to make a writable copy of > > the buffer anyway, in order to trim a trailing newline. > > > > I can certainly remove the re-check logic. The end result will be an > > API that is slightly less "user friendly" in return for saving a bit of > > pointer arithmetic and a 5-byte memcpy(). > > Just use the kernel block device name and that way you do not have to > parse anything as it is unique and no paths are having to be followed. > > That's the way that other LED apis are working, right? The "kernel block device name" is the a block device special path that a normal VFS path lookup is done on. This is the preferred block device API used by everyone. And yes, this includes resolving symlinks. The only other API is by dev_t, but it is highly discouraged and should really not grow any new users.
On Mon, 20 Sep 2021 07:43:52 +0100 Christoph Hellwig <hch@infradead.org> wrote: > On Sat, Sep 18, 2021 at 09:07:54AM +0200, Greg KH wrote: > > > Honestly, I wasn't particularly enthusiastic about it in the first > > > place; it feels like something that should be done in user space. > > > I wouldn't have included it if I didn't have to make a writable > > > copy of the buffer anyway, in order to trim a trailing newline. > > > > > > I can certainly remove the re-check logic. The end result will > > > be an API that is slightly less "user friendly" in return for > > > saving a bit of pointer arithmetic and a 5-byte memcpy(). > > > > Just use the kernel block device name and that way you do not have > > to parse anything as it is unique and no paths are having to be > > followed. > > > > That's the way that other LED apis are working, right? > > The "kernel block device name" is the a block device special path > that a normal VFS path lookup is done on. This is the preferred block > device API used by everyone. And yes, this includes resolving > symlinks. The only other API is by dev_t, but it is highly > discouraged and should really not grow any new users. Christoph, /sys/class/block lists block devices' kernel object names. I don't understand why can't blk API provide a function returns a block device given such name as seen in /sys/class/block directory. Can you elaborate on this? It seems really strange to me to not be able to do cd /sys/class/leds/<LED> echo blkdev >trigger echo sda1 >block_device and instead having to do (as the last command) echo /dev/sda1 >block_device And whas should we show when /dev/sda1 is paried to the trigger, and userspace reads the block_device sysfs file? Should we show the full path which was given when pairing, even if it may not be valid anymore? (Such as when the device file is removed from /dev.) Marek