From 0db6ca8a5e1ea585795db3643ec7d50fc8cb1aff Mon Sep 17 00:00:00 2001 From: Bart Van Assche Date: Fri, 2 Jun 2017 14:21:55 -0700 Subject: scsi: Protect SCSI device state changes with a mutex Serializing SCSI device state changes avoids that two state changes can occur concurrently, e.g. the state changes in scsi_target_block() and __scsi_remove_device(). This serialization is essential to make patch "Make __scsi_remove_device go straight from BLOCKED to DEL" work reliably. Enable this mechanism for all scsi_target_*block() callers but not for the scsi_internal_device_unblock() calls from the mpt3sas driver because that driver can call scsi_internal_device_unblock() from atomic context. Signed-off-by: Bart Van Assche Cc: Christoph Hellwig Cc: Hannes Reinecke Cc: Johannes Thumshirn Signed-off-by: Martin K. Petersen --- drivers/scsi/sd.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'drivers/scsi/sd.c') diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c index b6bb4e0ce0e3..53b6d72c214d 100644 --- a/drivers/scsi/sd.c +++ b/drivers/scsi/sd.c @@ -1818,8 +1818,9 @@ static void sd_eh_reset(struct scsi_cmnd *scmd) static int sd_eh_action(struct scsi_cmnd *scmd, int eh_disp) { struct scsi_disk *sdkp = scsi_disk(scmd->request->rq_disk); + struct scsi_device *sdev = scmd->device; - if (!scsi_device_online(scmd->device) || + if (!scsi_device_online(sdev) || !scsi_medium_access_command(scmd) || host_byte(scmd->result) != DID_TIME_OUT || eh_disp != SUCCESS) @@ -1845,7 +1846,9 @@ static int sd_eh_action(struct scsi_cmnd *scmd, int eh_disp) if (sdkp->medium_access_timed_out >= sdkp->max_medium_access_timeouts) { scmd_printk(KERN_ERR, scmd, "Medium access timeout failure. Offlining disk!\n"); - scsi_device_set_state(scmd->device, SDEV_OFFLINE); + mutex_lock(&sdev->state_mutex); + scsi_device_set_state(sdev, SDEV_OFFLINE); + mutex_unlock(&sdev->state_mutex); return SUCCESS; } -- cgit v1.2.1 From 4c11712ad0e1aa38154854a2a3efb1f4a90a059d Mon Sep 17 00:00:00 2001 From: "Martin K. Petersen" Date: Thu, 25 May 2017 09:34:30 -0400 Subject: scsi: sd: Use sysfs_match_string() Avoid unnecessary snprintf() when formatting variables for display in sysfs and switch to sysfs_match_string() for validating user input. Reviewed-by: Bart Van Assche Signed-off-by: Martin K. Petersen --- drivers/scsi/sd.c | 71 +++++++++++++++++++++++-------------------------------- 1 file changed, 29 insertions(+), 42 deletions(-) (limited to 'drivers/scsi/sd.c') diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c index 53b6d72c214d..7dc8cb8f1818 100644 --- a/drivers/scsi/sd.c +++ b/drivers/scsi/sd.c @@ -155,7 +155,7 @@ static ssize_t cache_type_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { - int i, ct = -1, rcd, wce, sp; + int ct, rcd, wce, sp; struct scsi_disk *sdkp = to_scsi_disk(dev); struct scsi_device *sdp = sdkp->device; char buffer[64]; @@ -178,16 +178,10 @@ cache_type_store(struct device *dev, struct device_attribute *attr, sdkp->cache_override = 0; } - for (i = 0; i < ARRAY_SIZE(sd_cache_types); i++) { - len = strlen(sd_cache_types[i]); - if (strncmp(sd_cache_types[i], buf, len) == 0 && - buf[len] == '\n') { - ct = i; - break; - } - } + ct = sysfs_match_string(sd_cache_types, buf); if (ct < 0) return -EINVAL; + rcd = ct & 0x01 ? 1 : 0; wce = (ct & 0x02) && !sdkp->write_prot ? 1 : 0; @@ -227,7 +221,7 @@ manage_start_stop_show(struct device *dev, struct device_attribute *attr, struct scsi_disk *sdkp = to_scsi_disk(dev); struct scsi_device *sdp = sdkp->device; - return snprintf(buf, 20, "%u\n", sdp->manage_start_stop); + return sprintf(buf, "%u\n", sdp->manage_start_stop); } static ssize_t @@ -251,7 +245,7 @@ allow_restart_show(struct device *dev, struct device_attribute *attr, char *buf) { struct scsi_disk *sdkp = to_scsi_disk(dev); - return snprintf(buf, 40, "%d\n", sdkp->device->allow_restart); + return sprintf(buf, "%u\n", sdkp->device->allow_restart); } static ssize_t @@ -279,7 +273,7 @@ cache_type_show(struct device *dev, struct device_attribute *attr, char *buf) struct scsi_disk *sdkp = to_scsi_disk(dev); int ct = sdkp->RCD + 2*sdkp->WCE; - return snprintf(buf, 40, "%s\n", sd_cache_types[ct]); + return sprintf(buf, "%s\n", sd_cache_types[ct]); } static DEVICE_ATTR_RW(cache_type); @@ -288,7 +282,7 @@ FUA_show(struct device *dev, struct device_attribute *attr, char *buf) { struct scsi_disk *sdkp = to_scsi_disk(dev); - return snprintf(buf, 20, "%u\n", sdkp->DPOFUA); + return sprintf(buf, "%u\n", sdkp->DPOFUA); } static DEVICE_ATTR_RO(FUA); @@ -298,7 +292,7 @@ protection_type_show(struct device *dev, struct device_attribute *attr, { struct scsi_disk *sdkp = to_scsi_disk(dev); - return snprintf(buf, 20, "%u\n", sdkp->protection_type); + return sprintf(buf, "%u\n", sdkp->protection_type); } static ssize_t @@ -341,9 +335,9 @@ protection_mode_show(struct device *dev, struct device_attribute *attr, } if (!dif && !dix) - return snprintf(buf, 20, "none\n"); + return sprintf(buf, "none\n"); - return snprintf(buf, 20, "%s%u\n", dix ? "dix" : "dif", dif); + return sprintf(buf, "%s%u\n", dix ? "dix" : "dif", dif); } static DEVICE_ATTR_RO(protection_mode); @@ -352,7 +346,7 @@ app_tag_own_show(struct device *dev, struct device_attribute *attr, char *buf) { struct scsi_disk *sdkp = to_scsi_disk(dev); - return snprintf(buf, 20, "%u\n", sdkp->ATO); + return sprintf(buf, "%u\n", sdkp->ATO); } static DEVICE_ATTR_RO(app_tag_own); @@ -362,10 +356,11 @@ thin_provisioning_show(struct device *dev, struct device_attribute *attr, { struct scsi_disk *sdkp = to_scsi_disk(dev); - return snprintf(buf, 20, "%u\n", sdkp->lbpme); + return sprintf(buf, "%u\n", sdkp->lbpme); } static DEVICE_ATTR_RO(thin_provisioning); +/* sysfs_match_string() requires dense arrays */ static const char *lbp_mode[] = { [SD_LBP_FULL] = "full", [SD_LBP_UNMAP] = "unmap", @@ -381,7 +376,7 @@ provisioning_mode_show(struct device *dev, struct device_attribute *attr, { struct scsi_disk *sdkp = to_scsi_disk(dev); - return snprintf(buf, 20, "%s\n", lbp_mode[sdkp->provisioning_mode]); + return sprintf(buf, "%s\n", lbp_mode[sdkp->provisioning_mode]); } static ssize_t @@ -390,6 +385,7 @@ provisioning_mode_store(struct device *dev, struct device_attribute *attr, { struct scsi_disk *sdkp = to_scsi_disk(dev); struct scsi_device *sdp = sdkp->device; + int mode; if (!capable(CAP_SYS_ADMIN)) return -EACCES; @@ -402,23 +398,17 @@ provisioning_mode_store(struct device *dev, struct device_attribute *attr, if (sdp->type != TYPE_DISK) return -EINVAL; - if (!strncmp(buf, lbp_mode[SD_LBP_UNMAP], 20)) - sd_config_discard(sdkp, SD_LBP_UNMAP); - else if (!strncmp(buf, lbp_mode[SD_LBP_WS16], 20)) - sd_config_discard(sdkp, SD_LBP_WS16); - else if (!strncmp(buf, lbp_mode[SD_LBP_WS10], 20)) - sd_config_discard(sdkp, SD_LBP_WS10); - else if (!strncmp(buf, lbp_mode[SD_LBP_ZERO], 20)) - sd_config_discard(sdkp, SD_LBP_ZERO); - else if (!strncmp(buf, lbp_mode[SD_LBP_DISABLE], 20)) - sd_config_discard(sdkp, SD_LBP_DISABLE); - else + mode = sysfs_match_string(lbp_mode, buf); + if (mode < 0) return -EINVAL; + sd_config_discard(sdkp, mode); + return count; } static DEVICE_ATTR_RW(provisioning_mode); +/* sysfs_match_string() requires dense arrays */ static const char *zeroing_mode[] = { [SD_ZERO_WRITE] = "write", [SD_ZERO_WS] = "writesame", @@ -432,7 +422,7 @@ zeroing_mode_show(struct device *dev, struct device_attribute *attr, { struct scsi_disk *sdkp = to_scsi_disk(dev); - return snprintf(buf, 20, "%s\n", zeroing_mode[sdkp->zeroing_mode]); + return sprintf(buf, "%s\n", zeroing_mode[sdkp->zeroing_mode]); } static ssize_t @@ -440,21 +430,17 @@ zeroing_mode_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { struct scsi_disk *sdkp = to_scsi_disk(dev); + int mode; if (!capable(CAP_SYS_ADMIN)) return -EACCES; - if (!strncmp(buf, zeroing_mode[SD_ZERO_WRITE], 20)) - sdkp->zeroing_mode = SD_ZERO_WRITE; - else if (!strncmp(buf, zeroing_mode[SD_ZERO_WS], 20)) - sdkp->zeroing_mode = SD_ZERO_WS; - else if (!strncmp(buf, zeroing_mode[SD_ZERO_WS16_UNMAP], 20)) - sdkp->zeroing_mode = SD_ZERO_WS16_UNMAP; - else if (!strncmp(buf, zeroing_mode[SD_ZERO_WS10_UNMAP], 20)) - sdkp->zeroing_mode = SD_ZERO_WS10_UNMAP; - else + mode = sysfs_match_string(zeroing_mode, buf); + if (mode < 0) return -EINVAL; + sdkp->zeroing_mode = mode; + return count; } static DEVICE_ATTR_RW(zeroing_mode); @@ -465,7 +451,7 @@ max_medium_access_timeouts_show(struct device *dev, { struct scsi_disk *sdkp = to_scsi_disk(dev); - return snprintf(buf, 20, "%u\n", sdkp->max_medium_access_timeouts); + return sprintf(buf, "%u\n", sdkp->max_medium_access_timeouts); } static ssize_t @@ -491,7 +477,7 @@ max_write_same_blocks_show(struct device *dev, struct device_attribute *attr, { struct scsi_disk *sdkp = to_scsi_disk(dev); - return snprintf(buf, 20, "%u\n", sdkp->max_ws_blocks); + return sprintf(buf, "%u\n", sdkp->max_ws_blocks); } static ssize_t @@ -696,6 +682,7 @@ static void sd_config_discard(struct scsi_disk *sdkp, unsigned int mode) switch (mode) { + case SD_LBP_FULL: case SD_LBP_DISABLE: blk_queue_max_discard_sectors(q, 0); queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q); -- cgit v1.2.1