blob: 2c73ed1a8131f9e02508dee0aee8e6b36f6b8c7e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 1994, Karl Keyte: Added support for disk statistics
4 * Elevator latency, (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
5 * Queue request tables / lock, selectable elevator, Jens Axboe <axboe@suse.de>
6 * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au> - July2000
7 * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
8 */
9
10/*
11 * This handles all read/write requests to block devices
12 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/backing-dev.h>
16#include <linux/bio.h>
17#include <linux/blkdev.h>
18#include <linux/highmem.h>
19#include <linux/mm.h>
20#include <linux/kernel_stat.h>
21#include <linux/string.h>
22#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/completion.h>
24#include <linux/slab.h>
25#include <linux/swap.h>
26#include <linux/writeback.h>
Andrew Mortonfaccbd4b2006-12-10 02:19:35 -080027#include <linux/task_io_accounting_ops.h>
Jens Axboeff856ba2006-01-09 16:02:34 +010028#include <linux/interrupt.h>
29#include <linux/cpu.h>
Jens Axboe2056a782006-03-23 20:00:26 +010030#include <linux/blktrace_api.h>
Akinobu Mitac17bb492006-12-08 02:39:46 -080031#include <linux/fault-inject.h>
Jens Axboef5659132007-09-21 10:44:19 +020032#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Jens Axboe8324aa92008-01-29 14:51:59 +010034#include "blk.h"
35
Jerome Marchandb238b3d2007-10-23 15:05:46 +020036static void drive_stat_acct(struct request *rq, int new_io);
Jens Axboe165125e2007-07-24 09:28:11 +020037static int __make_request(struct request_queue *q, struct bio *bio);
NeilBrown9dfa5282007-08-16 13:27:52 +020038static void blk_recalc_rq_segments(struct request *rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40/*
41 * For the allocated request tables
42 */
Jens Axboe8324aa92008-01-29 14:51:59 +010043struct kmem_cache *request_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45/*
46 * For queue allocation
47 */
Jens Axboe8324aa92008-01-29 14:51:59 +010048struct kmem_cache *blk_requestq_cachep = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 * Controlling structure to kblockd
52 */
Jens Axboeff856ba2006-01-09 16:02:34 +010053static struct workqueue_struct *kblockd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Jens Axboeff856ba2006-01-09 16:02:34 +010055static DEFINE_PER_CPU(struct list_head, blk_cpu_done);
56
Jens Axboe8324aa92008-01-29 14:51:59 +010057void blk_queue_congestion_threshold(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 int nr;
60
61 nr = q->nr_requests - (q->nr_requests / 8) + 1;
62 if (nr > q->nr_requests)
63 nr = q->nr_requests;
64 q->nr_congestion_on = nr;
65
66 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
67 if (nr < 1)
68 nr = 1;
69 q->nr_congestion_off = nr;
70}
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/**
73 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
74 * @bdev: device
75 *
76 * Locates the passed device's request queue and returns the address of its
77 * backing_dev_info
78 *
79 * Will return NULL if the request queue cannot be located.
80 */
81struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
82{
83 struct backing_dev_info *ret = NULL;
Jens Axboe165125e2007-07-24 09:28:11 +020084 struct request_queue *q = bdev_get_queue(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 if (q)
87 ret = &q->backing_dev_info;
88 return ret;
89}
Linus Torvalds1da177e2005-04-16 15:20:36 -070090EXPORT_SYMBOL(blk_get_backing_dev_info);
91
Jens Axboe86db1e22008-01-29 14:53:40 +010092void rq_init(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
94 INIT_LIST_HEAD(&rq->queuelist);
Jens Axboeff856ba2006-01-09 16:02:34 +010095 INIT_LIST_HEAD(&rq->donelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 rq->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 rq->bio = rq->biotail = NULL;
Jens Axboe2e662b62006-07-13 11:55:04 +020099 INIT_HLIST_NODE(&rq->hash);
100 RB_CLEAR_NODE(&rq->rb_node);
Jens Axboe22e2c502005-06-27 10:55:12 +0200101 rq->ioprio = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 rq->buffer = NULL;
103 rq->ref_count = 1;
104 rq->q = q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 rq->special = NULL;
106 rq->data_len = 0;
107 rq->data = NULL;
Mike Christie df46b9a2005-06-20 14:04:44 +0200108 rq->nr_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 rq->sense = NULL;
110 rq->end_io = NULL;
111 rq->end_io_data = NULL;
Jens Axboeff856ba2006-01-09 16:02:34 +0100112 rq->completion_data = NULL;
FUJITA Tomonoriabae1fd2007-07-16 08:52:14 +0200113 rq->next_rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
NeilBrown5bb23a62007-09-27 12:46:13 +0200116static void req_bio_endio(struct request *rq, struct bio *bio,
117 unsigned int nbytes, int error)
Tejun Heo797e7db2006-01-06 09:51:03 +0100118{
Jens Axboe165125e2007-07-24 09:28:11 +0200119 struct request_queue *q = rq->q;
Tejun Heo797e7db2006-01-06 09:51:03 +0100120
NeilBrown5bb23a62007-09-27 12:46:13 +0200121 if (&q->bar_rq != rq) {
122 if (error)
123 clear_bit(BIO_UPTODATE, &bio->bi_flags);
124 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
125 error = -EIO;
Tejun Heo797e7db2006-01-06 09:51:03 +0100126
NeilBrown5bb23a62007-09-27 12:46:13 +0200127 if (unlikely(nbytes > bio->bi_size)) {
128 printk("%s: want %u bytes done, only %u left\n",
129 __FUNCTION__, nbytes, bio->bi_size);
130 nbytes = bio->bi_size;
131 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100132
NeilBrown5bb23a62007-09-27 12:46:13 +0200133 bio->bi_size -= nbytes;
134 bio->bi_sector += (nbytes >> 9);
135 if (bio->bi_size == 0)
NeilBrown6712ecf2007-09-27 12:47:43 +0200136 bio_endio(bio, error);
NeilBrown5bb23a62007-09-27 12:46:13 +0200137 } else {
138
139 /*
140 * Okay, this is the barrier request in progress, just
141 * record the error;
142 */
143 if (error && !q->orderr)
144 q->orderr = error;
145 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100146}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148void blk_dump_rq_flags(struct request *rq, char *msg)
149{
150 int bit;
151
Jens Axboe4aff5e22006-08-10 08:44:47 +0200152 printk("%s: dev %s: type=%x, flags=%x\n", msg,
153 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
154 rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 printk("\nsector %llu, nr/cnr %lu/%u\n", (unsigned long long)rq->sector,
157 rq->nr_sectors,
158 rq->current_nr_sectors);
159 printk("bio %p, biotail %p, buffer %p, data %p, len %u\n", rq->bio, rq->biotail, rq->buffer, rq->data, rq->data_len);
160
Jens Axboe4aff5e22006-08-10 08:44:47 +0200161 if (blk_pc_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 printk("cdb: ");
163 for (bit = 0; bit < sizeof(rq->cmd); bit++)
164 printk("%02x ", rq->cmd[bit]);
165 printk("\n");
166 }
167}
168
169EXPORT_SYMBOL(blk_dump_rq_flags);
170
Jens Axboe165125e2007-07-24 09:28:11 +0200171void blk_recount_segments(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
NeilBrown9dfa5282007-08-16 13:27:52 +0200173 struct request rq;
174 struct bio *nxt = bio->bi_next;
175 rq.q = q;
176 rq.bio = rq.biotail = bio;
177 bio->bi_next = NULL;
178 blk_recalc_rq_segments(&rq);
179 bio->bi_next = nxt;
180 bio->bi_phys_segments = rq.nr_phys_segments;
181 bio->bi_hw_segments = rq.nr_hw_segments;
182 bio->bi_flags |= (1 << BIO_SEG_VALID);
183}
184EXPORT_SYMBOL(blk_recount_segments);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
NeilBrown9dfa5282007-08-16 13:27:52 +0200186static void blk_recalc_rq_segments(struct request *rq)
187{
188 int nr_phys_segs;
189 int nr_hw_segs;
190 unsigned int phys_size;
191 unsigned int hw_size;
192 struct bio_vec *bv, *bvprv = NULL;
193 int seg_size;
194 int hw_seg_size;
195 int cluster;
NeilBrown5705f702007-09-25 12:35:59 +0200196 struct req_iterator iter;
NeilBrown9dfa5282007-08-16 13:27:52 +0200197 int high, highprv = 1;
198 struct request_queue *q = rq->q;
199
200 if (!rq->bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return;
202
203 cluster = q->queue_flags & (1 << QUEUE_FLAG_CLUSTER);
NeilBrown9dfa5282007-08-16 13:27:52 +0200204 hw_seg_size = seg_size = 0;
205 phys_size = hw_size = nr_phys_segs = nr_hw_segs = 0;
NeilBrown5705f702007-09-25 12:35:59 +0200206 rq_for_each_segment(bv, rq, iter) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 /*
208 * the trick here is making sure that a high page is never
209 * considered part of another segment, since that might
210 * change with the bounce page.
211 */
Vasily Tarasovf772b3d2007-03-27 08:52:47 +0200212 high = page_to_pfn(bv->bv_page) > q->bounce_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (high || highprv)
214 goto new_hw_segment;
215 if (cluster) {
216 if (seg_size + bv->bv_len > q->max_segment_size)
217 goto new_segment;
218 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bv))
219 goto new_segment;
220 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bv))
221 goto new_segment;
222 if (BIOVEC_VIRT_OVERSIZE(hw_seg_size + bv->bv_len))
223 goto new_hw_segment;
224
225 seg_size += bv->bv_len;
226 hw_seg_size += bv->bv_len;
227 bvprv = bv;
228 continue;
229 }
230new_segment:
231 if (BIOVEC_VIRT_MERGEABLE(bvprv, bv) &&
NeilBrown9dfa5282007-08-16 13:27:52 +0200232 !BIOVEC_VIRT_OVERSIZE(hw_seg_size + bv->bv_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 hw_seg_size += bv->bv_len;
NeilBrown9dfa5282007-08-16 13:27:52 +0200234 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235new_hw_segment:
NeilBrown9dfa5282007-08-16 13:27:52 +0200236 if (nr_hw_segs == 1 &&
237 hw_seg_size > rq->bio->bi_hw_front_size)
238 rq->bio->bi_hw_front_size = hw_seg_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 hw_seg_size = BIOVEC_VIRT_START_SIZE(bv) + bv->bv_len;
240 nr_hw_segs++;
241 }
242
243 nr_phys_segs++;
244 bvprv = bv;
245 seg_size = bv->bv_len;
246 highprv = high;
247 }
NeilBrown9dfa5282007-08-16 13:27:52 +0200248
249 if (nr_hw_segs == 1 &&
250 hw_seg_size > rq->bio->bi_hw_front_size)
251 rq->bio->bi_hw_front_size = hw_seg_size;
252 if (hw_seg_size > rq->biotail->bi_hw_back_size)
253 rq->biotail->bi_hw_back_size = hw_seg_size;
254 rq->nr_phys_segments = nr_phys_segs;
255 rq->nr_hw_segments = nr_hw_segs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Jens Axboe165125e2007-07-24 09:28:11 +0200258static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 struct bio *nxt)
260{
261 if (!(q->queue_flags & (1 << QUEUE_FLAG_CLUSTER)))
262 return 0;
263
264 if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)))
265 return 0;
266 if (bio->bi_size + nxt->bi_size > q->max_segment_size)
267 return 0;
268
269 /*
270 * bio and nxt are contigous in memory, check if the queue allows
271 * these two to be merged into one
272 */
273 if (BIO_SEG_BOUNDARY(q, bio, nxt))
274 return 1;
275
276 return 0;
277}
278
Jens Axboe165125e2007-07-24 09:28:11 +0200279static int blk_hw_contig_segment(struct request_queue *q, struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 struct bio *nxt)
281{
282 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
283 blk_recount_segments(q, bio);
284 if (unlikely(!bio_flagged(nxt, BIO_SEG_VALID)))
285 blk_recount_segments(q, nxt);
286 if (!BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)) ||
Jens Axboe32eef962007-06-19 09:09:27 +0200287 BIOVEC_VIRT_OVERSIZE(bio->bi_hw_back_size + nxt->bi_hw_front_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return 0;
Jens Axboe32eef962007-06-19 09:09:27 +0200289 if (bio->bi_hw_back_size + nxt->bi_hw_front_size > q->max_segment_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return 0;
291
292 return 1;
293}
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295/*
296 * map a request to scatterlist, return number of sg entries setup. Caller
297 * must make sure sg can hold rq->nr_phys_segments entries
298 */
Jens Axboe165125e2007-07-24 09:28:11 +0200299int blk_rq_map_sg(struct request_queue *q, struct request *rq,
Jens Axboef5659132007-09-21 10:44:19 +0200300 struct scatterlist *sglist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 struct bio_vec *bvec, *bvprv;
NeilBrown5705f702007-09-25 12:35:59 +0200303 struct req_iterator iter;
Jens Axboeba951842007-10-17 19:34:11 +0200304 struct scatterlist *sg;
NeilBrown5705f702007-09-25 12:35:59 +0200305 int nsegs, cluster;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 nsegs = 0;
308 cluster = q->queue_flags & (1 << QUEUE_FLAG_CLUSTER);
309
310 /*
311 * for each bio in rq
312 */
313 bvprv = NULL;
Jens Axboeba951842007-10-17 19:34:11 +0200314 sg = NULL;
NeilBrown5705f702007-09-25 12:35:59 +0200315 rq_for_each_segment(bvec, rq, iter) {
Jens Axboe6c92e692007-08-16 13:43:12 +0200316 int nbytes = bvec->bv_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Jens Axboe6c92e692007-08-16 13:43:12 +0200318 if (bvprv && cluster) {
Jens Axboef5659132007-09-21 10:44:19 +0200319 if (sg->length + nbytes > q->max_segment_size)
Jens Axboe6c92e692007-08-16 13:43:12 +0200320 goto new_segment;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Jens Axboe6c92e692007-08-16 13:43:12 +0200322 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
323 goto new_segment;
324 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
325 goto new_segment;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Jens Axboef5659132007-09-21 10:44:19 +0200327 sg->length += nbytes;
Jens Axboe6c92e692007-08-16 13:43:12 +0200328 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329new_segment:
Jens Axboeba951842007-10-17 19:34:11 +0200330 if (!sg)
331 sg = sglist;
Jens Axboe7aeacf92007-10-23 09:49:25 +0200332 else {
333 /*
334 * If the driver previously mapped a shorter
335 * list, we could see a termination bit
336 * prematurely unless it fully inits the sg
337 * table on each mapping. We KNOW that there
338 * must be more entries here or the driver
339 * would be buggy, so force clear the
340 * termination bit to avoid doing a full
341 * sg_init_table() in drivers for each command.
342 */
343 sg->page_link &= ~0x02;
Jens Axboeba951842007-10-17 19:34:11 +0200344 sg = sg_next(sg);
Jens Axboe7aeacf92007-10-23 09:49:25 +0200345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Jens Axboe642f1492007-10-24 11:20:47 +0200347 sg_set_page(sg, bvec->bv_page, nbytes, bvec->bv_offset);
Jens Axboe6c92e692007-08-16 13:43:12 +0200348 nsegs++;
349 }
350 bvprv = bvec;
NeilBrown5705f702007-09-25 12:35:59 +0200351 } /* segments in rq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
James Bottomleyfa0ccd82008-01-10 11:30:36 -0600353 if (q->dma_drain_size) {
354 sg->page_link &= ~0x02;
355 sg = sg_next(sg);
356 sg_set_page(sg, virt_to_page(q->dma_drain_buffer),
357 q->dma_drain_size,
358 ((unsigned long)q->dma_drain_buffer) &
359 (PAGE_SIZE - 1));
360 nsegs++;
361 }
362
Jens Axboe9b617642007-10-22 19:39:33 +0200363 if (sg)
Jens Axboec46f2332007-10-31 12:06:37 +0100364 sg_mark_end(sg);
Jens Axboe9b617642007-10-22 19:39:33 +0200365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return nsegs;
367}
368
369EXPORT_SYMBOL(blk_rq_map_sg);
370
371/*
372 * the standard queue merge functions, can be overridden with device
373 * specific ones if so desired
374 */
375
Jens Axboe165125e2007-07-24 09:28:11 +0200376static inline int ll_new_mergeable(struct request_queue *q,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 struct request *req,
378 struct bio *bio)
379{
380 int nr_phys_segs = bio_phys_segments(q, bio);
381
382 if (req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
Jens Axboe4aff5e22006-08-10 08:44:47 +0200383 req->cmd_flags |= REQ_NOMERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (req == q->last_merge)
385 q->last_merge = NULL;
386 return 0;
387 }
388
389 /*
390 * A hw segment is just getting larger, bump just the phys
391 * counter.
392 */
393 req->nr_phys_segments += nr_phys_segs;
394 return 1;
395}
396
Jens Axboe165125e2007-07-24 09:28:11 +0200397static inline int ll_new_hw_segment(struct request_queue *q,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 struct request *req,
399 struct bio *bio)
400{
401 int nr_hw_segs = bio_hw_segments(q, bio);
402 int nr_phys_segs = bio_phys_segments(q, bio);
403
404 if (req->nr_hw_segments + nr_hw_segs > q->max_hw_segments
405 || req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
Jens Axboe4aff5e22006-08-10 08:44:47 +0200406 req->cmd_flags |= REQ_NOMERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (req == q->last_merge)
408 q->last_merge = NULL;
409 return 0;
410 }
411
412 /*
413 * This will form the start of a new hw segment. Bump both
414 * counters.
415 */
416 req->nr_hw_segments += nr_hw_segs;
417 req->nr_phys_segments += nr_phys_segs;
418 return 1;
419}
420
Jens Axboe86db1e22008-01-29 14:53:40 +0100421int ll_back_merge_fn(struct request_queue *q, struct request *req,
422 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Mike Christiedefd94b2005-12-05 02:37:06 -0600424 unsigned short max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 int len;
426
Mike Christiedefd94b2005-12-05 02:37:06 -0600427 if (unlikely(blk_pc_request(req)))
428 max_sectors = q->max_hw_sectors;
429 else
430 max_sectors = q->max_sectors;
431
432 if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
Jens Axboe4aff5e22006-08-10 08:44:47 +0200433 req->cmd_flags |= REQ_NOMERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (req == q->last_merge)
435 q->last_merge = NULL;
436 return 0;
437 }
438 if (unlikely(!bio_flagged(req->biotail, BIO_SEG_VALID)))
439 blk_recount_segments(q, req->biotail);
440 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
441 blk_recount_segments(q, bio);
442 len = req->biotail->bi_hw_back_size + bio->bi_hw_front_size;
443 if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(req->biotail), __BVEC_START(bio)) &&
444 !BIOVEC_VIRT_OVERSIZE(len)) {
445 int mergeable = ll_new_mergeable(q, req, bio);
446
447 if (mergeable) {
448 if (req->nr_hw_segments == 1)
449 req->bio->bi_hw_front_size = len;
450 if (bio->bi_hw_segments == 1)
451 bio->bi_hw_back_size = len;
452 }
453 return mergeable;
454 }
455
456 return ll_new_hw_segment(q, req, bio);
457}
458
Jens Axboe165125e2007-07-24 09:28:11 +0200459static int ll_front_merge_fn(struct request_queue *q, struct request *req,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 struct bio *bio)
461{
Mike Christiedefd94b2005-12-05 02:37:06 -0600462 unsigned short max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 int len;
464
Mike Christiedefd94b2005-12-05 02:37:06 -0600465 if (unlikely(blk_pc_request(req)))
466 max_sectors = q->max_hw_sectors;
467 else
468 max_sectors = q->max_sectors;
469
470
471 if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
Jens Axboe4aff5e22006-08-10 08:44:47 +0200472 req->cmd_flags |= REQ_NOMERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (req == q->last_merge)
474 q->last_merge = NULL;
475 return 0;
476 }
477 len = bio->bi_hw_back_size + req->bio->bi_hw_front_size;
478 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
479 blk_recount_segments(q, bio);
480 if (unlikely(!bio_flagged(req->bio, BIO_SEG_VALID)))
481 blk_recount_segments(q, req->bio);
482 if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(req->bio)) &&
483 !BIOVEC_VIRT_OVERSIZE(len)) {
484 int mergeable = ll_new_mergeable(q, req, bio);
485
486 if (mergeable) {
487 if (bio->bi_hw_segments == 1)
488 bio->bi_hw_front_size = len;
489 if (req->nr_hw_segments == 1)
490 req->biotail->bi_hw_back_size = len;
491 }
492 return mergeable;
493 }
494
495 return ll_new_hw_segment(q, req, bio);
496}
497
Jens Axboe165125e2007-07-24 09:28:11 +0200498static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 struct request *next)
500{
Nikita Danilovdfa1a552005-06-25 14:59:20 -0700501 int total_phys_segments;
502 int total_hw_segments;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 /*
505 * First check if the either of the requests are re-queued
506 * requests. Can't merge them if they are.
507 */
508 if (req->special || next->special)
509 return 0;
510
511 /*
Nikita Danilovdfa1a552005-06-25 14:59:20 -0700512 * Will it become too large?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 */
514 if ((req->nr_sectors + next->nr_sectors) > q->max_sectors)
515 return 0;
516
517 total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
518 if (blk_phys_contig_segment(q, req->biotail, next->bio))
519 total_phys_segments--;
520
521 if (total_phys_segments > q->max_phys_segments)
522 return 0;
523
524 total_hw_segments = req->nr_hw_segments + next->nr_hw_segments;
525 if (blk_hw_contig_segment(q, req->biotail, next->bio)) {
526 int len = req->biotail->bi_hw_back_size + next->bio->bi_hw_front_size;
527 /*
528 * propagate the combined length to the end of the requests
529 */
530 if (req->nr_hw_segments == 1)
531 req->bio->bi_hw_front_size = len;
532 if (next->nr_hw_segments == 1)
533 next->biotail->bi_hw_back_size = len;
534 total_hw_segments--;
535 }
536
537 if (total_hw_segments > q->max_hw_segments)
538 return 0;
539
540 /* Merge is OK... */
541 req->nr_phys_segments = total_phys_segments;
542 req->nr_hw_segments = total_hw_segments;
543 return 1;
544}
545
546/*
547 * "plug" the device if there are no outstanding requests: this will
548 * force the transfer to start only after we have put all the requests
549 * on the list.
550 *
551 * This is called with interrupts off and no requests on the queue and
552 * with the queue lock held.
553 */
Jens Axboe165125e2007-07-24 09:28:11 +0200554void blk_plug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
556 WARN_ON(!irqs_disabled());
557
558 /*
559 * don't plug a stopped queue, it must be paired with blk_start_queue()
560 * which will restart the queueing
561 */
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +0200562 if (blk_queue_stopped(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return;
564
Jens Axboe2056a782006-03-23 20:00:26 +0100565 if (!test_and_set_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
Jens Axboe2056a782006-03-23 20:00:26 +0100567 blk_add_trace_generic(q, NULL, 0, BLK_TA_PLUG);
568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
571EXPORT_SYMBOL(blk_plug_device);
572
573/*
574 * remove the queue from the plugged list, if present. called with
575 * queue lock held and interrupts disabled.
576 */
Jens Axboe165125e2007-07-24 09:28:11 +0200577int blk_remove_plug(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
579 WARN_ON(!irqs_disabled());
580
581 if (!test_and_clear_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags))
582 return 0;
583
584 del_timer(&q->unplug_timer);
585 return 1;
586}
587
588EXPORT_SYMBOL(blk_remove_plug);
589
590/*
591 * remove the plug and let it rip..
592 */
Jens Axboe165125e2007-07-24 09:28:11 +0200593void __generic_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +0200595 if (unlikely(blk_queue_stopped(q)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return;
597
598 if (!blk_remove_plug(q))
599 return;
600
Jens Axboe22e2c502005-06-27 10:55:12 +0200601 q->request_fn(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603EXPORT_SYMBOL(__generic_unplug_device);
604
605/**
606 * generic_unplug_device - fire a request queue
Jens Axboe165125e2007-07-24 09:28:11 +0200607 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 *
609 * Description:
610 * Linux uses plugging to build bigger requests queues before letting
611 * the device have at them. If a queue is plugged, the I/O scheduler
612 * is still adding and merging requests on the queue. Once the queue
613 * gets unplugged, the request_fn defined for the queue is invoked and
614 * transfers started.
615 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200616void generic_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
618 spin_lock_irq(q->queue_lock);
619 __generic_unplug_device(q);
620 spin_unlock_irq(q->queue_lock);
621}
622EXPORT_SYMBOL(generic_unplug_device);
623
624static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
625 struct page *page)
626{
Jens Axboe165125e2007-07-24 09:28:11 +0200627 struct request_queue *q = bdi->unplug_io_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500629 blk_unplug(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
Jens Axboe86db1e22008-01-29 14:53:40 +0100632void blk_unplug_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Jens Axboe165125e2007-07-24 09:28:11 +0200634 struct request_queue *q =
635 container_of(work, struct request_queue, unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Jens Axboe2056a782006-03-23 20:00:26 +0100637 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
638 q->rq.count[READ] + q->rq.count[WRITE]);
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 q->unplug_fn(q);
641}
642
Jens Axboe86db1e22008-01-29 14:53:40 +0100643void blk_unplug_timeout(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Jens Axboe165125e2007-07-24 09:28:11 +0200645 struct request_queue *q = (struct request_queue *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Jens Axboe2056a782006-03-23 20:00:26 +0100647 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_TIMER, NULL,
648 q->rq.count[READ] + q->rq.count[WRITE]);
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 kblockd_schedule_work(&q->unplug_work);
651}
652
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -0500653void blk_unplug(struct request_queue *q)
654{
655 /*
656 * devices don't necessarily have an ->unplug_fn defined
657 */
658 if (q->unplug_fn) {
659 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
660 q->rq.count[READ] + q->rq.count[WRITE]);
661
662 q->unplug_fn(q);
663 }
664}
665EXPORT_SYMBOL(blk_unplug);
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667/**
668 * blk_start_queue - restart a previously stopped queue
Jens Axboe165125e2007-07-24 09:28:11 +0200669 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 *
671 * Description:
672 * blk_start_queue() will clear the stop flag on the queue, and call
673 * the request_fn for the queue if it was in a stopped state when
674 * entered. Also see blk_stop_queue(). Queue lock must be held.
675 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200676void blk_start_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200678 WARN_ON(!irqs_disabled());
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 clear_bit(QUEUE_FLAG_STOPPED, &q->queue_flags);
681
682 /*
683 * one level of recursion is ok and is much faster than kicking
684 * the unplug handling
685 */
686 if (!test_and_set_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) {
687 q->request_fn(q);
688 clear_bit(QUEUE_FLAG_REENTER, &q->queue_flags);
689 } else {
690 blk_plug_device(q);
691 kblockd_schedule_work(&q->unplug_work);
692 }
693}
694
695EXPORT_SYMBOL(blk_start_queue);
696
697/**
698 * blk_stop_queue - stop a queue
Jens Axboe165125e2007-07-24 09:28:11 +0200699 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 *
701 * Description:
702 * The Linux block layer assumes that a block driver will consume all
703 * entries on the request queue when the request_fn strategy is called.
704 * Often this will not happen, because of hardware limitations (queue
705 * depth settings). If a device driver gets a 'queue full' response,
706 * or if it simply chooses not to queue more I/O at one point, it can
707 * call this function to prevent the request_fn from being called until
708 * the driver has signalled it's ready to go again. This happens by calling
709 * blk_start_queue() to restart queue operations. Queue lock must be held.
710 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200711void blk_stop_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 blk_remove_plug(q);
714 set_bit(QUEUE_FLAG_STOPPED, &q->queue_flags);
715}
716EXPORT_SYMBOL(blk_stop_queue);
717
718/**
719 * blk_sync_queue - cancel any pending callbacks on a queue
720 * @q: the queue
721 *
722 * Description:
723 * The block layer may perform asynchronous callback activity
724 * on a queue, such as calling the unplug function after a timeout.
725 * A block device may call blk_sync_queue to ensure that any
726 * such activity is cancelled, thus allowing it to release resources
Michael Opdenacker59c51592007-05-09 08:57:56 +0200727 * that the callbacks might use. The caller must already have made sure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 * that its ->make_request_fn will not re-add plugging prior to calling
729 * this function.
730 *
731 */
732void blk_sync_queue(struct request_queue *q)
733{
734 del_timer_sync(&q->unplug_timer);
Oleg Nesterovabbeb882007-10-23 15:08:19 +0200735 kblockd_flush_work(&q->unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737EXPORT_SYMBOL(blk_sync_queue);
738
739/**
740 * blk_run_queue - run a single device queue
741 * @q: The queue to run
742 */
743void blk_run_queue(struct request_queue *q)
744{
745 unsigned long flags;
746
747 spin_lock_irqsave(q->queue_lock, flags);
748 blk_remove_plug(q);
Jens Axboedac07ec2006-05-11 08:20:16 +0200749
750 /*
751 * Only recurse once to avoid overrunning the stack, let the unplug
752 * handling reinvoke the handler shortly if we already got there.
753 */
754 if (!elv_queue_empty(q)) {
755 if (!test_and_set_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) {
756 q->request_fn(q);
757 clear_bit(QUEUE_FLAG_REENTER, &q->queue_flags);
758 } else {
759 blk_plug_device(q);
760 kblockd_schedule_work(&q->unplug_work);
761 }
762 }
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 spin_unlock_irqrestore(q->queue_lock, flags);
765}
766EXPORT_SYMBOL(blk_run_queue);
767
Jens Axboe165125e2007-07-24 09:28:11 +0200768void blk_put_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -0500769{
770 kobject_put(&q->kobj);
771}
772EXPORT_SYMBOL(blk_put_queue);
773
Jens Axboe165125e2007-07-24 09:28:11 +0200774void blk_cleanup_queue(struct request_queue * q)
Al Viro483f4af2006-03-18 18:34:37 -0500775{
776 mutex_lock(&q->sysfs_lock);
777 set_bit(QUEUE_FLAG_DEAD, &q->queue_flags);
778 mutex_unlock(&q->sysfs_lock);
779
780 if (q->elevator)
781 elevator_exit(q->elevator);
782
783 blk_put_queue(q);
784}
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786EXPORT_SYMBOL(blk_cleanup_queue);
787
Jens Axboe165125e2007-07-24 09:28:11 +0200788static int blk_init_free_list(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
790 struct request_list *rl = &q->rq;
791
792 rl->count[READ] = rl->count[WRITE] = 0;
793 rl->starved[READ] = rl->starved[WRITE] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +0200794 rl->elvpriv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 init_waitqueue_head(&rl->wait[READ]);
796 init_waitqueue_head(&rl->wait[WRITE]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Christoph Lameter19460892005-06-23 00:08:19 -0700798 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
799 mempool_free_slab, request_cachep, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 if (!rl->rq_pool)
802 return -ENOMEM;
803
804 return 0;
805}
806
Jens Axboe165125e2007-07-24 09:28:11 +0200807struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Christoph Lameter19460892005-06-23 00:08:19 -0700809 return blk_alloc_queue_node(gfp_mask, -1);
810}
811EXPORT_SYMBOL(blk_alloc_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Jens Axboe165125e2007-07-24 09:28:11 +0200813struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -0700814{
Jens Axboe165125e2007-07-24 09:28:11 +0200815 struct request_queue *q;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700816 int err;
Christoph Lameter19460892005-06-23 00:08:19 -0700817
Jens Axboe8324aa92008-01-29 14:51:59 +0100818 q = kmem_cache_alloc_node(blk_requestq_cachep,
Christoph Lameter94f60302007-07-17 04:03:29 -0700819 gfp_mask | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (!q)
821 return NULL;
822
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700823 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
824 q->backing_dev_info.unplug_io_data = q;
825 err = bdi_init(&q->backing_dev_info);
826 if (err) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100827 kmem_cache_free(blk_requestq_cachep, q);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700828 return NULL;
829 }
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 init_timer(&q->unplug_timer);
Al Viro483f4af2006-03-18 18:34:37 -0500832
Jens Axboe8324aa92008-01-29 14:51:59 +0100833 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Al Viro483f4af2006-03-18 18:34:37 -0500835 mutex_init(&q->sysfs_lock);
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return q;
838}
Christoph Lameter19460892005-06-23 00:08:19 -0700839EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841/**
842 * blk_init_queue - prepare a request queue for use with a block device
843 * @rfn: The function to be called to process requests that have been
844 * placed on the queue.
845 * @lock: Request queue spin lock
846 *
847 * Description:
848 * If a block device wishes to use the standard request handling procedures,
849 * which sorts requests and coalesces adjacent requests, then it must
850 * call blk_init_queue(). The function @rfn will be called when there
851 * are requests on the queue that need to be processed. If the device
852 * supports plugging, then @rfn may not be called immediately when requests
853 * are available on the queue, but may be called at some time later instead.
854 * Plugged queues are generally unplugged when a buffer belonging to one
855 * of the requests on the queue is needed, or due to memory pressure.
856 *
857 * @rfn is not required, or even expected, to remove all requests off the
858 * queue, but only as many as it can handle at a time. If it does leave
859 * requests on the queue, it is responsible for arranging that the requests
860 * get dealt with eventually.
861 *
862 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +0200863 * request queue; this lock will be taken also from interrupt context, so irq
864 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 *
866 * Function returns a pointer to the initialized request queue, or NULL if
867 * it didn't succeed.
868 *
869 * Note:
870 * blk_init_queue() must be paired with a blk_cleanup_queue() call
871 * when the block device is deactivated (such as at module unload).
872 **/
Christoph Lameter19460892005-06-23 00:08:19 -0700873
Jens Axboe165125e2007-07-24 09:28:11 +0200874struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
Christoph Lameter19460892005-06-23 00:08:19 -0700876 return blk_init_queue_node(rfn, lock, -1);
877}
878EXPORT_SYMBOL(blk_init_queue);
879
Jens Axboe165125e2007-07-24 09:28:11 +0200880struct request_queue *
Christoph Lameter19460892005-06-23 00:08:19 -0700881blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
882{
Jens Axboe165125e2007-07-24 09:28:11 +0200883 struct request_queue *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 if (!q)
886 return NULL;
887
Christoph Lameter19460892005-06-23 00:08:19 -0700888 q->node = node_id;
Al Viro8669aaf2006-03-18 13:50:00 -0500889 if (blk_init_free_list(q)) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100890 kmem_cache_free(blk_requestq_cachep, q);
Al Viro8669aaf2006-03-18 13:50:00 -0500891 return NULL;
892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
152587d2005-04-12 16:22:06 -0500894 /*
895 * if caller didn't supply a lock, they get per-queue locking with
896 * our embedded lock
897 */
898 if (!lock) {
899 spin_lock_init(&q->__queue_lock);
900 lock = &q->__queue_lock;
901 }
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 q->request_fn = rfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 q->prep_rq_fn = NULL;
905 q->unplug_fn = generic_unplug_device;
906 q->queue_flags = (1 << QUEUE_FLAG_CLUSTER);
907 q->queue_lock = lock;
908
909 blk_queue_segment_boundary(q, 0xffffffff);
910
911 blk_queue_make_request(q, __make_request);
912 blk_queue_max_segment_size(q, MAX_SEGMENT_SIZE);
913
914 blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS);
915 blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS);
916
Alan Stern44ec9542007-02-20 11:01:57 -0500917 q->sg_reserved_size = INT_MAX;
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 /*
920 * all done
921 */
922 if (!elevator_init(q, NULL)) {
923 blk_queue_congestion_threshold(q);
924 return q;
925 }
926
Al Viro8669aaf2006-03-18 13:50:00 -0500927 blk_put_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return NULL;
929}
Christoph Lameter19460892005-06-23 00:08:19 -0700930EXPORT_SYMBOL(blk_init_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Jens Axboe165125e2007-07-24 09:28:11 +0200932int blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Nick Pigginfde6ad22005-06-23 00:08:53 -0700934 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
Al Viro483f4af2006-03-18 18:34:37 -0500935 kobject_get(&q->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return 0;
937 }
938
939 return 1;
940}
941
942EXPORT_SYMBOL(blk_get_queue);
943
Jens Axboe165125e2007-07-24 09:28:11 +0200944static inline void blk_free_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Jens Axboe4aff5e22006-08-10 08:44:47 +0200946 if (rq->cmd_flags & REQ_ELVPRIV)
Tejun Heocb98fc82005-10-28 08:29:39 +0200947 elv_put_request(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 mempool_free(rq, q->rq.rq_pool);
949}
950
Jens Axboe1ea25ecb2006-07-18 22:24:11 +0200951static struct request *
Jens Axboe165125e2007-07-24 09:28:11 +0200952blk_alloc_request(struct request_queue *q, int rw, int priv, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
954 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
955
956 if (!rq)
957 return NULL;
958
959 /*
Jens Axboe4aff5e22006-08-10 08:44:47 +0200960 * first three bits are identical in rq->cmd_flags and bio->bi_rw,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 * see bio.h and blkdev.h
962 */
Jens Axboe49171e52006-08-10 08:59:11 +0200963 rq->cmd_flags = rw | REQ_ALLOCED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Tejun Heocb98fc82005-10-28 08:29:39 +0200965 if (priv) {
Jens Axboecb78b282006-07-28 09:32:57 +0200966 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
Tejun Heocb98fc82005-10-28 08:29:39 +0200967 mempool_free(rq, q->rq.rq_pool);
968 return NULL;
969 }
Jens Axboe4aff5e22006-08-10 08:44:47 +0200970 rq->cmd_flags |= REQ_ELVPRIV;
Tejun Heocb98fc82005-10-28 08:29:39 +0200971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Tejun Heocb98fc82005-10-28 08:29:39 +0200973 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
975
976/*
977 * ioc_batching returns true if the ioc is a valid batching request and
978 * should be given priority access to a request.
979 */
Jens Axboe165125e2007-07-24 09:28:11 +0200980static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
982 if (!ioc)
983 return 0;
984
985 /*
986 * Make sure the process is able to allocate at least 1 request
987 * even if the batch times out, otherwise we could theoretically
988 * lose wakeups.
989 */
990 return ioc->nr_batch_requests == q->nr_batching ||
991 (ioc->nr_batch_requests > 0
992 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
993}
994
995/*
996 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
997 * will cause the process to be a "batcher" on all queues in the system. This
998 * is the behaviour we want though - once it gets a wakeup it should be given
999 * a nice run.
1000 */
Jens Axboe165125e2007-07-24 09:28:11 +02001001static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
1003 if (!ioc || ioc_batching(q, ioc))
1004 return;
1005
1006 ioc->nr_batch_requests = q->nr_batching;
1007 ioc->last_waited = jiffies;
1008}
1009
Jens Axboe165125e2007-07-24 09:28:11 +02001010static void __freed_request(struct request_queue *q, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
1012 struct request_list *rl = &q->rq;
1013
1014 if (rl->count[rw] < queue_congestion_off_threshold(q))
Thomas Maier79e2de42006-10-19 23:28:15 -07001015 blk_clear_queue_congested(q, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 if (rl->count[rw] + 1 <= q->nr_requests) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (waitqueue_active(&rl->wait[rw]))
1019 wake_up(&rl->wait[rw]);
1020
1021 blk_clear_queue_full(q, rw);
1022 }
1023}
1024
1025/*
1026 * A request has just been released. Account for it, update the full and
1027 * congestion status, wake up any waiters. Called under q->queue_lock.
1028 */
Jens Axboe165125e2007-07-24 09:28:11 +02001029static void freed_request(struct request_queue *q, int rw, int priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
1031 struct request_list *rl = &q->rq;
1032
1033 rl->count[rw]--;
Tejun Heocb98fc82005-10-28 08:29:39 +02001034 if (priv)
1035 rl->elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 __freed_request(q, rw);
1038
1039 if (unlikely(rl->starved[rw ^ 1]))
1040 __freed_request(q, rw ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
1043#define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist)
1044/*
Nick Piggind6344532005-06-28 20:45:14 -07001045 * Get a free request, queue_lock must be held.
1046 * Returns NULL on failure, with queue_lock held.
1047 * Returns !NULL on success, with queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 */
Jens Axboe165125e2007-07-24 09:28:11 +02001049static struct request *get_request(struct request_queue *q, int rw_flags,
Jens Axboe7749a8d2006-12-13 13:02:26 +01001050 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
1052 struct request *rq = NULL;
1053 struct request_list *rl = &q->rq;
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001054 struct io_context *ioc = NULL;
Jens Axboe7749a8d2006-12-13 13:02:26 +01001055 const int rw = rw_flags & 0x01;
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001056 int may_queue, priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Jens Axboe7749a8d2006-12-13 13:02:26 +01001058 may_queue = elv_may_queue(q, rw_flags);
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001059 if (may_queue == ELV_MQUEUE_NO)
1060 goto rq_starved;
1061
1062 if (rl->count[rw]+1 >= queue_congestion_on_threshold(q)) {
1063 if (rl->count[rw]+1 >= q->nr_requests) {
Jens Axboeb5deef92006-07-19 23:39:40 +02001064 ioc = current_io_context(GFP_ATOMIC, q->node);
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001065 /*
1066 * The queue will fill after this allocation, so set
1067 * it as full, and mark this process as "batching".
1068 * This process will be allowed to complete a batch of
1069 * requests, others will be blocked.
1070 */
1071 if (!blk_queue_full(q, rw)) {
1072 ioc_set_batching(q, ioc);
1073 blk_set_queue_full(q, rw);
1074 } else {
1075 if (may_queue != ELV_MQUEUE_MUST
1076 && !ioc_batching(q, ioc)) {
1077 /*
1078 * The queue is full and the allocating
1079 * process is not a "batcher", and not
1080 * exempted by the IO scheduler
1081 */
1082 goto out;
1083 }
1084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
Thomas Maier79e2de42006-10-19 23:28:15 -07001086 blk_set_queue_congested(q, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
1088
Jens Axboe082cf692005-06-28 16:35:11 +02001089 /*
1090 * Only allow batching queuers to allocate up to 50% over the defined
1091 * limit of requests, otherwise we could have thousands of requests
1092 * allocated with any setting of ->nr_requests
1093 */
Hugh Dickinsfd782a42005-06-29 15:15:40 +01001094 if (rl->count[rw] >= (3 * q->nr_requests / 2))
Jens Axboe082cf692005-06-28 16:35:11 +02001095 goto out;
Hugh Dickinsfd782a42005-06-29 15:15:40 +01001096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 rl->count[rw]++;
1098 rl->starved[rw] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +02001099
Jens Axboe64521d12005-10-28 08:30:39 +02001100 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Tejun Heocb98fc82005-10-28 08:29:39 +02001101 if (priv)
1102 rl->elvpriv++;
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 spin_unlock_irq(q->queue_lock);
1105
Jens Axboe7749a8d2006-12-13 13:02:26 +01001106 rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001107 if (unlikely(!rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 /*
1109 * Allocation failed presumably due to memory. Undo anything
1110 * we might have messed up.
1111 *
1112 * Allocating task should really be put onto the front of the
1113 * wait queue, but this is pretty rare.
1114 */
1115 spin_lock_irq(q->queue_lock);
Tejun Heocb98fc82005-10-28 08:29:39 +02001116 freed_request(q, rw, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 /*
1119 * in the very unlikely event that allocation failed and no
1120 * requests for this direction was pending, mark us starved
1121 * so that freeing of a request in the other direction will
1122 * notice us. another possible fix would be to split the
1123 * rq mempool into READ and WRITE
1124 */
1125rq_starved:
1126 if (unlikely(rl->count[rw] == 0))
1127 rl->starved[rw] = 1;
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 goto out;
1130 }
1131
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001132 /*
1133 * ioc may be NULL here, and ioc_batching will be false. That's
1134 * OK, if the queue is under the request limit then requests need
1135 * not count toward the nr_batch_requests limit. There will always
1136 * be some limit enforced by BLK_BATCH_TIME.
1137 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (ioc_batching(q, ioc))
1139 ioc->nr_batch_requests--;
1140
1141 rq_init(q, rq);
Jens Axboe2056a782006-03-23 20:00:26 +01001142
1143 blk_add_trace_generic(q, bio, rw, BLK_TA_GETRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 return rq;
1146}
1147
1148/*
1149 * No available requests for this queue, unplug the device and wait for some
1150 * requests to become available.
Nick Piggind6344532005-06-28 20:45:14 -07001151 *
1152 * Called with q->queue_lock held, and returns with it unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 */
Jens Axboe165125e2007-07-24 09:28:11 +02001154static struct request *get_request_wait(struct request_queue *q, int rw_flags,
Jens Axboe22e2c502005-06-27 10:55:12 +02001155 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
Jens Axboe7749a8d2006-12-13 13:02:26 +01001157 const int rw = rw_flags & 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 struct request *rq;
1159
Jens Axboe7749a8d2006-12-13 13:02:26 +01001160 rq = get_request(q, rw_flags, bio, GFP_NOIO);
Nick Piggin450991b2005-06-28 20:45:13 -07001161 while (!rq) {
1162 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 struct request_list *rl = &q->rq;
1164
1165 prepare_to_wait_exclusive(&rl->wait[rw], &wait,
1166 TASK_UNINTERRUPTIBLE);
1167
Jens Axboe7749a8d2006-12-13 13:02:26 +01001168 rq = get_request(q, rw_flags, bio, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 if (!rq) {
1171 struct io_context *ioc;
1172
Jens Axboe2056a782006-03-23 20:00:26 +01001173 blk_add_trace_generic(q, bio, rw, BLK_TA_SLEEPRQ);
1174
Nick Piggind6344532005-06-28 20:45:14 -07001175 __generic_unplug_device(q);
1176 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 io_schedule();
1178
1179 /*
1180 * After sleeping, we become a "batching" process and
1181 * will be able to allocate at least one request, and
1182 * up to a big batch of them for a small period time.
1183 * See ioc_batching, ioc_set_batching
1184 */
Jens Axboeb5deef92006-07-19 23:39:40 +02001185 ioc = current_io_context(GFP_NOIO, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 ioc_set_batching(q, ioc);
Nick Piggind6344532005-06-28 20:45:14 -07001187
1188 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 }
1190 finish_wait(&rl->wait[rw], &wait);
Nick Piggin450991b2005-06-28 20:45:13 -07001191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 return rq;
1194}
1195
Jens Axboe165125e2007-07-24 09:28:11 +02001196struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
1198 struct request *rq;
1199
1200 BUG_ON(rw != READ && rw != WRITE);
1201
Nick Piggind6344532005-06-28 20:45:14 -07001202 spin_lock_irq(q->queue_lock);
1203 if (gfp_mask & __GFP_WAIT) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001204 rq = get_request_wait(q, rw, NULL);
Nick Piggind6344532005-06-28 20:45:14 -07001205 } else {
Jens Axboe22e2c502005-06-27 10:55:12 +02001206 rq = get_request(q, rw, NULL, gfp_mask);
Nick Piggind6344532005-06-28 20:45:14 -07001207 if (!rq)
1208 spin_unlock_irq(q->queue_lock);
1209 }
1210 /* q->queue_lock is unlocked at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 return rq;
1213}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214EXPORT_SYMBOL(blk_get_request);
1215
1216/**
Jens Axboedc72ef42006-07-20 14:54:05 +02001217 * blk_start_queueing - initiate dispatch of requests to device
1218 * @q: request queue to kick into gear
1219 *
1220 * This is basically a helper to remove the need to know whether a queue
1221 * is plugged or not if someone just wants to initiate dispatch of requests
1222 * for this queue.
1223 *
1224 * The queue lock must be held with interrupts disabled.
1225 */
Jens Axboe165125e2007-07-24 09:28:11 +02001226void blk_start_queueing(struct request_queue *q)
Jens Axboedc72ef42006-07-20 14:54:05 +02001227{
1228 if (!blk_queue_plugged(q))
1229 q->request_fn(q);
1230 else
1231 __generic_unplug_device(q);
1232}
1233EXPORT_SYMBOL(blk_start_queueing);
1234
1235/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 * blk_requeue_request - put a request back on queue
1237 * @q: request queue where request should be inserted
1238 * @rq: request to be inserted
1239 *
1240 * Description:
1241 * Drivers often keep queueing requests until the hardware cannot accept
1242 * more, when that condition happens we need to put the request back
1243 * on the queue. Must be called with queue lock held.
1244 */
Jens Axboe165125e2007-07-24 09:28:11 +02001245void blk_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
Jens Axboe2056a782006-03-23 20:00:26 +01001247 blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (blk_rq_tagged(rq))
1250 blk_queue_end_tag(q, rq);
1251
1252 elv_requeue_request(q, rq);
1253}
1254
1255EXPORT_SYMBOL(blk_requeue_request);
1256
1257/**
1258 * blk_insert_request - insert a special request in to a request queue
1259 * @q: request queue where request should be inserted
1260 * @rq: request to be inserted
1261 * @at_head: insert request at head or tail of queue
1262 * @data: private data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 *
1264 * Description:
1265 * Many block devices need to execute commands asynchronously, so they don't
1266 * block the whole kernel from preemption during request execution. This is
1267 * accomplished normally by inserting aritficial requests tagged as
1268 * REQ_SPECIAL in to the corresponding request queue, and letting them be
1269 * scheduled for actual execution by the request queue.
1270 *
1271 * We have the option of inserting the head or the tail of the queue.
1272 * Typically we use the tail for new ioctls and so forth. We use the head
1273 * of the queue for things like a QUEUE_FULL message from a device, or a
1274 * host that is unable to accept a particular command.
1275 */
Jens Axboe165125e2007-07-24 09:28:11 +02001276void blk_insert_request(struct request_queue *q, struct request *rq,
Tejun Heo 867d1192005-04-24 02:06:05 -05001277 int at_head, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Tejun Heo 867d1192005-04-24 02:06:05 -05001279 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 unsigned long flags;
1281
1282 /*
1283 * tell I/O scheduler that this isn't a regular read/write (ie it
1284 * must not attempt merges on this) and that it acts as a soft
1285 * barrier
1286 */
Jens Axboe4aff5e22006-08-10 08:44:47 +02001287 rq->cmd_type = REQ_TYPE_SPECIAL;
1288 rq->cmd_flags |= REQ_SOFTBARRIER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 rq->special = data;
1291
1292 spin_lock_irqsave(q->queue_lock, flags);
1293
1294 /*
1295 * If command is tagged, release the tag
1296 */
Tejun Heo 867d1192005-04-24 02:06:05 -05001297 if (blk_rq_tagged(rq))
1298 blk_queue_end_tag(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Jerome Marchandb238b3d2007-10-23 15:05:46 +02001300 drive_stat_acct(rq, 1);
Tejun Heo 867d1192005-04-24 02:06:05 -05001301 __elv_add_request(q, rq, where, 0);
Jens Axboedc72ef42006-07-20 14:54:05 +02001302 blk_start_queueing(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 spin_unlock_irqrestore(q->queue_lock, flags);
1304}
1305
1306EXPORT_SYMBOL(blk_insert_request);
1307
Jerome Marchandb238b3d2007-10-23 15:05:46 +02001308static void drive_stat_acct(struct request *rq, int new_io)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
1310 int rw = rq_data_dir(rq);
1311
1312 if (!blk_fs_request(rq) || !rq->rq_disk)
1313 return;
1314
Jens Axboed72d9042005-11-01 08:35:42 +01001315 if (!new_io) {
Jens Axboea3623572005-11-01 09:26:16 +01001316 __disk_stat_inc(rq->rq_disk, merges[rw]);
Jens Axboed72d9042005-11-01 08:35:42 +01001317 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 disk_round_stats(rq->rq_disk);
1319 rq->rq_disk->in_flight++;
1320 }
1321}
1322
1323/*
1324 * add-request adds a request to the linked list.
1325 * queue lock is held and interrupts disabled, as we muck with the
1326 * request queue list.
1327 */
Jens Axboe165125e2007-07-24 09:28:11 +02001328static inline void add_request(struct request_queue * q, struct request * req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
Jerome Marchandb238b3d2007-10-23 15:05:46 +02001330 drive_stat_acct(req, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 /*
1333 * elevator indicated where it wants this request to be
1334 * inserted at elevator_merge time
1335 */
1336 __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
1337}
1338
1339/*
1340 * disk_round_stats() - Round off the performance stats on a struct
1341 * disk_stats.
1342 *
1343 * The average IO queue length and utilisation statistics are maintained
1344 * by observing the current state of the queue length and the amount of
1345 * time it has been in this state for.
1346 *
1347 * Normally, that accounting is done on IO completion, but that can result
1348 * in more than a second's worth of IO being accounted for within any one
1349 * second, leading to >100% utilisation. To deal with that, we call this
1350 * function to do a round-off before returning the results when reading
1351 * /proc/diskstats. This accounts immediately for all queue usage up to
1352 * the current jiffies and restarts the counters again.
1353 */
1354void disk_round_stats(struct gendisk *disk)
1355{
1356 unsigned long now = jiffies;
1357
Chen, Kenneth Wb2982642005-10-13 21:49:29 +02001358 if (now == disk->stamp)
1359 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Chen, Kenneth W20e5c812005-10-13 21:48:42 +02001361 if (disk->in_flight) {
1362 __disk_stat_add(disk, time_in_queue,
1363 disk->in_flight * (now - disk->stamp));
1364 __disk_stat_add(disk, io_ticks, (now - disk->stamp));
1365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 disk->stamp = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367}
1368
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08001369EXPORT_SYMBOL_GPL(disk_round_stats);
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371/*
1372 * queue lock must be held
1373 */
Jens Axboe165125e2007-07-24 09:28:11 +02001374void __blk_put_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 if (unlikely(!q))
1377 return;
1378 if (unlikely(--req->ref_count))
1379 return;
1380
Tejun Heo8922e162005-10-20 16:23:44 +02001381 elv_completed_request(q, req);
1382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 /*
1384 * Request may not have originated from ll_rw_blk. if not,
1385 * it didn't come out of our reserved rq pools
1386 */
Jens Axboe49171e52006-08-10 08:59:11 +02001387 if (req->cmd_flags & REQ_ALLOCED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 int rw = rq_data_dir(req);
Jens Axboe4aff5e22006-08-10 08:44:47 +02001389 int priv = req->cmd_flags & REQ_ELVPRIV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 BUG_ON(!list_empty(&req->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +02001392 BUG_ON(!hlist_unhashed(&req->hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 blk_free_request(q, req);
Tejun Heocb98fc82005-10-28 08:29:39 +02001395 freed_request(q, rw, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
1397}
1398
Mike Christie6e39b692005-11-11 05:30:24 -06001399EXPORT_SYMBOL_GPL(__blk_put_request);
1400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401void blk_put_request(struct request *req)
1402{
Tejun Heo8922e162005-10-20 16:23:44 +02001403 unsigned long flags;
Jens Axboe165125e2007-07-24 09:28:11 +02001404 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Tejun Heo8922e162005-10-20 16:23:44 +02001406 /*
1407 * Gee, IDE calls in w/ NULL q. Fix IDE and remove the
1408 * following if (q) test.
1409 */
1410 if (q) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 spin_lock_irqsave(q->queue_lock, flags);
1412 __blk_put_request(q, req);
1413 spin_unlock_irqrestore(q->queue_lock, flags);
1414 }
1415}
1416
1417EXPORT_SYMBOL(blk_put_request);
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419/*
1420 * Has to be called with the request spinlock acquired
1421 */
Jens Axboe165125e2007-07-24 09:28:11 +02001422static int attempt_merge(struct request_queue *q, struct request *req,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 struct request *next)
1424{
1425 if (!rq_mergeable(req) || !rq_mergeable(next))
1426 return 0;
1427
1428 /*
Andreas Mohrd6e05ed2006-06-26 18:35:02 +02001429 * not contiguous
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 */
1431 if (req->sector + req->nr_sectors != next->sector)
1432 return 0;
1433
1434 if (rq_data_dir(req) != rq_data_dir(next)
1435 || req->rq_disk != next->rq_disk
Jens Axboec00895a2006-09-30 20:29:12 +02001436 || next->special)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 return 0;
1438
1439 /*
1440 * If we are allowed to merge, then append bio list
1441 * from next to rq and release next. merge_requests_fn
1442 * will have updated segment counts, update sector
1443 * counts here.
1444 */
Jens Axboe1aa4f242006-12-19 08:33:11 +01001445 if (!ll_merge_requests_fn(q, req, next))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return 0;
1447
1448 /*
1449 * At this point we have either done a back merge
1450 * or front merge. We need the smaller start_time of
1451 * the merged requests to be the current request
1452 * for accounting purposes.
1453 */
1454 if (time_after(req->start_time, next->start_time))
1455 req->start_time = next->start_time;
1456
1457 req->biotail->bi_next = next->bio;
1458 req->biotail = next->biotail;
1459
1460 req->nr_sectors = req->hard_nr_sectors += next->hard_nr_sectors;
1461
1462 elv_merge_requests(q, req, next);
1463
1464 if (req->rq_disk) {
1465 disk_round_stats(req->rq_disk);
1466 req->rq_disk->in_flight--;
1467 }
1468
Jens Axboe22e2c502005-06-27 10:55:12 +02001469 req->ioprio = ioprio_best(req->ioprio, next->ioprio);
1470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 __blk_put_request(q, next);
1472 return 1;
1473}
1474
Jens Axboe165125e2007-07-24 09:28:11 +02001475static inline int attempt_back_merge(struct request_queue *q,
1476 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
1478 struct request *next = elv_latter_request(q, rq);
1479
1480 if (next)
1481 return attempt_merge(q, rq, next);
1482
1483 return 0;
1484}
1485
Jens Axboe165125e2007-07-24 09:28:11 +02001486static inline int attempt_front_merge(struct request_queue *q,
1487 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
1489 struct request *prev = elv_former_request(q, rq);
1490
1491 if (prev)
1492 return attempt_merge(q, prev, rq);
1493
1494 return 0;
1495}
1496
Jens Axboe86db1e22008-01-29 14:53:40 +01001497void init_request_from_bio(struct request *req, struct bio *bio)
Tejun Heo52d9e672006-01-06 09:49:58 +01001498{
Jens Axboe4aff5e22006-08-10 08:44:47 +02001499 req->cmd_type = REQ_TYPE_FS;
Tejun Heo52d9e672006-01-06 09:49:58 +01001500
1501 /*
1502 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
1503 */
1504 if (bio_rw_ahead(bio) || bio_failfast(bio))
Jens Axboe4aff5e22006-08-10 08:44:47 +02001505 req->cmd_flags |= REQ_FAILFAST;
Tejun Heo52d9e672006-01-06 09:49:58 +01001506
1507 /*
1508 * REQ_BARRIER implies no merging, but lets make it explicit
1509 */
1510 if (unlikely(bio_barrier(bio)))
Jens Axboe4aff5e22006-08-10 08:44:47 +02001511 req->cmd_flags |= (REQ_HARDBARRIER | REQ_NOMERGE);
Tejun Heo52d9e672006-01-06 09:49:58 +01001512
Jens Axboeb31dc662006-06-13 08:26:10 +02001513 if (bio_sync(bio))
Jens Axboe4aff5e22006-08-10 08:44:47 +02001514 req->cmd_flags |= REQ_RW_SYNC;
Jens Axboe5404bc72006-08-10 09:01:02 +02001515 if (bio_rw_meta(bio))
1516 req->cmd_flags |= REQ_RW_META;
Jens Axboeb31dc662006-06-13 08:26:10 +02001517
Tejun Heo52d9e672006-01-06 09:49:58 +01001518 req->errors = 0;
1519 req->hard_sector = req->sector = bio->bi_sector;
Tejun Heo52d9e672006-01-06 09:49:58 +01001520 req->ioprio = bio_prio(bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001521 req->start_time = jiffies;
NeilBrownbc1c56f2007-08-16 13:31:30 +02001522 blk_rq_bio_prep(req->q, req, bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01001523}
1524
Jens Axboe165125e2007-07-24 09:28:11 +02001525static int __make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
Nick Piggin450991b2005-06-28 20:45:13 -07001527 struct request *req;
Jens Axboe51da90f2006-07-18 04:14:45 +02001528 int el_ret, nr_sectors, barrier, err;
1529 const unsigned short prio = bio_prio(bio);
1530 const int sync = bio_sync(bio);
Jens Axboe7749a8d2006-12-13 13:02:26 +01001531 int rw_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 nr_sectors = bio_sectors(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 /*
1536 * low level driver can indicate that it wants pages above a
1537 * certain limit bounced to low memory (ie for highmem, or even
1538 * ISA dma in theory)
1539 */
1540 blk_queue_bounce(q, &bio);
1541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 barrier = bio_barrier(bio);
Tejun Heo797e7db2006-01-06 09:51:03 +01001543 if (unlikely(barrier) && (q->next_ordered == QUEUE_ORDERED_NONE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 err = -EOPNOTSUPP;
1545 goto end_io;
1546 }
1547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 spin_lock_irq(q->queue_lock);
1549
Nick Piggin450991b2005-06-28 20:45:13 -07001550 if (unlikely(barrier) || elv_queue_empty(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 goto get_rq;
1552
1553 el_ret = elv_merge(q, &req, bio);
1554 switch (el_ret) {
1555 case ELEVATOR_BACK_MERGE:
1556 BUG_ON(!rq_mergeable(req));
1557
Jens Axboe1aa4f242006-12-19 08:33:11 +01001558 if (!ll_back_merge_fn(q, req, bio))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 break;
1560
Jens Axboe2056a782006-03-23 20:00:26 +01001561 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
1562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 req->biotail->bi_next = bio;
1564 req->biotail = bio;
1565 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
Jens Axboe22e2c502005-06-27 10:55:12 +02001566 req->ioprio = ioprio_best(req->ioprio, prio);
Jerome Marchandb238b3d2007-10-23 15:05:46 +02001567 drive_stat_acct(req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 if (!attempt_back_merge(q, req))
Jens Axboe2e662b62006-07-13 11:55:04 +02001569 elv_merged_request(q, req, el_ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 goto out;
1571
1572 case ELEVATOR_FRONT_MERGE:
1573 BUG_ON(!rq_mergeable(req));
1574
Jens Axboe1aa4f242006-12-19 08:33:11 +01001575 if (!ll_front_merge_fn(q, req, bio))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 break;
1577
Jens Axboe2056a782006-03-23 20:00:26 +01001578 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
1579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 bio->bi_next = req->bio;
1581 req->bio = bio;
1582
1583 /*
1584 * may not be valid. if the low level driver said
1585 * it didn't need a bounce buffer then it better
1586 * not touch req->buffer either...
1587 */
1588 req->buffer = bio_data(bio);
Jens Axboe51da90f2006-07-18 04:14:45 +02001589 req->current_nr_sectors = bio_cur_sectors(bio);
1590 req->hard_cur_sectors = req->current_nr_sectors;
1591 req->sector = req->hard_sector = bio->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
Jens Axboe22e2c502005-06-27 10:55:12 +02001593 req->ioprio = ioprio_best(req->ioprio, prio);
Jerome Marchandb238b3d2007-10-23 15:05:46 +02001594 drive_stat_acct(req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 if (!attempt_front_merge(q, req))
Jens Axboe2e662b62006-07-13 11:55:04 +02001596 elv_merged_request(q, req, el_ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 goto out;
1598
Nick Piggin450991b2005-06-28 20:45:13 -07001599 /* ELV_NO_MERGE: elevator says don't/can't merge. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 default:
Nick Piggin450991b2005-06-28 20:45:13 -07001601 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 }
1603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07001605 /*
Jens Axboe7749a8d2006-12-13 13:02:26 +01001606 * This sync check and mask will be re-done in init_request_from_bio(),
1607 * but we need to set it earlier to expose the sync flag to the
1608 * rq allocator and io schedulers.
1609 */
1610 rw_flags = bio_data_dir(bio);
1611 if (sync)
1612 rw_flags |= REQ_RW_SYNC;
1613
1614 /*
Nick Piggin450991b2005-06-28 20:45:13 -07001615 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07001616 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07001617 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01001618 req = get_request_wait(q, rw_flags, bio);
Nick Piggind6344532005-06-28 20:45:14 -07001619
Nick Piggin450991b2005-06-28 20:45:13 -07001620 /*
1621 * After dropping the lock and possibly sleeping here, our request
1622 * may now be mergeable after it had proven unmergeable (above).
1623 * We don't worry about that case for efficiency. It won't happen
1624 * often, and the elevators are able to handle it.
1625 */
Tejun Heo52d9e672006-01-06 09:49:58 +01001626 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Nick Piggin450991b2005-06-28 20:45:13 -07001628 spin_lock_irq(q->queue_lock);
1629 if (elv_queue_empty(q))
1630 blk_plug_device(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 add_request(q, req);
1632out:
Jens Axboe4a534f92005-04-16 15:25:40 -07001633 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 __generic_unplug_device(q);
1635
1636 spin_unlock_irq(q->queue_lock);
1637 return 0;
1638
1639end_io:
NeilBrown6712ecf2007-09-27 12:47:43 +02001640 bio_endio(bio, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 return 0;
1642}
1643
1644/*
1645 * If bio->bi_dev is a partition, remap the location
1646 */
1647static inline void blk_partition_remap(struct bio *bio)
1648{
1649 struct block_device *bdev = bio->bi_bdev;
1650
Jens Axboebf2de6f2007-09-27 13:01:25 +02001651 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01001653 const int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Jens Axboea3623572005-11-01 09:26:16 +01001655 p->sectors[rw] += bio_sectors(bio);
1656 p->ios[rw]++;
1657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 bio->bi_sector += p->start_sect;
1659 bio->bi_bdev = bdev->bd_contains;
Alan D. Brunellec7149d62007-08-07 15:30:23 +02001660
1661 blk_add_trace_remap(bdev_get_queue(bio->bi_bdev), bio,
1662 bdev->bd_dev, bio->bi_sector,
1663 bio->bi_sector - p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
1665}
1666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667static void handle_bad_sector(struct bio *bio)
1668{
1669 char b[BDEVNAME_SIZE];
1670
1671 printk(KERN_INFO "attempt to access beyond end of device\n");
1672 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1673 bdevname(bio->bi_bdev, b),
1674 bio->bi_rw,
1675 (unsigned long long)bio->bi_sector + bio_sectors(bio),
1676 (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
1677
1678 set_bit(BIO_EOF, &bio->bi_flags);
1679}
1680
Akinobu Mitac17bb492006-12-08 02:39:46 -08001681#ifdef CONFIG_FAIL_MAKE_REQUEST
1682
1683static DECLARE_FAULT_ATTR(fail_make_request);
1684
1685static int __init setup_fail_make_request(char *str)
1686{
1687 return setup_fault_attr(&fail_make_request, str);
1688}
1689__setup("fail_make_request=", setup_fail_make_request);
1690
1691static int should_fail_request(struct bio *bio)
1692{
1693 if ((bio->bi_bdev->bd_disk->flags & GENHD_FL_FAIL) ||
1694 (bio->bi_bdev->bd_part && bio->bi_bdev->bd_part->make_it_fail))
1695 return should_fail(&fail_make_request, bio->bi_size);
1696
1697 return 0;
1698}
1699
1700static int __init fail_make_request_debugfs(void)
1701{
1702 return init_fault_attr_dentries(&fail_make_request,
1703 "fail_make_request");
1704}
1705
1706late_initcall(fail_make_request_debugfs);
1707
1708#else /* CONFIG_FAIL_MAKE_REQUEST */
1709
1710static inline int should_fail_request(struct bio *bio)
1711{
1712 return 0;
1713}
1714
1715#endif /* CONFIG_FAIL_MAKE_REQUEST */
1716
Jens Axboec07e2b42007-07-18 13:27:58 +02001717/*
1718 * Check whether this bio extends beyond the end of the device.
1719 */
1720static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1721{
1722 sector_t maxsector;
1723
1724 if (!nr_sectors)
1725 return 0;
1726
1727 /* Test device or partition size, when known. */
1728 maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
1729 if (maxsector) {
1730 sector_t sector = bio->bi_sector;
1731
1732 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1733 /*
1734 * This may well happen - the kernel calls bread()
1735 * without checking the size of the device, e.g., when
1736 * mounting a device.
1737 */
1738 handle_bad_sector(bio);
1739 return 1;
1740 }
1741 }
1742
1743 return 0;
1744}
1745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746/**
1747 * generic_make_request: hand a buffer to its device driver for I/O
1748 * @bio: The bio describing the location in memory and on the device.
1749 *
1750 * generic_make_request() is used to make I/O requests of block
1751 * devices. It is passed a &struct bio, which describes the I/O that needs
1752 * to be done.
1753 *
1754 * generic_make_request() does not return any status. The
1755 * success/failure status of the request, along with notification of
1756 * completion, is delivered asynchronously through the bio->bi_end_io
1757 * function described (one day) else where.
1758 *
1759 * The caller of generic_make_request must make sure that bi_io_vec
1760 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1761 * set to describe the device address, and the
1762 * bi_end_io and optionally bi_private are set to describe how
1763 * completion notification should be signaled.
1764 *
1765 * generic_make_request and the drivers it calls may use bi_next if this
1766 * bio happens to be merged with someone else, and may change bi_dev and
1767 * bi_sector for remaps as it sees fit. So the values of these fields
1768 * should NOT be depended on after the call to generic_make_request.
1769 */
Neil Brownd89d8792007-05-01 09:53:42 +02001770static inline void __generic_make_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771{
Jens Axboe165125e2007-07-24 09:28:11 +02001772 struct request_queue *q;
NeilBrown5ddfe962006-10-30 22:07:21 -08001773 sector_t old_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 int ret, nr_sectors = bio_sectors(bio);
Jens Axboe2056a782006-03-23 20:00:26 +01001775 dev_t old_dev;
Jens Axboe51fd77b2007-11-02 08:49:08 +01001776 int err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
1778 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Jens Axboec07e2b42007-07-18 13:27:58 +02001780 if (bio_check_eod(bio, nr_sectors))
1781 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
1783 /*
1784 * Resolve the mapping until finished. (drivers are
1785 * still free to implement/resolve their own stacking
1786 * by explicitly returning 0)
1787 *
1788 * NOTE: we don't repeat the blk_size check for each new device.
1789 * Stacking drivers are expected to know what they are doing.
1790 */
NeilBrown5ddfe962006-10-30 22:07:21 -08001791 old_sector = -1;
Jens Axboe2056a782006-03-23 20:00:26 +01001792 old_dev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 do {
1794 char b[BDEVNAME_SIZE];
1795
1796 q = bdev_get_queue(bio->bi_bdev);
1797 if (!q) {
1798 printk(KERN_ERR
1799 "generic_make_request: Trying to access "
1800 "nonexistent block-device %s (%Lu)\n",
1801 bdevname(bio->bi_bdev, b),
1802 (long long) bio->bi_sector);
1803end_io:
Jens Axboe51fd77b2007-11-02 08:49:08 +01001804 bio_endio(bio, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 break;
1806 }
1807
Jens Axboe4fa253f2007-07-18 13:13:10 +02001808 if (unlikely(nr_sectors > q->max_hw_sectors)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 printk("bio too big device %s (%u > %u)\n",
1810 bdevname(bio->bi_bdev, b),
1811 bio_sectors(bio),
1812 q->max_hw_sectors);
1813 goto end_io;
1814 }
1815
Nick Pigginfde6ad22005-06-23 00:08:53 -07001816 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 goto end_io;
1818
Akinobu Mitac17bb492006-12-08 02:39:46 -08001819 if (should_fail_request(bio))
1820 goto end_io;
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 /*
1823 * If this device has partitions, remap block n
1824 * of partition p to block n+start(p) of the disk.
1825 */
1826 blk_partition_remap(bio);
1827
NeilBrown5ddfe962006-10-30 22:07:21 -08001828 if (old_sector != -1)
Jens Axboe4fa253f2007-07-18 13:13:10 +02001829 blk_add_trace_remap(q, bio, old_dev, bio->bi_sector,
NeilBrown5ddfe962006-10-30 22:07:21 -08001830 old_sector);
Jens Axboe2056a782006-03-23 20:00:26 +01001831
1832 blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
1833
NeilBrown5ddfe962006-10-30 22:07:21 -08001834 old_sector = bio->bi_sector;
Jens Axboe2056a782006-03-23 20:00:26 +01001835 old_dev = bio->bi_bdev->bd_dev;
1836
Jens Axboec07e2b42007-07-18 13:27:58 +02001837 if (bio_check_eod(bio, nr_sectors))
1838 goto end_io;
Jens Axboe51fd77b2007-11-02 08:49:08 +01001839 if (bio_empty_barrier(bio) && !q->prepare_flush_fn) {
1840 err = -EOPNOTSUPP;
1841 goto end_io;
1842 }
NeilBrown5ddfe962006-10-30 22:07:21 -08001843
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 ret = q->make_request_fn(q, bio);
1845 } while (ret);
1846}
1847
Neil Brownd89d8792007-05-01 09:53:42 +02001848/*
1849 * We only want one ->make_request_fn to be active at a time,
1850 * else stack usage with stacked devices could be a problem.
1851 * So use current->bio_{list,tail} to keep a list of requests
1852 * submited by a make_request_fn function.
1853 * current->bio_tail is also used as a flag to say if
1854 * generic_make_request is currently active in this task or not.
1855 * If it is NULL, then no make_request is active. If it is non-NULL,
1856 * then a make_request is active, and new requests should be added
1857 * at the tail
1858 */
1859void generic_make_request(struct bio *bio)
1860{
1861 if (current->bio_tail) {
1862 /* make_request is active */
1863 *(current->bio_tail) = bio;
1864 bio->bi_next = NULL;
1865 current->bio_tail = &bio->bi_next;
1866 return;
1867 }
1868 /* following loop may be a bit non-obvious, and so deserves some
1869 * explanation.
1870 * Before entering the loop, bio->bi_next is NULL (as all callers
1871 * ensure that) so we have a list with a single bio.
1872 * We pretend that we have just taken it off a longer list, so
1873 * we assign bio_list to the next (which is NULL) and bio_tail
1874 * to &bio_list, thus initialising the bio_list of new bios to be
1875 * added. __generic_make_request may indeed add some more bios
1876 * through a recursive call to generic_make_request. If it
1877 * did, we find a non-NULL value in bio_list and re-enter the loop
1878 * from the top. In this case we really did just take the bio
1879 * of the top of the list (no pretending) and so fixup bio_list and
1880 * bio_tail or bi_next, and call into __generic_make_request again.
1881 *
1882 * The loop was structured like this to make only one call to
1883 * __generic_make_request (which is important as it is large and
1884 * inlined) and to keep the structure simple.
1885 */
1886 BUG_ON(bio->bi_next);
1887 do {
1888 current->bio_list = bio->bi_next;
1889 if (bio->bi_next == NULL)
1890 current->bio_tail = &current->bio_list;
1891 else
1892 bio->bi_next = NULL;
1893 __generic_make_request(bio);
1894 bio = current->bio_list;
1895 } while (bio);
1896 current->bio_tail = NULL; /* deactivate */
1897}
1898
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899EXPORT_SYMBOL(generic_make_request);
1900
1901/**
1902 * submit_bio: submit a bio to the block device layer for I/O
1903 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1904 * @bio: The &struct bio which describes the I/O
1905 *
1906 * submit_bio() is very similar in purpose to generic_make_request(), and
1907 * uses that function to do most of the work. Both are fairly rough
1908 * interfaces, @bio must be presetup and ready for I/O.
1909 *
1910 */
1911void submit_bio(int rw, struct bio *bio)
1912{
1913 int count = bio_sectors(bio);
1914
Jens Axboe22e2c502005-06-27 10:55:12 +02001915 bio->bi_rw |= rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Jens Axboebf2de6f2007-09-27 13:01:25 +02001917 /*
1918 * If it's a regular read/write or a barrier with data attached,
1919 * go through the normal accounting stuff before submission.
1920 */
1921 if (!bio_empty_barrier(bio)) {
1922
1923 BIO_BUG_ON(!bio->bi_size);
1924 BIO_BUG_ON(!bio->bi_io_vec);
1925
1926 if (rw & WRITE) {
1927 count_vm_events(PGPGOUT, count);
1928 } else {
1929 task_io_account_read(bio->bi_size);
1930 count_vm_events(PGPGIN, count);
1931 }
1932
1933 if (unlikely(block_dump)) {
1934 char b[BDEVNAME_SIZE];
1935 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001936 current->comm, task_pid_nr(current),
Jens Axboebf2de6f2007-09-27 13:01:25 +02001937 (rw & WRITE) ? "WRITE" : "READ",
1938 (unsigned long long)bio->bi_sector,
1939 bdevname(bio->bi_bdev,b));
1940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 }
1942
1943 generic_make_request(bio);
1944}
1945
1946EXPORT_SYMBOL(submit_bio);
1947
Adrian Bunk93d17d32005-06-25 14:59:10 -07001948static void blk_recalc_rq_sectors(struct request *rq, int nsect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
1950 if (blk_fs_request(rq)) {
1951 rq->hard_sector += nsect;
1952 rq->hard_nr_sectors -= nsect;
1953
1954 /*
1955 * Move the I/O submission pointers ahead if required.
1956 */
1957 if ((rq->nr_sectors >= rq->hard_nr_sectors) &&
1958 (rq->sector <= rq->hard_sector)) {
1959 rq->sector = rq->hard_sector;
1960 rq->nr_sectors = rq->hard_nr_sectors;
1961 rq->hard_cur_sectors = bio_cur_sectors(rq->bio);
1962 rq->current_nr_sectors = rq->hard_cur_sectors;
1963 rq->buffer = bio_data(rq->bio);
1964 }
1965
1966 /*
1967 * if total number of sectors is less than the first segment
1968 * size, something has gone terribly wrong
1969 */
1970 if (rq->nr_sectors < rq->current_nr_sectors) {
1971 printk("blk: request botched\n");
1972 rq->nr_sectors = rq->current_nr_sectors;
1973 }
1974 }
1975}
1976
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001977/**
1978 * __end_that_request_first - end I/O on a request
1979 * @req: the request being processed
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001980 * @error: 0 for success, < 0 for error
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05001981 * @nr_bytes: number of bytes to complete
1982 *
1983 * Description:
1984 * Ends I/O on a number of bytes attached to @req, and sets it up
1985 * for the next range of segments (if any) in the cluster.
1986 *
1987 * Return:
1988 * 0 - we are done with this request, call end_that_request_last()
1989 * 1 - still buffers pending for this request
1990 **/
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001991static int __end_that_request_first(struct request *req, int error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 int nr_bytes)
1993{
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05001994 int total_bytes, bio_nbytes, next_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 struct bio *bio;
1996
Jens Axboe2056a782006-03-23 20:00:26 +01001997 blk_add_trace_rq(req->q, req, BLK_TA_COMPLETE);
1998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 * for a REQ_BLOCK_PC request, we want to carry any eventual
2001 * sense key with us all the way through
2002 */
2003 if (!blk_pc_request(req))
2004 req->errors = 0;
2005
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05002006 if (error) {
Jens Axboe4aff5e22006-08-10 08:44:47 +02002007 if (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 printk("end_request: I/O error, dev %s, sector %llu\n",
2009 req->rq_disk ? req->rq_disk->disk_name : "?",
2010 (unsigned long long)req->sector);
2011 }
2012
Jens Axboed72d9042005-11-01 08:35:42 +01002013 if (blk_fs_request(req) && req->rq_disk) {
Jens Axboea3623572005-11-01 09:26:16 +01002014 const int rw = rq_data_dir(req);
2015
Jens Axboe53e86062006-01-17 11:09:27 +01002016 disk_stat_add(req->rq_disk, sectors[rw], nr_bytes >> 9);
Jens Axboed72d9042005-11-01 08:35:42 +01002017 }
2018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 total_bytes = bio_nbytes = 0;
2020 while ((bio = req->bio) != NULL) {
2021 int nbytes;
2022
Jens Axboebf2de6f2007-09-27 13:01:25 +02002023 /*
2024 * For an empty barrier request, the low level driver must
2025 * store a potential error location in ->sector. We pass
2026 * that back up in ->bi_sector.
2027 */
2028 if (blk_empty_barrier(req))
2029 bio->bi_sector = req->sector;
2030
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 if (nr_bytes >= bio->bi_size) {
2032 req->bio = bio->bi_next;
2033 nbytes = bio->bi_size;
NeilBrown5bb23a62007-09-27 12:46:13 +02002034 req_bio_endio(req, bio, nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 next_idx = 0;
2036 bio_nbytes = 0;
2037 } else {
2038 int idx = bio->bi_idx + next_idx;
2039
2040 if (unlikely(bio->bi_idx >= bio->bi_vcnt)) {
2041 blk_dump_rq_flags(req, "__end_that");
2042 printk("%s: bio idx %d >= vcnt %d\n",
2043 __FUNCTION__,
2044 bio->bi_idx, bio->bi_vcnt);
2045 break;
2046 }
2047
2048 nbytes = bio_iovec_idx(bio, idx)->bv_len;
2049 BIO_BUG_ON(nbytes > bio->bi_size);
2050
2051 /*
2052 * not a complete bvec done
2053 */
2054 if (unlikely(nbytes > nr_bytes)) {
2055 bio_nbytes += nr_bytes;
2056 total_bytes += nr_bytes;
2057 break;
2058 }
2059
2060 /*
2061 * advance to the next vector
2062 */
2063 next_idx++;
2064 bio_nbytes += nbytes;
2065 }
2066
2067 total_bytes += nbytes;
2068 nr_bytes -= nbytes;
2069
2070 if ((bio = req->bio)) {
2071 /*
2072 * end more in this run, or just return 'not-done'
2073 */
2074 if (unlikely(nr_bytes <= 0))
2075 break;
2076 }
2077 }
2078
2079 /*
2080 * completely done
2081 */
2082 if (!req->bio)
2083 return 0;
2084
2085 /*
2086 * if the request wasn't completed, update state
2087 */
2088 if (bio_nbytes) {
NeilBrown5bb23a62007-09-27 12:46:13 +02002089 req_bio_endio(req, bio, bio_nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 bio->bi_idx += next_idx;
2091 bio_iovec(bio)->bv_offset += nr_bytes;
2092 bio_iovec(bio)->bv_len -= nr_bytes;
2093 }
2094
2095 blk_recalc_rq_sectors(req, total_bytes >> 9);
2096 blk_recalc_rq_segments(req);
2097 return 1;
2098}
2099
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100/*
Jens Axboeff856ba2006-01-09 16:02:34 +01002101 * splice the completion data to a local structure and hand off to
2102 * process_completion_queue() to complete the requests
2103 */
2104static void blk_done_softirq(struct softirq_action *h)
2105{
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07002106 struct list_head *cpu_list, local_list;
Jens Axboeff856ba2006-01-09 16:02:34 +01002107
2108 local_irq_disable();
2109 cpu_list = &__get_cpu_var(blk_cpu_done);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07002110 list_replace_init(cpu_list, &local_list);
Jens Axboeff856ba2006-01-09 16:02:34 +01002111 local_irq_enable();
2112
2113 while (!list_empty(&local_list)) {
2114 struct request *rq = list_entry(local_list.next, struct request, donelist);
2115
2116 list_del_init(&rq->donelist);
2117 rq->q->softirq_done_fn(rq);
2118 }
2119}
2120
Satyam Sharmadb47d472007-08-23 09:29:40 +02002121static int __cpuinit blk_cpu_notify(struct notifier_block *self, unsigned long action,
Jens Axboeff856ba2006-01-09 16:02:34 +01002122 void *hcpu)
2123{
2124 /*
2125 * If a CPU goes away, splice its entries to the current CPU
2126 * and trigger a run of the softirq
2127 */
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07002128 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
Jens Axboeff856ba2006-01-09 16:02:34 +01002129 int cpu = (unsigned long) hcpu;
2130
2131 local_irq_disable();
2132 list_splice_init(&per_cpu(blk_cpu_done, cpu),
2133 &__get_cpu_var(blk_cpu_done));
2134 raise_softirq_irqoff(BLOCK_SOFTIRQ);
2135 local_irq_enable();
2136 }
2137
2138 return NOTIFY_OK;
2139}
2140
2141
Satyam Sharmadb47d472007-08-23 09:29:40 +02002142static struct notifier_block blk_cpu_notifier __cpuinitdata = {
Jens Axboeff856ba2006-01-09 16:02:34 +01002143 .notifier_call = blk_cpu_notify,
2144};
2145
Jens Axboeff856ba2006-01-09 16:02:34 +01002146/**
2147 * blk_complete_request - end I/O on a request
2148 * @req: the request being processed
2149 *
2150 * Description:
2151 * Ends all I/O on a request. It does not handle partial completions,
Andreas Mohrd6e05ed2006-06-26 18:35:02 +02002152 * unless the driver actually implements this in its completion callback
Jens Axboe4fa253f2007-07-18 13:13:10 +02002153 * through requeueing. The actual completion happens out-of-order,
Jens Axboeff856ba2006-01-09 16:02:34 +01002154 * through a softirq handler. The user must have registered a completion
2155 * callback through blk_queue_softirq_done().
2156 **/
2157
2158void blk_complete_request(struct request *req)
2159{
2160 struct list_head *cpu_list;
2161 unsigned long flags;
2162
2163 BUG_ON(!req->q->softirq_done_fn);
2164
2165 local_irq_save(flags);
2166
2167 cpu_list = &__get_cpu_var(blk_cpu_done);
2168 list_add_tail(&req->donelist, cpu_list);
2169 raise_softirq_irqoff(BLOCK_SOFTIRQ);
2170
2171 local_irq_restore(flags);
2172}
2173
2174EXPORT_SYMBOL(blk_complete_request);
2175
2176/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 * queue lock must be held
2178 */
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05002179static void end_that_request_last(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180{
2181 struct gendisk *disk = req->rq_disk;
Tejun Heo8ffdc652006-01-06 09:49:03 +01002182
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002183 if (blk_rq_tagged(req))
2184 blk_queue_end_tag(req->q, req);
2185
2186 if (blk_queued_rq(req))
2187 blkdev_dequeue_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
2189 if (unlikely(laptop_mode) && blk_fs_request(req))
2190 laptop_io_completion();
2191
Jens Axboefd0ff8a2006-05-23 11:23:49 +02002192 /*
2193 * Account IO completion. bar_rq isn't accounted as a normal
2194 * IO on queueing nor completion. Accounting the containing
2195 * request is enough.
2196 */
2197 if (disk && blk_fs_request(req) && req != &req->q->bar_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 unsigned long duration = jiffies - req->start_time;
Jens Axboea3623572005-11-01 09:26:16 +01002199 const int rw = rq_data_dir(req);
2200
2201 __disk_stat_inc(disk, ios[rw]);
2202 __disk_stat_add(disk, ticks[rw], duration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 disk_round_stats(disk);
2204 disk->in_flight--;
2205 }
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01002208 req->end_io(req, error);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002209 else {
2210 if (blk_bidi_rq(req))
2211 __blk_put_request(req->next_rq->q, req->next_rq);
2212
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 __blk_put_request(req->q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 }
2215}
2216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217static inline void __end_request(struct request *rq, int uptodate,
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05002218 unsigned int nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05002220 int error = 0;
2221
2222 if (uptodate <= 0)
2223 error = uptodate ? uptodate : -EIO;
2224
2225 __blk_end_request(rq, error, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226}
2227
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05002228/**
2229 * blk_rq_bytes - Returns bytes left to complete in the entire request
2230 **/
2231unsigned int blk_rq_bytes(struct request *rq)
Jens Axboea0cd1282007-09-21 10:41:07 +02002232{
2233 if (blk_fs_request(rq))
2234 return rq->hard_nr_sectors << 9;
2235
2236 return rq->data_len;
2237}
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05002238EXPORT_SYMBOL_GPL(blk_rq_bytes);
2239
2240/**
2241 * blk_rq_cur_bytes - Returns bytes left to complete in the current segment
2242 **/
2243unsigned int blk_rq_cur_bytes(struct request *rq)
2244{
2245 if (blk_fs_request(rq))
2246 return rq->current_nr_sectors << 9;
2247
2248 if (rq->bio)
2249 return rq->bio->bi_size;
2250
2251 return rq->data_len;
2252}
2253EXPORT_SYMBOL_GPL(blk_rq_cur_bytes);
Jens Axboea0cd1282007-09-21 10:41:07 +02002254
2255/**
2256 * end_queued_request - end all I/O on a queued request
2257 * @rq: the request being processed
2258 * @uptodate: error value or 0/1 uptodate flag
2259 *
2260 * Description:
2261 * Ends all I/O on a request, and removes it from the block layer queues.
2262 * Not suitable for normal IO completion, unless the driver still has
2263 * the request attached to the block layer.
2264 *
2265 **/
2266void end_queued_request(struct request *rq, int uptodate)
2267{
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05002268 __end_request(rq, uptodate, blk_rq_bytes(rq));
Jens Axboea0cd1282007-09-21 10:41:07 +02002269}
2270EXPORT_SYMBOL(end_queued_request);
2271
2272/**
2273 * end_dequeued_request - end all I/O on a dequeued request
2274 * @rq: the request being processed
2275 * @uptodate: error value or 0/1 uptodate flag
2276 *
2277 * Description:
2278 * Ends all I/O on a request. The request must already have been
2279 * dequeued using blkdev_dequeue_request(), as is normally the case
2280 * for most drivers.
2281 *
2282 **/
2283void end_dequeued_request(struct request *rq, int uptodate)
2284{
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05002285 __end_request(rq, uptodate, blk_rq_bytes(rq));
Jens Axboea0cd1282007-09-21 10:41:07 +02002286}
2287EXPORT_SYMBOL(end_dequeued_request);
2288
2289
2290/**
2291 * end_request - end I/O on the current segment of the request
Randy Dunlap8f731f72007-10-18 23:39:28 -07002292 * @req: the request being processed
Jens Axboea0cd1282007-09-21 10:41:07 +02002293 * @uptodate: error value or 0/1 uptodate flag
2294 *
2295 * Description:
2296 * Ends I/O on the current segment of a request. If that is the only
2297 * remaining segment, the request is also completed and freed.
2298 *
2299 * This is a remnant of how older block drivers handled IO completions.
2300 * Modern drivers typically end IO on the full request in one go, unless
2301 * they have a residual value to account for. For that case this function
2302 * isn't really useful, unless the residual just happens to be the
2303 * full current segment. In other words, don't use this function in new
2304 * code. Either use end_request_completely(), or the
2305 * end_that_request_chunk() (along with end_that_request_last()) for
2306 * partial completions.
2307 *
2308 **/
2309void end_request(struct request *req, int uptodate)
2310{
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05002311 __end_request(req, uptodate, req->hard_cur_sectors << 9);
Jens Axboea0cd1282007-09-21 10:41:07 +02002312}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313EXPORT_SYMBOL(end_request);
2314
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002315/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002316 * blk_end_io - Generic end_io function to complete a request.
2317 * @rq: the request being processed
2318 * @error: 0 for success, < 0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002319 * @nr_bytes: number of bytes to complete @rq
2320 * @bidi_bytes: number of bytes to complete @rq->next_rq
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002321 * @drv_callback: function called between completion of bios in the request
2322 * and completion of the request.
2323 * If the callback returns non 0, this helper returns without
2324 * completion of the request.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002325 *
2326 * Description:
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002327 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002328 * If @rq has leftover, sets it up for the next range of segments.
2329 *
2330 * Return:
2331 * 0 - we are done with this request
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002332 * 1 - this request is not freed yet, it still has pending buffers.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002333 **/
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002334static int blk_end_io(struct request *rq, int error, int nr_bytes,
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002335 int bidi_bytes, int (drv_callback)(struct request *))
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002336{
2337 struct request_queue *q = rq->q;
2338 unsigned long flags = 0UL;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002339
2340 if (blk_fs_request(rq) || blk_pc_request(rq)) {
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05002341 if (__end_that_request_first(rq, error, nr_bytes))
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002342 return 1;
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002343
2344 /* Bidi request must be completed as a whole */
2345 if (blk_bidi_rq(rq) &&
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05002346 __end_that_request_first(rq->next_rq, error, bidi_bytes))
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002347 return 1;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002348 }
2349
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002350 /* Special feature for tricky drivers */
2351 if (drv_callback && drv_callback(rq))
2352 return 1;
2353
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002354 add_disk_randomness(rq->rq_disk);
2355
2356 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002357 end_that_request_last(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002358 spin_unlock_irqrestore(q->queue_lock, flags);
2359
2360 return 0;
2361}
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002362
2363/**
2364 * blk_end_request - Helper function for drivers to complete the request.
2365 * @rq: the request being processed
2366 * @error: 0 for success, < 0 for error
2367 * @nr_bytes: number of bytes to complete
2368 *
2369 * Description:
2370 * Ends I/O on a number of bytes attached to @rq.
2371 * If @rq has leftover, sets it up for the next range of segments.
2372 *
2373 * Return:
2374 * 0 - we are done with this request
2375 * 1 - still buffers pending for this request
2376 **/
2377int blk_end_request(struct request *rq, int error, int nr_bytes)
2378{
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002379 return blk_end_io(rq, error, nr_bytes, 0, NULL);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002380}
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002381EXPORT_SYMBOL_GPL(blk_end_request);
2382
2383/**
2384 * __blk_end_request - Helper function for drivers to complete the request.
2385 * @rq: the request being processed
2386 * @error: 0 for success, < 0 for error
2387 * @nr_bytes: number of bytes to complete
2388 *
2389 * Description:
2390 * Must be called with queue lock held unlike blk_end_request().
2391 *
2392 * Return:
2393 * 0 - we are done with this request
2394 * 1 - still buffers pending for this request
2395 **/
2396int __blk_end_request(struct request *rq, int error, int nr_bytes)
2397{
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002398 if (blk_fs_request(rq) || blk_pc_request(rq)) {
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05002399 if (__end_that_request_first(rq, error, nr_bytes))
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002400 return 1;
2401 }
2402
2403 add_disk_randomness(rq->rq_disk);
2404
Kiyoshi Uedab8286232007-12-11 17:53:24 -05002405 end_that_request_last(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05002406
2407 return 0;
2408}
2409EXPORT_SYMBOL_GPL(__blk_end_request);
2410
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002411/**
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002412 * blk_end_bidi_request - Helper function for drivers to complete bidi request.
2413 * @rq: the bidi request being processed
2414 * @error: 0 for success, < 0 for error
2415 * @nr_bytes: number of bytes to complete @rq
2416 * @bidi_bytes: number of bytes to complete @rq->next_rq
2417 *
2418 * Description:
2419 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
2420 *
2421 * Return:
2422 * 0 - we are done with this request
2423 * 1 - still buffers pending for this request
2424 **/
2425int blk_end_bidi_request(struct request *rq, int error, int nr_bytes,
2426 int bidi_bytes)
2427{
2428 return blk_end_io(rq, error, nr_bytes, bidi_bytes, NULL);
2429}
2430EXPORT_SYMBOL_GPL(blk_end_bidi_request);
2431
2432/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002433 * blk_end_request_callback - Special helper function for tricky drivers
2434 * @rq: the request being processed
2435 * @error: 0 for success, < 0 for error
2436 * @nr_bytes: number of bytes to complete
2437 * @drv_callback: function called between completion of bios in the request
2438 * and completion of the request.
2439 * If the callback returns non 0, this helper returns without
2440 * completion of the request.
2441 *
2442 * Description:
2443 * Ends I/O on a number of bytes attached to @rq.
2444 * If @rq has leftover, sets it up for the next range of segments.
2445 *
2446 * This special helper function is used only for existing tricky drivers.
2447 * (e.g. cdrom_newpc_intr() of ide-cd)
2448 * This interface will be removed when such drivers are rewritten.
2449 * Don't use this interface in other places anymore.
2450 *
2451 * Return:
2452 * 0 - we are done with this request
2453 * 1 - this request is not freed yet.
2454 * this request still has pending buffers or
2455 * the driver doesn't want to finish this request yet.
2456 **/
2457int blk_end_request_callback(struct request *rq, int error, int nr_bytes,
2458 int (drv_callback)(struct request *))
2459{
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05002460 return blk_end_io(rq, error, nr_bytes, 0, drv_callback);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05002461}
2462EXPORT_SYMBOL_GPL(blk_end_request_callback);
2463
Jens Axboe86db1e22008-01-29 14:53:40 +01002464void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2465 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466{
Jens Axboe4aff5e22006-08-10 08:44:47 +02002467 /* first two bits are identical in rq->cmd_flags and bio->bi_rw */
2468 rq->cmd_flags |= (bio->bi_rw & 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
2470 rq->nr_phys_segments = bio_phys_segments(q, bio);
2471 rq->nr_hw_segments = bio_hw_segments(q, bio);
2472 rq->current_nr_sectors = bio_cur_sectors(bio);
2473 rq->hard_cur_sectors = rq->current_nr_sectors;
2474 rq->hard_nr_sectors = rq->nr_sectors = bio_sectors(bio);
2475 rq->buffer = bio_data(bio);
Mike Christie0e75f902006-12-01 10:40:55 +01002476 rq->data_len = bio->bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
2478 rq->bio = rq->biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
NeilBrown66846572007-08-16 13:31:28 +02002480 if (bio->bi_bdev)
2481 rq->rq_disk = bio->bi_bdev->bd_disk;
2482}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
2484int kblockd_schedule_work(struct work_struct *work)
2485{
2486 return queue_work(kblockd_workqueue, work);
2487}
2488
2489EXPORT_SYMBOL(kblockd_schedule_work);
2490
Andrew Morton19a75d82007-05-09 02:33:56 -07002491void kblockd_flush_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492{
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07002493 cancel_work_sync(work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494}
Andrew Morton19a75d82007-05-09 02:33:56 -07002495EXPORT_SYMBOL(kblockd_flush_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
2497int __init blk_dev_init(void)
2498{
Jens Axboeff856ba2006-01-09 16:02:34 +01002499 int i;
2500
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 kblockd_workqueue = create_workqueue("kblockd");
2502 if (!kblockd_workqueue)
2503 panic("Failed to create kblockd\n");
2504
2505 request_cachep = kmem_cache_create("blkdev_requests",
Paul Mundt20c2df82007-07-20 10:11:58 +09002506 sizeof(struct request), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
Jens Axboe8324aa92008-01-29 14:51:59 +01002508 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02002509 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002511 for_each_possible_cpu(i)
Jens Axboeff856ba2006-01-09 16:02:34 +01002512 INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i));
2513
2514 open_softirq(BLOCK_SOFTIRQ, blk_done_softirq, NULL);
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07002515 register_hotcpu_notifier(&blk_cpu_notifier);
Jens Axboeff856ba2006-01-09 16:02:34 +01002516
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 return 0;
2518}
2519