Before, when a write protected iSCSI target is attached as scsi-disk with BDRV_O_RDWR, we report it as writable, while in fact all writes will fail.
One way to improve this is to report write protect flag as true to guest, but a even better way is to refuse using a write protected LUN to guest. Target write protect flag is checked with a mode sense query. Signed-off-by: Fam Zheng <f...@redhat.com> --- block/iscsi.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/block/iscsi.c b/block/iscsi.c index 233f462..c154928 100644 --- a/block/iscsi.c +++ b/block/iscsi.c @@ -1339,6 +1339,36 @@ static int iscsi_open(BlockDriverState *bs, QDict *options, int flags, scsi_free_scsi_task(task); task = NULL; + /* Check the write protect flag of the LUN if we want to write */ + if (flags & BDRV_O_RDWR) { + struct scsi_mode_sense *ms; + + task = iscsi_modesense6_sync(iscsilun->iscsi, iscsilun->lun, + 1, SCSI_MODESENSE_PC_CURRENT, + 0x3F, + 0, 255); + + if (task == NULL) { + error_setg(errp, "Failed to send MODE_SENSE10 command: %s\n", + iscsi_get_error(iscsilun->iscsi)); + ret = -EINVAL; + goto out; + } + + if (task->status != SCSI_STATUS_GOOD) { + error_setg(errp, "MODE_SENSE10 failed: %s\n", + iscsi_get_error(iscsi)); + ret = -EINVAL; + goto out; + } + ms = scsi_datain_unmarshall(task); + if (ms->device_specific_parameter & 0x80) { + error_setg(errp, "Cannot open a write protected LUN as read-write"); + ret = -EPERM; + goto out; + } + } + iscsi_readcapacity_sync(iscsilun, &local_err); if (local_err != NULL) { error_propagate(errp, local_err); -- 1.9.3