blob: 937f9d0b9bd59111bd2937e6958f4b1cc9dd0733 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 1994, Karl Keyte: Added support for disk statistics
4 * Elevator latency, (C) 2000 Andrea Arcangeli <andrea@suse.de> SuSE
5 * Queue request tables / lock, selectable elevator, Jens Axboe <axboe@suse.de>
6 * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au> - July2000
7 * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001
8 */
9
10/*
11 * This handles all read/write requests to block devices
12 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/backing-dev.h>
16#include <linux/bio.h>
17#include <linux/blkdev.h>
18#include <linux/highmem.h>
19#include <linux/mm.h>
20#include <linux/kernel_stat.h>
21#include <linux/string.h>
22#include <linux/init.h>
23#include <linux/bootmem.h> /* for max_pfn/max_low_pfn */
24#include <linux/completion.h>
25#include <linux/slab.h>
26#include <linux/swap.h>
27#include <linux/writeback.h>
Andrew Mortonfaccbd4b2006-12-10 02:19:35 -080028#include <linux/task_io_accounting_ops.h>
Jens Axboeff856ba2006-01-09 16:02:34 +010029#include <linux/interrupt.h>
30#include <linux/cpu.h>
Jens Axboe2056a782006-03-23 20:00:26 +010031#include <linux/blktrace_api.h>
Akinobu Mitac17bb492006-12-08 02:39:46 -080032#include <linux/fault-inject.h>
Jens Axboef5659132007-09-21 10:44:19 +020033#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Jens Axboe8324aa92008-01-29 14:51:59 +010035#include "blk.h"
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/*
38 * for max sense size
39 */
40#include <scsi/scsi_cmnd.h>
41
David Howells65f27f32006-11-22 14:55:48 +000042static void blk_unplug_work(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043static void blk_unplug_timeout(unsigned long data);
Jerome Marchandb238b3d2007-10-23 15:05:46 +020044static void drive_stat_acct(struct request *rq, int new_io);
Tejun Heo52d9e672006-01-06 09:49:58 +010045static void init_request_from_bio(struct request *req, struct bio *bio);
Jens Axboe165125e2007-07-24 09:28:11 +020046static int __make_request(struct request_queue *q, struct bio *bio);
Jens Axboeb5deef92006-07-19 23:39:40 +020047static struct io_context *current_io_context(gfp_t gfp_flags, int node);
NeilBrown9dfa5282007-08-16 13:27:52 +020048static void blk_recalc_rq_segments(struct request *rq);
NeilBrown66846572007-08-16 13:31:28 +020049static void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
50 struct bio *bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/*
53 * For the allocated request tables
54 */
Jens Axboe8324aa92008-01-29 14:51:59 +010055struct kmem_cache *request_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57/*
58 * For queue allocation
59 */
Jens Axboe8324aa92008-01-29 14:51:59 +010060struct kmem_cache *blk_requestq_cachep = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62/*
63 * For io context allocations
64 */
Christoph Lametere18b8902006-12-06 20:33:20 -080065static struct kmem_cache *iocontext_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/*
68 * Controlling structure to kblockd
69 */
Jens Axboeff856ba2006-01-09 16:02:34 +010070static struct workqueue_struct *kblockd_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72unsigned long blk_max_low_pfn, blk_max_pfn;
73
74EXPORT_SYMBOL(blk_max_low_pfn);
75EXPORT_SYMBOL(blk_max_pfn);
76
Jens Axboeff856ba2006-01-09 16:02:34 +010077static DEFINE_PER_CPU(struct list_head, blk_cpu_done);
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/* Amount of time in which a process may batch requests */
80#define BLK_BATCH_TIME (HZ/50UL)
81
82/* Number of requests a "batching" process may submit */
83#define BLK_BATCH_REQ 32
84
Jens Axboe8324aa92008-01-29 14:51:59 +010085void blk_queue_congestion_threshold(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 int nr;
88
89 nr = q->nr_requests - (q->nr_requests / 8) + 1;
90 if (nr > q->nr_requests)
91 nr = q->nr_requests;
92 q->nr_congestion_on = nr;
93
94 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
95 if (nr < 1)
96 nr = 1;
97 q->nr_congestion_off = nr;
98}
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/**
101 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
102 * @bdev: device
103 *
104 * Locates the passed device's request queue and returns the address of its
105 * backing_dev_info
106 *
107 * Will return NULL if the request queue cannot be located.
108 */
109struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
110{
111 struct backing_dev_info *ret = NULL;
Jens Axboe165125e2007-07-24 09:28:11 +0200112 struct request_queue *q = bdev_get_queue(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 if (q)
115 ret = &q->backing_dev_info;
116 return ret;
117}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118EXPORT_SYMBOL(blk_get_backing_dev_info);
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120/**
121 * blk_queue_prep_rq - set a prepare_request function for queue
122 * @q: queue
123 * @pfn: prepare_request function
124 *
125 * It's possible for a queue to register a prepare_request callback which
126 * is invoked before the request is handed to the request_fn. The goal of
127 * the function is to prepare a request for I/O, it can be used to build a
128 * cdb from the request data for instance.
129 *
130 */
Jens Axboe165125e2007-07-24 09:28:11 +0200131void blk_queue_prep_rq(struct request_queue *q, prep_rq_fn *pfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 q->prep_rq_fn = pfn;
134}
135
136EXPORT_SYMBOL(blk_queue_prep_rq);
137
138/**
139 * blk_queue_merge_bvec - set a merge_bvec function for queue
140 * @q: queue
141 * @mbfn: merge_bvec_fn
142 *
143 * Usually queues have static limitations on the max sectors or segments that
144 * we can put in a request. Stacking drivers may have some settings that
145 * are dynamic, and thus we have to query the queue whether it is ok to
146 * add a new bio_vec to a bio at a given offset or not. If the block device
147 * has such limitations, it needs to register a merge_bvec_fn to control
148 * the size of bio's sent to it. Note that a block device *must* allow a
149 * single page to be added to an empty bio. The block device driver may want
150 * to use the bio_split() function to deal with these bio's. By default
151 * no merge_bvec_fn is defined for a queue, and only the fixed limits are
152 * honored.
153 */
Jens Axboe165125e2007-07-24 09:28:11 +0200154void blk_queue_merge_bvec(struct request_queue *q, merge_bvec_fn *mbfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 q->merge_bvec_fn = mbfn;
157}
158
159EXPORT_SYMBOL(blk_queue_merge_bvec);
160
Jens Axboe165125e2007-07-24 09:28:11 +0200161void blk_queue_softirq_done(struct request_queue *q, softirq_done_fn *fn)
Jens Axboeff856ba2006-01-09 16:02:34 +0100162{
163 q->softirq_done_fn = fn;
164}
165
166EXPORT_SYMBOL(blk_queue_softirq_done);
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/**
169 * blk_queue_make_request - define an alternate make_request function for a device
170 * @q: the request queue for the device to be affected
171 * @mfn: the alternate make_request function
172 *
173 * Description:
174 * The normal way for &struct bios to be passed to a device
175 * driver is for them to be collected into requests on a request
176 * queue, and then to allow the device driver to select requests
177 * off that queue when it is ready. This works well for many block
178 * devices. However some block devices (typically virtual devices
179 * such as md or lvm) do not benefit from the processing on the
180 * request queue, and are served best by having the requests passed
181 * directly to them. This can be achieved by providing a function
182 * to blk_queue_make_request().
183 *
184 * Caveat:
185 * The driver that does this *must* be able to deal appropriately
186 * with buffers in "highmemory". This can be accomplished by either calling
187 * __bio_kmap_atomic() to get a temporary kernel mapping, or by calling
188 * blk_queue_bounce() to create a buffer in normal memory.
189 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200190void blk_queue_make_request(struct request_queue * q, make_request_fn * mfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 /*
193 * set defaults
194 */
195 q->nr_requests = BLKDEV_MAX_RQ;
Stuart McLaren309c0a12005-09-06 15:17:47 -0700196 blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS);
197 blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 q->make_request_fn = mfn;
199 q->backing_dev_info.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
200 q->backing_dev_info.state = 0;
201 q->backing_dev_info.capabilities = BDI_CAP_MAP_COPY;
Mike Christiedefd94b2005-12-05 02:37:06 -0600202 blk_queue_max_sectors(q, SAFE_MAX_SECTORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 blk_queue_hardsect_size(q, 512);
204 blk_queue_dma_alignment(q, 511);
205 blk_queue_congestion_threshold(q);
206 q->nr_batching = BLK_BATCH_REQ;
207
208 q->unplug_thresh = 4; /* hmm */
209 q->unplug_delay = (3 * HZ) / 1000; /* 3 milliseconds */
210 if (q->unplug_delay == 0)
211 q->unplug_delay = 1;
212
David Howells65f27f32006-11-22 14:55:48 +0000213 INIT_WORK(&q->unplug_work, blk_unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 q->unplug_timer.function = blk_unplug_timeout;
216 q->unplug_timer.data = (unsigned long)q;
217
218 /*
219 * by default assume old behaviour and bounce for any highmem page
220 */
221 blk_queue_bounce_limit(q, BLK_BOUNCE_HIGH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224EXPORT_SYMBOL(blk_queue_make_request);
225
Jens Axboe165125e2007-07-24 09:28:11 +0200226static void rq_init(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 INIT_LIST_HEAD(&rq->queuelist);
Jens Axboeff856ba2006-01-09 16:02:34 +0100229 INIT_LIST_HEAD(&rq->donelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 rq->errors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 rq->bio = rq->biotail = NULL;
Jens Axboe2e662b62006-07-13 11:55:04 +0200233 INIT_HLIST_NODE(&rq->hash);
234 RB_CLEAR_NODE(&rq->rb_node);
Jens Axboe22e2c502005-06-27 10:55:12 +0200235 rq->ioprio = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 rq->buffer = NULL;
237 rq->ref_count = 1;
238 rq->q = q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 rq->special = NULL;
240 rq->data_len = 0;
241 rq->data = NULL;
Mike Christie df46b9a2005-06-20 14:04:44 +0200242 rq->nr_phys_segments = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 rq->sense = NULL;
244 rq->end_io = NULL;
245 rq->end_io_data = NULL;
Jens Axboeff856ba2006-01-09 16:02:34 +0100246 rq->completion_data = NULL;
FUJITA Tomonoriabae1fd2007-07-16 08:52:14 +0200247 rq->next_rq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
250/**
251 * blk_queue_ordered - does this queue support ordered writes
Tejun Heo797e7db2006-01-06 09:51:03 +0100252 * @q: the request queue
253 * @ordered: one of QUEUE_ORDERED_*
Jens Axboefddfdea2006-01-31 15:24:34 +0100254 * @prepare_flush_fn: rq setup helper for cache flush ordered writes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 *
256 * Description:
257 * For journalled file systems, doing ordered writes on a commit
258 * block instead of explicitly doing wait_on_buffer (which is bad
259 * for performance) can be a big win. Block drivers supporting this
260 * feature should call this function and indicate so.
261 *
262 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200263int blk_queue_ordered(struct request_queue *q, unsigned ordered,
Tejun Heo797e7db2006-01-06 09:51:03 +0100264 prepare_flush_fn *prepare_flush_fn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Tejun Heo797e7db2006-01-06 09:51:03 +0100266 if (ordered & (QUEUE_ORDERED_PREFLUSH | QUEUE_ORDERED_POSTFLUSH) &&
267 prepare_flush_fn == NULL) {
268 printk(KERN_ERR "blk_queue_ordered: prepare_flush_fn required\n");
269 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100271
272 if (ordered != QUEUE_ORDERED_NONE &&
273 ordered != QUEUE_ORDERED_DRAIN &&
274 ordered != QUEUE_ORDERED_DRAIN_FLUSH &&
275 ordered != QUEUE_ORDERED_DRAIN_FUA &&
276 ordered != QUEUE_ORDERED_TAG &&
277 ordered != QUEUE_ORDERED_TAG_FLUSH &&
278 ordered != QUEUE_ORDERED_TAG_FUA) {
279 printk(KERN_ERR "blk_queue_ordered: bad value %d\n", ordered);
280 return -EINVAL;
281 }
282
Tetsuo Takata60481b12006-01-24 10:34:36 +0100283 q->ordered = ordered;
Tejun Heo797e7db2006-01-06 09:51:03 +0100284 q->next_ordered = ordered;
285 q->prepare_flush_fn = prepare_flush_fn;
286
287 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
290EXPORT_SYMBOL(blk_queue_ordered);
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292/*
293 * Cache flushing for ordered writes handling
294 */
Jens Axboe165125e2007-07-24 09:28:11 +0200295inline unsigned blk_ordered_cur_seq(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Tejun Heo797e7db2006-01-06 09:51:03 +0100297 if (!q->ordseq)
298 return 0;
299 return 1 << ffz(q->ordseq);
300}
301
302unsigned blk_ordered_req_seq(struct request *rq)
303{
Jens Axboe165125e2007-07-24 09:28:11 +0200304 struct request_queue *q = rq->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Tejun Heo797e7db2006-01-06 09:51:03 +0100306 BUG_ON(q->ordseq == 0);
Tejun Heo8922e162005-10-20 16:23:44 +0200307
Tejun Heo797e7db2006-01-06 09:51:03 +0100308 if (rq == &q->pre_flush_rq)
309 return QUEUE_ORDSEQ_PREFLUSH;
310 if (rq == &q->bar_rq)
311 return QUEUE_ORDSEQ_BAR;
312 if (rq == &q->post_flush_rq)
313 return QUEUE_ORDSEQ_POSTFLUSH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Tejun Heobc90ba02007-06-15 13:24:28 +0200315 /*
316 * !fs requests don't need to follow barrier ordering. Always
317 * put them at the front. This fixes the following deadlock.
318 *
319 * http://thread.gmane.org/gmane.linux.kernel/537473
320 */
321 if (!blk_fs_request(rq))
322 return QUEUE_ORDSEQ_DRAIN;
323
Jens Axboe4aff5e22006-08-10 08:44:47 +0200324 if ((rq->cmd_flags & REQ_ORDERED_COLOR) ==
325 (q->orig_bar_rq->cmd_flags & REQ_ORDERED_COLOR))
Tejun Heo797e7db2006-01-06 09:51:03 +0100326 return QUEUE_ORDSEQ_DRAIN;
327 else
328 return QUEUE_ORDSEQ_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Jens Axboe165125e2007-07-24 09:28:11 +0200331void blk_ordered_complete_seq(struct request_queue *q, unsigned seq, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Tejun Heo797e7db2006-01-06 09:51:03 +0100333 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Tejun Heo797e7db2006-01-06 09:51:03 +0100335 if (error && !q->orderr)
336 q->orderr = error;
Tejun Heo8922e162005-10-20 16:23:44 +0200337
Tejun Heo797e7db2006-01-06 09:51:03 +0100338 BUG_ON(q->ordseq & seq);
339 q->ordseq |= seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Tejun Heo797e7db2006-01-06 09:51:03 +0100341 if (blk_ordered_cur_seq(q) != QUEUE_ORDSEQ_DONE)
342 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 /*
Tejun Heo797e7db2006-01-06 09:51:03 +0100345 * Okay, sequence complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 */
Tejun Heo797e7db2006-01-06 09:51:03 +0100347 q->ordseq = 0;
Jens Axboe4fa253f2007-07-18 13:13:10 +0200348 rq = q->orig_bar_rq;
Tejun Heo797e7db2006-01-06 09:51:03 +0100349
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -0500350 if (__blk_end_request(rq, q->orderr, blk_rq_bytes(rq)))
351 BUG();
Tejun Heo797e7db2006-01-06 09:51:03 +0100352}
353
354static void pre_flush_end_io(struct request *rq, int error)
355{
356 elv_completed_request(rq->q, rq);
357 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_PREFLUSH, error);
358}
359
360static void bar_end_io(struct request *rq, int error)
361{
362 elv_completed_request(rq->q, rq);
363 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_BAR, error);
364}
365
366static void post_flush_end_io(struct request *rq, int error)
367{
368 elv_completed_request(rq->q, rq);
369 blk_ordered_complete_seq(rq->q, QUEUE_ORDSEQ_POSTFLUSH, error);
370}
371
Jens Axboe165125e2007-07-24 09:28:11 +0200372static void queue_flush(struct request_queue *q, unsigned which)
Tejun Heo797e7db2006-01-06 09:51:03 +0100373{
374 struct request *rq;
375 rq_end_io_fn *end_io;
376
377 if (which == QUEUE_ORDERED_PREFLUSH) {
378 rq = &q->pre_flush_rq;
379 end_io = pre_flush_end_io;
380 } else {
381 rq = &q->post_flush_rq;
382 end_io = post_flush_end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
384
Jens Axboe4aff5e22006-08-10 08:44:47 +0200385 rq->cmd_flags = REQ_HARDBARRIER;
Tejun Heo797e7db2006-01-06 09:51:03 +0100386 rq_init(q, rq);
Tejun Heo797e7db2006-01-06 09:51:03 +0100387 rq->elevator_private = NULL;
Jens Axboec00895a2006-09-30 20:29:12 +0200388 rq->elevator_private2 = NULL;
Tejun Heo797e7db2006-01-06 09:51:03 +0100389 rq->rq_disk = q->bar_rq.rq_disk;
Tejun Heo797e7db2006-01-06 09:51:03 +0100390 rq->end_io = end_io;
391 q->prepare_flush_fn(q, rq);
392
Tejun Heo30e96562006-02-08 01:01:31 -0800393 elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
Tejun Heo797e7db2006-01-06 09:51:03 +0100394}
395
Jens Axboe165125e2007-07-24 09:28:11 +0200396static inline struct request *start_ordered(struct request_queue *q,
Tejun Heo797e7db2006-01-06 09:51:03 +0100397 struct request *rq)
398{
Tejun Heo797e7db2006-01-06 09:51:03 +0100399 q->orderr = 0;
400 q->ordered = q->next_ordered;
401 q->ordseq |= QUEUE_ORDSEQ_STARTED;
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 /*
Tejun Heo797e7db2006-01-06 09:51:03 +0100404 * Prep proxy barrier request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 */
Tejun Heo797e7db2006-01-06 09:51:03 +0100406 blkdev_dequeue_request(rq);
407 q->orig_bar_rq = rq;
408 rq = &q->bar_rq;
Jens Axboe4aff5e22006-08-10 08:44:47 +0200409 rq->cmd_flags = 0;
Tejun Heo797e7db2006-01-06 09:51:03 +0100410 rq_init(q, rq);
Jens Axboe4aff5e22006-08-10 08:44:47 +0200411 if (bio_data_dir(q->orig_bar_rq->bio) == WRITE)
412 rq->cmd_flags |= REQ_RW;
Jens Axboe4fa253f2007-07-18 13:13:10 +0200413 if (q->ordered & QUEUE_ORDERED_FUA)
414 rq->cmd_flags |= REQ_FUA;
Tejun Heo797e7db2006-01-06 09:51:03 +0100415 rq->elevator_private = NULL;
Jens Axboec00895a2006-09-30 20:29:12 +0200416 rq->elevator_private2 = NULL;
Tejun Heo797e7db2006-01-06 09:51:03 +0100417 init_request_from_bio(rq, q->orig_bar_rq->bio);
418 rq->end_io = bar_end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Tejun Heo797e7db2006-01-06 09:51:03 +0100420 /*
421 * Queue ordered sequence. As we stack them at the head, we
422 * need to queue in reverse order. Note that we rely on that
423 * no fs request uses ELEVATOR_INSERT_FRONT and thus no fs
Jens Axboebf2de6f2007-09-27 13:01:25 +0200424 * request gets inbetween ordered sequence. If this request is
425 * an empty barrier, we don't need to do a postflush ever since
426 * there will be no data written between the pre and post flush.
427 * Hence a single flush will suffice.
Tejun Heo797e7db2006-01-06 09:51:03 +0100428 */
Jens Axboebf2de6f2007-09-27 13:01:25 +0200429 if ((q->ordered & QUEUE_ORDERED_POSTFLUSH) && !blk_empty_barrier(rq))
Tejun Heo797e7db2006-01-06 09:51:03 +0100430 queue_flush(q, QUEUE_ORDERED_POSTFLUSH);
431 else
432 q->ordseq |= QUEUE_ORDSEQ_POSTFLUSH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Tejun Heo30e96562006-02-08 01:01:31 -0800434 elv_insert(q, rq, ELEVATOR_INSERT_FRONT);
Tejun Heo797e7db2006-01-06 09:51:03 +0100435
436 if (q->ordered & QUEUE_ORDERED_PREFLUSH) {
437 queue_flush(q, QUEUE_ORDERED_PREFLUSH);
438 rq = &q->pre_flush_rq;
439 } else
440 q->ordseq |= QUEUE_ORDSEQ_PREFLUSH;
441
442 if ((q->ordered & QUEUE_ORDERED_TAG) || q->in_flight == 0)
443 q->ordseq |= QUEUE_ORDSEQ_DRAIN;
444 else
445 rq = NULL;
446
447 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Jens Axboe165125e2007-07-24 09:28:11 +0200450int blk_do_ordered(struct request_queue *q, struct request **rqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Jens Axboe9a7a67a2006-02-04 23:27:38 -0800452 struct request *rq = *rqp;
Jens Axboebf2de6f2007-09-27 13:01:25 +0200453 const int is_barrier = blk_fs_request(rq) && blk_barrier_rq(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Tejun Heo797e7db2006-01-06 09:51:03 +0100455 if (!q->ordseq) {
456 if (!is_barrier)
457 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Tejun Heo797e7db2006-01-06 09:51:03 +0100459 if (q->next_ordered != QUEUE_ORDERED_NONE) {
460 *rqp = start_ordered(q, rq);
461 return 1;
462 } else {
463 /*
464 * This can happen when the queue switches to
465 * ORDERED_NONE while this request is on it.
466 */
467 blkdev_dequeue_request(rq);
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -0500468 if (__blk_end_request(rq, -EOPNOTSUPP,
469 blk_rq_bytes(rq)))
470 BUG();
Tejun Heo797e7db2006-01-06 09:51:03 +0100471 *rqp = NULL;
472 return 0;
473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Jens Axboe9a7a67a2006-02-04 23:27:38 -0800476 /*
477 * Ordered sequence in progress
478 */
479
480 /* Special requests are not subject to ordering rules. */
481 if (!blk_fs_request(rq) &&
482 rq != &q->pre_flush_rq && rq != &q->post_flush_rq)
483 return 1;
484
Tejun Heo797e7db2006-01-06 09:51:03 +0100485 if (q->ordered & QUEUE_ORDERED_TAG) {
Jens Axboe9a7a67a2006-02-04 23:27:38 -0800486 /* Ordered by tag. Blocking the next barrier is enough. */
Tejun Heo797e7db2006-01-06 09:51:03 +0100487 if (is_barrier && rq != &q->bar_rq)
488 *rqp = NULL;
Jens Axboe9a7a67a2006-02-04 23:27:38 -0800489 } else {
490 /* Ordered by draining. Wait for turn. */
491 WARN_ON(blk_ordered_req_seq(rq) < blk_ordered_cur_seq(q));
492 if (blk_ordered_req_seq(rq) > blk_ordered_cur_seq(q))
493 *rqp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
495
496 return 1;
497}
498
NeilBrown5bb23a62007-09-27 12:46:13 +0200499static void req_bio_endio(struct request *rq, struct bio *bio,
500 unsigned int nbytes, int error)
Tejun Heo797e7db2006-01-06 09:51:03 +0100501{
Jens Axboe165125e2007-07-24 09:28:11 +0200502 struct request_queue *q = rq->q;
Tejun Heo797e7db2006-01-06 09:51:03 +0100503
NeilBrown5bb23a62007-09-27 12:46:13 +0200504 if (&q->bar_rq != rq) {
505 if (error)
506 clear_bit(BIO_UPTODATE, &bio->bi_flags);
507 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
508 error = -EIO;
Tejun Heo797e7db2006-01-06 09:51:03 +0100509
NeilBrown5bb23a62007-09-27 12:46:13 +0200510 if (unlikely(nbytes > bio->bi_size)) {
511 printk("%s: want %u bytes done, only %u left\n",
512 __FUNCTION__, nbytes, bio->bi_size);
513 nbytes = bio->bi_size;
514 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100515
NeilBrown5bb23a62007-09-27 12:46:13 +0200516 bio->bi_size -= nbytes;
517 bio->bi_sector += (nbytes >> 9);
518 if (bio->bi_size == 0)
NeilBrown6712ecf2007-09-27 12:47:43 +0200519 bio_endio(bio, error);
NeilBrown5bb23a62007-09-27 12:46:13 +0200520 } else {
521
522 /*
523 * Okay, this is the barrier request in progress, just
524 * record the error;
525 */
526 if (error && !q->orderr)
527 q->orderr = error;
528 }
Tejun Heo797e7db2006-01-06 09:51:03 +0100529}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531/**
532 * blk_queue_bounce_limit - set bounce buffer limit for queue
533 * @q: the request queue for the device
534 * @dma_addr: bus address limit
535 *
536 * Description:
537 * Different hardware can have different requirements as to what pages
538 * it can do I/O directly to. A low level driver can call
539 * blk_queue_bounce_limit to have lower memory pages allocated as bounce
Andi Kleen5ee1af92006-03-08 17:57:26 -0800540 * buffers for doing I/O to pages residing above @page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200542void blk_queue_bounce_limit(struct request_queue *q, u64 dma_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 unsigned long bounce_pfn = dma_addr >> PAGE_SHIFT;
Andi Kleen5ee1af92006-03-08 17:57:26 -0800545 int dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Andi Kleen5ee1af92006-03-08 17:57:26 -0800547 q->bounce_gfp = GFP_NOIO;
548#if BITS_PER_LONG == 64
549 /* Assume anything <= 4GB can be handled by IOMMU.
550 Actually some IOMMUs can handle everything, but I don't
551 know of a way to test this here. */
Andi Kleen82697302006-06-21 14:48:09 +0200552 if (bounce_pfn < (min_t(u64,0xffffffff,BLK_BOUNCE_HIGH) >> PAGE_SHIFT))
Andi Kleen5ee1af92006-03-08 17:57:26 -0800553 dma = 1;
554 q->bounce_pfn = max_low_pfn;
555#else
556 if (bounce_pfn < blk_max_low_pfn)
557 dma = 1;
558 q->bounce_pfn = bounce_pfn;
559#endif
560 if (dma) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 init_emergency_isa_pool();
562 q->bounce_gfp = GFP_NOIO | GFP_DMA;
Andi Kleen5ee1af92006-03-08 17:57:26 -0800563 q->bounce_pfn = bounce_pfn;
564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
567EXPORT_SYMBOL(blk_queue_bounce_limit);
568
569/**
570 * blk_queue_max_sectors - set max sectors for a request for this queue
571 * @q: the request queue for the device
572 * @max_sectors: max sectors in the usual 512b unit
573 *
574 * Description:
575 * Enables a low level driver to set an upper limit on the size of
576 * received requests.
577 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200578void blk_queue_max_sectors(struct request_queue *q, unsigned int max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 if ((max_sectors << 9) < PAGE_CACHE_SIZE) {
581 max_sectors = 1 << (PAGE_CACHE_SHIFT - 9);
582 printk("%s: set to minimum %d\n", __FUNCTION__, max_sectors);
583 }
584
Mike Christiedefd94b2005-12-05 02:37:06 -0600585 if (BLK_DEF_MAX_SECTORS > max_sectors)
586 q->max_hw_sectors = q->max_sectors = max_sectors;
587 else {
588 q->max_sectors = BLK_DEF_MAX_SECTORS;
589 q->max_hw_sectors = max_sectors;
590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
593EXPORT_SYMBOL(blk_queue_max_sectors);
594
595/**
596 * blk_queue_max_phys_segments - set max phys segments for a request for this queue
597 * @q: the request queue for the device
598 * @max_segments: max number of segments
599 *
600 * Description:
601 * Enables a low level driver to set an upper limit on the number of
602 * physical data segments in a request. This would be the largest sized
603 * scatter list the driver could handle.
604 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200605void blk_queue_max_phys_segments(struct request_queue *q,
606 unsigned short max_segments)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
608 if (!max_segments) {
609 max_segments = 1;
610 printk("%s: set to minimum %d\n", __FUNCTION__, max_segments);
611 }
612
613 q->max_phys_segments = max_segments;
614}
615
616EXPORT_SYMBOL(blk_queue_max_phys_segments);
617
618/**
619 * blk_queue_max_hw_segments - set max hw segments for a request for this queue
620 * @q: the request queue for the device
621 * @max_segments: max number of segments
622 *
623 * Description:
624 * Enables a low level driver to set an upper limit on the number of
625 * hw data segments in a request. This would be the largest number of
626 * address/length pairs the host adapter can actually give as once
627 * to the device.
628 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200629void blk_queue_max_hw_segments(struct request_queue *q,
630 unsigned short max_segments)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
632 if (!max_segments) {
633 max_segments = 1;
634 printk("%s: set to minimum %d\n", __FUNCTION__, max_segments);
635 }
636
637 q->max_hw_segments = max_segments;
638}
639
640EXPORT_SYMBOL(blk_queue_max_hw_segments);
641
642/**
643 * blk_queue_max_segment_size - set max segment size for blk_rq_map_sg
644 * @q: the request queue for the device
645 * @max_size: max size of segment in bytes
646 *
647 * Description:
648 * Enables a low level driver to set an upper limit on the size of a
649 * coalesced segment
650 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200651void blk_queue_max_segment_size(struct request_queue *q, unsigned int max_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
653 if (max_size < PAGE_CACHE_SIZE) {
654 max_size = PAGE_CACHE_SIZE;
655 printk("%s: set to minimum %d\n", __FUNCTION__, max_size);
656 }
657
658 q->max_segment_size = max_size;
659}
660
661EXPORT_SYMBOL(blk_queue_max_segment_size);
662
663/**
664 * blk_queue_hardsect_size - set hardware sector size for the queue
665 * @q: the request queue for the device
666 * @size: the hardware sector size, in bytes
667 *
668 * Description:
669 * This should typically be set to the lowest possible sector size
670 * that the hardware can operate on (possible without reverting to
671 * even internal read-modify-write operations). Usually the default
672 * of 512 covers most hardware.
673 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200674void blk_queue_hardsect_size(struct request_queue *q, unsigned short size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
676 q->hardsect_size = size;
677}
678
679EXPORT_SYMBOL(blk_queue_hardsect_size);
680
681/*
682 * Returns the minimum that is _not_ zero, unless both are zero.
683 */
684#define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
685
686/**
687 * blk_queue_stack_limits - inherit underlying queue limits for stacked drivers
688 * @t: the stacking driver (top)
689 * @b: the underlying device (bottom)
690 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200691void blk_queue_stack_limits(struct request_queue *t, struct request_queue *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
693 /* zero is "infinity" */
Mike Christiedefd94b2005-12-05 02:37:06 -0600694 t->max_sectors = min_not_zero(t->max_sectors,b->max_sectors);
695 t->max_hw_sectors = min_not_zero(t->max_hw_sectors,b->max_hw_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 t->max_phys_segments = min(t->max_phys_segments,b->max_phys_segments);
698 t->max_hw_segments = min(t->max_hw_segments,b->max_hw_segments);
699 t->max_segment_size = min(t->max_segment_size,b->max_segment_size);
700 t->hardsect_size = max(t->hardsect_size,b->hardsect_size);
NeilBrown89e5c8b2006-03-27 01:18:02 -0800701 if (!test_bit(QUEUE_FLAG_CLUSTER, &b->queue_flags))
702 clear_bit(QUEUE_FLAG_CLUSTER, &t->queue_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
705EXPORT_SYMBOL(blk_queue_stack_limits);
706
707/**
James Bottomleyfa0ccd82008-01-10 11:30:36 -0600708 * blk_queue_dma_drain - Set up a drain buffer for excess dma.
709 *
710 * @q: the request queue for the device
711 * @buf: physically contiguous buffer
712 * @size: size of the buffer in bytes
713 *
714 * Some devices have excess DMA problems and can't simply discard (or
715 * zero fill) the unwanted piece of the transfer. They have to have a
716 * real area of memory to transfer it into. The use case for this is
717 * ATAPI devices in DMA mode. If the packet command causes a transfer
718 * bigger than the transfer size some HBAs will lock up if there
719 * aren't DMA elements to contain the excess transfer. What this API
720 * does is adjust the queue so that the buf is always appended
721 * silently to the scatterlist.
722 *
723 * Note: This routine adjusts max_hw_segments to make room for
724 * appending the drain buffer. If you call
725 * blk_queue_max_hw_segments() or blk_queue_max_phys_segments() after
726 * calling this routine, you must set the limit to one fewer than your
727 * device can support otherwise there won't be room for the drain
728 * buffer.
729 */
730int blk_queue_dma_drain(struct request_queue *q, void *buf,
731 unsigned int size)
732{
733 if (q->max_hw_segments < 2 || q->max_phys_segments < 2)
734 return -EINVAL;
735 /* make room for appending the drain */
736 --q->max_hw_segments;
737 --q->max_phys_segments;
738 q->dma_drain_buffer = buf;
739 q->dma_drain_size = size;
740
741 return 0;
742}
743
744EXPORT_SYMBOL_GPL(blk_queue_dma_drain);
745
746/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 * blk_queue_segment_boundary - set boundary rules for segment merging
748 * @q: the request queue for the device
749 * @mask: the memory boundary mask
750 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200751void blk_queue_segment_boundary(struct request_queue *q, unsigned long mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 if (mask < PAGE_CACHE_SIZE - 1) {
754 mask = PAGE_CACHE_SIZE - 1;
755 printk("%s: set to minimum %lx\n", __FUNCTION__, mask);
756 }
757
758 q->seg_boundary_mask = mask;
759}
760
761EXPORT_SYMBOL(blk_queue_segment_boundary);
762
763/**
764 * blk_queue_dma_alignment - set dma length and memory alignment
765 * @q: the request queue for the device
766 * @mask: alignment mask
767 *
768 * description:
769 * set required memory and length aligment for direct dma transactions.
770 * this is used when buiding direct io requests for the queue.
771 *
772 **/
Jens Axboe165125e2007-07-24 09:28:11 +0200773void blk_queue_dma_alignment(struct request_queue *q, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
775 q->dma_alignment = mask;
776}
777
778EXPORT_SYMBOL(blk_queue_dma_alignment);
779
780/**
James Bottomley11c3e682007-12-31 16:37:00 -0600781 * blk_queue_update_dma_alignment - update dma length and memory alignment
782 * @q: the request queue for the device
783 * @mask: alignment mask
784 *
785 * description:
786 * update required memory and length aligment for direct dma transactions.
787 * If the requested alignment is larger than the current alignment, then
788 * the current queue alignment is updated to the new value, otherwise it
789 * is left alone. The design of this is to allow multiple objects
790 * (driver, device, transport etc) to set their respective
791 * alignments without having them interfere.
792 *
793 **/
794void blk_queue_update_dma_alignment(struct request_queue *q, int mask)
795{
796 BUG_ON(mask > PAGE_SIZE);
797
798 if (mask > q->dma_alignment)
799 q->dma_alignment = mask;
800}
801
802EXPORT_SYMBOL(blk_queue_update_dma_alignment);
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804void blk_dump_rq_flags(struct request *rq, char *msg)
805{
806 int bit;
807
Jens Axboe4aff5e22006-08-10 08:44:47 +0200808 printk("%s: dev %s: type=%x, flags=%x\n", msg,
809 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
810 rq->cmd_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 printk("\nsector %llu, nr/cnr %lu/%u\n", (unsigned long long)rq->sector,
813 rq->nr_sectors,
814 rq->current_nr_sectors);
815 printk("bio %p, biotail %p, buffer %p, data %p, len %u\n", rq->bio, rq->biotail, rq->buffer, rq->data, rq->data_len);
816
Jens Axboe4aff5e22006-08-10 08:44:47 +0200817 if (blk_pc_request(rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 printk("cdb: ");
819 for (bit = 0; bit < sizeof(rq->cmd); bit++)
820 printk("%02x ", rq->cmd[bit]);
821 printk("\n");
822 }
823}
824
825EXPORT_SYMBOL(blk_dump_rq_flags);
826
Jens Axboe165125e2007-07-24 09:28:11 +0200827void blk_recount_segments(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
NeilBrown9dfa5282007-08-16 13:27:52 +0200829 struct request rq;
830 struct bio *nxt = bio->bi_next;
831 rq.q = q;
832 rq.bio = rq.biotail = bio;
833 bio->bi_next = NULL;
834 blk_recalc_rq_segments(&rq);
835 bio->bi_next = nxt;
836 bio->bi_phys_segments = rq.nr_phys_segments;
837 bio->bi_hw_segments = rq.nr_hw_segments;
838 bio->bi_flags |= (1 << BIO_SEG_VALID);
839}
840EXPORT_SYMBOL(blk_recount_segments);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
NeilBrown9dfa5282007-08-16 13:27:52 +0200842static void blk_recalc_rq_segments(struct request *rq)
843{
844 int nr_phys_segs;
845 int nr_hw_segs;
846 unsigned int phys_size;
847 unsigned int hw_size;
848 struct bio_vec *bv, *bvprv = NULL;
849 int seg_size;
850 int hw_seg_size;
851 int cluster;
NeilBrown5705f702007-09-25 12:35:59 +0200852 struct req_iterator iter;
NeilBrown9dfa5282007-08-16 13:27:52 +0200853 int high, highprv = 1;
854 struct request_queue *q = rq->q;
855
856 if (!rq->bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 return;
858
859 cluster = q->queue_flags & (1 << QUEUE_FLAG_CLUSTER);
NeilBrown9dfa5282007-08-16 13:27:52 +0200860 hw_seg_size = seg_size = 0;
861 phys_size = hw_size = nr_phys_segs = nr_hw_segs = 0;
NeilBrown5705f702007-09-25 12:35:59 +0200862 rq_for_each_segment(bv, rq, iter) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 /*
864 * the trick here is making sure that a high page is never
865 * considered part of another segment, since that might
866 * change with the bounce page.
867 */
Vasily Tarasovf772b3d2007-03-27 08:52:47 +0200868 high = page_to_pfn(bv->bv_page) > q->bounce_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (high || highprv)
870 goto new_hw_segment;
871 if (cluster) {
872 if (seg_size + bv->bv_len > q->max_segment_size)
873 goto new_segment;
874 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bv))
875 goto new_segment;
876 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bv))
877 goto new_segment;
878 if (BIOVEC_VIRT_OVERSIZE(hw_seg_size + bv->bv_len))
879 goto new_hw_segment;
880
881 seg_size += bv->bv_len;
882 hw_seg_size += bv->bv_len;
883 bvprv = bv;
884 continue;
885 }
886new_segment:
887 if (BIOVEC_VIRT_MERGEABLE(bvprv, bv) &&
NeilBrown9dfa5282007-08-16 13:27:52 +0200888 !BIOVEC_VIRT_OVERSIZE(hw_seg_size + bv->bv_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 hw_seg_size += bv->bv_len;
NeilBrown9dfa5282007-08-16 13:27:52 +0200890 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891new_hw_segment:
NeilBrown9dfa5282007-08-16 13:27:52 +0200892 if (nr_hw_segs == 1 &&
893 hw_seg_size > rq->bio->bi_hw_front_size)
894 rq->bio->bi_hw_front_size = hw_seg_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 hw_seg_size = BIOVEC_VIRT_START_SIZE(bv) + bv->bv_len;
896 nr_hw_segs++;
897 }
898
899 nr_phys_segs++;
900 bvprv = bv;
901 seg_size = bv->bv_len;
902 highprv = high;
903 }
NeilBrown9dfa5282007-08-16 13:27:52 +0200904
905 if (nr_hw_segs == 1 &&
906 hw_seg_size > rq->bio->bi_hw_front_size)
907 rq->bio->bi_hw_front_size = hw_seg_size;
908 if (hw_seg_size > rq->biotail->bi_hw_back_size)
909 rq->biotail->bi_hw_back_size = hw_seg_size;
910 rq->nr_phys_segments = nr_phys_segs;
911 rq->nr_hw_segments = nr_hw_segs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Jens Axboe165125e2007-07-24 09:28:11 +0200914static int blk_phys_contig_segment(struct request_queue *q, struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 struct bio *nxt)
916{
917 if (!(q->queue_flags & (1 << QUEUE_FLAG_CLUSTER)))
918 return 0;
919
920 if (!BIOVEC_PHYS_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)))
921 return 0;
922 if (bio->bi_size + nxt->bi_size > q->max_segment_size)
923 return 0;
924
925 /*
926 * bio and nxt are contigous in memory, check if the queue allows
927 * these two to be merged into one
928 */
929 if (BIO_SEG_BOUNDARY(q, bio, nxt))
930 return 1;
931
932 return 0;
933}
934
Jens Axboe165125e2007-07-24 09:28:11 +0200935static int blk_hw_contig_segment(struct request_queue *q, struct bio *bio,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 struct bio *nxt)
937{
938 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
939 blk_recount_segments(q, bio);
940 if (unlikely(!bio_flagged(nxt, BIO_SEG_VALID)))
941 blk_recount_segments(q, nxt);
942 if (!BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(nxt)) ||
Jens Axboe32eef962007-06-19 09:09:27 +0200943 BIOVEC_VIRT_OVERSIZE(bio->bi_hw_back_size + nxt->bi_hw_front_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return 0;
Jens Axboe32eef962007-06-19 09:09:27 +0200945 if (bio->bi_hw_back_size + nxt->bi_hw_front_size > q->max_segment_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return 0;
947
948 return 1;
949}
950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951/*
952 * map a request to scatterlist, return number of sg entries setup. Caller
953 * must make sure sg can hold rq->nr_phys_segments entries
954 */
Jens Axboe165125e2007-07-24 09:28:11 +0200955int blk_rq_map_sg(struct request_queue *q, struct request *rq,
Jens Axboef5659132007-09-21 10:44:19 +0200956 struct scatterlist *sglist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
958 struct bio_vec *bvec, *bvprv;
NeilBrown5705f702007-09-25 12:35:59 +0200959 struct req_iterator iter;
Jens Axboeba951842007-10-17 19:34:11 +0200960 struct scatterlist *sg;
NeilBrown5705f702007-09-25 12:35:59 +0200961 int nsegs, cluster;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 nsegs = 0;
964 cluster = q->queue_flags & (1 << QUEUE_FLAG_CLUSTER);
965
966 /*
967 * for each bio in rq
968 */
969 bvprv = NULL;
Jens Axboeba951842007-10-17 19:34:11 +0200970 sg = NULL;
NeilBrown5705f702007-09-25 12:35:59 +0200971 rq_for_each_segment(bvec, rq, iter) {
Jens Axboe6c92e692007-08-16 13:43:12 +0200972 int nbytes = bvec->bv_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Jens Axboe6c92e692007-08-16 13:43:12 +0200974 if (bvprv && cluster) {
Jens Axboef5659132007-09-21 10:44:19 +0200975 if (sg->length + nbytes > q->max_segment_size)
Jens Axboe6c92e692007-08-16 13:43:12 +0200976 goto new_segment;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Jens Axboe6c92e692007-08-16 13:43:12 +0200978 if (!BIOVEC_PHYS_MERGEABLE(bvprv, bvec))
979 goto new_segment;
980 if (!BIOVEC_SEG_BOUNDARY(q, bvprv, bvec))
981 goto new_segment;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Jens Axboef5659132007-09-21 10:44:19 +0200983 sg->length += nbytes;
Jens Axboe6c92e692007-08-16 13:43:12 +0200984 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985new_segment:
Jens Axboeba951842007-10-17 19:34:11 +0200986 if (!sg)
987 sg = sglist;
Jens Axboe7aeacf92007-10-23 09:49:25 +0200988 else {
989 /*
990 * If the driver previously mapped a shorter
991 * list, we could see a termination bit
992 * prematurely unless it fully inits the sg
993 * table on each mapping. We KNOW that there
994 * must be more entries here or the driver
995 * would be buggy, so force clear the
996 * termination bit to avoid doing a full
997 * sg_init_table() in drivers for each command.
998 */
999 sg->page_link &= ~0x02;
Jens Axboeba951842007-10-17 19:34:11 +02001000 sg = sg_next(sg);
Jens Axboe7aeacf92007-10-23 09:49:25 +02001001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Jens Axboe642f1492007-10-24 11:20:47 +02001003 sg_set_page(sg, bvec->bv_page, nbytes, bvec->bv_offset);
Jens Axboe6c92e692007-08-16 13:43:12 +02001004 nsegs++;
1005 }
1006 bvprv = bvec;
NeilBrown5705f702007-09-25 12:35:59 +02001007 } /* segments in rq */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
James Bottomleyfa0ccd82008-01-10 11:30:36 -06001009 if (q->dma_drain_size) {
1010 sg->page_link &= ~0x02;
1011 sg = sg_next(sg);
1012 sg_set_page(sg, virt_to_page(q->dma_drain_buffer),
1013 q->dma_drain_size,
1014 ((unsigned long)q->dma_drain_buffer) &
1015 (PAGE_SIZE - 1));
1016 nsegs++;
1017 }
1018
Jens Axboe9b617642007-10-22 19:39:33 +02001019 if (sg)
Jens Axboec46f2332007-10-31 12:06:37 +01001020 sg_mark_end(sg);
Jens Axboe9b617642007-10-22 19:39:33 +02001021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return nsegs;
1023}
1024
1025EXPORT_SYMBOL(blk_rq_map_sg);
1026
1027/*
1028 * the standard queue merge functions, can be overridden with device
1029 * specific ones if so desired
1030 */
1031
Jens Axboe165125e2007-07-24 09:28:11 +02001032static inline int ll_new_mergeable(struct request_queue *q,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 struct request *req,
1034 struct bio *bio)
1035{
1036 int nr_phys_segs = bio_phys_segments(q, bio);
1037
1038 if (req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
Jens Axboe4aff5e22006-08-10 08:44:47 +02001039 req->cmd_flags |= REQ_NOMERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (req == q->last_merge)
1041 q->last_merge = NULL;
1042 return 0;
1043 }
1044
1045 /*
1046 * A hw segment is just getting larger, bump just the phys
1047 * counter.
1048 */
1049 req->nr_phys_segments += nr_phys_segs;
1050 return 1;
1051}
1052
Jens Axboe165125e2007-07-24 09:28:11 +02001053static inline int ll_new_hw_segment(struct request_queue *q,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 struct request *req,
1055 struct bio *bio)
1056{
1057 int nr_hw_segs = bio_hw_segments(q, bio);
1058 int nr_phys_segs = bio_phys_segments(q, bio);
1059
1060 if (req->nr_hw_segments + nr_hw_segs > q->max_hw_segments
1061 || req->nr_phys_segments + nr_phys_segs > q->max_phys_segments) {
Jens Axboe4aff5e22006-08-10 08:44:47 +02001062 req->cmd_flags |= REQ_NOMERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (req == q->last_merge)
1064 q->last_merge = NULL;
1065 return 0;
1066 }
1067
1068 /*
1069 * This will form the start of a new hw segment. Bump both
1070 * counters.
1071 */
1072 req->nr_hw_segments += nr_hw_segs;
1073 req->nr_phys_segments += nr_phys_segs;
1074 return 1;
1075}
1076
NeilBrown3001ca72007-08-16 13:31:27 +02001077static int ll_back_merge_fn(struct request_queue *q, struct request *req,
1078 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Mike Christiedefd94b2005-12-05 02:37:06 -06001080 unsigned short max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 int len;
1082
Mike Christiedefd94b2005-12-05 02:37:06 -06001083 if (unlikely(blk_pc_request(req)))
1084 max_sectors = q->max_hw_sectors;
1085 else
1086 max_sectors = q->max_sectors;
1087
1088 if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
Jens Axboe4aff5e22006-08-10 08:44:47 +02001089 req->cmd_flags |= REQ_NOMERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 if (req == q->last_merge)
1091 q->last_merge = NULL;
1092 return 0;
1093 }
1094 if (unlikely(!bio_flagged(req->biotail, BIO_SEG_VALID)))
1095 blk_recount_segments(q, req->biotail);
1096 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
1097 blk_recount_segments(q, bio);
1098 len = req->biotail->bi_hw_back_size + bio->bi_hw_front_size;
1099 if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(req->biotail), __BVEC_START(bio)) &&
1100 !BIOVEC_VIRT_OVERSIZE(len)) {
1101 int mergeable = ll_new_mergeable(q, req, bio);
1102
1103 if (mergeable) {
1104 if (req->nr_hw_segments == 1)
1105 req->bio->bi_hw_front_size = len;
1106 if (bio->bi_hw_segments == 1)
1107 bio->bi_hw_back_size = len;
1108 }
1109 return mergeable;
1110 }
1111
1112 return ll_new_hw_segment(q, req, bio);
1113}
1114
Jens Axboe165125e2007-07-24 09:28:11 +02001115static int ll_front_merge_fn(struct request_queue *q, struct request *req,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 struct bio *bio)
1117{
Mike Christiedefd94b2005-12-05 02:37:06 -06001118 unsigned short max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 int len;
1120
Mike Christiedefd94b2005-12-05 02:37:06 -06001121 if (unlikely(blk_pc_request(req)))
1122 max_sectors = q->max_hw_sectors;
1123 else
1124 max_sectors = q->max_sectors;
1125
1126
1127 if (req->nr_sectors + bio_sectors(bio) > max_sectors) {
Jens Axboe4aff5e22006-08-10 08:44:47 +02001128 req->cmd_flags |= REQ_NOMERGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (req == q->last_merge)
1130 q->last_merge = NULL;
1131 return 0;
1132 }
1133 len = bio->bi_hw_back_size + req->bio->bi_hw_front_size;
1134 if (unlikely(!bio_flagged(bio, BIO_SEG_VALID)))
1135 blk_recount_segments(q, bio);
1136 if (unlikely(!bio_flagged(req->bio, BIO_SEG_VALID)))
1137 blk_recount_segments(q, req->bio);
1138 if (BIOVEC_VIRT_MERGEABLE(__BVEC_END(bio), __BVEC_START(req->bio)) &&
1139 !BIOVEC_VIRT_OVERSIZE(len)) {
1140 int mergeable = ll_new_mergeable(q, req, bio);
1141
1142 if (mergeable) {
1143 if (bio->bi_hw_segments == 1)
1144 bio->bi_hw_front_size = len;
1145 if (req->nr_hw_segments == 1)
1146 req->biotail->bi_hw_back_size = len;
1147 }
1148 return mergeable;
1149 }
1150
1151 return ll_new_hw_segment(q, req, bio);
1152}
1153
Jens Axboe165125e2007-07-24 09:28:11 +02001154static int ll_merge_requests_fn(struct request_queue *q, struct request *req,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 struct request *next)
1156{
Nikita Danilovdfa1a552005-06-25 14:59:20 -07001157 int total_phys_segments;
1158 int total_hw_segments;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160 /*
1161 * First check if the either of the requests are re-queued
1162 * requests. Can't merge them if they are.
1163 */
1164 if (req->special || next->special)
1165 return 0;
1166
1167 /*
Nikita Danilovdfa1a552005-06-25 14:59:20 -07001168 * Will it become too large?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 */
1170 if ((req->nr_sectors + next->nr_sectors) > q->max_sectors)
1171 return 0;
1172
1173 total_phys_segments = req->nr_phys_segments + next->nr_phys_segments;
1174 if (blk_phys_contig_segment(q, req->biotail, next->bio))
1175 total_phys_segments--;
1176
1177 if (total_phys_segments > q->max_phys_segments)
1178 return 0;
1179
1180 total_hw_segments = req->nr_hw_segments + next->nr_hw_segments;
1181 if (blk_hw_contig_segment(q, req->biotail, next->bio)) {
1182 int len = req->biotail->bi_hw_back_size + next->bio->bi_hw_front_size;
1183 /*
1184 * propagate the combined length to the end of the requests
1185 */
1186 if (req->nr_hw_segments == 1)
1187 req->bio->bi_hw_front_size = len;
1188 if (next->nr_hw_segments == 1)
1189 next->biotail->bi_hw_back_size = len;
1190 total_hw_segments--;
1191 }
1192
1193 if (total_hw_segments > q->max_hw_segments)
1194 return 0;
1195
1196 /* Merge is OK... */
1197 req->nr_phys_segments = total_phys_segments;
1198 req->nr_hw_segments = total_hw_segments;
1199 return 1;
1200}
1201
1202/*
1203 * "plug" the device if there are no outstanding requests: this will
1204 * force the transfer to start only after we have put all the requests
1205 * on the list.
1206 *
1207 * This is called with interrupts off and no requests on the queue and
1208 * with the queue lock held.
1209 */
Jens Axboe165125e2007-07-24 09:28:11 +02001210void blk_plug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
1212 WARN_ON(!irqs_disabled());
1213
1214 /*
1215 * don't plug a stopped queue, it must be paired with blk_start_queue()
1216 * which will restart the queueing
1217 */
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +02001218 if (blk_queue_stopped(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return;
1220
Jens Axboe2056a782006-03-23 20:00:26 +01001221 if (!test_and_set_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
Jens Axboe2056a782006-03-23 20:00:26 +01001223 blk_add_trace_generic(q, NULL, 0, BLK_TA_PLUG);
1224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
1227EXPORT_SYMBOL(blk_plug_device);
1228
1229/*
1230 * remove the queue from the plugged list, if present. called with
1231 * queue lock held and interrupts disabled.
1232 */
Jens Axboe165125e2007-07-24 09:28:11 +02001233int blk_remove_plug(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
1235 WARN_ON(!irqs_disabled());
1236
1237 if (!test_and_clear_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags))
1238 return 0;
1239
1240 del_timer(&q->unplug_timer);
1241 return 1;
1242}
1243
1244EXPORT_SYMBOL(blk_remove_plug);
1245
1246/*
1247 * remove the plug and let it rip..
1248 */
Jens Axboe165125e2007-07-24 09:28:11 +02001249void __generic_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
Coywolf Qi Hunt7daac492006-04-19 10:14:49 +02001251 if (unlikely(blk_queue_stopped(q)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return;
1253
1254 if (!blk_remove_plug(q))
1255 return;
1256
Jens Axboe22e2c502005-06-27 10:55:12 +02001257 q->request_fn(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258}
1259EXPORT_SYMBOL(__generic_unplug_device);
1260
1261/**
1262 * generic_unplug_device - fire a request queue
Jens Axboe165125e2007-07-24 09:28:11 +02001263 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 *
1265 * Description:
1266 * Linux uses plugging to build bigger requests queues before letting
1267 * the device have at them. If a queue is plugged, the I/O scheduler
1268 * is still adding and merging requests on the queue. Once the queue
1269 * gets unplugged, the request_fn defined for the queue is invoked and
1270 * transfers started.
1271 **/
Jens Axboe165125e2007-07-24 09:28:11 +02001272void generic_unplug_device(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
1274 spin_lock_irq(q->queue_lock);
1275 __generic_unplug_device(q);
1276 spin_unlock_irq(q->queue_lock);
1277}
1278EXPORT_SYMBOL(generic_unplug_device);
1279
1280static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
1281 struct page *page)
1282{
Jens Axboe165125e2007-07-24 09:28:11 +02001283 struct request_queue *q = bdi->unplug_io_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05001285 blk_unplug(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
David Howells65f27f32006-11-22 14:55:48 +00001288static void blk_unplug_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
Jens Axboe165125e2007-07-24 09:28:11 +02001290 struct request_queue *q =
1291 container_of(work, struct request_queue, unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Jens Axboe2056a782006-03-23 20:00:26 +01001293 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
1294 q->rq.count[READ] + q->rq.count[WRITE]);
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 q->unplug_fn(q);
1297}
1298
1299static void blk_unplug_timeout(unsigned long data)
1300{
Jens Axboe165125e2007-07-24 09:28:11 +02001301 struct request_queue *q = (struct request_queue *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Jens Axboe2056a782006-03-23 20:00:26 +01001303 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_TIMER, NULL,
1304 q->rq.count[READ] + q->rq.count[WRITE]);
1305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 kblockd_schedule_work(&q->unplug_work);
1307}
1308
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05001309void blk_unplug(struct request_queue *q)
1310{
1311 /*
1312 * devices don't necessarily have an ->unplug_fn defined
1313 */
1314 if (q->unplug_fn) {
1315 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
1316 q->rq.count[READ] + q->rq.count[WRITE]);
1317
1318 q->unplug_fn(q);
1319 }
1320}
1321EXPORT_SYMBOL(blk_unplug);
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323/**
1324 * blk_start_queue - restart a previously stopped queue
Jens Axboe165125e2007-07-24 09:28:11 +02001325 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 *
1327 * Description:
1328 * blk_start_queue() will clear the stop flag on the queue, and call
1329 * the request_fn for the queue if it was in a stopped state when
1330 * entered. Also see blk_stop_queue(). Queue lock must be held.
1331 **/
Jens Axboe165125e2007-07-24 09:28:11 +02001332void blk_start_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +02001334 WARN_ON(!irqs_disabled());
1335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 clear_bit(QUEUE_FLAG_STOPPED, &q->queue_flags);
1337
1338 /*
1339 * one level of recursion is ok and is much faster than kicking
1340 * the unplug handling
1341 */
1342 if (!test_and_set_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) {
1343 q->request_fn(q);
1344 clear_bit(QUEUE_FLAG_REENTER, &q->queue_flags);
1345 } else {
1346 blk_plug_device(q);
1347 kblockd_schedule_work(&q->unplug_work);
1348 }
1349}
1350
1351EXPORT_SYMBOL(blk_start_queue);
1352
1353/**
1354 * blk_stop_queue - stop a queue
Jens Axboe165125e2007-07-24 09:28:11 +02001355 * @q: The &struct request_queue in question
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 *
1357 * Description:
1358 * The Linux block layer assumes that a block driver will consume all
1359 * entries on the request queue when the request_fn strategy is called.
1360 * Often this will not happen, because of hardware limitations (queue
1361 * depth settings). If a device driver gets a 'queue full' response,
1362 * or if it simply chooses not to queue more I/O at one point, it can
1363 * call this function to prevent the request_fn from being called until
1364 * the driver has signalled it's ready to go again. This happens by calling
1365 * blk_start_queue() to restart queue operations. Queue lock must be held.
1366 **/
Jens Axboe165125e2007-07-24 09:28:11 +02001367void blk_stop_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
1369 blk_remove_plug(q);
1370 set_bit(QUEUE_FLAG_STOPPED, &q->queue_flags);
1371}
1372EXPORT_SYMBOL(blk_stop_queue);
1373
1374/**
1375 * blk_sync_queue - cancel any pending callbacks on a queue
1376 * @q: the queue
1377 *
1378 * Description:
1379 * The block layer may perform asynchronous callback activity
1380 * on a queue, such as calling the unplug function after a timeout.
1381 * A block device may call blk_sync_queue to ensure that any
1382 * such activity is cancelled, thus allowing it to release resources
Michael Opdenacker59c51592007-05-09 08:57:56 +02001383 * that the callbacks might use. The caller must already have made sure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 * that its ->make_request_fn will not re-add plugging prior to calling
1385 * this function.
1386 *
1387 */
1388void blk_sync_queue(struct request_queue *q)
1389{
1390 del_timer_sync(&q->unplug_timer);
Oleg Nesterovabbeb882007-10-23 15:08:19 +02001391 kblockd_flush_work(&q->unplug_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392}
1393EXPORT_SYMBOL(blk_sync_queue);
1394
1395/**
1396 * blk_run_queue - run a single device queue
1397 * @q: The queue to run
1398 */
1399void blk_run_queue(struct request_queue *q)
1400{
1401 unsigned long flags;
1402
1403 spin_lock_irqsave(q->queue_lock, flags);
1404 blk_remove_plug(q);
Jens Axboedac07ec2006-05-11 08:20:16 +02001405
1406 /*
1407 * Only recurse once to avoid overrunning the stack, let the unplug
1408 * handling reinvoke the handler shortly if we already got there.
1409 */
1410 if (!elv_queue_empty(q)) {
1411 if (!test_and_set_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) {
1412 q->request_fn(q);
1413 clear_bit(QUEUE_FLAG_REENTER, &q->queue_flags);
1414 } else {
1415 blk_plug_device(q);
1416 kblockd_schedule_work(&q->unplug_work);
1417 }
1418 }
1419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 spin_unlock_irqrestore(q->queue_lock, flags);
1421}
1422EXPORT_SYMBOL(blk_run_queue);
1423
Jens Axboe165125e2007-07-24 09:28:11 +02001424void blk_put_queue(struct request_queue *q)
Al Viro483f4af2006-03-18 18:34:37 -05001425{
1426 kobject_put(&q->kobj);
1427}
1428EXPORT_SYMBOL(blk_put_queue);
1429
Jens Axboe165125e2007-07-24 09:28:11 +02001430void blk_cleanup_queue(struct request_queue * q)
Al Viro483f4af2006-03-18 18:34:37 -05001431{
1432 mutex_lock(&q->sysfs_lock);
1433 set_bit(QUEUE_FLAG_DEAD, &q->queue_flags);
1434 mutex_unlock(&q->sysfs_lock);
1435
1436 if (q->elevator)
1437 elevator_exit(q->elevator);
1438
1439 blk_put_queue(q);
1440}
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442EXPORT_SYMBOL(blk_cleanup_queue);
1443
Jens Axboe165125e2007-07-24 09:28:11 +02001444static int blk_init_free_list(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
1446 struct request_list *rl = &q->rq;
1447
1448 rl->count[READ] = rl->count[WRITE] = 0;
1449 rl->starved[READ] = rl->starved[WRITE] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +02001450 rl->elvpriv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 init_waitqueue_head(&rl->wait[READ]);
1452 init_waitqueue_head(&rl->wait[WRITE]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Christoph Lameter19460892005-06-23 00:08:19 -07001454 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
1455 mempool_free_slab, request_cachep, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 if (!rl->rq_pool)
1458 return -ENOMEM;
1459
1460 return 0;
1461}
1462
Jens Axboe165125e2007-07-24 09:28:11 +02001463struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
Christoph Lameter19460892005-06-23 00:08:19 -07001465 return blk_alloc_queue_node(gfp_mask, -1);
1466}
1467EXPORT_SYMBOL(blk_alloc_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Jens Axboe165125e2007-07-24 09:28:11 +02001469struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
Christoph Lameter19460892005-06-23 00:08:19 -07001470{
Jens Axboe165125e2007-07-24 09:28:11 +02001471 struct request_queue *q;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07001472 int err;
Christoph Lameter19460892005-06-23 00:08:19 -07001473
Jens Axboe8324aa92008-01-29 14:51:59 +01001474 q = kmem_cache_alloc_node(blk_requestq_cachep,
Christoph Lameter94f60302007-07-17 04:03:29 -07001475 gfp_mask | __GFP_ZERO, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 if (!q)
1477 return NULL;
1478
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07001479 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
1480 q->backing_dev_info.unplug_io_data = q;
1481 err = bdi_init(&q->backing_dev_info);
1482 if (err) {
Jens Axboe8324aa92008-01-29 14:51:59 +01001483 kmem_cache_free(blk_requestq_cachep, q);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07001484 return NULL;
1485 }
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 init_timer(&q->unplug_timer);
Al Viro483f4af2006-03-18 18:34:37 -05001488
Jens Axboe8324aa92008-01-29 14:51:59 +01001489 kobject_init(&q->kobj, &blk_queue_ktype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Al Viro483f4af2006-03-18 18:34:37 -05001491 mutex_init(&q->sysfs_lock);
1492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 return q;
1494}
Christoph Lameter19460892005-06-23 00:08:19 -07001495EXPORT_SYMBOL(blk_alloc_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497/**
1498 * blk_init_queue - prepare a request queue for use with a block device
1499 * @rfn: The function to be called to process requests that have been
1500 * placed on the queue.
1501 * @lock: Request queue spin lock
1502 *
1503 * Description:
1504 * If a block device wishes to use the standard request handling procedures,
1505 * which sorts requests and coalesces adjacent requests, then it must
1506 * call blk_init_queue(). The function @rfn will be called when there
1507 * are requests on the queue that need to be processed. If the device
1508 * supports plugging, then @rfn may not be called immediately when requests
1509 * are available on the queue, but may be called at some time later instead.
1510 * Plugged queues are generally unplugged when a buffer belonging to one
1511 * of the requests on the queue is needed, or due to memory pressure.
1512 *
1513 * @rfn is not required, or even expected, to remove all requests off the
1514 * queue, but only as many as it can handle at a time. If it does leave
1515 * requests on the queue, it is responsible for arranging that the requests
1516 * get dealt with eventually.
1517 *
1518 * The queue spin lock must be held while manipulating the requests on the
Paolo 'Blaisorblade' Giarrussoa038e252006-06-05 12:09:01 +02001519 * request queue; this lock will be taken also from interrupt context, so irq
1520 * disabling is needed for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 *
1522 * Function returns a pointer to the initialized request queue, or NULL if
1523 * it didn't succeed.
1524 *
1525 * Note:
1526 * blk_init_queue() must be paired with a blk_cleanup_queue() call
1527 * when the block device is deactivated (such as at module unload).
1528 **/
Christoph Lameter19460892005-06-23 00:08:19 -07001529
Jens Axboe165125e2007-07-24 09:28:11 +02001530struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
Christoph Lameter19460892005-06-23 00:08:19 -07001532 return blk_init_queue_node(rfn, lock, -1);
1533}
1534EXPORT_SYMBOL(blk_init_queue);
1535
Jens Axboe165125e2007-07-24 09:28:11 +02001536struct request_queue *
Christoph Lameter19460892005-06-23 00:08:19 -07001537blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
1538{
Jens Axboe165125e2007-07-24 09:28:11 +02001539 struct request_queue *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 if (!q)
1542 return NULL;
1543
Christoph Lameter19460892005-06-23 00:08:19 -07001544 q->node = node_id;
Al Viro8669aaf2006-03-18 13:50:00 -05001545 if (blk_init_free_list(q)) {
Jens Axboe8324aa92008-01-29 14:51:59 +01001546 kmem_cache_free(blk_requestq_cachep, q);
Al Viro8669aaf2006-03-18 13:50:00 -05001547 return NULL;
1548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
152587d2005-04-12 16:22:06 -05001550 /*
1551 * if caller didn't supply a lock, they get per-queue locking with
1552 * our embedded lock
1553 */
1554 if (!lock) {
1555 spin_lock_init(&q->__queue_lock);
1556 lock = &q->__queue_lock;
1557 }
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 q->request_fn = rfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 q->prep_rq_fn = NULL;
1561 q->unplug_fn = generic_unplug_device;
1562 q->queue_flags = (1 << QUEUE_FLAG_CLUSTER);
1563 q->queue_lock = lock;
1564
1565 blk_queue_segment_boundary(q, 0xffffffff);
1566
1567 blk_queue_make_request(q, __make_request);
1568 blk_queue_max_segment_size(q, MAX_SEGMENT_SIZE);
1569
1570 blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS);
1571 blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS);
1572
Alan Stern44ec9542007-02-20 11:01:57 -05001573 q->sg_reserved_size = INT_MAX;
1574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 /*
1576 * all done
1577 */
1578 if (!elevator_init(q, NULL)) {
1579 blk_queue_congestion_threshold(q);
1580 return q;
1581 }
1582
Al Viro8669aaf2006-03-18 13:50:00 -05001583 blk_put_queue(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 return NULL;
1585}
Christoph Lameter19460892005-06-23 00:08:19 -07001586EXPORT_SYMBOL(blk_init_queue_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Jens Axboe165125e2007-07-24 09:28:11 +02001588int blk_get_queue(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
Nick Pigginfde6ad22005-06-23 00:08:53 -07001590 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
Al Viro483f4af2006-03-18 18:34:37 -05001591 kobject_get(&q->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 return 0;
1593 }
1594
1595 return 1;
1596}
1597
1598EXPORT_SYMBOL(blk_get_queue);
1599
Jens Axboe165125e2007-07-24 09:28:11 +02001600static inline void blk_free_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
Jens Axboe4aff5e22006-08-10 08:44:47 +02001602 if (rq->cmd_flags & REQ_ELVPRIV)
Tejun Heocb98fc82005-10-28 08:29:39 +02001603 elv_put_request(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 mempool_free(rq, q->rq.rq_pool);
1605}
1606
Jens Axboe1ea25ecb2006-07-18 22:24:11 +02001607static struct request *
Jens Axboe165125e2007-07-24 09:28:11 +02001608blk_alloc_request(struct request_queue *q, int rw, int priv, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609{
1610 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
1611
1612 if (!rq)
1613 return NULL;
1614
1615 /*
Jens Axboe4aff5e22006-08-10 08:44:47 +02001616 * first three bits are identical in rq->cmd_flags and bio->bi_rw,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 * see bio.h and blkdev.h
1618 */
Jens Axboe49171e52006-08-10 08:59:11 +02001619 rq->cmd_flags = rw | REQ_ALLOCED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Tejun Heocb98fc82005-10-28 08:29:39 +02001621 if (priv) {
Jens Axboecb78b282006-07-28 09:32:57 +02001622 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
Tejun Heocb98fc82005-10-28 08:29:39 +02001623 mempool_free(rq, q->rq.rq_pool);
1624 return NULL;
1625 }
Jens Axboe4aff5e22006-08-10 08:44:47 +02001626 rq->cmd_flags |= REQ_ELVPRIV;
Tejun Heocb98fc82005-10-28 08:29:39 +02001627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Tejun Heocb98fc82005-10-28 08:29:39 +02001629 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630}
1631
1632/*
1633 * ioc_batching returns true if the ioc is a valid batching request and
1634 * should be given priority access to a request.
1635 */
Jens Axboe165125e2007-07-24 09:28:11 +02001636static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
1638 if (!ioc)
1639 return 0;
1640
1641 /*
1642 * Make sure the process is able to allocate at least 1 request
1643 * even if the batch times out, otherwise we could theoretically
1644 * lose wakeups.
1645 */
1646 return ioc->nr_batch_requests == q->nr_batching ||
1647 (ioc->nr_batch_requests > 0
1648 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
1649}
1650
1651/*
1652 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
1653 * will cause the process to be a "batcher" on all queues in the system. This
1654 * is the behaviour we want though - once it gets a wakeup it should be given
1655 * a nice run.
1656 */
Jens Axboe165125e2007-07-24 09:28:11 +02001657static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
1659 if (!ioc || ioc_batching(q, ioc))
1660 return;
1661
1662 ioc->nr_batch_requests = q->nr_batching;
1663 ioc->last_waited = jiffies;
1664}
1665
Jens Axboe165125e2007-07-24 09:28:11 +02001666static void __freed_request(struct request_queue *q, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
1668 struct request_list *rl = &q->rq;
1669
1670 if (rl->count[rw] < queue_congestion_off_threshold(q))
Thomas Maier79e2de42006-10-19 23:28:15 -07001671 blk_clear_queue_congested(q, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
1673 if (rl->count[rw] + 1 <= q->nr_requests) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 if (waitqueue_active(&rl->wait[rw]))
1675 wake_up(&rl->wait[rw]);
1676
1677 blk_clear_queue_full(q, rw);
1678 }
1679}
1680
1681/*
1682 * A request has just been released. Account for it, update the full and
1683 * congestion status, wake up any waiters. Called under q->queue_lock.
1684 */
Jens Axboe165125e2007-07-24 09:28:11 +02001685static void freed_request(struct request_queue *q, int rw, int priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
1687 struct request_list *rl = &q->rq;
1688
1689 rl->count[rw]--;
Tejun Heocb98fc82005-10-28 08:29:39 +02001690 if (priv)
1691 rl->elvpriv--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
1693 __freed_request(q, rw);
1694
1695 if (unlikely(rl->starved[rw ^ 1]))
1696 __freed_request(q, rw ^ 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697}
1698
1699#define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist)
1700/*
Nick Piggind6344532005-06-28 20:45:14 -07001701 * Get a free request, queue_lock must be held.
1702 * Returns NULL on failure, with queue_lock held.
1703 * Returns !NULL on success, with queue_lock *not held*.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 */
Jens Axboe165125e2007-07-24 09:28:11 +02001705static struct request *get_request(struct request_queue *q, int rw_flags,
Jens Axboe7749a8d2006-12-13 13:02:26 +01001706 struct bio *bio, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
1708 struct request *rq = NULL;
1709 struct request_list *rl = &q->rq;
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001710 struct io_context *ioc = NULL;
Jens Axboe7749a8d2006-12-13 13:02:26 +01001711 const int rw = rw_flags & 0x01;
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001712 int may_queue, priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Jens Axboe7749a8d2006-12-13 13:02:26 +01001714 may_queue = elv_may_queue(q, rw_flags);
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001715 if (may_queue == ELV_MQUEUE_NO)
1716 goto rq_starved;
1717
1718 if (rl->count[rw]+1 >= queue_congestion_on_threshold(q)) {
1719 if (rl->count[rw]+1 >= q->nr_requests) {
Jens Axboeb5deef92006-07-19 23:39:40 +02001720 ioc = current_io_context(GFP_ATOMIC, q->node);
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001721 /*
1722 * The queue will fill after this allocation, so set
1723 * it as full, and mark this process as "batching".
1724 * This process will be allowed to complete a batch of
1725 * requests, others will be blocked.
1726 */
1727 if (!blk_queue_full(q, rw)) {
1728 ioc_set_batching(q, ioc);
1729 blk_set_queue_full(q, rw);
1730 } else {
1731 if (may_queue != ELV_MQUEUE_MUST
1732 && !ioc_batching(q, ioc)) {
1733 /*
1734 * The queue is full and the allocating
1735 * process is not a "batcher", and not
1736 * exempted by the IO scheduler
1737 */
1738 goto out;
1739 }
1740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 }
Thomas Maier79e2de42006-10-19 23:28:15 -07001742 blk_set_queue_congested(q, rw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 }
1744
Jens Axboe082cf692005-06-28 16:35:11 +02001745 /*
1746 * Only allow batching queuers to allocate up to 50% over the defined
1747 * limit of requests, otherwise we could have thousands of requests
1748 * allocated with any setting of ->nr_requests
1749 */
Hugh Dickinsfd782a42005-06-29 15:15:40 +01001750 if (rl->count[rw] >= (3 * q->nr_requests / 2))
Jens Axboe082cf692005-06-28 16:35:11 +02001751 goto out;
Hugh Dickinsfd782a42005-06-29 15:15:40 +01001752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 rl->count[rw]++;
1754 rl->starved[rw] = 0;
Tejun Heocb98fc82005-10-28 08:29:39 +02001755
Jens Axboe64521d12005-10-28 08:30:39 +02001756 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
Tejun Heocb98fc82005-10-28 08:29:39 +02001757 if (priv)
1758 rl->elvpriv++;
1759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 spin_unlock_irq(q->queue_lock);
1761
Jens Axboe7749a8d2006-12-13 13:02:26 +01001762 rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001763 if (unlikely(!rq)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 /*
1765 * Allocation failed presumably due to memory. Undo anything
1766 * we might have messed up.
1767 *
1768 * Allocating task should really be put onto the front of the
1769 * wait queue, but this is pretty rare.
1770 */
1771 spin_lock_irq(q->queue_lock);
Tejun Heocb98fc82005-10-28 08:29:39 +02001772 freed_request(q, rw, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
1774 /*
1775 * in the very unlikely event that allocation failed and no
1776 * requests for this direction was pending, mark us starved
1777 * so that freeing of a request in the other direction will
1778 * notice us. another possible fix would be to split the
1779 * rq mempool into READ and WRITE
1780 */
1781rq_starved:
1782 if (unlikely(rl->count[rw] == 0))
1783 rl->starved[rw] = 1;
1784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 goto out;
1786 }
1787
Jens Axboe88ee5ef2005-11-12 11:09:12 +01001788 /*
1789 * ioc may be NULL here, and ioc_batching will be false. That's
1790 * OK, if the queue is under the request limit then requests need
1791 * not count toward the nr_batch_requests limit. There will always
1792 * be some limit enforced by BLK_BATCH_TIME.
1793 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 if (ioc_batching(q, ioc))
1795 ioc->nr_batch_requests--;
1796
1797 rq_init(q, rq);
Jens Axboe2056a782006-03-23 20:00:26 +01001798
1799 blk_add_trace_generic(q, bio, rw, BLK_TA_GETRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 return rq;
1802}
1803
1804/*
1805 * No available requests for this queue, unplug the device and wait for some
1806 * requests to become available.
Nick Piggind6344532005-06-28 20:45:14 -07001807 *
1808 * Called with q->queue_lock held, and returns with it unlocked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 */
Jens Axboe165125e2007-07-24 09:28:11 +02001810static struct request *get_request_wait(struct request_queue *q, int rw_flags,
Jens Axboe22e2c502005-06-27 10:55:12 +02001811 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
Jens Axboe7749a8d2006-12-13 13:02:26 +01001813 const int rw = rw_flags & 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 struct request *rq;
1815
Jens Axboe7749a8d2006-12-13 13:02:26 +01001816 rq = get_request(q, rw_flags, bio, GFP_NOIO);
Nick Piggin450991b2005-06-28 20:45:13 -07001817 while (!rq) {
1818 DEFINE_WAIT(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 struct request_list *rl = &q->rq;
1820
1821 prepare_to_wait_exclusive(&rl->wait[rw], &wait,
1822 TASK_UNINTERRUPTIBLE);
1823
Jens Axboe7749a8d2006-12-13 13:02:26 +01001824 rq = get_request(q, rw_flags, bio, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
1826 if (!rq) {
1827 struct io_context *ioc;
1828
Jens Axboe2056a782006-03-23 20:00:26 +01001829 blk_add_trace_generic(q, bio, rw, BLK_TA_SLEEPRQ);
1830
Nick Piggind6344532005-06-28 20:45:14 -07001831 __generic_unplug_device(q);
1832 spin_unlock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 io_schedule();
1834
1835 /*
1836 * After sleeping, we become a "batching" process and
1837 * will be able to allocate at least one request, and
1838 * up to a big batch of them for a small period time.
1839 * See ioc_batching, ioc_set_batching
1840 */
Jens Axboeb5deef92006-07-19 23:39:40 +02001841 ioc = current_io_context(GFP_NOIO, q->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 ioc_set_batching(q, ioc);
Nick Piggind6344532005-06-28 20:45:14 -07001843
1844 spin_lock_irq(q->queue_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 }
1846 finish_wait(&rl->wait[rw], &wait);
Nick Piggin450991b2005-06-28 20:45:13 -07001847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
1849 return rq;
1850}
1851
Jens Axboe165125e2007-07-24 09:28:11 +02001852struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
1854 struct request *rq;
1855
1856 BUG_ON(rw != READ && rw != WRITE);
1857
Nick Piggind6344532005-06-28 20:45:14 -07001858 spin_lock_irq(q->queue_lock);
1859 if (gfp_mask & __GFP_WAIT) {
Jens Axboe22e2c502005-06-27 10:55:12 +02001860 rq = get_request_wait(q, rw, NULL);
Nick Piggind6344532005-06-28 20:45:14 -07001861 } else {
Jens Axboe22e2c502005-06-27 10:55:12 +02001862 rq = get_request(q, rw, NULL, gfp_mask);
Nick Piggind6344532005-06-28 20:45:14 -07001863 if (!rq)
1864 spin_unlock_irq(q->queue_lock);
1865 }
1866 /* q->queue_lock is unlocked at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868 return rq;
1869}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870EXPORT_SYMBOL(blk_get_request);
1871
1872/**
Jens Axboedc72ef42006-07-20 14:54:05 +02001873 * blk_start_queueing - initiate dispatch of requests to device
1874 * @q: request queue to kick into gear
1875 *
1876 * This is basically a helper to remove the need to know whether a queue
1877 * is plugged or not if someone just wants to initiate dispatch of requests
1878 * for this queue.
1879 *
1880 * The queue lock must be held with interrupts disabled.
1881 */
Jens Axboe165125e2007-07-24 09:28:11 +02001882void blk_start_queueing(struct request_queue *q)
Jens Axboedc72ef42006-07-20 14:54:05 +02001883{
1884 if (!blk_queue_plugged(q))
1885 q->request_fn(q);
1886 else
1887 __generic_unplug_device(q);
1888}
1889EXPORT_SYMBOL(blk_start_queueing);
1890
1891/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 * blk_requeue_request - put a request back on queue
1893 * @q: request queue where request should be inserted
1894 * @rq: request to be inserted
1895 *
1896 * Description:
1897 * Drivers often keep queueing requests until the hardware cannot accept
1898 * more, when that condition happens we need to put the request back
1899 * on the queue. Must be called with queue lock held.
1900 */
Jens Axboe165125e2007-07-24 09:28:11 +02001901void blk_requeue_request(struct request_queue *q, struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902{
Jens Axboe2056a782006-03-23 20:00:26 +01001903 blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
1904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (blk_rq_tagged(rq))
1906 blk_queue_end_tag(q, rq);
1907
1908 elv_requeue_request(q, rq);
1909}
1910
1911EXPORT_SYMBOL(blk_requeue_request);
1912
1913/**
1914 * blk_insert_request - insert a special request in to a request queue
1915 * @q: request queue where request should be inserted
1916 * @rq: request to be inserted
1917 * @at_head: insert request at head or tail of queue
1918 * @data: private data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 *
1920 * Description:
1921 * Many block devices need to execute commands asynchronously, so they don't
1922 * block the whole kernel from preemption during request execution. This is
1923 * accomplished normally by inserting aritficial requests tagged as
1924 * REQ_SPECIAL in to the corresponding request queue, and letting them be
1925 * scheduled for actual execution by the request queue.
1926 *
1927 * We have the option of inserting the head or the tail of the queue.
1928 * Typically we use the tail for new ioctls and so forth. We use the head
1929 * of the queue for things like a QUEUE_FULL message from a device, or a
1930 * host that is unable to accept a particular command.
1931 */
Jens Axboe165125e2007-07-24 09:28:11 +02001932void blk_insert_request(struct request_queue *q, struct request *rq,
Tejun Heo 867d1192005-04-24 02:06:05 -05001933 int at_head, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
Tejun Heo 867d1192005-04-24 02:06:05 -05001935 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 unsigned long flags;
1937
1938 /*
1939 * tell I/O scheduler that this isn't a regular read/write (ie it
1940 * must not attempt merges on this) and that it acts as a soft
1941 * barrier
1942 */
Jens Axboe4aff5e22006-08-10 08:44:47 +02001943 rq->cmd_type = REQ_TYPE_SPECIAL;
1944 rq->cmd_flags |= REQ_SOFTBARRIER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 rq->special = data;
1947
1948 spin_lock_irqsave(q->queue_lock, flags);
1949
1950 /*
1951 * If command is tagged, release the tag
1952 */
Tejun Heo 867d1192005-04-24 02:06:05 -05001953 if (blk_rq_tagged(rq))
1954 blk_queue_end_tag(q, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Jerome Marchandb238b3d2007-10-23 15:05:46 +02001956 drive_stat_acct(rq, 1);
Tejun Heo 867d1192005-04-24 02:06:05 -05001957 __elv_add_request(q, rq, where, 0);
Jens Axboedc72ef42006-07-20 14:54:05 +02001958 blk_start_queueing(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 spin_unlock_irqrestore(q->queue_lock, flags);
1960}
1961
1962EXPORT_SYMBOL(blk_insert_request);
1963
Mike Christie0e75f902006-12-01 10:40:55 +01001964static int __blk_rq_unmap_user(struct bio *bio)
1965{
1966 int ret = 0;
1967
1968 if (bio) {
1969 if (bio_flagged(bio, BIO_USER_MAPPED))
1970 bio_unmap_user(bio);
1971 else
1972 ret = bio_uncopy_user(bio);
1973 }
1974
1975 return ret;
1976}
1977
NeilBrown3001ca72007-08-16 13:31:27 +02001978int blk_rq_append_bio(struct request_queue *q, struct request *rq,
1979 struct bio *bio)
1980{
1981 if (!rq->bio)
1982 blk_rq_bio_prep(q, rq, bio);
1983 else if (!ll_back_merge_fn(q, rq, bio))
1984 return -EINVAL;
1985 else {
1986 rq->biotail->bi_next = bio;
1987 rq->biotail = bio;
1988
1989 rq->data_len += bio->bi_size;
1990 }
1991 return 0;
1992}
1993EXPORT_SYMBOL(blk_rq_append_bio);
1994
Jens Axboe165125e2007-07-24 09:28:11 +02001995static int __blk_rq_map_user(struct request_queue *q, struct request *rq,
Mike Christie0e75f902006-12-01 10:40:55 +01001996 void __user *ubuf, unsigned int len)
1997{
1998 unsigned long uaddr;
1999 struct bio *bio, *orig_bio;
2000 int reading, ret;
2001
2002 reading = rq_data_dir(rq) == READ;
2003
2004 /*
2005 * if alignment requirement is satisfied, map in user pages for
2006 * direct dma. else, set up kernel bounce buffers
2007 */
2008 uaddr = (unsigned long) ubuf;
2009 if (!(uaddr & queue_dma_alignment(q)) && !(len & queue_dma_alignment(q)))
2010 bio = bio_map_user(q, NULL, uaddr, len, reading);
2011 else
2012 bio = bio_copy_user(q, uaddr, len, reading);
2013
Jens Axboe29852592006-12-19 08:27:31 +01002014 if (IS_ERR(bio))
Mike Christie0e75f902006-12-01 10:40:55 +01002015 return PTR_ERR(bio);
Mike Christie0e75f902006-12-01 10:40:55 +01002016
2017 orig_bio = bio;
2018 blk_queue_bounce(q, &bio);
Jens Axboe29852592006-12-19 08:27:31 +01002019
Mike Christie0e75f902006-12-01 10:40:55 +01002020 /*
2021 * We link the bounce buffer in and could have to traverse it
2022 * later so we have to get a ref to prevent it from being freed
2023 */
2024 bio_get(bio);
2025
NeilBrown3001ca72007-08-16 13:31:27 +02002026 ret = blk_rq_append_bio(q, rq, bio);
2027 if (!ret)
2028 return bio->bi_size;
Mike Christie0e75f902006-12-01 10:40:55 +01002029
Mike Christie0e75f902006-12-01 10:40:55 +01002030 /* if it was boucned we must call the end io function */
NeilBrown6712ecf2007-09-27 12:47:43 +02002031 bio_endio(bio, 0);
Mike Christie0e75f902006-12-01 10:40:55 +01002032 __blk_rq_unmap_user(orig_bio);
2033 bio_put(bio);
2034 return ret;
2035}
2036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037/**
2038 * blk_rq_map_user - map user data to a request, for REQ_BLOCK_PC usage
2039 * @q: request queue where request should be inserted
Christoph Hellwig 73747ae2005-06-20 14:21:01 +02002040 * @rq: request structure to fill
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 * @ubuf: the user buffer
2042 * @len: length of user data
2043 *
2044 * Description:
2045 * Data will be mapped directly for zero copy io, if possible. Otherwise
2046 * a kernel bounce buffer is used.
2047 *
2048 * A matching blk_rq_unmap_user() must be issued at the end of io, while
2049 * still in process context.
2050 *
2051 * Note: The mapped bio may need to be bounced through blk_queue_bounce()
2052 * before being submitted to the device, as pages mapped may be out of
2053 * reach. It's the callers responsibility to make sure this happens. The
2054 * original bio must be passed back in to blk_rq_unmap_user() for proper
2055 * unmapping.
2056 */
Jens Axboe165125e2007-07-24 09:28:11 +02002057int blk_rq_map_user(struct request_queue *q, struct request *rq,
2058 void __user *ubuf, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059{
Mike Christie0e75f902006-12-01 10:40:55 +01002060 unsigned long bytes_read = 0;
Jens Axboe8e5cfc42006-12-19 11:12:46 +01002061 struct bio *bio = NULL;
Mike Christie0e75f902006-12-01 10:40:55 +01002062 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Mike Christiedefd94b2005-12-05 02:37:06 -06002064 if (len > (q->max_hw_sectors << 9))
Jens Axboedd1cab92005-06-20 14:06:01 +02002065 return -EINVAL;
2066 if (!len || !ubuf)
2067 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Mike Christie0e75f902006-12-01 10:40:55 +01002069 while (bytes_read != len) {
2070 unsigned long map_len, end, start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Mike Christie0e75f902006-12-01 10:40:55 +01002072 map_len = min_t(unsigned long, len - bytes_read, BIO_MAX_SIZE);
2073 end = ((unsigned long)ubuf + map_len + PAGE_SIZE - 1)
2074 >> PAGE_SHIFT;
2075 start = (unsigned long)ubuf >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
Mike Christie0e75f902006-12-01 10:40:55 +01002077 /*
2078 * A bad offset could cause us to require BIO_MAX_PAGES + 1
2079 * pages. If this happens we just lower the requested
2080 * mapping len by a page so that we can fit
2081 */
2082 if (end - start > BIO_MAX_PAGES)
2083 map_len -= PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Mike Christie0e75f902006-12-01 10:40:55 +01002085 ret = __blk_rq_map_user(q, rq, ubuf, map_len);
2086 if (ret < 0)
2087 goto unmap_rq;
Jens Axboe8e5cfc42006-12-19 11:12:46 +01002088 if (!bio)
2089 bio = rq->bio;
Mike Christie0e75f902006-12-01 10:40:55 +01002090 bytes_read += ret;
2091 ubuf += ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 }
2093
Mike Christie0e75f902006-12-01 10:40:55 +01002094 rq->buffer = rq->data = NULL;
2095 return 0;
2096unmap_rq:
Jens Axboe8e5cfc42006-12-19 11:12:46 +01002097 blk_rq_unmap_user(bio);
Mike Christie0e75f902006-12-01 10:40:55 +01002098 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099}
2100
2101EXPORT_SYMBOL(blk_rq_map_user);
2102
2103/**
James Bottomley f1970ba2005-06-20 14:06:52 +02002104 * blk_rq_map_user_iov - map user data to a request, for REQ_BLOCK_PC usage
2105 * @q: request queue where request should be inserted
2106 * @rq: request to map data to
2107 * @iov: pointer to the iovec
2108 * @iov_count: number of elements in the iovec
Randy Dunlapaf9997e2006-12-22 01:06:52 -08002109 * @len: I/O byte count
James Bottomley f1970ba2005-06-20 14:06:52 +02002110 *
2111 * Description:
2112 * Data will be mapped directly for zero copy io, if possible. Otherwise
2113 * a kernel bounce buffer is used.
2114 *
2115 * A matching blk_rq_unmap_user() must be issued at the end of io, while
2116 * still in process context.
2117 *
2118 * Note: The mapped bio may need to be bounced through blk_queue_bounce()
2119 * before being submitted to the device, as pages mapped may be out of
2120 * reach. It's the callers responsibility to make sure this happens. The
2121 * original bio must be passed back in to blk_rq_unmap_user() for proper
2122 * unmapping.
2123 */
Jens Axboe165125e2007-07-24 09:28:11 +02002124int blk_rq_map_user_iov(struct request_queue *q, struct request *rq,
Mike Christie0e75f902006-12-01 10:40:55 +01002125 struct sg_iovec *iov, int iov_count, unsigned int len)
James Bottomley f1970ba2005-06-20 14:06:52 +02002126{
2127 struct bio *bio;
2128
2129 if (!iov || iov_count <= 0)
2130 return -EINVAL;
2131
2132 /* we don't allow misaligned data like bio_map_user() does. If the
2133 * user is using sg, they're expected to know the alignment constraints
2134 * and respect them accordingly */
2135 bio = bio_map_user_iov(q, NULL, iov, iov_count, rq_data_dir(rq)== READ);
2136 if (IS_ERR(bio))
2137 return PTR_ERR(bio);
2138
Mike Christie0e75f902006-12-01 10:40:55 +01002139 if (bio->bi_size != len) {
NeilBrown6712ecf2007-09-27 12:47:43 +02002140 bio_endio(bio, 0);
Mike Christie0e75f902006-12-01 10:40:55 +01002141 bio_unmap_user(bio);
2142 return -EINVAL;
2143 }
2144
2145 bio_get(bio);
James Bottomley f1970ba2005-06-20 14:06:52 +02002146 blk_rq_bio_prep(q, rq, bio);
2147 rq->buffer = rq->data = NULL;
James Bottomley f1970ba2005-06-20 14:06:52 +02002148 return 0;
2149}
2150
2151EXPORT_SYMBOL(blk_rq_map_user_iov);
2152
2153/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 * blk_rq_unmap_user - unmap a request with user data
Jens Axboe8e5cfc42006-12-19 11:12:46 +01002155 * @bio: start of bio list
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 *
2157 * Description:
Jens Axboe8e5cfc42006-12-19 11:12:46 +01002158 * Unmap a rq previously mapped by blk_rq_map_user(). The caller must
2159 * supply the original rq->bio from the blk_rq_map_user() return, since
2160 * the io completion may have changed rq->bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 */
Jens Axboe8e5cfc42006-12-19 11:12:46 +01002162int blk_rq_unmap_user(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
Jens Axboe8e5cfc42006-12-19 11:12:46 +01002164 struct bio *mapped_bio;
Jens Axboe48785bb2006-12-19 11:07:59 +01002165 int ret = 0, ret2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Jens Axboe8e5cfc42006-12-19 11:12:46 +01002167 while (bio) {
2168 mapped_bio = bio;
2169 if (unlikely(bio_flagged(bio, BIO_BOUNCED)))
Mike Christie0e75f902006-12-01 10:40:55 +01002170 mapped_bio = bio->bi_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
Jens Axboe48785bb2006-12-19 11:07:59 +01002172 ret2 = __blk_rq_unmap_user(mapped_bio);
2173 if (ret2 && !ret)
2174 ret = ret2;
2175
Jens Axboe8e5cfc42006-12-19 11:12:46 +01002176 mapped_bio = bio;
2177 bio = bio->bi_next;
2178 bio_put(mapped_bio);
Mike Christie0e75f902006-12-01 10:40:55 +01002179 }
Jens Axboe48785bb2006-12-19 11:07:59 +01002180
2181 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
2183
2184EXPORT_SYMBOL(blk_rq_unmap_user);
2185
2186/**
Mike Christie df46b9a2005-06-20 14:04:44 +02002187 * blk_rq_map_kern - map kernel data to a request, for REQ_BLOCK_PC usage
2188 * @q: request queue where request should be inserted
Christoph Hellwig 73747ae2005-06-20 14:21:01 +02002189 * @rq: request to fill
Mike Christie df46b9a2005-06-20 14:04:44 +02002190 * @kbuf: the kernel buffer
2191 * @len: length of user data
Christoph Hellwig 73747ae2005-06-20 14:21:01 +02002192 * @gfp_mask: memory allocation flags
Mike Christie df46b9a2005-06-20 14:04:44 +02002193 */
Jens Axboe165125e2007-07-24 09:28:11 +02002194int blk_rq_map_kern(struct request_queue *q, struct request *rq, void *kbuf,
Al Viro8267e262005-10-21 03:20:53 -04002195 unsigned int len, gfp_t gfp_mask)
Mike Christie df46b9a2005-06-20 14:04:44 +02002196{
Mike Christie df46b9a2005-06-20 14:04:44 +02002197 struct bio *bio;
2198
Mike Christiedefd94b2005-12-05 02:37:06 -06002199 if (len > (q->max_hw_sectors << 9))
Jens Axboedd1cab92005-06-20 14:06:01 +02002200 return -EINVAL;
2201 if (!len || !kbuf)
2202 return -EINVAL;
Mike Christie df46b9a2005-06-20 14:04:44 +02002203
2204 bio = bio_map_kern(q, kbuf, len, gfp_mask);
Jens Axboedd1cab92005-06-20 14:06:01 +02002205 if (IS_ERR(bio))
2206 return PTR_ERR(bio);
Mike Christie df46b9a2005-06-20 14:04:44 +02002207
Jens Axboedd1cab92005-06-20 14:06:01 +02002208 if (rq_data_dir(rq) == WRITE)
2209 bio->bi_rw |= (1 << BIO_RW);
Mike Christie df46b9a2005-06-20 14:04:44 +02002210
Jens Axboedd1cab92005-06-20 14:06:01 +02002211 blk_rq_bio_prep(q, rq, bio);
Mike Christie821de3a2007-05-08 19:12:23 +02002212 blk_queue_bounce(q, &rq->bio);
Jens Axboedd1cab92005-06-20 14:06:01 +02002213 rq->buffer = rq->data = NULL;
Jens Axboedd1cab92005-06-20 14:06:01 +02002214 return 0;
Mike Christie df46b9a2005-06-20 14:04:44 +02002215}
2216
2217EXPORT_SYMBOL(blk_rq_map_kern);
2218
Christoph Hellwig 73747ae2005-06-20 14:21:01 +02002219/**
2220 * blk_execute_rq_nowait - insert a request into queue for execution
2221 * @q: queue to insert the request in
2222 * @bd_disk: matching gendisk
2223 * @rq: request to insert
2224 * @at_head: insert request at head or tail of queue
2225 * @done: I/O completion handler
2226 *
2227 * Description:
2228 * Insert a fully prepared request at the back of the io scheduler queue
2229 * for execution. Don't wait for completion.
2230 */
Jens Axboe165125e2007-07-24 09:28:11 +02002231void blk_execute_rq_nowait(struct request_queue *q, struct gendisk *bd_disk,
James Bottomley f1970ba2005-06-20 14:06:52 +02002232 struct request *rq, int at_head,
Tejun Heo8ffdc652006-01-06 09:49:03 +01002233 rq_end_io_fn *done)
James Bottomley f1970ba2005-06-20 14:06:52 +02002234{
2235 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
2236
2237 rq->rq_disk = bd_disk;
Jens Axboe4aff5e22006-08-10 08:44:47 +02002238 rq->cmd_flags |= REQ_NOMERGE;
James Bottomley f1970ba2005-06-20 14:06:52 +02002239 rq->end_io = done;
Andrew Morton4c5d0bb2006-03-22 08:08:01 +01002240 WARN_ON(irqs_disabled());
2241 spin_lock_irq(q->queue_lock);
2242 __elv_add_request(q, rq, where, 1);
2243 __generic_unplug_device(q);
2244 spin_unlock_irq(q->queue_lock);
James Bottomley f1970ba2005-06-20 14:06:52 +02002245}
Mike Christie6e39b69e2005-11-11 05:30:24 -06002246EXPORT_SYMBOL_GPL(blk_execute_rq_nowait);
2247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248/**
2249 * blk_execute_rq - insert a request into queue for execution
2250 * @q: queue to insert the request in
2251 * @bd_disk: matching gendisk
2252 * @rq: request to insert
James Bottomley 994ca9a2005-06-20 14:11:09 +02002253 * @at_head: insert request at head or tail of queue
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 *
2255 * Description:
2256 * Insert a fully prepared request at the back of the io scheduler queue
Christoph Hellwig 73747ae2005-06-20 14:21:01 +02002257 * for execution and wait for completion.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 */
Jens Axboe165125e2007-07-24 09:28:11 +02002259int blk_execute_rq(struct request_queue *q, struct gendisk *bd_disk,
James Bottomley 994ca9a2005-06-20 14:11:09 +02002260 struct request *rq, int at_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261{
Ingo Molnar60be6b92006-07-03 00:25:26 -07002262 DECLARE_COMPLETION_ONSTACK(wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 char sense[SCSI_SENSE_BUFFERSIZE];
2264 int err = 0;
2265
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 /*
2267 * we need an extra reference to the request, so we can look at
2268 * it after io completion
2269 */
2270 rq->ref_count++;
2271
2272 if (!rq->sense) {
2273 memset(sense, 0, sizeof(sense));
2274 rq->sense = sense;
2275 rq->sense_len = 0;
2276 }
2277
Jens Axboec00895a2006-09-30 20:29:12 +02002278 rq->end_io_data = &wait;
James Bottomley 994ca9a2005-06-20 14:11:09 +02002279 blk_execute_rq_nowait(q, bd_disk, rq, at_head, blk_end_sync_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 wait_for_completion(&wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
2282 if (rq->errors)
2283 err = -EIO;
2284
2285 return err;
2286}
2287
2288EXPORT_SYMBOL(blk_execute_rq);
2289
Jens Axboefd5d8062007-10-16 11:05:02 +02002290static void bio_end_empty_barrier(struct bio *bio, int err)
2291{
2292 if (err)
2293 clear_bit(BIO_UPTODATE, &bio->bi_flags);
2294
2295 complete(bio->bi_private);
2296}
2297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298/**
2299 * blkdev_issue_flush - queue a flush
2300 * @bdev: blockdev to issue flush for
2301 * @error_sector: error sector
2302 *
2303 * Description:
2304 * Issue a flush for the block device in question. Caller can supply
2305 * room for storing the error offset in case of a flush error, if they
2306 * wish to. Caller must run wait_for_completion() on its own.
2307 */
2308int blkdev_issue_flush(struct block_device *bdev, sector_t *error_sector)
2309{
Jens Axboefd5d8062007-10-16 11:05:02 +02002310 DECLARE_COMPLETION_ONSTACK(wait);
Jens Axboe165125e2007-07-24 09:28:11 +02002311 struct request_queue *q;
Jens Axboefd5d8062007-10-16 11:05:02 +02002312 struct bio *bio;
2313 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
2315 if (bdev->bd_disk == NULL)
2316 return -ENXIO;
2317
2318 q = bdev_get_queue(bdev);
2319 if (!q)
2320 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Jens Axboefd5d8062007-10-16 11:05:02 +02002322 bio = bio_alloc(GFP_KERNEL, 0);
2323 if (!bio)
2324 return -ENOMEM;
2325
2326 bio->bi_end_io = bio_end_empty_barrier;
2327 bio->bi_private = &wait;
2328 bio->bi_bdev = bdev;
2329 submit_bio(1 << BIO_RW_BARRIER, bio);
2330
2331 wait_for_completion(&wait);
2332
2333 /*
2334 * The driver must store the error location in ->bi_sector, if
2335 * it supports it. For non-stacked drivers, this should be copied
2336 * from rq->sector.
2337 */
2338 if (error_sector)
2339 *error_sector = bio->bi_sector;
2340
2341 ret = 0;
2342 if (!bio_flagged(bio, BIO_UPTODATE))
2343 ret = -EIO;
2344
2345 bio_put(bio);
2346 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347}
2348
2349EXPORT_SYMBOL(blkdev_issue_flush);
2350
Jerome Marchandb238b3d2007-10-23 15:05:46 +02002351static void drive_stat_acct(struct request *rq, int new_io)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352{
2353 int rw = rq_data_dir(rq);
2354
2355 if (!blk_fs_request(rq) || !rq->rq_disk)
2356 return;
2357
Jens Axboed72d9042005-11-01 08:35:42 +01002358 if (!new_io) {
Jens Axboea3623572005-11-01 09:26:16 +01002359 __disk_stat_inc(rq->rq_disk, merges[rw]);
Jens Axboed72d9042005-11-01 08:35:42 +01002360 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 disk_round_stats(rq->rq_disk);
2362 rq->rq_disk->in_flight++;
2363 }
2364}
2365
2366/*
2367 * add-request adds a request to the linked list.
2368 * queue lock is held and interrupts disabled, as we muck with the
2369 * request queue list.
2370 */
Jens Axboe165125e2007-07-24 09:28:11 +02002371static inline void add_request(struct request_queue * q, struct request * req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372{
Jerome Marchandb238b3d2007-10-23 15:05:46 +02002373 drive_stat_acct(req, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 /*
2376 * elevator indicated where it wants this request to be
2377 * inserted at elevator_merge time
2378 */
2379 __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
2380}
2381
2382/*
2383 * disk_round_stats() - Round off the performance stats on a struct
2384 * disk_stats.
2385 *
2386 * The average IO queue length and utilisation statistics are maintained
2387 * by observing the current state of the queue length and the amount of
2388 * time it has been in this state for.
2389 *
2390 * Normally, that accounting is done on IO completion, but that can result
2391 * in more than a second's worth of IO being accounted for within any one
2392 * second, leading to >100% utilisation. To deal with that, we call this
2393 * function to do a round-off before returning the results when reading
2394 * /proc/diskstats. This accounts immediately for all queue usage up to
2395 * the current jiffies and restarts the counters again.
2396 */
2397void disk_round_stats(struct gendisk *disk)
2398{
2399 unsigned long now = jiffies;
2400
Chen, Kenneth Wb2982642005-10-13 21:49:29 +02002401 if (now == disk->stamp)
2402 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Chen, Kenneth W20e5c812005-10-13 21:48:42 +02002404 if (disk->in_flight) {
2405 __disk_stat_add(disk, time_in_queue,
2406 disk->in_flight * (now - disk->stamp));
2407 __disk_stat_add(disk, io_ticks, (now - disk->stamp));
2408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 disk->stamp = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410}
2411
Jun'ichi "Nick" Nomura3eaf8402006-02-01 03:04:53 -08002412EXPORT_SYMBOL_GPL(disk_round_stats);
2413
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414/*
2415 * queue lock must be held
2416 */
Jens Axboe165125e2007-07-24 09:28:11 +02002417void __blk_put_request(struct request_queue *q, struct request *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 if (unlikely(!q))
2420 return;
2421 if (unlikely(--req->ref_count))
2422 return;
2423
Tejun Heo8922e162005-10-20 16:23:44 +02002424 elv_completed_request(q, req);
2425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 /*
2427 * Request may not have originated from ll_rw_blk. if not,
2428 * it didn't come out of our reserved rq pools
2429 */
Jens Axboe49171e52006-08-10 08:59:11 +02002430 if (req->cmd_flags & REQ_ALLOCED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 int rw = rq_data_dir(req);
Jens Axboe4aff5e22006-08-10 08:44:47 +02002432 int priv = req->cmd_flags & REQ_ELVPRIV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 BUG_ON(!list_empty(&req->queuelist));
Jens Axboe98170642006-07-28 09:23:08 +02002435 BUG_ON(!hlist_unhashed(&req->hash));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
2437 blk_free_request(q, req);
Tejun Heocb98fc82005-10-28 08:29:39 +02002438 freed_request(q, rw, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 }
2440}
2441
Mike Christie6e39b69e2005-11-11 05:30:24 -06002442EXPORT_SYMBOL_GPL(__blk_put_request);
2443
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444void blk_put_request(struct request *req)
2445{
Tejun Heo8922e162005-10-20 16:23:44 +02002446 unsigned long flags;
Jens Axboe165125e2007-07-24 09:28:11 +02002447 struct request_queue *q = req->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Tejun Heo8922e162005-10-20 16:23:44 +02002449 /*
2450 * Gee, IDE calls in w/ NULL q. Fix IDE and remove the
2451 * following if (q) test.
2452 */
2453 if (q) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 spin_lock_irqsave(q->queue_lock, flags);
2455 __blk_put_request(q, req);
2456 spin_unlock_irqrestore(q->queue_lock, flags);
2457 }
2458}
2459
2460EXPORT_SYMBOL(blk_put_request);
2461
2462/**
2463 * blk_end_sync_rq - executes a completion event on a request
2464 * @rq: request to complete
Jens Axboefddfdea2006-01-31 15:24:34 +01002465 * @error: end io status of the request
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 */
Tejun Heo8ffdc652006-01-06 09:49:03 +01002467void blk_end_sync_rq(struct request *rq, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468{
Jens Axboec00895a2006-09-30 20:29:12 +02002469 struct completion *waiting = rq->end_io_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
Jens Axboec00895a2006-09-30 20:29:12 +02002471 rq->end_io_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 __blk_put_request(rq->q, rq);
2473
2474 /*
2475 * complete last, if this is a stack request the process (and thus
2476 * the rq pointer) could be invalid right after this complete()
2477 */
2478 complete(waiting);
2479}
2480EXPORT_SYMBOL(blk_end_sync_rq);
2481
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482/*
2483 * Has to be called with the request spinlock acquired
2484 */
Jens Axboe165125e2007-07-24 09:28:11 +02002485static int attempt_merge(struct request_queue *q, struct request *req,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 struct request *next)
2487{
2488 if (!rq_mergeable(req) || !rq_mergeable(next))
2489 return 0;
2490
2491 /*
Andreas Mohrd6e05ed2006-06-26 18:35:02 +02002492 * not contiguous
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 */
2494 if (req->sector + req->nr_sectors != next->sector)
2495 return 0;
2496
2497 if (rq_data_dir(req) != rq_data_dir(next)
2498 || req->rq_disk != next->rq_disk
Jens Axboec00895a2006-09-30 20:29:12 +02002499 || next->special)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 return 0;
2501
2502 /*
2503 * If we are allowed to merge, then append bio list
2504 * from next to rq and release next. merge_requests_fn
2505 * will have updated segment counts, update sector
2506 * counts here.
2507 */
Jens Axboe1aa4f242006-12-19 08:33:11 +01002508 if (!ll_merge_requests_fn(q, req, next))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 return 0;
2510
2511 /*
2512 * At this point we have either done a back merge
2513 * or front merge. We need the smaller start_time of
2514 * the merged requests to be the current request
2515 * for accounting purposes.
2516 */
2517 if (time_after(req->start_time, next->start_time))
2518 req->start_time = next->start_time;
2519
2520 req->biotail->bi_next = next->bio;
2521 req->biotail = next->biotail;
2522
2523 req->nr_sectors = req->hard_nr_sectors += next->hard_nr_sectors;
2524
2525 elv_merge_requests(q, req, next);
2526
2527 if (req->rq_disk) {
2528 disk_round_stats(req->rq_disk);
2529 req->rq_disk->in_flight--;
2530 }
2531
Jens Axboe22e2c502005-06-27 10:55:12 +02002532 req->ioprio = ioprio_best(req->ioprio, next->ioprio);
2533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 __blk_put_request(q, next);
2535 return 1;
2536}
2537
Jens Axboe165125e2007-07-24 09:28:11 +02002538static inline int attempt_back_merge(struct request_queue *q,
2539 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540{
2541 struct request *next = elv_latter_request(q, rq);
2542
2543 if (next)
2544 return attempt_merge(q, rq, next);
2545
2546 return 0;
2547}
2548
Jens Axboe165125e2007-07-24 09:28:11 +02002549static inline int attempt_front_merge(struct request_queue *q,
2550 struct request *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551{
2552 struct request *prev = elv_former_request(q, rq);
2553
2554 if (prev)
2555 return attempt_merge(q, prev, rq);
2556
2557 return 0;
2558}
2559
Tejun Heo52d9e672006-01-06 09:49:58 +01002560static void init_request_from_bio(struct request *req, struct bio *bio)
2561{
Jens Axboe4aff5e22006-08-10 08:44:47 +02002562 req->cmd_type = REQ_TYPE_FS;
Tejun Heo52d9e672006-01-06 09:49:58 +01002563
2564 /*
2565 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
2566 */
2567 if (bio_rw_ahead(bio) || bio_failfast(bio))
Jens Axboe4aff5e22006-08-10 08:44:47 +02002568 req->cmd_flags |= REQ_FAILFAST;
Tejun Heo52d9e672006-01-06 09:49:58 +01002569
2570 /*
2571 * REQ_BARRIER implies no merging, but lets make it explicit
2572 */
2573 if (unlikely(bio_barrier(bio)))
Jens Axboe4aff5e22006-08-10 08:44:47 +02002574 req->cmd_flags |= (REQ_HARDBARRIER | REQ_NOMERGE);
Tejun Heo52d9e672006-01-06 09:49:58 +01002575
Jens Axboeb31dc662006-06-13 08:26:10 +02002576 if (bio_sync(bio))
Jens Axboe4aff5e22006-08-10 08:44:47 +02002577 req->cmd_flags |= REQ_RW_SYNC;
Jens Axboe5404bc72006-08-10 09:01:02 +02002578 if (bio_rw_meta(bio))
2579 req->cmd_flags |= REQ_RW_META;
Jens Axboeb31dc662006-06-13 08:26:10 +02002580
Tejun Heo52d9e672006-01-06 09:49:58 +01002581 req->errors = 0;
2582 req->hard_sector = req->sector = bio->bi_sector;
Tejun Heo52d9e672006-01-06 09:49:58 +01002583 req->ioprio = bio_prio(bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01002584 req->start_time = jiffies;
NeilBrownbc1c56f2007-08-16 13:31:30 +02002585 blk_rq_bio_prep(req->q, req, bio);
Tejun Heo52d9e672006-01-06 09:49:58 +01002586}
2587
Jens Axboe165125e2007-07-24 09:28:11 +02002588static int __make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589{
Nick Piggin450991b2005-06-28 20:45:13 -07002590 struct request *req;
Jens Axboe51da90f2006-07-18 04:14:45 +02002591 int el_ret, nr_sectors, barrier, err;
2592 const unsigned short prio = bio_prio(bio);
2593 const int sync = bio_sync(bio);
Jens Axboe7749a8d2006-12-13 13:02:26 +01002594 int rw_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 nr_sectors = bio_sectors(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
2598 /*
2599 * low level driver can indicate that it wants pages above a
2600 * certain limit bounced to low memory (ie for highmem, or even
2601 * ISA dma in theory)
2602 */
2603 blk_queue_bounce(q, &bio);
2604
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 barrier = bio_barrier(bio);
Tejun Heo797e7db2006-01-06 09:51:03 +01002606 if (unlikely(barrier) && (q->next_ordered == QUEUE_ORDERED_NONE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 err = -EOPNOTSUPP;
2608 goto end_io;
2609 }
2610
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 spin_lock_irq(q->queue_lock);
2612
Nick Piggin450991b2005-06-28 20:45:13 -07002613 if (unlikely(barrier) || elv_queue_empty(q))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 goto get_rq;
2615
2616 el_ret = elv_merge(q, &req, bio);
2617 switch (el_ret) {
2618 case ELEVATOR_BACK_MERGE:
2619 BUG_ON(!rq_mergeable(req));
2620
Jens Axboe1aa4f242006-12-19 08:33:11 +01002621 if (!ll_back_merge_fn(q, req, bio))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 break;
2623
Jens Axboe2056a782006-03-23 20:00:26 +01002624 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
2625
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 req->biotail->bi_next = bio;
2627 req->biotail = bio;
2628 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
Jens Axboe22e2c502005-06-27 10:55:12 +02002629 req->ioprio = ioprio_best(req->ioprio, prio);
Jerome Marchandb238b3d2007-10-23 15:05:46 +02002630 drive_stat_acct(req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 if (!attempt_back_merge(q, req))
Jens Axboe2e662b62006-07-13 11:55:04 +02002632 elv_merged_request(q, req, el_ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 goto out;
2634
2635 case ELEVATOR_FRONT_MERGE:
2636 BUG_ON(!rq_mergeable(req));
2637
Jens Axboe1aa4f242006-12-19 08:33:11 +01002638 if (!ll_front_merge_fn(q, req, bio))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 break;
2640
Jens Axboe2056a782006-03-23 20:00:26 +01002641 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
2642
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 bio->bi_next = req->bio;
2644 req->bio = bio;
2645
2646 /*
2647 * may not be valid. if the low level driver said
2648 * it didn't need a bounce buffer then it better
2649 * not touch req->buffer either...
2650 */
2651 req->buffer = bio_data(bio);
Jens Axboe51da90f2006-07-18 04:14:45 +02002652 req->current_nr_sectors = bio_cur_sectors(bio);
2653 req->hard_cur_sectors = req->current_nr_sectors;
2654 req->sector = req->hard_sector = bio->bi_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
Jens Axboe22e2c502005-06-27 10:55:12 +02002656 req->ioprio = ioprio_best(req->ioprio, prio);
Jerome Marchandb238b3d2007-10-23 15:05:46 +02002657 drive_stat_acct(req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 if (!attempt_front_merge(q, req))
Jens Axboe2e662b62006-07-13 11:55:04 +02002659 elv_merged_request(q, req, el_ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 goto out;
2661
Nick Piggin450991b2005-06-28 20:45:13 -07002662 /* ELV_NO_MERGE: elevator says don't/can't merge. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 default:
Nick Piggin450991b2005-06-28 20:45:13 -07002664 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 }
2666
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667get_rq:
Nick Piggin450991b2005-06-28 20:45:13 -07002668 /*
Jens Axboe7749a8d2006-12-13 13:02:26 +01002669 * This sync check and mask will be re-done in init_request_from_bio(),
2670 * but we need to set it earlier to expose the sync flag to the
2671 * rq allocator and io schedulers.
2672 */
2673 rw_flags = bio_data_dir(bio);
2674 if (sync)
2675 rw_flags |= REQ_RW_SYNC;
2676
2677 /*
Nick Piggin450991b2005-06-28 20:45:13 -07002678 * Grab a free request. This is might sleep but can not fail.
Nick Piggind6344532005-06-28 20:45:14 -07002679 * Returns with the queue unlocked.
Nick Piggin450991b2005-06-28 20:45:13 -07002680 */
Jens Axboe7749a8d2006-12-13 13:02:26 +01002681 req = get_request_wait(q, rw_flags, bio);
Nick Piggind6344532005-06-28 20:45:14 -07002682
Nick Piggin450991b2005-06-28 20:45:13 -07002683 /*
2684 * After dropping the lock and possibly sleeping here, our request
2685 * may now be mergeable after it had proven unmergeable (above).
2686 * We don't worry about that case for efficiency. It won't happen
2687 * often, and the elevators are able to handle it.
2688 */
Tejun Heo52d9e672006-01-06 09:49:58 +01002689 init_request_from_bio(req, bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
Nick Piggin450991b2005-06-28 20:45:13 -07002691 spin_lock_irq(q->queue_lock);
2692 if (elv_queue_empty(q))
2693 blk_plug_device(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 add_request(q, req);
2695out:
Jens Axboe4a534f92005-04-16 15:25:40 -07002696 if (sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 __generic_unplug_device(q);
2698
2699 spin_unlock_irq(q->queue_lock);
2700 return 0;
2701
2702end_io:
NeilBrown6712ecf2007-09-27 12:47:43 +02002703 bio_endio(bio, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 return 0;
2705}
2706
2707/*
2708 * If bio->bi_dev is a partition, remap the location
2709 */
2710static inline void blk_partition_remap(struct bio *bio)
2711{
2712 struct block_device *bdev = bio->bi_bdev;
2713
Jens Axboebf2de6f2007-09-27 13:01:25 +02002714 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 struct hd_struct *p = bdev->bd_part;
Jens Axboea3623572005-11-01 09:26:16 +01002716 const int rw = bio_data_dir(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
Jens Axboea3623572005-11-01 09:26:16 +01002718 p->sectors[rw] += bio_sectors(bio);
2719 p->ios[rw]++;
2720
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 bio->bi_sector += p->start_sect;
2722 bio->bi_bdev = bdev->bd_contains;
Alan D. Brunellec7149d62007-08-07 15:30:23 +02002723
2724 blk_add_trace_remap(bdev_get_queue(bio->bi_bdev), bio,
2725 bdev->bd_dev, bio->bi_sector,
2726 bio->bi_sector - p->start_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 }
2728}
2729
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730static void handle_bad_sector(struct bio *bio)
2731{
2732 char b[BDEVNAME_SIZE];
2733
2734 printk(KERN_INFO "attempt to access beyond end of device\n");
2735 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
2736 bdevname(bio->bi_bdev, b),
2737 bio->bi_rw,
2738 (unsigned long long)bio->bi_sector + bio_sectors(bio),
2739 (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
2740
2741 set_bit(BIO_EOF, &bio->bi_flags);
2742}
2743
Akinobu Mitac17bb492006-12-08 02:39:46 -08002744#ifdef CONFIG_FAIL_MAKE_REQUEST
2745
2746static DECLARE_FAULT_ATTR(fail_make_request);
2747
2748static int __init setup_fail_make_request(char *str)
2749{
2750 return setup_fault_attr(&fail_make_request, str);
2751}
2752__setup("fail_make_request=", setup_fail_make_request);
2753
2754static int should_fail_request(struct bio *bio)
2755{
2756 if ((bio->bi_bdev->bd_disk->flags & GENHD_FL_FAIL) ||
2757 (bio->bi_bdev->bd_part && bio->bi_bdev->bd_part->make_it_fail))
2758 return should_fail(&fail_make_request, bio->bi_size);
2759
2760 return 0;
2761}
2762
2763static int __init fail_make_request_debugfs(void)
2764{
2765 return init_fault_attr_dentries(&fail_make_request,
2766 "fail_make_request");
2767}
2768
2769late_initcall(fail_make_request_debugfs);
2770
2771#else /* CONFIG_FAIL_MAKE_REQUEST */
2772
2773static inline int should_fail_request(struct bio *bio)
2774{
2775 return 0;
2776}
2777
2778#endif /* CONFIG_FAIL_MAKE_REQUEST */
2779
Jens Axboec07e2b42007-07-18 13:27:58 +02002780/*
2781 * Check whether this bio extends beyond the end of the device.
2782 */
2783static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
2784{
2785 sector_t maxsector;
2786
2787 if (!nr_sectors)
2788 return 0;
2789
2790 /* Test device or partition size, when known. */
2791 maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
2792 if (maxsector) {
2793 sector_t sector = bio->bi_sector;
2794
2795 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
2796 /*
2797 * This may well happen - the kernel calls bread()
2798 * without checking the size of the device, e.g., when
2799 * mounting a device.
2800 */
2801 handle_bad_sector(bio);
2802 return 1;
2803 }
2804 }
2805
2806 return 0;
2807}
2808
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809/**
2810 * generic_make_request: hand a buffer to its device driver for I/O
2811 * @bio: The bio describing the location in memory and on the device.
2812 *
2813 * generic_make_request() is used to make I/O requests of block
2814 * devices. It is passed a &struct bio, which describes the I/O that needs
2815 * to be done.
2816 *
2817 * generic_make_request() does not return any status. The
2818 * success/failure status of the request, along with notification of
2819 * completion, is delivered asynchronously through the bio->bi_end_io
2820 * function described (one day) else where.
2821 *
2822 * The caller of generic_make_request must make sure that bi_io_vec
2823 * are set to describe the memory buffer, and that bi_dev and bi_sector are
2824 * set to describe the device address, and the
2825 * bi_end_io and optionally bi_private are set to describe how
2826 * completion notification should be signaled.
2827 *
2828 * generic_make_request and the drivers it calls may use bi_next if this
2829 * bio happens to be merged with someone else, and may change bi_dev and
2830 * bi_sector for remaps as it sees fit. So the values of these fields
2831 * should NOT be depended on after the call to generic_make_request.
2832 */
Neil Brownd89d8792007-05-01 09:53:42 +02002833static inline void __generic_make_request(struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834{
Jens Axboe165125e2007-07-24 09:28:11 +02002835 struct request_queue *q;
NeilBrown5ddfe962006-10-30 22:07:21 -08002836 sector_t old_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 int ret, nr_sectors = bio_sectors(bio);
Jens Axboe2056a782006-03-23 20:00:26 +01002838 dev_t old_dev;
Jens Axboe51fd77b2007-11-02 08:49:08 +01002839 int err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
2841 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
Jens Axboec07e2b42007-07-18 13:27:58 +02002843 if (bio_check_eod(bio, nr_sectors))
2844 goto end_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
2846 /*
2847 * Resolve the mapping until finished. (drivers are
2848 * still free to implement/resolve their own stacking
2849 * by explicitly returning 0)
2850 *
2851 * NOTE: we don't repeat the blk_size check for each new device.
2852 * Stacking drivers are expected to know what they are doing.
2853 */
NeilBrown5ddfe962006-10-30 22:07:21 -08002854 old_sector = -1;
Jens Axboe2056a782006-03-23 20:00:26 +01002855 old_dev = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 do {
2857 char b[BDEVNAME_SIZE];
2858
2859 q = bdev_get_queue(bio->bi_bdev);
2860 if (!q) {
2861 printk(KERN_ERR
2862 "generic_make_request: Trying to access "
2863 "nonexistent block-device %s (%Lu)\n",
2864 bdevname(bio->bi_bdev, b),
2865 (long long) bio->bi_sector);
2866end_io:
Jens Axboe51fd77b2007-11-02 08:49:08 +01002867 bio_endio(bio, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 break;
2869 }
2870
Jens Axboe4fa253f2007-07-18 13:13:10 +02002871 if (unlikely(nr_sectors > q->max_hw_sectors)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 printk("bio too big device %s (%u > %u)\n",
2873 bdevname(bio->bi_bdev, b),
2874 bio_sectors(bio),
2875 q->max_hw_sectors);
2876 goto end_io;
2877 }
2878
Nick Pigginfde6ad22005-06-23 00:08:53 -07002879 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 goto end_io;
2881
Akinobu Mitac17bb492006-12-08 02:39:46 -08002882 if (should_fail_request(bio))
2883 goto end_io;
2884
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 /*
2886 * If this device has partitions, remap block n
2887 * of partition p to block n+start(p) of the disk.
2888 */
2889 blk_partition_remap(bio);
2890
NeilBrown5ddfe962006-10-30 22:07:21 -08002891 if (old_sector != -1)
Jens Axboe4fa253f2007-07-18 13:13:10 +02002892 blk_add_trace_remap(q, bio, old_dev, bio->bi_sector,
NeilBrown5ddfe962006-10-30 22:07:21 -08002893 old_sector);
Jens Axboe2056a782006-03-23 20:00:26 +01002894
2895 blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
2896
NeilBrown5ddfe962006-10-30 22:07:21 -08002897 old_sector = bio->bi_sector;
Jens Axboe2056a782006-03-23 20:00:26 +01002898 old_dev = bio->bi_bdev->bd_dev;
2899
Jens Axboec07e2b42007-07-18 13:27:58 +02002900 if (bio_check_eod(bio, nr_sectors))
2901 goto end_io;
Jens Axboe51fd77b2007-11-02 08:49:08 +01002902 if (bio_empty_barrier(bio) && !q->prepare_flush_fn) {
2903 err = -EOPNOTSUPP;
2904 goto end_io;
2905 }
NeilBrown5ddfe962006-10-30 22:07:21 -08002906
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 ret = q->make_request_fn(q, bio);
2908 } while (ret);
2909}
2910
Neil Brownd89d8792007-05-01 09:53:42 +02002911/*
2912 * We only want one ->make_request_fn to be active at a time,
2913 * else stack usage with stacked devices could be a problem.
2914 * So use current->bio_{list,tail} to keep a list of requests
2915 * submited by a make_request_fn function.
2916 * current->bio_tail is also used as a flag to say if
2917 * generic_make_request is currently active in this task or not.
2918 * If it is NULL, then no make_request is active. If it is non-NULL,
2919 * then a make_request is active, and new requests should be added
2920 * at the tail
2921 */
2922void generic_make_request(struct bio *bio)
2923{
2924 if (current->bio_tail) {
2925 /* make_request is active */
2926 *(current->bio_tail) = bio;
2927 bio->bi_next = NULL;
2928 current->bio_tail = &bio->bi_next;
2929 return;
2930 }
2931 /* following loop may be a bit non-obvious, and so deserves some
2932 * explanation.
2933 * Before entering the loop, bio->bi_next is NULL (as all callers
2934 * ensure that) so we have a list with a single bio.
2935 * We pretend that we have just taken it off a longer list, so
2936 * we assign bio_list to the next (which is NULL) and bio_tail
2937 * to &bio_list, thus initialising the bio_list of new bios to be
2938 * added. __generic_make_request may indeed add some more bios
2939 * through a recursive call to generic_make_request. If it
2940 * did, we find a non-NULL value in bio_list and re-enter the loop
2941 * from the top. In this case we really did just take the bio
2942 * of the top of the list (no pretending) and so fixup bio_list and
2943 * bio_tail or bi_next, and call into __generic_make_request again.
2944 *
2945 * The loop was structured like this to make only one call to
2946 * __generic_make_request (which is important as it is large and
2947 * inlined) and to keep the structure simple.
2948 */
2949 BUG_ON(bio->bi_next);
2950 do {
2951 current->bio_list = bio->bi_next;
2952 if (bio->bi_next == NULL)
2953 current->bio_tail = &current->bio_list;
2954 else
2955 bio->bi_next = NULL;
2956 __generic_make_request(bio);
2957 bio = current->bio_list;
2958 } while (bio);
2959 current->bio_tail = NULL; /* deactivate */
2960}
2961
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962EXPORT_SYMBOL(generic_make_request);
2963
2964/**
2965 * submit_bio: submit a bio to the block device layer for I/O
2966 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
2967 * @bio: The &struct bio which describes the I/O
2968 *
2969 * submit_bio() is very similar in purpose to generic_make_request(), and
2970 * uses that function to do most of the work. Both are fairly rough
2971 * interfaces, @bio must be presetup and ready for I/O.
2972 *
2973 */
2974void submit_bio(int rw, struct bio *bio)
2975{
2976 int count = bio_sectors(bio);
2977
Jens Axboe22e2c502005-06-27 10:55:12 +02002978 bio->bi_rw |= rw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979
Jens Axboebf2de6f2007-09-27 13:01:25 +02002980 /*
2981 * If it's a regular read/write or a barrier with data attached,
2982 * go through the normal accounting stuff before submission.
2983 */
2984 if (!bio_empty_barrier(bio)) {
2985
2986 BIO_BUG_ON(!bio->bi_size);
2987 BIO_BUG_ON(!bio->bi_io_vec);
2988
2989 if (rw & WRITE) {
2990 count_vm_events(PGPGOUT, count);
2991 } else {
2992 task_io_account_read(bio->bi_size);
2993 count_vm_events(PGPGIN, count);
2994 }
2995
2996 if (unlikely(block_dump)) {
2997 char b[BDEVNAME_SIZE];
2998 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002999 current->comm, task_pid_nr(current),
Jens Axboebf2de6f2007-09-27 13:01:25 +02003000 (rw & WRITE) ? "WRITE" : "READ",
3001 (unsigned long long)bio->bi_sector,
3002 bdevname(bio->bi_bdev,b));
3003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 }
3005
3006 generic_make_request(bio);
3007}
3008
3009EXPORT_SYMBOL(submit_bio);
3010
Adrian Bunk93d17d32005-06-25 14:59:10 -07003011static void blk_recalc_rq_sectors(struct request *rq, int nsect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012{
3013 if (blk_fs_request(rq)) {
3014 rq->hard_sector += nsect;
3015 rq->hard_nr_sectors -= nsect;
3016
3017 /*
3018 * Move the I/O submission pointers ahead if required.
3019 */
3020 if ((rq->nr_sectors >= rq->hard_nr_sectors) &&
3021 (rq->sector <= rq->hard_sector)) {
3022 rq->sector = rq->hard_sector;
3023 rq->nr_sectors = rq->hard_nr_sectors;
3024 rq->hard_cur_sectors = bio_cur_sectors(rq->bio);
3025 rq->current_nr_sectors = rq->hard_cur_sectors;
3026 rq->buffer = bio_data(rq->bio);
3027 }
3028
3029 /*
3030 * if total number of sectors is less than the first segment
3031 * size, something has gone terribly wrong
3032 */
3033 if (rq->nr_sectors < rq->current_nr_sectors) {
3034 printk("blk: request botched\n");
3035 rq->nr_sectors = rq->current_nr_sectors;
3036 }
3037 }
3038}
3039
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05003040/**
3041 * __end_that_request_first - end I/O on a request
3042 * @req: the request being processed
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05003043 * @error: 0 for success, < 0 for error
Kiyoshi Ueda3bcddea2007-12-11 17:52:28 -05003044 * @nr_bytes: number of bytes to complete
3045 *
3046 * Description:
3047 * Ends I/O on a number of bytes attached to @req, and sets it up
3048 * for the next range of segments (if any) in the cluster.
3049 *
3050 * Return:
3051 * 0 - we are done with this request, call end_that_request_last()
3052 * 1 - still buffers pending for this request
3053 **/
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05003054static int __end_that_request_first(struct request *req, int error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 int nr_bytes)
3056{
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05003057 int total_bytes, bio_nbytes, next_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 struct bio *bio;
3059
Jens Axboe2056a782006-03-23 20:00:26 +01003060 blk_add_trace_rq(req->q, req, BLK_TA_COMPLETE);
3061
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 * for a REQ_BLOCK_PC request, we want to carry any eventual
3064 * sense key with us all the way through
3065 */
3066 if (!blk_pc_request(req))
3067 req->errors = 0;
3068
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05003069 if (error) {
Jens Axboe4aff5e22006-08-10 08:44:47 +02003070 if (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 printk("end_request: I/O error, dev %s, sector %llu\n",
3072 req->rq_disk ? req->rq_disk->disk_name : "?",
3073 (unsigned long long)req->sector);
3074 }
3075
Jens Axboed72d9042005-11-01 08:35:42 +01003076 if (blk_fs_request(req) && req->rq_disk) {
Jens Axboea3623572005-11-01 09:26:16 +01003077 const int rw = rq_data_dir(req);
3078
Jens Axboe53e86062006-01-17 11:09:27 +01003079 disk_stat_add(req->rq_disk, sectors[rw], nr_bytes >> 9);
Jens Axboed72d9042005-11-01 08:35:42 +01003080 }
3081
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 total_bytes = bio_nbytes = 0;
3083 while ((bio = req->bio) != NULL) {
3084 int nbytes;
3085
Jens Axboebf2de6f2007-09-27 13:01:25 +02003086 /*
3087 * For an empty barrier request, the low level driver must
3088 * store a potential error location in ->sector. We pass
3089 * that back up in ->bi_sector.
3090 */
3091 if (blk_empty_barrier(req))
3092 bio->bi_sector = req->sector;
3093
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 if (nr_bytes >= bio->bi_size) {
3095 req->bio = bio->bi_next;
3096 nbytes = bio->bi_size;
NeilBrown5bb23a62007-09-27 12:46:13 +02003097 req_bio_endio(req, bio, nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 next_idx = 0;
3099 bio_nbytes = 0;
3100 } else {
3101 int idx = bio->bi_idx + next_idx;
3102
3103 if (unlikely(bio->bi_idx >= bio->bi_vcnt)) {
3104 blk_dump_rq_flags(req, "__end_that");
3105 printk("%s: bio idx %d >= vcnt %d\n",
3106 __FUNCTION__,
3107 bio->bi_idx, bio->bi_vcnt);
3108 break;
3109 }
3110
3111 nbytes = bio_iovec_idx(bio, idx)->bv_len;
3112 BIO_BUG_ON(nbytes > bio->bi_size);
3113
3114 /*
3115 * not a complete bvec done
3116 */
3117 if (unlikely(nbytes > nr_bytes)) {
3118 bio_nbytes += nr_bytes;
3119 total_bytes += nr_bytes;
3120 break;
3121 }
3122
3123 /*
3124 * advance to the next vector
3125 */
3126 next_idx++;
3127 bio_nbytes += nbytes;
3128 }
3129
3130 total_bytes += nbytes;
3131 nr_bytes -= nbytes;
3132
3133 if ((bio = req->bio)) {
3134 /*
3135 * end more in this run, or just return 'not-done'
3136 */
3137 if (unlikely(nr_bytes <= 0))
3138 break;
3139 }
3140 }
3141
3142 /*
3143 * completely done
3144 */
3145 if (!req->bio)
3146 return 0;
3147
3148 /*
3149 * if the request wasn't completed, update state
3150 */
3151 if (bio_nbytes) {
NeilBrown5bb23a62007-09-27 12:46:13 +02003152 req_bio_endio(req, bio, bio_nbytes, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 bio->bi_idx += next_idx;
3154 bio_iovec(bio)->bv_offset += nr_bytes;
3155 bio_iovec(bio)->bv_len -= nr_bytes;
3156 }
3157
3158 blk_recalc_rq_sectors(req, total_bytes >> 9);
3159 blk_recalc_rq_segments(req);
3160 return 1;
3161}
3162
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163/*
Jens Axboeff856ba2006-01-09 16:02:34 +01003164 * splice the completion data to a local structure and hand off to
3165 * process_completion_queue() to complete the requests
3166 */
3167static void blk_done_softirq(struct softirq_action *h)
3168{
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07003169 struct list_head *cpu_list, local_list;
Jens Axboeff856ba2006-01-09 16:02:34 +01003170
3171 local_irq_disable();
3172 cpu_list = &__get_cpu_var(blk_cpu_done);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -07003173 list_replace_init(cpu_list, &local_list);
Jens Axboeff856ba2006-01-09 16:02:34 +01003174 local_irq_enable();
3175
3176 while (!list_empty(&local_list)) {
3177 struct request *rq = list_entry(local_list.next, struct request, donelist);
3178
3179 list_del_init(&rq->donelist);
3180 rq->q->softirq_done_fn(rq);
3181 }
3182}
3183
Satyam Sharmadb47d472007-08-23 09:29:40 +02003184static int __cpuinit blk_cpu_notify(struct notifier_block *self, unsigned long action,
Jens Axboeff856ba2006-01-09 16:02:34 +01003185 void *hcpu)
3186{
3187 /*
3188 * If a CPU goes away, splice its entries to the current CPU
3189 * and trigger a run of the softirq
3190 */
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07003191 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
Jens Axboeff856ba2006-01-09 16:02:34 +01003192 int cpu = (unsigned long) hcpu;
3193
3194 local_irq_disable();
3195 list_splice_init(&per_cpu(blk_cpu_done, cpu),
3196 &__get_cpu_var(blk_cpu_done));
3197 raise_softirq_irqoff(BLOCK_SOFTIRQ);
3198 local_irq_enable();
3199 }
3200
3201 return NOTIFY_OK;
3202}
3203
3204
Satyam Sharmadb47d472007-08-23 09:29:40 +02003205static struct notifier_block blk_cpu_notifier __cpuinitdata = {
Jens Axboeff856ba2006-01-09 16:02:34 +01003206 .notifier_call = blk_cpu_notify,
3207};
3208
Jens Axboeff856ba2006-01-09 16:02:34 +01003209/**
3210 * blk_complete_request - end I/O on a request
3211 * @req: the request being processed
3212 *
3213 * Description:
3214 * Ends all I/O on a request. It does not handle partial completions,
Andreas Mohrd6e05ed2006-06-26 18:35:02 +02003215 * unless the driver actually implements this in its completion callback
Jens Axboe4fa253f2007-07-18 13:13:10 +02003216 * through requeueing. The actual completion happens out-of-order,
Jens Axboeff856ba2006-01-09 16:02:34 +01003217 * through a softirq handler. The user must have registered a completion
3218 * callback through blk_queue_softirq_done().
3219 **/
3220
3221void blk_complete_request(struct request *req)
3222{
3223 struct list_head *cpu_list;
3224 unsigned long flags;
3225
3226 BUG_ON(!req->q->softirq_done_fn);
3227
3228 local_irq_save(flags);
3229
3230 cpu_list = &__get_cpu_var(blk_cpu_done);
3231 list_add_tail(&req->donelist, cpu_list);
3232 raise_softirq_irqoff(BLOCK_SOFTIRQ);
3233
3234 local_irq_restore(flags);
3235}
3236
3237EXPORT_SYMBOL(blk_complete_request);
3238
3239/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 * queue lock must be held
3241 */
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05003242static void end_that_request_last(struct request *req, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243{
3244 struct gendisk *disk = req->rq_disk;
Tejun Heo8ffdc652006-01-06 09:49:03 +01003245
Kiyoshi Uedab8286232007-12-11 17:53:24 -05003246 if (blk_rq_tagged(req))
3247 blk_queue_end_tag(req->q, req);
3248
3249 if (blk_queued_rq(req))
3250 blkdev_dequeue_request(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
3252 if (unlikely(laptop_mode) && blk_fs_request(req))
3253 laptop_io_completion();
3254
Jens Axboefd0ff8a2006-05-23 11:23:49 +02003255 /*
3256 * Account IO completion. bar_rq isn't accounted as a normal
3257 * IO on queueing nor completion. Accounting the containing
3258 * request is enough.
3259 */
3260 if (disk && blk_fs_request(req) && req != &req->q->bar_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 unsigned long duration = jiffies - req->start_time;
Jens Axboea3623572005-11-01 09:26:16 +01003262 const int rw = rq_data_dir(req);
3263
3264 __disk_stat_inc(disk, ios[rw]);
3265 __disk_stat_add(disk, ticks[rw], duration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 disk_round_stats(disk);
3267 disk->in_flight--;
3268 }
Kiyoshi Uedab8286232007-12-11 17:53:24 -05003269
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 if (req->end_io)
Tejun Heo8ffdc652006-01-06 09:49:03 +01003271 req->end_io(req, error);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05003272 else {
3273 if (blk_bidi_rq(req))
3274 __blk_put_request(req->next_rq->q, req->next_rq);
3275
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 __blk_put_request(req->q, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 }
3278}
3279
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280static inline void __end_request(struct request *rq, int uptodate,
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05003281 unsigned int nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282{
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05003283 int error = 0;
3284
3285 if (uptodate <= 0)
3286 error = uptodate ? uptodate : -EIO;
3287
3288 __blk_end_request(rq, error, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289}
3290
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05003291/**
3292 * blk_rq_bytes - Returns bytes left to complete in the entire request
3293 **/
3294unsigned int blk_rq_bytes(struct request *rq)
Jens Axboea0cd1282007-09-21 10:41:07 +02003295{
3296 if (blk_fs_request(rq))
3297 return rq->hard_nr_sectors << 9;
3298
3299 return rq->data_len;
3300}
Kiyoshi Ueda3b113132007-12-11 17:41:17 -05003301EXPORT_SYMBOL_GPL(blk_rq_bytes);
3302
3303/**
3304 * blk_rq_cur_bytes - Returns bytes left to complete in the current segment
3305 **/
3306unsigned int blk_rq_cur_bytes(struct request *rq)
3307{
3308 if (blk_fs_request(rq))
3309 return rq->current_nr_sectors << 9;
3310
3311 if (rq->bio)
3312 return rq->bio->bi_size;
3313
3314 return rq->data_len;
3315}
3316EXPORT_SYMBOL_GPL(blk_rq_cur_bytes);
Jens Axboea0cd1282007-09-21 10:41:07 +02003317
3318/**
3319 * end_queued_request - end all I/O on a queued request
3320 * @rq: the request being processed
3321 * @uptodate: error value or 0/1 uptodate flag
3322 *
3323 * Description:
3324 * Ends all I/O on a request, and removes it from the block layer queues.
3325 * Not suitable for normal IO completion, unless the driver still has
3326 * the request attached to the block layer.
3327 *
3328 **/
3329void end_queued_request(struct request *rq, int uptodate)
3330{
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05003331 __end_request(rq, uptodate, blk_rq_bytes(rq));
Jens Axboea0cd1282007-09-21 10:41:07 +02003332}
3333EXPORT_SYMBOL(end_queued_request);
3334
3335/**
3336 * end_dequeued_request - end all I/O on a dequeued request
3337 * @rq: the request being processed
3338 * @uptodate: error value or 0/1 uptodate flag
3339 *
3340 * Description:
3341 * Ends all I/O on a request. The request must already have been
3342 * dequeued using blkdev_dequeue_request(), as is normally the case
3343 * for most drivers.
3344 *
3345 **/
3346void end_dequeued_request(struct request *rq, int uptodate)
3347{
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05003348 __end_request(rq, uptodate, blk_rq_bytes(rq));
Jens Axboea0cd1282007-09-21 10:41:07 +02003349}
3350EXPORT_SYMBOL(end_dequeued_request);
3351
3352
3353/**
3354 * end_request - end I/O on the current segment of the request
Randy Dunlap8f731f72007-10-18 23:39:28 -07003355 * @req: the request being processed
Jens Axboea0cd1282007-09-21 10:41:07 +02003356 * @uptodate: error value or 0/1 uptodate flag
3357 *
3358 * Description:
3359 * Ends I/O on the current segment of a request. If that is the only
3360 * remaining segment, the request is also completed and freed.
3361 *
3362 * This is a remnant of how older block drivers handled IO completions.
3363 * Modern drivers typically end IO on the full request in one go, unless
3364 * they have a residual value to account for. For that case this function
3365 * isn't really useful, unless the residual just happens to be the
3366 * full current segment. In other words, don't use this function in new
3367 * code. Either use end_request_completely(), or the
3368 * end_that_request_chunk() (along with end_that_request_last()) for
3369 * partial completions.
3370 *
3371 **/
3372void end_request(struct request *req, int uptodate)
3373{
Kiyoshi Ueda9e6e39f2007-12-11 17:41:54 -05003374 __end_request(req, uptodate, req->hard_cur_sectors << 9);
Jens Axboea0cd1282007-09-21 10:41:07 +02003375}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376EXPORT_SYMBOL(end_request);
3377
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05003378/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05003379 * blk_end_io - Generic end_io function to complete a request.
3380 * @rq: the request being processed
3381 * @error: 0 for success, < 0 for error
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05003382 * @nr_bytes: number of bytes to complete @rq
3383 * @bidi_bytes: number of bytes to complete @rq->next_rq
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05003384 * @drv_callback: function called between completion of bios in the request
3385 * and completion of the request.
3386 * If the callback returns non 0, this helper returns without
3387 * completion of the request.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05003388 *
3389 * Description:
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05003390 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05003391 * If @rq has leftover, sets it up for the next range of segments.
3392 *
3393 * Return:
3394 * 0 - we are done with this request
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05003395 * 1 - this request is not freed yet, it still has pending buffers.
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05003396 **/
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05003397static int blk_end_io(struct request *rq, int error, int nr_bytes,
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05003398 int bidi_bytes, int (drv_callback)(struct request *))
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05003399{
3400 struct request_queue *q = rq->q;
3401 unsigned long flags = 0UL;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05003402
3403 if (blk_fs_request(rq) || blk_pc_request(rq)) {
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05003404 if (__end_that_request_first(rq, error, nr_bytes))
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05003405 return 1;
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05003406
3407 /* Bidi request must be completed as a whole */
3408 if (blk_bidi_rq(rq) &&
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05003409 __end_that_request_first(rq->next_rq, error, bidi_bytes))
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05003410 return 1;
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05003411 }
3412
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05003413 /* Special feature for tricky drivers */
3414 if (drv_callback && drv_callback(rq))
3415 return 1;
3416
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05003417 add_disk_randomness(rq->rq_disk);
3418
3419 spin_lock_irqsave(q->queue_lock, flags);
Kiyoshi Uedab8286232007-12-11 17:53:24 -05003420 end_that_request_last(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05003421 spin_unlock_irqrestore(q->queue_lock, flags);
3422
3423 return 0;
3424}
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05003425
3426/**
3427 * blk_end_request - Helper function for drivers to complete the request.
3428 * @rq: the request being processed
3429 * @error: 0 for success, < 0 for error
3430 * @nr_bytes: number of bytes to complete
3431 *
3432 * Description:
3433 * Ends I/O on a number of bytes attached to @rq.
3434 * If @rq has leftover, sets it up for the next range of segments.
3435 *
3436 * Return:
3437 * 0 - we are done with this request
3438 * 1 - still buffers pending for this request
3439 **/
3440int blk_end_request(struct request *rq, int error, int nr_bytes)
3441{
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05003442 return blk_end_io(rq, error, nr_bytes, 0, NULL);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05003443}
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05003444EXPORT_SYMBOL_GPL(blk_end_request);
3445
3446/**
3447 * __blk_end_request - Helper function for drivers to complete the request.
3448 * @rq: the request being processed
3449 * @error: 0 for success, < 0 for error
3450 * @nr_bytes: number of bytes to complete
3451 *
3452 * Description:
3453 * Must be called with queue lock held unlike blk_end_request().
3454 *
3455 * Return:
3456 * 0 - we are done with this request
3457 * 1 - still buffers pending for this request
3458 **/
3459int __blk_end_request(struct request *rq, int error, int nr_bytes)
3460{
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05003461 if (blk_fs_request(rq) || blk_pc_request(rq)) {
Kiyoshi Ueda5450d3e2007-12-11 17:53:03 -05003462 if (__end_that_request_first(rq, error, nr_bytes))
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05003463 return 1;
3464 }
3465
3466 add_disk_randomness(rq->rq_disk);
3467
Kiyoshi Uedab8286232007-12-11 17:53:24 -05003468 end_that_request_last(rq, error);
Kiyoshi Ueda336cdb42007-12-11 17:40:30 -05003469
3470 return 0;
3471}
3472EXPORT_SYMBOL_GPL(__blk_end_request);
3473
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05003474/**
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05003475 * blk_end_bidi_request - Helper function for drivers to complete bidi request.
3476 * @rq: the bidi request being processed
3477 * @error: 0 for success, < 0 for error
3478 * @nr_bytes: number of bytes to complete @rq
3479 * @bidi_bytes: number of bytes to complete @rq->next_rq
3480 *
3481 * Description:
3482 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
3483 *
3484 * Return:
3485 * 0 - we are done with this request
3486 * 1 - still buffers pending for this request
3487 **/
3488int blk_end_bidi_request(struct request *rq, int error, int nr_bytes,
3489 int bidi_bytes)
3490{
3491 return blk_end_io(rq, error, nr_bytes, bidi_bytes, NULL);
3492}
3493EXPORT_SYMBOL_GPL(blk_end_bidi_request);
3494
3495/**
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05003496 * blk_end_request_callback - Special helper function for tricky drivers
3497 * @rq: the request being processed
3498 * @error: 0 for success, < 0 for error
3499 * @nr_bytes: number of bytes to complete
3500 * @drv_callback: function called between completion of bios in the request
3501 * and completion of the request.
3502 * If the callback returns non 0, this helper returns without
3503 * completion of the request.
3504 *
3505 * Description:
3506 * Ends I/O on a number of bytes attached to @rq.
3507 * If @rq has leftover, sets it up for the next range of segments.
3508 *
3509 * This special helper function is used only for existing tricky drivers.
3510 * (e.g. cdrom_newpc_intr() of ide-cd)
3511 * This interface will be removed when such drivers are rewritten.
3512 * Don't use this interface in other places anymore.
3513 *
3514 * Return:
3515 * 0 - we are done with this request
3516 * 1 - this request is not freed yet.
3517 * this request still has pending buffers or
3518 * the driver doesn't want to finish this request yet.
3519 **/
3520int blk_end_request_callback(struct request *rq, int error, int nr_bytes,
3521 int (drv_callback)(struct request *))
3522{
Kiyoshi Uedae3a04fe2007-12-11 17:51:46 -05003523 return blk_end_io(rq, error, nr_bytes, 0, drv_callback);
Kiyoshi Uedae19a3ab2007-12-11 17:51:02 -05003524}
3525EXPORT_SYMBOL_GPL(blk_end_request_callback);
3526
NeilBrown66846572007-08-16 13:31:28 +02003527static void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
3528 struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529{
Jens Axboe4aff5e22006-08-10 08:44:47 +02003530 /* first two bits are identical in rq->cmd_flags and bio->bi_rw */
3531 rq->cmd_flags |= (bio->bi_rw & 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532
3533 rq->nr_phys_segments = bio_phys_segments(q, bio);
3534 rq->nr_hw_segments = bio_hw_segments(q, bio);
3535 rq->current_nr_sectors = bio_cur_sectors(bio);
3536 rq->hard_cur_sectors = rq->current_nr_sectors;
3537 rq->hard_nr_sectors = rq->nr_sectors = bio_sectors(bio);
3538 rq->buffer = bio_data(bio);
Mike Christie0e75f902006-12-01 10:40:55 +01003539 rq->data_len = bio->bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540
3541 rq->bio = rq->biotail = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542
NeilBrown66846572007-08-16 13:31:28 +02003543 if (bio->bi_bdev)
3544 rq->rq_disk = bio->bi_bdev->bd_disk;
3545}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546
3547int kblockd_schedule_work(struct work_struct *work)
3548{
3549 return queue_work(kblockd_workqueue, work);
3550}
3551
3552EXPORT_SYMBOL(kblockd_schedule_work);
3553
Andrew Morton19a75d82007-05-09 02:33:56 -07003554void kblockd_flush_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555{
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07003556 cancel_work_sync(work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557}
Andrew Morton19a75d82007-05-09 02:33:56 -07003558EXPORT_SYMBOL(kblockd_flush_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
3560int __init blk_dev_init(void)
3561{
Jens Axboeff856ba2006-01-09 16:02:34 +01003562 int i;
3563
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 kblockd_workqueue = create_workqueue("kblockd");
3565 if (!kblockd_workqueue)
3566 panic("Failed to create kblockd\n");
3567
3568 request_cachep = kmem_cache_create("blkdev_requests",
Paul Mundt20c2df82007-07-20 10:11:58 +09003569 sizeof(struct request), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570
Jens Axboe8324aa92008-01-29 14:51:59 +01003571 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
Jens Axboe165125e2007-07-24 09:28:11 +02003572 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573
3574 iocontext_cachep = kmem_cache_create("blkdev_ioc",
Paul Mundt20c2df82007-07-20 10:11:58 +09003575 sizeof(struct io_context), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08003577 for_each_possible_cpu(i)
Jens Axboeff856ba2006-01-09 16:02:34 +01003578 INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i));
3579
3580 open_softirq(BLOCK_SOFTIRQ, blk_done_softirq, NULL);
Chandra Seetharaman5a67e4c2006-06-27 02:54:11 -07003581 register_hotcpu_notifier(&blk_cpu_notifier);
Jens Axboeff856ba2006-01-09 16:02:34 +01003582
Vasily Tarasovf772b3d2007-03-27 08:52:47 +02003583 blk_max_low_pfn = max_low_pfn - 1;
3584 blk_max_pfn = max_pfn - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585
3586 return 0;
3587}
3588
Jens Axboe4ac845a2008-01-24 08:44:49 +01003589static void cfq_dtor(struct io_context *ioc)
3590{
3591 struct cfq_io_context *cic[1];
3592 int r;
3593
3594 /*
3595 * We don't have a specific key to lookup with, so use the gang
3596 * lookup to just retrieve the first item stored. The cfq exit
3597 * function will iterate the full tree, so any member will do.
3598 */
3599 r = radix_tree_gang_lookup(&ioc->radix_root, (void **) cic, 0, 1);
3600 if (r > 0)
3601 cic[0]->dtor(ioc);
3602}
3603
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604/*
Jens Axboed38ecf92008-01-24 08:53:35 +01003605 * IO Context helper functions. put_io_context() returns 1 if there are no
3606 * more users of this io context, 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 */
Jens Axboed38ecf92008-01-24 08:53:35 +01003608int put_io_context(struct io_context *ioc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609{
3610 if (ioc == NULL)
Jens Axboed38ecf92008-01-24 08:53:35 +01003611 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612
3613 BUG_ON(atomic_read(&ioc->refcount) == 0);
3614
3615 if (atomic_dec_and_test(&ioc->refcount)) {
Al Viro334e94d2006-03-18 15:05:53 -05003616 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 if (ioc->aic && ioc->aic->dtor)
3618 ioc->aic->dtor(ioc->aic);
Al Viro334e94d2006-03-18 15:05:53 -05003619 rcu_read_unlock();
Jens Axboe4ac845a2008-01-24 08:44:49 +01003620 cfq_dtor(ioc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621
3622 kmem_cache_free(iocontext_cachep, ioc);
Jens Axboed38ecf92008-01-24 08:53:35 +01003623 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 }
Jens Axboed38ecf92008-01-24 08:53:35 +01003625 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626}
3627EXPORT_SYMBOL(put_io_context);
3628
Jens Axboe4ac845a2008-01-24 08:44:49 +01003629static void cfq_exit(struct io_context *ioc)
3630{
3631 struct cfq_io_context *cic[1];
3632 int r;
3633
3634 rcu_read_lock();
3635 /*
3636 * See comment for cfq_dtor()
3637 */
3638 r = radix_tree_gang_lookup(&ioc->radix_root, (void **) cic, 0, 1);
3639 rcu_read_unlock();
3640
3641 if (r > 0)
3642 cic[0]->exit(ioc);
3643}
3644
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645/* Called by the exitting task */
3646void exit_io_context(void)
3647{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 struct io_context *ioc;
3649
Jens Axboe22e2c502005-06-27 10:55:12 +02003650 task_lock(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 ioc = current->io_context;
3652 current->io_context = NULL;
Jens Axboe22e2c502005-06-27 10:55:12 +02003653 task_unlock(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654
Jens Axboed38ecf92008-01-24 08:53:35 +01003655 if (atomic_dec_and_test(&ioc->nr_tasks)) {
3656 if (ioc->aic && ioc->aic->exit)
3657 ioc->aic->exit(ioc->aic);
Jens Axboe4ac845a2008-01-24 08:44:49 +01003658 cfq_exit(ioc);
Oleg Nesterov25034d72006-08-29 09:15:14 +02003659
Jens Axboed38ecf92008-01-24 08:53:35 +01003660 put_io_context(ioc);
3661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662}
3663
Jens Axboefd0928d2008-01-24 08:52:45 +01003664struct io_context *alloc_io_context(gfp_t gfp_flags, int node)
3665{
3666 struct io_context *ret;
3667
3668 ret = kmem_cache_alloc_node(iocontext_cachep, gfp_flags, node);
3669 if (ret) {
3670 atomic_set(&ret->refcount, 1);
Jens Axboed38ecf92008-01-24 08:53:35 +01003671 atomic_set(&ret->nr_tasks, 1);
3672 spin_lock_init(&ret->lock);
Jens Axboefd0928d2008-01-24 08:52:45 +01003673 ret->ioprio_changed = 0;
3674 ret->ioprio = 0;
3675 ret->last_waited = jiffies; /* doesn't matter... */
3676 ret->nr_batch_requests = 0; /* because this is 0 */
3677 ret->aic = NULL;
Jens Axboe4ac845a2008-01-24 08:44:49 +01003678 INIT_RADIX_TREE(&ret->radix_root, GFP_ATOMIC | __GFP_HIGH);
Jens Axboefd0928d2008-01-24 08:52:45 +01003679 ret->ioc_data = NULL;
3680 }
3681
3682 return ret;
3683}
3684
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685/*
3686 * If the current task has no IO context then create one and initialise it.
Nick Pigginfb3cc432005-06-28 20:45:15 -07003687 * Otherwise, return its existing IO context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 *
Nick Pigginfb3cc432005-06-28 20:45:15 -07003689 * This returned IO context doesn't have a specifically elevated refcount,
3690 * but since the current task itself holds a reference, the context can be
3691 * used in general code, so long as it stays within `current` context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 */
Jens Axboeb5deef92006-07-19 23:39:40 +02003693static struct io_context *current_io_context(gfp_t gfp_flags, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694{
3695 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 struct io_context *ret;
3697
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 ret = tsk->io_context;
Nick Pigginfb3cc432005-06-28 20:45:15 -07003699 if (likely(ret))
3700 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701
Jens Axboefd0928d2008-01-24 08:52:45 +01003702 ret = alloc_io_context(gfp_flags, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 if (ret) {
Oleg Nesterov9f83e452006-08-21 08:34:15 +02003704 /* make sure set_task_ioprio() sees the settings above */
3705 smp_wmb();
Nick Pigginfb3cc432005-06-28 20:45:15 -07003706 tsk->io_context = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 }
3708
3709 return ret;
3710}
Nick Pigginfb3cc432005-06-28 20:45:15 -07003711
3712/*
3713 * If the current task has no IO context then create one and initialise it.
3714 * If it does have a context, take a ref on it.
3715 *
3716 * This is always called in the context of the task which submitted the I/O.
3717 */
Jens Axboeb5deef92006-07-19 23:39:40 +02003718struct io_context *get_io_context(gfp_t gfp_flags, int node)
Nick Pigginfb3cc432005-06-28 20:45:15 -07003719{
Jens Axboed38ecf92008-01-24 08:53:35 +01003720 struct io_context *ret = NULL;
3721
3722 /*
3723 * Check for unlikely race with exiting task. ioc ref count is
3724 * zero when ioc is being detached.
3725 */
3726 do {
3727 ret = current_io_context(gfp_flags, node);
3728 if (unlikely(!ret))
3729 break;
3730 } while (!atomic_inc_not_zero(&ret->refcount));
3731
Nick Pigginfb3cc432005-06-28 20:45:15 -07003732 return ret;
3733}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734EXPORT_SYMBOL(get_io_context);
3735
3736void copy_io_context(struct io_context **pdst, struct io_context **psrc)
3737{
3738 struct io_context *src = *psrc;
3739 struct io_context *dst = *pdst;
3740
3741 if (src) {
3742 BUG_ON(atomic_read(&src->refcount) == 0);
3743 atomic_inc(&src->refcount);
3744 put_io_context(dst);
3745 *pdst = src;
3746 }
3747}
3748EXPORT_SYMBOL(copy_io_context);
3749
3750void swap_io_context(struct io_context **ioc1, struct io_context **ioc2)
3751{
3752 struct io_context *temp;
3753 temp = *ioc1;
3754 *ioc1 = *ioc2;
3755 *ioc2 = temp;
3756}
3757EXPORT_SYMBOL(swap_io_context);
3758