[SCSI] iscsi: break up session creation into two stages
[linux-2.6.git] / drivers / scsi / scsi_transport_iscsi.c
index 147c854..f39da0c 100644 (file)
@@ -228,13 +228,11 @@ static struct iscsi_cls_conn *iscsi_conn_lookup(uint32_t sid, uint32_t cid)
 static void iscsi_session_release(struct device *dev)
 {
        struct iscsi_cls_session *session = iscsi_dev_to_session(dev);
-       struct iscsi_transport *transport = session->transport;
        struct Scsi_Host *shost;
 
        shost = iscsi_session_to_shost(session);
        scsi_host_put(shost);
        kfree(session);
-       module_put(transport->owner);
 }
 
 static int iscsi_is_session_dev(const struct device *dev)
@@ -250,10 +248,9 @@ static int iscsi_user_scan(struct Scsi_Host *shost, uint channel,
 
        mutex_lock(&ihost->mutex);
        list_for_each_entry(session, &ihost->sessions, host_list) {
-               if ((channel == SCAN_WILD_CARD ||
-                    channel == session->channel) &&
+               if ((channel == SCAN_WILD_CARD || channel == 0) &&
                    (id == SCAN_WILD_CARD || id == session->target_id))
-                       scsi_scan_target(&session->dev, session->channel,
+                       scsi_scan_target(&session->dev, 0,
                                         session->target_id, lun, 1);
        }
        mutex_unlock(&ihost->mutex);
@@ -290,80 +287,92 @@ void iscsi_block_session(struct iscsi_cls_session *session)
 }
 EXPORT_SYMBOL_GPL(iscsi_block_session);
 
-/**
- * iscsi_create_session - create iscsi class session
- * @shost: scsi host
- * @transport: iscsi transport
- *
- * This can be called from a LLD or iscsi_transport.
- **/
 struct iscsi_cls_session *
-iscsi_create_session(struct Scsi_Host *shost,
-                    struct iscsi_transport *transport, int channel)
+iscsi_alloc_session(struct Scsi_Host *shost,
+                   struct iscsi_transport *transport)
 {
-       struct iscsi_host *ihost;
        struct iscsi_cls_session *session;
-       int err;
-
-       if (!try_module_get(transport->owner))
-               return NULL;
 
        session = kzalloc(sizeof(*session) + transport->sessiondata_size,
                          GFP_KERNEL);
        if (!session)
-               goto module_put;
+               return NULL;
+
        session->transport = transport;
        session->recovery_tmo = 120;
        INIT_WORK(&session->recovery_work, session_recovery_timedout, session);
        INIT_LIST_HEAD(&session->host_list);
        INIT_LIST_HEAD(&session->sess_list);
 
+       session->dev.parent = &shost->shost_gendev;
+       session->dev.release = iscsi_session_release;
+       device_initialize(&session->dev);
        if (transport->sessiondata_size)
                session->dd_data = &session[1];
+       return session;
+}
+EXPORT_SYMBOL_GPL(iscsi_alloc_session);
+
+int iscsi_add_session(struct iscsi_cls_session *session)
+{
+       struct Scsi_Host *shost = iscsi_session_to_shost(session);
+       struct iscsi_host *ihost;
+       int err;
 
        /* this is released in the dev's release function */
        scsi_host_get(shost);
        ihost = shost->shost_data;
 
        session->sid = iscsi_session_nr++;
-       session->channel = channel;
        session->target_id = ihost->next_target_id++;
 
        snprintf(session->dev.bus_id, BUS_ID_SIZE, "session%u",
                 session->sid);
-       session->dev.parent = &shost->shost_gendev;
-       session->dev.release = iscsi_session_release;
-       err = device_register(&session->dev);
+       err = device_add(&session->dev);
        if (err) {
                dev_printk(KERN_ERR, &session->dev, "iscsi: could not "
                           "register session's dev\n");
-               goto free_session;
+               goto release_host;
        }
        transport_register_device(&session->dev);
 
        mutex_lock(&ihost->mutex);
        list_add(&session->host_list, &ihost->sessions);
        mutex_unlock(&ihost->mutex);
+       return 0;
 
-       return session;
-
-free_session:
-       kfree(session);
-module_put:
-       module_put(transport->owner);
-       return NULL;
+release_host:
+       scsi_host_put(shost);
+       return err;
 }
-
-EXPORT_SYMBOL_GPL(iscsi_create_session);
+EXPORT_SYMBOL_GPL(iscsi_add_session);
 
 /**
- * iscsi_destroy_session - destroy iscsi session
- * @session: iscsi_session
+ * iscsi_create_session - create iscsi class session
+ * @shost: scsi host
+ * @transport: iscsi transport
  *
- * Can be called by a LLD or iscsi_transport. There must not be
- * any running connections.
+ * This can be called from a LLD or iscsi_transport.
  **/
-int iscsi_destroy_session(struct iscsi_cls_session *session)
+struct iscsi_cls_session *
+iscsi_create_session(struct Scsi_Host *shost,
+                    struct iscsi_transport *transport)
+{
+       struct iscsi_cls_session *session;
+
+       session = iscsi_alloc_session(shost, transport);
+       if (!session)
+               return NULL;
+
+       if (iscsi_add_session(session)) {
+               iscsi_free_session(session);
+               return NULL;
+       }
+       return session;
+}
+EXPORT_SYMBOL_GPL(iscsi_create_session);
+
+void iscsi_remove_session(struct iscsi_cls_session *session)
 {
        struct Scsi_Host *shost = iscsi_session_to_shost(session);
        struct iscsi_host *ihost = shost->shost_data;
@@ -375,11 +384,33 @@ int iscsi_destroy_session(struct iscsi_cls_session *session)
        list_del(&session->host_list);
        mutex_unlock(&ihost->mutex);
 
+       scsi_remove_target(&session->dev);
+
        transport_unregister_device(&session->dev);
-       device_unregister(&session->dev);
-       return 0;
+       device_del(&session->dev);
+}
+EXPORT_SYMBOL_GPL(iscsi_remove_session);
+
+void iscsi_free_session(struct iscsi_cls_session *session)
+{
+       put_device(&session->dev);
 }
 
+EXPORT_SYMBOL_GPL(iscsi_free_session);
+
+/**
+ * iscsi_destroy_session - destroy iscsi session
+ * @session: iscsi_session
+ *
+ * Can be called by a LLD or iscsi_transport. There must not be
+ * any running connections.
+ **/
+int iscsi_destroy_session(struct iscsi_cls_session *session)
+{
+       iscsi_remove_session(session);
+       iscsi_free_session(session);
+       return 0;
+}
 EXPORT_SYMBOL_GPL(iscsi_destroy_session);
 
 static void iscsi_conn_release(struct device *dev)