target_core_file.c 15 KB

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