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