target_core_iblock.c 21 KB

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