target_core_file.c 26 KB

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