target_core_file.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /*******************************************************************************
  2. * Filename: target_core_file.c
  3. *
  4. * This file contains the Storage Engine <-> FILEIO transport specific functions
  5. *
  6. * Copyright (c) 2005 PyX Technologies, Inc.
  7. * Copyright (c) 2005-2006 SBE, Inc. All Rights Reserved.
  8. * Copyright (c) 2007-2010 Rising Tide Systems
  9. * Copyright (c) 2008-2010 Linux-iSCSI.org
  10. *
  11. * Nicholas A. Bellinger <nab@kernel.org>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  26. *
  27. ******************************************************************************/
  28. #include <linux/string.h>
  29. #include <linux/parser.h>
  30. #include <linux/timer.h>
  31. #include <linux/blkdev.h>
  32. #include <linux/slab.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/module.h>
  35. #include <scsi/scsi.h>
  36. #include <scsi/scsi_host.h>
  37. #include <target/target_core_base.h>
  38. #include <target/target_core_backend.h>
  39. #include "target_core_file.h"
  40. static struct se_subsystem_api fileio_template;
  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 void *fd_allocate_virtdevice(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;
  83. }
  84. /* fd_create_virtdevice(): (Part of se_subsystem_api_t template)
  85. *
  86. *
  87. */
  88. static struct se_device *fd_create_virtdevice(
  89. struct se_hba *hba,
  90. struct se_subsystem_dev *se_dev,
  91. void *p)
  92. {
  93. struct se_device *dev;
  94. struct se_dev_limits dev_limits;
  95. struct queue_limits *limits;
  96. struct fd_dev *fd_dev = p;
  97. struct fd_host *fd_host = hba->hba_ptr;
  98. struct file *file;
  99. struct inode *inode = NULL;
  100. int dev_flags = 0, flags, ret = -EINVAL;
  101. memset(&dev_limits, 0, sizeof(struct se_dev_limits));
  102. /*
  103. * Use O_DSYNC by default instead of O_SYNC to forgo syncing
  104. * of pure timestamp updates.
  105. */
  106. flags = O_RDWR | O_CREAT | O_LARGEFILE | O_DSYNC;
  107. file = filp_open(fd_dev->fd_dev_name, flags, 0600);
  108. if (IS_ERR(file)) {
  109. pr_err("filp_open(%s) failed\n", fd_dev->fd_dev_name);
  110. ret = PTR_ERR(file);
  111. goto fail;
  112. }
  113. fd_dev->fd_file = file;
  114. /*
  115. * If using a block backend with this struct file, we extract
  116. * fd_dev->fd_[block,dev]_size from struct block_device.
  117. *
  118. * Otherwise, we use the passed fd_size= from configfs
  119. */
  120. inode = file->f_mapping->host;
  121. if (S_ISBLK(inode->i_mode)) {
  122. struct request_queue *q;
  123. unsigned long long dev_size;
  124. /*
  125. * Setup the local scope queue_limits from struct request_queue->limits
  126. * to pass into transport_add_device_to_core_hba() as struct se_dev_limits.
  127. */
  128. q = bdev_get_queue(inode->i_bdev);
  129. limits = &dev_limits.limits;
  130. limits->logical_block_size = bdev_logical_block_size(inode->i_bdev);
  131. limits->max_hw_sectors = queue_max_hw_sectors(q);
  132. limits->max_sectors = queue_max_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. fd_dev->fd_block_size = bdev_logical_block_size(inode->i_bdev);
  138. dev_size = (i_size_read(file->f_mapping->host) -
  139. fd_dev->fd_block_size);
  140. pr_debug("FILEIO: Using size: %llu bytes from struct"
  141. " block_device blocks: %llu logical_block_size: %d\n",
  142. dev_size, div_u64(dev_size, fd_dev->fd_block_size),
  143. fd_dev->fd_block_size);
  144. } else {
  145. if (!(fd_dev->fbd_flags & FBDF_HAS_SIZE)) {
  146. pr_err("FILEIO: Missing fd_dev_size="
  147. " parameter, and no backing struct"
  148. " block_device\n");
  149. goto fail;
  150. }
  151. limits = &dev_limits.limits;
  152. limits->logical_block_size = FD_BLOCKSIZE;
  153. limits->max_hw_sectors = FD_MAX_SECTORS;
  154. limits->max_sectors = FD_MAX_SECTORS;
  155. fd_dev->fd_block_size = FD_BLOCKSIZE;
  156. }
  157. dev_limits.hw_queue_depth = FD_MAX_DEVICE_QUEUE_DEPTH;
  158. dev_limits.queue_depth = FD_DEVICE_QUEUE_DEPTH;
  159. dev = transport_add_device_to_core_hba(hba, &fileio_template,
  160. se_dev, dev_flags, fd_dev,
  161. &dev_limits, "FILEIO", FD_VERSION);
  162. if (!dev)
  163. goto fail;
  164. fd_dev->fd_dev_id = fd_host->fd_host_dev_id_count++;
  165. fd_dev->fd_queue_depth = dev->queue_depth;
  166. pr_debug("CORE_FILE[%u] - Added TCM FILEIO Device ID: %u at %s,"
  167. " %llu total bytes\n", fd_host->fd_host_id, fd_dev->fd_dev_id,
  168. fd_dev->fd_dev_name, fd_dev->fd_dev_size);
  169. return dev;
  170. fail:
  171. if (fd_dev->fd_file) {
  172. filp_close(fd_dev->fd_file, NULL);
  173. fd_dev->fd_file = NULL;
  174. }
  175. return ERR_PTR(ret);
  176. }
  177. /* fd_free_device(): (Part of se_subsystem_api_t template)
  178. *
  179. *
  180. */
  181. static void fd_free_device(void *p)
  182. {
  183. struct fd_dev *fd_dev = p;
  184. if (fd_dev->fd_file) {
  185. filp_close(fd_dev->fd_file, NULL);
  186. fd_dev->fd_file = NULL;
  187. }
  188. kfree(fd_dev);
  189. }
  190. static int fd_do_readv(struct se_cmd *cmd, struct scatterlist *sgl,
  191. u32 sgl_nents)
  192. {
  193. struct se_device *se_dev = cmd->se_dev;
  194. struct fd_dev *dev = se_dev->dev_ptr;
  195. struct file *fd = dev->fd_file;
  196. struct scatterlist *sg;
  197. struct iovec *iov;
  198. mm_segment_t old_fs;
  199. loff_t pos = (cmd->t_task_lba *
  200. se_dev->se_sub_dev->se_dev_attrib.block_size);
  201. int ret = 0, i;
  202. iov = kzalloc(sizeof(struct iovec) * sgl_nents, GFP_KERNEL);
  203. if (!iov) {
  204. pr_err("Unable to allocate fd_do_readv iov[]\n");
  205. return -ENOMEM;
  206. }
  207. for_each_sg(sgl, sg, sgl_nents, i) {
  208. iov[i].iov_len = sg->length;
  209. iov[i].iov_base = sg_virt(sg);
  210. }
  211. old_fs = get_fs();
  212. set_fs(get_ds());
  213. ret = vfs_readv(fd, &iov[0], sgl_nents, &pos);
  214. set_fs(old_fs);
  215. kfree(iov);
  216. /*
  217. * Return zeros and GOOD status even if the READ did not return
  218. * the expected virt_size for struct file w/o a backing struct
  219. * block_device.
  220. */
  221. if (S_ISBLK(fd->f_dentry->d_inode->i_mode)) {
  222. if (ret < 0 || ret != cmd->data_length) {
  223. pr_err("vfs_readv() returned %d,"
  224. " expecting %d for S_ISBLK\n", ret,
  225. (int)cmd->data_length);
  226. return (ret < 0 ? ret : -EINVAL);
  227. }
  228. } else {
  229. if (ret < 0) {
  230. pr_err("vfs_readv() returned %d for non"
  231. " S_ISBLK\n", ret);
  232. return ret;
  233. }
  234. }
  235. return 1;
  236. }
  237. static int fd_do_writev(struct se_cmd *cmd, struct scatterlist *sgl,
  238. u32 sgl_nents)
  239. {
  240. struct se_device *se_dev = cmd->se_dev;
  241. struct fd_dev *dev = se_dev->dev_ptr;
  242. struct file *fd = dev->fd_file;
  243. struct scatterlist *sg;
  244. struct iovec *iov;
  245. mm_segment_t old_fs;
  246. loff_t pos = (cmd->t_task_lba *
  247. se_dev->se_sub_dev->se_dev_attrib.block_size);
  248. int ret, i = 0;
  249. iov = kzalloc(sizeof(struct iovec) * sgl_nents, GFP_KERNEL);
  250. if (!iov) {
  251. pr_err("Unable to allocate fd_do_writev iov[]\n");
  252. return -ENOMEM;
  253. }
  254. for_each_sg(sgl, sg, sgl_nents, i) {
  255. iov[i].iov_len = sg->length;
  256. iov[i].iov_base = sg_virt(sg);
  257. }
  258. old_fs = get_fs();
  259. set_fs(get_ds());
  260. ret = vfs_writev(fd, &iov[0], sgl_nents, &pos);
  261. set_fs(old_fs);
  262. kfree(iov);
  263. if (ret < 0 || ret != cmd->data_length) {
  264. pr_err("vfs_writev() returned %d\n", ret);
  265. return (ret < 0 ? ret : -EINVAL);
  266. }
  267. return 1;
  268. }
  269. static int fd_execute_sync_cache(struct se_cmd *cmd)
  270. {
  271. struct se_device *dev = cmd->se_dev;
  272. struct fd_dev *fd_dev = dev->dev_ptr;
  273. int immed = (cmd->t_task_cdb[1] & 0x2);
  274. loff_t start, end;
  275. int ret;
  276. /*
  277. * If the Immediate bit is set, queue up the GOOD response
  278. * for this SYNCHRONIZE_CACHE op
  279. */
  280. if (immed)
  281. target_complete_cmd(cmd, SAM_STAT_GOOD);
  282. /*
  283. * Determine if we will be flushing the entire device.
  284. */
  285. if (cmd->t_task_lba == 0 && cmd->data_length == 0) {
  286. start = 0;
  287. end = LLONG_MAX;
  288. } else {
  289. start = cmd->t_task_lba * dev->se_sub_dev->se_dev_attrib.block_size;
  290. if (cmd->data_length)
  291. end = start + cmd->data_length;
  292. else
  293. end = LLONG_MAX;
  294. }
  295. ret = vfs_fsync_range(fd_dev->fd_file, start, end, 1);
  296. if (ret != 0)
  297. pr_err("FILEIO: vfs_fsync_range() failed: %d\n", ret);
  298. if (immed)
  299. return 0;
  300. if (ret) {
  301. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  302. target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
  303. } else {
  304. target_complete_cmd(cmd, SAM_STAT_GOOD);
  305. }
  306. return 0;
  307. }
  308. static int fd_execute_rw(struct se_cmd *cmd)
  309. {
  310. struct scatterlist *sgl = cmd->t_data_sg;
  311. u32 sgl_nents = cmd->t_data_nents;
  312. enum dma_data_direction data_direction = cmd->data_direction;
  313. struct se_device *dev = cmd->se_dev;
  314. int ret = 0;
  315. /*
  316. * Call vectorized fileio functions to map struct scatterlist
  317. * physical memory addresses to struct iovec virtual memory.
  318. */
  319. if (data_direction == DMA_FROM_DEVICE) {
  320. ret = fd_do_readv(cmd, sgl, sgl_nents);
  321. } else {
  322. ret = fd_do_writev(cmd, sgl, sgl_nents);
  323. /*
  324. * Perform implict vfs_fsync_range() for fd_do_writev() ops
  325. * for SCSI WRITEs with Forced Unit Access (FUA) set.
  326. * Allow this to happen independent of WCE=0 setting.
  327. */
  328. if (ret > 0 &&
  329. dev->se_sub_dev->se_dev_attrib.emulate_fua_write > 0 &&
  330. (cmd->se_cmd_flags & SCF_FUA)) {
  331. struct fd_dev *fd_dev = dev->dev_ptr;
  332. loff_t start = cmd->t_task_lba *
  333. dev->se_sub_dev->se_dev_attrib.block_size;
  334. loff_t end = start + cmd->data_length;
  335. vfs_fsync_range(fd_dev->fd_file, start, end, 1);
  336. }
  337. }
  338. if (ret < 0) {
  339. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  340. return ret;
  341. }
  342. if (ret)
  343. target_complete_cmd(cmd, SAM_STAT_GOOD);
  344. return 0;
  345. }
  346. enum {
  347. Opt_fd_dev_name, Opt_fd_dev_size, Opt_fd_buffered_io, Opt_err
  348. };
  349. static match_table_t tokens = {
  350. {Opt_fd_dev_name, "fd_dev_name=%s"},
  351. {Opt_fd_dev_size, "fd_dev_size=%s"},
  352. {Opt_err, NULL}
  353. };
  354. static ssize_t fd_set_configfs_dev_params(
  355. struct se_hba *hba,
  356. struct se_subsystem_dev *se_dev,
  357. const char *page, ssize_t count)
  358. {
  359. struct fd_dev *fd_dev = se_dev->se_dev_su_ptr;
  360. char *orig, *ptr, *arg_p, *opts;
  361. substring_t args[MAX_OPT_ARGS];
  362. int ret = 0, token;
  363. opts = kstrdup(page, GFP_KERNEL);
  364. if (!opts)
  365. return -ENOMEM;
  366. orig = opts;
  367. while ((ptr = strsep(&opts, ",\n")) != NULL) {
  368. if (!*ptr)
  369. continue;
  370. token = match_token(ptr, tokens, args);
  371. switch (token) {
  372. case Opt_fd_dev_name:
  373. if (match_strlcpy(fd_dev->fd_dev_name, &args[0],
  374. FD_MAX_DEV_NAME) == 0) {
  375. ret = -EINVAL;
  376. break;
  377. }
  378. pr_debug("FILEIO: Referencing Path: %s\n",
  379. fd_dev->fd_dev_name);
  380. fd_dev->fbd_flags |= FBDF_HAS_PATH;
  381. break;
  382. case Opt_fd_dev_size:
  383. arg_p = match_strdup(&args[0]);
  384. if (!arg_p) {
  385. ret = -ENOMEM;
  386. break;
  387. }
  388. ret = strict_strtoull(arg_p, 0, &fd_dev->fd_dev_size);
  389. kfree(arg_p);
  390. if (ret < 0) {
  391. pr_err("strict_strtoull() failed for"
  392. " fd_dev_size=\n");
  393. goto out;
  394. }
  395. pr_debug("FILEIO: Referencing Size: %llu"
  396. " bytes\n", fd_dev->fd_dev_size);
  397. fd_dev->fbd_flags |= FBDF_HAS_SIZE;
  398. break;
  399. default:
  400. break;
  401. }
  402. }
  403. out:
  404. kfree(orig);
  405. return (!ret) ? count : ret;
  406. }
  407. static ssize_t fd_check_configfs_dev_params(struct se_hba *hba, struct se_subsystem_dev *se_dev)
  408. {
  409. struct fd_dev *fd_dev = se_dev->se_dev_su_ptr;
  410. if (!(fd_dev->fbd_flags & FBDF_HAS_PATH)) {
  411. pr_err("Missing fd_dev_name=\n");
  412. return -EINVAL;
  413. }
  414. return 0;
  415. }
  416. static ssize_t fd_show_configfs_dev_params(
  417. struct se_hba *hba,
  418. struct se_subsystem_dev *se_dev,
  419. char *b)
  420. {
  421. struct fd_dev *fd_dev = se_dev->se_dev_su_ptr;
  422. ssize_t bl = 0;
  423. bl = sprintf(b + bl, "TCM FILEIO ID: %u", fd_dev->fd_dev_id);
  424. bl += sprintf(b + bl, " File: %s Size: %llu Mode: O_DSYNC\n",
  425. fd_dev->fd_dev_name, fd_dev->fd_dev_size);
  426. return bl;
  427. }
  428. /* fd_get_device_rev(): (Part of se_subsystem_api_t template)
  429. *
  430. *
  431. */
  432. static u32 fd_get_device_rev(struct se_device *dev)
  433. {
  434. return SCSI_SPC_2; /* Returns SPC-3 in Initiator Data */
  435. }
  436. /* fd_get_device_type(): (Part of se_subsystem_api_t template)
  437. *
  438. *
  439. */
  440. static u32 fd_get_device_type(struct se_device *dev)
  441. {
  442. return TYPE_DISK;
  443. }
  444. static sector_t fd_get_blocks(struct se_device *dev)
  445. {
  446. struct fd_dev *fd_dev = dev->dev_ptr;
  447. struct file *f = fd_dev->fd_file;
  448. struct inode *i = f->f_mapping->host;
  449. unsigned long long dev_size;
  450. /*
  451. * When using a file that references an underlying struct block_device,
  452. * ensure dev_size is always based on the current inode size in order
  453. * to handle underlying block_device resize operations.
  454. */
  455. if (S_ISBLK(i->i_mode))
  456. dev_size = (i_size_read(i) - fd_dev->fd_block_size);
  457. else
  458. dev_size = fd_dev->fd_dev_size;
  459. return div_u64(dev_size, dev->se_sub_dev->se_dev_attrib.block_size);
  460. }
  461. static struct spc_ops fd_spc_ops = {
  462. .execute_rw = fd_execute_rw,
  463. .execute_sync_cache = fd_execute_sync_cache,
  464. };
  465. static int fd_parse_cdb(struct se_cmd *cmd)
  466. {
  467. return sbc_parse_cdb(cmd, &fd_spc_ops);
  468. }
  469. static struct se_subsystem_api fileio_template = {
  470. .name = "fileio",
  471. .owner = THIS_MODULE,
  472. .transport_type = TRANSPORT_PLUGIN_VHBA_PDEV,
  473. .write_cache_emulated = 1,
  474. .fua_write_emulated = 1,
  475. .attach_hba = fd_attach_hba,
  476. .detach_hba = fd_detach_hba,
  477. .allocate_virtdevice = fd_allocate_virtdevice,
  478. .create_virtdevice = fd_create_virtdevice,
  479. .free_device = fd_free_device,
  480. .parse_cdb = fd_parse_cdb,
  481. .check_configfs_dev_params = fd_check_configfs_dev_params,
  482. .set_configfs_dev_params = fd_set_configfs_dev_params,
  483. .show_configfs_dev_params = fd_show_configfs_dev_params,
  484. .get_device_rev = fd_get_device_rev,
  485. .get_device_type = fd_get_device_type,
  486. .get_blocks = fd_get_blocks,
  487. };
  488. static int __init fileio_module_init(void)
  489. {
  490. return transport_subsystem_register(&fileio_template);
  491. }
  492. static void fileio_module_exit(void)
  493. {
  494. transport_subsystem_release(&fileio_template);
  495. }
  496. MODULE_DESCRIPTION("TCM FILEIO subsystem plugin");
  497. MODULE_AUTHOR("nab@Linux-iSCSI.org");
  498. MODULE_LICENSE("GPL");
  499. module_init(fileio_module_init);
  500. module_exit(fileio_module_exit);