target_core_file.c 24 KB

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