mm: use bdev_is_zoned in claim_swapfile
[linux-block.git] / drivers / target / target_core_iblock.c
CommitLineData
1a59d1b8 1// SPDX-License-Identifier: GPL-2.0-or-later
c66ac9db
NB
2/*******************************************************************************
3 * Filename: target_core_iblock.c
4 *
5 * This file contains the Storage Engine <-> Linux BlockIO transport
6 * specific functions.
7 *
4c76251e 8 * (c) Copyright 2003-2013 Datera, Inc.
c66ac9db
NB
9 *
10 * Nicholas A. Bellinger <nab@kernel.org>
11 *
c66ac9db
NB
12 ******************************************************************************/
13
c66ac9db
NB
14#include <linux/string.h>
15#include <linux/parser.h>
16#include <linux/timer.h>
17#include <linux/fs.h>
18#include <linux/blkdev.h>
fe45e630 19#include <linux/blk-integrity.h>
c66ac9db
NB
20#include <linux/slab.h>
21#include <linux/spinlock.h>
c66ac9db 22#include <linux/bio.h>
c66ac9db 23#include <linux/file.h>
827509e3 24#include <linux/module.h>
24b83deb 25#include <linux/scatterlist.h>
ba929992 26#include <scsi/scsi_proto.h>
14150a6b 27#include <asm/unaligned.h>
c66ac9db
NB
28
29#include <target/target_core_base.h>
c4795fb2 30#include <target/target_core_backend.h>
c66ac9db
NB
31
32#include "target_core_iblock.h"
33
d5b4a21b
CH
34#define IBLOCK_MAX_BIO_PER_TASK 32 /* max # of bios to submit at a time */
35#define IBLOCK_BIO_POOL_SIZE 128
36
0fd97ccf
CH
37static inline struct iblock_dev *IBLOCK_DEV(struct se_device *dev)
38{
39 return container_of(dev, struct iblock_dev, dev);
40}
41
42
c66ac9db
NB
43static int iblock_attach_hba(struct se_hba *hba, u32 host_id)
44{
6708bb27 45 pr_debug("CORE_HBA[%d] - TCM iBlock HBA Driver %s on"
c66ac9db 46 " Generic Target Core Stack %s\n", hba->hba_id,
ce8dd25d 47 IBLOCK_VERSION, TARGET_CORE_VERSION);
c66ac9db
NB
48 return 0;
49}
50
51static void iblock_detach_hba(struct se_hba *hba)
52{
c66ac9db
NB
53}
54
0fd97ccf 55static struct se_device *iblock_alloc_device(struct se_hba *hba, const char *name)
c66ac9db
NB
56{
57 struct iblock_dev *ib_dev = NULL;
c66ac9db
NB
58
59 ib_dev = kzalloc(sizeof(struct iblock_dev), GFP_KERNEL);
6708bb27
AG
60 if (!ib_dev) {
61 pr_err("Unable to allocate struct iblock_dev\n");
c66ac9db
NB
62 return NULL;
63 }
c66ac9db 64
415ccd98
MC
65 ib_dev->ibd_plug = kcalloc(nr_cpu_ids, sizeof(*ib_dev->ibd_plug),
66 GFP_KERNEL);
67 if (!ib_dev->ibd_plug)
68 goto free_dev;
69
6708bb27 70 pr_debug( "IBLOCK: Allocated ib_dev for %s\n", name);
c66ac9db 71
0fd97ccf 72 return &ib_dev->dev;
415ccd98
MC
73
74free_dev:
75 kfree(ib_dev);
76 return NULL;
c66ac9db
NB
77}
78
0fd97ccf 79static int iblock_configure_device(struct se_device *dev)
c66ac9db 80{
0fd97ccf 81 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
c66ac9db 82 struct request_queue *q;
0fd97ccf 83 struct block_device *bd = NULL;
ecebbf6c 84 struct blk_integrity *bi;
44bfd018 85 fmode_t mode;
2237498f 86 unsigned int max_write_zeroes_sectors;
8f13142a 87 int ret;
c66ac9db 88
0fd97ccf
CH
89 if (!(ib_dev->ibd_flags & IBDF_HAS_UDEV_PATH)) {
90 pr_err("Missing udev_path= parameters for IBLOCK\n");
91 return -EINVAL;
c66ac9db 92 }
d5b4a21b 93
a47a28b7
KO
94 ret = bioset_init(&ib_dev->ibd_bio_set, IBLOCK_BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS);
95 if (ret) {
0fd97ccf
CH
96 pr_err("IBLOCK: Unable to create bioset\n");
97 goto out;
c66ac9db 98 }
0fd97ccf 99
6708bb27 100 pr_debug( "IBLOCK: Claiming struct block_device: %s\n",
c66ac9db
NB
101 ib_dev->ibd_udev_path);
102
44bfd018
AG
103 mode = FMODE_READ|FMODE_EXCL;
104 if (!ib_dev->ibd_readonly)
105 mode |= FMODE_WRITE;
eeeb9522
NB
106 else
107 dev->dev_flags |= DF_READ_ONLY;
44bfd018
AG
108
109 bd = blkdev_get_by_path(ib_dev->ibd_udev_path, mode, ib_dev);
613640e4
NB
110 if (IS_ERR(bd)) {
111 ret = PTR_ERR(bd);
0fd97ccf 112 goto out_free_bioset;
613640e4 113 }
c66ac9db
NB
114 ib_dev->ibd_bd = bd;
115
0fd97ccf
CH
116 q = bdev_get_queue(bd);
117
118 dev->dev_attrib.hw_block_size = bdev_logical_block_size(bd);
046ba642 119 dev->dev_attrib.hw_max_sectors = queue_max_hw_sectors(q);
0fd97ccf 120 dev->dev_attrib.hw_queue_depth = q->nr_requests;
c66ac9db 121
817e8b51 122 if (target_configure_unmap_from_queue(&dev->dev_attrib, bd))
6708bb27 123 pr_debug("IBLOCK: BLOCK Discard support available,"
8a9ebe71
MC
124 " disabled by default\n");
125
f6970ad3
NB
126 /*
127 * Enable write same emulation for IBLOCK and use 0xFFFF as
128 * the smaller WRITE_SAME(10) only has a two-byte block count.
129 */
2237498f
NB
130 max_write_zeroes_sectors = bdev_write_zeroes_sectors(bd);
131 if (max_write_zeroes_sectors)
132 dev->dev_attrib.max_write_same_len = max_write_zeroes_sectors;
133 else
134 dev->dev_attrib.max_write_same_len = 0xFFFF;
c66ac9db 135
e22a7f07 136 if (blk_queue_nonrot(q))
0fd97ccf 137 dev->dev_attrib.is_nonrot = 1;
d0c8b259 138
ecebbf6c
NB
139 bi = bdev_get_integrity(bd);
140 if (bi) {
a47a28b7 141 struct bio_set *bs = &ib_dev->ibd_bio_set;
ecebbf6c 142
0f8087ec
MP
143 if (!strcmp(bi->profile->name, "T10-DIF-TYPE3-IP") ||
144 !strcmp(bi->profile->name, "T10-DIF-TYPE1-IP")) {
ecebbf6c 145 pr_err("IBLOCK export of blk_integrity: %s not"
0f8087ec 146 " supported\n", bi->profile->name);
ecebbf6c
NB
147 ret = -ENOSYS;
148 goto out_blkdev_put;
149 }
150
0f8087ec 151 if (!strcmp(bi->profile->name, "T10-DIF-TYPE3-CRC")) {
ecebbf6c 152 dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE3_PROT;
0f8087ec 153 } else if (!strcmp(bi->profile->name, "T10-DIF-TYPE1-CRC")) {
ecebbf6c
NB
154 dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE1_PROT;
155 }
156
157 if (dev->dev_attrib.pi_prot_type) {
158 if (bioset_integrity_create(bs, IBLOCK_BIO_POOL_SIZE) < 0) {
159 pr_err("Unable to allocate bioset for PI\n");
160 ret = -ENOMEM;
161 goto out_blkdev_put;
162 }
163 pr_debug("IBLOCK setup BIP bs->bio_integrity_pool: %p\n",
f4f8154a 164 &bs->bio_integrity_pool);
ecebbf6c
NB
165 }
166 dev->dev_attrib.hw_pi_prot_type = dev->dev_attrib.pi_prot_type;
167 }
168
0fd97ccf 169 return 0;
c66ac9db 170
ecebbf6c
NB
171out_blkdev_put:
172 blkdev_put(ib_dev->ibd_bd, FMODE_WRITE|FMODE_READ|FMODE_EXCL);
0fd97ccf 173out_free_bioset:
a47a28b7 174 bioset_exit(&ib_dev->ibd_bio_set);
0fd97ccf
CH
175out:
176 return ret;
c66ac9db
NB
177}
178
4cc987ea
NB
179static void iblock_dev_call_rcu(struct rcu_head *p)
180{
181 struct se_device *dev = container_of(p, struct se_device, rcu_head);
182 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
183
415ccd98 184 kfree(ib_dev->ibd_plug);
4cc987ea
NB
185 kfree(ib_dev);
186}
187
0fd97ccf 188static void iblock_free_device(struct se_device *dev)
92634706
MC
189{
190 call_rcu(&dev->rcu_head, iblock_dev_call_rcu);
191}
192
193static void iblock_destroy_device(struct se_device *dev)
c66ac9db 194{
0fd97ccf 195 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
c66ac9db 196
bc665524
NB
197 if (ib_dev->ibd_bd != NULL)
198 blkdev_put(ib_dev->ibd_bd, FMODE_WRITE|FMODE_READ|FMODE_EXCL);
a47a28b7 199 bioset_exit(&ib_dev->ibd_bio_set);
c66ac9db
NB
200}
201
415ccd98
MC
202static struct se_dev_plug *iblock_plug_device(struct se_device *se_dev)
203{
204 struct iblock_dev *ib_dev = IBLOCK_DEV(se_dev);
205 struct iblock_dev_plug *ib_dev_plug;
206
207 /*
5aaeca25 208 * Each se_device has a per cpu work this can be run from. We
415ccd98
MC
209 * shouldn't have multiple threads on the same cpu calling this
210 * at the same time.
211 */
5aaeca25 212 ib_dev_plug = &ib_dev->ibd_plug[raw_smp_processor_id()];
415ccd98
MC
213 if (test_and_set_bit(IBD_PLUGF_PLUGGED, &ib_dev_plug->flags))
214 return NULL;
215
216 blk_start_plug(&ib_dev_plug->blk_plug);
217 return &ib_dev_plug->se_plug;
218}
219
220static void iblock_unplug_device(struct se_dev_plug *se_plug)
221{
222 struct iblock_dev_plug *ib_dev_plug = container_of(se_plug,
223 struct iblock_dev_plug, se_plug);
224
225 blk_finish_plug(&ib_dev_plug->blk_plug);
226 clear_bit(IBD_PLUGF_PLUGGED, &ib_dev_plug->flags);
227}
228
c66ac9db
NB
229static unsigned long long iblock_emulate_read_cap_with_block_size(
230 struct se_device *dev,
231 struct block_device *bd,
232 struct request_queue *q)
233{
c66ac9db 234 u32 block_size = bdev_logical_block_size(bd);
64f0f426
CH
235 unsigned long long blocks_long =
236 div_u64(bdev_nr_bytes(bd), block_size) - 1;
c66ac9db 237
0fd97ccf 238 if (block_size == dev->dev_attrib.block_size)
c66ac9db
NB
239 return blocks_long;
240
241 switch (block_size) {
242 case 4096:
0fd97ccf 243 switch (dev->dev_attrib.block_size) {
c66ac9db
NB
244 case 2048:
245 blocks_long <<= 1;
246 break;
247 case 1024:
248 blocks_long <<= 2;
249 break;
250 case 512:
251 blocks_long <<= 3;
492096ec 252 break;
c66ac9db
NB
253 default:
254 break;
255 }
256 break;
257 case 2048:
0fd97ccf 258 switch (dev->dev_attrib.block_size) {
c66ac9db
NB
259 case 4096:
260 blocks_long >>= 1;
261 break;
262 case 1024:
263 blocks_long <<= 1;
264 break;
265 case 512:
266 blocks_long <<= 2;
267 break;
268 default:
269 break;
270 }
271 break;
272 case 1024:
0fd97ccf 273 switch (dev->dev_attrib.block_size) {
c66ac9db
NB
274 case 4096:
275 blocks_long >>= 2;
276 break;
277 case 2048:
278 blocks_long >>= 1;
279 break;
280 case 512:
281 blocks_long <<= 1;
282 break;
283 default:
284 break;
285 }
286 break;
287 case 512:
0fd97ccf 288 switch (dev->dev_attrib.block_size) {
c66ac9db
NB
289 case 4096:
290 blocks_long >>= 3;
291 break;
292 case 2048:
293 blocks_long >>= 2;
294 break;
295 case 1024:
296 blocks_long >>= 1;
297 break;
298 default:
299 break;
300 }
301 break;
302 default:
303 break;
304 }
305
306 return blocks_long;
307}
308
3a41d85f
NB
309static void iblock_complete_cmd(struct se_cmd *cmd)
310{
311 struct iblock_req *ibr = cmd->priv;
312 u8 status;
313
5981c245 314 if (!refcount_dec_and_test(&ibr->pending))
3a41d85f
NB
315 return;
316
317 if (atomic_read(&ibr->ib_bio_err_cnt))
318 status = SAM_STAT_CHECK_CONDITION;
319 else
320 status = SAM_STAT_GOOD;
321
322 target_complete_cmd(cmd, status);
323 kfree(ibr);
324}
325
4246a0b6 326static void iblock_bio_done(struct bio *bio)
3a41d85f
NB
327{
328 struct se_cmd *cmd = bio->bi_private;
329 struct iblock_req *ibr = cmd->priv;
330
4e4cbee9
CH
331 if (bio->bi_status) {
332 pr_err("bio error: %p, err: %d\n", bio, bio->bi_status);
3a41d85f
NB
333 /*
334 * Bump the ib_bio_err_cnt and release bio.
335 */
336 atomic_inc(&ibr->ib_bio_err_cnt);
4e857c58 337 smp_mb__after_atomic();
3a41d85f
NB
338 }
339
340 bio_put(bio);
341
342 iblock_complete_cmd(cmd);
343}
344
bc9e0e36
CK
345static struct bio *iblock_get_bio(struct se_cmd *cmd, sector_t lba, u32 sg_num,
346 unsigned int opf)
3a41d85f
NB
347{
348 struct iblock_dev *ib_dev = IBLOCK_DEV(cmd->se_dev);
349 struct bio *bio;
350
351 /*
352 * Only allocate as many vector entries as the bio code allows us to,
353 * we'll loop later on until we have handled the whole request.
354 */
609be106
CH
355 bio = bio_alloc_bioset(ib_dev->ibd_bd, bio_max_segs(sg_num), opf,
356 GFP_NOIO, &ib_dev->ibd_bio_set);
3a41d85f
NB
357 if (!bio) {
358 pr_err("Unable to allocate memory for bio\n");
359 return NULL;
360 }
361
3a41d85f
NB
362 bio->bi_private = cmd;
363 bio->bi_end_io = &iblock_bio_done;
4f024f37 364 bio->bi_iter.bi_sector = lba;
3a41d85f
NB
365
366 return bio;
367}
368
4e49ea4a 369static void iblock_submit_bios(struct bio_list *list)
3a41d85f
NB
370{
371 struct blk_plug plug;
372 struct bio *bio;
415ccd98
MC
373 /*
374 * The block layer handles nested plugs, so just plug/unplug to handle
375 * fabric drivers that didn't support batching and multi bio cmds.
376 */
3a41d85f
NB
377 blk_start_plug(&plug);
378 while ((bio = bio_list_pop(list)))
4e49ea4a 379 submit_bio(bio);
3a41d85f
NB
380 blk_finish_plug(&plug);
381}
382
4246a0b6 383static void iblock_end_io_flush(struct bio *bio)
df5fa691
CH
384{
385 struct se_cmd *cmd = bio->bi_private;
386
4e4cbee9
CH
387 if (bio->bi_status)
388 pr_err("IBLOCK: cache flush failed: %d\n", bio->bi_status);
df5fa691 389
5787cacd 390 if (cmd) {
4e4cbee9 391 if (bio->bi_status)
5787cacd 392 target_complete_cmd(cmd, SAM_STAT_CHECK_CONDITION);
de103c93 393 else
5787cacd 394 target_complete_cmd(cmd, SAM_STAT_GOOD);
5787cacd
CH
395 }
396
df5fa691
CH
397 bio_put(bio);
398}
399
c66ac9db 400/*
df5fa691
CH
401 * Implement SYCHRONIZE CACHE. Note that we can't handle lba ranges and must
402 * always flush the whole cache.
c66ac9db 403 */
de103c93
CH
404static sense_reason_t
405iblock_execute_sync_cache(struct se_cmd *cmd)
c66ac9db 406{
0fd97ccf 407 struct iblock_dev *ib_dev = IBLOCK_DEV(cmd->se_dev);
a1d8b49a 408 int immed = (cmd->t_task_cdb[1] & 0x2);
df5fa691 409 struct bio *bio;
c66ac9db
NB
410
411 /*
412 * If the Immediate bit is set, queue up the GOOD response
df5fa691 413 * for this SYNCHRONIZE_CACHE op.
c66ac9db
NB
414 */
415 if (immed)
5787cacd 416 target_complete_cmd(cmd, SAM_STAT_GOOD);
c66ac9db 417
07888c66
CH
418 bio = bio_alloc(ib_dev->ibd_bd, 0, REQ_OP_WRITE | REQ_PREFLUSH,
419 GFP_KERNEL);
df5fa691 420 bio->bi_end_io = iblock_end_io_flush;
c66ac9db 421 if (!immed)
df5fa691 422 bio->bi_private = cmd;
4e49ea4a 423 submit_bio(bio);
ad67f0d9 424 return 0;
c66ac9db
NB
425}
426
dbc21c5a 427static sense_reason_t
62e46942 428iblock_execute_unmap(struct se_cmd *cmd, sector_t lba, sector_t nolb)
dbc21c5a 429{
62e46942 430 struct block_device *bdev = IBLOCK_DEV(cmd->se_dev)->ibd_bd;
8a9ebe71 431 struct se_device *dev = cmd->se_dev;
dbc21c5a
AH
432 int ret;
433
8a9ebe71
MC
434 ret = blkdev_issue_discard(bdev,
435 target_to_linux_sector(dev, lba),
436 target_to_linux_sector(dev, nolb),
437 GFP_KERNEL, 0);
dbc21c5a
AH
438 if (ret < 0) {
439 pr_err("blkdev_issue_discard() failed: %d\n", ret);
440 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
441 }
442
443 return 0;
444}
445
07b63196 446static sense_reason_t
2237498f 447iblock_execute_zero_out(struct block_device *bdev, struct se_cmd *cmd)
07b63196
MC
448{
449 struct se_device *dev = cmd->se_dev;
450 struct scatterlist *sg = &cmd->t_data_sg[0];
f5957dad
BL
451 unsigned char *buf, *not_zero;
452 int ret;
07b63196 453
2237498f
NB
454 buf = kmap(sg_page(sg)) + sg->offset;
455 if (!buf)
456 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
457 /*
458 * Fall back to block_execute_write_same() slow-path if
459 * incoming WRITE_SAME payload does not contain zeros.
460 */
f5957dad 461 not_zero = memchr_inv(buf, 0x00, cmd->data_length);
2237498f 462 kunmap(sg_page(sg));
07b63196 463
f5957dad 464 if (not_zero)
2237498f 465 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
07b63196 466
2237498f 467 ret = blkdev_issue_zeroout(bdev,
07b63196
MC
468 target_to_linux_sector(dev, cmd->t_task_lba),
469 target_to_linux_sector(dev,
470 sbc_get_write_same_sectors(cmd)),
1d2ff149 471 GFP_KERNEL, BLKDEV_ZERO_NOUNMAP);
07b63196
MC
472 if (ret)
473 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
474
14b40c1e 475 target_complete_cmd(cmd, SAM_STAT_GOOD);
07b63196
MC
476 return 0;
477}
478
f6970ad3
NB
479static sense_reason_t
480iblock_execute_write_same(struct se_cmd *cmd)
481{
07b63196 482 struct block_device *bdev = IBLOCK_DEV(cmd->se_dev)->ibd_bd;
f6970ad3
NB
483 struct iblock_req *ibr;
484 struct scatterlist *sg;
485 struct bio *bio;
486 struct bio_list list;
8a9ebe71
MC
487 struct se_device *dev = cmd->se_dev;
488 sector_t block_lba = target_to_linux_sector(dev, cmd->t_task_lba);
489 sector_t sectors = target_to_linux_sector(dev,
490 sbc_get_write_same_sectors(cmd));
f6970ad3 491
afd73f1b
NB
492 if (cmd->prot_op) {
493 pr_err("WRITE_SAME: Protection information with IBLOCK"
494 " backends not supported\n");
495 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
496 }
f6970ad3
NB
497 sg = &cmd->t_data_sg[0];
498
499 if (cmd->t_data_nents > 1 ||
500 sg->length != cmd->se_dev->dev_attrib.block_size) {
501 pr_err("WRITE_SAME: Illegal SGL t_data_nents: %u length: %u"
502 " block_size: %u\n", cmd->t_data_nents, sg->length,
503 cmd->se_dev->dev_attrib.block_size);
504 return TCM_INVALID_CDB_FIELD;
505 }
506
2237498f
NB
507 if (bdev_write_zeroes_sectors(bdev)) {
508 if (!iblock_execute_zero_out(bdev, cmd))
509 return 0;
510 }
07b63196 511
f6970ad3
NB
512 ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
513 if (!ibr)
514 goto fail;
515 cmd->priv = ibr;
516
bc9e0e36 517 bio = iblock_get_bio(cmd, block_lba, 1, REQ_OP_WRITE);
f6970ad3
NB
518 if (!bio)
519 goto fail_free_ibr;
520
521 bio_list_init(&list);
522 bio_list_add(&list, bio);
523
5981c245 524 refcount_set(&ibr->pending, 1);
f6970ad3
NB
525
526 while (sectors) {
527 while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
528 != sg->length) {
529
bc9e0e36 530 bio = iblock_get_bio(cmd, block_lba, 1, REQ_OP_WRITE);
f6970ad3
NB
531 if (!bio)
532 goto fail_put_bios;
533
5981c245 534 refcount_inc(&ibr->pending);
f6970ad3
NB
535 bio_list_add(&list, bio);
536 }
537
538 /* Always in 512 byte units for Linux/Block */
80b045b3 539 block_lba += sg->length >> SECTOR_SHIFT;
5676234f 540 sectors -= sg->length >> SECTOR_SHIFT;
f6970ad3
NB
541 }
542
4e49ea4a 543 iblock_submit_bios(&list);
f6970ad3
NB
544 return 0;
545
546fail_put_bios:
547 while ((bio = bio_list_pop(&list)))
548 bio_put(bio);
549fail_free_ibr:
550 kfree(ibr);
551fail:
552 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
553}
554
c66ac9db 555enum {
44bfd018 556 Opt_udev_path, Opt_readonly, Opt_force, Opt_err
c66ac9db
NB
557};
558
559static match_table_t tokens = {
560 {Opt_udev_path, "udev_path=%s"},
44bfd018 561 {Opt_readonly, "readonly=%d"},
c66ac9db
NB
562 {Opt_force, "force=%d"},
563 {Opt_err, NULL}
564};
565
0fd97ccf
CH
566static ssize_t iblock_set_configfs_dev_params(struct se_device *dev,
567 const char *page, ssize_t count)
c66ac9db 568{
0fd97ccf 569 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
6d180253 570 char *orig, *ptr, *arg_p, *opts;
c66ac9db 571 substring_t args[MAX_OPT_ARGS];
21bca31c 572 int ret = 0, token;
44bfd018 573 unsigned long tmp_readonly;
c66ac9db
NB
574
575 opts = kstrdup(page, GFP_KERNEL);
576 if (!opts)
577 return -ENOMEM;
578
579 orig = opts;
580
90c161b6 581 while ((ptr = strsep(&opts, ",\n")) != NULL) {
c66ac9db
NB
582 if (!*ptr)
583 continue;
584
585 token = match_token(ptr, tokens, args);
586 switch (token) {
587 case Opt_udev_path:
588 if (ib_dev->ibd_bd) {
6708bb27 589 pr_err("Unable to set udev_path= while"
c66ac9db
NB
590 " ib_dev->ibd_bd exists\n");
591 ret = -EEXIST;
592 goto out;
593 }
852b6ed1
NB
594 if (match_strlcpy(ib_dev->ibd_udev_path, &args[0],
595 SE_UDEV_PATH_LEN) == 0) {
596 ret = -EINVAL;
6d180253
JJ
597 break;
598 }
6708bb27 599 pr_debug("IBLOCK: Referencing UDEV path: %s\n",
c66ac9db
NB
600 ib_dev->ibd_udev_path);
601 ib_dev->ibd_flags |= IBDF_HAS_UDEV_PATH;
602 break;
44bfd018
AG
603 case Opt_readonly:
604 arg_p = match_strdup(&args[0]);
605 if (!arg_p) {
606 ret = -ENOMEM;
607 break;
608 }
57103d7f 609 ret = kstrtoul(arg_p, 0, &tmp_readonly);
44bfd018
AG
610 kfree(arg_p);
611 if (ret < 0) {
57103d7f 612 pr_err("kstrtoul() failed for"
44bfd018
AG
613 " readonly=\n");
614 goto out;
615 }
616 ib_dev->ibd_readonly = tmp_readonly;
617 pr_debug("IBLOCK: readonly: %d\n", ib_dev->ibd_readonly);
618 break;
c66ac9db 619 case Opt_force:
c66ac9db
NB
620 break;
621 default:
622 break;
623 }
624 }
625
626out:
627 kfree(orig);
628 return (!ret) ? count : ret;
629}
630
0fd97ccf 631static ssize_t iblock_show_configfs_dev_params(struct se_device *dev, char *b)
c66ac9db 632{
0fd97ccf
CH
633 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
634 struct block_device *bd = ib_dev->ibd_bd;
c66ac9db
NB
635 ssize_t bl = 0;
636
637 if (bd)
1b74ab77 638 bl += sprintf(b + bl, "iBlock device: %pg", bd);
0fd97ccf 639 if (ib_dev->ibd_flags & IBDF_HAS_UDEV_PATH)
44bfd018 640 bl += sprintf(b + bl, " UDEV PATH: %s",
0fd97ccf
CH
641 ib_dev->ibd_udev_path);
642 bl += sprintf(b + bl, " readonly: %d\n", ib_dev->ibd_readonly);
c66ac9db
NB
643
644 bl += sprintf(b + bl, " ");
645 if (bd) {
646 bl += sprintf(b + bl, "Major: %d Minor: %d %s\n",
57ba1059
CH
647 MAJOR(bd->bd_dev), MINOR(bd->bd_dev),
648 "CLAIMED: IBLOCK");
c66ac9db 649 } else {
21bca31c 650 bl += sprintf(b + bl, "Major: 0 Minor: 0\n");
c66ac9db
NB
651 }
652
653 return bl;
654}
655
ecebbf6c 656static int
fed564f6
GE
657iblock_alloc_bip(struct se_cmd *cmd, struct bio *bio,
658 struct sg_mapping_iter *miter)
ecebbf6c
NB
659{
660 struct se_device *dev = cmd->se_dev;
661 struct blk_integrity *bi;
662 struct bio_integrity_payload *bip;
663 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
fed564f6
GE
664 int rc;
665 size_t resid, len;
ecebbf6c
NB
666
667 bi = bdev_get_integrity(ib_dev->ibd_bd);
668 if (!bi) {
669 pr_err("Unable to locate bio_integrity\n");
670 return -ENODEV;
671 }
672
5f7136db 673 bip = bio_integrity_alloc(bio, GFP_NOIO, bio_max_segs(cmd->t_prot_nents));
06c1e390 674 if (IS_ERR(bip)) {
ecebbf6c 675 pr_err("Unable to allocate bio_integrity_payload\n");
06c1e390 676 return PTR_ERR(bip);
ecebbf6c
NB
677 }
678
fed564f6 679 bip->bip_iter.bi_size = bio_integrity_bytes(bi, bio_sectors(bio));
e4dc9a4c
IR
680 /* virtual start sector must be in integrity interval units */
681 bip_set_seed(bip, bio->bi_iter.bi_sector >>
682 (bi->interval_exp - SECTOR_SHIFT));
ecebbf6c 683
4e13c5d0
LT
684 pr_debug("IBLOCK BIP Size: %u Sector: %llu\n", bip->bip_iter.bi_size,
685 (unsigned long long)bip->bip_iter.bi_sector);
ecebbf6c 686
fed564f6
GE
687 resid = bip->bip_iter.bi_size;
688 while (resid > 0 && sg_miter_next(miter)) {
ecebbf6c 689
fed564f6
GE
690 len = min_t(size_t, miter->length, resid);
691 rc = bio_integrity_add_page(bio, miter->page, len,
692 offset_in_page(miter->addr));
693 if (rc != len) {
ecebbf6c 694 pr_err("bio_integrity_add_page() failed; %d\n", rc);
fed564f6 695 sg_miter_stop(miter);
ecebbf6c
NB
696 return -ENOMEM;
697 }
698
fed564f6
GE
699 pr_debug("Added bio integrity page: %p length: %zu offset: %lu\n",
700 miter->page, len, offset_in_page(miter->addr));
701
702 resid -= len;
703 if (len < miter->length)
704 miter->consumed -= miter->length - len;
ecebbf6c 705 }
fed564f6 706 sg_miter_stop(miter);
ecebbf6c
NB
707
708 return 0;
709}
710
de103c93 711static sense_reason_t
a82a9538
NB
712iblock_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,
713 enum dma_data_direction data_direction)
c66ac9db 714{
5951146d 715 struct se_device *dev = cmd->se_dev;
8a9ebe71 716 sector_t block_lba = target_to_linux_sector(dev, cmd->t_task_lba);
5787cacd 717 struct iblock_req *ibr;
fed564f6 718 struct bio *bio;
dbbf3e94 719 struct bio_list list;
c66ac9db 720 struct scatterlist *sg;
5787cacd 721 u32 sg_num = sgl_nents;
bc9e0e36 722 unsigned int opf;
d5b4a21b 723 unsigned bio_cnt;
bc9e0e36 724 int i, rc;
fed564f6 725 struct sg_mapping_iter prot_miter;
bc9e0e36 726 unsigned int miter_dir;
dbbf3e94 727
5787cacd 728 if (data_direction == DMA_TO_DEVICE) {
d0c8b259
NB
729 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
730 struct request_queue *q = bdev_get_queue(ib_dev->ibd_bd);
dbbf3e94 731 /*
70fd7614 732 * Force writethrough using REQ_FUA if a volatile write cache
d0c8b259 733 * is not enabled, or if initiator set the Force Unit Access bit.
dbbf3e94 734 */
bc9e0e36
CK
735 opf = REQ_OP_WRITE;
736 miter_dir = SG_MITER_TO_SG;
c888a8f9 737 if (test_bit(QUEUE_FLAG_FUA, &q->queue_flags)) {
d0c8b259 738 if (cmd->se_cmd_flags & SCF_FUA)
bc9e0e36 739 opf |= REQ_FUA;
c888a8f9 740 else if (!test_bit(QUEUE_FLAG_WC, &q->queue_flags))
bc9e0e36 741 opf |= REQ_FUA;
d0c8b259 742 }
dbbf3e94 743 } else {
bc9e0e36
CK
744 opf = REQ_OP_READ;
745 miter_dir = SG_MITER_FROM_SG;
dbbf3e94
CH
746 }
747
5787cacd
CH
748 ibr = kzalloc(sizeof(struct iblock_req), GFP_KERNEL);
749 if (!ibr)
750 goto fail;
751 cmd->priv = ibr;
752
e0de4457 753 if (!sgl_nents) {
5981c245 754 refcount_set(&ibr->pending, 1);
e0de4457
PB
755 iblock_complete_cmd(cmd);
756 return 0;
757 }
758
bc9e0e36 759 bio = iblock_get_bio(cmd, block_lba, sgl_nents, opf);
5787cacd
CH
760 if (!bio)
761 goto fail_free_ibr;
dbbf3e94
CH
762
763 bio_list_init(&list);
764 bio_list_add(&list, bio);
5787cacd 765
5981c245 766 refcount_set(&ibr->pending, 2);
d5b4a21b 767 bio_cnt = 1;
c66ac9db 768
fed564f6
GE
769 if (cmd->prot_type && dev->dev_attrib.pi_prot_type)
770 sg_miter_start(&prot_miter, cmd->t_prot_sg, cmd->t_prot_nents,
bc9e0e36 771 miter_dir);
fed564f6 772
5787cacd 773 for_each_sg(sgl, sg, sgl_nents, i) {
dbbf3e94
CH
774 /*
775 * XXX: if the length the device accepts is shorter than the
776 * length of the S/G list entry this will cause and
777 * endless loop. Better hope no driver uses huge pages.
778 */
779 while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
780 != sg->length) {
fed564f6
GE
781 if (cmd->prot_type && dev->dev_attrib.pi_prot_type) {
782 rc = iblock_alloc_bip(cmd, bio, &prot_miter);
783 if (rc)
784 goto fail_put_bios;
785 }
786
d5b4a21b 787 if (bio_cnt >= IBLOCK_MAX_BIO_PER_TASK) {
4e49ea4a 788 iblock_submit_bios(&list);
d5b4a21b
CH
789 bio_cnt = 0;
790 }
791
bc9e0e36 792 bio = iblock_get_bio(cmd, block_lba, sg_num, opf);
6708bb27 793 if (!bio)
5787cacd
CH
794 goto fail_put_bios;
795
5981c245 796 refcount_inc(&ibr->pending);
dbbf3e94 797 bio_list_add(&list, bio);
d5b4a21b 798 bio_cnt++;
c66ac9db 799 }
dbbf3e94 800
c66ac9db 801 /* Always in 512 byte units for Linux/Block */
80b045b3 802 block_lba += sg->length >> SECTOR_SHIFT;
c66ac9db 803 sg_num--;
c66ac9db
NB
804 }
805
6f16ec43 806 if (cmd->prot_type && dev->dev_attrib.pi_prot_type) {
fed564f6 807 rc = iblock_alloc_bip(cmd, bio, &prot_miter);
ecebbf6c
NB
808 if (rc)
809 goto fail_put_bios;
810 }
811
4e49ea4a 812 iblock_submit_bios(&list);
5787cacd 813 iblock_complete_cmd(cmd);
03e98c9e 814 return 0;
dbbf3e94 815
5787cacd 816fail_put_bios:
dbbf3e94 817 while ((bio = bio_list_pop(&list)))
c66ac9db 818 bio_put(bio);
5787cacd
CH
819fail_free_ibr:
820 kfree(ibr);
5787cacd 821fail:
de103c93 822 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
c66ac9db
NB
823}
824
c66ac9db
NB
825static sector_t iblock_get_blocks(struct se_device *dev)
826{
0fd97ccf
CH
827 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
828 struct block_device *bd = ib_dev->ibd_bd;
c66ac9db
NB
829 struct request_queue *q = bdev_get_queue(bd);
830
831 return iblock_emulate_read_cap_with_block_size(dev, bd, q);
832}
833
7f7caf6a
AG
834static sector_t iblock_get_alignment_offset_lbas(struct se_device *dev)
835{
836 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
837 struct block_device *bd = ib_dev->ibd_bd;
838 int ret;
839
840 ret = bdev_alignment_offset(bd);
841 if (ret == -1)
842 return 0;
843
844 /* convert offset-bytes to offset-lbas */
845 return ret / bdev_logical_block_size(bd);
846}
847
848static unsigned int iblock_get_lbppbe(struct se_device *dev)
849{
850 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
851 struct block_device *bd = ib_dev->ibd_bd;
c151eddb 852 unsigned int logs_per_phys =
a2c6c6a3 853 bdev_physical_block_size(bd) / bdev_logical_block_size(bd);
7f7caf6a
AG
854
855 return ilog2(logs_per_phys);
856}
857
858static unsigned int iblock_get_io_min(struct se_device *dev)
859{
860 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
861 struct block_device *bd = ib_dev->ibd_bd;
862
863 return bdev_io_min(bd);
864}
865
866static unsigned int iblock_get_io_opt(struct se_device *dev)
867{
868 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
869 struct block_device *bd = ib_dev->ibd_bd;
870
871 return bdev_io_opt(bd);
872}
873
9e999a6c 874static struct sbc_ops iblock_sbc_ops = {
0c2ad7d1 875 .execute_rw = iblock_execute_rw,
ad67f0d9 876 .execute_sync_cache = iblock_execute_sync_cache,
6f974e8c 877 .execute_write_same = iblock_execute_write_same,
14150a6b 878 .execute_unmap = iblock_execute_unmap,
0c2ad7d1
CH
879};
880
de103c93
CH
881static sense_reason_t
882iblock_parse_cdb(struct se_cmd *cmd)
0c2ad7d1 883{
9e999a6c 884 return sbc_parse_cdb(cmd, &iblock_sbc_ops);
0c2ad7d1
CH
885}
886
452e2010 887static bool iblock_get_write_cache(struct se_device *dev)
d0c8b259
NB
888{
889 struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
890 struct block_device *bd = ib_dev->ibd_bd;
891 struct request_queue *q = bdev_get_queue(bd);
892
c888a8f9 893 return test_bit(QUEUE_FLAG_WC, &q->queue_flags);
d0c8b259
NB
894}
895
0a06d430 896static const struct target_backend_ops iblock_ops = {
c66ac9db 897 .name = "iblock",
0fd97ccf
CH
898 .inquiry_prod = "IBLOCK",
899 .inquiry_rev = IBLOCK_VERSION,
c66ac9db 900 .owner = THIS_MODULE,
c66ac9db
NB
901 .attach_hba = iblock_attach_hba,
902 .detach_hba = iblock_detach_hba,
0fd97ccf
CH
903 .alloc_device = iblock_alloc_device,
904 .configure_device = iblock_configure_device,
92634706 905 .destroy_device = iblock_destroy_device,
c66ac9db 906 .free_device = iblock_free_device,
415ccd98
MC
907 .plug_device = iblock_plug_device,
908 .unplug_device = iblock_unplug_device,
0c2ad7d1 909 .parse_cdb = iblock_parse_cdb,
c66ac9db
NB
910 .set_configfs_dev_params = iblock_set_configfs_dev_params,
911 .show_configfs_dev_params = iblock_show_configfs_dev_params,
6f23ac8a 912 .get_device_type = sbc_get_device_type,
c66ac9db 913 .get_blocks = iblock_get_blocks,
7f7caf6a
AG
914 .get_alignment_offset_lbas = iblock_get_alignment_offset_lbas,
915 .get_lbppbe = iblock_get_lbppbe,
916 .get_io_min = iblock_get_io_min,
917 .get_io_opt = iblock_get_io_opt,
d0c8b259 918 .get_write_cache = iblock_get_write_cache,
5873c4d1 919 .tb_dev_attrib_attrs = sbc_attrib_attrs,
c66ac9db
NB
920};
921
922static int __init iblock_module_init(void)
923{
0a06d430 924 return transport_backend_register(&iblock_ops);
c66ac9db
NB
925}
926
63b91d5a 927static void __exit iblock_module_exit(void)
c66ac9db 928{
0a06d430 929 target_backend_unregister(&iblock_ops);
c66ac9db
NB
930}
931
932MODULE_DESCRIPTION("TCM IBLOCK subsystem plugin");
933MODULE_AUTHOR("nab@Linux-iSCSI.org");
934MODULE_LICENSE("GPL");
935
936module_init(iblock_module_init);
937module_exit(iblock_module_exit);