target_core_file.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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 void fd_emulate_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;
  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. }
  307. static int fd_execute_cmd(struct se_cmd *cmd, struct scatterlist *sgl,
  308. u32 sgl_nents, enum dma_data_direction data_direction)
  309. {
  310. struct se_device *dev = cmd->se_dev;
  311. int ret = 0;
  312. /*
  313. * Call vectorized fileio functions to map struct scatterlist
  314. * physical memory addresses to struct iovec virtual memory.
  315. */
  316. if (data_direction == DMA_FROM_DEVICE) {
  317. ret = fd_do_readv(cmd, sgl, sgl_nents);
  318. } else {
  319. ret = fd_do_writev(cmd, sgl, sgl_nents);
  320. /*
  321. * Perform implict vfs_fsync_range() for fd_do_writev() ops
  322. * for SCSI WRITEs with Forced Unit Access (FUA) set.
  323. * Allow this to happen independent of WCE=0 setting.
  324. */
  325. if (ret > 0 &&
  326. dev->se_sub_dev->se_dev_attrib.emulate_fua_write > 0 &&
  327. (cmd->se_cmd_flags & SCF_FUA)) {
  328. struct fd_dev *fd_dev = dev->dev_ptr;
  329. loff_t start = cmd->t_task_lba *
  330. dev->se_sub_dev->se_dev_attrib.block_size;
  331. loff_t end = start + cmd->data_length;
  332. vfs_fsync_range(fd_dev->fd_file, start, end, 1);
  333. }
  334. }
  335. if (ret < 0) {
  336. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  337. return ret;
  338. }
  339. if (ret)
  340. target_complete_cmd(cmd, SAM_STAT_GOOD);
  341. return 0;
  342. }
  343. enum {
  344. Opt_fd_dev_name, Opt_fd_dev_size, Opt_fd_buffered_io, Opt_err
  345. };
  346. static match_table_t tokens = {
  347. {Opt_fd_dev_name, "fd_dev_name=%s"},
  348. {Opt_fd_dev_size, "fd_dev_size=%s"},
  349. {Opt_err, NULL}
  350. };
  351. static ssize_t fd_set_configfs_dev_params(
  352. struct se_hba *hba,
  353. struct se_subsystem_dev *se_dev,
  354. const char *page, ssize_t count)
  355. {
  356. struct fd_dev *fd_dev = se_dev->se_dev_su_ptr;
  357. char *orig, *ptr, *arg_p, *opts;
  358. substring_t args[MAX_OPT_ARGS];
  359. int ret = 0, token;
  360. opts = kstrdup(page, GFP_KERNEL);
  361. if (!opts)
  362. return -ENOMEM;
  363. orig = opts;
  364. while ((ptr = strsep(&opts, ",\n")) != NULL) {
  365. if (!*ptr)
  366. continue;
  367. token = match_token(ptr, tokens, args);
  368. switch (token) {
  369. case Opt_fd_dev_name:
  370. if (match_strlcpy(fd_dev->fd_dev_name, &args[0],
  371. FD_MAX_DEV_NAME) == 0) {
  372. ret = -EINVAL;
  373. break;
  374. }
  375. pr_debug("FILEIO: Referencing Path: %s\n",
  376. fd_dev->fd_dev_name);
  377. fd_dev->fbd_flags |= FBDF_HAS_PATH;
  378. break;
  379. case Opt_fd_dev_size:
  380. arg_p = match_strdup(&args[0]);
  381. if (!arg_p) {
  382. ret = -ENOMEM;
  383. break;
  384. }
  385. ret = strict_strtoull(arg_p, 0, &fd_dev->fd_dev_size);
  386. kfree(arg_p);
  387. if (ret < 0) {
  388. pr_err("strict_strtoull() failed for"
  389. " fd_dev_size=\n");
  390. goto out;
  391. }
  392. pr_debug("FILEIO: Referencing Size: %llu"
  393. " bytes\n", fd_dev->fd_dev_size);
  394. fd_dev->fbd_flags |= FBDF_HAS_SIZE;
  395. break;
  396. default:
  397. break;
  398. }
  399. }
  400. out:
  401. kfree(orig);
  402. return (!ret) ? count : ret;
  403. }
  404. static ssize_t fd_check_configfs_dev_params(struct se_hba *hba, struct se_subsystem_dev *se_dev)
  405. {
  406. struct fd_dev *fd_dev = se_dev->se_dev_su_ptr;
  407. if (!(fd_dev->fbd_flags & FBDF_HAS_PATH)) {
  408. pr_err("Missing fd_dev_name=\n");
  409. return -EINVAL;
  410. }
  411. return 0;
  412. }
  413. static ssize_t fd_show_configfs_dev_params(
  414. struct se_hba *hba,
  415. struct se_subsystem_dev *se_dev,
  416. char *b)
  417. {
  418. struct fd_dev *fd_dev = se_dev->se_dev_su_ptr;
  419. ssize_t bl = 0;
  420. bl = sprintf(b + bl, "TCM FILEIO ID: %u", fd_dev->fd_dev_id);
  421. bl += sprintf(b + bl, " File: %s Size: %llu Mode: O_DSYNC\n",
  422. fd_dev->fd_dev_name, fd_dev->fd_dev_size);
  423. return bl;
  424. }
  425. /* fd_get_device_rev(): (Part of se_subsystem_api_t template)
  426. *
  427. *
  428. */
  429. static u32 fd_get_device_rev(struct se_device *dev)
  430. {
  431. return SCSI_SPC_2; /* Returns SPC-3 in Initiator Data */
  432. }
  433. /* fd_get_device_type(): (Part of se_subsystem_api_t template)
  434. *
  435. *
  436. */
  437. static u32 fd_get_device_type(struct se_device *dev)
  438. {
  439. return TYPE_DISK;
  440. }
  441. static sector_t fd_get_blocks(struct se_device *dev)
  442. {
  443. struct fd_dev *fd_dev = dev->dev_ptr;
  444. struct file *f = fd_dev->fd_file;
  445. struct inode *i = f->f_mapping->host;
  446. unsigned long long dev_size;
  447. /*
  448. * When using a file that references an underlying struct block_device,
  449. * ensure dev_size is always based on the current inode size in order
  450. * to handle underlying block_device resize operations.
  451. */
  452. if (S_ISBLK(i->i_mode))
  453. dev_size = (i_size_read(i) - fd_dev->fd_block_size);
  454. else
  455. dev_size = fd_dev->fd_dev_size;
  456. return div_u64(dev_size, dev->se_sub_dev->se_dev_attrib.block_size);
  457. }
  458. static struct se_subsystem_api fileio_template = {
  459. .name = "fileio",
  460. .owner = THIS_MODULE,
  461. .transport_type = TRANSPORT_PLUGIN_VHBA_PDEV,
  462. .write_cache_emulated = 1,
  463. .fua_write_emulated = 1,
  464. .attach_hba = fd_attach_hba,
  465. .detach_hba = fd_detach_hba,
  466. .allocate_virtdevice = fd_allocate_virtdevice,
  467. .create_virtdevice = fd_create_virtdevice,
  468. .free_device = fd_free_device,
  469. .execute_cmd = fd_execute_cmd,
  470. .do_sync_cache = fd_emulate_sync_cache,
  471. .check_configfs_dev_params = fd_check_configfs_dev_params,
  472. .set_configfs_dev_params = fd_set_configfs_dev_params,
  473. .show_configfs_dev_params = fd_show_configfs_dev_params,
  474. .get_device_rev = fd_get_device_rev,
  475. .get_device_type = fd_get_device_type,
  476. .get_blocks = fd_get_blocks,
  477. };
  478. static int __init fileio_module_init(void)
  479. {
  480. return transport_subsystem_register(&fileio_template);
  481. }
  482. static void fileio_module_exit(void)
  483. {
  484. transport_subsystem_release(&fileio_template);
  485. }
  486. MODULE_DESCRIPTION("TCM FILEIO subsystem plugin");
  487. MODULE_AUTHOR("nab@Linux-iSCSI.org");
  488. MODULE_LICENSE("GPL");
  489. module_init(fileio_module_init);
  490. module_exit(fileio_module_exit);