target_core_iblock.c 18 KB

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