From patchwork Wed Feb 13 04:23:58 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Do Q.Thang" X-Patchwork-Id: 2134331 Return-Path: X-Original-To: patchwork-ltsi-dev@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) by patchwork2.kernel.org (Postfix) with ESMTP id ACD76DFE75 for ; Wed, 13 Feb 2013 04:55:54 +0000 (UTC) Received: from mail.linux-foundation.org (localhost [IPv6:::1]) by mail.linuxfoundation.org (Postfix) with ESMTP id 20C1F714; Wed, 13 Feb 2013 04:55:52 +0000 (UTC) X-Original-To: ltsi-dev@lists.linuxfoundation.org Delivered-To: ltsi-dev@mail.linuxfoundation.org Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTP id E48B0710 for ; Wed, 13 Feb 2013 04:55:50 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail.omesemicon.co.jp.omesemicon.co.jp (219-118-191-130.cust.bit-drive.ne.jp [219.118.191.130]) by smtp1.linuxfoundation.org (Postfix) with ESMTP id 422D21F8AB for ; Wed, 13 Feb 2013 04:55:50 +0000 (UTC) Received: from [192.168.11.3] by [192.168.11.254] with ESMTP; Wed, 13 Feb 2013 13:55:50 +0900 Received: from localhost (p14010-ipadfx41marunouchi.tokyo.ocn.ne.jp [61.118.107.10]) by mail.omesemicon.co.jp.omesemicon.co.jp (8.13.1/3.7W) with ESMTP id r1D4OtdC007096 for ; Wed, 13 Feb 2013 13:24:55 +0900 From: Do Quang Thang To: ltsi-dev@lists.linuxfoundation.org Date: Wed, 13 Feb 2013 13:23:58 +0900 Message-Id: <1360729438-10731-11-git-send-email-dq-thang@jinso.co.jp> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1360729438-10731-1-git-send-email-dq-thang@jinso.co.jp> References: <1360729438-10731-1-git-send-email-dq-thang@jinso.co.jp> MIME-Version: 1.0 X-Spam-Status: No, score=0.1 required=5.0 tests=BAYES_00, RCVD_DOUBLE_IP_LOOSE, RDNS_DYNAMIC autolearn=no version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Subject: [LTSI-dev] [PATCH 10/10] usb: gadget: mass_storage: make "file" and "ro" read only in some cases X-BeenThere: ltsi-dev@lists.linuxfoundation.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: "A list to discuss patches, development, and other things related to the LTSI project" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: ltsi-dev-bounces@lists.linuxfoundation.org Errors-To: ltsi-dev-bounces@lists.linuxfoundation.org From: Michal Nazarewicz 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 Signed-off-by: Felipe Balbi (cherry picked from commit 48a31af74404e6460eabca410bf0b4a625bfd372) Signed-off-by: Do Quang Thang --- drivers/usb/gadget/f_mass_storage.c | 26 +++++++++++++++++++++----- drivers/usb/gadget/storage_common.c | 1 - 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/drivers/usb/gadget/f_mass_storage.c b/drivers/usb/gadget/f_mass_storage.c index 4188e6d..8840fbd 100644 --- a/drivers/usb/gadget/f_mass_storage.c +++ b/drivers/usb/gadget/f_mass_storage.c @@ -2609,11 +2609,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 ******************************/ @@ -2724,10 +2728,16 @@ static struct fsg_common *fsg_common_init(struct fsg_common *common, 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); @@ -2862,8 +2872,14 @@ static void fsg_common_release(struct kref *ref) /* 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); } diff --git a/drivers/usb/gadget/storage_common.c b/drivers/usb/gadget/storage_common.c index e576678..ae8b188 100644 --- 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 device *dev, struct device_attribute *attr, if (count > 0 && buf[count-1] == '\n') ((char *) buf)[count-1] = 0; /* Ugh! */ - /* Load new medium */ down_write(filesem); if (count > 0 && buf[0]) {