Skip to content

Commit d0c8b25

Browse files
author
Nicholas Bellinger
committed
target/iblock: Use backend REQ_FLUSH hint for WriteCacheEnabled status
This patch allows IBLOCK to check block hints in request_queue->flush_flags when reporting current backend device WriteCacheEnabled status to a remote SCSI initiator port. This is done via a se_subsystem_api->get_write_cache() call instead of a backend se_device creation time flag, as we expect REQ_FLUSH bits to possibly change from an underlying blk_queue_flush() by the SCSI disk driver, or internal raw struct block_device driver usage. Also go ahead and update iblock_execute_rw() bio I/O path code to use REQ_FLUSH + REQ_FUA hints when determining WRITE_FUA usage, and make SPC emulation code use a spc_check_dev_wce() helper to handle both types of cases for virtual backend subsystem drivers. (asias: Drop unnecessary comparsion operators) Reported-by: majianpeng <majianpeng@gmail.com> Cc: majianpeng <majianpeng@gmail.com> Cc: Christoph Hellwig <hch@infradead.org> Cc: Jens Axboe <axboe@kernel.dk> Cc: James Bottomley <JBottomley@Parallels.com> Signed-off-by: Nicholas Bellinger <nab@linux-iscsi.org>
1 parent 1be2956 commit d0c8b25

4 files changed

Lines changed: 48 additions & 11 deletions

File tree

‎drivers/target/target_core_device.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,12 @@ int se_dev_set_emulate_write_cache(struct se_device *dev, int flag)
772772
pr_err("emulate_write_cache not supported for pSCSI\n");
773773
return -EINVAL;
774774
}
775+
if (dev->transport->get_write_cache) {
776+
pr_warn("emulate_write_cache cannot be changed when underlying"
777+
" HW reports WriteCacheEnabled, ignoring request\n");
778+
return 0;
779+
}
780+
775781
dev->dev_attrib.emulate_write_cache = flag;
776782
pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
777783
dev, dev->dev_attrib.emulate_write_cache);

‎drivers/target/target_core_iblock.c‎

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ static int iblock_configure_device(struct se_device *dev)
154154

155155
if (blk_queue_nonrot(q))
156156
dev->dev_attrib.is_nonrot = 1;
157+
157158
return 0;
158159

159160
out_free_bioset:
@@ -654,20 +655,24 @@ iblock_execute_rw(struct se_cmd *cmd)
654655
u32 sg_num = sgl_nents;
655656
sector_t block_lba;
656657
unsigned bio_cnt;
657-
int rw;
658+
int rw = 0;
658659
int i;
659660

660661
if (data_direction == DMA_TO_DEVICE) {
662+
struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
663+
struct request_queue *q = bdev_get_queue(ib_dev->ibd_bd);
661664
/*
662-
* Force data to disk if we pretend to not have a volatile
663-
* write cache, or the initiator set the Force Unit Access bit.
665+
* Force writethrough using WRITE_FUA if a volatile write cache
666+
* is not enabled, or if initiator set the Force Unit Access bit.
664667
*/
665-
if (dev->dev_attrib.emulate_write_cache == 0 ||
666-
(dev->dev_attrib.emulate_fua_write > 0 &&
667-
(cmd->se_cmd_flags & SCF_FUA)))
668-
rw = WRITE_FUA;
669-
else
668+
if (q->flush_flags & REQ_FUA) {
669+
if (cmd->se_cmd_flags & SCF_FUA)
670+
rw = WRITE_FUA;
671+
else if (!(q->flush_flags & REQ_FLUSH))
672+
rw = WRITE_FUA;
673+
} else {
670674
rw = WRITE;
675+
}
671676
} else {
672677
rw = READ;
673678
}
@@ -774,6 +779,15 @@ iblock_parse_cdb(struct se_cmd *cmd)
774779
return sbc_parse_cdb(cmd, &iblock_sbc_ops);
775780
}
776781

782+
bool iblock_get_write_cache(struct se_device *dev)
783+
{
784+
struct iblock_dev *ib_dev = IBLOCK_DEV(dev);
785+
struct block_device *bd = ib_dev->ibd_bd;
786+
struct request_queue *q = bdev_get_queue(bd);
787+
788+
return q->flush_flags & REQ_FLUSH;
789+
}
790+
777791
static struct se_subsystem_api iblock_template = {
778792
.name = "iblock",
779793
.inquiry_prod = "IBLOCK",
@@ -790,6 +804,7 @@ static struct se_subsystem_api iblock_template = {
790804
.show_configfs_dev_params = iblock_show_configfs_dev_params,
791805
.get_device_type = sbc_get_device_type,
792806
.get_blocks = iblock_get_blocks,
807+
.get_write_cache = iblock_get_write_cache,
793808
};
794809

795810
static int __init iblock_module_init(void)

‎drivers/target/target_core_spc.c‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,16 +407,31 @@ spc_emulate_evpd_83(struct se_cmd *cmd, unsigned char *buf)
407407
}
408408
EXPORT_SYMBOL(spc_emulate_evpd_83);
409409

410+
static bool
411+
spc_check_dev_wce(struct se_device *dev)
412+
{
413+
bool wce = false;
414+
415+
if (dev->transport->get_write_cache)
416+
wce = dev->transport->get_write_cache(dev);
417+
else if (dev->dev_attrib.emulate_write_cache > 0)
418+
wce = true;
419+
420+
return wce;
421+
}
422+
410423
/* Extended INQUIRY Data VPD Page */
411424
static sense_reason_t
412425
spc_emulate_evpd_86(struct se_cmd *cmd, unsigned char *buf)
413426
{
427+
struct se_device *dev = cmd->se_dev;
428+
414429
buf[3] = 0x3c;
415430
/* Set HEADSUP, ORDSUP, SIMPSUP */
416431
buf[5] = 0x07;
417432

418433
/* If WriteCache emulation is enabled, set V_SUP */
419-
if (cmd->se_dev->dev_attrib.emulate_write_cache > 0)
434+
if (spc_check_dev_wce(dev))
420435
buf[6] = 0x01;
421436
return 0;
422437
}
@@ -766,7 +781,7 @@ static int spc_modesense_caching(struct se_device *dev, u8 pc, u8 *p)
766781
if (pc == 1)
767782
goto out;
768783

769-
if (dev->dev_attrib.emulate_write_cache > 0)
784+
if (spc_check_dev_wce(dev))
770785
p[2] = 0x04; /* Write Cache Enable */
771786
p[12] = 0x20; /* Disabled Read Ahead */
772787

@@ -878,7 +893,7 @@ static sense_reason_t spc_emulate_modesense(struct se_cmd *cmd)
878893
(cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)))
879894
spc_modesense_write_protect(&buf[length], type);
880895

881-
if ((dev->dev_attrib.emulate_write_cache > 0) &&
896+
if ((spc_check_dev_wce(dev)) &&
882897
(dev->dev_attrib.emulate_fua_write > 0))
883898
spc_modesense_dpofua(&buf[length], type);
884899

‎include/target/target_core_backend.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ struct se_subsystem_api {
3535
u32 (*get_device_type)(struct se_device *);
3636
sector_t (*get_blocks)(struct se_device *);
3737
unsigned char *(*get_sense_buffer)(struct se_cmd *);
38+
bool (*get_write_cache)(struct se_device *);
3839
};
3940

4041
struct sbc_ops {

0 commit comments

Comments
 (0)