target_core_iblock.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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 <target/target_core_base.h>
  43. #include <target/target_core_backend.h>
  44. #include "target_core_iblock.h"
  45. #define IBLOCK_MAX_BIO_PER_TASK 32 /* max # of bios to submit at a time */
  46. #define IBLOCK_BIO_POOL_SIZE 128
  47. static struct se_subsystem_api iblock_template;
  48. static void iblock_bio_done(struct bio *, int);
  49. /* iblock_attach_hba(): (Part of se_subsystem_api_t template)
  50. *
  51. *
  52. */
  53. static int iblock_attach_hba(struct se_hba *hba, u32 host_id)
  54. {
  55. pr_debug("CORE_HBA[%d] - TCM iBlock HBA Driver %s on"
  56. " Generic Target Core Stack %s\n", hba->hba_id,
  57. IBLOCK_VERSION, TARGET_CORE_MOD_VERSION);
  58. return 0;
  59. }
  60. static void iblock_detach_hba(struct se_hba *hba)
  61. {
  62. }
  63. static void *iblock_allocate_virtdevice(struct se_hba *hba, const char *name)
  64. {
  65. struct iblock_dev *ib_dev = NULL;
  66. ib_dev = kzalloc(sizeof(struct iblock_dev), GFP_KERNEL);
  67. if (!ib_dev) {
  68. pr_err("Unable to allocate struct iblock_dev\n");
  69. return NULL;
  70. }
  71. pr_debug( "IBLOCK: Allocated ib_dev for %s\n", name);
  72. return ib_dev;
  73. }
  74. static struct se_device *iblock_create_virtdevice(
  75. struct se_hba *hba,
  76. struct se_subsystem_dev *se_dev,
  77. void *p)
  78. {
  79. struct iblock_dev *ib_dev = p;
  80. struct se_device *dev;
  81. struct se_dev_limits dev_limits;
  82. struct block_device *bd = NULL;
  83. struct request_queue *q;
  84. struct queue_limits *limits;
  85. u32 dev_flags = 0;
  86. int ret = -EINVAL;
  87. if (!ib_dev) {
  88. pr_err("Unable to locate struct iblock_dev parameter\n");
  89. return ERR_PTR(ret);
  90. }
  91. memset(&dev_limits, 0, sizeof(struct se_dev_limits));
  92. ib_dev->ibd_bio_set = bioset_create(IBLOCK_BIO_POOL_SIZE, 0);
  93. if (!ib_dev->ibd_bio_set) {
  94. pr_err("IBLOCK: Unable to create bioset()\n");
  95. return ERR_PTR(-ENOMEM);
  96. }
  97. pr_debug("IBLOCK: Created bio_set()\n");
  98. /*
  99. * iblock_check_configfs_dev_params() ensures that ib_dev->ibd_udev_path
  100. * must already have been set in order for echo 1 > $HBA/$DEV/enable to run.
  101. */
  102. pr_debug( "IBLOCK: Claiming struct block_device: %s\n",
  103. ib_dev->ibd_udev_path);
  104. bd = blkdev_get_by_path(ib_dev->ibd_udev_path,
  105. FMODE_WRITE|FMODE_READ|FMODE_EXCL, ib_dev);
  106. if (IS_ERR(bd)) {
  107. ret = PTR_ERR(bd);
  108. goto failed;
  109. }
  110. /*
  111. * Setup the local scope queue_limits from struct request_queue->limits
  112. * to pass into transport_add_device_to_core_hba() as struct se_dev_limits.
  113. */
  114. q = bdev_get_queue(bd);
  115. limits = &dev_limits.limits;
  116. limits->logical_block_size = bdev_logical_block_size(bd);
  117. limits->max_hw_sectors = UINT_MAX;
  118. limits->max_sectors = UINT_MAX;
  119. dev_limits.hw_queue_depth = q->nr_requests;
  120. dev_limits.queue_depth = q->nr_requests;
  121. ib_dev->ibd_bd = bd;
  122. dev = transport_add_device_to_core_hba(hba,
  123. &iblock_template, se_dev, dev_flags, ib_dev,
  124. &dev_limits, "IBLOCK", IBLOCK_VERSION);
  125. if (!dev)
  126. goto failed;
  127. /*
  128. * Check if the underlying struct block_device request_queue supports
  129. * the QUEUE_FLAG_DISCARD bit for UNMAP/WRITE_SAME in SCSI + TRIM
  130. * in ATA and we need to set TPE=1
  131. */
  132. if (blk_queue_discard(q)) {
  133. dev->se_sub_dev->se_dev_attrib.max_unmap_lba_count =
  134. q->limits.max_discard_sectors;
  135. /*
  136. * Currently hardcoded to 1 in Linux/SCSI code..
  137. */
  138. dev->se_sub_dev->se_dev_attrib.max_unmap_block_desc_count = 1;
  139. dev->se_sub_dev->se_dev_attrib.unmap_granularity =
  140. q->limits.discard_granularity >> 9;
  141. dev->se_sub_dev->se_dev_attrib.unmap_granularity_alignment =
  142. q->limits.discard_alignment;
  143. pr_debug("IBLOCK: BLOCK Discard support available,"
  144. " disabled by default\n");
  145. }
  146. if (blk_queue_nonrot(q))
  147. dev->se_sub_dev->se_dev_attrib.is_nonrot = 1;
  148. return dev;
  149. failed:
  150. if (ib_dev->ibd_bio_set) {
  151. bioset_free(ib_dev->ibd_bio_set);
  152. ib_dev->ibd_bio_set = NULL;
  153. }
  154. ib_dev->ibd_bd = NULL;
  155. return ERR_PTR(ret);
  156. }
  157. static void iblock_free_device(void *p)
  158. {
  159. struct iblock_dev *ib_dev = p;
  160. if (ib_dev->ibd_bd != NULL)
  161. blkdev_put(ib_dev->ibd_bd, FMODE_WRITE|FMODE_READ|FMODE_EXCL);
  162. if (ib_dev->ibd_bio_set != NULL)
  163. bioset_free(ib_dev->ibd_bio_set);
  164. kfree(ib_dev);
  165. }
  166. static inline struct iblock_req *IBLOCK_REQ(struct se_task *task)
  167. {
  168. return container_of(task, struct iblock_req, ib_task);
  169. }
  170. static struct se_task *
  171. iblock_alloc_task(unsigned char *cdb)
  172. {
  173. struct iblock_req *ib_req;
  174. ib_req = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
  175. if (!ib_req) {
  176. pr_err("Unable to allocate memory for struct iblock_req\n");
  177. return NULL;
  178. }
  179. atomic_set(&ib_req->pending, 1);
  180. return &ib_req->ib_task;
  181. }
  182. static unsigned long long iblock_emulate_read_cap_with_block_size(
  183. struct se_device *dev,
  184. struct block_device *bd,
  185. struct request_queue *q)
  186. {
  187. unsigned long long blocks_long = (div_u64(i_size_read(bd->bd_inode),
  188. bdev_logical_block_size(bd)) - 1);
  189. u32 block_size = bdev_logical_block_size(bd);
  190. if (block_size == dev->se_sub_dev->se_dev_attrib.block_size)
  191. return blocks_long;
  192. switch (block_size) {
  193. case 4096:
  194. switch (dev->se_sub_dev->se_dev_attrib.block_size) {
  195. case 2048:
  196. blocks_long <<= 1;
  197. break;
  198. case 1024:
  199. blocks_long <<= 2;
  200. break;
  201. case 512:
  202. blocks_long <<= 3;
  203. default:
  204. break;
  205. }
  206. break;
  207. case 2048:
  208. switch (dev->se_sub_dev->se_dev_attrib.block_size) {
  209. case 4096:
  210. blocks_long >>= 1;
  211. break;
  212. case 1024:
  213. blocks_long <<= 1;
  214. break;
  215. case 512:
  216. blocks_long <<= 2;
  217. break;
  218. default:
  219. break;
  220. }
  221. break;
  222. case 1024:
  223. switch (dev->se_sub_dev->se_dev_attrib.block_size) {
  224. case 4096:
  225. blocks_long >>= 2;
  226. break;
  227. case 2048:
  228. blocks_long >>= 1;
  229. break;
  230. case 512:
  231. blocks_long <<= 1;
  232. break;
  233. default:
  234. break;
  235. }
  236. break;
  237. case 512:
  238. switch (dev->se_sub_dev->se_dev_attrib.block_size) {
  239. case 4096:
  240. blocks_long >>= 3;
  241. break;
  242. case 2048:
  243. blocks_long >>= 2;
  244. break;
  245. case 1024:
  246. blocks_long >>= 1;
  247. break;
  248. default:
  249. break;
  250. }
  251. break;
  252. default:
  253. break;
  254. }
  255. return blocks_long;
  256. }
  257. static void iblock_end_io_flush(struct bio *bio, int err)
  258. {
  259. struct se_cmd *cmd = bio->bi_private;
  260. if (err)
  261. pr_err("IBLOCK: cache flush failed: %d\n", err);
  262. if (cmd)
  263. transport_complete_sync_cache(cmd, err == 0);
  264. bio_put(bio);
  265. }
  266. /*
  267. * Implement SYCHRONIZE CACHE. Note that we can't handle lba ranges and must
  268. * always flush the whole cache.
  269. */
  270. static void iblock_emulate_sync_cache(struct se_task *task)
  271. {
  272. struct se_cmd *cmd = task->task_se_cmd;
  273. struct iblock_dev *ib_dev = cmd->se_dev->dev_ptr;
  274. int immed = (cmd->t_task_cdb[1] & 0x2);
  275. struct bio *bio;
  276. /*
  277. * If the Immediate bit is set, queue up the GOOD response
  278. * for this SYNCHRONIZE_CACHE op.
  279. */
  280. if (immed)
  281. transport_complete_sync_cache(cmd, 1);
  282. bio = bio_alloc(GFP_KERNEL, 0);
  283. bio->bi_end_io = iblock_end_io_flush;
  284. bio->bi_bdev = ib_dev->ibd_bd;
  285. if (!immed)
  286. bio->bi_private = cmd;
  287. submit_bio(WRITE_FLUSH, bio);
  288. }
  289. static int iblock_do_discard(struct se_device *dev, sector_t lba, u32 range)
  290. {
  291. struct iblock_dev *ibd = dev->dev_ptr;
  292. struct block_device *bd = ibd->ibd_bd;
  293. int barrier = 0;
  294. return blkdev_issue_discard(bd, lba, range, GFP_KERNEL, barrier);
  295. }
  296. static void iblock_free_task(struct se_task *task)
  297. {
  298. kfree(IBLOCK_REQ(task));
  299. }
  300. enum {
  301. Opt_udev_path, Opt_force, Opt_err
  302. };
  303. static match_table_t tokens = {
  304. {Opt_udev_path, "udev_path=%s"},
  305. {Opt_force, "force=%d"},
  306. {Opt_err, NULL}
  307. };
  308. static ssize_t iblock_set_configfs_dev_params(struct se_hba *hba,
  309. struct se_subsystem_dev *se_dev,
  310. const char *page, ssize_t count)
  311. {
  312. struct iblock_dev *ib_dev = se_dev->se_dev_su_ptr;
  313. char *orig, *ptr, *arg_p, *opts;
  314. substring_t args[MAX_OPT_ARGS];
  315. int ret = 0, token;
  316. opts = kstrdup(page, GFP_KERNEL);
  317. if (!opts)
  318. return -ENOMEM;
  319. orig = opts;
  320. while ((ptr = strsep(&opts, ",\n")) != NULL) {
  321. if (!*ptr)
  322. continue;
  323. token = match_token(ptr, tokens, args);
  324. switch (token) {
  325. case Opt_udev_path:
  326. if (ib_dev->ibd_bd) {
  327. pr_err("Unable to set udev_path= while"
  328. " ib_dev->ibd_bd exists\n");
  329. ret = -EEXIST;
  330. goto out;
  331. }
  332. arg_p = match_strdup(&args[0]);
  333. if (!arg_p) {
  334. ret = -ENOMEM;
  335. break;
  336. }
  337. snprintf(ib_dev->ibd_udev_path, SE_UDEV_PATH_LEN,
  338. "%s", arg_p);
  339. kfree(arg_p);
  340. pr_debug("IBLOCK: Referencing UDEV path: %s\n",
  341. ib_dev->ibd_udev_path);
  342. ib_dev->ibd_flags |= IBDF_HAS_UDEV_PATH;
  343. break;
  344. case Opt_force:
  345. break;
  346. default:
  347. break;
  348. }
  349. }
  350. out:
  351. kfree(orig);
  352. return (!ret) ? count : ret;
  353. }
  354. static ssize_t iblock_check_configfs_dev_params(
  355. struct se_hba *hba,
  356. struct se_subsystem_dev *se_dev)
  357. {
  358. struct iblock_dev *ibd = se_dev->se_dev_su_ptr;
  359. if (!(ibd->ibd_flags & IBDF_HAS_UDEV_PATH)) {
  360. pr_err("Missing udev_path= parameters for IBLOCK\n");
  361. return -EINVAL;
  362. }
  363. return 0;
  364. }
  365. static ssize_t iblock_show_configfs_dev_params(
  366. struct se_hba *hba,
  367. struct se_subsystem_dev *se_dev,
  368. char *b)
  369. {
  370. struct iblock_dev *ibd = se_dev->se_dev_su_ptr;
  371. struct block_device *bd = ibd->ibd_bd;
  372. char buf[BDEVNAME_SIZE];
  373. ssize_t bl = 0;
  374. if (bd)
  375. bl += sprintf(b + bl, "iBlock device: %s",
  376. bdevname(bd, buf));
  377. if (ibd->ibd_flags & IBDF_HAS_UDEV_PATH) {
  378. bl += sprintf(b + bl, " UDEV PATH: %s\n",
  379. ibd->ibd_udev_path);
  380. } else
  381. bl += sprintf(b + bl, "\n");
  382. bl += sprintf(b + bl, " ");
  383. if (bd) {
  384. bl += sprintf(b + bl, "Major: %d Minor: %d %s\n",
  385. MAJOR(bd->bd_dev), MINOR(bd->bd_dev), (!bd->bd_contains) ?
  386. "" : (bd->bd_holder == ibd) ?
  387. "CLAIMED: IBLOCK" : "CLAIMED: OS");
  388. } else {
  389. bl += sprintf(b + bl, "Major: 0 Minor: 0\n");
  390. }
  391. return bl;
  392. }
  393. static void iblock_bio_destructor(struct bio *bio)
  394. {
  395. struct se_task *task = bio->bi_private;
  396. struct iblock_dev *ib_dev = task->task_se_cmd->se_dev->dev_ptr;
  397. bio_free(bio, ib_dev->ibd_bio_set);
  398. }
  399. static struct bio *
  400. iblock_get_bio(struct se_task *task, sector_t lba, u32 sg_num)
  401. {
  402. struct iblock_dev *ib_dev = task->task_se_cmd->se_dev->dev_ptr;
  403. struct iblock_req *ib_req = IBLOCK_REQ(task);
  404. struct bio *bio;
  405. /*
  406. * Only allocate as many vector entries as the bio code allows us to,
  407. * we'll loop later on until we have handled the whole request.
  408. */
  409. if (sg_num > BIO_MAX_PAGES)
  410. sg_num = BIO_MAX_PAGES;
  411. bio = bio_alloc_bioset(GFP_NOIO, sg_num, ib_dev->ibd_bio_set);
  412. if (!bio) {
  413. pr_err("Unable to allocate memory for bio\n");
  414. return NULL;
  415. }
  416. pr_debug("Allocated bio: %p task_sg_nents: %u using ibd_bio_set:"
  417. " %p\n", bio, task->task_sg_nents, ib_dev->ibd_bio_set);
  418. pr_debug("Allocated bio: %p task_size: %u\n", bio, task->task_size);
  419. bio->bi_bdev = ib_dev->ibd_bd;
  420. bio->bi_private = task;
  421. bio->bi_destructor = iblock_bio_destructor;
  422. bio->bi_end_io = &iblock_bio_done;
  423. bio->bi_sector = lba;
  424. atomic_inc(&ib_req->pending);
  425. pr_debug("Set bio->bi_sector: %llu\n", (unsigned long long)bio->bi_sector);
  426. pr_debug("Set ib_req->pending: %d\n", atomic_read(&ib_req->pending));
  427. return bio;
  428. }
  429. static void iblock_submit_bios(struct bio_list *list, int rw)
  430. {
  431. struct blk_plug plug;
  432. struct bio *bio;
  433. blk_start_plug(&plug);
  434. while ((bio = bio_list_pop(list)))
  435. submit_bio(rw, bio);
  436. blk_finish_plug(&plug);
  437. }
  438. static int iblock_do_task(struct se_task *task)
  439. {
  440. struct se_cmd *cmd = task->task_se_cmd;
  441. struct se_device *dev = cmd->se_dev;
  442. struct iblock_req *ibr = IBLOCK_REQ(task);
  443. struct bio *bio;
  444. struct bio_list list;
  445. struct scatterlist *sg;
  446. u32 i, sg_num = task->task_sg_nents;
  447. sector_t block_lba;
  448. unsigned bio_cnt;
  449. int rw;
  450. if (task->task_data_direction == DMA_TO_DEVICE) {
  451. /*
  452. * Force data to disk if we pretend to not have a volatile
  453. * write cache, or the initiator set the Force Unit Access bit.
  454. */
  455. if (dev->se_sub_dev->se_dev_attrib.emulate_write_cache == 0 ||
  456. (dev->se_sub_dev->se_dev_attrib.emulate_fua_write > 0 &&
  457. (cmd->se_cmd_flags & SCF_FUA)))
  458. rw = WRITE_FUA;
  459. else
  460. rw = WRITE;
  461. } else {
  462. rw = READ;
  463. }
  464. /*
  465. * Do starting conversion up from non 512-byte blocksize with
  466. * struct se_task SCSI blocksize into Linux/Block 512 units for BIO.
  467. */
  468. if (dev->se_sub_dev->se_dev_attrib.block_size == 4096)
  469. block_lba = (task->task_lba << 3);
  470. else if (dev->se_sub_dev->se_dev_attrib.block_size == 2048)
  471. block_lba = (task->task_lba << 2);
  472. else if (dev->se_sub_dev->se_dev_attrib.block_size == 1024)
  473. block_lba = (task->task_lba << 1);
  474. else if (dev->se_sub_dev->se_dev_attrib.block_size == 512)
  475. block_lba = task->task_lba;
  476. else {
  477. pr_err("Unsupported SCSI -> BLOCK LBA conversion:"
  478. " %u\n", dev->se_sub_dev->se_dev_attrib.block_size);
  479. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  480. return -ENOSYS;
  481. }
  482. bio = iblock_get_bio(task, block_lba, sg_num);
  483. if (!bio) {
  484. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  485. return -ENOMEM;
  486. }
  487. bio_list_init(&list);
  488. bio_list_add(&list, bio);
  489. bio_cnt = 1;
  490. for_each_sg(task->task_sg, sg, task->task_sg_nents, i) {
  491. /*
  492. * XXX: if the length the device accepts is shorter than the
  493. * length of the S/G list entry this will cause and
  494. * endless loop. Better hope no driver uses huge pages.
  495. */
  496. while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
  497. != sg->length) {
  498. if (bio_cnt >= IBLOCK_MAX_BIO_PER_TASK) {
  499. iblock_submit_bios(&list, rw);
  500. bio_cnt = 0;
  501. }
  502. bio = iblock_get_bio(task, block_lba, sg_num);
  503. if (!bio)
  504. goto fail;
  505. bio_list_add(&list, bio);
  506. bio_cnt++;
  507. }
  508. /* Always in 512 byte units for Linux/Block */
  509. block_lba += sg->length >> IBLOCK_LBA_SHIFT;
  510. sg_num--;
  511. }
  512. iblock_submit_bios(&list, rw);
  513. if (atomic_dec_and_test(&ibr->pending)) {
  514. transport_complete_task(task,
  515. !atomic_read(&ibr->ib_bio_err_cnt));
  516. }
  517. return 0;
  518. fail:
  519. while ((bio = bio_list_pop(&list)))
  520. bio_put(bio);
  521. cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  522. return -ENOMEM;
  523. }
  524. static u32 iblock_get_device_rev(struct se_device *dev)
  525. {
  526. return SCSI_SPC_2; /* Returns SPC-3 in Initiator Data */
  527. }
  528. static u32 iblock_get_device_type(struct se_device *dev)
  529. {
  530. return TYPE_DISK;
  531. }
  532. static sector_t iblock_get_blocks(struct se_device *dev)
  533. {
  534. struct iblock_dev *ibd = dev->dev_ptr;
  535. struct block_device *bd = ibd->ibd_bd;
  536. struct request_queue *q = bdev_get_queue(bd);
  537. return iblock_emulate_read_cap_with_block_size(dev, bd, q);
  538. }
  539. static void iblock_bio_done(struct bio *bio, int err)
  540. {
  541. struct se_task *task = bio->bi_private;
  542. struct iblock_req *ibr = IBLOCK_REQ(task);
  543. /*
  544. * Set -EIO if !BIO_UPTODATE and the passed is still err=0
  545. */
  546. if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && !err)
  547. err = -EIO;
  548. if (err != 0) {
  549. pr_err("test_bit(BIO_UPTODATE) failed for bio: %p,"
  550. " err: %d\n", bio, err);
  551. /*
  552. * Bump the ib_bio_err_cnt and release bio.
  553. */
  554. atomic_inc(&ibr->ib_bio_err_cnt);
  555. smp_mb__after_atomic_inc();
  556. }
  557. bio_put(bio);
  558. if (!atomic_dec_and_test(&ibr->pending))
  559. return;
  560. pr_debug("done[%p] bio: %p task_lba: %llu bio_lba: %llu err=%d\n",
  561. task, bio, task->task_lba,
  562. (unsigned long long)bio->bi_sector, err);
  563. transport_complete_task(task, !atomic_read(&ibr->ib_bio_err_cnt));
  564. }
  565. static struct se_subsystem_api iblock_template = {
  566. .name = "iblock",
  567. .owner = THIS_MODULE,
  568. .transport_type = TRANSPORT_PLUGIN_VHBA_PDEV,
  569. .write_cache_emulated = 1,
  570. .fua_write_emulated = 1,
  571. .attach_hba = iblock_attach_hba,
  572. .detach_hba = iblock_detach_hba,
  573. .allocate_virtdevice = iblock_allocate_virtdevice,
  574. .create_virtdevice = iblock_create_virtdevice,
  575. .free_device = iblock_free_device,
  576. .alloc_task = iblock_alloc_task,
  577. .do_task = iblock_do_task,
  578. .do_discard = iblock_do_discard,
  579. .do_sync_cache = iblock_emulate_sync_cache,
  580. .free_task = iblock_free_task,
  581. .check_configfs_dev_params = iblock_check_configfs_dev_params,
  582. .set_configfs_dev_params = iblock_set_configfs_dev_params,
  583. .show_configfs_dev_params = iblock_show_configfs_dev_params,
  584. .get_device_rev = iblock_get_device_rev,
  585. .get_device_type = iblock_get_device_type,
  586. .get_blocks = iblock_get_blocks,
  587. };
  588. static int __init iblock_module_init(void)
  589. {
  590. return transport_subsystem_register(&iblock_template);
  591. }
  592. static void iblock_module_exit(void)
  593. {
  594. transport_subsystem_release(&iblock_template);
  595. }
  596. MODULE_DESCRIPTION("TCM IBLOCK subsystem plugin");
  597. MODULE_AUTHOR("nab@Linux-iSCSI.org");
  598. MODULE_LICENSE("GPL");
  599. module_init(iblock_module_init);
  600. module_exit(iblock_module_exit);