blob: 3b6aad2affd80f9a47dc82c5f59082b37f43cc59 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 1994, Karl Keyte: Added support for disk statistics
4 * Elevator latency, (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
5 * Queue request tables / lock, selectable elevator, Jens Axboe <axboe@suse.de>
6 * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au> - July2000
7 * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
8 */
9
10/*
11 * This handles all read/write requests to block devices
12 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/backing-dev.h>
16#include <linux/bio.h>
17#include <linux/blkdev.h>
18#include <linux/highmem.h>
19#include <linux/mm.h>
20#include <linux/kernel_stat.h>
21#include <linux/string.h>
22#include <linux/init.h>
23#include <linux/bootmem.h> /* for max_pfn/max_low_pfn */
24#include <linux/completion.h>
25#include <linux/slab.h>
26#include <linux/swap.h>
27#include <linux/writeback.h>
Jens Axboeff856ba2006-01-09 16:02:34 +010028#include <linux/interrupt.h>
29#include <linux/cpu.h>
Jens Axboe2056a782006-03-23 20:00:26 +010030#include <linux/blktrace_api.h>
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 Axboe2e662b62006-07-13 11:55:04 +0200288 INIT_HLIST_NODE(&rq->hash);
289 RB_CLEAR_NODE(&rq->rb_node);
Jens Axboe22e2c502005-06-27 10:55:12 +0200290 rq->ioprio = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 rq->buffer = NULL;
292 rq->ref_count = 1;
293 rq->q = q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 rq->special = NULL;
295 rq->data_len = 0;
296 rq->data = NULL;
Mike Christie df46b9a2005-06-20 14:04:44 +0200297 rq->nr_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 rq->sense = NULL;
299 rq->end_io = NULL;
300 rq->end_io_data = NULL;
Jens Axboeff856ba2006-01-09 16:02:34 +0100301 rq->completion_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
304/**
305 * blk_queue_ordered - does this queue support ordered writes
Tejun Heo797e7db2006-01-06 09:51:03 +0100306 * @q: the request queue
307 * @ordered: one of QUEUE_ORDERED_*
Jens Axboefddfdea2006-01-31 15:24:34 +0100308 * @prepare_flush_fn: rq setup helper for cache flush ordered writes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 *
310 * Description:
311 * For journalled file systems, doing ordered writes on a commit
312 * block instead of explicitly doing wait_on_buffer (which is bad
313 * for performance) can be a big win. Block drivers supporting this
314 * feature should call this function and indicate so.
315 *
316 **/
Tejun Heo797e7db2006-01-06 09:51:03 +0100317int blk_queue_ordered(request_queue_t *q, unsigned ordered,
318 prepare_flush_fn *prepare_flush_fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Tejun Heo797e7db2006-01-06 09:51:03 +0100320 if (ordered & (QUEUE_ORDERED_PREFLUSH | QUEUE_ORDERED_POSTFLUSH) &&
321 prepare_flush_fn == NULL) {
322 printk(KERN_ERR "blk_queue_ordered: prepare_flush_fn required\n");
323 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100325
326 if (ordered != QUEUE_ORDERED_NONE &&
327 ordered != QUEUE_ORDERED_DRAIN &&
328 ordered != QUEUE_ORDERED_DRAIN_FLUSH &&
329 ordered != QUEUE_ORDERED_DRAIN_FUA &&
330 ordered != QUEUE_ORDERED_TAG &&
331 ordered != QUEUE_ORDERED_TAG_FLUSH &&
332 ordered != QUEUE_ORDERED_TAG_FUA) {
333 printk(KERN_ERR "blk_queue_ordered: bad value %d\n", ordered);
334 return -EINVAL;
335 }
336
Tetsuo Takata60481b12006-01-24 10:34:36 +0100337 q->ordered = ordered;
Tejun Heo797e7db2006-01-06 09:51:03 +0100338 q->next_ordered = ordered;
339 q->prepare_flush_fn = prepare_flush_fn;
340
341 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
344EXPORT_SYMBOL(blk_queue_ordered);
345
346/**
347 * blk_queue_issue_flush_fn - set function for issuing a flush
348 * @q: the request queue
349 * @iff: the function to be called issuing the flush
350 *
351 * Description:
352 * If a driver supports issuing a flush command, the support is notified
353 * to the block layer by defining it through this call.
354 *
355 **/
356void blk_queue_issue_flush_fn(request_queue_t *q, issue_flush_fn *iff)
357{
358 q->issue_flush_fn = iff;
359}
360
361EXPORT_SYMBOL(blk_queue_issue_flush_fn);
362
363/*
364 * Cache flushing for ordered writes handling
365 */
Tejun Heo797e7db2006-01-06 09:51:03 +0100366inline unsigned blk_ordered_cur_seq(request_queue_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Tejun Heo797e7db2006-01-06 09:51:03 +0100368 if (!q->ordseq)
369 return 0;
370 return 1 << ffz(q->ordseq);
371}
372
373unsigned blk_ordered_req_seq(struct request *rq)
374{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 request_queue_t *q = rq->q;
376
Tejun Heo797e7db2006-01-06 09:51:03 +0100377 BUG_ON(q->ordseq == 0);
Tejun Heo8922e162005-10-20 16:23:44 +0200378
Tejun Heo797e7db2006-01-06 09:51:03 +0100379 if (rq == &q->pre_flush_rq)
380 return QUEUE_ORDSEQ_PREFLUSH;
381 if (rq == &q->bar_rq)
382 return QUEUE_ORDSEQ_BAR;
383 if (rq == &q->post_flush_rq)
384 return QUEUE_ORDSEQ_POSTFLUSH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Jens Axboe4aff5e22006-08-10 08:44:47 +0200386 if ((rq->cmd_flags & REQ_ORDERED_COLOR) ==
387 (q->orig_bar_rq->cmd_flags & REQ_ORDERED_COLOR))
Tejun Heo797e7db2006-01-06 09:51:03 +0100388 return QUEUE_ORDSEQ_DRAIN;
389 else
390 return QUEUE_ORDSEQ_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
Tejun Heo797e7db2006-01-06 09:51:03 +0100393void blk_ordered_complete_seq(request_queue_t *q, unsigned seq, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Tejun Heo797e7db2006-01-06 09:51:03 +0100395 struct request *rq;
396 int uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Tejun Heo797e7db2006-01-06 09:51:03 +0100398 if (error && !q->orderr)
399 q->orderr = error;
Tejun Heo8922e162005-10-20 16:23:44 +0200400
Tejun Heo797e7db2006-01-06 09:51:03 +0100401 BUG_ON(q->ordseq & seq);
402 q->ordseq |= seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Tejun Heo797e7db2006-01-06 09:51:03 +0100404 if (blk_ordered_cur_seq(q) != QUEUE_ORDSEQ_DONE)
405 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 /*
Tejun Heo797e7db2006-01-06 09:51:03 +0100408 * Okay, sequence complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 */
Tejun Heo797e7db2006-01-06 09:51:03 +0100410 rq = q->orig_bar_rq;
411 uptodate = q->orderr ? q->orderr : 1;
412
413 q->ordseq = 0;
414
415 end_that_request_first(rq, uptodate, rq->hard_nr_sectors);
416 end_that_request_last(rq, uptodate);
417}
418
419static void pre_flush_end_io(struct request *rq, int error)
420{
421 elv_completed_request(rq->q, rq);
422 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_PREFLUSH, error);
423}
424
425static void bar_end_io(struct request *rq, int error)
426{
427 elv_completed_request(rq->q, rq);
428 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_BAR, error);
429}
430
431static void post_flush_end_io(struct request *rq, int error)
432{
433 elv_completed_request(rq->q, rq);
434 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_POSTFLUSH, error);
435}
436
437static void queue_flush(request_queue_t *q, unsigned which)
438{
439 struct request *rq;
440 rq_end_io_fn *end_io;
441
442 if (which == QUEUE_ORDERED_PREFLUSH) {
443 rq = &q->pre_flush_rq;
444 end_io = pre_flush_end_io;
445 } else {
446 rq = &q->post_flush_rq;
447 end_io = post_flush_end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449
Jens Axboe4aff5e22006-08-10 08:44:47 +0200450 rq->cmd_flags = REQ_HARDBARRIER;
Tejun Heo797e7db2006-01-06 09:51:03 +0100451 rq_init(q, rq);
Tejun Heo797e7db2006-01-06 09:51:03 +0100452 rq->elevator_private = NULL;
Jens Axboec00895a2006-09-30 20:29:12 +0200453 rq->elevator_private2 = NULL;
Tejun Heo797e7db2006-01-06 09:51:03 +0100454 rq->rq_disk = q->bar_rq.rq_disk;
455 rq->rl = NULL;
456 rq->end_io = end_io;
457 q->prepare_flush_fn(q, rq);
458
Tejun Heo30e96562006-02-08 01:01:31 -0800459 elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
Tejun Heo797e7db2006-01-06 09:51:03 +0100460}
461
462static inline struct request *start_ordered(request_queue_t *q,
463 struct request *rq)
464{
465 q->bi_size = 0;
466 q->orderr = 0;
467 q->ordered = q->next_ordered;
468 q->ordseq |= QUEUE_ORDSEQ_STARTED;
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 /*
Tejun Heo797e7db2006-01-06 09:51:03 +0100471 * Prep proxy barrier request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 */
Tejun Heo797e7db2006-01-06 09:51:03 +0100473 blkdev_dequeue_request(rq);
474 q->orig_bar_rq = rq;
475 rq = &q->bar_rq;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200476 rq->cmd_flags = 0;
Tejun Heo797e7db2006-01-06 09:51:03 +0100477 rq_init(q, rq);
Jens Axboe4aff5e22006-08-10 08:44:47 +0200478 if (bio_data_dir(q->orig_bar_rq->bio) == WRITE)
479 rq->cmd_flags |= REQ_RW;
480 rq->cmd_flags |= q->ordered & QUEUE_ORDERED_FUA ? REQ_FUA : 0;
Tejun Heo797e7db2006-01-06 09:51:03 +0100481 rq->elevator_private = NULL;
Jens Axboec00895a2006-09-30 20:29:12 +0200482 rq->elevator_private2 = NULL;
Tejun Heo797e7db2006-01-06 09:51:03 +0100483 rq->rl = NULL;
484 init_request_from_bio(rq, q->orig_bar_rq->bio);
485 rq->end_io = bar_end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Tejun Heo797e7db2006-01-06 09:51:03 +0100487 /*
488 * Queue ordered sequence. As we stack them at the head, we
489 * need to queue in reverse order. Note that we rely on that
490 * no fs request uses ELEVATOR_INSERT_FRONT and thus no fs
491 * request gets inbetween ordered sequence.
492 */
493 if (q->ordered & QUEUE_ORDERED_POSTFLUSH)
494 queue_flush(q, QUEUE_ORDERED_POSTFLUSH);
495 else
496 q->ordseq |= QUEUE_ORDSEQ_POSTFLUSH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Tejun Heo30e96562006-02-08 01:01:31 -0800498 elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
Tejun Heo797e7db2006-01-06 09:51:03 +0100499
500 if (q->ordered & QUEUE_ORDERED_PREFLUSH) {
501 queue_flush(q, QUEUE_ORDERED_PREFLUSH);
502 rq = &q->pre_flush_rq;
503 } else
504 q->ordseq |= QUEUE_ORDSEQ_PREFLUSH;
505
506 if ((q->ordered & QUEUE_ORDERED_TAG) || q->in_flight == 0)
507 q->ordseq |= QUEUE_ORDSEQ_DRAIN;
508 else
509 rq = NULL;
510
511 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
Tejun Heo797e7db2006-01-06 09:51:03 +0100514int blk_do_ordered(request_queue_t *q, struct request **rqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Jens Axboe9a7a67a2006-02-04 23:27:38 -0800516 struct request *rq = *rqp;
Tejun Heo797e7db2006-01-06 09:51:03 +0100517 int is_barrier = blk_fs_request(rq) && blk_barrier_rq(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Tejun Heo797e7db2006-01-06 09:51:03 +0100519 if (!q->ordseq) {
520 if (!is_barrier)
521 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Tejun Heo797e7db2006-01-06 09:51:03 +0100523 if (q->next_ordered != QUEUE_ORDERED_NONE) {
524 *rqp = start_ordered(q, rq);
525 return 1;
526 } else {
527 /*
528 * This can happen when the queue switches to
529 * ORDERED_NONE while this request is on it.
530 */
531 blkdev_dequeue_request(rq);
532 end_that_request_first(rq, -EOPNOTSUPP,
533 rq->hard_nr_sectors);
534 end_that_request_last(rq, -EOPNOTSUPP);
535 *rqp = NULL;
536 return 0;
537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Jens Axboe9a7a67a2006-02-04 23:27:38 -0800540 /*
541 * Ordered sequence in progress
542 */
543
544 /* Special requests are not subject to ordering rules. */
545 if (!blk_fs_request(rq) &&
546 rq != &q->pre_flush_rq && rq != &q->post_flush_rq)
547 return 1;
548
Tejun Heo797e7db2006-01-06 09:51:03 +0100549 if (q->ordered & QUEUE_ORDERED_TAG) {
Jens Axboe9a7a67a2006-02-04 23:27:38 -0800550 /* Ordered by tag. Blocking the next barrier is enough. */
Tejun Heo797e7db2006-01-06 09:51:03 +0100551 if (is_barrier && rq != &q->bar_rq)
552 *rqp = NULL;
Jens Axboe9a7a67a2006-02-04 23:27:38 -0800553 } else {
554 /* Ordered by draining. Wait for turn. */
555 WARN_ON(blk_ordered_req_seq(rq) < blk_ordered_cur_seq(q));
556 if (blk_ordered_req_seq(rq) > blk_ordered_cur_seq(q))
557 *rqp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559
560 return 1;
561}
562
Tejun Heo797e7db2006-01-06 09:51:03 +0100563static int flush_dry_bio_endio(struct bio *bio, unsigned int bytes, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Tejun Heo797e7db2006-01-06 09:51:03 +0100565 request_queue_t *q = bio->bi_private;
566 struct bio_vec *bvec;
567 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Tejun Heo797e7db2006-01-06 09:51:03 +0100569 /*
570 * This is dry run, restore bio_sector and size. We'll finish
571 * this request again with the original bi_end_io after an
572 * error occurs or post flush is complete.
573 */
574 q->bi_size += bytes;
575
576 if (bio->bi_size)
577 return 1;
578
579 /* Rewind bvec's */
580 bio->bi_idx = 0;
581 bio_for_each_segment(bvec, bio, i) {
582 bvec->bv_len += bvec->bv_offset;
583 bvec->bv_offset = 0;
584 }
585
586 /* Reset bio */
587 set_bit(BIO_UPTODATE, &bio->bi_flags);
588 bio->bi_size = q->bi_size;
589 bio->bi_sector -= (q->bi_size >> 9);
590 q->bi_size = 0;
591
592 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
Tejun Heo797e7db2006-01-06 09:51:03 +0100594
595static inline int ordered_bio_endio(struct request *rq, struct bio *bio,
596 unsigned int nbytes, int error)
597{
598 request_queue_t *q = rq->q;
599 bio_end_io_t *endio;
600 void *private;
601
602 if (&q->bar_rq != rq)
603 return 0;
604
605 /*
606 * Okay, this is the barrier request in progress, dry finish it.
607 */
608 if (error && !q->orderr)
609 q->orderr = error;
610
611 endio = bio->bi_end_io;
612 private = bio->bi_private;
613 bio->bi_end_io = flush_dry_bio_endio;
614 bio->bi_private = q;
615
616 bio_endio(bio, nbytes, error);
617
618 bio->bi_end_io = endio;
619 bio->bi_private = private;
620
621 return 1;
622}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624/**
625 * blk_queue_bounce_limit - set bounce buffer limit for queue
626 * @q: the request queue for the device
627 * @dma_addr: bus address limit
628 *
629 * Description:
630 * Different hardware can have different requirements as to what pages
631 * it can do I/O directly to. A low level driver can call
632 * blk_queue_bounce_limit to have lower memory pages allocated as bounce
Andi Kleen5ee1af92006-03-08 17:57:26 -0800633 * buffers for doing I/O to pages residing above @page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 **/
635void blk_queue_bounce_limit(request_queue_t *q, u64 dma_addr)
636{
637 unsigned long bounce_pfn = dma_addr >> PAGE_SHIFT;
Andi Kleen5ee1af92006-03-08 17:57:26 -0800638 int dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Andi Kleen5ee1af92006-03-08 17:57:26 -0800640 q->bounce_gfp = GFP_NOIO;
641#if BITS_PER_LONG == 64
642 /* Assume anything <= 4GB can be handled by IOMMU.
643 Actually some IOMMUs can handle everything, but I don't
644 know of a way to test this here. */
Andi Kleen82697302006-06-21 14:48:09 +0200645 if (bounce_pfn < (min_t(u64,0xffffffff,BLK_BOUNCE_HIGH) >> PAGE_SHIFT))
Andi Kleen5ee1af92006-03-08 17:57:26 -0800646 dma = 1;
647 q->bounce_pfn = max_low_pfn;
648#else
649 if (bounce_pfn < blk_max_low_pfn)
650 dma = 1;
651 q->bounce_pfn = bounce_pfn;
652#endif
653 if (dma) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 init_emergency_isa_pool();
655 q->bounce_gfp = GFP_NOIO | GFP_DMA;
Andi Kleen5ee1af92006-03-08 17:57:26 -0800656 q->bounce_pfn = bounce_pfn;
657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
660EXPORT_SYMBOL(blk_queue_bounce_limit);
661
662/**
663 * blk_queue_max_sectors - set max sectors for a request for this queue
664 * @q: the request queue for the device
665 * @max_sectors: max sectors in the usual 512b unit
666 *
667 * Description:
668 * Enables a low level driver to set an upper limit on the size of
669 * received requests.
670 **/
Jens Axboe2cb2e142006-01-17 09:04:32 +0100671void blk_queue_max_sectors(request_queue_t *q, unsigned int max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 if ((max_sectors << 9) < PAGE_CACHE_SIZE) {
674 max_sectors = 1 << (PAGE_CACHE_SHIFT - 9);
675 printk("%s: set to minimum %d\n", __FUNCTION__, max_sectors);
676 }
677
Mike Christiedefd94b2005-12-05 02:37:06 -0600678 if (BLK_DEF_MAX_SECTORS > max_sectors)
679 q->max_hw_sectors = q->max_sectors = max_sectors;
680 else {
681 q->max_sectors = BLK_DEF_MAX_SECTORS;
682 q->max_hw_sectors = max_sectors;
683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
686EXPORT_SYMBOL(blk_queue_max_sectors);
687
688/**
689 * blk_queue_max_phys_segments - set max phys segments for a request for this queue
690 * @q: the request queue for the device
691 * @max_segments: max number of segments
692 *
693 * Description:
694 * Enables a low level driver to set an upper limit on the number of
695 * physical data segments in a request. This would be the largest sized
696 * scatter list the driver could handle.
697 **/
698void blk_queue_max_phys_segments(request_queue_t *q, unsigned short max_segments)
699{
700 if (!max_segments) {
701 max_segments = 1;
702 printk("%s: set to minimum %d\n", __FUNCTION__, max_segments);
703 }
704
705 q->max_phys_segments = max_segments;
706}
707
708EXPORT_SYMBOL(blk_queue_max_phys_segments);
709
710/**
711 * blk_queue_max_hw_segments - set max hw segments for a request for this queue
712 * @q: the request queue for the device
713 * @max_segments: max number of segments
714 *
715 * Description:
716 * Enables a low level driver to set an upper limit on the number of
717 * hw data segments in a request. This would be the largest number of
718 * address/length pairs the host adapter can actually give as once
719 * to the device.
720 **/
721void blk_queue_max_hw_segments(request_queue_t *q, unsigned short max_segments)
722{
723 if (!max_segments) {
724 max_segments = 1;
725 printk("%s: set to minimum %d\n", __FUNCTION__, max_segments);
726 }
727
728 q->max_hw_segments = max_segments;
729}
730
731EXPORT_SYMBOL(blk_queue_max_hw_segments);
732
733/**
734 * blk_queue_max_segment_size - set max segment size for blk_rq_map_sg
735 * @q: the request queue for the device
736 * @max_size: max size of segment in bytes
737 *
738 * Description:
739 * Enables a low level driver to set an upper limit on the size of a
740 * coalesced segment
741 **/
742void blk_queue_max_segment_size(request_queue_t *q, unsigned int max_size)
743{
744 if (max_size < PAGE_CACHE_SIZE) {
745 max_size = PAGE_CACHE_SIZE;
746 printk("%s: set to minimum %d\n", __FUNCTION__, max_size);
747 }
748
749 q->max_segment_size = max_size;
750}
751
752EXPORT_SYMBOL(blk_queue_max_segment_size);
753
754/**
755 * blk_queue_hardsect_size - set hardware sector size for the queue
756 * @q: the request queue for the device
757 * @size: the hardware sector size, in bytes
758 *
759 * Description:
760 * This should typically be set to the lowest possible sector size
761 * that the hardware can operate on (possible without reverting to
762 * even internal read-modify-write operations). Usually the default
763 * of 512 covers most hardware.
764 **/
765void blk_queue_hardsect_size(request_queue_t *q, unsigned short size)
766{
767 q->hardsect_size = size;
768}
769
770EXPORT_SYMBOL(blk_queue_hardsect_size);
771
772/*
773 * Returns the minimum that is _not_ zero, unless both are zero.
774 */
775#define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
776
777/**
778 * blk_queue_stack_limits - inherit underlying queue limits for stacked drivers
779 * @t: the stacking driver (top)
780 * @b: the underlying device (bottom)
781 **/
782void blk_queue_stack_limits(request_queue_t *t, request_queue_t *b)
783{
784 /* zero is "infinity" */
Mike Christiedefd94b2005-12-05 02:37:06 -0600785 t->max_sectors = min_not_zero(t->max_sectors,b->max_sectors);
786 t->max_hw_sectors = min_not_zero(t->max_hw_sectors,b->max_hw_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 t->max_phys_segments = min(t->max_phys_segments,b->max_phys_segments);
789 t->max_hw_segments = min(t->max_hw_segments,b->max_hw_segments);
790 t->max_segment_size = min(t->max_segment_size,b->max_segment_size);
791 t->hardsect_size = max(t->hardsect_size,b->hardsect_size);
NeilBrown89e5c8b2006-03-27 01:18:02 -0800792 if (!test_bit(QUEUE_FLAG_CLUSTER, &b->queue_flags))
793 clear_bit(QUEUE_FLAG_CLUSTER, &t->queue_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795
796EXPORT_SYMBOL(blk_queue_stack_limits);
797
798/**
799 * blk_queue_segment_boundary - set boundary rules for segment merging
800 * @q: the request queue for the device
801 * @mask: the memory boundary mask
802 **/
803void blk_queue_segment_boundary(request_queue_t *q, unsigned long mask)
804{
805 if (mask < PAGE_CACHE_SIZE - 1) {
806 mask = PAGE_CACHE_SIZE - 1;
807 printk("%s: set to minimum %lx\n", __FUNCTION__, mask);
808 }
809
810 q->seg_boundary_mask = mask;
811}
812
813EXPORT_SYMBOL(blk_queue_segment_boundary);
814
815/**
816 * blk_queue_dma_alignment - set dma length and memory alignment
817 * @q: the request queue for the device
818 * @mask: alignment mask
819 *
820 * description:
821 * set required memory and length aligment for direct dma transactions.
822 * this is used when buiding direct io requests for the queue.
823 *
824 **/
825void blk_queue_dma_alignment(request_queue_t *q, int mask)
826{
827 q->dma_alignment = mask;
828}
829
830EXPORT_SYMBOL(blk_queue_dma_alignment);
831
832/**
833 * blk_queue_find_tag - find a request by its tag and queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 * @q: The request queue for the device
835 * @tag: The tag of the request
836 *
837 * Notes:
838 * Should be used when a device returns a tag and you want to match
839 * it with a request.
840 *
841 * no locks need be held.
842 **/
843struct request *blk_queue_find_tag(request_queue_t *q, int tag)
844{
845 struct blk_queue_tag *bqt = q->queue_tags;
846
Tejun Heoba025082005-08-05 13:28:11 -0700847 if (unlikely(bqt == NULL || tag >= bqt->real_max_depth))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return NULL;
849
850 return bqt->tag_index[tag];
851}
852
853EXPORT_SYMBOL(blk_queue_find_tag);
854
855/**
James Bottomley492dfb42006-08-30 15:48:45 -0400856 * __blk_free_tags - release a given set of tag maintenance info
857 * @bqt: the tag map to free
858 *
859 * Tries to free the specified @bqt@. Returns true if it was
860 * actually freed and false if there are still references using it
861 */
862static int __blk_free_tags(struct blk_queue_tag *bqt)
863{
864 int retval;
865
866 retval = atomic_dec_and_test(&bqt->refcnt);
867 if (retval) {
868 BUG_ON(bqt->busy);
869 BUG_ON(!list_empty(&bqt->busy_list));
870
871 kfree(bqt->tag_index);
872 bqt->tag_index = NULL;
873
874 kfree(bqt->tag_map);
875 bqt->tag_map = NULL;
876
877 kfree(bqt);
878
879 }
880
881 return retval;
882}
883
884/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 * __blk_queue_free_tags - release tag maintenance info
886 * @q: the request queue for the device
887 *
888 * Notes:
889 * blk_cleanup_queue() will take care of calling this function, if tagging
890 * has been used. So there's no need to call this directly.
891 **/
892static void __blk_queue_free_tags(request_queue_t *q)
893{
894 struct blk_queue_tag *bqt = q->queue_tags;
895
896 if (!bqt)
897 return;
898
James Bottomley492dfb42006-08-30 15:48:45 -0400899 __blk_free_tags(bqt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 q->queue_tags = NULL;
902 q->queue_flags &= ~(1 << QUEUE_FLAG_QUEUED);
903}
904
James Bottomley492dfb42006-08-30 15:48:45 -0400905
906/**
907 * blk_free_tags - release a given set of tag maintenance info
908 * @bqt: the tag map to free
909 *
910 * For externally managed @bqt@ frees the map. Callers of this
911 * function must guarantee to have released all the queues that
912 * might have been using this tag map.
913 */
914void blk_free_tags(struct blk_queue_tag *bqt)
915{
916 if (unlikely(!__blk_free_tags(bqt)))
917 BUG();
918}
919EXPORT_SYMBOL(blk_free_tags);
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921/**
922 * blk_queue_free_tags - release tag maintenance info
923 * @q: the request queue for the device
924 *
925 * Notes:
926 * This is used to disabled tagged queuing to a device, yet leave
927 * queue in function.
928 **/
929void blk_queue_free_tags(request_queue_t *q)
930{
931 clear_bit(QUEUE_FLAG_QUEUED, &q->queue_flags);
932}
933
934EXPORT_SYMBOL(blk_queue_free_tags);
935
936static int
937init_tag_map(request_queue_t *q, struct blk_queue_tag *tags, int depth)
938{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 struct request **tag_index;
940 unsigned long *tag_map;
Tejun Heofa72b902005-06-23 00:08:49 -0700941 int nr_ulongs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
James Bottomley492dfb42006-08-30 15:48:45 -0400943 if (q && depth > q->nr_requests * 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 depth = q->nr_requests * 2;
945 printk(KERN_ERR "%s: adjusted depth to %d\n",
946 __FUNCTION__, depth);
947 }
948
Jens Axboef68110f2006-03-08 13:31:44 +0100949 tag_index = kzalloc(depth * sizeof(struct request *), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (!tag_index)
951 goto fail;
952
Tejun Heof7d37d02005-06-23 00:08:50 -0700953 nr_ulongs = ALIGN(depth, BITS_PER_LONG) / BITS_PER_LONG;
Jens Axboef68110f2006-03-08 13:31:44 +0100954 tag_map = kzalloc(nr_ulongs * sizeof(unsigned long), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (!tag_map)
956 goto fail;
957
Tejun Heoba025082005-08-05 13:28:11 -0700958 tags->real_max_depth = depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 tags->max_depth = depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 tags->tag_index = tag_index;
961 tags->tag_map = tag_map;
962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 return 0;
964fail:
965 kfree(tag_index);
966 return -ENOMEM;
967}
968
James Bottomley492dfb42006-08-30 15:48:45 -0400969static struct blk_queue_tag *__blk_queue_init_tags(struct request_queue *q,
970 int depth)
971{
972 struct blk_queue_tag *tags;
973
974 tags = kmalloc(sizeof(struct blk_queue_tag), GFP_ATOMIC);
975 if (!tags)
976 goto fail;
977
978 if (init_tag_map(q, tags, depth))
979 goto fail;
980
981 INIT_LIST_HEAD(&tags->busy_list);
982 tags->busy = 0;
983 atomic_set(&tags->refcnt, 1);
984 return tags;
985fail:
986 kfree(tags);
987 return NULL;
988}
989
990/**
991 * blk_init_tags - initialize the tag info for an external tag map
992 * @depth: the maximum queue depth supported
993 * @tags: the tag to use
994 **/
995struct blk_queue_tag *blk_init_tags(int depth)
996{
997 return __blk_queue_init_tags(NULL, depth);
998}
999EXPORT_SYMBOL(blk_init_tags);
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001/**
1002 * blk_queue_init_tags - initialize the queue tag info
1003 * @q: the request queue for the device
1004 * @depth: the maximum queue depth supported
1005 * @tags: the tag to use
1006 **/
1007int blk_queue_init_tags(request_queue_t *q, int depth,
1008 struct blk_queue_tag *tags)
1009{
1010 int rc;
1011
1012 BUG_ON(tags && q->queue_tags && tags != q->queue_tags);
1013
1014 if (!tags && !q->queue_tags) {
James Bottomley492dfb42006-08-30 15:48:45 -04001015 tags = __blk_queue_init_tags(q, depth);
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (!tags)
1018 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 } else if (q->queue_tags) {
1020 if ((rc = blk_queue_resize_tags(q, depth)))
1021 return rc;
1022 set_bit(QUEUE_FLAG_QUEUED, &q->queue_flags);
1023 return 0;
1024 } else
1025 atomic_inc(&tags->refcnt);
1026
1027 /*
1028 * assign it, all done
1029 */
1030 q->queue_tags = tags;
1031 q->queue_flags |= (1 << QUEUE_FLAG_QUEUED);
1032 return 0;
1033fail:
1034 kfree(tags);
1035 return -ENOMEM;
1036}
1037
1038EXPORT_SYMBOL(blk_queue_init_tags);
1039
1040/**
1041 * blk_queue_resize_tags - change the queueing depth
1042 * @q: the request queue for the device
1043 * @new_depth: the new max command queueing depth
1044 *
1045 * Notes:
1046 * Must be called with the queue lock held.
1047 **/
1048int blk_queue_resize_tags(request_queue_t *q, int new_depth)
1049{
1050 struct blk_queue_tag *bqt = q->queue_tags;
1051 struct request **tag_index;
1052 unsigned long *tag_map;
Tejun Heofa72b902005-06-23 00:08:49 -07001053 int max_depth, nr_ulongs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 if (!bqt)
1056 return -ENXIO;
1057
1058 /*
Tejun Heoba025082005-08-05 13:28:11 -07001059 * if we already have large enough real_max_depth. just
1060 * adjust max_depth. *NOTE* as requests with tag value
1061 * between new_depth and real_max_depth can be in-flight, tag
1062 * map can not be shrunk blindly here.
1063 */
1064 if (new_depth <= bqt->real_max_depth) {
1065 bqt->max_depth = new_depth;
1066 return 0;
1067 }
1068
1069 /*
James Bottomley492dfb42006-08-30 15:48:45 -04001070 * Currently cannot replace a shared tag map with a new
1071 * one, so error out if this is the case
1072 */
1073 if (atomic_read(&bqt->refcnt) != 1)
1074 return -EBUSY;
1075
1076 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 * save the old state info, so we can copy it back
1078 */
1079 tag_index = bqt->tag_index;
1080 tag_map = bqt->tag_map;
Tejun Heoba025082005-08-05 13:28:11 -07001081 max_depth = bqt->real_max_depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 if (init_tag_map(q, bqt, new_depth))
1084 return -ENOMEM;
1085
1086 memcpy(bqt->tag_index, tag_index, max_depth * sizeof(struct request *));
Tejun Heof7d37d02005-06-23 00:08:50 -07001087 nr_ulongs = ALIGN(max_depth, BITS_PER_LONG) / BITS_PER_LONG;
Tejun Heofa72b902005-06-23 00:08:49 -07001088 memcpy(bqt->tag_map, tag_map, nr_ulongs * sizeof(unsigned long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 kfree(tag_index);
1091 kfree(tag_map);
1092 return 0;
1093}
1094
1095EXPORT_SYMBOL(blk_queue_resize_tags);
1096
1097/**
1098 * blk_queue_end_tag - end tag operations for a request
1099 * @q: the request queue for the device
1100 * @rq: the request that has completed
1101 *
1102 * Description:
1103 * Typically called when end_that_request_first() returns 0, meaning
1104 * all transfers have been done for a request. It's important to call
1105 * this function before end_that_request_last(), as that will put the
1106 * request back on the free list thus corrupting the internal tag list.
1107 *
1108 * Notes:
1109 * queue lock must be held.
1110 **/
1111void blk_queue_end_tag(request_queue_t *q, struct request *rq)
1112{
1113 struct blk_queue_tag *bqt = q->queue_tags;
1114 int tag = rq->tag;
1115
1116 BUG_ON(tag == -1);
1117
Tejun Heoba025082005-08-05 13:28:11 -07001118 if (unlikely(tag >= bqt->real_max_depth))
Tejun Heo040c9282005-06-23 00:08:51 -07001119 /*
1120 * This can happen after tag depth has been reduced.
1121 * FIXME: how about a warning or info message here?
1122 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 return;
1124
1125 if (unlikely(!__test_and_clear_bit(tag, bqt->tag_map))) {
Tejun Heo040c9282005-06-23 00:08:51 -07001126 printk(KERN_ERR "%s: attempt to clear non-busy tag (%d)\n",
1127 __FUNCTION__, tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return;
1129 }
1130
1131 list_del_init(&rq->queuelist);
Jens Axboe4aff5e22006-08-10 08:44:47 +02001132 rq->cmd_flags &= ~REQ_QUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 rq->tag = -1;
1134
1135 if (unlikely(bqt->tag_index[tag] == NULL))
Tejun Heo040c9282005-06-23 00:08:51 -07001136 printk(KERN_ERR "%s: tag %d is missing\n",
1137 __FUNCTION__, tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 bqt->tag_index[tag] = NULL;
1140 bqt->busy--;
1141}
1142
1143EXPORT_SYMBOL(blk_queue_end_tag);
1144
1145/**
1146 * blk_queue_start_tag - find a free tag and assign it
1147 * @q: the request queue for the device
1148 * @rq: the block request that needs tagging
1149 *
1150 * Description:
1151 * This can either be used as a stand-alone helper, or possibly be
1152 * assigned as the queue &prep_rq_fn (in which case &struct request
1153 * automagically gets a tag assigned). Note that this function
1154 * assumes that any type of request can be queued! if this is not
1155 * true for your device, you must check the request type before
1156 * calling this function. The request will also be removed from
1157 * the request queue, so it's the drivers responsibility to readd
1158 * it if it should need to be restarted for some reason.
1159 *
1160 * Notes:
1161 * queue lock must be held.
1162 **/
1163int blk_queue_start_tag(request_queue_t *q, struct request *rq)
1164{
1165 struct blk_queue_tag *bqt = q->queue_tags;
Tejun Heo2bf0fda2005-06-23 00:08:48 -07001166 int tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Jens Axboe4aff5e22006-08-10 08:44:47 +02001168 if (unlikely((rq->cmd_flags & REQ_QUEUED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 printk(KERN_ERR
Tejun Heo040c9282005-06-23 00:08:51 -07001170 "%s: request %p for device [%s] already tagged %d",
1171 __FUNCTION__, rq,
1172 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 BUG();
1174 }
1175
Tejun Heo2bf0fda2005-06-23 00:08:48 -07001176 tag = find_first_zero_bit(bqt->tag_map, bqt->max_depth);
1177 if (tag >= bqt->max_depth)
1178 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 __set_bit(tag, bqt->tag_map);
1181
Jens Axboe4aff5e22006-08-10 08:44:47 +02001182 rq->cmd_flags |= REQ_QUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 rq->tag = tag;
1184 bqt->tag_index[tag] = rq;
1185 blkdev_dequeue_request(rq);
1186 list_add(&rq->queuelist, &bqt->busy_list);
1187 bqt->busy++;
1188 return 0;
1189}
1190
1191EXPORT_SYMBOL(blk_queue_start_tag);
1192
1193/**
1194 * blk_queue_invalidate_tags - invalidate all pending tags
1195 * @q: the request queue for the device
1196 *
1197 * Description:
1198 * Hardware conditions may dictate a need to stop all pending requests.
1199 * In this case, we will safely clear the block side of the tag queue and
1200 * readd all requests to the request queue in the right order.
1201 *
1202 * Notes:
1203 * queue lock must be held.
1204 **/
1205void blk_queue_invalidate_tags(request_queue_t *q)
1206{
1207 struct blk_queue_tag *bqt = q->queue_tags;
1208 struct list_head *tmp, *n;
1209 struct request *rq;
1210
1211 list_for_each_safe(tmp, n, &bqt->busy_list) {
1212 rq = list_entry_rq(tmp);
1213
1214 if (rq->tag == -1) {
Tejun Heo040c9282005-06-23 00:08:51 -07001215 printk(KERN_ERR
1216 "%s: bad tag found on list\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 list_del_init(&rq->queuelist);
Jens Axboe4aff5e22006-08-10 08:44:47 +02001218 rq->cmd_flags &= ~REQ_QUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 } else
1220 blk_queue_end_tag(q, rq);
1221
Jens Axboe4aff5e22006-08-10 08:44:47 +02001222 rq->cmd_flags &= ~REQ_STARTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 __elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 0);
1224 }
1225}
1226
1227EXPORT_SYMBOL(blk_queue_invalidate_tags);
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229void blk_dump_rq_flags(struct request *rq, char *msg)
1230{
1231 int bit;
1232
Jens Axboe4aff5e22006-08-10 08:44:47 +02001233 printk("%s: dev %s: type=%x, flags=%x\n", msg,
1234 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
1235 rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 printk("\nsector %llu, nr/cnr %lu/%u\n", (unsigned long long)rq->sector,
1238 rq->nr_sectors,
1239 rq->current_nr_sectors);
1240 printk("bio %p, biotail %p, buffer %p, data %p, len %u\n", rq->bio, rq->biotail, rq->buffer, rq->data, rq->data_len);
1241
Jens Axboe4aff5e22006-08-10 08:44:47 +02001242 if (blk_pc_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 printk("cdb: ");
1244 for (bit = 0; bit < sizeof(rq->cmd); bit++)
1245 printk("%02x ", rq->cmd[bit]);
1246 printk("\n");
1247 }
1248}
1249
1250EXPORT_SYMBOL(blk_dump_rq_flags);
1251
1252void blk_recount_segments(request_queue_t *q, struct bio *bio)
1253{
1254 struct bio_vec *bv, *bvprv = NULL;
1255 int i, nr_phys_segs, nr_hw_segs, seg_size, hw_seg_size, cluster;
1256 int high, highprv = 1;
1257
1258 if (unlikely(!bio->bi_io_vec))
1259 return;
1260
1261 cluster = q->queue_flags & (1 << QUEUE_FLAG_CLUSTER);
1262 hw_seg_size = seg_size = nr_phys_segs = nr_hw_segs = 0;
1263 bio_for_each_segment(bv, bio, i) {
1264 /*
1265 * the trick here is making sure that a high page is never
1266 * considered part of another segment, since that might
1267 * change with the bounce page.
1268 */
1269 high = page_to_pfn(bv->bv_page) >= q->bounce_pfn;
1270 if (high || highprv)
1271 goto new_hw_segment;
1272 if (cluster) {
1273 if (seg_size + bv->bv_len > q->max_segment_size)
1274 goto new_segment;
1275 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bv))
1276 goto new_segment;
1277 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bv))
1278 goto new_segment;
1279 if (BIOVEC_VIRT_OVERSIZE(hw_seg_size + bv->bv_len))
1280 goto new_hw_segment;
1281
1282 seg_size += bv->bv_len;
1283 hw_seg_size += bv->bv_len;
1284 bvprv = bv;
1285 continue;
1286 }
1287new_segment:
1288 if (BIOVEC_VIRT_MERGEABLE(bvprv, bv) &&
1289 !BIOVEC_VIRT_OVERSIZE(hw_seg_size + bv->bv_len)) {
1290 hw_seg_size += bv->bv_len;
1291 } else {
1292new_hw_segment:
1293 if (hw_seg_size > bio->bi_hw_front_size)
1294 bio->bi_hw_front_size = hw_seg_size;
1295 hw_seg_size = BIOVEC_VIRT_START_SIZE(bv) + bv->bv_len;
1296 nr_hw_segs++;
1297 }
1298
1299 nr_phys_segs++;
1300 bvprv = bv;
1301 seg_size = bv->bv_len;
1302 highprv = high;
1303 }
1304 if (hw_seg_size > bio->bi_hw_back_size)
1305 bio->bi_hw_back_size = hw_seg_size;
1306 if (nr_hw_segs == 1 && hw_seg_size > bio->bi_hw_front_size)
1307 bio->bi_hw_front_size = hw_seg_size;
1308 bio->bi_phys_segments = nr_phys_segs;
1309 bio->bi_hw_segments = nr_hw_segs;
1310 bio->bi_flags |= (1 << BIO_SEG_VALID);
1311}
1312
1313
Adrian Bunk93d17d32005-06-25 14:59:10 -07001314static int blk_phys_contig_segment(request_queue_t *q, struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 struct bio *nxt)
1316{
1317 if (!(q->queue_flags & (1 << QUEUE_FLAG_CLUSTER)))
1318 return 0;
1319
1320 if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)))
1321 return 0;
1322 if (bio->bi_size + nxt->bi_size > q->max_segment_size)
1323 return 0;
1324
1325 /*
1326 * bio and nxt are contigous in memory, check if the queue allows
1327 * these two to be merged into one
1328 */
1329 if (BIO_SEG_BOUNDARY(q, bio, nxt))
1330 return 1;
1331
1332 return 0;
1333}
1334
Adrian Bunk93d17d32005-06-25 14:59:10 -07001335static int blk_hw_contig_segment(request_queue_t *q, struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 struct bio *nxt)
1337{
1338 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
1339 blk_recount_segments(q, bio);
1340 if (unlikely(!bio_flagged(nxt, BIO_SEG_VALID)))
1341 blk_recount_segments(q, nxt);
1342 if (!BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)) ||
1343 BIOVEC_VIRT_OVERSIZE(bio->bi_hw_front_size + bio->bi_hw_back_size))
1344 return 0;
1345 if (bio->bi_size + nxt->bi_size > q->max_segment_size)
1346 return 0;
1347
1348 return 1;
1349}
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351/*
1352 * map a request to scatterlist, return number of sg entries setup. Caller
1353 * must make sure sg can hold rq->nr_phys_segments entries
1354 */
1355int blk_rq_map_sg(request_queue_t *q, struct request *rq, struct scatterlist *sg)
1356{
1357 struct bio_vec *bvec, *bvprv;
1358 struct bio *bio;
1359 int nsegs, i, cluster;
1360
1361 nsegs = 0;
1362 cluster = q->queue_flags & (1 << QUEUE_FLAG_CLUSTER);
1363
1364 /*
1365 * for each bio in rq
1366 */
1367 bvprv = NULL;
1368 rq_for_each_bio(bio, rq) {
1369 /*
1370 * for each segment in bio
1371 */
1372 bio_for_each_segment(bvec, bio, i) {
1373 int nbytes = bvec->bv_len;
1374
1375 if (bvprv && cluster) {
1376 if (sg[nsegs - 1].length + nbytes > q->max_segment_size)
1377 goto new_segment;
1378
1379 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
1380 goto new_segment;
1381 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
1382 goto new_segment;
1383
1384 sg[nsegs - 1].length += nbytes;
1385 } else {
1386new_segment:
1387 memset(&sg[nsegs],0,sizeof(struct scatterlist));
1388 sg[nsegs].page = bvec->bv_page;
1389 sg[nsegs].length = nbytes;
1390 sg[nsegs].offset = bvec->bv_offset;
1391
1392 nsegs++;
1393 }
1394 bvprv = bvec;
1395 } /* segments in bio */
1396 } /* bios in rq */
1397
1398 return nsegs;
1399}
1400
1401EXPORT_SYMBOL(blk_rq_map_sg);
1402
1403/*
1404 * the standard queue merge functions, can be overridden with device
1405 * specific ones if so desired
1406 */
1407
1408static inline int ll_new_mergeable(request_queue_t *q,
1409 struct request *req,
1410 struct bio *bio)
1411{
1412 int nr_phys_segs = bio_phys_segments(q, bio);
1413
1414 if (req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
Jens Axboe4aff5e22006-08-10 08:44:47 +02001415 req->cmd_flags |= REQ_NOMERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 if (req == q->last_merge)
1417 q->last_merge = NULL;
1418 return 0;
1419 }
1420
1421 /*
1422 * A hw segment is just getting larger, bump just the phys
1423 * counter.
1424 */
1425 req->nr_phys_segments += nr_phys_segs;
1426 return 1;
1427}
1428
1429static inline int ll_new_hw_segment(request_queue_t *q,
1430 struct request *req,
1431 struct bio *bio)
1432{
1433 int nr_hw_segs = bio_hw_segments(q, bio);
1434 int nr_phys_segs = bio_phys_segments(q, bio);
1435
1436 if (req->nr_hw_segments + nr_hw_segs > q->max_hw_segments
1437 || req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
Jens Axboe4aff5e22006-08-10 08:44:47 +02001438 req->cmd_flags |= REQ_NOMERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 if (req == q->last_merge)
1440 q->last_merge = NULL;
1441 return 0;
1442 }
1443
1444 /*
1445 * This will form the start of a new hw segment. Bump both
1446 * counters.
1447 */
1448 req->nr_hw_segments += nr_hw_segs;
1449 req->nr_phys_segments += nr_phys_segs;
1450 return 1;
1451}
1452
1453static int ll_back_merge_fn(request_queue_t *q, struct request *req,
1454 struct bio *bio)
1455{
Mike Christiedefd94b2005-12-05 02:37:06 -06001456 unsigned short max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 int len;
1458
Mike Christiedefd94b2005-12-05 02:37:06 -06001459 if (unlikely(blk_pc_request(req)))
1460 max_sectors = q->max_hw_sectors;
1461 else
1462 max_sectors = q->max_sectors;
1463
1464 if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
Jens Axboe4aff5e22006-08-10 08:44:47 +02001465 req->cmd_flags |= REQ_NOMERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 if (req == q->last_merge)
1467 q->last_merge = NULL;
1468 return 0;
1469 }
1470 if (unlikely(!bio_flagged(req->biotail, BIO_SEG_VALID)))
1471 blk_recount_segments(q, req->biotail);
1472 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
1473 blk_recount_segments(q, bio);
1474 len = req->biotail->bi_hw_back_size + bio->bi_hw_front_size;
1475 if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(req->biotail), __BVEC_START(bio)) &&
1476 !BIOVEC_VIRT_OVERSIZE(len)) {
1477 int mergeable = ll_new_mergeable(q, req, bio);
1478
1479 if (mergeable) {
1480 if (req->nr_hw_segments == 1)
1481 req->bio->bi_hw_front_size = len;
1482 if (bio->bi_hw_segments == 1)
1483 bio->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_front_merge_fn(request_queue_t *q, struct request *req,
1492 struct bio *bio)
1493{
Mike Christiedefd94b2005-12-05 02:37:06 -06001494 unsigned short max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 int len;
1496
Mike Christiedefd94b2005-12-05 02:37:06 -06001497 if (unlikely(blk_pc_request(req)))
1498 max_sectors = q->max_hw_sectors;
1499 else
1500 max_sectors = q->max_sectors;
1501
1502
1503 if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
Jens Axboe4aff5e22006-08-10 08:44:47 +02001504 req->cmd_flags |= REQ_NOMERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 if (req == q->last_merge)
1506 q->last_merge = NULL;
1507 return 0;
1508 }
1509 len = bio->bi_hw_back_size + req->bio->bi_hw_front_size;
1510 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
1511 blk_recount_segments(q, bio);
1512 if (unlikely(!bio_flagged(req->bio, BIO_SEG_VALID)))
1513 blk_recount_segments(q, req->bio);
1514 if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(req->bio)) &&
1515 !BIOVEC_VIRT_OVERSIZE(len)) {
1516 int mergeable = ll_new_mergeable(q, req, bio);
1517
1518 if (mergeable) {
1519 if (bio->bi_hw_segments == 1)
1520 bio->bi_hw_front_size = len;
1521 if (req->nr_hw_segments == 1)
1522 req->biotail->bi_hw_back_size = len;
1523 }
1524 return mergeable;
1525 }
1526
1527 return ll_new_hw_segment(q, req, bio);
1528}
1529
1530static int ll_merge_requests_fn(request_queue_t *q, struct request *req,
1531 struct request *next)
1532{
Nikita Danilovdfa1a552005-06-25 14:59:20 -07001533 int total_phys_segments;
1534 int total_hw_segments;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536 /*
1537 * First check if the either of the requests are re-queued
1538 * requests. Can't merge them if they are.
1539 */
1540 if (req->special || next->special)
1541 return 0;
1542
1543 /*
Nikita Danilovdfa1a552005-06-25 14:59:20 -07001544 * Will it become too large?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 */
1546 if ((req->nr_sectors + next->nr_sectors) > q->max_sectors)
1547 return 0;
1548
1549 total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
1550 if (blk_phys_contig_segment(q, req->biotail, next->bio))
1551 total_phys_segments--;
1552
1553 if (total_phys_segments > q->max_phys_segments)
1554 return 0;
1555
1556 total_hw_segments = req->nr_hw_segments + next->nr_hw_segments;
1557 if (blk_hw_contig_segment(q, req->biotail, next->bio)) {
1558 int len = req->biotail->bi_hw_back_size + next->bio->bi_hw_front_size;
1559 /*
1560 * propagate the combined length to the end of the requests
1561 */
1562 if (req->nr_hw_segments == 1)
1563 req->bio->bi_hw_front_size = len;
1564 if (next->nr_hw_segments == 1)
1565 next->biotail->bi_hw_back_size = len;
1566 total_hw_segments--;
1567 }
1568
1569 if (total_hw_segments > q->max_hw_segments)
1570 return 0;
1571
1572 /* Merge is OK... */
1573 req->nr_phys_segments = total_phys_segments;
1574 req->nr_hw_segments = total_hw_segments;
1575 return 1;
1576}
1577
1578/*
1579 * "plug" the device if there are no outstanding requests: this will
1580 * force the transfer to start only after we have put all the requests
1581 * on the list.
1582 *
1583 * This is called with interrupts off and no requests on the queue and
1584 * with the queue lock held.
1585 */
1586void blk_plug_device(request_queue_t *q)
1587{
1588 WARN_ON(!irqs_disabled());
1589
1590 /*
1591 * don't plug a stopped queue, it must be paired with blk_start_queue()
1592 * which will restart the queueing
1593 */
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +02001594 if (blk_queue_stopped(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 return;
1596
Jens Axboe2056a782006-03-23 20:00:26 +01001597 if (!test_and_set_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
Jens Axboe2056a782006-03-23 20:00:26 +01001599 blk_add_trace_generic(q, NULL, 0, BLK_TA_PLUG);
1600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601}
1602
1603EXPORT_SYMBOL(blk_plug_device);
1604
1605/*
1606 * remove the queue from the plugged list, if present. called with
1607 * queue lock held and interrupts disabled.
1608 */
1609int blk_remove_plug(request_queue_t *q)
1610{
1611 WARN_ON(!irqs_disabled());
1612
1613 if (!test_and_clear_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags))
1614 return 0;
1615
1616 del_timer(&q->unplug_timer);
1617 return 1;
1618}
1619
1620EXPORT_SYMBOL(blk_remove_plug);
1621
1622/*
1623 * remove the plug and let it rip..
1624 */
1625void __generic_unplug_device(request_queue_t *q)
1626{
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +02001627 if (unlikely(blk_queue_stopped(q)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 return;
1629
1630 if (!blk_remove_plug(q))
1631 return;
1632
Jens Axboe22e2c502005-06-27 10:55:12 +02001633 q->request_fn(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
1635EXPORT_SYMBOL(__generic_unplug_device);
1636
1637/**
1638 * generic_unplug_device - fire a request queue
1639 * @q: The &request_queue_t in question
1640 *
1641 * Description:
1642 * Linux uses plugging to build bigger requests queues before letting
1643 * the device have at them. If a queue is plugged, the I/O scheduler
1644 * is still adding and merging requests on the queue. Once the queue
1645 * gets unplugged, the request_fn defined for the queue is invoked and
1646 * transfers started.
1647 **/
1648void generic_unplug_device(request_queue_t *q)
1649{
1650 spin_lock_irq(q->queue_lock);
1651 __generic_unplug_device(q);
1652 spin_unlock_irq(q->queue_lock);
1653}
1654EXPORT_SYMBOL(generic_unplug_device);
1655
1656static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
1657 struct page *page)
1658{
1659 request_queue_t *q = bdi->unplug_io_data;
1660
1661 /*
1662 * devices don't necessarily have an ->unplug_fn defined
1663 */
Jens Axboe2056a782006-03-23 20:00:26 +01001664 if (q->unplug_fn) {
1665 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
1666 q->rq.count[READ] + q->rq.count[WRITE]);
1667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 q->unplug_fn(q);
Jens Axboe2056a782006-03-23 20:00:26 +01001669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670}
1671
1672static void blk_unplug_work(void *data)
1673{
1674 request_queue_t *q = data;
1675
Jens Axboe2056a782006-03-23 20:00:26 +01001676 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
1677 q->rq.count[READ] + q->rq.count[WRITE]);
1678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 q->unplug_fn(q);
1680}
1681
1682static void blk_unplug_timeout(unsigned long data)
1683{
1684 request_queue_t *q = (request_queue_t *)data;
1685
Jens Axboe2056a782006-03-23 20:00:26 +01001686 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_TIMER, NULL,
1687 q->rq.count[READ] + q->rq.count[WRITE]);
1688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 kblockd_schedule_work(&q->unplug_work);
1690}
1691
1692/**
1693 * blk_start_queue - restart a previously stopped queue
1694 * @q: The &request_queue_t in question
1695 *
1696 * Description:
1697 * blk_start_queue() will clear the stop flag on the queue, and call
1698 * the request_fn for the queue if it was in a stopped state when
1699 * entered. Also see blk_stop_queue(). Queue lock must be held.
1700 **/
1701void blk_start_queue(request_queue_t *q)
1702{
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +02001703 WARN_ON(!irqs_disabled());
1704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 clear_bit(QUEUE_FLAG_STOPPED, &q->queue_flags);
1706
1707 /*
1708 * one level of recursion is ok and is much faster than kicking
1709 * the unplug handling
1710 */
1711 if (!test_and_set_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) {
1712 q->request_fn(q);
1713 clear_bit(QUEUE_FLAG_REENTER, &q->queue_flags);
1714 } else {
1715 blk_plug_device(q);
1716 kblockd_schedule_work(&q->unplug_work);
1717 }
1718}
1719
1720EXPORT_SYMBOL(blk_start_queue);
1721
1722/**
1723 * blk_stop_queue - stop a queue
1724 * @q: The &request_queue_t in question
1725 *
1726 * Description:
1727 * The Linux block layer assumes that a block driver will consume all
1728 * entries on the request queue when the request_fn strategy is called.
1729 * Often this will not happen, because of hardware limitations (queue
1730 * depth settings). If a device driver gets a 'queue full' response,
1731 * or if it simply chooses not to queue more I/O at one point, it can
1732 * call this function to prevent the request_fn from being called until
1733 * the driver has signalled it's ready to go again. This happens by calling
1734 * blk_start_queue() to restart queue operations. Queue lock must be held.
1735 **/
1736void blk_stop_queue(request_queue_t *q)
1737{
1738 blk_remove_plug(q);
1739 set_bit(QUEUE_FLAG_STOPPED, &q->queue_flags);
1740}
1741EXPORT_SYMBOL(blk_stop_queue);
1742
1743/**
1744 * blk_sync_queue - cancel any pending callbacks on a queue
1745 * @q: the queue
1746 *
1747 * Description:
1748 * The block layer may perform asynchronous callback activity
1749 * on a queue, such as calling the unplug function after a timeout.
1750 * A block device may call blk_sync_queue to ensure that any
1751 * such activity is cancelled, thus allowing it to release resources
1752 * the the callbacks might use. The caller must already have made sure
1753 * that its ->make_request_fn will not re-add plugging prior to calling
1754 * this function.
1755 *
1756 */
1757void blk_sync_queue(struct request_queue *q)
1758{
1759 del_timer_sync(&q->unplug_timer);
1760 kblockd_flush();
1761}
1762EXPORT_SYMBOL(blk_sync_queue);
1763
1764/**
1765 * blk_run_queue - run a single device queue
1766 * @q: The queue to run
1767 */
1768void blk_run_queue(struct request_queue *q)
1769{
1770 unsigned long flags;
1771
1772 spin_lock_irqsave(q->queue_lock, flags);
1773 blk_remove_plug(q);
Jens Axboedac07ec2006-05-11 08:20:16 +02001774
1775 /*
1776 * Only recurse once to avoid overrunning the stack, let the unplug
1777 * handling reinvoke the handler shortly if we already got there.
1778 */
1779 if (!elv_queue_empty(q)) {
1780 if (!test_and_set_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) {
1781 q->request_fn(q);
1782 clear_bit(QUEUE_FLAG_REENTER, &q->queue_flags);
1783 } else {
1784 blk_plug_device(q);
1785 kblockd_schedule_work(&q->unplug_work);
1786 }
1787 }
1788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 spin_unlock_irqrestore(q->queue_lock, flags);
1790}
1791EXPORT_SYMBOL(blk_run_queue);
1792
1793/**
1794 * blk_cleanup_queue: - release a &request_queue_t when it is no longer needed
Martin Waitza5802902006-04-02 13:59:55 +02001795 * @kobj: the kobj belonging of the request queue to be released
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 *
1797 * Description:
1798 * blk_cleanup_queue is the pair to blk_init_queue() or
1799 * blk_queue_make_request(). It should be called when a request queue is
1800 * being released; typically when a block device is being de-registered.
1801 * Currently, its primary task it to free all the &struct request
1802 * structures that were allocated to the queue and the queue itself.
1803 *
1804 * Caveat:
1805 * Hopefully the low level driver will have finished any
1806 * outstanding requests first...
1807 **/
Al Viro483f4af2006-03-18 18:34:37 -05001808static void blk_release_queue(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
Al Viro483f4af2006-03-18 18:34:37 -05001810 request_queue_t *q = container_of(kobj, struct request_queue, kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 struct request_list *rl = &q->rq;
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 blk_sync_queue(q);
1814
1815 if (rl->rq_pool)
1816 mempool_destroy(rl->rq_pool);
1817
1818 if (q->queue_tags)
1819 __blk_queue_free_tags(q);
1820
Alexey Dobriyan6c5c9342006-09-29 01:59:40 -07001821 blk_trace_shutdown(q);
Jens Axboe2056a782006-03-23 20:00:26 +01001822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 kmem_cache_free(requestq_cachep, q);
1824}
1825
Al Viro483f4af2006-03-18 18:34:37 -05001826void blk_put_queue(request_queue_t *q)
1827{
1828 kobject_put(&q->kobj);
1829}
1830EXPORT_SYMBOL(blk_put_queue);
1831
1832void blk_cleanup_queue(request_queue_t * q)
1833{
1834 mutex_lock(&q->sysfs_lock);
1835 set_bit(QUEUE_FLAG_DEAD, &q->queue_flags);
1836 mutex_unlock(&q->sysfs_lock);
1837
1838 if (q->elevator)
1839 elevator_exit(q->elevator);
1840
1841 blk_put_queue(q);
1842}
1843
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844EXPORT_SYMBOL(blk_cleanup_queue);
1845
1846static int blk_init_free_list(request_queue_t *q)
1847{
1848 struct request_list *rl = &q->rq;
1849
1850 rl->count[READ] = rl->count[WRITE] = 0;
1851 rl->starved[READ] = rl->starved[WRITE] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +02001852 rl->elvpriv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 init_waitqueue_head(&rl->wait[READ]);
1854 init_waitqueue_head(&rl->wait[WRITE]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Christoph Lameter19460892005-06-23 00:08:19 -07001856 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
1857 mempool_free_slab, request_cachep, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
1859 if (!rl->rq_pool)
1860 return -ENOMEM;
1861
1862 return 0;
1863}
1864
Al Viro8267e262005-10-21 03:20:53 -04001865request_queue_t *blk_alloc_queue(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
Christoph Lameter19460892005-06-23 00:08:19 -07001867 return blk_alloc_queue_node(gfp_mask, -1);
1868}
1869EXPORT_SYMBOL(blk_alloc_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Al Viro483f4af2006-03-18 18:34:37 -05001871static struct kobj_type queue_ktype;
1872
Al Viro8267e262005-10-21 03:20:53 -04001873request_queue_t *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -07001874{
1875 request_queue_t *q;
1876
1877 q = kmem_cache_alloc_node(requestq_cachep, gfp_mask, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 if (!q)
1879 return NULL;
1880
1881 memset(q, 0, sizeof(*q));
1882 init_timer(&q->unplug_timer);
Al Viro483f4af2006-03-18 18:34:37 -05001883
1884 snprintf(q->kobj.name, KOBJ_NAME_LEN, "%s", "queue");
1885 q->kobj.ktype = &queue_ktype;
1886 kobject_init(&q->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
1888 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
1889 q->backing_dev_info.unplug_io_data = q;
1890
Al Viro483f4af2006-03-18 18:34:37 -05001891 mutex_init(&q->sysfs_lock);
1892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 return q;
1894}
Christoph Lameter19460892005-06-23 00:08:19 -07001895EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
1897/**
1898 * blk_init_queue - prepare a request queue for use with a block device
1899 * @rfn: The function to be called to process requests that have been
1900 * placed on the queue.
1901 * @lock: Request queue spin lock
1902 *
1903 * Description:
1904 * If a block device wishes to use the standard request handling procedures,
1905 * which sorts requests and coalesces adjacent requests, then it must
1906 * call blk_init_queue(). The function @rfn will be called when there
1907 * are requests on the queue that need to be processed. If the device
1908 * supports plugging, then @rfn may not be called immediately when requests
1909 * are available on the queue, but may be called at some time later instead.
1910 * Plugged queues are generally unplugged when a buffer belonging to one
1911 * of the requests on the queue is needed, or due to memory pressure.
1912 *
1913 * @rfn is not required, or even expected, to remove all requests off the
1914 * queue, but only as many as it can handle at a time. If it does leave
1915 * requests on the queue, it is responsible for arranging that the requests
1916 * get dealt with eventually.
1917 *
1918 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +02001919 * request queue; this lock will be taken also from interrupt context, so irq
1920 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 *
1922 * Function returns a pointer to the initialized request queue, or NULL if
1923 * it didn't succeed.
1924 *
1925 * Note:
1926 * blk_init_queue() must be paired with a blk_cleanup_queue() call
1927 * when the block device is deactivated (such as at module unload).
1928 **/
Christoph Lameter19460892005-06-23 00:08:19 -07001929
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930request_queue_t *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
1931{
Christoph Lameter19460892005-06-23 00:08:19 -07001932 return blk_init_queue_node(rfn, lock, -1);
1933}
1934EXPORT_SYMBOL(blk_init_queue);
1935
1936request_queue_t *
1937blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
1938{
1939 request_queue_t *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
1941 if (!q)
1942 return NULL;
1943
Christoph Lameter19460892005-06-23 00:08:19 -07001944 q->node = node_id;
Al Viro8669aaf2006-03-18 13:50:00 -05001945 if (blk_init_free_list(q)) {
1946 kmem_cache_free(requestq_cachep, q);
1947 return NULL;
1948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
152587d2005-04-12 16:22:06 -05001950 /*
1951 * if caller didn't supply a lock, they get per-queue locking with
1952 * our embedded lock
1953 */
1954 if (!lock) {
1955 spin_lock_init(&q->__queue_lock);
1956 lock = &q->__queue_lock;
1957 }
1958
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 q->request_fn = rfn;
1960 q->back_merge_fn = ll_back_merge_fn;
1961 q->front_merge_fn = ll_front_merge_fn;
1962 q->merge_requests_fn = ll_merge_requests_fn;
1963 q->prep_rq_fn = NULL;
1964 q->unplug_fn = generic_unplug_device;
1965 q->queue_flags = (1 << QUEUE_FLAG_CLUSTER);
1966 q->queue_lock = lock;
1967
1968 blk_queue_segment_boundary(q, 0xffffffff);
1969
1970 blk_queue_make_request(q, __make_request);
1971 blk_queue_max_segment_size(q, MAX_SEGMENT_SIZE);
1972
1973 blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS);
1974 blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS);
1975
1976 /*
1977 * all done
1978 */
1979 if (!elevator_init(q, NULL)) {
1980 blk_queue_congestion_threshold(q);
1981 return q;
1982 }
1983
Al Viro8669aaf2006-03-18 13:50:00 -05001984 blk_put_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 return NULL;
1986}
Christoph Lameter19460892005-06-23 00:08:19 -07001987EXPORT_SYMBOL(blk_init_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
1989int blk_get_queue(request_queue_t *q)
1990{
Nick Pigginfde6ad22005-06-23 00:08:53 -07001991 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
Al Viro483f4af2006-03-18 18:34:37 -05001992 kobject_get(&q->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 return 0;
1994 }
1995
1996 return 1;
1997}
1998
1999EXPORT_SYMBOL(blk_get_queue);
2000
2001static inline void blk_free_request(request_queue_t *q, struct request *rq)
2002{
Jens Axboe4aff5e22006-08-10 08:44:47 +02002003 if (rq->cmd_flags & REQ_ELVPRIV)
Tejun Heocb98fc82005-10-28 08:29:39 +02002004 elv_put_request(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 mempool_free(rq, q->rq.rq_pool);
2006}
2007
Jens Axboe22e2c502005-06-27 10:55:12 +02002008static inline struct request *
Tejun Heocb98fc82005-10-28 08:29:39 +02002009blk_alloc_request(request_queue_t *q, int rw, struct bio *bio,
Linus Torvalds5dd96242005-10-28 08:56:34 -07002010 int priv, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011{
2012 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
2013
2014 if (!rq)
2015 return NULL;
2016
2017 /*
Jens Axboe4aff5e22006-08-10 08:44:47 +02002018 * first three bits are identical in rq->cmd_flags and bio->bi_rw,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 * see bio.h and blkdev.h
2020 */
Jens Axboe4aff5e22006-08-10 08:44:47 +02002021 rq->cmd_flags = rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Tejun Heocb98fc82005-10-28 08:29:39 +02002023 if (priv) {
2024 if (unlikely(elv_set_request(q, rq, bio, gfp_mask))) {
2025 mempool_free(rq, q->rq.rq_pool);
2026 return NULL;
2027 }
Jens Axboe4aff5e22006-08-10 08:44:47 +02002028 rq->cmd_flags |= REQ_ELVPRIV;
Tejun Heocb98fc82005-10-28 08:29:39 +02002029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Tejun Heocb98fc82005-10-28 08:29:39 +02002031 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032}
2033
2034/*
2035 * ioc_batching returns true if the ioc is a valid batching request and
2036 * should be given priority access to a request.
2037 */
2038static inline int ioc_batching(request_queue_t *q, struct io_context *ioc)
2039{
2040 if (!ioc)
2041 return 0;
2042
2043 /*
2044 * Make sure the process is able to allocate at least 1 request
2045 * even if the batch times out, otherwise we could theoretically
2046 * lose wakeups.
2047 */
2048 return ioc->nr_batch_requests == q->nr_batching ||
2049 (ioc->nr_batch_requests > 0
2050 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
2051}
2052
2053/*
2054 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
2055 * will cause the process to be a "batcher" on all queues in the system. This
2056 * is the behaviour we want though - once it gets a wakeup it should be given
2057 * a nice run.
2058 */
Adrian Bunk93d17d32005-06-25 14:59:10 -07002059static void ioc_set_batching(request_queue_t *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
2061 if (!ioc || ioc_batching(q, ioc))
2062 return;
2063
2064 ioc->nr_batch_requests = q->nr_batching;
2065 ioc->last_waited = jiffies;
2066}
2067
2068static void __freed_request(request_queue_t *q, int rw)
2069{
2070 struct request_list *rl = &q->rq;
2071
2072 if (rl->count[rw] < queue_congestion_off_threshold(q))
2073 clear_queue_congested(q, rw);
2074
2075 if (rl->count[rw] + 1 <= q->nr_requests) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 if (waitqueue_active(&rl->wait[rw]))
2077 wake_up(&rl->wait[rw]);
2078
2079 blk_clear_queue_full(q, rw);
2080 }
2081}
2082
2083/*
2084 * A request has just been released. Account for it, update the full and
2085 * congestion status, wake up any waiters. Called under q->queue_lock.
2086 */
Tejun Heocb98fc82005-10-28 08:29:39 +02002087static void freed_request(request_queue_t *q, int rw, int priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
2089 struct request_list *rl = &q->rq;
2090
2091 rl->count[rw]--;
Tejun Heocb98fc82005-10-28 08:29:39 +02002092 if (priv)
2093 rl->elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
2095 __freed_request(q, rw);
2096
2097 if (unlikely(rl->starved[rw ^ 1]))
2098 __freed_request(q, rw ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099}
2100
2101#define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist)
2102/*
Nick Piggind6344532005-06-28 20:45:14 -07002103 * Get a free request, queue_lock must be held.
2104 * Returns NULL on failure, with queue_lock held.
2105 * Returns !NULL on success, with queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 */
Jens Axboe22e2c502005-06-27 10:55:12 +02002107static struct request *get_request(request_queue_t *q, int rw, struct bio *bio,
Al Viro8267e262005-10-21 03:20:53 -04002108 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109{
2110 struct request *rq = NULL;
2111 struct request_list *rl = &q->rq;
Jens Axboe88ee5ef2005-11-12 11:09:12 +01002112 struct io_context *ioc = NULL;
2113 int may_queue, priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
Jens Axboe88ee5ef2005-11-12 11:09:12 +01002115 may_queue = elv_may_queue(q, rw, bio);
2116 if (may_queue == ELV_MQUEUE_NO)
2117 goto rq_starved;
2118
2119 if (rl->count[rw]+1 >= queue_congestion_on_threshold(q)) {
2120 if (rl->count[rw]+1 >= q->nr_requests) {
2121 ioc = current_io_context(GFP_ATOMIC);
2122 /*
2123 * The queue will fill after this allocation, so set
2124 * it as full, and mark this process as "batching".
2125 * This process will be allowed to complete a batch of
2126 * requests, others will be blocked.
2127 */
2128 if (!blk_queue_full(q, rw)) {
2129 ioc_set_batching(q, ioc);
2130 blk_set_queue_full(q, rw);
2131 } else {
2132 if (may_queue != ELV_MQUEUE_MUST
2133 && !ioc_batching(q, ioc)) {
2134 /*
2135 * The queue is full and the allocating
2136 * process is not a "batcher", and not
2137 * exempted by the IO scheduler
2138 */
2139 goto out;
2140 }
2141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 }
Jens Axboe88ee5ef2005-11-12 11:09:12 +01002143 set_queue_congested(q, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 }
2145
Jens Axboe082cf692005-06-28 16:35:11 +02002146 /*
2147 * Only allow batching queuers to allocate up to 50% over the defined
2148 * limit of requests, otherwise we could have thousands of requests
2149 * allocated with any setting of ->nr_requests
2150 */
Hugh Dickinsfd782a42005-06-29 15:15:40 +01002151 if (rl->count[rw] >= (3 * q->nr_requests / 2))
Jens Axboe082cf692005-06-28 16:35:11 +02002152 goto out;
Hugh Dickinsfd782a42005-06-29 15:15:40 +01002153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 rl->count[rw]++;
2155 rl->starved[rw] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +02002156
Jens Axboe64521d12005-10-28 08:30:39 +02002157 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Tejun Heocb98fc82005-10-28 08:29:39 +02002158 if (priv)
2159 rl->elvpriv++;
2160
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 spin_unlock_irq(q->queue_lock);
2162
Tejun Heocb98fc82005-10-28 08:29:39 +02002163 rq = blk_alloc_request(q, rw, bio, priv, gfp_mask);
Jens Axboe88ee5ef2005-11-12 11:09:12 +01002164 if (unlikely(!rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 /*
2166 * Allocation failed presumably due to memory. Undo anything
2167 * we might have messed up.
2168 *
2169 * Allocating task should really be put onto the front of the
2170 * wait queue, but this is pretty rare.
2171 */
2172 spin_lock_irq(q->queue_lock);
Tejun Heocb98fc82005-10-28 08:29:39 +02002173 freed_request(q, rw, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
2175 /*
2176 * in the very unlikely event that allocation failed and no
2177 * requests for this direction was pending, mark us starved
2178 * so that freeing of a request in the other direction will
2179 * notice us. another possible fix would be to split the
2180 * rq mempool into READ and WRITE
2181 */
2182rq_starved:
2183 if (unlikely(rl->count[rw] == 0))
2184 rl->starved[rw] = 1;
2185
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 goto out;
2187 }
2188
Jens Axboe88ee5ef2005-11-12 11:09:12 +01002189 /*
2190 * ioc may be NULL here, and ioc_batching will be false. That's
2191 * OK, if the queue is under the request limit then requests need
2192 * not count toward the nr_batch_requests limit. There will always
2193 * be some limit enforced by BLK_BATCH_TIME.
2194 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 if (ioc_batching(q, ioc))
2196 ioc->nr_batch_requests--;
2197
2198 rq_init(q, rq);
2199 rq->rl = rl;
Jens Axboe2056a782006-03-23 20:00:26 +01002200
2201 blk_add_trace_generic(q, bio, rw, BLK_TA_GETRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 return rq;
2204}
2205
2206/*
2207 * No available requests for this queue, unplug the device and wait for some
2208 * requests to become available.
Nick Piggind6344532005-06-28 20:45:14 -07002209 *
2210 * Called with q->queue_lock held, and returns with it unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 */
Jens Axboe22e2c502005-06-27 10:55:12 +02002212static struct request *get_request_wait(request_queue_t *q, int rw,
2213 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 struct request *rq;
2216
Nick Piggin450991b2005-06-28 20:45:13 -07002217 rq = get_request(q, rw, bio, GFP_NOIO);
2218 while (!rq) {
2219 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 struct request_list *rl = &q->rq;
2221
2222 prepare_to_wait_exclusive(&rl->wait[rw], &wait,
2223 TASK_UNINTERRUPTIBLE);
2224
Jens Axboe22e2c502005-06-27 10:55:12 +02002225 rq = get_request(q, rw, bio, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
2227 if (!rq) {
2228 struct io_context *ioc;
2229
Jens Axboe2056a782006-03-23 20:00:26 +01002230 blk_add_trace_generic(q, bio, rw, BLK_TA_SLEEPRQ);
2231
Nick Piggind6344532005-06-28 20:45:14 -07002232 __generic_unplug_device(q);
2233 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 io_schedule();
2235
2236 /*
2237 * After sleeping, we become a "batching" process and
2238 * will be able to allocate at least one request, and
2239 * up to a big batch of them for a small period time.
2240 * See ioc_batching, ioc_set_batching
2241 */
Nick Pigginfb3cc432005-06-28 20:45:15 -07002242 ioc = current_io_context(GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 ioc_set_batching(q, ioc);
Nick Piggind6344532005-06-28 20:45:14 -07002244
2245 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 }
2247 finish_wait(&rl->wait[rw], &wait);
Nick Piggin450991b2005-06-28 20:45:13 -07002248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
2250 return rq;
2251}
2252
Al Viro8267e262005-10-21 03:20:53 -04002253struct request *blk_get_request(request_queue_t *q, int rw, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254{
2255 struct request *rq;
2256
2257 BUG_ON(rw != READ && rw != WRITE);
2258
Nick Piggind6344532005-06-28 20:45:14 -07002259 spin_lock_irq(q->queue_lock);
2260 if (gfp_mask & __GFP_WAIT) {
Jens Axboe22e2c502005-06-27 10:55:12 +02002261 rq = get_request_wait(q, rw, NULL);
Nick Piggind6344532005-06-28 20:45:14 -07002262 } else {
Jens Axboe22e2c502005-06-27 10:55:12 +02002263 rq = get_request(q, rw, NULL, gfp_mask);
Nick Piggind6344532005-06-28 20:45:14 -07002264 if (!rq)
2265 spin_unlock_irq(q->queue_lock);
2266 }
2267 /* q->queue_lock is unlocked at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
2269 return rq;
2270}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271EXPORT_SYMBOL(blk_get_request);
2272
2273/**
2274 * blk_requeue_request - put a request back on queue
2275 * @q: request queue where request should be inserted
2276 * @rq: request to be inserted
2277 *
2278 * Description:
2279 * Drivers often keep queueing requests until the hardware cannot accept
2280 * more, when that condition happens we need to put the request back
2281 * on the queue. Must be called with queue lock held.
2282 */
2283void blk_requeue_request(request_queue_t *q, struct request *rq)
2284{
Jens Axboe2056a782006-03-23 20:00:26 +01002285 blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
2286
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 if (blk_rq_tagged(rq))
2288 blk_queue_end_tag(q, rq);
2289
2290 elv_requeue_request(q, rq);
2291}
2292
2293EXPORT_SYMBOL(blk_requeue_request);
2294
2295/**
2296 * blk_insert_request - insert a special request in to a request queue
2297 * @q: request queue where request should be inserted
2298 * @rq: request to be inserted
2299 * @at_head: insert request at head or tail of queue
2300 * @data: private data
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 *
2302 * Description:
2303 * Many block devices need to execute commands asynchronously, so they don't
2304 * block the whole kernel from preemption during request execution. This is
2305 * accomplished normally by inserting aritficial requests tagged as
2306 * REQ_SPECIAL in to the corresponding request queue, and letting them be
2307 * scheduled for actual execution by the request queue.
2308 *
2309 * We have the option of inserting the head or the tail of the queue.
2310 * Typically we use the tail for new ioctls and so forth. We use the head
2311 * of the queue for things like a QUEUE_FULL message from a device, or a
2312 * host that is unable to accept a particular command.
2313 */
2314void blk_insert_request(request_queue_t *q, struct request *rq,
Tejun Heo 867d1192005-04-24 02:06:05 -05002315 int at_head, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316{
Tejun Heo 867d1192005-04-24 02:06:05 -05002317 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 unsigned long flags;
2319
2320 /*
2321 * tell I/O scheduler that this isn't a regular read/write (ie it
2322 * must not attempt merges on this) and that it acts as a soft
2323 * barrier
2324 */
Jens Axboe4aff5e22006-08-10 08:44:47 +02002325 rq->cmd_type = REQ_TYPE_SPECIAL;
2326 rq->cmd_flags |= REQ_SOFTBARRIER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
2328 rq->special = data;
2329
2330 spin_lock_irqsave(q->queue_lock, flags);
2331
2332 /*
2333 * If command is tagged, release the tag
2334 */
Tejun Heo 867d1192005-04-24 02:06:05 -05002335 if (blk_rq_tagged(rq))
2336 blk_queue_end_tag(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
Tejun Heo 867d1192005-04-24 02:06:05 -05002338 drive_stat_acct(rq, rq->nr_sectors, 1);
2339 __elv_add_request(q, rq, where, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 if (blk_queue_plugged(q))
2342 __generic_unplug_device(q);
2343 else
2344 q->request_fn(q);
2345 spin_unlock_irqrestore(q->queue_lock, flags);
2346}
2347
2348EXPORT_SYMBOL(blk_insert_request);
2349
2350/**
2351 * blk_rq_map_user - map user data to a request, for REQ_BLOCK_PC usage
2352 * @q: request queue where request should be inserted
Christoph Hellwig 73747ae2005-06-20 14:21:01 +02002353 * @rq: request structure to fill
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 * @ubuf: the user buffer
2355 * @len: length of user data
2356 *
2357 * Description:
2358 * Data will be mapped directly for zero copy io, if possible. Otherwise
2359 * a kernel bounce buffer is used.
2360 *
2361 * A matching blk_rq_unmap_user() must be issued at the end of io, while
2362 * still in process context.
2363 *
2364 * Note: The mapped bio may need to be bounced through blk_queue_bounce()
2365 * before being submitted to the device, as pages mapped may be out of
2366 * reach. It's the callers responsibility to make sure this happens. The
2367 * original bio must be passed back in to blk_rq_unmap_user() for proper
2368 * unmapping.
2369 */
Jens Axboedd1cab92005-06-20 14:06:01 +02002370int blk_rq_map_user(request_queue_t *q, struct request *rq, void __user *ubuf,
2371 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372{
2373 unsigned long uaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 struct bio *bio;
Jens Axboedd1cab92005-06-20 14:06:01 +02002375 int reading;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Mike Christiedefd94b2005-12-05 02:37:06 -06002377 if (len > (q->max_hw_sectors << 9))
Jens Axboedd1cab92005-06-20 14:06:01 +02002378 return -EINVAL;
2379 if (!len || !ubuf)
2380 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
Jens Axboedd1cab92005-06-20 14:06:01 +02002382 reading = rq_data_dir(rq) == READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
2384 /*
2385 * if alignment requirement is satisfied, map in user pages for
2386 * direct dma. else, set up kernel bounce buffers
2387 */
2388 uaddr = (unsigned long) ubuf;
2389 if (!(uaddr & queue_dma_alignment(q)) && !(len & queue_dma_alignment(q)))
Jens Axboedd1cab92005-06-20 14:06:01 +02002390 bio = bio_map_user(q, NULL, uaddr, len, reading);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 else
Jens Axboedd1cab92005-06-20 14:06:01 +02002392 bio = bio_copy_user(q, uaddr, len, reading);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
2394 if (!IS_ERR(bio)) {
2395 rq->bio = rq->biotail = bio;
2396 blk_rq_bio_prep(q, rq, bio);
2397
2398 rq->buffer = rq->data = NULL;
2399 rq->data_len = len;
Jens Axboedd1cab92005-06-20 14:06:01 +02002400 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 }
2402
2403 /*
2404 * bio is the err-ptr
2405 */
Jens Axboedd1cab92005-06-20 14:06:01 +02002406 return PTR_ERR(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407}
2408
2409EXPORT_SYMBOL(blk_rq_map_user);
2410
2411/**
James Bottomley f1970ba2005-06-20 14:06:52 +02002412 * blk_rq_map_user_iov - map user data to a request, for REQ_BLOCK_PC usage
2413 * @q: request queue where request should be inserted
2414 * @rq: request to map data to
2415 * @iov: pointer to the iovec
2416 * @iov_count: number of elements in the iovec
2417 *
2418 * Description:
2419 * Data will be mapped directly for zero copy io, if possible. Otherwise
2420 * a kernel bounce buffer is used.
2421 *
2422 * A matching blk_rq_unmap_user() must be issued at the end of io, while
2423 * still in process context.
2424 *
2425 * Note: The mapped bio may need to be bounced through blk_queue_bounce()
2426 * before being submitted to the device, as pages mapped may be out of
2427 * reach. It's the callers responsibility to make sure this happens. The
2428 * original bio must be passed back in to blk_rq_unmap_user() for proper
2429 * unmapping.
2430 */
2431int blk_rq_map_user_iov(request_queue_t *q, struct request *rq,
2432 struct sg_iovec *iov, int iov_count)
2433{
2434 struct bio *bio;
2435
2436 if (!iov || iov_count <= 0)
2437 return -EINVAL;
2438
2439 /* we don't allow misaligned data like bio_map_user() does. If the
2440 * user is using sg, they're expected to know the alignment constraints
2441 * and respect them accordingly */
2442 bio = bio_map_user_iov(q, NULL, iov, iov_count, rq_data_dir(rq)== READ);
2443 if (IS_ERR(bio))
2444 return PTR_ERR(bio);
2445
2446 rq->bio = rq->biotail = bio;
2447 blk_rq_bio_prep(q, rq, bio);
2448 rq->buffer = rq->data = NULL;
2449 rq->data_len = bio->bi_size;
2450 return 0;
2451}
2452
2453EXPORT_SYMBOL(blk_rq_map_user_iov);
2454
2455/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 * blk_rq_unmap_user - unmap a request with user data
Christoph Hellwig 73747ae2005-06-20 14:21:01 +02002457 * @bio: bio to be unmapped
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 * @ulen: length of user buffer
2459 *
2460 * Description:
Christoph Hellwig 73747ae2005-06-20 14:21:01 +02002461 * Unmap a bio previously mapped by blk_rq_map_user().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 */
Jens Axboedd1cab92005-06-20 14:06:01 +02002463int blk_rq_unmap_user(struct bio *bio, unsigned int ulen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464{
2465 int ret = 0;
2466
2467 if (bio) {
2468 if (bio_flagged(bio, BIO_USER_MAPPED))
2469 bio_unmap_user(bio);
2470 else
2471 ret = bio_uncopy_user(bio);
2472 }
2473
Jens Axboedd1cab92005-06-20 14:06:01 +02002474 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475}
2476
2477EXPORT_SYMBOL(blk_rq_unmap_user);
2478
2479/**
Mike Christie df46b9a2005-06-20 14:04:44 +02002480 * blk_rq_map_kern - map kernel data to a request, for REQ_BLOCK_PC usage
2481 * @q: request queue where request should be inserted
Christoph Hellwig 73747ae2005-06-20 14:21:01 +02002482 * @rq: request to fill
Mike Christie df46b9a2005-06-20 14:04:44 +02002483 * @kbuf: the kernel buffer
2484 * @len: length of user data
Christoph Hellwig 73747ae2005-06-20 14:21:01 +02002485 * @gfp_mask: memory allocation flags
Mike Christie df46b9a2005-06-20 14:04:44 +02002486 */
Jens Axboedd1cab92005-06-20 14:06:01 +02002487int blk_rq_map_kern(request_queue_t *q, struct request *rq, void *kbuf,
Al Viro8267e262005-10-21 03:20:53 -04002488 unsigned int len, gfp_t gfp_mask)
Mike Christie df46b9a2005-06-20 14:04:44 +02002489{
Mike Christie df46b9a2005-06-20 14:04:44 +02002490 struct bio *bio;
2491
Mike Christiedefd94b2005-12-05 02:37:06 -06002492 if (len > (q->max_hw_sectors << 9))
Jens Axboedd1cab92005-06-20 14:06:01 +02002493 return -EINVAL;
2494 if (!len || !kbuf)
2495 return -EINVAL;
Mike Christie df46b9a2005-06-20 14:04:44 +02002496
2497 bio = bio_map_kern(q, kbuf, len, gfp_mask);
Jens Axboedd1cab92005-06-20 14:06:01 +02002498 if (IS_ERR(bio))
2499 return PTR_ERR(bio);
Mike Christie df46b9a2005-06-20 14:04:44 +02002500
Jens Axboedd1cab92005-06-20 14:06:01 +02002501 if (rq_data_dir(rq) == WRITE)
2502 bio->bi_rw |= (1 << BIO_RW);
Mike Christie df46b9a2005-06-20 14:04:44 +02002503
Jens Axboedd1cab92005-06-20 14:06:01 +02002504 rq->bio = rq->biotail = bio;
2505 blk_rq_bio_prep(q, rq, bio);
Mike Christie df46b9a2005-06-20 14:04:44 +02002506
Jens Axboedd1cab92005-06-20 14:06:01 +02002507 rq->buffer = rq->data = NULL;
2508 rq->data_len = len;
2509 return 0;
Mike Christie df46b9a2005-06-20 14:04:44 +02002510}
2511
2512EXPORT_SYMBOL(blk_rq_map_kern);
2513
Christoph Hellwig 73747ae2005-06-20 14:21:01 +02002514/**
2515 * blk_execute_rq_nowait - insert a request into queue for execution
2516 * @q: queue to insert the request in
2517 * @bd_disk: matching gendisk
2518 * @rq: request to insert
2519 * @at_head: insert request at head or tail of queue
2520 * @done: I/O completion handler
2521 *
2522 * Description:
2523 * Insert a fully prepared request at the back of the io scheduler queue
2524 * for execution. Don't wait for completion.
2525 */
James Bottomley f1970ba2005-06-20 14:06:52 +02002526void blk_execute_rq_nowait(request_queue_t *q, struct gendisk *bd_disk,
2527 struct request *rq, int at_head,
Tejun Heo8ffdc652006-01-06 09:49:03 +01002528 rq_end_io_fn *done)
James Bottomley f1970ba2005-06-20 14:06:52 +02002529{
2530 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
2531
2532 rq->rq_disk = bd_disk;
Jens Axboe4aff5e22006-08-10 08:44:47 +02002533 rq->cmd_flags |= REQ_NOMERGE;
James Bottomley f1970ba2005-06-20 14:06:52 +02002534 rq->end_io = done;
Andrew Morton4c5d0bb2006-03-22 08:08:01 +01002535 WARN_ON(irqs_disabled());
2536 spin_lock_irq(q->queue_lock);
2537 __elv_add_request(q, rq, where, 1);
2538 __generic_unplug_device(q);
2539 spin_unlock_irq(q->queue_lock);
James Bottomley f1970ba2005-06-20 14:06:52 +02002540}
Mike Christie6e39b692005-11-11 05:30:24 -06002541EXPORT_SYMBOL_GPL(blk_execute_rq_nowait);
2542
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543/**
2544 * blk_execute_rq - insert a request into queue for execution
2545 * @q: queue to insert the request in
2546 * @bd_disk: matching gendisk
2547 * @rq: request to insert
James Bottomley 994ca9a2005-06-20 14:11:09 +02002548 * @at_head: insert request at head or tail of queue
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 *
2550 * Description:
2551 * Insert a fully prepared request at the back of the io scheduler queue
Christoph Hellwig 73747ae2005-06-20 14:21:01 +02002552 * for execution and wait for completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 */
2554int blk_execute_rq(request_queue_t *q, struct gendisk *bd_disk,
James Bottomley 994ca9a2005-06-20 14:11:09 +02002555 struct request *rq, int at_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556{
Ingo Molnar60be6b92006-07-03 00:25:26 -07002557 DECLARE_COMPLETION_ONSTACK(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 char sense[SCSI_SENSE_BUFFERSIZE];
2559 int err = 0;
2560
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 /*
2562 * we need an extra reference to the request, so we can look at
2563 * it after io completion
2564 */
2565 rq->ref_count++;
2566
2567 if (!rq->sense) {
2568 memset(sense, 0, sizeof(sense));
2569 rq->sense = sense;
2570 rq->sense_len = 0;
2571 }
2572
Jens Axboec00895a2006-09-30 20:29:12 +02002573 rq->end_io_data = &wait;
James Bottomley 994ca9a2005-06-20 14:11:09 +02002574 blk_execute_rq_nowait(q, bd_disk, rq, at_head, blk_end_sync_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 wait_for_completion(&wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
2577 if (rq->errors)
2578 err = -EIO;
2579
2580 return err;
2581}
2582
2583EXPORT_SYMBOL(blk_execute_rq);
2584
2585/**
2586 * blkdev_issue_flush - queue a flush
2587 * @bdev: blockdev to issue flush for
2588 * @error_sector: error sector
2589 *
2590 * Description:
2591 * Issue a flush for the block device in question. Caller can supply
2592 * room for storing the error offset in case of a flush error, if they
2593 * wish to. Caller must run wait_for_completion() on its own.
2594 */
2595int blkdev_issue_flush(struct block_device *bdev, sector_t *error_sector)
2596{
2597 request_queue_t *q;
2598
2599 if (bdev->bd_disk == NULL)
2600 return -ENXIO;
2601
2602 q = bdev_get_queue(bdev);
2603 if (!q)
2604 return -ENXIO;
2605 if (!q->issue_flush_fn)
2606 return -EOPNOTSUPP;
2607
2608 return q->issue_flush_fn(q, bdev->bd_disk, error_sector);
2609}
2610
2611EXPORT_SYMBOL(blkdev_issue_flush);
2612
Adrian Bunk93d17d32005-06-25 14:59:10 -07002613static void drive_stat_acct(struct request *rq, int nr_sectors, int new_io)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614{
2615 int rw = rq_data_dir(rq);
2616
2617 if (!blk_fs_request(rq) || !rq->rq_disk)
2618 return;
2619
Jens Axboed72d9042005-11-01 08:35:42 +01002620 if (!new_io) {
Jens Axboea3623572005-11-01 09:26:16 +01002621 __disk_stat_inc(rq->rq_disk, merges[rw]);
Jens Axboed72d9042005-11-01 08:35:42 +01002622 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 disk_round_stats(rq->rq_disk);
2624 rq->rq_disk->in_flight++;
2625 }
2626}
2627
2628/*
2629 * add-request adds a request to the linked list.
2630 * queue lock is held and interrupts disabled, as we muck with the
2631 * request queue list.
2632 */
2633static inline void add_request(request_queue_t * q, struct request * req)
2634{
2635 drive_stat_acct(req, req->nr_sectors, 1);
2636
2637 if (q->activity_fn)
2638 q->activity_fn(q->activity_data, rq_data_dir(req));
2639
2640 /*
2641 * elevator indicated where it wants this request to be
2642 * inserted at elevator_merge time
2643 */
2644 __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
2645}
2646
2647/*
2648 * disk_round_stats() - Round off the performance stats on a struct
2649 * disk_stats.
2650 *
2651 * The average IO queue length and utilisation statistics are maintained
2652 * by observing the current state of the queue length and the amount of
2653 * time it has been in this state for.
2654 *
2655 * Normally, that accounting is done on IO completion, but that can result
2656 * in more than a second's worth of IO being accounted for within any one
2657 * second, leading to >100% utilisation. To deal with that, we call this
2658 * function to do a round-off before returning the results when reading
2659 * /proc/diskstats. This accounts immediately for all queue usage up to
2660 * the current jiffies and restarts the counters again.
2661 */
2662void disk_round_stats(struct gendisk *disk)
2663{
2664 unsigned long now = jiffies;
2665
Chen, Kenneth Wb2982642005-10-13 21:49:29 +02002666 if (now == disk->stamp)
2667 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
Chen, Kenneth W20e5c812005-10-13 21:48:42 +02002669 if (disk->in_flight) {
2670 __disk_stat_add(disk, time_in_queue,
2671 disk->in_flight * (now - disk->stamp));
2672 __disk_stat_add(disk, io_ticks, (now - disk->stamp));
2673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 disk->stamp = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675}
2676
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08002677EXPORT_SYMBOL_GPL(disk_round_stats);
2678
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679/*
2680 * queue lock must be held
2681 */
Mike Christie6e39b692005-11-11 05:30:24 -06002682void __blk_put_request(request_queue_t *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683{
2684 struct request_list *rl = req->rl;
2685
2686 if (unlikely(!q))
2687 return;
2688 if (unlikely(--req->ref_count))
2689 return;
2690
Tejun Heo8922e162005-10-20 16:23:44 +02002691 elv_completed_request(q, req);
2692
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 req->rq_status = RQ_INACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 req->rl = NULL;
2695
2696 /*
2697 * Request may not have originated from ll_rw_blk. if not,
2698 * it didn't come out of our reserved rq pools
2699 */
2700 if (rl) {
2701 int rw = rq_data_dir(req);
Jens Axboe4aff5e22006-08-10 08:44:47 +02002702 int priv = req->cmd_flags & REQ_ELVPRIV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 BUG_ON(!list_empty(&req->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +02002705 BUG_ON(!hlist_unhashed(&req->hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
2707 blk_free_request(q, req);
Tejun Heocb98fc82005-10-28 08:29:39 +02002708 freed_request(q, rw, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 }
2710}
2711
Mike Christie6e39b692005-11-11 05:30:24 -06002712EXPORT_SYMBOL_GPL(__blk_put_request);
2713
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714void blk_put_request(struct request *req)
2715{
Tejun Heo8922e162005-10-20 16:23:44 +02002716 unsigned long flags;
2717 request_queue_t *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
Tejun Heo8922e162005-10-20 16:23:44 +02002719 /*
2720 * Gee, IDE calls in w/ NULL q. Fix IDE and remove the
2721 * following if (q) test.
2722 */
2723 if (q) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 spin_lock_irqsave(q->queue_lock, flags);
2725 __blk_put_request(q, req);
2726 spin_unlock_irqrestore(q->queue_lock, flags);
2727 }
2728}
2729
2730EXPORT_SYMBOL(blk_put_request);
2731
2732/**
2733 * blk_end_sync_rq - executes a completion event on a request
2734 * @rq: request to complete
Jens Axboefddfdea2006-01-31 15:24:34 +01002735 * @error: end io status of the request
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 */
Tejun Heo8ffdc652006-01-06 09:49:03 +01002737void blk_end_sync_rq(struct request *rq, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738{
Jens Axboec00895a2006-09-30 20:29:12 +02002739 struct completion *waiting = rq->end_io_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
Jens Axboec00895a2006-09-30 20:29:12 +02002741 rq->end_io_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 __blk_put_request(rq->q, rq);
2743
2744 /*
2745 * complete last, if this is a stack request the process (and thus
2746 * the rq pointer) could be invalid right after this complete()
2747 */
2748 complete(waiting);
2749}
2750EXPORT_SYMBOL(blk_end_sync_rq);
2751
2752/**
2753 * blk_congestion_wait - wait for a queue to become uncongested
2754 * @rw: READ or WRITE
2755 * @timeout: timeout in jiffies
2756 *
2757 * Waits for up to @timeout jiffies for a queue (any queue) to exit congestion.
2758 * If no queues are congested then just wait for the next request to be
2759 * returned.
2760 */
2761long blk_congestion_wait(int rw, long timeout)
2762{
2763 long ret;
2764 DEFINE_WAIT(wait);
2765 wait_queue_head_t *wqh = &congestion_wqh[rw];
2766
2767 prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
2768 ret = io_schedule_timeout(timeout);
2769 finish_wait(wqh, &wait);
2770 return ret;
2771}
2772
2773EXPORT_SYMBOL(blk_congestion_wait);
2774
Trond Myklebust275a0822006-08-22 20:06:24 -04002775/**
2776 * blk_congestion_end - wake up sleepers on a congestion queue
2777 * @rw: READ or WRITE
2778 */
2779void blk_congestion_end(int rw)
2780{
2781 wait_queue_head_t *wqh = &congestion_wqh[rw];
2782
2783 if (waitqueue_active(wqh))
2784 wake_up(wqh);
2785}
2786
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787/*
2788 * Has to be called with the request spinlock acquired
2789 */
2790static int attempt_merge(request_queue_t *q, struct request *req,
2791 struct request *next)
2792{
2793 if (!rq_mergeable(req) || !rq_mergeable(next))
2794 return 0;
2795
2796 /*
Andreas Mohrd6e05ed2006-06-26 18:35:02 +02002797 * not contiguous
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 */
2799 if (req->sector + req->nr_sectors != next->sector)
2800 return 0;
2801
2802 if (rq_data_dir(req) != rq_data_dir(next)
2803 || req->rq_disk != next->rq_disk
Jens Axboec00895a2006-09-30 20:29:12 +02002804 || next->special)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 return 0;
2806
2807 /*
2808 * If we are allowed to merge, then append bio list
2809 * from next to rq and release next. merge_requests_fn
2810 * will have updated segment counts, update sector
2811 * counts here.
2812 */
2813 if (!q->merge_requests_fn(q, req, next))
2814 return 0;
2815
2816 /*
2817 * At this point we have either done a back merge
2818 * or front merge. We need the smaller start_time of
2819 * the merged requests to be the current request
2820 * for accounting purposes.
2821 */
2822 if (time_after(req->start_time, next->start_time))
2823 req->start_time = next->start_time;
2824
2825 req->biotail->bi_next = next->bio;
2826 req->biotail = next->biotail;
2827
2828 req->nr_sectors = req->hard_nr_sectors += next->hard_nr_sectors;
2829
2830 elv_merge_requests(q, req, next);
2831
2832 if (req->rq_disk) {
2833 disk_round_stats(req->rq_disk);
2834 req->rq_disk->in_flight--;
2835 }
2836
Jens Axboe22e2c502005-06-27 10:55:12 +02002837 req->ioprio = ioprio_best(req->ioprio, next->ioprio);
2838
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 __blk_put_request(q, next);
2840 return 1;
2841}
2842
2843static inline int attempt_back_merge(request_queue_t *q, struct request *rq)
2844{
2845 struct request *next = elv_latter_request(q, rq);
2846
2847 if (next)
2848 return attempt_merge(q, rq, next);
2849
2850 return 0;
2851}
2852
2853static inline int attempt_front_merge(request_queue_t *q, struct request *rq)
2854{
2855 struct request *prev = elv_former_request(q, rq);
2856
2857 if (prev)
2858 return attempt_merge(q, prev, rq);
2859
2860 return 0;
2861}
2862
Tejun Heo52d9e672006-01-06 09:49:58 +01002863static void init_request_from_bio(struct request *req, struct bio *bio)
2864{
Jens Axboe4aff5e22006-08-10 08:44:47 +02002865 req->cmd_type = REQ_TYPE_FS;
Tejun Heo52d9e672006-01-06 09:49:58 +01002866
2867 /*
2868 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
2869 */
2870 if (bio_rw_ahead(bio) || bio_failfast(bio))
Jens Axboe4aff5e22006-08-10 08:44:47 +02002871 req->cmd_flags |= REQ_FAILFAST;
Tejun Heo52d9e672006-01-06 09:49:58 +01002872
2873 /*
2874 * REQ_BARRIER implies no merging, but lets make it explicit
2875 */
2876 if (unlikely(bio_barrier(bio)))
Jens Axboe4aff5e22006-08-10 08:44:47 +02002877 req->cmd_flags |= (REQ_HARDBARRIER | REQ_NOMERGE);
Tejun Heo52d9e672006-01-06 09:49:58 +01002878
Jens Axboeb31dc662006-06-13 08:26:10 +02002879 if (bio_sync(bio))
Jens Axboe4aff5e22006-08-10 08:44:47 +02002880 req->cmd_flags |= REQ_RW_SYNC;
Jens Axboeb31dc662006-06-13 08:26:10 +02002881
Tejun Heo52d9e672006-01-06 09:49:58 +01002882 req->errors = 0;
2883 req->hard_sector = req->sector = bio->bi_sector;
2884 req->hard_nr_sectors = req->nr_sectors = bio_sectors(bio);
2885 req->current_nr_sectors = req->hard_cur_sectors = bio_cur_sectors(bio);
2886 req->nr_phys_segments = bio_phys_segments(req->q, bio);
2887 req->nr_hw_segments = bio_hw_segments(req->q, bio);
2888 req->buffer = bio_data(bio); /* see ->buffer comment above */
Tejun Heo52d9e672006-01-06 09:49:58 +01002889 req->bio = req->biotail = bio;
2890 req->ioprio = bio_prio(bio);
2891 req->rq_disk = bio->bi_bdev->bd_disk;
2892 req->start_time = jiffies;
2893}
2894
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895static int __make_request(request_queue_t *q, struct bio *bio)
2896{
Nick Piggin450991b2005-06-28 20:45:13 -07002897 struct request *req;
Jens Axboe4a534f92005-04-16 15:25:40 -07002898 int el_ret, rw, nr_sectors, cur_nr_sectors, barrier, err, sync;
Jens Axboe22e2c502005-06-27 10:55:12 +02002899 unsigned short prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 sector_t sector;
2901
2902 sector = bio->bi_sector;
2903 nr_sectors = bio_sectors(bio);
2904 cur_nr_sectors = bio_cur_sectors(bio);
Jens Axboe22e2c502005-06-27 10:55:12 +02002905 prio = bio_prio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
2907 rw = bio_data_dir(bio);
Jens Axboe4a534f92005-04-16 15:25:40 -07002908 sync = bio_sync(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909
2910 /*
2911 * low level driver can indicate that it wants pages above a
2912 * certain limit bounced to low memory (ie for highmem, or even
2913 * ISA dma in theory)
2914 */
2915 blk_queue_bounce(q, &bio);
2916
2917 spin_lock_prefetch(q->queue_lock);
2918
2919 barrier = bio_barrier(bio);
Tejun Heo797e7db2006-01-06 09:51:03 +01002920 if (unlikely(barrier) && (q->next_ordered == QUEUE_ORDERED_NONE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 err = -EOPNOTSUPP;
2922 goto end_io;
2923 }
2924
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 spin_lock_irq(q->queue_lock);
2926
Nick Piggin450991b2005-06-28 20:45:13 -07002927 if (unlikely(barrier) || elv_queue_empty(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 goto get_rq;
2929
2930 el_ret = elv_merge(q, &req, bio);
2931 switch (el_ret) {
2932 case ELEVATOR_BACK_MERGE:
2933 BUG_ON(!rq_mergeable(req));
2934
2935 if (!q->back_merge_fn(q, req, bio))
2936 break;
2937
Jens Axboe2056a782006-03-23 20:00:26 +01002938 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
2939
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 req->biotail->bi_next = bio;
2941 req->biotail = bio;
2942 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
Jens Axboe22e2c502005-06-27 10:55:12 +02002943 req->ioprio = ioprio_best(req->ioprio, prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 drive_stat_acct(req, nr_sectors, 0);
2945 if (!attempt_back_merge(q, req))
Jens Axboe2e662b62006-07-13 11:55:04 +02002946 elv_merged_request(q, req, el_ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 goto out;
2948
2949 case ELEVATOR_FRONT_MERGE:
2950 BUG_ON(!rq_mergeable(req));
2951
2952 if (!q->front_merge_fn(q, req, bio))
2953 break;
2954
Jens Axboe2056a782006-03-23 20:00:26 +01002955 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
2956
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 bio->bi_next = req->bio;
2958 req->bio = bio;
2959
2960 /*
2961 * may not be valid. if the low level driver said
2962 * it didn't need a bounce buffer then it better
2963 * not touch req->buffer either...
2964 */
2965 req->buffer = bio_data(bio);
2966 req->current_nr_sectors = cur_nr_sectors;
2967 req->hard_cur_sectors = cur_nr_sectors;
2968 req->sector = req->hard_sector = sector;
2969 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
Jens Axboe22e2c502005-06-27 10:55:12 +02002970 req->ioprio = ioprio_best(req->ioprio, prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 drive_stat_acct(req, nr_sectors, 0);
2972 if (!attempt_front_merge(q, req))
Jens Axboe2e662b62006-07-13 11:55:04 +02002973 elv_merged_request(q, req, el_ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 goto out;
2975
Nick Piggin450991b2005-06-28 20:45:13 -07002976 /* ELV_NO_MERGE: elevator says don't/can't merge. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 default:
Nick Piggin450991b2005-06-28 20:45:13 -07002978 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 }
2980
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07002982 /*
2983 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07002984 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07002985 */
Nick Piggin450991b2005-06-28 20:45:13 -07002986 req = get_request_wait(q, rw, bio);
Nick Piggind6344532005-06-28 20:45:14 -07002987
Nick Piggin450991b2005-06-28 20:45:13 -07002988 /*
2989 * After dropping the lock and possibly sleeping here, our request
2990 * may now be mergeable after it had proven unmergeable (above).
2991 * We don't worry about that case for efficiency. It won't happen
2992 * often, and the elevators are able to handle it.
2993 */
Tejun Heo52d9e672006-01-06 09:49:58 +01002994 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
Nick Piggin450991b2005-06-28 20:45:13 -07002996 spin_lock_irq(q->queue_lock);
2997 if (elv_queue_empty(q))
2998 blk_plug_device(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 add_request(q, req);
3000out:
Jens Axboe4a534f92005-04-16 15:25:40 -07003001 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 __generic_unplug_device(q);
3003
3004 spin_unlock_irq(q->queue_lock);
3005 return 0;
3006
3007end_io:
3008 bio_endio(bio, nr_sectors << 9, err);
3009 return 0;
3010}
3011
3012/*
3013 * If bio->bi_dev is a partition, remap the location
3014 */
3015static inline void blk_partition_remap(struct bio *bio)
3016{
3017 struct block_device *bdev = bio->bi_bdev;
3018
3019 if (bdev != bdev->bd_contains) {
3020 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01003021 const int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
Jens Axboea3623572005-11-01 09:26:16 +01003023 p->sectors[rw] += bio_sectors(bio);
3024 p->ios[rw]++;
3025
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 bio->bi_sector += p->start_sect;
3027 bio->bi_bdev = bdev->bd_contains;
3028 }
3029}
3030
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031static void handle_bad_sector(struct bio *bio)
3032{
3033 char b[BDEVNAME_SIZE];
3034
3035 printk(KERN_INFO "attempt to access beyond end of device\n");
3036 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
3037 bdevname(bio->bi_bdev, b),
3038 bio->bi_rw,
3039 (unsigned long long)bio->bi_sector + bio_sectors(bio),
3040 (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
3041
3042 set_bit(BIO_EOF, &bio->bi_flags);
3043}
3044
3045/**
3046 * generic_make_request: hand a buffer to its device driver for I/O
3047 * @bio: The bio describing the location in memory and on the device.
3048 *
3049 * generic_make_request() is used to make I/O requests of block
3050 * devices. It is passed a &struct bio, which describes the I/O that needs
3051 * to be done.
3052 *
3053 * generic_make_request() does not return any status. The
3054 * success/failure status of the request, along with notification of
3055 * completion, is delivered asynchronously through the bio->bi_end_io
3056 * function described (one day) else where.
3057 *
3058 * The caller of generic_make_request must make sure that bi_io_vec
3059 * are set to describe the memory buffer, and that bi_dev and bi_sector are
3060 * set to describe the device address, and the
3061 * bi_end_io and optionally bi_private are set to describe how
3062 * completion notification should be signaled.
3063 *
3064 * generic_make_request and the drivers it calls may use bi_next if this
3065 * bio happens to be merged with someone else, and may change bi_dev and
3066 * bi_sector for remaps as it sees fit. So the values of these fields
3067 * should NOT be depended on after the call to generic_make_request.
3068 */
3069void generic_make_request(struct bio *bio)
3070{
3071 request_queue_t *q;
3072 sector_t maxsector;
3073 int ret, nr_sectors = bio_sectors(bio);
Jens Axboe2056a782006-03-23 20:00:26 +01003074 dev_t old_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
3076 might_sleep();
3077 /* Test device or partition size, when known. */
3078 maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
3079 if (maxsector) {
3080 sector_t sector = bio->bi_sector;
3081
3082 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
3083 /*
3084 * This may well happen - the kernel calls bread()
3085 * without checking the size of the device, e.g., when
3086 * mounting a device.
3087 */
3088 handle_bad_sector(bio);
3089 goto end_io;
3090 }
3091 }
3092
3093 /*
3094 * Resolve the mapping until finished. (drivers are
3095 * still free to implement/resolve their own stacking
3096 * by explicitly returning 0)
3097 *
3098 * NOTE: we don't repeat the blk_size check for each new device.
3099 * Stacking drivers are expected to know what they are doing.
3100 */
Jens Axboe2056a782006-03-23 20:00:26 +01003101 maxsector = -1;
3102 old_dev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 do {
3104 char b[BDEVNAME_SIZE];
3105
3106 q = bdev_get_queue(bio->bi_bdev);
3107 if (!q) {
3108 printk(KERN_ERR
3109 "generic_make_request: Trying to access "
3110 "nonexistent block-device %s (%Lu)\n",
3111 bdevname(bio->bi_bdev, b),
3112 (long long) bio->bi_sector);
3113end_io:
3114 bio_endio(bio, bio->bi_size, -EIO);
3115 break;
3116 }
3117
3118 if (unlikely(bio_sectors(bio) > q->max_hw_sectors)) {
3119 printk("bio too big device %s (%u > %u)\n",
3120 bdevname(bio->bi_bdev, b),
3121 bio_sectors(bio),
3122 q->max_hw_sectors);
3123 goto end_io;
3124 }
3125
Nick Pigginfde6ad22005-06-23 00:08:53 -07003126 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 goto end_io;
3128
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 /*
3130 * If this device has partitions, remap block n
3131 * of partition p to block n+start(p) of the disk.
3132 */
3133 blk_partition_remap(bio);
3134
Jens Axboe2056a782006-03-23 20:00:26 +01003135 if (maxsector != -1)
3136 blk_add_trace_remap(q, bio, old_dev, bio->bi_sector,
3137 maxsector);
3138
3139 blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
3140
3141 maxsector = bio->bi_sector;
3142 old_dev = bio->bi_bdev->bd_dev;
3143
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 ret = q->make_request_fn(q, bio);
3145 } while (ret);
3146}
3147
3148EXPORT_SYMBOL(generic_make_request);
3149
3150/**
3151 * submit_bio: submit a bio to the block device layer for I/O
3152 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
3153 * @bio: The &struct bio which describes the I/O
3154 *
3155 * submit_bio() is very similar in purpose to generic_make_request(), and
3156 * uses that function to do most of the work. Both are fairly rough
3157 * interfaces, @bio must be presetup and ready for I/O.
3158 *
3159 */
3160void submit_bio(int rw, struct bio *bio)
3161{
3162 int count = bio_sectors(bio);
3163
3164 BIO_BUG_ON(!bio->bi_size);
3165 BIO_BUG_ON(!bio->bi_io_vec);
Jens Axboe22e2c502005-06-27 10:55:12 +02003166 bio->bi_rw |= rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 if (rw & WRITE)
Christoph Lameterf8891e52006-06-30 01:55:45 -07003168 count_vm_events(PGPGOUT, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 else
Christoph Lameterf8891e52006-06-30 01:55:45 -07003170 count_vm_events(PGPGIN, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
3172 if (unlikely(block_dump)) {
3173 char b[BDEVNAME_SIZE];
3174 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
3175 current->comm, current->pid,
3176 (rw & WRITE) ? "WRITE" : "READ",
3177 (unsigned long long)bio->bi_sector,
3178 bdevname(bio->bi_bdev,b));
3179 }
3180
3181 generic_make_request(bio);
3182}
3183
3184EXPORT_SYMBOL(submit_bio);
3185
Adrian Bunk93d17d32005-06-25 14:59:10 -07003186static void blk_recalc_rq_segments(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187{
3188 struct bio *bio, *prevbio = NULL;
3189 int nr_phys_segs, nr_hw_segs;
3190 unsigned int phys_size, hw_size;
3191 request_queue_t *q = rq->q;
3192
3193 if (!rq->bio)
3194 return;
3195
3196 phys_size = hw_size = nr_phys_segs = nr_hw_segs = 0;
3197 rq_for_each_bio(bio, rq) {
3198 /* Force bio hw/phys segs to be recalculated. */
3199 bio->bi_flags &= ~(1 << BIO_SEG_VALID);
3200
3201 nr_phys_segs += bio_phys_segments(q, bio);
3202 nr_hw_segs += bio_hw_segments(q, bio);
3203 if (prevbio) {
3204 int pseg = phys_size + prevbio->bi_size + bio->bi_size;
3205 int hseg = hw_size + prevbio->bi_size + bio->bi_size;
3206
3207 if (blk_phys_contig_segment(q, prevbio, bio) &&
3208 pseg <= q->max_segment_size) {
3209 nr_phys_segs--;
3210 phys_size += prevbio->bi_size + bio->bi_size;
3211 } else
3212 phys_size = 0;
3213
3214 if (blk_hw_contig_segment(q, prevbio, bio) &&
3215 hseg <= q->max_segment_size) {
3216 nr_hw_segs--;
3217 hw_size += prevbio->bi_size + bio->bi_size;
3218 } else
3219 hw_size = 0;
3220 }
3221 prevbio = bio;
3222 }
3223
3224 rq->nr_phys_segments = nr_phys_segs;
3225 rq->nr_hw_segments = nr_hw_segs;
3226}
3227
Adrian Bunk93d17d32005-06-25 14:59:10 -07003228static void blk_recalc_rq_sectors(struct request *rq, int nsect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229{
3230 if (blk_fs_request(rq)) {
3231 rq->hard_sector += nsect;
3232 rq->hard_nr_sectors -= nsect;
3233
3234 /*
3235 * Move the I/O submission pointers ahead if required.
3236 */
3237 if ((rq->nr_sectors >= rq->hard_nr_sectors) &&
3238 (rq->sector <= rq->hard_sector)) {
3239 rq->sector = rq->hard_sector;
3240 rq->nr_sectors = rq->hard_nr_sectors;
3241 rq->hard_cur_sectors = bio_cur_sectors(rq->bio);
3242 rq->current_nr_sectors = rq->hard_cur_sectors;
3243 rq->buffer = bio_data(rq->bio);
3244 }
3245
3246 /*
3247 * if total number of sectors is less than the first segment
3248 * size, something has gone terribly wrong
3249 */
3250 if (rq->nr_sectors < rq->current_nr_sectors) {
3251 printk("blk: request botched\n");
3252 rq->nr_sectors = rq->current_nr_sectors;
3253 }
3254 }
3255}
3256
3257static int __end_that_request_first(struct request *req, int uptodate,
3258 int nr_bytes)
3259{
3260 int total_bytes, bio_nbytes, error, next_idx = 0;
3261 struct bio *bio;
3262
Jens Axboe2056a782006-03-23 20:00:26 +01003263 blk_add_trace_rq(req->q, req, BLK_TA_COMPLETE);
3264
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 /*
3266 * extend uptodate bool to allow < 0 value to be direct io error
3267 */
3268 error = 0;
3269 if (end_io_error(uptodate))
3270 error = !uptodate ? -EIO : uptodate;
3271
3272 /*
3273 * for a REQ_BLOCK_PC request, we want to carry any eventual
3274 * sense key with us all the way through
3275 */
3276 if (!blk_pc_request(req))
3277 req->errors = 0;
3278
3279 if (!uptodate) {
Jens Axboe4aff5e22006-08-10 08:44:47 +02003280 if (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 printk("end_request: I/O error, dev %s, sector %llu\n",
3282 req->rq_disk ? req->rq_disk->disk_name : "?",
3283 (unsigned long long)req->sector);
3284 }
3285
Jens Axboed72d9042005-11-01 08:35:42 +01003286 if (blk_fs_request(req) && req->rq_disk) {
Jens Axboea3623572005-11-01 09:26:16 +01003287 const int rw = rq_data_dir(req);
3288
Jens Axboe53e86062006-01-17 11:09:27 +01003289 disk_stat_add(req->rq_disk, sectors[rw], nr_bytes >> 9);
Jens Axboed72d9042005-11-01 08:35:42 +01003290 }
3291
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 total_bytes = bio_nbytes = 0;
3293 while ((bio = req->bio) != NULL) {
3294 int nbytes;
3295
3296 if (nr_bytes >= bio->bi_size) {
3297 req->bio = bio->bi_next;
3298 nbytes = bio->bi_size;
Tejun Heo797e7db2006-01-06 09:51:03 +01003299 if (!ordered_bio_endio(req, bio, nbytes, error))
3300 bio_endio(bio, nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 next_idx = 0;
3302 bio_nbytes = 0;
3303 } else {
3304 int idx = bio->bi_idx + next_idx;
3305
3306 if (unlikely(bio->bi_idx >= bio->bi_vcnt)) {
3307 blk_dump_rq_flags(req, "__end_that");
3308 printk("%s: bio idx %d >= vcnt %d\n",
3309 __FUNCTION__,
3310 bio->bi_idx, bio->bi_vcnt);
3311 break;
3312 }
3313
3314 nbytes = bio_iovec_idx(bio, idx)->bv_len;
3315 BIO_BUG_ON(nbytes > bio->bi_size);
3316
3317 /*
3318 * not a complete bvec done
3319 */
3320 if (unlikely(nbytes > nr_bytes)) {
3321 bio_nbytes += nr_bytes;
3322 total_bytes += nr_bytes;
3323 break;
3324 }
3325
3326 /*
3327 * advance to the next vector
3328 */
3329 next_idx++;
3330 bio_nbytes += nbytes;
3331 }
3332
3333 total_bytes += nbytes;
3334 nr_bytes -= nbytes;
3335
3336 if ((bio = req->bio)) {
3337 /*
3338 * end more in this run, or just return 'not-done'
3339 */
3340 if (unlikely(nr_bytes <= 0))
3341 break;
3342 }
3343 }
3344
3345 /*
3346 * completely done
3347 */
3348 if (!req->bio)
3349 return 0;
3350
3351 /*
3352 * if the request wasn't completed, update state
3353 */
3354 if (bio_nbytes) {
Tejun Heo797e7db2006-01-06 09:51:03 +01003355 if (!ordered_bio_endio(req, bio, bio_nbytes, error))
3356 bio_endio(bio, bio_nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 bio->bi_idx += next_idx;
3358 bio_iovec(bio)->bv_offset += nr_bytes;
3359 bio_iovec(bio)->bv_len -= nr_bytes;
3360 }
3361
3362 blk_recalc_rq_sectors(req, total_bytes >> 9);
3363 blk_recalc_rq_segments(req);
3364 return 1;
3365}
3366
3367/**
3368 * end_that_request_first - end I/O on a request
3369 * @req: the request being processed
3370 * @uptodate: 1 for success, 0 for I/O error, < 0 for specific error
3371 * @nr_sectors: number of sectors to end I/O on
3372 *
3373 * Description:
3374 * Ends I/O on a number of sectors attached to @req, and sets it up
3375 * for the next range of segments (if any) in the cluster.
3376 *
3377 * Return:
3378 * 0 - we are done with this request, call end_that_request_last()
3379 * 1 - still buffers pending for this request
3380 **/
3381int end_that_request_first(struct request *req, int uptodate, int nr_sectors)
3382{
3383 return __end_that_request_first(req, uptodate, nr_sectors << 9);
3384}
3385
3386EXPORT_SYMBOL(end_that_request_first);
3387
3388/**
3389 * end_that_request_chunk - end I/O on a request
3390 * @req: the request being processed
3391 * @uptodate: 1 for success, 0 for I/O error, < 0 for specific error
3392 * @nr_bytes: number of bytes to complete
3393 *
3394 * Description:
3395 * Ends I/O on a number of bytes attached to @req, and sets it up
3396 * for the next range of segments (if any). Like end_that_request_first(),
3397 * but deals with bytes instead of sectors.
3398 *
3399 * Return:
3400 * 0 - we are done with this request, call end_that_request_last()
3401 * 1 - still buffers pending for this request
3402 **/
3403int end_that_request_chunk(struct request *req, int uptodate, int nr_bytes)
3404{
3405 return __end_that_request_first(req, uptodate, nr_bytes);
3406}
3407
3408EXPORT_SYMBOL(end_that_request_chunk);
3409
3410/*
Jens Axboeff856ba2006-01-09 16:02:34 +01003411 * splice the completion data to a local structure and hand off to
3412 * process_completion_queue() to complete the requests
3413 */
3414static void blk_done_softirq(struct softirq_action *h)
3415{
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07003416 struct list_head *cpu_list, local_list;
Jens Axboeff856ba2006-01-09 16:02:34 +01003417
3418 local_irq_disable();
3419 cpu_list = &__get_cpu_var(blk_cpu_done);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07003420 list_replace_init(cpu_list, &local_list);
Jens Axboeff856ba2006-01-09 16:02:34 +01003421 local_irq_enable();
3422
3423 while (!list_empty(&local_list)) {
3424 struct request *rq = list_entry(local_list.next, struct request, donelist);
3425
3426 list_del_init(&rq->donelist);
3427 rq->q->softirq_done_fn(rq);
3428 }
3429}
3430
3431#ifdef CONFIG_HOTPLUG_CPU
3432
3433static int blk_cpu_notify(struct notifier_block *self, unsigned long action,
3434 void *hcpu)
3435{
3436 /*
3437 * If a CPU goes away, splice its entries to the current CPU
3438 * and trigger a run of the softirq
3439 */
3440 if (action == CPU_DEAD) {
3441 int cpu = (unsigned long) hcpu;
3442
3443 local_irq_disable();
3444 list_splice_init(&per_cpu(blk_cpu_done, cpu),
3445 &__get_cpu_var(blk_cpu_done));
3446 raise_softirq_irqoff(BLOCK_SOFTIRQ);
3447 local_irq_enable();
3448 }
3449
3450 return NOTIFY_OK;
3451}
3452
3453
Chandra Seetharaman054cc8a2006-06-27 02:54:07 -07003454static struct notifier_block __devinitdata blk_cpu_notifier = {
Jens Axboeff856ba2006-01-09 16:02:34 +01003455 .notifier_call = blk_cpu_notify,
3456};
3457
3458#endif /* CONFIG_HOTPLUG_CPU */
3459
3460/**
3461 * blk_complete_request - end I/O on a request
3462 * @req: the request being processed
3463 *
3464 * Description:
3465 * Ends all I/O on a request. It does not handle partial completions,
Andreas Mohrd6e05ed2006-06-26 18:35:02 +02003466 * unless the driver actually implements this in its completion callback
Jens Axboeff856ba2006-01-09 16:02:34 +01003467 * through requeueing. Theh actual completion happens out-of-order,
3468 * through a softirq handler. The user must have registered a completion
3469 * callback through blk_queue_softirq_done().
3470 **/
3471
3472void blk_complete_request(struct request *req)
3473{
3474 struct list_head *cpu_list;
3475 unsigned long flags;
3476
3477 BUG_ON(!req->q->softirq_done_fn);
3478
3479 local_irq_save(flags);
3480
3481 cpu_list = &__get_cpu_var(blk_cpu_done);
3482 list_add_tail(&req->donelist, cpu_list);
3483 raise_softirq_irqoff(BLOCK_SOFTIRQ);
3484
3485 local_irq_restore(flags);
3486}
3487
3488EXPORT_SYMBOL(blk_complete_request);
3489
3490/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 * queue lock must be held
3492 */
Tejun Heo8ffdc652006-01-06 09:49:03 +01003493void end_that_request_last(struct request *req, int uptodate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494{
3495 struct gendisk *disk = req->rq_disk;
Tejun Heo8ffdc652006-01-06 09:49:03 +01003496 int error;
3497
3498 /*
3499 * extend uptodate bool to allow < 0 value to be direct io error
3500 */
3501 error = 0;
3502 if (end_io_error(uptodate))
3503 error = !uptodate ? -EIO : uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504
3505 if (unlikely(laptop_mode) && blk_fs_request(req))
3506 laptop_io_completion();
3507
Jens Axboefd0ff8a2006-05-23 11:23:49 +02003508 /*
3509 * Account IO completion. bar_rq isn't accounted as a normal
3510 * IO on queueing nor completion. Accounting the containing
3511 * request is enough.
3512 */
3513 if (disk && blk_fs_request(req) && req != &req->q->bar_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 unsigned long duration = jiffies - req->start_time;
Jens Axboea3623572005-11-01 09:26:16 +01003515 const int rw = rq_data_dir(req);
3516
3517 __disk_stat_inc(disk, ios[rw]);
3518 __disk_stat_add(disk, ticks[rw], duration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 disk_round_stats(disk);
3520 disk->in_flight--;
3521 }
3522 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01003523 req->end_io(req, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 else
3525 __blk_put_request(req->q, req);
3526}
3527
3528EXPORT_SYMBOL(end_that_request_last);
3529
3530void end_request(struct request *req, int uptodate)
3531{
3532 if (!end_that_request_first(req, uptodate, req->hard_cur_sectors)) {
3533 add_disk_randomness(req->rq_disk);
3534 blkdev_dequeue_request(req);
Tejun Heo8ffdc652006-01-06 09:49:03 +01003535 end_that_request_last(req, uptodate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 }
3537}
3538
3539EXPORT_SYMBOL(end_request);
3540
3541void blk_rq_bio_prep(request_queue_t *q, struct request *rq, struct bio *bio)
3542{
Jens Axboe4aff5e22006-08-10 08:44:47 +02003543 /* first two bits are identical in rq->cmd_flags and bio->bi_rw */
3544 rq->cmd_flags |= (bio->bi_rw & 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
3546 rq->nr_phys_segments = bio_phys_segments(q, bio);
3547 rq->nr_hw_segments = bio_hw_segments(q, bio);
3548 rq->current_nr_sectors = bio_cur_sectors(bio);
3549 rq->hard_cur_sectors = rq->current_nr_sectors;
3550 rq->hard_nr_sectors = rq->nr_sectors = bio_sectors(bio);
3551 rq->buffer = bio_data(bio);
3552
3553 rq->bio = rq->biotail = bio;
3554}
3555
3556EXPORT_SYMBOL(blk_rq_bio_prep);
3557
3558int kblockd_schedule_work(struct work_struct *work)
3559{
3560 return queue_work(kblockd_workqueue, work);
3561}
3562
3563EXPORT_SYMBOL(kblockd_schedule_work);
3564
3565void kblockd_flush(void)
3566{
3567 flush_workqueue(kblockd_workqueue);
3568}
3569EXPORT_SYMBOL(kblockd_flush);
3570
3571int __init blk_dev_init(void)
3572{
Jens Axboeff856ba2006-01-09 16:02:34 +01003573 int i;
3574
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 kblockd_workqueue = create_workqueue("kblockd");
3576 if (!kblockd_workqueue)
3577 panic("Failed to create kblockd\n");
3578
3579 request_cachep = kmem_cache_create("blkdev_requests",
3580 sizeof(struct request), 0, SLAB_PANIC, NULL, NULL);
3581
3582 requestq_cachep = kmem_cache_create("blkdev_queue",
3583 sizeof(request_queue_t), 0, SLAB_PANIC, NULL, NULL);
3584
3585 iocontext_cachep = kmem_cache_create("blkdev_ioc",
3586 sizeof(struct io_context), 0, SLAB_PANIC, NULL, NULL);
3587
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08003588 for_each_possible_cpu(i)
Jens Axboeff856ba2006-01-09 16:02:34 +01003589 INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i));
3590
3591 open_softirq(BLOCK_SOFTIRQ, blk_done_softirq, NULL);
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07003592 register_hotcpu_notifier(&blk_cpu_notifier);
Jens Axboeff856ba2006-01-09 16:02:34 +01003593
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 blk_max_low_pfn = max_low_pfn;
3595 blk_max_pfn = max_pfn;
3596
3597 return 0;
3598}
3599
3600/*
3601 * IO Context helper functions
3602 */
3603void put_io_context(struct io_context *ioc)
3604{
3605 if (ioc == NULL)
3606 return;
3607
3608 BUG_ON(atomic_read(&ioc->refcount) == 0);
3609
3610 if (atomic_dec_and_test(&ioc->refcount)) {
Jens Axboee2d74ac2006-03-28 08:59:01 +02003611 struct cfq_io_context *cic;
3612
Al Viro334e94d2006-03-18 15:05:53 -05003613 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 if (ioc->aic && ioc->aic->dtor)
3615 ioc->aic->dtor(ioc->aic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02003616 if (ioc->cic_root.rb_node != NULL) {
Jens Axboe7143dd42006-03-28 09:00:28 +02003617 struct rb_node *n = rb_first(&ioc->cic_root);
3618
3619 cic = rb_entry(n, struct cfq_io_context, rb_node);
Jens Axboee2d74ac2006-03-28 08:59:01 +02003620 cic->dtor(ioc);
3621 }
Al Viro334e94d2006-03-18 15:05:53 -05003622 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623
3624 kmem_cache_free(iocontext_cachep, ioc);
3625 }
3626}
3627EXPORT_SYMBOL(put_io_context);
3628
3629/* Called by the exitting task */
3630void exit_io_context(void)
3631{
3632 unsigned long flags;
3633 struct io_context *ioc;
Jens Axboee2d74ac2006-03-28 08:59:01 +02003634 struct cfq_io_context *cic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635
3636 local_irq_save(flags);
Jens Axboe22e2c502005-06-27 10:55:12 +02003637 task_lock(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 ioc = current->io_context;
3639 current->io_context = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02003640 ioc->task = NULL;
3641 task_unlock(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 local_irq_restore(flags);
3643
3644 if (ioc->aic && ioc->aic->exit)
3645 ioc->aic->exit(ioc->aic);
Jens Axboee2d74ac2006-03-28 08:59:01 +02003646 if (ioc->cic_root.rb_node != NULL) {
3647 cic = rb_entry(rb_first(&ioc->cic_root), struct cfq_io_context, rb_node);
3648 cic->exit(ioc);
3649 }
3650
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 put_io_context(ioc);
3652}
3653
3654/*
3655 * If the current task has no IO context then create one and initialise it.
Nick Pigginfb3cc432005-06-28 20:45:15 -07003656 * Otherwise, return its existing IO context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 *
Nick Pigginfb3cc432005-06-28 20:45:15 -07003658 * This returned IO context doesn't have a specifically elevated refcount,
3659 * but since the current task itself holds a reference, the context can be
3660 * used in general code, so long as it stays within `current` context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 */
Al Viro8267e262005-10-21 03:20:53 -04003662struct io_context *current_io_context(gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663{
3664 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 struct io_context *ret;
3666
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 ret = tsk->io_context;
Nick Pigginfb3cc432005-06-28 20:45:15 -07003668 if (likely(ret))
3669 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670
3671 ret = kmem_cache_alloc(iocontext_cachep, gfp_flags);
3672 if (ret) {
3673 atomic_set(&ret->refcount, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02003674 ret->task = current;
3675 ret->set_ioprio = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 ret->last_waited = jiffies; /* doesn't matter... */
3677 ret->nr_batch_requests = 0; /* because this is 0 */
3678 ret->aic = NULL;
Jens Axboee2d74ac2006-03-28 08:59:01 +02003679 ret->cic_root.rb_node = NULL;
Oleg Nesterov9f83e452006-08-21 08:34:15 +02003680 /* make sure set_task_ioprio() sees the settings above */
3681 smp_wmb();
Nick Pigginfb3cc432005-06-28 20:45:15 -07003682 tsk->io_context = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 }
3684
3685 return ret;
3686}
Nick Pigginfb3cc432005-06-28 20:45:15 -07003687EXPORT_SYMBOL(current_io_context);
3688
3689/*
3690 * If the current task has no IO context then create one and initialise it.
3691 * If it does have a context, take a ref on it.
3692 *
3693 * This is always called in the context of the task which submitted the I/O.
3694 */
Al Viro8267e262005-10-21 03:20:53 -04003695struct io_context *get_io_context(gfp_t gfp_flags)
Nick Pigginfb3cc432005-06-28 20:45:15 -07003696{
3697 struct io_context *ret;
3698 ret = current_io_context(gfp_flags);
3699 if (likely(ret))
3700 atomic_inc(&ret->refcount);
3701 return ret;
3702}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703EXPORT_SYMBOL(get_io_context);
3704
3705void copy_io_context(struct io_context **pdst, struct io_context **psrc)
3706{
3707 struct io_context *src = *psrc;
3708 struct io_context *dst = *pdst;
3709
3710 if (src) {
3711 BUG_ON(atomic_read(&src->refcount) == 0);
3712 atomic_inc(&src->refcount);
3713 put_io_context(dst);
3714 *pdst = src;
3715 }
3716}
3717EXPORT_SYMBOL(copy_io_context);
3718
3719void swap_io_context(struct io_context **ioc1, struct io_context **ioc2)
3720{
3721 struct io_context *temp;
3722 temp = *ioc1;
3723 *ioc1 = *ioc2;
3724 *ioc2 = temp;
3725}
3726EXPORT_SYMBOL(swap_io_context);
3727
3728/*
3729 * sysfs parts below
3730 */
3731struct queue_sysfs_entry {
3732 struct attribute attr;
3733 ssize_t (*show)(struct request_queue *, char *);
3734 ssize_t (*store)(struct request_queue *, const char *, size_t);
3735};
3736
3737static ssize_t
3738queue_var_show(unsigned int var, char *page)
3739{
3740 return sprintf(page, "%d\n", var);
3741}
3742
3743static ssize_t
3744queue_var_store(unsigned long *var, const char *page, size_t count)
3745{
3746 char *p = (char *) page;
3747
3748 *var = simple_strtoul(p, &p, 10);
3749 return count;
3750}
3751
3752static ssize_t queue_requests_show(struct request_queue *q, char *page)
3753{
3754 return queue_var_show(q->nr_requests, (page));
3755}
3756
3757static ssize_t
3758queue_requests_store(struct request_queue *q, const char *page, size_t count)
3759{
3760 struct request_list *rl = &q->rq;
Al Viroc981ff92006-03-18 13:51:29 -05003761 unsigned long nr;
3762 int ret = queue_var_store(&nr, page, count);
3763 if (nr < BLKDEV_MIN_RQ)
3764 nr = BLKDEV_MIN_RQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765
Al Viroc981ff92006-03-18 13:51:29 -05003766 spin_lock_irq(q->queue_lock);
3767 q->nr_requests = nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 blk_queue_congestion_threshold(q);
3769
3770 if (rl->count[READ] >= queue_congestion_on_threshold(q))
3771 set_queue_congested(q, READ);
3772 else if (rl->count[READ] < queue_congestion_off_threshold(q))
3773 clear_queue_congested(q, READ);
3774
3775 if (rl->count[WRITE] >= queue_congestion_on_threshold(q))
3776 set_queue_congested(q, WRITE);
3777 else if (rl->count[WRITE] < queue_congestion_off_threshold(q))
3778 clear_queue_congested(q, WRITE);
3779
3780 if (rl->count[READ] >= q->nr_requests) {
3781 blk_set_queue_full(q, READ);
3782 } else if (rl->count[READ]+1 <= q->nr_requests) {
3783 blk_clear_queue_full(q, READ);
3784 wake_up(&rl->wait[READ]);
3785 }
3786
3787 if (rl->count[WRITE] >= q->nr_requests) {
3788 blk_set_queue_full(q, WRITE);
3789 } else if (rl->count[WRITE]+1 <= q->nr_requests) {
3790 blk_clear_queue_full(q, WRITE);
3791 wake_up(&rl->wait[WRITE]);
3792 }
Al Viroc981ff92006-03-18 13:51:29 -05003793 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 return ret;
3795}
3796
3797static ssize_t queue_ra_show(struct request_queue *q, char *page)
3798{
3799 int ra_kb = q->backing_dev_info.ra_pages << (PAGE_CACHE_SHIFT - 10);
3800
3801 return queue_var_show(ra_kb, (page));
3802}
3803
3804static ssize_t
3805queue_ra_store(struct request_queue *q, const char *page, size_t count)
3806{
3807 unsigned long ra_kb;
3808 ssize_t ret = queue_var_store(&ra_kb, page, count);
3809
3810 spin_lock_irq(q->queue_lock);
3811 if (ra_kb > (q->max_sectors >> 1))
3812 ra_kb = (q->max_sectors >> 1);
3813
3814 q->backing_dev_info.ra_pages = ra_kb >> (PAGE_CACHE_SHIFT - 10);
3815 spin_unlock_irq(q->queue_lock);
3816
3817 return ret;
3818}
3819
3820static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
3821{
3822 int max_sectors_kb = q->max_sectors >> 1;
3823
3824 return queue_var_show(max_sectors_kb, (page));
3825}
3826
3827static ssize_t
3828queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
3829{
3830 unsigned long max_sectors_kb,
3831 max_hw_sectors_kb = q->max_hw_sectors >> 1,
3832 page_kb = 1 << (PAGE_CACHE_SHIFT - 10);
3833 ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
3834 int ra_kb;
3835
3836 if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
3837 return -EINVAL;
3838 /*
3839 * Take the queue lock to update the readahead and max_sectors
3840 * values synchronously:
3841 */
3842 spin_lock_irq(q->queue_lock);
3843 /*
3844 * Trim readahead window as well, if necessary:
3845 */
3846 ra_kb = q->backing_dev_info.ra_pages << (PAGE_CACHE_SHIFT - 10);
3847 if (ra_kb > max_sectors_kb)
3848 q->backing_dev_info.ra_pages =
3849 max_sectors_kb >> (PAGE_CACHE_SHIFT - 10);
3850
3851 q->max_sectors = max_sectors_kb << 1;
3852 spin_unlock_irq(q->queue_lock);
3853
3854 return ret;
3855}
3856
3857static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
3858{
3859 int max_hw_sectors_kb = q->max_hw_sectors >> 1;
3860
3861 return queue_var_show(max_hw_sectors_kb, (page));
3862}
3863
3864
3865static struct queue_sysfs_entry queue_requests_entry = {
3866 .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
3867 .show = queue_requests_show,
3868 .store = queue_requests_store,
3869};
3870
3871static struct queue_sysfs_entry queue_ra_entry = {
3872 .attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR },
3873 .show = queue_ra_show,
3874 .store = queue_ra_store,
3875};
3876
3877static struct queue_sysfs_entry queue_max_sectors_entry = {
3878 .attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR },
3879 .show = queue_max_sectors_show,
3880 .store = queue_max_sectors_store,
3881};
3882
3883static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
3884 .attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO },
3885 .show = queue_max_hw_sectors_show,
3886};
3887
3888static struct queue_sysfs_entry queue_iosched_entry = {
3889 .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR },
3890 .show = elv_iosched_show,
3891 .store = elv_iosched_store,
3892};
3893
3894static struct attribute *default_attrs[] = {
3895 &queue_requests_entry.attr,
3896 &queue_ra_entry.attr,
3897 &queue_max_hw_sectors_entry.attr,
3898 &queue_max_sectors_entry.attr,
3899 &queue_iosched_entry.attr,
3900 NULL,
3901};
3902
3903#define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
3904
3905static ssize_t
3906queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3907{
3908 struct queue_sysfs_entry *entry = to_queue(attr);
Al Viro483f4af2006-03-18 18:34:37 -05003909 request_queue_t *q = container_of(kobj, struct request_queue, kobj);
3910 ssize_t res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 if (!entry->show)
Dmitry Torokhov6c1852a2005-04-29 01:26:06 -05003913 return -EIO;
Al Viro483f4af2006-03-18 18:34:37 -05003914 mutex_lock(&q->sysfs_lock);
3915 if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) {
3916 mutex_unlock(&q->sysfs_lock);
3917 return -ENOENT;
3918 }
3919 res = entry->show(q, page);
3920 mutex_unlock(&q->sysfs_lock);
3921 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922}
3923
3924static ssize_t
3925queue_attr_store(struct kobject *kobj, struct attribute *attr,
3926 const char *page, size_t length)
3927{
3928 struct queue_sysfs_entry *entry = to_queue(attr);
Al Viro483f4af2006-03-18 18:34:37 -05003929 request_queue_t *q = container_of(kobj, struct request_queue, kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930
Al Viro483f4af2006-03-18 18:34:37 -05003931 ssize_t res;
3932
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 if (!entry->store)
Dmitry Torokhov6c1852a2005-04-29 01:26:06 -05003934 return -EIO;
Al Viro483f4af2006-03-18 18:34:37 -05003935 mutex_lock(&q->sysfs_lock);
3936 if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) {
3937 mutex_unlock(&q->sysfs_lock);
3938 return -ENOENT;
3939 }
3940 res = entry->store(q, page, length);
3941 mutex_unlock(&q->sysfs_lock);
3942 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943}
3944
3945static struct sysfs_ops queue_sysfs_ops = {
3946 .show = queue_attr_show,
3947 .store = queue_attr_store,
3948};
3949
Adrian Bunk93d17d32005-06-25 14:59:10 -07003950static struct kobj_type queue_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 .sysfs_ops = &queue_sysfs_ops,
3952 .default_attrs = default_attrs,
Al Viro483f4af2006-03-18 18:34:37 -05003953 .release = blk_release_queue,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954};
3955
3956int blk_register_queue(struct gendisk *disk)
3957{
3958 int ret;
3959
3960 request_queue_t *q = disk->queue;
3961
3962 if (!q || !q->request_fn)
3963 return -ENXIO;
3964
3965 q->kobj.parent = kobject_get(&disk->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966
Al Viro483f4af2006-03-18 18:34:37 -05003967 ret = kobject_add(&q->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 if (ret < 0)
3969 return ret;
3970
Al Viro483f4af2006-03-18 18:34:37 -05003971 kobject_uevent(&q->kobj, KOBJ_ADD);
3972
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 ret = elv_register_queue(q);
3974 if (ret) {
Al Viro483f4af2006-03-18 18:34:37 -05003975 kobject_uevent(&q->kobj, KOBJ_REMOVE);
3976 kobject_del(&q->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 return ret;
3978 }
3979
3980 return 0;
3981}
3982
3983void blk_unregister_queue(struct gendisk *disk)
3984{
3985 request_queue_t *q = disk->queue;
3986
3987 if (q && q->request_fn) {
3988 elv_unregister_queue(q);
3989
Al Viro483f4af2006-03-18 18:34:37 -05003990 kobject_uevent(&q->kobj, KOBJ_REMOVE);
3991 kobject_del(&q->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 kobject_put(&disk->kobj);
3993 }
3994}