]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blobdiff - drivers/scsi/scsi_transport_fc.c
block: add reference counting for struct bsg_job
[mirror_ubuntu-zesty-kernel.git] / drivers / scsi / scsi_transport_fc.c
index 0f3a3869524bc089f57b2c4e87d44c4875fe919f..9009acc27aedc997793357e590d971af628d7e53 100644 (file)
@@ -2592,7 +2592,7 @@ fc_rport_final_delete(struct work_struct *work)
 
 
 /**
- * fc_rport_create - allocates and creates a remote FC port.
+ * fc_remote_port_create - allocates and creates a remote FC port.
  * @shost:     scsi host the remote port is connected to.
  * @channel:   Channel on shost port connected to.
  * @ids:       The world wide names, fc address, and FC4 port
@@ -2605,8 +2605,8 @@ fc_rport_final_delete(struct work_struct *work)
  *     This routine assumes no locks are held on entry.
  */
 static struct fc_rport *
-fc_rport_create(struct Scsi_Host *shost, int channel,
-       struct fc_rport_identifiers  *ids)
+fc_remote_port_create(struct Scsi_Host *shost, int channel,
+                     struct fc_rport_identifiers  *ids)
 {
        struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
        struct fc_internal *fci = to_fc_internal(shost->transportt);
@@ -2914,7 +2914,7 @@ fc_remote_port_add(struct Scsi_Host *shost, int channel,
        spin_unlock_irqrestore(shost->host_lock, flags);
 
        /* No consistent binding found - create new remote port entry */
-       rport = fc_rport_create(shost, channel, ids);
+       rport = fc_remote_port_create(shost, channel, ids);
 
        return rport;
 }
@@ -3560,16 +3560,12 @@ fc_vport_sched_delete(struct work_struct *work)
  * @job:       fc_bsg_job that is to be torn down
  */
 static void
-fc_destroy_bsgjob(struct fc_bsg_job *job)
+fc_destroy_bsgjob(struct kref *kref)
 {
-       unsigned long flags;
+       struct fc_bsg_job *job = container_of(kref, struct fc_bsg_job, kref);
+       struct request *rq = job->req;
 
-       spin_lock_irqsave(&job->job_lock, flags);
-       if (job->ref_cnt) {
-               spin_unlock_irqrestore(&job->job_lock, flags);
-               return;
-       }
-       spin_unlock_irqrestore(&job->job_lock, flags);
+       blk_end_request_all(rq, rq->errors);
 
        put_device(job->dev);   /* release reference for the request */
 
@@ -3582,15 +3578,17 @@ fc_destroy_bsgjob(struct fc_bsg_job *job)
  * fc_bsg_jobdone - completion routine for bsg requests that the LLD has
  *                  completed
  * @job:       fc_bsg_job that is complete
+ * @result:    job reply result
+ * @reply_payload_rcv_len: length of payload received
  */
-static void
-fc_bsg_jobdone(struct fc_bsg_job *job)
+void fc_bsg_jobdone(struct fc_bsg_job *job, int result,
+                   unsigned int reply_payload_rcv_len)
 {
        struct request *req = job->req;
        struct request *rsp = req->next_rq;
        int err;
 
-       err = job->req->errors = job->reply->result;
+       err = job->req->errors = result;
 
        if (err < 0)
                /* we're only returning the result field in the reply */
@@ -3602,14 +3600,14 @@ fc_bsg_jobdone(struct fc_bsg_job *job)
        req->resid_len = 0;
 
        if (rsp) {
-               WARN_ON(job->reply->reply_payload_rcv_len > rsp->resid_len);
+               WARN_ON(reply_payload_rcv_len > rsp->resid_len);
 
                /* set reply (bidi) residual */
-               rsp->resid_len -= min(job->reply->reply_payload_rcv_len,
-                                     rsp->resid_len);
+               rsp->resid_len -= min(reply_payload_rcv_len, rsp->resid_len);
        }
        blk_complete_request(req);
 }
+EXPORT_SYMBOL_GPL(fc_bsg_jobdone);
 
 /**
  * fc_bsg_softirq_done - softirq done routine for destroying the bsg requests
@@ -3618,15 +3616,8 @@ fc_bsg_jobdone(struct fc_bsg_job *job)
 static void fc_bsg_softirq_done(struct request *rq)
 {
        struct fc_bsg_job *job = rq->special;
-       unsigned long flags;
-
-       spin_lock_irqsave(&job->job_lock, flags);
-       job->state_flags |= FC_RQST_STATE_DONE;
-       job->ref_cnt--;
-       spin_unlock_irqrestore(&job->job_lock, flags);
 
-       blk_end_request_all(rq, rq->errors);
-       fc_destroy_bsgjob(job);
+       kref_put(&job->kref, fc_destroy_bsgjob);
 }
 
 /**
@@ -3637,26 +3628,21 @@ static enum blk_eh_timer_return
 fc_bsg_job_timeout(struct request *req)
 {
        struct fc_bsg_job *job = (void *) req->special;
-       struct Scsi_Host *shost = job->shost;
+       struct Scsi_Host *shost = fc_bsg_to_shost(job);
+       struct fc_rport *rport = fc_bsg_to_rport(job);
        struct fc_internal *i = to_fc_internal(shost->transportt);
-       unsigned long flags;
-       int err = 0, done = 0;
+       int err = 0, inflight = 0;
 
-       if (job->rport && job->rport->port_state == FC_PORTSTATE_BLOCKED)
+       if (rport && rport->port_state == FC_PORTSTATE_BLOCKED)
                return BLK_EH_RESET_TIMER;
 
-       spin_lock_irqsave(&job->job_lock, flags);
-       if (job->state_flags & FC_RQST_STATE_DONE)
-               done = 1;
-       else
-               job->ref_cnt++;
-       spin_unlock_irqrestore(&job->job_lock, flags);
+       inflight = kref_get_unless_zero(&job->kref);
 
-       if (!done && i->f->bsg_timeout) {
+       if (inflight && i->f->bsg_timeout) {
                /* call LLDD to abort the i/o as it has timed out */
                err = i->f->bsg_timeout(job);
                if (err == -EAGAIN) {
-                       job->ref_cnt--;
+                       kref_put(&job->kref, fc_destroy_bsgjob);
                        return BLK_EH_RESET_TIMER;
                } else if (err)
                        printk(KERN_ERR "ERROR: FC BSG request timeout - LLD "
@@ -3664,14 +3650,14 @@ fc_bsg_job_timeout(struct request *req)
        }
 
        /* the blk_end_sync_io() doesn't check the error */
-       if (done)
+       if (!inflight)
                return BLK_EH_NOT_HANDLED;
        else
                return BLK_EH_HANDLED;
 }
 
 static int
-fc_bsg_map_buffer(struct fc_bsg_buffer *buf, struct request *req)
+fc_bsg_map_buffer(struct bsg_buffer *buf, struct request *req)
 {
        size_t sz = (sizeof(struct scatterlist) * req->nr_phys_segments);
 
@@ -3725,7 +3711,6 @@ fc_req_to_bsgjob(struct Scsi_Host *shost, struct fc_rport *rport,
        job->req = req;
        if (i->f->dd_bsg_size)
                job->dd_data = (void *)&job[1];
-       spin_lock_init(&job->job_lock);
        job->request = (struct fc_bsg_request *)req->cmd;
        job->request_len = req->cmd_len;
        job->reply = req->sense;
@@ -3741,14 +3726,13 @@ fc_req_to_bsgjob(struct Scsi_Host *shost, struct fc_rport *rport,
                if (ret)
                        goto failjob_rls_rqst_payload;
        }
-       job->job_done = fc_bsg_jobdone;
        if (rport)
                job->dev = &rport->dev;
        else
                job->dev = &shost->shost_gendev;
        get_device(job->dev);           /* take a reference for the request */
 
-       job->ref_cnt = 1;
+       kref_init(&job->kref);
 
        return 0;
 
@@ -3779,11 +3763,19 @@ fc_bsg_host_dispatch(struct request_queue *q, struct Scsi_Host *shost,
                         struct fc_bsg_job *job)
 {
        struct fc_internal *i = to_fc_internal(shost->transportt);
+       struct fc_bsg_request *bsg_request = job->request;
+       struct fc_bsg_reply *bsg_reply = job->reply;
        int cmdlen = sizeof(uint32_t);  /* start with length of msgcode */
        int ret;
 
+       /* check if we really have all the request data needed */
+       if (job->request_len < cmdlen) {
+               ret = -ENOMSG;
+               goto fail_host_msg;
+       }
+
        /* Validate the host command */
-       switch (job->request->msgcode) {
+       switch (bsg_request->msgcode) {
        case FC_BSG_HST_ADD_RPORT:
                cmdlen += sizeof(struct fc_bsg_host_add_rport);
                break;
@@ -3815,7 +3807,7 @@ fc_bsg_host_dispatch(struct request_queue *q, struct Scsi_Host *shost,
        case FC_BSG_HST_VENDOR:
                cmdlen += sizeof(struct fc_bsg_host_vendor);
                if ((shost->hostt->vendor_id == 0L) ||
-                   (job->request->rqst_data.h_vendor.vendor_id !=
+                   (bsg_request->rqst_data.h_vendor.vendor_id !=
                        shost->hostt->vendor_id)) {
                        ret = -ESRCH;
                        goto fail_host_msg;
@@ -3827,12 +3819,6 @@ fc_bsg_host_dispatch(struct request_queue *q, struct Scsi_Host *shost,
                goto fail_host_msg;
        }
 
-       /* check if we really have all the request data needed */
-       if (job->request_len < cmdlen) {
-               ret = -ENOMSG;
-               goto fail_host_msg;
-       }
-
        ret = i->f->bsg_request(job);
        if (!ret)
                return FC_DISPATCH_UNLOCKED;
@@ -3840,10 +3826,11 @@ fc_bsg_host_dispatch(struct request_queue *q, struct Scsi_Host *shost,
 fail_host_msg:
        /* return the errno failure code as the only status */
        BUG_ON(job->reply_len < sizeof(uint32_t));
-       job->reply->reply_payload_rcv_len = 0;
-       job->reply->result = ret;
+       bsg_reply->reply_payload_rcv_len = 0;
+       bsg_reply->result = ret;
        job->reply_len = sizeof(uint32_t);
-       fc_bsg_jobdone(job);
+       fc_bsg_jobdone(job, bsg_reply->result,
+                      bsg_reply->reply_payload_rcv_len);
        return FC_DISPATCH_UNLOCKED;
 }
 
@@ -3855,15 +3842,15 @@ fail_host_msg:
 static void
 fc_bsg_goose_queue(struct fc_rport *rport)
 {
-       if (!rport->rqst_q)
+       struct request_queue *q = rport->rqst_q;
+       unsigned long flags;
+
+       if (!q)
                return;
 
-       /*
-        * This get/put dance makes no sense
-        */
-       get_device(&rport->dev);
-       blk_run_queue_async(rport->rqst_q);
-       put_device(&rport->dev);
+       spin_lock_irqsave(q->queue_lock, flags);
+       blk_run_queue_async(q);
+       spin_unlock_irqrestore(q->queue_lock, flags);
 }
 
 /**
@@ -3878,11 +3865,19 @@ fc_bsg_rport_dispatch(struct request_queue *q, struct Scsi_Host *shost,
                         struct fc_rport *rport, struct fc_bsg_job *job)
 {
        struct fc_internal *i = to_fc_internal(shost->transportt);
+       struct fc_bsg_request *bsg_request = job->request;
+       struct fc_bsg_reply *bsg_reply = job->reply;
        int cmdlen = sizeof(uint32_t);  /* start with length of msgcode */
        int ret;
 
+       /* check if we really have all the request data needed */
+       if (job->request_len < cmdlen) {
+               ret = -ENOMSG;
+               goto fail_rport_msg;
+       }
+
        /* Validate the rport command */
-       switch (job->request->msgcode) {
+       switch (bsg_request->msgcode) {
        case FC_BSG_RPT_ELS:
                cmdlen += sizeof(struct fc_bsg_rport_els);
                goto check_bidi;
@@ -3902,12 +3897,6 @@ check_bidi:
                goto fail_rport_msg;
        }
 
-       /* check if we really have all the request data needed */
-       if (job->request_len < cmdlen) {
-               ret = -ENOMSG;
-               goto fail_rport_msg;
-       }
-
        ret = i->f->bsg_request(job);
        if (!ret)
                return FC_DISPATCH_UNLOCKED;
@@ -3915,10 +3904,11 @@ check_bidi:
 fail_rport_msg:
        /* return the errno failure code as the only status */
        BUG_ON(job->reply_len < sizeof(uint32_t));
-       job->reply->reply_payload_rcv_len = 0;
-       job->reply->result = ret;
+       bsg_reply->reply_payload_rcv_len = 0;
+       bsg_reply->result = ret;
        job->reply_len = sizeof(uint32_t);
-       fc_bsg_jobdone(job);
+       fc_bsg_jobdone(job, bsg_reply->result,
+                      bsg_reply->reply_payload_rcv_len);
        return FC_DISPATCH_UNLOCKED;
 }
 
@@ -3937,6 +3927,7 @@ fc_bsg_request_handler(struct request_queue *q, struct Scsi_Host *shost,
        struct request *req;
        struct fc_bsg_job *job;
        enum fc_dispatch_result ret;
+       struct fc_bsg_reply *bsg_reply;
 
        if (!get_device(dev))
                return;
@@ -3973,10 +3964,12 @@ fc_bsg_request_handler(struct request_queue *q, struct Scsi_Host *shost,
                /* check if we have the msgcode value at least */
                if (job->request_len < sizeof(uint32_t)) {
                        BUG_ON(job->reply_len < sizeof(uint32_t));
-                       job->reply->reply_payload_rcv_len = 0;
-                       job->reply->result = -ENOMSG;
+                       bsg_reply = job->reply;
+                       bsg_reply->reply_payload_rcv_len = 0;
+                       bsg_reply->result = -ENOMSG;
                        job->reply_len = sizeof(uint32_t);
-                       fc_bsg_jobdone(job);
+                       fc_bsg_jobdone(job, bsg_reply->result,
+                                      bsg_reply->reply_payload_rcv_len);
                        spin_lock_irq(q->queue_lock);
                        continue;
                }