target_core_iblock.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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)
  266. {
  267. struct se_cmd *cmd = bio->bi_private;
  268. struct iblock_req *ibr = cmd->priv;
  269. if (bio->bi_error) {
  270. pr_err("bio error: %p, err: %d\n", bio, bio->bi_error);
  271. /*
  272. * Bump the ib_bio_err_cnt and release bio.
  273. */
  274. atomic_inc(&ibr->ib_bio_err_cnt);
  275. smp_mb__after_atomic();
  276. }
  277. bio_put(bio);
  278. iblock_complete_cmd(cmd);
  279. }
  280. static struct bio *
  281. iblock_get_bio(struct se_cmd *cmd, sector_t lba, u32 sg_num)
  282. {
  283. struct iblock_dev *ib_dev = IBLOCK_DEV(cmd->se_dev);
  284. struct bio *bio;
  285. /*
  286. * Only allocate as many vector entries as the bio code allows us to,
  287. * we'll loop later on until we have handled the whole request.
  288. */
  289. if (sg_num > BIO_MAX_PAGES)
  290. sg_num = BIO_MAX_PAGES;
  291. bio = bio_alloc_bioset(GFP_NOIO, sg_num, ib_dev->ibd_bio_set);
  292. if (!bio) {
  293. pr_err("Unable to allocate memory for bio\n");
  294. return NULL;
  295. }
  296. bio->bi_bdev = ib_dev->ibd_bd;
  297. bio->bi_private = cmd;
  298. bio->bi_end_io = &iblock_bio_done;
  299. bio->bi_iter.bi_sector = lba;
  300. return bio;
  301. }
  302. static void iblock_submit_bios(struct bio_list *list, int rw)
  303. {
  304. struct blk_plug plug;
  305. struct bio *bio;
  306. blk_start_plug(&plug);
  307. while ((bio = bio_list_pop(list)))
  308. submit_bio(rw, bio);
  309. blk_finish_plug(&plug);
  310. }
  311. static void iblock_end_io_flush(struct bio *bio)
  312. {
  313. struct se_cmd *cmd = bio->bi_private;
  314. if (bio->bi_error)
  315. pr_err("IBLOCK: cache flush failed: %d\n", bio->bi_error);
  316. if (cmd) {
  317. if (bio->bi_error)
  318. target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
  319. else
  320. target_complete_cmd(cmd, SAM_STAT_GOOD);
  321. }
  322. bio_put(bio);
  323. }
  324. /*
  325. * Implement SYCHRONIZE CACHE. Note that we can't handle lba ranges and must
  326. * always flush the whole cache.
  327. */
  328. static sense_reason_t
  329. iblock_execute_sync_cache(struct se_cmd *cmd)
  330. {
  331. struct iblock_dev *ib_dev = IBLOCK_DEV(cmd->se_dev);
  332. int immed = (cmd->t_task_cdb[1] & 0x2);
  333. struct bio *bio;
  334. /*
  335. * If the Immediate bit is set, queue up the GOOD response
  336. * for this SYNCHRONIZE_CACHE op.
  337. */
  338. if (immed)
  339. target_complete_cmd(cmd, SAM_STAT_GOOD);
  340. bio = bio_alloc(GFP_KERNEL, 0);
  341. bio->bi_end_io = iblock_end_io_flush;
  342. bio->bi_bdev = ib_dev->ibd_bd;
  343. if (!immed)
  344. bio->bi_private = cmd;
  345. submit_bio(WRITE_FLUSH, bio);
  346. return 0;
  347. }
  348. static sense_reason_t
  349. iblock_execute_unmap(struct se_cmd *cmd, sector_t lba, sector_t nolb)
  350. {
  351. struct block_device *bdev = IBLOCK_DEV(cmd->se_dev)->ibd_bd;
  352. int ret;
  353. ret = blkdev_issue_discard(bdev, lba, nolb, GFP_KERNEL, 0);
  354. if (ret < 0) {
  355. pr_err("blkdev_issue_discard() failed: %d\n", ret);
  356. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  357. }
  358. return 0;
  359. }
  360. static sense_reason_t
  361. iblock_execute_write_same(struct se_cmd *cmd)
  362. {
  363. struct iblock_req *ibr;
  364. struct scatterlist *sg;
  365. struct bio *bio;
  366. struct bio_list list;
  367. sector_t block_lba = cmd->t_task_lba;
  368. sector_t sectors = sbc_get_write_same_sectors(cmd);
  369. if (cmd->prot_op) {
  370. pr_err("WRITE_SAME: Protection information with IBLOCK"
  371. " backends not supported\n");
  372. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  373. }
  374. sg = &cmd->t_data_sg[0];
  375. if (cmd->t_data_nents > 1 ||
  376. sg->length != cmd->se_dev->dev_attrib.block_size) {
  377. pr_err("WRITE_SAME: Illegal SGL t_data_nents: %u length: %u"
  378. " block_size: %u\n", cmd->t_data_nents, sg->length,
  379. cmd->se_dev->dev_attrib.block_size);
  380. return TCM_INVALID_CDB_FIELD;
  381. }
  382. ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
  383. if (!ibr)
  384. goto fail;
  385. cmd->priv = ibr;
  386. bio = iblock_get_bio(cmd, block_lba, 1);
  387. if (!bio)
  388. goto fail_free_ibr;
  389. bio_list_init(&list);
  390. bio_list_add(&list, bio);
  391. atomic_set(&ibr->pending, 1);
  392. while (sectors) {
  393. while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
  394. != sg->length) {
  395. bio = iblock_get_bio(cmd, block_lba, 1);
  396. if (!bio)
  397. goto fail_put_bios;
  398. atomic_inc(&ibr->pending);
  399. bio_list_add(&list, bio);
  400. }
  401. /* Always in 512 byte units for Linux/Block */
  402. block_lba += sg->length >> IBLOCK_LBA_SHIFT;
  403. sectors -= 1;
  404. }
  405. iblock_submit_bios(&list, WRITE);
  406. return 0;
  407. fail_put_bios:
  408. while ((bio = bio_list_pop(&list)))
  409. bio_put(bio);
  410. fail_free_ibr:
  411. kfree(ibr);
  412. fail:
  413. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  414. }
  415. enum {
  416. Opt_udev_path, Opt_readonly, Opt_force, Opt_err
  417. };
  418. static match_table_t tokens = {
  419. {Opt_udev_path, "udev_path=%s"},
  420. {Opt_readonly, "readonly=%d"},
  421. {Opt_force, "force=%d"},
  422. {Opt_err, NULL}
  423. };
  424. static ssize_t iblock_set_configfs_dev_params(struct se_device *dev,
  425. const char *page, ssize_t count)
  426. {
  427. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  428. char *orig, *ptr, *arg_p, *opts;
  429. substring_t args[MAX_OPT_ARGS];
  430. int ret = 0, token;
  431. unsigned long tmp_readonly;
  432. opts = kstrdup(page, GFP_KERNEL);
  433. if (!opts)
  434. return -ENOMEM;
  435. orig = opts;
  436. while ((ptr = strsep(&opts, ",\n")) != NULL) {
  437. if (!*ptr)
  438. continue;
  439. token = match_token(ptr, tokens, args);
  440. switch (token) {
  441. case Opt_udev_path:
  442. if (ib_dev->ibd_bd) {
  443. pr_err("Unable to set udev_path= while"
  444. " ib_dev->ibd_bd exists\n");
  445. ret = -EEXIST;
  446. goto out;
  447. }
  448. if (match_strlcpy(ib_dev->ibd_udev_path, &args[0],
  449. SE_UDEV_PATH_LEN) == 0) {
  450. ret = -EINVAL;
  451. break;
  452. }
  453. pr_debug("IBLOCK: Referencing UDEV path: %s\n",
  454. ib_dev->ibd_udev_path);
  455. ib_dev->ibd_flags |= IBDF_HAS_UDEV_PATH;
  456. break;
  457. case Opt_readonly:
  458. arg_p = match_strdup(&args[0]);
  459. if (!arg_p) {
  460. ret = -ENOMEM;
  461. break;
  462. }
  463. ret = kstrtoul(arg_p, 0, &tmp_readonly);
  464. kfree(arg_p);
  465. if (ret < 0) {
  466. pr_err("kstrtoul() failed for"
  467. " readonly=\n");
  468. goto out;
  469. }
  470. ib_dev->ibd_readonly = tmp_readonly;
  471. pr_debug("IBLOCK: readonly: %d\n", ib_dev->ibd_readonly);
  472. break;
  473. case Opt_force:
  474. break;
  475. default:
  476. break;
  477. }
  478. }
  479. out:
  480. kfree(orig);
  481. return (!ret) ? count : ret;
  482. }
  483. static ssize_t iblock_show_configfs_dev_params(struct se_device *dev, char *b)
  484. {
  485. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  486. struct block_device *bd = ib_dev->ibd_bd;
  487. char buf[BDEVNAME_SIZE];
  488. ssize_t bl = 0;
  489. if (bd)
  490. bl += sprintf(b + bl, "iBlock device: %s",
  491. bdevname(bd, buf));
  492. if (ib_dev->ibd_flags & IBDF_HAS_UDEV_PATH)
  493. bl += sprintf(b + bl, " UDEV PATH: %s",
  494. ib_dev->ibd_udev_path);
  495. bl += sprintf(b + bl, " readonly: %d\n", ib_dev->ibd_readonly);
  496. bl += sprintf(b + bl, " ");
  497. if (bd) {
  498. bl += sprintf(b + bl, "Major: %d Minor: %d %s\n",
  499. MAJOR(bd->bd_dev), MINOR(bd->bd_dev), (!bd->bd_contains) ?
  500. "" : (bd->bd_holder == ib_dev) ?
  501. "CLAIMED: IBLOCK" : "CLAIMED: OS");
  502. } else {
  503. bl += sprintf(b + bl, "Major: 0 Minor: 0\n");
  504. }
  505. return bl;
  506. }
  507. static int
  508. iblock_alloc_bip(struct se_cmd *cmd, struct bio *bio)
  509. {
  510. struct se_device *dev = cmd->se_dev;
  511. struct blk_integrity *bi;
  512. struct bio_integrity_payload *bip;
  513. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  514. struct scatterlist *sg;
  515. int i, rc;
  516. bi = bdev_get_integrity(ib_dev->ibd_bd);
  517. if (!bi) {
  518. pr_err("Unable to locate bio_integrity\n");
  519. return -ENODEV;
  520. }
  521. bip = bio_integrity_alloc(bio, GFP_NOIO, cmd->t_prot_nents);
  522. if (!bip) {
  523. pr_err("Unable to allocate bio_integrity_payload\n");
  524. return -ENOMEM;
  525. }
  526. bip->bip_iter.bi_size = (cmd->data_length / dev->dev_attrib.block_size) *
  527. dev->prot_length;
  528. bip->bip_iter.bi_sector = bio->bi_iter.bi_sector;
  529. pr_debug("IBLOCK BIP Size: %u Sector: %llu\n", bip->bip_iter.bi_size,
  530. (unsigned long long)bip->bip_iter.bi_sector);
  531. for_each_sg(cmd->t_prot_sg, sg, cmd->t_prot_nents, i) {
  532. rc = bio_integrity_add_page(bio, sg_page(sg), sg->length,
  533. sg->offset);
  534. if (rc != sg->length) {
  535. pr_err("bio_integrity_add_page() failed; %d\n", rc);
  536. return -ENOMEM;
  537. }
  538. pr_debug("Added bio integrity page: %p length: %d offset; %d\n",
  539. sg_page(sg), sg->length, sg->offset);
  540. }
  541. return 0;
  542. }
  543. static sense_reason_t
  544. iblock_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
  545. enum dma_data_direction data_direction)
  546. {
  547. struct se_device *dev = cmd->se_dev;
  548. struct iblock_req *ibr;
  549. struct bio *bio, *bio_start;
  550. struct bio_list list;
  551. struct scatterlist *sg;
  552. u32 sg_num = sgl_nents;
  553. sector_t block_lba;
  554. unsigned bio_cnt;
  555. int rw = 0;
  556. int i;
  557. if (data_direction == DMA_TO_DEVICE) {
  558. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  559. struct request_queue *q = bdev_get_queue(ib_dev->ibd_bd);
  560. /*
  561. * Force writethrough using WRITE_FUA if a volatile write cache
  562. * is not enabled, or if initiator set the Force Unit Access bit.
  563. */
  564. if (q->flush_flags & REQ_FUA) {
  565. if (cmd->se_cmd_flags & SCF_FUA)
  566. rw = WRITE_FUA;
  567. else if (!(q->flush_flags & REQ_FLUSH))
  568. rw = WRITE_FUA;
  569. else
  570. rw = WRITE;
  571. } else {
  572. rw = WRITE;
  573. }
  574. } else {
  575. rw = READ;
  576. }
  577. /*
  578. * Convert the blocksize advertised to the initiator to the 512 byte
  579. * units unconditionally used by the Linux block layer.
  580. */
  581. if (dev->dev_attrib.block_size == 4096)
  582. block_lba = (cmd->t_task_lba << 3);
  583. else if (dev->dev_attrib.block_size == 2048)
  584. block_lba = (cmd->t_task_lba << 2);
  585. else if (dev->dev_attrib.block_size == 1024)
  586. block_lba = (cmd->t_task_lba << 1);
  587. else if (dev->dev_attrib.block_size == 512)
  588. block_lba = cmd->t_task_lba;
  589. else {
  590. pr_err("Unsupported SCSI -> BLOCK LBA conversion:"
  591. " %u\n", dev->dev_attrib.block_size);
  592. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  593. }
  594. ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
  595. if (!ibr)
  596. goto fail;
  597. cmd->priv = ibr;
  598. if (!sgl_nents) {
  599. atomic_set(&ibr->pending, 1);
  600. iblock_complete_cmd(cmd);
  601. return 0;
  602. }
  603. bio = iblock_get_bio(cmd, block_lba, sgl_nents);
  604. if (!bio)
  605. goto fail_free_ibr;
  606. bio_start = bio;
  607. bio_list_init(&list);
  608. bio_list_add(&list, bio);
  609. atomic_set(&ibr->pending, 2);
  610. bio_cnt = 1;
  611. for_each_sg(sgl, sg, sgl_nents, i) {
  612. /*
  613. * XXX: if the length the device accepts is shorter than the
  614. * length of the S/G list entry this will cause and
  615. * endless loop. Better hope no driver uses huge pages.
  616. */
  617. while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
  618. != sg->length) {
  619. if (bio_cnt >= IBLOCK_MAX_BIO_PER_TASK) {
  620. iblock_submit_bios(&list, rw);
  621. bio_cnt = 0;
  622. }
  623. bio = iblock_get_bio(cmd, block_lba, sg_num);
  624. if (!bio)
  625. goto fail_put_bios;
  626. atomic_inc(&ibr->pending);
  627. bio_list_add(&list, bio);
  628. bio_cnt++;
  629. }
  630. /* Always in 512 byte units for Linux/Block */
  631. block_lba += sg->length >> IBLOCK_LBA_SHIFT;
  632. sg_num--;
  633. }
  634. if (cmd->prot_type && dev->dev_attrib.pi_prot_type) {
  635. int rc = iblock_alloc_bip(cmd, bio_start);
  636. if (rc)
  637. goto fail_put_bios;
  638. }
  639. iblock_submit_bios(&list, rw);
  640. iblock_complete_cmd(cmd);
  641. return 0;
  642. fail_put_bios:
  643. while ((bio = bio_list_pop(&list)))
  644. bio_put(bio);
  645. fail_free_ibr:
  646. kfree(ibr);
  647. fail:
  648. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  649. }
  650. static sector_t iblock_get_blocks(struct se_device *dev)
  651. {
  652. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  653. struct block_device *bd = ib_dev->ibd_bd;
  654. struct request_queue *q = bdev_get_queue(bd);
  655. return iblock_emulate_read_cap_with_block_size(dev, bd, q);
  656. }
  657. static sector_t iblock_get_alignment_offset_lbas(struct se_device *dev)
  658. {
  659. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  660. struct block_device *bd = ib_dev->ibd_bd;
  661. int ret;
  662. ret = bdev_alignment_offset(bd);
  663. if (ret == -1)
  664. return 0;
  665. /* convert offset-bytes to offset-lbas */
  666. return ret / bdev_logical_block_size(bd);
  667. }
  668. static unsigned int iblock_get_lbppbe(struct se_device *dev)
  669. {
  670. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  671. struct block_device *bd = ib_dev->ibd_bd;
  672. int logs_per_phys = bdev_physical_block_size(bd) / bdev_logical_block_size(bd);
  673. return ilog2(logs_per_phys);
  674. }
  675. static unsigned int iblock_get_io_min(struct se_device *dev)
  676. {
  677. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  678. struct block_device *bd = ib_dev->ibd_bd;
  679. return bdev_io_min(bd);
  680. }
  681. static unsigned int iblock_get_io_opt(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_opt(bd);
  686. }
  687. static struct sbc_ops iblock_sbc_ops = {
  688. .execute_rw = iblock_execute_rw,
  689. .execute_sync_cache = iblock_execute_sync_cache,
  690. .execute_write_same = iblock_execute_write_same,
  691. .execute_unmap = iblock_execute_unmap,
  692. };
  693. static sense_reason_t
  694. iblock_parse_cdb(struct se_cmd *cmd)
  695. {
  696. return sbc_parse_cdb(cmd, &iblock_sbc_ops);
  697. }
  698. static bool iblock_get_write_cache(struct se_device *dev)
  699. {
  700. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  701. struct block_device *bd = ib_dev->ibd_bd;
  702. struct request_queue *q = bdev_get_queue(bd);
  703. return q->flush_flags & REQ_FLUSH;
  704. }
  705. static const struct target_backend_ops iblock_ops = {
  706. .name = "iblock",
  707. .inquiry_prod = "IBLOCK",
  708. .inquiry_rev = IBLOCK_VERSION,
  709. .owner = THIS_MODULE,
  710. .attach_hba = iblock_attach_hba,
  711. .detach_hba = iblock_detach_hba,
  712. .alloc_device = iblock_alloc_device,
  713. .configure_device = iblock_configure_device,
  714. .free_device = iblock_free_device,
  715. .parse_cdb = iblock_parse_cdb,
  716. .set_configfs_dev_params = iblock_set_configfs_dev_params,
  717. .show_configfs_dev_params = iblock_show_configfs_dev_params,
  718. .get_device_type = sbc_get_device_type,
  719. .get_blocks = iblock_get_blocks,
  720. .get_alignment_offset_lbas = iblock_get_alignment_offset_lbas,
  721. .get_lbppbe = iblock_get_lbppbe,
  722. .get_io_min = iblock_get_io_min,
  723. .get_io_opt = iblock_get_io_opt,
  724. .get_write_cache = iblock_get_write_cache,
  725. .tb_dev_attrib_attrs = sbc_attrib_attrs,
  726. };
  727. static int __init iblock_module_init(void)
  728. {
  729. return transport_backend_register(&iblock_ops);
  730. }
  731. static void __exit iblock_module_exit(void)
  732. {
  733. target_backend_unregister(&iblock_ops);
  734. }
  735. MODULE_DESCRIPTION("TCM IBLOCK subsystem plugin");
  736. MODULE_AUTHOR("nab@Linux-iSCSI.org");
  737. MODULE_LICENSE("GPL");
  738. module_init(iblock_module_init);
  739. module_exit(iblock_module_exit);