target_core_iblock.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /*******************************************************************************
  2. * Filename: target_core_iblock.c
  3. *
  4. * This file contains the Storage Engine <-> Linux BlockIO transport
  5. * specific functions.
  6. *
  7. * Copyright (c) 2003, 2004, 2005 PyX Technologies, Inc.
  8. * Copyright (c) 2005, 2006, 2007 SBE, Inc.
  9. * Copyright (c) 2007-2010 Rising Tide Systems
  10. * Copyright (c) 2008-2010 Linux-iSCSI.org
  11. *
  12. * Nicholas A. Bellinger <nab@kernel.org>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  27. *
  28. ******************************************************************************/
  29. #include <linux/string.h>
  30. #include <linux/parser.h>
  31. #include <linux/timer.h>
  32. #include <linux/fs.h>
  33. #include <linux/blkdev.h>
  34. #include <linux/slab.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/bio.h>
  37. #include <linux/genhd.h>
  38. #include <linux/file.h>
  39. #include <linux/module.h>
  40. #include <scsi/scsi.h>
  41. #include <scsi/scsi_host.h>
  42. #include <target/target_core_base.h>
  43. #include <target/target_core_backend.h>
  44. #include "target_core_iblock.h"
  45. #define IBLOCK_MAX_BIO_PER_TASK 32 /* max # of bios to submit at a time */
  46. #define IBLOCK_BIO_POOL_SIZE 128
  47. static struct se_subsystem_api iblock_template;
  48. static void iblock_bio_done(struct bio *, int);
  49. /* iblock_attach_hba(): (Part of se_subsystem_api_t template)
  50. *
  51. *
  52. */
  53. static int iblock_attach_hba(struct se_hba *hba, u32 host_id)
  54. {
  55. pr_debug("CORE_HBA[%d] - TCM iBlock HBA Driver %s on"
  56. " Generic Target Core Stack %s\n", hba->hba_id,
  57. IBLOCK_VERSION, TARGET_CORE_MOD_VERSION);
  58. return 0;
  59. }
  60. static void iblock_detach_hba(struct se_hba *hba)
  61. {
  62. }
  63. static void *iblock_allocate_virtdevice(struct se_hba *hba, const char *name)
  64. {
  65. struct iblock_dev *ib_dev = NULL;
  66. ib_dev = kzalloc(sizeof(struct iblock_dev), GFP_KERNEL);
  67. if (!ib_dev) {
  68. pr_err("Unable to allocate struct iblock_dev\n");
  69. return NULL;
  70. }
  71. pr_debug( "IBLOCK: Allocated ib_dev for %s\n", name);
  72. return ib_dev;
  73. }
  74. static struct se_device *iblock_create_virtdevice(
  75. struct se_hba *hba,
  76. struct se_subsystem_dev *se_dev,
  77. void *p)
  78. {
  79. struct iblock_dev *ib_dev = p;
  80. struct se_device *dev;
  81. struct se_dev_limits dev_limits;
  82. struct block_device *bd = NULL;
  83. struct request_queue *q;
  84. struct queue_limits *limits;
  85. u32 dev_flags = 0;
  86. fmode_t mode;
  87. int ret = -EINVAL;
  88. if (!ib_dev) {
  89. pr_err("Unable to locate struct iblock_dev parameter\n");
  90. return ERR_PTR(ret);
  91. }
  92. memset(&dev_limits, 0, sizeof(struct se_dev_limits));
  93. ib_dev->ibd_bio_set = bioset_create(IBLOCK_BIO_POOL_SIZE, 0);
  94. if (!ib_dev->ibd_bio_set) {
  95. pr_err("IBLOCK: Unable to create bioset()\n");
  96. return ERR_PTR(-ENOMEM);
  97. }
  98. pr_debug("IBLOCK: Created bio_set()\n");
  99. /*
  100. * iblock_check_configfs_dev_params() ensures that ib_dev->ibd_udev_path
  101. * must already have been set in order for echo 1 > $HBA/$DEV/enable to run.
  102. */
  103. pr_debug( "IBLOCK: Claiming struct block_device: %s\n",
  104. ib_dev->ibd_udev_path);
  105. mode = FMODE_READ|FMODE_EXCL;
  106. if (!ib_dev->ibd_readonly)
  107. mode |= FMODE_WRITE;
  108. bd = blkdev_get_by_path(ib_dev->ibd_udev_path, mode, ib_dev);
  109. if (IS_ERR(bd)) {
  110. ret = PTR_ERR(bd);
  111. goto failed;
  112. }
  113. /*
  114. * Setup the local scope queue_limits from struct request_queue->limits
  115. * to pass into transport_add_device_to_core_hba() as struct se_dev_limits.
  116. */
  117. q = bdev_get_queue(bd);
  118. limits = &dev_limits.limits;
  119. limits->logical_block_size = bdev_logical_block_size(bd);
  120. limits->max_hw_sectors = UINT_MAX;
  121. limits->max_sectors = UINT_MAX;
  122. dev_limits.hw_queue_depth = q->nr_requests;
  123. dev_limits.queue_depth = q->nr_requests;
  124. ib_dev->ibd_bd = bd;
  125. dev = transport_add_device_to_core_hba(hba,
  126. &iblock_template, se_dev, dev_flags, ib_dev,
  127. &dev_limits, "IBLOCK", IBLOCK_VERSION);
  128. if (!dev)
  129. goto failed;
  130. /*
  131. * Check if the underlying struct block_device request_queue supports
  132. * the QUEUE_FLAG_DISCARD bit for UNMAP/WRITE_SAME in SCSI + TRIM
  133. * in ATA and we need to set TPE=1
  134. */
  135. if (blk_queue_discard(q)) {
  136. dev->se_sub_dev->se_dev_attrib.max_unmap_lba_count =
  137. q->limits.max_discard_sectors;
  138. /*
  139. * Currently hardcoded to 1 in Linux/SCSI code..
  140. */
  141. dev->se_sub_dev->se_dev_attrib.max_unmap_block_desc_count = 1;
  142. dev->se_sub_dev->se_dev_attrib.unmap_granularity =
  143. q->limits.discard_granularity >> 9;
  144. dev->se_sub_dev->se_dev_attrib.unmap_granularity_alignment =
  145. q->limits.discard_alignment;
  146. pr_debug("IBLOCK: BLOCK Discard support available,"
  147. " disabled by default\n");
  148. }
  149. if (blk_queue_nonrot(q))
  150. dev->se_sub_dev->se_dev_attrib.is_nonrot = 1;
  151. return dev;
  152. failed:
  153. if (ib_dev->ibd_bio_set) {
  154. bioset_free(ib_dev->ibd_bio_set);
  155. ib_dev->ibd_bio_set = NULL;
  156. }
  157. ib_dev->ibd_bd = NULL;
  158. return ERR_PTR(ret);
  159. }
  160. static void iblock_free_device(void *p)
  161. {
  162. struct iblock_dev *ib_dev = p;
  163. if (ib_dev->ibd_bd != NULL)
  164. blkdev_put(ib_dev->ibd_bd, FMODE_WRITE|FMODE_READ|FMODE_EXCL);
  165. if (ib_dev->ibd_bio_set != NULL)
  166. bioset_free(ib_dev->ibd_bio_set);
  167. kfree(ib_dev);
  168. }
  169. static unsigned long long iblock_emulate_read_cap_with_block_size(
  170. struct se_device *dev,
  171. struct block_device *bd,
  172. struct request_queue *q)
  173. {
  174. unsigned long long blocks_long = (div_u64(i_size_read(bd->bd_inode),
  175. bdev_logical_block_size(bd)) - 1);
  176. u32 block_size = bdev_logical_block_size(bd);
  177. if (block_size == dev->se_sub_dev->se_dev_attrib.block_size)
  178. return blocks_long;
  179. switch (block_size) {
  180. case 4096:
  181. switch (dev->se_sub_dev->se_dev_attrib.block_size) {
  182. case 2048:
  183. blocks_long <<= 1;
  184. break;
  185. case 1024:
  186. blocks_long <<= 2;
  187. break;
  188. case 512:
  189. blocks_long <<= 3;
  190. default:
  191. break;
  192. }
  193. break;
  194. case 2048:
  195. switch (dev->se_sub_dev->se_dev_attrib.block_size) {
  196. case 4096:
  197. blocks_long >>= 1;
  198. break;
  199. case 1024:
  200. blocks_long <<= 1;
  201. break;
  202. case 512:
  203. blocks_long <<= 2;
  204. break;
  205. default:
  206. break;
  207. }
  208. break;
  209. case 1024:
  210. switch (dev->se_sub_dev->se_dev_attrib.block_size) {
  211. case 4096:
  212. blocks_long >>= 2;
  213. break;
  214. case 2048:
  215. blocks_long >>= 1;
  216. break;
  217. case 512:
  218. blocks_long <<= 1;
  219. break;
  220. default:
  221. break;
  222. }
  223. break;
  224. case 512:
  225. switch (dev->se_sub_dev->se_dev_attrib.block_size) {
  226. case 4096:
  227. blocks_long >>= 3;
  228. break;
  229. case 2048:
  230. blocks_long >>= 2;
  231. break;
  232. case 1024:
  233. blocks_long >>= 1;
  234. break;
  235. default:
  236. break;
  237. }
  238. break;
  239. default:
  240. break;
  241. }
  242. return blocks_long;
  243. }
  244. static void iblock_end_io_flush(struct bio *bio, int err)
  245. {
  246. struct se_cmd *cmd = bio->bi_private;
  247. if (err)
  248. pr_err("IBLOCK: cache flush failed: %d\n", err);
  249. if (cmd) {
  250. if (err) {
  251. cmd->scsi_sense_reason =
  252. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  253. target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
  254. } else {
  255. target_complete_cmd(cmd, SAM_STAT_GOOD);
  256. }
  257. }
  258. bio_put(bio);
  259. }
  260. /*
  261. * Implement SYCHRONIZE CACHE. Note that we can't handle lba ranges and must
  262. * always flush the whole cache.
  263. */
  264. static int iblock_execute_sync_cache(struct se_cmd *cmd)
  265. {
  266. struct iblock_dev *ib_dev = cmd->se_dev->dev_ptr;
  267. int immed = (cmd->t_task_cdb[1] & 0x2);
  268. struct bio *bio;
  269. /*
  270. * If the Immediate bit is set, queue up the GOOD response
  271. * for this SYNCHRONIZE_CACHE op.
  272. */
  273. if (immed)
  274. target_complete_cmd(cmd, SAM_STAT_GOOD);
  275. bio = bio_alloc(GFP_KERNEL, 0);
  276. bio->bi_end_io = iblock_end_io_flush;
  277. bio->bi_bdev = ib_dev->ibd_bd;
  278. if (!immed)
  279. bio->bi_private = cmd;
  280. submit_bio(WRITE_FLUSH, bio);
  281. return 0;
  282. }
  283. static int iblock_do_discard(struct se_device *dev, sector_t lba, u32 range)
  284. {
  285. struct iblock_dev *ibd = dev->dev_ptr;
  286. struct block_device *bd = ibd->ibd_bd;
  287. int barrier = 0;
  288. return blkdev_issue_discard(bd, lba, range, GFP_KERNEL, barrier);
  289. }
  290. static int iblock_execute_write_same(struct se_cmd *cmd)
  291. {
  292. struct iblock_dev *ibd = cmd->se_dev->dev_ptr;
  293. int ret;
  294. ret = blkdev_issue_discard(ibd->ibd_bd, cmd->t_task_lba,
  295. spc_get_write_same_sectors(cmd), GFP_KERNEL,
  296. 0);
  297. if (ret < 0) {
  298. pr_debug("blkdev_issue_discard() failed for WRITE_SAME\n");
  299. return ret;
  300. }
  301. target_complete_cmd(cmd, GOOD);
  302. return 0;
  303. }
  304. enum {
  305. Opt_udev_path, Opt_readonly, Opt_force, Opt_err
  306. };
  307. static match_table_t tokens = {
  308. {Opt_udev_path, "udev_path=%s"},
  309. {Opt_readonly, "readonly=%d"},
  310. {Opt_force, "force=%d"},
  311. {Opt_err, NULL}
  312. };
  313. static ssize_t iblock_set_configfs_dev_params(struct se_hba *hba,
  314. struct se_subsystem_dev *se_dev,
  315. const char *page, ssize_t count)
  316. {
  317. struct iblock_dev *ib_dev = se_dev->se_dev_su_ptr;
  318. char *orig, *ptr, *arg_p, *opts;
  319. substring_t args[MAX_OPT_ARGS];
  320. int ret = 0, token;
  321. unsigned long tmp_readonly;
  322. opts = kstrdup(page, GFP_KERNEL);
  323. if (!opts)
  324. return -ENOMEM;
  325. orig = opts;
  326. while ((ptr = strsep(&opts, ",\n")) != NULL) {
  327. if (!*ptr)
  328. continue;
  329. token = match_token(ptr, tokens, args);
  330. switch (token) {
  331. case Opt_udev_path:
  332. if (ib_dev->ibd_bd) {
  333. pr_err("Unable to set udev_path= while"
  334. " ib_dev->ibd_bd exists\n");
  335. ret = -EEXIST;
  336. goto out;
  337. }
  338. arg_p = match_strdup(&args[0]);
  339. if (!arg_p) {
  340. ret = -ENOMEM;
  341. break;
  342. }
  343. snprintf(ib_dev->ibd_udev_path, SE_UDEV_PATH_LEN,
  344. "%s", arg_p);
  345. kfree(arg_p);
  346. pr_debug("IBLOCK: Referencing UDEV path: %s\n",
  347. ib_dev->ibd_udev_path);
  348. ib_dev->ibd_flags |= IBDF_HAS_UDEV_PATH;
  349. break;
  350. case Opt_readonly:
  351. arg_p = match_strdup(&args[0]);
  352. if (!arg_p) {
  353. ret = -ENOMEM;
  354. break;
  355. }
  356. ret = strict_strtoul(arg_p, 0, &tmp_readonly);
  357. kfree(arg_p);
  358. if (ret < 0) {
  359. pr_err("strict_strtoul() failed for"
  360. " readonly=\n");
  361. goto out;
  362. }
  363. ib_dev->ibd_readonly = tmp_readonly;
  364. pr_debug("IBLOCK: readonly: %d\n", ib_dev->ibd_readonly);
  365. break;
  366. case Opt_force:
  367. break;
  368. default:
  369. break;
  370. }
  371. }
  372. out:
  373. kfree(orig);
  374. return (!ret) ? count : ret;
  375. }
  376. static ssize_t iblock_check_configfs_dev_params(
  377. struct se_hba *hba,
  378. struct se_subsystem_dev *se_dev)
  379. {
  380. struct iblock_dev *ibd = se_dev->se_dev_su_ptr;
  381. if (!(ibd->ibd_flags & IBDF_HAS_UDEV_PATH)) {
  382. pr_err("Missing udev_path= parameters for IBLOCK\n");
  383. return -EINVAL;
  384. }
  385. return 0;
  386. }
  387. static ssize_t iblock_show_configfs_dev_params(
  388. struct se_hba *hba,
  389. struct se_subsystem_dev *se_dev,
  390. char *b)
  391. {
  392. struct iblock_dev *ibd = se_dev->se_dev_su_ptr;
  393. struct block_device *bd = ibd->ibd_bd;
  394. char buf[BDEVNAME_SIZE];
  395. ssize_t bl = 0;
  396. if (bd)
  397. bl += sprintf(b + bl, "iBlock device: %s",
  398. bdevname(bd, buf));
  399. if (ibd->ibd_flags & IBDF_HAS_UDEV_PATH)
  400. bl += sprintf(b + bl, " UDEV PATH: %s",
  401. ibd->ibd_udev_path);
  402. bl += sprintf(b + bl, " readonly: %d\n", ibd->ibd_readonly);
  403. bl += sprintf(b + bl, " ");
  404. if (bd) {
  405. bl += sprintf(b + bl, "Major: %d Minor: %d %s\n",
  406. MAJOR(bd->bd_dev), MINOR(bd->bd_dev), (!bd->bd_contains) ?
  407. "" : (bd->bd_holder == ibd) ?
  408. "CLAIMED: IBLOCK" : "CLAIMED: OS");
  409. } else {
  410. bl += sprintf(b + bl, "Major: 0 Minor: 0\n");
  411. }
  412. return bl;
  413. }
  414. static void iblock_complete_cmd(struct se_cmd *cmd)
  415. {
  416. struct iblock_req *ibr = cmd->priv;
  417. u8 status;
  418. if (!atomic_dec_and_test(&ibr->pending))
  419. return;
  420. if (atomic_read(&ibr->ib_bio_err_cnt))
  421. status = SAM_STAT_CHECK_CONDITION;
  422. else
  423. status = SAM_STAT_GOOD;
  424. target_complete_cmd(cmd, status);
  425. kfree(ibr);
  426. }
  427. static void iblock_bio_destructor(struct bio *bio)
  428. {
  429. struct se_cmd *cmd = bio->bi_private;
  430. struct iblock_dev *ib_dev = cmd->se_dev->dev_ptr;
  431. bio_free(bio, ib_dev->ibd_bio_set);
  432. }
  433. static struct bio *
  434. iblock_get_bio(struct se_cmd *cmd, sector_t lba, u32 sg_num)
  435. {
  436. struct iblock_dev *ib_dev = cmd->se_dev->dev_ptr;
  437. struct bio *bio;
  438. /*
  439. * Only allocate as many vector entries as the bio code allows us to,
  440. * we'll loop later on until we have handled the whole request.
  441. */
  442. if (sg_num > BIO_MAX_PAGES)
  443. sg_num = BIO_MAX_PAGES;
  444. bio = bio_alloc_bioset(GFP_NOIO, sg_num, ib_dev->ibd_bio_set);
  445. if (!bio) {
  446. pr_err("Unable to allocate memory for bio\n");
  447. return NULL;
  448. }
  449. bio->bi_bdev = ib_dev->ibd_bd;
  450. bio->bi_private = cmd;
  451. bio->bi_destructor = iblock_bio_destructor;
  452. bio->bi_end_io = &iblock_bio_done;
  453. bio->bi_sector = lba;
  454. return bio;
  455. }
  456. static void iblock_submit_bios(struct bio_list *list, int rw)
  457. {
  458. struct blk_plug plug;
  459. struct bio *bio;
  460. blk_start_plug(&plug);
  461. while ((bio = bio_list_pop(list)))
  462. submit_bio(rw, bio);
  463. blk_finish_plug(&plug);
  464. }
  465. static int iblock_execute_rw(struct se_cmd *cmd)
  466. {
  467. struct scatterlist *sgl = cmd->t_data_sg;
  468. u32 sgl_nents = cmd->t_data_nents;
  469. enum dma_data_direction data_direction = cmd->data_direction;
  470. struct se_device *dev = cmd->se_dev;
  471. struct iblock_req *ibr;
  472. struct bio *bio;
  473. struct bio_list list;
  474. struct scatterlist *sg;
  475. u32 sg_num = sgl_nents;
  476. sector_t block_lba;
  477. unsigned bio_cnt;
  478. int rw;
  479. int i;
  480. if (data_direction == DMA_TO_DEVICE) {
  481. /*
  482. * Force data to disk if we pretend to not have a volatile
  483. * write cache, or the initiator set the Force Unit Access bit.
  484. */
  485. if (dev->se_sub_dev->se_dev_attrib.emulate_write_cache == 0 ||
  486. (dev->se_sub_dev->se_dev_attrib.emulate_fua_write > 0 &&
  487. (cmd->se_cmd_flags & SCF_FUA)))
  488. rw = WRITE_FUA;
  489. else
  490. rw = WRITE;
  491. } else {
  492. rw = READ;
  493. }
  494. /*
  495. * Convert the blocksize advertised to the initiator to the 512 byte
  496. * units unconditionally used by the Linux block layer.
  497. */
  498. if (dev->se_sub_dev->se_dev_attrib.block_size == 4096)
  499. block_lba = (cmd->t_task_lba << 3);
  500. else if (dev->se_sub_dev->se_dev_attrib.block_size == 2048)
  501. block_lba = (cmd->t_task_lba << 2);
  502. else if (dev->se_sub_dev->se_dev_attrib.block_size == 1024)
  503. block_lba = (cmd->t_task_lba << 1);
  504. else if (dev->se_sub_dev->se_dev_attrib.block_size == 512)
  505. block_lba = cmd->t_task_lba;
  506. else {
  507. pr_err("Unsupported SCSI -> BLOCK LBA conversion:"
  508. " %u\n", dev->se_sub_dev->se_dev_attrib.block_size);
  509. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  510. return -ENOSYS;
  511. }
  512. ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
  513. if (!ibr)
  514. goto fail;
  515. cmd->priv = ibr;
  516. bio = iblock_get_bio(cmd, block_lba, sgl_nents);
  517. if (!bio)
  518. goto fail_free_ibr;
  519. bio_list_init(&list);
  520. bio_list_add(&list, bio);
  521. atomic_set(&ibr->pending, 2);
  522. bio_cnt = 1;
  523. for_each_sg(sgl, sg, sgl_nents, i) {
  524. /*
  525. * XXX: if the length the device accepts is shorter than the
  526. * length of the S/G list entry this will cause and
  527. * endless loop. Better hope no driver uses huge pages.
  528. */
  529. while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
  530. != sg->length) {
  531. if (bio_cnt >= IBLOCK_MAX_BIO_PER_TASK) {
  532. iblock_submit_bios(&list, rw);
  533. bio_cnt = 0;
  534. }
  535. bio = iblock_get_bio(cmd, block_lba, sg_num);
  536. if (!bio)
  537. goto fail_put_bios;
  538. atomic_inc(&ibr->pending);
  539. bio_list_add(&list, bio);
  540. bio_cnt++;
  541. }
  542. /* Always in 512 byte units for Linux/Block */
  543. block_lba += sg->length >> IBLOCK_LBA_SHIFT;
  544. sg_num--;
  545. }
  546. iblock_submit_bios(&list, rw);
  547. iblock_complete_cmd(cmd);
  548. return 0;
  549. fail_put_bios:
  550. while ((bio = bio_list_pop(&list)))
  551. bio_put(bio);
  552. fail_free_ibr:
  553. kfree(ibr);
  554. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  555. fail:
  556. return -ENOMEM;
  557. }
  558. static u32 iblock_get_device_rev(struct se_device *dev)
  559. {
  560. return SCSI_SPC_2; /* Returns SPC-3 in Initiator Data */
  561. }
  562. static u32 iblock_get_device_type(struct se_device *dev)
  563. {
  564. return TYPE_DISK;
  565. }
  566. static sector_t iblock_get_blocks(struct se_device *dev)
  567. {
  568. struct iblock_dev *ibd = dev->dev_ptr;
  569. struct block_device *bd = ibd->ibd_bd;
  570. struct request_queue *q = bdev_get_queue(bd);
  571. return iblock_emulate_read_cap_with_block_size(dev, bd, q);
  572. }
  573. static void iblock_bio_done(struct bio *bio, int err)
  574. {
  575. struct se_cmd *cmd = bio->bi_private;
  576. struct iblock_req *ibr = cmd->priv;
  577. /*
  578. * Set -EIO if !BIO_UPTODATE and the passed is still err=0
  579. */
  580. if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && !err)
  581. err = -EIO;
  582. if (err != 0) {
  583. pr_err("test_bit(BIO_UPTODATE) failed for bio: %p,"
  584. " err: %d\n", bio, err);
  585. /*
  586. * Bump the ib_bio_err_cnt and release bio.
  587. */
  588. atomic_inc(&ibr->ib_bio_err_cnt);
  589. smp_mb__after_atomic_inc();
  590. }
  591. bio_put(bio);
  592. iblock_complete_cmd(cmd);
  593. }
  594. static struct spc_ops iblock_spc_ops = {
  595. .execute_rw = iblock_execute_rw,
  596. .execute_sync_cache = iblock_execute_sync_cache,
  597. .execute_write_same = iblock_execute_write_same,
  598. };
  599. static int iblock_parse_cdb(struct se_cmd *cmd)
  600. {
  601. return sbc_parse_cdb(cmd, &iblock_spc_ops);
  602. }
  603. static struct se_subsystem_api iblock_template = {
  604. .name = "iblock",
  605. .owner = THIS_MODULE,
  606. .transport_type = TRANSPORT_PLUGIN_VHBA_PDEV,
  607. .write_cache_emulated = 1,
  608. .fua_write_emulated = 1,
  609. .attach_hba = iblock_attach_hba,
  610. .detach_hba = iblock_detach_hba,
  611. .allocate_virtdevice = iblock_allocate_virtdevice,
  612. .create_virtdevice = iblock_create_virtdevice,
  613. .free_device = iblock_free_device,
  614. .parse_cdb = iblock_parse_cdb,
  615. .do_discard = iblock_do_discard,
  616. .check_configfs_dev_params = iblock_check_configfs_dev_params,
  617. .set_configfs_dev_params = iblock_set_configfs_dev_params,
  618. .show_configfs_dev_params = iblock_show_configfs_dev_params,
  619. .get_device_rev = iblock_get_device_rev,
  620. .get_device_type = iblock_get_device_type,
  621. .get_blocks = iblock_get_blocks,
  622. };
  623. static int __init iblock_module_init(void)
  624. {
  625. return transport_subsystem_register(&iblock_template);
  626. }
  627. static void iblock_module_exit(void)
  628. {
  629. transport_subsystem_release(&iblock_template);
  630. }
  631. MODULE_DESCRIPTION("TCM IBLOCK subsystem plugin");
  632. MODULE_AUTHOR("nab@Linux-iSCSI.org");
  633. MODULE_LICENSE("GPL");
  634. module_init(iblock_module_init);
  635. module_exit(iblock_module_exit);