target_core_iblock.c 21 KB

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