target_core_file.c 22 KB

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