target_core_iblock.c 19 KB

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