target_core_iblock.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*******************************************************************************
  3. * Filename: target_core_iblock.c
  4. *
  5. * This file contains the Storage Engine <-> Linux BlockIO transport
  6. * specific functions.
  7. *
  8. * (c) Copyright 2003-2013 Datera, Inc.
  9. *
  10. * Nicholas A. Bellinger <nab@kernel.org>
  11. *
  12. ******************************************************************************/
  13. #include <linux/string.h>
  14. #include <linux/parser.h>
  15. #include <linux/timer.h>
  16. #include <linux/fs.h>
  17. #include <linux/blkdev.h>
  18. #include <linux/slab.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/bio.h>
  21. #include <linux/genhd.h>
  22. #include <linux/file.h>
  23. #include <linux/module.h>
  24. #include <scsi/scsi_proto.h>
  25. #include <asm/unaligned.h>
  26. #include <target/target_core_base.h>
  27. #include <target/target_core_backend.h>
  28. #include "target_core_iblock.h"
  29. #define IBLOCK_MAX_BIO_PER_TASK 32 /* max # of bios to submit at a time */
  30. #define IBLOCK_BIO_POOL_SIZE 128
  31. static inline struct iblock_dev *IBLOCK_DEV(struct se_device *dev)
  32. {
  33. return container_of(dev, struct iblock_dev, dev);
  34. }
  35. static int iblock_attach_hba(struct se_hba *hba, u32 host_id)
  36. {
  37. pr_debug("CORE_HBA[%d] - TCM iBlock HBA Driver %s on"
  38. " Generic Target Core Stack %s\n", hba->hba_id,
  39. IBLOCK_VERSION, TARGET_CORE_VERSION);
  40. return 0;
  41. }
  42. static void iblock_detach_hba(struct se_hba *hba)
  43. {
  44. }
  45. static struct se_device *iblock_alloc_device(struct se_hba *hba, const char *name)
  46. {
  47. struct iblock_dev *ib_dev = NULL;
  48. ib_dev = kzalloc(sizeof(struct iblock_dev), GFP_KERNEL);
  49. if (!ib_dev) {
  50. pr_err("Unable to allocate struct iblock_dev\n");
  51. return NULL;
  52. }
  53. pr_debug( "IBLOCK: Allocated ib_dev for %s\n", name);
  54. return &ib_dev->dev;
  55. }
  56. static int iblock_configure_device(struct se_device *dev)
  57. {
  58. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  59. struct request_queue *q;
  60. struct block_device *bd = NULL;
  61. struct blk_integrity *bi;
  62. fmode_t mode;
  63. unsigned int max_write_zeroes_sectors;
  64. int ret = -ENOMEM;
  65. if (!(ib_dev->ibd_flags & IBDF_HAS_UDEV_PATH)) {
  66. pr_err("Missing udev_path= parameters for IBLOCK\n");
  67. return -EINVAL;
  68. }
  69. ret = bioset_init(&ib_dev->ibd_bio_set, IBLOCK_BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
  70. if (ret) {
  71. pr_err("IBLOCK: Unable to create bioset\n");
  72. goto out;
  73. }
  74. pr_debug( "IBLOCK: Claiming struct block_device: %s\n",
  75. ib_dev->ibd_udev_path);
  76. mode = FMODE_READ|FMODE_EXCL;
  77. if (!ib_dev->ibd_readonly)
  78. mode |= FMODE_WRITE;
  79. else
  80. dev->dev_flags |= DF_READ_ONLY;
  81. bd = blkdev_get_by_path(ib_dev->ibd_udev_path, mode, ib_dev);
  82. if (IS_ERR(bd)) {
  83. ret = PTR_ERR(bd);
  84. goto out_free_bioset;
  85. }
  86. ib_dev->ibd_bd = bd;
  87. q = bdev_get_queue(bd);
  88. dev->dev_attrib.hw_block_size = bdev_logical_block_size(bd);
  89. dev->dev_attrib.hw_max_sectors = queue_max_hw_sectors(q);
  90. dev->dev_attrib.hw_queue_depth = q->nr_requests;
  91. if (target_configure_unmap_from_queue(&dev->dev_attrib, q))
  92. pr_debug("IBLOCK: BLOCK Discard support available,"
  93. " disabled by default\n");
  94. /*
  95. * Enable write same emulation for IBLOCK and use 0xFFFF as
  96. * the smaller WRITE_SAME(10) only has a two-byte block count.
  97. */
  98. max_write_zeroes_sectors = bdev_write_zeroes_sectors(bd);
  99. if (max_write_zeroes_sectors)
  100. dev->dev_attrib.max_write_same_len = max_write_zeroes_sectors;
  101. else
  102. dev->dev_attrib.max_write_same_len = 0xFFFF;
  103. if (blk_queue_nonrot(q))
  104. dev->dev_attrib.is_nonrot = 1;
  105. bi = bdev_get_integrity(bd);
  106. if (bi) {
  107. struct bio_set *bs = &ib_dev->ibd_bio_set;
  108. if (!strcmp(bi->profile->name, "T10-DIF-TYPE3-IP") ||
  109. !strcmp(bi->profile->name, "T10-DIF-TYPE1-IP")) {
  110. pr_err("IBLOCK export of blk_integrity: %s not"
  111. " supported\n", bi->profile->name);
  112. ret = -ENOSYS;
  113. goto out_blkdev_put;
  114. }
  115. if (!strcmp(bi->profile->name, "T10-DIF-TYPE3-CRC")) {
  116. dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE3_PROT;
  117. } else if (!strcmp(bi->profile->name, "T10-DIF-TYPE1-CRC")) {
  118. dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE1_PROT;
  119. }
  120. if (dev->dev_attrib.pi_prot_type) {
  121. if (bioset_integrity_create(bs, IBLOCK_BIO_POOL_SIZE) < 0) {
  122. pr_err("Unable to allocate bioset for PI\n");
  123. ret = -ENOMEM;
  124. goto out_blkdev_put;
  125. }
  126. pr_debug("IBLOCK setup BIP bs->bio_integrity_pool: %p\n",
  127. &bs->bio_integrity_pool);
  128. }
  129. dev->dev_attrib.hw_pi_prot_type = dev->dev_attrib.pi_prot_type;
  130. }
  131. return 0;
  132. out_blkdev_put:
  133. blkdev_put(ib_dev->ibd_bd, FMODE_WRITE|FMODE_READ|FMODE_EXCL);
  134. out_free_bioset:
  135. bioset_exit(&ib_dev->ibd_bio_set);
  136. out:
  137. return ret;
  138. }
  139. static void iblock_dev_call_rcu(struct rcu_head *p)
  140. {
  141. struct se_device *dev = container_of(p, struct se_device, rcu_head);
  142. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  143. kfree(ib_dev);
  144. }
  145. static void iblock_free_device(struct se_device *dev)
  146. {
  147. call_rcu(&dev->rcu_head, iblock_dev_call_rcu);
  148. }
  149. static void iblock_destroy_device(struct se_device *dev)
  150. {
  151. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  152. if (ib_dev->ibd_bd != NULL)
  153. blkdev_put(ib_dev->ibd_bd, FMODE_WRITE|FMODE_READ|FMODE_EXCL);
  154. bioset_exit(&ib_dev->ibd_bio_set);
  155. }
  156. static unsigned long long iblock_emulate_read_cap_with_block_size(
  157. struct se_device *dev,
  158. struct block_device *bd,
  159. struct request_queue *q)
  160. {
  161. unsigned long long blocks_long = (div_u64(i_size_read(bd->bd_inode),
  162. bdev_logical_block_size(bd)) - 1);
  163. u32 block_size = bdev_logical_block_size(bd);
  164. if (block_size == dev->dev_attrib.block_size)
  165. return blocks_long;
  166. switch (block_size) {
  167. case 4096:
  168. switch (dev->dev_attrib.block_size) {
  169. case 2048:
  170. blocks_long <<= 1;
  171. break;
  172. case 1024:
  173. blocks_long <<= 2;
  174. break;
  175. case 512:
  176. blocks_long <<= 3;
  177. default:
  178. break;
  179. }
  180. break;
  181. case 2048:
  182. switch (dev->dev_attrib.block_size) {
  183. case 4096:
  184. blocks_long >>= 1;
  185. break;
  186. case 1024:
  187. blocks_long <<= 1;
  188. break;
  189. case 512:
  190. blocks_long <<= 2;
  191. break;
  192. default:
  193. break;
  194. }
  195. break;
  196. case 1024:
  197. switch (dev->dev_attrib.block_size) {
  198. case 4096:
  199. blocks_long >>= 2;
  200. break;
  201. case 2048:
  202. blocks_long >>= 1;
  203. break;
  204. case 512:
  205. blocks_long <<= 1;
  206. break;
  207. default:
  208. break;
  209. }
  210. break;
  211. case 512:
  212. switch (dev->dev_attrib.block_size) {
  213. case 4096:
  214. blocks_long >>= 3;
  215. break;
  216. case 2048:
  217. blocks_long >>= 2;
  218. break;
  219. case 1024:
  220. blocks_long >>= 1;
  221. break;
  222. default:
  223. break;
  224. }
  225. break;
  226. default:
  227. break;
  228. }
  229. return blocks_long;
  230. }
  231. static void iblock_complete_cmd(struct se_cmd *cmd)
  232. {
  233. struct iblock_req *ibr = cmd->priv;
  234. u8 status;
  235. if (!refcount_dec_and_test(&ibr->pending))
  236. return;
  237. if (atomic_read(&ibr->ib_bio_err_cnt))
  238. status = SAM_STAT_CHECK_CONDITION;
  239. else
  240. status = SAM_STAT_GOOD;
  241. target_complete_cmd(cmd, status);
  242. kfree(ibr);
  243. }
  244. static void iblock_bio_done(struct bio *bio)
  245. {
  246. struct se_cmd *cmd = bio->bi_private;
  247. struct iblock_req *ibr = cmd->priv;
  248. if (bio->bi_status) {
  249. pr_err("bio error: %p, err: %d\n", bio, bio->bi_status);
  250. /*
  251. * Bump the ib_bio_err_cnt and release bio.
  252. */
  253. atomic_inc(&ibr->ib_bio_err_cnt);
  254. smp_mb__after_atomic();
  255. }
  256. bio_put(bio);
  257. iblock_complete_cmd(cmd);
  258. }
  259. static struct bio *
  260. iblock_get_bio(struct se_cmd *cmd, sector_t lba, u32 sg_num, int op,
  261. int op_flags)
  262. {
  263. struct iblock_dev *ib_dev = IBLOCK_DEV(cmd->se_dev);
  264. struct bio *bio;
  265. /*
  266. * Only allocate as many vector entries as the bio code allows us to,
  267. * we'll loop later on until we have handled the whole request.
  268. */
  269. if (sg_num > BIO_MAX_PAGES)
  270. sg_num = BIO_MAX_PAGES;
  271. bio = bio_alloc_bioset(GFP_NOIO, sg_num, &ib_dev->ibd_bio_set);
  272. if (!bio) {
  273. pr_err("Unable to allocate memory for bio\n");
  274. return NULL;
  275. }
  276. bio_set_dev(bio, ib_dev->ibd_bd);
  277. bio->bi_private = cmd;
  278. bio->bi_end_io = &iblock_bio_done;
  279. bio->bi_iter.bi_sector = lba;
  280. bio_set_op_attrs(bio, op, op_flags);
  281. return bio;
  282. }
  283. static void iblock_submit_bios(struct bio_list *list)
  284. {
  285. struct blk_plug plug;
  286. struct bio *bio;
  287. blk_start_plug(&plug);
  288. while ((bio = bio_list_pop(list)))
  289. submit_bio(bio);
  290. blk_finish_plug(&plug);
  291. }
  292. static void iblock_end_io_flush(struct bio *bio)
  293. {
  294. struct se_cmd *cmd = bio->bi_private;
  295. if (bio->bi_status)
  296. pr_err("IBLOCK: cache flush failed: %d\n", bio->bi_status);
  297. if (cmd) {
  298. if (bio->bi_status)
  299. target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
  300. else
  301. target_complete_cmd(cmd, SAM_STAT_GOOD);
  302. }
  303. bio_put(bio);
  304. }
  305. /*
  306. * Implement SYCHRONIZE CACHE. Note that we can't handle lba ranges and must
  307. * always flush the whole cache.
  308. */
  309. static sense_reason_t
  310. iblock_execute_sync_cache(struct se_cmd *cmd)
  311. {
  312. struct iblock_dev *ib_dev = IBLOCK_DEV(cmd->se_dev);
  313. int immed = (cmd->t_task_cdb[1] & 0x2);
  314. struct bio *bio;
  315. /*
  316. * If the Immediate bit is set, queue up the GOOD response
  317. * for this SYNCHRONIZE_CACHE op.
  318. */
  319. if (immed)
  320. target_complete_cmd(cmd, SAM_STAT_GOOD);
  321. bio = bio_alloc(GFP_KERNEL, 0);
  322. bio->bi_end_io = iblock_end_io_flush;
  323. bio_set_dev(bio, ib_dev->ibd_bd);
  324. bio->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
  325. if (!immed)
  326. bio->bi_private = cmd;
  327. submit_bio(bio);
  328. return 0;
  329. }
  330. static sense_reason_t
  331. iblock_execute_unmap(struct se_cmd *cmd, sector_t lba, sector_t nolb)
  332. {
  333. struct block_device *bdev = IBLOCK_DEV(cmd->se_dev)->ibd_bd;
  334. struct se_device *dev = cmd->se_dev;
  335. int ret;
  336. ret = blkdev_issue_discard(bdev,
  337. target_to_linux_sector(dev, lba),
  338. target_to_linux_sector(dev, nolb),
  339. GFP_KERNEL, 0);
  340. if (ret < 0) {
  341. pr_err("blkdev_issue_discard() failed: %d\n", ret);
  342. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  343. }
  344. return 0;
  345. }
  346. static sense_reason_t
  347. iblock_execute_zero_out(struct block_device *bdev, struct se_cmd *cmd)
  348. {
  349. struct se_device *dev = cmd->se_dev;
  350. struct scatterlist *sg = &cmd->t_data_sg[0];
  351. unsigned char *buf, *not_zero;
  352. int ret;
  353. buf = kmap(sg_page(sg)) + sg->offset;
  354. if (!buf)
  355. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  356. /*
  357. * Fall back to block_execute_write_same() slow-path if
  358. * incoming WRITE_SAME payload does not contain zeros.
  359. */
  360. not_zero = memchr_inv(buf, 0x00, cmd->data_length);
  361. kunmap(sg_page(sg));
  362. if (not_zero)
  363. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  364. ret = blkdev_issue_zeroout(bdev,
  365. target_to_linux_sector(dev, cmd->t_task_lba),
  366. target_to_linux_sector(dev,
  367. sbc_get_write_same_sectors(cmd)),
  368. GFP_KERNEL, BLKDEV_ZERO_NOUNMAP);
  369. if (ret)
  370. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  371. target_complete_cmd(cmd, GOOD);
  372. return 0;
  373. }
  374. static sense_reason_t
  375. iblock_execute_write_same(struct se_cmd *cmd)
  376. {
  377. struct block_device *bdev = IBLOCK_DEV(cmd->se_dev)->ibd_bd;
  378. struct iblock_req *ibr;
  379. struct scatterlist *sg;
  380. struct bio *bio;
  381. struct bio_list list;
  382. struct se_device *dev = cmd->se_dev;
  383. sector_t block_lba = target_to_linux_sector(dev, cmd->t_task_lba);
  384. sector_t sectors = target_to_linux_sector(dev,
  385. sbc_get_write_same_sectors(cmd));
  386. if (cmd->prot_op) {
  387. pr_err("WRITE_SAME: Protection information with IBLOCK"
  388. " backends not supported\n");
  389. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  390. }
  391. sg = &cmd->t_data_sg[0];
  392. if (cmd->t_data_nents > 1 ||
  393. sg->length != cmd->se_dev->dev_attrib.block_size) {
  394. pr_err("WRITE_SAME: Illegal SGL t_data_nents: %u length: %u"
  395. " block_size: %u\n", cmd->t_data_nents, sg->length,
  396. cmd->se_dev->dev_attrib.block_size);
  397. return TCM_INVALID_CDB_FIELD;
  398. }
  399. if (bdev_write_zeroes_sectors(bdev)) {
  400. if (!iblock_execute_zero_out(bdev, cmd))
  401. return 0;
  402. }
  403. ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
  404. if (!ibr)
  405. goto fail;
  406. cmd->priv = ibr;
  407. bio = iblock_get_bio(cmd, block_lba, 1, REQ_OP_WRITE, 0);
  408. if (!bio)
  409. goto fail_free_ibr;
  410. bio_list_init(&list);
  411. bio_list_add(&list, bio);
  412. refcount_set(&ibr->pending, 1);
  413. while (sectors) {
  414. while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
  415. != sg->length) {
  416. bio = iblock_get_bio(cmd, block_lba, 1, REQ_OP_WRITE,
  417. 0);
  418. if (!bio)
  419. goto fail_put_bios;
  420. refcount_inc(&ibr->pending);
  421. bio_list_add(&list, bio);
  422. }
  423. /* Always in 512 byte units for Linux/Block */
  424. block_lba += sg->length >> SECTOR_SHIFT;
  425. sectors -= sg->length >> SECTOR_SHIFT;
  426. }
  427. iblock_submit_bios(&list);
  428. return 0;
  429. fail_put_bios:
  430. while ((bio = bio_list_pop(&list)))
  431. bio_put(bio);
  432. fail_free_ibr:
  433. kfree(ibr);
  434. fail:
  435. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  436. }
  437. enum {
  438. Opt_udev_path, Opt_readonly, Opt_force, Opt_err
  439. };
  440. static match_table_t tokens = {
  441. {Opt_udev_path, "udev_path=%s"},
  442. {Opt_readonly, "readonly=%d"},
  443. {Opt_force, "force=%d"},
  444. {Opt_err, NULL}
  445. };
  446. static ssize_t iblock_set_configfs_dev_params(struct se_device *dev,
  447. const char *page, ssize_t count)
  448. {
  449. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  450. char *orig, *ptr, *arg_p, *opts;
  451. substring_t args[MAX_OPT_ARGS];
  452. int ret = 0, token;
  453. unsigned long tmp_readonly;
  454. opts = kstrdup(page, GFP_KERNEL);
  455. if (!opts)
  456. return -ENOMEM;
  457. orig = opts;
  458. while ((ptr = strsep(&opts, ",\n")) != NULL) {
  459. if (!*ptr)
  460. continue;
  461. token = match_token(ptr, tokens, args);
  462. switch (token) {
  463. case Opt_udev_path:
  464. if (ib_dev->ibd_bd) {
  465. pr_err("Unable to set udev_path= while"
  466. " ib_dev->ibd_bd exists\n");
  467. ret = -EEXIST;
  468. goto out;
  469. }
  470. if (match_strlcpy(ib_dev->ibd_udev_path, &args[0],
  471. SE_UDEV_PATH_LEN) == 0) {
  472. ret = -EINVAL;
  473. break;
  474. }
  475. pr_debug("IBLOCK: Referencing UDEV path: %s\n",
  476. ib_dev->ibd_udev_path);
  477. ib_dev->ibd_flags |= IBDF_HAS_UDEV_PATH;
  478. break;
  479. case Opt_readonly:
  480. arg_p = match_strdup(&args[0]);
  481. if (!arg_p) {
  482. ret = -ENOMEM;
  483. break;
  484. }
  485. ret = kstrtoul(arg_p, 0, &tmp_readonly);
  486. kfree(arg_p);
  487. if (ret < 0) {
  488. pr_err("kstrtoul() failed for"
  489. " readonly=\n");
  490. goto out;
  491. }
  492. ib_dev->ibd_readonly = tmp_readonly;
  493. pr_debug("IBLOCK: readonly: %d\n", ib_dev->ibd_readonly);
  494. break;
  495. case Opt_force:
  496. break;
  497. default:
  498. break;
  499. }
  500. }
  501. out:
  502. kfree(orig);
  503. return (!ret) ? count : ret;
  504. }
  505. static ssize_t iblock_show_configfs_dev_params(struct se_device *dev, char *b)
  506. {
  507. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  508. struct block_device *bd = ib_dev->ibd_bd;
  509. char buf[BDEVNAME_SIZE];
  510. ssize_t bl = 0;
  511. if (bd)
  512. bl += sprintf(b + bl, "iBlock device: %s",
  513. bdevname(bd, buf));
  514. if (ib_dev->ibd_flags & IBDF_HAS_UDEV_PATH)
  515. bl += sprintf(b + bl, " UDEV PATH: %s",
  516. ib_dev->ibd_udev_path);
  517. bl += sprintf(b + bl, " readonly: %d\n", ib_dev->ibd_readonly);
  518. bl += sprintf(b + bl, " ");
  519. if (bd) {
  520. bl += sprintf(b + bl, "Major: %d Minor: %d %s\n",
  521. MAJOR(bd->bd_dev), MINOR(bd->bd_dev),
  522. "CLAIMED: IBLOCK");
  523. } else {
  524. bl += sprintf(b + bl, "Major: 0 Minor: 0\n");
  525. }
  526. return bl;
  527. }
  528. static int
  529. iblock_alloc_bip(struct se_cmd *cmd, struct bio *bio,
  530. struct sg_mapping_iter *miter)
  531. {
  532. struct se_device *dev = cmd->se_dev;
  533. struct blk_integrity *bi;
  534. struct bio_integrity_payload *bip;
  535. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  536. int rc;
  537. size_t resid, len;
  538. bi = bdev_get_integrity(ib_dev->ibd_bd);
  539. if (!bi) {
  540. pr_err("Unable to locate bio_integrity\n");
  541. return -ENODEV;
  542. }
  543. bip = bio_integrity_alloc(bio, GFP_NOIO,
  544. min_t(unsigned int, cmd->t_prot_nents, BIO_MAX_PAGES));
  545. if (IS_ERR(bip)) {
  546. pr_err("Unable to allocate bio_integrity_payload\n");
  547. return PTR_ERR(bip);
  548. }
  549. bip->bip_iter.bi_size = bio_integrity_bytes(bi, bio_sectors(bio));
  550. /* virtual start sector must be in integrity interval units */
  551. bip_set_seed(bip, bio->bi_iter.bi_sector >>
  552. (bi->interval_exp - SECTOR_SHIFT));
  553. pr_debug("IBLOCK BIP Size: %u Sector: %llu\n", bip->bip_iter.bi_size,
  554. (unsigned long long)bip->bip_iter.bi_sector);
  555. resid = bip->bip_iter.bi_size;
  556. while (resid > 0 && sg_miter_next(miter)) {
  557. len = min_t(size_t, miter->length, resid);
  558. rc = bio_integrity_add_page(bio, miter->page, len,
  559. offset_in_page(miter->addr));
  560. if (rc != len) {
  561. pr_err("bio_integrity_add_page() failed; %d\n", rc);
  562. sg_miter_stop(miter);
  563. return -ENOMEM;
  564. }
  565. pr_debug("Added bio integrity page: %p length: %zu offset: %lu\n",
  566. miter->page, len, offset_in_page(miter->addr));
  567. resid -= len;
  568. if (len < miter->length)
  569. miter->consumed -= miter->length - len;
  570. }
  571. sg_miter_stop(miter);
  572. return 0;
  573. }
  574. static sense_reason_t
  575. iblock_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
  576. enum dma_data_direction data_direction)
  577. {
  578. struct se_device *dev = cmd->se_dev;
  579. sector_t block_lba = target_to_linux_sector(dev, cmd->t_task_lba);
  580. struct iblock_req *ibr;
  581. struct bio *bio;
  582. struct bio_list list;
  583. struct scatterlist *sg;
  584. u32 sg_num = sgl_nents;
  585. unsigned bio_cnt;
  586. int i, rc, op, op_flags = 0;
  587. struct sg_mapping_iter prot_miter;
  588. if (data_direction == DMA_TO_DEVICE) {
  589. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  590. struct request_queue *q = bdev_get_queue(ib_dev->ibd_bd);
  591. /*
  592. * Force writethrough using REQ_FUA if a volatile write cache
  593. * is not enabled, or if initiator set the Force Unit Access bit.
  594. */
  595. op = REQ_OP_WRITE;
  596. if (test_bit(QUEUE_FLAG_FUA, &q->queue_flags)) {
  597. if (cmd->se_cmd_flags & SCF_FUA)
  598. op_flags = REQ_FUA;
  599. else if (!test_bit(QUEUE_FLAG_WC, &q->queue_flags))
  600. op_flags = REQ_FUA;
  601. }
  602. } else {
  603. op = REQ_OP_READ;
  604. }
  605. ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
  606. if (!ibr)
  607. goto fail;
  608. cmd->priv = ibr;
  609. if (!sgl_nents) {
  610. refcount_set(&ibr->pending, 1);
  611. iblock_complete_cmd(cmd);
  612. return 0;
  613. }
  614. bio = iblock_get_bio(cmd, block_lba, sgl_nents, op, op_flags);
  615. if (!bio)
  616. goto fail_free_ibr;
  617. bio_list_init(&list);
  618. bio_list_add(&list, bio);
  619. refcount_set(&ibr->pending, 2);
  620. bio_cnt = 1;
  621. if (cmd->prot_type && dev->dev_attrib.pi_prot_type)
  622. sg_miter_start(&prot_miter, cmd->t_prot_sg, cmd->t_prot_nents,
  623. op == REQ_OP_READ ? SG_MITER_FROM_SG :
  624. SG_MITER_TO_SG);
  625. for_each_sg(sgl, sg, sgl_nents, i) {
  626. /*
  627. * XXX: if the length the device accepts is shorter than the
  628. * length of the S/G list entry this will cause and
  629. * endless loop. Better hope no driver uses huge pages.
  630. */
  631. while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
  632. != sg->length) {
  633. if (cmd->prot_type && dev->dev_attrib.pi_prot_type) {
  634. rc = iblock_alloc_bip(cmd, bio, &prot_miter);
  635. if (rc)
  636. goto fail_put_bios;
  637. }
  638. if (bio_cnt >= IBLOCK_MAX_BIO_PER_TASK) {
  639. iblock_submit_bios(&list);
  640. bio_cnt = 0;
  641. }
  642. bio = iblock_get_bio(cmd, block_lba, sg_num, op,
  643. op_flags);
  644. if (!bio)
  645. goto fail_put_bios;
  646. refcount_inc(&ibr->pending);
  647. bio_list_add(&list, bio);
  648. bio_cnt++;
  649. }
  650. /* Always in 512 byte units for Linux/Block */
  651. block_lba += sg->length >> SECTOR_SHIFT;
  652. sg_num--;
  653. }
  654. if (cmd->prot_type && dev->dev_attrib.pi_prot_type) {
  655. rc = iblock_alloc_bip(cmd, bio, &prot_miter);
  656. if (rc)
  657. goto fail_put_bios;
  658. }
  659. iblock_submit_bios(&list);
  660. iblock_complete_cmd(cmd);
  661. return 0;
  662. fail_put_bios:
  663. while ((bio = bio_list_pop(&list)))
  664. bio_put(bio);
  665. fail_free_ibr:
  666. kfree(ibr);
  667. fail:
  668. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  669. }
  670. static sector_t iblock_get_blocks(struct se_device *dev)
  671. {
  672. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  673. struct block_device *bd = ib_dev->ibd_bd;
  674. struct request_queue *q = bdev_get_queue(bd);
  675. return iblock_emulate_read_cap_with_block_size(dev, bd, q);
  676. }
  677. static sector_t iblock_get_alignment_offset_lbas(struct se_device *dev)
  678. {
  679. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  680. struct block_device *bd = ib_dev->ibd_bd;
  681. int ret;
  682. ret = bdev_alignment_offset(bd);
  683. if (ret == -1)
  684. return 0;
  685. /* convert offset-bytes to offset-lbas */
  686. return ret / bdev_logical_block_size(bd);
  687. }
  688. static unsigned int iblock_get_lbppbe(struct se_device *dev)
  689. {
  690. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  691. struct block_device *bd = ib_dev->ibd_bd;
  692. int logs_per_phys = bdev_physical_block_size(bd) / bdev_logical_block_size(bd);
  693. return ilog2(logs_per_phys);
  694. }
  695. static unsigned int iblock_get_io_min(struct se_device *dev)
  696. {
  697. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  698. struct block_device *bd = ib_dev->ibd_bd;
  699. return bdev_io_min(bd);
  700. }
  701. static unsigned int iblock_get_io_opt(struct se_device *dev)
  702. {
  703. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  704. struct block_device *bd = ib_dev->ibd_bd;
  705. return bdev_io_opt(bd);
  706. }
  707. static struct sbc_ops iblock_sbc_ops = {
  708. .execute_rw = iblock_execute_rw,
  709. .execute_sync_cache = iblock_execute_sync_cache,
  710. .execute_write_same = iblock_execute_write_same,
  711. .execute_unmap = iblock_execute_unmap,
  712. };
  713. static sense_reason_t
  714. iblock_parse_cdb(struct se_cmd *cmd)
  715. {
  716. return sbc_parse_cdb(cmd, &iblock_sbc_ops);
  717. }
  718. static bool iblock_get_write_cache(struct se_device *dev)
  719. {
  720. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  721. struct block_device *bd = ib_dev->ibd_bd;
  722. struct request_queue *q = bdev_get_queue(bd);
  723. return test_bit(QUEUE_FLAG_WC, &q->queue_flags);
  724. }
  725. static const struct target_backend_ops iblock_ops = {
  726. .name = "iblock",
  727. .inquiry_prod = "IBLOCK",
  728. .inquiry_rev = IBLOCK_VERSION,
  729. .owner = THIS_MODULE,
  730. .attach_hba = iblock_attach_hba,
  731. .detach_hba = iblock_detach_hba,
  732. .alloc_device = iblock_alloc_device,
  733. .configure_device = iblock_configure_device,
  734. .destroy_device = iblock_destroy_device,
  735. .free_device = iblock_free_device,
  736. .parse_cdb = iblock_parse_cdb,
  737. .set_configfs_dev_params = iblock_set_configfs_dev_params,
  738. .show_configfs_dev_params = iblock_show_configfs_dev_params,
  739. .get_device_type = sbc_get_device_type,
  740. .get_blocks = iblock_get_blocks,
  741. .get_alignment_offset_lbas = iblock_get_alignment_offset_lbas,
  742. .get_lbppbe = iblock_get_lbppbe,
  743. .get_io_min = iblock_get_io_min,
  744. .get_io_opt = iblock_get_io_opt,
  745. .get_write_cache = iblock_get_write_cache,
  746. .tb_dev_attrib_attrs = sbc_attrib_attrs,
  747. };
  748. static int __init iblock_module_init(void)
  749. {
  750. return transport_backend_register(&iblock_ops);
  751. }
  752. static void __exit iblock_module_exit(void)
  753. {
  754. target_backend_unregister(&iblock_ops);
  755. }
  756. MODULE_DESCRIPTION("TCM IBLOCK subsystem plugin");
  757. MODULE_AUTHOR("nab@Linux-iSCSI.org");
  758. MODULE_LICENSE("GPL");
  759. module_init(iblock_module_init);
  760. module_exit(iblock_module_exit);