blob: c4bc18961fcce247f18f5dac0945de43140e3336 [file] [log] [blame]
From dq-thang@jinso.co.jp Sun Feb 17 18:40:19 2013
From: Do Quang Thang <dq-thang@jinso.co.jp>
Date: Mon, 18 Feb 2013 11:40:10 +0900
Subject: usb: gadget: mass_storage: make "file" and "ro" read only in some cases
To: Greg KH <greg@kroah.com>
Cc: ltsi-dev@lists.linuxfoundation.org
Message-ID: <1361155210-5674-1-git-send-email-dq-thang@jinso.co.jp>
From: Michal Nazarewicz <mina86@mina86.com>
The “file” sysfs entry for LUNs was writable even for non-removable
LUNs and the fsg_store_file() function did not check whether LUN is
removable or not. This made it possible to change or even close
LUN's backing file.
The same is true for “ro” sysfs entry and LUNs simulating CD-ROM.
For those LUNs, the file should not be writable.
This commit introduces two new device_attribute structures for those
two special cases so that the file/ro sysfs entries are made
non-writable when not desired.
Signed-off-by: Michal Nazarewicz <mina86@mina86.com>
Signed-off-by: Felipe Balbi <balbi@ti.com>
(cherry picked from commit 48a31af74404e6460eabca410bf0b4a625bfd372)
Signed-off-by: Do Quang Thang <dq-thang@jinso.co.jp>
---
drivers/usb/gadget/f_mass_storage.c | 26 +++++++++++++++++++++-----
drivers/usb/gadget/storage_common.c | 1 -
2 files changed, 21 insertions(+), 6 deletions(-)
--- a/drivers/usb/gadget/f_mass_storage.c
+++ b/drivers/usb/gadget/f_mass_storage.c
@@ -2611,11 +2611,15 @@ static int fsg_main_thread(void *common_
/*************************** DEVICE ATTRIBUTES ***************************/
-/* Write permission is checked per LUN in store_*() functions. */
static DEVICE_ATTR(ro, 0644, fsg_show_ro, fsg_store_ro);
static DEVICE_ATTR(nofua, 0644, fsg_show_nofua, fsg_store_nofua);
static DEVICE_ATTR(file, 0644, fsg_show_file, fsg_store_file);
+static struct device_attribute dev_attr_ro_cdrom =
+ __ATTR(ro, 0444, fsg_show_ro, NULL);
+static struct device_attribute dev_attr_file_nonremovable =
+ __ATTR(file, 0444, fsg_show_file, NULL);
+
/****************************** FSG COMMON ******************************/
@@ -2726,10 +2730,16 @@ static struct fsg_common *fsg_common_ini
goto error_release;
}
- rc = device_create_file(&curlun->dev, &dev_attr_ro);
+ rc = device_create_file(&curlun->dev,
+ curlun->cdrom
+ ? &dev_attr_ro_cdrom
+ : &dev_attr_ro);
if (rc)
goto error_luns;
- rc = device_create_file(&curlun->dev, &dev_attr_file);
+ rc = device_create_file(&curlun->dev,
+ curlun->removable
+ ? &dev_attr_file
+ : &dev_attr_file_nonremovable);
if (rc)
goto error_luns;
rc = device_create_file(&curlun->dev, &dev_attr_nofua);
@@ -2864,8 +2874,14 @@ static void fsg_common_release(struct kr
/* In error recovery common->nluns may be zero. */
for (; i; --i, ++lun) {
device_remove_file(&lun->dev, &dev_attr_nofua);
- device_remove_file(&lun->dev, &dev_attr_ro);
- device_remove_file(&lun->dev, &dev_attr_file);
+ device_remove_file(&lun->dev,
+ lun->cdrom
+ ? &dev_attr_ro_cdrom
+ : &dev_attr_ro);
+ device_remove_file(&lun->dev,
+ lun->removable
+ ? &dev_attr_file
+ : &dev_attr_file_nonremovable);
fsg_lun_close(lun);
device_unregister(&lun->dev);
}
--- a/drivers/usb/gadget/storage_common.c
+++ b/drivers/usb/gadget/storage_common.c
@@ -878,7 +878,6 @@ static ssize_t fsg_store_file(struct dev
if (count > 0 && buf[count-1] == '\n')
((char *) buf)[count-1] = 0; /* Ugh! */
-
/* Load new medium */
down_write(filesem);
if (count > 0 && buf[0]) {