target_core_file.c 17 KB

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