blob: 4f52f279205942f48c2870d4ee77447d0aea515a [file] [log] [blame]
Jens Axboe86db1e22008-01-29 14:53:40 +01001/*
2 * Functions related to setting various queue properties from drivers
3 */
4#include <linux/kernel.h>
5#include <linux/module.h>
6#include <linux/bio.h>
7#include <linux/blkdev.h>
8
9#include "blk.h"
10
11/*
12 * for max sense size
13 */
14#include <scsi/scsi_cmnd.h>
15
16/**
17 * blk_end_sync_rq - executes a completion event on a request
18 * @rq: request to complete
19 * @error: end io status of the request
20 */
21void blk_end_sync_rq(struct request *rq, int error)
22{
23 struct completion *waiting = rq->end_io_data;
24
25 rq->end_io_data = NULL;
26 __blk_put_request(rq->q, rq);
27
28 /*
29 * complete last, if this is a stack request the process (and thus
30 * the rq pointer) could be invalid right after this complete()
31 */
32 complete(waiting);
33}
34EXPORT_SYMBOL(blk_end_sync_rq);
35
36/**
37 * blk_execute_rq_nowait - insert a request into queue for execution
38 * @q: queue to insert the request in
39 * @bd_disk: matching gendisk
40 * @rq: request to insert
41 * @at_head: insert request at head or tail of queue
42 * @done: I/O completion handler
43 *
44 * Description:
45 * Insert a fully prepared request at the back of the io scheduler queue
46 * for execution. Don't wait for completion.
47 */
48void blk_execute_rq_nowait(struct request_queue *q, struct gendisk *bd_disk,
49 struct request *rq, int at_head,
50 rq_end_io_fn *done)
51{
52 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
53
54 rq->rq_disk = bd_disk;
55 rq->cmd_flags |= REQ_NOMERGE;
56 rq->end_io = done;
57 WARN_ON(irqs_disabled());
58 spin_lock_irq(q->queue_lock);
59 __elv_add_request(q, rq, where, 1);
60 __generic_unplug_device(q);
Bartlomiej Zolnierkiewicz9a2d43b2008-07-15 21:21:43 +020061 /* the queue is stopped so it won't be plugged+unplugged */
62 if (blk_pm_resume_request(rq))
63 q->request_fn(q);
Jens Axboe86db1e22008-01-29 14:53:40 +010064 spin_unlock_irq(q->queue_lock);
65}
66EXPORT_SYMBOL_GPL(blk_execute_rq_nowait);
67
68/**
69 * blk_execute_rq - insert a request into queue for execution
70 * @q: queue to insert the request in
71 * @bd_disk: matching gendisk
72 * @rq: request to insert
73 * @at_head: insert request at head or tail of queue
74 *
75 * Description:
76 * Insert a fully prepared request at the back of the io scheduler queue
77 * for execution and wait for completion.
78 */
79int blk_execute_rq(struct request_queue *q, struct gendisk *bd_disk,
80 struct request *rq, int at_head)
81{
82 DECLARE_COMPLETION_ONSTACK(wait);
83 char sense[SCSI_SENSE_BUFFERSIZE];
84 int err = 0;
85
86 /*
87 * we need an extra reference to the request, so we can look at
88 * it after io completion
89 */
90 rq->ref_count++;
91
92 if (!rq->sense) {
93 memset(sense, 0, sizeof(sense));
94 rq->sense = sense;
95 rq->sense_len = 0;
96 }
97
98 rq->end_io_data = &wait;
99 blk_execute_rq_nowait(q, bd_disk, rq, at_head, blk_end_sync_rq);
100 wait_for_completion(&wait);
101
102 if (rq->errors)
103 err = -EIO;
104
105 return err;
106}
Jens Axboe86db1e22008-01-29 14:53:40 +0100107EXPORT_SYMBOL(blk_execute_rq);