target_core_iblock.c 20 KB

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