target_core_file.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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. loff_t pos = (cmd->t_task_lba * se_dev->prot_length);
  230. unsigned char *buf;
  231. u32 prot_size;
  232. int rc, ret = 1;
  233. prot_size = (cmd->data_length / se_dev->dev_attrib.block_size) *
  234. se_dev->prot_length;
  235. if (!is_write) {
  236. fd_prot->prot_buf = kzalloc(prot_size, GFP_KERNEL);
  237. if (!fd_prot->prot_buf) {
  238. pr_err("Unable to allocate fd_prot->prot_buf\n");
  239. return -ENOMEM;
  240. }
  241. buf = fd_prot->prot_buf;
  242. fd_prot->prot_sg_nents = 1;
  243. fd_prot->prot_sg = kzalloc(sizeof(struct scatterlist),
  244. GFP_KERNEL);
  245. if (!fd_prot->prot_sg) {
  246. pr_err("Unable to allocate fd_prot->prot_sg\n");
  247. kfree(fd_prot->prot_buf);
  248. return -ENOMEM;
  249. }
  250. sg_init_table(fd_prot->prot_sg, fd_prot->prot_sg_nents);
  251. sg_set_buf(fd_prot->prot_sg, buf, prot_size);
  252. }
  253. if (is_write) {
  254. rc = kernel_write(prot_fd, fd_prot->prot_buf, prot_size, pos);
  255. if (rc < 0 || prot_size != rc) {
  256. pr_err("kernel_write() for fd_do_prot_rw failed:"
  257. " %d\n", rc);
  258. ret = -EINVAL;
  259. }
  260. } else {
  261. rc = kernel_read(prot_fd, pos, fd_prot->prot_buf, prot_size);
  262. if (rc < 0) {
  263. pr_err("kernel_read() for fd_do_prot_rw failed:"
  264. " %d\n", rc);
  265. ret = -EINVAL;
  266. }
  267. }
  268. if (is_write || ret < 0) {
  269. kfree(fd_prot->prot_sg);
  270. kfree(fd_prot->prot_buf);
  271. }
  272. return ret;
  273. }
  274. static int fd_do_rw(struct se_cmd *cmd, struct scatterlist *sgl,
  275. u32 sgl_nents, int is_write)
  276. {
  277. struct se_device *se_dev = cmd->se_dev;
  278. struct fd_dev *dev = FD_DEV(se_dev);
  279. struct file *fd = dev->fd_file;
  280. struct scatterlist *sg;
  281. struct iovec *iov;
  282. mm_segment_t old_fs;
  283. loff_t pos = (cmd->t_task_lba * se_dev->dev_attrib.block_size);
  284. int ret = 0, i;
  285. iov = kzalloc(sizeof(struct iovec) * sgl_nents, GFP_KERNEL);
  286. if (!iov) {
  287. pr_err("Unable to allocate fd_do_readv iov[]\n");
  288. return -ENOMEM;
  289. }
  290. for_each_sg(sgl, sg, sgl_nents, i) {
  291. iov[i].iov_len = sg->length;
  292. iov[i].iov_base = kmap(sg_page(sg)) + sg->offset;
  293. }
  294. old_fs = get_fs();
  295. set_fs(get_ds());
  296. if (is_write)
  297. ret = vfs_writev(fd, &iov[0], sgl_nents, &pos);
  298. else
  299. ret = vfs_readv(fd, &iov[0], sgl_nents, &pos);
  300. set_fs(old_fs);
  301. for_each_sg(sgl, sg, sgl_nents, i)
  302. kunmap(sg_page(sg));
  303. kfree(iov);
  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 unsigned char *
  371. fd_setup_write_same_buf(struct se_cmd *cmd, struct scatterlist *sg,
  372. unsigned int len)
  373. {
  374. struct se_device *se_dev = cmd->se_dev;
  375. unsigned int block_size = se_dev->dev_attrib.block_size;
  376. unsigned int i = 0, end;
  377. unsigned char *buf, *p, *kmap_buf;
  378. buf = kzalloc(min_t(unsigned int, len, PAGE_SIZE), GFP_KERNEL);
  379. if (!buf) {
  380. pr_err("Unable to allocate fd_execute_write_same buf\n");
  381. return NULL;
  382. }
  383. kmap_buf = kmap(sg_page(sg)) + sg->offset;
  384. if (!kmap_buf) {
  385. pr_err("kmap() failed in fd_setup_write_same\n");
  386. kfree(buf);
  387. return NULL;
  388. }
  389. /*
  390. * Fill local *buf to contain multiple WRITE_SAME blocks up to
  391. * min(len, PAGE_SIZE)
  392. */
  393. p = buf;
  394. end = min_t(unsigned int, len, PAGE_SIZE);
  395. while (i < end) {
  396. memcpy(p, kmap_buf, block_size);
  397. i += block_size;
  398. p += block_size;
  399. }
  400. kunmap(sg_page(sg));
  401. return buf;
  402. }
  403. static sense_reason_t
  404. fd_execute_write_same(struct se_cmd *cmd)
  405. {
  406. struct se_device *se_dev = cmd->se_dev;
  407. struct fd_dev *fd_dev = FD_DEV(se_dev);
  408. struct file *f = fd_dev->fd_file;
  409. struct scatterlist *sg;
  410. struct iovec *iov;
  411. mm_segment_t old_fs;
  412. sector_t nolb = sbc_get_write_same_sectors(cmd);
  413. loff_t pos = cmd->t_task_lba * se_dev->dev_attrib.block_size;
  414. unsigned int len, len_tmp, iov_num;
  415. int i, rc;
  416. unsigned char *buf;
  417. if (!nolb) {
  418. target_complete_cmd(cmd, SAM_STAT_GOOD);
  419. return 0;
  420. }
  421. sg = &cmd->t_data_sg[0];
  422. if (cmd->t_data_nents > 1 ||
  423. sg->length != cmd->se_dev->dev_attrib.block_size) {
  424. pr_err("WRITE_SAME: Illegal SGL t_data_nents: %u length: %u"
  425. " block_size: %u\n", cmd->t_data_nents, sg->length,
  426. cmd->se_dev->dev_attrib.block_size);
  427. return TCM_INVALID_CDB_FIELD;
  428. }
  429. len = len_tmp = nolb * se_dev->dev_attrib.block_size;
  430. iov_num = DIV_ROUND_UP(len, PAGE_SIZE);
  431. buf = fd_setup_write_same_buf(cmd, sg, len);
  432. if (!buf)
  433. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  434. iov = vzalloc(sizeof(struct iovec) * iov_num);
  435. if (!iov) {
  436. pr_err("Unable to allocate fd_execute_write_same iovecs\n");
  437. kfree(buf);
  438. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  439. }
  440. /*
  441. * Map the single fabric received scatterlist block now populated
  442. * in *buf into each iovec for I/O submission.
  443. */
  444. for (i = 0; i < iov_num; i++) {
  445. iov[i].iov_base = buf;
  446. iov[i].iov_len = min_t(unsigned int, len_tmp, PAGE_SIZE);
  447. len_tmp -= iov[i].iov_len;
  448. }
  449. old_fs = get_fs();
  450. set_fs(get_ds());
  451. rc = vfs_writev(f, &iov[0], iov_num, &pos);
  452. set_fs(old_fs);
  453. vfree(iov);
  454. kfree(buf);
  455. if (rc < 0 || rc != len) {
  456. pr_err("vfs_writev() returned %d for write same\n", rc);
  457. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  458. }
  459. target_complete_cmd(cmd, SAM_STAT_GOOD);
  460. return 0;
  461. }
  462. static int
  463. fd_do_prot_fill(struct se_device *se_dev, sector_t lba, sector_t nolb,
  464. void *buf, size_t bufsize)
  465. {
  466. struct fd_dev *fd_dev = FD_DEV(se_dev);
  467. struct file *prot_fd = fd_dev->fd_prot_file;
  468. sector_t prot_length, prot;
  469. loff_t pos = lba * se_dev->prot_length;
  470. if (!prot_fd) {
  471. pr_err("Unable to locate fd_dev->fd_prot_file\n");
  472. return -ENODEV;
  473. }
  474. prot_length = nolb * se_dev->prot_length;
  475. for (prot = 0; prot < prot_length;) {
  476. sector_t len = min_t(sector_t, bufsize, prot_length - prot);
  477. ssize_t ret = kernel_write(prot_fd, buf, len, pos + prot);
  478. if (ret != len) {
  479. pr_err("vfs_write to prot file failed: %zd\n", ret);
  480. return ret < 0 ? ret : -ENODEV;
  481. }
  482. prot += ret;
  483. }
  484. return 0;
  485. }
  486. static int
  487. fd_do_prot_unmap(struct se_cmd *cmd, sector_t lba, sector_t nolb)
  488. {
  489. void *buf;
  490. int rc;
  491. buf = (void *)__get_free_page(GFP_KERNEL);
  492. if (!buf) {
  493. pr_err("Unable to allocate FILEIO prot buf\n");
  494. return -ENOMEM;
  495. }
  496. memset(buf, 0xff, PAGE_SIZE);
  497. rc = fd_do_prot_fill(cmd->se_dev, lba, nolb, buf, PAGE_SIZE);
  498. free_page((unsigned long)buf);
  499. return rc;
  500. }
  501. static sense_reason_t
  502. fd_do_unmap(struct se_cmd *cmd, void *priv, sector_t lba, sector_t nolb)
  503. {
  504. struct file *file = priv;
  505. struct inode *inode = file->f_mapping->host;
  506. int ret;
  507. if (cmd->se_dev->dev_attrib.pi_prot_type) {
  508. ret = fd_do_prot_unmap(cmd, lba, nolb);
  509. if (ret)
  510. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  511. }
  512. if (S_ISBLK(inode->i_mode)) {
  513. /* The backend is block device, use discard */
  514. struct block_device *bdev = inode->i_bdev;
  515. ret = blkdev_issue_discard(bdev, lba,
  516. nolb, GFP_KERNEL, 0);
  517. if (ret < 0) {
  518. pr_warn("FILEIO: blkdev_issue_discard() failed: %d\n",
  519. ret);
  520. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  521. }
  522. } else {
  523. /* The backend is normal file, use fallocate */
  524. struct se_device *se_dev = cmd->se_dev;
  525. loff_t pos = lba * se_dev->dev_attrib.block_size;
  526. unsigned int len = nolb * se_dev->dev_attrib.block_size;
  527. int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
  528. if (!file->f_op->fallocate)
  529. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  530. ret = file->f_op->fallocate(file, mode, pos, len);
  531. if (ret < 0) {
  532. pr_warn("FILEIO: fallocate() failed: %d\n", ret);
  533. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  534. }
  535. }
  536. return 0;
  537. }
  538. static sense_reason_t
  539. fd_execute_write_same_unmap(struct se_cmd *cmd)
  540. {
  541. struct se_device *se_dev = cmd->se_dev;
  542. struct fd_dev *fd_dev = FD_DEV(se_dev);
  543. struct file *file = fd_dev->fd_file;
  544. sector_t lba = cmd->t_task_lba;
  545. sector_t nolb = sbc_get_write_same_sectors(cmd);
  546. int ret;
  547. if (!nolb) {
  548. target_complete_cmd(cmd, SAM_STAT_GOOD);
  549. return 0;
  550. }
  551. ret = fd_do_unmap(cmd, file, lba, nolb);
  552. if (ret)
  553. return ret;
  554. target_complete_cmd(cmd, GOOD);
  555. return 0;
  556. }
  557. static sense_reason_t
  558. fd_execute_unmap(struct se_cmd *cmd)
  559. {
  560. struct file *file = FD_DEV(cmd->se_dev)->fd_file;
  561. return sbc_execute_unmap(cmd, fd_do_unmap, file);
  562. }
  563. static sense_reason_t
  564. fd_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
  565. enum dma_data_direction data_direction)
  566. {
  567. struct se_device *dev = cmd->se_dev;
  568. struct fd_prot fd_prot;
  569. sense_reason_t rc;
  570. int ret = 0;
  571. /*
  572. * We are currently limited by the number of iovecs (2048) per
  573. * single vfs_[writev,readv] call.
  574. */
  575. if (cmd->data_length > FD_MAX_BYTES) {
  576. pr_err("FILEIO: Not able to process I/O of %u bytes due to"
  577. "FD_MAX_BYTES: %u iovec count limitiation\n",
  578. cmd->data_length, FD_MAX_BYTES);
  579. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  580. }
  581. /*
  582. * Call vectorized fileio functions to map struct scatterlist
  583. * physical memory addresses to struct iovec virtual memory.
  584. */
  585. if (data_direction == DMA_FROM_DEVICE) {
  586. memset(&fd_prot, 0, sizeof(struct fd_prot));
  587. if (cmd->prot_type) {
  588. ret = fd_do_prot_rw(cmd, &fd_prot, false);
  589. if (ret < 0)
  590. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  591. }
  592. ret = fd_do_rw(cmd, sgl, sgl_nents, 0);
  593. if (ret > 0 && cmd->prot_type) {
  594. u32 sectors = cmd->data_length / dev->dev_attrib.block_size;
  595. rc = sbc_dif_verify_read(cmd, cmd->t_task_lba, sectors,
  596. 0, fd_prot.prot_sg, 0);
  597. if (rc) {
  598. kfree(fd_prot.prot_sg);
  599. kfree(fd_prot.prot_buf);
  600. return rc;
  601. }
  602. kfree(fd_prot.prot_sg);
  603. kfree(fd_prot.prot_buf);
  604. }
  605. } else {
  606. memset(&fd_prot, 0, sizeof(struct fd_prot));
  607. if (cmd->prot_type) {
  608. u32 sectors = cmd->data_length / dev->dev_attrib.block_size;
  609. ret = fd_do_prot_rw(cmd, &fd_prot, false);
  610. if (ret < 0)
  611. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  612. rc = sbc_dif_verify_write(cmd, cmd->t_task_lba, sectors,
  613. 0, fd_prot.prot_sg, 0);
  614. if (rc) {
  615. kfree(fd_prot.prot_sg);
  616. kfree(fd_prot.prot_buf);
  617. return rc;
  618. }
  619. }
  620. ret = fd_do_rw(cmd, sgl, sgl_nents, 1);
  621. /*
  622. * Perform implicit vfs_fsync_range() for fd_do_writev() ops
  623. * for SCSI WRITEs with Forced Unit Access (FUA) set.
  624. * Allow this to happen independent of WCE=0 setting.
  625. */
  626. if (ret > 0 &&
  627. dev->dev_attrib.emulate_fua_write > 0 &&
  628. (cmd->se_cmd_flags & SCF_FUA)) {
  629. struct fd_dev *fd_dev = FD_DEV(dev);
  630. loff_t start = cmd->t_task_lba *
  631. dev->dev_attrib.block_size;
  632. loff_t end;
  633. if (cmd->data_length)
  634. end = start + cmd->data_length - 1;
  635. else
  636. end = LLONG_MAX;
  637. vfs_fsync_range(fd_dev->fd_file, start, end, 1);
  638. }
  639. if (ret > 0 && cmd->prot_type) {
  640. ret = fd_do_prot_rw(cmd, &fd_prot, true);
  641. if (ret < 0)
  642. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  643. }
  644. }
  645. if (ret < 0) {
  646. kfree(fd_prot.prot_sg);
  647. kfree(fd_prot.prot_buf);
  648. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  649. }
  650. if (ret)
  651. target_complete_cmd(cmd, SAM_STAT_GOOD);
  652. return 0;
  653. }
  654. enum {
  655. Opt_fd_dev_name, Opt_fd_dev_size, Opt_fd_buffered_io, Opt_err
  656. };
  657. static match_table_t tokens = {
  658. {Opt_fd_dev_name, "fd_dev_name=%s"},
  659. {Opt_fd_dev_size, "fd_dev_size=%s"},
  660. {Opt_fd_buffered_io, "fd_buffered_io=%d"},
  661. {Opt_err, NULL}
  662. };
  663. static ssize_t fd_set_configfs_dev_params(struct se_device *dev,
  664. const char *page, ssize_t count)
  665. {
  666. struct fd_dev *fd_dev = FD_DEV(dev);
  667. char *orig, *ptr, *arg_p, *opts;
  668. substring_t args[MAX_OPT_ARGS];
  669. int ret = 0, arg, token;
  670. opts = kstrdup(page, GFP_KERNEL);
  671. if (!opts)
  672. return -ENOMEM;
  673. orig = opts;
  674. while ((ptr = strsep(&opts, ",\n")) != NULL) {
  675. if (!*ptr)
  676. continue;
  677. token = match_token(ptr, tokens, args);
  678. switch (token) {
  679. case Opt_fd_dev_name:
  680. if (match_strlcpy(fd_dev->fd_dev_name, &args[0],
  681. FD_MAX_DEV_NAME) == 0) {
  682. ret = -EINVAL;
  683. break;
  684. }
  685. pr_debug("FILEIO: Referencing Path: %s\n",
  686. fd_dev->fd_dev_name);
  687. fd_dev->fbd_flags |= FBDF_HAS_PATH;
  688. break;
  689. case Opt_fd_dev_size:
  690. arg_p = match_strdup(&args[0]);
  691. if (!arg_p) {
  692. ret = -ENOMEM;
  693. break;
  694. }
  695. ret = kstrtoull(arg_p, 0, &fd_dev->fd_dev_size);
  696. kfree(arg_p);
  697. if (ret < 0) {
  698. pr_err("kstrtoull() failed for"
  699. " fd_dev_size=\n");
  700. goto out;
  701. }
  702. pr_debug("FILEIO: Referencing Size: %llu"
  703. " bytes\n", fd_dev->fd_dev_size);
  704. fd_dev->fbd_flags |= FBDF_HAS_SIZE;
  705. break;
  706. case Opt_fd_buffered_io:
  707. ret = match_int(args, &arg);
  708. if (ret)
  709. goto out;
  710. if (arg != 1) {
  711. pr_err("bogus fd_buffered_io=%d value\n", arg);
  712. ret = -EINVAL;
  713. goto out;
  714. }
  715. pr_debug("FILEIO: Using buffered I/O"
  716. " operations for struct fd_dev\n");
  717. fd_dev->fbd_flags |= FDBD_HAS_BUFFERED_IO_WCE;
  718. break;
  719. default:
  720. break;
  721. }
  722. }
  723. out:
  724. kfree(orig);
  725. return (!ret) ? count : ret;
  726. }
  727. static ssize_t fd_show_configfs_dev_params(struct se_device *dev, char *b)
  728. {
  729. struct fd_dev *fd_dev = FD_DEV(dev);
  730. ssize_t bl = 0;
  731. bl = sprintf(b + bl, "TCM FILEIO ID: %u", fd_dev->fd_dev_id);
  732. bl += sprintf(b + bl, " File: %s Size: %llu Mode: %s\n",
  733. fd_dev->fd_dev_name, fd_dev->fd_dev_size,
  734. (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) ?
  735. "Buffered-WCE" : "O_DSYNC");
  736. return bl;
  737. }
  738. static sector_t fd_get_blocks(struct se_device *dev)
  739. {
  740. struct fd_dev *fd_dev = FD_DEV(dev);
  741. struct file *f = fd_dev->fd_file;
  742. struct inode *i = f->f_mapping->host;
  743. unsigned long long dev_size;
  744. /*
  745. * When using a file that references an underlying struct block_device,
  746. * ensure dev_size is always based on the current inode size in order
  747. * to handle underlying block_device resize operations.
  748. */
  749. if (S_ISBLK(i->i_mode))
  750. dev_size = i_size_read(i);
  751. else
  752. dev_size = fd_dev->fd_dev_size;
  753. return div_u64(dev_size - dev->dev_attrib.block_size,
  754. dev->dev_attrib.block_size);
  755. }
  756. static int fd_init_prot(struct se_device *dev)
  757. {
  758. struct fd_dev *fd_dev = FD_DEV(dev);
  759. struct file *prot_file, *file = fd_dev->fd_file;
  760. struct inode *inode;
  761. int ret, flags = O_RDWR | O_CREAT | O_LARGEFILE | O_DSYNC;
  762. char buf[FD_MAX_DEV_PROT_NAME];
  763. if (!file) {
  764. pr_err("Unable to locate fd_dev->fd_file\n");
  765. return -ENODEV;
  766. }
  767. inode = file->f_mapping->host;
  768. if (S_ISBLK(inode->i_mode)) {
  769. pr_err("FILEIO Protection emulation only supported on"
  770. " !S_ISBLK\n");
  771. return -ENOSYS;
  772. }
  773. if (fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE)
  774. flags &= ~O_DSYNC;
  775. snprintf(buf, FD_MAX_DEV_PROT_NAME, "%s.protection",
  776. fd_dev->fd_dev_name);
  777. prot_file = filp_open(buf, flags, 0600);
  778. if (IS_ERR(prot_file)) {
  779. pr_err("filp_open(%s) failed\n", buf);
  780. ret = PTR_ERR(prot_file);
  781. return ret;
  782. }
  783. fd_dev->fd_prot_file = prot_file;
  784. return 0;
  785. }
  786. static int fd_format_prot(struct se_device *dev)
  787. {
  788. unsigned char *buf;
  789. int unit_size = FDBD_FORMAT_UNIT_SIZE * dev->dev_attrib.block_size;
  790. int ret;
  791. if (!dev->dev_attrib.pi_prot_type) {
  792. pr_err("Unable to format_prot while pi_prot_type == 0\n");
  793. return -ENODEV;
  794. }
  795. buf = vzalloc(unit_size);
  796. if (!buf) {
  797. pr_err("Unable to allocate FILEIO prot buf\n");
  798. return -ENOMEM;
  799. }
  800. pr_debug("Using FILEIO prot_length: %llu\n",
  801. (unsigned long long)(dev->transport->get_blocks(dev) + 1) *
  802. dev->prot_length);
  803. memset(buf, 0xff, unit_size);
  804. ret = fd_do_prot_fill(dev, 0, dev->transport->get_blocks(dev) + 1,
  805. buf, unit_size);
  806. vfree(buf);
  807. return ret;
  808. }
  809. static void fd_free_prot(struct se_device *dev)
  810. {
  811. struct fd_dev *fd_dev = FD_DEV(dev);
  812. if (!fd_dev->fd_prot_file)
  813. return;
  814. filp_close(fd_dev->fd_prot_file, NULL);
  815. fd_dev->fd_prot_file = NULL;
  816. }
  817. static struct sbc_ops fd_sbc_ops = {
  818. .execute_rw = fd_execute_rw,
  819. .execute_sync_cache = fd_execute_sync_cache,
  820. .execute_write_same = fd_execute_write_same,
  821. .execute_write_same_unmap = fd_execute_write_same_unmap,
  822. .execute_unmap = fd_execute_unmap,
  823. };
  824. static sense_reason_t
  825. fd_parse_cdb(struct se_cmd *cmd)
  826. {
  827. return sbc_parse_cdb(cmd, &fd_sbc_ops);
  828. }
  829. static struct se_subsystem_api fileio_template = {
  830. .name = "fileio",
  831. .inquiry_prod = "FILEIO",
  832. .inquiry_rev = FD_VERSION,
  833. .owner = THIS_MODULE,
  834. .transport_type = TRANSPORT_PLUGIN_VHBA_PDEV,
  835. .attach_hba = fd_attach_hba,
  836. .detach_hba = fd_detach_hba,
  837. .alloc_device = fd_alloc_device,
  838. .configure_device = fd_configure_device,
  839. .free_device = fd_free_device,
  840. .parse_cdb = fd_parse_cdb,
  841. .set_configfs_dev_params = fd_set_configfs_dev_params,
  842. .show_configfs_dev_params = fd_show_configfs_dev_params,
  843. .get_device_type = sbc_get_device_type,
  844. .get_blocks = fd_get_blocks,
  845. .init_prot = fd_init_prot,
  846. .format_prot = fd_format_prot,
  847. .free_prot = fd_free_prot,
  848. };
  849. static int __init fileio_module_init(void)
  850. {
  851. return transport_subsystem_register(&fileio_template);
  852. }
  853. static void __exit fileio_module_exit(void)
  854. {
  855. transport_subsystem_release(&fileio_template);
  856. }
  857. MODULE_DESCRIPTION("TCM FILEIO subsystem plugin");
  858. MODULE_AUTHOR("nab@Linux-iSCSI.org");
  859. MODULE_LICENSE("GPL");
  860. module_init(fileio_module_init);
  861. module_exit(fileio_module_exit);