target_core_iblock.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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. unsigned char *cdb = &cmd->t_task_cdb[0];
  290. sector_t lba;
  291. unsigned int size = cmd->data_length, range;
  292. int ret = 0, offset;
  293. unsigned short dl, bd_dl;
  294. /* First UNMAP block descriptor starts at 8 byte offset */
  295. offset = 8;
  296. size -= 8;
  297. dl = get_unaligned_be16(&cdb[0]);
  298. bd_dl = get_unaligned_be16(&cdb[2]);
  299. buf = transport_kmap_data_sg(cmd);
  300. ptr = &buf[offset];
  301. pr_debug("UNMAP: Sub: %s Using dl: %hu bd_dl: %hu size: %hu"
  302. " ptr: %p\n", dev->transport->name, dl, bd_dl, size, ptr);
  303. while (size) {
  304. lba = get_unaligned_be64(&ptr[0]);
  305. range = get_unaligned_be32(&ptr[8]);
  306. pr_debug("UNMAP: Using lba: %llu and range: %u\n",
  307. (unsigned long long)lba, range);
  308. ret = blkdev_issue_discard(ibd->ibd_bd, lba, range,
  309. GFP_KERNEL, 0);
  310. if (ret < 0) {
  311. pr_err("blkdev_issue_discard() failed: %d\n",
  312. ret);
  313. goto err;
  314. }
  315. ptr += 16;
  316. size -= 16;
  317. }
  318. err:
  319. transport_kunmap_data_sg(cmd);
  320. if (!ret)
  321. target_complete_cmd(cmd, GOOD);
  322. return ret;
  323. }
  324. static int iblock_execute_write_same(struct se_cmd *cmd)
  325. {
  326. struct iblock_dev *ibd = cmd->se_dev->dev_ptr;
  327. int ret;
  328. ret = blkdev_issue_discard(ibd->ibd_bd, cmd->t_task_lba,
  329. spc_get_write_same_sectors(cmd), GFP_KERNEL,
  330. 0);
  331. if (ret < 0) {
  332. pr_debug("blkdev_issue_discard() failed for WRITE_SAME\n");
  333. return ret;
  334. }
  335. target_complete_cmd(cmd, GOOD);
  336. return 0;
  337. }
  338. enum {
  339. Opt_udev_path, Opt_readonly, Opt_force, Opt_err
  340. };
  341. static match_table_t tokens = {
  342. {Opt_udev_path, "udev_path=%s"},
  343. {Opt_readonly, "readonly=%d"},
  344. {Opt_force, "force=%d"},
  345. {Opt_err, NULL}
  346. };
  347. static ssize_t iblock_set_configfs_dev_params(struct se_hba *hba,
  348. struct se_subsystem_dev *se_dev,
  349. const char *page, ssize_t count)
  350. {
  351. struct iblock_dev *ib_dev = se_dev->se_dev_su_ptr;
  352. char *orig, *ptr, *arg_p, *opts;
  353. substring_t args[MAX_OPT_ARGS];
  354. int ret = 0, token;
  355. unsigned long tmp_readonly;
  356. opts = kstrdup(page, GFP_KERNEL);
  357. if (!opts)
  358. return -ENOMEM;
  359. orig = opts;
  360. while ((ptr = strsep(&opts, ",\n")) != NULL) {
  361. if (!*ptr)
  362. continue;
  363. token = match_token(ptr, tokens, args);
  364. switch (token) {
  365. case Opt_udev_path:
  366. if (ib_dev->ibd_bd) {
  367. pr_err("Unable to set udev_path= while"
  368. " ib_dev->ibd_bd exists\n");
  369. ret = -EEXIST;
  370. goto out;
  371. }
  372. arg_p = match_strdup(&args[0]);
  373. if (!arg_p) {
  374. ret = -ENOMEM;
  375. break;
  376. }
  377. snprintf(ib_dev->ibd_udev_path, SE_UDEV_PATH_LEN,
  378. "%s", arg_p);
  379. kfree(arg_p);
  380. pr_debug("IBLOCK: Referencing UDEV path: %s\n",
  381. ib_dev->ibd_udev_path);
  382. ib_dev->ibd_flags |= IBDF_HAS_UDEV_PATH;
  383. break;
  384. case Opt_readonly:
  385. arg_p = match_strdup(&args[0]);
  386. if (!arg_p) {
  387. ret = -ENOMEM;
  388. break;
  389. }
  390. ret = strict_strtoul(arg_p, 0, &tmp_readonly);
  391. kfree(arg_p);
  392. if (ret < 0) {
  393. pr_err("strict_strtoul() failed for"
  394. " readonly=\n");
  395. goto out;
  396. }
  397. ib_dev->ibd_readonly = tmp_readonly;
  398. pr_debug("IBLOCK: readonly: %d\n", ib_dev->ibd_readonly);
  399. break;
  400. case Opt_force:
  401. break;
  402. default:
  403. break;
  404. }
  405. }
  406. out:
  407. kfree(orig);
  408. return (!ret) ? count : ret;
  409. }
  410. static ssize_t iblock_check_configfs_dev_params(
  411. struct se_hba *hba,
  412. struct se_subsystem_dev *se_dev)
  413. {
  414. struct iblock_dev *ibd = se_dev->se_dev_su_ptr;
  415. if (!(ibd->ibd_flags & IBDF_HAS_UDEV_PATH)) {
  416. pr_err("Missing udev_path= parameters for IBLOCK\n");
  417. return -EINVAL;
  418. }
  419. return 0;
  420. }
  421. static ssize_t iblock_show_configfs_dev_params(
  422. struct se_hba *hba,
  423. struct se_subsystem_dev *se_dev,
  424. char *b)
  425. {
  426. struct iblock_dev *ibd = se_dev->se_dev_su_ptr;
  427. struct block_device *bd = ibd->ibd_bd;
  428. char buf[BDEVNAME_SIZE];
  429. ssize_t bl = 0;
  430. if (bd)
  431. bl += sprintf(b + bl, "iBlock device: %s",
  432. bdevname(bd, buf));
  433. if (ibd->ibd_flags & IBDF_HAS_UDEV_PATH)
  434. bl += sprintf(b + bl, " UDEV PATH: %s",
  435. ibd->ibd_udev_path);
  436. bl += sprintf(b + bl, " readonly: %d\n", ibd->ibd_readonly);
  437. bl += sprintf(b + bl, " ");
  438. if (bd) {
  439. bl += sprintf(b + bl, "Major: %d Minor: %d %s\n",
  440. MAJOR(bd->bd_dev), MINOR(bd->bd_dev), (!bd->bd_contains) ?
  441. "" : (bd->bd_holder == ibd) ?
  442. "CLAIMED: IBLOCK" : "CLAIMED: OS");
  443. } else {
  444. bl += sprintf(b + bl, "Major: 0 Minor: 0\n");
  445. }
  446. return bl;
  447. }
  448. static void iblock_complete_cmd(struct se_cmd *cmd)
  449. {
  450. struct iblock_req *ibr = cmd->priv;
  451. u8 status;
  452. if (!atomic_dec_and_test(&ibr->pending))
  453. return;
  454. if (atomic_read(&ibr->ib_bio_err_cnt))
  455. status = SAM_STAT_CHECK_CONDITION;
  456. else
  457. status = SAM_STAT_GOOD;
  458. target_complete_cmd(cmd, status);
  459. kfree(ibr);
  460. }
  461. static void iblock_bio_destructor(struct bio *bio)
  462. {
  463. struct se_cmd *cmd = bio->bi_private;
  464. struct iblock_dev *ib_dev = cmd->se_dev->dev_ptr;
  465. bio_free(bio, ib_dev->ibd_bio_set);
  466. }
  467. static struct bio *
  468. iblock_get_bio(struct se_cmd *cmd, sector_t lba, u32 sg_num)
  469. {
  470. struct iblock_dev *ib_dev = cmd->se_dev->dev_ptr;
  471. struct bio *bio;
  472. /*
  473. * Only allocate as many vector entries as the bio code allows us to,
  474. * we'll loop later on until we have handled the whole request.
  475. */
  476. if (sg_num > BIO_MAX_PAGES)
  477. sg_num = BIO_MAX_PAGES;
  478. bio = bio_alloc_bioset(GFP_NOIO, sg_num, ib_dev->ibd_bio_set);
  479. if (!bio) {
  480. pr_err("Unable to allocate memory for bio\n");
  481. return NULL;
  482. }
  483. bio->bi_bdev = ib_dev->ibd_bd;
  484. bio->bi_private = cmd;
  485. bio->bi_destructor = iblock_bio_destructor;
  486. bio->bi_end_io = &iblock_bio_done;
  487. bio->bi_sector = lba;
  488. return bio;
  489. }
  490. static void iblock_submit_bios(struct bio_list *list, int rw)
  491. {
  492. struct blk_plug plug;
  493. struct bio *bio;
  494. blk_start_plug(&plug);
  495. while ((bio = bio_list_pop(list)))
  496. submit_bio(rw, bio);
  497. blk_finish_plug(&plug);
  498. }
  499. static int iblock_execute_rw(struct se_cmd *cmd)
  500. {
  501. struct scatterlist *sgl = cmd->t_data_sg;
  502. u32 sgl_nents = cmd->t_data_nents;
  503. enum dma_data_direction data_direction = cmd->data_direction;
  504. struct se_device *dev = cmd->se_dev;
  505. struct iblock_req *ibr;
  506. struct bio *bio;
  507. struct bio_list list;
  508. struct scatterlist *sg;
  509. u32 sg_num = sgl_nents;
  510. sector_t block_lba;
  511. unsigned bio_cnt;
  512. int rw;
  513. int i;
  514. if (data_direction == DMA_TO_DEVICE) {
  515. /*
  516. * Force data to disk if we pretend to not have a volatile
  517. * write cache, or the initiator set the Force Unit Access bit.
  518. */
  519. if (dev->se_sub_dev->se_dev_attrib.emulate_write_cache == 0 ||
  520. (dev->se_sub_dev->se_dev_attrib.emulate_fua_write > 0 &&
  521. (cmd->se_cmd_flags & SCF_FUA)))
  522. rw = WRITE_FUA;
  523. else
  524. rw = WRITE;
  525. } else {
  526. rw = READ;
  527. }
  528. /*
  529. * Convert the blocksize advertised to the initiator to the 512 byte
  530. * units unconditionally used by the Linux block layer.
  531. */
  532. if (dev->se_sub_dev->se_dev_attrib.block_size == 4096)
  533. block_lba = (cmd->t_task_lba << 3);
  534. else if (dev->se_sub_dev->se_dev_attrib.block_size == 2048)
  535. block_lba = (cmd->t_task_lba << 2);
  536. else if (dev->se_sub_dev->se_dev_attrib.block_size == 1024)
  537. block_lba = (cmd->t_task_lba << 1);
  538. else if (dev->se_sub_dev->se_dev_attrib.block_size == 512)
  539. block_lba = cmd->t_task_lba;
  540. else {
  541. pr_err("Unsupported SCSI -> BLOCK LBA conversion:"
  542. " %u\n", dev->se_sub_dev->se_dev_attrib.block_size);
  543. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  544. return -ENOSYS;
  545. }
  546. ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
  547. if (!ibr)
  548. goto fail;
  549. cmd->priv = ibr;
  550. bio = iblock_get_bio(cmd, block_lba, sgl_nents);
  551. if (!bio)
  552. goto fail_free_ibr;
  553. bio_list_init(&list);
  554. bio_list_add(&list, bio);
  555. atomic_set(&ibr->pending, 2);
  556. bio_cnt = 1;
  557. for_each_sg(sgl, sg, sgl_nents, i) {
  558. /*
  559. * XXX: if the length the device accepts is shorter than the
  560. * length of the S/G list entry this will cause and
  561. * endless loop. Better hope no driver uses huge pages.
  562. */
  563. while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
  564. != sg->length) {
  565. if (bio_cnt >= IBLOCK_MAX_BIO_PER_TASK) {
  566. iblock_submit_bios(&list, rw);
  567. bio_cnt = 0;
  568. }
  569. bio = iblock_get_bio(cmd, block_lba, sg_num);
  570. if (!bio)
  571. goto fail_put_bios;
  572. atomic_inc(&ibr->pending);
  573. bio_list_add(&list, bio);
  574. bio_cnt++;
  575. }
  576. /* Always in 512 byte units for Linux/Block */
  577. block_lba += sg->length >> IBLOCK_LBA_SHIFT;
  578. sg_num--;
  579. }
  580. iblock_submit_bios(&list, rw);
  581. iblock_complete_cmd(cmd);
  582. return 0;
  583. fail_put_bios:
  584. while ((bio = bio_list_pop(&list)))
  585. bio_put(bio);
  586. fail_free_ibr:
  587. kfree(ibr);
  588. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  589. fail:
  590. return -ENOMEM;
  591. }
  592. static u32 iblock_get_device_rev(struct se_device *dev)
  593. {
  594. return SCSI_SPC_2; /* Returns SPC-3 in Initiator Data */
  595. }
  596. static u32 iblock_get_device_type(struct se_device *dev)
  597. {
  598. return TYPE_DISK;
  599. }
  600. static sector_t iblock_get_blocks(struct se_device *dev)
  601. {
  602. struct iblock_dev *ibd = dev->dev_ptr;
  603. struct block_device *bd = ibd->ibd_bd;
  604. struct request_queue *q = bdev_get_queue(bd);
  605. return iblock_emulate_read_cap_with_block_size(dev, bd, q);
  606. }
  607. static void iblock_bio_done(struct bio *bio, int err)
  608. {
  609. struct se_cmd *cmd = bio->bi_private;
  610. struct iblock_req *ibr = cmd->priv;
  611. /*
  612. * Set -EIO if !BIO_UPTODATE and the passed is still err=0
  613. */
  614. if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && !err)
  615. err = -EIO;
  616. if (err != 0) {
  617. pr_err("test_bit(BIO_UPTODATE) failed for bio: %p,"
  618. " err: %d\n", bio, err);
  619. /*
  620. * Bump the ib_bio_err_cnt and release bio.
  621. */
  622. atomic_inc(&ibr->ib_bio_err_cnt);
  623. smp_mb__after_atomic_inc();
  624. }
  625. bio_put(bio);
  626. iblock_complete_cmd(cmd);
  627. }
  628. static struct spc_ops iblock_spc_ops = {
  629. .execute_rw = iblock_execute_rw,
  630. .execute_sync_cache = iblock_execute_sync_cache,
  631. .execute_write_same = iblock_execute_write_same,
  632. .execute_unmap = iblock_execute_unmap,
  633. };
  634. static int iblock_parse_cdb(struct se_cmd *cmd)
  635. {
  636. return sbc_parse_cdb(cmd, &iblock_spc_ops);
  637. }
  638. static struct se_subsystem_api iblock_template = {
  639. .name = "iblock",
  640. .owner = THIS_MODULE,
  641. .transport_type = TRANSPORT_PLUGIN_VHBA_PDEV,
  642. .write_cache_emulated = 1,
  643. .fua_write_emulated = 1,
  644. .attach_hba = iblock_attach_hba,
  645. .detach_hba = iblock_detach_hba,
  646. .allocate_virtdevice = iblock_allocate_virtdevice,
  647. .create_virtdevice = iblock_create_virtdevice,
  648. .free_device = iblock_free_device,
  649. .parse_cdb = iblock_parse_cdb,
  650. .check_configfs_dev_params = iblock_check_configfs_dev_params,
  651. .set_configfs_dev_params = iblock_set_configfs_dev_params,
  652. .show_configfs_dev_params = iblock_show_configfs_dev_params,
  653. .get_device_rev = iblock_get_device_rev,
  654. .get_device_type = iblock_get_device_type,
  655. .get_blocks = iblock_get_blocks,
  656. };
  657. static int __init iblock_module_init(void)
  658. {
  659. return transport_subsystem_register(&iblock_template);
  660. }
  661. static void iblock_module_exit(void)
  662. {
  663. transport_subsystem_release(&iblock_template);
  664. }
  665. MODULE_DESCRIPTION("TCM IBLOCK subsystem plugin");
  666. MODULE_AUTHOR("nab@Linux-iSCSI.org");
  667. MODULE_LICENSE("GPL");
  668. module_init(iblock_module_init);
  669. module_exit(iblock_module_exit);