target_core_iblock.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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-2012 RisingTide Systems LLC.
  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_core_iblock.h"
  43. #define IBLOCK_MAX_BIO_PER_TASK 32 /* max # of bios to submit at a time */
  44. #define IBLOCK_BIO_POOL_SIZE 128
  45. static inline struct iblock_dev *IBLOCK_DEV(struct se_device *dev)
  46. {
  47. return container_of(dev, struct iblock_dev, dev);
  48. }
  49. static struct se_subsystem_api iblock_template;
  50. /* iblock_attach_hba(): (Part of se_subsystem_api_t template)
  51. *
  52. *
  53. */
  54. static int iblock_attach_hba(struct se_hba *hba, u32 host_id)
  55. {
  56. pr_debug("CORE_HBA[%d] - TCM iBlock HBA Driver %s on"
  57. " Generic Target Core Stack %s\n", hba->hba_id,
  58. IBLOCK_VERSION, TARGET_CORE_MOD_VERSION);
  59. return 0;
  60. }
  61. static void iblock_detach_hba(struct se_hba *hba)
  62. {
  63. }
  64. static struct se_device *iblock_alloc_device(struct se_hba *hba, const char *name)
  65. {
  66. struct iblock_dev *ib_dev = NULL;
  67. ib_dev = kzalloc(sizeof(struct iblock_dev), GFP_KERNEL);
  68. if (!ib_dev) {
  69. pr_err("Unable to allocate struct iblock_dev\n");
  70. return NULL;
  71. }
  72. pr_debug( "IBLOCK: Allocated ib_dev for %s\n", name);
  73. return &ib_dev->dev;
  74. }
  75. static int iblock_configure_device(struct se_device *dev)
  76. {
  77. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  78. struct request_queue *q;
  79. struct block_device *bd = NULL;
  80. fmode_t mode;
  81. int ret = -ENOMEM;
  82. if (!(ib_dev->ibd_flags & IBDF_HAS_UDEV_PATH)) {
  83. pr_err("Missing udev_path= parameters for IBLOCK\n");
  84. return -EINVAL;
  85. }
  86. ib_dev->ibd_bio_set = bioset_create(IBLOCK_BIO_POOL_SIZE, 0);
  87. if (!ib_dev->ibd_bio_set) {
  88. pr_err("IBLOCK: Unable to create bioset\n");
  89. goto out;
  90. }
  91. pr_debug( "IBLOCK: Claiming struct block_device: %s\n",
  92. ib_dev->ibd_udev_path);
  93. mode = FMODE_READ|FMODE_EXCL;
  94. if (!ib_dev->ibd_readonly)
  95. mode |= FMODE_WRITE;
  96. bd = blkdev_get_by_path(ib_dev->ibd_udev_path, mode, ib_dev);
  97. if (IS_ERR(bd)) {
  98. ret = PTR_ERR(bd);
  99. goto out_free_bioset;
  100. }
  101. ib_dev->ibd_bd = bd;
  102. q = bdev_get_queue(bd);
  103. dev->dev_attrib.hw_block_size = bdev_logical_block_size(bd);
  104. dev->dev_attrib.hw_max_sectors = UINT_MAX;
  105. dev->dev_attrib.hw_queue_depth = q->nr_requests;
  106. /*
  107. * Check if the underlying struct block_device request_queue supports
  108. * the QUEUE_FLAG_DISCARD bit for UNMAP/WRITE_SAME in SCSI + TRIM
  109. * in ATA and we need to set TPE=1
  110. */
  111. if (blk_queue_discard(q)) {
  112. dev->dev_attrib.max_unmap_lba_count =
  113. q->limits.max_discard_sectors;
  114. /*
  115. * Currently hardcoded to 1 in Linux/SCSI code..
  116. */
  117. dev->dev_attrib.max_unmap_block_desc_count = 1;
  118. dev->dev_attrib.unmap_granularity =
  119. q->limits.discard_granularity >> 9;
  120. dev->dev_attrib.unmap_granularity_alignment =
  121. q->limits.discard_alignment;
  122. pr_debug("IBLOCK: BLOCK Discard support available,"
  123. " disabled by default\n");
  124. }
  125. /*
  126. * Enable write same emulation for IBLOCK and use 0xFFFF as
  127. * the smaller WRITE_SAME(10) only has a two-byte block count.
  128. */
  129. dev->dev_attrib.max_write_same_len = 0xFFFF;
  130. if (blk_queue_nonrot(q))
  131. dev->dev_attrib.is_nonrot = 1;
  132. return 0;
  133. out_free_bioset:
  134. bioset_free(ib_dev->ibd_bio_set);
  135. ib_dev->ibd_bio_set = NULL;
  136. out:
  137. return ret;
  138. }
  139. static void iblock_free_device(struct se_device *dev)
  140. {
  141. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  142. if (ib_dev->ibd_bd != NULL)
  143. blkdev_put(ib_dev->ibd_bd, FMODE_WRITE|FMODE_READ|FMODE_EXCL);
  144. if (ib_dev->ibd_bio_set != NULL)
  145. bioset_free(ib_dev->ibd_bio_set);
  146. kfree(ib_dev);
  147. }
  148. static unsigned long long iblock_emulate_read_cap_with_block_size(
  149. struct se_device *dev,
  150. struct block_device *bd,
  151. struct request_queue *q)
  152. {
  153. unsigned long long blocks_long = (div_u64(i_size_read(bd->bd_inode),
  154. bdev_logical_block_size(bd)) - 1);
  155. u32 block_size = bdev_logical_block_size(bd);
  156. if (block_size == dev->dev_attrib.block_size)
  157. return blocks_long;
  158. switch (block_size) {
  159. case 4096:
  160. switch (dev->dev_attrib.block_size) {
  161. case 2048:
  162. blocks_long <<= 1;
  163. break;
  164. case 1024:
  165. blocks_long <<= 2;
  166. break;
  167. case 512:
  168. blocks_long <<= 3;
  169. default:
  170. break;
  171. }
  172. break;
  173. case 2048:
  174. switch (dev->dev_attrib.block_size) {
  175. case 4096:
  176. blocks_long >>= 1;
  177. break;
  178. case 1024:
  179. blocks_long <<= 1;
  180. break;
  181. case 512:
  182. blocks_long <<= 2;
  183. break;
  184. default:
  185. break;
  186. }
  187. break;
  188. case 1024:
  189. switch (dev->dev_attrib.block_size) {
  190. case 4096:
  191. blocks_long >>= 2;
  192. break;
  193. case 2048:
  194. blocks_long >>= 1;
  195. break;
  196. case 512:
  197. blocks_long <<= 1;
  198. break;
  199. default:
  200. break;
  201. }
  202. break;
  203. case 512:
  204. switch (dev->dev_attrib.block_size) {
  205. case 4096:
  206. blocks_long >>= 3;
  207. break;
  208. case 2048:
  209. blocks_long >>= 2;
  210. break;
  211. case 1024:
  212. blocks_long >>= 1;
  213. break;
  214. default:
  215. break;
  216. }
  217. break;
  218. default:
  219. break;
  220. }
  221. return blocks_long;
  222. }
  223. static void iblock_complete_cmd(struct se_cmd *cmd)
  224. {
  225. struct iblock_req *ibr = cmd->priv;
  226. u8 status;
  227. if (!atomic_dec_and_test(&ibr->pending))
  228. return;
  229. if (atomic_read(&ibr->ib_bio_err_cnt))
  230. status = SAM_STAT_CHECK_CONDITION;
  231. else
  232. status = SAM_STAT_GOOD;
  233. target_complete_cmd(cmd, status);
  234. kfree(ibr);
  235. }
  236. static void iblock_bio_done(struct bio *bio, int err)
  237. {
  238. struct se_cmd *cmd = bio->bi_private;
  239. struct iblock_req *ibr = cmd->priv;
  240. /*
  241. * Set -EIO if !BIO_UPTODATE and the passed is still err=0
  242. */
  243. if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && !err)
  244. err = -EIO;
  245. if (err != 0) {
  246. pr_err("test_bit(BIO_UPTODATE) failed for bio: %p,"
  247. " err: %d\n", bio, err);
  248. /*
  249. * Bump the ib_bio_err_cnt and release bio.
  250. */
  251. atomic_inc(&ibr->ib_bio_err_cnt);
  252. smp_mb__after_atomic_inc();
  253. }
  254. bio_put(bio);
  255. iblock_complete_cmd(cmd);
  256. }
  257. static struct bio *
  258. iblock_get_bio(struct se_cmd *cmd, sector_t lba, u32 sg_num)
  259. {
  260. struct iblock_dev *ib_dev = IBLOCK_DEV(cmd->se_dev);
  261. struct bio *bio;
  262. /*
  263. * Only allocate as many vector entries as the bio code allows us to,
  264. * we'll loop later on until we have handled the whole request.
  265. */
  266. if (sg_num > BIO_MAX_PAGES)
  267. sg_num = BIO_MAX_PAGES;
  268. bio = bio_alloc_bioset(GFP_NOIO, sg_num, ib_dev->ibd_bio_set);
  269. if (!bio) {
  270. pr_err("Unable to allocate memory for bio\n");
  271. return NULL;
  272. }
  273. bio->bi_bdev = ib_dev->ibd_bd;
  274. bio->bi_private = cmd;
  275. bio->bi_end_io = &iblock_bio_done;
  276. bio->bi_sector = lba;
  277. return bio;
  278. }
  279. static void iblock_submit_bios(struct bio_list *list, int rw)
  280. {
  281. struct blk_plug plug;
  282. struct bio *bio;
  283. blk_start_plug(&plug);
  284. while ((bio = bio_list_pop(list)))
  285. submit_bio(rw, bio);
  286. blk_finish_plug(&plug);
  287. }
  288. static void iblock_end_io_flush(struct bio *bio, int err)
  289. {
  290. struct se_cmd *cmd = bio->bi_private;
  291. if (err)
  292. pr_err("IBLOCK: cache flush failed: %d\n", err);
  293. if (cmd) {
  294. if (err)
  295. target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
  296. else
  297. target_complete_cmd(cmd, SAM_STAT_GOOD);
  298. }
  299. bio_put(bio);
  300. }
  301. /*
  302. * Implement SYCHRONIZE CACHE. Note that we can't handle lba ranges and must
  303. * always flush the whole cache.
  304. */
  305. static sense_reason_t
  306. iblock_execute_sync_cache(struct se_cmd *cmd)
  307. {
  308. struct iblock_dev *ib_dev = IBLOCK_DEV(cmd->se_dev);
  309. int immed = (cmd->t_task_cdb[1] & 0x2);
  310. struct bio *bio;
  311. /*
  312. * If the Immediate bit is set, queue up the GOOD response
  313. * for this SYNCHRONIZE_CACHE op.
  314. */
  315. if (immed)
  316. target_complete_cmd(cmd, SAM_STAT_GOOD);
  317. bio = bio_alloc(GFP_KERNEL, 0);
  318. bio->bi_end_io = iblock_end_io_flush;
  319. bio->bi_bdev = ib_dev->ibd_bd;
  320. if (!immed)
  321. bio->bi_private = cmd;
  322. submit_bio(WRITE_FLUSH, bio);
  323. return 0;
  324. }
  325. static sense_reason_t
  326. iblock_execute_unmap(struct se_cmd *cmd)
  327. {
  328. struct se_device *dev = cmd->se_dev;
  329. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  330. unsigned char *buf, *ptr = NULL;
  331. sector_t lba;
  332. int size;
  333. u32 range;
  334. sense_reason_t ret = 0;
  335. int dl, bd_dl, err;
  336. if (cmd->data_length < 8) {
  337. pr_warn("UNMAP parameter list length %u too small\n",
  338. cmd->data_length);
  339. return TCM_INVALID_PARAMETER_LIST;
  340. }
  341. buf = transport_kmap_data_sg(cmd);
  342. if (!buf)
  343. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  344. dl = get_unaligned_be16(&buf[0]);
  345. bd_dl = get_unaligned_be16(&buf[2]);
  346. size = cmd->data_length - 8;
  347. if (bd_dl > size)
  348. pr_warn("UNMAP parameter list length %u too small, ignoring bd_dl %u\n",
  349. cmd->data_length, bd_dl);
  350. else
  351. size = bd_dl;
  352. if (size / 16 > dev->dev_attrib.max_unmap_block_desc_count) {
  353. ret = TCM_INVALID_PARAMETER_LIST;
  354. goto err;
  355. }
  356. /* First UNMAP block descriptor starts at 8 byte offset */
  357. ptr = &buf[8];
  358. pr_debug("UNMAP: Sub: %s Using dl: %u bd_dl: %u size: %u"
  359. " ptr: %p\n", dev->transport->name, dl, bd_dl, size, ptr);
  360. while (size >= 16) {
  361. lba = get_unaligned_be64(&ptr[0]);
  362. range = get_unaligned_be32(&ptr[8]);
  363. pr_debug("UNMAP: Using lba: %llu and range: %u\n",
  364. (unsigned long long)lba, range);
  365. if (range > dev->dev_attrib.max_unmap_lba_count) {
  366. ret = TCM_INVALID_PARAMETER_LIST;
  367. goto err;
  368. }
  369. if (lba + range > dev->transport->get_blocks(dev) + 1) {
  370. ret = TCM_ADDRESS_OUT_OF_RANGE;
  371. goto err;
  372. }
  373. err = blkdev_issue_discard(ib_dev->ibd_bd, lba, range,
  374. GFP_KERNEL, 0);
  375. if (err < 0) {
  376. pr_err("blkdev_issue_discard() failed: %d\n",
  377. err);
  378. ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  379. goto err;
  380. }
  381. ptr += 16;
  382. size -= 16;
  383. }
  384. err:
  385. transport_kunmap_data_sg(cmd);
  386. if (!ret)
  387. target_complete_cmd(cmd, GOOD);
  388. return ret;
  389. }
  390. static sense_reason_t
  391. iblock_execute_write_same_unmap(struct se_cmd *cmd)
  392. {
  393. struct iblock_dev *ib_dev = IBLOCK_DEV(cmd->se_dev);
  394. int rc;
  395. rc = blkdev_issue_discard(ib_dev->ibd_bd, cmd->t_task_lba,
  396. spc_get_write_same_sectors(cmd), GFP_KERNEL, 0);
  397. if (rc < 0) {
  398. pr_warn("blkdev_issue_discard() failed: %d\n", rc);
  399. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  400. }
  401. target_complete_cmd(cmd, GOOD);
  402. return 0;
  403. }
  404. static sense_reason_t
  405. iblock_execute_write_same(struct se_cmd *cmd)
  406. {
  407. struct iblock_req *ibr;
  408. struct scatterlist *sg;
  409. struct bio *bio;
  410. struct bio_list list;
  411. sector_t block_lba = cmd->t_task_lba;
  412. sector_t sectors = spc_get_write_same_sectors(cmd);
  413. sg = &cmd->t_data_sg[0];
  414. if (cmd->t_data_nents > 1 ||
  415. sg->length != cmd->se_dev->dev_attrib.block_size) {
  416. pr_err("WRITE_SAME: Illegal SGL t_data_nents: %u length: %u"
  417. " block_size: %u\n", cmd->t_data_nents, sg->length,
  418. cmd->se_dev->dev_attrib.block_size);
  419. return TCM_INVALID_CDB_FIELD;
  420. }
  421. ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
  422. if (!ibr)
  423. goto fail;
  424. cmd->priv = ibr;
  425. bio = iblock_get_bio(cmd, block_lba, 1);
  426. if (!bio)
  427. goto fail_free_ibr;
  428. bio_list_init(&list);
  429. bio_list_add(&list, bio);
  430. atomic_set(&ibr->pending, 1);
  431. while (sectors) {
  432. while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
  433. != sg->length) {
  434. bio = iblock_get_bio(cmd, block_lba, 1);
  435. if (!bio)
  436. goto fail_put_bios;
  437. atomic_inc(&ibr->pending);
  438. bio_list_add(&list, bio);
  439. }
  440. /* Always in 512 byte units for Linux/Block */
  441. block_lba += sg->length >> IBLOCK_LBA_SHIFT;
  442. sectors -= 1;
  443. }
  444. iblock_submit_bios(&list, WRITE);
  445. return 0;
  446. fail_put_bios:
  447. while ((bio = bio_list_pop(&list)))
  448. bio_put(bio);
  449. fail_free_ibr:
  450. kfree(ibr);
  451. fail:
  452. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  453. }
  454. enum {
  455. Opt_udev_path, Opt_readonly, Opt_force, Opt_err
  456. };
  457. static match_table_t tokens = {
  458. {Opt_udev_path, "udev_path=%s"},
  459. {Opt_readonly, "readonly=%d"},
  460. {Opt_force, "force=%d"},
  461. {Opt_err, NULL}
  462. };
  463. static ssize_t iblock_set_configfs_dev_params(struct se_device *dev,
  464. const char *page, ssize_t count)
  465. {
  466. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  467. char *orig, *ptr, *arg_p, *opts;
  468. substring_t args[MAX_OPT_ARGS];
  469. int ret = 0, token;
  470. unsigned long tmp_readonly;
  471. opts = kstrdup(page, GFP_KERNEL);
  472. if (!opts)
  473. return -ENOMEM;
  474. orig = opts;
  475. while ((ptr = strsep(&opts, ",\n")) != NULL) {
  476. if (!*ptr)
  477. continue;
  478. token = match_token(ptr, tokens, args);
  479. switch (token) {
  480. case Opt_udev_path:
  481. if (ib_dev->ibd_bd) {
  482. pr_err("Unable to set udev_path= while"
  483. " ib_dev->ibd_bd exists\n");
  484. ret = -EEXIST;
  485. goto out;
  486. }
  487. if (match_strlcpy(ib_dev->ibd_udev_path, &args[0],
  488. SE_UDEV_PATH_LEN) == 0) {
  489. ret = -EINVAL;
  490. break;
  491. }
  492. pr_debug("IBLOCK: Referencing UDEV path: %s\n",
  493. ib_dev->ibd_udev_path);
  494. ib_dev->ibd_flags |= IBDF_HAS_UDEV_PATH;
  495. break;
  496. case Opt_readonly:
  497. arg_p = match_strdup(&args[0]);
  498. if (!arg_p) {
  499. ret = -ENOMEM;
  500. break;
  501. }
  502. ret = strict_strtoul(arg_p, 0, &tmp_readonly);
  503. kfree(arg_p);
  504. if (ret < 0) {
  505. pr_err("strict_strtoul() failed for"
  506. " readonly=\n");
  507. goto out;
  508. }
  509. ib_dev->ibd_readonly = tmp_readonly;
  510. pr_debug("IBLOCK: readonly: %d\n", ib_dev->ibd_readonly);
  511. break;
  512. case Opt_force:
  513. break;
  514. default:
  515. break;
  516. }
  517. }
  518. out:
  519. kfree(orig);
  520. return (!ret) ? count : ret;
  521. }
  522. static ssize_t iblock_show_configfs_dev_params(struct se_device *dev, char *b)
  523. {
  524. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  525. struct block_device *bd = ib_dev->ibd_bd;
  526. char buf[BDEVNAME_SIZE];
  527. ssize_t bl = 0;
  528. if (bd)
  529. bl += sprintf(b + bl, "iBlock device: %s",
  530. bdevname(bd, buf));
  531. if (ib_dev->ibd_flags & IBDF_HAS_UDEV_PATH)
  532. bl += sprintf(b + bl, " UDEV PATH: %s",
  533. ib_dev->ibd_udev_path);
  534. bl += sprintf(b + bl, " readonly: %d\n", ib_dev->ibd_readonly);
  535. bl += sprintf(b + bl, " ");
  536. if (bd) {
  537. bl += sprintf(b + bl, "Major: %d Minor: %d %s\n",
  538. MAJOR(bd->bd_dev), MINOR(bd->bd_dev), (!bd->bd_contains) ?
  539. "" : (bd->bd_holder == ib_dev) ?
  540. "CLAIMED: IBLOCK" : "CLAIMED: OS");
  541. } else {
  542. bl += sprintf(b + bl, "Major: 0 Minor: 0\n");
  543. }
  544. return bl;
  545. }
  546. static sense_reason_t
  547. iblock_execute_rw(struct se_cmd *cmd)
  548. {
  549. struct scatterlist *sgl = cmd->t_data_sg;
  550. u32 sgl_nents = cmd->t_data_nents;
  551. enum dma_data_direction data_direction = cmd->data_direction;
  552. struct se_device *dev = cmd->se_dev;
  553. struct iblock_req *ibr;
  554. struct bio *bio;
  555. struct bio_list list;
  556. struct scatterlist *sg;
  557. u32 sg_num = sgl_nents;
  558. sector_t block_lba;
  559. unsigned bio_cnt;
  560. int rw;
  561. int i;
  562. if (data_direction == DMA_TO_DEVICE) {
  563. /*
  564. * Force data to disk if we pretend to not have a volatile
  565. * write cache, or the initiator set the Force Unit Access bit.
  566. */
  567. if (dev->dev_attrib.emulate_write_cache == 0 ||
  568. (dev->dev_attrib.emulate_fua_write > 0 &&
  569. (cmd->se_cmd_flags & SCF_FUA)))
  570. rw = WRITE_FUA;
  571. else
  572. rw = WRITE;
  573. } else {
  574. rw = READ;
  575. }
  576. /*
  577. * Convert the blocksize advertised to the initiator to the 512 byte
  578. * units unconditionally used by the Linux block layer.
  579. */
  580. if (dev->dev_attrib.block_size == 4096)
  581. block_lba = (cmd->t_task_lba << 3);
  582. else if (dev->dev_attrib.block_size == 2048)
  583. block_lba = (cmd->t_task_lba << 2);
  584. else if (dev->dev_attrib.block_size == 1024)
  585. block_lba = (cmd->t_task_lba << 1);
  586. else if (dev->dev_attrib.block_size == 512)
  587. block_lba = cmd->t_task_lba;
  588. else {
  589. pr_err("Unsupported SCSI -> BLOCK LBA conversion:"
  590. " %u\n", dev->dev_attrib.block_size);
  591. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  592. }
  593. ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
  594. if (!ibr)
  595. goto fail;
  596. cmd->priv = ibr;
  597. if (!sgl_nents) {
  598. atomic_set(&ibr->pending, 1);
  599. iblock_complete_cmd(cmd);
  600. return 0;
  601. }
  602. bio = iblock_get_bio(cmd, block_lba, sgl_nents);
  603. if (!bio)
  604. goto fail_free_ibr;
  605. bio_list_init(&list);
  606. bio_list_add(&list, bio);
  607. atomic_set(&ibr->pending, 2);
  608. bio_cnt = 1;
  609. for_each_sg(sgl, sg, sgl_nents, i) {
  610. /*
  611. * XXX: if the length the device accepts is shorter than the
  612. * length of the S/G list entry this will cause and
  613. * endless loop. Better hope no driver uses huge pages.
  614. */
  615. while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
  616. != sg->length) {
  617. if (bio_cnt >= IBLOCK_MAX_BIO_PER_TASK) {
  618. iblock_submit_bios(&list, rw);
  619. bio_cnt = 0;
  620. }
  621. bio = iblock_get_bio(cmd, block_lba, sg_num);
  622. if (!bio)
  623. goto fail_put_bios;
  624. atomic_inc(&ibr->pending);
  625. bio_list_add(&list, bio);
  626. bio_cnt++;
  627. }
  628. /* Always in 512 byte units for Linux/Block */
  629. block_lba += sg->length >> IBLOCK_LBA_SHIFT;
  630. sg_num--;
  631. }
  632. iblock_submit_bios(&list, rw);
  633. iblock_complete_cmd(cmd);
  634. return 0;
  635. fail_put_bios:
  636. while ((bio = bio_list_pop(&list)))
  637. bio_put(bio);
  638. fail_free_ibr:
  639. kfree(ibr);
  640. fail:
  641. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  642. }
  643. static sector_t iblock_get_blocks(struct se_device *dev)
  644. {
  645. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  646. struct block_device *bd = ib_dev->ibd_bd;
  647. struct request_queue *q = bdev_get_queue(bd);
  648. return iblock_emulate_read_cap_with_block_size(dev, bd, q);
  649. }
  650. static struct sbc_ops iblock_sbc_ops = {
  651. .execute_rw = iblock_execute_rw,
  652. .execute_sync_cache = iblock_execute_sync_cache,
  653. .execute_write_same = iblock_execute_write_same,
  654. .execute_write_same_unmap = iblock_execute_write_same_unmap,
  655. .execute_unmap = iblock_execute_unmap,
  656. };
  657. static sense_reason_t
  658. iblock_parse_cdb(struct se_cmd *cmd)
  659. {
  660. return sbc_parse_cdb(cmd, &iblock_sbc_ops);
  661. }
  662. static struct se_subsystem_api iblock_template = {
  663. .name = "iblock",
  664. .inquiry_prod = "IBLOCK",
  665. .inquiry_rev = IBLOCK_VERSION,
  666. .owner = THIS_MODULE,
  667. .transport_type = TRANSPORT_PLUGIN_VHBA_PDEV,
  668. .attach_hba = iblock_attach_hba,
  669. .detach_hba = iblock_detach_hba,
  670. .alloc_device = iblock_alloc_device,
  671. .configure_device = iblock_configure_device,
  672. .free_device = iblock_free_device,
  673. .parse_cdb = iblock_parse_cdb,
  674. .set_configfs_dev_params = iblock_set_configfs_dev_params,
  675. .show_configfs_dev_params = iblock_show_configfs_dev_params,
  676. .get_device_type = sbc_get_device_type,
  677. .get_blocks = iblock_get_blocks,
  678. };
  679. static int __init iblock_module_init(void)
  680. {
  681. return transport_subsystem_register(&iblock_template);
  682. }
  683. static void iblock_module_exit(void)
  684. {
  685. transport_subsystem_release(&iblock_template);
  686. }
  687. MODULE_DESCRIPTION("TCM IBLOCK subsystem plugin");
  688. MODULE_AUTHOR("nab@Linux-iSCSI.org");
  689. MODULE_LICENSE("GPL");
  690. module_init(iblock_module_init);
  691. module_exit(iblock_module_exit);