Merge tag '4.4-scsi-mkp' into misc
[firefly-linux-kernel-4.4.55.git] / drivers / scsi / scsi_sysfs.c
index 8b7fa8aece661226d72a5706b3ede694a08fb325..658843bb162ad5357192f05229cf2ad73cb846e9 100644 (file)
@@ -1108,9 +1108,7 @@ void __scsi_remove_device(struct scsi_device *sdev)
                device_unregister(&sdev->sdev_dev);
                transport_remove_device(dev);
                scsi_dh_remove_device(sdev);
-               device_del(dev);
-       } else
-               put_device(&sdev->sdev_dev);
+       }
 
        /*
         * Stop accepting new requests and wait until all queuecommand() and
@@ -1121,6 +1119,16 @@ void __scsi_remove_device(struct scsi_device *sdev)
        blk_cleanup_queue(sdev->request_queue);
        cancel_work_sync(&sdev->requeue_work);
 
+       /*
+        * Remove the device after blk_cleanup_queue() has been called such
+        * a possible bdi_register() call with the same name occurs after
+        * blk_cleanup_queue() has called bdi_destroy().
+        */
+       if (sdev->is_visible)
+               device_del(dev);
+       else
+               put_device(&sdev->sdev_dev);
+
        if (sdev->host->hostt->slave_destroy)
                sdev->host->hostt->slave_destroy(sdev);
        transport_destroy_device(dev);
@@ -1182,31 +1190,23 @@ static void __scsi_remove_target(struct scsi_target *starget)
 void scsi_remove_target(struct device *dev)
 {
        struct Scsi_Host *shost = dev_to_shost(dev->parent);
-       struct scsi_target *starget, *last = NULL;
+       struct scsi_target *starget;
        unsigned long flags;
 
-       /* remove targets being careful to lookup next entry before
-        * deleting the last
-        */
+restart:
        spin_lock_irqsave(shost->host_lock, flags);
        list_for_each_entry(starget, &shost->__targets, siblings) {
                if (starget->state == STARGET_DEL)
                        continue;
                if (starget->dev.parent == dev || &starget->dev == dev) {
-                       /* assuming new targets arrive at the end */
                        kref_get(&starget->reap_ref);
                        spin_unlock_irqrestore(shost->host_lock, flags);
-                       if (last)
-                               scsi_target_reap(last);
-                       last = starget;
                        __scsi_remove_target(starget);
-                       spin_lock_irqsave(shost->host_lock, flags);
+                       scsi_target_reap(starget);
+                       goto restart;
                }
        }
        spin_unlock_irqrestore(shost->host_lock, flags);
-
-       if (last)
-               scsi_target_reap(last);
 }
 EXPORT_SYMBOL(scsi_remove_target);