Skip to contentSkip to conversation

usb: gadget: f_mass_storage: forced_eject attribute

PATCH Superseded Maxim Devaev 3 participants 4 messages Jul 6, 2022 – Jul 7, 2022

Subsystems (2)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 usb: gadget: f_mass_storage: forced_eject attribute

It allows to reset prevent_medium_removal flag and "eject" the image.

The patch is a completely alternative implementation of the previously proposed [1], the idea of which was born after the mentioned discussion.

---
 .../testing/configfs-usb-gadget-mass-storage  |  6 +++++
 Documentation/usb/gadget-testing.rst          |  6 +++++
 Documentation/usb/mass-storage.rst            |  9 +++++++
 drivers/usb/gadget/function/f_mass_storage.c  | 25 +++++++++++++++++++
 drivers/usb/gadget/function/storage_common.c  | 11 ++++++++
 drivers/usb/gadget/function/storage_common.h  |  2 ++
 6 files changed, 59 insertions(+)
Documentation/ABI/testing/configfs-usb-gadget-mass-storage +6 -0
index c86b63a7bb43..87859ef40579 100644
@@ -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 flag only makes sence when
+				the function is active. It causes a forced
+				detaching of the backing file from the LUN,
+				regardless of whether the host has allowed it.
+				Any non-zero number of bytes written will
+				result in ejection.
 		===========	==============================================
Documentation/usb/gadget-testing.rst +6 -0
index c18113077889..1481173d8719 100644
@@ -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 flag only makes sence when
+			the function is active. It causes a forced
+			detaching of the backing file 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
Documentation/usb/mass-storage.rst +9 -0
index d181b47c3cb6..f72e59237bce 100644
@@ -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 allows to detach the backing file for given
+    logical unit, regardless of whether the host has allowed it.
+    The content doesn't matter, any non-zero number of bytes will
+    lead the forced eject.
+
+    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.
 
drivers/usb/gadget/function/f_mass_storage.c +25 -0
index 6ad669dde41c..00cac2a38178 100644
@@ -2520,10 +2520,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 ******************************/
 
@@ -2677,6 +2688,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
 };
 
@@ -3090,6 +3102,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,
@@ -3097,6 +3121,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,
 };
 
drivers/usb/gadget/function/storage_common.c +11 -0
index b859a158a414..8cd95bf7831f 100644
@@ -519,4 +519,15 @@ 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;
+
+	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");
drivers/usb/gadget/function/storage_common.h +2 -0
index bdeb1e233fc9..0a544a82cbf8 100644
@@ -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 */
Signature · 1 line
2.37.0
maintainer
#1 lore raw
Quoted 164 lines · Maxim Devaev

It allows to reset prevent_medium_removal flag and "eject" the image.

The patch is a completely alternative implementation of the previously proposed [1], the idea of which was born after the mentioned discussion.

---
 .../testing/configfs-usb-gadget-mass-storage  |  6 +++++
 Documentation/usb/gadget-testing.rst          |  6 +++++
 Documentation/usb/mass-storage.rst            |  9 +++++++
 drivers/usb/gadget/function/f_mass_storage.c  | 25 +++++++++++++++++++
 drivers/usb/gadget/function/storage_common.c  | 11 ++++++++
 drivers/usb/gadget/function/storage_common.h  |  2 ++
 6 files changed, 59 insertions(+)
Documentation/ABI/testing/configfs-usb-gadget-mass-storage +6 -0
index c86b63a7bb43..87859ef40579 100644
@@ -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 flag only makes sence when
+				the function is active. It causes a forced
+				detaching of the backing file from the LUN,
+				regardless of whether the host has allowed it.
+				Any non-zero number of bytes written will
+				result in ejection.
 		===========	==============================================
Documentation/usb/gadget-testing.rst +6 -0
index c18113077889..1481173d8719 100644
@@ -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 flag only makes sence when
+			the function is active. It causes a forced
+			detaching of the backing file 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
Documentation/usb/mass-storage.rst +9 -0
index d181b47c3cb6..f72e59237bce 100644
@@ -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 allows to detach the backing file for given
+    logical unit, regardless of whether the host has allowed it.
+    The content doesn't matter, any non-zero number of bytes will
+    lead the forced eject.
+
+    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.
 
drivers/usb/gadget/function/f_mass_storage.c +25 -0
index 6ad669dde41c..00cac2a38178 100644
@@ -2520,10 +2520,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 ******************************/
 
@@ -2677,6 +2688,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
 };
 
@@ -3090,6 +3102,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,
@@ -3097,6 +3121,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,
 };
 
drivers/usb/gadget/function/storage_common.c +11 -0
index b859a158a414..8cd95bf7831f 100644
@@ -519,4 +519,15 @@ 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;
+
+	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");
drivers/usb/gadget/function/storage_common.h +2 -0
index bdeb1e233fc9..0a544a82cbf8 100644
@@ -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 */
Signature · 1 line
2.37.0

Hi,

This is the friendly patch-bot of Greg Kroah-Hartman. You have sent him a patch that has triggered this response. He used to manually respond to these common problems, but in order to save his sanity (he kept writing the same thing over and over, yet to different people), I was created. Hopefully you will not take offence and will fix the problem in your patch and resubmit it so that it can be accepted into the Linux kernel tree.

You are receiving this message because of the following common error(s) as indicated below:

