storage_common.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  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
  6. * Author: Michal Nazarewicz (m.nazarewicz@samsung.com)
  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.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. /*
  23. * This file requires the following identifiers used in USB strings to
  24. * be defined (each of type pointer to char):
  25. * - fsg_string_manufacturer -- name of the manufacturer
  26. * - fsg_string_product -- name of the product
  27. * - fsg_string_config -- name of the configuration
  28. * - fsg_string_interface -- name of the interface
  29. * The first four are only needed when FSG_DESCRIPTORS_DEVICE_STRINGS
  30. * macro is defined prior to including this file.
  31. */
  32. /*
  33. * When FSG_NO_INTR_EP is defined fsg_fs_intr_in_desc and
  34. * fsg_hs_intr_in_desc objects as well as
  35. * FSG_FS_FUNCTION_PRE_EP_ENTRIES and FSG_HS_FUNCTION_PRE_EP_ENTRIES
  36. * macros are not defined.
  37. *
  38. * When FSG_NO_DEVICE_STRINGS is defined FSG_STRING_MANUFACTURER,
  39. * FSG_STRING_PRODUCT, FSG_STRING_SERIAL and FSG_STRING_CONFIG are not
  40. * defined (as well as corresponding entries in string tables are
  41. * missing) and FSG_STRING_INTERFACE has value of zero.
  42. *
  43. * When FSG_NO_OTG is defined fsg_otg_desc won't be defined.
  44. */
  45. /*
  46. * When FSG_BUFFHD_STATIC_BUFFER is defined when this file is included
  47. * the fsg_buffhd structure's buf field will be an array of FSG_BUFLEN
  48. * characters rather then a pointer to void.
  49. */
  50. #include <linux/usb/storage.h>
  51. #include <scsi/scsi.h>
  52. #include <asm/unaligned.h>
  53. /*
  54. * Thanks to NetChip Technologies for donating this product ID.
  55. *
  56. * DO NOT REUSE THESE IDs with any other driver!! Ever!!
  57. * Instead: allocate your own, using normal USB-IF procedures.
  58. */
  59. #define FSG_VENDOR_ID 0x0525 /* NetChip */
  60. #define FSG_PRODUCT_ID 0xa4a5 /* Linux-USB File-backed Storage Gadget */
  61. /*-------------------------------------------------------------------------*/
  62. #ifndef DEBUG
  63. #undef VERBOSE_DEBUG
  64. #undef DUMP_MSGS
  65. #endif /* !DEBUG */
  66. #ifdef VERBOSE_DEBUG
  67. #define VLDBG LDBG
  68. #else
  69. #define VLDBG(lun, fmt, args...) do { } while (0)
  70. #endif /* VERBOSE_DEBUG */
  71. #define LDBG(lun, fmt, args...) dev_dbg (&(lun)->dev, fmt, ## args)
  72. #define LERROR(lun, fmt, args...) dev_err (&(lun)->dev, fmt, ## args)
  73. #define LWARN(lun, fmt, args...) dev_warn(&(lun)->dev, fmt, ## args)
  74. #define LINFO(lun, fmt, args...) dev_info(&(lun)->dev, fmt, ## args)
  75. /*
  76. * Keep those macros in sync with those in
  77. * include/linux/usb/composite.h or else GCC will complain. If they
  78. * are identical (the same names of arguments, white spaces in the
  79. * same places) GCC will allow redefinition otherwise (even if some
  80. * white space is removed or added) warning will be issued.
  81. *
  82. * Those macros are needed here because File Storage Gadget does not
  83. * include the composite.h header. For composite gadgets those macros
  84. * are redundant since composite.h is included any way.
  85. *
  86. * One could check whether those macros are already defined (which
  87. * would indicate composite.h had been included) or not (which would
  88. * indicate we were in FSG) but this is not done because a warning is
  89. * desired if definitions here differ from the ones in composite.h.
  90. *
  91. * We want the definitions to match and be the same in File Storage
  92. * Gadget as well as Mass Storage Function (and so composite gadgets
  93. * using MSF). If someone changes them in composite.h it will produce
  94. * a warning in this file when building MSF.
  95. */
  96. #define DBG(d, fmt, args...) dev_dbg(&(d)->gadget->dev , fmt , ## args)
  97. #define VDBG(d, fmt, args...) dev_vdbg(&(d)->gadget->dev , fmt , ## args)
  98. #define ERROR(d, fmt, args...) dev_err(&(d)->gadget->dev , fmt , ## args)
  99. #define WARNING(d, fmt, args...) dev_warn(&(d)->gadget->dev , fmt , ## args)
  100. #define INFO(d, fmt, args...) dev_info(&(d)->gadget->dev , fmt , ## args)
  101. #ifdef DUMP_MSGS
  102. # define dump_msg(fsg, /* const char * */ label, \
  103. /* const u8 * */ buf, /* unsigned */ length) do { \
  104. if (length < 512) { \
  105. DBG(fsg, "%s, length %u:\n", label, length); \
  106. print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, \
  107. 16, 1, buf, length, 0); \
  108. } \
  109. } while (0)
  110. # define dump_cdb(fsg) do { } while (0)
  111. #else
  112. # define dump_msg(fsg, /* const char * */ label, \
  113. /* const u8 * */ buf, /* unsigned */ length) do { } while (0)
  114. # ifdef VERBOSE_DEBUG
  115. # define dump_cdb(fsg) \
  116. print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE, \
  117. 16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0) \
  118. # else
  119. # define dump_cdb(fsg) do { } while (0)
  120. # endif /* VERBOSE_DEBUG */
  121. #endif /* DUMP_MSGS */
  122. /*-------------------------------------------------------------------------*/
  123. /* Bulk-only data structures */
  124. /* Command Block Wrapper */
  125. struct fsg_bulk_cb_wrap {
  126. __le32 Signature; /* Contains 'USBC' */
  127. u32 Tag; /* Unique per command id */
  128. __le32 DataTransferLength; /* Size of the data */
  129. u8 Flags; /* Direction in bit 7 */
  130. u8 Lun; /* LUN (normally 0) */
  131. u8 Length; /* Of the CDB, <= MAX_COMMAND_SIZE */
  132. u8 CDB[16]; /* Command Data Block */
  133. };
  134. #define USB_BULK_CB_WRAP_LEN 31
  135. #define USB_BULK_CB_SIG 0x43425355 /* Spells out USBC */
  136. #define USB_BULK_IN_FLAG 0x80
  137. /* Command Status Wrapper */
  138. struct bulk_cs_wrap {
  139. __le32 Signature; /* Should = 'USBS' */
  140. u32 Tag; /* Same as original command */
  141. __le32 Residue; /* Amount not transferred */
  142. u8 Status; /* See below */
  143. };
  144. #define USB_BULK_CS_WRAP_LEN 13
  145. #define USB_BULK_CS_SIG 0x53425355 /* Spells out 'USBS' */
  146. #define USB_STATUS_PASS 0
  147. #define USB_STATUS_FAIL 1
  148. #define USB_STATUS_PHASE_ERROR 2
  149. /* Bulk-only class specific requests */
  150. #define USB_BULK_RESET_REQUEST 0xff
  151. #define USB_BULK_GET_MAX_LUN_REQUEST 0xfe
  152. /* CBI Interrupt data structure */
  153. struct interrupt_data {
  154. u8 bType;
  155. u8 bValue;
  156. };
  157. #define CBI_INTERRUPT_DATA_LEN 2
  158. /* CBI Accept Device-Specific Command request */
  159. #define USB_CBI_ADSC_REQUEST 0x00
  160. /* Length of a SCSI Command Data Block */
  161. #define MAX_COMMAND_SIZE 16
  162. /* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
  163. #define SS_NO_SENSE 0
  164. #define SS_COMMUNICATION_FAILURE 0x040800
  165. #define SS_INVALID_COMMAND 0x052000
  166. #define SS_INVALID_FIELD_IN_CDB 0x052400
  167. #define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
  168. #define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
  169. #define SS_MEDIUM_NOT_PRESENT 0x023a00
  170. #define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
  171. #define SS_NOT_READY_TO_READY_TRANSITION 0x062800
  172. #define SS_RESET_OCCURRED 0x062900
  173. #define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
  174. #define SS_UNRECOVERED_READ_ERROR 0x031100
  175. #define SS_WRITE_ERROR 0x030c02
  176. #define SS_WRITE_PROTECTED 0x072700
  177. #define SK(x) ((u8) ((x) >> 16)) /* Sense Key byte, etc. */
  178. #define ASC(x) ((u8) ((x) >> 8))
  179. #define ASCQ(x) ((u8) (x))
  180. /*-------------------------------------------------------------------------*/
  181. struct fsg_lun {
  182. struct file *filp;
  183. loff_t file_length;
  184. loff_t num_sectors;
  185. unsigned int initially_ro:1;
  186. unsigned int ro:1;
  187. unsigned int removable:1;
  188. unsigned int cdrom:1;
  189. unsigned int prevent_medium_removal:1;
  190. unsigned int registered:1;
  191. unsigned int info_valid:1;
  192. unsigned int nofua:1;
  193. u32 sense_data;
  194. u32 sense_data_info;
  195. u32 unit_attention_data;
  196. struct device dev;
  197. };
  198. #define fsg_lun_is_open(curlun) ((curlun)->filp != NULL)
  199. static struct fsg_lun *fsg_lun_from_dev(struct device *dev)
  200. {
  201. return container_of(dev, struct fsg_lun, dev);
  202. }
  203. /* Big enough to hold our biggest descriptor */
  204. #define EP0_BUFSIZE 256
  205. #define DELAYED_STATUS (EP0_BUFSIZE + 999) /* An impossibly large value */
  206. /* Number of buffers we will use. 2 is enough for double-buffering */
  207. #define FSG_NUM_BUFFERS 2
  208. /* Default size of buffer length. */
  209. #define FSG_BUFLEN ((u32)16384)
  210. /* Maximal number of LUNs supported in mass storage function */
  211. #define FSG_MAX_LUNS 8
  212. enum fsg_buffer_state {
  213. BUF_STATE_EMPTY = 0,
  214. BUF_STATE_FULL,
  215. BUF_STATE_BUSY
  216. };
  217. struct fsg_buffhd {
  218. #ifdef FSG_BUFFHD_STATIC_BUFFER
  219. char buf[FSG_BUFLEN];
  220. #else
  221. void *buf;
  222. #endif
  223. enum fsg_buffer_state state;
  224. struct fsg_buffhd *next;
  225. /*
  226. * The NetChip 2280 is faster, and handles some protocol faults
  227. * better, if we don't submit any short bulk-out read requests.
  228. * So we will record the intended request length here.
  229. */
  230. unsigned int bulk_out_intended_length;
  231. struct usb_request *inreq;
  232. int inreq_busy;
  233. struct usb_request *outreq;
  234. int outreq_busy;
  235. };
  236. enum fsg_state {
  237. /* This one isn't used anywhere */
  238. FSG_STATE_COMMAND_PHASE = -10,
  239. FSG_STATE_DATA_PHASE,
  240. FSG_STATE_STATUS_PHASE,
  241. FSG_STATE_IDLE = 0,
  242. FSG_STATE_ABORT_BULK_OUT,
  243. FSG_STATE_RESET,
  244. FSG_STATE_INTERFACE_CHANGE,
  245. FSG_STATE_CONFIG_CHANGE,
  246. FSG_STATE_DISCONNECT,
  247. FSG_STATE_EXIT,
  248. FSG_STATE_TERMINATED
  249. };
  250. enum data_direction {
  251. DATA_DIR_UNKNOWN = 0,
  252. DATA_DIR_FROM_HOST,
  253. DATA_DIR_TO_HOST,
  254. DATA_DIR_NONE
  255. };
  256. /*-------------------------------------------------------------------------*/
  257. static inline u32 get_unaligned_be24(u8 *buf)
  258. {
  259. return 0xffffff & (u32) get_unaligned_be32(buf - 1);
  260. }
  261. /*-------------------------------------------------------------------------*/
  262. enum {
  263. #ifndef FSG_NO_DEVICE_STRINGS
  264. FSG_STRING_MANUFACTURER = 1,
  265. FSG_STRING_PRODUCT,
  266. FSG_STRING_SERIAL,
  267. FSG_STRING_CONFIG,
  268. #endif
  269. FSG_STRING_INTERFACE
  270. };
  271. #ifndef FSG_NO_OTG
  272. static struct usb_otg_descriptor
  273. fsg_otg_desc = {
  274. .bLength = sizeof fsg_otg_desc,
  275. .bDescriptorType = USB_DT_OTG,
  276. .bmAttributes = USB_OTG_SRP,
  277. };
  278. #endif
  279. /* There is only one interface. */
  280. static struct usb_interface_descriptor
  281. fsg_intf_desc = {
  282. .bLength = sizeof fsg_intf_desc,
  283. .bDescriptorType = USB_DT_INTERFACE,
  284. .bNumEndpoints = 2, /* Adjusted during fsg_bind() */
  285. .bInterfaceClass = USB_CLASS_MASS_STORAGE,
  286. .bInterfaceSubClass = USB_SC_SCSI, /* Adjusted during fsg_bind() */
  287. .bInterfaceProtocol = USB_PR_BULK, /* Adjusted during fsg_bind() */
  288. .iInterface = FSG_STRING_INTERFACE,
  289. };
  290. /*
  291. * Three full-speed endpoint descriptors: bulk-in, bulk-out, and
  292. * interrupt-in.
  293. */
  294. static struct usb_endpoint_descriptor
  295. fsg_fs_bulk_in_desc = {
  296. .bLength = USB_DT_ENDPOINT_SIZE,
  297. .bDescriptorType = USB_DT_ENDPOINT,
  298. .bEndpointAddress = USB_DIR_IN,
  299. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  300. /* wMaxPacketSize set by autoconfiguration */
  301. };
  302. static struct usb_endpoint_descriptor
  303. fsg_fs_bulk_out_desc = {
  304. .bLength = USB_DT_ENDPOINT_SIZE,
  305. .bDescriptorType = USB_DT_ENDPOINT,
  306. .bEndpointAddress = USB_DIR_OUT,
  307. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  308. /* wMaxPacketSize set by autoconfiguration */
  309. };
  310. #ifndef FSG_NO_INTR_EP
  311. static struct usb_endpoint_descriptor
  312. fsg_fs_intr_in_desc = {
  313. .bLength = USB_DT_ENDPOINT_SIZE,
  314. .bDescriptorType = USB_DT_ENDPOINT,
  315. .bEndpointAddress = USB_DIR_IN,
  316. .bmAttributes = USB_ENDPOINT_XFER_INT,
  317. .wMaxPacketSize = cpu_to_le16(2),
  318. .bInterval = 32, /* frames -> 32 ms */
  319. };
  320. #ifndef FSG_NO_OTG
  321. # define FSG_FS_FUNCTION_PRE_EP_ENTRIES 2
  322. #else
  323. # define FSG_FS_FUNCTION_PRE_EP_ENTRIES 1
  324. #endif
  325. #endif
  326. static struct usb_descriptor_header *fsg_fs_function[] = {
  327. #ifndef FSG_NO_OTG
  328. (struct usb_descriptor_header *) &fsg_otg_desc,
  329. #endif
  330. (struct usb_descriptor_header *) &fsg_intf_desc,
  331. (struct usb_descriptor_header *) &fsg_fs_bulk_in_desc,
  332. (struct usb_descriptor_header *) &fsg_fs_bulk_out_desc,
  333. #ifndef FSG_NO_INTR_EP
  334. (struct usb_descriptor_header *) &fsg_fs_intr_in_desc,
  335. #endif
  336. NULL,
  337. };
  338. /*
  339. * USB 2.0 devices need to expose both high speed and full speed
  340. * descriptors, unless they only run at full speed.
  341. *
  342. * That means alternate endpoint descriptors (bigger packets)
  343. * and a "device qualifier" ... plus more construction options
  344. * for the configuration descriptor.
  345. */
  346. static struct usb_endpoint_descriptor
  347. fsg_hs_bulk_in_desc = {
  348. .bLength = USB_DT_ENDPOINT_SIZE,
  349. .bDescriptorType = USB_DT_ENDPOINT,
  350. /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
  351. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  352. .wMaxPacketSize = cpu_to_le16(512),
  353. };
  354. static struct usb_endpoint_descriptor
  355. fsg_hs_bulk_out_desc = {
  356. .bLength = USB_DT_ENDPOINT_SIZE,
  357. .bDescriptorType = USB_DT_ENDPOINT,
  358. /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
  359. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  360. .wMaxPacketSize = cpu_to_le16(512),
  361. .bInterval = 1, /* NAK every 1 uframe */
  362. };
  363. #ifndef FSG_NO_INTR_EP
  364. static struct usb_endpoint_descriptor
  365. fsg_hs_intr_in_desc = {
  366. .bLength = USB_DT_ENDPOINT_SIZE,
  367. .bDescriptorType = USB_DT_ENDPOINT,
  368. /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */
  369. .bmAttributes = USB_ENDPOINT_XFER_INT,
  370. .wMaxPacketSize = cpu_to_le16(2),
  371. .bInterval = 9, /* 2**(9-1) = 256 uframes -> 32 ms */
  372. };
  373. #ifndef FSG_NO_OTG
  374. # define FSG_HS_FUNCTION_PRE_EP_ENTRIES 2
  375. #else
  376. # define FSG_HS_FUNCTION_PRE_EP_ENTRIES 1
  377. #endif
  378. #endif
  379. static struct usb_descriptor_header *fsg_hs_function[] = {
  380. #ifndef FSG_NO_OTG
  381. (struct usb_descriptor_header *) &fsg_otg_desc,
  382. #endif
  383. (struct usb_descriptor_header *) &fsg_intf_desc,
  384. (struct usb_descriptor_header *) &fsg_hs_bulk_in_desc,
  385. (struct usb_descriptor_header *) &fsg_hs_bulk_out_desc,
  386. #ifndef FSG_NO_INTR_EP
  387. (struct usb_descriptor_header *) &fsg_hs_intr_in_desc,
  388. #endif
  389. NULL,
  390. };
  391. /* Maxpacket and other transfer characteristics vary by speed. */
  392. static struct usb_endpoint_descriptor *
  393. fsg_ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *fs,
  394. struct usb_endpoint_descriptor *hs)
  395. {
  396. if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
  397. return hs;
  398. return fs;
  399. }
  400. /* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
  401. static struct usb_string fsg_strings[] = {
  402. #ifndef FSG_NO_DEVICE_STRINGS
  403. {FSG_STRING_MANUFACTURER, fsg_string_manufacturer},
  404. {FSG_STRING_PRODUCT, fsg_string_product},
  405. {FSG_STRING_SERIAL, ""},
  406. {FSG_STRING_CONFIG, fsg_string_config},
  407. #endif
  408. {FSG_STRING_INTERFACE, fsg_string_interface},
  409. {}
  410. };
  411. static struct usb_gadget_strings fsg_stringtab = {
  412. .language = 0x0409, /* en-us */
  413. .strings = fsg_strings,
  414. };
  415. /*-------------------------------------------------------------------------*/
  416. /*
  417. * If the next two routines are called while the gadget is registered,
  418. * the caller must own fsg->filesem for writing.
  419. */
  420. static int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
  421. {
  422. int ro;
  423. struct file *filp = NULL;
  424. int rc = -EINVAL;
  425. struct inode *inode = NULL;
  426. loff_t size;
  427. loff_t num_sectors;
  428. loff_t min_sectors;
  429. /* R/W if we can, R/O if we must */
  430. ro = curlun->initially_ro;
  431. if (!ro) {
  432. filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0);
  433. if (-EROFS == PTR_ERR(filp))
  434. ro = 1;
  435. }
  436. if (ro)
  437. filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0);
  438. if (IS_ERR(filp)) {
  439. LINFO(curlun, "unable to open backing file: %s\n", filename);
  440. return PTR_ERR(filp);
  441. }
  442. if (!(filp->f_mode & FMODE_WRITE))
  443. ro = 1;
  444. if (filp->f_path.dentry)
  445. inode = filp->f_path.dentry->d_inode;
  446. if (inode && S_ISBLK(inode->i_mode)) {
  447. if (bdev_read_only(inode->i_bdev))
  448. ro = 1;
  449. } else if (!inode || !S_ISREG(inode->i_mode)) {
  450. LINFO(curlun, "invalid file type: %s\n", filename);
  451. goto out;
  452. }
  453. /*
  454. * If we can't read the file, it's no good.
  455. * If we can't write the file, use it read-only.
  456. */
  457. if (!filp->f_op || !(filp->f_op->read || filp->f_op->aio_read)) {
  458. LINFO(curlun, "file not readable: %s\n", filename);
  459. goto out;
  460. }
  461. if (!(filp->f_op->write || filp->f_op->aio_write))
  462. ro = 1;
  463. size = i_size_read(inode->i_mapping->host);
  464. if (size < 0) {
  465. LINFO(curlun, "unable to find file size: %s\n", filename);
  466. rc = (int) size;
  467. goto out;
  468. }
  469. num_sectors = size >> 9; /* File size in 512-byte blocks */
  470. min_sectors = 1;
  471. if (curlun->cdrom) {
  472. num_sectors &= ~3; /* Reduce to a multiple of 2048 */
  473. min_sectors = 300*4; /* Smallest track is 300 frames */
  474. if (num_sectors >= 256*60*75*4) {
  475. num_sectors = (256*60*75 - 1) * 4;
  476. LINFO(curlun, "file too big: %s\n", filename);
  477. LINFO(curlun, "using only first %d blocks\n",
  478. (int) num_sectors);
  479. }
  480. }
  481. if (num_sectors < min_sectors) {
  482. LINFO(curlun, "file too small: %s\n", filename);
  483. rc = -ETOOSMALL;
  484. goto out;
  485. }
  486. get_file(filp);
  487. curlun->ro = ro;
  488. curlun->filp = filp;
  489. curlun->file_length = size;
  490. curlun->num_sectors = num_sectors;
  491. LDBG(curlun, "open backing file: %s\n", filename);
  492. rc = 0;
  493. out:
  494. filp_close(filp, current->files);
  495. return rc;
  496. }
  497. static void fsg_lun_close(struct fsg_lun *curlun)
  498. {
  499. if (curlun->filp) {
  500. LDBG(curlun, "close backing file\n");
  501. fput(curlun->filp);
  502. curlun->filp = NULL;
  503. }
  504. }
  505. /*-------------------------------------------------------------------------*/
  506. /*
  507. * Sync the file data, don't bother with the metadata.
  508. * This code was copied from fs/buffer.c:sys_fdatasync().
  509. */
  510. static int fsg_lun_fsync_sub(struct fsg_lun *curlun)
  511. {
  512. struct file *filp = curlun->filp;
  513. if (curlun->ro || !filp)
  514. return 0;
  515. return vfs_fsync(filp, 1);
  516. }
  517. static void store_cdrom_address(u8 *dest, int msf, u32 addr)
  518. {
  519. if (msf) {
  520. /* Convert to Minutes-Seconds-Frames */
  521. addr >>= 2; /* Convert to 2048-byte frames */
  522. addr += 2*75; /* Lead-in occupies 2 seconds */
  523. dest[3] = addr % 75; /* Frames */
  524. addr /= 75;
  525. dest[2] = addr % 60; /* Seconds */
  526. addr /= 60;
  527. dest[1] = addr; /* Minutes */
  528. dest[0] = 0; /* Reserved */
  529. } else {
  530. /* Absolute sector */
  531. put_unaligned_be32(addr, dest);
  532. }
  533. }
  534. /*-------------------------------------------------------------------------*/
  535. static ssize_t fsg_show_ro(struct device *dev, struct device_attribute *attr,
  536. char *buf)
  537. {
  538. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  539. return sprintf(buf, "%d\n", fsg_lun_is_open(curlun)
  540. ? curlun->ro
  541. : curlun->initially_ro);
  542. }
  543. static ssize_t fsg_show_nofua(struct device *dev, struct device_attribute *attr,
  544. char *buf)
  545. {
  546. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  547. return sprintf(buf, "%u\n", curlun->nofua);
  548. }
  549. static ssize_t fsg_show_file(struct device *dev, struct device_attribute *attr,
  550. char *buf)
  551. {
  552. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  553. struct rw_semaphore *filesem = dev_get_drvdata(dev);
  554. char *p;
  555. ssize_t rc;
  556. down_read(filesem);
  557. if (fsg_lun_is_open(curlun)) { /* Get the complete pathname */
  558. p = d_path(&curlun->filp->f_path, buf, PAGE_SIZE - 1);
  559. if (IS_ERR(p))
  560. rc = PTR_ERR(p);
  561. else {
  562. rc = strlen(p);
  563. memmove(buf, p, rc);
  564. buf[rc] = '\n'; /* Add a newline */
  565. buf[++rc] = 0;
  566. }
  567. } else { /* No file, return 0 bytes */
  568. *buf = 0;
  569. rc = 0;
  570. }
  571. up_read(filesem);
  572. return rc;
  573. }
  574. static ssize_t fsg_store_ro(struct device *dev, struct device_attribute *attr,
  575. const char *buf, size_t count)
  576. {
  577. ssize_t rc = count;
  578. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  579. struct rw_semaphore *filesem = dev_get_drvdata(dev);
  580. unsigned long ro;
  581. if (strict_strtoul(buf, 2, &ro))
  582. return -EINVAL;
  583. /*
  584. * Allow the write-enable status to change only while the
  585. * backing file is closed.
  586. */
  587. down_read(filesem);
  588. if (fsg_lun_is_open(curlun)) {
  589. LDBG(curlun, "read-only status change prevented\n");
  590. rc = -EBUSY;
  591. } else {
  592. curlun->ro = ro;
  593. curlun->initially_ro = ro;
  594. LDBG(curlun, "read-only status set to %d\n", curlun->ro);
  595. }
  596. up_read(filesem);
  597. return rc;
  598. }
  599. static ssize_t fsg_store_nofua(struct device *dev,
  600. struct device_attribute *attr,
  601. const char *buf, size_t count)
  602. {
  603. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  604. unsigned long nofua;
  605. if (strict_strtoul(buf, 2, &nofua))
  606. return -EINVAL;
  607. /* Sync data when switching from async mode to sync */
  608. if (!nofua && curlun->nofua)
  609. fsg_lun_fsync_sub(curlun);
  610. curlun->nofua = nofua;
  611. return count;
  612. }
  613. static ssize_t fsg_store_file(struct device *dev, struct device_attribute *attr,
  614. const char *buf, size_t count)
  615. {
  616. struct fsg_lun *curlun = fsg_lun_from_dev(dev);
  617. struct rw_semaphore *filesem = dev_get_drvdata(dev);
  618. int rc = 0;
  619. if (curlun->prevent_medium_removal && fsg_lun_is_open(curlun)) {
  620. LDBG(curlun, "eject attempt prevented\n");
  621. return -EBUSY; /* "Door is locked" */
  622. }
  623. /* Remove a trailing newline */
  624. if (count > 0 && buf[count-1] == '\n')
  625. ((char *) buf)[count-1] = 0; /* Ugh! */
  626. /* Eject current medium */
  627. down_write(filesem);
  628. if (fsg_lun_is_open(curlun)) {
  629. fsg_lun_close(curlun);
  630. curlun->unit_attention_data = SS_MEDIUM_NOT_PRESENT;
  631. }
  632. /* Load new medium */
  633. if (count > 0 && buf[0]) {
  634. rc = fsg_lun_open(curlun, buf);
  635. if (rc == 0)
  636. curlun->unit_attention_data =
  637. SS_NOT_READY_TO_READY_TRANSITION;
  638. }
  639. up_write(filesem);
  640. return (rc < 0 ? rc : count);
  641. }