[SCSI] rework scsi_target allocation
authorJames Bottomley <James.Bottomley@HansenPartnership.com>
Sun, 23 Mar 2008 03:42:27 +0000 (22:42 -0500)
committerJames Bottomley <James.Bottomley@HansenPartnership.com>
Tue, 22 Apr 2008 20:16:31 +0000 (15:16 -0500)
The current target allocation code registeres each possible target
with sysfs; it will be deleted again if no useable LUN on this target
was found. This results in a string of 'target add/target remove' uevents.

Based on a patch by Hannes Reinecke <hare@suse.de> this patch reworks
the target allocation code so that only uevents for existing targets
are sent. The sysfs registration is split off from the existing
scsi_target_alloc() into a in a new scsi_add_target() function, which
should be called whenever an existing target is found. Only then a
uevent is sent, so we'll be generating events for existing targets
only.

Signed-off-by: James Bottomley <James.Bottomley@HansenPartnership.com>
drivers/scsi/scsi_scan.c
drivers/scsi/scsi_sysfs.c
include/scsi/scsi_device.h

index e1644b270cdc80132fd0aeb92a5b4d5f0c087363..fcd7455ffc39acf226233c10c3a45c1ee76537e0 100644 (file)
@@ -322,6 +322,21 @@ out:
        return NULL;
 }
 
