target_core_iblock.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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. enum {
  291. Opt_udev_path, Opt_readonly, Opt_force, Opt_err
  292. };
  293. static match_table_t tokens = {
  294. {Opt_udev_path, "udev_path=%s"},
  295. {Opt_readonly, "readonly=%d"},
  296. {Opt_force, "force=%d"},
  297. {Opt_err, NULL}
  298. };
  299. static ssize_t iblock_set_configfs_dev_params(struct se_hba *hba,
  300. struct se_subsystem_dev *se_dev,
  301. const char *page, ssize_t count)
  302. {
  303. struct iblock_dev *ib_dev = se_dev->se_dev_su_ptr;
  304. char *orig, *ptr, *arg_p, *opts;
  305. substring_t args[MAX_OPT_ARGS];
  306. int ret = 0, token;
  307. unsigned long tmp_readonly;
  308. opts = kstrdup(page, GFP_KERNEL);
  309. if (!opts)
  310. return -ENOMEM;
  311. orig = opts;
  312. while ((ptr = strsep(&opts, ",\n")) != NULL) {
  313. if (!*ptr)
  314. continue;
  315. token = match_token(ptr, tokens, args);
  316. switch (token) {
  317. case Opt_udev_path:
  318. if (ib_dev->ibd_bd) {
  319. pr_err("Unable to set udev_path= while"
  320. " ib_dev->ibd_bd exists\n");
  321. ret = -EEXIST;
  322. goto out;
  323. }
  324. arg_p = match_strdup(&args[0]);
  325. if (!arg_p) {
  326. ret = -ENOMEM;
  327. break;
  328. }
  329. snprintf(ib_dev->ibd_udev_path, SE_UDEV_PATH_LEN,
  330. "%s", arg_p);
  331. kfree(arg_p);
  332. pr_debug("IBLOCK: Referencing UDEV path: %s\n",
  333. ib_dev->ibd_udev_path);
  334. ib_dev->ibd_flags |= IBDF_HAS_UDEV_PATH;
  335. break;
  336. case Opt_readonly:
  337. arg_p = match_strdup(&args[0]);
  338. if (!arg_p) {
  339. ret = -ENOMEM;
  340. break;
  341. }
  342. ret = strict_strtoul(arg_p, 0, &tmp_readonly);
  343. kfree(arg_p);
  344. if (ret < 0) {
  345. pr_err("strict_strtoul() failed for"
  346. " readonly=\n");
  347. goto out;
  348. }
  349. ib_dev->ibd_readonly = tmp_readonly;
  350. pr_debug("IBLOCK: readonly: %d\n", ib_dev->ibd_readonly);
  351. break;
  352. case Opt_force:
  353. break;
  354. default:
  355. break;
  356. }
  357. }
  358. out:
  359. kfree(orig);
  360. return (!ret) ? count : ret;
  361. }
  362. static ssize_t iblock_check_configfs_dev_params(
  363. struct se_hba *hba,
  364. struct se_subsystem_dev *se_dev)
  365. {
  366. struct iblock_dev *ibd = se_dev->se_dev_su_ptr;
  367. if (!(ibd->ibd_flags & IBDF_HAS_UDEV_PATH)) {
  368. pr_err("Missing udev_path= parameters for IBLOCK\n");
  369. return -EINVAL;
  370. }
  371. return 0;
  372. }
  373. static ssize_t iblock_show_configfs_dev_params(
  374. struct se_hba *hba,
  375. struct se_subsystem_dev *se_dev,
  376. char *b)
  377. {
  378. struct iblock_dev *ibd = se_dev->se_dev_su_ptr;
  379. struct block_device *bd = ibd->ibd_bd;
  380. char buf[BDEVNAME_SIZE];
  381. ssize_t bl = 0;
  382. if (bd)
  383. bl += sprintf(b + bl, "iBlock device: %s",
  384. bdevname(bd, buf));
  385. if (ibd->ibd_flags & IBDF_HAS_UDEV_PATH)
  386. bl += sprintf(b + bl, " UDEV PATH: %s",
  387. ibd->ibd_udev_path);
  388. bl += sprintf(b + bl, " readonly: %d\n", ibd->ibd_readonly);
  389. bl += sprintf(b + bl, " ");
  390. if (bd) {
  391. bl += sprintf(b + bl, "Major: %d Minor: %d %s\n",
  392. MAJOR(bd->bd_dev), MINOR(bd->bd_dev), (!bd->bd_contains) ?
  393. "" : (bd->bd_holder == ibd) ?
  394. "CLAIMED: IBLOCK" : "CLAIMED: OS");
  395. } else {
  396. bl += sprintf(b + bl, "Major: 0 Minor: 0\n");
  397. }
  398. return bl;
  399. }
  400. static void iblock_complete_cmd(struct se_cmd *cmd)
  401. {
  402. struct iblock_req *ibr = cmd->priv;
  403. u8 status;
  404. if (!atomic_dec_and_test(&ibr->pending))
  405. return;
  406. if (atomic_read(&ibr->ib_bio_err_cnt))
  407. status = SAM_STAT_CHECK_CONDITION;
  408. else
  409. status = SAM_STAT_GOOD;
  410. target_complete_cmd(cmd, status);
  411. kfree(ibr);
  412. }
  413. static void iblock_bio_destructor(struct bio *bio)
  414. {
  415. struct se_cmd *cmd = bio->bi_private;
  416. struct iblock_dev *ib_dev = cmd->se_dev->dev_ptr;
  417. bio_free(bio, ib_dev->ibd_bio_set);
  418. }
  419. static struct bio *
  420. iblock_get_bio(struct se_cmd *cmd, sector_t lba, u32 sg_num)
  421. {
  422. struct iblock_dev *ib_dev = cmd->se_dev->dev_ptr;
  423. struct bio *bio;
  424. /*
  425. * Only allocate as many vector entries as the bio code allows us to,
  426. * we'll loop later on until we have handled the whole request.
  427. */
  428. if (sg_num > BIO_MAX_PAGES)
  429. sg_num = BIO_MAX_PAGES;
  430. bio = bio_alloc_bioset(GFP_NOIO, sg_num, ib_dev->ibd_bio_set);
  431. if (!bio) {
  432. pr_err("Unable to allocate memory for bio\n");
  433. return NULL;
  434. }
  435. bio->bi_bdev = ib_dev->ibd_bd;
  436. bio->bi_private = cmd;
  437. bio->bi_destructor = iblock_bio_destructor;
  438. bio->bi_end_io = &iblock_bio_done;
  439. bio->bi_sector = lba;
  440. return bio;
  441. }
  442. static void iblock_submit_bios(struct bio_list *list, int rw)
  443. {
  444. struct blk_plug plug;
  445. struct bio *bio;
  446. blk_start_plug(&plug);
  447. while ((bio = bio_list_pop(list)))
  448. submit_bio(rw, bio);
  449. blk_finish_plug(&plug);
  450. }
  451. static int iblock_execute_rw(struct se_cmd *cmd)
  452. {
  453. struct scatterlist *sgl = cmd->t_data_sg;
  454. u32 sgl_nents = cmd->t_data_nents;
  455. enum dma_data_direction data_direction = cmd->data_direction;
  456. struct se_device *dev = cmd->se_dev;
  457. struct iblock_req *ibr;
  458. struct bio *bio;
  459. struct bio_list list;
  460. struct scatterlist *sg;
  461. u32 sg_num = sgl_nents;
  462. sector_t block_lba;
  463. unsigned bio_cnt;
  464. int rw;
  465. int i;
  466. if (data_direction == DMA_TO_DEVICE) {
  467. /*
  468. * Force data to disk if we pretend to not have a volatile
  469. * write cache, or the initiator set the Force Unit Access bit.
  470. */
  471. if (dev->se_sub_dev->se_dev_attrib.emulate_write_cache == 0 ||
  472. (dev->se_sub_dev->se_dev_attrib.emulate_fua_write > 0 &&
  473. (cmd->se_cmd_flags & SCF_FUA)))
  474. rw = WRITE_FUA;
  475. else
  476. rw = WRITE;
  477. } else {
  478. rw = READ;
  479. }
  480. /*
  481. * Convert the blocksize advertised to the initiator to the 512 byte
  482. * units unconditionally used by the Linux block layer.
  483. */
  484. if (dev->se_sub_dev->se_dev_attrib.block_size == 4096)
  485. block_lba = (cmd->t_task_lba << 3);
  486. else if (dev->se_sub_dev->se_dev_attrib.block_size == 2048)
  487. block_lba = (cmd->t_task_lba << 2);
  488. else if (dev->se_sub_dev->se_dev_attrib.block_size == 1024)
  489. block_lba = (cmd->t_task_lba << 1);
  490. else if (dev->se_sub_dev->se_dev_attrib.block_size == 512)
  491. block_lba = cmd->t_task_lba;
  492. else {
  493. pr_err("Unsupported SCSI -> BLOCK LBA conversion:"
  494. " %u\n", dev->se_sub_dev->se_dev_attrib.block_size);
  495. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  496. return -ENOSYS;
  497. }
  498. ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
  499. if (!ibr)
  500. goto fail;
  501. cmd->priv = ibr;
  502. bio = iblock_get_bio(cmd, block_lba, sgl_nents);
  503. if (!bio)
  504. goto fail_free_ibr;
  505. bio_list_init(&list);
  506. bio_list_add(&list, bio);
  507. atomic_set(&ibr->pending, 2);
  508. bio_cnt = 1;
  509. for_each_sg(sgl, sg, sgl_nents, i) {
  510. /*
  511. * XXX: if the length the device accepts is shorter than the
  512. * length of the S/G list entry this will cause and
  513. * endless loop. Better hope no driver uses huge pages.
  514. */
  515. while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
  516. != sg->length) {
  517. if (bio_cnt >= IBLOCK_MAX_BIO_PER_TASK) {
  518. iblock_submit_bios(&list, rw);
  519. bio_cnt = 0;
  520. }
  521. bio = iblock_get_bio(cmd, block_lba, sg_num);
  522. if (!bio)
  523. goto fail_put_bios;
  524. atomic_inc(&ibr->pending);
  525. bio_list_add(&list, bio);
  526. bio_cnt++;
  527. }
  528. /* Always in 512 byte units for Linux/Block */
  529. block_lba += sg->length >> IBLOCK_LBA_SHIFT;
  530. sg_num--;
  531. }
  532. iblock_submit_bios(&list, rw);
  533. iblock_complete_cmd(cmd);
  534. return 0;
  535. fail_put_bios:
  536. while ((bio = bio_list_pop(&list)))
  537. bio_put(bio);
  538. fail_free_ibr:
  539. kfree(ibr);
  540. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  541. fail:
  542. return -ENOMEM;
  543. }
  544. static u32 iblock_get_device_rev(struct se_device *dev)
  545. {
  546. return SCSI_SPC_2; /* Returns SPC-3 in Initiator Data */
  547. }
  548. static u32 iblock_get_device_type(struct se_device *dev)
  549. {
  550. return TYPE_DISK;
  551. }
  552. static sector_t iblock_get_blocks(struct se_device *dev)
  553. {
  554. struct iblock_dev *ibd = dev->dev_ptr;
  555. struct block_device *bd = ibd->ibd_bd;
  556. struct request_queue *q = bdev_get_queue(bd);
  557. return iblock_emulate_read_cap_with_block_size(dev, bd, q);
  558. }
  559. static void iblock_bio_done(struct bio *bio, int err)
  560. {
  561. struct se_cmd *cmd = bio->bi_private;
  562. struct iblock_req *ibr = cmd->priv;
  563. /*
  564. * Set -EIO if !BIO_UPTODATE and the passed is still err=0
  565. */
  566. if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && !err)
  567. err = -EIO;
  568. if (err != 0) {
  569. pr_err("test_bit(BIO_UPTODATE) failed for bio: %p,"
  570. " err: %d\n", bio, err);
  571. /*
  572. * Bump the ib_bio_err_cnt and release bio.
  573. */
  574. atomic_inc(&ibr->ib_bio_err_cnt);
  575. smp_mb__after_atomic_inc();
  576. }
  577. bio_put(bio);
  578. iblock_complete_cmd(cmd);
  579. }
  580. static struct spc_ops iblock_spc_ops = {
  581. .execute_rw = iblock_execute_rw,
  582. .execute_sync_cache = iblock_execute_sync_cache,
  583. };
  584. static int iblock_parse_cdb(struct se_cmd *cmd)
  585. {
  586. return sbc_parse_cdb(cmd, &iblock_spc_ops);
  587. }
  588. static struct se_subsystem_api iblock_template = {
  589. .name = "iblock",
  590. .owner = THIS_MODULE,
  591. .transport_type = TRANSPORT_PLUGIN_VHBA_PDEV,
  592. .write_cache_emulated = 1,
  593. .fua_write_emulated = 1,
  594. .attach_hba = iblock_attach_hba,
  595. .detach_hba = iblock_detach_hba,
  596. .allocate_virtdevice = iblock_allocate_virtdevice,
  597. .create_virtdevice = iblock_create_virtdevice,
  598. .free_device = iblock_free_device,
  599. .parse_cdb = iblock_parse_cdb,
  600. .do_discard = iblock_do_discard,
  601. .check_configfs_dev_params = iblock_check_configfs_dev_params,
  602. .set_configfs_dev_params = iblock_set_configfs_dev_params,
  603. .show_configfs_dev_params = iblock_show_configfs_dev_params,
  604. .get_device_rev = iblock_get_device_rev,
  605. .get_device_type = iblock_get_device_type,
  606. .get_blocks = iblock_get_blocks,
  607. };
  608. static int __init iblock_module_init(void)
  609. {
  610. return transport_subsystem_register(&iblock_template);
  611. }
  612. static void iblock_module_exit(void)
  613. {
  614. transport_subsystem_release(&iblock_template);
  615. }
  616. MODULE_DESCRIPTION("TCM IBLOCK subsystem plugin");
  617. MODULE_AUTHOR("nab@Linux-iSCSI.org");
  618. MODULE_LICENSE("GPL");
  619. module_init(iblock_module_init);
  620. module_exit(iblock_module_exit);