Merge tag 'nvme-5.14-2021-07-22' of git://git.infradead.org/nvme into block-5.14
[linux-2.6-block.git] / drivers / nvme / target / fc.c
index d375745fc4ed3d13d14b6c01a36727fcf94f1059..22b5108168a6a2d2bfd62b047aaf5c2cb9522e53 100644 (file)
@@ -1020,61 +1020,76 @@ nvmet_fc_free_hostport(struct nvmet_fc_hostport *hostport)
        nvmet_fc_hostport_put(hostport);
 }
 
+static struct nvmet_fc_hostport *
+nvmet_fc_match_hostport(struct nvmet_fc_tgtport *tgtport, void *hosthandle)
+{
+       struct nvmet_fc_hostport *host;
+
+       lockdep_assert_held(&tgtport->lock);
+
+       list_for_each_entry(host, &tgtport->host_list, host_list) {
+               if (host->hosthandle == hosthandle && !host->invalid) {
+                       if (nvmet_fc_hostport_get(host))
+                               return (host);
+               }
+       }
+
+       return NULL;
+}
+
 static struct nvmet_fc_hostport *
 nvmet_fc_alloc_hostport(struct nvmet_fc_tgtport *tgtport, void *hosthandle)
 {
-       struct nvmet_fc_hostport *newhost, *host, *match = NULL;
+       struct nvmet_fc_hostport *newhost, *match = NULL;
        unsigned long flags;
 
        /* if LLDD not implemented, leave as NULL */
        if (!hosthandle)
                return NULL;
 
-       /* take reference for what will be the newly allocated hostport */
+       /*
+        * take reference for what will be the newly allocated hostport if
+        * we end up using a new allocation
+        */
        if (!nvmet_fc_tgtport_get(tgtport))
                return ERR_PTR(-EINVAL);
 
+       spin_lock_irqsave(&tgtport->lock, flags);
+       match = nvmet_fc_match_hostport(tgtport, hosthandle);
+       spin_unlock_irqrestore(&tgtport->lock, flags);
+
+       if (match) {
+               /* no new allocation - release reference */
+               nvmet_fc_tgtport_put(tgtport);
+               return match;
+       }
+
        newhost = kzalloc(sizeof(*newhost), GFP_KERNEL);
        if (!newhost) {
-               spin_lock_irqsave(&tgtport->lock, flags);
-               list_for_each_entry(host, &tgtport->host_list, host_list) {
-                       if (host->hosthandle == hosthandle && !host->invalid) {
-                               if (nvmet_fc_hostport_get(host)) {
-                                       match = host;
-                                       break;
-                               }
-                       }
-               }
-               spin_unlock_irqrestore(&tgtport->lock, flags);
-               /* no allocation - release reference */
+               /* no new allocation - release reference */
                nvmet_fc_tgtport_put(tgtport);
-               return (match) ? match : ERR_PTR(-ENOMEM);
+               return ERR_PTR(-ENOMEM);
        }
 
-       newhost->tgtport = tgtport;
-       newhost->hosthandle = hosthandle;
-       INIT_LIST_HEAD(&newhost->host_list);
-       kref_init(&newhost->ref);
-
        spin_lock_irqsave(&tgtport->lock, flags);
-       list_for_each_entry(host, &tgtport->host_list, host_list) {
-               if (host->hosthandle == hosthandle && !host->invalid) {
-                       if (nvmet_fc_hostport_get(host)) {
-                               match = host;
-                               break;
-                       }
-               }
-       }
+       match = nvmet_fc_match_hostport(tgtport, hosthandle);
        if (match) {
+               /* new allocation not needed */
                kfree(newhost);
-               newhost = NULL;
-               /* releasing allocation - release reference */
+               newhost = match;
+               /* no new allocation - release reference */
                nvmet_fc_tgtport_put(tgtport);
-       } else
+       } else {
+               newhost->tgtport = tgtport;
+               newhost->hosthandle = hosthandle;
+               INIT_LIST_HEAD(&newhost->host_list);
+               kref_init(&newhost->ref);
+
                list_add_tail(&newhost->host_list, &tgtport->host_list);
+       }
        spin_unlock_irqrestore(&tgtport->lock, flags);
 
-       return (match) ? match : newhost;
+       return newhost;
 }
 
 static void
@@ -1996,6 +2011,7 @@ nvmet_fc_handle_ls_rqst_work(struct work_struct *work)
  *
  * @target_port: pointer to the (registered) target port the LS was
  *              received on.
+ * @hosthandle: pointer to the host specific data, gets stored in iod.
  * @lsrsp:      pointer to a lsrsp structure to be used to reference
  *              the exchange corresponding to the LS.
  * @lsreqbuf:   pointer to the buffer containing the LS Request
@@ -2494,13 +2510,6 @@ nvmet_fc_handle_fcp_rqst(struct nvmet_fc_tgtport *tgtport,
        u32 xfrlen = be32_to_cpu(cmdiu->data_len);
        int ret;
 
-       /*
-        * if there is no nvmet mapping to the targetport there
-        * shouldn't be requests. just terminate them.
-        */
-       if (!tgtport->pe)
-               goto transport_error;
-
        /*
         * Fused commands are currently not supported in the linux
         * implementation.
@@ -2528,7 +2537,8 @@ nvmet_fc_handle_fcp_rqst(struct nvmet_fc_tgtport *tgtport,
 
        fod->req.cmd = &fod->cmdiubuf.sqe;
        fod->req.cqe = &fod->rspiubuf.cqe;
-       fod->req.port = tgtport->pe->port;
+       if (tgtport->pe)
+               fod->req.port = tgtport->pe->port;
 
        /* clear any response payload */
        memset(&fod->rspiubuf, 0, sizeof(fod->rspiubuf));