target_core_iblock.c 21 KB

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