usb: gadget: f_mass_storage: forced_eject attribute
authorMaxim Devaev <mdevaev@gmail.com>
Mon, 11 Jul 2022 10:29:57 +0000 (13:29 +0300)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 14 Jul 2022 14:06:42 +0000 (16:06 +0200)
It allows to reset prevent_medium_removal flag and "eject" the image.
This can be useful to free the drive from a hunging host or if the host
continues to use the drive even after unmounting (Linux does this).
It's also a bit like using an unfolded paperclip on an optical drive.

Previously, the undocumented method of sending SIGUSR1 to a special
"file-storage" kernel thread could be used for these purposes,
but when using multiple storages there was no way to distinguish
one from the other, so we had to send a signal to everyone.

Reviewed-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Maxim Devaev <mdevaev@gmail.com>
Link: https://lore.kernel.org/r/20220711102956.19642-1-mdevaev@gmail.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Documentation/ABI/testing/configfs-usb-gadget-mass-storage
Documentation/usb/gadget-testing.rst
Documentation/usb/mass-storage.rst
drivers/usb/gadget/function/f_mass_storage.c
drivers/usb/gadget/function/storage_common.c
drivers/usb/gadget/function/storage_common.h

index c86b63a7bb435be23ad5761d4ca8c6cf98b571ef..d899adb57e81d2510b27ea1d53ff9dd036c72187 100644 (file)
@@ -32,4 +32,10 @@ Description:
                                being a CD-ROM.
                nofua           Flag specifying that FUA flag
                                in SCSI WRITE(10,12)
+               forced_eject    This write-only file is useful only when
+                               the function is active. It causes the backing
+                               file to be forcibly detached from the LUN,
+                               regardless of whether the host has allowed it.
+                               Any non-zero number of bytes written will
+                               result in ejection.
                ===========     ==============================================
index 1c37159fa17104439f198d2b07d35aba0627c14a..2278c9ffb74a7019d0fb262fbc4368cca016c964 100644 (file)
@@ -333,6 +333,12 @@ In each lun directory there are the following attribute files:
                        being a CD-ROM.
        nofua           Flag specifying that FUA flag
                        in SCSI WRITE(10,12)
+       forced_eject    This write-only file is useful only when
+                       the function is active. It causes the backing
+                       file to be forcibly detached from the LUN,
+                       regardless of whether the host has allowed it.
+                       Any non-zero number of bytes written will
+                       result in ejection.
        =============== ==============================================
 
 Testing the MASS STORAGE function
index d181b47c3cb61429fd7bab5a4dd763d224effc98..f399ec6315997b3af87892aeb1f81f332a6cb962 100644 (file)
@@ -181,6 +181,15 @@ sysfs entries
     Reflects the state of nofua flag for given logical unit.  It can
     be read and written.
 
+  - forced_eject
+
+    When written into, it causes the backing file to be forcibly
+    detached from the LUN, regardless of whether the host has allowed
+    it.  The content doesn't matter, any non-zero number of bytes
+    written will result in ejection.
+
+    Can not be read.
+
   Other then those, as usual, the values of module parameters can be
   read from /sys/module/g_mass_storage/parameters/* files.
 
index e884f295504f69d9af641236c42dcb7a4daf9aad..925e99f9775c723d567215258978c824263d7671 100644 (file)
@@ -2651,10 +2651,21 @@ static ssize_t file_store(struct device *dev, struct device_attribute *attr,
        return fsg_store_file(curlun, filesem, buf, count);
 }
 
+static ssize_t forced_eject_store(struct device *dev,
+                                 struct device_attribute *attr,
+                                 const char *buf, size_t count)
+{
+       struct fsg_lun          *curlun = fsg_lun_from_dev(dev);
+       struct rw_semaphore     *filesem = dev_get_drvdata(dev);
+
+       return fsg_store_forced_eject(curlun, filesem, buf, count);
+}
+
 static DEVICE_ATTR_RW(nofua);
 /* mode wil be set in fsg_lun_attr_is_visible() */
 static DEVICE_ATTR(ro, 0, ro_show, ro_store);
 static DEVICE_ATTR(file, 0, file_show, file_store);
+static DEVICE_ATTR_WO(forced_eject);
 
 /****************************** FSG COMMON ******************************/
 
@@ -2808,6 +2819,7 @@ static struct attribute *fsg_lun_dev_attrs[] = {
        &dev_attr_ro.attr,
        &dev_attr_file.attr,
        &dev_attr_nofua.attr,
+       &dev_attr_forced_eject.attr,
        NULL
 };
 
@@ -3221,6 +3233,18 @@ static ssize_t fsg_lun_opts_inquiry_string_store(struct config_item *item,
 
 CONFIGFS_ATTR(fsg_lun_opts_, inquiry_string);
 
+static ssize_t fsg_lun_opts_forced_eject_store(struct config_item *item,
+                                              const char *page, size_t len)
+{
+       struct fsg_lun_opts *opts = to_fsg_lun_opts(item);
+       struct fsg_opts *fsg_opts = to_fsg_opts(opts->group.cg_item.ci_parent);
+
+       return fsg_store_forced_eject(opts->lun, &fsg_opts->common->filesem,
+                                     page, len);
+}
+
+CONFIGFS_ATTR_WO(fsg_lun_opts_, forced_eject);
+
 static struct configfs_attribute *fsg_lun_attrs[] = {
        &fsg_lun_opts_attr_file,
        &fsg_lun_opts_attr_ro,
@@ -3228,6 +3252,7 @@ static struct configfs_attribute *fsg_lun_attrs[] = {
        &fsg_lun_opts_attr_cdrom,
        &fsg_lun_opts_attr_nofua,
        &fsg_lun_opts_attr_inquiry_string,
+       &fsg_lun_opts_attr_forced_eject,
        NULL,
 };
 
index b859a158a4140ef847b8fd2a6095f2af8b682a05..03035dbbe97b87a67e098b047f8461363cfe848c 100644 (file)
@@ -519,4 +519,19 @@ ssize_t fsg_store_inquiry_string(struct fsg_lun *curlun, const char *buf,
 }
 EXPORT_SYMBOL_GPL(fsg_store_inquiry_string);
 
+ssize_t fsg_store_forced_eject(struct fsg_lun *curlun, struct rw_semaphore *filesem,
+                              const char *buf, size_t count)
+{
+       int ret;
+
+       /*
+        * Forcibly detach the backing file from the LUN
+        * regardless of whether the host has allowed it.
+        */
+       curlun->prevent_medium_removal = 0;
+       ret = fsg_store_file(curlun, filesem, "", 0);
+       return ret < 0 ? ret : count;
+}
+EXPORT_SYMBOL_GPL(fsg_store_forced_eject);
+
 MODULE_LICENSE("GPL");
index bdeb1e233fc9b81251f66007f0029f8b51db7f38..0a544a82cbf8504a7a351def7bc0a47da12dd871 100644 (file)
@@ -219,5 +219,7 @@ ssize_t fsg_store_removable(struct fsg_lun *curlun, const char *buf,
                            size_t count);
 ssize_t fsg_store_inquiry_string(struct fsg_lun *curlun, const char *buf,
                                 size_t count);
+ssize_t fsg_store_forced_eject(struct fsg_lun *curlun, struct rw_semaphore *filesem,
+                              const char *buf, size_t count);
 
 #endif /* USB_STORAGE_COMMON_H */