target_core_file.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. /*******************************************************************************
  2. * Filename: target_core_file.c
  3. *
  4. * This file contains the Storage Engine <-> FILEIO transport specific functions
  5. *
  6. * (c) Copyright 2005-2012 RisingTide Systems LLC.
  7. *
  8. * Nicholas A. Bellinger <nab@kernel.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. *
  24. ******************************************************************************/
  25. #include <linux/string.h>
  26. #include <linux/parser.h>
  27. #include <linux/timer.h>
  28. #include <linux/blkdev.h>
  29. #include <linux/slab.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/module.h>
  32. #include <linux/falloc.h>
  33. #include <scsi/scsi.h>
  34. #include <scsi/scsi_host.h>
  35. #include <target/target_core_base.h>
  36. #include <target/target_core_backend.h>
  37. #include "target_core_file.h"
  38. static inline struct fd_dev *FD_DEV(struct se_device *dev)
  39. {
  40. return container_of(dev, struct fd_dev, dev);
  41. }
  42. /* fd_attach_hba(): (Part of se_subsystem_api_t template)
  43. *
  44. *
  45. */
  46. static int fd_attach_hba(struct se_hba *hba, u32 host_id)
  47. {
  48. struct fd_host *fd_host;
  49. fd_host = kzalloc(sizeof(struct fd_host), GFP_KERNEL);
  50. if (!fd_host) {
  51. pr_err("Unable to allocate memory for struct fd_host\n");
  52. return -ENOMEM;
  53. }
  54. fd_host->fd_host_id = host_id;
  55. hba->hba_ptr = fd_host;
  56. pr_debug("CORE_HBA[%d] - TCM FILEIO HBA Driver %s on Generic"
  57. " Target Core Stack %s\n", hba->hba_id, FD_VERSION,
  58. TARGET_CORE_MOD_VERSION);
  59. pr_debug("CORE_HBA[%d] - Attached FILEIO HBA: %u to Generic"
  60. " MaxSectors: %u\n",
  61. hba->hba_id, fd_host->fd_host_id, FD_MAX_SECTORS);
  62. return 0;
  63. }
  64. static void fd_detach_hba(struct se_hba *hba)
  65. {
  66. struct fd_host *fd_host = hba->hba_ptr;
  67. pr_debug("CORE_HBA[%d] - Detached FILEIO HBA: %u from Generic"
  68. " Target Core\n", hba->hba_id, fd_host->fd_host_id);
  69. kfree(fd_host);
  70. hba->hba_ptr = NULL;
  71. }
  72. static struct se_device *fd_alloc_device(struct se_hba *hba, const char *name)
  73. {
  74. struct fd_dev *fd_dev;
  75. struct fd_host *fd_host = hba->hba_ptr;
  76. fd_dev = kzalloc(sizeof(struct fd_dev), GFP_KERNEL);
  77. if (!fd_dev) {
  78. pr_err("Unable to allocate memory for struct fd_dev\n");
  79. return NULL;
  80. }
  81. fd_dev->fd_host = fd_host;
  82. pr_debug("FILEIO: Allocated fd_dev for %p\n", name);
  83. return &fd_dev->dev;
  84. }
  85. static int fd_configure_device(struct se_device *dev)
  86. {
  87. struct fd_dev *fd_dev = FD_DEV(dev);
  88. struct fd_host *fd_host = dev->se_hba->hba_ptr;
  89. struct file *file;
  90. struct inode *inode = NULL;
  91. int flags, ret = -EINVAL;
  92. if (!(fd_dev->fbd_flags & FBDF_HAS_PATH)) {
  93. pr_err("Missing fd_dev_name=\n");
  94. return -EINVAL;
  95. }
  96. /*
  97. * Use O_DSYNC by default instead of O_SYNC to forgo syncing
  98. * of pure timestamp updates.
  99. */
  100. flags = O_RDWR | O_CREAT | O_LARGEFILE | O_DSYNC;
  101. /*
  102. * Optionally allow fd_buffered_io=1 to be enabled for people
  103. * who want use the fs buffer cache as an WriteCache mechanism.
  104. *
  105. * This means that in event of a hard failure, there is a risk
  106. * of silent data-loss if the SCSI client has *not* performed a
  107. * forced unit access (FUA) write, or issued SYNCHRONIZE_CACHE
  108. * to write-out the entire device cache.
  109. */
  110. if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) {
  111. pr_debug("FILEIO: Disabling O_DSYNC, using buffered FILEIO\n");
  112. flags &= ~O_DSYNC;
  113. }
  114. file = filp_open(fd_dev->fd_dev_name, flags, 0600);
  115. if (IS_ERR(file)) {
  116. pr_err("filp_open(%s) failed\n", fd_dev->fd_dev_name);
  117. ret = PTR_ERR(file);
  118. goto fail;
  119. }
  120. fd_dev->fd_file = file;
  121. /*
  122. * If using a block backend with this struct file, we extract
  123. * fd_dev->fd_[block,dev]_size from struct block_device.
  124. *
  125. * Otherwise, we use the passed fd_size= from configfs
  126. */
  127. inode = file->f_mapping->host;
  128. if (S_ISBLK(inode->i_mode)) {
  129. struct request_queue *q = bdev_get_queue(inode->i_bdev);
  130. unsigned long long dev_size;
  131. dev->dev_attrib.hw_block_size =
  132. bdev_logical_block_size(inode->i_bdev);
  133. dev->dev_attrib.hw_max_sectors = queue_max_hw_sectors(q);
  134. /*
  135. * Determine the number of bytes from i_size_read() minus
  136. * one (1) logical sector from underlying struct block_device
  137. */
  138. dev_size = (i_size_read(file->f_mapping->host) -
  139. fd_dev->fd_block_size);
  140. pr_debug("FILEIO: Using size: %llu bytes from struct"
  141. " block_device blocks: %llu logical_block_size: %d\n",
  142. dev_size, div_u64(dev_size, fd_dev->fd_block_size),
  143. fd_dev->fd_block_size);
  144. /*
  145. * Check if the underlying struct block_device request_queue supports
  146. * the QUEUE_FLAG_DISCARD bit for UNMAP/WRITE_SAME in SCSI + TRIM
  147. * in ATA and we need to set TPE=1
  148. */
  149. if (blk_queue_discard(q)) {
  150. dev->dev_attrib.max_unmap_lba_count =
  151. q->limits.max_discard_sectors;
  152. /*
  153. * Currently hardcoded to 1 in Linux/SCSI code..
  154. */
  155. dev->dev_attrib.max_unmap_block_desc_count = 1;
  156. dev->dev_attrib.unmap_granularity =
  157. q->limits.discard_granularity >> 9;
  158. dev->dev_attrib.unmap_granularity_alignment =
  159. q->limits.discard_alignment;
  160. pr_debug("IFILE: BLOCK Discard support available,"
  161. " disabled by default\n");
  162. }
  163. /*
  164. * Enable write same emulation for IBLOCK and use 0xFFFF as
  165. * the smaller WRITE_SAME(10) only has a two-byte block count.
  166. */
  167. dev->dev_attrib.max_write_same_len = 0xFFFF;
  168. } else {
  169. if (!(fd_dev->fbd_flags & FBDF_HAS_SIZE)) {
  170. pr_err("FILEIO: Missing fd_dev_size="
  171. " parameter, and no backing struct"
  172. " block_device\n");
  173. goto fail;
  174. }
  175. dev->dev_attrib.hw_block_size = FD_BLOCKSIZE;
  176. dev->dev_attrib.hw_max_sectors = FD_MAX_SECTORS;
  177. /*
  178. * Limit UNMAP emulation to 8k Number of LBAs (NoLB)
  179. */
  180. dev->dev_attrib.max_unmap_lba_count = 0x2000;
  181. /*
  182. * Currently hardcoded to 1 in Linux/SCSI code..
  183. */
  184. dev->dev_attrib.max_unmap_block_desc_count = 1;
  185. dev->dev_attrib.unmap_granularity = 1;
  186. dev->dev_attrib.unmap_granularity_alignment = 0;
  187. /*
  188. * Limit WRITE_SAME w/ UNMAP=0 emulation to 8k Number of LBAs (NoLB)
  189. * based upon struct iovec limit for vfs_writev()
  190. */
  191. dev->dev_attrib.max_write_same_len = 0x1000;
  192. }
  193. fd_dev->fd_block_size = dev->dev_attrib.hw_block_size;
  194. dev->dev_attrib.hw_queue_depth = FD_MAX_DEVICE_QUEUE_DEPTH;
  195. if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) {
  196. pr_debug("FILEIO: Forcing setting of emulate_write_cache=1"
  197. " with FDBD_HAS_BUFFERED_IO_WCE\n");
  198. dev->dev_attrib.emulate_write_cache = 1;
  199. }
  200. fd_dev->fd_dev_id = fd_host->fd_host_dev_id_count++;
  201. fd_dev->fd_queue_depth = dev->queue_depth;
  202. pr_debug("CORE_FILE[%u] - Added TCM FILEIO Device ID: %u at %s,"
  203. " %llu total bytes\n", fd_host->fd_host_id, fd_dev->fd_dev_id,
  204. fd_dev->fd_dev_name, fd_dev->fd_dev_size);
  205. return 0;
  206. fail:
  207. if (fd_dev->fd_file) {
  208. filp_close(fd_dev->fd_file, NULL);
  209. fd_dev->fd_file = NULL;
  210. }
  211. return ret;
  212. }
  213. static void fd_free_device(struct se_device *dev)
  214. {
  215. struct fd_dev *fd_dev = FD_DEV(dev);
  216. if (fd_dev->fd_file) {
  217. filp_close(fd_dev->fd_file, NULL);
  218. fd_dev->fd_file = NULL;
  219. }
  220. kfree(fd_dev);
  221. }
  222. static int fd_do_rw(struct se_cmd *cmd, struct scatterlist *sgl,
  223. u32 sgl_nents, int is_write)
  224. {
  225. struct se_device *se_dev = cmd->se_dev;
  226. struct fd_dev *dev = FD_DEV(se_dev);
  227. struct file *fd = dev->fd_file;
  228. struct scatterlist *sg;
  229. struct iovec *iov;
  230. mm_segment_t old_fs;
  231. loff_t pos = (cmd->t_task_lba * se_dev->dev_attrib.block_size);
  232. int ret = 0, i;
  233. iov = kzalloc(sizeof(struct iovec) * sgl_nents, GFP_KERNEL);
  234. if (!iov) {
  235. pr_err("Unable to allocate fd_do_readv iov[]\n");
  236. return -ENOMEM;
  237. }
  238. for_each_sg(sgl, sg, sgl_nents, i) {
  239. iov[i].iov_len = sg->length;
  240. iov[i].iov_base = kmap(sg_page(sg)) + sg->offset;
  241. }
  242. old_fs = get_fs();
  243. set_fs(get_ds());
  244. if (is_write)
  245. ret = vfs_writev(fd, &iov[0], sgl_nents, &pos);
  246. else
  247. ret = vfs_readv(fd, &iov[0], sgl_nents, &pos);
  248. set_fs(old_fs);
  249. for_each_sg(sgl, sg, sgl_nents, i)
  250. kunmap(sg_page(sg));
  251. kfree(iov);
  252. if (is_write) {
  253. if (ret < 0 || ret != cmd->data_length) {
  254. pr_err("%s() write returned %d\n", __func__, ret);
  255. return (ret < 0 ? ret : -EINVAL);
  256. }
  257. } else {
  258. /*
  259. * Return zeros and GOOD status even if the READ did not return
  260. * the expected virt_size for struct file w/o a backing struct
  261. * block_device.
  262. */
  263. if (S_ISBLK(file_inode(fd)->i_mode)) {
  264. if (ret < 0 || ret != cmd->data_length) {
  265. pr_err("%s() returned %d, expecting %u for "
  266. "S_ISBLK\n", __func__, ret,
  267. cmd->data_length);
  268. return (ret < 0 ? ret : -EINVAL);
  269. }
  270. } else {
  271. if (ret < 0) {
  272. pr_err("%s() returned %d for non S_ISBLK\n",
  273. __func__, ret);
  274. return ret;
  275. }
  276. }
  277. }
  278. return 1;
  279. }
  280. static sense_reason_t
  281. fd_execute_sync_cache(struct se_cmd *cmd)
  282. {
  283. struct se_device *dev = cmd->se_dev;
  284. struct fd_dev *fd_dev = FD_DEV(dev);
  285. int immed = (cmd->t_task_cdb[1] & 0x2);
  286. loff_t start, end;
  287. int ret;
  288. /*
  289. * If the Immediate bit is set, queue up the GOOD response
  290. * for this SYNCHRONIZE_CACHE op
  291. */
  292. if (immed)
  293. target_complete_cmd(cmd, SAM_STAT_GOOD);
  294. /*
  295. * Determine if we will be flushing the entire device.
  296. */
  297. if (cmd->t_task_lba == 0 && cmd->data_length == 0) {
  298. start = 0;
  299. end = LLONG_MAX;
  300. } else {
  301. start = cmd->t_task_lba * dev->dev_attrib.block_size;
  302. if (cmd->data_length)
  303. end = start + cmd->data_length;
  304. else
  305. end = LLONG_MAX;
  306. }
  307. ret = vfs_fsync_range(fd_dev->fd_file, start, end, 1);
  308. if (ret != 0)
  309. pr_err("FILEIO: vfs_fsync_range() failed: %d\n", ret);
  310. if (immed)
  311. return 0;
  312. if (ret)
  313. target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
  314. else
  315. target_complete_cmd(cmd, SAM_STAT_GOOD);
  316. return 0;
  317. }
  318. static unsigned char *
  319. fd_setup_write_same_buf(struct se_cmd *cmd, struct scatterlist *sg,
  320. unsigned int len)
  321. {
  322. struct se_device *se_dev = cmd->se_dev;
  323. unsigned int block_size = se_dev->dev_attrib.block_size;
  324. unsigned int i = 0, end;
  325. unsigned char *buf, *p, *kmap_buf;
  326. buf = kzalloc(min_t(unsigned int, len, PAGE_SIZE), GFP_KERNEL);
  327. if (!buf) {
  328. pr_err("Unable to allocate fd_execute_write_same buf\n");
  329. return NULL;
  330. }
  331. kmap_buf = kmap(sg_page(sg)) + sg->offset;
  332. if (!kmap_buf) {
  333. pr_err("kmap() failed in fd_setup_write_same\n");
  334. kfree(buf);
  335. return NULL;
  336. }
  337. /*
  338. * Fill local *buf to contain multiple WRITE_SAME blocks up to
  339. * min(len, PAGE_SIZE)
  340. */
  341. p = buf;
  342. end = min_t(unsigned int, len, PAGE_SIZE);
  343. while (i < end) {
  344. memcpy(p, kmap_buf, block_size);
  345. i += block_size;
  346. p += block_size;
  347. }
  348. kunmap(sg_page(sg));
  349. return buf;
  350. }
  351. static sense_reason_t
  352. fd_execute_write_same(struct se_cmd *cmd)
  353. {
  354. struct se_device *se_dev = cmd->se_dev;
  355. struct fd_dev *fd_dev = FD_DEV(se_dev);
  356. struct file *f = fd_dev->fd_file;
  357. struct scatterlist *sg;
  358. struct iovec *iov;
  359. mm_segment_t old_fs;
  360. sector_t nolb = sbc_get_write_same_sectors(cmd);
  361. loff_t pos = cmd->t_task_lba * se_dev->dev_attrib.block_size;
  362. unsigned int len, len_tmp, iov_num;
  363. int i, rc;
  364. unsigned char *buf;
  365. if (!nolb) {
  366. target_complete_cmd(cmd, SAM_STAT_GOOD);
  367. return 0;
  368. }
  369. sg = &cmd->t_data_sg[0];
  370. if (cmd->t_data_nents > 1 ||
  371. sg->length != cmd->se_dev->dev_attrib.block_size) {
  372. pr_err("WRITE_SAME: Illegal SGL t_data_nents: %u length: %u"
  373. " block_size: %u\n", cmd->t_data_nents, sg->length,
  374. cmd->se_dev->dev_attrib.block_size);
  375. return TCM_INVALID_CDB_FIELD;
  376. }
  377. len = len_tmp = nolb * se_dev->dev_attrib.block_size;
  378. iov_num = DIV_ROUND_UP(len, PAGE_SIZE);
  379. buf = fd_setup_write_same_buf(cmd, sg, len);
  380. if (!buf)
  381. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  382. iov = vzalloc(sizeof(struct iovec) * iov_num);
  383. if (!iov) {
  384. pr_err("Unable to allocate fd_execute_write_same iovecs\n");
  385. kfree(buf);
  386. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  387. }
  388. /*
  389. * Map the single fabric received scatterlist block now populated
  390. * in *buf into each iovec for I/O submission.
  391. */
  392. for (i = 0; i < iov_num; i++) {
  393. iov[i].iov_base = buf;
  394. iov[i].iov_len = min_t(unsigned int, len_tmp, PAGE_SIZE);
  395. len_tmp -= iov[i].iov_len;
  396. }
  397. old_fs = get_fs();
  398. set_fs(get_ds());
  399. rc = vfs_writev(f, &iov[0], iov_num, &pos);
  400. set_fs(old_fs);
  401. vfree(iov);
  402. kfree(buf);
  403. if (rc < 0 || rc != len) {
  404. pr_err("vfs_writev() returned %d for write same\n", rc);
  405. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  406. }
  407. target_complete_cmd(cmd, SAM_STAT_GOOD);
  408. return 0;
  409. }
  410. static sense_reason_t
  411. fd_execute_write_same_unmap(struct se_cmd *cmd)
  412. {
  413. struct se_device *se_dev = cmd->se_dev;
  414. struct fd_dev *fd_dev = FD_DEV(se_dev);
  415. struct file *file = fd_dev->fd_file;
  416. struct inode *inode = file->f_mapping->host;
  417. sector_t nolb = sbc_get_write_same_sectors(cmd);
  418. int ret;
  419. if (!nolb) {
  420. target_complete_cmd(cmd, SAM_STAT_GOOD);
  421. return 0;
  422. }
  423. if (S_ISBLK(inode->i_mode)) {
  424. /* The backend is block device, use discard */
  425. struct block_device *bdev = inode->i_bdev;
  426. ret = blkdev_issue_discard(bdev, cmd->t_task_lba,
  427. nolb, GFP_KERNEL, 0);
  428. if (ret < 0) {
  429. pr_warn("FILEIO: blkdev_issue_discard() failed: %d\n",
  430. ret);
  431. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  432. }
  433. } else {
  434. /* The backend is normal file, use fallocate */
  435. loff_t pos = cmd->t_task_lba * se_dev->dev_attrib.block_size;
  436. unsigned int len = nolb * se_dev->dev_attrib.block_size;
  437. int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
  438. if (!file->f_op->fallocate)
  439. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  440. ret = file->f_op->fallocate(file, mode, pos, len);
  441. if (ret < 0) {
  442. pr_warn("FILEIO: fallocate() failed: %d\n", ret);
  443. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  444. }
  445. }
  446. target_complete_cmd(cmd, GOOD);
  447. return 0;
  448. }
  449. static sense_reason_t
  450. fd_execute_rw(struct se_cmd *cmd)
  451. {
  452. struct scatterlist *sgl = cmd->t_data_sg;
  453. u32 sgl_nents = cmd->t_data_nents;
  454. enum dma_data_direction data_direction = cmd->data_direction;
  455. struct se_device *dev = cmd->se_dev;
  456. int ret = 0;
  457. /*
  458. * Call vectorized fileio functions to map struct scatterlist
  459. * physical memory addresses to struct iovec virtual memory.
  460. */
  461. if (data_direction == DMA_FROM_DEVICE) {
  462. ret = fd_do_rw(cmd, sgl, sgl_nents, 0);
  463. } else {
  464. ret = fd_do_rw(cmd, sgl, sgl_nents, 1);
  465. /*
  466. * Perform implict vfs_fsync_range() for fd_do_writev() ops
  467. * for SCSI WRITEs with Forced Unit Access (FUA) set.
  468. * Allow this to happen independent of WCE=0 setting.
  469. */
  470. if (ret > 0 &&
  471. dev->dev_attrib.emulate_fua_write > 0 &&
  472. (cmd->se_cmd_flags & SCF_FUA)) {
  473. struct fd_dev *fd_dev = FD_DEV(dev);
  474. loff_t start = cmd->t_task_lba *
  475. dev->dev_attrib.block_size;
  476. loff_t end = start + cmd->data_length;
  477. vfs_fsync_range(fd_dev->fd_file, start, end, 1);
  478. }
  479. }
  480. if (ret < 0)
  481. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  482. if (ret)
  483. target_complete_cmd(cmd, SAM_STAT_GOOD);
  484. return 0;
  485. }
  486. enum {
  487. Opt_fd_dev_name, Opt_fd_dev_size, Opt_fd_buffered_io, Opt_err
  488. };
  489. static match_table_t tokens = {
  490. {Opt_fd_dev_name, "fd_dev_name=%s"},
  491. {Opt_fd_dev_size, "fd_dev_size=%s"},
  492. {Opt_fd_buffered_io, "fd_buffered_io=%d"},
  493. {Opt_err, NULL}
  494. };
  495. static ssize_t fd_set_configfs_dev_params(struct se_device *dev,
  496. const char *page, ssize_t count)
  497. {
  498. struct fd_dev *fd_dev = FD_DEV(dev);
  499. char *orig, *ptr, *arg_p, *opts;
  500. substring_t args[MAX_OPT_ARGS];
  501. int ret = 0, arg, token;
  502. opts = kstrdup(page, GFP_KERNEL);
  503. if (!opts)
  504. return -ENOMEM;
  505. orig = opts;
  506. while ((ptr = strsep(&opts, ",\n")) != NULL) {
  507. if (!*ptr)
  508. continue;
  509. token = match_token(ptr, tokens, args);
  510. switch (token) {
  511. case Opt_fd_dev_name:
  512. if (match_strlcpy(fd_dev->fd_dev_name, &args[0],
  513. FD_MAX_DEV_NAME) == 0) {
  514. ret = -EINVAL;
  515. break;
  516. }
  517. pr_debug("FILEIO: Referencing Path: %s\n",
  518. fd_dev->fd_dev_name);
  519. fd_dev->fbd_flags |= FBDF_HAS_PATH;
  520. break;
  521. case Opt_fd_dev_size:
  522. arg_p = match_strdup(&args[0]);
  523. if (!arg_p) {
  524. ret = -ENOMEM;
  525. break;
  526. }
  527. ret = strict_strtoull(arg_p, 0, &fd_dev->fd_dev_size);
  528. kfree(arg_p);
  529. if (ret < 0) {
  530. pr_err("strict_strtoull() failed for"
  531. " fd_dev_size=\n");
  532. goto out;
  533. }
  534. pr_debug("FILEIO: Referencing Size: %llu"
  535. " bytes\n", fd_dev->fd_dev_size);
  536. fd_dev->fbd_flags |= FBDF_HAS_SIZE;
  537. break;
  538. case Opt_fd_buffered_io:
  539. match_int(args, &arg);
  540. if (arg != 1) {
  541. pr_err("bogus fd_buffered_io=%d value\n", arg);
  542. ret = -EINVAL;
  543. goto out;
  544. }
  545. pr_debug("FILEIO: Using buffered I/O"
  546. " operations for struct fd_dev\n");
  547. fd_dev->fbd_flags |= FDBD_HAS_BUFFERED_IO_WCE;
  548. break;
  549. default:
  550. break;
  551. }
  552. }
  553. out:
  554. kfree(orig);
  555. return (!ret) ? count : ret;
  556. }
  557. static ssize_t fd_show_configfs_dev_params(struct se_device *dev, char *b)
  558. {
  559. struct fd_dev *fd_dev = FD_DEV(dev);
  560. ssize_t bl = 0;
  561. bl = sprintf(b + bl, "TCM FILEIO ID: %u", fd_dev->fd_dev_id);
  562. bl += sprintf(b + bl, " File: %s Size: %llu Mode: %s\n",
  563. fd_dev->fd_dev_name, fd_dev->fd_dev_size,
  564. (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) ?
  565. "Buffered-WCE" : "O_DSYNC");
  566. return bl;
  567. }
  568. static sector_t fd_get_blocks(struct se_device *dev)
  569. {
  570. struct fd_dev *fd_dev = FD_DEV(dev);
  571. struct file *f = fd_dev->fd_file;
  572. struct inode *i = f->f_mapping->host;
  573. unsigned long long dev_size;
  574. /*
  575. * When using a file that references an underlying struct block_device,
  576. * ensure dev_size is always based on the current inode size in order
  577. * to handle underlying block_device resize operations.
  578. */
  579. if (S_ISBLK(i->i_mode))
  580. dev_size = (i_size_read(i) - fd_dev->fd_block_size);
  581. else
  582. dev_size = fd_dev->fd_dev_size;
  583. return div_u64(dev_size, dev->dev_attrib.block_size);
  584. }
  585. static struct sbc_ops fd_sbc_ops = {
  586. .execute_rw = fd_execute_rw,
  587. .execute_sync_cache = fd_execute_sync_cache,
  588. .execute_write_same = fd_execute_write_same,
  589. .execute_write_same_unmap = fd_execute_write_same_unmap,
  590. };
  591. static sense_reason_t
  592. fd_parse_cdb(struct se_cmd *cmd)
  593. {
  594. return sbc_parse_cdb(cmd, &fd_sbc_ops);
  595. }
  596. static struct se_subsystem_api fileio_template = {
  597. .name = "fileio",
  598. .inquiry_prod = "FILEIO",
  599. .inquiry_rev = FD_VERSION,
  600. .owner = THIS_MODULE,
  601. .transport_type = TRANSPORT_PLUGIN_VHBA_PDEV,
  602. .attach_hba = fd_attach_hba,
  603. .detach_hba = fd_detach_hba,
  604. .alloc_device = fd_alloc_device,
  605. .configure_device = fd_configure_device,
  606. .free_device = fd_free_device,
  607. .parse_cdb = fd_parse_cdb,
  608. .set_configfs_dev_params = fd_set_configfs_dev_params,
  609. .show_configfs_dev_params = fd_show_configfs_dev_params,
  610. .get_device_type = sbc_get_device_type,
  611. .get_blocks = fd_get_blocks,
  612. };
  613. static int __init fileio_module_init(void)
  614. {
  615. return transport_subsystem_register(&fileio_template);
  616. }
  617. static void __exit fileio_module_exit(void)
  618. {
  619. transport_subsystem_release(&fileio_template);
  620. }
  621. MODULE_DESCRIPTION("TCM FILEIO subsystem plugin");
  622. MODULE_AUTHOR("nab@Linux-iSCSI.org");
  623. MODULE_LICENSE("GPL");
  624. module_init(fileio_module_init);
  625. module_exit(fileio_module_exit);