storage_common.c 13 KB

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