target_core_file.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  1. /*******************************************************************************
  2. * Filename: target_core_file.c
  3. *
  4. * This file contains the Storage Engine <-> FILEIO transport specific functions
  5. *
  6. * (c) Copyright 2005-2013 Datera, Inc.
  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 <linux/falloc.h>
  33. #include <scsi/scsi.h>
  34. #include <scsi/scsi_host.h>
  35. #include <asm/unaligned.h>
  36. #include <target/target_core_base.h>
  37. #include <target/target_core_backend.h>
  38. #include "target_core_file.h"
  39. static inline struct fd_dev *FD_DEV(struct se_device *dev)
  40. {
  41. return container_of(dev, struct fd_dev, dev);
  42. }
  43. /* fd_attach_hba(): (Part of se_subsystem_api_t template)
  44. *
  45. *
  46. */
  47. static int fd_attach_hba(struct se_hba *hba, u32 host_id)
  48. {
  49. struct fd_host *fd_host;
  50. fd_host = kzalloc(sizeof(struct fd_host), GFP_KERNEL);
  51. if (!fd_host) {
  52. pr_err("Unable to allocate memory for struct fd_host\n");
  53. return -ENOMEM;
  54. }
  55. fd_host->fd_host_id = host_id;
  56. hba->hba_ptr = fd_host;
  57. pr_debug("CORE_HBA[%d] - TCM FILEIO HBA Driver %s on Generic"
  58. " Target Core Stack %s\n", hba->hba_id, FD_VERSION,
  59. TARGET_CORE_MOD_VERSION);
  60. pr_debug("CORE_HBA[%d] - Attached FILEIO HBA: %u to Generic\n",
  61. hba->hba_id, fd_host->fd_host_id);
  62. return 0;
  63. }
  64. static void fd_detach_hba(struct se_hba *hba)
  65. {
  66. struct fd_host *fd_host = hba->hba_ptr;
  67. pr_debug("CORE_HBA[%d] - Detached FILEIO HBA: %u from Generic"
  68. " Target Core\n", hba->hba_id, fd_host->fd_host_id);
  69. kfree(fd_host);
  70. hba->hba_ptr = NULL;
  71. }
  72. static struct se_device *fd_alloc_device(struct se_hba *hba, const char *name)
  73. {
  74. struct fd_dev *fd_dev;
  75. struct fd_host *fd_host = hba->hba_ptr;
  76. fd_dev = kzalloc(sizeof(struct fd_dev), GFP_KERNEL);
  77. if (!fd_dev) {
  78. pr_err("Unable to allocate memory for struct fd_dev\n");
  79. return NULL;
  80. }
  81. fd_dev->fd_host = fd_host;
  82. pr_debug("FILEIO: Allocated fd_dev for %p\n", name);
  83. return &fd_dev->dev;
  84. }
  85. static int fd_configure_device(struct se_device *dev)
  86. {
  87. struct fd_dev *fd_dev = FD_DEV(dev);
  88. struct fd_host *fd_host = dev->se_hba->hba_ptr;
  89. struct file *file;
  90. struct inode *inode = NULL;
  91. int flags, ret = -EINVAL;
  92. if (!(fd_dev->fbd_flags & FBDF_HAS_PATH)) {
  93. pr_err("Missing fd_dev_name=\n");
  94. return -EINVAL;
  95. }
  96. /*
  97. * Use O_DSYNC by default instead of O_SYNC to forgo syncing
  98. * of pure timestamp updates.
  99. */
  100. flags = O_RDWR | O_CREAT | O_LARGEFILE | O_DSYNC;
  101. /*
  102. * Optionally allow fd_buffered_io=1 to be enabled for people
  103. * who want use the fs buffer cache as an WriteCache mechanism.
  104. *
  105. * This means that in event of a hard failure, there is a risk
  106. * of silent data-loss if the SCSI client has *not* performed a
  107. * forced unit access (FUA) write, or issued SYNCHRONIZE_CACHE
  108. * to write-out the entire device cache.
  109. */
  110. if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) {
  111. pr_debug("FILEIO: Disabling O_DSYNC, using buffered FILEIO\n");
  112. flags &= ~O_DSYNC;
  113. }
  114. file = filp_open(fd_dev->fd_dev_name, flags, 0600);
  115. if (IS_ERR(file)) {
  116. pr_err("filp_open(%s) failed\n", fd_dev->fd_dev_name);
  117. ret = PTR_ERR(file);
  118. goto fail;
  119. }
  120. fd_dev->fd_file = file;
  121. /*
  122. * If using a block backend with this struct file, we extract
  123. * fd_dev->fd_[block,dev]_size from struct block_device.
  124. *
  125. * Otherwise, we use the passed fd_size= from configfs
  126. */
  127. inode = file->f_mapping->host;
  128. if (S_ISBLK(inode->i_mode)) {
  129. struct request_queue *q = bdev_get_queue(inode->i_bdev);
  130. unsigned long long dev_size;
  131. fd_dev->fd_block_size = bdev_logical_block_size(inode->i_bdev);
  132. /*
  133. * Determine the number of bytes from i_size_read() minus
  134. * one (1) logical sector from underlying struct block_device
  135. */
  136. dev_size = (i_size_read(file->f_mapping->host) -
  137. fd_dev->fd_block_size);
  138. pr_debug("FILEIO: Using size: %llu bytes from struct"
  139. " block_device blocks: %llu logical_block_size: %d\n",
  140. dev_size, div_u64(dev_size, fd_dev->fd_block_size),
  141. fd_dev->fd_block_size);
  142. /*
  143. * Check if the underlying struct block_device request_queue supports
  144. * the QUEUE_FLAG_DISCARD bit for UNMAP/WRITE_SAME in SCSI + TRIM
  145. * in ATA and we need to set TPE=1
  146. */
  147. if (blk_queue_discard(q)) {
  148. dev->dev_attrib.max_unmap_lba_count =
  149. q->limits.max_discard_sectors;
  150. /*
  151. * Currently hardcoded to 1 in Linux/SCSI code..
  152. */
  153. dev->dev_attrib.max_unmap_block_desc_count = 1;
  154. dev->dev_attrib.unmap_granularity =
  155. q->limits.discard_granularity >> 9;
  156. dev->dev_attrib.unmap_granularity_alignment =
  157. q->limits.discard_alignment;
  158. pr_debug("IFILE: BLOCK Discard support available,"
  159. " disabled by default\n");
  160. }
  161. /*
  162. * Enable write same emulation for IBLOCK and use 0xFFFF as
  163. * the smaller WRITE_SAME(10) only has a two-byte block count.
  164. */
  165. dev->dev_attrib.max_write_same_len = 0xFFFF;
  166. if (blk_queue_nonrot(q))
  167. dev->dev_attrib.is_nonrot = 1;
  168. } else {
  169. if (!(fd_dev->fbd_flags & FBDF_HAS_SIZE)) {
  170. pr_err("FILEIO: Missing fd_dev_size="
  171. " parameter, and no backing struct"
  172. " block_device\n");
  173. goto fail;
  174. }
  175. fd_dev->fd_block_size = FD_BLOCKSIZE;
  176. /*
  177. * Limit UNMAP emulation to 8k Number of LBAs (NoLB)
  178. */
  179. dev->dev_attrib.max_unmap_lba_count = 0x2000;
  180. /*
  181. * Currently hardcoded to 1 in Linux/SCSI code..
  182. */
  183. dev->dev_attrib.max_unmap_block_desc_count = 1;
  184. dev->dev_attrib.unmap_granularity = 1;
  185. dev->dev_attrib.unmap_granularity_alignment = 0;
  186. /*
  187. * Limit WRITE_SAME w/ UNMAP=0 emulation to 8k Number of LBAs (NoLB)
  188. * based upon struct iovec limit for vfs_writev()
  189. */
  190. dev->dev_attrib.max_write_same_len = 0x1000;
  191. }
  192. dev->dev_attrib.hw_block_size = fd_dev->fd_block_size;
  193. dev->dev_attrib.max_bytes_per_io = FD_MAX_BYTES;
  194. dev->dev_attrib.hw_max_sectors = FD_MAX_BYTES / fd_dev->fd_block_size;
  195. dev->dev_attrib.hw_queue_depth = FD_MAX_DEVICE_QUEUE_DEPTH;
  196. if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) {
  197. pr_debug("FILEIO: Forcing setting of emulate_write_cache=1"
  198. " with FDBD_HAS_BUFFERED_IO_WCE\n");
  199. dev->dev_attrib.emulate_write_cache = 1;
  200. }
  201. fd_dev->fd_dev_id = fd_host->fd_host_dev_id_count++;
  202. fd_dev->fd_queue_depth = dev->queue_depth;
  203. pr_debug("CORE_FILE[%u] - Added TCM FILEIO Device ID: %u at %s,"
  204. " %llu total bytes\n", fd_host->fd_host_id, fd_dev->fd_dev_id,
  205. fd_dev->fd_dev_name, fd_dev->fd_dev_size);
  206. return 0;
  207. fail:
  208. if (fd_dev->fd_file) {
  209. filp_close(fd_dev->fd_file, NULL);
  210. fd_dev->fd_file = NULL;
  211. }
  212. return ret;
  213. }
  214. static void fd_free_device(struct se_device *dev)
  215. {
  216. struct fd_dev *fd_dev = FD_DEV(dev);
  217. if (fd_dev->fd_file) {
  218. filp_close(fd_dev->fd_file, NULL);
  219. fd_dev->fd_file = NULL;
  220. }
  221. kfree(fd_dev);
  222. }
  223. static int fd_do_prot_rw(struct se_cmd *cmd, struct fd_prot *fd_prot,
  224. int is_write)
  225. {
  226. struct se_device *se_dev = cmd->se_dev;
  227. struct fd_dev *dev = FD_DEV(se_dev);
  228. struct file *prot_fd = dev->fd_prot_file;
  229. struct scatterlist *sg;
  230. loff_t pos = (cmd->t_task_lba * se_dev->prot_length);
  231. unsigned char *buf;
  232. u32 prot_size, len, size;
  233. int rc, ret = 1, i;
  234. prot_size = (cmd->data_length / se_dev->dev_attrib.block_size) *
  235. se_dev->prot_length;
  236. if (!is_write) {
  237. fd_prot->prot_buf = vzalloc(prot_size);
  238. if (!fd_prot->prot_buf) {
  239. pr_err("Unable to allocate fd_prot->prot_buf\n");
  240. return -ENOMEM;
  241. }
  242. buf = fd_prot->prot_buf;
  243. fd_prot->prot_sg_nents = cmd->t_prot_nents;
  244. fd_prot->prot_sg = kzalloc(sizeof(struct scatterlist) *
  245. fd_prot->prot_sg_nents, GFP_KERNEL);
  246. if (!fd_prot->prot_sg) {
  247. pr_err("Unable to allocate fd_prot->prot_sg\n");
  248. vfree(fd_prot->prot_buf);
  249. return -ENOMEM;
  250. }
  251. size = prot_size;
  252. for_each_sg(fd_prot->prot_sg, sg, fd_prot->prot_sg_nents, i) {
  253. len = min_t(u32, PAGE_SIZE, size);
  254. sg_set_buf(sg, buf, len);
  255. size -= len;
  256. buf += len;
  257. }
  258. }
  259. if (is_write) {
  260. rc = kernel_write(prot_fd, fd_prot->prot_buf, prot_size, pos);
  261. if (rc < 0 || prot_size != rc) {
  262. pr_err("kernel_write() for fd_do_prot_rw failed:"
  263. " %d\n", rc);
  264. ret = -EINVAL;
  265. }
  266. } else {
  267. rc = kernel_read(prot_fd, pos, fd_prot->prot_buf, prot_size);
  268. if (rc < 0) {
  269. pr_err("kernel_read() for fd_do_prot_rw failed:"
  270. " %d\n", rc);
  271. ret = -EINVAL;
  272. }
  273. }
  274. if (is_write || ret < 0) {
  275. kfree(fd_prot->prot_sg);
  276. vfree(fd_prot->prot_buf);
  277. }
  278. return ret;
  279. }
  280. static int fd_do_rw(struct se_cmd *cmd, struct scatterlist *sgl,
  281. u32 sgl_nents, int is_write)
  282. {
  283. struct se_device *se_dev = cmd->se_dev;
  284. struct fd_dev *dev = FD_DEV(se_dev);
  285. struct file *fd = dev->fd_file;
  286. struct scatterlist *sg;
  287. struct iovec *iov;
  288. mm_segment_t old_fs;
  289. loff_t pos = (cmd->t_task_lba * se_dev->dev_attrib.block_size);
  290. int ret = 0, i;
  291. iov = kzalloc(sizeof(struct iovec) * sgl_nents, GFP_KERNEL);
  292. if (!iov) {
  293. pr_err("Unable to allocate fd_do_readv iov[]\n");
  294. return -ENOMEM;
  295. }
  296. for_each_sg(sgl, sg, sgl_nents, i) {
  297. iov[i].iov_len = sg->length;
  298. iov[i].iov_base = kmap(sg_page(sg)) + sg->offset;
  299. }
  300. old_fs = get_fs();
  301. set_fs(get_ds());
  302. if (is_write)
  303. ret = vfs_writev(fd, &iov[0], sgl_nents, &pos);
  304. else
  305. ret = vfs_readv(fd, &iov[0], sgl_nents, &pos);
  306. set_fs(old_fs);
  307. for_each_sg(sgl, sg, sgl_nents, i)
  308. kunmap(sg_page(sg));
  309. kfree(iov);
  310. if (is_write) {
  311. if (ret < 0 || ret != cmd->data_length) {
  312. pr_err("%s() write returned %d\n", __func__, ret);
  313. return (ret < 0 ? ret : -EINVAL);
  314. }
  315. } else {
  316. /*
  317. * Return zeros and GOOD status even if the READ did not return
  318. * the expected virt_size for struct file w/o a backing struct
  319. * block_device.
  320. */
  321. if (S_ISBLK(file_inode(fd)->i_mode)) {
  322. if (ret < 0 || ret != cmd->data_length) {
  323. pr_err("%s() returned %d, expecting %u for "
  324. "S_ISBLK\n", __func__, ret,
  325. cmd->data_length);
  326. return (ret < 0 ? ret : -EINVAL);
  327. }
  328. } else {
  329. if (ret < 0) {
  330. pr_err("%s() returned %d for non S_ISBLK\n",
  331. __func__, ret);
  332. return ret;
  333. }
  334. }
  335. }
  336. return 1;
  337. }
  338. static sense_reason_t
  339. fd_execute_sync_cache(struct se_cmd *cmd)
  340. {
  341. struct se_device *dev = cmd->se_dev;
  342. struct fd_dev *fd_dev = FD_DEV(dev);
  343. int immed = (cmd->t_task_cdb[1] & 0x2);
  344. loff_t start, end;
  345. int ret;
  346. /*
  347. * If the Immediate bit is set, queue up the GOOD response
  348. * for this SYNCHRONIZE_CACHE op
  349. */
  350. if (immed)
  351. target_complete_cmd(cmd, SAM_STAT_GOOD);
  352. /*
  353. * Determine if we will be flushing the entire device.
  354. */
  355. if (cmd->t_task_lba == 0 && cmd->data_length == 0) {
  356. start = 0;
  357. end = LLONG_MAX;
  358. } else {
  359. start = cmd->t_task_lba * dev->dev_attrib.block_size;
  360. if (cmd->data_length)
  361. end = start + cmd->data_length;
  362. else
  363. end = LLONG_MAX;
  364. }
  365. ret = vfs_fsync_range(fd_dev->fd_file, start, end, 1);
  366. if (ret != 0)
  367. pr_err("FILEIO: vfs_fsync_range() failed: %d\n", ret);
  368. if (immed)
  369. return 0;
  370. if (ret)
  371. target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
  372. else
  373. target_complete_cmd(cmd, SAM_STAT_GOOD);
  374. return 0;
  375. }
  376. static unsigned char *
  377. fd_setup_write_same_buf(struct se_cmd *cmd, struct scatterlist *sg,
  378. unsigned int len)
  379. {
  380. struct se_device *se_dev = cmd->se_dev;
  381. unsigned int block_size = se_dev->dev_attrib.block_size;
  382. unsigned int i = 0, end;
  383. unsigned char *buf, *p, *kmap_buf;
  384. buf = kzalloc(min_t(unsigned int, len, PAGE_SIZE), GFP_KERNEL);
  385. if (!buf) {
  386. pr_err("Unable to allocate fd_execute_write_same buf\n");
  387. return NULL;
  388. }
  389. kmap_buf = kmap(sg_page(sg)) + sg->offset;
  390. if (!kmap_buf) {
  391. pr_err("kmap() failed in fd_setup_write_same\n");
  392. kfree(buf);
  393. return NULL;
  394. }
  395. /*
  396. * Fill local *buf to contain multiple WRITE_SAME blocks up to
  397. * min(len, PAGE_SIZE)
  398. */
  399. p = buf;
  400. end = min_t(unsigned int, len, PAGE_SIZE);
  401. while (i < end) {
  402. memcpy(p, kmap_buf, block_size);
  403. i += block_size;
  404. p += block_size;
  405. }
  406. kunmap(sg_page(sg));
  407. return buf;
  408. }
  409. static sense_reason_t
  410. fd_execute_write_same(struct se_cmd *cmd)
  411. {
  412. struct se_device *se_dev = cmd->se_dev;
  413. struct fd_dev *fd_dev = FD_DEV(se_dev);
  414. struct file *f = fd_dev->fd_file;
  415. struct scatterlist *sg;
  416. struct iovec *iov;
  417. mm_segment_t old_fs;
  418. sector_t nolb = sbc_get_write_same_sectors(cmd);
  419. loff_t pos = cmd->t_task_lba * se_dev->dev_attrib.block_size;
  420. unsigned int len, len_tmp, iov_num;
  421. int i, rc;
  422. unsigned char *buf;
  423. if (!nolb) {
  424. target_complete_cmd(cmd, SAM_STAT_GOOD);
  425. return 0;
  426. }
  427. sg = &cmd->t_data_sg[0];
  428. if (cmd->t_data_nents > 1 ||
  429. sg->length != cmd->se_dev->dev_attrib.block_size) {
  430. pr_err("WRITE_SAME: Illegal SGL t_data_nents: %u length: %u"
  431. " block_size: %u\n", cmd->t_data_nents, sg->length,
  432. cmd->se_dev->dev_attrib.block_size);
  433. return TCM_INVALID_CDB_FIELD;
  434. }
  435. len = len_tmp = nolb * se_dev->dev_attrib.block_size;
  436. iov_num = DIV_ROUND_UP(len, PAGE_SIZE);
  437. buf = fd_setup_write_same_buf(cmd, sg, len);
  438. if (!buf)
  439. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  440. iov = vzalloc(sizeof(struct iovec) * iov_num);
  441. if (!iov) {
  442. pr_err("Unable to allocate fd_execute_write_same iovecs\n");
  443. kfree(buf);
  444. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  445. }
  446. /*
  447. * Map the single fabric received scatterlist block now populated
  448. * in *buf into each iovec for I/O submission.
  449. */
  450. for (i = 0; i < iov_num; i++) {
  451. iov[i].iov_base = buf;
  452. iov[i].iov_len = min_t(unsigned int, len_tmp, PAGE_SIZE);
  453. len_tmp -= iov[i].iov_len;
  454. }
  455. old_fs = get_fs();
  456. set_fs(get_ds());
  457. rc = vfs_writev(f, &iov[0], iov_num, &pos);
  458. set_fs(old_fs);
  459. vfree(iov);
  460. kfree(buf);
  461. if (rc < 0 || rc != len) {
  462. pr_err("vfs_writev() returned %d for write same\n", rc);
  463. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  464. }
  465. target_complete_cmd(cmd, SAM_STAT_GOOD);
  466. return 0;
  467. }
  468. static sense_reason_t
  469. fd_do_unmap(struct se_cmd *cmd, void *priv, sector_t lba, sector_t nolb)
  470. {
  471. struct file *file = priv;
  472. struct inode *inode = file->f_mapping->host;
  473. int ret;
  474. if (S_ISBLK(inode->i_mode)) {
  475. /* The backend is block device, use discard */
  476. struct block_device *bdev = inode->i_bdev;
  477. ret = blkdev_issue_discard(bdev, lba,
  478. nolb, GFP_KERNEL, 0);
  479. if (ret < 0) {
  480. pr_warn("FILEIO: blkdev_issue_discard() failed: %d\n",
  481. ret);
  482. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  483. }
  484. } else {
  485. /* The backend is normal file, use fallocate */
  486. struct se_device *se_dev = cmd->se_dev;
  487. loff_t pos = lba * se_dev->dev_attrib.block_size;
  488. unsigned int len = nolb * se_dev->dev_attrib.block_size;
  489. int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
  490. if (!file->f_op->fallocate)
  491. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  492. ret = file->f_op->fallocate(file, mode, pos, len);
  493. if (ret < 0) {
  494. pr_warn("FILEIO: fallocate() failed: %d\n", ret);
  495. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  496. }
  497. }
  498. return 0;
  499. }
  500. static sense_reason_t
  501. fd_execute_write_same_unmap(struct se_cmd *cmd)
  502. {
  503. struct se_device *se_dev = cmd->se_dev;
  504. struct fd_dev *fd_dev = FD_DEV(se_dev);
  505. struct file *file = fd_dev->fd_file;
  506. sector_t lba = cmd->t_task_lba;
  507. sector_t nolb = sbc_get_write_same_sectors(cmd);
  508. int ret;
  509. if (!nolb) {
  510. target_complete_cmd(cmd, SAM_STAT_GOOD);
  511. return 0;
  512. }
  513. ret = fd_do_unmap(cmd, file, lba, nolb);
  514. if (ret)
  515. return ret;
  516. target_complete_cmd(cmd, GOOD);
  517. return 0;
  518. }
  519. static sense_reason_t
  520. fd_execute_unmap(struct se_cmd *cmd)
  521. {
  522. struct file *file = FD_DEV(cmd->se_dev)->fd_file;
  523. return sbc_execute_unmap(cmd, fd_do_unmap, file);
  524. }
  525. static sense_reason_t
  526. fd_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
  527. enum dma_data_direction data_direction)
  528. {
  529. struct se_device *dev = cmd->se_dev;
  530. struct fd_prot fd_prot;
  531. sense_reason_t rc;
  532. int ret = 0;
  533. /*
  534. * Call vectorized fileio functions to map struct scatterlist
  535. * physical memory addresses to struct iovec virtual memory.
  536. */
  537. if (data_direction == DMA_FROM_DEVICE) {
  538. memset(&fd_prot, 0, sizeof(struct fd_prot));
  539. if (cmd->prot_type) {
  540. ret = fd_do_prot_rw(cmd, &fd_prot, false);
  541. if (ret < 0)
  542. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  543. }
  544. ret = fd_do_rw(cmd, sgl, sgl_nents, 0);
  545. if (ret > 0 && cmd->prot_type) {
  546. u32 sectors = cmd->data_length / dev->dev_attrib.block_size;
  547. rc = sbc_dif_verify_read(cmd, cmd->t_task_lba, sectors,
  548. 0, fd_prot.prot_sg, 0);
  549. if (rc) {
  550. kfree(fd_prot.prot_sg);
  551. vfree(fd_prot.prot_buf);
  552. return rc;
  553. }
  554. kfree(fd_prot.prot_sg);
  555. vfree(fd_prot.prot_buf);
  556. }
  557. } else {
  558. memset(&fd_prot, 0, sizeof(struct fd_prot));
  559. if (cmd->prot_type) {
  560. u32 sectors = cmd->data_length / dev->dev_attrib.block_size;
  561. ret = fd_do_prot_rw(cmd, &fd_prot, false);
  562. if (ret < 0)
  563. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  564. rc = sbc_dif_verify_write(cmd, cmd->t_task_lba, sectors,
  565. 0, fd_prot.prot_sg, 0);
  566. if (rc) {
  567. kfree(fd_prot.prot_sg);
  568. vfree(fd_prot.prot_buf);
  569. return rc;
  570. }
  571. }
  572. ret = fd_do_rw(cmd, sgl, sgl_nents, 1);
  573. /*
  574. * Perform implicit vfs_fsync_range() for fd_do_writev() ops
  575. * for SCSI WRITEs with Forced Unit Access (FUA) set.
  576. * Allow this to happen independent of WCE=0 setting.
  577. */
  578. if (ret > 0 &&
  579. dev->dev_attrib.emulate_fua_write > 0 &&
  580. (cmd->se_cmd_flags & SCF_FUA)) {
  581. struct fd_dev *fd_dev = FD_DEV(dev);
  582. loff_t start = cmd->t_task_lba *
  583. dev->dev_attrib.block_size;
  584. loff_t end = start + cmd->data_length;
  585. vfs_fsync_range(fd_dev->fd_file, start, end, 1);
  586. }
  587. if (ret > 0 && cmd->prot_type) {
  588. ret = fd_do_prot_rw(cmd, &fd_prot, true);
  589. if (ret < 0)
  590. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  591. }
  592. }
  593. if (ret < 0) {
  594. kfree(fd_prot.prot_sg);
  595. vfree(fd_prot.prot_buf);
  596. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  597. }
  598. if (ret)
  599. target_complete_cmd(cmd, SAM_STAT_GOOD);
  600. return 0;
  601. }
  602. enum {
  603. Opt_fd_dev_name, Opt_fd_dev_size, Opt_fd_buffered_io, Opt_err
  604. };
  605. static match_table_t tokens = {
  606. {Opt_fd_dev_name, "fd_dev_name=%s"},
  607. {Opt_fd_dev_size, "fd_dev_size=%s"},
  608. {Opt_fd_buffered_io, "fd_buffered_io=%d"},
  609. {Opt_err, NULL}
  610. };
  611. static ssize_t fd_set_configfs_dev_params(struct se_device *dev,
  612. const char *page, ssize_t count)
  613. {
  614. struct fd_dev *fd_dev = FD_DEV(dev);
  615. char *orig, *ptr, *arg_p, *opts;
  616. substring_t args[MAX_OPT_ARGS];
  617. int ret = 0, arg, token;
  618. opts = kstrdup(page, GFP_KERNEL);
  619. if (!opts)
  620. return -ENOMEM;
  621. orig = opts;
  622. while ((ptr = strsep(&opts, ",\n")) != NULL) {
  623. if (!*ptr)
  624. continue;
  625. token = match_token(ptr, tokens, args);
  626. switch (token) {
  627. case Opt_fd_dev_name:
  628. if (match_strlcpy(fd_dev->fd_dev_name, &args[0],
  629. FD_MAX_DEV_NAME) == 0) {
  630. ret = -EINVAL;
  631. break;
  632. }
  633. pr_debug("FILEIO: Referencing Path: %s\n",
  634. fd_dev->fd_dev_name);
  635. fd_dev->fbd_flags |= FBDF_HAS_PATH;
  636. break;
  637. case Opt_fd_dev_size:
  638. arg_p = match_strdup(&args[0]);
  639. if (!arg_p) {
  640. ret = -ENOMEM;
  641. break;
  642. }
  643. ret = kstrtoull(arg_p, 0, &fd_dev->fd_dev_size);
  644. kfree(arg_p);
  645. if (ret < 0) {
  646. pr_err("kstrtoull() failed for"
  647. " fd_dev_size=\n");
  648. goto out;
  649. }
  650. pr_debug("FILEIO: Referencing Size: %llu"
  651. " bytes\n", fd_dev->fd_dev_size);
  652. fd_dev->fbd_flags |= FBDF_HAS_SIZE;
  653. break;
  654. case Opt_fd_buffered_io:
  655. match_int(args, &arg);
  656. if (arg != 1) {
  657. pr_err("bogus fd_buffered_io=%d value\n", arg);
  658. ret = -EINVAL;
  659. goto out;
  660. }
  661. pr_debug("FILEIO: Using buffered I/O"
  662. " operations for struct fd_dev\n");
  663. fd_dev->fbd_flags |= FDBD_HAS_BUFFERED_IO_WCE;
  664. break;
  665. default:
  666. break;
  667. }
  668. }
  669. out:
  670. kfree(orig);
  671. return (!ret) ? count : ret;
  672. }
  673. static ssize_t fd_show_configfs_dev_params(struct se_device *dev, char *b)
  674. {
  675. struct fd_dev *fd_dev = FD_DEV(dev);
  676. ssize_t bl = 0;
  677. bl = sprintf(b + bl, "TCM FILEIO ID: %u", fd_dev->fd_dev_id);
  678. bl += sprintf(b + bl, " File: %s Size: %llu Mode: %s\n",
  679. fd_dev->fd_dev_name, fd_dev->fd_dev_size,
  680. (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) ?
  681. "Buffered-WCE" : "O_DSYNC");
  682. return bl;
  683. }
  684. static sector_t fd_get_blocks(struct se_device *dev)
  685. {
  686. struct fd_dev *fd_dev = FD_DEV(dev);
  687. struct file *f = fd_dev->fd_file;
  688. struct inode *i = f->f_mapping->host;
  689. unsigned long long dev_size;
  690. /*
  691. * When using a file that references an underlying struct block_device,
  692. * ensure dev_size is always based on the current inode size in order
  693. * to handle underlying block_device resize operations.
  694. */
  695. if (S_ISBLK(i->i_mode))
  696. dev_size = i_size_read(i);
  697. else
  698. dev_size = fd_dev->fd_dev_size;
  699. return div_u64(dev_size - dev->dev_attrib.block_size,
  700. dev->dev_attrib.block_size);
  701. }
  702. static int fd_init_prot(struct se_device *dev)
  703. {
  704. struct fd_dev *fd_dev = FD_DEV(dev);
  705. struct file *prot_file, *file = fd_dev->fd_file;
  706. struct inode *inode;
  707. int ret, flags = O_RDWR | O_CREAT | O_LARGEFILE | O_DSYNC;
  708. char buf[FD_MAX_DEV_PROT_NAME];
  709. if (!file) {
  710. pr_err("Unable to locate fd_dev->fd_file\n");
  711. return -ENODEV;
  712. }
  713. inode = file->f_mapping->host;
  714. if (S_ISBLK(inode->i_mode)) {
  715. pr_err("FILEIO Protection emulation only supported on"
  716. " !S_ISBLK\n");
  717. return -ENOSYS;
  718. }
  719. if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE)
  720. flags &= ~O_DSYNC;
  721. snprintf(buf, FD_MAX_DEV_PROT_NAME, "%s.protection",
  722. fd_dev->fd_dev_name);
  723. prot_file = filp_open(buf, flags, 0600);
  724. if (IS_ERR(prot_file)) {
  725. pr_err("filp_open(%s) failed\n", buf);
  726. ret = PTR_ERR(prot_file);
  727. return ret;
  728. }
  729. fd_dev->fd_prot_file = prot_file;
  730. return 0;
  731. }
  732. static int fd_format_prot(struct se_device *dev)
  733. {
  734. struct fd_dev *fd_dev = FD_DEV(dev);
  735. struct file *prot_fd = fd_dev->fd_prot_file;
  736. sector_t prot_length, prot;
  737. unsigned char *buf;
  738. loff_t pos = 0;
  739. int unit_size = FDBD_FORMAT_UNIT_SIZE * dev->dev_attrib.block_size;
  740. int rc, ret = 0, size, len;
  741. if (!dev->dev_attrib.pi_prot_type) {
  742. pr_err("Unable to format_prot while pi_prot_type == 0\n");
  743. return -ENODEV;
  744. }
  745. if (!prot_fd) {
  746. pr_err("Unable to locate fd_dev->fd_prot_file\n");
  747. return -ENODEV;
  748. }
  749. buf = vzalloc(unit_size);
  750. if (!buf) {
  751. pr_err("Unable to allocate FILEIO prot buf\n");
  752. return -ENOMEM;
  753. }
  754. prot_length = (dev->transport->get_blocks(dev) + 1) * dev->prot_length;
  755. size = prot_length;
  756. pr_debug("Using FILEIO prot_length: %llu\n",
  757. (unsigned long long)prot_length);
  758. memset(buf, 0xff, unit_size);
  759. for (prot = 0; prot < prot_length; prot += unit_size) {
  760. len = min(unit_size, size);
  761. rc = kernel_write(prot_fd, buf, len, pos);
  762. if (rc != len) {
  763. pr_err("vfs_write to prot file failed: %d\n", rc);
  764. ret = -ENODEV;
  765. goto out;
  766. }
  767. pos += len;
  768. size -= len;
  769. }
  770. out:
  771. vfree(buf);
  772. return ret;
  773. }
  774. static void fd_free_prot(struct se_device *dev)
  775. {
  776. struct fd_dev *fd_dev = FD_DEV(dev);
  777. if (!fd_dev->fd_prot_file)
  778. return;
  779. filp_close(fd_dev->fd_prot_file, NULL);
  780. fd_dev->fd_prot_file = NULL;
  781. }
  782. static struct sbc_ops fd_sbc_ops = {
  783. .execute_rw = fd_execute_rw,
  784. .execute_sync_cache = fd_execute_sync_cache,
  785. .execute_write_same = fd_execute_write_same,
  786. .execute_write_same_unmap = fd_execute_write_same_unmap,
  787. .execute_unmap = fd_execute_unmap,
  788. };
  789. static sense_reason_t
  790. fd_parse_cdb(struct se_cmd *cmd)
  791. {
  792. return sbc_parse_cdb(cmd, &fd_sbc_ops);
  793. }
  794. static struct se_subsystem_api fileio_template = {
  795. .name = "fileio",
  796. .inquiry_prod = "FILEIO",
  797. .inquiry_rev = FD_VERSION,
  798. .owner = THIS_MODULE,
  799. .transport_type = TRANSPORT_PLUGIN_VHBA_PDEV,
  800. .attach_hba = fd_attach_hba,
  801. .detach_hba = fd_detach_hba,
  802. .alloc_device = fd_alloc_device,
  803. .configure_device = fd_configure_device,
  804. .free_device = fd_free_device,
  805. .parse_cdb = fd_parse_cdb,
  806. .set_configfs_dev_params = fd_set_configfs_dev_params,
  807. .show_configfs_dev_params = fd_show_configfs_dev_params,
  808. .get_device_type = sbc_get_device_type,
  809. .get_blocks = fd_get_blocks,
  810. .init_prot = fd_init_prot,
  811. .format_prot = fd_format_prot,
  812. .free_prot = fd_free_prot,
  813. };
  814. static int __init fileio_module_init(void)
  815. {
  816. return transport_subsystem_register(&fileio_template);
  817. }
  818. static void __exit fileio_module_exit(void)
  819. {
  820. transport_subsystem_release(&fileio_template);
  821. }
  822. MODULE_DESCRIPTION("TCM FILEIO subsystem plugin");
  823. MODULE_AUTHOR("nab@Linux-iSCSI.org");
  824. MODULE_LICENSE("GPL");
  825. module_init(fileio_module_init);
  826. module_exit(fileio_module_exit);