blob: 91d3b4828c4916290ded65ab1f016cf2cdfc50ae [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 1994, Karl Keyte: Added support for disk statistics
4 * Elevator latency, (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
5 * Queue request tables / lock, selectable elevator, Jens Axboe <axboe@suse.de>
6 * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au> - July2000
7 * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
8 */
9
10/*
11 * This handles all read/write requests to block devices
12 */
13#include <linux/config.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/backing-dev.h>
17#include <linux/bio.h>
18#include <linux/blkdev.h>
19#include <linux/highmem.h>
20#include <linux/mm.h>
21#include <linux/kernel_stat.h>
22#include <linux/string.h>
23#include <linux/init.h>
24#include <linux/bootmem.h> /* for max_pfn/max_low_pfn */
25#include <linux/completion.h>
26#include <linux/slab.h>
27#include <linux/swap.h>
28#include <linux/writeback.h>
Christoph Lameter19460892005-06-23 00:08:19 -070029#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31/*
32 * for max sense size
33 */
34#include <scsi/scsi_cmnd.h>
35
36static void blk_unplug_work(void *data);
37static void blk_unplug_timeout(unsigned long data);
Adrian Bunk93d17d32005-06-25 14:59:10 -070038static void drive_stat_acct(struct request *rq, int nr_sectors, int new_io);
Tejun Heo52d9e672006-01-06 09:49:58 +010039static void init_request_from_bio(struct request *req, struct bio *bio);
40static int __make_request(request_queue_t *q, struct bio *bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/*
43 * For the allocated request tables
44 */
45static kmem_cache_t *request_cachep;
46
47/*
48 * For queue allocation
49 */
50static kmem_cache_t *requestq_cachep;
51
52/*
53 * For io context allocations
54 */
55static kmem_cache_t *iocontext_cachep;
56
57static wait_queue_head_t congestion_wqh[2] = {
58 __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[0]),
59 __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[1])
60 };
61
62/*
63 * Controlling structure to kblockd
64 */
65static struct workqueue_struct *kblockd_workqueue;
66
67unsigned long blk_max_low_pfn, blk_max_pfn;
68
69EXPORT_SYMBOL(blk_max_low_pfn);
70EXPORT_SYMBOL(blk_max_pfn);
71
72/* Amount of time in which a process may batch requests */
73#define BLK_BATCH_TIME (HZ/50UL)
74
75/* Number of requests a "batching" process may submit */
76#define BLK_BATCH_REQ 32
77
78/*
79 * Return the threshold (number of used requests) at which the queue is
80 * considered to be congested. It include a little hysteresis to keep the
81 * context switch rate down.
82 */
83static inline int queue_congestion_on_threshold(struct request_queue *q)
84{
85 return q->nr_congestion_on;
86}
87
88/*
89 * The threshold at which a queue is considered to be uncongested
90 */
91static inline int queue_congestion_off_threshold(struct request_queue *q)
92{
93 return q->nr_congestion_off;
94}
95
96static void blk_queue_congestion_threshold(struct request_queue *q)
97{
98 int nr;
99
100 nr = q->nr_requests - (q->nr_requests / 8) + 1;
101 if (nr > q->nr_requests)
102 nr = q->nr_requests;
103 q->nr_congestion_on = nr;
104
105 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
106 if (nr < 1)
107 nr = 1;
108 q->nr_congestion_off = nr;
109}
110
111/*
112 * A queue has just exitted congestion. Note this in the global counter of
113 * congested queues, and wake up anyone who was waiting for requests to be
114 * put back.
115 */
116static void clear_queue_congested(request_queue_t *q, int rw)
117{
118 enum bdi_state bit;
119 wait_queue_head_t *wqh = &congestion_wqh[rw];
120
121 bit = (rw == WRITE) ? BDI_write_congested : BDI_read_congested;
122 clear_bit(bit, &q->backing_dev_info.state);
123 smp_mb__after_clear_bit();
124 if (waitqueue_active(wqh))
125 wake_up(wqh);
126}
127
128/*
129 * A queue has just entered congestion. Flag that in the queue's VM-visible
130 * state flags and increment the global gounter of congested queues.
131 */
132static void set_queue_congested(request_queue_t *q, int rw)
133{
134 enum bdi_state bit;
135
136 bit = (rw == WRITE) ? BDI_write_congested : BDI_read_congested;
137 set_bit(bit, &q->backing_dev_info.state);
138}
139
140/**
141 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
142 * @bdev: device
143 *
144 * Locates the passed device's request queue and returns the address of its
145 * backing_dev_info
146 *
147 * Will return NULL if the request queue cannot be located.
148 */
149struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
150{
151 struct backing_dev_info *ret = NULL;
152 request_queue_t *q = bdev_get_queue(bdev);
153
154 if (q)
155 ret = &q->backing_dev_info;
156 return ret;
157}
158
159EXPORT_SYMBOL(blk_get_backing_dev_info);
160
161void blk_queue_activity_fn(request_queue_t *q, activity_fn *fn, void *data)
162{
163 q->activity_fn = fn;
164 q->activity_data = data;
165}
166
167EXPORT_SYMBOL(blk_queue_activity_fn);
168
169/**
170 * blk_queue_prep_rq - set a prepare_request function for queue
171 * @q: queue
172 * @pfn: prepare_request function
173 *
174 * It's possible for a queue to register a prepare_request callback which
175 * is invoked before the request is handed to the request_fn. The goal of
176 * the function is to prepare a request for I/O, it can be used to build a
177 * cdb from the request data for instance.
178 *
179 */
180void blk_queue_prep_rq(request_queue_t *q, prep_rq_fn *pfn)
181{
182 q->prep_rq_fn = pfn;
183}
184
185EXPORT_SYMBOL(blk_queue_prep_rq);
186
187/**
188 * blk_queue_merge_bvec - set a merge_bvec function for queue
189 * @q: queue
190 * @mbfn: merge_bvec_fn
191 *
192 * Usually queues have static limitations on the max sectors or segments that
193 * we can put in a request. Stacking drivers may have some settings that
194 * are dynamic, and thus we have to query the queue whether it is ok to
195 * add a new bio_vec to a bio at a given offset or not. If the block device
196 * has such limitations, it needs to register a merge_bvec_fn to control
197 * the size of bio's sent to it. Note that a block device *must* allow a
198 * single page to be added to an empty bio. The block device driver may want
199 * to use the bio_split() function to deal with these bio's. By default
200 * no merge_bvec_fn is defined for a queue, and only the fixed limits are
201 * honored.
202 */
203void blk_queue_merge_bvec(request_queue_t *q, merge_bvec_fn *mbfn)
204{
205 q->merge_bvec_fn = mbfn;
206}
207
208EXPORT_SYMBOL(blk_queue_merge_bvec);
209
210/**
211 * blk_queue_make_request - define an alternate make_request function for a device
212 * @q: the request queue for the device to be affected
213 * @mfn: the alternate make_request function
214 *
215 * Description:
216 * The normal way for &struct bios to be passed to a device
217 * driver is for them to be collected into requests on a request
218 * queue, and then to allow the device driver to select requests
219 * off that queue when it is ready. This works well for many block
220 * devices. However some block devices (typically virtual devices
221 * such as md or lvm) do not benefit from the processing on the
222 * request queue, and are served best by having the requests passed
223 * directly to them. This can be achieved by providing a function
224 * to blk_queue_make_request().
225 *
226 * Caveat:
227 * The driver that does this *must* be able to deal appropriately
228 * with buffers in "highmemory". This can be accomplished by either calling
229 * __bio_kmap_atomic() to get a temporary kernel mapping, or by calling
230 * blk_queue_bounce() to create a buffer in normal memory.
231 **/
232void blk_queue_make_request(request_queue_t * q, make_request_fn * mfn)
233{
234 /*
235 * set defaults
236 */
237 q->nr_requests = BLKDEV_MAX_RQ;
Stuart McLaren309c0a12005-09-06 15:17:47 -0700238 blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS);
239 blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 q->make_request_fn = mfn;
241 q->backing_dev_info.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
242 q->backing_dev_info.state = 0;
243 q->backing_dev_info.capabilities = BDI_CAP_MAP_COPY;
Mike Christiedefd94b2005-12-05 02:37:06 -0600244 blk_queue_max_sectors(q, SAFE_MAX_SECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 blk_queue_hardsect_size(q, 512);
246 blk_queue_dma_alignment(q, 511);
247 blk_queue_congestion_threshold(q);
248 q->nr_batching = BLK_BATCH_REQ;
249
250 q->unplug_thresh = 4; /* hmm */
251 q->unplug_delay = (3 * HZ) / 1000; /* 3 milliseconds */
252 if (q->unplug_delay == 0)
253 q->unplug_delay = 1;
254
255 INIT_WORK(&q->unplug_work, blk_unplug_work, q);
256
257 q->unplug_timer.function = blk_unplug_timeout;
258 q->unplug_timer.data = (unsigned long)q;
259
260 /*
261 * by default assume old behaviour and bounce for any highmem page
262 */
263 blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
264
265 blk_queue_activity_fn(q, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
268EXPORT_SYMBOL(blk_queue_make_request);
269
270static inline void rq_init(request_queue_t *q, struct request *rq)
271{
272 INIT_LIST_HEAD(&rq->queuelist);
273
274 rq->errors = 0;
275 rq->rq_status = RQ_ACTIVE;
276 rq->bio = rq->biotail = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +0200277 rq->ioprio = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 rq->buffer = NULL;
279 rq->ref_count = 1;
280 rq->q = q;
281 rq->waiting = NULL;
282 rq->special = NULL;
283 rq->data_len = 0;
284 rq->data = NULL;
Mike Christie df46b9a2005-06-20 14:04:44 +0200285 rq->nr_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 rq->sense = NULL;
287 rq->end_io = NULL;
288 rq->end_io_data = NULL;
289}
290
291/**
292 * blk_queue_ordered - does this queue support ordered writes
Tejun Heo797e7db2006-01-06 09:51:03 +0100293 * @q: the request queue
294 * @ordered: one of QUEUE_ORDERED_*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 *
296 * Description:
297 * For journalled file systems, doing ordered writes on a commit
298 * block instead of explicitly doing wait_on_buffer (which is bad
299 * for performance) can be a big win. Block drivers supporting this
300 * feature should call this function and indicate so.
301 *
302 **/
Tejun Heo797e7db2006-01-06 09:51:03 +0100303int blk_queue_ordered(request_queue_t *q, unsigned ordered,
304 prepare_flush_fn *prepare_flush_fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Tejun Heo797e7db2006-01-06 09:51:03 +0100306 if (ordered & (QUEUE_ORDERED_PREFLUSH | QUEUE_ORDERED_POSTFLUSH) &&
307 prepare_flush_fn == NULL) {
308 printk(KERN_ERR "blk_queue_ordered: prepare_flush_fn required\n");
309 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100311
312 if (ordered != QUEUE_ORDERED_NONE &&
313 ordered != QUEUE_ORDERED_DRAIN &&
314 ordered != QUEUE_ORDERED_DRAIN_FLUSH &&
315 ordered != QUEUE_ORDERED_DRAIN_FUA &&
316 ordered != QUEUE_ORDERED_TAG &&
317 ordered != QUEUE_ORDERED_TAG_FLUSH &&
318 ordered != QUEUE_ORDERED_TAG_FUA) {
319 printk(KERN_ERR "blk_queue_ordered: bad value %d\n", ordered);
320 return -EINVAL;
321 }
322
323 q->next_ordered = ordered;
324 q->prepare_flush_fn = prepare_flush_fn;
325
326 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
329EXPORT_SYMBOL(blk_queue_ordered);
330
331/**
332 * blk_queue_issue_flush_fn - set function for issuing a flush
333 * @q: the request queue
334 * @iff: the function to be called issuing the flush
335 *
336 * Description:
337 * If a driver supports issuing a flush command, the support is notified
338 * to the block layer by defining it through this call.
339 *
340 **/
341void blk_queue_issue_flush_fn(request_queue_t *q, issue_flush_fn *iff)
342{
343 q->issue_flush_fn = iff;
344}
345
346EXPORT_SYMBOL(blk_queue_issue_flush_fn);
347
348/*
349 * Cache flushing for ordered writes handling
350 */
Tejun Heo797e7db2006-01-06 09:51:03 +0100351inline unsigned blk_ordered_cur_seq(request_queue_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Tejun Heo797e7db2006-01-06 09:51:03 +0100353 if (!q->ordseq)
354 return 0;
355 return 1 << ffz(q->ordseq);
356}
357
358unsigned blk_ordered_req_seq(struct request *rq)
359{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 request_queue_t *q = rq->q;
361
Tejun Heo797e7db2006-01-06 09:51:03 +0100362 BUG_ON(q->ordseq == 0);
Tejun Heo8922e162005-10-20 16:23:44 +0200363
Tejun Heo797e7db2006-01-06 09:51:03 +0100364 if (rq == &q->pre_flush_rq)
365 return QUEUE_ORDSEQ_PREFLUSH;
366 if (rq == &q->bar_rq)
367 return QUEUE_ORDSEQ_BAR;
368 if (rq == &q->post_flush_rq)
369 return QUEUE_ORDSEQ_POSTFLUSH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Tejun Heo797e7db2006-01-06 09:51:03 +0100371 if ((rq->flags & REQ_ORDERED_COLOR) ==
372 (q->orig_bar_rq->flags & REQ_ORDERED_COLOR))
373 return QUEUE_ORDSEQ_DRAIN;
374 else
375 return QUEUE_ORDSEQ_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
Tejun Heo797e7db2006-01-06 09:51:03 +0100378void blk_ordered_complete_seq(request_queue_t *q, unsigned seq, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Tejun Heo797e7db2006-01-06 09:51:03 +0100380 struct request *rq;
381 int uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Tejun Heo797e7db2006-01-06 09:51:03 +0100383 if (error && !q->orderr)
384 q->orderr = error;
Tejun Heo8922e162005-10-20 16:23:44 +0200385
Tejun Heo797e7db2006-01-06 09:51:03 +0100386 BUG_ON(q->ordseq & seq);
387 q->ordseq |= seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Tejun Heo797e7db2006-01-06 09:51:03 +0100389 if (blk_ordered_cur_seq(q) != QUEUE_ORDSEQ_DONE)
390 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 /*
Tejun Heo797e7db2006-01-06 09:51:03 +0100393 * Okay, sequence complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 */
Tejun Heo797e7db2006-01-06 09:51:03 +0100395 rq = q->orig_bar_rq;
396 uptodate = q->orderr ? q->orderr : 1;
397
398 q->ordseq = 0;
399
400 end_that_request_first(rq, uptodate, rq->hard_nr_sectors);
401 end_that_request_last(rq, uptodate);
402}
403
404static void pre_flush_end_io(struct request *rq, int error)
405{
406 elv_completed_request(rq->q, rq);
407 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_PREFLUSH, error);
408}
409
410static void bar_end_io(struct request *rq, int error)
411{
412 elv_completed_request(rq->q, rq);
413 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_BAR, error);
414}
415
416static void post_flush_end_io(struct request *rq, int error)
417{
418 elv_completed_request(rq->q, rq);
419 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_POSTFLUSH, error);
420}
421
422static void queue_flush(request_queue_t *q, unsigned which)
423{
424 struct request *rq;
425 rq_end_io_fn *end_io;
426
427 if (which == QUEUE_ORDERED_PREFLUSH) {
428 rq = &q->pre_flush_rq;
429 end_io = pre_flush_end_io;
430 } else {
431 rq = &q->post_flush_rq;
432 end_io = post_flush_end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434
Tejun Heo797e7db2006-01-06 09:51:03 +0100435 rq_init(q, rq);
436 rq->flags = REQ_HARDBARRIER;
437 rq->elevator_private = NULL;
438 rq->rq_disk = q->bar_rq.rq_disk;
439 rq->rl = NULL;
440 rq->end_io = end_io;
441 q->prepare_flush_fn(q, rq);
442
443 __elv_add_request(q, rq, ELEVATOR_INSERT_FRONT, 0);
444}
445
446static inline struct request *start_ordered(request_queue_t *q,
447 struct request *rq)
448{
449 q->bi_size = 0;
450 q->orderr = 0;
451 q->ordered = q->next_ordered;
452 q->ordseq |= QUEUE_ORDSEQ_STARTED;
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 /*
Tejun Heo797e7db2006-01-06 09:51:03 +0100455 * Prep proxy barrier request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 */
Tejun Heo797e7db2006-01-06 09:51:03 +0100457 blkdev_dequeue_request(rq);
458 q->orig_bar_rq = rq;
459 rq = &q->bar_rq;
460 rq_init(q, rq);
461 rq->flags = bio_data_dir(q->orig_bar_rq->bio);
462 rq->flags |= q->ordered & QUEUE_ORDERED_FUA ? REQ_FUA : 0;
463 rq->elevator_private = NULL;
464 rq->rl = NULL;
465 init_request_from_bio(rq, q->orig_bar_rq->bio);
466 rq->end_io = bar_end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Tejun Heo797e7db2006-01-06 09:51:03 +0100468 /*
469 * Queue ordered sequence. As we stack them at the head, we
470 * need to queue in reverse order. Note that we rely on that
471 * no fs request uses ELEVATOR_INSERT_FRONT and thus no fs
472 * request gets inbetween ordered sequence.
473 */
474 if (q->ordered & QUEUE_ORDERED_POSTFLUSH)
475 queue_flush(q, QUEUE_ORDERED_POSTFLUSH);
476 else
477 q->ordseq |= QUEUE_ORDSEQ_POSTFLUSH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Tejun Heo797e7db2006-01-06 09:51:03 +0100479 __elv_add_request(q, rq, ELEVATOR_INSERT_FRONT, 0);
480
481 if (q->ordered & QUEUE_ORDERED_PREFLUSH) {
482 queue_flush(q, QUEUE_ORDERED_PREFLUSH);
483 rq = &q->pre_flush_rq;
484 } else
485 q->ordseq |= QUEUE_ORDSEQ_PREFLUSH;
486
487 if ((q->ordered & QUEUE_ORDERED_TAG) || q->in_flight == 0)
488 q->ordseq |= QUEUE_ORDSEQ_DRAIN;
489 else
490 rq = NULL;
491
492 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
Tejun Heo797e7db2006-01-06 09:51:03 +0100495int blk_do_ordered(request_queue_t *q, struct request **rqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Tejun Heo797e7db2006-01-06 09:51:03 +0100497 struct request *rq = *rqp, *allowed_rq;
498 int is_barrier = blk_fs_request(rq) && blk_barrier_rq(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Tejun Heo797e7db2006-01-06 09:51:03 +0100500 if (!q->ordseq) {
501 if (!is_barrier)
502 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Tejun Heo797e7db2006-01-06 09:51:03 +0100504 if (q->next_ordered != QUEUE_ORDERED_NONE) {
505 *rqp = start_ordered(q, rq);
506 return 1;
507 } else {
508 /*
509 * This can happen when the queue switches to
510 * ORDERED_NONE while this request is on it.
511 */
512 blkdev_dequeue_request(rq);
513 end_that_request_first(rq, -EOPNOTSUPP,
514 rq->hard_nr_sectors);
515 end_that_request_last(rq, -EOPNOTSUPP);
516 *rqp = NULL;
517 return 0;
518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Tejun Heo797e7db2006-01-06 09:51:03 +0100521 if (q->ordered & QUEUE_ORDERED_TAG) {
522 if (is_barrier && rq != &q->bar_rq)
523 *rqp = NULL;
524 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
526
Tejun Heo797e7db2006-01-06 09:51:03 +0100527 switch (blk_ordered_cur_seq(q)) {
528 case QUEUE_ORDSEQ_PREFLUSH:
529 allowed_rq = &q->pre_flush_rq;
530 break;
531 case QUEUE_ORDSEQ_BAR:
532 allowed_rq = &q->bar_rq;
533 break;
534 case QUEUE_ORDSEQ_POSTFLUSH:
535 allowed_rq = &q->post_flush_rq;
536 break;
537 default:
538 allowed_rq = NULL;
539 break;
540 }
541
542 if (rq != allowed_rq &&
543 (blk_fs_request(rq) || rq == &q->pre_flush_rq ||
544 rq == &q->post_flush_rq))
545 *rqp = NULL;
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return 1;
548}
549
Tejun Heo797e7db2006-01-06 09:51:03 +0100550static int flush_dry_bio_endio(struct bio *bio, unsigned int bytes, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
Tejun Heo797e7db2006-01-06 09:51:03 +0100552 request_queue_t *q = bio->bi_private;
553 struct bio_vec *bvec;
554 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Tejun Heo797e7db2006-01-06 09:51:03 +0100556 /*
557 * This is dry run, restore bio_sector and size. We'll finish
558 * this request again with the original bi_end_io after an
559 * error occurs or post flush is complete.
560 */
561 q->bi_size += bytes;
562
563 if (bio->bi_size)
564 return 1;
565
566 /* Rewind bvec's */
567 bio->bi_idx = 0;
568 bio_for_each_segment(bvec, bio, i) {
569 bvec->bv_len += bvec->bv_offset;
570 bvec->bv_offset = 0;
571 }
572
573 /* Reset bio */
574 set_bit(BIO_UPTODATE, &bio->bi_flags);
575 bio->bi_size = q->bi_size;
576 bio->bi_sector -= (q->bi_size >> 9);
577 q->bi_size = 0;
578
579 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
Tejun Heo797e7db2006-01-06 09:51:03 +0100581
582static inline int ordered_bio_endio(struct request *rq, struct bio *bio,
583 unsigned int nbytes, int error)
584{
585 request_queue_t *q = rq->q;
586 bio_end_io_t *endio;
587 void *private;
588
589 if (&q->bar_rq != rq)
590 return 0;
591
592 /*
593 * Okay, this is the barrier request in progress, dry finish it.
594 */
595 if (error && !q->orderr)
596 q->orderr = error;
597
598 endio = bio->bi_end_io;
599 private = bio->bi_private;
600 bio->bi_end_io = flush_dry_bio_endio;
601 bio->bi_private = q;
602
603 bio_endio(bio, nbytes, error);
604
605 bio->bi_end_io = endio;
606 bio->bi_private = private;
607
608 return 1;
609}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611/**
612 * blk_queue_bounce_limit - set bounce buffer limit for queue
613 * @q: the request queue for the device
614 * @dma_addr: bus address limit
615 *
616 * Description:
617 * Different hardware can have different requirements as to what pages
618 * it can do I/O directly to. A low level driver can call
619 * blk_queue_bounce_limit to have lower memory pages allocated as bounce
620 * buffers for doing I/O to pages residing above @page. By default
621 * the block layer sets this to the highest numbered "low" memory page.
622 **/
623void blk_queue_bounce_limit(request_queue_t *q, u64 dma_addr)
624{
625 unsigned long bounce_pfn = dma_addr >> PAGE_SHIFT;
626
627 /*
628 * set appropriate bounce gfp mask -- unfortunately we don't have a
629 * full 4GB zone, so we have to resort to low memory for any bounces.
630 * ISA has its own < 16MB zone.
631 */
632 if (bounce_pfn < blk_max_low_pfn) {
633 BUG_ON(dma_addr < BLK_BOUNCE_ISA);
634 init_emergency_isa_pool();
635 q->bounce_gfp = GFP_NOIO | GFP_DMA;
636 } else
637 q->bounce_gfp = GFP_NOIO;
638
639 q->bounce_pfn = bounce_pfn;
640}
641
642EXPORT_SYMBOL(blk_queue_bounce_limit);
643
644/**
645 * blk_queue_max_sectors - set max sectors for a request for this queue
646 * @q: the request queue for the device
647 * @max_sectors: max sectors in the usual 512b unit
648 *
649 * Description:
650 * Enables a low level driver to set an upper limit on the size of
651 * received requests.
652 **/
653void blk_queue_max_sectors(request_queue_t *q, unsigned short max_sectors)
654{
655 if ((max_sectors << 9) < PAGE_CACHE_SIZE) {
656 max_sectors = 1 << (PAGE_CACHE_SHIFT - 9);
657 printk("%s: set to minimum %d\n", __FUNCTION__, max_sectors);
658 }
659
Mike Christiedefd94b2005-12-05 02:37:06 -0600660 if (BLK_DEF_MAX_SECTORS > max_sectors)
661 q->max_hw_sectors = q->max_sectors = max_sectors;
662 else {
663 q->max_sectors = BLK_DEF_MAX_SECTORS;
664 q->max_hw_sectors = max_sectors;
665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
668EXPORT_SYMBOL(blk_queue_max_sectors);
669
670/**
671 * blk_queue_max_phys_segments - set max phys segments for a request for this queue
672 * @q: the request queue for the device
673 * @max_segments: max number of segments
674 *
675 * Description:
676 * Enables a low level driver to set an upper limit on the number of
677 * physical data segments in a request. This would be the largest sized
678 * scatter list the driver could handle.
679 **/
680void blk_queue_max_phys_segments(request_queue_t *q, unsigned short max_segments)
681{
682 if (!max_segments) {
683 max_segments = 1;
684 printk("%s: set to minimum %d\n", __FUNCTION__, max_segments);
685 }
686
687 q->max_phys_segments = max_segments;
688}
689
690EXPORT_SYMBOL(blk_queue_max_phys_segments);
691
692/**
693 * blk_queue_max_hw_segments - set max hw segments for a request for this queue
694 * @q: the request queue for the device
695 * @max_segments: max number of segments
696 *
697 * Description:
698 * Enables a low level driver to set an upper limit on the number of
699 * hw data segments in a request. This would be the largest number of
700 * address/length pairs the host adapter can actually give as once
701 * to the device.
702 **/
703void blk_queue_max_hw_segments(request_queue_t *q, unsigned short max_segments)
704{
705 if (!max_segments) {
706 max_segments = 1;
707 printk("%s: set to minimum %d\n", __FUNCTION__, max_segments);
708 }
709
710 q->max_hw_segments = max_segments;
711}
712
713EXPORT_SYMBOL(blk_queue_max_hw_segments);
714
715/**
716 * blk_queue_max_segment_size - set max segment size for blk_rq_map_sg
717 * @q: the request queue for the device
718 * @max_size: max size of segment in bytes
719 *
720 * Description:
721 * Enables a low level driver to set an upper limit on the size of a
722 * coalesced segment
723 **/
724void blk_queue_max_segment_size(request_queue_t *q, unsigned int max_size)
725{
726 if (max_size < PAGE_CACHE_SIZE) {
727 max_size = PAGE_CACHE_SIZE;
728 printk("%s: set to minimum %d\n", __FUNCTION__, max_size);
729 }
730
731 q->max_segment_size = max_size;
732}
733
734EXPORT_SYMBOL(blk_queue_max_segment_size);
735
736/**
737 * blk_queue_hardsect_size - set hardware sector size for the queue
738 * @q: the request queue for the device
739 * @size: the hardware sector size, in bytes
740 *
741 * Description:
742 * This should typically be set to the lowest possible sector size
743 * that the hardware can operate on (possible without reverting to
744 * even internal read-modify-write operations). Usually the default
745 * of 512 covers most hardware.
746 **/
747void blk_queue_hardsect_size(request_queue_t *q, unsigned short size)
748{
749 q->hardsect_size = size;
750}
751
752EXPORT_SYMBOL(blk_queue_hardsect_size);
753
754/*
755 * Returns the minimum that is _not_ zero, unless both are zero.
756 */
757#define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
758
759/**
760 * blk_queue_stack_limits - inherit underlying queue limits for stacked drivers
761 * @t: the stacking driver (top)
762 * @b: the underlying device (bottom)
763 **/
764void blk_queue_stack_limits(request_queue_t *t, request_queue_t *b)
765{
766 /* zero is "infinity" */
Mike Christiedefd94b2005-12-05 02:37:06 -0600767 t->max_sectors = min_not_zero(t->max_sectors,b->max_sectors);
768 t->max_hw_sectors = min_not_zero(t->max_hw_sectors,b->max_hw_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 t->max_phys_segments = min(t->max_phys_segments,b->max_phys_segments);
771 t->max_hw_segments = min(t->max_hw_segments,b->max_hw_segments);
772 t->max_segment_size = min(t->max_segment_size,b->max_segment_size);
773 t->hardsect_size = max(t->hardsect_size,b->hardsect_size);
774}
775
776EXPORT_SYMBOL(blk_queue_stack_limits);
777
778/**
779 * blk_queue_segment_boundary - set boundary rules for segment merging
780 * @q: the request queue for the device
781 * @mask: the memory boundary mask
782 **/
783void blk_queue_segment_boundary(request_queue_t *q, unsigned long mask)
784{
785 if (mask < PAGE_CACHE_SIZE - 1) {
786 mask = PAGE_CACHE_SIZE - 1;
787 printk("%s: set to minimum %lx\n", __FUNCTION__, mask);
788 }
789
790 q->seg_boundary_mask = mask;
791}
792
793EXPORT_SYMBOL(blk_queue_segment_boundary);
794
795/**
796 * blk_queue_dma_alignment - set dma length and memory alignment
797 * @q: the request queue for the device
798 * @mask: alignment mask
799 *
800 * description:
801 * set required memory and length aligment for direct dma transactions.
802 * this is used when buiding direct io requests for the queue.
803 *
804 **/
805void blk_queue_dma_alignment(request_queue_t *q, int mask)
806{
807 q->dma_alignment = mask;
808}
809
810EXPORT_SYMBOL(blk_queue_dma_alignment);
811
812/**
813 * blk_queue_find_tag - find a request by its tag and queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 * @q: The request queue for the device
815 * @tag: The tag of the request
816 *
817 * Notes:
818 * Should be used when a device returns a tag and you want to match
819 * it with a request.
820 *
821 * no locks need be held.
822 **/
823struct request *blk_queue_find_tag(request_queue_t *q, int tag)
824{
825 struct blk_queue_tag *bqt = q->queue_tags;
826
Tejun Heoba025082005-08-05 13:28:11 -0700827 if (unlikely(bqt == NULL || tag >= bqt->real_max_depth))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return NULL;
829
830 return bqt->tag_index[tag];
831}
832
833EXPORT_SYMBOL(blk_queue_find_tag);
834
835/**
836 * __blk_queue_free_tags - release tag maintenance info
837 * @q: the request queue for the device
838 *
839 * Notes:
840 * blk_cleanup_queue() will take care of calling this function, if tagging
841 * has been used. So there's no need to call this directly.
842 **/
843static void __blk_queue_free_tags(request_queue_t *q)
844{
845 struct blk_queue_tag *bqt = q->queue_tags;
846
847 if (!bqt)
848 return;
849
850 if (atomic_dec_and_test(&bqt->refcnt)) {
851 BUG_ON(bqt->busy);
852 BUG_ON(!list_empty(&bqt->busy_list));
853
854 kfree(bqt->tag_index);
855 bqt->tag_index = NULL;
856
857 kfree(bqt->tag_map);
858 bqt->tag_map = NULL;
859
860 kfree(bqt);
861 }
862
863 q->queue_tags = NULL;
864 q->queue_flags &= ~(1 << QUEUE_FLAG_QUEUED);
865}
866
867/**
868 * blk_queue_free_tags - release tag maintenance info
869 * @q: the request queue for the device
870 *
871 * Notes:
872 * This is used to disabled tagged queuing to a device, yet leave
873 * queue in function.
874 **/
875void blk_queue_free_tags(request_queue_t *q)
876{
877 clear_bit(QUEUE_FLAG_QUEUED, &q->queue_flags);
878}
879
880EXPORT_SYMBOL(blk_queue_free_tags);
881
882static int
883init_tag_map(request_queue_t *q, struct blk_queue_tag *tags, int depth)
884{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 struct request **tag_index;
886 unsigned long *tag_map;
Tejun Heofa72b902005-06-23 00:08:49 -0700887 int nr_ulongs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 if (depth > q->nr_requests * 2) {
890 depth = q->nr_requests * 2;
891 printk(KERN_ERR "%s: adjusted depth to %d\n",
892 __FUNCTION__, depth);
893 }
894
895 tag_index = kmalloc(depth * sizeof(struct request *), GFP_ATOMIC);
896 if (!tag_index)
897 goto fail;
898
Tejun Heof7d37d02005-06-23 00:08:50 -0700899 nr_ulongs = ALIGN(depth, BITS_PER_LONG) / BITS_PER_LONG;
Tejun Heofa72b902005-06-23 00:08:49 -0700900 tag_map = kmalloc(nr_ulongs * sizeof(unsigned long), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 if (!tag_map)
902 goto fail;
903
904 memset(tag_index, 0, depth * sizeof(struct request *));
Tejun Heofa72b902005-06-23 00:08:49 -0700905 memset(tag_map, 0, nr_ulongs * sizeof(unsigned long));
Tejun Heoba025082005-08-05 13:28:11 -0700906 tags->real_max_depth = depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 tags->max_depth = depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 tags->tag_index = tag_index;
909 tags->tag_map = tag_map;
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return 0;
912fail:
913 kfree(tag_index);
914 return -ENOMEM;
915}
916
917/**
918 * blk_queue_init_tags - initialize the queue tag info
919 * @q: the request queue for the device
920 * @depth: the maximum queue depth supported
921 * @tags: the tag to use
922 **/
923int blk_queue_init_tags(request_queue_t *q, int depth,
924 struct blk_queue_tag *tags)
925{
926 int rc;
927
928 BUG_ON(tags && q->queue_tags && tags != q->queue_tags);
929
930 if (!tags && !q->queue_tags) {
931 tags = kmalloc(sizeof(struct blk_queue_tag), GFP_ATOMIC);
932 if (!tags)
933 goto fail;
934
935 if (init_tag_map(q, tags, depth))
936 goto fail;
937
938 INIT_LIST_HEAD(&tags->busy_list);
939 tags->busy = 0;
940 atomic_set(&tags->refcnt, 1);
941 } else if (q->queue_tags) {
942 if ((rc = blk_queue_resize_tags(q, depth)))
943 return rc;
944 set_bit(QUEUE_FLAG_QUEUED, &q->queue_flags);
945 return 0;
946 } else
947 atomic_inc(&tags->refcnt);
948
949 /*
950 * assign it, all done
951 */
952 q->queue_tags = tags;
953 q->queue_flags |= (1 << QUEUE_FLAG_QUEUED);
954 return 0;
955fail:
956 kfree(tags);
957 return -ENOMEM;
958}
959
960EXPORT_SYMBOL(blk_queue_init_tags);
961
962/**
963 * blk_queue_resize_tags - change the queueing depth
964 * @q: the request queue for the device
965 * @new_depth: the new max command queueing depth
966 *
967 * Notes:
968 * Must be called with the queue lock held.
969 **/
970int blk_queue_resize_tags(request_queue_t *q, int new_depth)
971{
972 struct blk_queue_tag *bqt = q->queue_tags;
973 struct request **tag_index;
974 unsigned long *tag_map;
Tejun Heofa72b902005-06-23 00:08:49 -0700975 int max_depth, nr_ulongs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 if (!bqt)
978 return -ENXIO;
979
980 /*
Tejun Heoba025082005-08-05 13:28:11 -0700981 * if we already have large enough real_max_depth. just
982 * adjust max_depth. *NOTE* as requests with tag value
983 * between new_depth and real_max_depth can be in-flight, tag
984 * map can not be shrunk blindly here.
985 */
986 if (new_depth <= bqt->real_max_depth) {
987 bqt->max_depth = new_depth;
988 return 0;
989 }
990
991 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 * save the old state info, so we can copy it back
993 */
994 tag_index = bqt->tag_index;
995 tag_map = bqt->tag_map;
Tejun Heoba025082005-08-05 13:28:11 -0700996 max_depth = bqt->real_max_depth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 if (init_tag_map(q, bqt, new_depth))
999 return -ENOMEM;
1000
1001 memcpy(bqt->tag_index, tag_index, max_depth * sizeof(struct request *));
Tejun Heof7d37d02005-06-23 00:08:50 -07001002 nr_ulongs = ALIGN(max_depth, BITS_PER_LONG) / BITS_PER_LONG;
Tejun Heofa72b902005-06-23 00:08:49 -07001003 memcpy(bqt->tag_map, tag_map, nr_ulongs * sizeof(unsigned long));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 kfree(tag_index);
1006 kfree(tag_map);
1007 return 0;
1008}
1009
1010EXPORT_SYMBOL(blk_queue_resize_tags);
1011
1012/**
1013 * blk_queue_end_tag - end tag operations for a request
1014 * @q: the request queue for the device
1015 * @rq: the request that has completed
1016 *
1017 * Description:
1018 * Typically called when end_that_request_first() returns 0, meaning
1019 * all transfers have been done for a request. It's important to call
1020 * this function before end_that_request_last(), as that will put the
1021 * request back on the free list thus corrupting the internal tag list.
1022 *
1023 * Notes:
1024 * queue lock must be held.
1025 **/
1026void blk_queue_end_tag(request_queue_t *q, struct request *rq)
1027{
1028 struct blk_queue_tag *bqt = q->queue_tags;
1029 int tag = rq->tag;
1030
1031 BUG_ON(tag == -1);
1032
Tejun Heoba025082005-08-05 13:28:11 -07001033 if (unlikely(tag >= bqt->real_max_depth))
Tejun Heo040c9282005-06-23 00:08:51 -07001034 /*
1035 * This can happen after tag depth has been reduced.
1036 * FIXME: how about a warning or info message here?
1037 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return;
1039
1040 if (unlikely(!__test_and_clear_bit(tag, bqt->tag_map))) {
Tejun Heo040c9282005-06-23 00:08:51 -07001041 printk(KERN_ERR "%s: attempt to clear non-busy tag (%d)\n",
1042 __FUNCTION__, tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return;
1044 }
1045
1046 list_del_init(&rq->queuelist);
1047 rq->flags &= ~REQ_QUEUED;
1048 rq->tag = -1;
1049
1050 if (unlikely(bqt->tag_index[tag] == NULL))
Tejun Heo040c9282005-06-23 00:08:51 -07001051 printk(KERN_ERR "%s: tag %d is missing\n",
1052 __FUNCTION__, tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 bqt->tag_index[tag] = NULL;
1055 bqt->busy--;
1056}
1057
1058EXPORT_SYMBOL(blk_queue_end_tag);
1059
1060/**
1061 * blk_queue_start_tag - find a free tag and assign it
1062 * @q: the request queue for the device
1063 * @rq: the block request that needs tagging
1064 *
1065 * Description:
1066 * This can either be used as a stand-alone helper, or possibly be
1067 * assigned as the queue &prep_rq_fn (in which case &struct request
1068 * automagically gets a tag assigned). Note that this function
1069 * assumes that any type of request can be queued! if this is not
1070 * true for your device, you must check the request type before
1071 * calling this function. The request will also be removed from
1072 * the request queue, so it's the drivers responsibility to readd
1073 * it if it should need to be restarted for some reason.
1074 *
1075 * Notes:
1076 * queue lock must be held.
1077 **/
1078int blk_queue_start_tag(request_queue_t *q, struct request *rq)
1079{
1080 struct blk_queue_tag *bqt = q->queue_tags;
Tejun Heo2bf0fda2005-06-23 00:08:48 -07001081 int tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 if (unlikely((rq->flags & REQ_QUEUED))) {
1084 printk(KERN_ERR
Tejun Heo040c9282005-06-23 00:08:51 -07001085 "%s: request %p for device [%s] already tagged %d",
1086 __FUNCTION__, rq,
1087 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 BUG();
1089 }
1090
Tejun Heo2bf0fda2005-06-23 00:08:48 -07001091 tag = find_first_zero_bit(bqt->tag_map, bqt->max_depth);
1092 if (tag >= bqt->max_depth)
1093 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 __set_bit(tag, bqt->tag_map);
1096
1097 rq->flags |= REQ_QUEUED;
1098 rq->tag = tag;
1099 bqt->tag_index[tag] = rq;
1100 blkdev_dequeue_request(rq);
1101 list_add(&rq->queuelist, &bqt->busy_list);
1102 bqt->busy++;
1103 return 0;
1104}
1105
1106EXPORT_SYMBOL(blk_queue_start_tag);
1107
1108/**
1109 * blk_queue_invalidate_tags - invalidate all pending tags
1110 * @q: the request queue for the device
1111 *
1112 * Description:
1113 * Hardware conditions may dictate a need to stop all pending requests.
1114 * In this case, we will safely clear the block side of the tag queue and
1115 * readd all requests to the request queue in the right order.
1116 *
1117 * Notes:
1118 * queue lock must be held.
1119 **/
1120void blk_queue_invalidate_tags(request_queue_t *q)
1121{
1122 struct blk_queue_tag *bqt = q->queue_tags;
1123 struct list_head *tmp, *n;
1124 struct request *rq;
1125
1126 list_for_each_safe(tmp, n, &bqt->busy_list) {
1127 rq = list_entry_rq(tmp);
1128
1129 if (rq->tag == -1) {
Tejun Heo040c9282005-06-23 00:08:51 -07001130 printk(KERN_ERR
1131 "%s: bad tag found on list\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 list_del_init(&rq->queuelist);
1133 rq->flags &= ~REQ_QUEUED;
1134 } else
1135 blk_queue_end_tag(q, rq);
1136
1137 rq->flags &= ~REQ_STARTED;
1138 __elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 0);
1139 }
1140}
1141
1142EXPORT_SYMBOL(blk_queue_invalidate_tags);
1143
Arjan van de Ven64100092006-01-06 09:46:02 +01001144static const char * const rq_flags[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 "REQ_RW",
1146 "REQ_FAILFAST",
Tejun Heo8922e162005-10-20 16:23:44 +02001147 "REQ_SORTED",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 "REQ_SOFTBARRIER",
1149 "REQ_HARDBARRIER",
Tejun Heo797e7db2006-01-06 09:51:03 +01001150 "REQ_FUA",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 "REQ_CMD",
1152 "REQ_NOMERGE",
1153 "REQ_STARTED",
1154 "REQ_DONTPREP",
1155 "REQ_QUEUED",
Tejun Heocb98fc82005-10-28 08:29:39 +02001156 "REQ_ELVPRIV",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 "REQ_PC",
1158 "REQ_BLOCK_PC",
1159 "REQ_SENSE",
1160 "REQ_FAILED",
1161 "REQ_QUIET",
1162 "REQ_SPECIAL",
1163 "REQ_DRIVE_CMD",
1164 "REQ_DRIVE_TASK",
1165 "REQ_DRIVE_TASKFILE",
1166 "REQ_PREEMPT",
1167 "REQ_PM_SUSPEND",
1168 "REQ_PM_RESUME",
1169 "REQ_PM_SHUTDOWN",
Tejun Heo797e7db2006-01-06 09:51:03 +01001170 "REQ_ORDERED_COLOR",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171};
1172
1173void blk_dump_rq_flags(struct request *rq, char *msg)
1174{
1175 int bit;
1176
1177 printk("%s: dev %s: flags = ", msg,
1178 rq->rq_disk ? rq->rq_disk->disk_name : "?");
1179 bit = 0;
1180 do {
1181 if (rq->flags & (1 << bit))
1182 printk("%s ", rq_flags[bit]);
1183 bit++;
1184 } while (bit < __REQ_NR_BITS);
1185
1186 printk("\nsector %llu, nr/cnr %lu/%u\n", (unsigned long long)rq->sector,
1187 rq->nr_sectors,
1188 rq->current_nr_sectors);
1189 printk("bio %p, biotail %p, buffer %p, data %p, len %u\n", rq->bio, rq->biotail, rq->buffer, rq->data, rq->data_len);
1190
1191 if (rq->flags & (REQ_BLOCK_PC | REQ_PC)) {
1192 printk("cdb: ");
1193 for (bit = 0; bit < sizeof(rq->cmd); bit++)
1194 printk("%02x ", rq->cmd[bit]);
1195 printk("\n");
1196 }
1197}
1198
1199EXPORT_SYMBOL(blk_dump_rq_flags);
1200
1201void blk_recount_segments(request_queue_t *q, struct bio *bio)
1202{
1203 struct bio_vec *bv, *bvprv = NULL;
1204 int i, nr_phys_segs, nr_hw_segs, seg_size, hw_seg_size, cluster;
1205 int high, highprv = 1;
1206
1207 if (unlikely(!bio->bi_io_vec))
1208 return;
1209
1210 cluster = q->queue_flags & (1 << QUEUE_FLAG_CLUSTER);
1211 hw_seg_size = seg_size = nr_phys_segs = nr_hw_segs = 0;
1212 bio_for_each_segment(bv, bio, i) {
1213 /*
1214 * the trick here is making sure that a high page is never
1215 * considered part of another segment, since that might
1216 * change with the bounce page.
1217 */
1218 high = page_to_pfn(bv->bv_page) >= q->bounce_pfn;
1219 if (high || highprv)
1220 goto new_hw_segment;
1221 if (cluster) {
1222 if (seg_size + bv->bv_len > q->max_segment_size)
1223 goto new_segment;
1224 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bv))
1225 goto new_segment;
1226 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bv))
1227 goto new_segment;
1228 if (BIOVEC_VIRT_OVERSIZE(hw_seg_size + bv->bv_len))
1229 goto new_hw_segment;
1230
1231 seg_size += bv->bv_len;
1232 hw_seg_size += bv->bv_len;
1233 bvprv = bv;
1234 continue;
1235 }
1236new_segment:
1237 if (BIOVEC_VIRT_MERGEABLE(bvprv, bv) &&
1238 !BIOVEC_VIRT_OVERSIZE(hw_seg_size + bv->bv_len)) {
1239 hw_seg_size += bv->bv_len;
1240 } else {
1241new_hw_segment:
1242 if (hw_seg_size > bio->bi_hw_front_size)
1243 bio->bi_hw_front_size = hw_seg_size;
1244 hw_seg_size = BIOVEC_VIRT_START_SIZE(bv) + bv->bv_len;
1245 nr_hw_segs++;
1246 }
1247
1248 nr_phys_segs++;
1249 bvprv = bv;
1250 seg_size = bv->bv_len;
1251 highprv = high;
1252 }
1253 if (hw_seg_size > bio->bi_hw_back_size)
1254 bio->bi_hw_back_size = hw_seg_size;
1255 if (nr_hw_segs == 1 && hw_seg_size > bio->bi_hw_front_size)
1256 bio->bi_hw_front_size = hw_seg_size;
1257 bio->bi_phys_segments = nr_phys_segs;
1258 bio->bi_hw_segments = nr_hw_segs;
1259 bio->bi_flags |= (1 << BIO_SEG_VALID);
1260}
1261
1262
Adrian Bunk93d17d32005-06-25 14:59:10 -07001263static int blk_phys_contig_segment(request_queue_t *q, struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 struct bio *nxt)
1265{
1266 if (!(q->queue_flags & (1 << QUEUE_FLAG_CLUSTER)))
1267 return 0;
1268
1269 if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)))
1270 return 0;
1271 if (bio->bi_size + nxt->bi_size > q->max_segment_size)
1272 return 0;
1273
1274 /*
1275 * bio and nxt are contigous in memory, check if the queue allows
1276 * these two to be merged into one
1277 */
1278 if (BIO_SEG_BOUNDARY(q, bio, nxt))
1279 return 1;
1280
1281 return 0;
1282}
1283
Adrian Bunk93d17d32005-06-25 14:59:10 -07001284static int blk_hw_contig_segment(request_queue_t *q, struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 struct bio *nxt)
1286{
1287 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
1288 blk_recount_segments(q, bio);
1289 if (unlikely(!bio_flagged(nxt, BIO_SEG_VALID)))
1290 blk_recount_segments(q, nxt);
1291 if (!BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)) ||
1292 BIOVEC_VIRT_OVERSIZE(bio->bi_hw_front_size + bio->bi_hw_back_size))
1293 return 0;
1294 if (bio->bi_size + nxt->bi_size > q->max_segment_size)
1295 return 0;
1296
1297 return 1;
1298}
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300/*
1301 * map a request to scatterlist, return number of sg entries setup. Caller
1302 * must make sure sg can hold rq->nr_phys_segments entries
1303 */
1304int blk_rq_map_sg(request_queue_t *q, struct request *rq, struct scatterlist *sg)
1305{
1306 struct bio_vec *bvec, *bvprv;
1307 struct bio *bio;
1308 int nsegs, i, cluster;
1309
1310 nsegs = 0;
1311 cluster = q->queue_flags & (1 << QUEUE_FLAG_CLUSTER);
1312
1313 /*
1314 * for each bio in rq
1315 */
1316 bvprv = NULL;
1317 rq_for_each_bio(bio, rq) {
1318 /*
1319 * for each segment in bio
1320 */
1321 bio_for_each_segment(bvec, bio, i) {
1322 int nbytes = bvec->bv_len;
1323
1324 if (bvprv && cluster) {
1325 if (sg[nsegs - 1].length + nbytes > q->max_segment_size)
1326 goto new_segment;
1327
1328 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
1329 goto new_segment;
1330 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
1331 goto new_segment;
1332
1333 sg[nsegs - 1].length += nbytes;
1334 } else {
1335new_segment:
1336 memset(&sg[nsegs],0,sizeof(struct scatterlist));
1337 sg[nsegs].page = bvec->bv_page;
1338 sg[nsegs].length = nbytes;
1339 sg[nsegs].offset = bvec->bv_offset;
1340
1341 nsegs++;
1342 }
1343 bvprv = bvec;
1344 } /* segments in bio */
1345 } /* bios in rq */
1346
1347 return nsegs;
1348}
1349
1350EXPORT_SYMBOL(blk_rq_map_sg);
1351
1352/*
1353 * the standard queue merge functions, can be overridden with device
1354 * specific ones if so desired
1355 */
1356
1357static inline int ll_new_mergeable(request_queue_t *q,
1358 struct request *req,
1359 struct bio *bio)
1360{
1361 int nr_phys_segs = bio_phys_segments(q, bio);
1362
1363 if (req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
1364 req->flags |= REQ_NOMERGE;
1365 if (req == q->last_merge)
1366 q->last_merge = NULL;
1367 return 0;
1368 }
1369
1370 /*
1371 * A hw segment is just getting larger, bump just the phys
1372 * counter.
1373 */
1374 req->nr_phys_segments += nr_phys_segs;
1375 return 1;
1376}
1377
1378static inline int ll_new_hw_segment(request_queue_t *q,
1379 struct request *req,
1380 struct bio *bio)
1381{
1382 int nr_hw_segs = bio_hw_segments(q, bio);
1383 int nr_phys_segs = bio_phys_segments(q, bio);
1384
1385 if (req->nr_hw_segments + nr_hw_segs > q->max_hw_segments
1386 || req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
1387 req->flags |= REQ_NOMERGE;
1388 if (req == q->last_merge)
1389 q->last_merge = NULL;
1390 return 0;
1391 }
1392
1393 /*
1394 * This will form the start of a new hw segment. Bump both
1395 * counters.
1396 */
1397 req->nr_hw_segments += nr_hw_segs;
1398 req->nr_phys_segments += nr_phys_segs;
1399 return 1;
1400}
1401
1402static int ll_back_merge_fn(request_queue_t *q, struct request *req,
1403 struct bio *bio)
1404{
Mike Christiedefd94b2005-12-05 02:37:06 -06001405 unsigned short max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 int len;
1407
Mike Christiedefd94b2005-12-05 02:37:06 -06001408 if (unlikely(blk_pc_request(req)))
1409 max_sectors = q->max_hw_sectors;
1410 else
1411 max_sectors = q->max_sectors;
1412
1413 if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 req->flags |= REQ_NOMERGE;
1415 if (req == q->last_merge)
1416 q->last_merge = NULL;
1417 return 0;
1418 }
1419 if (unlikely(!bio_flagged(req->biotail, BIO_SEG_VALID)))
1420 blk_recount_segments(q, req->biotail);
1421 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
1422 blk_recount_segments(q, bio);
1423 len = req->biotail->bi_hw_back_size + bio->bi_hw_front_size;
1424 if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(req->biotail), __BVEC_START(bio)) &&
1425 !BIOVEC_VIRT_OVERSIZE(len)) {
1426 int mergeable = ll_new_mergeable(q, req, bio);
1427
1428 if (mergeable) {
1429 if (req->nr_hw_segments == 1)
1430 req->bio->bi_hw_front_size = len;
1431 if (bio->bi_hw_segments == 1)
1432 bio->bi_hw_back_size = len;
1433 }
1434 return mergeable;
1435 }
1436
1437 return ll_new_hw_segment(q, req, bio);
1438}
1439
1440static int ll_front_merge_fn(request_queue_t *q, struct request *req,
1441 struct bio *bio)
1442{
Mike Christiedefd94b2005-12-05 02:37:06 -06001443 unsigned short max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 int len;
1445
Mike Christiedefd94b2005-12-05 02:37:06 -06001446 if (unlikely(blk_pc_request(req)))
1447 max_sectors = q->max_hw_sectors;
1448 else
1449 max_sectors = q->max_sectors;
1450
1451
1452 if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 req->flags |= REQ_NOMERGE;
1454 if (req == q->last_merge)
1455 q->last_merge = NULL;
1456 return 0;
1457 }
1458 len = bio->bi_hw_back_size + req->bio->bi_hw_front_size;
1459 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
1460 blk_recount_segments(q, bio);
1461 if (unlikely(!bio_flagged(req->bio, BIO_SEG_VALID)))
1462 blk_recount_segments(q, req->bio);
1463 if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(req->bio)) &&
1464 !BIOVEC_VIRT_OVERSIZE(len)) {
1465 int mergeable = ll_new_mergeable(q, req, bio);
1466
1467 if (mergeable) {
1468 if (bio->bi_hw_segments == 1)
1469 bio->bi_hw_front_size = len;
1470 if (req->nr_hw_segments == 1)
1471 req->biotail->bi_hw_back_size = len;
1472 }
1473 return mergeable;
1474 }
1475
1476 return ll_new_hw_segment(q, req, bio);
1477}
1478
1479static int ll_merge_requests_fn(request_queue_t *q, struct request *req,
1480 struct request *next)
1481{
Nikita Danilovdfa1a552005-06-25 14:59:20 -07001482 int total_phys_segments;
1483 int total_hw_segments;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
1485 /*
1486 * First check if the either of the requests are re-queued
1487 * requests. Can't merge them if they are.
1488 */
1489 if (req->special || next->special)
1490 return 0;
1491
1492 /*
Nikita Danilovdfa1a552005-06-25 14:59:20 -07001493 * Will it become too large?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 */
1495 if ((req->nr_sectors + next->nr_sectors) > q->max_sectors)
1496 return 0;
1497
1498 total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
1499 if (blk_phys_contig_segment(q, req->biotail, next->bio))
1500 total_phys_segments--;
1501
1502 if (total_phys_segments > q->max_phys_segments)
1503 return 0;
1504
1505 total_hw_segments = req->nr_hw_segments + next->nr_hw_segments;
1506 if (blk_hw_contig_segment(q, req->biotail, next->bio)) {
1507 int len = req->biotail->bi_hw_back_size + next->bio->bi_hw_front_size;
1508 /*
1509 * propagate the combined length to the end of the requests
1510 */
1511 if (req->nr_hw_segments == 1)
1512 req->bio->bi_hw_front_size = len;
1513 if (next->nr_hw_segments == 1)
1514 next->biotail->bi_hw_back_size = len;
1515 total_hw_segments--;
1516 }
1517
1518 if (total_hw_segments > q->max_hw_segments)
1519 return 0;
1520
1521 /* Merge is OK... */
1522 req->nr_phys_segments = total_phys_segments;
1523 req->nr_hw_segments = total_hw_segments;
1524 return 1;
1525}
1526
1527/*
1528 * "plug" the device if there are no outstanding requests: this will
1529 * force the transfer to start only after we have put all the requests
1530 * on the list.
1531 *
1532 * This is called with interrupts off and no requests on the queue and
1533 * with the queue lock held.
1534 */
1535void blk_plug_device(request_queue_t *q)
1536{
1537 WARN_ON(!irqs_disabled());
1538
1539 /*
1540 * don't plug a stopped queue, it must be paired with blk_start_queue()
1541 * which will restart the queueing
1542 */
1543 if (test_bit(QUEUE_FLAG_STOPPED, &q->queue_flags))
1544 return;
1545
1546 if (!test_and_set_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags))
1547 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
1548}
1549
1550EXPORT_SYMBOL(blk_plug_device);
1551
1552/*
1553 * remove the queue from the plugged list, if present. called with
1554 * queue lock held and interrupts disabled.
1555 */
1556int blk_remove_plug(request_queue_t *q)
1557{
1558 WARN_ON(!irqs_disabled());
1559
1560 if (!test_and_clear_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags))
1561 return 0;
1562
1563 del_timer(&q->unplug_timer);
1564 return 1;
1565}
1566
1567EXPORT_SYMBOL(blk_remove_plug);
1568
1569/*
1570 * remove the plug and let it rip..
1571 */
1572void __generic_unplug_device(request_queue_t *q)
1573{
Nick Pigginfde6ad22005-06-23 00:08:53 -07001574 if (unlikely(test_bit(QUEUE_FLAG_STOPPED, &q->queue_flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 return;
1576
1577 if (!blk_remove_plug(q))
1578 return;
1579
Jens Axboe22e2c502005-06-27 10:55:12 +02001580 q->request_fn(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581}
1582EXPORT_SYMBOL(__generic_unplug_device);
1583
1584/**
1585 * generic_unplug_device - fire a request queue
1586 * @q: The &request_queue_t in question
1587 *
1588 * Description:
1589 * Linux uses plugging to build bigger requests queues before letting
1590 * the device have at them. If a queue is plugged, the I/O scheduler
1591 * is still adding and merging requests on the queue. Once the queue
1592 * gets unplugged, the request_fn defined for the queue is invoked and
1593 * transfers started.
1594 **/
1595void generic_unplug_device(request_queue_t *q)
1596{
1597 spin_lock_irq(q->queue_lock);
1598 __generic_unplug_device(q);
1599 spin_unlock_irq(q->queue_lock);
1600}
1601EXPORT_SYMBOL(generic_unplug_device);
1602
1603static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
1604 struct page *page)
1605{
1606 request_queue_t *q = bdi->unplug_io_data;
1607
1608 /*
1609 * devices don't necessarily have an ->unplug_fn defined
1610 */
1611 if (q->unplug_fn)
1612 q->unplug_fn(q);
1613}
1614
1615static void blk_unplug_work(void *data)
1616{
1617 request_queue_t *q = data;
1618
1619 q->unplug_fn(q);
1620}
1621
1622static void blk_unplug_timeout(unsigned long data)
1623{
1624 request_queue_t *q = (request_queue_t *)data;
1625
1626 kblockd_schedule_work(&q->unplug_work);
1627}
1628
1629/**
1630 * blk_start_queue - restart a previously stopped queue
1631 * @q: The &request_queue_t in question
1632 *
1633 * Description:
1634 * blk_start_queue() will clear the stop flag on the queue, and call
1635 * the request_fn for the queue if it was in a stopped state when
1636 * entered. Also see blk_stop_queue(). Queue lock must be held.
1637 **/
1638void blk_start_queue(request_queue_t *q)
1639{
1640 clear_bit(QUEUE_FLAG_STOPPED, &q->queue_flags);
1641
1642 /*
1643 * one level of recursion is ok and is much faster than kicking
1644 * the unplug handling
1645 */
1646 if (!test_and_set_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) {
1647 q->request_fn(q);
1648 clear_bit(QUEUE_FLAG_REENTER, &q->queue_flags);
1649 } else {
1650 blk_plug_device(q);
1651 kblockd_schedule_work(&q->unplug_work);
1652 }
1653}
1654
1655EXPORT_SYMBOL(blk_start_queue);
1656
1657/**
1658 * blk_stop_queue - stop a queue
1659 * @q: The &request_queue_t in question
1660 *
1661 * Description:
1662 * The Linux block layer assumes that a block driver will consume all
1663 * entries on the request queue when the request_fn strategy is called.
1664 * Often this will not happen, because of hardware limitations (queue
1665 * depth settings). If a device driver gets a 'queue full' response,
1666 * or if it simply chooses not to queue more I/O at one point, it can
1667 * call this function to prevent the request_fn from being called until
1668 * the driver has signalled it's ready to go again. This happens by calling
1669 * blk_start_queue() to restart queue operations. Queue lock must be held.
1670 **/
1671void blk_stop_queue(request_queue_t *q)
1672{
1673 blk_remove_plug(q);
1674 set_bit(QUEUE_FLAG_STOPPED, &q->queue_flags);
1675}
1676EXPORT_SYMBOL(blk_stop_queue);
1677
1678/**
1679 * blk_sync_queue - cancel any pending callbacks on a queue
1680 * @q: the queue
1681 *
1682 * Description:
1683 * The block layer may perform asynchronous callback activity
1684 * on a queue, such as calling the unplug function after a timeout.
1685 * A block device may call blk_sync_queue to ensure that any
1686 * such activity is cancelled, thus allowing it to release resources
1687 * the the callbacks might use. The caller must already have made sure
1688 * that its ->make_request_fn will not re-add plugging prior to calling
1689 * this function.
1690 *
1691 */
1692void blk_sync_queue(struct request_queue *q)
1693{
1694 del_timer_sync(&q->unplug_timer);
1695 kblockd_flush();
1696}
1697EXPORT_SYMBOL(blk_sync_queue);
1698
1699/**
1700 * blk_run_queue - run a single device queue
1701 * @q: The queue to run
1702 */
1703void blk_run_queue(struct request_queue *q)
1704{
1705 unsigned long flags;
1706
1707 spin_lock_irqsave(q->queue_lock, flags);
1708 blk_remove_plug(q);
Ken Chena2997382005-04-16 15:25:43 -07001709 if (!elv_queue_empty(q))
1710 q->request_fn(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 spin_unlock_irqrestore(q->queue_lock, flags);
1712}
1713EXPORT_SYMBOL(blk_run_queue);
1714
1715/**
1716 * blk_cleanup_queue: - release a &request_queue_t when it is no longer needed
1717 * @q: the request queue to be released
1718 *
1719 * Description:
1720 * blk_cleanup_queue is the pair to blk_init_queue() or
1721 * blk_queue_make_request(). It should be called when a request queue is
1722 * being released; typically when a block device is being de-registered.
1723 * Currently, its primary task it to free all the &struct request
1724 * structures that were allocated to the queue and the queue itself.
1725 *
1726 * Caveat:
1727 * Hopefully the low level driver will have finished any
1728 * outstanding requests first...
1729 **/
1730void blk_cleanup_queue(request_queue_t * q)
1731{
1732 struct request_list *rl = &q->rq;
1733
1734 if (!atomic_dec_and_test(&q->refcnt))
1735 return;
1736
1737 if (q->elevator)
1738 elevator_exit(q->elevator);
1739
1740 blk_sync_queue(q);
1741
1742 if (rl->rq_pool)
1743 mempool_destroy(rl->rq_pool);
1744
1745 if (q->queue_tags)
1746 __blk_queue_free_tags(q);
1747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 kmem_cache_free(requestq_cachep, q);
1749}
1750
1751EXPORT_SYMBOL(blk_cleanup_queue);
1752
1753static int blk_init_free_list(request_queue_t *q)
1754{
1755 struct request_list *rl = &q->rq;
1756
1757 rl->count[READ] = rl->count[WRITE] = 0;
1758 rl->starved[READ] = rl->starved[WRITE] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +02001759 rl->elvpriv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 init_waitqueue_head(&rl->wait[READ]);
1761 init_waitqueue_head(&rl->wait[WRITE]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Christoph Lameter19460892005-06-23 00:08:19 -07001763 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
1764 mempool_free_slab, request_cachep, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 if (!rl->rq_pool)
1767 return -ENOMEM;
1768
1769 return 0;
1770}
1771
Al Viro8267e262005-10-21 03:20:53 -04001772request_queue_t *blk_alloc_queue(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
Christoph Lameter19460892005-06-23 00:08:19 -07001774 return blk_alloc_queue_node(gfp_mask, -1);
1775}
1776EXPORT_SYMBOL(blk_alloc_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Al Viro8267e262005-10-21 03:20:53 -04001778request_queue_t *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -07001779{
1780 request_queue_t *q;
1781
1782 q = kmem_cache_alloc_node(requestq_cachep, gfp_mask, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 if (!q)
1784 return NULL;
1785
1786 memset(q, 0, sizeof(*q));
1787 init_timer(&q->unplug_timer);
1788 atomic_set(&q->refcnt, 1);
1789
1790 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
1791 q->backing_dev_info.unplug_io_data = q;
1792
1793 return q;
1794}
Christoph Lameter19460892005-06-23 00:08:19 -07001795EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
1797/**
1798 * blk_init_queue - prepare a request queue for use with a block device
1799 * @rfn: The function to be called to process requests that have been
1800 * placed on the queue.
1801 * @lock: Request queue spin lock
1802 *
1803 * Description:
1804 * If a block device wishes to use the standard request handling procedures,
1805 * which sorts requests and coalesces adjacent requests, then it must
1806 * call blk_init_queue(). The function @rfn will be called when there
1807 * are requests on the queue that need to be processed. If the device
1808 * supports plugging, then @rfn may not be called immediately when requests
1809 * are available on the queue, but may be called at some time later instead.
1810 * Plugged queues are generally unplugged when a buffer belonging to one
1811 * of the requests on the queue is needed, or due to memory pressure.
1812 *
1813 * @rfn is not required, or even expected, to remove all requests off the
1814 * queue, but only as many as it can handle at a time. If it does leave
1815 * requests on the queue, it is responsible for arranging that the requests
1816 * get dealt with eventually.
1817 *
1818 * The queue spin lock must be held while manipulating the requests on the
1819 * request queue.
1820 *
1821 * Function returns a pointer to the initialized request queue, or NULL if
1822 * it didn't succeed.
1823 *
1824 * Note:
1825 * blk_init_queue() must be paired with a blk_cleanup_queue() call
1826 * when the block device is deactivated (such as at module unload).
1827 **/
Christoph Lameter19460892005-06-23 00:08:19 -07001828
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829request_queue_t *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
1830{
Christoph Lameter19460892005-06-23 00:08:19 -07001831 return blk_init_queue_node(rfn, lock, -1);
1832}
1833EXPORT_SYMBOL(blk_init_queue);
1834
1835request_queue_t *
1836blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
1837{
1838 request_queue_t *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
1840 if (!q)
1841 return NULL;
1842
Christoph Lameter19460892005-06-23 00:08:19 -07001843 q->node = node_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 if (blk_init_free_list(q))
1845 goto out_init;
1846
152587d2005-04-12 16:22:06 -05001847 /*
1848 * if caller didn't supply a lock, they get per-queue locking with
1849 * our embedded lock
1850 */
1851 if (!lock) {
1852 spin_lock_init(&q->__queue_lock);
1853 lock = &q->__queue_lock;
1854 }
1855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 q->request_fn = rfn;
1857 q->back_merge_fn = ll_back_merge_fn;
1858 q->front_merge_fn = ll_front_merge_fn;
1859 q->merge_requests_fn = ll_merge_requests_fn;
1860 q->prep_rq_fn = NULL;
1861 q->unplug_fn = generic_unplug_device;
1862 q->queue_flags = (1 << QUEUE_FLAG_CLUSTER);
1863 q->queue_lock = lock;
1864
1865 blk_queue_segment_boundary(q, 0xffffffff);
1866
1867 blk_queue_make_request(q, __make_request);
1868 blk_queue_max_segment_size(q, MAX_SEGMENT_SIZE);
1869
1870 blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS);
1871 blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS);
1872
1873 /*
1874 * all done
1875 */
1876 if (!elevator_init(q, NULL)) {
1877 blk_queue_congestion_threshold(q);
1878 return q;
1879 }
1880
1881 blk_cleanup_queue(q);
1882out_init:
1883 kmem_cache_free(requestq_cachep, q);
1884 return NULL;
1885}
Christoph Lameter19460892005-06-23 00:08:19 -07001886EXPORT_SYMBOL(blk_init_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
1888int blk_get_queue(request_queue_t *q)
1889{
Nick Pigginfde6ad22005-06-23 00:08:53 -07001890 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 atomic_inc(&q->refcnt);
1892 return 0;
1893 }
1894
1895 return 1;
1896}
1897
1898EXPORT_SYMBOL(blk_get_queue);
1899
1900static inline void blk_free_request(request_queue_t *q, struct request *rq)
1901{
Tejun Heocb98fc82005-10-28 08:29:39 +02001902 if (rq->flags & REQ_ELVPRIV)
1903 elv_put_request(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 mempool_free(rq, q->rq.rq_pool);
1905}
1906
Jens Axboe22e2c502005-06-27 10:55:12 +02001907static inline struct request *
Tejun Heocb98fc82005-10-28 08:29:39 +02001908blk_alloc_request(request_queue_t *q, int rw, struct bio *bio,
Linus Torvalds5dd96242005-10-28 08:56:34 -07001909 int priv, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
1911 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
1912
1913 if (!rq)
1914 return NULL;
1915
1916 /*
1917 * first three bits are identical in rq->flags and bio->bi_rw,
1918 * see bio.h and blkdev.h
1919 */
1920 rq->flags = rw;
1921
Tejun Heocb98fc82005-10-28 08:29:39 +02001922 if (priv) {
1923 if (unlikely(elv_set_request(q, rq, bio, gfp_mask))) {
1924 mempool_free(rq, q->rq.rq_pool);
1925 return NULL;
1926 }
1927 rq->flags |= REQ_ELVPRIV;
1928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Tejun Heocb98fc82005-10-28 08:29:39 +02001930 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931}
1932
1933/*
1934 * ioc_batching returns true if the ioc is a valid batching request and
1935 * should be given priority access to a request.
1936 */
1937static inline int ioc_batching(request_queue_t *q, struct io_context *ioc)
1938{
1939 if (!ioc)
1940 return 0;
1941
1942 /*
1943 * Make sure the process is able to allocate at least 1 request
1944 * even if the batch times out, otherwise we could theoretically
1945 * lose wakeups.
1946 */
1947 return ioc->nr_batch_requests == q->nr_batching ||
1948 (ioc->nr_batch_requests > 0
1949 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
1950}
1951
1952/*
1953 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
1954 * will cause the process to be a "batcher" on all queues in the system. This
1955 * is the behaviour we want though - once it gets a wakeup it should be given
1956 * a nice run.
1957 */
Adrian Bunk93d17d32005-06-25 14:59:10 -07001958static void ioc_set_batching(request_queue_t *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
1960 if (!ioc || ioc_batching(q, ioc))
1961 return;
1962
1963 ioc->nr_batch_requests = q->nr_batching;
1964 ioc->last_waited = jiffies;
1965}
1966
1967static void __freed_request(request_queue_t *q, int rw)
1968{
1969 struct request_list *rl = &q->rq;
1970
1971 if (rl->count[rw] < queue_congestion_off_threshold(q))
1972 clear_queue_congested(q, rw);
1973
1974 if (rl->count[rw] + 1 <= q->nr_requests) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 if (waitqueue_active(&rl->wait[rw]))
1976 wake_up(&rl->wait[rw]);
1977
1978 blk_clear_queue_full(q, rw);
1979 }
1980}
1981
1982/*
1983 * A request has just been released. Account for it, update the full and
1984 * congestion status, wake up any waiters. Called under q->queue_lock.
1985 */
Tejun Heocb98fc82005-10-28 08:29:39 +02001986static void freed_request(request_queue_t *q, int rw, int priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987{
1988 struct request_list *rl = &q->rq;
1989
1990 rl->count[rw]--;
Tejun Heocb98fc82005-10-28 08:29:39 +02001991 if (priv)
1992 rl->elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
1994 __freed_request(q, rw);
1995
1996 if (unlikely(rl->starved[rw ^ 1]))
1997 __freed_request(q, rw ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998}
1999
2000#define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist)
2001/*
Nick Piggind6344532005-06-28 20:45:14 -07002002 * Get a free request, queue_lock must be held.
2003 * Returns NULL on failure, with queue_lock held.
2004 * Returns !NULL on success, with queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 */
Jens Axboe22e2c502005-06-27 10:55:12 +02002006static struct request *get_request(request_queue_t *q, int rw, struct bio *bio,
Al Viro8267e262005-10-21 03:20:53 -04002007 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008{
2009 struct request *rq = NULL;
2010 struct request_list *rl = &q->rq;
Jens Axboe88ee5ef2005-11-12 11:09:12 +01002011 struct io_context *ioc = NULL;
2012 int may_queue, priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Jens Axboe88ee5ef2005-11-12 11:09:12 +01002014 may_queue = elv_may_queue(q, rw, bio);
2015 if (may_queue == ELV_MQUEUE_NO)
2016 goto rq_starved;
2017
2018 if (rl->count[rw]+1 >= queue_congestion_on_threshold(q)) {
2019 if (rl->count[rw]+1 >= q->nr_requests) {
2020 ioc = current_io_context(GFP_ATOMIC);
2021 /*
2022 * The queue will fill after this allocation, so set
2023 * it as full, and mark this process as "batching".
2024 * This process will be allowed to complete a batch of
2025 * requests, others will be blocked.
2026 */
2027 if (!blk_queue_full(q, rw)) {
2028 ioc_set_batching(q, ioc);
2029 blk_set_queue_full(q, rw);
2030 } else {
2031 if (may_queue != ELV_MQUEUE_MUST
2032 && !ioc_batching(q, ioc)) {
2033 /*
2034 * The queue is full and the allocating
2035 * process is not a "batcher", and not
2036 * exempted by the IO scheduler
2037 */
2038 goto out;
2039 }
2040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 }
Jens Axboe88ee5ef2005-11-12 11:09:12 +01002042 set_queue_congested(q, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 }
2044
Jens Axboe082cf692005-06-28 16:35:11 +02002045 /*
2046 * Only allow batching queuers to allocate up to 50% over the defined
2047 * limit of requests, otherwise we could have thousands of requests
2048 * allocated with any setting of ->nr_requests
2049 */
Hugh Dickinsfd782a42005-06-29 15:15:40 +01002050 if (rl->count[rw] >= (3 * q->nr_requests / 2))
Jens Axboe082cf692005-06-28 16:35:11 +02002051 goto out;
Hugh Dickinsfd782a42005-06-29 15:15:40 +01002052
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 rl->count[rw]++;
2054 rl->starved[rw] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +02002055
Jens Axboe64521d12005-10-28 08:30:39 +02002056 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Tejun Heocb98fc82005-10-28 08:29:39 +02002057 if (priv)
2058 rl->elvpriv++;
2059
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 spin_unlock_irq(q->queue_lock);
2061
Tejun Heocb98fc82005-10-28 08:29:39 +02002062 rq = blk_alloc_request(q, rw, bio, priv, gfp_mask);
Jens Axboe88ee5ef2005-11-12 11:09:12 +01002063 if (unlikely(!rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 /*
2065 * Allocation failed presumably due to memory. Undo anything
2066 * we might have messed up.
2067 *
2068 * Allocating task should really be put onto the front of the
2069 * wait queue, but this is pretty rare.
2070 */
2071 spin_lock_irq(q->queue_lock);
Tejun Heocb98fc82005-10-28 08:29:39 +02002072 freed_request(q, rw, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
2074 /*
2075 * in the very unlikely event that allocation failed and no
2076 * requests for this direction was pending, mark us starved
2077 * so that freeing of a request in the other direction will
2078 * notice us. another possible fix would be to split the
2079 * rq mempool into READ and WRITE
2080 */
2081rq_starved:
2082 if (unlikely(rl->count[rw] == 0))
2083 rl->starved[rw] = 1;
2084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 goto out;
2086 }
2087
Jens Axboe88ee5ef2005-11-12 11:09:12 +01002088 /*
2089 * ioc may be NULL here, and ioc_batching will be false. That's
2090 * OK, if the queue is under the request limit then requests need
2091 * not count toward the nr_batch_requests limit. There will always
2092 * be some limit enforced by BLK_BATCH_TIME.
2093 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 if (ioc_batching(q, ioc))
2095 ioc->nr_batch_requests--;
2096
2097 rq_init(q, rq);
2098 rq->rl = rl;
2099out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 return rq;
2101}
2102
2103/*
2104 * No available requests for this queue, unplug the device and wait for some
2105 * requests to become available.
Nick Piggind6344532005-06-28 20:45:14 -07002106 *
2107 * Called with q->queue_lock held, and returns with it unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 */
Jens Axboe22e2c502005-06-27 10:55:12 +02002109static struct request *get_request_wait(request_queue_t *q, int rw,
2110 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 struct request *rq;
2113
Nick Piggin450991b2005-06-28 20:45:13 -07002114 rq = get_request(q, rw, bio, GFP_NOIO);
2115 while (!rq) {
2116 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 struct request_list *rl = &q->rq;
2118
2119 prepare_to_wait_exclusive(&rl->wait[rw], &wait,
2120 TASK_UNINTERRUPTIBLE);
2121
Jens Axboe22e2c502005-06-27 10:55:12 +02002122 rq = get_request(q, rw, bio, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
2124 if (!rq) {
2125 struct io_context *ioc;
2126
Nick Piggind6344532005-06-28 20:45:14 -07002127 __generic_unplug_device(q);
2128 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 io_schedule();
2130
2131 /*
2132 * After sleeping, we become a "batching" process and
2133 * will be able to allocate at least one request, and
2134 * up to a big batch of them for a small period time.
2135 * See ioc_batching, ioc_set_batching
2136 */
Nick Pigginfb3cc432005-06-28 20:45:15 -07002137 ioc = current_io_context(GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 ioc_set_batching(q, ioc);
Nick Piggind6344532005-06-28 20:45:14 -07002139
2140 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 }
2142 finish_wait(&rl->wait[rw], &wait);
Nick Piggin450991b2005-06-28 20:45:13 -07002143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
2145 return rq;
2146}
2147
Al Viro8267e262005-10-21 03:20:53 -04002148struct request *blk_get_request(request_queue_t *q, int rw, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149{
2150 struct request *rq;
2151
2152 BUG_ON(rw != READ && rw != WRITE);
2153
Nick Piggind6344532005-06-28 20:45:14 -07002154 spin_lock_irq(q->queue_lock);
2155 if (gfp_mask & __GFP_WAIT) {
Jens Axboe22e2c502005-06-27 10:55:12 +02002156 rq = get_request_wait(q, rw, NULL);
Nick Piggind6344532005-06-28 20:45:14 -07002157 } else {
Jens Axboe22e2c502005-06-27 10:55:12 +02002158 rq = get_request(q, rw, NULL, gfp_mask);
Nick Piggind6344532005-06-28 20:45:14 -07002159 if (!rq)
2160 spin_unlock_irq(q->queue_lock);
2161 }
2162 /* q->queue_lock is unlocked at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
2164 return rq;
2165}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166EXPORT_SYMBOL(blk_get_request);
2167
2168/**
2169 * blk_requeue_request - put a request back on queue
2170 * @q: request queue where request should be inserted
2171 * @rq: request to be inserted
2172 *
2173 * Description:
2174 * Drivers often keep queueing requests until the hardware cannot accept
2175 * more, when that condition happens we need to put the request back
2176 * on the queue. Must be called with queue lock held.
2177 */
2178void blk_requeue_request(request_queue_t *q, struct request *rq)
2179{
2180 if (blk_rq_tagged(rq))
2181 blk_queue_end_tag(q, rq);
2182
2183 elv_requeue_request(q, rq);
2184}
2185
2186EXPORT_SYMBOL(blk_requeue_request);
2187
2188/**
2189 * blk_insert_request - insert a special request in to a request queue
2190 * @q: request queue where request should be inserted
2191 * @rq: request to be inserted
2192 * @at_head: insert request at head or tail of queue
2193 * @data: private data
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 *
2195 * Description:
2196 * Many block devices need to execute commands asynchronously, so they don't
2197 * block the whole kernel from preemption during request execution. This is
2198 * accomplished normally by inserting aritficial requests tagged as
2199 * REQ_SPECIAL in to the corresponding request queue, and letting them be
2200 * scheduled for actual execution by the request queue.
2201 *
2202 * We have the option of inserting the head or the tail of the queue.
2203 * Typically we use the tail for new ioctls and so forth. We use the head
2204 * of the queue for things like a QUEUE_FULL message from a device, or a
2205 * host that is unable to accept a particular command.
2206 */
2207void blk_insert_request(request_queue_t *q, struct request *rq,
Tejun Heo 867d1192005-04-24 02:06:05 -05002208 int at_head, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
Tejun Heo 867d1192005-04-24 02:06:05 -05002210 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 unsigned long flags;
2212
2213 /*
2214 * tell I/O scheduler that this isn't a regular read/write (ie it
2215 * must not attempt merges on this) and that it acts as a soft
2216 * barrier
2217 */
2218 rq->flags |= REQ_SPECIAL | REQ_SOFTBARRIER;
2219
2220 rq->special = data;
2221
2222 spin_lock_irqsave(q->queue_lock, flags);
2223
2224 /*
2225 * If command is tagged, release the tag
2226 */
Tejun Heo 867d1192005-04-24 02:06:05 -05002227 if (blk_rq_tagged(rq))
2228 blk_queue_end_tag(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Tejun Heo 867d1192005-04-24 02:06:05 -05002230 drive_stat_acct(rq, rq->nr_sectors, 1);
2231 __elv_add_request(q, rq, where, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 if (blk_queue_plugged(q))
2234 __generic_unplug_device(q);
2235 else
2236 q->request_fn(q);
2237 spin_unlock_irqrestore(q->queue_lock, flags);
2238}
2239
2240EXPORT_SYMBOL(blk_insert_request);
2241
2242/**
2243 * blk_rq_map_user - map user data to a request, for REQ_BLOCK_PC usage
2244 * @q: request queue where request should be inserted
Christoph Hellwig 73747ae2005-06-20 14:21:01 +02002245 * @rq: request structure to fill
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 * @ubuf: the user buffer
2247 * @len: length of user data
2248 *
2249 * Description:
2250 * Data will be mapped directly for zero copy io, if possible. Otherwise
2251 * a kernel bounce buffer is used.
2252 *
2253 * A matching blk_rq_unmap_user() must be issued at the end of io, while
2254 * still in process context.
2255 *
2256 * Note: The mapped bio may need to be bounced through blk_queue_bounce()
2257 * before being submitted to the device, as pages mapped may be out of
2258 * reach. It's the callers responsibility to make sure this happens. The
2259 * original bio must be passed back in to blk_rq_unmap_user() for proper
2260 * unmapping.
2261 */
Jens Axboedd1cab92005-06-20 14:06:01 +02002262int blk_rq_map_user(request_queue_t *q, struct request *rq, void __user *ubuf,
2263 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264{
2265 unsigned long uaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 struct bio *bio;
Jens Axboedd1cab92005-06-20 14:06:01 +02002267 int reading;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
Mike Christiedefd94b2005-12-05 02:37:06 -06002269 if (len > (q->max_hw_sectors << 9))
Jens Axboedd1cab92005-06-20 14:06:01 +02002270 return -EINVAL;
2271 if (!len || !ubuf)
2272 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
Jens Axboedd1cab92005-06-20 14:06:01 +02002274 reading = rq_data_dir(rq) == READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
2276 /*
2277 * if alignment requirement is satisfied, map in user pages for
2278 * direct dma. else, set up kernel bounce buffers
2279 */
2280 uaddr = (unsigned long) ubuf;
2281 if (!(uaddr & queue_dma_alignment(q)) && !(len & queue_dma_alignment(q)))
Jens Axboedd1cab92005-06-20 14:06:01 +02002282 bio = bio_map_user(q, NULL, uaddr, len, reading);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 else
Jens Axboedd1cab92005-06-20 14:06:01 +02002284 bio = bio_copy_user(q, uaddr, len, reading);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
2286 if (!IS_ERR(bio)) {
2287 rq->bio = rq->biotail = bio;
2288 blk_rq_bio_prep(q, rq, bio);
2289
2290 rq->buffer = rq->data = NULL;
2291 rq->data_len = len;
Jens Axboedd1cab92005-06-20 14:06:01 +02002292 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 }
2294
2295 /*
2296 * bio is the err-ptr
2297 */
Jens Axboedd1cab92005-06-20 14:06:01 +02002298 return PTR_ERR(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299}
2300
2301EXPORT_SYMBOL(blk_rq_map_user);
2302
2303/**
James Bottomley f1970ba2005-06-20 14:06:52 +02002304 * blk_rq_map_user_iov - map user data to a request, for REQ_BLOCK_PC usage
2305 * @q: request queue where request should be inserted
2306 * @rq: request to map data to
2307 * @iov: pointer to the iovec
2308 * @iov_count: number of elements in the iovec
2309 *
2310 * Description:
2311 * Data will be mapped directly for zero copy io, if possible. Otherwise
2312 * a kernel bounce buffer is used.
2313 *
2314 * A matching blk_rq_unmap_user() must be issued at the end of io, while
2315 * still in process context.
2316 *
2317 * Note: The mapped bio may need to be bounced through blk_queue_bounce()
2318 * before being submitted to the device, as pages mapped may be out of
2319 * reach. It's the callers responsibility to make sure this happens. The
2320 * original bio must be passed back in to blk_rq_unmap_user() for proper
2321 * unmapping.
2322 */
2323int blk_rq_map_user_iov(request_queue_t *q, struct request *rq,
2324 struct sg_iovec *iov, int iov_count)
2325{
2326 struct bio *bio;
2327
2328 if (!iov || iov_count <= 0)
2329 return -EINVAL;
2330
2331 /* we don't allow misaligned data like bio_map_user() does. If the
2332 * user is using sg, they're expected to know the alignment constraints
2333 * and respect them accordingly */
2334 bio = bio_map_user_iov(q, NULL, iov, iov_count, rq_data_dir(rq)== READ);
2335 if (IS_ERR(bio))
2336 return PTR_ERR(bio);
2337
2338 rq->bio = rq->biotail = bio;
2339 blk_rq_bio_prep(q, rq, bio);
2340 rq->buffer = rq->data = NULL;
2341 rq->data_len = bio->bi_size;
2342 return 0;
2343}
2344
2345EXPORT_SYMBOL(blk_rq_map_user_iov);
2346
2347/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 * blk_rq_unmap_user - unmap a request with user data
Christoph Hellwig 73747ae2005-06-20 14:21:01 +02002349 * @bio: bio to be unmapped
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 * @ulen: length of user buffer
2351 *
2352 * Description:
Christoph Hellwig 73747ae2005-06-20 14:21:01 +02002353 * Unmap a bio previously mapped by blk_rq_map_user().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 */
Jens Axboedd1cab92005-06-20 14:06:01 +02002355int blk_rq_unmap_user(struct bio *bio, unsigned int ulen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356{
2357 int ret = 0;
2358
2359 if (bio) {
2360 if (bio_flagged(bio, BIO_USER_MAPPED))
2361 bio_unmap_user(bio);
2362 else
2363 ret = bio_uncopy_user(bio);
2364 }
2365
Jens Axboedd1cab92005-06-20 14:06:01 +02002366 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367}
2368
2369EXPORT_SYMBOL(blk_rq_unmap_user);
2370
2371/**
Mike Christie df46b9a2005-06-20 14:04:44 +02002372 * blk_rq_map_kern - map kernel data to a request, for REQ_BLOCK_PC usage
2373 * @q: request queue where request should be inserted
Christoph Hellwig 73747ae2005-06-20 14:21:01 +02002374 * @rq: request to fill
Mike Christie df46b9a2005-06-20 14:04:44 +02002375 * @kbuf: the kernel buffer
2376 * @len: length of user data
Christoph Hellwig 73747ae2005-06-20 14:21:01 +02002377 * @gfp_mask: memory allocation flags
Mike Christie df46b9a2005-06-20 14:04:44 +02002378 */
Jens Axboedd1cab92005-06-20 14:06:01 +02002379int blk_rq_map_kern(request_queue_t *q, struct request *rq, void *kbuf,
Al Viro8267e262005-10-21 03:20:53 -04002380 unsigned int len, gfp_t gfp_mask)
Mike Christie df46b9a2005-06-20 14:04:44 +02002381{
Mike Christie df46b9a2005-06-20 14:04:44 +02002382 struct bio *bio;
2383
Mike Christiedefd94b2005-12-05 02:37:06 -06002384 if (len > (q->max_hw_sectors << 9))
Jens Axboedd1cab92005-06-20 14:06:01 +02002385 return -EINVAL;
2386 if (!len || !kbuf)
2387 return -EINVAL;
Mike Christie df46b9a2005-06-20 14:04:44 +02002388
2389 bio = bio_map_kern(q, kbuf, len, gfp_mask);
Jens Axboedd1cab92005-06-20 14:06:01 +02002390 if (IS_ERR(bio))
2391 return PTR_ERR(bio);
Mike Christie df46b9a2005-06-20 14:04:44 +02002392
Jens Axboedd1cab92005-06-20 14:06:01 +02002393 if (rq_data_dir(rq) == WRITE)
2394 bio->bi_rw |= (1 << BIO_RW);
Mike Christie df46b9a2005-06-20 14:04:44 +02002395
Jens Axboedd1cab92005-06-20 14:06:01 +02002396 rq->bio = rq->biotail = bio;
2397 blk_rq_bio_prep(q, rq, bio);
Mike Christie df46b9a2005-06-20 14:04:44 +02002398
Jens Axboedd1cab92005-06-20 14:06:01 +02002399 rq->buffer = rq->data = NULL;
2400 rq->data_len = len;
2401 return 0;
Mike Christie df46b9a2005-06-20 14:04:44 +02002402}
2403
2404EXPORT_SYMBOL(blk_rq_map_kern);
2405
Christoph Hellwig 73747ae2005-06-20 14:21:01 +02002406/**
2407 * blk_execute_rq_nowait - insert a request into queue for execution
2408 * @q: queue to insert the request in
2409 * @bd_disk: matching gendisk
2410 * @rq: request to insert
2411 * @at_head: insert request at head or tail of queue
2412 * @done: I/O completion handler
2413 *
2414 * Description:
2415 * Insert a fully prepared request at the back of the io scheduler queue
2416 * for execution. Don't wait for completion.
2417 */
James Bottomley f1970ba2005-06-20 14:06:52 +02002418void blk_execute_rq_nowait(request_queue_t *q, struct gendisk *bd_disk,
2419 struct request *rq, int at_head,
Tejun Heo8ffdc652006-01-06 09:49:03 +01002420 rq_end_io_fn *done)
James Bottomley f1970ba2005-06-20 14:06:52 +02002421{
2422 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
2423
2424 rq->rq_disk = bd_disk;
2425 rq->flags |= REQ_NOMERGE;
2426 rq->end_io = done;
2427 elv_add_request(q, rq, where, 1);
2428 generic_unplug_device(q);
2429}
2430
Mike Christie6e39b69e2005-11-11 05:30:24 -06002431EXPORT_SYMBOL_GPL(blk_execute_rq_nowait);
2432
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433/**
2434 * blk_execute_rq - insert a request into queue for execution
2435 * @q: queue to insert the request in
2436 * @bd_disk: matching gendisk
2437 * @rq: request to insert
James Bottomley 994ca9a2005-06-20 14:11:09 +02002438 * @at_head: insert request at head or tail of queue
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 *
2440 * Description:
2441 * Insert a fully prepared request at the back of the io scheduler queue
Christoph Hellwig 73747ae2005-06-20 14:21:01 +02002442 * for execution and wait for completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 */
2444int blk_execute_rq(request_queue_t *q, struct gendisk *bd_disk,
James Bottomley 994ca9a2005-06-20 14:11:09 +02002445 struct request *rq, int at_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446{
2447 DECLARE_COMPLETION(wait);
2448 char sense[SCSI_SENSE_BUFFERSIZE];
2449 int err = 0;
2450
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 /*
2452 * we need an extra reference to the request, so we can look at
2453 * it after io completion
2454 */
2455 rq->ref_count++;
2456
2457 if (!rq->sense) {
2458 memset(sense, 0, sizeof(sense));
2459 rq->sense = sense;
2460 rq->sense_len = 0;
2461 }
2462
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 rq->waiting = &wait;
James Bottomley 994ca9a2005-06-20 14:11:09 +02002464 blk_execute_rq_nowait(q, bd_disk, rq, at_head, blk_end_sync_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 wait_for_completion(&wait);
2466 rq->waiting = NULL;
2467
2468 if (rq->errors)
2469 err = -EIO;
2470
2471 return err;
2472}
2473
2474EXPORT_SYMBOL(blk_execute_rq);
2475
2476/**
2477 * blkdev_issue_flush - queue a flush
2478 * @bdev: blockdev to issue flush for
2479 * @error_sector: error sector
2480 *
2481 * Description:
2482 * Issue a flush for the block device in question. Caller can supply
2483 * room for storing the error offset in case of a flush error, if they
2484 * wish to. Caller must run wait_for_completion() on its own.
2485 */
2486int blkdev_issue_flush(struct block_device *bdev, sector_t *error_sector)
2487{
2488 request_queue_t *q;
2489
2490 if (bdev->bd_disk == NULL)
2491 return -ENXIO;
2492
2493 q = bdev_get_queue(bdev);
2494 if (!q)
2495 return -ENXIO;
2496 if (!q->issue_flush_fn)
2497 return -EOPNOTSUPP;
2498
2499 return q->issue_flush_fn(q, bdev->bd_disk, error_sector);
2500}
2501
2502EXPORT_SYMBOL(blkdev_issue_flush);
2503
Adrian Bunk93d17d32005-06-25 14:59:10 -07002504static void drive_stat_acct(struct request *rq, int nr_sectors, int new_io)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505{
2506 int rw = rq_data_dir(rq);
2507
2508 if (!blk_fs_request(rq) || !rq->rq_disk)
2509 return;
2510
Jens Axboed72d9042005-11-01 08:35:42 +01002511 if (!new_io) {
Jens Axboea3623572005-11-01 09:26:16 +01002512 __disk_stat_inc(rq->rq_disk, merges[rw]);
Jens Axboed72d9042005-11-01 08:35:42 +01002513 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 disk_round_stats(rq->rq_disk);
2515 rq->rq_disk->in_flight++;
2516 }
2517}
2518
2519/*
2520 * add-request adds a request to the linked list.
2521 * queue lock is held and interrupts disabled, as we muck with the
2522 * request queue list.
2523 */
2524static inline void add_request(request_queue_t * q, struct request * req)
2525{
2526 drive_stat_acct(req, req->nr_sectors, 1);
2527
2528 if (q->activity_fn)
2529 q->activity_fn(q->activity_data, rq_data_dir(req));
2530
2531 /*
2532 * elevator indicated where it wants this request to be
2533 * inserted at elevator_merge time
2534 */
2535 __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
2536}
2537
2538/*
2539 * disk_round_stats() - Round off the performance stats on a struct
2540 * disk_stats.
2541 *
2542 * The average IO queue length and utilisation statistics are maintained
2543 * by observing the current state of the queue length and the amount of
2544 * time it has been in this state for.
2545 *
2546 * Normally, that accounting is done on IO completion, but that can result
2547 * in more than a second's worth of IO being accounted for within any one
2548 * second, leading to >100% utilisation. To deal with that, we call this
2549 * function to do a round-off before returning the results when reading
2550 * /proc/diskstats. This accounts immediately for all queue usage up to
2551 * the current jiffies and restarts the counters again.
2552 */
2553void disk_round_stats(struct gendisk *disk)
2554{
2555 unsigned long now = jiffies;
2556
Chen, Kenneth Wb2982642005-10-13 21:49:29 +02002557 if (now == disk->stamp)
2558 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
Chen, Kenneth W20e5c812005-10-13 21:48:42 +02002560 if (disk->in_flight) {
2561 __disk_stat_add(disk, time_in_queue,
2562 disk->in_flight * (now - disk->stamp));
2563 __disk_stat_add(disk, io_ticks, (now - disk->stamp));
2564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 disk->stamp = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566}
2567
2568/*
2569 * queue lock must be held
2570 */
Mike Christie6e39b69e2005-11-11 05:30:24 -06002571void __blk_put_request(request_queue_t *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572{
2573 struct request_list *rl = req->rl;
2574
2575 if (unlikely(!q))
2576 return;
2577 if (unlikely(--req->ref_count))
2578 return;
2579
Tejun Heo8922e162005-10-20 16:23:44 +02002580 elv_completed_request(q, req);
2581
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 req->rq_status = RQ_INACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 req->rl = NULL;
2584
2585 /*
2586 * Request may not have originated from ll_rw_blk. if not,
2587 * it didn't come out of our reserved rq pools
2588 */
2589 if (rl) {
2590 int rw = rq_data_dir(req);
Tejun Heocb98fc82005-10-28 08:29:39 +02002591 int priv = req->flags & REQ_ELVPRIV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 BUG_ON(!list_empty(&req->queuelist));
2594
2595 blk_free_request(q, req);
Tejun Heocb98fc82005-10-28 08:29:39 +02002596 freed_request(q, rw, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 }
2598}
2599
Mike Christie6e39b69e2005-11-11 05:30:24 -06002600EXPORT_SYMBOL_GPL(__blk_put_request);
2601
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602void blk_put_request(struct request *req)
2603{
Tejun Heo8922e162005-10-20 16:23:44 +02002604 unsigned long flags;
2605 request_queue_t *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Tejun Heo8922e162005-10-20 16:23:44 +02002607 /*
2608 * Gee, IDE calls in w/ NULL q. Fix IDE and remove the
2609 * following if (q) test.
2610 */
2611 if (q) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 spin_lock_irqsave(q->queue_lock, flags);
2613 __blk_put_request(q, req);
2614 spin_unlock_irqrestore(q->queue_lock, flags);
2615 }
2616}
2617
2618EXPORT_SYMBOL(blk_put_request);
2619
2620/**
2621 * blk_end_sync_rq - executes a completion event on a request
2622 * @rq: request to complete
2623 */
Tejun Heo8ffdc652006-01-06 09:49:03 +01002624void blk_end_sync_rq(struct request *rq, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625{
2626 struct completion *waiting = rq->waiting;
2627
2628 rq->waiting = NULL;
2629 __blk_put_request(rq->q, rq);
2630
2631 /*
2632 * complete last, if this is a stack request the process (and thus
2633 * the rq pointer) could be invalid right after this complete()
2634 */
2635 complete(waiting);
2636}
2637EXPORT_SYMBOL(blk_end_sync_rq);
2638
2639/**
2640 * blk_congestion_wait - wait for a queue to become uncongested
2641 * @rw: READ or WRITE
2642 * @timeout: timeout in jiffies
2643 *
2644 * Waits for up to @timeout jiffies for a queue (any queue) to exit congestion.
2645 * If no queues are congested then just wait for the next request to be
2646 * returned.
2647 */
2648long blk_congestion_wait(int rw, long timeout)
2649{
2650 long ret;
2651 DEFINE_WAIT(wait);
2652 wait_queue_head_t *wqh = &congestion_wqh[rw];
2653
2654 prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
2655 ret = io_schedule_timeout(timeout);
2656 finish_wait(wqh, &wait);
2657 return ret;
2658}
2659
2660EXPORT_SYMBOL(blk_congestion_wait);
2661
2662/*
2663 * Has to be called with the request spinlock acquired
2664 */
2665static int attempt_merge(request_queue_t *q, struct request *req,
2666 struct request *next)
2667{
2668 if (!rq_mergeable(req) || !rq_mergeable(next))
2669 return 0;
2670
2671 /*
2672 * not contigious
2673 */
2674 if (req->sector + req->nr_sectors != next->sector)
2675 return 0;
2676
2677 if (rq_data_dir(req) != rq_data_dir(next)
2678 || req->rq_disk != next->rq_disk
2679 || next->waiting || next->special)
2680 return 0;
2681
2682 /*
2683 * If we are allowed to merge, then append bio list
2684 * from next to rq and release next. merge_requests_fn
2685 * will have updated segment counts, update sector
2686 * counts here.
2687 */
2688 if (!q->merge_requests_fn(q, req, next))
2689 return 0;
2690
2691 /*
2692 * At this point we have either done a back merge
2693 * or front merge. We need the smaller start_time of
2694 * the merged requests to be the current request
2695 * for accounting purposes.
2696 */
2697 if (time_after(req->start_time, next->start_time))
2698 req->start_time = next->start_time;
2699
2700 req->biotail->bi_next = next->bio;
2701 req->biotail = next->biotail;
2702
2703 req->nr_sectors = req->hard_nr_sectors += next->hard_nr_sectors;
2704
2705 elv_merge_requests(q, req, next);
2706
2707 if (req->rq_disk) {
2708 disk_round_stats(req->rq_disk);
2709 req->rq_disk->in_flight--;
2710 }
2711
Jens Axboe22e2c502005-06-27 10:55:12 +02002712 req->ioprio = ioprio_best(req->ioprio, next->ioprio);
2713
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 __blk_put_request(q, next);
2715 return 1;
2716}
2717
2718static inline int attempt_back_merge(request_queue_t *q, struct request *rq)
2719{
2720 struct request *next = elv_latter_request(q, rq);
2721
2722 if (next)
2723 return attempt_merge(q, rq, next);
2724
2725 return 0;
2726}
2727
2728static inline int attempt_front_merge(request_queue_t *q, struct request *rq)
2729{
2730 struct request *prev = elv_former_request(q, rq);
2731
2732 if (prev)
2733 return attempt_merge(q, prev, rq);
2734
2735 return 0;
2736}
2737
2738/**
2739 * blk_attempt_remerge - attempt to remerge active head with next request
2740 * @q: The &request_queue_t belonging to the device
2741 * @rq: The head request (usually)
2742 *
2743 * Description:
2744 * For head-active devices, the queue can easily be unplugged so quickly
2745 * that proper merging is not done on the front request. This may hurt
2746 * performance greatly for some devices. The block layer cannot safely
2747 * do merging on that first request for these queues, but the driver can
2748 * call this function and make it happen any way. Only the driver knows
2749 * when it is safe to do so.
2750 **/
2751void blk_attempt_remerge(request_queue_t *q, struct request *rq)
2752{
2753 unsigned long flags;
2754
2755 spin_lock_irqsave(q->queue_lock, flags);
2756 attempt_back_merge(q, rq);
2757 spin_unlock_irqrestore(q->queue_lock, flags);
2758}
2759
2760EXPORT_SYMBOL(blk_attempt_remerge);
2761
Tejun Heo52d9e672006-01-06 09:49:58 +01002762static void init_request_from_bio(struct request *req, struct bio *bio)
2763{
2764 req->flags |= REQ_CMD;
2765
2766 /*
2767 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
2768 */
2769 if (bio_rw_ahead(bio) || bio_failfast(bio))
2770 req->flags |= REQ_FAILFAST;
2771
2772 /*
2773 * REQ_BARRIER implies no merging, but lets make it explicit
2774 */
2775 if (unlikely(bio_barrier(bio)))
2776 req->flags |= (REQ_HARDBARRIER | REQ_NOMERGE);
2777
2778 req->errors = 0;
2779 req->hard_sector = req->sector = bio->bi_sector;
2780 req->hard_nr_sectors = req->nr_sectors = bio_sectors(bio);
2781 req->current_nr_sectors = req->hard_cur_sectors = bio_cur_sectors(bio);
2782 req->nr_phys_segments = bio_phys_segments(req->q, bio);
2783 req->nr_hw_segments = bio_hw_segments(req->q, bio);
2784 req->buffer = bio_data(bio); /* see ->buffer comment above */
2785 req->waiting = NULL;
2786 req->bio = req->biotail = bio;
2787 req->ioprio = bio_prio(bio);
2788 req->rq_disk = bio->bi_bdev->bd_disk;
2789 req->start_time = jiffies;
2790}
2791
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792static int __make_request(request_queue_t *q, struct bio *bio)
2793{
Nick Piggin450991b2005-06-28 20:45:13 -07002794 struct request *req;
Jens Axboe4a534f92005-04-16 15:25:40 -07002795 int el_ret, rw, nr_sectors, cur_nr_sectors, barrier, err, sync;
Jens Axboe22e2c502005-06-27 10:55:12 +02002796 unsigned short prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 sector_t sector;
2798
2799 sector = bio->bi_sector;
2800 nr_sectors = bio_sectors(bio);
2801 cur_nr_sectors = bio_cur_sectors(bio);
Jens Axboe22e2c502005-06-27 10:55:12 +02002802 prio = bio_prio(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
2804 rw = bio_data_dir(bio);
Jens Axboe4a534f92005-04-16 15:25:40 -07002805 sync = bio_sync(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806
2807 /*
2808 * low level driver can indicate that it wants pages above a
2809 * certain limit bounced to low memory (ie for highmem, or even
2810 * ISA dma in theory)
2811 */
2812 blk_queue_bounce(q, &bio);
2813
2814 spin_lock_prefetch(q->queue_lock);
2815
2816 barrier = bio_barrier(bio);
Tejun Heo797e7db2006-01-06 09:51:03 +01002817 if (unlikely(barrier) && (q->next_ordered == QUEUE_ORDERED_NONE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 err = -EOPNOTSUPP;
2819 goto end_io;
2820 }
2821
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 spin_lock_irq(q->queue_lock);
2823
Nick Piggin450991b2005-06-28 20:45:13 -07002824 if (unlikely(barrier) || elv_queue_empty(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 goto get_rq;
2826
2827 el_ret = elv_merge(q, &req, bio);
2828 switch (el_ret) {
2829 case ELEVATOR_BACK_MERGE:
2830 BUG_ON(!rq_mergeable(req));
2831
2832 if (!q->back_merge_fn(q, req, bio))
2833 break;
2834
2835 req->biotail->bi_next = bio;
2836 req->biotail = bio;
2837 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
Jens Axboe22e2c502005-06-27 10:55:12 +02002838 req->ioprio = ioprio_best(req->ioprio, prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 drive_stat_acct(req, nr_sectors, 0);
2840 if (!attempt_back_merge(q, req))
2841 elv_merged_request(q, req);
2842 goto out;
2843
2844 case ELEVATOR_FRONT_MERGE:
2845 BUG_ON(!rq_mergeable(req));
2846
2847 if (!q->front_merge_fn(q, req, bio))
2848 break;
2849
2850 bio->bi_next = req->bio;
2851 req->bio = bio;
2852
2853 /*
2854 * may not be valid. if the low level driver said
2855 * it didn't need a bounce buffer then it better
2856 * not touch req->buffer either...
2857 */
2858 req->buffer = bio_data(bio);
2859 req->current_nr_sectors = cur_nr_sectors;
2860 req->hard_cur_sectors = cur_nr_sectors;
2861 req->sector = req->hard_sector = sector;
2862 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
Jens Axboe22e2c502005-06-27 10:55:12 +02002863 req->ioprio = ioprio_best(req->ioprio, prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 drive_stat_acct(req, nr_sectors, 0);
2865 if (!attempt_front_merge(q, req))
2866 elv_merged_request(q, req);
2867 goto out;
2868
Nick Piggin450991b2005-06-28 20:45:13 -07002869 /* ELV_NO_MERGE: elevator says don't/can't merge. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 default:
Nick Piggin450991b2005-06-28 20:45:13 -07002871 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 }
2873
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07002875 /*
2876 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07002877 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07002878 */
Nick Piggin450991b2005-06-28 20:45:13 -07002879 req = get_request_wait(q, rw, bio);
Nick Piggind6344532005-06-28 20:45:14 -07002880
Nick Piggin450991b2005-06-28 20:45:13 -07002881 /*
2882 * After dropping the lock and possibly sleeping here, our request
2883 * may now be mergeable after it had proven unmergeable (above).
2884 * We don't worry about that case for efficiency. It won't happen
2885 * often, and the elevators are able to handle it.
2886 */
Tejun Heo52d9e672006-01-06 09:49:58 +01002887 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
Nick Piggin450991b2005-06-28 20:45:13 -07002889 spin_lock_irq(q->queue_lock);
2890 if (elv_queue_empty(q))
2891 blk_plug_device(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 add_request(q, req);
2893out:
Jens Axboe4a534f92005-04-16 15:25:40 -07002894 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 __generic_unplug_device(q);
2896
2897 spin_unlock_irq(q->queue_lock);
2898 return 0;
2899
2900end_io:
2901 bio_endio(bio, nr_sectors << 9, err);
2902 return 0;
2903}
2904
2905/*
2906 * If bio->bi_dev is a partition, remap the location
2907 */
2908static inline void blk_partition_remap(struct bio *bio)
2909{
2910 struct block_device *bdev = bio->bi_bdev;
2911
2912 if (bdev != bdev->bd_contains) {
2913 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01002914 const int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915
Jens Axboea3623572005-11-01 09:26:16 +01002916 p->sectors[rw] += bio_sectors(bio);
2917 p->ios[rw]++;
2918
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 bio->bi_sector += p->start_sect;
2920 bio->bi_bdev = bdev->bd_contains;
2921 }
2922}
2923
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924static void handle_bad_sector(struct bio *bio)
2925{
2926 char b[BDEVNAME_SIZE];
2927
2928 printk(KERN_INFO "attempt to access beyond end of device\n");
2929 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
2930 bdevname(bio->bi_bdev, b),
2931 bio->bi_rw,
2932 (unsigned long long)bio->bi_sector + bio_sectors(bio),
2933 (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
2934
2935 set_bit(BIO_EOF, &bio->bi_flags);
2936}
2937
2938/**
2939 * generic_make_request: hand a buffer to its device driver for I/O
2940 * @bio: The bio describing the location in memory and on the device.
2941 *
2942 * generic_make_request() is used to make I/O requests of block
2943 * devices. It is passed a &struct bio, which describes the I/O that needs
2944 * to be done.
2945 *
2946 * generic_make_request() does not return any status. The
2947 * success/failure status of the request, along with notification of
2948 * completion, is delivered asynchronously through the bio->bi_end_io
2949 * function described (one day) else where.
2950 *
2951 * The caller of generic_make_request must make sure that bi_io_vec
2952 * are set to describe the memory buffer, and that bi_dev and bi_sector are
2953 * set to describe the device address, and the
2954 * bi_end_io and optionally bi_private are set to describe how
2955 * completion notification should be signaled.
2956 *
2957 * generic_make_request and the drivers it calls may use bi_next if this
2958 * bio happens to be merged with someone else, and may change bi_dev and
2959 * bi_sector for remaps as it sees fit. So the values of these fields
2960 * should NOT be depended on after the call to generic_make_request.
2961 */
2962void generic_make_request(struct bio *bio)
2963{
2964 request_queue_t *q;
2965 sector_t maxsector;
2966 int ret, nr_sectors = bio_sectors(bio);
2967
2968 might_sleep();
2969 /* Test device or partition size, when known. */
2970 maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
2971 if (maxsector) {
2972 sector_t sector = bio->bi_sector;
2973
2974 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
2975 /*
2976 * This may well happen - the kernel calls bread()
2977 * without checking the size of the device, e.g., when
2978 * mounting a device.
2979 */
2980 handle_bad_sector(bio);
2981 goto end_io;
2982 }
2983 }
2984
2985 /*
2986 * Resolve the mapping until finished. (drivers are
2987 * still free to implement/resolve their own stacking
2988 * by explicitly returning 0)
2989 *
2990 * NOTE: we don't repeat the blk_size check for each new device.
2991 * Stacking drivers are expected to know what they are doing.
2992 */
2993 do {
2994 char b[BDEVNAME_SIZE];
2995
2996 q = bdev_get_queue(bio->bi_bdev);
2997 if (!q) {
2998 printk(KERN_ERR
2999 "generic_make_request: Trying to access "
3000 "nonexistent block-device %s (%Lu)\n",
3001 bdevname(bio->bi_bdev, b),
3002 (long long) bio->bi_sector);
3003end_io:
3004 bio_endio(bio, bio->bi_size, -EIO);
3005 break;
3006 }
3007
3008 if (unlikely(bio_sectors(bio) > q->max_hw_sectors)) {
3009 printk("bio too big device %s (%u > %u)\n",
3010 bdevname(bio->bi_bdev, b),
3011 bio_sectors(bio),
3012 q->max_hw_sectors);
3013 goto end_io;
3014 }
3015
Nick Pigginfde6ad22005-06-23 00:08:53 -07003016 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 goto end_io;
3018
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 /*
3020 * If this device has partitions, remap block n
3021 * of partition p to block n+start(p) of the disk.
3022 */
3023 blk_partition_remap(bio);
3024
3025 ret = q->make_request_fn(q, bio);
3026 } while (ret);
3027}
3028
3029EXPORT_SYMBOL(generic_make_request);
3030
3031/**
3032 * submit_bio: submit a bio to the block device layer for I/O
3033 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
3034 * @bio: The &struct bio which describes the I/O
3035 *
3036 * submit_bio() is very similar in purpose to generic_make_request(), and
3037 * uses that function to do most of the work. Both are fairly rough
3038 * interfaces, @bio must be presetup and ready for I/O.
3039 *
3040 */
3041void submit_bio(int rw, struct bio *bio)
3042{
3043 int count = bio_sectors(bio);
3044
3045 BIO_BUG_ON(!bio->bi_size);
3046 BIO_BUG_ON(!bio->bi_io_vec);
Jens Axboe22e2c502005-06-27 10:55:12 +02003047 bio->bi_rw |= rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 if (rw & WRITE)
3049 mod_page_state(pgpgout, count);
3050 else
3051 mod_page_state(pgpgin, count);
3052
3053 if (unlikely(block_dump)) {
3054 char b[BDEVNAME_SIZE];
3055 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
3056 current->comm, current->pid,
3057 (rw & WRITE) ? "WRITE" : "READ",
3058 (unsigned long long)bio->bi_sector,
3059 bdevname(bio->bi_bdev,b));
3060 }
3061
3062 generic_make_request(bio);
3063}
3064
3065EXPORT_SYMBOL(submit_bio);
3066
Adrian Bunk93d17d32005-06-25 14:59:10 -07003067static void blk_recalc_rq_segments(struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068{
3069 struct bio *bio, *prevbio = NULL;
3070 int nr_phys_segs, nr_hw_segs;
3071 unsigned int phys_size, hw_size;
3072 request_queue_t *q = rq->q;
3073
3074 if (!rq->bio)
3075 return;
3076
3077 phys_size = hw_size = nr_phys_segs = nr_hw_segs = 0;
3078 rq_for_each_bio(bio, rq) {
3079 /* Force bio hw/phys segs to be recalculated. */
3080 bio->bi_flags &= ~(1 << BIO_SEG_VALID);
3081
3082 nr_phys_segs += bio_phys_segments(q, bio);
3083 nr_hw_segs += bio_hw_segments(q, bio);
3084 if (prevbio) {
3085 int pseg = phys_size + prevbio->bi_size + bio->bi_size;
3086 int hseg = hw_size + prevbio->bi_size + bio->bi_size;
3087
3088 if (blk_phys_contig_segment(q, prevbio, bio) &&
3089 pseg <= q->max_segment_size) {
3090 nr_phys_segs--;
3091 phys_size += prevbio->bi_size + bio->bi_size;
3092 } else
3093 phys_size = 0;
3094
3095 if (blk_hw_contig_segment(q, prevbio, bio) &&
3096 hseg <= q->max_segment_size) {
3097 nr_hw_segs--;
3098 hw_size += prevbio->bi_size + bio->bi_size;
3099 } else
3100 hw_size = 0;
3101 }
3102 prevbio = bio;
3103 }
3104
3105 rq->nr_phys_segments = nr_phys_segs;
3106 rq->nr_hw_segments = nr_hw_segs;
3107}
3108
Adrian Bunk93d17d32005-06-25 14:59:10 -07003109static void blk_recalc_rq_sectors(struct request *rq, int nsect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110{
3111 if (blk_fs_request(rq)) {
3112 rq->hard_sector += nsect;
3113 rq->hard_nr_sectors -= nsect;
3114
3115 /*
3116 * Move the I/O submission pointers ahead if required.
3117 */
3118 if ((rq->nr_sectors >= rq->hard_nr_sectors) &&
3119 (rq->sector <= rq->hard_sector)) {
3120 rq->sector = rq->hard_sector;
3121 rq->nr_sectors = rq->hard_nr_sectors;
3122 rq->hard_cur_sectors = bio_cur_sectors(rq->bio);
3123 rq->current_nr_sectors = rq->hard_cur_sectors;
3124 rq->buffer = bio_data(rq->bio);
3125 }
3126
3127 /*
3128 * if total number of sectors is less than the first segment
3129 * size, something has gone terribly wrong
3130 */
3131 if (rq->nr_sectors < rq->current_nr_sectors) {
3132 printk("blk: request botched\n");
3133 rq->nr_sectors = rq->current_nr_sectors;
3134 }
3135 }
3136}
3137
3138static int __end_that_request_first(struct request *req, int uptodate,
3139 int nr_bytes)
3140{
3141 int total_bytes, bio_nbytes, error, next_idx = 0;
3142 struct bio *bio;
3143
3144 /*
3145 * extend uptodate bool to allow < 0 value to be direct io error
3146 */
3147 error = 0;
3148 if (end_io_error(uptodate))
3149 error = !uptodate ? -EIO : uptodate;
3150
3151 /*
3152 * for a REQ_BLOCK_PC request, we want to carry any eventual
3153 * sense key with us all the way through
3154 */
3155 if (!blk_pc_request(req))
3156 req->errors = 0;
3157
3158 if (!uptodate) {
3159 if (blk_fs_request(req) && !(req->flags & REQ_QUIET))
3160 printk("end_request: I/O error, dev %s, sector %llu\n",
3161 req->rq_disk ? req->rq_disk->disk_name : "?",
3162 (unsigned long long)req->sector);
3163 }
3164
Jens Axboed72d9042005-11-01 08:35:42 +01003165 if (blk_fs_request(req) && req->rq_disk) {
Jens Axboea3623572005-11-01 09:26:16 +01003166 const int rw = rq_data_dir(req);
3167
3168 __disk_stat_add(req->rq_disk, sectors[rw], nr_bytes >> 9);
Jens Axboed72d9042005-11-01 08:35:42 +01003169 }
3170
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 total_bytes = bio_nbytes = 0;
3172 while ((bio = req->bio) != NULL) {
3173 int nbytes;
3174
3175 if (nr_bytes >= bio->bi_size) {
3176 req->bio = bio->bi_next;
3177 nbytes = bio->bi_size;
Tejun Heo797e7db2006-01-06 09:51:03 +01003178 if (!ordered_bio_endio(req, bio, nbytes, error))
3179 bio_endio(bio, nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 next_idx = 0;
3181 bio_nbytes = 0;
3182 } else {
3183 int idx = bio->bi_idx + next_idx;
3184
3185 if (unlikely(bio->bi_idx >= bio->bi_vcnt)) {
3186 blk_dump_rq_flags(req, "__end_that");
3187 printk("%s: bio idx %d >= vcnt %d\n",
3188 __FUNCTION__,
3189 bio->bi_idx, bio->bi_vcnt);
3190 break;
3191 }
3192
3193 nbytes = bio_iovec_idx(bio, idx)->bv_len;
3194 BIO_BUG_ON(nbytes > bio->bi_size);
3195
3196 /*
3197 * not a complete bvec done
3198 */
3199 if (unlikely(nbytes > nr_bytes)) {
3200 bio_nbytes += nr_bytes;
3201 total_bytes += nr_bytes;
3202 break;
3203 }
3204
3205 /*
3206 * advance to the next vector
3207 */
3208 next_idx++;
3209 bio_nbytes += nbytes;
3210 }
3211
3212 total_bytes += nbytes;
3213 nr_bytes -= nbytes;
3214
3215 if ((bio = req->bio)) {
3216 /*
3217 * end more in this run, or just return 'not-done'
3218 */
3219 if (unlikely(nr_bytes <= 0))
3220 break;
3221 }
3222 }
3223
3224 /*
3225 * completely done
3226 */
3227 if (!req->bio)
3228 return 0;
3229
3230 /*
3231 * if the request wasn't completed, update state
3232 */
3233 if (bio_nbytes) {
Tejun Heo797e7db2006-01-06 09:51:03 +01003234 if (!ordered_bio_endio(req, bio, bio_nbytes, error))
3235 bio_endio(bio, bio_nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 bio->bi_idx += next_idx;
3237 bio_iovec(bio)->bv_offset += nr_bytes;
3238 bio_iovec(bio)->bv_len -= nr_bytes;
3239 }
3240
3241 blk_recalc_rq_sectors(req, total_bytes >> 9);
3242 blk_recalc_rq_segments(req);
3243 return 1;
3244}
3245
3246/**
3247 * end_that_request_first - end I/O on a request
3248 * @req: the request being processed
3249 * @uptodate: 1 for success, 0 for I/O error, < 0 for specific error
3250 * @nr_sectors: number of sectors to end I/O on
3251 *
3252 * Description:
3253 * Ends I/O on a number of sectors attached to @req, and sets it up
3254 * for the next range of segments (if any) in the cluster.
3255 *
3256 * Return:
3257 * 0 - we are done with this request, call end_that_request_last()
3258 * 1 - still buffers pending for this request
3259 **/
3260int end_that_request_first(struct request *req, int uptodate, int nr_sectors)
3261{
3262 return __end_that_request_first(req, uptodate, nr_sectors << 9);
3263}
3264
3265EXPORT_SYMBOL(end_that_request_first);
3266
3267/**
3268 * end_that_request_chunk - end I/O on a request
3269 * @req: the request being processed
3270 * @uptodate: 1 for success, 0 for I/O error, < 0 for specific error
3271 * @nr_bytes: number of bytes to complete
3272 *
3273 * Description:
3274 * Ends I/O on a number of bytes attached to @req, and sets it up
3275 * for the next range of segments (if any). Like end_that_request_first(),
3276 * but deals with bytes instead of sectors.
3277 *
3278 * Return:
3279 * 0 - we are done with this request, call end_that_request_last()
3280 * 1 - still buffers pending for this request
3281 **/
3282int end_that_request_chunk(struct request *req, int uptodate, int nr_bytes)
3283{
3284 return __end_that_request_first(req, uptodate, nr_bytes);
3285}
3286
3287EXPORT_SYMBOL(end_that_request_chunk);
3288
3289/*
3290 * queue lock must be held
3291 */
Tejun Heo8ffdc652006-01-06 09:49:03 +01003292void end_that_request_last(struct request *req, int uptodate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293{
3294 struct gendisk *disk = req->rq_disk;
Tejun Heo8ffdc652006-01-06 09:49:03 +01003295 int error;
3296
3297 /*
3298 * extend uptodate bool to allow < 0 value to be direct io error
3299 */
3300 error = 0;
3301 if (end_io_error(uptodate))
3302 error = !uptodate ? -EIO : uptodate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303
3304 if (unlikely(laptop_mode) && blk_fs_request(req))
3305 laptop_io_completion();
3306
3307 if (disk && blk_fs_request(req)) {
3308 unsigned long duration = jiffies - req->start_time;
Jens Axboea3623572005-11-01 09:26:16 +01003309 const int rw = rq_data_dir(req);
3310
3311 __disk_stat_inc(disk, ios[rw]);
3312 __disk_stat_add(disk, ticks[rw], duration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 disk_round_stats(disk);
3314 disk->in_flight--;
3315 }
3316 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01003317 req->end_io(req, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 else
3319 __blk_put_request(req->q, req);
3320}
3321
3322EXPORT_SYMBOL(end_that_request_last);
3323
3324void end_request(struct request *req, int uptodate)
3325{
3326 if (!end_that_request_first(req, uptodate, req->hard_cur_sectors)) {
3327 add_disk_randomness(req->rq_disk);
3328 blkdev_dequeue_request(req);
Tejun Heo8ffdc652006-01-06 09:49:03 +01003329 end_that_request_last(req, uptodate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 }
3331}
3332
3333EXPORT_SYMBOL(end_request);
3334
3335void blk_rq_bio_prep(request_queue_t *q, struct request *rq, struct bio *bio)
3336{
3337 /* first three bits are identical in rq->flags and bio->bi_rw */
3338 rq->flags |= (bio->bi_rw & 7);
3339
3340 rq->nr_phys_segments = bio_phys_segments(q, bio);
3341 rq->nr_hw_segments = bio_hw_segments(q, bio);
3342 rq->current_nr_sectors = bio_cur_sectors(bio);
3343 rq->hard_cur_sectors = rq->current_nr_sectors;
3344 rq->hard_nr_sectors = rq->nr_sectors = bio_sectors(bio);
3345 rq->buffer = bio_data(bio);
3346
3347 rq->bio = rq->biotail = bio;
3348}
3349
3350EXPORT_SYMBOL(blk_rq_bio_prep);
3351
3352int kblockd_schedule_work(struct work_struct *work)
3353{
3354 return queue_work(kblockd_workqueue, work);
3355}
3356
3357EXPORT_SYMBOL(kblockd_schedule_work);
3358
3359void kblockd_flush(void)
3360{
3361 flush_workqueue(kblockd_workqueue);
3362}
3363EXPORT_SYMBOL(kblockd_flush);
3364
3365int __init blk_dev_init(void)
3366{
3367 kblockd_workqueue = create_workqueue("kblockd");
3368 if (!kblockd_workqueue)
3369 panic("Failed to create kblockd\n");
3370
3371 request_cachep = kmem_cache_create("blkdev_requests",
3372 sizeof(struct request), 0, SLAB_PANIC, NULL, NULL);
3373
3374 requestq_cachep = kmem_cache_create("blkdev_queue",
3375 sizeof(request_queue_t), 0, SLAB_PANIC, NULL, NULL);
3376
3377 iocontext_cachep = kmem_cache_create("blkdev_ioc",
3378 sizeof(struct io_context), 0, SLAB_PANIC, NULL, NULL);
3379
3380 blk_max_low_pfn = max_low_pfn;
3381 blk_max_pfn = max_pfn;
3382
3383 return 0;
3384}
3385
3386/*
3387 * IO Context helper functions
3388 */
3389void put_io_context(struct io_context *ioc)
3390{
3391 if (ioc == NULL)
3392 return;
3393
3394 BUG_ON(atomic_read(&ioc->refcount) == 0);
3395
3396 if (atomic_dec_and_test(&ioc->refcount)) {
3397 if (ioc->aic && ioc->aic->dtor)
3398 ioc->aic->dtor(ioc->aic);
3399 if (ioc->cic && ioc->cic->dtor)
3400 ioc->cic->dtor(ioc->cic);
3401
3402 kmem_cache_free(iocontext_cachep, ioc);
3403 }
3404}
3405EXPORT_SYMBOL(put_io_context);
3406
3407/* Called by the exitting task */
3408void exit_io_context(void)
3409{
3410 unsigned long flags;
3411 struct io_context *ioc;
3412
3413 local_irq_save(flags);
Jens Axboe22e2c502005-06-27 10:55:12 +02003414 task_lock(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 ioc = current->io_context;
3416 current->io_context = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02003417 ioc->task = NULL;
3418 task_unlock(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 local_irq_restore(flags);
3420
3421 if (ioc->aic && ioc->aic->exit)
3422 ioc->aic->exit(ioc->aic);
3423 if (ioc->cic && ioc->cic->exit)
3424 ioc->cic->exit(ioc->cic);
3425
3426 put_io_context(ioc);
3427}
3428
3429/*
3430 * If the current task has no IO context then create one and initialise it.
Nick Pigginfb3cc432005-06-28 20:45:15 -07003431 * Otherwise, return its existing IO context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 *
Nick Pigginfb3cc432005-06-28 20:45:15 -07003433 * This returned IO context doesn't have a specifically elevated refcount,
3434 * but since the current task itself holds a reference, the context can be
3435 * used in general code, so long as it stays within `current` context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 */
Al Viro8267e262005-10-21 03:20:53 -04003437struct io_context *current_io_context(gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438{
3439 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 struct io_context *ret;
3441
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 ret = tsk->io_context;
Nick Pigginfb3cc432005-06-28 20:45:15 -07003443 if (likely(ret))
3444 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445
3446 ret = kmem_cache_alloc(iocontext_cachep, gfp_flags);
3447 if (ret) {
3448 atomic_set(&ret->refcount, 1);
Jens Axboe22e2c502005-06-27 10:55:12 +02003449 ret->task = current;
3450 ret->set_ioprio = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 ret->last_waited = jiffies; /* doesn't matter... */
3452 ret->nr_batch_requests = 0; /* because this is 0 */
3453 ret->aic = NULL;
3454 ret->cic = NULL;
Nick Pigginfb3cc432005-06-28 20:45:15 -07003455 tsk->io_context = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 }
3457
3458 return ret;
3459}
Nick Pigginfb3cc432005-06-28 20:45:15 -07003460EXPORT_SYMBOL(current_io_context);
3461
3462/*
3463 * If the current task has no IO context then create one and initialise it.
3464 * If it does have a context, take a ref on it.
3465 *
3466 * This is always called in the context of the task which submitted the I/O.
3467 */
Al Viro8267e262005-10-21 03:20:53 -04003468struct io_context *get_io_context(gfp_t gfp_flags)
Nick Pigginfb3cc432005-06-28 20:45:15 -07003469{
3470 struct io_context *ret;
3471 ret = current_io_context(gfp_flags);
3472 if (likely(ret))
3473 atomic_inc(&ret->refcount);
3474 return ret;
3475}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476EXPORT_SYMBOL(get_io_context);
3477
3478void copy_io_context(struct io_context **pdst, struct io_context **psrc)
3479{
3480 struct io_context *src = *psrc;
3481 struct io_context *dst = *pdst;
3482
3483 if (src) {
3484 BUG_ON(atomic_read(&src->refcount) == 0);
3485 atomic_inc(&src->refcount);
3486 put_io_context(dst);
3487 *pdst = src;
3488 }
3489}
3490EXPORT_SYMBOL(copy_io_context);
3491
3492void swap_io_context(struct io_context **ioc1, struct io_context **ioc2)
3493{
3494 struct io_context *temp;
3495 temp = *ioc1;
3496 *ioc1 = *ioc2;
3497 *ioc2 = temp;
3498}
3499EXPORT_SYMBOL(swap_io_context);
3500
3501/*
3502 * sysfs parts below
3503 */
3504struct queue_sysfs_entry {
3505 struct attribute attr;
3506 ssize_t (*show)(struct request_queue *, char *);
3507 ssize_t (*store)(struct request_queue *, const char *, size_t);
3508};
3509
3510static ssize_t
3511queue_var_show(unsigned int var, char *page)
3512{
3513 return sprintf(page, "%d\n", var);
3514}
3515
3516static ssize_t
3517queue_var_store(unsigned long *var, const char *page, size_t count)
3518{
3519 char *p = (char *) page;
3520
3521 *var = simple_strtoul(p, &p, 10);
3522 return count;
3523}
3524
3525static ssize_t queue_requests_show(struct request_queue *q, char *page)
3526{
3527 return queue_var_show(q->nr_requests, (page));
3528}
3529
3530static ssize_t
3531queue_requests_store(struct request_queue *q, const char *page, size_t count)
3532{
3533 struct request_list *rl = &q->rq;
3534
3535 int ret = queue_var_store(&q->nr_requests, page, count);
3536 if (q->nr_requests < BLKDEV_MIN_RQ)
3537 q->nr_requests = BLKDEV_MIN_RQ;
3538 blk_queue_congestion_threshold(q);
3539
3540 if (rl->count[READ] >= queue_congestion_on_threshold(q))
3541 set_queue_congested(q, READ);
3542 else if (rl->count[READ] < queue_congestion_off_threshold(q))
3543 clear_queue_congested(q, READ);
3544
3545 if (rl->count[WRITE] >= queue_congestion_on_threshold(q))
3546 set_queue_congested(q, WRITE);
3547 else if (rl->count[WRITE] < queue_congestion_off_threshold(q))
3548 clear_queue_congested(q, WRITE);
3549
3550 if (rl->count[READ] >= q->nr_requests) {
3551 blk_set_queue_full(q, READ);
3552 } else if (rl->count[READ]+1 <= q->nr_requests) {
3553 blk_clear_queue_full(q, READ);
3554 wake_up(&rl->wait[READ]);
3555 }
3556
3557 if (rl->count[WRITE] >= q->nr_requests) {
3558 blk_set_queue_full(q, WRITE);
3559 } else if (rl->count[WRITE]+1 <= q->nr_requests) {
3560 blk_clear_queue_full(q, WRITE);
3561 wake_up(&rl->wait[WRITE]);
3562 }
3563 return ret;
3564}
3565
3566static ssize_t queue_ra_show(struct request_queue *q, char *page)
3567{
3568 int ra_kb = q->backing_dev_info.ra_pages << (PAGE_CACHE_SHIFT - 10);
3569
3570 return queue_var_show(ra_kb, (page));
3571}
3572
3573static ssize_t
3574queue_ra_store(struct request_queue *q, const char *page, size_t count)
3575{
3576 unsigned long ra_kb;
3577 ssize_t ret = queue_var_store(&ra_kb, page, count);
3578
3579 spin_lock_irq(q->queue_lock);
3580 if (ra_kb > (q->max_sectors >> 1))
3581 ra_kb = (q->max_sectors >> 1);
3582
3583 q->backing_dev_info.ra_pages = ra_kb >> (PAGE_CACHE_SHIFT - 10);
3584 spin_unlock_irq(q->queue_lock);
3585
3586 return ret;
3587}
3588
3589static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
3590{
3591 int max_sectors_kb = q->max_sectors >> 1;
3592
3593 return queue_var_show(max_sectors_kb, (page));
3594}
3595
3596static ssize_t
3597queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
3598{
3599 unsigned long max_sectors_kb,
3600 max_hw_sectors_kb = q->max_hw_sectors >> 1,
3601 page_kb = 1 << (PAGE_CACHE_SHIFT - 10);
3602 ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
3603 int ra_kb;
3604
3605 if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
3606 return -EINVAL;
3607 /*
3608 * Take the queue lock to update the readahead and max_sectors
3609 * values synchronously:
3610 */
3611 spin_lock_irq(q->queue_lock);
3612 /*
3613 * Trim readahead window as well, if necessary:
3614 */
3615 ra_kb = q->backing_dev_info.ra_pages << (PAGE_CACHE_SHIFT - 10);
3616 if (ra_kb > max_sectors_kb)
3617 q->backing_dev_info.ra_pages =
3618 max_sectors_kb >> (PAGE_CACHE_SHIFT - 10);
3619
3620 q->max_sectors = max_sectors_kb << 1;
3621 spin_unlock_irq(q->queue_lock);
3622
3623 return ret;
3624}
3625
3626static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
3627{
3628 int max_hw_sectors_kb = q->max_hw_sectors >> 1;
3629
3630 return queue_var_show(max_hw_sectors_kb, (page));
3631}
3632
3633
3634static struct queue_sysfs_entry queue_requests_entry = {
3635 .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
3636 .show = queue_requests_show,
3637 .store = queue_requests_store,
3638};
3639
3640static struct queue_sysfs_entry queue_ra_entry = {
3641 .attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR },
3642 .show = queue_ra_show,
3643 .store = queue_ra_store,
3644};
3645
3646static struct queue_sysfs_entry queue_max_sectors_entry = {
3647 .attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR },
3648 .show = queue_max_sectors_show,
3649 .store = queue_max_sectors_store,
3650};
3651
3652static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
3653 .attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO },
3654 .show = queue_max_hw_sectors_show,
3655};
3656
3657static struct queue_sysfs_entry queue_iosched_entry = {
3658 .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR },
3659 .show = elv_iosched_show,
3660 .store = elv_iosched_store,
3661};
3662
3663static struct attribute *default_attrs[] = {
3664 &queue_requests_entry.attr,
3665 &queue_ra_entry.attr,
3666 &queue_max_hw_sectors_entry.attr,
3667 &queue_max_sectors_entry.attr,
3668 &queue_iosched_entry.attr,
3669 NULL,
3670};
3671
3672#define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
3673
3674static ssize_t
3675queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3676{
3677 struct queue_sysfs_entry *entry = to_queue(attr);
3678 struct request_queue *q;
3679
3680 q = container_of(kobj, struct request_queue, kobj);
3681 if (!entry->show)
Dmitry Torokhov6c1852a2005-04-29 01:26:06 -05003682 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683
3684 return entry->show(q, page);
3685}
3686
3687static ssize_t
3688queue_attr_store(struct kobject *kobj, struct attribute *attr,
3689 const char *page, size_t length)
3690{
3691 struct queue_sysfs_entry *entry = to_queue(attr);
3692 struct request_queue *q;
3693
3694 q = container_of(kobj, struct request_queue, kobj);
3695 if (!entry->store)
Dmitry Torokhov6c1852a2005-04-29 01:26:06 -05003696 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697
3698 return entry->store(q, page, length);
3699}
3700
3701static struct sysfs_ops queue_sysfs_ops = {
3702 .show = queue_attr_show,
3703 .store = queue_attr_store,
3704};
3705
Adrian Bunk93d17d32005-06-25 14:59:10 -07003706static struct kobj_type queue_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 .sysfs_ops = &queue_sysfs_ops,
3708 .default_attrs = default_attrs,
3709};
3710
3711int blk_register_queue(struct gendisk *disk)
3712{
3713 int ret;
3714
3715 request_queue_t *q = disk->queue;
3716
3717 if (!q || !q->request_fn)
3718 return -ENXIO;
3719
3720 q->kobj.parent = kobject_get(&disk->kobj);
3721 if (!q->kobj.parent)
3722 return -EBUSY;
3723
3724 snprintf(q->kobj.name, KOBJ_NAME_LEN, "%s", "queue");
3725 q->kobj.ktype = &queue_ktype;
3726
3727 ret = kobject_register(&q->kobj);
3728 if (ret < 0)
3729 return ret;
3730
3731 ret = elv_register_queue(q);
3732 if (ret) {
3733 kobject_unregister(&q->kobj);
3734 return ret;
3735 }
3736
3737 return 0;
3738}
3739
3740void blk_unregister_queue(struct gendisk *disk)
3741{
3742 request_queue_t *q = disk->queue;
3743
3744 if (q && q->request_fn) {
3745 elv_unregister_queue(q);
3746
3747 kobject_unregister(&q->kobj);
3748 kobject_put(&disk->kobj);
3749 }
3750}