+static void scsi_target_destroy(struct scsi_target *starget)
+{
+       struct device *dev = &starget->dev;
+       struct Scsi_Host *shost = dev_to_shost(dev->parent);
+       unsigned long flags;
+
+       transport_destroy_device(dev);
+       spin_lock_irqsave(shost->host_lock, flags);
+       if (shost->hostt->target_destroy)
+               shost->hostt->target_destroy(starget);
+       list_del_init(&starget->siblings);
+       spin_unlock_irqrestore(shost->host_lock, flags);
+       put_device(dev);
+}
+
 static void scsi_target_dev_release(struct device *dev)
 {
        struct device *parent = dev->parent;
@@ -406,7 +421,7 @@ static struct scsi_target *scsi_alloc_target(struct device *parent,
        starget->channel = channel;
        INIT_LIST_HEAD(&starget->siblings);
        INIT_LIST_HEAD(&starget->devices);
-       starget->state = STARGET_RUNNING;
+       starget->state = STARGET_CREATED;
        starget->scsi_level = SCSI_2;
  retry:
        spin_lock_irqsave(shost->host_lock, flags);
@@ -419,18 +434,6 @@ static struct scsi_target *scsi_alloc_target(struct device *parent,
        spin_unlock_irqrestore(shost->host_lock, flags);
        /* allocate and add */
        transport_setup_device(dev);
-       error = device_add(dev);
-       if (error) {
-               dev_err(dev, "target device_add failed, error %d\n", error);
-               spin_lock_irqsave(shost->host_lock, flags);
-               list_del_init(&starget->siblings);
-               spin_unlock_irqrestore(shost->host_lock, flags);
-               transport_destroy_device(dev);
-               put_device(parent);
-               kfree(starget);
-               return NULL;
-       }
-       transport_add_device(dev);
        if (shost->hostt->target_alloc) {
                error = shost->hostt->target_alloc(starget);
 
@@ -438,9 +441,7 @@ static struct scsi_target *scsi_alloc_target(struct device *parent,
                        dev_printk(KERN_ERR, dev, "target allocation failed, error %d\n", error);
                        /* don't want scsi_target_reap to do the final
                         * put because it will be under the host lock */
-                       get_device(dev);
-                       scsi_target_reap(starget);
-                       put_device(dev);
+                       scsi_target_destroy(starget);
                        return NULL;
                }
        }
@@ -467,18 +468,10 @@ static void scsi_target_reap_usercontext(struct work_struct *work)
 {
        struct scsi_target *starget =
                container_of(work, struct scsi_target, ew.work);
-       struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
-       unsigned long flags;
 
        transport_remove_device(&starget->dev);
        device_del(&starget->dev);
-       transport_destroy_device(&starget->dev);
-       spin_lock_irqsave(shost->host_lock, flags);
-       if (shost->hostt->target_destroy)
-               shost->hostt->target_destroy(starget);
-       list_del_init(&starget->siblings);
-       spin_unlock_irqrestore(shost->host_lock, flags);
-       put_device(&starget->dev);
+       scsi_target_destroy(starget);
 }
 
 /**
@@ -493,21 +486,25 @@ void scsi_target_reap(struct scsi_target *starget)
 {
        struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
        unsigned long flags;
+       enum scsi_target_state state;
+       int empty;
 
        spin_lock_irqsave(shost->host_lock, flags);
+       state = starget->state;
+       empty = --starget->reap_ref == 0 &&
+               list_empty(&starget->devices) ? 1 : 0;
+       spin_unlock_irqrestore(shost->host_lock, flags);
 
-       if (--starget->reap_ref == 0 && list_empty(&starget->devices)) {
-               BUG_ON(starget->state == STARGET_DEL);
-               starget->state = STARGET_DEL;
-               spin_unlock_irqrestore(shost->host_lock, flags);
-               execute_in_process_context(scsi_target_reap_usercontext,
-                                          &starget->ew);
+       if (!empty)
                return;
 
-       }
-       spin_unlock_irqrestore(shost->host_lock, flags);
-
-       return;
+       BUG_ON(state == STARGET_DEL);
+       starget->state = STARGET_DEL;
+       if (state == STARGET_CREATED)
+               scsi_target_destroy(starget);
+       else
+               execute_in_process_context(scsi_target_reap_usercontext,
+                                          &starget->ew);
 }
 
 /**
@@ -1056,8 +1053,9 @@ static int scsi_probe_and_add_lun(struct scsi_target *starget,
                                        scsi_inq_str(vend, result, 8, 16),
                                        scsi_inq_str(mod, result, 16, 32));
                        });
+
                }
-               
+
                res = SCSI_SCAN_TARGET_PRESENT;
                goto out_free_result;
        }
@@ -1497,7 +1495,6 @@ struct scsi_device *__scsi_add_device(struct Scsi_Host *shost, uint channel,
        if (scsi_host_scan_allowed(shost))
                scsi_probe_and_add_lun(starget, lun, NULL, &sdev, 1, hostdata);
        mutex_unlock(&shost->scan_mutex);
-       transport_configure_device(&starget->dev);
        scsi_target_reap(starget);
        put_device(&starget->dev);
 
@@ -1578,7 +1575,6 @@ static void __scsi_scan_target(struct device *parent, unsigned int channel,
  out_reap:
        /* now determine if the target has any children at all
         * and if not, nuke it */
-       transport_configure_device(&starget->dev);
        scsi_target_reap(starget);
 
        put_device(&starget->dev);
index 84e2a8ad83c9509a779b8f83727ce2413335537c..198aa4571e358f75bebec9e8a2267828392ad3b4 100644 (file)
@@ -809,6 +809,27 @@ sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr,
        return count;
 }
 
+static int scsi_target_add(struct scsi_target *starget)
+{
+       int error;
+
+       if (starget->state != STARGET_CREATED)
+               return 0;
+
+       error = device_add(&starget->dev);
+       if (error) {
+               dev_err(&starget->dev, "target device_add failed, error %d\n", error);
+               get_device(&starget->dev);
+               scsi_target_reap(starget);
+               put_device(&starget->dev);
+               return error;
+       }
+       transport_add_device(&starget->dev);
+       starget->state = STARGET_RUNNING;
+
+       return 0;
+}
+
 static struct device_attribute sdev_attr_queue_type_rw =
        __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
               sdev_store_queue_type_rw);
@@ -824,10 +845,16 @@ int scsi_sysfs_add_sdev(struct scsi_device *sdev)
 {
        int error, i;
        struct request_queue *rq = sdev->request_queue;
+       struct scsi_target *starget = sdev->sdev_target;
 
        if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
                return error;
 
+       error = scsi_target_add(starget);
+       if (error)
+               return error;
+
+       transport_configure_device(&starget->dev);
        error = device_add(&sdev->sdev_gendev);
        if (error) {
                put_device(sdev->sdev_gendev.parent);
index b8b19e2f57bbece76e739752961ce91979512f23..f6a9fe0ef09c62cf38b28f6cdd9bfd9bad06121a 100644 (file)
@@ -181,7 +181,8 @@ struct scsi_device {
        sdev_printk(prefix, (scmd)->device, fmt, ##a)
 
 enum scsi_target_state {
-       STARGET_RUNNING = 1,
+       STARGET_CREATED = 1,
+       STARGET_RUNNING,
        STARGET_DEL,
 };