target_core_file.c 26 KB

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