target_core_iblock.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. /*******************************************************************************
  2. * Filename: target_core_iblock.c
  3. *
  4. * This file contains the Storage Engine <-> Linux BlockIO transport
  5. * specific functions.
  6. *
  7. * Copyright (c) 2003, 2004, 2005 PyX Technologies, Inc.
  8. * Copyright (c) 2005, 2006, 2007 SBE, Inc.
  9. * Copyright (c) 2007-2010 Rising Tide Systems
  10. * Copyright (c) 2008-2010 Linux-iSCSI.org
  11. *
  12. * Nicholas A. Bellinger <nab@kernel.org>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  27. *
  28. ******************************************************************************/
  29. #include <linux/string.h>
  30. #include <linux/parser.h>
  31. #include <linux/timer.h>
  32. #include <linux/fs.h>
  33. #include <linux/blkdev.h>
  34. #include <linux/slab.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/bio.h>
  37. #include <linux/genhd.h>
  38. #include <linux/file.h>
  39. #include <linux/module.h>
  40. #include <scsi/scsi.h>
  41. #include <scsi/scsi_host.h>
  42. #include <asm/unaligned.h>
  43. #include <target/target_core_base.h>
  44. #include <target/target_core_backend.h>
  45. #include "target_core_iblock.h"
  46. #define IBLOCK_MAX_BIO_PER_TASK 32 /* max # of bios to submit at a time */
  47. #define IBLOCK_BIO_POOL_SIZE 128
  48. static struct se_subsystem_api iblock_template;
  49. static void iblock_bio_done(struct bio *, int);
  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 void *iblock_allocate_virtdevice(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;
  74. }
  75. static struct se_device *iblock_create_virtdevice(
  76. struct se_hba *hba,
  77. struct se_subsystem_dev *se_dev,
  78. void *p)
  79. {
  80. struct iblock_dev *ib_dev = p;
  81. struct se_device *dev;
  82. struct se_dev_limits dev_limits;
  83. struct block_device *bd = NULL;
  84. struct request_queue *q;
  85. struct queue_limits *limits;
  86. u32 dev_flags = 0;
  87. fmode_t mode;
  88. int ret = -EINVAL;
  89. if (!ib_dev) {
  90. pr_err("Unable to locate struct iblock_dev parameter\n");
  91. return ERR_PTR(ret);
  92. }
  93. memset(&dev_limits, 0, sizeof(struct se_dev_limits));
  94. ib_dev->ibd_bio_set = bioset_create(IBLOCK_BIO_POOL_SIZE, 0);
  95. if (!ib_dev->ibd_bio_set) {
  96. pr_err("IBLOCK: Unable to create bioset()\n");
  97. return ERR_PTR(-ENOMEM);
  98. }
  99. pr_debug("IBLOCK: Created bio_set()\n");
  100. /*
  101. * iblock_check_configfs_dev_params() ensures that ib_dev->ibd_udev_path
  102. * must already have been set in order for echo 1 > $HBA/$DEV/enable to run.
  103. */
  104. pr_debug( "IBLOCK: Claiming struct block_device: %s\n",
  105. ib_dev->ibd_udev_path);
  106. mode = FMODE_READ|FMODE_EXCL;
  107. if (!ib_dev->ibd_readonly)
  108. mode |= FMODE_WRITE;
  109. bd = blkdev_get_by_path(ib_dev->ibd_udev_path, mode, ib_dev);
  110. if (IS_ERR(bd)) {
  111. ret = PTR_ERR(bd);
  112. goto failed;
  113. }
  114. /*
  115. * Setup the local scope queue_limits from struct request_queue->limits
  116. * to pass into transport_add_device_to_core_hba() as struct se_dev_limits.
  117. */
  118. q = bdev_get_queue(bd);
  119. limits = &dev_limits.limits;
  120. limits->logical_block_size = bdev_logical_block_size(bd);
  121. limits->max_hw_sectors = UINT_MAX;
  122. limits->max_sectors = UINT_MAX;
  123. dev_limits.hw_queue_depth = q->nr_requests;
  124. dev_limits.queue_depth = q->nr_requests;
  125. ib_dev->ibd_bd = bd;
  126. dev = transport_add_device_to_core_hba(hba,
  127. &iblock_template, se_dev, dev_flags, ib_dev,
  128. &dev_limits, "IBLOCK", IBLOCK_VERSION);
  129. if (!dev)
  130. goto failed;
  131. /*
  132. * Check if the underlying struct block_device request_queue supports
  133. * the QUEUE_FLAG_DISCARD bit for UNMAP/WRITE_SAME in SCSI + TRIM
  134. * in ATA and we need to set TPE=1
  135. */
  136. if (blk_queue_discard(q)) {
  137. dev->se_sub_dev->se_dev_attrib.max_unmap_lba_count =
  138. q->limits.max_discard_sectors;
  139. /*
  140. * Currently hardcoded to 1 in Linux/SCSI code..
  141. */
  142. dev->se_sub_dev->se_dev_attrib.max_unmap_block_desc_count = 1;
  143. dev->se_sub_dev->se_dev_attrib.unmap_granularity =
  144. q->limits.discard_granularity >> 9;
  145. dev->se_sub_dev->se_dev_attrib.unmap_granularity_alignment =
  146. q->limits.discard_alignment;
  147. pr_debug("IBLOCK: BLOCK Discard support available,"
  148. " disabled by default\n");
  149. }
  150. if (blk_queue_nonrot(q))
  151. dev->se_sub_dev->se_dev_attrib.is_nonrot = 1;
  152. return dev;
  153. failed:
  154. if (ib_dev->ibd_bio_set) {
  155. bioset_free(ib_dev->ibd_bio_set);
  156. ib_dev->ibd_bio_set = NULL;
  157. }
  158. ib_dev->ibd_bd = NULL;
  159. return ERR_PTR(ret);
  160. }
  161. static void iblock_free_device(void *p)
  162. {
  163. struct iblock_dev *ib_dev = p;
  164. if (ib_dev->ibd_bd != NULL)
  165. blkdev_put(ib_dev->ibd_bd, FMODE_WRITE|FMODE_READ|FMODE_EXCL);
  166. if (ib_dev->ibd_bio_set != NULL)
  167. bioset_free(ib_dev->ibd_bio_set);
  168. kfree(ib_dev);
  169. }
  170. static unsigned long long iblock_emulate_read_cap_with_block_size(
  171. struct se_device *dev,
  172. struct block_device *bd,
  173. struct request_queue *q)
  174. {
  175. unsigned long long blocks_long = (div_u64(i_size_read(bd->bd_inode),
  176. bdev_logical_block_size(bd)) - 1);
  177. u32 block_size = bdev_logical_block_size(bd);
  178. if (block_size == dev->se_sub_dev->se_dev_attrib.block_size)
  179. return blocks_long;
  180. switch (block_size) {
  181. case 4096:
  182. switch (dev->se_sub_dev->se_dev_attrib.block_size) {
  183. case 2048:
  184. blocks_long <<= 1;
  185. break;
  186. case 1024:
  187. blocks_long <<= 2;
  188. break;
  189. case 512:
  190. blocks_long <<= 3;
  191. default:
  192. break;
  193. }
  194. break;
  195. case 2048:
  196. switch (dev->se_sub_dev->se_dev_attrib.block_size) {
  197. case 4096:
  198. blocks_long >>= 1;
  199. break;
  200. case 1024:
  201. blocks_long <<= 1;
  202. break;
  203. case 512:
  204. blocks_long <<= 2;
  205. break;
  206. default:
  207. break;
  208. }
  209. break;
  210. case 1024:
  211. switch (dev->se_sub_dev->se_dev_attrib.block_size) {
  212. case 4096:
  213. blocks_long >>= 2;
  214. break;
  215. case 2048:
  216. blocks_long >>= 1;
  217. break;
  218. case 512:
  219. blocks_long <<= 1;
  220. break;
  221. default:
  222. break;
  223. }
  224. break;
  225. case 512:
  226. switch (dev->se_sub_dev->se_dev_attrib.block_size) {
  227. case 4096:
  228. blocks_long >>= 3;
  229. break;
  230. case 2048:
  231. blocks_long >>= 2;
  232. break;
  233. case 1024:
  234. blocks_long >>= 1;
  235. break;
  236. default:
  237. break;
  238. }
  239. break;
  240. default:
  241. break;
  242. }
  243. return blocks_long;
  244. }
  245. static void iblock_end_io_flush(struct bio *bio, int err)
  246. {
  247. struct se_cmd *cmd = bio->bi_private;
  248. if (err)
  249. pr_err("IBLOCK: cache flush failed: %d\n", err);
  250. if (cmd) {
  251. if (err) {
  252. cmd->scsi_sense_reason =
  253. TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  254. target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
  255. } else {
  256. target_complete_cmd(cmd, SAM_STAT_GOOD);
  257. }
  258. }
  259. bio_put(bio);
  260. }
  261. /*
  262. * Implement SYCHRONIZE CACHE. Note that we can't handle lba ranges and must
  263. * always flush the whole cache.
  264. */
  265. static int iblock_execute_sync_cache(struct se_cmd *cmd)
  266. {
  267. struct iblock_dev *ib_dev = cmd->se_dev->dev_ptr;
  268. int immed = (cmd->t_task_cdb[1] & 0x2);
  269. struct bio *bio;
  270. /*
  271. * If the Immediate bit is set, queue up the GOOD response
  272. * for this SYNCHRONIZE_CACHE op.
  273. */
  274. if (immed)
  275. target_complete_cmd(cmd, SAM_STAT_GOOD);
  276. bio = bio_alloc(GFP_KERNEL, 0);
  277. bio->bi_end_io = iblock_end_io_flush;
  278. bio->bi_bdev = ib_dev->ibd_bd;
  279. if (!immed)
  280. bio->bi_private = cmd;
  281. submit_bio(WRITE_FLUSH, bio);
  282. return 0;
  283. }
  284. static int iblock_execute_unmap(struct se_cmd *cmd)
  285. {
  286. struct se_device *dev = cmd->se_dev;
  287. struct iblock_dev *ibd = dev->dev_ptr;
  288. unsigned char *buf, *ptr = NULL;
  289. sector_t lba;
  290. int size = cmd->data_length;
  291. u32 range;
  292. int ret = 0;
  293. int dl, bd_dl;
  294. buf = transport_kmap_data_sg(cmd);
  295. dl = get_unaligned_be16(&buf[0]);
  296. bd_dl = get_unaligned_be16(&buf[2]);
  297. size = min(size - 8, bd_dl);
  298. if (size / 16 > dev->se_sub_dev->se_dev_attrib.max_unmap_block_desc_count) {
  299. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  300. ret = -EINVAL;
  301. goto err;
  302. }
  303. /* First UNMAP block descriptor starts at 8 byte offset */
  304. ptr = &buf[8];
  305. pr_debug("UNMAP: Sub: %s Using dl: %u bd_dl: %u size: %u"
  306. " ptr: %p\n", dev->transport->name, dl, bd_dl, size, ptr);
  307. while (size >= 16) {
  308. lba = get_unaligned_be64(&ptr[0]);
  309. range = get_unaligned_be32(&ptr[8]);
  310. pr_debug("UNMAP: Using lba: %llu and range: %u\n",
  311. (unsigned long long)lba, range);
  312. if (range > dev->se_sub_dev->se_dev_attrib.max_unmap_lba_count) {
  313. cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  314. ret = -EINVAL;
  315. goto err;
  316. }
  317. if (lba + range > dev->transport->get_blocks(dev) + 1) {
  318. cmd->scsi_sense_reason = TCM_ADDRESS_OUT_OF_RANGE;
  319. ret = -EINVAL;
  320. goto err;
  321. }
  322. ret = blkdev_issue_discard(ibd->ibd_bd, lba, range,
  323. GFP_KERNEL, 0);
  324. if (ret < 0) {
  325. pr_err("blkdev_issue_discard() failed: %d\n",
  326. ret);
  327. goto err;
  328. }
  329. ptr += 16;
  330. size -= 16;
  331. }
  332. err:
  333. transport_kunmap_data_sg(cmd);
  334. if (!ret)
  335. target_complete_cmd(cmd, GOOD);
  336. return ret;
  337. }
  338. static int iblock_execute_write_same(struct se_cmd *cmd)
  339. {
  340. struct iblock_dev *ibd = cmd->se_dev->dev_ptr;
  341. int ret;
  342. ret = blkdev_issue_discard(ibd->ibd_bd, cmd->t_task_lba,
  343. spc_get_write_same_sectors(cmd), GFP_KERNEL,
  344. 0);
  345. if (ret < 0) {
  346. pr_debug("blkdev_issue_discard() failed for WRITE_SAME\n");
  347. return ret;
  348. }
  349. target_complete_cmd(cmd, GOOD);
  350. return 0;
  351. }
  352. enum {
  353. Opt_udev_path, Opt_readonly, Opt_force, Opt_err
  354. };
  355. static match_table_t tokens = {
  356. {Opt_udev_path, "udev_path=%s"},
  357. {Opt_readonly, "readonly=%d"},
  358. {Opt_force, "force=%d"},
  359. {Opt_err, NULL}
  360. };
  361. static ssize_t iblock_set_configfs_dev_params(struct se_hba *hba,
  362. struct se_subsystem_dev *se_dev,
  363. const char *page, ssize_t count)
  364. {
  365. struct iblock_dev *ib_dev = se_dev->se_dev_su_ptr;
  366. char *orig, *ptr, *arg_p, *opts;
  367. substring_t args[MAX_OPT_ARGS];
  368. int ret = 0, token;
  369. unsigned long tmp_readonly;
  370. opts = kstrdup(page, GFP_KERNEL);
  371. if (!opts)
  372. return -ENOMEM;
  373. orig = opts;
  374. while ((ptr = strsep(&opts, ",\n")) != NULL) {
  375. if (!*ptr)
  376. continue;
  377. token = match_token(ptr, tokens, args);
  378. switch (token) {
  379. case Opt_udev_path:
  380. if (ib_dev->ibd_bd) {
  381. pr_err("Unable to set udev_path= while"
  382. " ib_dev->ibd_bd exists\n");
  383. ret = -EEXIST;
  384. goto out;
  385. }
  386. arg_p = match_strdup(&args[0]);
  387. if (!arg_p) {
  388. ret = -ENOMEM;
  389. break;
  390. }
  391. snprintf(ib_dev->ibd_udev_path, SE_UDEV_PATH_LEN,
  392. "%s", arg_p);
  393. kfree(arg_p);
  394. pr_debug("IBLOCK: Referencing UDEV path: %s\n",
  395. ib_dev->ibd_udev_path);
  396. ib_dev->ibd_flags |= IBDF_HAS_UDEV_PATH;
  397. break;
  398. case Opt_readonly:
  399. arg_p = match_strdup(&args[0]);
  400. if (!arg_p) {
  401. ret = -ENOMEM;
  402. break;
  403. }
  404. ret = strict_strtoul(arg_p, 0, &tmp_readonly);
  405. kfree(arg_p);
  406. if (ret < 0) {
  407. pr_err("strict_strtoul() failed for"
  408. " readonly=\n");
  409. goto out;
  410. }
  411. ib_dev->ibd_readonly = tmp_readonly;
  412. pr_debug("IBLOCK: readonly: %d\n", ib_dev->ibd_readonly);
  413. break;
  414. case Opt_force:
  415. break;
  416. default:
  417. break;
  418. }
  419. }
  420. out:
  421. kfree(orig);
  422. return (!ret) ? count : ret;
  423. }
  424. static ssize_t iblock_check_configfs_dev_params(
  425. struct se_hba *hba,
  426. struct se_subsystem_dev *se_dev)
  427. {
  428. struct iblock_dev *ibd = se_dev->se_dev_su_ptr;
  429. if (!(ibd->ibd_flags & IBDF_HAS_UDEV_PATH)) {
  430. pr_err("Missing udev_path= parameters for IBLOCK\n");
  431. return -EINVAL;
  432. }
  433. return 0;
  434. }
  435. static ssize_t iblock_show_configfs_dev_params(
  436. struct se_hba *hba,
  437. struct se_subsystem_dev *se_dev,
  438. char *b)
  439. {
  440. struct iblock_dev *ibd = se_dev->se_dev_su_ptr;
  441. struct block_device *bd = ibd->ibd_bd;
  442. char buf[BDEVNAME_SIZE];
  443. ssize_t bl = 0;
  444. if (bd)
  445. bl += sprintf(b + bl, "iBlock device: %s",
  446. bdevname(bd, buf));
  447. if (ibd->ibd_flags & IBDF_HAS_UDEV_PATH)
  448. bl += sprintf(b + bl, " UDEV PATH: %s",
  449. ibd->ibd_udev_path);
  450. bl += sprintf(b + bl, " readonly: %d\n", ibd->ibd_readonly);
  451. bl += sprintf(b + bl, " ");
  452. if (bd) {
  453. bl += sprintf(b + bl, "Major: %d Minor: %d %s\n",
  454. MAJOR(bd->bd_dev), MINOR(bd->bd_dev), (!bd->bd_contains) ?
  455. "" : (bd->bd_holder == ibd) ?
  456. "CLAIMED: IBLOCK" : "CLAIMED: OS");
  457. } else {
  458. bl += sprintf(b + bl, "Major: 0 Minor: 0\n");
  459. }
  460. return bl;
  461. }
  462. static void iblock_complete_cmd(struct se_cmd *cmd)
  463. {
  464. struct iblock_req *ibr = cmd->priv;
  465. u8 status;
  466. if (!atomic_dec_and_test(&ibr->pending))
  467. return;
  468. if (atomic_read(&ibr->ib_bio_err_cnt))
  469. status = SAM_STAT_CHECK_CONDITION;
  470. else
  471. status = SAM_STAT_GOOD;
  472. target_complete_cmd(cmd, status);
  473. kfree(ibr);
  474. }
  475. static void iblock_bio_destructor(struct bio *bio)
  476. {
  477. struct se_cmd *cmd = bio->bi_private;
  478. struct iblock_dev *ib_dev = cmd->se_dev->dev_ptr;
  479. bio_free(bio, ib_dev->ibd_bio_set);
  480. }
  481. static struct bio *
  482. iblock_get_bio(struct se_cmd *cmd, sector_t lba, u32 sg_num)
  483. {
  484. struct iblock_dev *ib_dev = cmd->se_dev->dev_ptr;
  485. struct bio *bio;
  486. /*
  487. * Only allocate as many vector entries as the bio code allows us to,
  488. * we'll loop later on until we have handled the whole request.
  489. */
  490. if (sg_num > BIO_MAX_PAGES)
  491. sg_num = BIO_MAX_PAGES;
  492. bio = bio_alloc_bioset(GFP_NOIO, sg_num, ib_dev->ibd_bio_set);
  493. if (!bio) {
  494. pr_err("Unable to allocate memory for bio\n");
  495. return NULL;
  496. }
  497. bio->bi_bdev = ib_dev->ibd_bd;
  498. bio->bi_private = cmd;
  499. bio->bi_destructor = iblock_bio_destructor;
  500. bio->bi_end_io = &iblock_bio_done;
  501. bio->bi_sector = lba;
  502. return bio;
  503. }
  504. static void iblock_submit_bios(struct bio_list *list, int rw)
  505. {
  506. struct blk_plug plug;
  507. struct bio *bio;
  508. blk_start_plug(&plug);
  509. while ((bio = bio_list_pop(list)))
  510. submit_bio(rw, bio);
  511. blk_finish_plug(&plug);
  512. }
  513. static int iblock_execute_rw(struct se_cmd *cmd)
  514. {
  515. struct scatterlist *sgl = cmd->t_data_sg;
  516. u32 sgl_nents = cmd->t_data_nents;
  517. enum dma_data_direction data_direction = cmd->data_direction;
  518. struct se_device *dev = cmd->se_dev;
  519. struct iblock_req *ibr;
  520. struct bio *bio;
  521. struct bio_list list;
  522. struct scatterlist *sg;
  523. u32 sg_num = sgl_nents;
  524. sector_t block_lba;
  525. unsigned bio_cnt;
  526. int rw;
  527. int i;
  528. if (data_direction == DMA_TO_DEVICE) {
  529. /*
  530. * Force data to disk if we pretend to not have a volatile
  531. * write cache, or the initiator set the Force Unit Access bit.
  532. */
  533. if (dev->se_sub_dev->se_dev_attrib.emulate_write_cache == 0 ||
  534. (dev->se_sub_dev->se_dev_attrib.emulate_fua_write > 0 &&
  535. (cmd->se_cmd_flags & SCF_FUA)))
  536. rw = WRITE_FUA;
  537. else
  538. rw = WRITE;
  539. } else {
  540. rw = READ;
  541. }
  542. /*
  543. * Convert the blocksize advertised to the initiator to the 512 byte
  544. * units unconditionally used by the Linux block layer.
  545. */
  546. if (dev->se_sub_dev->se_dev_attrib.block_size == 4096)
  547. block_lba = (cmd->t_task_lba << 3);
  548. else if (dev->se_sub_dev->se_dev_attrib.block_size == 2048)
  549. block_lba = (cmd->t_task_lba << 2);
  550. else if (dev->se_sub_dev->se_dev_attrib.block_size == 1024)
  551. block_lba = (cmd->t_task_lba << 1);
  552. else if (dev->se_sub_dev->se_dev_attrib.block_size == 512)
  553. block_lba = cmd->t_task_lba;
  554. else {
  555. pr_err("Unsupported SCSI -> BLOCK LBA conversion:"
  556. " %u\n", dev->se_sub_dev->se_dev_attrib.block_size);
  557. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  558. return -ENOSYS;
  559. }
  560. ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
  561. if (!ibr)
  562. goto fail;
  563. cmd->priv = ibr;
  564. bio = iblock_get_bio(cmd, block_lba, sgl_nents);
  565. if (!bio)
  566. goto fail_free_ibr;
  567. bio_list_init(&list);
  568. bio_list_add(&list, bio);
  569. atomic_set(&ibr->pending, 2);
  570. bio_cnt = 1;
  571. for_each_sg(sgl, sg, sgl_nents, i) {
  572. /*
  573. * XXX: if the length the device accepts is shorter than the
  574. * length of the S/G list entry this will cause and
  575. * endless loop. Better hope no driver uses huge pages.
  576. */
  577. while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
  578. != sg->length) {
  579. if (bio_cnt >= IBLOCK_MAX_BIO_PER_TASK) {
  580. iblock_submit_bios(&list, rw);
  581. bio_cnt = 0;
  582. }
  583. bio = iblock_get_bio(cmd, block_lba, sg_num);
  584. if (!bio)
  585. goto fail_put_bios;
  586. atomic_inc(&ibr->pending);
  587. bio_list_add(&list, bio);
  588. bio_cnt++;
  589. }
  590. /* Always in 512 byte units for Linux/Block */
  591. block_lba += sg->length >> IBLOCK_LBA_SHIFT;
  592. sg_num--;
  593. }
  594. iblock_submit_bios(&list, rw);
  595. iblock_complete_cmd(cmd);
  596. return 0;
  597. fail_put_bios:
  598. while ((bio = bio_list_pop(&list)))
  599. bio_put(bio);
  600. fail_free_ibr:
  601. kfree(ibr);
  602. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  603. fail:
  604. return -ENOMEM;
  605. }
  606. static u32 iblock_get_device_rev(struct se_device *dev)
  607. {
  608. return SCSI_SPC_2; /* Returns SPC-3 in Initiator Data */
  609. }
  610. static u32 iblock_get_device_type(struct se_device *dev)
  611. {
  612. return TYPE_DISK;
  613. }
  614. static sector_t iblock_get_blocks(struct se_device *dev)
  615. {
  616. struct iblock_dev *ibd = dev->dev_ptr;
  617. struct block_device *bd = ibd->ibd_bd;
  618. struct request_queue *q = bdev_get_queue(bd);
  619. return iblock_emulate_read_cap_with_block_size(dev, bd, q);
  620. }
  621. static void iblock_bio_done(struct bio *bio, int err)
  622. {
  623. struct se_cmd *cmd = bio->bi_private;
  624. struct iblock_req *ibr = cmd->priv;
  625. /*
  626. * Set -EIO if !BIO_UPTODATE and the passed is still err=0
  627. */
  628. if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && !err)
  629. err = -EIO;
  630. if (err != 0) {
  631. pr_err("test_bit(BIO_UPTODATE) failed for bio: %p,"
  632. " err: %d\n", bio, err);
  633. /*
  634. * Bump the ib_bio_err_cnt and release bio.
  635. */
  636. atomic_inc(&ibr->ib_bio_err_cnt);
  637. smp_mb__after_atomic_inc();
  638. }
  639. bio_put(bio);
  640. iblock_complete_cmd(cmd);
  641. }
  642. static struct spc_ops iblock_spc_ops = {
  643. .execute_rw = iblock_execute_rw,
  644. .execute_sync_cache = iblock_execute_sync_cache,
  645. .execute_write_same = iblock_execute_write_same,
  646. .execute_unmap = iblock_execute_unmap,
  647. };
  648. static int iblock_parse_cdb(struct se_cmd *cmd)
  649. {
  650. return sbc_parse_cdb(cmd, &iblock_spc_ops);
  651. }
  652. static struct se_subsystem_api iblock_template = {
  653. .name = "iblock",
  654. .owner = THIS_MODULE,
  655. .transport_type = TRANSPORT_PLUGIN_VHBA_PDEV,
  656. .write_cache_emulated = 1,
  657. .fua_write_emulated = 1,
  658. .attach_hba = iblock_attach_hba,
  659. .detach_hba = iblock_detach_hba,
  660. .allocate_virtdevice = iblock_allocate_virtdevice,
  661. .create_virtdevice = iblock_create_virtdevice,
  662. .free_device = iblock_free_device,
  663. .parse_cdb = iblock_parse_cdb,
  664. .check_configfs_dev_params = iblock_check_configfs_dev_params,
  665. .set_configfs_dev_params = iblock_set_configfs_dev_params,
  666. .show_configfs_dev_params = iblock_show_configfs_dev_params,
  667. .get_device_rev = iblock_get_device_rev,
  668. .get_device_type = iblock_get_device_type,
  669. .get_blocks = iblock_get_blocks,
  670. };
  671. static int __init iblock_module_init(void)
  672. {
  673. return transport_subsystem_register(&iblock_template);
  674. }
  675. static void iblock_module_exit(void)
  676. {
  677. transport_subsystem_release(&iblock_template);
  678. }
  679. MODULE_DESCRIPTION("TCM IBLOCK subsystem plugin");
  680. MODULE_AUTHOR("nab@Linux-iSCSI.org");
  681. MODULE_LICENSE("GPL");
  682. module_init(iblock_module_init);
  683. module_exit(iblock_module_exit);