1 /*******************************************************************************
2 * Filename: target_core_iblock.c
4 * This file contains the Storage Engine <-> Linux BlockIO transport
7 * Copyright (c) 2003, 2004, 2005 PyX Technologies, Inc.
8 * Copyright (c) 2005, 2006, 2007 SBE, Inc.
9 * Copyright (c) 2007-2010 Rising Tide Systems
10 * Copyright (c) 2008-2010 Linux-iSCSI.org
12 * Nicholas A. Bellinger <nab@kernel.org>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 ******************************************************************************/
30 #include <linux/string.h>
31 #include <linux/parser.h>
32 #include <linux/timer.h>
34 #include <linux/blkdev.h>
35 #include <linux/slab.h>
36 #include <linux/spinlock.h>
37 #include <linux/bio.h>
38 #include <linux/genhd.h>
39 #include <linux/file.h>
40 #include <linux/module.h>
41 #include <scsi/scsi.h>
42 #include <scsi/scsi_host.h>
43 #include <asm/unaligned.h>
45 #include <target/target_core_base.h>
46 #include <target/target_core_backend.h>
48 #include "target_core_iblock.h"
50 #define IBLOCK_MAX_BIO_PER_TASK 32 /* max # of bios to submit at a time */
51 #define IBLOCK_BIO_POOL_SIZE 128
53 static struct se_subsystem_api iblock_template
;
55 static void iblock_bio_done(struct bio
*, int);
57 /* iblock_attach_hba(): (Part of se_subsystem_api_t template)
61 static int iblock_attach_hba(struct se_hba
*hba
, u32 host_id
)
63 pr_debug("CORE_HBA[%d] - TCM iBlock HBA Driver %s on"
64 " Generic Target Core Stack %s\n", hba
->hba_id
,
65 IBLOCK_VERSION
, TARGET_CORE_MOD_VERSION
);
69 static void iblock_detach_hba(struct se_hba
*hba
)
73 static void *iblock_allocate_virtdevice(struct se_hba
*hba
, const char *name
)
75 struct iblock_dev
*ib_dev
= NULL
;
77 ib_dev
= kzalloc(sizeof(struct iblock_dev
), GFP_KERNEL
);
79 pr_err("Unable to allocate struct iblock_dev\n");
83 pr_debug( "IBLOCK: Allocated ib_dev for %s\n", name
);
88 static struct se_device
*iblock_create_virtdevice(
90 struct se_subsystem_dev
*se_dev
,
93 struct iblock_dev
*ib_dev
= p
;
94 struct se_device
*dev
;
95 struct se_dev_limits dev_limits
;
96 struct block_device
*bd
= NULL
;
97 struct request_queue
*q
;
98 struct queue_limits
*limits
;
104 pr_err("Unable to locate struct iblock_dev parameter\n");
107 memset(&dev_limits
, 0, sizeof(struct se_dev_limits
));
109 ib_dev
->ibd_bio_set
= bioset_create(IBLOCK_BIO_POOL_SIZE
, 0);
110 if (!ib_dev
->ibd_bio_set
) {
111 pr_err("IBLOCK: Unable to create bioset()\n");
112 return ERR_PTR(-ENOMEM
);
114 pr_debug("IBLOCK: Created bio_set()\n");
116 * iblock_check_configfs_dev_params() ensures that ib_dev->ibd_udev_path
117 * must already have been set in order for echo 1 > $HBA/$DEV/enable to run.
119 pr_debug( "IBLOCK: Claiming struct block_device: %s\n",
120 ib_dev
->ibd_udev_path
);
122 mode
= FMODE_READ
|FMODE_EXCL
;
123 if (!ib_dev
->ibd_readonly
)
126 bd
= blkdev_get_by_path(ib_dev
->ibd_udev_path
, mode
, ib_dev
);
132 * Setup the local scope queue_limits from struct request_queue->limits
133 * to pass into transport_add_device_to_core_hba() as struct se_dev_limits.
135 q
= bdev_get_queue(bd
);
136 limits
= &dev_limits
.limits
;
137 limits
->logical_block_size
= bdev_logical_block_size(bd
);
138 limits
->max_hw_sectors
= UINT_MAX
;
139 limits
->max_sectors
= UINT_MAX
;
140 dev_limits
.hw_queue_depth
= q
->nr_requests
;
141 dev_limits
.queue_depth
= q
->nr_requests
;
145 dev
= transport_add_device_to_core_hba(hba
,
146 &iblock_template
, se_dev
, dev_flags
, ib_dev
,
147 &dev_limits
, "IBLOCK", IBLOCK_VERSION
);
152 * Check if the underlying struct block_device request_queue supports
153 * the QUEUE_FLAG_DISCARD bit for UNMAP/WRITE_SAME in SCSI + TRIM
154 * in ATA and we need to set TPE=1
156 if (blk_queue_discard(q
)) {
157 dev
->se_sub_dev
->se_dev_attrib
.max_unmap_lba_count
=
158 q
->limits
.max_discard_sectors
;
160 * Currently hardcoded to 1 in Linux/SCSI code..
162 dev
->se_sub_dev
->se_dev_attrib
.max_unmap_block_desc_count
= 1;
163 dev
->se_sub_dev
->se_dev_attrib
.unmap_granularity
=
164 q
->limits
.discard_granularity
>> 9;
165 dev
->se_sub_dev
->se_dev_attrib
.unmap_granularity_alignment
=
166 q
->limits
.discard_alignment
;
168 pr_debug("IBLOCK: BLOCK Discard support available,"
169 " disabled by default\n");
172 if (blk_queue_nonrot(q
))
173 dev
->se_sub_dev
->se_dev_attrib
.is_nonrot
= 1;
178 if (ib_dev
->ibd_bio_set
) {
179 bioset_free(ib_dev
->ibd_bio_set
);
180 ib_dev
->ibd_bio_set
= NULL
;
182 ib_dev
->ibd_bd
= NULL
;
186 static void iblock_free_device(void *p
)
188 struct iblock_dev
*ib_dev
= p
;
190 if (ib_dev
->ibd_bd
!= NULL
)
191 blkdev_put(ib_dev
->ibd_bd
, FMODE_WRITE
|FMODE_READ
|FMODE_EXCL
);
192 if (ib_dev
->ibd_bio_set
!= NULL
)
193 bioset_free(ib_dev
->ibd_bio_set
);
197 static unsigned long long iblock_emulate_read_cap_with_block_size(
198 struct se_device
*dev
,
199 struct block_device
*bd
,
200 struct request_queue
*q
)
202 unsigned long long blocks_long
= (div_u64(i_size_read(bd
->bd_inode
),
203 bdev_logical_block_size(bd
)) - 1);
204 u32 block_size
= bdev_logical_block_size(bd
);
206 if (block_size
== dev
->se_sub_dev
->se_dev_attrib
.block_size
)
209 switch (block_size
) {
211 switch (dev
->se_sub_dev
->se_dev_attrib
.block_size
) {
225 switch (dev
->se_sub_dev
->se_dev_attrib
.block_size
) {
240 switch (dev
->se_sub_dev
->se_dev_attrib
.block_size
) {
255 switch (dev
->se_sub_dev
->se_dev_attrib
.block_size
) {
276 static void iblock_end_io_flush(struct bio
*bio
, int err
)
278 struct se_cmd
*cmd
= bio
->bi_private
;
281 pr_err("IBLOCK: cache flush failed: %d\n", err
);
285 cmd
->scsi_sense_reason
=
286 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
287 target_complete_cmd(cmd
, SAM_STAT_CHECK_CONDITION
);
289 target_complete_cmd(cmd
, SAM_STAT_GOOD
);
297 * Implement SYCHRONIZE CACHE. Note that we can't handle lba ranges and must
298 * always flush the whole cache.
300 static int iblock_execute_sync_cache(struct se_cmd
*cmd
)
302 struct iblock_dev
*ib_dev
= cmd
->se_dev
->dev_ptr
;
303 int immed
= (cmd
->t_task_cdb
[1] & 0x2);
307 * If the Immediate bit is set, queue up the GOOD response
308 * for this SYNCHRONIZE_CACHE op.
311 target_complete_cmd(cmd
, SAM_STAT_GOOD
);
313 bio
= bio_alloc(GFP_KERNEL
, 0);
314 bio
->bi_end_io
= iblock_end_io_flush
;
315 bio
->bi_bdev
= ib_dev
->ibd_bd
;
317 bio
->bi_private
= cmd
;
318 submit_bio(WRITE_FLUSH
, bio
);
322 static int iblock_execute_unmap(struct se_cmd
*cmd
)
324 struct se_device
*dev
= cmd
->se_dev
;
325 struct iblock_dev
*ibd
= dev
->dev_ptr
;
326 unsigned char *buf
, *ptr
= NULL
;
333 if (cmd
->data_length
< 8) {
334 pr_warn("UNMAP parameter list length %u too small\n",
336 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
340 buf
= transport_kmap_data_sg(cmd
);
342 dl
= get_unaligned_be16(&buf
[0]);
343 bd_dl
= get_unaligned_be16(&buf
[2]);
345 size
= cmd
->data_length
- 8;
347 pr_warn("UNMAP parameter list length %u too small, ignoring bd_dl %u\n",
348 cmd
->data_length
, bd_dl
);
352 if (size
/ 16 > dev
->se_sub_dev
->se_dev_attrib
.max_unmap_block_desc_count
) {
353 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
358 /* First UNMAP block descriptor starts at 8 byte offset */
360 pr_debug("UNMAP: Sub: %s Using dl: %u bd_dl: %u size: %u"
361 " ptr: %p\n", dev
->transport
->name
, dl
, bd_dl
, size
, ptr
);
364 lba
= get_unaligned_be64(&ptr
[0]);
365 range
= get_unaligned_be32(&ptr
[8]);
366 pr_debug("UNMAP: Using lba: %llu and range: %u\n",
367 (unsigned long long)lba
, range
);
369 if (range
> dev
->se_sub_dev
->se_dev_attrib
.max_unmap_lba_count
) {
370 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
375 if (lba
+ range
> dev
->transport
->get_blocks(dev
) + 1) {
376 cmd
->scsi_sense_reason
= TCM_ADDRESS_OUT_OF_RANGE
;
381 ret
= blkdev_issue_discard(ibd
->ibd_bd
, lba
, range
,
384 pr_err("blkdev_issue_discard() failed: %d\n",
394 transport_kunmap_data_sg(cmd
);
396 target_complete_cmd(cmd
, GOOD
);
400 static int iblock_execute_write_same(struct se_cmd
*cmd
)
402 struct iblock_dev
*ibd
= cmd
->se_dev
->dev_ptr
;
405 ret
= blkdev_issue_discard(ibd
->ibd_bd
, cmd
->t_task_lba
,
406 spc_get_write_same_sectors(cmd
), GFP_KERNEL
,
409 pr_debug("blkdev_issue_discard() failed for WRITE_SAME\n");
413 target_complete_cmd(cmd
, GOOD
);
418 Opt_udev_path
, Opt_readonly
, Opt_force
, Opt_err
421 static match_table_t tokens
= {
422 {Opt_udev_path
, "udev_path=%s"},
423 {Opt_readonly
, "readonly=%d"},
424 {Opt_force
, "force=%d"},
428 static ssize_t
iblock_set_configfs_dev_params(struct se_hba
*hba
,
429 struct se_subsystem_dev
*se_dev
,
430 const char *page
, ssize_t count
)
432 struct iblock_dev
*ib_dev
= se_dev
->se_dev_su_ptr
;
433 char *orig
, *ptr
, *arg_p
, *opts
;
434 substring_t args
[MAX_OPT_ARGS
];
436 unsigned long tmp_readonly
;
438 opts
= kstrdup(page
, GFP_KERNEL
);
444 while ((ptr
= strsep(&opts
, ",\n")) != NULL
) {
448 token
= match_token(ptr
, tokens
, args
);
451 if (ib_dev
->ibd_bd
) {
452 pr_err("Unable to set udev_path= while"
453 " ib_dev->ibd_bd exists\n");
457 arg_p
= match_strdup(&args
[0]);
462 snprintf(ib_dev
->ibd_udev_path
, SE_UDEV_PATH_LEN
,
465 pr_debug("IBLOCK: Referencing UDEV path: %s\n",
466 ib_dev
->ibd_udev_path
);
467 ib_dev
->ibd_flags
|= IBDF_HAS_UDEV_PATH
;
470 arg_p
= match_strdup(&args
[0]);
475 ret
= strict_strtoul(arg_p
, 0, &tmp_readonly
);
478 pr_err("strict_strtoul() failed for"
482 ib_dev
->ibd_readonly
= tmp_readonly
;
483 pr_debug("IBLOCK: readonly: %d\n", ib_dev
->ibd_readonly
);
494 return (!ret
) ? count
: ret
;
497 static ssize_t
iblock_check_configfs_dev_params(
499 struct se_subsystem_dev
*se_dev
)
501 struct iblock_dev
*ibd
= se_dev
->se_dev_su_ptr
;
503 if (!(ibd
->ibd_flags
& IBDF_HAS_UDEV_PATH
)) {
504 pr_err("Missing udev_path= parameters for IBLOCK\n");
511 static ssize_t
iblock_show_configfs_dev_params(
513 struct se_subsystem_dev
*se_dev
,
516 struct iblock_dev
*ibd
= se_dev
->se_dev_su_ptr
;
517 struct block_device
*bd
= ibd
->ibd_bd
;
518 char buf
[BDEVNAME_SIZE
];
522 bl
+= sprintf(b
+ bl
, "iBlock device: %s",
524 if (ibd
->ibd_flags
& IBDF_HAS_UDEV_PATH
)
525 bl
+= sprintf(b
+ bl
, " UDEV PATH: %s",
527 bl
+= sprintf(b
+ bl
, " readonly: %d\n", ibd
->ibd_readonly
);
529 bl
+= sprintf(b
+ bl
, " ");
531 bl
+= sprintf(b
+ bl
, "Major: %d Minor: %d %s\n",
532 MAJOR(bd
->bd_dev
), MINOR(bd
->bd_dev
), (!bd
->bd_contains
) ?
533 "" : (bd
->bd_holder
== ibd
) ?
534 "CLAIMED: IBLOCK" : "CLAIMED: OS");
536 bl
+= sprintf(b
+ bl
, "Major: 0 Minor: 0\n");
542 static void iblock_complete_cmd(struct se_cmd
*cmd
)
544 struct iblock_req
*ibr
= cmd
->priv
;
547 if (!atomic_dec_and_test(&ibr
->pending
))
550 if (atomic_read(&ibr
->ib_bio_err_cnt
))
551 status
= SAM_STAT_CHECK_CONDITION
;
553 status
= SAM_STAT_GOOD
;
555 target_complete_cmd(cmd
, status
);
559 static void iblock_bio_destructor(struct bio
*bio
)
561 struct se_cmd
*cmd
= bio
->bi_private
;
562 struct iblock_dev
*ib_dev
= cmd
->se_dev
->dev_ptr
;
564 bio_free(bio
, ib_dev
->ibd_bio_set
);
568 iblock_get_bio(struct se_cmd
*cmd
, sector_t lba
, u32 sg_num
)
570 struct iblock_dev
*ib_dev
= cmd
->se_dev
->dev_ptr
;
574 * Only allocate as many vector entries as the bio code allows us to,
575 * we'll loop later on until we have handled the whole request.
577 if (sg_num
> BIO_MAX_PAGES
)
578 sg_num
= BIO_MAX_PAGES
;
580 bio
= bio_alloc_bioset(GFP_NOIO
, sg_num
, ib_dev
->ibd_bio_set
);
582 pr_err("Unable to allocate memory for bio\n");
586 bio
->bi_bdev
= ib_dev
->ibd_bd
;
587 bio
->bi_private
= cmd
;
588 bio
->bi_destructor
= iblock_bio_destructor
;
589 bio
->bi_end_io
= &iblock_bio_done
;
590 bio
->bi_sector
= lba
;
594 static void iblock_submit_bios(struct bio_list
*list
, int rw
)
596 struct blk_plug plug
;
599 blk_start_plug(&plug
);
600 while ((bio
= bio_list_pop(list
)))
602 blk_finish_plug(&plug
);
605 static int iblock_execute_rw(struct se_cmd
*cmd
)
607 struct scatterlist
*sgl
= cmd
->t_data_sg
;
608 u32 sgl_nents
= cmd
->t_data_nents
;
609 enum dma_data_direction data_direction
= cmd
->data_direction
;
610 struct se_device
*dev
= cmd
->se_dev
;
611 struct iblock_req
*ibr
;
613 struct bio_list list
;
614 struct scatterlist
*sg
;
615 u32 sg_num
= sgl_nents
;
621 if (data_direction
== DMA_TO_DEVICE
) {
623 * Force data to disk if we pretend to not have a volatile
624 * write cache, or the initiator set the Force Unit Access bit.
626 if (dev
->se_sub_dev
->se_dev_attrib
.emulate_write_cache
== 0 ||
627 (dev
->se_sub_dev
->se_dev_attrib
.emulate_fua_write
> 0 &&
628 (cmd
->se_cmd_flags
& SCF_FUA
)))
637 * Convert the blocksize advertised to the initiator to the 512 byte
638 * units unconditionally used by the Linux block layer.
640 if (dev
->se_sub_dev
->se_dev_attrib
.block_size
== 4096)
641 block_lba
= (cmd
->t_task_lba
<< 3);
642 else if (dev
->se_sub_dev
->se_dev_attrib
.block_size
== 2048)
643 block_lba
= (cmd
->t_task_lba
<< 2);
644 else if (dev
->se_sub_dev
->se_dev_attrib
.block_size
== 1024)
645 block_lba
= (cmd
->t_task_lba
<< 1);
646 else if (dev
->se_sub_dev
->se_dev_attrib
.block_size
== 512)
647 block_lba
= cmd
->t_task_lba
;
649 pr_err("Unsupported SCSI -> BLOCK LBA conversion:"
650 " %u\n", dev
->se_sub_dev
->se_dev_attrib
.block_size
);
651 cmd
->scsi_sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
655 ibr
= kzalloc(sizeof(struct iblock_req
), GFP_KERNEL
);
660 bio
= iblock_get_bio(cmd
, block_lba
, sgl_nents
);
664 bio_list_init(&list
);
665 bio_list_add(&list
, bio
);
667 atomic_set(&ibr
->pending
, 2);
670 for_each_sg(sgl
, sg
, sgl_nents
, i
) {
672 * XXX: if the length the device accepts is shorter than the
673 * length of the S/G list entry this will cause and
674 * endless loop. Better hope no driver uses huge pages.
676 while (bio_add_page(bio
, sg_page(sg
), sg
->length
, sg
->offset
)
678 if (bio_cnt
>= IBLOCK_MAX_BIO_PER_TASK
) {
679 iblock_submit_bios(&list
, rw
);
683 bio
= iblock_get_bio(cmd
, block_lba
, sg_num
);
687 atomic_inc(&ibr
->pending
);
688 bio_list_add(&list
, bio
);
692 /* Always in 512 byte units for Linux/Block */
693 block_lba
+= sg
->length
>> IBLOCK_LBA_SHIFT
;
697 iblock_submit_bios(&list
, rw
);
698 iblock_complete_cmd(cmd
);
702 while ((bio
= bio_list_pop(&list
)))
706 cmd
->scsi_sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
711 static u32
iblock_get_device_rev(struct se_device
*dev
)
713 return SCSI_SPC_2
; /* Returns SPC-3 in Initiator Data */
716 static u32
iblock_get_device_type(struct se_device
*dev
)
721 static sector_t
iblock_get_blocks(struct se_device
*dev
)
723 struct iblock_dev
*ibd
= dev
->dev_ptr
;
724 struct block_device
*bd
= ibd
->ibd_bd
;
725 struct request_queue
*q
= bdev_get_queue(bd
);
727 return iblock_emulate_read_cap_with_block_size(dev
, bd
, q
);
730 static void iblock_bio_done(struct bio
*bio
, int err
)
732 struct se_cmd
*cmd
= bio
->bi_private
;
733 struct iblock_req
*ibr
= cmd
->priv
;
736 * Set -EIO if !BIO_UPTODATE and the passed is still err=0
738 if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
) && !err
)
742 pr_err("test_bit(BIO_UPTODATE) failed for bio: %p,"
743 " err: %d\n", bio
, err
);
745 * Bump the ib_bio_err_cnt and release bio.
747 atomic_inc(&ibr
->ib_bio_err_cnt
);
748 smp_mb__after_atomic_inc();
753 iblock_complete_cmd(cmd
);
756 static struct spc_ops iblock_spc_ops
= {
757 .execute_rw
= iblock_execute_rw
,
758 .execute_sync_cache
= iblock_execute_sync_cache
,
759 .execute_write_same
= iblock_execute_write_same
,
760 .execute_unmap
= iblock_execute_unmap
,
763 static int iblock_parse_cdb(struct se_cmd
*cmd
)
765 return sbc_parse_cdb(cmd
, &iblock_spc_ops
);
768 static struct se_subsystem_api iblock_template
= {
770 .owner
= THIS_MODULE
,
771 .transport_type
= TRANSPORT_PLUGIN_VHBA_PDEV
,
772 .write_cache_emulated
= 1,
773 .fua_write_emulated
= 1,
774 .attach_hba
= iblock_attach_hba
,
775 .detach_hba
= iblock_detach_hba
,
776 .allocate_virtdevice
= iblock_allocate_virtdevice
,
777 .create_virtdevice
= iblock_create_virtdevice
,
778 .free_device
= iblock_free_device
,
779 .parse_cdb
= iblock_parse_cdb
,
780 .check_configfs_dev_params
= iblock_check_configfs_dev_params
,
781 .set_configfs_dev_params
= iblock_set_configfs_dev_params
,
782 .show_configfs_dev_params
= iblock_show_configfs_dev_params
,
783 .get_device_rev
= iblock_get_device_rev
,
784 .get_device_type
= iblock_get_device_type
,
785 .get_blocks
= iblock_get_blocks
,
788 static int __init
iblock_module_init(void)
790 return transport_subsystem_register(&iblock_template
);
793 static void iblock_module_exit(void)
795 transport_subsystem_release(&iblock_template
);
798 MODULE_DESCRIPTION("TCM IBLOCK subsystem plugin");
799 MODULE_AUTHOR("nab@Linux-iSCSI.org");
800 MODULE_LICENSE("GPL");
802 module_init(iblock_module_init
);
803 module_exit(iblock_module_exit
);