target_core_iblock.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  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/target_core_backend_configfs.h>
  43. #include "target_core_iblock.h"
  44. #define IBLOCK_MAX_BIO_PER_TASK 32 /* max # of bios to submit at a time */
  45. #define IBLOCK_BIO_POOL_SIZE 128
  46. static inline struct iblock_dev *IBLOCK_DEV(struct se_device *dev)
  47. {
  48. return container_of(dev, struct iblock_dev, dev);
  49. }
  50. static struct se_subsystem_api iblock_template;
  51. /* iblock_attach_hba(): (Part of se_subsystem_api_t template)
  52. *
  53. *
  54. */
  55. static int iblock_attach_hba(struct se_hba *hba, u32 host_id)
  56. {
  57. pr_debug("CORE_HBA[%d] - TCM iBlock HBA Driver %s on"
  58. " Generic Target Core Stack %s\n", hba->hba_id,
  59. IBLOCK_VERSION, TARGET_CORE_MOD_VERSION);
  60. return 0;
  61. }
  62. static void iblock_detach_hba(struct se_hba *hba)
  63. {
  64. }
  65. static struct se_device *iblock_alloc_device(struct se_hba *hba, const char *name)
  66. {
  67. struct iblock_dev *ib_dev = NULL;
  68. ib_dev = kzalloc(sizeof(struct iblock_dev), GFP_KERNEL);
  69. if (!ib_dev) {
  70. pr_err("Unable to allocate struct iblock_dev\n");
  71. return NULL;
  72. }
  73. pr_debug( "IBLOCK: Allocated ib_dev for %s\n", name);
  74. return &ib_dev->dev;
  75. }
  76. static int iblock_configure_device(struct se_device *dev)
  77. {
  78. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  79. struct request_queue *q;
  80. struct block_device *bd = NULL;
  81. struct blk_integrity *bi;
  82. fmode_t mode;
  83. int ret = -ENOMEM;
  84. if (!(ib_dev->ibd_flags & IBDF_HAS_UDEV_PATH)) {
  85. pr_err("Missing udev_path= parameters for IBLOCK\n");
  86. return -EINVAL;
  87. }
  88. ib_dev->ibd_bio_set = bioset_create(IBLOCK_BIO_POOL_SIZE, 0);
  89. if (!ib_dev->ibd_bio_set) {
  90. pr_err("IBLOCK: Unable to create bioset\n");
  91. goto out;
  92. }
  93. pr_debug( "IBLOCK: Claiming struct block_device: %s\n",
  94. ib_dev->ibd_udev_path);
  95. mode = FMODE_READ|FMODE_EXCL;
  96. if (!ib_dev->ibd_readonly)
  97. mode |= FMODE_WRITE;
  98. bd = blkdev_get_by_path(ib_dev->ibd_udev_path, mode, ib_dev);
  99. if (IS_ERR(bd)) {
  100. ret = PTR_ERR(bd);
  101. goto out_free_bioset;
  102. }
  103. ib_dev->ibd_bd = bd;
  104. q = bdev_get_queue(bd);
  105. dev->dev_attrib.hw_block_size = bdev_logical_block_size(bd);
  106. dev->dev_attrib.hw_max_sectors = queue_max_hw_sectors(q);
  107. dev->dev_attrib.hw_queue_depth = q->nr_requests;
  108. /*
  109. * Check if the underlying struct block_device request_queue supports
  110. * the QUEUE_FLAG_DISCARD bit for UNMAP/WRITE_SAME in SCSI + TRIM
  111. * in ATA and we need to set TPE=1
  112. */
  113. if (blk_queue_discard(q)) {
  114. dev->dev_attrib.max_unmap_lba_count =
  115. q->limits.max_discard_sectors;
  116. /*
  117. * Currently hardcoded to 1 in Linux/SCSI code..
  118. */
  119. dev->dev_attrib.max_unmap_block_desc_count = 1;
  120. dev->dev_attrib.unmap_granularity =
  121. q->limits.discard_granularity >> 9;
  122. dev->dev_attrib.unmap_granularity_alignment =
  123. q->limits.discard_alignment;
  124. pr_debug("IBLOCK: BLOCK Discard support available,"
  125. " disabled by default\n");
  126. }
  127. /*
  128. * Enable write same emulation for IBLOCK and use 0xFFFF as
  129. * the smaller WRITE_SAME(10) only has a two-byte block count.
  130. */
  131. dev->dev_attrib.max_write_same_len = 0xFFFF;
  132. if (blk_queue_nonrot(q))
  133. dev->dev_attrib.is_nonrot = 1;
  134. bi = bdev_get_integrity(bd);
  135. if (bi) {
  136. struct bio_set *bs = ib_dev->ibd_bio_set;
  137. if (!strcmp(bi->name, "T10-DIF-TYPE3-IP") ||
  138. !strcmp(bi->name, "T10-DIF-TYPE1-IP")) {
  139. pr_err("IBLOCK export of blk_integrity: %s not"
  140. " supported\n", bi->name);
  141. ret = -ENOSYS;
  142. goto out_blkdev_put;
  143. }
  144. if (!strcmp(bi->name, "T10-DIF-TYPE3-CRC")) {
  145. dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE3_PROT;
  146. } else if (!strcmp(bi->name, "T10-DIF-TYPE1-CRC")) {
  147. dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE1_PROT;
  148. }
  149. if (dev->dev_attrib.pi_prot_type) {
  150. if (bioset_integrity_create(bs, IBLOCK_BIO_POOL_SIZE) < 0) {
  151. pr_err("Unable to allocate bioset for PI\n");
  152. ret = -ENOMEM;
  153. goto out_blkdev_put;
  154. }
  155. pr_debug("IBLOCK setup BIP bs->bio_integrity_pool: %p\n",
  156. bs->bio_integrity_pool);
  157. }
  158. dev->dev_attrib.hw_pi_prot_type = dev->dev_attrib.pi_prot_type;
  159. }
  160. return 0;
  161. out_blkdev_put:
  162. blkdev_put(ib_dev->ibd_bd, FMODE_WRITE|FMODE_READ|FMODE_EXCL);
  163. out_free_bioset:
  164. bioset_free(ib_dev->ibd_bio_set);
  165. ib_dev->ibd_bio_set = NULL;
  166. out:
  167. return ret;
  168. }
  169. static void iblock_free_device(struct se_device *dev)
  170. {
  171. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  172. if (ib_dev->ibd_bd != NULL)
  173. blkdev_put(ib_dev->ibd_bd, FMODE_WRITE|FMODE_READ|FMODE_EXCL);
  174. if (ib_dev->ibd_bio_set != NULL)
  175. bioset_free(ib_dev->ibd_bio_set);
  176. kfree(ib_dev);
  177. }
  178. static unsigned long long iblock_emulate_read_cap_with_block_size(
  179. struct se_device *dev,
  180. struct block_device *bd,
  181. struct request_queue *q)
  182. {
  183. unsigned long long blocks_long = (div_u64(i_size_read(bd->bd_inode),
  184. bdev_logical_block_size(bd)) - 1);
  185. u32 block_size = bdev_logical_block_size(bd);
  186. if (block_size == dev->dev_attrib.block_size)
  187. return blocks_long;
  188. switch (block_size) {
  189. case 4096:
  190. switch (dev->dev_attrib.block_size) {
  191. case 2048:
  192. blocks_long <<= 1;
  193. break;
  194. case 1024:
  195. blocks_long <<= 2;
  196. break;
  197. case 512:
  198. blocks_long <<= 3;
  199. default:
  200. break;
  201. }
  202. break;
  203. case 2048:
  204. switch (dev->dev_attrib.block_size) {
  205. case 4096:
  206. blocks_long >>= 1;
  207. break;
  208. case 1024:
  209. blocks_long <<= 1;
  210. break;
  211. case 512:
  212. blocks_long <<= 2;
  213. break;
  214. default:
  215. break;
  216. }
  217. break;
  218. case 1024:
  219. switch (dev->dev_attrib.block_size) {
  220. case 4096:
  221. blocks_long >>= 2;
  222. break;
  223. case 2048:
  224. blocks_long >>= 1;
  225. break;
  226. case 512:
  227. blocks_long <<= 1;
  228. break;
  229. default:
  230. break;
  231. }
  232. break;
  233. case 512:
  234. switch (dev->dev_attrib.block_size) {
  235. case 4096:
  236. blocks_long >>= 3;
  237. break;
  238. case 2048:
  239. blocks_long >>= 2;
  240. break;
  241. case 1024:
  242. blocks_long >>= 1;
  243. break;
  244. default:
  245. break;
  246. }
  247. break;
  248. default:
  249. break;
  250. }
  251. return blocks_long;
  252. }
  253. static void iblock_complete_cmd(struct se_cmd *cmd)
  254. {
  255. struct iblock_req *ibr = cmd->priv;
  256. u8 status;
  257. if (!atomic_dec_and_test(&ibr->pending))
  258. return;
  259. if (atomic_read(&ibr->ib_bio_err_cnt))
  260. status = SAM_STAT_CHECK_CONDITION;
  261. else
  262. status = SAM_STAT_GOOD;
  263. target_complete_cmd(cmd, status);
  264. kfree(ibr);
  265. }
  266. static void iblock_bio_done(struct bio *bio, int err)
  267. {
  268. struct se_cmd *cmd = bio->bi_private;
  269. struct iblock_req *ibr = cmd->priv;
  270. /*
  271. * Set -EIO if !BIO_UPTODATE and the passed is still err=0
  272. */
  273. if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && !err)
  274. err = -EIO;
  275. if (err != 0) {
  276. pr_err("test_bit(BIO_UPTODATE) failed for bio: %p,"
  277. " err: %d\n", bio, err);
  278. /*
  279. * Bump the ib_bio_err_cnt and release bio.
  280. */
  281. atomic_inc(&ibr->ib_bio_err_cnt);
  282. smp_mb__after_atomic();
  283. }
  284. bio_put(bio);
  285. iblock_complete_cmd(cmd);
  286. }
  287. static struct bio *
  288. iblock_get_bio(struct se_cmd *cmd, sector_t lba, u32 sg_num)
  289. {
  290. struct iblock_dev *ib_dev = IBLOCK_DEV(cmd->se_dev);
  291. struct bio *bio;
  292. /*
  293. * Only allocate as many vector entries as the bio code allows us to,
  294. * we'll loop later on until we have handled the whole request.
  295. */
  296. if (sg_num > BIO_MAX_PAGES)
  297. sg_num = BIO_MAX_PAGES;
  298. bio = bio_alloc_bioset(GFP_NOIO, sg_num, ib_dev->ibd_bio_set);
  299. if (!bio) {
  300. pr_err("Unable to allocate memory for bio\n");
  301. return NULL;
  302. }
  303. bio->bi_bdev = ib_dev->ibd_bd;
  304. bio->bi_private = cmd;
  305. bio->bi_end_io = &iblock_bio_done;
  306. bio->bi_iter.bi_sector = lba;
  307. return bio;
  308. }
  309. static void iblock_submit_bios(struct bio_list *list, int rw)
  310. {
  311. struct blk_plug plug;
  312. struct bio *bio;
  313. blk_start_plug(&plug);
  314. while ((bio = bio_list_pop(list)))
  315. submit_bio(rw, bio);
  316. blk_finish_plug(&plug);
  317. }
  318. static void iblock_end_io_flush(struct bio *bio, int err)
  319. {
  320. struct se_cmd *cmd = bio->bi_private;
  321. if (err)
  322. pr_err("IBLOCK: cache flush failed: %d\n", err);
  323. if (cmd) {
  324. if (err)
  325. target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
  326. else
  327. target_complete_cmd(cmd, SAM_STAT_GOOD);
  328. }
  329. bio_put(bio);
  330. }
  331. /*
  332. * Implement SYCHRONIZE CACHE. Note that we can't handle lba ranges and must
  333. * always flush the whole cache.
  334. */
  335. static sense_reason_t
  336. iblock_execute_sync_cache(struct se_cmd *cmd)
  337. {
  338. struct iblock_dev *ib_dev = IBLOCK_DEV(cmd->se_dev);
  339. int immed = (cmd->t_task_cdb[1] & 0x2);
  340. struct bio *bio;
  341. /*
  342. * If the Immediate bit is set, queue up the GOOD response
  343. * for this SYNCHRONIZE_CACHE op.
  344. */
  345. if (immed)
  346. target_complete_cmd(cmd, SAM_STAT_GOOD);
  347. bio = bio_alloc(GFP_KERNEL, 0);
  348. bio->bi_end_io = iblock_end_io_flush;
  349. bio->bi_bdev = ib_dev->ibd_bd;
  350. if (!immed)
  351. bio->bi_private = cmd;
  352. submit_bio(WRITE_FLUSH, bio);
  353. return 0;
  354. }
  355. static sense_reason_t
  356. iblock_do_unmap(struct se_cmd *cmd, void *priv,
  357. sector_t lba, sector_t nolb)
  358. {
  359. struct block_device *bdev = priv;
  360. int ret;
  361. ret = blkdev_issue_discard(bdev, lba, nolb, GFP_KERNEL, 0);
  362. if (ret < 0) {
  363. pr_err("blkdev_issue_discard() failed: %d\n", ret);
  364. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  365. }
  366. return 0;
  367. }
  368. static sense_reason_t
  369. iblock_execute_unmap(struct se_cmd *cmd)
  370. {
  371. struct block_device *bdev = IBLOCK_DEV(cmd->se_dev)->ibd_bd;
  372. return sbc_execute_unmap(cmd, iblock_do_unmap, bdev);
  373. }
  374. static sense_reason_t
  375. iblock_execute_write_same_unmap(struct se_cmd *cmd)
  376. {
  377. struct block_device *bdev = IBLOCK_DEV(cmd->se_dev)->ibd_bd;
  378. sector_t lba = cmd->t_task_lba;
  379. sector_t nolb = sbc_get_write_same_sectors(cmd);
  380. int ret;
  381. ret = iblock_do_unmap(cmd, bdev, lba, nolb);
  382. if (ret)
  383. return ret;
  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 iblock_req *ibr;
  391. struct scatterlist *sg;
  392. struct bio *bio;
  393. struct bio_list list;
  394. sector_t block_lba = cmd->t_task_lba;
  395. sector_t sectors = sbc_get_write_same_sectors(cmd);
  396. sg = &cmd->t_data_sg[0];
  397. if (cmd->t_data_nents > 1 ||
  398. sg->length != cmd->se_dev->dev_attrib.block_size) {
  399. pr_err("WRITE_SAME: Illegal SGL t_data_nents: %u length: %u"
  400. " block_size: %u\n", cmd->t_data_nents, sg->length,
  401. cmd->se_dev->dev_attrib.block_size);
  402. return TCM_INVALID_CDB_FIELD;
  403. }
  404. ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
  405. if (!ibr)
  406. goto fail;
  407. cmd->priv = ibr;
  408. bio = iblock_get_bio(cmd, block_lba, 1);
  409. if (!bio)
  410. goto fail_free_ibr;
  411. bio_list_init(&list);
  412. bio_list_add(&list, bio);
  413. atomic_set(&ibr->pending, 1);
  414. while (sectors) {
  415. while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
  416. != sg->length) {
  417. bio = iblock_get_bio(cmd, block_lba, 1);
  418. if (!bio)
  419. goto fail_put_bios;
  420. atomic_inc(&ibr->pending);
  421. bio_list_add(&list, bio);
  422. }
  423. /* Always in 512 byte units for Linux/Block */
  424. block_lba += sg->length >> IBLOCK_LBA_SHIFT;
  425. sectors -= 1;
  426. }
  427. iblock_submit_bios(&list, WRITE);
  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), (!bd->bd_contains) ?
  522. "" : (bd->bd_holder == ib_dev) ?
  523. "CLAIMED: IBLOCK" : "CLAIMED: OS");
  524. } else {
  525. bl += sprintf(b + bl, "Major: 0 Minor: 0\n");
  526. }
  527. return bl;
  528. }
  529. static int
  530. iblock_alloc_bip(struct se_cmd *cmd, struct bio *bio)
  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. struct scatterlist *sg;
  537. int i, rc;
  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, cmd->t_prot_nents);
  544. if (!bip) {
  545. pr_err("Unable to allocate bio_integrity_payload\n");
  546. return -ENOMEM;
  547. }
  548. bip->bip_iter.bi_size = (cmd->data_length / dev->dev_attrib.block_size) *
  549. dev->prot_length;
  550. bip->bip_iter.bi_sector = bio->bi_iter.bi_sector;
  551. pr_debug("IBLOCK BIP Size: %u Sector: %llu\n", bip->bip_iter.bi_size,
  552. (unsigned long long)bip->bip_iter.bi_sector);
  553. for_each_sg(cmd->t_prot_sg, sg, cmd->t_prot_nents, i) {
  554. rc = bio_integrity_add_page(bio, sg_page(sg), sg->length,
  555. sg->offset);
  556. if (rc != sg->length) {
  557. pr_err("bio_integrity_add_page() failed; %d\n", rc);
  558. return -ENOMEM;
  559. }
  560. pr_debug("Added bio integrity page: %p length: %d offset; %d\n",
  561. sg_page(sg), sg->length, sg->offset);
  562. }
  563. return 0;
  564. }
  565. static sense_reason_t
  566. iblock_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
  567. enum dma_data_direction data_direction)
  568. {
  569. struct se_device *dev = cmd->se_dev;
  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. sector_t block_lba;
  576. unsigned bio_cnt;
  577. int rw = 0;
  578. int i;
  579. if (data_direction == DMA_TO_DEVICE) {
  580. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  581. struct request_queue *q = bdev_get_queue(ib_dev->ibd_bd);
  582. /*
  583. * Force writethrough using WRITE_FUA if a volatile write cache
  584. * is not enabled, or if initiator set the Force Unit Access bit.
  585. */
  586. if (q->flush_flags & REQ_FUA) {
  587. if (cmd->se_cmd_flags & SCF_FUA)
  588. rw = WRITE_FUA;
  589. else if (!(q->flush_flags & REQ_FLUSH))
  590. rw = WRITE_FUA;
  591. else
  592. rw = WRITE;
  593. } else {
  594. rw = WRITE;
  595. }
  596. } else {
  597. rw = READ;
  598. }
  599. /*
  600. * Convert the blocksize advertised to the initiator to the 512 byte
  601. * units unconditionally used by the Linux block layer.
  602. */
  603. if (dev->dev_attrib.block_size == 4096)
  604. block_lba = (cmd->t_task_lba << 3);
  605. else if (dev->dev_attrib.block_size == 2048)
  606. block_lba = (cmd->t_task_lba << 2);
  607. else if (dev->dev_attrib.block_size == 1024)
  608. block_lba = (cmd->t_task_lba << 1);
  609. else if (dev->dev_attrib.block_size == 512)
  610. block_lba = cmd->t_task_lba;
  611. else {
  612. pr_err("Unsupported SCSI -> BLOCK LBA conversion:"
  613. " %u\n", dev->dev_attrib.block_size);
  614. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  615. }
  616. ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
  617. if (!ibr)
  618. goto fail;
  619. cmd->priv = ibr;
  620. if (!sgl_nents) {
  621. atomic_set(&ibr->pending, 1);
  622. iblock_complete_cmd(cmd);
  623. return 0;
  624. }
  625. bio = iblock_get_bio(cmd, block_lba, sgl_nents);
  626. if (!bio)
  627. goto fail_free_ibr;
  628. bio_start = bio;
  629. bio_list_init(&list);
  630. bio_list_add(&list, bio);
  631. atomic_set(&ibr->pending, 2);
  632. bio_cnt = 1;
  633. for_each_sg(sgl, sg, sgl_nents, i) {
  634. /*
  635. * XXX: if the length the device accepts is shorter than the
  636. * length of the S/G list entry this will cause and
  637. * endless loop. Better hope no driver uses huge pages.
  638. */
  639. while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
  640. != sg->length) {
  641. if (bio_cnt >= IBLOCK_MAX_BIO_PER_TASK) {
  642. iblock_submit_bios(&list, rw);
  643. bio_cnt = 0;
  644. }
  645. bio = iblock_get_bio(cmd, block_lba, sg_num);
  646. if (!bio)
  647. goto fail_put_bios;
  648. atomic_inc(&ibr->pending);
  649. bio_list_add(&list, bio);
  650. bio_cnt++;
  651. }
  652. /* Always in 512 byte units for Linux/Block */
  653. block_lba += sg->length >> IBLOCK_LBA_SHIFT;
  654. sg_num--;
  655. }
  656. if (cmd->prot_type) {
  657. int rc = iblock_alloc_bip(cmd, bio_start);
  658. if (rc)
  659. goto fail_put_bios;
  660. }
  661. iblock_submit_bios(&list, rw);
  662. iblock_complete_cmd(cmd);
  663. return 0;
  664. fail_put_bios:
  665. while ((bio = bio_list_pop(&list)))
  666. bio_put(bio);
  667. fail_free_ibr:
  668. kfree(ibr);
  669. fail:
  670. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  671. }
  672. static sector_t iblock_get_blocks(struct se_device *dev)
  673. {
  674. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  675. struct block_device *bd = ib_dev->ibd_bd;
  676. struct request_queue *q = bdev_get_queue(bd);
  677. return iblock_emulate_read_cap_with_block_size(dev, bd, q);
  678. }
  679. static sector_t iblock_get_alignment_offset_lbas(struct se_device *dev)
  680. {
  681. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  682. struct block_device *bd = ib_dev->ibd_bd;
  683. int ret;
  684. ret = bdev_alignment_offset(bd);
  685. if (ret == -1)
  686. return 0;
  687. /* convert offset-bytes to offset-lbas */
  688. return ret / bdev_logical_block_size(bd);
  689. }
  690. static unsigned int iblock_get_lbppbe(struct se_device *dev)
  691. {
  692. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  693. struct block_device *bd = ib_dev->ibd_bd;
  694. int logs_per_phys = bdev_physical_block_size(bd) / bdev_logical_block_size(bd);
  695. return ilog2(logs_per_phys);
  696. }
  697. static unsigned int iblock_get_io_min(struct se_device *dev)
  698. {
  699. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  700. struct block_device *bd = ib_dev->ibd_bd;
  701. return bdev_io_min(bd);
  702. }
  703. static unsigned int iblock_get_io_opt(struct se_device *dev)
  704. {
  705. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  706. struct block_device *bd = ib_dev->ibd_bd;
  707. return bdev_io_opt(bd);
  708. }
  709. static struct sbc_ops iblock_sbc_ops = {
  710. .execute_rw = iblock_execute_rw,
  711. .execute_sync_cache = iblock_execute_sync_cache,
  712. .execute_write_same = iblock_execute_write_same,
  713. .execute_write_same_unmap = iblock_execute_write_same_unmap,
  714. .execute_unmap = iblock_execute_unmap,
  715. };
  716. static sense_reason_t
  717. iblock_parse_cdb(struct se_cmd *cmd)
  718. {
  719. return sbc_parse_cdb(cmd, &iblock_sbc_ops);
  720. }
  721. static bool iblock_get_write_cache(struct se_device *dev)
  722. {
  723. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  724. struct block_device *bd = ib_dev->ibd_bd;
  725. struct request_queue *q = bdev_get_queue(bd);
  726. return q->flush_flags & REQ_FLUSH;
  727. }
  728. DEF_TB_DEFAULT_ATTRIBS(iblock);
  729. static struct configfs_attribute *iblock_backend_dev_attrs[] = {
  730. &iblock_dev_attrib_emulate_model_alias.attr,
  731. &iblock_dev_attrib_emulate_dpo.attr,
  732. &iblock_dev_attrib_emulate_fua_write.attr,
  733. &iblock_dev_attrib_emulate_fua_read.attr,
  734. &iblock_dev_attrib_emulate_write_cache.attr,
  735. &iblock_dev_attrib_emulate_ua_intlck_ctrl.attr,
  736. &iblock_dev_attrib_emulate_tas.attr,
  737. &iblock_dev_attrib_emulate_tpu.attr,
  738. &iblock_dev_attrib_emulate_tpws.attr,
  739. &iblock_dev_attrib_emulate_caw.attr,
  740. &iblock_dev_attrib_emulate_3pc.attr,
  741. &iblock_dev_attrib_pi_prot_type.attr,
  742. &iblock_dev_attrib_hw_pi_prot_type.attr,
  743. &iblock_dev_attrib_pi_prot_format.attr,
  744. &iblock_dev_attrib_enforce_pr_isids.attr,
  745. &iblock_dev_attrib_is_nonrot.attr,
  746. &iblock_dev_attrib_emulate_rest_reord.attr,
  747. &iblock_dev_attrib_force_pr_aptpl.attr,
  748. &iblock_dev_attrib_hw_block_size.attr,
  749. &iblock_dev_attrib_block_size.attr,
  750. &iblock_dev_attrib_hw_max_sectors.attr,
  751. &iblock_dev_attrib_optimal_sectors.attr,
  752. &iblock_dev_attrib_hw_queue_depth.attr,
  753. &iblock_dev_attrib_queue_depth.attr,
  754. &iblock_dev_attrib_max_unmap_lba_count.attr,
  755. &iblock_dev_attrib_max_unmap_block_desc_count.attr,
  756. &iblock_dev_attrib_unmap_granularity.attr,
  757. &iblock_dev_attrib_unmap_granularity_alignment.attr,
  758. &iblock_dev_attrib_max_write_same_len.attr,
  759. NULL,
  760. };
  761. static struct se_subsystem_api iblock_template = {
  762. .name = "iblock",
  763. .inquiry_prod = "IBLOCK",
  764. .inquiry_rev = IBLOCK_VERSION,
  765. .owner = THIS_MODULE,
  766. .transport_type = TRANSPORT_PLUGIN_VHBA_PDEV,
  767. .attach_hba = iblock_attach_hba,
  768. .detach_hba = iblock_detach_hba,
  769. .alloc_device = iblock_alloc_device,
  770. .configure_device = iblock_configure_device,
  771. .free_device = iblock_free_device,
  772. .parse_cdb = iblock_parse_cdb,
  773. .set_configfs_dev_params = iblock_set_configfs_dev_params,
  774. .show_configfs_dev_params = iblock_show_configfs_dev_params,
  775. .get_device_type = sbc_get_device_type,
  776. .get_blocks = iblock_get_blocks,
  777. .get_alignment_offset_lbas = iblock_get_alignment_offset_lbas,
  778. .get_lbppbe = iblock_get_lbppbe,
  779. .get_io_min = iblock_get_io_min,
  780. .get_io_opt = iblock_get_io_opt,
  781. .get_write_cache = iblock_get_write_cache,
  782. };
  783. static int __init iblock_module_init(void)
  784. {
  785. struct target_backend_cits *tbc = &iblock_template.tb_cits;
  786. target_core_setup_sub_cits(&iblock_template);
  787. tbc->tb_dev_attrib_cit.ct_attrs = iblock_backend_dev_attrs;
  788. return transport_subsystem_register(&iblock_template);
  789. }
  790. static void __exit iblock_module_exit(void)
  791. {
  792. transport_subsystem_release(&iblock_template);
  793. }
  794. MODULE_DESCRIPTION("TCM IBLOCK subsystem plugin");
  795. MODULE_AUTHOR("nab@Linux-iSCSI.org");
  796. MODULE_LICENSE("GPL");
  797. module_init(iblock_module_init);
  798. module_exit(iblock_module_exit);