- This looks like a new version of a previously submitted patch, but you did not list below the --- line any changes from the previous version. Please read the section entitled "The canonical patch format" in the kernel file, Documentation/SubmittingPatches for what needs to be done here to properly describe this.

If you wish to discuss this problem further, or you have questions about how to resolve this issue, please feel free to respond to this email and Greg will reply once he has dug out from the pending patches received from other developers.

thanks,

greg k-h's patch email bot

#2 lore raw
In reply to Maxim Devaev
Quoted 4 lines · Maxim Devaev

It allows to reset prevent_medium_removal flag and "eject" the image.

The patch is a completely alternative implementation of the previously proposed [1], the idea of which was born after the mentioned discussion.

Generally quite good, but the documentation needs some improvement.

Quoted 16 lines
---
 .../testing/configfs-usb-gadget-mass-storage  |  6 +++++
 Documentation/usb/gadget-testing.rst          |  6 +++++
 Documentation/usb/mass-storage.rst            |  9 +++++++
 drivers/usb/gadget/function/f_mass_storage.c  | 25 +++++++++++++++++++
 drivers/usb/gadget/function/storage_common.c  | 11 ++++++++
 drivers/usb/gadget/function/storage_common.h  |  2 ++
 6 files changed, 59 insertions(+)
Documentation/ABI/testing/configfs-usb-gadget-mass-storage +0 -0
index c86b63a7bb43..87859ef40579 100644

Documentation/ABI/testing/configfs-usb-gadget-mass-storage Description: · line 35

Quoted 4 lines
Documentation/ABI/testing/configfs-usb-gadget-mass-storage +1 -0
@@ -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 flag only makes sence when

s/flag/file/

Instead of "only makes sense" (note the spelling), how about "is useful only" or "operates only"?

By the way, what does happen if someone writes to this attribute file before the function is active? Have you tested this?

Documentation/ABI/testing/configfs-usb-gadget-mass-storage Description: · line 37

Quoted 2 lines
Documentation/ABI/testing/configfs-usb-gadget-mass-storage +2 -0
+				the function is active. It causes a forced
+				detaching of the backing file from the LUN,

Slightly improved wording: "It causes the backing file to be forcibly detached from the LUN".

Documentation/ABI/testing/configfs-usb-gadget-mass-storage Description: · line 40

Quoted 3 lines
Documentation/ABI/testing/configfs-usb-gadget-mass-storage +3 -0
+				regardless of whether the host has allowed it.
+				Any non-zero number of bytes written will
+				result in ejection.

Doesn't a zero-byte write also work?

The same suggested changes apply to the documentation added below.

Quoted 23 lines
Documentation/ABI/testing/configfs-usb-gadget-mass-storage +0 -0
 		===========	==============================================
Documentation/usb/gadget-testing.rst +6 -0
index c18113077889..1481173d8719 100644
@@ -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 flag only makes sence when
+			the function is active. It causes a forced
+			detaching of the backing file 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
Documentation/usb/mass-storage.rst +0 -0
index d181b47c3cb6..f72e59237bce 100644

Documentation/usb/mass-storage.rst sysfs entries · line 186

Quoted 6 lines
Documentation/usb/mass-storage.rst +3 -0
@@ -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 allows to detach the backing file for given

This should be phrased in the same way as above. The file doesn't "allow" anything; it _causes_ the backing file to be detached.

Documentation/usb/mass-storage.rst sysfs entries · line 187

Quoted 1 line
Documentation/usb/mass-storage.rst +1 -0
+    logical unit, regardless of whether the host has allowed it.

Same as above, use "LUN" here instead of "logical unit".

Alan Stern

Quoted 104 lines
Documentation/usb/mass-storage.rst +5 -0
+    The content doesn't matter, any non-zero number of bytes will
+    lead the forced eject.
+
+    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.
 
drivers/usb/gadget/function/f_mass_storage.c +25 -0
index 6ad669dde41c..00cac2a38178 100644
@@ -2520,10 +2520,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 ******************************/
 
@@ -2677,6 +2688,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
 };
 
@@ -3090,6 +3102,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,
@@ -3097,6 +3121,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,
 };
 
drivers/usb/gadget/function/storage_common.c +11 -0
index b859a158a414..8cd95bf7831f 100644
@@ -519,4 +519,15 @@ 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;
+
+	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");
drivers/usb/gadget/function/storage_common.h +2 -0
index bdeb1e233fc9..0a544a82cbf8 100644
@@ -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 */
Signature · 1 line
2.37.0
#3 lore raw

В Thu, 7 Jul 2022 10:57:42 -0400

Quoted 7 lines · Alan Stern
Quoted 4 lines · Maxim Devaev

It allows to reset prevent_medium_removal flag and "eject" the image.

The patch is a completely alternative implementation of the previously proposed [1], the idea of which was born after the mentioned discussion.

Generally quite good, but the documentation needs some improvement.

Thanks for your help, I've fixed it and submitted v3 patch.

Quoted 2 lines

By the way, what does happen if someone writes to this attribute file before the function is active? Have you tested this?

Yes. This simply detaches the backing file, as if you had written an empty string to the "file" attribute. This is literally the same code, just resetting a flag that, with an inactive configuration, cannot even be set.

Quoted 1 line

Doesn't a zero-byte write also work?

No, the same as with the rest of the configfs attributes.