Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

usb-storage: implement "soft" unbinding

This patch (as1092) implements "soft" unbinding for usb-storage. When
the disconnect routine is called, all commands and reset delays are
allowed to complete normally until after scsi_remove_host() returns.
This means that the commands needed for an orderly shutdown will be
sent through to the device.

Unlike before, the driver will now execute every command that it
accepts. Hence there's no need for special code to catch unexecuted
commands and fail them.

The new sequence of events when disconnect runs goes as follows:

If the device is truly unplugged, set the DISCONNECTING
flag so we won't try to access it any more.

If the SCSI-scanning thread hasn't started up yet, prevent
it from doing anything by setting the new DONT_SCAN flag.
Then wake it up and wait for it to terminate.

Remove the SCSI host. This unbinds the upper-level drivers,
doing an orderly shutdown. Commands sent to quiesce the
device will be transmitted normally, unless the device is
unplugged.

Set the DISCONNECTING flag so that we won't accept any new
commands that might get submitted (there aren't supposed to be
any) and we won't try to access the device for resets.

Tell the control thread to exit by waking it up with no
pending command, and wait for it to terminate.

Go on to do all the other normal stuff: releasing resources,
freeing memory, and so on.

Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>

authored by

Alan Stern and committed by
Greg Kroah-Hartman
543f7810 9da82bd4

