target_core_iblock.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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 <scsi/scsi.h>
  40. #include <scsi/scsi_host.h>
  41. #include <target/target_core_base.h>
  42. #include <target/target_core_device.h>
  43. #include <target/target_core_transport.h>
  44. #include "target_core_iblock.h"
  45. static struct se_subsystem_api iblock_template;
  46. static void iblock_bio_done(struct bio *, int);
  47. /* iblock_attach_hba(): (Part of se_subsystem_api_t template)
  48. *
  49. *
  50. */
  51. static int iblock_attach_hba(struct se_hba *hba, u32 host_id)
  52. {
  53. struct iblock_hba *ib_host;
  54. ib_host = kzalloc(sizeof(struct iblock_hba), GFP_KERNEL);
  55. if (!ib_host) {
  56. pr_err("Unable to allocate memory for"
  57. " struct iblock_hba\n");
  58. return -ENOMEM;
  59. }
  60. ib_host->iblock_host_id = host_id;
  61. hba->hba_ptr = ib_host;
  62. pr_debug("CORE_HBA[%d] - TCM iBlock HBA Driver %s on"
  63. " Generic Target Core Stack %s\n", hba->hba_id,
  64. IBLOCK_VERSION, TARGET_CORE_MOD_VERSION);
  65. pr_debug("CORE_HBA[%d] - Attached iBlock HBA: %u to Generic\n",
  66. hba->hba_id, ib_host->iblock_host_id);
  67. return 0;
  68. }
  69. static void iblock_detach_hba(struct se_hba *hba)
  70. {
  71. struct iblock_hba *ib_host = hba->hba_ptr;
  72. pr_debug("CORE_HBA[%d] - Detached iBlock HBA: %u from Generic"
  73. " Target Core\n", hba->hba_id, ib_host->iblock_host_id);
  74. kfree(ib_host);
  75. hba->hba_ptr = NULL;
  76. }
  77. static void *iblock_allocate_virtdevice(struct se_hba *hba, const char *name)
  78. {
  79. struct iblock_dev *ib_dev = NULL;
  80. struct iblock_hba *ib_host = hba->hba_ptr;
  81. ib_dev = kzalloc(sizeof(struct iblock_dev), GFP_KERNEL);
  82. if (!ib_dev) {
  83. pr_err("Unable to allocate struct iblock_dev\n");
  84. return NULL;
  85. }
  86. ib_dev->ibd_host = ib_host;
  87. pr_debug( "IBLOCK: Allocated ib_dev for %s\n", name);
  88. return ib_dev;
  89. }
  90. static struct se_device *iblock_create_virtdevice(
  91. struct se_hba *hba,
  92. struct se_subsystem_dev *se_dev,
  93. void *p)
  94. {
  95. struct iblock_dev *ib_dev = p;
  96. struct se_device *dev;
  97. struct se_dev_limits dev_limits;
  98. struct block_device *bd = NULL;
  99. struct request_queue *q;
  100. struct queue_limits *limits;
  101. u32 dev_flags = 0;
  102. int ret = -EINVAL;
  103. if (!ib_dev) {
  104. pr_err("Unable to locate struct iblock_dev parameter\n");
  105. return ERR_PTR(ret);
  106. }
  107. memset(&dev_limits, 0, sizeof(struct se_dev_limits));
  108. /*
  109. * These settings need to be made tunable..
  110. */
  111. ib_dev->ibd_bio_set = bioset_create(32, 64);
  112. if (!ib_dev->ibd_bio_set) {
  113. pr_err("IBLOCK: Unable to create bioset()\n");
  114. return ERR_PTR(-ENOMEM);
  115. }
  116. pr_debug("IBLOCK: Created bio_set()\n");
  117. /*
  118. * iblock_check_configfs_dev_params() ensures that ib_dev->ibd_udev_path
  119. * must already have been set in order for echo 1 > $HBA/$DEV/enable to run.
  120. */
  121. pr_debug( "IBLOCK: Claiming struct block_device: %s\n",
  122. ib_dev->ibd_udev_path);
  123. bd = blkdev_get_by_path(ib_dev->ibd_udev_path,
  124. FMODE_WRITE|FMODE_READ|FMODE_EXCL, ib_dev);
  125. if (IS_ERR(bd)) {
  126. ret = PTR_ERR(bd);
  127. goto failed;
  128. }
  129. /*
  130. * Setup the local scope queue_limits from struct request_queue->limits
  131. * to pass into transport_add_device_to_core_hba() as struct se_dev_limits.
  132. */
  133. q = bdev_get_queue(bd);
  134. limits = &dev_limits.limits;
  135. limits->logical_block_size = bdev_logical_block_size(bd);
  136. limits->max_hw_sectors = queue_max_hw_sectors(q);
  137. limits->max_sectors = queue_max_sectors(q);
  138. dev_limits.hw_queue_depth = q->nr_requests;
  139. dev_limits.queue_depth = q->nr_requests;
  140. ib_dev->ibd_bd = bd;
  141. dev = transport_add_device_to_core_hba(hba,
  142. &iblock_template, se_dev, dev_flags, ib_dev,
  143. &dev_limits, "IBLOCK", IBLOCK_VERSION);
  144. if (!dev)
  145. goto failed;
  146. /*
  147. * Check if the underlying struct block_device request_queue supports
  148. * the QUEUE_FLAG_DISCARD bit for UNMAP/WRITE_SAME in SCSI + TRIM
  149. * in ATA and we need to set TPE=1
  150. */
  151. if (blk_queue_discard(q)) {
  152. dev->se_sub_dev->se_dev_attrib.max_unmap_lba_count =
  153. q->limits.max_discard_sectors;
  154. /*
  155. * Currently hardcoded to 1 in Linux/SCSI code..
  156. */
  157. dev->se_sub_dev->se_dev_attrib.max_unmap_block_desc_count = 1;
  158. dev->se_sub_dev->se_dev_attrib.unmap_granularity =
  159. q->limits.discard_granularity;
  160. dev->se_sub_dev->se_dev_attrib.unmap_granularity_alignment =
  161. q->limits.discard_alignment;
  162. pr_debug("IBLOCK: BLOCK Discard support available,"
  163. " disabled by default\n");
  164. }
  165. if (blk_queue_nonrot(q))
  166. dev->se_sub_dev->se_dev_attrib.is_nonrot = 1;
  167. return dev;
  168. failed:
  169. if (ib_dev->ibd_bio_set) {
  170. bioset_free(ib_dev->ibd_bio_set);
  171. ib_dev->ibd_bio_set = NULL;
  172. }
  173. ib_dev->ibd_bd = NULL;
  174. return ERR_PTR(ret);
  175. }
  176. static void iblock_free_device(void *p)
  177. {
  178. struct iblock_dev *ib_dev = p;
  179. if (ib_dev->ibd_bd != NULL)
  180. blkdev_put(ib_dev->ibd_bd, FMODE_WRITE|FMODE_READ|FMODE_EXCL);
  181. if (ib_dev->ibd_bio_set != NULL)
  182. bioset_free(ib_dev->ibd_bio_set);
  183. kfree(ib_dev);
  184. }
  185. static inline struct iblock_req *IBLOCK_REQ(struct se_task *task)
  186. {
  187. return container_of(task, struct iblock_req, ib_task);
  188. }
  189. static struct se_task *
  190. iblock_alloc_task(unsigned char *cdb)
  191. {
  192. struct iblock_req *ib_req;
  193. ib_req = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
  194. if (!ib_req) {
  195. pr_err("Unable to allocate memory for struct iblock_req\n");
  196. return NULL;
  197. }
  198. atomic_set(&ib_req->ib_bio_cnt, 0);
  199. return &ib_req->ib_task;
  200. }
  201. static unsigned long long iblock_emulate_read_cap_with_block_size(
  202. struct se_device *dev,
  203. struct block_device *bd,
  204. struct request_queue *q)
  205. {
  206. unsigned long long blocks_long = (div_u64(i_size_read(bd->bd_inode),
  207. bdev_logical_block_size(bd)) - 1);
  208. u32 block_size = bdev_logical_block_size(bd);
  209. if (block_size == dev->se_sub_dev->se_dev_attrib.block_size)
  210. return blocks_long;
  211. switch (block_size) {
  212. case 4096:
  213. switch (dev->se_sub_dev->se_dev_attrib.block_size) {
  214. case 2048:
  215. blocks_long <<= 1;
  216. break;
  217. case 1024:
  218. blocks_long <<= 2;
  219. break;
  220. case 512:
  221. blocks_long <<= 3;
  222. default:
  223. break;
  224. }
  225. break;
  226. case 2048:
  227. switch (dev->se_sub_dev->se_dev_attrib.block_size) {
  228. case 4096:
  229. blocks_long >>= 1;
  230. break;
  231. case 1024:
  232. blocks_long <<= 1;
  233. break;
  234. case 512:
  235. blocks_long <<= 2;
  236. break;
  237. default:
  238. break;
  239. }
  240. break;
  241. case 1024:
  242. switch (dev->se_sub_dev->se_dev_attrib.block_size) {
  243. case 4096:
  244. blocks_long >>= 2;
  245. break;
  246. case 2048:
  247. blocks_long >>= 1;
  248. break;
  249. case 512:
  250. blocks_long <<= 1;
  251. break;
  252. default:
  253. break;
  254. }
  255. break;
  256. case 512:
  257. switch (dev->se_sub_dev->se_dev_attrib.block_size) {
  258. case 4096:
  259. blocks_long >>= 3;
  260. break;
  261. case 2048:
  262. blocks_long >>= 2;
  263. break;
  264. case 1024:
  265. blocks_long >>= 1;
  266. break;
  267. default:
  268. break;
  269. }
  270. break;
  271. default:
  272. break;
  273. }
  274. return blocks_long;
  275. }
  276. /*
  277. * Emulate SYCHRONIZE_CACHE_*
  278. */
  279. static void iblock_emulate_sync_cache(struct se_task *task)
  280. {
  281. struct se_cmd *cmd = task->task_se_cmd;
  282. struct iblock_dev *ib_dev = cmd->se_dev->dev_ptr;
  283. int immed = (cmd->t_task_cdb[1] & 0x2);
  284. sector_t error_sector;
  285. int ret;
  286. /*
  287. * If the Immediate bit is set, queue up the GOOD response
  288. * for this SYNCHRONIZE_CACHE op
  289. */
  290. if (immed)
  291. transport_complete_sync_cache(cmd, 1);
  292. /*
  293. * blkdev_issue_flush() does not support a specifying a range, so
  294. * we have to flush the entire cache.
  295. */
  296. ret = blkdev_issue_flush(ib_dev->ibd_bd, GFP_KERNEL, &error_sector);
  297. if (ret != 0) {
  298. pr_err("IBLOCK: block_issue_flush() failed: %d "
  299. " error_sector: %llu\n", ret,
  300. (unsigned long long)error_sector);
  301. }
  302. if (!immed)
  303. transport_complete_sync_cache(cmd, ret == 0);
  304. }
  305. /*
  306. * Tell TCM Core that we are capable of WriteCache emulation for
  307. * an underlying struct se_device.
  308. */
  309. static int iblock_emulated_write_cache(struct se_device *dev)
  310. {
  311. return 1;
  312. }
  313. static int iblock_emulated_dpo(struct se_device *dev)
  314. {
  315. return 0;
  316. }
  317. /*
  318. * Tell TCM Core that we will be emulating Forced Unit Access (FUA) for WRITEs
  319. * for TYPE_DISK.
  320. */
  321. static int iblock_emulated_fua_write(struct se_device *dev)
  322. {
  323. return 1;
  324. }
  325. static int iblock_emulated_fua_read(struct se_device *dev)
  326. {
  327. return 0;
  328. }
  329. static int iblock_do_task(struct se_task *task)
  330. {
  331. struct se_device *dev = task->task_se_cmd->se_dev;
  332. struct iblock_req *req = IBLOCK_REQ(task);
  333. struct bio *bio = req->ib_bio, *nbio = NULL;
  334. struct blk_plug plug;
  335. int rw;
  336. if (task->task_data_direction == DMA_TO_DEVICE) {
  337. /*
  338. * Force data to disk if we pretend to not have a volatile
  339. * write cache, or the initiator set the Force Unit Access bit.
  340. */
  341. if (dev->se_sub_dev->se_dev_attrib.emulate_write_cache == 0 ||
  342. (dev->se_sub_dev->se_dev_attrib.emulate_fua_write > 0 &&
  343. task->task_se_cmd->t_tasks_fua))
  344. rw = WRITE_FUA;
  345. else
  346. rw = WRITE;
  347. } else {
  348. rw = READ;
  349. }
  350. blk_start_plug(&plug);
  351. while (bio) {
  352. nbio = bio->bi_next;
  353. bio->bi_next = NULL;
  354. pr_debug("Calling submit_bio() task: %p bio: %p"
  355. " bio->bi_sector: %llu\n", task, bio,
  356. (unsigned long long)bio->bi_sector);
  357. submit_bio(rw, bio);
  358. bio = nbio;
  359. }
  360. blk_finish_plug(&plug);
  361. return PYX_TRANSPORT_SENT_TO_TRANSPORT;
  362. }
  363. static int iblock_do_discard(struct se_device *dev, sector_t lba, u32 range)
  364. {
  365. struct iblock_dev *ibd = dev->dev_ptr;
  366. struct block_device *bd = ibd->ibd_bd;
  367. int barrier = 0;
  368. return blkdev_issue_discard(bd, lba, range, GFP_KERNEL, barrier);
  369. }
  370. static void iblock_free_task(struct se_task *task)
  371. {
  372. struct iblock_req *req = IBLOCK_REQ(task);
  373. struct bio *bio, *hbio = req->ib_bio;
  374. /*
  375. * We only release the bio(s) here if iblock_bio_done() has not called
  376. * bio_put() -> iblock_bio_destructor().
  377. */
  378. while (hbio != NULL) {
  379. bio = hbio;
  380. hbio = hbio->bi_next;
  381. bio->bi_next = NULL;
  382. bio_put(bio);
  383. }
  384. kfree(req);
  385. }
  386. enum {
  387. Opt_udev_path, Opt_force, Opt_err
  388. };
  389. static match_table_t tokens = {
  390. {Opt_udev_path, "udev_path=%s"},
  391. {Opt_force, "force=%d"},
  392. {Opt_err, NULL}
  393. };
  394. static ssize_t iblock_set_configfs_dev_params(struct se_hba *hba,
  395. struct se_subsystem_dev *se_dev,
  396. const char *page, ssize_t count)
  397. {
  398. struct iblock_dev *ib_dev = se_dev->se_dev_su_ptr;
  399. char *orig, *ptr, *arg_p, *opts;
  400. substring_t args[MAX_OPT_ARGS];
  401. int ret = 0, token;
  402. opts = kstrdup(page, GFP_KERNEL);
  403. if (!opts)
  404. return -ENOMEM;
  405. orig = opts;
  406. while ((ptr = strsep(&opts, ",")) != NULL) {
  407. if (!*ptr)
  408. continue;
  409. token = match_token(ptr, tokens, args);
  410. switch (token) {
  411. case Opt_udev_path:
  412. if (ib_dev->ibd_bd) {
  413. pr_err("Unable to set udev_path= while"
  414. " ib_dev->ibd_bd exists\n");
  415. ret = -EEXIST;
  416. goto out;
  417. }
  418. arg_p = match_strdup(&args[0]);
  419. if (!arg_p) {
  420. ret = -ENOMEM;
  421. break;
  422. }
  423. snprintf(ib_dev->ibd_udev_path, SE_UDEV_PATH_LEN,
  424. "%s", arg_p);
  425. kfree(arg_p);
  426. pr_debug("IBLOCK: Referencing UDEV path: %s\n",
  427. ib_dev->ibd_udev_path);
  428. ib_dev->ibd_flags |= IBDF_HAS_UDEV_PATH;
  429. break;
  430. case Opt_force:
  431. break;
  432. default:
  433. break;
  434. }
  435. }
  436. out:
  437. kfree(orig);
  438. return (!ret) ? count : ret;
  439. }
  440. static ssize_t iblock_check_configfs_dev_params(
  441. struct se_hba *hba,
  442. struct se_subsystem_dev *se_dev)
  443. {
  444. struct iblock_dev *ibd = se_dev->se_dev_su_ptr;
  445. if (!(ibd->ibd_flags & IBDF_HAS_UDEV_PATH)) {
  446. pr_err("Missing udev_path= parameters for IBLOCK\n");
  447. return -EINVAL;
  448. }
  449. return 0;
  450. }
  451. static ssize_t iblock_show_configfs_dev_params(
  452. struct se_hba *hba,
  453. struct se_subsystem_dev *se_dev,
  454. char *b)
  455. {
  456. struct iblock_dev *ibd = se_dev->se_dev_su_ptr;
  457. struct block_device *bd = ibd->ibd_bd;
  458. char buf[BDEVNAME_SIZE];
  459. ssize_t bl = 0;
  460. if (bd)
  461. bl += sprintf(b + bl, "iBlock device: %s",
  462. bdevname(bd, buf));
  463. if (ibd->ibd_flags & IBDF_HAS_UDEV_PATH) {
  464. bl += sprintf(b + bl, " UDEV PATH: %s\n",
  465. ibd->ibd_udev_path);
  466. } else
  467. bl += sprintf(b + bl, "\n");
  468. bl += sprintf(b + bl, " ");
  469. if (bd) {
  470. bl += sprintf(b + bl, "Major: %d Minor: %d %s\n",
  471. MAJOR(bd->bd_dev), MINOR(bd->bd_dev), (!bd->bd_contains) ?
  472. "" : (bd->bd_holder == (struct iblock_dev *)ibd) ?
  473. "CLAIMED: IBLOCK" : "CLAIMED: OS");
  474. } else {
  475. bl += sprintf(b + bl, "Major: 0 Minor: 0\n");
  476. }
  477. return bl;
  478. }
  479. static void iblock_bio_destructor(struct bio *bio)
  480. {
  481. struct se_task *task = bio->bi_private;
  482. struct iblock_dev *ib_dev = task->se_dev->dev_ptr;
  483. bio_free(bio, ib_dev->ibd_bio_set);
  484. }
  485. static struct bio *iblock_get_bio(
  486. struct se_task *task,
  487. struct iblock_req *ib_req,
  488. struct iblock_dev *ib_dev,
  489. int *ret,
  490. sector_t lba,
  491. u32 sg_num)
  492. {
  493. struct bio *bio;
  494. bio = bio_alloc_bioset(GFP_NOIO, sg_num, ib_dev->ibd_bio_set);
  495. if (!bio) {
  496. pr_err("Unable to allocate memory for bio\n");
  497. *ret = PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
  498. return NULL;
  499. }
  500. pr_debug("Allocated bio: %p task_sg_nents: %u using ibd_bio_set:"
  501. " %p\n", bio, task->task_sg_nents, ib_dev->ibd_bio_set);
  502. pr_debug("Allocated bio: %p task_size: %u\n", bio, task->task_size);
  503. bio->bi_bdev = ib_dev->ibd_bd;
  504. bio->bi_private = task;
  505. bio->bi_destructor = iblock_bio_destructor;
  506. bio->bi_end_io = &iblock_bio_done;
  507. bio->bi_sector = lba;
  508. atomic_inc(&ib_req->ib_bio_cnt);
  509. pr_debug("Set bio->bi_sector: %llu\n", (unsigned long long)bio->bi_sector);
  510. pr_debug("Set ib_req->ib_bio_cnt: %d\n",
  511. atomic_read(&ib_req->ib_bio_cnt));
  512. return bio;
  513. }
  514. static int iblock_map_data_SG(struct se_task *task)
  515. {
  516. struct se_cmd *cmd = task->task_se_cmd;
  517. struct se_device *dev = cmd->se_dev;
  518. struct iblock_dev *ib_dev = task->se_dev->dev_ptr;
  519. struct iblock_req *ib_req = IBLOCK_REQ(task);
  520. struct bio *bio = NULL, *hbio = NULL, *tbio = NULL;
  521. struct scatterlist *sg;
  522. int ret = 0;
  523. u32 i, sg_num = task->task_sg_nents;
  524. sector_t block_lba;
  525. /*
  526. * Do starting conversion up from non 512-byte blocksize with
  527. * struct se_task SCSI blocksize into Linux/Block 512 units for BIO.
  528. */
  529. if (dev->se_sub_dev->se_dev_attrib.block_size == 4096)
  530. block_lba = (task->task_lba << 3);
  531. else if (dev->se_sub_dev->se_dev_attrib.block_size == 2048)
  532. block_lba = (task->task_lba << 2);
  533. else if (dev->se_sub_dev->se_dev_attrib.block_size == 1024)
  534. block_lba = (task->task_lba << 1);
  535. else if (dev->se_sub_dev->se_dev_attrib.block_size == 512)
  536. block_lba = task->task_lba;
  537. else {
  538. pr_err("Unsupported SCSI -> BLOCK LBA conversion:"
  539. " %u\n", dev->se_sub_dev->se_dev_attrib.block_size);
  540. return PYX_TRANSPORT_LU_COMM_FAILURE;
  541. }
  542. bio = iblock_get_bio(task, ib_req, ib_dev, &ret, block_lba, sg_num);
  543. if (!bio)
  544. return ret;
  545. ib_req->ib_bio = bio;
  546. hbio = tbio = bio;
  547. /*
  548. * Use fs/bio.c:bio_add_pages() to setup the bio_vec maplist
  549. * from task->task_sg -> struct scatterlist memory.
  550. */
  551. for_each_sg(task->task_sg, sg, task->task_sg_nents, i) {
  552. pr_debug("task: %p bio: %p Calling bio_add_page(): page:"
  553. " %p len: %u offset: %u\n", task, bio, sg_page(sg),
  554. sg->length, sg->offset);
  555. again:
  556. ret = bio_add_page(bio, sg_page(sg), sg->length, sg->offset);
  557. if (ret != sg->length) {
  558. pr_debug("*** Set bio->bi_sector: %llu\n",
  559. (unsigned long long)bio->bi_sector);
  560. pr_debug("** task->task_size: %u\n",
  561. task->task_size);
  562. pr_debug("*** bio->bi_max_vecs: %u\n",
  563. bio->bi_max_vecs);
  564. pr_debug("*** bio->bi_vcnt: %u\n",
  565. bio->bi_vcnt);
  566. bio = iblock_get_bio(task, ib_req, ib_dev, &ret,
  567. block_lba, sg_num);
  568. if (!bio)
  569. goto fail;
  570. tbio = tbio->bi_next = bio;
  571. pr_debug("-----------------> Added +1 bio: %p to"
  572. " list, Going to again\n", bio);
  573. goto again;
  574. }
  575. /* Always in 512 byte units for Linux/Block */
  576. block_lba += sg->length >> IBLOCK_LBA_SHIFT;
  577. sg_num--;
  578. pr_debug("task: %p bio-add_page() passed!, decremented"
  579. " sg_num to %u\n", task, sg_num);
  580. pr_debug("task: %p bio_add_page() passed!, increased lba"
  581. " to %llu\n", task, (unsigned long long)block_lba);
  582. pr_debug("task: %p bio_add_page() passed!, bio->bi_vcnt:"
  583. " %u\n", task, bio->bi_vcnt);
  584. }
  585. return 0;
  586. fail:
  587. while (hbio) {
  588. bio = hbio;
  589. hbio = hbio->bi_next;
  590. bio->bi_next = NULL;
  591. bio_put(bio);
  592. }
  593. return ret;
  594. }
  595. static unsigned char *iblock_get_cdb(struct se_task *task)
  596. {
  597. return IBLOCK_REQ(task)->ib_scsi_cdb;
  598. }
  599. static u32 iblock_get_device_rev(struct se_device *dev)
  600. {
  601. return SCSI_SPC_2; /* Returns SPC-3 in Initiator Data */
  602. }
  603. static u32 iblock_get_device_type(struct se_device *dev)
  604. {
  605. return TYPE_DISK;
  606. }
  607. static sector_t iblock_get_blocks(struct se_device *dev)
  608. {
  609. struct iblock_dev *ibd = dev->dev_ptr;
  610. struct block_device *bd = ibd->ibd_bd;
  611. struct request_queue *q = bdev_get_queue(bd);
  612. return iblock_emulate_read_cap_with_block_size(dev, bd, q);
  613. }
  614. static void iblock_bio_done(struct bio *bio, int err)
  615. {
  616. struct se_task *task = bio->bi_private;
  617. struct iblock_req *ibr = IBLOCK_REQ(task);
  618. /*
  619. * Set -EIO if !BIO_UPTODATE and the passed is still err=0
  620. */
  621. if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && !err)
  622. err = -EIO;
  623. if (err != 0) {
  624. pr_err("test_bit(BIO_UPTODATE) failed for bio: %p,"
  625. " err: %d\n", bio, err);
  626. /*
  627. * Bump the ib_bio_err_cnt and release bio.
  628. */
  629. atomic_inc(&ibr->ib_bio_err_cnt);
  630. smp_mb__after_atomic_inc();
  631. bio_put(bio);
  632. /*
  633. * Wait to complete the task until the last bio as completed.
  634. */
  635. if (!atomic_dec_and_test(&ibr->ib_bio_cnt))
  636. return;
  637. ibr->ib_bio = NULL;
  638. transport_complete_task(task, 0);
  639. return;
  640. }
  641. pr_debug("done[%p] bio: %p task_lba: %llu bio_lba: %llu err=%d\n",
  642. task, bio, task->task_lba, (unsigned long long)bio->bi_sector, err);
  643. /*
  644. * bio_put() will call iblock_bio_destructor() to release the bio back
  645. * to ibr->ib_bio_set.
  646. */
  647. bio_put(bio);
  648. /*
  649. * Wait to complete the task until the last bio as completed.
  650. */
  651. if (!atomic_dec_and_test(&ibr->ib_bio_cnt))
  652. return;
  653. /*
  654. * Return GOOD status for task if zero ib_bio_err_cnt exists.
  655. */
  656. ibr->ib_bio = NULL;
  657. transport_complete_task(task, (!atomic_read(&ibr->ib_bio_err_cnt)));
  658. }
  659. static struct se_subsystem_api iblock_template = {
  660. .name = "iblock",
  661. .owner = THIS_MODULE,
  662. .transport_type = TRANSPORT_PLUGIN_VHBA_PDEV,
  663. .map_data_SG = iblock_map_data_SG,
  664. .attach_hba = iblock_attach_hba,
  665. .detach_hba = iblock_detach_hba,
  666. .allocate_virtdevice = iblock_allocate_virtdevice,
  667. .create_virtdevice = iblock_create_virtdevice,
  668. .free_device = iblock_free_device,
  669. .dpo_emulated = iblock_emulated_dpo,
  670. .fua_write_emulated = iblock_emulated_fua_write,
  671. .fua_read_emulated = iblock_emulated_fua_read,
  672. .write_cache_emulated = iblock_emulated_write_cache,
  673. .alloc_task = iblock_alloc_task,
  674. .do_task = iblock_do_task,
  675. .do_discard = iblock_do_discard,
  676. .do_sync_cache = iblock_emulate_sync_cache,
  677. .free_task = iblock_free_task,
  678. .check_configfs_dev_params = iblock_check_configfs_dev_params,
  679. .set_configfs_dev_params = iblock_set_configfs_dev_params,
  680. .show_configfs_dev_params = iblock_show_configfs_dev_params,
  681. .get_cdb = iblock_get_cdb,
  682. .get_device_rev = iblock_get_device_rev,
  683. .get_device_type = iblock_get_device_type,
  684. .get_blocks = iblock_get_blocks,
  685. };
  686. static int __init iblock_module_init(void)
  687. {
  688. return transport_subsystem_register(&iblock_template);
  689. }
  690. static void iblock_module_exit(void)
  691. {
  692. transport_subsystem_release(&iblock_template);
  693. }
  694. MODULE_DESCRIPTION("TCM IBLOCK subsystem plugin");
  695. MODULE_AUTHOR("nab@Linux-iSCSI.org");
  696. MODULE_LICENSE("GPL");
  697. module_init(iblock_module_init);
  698. module_exit(iblock_module_exit);