target_core_file.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*******************************************************************************
  3. * Filename: target_core_file.c
  4. *
  5. * This file contains the Storage Engine <-> FILEIO transport specific functions
  6. *
  7. * (c) Copyright 2005-2013 Datera, Inc.
  8. *
  9. * Nicholas A. Bellinger <nab@kernel.org>
  10. *
  11. ******************************************************************************/
  12. #include <linux/string.h>
  13. #include <linux/parser.h>
  14. #include <linux/timer.h>
  15. #include <linux/blkdev.h>
  16. #include <linux/slab.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/module.h>
  19. #include <linux/vmalloc.h>
  20. #include <linux/falloc.h>
  21. #include <linux/uio.h>
  22. #include <linux/scatterlist.h>
  23. #include <scsi/scsi_proto.h>
  24. #include <linux/unaligned.h>
  25. #include <target/target_core_base.h>
  26. #include <target/target_core_backend.h>
  27. #include "target_core_file.h"
  28. static inline struct fd_dev *FD_DEV(struct se_device *dev)
  29. {
  30. return container_of(dev, struct fd_dev, dev);
  31. }
  32. static int fd_attach_hba(struct se_hba *hba, u32 host_id)
  33. {
  34. struct fd_host *fd_host;
  35. fd_host = kzalloc(sizeof(struct fd_host), GFP_KERNEL);
  36. if (!fd_host) {
  37. pr_err("Unable to allocate memory for struct fd_host\n");
  38. return -ENOMEM;
  39. }
  40. fd_host->fd_host_id = host_id;
  41. hba->hba_ptr = fd_host;
  42. pr_debug("CORE_HBA[%d] - TCM FILEIO HBA Driver %s on Generic"
  43. " Target Core Stack %s\n", hba->hba_id, FD_VERSION,
  44. TARGET_CORE_VERSION);
  45. pr_debug("CORE_HBA[%d] - Attached FILEIO HBA: %u to Generic\n",
  46. hba->hba_id, fd_host->fd_host_id);
  47. return 0;
  48. }
  49. static void fd_detach_hba(struct se_hba *hba)
  50. {
  51. struct fd_host *fd_host = hba->hba_ptr;
  52. pr_debug("CORE_HBA[%d] - Detached FILEIO HBA: %u from Generic"
  53. " Target Core\n", hba->hba_id, fd_host->fd_host_id);
  54. kfree(fd_host);
  55. hba->hba_ptr = NULL;
  56. }
  57. static struct se_device *fd_alloc_device(struct se_hba *hba, const char *name)
  58. {
  59. struct fd_dev *fd_dev;
  60. struct fd_host *fd_host = hba->hba_ptr;
  61. fd_dev = kzalloc(sizeof(struct fd_dev), GFP_KERNEL);
  62. if (!fd_dev) {
  63. pr_err("Unable to allocate memory for struct fd_dev\n");
  64. return NULL;
  65. }
  66. fd_dev->fd_host = fd_host;
  67. pr_debug("FILEIO: Allocated fd_dev for %p\n", name);
  68. return &fd_dev->dev;
  69. }
  70. static bool fd_configure_unmap(struct se_device *dev)
  71. {
  72. struct file *file = FD_DEV(dev)->fd_file;
  73. struct inode *inode = file->f_mapping->host;
  74. if (S_ISBLK(inode->i_mode))
  75. return target_configure_unmap_from_queue(&dev->dev_attrib,
  76. I_BDEV(inode));
  77. /* Limit UNMAP emulation to 8k Number of LBAs (NoLB) */
  78. dev->dev_attrib.max_unmap_lba_count = 0x2000;
  79. /* Currently hardcoded to 1 in Linux/SCSI code. */
  80. dev->dev_attrib.max_unmap_block_desc_count = 1;
  81. dev->dev_attrib.unmap_granularity = 1;
  82. dev->dev_attrib.unmap_granularity_alignment = 0;
  83. return true;
  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 block_device *bdev = I_BDEV(inode);
  130. unsigned long long dev_size;
  131. fd_dev->fd_block_size = bdev_logical_block_size(bdev);
  132. /*
  133. * Determine the number of bytes from i_size_read() minus
  134. * one (1) logical sector from underlying struct block_device
  135. */
  136. dev_size = (i_size_read(file->f_mapping->host) -
  137. fd_dev->fd_block_size);
  138. pr_debug("FILEIO: Using size: %llu bytes from struct"
  139. " block_device blocks: %llu logical_block_size: %d\n",
  140. dev_size, div_u64(dev_size, fd_dev->fd_block_size),
  141. fd_dev->fd_block_size);
  142. /*
  143. * Enable write same emulation for IBLOCK and use 0xFFFF as
  144. * the smaller WRITE_SAME(10) only has a two-byte block count.
  145. */
  146. dev->dev_attrib.max_write_same_len = 0xFFFF;
  147. if (bdev_nonrot(bdev))
  148. dev->dev_attrib.is_nonrot = 1;
  149. } else {
  150. if (!(fd_dev->fbd_flags & FBDF_HAS_SIZE)) {
  151. pr_err("FILEIO: Missing fd_dev_size="
  152. " parameter, and no backing struct"
  153. " block_device\n");
  154. goto fail;
  155. }
  156. fd_dev->fd_block_size = FD_BLOCKSIZE;
  157. /*
  158. * Limit WRITE_SAME w/ UNMAP=0 emulation to 8k Number of LBAs (NoLB)
  159. * based upon struct iovec limit for vfs_writev()
  160. */
  161. dev->dev_attrib.max_write_same_len = 0x1000;
  162. }
  163. dev->dev_attrib.hw_block_size = fd_dev->fd_block_size;
  164. dev->dev_attrib.hw_max_sectors = FD_MAX_BYTES / fd_dev->fd_block_size;
  165. dev->dev_attrib.hw_queue_depth = FD_MAX_DEVICE_QUEUE_DEPTH;
  166. if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) {
  167. pr_debug("FILEIO: Forcing setting of emulate_write_cache=1"
  168. " with FDBD_HAS_BUFFERED_IO_WCE\n");
  169. dev->dev_attrib.emulate_write_cache = 1;
  170. }
  171. fd_dev->fd_dev_id = fd_host->fd_host_dev_id_count++;
  172. fd_dev->fd_queue_depth = dev->queue_depth;
  173. pr_debug("CORE_FILE[%u] - Added TCM FILEIO Device ID: %u at %s,"
  174. " %llu total bytes\n", fd_host->fd_host_id, fd_dev->fd_dev_id,
  175. fd_dev->fd_dev_name, fd_dev->fd_dev_size);
  176. return 0;
  177. fail:
  178. if (fd_dev->fd_file) {
  179. filp_close(fd_dev->fd_file, NULL);
  180. fd_dev->fd_file = NULL;
  181. }
  182. return ret;
  183. }
  184. static void fd_dev_call_rcu(struct rcu_head *p)
  185. {
  186. struct se_device *dev = container_of(p, struct se_device, rcu_head);
  187. struct fd_dev *fd_dev = FD_DEV(dev);
  188. kfree(fd_dev);
  189. }
  190. static void fd_free_device(struct se_device *dev)
  191. {
  192. call_rcu(&dev->rcu_head, fd_dev_call_rcu);
  193. }
  194. static void fd_destroy_device(struct se_device *dev)
  195. {
  196. struct fd_dev *fd_dev = FD_DEV(dev);
  197. if (fd_dev->fd_file) {
  198. filp_close(fd_dev->fd_file, NULL);
  199. fd_dev->fd_file = NULL;
  200. }
  201. }
  202. struct target_core_file_cmd {
  203. unsigned long len;
  204. struct se_cmd *cmd;
  205. struct kiocb iocb;
  206. struct bio_vec bvecs[];
  207. };
  208. static void cmd_rw_aio_complete(struct kiocb *iocb, long ret)
  209. {
  210. struct target_core_file_cmd *cmd;
  211. cmd = container_of(iocb, struct target_core_file_cmd, iocb);
  212. if (ret != cmd->len)
  213. target_complete_cmd(cmd->cmd, SAM_STAT_CHECK_CONDITION);
  214. else
  215. target_complete_cmd(cmd->cmd, SAM_STAT_GOOD);
  216. kfree(cmd);
  217. }
  218. static sense_reason_t
  219. fd_execute_rw_aio(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
  220. enum dma_data_direction data_direction)
  221. {
  222. int is_write = !(data_direction == DMA_FROM_DEVICE);
  223. struct se_device *dev = cmd->se_dev;
  224. struct fd_dev *fd_dev = FD_DEV(dev);
  225. struct file *file = fd_dev->fd_file;
  226. struct target_core_file_cmd *aio_cmd;
  227. struct iov_iter iter;
  228. struct scatterlist *sg;
  229. ssize_t len = 0;
  230. int ret = 0, i;
  231. aio_cmd = kmalloc(struct_size(aio_cmd, bvecs, sgl_nents), GFP_KERNEL);
  232. if (!aio_cmd)
  233. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  234. for_each_sg(sgl, sg, sgl_nents, i) {
  235. bvec_set_page(&aio_cmd->bvecs[i], sg_page(sg), sg->length,
  236. sg->offset);
  237. len += sg->length;
  238. }
  239. iov_iter_bvec(&iter, is_write, aio_cmd->bvecs, sgl_nents, len);
  240. aio_cmd->cmd = cmd;
  241. aio_cmd->len = len;
  242. aio_cmd->iocb.ki_pos = cmd->t_task_lba * dev->dev_attrib.block_size;
  243. aio_cmd->iocb.ki_filp = file;
  244. aio_cmd->iocb.ki_complete = cmd_rw_aio_complete;
  245. aio_cmd->iocb.ki_flags = IOCB_DIRECT;
  246. if (is_write && (cmd->se_cmd_flags & SCF_FUA))
  247. aio_cmd->iocb.ki_flags |= IOCB_DSYNC;
  248. if (is_write)
  249. ret = file->f_op->write_iter(&aio_cmd->iocb, &iter);
  250. else
  251. ret = file->f_op->read_iter(&aio_cmd->iocb, &iter);
  252. if (ret != -EIOCBQUEUED)
  253. cmd_rw_aio_complete(&aio_cmd->iocb, ret);
  254. return 0;
  255. }
  256. static int fd_do_rw(struct se_cmd *cmd, struct file *fd,
  257. u32 block_size, struct scatterlist *sgl,
  258. u32 sgl_nents, u32 data_length, int is_write)
  259. {
  260. struct scatterlist *sg;
  261. struct iov_iter iter;
  262. struct bio_vec *bvec;
  263. ssize_t len = 0;
  264. loff_t pos = (cmd->t_task_lba * block_size);
  265. int ret = 0, i;
  266. bvec = kcalloc(sgl_nents, sizeof(struct bio_vec), GFP_KERNEL);
  267. if (!bvec) {
  268. pr_err("Unable to allocate fd_do_readv iov[]\n");
  269. return -ENOMEM;
  270. }
  271. for_each_sg(sgl, sg, sgl_nents, i) {
  272. bvec_set_page(&bvec[i], sg_page(sg), sg->length, sg->offset);
  273. len += sg->length;
  274. }
  275. iov_iter_bvec(&iter, is_write, bvec, sgl_nents, len);
  276. if (is_write)
  277. ret = vfs_iter_write(fd, &iter, &pos, 0);
  278. else
  279. ret = vfs_iter_read(fd, &iter, &pos, 0);
  280. if (is_write) {
  281. if (ret < 0 || ret != data_length) {
  282. pr_err("%s() write returned %d\n", __func__, ret);
  283. if (ret >= 0)
  284. ret = -EINVAL;
  285. }
  286. } else {
  287. /*
  288. * Return zeros and GOOD status even if the READ did not return
  289. * the expected virt_size for struct file w/o a backing struct
  290. * block_device.
  291. */
  292. if (S_ISBLK(file_inode(fd)->i_mode)) {
  293. if (ret < 0 || ret != data_length) {
  294. pr_err("%s() returned %d, expecting %u for "
  295. "S_ISBLK\n", __func__, ret,
  296. data_length);
  297. if (ret >= 0)
  298. ret = -EINVAL;
  299. }
  300. } else {
  301. if (ret < 0) {
  302. pr_err("%s() returned %d for non S_ISBLK\n",
  303. __func__, ret);
  304. } else if (ret != data_length) {
  305. /*
  306. * Short read case:
  307. * Probably some one truncate file under us.
  308. * We must explicitly zero sg-pages to prevent
  309. * expose uninizialized pages to userspace.
  310. */
  311. if (ret < data_length)
  312. ret += iov_iter_zero(data_length - ret, &iter);
  313. else
  314. ret = -EINVAL;
  315. }
  316. }
  317. }
  318. kfree(bvec);
  319. return ret;
  320. }
  321. static sense_reason_t
  322. fd_execute_sync_cache(struct se_cmd *cmd)
  323. {
  324. struct se_device *dev = cmd->se_dev;
  325. struct fd_dev *fd_dev = FD_DEV(dev);
  326. int immed = (cmd->t_task_cdb[1] & 0x2);
  327. loff_t start, end;
  328. int ret;
  329. /*
  330. * If the Immediate bit is set, queue up the GOOD response
  331. * for this SYNCHRONIZE_CACHE op
  332. */
  333. if (immed)
  334. target_complete_cmd(cmd, SAM_STAT_GOOD);
  335. /*
  336. * Determine if we will be flushing the entire device.
  337. */
  338. if (cmd->t_task_lba == 0 && cmd->data_length == 0) {
  339. start = 0;
  340. end = LLONG_MAX;
  341. } else {
  342. start = cmd->t_task_lba * dev->dev_attrib.block_size;
  343. if (cmd->data_length)
  344. end = start + cmd->data_length - 1;
  345. else
  346. end = LLONG_MAX;
  347. }
  348. ret = vfs_fsync_range(fd_dev->fd_file, start, end, 1);
  349. if (ret != 0)
  350. pr_err("FILEIO: vfs_fsync_range() failed: %d\n", ret);
  351. if (immed)
  352. return 0;
  353. if (ret)
  354. target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
  355. else
  356. target_complete_cmd(cmd, SAM_STAT_GOOD);
  357. return 0;
  358. }
  359. static sense_reason_t
  360. fd_execute_write_same(struct se_cmd *cmd)
  361. {
  362. struct se_device *se_dev = cmd->se_dev;
  363. struct fd_dev *fd_dev = FD_DEV(se_dev);
  364. loff_t pos = cmd->t_task_lba * se_dev->dev_attrib.block_size;
  365. sector_t nolb = sbc_get_write_same_sectors(cmd);
  366. struct iov_iter iter;
  367. struct bio_vec *bvec;
  368. unsigned int len = 0, i;
  369. ssize_t ret;
  370. if (cmd->prot_op) {
  371. pr_err("WRITE_SAME: Protection information with FILEIO"
  372. " backends not supported\n");
  373. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  374. }
  375. if (!cmd->t_data_nents)
  376. return TCM_INVALID_CDB_FIELD;
  377. if (cmd->t_data_nents > 1 ||
  378. cmd->t_data_sg[0].length != cmd->se_dev->dev_attrib.block_size) {
  379. pr_err("WRITE_SAME: Illegal SGL t_data_nents: %u length: %u"
  380. " block_size: %u\n",
  381. cmd->t_data_nents,
  382. cmd->t_data_sg[0].length,
  383. cmd->se_dev->dev_attrib.block_size);
  384. return TCM_INVALID_CDB_FIELD;
  385. }
  386. bvec = kcalloc(nolb, sizeof(struct bio_vec), GFP_KERNEL);
  387. if (!bvec)
  388. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  389. for (i = 0; i < nolb; i++) {
  390. bvec_set_page(&bvec[i], sg_page(&cmd->t_data_sg[0]),
  391. cmd->t_data_sg[0].length,
  392. cmd->t_data_sg[0].offset);
  393. len += se_dev->dev_attrib.block_size;
  394. }
  395. iov_iter_bvec(&iter, ITER_SOURCE, bvec, nolb, len);
  396. ret = vfs_iter_write(fd_dev->fd_file, &iter, &pos, 0);
  397. kfree(bvec);
  398. if (ret < 0 || ret != len) {
  399. pr_err("vfs_iter_write() returned %zd for write same\n", ret);
  400. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  401. }
  402. target_complete_cmd(cmd, SAM_STAT_GOOD);
  403. return 0;
  404. }
  405. static int
  406. fd_do_prot_fill(struct se_device *se_dev, sector_t lba, sector_t nolb,
  407. void *buf, size_t bufsize)
  408. {
  409. struct fd_dev *fd_dev = FD_DEV(se_dev);
  410. struct file *prot_fd = fd_dev->fd_prot_file;
  411. sector_t prot_length, prot;
  412. loff_t pos = lba * se_dev->prot_length;
  413. if (!prot_fd) {
  414. pr_err("Unable to locate fd_dev->fd_prot_file\n");
  415. return -ENODEV;
  416. }
  417. prot_length = nolb * se_dev->prot_length;
  418. memset(buf, 0xff, bufsize);
  419. for (prot = 0; prot < prot_length;) {
  420. sector_t len = min_t(sector_t, bufsize, prot_length - prot);
  421. ssize_t ret = kernel_write(prot_fd, buf, len, &pos);
  422. if (ret != len) {
  423. pr_err("vfs_write to prot file failed: %zd\n", ret);
  424. return ret < 0 ? ret : -ENODEV;
  425. }
  426. prot += ret;
  427. }
  428. return 0;
  429. }
  430. static int
  431. fd_do_prot_unmap(struct se_cmd *cmd, sector_t lba, sector_t nolb)
  432. {
  433. void *buf;
  434. int rc;
  435. buf = (void *)__get_free_page(GFP_KERNEL);
  436. if (!buf) {
  437. pr_err("Unable to allocate FILEIO prot buf\n");
  438. return -ENOMEM;
  439. }
  440. rc = fd_do_prot_fill(cmd->se_dev, lba, nolb, buf, PAGE_SIZE);
  441. free_page((unsigned long)buf);
  442. return rc;
  443. }
  444. static sense_reason_t
  445. fd_execute_unmap(struct se_cmd *cmd, sector_t lba, sector_t nolb)
  446. {
  447. struct file *file = FD_DEV(cmd->se_dev)->fd_file;
  448. struct inode *inode = file->f_mapping->host;
  449. int ret;
  450. if (!nolb) {
  451. return 0;
  452. }
  453. if (cmd->se_dev->dev_attrib.pi_prot_type) {
  454. ret = fd_do_prot_unmap(cmd, lba, nolb);
  455. if (ret)
  456. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  457. }
  458. if (S_ISBLK(inode->i_mode)) {
  459. /* The backend is block device, use discard */
  460. struct block_device *bdev = I_BDEV(inode);
  461. struct se_device *dev = cmd->se_dev;
  462. ret = blkdev_issue_discard(bdev,
  463. target_to_linux_sector(dev, lba),
  464. target_to_linux_sector(dev, nolb),
  465. GFP_KERNEL);
  466. if (ret < 0) {
  467. pr_warn("FILEIO: blkdev_issue_discard() failed: %d\n",
  468. ret);
  469. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  470. }
  471. } else {
  472. /* The backend is normal file, use fallocate */
  473. struct se_device *se_dev = cmd->se_dev;
  474. loff_t pos = lba * se_dev->dev_attrib.block_size;
  475. unsigned int len = nolb * se_dev->dev_attrib.block_size;
  476. int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
  477. if (!file->f_op->fallocate)
  478. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  479. ret = file->f_op->fallocate(file, mode, pos, len);
  480. if (ret < 0) {
  481. pr_warn("FILEIO: fallocate() failed: %d\n", ret);
  482. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  483. }
  484. }
  485. return 0;
  486. }
  487. static sense_reason_t
  488. fd_execute_rw_buffered(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
  489. enum dma_data_direction data_direction)
  490. {
  491. struct se_device *dev = cmd->se_dev;
  492. struct fd_dev *fd_dev = FD_DEV(dev);
  493. struct file *file = fd_dev->fd_file;
  494. struct file *pfile = fd_dev->fd_prot_file;
  495. sense_reason_t rc;
  496. int ret = 0;
  497. /*
  498. * Call vectorized fileio functions to map struct scatterlist
  499. * physical memory addresses to struct iovec virtual memory.
  500. */
  501. if (data_direction == DMA_FROM_DEVICE) {
  502. if (cmd->prot_type && dev->dev_attrib.pi_prot_type) {
  503. ret = fd_do_rw(cmd, pfile, dev->prot_length,
  504. cmd->t_prot_sg, cmd->t_prot_nents,
  505. cmd->prot_length, 0);
  506. if (ret < 0)
  507. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  508. }
  509. ret = fd_do_rw(cmd, file, dev->dev_attrib.block_size,
  510. sgl, sgl_nents, cmd->data_length, 0);
  511. if (ret > 0 && cmd->prot_type && dev->dev_attrib.pi_prot_type &&
  512. dev->dev_attrib.pi_prot_verify) {
  513. u32 sectors = cmd->data_length >>
  514. ilog2(dev->dev_attrib.block_size);
  515. rc = sbc_dif_verify(cmd, cmd->t_task_lba, sectors,
  516. 0, cmd->t_prot_sg, 0);
  517. if (rc)
  518. return rc;
  519. }
  520. } else {
  521. if (cmd->prot_type && dev->dev_attrib.pi_prot_type &&
  522. dev->dev_attrib.pi_prot_verify) {
  523. u32 sectors = cmd->data_length >>
  524. ilog2(dev->dev_attrib.block_size);
  525. rc = sbc_dif_verify(cmd, cmd->t_task_lba, sectors,
  526. 0, cmd->t_prot_sg, 0);
  527. if (rc)
  528. return rc;
  529. }
  530. ret = fd_do_rw(cmd, file, dev->dev_attrib.block_size,
  531. sgl, sgl_nents, cmd->data_length, 1);
  532. /*
  533. * Perform implicit vfs_fsync_range() for fd_do_writev() ops
  534. * for SCSI WRITEs with Forced Unit Access (FUA) set.
  535. * Allow this to happen independent of WCE=0 setting.
  536. */
  537. if (ret > 0 && (cmd->se_cmd_flags & SCF_FUA)) {
  538. loff_t start = cmd->t_task_lba *
  539. dev->dev_attrib.block_size;
  540. loff_t end;
  541. if (cmd->data_length)
  542. end = start + cmd->data_length - 1;
  543. else
  544. end = LLONG_MAX;
  545. vfs_fsync_range(fd_dev->fd_file, start, end, 1);
  546. }
  547. if (ret > 0 && cmd->prot_type && dev->dev_attrib.pi_prot_type) {
  548. ret = fd_do_rw(cmd, pfile, dev->prot_length,
  549. cmd->t_prot_sg, cmd->t_prot_nents,
  550. cmd->prot_length, 1);
  551. if (ret < 0)
  552. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  553. }
  554. }
  555. if (ret < 0)
  556. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  557. target_complete_cmd(cmd, SAM_STAT_GOOD);
  558. return 0;
  559. }
  560. static sense_reason_t
  561. fd_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
  562. enum dma_data_direction data_direction)
  563. {
  564. struct se_device *dev = cmd->se_dev;
  565. struct fd_dev *fd_dev = FD_DEV(dev);
  566. /*
  567. * We are currently limited by the number of iovecs (2048) per
  568. * single vfs_[writev,readv] call.
  569. */
  570. if (cmd->data_length > FD_MAX_BYTES) {
  571. pr_err("FILEIO: Not able to process I/O of %u bytes due to"
  572. "FD_MAX_BYTES: %u iovec count limitation\n",
  573. cmd->data_length, FD_MAX_BYTES);
  574. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  575. }
  576. if (fd_dev->fbd_flags & FDBD_HAS_ASYNC_IO)
  577. return fd_execute_rw_aio(cmd, sgl, sgl_nents, data_direction);
  578. return fd_execute_rw_buffered(cmd, sgl, sgl_nents, data_direction);
  579. }
  580. enum {
  581. Opt_fd_dev_name, Opt_fd_dev_size, Opt_fd_buffered_io,
  582. Opt_fd_async_io, Opt_err
  583. };
  584. static match_table_t tokens = {
  585. {Opt_fd_dev_name, "fd_dev_name=%s"},
  586. {Opt_fd_dev_size, "fd_dev_size=%s"},
  587. {Opt_fd_buffered_io, "fd_buffered_io=%d"},
  588. {Opt_fd_async_io, "fd_async_io=%d"},
  589. {Opt_err, NULL}
  590. };
  591. static ssize_t fd_set_configfs_dev_params(struct se_device *dev,
  592. const char *page, ssize_t count)
  593. {
  594. struct fd_dev *fd_dev = FD_DEV(dev);
  595. char *orig, *ptr, *arg_p, *opts;
  596. substring_t args[MAX_OPT_ARGS];
  597. int ret = 0, arg, token;
  598. opts = kstrdup(page, GFP_KERNEL);
  599. if (!opts)
  600. return -ENOMEM;
  601. orig = opts;
  602. while ((ptr = strsep(&opts, ",\n")) != NULL) {
  603. if (!*ptr)
  604. continue;
  605. token = match_token(ptr, tokens, args);
  606. switch (token) {
  607. case Opt_fd_dev_name:
  608. if (match_strlcpy(fd_dev->fd_dev_name, &args[0],
  609. FD_MAX_DEV_NAME) == 0) {
  610. ret = -EINVAL;
  611. break;
  612. }
  613. pr_debug("FILEIO: Referencing Path: %s\n",
  614. fd_dev->fd_dev_name);
  615. fd_dev->fbd_flags |= FBDF_HAS_PATH;
  616. break;
  617. case Opt_fd_dev_size:
  618. arg_p = match_strdup(&args[0]);
  619. if (!arg_p) {
  620. ret = -ENOMEM;
  621. break;
  622. }
  623. ret = kstrtoull(arg_p, 0, &fd_dev->fd_dev_size);
  624. kfree(arg_p);
  625. if (ret < 0) {
  626. pr_err("kstrtoull() failed for"
  627. " fd_dev_size=\n");
  628. goto out;
  629. }
  630. pr_debug("FILEIO: Referencing Size: %llu"
  631. " bytes\n", fd_dev->fd_dev_size);
  632. fd_dev->fbd_flags |= FBDF_HAS_SIZE;
  633. break;
  634. case Opt_fd_buffered_io:
  635. ret = match_int(args, &arg);
  636. if (ret)
  637. goto out;
  638. if (arg != 1) {
  639. pr_err("bogus fd_buffered_io=%d value\n", arg);
  640. ret = -EINVAL;
  641. goto out;
  642. }
  643. pr_debug("FILEIO: Using buffered I/O"
  644. " operations for struct fd_dev\n");
  645. fd_dev->fbd_flags |= FDBD_HAS_BUFFERED_IO_WCE;
  646. break;
  647. case Opt_fd_async_io:
  648. ret = match_int(args, &arg);
  649. if (ret)
  650. goto out;
  651. if (arg != 1) {
  652. pr_err("bogus fd_async_io=%d value\n", arg);
  653. ret = -EINVAL;
  654. goto out;
  655. }
  656. pr_debug("FILEIO: Using async I/O"
  657. " operations for struct fd_dev\n");
  658. fd_dev->fbd_flags |= FDBD_HAS_ASYNC_IO;
  659. break;
  660. default:
  661. break;
  662. }
  663. }
  664. out:
  665. kfree(orig);
  666. return (!ret) ? count : ret;
  667. }
  668. static ssize_t fd_show_configfs_dev_params(struct se_device *dev, char *b)
  669. {
  670. struct fd_dev *fd_dev = FD_DEV(dev);
  671. ssize_t bl = 0;
  672. bl = sprintf(b + bl, "TCM FILEIO ID: %u", fd_dev->fd_dev_id);
  673. bl += sprintf(b + bl, " File: %s Size: %llu Mode: %s Async: %d\n",
  674. fd_dev->fd_dev_name, fd_dev->fd_dev_size,
  675. (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) ?
  676. "Buffered-WCE" : "O_DSYNC",
  677. !!(fd_dev->fbd_flags & FDBD_HAS_ASYNC_IO));
  678. return bl;
  679. }
  680. static sector_t fd_get_blocks(struct se_device *dev)
  681. {
  682. struct fd_dev *fd_dev = FD_DEV(dev);
  683. struct file *f = fd_dev->fd_file;
  684. struct inode *i = f->f_mapping->host;
  685. unsigned long long dev_size;
  686. /*
  687. * When using a file that references an underlying struct block_device,
  688. * ensure dev_size is always based on the current inode size in order
  689. * to handle underlying block_device resize operations.
  690. */
  691. if (S_ISBLK(i->i_mode))
  692. dev_size = i_size_read(i);
  693. else
  694. dev_size = fd_dev->fd_dev_size;
  695. return div_u64(dev_size - dev->dev_attrib.block_size,
  696. dev->dev_attrib.block_size);
  697. }
  698. static int fd_init_prot(struct se_device *dev)
  699. {
  700. struct fd_dev *fd_dev = FD_DEV(dev);
  701. struct file *prot_file, *file = fd_dev->fd_file;
  702. struct inode *inode;
  703. int ret, flags = O_RDWR | O_CREAT | O_LARGEFILE | O_DSYNC;
  704. char buf[FD_MAX_DEV_PROT_NAME];
  705. if (!file) {
  706. pr_err("Unable to locate fd_dev->fd_file\n");
  707. return -ENODEV;
  708. }
  709. inode = file->f_mapping->host;
  710. if (S_ISBLK(inode->i_mode)) {
  711. pr_err("FILEIO Protection emulation only supported on"
  712. " !S_ISBLK\n");
  713. return -ENOSYS;
  714. }
  715. if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE)
  716. flags &= ~O_DSYNC;
  717. snprintf(buf, FD_MAX_DEV_PROT_NAME, "%s.protection",
  718. fd_dev->fd_dev_name);
  719. prot_file = filp_open(buf, flags, 0600);
  720. if (IS_ERR(prot_file)) {
  721. pr_err("filp_open(%s) failed\n", buf);
  722. ret = PTR_ERR(prot_file);
  723. return ret;
  724. }
  725. fd_dev->fd_prot_file = prot_file;
  726. return 0;
  727. }
  728. static int fd_format_prot(struct se_device *dev)
  729. {
  730. unsigned char *buf;
  731. int unit_size = FDBD_FORMAT_UNIT_SIZE * dev->dev_attrib.block_size;
  732. int ret;
  733. if (!dev->dev_attrib.pi_prot_type) {
  734. pr_err("Unable to format_prot while pi_prot_type == 0\n");
  735. return -ENODEV;
  736. }
  737. buf = vzalloc(unit_size);
  738. if (!buf) {
  739. pr_err("Unable to allocate FILEIO prot buf\n");
  740. return -ENOMEM;
  741. }
  742. pr_debug("Using FILEIO prot_length: %llu\n",
  743. (unsigned long long)(dev->transport->get_blocks(dev) + 1) *
  744. dev->prot_length);
  745. ret = fd_do_prot_fill(dev, 0, dev->transport->get_blocks(dev) + 1,
  746. buf, unit_size);
  747. vfree(buf);
  748. return ret;
  749. }
  750. static void fd_free_prot(struct se_device *dev)
  751. {
  752. struct fd_dev *fd_dev = FD_DEV(dev);
  753. if (!fd_dev->fd_prot_file)
  754. return;
  755. filp_close(fd_dev->fd_prot_file, NULL);
  756. fd_dev->fd_prot_file = NULL;
  757. }
  758. static struct exec_cmd_ops fd_exec_cmd_ops = {
  759. .execute_rw = fd_execute_rw,
  760. .execute_sync_cache = fd_execute_sync_cache,
  761. .execute_write_same = fd_execute_write_same,
  762. .execute_unmap = fd_execute_unmap,
  763. };
  764. static sense_reason_t
  765. fd_parse_cdb(struct se_cmd *cmd)
  766. {
  767. return sbc_parse_cdb(cmd, &fd_exec_cmd_ops);
  768. }
  769. static const struct target_backend_ops fileio_ops = {
  770. .name = "fileio",
  771. .inquiry_prod = "FILEIO",
  772. .inquiry_rev = FD_VERSION,
  773. .owner = THIS_MODULE,
  774. .attach_hba = fd_attach_hba,
  775. .detach_hba = fd_detach_hba,
  776. .alloc_device = fd_alloc_device,
  777. .configure_device = fd_configure_device,
  778. .destroy_device = fd_destroy_device,
  779. .free_device = fd_free_device,
  780. .configure_unmap = fd_configure_unmap,
  781. .parse_cdb = fd_parse_cdb,
  782. .set_configfs_dev_params = fd_set_configfs_dev_params,
  783. .show_configfs_dev_params = fd_show_configfs_dev_params,
  784. .get_device_type = sbc_get_device_type,
  785. .get_blocks = fd_get_blocks,
  786. .init_prot = fd_init_prot,
  787. .format_prot = fd_format_prot,
  788. .free_prot = fd_free_prot,
  789. .tb_dev_attrib_attrs = sbc_attrib_attrs,
  790. };
  791. static int __init fileio_module_init(void)
  792. {
  793. return transport_backend_register(&fileio_ops);
  794. }
  795. static void __exit fileio_module_exit(void)
  796. {
  797. target_backend_unregister(&fileio_ops);
  798. }
  799. MODULE_DESCRIPTION("TCM FILEIO subsystem plugin");
  800. MODULE_AUTHOR("nab@Linux-iSCSI.org");
  801. MODULE_LICENSE("GPL");
  802. module_init(fileio_module_init);
  803. module_exit(fileio_module_exit);