From 32c356d76d7e13dcd0675189d8e9c64ef66aa561 Mon Sep 17 00:00:00 2001 From: James Bottomley Date: Wed, 20 Aug 2008 00:58:13 +0200 Subject: [SCSI] fix removable device inability to detect disk changes On Tue, 12 Aug 2008 15:08:14 +0200 Giuliano Pochini wrote: > Fujitsu magneto-optical drive, Adaptec 29160 and > Linux Jay 2.6.26 #7 SMP Sun Aug 10 18:34:22 CEST 2008 ppc 7455, altivec supported PowerMac3,6 GNU/Linux > > When I insert a disk and I mount it, scsi_test_unit_ready() is called and > the do-while loop gets sshdr->sense_key == UNIT_ATTENTION in the first > cycle and 0 in the second one. So the if below misses the UNIT_ATTENTION > and sdev->changed = 1 is not executed. At this point bad things can > happen... I'm not sure how to fix this. Any clue ? The problem is essentially caused by us eating UNIT_ATTENTION conditions in scsi_test_unit_ready(). Fix by updating the ->changed flag when this happens if the media is removable. [pochini@shiny.it: updates to tidy up patch] Signed-off-by: Giuliano Pochini Signed-off-by: James Bottomley --- drivers/scsi/scsi_lib.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'drivers/scsi/scsi_lib.c') diff --git a/drivers/scsi/scsi_lib.c b/drivers/scsi/scsi_lib.c index e5a9526d203..f35a0951e46 100644 --- a/drivers/scsi/scsi_lib.c +++ b/drivers/scsi/scsi_lib.c @@ -2105,22 +2105,21 @@ scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries, do { result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0, sshdr, timeout, retries); - } while ((driver_byte(result) & DRIVER_SENSE) && - sshdr && sshdr->sense_key == UNIT_ATTENTION && - --retries); + if (sdev->removable && scsi_sense_valid(sshdr) && + sshdr->sense_key == UNIT_ATTENTION) + sdev->changed = 1; + } while (scsi_sense_valid(sshdr) && + sshdr->sense_key == UNIT_ATTENTION && --retries); if (!sshdr) /* could not allocate sense buffer, so can't process it */ return result; - if ((driver_byte(result) & DRIVER_SENSE) && sdev->removable) { - - if ((scsi_sense_valid(sshdr)) && - ((sshdr->sense_key == UNIT_ATTENTION) || - (sshdr->sense_key == NOT_READY))) { - sdev->changed = 1; - result = 0; - } + if (sdev->removable && scsi_sense_valid(sshdr) && + (sshdr->sense_key == UNIT_ATTENTION || + sshdr->sense_key == NOT_READY)) { + sdev->changed = 1; + result = 0; } if (!sshdr_external) kfree(sshdr); -- cgit v1.2.3 From 9d11251709f31d49c8167a619d4475fdf6cd7f73 Mon Sep 17 00:00:00 2001 From: Kiyoshi Ueda Date: Sat, 4 Oct 2008 14:11:06 -0400 Subject: [SCSI] refactor sdev/starget/shost busy checking This patch refactors the busy checking codes of scsi_device, Scsi_Host and scsi_target. There should be no functional change. This is a preparation for another patch which exports scsi's busy state to the block layer for request stacking drivers. Signed-off-by: Kiyoshi Ueda Signed-off-by: Jun'ichi Nomura Signed-off-by: James Bottomley --- drivers/scsi/scsi_lib.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) (limited to 'drivers/scsi/scsi_lib.c') diff --git a/drivers/scsi/scsi_lib.c b/drivers/scsi/scsi_lib.c index f35a0951e46..1e7c9e9ef77 100644 --- a/drivers/scsi/scsi_lib.c +++ b/drivers/scsi/scsi_lib.c @@ -529,6 +529,14 @@ static void scsi_single_lun_run(struct scsi_device *current_sdev) spin_unlock_irqrestore(shost->host_lock, flags); } +static inline int scsi_device_is_busy(struct scsi_device *sdev) +{ + if (sdev->device_busy >= sdev->queue_depth || sdev->device_blocked) + return 1; + + return 0; +} + static inline int scsi_target_is_busy(struct scsi_target *starget) { return ((starget->can_queue > 0 && @@ -536,6 +544,15 @@ static inline int scsi_target_is_busy(struct scsi_target *starget) starget->target_blocked); } +static inline int scsi_host_is_busy(struct Scsi_Host *shost) +{ + if ((shost->can_queue > 0 && shost->host_busy >= shost->can_queue) || + shost->host_blocked || shost->host_self_blocked) + return 1; + + return 0; +} + /* * Function: scsi_run_queue() * @@ -558,11 +575,7 @@ static void scsi_run_queue(struct request_queue *q) scsi_single_lun_run(sdev); spin_lock_irqsave(shost->host_lock, flags); - while (!list_empty(&shost->starved_list) && - !shost->host_blocked && !shost->host_self_blocked && - !((shost->can_queue > 0) && - (shost->host_busy >= shost->can_queue))) { - + while (!list_empty(&shost->starved_list) && !scsi_host_is_busy(shost)) { int flagset; /* @@ -1348,8 +1361,6 @@ int scsi_prep_fn(struct request_queue *q, struct request *req) static inline int scsi_dev_queue_ready(struct request_queue *q, struct scsi_device *sdev) { - if (sdev->device_busy >= sdev->queue_depth) - return 0; if (sdev->device_busy == 0 && sdev->device_blocked) { /* * unblock after device_blocked iterates to zero @@ -1363,7 +1374,7 @@ static inline int scsi_dev_queue_ready(struct request_queue *q, return 0; } } - if (sdev->device_blocked) + if (scsi_device_is_busy(sdev)) return 0; return 1; @@ -1440,8 +1451,7 @@ static inline int scsi_host_queue_ready(struct request_queue *q, return 0; } } - if ((shost->can_queue > 0 && shost->host_busy >= shost->can_queue) || - shost->host_blocked || shost->host_self_blocked) { + if (scsi_host_is_busy(shost)) { if (list_empty(&sdev->starved_entry)) list_add_tail(&sdev->starved_entry, &shost->starved_list); return 0; -- cgit v1.2.3 From 6c5121b78ba5c70a9990e2af6cb4d6bbffe0d4d8 Mon Sep 17 00:00:00 2001 From: Kiyoshi Ueda Date: Sat, 4 Oct 2008 14:11:35 -0400 Subject: [SCSI] export busy state via q->lld_busy_fn() This patch implements q->lld_busy_fn() for scsi mid layer to export its busy state for request stacking drivers. For efficiency, no lock is taken to check the busy state of shost/starget/sdev, since the returned value is not guaranteed and may be changed after request stacking drivers call the function, regardless of taking lock or not. When scsi can't dispatch I/Os anymore and needs to kill I/Os (e.g. !sdev), scsi needs to return 'not busy'. Otherwise, request stacking drivers may hold requests forever. Signed-off-by: Kiyoshi Ueda Signed-off-by: Jun'ichi Nomura Signed-off-by: James Bottomley --- drivers/scsi/scsi_lib.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'drivers/scsi/scsi_lib.c') diff --git a/drivers/scsi/scsi_lib.c b/drivers/scsi/scsi_lib.c index 1e7c9e9ef77..f5d3b96890d 100644 --- a/drivers/scsi/scsi_lib.c +++ b/drivers/scsi/scsi_lib.c @@ -1464,6 +1464,37 @@ static inline int scsi_host_queue_ready(struct request_queue *q, return 1; } +/* + * Busy state exporting function for request stacking drivers. + * + * For efficiency, no lock is taken to check the busy state of + * shost/starget/sdev, since the returned value is not guaranteed and + * may be changed after request stacking drivers call the function, + * regardless of taking lock or not. + * + * When scsi can't dispatch I/Os anymore and needs to kill I/Os + * (e.g. !sdev), scsi needs to return 'not busy'. + * Otherwise, request stacking drivers may hold requests forever. + */ +static int scsi_lld_busy(struct request_queue *q) +{ + struct scsi_device *sdev = q->queuedata; + struct Scsi_Host *shost; + struct scsi_target *starget; + + if (!sdev) + return 0; + + shost = sdev->host; + starget = scsi_target(sdev); + + if (scsi_host_in_recovery(shost) || scsi_host_is_busy(shost) || + scsi_target_is_busy(starget) || scsi_device_is_busy(sdev)) + return 1; + + return 0; +} + /* * Kill a request for a dead device */ @@ -1767,6 +1798,7 @@ struct request_queue *scsi_alloc_queue(struct scsi_device *sdev) blk_queue_prep_rq(q, scsi_prep_fn); blk_queue_softirq_done(q, scsi_softirq_done); blk_queue_rq_timed_out(q, scsi_times_out); + blk_queue_lld_busy(q, scsi_lld_busy); return q; } -- cgit v1.2.3