Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 1 | /* |
| 2 | * storage_common.c -- Common definitions for mass storage functionality |
| 3 | * |
| 4 | * Copyright (C) 2003-2008 Alan Stern |
| 5 | * Copyeight (C) 2009 Samsung Electronics |
Michal Nazarewicz | 54b8360 | 2012-01-13 15:05:16 +0100 | [diff] [blame] | 6 | * Author: Michal Nazarewicz (mina86@mina86.com) |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 12 | */ |
| 13 | |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 14 | /* |
| 15 | * This file requires the following identifiers used in USB strings to |
| 16 | * be defined (each of type pointer to char): |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 17 | * - fsg_string_interface -- name of the interface |
Michal Nazarewicz | 93bcf12 | 2009-10-28 16:57:19 +0100 | [diff] [blame] | 18 | */ |
| 19 | |
Michal Nazarewicz | 606206c2 | 2009-10-28 16:57:21 +0100 | [diff] [blame] | 20 | /* |
Per Forlin | 6532c7f | 2011-08-19 21:21:27 +0200 | [diff] [blame] | 21 | * When USB_GADGET_DEBUG_FILES is defined the module param num_buffers |
| 22 | * sets the number of pipeline buffers (length of the fsg_buffhd array). |
| 23 | * The valid range of num_buffers is: num >= 2 && num <= 4. |
| 24 | */ |
| 25 | |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 26 | #include <linux/module.h> |
| 27 | #include <linux/blkdev.h> |
| 28 | #include <linux/file.h> |
| 29 | #include <linux/fs.h> |
| 30 | #include <linux/usb/composite.h> |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 31 | |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 32 | #include "storage_common.h" |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 33 | |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 34 | /* There is only one interface. */ |
| 35 | |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 36 | struct usb_interface_descriptor fsg_intf_desc = { |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 37 | .bLength = sizeof fsg_intf_desc, |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 38 | .bDescriptorType = USB_DT_INTERFACE, |
| 39 | |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 40 | .bNumEndpoints = 2, /* Adjusted during fsg_bind() */ |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 41 | .bInterfaceClass = USB_CLASS_MASS_STORAGE, |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 42 | .bInterfaceSubClass = USB_SC_SCSI, /* Adjusted during fsg_bind() */ |
| 43 | .bInterfaceProtocol = USB_PR_BULK, /* Adjusted during fsg_bind() */ |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 44 | .iInterface = FSG_STRING_INTERFACE, |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 45 | }; |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 46 | EXPORT_SYMBOL(fsg_intf_desc); |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 47 | |
Michal Nazarewicz | d089326 | 2010-07-05 16:38:04 +0200 | [diff] [blame] | 48 | /* |
| 49 | * Three full-speed endpoint descriptors: bulk-in, bulk-out, and |
| 50 | * interrupt-in. |
| 51 | */ |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 52 | |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 53 | struct usb_endpoint_descriptor fsg_fs_bulk_in_desc = { |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 54 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 55 | .bDescriptorType = USB_DT_ENDPOINT, |
| 56 | |
| 57 | .bEndpointAddress = USB_DIR_IN, |
| 58 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 59 | /* wMaxPacketSize set by autoconfiguration */ |
| 60 | }; |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 61 | EXPORT_SYMBOL(fsg_fs_bulk_in_desc); |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 62 | |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 63 | struct usb_endpoint_descriptor fsg_fs_bulk_out_desc = { |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 64 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 65 | .bDescriptorType = USB_DT_ENDPOINT, |
| 66 | |
| 67 | .bEndpointAddress = USB_DIR_OUT, |
| 68 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 69 | /* wMaxPacketSize set by autoconfiguration */ |
| 70 | }; |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 71 | EXPORT_SYMBOL(fsg_fs_bulk_out_desc); |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 72 | |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 73 | struct usb_descriptor_header *fsg_fs_function[] = { |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 74 | (struct usb_descriptor_header *) &fsg_intf_desc, |
| 75 | (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc, |
| 76 | (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc, |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 77 | NULL, |
| 78 | }; |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 79 | EXPORT_SYMBOL(fsg_fs_function); |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 80 | |
| 81 | |
| 82 | /* |
| 83 | * USB 2.0 devices need to expose both high speed and full speed |
| 84 | * descriptors, unless they only run at full speed. |
| 85 | * |
| 86 | * That means alternate endpoint descriptors (bigger packets) |
| 87 | * and a "device qualifier" ... plus more construction options |
Michal Nazarewicz | d089326 | 2010-07-05 16:38:04 +0200 | [diff] [blame] | 88 | * for the configuration descriptor. |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 89 | */ |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 90 | struct usb_endpoint_descriptor fsg_hs_bulk_in_desc = { |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 91 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 92 | .bDescriptorType = USB_DT_ENDPOINT, |
| 93 | |
| 94 | /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */ |
| 95 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 96 | .wMaxPacketSize = cpu_to_le16(512), |
| 97 | }; |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 98 | EXPORT_SYMBOL(fsg_hs_bulk_in_desc); |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 99 | |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 100 | struct usb_endpoint_descriptor fsg_hs_bulk_out_desc = { |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 101 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 102 | .bDescriptorType = USB_DT_ENDPOINT, |
| 103 | |
| 104 | /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */ |
| 105 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 106 | .wMaxPacketSize = cpu_to_le16(512), |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 107 | .bInterval = 1, /* NAK every 1 uframe */ |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 108 | }; |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 109 | EXPORT_SYMBOL(fsg_hs_bulk_out_desc); |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 110 | |
Michal Nazarewicz | 93bcf12 | 2009-10-28 16:57:19 +0100 | [diff] [blame] | 111 | |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 112 | struct usb_descriptor_header *fsg_hs_function[] = { |
Michal Nazarewicz | d618170 | 2009-10-28 16:57:15 +0100 | [diff] [blame] | 113 | (struct usb_descriptor_header *) &fsg_intf_desc, |
| 114 | (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc, |
| 115 | (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc, |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 116 | NULL, |
| 117 | }; |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 118 | EXPORT_SYMBOL(fsg_hs_function); |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 119 | |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 120 | struct usb_endpoint_descriptor fsg_ss_bulk_in_desc = { |
Felipe Balbi | 4bb99b7 | 2011-08-03 14:33:27 +0300 | [diff] [blame] | 121 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 122 | .bDescriptorType = USB_DT_ENDPOINT, |
| 123 | |
| 124 | /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */ |
| 125 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 126 | .wMaxPacketSize = cpu_to_le16(1024), |
| 127 | }; |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 128 | EXPORT_SYMBOL(fsg_ss_bulk_in_desc); |
Felipe Balbi | 4bb99b7 | 2011-08-03 14:33:27 +0300 | [diff] [blame] | 129 | |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 130 | struct usb_ss_ep_comp_descriptor fsg_ss_bulk_in_comp_desc = { |
Felipe Balbi | 4bb99b7 | 2011-08-03 14:33:27 +0300 | [diff] [blame] | 131 | .bLength = sizeof(fsg_ss_bulk_in_comp_desc), |
| 132 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 133 | |
| 134 | /*.bMaxBurst = DYNAMIC, */ |
| 135 | }; |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 136 | EXPORT_SYMBOL(fsg_ss_bulk_in_comp_desc); |
Felipe Balbi | 4bb99b7 | 2011-08-03 14:33:27 +0300 | [diff] [blame] | 137 | |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 138 | struct usb_endpoint_descriptor fsg_ss_bulk_out_desc = { |
Felipe Balbi | 4bb99b7 | 2011-08-03 14:33:27 +0300 | [diff] [blame] | 139 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 140 | .bDescriptorType = USB_DT_ENDPOINT, |
| 141 | |
| 142 | /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */ |
| 143 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 144 | .wMaxPacketSize = cpu_to_le16(1024), |
| 145 | }; |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 146 | EXPORT_SYMBOL(fsg_ss_bulk_out_desc); |
Felipe Balbi | 4bb99b7 | 2011-08-03 14:33:27 +0300 | [diff] [blame] | 147 | |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 148 | struct usb_ss_ep_comp_descriptor fsg_ss_bulk_out_comp_desc = { |
Felipe Balbi | 4bb99b7 | 2011-08-03 14:33:27 +0300 | [diff] [blame] | 149 | .bLength = sizeof(fsg_ss_bulk_in_comp_desc), |
| 150 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 151 | |
| 152 | /*.bMaxBurst = DYNAMIC, */ |
| 153 | }; |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 154 | EXPORT_SYMBOL(fsg_ss_bulk_out_comp_desc); |
Felipe Balbi | 4bb99b7 | 2011-08-03 14:33:27 +0300 | [diff] [blame] | 155 | |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 156 | struct usb_descriptor_header *fsg_ss_function[] = { |
Felipe Balbi | 4bb99b7 | 2011-08-03 14:33:27 +0300 | [diff] [blame] | 157 | (struct usb_descriptor_header *) &fsg_intf_desc, |
| 158 | (struct usb_descriptor_header *) &fsg_ss_bulk_in_desc, |
| 159 | (struct usb_descriptor_header *) &fsg_ss_bulk_in_comp_desc, |
| 160 | (struct usb_descriptor_header *) &fsg_ss_bulk_out_desc, |
| 161 | (struct usb_descriptor_header *) &fsg_ss_bulk_out_comp_desc, |
Felipe Balbi | 4bb99b7 | 2011-08-03 14:33:27 +0300 | [diff] [blame] | 162 | NULL, |
| 163 | }; |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 164 | EXPORT_SYMBOL(fsg_ss_function); |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 165 | |
| 166 | |
| 167 | /*-------------------------------------------------------------------------*/ |
| 168 | |
Michal Nazarewicz | d089326 | 2010-07-05 16:38:04 +0200 | [diff] [blame] | 169 | /* |
| 170 | * If the next two routines are called while the gadget is registered, |
| 171 | * the caller must own fsg->filesem for writing. |
| 172 | */ |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 173 | |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 174 | void fsg_lun_close(struct fsg_lun *curlun) |
Michal Nazarewicz | d6e16a8 | 2012-06-18 14:37:20 +0200 | [diff] [blame] | 175 | { |
| 176 | if (curlun->filp) { |
| 177 | LDBG(curlun, "close backing file\n"); |
| 178 | fput(curlun->filp); |
| 179 | curlun->filp = NULL; |
| 180 | } |
| 181 | } |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 182 | EXPORT_SYMBOL(fsg_lun_close); |
Michal Nazarewicz | d6e16a8 | 2012-06-18 14:37:20 +0200 | [diff] [blame] | 183 | |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 184 | int fsg_lun_open(struct fsg_lun *curlun, const char *filename) |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 185 | { |
| 186 | int ro; |
| 187 | struct file *filp = NULL; |
| 188 | int rc = -EINVAL; |
| 189 | struct inode *inode = NULL; |
| 190 | loff_t size; |
| 191 | loff_t num_sectors; |
| 192 | loff_t min_sectors; |
Michal Nazarewicz | d6e16a8 | 2012-06-18 14:37:20 +0200 | [diff] [blame] | 193 | unsigned int blkbits; |
| 194 | unsigned int blksize; |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 195 | |
| 196 | /* R/W if we can, R/O if we must */ |
Michal Nazarewicz | e909ef5 | 2009-10-28 16:57:16 +0100 | [diff] [blame] | 197 | ro = curlun->initially_ro; |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 198 | if (!ro) { |
| 199 | filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0); |
Tejun Heo | 75f1dc0 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 200 | if (PTR_ERR(filp) == -EROFS || PTR_ERR(filp) == -EACCES) |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 201 | ro = 1; |
| 202 | } |
| 203 | if (ro) |
| 204 | filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0); |
| 205 | if (IS_ERR(filp)) { |
| 206 | LINFO(curlun, "unable to open backing file: %s\n", filename); |
| 207 | return PTR_ERR(filp); |
| 208 | } |
| 209 | |
| 210 | if (!(filp->f_mode & FMODE_WRITE)) |
| 211 | ro = 1; |
| 212 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 213 | inode = file_inode(filp); |
Al Viro | 20818a0 | 2012-07-22 21:23:33 +0400 | [diff] [blame] | 214 | if ((!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))) { |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 215 | LINFO(curlun, "invalid file type: %s\n", filename); |
| 216 | goto out; |
| 217 | } |
| 218 | |
Michal Nazarewicz | d089326 | 2010-07-05 16:38:04 +0200 | [diff] [blame] | 219 | /* |
| 220 | * If we can't read the file, it's no good. |
| 221 | * If we can't write the file, use it read-only. |
| 222 | */ |
Al Viro | 20818a0 | 2012-07-22 21:23:33 +0400 | [diff] [blame] | 223 | if (!(filp->f_op->read || filp->f_op->aio_read)) { |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 224 | LINFO(curlun, "file not readable: %s\n", filename); |
| 225 | goto out; |
| 226 | } |
| 227 | if (!(filp->f_op->write || filp->f_op->aio_write)) |
| 228 | ro = 1; |
| 229 | |
| 230 | size = i_size_read(inode->i_mapping->host); |
| 231 | if (size < 0) { |
| 232 | LINFO(curlun, "unable to find file size: %s\n", filename); |
| 233 | rc = (int) size; |
| 234 | goto out; |
| 235 | } |
Peiyu Li | 3f565a3 | 2011-08-17 22:52:59 -0700 | [diff] [blame] | 236 | |
| 237 | if (curlun->cdrom) { |
Michal Nazarewicz | d6e16a8 | 2012-06-18 14:37:20 +0200 | [diff] [blame] | 238 | blksize = 2048; |
| 239 | blkbits = 11; |
Peiyu Li | 3f565a3 | 2011-08-17 22:52:59 -0700 | [diff] [blame] | 240 | } else if (inode->i_bdev) { |
Michal Nazarewicz | d6e16a8 | 2012-06-18 14:37:20 +0200 | [diff] [blame] | 241 | blksize = bdev_logical_block_size(inode->i_bdev); |
| 242 | blkbits = blksize_bits(blksize); |
Peiyu Li | 3f565a3 | 2011-08-17 22:52:59 -0700 | [diff] [blame] | 243 | } else { |
Michal Nazarewicz | d6e16a8 | 2012-06-18 14:37:20 +0200 | [diff] [blame] | 244 | blksize = 512; |
| 245 | blkbits = 9; |
Peiyu Li | 3f565a3 | 2011-08-17 22:52:59 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Michal Nazarewicz | d6e16a8 | 2012-06-18 14:37:20 +0200 | [diff] [blame] | 248 | num_sectors = size >> blkbits; /* File size in logic-block-size blocks */ |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 249 | min_sectors = 1; |
Michal Nazarewicz | e909ef5 | 2009-10-28 16:57:16 +0100 | [diff] [blame] | 250 | if (curlun->cdrom) { |
Peiyu Li | 3f565a3 | 2011-08-17 22:52:59 -0700 | [diff] [blame] | 251 | min_sectors = 300; /* Smallest track is 300 frames */ |
| 252 | if (num_sectors >= 256*60*75) { |
| 253 | num_sectors = 256*60*75 - 1; |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 254 | LINFO(curlun, "file too big: %s\n", filename); |
| 255 | LINFO(curlun, "using only first %d blocks\n", |
| 256 | (int) num_sectors); |
| 257 | } |
| 258 | } |
| 259 | if (num_sectors < min_sectors) { |
| 260 | LINFO(curlun, "file too small: %s\n", filename); |
| 261 | rc = -ETOOSMALL; |
| 262 | goto out; |
| 263 | } |
| 264 | |
Michal Nazarewicz | d6e16a8 | 2012-06-18 14:37:20 +0200 | [diff] [blame] | 265 | if (fsg_lun_is_open(curlun)) |
| 266 | fsg_lun_close(curlun); |
| 267 | |
Michal Nazarewicz | d6e16a8 | 2012-06-18 14:37:20 +0200 | [diff] [blame] | 268 | curlun->blksize = blksize; |
| 269 | curlun->blkbits = blkbits; |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 270 | curlun->ro = ro; |
| 271 | curlun->filp = filp; |
| 272 | curlun->file_length = size; |
| 273 | curlun->num_sectors = num_sectors; |
| 274 | LDBG(curlun, "open backing file: %s\n", filename); |
Al Viro | 20818a0 | 2012-07-22 21:23:33 +0400 | [diff] [blame] | 275 | return 0; |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 276 | |
| 277 | out: |
Al Viro | 20818a0 | 2012-07-22 21:23:33 +0400 | [diff] [blame] | 278 | fput(filp); |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 279 | return rc; |
| 280 | } |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 281 | EXPORT_SYMBOL(fsg_lun_open); |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 282 | |
| 283 | |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 284 | /*-------------------------------------------------------------------------*/ |
| 285 | |
Michal Nazarewicz | d089326 | 2010-07-05 16:38:04 +0200 | [diff] [blame] | 286 | /* |
| 287 | * Sync the file data, don't bother with the metadata. |
| 288 | * This code was copied from fs/buffer.c:sys_fdatasync(). |
| 289 | */ |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 290 | int fsg_lun_fsync_sub(struct fsg_lun *curlun) |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 291 | { |
| 292 | struct file *filp = curlun->filp; |
| 293 | |
| 294 | if (curlun->ro || !filp) |
| 295 | return 0; |
Christoph Hellwig | 8018ab0 | 2010-03-22 17:32:25 +0100 | [diff] [blame] | 296 | return vfs_fsync(filp, 1); |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 297 | } |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 298 | EXPORT_SYMBOL(fsg_lun_fsync_sub); |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 299 | |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 300 | void store_cdrom_address(u8 *dest, int msf, u32 addr) |
Michal Nazarewicz | b6058d0 | 2009-10-28 16:57:14 +0100 | [diff] [blame] | 301 | { |
| 302 | if (msf) { |
| 303 | /* Convert to Minutes-Seconds-Frames */ |
| 304 | addr >>= 2; /* Convert to 2048-byte frames */ |
| 305 | addr += 2*75; /* Lead-in occupies 2 seconds */ |
| 306 | dest[3] = addr % 75; /* Frames */ |
| 307 | addr /= 75; |
| 308 | dest[2] = addr % 60; /* Seconds */ |
| 309 | addr /= 60; |
| 310 | dest[1] = addr; /* Minutes */ |
| 311 | dest[0] = 0; /* Reserved */ |
| 312 | } else { |
| 313 | /* Absolute sector */ |
| 314 | put_unaligned_be32(addr, dest); |
| 315 | } |
| 316 | } |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 317 | EXPORT_SYMBOL(store_cdrom_address); |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 318 | |
| 319 | /*-------------------------------------------------------------------------*/ |
| 320 | |
| 321 | |
Andrzej Pietrasiewicz | 77850ae | 2013-10-09 10:06:03 +0200 | [diff] [blame] | 322 | ssize_t fsg_show_ro(struct fsg_lun *curlun, char *buf) |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 323 | { |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 324 | return sprintf(buf, "%d\n", fsg_lun_is_open(curlun) |
| 325 | ? curlun->ro |
| 326 | : curlun->initially_ro); |
| 327 | } |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 328 | EXPORT_SYMBOL(fsg_show_ro); |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 329 | |
Andrzej Pietrasiewicz | 77850ae | 2013-10-09 10:06:03 +0200 | [diff] [blame] | 330 | ssize_t fsg_show_nofua(struct fsg_lun *curlun, char *buf) |
Andy Shevchenko | a93917d | 2010-07-22 17:53:56 +0300 | [diff] [blame] | 331 | { |
Andy Shevchenko | a93917d | 2010-07-22 17:53:56 +0300 | [diff] [blame] | 332 | return sprintf(buf, "%u\n", curlun->nofua); |
| 333 | } |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 334 | EXPORT_SYMBOL(fsg_show_nofua); |
Andy Shevchenko | a93917d | 2010-07-22 17:53:56 +0300 | [diff] [blame] | 335 | |
Andrzej Pietrasiewicz | 77850ae | 2013-10-09 10:06:03 +0200 | [diff] [blame] | 336 | ssize_t fsg_show_file(struct fsg_lun *curlun, struct rw_semaphore *filesem, |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 337 | char *buf) |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 338 | { |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 339 | char *p; |
| 340 | ssize_t rc; |
| 341 | |
| 342 | down_read(filesem); |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 343 | if (fsg_lun_is_open(curlun)) { /* Get the complete pathname */ |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 344 | p = d_path(&curlun->filp->f_path, buf, PAGE_SIZE - 1); |
| 345 | if (IS_ERR(p)) |
| 346 | rc = PTR_ERR(p); |
| 347 | else { |
| 348 | rc = strlen(p); |
| 349 | memmove(buf, p, rc); |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 350 | buf[rc] = '\n'; /* Add a newline */ |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 351 | buf[++rc] = 0; |
| 352 | } |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 353 | } else { /* No file, return 0 bytes */ |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 354 | *buf = 0; |
| 355 | rc = 0; |
| 356 | } |
| 357 | up_read(filesem); |
| 358 | return rc; |
| 359 | } |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 360 | EXPORT_SYMBOL(fsg_show_file); |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 361 | |
Andrzej Pietrasiewicz | 864328e | 2013-10-09 10:06:04 +0200 | [diff] [blame] | 362 | ssize_t fsg_show_cdrom(struct fsg_lun *curlun, char *buf) |
| 363 | { |
| 364 | return sprintf(buf, "%u\n", curlun->cdrom); |
| 365 | } |
| 366 | EXPORT_SYMBOL(fsg_show_cdrom); |
| 367 | |
| 368 | ssize_t fsg_show_removable(struct fsg_lun *curlun, char *buf) |
| 369 | { |
| 370 | return sprintf(buf, "%u\n", curlun->removable); |
| 371 | } |
| 372 | EXPORT_SYMBOL(fsg_show_removable); |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 373 | |
Andrzej Pietrasiewicz | b879863 | 2013-10-15 08:33:13 +0200 | [diff] [blame] | 374 | /* |
| 375 | * The caller must hold fsg->filesem for reading when calling this function. |
| 376 | */ |
| 377 | static ssize_t _fsg_store_ro(struct fsg_lun *curlun, bool ro) |
| 378 | { |
| 379 | if (fsg_lun_is_open(curlun)) { |
| 380 | LDBG(curlun, "read-only status change prevented\n"); |
| 381 | return -EBUSY; |
| 382 | } |
| 383 | |
| 384 | curlun->ro = ro; |
| 385 | curlun->initially_ro = ro; |
| 386 | LDBG(curlun, "read-only status set to %d\n", curlun->ro); |
| 387 | |
| 388 | return 0; |
| 389 | } |
| 390 | |
Andrzej Pietrasiewicz | 77850ae | 2013-10-09 10:06:03 +0200 | [diff] [blame] | 391 | ssize_t fsg_store_ro(struct fsg_lun *curlun, struct rw_semaphore *filesem, |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 392 | const char *buf, size_t count) |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 393 | { |
Michal Nazarewicz | fd4477b | 2011-04-14 11:55:43 +0200 | [diff] [blame] | 394 | ssize_t rc; |
Andrzej Pietrasiewicz | 81a1d5e | 2013-10-15 08:33:12 +0200 | [diff] [blame] | 395 | bool ro; |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 396 | |
Andrzej Pietrasiewicz | 81a1d5e | 2013-10-15 08:33:12 +0200 | [diff] [blame] | 397 | rc = strtobool(buf, &ro); |
Michal Nazarewicz | db8fa28 | 2011-04-14 00:37:00 +0200 | [diff] [blame] | 398 | if (rc) |
| 399 | return rc; |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 400 | |
Michal Nazarewicz | d089326 | 2010-07-05 16:38:04 +0200 | [diff] [blame] | 401 | /* |
| 402 | * Allow the write-enable status to change only while the |
| 403 | * backing file is closed. |
| 404 | */ |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 405 | down_read(filesem); |
Andrzej Pietrasiewicz | b879863 | 2013-10-15 08:33:13 +0200 | [diff] [blame] | 406 | rc = _fsg_store_ro(curlun, ro); |
| 407 | if (!rc) |
Michal Nazarewicz | fd4477b | 2011-04-14 11:55:43 +0200 | [diff] [blame] | 408 | rc = count; |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 409 | up_read(filesem); |
Andrzej Pietrasiewicz | b879863 | 2013-10-15 08:33:13 +0200 | [diff] [blame] | 410 | |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 411 | return rc; |
| 412 | } |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 413 | EXPORT_SYMBOL(fsg_store_ro); |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 414 | |
Andrzej Pietrasiewicz | 77850ae | 2013-10-09 10:06:03 +0200 | [diff] [blame] | 415 | ssize_t fsg_store_nofua(struct fsg_lun *curlun, const char *buf, size_t count) |
Andy Shevchenko | a93917d | 2010-07-22 17:53:56 +0300 | [diff] [blame] | 416 | { |
Andrzej Pietrasiewicz | 81a1d5e | 2013-10-15 08:33:12 +0200 | [diff] [blame] | 417 | bool nofua; |
Michal Nazarewicz | db8fa28 | 2011-04-14 00:37:00 +0200 | [diff] [blame] | 418 | int ret; |
Andy Shevchenko | a93917d | 2010-07-22 17:53:56 +0300 | [diff] [blame] | 419 | |
Andrzej Pietrasiewicz | 81a1d5e | 2013-10-15 08:33:12 +0200 | [diff] [blame] | 420 | ret = strtobool(buf, &nofua); |
Michal Nazarewicz | db8fa28 | 2011-04-14 00:37:00 +0200 | [diff] [blame] | 421 | if (ret) |
| 422 | return ret; |
Andy Shevchenko | a93917d | 2010-07-22 17:53:56 +0300 | [diff] [blame] | 423 | |
| 424 | /* Sync data when switching from async mode to sync */ |
| 425 | if (!nofua && curlun->nofua) |
| 426 | fsg_lun_fsync_sub(curlun); |
| 427 | |
| 428 | curlun->nofua = nofua; |
| 429 | |
| 430 | return count; |
| 431 | } |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 432 | EXPORT_SYMBOL(fsg_store_nofua); |
Andy Shevchenko | a93917d | 2010-07-22 17:53:56 +0300 | [diff] [blame] | 433 | |
Andrzej Pietrasiewicz | 77850ae | 2013-10-09 10:06:03 +0200 | [diff] [blame] | 434 | ssize_t fsg_store_file(struct fsg_lun *curlun, struct rw_semaphore *filesem, |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 435 | const char *buf, size_t count) |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 436 | { |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 437 | int rc = 0; |
| 438 | |
| 439 | if (curlun->prevent_medium_removal && fsg_lun_is_open(curlun)) { |
| 440 | LDBG(curlun, "eject attempt prevented\n"); |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 441 | return -EBUSY; /* "Door is locked" */ |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | /* Remove a trailing newline */ |
| 445 | if (count > 0 && buf[count-1] == '\n') |
Michal Nazarewicz | d26a6aa | 2009-11-09 14:15:23 +0100 | [diff] [blame] | 446 | ((char *) buf)[count-1] = 0; /* Ugh! */ |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 447 | |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 448 | /* Load new medium */ |
Michal Nazarewicz | d6e16a8 | 2012-06-18 14:37:20 +0200 | [diff] [blame] | 449 | down_write(filesem); |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 450 | if (count > 0 && buf[0]) { |
Michal Nazarewicz | d6e16a8 | 2012-06-18 14:37:20 +0200 | [diff] [blame] | 451 | /* fsg_lun_open() will close existing file if any. */ |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 452 | rc = fsg_lun_open(curlun, buf); |
| 453 | if (rc == 0) |
| 454 | curlun->unit_attention_data = |
| 455 | SS_NOT_READY_TO_READY_TRANSITION; |
Michal Nazarewicz | d6e16a8 | 2012-06-18 14:37:20 +0200 | [diff] [blame] | 456 | } else if (fsg_lun_is_open(curlun)) { |
| 457 | fsg_lun_close(curlun); |
| 458 | curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT; |
Michal Nazarewicz | 93f9374 | 2009-10-28 16:57:17 +0100 | [diff] [blame] | 459 | } |
| 460 | up_write(filesem); |
| 461 | return (rc < 0 ? rc : count); |
| 462 | } |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 463 | EXPORT_SYMBOL(fsg_store_file); |
| 464 | |
Andrzej Pietrasiewicz | b879863 | 2013-10-15 08:33:13 +0200 | [diff] [blame] | 465 | ssize_t fsg_store_cdrom(struct fsg_lun *curlun, struct rw_semaphore *filesem, |
| 466 | const char *buf, size_t count) |
Andrzej Pietrasiewicz | 864328e | 2013-10-09 10:06:04 +0200 | [diff] [blame] | 467 | { |
Andrzej Pietrasiewicz | 81a1d5e | 2013-10-15 08:33:12 +0200 | [diff] [blame] | 468 | bool cdrom; |
Andrzej Pietrasiewicz | 864328e | 2013-10-09 10:06:04 +0200 | [diff] [blame] | 469 | int ret; |
| 470 | |
Andrzej Pietrasiewicz | 81a1d5e | 2013-10-15 08:33:12 +0200 | [diff] [blame] | 471 | ret = strtobool(buf, &cdrom); |
Andrzej Pietrasiewicz | 864328e | 2013-10-09 10:06:04 +0200 | [diff] [blame] | 472 | if (ret) |
| 473 | return ret; |
| 474 | |
Andrzej Pietrasiewicz | b879863 | 2013-10-15 08:33:13 +0200 | [diff] [blame] | 475 | down_read(filesem); |
| 476 | ret = cdrom ? _fsg_store_ro(curlun, true) : 0; |
Andrzej Pietrasiewicz | 864328e | 2013-10-09 10:06:04 +0200 | [diff] [blame] | 477 | |
Andrzej Pietrasiewicz | b879863 | 2013-10-15 08:33:13 +0200 | [diff] [blame] | 478 | if (!ret) { |
| 479 | curlun->cdrom = cdrom; |
| 480 | ret = count; |
| 481 | } |
| 482 | up_read(filesem); |
| 483 | |
| 484 | return ret; |
Andrzej Pietrasiewicz | 864328e | 2013-10-09 10:06:04 +0200 | [diff] [blame] | 485 | } |
| 486 | EXPORT_SYMBOL(fsg_store_cdrom); |
| 487 | |
| 488 | ssize_t fsg_store_removable(struct fsg_lun *curlun, const char *buf, |
| 489 | size_t count) |
| 490 | { |
Andrzej Pietrasiewicz | 81a1d5e | 2013-10-15 08:33:12 +0200 | [diff] [blame] | 491 | bool removable; |
Andrzej Pietrasiewicz | 864328e | 2013-10-09 10:06:04 +0200 | [diff] [blame] | 492 | int ret; |
| 493 | |
Andrzej Pietrasiewicz | 81a1d5e | 2013-10-15 08:33:12 +0200 | [diff] [blame] | 494 | ret = strtobool(buf, &removable); |
Andrzej Pietrasiewicz | 864328e | 2013-10-09 10:06:04 +0200 | [diff] [blame] | 495 | if (ret) |
| 496 | return ret; |
| 497 | |
| 498 | curlun->removable = removable; |
| 499 | |
| 500 | return count; |
| 501 | } |
| 502 | EXPORT_SYMBOL(fsg_store_removable); |
| 503 | |
Andrzej Pietrasiewicz | 6fdc5dd | 2013-09-26 14:38:16 +0200 | [diff] [blame] | 504 | MODULE_LICENSE("GPL"); |