+46 -53
+8 -8
drivers/usb/storage/transport.c
··· 127 127 long timeleft; 128 128 int status; 129 129 130 - /* don't submit URBs during abort/disconnect processing */ 131 - if (us->dflags & ABORTING_OR_DISCONNECTING) 130 + /* don't submit URBs during abort processing */ 131 + if (test_bit(US_FLIDX_ABORTING, &us->dflags)) 132 132 return -EIO; 133 133 134 134 /* set up data structures for the wakeup system */ ··· 161 161 * to cancel it */ 162 162 set_bit(US_FLIDX_URB_ACTIVE, &us->dflags); 163 163 164 - /* did an abort/disconnect occur during the submission? */ 165 - if (us->dflags & ABORTING_OR_DISCONNECTING) { 164 + /* did an abort occur during the submission? */ 165 + if (test_bit(US_FLIDX_ABORTING, &us->dflags)) { 166 166 167 167 /* cancel the URB, if it hasn't been cancelled already */ 168 168 if (test_and_clear_bit(US_FLIDX_URB_ACTIVE, &us->dflags)) { ··· 419 419 { 420 420 int result; 421 421 422 - /* don't submit s-g requests during abort/disconnect processing */ 423 - if (us->dflags & ABORTING_OR_DISCONNECTING) 422 + /* don't submit s-g requests during abort processing */ 423 + if (test_bit(US_FLIDX_ABORTING, &us->dflags)) 424 424 return USB_STOR_XFER_ERROR; 425 425 426 426 /* initialize the scatter-gather request block */ ··· 437 437 * okay to cancel it */ 438 438 set_bit(US_FLIDX_SG_ACTIVE, &us->dflags); 439 439 440 - /* did an abort/disconnect occur during the submission? */ 441 - if (us->dflags & ABORTING_OR_DISCONNECTING) { 440 + /* did an abort occur during the submission? */ 441 + if (test_bit(US_FLIDX_ABORTING, &us->dflags)) { 442 442 443 443 /* cancel the request, if it hasn't been cancelled already */ 444 444 if (test_and_clear_bit(US_FLIDX_SG_ACTIVE, &us->dflags)) {
+37 -42
drivers/usb/storage/usb.c
··· 320 320 /* lock the device pointers */ 321 321 mutex_lock(&(us->dev_mutex)); 322 322 323 - /* if the device has disconnected, we are free to exit */ 324 - if (test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) { 325 - US_DEBUGP("-- exiting\n"); 326 - mutex_unlock(&us->dev_mutex); 327 - break; 328 - } 329 - 330 323 /* lock access to the state */ 331 324 scsi_lock(host); 325 + 326 + /* When we are called with no command pending, we're done */ 327 + if (us->srb == NULL) { 328 + scsi_unlock(host); 329 + mutex_unlock(&us->dev_mutex); 330 + US_DEBUGP("-- exiting\n"); 331 + break; 332 + } 332 333 333 334 /* has the command timed out *already* ? */ 334 335 if (test_bit(US_FLIDX_TIMED_OUT, &us->dflags)) { ··· 385 384 /* lock access to the state */ 386 385 scsi_lock(host); 387 386 388 - /* did the command already complete because of a disconnect? */ 389 - if (!us->srb) 390 - ; /* nothing to do */ 391 - 392 387 /* indicate that the command is done */ 393 - else if (us->srb->result != DID_ABORT << 16) { 388 + if (us->srb->result != DID_ABORT << 16) { 394 389 US_DEBUGP("scsi cmd done, result=0x%x\n", 395 390 us->srb->result); 396 391 us->srb->scsi_done(us->srb); ··· 817 820 US_DEBUGP("-- %s\n", __func__); 818 821 819 822 /* Tell the control thread to exit. The SCSI host must 820 - * already have been removed so it won't try to queue 821 - * any more commands. 823 + * already have been removed and the DISCONNECTING flag set 824 + * so that we won't accept any more commands. 822 825 */ 823 826 US_DEBUGP("-- sending exit command to thread\n"); 824 - set_bit(US_FLIDX_DISCONNECTING, &us->dflags); 825 827 complete(&us->cmnd_ready); 826 828 if (us->ctl_thread) 827 829 kthread_stop(us->ctl_thread); ··· 855 859 usb_set_intfdata(us->pusb_intf, NULL); 856 860 } 857 861 858 - /* First stage of disconnect processing: stop all commands and remove 859 - * the host */ 862 + /* First stage of disconnect processing: stop SCSI scanning, 863 + * remove the host, and stop accepting new commands 864 + */ 860 865 static void quiesce_and_remove_host(struct us_data *us) 861 866 { 862 867 struct Scsi_Host *host = us_to_host(us); 863 868 864 - /* Prevent new USB transfers, stop the current command, and 865 - * interrupt a SCSI-scan or device-reset delay */ 869 + /* If the device is really gone, cut short reset delays */ 870 + if (us->pusb_dev->state == USB_STATE_NOTATTACHED) 871 + set_bit(US_FLIDX_DISCONNECTING, &us->dflags); 872 + 873 + /* Prevent SCSI-scanning (if it hasn't started yet) 874 + * and wait for the SCSI-scanning thread to stop. 875 + */ 876 + set_bit(US_FLIDX_DONT_SCAN, &us->dflags); 877 + wake_up(&us->delay_wait); 878 + wait_for_completion(&us->scanning_done); 879 + 880 + /* Removing the host will perform an orderly shutdown: caches 881 + * synchronized, disks spun down, etc. 882 + */ 883 + scsi_remove_host(host); 884 + 885 + /* Prevent any new commands from being accepted and cut short 886 + * reset delays. 887 + */ 866 888 scsi_lock(host); 867 889 set_bit(US_FLIDX_DISCONNECTING, &us->dflags); 868 890 scsi_unlock(host); 869 - usb_stor_stop_transport(us); 870 891 wake_up(&us->delay_wait); 871 - 872 - /* queuecommand won't accept any new commands and the control 873 - * thread won't execute a previously-queued command. If there 874 - * is such a command pending, complete it with an error. */ 875 - mutex_lock(&us->dev_mutex); 876 - if (us->srb) { 877 - us->srb->result = DID_NO_CONNECT << 16; 878 - scsi_lock(host); 879 - us->srb->scsi_done(us->srb); 880 - us->srb = NULL; 881 - complete(&us->notify); /* in case of an abort */ 882 - scsi_unlock(host); 883 - } 884 - mutex_unlock(&us->dev_mutex); 885 - 886 - /* Now we own no commands so it's safe to remove the SCSI host */ 887 - scsi_remove_host(host); 888 - 889 - /* Wait for the SCSI-scanning thread to stop */ 890 - wait_for_completion(&us->scanning_done); 891 892 } 892 893 893 894 /* Second stage of disconnect processing: deallocate all resources */ ··· 912 919 printk(KERN_DEBUG "usb-storage: waiting for device " 913 920 "to settle before scanning\n"); 914 921 wait_event_freezable_timeout(us->delay_wait, 915 - test_bit(US_FLIDX_DISCONNECTING, &us->dflags), 922 + test_bit(US_FLIDX_DONT_SCAN, &us->dflags), 916 923 delay_use * HZ); 917 924 } 918 925 919 926 /* If the device is still connected, perform the scanning */ 920 - if (!test_bit(US_FLIDX_DISCONNECTING, &us->dflags)) { 927 + if (!test_bit(US_FLIDX_DONT_SCAN, &us->dflags)) { 921 928 922 929 /* For bulk-only devices, determine the max LUN value */ 923 930 if (us->protocol == US_PR_BULK && ··· 1016 1023 if (IS_ERR(th)) { 1017 1024 printk(KERN_WARNING USB_STORAGE 1018 1025 "Unable to start the device-scanning thread\n"); 1026 + complete(&us->scanning_done); 1019 1027 quiesce_and_remove_host(us); 1020 1028 result = PTR_ERR(th); 1021 1029 goto BadDevice; ··· 1059 1065 .pre_reset = storage_pre_reset, 1060 1066 .post_reset = storage_post_reset, 1061 1067 .id_table = storage_usb_ids, 1068 + .soft_unbind = 1, 1062 1069 }; 1063 1070 1064 1071 static int __init usb_stor_init(void)
+1 -3
drivers/usb/storage/usb.h
··· 72 72 #define US_FLIDX_SG_ACTIVE 1 /* current_sg is in use */ 73 73 #define US_FLIDX_ABORTING 2 /* abort is in progress */ 74 74 #define US_FLIDX_DISCONNECTING 3 /* disconnect in progress */ 75 - #define ABORTING_OR_DISCONNECTING ((1UL << US_FLIDX_ABORTING) | \ 76 - (1UL << US_FLIDX_DISCONNECTING)) 77 75 #define US_FLIDX_RESETTING 4 /* device reset in progress */ 78 76 #define US_FLIDX_TIMED_OUT 5 /* SCSI midlayer timed out */ 79 - 77 + #define US_FLIDX_DONT_SCAN 6 /* don't scan (disconnect) */ 80 78 81 79 #define USB_STOR_STRING_LEN 32 82 80