target_core_iblock.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*******************************************************************************
  3. * Filename: target_core_iblock.c
  4. *
  5. * This file contains the Storage Engine <-> Linux BlockIO transport
  6. * specific functions.
  7. *
  8. * (c) Copyright 2003-2013 Datera, Inc.
  9. *
  10. * Nicholas A. Bellinger <nab@kernel.org>
  11. *
  12. ******************************************************************************/
  13. #include <linux/string.h>
  14. #include <linux/parser.h>
  15. #include <linux/timer.h>
  16. #include <linux/fs.h>
  17. #include <linux/blkdev.h>
  18. #include <linux/blk-integrity.h>
  19. #include <linux/slab.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/bio.h>
  22. #include <linux/file.h>
  23. #include <linux/module.h>
  24. #include <linux/scatterlist.h>
  25. #include <linux/pr.h>
  26. #include <scsi/scsi_proto.h>
  27. #include <scsi/scsi_common.h>
  28. #include <linux/unaligned.h>
  29. #include <target/target_core_base.h>
  30. #include <target/target_core_backend.h>
  31. #include "target_core_iblock.h"
  32. #include "target_core_pr.h"
  33. #define IBLOCK_MAX_BIO_PER_TASK 32 /* max # of bios to submit at a time */
  34. #define IBLOCK_BIO_POOL_SIZE 128
  35. static inline struct iblock_dev *IBLOCK_DEV(struct se_device *dev)
  36. {
  37. return container_of(dev, struct iblock_dev, dev);
  38. }
  39. static int iblock_attach_hba(struct se_hba *hba, u32 host_id)
  40. {
  41. pr_debug("CORE_HBA[%d] - TCM iBlock HBA Driver %s on"
  42. " Generic Target Core Stack %s\n", hba->hba_id,
  43. IBLOCK_VERSION, TARGET_CORE_VERSION);
  44. return 0;
  45. }
  46. static void iblock_detach_hba(struct se_hba *hba)
  47. {
  48. }
  49. static struct se_device *iblock_alloc_device(struct se_hba *hba, const char *name)
  50. {
  51. struct iblock_dev *ib_dev = NULL;
  52. ib_dev = kzalloc(sizeof(struct iblock_dev), GFP_KERNEL);
  53. if (!ib_dev) {
  54. pr_err("Unable to allocate struct iblock_dev\n");
  55. return NULL;
  56. }
  57. ib_dev->ibd_plug = kcalloc(nr_cpu_ids, sizeof(*ib_dev->ibd_plug),
  58. GFP_KERNEL);
  59. if (!ib_dev->ibd_plug)
  60. goto free_dev;
  61. pr_debug( "IBLOCK: Allocated ib_dev for %s\n", name);
  62. return &ib_dev->dev;
  63. free_dev:
  64. kfree(ib_dev);
  65. return NULL;
  66. }
  67. static bool iblock_configure_unmap(struct se_device *dev)
  68. {
  69. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  70. return target_configure_unmap_from_queue(&dev->dev_attrib,
  71. ib_dev->ibd_bd);
  72. }
  73. static int iblock_configure_device(struct se_device *dev)
  74. {
  75. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  76. struct request_queue *q;
  77. struct file *bdev_file;
  78. struct block_device *bd;
  79. struct blk_integrity *bi;
  80. blk_mode_t mode = BLK_OPEN_READ;
  81. unsigned int max_write_zeroes_sectors;
  82. int ret;
  83. if (!(ib_dev->ibd_flags & IBDF_HAS_UDEV_PATH)) {
  84. pr_err("Missing udev_path= parameters for IBLOCK\n");
  85. return -EINVAL;
  86. }
  87. ret = bioset_init(&ib_dev->ibd_bio_set, IBLOCK_BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
  88. if (ret) {
  89. pr_err("IBLOCK: Unable to create bioset\n");
  90. goto out;
  91. }
  92. pr_debug( "IBLOCK: Claiming struct block_device: %s\n",
  93. ib_dev->ibd_udev_path);
  94. if (!ib_dev->ibd_readonly)
  95. mode |= BLK_OPEN_WRITE;
  96. else
  97. dev->dev_flags |= DF_READ_ONLY;
  98. bdev_file = bdev_file_open_by_path(ib_dev->ibd_udev_path, mode, ib_dev,
  99. NULL);
  100. if (IS_ERR(bdev_file)) {
  101. ret = PTR_ERR(bdev_file);
  102. goto out_free_bioset;
  103. }
  104. ib_dev->ibd_bdev_file = bdev_file;
  105. ib_dev->ibd_bd = bd = file_bdev(bdev_file);
  106. q = bdev_get_queue(bd);
  107. dev->dev_attrib.hw_block_size = bdev_logical_block_size(bd);
  108. dev->dev_attrib.hw_max_sectors = mult_frac(queue_max_hw_sectors(q),
  109. SECTOR_SIZE,
  110. dev->dev_attrib.hw_block_size);
  111. dev->dev_attrib.hw_queue_depth = q->nr_requests;
  112. /*
  113. * Enable write same emulation for IBLOCK and use 0xFFFF as
  114. * the smaller WRITE_SAME(10) only has a two-byte block count.
  115. */
  116. max_write_zeroes_sectors = bdev_write_zeroes_sectors(bd);
  117. if (max_write_zeroes_sectors)
  118. dev->dev_attrib.max_write_same_len = max_write_zeroes_sectors;
  119. else
  120. dev->dev_attrib.max_write_same_len = 0xFFFF;
  121. if (bdev_nonrot(bd))
  122. dev->dev_attrib.is_nonrot = 1;
  123. bi = bdev_get_integrity(bd);
  124. if (!bi)
  125. return 0;
  126. switch (bi->csum_type) {
  127. case BLK_INTEGRITY_CSUM_IP:
  128. pr_err("IBLOCK export of blk_integrity: %s not supported\n",
  129. blk_integrity_profile_name(bi));
  130. ret = -ENOSYS;
  131. goto out_blkdev_put;
  132. case BLK_INTEGRITY_CSUM_CRC:
  133. if (bi->flags & BLK_INTEGRITY_REF_TAG)
  134. dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE1_PROT;
  135. else
  136. dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE3_PROT;
  137. break;
  138. default:
  139. break;
  140. }
  141. if (dev->dev_attrib.pi_prot_type) {
  142. struct bio_set *bs = &ib_dev->ibd_bio_set;
  143. if (bioset_integrity_create(bs, IBLOCK_BIO_POOL_SIZE) < 0) {
  144. pr_err("Unable to allocate bioset for PI\n");
  145. ret = -ENOMEM;
  146. goto out_blkdev_put;
  147. }
  148. pr_debug("IBLOCK setup BIP bs->bio_integrity_pool: %p\n",
  149. &bs->bio_integrity_pool);
  150. }
  151. dev->dev_attrib.hw_pi_prot_type = dev->dev_attrib.pi_prot_type;
  152. return 0;
  153. out_blkdev_put:
  154. fput(ib_dev->ibd_bdev_file);
  155. out_free_bioset:
  156. bioset_exit(&ib_dev->ibd_bio_set);
  157. out:
  158. return ret;
  159. }
  160. static void iblock_dev_call_rcu(struct rcu_head *p)
  161. {
  162. struct se_device *dev = container_of(p, struct se_device, rcu_head);
  163. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  164. kfree(ib_dev->ibd_plug);
  165. kfree(ib_dev);
  166. }
  167. static void iblock_free_device(struct se_device *dev)
  168. {
  169. call_rcu(&dev->rcu_head, iblock_dev_call_rcu);
  170. }
  171. static void iblock_destroy_device(struct se_device *dev)
  172. {
  173. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  174. if (ib_dev->ibd_bdev_file)
  175. fput(ib_dev->ibd_bdev_file);
  176. bioset_exit(&ib_dev->ibd_bio_set);
  177. }
  178. static struct se_dev_plug *iblock_plug_device(struct se_device *se_dev)
  179. {
  180. struct iblock_dev *ib_dev = IBLOCK_DEV(se_dev);
  181. struct iblock_dev_plug *ib_dev_plug;
  182. /*
  183. * Each se_device has a per cpu work this can be run from. We
  184. * shouldn't have multiple threads on the same cpu calling this
  185. * at the same time.
  186. */
  187. ib_dev_plug = &ib_dev->ibd_plug[raw_smp_processor_id()];
  188. if (test_and_set_bit(IBD_PLUGF_PLUGGED, &ib_dev_plug->flags))
  189. return NULL;
  190. blk_start_plug(&ib_dev_plug->blk_plug);
  191. return &ib_dev_plug->se_plug;
  192. }
  193. static void iblock_unplug_device(struct se_dev_plug *se_plug)
  194. {
  195. struct iblock_dev_plug *ib_dev_plug = container_of(se_plug,
  196. struct iblock_dev_plug, se_plug);
  197. blk_finish_plug(&ib_dev_plug->blk_plug);
  198. clear_bit(IBD_PLUGF_PLUGGED, &ib_dev_plug->flags);
  199. }
  200. static sector_t iblock_get_blocks(struct se_device *dev)
  201. {
  202. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  203. u32 block_size = bdev_logical_block_size(ib_dev->ibd_bd);
  204. unsigned long long blocks_long =
  205. div_u64(bdev_nr_bytes(ib_dev->ibd_bd), block_size) - 1;
  206. if (block_size == dev->dev_attrib.block_size)
  207. return blocks_long;
  208. switch (block_size) {
  209. case 4096:
  210. switch (dev->dev_attrib.block_size) {
  211. case 2048:
  212. blocks_long <<= 1;
  213. break;
  214. case 1024:
  215. blocks_long <<= 2;
  216. break;
  217. case 512:
  218. blocks_long <<= 3;
  219. break;
  220. default:
  221. break;
  222. }
  223. break;
  224. case 2048:
  225. switch (dev->dev_attrib.block_size) {
  226. case 4096:
  227. blocks_long >>= 1;
  228. break;
  229. case 1024:
  230. blocks_long <<= 1;
  231. break;
  232. case 512:
  233. blocks_long <<= 2;
  234. break;
  235. default:
  236. break;
  237. }
  238. break;
  239. case 1024:
  240. switch (dev->dev_attrib.block_size) {
  241. case 4096:
  242. blocks_long >>= 2;
  243. break;
  244. case 2048:
  245. blocks_long >>= 1;
  246. break;
  247. case 512:
  248. blocks_long <<= 1;
  249. break;
  250. default:
  251. break;
  252. }
  253. break;
  254. case 512:
  255. switch (dev->dev_attrib.block_size) {
  256. case 4096:
  257. blocks_long >>= 3;
  258. break;
  259. case 2048:
  260. blocks_long >>= 2;
  261. break;
  262. case 1024:
  263. blocks_long >>= 1;
  264. break;
  265. default:
  266. break;
  267. }
  268. break;
  269. default:
  270. break;
  271. }
  272. return blocks_long;
  273. }
  274. static void iblock_complete_cmd(struct se_cmd *cmd, blk_status_t blk_status)
  275. {
  276. struct iblock_req *ibr = cmd->priv;
  277. u8 status;
  278. if (!refcount_dec_and_test(&ibr->pending))
  279. return;
  280. if (blk_status == BLK_STS_RESV_CONFLICT)
  281. status = SAM_STAT_RESERVATION_CONFLICT;
  282. else if (atomic_read(&ibr->ib_bio_err_cnt))
  283. status = SAM_STAT_CHECK_CONDITION;
  284. else
  285. status = SAM_STAT_GOOD;
  286. target_complete_cmd(cmd, status);
  287. kfree(ibr);
  288. }
  289. static void iblock_bio_done(struct bio *bio)
  290. {
  291. struct se_cmd *cmd = bio->bi_private;
  292. struct iblock_req *ibr = cmd->priv;
  293. blk_status_t blk_status = bio->bi_status;
  294. if (bio->bi_status) {
  295. pr_err("bio error: %p, err: %d\n", bio, bio->bi_status);
  296. /*
  297. * Bump the ib_bio_err_cnt and release bio.
  298. */
  299. atomic_inc(&ibr->ib_bio_err_cnt);
  300. smp_mb__after_atomic();
  301. }
  302. bio_put(bio);
  303. iblock_complete_cmd(cmd, blk_status);
  304. }
  305. static struct bio *iblock_get_bio(struct se_cmd *cmd, sector_t lba, u32 sg_num,
  306. blk_opf_t opf)
  307. {
  308. struct iblock_dev *ib_dev = IBLOCK_DEV(cmd->se_dev);
  309. struct bio *bio;
  310. /*
  311. * Only allocate as many vector entries as the bio code allows us to,
  312. * we'll loop later on until we have handled the whole request.
  313. */
  314. bio = bio_alloc_bioset(ib_dev->ibd_bd, bio_max_segs(sg_num), opf,
  315. GFP_NOIO, &ib_dev->ibd_bio_set);
  316. if (!bio) {
  317. pr_err("Unable to allocate memory for bio\n");
  318. return NULL;
  319. }
  320. bio->bi_private = cmd;
  321. bio->bi_end_io = &iblock_bio_done;
  322. bio->bi_iter.bi_sector = lba;
  323. return bio;
  324. }
  325. static void iblock_submit_bios(struct bio_list *list)
  326. {
  327. struct blk_plug plug;
  328. struct bio *bio;
  329. /*
  330. * The block layer handles nested plugs, so just plug/unplug to handle
  331. * fabric drivers that didn't support batching and multi bio cmds.
  332. */
  333. blk_start_plug(&plug);
  334. while ((bio = bio_list_pop(list)))
  335. submit_bio(bio);
  336. blk_finish_plug(&plug);
  337. }
  338. static void iblock_end_io_flush(struct bio *bio)
  339. {
  340. struct se_cmd *cmd = bio->bi_private;
  341. if (bio->bi_status)
  342. pr_err("IBLOCK: cache flush failed: %d\n", bio->bi_status);
  343. if (cmd) {
  344. if (bio->bi_status)
  345. target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
  346. else
  347. target_complete_cmd(cmd, SAM_STAT_GOOD);
  348. }
  349. bio_put(bio);
  350. }
  351. /*
  352. * Implement SYCHRONIZE CACHE. Note that we can't handle lba ranges and must
  353. * always flush the whole cache.
  354. */
  355. static sense_reason_t
  356. iblock_execute_sync_cache(struct se_cmd *cmd)
  357. {
  358. struct iblock_dev *ib_dev = IBLOCK_DEV(cmd->se_dev);
  359. int immed = (cmd->t_task_cdb[1] & 0x2);
  360. struct bio *bio;
  361. /*
  362. * If the Immediate bit is set, queue up the GOOD response
  363. * for this SYNCHRONIZE_CACHE op.
  364. */
  365. if (immed)
  366. target_complete_cmd(cmd, SAM_STAT_GOOD);
  367. bio = bio_alloc(ib_dev->ibd_bd, 0, REQ_OP_WRITE | REQ_PREFLUSH,
  368. GFP_KERNEL);
  369. bio->bi_end_io = iblock_end_io_flush;
  370. if (!immed)
  371. bio->bi_private = cmd;
  372. submit_bio(bio);
  373. return 0;
  374. }
  375. static sense_reason_t
  376. iblock_execute_unmap(struct se_cmd *cmd, sector_t lba, sector_t nolb)
  377. {
  378. struct block_device *bdev = IBLOCK_DEV(cmd->se_dev)->ibd_bd;
  379. struct se_device *dev = cmd->se_dev;
  380. int ret;
  381. ret = blkdev_issue_discard(bdev,
  382. target_to_linux_sector(dev, lba),
  383. target_to_linux_sector(dev, nolb),
  384. GFP_KERNEL);
  385. if (ret < 0) {
  386. pr_err("blkdev_issue_discard() failed: %d\n", ret);
  387. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  388. }
  389. return 0;
  390. }
  391. static sense_reason_t
  392. iblock_execute_zero_out(struct block_device *bdev, struct se_cmd *cmd)
  393. {
  394. struct se_device *dev = cmd->se_dev;
  395. struct scatterlist *sg = &cmd->t_data_sg[0];
  396. unsigned char *buf, *not_zero;
  397. int ret;
  398. buf = kmap(sg_page(sg)) + sg->offset;
  399. if (!buf)
  400. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  401. /*
  402. * Fall back to block_execute_write_same() slow-path if
  403. * incoming WRITE_SAME payload does not contain zeros.
  404. */
  405. not_zero = memchr_inv(buf, 0x00, cmd->data_length);
  406. kunmap(sg_page(sg));
  407. if (not_zero)
  408. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  409. ret = blkdev_issue_zeroout(bdev,
  410. target_to_linux_sector(dev, cmd->t_task_lba),
  411. target_to_linux_sector(dev,
  412. sbc_get_write_same_sectors(cmd)),
  413. GFP_KERNEL, BLKDEV_ZERO_NOUNMAP);
  414. if (ret)
  415. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  416. target_complete_cmd(cmd, SAM_STAT_GOOD);
  417. return 0;
  418. }
  419. static sense_reason_t
  420. iblock_execute_write_same(struct se_cmd *cmd)
  421. {
  422. struct block_device *bdev = IBLOCK_DEV(cmd->se_dev)->ibd_bd;
  423. struct iblock_req *ibr;
  424. struct scatterlist *sg;
  425. struct bio *bio;
  426. struct bio_list list;
  427. struct se_device *dev = cmd->se_dev;
  428. sector_t block_lba = target_to_linux_sector(dev, cmd->t_task_lba);
  429. sector_t sectors = target_to_linux_sector(dev,
  430. sbc_get_write_same_sectors(cmd));
  431. if (cmd->prot_op) {
  432. pr_err("WRITE_SAME: Protection information with IBLOCK"
  433. " backends not supported\n");
  434. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  435. }
  436. if (!cmd->t_data_nents)
  437. return TCM_INVALID_CDB_FIELD;
  438. sg = &cmd->t_data_sg[0];
  439. if (cmd->t_data_nents > 1 ||
  440. sg->length != cmd->se_dev->dev_attrib.block_size) {
  441. pr_err("WRITE_SAME: Illegal SGL t_data_nents: %u length: %u"
  442. " block_size: %u\n", cmd->t_data_nents, sg->length,
  443. cmd->se_dev->dev_attrib.block_size);
  444. return TCM_INVALID_CDB_FIELD;
  445. }
  446. if (bdev_write_zeroes_sectors(bdev)) {
  447. if (!iblock_execute_zero_out(bdev, cmd))
  448. return 0;
  449. }
  450. ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
  451. if (!ibr)
  452. goto fail;
  453. cmd->priv = ibr;
  454. bio = iblock_get_bio(cmd, block_lba, 1, REQ_OP_WRITE);
  455. if (!bio)
  456. goto fail_free_ibr;
  457. bio_list_init(&list);
  458. bio_list_add(&list, bio);
  459. refcount_set(&ibr->pending, 1);
  460. while (sectors) {
  461. while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
  462. != sg->length) {
  463. bio = iblock_get_bio(cmd, block_lba, 1, REQ_OP_WRITE);
  464. if (!bio)
  465. goto fail_put_bios;
  466. refcount_inc(&ibr->pending);
  467. bio_list_add(&list, bio);
  468. }
  469. /* Always in 512 byte units for Linux/Block */
  470. block_lba += sg->length >> SECTOR_SHIFT;
  471. sectors -= sg->length >> SECTOR_SHIFT;
  472. }
  473. iblock_submit_bios(&list);
  474. return 0;
  475. fail_put_bios:
  476. while ((bio = bio_list_pop(&list)))
  477. bio_put(bio);
  478. fail_free_ibr:
  479. kfree(ibr);
  480. fail:
  481. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  482. }
  483. enum {
  484. Opt_udev_path, Opt_readonly, Opt_force, Opt_err
  485. };
  486. static match_table_t tokens = {
  487. {Opt_udev_path, "udev_path=%s"},
  488. {Opt_readonly, "readonly=%d"},
  489. {Opt_force, "force=%d"},
  490. {Opt_err, NULL}
  491. };
  492. static ssize_t iblock_set_configfs_dev_params(struct se_device *dev,
  493. const char *page, ssize_t count)
  494. {
  495. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  496. char *orig, *ptr, *arg_p, *opts;
  497. substring_t args[MAX_OPT_ARGS];
  498. int ret = 0, token;
  499. unsigned long tmp_readonly;
  500. opts = kstrdup(page, GFP_KERNEL);
  501. if (!opts)
  502. return -ENOMEM;
  503. orig = opts;
  504. while ((ptr = strsep(&opts, ",\n")) != NULL) {
  505. if (!*ptr)
  506. continue;
  507. token = match_token(ptr, tokens, args);
  508. switch (token) {
  509. case Opt_udev_path:
  510. if (ib_dev->ibd_bd) {
  511. pr_err("Unable to set udev_path= while"
  512. " ib_dev->ibd_bd exists\n");
  513. ret = -EEXIST;
  514. goto out;
  515. }
  516. if (match_strlcpy(ib_dev->ibd_udev_path, &args[0],
  517. SE_UDEV_PATH_LEN) == 0) {
  518. ret = -EINVAL;
  519. break;
  520. }
  521. pr_debug("IBLOCK: Referencing UDEV path: %s\n",
  522. ib_dev->ibd_udev_path);
  523. ib_dev->ibd_flags |= IBDF_HAS_UDEV_PATH;
  524. break;
  525. case Opt_readonly:
  526. arg_p = match_strdup(&args[0]);
  527. if (!arg_p) {
  528. ret = -ENOMEM;
  529. break;
  530. }
  531. ret = kstrtoul(arg_p, 0, &tmp_readonly);
  532. kfree(arg_p);
  533. if (ret < 0) {
  534. pr_err("kstrtoul() failed for"
  535. " readonly=\n");
  536. goto out;
  537. }
  538. ib_dev->ibd_readonly = tmp_readonly;
  539. pr_debug("IBLOCK: readonly: %d\n", ib_dev->ibd_readonly);
  540. break;
  541. case Opt_force:
  542. break;
  543. default:
  544. break;
  545. }
  546. }
  547. out:
  548. kfree(orig);
  549. return (!ret) ? count : ret;
  550. }
  551. static ssize_t iblock_show_configfs_dev_params(struct se_device *dev, char *b)
  552. {
  553. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  554. struct block_device *bd = ib_dev->ibd_bd;
  555. ssize_t bl = 0;
  556. if (bd)
  557. bl += sprintf(b + bl, "iBlock device: %pg", bd);
  558. if (ib_dev->ibd_flags & IBDF_HAS_UDEV_PATH)
  559. bl += sprintf(b + bl, " UDEV PATH: %s",
  560. ib_dev->ibd_udev_path);
  561. bl += sprintf(b + bl, " readonly: %d\n", ib_dev->ibd_readonly);
  562. bl += sprintf(b + bl, " ");
  563. if (bd) {
  564. bl += sprintf(b + bl, "Major: %d Minor: %d %s\n",
  565. MAJOR(bd->bd_dev), MINOR(bd->bd_dev),
  566. "CLAIMED: IBLOCK");
  567. } else {
  568. bl += sprintf(b + bl, "Major: 0 Minor: 0\n");
  569. }
  570. return bl;
  571. }
  572. static int
  573. iblock_alloc_bip(struct se_cmd *cmd, struct bio *bio,
  574. struct sg_mapping_iter *miter)
  575. {
  576. struct se_device *dev = cmd->se_dev;
  577. struct blk_integrity *bi;
  578. struct bio_integrity_payload *bip;
  579. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  580. int rc;
  581. size_t resid, len;
  582. bi = bdev_get_integrity(ib_dev->ibd_bd);
  583. if (!bi) {
  584. pr_err("Unable to locate bio_integrity\n");
  585. return -ENODEV;
  586. }
  587. bip = bio_integrity_alloc(bio, GFP_NOIO, bio_max_segs(cmd->t_prot_nents));
  588. if (IS_ERR(bip)) {
  589. pr_err("Unable to allocate bio_integrity_payload\n");
  590. return PTR_ERR(bip);
  591. }
  592. /* virtual start sector must be in integrity interval units */
  593. bip_set_seed(bip, bio->bi_iter.bi_sector >>
  594. (bi->interval_exp - SECTOR_SHIFT));
  595. pr_debug("IBLOCK BIP Size: %u Sector: %llu\n", bip->bip_iter.bi_size,
  596. (unsigned long long)bip->bip_iter.bi_sector);
  597. resid = bio_integrity_bytes(bi, bio_sectors(bio));
  598. while (resid > 0 && sg_miter_next(miter)) {
  599. len = min_t(size_t, miter->length, resid);
  600. rc = bio_integrity_add_page(bio, miter->page, len,
  601. offset_in_page(miter->addr));
  602. if (rc != len) {
  603. pr_err("bio_integrity_add_page() failed; %d\n", rc);
  604. sg_miter_stop(miter);
  605. return -ENOMEM;
  606. }
  607. pr_debug("Added bio integrity page: %p length: %zu offset: %lu\n",
  608. miter->page, len, offset_in_page(miter->addr));
  609. resid -= len;
  610. if (len < miter->length)
  611. miter->consumed -= miter->length - len;
  612. }
  613. sg_miter_stop(miter);
  614. return 0;
  615. }
  616. static sense_reason_t
  617. iblock_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
  618. enum dma_data_direction data_direction)
  619. {
  620. struct se_device *dev = cmd->se_dev;
  621. sector_t block_lba = target_to_linux_sector(dev, cmd->t_task_lba);
  622. struct iblock_req *ibr;
  623. struct bio *bio;
  624. struct bio_list list;
  625. struct scatterlist *sg;
  626. u32 sg_num = sgl_nents;
  627. blk_opf_t opf;
  628. unsigned bio_cnt;
  629. int i, rc;
  630. struct sg_mapping_iter prot_miter;
  631. unsigned int miter_dir;
  632. if (data_direction == DMA_TO_DEVICE) {
  633. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  634. /*
  635. * Set bits to indicate WRITE_ODIRECT so we are not throttled
  636. * by WBT.
  637. */
  638. opf = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
  639. /*
  640. * Force writethrough using REQ_FUA if a volatile write cache
  641. * is not enabled, or if initiator set the Force Unit Access bit.
  642. */
  643. miter_dir = SG_MITER_TO_SG;
  644. if (bdev_fua(ib_dev->ibd_bd)) {
  645. if (cmd->se_cmd_flags & SCF_FUA)
  646. opf |= REQ_FUA;
  647. else if (!bdev_write_cache(ib_dev->ibd_bd))
  648. opf |= REQ_FUA;
  649. }
  650. } else {
  651. opf = REQ_OP_READ;
  652. miter_dir = SG_MITER_FROM_SG;
  653. }
  654. ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
  655. if (!ibr)
  656. goto fail;
  657. cmd->priv = ibr;
  658. if (!sgl_nents) {
  659. refcount_set(&ibr->pending, 1);
  660. iblock_complete_cmd(cmd, BLK_STS_OK);
  661. return 0;
  662. }
  663. bio = iblock_get_bio(cmd, block_lba, sgl_nents, opf);
  664. if (!bio)
  665. goto fail_free_ibr;
  666. bio_list_init(&list);
  667. bio_list_add(&list, bio);
  668. refcount_set(&ibr->pending, 2);
  669. bio_cnt = 1;
  670. if (cmd->prot_type && dev->dev_attrib.pi_prot_type)
  671. sg_miter_start(&prot_miter, cmd->t_prot_sg, cmd->t_prot_nents,
  672. miter_dir);
  673. for_each_sg(sgl, sg, sgl_nents, i) {
  674. /*
  675. * XXX: if the length the device accepts is shorter than the
  676. * length of the S/G list entry this will cause and
  677. * endless loop. Better hope no driver uses huge pages.
  678. */
  679. while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
  680. != sg->length) {
  681. if (cmd->prot_type && dev->dev_attrib.pi_prot_type) {
  682. rc = iblock_alloc_bip(cmd, bio, &prot_miter);
  683. if (rc)
  684. goto fail_put_bios;
  685. }
  686. if (bio_cnt >= IBLOCK_MAX_BIO_PER_TASK) {
  687. iblock_submit_bios(&list);
  688. bio_cnt = 0;
  689. }
  690. bio = iblock_get_bio(cmd, block_lba, sg_num, opf);
  691. if (!bio)
  692. goto fail_put_bios;
  693. refcount_inc(&ibr->pending);
  694. bio_list_add(&list, bio);
  695. bio_cnt++;
  696. }
  697. /* Always in 512 byte units for Linux/Block */
  698. block_lba += sg->length >> SECTOR_SHIFT;
  699. sg_num--;
  700. }
  701. if (cmd->prot_type && dev->dev_attrib.pi_prot_type) {
  702. rc = iblock_alloc_bip(cmd, bio, &prot_miter);
  703. if (rc)
  704. goto fail_put_bios;
  705. }
  706. iblock_submit_bios(&list);
  707. iblock_complete_cmd(cmd, BLK_STS_OK);
  708. return 0;
  709. fail_put_bios:
  710. while ((bio = bio_list_pop(&list)))
  711. bio_put(bio);
  712. fail_free_ibr:
  713. kfree(ibr);
  714. fail:
  715. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  716. }
  717. static sense_reason_t iblock_execute_pr_out(struct se_cmd *cmd, u8 sa, u64 key,
  718. u64 sa_key, u8 type, bool aptpl)
  719. {
  720. struct se_device *dev = cmd->se_dev;
  721. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  722. struct block_device *bdev = ib_dev->ibd_bd;
  723. const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
  724. int ret;
  725. if (!ops) {
  726. pr_err("Block device does not support pr_ops but iblock device has been configured for PR passthrough.\n");
  727. return TCM_UNSUPPORTED_SCSI_OPCODE;
  728. }
  729. switch (sa) {
  730. case PRO_REGISTER:
  731. case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
  732. if (!ops->pr_register) {
  733. pr_err("block device does not support pr_register.\n");
  734. return TCM_UNSUPPORTED_SCSI_OPCODE;
  735. }
  736. /* The block layer pr ops always enables aptpl */
  737. if (!aptpl)
  738. pr_info("APTPL not set by initiator, but will be used.\n");
  739. ret = ops->pr_register(bdev, key, sa_key,
  740. sa == PRO_REGISTER ? 0 : PR_FL_IGNORE_KEY);
  741. break;
  742. case PRO_RESERVE:
  743. if (!ops->pr_reserve) {
  744. pr_err("block_device does not support pr_reserve.\n");
  745. return TCM_UNSUPPORTED_SCSI_OPCODE;
  746. }
  747. ret = ops->pr_reserve(bdev, key, scsi_pr_type_to_block(type), 0);
  748. break;
  749. case PRO_CLEAR:
  750. if (!ops->pr_clear) {
  751. pr_err("block_device does not support pr_clear.\n");
  752. return TCM_UNSUPPORTED_SCSI_OPCODE;
  753. }
  754. ret = ops->pr_clear(bdev, key);
  755. break;
  756. case PRO_PREEMPT:
  757. case PRO_PREEMPT_AND_ABORT:
  758. if (!ops->pr_clear) {
  759. pr_err("block_device does not support pr_preempt.\n");
  760. return TCM_UNSUPPORTED_SCSI_OPCODE;
  761. }
  762. ret = ops->pr_preempt(bdev, key, sa_key,
  763. scsi_pr_type_to_block(type),
  764. sa == PRO_PREEMPT_AND_ABORT);
  765. break;
  766. case PRO_RELEASE:
  767. if (!ops->pr_clear) {
  768. pr_err("block_device does not support pr_pclear.\n");
  769. return TCM_UNSUPPORTED_SCSI_OPCODE;
  770. }
  771. ret = ops->pr_release(bdev, key, scsi_pr_type_to_block(type));
  772. break;
  773. default:
  774. pr_err("Unknown PERSISTENT_RESERVE_OUT SA: 0x%02x\n", sa);
  775. return TCM_UNSUPPORTED_SCSI_OPCODE;
  776. }
  777. if (!ret)
  778. return TCM_NO_SENSE;
  779. else if (ret == PR_STS_RESERVATION_CONFLICT)
  780. return TCM_RESERVATION_CONFLICT;
  781. else
  782. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  783. }
  784. static void iblock_pr_report_caps(unsigned char *param_data)
  785. {
  786. u16 len = 8;
  787. put_unaligned_be16(len, &param_data[0]);
  788. /*
  789. * When using the pr_ops passthrough method we only support exporting
  790. * the device through one target port because from the backend module
  791. * level we can't see the target port config. As a result we only
  792. * support registration directly from the I_T nexus the cmd is sent
  793. * through and do not set ATP_C here.
  794. *
  795. * The block layer pr_ops do not support passing in initiators so
  796. * we don't set SIP_C here.
  797. */
  798. /* PTPL_C: Persistence across Target Power Loss bit */
  799. param_data[2] |= 0x01;
  800. /*
  801. * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
  802. * set the TMV: Task Mask Valid bit.
  803. */
  804. param_data[3] |= 0x80;
  805. /*
  806. * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
  807. */
  808. param_data[3] |= 0x10; /* ALLOW COMMANDs field 001b */
  809. /*
  810. * PTPL_A: Persistence across Target Power Loss Active bit. The block
  811. * layer pr ops always enables this so report it active.
  812. */
  813. param_data[3] |= 0x01;
  814. /*
  815. * Setup the PERSISTENT RESERVATION TYPE MASK from Table 212 spc4r37.
  816. */
  817. param_data[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
  818. param_data[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
  819. param_data[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
  820. param_data[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
  821. param_data[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
  822. param_data[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
  823. }
  824. static sense_reason_t iblock_pr_read_keys(struct se_cmd *cmd,
  825. unsigned char *param_data)
  826. {
  827. struct se_device *dev = cmd->se_dev;
  828. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  829. struct block_device *bdev = ib_dev->ibd_bd;
  830. const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
  831. int i, len, paths, data_offset;
  832. struct pr_keys *keys;
  833. sense_reason_t ret;
  834. if (!ops) {
  835. pr_err("Block device does not support pr_ops but iblock device has been configured for PR passthrough.\n");
  836. return TCM_UNSUPPORTED_SCSI_OPCODE;
  837. }
  838. if (!ops->pr_read_keys) {
  839. pr_err("Block device does not support read_keys.\n");
  840. return TCM_UNSUPPORTED_SCSI_OPCODE;
  841. }
  842. /*
  843. * We don't know what's under us, but dm-multipath will register every
  844. * path with the same key, so start off with enough space for 16 paths.
  845. * which is not a lot of memory and should normally be enough.
  846. */
  847. paths = 16;
  848. retry:
  849. len = 8 * paths;
  850. keys = kzalloc(sizeof(*keys) + len, GFP_KERNEL);
  851. if (!keys)
  852. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  853. keys->num_keys = paths;
  854. if (!ops->pr_read_keys(bdev, keys)) {
  855. if (keys->num_keys > paths) {
  856. kfree(keys);
  857. paths *= 2;
  858. goto retry;
  859. }
  860. } else {
  861. ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  862. goto free_keys;
  863. }
  864. ret = TCM_NO_SENSE;
  865. put_unaligned_be32(keys->generation, &param_data[0]);
  866. if (!keys->num_keys) {
  867. put_unaligned_be32(0, &param_data[4]);
  868. goto free_keys;
  869. }
  870. put_unaligned_be32(8 * keys->num_keys, &param_data[4]);
  871. data_offset = 8;
  872. for (i = 0; i < keys->num_keys; i++) {
  873. if (data_offset + 8 > cmd->data_length)
  874. break;
  875. put_unaligned_be64(keys->keys[i], &param_data[data_offset]);
  876. data_offset += 8;
  877. }
  878. free_keys:
  879. kfree(keys);
  880. return ret;
  881. }
  882. static sense_reason_t iblock_pr_read_reservation(struct se_cmd *cmd,
  883. unsigned char *param_data)
  884. {
  885. struct se_device *dev = cmd->se_dev;
  886. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  887. struct block_device *bdev = ib_dev->ibd_bd;
  888. const struct pr_ops *ops = bdev->bd_disk->fops->pr_ops;
  889. struct pr_held_reservation rsv = { };
  890. if (!ops) {
  891. pr_err("Block device does not support pr_ops but iblock device has been configured for PR passthrough.\n");
  892. return TCM_UNSUPPORTED_SCSI_OPCODE;
  893. }
  894. if (!ops->pr_read_reservation) {
  895. pr_err("Block device does not support read_keys.\n");
  896. return TCM_UNSUPPORTED_SCSI_OPCODE;
  897. }
  898. if (ops->pr_read_reservation(bdev, &rsv))
  899. return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  900. put_unaligned_be32(rsv.generation, &param_data[0]);
  901. if (!block_pr_type_to_scsi(rsv.type)) {
  902. put_unaligned_be32(0, &param_data[4]);
  903. return TCM_NO_SENSE;
  904. }
  905. put_unaligned_be32(16, &param_data[4]);
  906. if (cmd->data_length < 16)
  907. return TCM_NO_SENSE;
  908. put_unaligned_be64(rsv.key, &param_data[8]);
  909. if (cmd->data_length < 22)
  910. return TCM_NO_SENSE;
  911. param_data[21] = block_pr_type_to_scsi(rsv.type);
  912. return TCM_NO_SENSE;
  913. }
  914. static sense_reason_t iblock_execute_pr_in(struct se_cmd *cmd, u8 sa,
  915. unsigned char *param_data)
  916. {
  917. sense_reason_t ret = TCM_NO_SENSE;
  918. switch (sa) {
  919. case PRI_REPORT_CAPABILITIES:
  920. iblock_pr_report_caps(param_data);
  921. break;
  922. case PRI_READ_KEYS:
  923. ret = iblock_pr_read_keys(cmd, param_data);
  924. break;
  925. case PRI_READ_RESERVATION:
  926. ret = iblock_pr_read_reservation(cmd, param_data);
  927. break;
  928. default:
  929. pr_err("Unknown PERSISTENT_RESERVE_IN SA: 0x%02x\n", sa);
  930. return TCM_UNSUPPORTED_SCSI_OPCODE;
  931. }
  932. return ret;
  933. }
  934. static sector_t iblock_get_alignment_offset_lbas(struct se_device *dev)
  935. {
  936. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  937. struct block_device *bd = ib_dev->ibd_bd;
  938. int ret;
  939. ret = bdev_alignment_offset(bd);
  940. if (ret == -1)
  941. return 0;
  942. /* convert offset-bytes to offset-lbas */
  943. return ret / bdev_logical_block_size(bd);
  944. }
  945. static unsigned int iblock_get_lbppbe(struct se_device *dev)
  946. {
  947. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  948. struct block_device *bd = ib_dev->ibd_bd;
  949. unsigned int logs_per_phys =
  950. bdev_physical_block_size(bd) / bdev_logical_block_size(bd);
  951. return ilog2(logs_per_phys);
  952. }
  953. static unsigned int iblock_get_io_min(struct se_device *dev)
  954. {
  955. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  956. struct block_device *bd = ib_dev->ibd_bd;
  957. return bdev_io_min(bd);
  958. }
  959. static unsigned int iblock_get_io_opt(struct se_device *dev)
  960. {
  961. struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
  962. struct block_device *bd = ib_dev->ibd_bd;
  963. return bdev_io_opt(bd);
  964. }
  965. static struct exec_cmd_ops iblock_exec_cmd_ops = {
  966. .execute_rw = iblock_execute_rw,
  967. .execute_sync_cache = iblock_execute_sync_cache,
  968. .execute_write_same = iblock_execute_write_same,
  969. .execute_unmap = iblock_execute_unmap,
  970. .execute_pr_out = iblock_execute_pr_out,
  971. .execute_pr_in = iblock_execute_pr_in,
  972. };
  973. static sense_reason_t
  974. iblock_parse_cdb(struct se_cmd *cmd)
  975. {
  976. return sbc_parse_cdb(cmd, &iblock_exec_cmd_ops);
  977. }
  978. static bool iblock_get_write_cache(struct se_device *dev)
  979. {
  980. return bdev_write_cache(IBLOCK_DEV(dev)->ibd_bd);
  981. }
  982. static const struct target_backend_ops iblock_ops = {
  983. .name = "iblock",
  984. .inquiry_prod = "IBLOCK",
  985. .transport_flags_changeable = TRANSPORT_FLAG_PASSTHROUGH_PGR,
  986. .inquiry_rev = IBLOCK_VERSION,
  987. .owner = THIS_MODULE,
  988. .attach_hba = iblock_attach_hba,
  989. .detach_hba = iblock_detach_hba,
  990. .alloc_device = iblock_alloc_device,
  991. .configure_device = iblock_configure_device,
  992. .destroy_device = iblock_destroy_device,
  993. .free_device = iblock_free_device,
  994. .configure_unmap = iblock_configure_unmap,
  995. .plug_device = iblock_plug_device,
  996. .unplug_device = iblock_unplug_device,
  997. .parse_cdb = iblock_parse_cdb,
  998. .set_configfs_dev_params = iblock_set_configfs_dev_params,
  999. .show_configfs_dev_params = iblock_show_configfs_dev_params,
  1000. .get_device_type = sbc_get_device_type,
  1001. .get_blocks = iblock_get_blocks,
  1002. .get_alignment_offset_lbas = iblock_get_alignment_offset_lbas,
  1003. .get_lbppbe = iblock_get_lbppbe,
  1004. .get_io_min = iblock_get_io_min,
  1005. .get_io_opt = iblock_get_io_opt,
  1006. .get_write_cache = iblock_get_write_cache,
  1007. .tb_dev_attrib_attrs = sbc_attrib_attrs,
  1008. };
  1009. static int __init iblock_module_init(void)
  1010. {
  1011. return transport_backend_register(&iblock_ops);
  1012. }
  1013. static void __exit iblock_module_exit(void)
  1014. {
  1015. target_backend_unregister(&iblock_ops);
  1016. }
  1017. MODULE_DESCRIPTION("TCM IBLOCK subsystem plugin");
  1018. MODULE_AUTHOR("nab@Linux-iSCSI.org");
  1019. MODULE_LICENSE("GPL");
  1020. module_init(iblock_module_init);
  1021. module_exit(iblock_module_exit);