target_core_file.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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 <scsi/scsi.h>
  33. #include <scsi/scsi_host.h>
  34. #include <target/target_core_base.h>
  35. #include <target/target_core_backend.h>
  36. #include "target_core_file.h"
  37. static inline struct fd_dev *FD_DEV(struct se_device *dev)
  38. {
  39. return container_of(dev, struct fd_dev, dev);
  40. }
  41. /* fd_attach_hba(): (Part of se_subsystem_api_t template)
  42. *
  43. *
  44. */
  45. static int fd_attach_hba(struct se_hba *hba, u32 host_id)
  46. {
  47. struct fd_host *fd_host;
  48. fd_host = kzalloc(sizeof(struct fd_host), GFP_KERNEL);
  49. if (!fd_host) {
  50. pr_err("Unable to allocate memory for struct fd_host\n");
  51. return -ENOMEM;
  52. }
  53. fd_host->fd_host_id = host_id;
  54. hba->hba_ptr = fd_host;
  55. pr_debug("CORE_HBA[%d] - TCM FILEIO HBA Driver %s on Generic"
  56. " Target Core Stack %s\n", hba->hba_id, FD_VERSION,
  57. TARGET_CORE_MOD_VERSION);
  58. pr_debug("CORE_HBA[%d] - Attached FILEIO HBA: %u to Generic"
  59. " MaxSectors: %u\n",
  60. hba->hba_id, fd_host->fd_host_id, FD_MAX_SECTORS);
  61. return 0;
  62. }
  63. static void fd_detach_hba(struct se_hba *hba)
  64. {
  65. struct fd_host *fd_host = hba->hba_ptr;
  66. pr_debug("CORE_HBA[%d] - Detached FILEIO HBA: %u from Generic"
  67. " Target Core\n", hba->hba_id, fd_host->fd_host_id);
  68. kfree(fd_host);
  69. hba->hba_ptr = NULL;
  70. }
  71. static struct se_device *fd_alloc_device(struct se_hba *hba, const char *name)
  72. {
  73. struct fd_dev *fd_dev;
  74. struct fd_host *fd_host = hba->hba_ptr;
  75. fd_dev = kzalloc(sizeof(struct fd_dev), GFP_KERNEL);
  76. if (!fd_dev) {
  77. pr_err("Unable to allocate memory for struct fd_dev\n");
  78. return NULL;
  79. }
  80. fd_dev->fd_host = fd_host;
  81. pr_debug("FILEIO: Allocated fd_dev for %p\n", name);
  82. return &fd_dev->dev;
  83. }
  84. static int fd_configure_device(struct se_device *dev)
  85. {
  86. struct fd_dev *fd_dev = FD_DEV(dev);
  87. struct fd_host *fd_host = dev->se_hba->hba_ptr;
  88. struct file *file;
  89. struct inode *inode = NULL;
  90. int flags, ret = -EINVAL;
  91. if (!(fd_dev->fbd_flags & FBDF_HAS_PATH)) {
  92. pr_err("Missing fd_dev_name=\n");
  93. return -EINVAL;
  94. }
  95. /*
  96. * Use O_DSYNC by default instead of O_SYNC to forgo syncing
  97. * of pure timestamp updates.
  98. */
  99. flags = O_RDWR | O_CREAT | O_LARGEFILE | O_DSYNC;
  100. /*
  101. * Optionally allow fd_buffered_io=1 to be enabled for people
  102. * who want use the fs buffer cache as an WriteCache mechanism.
  103. *
  104. * This means that in event of a hard failure, there is a risk
  105. * of silent data-loss if the SCSI client has *not* performed a
  106. * forced unit access (FUA) write, or issued SYNCHRONIZE_CACHE
  107. * to write-out the entire device cache.
  108. */
  109. if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) {
  110. pr_debug("FILEIO: Disabling O_DSYNC, using buffered FILEIO\n");
  111. flags &= ~O_DSYNC;
  112. }
  113. file = filp_open(fd_dev->fd_dev_name, flags, 0600);
  114. if (IS_ERR(file)) {
  115. pr_err("filp_open(%s) failed\n", fd_dev->fd_dev_name);
  116. ret = PTR_ERR(file);
  117. goto fail;
  118. }
  119. fd_dev->fd_file = file;
  120. /*
  121. * If using a block backend with this struct file, we extract
  122. * fd_dev->fd_[block,dev]_size from struct block_device.
  123. *
  124. * Otherwise, we use the passed fd_size= from configfs
  125. */
  126. inode = file->f_mapping->host;
  127. if (S_ISBLK(inode->i_mode)) {
  128. struct request_queue *q = bdev_get_queue(inode->i_bdev);
  129. unsigned long long dev_size;
  130. dev->dev_attrib.hw_block_size =
  131. bdev_logical_block_size(inode->i_bdev);
  132. dev->dev_attrib.hw_max_sectors = queue_max_hw_sectors(q);
  133. /*
  134. * Determine the number of bytes from i_size_read() minus
  135. * one (1) logical sector from underlying struct block_device
  136. */
  137. dev_size = (i_size_read(file->f_mapping->host) -
  138. fd_dev->fd_block_size);
  139. pr_debug("FILEIO: Using size: %llu bytes from struct"
  140. " block_device blocks: %llu logical_block_size: %d\n",
  141. dev_size, div_u64(dev_size, fd_dev->fd_block_size),
  142. fd_dev->fd_block_size);
  143. } else {
  144. if (!(fd_dev->fbd_flags & FBDF_HAS_SIZE)) {
  145. pr_err("FILEIO: Missing fd_dev_size="
  146. " parameter, and no backing struct"
  147. " block_device\n");
  148. goto fail;
  149. }
  150. dev->dev_attrib.hw_block_size = FD_BLOCKSIZE;
  151. dev->dev_attrib.hw_max_sectors = FD_MAX_SECTORS;
  152. }
  153. fd_dev->fd_block_size = dev->dev_attrib.hw_block_size;
  154. dev->dev_attrib.hw_queue_depth = FD_MAX_DEVICE_QUEUE_DEPTH;
  155. if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) {
  156. pr_debug("FILEIO: Forcing setting of emulate_write_cache=1"
  157. " with FDBD_HAS_BUFFERED_IO_WCE\n");
  158. dev->dev_attrib.emulate_write_cache = 1;
  159. }
  160. fd_dev->fd_dev_id = fd_host->fd_host_dev_id_count++;
  161. fd_dev->fd_queue_depth = dev->queue_depth;
  162. pr_debug("CORE_FILE[%u] - Added TCM FILEIO Device ID: %u at %s,"
  163. " %llu total bytes\n", fd_host->fd_host_id, fd_dev->fd_dev_id,
  164. fd_dev->fd_dev_name, fd_dev->fd_dev_size);
  165. return 0;
  166. fail:
  167. if (fd_dev->fd_file) {
  168. filp_close(fd_dev->fd_file, NULL);
  169. fd_dev->fd_file = NULL;
  170. }
  171. return ret;
  172. }
  173. static void fd_free_device(struct se_device *dev)
  174. {
  175. struct fd_dev *fd_dev = FD_DEV(dev);
  176. if (fd_dev->fd_file) {
  177. filp_close(fd_dev->fd_file, NULL);
  178. fd_dev->fd_file = NULL;
  179. }
  180. kfree(fd_dev);
  181. }
  182. static int fd_do_rw(struct se_cmd *cmd, struct scatterlist *sgl,
  183. u32 sgl_nents, int is_write)
  184. {
  185. struct se_device *se_dev = cmd->se_dev;
  186. struct fd_dev *dev = FD_DEV(se_dev);
  187. struct file *fd = dev->fd_file;
  188. struct scatterlist *sg;
  189. struct iovec *iov;
  190. mm_segment_t old_fs;
  191. loff_t pos = (cmd->t_task_lba * se_dev->dev_attrib.block_size);
  192. int ret = 0, i;
  193. iov = kzalloc(sizeof(struct iovec) * sgl_nents, GFP_KERNEL);
  194. if (!iov) {
  195. pr_err("Unable to allocate fd_do_readv iov[]\n");
  196. return -ENOMEM;
  197. }
  198. for_each_sg(sgl, sg, sgl_nents, i) {
  199. iov[i].iov_len = sg->length;
  200. iov[i].iov_base = kmap(sg_page(sg)) + sg->offset;
  201. }
  202. old_fs = get_fs();
  203. set_fs(get_ds());
  204. if (is_write)
  205. ret = vfs_writev(fd, &iov[0], sgl_nents, &pos);
  206. else
  207. ret = vfs_readv(fd, &iov[0], sgl_nents, &pos);
  208. set_fs(old_fs);
  209. for_each_sg(sgl, sg, sgl_nents, i)
  210. kunmap(sg_page(sg));
  211. kfree(iov);
  212. if (is_write) {
  213. if (ret < 0 || ret != cmd->data_length) {
  214. pr_err("%s() write returned %d\n", __func__, ret);
  215. return (ret < 0 ? ret : -EINVAL);
  216. }
  217. } else {
  218. /*
  219. * Return zeros and GOOD status even if the READ did not return
  220. * the expected virt_size for struct file w/o a backing struct
  221. * block_device.
  222. */
  223. if (S_ISBLK(fd->f_dentry->d_inode->i_mode)) {
  224. if (ret < 0 || ret != cmd->data_length) {
  225. pr_err("%s() returned %d, expecting %u for "
  226. "S_ISBLK\n", __func__, ret,
  227. cmd->data_length);
  228. return (ret < 0 ? ret : -EINVAL);
  229. }
  230. } else {
  231. if (ret < 0) {
  232. pr_err("%s() returned %d for non S_ISBLK\n",
  233. __func__, ret);
  234. return ret;
  235. }
  236. }
  237. }
  238. return 1;
  239. }
  240. static sense_reason_t
  241. fd_execute_sync_cache(struct se_cmd *cmd)
  242. {
  243. struct se_device *dev = cmd->se_dev;
  244. struct fd_dev *fd_dev = FD_DEV(dev);
  245. int immed = (cmd->t_task_cdb[1] & 0x2);
  246. loff_t start, end;
  247. int ret;
  248. /*
  249. * If the Immediate bit is set, queue up the GOOD response
  250. * for this SYNCHRONIZE_CACHE op
  251. */
  252. if (immed)
  253. target_complete_cmd(cmd, SAM_STAT_GOOD);
  254. /*
  255. * Determine if we will be flushing the entire device.
  256. */
  257. if (cmd->t_task_lba == 0 && cmd->data_length == 0) {
  258. start = 0;
  259. end = LLONG_MAX;
  260. } else {
  261. start = cmd->t_task_lba * dev->dev_attrib.block_size;
  262. if (cmd->data_length)
  263. end = start + cmd->data_length;
  264. else
  265. end = LLONG_MAX;
  266. }
  267. ret = vfs_fsync_range(fd_dev->fd_file, start, end, 1);
  268. if (ret != 0)
  269. pr_err("FILEIO: vfs_fsync_range() failed: %d\n", ret);
  270. if (immed)
  271. return 0;
  272. if (ret)
  273. target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
  274. else
  275. target_complete_cmd(cmd, SAM_STAT_GOOD);
  276. return 0;
  277. }
  278. static sense_reason_t
  279. fd_execute_rw(struct se_cmd *cmd)
  280. {
  281. struct scatterlist *sgl = cmd->t_data_sg;
  282. u32 sgl_nents = cmd->t_data_nents;
  283. enum dma_data_direction data_direction = cmd->data_direction;
  284. struct se_device *dev = cmd->se_dev;
  285. int ret = 0;
  286. /*
  287. * Call vectorized fileio functions to map struct scatterlist
  288. * physical memory addresses to struct iovec virtual memory.
  289. */
  290. if (data_direction == DMA_FROM_DEVICE) {
  291. ret = fd_do_rw(cmd, sgl, sgl_nents, 0);
  292. } else {
  293. ret = fd_do_rw(cmd, sgl, sgl_nents, 1);
  294. /*
  295. * Perform implict vfs_fsync_range() for fd_do_writev() ops
  296. * for SCSI WRITEs with Forced Unit Access (FUA) set.
  297. * Allow this to happen independent of WCE=0 setting.
  298. */
  299. if (ret > 0 &&
  300. dev->dev_attrib.emulate_fua_write > 0 &&
  301. (cmd->se_cmd_flags & SCF_FUA)) {
  302. struct fd_dev *fd_dev = FD_DEV(dev);
  303. loff_t start = cmd->t_task_lba *
  304. dev->dev_attrib.block_size;
  305. loff_t end = start + cmd->data_length;
  306. vfs_fsync_range(fd_dev->fd_file, start, end, 1);
  307. }
  308. }
  309. if (ret < 0)
  310. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  311. if (ret)
  312. target_complete_cmd(cmd, SAM_STAT_GOOD);
  313. return 0;
  314. }
  315. enum {
  316. Opt_fd_dev_name, Opt_fd_dev_size, Opt_fd_buffered_io, Opt_err
  317. };
  318. static match_table_t tokens = {
  319. {Opt_fd_dev_name, "fd_dev_name=%s"},
  320. {Opt_fd_dev_size, "fd_dev_size=%s"},
  321. {Opt_fd_buffered_io, "fd_buffered_io=%d"},
  322. {Opt_err, NULL}
  323. };
  324. static ssize_t fd_set_configfs_dev_params(struct se_device *dev,
  325. const char *page, ssize_t count)
  326. {
  327. struct fd_dev *fd_dev = FD_DEV(dev);
  328. char *orig, *ptr, *arg_p, *opts;
  329. substring_t args[MAX_OPT_ARGS];
  330. int ret = 0, arg, token;
  331. opts = kstrdup(page, GFP_KERNEL);
  332. if (!opts)
  333. return -ENOMEM;
  334. orig = opts;
  335. while ((ptr = strsep(&opts, ",\n")) != NULL) {
  336. if (!*ptr)
  337. continue;
  338. token = match_token(ptr, tokens, args);
  339. switch (token) {
  340. case Opt_fd_dev_name:
  341. if (match_strlcpy(fd_dev->fd_dev_name, &args[0],
  342. FD_MAX_DEV_NAME) == 0) {
  343. ret = -EINVAL;
  344. break;
  345. }
  346. pr_debug("FILEIO: Referencing Path: %s\n",
  347. fd_dev->fd_dev_name);
  348. fd_dev->fbd_flags |= FBDF_HAS_PATH;
  349. break;
  350. case Opt_fd_dev_size:
  351. arg_p = match_strdup(&args[0]);
  352. if (!arg_p) {
  353. ret = -ENOMEM;
  354. break;
  355. }
  356. ret = strict_strtoull(arg_p, 0, &fd_dev->fd_dev_size);
  357. kfree(arg_p);
  358. if (ret < 0) {
  359. pr_err("strict_strtoull() failed for"
  360. " fd_dev_size=\n");
  361. goto out;
  362. }
  363. pr_debug("FILEIO: Referencing Size: %llu"
  364. " bytes\n", fd_dev->fd_dev_size);
  365. fd_dev->fbd_flags |= FBDF_HAS_SIZE;
  366. break;
  367. case Opt_fd_buffered_io:
  368. match_int(args, &arg);
  369. if (arg != 1) {
  370. pr_err("bogus fd_buffered_io=%d value\n", arg);
  371. ret = -EINVAL;
  372. goto out;
  373. }
  374. pr_debug("FILEIO: Using buffered I/O"
  375. " operations for struct fd_dev\n");
  376. fd_dev->fbd_flags |= FDBD_HAS_BUFFERED_IO_WCE;
  377. break;
  378. default:
  379. break;
  380. }
  381. }
  382. out:
  383. kfree(orig);
  384. return (!ret) ? count : ret;
  385. }
  386. static ssize_t fd_show_configfs_dev_params(struct se_device *dev, char *b)
  387. {
  388. struct fd_dev *fd_dev = FD_DEV(dev);
  389. ssize_t bl = 0;
  390. bl = sprintf(b + bl, "TCM FILEIO ID: %u", fd_dev->fd_dev_id);
  391. bl += sprintf(b + bl, " File: %s Size: %llu Mode: %s\n",
  392. fd_dev->fd_dev_name, fd_dev->fd_dev_size,
  393. (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) ?
  394. "Buffered-WCE" : "O_DSYNC");
  395. return bl;
  396. }
  397. static sector_t fd_get_blocks(struct se_device *dev)
  398. {
  399. struct fd_dev *fd_dev = FD_DEV(dev);
  400. struct file *f = fd_dev->fd_file;
  401. struct inode *i = f->f_mapping->host;
  402. unsigned long long dev_size;
  403. /*
  404. * When using a file that references an underlying struct block_device,
  405. * ensure dev_size is always based on the current inode size in order
  406. * to handle underlying block_device resize operations.
  407. */
  408. if (S_ISBLK(i->i_mode))
  409. dev_size = (i_size_read(i) - fd_dev->fd_block_size);
  410. else
  411. dev_size = fd_dev->fd_dev_size;
  412. return div_u64(dev_size, dev->dev_attrib.block_size);
  413. }
  414. static struct sbc_ops fd_sbc_ops = {
  415. .execute_rw = fd_execute_rw,
  416. .execute_sync_cache = fd_execute_sync_cache,
  417. };
  418. static sense_reason_t
  419. fd_parse_cdb(struct se_cmd *cmd)
  420. {
  421. return sbc_parse_cdb(cmd, &fd_sbc_ops);
  422. }
  423. static struct se_subsystem_api fileio_template = {
  424. .name = "fileio",
  425. .inquiry_prod = "FILEIO",
  426. .inquiry_rev = FD_VERSION,
  427. .owner = THIS_MODULE,
  428. .transport_type = TRANSPORT_PLUGIN_VHBA_PDEV,
  429. .attach_hba = fd_attach_hba,
  430. .detach_hba = fd_detach_hba,
  431. .alloc_device = fd_alloc_device,
  432. .configure_device = fd_configure_device,
  433. .free_device = fd_free_device,
  434. .parse_cdb = fd_parse_cdb,
  435. .set_configfs_dev_params = fd_set_configfs_dev_params,
  436. .show_configfs_dev_params = fd_show_configfs_dev_params,
  437. .get_device_type = sbc_get_device_type,
  438. .get_blocks = fd_get_blocks,
  439. };
  440. static int __init fileio_module_init(void)
  441. {
  442. return transport_subsystem_register(&fileio_template);
  443. }
  444. static void fileio_module_exit(void)
  445. {
  446. transport_subsystem_release(&fileio_template);
  447. }
  448. MODULE_DESCRIPTION("TCM FILEIO subsystem plugin");
  449. MODULE_AUTHOR("nab@Linux-iSCSI.org");
  450. MODULE_LICENSE("GPL");
  451. module_init(fileio_module_init);
  452. module_exit(fileio_module_exit);