target_core_iblock.c 17 KB

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