storage_common.c 13 KB

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