target_core_iblock.c 30 KB

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