target_core_file.c 25 KB

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