blob: 5c62507a510d1d9a3d1320e6e6b3a1378f94bd6a [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 */
13#include <linux/config.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/backing-dev.h>
17#include <linux/bio.h>
18#include <linux/blkdev.h>
19#include <linux/highmem.h>
20#include <linux/mm.h>
21#include <linux/kernel_stat.h>
22#include <linux/string.h>
23#include <linux/init.h>
24#include <linux/bootmem.h> /* for max_pfn/max_low_pfn */
25#include <linux/completion.h>
26#include <linux/slab.h>
27#include <linux/swap.h>
28#include <linux/writeback.h>
Jens Axboeff856ba2006-01-09 16:02:34 +010029#include <linux/interrupt.h>
30#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32/*
33 * for max sense size
34 */
35#include <scsi/scsi_cmnd.h>
36
37static void blk_unplug_work(void *data);
38static void blk_unplug_timeout(unsigned long data);
Adrian Bunk93d17d32005-06-25 14:59:10 -070039static void drive_stat_acct(struct request *rq, int nr_sectors, int new_io);
Tejun Heo52d9e672006-01-06 09:49:58 +010040static void init_request_from_bio(struct request *req, struct bio *bio);
41static int __make_request(request_queue_t *q, struct bio *bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/*
44 * For the allocated request tables
45 */
46static kmem_cache_t *request_cachep;
47
48/*
49 * For queue allocation
50 */
51static kmem_cache_t *requestq_cachep;
52
53/*
54 * For io context allocations
55 */
56static kmem_cache_t *iocontext_cachep;
57
58static wait_queue_head_t congestion_wqh[2] = {
59 __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[0]),
60 __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[1])
61 };
62
63/*
64 * Controlling structure to kblockd
65 */
Jens Axboeff856ba2006-01-09 16:02:34 +010066static struct workqueue_struct *kblockd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68unsigned long blk_max_low_pfn, blk_max_pfn;
69
70EXPORT_SYMBOL(blk_max_low_pfn);
71EXPORT_SYMBOL(blk_max_pfn);
72
Jens Axboeff856ba2006-01-09 16:02:34 +010073static DEFINE_PER_CPU(struct list_head, blk_cpu_done);
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/* Amount of time in which a process may batch requests */
76#define BLK_BATCH_TIME (HZ/50UL)
77
78/* Number of requests a "batching" process may submit */
79#define BLK_BATCH_REQ 32
80
81/*
82 * Return the threshold (number of used requests) at which the queue is
83 * considered to be congested. It include a little hysteresis to keep the
84 * context switch rate down.
85 */
86static inline int queue_congestion_on_threshold(struct request_queue *q)
87{
88 return q->nr_congestion_on;
89}
90
91/*
92 * The threshold at which a queue is considered to be uncongested
93 */
94static inline int queue_congestion_off_threshold(struct request_queue *q)
95{
96 return q->nr_congestion_off;
97}
98
99static void blk_queue_congestion_threshold(struct request_queue *q)
100{
101 int nr;
102
103 nr = q->nr_requests - (q->nr_requests / 8) + 1;
104 if (nr > q->nr_requests)
105 nr = q->nr_requests;
106 q->nr_congestion_on = nr;
107
108 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
109 if (nr < 1)
110 nr = 1;
111 q->nr_congestion_off = nr;
112}
113
114/*
115 * A queue has just exitted congestion. Note this in the global counter of
116 * congested queues, and wake up anyone who was waiting for requests to be
117 * put back.
118 */
119static void clear_queue_congested(request_queue_t *q, int rw)
120{
121 enum bdi_state bit;
122 wait_queue_head_t *wqh = &congestion_wqh[rw];
123
124 bit = (rw == WRITE) ? BDI_write_congested : BDI_read_congested;
125 clear_bit(bit, &q->backing_dev_info.state);
126 smp_mb__after_clear_bit();
127 if (waitqueue_active(wqh))
128 wake_up(wqh);
129}
130
131/*
132 * A queue has just entered congestion. Flag that in the queue's VM-visible
133 * state flags and increment the global gounter of congested queues.
134 */
135static void set_queue_congested(request_queue_t *q, int rw)
136{
137 enum bdi_state bit;
138
139 bit = (rw == WRITE) ? BDI_write_congested : BDI_read_congested;
140 set_bit(bit, &q->backing_dev_info.state);
141}
142
143/**
144 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
145 * @bdev: device
146 *
147 * Locates the passed device's request queue and returns the address of its
148 * backing_dev_info
149 *
150 * Will return NULL if the request queue cannot be located.
151 */
152struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
153{
154 struct backing_dev_info *ret = NULL;
155 request_queue_t *q = bdev_get_queue(bdev);
156
157 if (q)
158 ret = &q->backing_dev_info;
159 return ret;
160}
161
162EXPORT_SYMBOL(blk_get_backing_dev_info);
163
164void blk_queue_activity_fn(request_queue_t *q, activity_fn *fn, void *data)
165{
166 q->activity_fn = fn;
167 q->activity_data = data;
168}
169
170EXPORT_SYMBOL(blk_queue_activity_fn);
171
172/**
173 * blk_queue_prep_rq - set a prepare_request function for queue
174 * @q: queue
175 * @pfn: prepare_request function
176 *
177 * It's possible for a queue to register a prepare_request callback which
178 * is invoked before the request is handed to the request_fn. The goal of
179 * the function is to prepare a request for I/O, it can be used to build a
180 * cdb from the request data for instance.
181 *
182 */
183void blk_queue_prep_rq(request_queue_t *q, prep_rq_fn *pfn)
184{
185 q->prep_rq_fn = pfn;
186}
187
188EXPORT_SYMBOL(blk_queue_prep_rq);
189
190/**
191 * blk_queue_merge_bvec - set a merge_bvec function for queue
192 * @q: queue
193 * @mbfn: merge_bvec_fn
194 *
195 * Usually queues have static limitations on the max sectors or segments that
196 * we can put in a request. Stacking drivers may have some settings that
197 * are dynamic, and thus we have to query the queue whether it is ok to
198 * add a new bio_vec to a bio at a given offset or not. If the block device
199 * has such limitations, it needs to register a merge_bvec_fn to control
200 * the size of bio's sent to it. Note that a block device *must* allow a
201 * single page to be added to an empty bio. The block device driver may want
202 * to use the bio_split() function to deal with these bio's. By default
203 * no merge_bvec_fn is defined for a queue, and only the fixed limits are
204 * honored.
205 */
206void blk_queue_merge_bvec(request_queue_t *q, merge_bvec_fn *mbfn)
207{
208 q->merge_bvec_fn = mbfn;
209}
210
211EXPORT_SYMBOL(blk_queue_merge_bvec);
212
Jens Axboeff856ba2006-01-09 16:02:34 +0100213void blk_queue_softirq_done(request_queue_t *q, softirq_done_fn *fn)
214{
215 q->softirq_done_fn = fn;
216}
217
218EXPORT_SYMBOL(blk_queue_softirq_done);
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220/**
221 * blk_queue_make_request - define an alternate make_request function for a device
222 * @q: the request queue for the device to be affected
223 * @mfn: the alternate make_request function
224 *
225 * Description:
226 * The normal way for &struct bios to be passed to a device
227 * driver is for them to be collected into requests on a request
228 * queue, and then to allow the device driver to select requests
229 * off that queue when it is ready. This works well for many block
230 * devices. However some block devices (typically virtual devices
231 * such as md or lvm) do not benefit from the processing on the
232 * request queue, and are served best by having the requests passed
233 * directly to them. This can be achieved by providing a function
234 * to blk_queue_make_request().
235 *
236 * Caveat:
237 * The driver that does this *must* be able to deal appropriately
238 * with buffers in "highmemory". This can be accomplished by either calling
239 * __bio_kmap_atomic() to get a temporary kernel mapping, or by calling
240 * blk_queue_bounce() to create a buffer in normal memory.
241 **/
242void blk_queue_make_request(request_queue_t * q, make_request_fn * mfn)
243{
244 /*
245 * set defaults
246 */
247 q->nr_requests = BLKDEV_MAX_RQ;
Stuart McLaren309c0a12005-09-06 15:17:47 -0700248 blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS);
249 blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 q->make_request_fn = mfn;
251 q->backing_dev_info.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
252 q->backing_dev_info.state = 0;
253 q->backing_dev_info.capabilities = BDI_CAP_MAP_COPY;
Mike Christiedefd94b2005-12-05 02:37:06 -0600254 blk_queue_max_sectors(q, SAFE_MAX_SECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 blk_queue_hardsect_size(q, 512);
256 blk_queue_dma_alignment(q, 511);
257 blk_queue_congestion_threshold(q);
258 q->nr_batching = BLK_BATCH_REQ;
259
260 q->unplug_thresh = 4; /* hmm */
261 q->unplug_delay = (3 * HZ) / 1000; /* 3 milliseconds */
262 if (q->unplug_delay == 0)
263 q->unplug_delay = 1;
264
265 INIT_WORK(&q->unplug_work, blk_unplug_work, q);
266
267 q->unplug_timer.function = blk_unplug_timeout;
268 q->unplug_timer.data = (unsigned long)q;
269
270 /*
271 * by default assume old behaviour and bounce for any highmem page
272 */
273 blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
274
275 blk_queue_activity_fn(q, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278EXPORT_SYMBOL(blk_queue_make_request);
279
280static inline void rq_init(request_queue_t *q, struct request *rq)
281{
282 INIT_LIST_HEAD(&rq->queuelist);
Jens Axboeff856ba2006-01-09 16:02:34 +0100283 INIT_LIST_HEAD(&rq->donelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 rq->errors = 0;
286 rq->rq_status = RQ_ACTIVE;
287 rq->bio = rq->biotail = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +0200288 rq->ioprio = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 rq->buffer = NULL;
290 rq->ref_count = 1;
291 rq->q = q;
292 rq->waiting = NULL;
293 rq->special = NULL;
294 rq->data_len = 0;
295 rq->data = NULL;
Mike Christie df46b9a2005-06-20 14:04:44 +0200296 rq->nr_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 rq->sense = NULL;
298 rq->end_io = NULL;
299 rq->end_io_data = NULL;
Jens Axboeff856ba2006-01-09 16:02:34 +0100300 rq->completion_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
303/**
304 * blk_queue_ordered - does this queue support ordered writes
Tejun Heo797e7db2006-01-06 09:51:03 +0100305 * @q: the request queue
306 * @ordered: one of QUEUE_ORDERED_*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 *
308 * Description:
309 * For journalled file systems, doing ordered writes on a commit
310 * block instead of explicitly doing wait_on_buffer (which is bad
311 * for performance) can be a big win. Block drivers supporting this
312 * feature should call this function and indicate so.
313 *
314 **/
Tejun Heo797e7db2006-01-06 09:51:03 +0100315int blk_queue_ordered(request_queue_t *q, unsigned ordered,
316 prepare_flush_fn *prepare_flush_fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Tejun Heo797e7db2006-01-06 09:51:03 +0100318 if (ordered & (QUEUE_ORDERED_PREFLUSH | QUEUE_ORDERED_POSTFLUSH) &&
319 prepare_flush_fn == NULL) {
320 printk(KERN_ERR "blk_queue_ordered: prepare_flush_fn required\n");
321 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100323
324 if (ordered != QUEUE_ORDERED_NONE &&
325 ordered != QUEUE_ORDERED_DRAIN &&
326 ordered != QUEUE_ORDERED_DRAIN_FLUSH &&
327 ordered != QUEUE_ORDERED_DRAIN_FUA &&
328 ordered != QUEUE_ORDERED_TAG &&
329 ordered != QUEUE_ORDERED_TAG_FLUSH &&
330 ordered != QUEUE_ORDERED_TAG_FUA) {
331 printk(KERN_ERR "blk_queue_ordered: bad value %d\n", ordered);
332 return -EINVAL;
333 }
334
335 q->next_ordered = ordered;
336 q->prepare_flush_fn = prepare_flush_fn;
337
338 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
341EXPORT_SYMBOL(blk_queue_ordered);
342
343/**
344 * blk_queue_issue_flush_fn - set function for issuing a flush
345 * @q: the request queue
346 * @iff: the function to be called issuing the flush
347 *
348 * Description:
349 * If a driver supports issuing a flush command, the support is notified
350 * to the block layer by defining it through this call.
351 *
352 **/
353void blk_queue_issue_flush_fn(request_queue_t *q, issue_flush_fn *iff)
354{
355 q->issue_flush_fn = iff;
356}
357
358EXPORT_SYMBOL(blk_queue_issue_flush_fn);
359
360/*
361 * Cache flushing for ordered writes handling
362 */
Tejun Heo797e7db2006-01-06 09:51:03 +0100363inline unsigned blk_ordered_cur_seq(request_queue_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Tejun Heo797e7db2006-01-06 09:51:03 +0100365 if (!q->ordseq)
366 return 0;
367 return 1 << ffz(q->ordseq);
368}
369
370unsigned blk_ordered_req_seq(struct request *rq)
371{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 request_queue_t *q = rq->q;
373
Tejun Heo797e7db2006-01-06 09:51:03 +0100374 BUG_ON(q->ordseq == 0);
Tejun Heo8922e162005-10-20 16:23:44 +0200375
Tejun Heo797e7db2006-01-06 09:51:03 +0100376 if (rq == &q->pre_flush_rq)
377 return QUEUE_ORDSEQ_PREFLUSH;
378 if (rq == &q->bar_rq)
379 return QUEUE_ORDSEQ_BAR;
380 if (rq == &q->post_flush_rq)
381 return QUEUE_ORDSEQ_POSTFLUSH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Tejun Heo797e7db2006-01-06 09:51:03 +0100383 if ((rq->flags & REQ_ORDERED_COLOR) ==
384 (q->orig_bar_rq->flags & REQ_ORDERED_COLOR))
385 return QUEUE_ORDSEQ_DRAIN;
386 else
387 return QUEUE_ORDSEQ_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
Tejun Heo797e7db2006-01-06 09:51:03 +0100390void blk_ordered_complete_seq(request_queue_t *q, unsigned seq, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Tejun Heo797e7db2006-01-06 09:51:03 +0100392 struct request *rq;
393 int uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Tejun Heo797e7db2006-01-06 09:51:03 +0100395 if (error && !q->orderr)
396 q->orderr = error;
Tejun Heo8922e162005-10-20 16:23:44 +0200397
Tejun Heo797e7db2006-01-06 09:51:03 +0100398 BUG_ON(q->ordseq & seq);
399 q->ordseq |= seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Tejun Heo797e7db2006-01-06 09:51:03 +0100401 if (blk_ordered_cur_seq(q) != QUEUE_ORDSEQ_DONE)
402 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 /*
Tejun Heo797e7db2006-01-06 09:51:03 +0100405 * Okay, sequence complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 */
Tejun Heo797e7db2006-01-06 09:51:03 +0100407 rq = q->orig_bar_rq;
408 uptodate = q->orderr ? q->orderr : 1;
409
410 q->ordseq = 0;
411
412 end_that_request_first(rq, uptodate, rq->hard_nr_sectors);
413 end_that_request_last(rq, uptodate);
414}
415
416static void pre_flush_end_io(struct request *rq, int error)
417{
418 elv_completed_request(rq->q, rq);
419 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_PREFLUSH, error);
420}
421
422static void bar_end_io(struct request *rq, int error)
423{
424 elv_completed_request(rq->q, rq);
425 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_BAR, error);
426}
427
428static void post_flush_end_io(struct request *rq, int error)
429{
430 elv_completed_request(rq->q, rq);
431 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_POSTFLUSH, error);
432}
433
434static void queue_flush(request_queue_t *q, unsigned which)
435{
436 struct request *rq;
437 rq_end_io_fn *end_io;
438
439 if (which == QUEUE_ORDERED_PREFLUSH) {
440 rq = &q->pre_flush_rq;
441 end_io = pre_flush_end_io;
442 } else {
443 rq = &q->post_flush_rq;
444 end_io = post_flush_end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446
Tejun Heo797e7db2006-01-06 09:51:03 +0100447 rq_init(q, rq);
448 rq->flags = REQ_HARDBARRIER;
449 rq->elevator_private = NULL;
450 rq->rq_disk = q->bar_rq.rq_disk;
451 rq->rl = NULL;
452 rq->end_io = end_io;
453 q->prepare_flush_fn(q, rq);
454
455 __elv_add_request(q, rq, ELEVATOR_INSERT_FRONT, 0);
456}
457
458static inline struct request *start_ordered(request_queue_t *q,
459 struct request *rq)
460{
461 q->bi_size = 0;
462 q->orderr = 0;
463 q->ordered = q->next_ordered;
464 q->ordseq |= QUEUE_ORDSEQ_STARTED;
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 /*
Tejun Heo797e7db2006-01-06 09:51:03 +0100467 * Prep proxy barrier request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 */
Tejun Heo797e7db2006-01-06 09:51:03 +0100469 blkdev_dequeue_request(rq);
470 q->orig_bar_rq = rq;
471 rq = &q->bar_rq;
472 rq_init(q, rq);
473 rq->flags = bio_data_dir(q->orig_bar_rq->bio);
474 rq->flags |= q->ordered & QUEUE_ORDERED_FUA ? REQ_FUA : 0;
475 rq->elevator_private = NULL;
476 rq->rl = NULL;
477 init_request_from_bio(rq, q->orig_bar_rq->bio);
478 rq->end_io = bar_end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Tejun Heo797e7db2006-01-06 09:51:03 +0100480 /*
481 * Queue ordered sequence. As we stack them at the head, we
482 * need to queue in reverse order. Note that we rely on that
483 * no fs request uses ELEVATOR_INSERT_FRONT and thus no fs
484 * request gets inbetween ordered sequence.
485 */
486 if (q->ordered & QUEUE_ORDERED_POSTFLUSH)
487 queue_flush(q, QUEUE_ORDERED_POSTFLUSH);
488 else
489 q->ordseq |= QUEUE_ORDSEQ_POSTFLUSH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Tejun Heo797e7db2006-01-06 09:51:03 +0100491 __elv_add_request(q, rq, ELEVATOR_INSERT_FRONT, 0);
492
493 if (q->ordered & QUEUE_ORDERED_PREFLUSH) {
494 queue_flush(q, QUEUE_ORDERED_PREFLUSH);
495 rq = &q->pre_flush_rq;
496 } else
497 q->ordseq |= QUEUE_ORDSEQ_PREFLUSH;
498
499 if ((q->ordered & QUEUE_ORDERED_TAG) || q->in_flight == 0)
500 q->ordseq |= QUEUE_ORDSEQ_DRAIN;
501 else
502 rq = NULL;
503
504 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
Tejun Heo797e7db2006-01-06 09:51:03 +0100507int blk_do_ordered(request_queue_t *q, struct request **rqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Tejun Heo797e7db2006-01-06 09:51:03 +0100509 struct request *rq = *rqp, *allowed_rq;
510 int is_barrier = blk_fs_request(rq) && blk_barrier_rq(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Tejun Heo797e7db2006-01-06 09:51:03 +0100512 if (!q->ordseq) {
513 if (!is_barrier)
514 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Tejun Heo797e7db2006-01-06 09:51:03 +0100516 if (q->next_ordered != QUEUE_ORDERED_NONE) {
517 *rqp = start_ordered(q, rq);
518 return 1;
519 } else {
520 /*
521 * This can happen when the queue switches to
522 * ORDERED_NONE while this request is on it.
523 */
524 blkdev_dequeue_request(rq);
525 end_that_request_first(rq, -EOPNOTSUPP,
526 rq->hard_nr_sectors);
527 end_that_request_last(rq, -EOPNOTSUPP);
528 *rqp = NULL;
529 return 0;
530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Tejun Heo797e7db2006-01-06 09:51:03 +0100533 if (q->ordered & QUEUE_ORDERED_TAG) {
534 if (is_barrier && rq != &q->bar_rq)
535 *rqp = NULL;
536 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
538
Tejun Heo797e7db2006-01-06 09:51:03 +0100539 switch (blk_ordered_cur_seq(q)) {
540 case QUEUE_ORDSEQ_PREFLUSH:
541 allowed_rq = &q->pre_flush_rq;
542 break;
543 case QUEUE_ORDSEQ_BAR:
544 allowed_rq = &q->bar_rq;
545 break;
546 case QUEUE_ORDSEQ_POSTFLUSH:
547 allowed_rq = &q->post_flush_rq;
548 break;
549 default:
550 allowed_rq = NULL;
551 break;
552 }
553
554 if (rq != allowed_rq &&
555 (blk_fs_request(rq) || rq == &q->pre_flush_rq ||
556 rq == &q->post_flush_rq))
557 *rqp = NULL;
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return 1;
560}
561
Tejun Heo797e7db2006-01-06 09:51:03 +0100562static int flush_dry_bio_endio(struct bio *bio, unsigned int bytes, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Tejun Heo797e7db2006-01-06 09:51:03 +0100564 request_queue_t *q = bio->bi_private;
565 struct bio_vec *bvec;
566 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Tejun Heo797e7db2006-01-06 09:51:03 +0100568 /*
569 * This is dry run, restore bio_sector and size. We'll finish
570 * this request again with the original bi_end_io after an
571 * error occurs or post flush is complete.
572 */
573 q->bi_size += bytes;
574
575 if (bio->bi_size)
576 return 1;
577
578 /* Rewind bvec's */
579 bio->bi_idx = 0;
580 bio_for_each_segment(bvec, bio, i) {
581 bvec->bv_len += bvec->bv_offset;
582 bvec->bv_offset = 0;
583 }
584
585 /* Reset bio */
586 set_bit(BIO_UPTODATE, &bio->bi_flags);
587 bio->bi_size = q->bi_size;
588 bio->bi_sector -= (q->bi_size >> 9);
589 q->bi_size = 0;
590
591 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
Tejun Heo797e7db2006-01-06 09:51:03 +0100593
594static inline int ordered_bio_endio(struct request *rq, struct bio *bio,
595 unsigned int nbytes, int error)
596{
597 request_queue_t *q = rq->q;
598 bio_end_io_t *endio;
599 void *private;
600
601 if (&q->bar_rq != rq)
602 return 0;
603
604 /*
605 * Okay, this is the barrier request in progress, dry finish it.
606 */
607 if (error && !q->orderr)
608 q->orderr = error;
609
610 endio = bio->bi_end_io;
611 private = bio->bi_private;
612 bio->bi_end_io = flush_dry_bio_endio;
613 bio->bi_private = q;
614
615 bio_endio(bio, nbytes, error);
616
617 bio->bi_end_io = endio;
618 bio->bi_private = private;
619
620 return 1;
621}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623/**
624 * blk_queue_bounce_limit - set bounce buffer limit for queue
625 * @q: the request queue for the device
626 * @dma_addr: bus address limit
627 *
628 * Description:
629 * Different hardware can have different requirements as to what pages
630 * it can do I/O directly to. A low level driver can call
631 * blk_queue_bounce_limit to have lower memory pages allocated as bounce
632 * buffers for doing I/O to pages residing above @page. By default
633 * the block layer sets this to the highest numbered "low" memory page.
634 **/
635void blk_queue_bounce_limit(request_queue_t *q, u64 dma_addr)
636{
637 unsigned long bounce_pfn = dma_addr >> PAGE_SHIFT;
638
639 /*
640 * set appropriate bounce gfp mask -- unfortunately we don't have a
641 * full 4GB zone, so we have to resort to low memory for any bounces.
642 * ISA has its own < 16MB zone.
643 */
644 if (bounce_pfn < blk_max_low_pfn) {
645 BUG_ON(dma_addr < BLK_BOUNCE_ISA);
646 init_emergency_isa_pool();
647 q->bounce_gfp = GFP_NOIO | GFP_DMA;
648 } else
649 q->bounce_gfp = GFP_NOIO;
650
651 q->bounce_pfn = bounce_pfn;
652}
653
654EXPORT_SYMBOL(blk_queue_bounce_limit);
655
656/**
657 * blk_queue_max_sectors - set max sectors for a request for this queue
658 * @q: the request queue for the device
659 * @max_sectors: max sectors in the usual 512b unit
660 *
661 * Description:
662 * Enables a low level driver to set an upper limit on the size of
663 * received requests.
664 **/
Jens Axboe2cb2e142006-01-17 09:04:32 +0100665void blk_queue_max_sectors(request_queue_t *q, unsigned int max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
667 if ((max_sectors << 9) < PAGE_CACHE_SIZE) {
668 max_sectors = 1 << (PAGE_CACHE_SHIFT - 9);
669 printk("%s: set to minimum %d\n", __FUNCTION__, max_sectors);
670 }
671
Mike Christiedefd94b2005-12-05 02:37:06 -0600672 if (BLK_DEF_MAX_SECTORS > max_sectors)
673 q->max_hw_sectors = q->max_sectors = max_sectors;
674 else {
675 q->max_sectors = BLK_DEF_MAX_SECTORS;
676 q->max_hw_sectors = max_sectors;
677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
680EXPORT_SYMBOL(blk_queue_max_sectors);
681
682/**
683 * blk_queue_max_phys_segments - set max phys segments for a request for this queue
684 * @q: the request queue for the device
685 * @max_segments: max number of segments
686 *
687 * Description:
688 * Enables a low level driver to set an upper limit on the number of
689 * physical data segments in a request. This would be the largest sized
690 * scatter list the driver could handle.
691 **/
692void blk_queue_max_phys_segments(request_queue_t *q, unsigned short max_segments)
693{
694 if (!max_segments) {
695 max_segments = 1;
696 printk("%s: set to minimum %d\n", __FUNCTION__, max_segments);
697 }
698
699 q->max_phys_segments = max_segments;
700}
701
702EXPORT_SYMBOL(blk_queue_max_phys_segments);
703
704/**
705 * blk_queue_max_hw_segments - set max hw segments for a request for this queue
706 * @q: the request queue for the device
707 * @max_segments: max number of segments
708 *
709 * Description:
710 * Enables a low level driver to set an upper limit on the number of
711 * hw data segments in a request. This would be the largest number of
712 * address/length pairs the host adapter can actually give as once
713 * to the device.
714 **/
715void blk_queue_max_hw_segments(request_queue_t *q, unsigned short max_segments)
716{
717 if (!max_segments) {
718 max_segments = 1;
719 printk("%s: set to minimum %d\n", __FUNCTION__, max_segments);
720 }
721
722 q->max_hw_segments = max_segments;
723}
724
725EXPORT_SYMBOL(blk_queue_max_hw_segments);
726
727/**
728 * blk_queue_max_segment_size - set max segment size for blk_rq_map_sg
729 * @q: the request queue for the device
730 * @max_size: max size of segment in bytes
731 *
732 * Description:
733 * Enables a low level driver to set an upper limit on the size of a
734 * coalesced segment
735 **/
736void blk_queue_max_segment_size(request_queue_t *q, unsigned int max_size)
737{
738 if (max_size < PAGE_CACHE_SIZE) {
739 max_size = PAGE_CACHE_SIZE;
740 printk("%s: set to minimum %d\n", __FUNCTION__, max_size);
741 }
742
743 q->max_segment_size = max_size;
744}
745
746EXPORT_SYMBOL(blk_queue_max_segment_size);
747
748/**
749 * blk_queue_hardsect_size - set hardware sector size for the queue
750 * @q: the request queue for the device
751 * @size: the hardware sector size, in bytes
752 *
753 * Description:
754 * This should typically be set to the lowest possible sector size
755 * that the hardware can operate on (possible without reverting to
756 * even internal read-modify-write operations). Usually the default
757 * of 512 covers most hardware.
758 **/
759void blk_queue_hardsect_size(request_queue_t *q, unsigned short size)
760{
761 q->hardsect_size = size;
762}
763
764EXPORT_SYMBOL(blk_queue_hardsect_size);
765
766/*
767 * Returns the minimum that is _not_ zero, unless both are zero.
768 */
769#define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
770
771/**
772 * blk_queue_stack_limits - inherit underlying queue limits for stacked drivers
773 * @t: the stacking driver (top)
774 * @b: the underlying device (bottom)
775 **/
776void blk_queue_stack_limits(request_queue_t *t, request_queue_t *b)
777{
778 /* zero is "infinity" */
Mike Christiedefd94b2005-12-05 02:37:06 -0600779 t->max_sectors = min_not_zero(t->max_sectors,b->max_sectors);
780 t->max_hw_sectors = min_not_zero(t->max_hw_sectors,b->max_hw_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 t->max_phys_segments = min(t->max_phys_segments,b->max_phys_segments);
783 t->max_hw_segments = min(t->max_hw_segments,b->max_hw_segments);
784 t->max_segment_size = min(t->max_segment_size,b->max_segment_size);
785 t->hardsect_size = max(t->hardsect_size,b->hardsect_size);
786}
787
788EXPORT_SYMBOL(blk_queue_stack_limits);
789
790/**
791 * blk_queue_segment_boundary - set boundary rules for segment merging
792 * @q: the request queue for the device
793 * @mask: the memory boundary mask
794 **/
795void blk_queue_segment_boundary(request_queue_t *q, unsigned long mask)
796{
797 if (mask < PAGE_CACHE_SIZE - 1) {
798 mask = PAGE_CACHE_SIZE - 1;
799 printk("%s: set to minimum %lx\n", __FUNCTION__, mask);
800 }
801
802 q->seg_boundary_mask = mask;
803}
804
805EXPORT_SYMBOL(blk_queue_segment_boundary);
806
807/**
808 * blk_queue_dma_alignment - set dma length and memory alignment
809 * @q: the request queue for the device
810 * @mask: alignment mask
811 *
812 * description:
813 * set required memory and length aligment for direct dma transactions.
814 * this is used when buiding direct io requests for the queue.
815 *
816 **/
817void blk_queue_dma_alignment(request_queue_t *q, int mask)
818{
819 q->dma_alignment = mask;
820}
821
822EXPORT_SYMBOL(blk_queue_dma_alignment);
823
824/**
825 * blk_queue_find_tag - find a request by its tag and queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 * @q: The request queue for the device
827 * @tag: The tag of the request
828 *
829 * Notes:
830 * Should be used when a device returns a tag and you want to match
831 * it with a request.
832 *
833 * no locks need be held.
834 **/
835struct request *blk_queue_find_tag(request_queue_t *q, int tag)
836{
837 struct blk_queue_tag *bqt = q->queue_tags;
838
Tejun Heoba025082005-08-05 13:28:11 -0700839 if (unlikely(bqt == NULL || tag >= bqt->real_max_depth))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return NULL;
841
842 return bqt->tag_index[tag];
843}
844
845EXPORT_SYMBOL(blk_queue_find_tag);
846
847/**
848 * __blk_queue_free_tags - release tag maintenance info
849 * @q: the request queue for the device
850 *
851 * Notes:
852 * blk_cleanup_queue() will take care of calling this function, if tagging
853 * has been used. So there's no need to call this directly.
854 **/
855static void __blk_queue_free_tags(request_queue_t *q)
856{
857 struct blk_queue_tag *bqt = q->queue_tags;
858
859 if (!bqt)
860 return;
861
862 if (atomic_dec_and_test(&bqt->refcnt)) {
863 BUG_ON(bqt->busy);
864 BUG_ON(!list_empty(&bqt->busy_list));
865
866 kfree(bqt->tag_index);
867 bqt->tag_index = NULL;
868
869 kfree(bqt->tag_map);
870 bqt->tag_map = NULL;
871
872 kfree(bqt);
873 }
874
875 q->queue_tags = NULL;
876 q->queue_flags &= ~(1 << QUEUE_FLAG_QUEUED);
877}
878
879/**
880 * blk_queue_free_tags - release tag maintenance info
881 * @q: the request queue for the device
882 *
883 * Notes:
884 * This is used to disabled tagged queuing to a device, yet leave
885 * queue in function.
886 **/
887void blk_queue_free_tags(request_queue_t *q)
888{
889 clear_bit(QUEUE_FLAG_QUEUED, &q->queue_flags);
890}
891
892EXPORT_SYMBOL(blk_queue_free_tags);
893
894static int
895init_tag_map(request_queue_t *q, struct blk_queue_tag *tags, int depth)
896{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 struct request **tag_index;
898 unsigned long *tag_map;
Tejun Heofa72b902005-06-23 00:08:49 -0700899 int nr_ulongs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 if (depth > q->nr_requests * 2) {
902 depth = q->nr_requests * 2;
903 printk(KERN_ERR "%s: adjusted depth to %d\n",
904 __FUNCTION__, depth);
905 }
906
907 tag_index = kmalloc(depth * sizeof(struct request *), GFP_ATOMIC);
908 if (!tag_index)
909 goto fail;
910
Tejun Heof7d37d02005-06-23 00:08:50 -0700911 nr_ulongs = ALIGN(depth, BITS_PER_LONG) / BITS_PER_LONG;
Tejun Heofa72b902005-06-23 00:08:49 -0700912 tag_map = kmalloc(nr_ulongs * sizeof(unsigned long), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (!tag_map)
914 goto fail;
915
916 memset(tag_index, 0, depth * sizeof(struct request *));
Tejun Heofa72b902005-06-23 00:08:49 -0700917 memset(tag_map, 0, nr_ulongs * sizeof(unsigned long));
Tejun Heoba025082005-08-05 13:28:11 -0700918 tags->real_max_depth = depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 tags->max_depth = depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 tags->tag_index = tag_index;
921 tags->tag_map = tag_map;
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return 0;
924fail:
925 kfree(tag_index);
926 return -ENOMEM;
927}
928
929/**
930 * blk_queue_init_tags - initialize the queue tag info
931 * @q: the request queue for the device
932 * @depth: the maximum queue depth supported
933 * @tags: the tag to use
934 **/
935int blk_queue_init_tags(request_queue_t *q, int depth,
936 struct blk_queue_tag *tags)
937{
938 int rc;
939
940 BUG_ON(tags && q->queue_tags && tags != q->queue_tags);
941
942 if (!tags && !q->queue_tags) {
943 tags = kmalloc(sizeof(struct blk_queue_tag), GFP_ATOMIC);
944 if (!tags)
945 goto fail;
946
947 if (init_tag_map(q, tags, depth))
948 goto fail;
949
950 INIT_LIST_HEAD(&tags->busy_list);
951 tags->busy = 0;
952 atomic_set(&tags->refcnt, 1);
953 } else if (q->queue_tags) {
954 if ((rc = blk_queue_resize_tags(q, depth)))
955 return rc;
956 set_bit(QUEUE_FLAG_QUEUED, &q->queue_flags);
957 return 0;
958 } else
959 atomic_inc(&tags->refcnt);
960
961 /*
962 * assign it, all done
963 */
964 q->queue_tags = tags;
965 q->queue_flags |= (1 << QUEUE_FLAG_QUEUED);
966 return 0;
967fail:
968 kfree(tags);
969 return -ENOMEM;
970}
971
972EXPORT_SYMBOL(blk_queue_init_tags);
973
974/**
975 * blk_queue_resize_tags - change the queueing depth
976 * @q: the request queue for the device
977 * @new_depth: the new max command queueing depth
978 *
979 * Notes:
980 * Must be called with the queue lock held.
981 **/
982int blk_queue_resize_tags(request_queue_t *q, int new_depth)
983{
984 struct blk_queue_tag *bqt = q->queue_tags;
985 struct request **tag_index;
986 unsigned long *tag_map;
Tejun Heofa72b902005-06-23 00:08:49 -0700987 int max_depth, nr_ulongs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 if (!bqt)
990 return -ENXIO;
991
992 /*
Tejun Heoba025082005-08-05 13:28:11 -0700993 * if we already have large enough real_max_depth. just
994 * adjust max_depth. *NOTE* as requests with tag value
995 * between new_depth and real_max_depth can be in-flight, tag
996 * map can not be shrunk blindly here.
997 */
998 if (new_depth <= bqt->real_max_depth) {
999 bqt->max_depth = new_depth;
1000 return 0;
1001 }
1002
1003 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 * save the old state info, so we can copy it back
1005 */
1006 tag_index = bqt->tag_index;
1007 tag_map = bqt->tag_map;
Tejun Heoba025082005-08-05 13:28:11 -07001008 max_depth = bqt->real_max_depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 if (init_tag_map(q, bqt, new_depth))
1011 return -ENOMEM;
1012
1013 memcpy(bqt->tag_index, tag_index, max_depth * sizeof(struct request *));
Tejun Heof7d37d02005-06-23 00:08:50 -07001014 nr_ulongs = ALIGN(max_depth, BITS_PER_LONG) / BITS_PER_LONG;
Tejun Heofa72b902005-06-23 00:08:49 -07001015 memcpy(bqt->tag_map, tag_map, nr_ulongs * sizeof(unsigned long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 kfree(tag_index);
1018 kfree(tag_map);
1019 return 0;
1020}
1021
1022EXPORT_SYMBOL(blk_queue_resize_tags);
1023
1024/**
1025 * blk_queue_end_tag - end tag operations for a request
1026 * @q: the request queue for the device
1027 * @rq: the request that has completed
1028 *
1029 * Description:
1030 * Typically called when end_that_request_first() returns 0, meaning
1031 * all transfers have been done for a request. It's important to call
1032 * this function before end_that_request_last(), as that will put the
1033 * request back on the free list thus corrupting the internal tag list.
1034 *
1035 * Notes:
1036 * queue lock must be held.
1037 **/
1038void blk_queue_end_tag(request_queue_t *q, struct request *rq)
1039{
1040 struct blk_queue_tag *bqt = q->queue_tags;
1041 int tag = rq->tag;
1042
1043 BUG_ON(tag == -1);
1044
Tejun Heoba025082005-08-05 13:28:11 -07001045 if (unlikely(tag >= bqt->real_max_depth))
Tejun Heo040c9282005-06-23 00:08:51 -07001046 /*
1047 * This can happen after tag depth has been reduced.
1048 * FIXME: how about a warning or info message here?
1049 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 return;
1051
1052 if (unlikely(!__test_and_clear_bit(tag, bqt->tag_map))) {
Tejun Heo040c9282005-06-23 00:08:51 -07001053 printk(KERN_ERR "%s: attempt to clear non-busy tag (%d)\n",
1054 __FUNCTION__, tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 return;
1056 }
1057
1058 list_del_init(&rq->queuelist);
1059 rq->flags &= ~REQ_QUEUED;
1060 rq->tag = -1;
1061
1062 if (unlikely(bqt->tag_index[tag] == NULL))
Tejun Heo040c9282005-06-23 00:08:51 -07001063 printk(KERN_ERR "%s: tag %d is missing\n",
1064 __FUNCTION__, tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 bqt->tag_index[tag] = NULL;
1067 bqt->busy--;
1068}
1069
1070EXPORT_SYMBOL(blk_queue_end_tag);
1071
1072/**
1073 * blk_queue_start_tag - find a free tag and assign it
1074 * @q: the request queue for the device
1075 * @rq: the block request that needs tagging
1076 *
1077 * Description:
1078 * This can either be used as a stand-alone helper, or possibly be
1079 * assigned as the queue &prep_rq_fn (in which case &struct request
1080 * automagically gets a tag assigned). Note that this function
1081 * assumes that any type of request can be queued! if this is not
1082 * true for your device, you must check the request type before
1083 * calling this function. The request will also be removed from
1084 * the request queue, so it's the drivers responsibility to readd
1085 * it if it should need to be restarted for some reason.
1086 *
1087 * Notes:
1088 * queue lock must be held.
1089 **/
1090int blk_queue_start_tag(request_queue_t *q, struct request *rq)
1091{
1092 struct blk_queue_tag *bqt = q->queue_tags;
Tejun Heo2bf0fdad2005-06-23 00:08:48 -07001093 int tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095 if (unlikely((rq->flags & REQ_QUEUED))) {
1096 printk(KERN_ERR
Tejun Heo040c9282005-06-23 00:08:51 -07001097 "%s: request %p for device [%s] already tagged %d",
1098 __FUNCTION__, rq,
1099 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 BUG();
1101 }
1102
Tejun Heo2bf0fdad2005-06-23 00:08:48 -07001103 tag = find_first_zero_bit(bqt->tag_map, bqt->max_depth);
1104 if (tag >= bqt->max_depth)
1105 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 __set_bit(tag, bqt->tag_map);
1108
1109 rq->flags |= REQ_QUEUED;
1110 rq->tag = tag;
1111 bqt->tag_index[tag] = rq;
1112 blkdev_dequeue_request(rq);
1113 list_add(&rq->queuelist, &bqt->busy_list);
1114 bqt->busy++;
1115 return 0;
1116}
1117
1118EXPORT_SYMBOL(blk_queue_start_tag);
1119
1120/**
1121 * blk_queue_invalidate_tags - invalidate all pending tags
1122 * @q: the request queue for the device
1123 *
1124 * Description:
1125 * Hardware conditions may dictate a need to stop all pending requests.
1126 * In this case, we will safely clear the block side of the tag queue and
1127 * readd all requests to the request queue in the right order.
1128 *
1129 * Notes:
1130 * queue lock must be held.
1131 **/
1132void blk_queue_invalidate_tags(request_queue_t *q)
1133{
1134 struct blk_queue_tag *bqt = q->queue_tags;
1135 struct list_head *tmp, *n;
1136 struct request *rq;
1137
1138 list_for_each_safe(tmp, n, &bqt->busy_list) {
1139 rq = list_entry_rq(tmp);
1140
1141 if (rq->tag == -1) {
Tejun Heo040c9282005-06-23 00:08:51 -07001142 printk(KERN_ERR
1143 "%s: bad tag found on list\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 list_del_init(&rq->queuelist);
1145 rq->flags &= ~REQ_QUEUED;
1146 } else
1147 blk_queue_end_tag(q, rq);
1148
1149 rq->flags &= ~REQ_STARTED;
1150 __elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 0);
1151 }
1152}
1153
1154EXPORT_SYMBOL(blk_queue_invalidate_tags);
1155
Arjan van de Ven64100092006-01-06 09:46:02 +01001156static const char * const rq_flags[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 "REQ_RW",
1158 "REQ_FAILFAST",
Tejun Heo8922e162005-10-20 16:23:44 +02001159 "REQ_SORTED",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 "REQ_SOFTBARRIER",
1161 "REQ_HARDBARRIER",
Tejun Heo797e7db2006-01-06 09:51:03 +01001162 "REQ_FUA",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 "REQ_CMD",
1164 "REQ_NOMERGE",
1165 "REQ_STARTED",
1166 "REQ_DONTPREP",
1167 "REQ_QUEUED",
Tejun Heocb98fc82005-10-28 08:29:39 +02001168 "REQ_ELVPRIV",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 "REQ_PC",
1170 "REQ_BLOCK_PC",
1171 "REQ_SENSE",
1172 "REQ_FAILED",
1173 "REQ_QUIET",
1174 "REQ_SPECIAL",
1175 "REQ_DRIVE_CMD",
1176 "REQ_DRIVE_TASK",
1177 "REQ_DRIVE_TASKFILE",
1178 "REQ_PREEMPT",
1179 "REQ_PM_SUSPEND",
1180 "REQ_PM_RESUME",
1181 "REQ_PM_SHUTDOWN",
Tejun Heo797e7db2006-01-06 09:51:03 +01001182 "REQ_ORDERED_COLOR",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183};
1184
1185void blk_dump_rq_flags(struct request *rq, char *msg)
1186{
1187 int bit;
1188
1189 printk("%s: dev %s: flags = ", msg,
1190 rq->rq_disk ? rq->rq_disk->disk_name : "?");
1191 bit = 0;
1192 do {
1193 if (rq->flags & (1 << bit))
1194 printk("%s ", rq_flags[bit]);
1195 bit++;
1196 } while (bit < __REQ_NR_BITS);
1197
1198 printk("\nsector %llu, nr/cnr %lu/%u\n", (unsigned long long)rq->sector,
1199 rq->nr_sectors,
1200 rq->current_nr_sectors);
1201 printk("bio %p, biotail %p, buffer %p, data %p, len %u\n", rq->bio, rq->biotail, rq->buffer, rq->data, rq->data_len);
1202
1203 if (rq->flags & (REQ_BLOCK_PC | REQ_PC)) {
1204 printk("cdb: ");
1205 for (bit = 0; bit < sizeof(rq->cmd); bit++)
1206 printk("%02x ", rq->cmd[bit]);
1207 printk("\n");
1208 }
1209}
1210
1211EXPORT_SYMBOL(blk_dump_rq_flags);
1212
1213void blk_recount_segments(request_queue_t *q, struct bio *bio)
1214{
1215 struct bio_vec *bv, *bvprv = NULL;
1216 int i, nr_phys_segs, nr_hw_segs, seg_size, hw_seg_size, cluster;
1217 int high, highprv = 1;
1218
1219 if (unlikely(!bio->bi_io_vec))
1220 return;
1221
1222 cluster = q->queue_flags & (1 << QUEUE_FLAG_CLUSTER);
1223 hw_seg_size = seg_size = nr_phys_segs = nr_hw_segs = 0;
1224 bio_for_each_segment(bv, bio, i) {
1225 /*
1226 * the trick here is making sure that a high page is never
1227 * considered part of another segment, since that might
1228 * change with the bounce page.
1229 */
1230 high = page_to_pfn(bv->bv_page) >= q->bounce_pfn;
1231 if (high || highprv)
1232 goto new_hw_segment;
1233 if (cluster) {
1234 if (seg_size + bv->bv_len > q->max_segment_size)
1235 goto new_segment;
1236 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bv))
1237 goto new_segment;
1238 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bv))
1239 goto new_segment;
1240 if (BIOVEC_VIRT_OVERSIZE(hw_seg_size + bv->bv_len))
1241 goto new_hw_segment;
1242
1243 seg_size += bv->bv_len;
1244 hw_seg_size += bv->bv_len;
1245 bvprv = bv;
1246 continue;
1247 }
1248new_segment:
1249 if (BIOVEC_VIRT_MERGEABLE(bvprv, bv) &&
1250 !BIOVEC_VIRT_OVERSIZE(hw_seg_size + bv->bv_len)) {
1251 hw_seg_size += bv->bv_len;
1252 } else {
1253new_hw_segment:
1254 if (hw_seg_size > bio->bi_hw_front_size)
1255 bio->bi_hw_front_size = hw_seg_size;
1256 hw_seg_size = BIOVEC_VIRT_START_SIZE(bv) + bv->bv_len;
1257 nr_hw_segs++;
1258 }
1259
1260 nr_phys_segs++;
1261 bvprv = bv;
1262 seg_size = bv->bv_len;
1263 highprv = high;
1264 }
1265 if (hw_seg_size > bio->bi_hw_back_size)
1266 bio->bi_hw_back_size = hw_seg_size;
1267 if (nr_hw_segs == 1 && hw_seg_size > bio->bi_hw_front_size)
1268 bio->bi_hw_front_size = hw_seg_size;
1269 bio->bi_phys_segments = nr_phys_segs;
1270 bio->bi_hw_segments = nr_hw_segs;
1271 bio->bi_flags |= (1 << BIO_SEG_VALID);
1272}
1273
1274
Adrian Bunk93d17d32005-06-25 14:59:10 -07001275static int blk_phys_contig_segment(request_queue_t *q, struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 struct bio *nxt)
1277{
1278 if (!(q->queue_flags & (1 << QUEUE_FLAG_CLUSTER)))
1279 return 0;
1280
1281 if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)))
1282 return 0;
1283 if (bio->bi_size + nxt->bi_size > q->max_segment_size)
1284 return 0;
1285
1286 /*
1287 * bio and nxt are contigous in memory, check if the queue allows
1288 * these two to be merged into one
1289 */
1290 if (BIO_SEG_BOUNDARY(q, bio, nxt))
1291 return 1;
1292
1293 return 0;
1294}
1295
Adrian Bunk93d17d32005-06-25 14:59:10 -07001296static int blk_hw_contig_segment(request_queue_t *q, struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 struct bio *nxt)
1298{
1299 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
1300 blk_recount_segments(q, bio);
1301 if (unlikely(!bio_flagged(nxt, BIO_SEG_VALID)))
1302 blk_recount_segments(q, nxt);
1303 if (!BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)) ||
1304 BIOVEC_VIRT_OVERSIZE(bio->bi_hw_front_size + bio->bi_hw_back_size))
1305 return 0;
1306 if (bio->bi_size + nxt->bi_size > q->max_segment_size)
1307 return 0;
1308
1309 return 1;
1310}
1311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312/*
1313 * map a request to scatterlist, return number of sg entries setup. Caller
1314 * must make sure sg can hold rq->nr_phys_segments entries
1315 */
1316int blk_rq_map_sg(request_queue_t *q, struct request *rq, struct scatterlist *sg)
1317{
1318 struct bio_vec *bvec, *bvprv;
1319 struct bio *bio;
1320 int nsegs, i, cluster;
1321
1322 nsegs = 0;
1323 cluster = q->queue_flags & (1 << QUEUE_FLAG_CLUSTER);
1324
1325 /*
1326 * for each bio in rq
1327 */
1328 bvprv = NULL;
1329 rq_for_each_bio(bio, rq) {
1330 /*
1331 * for each segment in bio
1332 */
1333 bio_for_each_segment(bvec, bio, i) {
1334 int nbytes = bvec->bv_len;
1335
1336 if (bvprv && cluster) {
1337 if (sg[nsegs - 1].length + nbytes > q->max_segment_size)
1338 goto new_segment;
1339
1340 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
1341 goto new_segment;
1342 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
1343 goto new_segment;
1344
1345 sg[nsegs - 1].length += nbytes;
1346 } else {
1347new_segment:
1348 memset(&sg[nsegs],0,sizeof(struct scatterlist));
1349 sg[nsegs].page = bvec->bv_page;
1350 sg[nsegs].length = nbytes;
1351 sg[nsegs].offset = bvec->bv_offset;
1352
1353 nsegs++;
1354 }
1355 bvprv = bvec;
1356 } /* segments in bio */
1357 } /* bios in rq */
1358
1359 return nsegs;
1360}
1361
1362EXPORT_SYMBOL(blk_rq_map_sg);
1363
1364/*
1365 * the standard queue merge functions, can be overridden with device
1366 * specific ones if so desired
1367 */
1368
1369static inline int ll_new_mergeable(request_queue_t *q,
1370 struct request *req,
1371 struct bio *bio)
1372{
1373 int nr_phys_segs = bio_phys_segments(q, bio);
1374
1375 if (req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
1376 req->flags |= REQ_NOMERGE;
1377 if (req == q->last_merge)
1378 q->last_merge = NULL;
1379 return 0;
1380 }
1381
1382 /*
1383 * A hw segment is just getting larger, bump just the phys
1384 * counter.
1385 */
1386 req->nr_phys_segments += nr_phys_segs;
1387 return 1;
1388}
1389
1390static inline int ll_new_hw_segment(request_queue_t *q,
1391 struct request *req,
1392 struct bio *bio)
1393{
1394 int nr_hw_segs = bio_hw_segments(q, bio);
1395 int nr_phys_segs = bio_phys_segments(q, bio);
1396
1397 if (req->nr_hw_segments + nr_hw_segs > q->max_hw_segments
1398 || req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
1399 req->flags |= REQ_NOMERGE;
1400 if (req == q->last_merge)
1401 q->last_merge = NULL;
1402 return 0;
1403 }
1404
1405 /*
1406 * This will form the start of a new hw segment. Bump both
1407 * counters.
1408 */
1409 req->nr_hw_segments += nr_hw_segs;
1410 req->nr_phys_segments += nr_phys_segs;
1411 return 1;
1412}
1413
1414static int ll_back_merge_fn(request_queue_t *q, struct request *req,
1415 struct bio *bio)
1416{
Mike Christiedefd94b2005-12-05 02:37:06 -06001417 unsigned short max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 int len;
1419
Mike Christiedefd94b2005-12-05 02:37:06 -06001420 if (unlikely(blk_pc_request(req)))
1421 max_sectors = q->max_hw_sectors;
1422 else
1423 max_sectors = q->max_sectors;
1424
1425 if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 req->flags |= REQ_NOMERGE;
1427 if (req == q->last_merge)
1428 q->last_merge = NULL;
1429 return 0;
1430 }
1431 if (unlikely(!bio_flagged(req->biotail, BIO_SEG_VALID)))
1432 blk_recount_segments(q, req->biotail);
1433 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
1434 blk_recount_segments(q, bio);
1435 len = req->biotail->bi_hw_back_size + bio->bi_hw_front_size;
1436 if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(req->biotail), __BVEC_START(bio)) &&
1437 !BIOVEC_VIRT_OVERSIZE(len)) {
1438 int mergeable = ll_new_mergeable(q, req, bio);
1439
1440 if (mergeable) {
1441 if (req->nr_hw_segments == 1)
1442 req->bio->bi_hw_front_size = len;
1443 if (bio->bi_hw_segments == 1)
1444 bio->bi_hw_back_size = len;
1445 }
1446 return mergeable;
1447 }
1448
1449 return ll_new_hw_segment(q, req, bio);
1450}
1451
1452static int ll_front_merge_fn(request_queue_t *q, struct request *req,
1453 struct bio *bio)
1454{
Mike Christiedefd94b2005-12-05 02:37:06 -06001455 unsigned short max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 int len;
1457
Mike Christiedefd94b2005-12-05 02:37:06 -06001458 if (unlikely(blk_pc_request(req)))
1459 max_sectors = q->max_hw_sectors;
1460 else
1461 max_sectors = q->max_sectors;
1462
1463
1464 if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 req->flags |= REQ_NOMERGE;
1466 if (req == q->last_merge)
1467 q->last_merge = NULL;
1468 return 0;
1469 }
1470 len = bio->bi_hw_back_size + req->bio->bi_hw_front_size;
1471 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
1472 blk_recount_segments(q, bio);
1473 if (unlikely(!bio_flagged(req->bio, BIO_SEG_VALID)))
1474 blk_recount_segments(q, req->bio);
1475 if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(req->bio)) &&
1476 !BIOVEC_VIRT_OVERSIZE(len)) {
1477 int mergeable = ll_new_mergeable(q, req, bio);
1478
1479 if (mergeable) {
1480 if (bio->bi_hw_segments == 1)
1481 bio->bi_hw_front_size = len;
1482 if (req->nr_hw_segments == 1)
1483 req->biotail->bi_hw_back_size = len;
1484 }
1485 return mergeable;
1486 }
1487
1488 return ll_new_hw_segment(q, req, bio);
1489}
1490
1491static int ll_merge_requests_fn(request_queue_t *q, struct request *req,
1492 struct request *next)
1493{
Nikita Danilovdfa1a552005-06-25 14:59:20 -07001494 int total_phys_segments;
1495 int total_hw_segments;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497 /*
1498 * First check if the either of the requests are re-queued
1499 * requests. Can't merge them if they are.
1500 */
1501 if (req->special || next->special)
1502 return 0;
1503
1504 /*
Nikita Danilovdfa1a552005-06-25 14:59:20 -07001505 * Will it become too large?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 */
1507 if ((req->nr_sectors + next->nr_sectors) > q->max_sectors)
1508 return 0;
1509
1510 total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
1511 if (blk_phys_contig_segment(q, req->biotail, next->bio))
1512 total_phys_segments--;
1513
1514 if (total_phys_segments > q->max_phys_segments)
1515 return 0;
1516
1517 total_hw_segments = req->nr_hw_segments + next->nr_hw_segments;
1518 if (blk_hw_contig_segment(q, req->biotail, next->bio)) {
1519 int len = req->biotail->bi_hw_back_size + next->bio->bi_hw_front_size;
1520 /*
1521 * propagate the combined length to the end of the requests
1522 */
1523 if (req->nr_hw_segments == 1)
1524 req->bio->bi_hw_front_size = len;
1525 if (next->nr_hw_segments == 1)
1526 next->biotail->bi_hw_back_size = len;
1527 total_hw_segments--;
1528 }
1529
1530 if (total_hw_segments > q->max_hw_segments)
1531 return 0;
1532
1533 /* Merge is OK... */
1534 req->nr_phys_segments = total_phys_segments;
1535 req->nr_hw_segments = total_hw_segments;
1536 return 1;
1537}
1538
1539/*
1540 * "plug" the device if there are no outstanding requests: this will
1541 * force the transfer to start only after we have put all the requests
1542 * on the list.
1543 *
1544 * This is called with interrupts off and no requests on the queue and
1545 * with the queue lock held.
1546 */
1547void blk_plug_device(request_queue_t *q)
1548{
1549 WARN_ON(!irqs_disabled());
1550
1551 /*
1552 * don't plug a stopped queue, it must be paired with blk_start_queue()
1553 * which will restart the queueing
1554 */
1555 if (test_bit(QUEUE_FLAG_STOPPED, &q->queue_flags))
1556 return;
1557
1558 if (!test_and_set_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags))
1559 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
1560}
1561
1562EXPORT_SYMBOL(blk_plug_device);
1563
1564/*
1565 * remove the queue from the plugged list, if present. called with
1566 * queue lock held and interrupts disabled.
1567 */
1568int blk_remove_plug(request_queue_t *q)
1569{
1570 WARN_ON(!irqs_disabled());
1571
1572 if (!test_and_clear_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags))
1573 return 0;
1574
1575 del_timer(&q->unplug_timer);
1576 return 1;
1577}
1578
1579EXPORT_SYMBOL(blk_remove_plug);
1580
1581/*
1582 * remove the plug and let it rip..
1583 */
1584void __generic_unplug_device(request_queue_t *q)
1585{
Nick Pigginfde6ad22005-06-23 00:08:53 -07001586 if (unlikely(test_bit(QUEUE_FLAG_STOPPED, &q->queue_flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 return;
1588
1589 if (!blk_remove_plug(q))
1590 return;
1591
Jens Axboe22e2c502005-06-27 10:55:12 +02001592 q->request_fn(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593}
1594EXPORT_SYMBOL(__generic_unplug_device);
1595
1596/**
1597 * generic_unplug_device - fire a request queue
1598 * @q: The &request_queue_t in question
1599 *
1600 * Description:
1601 * Linux uses plugging to build bigger requests queues before letting
1602 * the device have at them. If a queue is plugged, the I/O scheduler
1603 * is still adding and merging requests on the queue. Once the queue
1604 * gets unplugged, the request_fn defined for the queue is invoked and
1605 * transfers started.
1606 **/
1607void generic_unplug_device(request_queue_t *q)
1608{
1609 spin_lock_irq(q->queue_lock);
1610 __generic_unplug_device(q);
1611 spin_unlock_irq(q->queue_lock);
1612}
1613EXPORT_SYMBOL(generic_unplug_device);
1614
1615static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
1616 struct page *page)
1617{
1618 request_queue_t *q = bdi->unplug_io_data;
1619
1620 /*
1621 * devices don't necessarily have an ->unplug_fn defined
1622 */
1623 if (q->unplug_fn)
1624 q->unplug_fn(q);
1625}
1626
1627static void blk_unplug_work(void *data)
1628{
1629 request_queue_t *q = data;
1630
1631 q->unplug_fn(q);
1632}
1633
1634static void blk_unplug_timeout(unsigned long data)
1635{
1636 request_queue_t *q = (request_queue_t *)data;
1637
1638 kblockd_schedule_work(&q->unplug_work);
1639}
1640
1641/**
1642 * blk_start_queue - restart a previously stopped queue
1643 * @q: The &request_queue_t in question
1644 *
1645 * Description:
1646 * blk_start_queue() will clear the stop flag on the queue, and call
1647 * the request_fn for the queue if it was in a stopped state when
1648 * entered. Also see blk_stop_queue(). Queue lock must be held.
1649 **/
1650void blk_start_queue(request_queue_t *q)
1651{
1652 clear_bit(QUEUE_FLAG_STOPPED, &q->queue_flags);
1653
1654 /*
1655 * one level of recursion is ok and is much faster than kicking
1656 * the unplug handling
1657 */
1658 if (!test_and_set_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) {
1659 q->request_fn(q);
1660 clear_bit(QUEUE_FLAG_REENTER, &q->queue_flags);
1661 } else {
1662 blk_plug_device(q);
1663 kblockd_schedule_work(&q->unplug_work);
1664 }
1665}
1666
1667EXPORT_SYMBOL(blk_start_queue);
1668
1669/**
1670 * blk_stop_queue - stop a queue
1671 * @q: The &request_queue_t in question
1672 *
1673 * Description:
1674 * The Linux block layer assumes that a block driver will consume all
1675 * entries on the request queue when the request_fn strategy is called.
1676 * Often this will not happen, because of hardware limitations (queue
1677 * depth settings). If a device driver gets a 'queue full' response,
1678 * or if it simply chooses not to queue more I/O at one point, it can
1679 * call this function to prevent the request_fn from being called until
1680 * the driver has signalled it's ready to go again. This happens by calling
1681 * blk_start_queue() to restart queue operations. Queue lock must be held.
1682 **/
1683void blk_stop_queue(request_queue_t *q)
1684{
1685 blk_remove_plug(q);
1686 set_bit(QUEUE_FLAG_STOPPED, &q->queue_flags);
1687}
1688EXPORT_SYMBOL(blk_stop_queue);
1689
1690/**
1691 * blk_sync_queue - cancel any pending callbacks on a queue
1692 * @q: the queue
1693 *
1694 * Description:
1695 * The block layer may perform asynchronous callback activity
1696 * on a queue, such as calling the unplug function after a timeout.
1697 * A block device may call blk_sync_queue to ensure that any
1698 * such activity is cancelled, thus allowing it to release resources
1699 * the the callbacks might use. The caller must already have made sure
1700 * that its ->make_request_fn will not re-add plugging prior to calling
1701 * this function.
1702 *
1703 */
1704void blk_sync_queue(struct request_queue *q)
1705{
1706 del_timer_sync(&q->unplug_timer);
1707 kblockd_flush();
1708}
1709EXPORT_SYMBOL(blk_sync_queue);
1710
1711/**
1712 * blk_run_queue - run a single device queue
1713 * @q: The queue to run
1714 */
1715void blk_run_queue(struct request_queue *q)
1716{
1717 unsigned long flags;
1718
1719 spin_lock_irqsave(q->queue_lock, flags);
1720 blk_remove_plug(q);
Ken Chena2997382005-04-16 15:25:43 -07001721 if (!elv_queue_empty(q))
1722 q->request_fn(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 spin_unlock_irqrestore(q->queue_lock, flags);
1724}
1725EXPORT_SYMBOL(blk_run_queue);
1726
1727/**
1728 * blk_cleanup_queue: - release a &request_queue_t when it is no longer needed
1729 * @q: the request queue to be released
1730 *
1731 * Description:
1732 * blk_cleanup_queue is the pair to blk_init_queue() or
1733 * blk_queue_make_request(). It should be called when a request queue is
1734 * being released; typically when a block device is being de-registered.
1735 * Currently, its primary task it to free all the &struct request
1736 * structures that were allocated to the queue and the queue itself.
1737 *
1738 * Caveat:
1739 * Hopefully the low level driver will have finished any
1740 * outstanding requests first...
1741 **/
1742void blk_cleanup_queue(request_queue_t * q)
1743{
1744 struct request_list *rl = &q->rq;
1745
1746 if (!atomic_dec_and_test(&q->refcnt))
1747 return;
1748
1749 if (q->elevator)
1750 elevator_exit(q->elevator);
1751
1752 blk_sync_queue(q);
1753
1754 if (rl->rq_pool)
1755 mempool_destroy(rl->rq_pool);
1756
1757 if (q->queue_tags)
1758 __blk_queue_free_tags(q);
1759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 kmem_cache_free(requestq_cachep, q);
1761}
1762
1763EXPORT_SYMBOL(blk_cleanup_queue);
1764
1765static int blk_init_free_list(request_queue_t *q)
1766{
1767 struct request_list *rl = &q->rq;
1768
1769 rl->count[READ] = rl->count[WRITE] = 0;
1770 rl->starved[READ] = rl->starved[WRITE] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +02001771 rl->elvpriv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 init_waitqueue_head(&rl->wait[READ]);
1773 init_waitqueue_head(&rl->wait[WRITE]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Christoph Lameter19460892005-06-23 00:08:19 -07001775 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
1776 mempool_free_slab, request_cachep, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
1778 if (!rl->rq_pool)
1779 return -ENOMEM;
1780
1781 return 0;
1782}
1783
Al Viro8267e262005-10-21 03:20:53 -04001784request_queue_t *blk_alloc_queue(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
Christoph Lameter19460892005-06-23 00:08:19 -07001786 return blk_alloc_queue_node(gfp_mask, -1);
1787}
1788EXPORT_SYMBOL(blk_alloc_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Al Viro8267e262005-10-21 03:20:53 -04001790request_queue_t *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -07001791{
1792 request_queue_t *q;
1793
1794 q = kmem_cache_alloc_node(requestq_cachep, gfp_mask, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 if (!q)
1796 return NULL;
1797
1798 memset(q, 0, sizeof(*q));
1799 init_timer(&q->unplug_timer);
1800 atomic_set(&q->refcnt, 1);
1801
1802 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
1803 q->backing_dev_info.unplug_io_data = q;
1804
1805 return q;
1806}
Christoph Lameter19460892005-06-23 00:08:19 -07001807EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809/**
1810 * blk_init_queue - prepare a request queue for use with a block device
1811 * @rfn: The function to be called to process requests that have been
1812 * placed on the queue.
1813 * @lock: Request queue spin lock
1814 *
1815 * Description:
1816 * If a block device wishes to use the standard request handling procedures,
1817 * which sorts requests and coalesces adjacent requests, then it must
1818 * call blk_init_queue(). The function @rfn will be called when there
1819 * are requests on the queue that need to be processed. If the device
1820 * supports plugging, then @rfn may not be called immediately when requests
1821 * are available on the queue, but may be called at some time later instead.
1822 * Plugged queues are generally unplugged when a buffer belonging to one
1823 * of the requests on the queue is needed, or due to memory pressure.
1824 *
1825 * @rfn is not required, or even expected, to remove all requests off the
1826 * queue, but only as many as it can handle at a time. If it does leave
1827 * requests on the queue, it is responsible for arranging that the requests
1828 * get dealt with eventually.
1829 *
1830 * The queue spin lock must be held while manipulating the requests on the
1831 * request queue.
1832 *
1833 * Function returns a pointer to the initialized request queue, or NULL if
1834 * it didn't succeed.
1835 *
1836 * Note:
1837 * blk_init_queue() must be paired with a blk_cleanup_queue() call
1838 * when the block device is deactivated (such as at module unload).
1839 **/
Christoph Lameter19460892005-06-23 00:08:19 -07001840
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841request_queue_t *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
1842{
Christoph Lameter19460892005-06-23 00:08:19 -07001843 return blk_init_queue_node(rfn, lock, -1);
1844}
1845EXPORT_SYMBOL(blk_init_queue);
1846
1847request_queue_t *
1848blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
1849{
1850 request_queue_t *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
1852 if (!q)
1853 return NULL;
1854
Christoph Lameter19460892005-06-23 00:08:19 -07001855 q->node = node_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 if (blk_init_free_list(q))
1857 goto out_init;
1858
152587d2005-04-12 16:22:06 -05001859 /*
1860 * if caller didn't supply a lock, they get per-queue locking with
1861 * our embedded lock
1862 */
1863 if (!lock) {
1864 spin_lock_init(&q->__queue_lock);
1865 lock = &q->__queue_lock;
1866 }
1867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 q->request_fn = rfn;
1869 q->back_merge_fn = ll_back_merge_fn;
1870 q->front_merge_fn = ll_front_merge_fn;
1871 q->merge_requests_fn = ll_merge_requests_fn;
1872 q->prep_rq_fn = NULL;
1873 q->unplug_fn = generic_unplug_device;
1874 q->queue_flags = (1 << QUEUE_FLAG_CLUSTER);
1875 q->queue_lock = lock;
1876
1877 blk_queue_segment_boundary(q, 0xffffffff);
1878
1879 blk_queue_make_request(q, __make_request);
1880 blk_queue_max_segment_size(q, MAX_SEGMENT_SIZE);
1881
1882 blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS);
1883 blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS);
1884
1885 /*
1886 * all done
1887 */
1888 if (!elevator_init(q, NULL)) {
1889 blk_queue_congestion_threshold(q);
1890 return q;
1891 }
1892
1893 blk_cleanup_queue(q);
1894out_init:
1895 kmem_cache_free(requestq_cachep, q);
1896 return NULL;
1897}
Christoph Lameter19460892005-06-23 00:08:19 -07001898EXPORT_SYMBOL(blk_init_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
1900int blk_get_queue(request_queue_t *q)
1901{
Nick Pigginfde6ad22005-06-23 00:08:53 -07001902 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 atomic_inc(&q->refcnt);
1904 return 0;
1905 }
1906
1907 return 1;
1908}
1909
1910EXPORT_SYMBOL(blk_get_queue);
1911
1912static inline void blk_free_request(request_queue_t *q, struct request *rq)
1913{
Tejun Heocb98fc82005-10-28 08:29:39 +02001914 if (rq->flags & REQ_ELVPRIV)
1915 elv_put_request(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 mempool_free(rq, q->rq.rq_pool);
1917}
1918
Jens Axboe22e2c502005-06-27 10:55:12 +02001919static inline struct request *
Tejun Heocb98fc82005-10-28 08:29:39 +02001920blk_alloc_request(request_queue_t *q, int rw, struct bio *bio,
Linus Torvalds5dd96242005-10-28 08:56:34 -07001921 int priv, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
1923 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
1924
1925 if (!rq)
1926 return NULL;
1927
1928 /*
1929 * first three bits are identical in rq->flags and bio->bi_rw,
1930 * see bio.h and blkdev.h
1931 */
1932 rq->flags = rw;
1933
Tejun Heocb98fc82005-10-28 08:29:39 +02001934 if (priv) {
1935 if (unlikely(elv_set_request(q, rq, bio, gfp_mask))) {
1936 mempool_free(rq, q->rq.rq_pool);
1937 return NULL;
1938 }
1939 rq->flags |= REQ_ELVPRIV;
1940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Tejun Heocb98fc82005-10-28 08:29:39 +02001942 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943}
1944
1945/*
1946 * ioc_batching returns true if the ioc is a valid batching request and
1947 * should be given priority access to a request.
1948 */
1949static inline int ioc_batching(request_queue_t *q, struct io_context *ioc)
1950{
1951 if (!ioc)
1952 return 0;
1953
1954 /*
1955 * Make sure the process is able to allocate at least 1 request
1956 * even if the batch times out, otherwise we could theoretically
1957 * lose wakeups.
1958 */
1959 return ioc->nr_batch_requests == q->nr_batching ||
1960 (ioc->nr_batch_requests > 0
1961 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
1962}
1963
1964/*
1965 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
1966 * will cause the process to be a "batcher" on all queues in the system. This
1967 * is the behaviour we want though - once it gets a wakeup it should be given
1968 * a nice run.
1969 */
Adrian Bunk93d17d32005-06-25 14:59:10 -07001970static void ioc_set_batching(request_queue_t *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
1972 if (!ioc || ioc_batching(q, ioc))
1973 return;
1974
1975 ioc->nr_batch_requests = q->nr_batching;
1976 ioc->last_waited = jiffies;
1977}
1978
1979static void __freed_request(request_queue_t *q, int rw)
1980{
1981 struct request_list *rl = &q->rq;
1982
1983 if (rl->count[rw] < queue_congestion_off_threshold(q))
1984 clear_queue_congested(q, rw);
1985
1986 if (rl->count[rw] + 1 <= q->nr_requests) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 if (waitqueue_active(&rl->wait[rw]))
1988 wake_up(&rl->wait[rw]);
1989
1990 blk_clear_queue_full(q, rw);
1991 }
1992}
1993
1994/*
1995 * A request has just been released. Account for it, update the full and
1996 * congestion status, wake up any waiters. Called under q->queue_lock.
1997 */
Tejun Heocb98fc82005-10-28 08:29:39 +02001998static void freed_request(request_queue_t *q, int rw, int priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999{
2000 struct request_list *rl = &q->rq;
2001
2002 rl->count[rw]--;
Tejun Heocb98fc82005-10-28 08:29:39 +02002003 if (priv)
2004 rl->elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
2006 __freed_request(q, rw);
2007
2008 if (unlikely(rl->starved[rw ^ 1]))
2009 __freed_request(q, rw ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010}
2011
2012#define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist)
2013/*
Nick Piggind6344532005-06-28 20:45:14 -07002014 * Get a free request, queue_lock must be held.
2015 * Returns NULL on failure, with queue_lock held.
2016 * Returns !NULL on success, with queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 */
Jens Axboe22e2c502005-06-27 10:55:12 +02002018static struct request *get_request(request_queue_t *q, int rw, struct bio *bio,
Al Viro8267e262005-10-21 03:20:53 -04002019 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
2021 struct request *rq = NULL;
2022 struct request_list *rl = &q->rq;
Jens Axboe88ee5ef2005-11-12 11:09:12 +01002023 struct io_context *ioc = NULL;
2024 int may_queue, priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Jens Axboe88ee5ef2005-11-12 11:09:12 +01002026 may_queue = elv_may_queue(q, rw, bio);
2027 if (may_queue == ELV_MQUEUE_NO)
2028 goto rq_starved;
2029
2030 if (rl->count[rw]+1 >= queue_congestion_on_threshold(q)) {
2031 if (rl->count[rw]+1 >= q->nr_requests) {
2032 ioc = current_io_context(GFP_ATOMIC);
2033 /*
2034 * The queue will fill after this allocation, so set
2035 * it as full, and mark this process as "batching".
2036 * This process will be allowed to complete a batch of
2037 * requests, others will be blocked.
2038 */
2039 if (!blk_queue_full(q, rw)) {
2040 ioc_set_batching(q, ioc);
2041 blk_set_queue_full(q, rw);
2042 } else {
2043 if (may_queue != ELV_MQUEUE_MUST
2044 && !ioc_batching(q, ioc)) {
2045 /*
2046 * The queue is full and the allocating
2047 * process is not a "batcher", and not
2048 * exempted by the IO scheduler
2049 */
2050 goto out;
2051 }
2052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 }
Jens Axboe88ee5ef2005-11-12 11:09:12 +01002054 set_queue_congested(q, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 }
2056
Jens Axboe082cf692005-06-28 16:35:11 +02002057 /*
2058 * Only allow batching queuers to allocate up to 50% over the defined
2059 * limit of requests, otherwise we could have thousands of requests
2060 * allocated with any setting of ->nr_requests
2061 */
Hugh Dickinsfd782a42005-06-29 15:15:40 +01002062 if (rl->count[rw] >= (3 * q->nr_requests / 2))
Jens Axboe082cf692005-06-28 16:35:11 +02002063 goto out;
Hugh Dickinsfd782a42005-06-29 15:15:40 +01002064
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 rl->count[rw]++;
2066 rl->starved[rw] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +02002067
Jens Axboe64521d12005-10-28 08:30:39 +02002068 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Tejun Heocb98fc82005-10-28 08:29:39 +02002069 if (priv)
2070 rl->elvpriv++;
2071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 spin_unlock_irq(q->queue_lock);
2073
Tejun Heocb98fc82005-10-28 08:29:39 +02002074 rq = blk_alloc_request(q, rw, bio, priv, gfp_mask);
Jens Axboe88ee5ef2005-11-12 11:09:12 +01002075 if (unlikely(!rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 /*
2077 * Allocation failed presumably due to memory. Undo anything
2078 * we might have messed up.
2079 *
2080 * Allocating task should really be put onto the front of the
2081 * wait queue, but this is pretty rare.
2082 */
2083 spin_lock_irq(q->queue_lock);
Tejun Heocb98fc82005-10-28 08:29:39 +02002084 freed_request(q, rw, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
2086 /*
2087 * in the very unlikely event that allocation failed and no
2088 * requests for this direction was pending, mark us starved
2089 * so that freeing of a request in the other direction will
2090 * notice us. another possible fix would be to split the
2091 * rq mempool into READ and WRITE
2092 */
2093rq_starved:
2094 if (unlikely(rl->count[rw] == 0))
2095 rl->starved[rw] = 1;
2096
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 goto out;
2098 }
2099
Jens Axboe88ee5ef2005-11-12 11:09:12 +01002100 /*
2101 * ioc may be NULL here, and ioc_batching will be false. That's
2102 * OK, if the queue is under the request limit then requests need
2103 * not count toward the nr_batch_requests limit. There will always
2104 * be some limit enforced by BLK_BATCH_TIME.
2105 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 if (ioc_batching(q, ioc))
2107 ioc->nr_batch_requests--;
2108
2109 rq_init(q, rq);
2110 rq->rl = rl;
2111out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 return rq;
2113}
2114
2115/*
2116 * No available requests for this queue, unplug the device and wait for some
2117 * requests to become available.
Nick Piggind6344532005-06-28 20:45:14 -07002118 *
2119 * Called with q->queue_lock held, and returns with it unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 */
Jens Axboe22e2c502005-06-27 10:55:12 +02002121static struct request *get_request_wait(request_queue_t *q, int rw,
2122 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 struct request *rq;
2125
Nick Piggin450991b2005-06-28 20:45:13 -07002126 rq = get_request(q, rw, bio, GFP_NOIO);
2127 while (!rq) {
2128 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 struct request_list *rl = &q->rq;
2130
2131 prepare_to_wait_exclusive(&rl->wait[rw], &wait,
2132 TASK_UNINTERRUPTIBLE);
2133
Jens Axboe22e2c502005-06-27 10:55:12 +02002134 rq = get_request(q, rw, bio, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136 if (!rq) {
2137 struct io_context *ioc;
2138
Nick Piggind6344532005-06-28 20:45:14 -07002139 __generic_unplug_device(q);
2140 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 io_schedule();
2142
2143 /*
2144 * After sleeping, we become a "batching" process and
2145 * will be able to allocate at least one request, and
2146 * up to a big batch of them for a small period time.
2147 * See ioc_batching, ioc_set_batching
2148 */
Nick Pigginfb3cc432005-06-28 20:45:15 -07002149 ioc = current_io_context(GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 ioc_set_batching(q, ioc);
Nick Piggind6344532005-06-28 20:45:14 -07002151
2152 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 }
2154 finish_wait(&rl->wait[rw], &wait);
Nick Piggin450991b2005-06-28 20:45:13 -07002155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
2157 return rq;
2158}
2159
Al Viro8267e262005-10-21 03:20:53 -04002160struct request *blk_get_request(request_queue_t *q, int rw, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161{
2162 struct request *rq;
2163
2164 BUG_ON(rw != READ && rw != WRITE);
2165
Nick Piggind6344532005-06-28 20:45:14 -07002166 spin_lock_irq(q->queue_lock);
2167 if (gfp_mask & __GFP_WAIT) {
Jens Axboe22e2c502005-06-27 10:55:12 +02002168 rq = get_request_wait(q, rw, NULL);
Nick Piggind6344532005-06-28 20:45:14 -07002169 } else {
Jens Axboe22e2c502005-06-27 10:55:12 +02002170 rq = get_request(q, rw, NULL, gfp_mask);
Nick Piggind6344532005-06-28 20:45:14 -07002171 if (!rq)
2172 spin_unlock_irq(q->queue_lock);
2173 }
2174 /* q->queue_lock is unlocked at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
2176 return rq;
2177}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178EXPORT_SYMBOL(blk_get_request);
2179
2180/**
2181 * blk_requeue_request - put a request back on queue
2182 * @q: request queue where request should be inserted
2183 * @rq: request to be inserted
2184 *
2185 * Description:
2186 * Drivers often keep queueing requests until the hardware cannot accept
2187 * more, when that condition happens we need to put the request back
2188 * on the queue. Must be called with queue lock held.
2189 */
2190void blk_requeue_request(request_queue_t *q, struct request *rq)
2191{
2192 if (blk_rq_tagged(rq))
2193 blk_queue_end_tag(q, rq);
2194
2195 elv_requeue_request(q, rq);
2196}
2197
2198EXPORT_SYMBOL(blk_requeue_request);
2199
2200/**
2201 * blk_insert_request - insert a special request in to a request queue
2202 * @q: request queue where request should be inserted
2203 * @rq: request to be inserted
2204 * @at_head: insert request at head or tail of queue
2205 * @data: private data
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 *
2207 * Description:
2208 * Many block devices need to execute commands asynchronously, so they don't
2209 * block the whole kernel from preemption during request execution. This is
2210 * accomplished normally by inserting aritficial requests tagged as
2211 * REQ_SPECIAL in to the corresponding request queue, and letting them be
2212 * scheduled for actual execution by the request queue.
2213 *
2214 * We have the option of inserting the head or the tail of the queue.
2215 * Typically we use the tail for new ioctls and so forth. We use the head
2216 * of the queue for things like a QUEUE_FULL message from a device, or a
2217 * host that is unable to accept a particular command.
2218 */
2219void blk_insert_request(request_queue_t *q, struct request *rq,
Tejun Heo 867d1192005-04-24 02:06:05 -05002220 int at_head, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221{
Tejun Heo 867d1192005-04-24 02:06:05 -05002222 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 unsigned long flags;
2224
2225 /*
2226 * tell I/O scheduler that this isn't a regular read/write (ie it
2227 * must not attempt merges on this) and that it acts as a soft
2228 * barrier
2229 */
2230 rq->flags |= REQ_SPECIAL | REQ_SOFTBARRIER;
2231
2232 rq->special = data;
2233
2234 spin_lock_irqsave(q->queue_lock, flags);
2235
2236 /*
2237 * If command is tagged, release the tag
2238 */
Tejun Heo 867d1192005-04-24 02:06:05 -05002239 if (blk_rq_tagged(rq))
2240 blk_queue_end_tag(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
Tejun Heo 867d1192005-04-24 02:06:05 -05002242 drive_stat_acct(rq, rq->nr_sectors, 1);
2243 __elv_add_request(q, rq, where, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 if (blk_queue_plugged(q))
2246 __generic_unplug_device(q);
2247 else
2248 q->request_fn(q);
2249 spin_unlock_irqrestore(q->queue_lock, flags);
2250}
2251
2252EXPORT_SYMBOL(blk_insert_request);
2253
2254/**
2255 * blk_rq_map_user - map user data to a request, for REQ_BLOCK_PC usage
2256 * @q: request queue where request should be inserted
Christoph Hellwig 73747ae2005-06-20 14:21:01 +02002257 * @rq: request structure to fill
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 * @ubuf: the user buffer
2259 * @len: length of user data
2260 *
2261 * Description:
2262 * Data will be mapped directly for zero copy io, if possible. Otherwise
2263 * a kernel bounce buffer is used.
2264 *
2265 * A matching blk_rq_unmap_user() must be issued at the end of io, while
2266 * still in process context.
2267 *
2268 * Note: The mapped bio may need to be bounced through blk_queue_bounce()
2269 * before being submitted to the device, as pages mapped may be out of
2270 * reach. It's the callers responsibility to make sure this happens. The
2271 * original bio must be passed back in to blk_rq_unmap_user() for proper
2272 * unmapping.
2273 */
Jens Axboedd1cab92005-06-20 14:06:01 +02002274int blk_rq_map_user(request_queue_t *q, struct request *rq, void __user *ubuf,
2275 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276{
2277 unsigned long uaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 struct bio *bio;
Jens Axboedd1cab92005-06-20 14:06:01 +02002279 int reading;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Mike Christiedefd94b2005-12-05 02:37:06 -06002281 if (len > (q->max_hw_sectors << 9))
Jens Axboedd1cab92005-06-20 14:06:01 +02002282 return -EINVAL;
2283 if (!len || !ubuf)
2284 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
Jens Axboedd1cab92005-06-20 14:06:01 +02002286 reading = rq_data_dir(rq) == READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
2288 /*
2289 * if alignment requirement is satisfied, map in user pages for
2290 * direct dma. else, set up kernel bounce buffers
2291 */
2292 uaddr = (unsigned long) ubuf;
2293 if (!(uaddr & queue_dma_alignment(q)) && !(len & queue_dma_alignment(q)))
Jens Axboedd1cab92005-06-20 14:06:01 +02002294 bio = bio_map_user(q, NULL, uaddr, len, reading);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 else
Jens Axboedd1cab92005-06-20 14:06:01 +02002296 bio = bio_copy_user(q, uaddr, len, reading);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
2298 if (!IS_ERR(bio)) {
2299 rq->bio = rq->biotail = bio;
2300 blk_rq_bio_prep(q, rq, bio);
2301
2302 rq->buffer = rq->data = NULL;
2303 rq->data_len = len;
Jens Axboedd1cab92005-06-20 14:06:01 +02002304 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 }
2306
2307 /*
2308 * bio is the err-ptr
2309 */
Jens Axboedd1cab92005-06-20 14:06:01 +02002310 return PTR_ERR(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311}
2312
2313EXPORT_SYMBOL(blk_rq_map_user);
2314
2315/**
James Bottomley f1970ba2005-06-20 14:06:52 +02002316 * blk_rq_map_user_iov - map user data to a request, for REQ_BLOCK_PC usage
2317 * @q: request queue where request should be inserted
2318 * @rq: request to map data to
2319 * @iov: pointer to the iovec
2320 * @iov_count: number of elements in the iovec
2321 *
2322 * Description:
2323 * Data will be mapped directly for zero copy io, if possible. Otherwise
2324 * a kernel bounce buffer is used.
2325 *
2326 * A matching blk_rq_unmap_user() must be issued at the end of io, while
2327 * still in process context.
2328 *
2329 * Note: The mapped bio may need to be bounced through blk_queue_bounce()
2330 * before being submitted to the device, as pages mapped may be out of
2331 * reach. It's the callers responsibility to make sure this happens. The
2332 * original bio must be passed back in to blk_rq_unmap_user() for proper
2333 * unmapping.
2334 */
2335int blk_rq_map_user_iov(request_queue_t *q, struct request *rq,
2336 struct sg_iovec *iov, int iov_count)
2337{
2338 struct bio *bio;
2339
2340 if (!iov || iov_count <= 0)
2341 return -EINVAL;
2342
2343 /* we don't allow misaligned data like bio_map_user() does. If the
2344 * user is using sg, they're expected to know the alignment constraints
2345 * and respect them accordingly */
2346 bio = bio_map_user_iov(q, NULL, iov, iov_count, rq_data_dir(rq)== READ);
2347 if (IS_ERR(bio))
2348 return PTR_ERR(bio);
2349
2350 rq->bio = rq->biotail = bio;
2351 blk_rq_bio_prep(q, rq, bio);
2352 rq->buffer = rq->data = NULL;
2353 rq->data_len = bio->bi_size;
2354 return 0;
2355}
2356
2357EXPORT_SYMBOL(blk_rq_map_user_iov);
2358
2359/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 * blk_rq_unmap_user - unmap a request with user data
Christoph Hellwig 73747ae2005-06-20 14:21:01 +02002361 * @bio: bio to be unmapped
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 * @ulen: length of user buffer
2363 *
2364 * Description:
Christoph Hellwig 73747ae2005-06-20 14:21:01 +02002365 * Unmap a bio previously mapped by blk_rq_map_user().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 */
Jens Axboedd1cab92005-06-20 14:06:01 +02002367int blk_rq_unmap_user(struct bio *bio, unsigned int ulen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368{
2369 int ret = 0;
2370
2371 if (bio) {
2372 if (bio_flagged(bio, BIO_USER_MAPPED))
2373 bio_unmap_user(bio);
2374 else
2375 ret = bio_uncopy_user(bio);
2376 }
2377
Jens Axboedd1cab92005-06-20 14:06:01 +02002378 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379}
2380
2381EXPORT_SYMBOL(blk_rq_unmap_user);
2382
2383/**
Mike Christie df46b9a2005-06-20 14:04:44 +02002384 * blk_rq_map_kern - map kernel data to a request, for REQ_BLOCK_PC usage
2385 * @q: request queue where request should be inserted
Christoph Hellwig 73747ae2005-06-20 14:21:01 +02002386 * @rq: request to fill
Mike Christie df46b9a2005-06-20 14:04:44 +02002387 * @kbuf: the kernel buffer
2388 * @len: length of user data
Christoph Hellwig 73747ae2005-06-20 14:21:01 +02002389 * @gfp_mask: memory allocation flags
Mike Christie df46b9a2005-06-20 14:04:44 +02002390 */
Jens Axboedd1cab92005-06-20 14:06:01 +02002391int blk_rq_map_kern(request_queue_t *q, struct request *rq, void *kbuf,
Al Viro8267e262005-10-21 03:20:53 -04002392 unsigned int len, gfp_t gfp_mask)
Mike Christie df46b9a2005-06-20 14:04:44 +02002393{
Mike Christie df46b9a2005-06-20 14:04:44 +02002394 struct bio *bio;
2395
Mike Christiedefd94b2005-12-05 02:37:06 -06002396 if (len > (q->max_hw_sectors << 9))
Jens Axboedd1cab92005-06-20 14:06:01 +02002397 return -EINVAL;
2398 if (!len || !kbuf)
2399 return -EINVAL;
Mike Christie df46b9a2005-06-20 14:04:44 +02002400
2401 bio = bio_map_kern(q, kbuf, len, gfp_mask);
Jens Axboedd1cab92005-06-20 14:06:01 +02002402 if (IS_ERR(bio))
2403 return PTR_ERR(bio);
Mike Christie df46b9a2005-06-20 14:04:44 +02002404
Jens Axboedd1cab92005-06-20 14:06:01 +02002405 if (rq_data_dir(rq) == WRITE)
2406 bio->bi_rw |= (1 << BIO_RW);
Mike Christie df46b9a2005-06-20 14:04:44 +02002407
Jens Axboedd1cab92005-06-20 14:06:01 +02002408 rq->bio = rq->biotail = bio;
2409 blk_rq_bio_prep(q, rq, bio);
Mike Christie df46b9a2005-06-20 14:04:44 +02002410
Jens Axboedd1cab92005-06-20 14:06:01 +02002411 rq->buffer = rq->data = NULL;
2412 rq->data_len = len;
2413 return 0;
Mike Christie df46b9a2005-06-20 14:04:44 +02002414}
2415
2416EXPORT_SYMBOL(blk_rq_map_kern);
2417
Christoph Hellwig 73747ae2005-06-20 14:21:01 +02002418/**
2419 * blk_execute_rq_nowait - insert a request into queue for execution
2420 * @q: queue to insert the request in
2421 * @bd_disk: matching gendisk
2422 * @rq: request to insert
2423 * @at_head: insert request at head or tail of queue
2424 * @done: I/O completion handler
2425 *
2426 * Description:
2427 * Insert a fully prepared request at the back of the io scheduler queue
2428 * for execution. Don't wait for completion.
2429 */
James Bottomley f1970ba2005-06-20 14:06:52 +02002430void blk_execute_rq_nowait(request_queue_t *q, struct gendisk *bd_disk,
2431 struct request *rq, int at_head,
Tejun Heo8ffdc652006-01-06 09:49:03 +01002432 rq_end_io_fn *done)
James Bottomley f1970ba2005-06-20 14:06:52 +02002433{
2434 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
2435
2436 rq->rq_disk = bd_disk;
2437 rq->flags |= REQ_NOMERGE;
2438 rq->end_io = done;
2439 elv_add_request(q, rq, where, 1);
2440 generic_unplug_device(q);
2441}
2442
Mike Christie6e39b692005-11-11 05:30:24 -06002443EXPORT_SYMBOL_GPL(blk_execute_rq_nowait);
2444
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445/**
2446 * blk_execute_rq - insert a request into queue for execution
2447 * @q: queue to insert the request in
2448 * @bd_disk: matching gendisk
2449 * @rq: request to insert
James Bottomley 994ca9a2005-06-20 14:11:09 +02002450 * @at_head: insert request at head or tail of queue
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 *
2452 * Description:
2453 * Insert a fully prepared request at the back of the io scheduler queue
Christoph Hellwig 73747ae2005-06-20 14:21:01 +02002454 * for execution and wait for completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 */
2456int blk_execute_rq(request_queue_t *q, struct gendisk *bd_disk,
James Bottomley 994ca9a2005-06-20 14:11:09 +02002457 struct request *rq, int at_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458{
2459 DECLARE_COMPLETION(wait);
2460 char sense[SCSI_SENSE_BUFFERSIZE];
2461 int err = 0;
2462
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 /*
2464 * we need an extra reference to the request, so we can look at
2465 * it after io completion
2466 */
2467 rq->ref_count++;
2468
2469 if (!rq->sense) {
2470 memset(sense, 0, sizeof(sense));
2471 rq->sense = sense;
2472 rq->sense_len = 0;
2473 }
2474
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 rq->waiting = &wait;
James Bottomley 994ca9a2005-06-20 14:11:09 +02002476 blk_execute_rq_nowait(q, bd_disk, rq, at_head, blk_end_sync_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 wait_for_completion(&wait);
2478 rq->waiting = NULL;
2479
2480 if (rq->errors)
2481 err = -EIO;
2482
2483 return err;
2484}
2485
2486EXPORT_SYMBOL(blk_execute_rq);
2487
2488/**
2489 * blkdev_issue_flush - queue a flush
2490 * @bdev: blockdev to issue flush for
2491 * @error_sector: error sector
2492 *
2493 * Description:
2494 * Issue a flush for the block device in question. Caller can supply
2495 * room for storing the error offset in case of a flush error, if they
2496 * wish to. Caller must run wait_for_completion() on its own.
2497 */
2498int blkdev_issue_flush(struct block_device *bdev, sector_t *error_sector)
2499{
2500 request_queue_t *q;
2501
2502 if (bdev->bd_disk == NULL)
2503 return -ENXIO;
2504
2505 q = bdev_get_queue(bdev);
2506 if (!q)
2507 return -ENXIO;
2508 if (!q->issue_flush_fn)
2509 return -EOPNOTSUPP;
2510
2511 return q->issue_flush_fn(q, bdev->bd_disk, error_sector);
2512}
2513
2514EXPORT_SYMBOL(blkdev_issue_flush);
2515
Adrian Bunk93d17d32005-06-25 14:59:10 -07002516static void drive_stat_acct(struct request *rq, int nr_sectors, int new_io)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517{
2518 int rw = rq_data_dir(rq);
2519
2520 if (!blk_fs_request(rq) || !rq->rq_disk)
2521 return;
2522
Jens Axboed72d9042005-11-01 08:35:42 +01002523 if (!new_io) {
Jens Axboea3623572005-11-01 09:26:16 +01002524 __disk_stat_inc(rq->rq_disk, merges[rw]);
Jens Axboed72d9042005-11-01 08:35:42 +01002525 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 disk_round_stats(rq->rq_disk);
2527 rq->rq_disk->in_flight++;
2528 }
2529}
2530
2531/*
2532 * add-request adds a request to the linked list.
2533 * queue lock is held and interrupts disabled, as we muck with the
2534 * request queue list.
2535 */
2536static inline void add_request(request_queue_t * q, struct request * req)
2537{
2538 drive_stat_acct(req, req->nr_sectors, 1);
2539
2540 if (q->activity_fn)
2541 q->activity_fn(q->activity_data, rq_data_dir(req));
2542
2543 /*
2544 * elevator indicated where it wants this request to be
2545 * inserted at elevator_merge time
2546 */
2547 __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
2548}
2549
2550/*
2551 * disk_round_stats() - Round off the performance stats on a struct
2552 * disk_stats.
2553 *
2554 * The average IO queue length and utilisation statistics are maintained
2555 * by observing the current state of the queue length and the amount of
2556 * time it has been in this state for.
2557 *
2558 * Normally, that accounting is done on IO completion, but that can result
2559 * in more than a second's worth of IO being accounted for within any one
2560 * second, leading to >100% utilisation. To deal with that, we call this
2561 * function to do a round-off before returning the results when reading
2562 * /proc/diskstats. This accounts immediately for all queue usage up to
2563 * the current jiffies and restarts the counters again.
2564 */
2565void disk_round_stats(struct gendisk *disk)
2566{
2567 unsigned long now = jiffies;
2568
Chen, Kenneth Wb2982642005-10-13 21:49:29 +02002569 if (now == disk->stamp)
2570 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Chen, Kenneth W20e5c812005-10-13 21:48:42 +02002572 if (disk->in_flight) {
2573 __disk_stat_add(disk, time_in_queue,
2574 disk->in_flight * (now - disk->stamp));
2575 __disk_stat_add(disk, io_ticks, (now - disk->stamp));
2576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 disk->stamp = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578}
2579
2580/*
2581 * queue lock must be held
2582 */
Mike Christie6e39b692005-11-11 05:30:24 -06002583void __blk_put_request(request_queue_t *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584{
2585 struct request_list *rl = req->rl;
2586
2587 if (unlikely(!q))
2588 return;
2589 if (unlikely(--req->ref_count))
2590 return;
2591
Tejun Heo8922e162005-10-20 16:23:44 +02002592 elv_completed_request(q, req);
2593
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 req->rq_status = RQ_INACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 req->rl = NULL;
2596
2597 /*
2598 * Request may not have originated from ll_rw_blk. if not,
2599 * it didn't come out of our reserved rq pools
2600 */
2601 if (rl) {
2602 int rw = rq_data_dir(req);
Tejun Heocb98fc82005-10-28 08:29:39 +02002603 int priv = req->flags & REQ_ELVPRIV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 BUG_ON(!list_empty(&req->queuelist));
2606
2607 blk_free_request(q, req);
Tejun Heocb98fc82005-10-28 08:29:39 +02002608 freed_request(q, rw, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 }
2610}
2611
Mike Christie6e39b692005-11-11 05:30:24 -06002612EXPORT_SYMBOL_GPL(__blk_put_request);
2613
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614void blk_put_request(struct request *req)
2615{
Tejun Heo8922e162005-10-20 16:23:44 +02002616 unsigned long flags;
2617 request_queue_t *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
Tejun Heo8922e162005-10-20 16:23:44 +02002619 /*
2620 * Gee, IDE calls in w/ NULL q. Fix IDE and remove the
2621 * following if (q) test.
2622 */
2623 if (q) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 spin_lock_irqsave(q->queue_lock, flags);
2625 __blk_put_request(q, req);
2626 spin_unlock_irqrestore(q->queue_lock, flags);
2627 }
2628}
2629
2630EXPORT_SYMBOL(blk_put_request);
2631
2632/**
2633 * blk_end_sync_rq - executes a completion event on a request
2634 * @rq: request to complete
2635 */
Tejun Heo8ffdc652006-01-06 09:49:03 +01002636void blk_end_sync_rq(struct request *rq, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637{
2638 struct completion *waiting = rq->waiting;
2639
2640 rq->waiting = NULL;
2641 __blk_put_request(rq->q, rq);
2642
2643 /*
2644 * complete last, if this is a stack request the process (and thus
2645 * the rq pointer) could be invalid right after this complete()
2646 */
2647 complete(waiting);
2648}
2649EXPORT_SYMBOL(blk_end_sync_rq);
2650
2651/**
2652 * blk_congestion_wait - wait for a queue to become uncongested
2653 * @rw: READ or WRITE
2654 * @timeout: timeout in jiffies
2655 *
2656 * Waits for up to @timeout jiffies for a queue (any queue) to exit congestion.
2657 * If no queues are congested then just wait for the next request to be
2658 * returned.
2659 */
2660long blk_congestion_wait(int rw, long timeout)
2661{
2662 long ret;
2663 DEFINE_WAIT(wait);
2664 wait_queue_head_t *wqh = &congestion_wqh[rw];
2665
2666 prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
2667 ret = io_schedule_timeout(timeout);
2668 finish_wait(wqh, &wait);
2669 return ret;
2670}
2671
2672EXPORT_SYMBOL(blk_congestion_wait);
2673
2674/*
2675 * Has to be called with the request spinlock acquired
2676 */
2677static int attempt_merge(request_queue_t *q, struct request *req,
2678 struct request *next)
2679{
2680 if (!rq_mergeable(req) || !rq_mergeable(next))
2681 return 0;
2682
2683 /*
2684 * not contigious
2685 */
2686 if (req->sector + req->nr_sectors != next->sector)
2687 return 0;
2688
2689 if (rq_data_dir(req) != rq_data_dir(next)
2690 || req->rq_disk != next->rq_disk
2691 || next->waiting || next->special)
2692 return 0;
2693
2694 /*
2695 * If we are allowed to merge, then append bio list
2696 * from next to rq and release next. merge_requests_fn
2697 * will have updated segment counts, update sector
2698 * counts here.
2699 */
2700 if (!q->merge_requests_fn(q, req, next))
2701 return 0;
2702
2703 /*
2704 * At this point we have either done a back merge
2705 * or front merge. We need the smaller start_time of
2706 * the merged requests to be the current request
2707 * for accounting purposes.
2708 */
2709 if (time_after(req->start_time, next->start_time))
2710 req->start_time = next->start_time;
2711
2712 req->biotail->bi_next = next->bio;
2713 req->biotail = next->biotail;
2714
2715 req->nr_sectors = req->hard_nr_sectors += next->hard_nr_sectors;
2716
2717 elv_merge_requests(q, req, next);
2718
2719 if (req->rq_disk) {
2720 disk_round_stats(req->rq_disk);
2721 req->rq_disk->in_flight--;
2722 }
2723
Jens Axboe22e2c502005-06-27 10:55:12 +02002724 req->ioprio = ioprio_best(req->ioprio, next->ioprio);
2725
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 __blk_put_request(q, next);
2727 return 1;
2728}
2729
2730static inline int attempt_back_merge(request_queue_t *q, struct request *rq)
2731{
2732 struct request *next = elv_latter_request(q, rq);
2733
2734 if (next)
2735 return attempt_merge(q, rq, next);
2736
2737 return 0;
2738}
2739
2740static inline int attempt_front_merge(request_queue_t *q, struct request *rq)
2741{
2742 struct request *prev = elv_former_request(q, rq);
2743
2744 if (prev)
2745 return attempt_merge(q, prev, rq);
2746
2747 return 0;
2748}
2749
Tejun Heo52d9e672006-01-06 09:49:58 +01002750static void init_request_from_bio(struct request *req, struct bio *bio)
2751{
2752 req->flags |= REQ_CMD;
2753
2754 /*
2755 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
2756 */
2757 if (bio_rw_ahead(bio) || bio_failfast(bio))
2758 req->flags |= REQ_FAILFAST;
2759
2760 /*
2761 * REQ_BARRIER implies no merging, but lets make it explicit
2762 */
2763 if (unlikely(bio_barrier(bio)))
2764 req->flags |= (REQ_HARDBARRIER | REQ_NOMERGE);
2765
2766 req->errors = 0;
2767 req->hard_sector = req->sector = bio->bi_sector;
2768 req->hard_nr_sectors = req->nr_sectors = bio_sectors(bio);
2769 req->current_nr_sectors = req->hard_cur_sectors = bio_cur_sectors(bio);
2770 req->nr_phys_segments = bio_phys_segments(req->q, bio);
2771 req->nr_hw_segments = bio_hw_segments(req->q, bio);
2772 req->buffer = bio_data(bio); /* see ->buffer comment above */
2773 req->waiting = NULL;
2774 req->bio = req->biotail = bio;
2775 req->ioprio = bio_prio(bio);
2776 req->rq_disk = bio->bi_bdev->bd_disk;
2777 req->start_time = jiffies;
2778}
2779
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780static int __make_request(request_queue_t *q, struct bio *bio)
2781{
Nick Piggin450991b2005-06-28 20:45:13 -07002782 struct request *req;
Jens Axboe4a534f92005-04-16 15:25:40 -07002783 int el_ret, rw, nr_sectors, cur_nr_sectors, barrier, err, sync;
Jens Axboe22e2c502005-06-27 10:55:12 +02002784 unsigned short prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 sector_t sector;
2786
2787 sector = bio->bi_sector;
2788 nr_sectors = bio_sectors(bio);
2789 cur_nr_sectors = bio_cur_sectors(bio);
Jens Axboe22e2c502005-06-27 10:55:12 +02002790 prio = bio_prio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791
2792 rw = bio_data_dir(bio);
Jens Axboe4a534f92005-04-16 15:25:40 -07002793 sync = bio_sync(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
2795 /*
2796 * low level driver can indicate that it wants pages above a
2797 * certain limit bounced to low memory (ie for highmem, or even
2798 * ISA dma in theory)
2799 */
2800 blk_queue_bounce(q, &bio);
2801
2802 spin_lock_prefetch(q->queue_lock);
2803
2804 barrier = bio_barrier(bio);
Tejun Heo797e7db2006-01-06 09:51:03 +01002805 if (unlikely(barrier) && (q->next_ordered == QUEUE_ORDERED_NONE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 err = -EOPNOTSUPP;
2807 goto end_io;
2808 }
2809
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 spin_lock_irq(q->queue_lock);
2811
Nick Piggin450991b2005-06-28 20:45:13 -07002812 if (unlikely(barrier) || elv_queue_empty(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 goto get_rq;
2814
2815 el_ret = elv_merge(q, &req, bio);
2816 switch (el_ret) {
2817 case ELEVATOR_BACK_MERGE:
2818 BUG_ON(!rq_mergeable(req));
2819
2820 if (!q->back_merge_fn(q, req, bio))
2821 break;
2822
2823 req->biotail->bi_next = bio;
2824 req->biotail = bio;
2825 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
Jens Axboe22e2c502005-06-27 10:55:12 +02002826 req->ioprio = ioprio_best(req->ioprio, prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 drive_stat_acct(req, nr_sectors, 0);
2828 if (!attempt_back_merge(q, req))
2829 elv_merged_request(q, req);
2830 goto out;
2831
2832 case ELEVATOR_FRONT_MERGE:
2833 BUG_ON(!rq_mergeable(req));
2834
2835 if (!q->front_merge_fn(q, req, bio))
2836 break;
2837
2838 bio->bi_next = req->bio;
2839 req->bio = bio;
2840
2841 /*
2842 * may not be valid. if the low level driver said
2843 * it didn't need a bounce buffer then it better
2844 * not touch req->buffer either...
2845 */
2846 req->buffer = bio_data(bio);
2847 req->current_nr_sectors = cur_nr_sectors;
2848 req->hard_cur_sectors = cur_nr_sectors;
2849 req->sector = req->hard_sector = sector;
2850 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
Jens Axboe22e2c502005-06-27 10:55:12 +02002851 req->ioprio = ioprio_best(req->ioprio, prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 drive_stat_acct(req, nr_sectors, 0);
2853 if (!attempt_front_merge(q, req))
2854 elv_merged_request(q, req);
2855 goto out;
2856
Nick Piggin450991b2005-06-28 20:45:13 -07002857 /* ELV_NO_MERGE: elevator says don't/can't merge. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 default:
Nick Piggin450991b2005-06-28 20:45:13 -07002859 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 }
2861
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07002863 /*
2864 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07002865 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07002866 */
Nick Piggin450991b2005-06-28 20:45:13 -07002867 req = get_request_wait(q, rw, bio);
Nick Piggind6344532005-06-28 20:45:14 -07002868
Nick Piggin450991b2005-06-28 20:45:13 -07002869 /*
2870 * After dropping the lock and possibly sleeping here, our request
2871 * may now be mergeable after it had proven unmergeable (above).
2872 * We don't worry about that case for efficiency. It won't happen
2873 * often, and the elevators are able to handle it.
2874 */
Tejun Heo52d9e672006-01-06 09:49:58 +01002875 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
Nick Piggin450991b2005-06-28 20:45:13 -07002877 spin_lock_irq(q->queue_lock);
2878 if (elv_queue_empty(q))
2879 blk_plug_device(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 add_request(q, req);
2881out:
Jens Axboe4a534f92005-04-16 15:25:40 -07002882 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 __generic_unplug_device(q);
2884
2885 spin_unlock_irq(q->queue_lock);
2886 return 0;
2887
2888end_io:
2889 bio_endio(bio, nr_sectors << 9, err);
2890 return 0;
2891}
2892
2893/*
2894 * If bio->bi_dev is a partition, remap the location
2895 */
2896static inline void blk_partition_remap(struct bio *bio)
2897{
2898 struct block_device *bdev = bio->bi_bdev;
2899
2900 if (bdev != bdev->bd_contains) {
2901 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01002902 const int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
Jens Axboea3623572005-11-01 09:26:16 +01002904 p->sectors[rw] += bio_sectors(bio);
2905 p->ios[rw]++;
2906
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 bio->bi_sector += p->start_sect;
2908 bio->bi_bdev = bdev->bd_contains;
2909 }
2910}
2911
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912static void handle_bad_sector(struct bio *bio)
2913{
2914 char b[BDEVNAME_SIZE];
2915
2916 printk(KERN_INFO "attempt to access beyond end of device\n");
2917 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
2918 bdevname(bio->bi_bdev, b),
2919 bio->bi_rw,
2920 (unsigned long long)bio->bi_sector + bio_sectors(bio),
2921 (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
2922
2923 set_bit(BIO_EOF, &bio->bi_flags);
2924}
2925
2926/**
2927 * generic_make_request: hand a buffer to its device driver for I/O
2928 * @bio: The bio describing the location in memory and on the device.
2929 *
2930 * generic_make_request() is used to make I/O requests of block
2931 * devices. It is passed a &struct bio, which describes the I/O that needs
2932 * to be done.
2933 *
2934 * generic_make_request() does not return any status. The
2935 * success/failure status of the request, along with notification of
2936 * completion, is delivered asynchronously through the bio->bi_end_io
2937 * function described (one day) else where.
2938 *
2939 * The caller of generic_make_request must make sure that bi_io_vec
2940 * are set to describe the memory buffer, and that bi_dev and bi_sector are
2941 * set to describe the device address, and the
2942 * bi_end_io and optionally bi_private are set to describe how
2943 * completion notification should be signaled.
2944 *
2945 * generic_make_request and the drivers it calls may use bi_next if this
2946 * bio happens to be merged with someone else, and may change bi_dev and
2947 * bi_sector for remaps as it sees fit. So the values of these fields
2948 * should NOT be depended on after the call to generic_make_request.
2949 */
2950void generic_make_request(struct bio *bio)
2951{
2952 request_queue_t *q;
2953 sector_t maxsector;
2954 int ret, nr_sectors = bio_sectors(bio);
2955
2956 might_sleep();
2957 /* Test device or partition size, when known. */
2958 maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
2959 if (maxsector) {
2960 sector_t sector = bio->bi_sector;
2961
2962 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
2963 /*
2964 * This may well happen - the kernel calls bread()
2965 * without checking the size of the device, e.g., when
2966 * mounting a device.
2967 */
2968 handle_bad_sector(bio);
2969 goto end_io;
2970 }
2971 }
2972
2973 /*
2974 * Resolve the mapping until finished. (drivers are
2975 * still free to implement/resolve their own stacking
2976 * by explicitly returning 0)
2977 *
2978 * NOTE: we don't repeat the blk_size check for each new device.
2979 * Stacking drivers are expected to know what they are doing.
2980 */
2981 do {
2982 char b[BDEVNAME_SIZE];
2983
2984 q = bdev_get_queue(bio->bi_bdev);
2985 if (!q) {
2986 printk(KERN_ERR
2987 "generic_make_request: Trying to access "
2988 "nonexistent block-device %s (%Lu)\n",
2989 bdevname(bio->bi_bdev, b),
2990 (long long) bio->bi_sector);
2991end_io:
2992 bio_endio(bio, bio->bi_size, -EIO);
2993 break;
2994 }
2995
2996 if (unlikely(bio_sectors(bio) > q->max_hw_sectors)) {
2997 printk("bio too big device %s (%u > %u)\n",
2998 bdevname(bio->bi_bdev, b),
2999 bio_sectors(bio),
3000 q->max_hw_sectors);
3001 goto end_io;
3002 }
3003
Nick Pigginfde6ad22005-06-23 00:08:53 -07003004 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 goto end_io;
3006
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 /*
3008 * If this device has partitions, remap block n
3009 * of partition p to block n+start(p) of the disk.
3010 */
3011 blk_partition_remap(bio);
3012
3013 ret = q->make_request_fn(q, bio);
3014 } while (ret);
3015}
3016
3017EXPORT_SYMBOL(generic_make_request);
3018
3019/**
3020 * submit_bio: submit a bio to the block device layer for I/O
3021 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
3022 * @bio: The &struct bio which describes the I/O
3023 *
3024 * submit_bio() is very similar in purpose to generic_make_request(), and
3025 * uses that function to do most of the work. Both are fairly rough
3026 * interfaces, @bio must be presetup and ready for I/O.
3027 *
3028 */
3029void submit_bio(int rw, struct bio *bio)
3030{
3031 int count = bio_sectors(bio);
3032
3033 BIO_BUG_ON(!bio->bi_size);
3034 BIO_BUG_ON(!bio->bi_io_vec);
Jens Axboe22e2c502005-06-27 10:55:12 +02003035 bio->bi_rw |= rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 if (rw & WRITE)
3037 mod_page_state(pgpgout, count);
3038 else
3039 mod_page_state(pgpgin, count);
3040
3041 if (unlikely(block_dump)) {
3042 char b[BDEVNAME_SIZE];
3043 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
3044 current->comm, current->pid,
3045 (rw & WRITE) ? "WRITE" : "READ",
3046 (unsigned long long)bio->bi_sector,
3047 bdevname(bio->bi_bdev,b));
3048 }
3049
3050 generic_make_request(bio);
3051}
3052
3053EXPORT_SYMBOL(submit_bio);
3054
Adrian Bunk93d17d32005-06-25 14:59:10 -07003055static void blk_recalc_rq_segments(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056{
3057 struct bio *bio, *prevbio = NULL;
3058 int nr_phys_segs, nr_hw_segs;
3059 unsigned int phys_size, hw_size;
3060 request_queue_t *q = rq->q;
3061
3062 if (!rq->bio)
3063 return;
3064
3065 phys_size = hw_size = nr_phys_segs = nr_hw_segs = 0;
3066 rq_for_each_bio(bio, rq) {
3067 /* Force bio hw/phys segs to be recalculated. */
3068 bio->bi_flags &= ~(1 << BIO_SEG_VALID);
3069
3070 nr_phys_segs += bio_phys_segments(q, bio);
3071 nr_hw_segs += bio_hw_segments(q, bio);
3072 if (prevbio) {
3073 int pseg = phys_size + prevbio->bi_size + bio->bi_size;
3074 int hseg = hw_size + prevbio->bi_size + bio->bi_size;
3075
3076 if (blk_phys_contig_segment(q, prevbio, bio) &&
3077 pseg <= q->max_segment_size) {
3078 nr_phys_segs--;
3079 phys_size += prevbio->bi_size + bio->bi_size;
3080 } else
3081 phys_size = 0;
3082
3083 if (blk_hw_contig_segment(q, prevbio, bio) &&
3084 hseg <= q->max_segment_size) {
3085 nr_hw_segs--;
3086 hw_size += prevbio->bi_size + bio->bi_size;
3087 } else
3088 hw_size = 0;
3089 }
3090 prevbio = bio;
3091 }
3092
3093 rq->nr_phys_segments = nr_phys_segs;
3094 rq->nr_hw_segments = nr_hw_segs;
3095}
3096
Adrian Bunk93d17d32005-06-25 14:59:10 -07003097static void blk_recalc_rq_sectors(struct request *rq, int nsect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098{
3099 if (blk_fs_request(rq)) {
3100 rq->hard_sector += nsect;
3101 rq->hard_nr_sectors -= nsect;
3102
3103 /*
3104 * Move the I/O submission pointers ahead if required.
3105 */
3106 if ((rq->nr_sectors >= rq->hard_nr_sectors) &&
3107 (rq->sector <= rq->hard_sector)) {
3108 rq->sector = rq->hard_sector;
3109 rq->nr_sectors = rq->hard_nr_sectors;
3110 rq->hard_cur_sectors = bio_cur_sectors(rq->bio);
3111 rq->current_nr_sectors = rq->hard_cur_sectors;
3112 rq->buffer = bio_data(rq->bio);
3113 }
3114
3115 /*
3116 * if total number of sectors is less than the first segment
3117 * size, something has gone terribly wrong
3118 */
3119 if (rq->nr_sectors < rq->current_nr_sectors) {
3120 printk("blk: request botched\n");
3121 rq->nr_sectors = rq->current_nr_sectors;
3122 }
3123 }
3124}
3125
3126static int __end_that_request_first(struct request *req, int uptodate,
3127 int nr_bytes)
3128{
3129 int total_bytes, bio_nbytes, error, next_idx = 0;
3130 struct bio *bio;
3131
3132 /*
3133 * extend uptodate bool to allow < 0 value to be direct io error
3134 */
3135 error = 0;
3136 if (end_io_error(uptodate))
3137 error = !uptodate ? -EIO : uptodate;
3138
3139 /*
3140 * for a REQ_BLOCK_PC request, we want to carry any eventual
3141 * sense key with us all the way through
3142 */
3143 if (!blk_pc_request(req))
3144 req->errors = 0;
3145
3146 if (!uptodate) {
3147 if (blk_fs_request(req) && !(req->flags & REQ_QUIET))
3148 printk("end_request: I/O error, dev %s, sector %llu\n",
3149 req->rq_disk ? req->rq_disk->disk_name : "?",
3150 (unsigned long long)req->sector);
3151 }
3152
Jens Axboed72d9042005-11-01 08:35:42 +01003153 if (blk_fs_request(req) && req->rq_disk) {
Jens Axboea3623572005-11-01 09:26:16 +01003154 const int rw = rq_data_dir(req);
3155
3156 __disk_stat_add(req->rq_disk, sectors[rw], nr_bytes >> 9);
Jens Axboed72d9042005-11-01 08:35:42 +01003157 }
3158
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 total_bytes = bio_nbytes = 0;
3160 while ((bio = req->bio) != NULL) {
3161 int nbytes;
3162
3163 if (nr_bytes >= bio->bi_size) {
3164 req->bio = bio->bi_next;
3165 nbytes = bio->bi_size;
Tejun Heo797e7db2006-01-06 09:51:03 +01003166 if (!ordered_bio_endio(req, bio, nbytes, error))
3167 bio_endio(bio, nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 next_idx = 0;
3169 bio_nbytes = 0;
3170 } else {
3171 int idx = bio->bi_idx + next_idx;
3172
3173 if (unlikely(bio->bi_idx >= bio->bi_vcnt)) {
3174 blk_dump_rq_flags(req, "__end_that");
3175 printk("%s: bio idx %d >= vcnt %d\n",
3176 __FUNCTION__,
3177 bio->bi_idx, bio->bi_vcnt);
3178 break;
3179 }
3180
3181 nbytes = bio_iovec_idx(bio, idx)->bv_len;
3182 BIO_BUG_ON(nbytes > bio->bi_size);
3183
3184 /*
3185 * not a complete bvec done
3186 */
3187 if (unlikely(nbytes > nr_bytes)) {
3188 bio_nbytes += nr_bytes;
3189 total_bytes += nr_bytes;
3190 break;
3191 }
3192
3193 /*
3194 * advance to the next vector
3195 */
3196 next_idx++;
3197 bio_nbytes += nbytes;
3198 }
3199
3200 total_bytes += nbytes;
3201 nr_bytes -= nbytes;
3202
3203 if ((bio = req->bio)) {
3204 /*
3205 * end more in this run, or just return 'not-done'
3206 */
3207 if (unlikely(nr_bytes <= 0))
3208 break;
3209 }
3210 }
3211
3212 /*
3213 * completely done
3214 */
3215 if (!req->bio)
3216 return 0;
3217
3218 /*
3219 * if the request wasn't completed, update state
3220 */
3221 if (bio_nbytes) {
Tejun Heo797e7db2006-01-06 09:51:03 +01003222 if (!ordered_bio_endio(req, bio, bio_nbytes, error))
3223 bio_endio(bio, bio_nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 bio->bi_idx += next_idx;
3225 bio_iovec(bio)->bv_offset += nr_bytes;
3226 bio_iovec(bio)->bv_len -= nr_bytes;
3227 }
3228
3229 blk_recalc_rq_sectors(req, total_bytes >> 9);
3230 blk_recalc_rq_segments(req);
3231 return 1;
3232}
3233
3234/**
3235 * end_that_request_first - end I/O on a request
3236 * @req: the request being processed
3237 * @uptodate: 1 for success, 0 for I/O error, < 0 for specific error
3238 * @nr_sectors: number of sectors to end I/O on
3239 *
3240 * Description:
3241 * Ends I/O on a number of sectors attached to @req, and sets it up
3242 * for the next range of segments (if any) in the cluster.
3243 *
3244 * Return:
3245 * 0 - we are done with this request, call end_that_request_last()
3246 * 1 - still buffers pending for this request
3247 **/
3248int end_that_request_first(struct request *req, int uptodate, int nr_sectors)
3249{
3250 return __end_that_request_first(req, uptodate, nr_sectors << 9);
3251}
3252
3253EXPORT_SYMBOL(end_that_request_first);
3254
3255/**
3256 * end_that_request_chunk - end I/O on a request
3257 * @req: the request being processed
3258 * @uptodate: 1 for success, 0 for I/O error, < 0 for specific error
3259 * @nr_bytes: number of bytes to complete
3260 *
3261 * Description:
3262 * Ends I/O on a number of bytes attached to @req, and sets it up
3263 * for the next range of segments (if any). Like end_that_request_first(),
3264 * but deals with bytes instead of sectors.
3265 *
3266 * Return:
3267 * 0 - we are done with this request, call end_that_request_last()
3268 * 1 - still buffers pending for this request
3269 **/
3270int end_that_request_chunk(struct request *req, int uptodate, int nr_bytes)
3271{
3272 return __end_that_request_first(req, uptodate, nr_bytes);
3273}
3274
3275EXPORT_SYMBOL(end_that_request_chunk);
3276
3277/*
Jens Axboeff856ba2006-01-09 16:02:34 +01003278 * splice the completion data to a local structure and hand off to
3279 * process_completion_queue() to complete the requests
3280 */
3281static void blk_done_softirq(struct softirq_action *h)
3282{
3283 struct list_head *cpu_list;
3284 LIST_HEAD(local_list);
3285
3286 local_irq_disable();
3287 cpu_list = &__get_cpu_var(blk_cpu_done);
3288 list_splice_init(cpu_list, &local_list);
3289 local_irq_enable();
3290
3291 while (!list_empty(&local_list)) {
3292 struct request *rq = list_entry(local_list.next, struct request, donelist);
3293
3294 list_del_init(&rq->donelist);
3295 rq->q->softirq_done_fn(rq);
3296 }
3297}
3298
3299#ifdef CONFIG_HOTPLUG_CPU
3300
3301static int blk_cpu_notify(struct notifier_block *self, unsigned long action,
3302 void *hcpu)
3303{
3304 /*
3305 * If a CPU goes away, splice its entries to the current CPU
3306 * and trigger a run of the softirq
3307 */
3308 if (action == CPU_DEAD) {
3309 int cpu = (unsigned long) hcpu;
3310
3311 local_irq_disable();
3312 list_splice_init(&per_cpu(blk_cpu_done, cpu),
3313 &__get_cpu_var(blk_cpu_done));
3314 raise_softirq_irqoff(BLOCK_SOFTIRQ);
3315 local_irq_enable();
3316 }
3317
3318 return NOTIFY_OK;
3319}
3320
3321
3322static struct notifier_block __devinitdata blk_cpu_notifier = {
3323 .notifier_call = blk_cpu_notify,
3324};
3325
3326#endif /* CONFIG_HOTPLUG_CPU */
3327
3328/**
3329 * blk_complete_request - end I/O on a request
3330 * @req: the request being processed
3331 *
3332 * Description:
3333 * Ends all I/O on a request. It does not handle partial completions,
3334 * unless the driver actually implements this in its completionc callback
3335 * through requeueing. Theh actual completion happens out-of-order,
3336 * through a softirq handler. The user must have registered a completion
3337 * callback through blk_queue_softirq_done().
3338 **/
3339
3340void blk_complete_request(struct request *req)
3341{
3342 struct list_head *cpu_list;
3343 unsigned long flags;
3344
3345 BUG_ON(!req->q->softirq_done_fn);
3346
3347 local_irq_save(flags);
3348
3349 cpu_list = &__get_cpu_var(blk_cpu_done);
3350 list_add_tail(&req->donelist, cpu_list);
3351 raise_softirq_irqoff(BLOCK_SOFTIRQ);
3352
3353 local_irq_restore(flags);
3354}
3355
3356EXPORT_SYMBOL(blk_complete_request);
3357
3358/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 * queue lock must be held
3360 */
Tejun Heo8ffdc652006-01-06 09:49:03 +01003361void end_that_request_last(struct request *req, int uptodate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362{
3363 struct gendisk *disk = req->rq_disk;
Tejun Heo8ffdc652006-01-06 09:49:03 +01003364 int error;
3365
3366 /*
3367 * extend uptodate bool to allow < 0 value to be direct io error
3368 */
3369 error = 0;
3370 if (end_io_error(uptodate))
3371 error = !uptodate ? -EIO : uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372
3373 if (unlikely(laptop_mode) && blk_fs_request(req))
3374 laptop_io_completion();
3375
3376 if (disk && blk_fs_request(req)) {
3377 unsigned long duration = jiffies - req->start_time;
Jens Axboea3623572005-11-01 09:26:16 +01003378 const int rw = rq_data_dir(req);
3379
3380 __disk_stat_inc(disk, ios[rw]);
3381 __disk_stat_add(disk, ticks[rw], duration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 disk_round_stats(disk);
3383 disk->in_flight--;
3384 }
3385 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01003386 req->end_io(req, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 else
3388 __blk_put_request(req->q, req);
3389}
3390
3391EXPORT_SYMBOL(end_that_request_last);
3392
3393void end_request(struct request *req, int uptodate)
3394{
3395 if (!end_that_request_first(req, uptodate, req->hard_cur_sectors)) {
3396 add_disk_randomness(req->rq_disk);
3397 blkdev_dequeue_request(req);
Tejun Heo8ffdc652006-01-06 09:49:03 +01003398 end_that_request_last(req, uptodate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 }
3400}
3401
3402EXPORT_SYMBOL(end_request);
3403
3404void blk_rq_bio_prep(request_queue_t *q, struct request *rq, struct bio *bio)
3405{
3406 /* first three bits are identical in rq->flags and bio->bi_rw */
3407 rq->flags |= (bio->bi_rw & 7);
3408
3409 rq->nr_phys_segments = bio_phys_segments(q, bio);
3410 rq->nr_hw_segments = bio_hw_segments(q, bio);
3411 rq->current_nr_sectors = bio_cur_sectors(bio);
3412 rq->hard_cur_sectors = rq->current_nr_sectors;
3413 rq->hard_nr_sectors = rq->nr_sectors = bio_sectors(bio);
3414 rq->buffer = bio_data(bio);
3415
3416 rq->bio = rq->biotail = bio;
3417}
3418
3419EXPORT_SYMBOL(blk_rq_bio_prep);
3420
3421int kblockd_schedule_work(struct work_struct *work)
3422{
3423 return queue_work(kblockd_workqueue, work);
3424}
3425
3426EXPORT_SYMBOL(kblockd_schedule_work);
3427
3428void kblockd_flush(void)
3429{
3430 flush_workqueue(kblockd_workqueue);
3431}
3432EXPORT_SYMBOL(kblockd_flush);
3433
3434int __init blk_dev_init(void)
3435{
Jens Axboeff856ba2006-01-09 16:02:34 +01003436 int i;
3437
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 kblockd_workqueue = create_workqueue("kblockd");
3439 if (!kblockd_workqueue)
3440 panic("Failed to create kblockd\n");
3441
3442 request_cachep = kmem_cache_create("blkdev_requests",
3443 sizeof(struct request), 0, SLAB_PANIC, NULL, NULL);
3444
3445 requestq_cachep = kmem_cache_create("blkdev_queue",
3446 sizeof(request_queue_t), 0, SLAB_PANIC, NULL, NULL);
3447
3448 iocontext_cachep = kmem_cache_create("blkdev_ioc",
3449 sizeof(struct io_context), 0, SLAB_PANIC, NULL, NULL);
3450
Jens Axboeff856ba2006-01-09 16:02:34 +01003451 for (i = 0; i < NR_CPUS; i++)
3452 INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i));
3453
3454 open_softirq(BLOCK_SOFTIRQ, blk_done_softirq, NULL);
3455#ifdef CONFIG_HOTPLUG_CPU
3456 register_cpu_notifier(&blk_cpu_notifier);
3457#endif
3458
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 blk_max_low_pfn = max_low_pfn;
3460 blk_max_pfn = max_pfn;
3461
3462 return 0;
3463}
3464
3465/*
3466 * IO Context helper functions
3467 */
3468void put_io_context(struct io_context *ioc)
3469{
3470 if (ioc == NULL)
3471 return;
3472
3473 BUG_ON(atomic_read(&ioc->refcount) == 0);
3474
3475 if (atomic_dec_and_test(&ioc->refcount)) {
3476 if (ioc->aic && ioc->aic->dtor)
3477 ioc->aic->dtor(ioc->aic);
3478 if (ioc->cic && ioc->cic->dtor)
3479 ioc->cic->dtor(ioc->cic);
3480
3481 kmem_cache_free(iocontext_cachep, ioc);
3482 }
3483}
3484EXPORT_SYMBOL(put_io_context);
3485
3486/* Called by the exitting task */
3487void exit_io_context(void)
3488{
3489 unsigned long flags;
3490 struct io_context *ioc;
3491
3492 local_irq_save(flags);
Jens Axboe22e2c502005-06-27 10:55:12 +02003493 task_lock(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 ioc = current->io_context;
3495 current->io_context = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02003496 ioc->task = NULL;
3497 task_unlock(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 local_irq_restore(flags);
3499
3500 if (ioc->aic && ioc->aic->exit)
3501 ioc->aic->exit(ioc->aic);
3502 if (ioc->cic && ioc->cic->exit)
3503 ioc->cic->exit(ioc->cic);
3504
3505 put_io_context(ioc);
3506}
3507
3508/*
3509 * If the current task has no IO context then create one and initialise it.
Nick Pigginfb3cc432005-06-28 20:45:15 -07003510 * Otherwise, return its existing IO context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 *
Nick Pigginfb3cc432005-06-28 20:45:15 -07003512 * This returned IO context doesn't have a specifically elevated refcount,
3513 * but since the current task itself holds a reference, the context can be
3514 * used in general code, so long as it stays within `current` context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 */
Al Viro8267e262005-10-21 03:20:53 -04003516struct io_context *current_io_context(gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517{
3518 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 struct io_context *ret;
3520
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 ret = tsk->io_context;
Nick Pigginfb3cc432005-06-28 20:45:15 -07003522 if (likely(ret))
3523 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524
3525 ret = kmem_cache_alloc(iocontext_cachep, gfp_flags);
3526 if (ret) {
3527 atomic_set(&ret->refcount, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02003528 ret->task = current;
3529 ret->set_ioprio = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 ret->last_waited = jiffies; /* doesn't matter... */
3531 ret->nr_batch_requests = 0; /* because this is 0 */
3532 ret->aic = NULL;
3533 ret->cic = NULL;
Nick Pigginfb3cc432005-06-28 20:45:15 -07003534 tsk->io_context = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 }
3536
3537 return ret;
3538}
Nick Pigginfb3cc432005-06-28 20:45:15 -07003539EXPORT_SYMBOL(current_io_context);
3540
3541/*
3542 * If the current task has no IO context then create one and initialise it.
3543 * If it does have a context, take a ref on it.
3544 *
3545 * This is always called in the context of the task which submitted the I/O.
3546 */
Al Viro8267e262005-10-21 03:20:53 -04003547struct io_context *get_io_context(gfp_t gfp_flags)
Nick Pigginfb3cc432005-06-28 20:45:15 -07003548{
3549 struct io_context *ret;
3550 ret = current_io_context(gfp_flags);
3551 if (likely(ret))
3552 atomic_inc(&ret->refcount);
3553 return ret;
3554}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555EXPORT_SYMBOL(get_io_context);
3556
3557void copy_io_context(struct io_context **pdst, struct io_context **psrc)
3558{
3559 struct io_context *src = *psrc;
3560 struct io_context *dst = *pdst;
3561
3562 if (src) {
3563 BUG_ON(atomic_read(&src->refcount) == 0);
3564 atomic_inc(&src->refcount);
3565 put_io_context(dst);
3566 *pdst = src;
3567 }
3568}
3569EXPORT_SYMBOL(copy_io_context);
3570
3571void swap_io_context(struct io_context **ioc1, struct io_context **ioc2)
3572{
3573 struct io_context *temp;
3574 temp = *ioc1;
3575 *ioc1 = *ioc2;
3576 *ioc2 = temp;
3577}
3578EXPORT_SYMBOL(swap_io_context);
3579
3580/*
3581 * sysfs parts below
3582 */
3583struct queue_sysfs_entry {
3584 struct attribute attr;
3585 ssize_t (*show)(struct request_queue *, char *);
3586 ssize_t (*store)(struct request_queue *, const char *, size_t);
3587};
3588
3589static ssize_t
3590queue_var_show(unsigned int var, char *page)
3591{
3592 return sprintf(page, "%d\n", var);
3593}
3594
3595static ssize_t
3596queue_var_store(unsigned long *var, const char *page, size_t count)
3597{
3598 char *p = (char *) page;
3599
3600 *var = simple_strtoul(p, &p, 10);
3601 return count;
3602}
3603
3604static ssize_t queue_requests_show(struct request_queue *q, char *page)
3605{
3606 return queue_var_show(q->nr_requests, (page));
3607}
3608
3609static ssize_t
3610queue_requests_store(struct request_queue *q, const char *page, size_t count)
3611{
3612 struct request_list *rl = &q->rq;
3613
3614 int ret = queue_var_store(&q->nr_requests, page, count);
3615 if (q->nr_requests < BLKDEV_MIN_RQ)
3616 q->nr_requests = BLKDEV_MIN_RQ;
3617 blk_queue_congestion_threshold(q);
3618
3619 if (rl->count[READ] >= queue_congestion_on_threshold(q))
3620 set_queue_congested(q, READ);
3621 else if (rl->count[READ] < queue_congestion_off_threshold(q))
3622 clear_queue_congested(q, READ);
3623
3624 if (rl->count[WRITE] >= queue_congestion_on_threshold(q))
3625 set_queue_congested(q, WRITE);
3626 else if (rl->count[WRITE] < queue_congestion_off_threshold(q))
3627 clear_queue_congested(q, WRITE);
3628
3629 if (rl->count[READ] >= q->nr_requests) {
3630 blk_set_queue_full(q, READ);
3631 } else if (rl->count[READ]+1 <= q->nr_requests) {
3632 blk_clear_queue_full(q, READ);
3633 wake_up(&rl->wait[READ]);
3634 }
3635
3636 if (rl->count[WRITE] >= q->nr_requests) {
3637 blk_set_queue_full(q, WRITE);
3638 } else if (rl->count[WRITE]+1 <= q->nr_requests) {
3639 blk_clear_queue_full(q, WRITE);
3640 wake_up(&rl->wait[WRITE]);
3641 }
3642 return ret;
3643}
3644
3645static ssize_t queue_ra_show(struct request_queue *q, char *page)
3646{
3647 int ra_kb = q->backing_dev_info.ra_pages << (PAGE_CACHE_SHIFT - 10);
3648
3649 return queue_var_show(ra_kb, (page));
3650}
3651
3652static ssize_t
3653queue_ra_store(struct request_queue *q, const char *page, size_t count)
3654{
3655 unsigned long ra_kb;
3656 ssize_t ret = queue_var_store(&ra_kb, page, count);
3657
3658 spin_lock_irq(q->queue_lock);
3659 if (ra_kb > (q->max_sectors >> 1))
3660 ra_kb = (q->max_sectors >> 1);
3661
3662 q->backing_dev_info.ra_pages = ra_kb >> (PAGE_CACHE_SHIFT - 10);
3663 spin_unlock_irq(q->queue_lock);
3664
3665 return ret;
3666}
3667
3668static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
3669{
3670 int max_sectors_kb = q->max_sectors >> 1;
3671
3672 return queue_var_show(max_sectors_kb, (page));
3673}
3674
3675static ssize_t
3676queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
3677{
3678 unsigned long max_sectors_kb,
3679 max_hw_sectors_kb = q->max_hw_sectors >> 1,
3680 page_kb = 1 << (PAGE_CACHE_SHIFT - 10);
3681 ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
3682 int ra_kb;
3683
3684 if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
3685 return -EINVAL;
3686 /*
3687 * Take the queue lock to update the readahead and max_sectors
3688 * values synchronously:
3689 */
3690 spin_lock_irq(q->queue_lock);
3691 /*
3692 * Trim readahead window as well, if necessary:
3693 */
3694 ra_kb = q->backing_dev_info.ra_pages << (PAGE_CACHE_SHIFT - 10);
3695 if (ra_kb > max_sectors_kb)
3696 q->backing_dev_info.ra_pages =
3697 max_sectors_kb >> (PAGE_CACHE_SHIFT - 10);
3698
3699 q->max_sectors = max_sectors_kb << 1;
3700 spin_unlock_irq(q->queue_lock);
3701
3702 return ret;
3703}
3704
3705static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
3706{
3707 int max_hw_sectors_kb = q->max_hw_sectors >> 1;
3708
3709 return queue_var_show(max_hw_sectors_kb, (page));
3710}
3711
3712
3713static struct queue_sysfs_entry queue_requests_entry = {
3714 .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
3715 .show = queue_requests_show,
3716 .store = queue_requests_store,
3717};
3718
3719static struct queue_sysfs_entry queue_ra_entry = {
3720 .attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR },
3721 .show = queue_ra_show,
3722 .store = queue_ra_store,
3723};
3724
3725static struct queue_sysfs_entry queue_max_sectors_entry = {
3726 .attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR },
3727 .show = queue_max_sectors_show,
3728 .store = queue_max_sectors_store,
3729};
3730
3731static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
3732 .attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO },
3733 .show = queue_max_hw_sectors_show,
3734};
3735
3736static struct queue_sysfs_entry queue_iosched_entry = {
3737 .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR },
3738 .show = elv_iosched_show,
3739 .store = elv_iosched_store,
3740};
3741
3742static struct attribute *default_attrs[] = {
3743 &queue_requests_entry.attr,
3744 &queue_ra_entry.attr,
3745 &queue_max_hw_sectors_entry.attr,
3746 &queue_max_sectors_entry.attr,
3747 &queue_iosched_entry.attr,
3748 NULL,
3749};
3750
3751#define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
3752
3753static ssize_t
3754queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3755{
3756 struct queue_sysfs_entry *entry = to_queue(attr);
3757 struct request_queue *q;
3758
3759 q = container_of(kobj, struct request_queue, kobj);
3760 if (!entry->show)
Dmitry Torokhov6c1852a2005-04-29 01:26:06 -05003761 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762
3763 return entry->show(q, page);
3764}
3765
3766static ssize_t
3767queue_attr_store(struct kobject *kobj, struct attribute *attr,
3768 const char *page, size_t length)
3769{
3770 struct queue_sysfs_entry *entry = to_queue(attr);
3771 struct request_queue *q;
3772
3773 q = container_of(kobj, struct request_queue, kobj);
3774 if (!entry->store)
Dmitry Torokhov6c1852a2005-04-29 01:26:06 -05003775 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
3777 return entry->store(q, page, length);
3778}
3779
3780static struct sysfs_ops queue_sysfs_ops = {
3781 .show = queue_attr_show,
3782 .store = queue_attr_store,
3783};
3784
Adrian Bunk93d17d32005-06-25 14:59:10 -07003785static struct kobj_type queue_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 .sysfs_ops = &queue_sysfs_ops,
3787 .default_attrs = default_attrs,
3788};
3789
3790int blk_register_queue(struct gendisk *disk)
3791{
3792 int ret;
3793
3794 request_queue_t *q = disk->queue;
3795
3796 if (!q || !q->request_fn)
3797 return -ENXIO;
3798
3799 q->kobj.parent = kobject_get(&disk->kobj);
3800 if (!q->kobj.parent)
3801 return -EBUSY;
3802
3803 snprintf(q->kobj.name, KOBJ_NAME_LEN, "%s", "queue");
3804 q->kobj.ktype = &queue_ktype;
3805
3806 ret = kobject_register(&q->kobj);
3807 if (ret < 0)
3808 return ret;
3809
3810 ret = elv_register_queue(q);
3811 if (ret) {
3812 kobject_unregister(&q->kobj);
3813 return ret;
3814 }
3815
3816 return 0;
3817}
3818
3819void blk_unregister_queue(struct gendisk *disk)
3820{
3821 request_queue_t *q = disk->queue;
3822
3823 if (q && q->request_fn) {
3824 elv_unregister_queue(q);
3825
3826 kobject_unregister(&q->kobj);
3827 kobject_put(&disk->kobj);
3828 }
3829}