target_core_iblock.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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-2013 Datera, Inc.
  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. struct blk_integrity *bi;
  81. fmode_t mode;
  82. int ret = -ENOMEM;
  83. if (!(ib_dev->ibd_flags & IBDF_HAS_UDEV_PATH)) {
  84. pr_err("Missing udev_path= parameters for IBLOCK\n");
  85. return -EINVAL;
  86. }
  87. ib_dev->ibd_bio_set = bioset_create(IBLOCK_BIO_POOL_SIZE, 0);
  88. if (!ib_dev->ibd_bio_set) {
  89. pr_err("IBLOCK: Unable to create bioset\n");
  90. goto out;
  91. }
  92. pr_debug( "IBLOCK: Claiming struct block_device: %s\n",
  93. ib_dev->ibd_udev_path);
  94. mode = FMODE_READ|FMODE_EXCL;
  95. if (!ib_dev->ibd_readonly)
  96. mode |= FMODE_WRITE;
  97. bd = blkdev_get_by_path(ib_dev->ibd_udev_path, mode, ib_dev);
  98. if (IS_ERR(bd)) {
  99. ret = PTR_ERR(bd);
  100. goto out_free_bioset;
  101. }
  102. ib_dev->ibd_bd = bd;
  103. q = bdev_get_queue(bd);
  104. dev->dev_attrib.hw_block_size = bdev_logical_block_size(bd);
  105. dev->dev_attrib.hw_max_sectors = UINT_MAX;
  106. dev->dev_attrib.hw_queue_depth = q->nr_requests;
  107. /*
  108. * Check if the underlying struct block_device request_queue supports
  109. * the QUEUE_FLAG_DISCARD bit for UNMAP/WRITE_SAME in SCSI + TRIM
  110. * in ATA and we need to set TPE=1
  111. */
  112. if (blk_queue_discard(q)) {
  113. dev->dev_attrib.max_unmap_lba_count =
  114. q->limits.max_discard_sectors;
  115. /*
  116. * Currently hardcoded to 1 in Linux/SCSI code..
  117. */
  118. dev->dev_attrib.max_unmap_block_desc_count = 1;
  119. dev->dev_attrib.unmap_granularity =
  120. q->limits.discard_granularity >> 9;
  121. dev->dev_attrib.unmap_granularity_alignment =
  122. q->limits.discard_alignment;
  123. pr_debug("IBLOCK: BLOCK Discard support available,"
  124. " disabled by default\n");
  125. }
  126. /*
  127. * Enable write same emulation for IBLOCK and use 0xFFFF as
  128. * the smaller WRITE_SAME(10) only has a two-byte block count.
  129. */
  130. dev->dev_attrib.max_write_same_len = 0xFFFF;
  131. if (blk_queue_nonrot(q))
  132. dev->dev_attrib.is_nonrot = 1;
  133. bi = bdev_get_integrity(bd);
  134. if (bi) {
  135. struct bio_set *bs = ib_dev->ibd_bio_set;
  136. if (!strcmp(bi->name, "T10-DIF-TYPE3-IP") ||
  137. !strcmp(bi->name, "T10-DIF-TYPE1-IP")) {
  138. pr_err("IBLOCK export of blk_integrity: %s not"
  139. " supported\n", bi->name);
  140. ret = -ENOSYS;
  141. goto out_blkdev_put;
  142. }
  143. if (!strcmp(bi->name, "T10-DIF-TYPE3-CRC")) {
  144. dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE3_PROT;
  145. } else if (!strcmp(bi->name, "T10-DIF-TYPE1-CRC")) {
  146. dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE1_PROT;
  147. }
  148. if (dev->dev_attrib.pi_prot_type) {
  149. if (bioset_integrity_create(bs, IBLOCK_BIO_POOL_SIZE) < 0) {
  150. pr_err("Unable to allocate bioset for PI\n");
  151. ret = -ENOMEM;
  152. goto out_blkdev_put;
  153. }
  154. pr_debug("IBLOCK setup BIP bs->bio_integrity_pool: %p\n",
  155. bs->bio_integrity_pool);
  156. }
  157. dev->dev_attrib.hw_pi_prot_type = dev->dev_attrib.pi_prot_type;
  158. }
  159. return 0;
  160. out_blkdev_put:
  161. blkdev_put(ib_dev->ibd_bd, FMODE_WRITE|FMODE_READ|FMODE_EXCL);
  162. out_free_bioset:
  163. bioset_free(ib_dev->ibd_bio_set);
  164. ib_dev->ibd_bio_set = NULL;
  165. out:
  166. return ret;
  167. }
  168. static void iblock_free_device(struct se_device *dev)
  169. {
  170. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  171. if (ib_dev->ibd_bd != NULL)
  172. blkdev_put(ib_dev->ibd_bd, FMODE_WRITE|FMODE_READ|FMODE_EXCL);
  173. if (ib_dev->ibd_bio_set != NULL) {
  174. bioset_integrity_free(ib_dev->ibd_bio_set);
  175. bioset_free(ib_dev->ibd_bio_set);
  176. }
  177. kfree(ib_dev);
  178. }
  179. static unsigned long long iblock_emulate_read_cap_with_block_size(
  180. struct se_device *dev,
  181. struct block_device *bd,
  182. struct request_queue *q)
  183. {
  184. unsigned long long blocks_long = (div_u64(i_size_read(bd->bd_inode),
  185. bdev_logical_block_size(bd)) - 1);
  186. u32 block_size = bdev_logical_block_size(bd);
  187. if (block_size == dev->dev_attrib.block_size)
  188. return blocks_long;
  189. switch (block_size) {
  190. case 4096:
  191. switch (dev->dev_attrib.block_size) {
  192. case 2048:
  193. blocks_long <<= 1;
  194. break;
  195. case 1024:
  196. blocks_long <<= 2;
  197. break;
  198. case 512:
  199. blocks_long <<= 3;
  200. default:
  201. break;
  202. }
  203. break;
  204. case 2048:
  205. switch (dev->dev_attrib.block_size) {
  206. case 4096:
  207. blocks_long >>= 1;
  208. break;
  209. case 1024:
  210. blocks_long <<= 1;
  211. break;
  212. case 512:
  213. blocks_long <<= 2;
  214. break;
  215. default:
  216. break;
  217. }
  218. break;
  219. case 1024:
  220. switch (dev->dev_attrib.block_size) {
  221. case 4096:
  222. blocks_long >>= 2;
  223. break;
  224. case 2048:
  225. blocks_long >>= 1;
  226. break;
  227. case 512:
  228. blocks_long <<= 1;
  229. break;
  230. default:
  231. break;
  232. }
  233. break;
  234. case 512:
  235. switch (dev->dev_attrib.block_size) {
  236. case 4096:
  237. blocks_long >>= 3;
  238. break;
  239. case 2048:
  240. blocks_long >>= 2;
  241. break;
  242. case 1024:
  243. blocks_long >>= 1;
  244. break;
  245. default:
  246. break;
  247. }
  248. break;
  249. default:
  250. break;
  251. }
  252. return blocks_long;
  253. }
  254. static void iblock_complete_cmd(struct se_cmd *cmd)
  255. {
  256. struct iblock_req *ibr = cmd->priv;
  257. u8 status;
  258. if (!atomic_dec_and_test(&ibr->pending))
  259. return;
  260. if (atomic_read(&ibr->ib_bio_err_cnt))
  261. status = SAM_STAT_CHECK_CONDITION;
  262. else
  263. status = SAM_STAT_GOOD;
  264. target_complete_cmd(cmd, status);
  265. kfree(ibr);
  266. }
  267. static void iblock_bio_done(struct bio *bio, int err)
  268. {
  269. struct se_cmd *cmd = bio->bi_private;
  270. struct iblock_req *ibr = cmd->priv;
  271. /*
  272. * Set -EIO if !BIO_UPTODATE and the passed is still err=0
  273. */
  274. if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && !err)
  275. err = -EIO;
  276. if (err != 0) {
  277. pr_err("test_bit(BIO_UPTODATE) failed for bio: %p,"
  278. " err: %d\n", bio, err);
  279. /*
  280. * Bump the ib_bio_err_cnt and release bio.
  281. */
  282. atomic_inc(&ibr->ib_bio_err_cnt);
  283. smp_mb__after_atomic_inc();
  284. }
  285. bio_put(bio);
  286. iblock_complete_cmd(cmd);
  287. }
  288. static struct bio *
  289. iblock_get_bio(struct se_cmd *cmd, sector_t lba, u32 sg_num)
  290. {
  291. struct iblock_dev *ib_dev = IBLOCK_DEV(cmd->se_dev);
  292. struct bio *bio;
  293. /*
  294. * Only allocate as many vector entries as the bio code allows us to,
  295. * we'll loop later on until we have handled the whole request.
  296. */
  297. if (sg_num > BIO_MAX_PAGES)
  298. sg_num = BIO_MAX_PAGES;
  299. bio = bio_alloc_bioset(GFP_NOIO, sg_num, ib_dev->ibd_bio_set);
  300. if (!bio) {
  301. pr_err("Unable to allocate memory for bio\n");
  302. return NULL;
  303. }
  304. bio->bi_bdev = ib_dev->ibd_bd;
  305. bio->bi_private = cmd;
  306. bio->bi_end_io = &iblock_bio_done;
  307. bio->bi_iter.bi_sector = lba;
  308. return bio;
  309. }
  310. static void iblock_submit_bios(struct bio_list *list, int rw)
  311. {
  312. struct blk_plug plug;
  313. struct bio *bio;
  314. blk_start_plug(&plug);
  315. while ((bio = bio_list_pop(list)))
  316. submit_bio(rw, bio);
  317. blk_finish_plug(&plug);
  318. }
  319. static void iblock_end_io_flush(struct bio *bio, int err)
  320. {
  321. struct se_cmd *cmd = bio->bi_private;
  322. if (err)
  323. pr_err("IBLOCK: cache flush failed: %d\n", err);
  324. if (cmd) {
  325. if (err)
  326. target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
  327. else
  328. target_complete_cmd(cmd, SAM_STAT_GOOD);
  329. }
  330. bio_put(bio);
  331. }
  332. /*
  333. * Implement SYCHRONIZE CACHE. Note that we can't handle lba ranges and must
  334. * always flush the whole cache.
  335. */
  336. static sense_reason_t
  337. iblock_execute_sync_cache(struct se_cmd *cmd)
  338. {
  339. struct iblock_dev *ib_dev = IBLOCK_DEV(cmd->se_dev);
  340. int immed = (cmd->t_task_cdb[1] & 0x2);
  341. struct bio *bio;
  342. /*
  343. * If the Immediate bit is set, queue up the GOOD response
  344. * for this SYNCHRONIZE_CACHE op.
  345. */
  346. if (immed)
  347. target_complete_cmd(cmd, SAM_STAT_GOOD);
  348. bio = bio_alloc(GFP_KERNEL, 0);
  349. bio->bi_end_io = iblock_end_io_flush;
  350. bio->bi_bdev = ib_dev->ibd_bd;
  351. if (!immed)
  352. bio->bi_private = cmd;
  353. submit_bio(WRITE_FLUSH, bio);
  354. return 0;
  355. }
  356. static sense_reason_t
  357. iblock_do_unmap(struct se_cmd *cmd, void *priv,
  358. sector_t lba, sector_t nolb)
  359. {
  360. struct block_device *bdev = priv;
  361. int ret;
  362. ret = blkdev_issue_discard(bdev, lba, nolb, GFP_KERNEL, 0);
  363. if (ret < 0) {
  364. pr_err("blkdev_issue_discard() failed: %d\n", ret);
  365. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  366. }
  367. return 0;
  368. }
  369. static sense_reason_t
  370. iblock_execute_unmap(struct se_cmd *cmd)
  371. {
  372. struct block_device *bdev = IBLOCK_DEV(cmd->se_dev)->ibd_bd;
  373. return sbc_execute_unmap(cmd, iblock_do_unmap, bdev);
  374. }
  375. static sense_reason_t
  376. iblock_execute_write_same_unmap(struct se_cmd *cmd)
  377. {
  378. struct block_device *bdev = IBLOCK_DEV(cmd->se_dev)->ibd_bd;
  379. sector_t lba = cmd->t_task_lba;
  380. sector_t nolb = sbc_get_write_same_sectors(cmd);
  381. int ret;
  382. ret = iblock_do_unmap(cmd, bdev, lba, nolb);
  383. if (ret)
  384. return ret;
  385. target_complete_cmd(cmd, GOOD);
  386. return 0;
  387. }
  388. static sense_reason_t
  389. iblock_execute_write_same(struct se_cmd *cmd)
  390. {
  391. struct iblock_req *ibr;
  392. struct scatterlist *sg;
  393. struct bio *bio;
  394. struct bio_list list;
  395. sector_t block_lba = cmd->t_task_lba;
  396. sector_t sectors = sbc_get_write_same_sectors(cmd);
  397. sg = &cmd->t_data_sg[0];
  398. if (cmd->t_data_nents > 1 ||
  399. sg->length != cmd->se_dev->dev_attrib.block_size) {
  400. pr_err("WRITE_SAME: Illegal SGL t_data_nents: %u length: %u"
  401. " block_size: %u\n", cmd->t_data_nents, sg->length,
  402. cmd->se_dev->dev_attrib.block_size);
  403. return TCM_INVALID_CDB_FIELD;
  404. }
  405. ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
  406. if (!ibr)
  407. goto fail;
  408. cmd->priv = ibr;
  409. bio = iblock_get_bio(cmd, block_lba, 1);
  410. if (!bio)
  411. goto fail_free_ibr;
  412. bio_list_init(&list);
  413. bio_list_add(&list, bio);
  414. atomic_set(&ibr->pending, 1);
  415. while (sectors) {
  416. while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
  417. != sg->length) {
  418. bio = iblock_get_bio(cmd, block_lba, 1);
  419. if (!bio)
  420. goto fail_put_bios;
  421. atomic_inc(&ibr->pending);
  422. bio_list_add(&list, bio);
  423. }
  424. /* Always in 512 byte units for Linux/Block */
  425. block_lba += sg->length >> IBLOCK_LBA_SHIFT;
  426. sectors -= 1;
  427. }
  428. iblock_submit_bios(&list, WRITE);
  429. return 0;
  430. fail_put_bios:
  431. while ((bio = bio_list_pop(&list)))
  432. bio_put(bio);
  433. fail_free_ibr:
  434. kfree(ibr);
  435. fail:
  436. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  437. }
  438. enum {
  439. Opt_udev_path, Opt_readonly, Opt_force, Opt_err
  440. };
  441. static match_table_t tokens = {
  442. {Opt_udev_path, "udev_path=%s"},
  443. {Opt_readonly, "readonly=%d"},
  444. {Opt_force, "force=%d"},
  445. {Opt_err, NULL}
  446. };
  447. static ssize_t iblock_set_configfs_dev_params(struct se_device *dev,
  448. const char *page, ssize_t count)
  449. {
  450. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  451. char *orig, *ptr, *arg_p, *opts;
  452. substring_t args[MAX_OPT_ARGS];
  453. int ret = 0, token;
  454. unsigned long tmp_readonly;
  455. opts = kstrdup(page, GFP_KERNEL);
  456. if (!opts)
  457. return -ENOMEM;
  458. orig = opts;
  459. while ((ptr = strsep(&opts, ",\n")) != NULL) {
  460. if (!*ptr)
  461. continue;
  462. token = match_token(ptr, tokens, args);
  463. switch (token) {
  464. case Opt_udev_path:
  465. if (ib_dev->ibd_bd) {
  466. pr_err("Unable to set udev_path= while"
  467. " ib_dev->ibd_bd exists\n");
  468. ret = -EEXIST;
  469. goto out;
  470. }
  471. if (match_strlcpy(ib_dev->ibd_udev_path, &args[0],
  472. SE_UDEV_PATH_LEN) == 0) {
  473. ret = -EINVAL;
  474. break;
  475. }
  476. pr_debug("IBLOCK: Referencing UDEV path: %s\n",
  477. ib_dev->ibd_udev_path);
  478. ib_dev->ibd_flags |= IBDF_HAS_UDEV_PATH;
  479. break;
  480. case Opt_readonly:
  481. arg_p = match_strdup(&args[0]);
  482. if (!arg_p) {
  483. ret = -ENOMEM;
  484. break;
  485. }
  486. ret = kstrtoul(arg_p, 0, &tmp_readonly);
  487. kfree(arg_p);
  488. if (ret < 0) {
  489. pr_err("kstrtoul() failed for"
  490. " readonly=\n");
  491. goto out;
  492. }
  493. ib_dev->ibd_readonly = tmp_readonly;
  494. pr_debug("IBLOCK: readonly: %d\n", ib_dev->ibd_readonly);
  495. break;
  496. case Opt_force:
  497. break;
  498. default:
  499. break;
  500. }
  501. }
  502. out:
  503. kfree(orig);
  504. return (!ret) ? count : ret;
  505. }
  506. static ssize_t iblock_show_configfs_dev_params(struct se_device *dev, char *b)
  507. {
  508. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  509. struct block_device *bd = ib_dev->ibd_bd;
  510. char buf[BDEVNAME_SIZE];
  511. ssize_t bl = 0;
  512. if (bd)
  513. bl += sprintf(b + bl, "iBlock device: %s",
  514. bdevname(bd, buf));
  515. if (ib_dev->ibd_flags & IBDF_HAS_UDEV_PATH)
  516. bl += sprintf(b + bl, " UDEV PATH: %s",
  517. ib_dev->ibd_udev_path);
  518. bl += sprintf(b + bl, " readonly: %d\n", ib_dev->ibd_readonly);
  519. bl += sprintf(b + bl, " ");
  520. if (bd) {
  521. bl += sprintf(b + bl, "Major: %d Minor: %d %s\n",
  522. MAJOR(bd->bd_dev), MINOR(bd->bd_dev), (!bd->bd_contains) ?
  523. "" : (bd->bd_holder == ib_dev) ?
  524. "CLAIMED: IBLOCK" : "CLAIMED: OS");
  525. } else {
  526. bl += sprintf(b + bl, "Major: 0 Minor: 0\n");
  527. }
  528. return bl;
  529. }
  530. static int
  531. iblock_alloc_bip(struct se_cmd *cmd, struct bio *bio)
  532. {
  533. struct se_device *dev = cmd->se_dev;
  534. struct blk_integrity *bi;
  535. struct bio_integrity_payload *bip;
  536. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  537. struct scatterlist *sg;
  538. int i, rc;
  539. bi = bdev_get_integrity(ib_dev->ibd_bd);
  540. if (!bi) {
  541. pr_err("Unable to locate bio_integrity\n");
  542. return -ENODEV;
  543. }
  544. bip = bio_integrity_alloc(bio, GFP_NOIO, cmd->t_prot_nents);
  545. if (!bip) {
  546. pr_err("Unable to allocate bio_integrity_payload\n");
  547. return -ENOMEM;
  548. }
  549. bip->bip_iter.bi_size = (cmd->data_length / dev->dev_attrib.block_size) *
  550. dev->prot_length;
  551. bip->bip_iter.bi_sector = bio->bi_iter.bi_sector;
  552. pr_debug("IBLOCK BIP Size: %u Sector: %llu\n", bip->bip_iter.bi_size,
  553. (unsigned long long)bip->bip_iter.bi_sector);
  554. for_each_sg(cmd->t_prot_sg, sg, cmd->t_prot_nents, i) {
  555. rc = bio_integrity_add_page(bio, sg_page(sg), sg->length,
  556. sg->offset);
  557. if (rc != sg->length) {
  558. pr_err("bio_integrity_add_page() failed; %d\n", rc);
  559. return -ENOMEM;
  560. }
  561. pr_debug("Added bio integrity page: %p length: %d offset; %d\n",
  562. sg_page(sg), sg->length, sg->offset);
  563. }
  564. return 0;
  565. }
  566. static sense_reason_t
  567. iblock_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
  568. enum dma_data_direction data_direction)
  569. {
  570. struct se_device *dev = cmd->se_dev;
  571. struct iblock_req *ibr;
  572. struct bio *bio, *bio_start;
  573. struct bio_list list;
  574. struct scatterlist *sg;
  575. u32 sg_num = sgl_nents;
  576. sector_t block_lba;
  577. unsigned bio_cnt;
  578. int rw = 0;
  579. int i;
  580. if (data_direction == DMA_TO_DEVICE) {
  581. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  582. struct request_queue *q = bdev_get_queue(ib_dev->ibd_bd);
  583. /*
  584. * Force writethrough using WRITE_FUA if a volatile write cache
  585. * is not enabled, or if initiator set the Force Unit Access bit.
  586. */
  587. if (q->flush_flags & REQ_FUA) {
  588. if (cmd->se_cmd_flags & SCF_FUA)
  589. rw = WRITE_FUA;
  590. else if (!(q->flush_flags & REQ_FLUSH))
  591. rw = WRITE_FUA;
  592. else
  593. rw = WRITE;
  594. } else {
  595. rw = WRITE;
  596. }
  597. } else {
  598. rw = READ;
  599. }
  600. /*
  601. * Convert the blocksize advertised to the initiator to the 512 byte
  602. * units unconditionally used by the Linux block layer.
  603. */
  604. if (dev->dev_attrib.block_size == 4096)
  605. block_lba = (cmd->t_task_lba << 3);
  606. else if (dev->dev_attrib.block_size == 2048)
  607. block_lba = (cmd->t_task_lba << 2);
  608. else if (dev->dev_attrib.block_size == 1024)
  609. block_lba = (cmd->t_task_lba << 1);
  610. else if (dev->dev_attrib.block_size == 512)
  611. block_lba = cmd->t_task_lba;
  612. else {
  613. pr_err("Unsupported SCSI -> BLOCK LBA conversion:"
  614. " %u\n", dev->dev_attrib.block_size);
  615. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  616. }
  617. ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
  618. if (!ibr)
  619. goto fail;
  620. cmd->priv = ibr;
  621. if (!sgl_nents) {
  622. atomic_set(&ibr->pending, 1);
  623. iblock_complete_cmd(cmd);
  624. return 0;
  625. }
  626. bio = iblock_get_bio(cmd, block_lba, sgl_nents);
  627. if (!bio)
  628. goto fail_free_ibr;
  629. bio_start = bio;
  630. bio_list_init(&list);
  631. bio_list_add(&list, bio);
  632. atomic_set(&ibr->pending, 2);
  633. bio_cnt = 1;
  634. for_each_sg(sgl, sg, sgl_nents, i) {
  635. /*
  636. * XXX: if the length the device accepts is shorter than the
  637. * length of the S/G list entry this will cause and
  638. * endless loop. Better hope no driver uses huge pages.
  639. */
  640. while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
  641. != sg->length) {
  642. if (bio_cnt >= IBLOCK_MAX_BIO_PER_TASK) {
  643. iblock_submit_bios(&list, rw);
  644. bio_cnt = 0;
  645. }
  646. bio = iblock_get_bio(cmd, block_lba, sg_num);
  647. if (!bio)
  648. goto fail_put_bios;
  649. atomic_inc(&ibr->pending);
  650. bio_list_add(&list, bio);
  651. bio_cnt++;
  652. }
  653. /* Always in 512 byte units for Linux/Block */
  654. block_lba += sg->length >> IBLOCK_LBA_SHIFT;
  655. sg_num--;
  656. }
  657. if (cmd->prot_type) {
  658. int rc = iblock_alloc_bip(cmd, bio_start);
  659. if (rc)
  660. goto fail_put_bios;
  661. }
  662. iblock_submit_bios(&list, rw);
  663. iblock_complete_cmd(cmd);
  664. return 0;
  665. fail_put_bios:
  666. while ((bio = bio_list_pop(&list)))
  667. bio_put(bio);
  668. fail_free_ibr:
  669. kfree(ibr);
  670. fail:
  671. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  672. }
  673. static sector_t iblock_get_blocks(struct se_device *dev)
  674. {
  675. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  676. struct block_device *bd = ib_dev->ibd_bd;
  677. struct request_queue *q = bdev_get_queue(bd);
  678. return iblock_emulate_read_cap_with_block_size(dev, bd, q);
  679. }
  680. static sector_t iblock_get_alignment_offset_lbas(struct se_device *dev)
  681. {
  682. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  683. struct block_device *bd = ib_dev->ibd_bd;
  684. int ret;
  685. ret = bdev_alignment_offset(bd);
  686. if (ret == -1)
  687. return 0;
  688. /* convert offset-bytes to offset-lbas */
  689. return ret / bdev_logical_block_size(bd);
  690. }
  691. static unsigned int iblock_get_lbppbe(struct se_device *dev)
  692. {
  693. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  694. struct block_device *bd = ib_dev->ibd_bd;
  695. int logs_per_phys = bdev_physical_block_size(bd) / bdev_logical_block_size(bd);
  696. return ilog2(logs_per_phys);
  697. }
  698. static unsigned int iblock_get_io_min(struct se_device *dev)
  699. {
  700. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  701. struct block_device *bd = ib_dev->ibd_bd;
  702. return bdev_io_min(bd);
  703. }
  704. static unsigned int iblock_get_io_opt(struct se_device *dev)
  705. {
  706. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  707. struct block_device *bd = ib_dev->ibd_bd;
  708. return bdev_io_opt(bd);
  709. }
  710. static struct sbc_ops iblock_sbc_ops = {
  711. .execute_rw = iblock_execute_rw,
  712. .execute_sync_cache = iblock_execute_sync_cache,
  713. .execute_write_same = iblock_execute_write_same,
  714. .execute_write_same_unmap = iblock_execute_write_same_unmap,
  715. .execute_unmap = iblock_execute_unmap,
  716. };
  717. static sense_reason_t
  718. iblock_parse_cdb(struct se_cmd *cmd)
  719. {
  720. return sbc_parse_cdb(cmd, &iblock_sbc_ops);
  721. }
  722. static bool iblock_get_write_cache(struct se_device *dev)
  723. {
  724. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  725. struct block_device *bd = ib_dev->ibd_bd;
  726. struct request_queue *q = bdev_get_queue(bd);
  727. return q->flush_flags & REQ_FLUSH;
  728. }
  729. static struct se_subsystem_api iblock_template = {
  730. .name = "iblock",
  731. .inquiry_prod = "IBLOCK",
  732. .inquiry_rev = IBLOCK_VERSION,
  733. .owner = THIS_MODULE,
  734. .transport_type = TRANSPORT_PLUGIN_VHBA_PDEV,
  735. .attach_hba = iblock_attach_hba,
  736. .detach_hba = iblock_detach_hba,
  737. .alloc_device = iblock_alloc_device,
  738. .configure_device = iblock_configure_device,
  739. .free_device = iblock_free_device,
  740. .parse_cdb = iblock_parse_cdb,
  741. .set_configfs_dev_params = iblock_set_configfs_dev_params,
  742. .show_configfs_dev_params = iblock_show_configfs_dev_params,
  743. .get_device_type = sbc_get_device_type,
  744. .get_blocks = iblock_get_blocks,
  745. .get_alignment_offset_lbas = iblock_get_alignment_offset_lbas,
  746. .get_lbppbe = iblock_get_lbppbe,
  747. .get_io_min = iblock_get_io_min,
  748. .get_io_opt = iblock_get_io_opt,
  749. .get_write_cache = iblock_get_write_cache,
  750. };
  751. static int __init iblock_module_init(void)
  752. {
  753. return transport_subsystem_register(&iblock_template);
  754. }
  755. static void __exit iblock_module_exit(void)
  756. {
  757. transport_subsystem_release(&iblock_template);
  758. }
  759. MODULE_DESCRIPTION("TCM IBLOCK subsystem plugin");
  760. MODULE_AUTHOR("nab@Linux-iSCSI.org");
  761. MODULE_LICENSE("GPL");
  762. module_init(iblock_module_init);
  763. module_exit(iblock_module_exit);