target_core_iblock.c 21 KB

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