target_core_file.c 22 KB

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