target_core_iblock.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  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. /* We never set ANC_SUP */
  337. if (cmd->t_task_cdb[1])
  338. return TCM_INVALID_CDB_FIELD;
  339. if (cmd->data_length == 0) {
  340. target_complete_cmd(cmd, SAM_STAT_GOOD);
  341. return 0;
  342. }
  343. if (cmd->data_length < 8) {
  344. pr_warn("UNMAP parameter list length %u too small\n",
  345. cmd->data_length);
  346. return TCM_PARAMETER_LIST_LENGTH_ERROR;
  347. }
  348. buf = transport_kmap_data_sg(cmd);
  349. if (!buf)
  350. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  351. dl = get_unaligned_be16(&buf[0]);
  352. bd_dl = get_unaligned_be16(&buf[2]);
  353. size = cmd->data_length - 8;
  354. if (bd_dl > size)
  355. pr_warn("UNMAP parameter list length %u too small, ignoring bd_dl %u\n",
  356. cmd->data_length, bd_dl);
  357. else
  358. size = bd_dl;
  359. if (size / 16 > dev->dev_attrib.max_unmap_block_desc_count) {
  360. ret = TCM_INVALID_PARAMETER_LIST;
  361. goto err;
  362. }
  363. /* First UNMAP block descriptor starts at 8 byte offset */
  364. ptr = &buf[8];
  365. pr_debug("UNMAP: Sub: %s Using dl: %u bd_dl: %u size: %u"
  366. " ptr: %p\n", dev->transport->name, dl, bd_dl, size, ptr);
  367. while (size >= 16) {
  368. lba = get_unaligned_be64(&ptr[0]);
  369. range = get_unaligned_be32(&ptr[8]);
  370. pr_debug("UNMAP: Using lba: %llu and range: %u\n",
  371. (unsigned long long)lba, range);
  372. if (range > dev->dev_attrib.max_unmap_lba_count) {
  373. ret = TCM_INVALID_PARAMETER_LIST;
  374. goto err;
  375. }
  376. if (lba + range > dev->transport->get_blocks(dev) + 1) {
  377. ret = TCM_ADDRESS_OUT_OF_RANGE;
  378. goto err;
  379. }
  380. err = blkdev_issue_discard(ib_dev->ibd_bd, lba, range,
  381. GFP_KERNEL, 0);
  382. if (err < 0) {
  383. pr_err("blkdev_issue_discard() failed: %d\n",
  384. err);
  385. ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  386. goto err;
  387. }
  388. ptr += 16;
  389. size -= 16;
  390. }
  391. err:
  392. transport_kunmap_data_sg(cmd);
  393. if (!ret)
  394. target_complete_cmd(cmd, GOOD);
  395. return ret;
  396. }
  397. static sense_reason_t
  398. iblock_execute_write_same_unmap(struct se_cmd *cmd)
  399. {
  400. struct iblock_dev *ib_dev = IBLOCK_DEV(cmd->se_dev);
  401. int rc;
  402. rc = blkdev_issue_discard(ib_dev->ibd_bd, cmd->t_task_lba,
  403. sbc_get_write_same_sectors(cmd), GFP_KERNEL, 0);
  404. if (rc < 0) {
  405. pr_warn("blkdev_issue_discard() failed: %d\n", rc);
  406. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  407. }
  408. target_complete_cmd(cmd, GOOD);
  409. return 0;
  410. }
  411. static sense_reason_t
  412. iblock_execute_write_same(struct se_cmd *cmd)
  413. {
  414. struct iblock_req *ibr;
  415. struct scatterlist *sg;
  416. struct bio *bio;
  417. struct bio_list list;
  418. sector_t block_lba = cmd->t_task_lba;
  419. sector_t sectors = sbc_get_write_same_sectors(cmd);
  420. sg = &cmd->t_data_sg[0];
  421. if (cmd->t_data_nents > 1 ||
  422. sg->length != cmd->se_dev->dev_attrib.block_size) {
  423. pr_err("WRITE_SAME: Illegal SGL t_data_nents: %u length: %u"
  424. " block_size: %u\n", cmd->t_data_nents, sg->length,
  425. cmd->se_dev->dev_attrib.block_size);
  426. return TCM_INVALID_CDB_FIELD;
  427. }
  428. ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
  429. if (!ibr)
  430. goto fail;
  431. cmd->priv = ibr;
  432. bio = iblock_get_bio(cmd, block_lba, 1);
  433. if (!bio)
  434. goto fail_free_ibr;
  435. bio_list_init(&list);
  436. bio_list_add(&list, bio);
  437. atomic_set(&ibr->pending, 1);
  438. while (sectors) {
  439. while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
  440. != sg->length) {
  441. bio = iblock_get_bio(cmd, block_lba, 1);
  442. if (!bio)
  443. goto fail_put_bios;
  444. atomic_inc(&ibr->pending);
  445. bio_list_add(&list, bio);
  446. }
  447. /* Always in 512 byte units for Linux/Block */
  448. block_lba += sg->length >> IBLOCK_LBA_SHIFT;
  449. sectors -= 1;
  450. }
  451. iblock_submit_bios(&list, WRITE);
  452. return 0;
  453. fail_put_bios:
  454. while ((bio = bio_list_pop(&list)))
  455. bio_put(bio);
  456. fail_free_ibr:
  457. kfree(ibr);
  458. fail:
  459. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  460. }
  461. enum {
  462. Opt_udev_path, Opt_readonly, Opt_force, Opt_err
  463. };
  464. static match_table_t tokens = {
  465. {Opt_udev_path, "udev_path=%s"},
  466. {Opt_readonly, "readonly=%d"},
  467. {Opt_force, "force=%d"},
  468. {Opt_err, NULL}
  469. };
  470. static ssize_t iblock_set_configfs_dev_params(struct se_device *dev,
  471. const char *page, ssize_t count)
  472. {
  473. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  474. char *orig, *ptr, *arg_p, *opts;
  475. substring_t args[MAX_OPT_ARGS];
  476. int ret = 0, token;
  477. unsigned long tmp_readonly;
  478. opts = kstrdup(page, GFP_KERNEL);
  479. if (!opts)
  480. return -ENOMEM;
  481. orig = opts;
  482. while ((ptr = strsep(&opts, ",\n")) != NULL) {
  483. if (!*ptr)
  484. continue;
  485. token = match_token(ptr, tokens, args);
  486. switch (token) {
  487. case Opt_udev_path:
  488. if (ib_dev->ibd_bd) {
  489. pr_err("Unable to set udev_path= while"
  490. " ib_dev->ibd_bd exists\n");
  491. ret = -EEXIST;
  492. goto out;
  493. }
  494. if (match_strlcpy(ib_dev->ibd_udev_path, &args[0],
  495. SE_UDEV_PATH_LEN) == 0) {
  496. ret = -EINVAL;
  497. break;
  498. }
  499. pr_debug("IBLOCK: Referencing UDEV path: %s\n",
  500. ib_dev->ibd_udev_path);
  501. ib_dev->ibd_flags |= IBDF_HAS_UDEV_PATH;
  502. break;
  503. case Opt_readonly:
  504. arg_p = match_strdup(&args[0]);
  505. if (!arg_p) {
  506. ret = -ENOMEM;
  507. break;
  508. }
  509. ret = strict_strtoul(arg_p, 0, &tmp_readonly);
  510. kfree(arg_p);
  511. if (ret < 0) {
  512. pr_err("strict_strtoul() failed for"
  513. " readonly=\n");
  514. goto out;
  515. }
  516. ib_dev->ibd_readonly = tmp_readonly;
  517. pr_debug("IBLOCK: readonly: %d\n", ib_dev->ibd_readonly);
  518. break;
  519. case Opt_force:
  520. break;
  521. default:
  522. break;
  523. }
  524. }
  525. out:
  526. kfree(orig);
  527. return (!ret) ? count : ret;
  528. }
  529. static ssize_t iblock_show_configfs_dev_params(struct se_device *dev, char *b)
  530. {
  531. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  532. struct block_device *bd = ib_dev->ibd_bd;
  533. char buf[BDEVNAME_SIZE];
  534. ssize_t bl = 0;
  535. if (bd)
  536. bl += sprintf(b + bl, "iBlock device: %s",
  537. bdevname(bd, buf));
  538. if (ib_dev->ibd_flags & IBDF_HAS_UDEV_PATH)
  539. bl += sprintf(b + bl, " UDEV PATH: %s",
  540. ib_dev->ibd_udev_path);
  541. bl += sprintf(b + bl, " readonly: %d\n", ib_dev->ibd_readonly);
  542. bl += sprintf(b + bl, " ");
  543. if (bd) {
  544. bl += sprintf(b + bl, "Major: %d Minor: %d %s\n",
  545. MAJOR(bd->bd_dev), MINOR(bd->bd_dev), (!bd->bd_contains) ?
  546. "" : (bd->bd_holder == ib_dev) ?
  547. "CLAIMED: IBLOCK" : "CLAIMED: OS");
  548. } else {
  549. bl += sprintf(b + bl, "Major: 0 Minor: 0\n");
  550. }
  551. return bl;
  552. }
  553. static sense_reason_t
  554. iblock_execute_rw(struct se_cmd *cmd)
  555. {
  556. struct scatterlist *sgl = cmd->t_data_sg;
  557. u32 sgl_nents = cmd->t_data_nents;
  558. enum dma_data_direction data_direction = cmd->data_direction;
  559. struct se_device *dev = cmd->se_dev;
  560. struct iblock_req *ibr;
  561. struct bio *bio;
  562. struct bio_list list;
  563. struct scatterlist *sg;
  564. u32 sg_num = sgl_nents;
  565. sector_t block_lba;
  566. unsigned bio_cnt;
  567. int rw = 0;
  568. int i;
  569. if (data_direction == DMA_TO_DEVICE) {
  570. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  571. struct request_queue *q = bdev_get_queue(ib_dev->ibd_bd);
  572. /*
  573. * Force writethrough using WRITE_FUA if a volatile write cache
  574. * is not enabled, or if initiator set the Force Unit Access bit.
  575. */
  576. if (q->flush_flags & REQ_FUA) {
  577. if (cmd->se_cmd_flags & SCF_FUA)
  578. rw = WRITE_FUA;
  579. else if (!(q->flush_flags & REQ_FLUSH))
  580. rw = WRITE_FUA;
  581. } else {
  582. rw = WRITE;
  583. }
  584. } else {
  585. rw = READ;
  586. }
  587. /*
  588. * Convert the blocksize advertised to the initiator to the 512 byte
  589. * units unconditionally used by the Linux block layer.
  590. */
  591. if (dev->dev_attrib.block_size == 4096)
  592. block_lba = (cmd->t_task_lba << 3);
  593. else if (dev->dev_attrib.block_size == 2048)
  594. block_lba = (cmd->t_task_lba << 2);
  595. else if (dev->dev_attrib.block_size == 1024)
  596. block_lba = (cmd->t_task_lba << 1);
  597. else if (dev->dev_attrib.block_size == 512)
  598. block_lba = cmd->t_task_lba;
  599. else {
  600. pr_err("Unsupported SCSI -> BLOCK LBA conversion:"
  601. " %u\n", dev->dev_attrib.block_size);
  602. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  603. }
  604. ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
  605. if (!ibr)
  606. goto fail;
  607. cmd->priv = ibr;
  608. if (!sgl_nents) {
  609. atomic_set(&ibr->pending, 1);
  610. iblock_complete_cmd(cmd);
  611. return 0;
  612. }
  613. bio = iblock_get_bio(cmd, block_lba, sgl_nents);
  614. if (!bio)
  615. goto fail_free_ibr;
  616. bio_list_init(&list);
  617. bio_list_add(&list, bio);
  618. atomic_set(&ibr->pending, 2);
  619. bio_cnt = 1;
  620. for_each_sg(sgl, sg, sgl_nents, i) {
  621. /*
  622. * XXX: if the length the device accepts is shorter than the
  623. * length of the S/G list entry this will cause and
  624. * endless loop. Better hope no driver uses huge pages.
  625. */
  626. while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
  627. != sg->length) {
  628. if (bio_cnt >= IBLOCK_MAX_BIO_PER_TASK) {
  629. iblock_submit_bios(&list, rw);
  630. bio_cnt = 0;
  631. }
  632. bio = iblock_get_bio(cmd, block_lba, sg_num);
  633. if (!bio)
  634. goto fail_put_bios;
  635. atomic_inc(&ibr->pending);
  636. bio_list_add(&list, bio);
  637. bio_cnt++;
  638. }
  639. /* Always in 512 byte units for Linux/Block */
  640. block_lba += sg->length >> IBLOCK_LBA_SHIFT;
  641. sg_num--;
  642. }
  643. iblock_submit_bios(&list, rw);
  644. iblock_complete_cmd(cmd);
  645. return 0;
  646. fail_put_bios:
  647. while ((bio = bio_list_pop(&list)))
  648. bio_put(bio);
  649. fail_free_ibr:
  650. kfree(ibr);
  651. fail:
  652. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  653. }
  654. static sector_t iblock_get_blocks(struct se_device *dev)
  655. {
  656. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  657. struct block_device *bd = ib_dev->ibd_bd;
  658. struct request_queue *q = bdev_get_queue(bd);
  659. return iblock_emulate_read_cap_with_block_size(dev, bd, q);
  660. }
  661. static struct sbc_ops iblock_sbc_ops = {
  662. .execute_rw = iblock_execute_rw,
  663. .execute_sync_cache = iblock_execute_sync_cache,
  664. .execute_write_same = iblock_execute_write_same,
  665. .execute_write_same_unmap = iblock_execute_write_same_unmap,
  666. .execute_unmap = iblock_execute_unmap,
  667. };
  668. static sense_reason_t
  669. iblock_parse_cdb(struct se_cmd *cmd)
  670. {
  671. return sbc_parse_cdb(cmd, &iblock_sbc_ops);
  672. }
  673. bool iblock_get_write_cache(struct se_device *dev)
  674. {
  675. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  676. struct block_device *bd = ib_dev->ibd_bd;
  677. struct request_queue *q = bdev_get_queue(bd);
  678. return q->flush_flags & REQ_FLUSH;
  679. }
  680. static struct se_subsystem_api iblock_template = {
  681. .name = "iblock",
  682. .inquiry_prod = "IBLOCK",
  683. .inquiry_rev = IBLOCK_VERSION,
  684. .owner = THIS_MODULE,
  685. .transport_type = TRANSPORT_PLUGIN_VHBA_PDEV,
  686. .attach_hba = iblock_attach_hba,
  687. .detach_hba = iblock_detach_hba,
  688. .alloc_device = iblock_alloc_device,
  689. .configure_device = iblock_configure_device,
  690. .free_device = iblock_free_device,
  691. .parse_cdb = iblock_parse_cdb,
  692. .set_configfs_dev_params = iblock_set_configfs_dev_params,
  693. .show_configfs_dev_params = iblock_show_configfs_dev_params,
  694. .get_device_type = sbc_get_device_type,
  695. .get_blocks = iblock_get_blocks,
  696. .get_write_cache = iblock_get_write_cache,
  697. };
  698. static int __init iblock_module_init(void)
  699. {
  700. return transport_subsystem_register(&iblock_template);
  701. }
  702. static void __exit iblock_module_exit(void)
  703. {
  704. transport_subsystem_release(&iblock_template);
  705. }
  706. MODULE_DESCRIPTION("TCM IBLOCK subsystem plugin");
  707. MODULE_AUTHOR("nab@Linux-iSCSI.org");
  708. MODULE_LICENSE("GPL");
  709. module_init(iblock_module_init);
  710. module_exit(iblock_module_exit);