Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
1da177e4 LT |
2 | * 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> | |
6728cb0e JA |
6 | * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au> |
7 | * - July2000 | |
1da177e4 LT |
8 | * bio rewrite, highmem i/o, etc, Jens Axboe <axboe@suse.de> - may 2001 |
9 | */ | |
10 | ||
11 | /* | |
12 | * This handles all read/write requests to block devices | |
13 | */ | |
1da177e4 LT |
14 | #include <linux/kernel.h> |
15 | #include <linux/module.h> | |
16 | #include <linux/backing-dev.h> | |
17 | #include <linux/bio.h> | |
18 | #include <linux/blkdev.h> | |
19 | #include <linux/highmem.h> | |
20 | #include <linux/mm.h> | |
21 | #include <linux/kernel_stat.h> | |
22 | #include <linux/string.h> | |
23 | #include <linux/init.h> | |
1da177e4 LT |
24 | #include <linux/completion.h> |
25 | #include <linux/slab.h> | |
26 | #include <linux/swap.h> | |
27 | #include <linux/writeback.h> | |
faccbd4b | 28 | #include <linux/task_io_accounting_ops.h> |
c17bb495 | 29 | #include <linux/fault-inject.h> |
73c10101 | 30 | #include <linux/list_sort.h> |
e3c78ca5 | 31 | #include <linux/delay.h> |
55782138 LZ |
32 | |
33 | #define CREATE_TRACE_POINTS | |
34 | #include <trace/events/block.h> | |
1da177e4 | 35 | |
8324aa91 | 36 | #include "blk.h" |
5efd6113 | 37 | #include "blk-cgroup.h" |
8324aa91 | 38 | |
d07335e5 | 39 | EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_remap); |
b0da3f0d | 40 | EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap); |
55782138 | 41 | EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete); |
0bfc2455 | 42 | |
a73f730d TH |
43 | DEFINE_IDA(blk_queue_ida); |
44 | ||
1da177e4 LT |
45 | /* |
46 | * For the allocated request tables | |
47 | */ | |
5ece6c52 | 48 | static struct kmem_cache *request_cachep; |
1da177e4 LT |
49 | |
50 | /* | |
51 | * For queue allocation | |
52 | */ | |
6728cb0e | 53 | struct kmem_cache *blk_requestq_cachep; |
1da177e4 | 54 | |
1da177e4 LT |
55 | /* |
56 | * Controlling structure to kblockd | |
57 | */ | |
ff856bad | 58 | static struct workqueue_struct *kblockd_workqueue; |
1da177e4 | 59 | |
26b8256e JA |
60 | static void drive_stat_acct(struct request *rq, int new_io) |
61 | { | |
28f13702 | 62 | struct hd_struct *part; |
26b8256e | 63 | int rw = rq_data_dir(rq); |
c9959059 | 64 | int cpu; |
26b8256e | 65 | |
c2553b58 | 66 | if (!blk_do_io_stat(rq)) |
26b8256e JA |
67 | return; |
68 | ||
074a7aca | 69 | cpu = part_stat_lock(); |
c9959059 | 70 | |
09e099d4 JM |
71 | if (!new_io) { |
72 | part = rq->part; | |
074a7aca | 73 | part_stat_inc(cpu, part, merges[rw]); |
09e099d4 JM |
74 | } else { |
75 | part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq)); | |
6c23a968 | 76 | if (!hd_struct_try_get(part)) { |
09e099d4 JM |
77 | /* |
78 | * The partition is already being removed, | |
79 | * the request will be accounted on the disk only | |
80 | * | |
81 | * We take a reference on disk->part0 although that | |
82 | * partition will never be deleted, so we can treat | |
83 | * it as any other partition. | |
84 | */ | |
85 | part = &rq->rq_disk->part0; | |
6c23a968 | 86 | hd_struct_get(part); |
09e099d4 | 87 | } |
074a7aca | 88 | part_round_stats(cpu, part); |
316d315b | 89 | part_inc_in_flight(part, rw); |
09e099d4 | 90 | rq->part = part; |
26b8256e | 91 | } |
e71bf0d0 | 92 | |
074a7aca | 93 | part_stat_unlock(); |
26b8256e JA |
94 | } |
95 | ||
8324aa91 | 96 | void blk_queue_congestion_threshold(struct request_queue *q) |
1da177e4 LT |
97 | { |
98 | int nr; | |
99 | ||
100 | nr = q->nr_requests - (q->nr_requests / 8) + 1; | |
101 | if (nr > q->nr_requests) | |
102 | nr = q->nr_requests; | |
103 | q->nr_congestion_on = nr; | |
104 | ||
105 | nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1; | |
106 | if (nr < 1) | |
107 | nr = 1; | |
108 | q->nr_congestion_off = nr; | |
109 | } | |
110 | ||
1da177e4 LT |
111 | /** |
112 | * blk_get_backing_dev_info - get the address of a queue's backing_dev_info | |
113 | * @bdev: device | |
114 | * | |
115 | * Locates the passed device's request queue and returns the address of its | |
116 | * backing_dev_info | |
117 | * | |
118 | * Will return NULL if the request queue cannot be located. | |
119 | */ | |
120 | struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev) | |
121 | { | |
122 | struct backing_dev_info *ret = NULL; | |
165125e1 | 123 | struct request_queue *q = bdev_get_queue(bdev); |
1da177e4 LT |
124 | |
125 | if (q) | |
126 | ret = &q->backing_dev_info; | |
127 | return ret; | |
128 | } | |
1da177e4 LT |
129 | EXPORT_SYMBOL(blk_get_backing_dev_info); |
130 | ||
2a4aa30c | 131 | void blk_rq_init(struct request_queue *q, struct request *rq) |
1da177e4 | 132 | { |
1afb20f3 FT |
133 | memset(rq, 0, sizeof(*rq)); |
134 | ||
1da177e4 | 135 | INIT_LIST_HEAD(&rq->queuelist); |
242f9dcb | 136 | INIT_LIST_HEAD(&rq->timeout_list); |
c7c22e4d | 137 | rq->cpu = -1; |
63a71386 | 138 | rq->q = q; |
a2dec7b3 | 139 | rq->__sector = (sector_t) -1; |
2e662b65 JA |
140 | INIT_HLIST_NODE(&rq->hash); |
141 | RB_CLEAR_NODE(&rq->rb_node); | |
d7e3c324 | 142 | rq->cmd = rq->__cmd; |
e2494e1b | 143 | rq->cmd_len = BLK_MAX_CDB; |
63a71386 | 144 | rq->tag = -1; |
1da177e4 | 145 | rq->ref_count = 1; |
b243ddcb | 146 | rq->start_time = jiffies; |
9195291e | 147 | set_start_time_ns(rq); |
09e099d4 | 148 | rq->part = NULL; |
1da177e4 | 149 | } |
2a4aa30c | 150 | EXPORT_SYMBOL(blk_rq_init); |
1da177e4 | 151 | |
5bb23a68 N |
152 | static void req_bio_endio(struct request *rq, struct bio *bio, |
153 | unsigned int nbytes, int error) | |
1da177e4 | 154 | { |
143a87f4 TH |
155 | if (error) |
156 | clear_bit(BIO_UPTODATE, &bio->bi_flags); | |
157 | else if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) | |
158 | error = -EIO; | |
797e7dbb | 159 | |
143a87f4 TH |
160 | if (unlikely(nbytes > bio->bi_size)) { |
161 | printk(KERN_ERR "%s: want %u bytes done, %u left\n", | |
162 | __func__, nbytes, bio->bi_size); | |
163 | nbytes = bio->bi_size; | |
5bb23a68 | 164 | } |
797e7dbb | 165 | |
143a87f4 TH |
166 | if (unlikely(rq->cmd_flags & REQ_QUIET)) |
167 | set_bit(BIO_QUIET, &bio->bi_flags); | |
08bafc03 | 168 | |
143a87f4 TH |
169 | bio->bi_size -= nbytes; |
170 | bio->bi_sector += (nbytes >> 9); | |
7ba1ba12 | 171 | |
143a87f4 TH |
172 | if (bio_integrity(bio)) |
173 | bio_integrity_advance(bio, nbytes); | |
7ba1ba12 | 174 | |
143a87f4 TH |
175 | /* don't actually finish bio if it's part of flush sequence */ |
176 | if (bio->bi_size == 0 && !(rq->cmd_flags & REQ_FLUSH_SEQ)) | |
177 | bio_endio(bio, error); | |
1da177e4 | 178 | } |
1da177e4 | 179 | |
1da177e4 LT |
180 | void blk_dump_rq_flags(struct request *rq, char *msg) |
181 | { | |
182 | int bit; | |
183 | ||
6728cb0e | 184 | printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg, |
4aff5e23 JA |
185 | rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type, |
186 | rq->cmd_flags); | |
1da177e4 | 187 | |
83096ebf TH |
188 | printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n", |
189 | (unsigned long long)blk_rq_pos(rq), | |
190 | blk_rq_sectors(rq), blk_rq_cur_sectors(rq)); | |
731ec497 | 191 | printk(KERN_INFO " bio %p, biotail %p, buffer %p, len %u\n", |
2e46e8b2 | 192 | rq->bio, rq->biotail, rq->buffer, blk_rq_bytes(rq)); |
1da177e4 | 193 | |
33659ebb | 194 | if (rq->cmd_type == REQ_TYPE_BLOCK_PC) { |
6728cb0e | 195 | printk(KERN_INFO " cdb: "); |
d34c87e4 | 196 | for (bit = 0; bit < BLK_MAX_CDB; bit++) |
1da177e4 LT |
197 | printk("%02x ", rq->cmd[bit]); |
198 | printk("\n"); | |
199 | } | |
200 | } | |
1da177e4 LT |
201 | EXPORT_SYMBOL(blk_dump_rq_flags); |
202 | ||
3cca6dc1 | 203 | static void blk_delay_work(struct work_struct *work) |
1da177e4 | 204 | { |
3cca6dc1 | 205 | struct request_queue *q; |
1da177e4 | 206 | |
3cca6dc1 JA |
207 | q = container_of(work, struct request_queue, delay_work.work); |
208 | spin_lock_irq(q->queue_lock); | |
24ecfbe2 | 209 | __blk_run_queue(q); |
3cca6dc1 | 210 | spin_unlock_irq(q->queue_lock); |
1da177e4 | 211 | } |
1da177e4 LT |
212 | |
213 | /** | |
3cca6dc1 JA |
214 | * blk_delay_queue - restart queueing after defined interval |
215 | * @q: The &struct request_queue in question | |
216 | * @msecs: Delay in msecs | |
1da177e4 LT |
217 | * |
218 | * Description: | |
3cca6dc1 JA |
219 | * Sometimes queueing needs to be postponed for a little while, to allow |
220 | * resources to come back. This function will make sure that queueing is | |
221 | * restarted around the specified time. | |
222 | */ | |
223 | void blk_delay_queue(struct request_queue *q, unsigned long msecs) | |
2ad8b1ef | 224 | { |
4521cc4e JA |
225 | queue_delayed_work(kblockd_workqueue, &q->delay_work, |
226 | msecs_to_jiffies(msecs)); | |
2ad8b1ef | 227 | } |
3cca6dc1 | 228 | EXPORT_SYMBOL(blk_delay_queue); |
2ad8b1ef | 229 | |
1da177e4 LT |
230 | /** |
231 | * blk_start_queue - restart a previously stopped queue | |
165125e1 | 232 | * @q: The &struct request_queue in question |
1da177e4 LT |
233 | * |
234 | * Description: | |
235 | * blk_start_queue() will clear the stop flag on the queue, and call | |
236 | * the request_fn for the queue if it was in a stopped state when | |
237 | * entered. Also see blk_stop_queue(). Queue lock must be held. | |
238 | **/ | |
165125e1 | 239 | void blk_start_queue(struct request_queue *q) |
1da177e4 | 240 | { |
a038e253 PBG |
241 | WARN_ON(!irqs_disabled()); |
242 | ||
75ad23bc | 243 | queue_flag_clear(QUEUE_FLAG_STOPPED, q); |
24ecfbe2 | 244 | __blk_run_queue(q); |
1da177e4 | 245 | } |
1da177e4 LT |
246 | EXPORT_SYMBOL(blk_start_queue); |
247 | ||
248 | /** | |
249 | * blk_stop_queue - stop a queue | |
165125e1 | 250 | * @q: The &struct request_queue in question |
1da177e4 LT |
251 | * |
252 | * Description: | |
253 | * The Linux block layer assumes that a block driver will consume all | |
254 | * entries on the request queue when the request_fn strategy is called. | |
255 | * Often this will not happen, because of hardware limitations (queue | |
256 | * depth settings). If a device driver gets a 'queue full' response, | |
257 | * or if it simply chooses not to queue more I/O at one point, it can | |
258 | * call this function to prevent the request_fn from being called until | |
259 | * the driver has signalled it's ready to go again. This happens by calling | |
260 | * blk_start_queue() to restart queue operations. Queue lock must be held. | |
261 | **/ | |
165125e1 | 262 | void blk_stop_queue(struct request_queue *q) |
1da177e4 | 263 | { |
ad3d9d7e | 264 | __cancel_delayed_work(&q->delay_work); |
75ad23bc | 265 | queue_flag_set(QUEUE_FLAG_STOPPED, q); |
1da177e4 LT |
266 | } |
267 | EXPORT_SYMBOL(blk_stop_queue); | |
268 | ||
269 | /** | |
270 | * blk_sync_queue - cancel any pending callbacks on a queue | |
271 | * @q: the queue | |
272 | * | |
273 | * Description: | |
274 | * The block layer may perform asynchronous callback activity | |
275 | * on a queue, such as calling the unplug function after a timeout. | |
276 | * A block device may call blk_sync_queue to ensure that any | |
277 | * such activity is cancelled, thus allowing it to release resources | |
59c51591 | 278 | * that the callbacks might use. The caller must already have made sure |
1da177e4 LT |
279 | * that its ->make_request_fn will not re-add plugging prior to calling |
280 | * this function. | |
281 | * | |
da527770 VG |
282 | * This function does not cancel any asynchronous activity arising |
283 | * out of elevator or throttling code. That would require elevaotor_exit() | |
5efd6113 | 284 | * and blkcg_exit_queue() to be called with queue lock initialized. |
da527770 | 285 | * |
1da177e4 LT |
286 | */ |
287 | void blk_sync_queue(struct request_queue *q) | |
288 | { | |
70ed28b9 | 289 | del_timer_sync(&q->timeout); |
3cca6dc1 | 290 | cancel_delayed_work_sync(&q->delay_work); |
1da177e4 LT |
291 | } |
292 | EXPORT_SYMBOL(blk_sync_queue); | |
293 | ||
294 | /** | |
80a4b58e | 295 | * __blk_run_queue - run a single device queue |
1da177e4 | 296 | * @q: The queue to run |
80a4b58e JA |
297 | * |
298 | * Description: | |
299 | * See @blk_run_queue. This variant must be called with the queue lock | |
24ecfbe2 | 300 | * held and interrupts disabled. |
1da177e4 | 301 | */ |
24ecfbe2 | 302 | void __blk_run_queue(struct request_queue *q) |
1da177e4 | 303 | { |
a538cd03 TH |
304 | if (unlikely(blk_queue_stopped(q))) |
305 | return; | |
306 | ||
c21e6beb | 307 | q->request_fn(q); |
75ad23bc NP |
308 | } |
309 | EXPORT_SYMBOL(__blk_run_queue); | |
dac07ec1 | 310 | |
24ecfbe2 CH |
311 | /** |
312 | * blk_run_queue_async - run a single device queue in workqueue context | |
313 | * @q: The queue to run | |
314 | * | |
315 | * Description: | |
316 | * Tells kblockd to perform the equivalent of @blk_run_queue on behalf | |
317 | * of us. | |
318 | */ | |
319 | void blk_run_queue_async(struct request_queue *q) | |
320 | { | |
3ec717b7 SL |
321 | if (likely(!blk_queue_stopped(q))) { |
322 | __cancel_delayed_work(&q->delay_work); | |
24ecfbe2 | 323 | queue_delayed_work(kblockd_workqueue, &q->delay_work, 0); |
3ec717b7 | 324 | } |
24ecfbe2 | 325 | } |
c21e6beb | 326 | EXPORT_SYMBOL(blk_run_queue_async); |
24ecfbe2 | 327 | |
75ad23bc NP |
328 | /** |
329 | * blk_run_queue - run a single device queue | |
330 | * @q: The queue to run | |
80a4b58e JA |
331 | * |
332 | * Description: | |
333 | * Invoke request handling on this queue, if it has pending work to do. | |
a7f55792 | 334 | * May be used to restart queueing when a request has completed. |
75ad23bc NP |
335 | */ |
336 | void blk_run_queue(struct request_queue *q) | |
337 | { | |
338 | unsigned long flags; | |
339 | ||
340 | spin_lock_irqsave(q->queue_lock, flags); | |
24ecfbe2 | 341 | __blk_run_queue(q); |
1da177e4 LT |
342 | spin_unlock_irqrestore(q->queue_lock, flags); |
343 | } | |
344 | EXPORT_SYMBOL(blk_run_queue); | |
345 | ||
165125e1 | 346 | void blk_put_queue(struct request_queue *q) |
483f4afc AV |
347 | { |
348 | kobject_put(&q->kobj); | |
349 | } | |
d86e0e83 | 350 | EXPORT_SYMBOL(blk_put_queue); |
483f4afc | 351 | |
e3c78ca5 TH |
352 | /** |
353 | * blk_drain_queue - drain requests from request_queue | |
354 | * @q: queue to drain | |
c9a929dd | 355 | * @drain_all: whether to drain all requests or only the ones w/ ELVPRIV |
e3c78ca5 | 356 | * |
c9a929dd TH |
357 | * Drain requests from @q. If @drain_all is set, all requests are drained. |
358 | * If not, only ELVPRIV requests are drained. The caller is responsible | |
359 | * for ensuring that no new requests which need to be drained are queued. | |
e3c78ca5 | 360 | */ |
c9a929dd | 361 | void blk_drain_queue(struct request_queue *q, bool drain_all) |
e3c78ca5 TH |
362 | { |
363 | while (true) { | |
481a7d64 TH |
364 | bool drain = false; |
365 | int i; | |
e3c78ca5 TH |
366 | |
367 | spin_lock_irq(q->queue_lock); | |
368 | ||
b855b04a TH |
369 | /* |
370 | * The caller might be trying to drain @q before its | |
371 | * elevator is initialized. | |
372 | */ | |
373 | if (q->elevator) | |
374 | elv_drain_elevator(q); | |
375 | ||
5efd6113 | 376 | blkcg_drain_queue(q); |
e3c78ca5 | 377 | |
4eabc941 TH |
378 | /* |
379 | * This function might be called on a queue which failed | |
b855b04a TH |
380 | * driver init after queue creation or is not yet fully |
381 | * active yet. Some drivers (e.g. fd and loop) get unhappy | |
382 | * in such cases. Kick queue iff dispatch queue has | |
383 | * something on it and @q has request_fn set. | |
4eabc941 | 384 | */ |
b855b04a | 385 | if (!list_empty(&q->queue_head) && q->request_fn) |
4eabc941 | 386 | __blk_run_queue(q); |
c9a929dd | 387 | |
481a7d64 TH |
388 | drain |= q->rq.elvpriv; |
389 | ||
390 | /* | |
391 | * Unfortunately, requests are queued at and tracked from | |
392 | * multiple places and there's no single counter which can | |
393 | * be drained. Check all the queues and counters. | |
394 | */ | |
395 | if (drain_all) { | |
396 | drain |= !list_empty(&q->queue_head); | |
397 | for (i = 0; i < 2; i++) { | |
398 | drain |= q->rq.count[i]; | |
399 | drain |= q->in_flight[i]; | |
400 | drain |= !list_empty(&q->flush_queue[i]); | |
401 | } | |
402 | } | |
e3c78ca5 TH |
403 | |
404 | spin_unlock_irq(q->queue_lock); | |
405 | ||
481a7d64 | 406 | if (!drain) |
e3c78ca5 TH |
407 | break; |
408 | msleep(10); | |
409 | } | |
410 | } | |
411 | ||
d732580b TH |
412 | /** |
413 | * blk_queue_bypass_start - enter queue bypass mode | |
414 | * @q: queue of interest | |
415 | * | |
416 | * In bypass mode, only the dispatch FIFO queue of @q is used. This | |
417 | * function makes @q enter bypass mode and drains all requests which were | |
6ecf23af TH |
418 | * throttled or issued before. On return, it's guaranteed that no request |
419 | * is being throttled or has ELVPRIV set. | |
d732580b TH |
420 | */ |
421 | void blk_queue_bypass_start(struct request_queue *q) | |
422 | { | |
423 | spin_lock_irq(q->queue_lock); | |
424 | q->bypass_depth++; | |
425 | queue_flag_set(QUEUE_FLAG_BYPASS, q); | |
426 | spin_unlock_irq(q->queue_lock); | |
427 | ||
428 | blk_drain_queue(q, false); | |
429 | } | |
430 | EXPORT_SYMBOL_GPL(blk_queue_bypass_start); | |
431 | ||
432 | /** | |
433 | * blk_queue_bypass_end - leave queue bypass mode | |
434 | * @q: queue of interest | |
435 | * | |
436 | * Leave bypass mode and restore the normal queueing behavior. | |
437 | */ | |
438 | void blk_queue_bypass_end(struct request_queue *q) | |
439 | { | |
440 | spin_lock_irq(q->queue_lock); | |
441 | if (!--q->bypass_depth) | |
442 | queue_flag_clear(QUEUE_FLAG_BYPASS, q); | |
443 | WARN_ON_ONCE(q->bypass_depth < 0); | |
444 | spin_unlock_irq(q->queue_lock); | |
445 | } | |
446 | EXPORT_SYMBOL_GPL(blk_queue_bypass_end); | |
447 | ||
c9a929dd TH |
448 | /** |
449 | * blk_cleanup_queue - shutdown a request queue | |
450 | * @q: request queue to shutdown | |
451 | * | |
452 | * Mark @q DEAD, drain all pending requests, destroy and put it. All | |
453 | * future requests will be failed immediately with -ENODEV. | |
c94a96ac | 454 | */ |
6728cb0e | 455 | void blk_cleanup_queue(struct request_queue *q) |
483f4afc | 456 | { |
c9a929dd | 457 | spinlock_t *lock = q->queue_lock; |
e3335de9 | 458 | |
c9a929dd | 459 | /* mark @q DEAD, no new request or merges will be allowed afterwards */ |
483f4afc | 460 | mutex_lock(&q->sysfs_lock); |
75ad23bc | 461 | queue_flag_set_unlocked(QUEUE_FLAG_DEAD, q); |
c9a929dd TH |
462 | |
463 | spin_lock_irq(lock); | |
6ecf23af TH |
464 | |
465 | /* dead queue is permanently in bypass mode till released */ | |
466 | q->bypass_depth++; | |
467 | queue_flag_set(QUEUE_FLAG_BYPASS, q); | |
468 | ||
c9a929dd TH |
469 | queue_flag_set(QUEUE_FLAG_NOMERGES, q); |
470 | queue_flag_set(QUEUE_FLAG_NOXMERGES, q); | |
471 | queue_flag_set(QUEUE_FLAG_DEAD, q); | |
483f4afc | 472 | |
777eb1bf HR |
473 | if (q->queue_lock != &q->__queue_lock) |
474 | q->queue_lock = &q->__queue_lock; | |
da527770 | 475 | |
c9a929dd TH |
476 | spin_unlock_irq(lock); |
477 | mutex_unlock(&q->sysfs_lock); | |
478 | ||
b855b04a TH |
479 | /* drain all requests queued before DEAD marking */ |
480 | blk_drain_queue(q, true); | |
c9a929dd TH |
481 | |
482 | /* @q won't process any more request, flush async actions */ | |
483 | del_timer_sync(&q->backing_dev_info.laptop_mode_wb_timer); | |
484 | blk_sync_queue(q); | |
485 | ||
486 | /* @q is and will stay empty, shutdown and put */ | |
483f4afc AV |
487 | blk_put_queue(q); |
488 | } | |
1da177e4 LT |
489 | EXPORT_SYMBOL(blk_cleanup_queue); |
490 | ||
165125e1 | 491 | static int blk_init_free_list(struct request_queue *q) |
1da177e4 LT |
492 | { |
493 | struct request_list *rl = &q->rq; | |
494 | ||
1abec4fd MS |
495 | if (unlikely(rl->rq_pool)) |
496 | return 0; | |
497 | ||
1faa16d2 JA |
498 | rl->count[BLK_RW_SYNC] = rl->count[BLK_RW_ASYNC] = 0; |
499 | rl->starved[BLK_RW_SYNC] = rl->starved[BLK_RW_ASYNC] = 0; | |
cb98fc8b | 500 | rl->elvpriv = 0; |
1faa16d2 JA |
501 | init_waitqueue_head(&rl->wait[BLK_RW_SYNC]); |
502 | init_waitqueue_head(&rl->wait[BLK_RW_ASYNC]); | |
1da177e4 | 503 | |
1946089a CL |
504 | rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab, |
505 | mempool_free_slab, request_cachep, q->node); | |
1da177e4 LT |
506 | |
507 | if (!rl->rq_pool) | |
508 | return -ENOMEM; | |
509 | ||
510 | return 0; | |
511 | } | |
512 | ||
165125e1 | 513 | struct request_queue *blk_alloc_queue(gfp_t gfp_mask) |
1da177e4 | 514 | { |
1946089a CL |
515 | return blk_alloc_queue_node(gfp_mask, -1); |
516 | } | |
517 | EXPORT_SYMBOL(blk_alloc_queue); | |
1da177e4 | 518 | |
165125e1 | 519 | struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id) |
1946089a | 520 | { |
165125e1 | 521 | struct request_queue *q; |
e0bf68dd | 522 | int err; |
1946089a | 523 | |
8324aa91 | 524 | q = kmem_cache_alloc_node(blk_requestq_cachep, |
94f6030c | 525 | gfp_mask | __GFP_ZERO, node_id); |
1da177e4 LT |
526 | if (!q) |
527 | return NULL; | |
528 | ||
a73f730d TH |
529 | q->id = ida_simple_get(&blk_queue_ida, 0, 0, GFP_KERNEL); |
530 | if (q->id < 0) | |
531 | goto fail_q; | |
532 | ||
0989a025 JA |
533 | q->backing_dev_info.ra_pages = |
534 | (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE; | |
535 | q->backing_dev_info.state = 0; | |
536 | q->backing_dev_info.capabilities = BDI_CAP_MAP_COPY; | |
d993831f | 537 | q->backing_dev_info.name = "block"; |
5151412d | 538 | q->node = node_id; |
0989a025 | 539 | |
e0bf68dd | 540 | err = bdi_init(&q->backing_dev_info); |
a73f730d TH |
541 | if (err) |
542 | goto fail_id; | |
e0bf68dd | 543 | |
31373d09 MG |
544 | setup_timer(&q->backing_dev_info.laptop_mode_wb_timer, |
545 | laptop_mode_timer_fn, (unsigned long) q); | |
242f9dcb | 546 | setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q); |
b855b04a | 547 | INIT_LIST_HEAD(&q->queue_head); |
242f9dcb | 548 | INIT_LIST_HEAD(&q->timeout_list); |
a612fddf | 549 | INIT_LIST_HEAD(&q->icq_list); |
ae1b1539 TH |
550 | INIT_LIST_HEAD(&q->flush_queue[0]); |
551 | INIT_LIST_HEAD(&q->flush_queue[1]); | |
552 | INIT_LIST_HEAD(&q->flush_data_in_flight); | |
3cca6dc1 | 553 | INIT_DELAYED_WORK(&q->delay_work, blk_delay_work); |
483f4afc | 554 | |
8324aa91 | 555 | kobject_init(&q->kobj, &blk_queue_ktype); |
1da177e4 | 556 | |
483f4afc | 557 | mutex_init(&q->sysfs_lock); |
e7e72bf6 | 558 | spin_lock_init(&q->__queue_lock); |
483f4afc | 559 | |
c94a96ac VG |
560 | /* |
561 | * By default initialize queue_lock to internal lock and driver can | |
562 | * override it later if need be. | |
563 | */ | |
564 | q->queue_lock = &q->__queue_lock; | |
565 | ||
5efd6113 | 566 | if (blkcg_init_queue(q)) |
f51b802c TH |
567 | goto fail_id; |
568 | ||
1da177e4 | 569 | return q; |
a73f730d TH |
570 | |
571 | fail_id: | |
572 | ida_simple_remove(&blk_queue_ida, q->id); | |
573 | fail_q: | |
574 | kmem_cache_free(blk_requestq_cachep, q); | |
575 | return NULL; | |
1da177e4 | 576 | } |
1946089a | 577 | EXPORT_SYMBOL(blk_alloc_queue_node); |
1da177e4 LT |
578 | |
579 | /** | |
580 | * blk_init_queue - prepare a request queue for use with a block device | |
581 | * @rfn: The function to be called to process requests that have been | |
582 | * placed on the queue. | |
583 | * @lock: Request queue spin lock | |
584 | * | |
585 | * Description: | |
586 | * If a block device wishes to use the standard request handling procedures, | |
587 | * which sorts requests and coalesces adjacent requests, then it must | |
588 | * call blk_init_queue(). The function @rfn will be called when there | |
589 | * are requests on the queue that need to be processed. If the device | |
590 | * supports plugging, then @rfn may not be called immediately when requests | |
591 | * are available on the queue, but may be called at some time later instead. | |
592 | * Plugged queues are generally unplugged when a buffer belonging to one | |
593 | * of the requests on the queue is needed, or due to memory pressure. | |
594 | * | |
595 | * @rfn is not required, or even expected, to remove all requests off the | |
596 | * queue, but only as many as it can handle at a time. If it does leave | |
597 | * requests on the queue, it is responsible for arranging that the requests | |
598 | * get dealt with eventually. | |
599 | * | |
600 | * The queue spin lock must be held while manipulating the requests on the | |
a038e253 PBG |
601 | * request queue; this lock will be taken also from interrupt context, so irq |
602 | * disabling is needed for it. | |
1da177e4 | 603 | * |
710027a4 | 604 | * Function returns a pointer to the initialized request queue, or %NULL if |
1da177e4 LT |
605 | * it didn't succeed. |
606 | * | |
607 | * Note: | |
608 | * blk_init_queue() must be paired with a blk_cleanup_queue() call | |
609 | * when the block device is deactivated (such as at module unload). | |
610 | **/ | |
1946089a | 611 | |
165125e1 | 612 | struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock) |
1da177e4 | 613 | { |
1946089a CL |
614 | return blk_init_queue_node(rfn, lock, -1); |
615 | } | |
616 | EXPORT_SYMBOL(blk_init_queue); | |
617 | ||
165125e1 | 618 | struct request_queue * |
1946089a CL |
619 | blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id) |
620 | { | |
c86d1b8a | 621 | struct request_queue *uninit_q, *q; |
1da177e4 | 622 | |
c86d1b8a MS |
623 | uninit_q = blk_alloc_queue_node(GFP_KERNEL, node_id); |
624 | if (!uninit_q) | |
625 | return NULL; | |
626 | ||
5151412d | 627 | q = blk_init_allocated_queue(uninit_q, rfn, lock); |
c86d1b8a MS |
628 | if (!q) |
629 | blk_cleanup_queue(uninit_q); | |
630 | ||
631 | return q; | |
01effb0d MS |
632 | } |
633 | EXPORT_SYMBOL(blk_init_queue_node); | |
634 | ||
635 | struct request_queue * | |
636 | blk_init_allocated_queue(struct request_queue *q, request_fn_proc *rfn, | |
637 | spinlock_t *lock) | |
01effb0d | 638 | { |
1da177e4 LT |
639 | if (!q) |
640 | return NULL; | |
641 | ||
c86d1b8a | 642 | if (blk_init_free_list(q)) |
8669aafd | 643 | return NULL; |
1da177e4 LT |
644 | |
645 | q->request_fn = rfn; | |
1da177e4 | 646 | q->prep_rq_fn = NULL; |
28018c24 | 647 | q->unprep_rq_fn = NULL; |
bc58ba94 | 648 | q->queue_flags = QUEUE_FLAG_DEFAULT; |
c94a96ac VG |
649 | |
650 | /* Override internal queue lock with supplied lock pointer */ | |
651 | if (lock) | |
652 | q->queue_lock = lock; | |
1da177e4 | 653 | |
f3b144aa JA |
654 | /* |
655 | * This also sets hw/phys segments, boundary and size | |
656 | */ | |
c20e8de2 | 657 | blk_queue_make_request(q, blk_queue_bio); |
1da177e4 | 658 | |
44ec9542 AS |
659 | q->sg_reserved_size = INT_MAX; |
660 | ||
1da177e4 LT |
661 | /* |
662 | * all done | |
663 | */ | |
664 | if (!elevator_init(q, NULL)) { | |
665 | blk_queue_congestion_threshold(q); | |
666 | return q; | |
667 | } | |
668 | ||
1da177e4 LT |
669 | return NULL; |
670 | } | |
5151412d | 671 | EXPORT_SYMBOL(blk_init_allocated_queue); |
1da177e4 | 672 | |
09ac46c4 | 673 | bool blk_get_queue(struct request_queue *q) |
1da177e4 | 674 | { |
34f6055c | 675 | if (likely(!blk_queue_dead(q))) { |
09ac46c4 TH |
676 | __blk_get_queue(q); |
677 | return true; | |
1da177e4 LT |
678 | } |
679 | ||
09ac46c4 | 680 | return false; |
1da177e4 | 681 | } |
d86e0e83 | 682 | EXPORT_SYMBOL(blk_get_queue); |
1da177e4 | 683 | |
165125e1 | 684 | static inline void blk_free_request(struct request_queue *q, struct request *rq) |
1da177e4 | 685 | { |
f1f8cc94 | 686 | if (rq->cmd_flags & REQ_ELVPRIV) { |
cb98fc8b | 687 | elv_put_request(q, rq); |
f1f8cc94 | 688 | if (rq->elv.icq) |
11a3122f | 689 | put_io_context(rq->elv.icq->ioc); |
f1f8cc94 TH |
690 | } |
691 | ||
1da177e4 LT |
692 | mempool_free(rq, q->rq.rq_pool); |
693 | } | |
694 | ||
1ea25ecb | 695 | static struct request * |
f1f8cc94 TH |
696 | blk_alloc_request(struct request_queue *q, struct io_cq *icq, |
697 | unsigned int flags, gfp_t gfp_mask) | |
1da177e4 LT |
698 | { |
699 | struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask); | |
700 | ||
701 | if (!rq) | |
702 | return NULL; | |
703 | ||
2a4aa30c | 704 | blk_rq_init(q, rq); |
1afb20f3 | 705 | |
42dad764 | 706 | rq->cmd_flags = flags | REQ_ALLOCED; |
1da177e4 | 707 | |
f1f8cc94 TH |
708 | if (flags & REQ_ELVPRIV) { |
709 | rq->elv.icq = icq; | |
710 | if (unlikely(elv_set_request(q, rq, gfp_mask))) { | |
711 | mempool_free(rq, q->rq.rq_pool); | |
712 | return NULL; | |
713 | } | |
714 | /* @rq->elv.icq holds on to io_context until @rq is freed */ | |
715 | if (icq) | |
716 | get_io_context(icq->ioc); | |
cb98fc8b | 717 | } |
1da177e4 | 718 | |
cb98fc8b | 719 | return rq; |
1da177e4 LT |
720 | } |
721 | ||
722 | /* | |
723 | * ioc_batching returns true if the ioc is a valid batching request and | |
724 | * should be given priority access to a request. | |
725 | */ | |
165125e1 | 726 | static inline int ioc_batching(struct request_queue *q, struct io_context *ioc) |
1da177e4 LT |
727 | { |
728 | if (!ioc) | |
729 | return 0; | |
730 | ||
731 | /* | |
732 | * Make sure the process is able to allocate at least 1 request | |
733 | * even if the batch times out, otherwise we could theoretically | |
734 | * lose wakeups. | |
735 | */ | |
736 | return ioc->nr_batch_requests == q->nr_batching || | |
737 | (ioc->nr_batch_requests > 0 | |
738 | && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME)); | |
739 | } | |
740 | ||
741 | /* | |
742 | * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This | |
743 | * will cause the process to be a "batcher" on all queues in the system. This | |
744 | * is the behaviour we want though - once it gets a wakeup it should be given | |
745 | * a nice run. | |
746 | */ | |
165125e1 | 747 | static void ioc_set_batching(struct request_queue *q, struct io_context *ioc) |
1da177e4 LT |
748 | { |
749 | if (!ioc || ioc_batching(q, ioc)) | |
750 | return; | |
751 | ||
752 | ioc->nr_batch_requests = q->nr_batching; | |
753 | ioc->last_waited = jiffies; | |
754 | } | |
755 | ||
1faa16d2 | 756 | static void __freed_request(struct request_queue *q, int sync) |
1da177e4 LT |
757 | { |
758 | struct request_list *rl = &q->rq; | |
759 | ||
1faa16d2 JA |
760 | if (rl->count[sync] < queue_congestion_off_threshold(q)) |
761 | blk_clear_queue_congested(q, sync); | |
1da177e4 | 762 | |
1faa16d2 JA |
763 | if (rl->count[sync] + 1 <= q->nr_requests) { |
764 | if (waitqueue_active(&rl->wait[sync])) | |
765 | wake_up(&rl->wait[sync]); | |
1da177e4 | 766 | |
1faa16d2 | 767 | blk_clear_queue_full(q, sync); |
1da177e4 LT |
768 | } |
769 | } | |
770 | ||
771 | /* | |
772 | * A request has just been released. Account for it, update the full and | |
773 | * congestion status, wake up any waiters. Called under q->queue_lock. | |
774 | */ | |
75eb6c37 | 775 | static void freed_request(struct request_queue *q, unsigned int flags) |
1da177e4 LT |
776 | { |
777 | struct request_list *rl = &q->rq; | |
75eb6c37 | 778 | int sync = rw_is_sync(flags); |
1da177e4 | 779 | |
1faa16d2 | 780 | rl->count[sync]--; |
75eb6c37 | 781 | if (flags & REQ_ELVPRIV) |
cb98fc8b | 782 | rl->elvpriv--; |
1da177e4 | 783 | |
1faa16d2 | 784 | __freed_request(q, sync); |
1da177e4 | 785 | |
1faa16d2 JA |
786 | if (unlikely(rl->starved[sync ^ 1])) |
787 | __freed_request(q, sync ^ 1); | |
1da177e4 LT |
788 | } |
789 | ||
9d5a4e94 MS |
790 | /* |
791 | * Determine if elevator data should be initialized when allocating the | |
792 | * request associated with @bio. | |
793 | */ | |
794 | static bool blk_rq_should_init_elevator(struct bio *bio) | |
795 | { | |
796 | if (!bio) | |
797 | return true; | |
798 | ||
799 | /* | |
800 | * Flush requests do not use the elevator so skip initialization. | |
801 | * This allows a request to share the flush and elevator data. | |
802 | */ | |
803 | if (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) | |
804 | return false; | |
805 | ||
806 | return true; | |
807 | } | |
808 | ||
da8303c6 TH |
809 | /** |
810 | * get_request - get a free request | |
811 | * @q: request_queue to allocate request from | |
812 | * @rw_flags: RW and SYNC flags | |
813 | * @bio: bio to allocate request for (can be %NULL) | |
814 | * @gfp_mask: allocation mask | |
815 | * | |
816 | * Get a free request from @q. This function may fail under memory | |
817 | * pressure or if @q is dead. | |
818 | * | |
819 | * Must be callled with @q->queue_lock held and, | |
820 | * Returns %NULL on failure, with @q->queue_lock held. | |
821 | * Returns !%NULL on success, with @q->queue_lock *not held*. | |
1da177e4 | 822 | */ |
165125e1 | 823 | static struct request *get_request(struct request_queue *q, int rw_flags, |
7749a8d4 | 824 | struct bio *bio, gfp_t gfp_mask) |
1da177e4 LT |
825 | { |
826 | struct request *rq = NULL; | |
827 | struct request_list *rl = &q->rq; | |
f1f8cc94 | 828 | struct elevator_type *et; |
f2dbd76a | 829 | struct io_context *ioc; |
f1f8cc94 | 830 | struct io_cq *icq = NULL; |
1faa16d2 | 831 | const bool is_sync = rw_is_sync(rw_flags) != 0; |
f2dbd76a | 832 | bool retried = false; |
75eb6c37 | 833 | int may_queue; |
f2dbd76a | 834 | retry: |
f1f8cc94 | 835 | et = q->elevator->type; |
f2dbd76a | 836 | ioc = current->io_context; |
88ee5ef1 | 837 | |
34f6055c | 838 | if (unlikely(blk_queue_dead(q))) |
da8303c6 TH |
839 | return NULL; |
840 | ||
7749a8d4 | 841 | may_queue = elv_may_queue(q, rw_flags); |
88ee5ef1 JA |
842 | if (may_queue == ELV_MQUEUE_NO) |
843 | goto rq_starved; | |
844 | ||
1faa16d2 JA |
845 | if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) { |
846 | if (rl->count[is_sync]+1 >= q->nr_requests) { | |
f2dbd76a TH |
847 | /* |
848 | * We want ioc to record batching state. If it's | |
849 | * not already there, creating a new one requires | |
850 | * dropping queue_lock, which in turn requires | |
851 | * retesting conditions to avoid queue hang. | |
852 | */ | |
853 | if (!ioc && !retried) { | |
854 | spin_unlock_irq(q->queue_lock); | |
855 | create_io_context(current, gfp_mask, q->node); | |
856 | spin_lock_irq(q->queue_lock); | |
857 | retried = true; | |
858 | goto retry; | |
859 | } | |
860 | ||
88ee5ef1 JA |
861 | /* |
862 | * The queue will fill after this allocation, so set | |
863 | * it as full, and mark this process as "batching". | |
864 | * This process will be allowed to complete a batch of | |
865 | * requests, others will be blocked. | |
866 | */ | |
1faa16d2 | 867 | if (!blk_queue_full(q, is_sync)) { |
88ee5ef1 | 868 | ioc_set_batching(q, ioc); |
1faa16d2 | 869 | blk_set_queue_full(q, is_sync); |
88ee5ef1 JA |
870 | } else { |
871 | if (may_queue != ELV_MQUEUE_MUST | |
872 | && !ioc_batching(q, ioc)) { | |
873 | /* | |
874 | * The queue is full and the allocating | |
875 | * process is not a "batcher", and not | |
876 | * exempted by the IO scheduler | |
877 | */ | |
878 | goto out; | |
879 | } | |
880 | } | |
1da177e4 | 881 | } |
1faa16d2 | 882 | blk_set_queue_congested(q, is_sync); |
1da177e4 LT |
883 | } |
884 | ||
082cf69e JA |
885 | /* |
886 | * Only allow batching queuers to allocate up to 50% over the defined | |
887 | * limit of requests, otherwise we could have thousands of requests | |
888 | * allocated with any setting of ->nr_requests | |
889 | */ | |
1faa16d2 | 890 | if (rl->count[is_sync] >= (3 * q->nr_requests / 2)) |
082cf69e | 891 | goto out; |
fd782a4a | 892 | |
1faa16d2 JA |
893 | rl->count[is_sync]++; |
894 | rl->starved[is_sync] = 0; | |
cb98fc8b | 895 | |
f1f8cc94 TH |
896 | /* |
897 | * Decide whether the new request will be managed by elevator. If | |
898 | * so, mark @rw_flags and increment elvpriv. Non-zero elvpriv will | |
899 | * prevent the current elevator from being destroyed until the new | |
900 | * request is freed. This guarantees icq's won't be destroyed and | |
901 | * makes creating new ones safe. | |
902 | * | |
903 | * Also, lookup icq while holding queue_lock. If it doesn't exist, | |
904 | * it will be created after releasing queue_lock. | |
905 | */ | |
d732580b | 906 | if (blk_rq_should_init_elevator(bio) && !blk_queue_bypass(q)) { |
75eb6c37 TH |
907 | rw_flags |= REQ_ELVPRIV; |
908 | rl->elvpriv++; | |
f1f8cc94 TH |
909 | if (et->icq_cache && ioc) |
910 | icq = ioc_lookup_icq(ioc, q); | |
9d5a4e94 | 911 | } |
cb98fc8b | 912 | |
f253b86b JA |
913 | if (blk_queue_io_stat(q)) |
914 | rw_flags |= REQ_IO_STAT; | |
1da177e4 LT |
915 | spin_unlock_irq(q->queue_lock); |
916 | ||
f1f8cc94 | 917 | /* create icq if missing */ |
05c30b95 | 918 | if ((rw_flags & REQ_ELVPRIV) && unlikely(et->icq_cache && !icq)) { |
f1f8cc94 | 919 | icq = ioc_create_icq(q, gfp_mask); |
05c30b95 SL |
920 | if (!icq) |
921 | goto fail_icq; | |
922 | } | |
f1f8cc94 | 923 | |
05c30b95 | 924 | rq = blk_alloc_request(q, icq, rw_flags, gfp_mask); |
f1f8cc94 | 925 | |
05c30b95 | 926 | fail_icq: |
88ee5ef1 | 927 | if (unlikely(!rq)) { |
1da177e4 LT |
928 | /* |
929 | * Allocation failed presumably due to memory. Undo anything | |
930 | * we might have messed up. | |
931 | * | |
932 | * Allocating task should really be put onto the front of the | |
933 | * wait queue, but this is pretty rare. | |
934 | */ | |
935 | spin_lock_irq(q->queue_lock); | |
75eb6c37 | 936 | freed_request(q, rw_flags); |
1da177e4 LT |
937 | |
938 | /* | |
939 | * in the very unlikely event that allocation failed and no | |
940 | * requests for this direction was pending, mark us starved | |
941 | * so that freeing of a request in the other direction will | |
942 | * notice us. another possible fix would be to split the | |
943 | * rq mempool into READ and WRITE | |
944 | */ | |
945 | rq_starved: | |
1faa16d2 JA |
946 | if (unlikely(rl->count[is_sync] == 0)) |
947 | rl->starved[is_sync] = 1; | |
1da177e4 | 948 | |
1da177e4 LT |
949 | goto out; |
950 | } | |
951 | ||
88ee5ef1 JA |
952 | /* |
953 | * ioc may be NULL here, and ioc_batching will be false. That's | |
954 | * OK, if the queue is under the request limit then requests need | |
955 | * not count toward the nr_batch_requests limit. There will always | |
956 | * be some limit enforced by BLK_BATCH_TIME. | |
957 | */ | |
1da177e4 LT |
958 | if (ioc_batching(q, ioc)) |
959 | ioc->nr_batch_requests--; | |
6728cb0e | 960 | |
1faa16d2 | 961 | trace_block_getrq(q, bio, rw_flags & 1); |
1da177e4 | 962 | out: |
1da177e4 LT |
963 | return rq; |
964 | } | |
965 | ||
da8303c6 TH |
966 | /** |
967 | * get_request_wait - get a free request with retry | |
968 | * @q: request_queue to allocate request from | |
969 | * @rw_flags: RW and SYNC flags | |
970 | * @bio: bio to allocate request for (can be %NULL) | |
971 | * | |
972 | * Get a free request from @q. This function keeps retrying under memory | |
973 | * pressure and fails iff @q is dead. | |
d6344532 | 974 | * |
da8303c6 TH |
975 | * Must be callled with @q->queue_lock held and, |
976 | * Returns %NULL on failure, with @q->queue_lock held. | |
977 | * Returns !%NULL on success, with @q->queue_lock *not held*. | |
1da177e4 | 978 | */ |
165125e1 | 979 | static struct request *get_request_wait(struct request_queue *q, int rw_flags, |
22e2c507 | 980 | struct bio *bio) |
1da177e4 | 981 | { |
1faa16d2 | 982 | const bool is_sync = rw_is_sync(rw_flags) != 0; |
1da177e4 LT |
983 | struct request *rq; |
984 | ||
7749a8d4 | 985 | rq = get_request(q, rw_flags, bio, GFP_NOIO); |
450991bc NP |
986 | while (!rq) { |
987 | DEFINE_WAIT(wait); | |
1da177e4 LT |
988 | struct request_list *rl = &q->rq; |
989 | ||
34f6055c | 990 | if (unlikely(blk_queue_dead(q))) |
da8303c6 TH |
991 | return NULL; |
992 | ||
1faa16d2 | 993 | prepare_to_wait_exclusive(&rl->wait[is_sync], &wait, |
1da177e4 LT |
994 | TASK_UNINTERRUPTIBLE); |
995 | ||
1faa16d2 | 996 | trace_block_sleeprq(q, bio, rw_flags & 1); |
1da177e4 | 997 | |
05caf8db ZY |
998 | spin_unlock_irq(q->queue_lock); |
999 | io_schedule(); | |
1da177e4 | 1000 | |
05caf8db ZY |
1001 | /* |
1002 | * After sleeping, we become a "batching" process and | |
1003 | * will be able to allocate at least one request, and | |
1004 | * up to a big batch of them for a small period time. | |
1005 | * See ioc_batching, ioc_set_batching | |
1006 | */ | |
f2dbd76a TH |
1007 | create_io_context(current, GFP_NOIO, q->node); |
1008 | ioc_set_batching(q, current->io_context); | |
d6344532 | 1009 | |
05caf8db | 1010 | spin_lock_irq(q->queue_lock); |
1faa16d2 | 1011 | finish_wait(&rl->wait[is_sync], &wait); |
05caf8db ZY |
1012 | |
1013 | rq = get_request(q, rw_flags, bio, GFP_NOIO); | |
1014 | }; | |
1da177e4 LT |
1015 | |
1016 | return rq; | |
1017 | } | |
1018 | ||
165125e1 | 1019 | struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask) |
1da177e4 LT |
1020 | { |
1021 | struct request *rq; | |
1022 | ||
1023 | BUG_ON(rw != READ && rw != WRITE); | |
1024 | ||
d6344532 | 1025 | spin_lock_irq(q->queue_lock); |
da8303c6 | 1026 | if (gfp_mask & __GFP_WAIT) |
22e2c507 | 1027 | rq = get_request_wait(q, rw, NULL); |
da8303c6 | 1028 | else |
22e2c507 | 1029 | rq = get_request(q, rw, NULL, gfp_mask); |
da8303c6 TH |
1030 | if (!rq) |
1031 | spin_unlock_irq(q->queue_lock); | |
d6344532 | 1032 | /* q->queue_lock is unlocked at this point */ |
1da177e4 LT |
1033 | |
1034 | return rq; | |
1035 | } | |
1da177e4 LT |
1036 | EXPORT_SYMBOL(blk_get_request); |
1037 | ||
dc72ef4a | 1038 | /** |
79eb63e9 | 1039 | * blk_make_request - given a bio, allocate a corresponding struct request. |
8ebf9756 | 1040 | * @q: target request queue |
79eb63e9 BH |
1041 | * @bio: The bio describing the memory mappings that will be submitted for IO. |
1042 | * It may be a chained-bio properly constructed by block/bio layer. | |
8ebf9756 | 1043 | * @gfp_mask: gfp flags to be used for memory allocation |
dc72ef4a | 1044 | * |
79eb63e9 BH |
1045 | * blk_make_request is the parallel of generic_make_request for BLOCK_PC |
1046 | * type commands. Where the struct request needs to be farther initialized by | |
1047 | * the caller. It is passed a &struct bio, which describes the memory info of | |
1048 | * the I/O transfer. | |
dc72ef4a | 1049 | * |
79eb63e9 BH |
1050 | * The caller of blk_make_request must make sure that bi_io_vec |
1051 | * are set to describe the memory buffers. That bio_data_dir() will return | |
1052 | * the needed direction of the request. (And all bio's in the passed bio-chain | |
1053 | * are properly set accordingly) | |
1054 | * | |
1055 | * If called under none-sleepable conditions, mapped bio buffers must not | |
1056 | * need bouncing, by calling the appropriate masked or flagged allocator, | |
1057 | * suitable for the target device. Otherwise the call to blk_queue_bounce will | |
1058 | * BUG. | |
53674ac5 JA |
1059 | * |
1060 | * WARNING: When allocating/cloning a bio-chain, careful consideration should be | |
1061 | * given to how you allocate bios. In particular, you cannot use __GFP_WAIT for | |
1062 | * anything but the first bio in the chain. Otherwise you risk waiting for IO | |
1063 | * completion of a bio that hasn't been submitted yet, thus resulting in a | |
1064 | * deadlock. Alternatively bios should be allocated using bio_kmalloc() instead | |
1065 | * of bio_alloc(), as that avoids the mempool deadlock. | |
1066 | * If possible a big IO should be split into smaller parts when allocation | |
1067 | * fails. Partial allocation should not be an error, or you risk a live-lock. | |
dc72ef4a | 1068 | */ |
79eb63e9 BH |
1069 | struct request *blk_make_request(struct request_queue *q, struct bio *bio, |
1070 | gfp_t gfp_mask) | |
dc72ef4a | 1071 | { |
79eb63e9 BH |
1072 | struct request *rq = blk_get_request(q, bio_data_dir(bio), gfp_mask); |
1073 | ||
1074 | if (unlikely(!rq)) | |
1075 | return ERR_PTR(-ENOMEM); | |
1076 | ||
1077 | for_each_bio(bio) { | |
1078 | struct bio *bounce_bio = bio; | |
1079 | int ret; | |
1080 | ||
1081 | blk_queue_bounce(q, &bounce_bio); | |
1082 | ret = blk_rq_append_bio(q, rq, bounce_bio); | |
1083 | if (unlikely(ret)) { | |
1084 | blk_put_request(rq); | |
1085 | return ERR_PTR(ret); | |
1086 | } | |
1087 | } | |
1088 | ||
1089 | return rq; | |
dc72ef4a | 1090 | } |
79eb63e9 | 1091 | EXPORT_SYMBOL(blk_make_request); |
dc72ef4a | 1092 | |
1da177e4 LT |
1093 | /** |
1094 | * blk_requeue_request - put a request back on queue | |
1095 | * @q: request queue where request should be inserted | |
1096 | * @rq: request to be inserted | |
1097 | * | |
1098 | * Description: | |
1099 | * Drivers often keep queueing requests until the hardware cannot accept | |
1100 | * more, when that condition happens we need to put the request back | |
1101 | * on the queue. Must be called with queue lock held. | |
1102 | */ | |
165125e1 | 1103 | void blk_requeue_request(struct request_queue *q, struct request *rq) |
1da177e4 | 1104 | { |
242f9dcb JA |
1105 | blk_delete_timer(rq); |
1106 | blk_clear_rq_complete(rq); | |
5f3ea37c | 1107 | trace_block_rq_requeue(q, rq); |
2056a782 | 1108 | |
1da177e4 LT |
1109 | if (blk_rq_tagged(rq)) |
1110 | blk_queue_end_tag(q, rq); | |
1111 | ||
ba396a6c JB |
1112 | BUG_ON(blk_queued_rq(rq)); |
1113 | ||
1da177e4 LT |
1114 | elv_requeue_request(q, rq); |
1115 | } | |
1da177e4 LT |
1116 | EXPORT_SYMBOL(blk_requeue_request); |
1117 | ||
73c10101 JA |
1118 | static void add_acct_request(struct request_queue *q, struct request *rq, |
1119 | int where) | |
1120 | { | |
1121 | drive_stat_acct(rq, 1); | |
7eaceacc | 1122 | __elv_add_request(q, rq, where); |
73c10101 JA |
1123 | } |
1124 | ||
074a7aca TH |
1125 | static void part_round_stats_single(int cpu, struct hd_struct *part, |
1126 | unsigned long now) | |
1127 | { | |
1128 | if (now == part->stamp) | |
1129 | return; | |
1130 | ||
316d315b | 1131 | if (part_in_flight(part)) { |
074a7aca | 1132 | __part_stat_add(cpu, part, time_in_queue, |
316d315b | 1133 | part_in_flight(part) * (now - part->stamp)); |
074a7aca TH |
1134 | __part_stat_add(cpu, part, io_ticks, (now - part->stamp)); |
1135 | } | |
1136 | part->stamp = now; | |
1137 | } | |
1138 | ||
1139 | /** | |
496aa8a9 RD |
1140 | * part_round_stats() - Round off the performance stats on a struct disk_stats. |
1141 | * @cpu: cpu number for stats access | |
1142 | * @part: target partition | |
1da177e4 LT |
1143 | * |
1144 | * The average IO queue length and utilisation statistics are maintained | |
1145 | * by observing the current state of the queue length and the amount of | |
1146 | * time it has been in this state for. | |
1147 | * | |
1148 | * Normally, that accounting is done on IO completion, but that can result | |
1149 | * in more than a second's worth of IO being accounted for within any one | |
1150 | * second, leading to >100% utilisation. To deal with that, we call this | |
1151 | * function to do a round-off before returning the results when reading | |
1152 | * /proc/diskstats. This accounts immediately for all queue usage up to | |
1153 | * the current jiffies and restarts the counters again. | |
1154 | */ | |
c9959059 | 1155 | void part_round_stats(int cpu, struct hd_struct *part) |
6f2576af JM |
1156 | { |
1157 | unsigned long now = jiffies; | |
1158 | ||
074a7aca TH |
1159 | if (part->partno) |
1160 | part_round_stats_single(cpu, &part_to_disk(part)->part0, now); | |
1161 | part_round_stats_single(cpu, part, now); | |
6f2576af | 1162 | } |
074a7aca | 1163 | EXPORT_SYMBOL_GPL(part_round_stats); |
6f2576af | 1164 | |
1da177e4 LT |
1165 | /* |
1166 | * queue lock must be held | |
1167 | */ | |
165125e1 | 1168 | void __blk_put_request(struct request_queue *q, struct request *req) |
1da177e4 | 1169 | { |
1da177e4 LT |
1170 | if (unlikely(!q)) |
1171 | return; | |
1172 | if (unlikely(--req->ref_count)) | |
1173 | return; | |
1174 | ||
8922e16c TH |
1175 | elv_completed_request(q, req); |
1176 | ||
1cd96c24 BH |
1177 | /* this is a bio leak */ |
1178 | WARN_ON(req->bio != NULL); | |
1179 | ||
1da177e4 LT |
1180 | /* |
1181 | * Request may not have originated from ll_rw_blk. if not, | |
1182 | * it didn't come out of our reserved rq pools | |
1183 | */ | |
49171e5c | 1184 | if (req->cmd_flags & REQ_ALLOCED) { |
75eb6c37 | 1185 | unsigned int flags = req->cmd_flags; |
1da177e4 | 1186 | |
1da177e4 | 1187 | BUG_ON(!list_empty(&req->queuelist)); |
9817064b | 1188 | BUG_ON(!hlist_unhashed(&req->hash)); |
1da177e4 LT |
1189 | |
1190 | blk_free_request(q, req); | |
75eb6c37 | 1191 | freed_request(q, flags); |
1da177e4 LT |
1192 | } |
1193 | } | |
6e39b69e MC |
1194 | EXPORT_SYMBOL_GPL(__blk_put_request); |
1195 | ||
1da177e4 LT |
1196 | void blk_put_request(struct request *req) |
1197 | { | |
8922e16c | 1198 | unsigned long flags; |
165125e1 | 1199 | struct request_queue *q = req->q; |
8922e16c | 1200 | |
52a93ba8 FT |
1201 | spin_lock_irqsave(q->queue_lock, flags); |
1202 | __blk_put_request(q, req); | |
1203 | spin_unlock_irqrestore(q->queue_lock, flags); | |
1da177e4 | 1204 | } |
1da177e4 LT |
1205 | EXPORT_SYMBOL(blk_put_request); |
1206 | ||
66ac0280 CH |
1207 | /** |
1208 | * blk_add_request_payload - add a payload to a request | |
1209 | * @rq: request to update | |
1210 | * @page: page backing the payload | |
1211 | * @len: length of the payload. | |
1212 | * | |
1213 | * This allows to later add a payload to an already submitted request by | |
1214 | * a block driver. The driver needs to take care of freeing the payload | |
1215 | * itself. | |
1216 | * | |
1217 | * Note that this is a quite horrible hack and nothing but handling of | |
1218 | * discard requests should ever use it. | |
1219 | */ | |
1220 | void blk_add_request_payload(struct request *rq, struct page *page, | |
1221 | unsigned int len) | |
1222 | { | |
1223 | struct bio *bio = rq->bio; | |
1224 | ||
1225 | bio->bi_io_vec->bv_page = page; | |
1226 | bio->bi_io_vec->bv_offset = 0; | |
1227 | bio->bi_io_vec->bv_len = len; | |
1228 | ||
1229 | bio->bi_size = len; | |
1230 | bio->bi_vcnt = 1; | |
1231 | bio->bi_phys_segments = 1; | |
1232 | ||
1233 | rq->__data_len = rq->resid_len = len; | |
1234 | rq->nr_phys_segments = 1; | |
1235 | rq->buffer = bio_data(bio); | |
1236 | } | |
1237 | EXPORT_SYMBOL_GPL(blk_add_request_payload); | |
1238 | ||
73c10101 JA |
1239 | static bool bio_attempt_back_merge(struct request_queue *q, struct request *req, |
1240 | struct bio *bio) | |
1241 | { | |
1242 | const int ff = bio->bi_rw & REQ_FAILFAST_MASK; | |
1243 | ||
73c10101 JA |
1244 | if (!ll_back_merge_fn(q, req, bio)) |
1245 | return false; | |
1246 | ||
1247 | trace_block_bio_backmerge(q, bio); | |
1248 | ||
1249 | if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff) | |
1250 | blk_rq_set_mixed_merge(req); | |
1251 | ||
1252 | req->biotail->bi_next = bio; | |
1253 | req->biotail = bio; | |
1254 | req->__data_len += bio->bi_size; | |
1255 | req->ioprio = ioprio_best(req->ioprio, bio_prio(bio)); | |
1256 | ||
1257 | drive_stat_acct(req, 0); | |
1258 | return true; | |
1259 | } | |
1260 | ||
1261 | static bool bio_attempt_front_merge(struct request_queue *q, | |
1262 | struct request *req, struct bio *bio) | |
1263 | { | |
1264 | const int ff = bio->bi_rw & REQ_FAILFAST_MASK; | |
73c10101 | 1265 | |
73c10101 JA |
1266 | if (!ll_front_merge_fn(q, req, bio)) |
1267 | return false; | |
1268 | ||
1269 | trace_block_bio_frontmerge(q, bio); | |
1270 | ||
1271 | if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff) | |
1272 | blk_rq_set_mixed_merge(req); | |
1273 | ||
73c10101 JA |
1274 | bio->bi_next = req->bio; |
1275 | req->bio = bio; | |
1276 | ||
1277 | /* | |
1278 | * may not be valid. if the low level driver said | |
1279 | * it didn't need a bounce buffer then it better | |
1280 | * not touch req->buffer either... | |
1281 | */ | |
1282 | req->buffer = bio_data(bio); | |
1283 | req->__sector = bio->bi_sector; | |
1284 | req->__data_len += bio->bi_size; | |
1285 | req->ioprio = ioprio_best(req->ioprio, bio_prio(bio)); | |
1286 | ||
1287 | drive_stat_acct(req, 0); | |
1288 | return true; | |
1289 | } | |
1290 | ||
bd87b589 TH |
1291 | /** |
1292 | * attempt_plug_merge - try to merge with %current's plugged list | |
1293 | * @q: request_queue new bio is being queued at | |
1294 | * @bio: new bio being queued | |
1295 | * @request_count: out parameter for number of traversed plugged requests | |
1296 | * | |
1297 | * Determine whether @bio being queued on @q can be merged with a request | |
1298 | * on %current's plugged list. Returns %true if merge was successful, | |
1299 | * otherwise %false. | |
1300 | * | |
07c2bd37 TH |
1301 | * Plugging coalesces IOs from the same issuer for the same purpose without |
1302 | * going through @q->queue_lock. As such it's more of an issuing mechanism | |
1303 | * than scheduling, and the request, while may have elvpriv data, is not | |
1304 | * added on the elevator at this point. In addition, we don't have | |
1305 | * reliable access to the elevator outside queue lock. Only check basic | |
1306 | * merging parameters without querying the elevator. | |
73c10101 | 1307 | */ |
bd87b589 TH |
1308 | static bool attempt_plug_merge(struct request_queue *q, struct bio *bio, |
1309 | unsigned int *request_count) | |
73c10101 JA |
1310 | { |
1311 | struct blk_plug *plug; | |
1312 | struct request *rq; | |
1313 | bool ret = false; | |
1314 | ||
bd87b589 | 1315 | plug = current->plug; |
73c10101 JA |
1316 | if (!plug) |
1317 | goto out; | |
56ebdaf2 | 1318 | *request_count = 0; |
73c10101 JA |
1319 | |
1320 | list_for_each_entry_reverse(rq, &plug->list, queuelist) { | |
1321 | int el_ret; | |
1322 | ||
56ebdaf2 SL |
1323 | (*request_count)++; |
1324 | ||
07c2bd37 | 1325 | if (rq->q != q || !blk_rq_merge_ok(rq, bio)) |
73c10101 JA |
1326 | continue; |
1327 | ||
050c8ea8 | 1328 | el_ret = blk_try_merge(rq, bio); |
73c10101 JA |
1329 | if (el_ret == ELEVATOR_BACK_MERGE) { |
1330 | ret = bio_attempt_back_merge(q, rq, bio); | |
1331 | if (ret) | |
1332 | break; | |
1333 | } else if (el_ret == ELEVATOR_FRONT_MERGE) { | |
1334 | ret = bio_attempt_front_merge(q, rq, bio); | |
1335 | if (ret) | |
1336 | break; | |
1337 | } | |
1338 | } | |
1339 | out: | |
1340 | return ret; | |
1341 | } | |
1342 | ||
86db1e29 | 1343 | void init_request_from_bio(struct request *req, struct bio *bio) |
52d9e675 | 1344 | { |
4aff5e23 | 1345 | req->cmd_type = REQ_TYPE_FS; |
52d9e675 | 1346 | |
7b6d91da CH |
1347 | req->cmd_flags |= bio->bi_rw & REQ_COMMON_MASK; |
1348 | if (bio->bi_rw & REQ_RAHEAD) | |
a82afdfc | 1349 | req->cmd_flags |= REQ_FAILFAST_MASK; |
b31dc66a | 1350 | |
52d9e675 | 1351 | req->errors = 0; |
a2dec7b3 | 1352 | req->__sector = bio->bi_sector; |
52d9e675 | 1353 | req->ioprio = bio_prio(bio); |
bc1c56fd | 1354 | blk_rq_bio_prep(req->q, req, bio); |
52d9e675 TH |
1355 | } |
1356 | ||
5a7bbad2 | 1357 | void blk_queue_bio(struct request_queue *q, struct bio *bio) |
1da177e4 | 1358 | { |
5e00d1b5 | 1359 | const bool sync = !!(bio->bi_rw & REQ_SYNC); |
73c10101 JA |
1360 | struct blk_plug *plug; |
1361 | int el_ret, rw_flags, where = ELEVATOR_INSERT_SORT; | |
1362 | struct request *req; | |
56ebdaf2 | 1363 | unsigned int request_count = 0; |
1da177e4 | 1364 | |
1da177e4 LT |
1365 | /* |
1366 | * low level driver can indicate that it wants pages above a | |
1367 | * certain limit bounced to low memory (ie for highmem, or even | |
1368 | * ISA dma in theory) | |
1369 | */ | |
1370 | blk_queue_bounce(q, &bio); | |
1371 | ||
4fed947c | 1372 | if (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) { |
73c10101 | 1373 | spin_lock_irq(q->queue_lock); |
ae1b1539 | 1374 | where = ELEVATOR_INSERT_FLUSH; |
28e7d184 TH |
1375 | goto get_rq; |
1376 | } | |
1377 | ||
73c10101 JA |
1378 | /* |
1379 | * Check if we can merge with the plugged list before grabbing | |
1380 | * any locks. | |
1381 | */ | |
bd87b589 | 1382 | if (attempt_plug_merge(q, bio, &request_count)) |
5a7bbad2 | 1383 | return; |
1da177e4 | 1384 | |
73c10101 | 1385 | spin_lock_irq(q->queue_lock); |
2056a782 | 1386 | |
73c10101 JA |
1387 | el_ret = elv_merge(q, &req, bio); |
1388 | if (el_ret == ELEVATOR_BACK_MERGE) { | |
73c10101 | 1389 | if (bio_attempt_back_merge(q, req, bio)) { |
07c2bd37 | 1390 | elv_bio_merged(q, req, bio); |
73c10101 JA |
1391 | if (!attempt_back_merge(q, req)) |
1392 | elv_merged_request(q, req, el_ret); | |
1393 | goto out_unlock; | |
1394 | } | |
1395 | } else if (el_ret == ELEVATOR_FRONT_MERGE) { | |
73c10101 | 1396 | if (bio_attempt_front_merge(q, req, bio)) { |
07c2bd37 | 1397 | elv_bio_merged(q, req, bio); |
73c10101 JA |
1398 | if (!attempt_front_merge(q, req)) |
1399 | elv_merged_request(q, req, el_ret); | |
1400 | goto out_unlock; | |
80a761fd | 1401 | } |
1da177e4 LT |
1402 | } |
1403 | ||
450991bc | 1404 | get_rq: |
7749a8d4 JA |
1405 | /* |
1406 | * This sync check and mask will be re-done in init_request_from_bio(), | |
1407 | * but we need to set it earlier to expose the sync flag to the | |
1408 | * rq allocator and io schedulers. | |
1409 | */ | |
1410 | rw_flags = bio_data_dir(bio); | |
1411 | if (sync) | |
7b6d91da | 1412 | rw_flags |= REQ_SYNC; |
7749a8d4 | 1413 | |
1da177e4 | 1414 | /* |
450991bc | 1415 | * Grab a free request. This is might sleep but can not fail. |
d6344532 | 1416 | * Returns with the queue unlocked. |
450991bc | 1417 | */ |
7749a8d4 | 1418 | req = get_request_wait(q, rw_flags, bio); |
da8303c6 TH |
1419 | if (unlikely(!req)) { |
1420 | bio_endio(bio, -ENODEV); /* @q is dead */ | |
1421 | goto out_unlock; | |
1422 | } | |
d6344532 | 1423 | |
450991bc NP |
1424 | /* |
1425 | * After dropping the lock and possibly sleeping here, our request | |
1426 | * may now be mergeable after it had proven unmergeable (above). | |
1427 | * We don't worry about that case for efficiency. It won't happen | |
1428 | * often, and the elevators are able to handle it. | |
1da177e4 | 1429 | */ |
52d9e675 | 1430 | init_request_from_bio(req, bio); |
1da177e4 | 1431 | |
9562ad9a | 1432 | if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags)) |
11ccf116 | 1433 | req->cpu = raw_smp_processor_id(); |
73c10101 JA |
1434 | |
1435 | plug = current->plug; | |
721a9602 | 1436 | if (plug) { |
dc6d36c9 JA |
1437 | /* |
1438 | * If this is the first request added after a plug, fire | |
1439 | * of a plug trace. If others have been added before, check | |
1440 | * if we have multiple devices in this plug. If so, make a | |
1441 | * note to sort the list before dispatch. | |
1442 | */ | |
1443 | if (list_empty(&plug->list)) | |
1444 | trace_block_plug(q); | |
3540d5e8 SL |
1445 | else { |
1446 | if (!plug->should_sort) { | |
1447 | struct request *__rq; | |
73c10101 | 1448 | |
3540d5e8 SL |
1449 | __rq = list_entry_rq(plug->list.prev); |
1450 | if (__rq->q != q) | |
1451 | plug->should_sort = 1; | |
1452 | } | |
019ceb7d | 1453 | if (request_count >= BLK_MAX_REQUEST_COUNT) { |
3540d5e8 | 1454 | blk_flush_plug_list(plug, false); |
019ceb7d SL |
1455 | trace_block_plug(q); |
1456 | } | |
73c10101 | 1457 | } |
73c10101 JA |
1458 | list_add_tail(&req->queuelist, &plug->list); |
1459 | drive_stat_acct(req, 1); | |
1460 | } else { | |
1461 | spin_lock_irq(q->queue_lock); | |
1462 | add_acct_request(q, req, where); | |
24ecfbe2 | 1463 | __blk_run_queue(q); |
73c10101 JA |
1464 | out_unlock: |
1465 | spin_unlock_irq(q->queue_lock); | |
1466 | } | |
1da177e4 | 1467 | } |
c20e8de2 | 1468 | EXPORT_SYMBOL_GPL(blk_queue_bio); /* for device mapper only */ |
1da177e4 LT |
1469 | |
1470 | /* | |
1471 | * If bio->bi_dev is a partition, remap the location | |
1472 | */ | |
1473 | static inline void blk_partition_remap(struct bio *bio) | |
1474 | { | |
1475 | struct block_device *bdev = bio->bi_bdev; | |
1476 | ||
bf2de6f5 | 1477 | if (bio_sectors(bio) && bdev != bdev->bd_contains) { |
1da177e4 LT |
1478 | struct hd_struct *p = bdev->bd_part; |
1479 | ||
1da177e4 LT |
1480 | bio->bi_sector += p->start_sect; |
1481 | bio->bi_bdev = bdev->bd_contains; | |
c7149d6b | 1482 | |
d07335e5 MS |
1483 | trace_block_bio_remap(bdev_get_queue(bio->bi_bdev), bio, |
1484 | bdev->bd_dev, | |
1485 | bio->bi_sector - p->start_sect); | |
1da177e4 LT |
1486 | } |
1487 | } | |
1488 | ||
1da177e4 LT |
1489 | static void handle_bad_sector(struct bio *bio) |
1490 | { | |
1491 | char b[BDEVNAME_SIZE]; | |
1492 | ||
1493 | printk(KERN_INFO "attempt to access beyond end of device\n"); | |
1494 | printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n", | |
1495 | bdevname(bio->bi_bdev, b), | |
1496 | bio->bi_rw, | |
1497 | (unsigned long long)bio->bi_sector + bio_sectors(bio), | |
77304d2a | 1498 | (long long)(i_size_read(bio->bi_bdev->bd_inode) >> 9)); |
1da177e4 LT |
1499 | |
1500 | set_bit(BIO_EOF, &bio->bi_flags); | |
1501 | } | |
1502 | ||
c17bb495 AM |
1503 | #ifdef CONFIG_FAIL_MAKE_REQUEST |
1504 | ||
1505 | static DECLARE_FAULT_ATTR(fail_make_request); | |
1506 | ||
1507 | static int __init setup_fail_make_request(char *str) | |
1508 | { | |
1509 | return setup_fault_attr(&fail_make_request, str); | |
1510 | } | |
1511 | __setup("fail_make_request=", setup_fail_make_request); | |
1512 | ||
b2c9cd37 | 1513 | static bool should_fail_request(struct hd_struct *part, unsigned int bytes) |
c17bb495 | 1514 | { |
b2c9cd37 | 1515 | return part->make_it_fail && should_fail(&fail_make_request, bytes); |
c17bb495 AM |
1516 | } |
1517 | ||
1518 | static int __init fail_make_request_debugfs(void) | |
1519 | { | |
dd48c085 AM |
1520 | struct dentry *dir = fault_create_debugfs_attr("fail_make_request", |
1521 | NULL, &fail_make_request); | |
1522 | ||
1523 | return IS_ERR(dir) ? PTR_ERR(dir) : 0; | |
c17bb495 AM |
1524 | } |
1525 | ||
1526 | late_initcall(fail_make_request_debugfs); | |
1527 | ||
1528 | #else /* CONFIG_FAIL_MAKE_REQUEST */ | |
1529 | ||
b2c9cd37 AM |
1530 | static inline bool should_fail_request(struct hd_struct *part, |
1531 | unsigned int bytes) | |
c17bb495 | 1532 | { |
b2c9cd37 | 1533 | return false; |
c17bb495 AM |
1534 | } |
1535 | ||
1536 | #endif /* CONFIG_FAIL_MAKE_REQUEST */ | |
1537 | ||
c07e2b41 JA |
1538 | /* |
1539 | * Check whether this bio extends beyond the end of the device. | |
1540 | */ | |
1541 | static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors) | |
1542 | { | |
1543 | sector_t maxsector; | |
1544 | ||
1545 | if (!nr_sectors) | |
1546 | return 0; | |
1547 | ||
1548 | /* Test device or partition size, when known. */ | |
77304d2a | 1549 | maxsector = i_size_read(bio->bi_bdev->bd_inode) >> 9; |
c07e2b41 JA |
1550 | if (maxsector) { |
1551 | sector_t sector = bio->bi_sector; | |
1552 | ||
1553 | if (maxsector < nr_sectors || maxsector - nr_sectors < sector) { | |
1554 | /* | |
1555 | * This may well happen - the kernel calls bread() | |
1556 | * without checking the size of the device, e.g., when | |
1557 | * mounting a device. | |
1558 | */ | |
1559 | handle_bad_sector(bio); | |
1560 | return 1; | |
1561 | } | |
1562 | } | |
1563 | ||
1564 | return 0; | |
1565 | } | |
1566 | ||
27a84d54 CH |
1567 | static noinline_for_stack bool |
1568 | generic_make_request_checks(struct bio *bio) | |
1da177e4 | 1569 | { |
165125e1 | 1570 | struct request_queue *q; |
5a7bbad2 | 1571 | int nr_sectors = bio_sectors(bio); |
51fd77bd | 1572 | int err = -EIO; |
5a7bbad2 CH |
1573 | char b[BDEVNAME_SIZE]; |
1574 | struct hd_struct *part; | |
1da177e4 LT |
1575 | |
1576 | might_sleep(); | |
1da177e4 | 1577 | |
c07e2b41 JA |
1578 | if (bio_check_eod(bio, nr_sectors)) |
1579 | goto end_io; | |
1da177e4 | 1580 | |
5a7bbad2 CH |
1581 | q = bdev_get_queue(bio->bi_bdev); |
1582 | if (unlikely(!q)) { | |
1583 | printk(KERN_ERR | |
1584 | "generic_make_request: Trying to access " | |
1585 | "nonexistent block-device %s (%Lu)\n", | |
1586 | bdevname(bio->bi_bdev, b), | |
1587 | (long long) bio->bi_sector); | |
1588 | goto end_io; | |
1589 | } | |
c17bb495 | 1590 | |
5a7bbad2 CH |
1591 | if (unlikely(!(bio->bi_rw & REQ_DISCARD) && |
1592 | nr_sectors > queue_max_hw_sectors(q))) { | |
1593 | printk(KERN_ERR "bio too big device %s (%u > %u)\n", | |
1594 | bdevname(bio->bi_bdev, b), | |
1595 | bio_sectors(bio), | |
1596 | queue_max_hw_sectors(q)); | |
1597 | goto end_io; | |
1598 | } | |
1da177e4 | 1599 | |
5a7bbad2 CH |
1600 | part = bio->bi_bdev->bd_part; |
1601 | if (should_fail_request(part, bio->bi_size) || | |
1602 | should_fail_request(&part_to_disk(part)->part0, | |
1603 | bio->bi_size)) | |
1604 | goto end_io; | |
2056a782 | 1605 | |
5a7bbad2 CH |
1606 | /* |
1607 | * If this device has partitions, remap block n | |
1608 | * of partition p to block n+start(p) of the disk. | |
1609 | */ | |
1610 | blk_partition_remap(bio); | |
2056a782 | 1611 | |
5a7bbad2 CH |
1612 | if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) |
1613 | goto end_io; | |
a7384677 | 1614 | |
5a7bbad2 CH |
1615 | if (bio_check_eod(bio, nr_sectors)) |
1616 | goto end_io; | |
1e87901e | 1617 | |
5a7bbad2 CH |
1618 | /* |
1619 | * Filter flush bio's early so that make_request based | |
1620 | * drivers without flush support don't have to worry | |
1621 | * about them. | |
1622 | */ | |
1623 | if ((bio->bi_rw & (REQ_FLUSH | REQ_FUA)) && !q->flush_flags) { | |
1624 | bio->bi_rw &= ~(REQ_FLUSH | REQ_FUA); | |
1625 | if (!nr_sectors) { | |
1626 | err = 0; | |
51fd77bd JA |
1627 | goto end_io; |
1628 | } | |
5a7bbad2 | 1629 | } |
5ddfe969 | 1630 | |
5a7bbad2 CH |
1631 | if ((bio->bi_rw & REQ_DISCARD) && |
1632 | (!blk_queue_discard(q) || | |
1633 | ((bio->bi_rw & REQ_SECURE) && | |
1634 | !blk_queue_secdiscard(q)))) { | |
1635 | err = -EOPNOTSUPP; | |
1636 | goto end_io; | |
1637 | } | |
01edede4 | 1638 | |
bc16a4f9 TH |
1639 | if (blk_throtl_bio(q, bio)) |
1640 | return false; /* throttled, will be resubmitted later */ | |
27a84d54 | 1641 | |
5a7bbad2 | 1642 | trace_block_bio_queue(q, bio); |
27a84d54 | 1643 | return true; |
a7384677 TH |
1644 | |
1645 | end_io: | |
1646 | bio_endio(bio, err); | |
27a84d54 | 1647 | return false; |
1da177e4 LT |
1648 | } |
1649 | ||
27a84d54 CH |
1650 | /** |
1651 | * generic_make_request - hand a buffer to its device driver for I/O | |
1652 | * @bio: The bio describing the location in memory and on the device. | |
1653 | * | |
1654 | * generic_make_request() is used to make I/O requests of block | |
1655 | * devices. It is passed a &struct bio, which describes the I/O that needs | |
1656 | * to be done. | |
1657 | * | |
1658 | * generic_make_request() does not return any status. The | |
1659 | * success/failure status of the request, along with notification of | |
1660 | * completion, is delivered asynchronously through the bio->bi_end_io | |
1661 | * function described (one day) else where. | |
1662 | * | |
1663 | * The caller of generic_make_request must make sure that bi_io_vec | |
1664 | * are set to describe the memory buffer, and that bi_dev and bi_sector are | |
1665 | * set to describe the device address, and the | |
1666 | * bi_end_io and optionally bi_private are set to describe how | |
1667 | * completion notification should be signaled. | |
1668 | * | |
1669 | * generic_make_request and the drivers it calls may use bi_next if this | |
1670 | * bio happens to be merged with someone else, and may resubmit the bio to | |
1671 | * a lower device by calling into generic_make_request recursively, which | |
1672 | * means the bio should NOT be touched after the call to ->make_request_fn. | |
d89d8796 NB |
1673 | */ |
1674 | void generic_make_request(struct bio *bio) | |
1675 | { | |
bddd87c7 AM |
1676 | struct bio_list bio_list_on_stack; |
1677 | ||
27a84d54 CH |
1678 | if (!generic_make_request_checks(bio)) |
1679 | return; | |
1680 | ||
1681 | /* | |
1682 | * We only want one ->make_request_fn to be active at a time, else | |
1683 | * stack usage with stacked devices could be a problem. So use | |
1684 | * current->bio_list to keep a list of requests submited by a | |
1685 | * make_request_fn function. current->bio_list is also used as a | |
1686 | * flag to say if generic_make_request is currently active in this | |
1687 | * task or not. If it is NULL, then no make_request is active. If | |
1688 | * it is non-NULL, then a make_request is active, and new requests | |
1689 | * should be added at the tail | |
1690 | */ | |
bddd87c7 | 1691 | if (current->bio_list) { |
bddd87c7 | 1692 | bio_list_add(current->bio_list, bio); |
d89d8796 NB |
1693 | return; |
1694 | } | |
27a84d54 | 1695 | |
d89d8796 NB |
1696 | /* following loop may be a bit non-obvious, and so deserves some |
1697 | * explanation. | |
1698 | * Before entering the loop, bio->bi_next is NULL (as all callers | |
1699 | * ensure that) so we have a list with a single bio. | |
1700 | * We pretend that we have just taken it off a longer list, so | |
bddd87c7 AM |
1701 | * we assign bio_list to a pointer to the bio_list_on_stack, |
1702 | * thus initialising the bio_list of new bios to be | |
27a84d54 | 1703 | * added. ->make_request() may indeed add some more bios |
d89d8796 NB |
1704 | * through a recursive call to generic_make_request. If it |
1705 | * did, we find a non-NULL value in bio_list and re-enter the loop | |
1706 | * from the top. In this case we really did just take the bio | |
bddd87c7 | 1707 | * of the top of the list (no pretending) and so remove it from |
27a84d54 | 1708 | * bio_list, and call into ->make_request() again. |
d89d8796 NB |
1709 | */ |
1710 | BUG_ON(bio->bi_next); | |
bddd87c7 AM |
1711 | bio_list_init(&bio_list_on_stack); |
1712 | current->bio_list = &bio_list_on_stack; | |
d89d8796 | 1713 | do { |
27a84d54 CH |
1714 | struct request_queue *q = bdev_get_queue(bio->bi_bdev); |
1715 | ||
1716 | q->make_request_fn(q, bio); | |
1717 | ||
bddd87c7 | 1718 | bio = bio_list_pop(current->bio_list); |
d89d8796 | 1719 | } while (bio); |
bddd87c7 | 1720 | current->bio_list = NULL; /* deactivate */ |
d89d8796 | 1721 | } |
1da177e4 LT |
1722 | EXPORT_SYMBOL(generic_make_request); |
1723 | ||
1724 | /** | |
710027a4 | 1725 | * submit_bio - submit a bio to the block device layer for I/O |
1da177e4 LT |
1726 | * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead) |
1727 | * @bio: The &struct bio which describes the I/O | |
1728 | * | |
1729 | * submit_bio() is very similar in purpose to generic_make_request(), and | |
1730 | * uses that function to do most of the work. Both are fairly rough | |
710027a4 | 1731 | * interfaces; @bio must be presetup and ready for I/O. |
1da177e4 LT |
1732 | * |
1733 | */ | |
1734 | void submit_bio(int rw, struct bio *bio) | |
1735 | { | |
1736 | int count = bio_sectors(bio); | |
1737 | ||
22e2c507 | 1738 | bio->bi_rw |= rw; |
1da177e4 | 1739 | |
bf2de6f5 JA |
1740 | /* |
1741 | * If it's a regular read/write or a barrier with data attached, | |
1742 | * go through the normal accounting stuff before submission. | |
1743 | */ | |
3ffb52e7 | 1744 | if (bio_has_data(bio) && !(rw & REQ_DISCARD)) { |
bf2de6f5 JA |
1745 | if (rw & WRITE) { |
1746 | count_vm_events(PGPGOUT, count); | |
1747 | } else { | |
1748 | task_io_account_read(bio->bi_size); | |
1749 | count_vm_events(PGPGIN, count); | |
1750 | } | |
1751 | ||
1752 | if (unlikely(block_dump)) { | |
1753 | char b[BDEVNAME_SIZE]; | |
8dcbdc74 | 1754 | printk(KERN_DEBUG "%s(%d): %s block %Lu on %s (%u sectors)\n", |
ba25f9dc | 1755 | current->comm, task_pid_nr(current), |
bf2de6f5 JA |
1756 | (rw & WRITE) ? "WRITE" : "READ", |
1757 | (unsigned long long)bio->bi_sector, | |
8dcbdc74 SM |
1758 | bdevname(bio->bi_bdev, b), |
1759 | count); | |
bf2de6f5 | 1760 | } |
1da177e4 LT |
1761 | } |
1762 | ||
1763 | generic_make_request(bio); | |
1764 | } | |
1da177e4 LT |
1765 | EXPORT_SYMBOL(submit_bio); |
1766 | ||
82124d60 KU |
1767 | /** |
1768 | * blk_rq_check_limits - Helper function to check a request for the queue limit | |
1769 | * @q: the queue | |
1770 | * @rq: the request being checked | |
1771 | * | |
1772 | * Description: | |
1773 | * @rq may have been made based on weaker limitations of upper-level queues | |
1774 | * in request stacking drivers, and it may violate the limitation of @q. | |
1775 | * Since the block layer and the underlying device driver trust @rq | |
1776 | * after it is inserted to @q, it should be checked against @q before | |
1777 | * the insertion using this generic function. | |
1778 | * | |
1779 | * This function should also be useful for request stacking drivers | |
eef35c2d | 1780 | * in some cases below, so export this function. |
82124d60 KU |
1781 | * Request stacking drivers like request-based dm may change the queue |
1782 | * limits while requests are in the queue (e.g. dm's table swapping). | |
1783 | * Such request stacking drivers should check those requests agaist | |
1784 | * the new queue limits again when they dispatch those requests, | |
1785 | * although such checkings are also done against the old queue limits | |
1786 | * when submitting requests. | |
1787 | */ | |
1788 | int blk_rq_check_limits(struct request_queue *q, struct request *rq) | |
1789 | { | |
3383977f S |
1790 | if (rq->cmd_flags & REQ_DISCARD) |
1791 | return 0; | |
1792 | ||
ae03bf63 MP |
1793 | if (blk_rq_sectors(rq) > queue_max_sectors(q) || |
1794 | blk_rq_bytes(rq) > queue_max_hw_sectors(q) << 9) { | |
82124d60 KU |
1795 | printk(KERN_ERR "%s: over max size limit.\n", __func__); |
1796 | return -EIO; | |
1797 | } | |
1798 | ||
1799 | /* | |
1800 | * queue's settings related to segment counting like q->bounce_pfn | |
1801 | * may differ from that of other stacking queues. | |
1802 | * Recalculate it to check the request correctly on this queue's | |
1803 | * limitation. | |
1804 | */ | |
1805 | blk_recalc_rq_segments(rq); | |
8a78362c | 1806 | if (rq->nr_phys_segments > queue_max_segments(q)) { |
82124d60 KU |
1807 | printk(KERN_ERR "%s: over max segments limit.\n", __func__); |
1808 | return -EIO; | |
1809 | } | |
1810 | ||
1811 | return 0; | |
1812 | } | |
1813 | EXPORT_SYMBOL_GPL(blk_rq_check_limits); | |
1814 | ||
1815 | /** | |
1816 | * blk_insert_cloned_request - Helper for stacking drivers to submit a request | |
1817 | * @q: the queue to submit the request | |
1818 | * @rq: the request being queued | |
1819 | */ | |
1820 | int blk_insert_cloned_request(struct request_queue *q, struct request *rq) | |
1821 | { | |
1822 | unsigned long flags; | |
4853abaa | 1823 | int where = ELEVATOR_INSERT_BACK; |
82124d60 KU |
1824 | |
1825 | if (blk_rq_check_limits(q, rq)) | |
1826 | return -EIO; | |
1827 | ||
b2c9cd37 AM |
1828 | if (rq->rq_disk && |
1829 | should_fail_request(&rq->rq_disk->part0, blk_rq_bytes(rq))) | |
82124d60 | 1830 | return -EIO; |
82124d60 KU |
1831 | |
1832 | spin_lock_irqsave(q->queue_lock, flags); | |
8ba61435 TH |
1833 | if (unlikely(blk_queue_dead(q))) { |
1834 | spin_unlock_irqrestore(q->queue_lock, flags); | |
1835 | return -ENODEV; | |
1836 | } | |
82124d60 KU |
1837 | |
1838 | /* | |
1839 | * Submitting request must be dequeued before calling this function | |
1840 | * because it will be linked to another request_queue | |
1841 | */ | |
1842 | BUG_ON(blk_queued_rq(rq)); | |
1843 | ||
4853abaa JM |
1844 | if (rq->cmd_flags & (REQ_FLUSH|REQ_FUA)) |
1845 | where = ELEVATOR_INSERT_FLUSH; | |
1846 | ||
1847 | add_acct_request(q, rq, where); | |
e67b77c7 JM |
1848 | if (where == ELEVATOR_INSERT_FLUSH) |
1849 | __blk_run_queue(q); | |
82124d60 KU |
1850 | spin_unlock_irqrestore(q->queue_lock, flags); |
1851 | ||
1852 | return 0; | |
1853 | } | |
1854 | EXPORT_SYMBOL_GPL(blk_insert_cloned_request); | |
1855 | ||
80a761fd TH |
1856 | /** |
1857 | * blk_rq_err_bytes - determine number of bytes till the next failure boundary | |
1858 | * @rq: request to examine | |
1859 | * | |
1860 | * Description: | |
1861 | * A request could be merge of IOs which require different failure | |
1862 | * handling. This function determines the number of bytes which | |
1863 | * can be failed from the beginning of the request without | |
1864 | * crossing into area which need to be retried further. | |
1865 | * | |
1866 | * Return: | |
1867 | * The number of bytes to fail. | |
1868 | * | |
1869 | * Context: | |
1870 | * queue_lock must be held. | |
1871 | */ | |
1872 | unsigned int blk_rq_err_bytes(const struct request *rq) | |
1873 | { | |
1874 | unsigned int ff = rq->cmd_flags & REQ_FAILFAST_MASK; | |
1875 | unsigned int bytes = 0; | |
1876 | struct bio *bio; | |
1877 | ||
1878 | if (!(rq->cmd_flags & REQ_MIXED_MERGE)) | |
1879 | return blk_rq_bytes(rq); | |
1880 | ||
1881 | /* | |
1882 | * Currently the only 'mixing' which can happen is between | |
1883 | * different fastfail types. We can safely fail portions | |
1884 | * which have all the failfast bits that the first one has - | |
1885 | * the ones which are at least as eager to fail as the first | |
1886 | * one. | |
1887 | */ | |
1888 | for (bio = rq->bio; bio; bio = bio->bi_next) { | |
1889 | if ((bio->bi_rw & ff) != ff) | |
1890 | break; | |
1891 | bytes += bio->bi_size; | |
1892 | } | |
1893 | ||
1894 | /* this could lead to infinite loop */ | |
1895 | BUG_ON(blk_rq_bytes(rq) && !bytes); | |
1896 | return bytes; | |
1897 | } | |
1898 | EXPORT_SYMBOL_GPL(blk_rq_err_bytes); | |
1899 | ||
bc58ba94 JA |
1900 | static void blk_account_io_completion(struct request *req, unsigned int bytes) |
1901 | { | |
c2553b58 | 1902 | if (blk_do_io_stat(req)) { |
bc58ba94 JA |
1903 | const int rw = rq_data_dir(req); |
1904 | struct hd_struct *part; | |
1905 | int cpu; | |
1906 | ||
1907 | cpu = part_stat_lock(); | |
09e099d4 | 1908 | part = req->part; |
bc58ba94 JA |
1909 | part_stat_add(cpu, part, sectors[rw], bytes >> 9); |
1910 | part_stat_unlock(); | |
1911 | } | |
1912 | } | |
1913 | ||
1914 | static void blk_account_io_done(struct request *req) | |
1915 | { | |
bc58ba94 | 1916 | /* |
dd4c133f TH |
1917 | * Account IO completion. flush_rq isn't accounted as a |
1918 | * normal IO on queueing nor completion. Accounting the | |
1919 | * containing request is enough. | |
bc58ba94 | 1920 | */ |
414b4ff5 | 1921 | if (blk_do_io_stat(req) && !(req->cmd_flags & REQ_FLUSH_SEQ)) { |
bc58ba94 JA |
1922 | unsigned long duration = jiffies - req->start_time; |
1923 | const int rw = rq_data_dir(req); | |
1924 | struct hd_struct *part; | |
1925 | int cpu; | |
1926 | ||
1927 | cpu = part_stat_lock(); | |
09e099d4 | 1928 | part = req->part; |
bc58ba94 JA |
1929 | |
1930 | part_stat_inc(cpu, part, ios[rw]); | |
1931 | part_stat_add(cpu, part, ticks[rw], duration); | |
1932 | part_round_stats(cpu, part); | |
316d315b | 1933 | part_dec_in_flight(part, rw); |
bc58ba94 | 1934 | |
6c23a968 | 1935 | hd_struct_put(part); |
bc58ba94 JA |
1936 | part_stat_unlock(); |
1937 | } | |
1938 | } | |
1939 | ||
3bcddeac | 1940 | /** |
9934c8c0 TH |
1941 | * blk_peek_request - peek at the top of a request queue |
1942 | * @q: request queue to peek at | |
1943 | * | |
1944 | * Description: | |
1945 | * Return the request at the top of @q. The returned request | |
1946 | * should be started using blk_start_request() before LLD starts | |
1947 | * processing it. | |
1948 | * | |
1949 | * Return: | |
1950 | * Pointer to the request at the top of @q if available. Null | |
1951 | * otherwise. | |
1952 | * | |
1953 | * Context: | |
1954 | * queue_lock must be held. | |
1955 | */ | |
1956 | struct request *blk_peek_request(struct request_queue *q) | |
158dbda0 TH |
1957 | { |
1958 | struct request *rq; | |
1959 | int ret; | |
1960 | ||
1961 | while ((rq = __elv_next_request(q)) != NULL) { | |
1962 | if (!(rq->cmd_flags & REQ_STARTED)) { | |
1963 | /* | |
1964 | * This is the first time the device driver | |
1965 | * sees this request (possibly after | |
1966 | * requeueing). Notify IO scheduler. | |
1967 | */ | |
33659ebb | 1968 | if (rq->cmd_flags & REQ_SORTED) |
158dbda0 TH |
1969 | elv_activate_rq(q, rq); |
1970 | ||
1971 | /* | |
1972 | * just mark as started even if we don't start | |
1973 | * it, a request that has been delayed should | |
1974 | * not be passed by new incoming requests | |
1975 | */ | |
1976 | rq->cmd_flags |= REQ_STARTED; | |
1977 | trace_block_rq_issue(q, rq); | |
1978 | } | |
1979 | ||
1980 | if (!q->boundary_rq || q->boundary_rq == rq) { | |
1981 | q->end_sector = rq_end_sector(rq); | |
1982 | q->boundary_rq = NULL; | |
1983 | } | |
1984 | ||
1985 | if (rq->cmd_flags & REQ_DONTPREP) | |
1986 | break; | |
1987 | ||
2e46e8b2 | 1988 | if (q->dma_drain_size && blk_rq_bytes(rq)) { |
158dbda0 TH |
1989 | /* |
1990 | * make sure space for the drain appears we | |
1991 | * know we can do this because max_hw_segments | |
1992 | * has been adjusted to be one fewer than the | |
1993 | * device can handle | |
1994 | */ | |
1995 | rq->nr_phys_segments++; | |
1996 | } | |
1997 | ||
1998 | if (!q->prep_rq_fn) | |
1999 | break; | |
2000 | ||
2001 | ret = q->prep_rq_fn(q, rq); | |
2002 | if (ret == BLKPREP_OK) { | |
2003 | break; | |
2004 | } else if (ret == BLKPREP_DEFER) { | |
2005 | /* | |
2006 | * the request may have been (partially) prepped. | |
2007 | * we need to keep this request in the front to | |
2008 | * avoid resource deadlock. REQ_STARTED will | |
2009 | * prevent other fs requests from passing this one. | |
2010 | */ | |
2e46e8b2 | 2011 | if (q->dma_drain_size && blk_rq_bytes(rq) && |
158dbda0 TH |
2012 | !(rq->cmd_flags & REQ_DONTPREP)) { |
2013 | /* | |
2014 | * remove the space for the drain we added | |
2015 | * so that we don't add it again | |
2016 | */ | |
2017 | --rq->nr_phys_segments; | |
2018 | } | |
2019 | ||
2020 | rq = NULL; | |
2021 | break; | |
2022 | } else if (ret == BLKPREP_KILL) { | |
2023 | rq->cmd_flags |= REQ_QUIET; | |
c143dc90 JB |
2024 | /* |
2025 | * Mark this request as started so we don't trigger | |
2026 | * any debug logic in the end I/O path. | |
2027 | */ | |
2028 | blk_start_request(rq); | |
40cbbb78 | 2029 | __blk_end_request_all(rq, -EIO); |
158dbda0 TH |
2030 | } else { |
2031 | printk(KERN_ERR "%s: bad return=%d\n", __func__, ret); | |
2032 | break; | |
2033 | } | |
2034 | } | |
2035 | ||
2036 | return rq; | |
2037 | } | |
9934c8c0 | 2038 | EXPORT_SYMBOL(blk_peek_request); |
158dbda0 | 2039 | |
9934c8c0 | 2040 | void blk_dequeue_request(struct request *rq) |
158dbda0 | 2041 | { |
9934c8c0 TH |
2042 | struct request_queue *q = rq->q; |
2043 | ||
158dbda0 TH |
2044 | BUG_ON(list_empty(&rq->queuelist)); |
2045 | BUG_ON(ELV_ON_HASH(rq)); | |
2046 | ||
2047 | list_del_init(&rq->queuelist); | |
2048 | ||
2049 | /* | |
2050 | * the time frame between a request being removed from the lists | |
2051 | * and to it is freed is accounted as io that is in progress at | |
2052 | * the driver side. | |
2053 | */ | |
9195291e | 2054 | if (blk_account_rq(rq)) { |
0a7ae2ff | 2055 | q->in_flight[rq_is_sync(rq)]++; |
9195291e DS |
2056 | set_io_start_time_ns(rq); |
2057 | } | |
158dbda0 TH |
2058 | } |
2059 | ||
9934c8c0 TH |
2060 | /** |
2061 | * blk_start_request - start request processing on the driver | |
2062 | * @req: request to dequeue | |
2063 | * | |
2064 | * Description: | |
2065 | * Dequeue @req and start timeout timer on it. This hands off the | |
2066 | * request to the driver. | |
2067 | * | |
2068 | * Block internal functions which don't want to start timer should | |
2069 | * call blk_dequeue_request(). | |
2070 | * | |
2071 | * Context: | |
2072 | * queue_lock must be held. | |
2073 | */ | |
2074 | void blk_start_request(struct request *req) | |
2075 | { | |
2076 | blk_dequeue_request(req); | |
2077 | ||
2078 | /* | |
5f49f631 TH |
2079 | * We are now handing the request to the hardware, initialize |
2080 | * resid_len to full count and add the timeout handler. | |
9934c8c0 | 2081 | */ |
5f49f631 | 2082 | req->resid_len = blk_rq_bytes(req); |
dbb66c4b FT |
2083 | if (unlikely(blk_bidi_rq(req))) |
2084 | req->next_rq->resid_len = blk_rq_bytes(req->next_rq); | |
2085 | ||
9934c8c0 TH |
2086 | blk_add_timer(req); |
2087 | } | |
2088 | EXPORT_SYMBOL(blk_start_request); | |
2089 | ||
2090 | /** | |
2091 | * blk_fetch_request - fetch a request from a request queue | |
2092 | * @q: request queue to fetch a request from | |
2093 | * | |
2094 | * Description: | |
2095 | * Return the request at the top of @q. The request is started on | |
2096 | * return and LLD can start processing it immediately. | |
2097 | * | |
2098 | * Return: | |
2099 | * Pointer to the request at the top of @q if available. Null | |
2100 | * otherwise. | |
2101 | * | |
2102 | * Context: | |
2103 | * queue_lock must be held. | |
2104 | */ | |
2105 | struct request *blk_fetch_request(struct request_queue *q) | |
2106 | { | |
2107 | struct request *rq; | |
2108 | ||
2109 | rq = blk_peek_request(q); | |
2110 | if (rq) | |
2111 | blk_start_request(rq); | |
2112 | return rq; | |
2113 | } | |
2114 | EXPORT_SYMBOL(blk_fetch_request); | |
2115 | ||
3bcddeac | 2116 | /** |
2e60e022 | 2117 | * blk_update_request - Special helper function for request stacking drivers |
8ebf9756 | 2118 | * @req: the request being processed |
710027a4 | 2119 | * @error: %0 for success, < %0 for error |
8ebf9756 | 2120 | * @nr_bytes: number of bytes to complete @req |
3bcddeac KU |
2121 | * |
2122 | * Description: | |
8ebf9756 RD |
2123 | * Ends I/O on a number of bytes attached to @req, but doesn't complete |
2124 | * the request structure even if @req doesn't have leftover. | |
2125 | * If @req has leftover, sets it up for the next range of segments. | |
2e60e022 TH |
2126 | * |
2127 | * This special helper function is only for request stacking drivers | |
2128 | * (e.g. request-based dm) so that they can handle partial completion. | |
2129 | * Actual device drivers should use blk_end_request instead. | |
2130 | * | |
2131 | * Passing the result of blk_rq_bytes() as @nr_bytes guarantees | |
2132 | * %false return from this function. | |
3bcddeac KU |
2133 | * |
2134 | * Return: | |
2e60e022 TH |
2135 | * %false - this request doesn't have any more data |
2136 | * %true - this request has more data | |
3bcddeac | 2137 | **/ |
2e60e022 | 2138 | bool blk_update_request(struct request *req, int error, unsigned int nr_bytes) |
1da177e4 | 2139 | { |
5450d3e1 | 2140 | int total_bytes, bio_nbytes, next_idx = 0; |
1da177e4 LT |
2141 | struct bio *bio; |
2142 | ||
2e60e022 TH |
2143 | if (!req->bio) |
2144 | return false; | |
2145 | ||
5f3ea37c | 2146 | trace_block_rq_complete(req->q, req); |
2056a782 | 2147 | |
1da177e4 | 2148 | /* |
6f41469c TH |
2149 | * For fs requests, rq is just carrier of independent bio's |
2150 | * and each partial completion should be handled separately. | |
2151 | * Reset per-request error on each partial completion. | |
2152 | * | |
2153 | * TODO: tj: This is too subtle. It would be better to let | |
2154 | * low level drivers do what they see fit. | |
1da177e4 | 2155 | */ |
33659ebb | 2156 | if (req->cmd_type == REQ_TYPE_FS) |
1da177e4 LT |
2157 | req->errors = 0; |
2158 | ||
33659ebb CH |
2159 | if (error && req->cmd_type == REQ_TYPE_FS && |
2160 | !(req->cmd_flags & REQ_QUIET)) { | |
79775567 HR |
2161 | char *error_type; |
2162 | ||
2163 | switch (error) { | |
2164 | case -ENOLINK: | |
2165 | error_type = "recoverable transport"; | |
2166 | break; | |
2167 | case -EREMOTEIO: | |
2168 | error_type = "critical target"; | |
2169 | break; | |
2170 | case -EBADE: | |
2171 | error_type = "critical nexus"; | |
2172 | break; | |
2173 | case -EIO: | |
2174 | default: | |
2175 | error_type = "I/O"; | |
2176 | break; | |
2177 | } | |
2178 | printk(KERN_ERR "end_request: %s error, dev %s, sector %llu\n", | |
2179 | error_type, req->rq_disk ? req->rq_disk->disk_name : "?", | |
2180 | (unsigned long long)blk_rq_pos(req)); | |
1da177e4 LT |
2181 | } |
2182 | ||
bc58ba94 | 2183 | blk_account_io_completion(req, nr_bytes); |
d72d904a | 2184 | |
1da177e4 LT |
2185 | total_bytes = bio_nbytes = 0; |
2186 | while ((bio = req->bio) != NULL) { | |
2187 | int nbytes; | |
2188 | ||
2189 | if (nr_bytes >= bio->bi_size) { | |
2190 | req->bio = bio->bi_next; | |
2191 | nbytes = bio->bi_size; | |
5bb23a68 | 2192 | req_bio_endio(req, bio, nbytes, error); |
1da177e4 LT |
2193 | next_idx = 0; |
2194 | bio_nbytes = 0; | |
2195 | } else { | |
2196 | int idx = bio->bi_idx + next_idx; | |
2197 | ||
af498d7f | 2198 | if (unlikely(idx >= bio->bi_vcnt)) { |
1da177e4 | 2199 | blk_dump_rq_flags(req, "__end_that"); |
6728cb0e | 2200 | printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n", |
af498d7f | 2201 | __func__, idx, bio->bi_vcnt); |
1da177e4 LT |
2202 | break; |
2203 | } | |
2204 | ||
2205 | nbytes = bio_iovec_idx(bio, idx)->bv_len; | |
2206 | BIO_BUG_ON(nbytes > bio->bi_size); | |
2207 | ||
2208 | /* | |
2209 | * not a complete bvec done | |
2210 | */ | |
2211 | if (unlikely(nbytes > nr_bytes)) { | |
2212 | bio_nbytes += nr_bytes; | |
2213 | total_bytes += nr_bytes; | |
2214 | break; | |
2215 | } | |
2216 | ||
2217 | /* | |
2218 | * advance to the next vector | |
2219 | */ | |
2220 | next_idx++; | |
2221 | bio_nbytes += nbytes; | |
2222 | } | |
2223 | ||
2224 | total_bytes += nbytes; | |
2225 | nr_bytes -= nbytes; | |
2226 | ||
6728cb0e JA |
2227 | bio = req->bio; |
2228 | if (bio) { | |
1da177e4 LT |
2229 | /* |
2230 | * end more in this run, or just return 'not-done' | |
2231 | */ | |
2232 | if (unlikely(nr_bytes <= 0)) | |
2233 | break; | |
2234 | } | |
2235 | } | |
2236 | ||
2237 | /* | |
2238 | * completely done | |
2239 | */ | |
2e60e022 TH |
2240 | if (!req->bio) { |
2241 | /* | |
2242 | * Reset counters so that the request stacking driver | |
2243 | * can find how many bytes remain in the request | |
2244 | * later. | |
2245 | */ | |
a2dec7b3 | 2246 | req->__data_len = 0; |
2e60e022 TH |
2247 | return false; |
2248 | } | |
1da177e4 LT |
2249 | |
2250 | /* | |
2251 | * if the request wasn't completed, update state | |
2252 | */ | |
2253 | if (bio_nbytes) { | |
5bb23a68 | 2254 | req_bio_endio(req, bio, bio_nbytes, error); |
1da177e4 LT |
2255 | bio->bi_idx += next_idx; |
2256 | bio_iovec(bio)->bv_offset += nr_bytes; | |
2257 | bio_iovec(bio)->bv_len -= nr_bytes; | |
2258 | } | |
2259 | ||
a2dec7b3 | 2260 | req->__data_len -= total_bytes; |
2e46e8b2 TH |
2261 | req->buffer = bio_data(req->bio); |
2262 | ||
2263 | /* update sector only for requests with clear definition of sector */ | |
33659ebb | 2264 | if (req->cmd_type == REQ_TYPE_FS || (req->cmd_flags & REQ_DISCARD)) |
a2dec7b3 | 2265 | req->__sector += total_bytes >> 9; |
2e46e8b2 | 2266 | |
80a761fd TH |
2267 | /* mixed attributes always follow the first bio */ |
2268 | if (req->cmd_flags & REQ_MIXED_MERGE) { | |
2269 | req->cmd_flags &= ~REQ_FAILFAST_MASK; | |
2270 | req->cmd_flags |= req->bio->bi_rw & REQ_FAILFAST_MASK; | |
2271 | } | |
2272 | ||
2e46e8b2 TH |
2273 | /* |
2274 | * If total number of sectors is less than the first segment | |
2275 | * size, something has gone terribly wrong. | |
2276 | */ | |
2277 | if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) { | |
8182924b | 2278 | blk_dump_rq_flags(req, "request botched"); |
a2dec7b3 | 2279 | req->__data_len = blk_rq_cur_bytes(req); |
2e46e8b2 TH |
2280 | } |
2281 | ||
2282 | /* recalculate the number of segments */ | |
1da177e4 | 2283 | blk_recalc_rq_segments(req); |
2e46e8b2 | 2284 | |
2e60e022 | 2285 | return true; |
1da177e4 | 2286 | } |
2e60e022 | 2287 | EXPORT_SYMBOL_GPL(blk_update_request); |
1da177e4 | 2288 | |
2e60e022 TH |
2289 | static bool blk_update_bidi_request(struct request *rq, int error, |
2290 | unsigned int nr_bytes, | |
2291 | unsigned int bidi_bytes) | |
5efccd17 | 2292 | { |
2e60e022 TH |
2293 | if (blk_update_request(rq, error, nr_bytes)) |
2294 | return true; | |
5efccd17 | 2295 | |
2e60e022 TH |
2296 | /* Bidi request must be completed as a whole */ |
2297 | if (unlikely(blk_bidi_rq(rq)) && | |
2298 | blk_update_request(rq->next_rq, error, bidi_bytes)) | |
2299 | return true; | |
5efccd17 | 2300 | |
e2e1a148 JA |
2301 | if (blk_queue_add_random(rq->q)) |
2302 | add_disk_randomness(rq->rq_disk); | |
2e60e022 TH |
2303 | |
2304 | return false; | |
1da177e4 LT |
2305 | } |
2306 | ||
28018c24 JB |
2307 | /** |
2308 | * blk_unprep_request - unprepare a request | |
2309 | * @req: the request | |
2310 | * | |
2311 | * This function makes a request ready for complete resubmission (or | |
2312 | * completion). It happens only after all error handling is complete, | |
2313 | * so represents the appropriate moment to deallocate any resources | |
2314 | * that were allocated to the request in the prep_rq_fn. The queue | |
2315 | * lock is held when calling this. | |
2316 | */ | |
2317 | void blk_unprep_request(struct request *req) | |
2318 | { | |
2319 | struct request_queue *q = req->q; | |
2320 | ||
2321 | req->cmd_flags &= ~REQ_DONTPREP; | |
2322 | if (q->unprep_rq_fn) | |
2323 | q->unprep_rq_fn(q, req); | |
2324 | } | |
2325 | EXPORT_SYMBOL_GPL(blk_unprep_request); | |
2326 | ||
1da177e4 LT |
2327 | /* |
2328 | * queue lock must be held | |
2329 | */ | |
2e60e022 | 2330 | static void blk_finish_request(struct request *req, int error) |
1da177e4 | 2331 | { |
b8286239 KU |
2332 | if (blk_rq_tagged(req)) |
2333 | blk_queue_end_tag(req->q, req); | |
2334 | ||
ba396a6c | 2335 | BUG_ON(blk_queued_rq(req)); |
1da177e4 | 2336 | |
33659ebb | 2337 | if (unlikely(laptop_mode) && req->cmd_type == REQ_TYPE_FS) |
31373d09 | 2338 | laptop_io_completion(&req->q->backing_dev_info); |
1da177e4 | 2339 | |
e78042e5 MA |
2340 | blk_delete_timer(req); |
2341 | ||
28018c24 JB |
2342 | if (req->cmd_flags & REQ_DONTPREP) |
2343 | blk_unprep_request(req); | |
2344 | ||
2345 | ||
bc58ba94 | 2346 | blk_account_io_done(req); |
b8286239 | 2347 | |
1da177e4 | 2348 | if (req->end_io) |
8ffdc655 | 2349 | req->end_io(req, error); |
b8286239 KU |
2350 | else { |
2351 | if (blk_bidi_rq(req)) | |
2352 | __blk_put_request(req->next_rq->q, req->next_rq); | |
2353 | ||
1da177e4 | 2354 | __blk_put_request(req->q, req); |
b8286239 | 2355 | } |
1da177e4 LT |
2356 | } |
2357 | ||
3b11313a | 2358 | /** |
2e60e022 TH |
2359 | * blk_end_bidi_request - Complete a bidi request |
2360 | * @rq: the request to complete | |
2361 | * @error: %0 for success, < %0 for error | |
2362 | * @nr_bytes: number of bytes to complete @rq | |
2363 | * @bidi_bytes: number of bytes to complete @rq->next_rq | |
a0cd1285 JA |
2364 | * |
2365 | * Description: | |
e3a04fe3 | 2366 | * Ends I/O on a number of bytes attached to @rq and @rq->next_rq. |
2e60e022 TH |
2367 | * Drivers that supports bidi can safely call this member for any |
2368 | * type of request, bidi or uni. In the later case @bidi_bytes is | |
2369 | * just ignored. | |
336cdb40 KU |
2370 | * |
2371 | * Return: | |
2e60e022 TH |
2372 | * %false - we are done with this request |
2373 | * %true - still buffers pending for this request | |
a0cd1285 | 2374 | **/ |
b1f74493 | 2375 | static bool blk_end_bidi_request(struct request *rq, int error, |
32fab448 KU |
2376 | unsigned int nr_bytes, unsigned int bidi_bytes) |
2377 | { | |
336cdb40 | 2378 | struct request_queue *q = rq->q; |
2e60e022 | 2379 | unsigned long flags; |
32fab448 | 2380 | |
2e60e022 TH |
2381 | if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes)) |
2382 | return true; | |
32fab448 | 2383 | |
336cdb40 | 2384 | spin_lock_irqsave(q->queue_lock, flags); |
2e60e022 | 2385 | blk_finish_request(rq, error); |
336cdb40 KU |
2386 | spin_unlock_irqrestore(q->queue_lock, flags); |
2387 | ||
2e60e022 | 2388 | return false; |
32fab448 KU |
2389 | } |
2390 | ||
336cdb40 | 2391 | /** |
2e60e022 TH |
2392 | * __blk_end_bidi_request - Complete a bidi request with queue lock held |
2393 | * @rq: the request to complete | |
710027a4 | 2394 | * @error: %0 for success, < %0 for error |
e3a04fe3 KU |
2395 | * @nr_bytes: number of bytes to complete @rq |
2396 | * @bidi_bytes: number of bytes to complete @rq->next_rq | |
336cdb40 KU |
2397 | * |
2398 | * Description: | |
2e60e022 TH |
2399 | * Identical to blk_end_bidi_request() except that queue lock is |
2400 | * assumed to be locked on entry and remains so on return. | |
336cdb40 KU |
2401 | * |
2402 | * Return: | |
2e60e022 TH |
2403 | * %false - we are done with this request |
2404 | * %true - still buffers pending for this request | |
336cdb40 | 2405 | **/ |
4853abaa | 2406 | bool __blk_end_bidi_request(struct request *rq, int error, |
b1f74493 | 2407 | unsigned int nr_bytes, unsigned int bidi_bytes) |
336cdb40 | 2408 | { |
2e60e022 TH |
2409 | if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes)) |
2410 | return true; | |
336cdb40 | 2411 | |
2e60e022 | 2412 | blk_finish_request(rq, error); |
336cdb40 | 2413 | |
2e60e022 | 2414 | return false; |
336cdb40 | 2415 | } |
e19a3ab0 KU |
2416 | |
2417 | /** | |
2418 | * blk_end_request - Helper function for drivers to complete the request. | |
2419 | * @rq: the request being processed | |
710027a4 | 2420 | * @error: %0 for success, < %0 for error |
e19a3ab0 KU |
2421 | * @nr_bytes: number of bytes to complete |
2422 | * | |
2423 | * Description: | |
2424 | * Ends I/O on a number of bytes attached to @rq. | |
2425 | * If @rq has leftover, sets it up for the next range of segments. | |
2426 | * | |
2427 | * Return: | |
b1f74493 FT |
2428 | * %false - we are done with this request |
2429 | * %true - still buffers pending for this request | |
e19a3ab0 | 2430 | **/ |
b1f74493 | 2431 | bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes) |
e19a3ab0 | 2432 | { |
b1f74493 | 2433 | return blk_end_bidi_request(rq, error, nr_bytes, 0); |
e19a3ab0 | 2434 | } |
56ad1740 | 2435 | EXPORT_SYMBOL(blk_end_request); |
336cdb40 KU |
2436 | |
2437 | /** | |
b1f74493 FT |
2438 | * blk_end_request_all - Helper function for drives to finish the request. |
2439 | * @rq: the request to finish | |
8ebf9756 | 2440 | * @error: %0 for success, < %0 for error |
336cdb40 KU |
2441 | * |
2442 | * Description: | |
b1f74493 FT |
2443 | * Completely finish @rq. |
2444 | */ | |
2445 | void blk_end_request_all(struct request *rq, int error) | |
336cdb40 | 2446 | { |
b1f74493 FT |
2447 | bool pending; |
2448 | unsigned int bidi_bytes = 0; | |
336cdb40 | 2449 | |
b1f74493 FT |
2450 | if (unlikely(blk_bidi_rq(rq))) |
2451 | bidi_bytes = blk_rq_bytes(rq->next_rq); | |
336cdb40 | 2452 | |
b1f74493 FT |
2453 | pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes); |
2454 | BUG_ON(pending); | |
2455 | } | |
56ad1740 | 2456 | EXPORT_SYMBOL(blk_end_request_all); |
336cdb40 | 2457 | |
b1f74493 FT |
2458 | /** |
2459 | * blk_end_request_cur - Helper function to finish the current request chunk. | |
2460 | * @rq: the request to finish the current chunk for | |
8ebf9756 | 2461 | * @error: %0 for success, < %0 for error |
b1f74493 FT |
2462 | * |
2463 | * Description: | |
2464 | * Complete the current consecutively mapped chunk from @rq. | |
2465 | * | |
2466 | * Return: | |
2467 | * %false - we are done with this request | |
2468 | * %true - still buffers pending for this request | |
2469 | */ | |
2470 | bool blk_end_request_cur(struct request *rq, int error) | |
2471 | { | |
2472 | return blk_end_request(rq, error, blk_rq_cur_bytes(rq)); | |
336cdb40 | 2473 | } |
56ad1740 | 2474 | EXPORT_SYMBOL(blk_end_request_cur); |
336cdb40 | 2475 | |
80a761fd TH |
2476 | /** |
2477 | * blk_end_request_err - Finish a request till the next failure boundary. | |
2478 | * @rq: the request to finish till the next failure boundary for | |
2479 | * @error: must be negative errno | |
2480 | * | |
2481 | * Description: | |
2482 | * Complete @rq till the next failure boundary. | |
2483 | * | |
2484 | * Return: | |
2485 | * %false - we are done with this request | |
2486 | * %true - still buffers pending for this request | |
2487 | */ | |
2488 | bool blk_end_request_err(struct request *rq, int error) | |
2489 | { | |
2490 | WARN_ON(error >= 0); | |
2491 | return blk_end_request(rq, error, blk_rq_err_bytes(rq)); | |
2492 | } | |
2493 | EXPORT_SYMBOL_GPL(blk_end_request_err); | |
2494 | ||
e3a04fe3 | 2495 | /** |
b1f74493 FT |
2496 | * __blk_end_request - Helper function for drivers to complete the request. |
2497 | * @rq: the request being processed | |
2498 | * @error: %0 for success, < %0 for error | |
2499 | * @nr_bytes: number of bytes to complete | |
e3a04fe3 KU |
2500 | * |
2501 | * Description: | |
b1f74493 | 2502 | * Must be called with queue lock held unlike blk_end_request(). |
e3a04fe3 KU |
2503 | * |
2504 | * Return: | |
b1f74493 FT |
2505 | * %false - we are done with this request |
2506 | * %true - still buffers pending for this request | |
e3a04fe3 | 2507 | **/ |
b1f74493 | 2508 | bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes) |
e3a04fe3 | 2509 | { |
b1f74493 | 2510 | return __blk_end_bidi_request(rq, error, nr_bytes, 0); |
e3a04fe3 | 2511 | } |
56ad1740 | 2512 | EXPORT_SYMBOL(__blk_end_request); |
e3a04fe3 | 2513 | |
32fab448 | 2514 | /** |
b1f74493 FT |
2515 | * __blk_end_request_all - Helper function for drives to finish the request. |
2516 | * @rq: the request to finish | |
8ebf9756 | 2517 | * @error: %0 for success, < %0 for error |
32fab448 KU |
2518 | * |
2519 | * Description: | |
b1f74493 | 2520 | * Completely finish @rq. Must be called with queue lock held. |
32fab448 | 2521 | */ |
b1f74493 | 2522 | void __blk_end_request_all(struct request *rq, int error) |
32fab448 | 2523 | { |
b1f74493 FT |
2524 | bool pending; |
2525 | unsigned int bidi_bytes = 0; | |
2526 | ||
2527 | if (unlikely(blk_bidi_rq(rq))) | |
2528 | bidi_bytes = blk_rq_bytes(rq->next_rq); | |
2529 | ||
2530 | pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes); | |
2531 | BUG_ON(pending); | |
32fab448 | 2532 | } |
56ad1740 | 2533 | EXPORT_SYMBOL(__blk_end_request_all); |
32fab448 | 2534 | |
e19a3ab0 | 2535 | /** |
b1f74493 FT |
2536 | * __blk_end_request_cur - Helper function to finish the current request chunk. |
2537 | * @rq: the request to finish the current chunk for | |
8ebf9756 | 2538 | * @error: %0 for success, < %0 for error |
e19a3ab0 KU |
2539 | * |
2540 | * Description: | |
b1f74493 FT |
2541 | * Complete the current consecutively mapped chunk from @rq. Must |
2542 | * be called with queue lock held. | |
e19a3ab0 KU |
2543 | * |
2544 | * Return: | |
b1f74493 FT |
2545 | * %false - we are done with this request |
2546 | * %true - still buffers pending for this request | |
2547 | */ | |
2548 | bool __blk_end_request_cur(struct request *rq, int error) | |
e19a3ab0 | 2549 | { |
b1f74493 | 2550 | return __blk_end_request(rq, error, blk_rq_cur_bytes(rq)); |
e19a3ab0 | 2551 | } |
56ad1740 | 2552 | EXPORT_SYMBOL(__blk_end_request_cur); |
e19a3ab0 | 2553 | |
80a761fd TH |
2554 | /** |
2555 | * __blk_end_request_err - Finish a request till the next failure boundary. | |
2556 | * @rq: the request to finish till the next failure boundary for | |
2557 | * @error: must be negative errno | |
2558 | * | |
2559 | * Description: | |
2560 | * Complete @rq till the next failure boundary. Must be called | |
2561 | * with queue lock held. | |
2562 | * | |
2563 | * Return: | |
2564 | * %false - we are done with this request | |
2565 | * %true - still buffers pending for this request | |
2566 | */ | |
2567 | bool __blk_end_request_err(struct request *rq, int error) | |
2568 | { | |
2569 | WARN_ON(error >= 0); | |
2570 | return __blk_end_request(rq, error, blk_rq_err_bytes(rq)); | |
2571 | } | |
2572 | EXPORT_SYMBOL_GPL(__blk_end_request_err); | |
2573 | ||
86db1e29 JA |
2574 | void blk_rq_bio_prep(struct request_queue *q, struct request *rq, |
2575 | struct bio *bio) | |
1da177e4 | 2576 | { |
a82afdfc | 2577 | /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw */ |
7b6d91da | 2578 | rq->cmd_flags |= bio->bi_rw & REQ_WRITE; |
1da177e4 | 2579 | |
fb2dce86 DW |
2580 | if (bio_has_data(bio)) { |
2581 | rq->nr_phys_segments = bio_phys_segments(q, bio); | |
fb2dce86 DW |
2582 | rq->buffer = bio_data(bio); |
2583 | } | |
a2dec7b3 | 2584 | rq->__data_len = bio->bi_size; |
1da177e4 | 2585 | rq->bio = rq->biotail = bio; |
1da177e4 | 2586 | |
66846572 N |
2587 | if (bio->bi_bdev) |
2588 | rq->rq_disk = bio->bi_bdev->bd_disk; | |
2589 | } | |
1da177e4 | 2590 | |
2d4dc890 IL |
2591 | #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE |
2592 | /** | |
2593 | * rq_flush_dcache_pages - Helper function to flush all pages in a request | |
2594 | * @rq: the request to be flushed | |
2595 | * | |
2596 | * Description: | |
2597 | * Flush all pages in @rq. | |
2598 | */ | |
2599 | void rq_flush_dcache_pages(struct request *rq) | |
2600 | { | |
2601 | struct req_iterator iter; | |
2602 | struct bio_vec *bvec; | |
2603 | ||
2604 | rq_for_each_segment(bvec, rq, iter) | |
2605 | flush_dcache_page(bvec->bv_page); | |
2606 | } | |
2607 | EXPORT_SYMBOL_GPL(rq_flush_dcache_pages); | |
2608 | #endif | |
2609 | ||
ef9e3fac KU |
2610 | /** |
2611 | * blk_lld_busy - Check if underlying low-level drivers of a device are busy | |
2612 | * @q : the queue of the device being checked | |
2613 | * | |
2614 | * Description: | |
2615 | * Check if underlying low-level drivers of a device are busy. | |
2616 | * If the drivers want to export their busy state, they must set own | |
2617 | * exporting function using blk_queue_lld_busy() first. | |
2618 | * | |
2619 | * Basically, this function is used only by request stacking drivers | |
2620 | * to stop dispatching requests to underlying devices when underlying | |
2621 | * devices are busy. This behavior helps more I/O merging on the queue | |
2622 | * of the request stacking driver and prevents I/O throughput regression | |
2623 | * on burst I/O load. | |
2624 | * | |
2625 | * Return: | |
2626 | * 0 - Not busy (The request stacking driver should dispatch request) | |
2627 | * 1 - Busy (The request stacking driver should stop dispatching request) | |
2628 | */ | |
2629 | int blk_lld_busy(struct request_queue *q) | |
2630 | { | |
2631 | if (q->lld_busy_fn) | |
2632 | return q->lld_busy_fn(q); | |
2633 | ||
2634 | return 0; | |
2635 | } | |
2636 | EXPORT_SYMBOL_GPL(blk_lld_busy); | |
2637 | ||
b0fd271d KU |
2638 | /** |
2639 | * blk_rq_unprep_clone - Helper function to free all bios in a cloned request | |
2640 | * @rq: the clone request to be cleaned up | |
2641 | * | |
2642 | * Description: | |
2643 | * Free all bios in @rq for a cloned request. | |
2644 | */ | |
2645 | void blk_rq_unprep_clone(struct request *rq) | |
2646 | { | |
2647 | struct bio *bio; | |
2648 | ||
2649 | while ((bio = rq->bio) != NULL) { | |
2650 | rq->bio = bio->bi_next; | |
2651 | ||
2652 | bio_put(bio); | |
2653 | } | |
2654 | } | |
2655 | EXPORT_SYMBOL_GPL(blk_rq_unprep_clone); | |
2656 | ||
2657 | /* | |
2658 | * Copy attributes of the original request to the clone request. | |
2659 | * The actual data parts (e.g. ->cmd, ->buffer, ->sense) are not copied. | |
2660 | */ | |
2661 | static void __blk_rq_prep_clone(struct request *dst, struct request *src) | |
2662 | { | |
2663 | dst->cpu = src->cpu; | |
3a2edd0d | 2664 | dst->cmd_flags = (src->cmd_flags & REQ_CLONE_MASK) | REQ_NOMERGE; |
b0fd271d KU |
2665 | dst->cmd_type = src->cmd_type; |
2666 | dst->__sector = blk_rq_pos(src); | |
2667 | dst->__data_len = blk_rq_bytes(src); | |
2668 | dst->nr_phys_segments = src->nr_phys_segments; | |
2669 | dst->ioprio = src->ioprio; | |
2670 | dst->extra_len = src->extra_len; | |
2671 | } | |
2672 | ||
2673 | /** | |
2674 | * blk_rq_prep_clone - Helper function to setup clone request | |
2675 | * @rq: the request to be setup | |
2676 | * @rq_src: original request to be cloned | |
2677 | * @bs: bio_set that bios for clone are allocated from | |
2678 | * @gfp_mask: memory allocation mask for bio | |
2679 | * @bio_ctr: setup function to be called for each clone bio. | |
2680 | * Returns %0 for success, non %0 for failure. | |
2681 | * @data: private data to be passed to @bio_ctr | |
2682 | * | |
2683 | * Description: | |
2684 | * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq. | |
2685 | * The actual data parts of @rq_src (e.g. ->cmd, ->buffer, ->sense) | |
2686 | * are not copied, and copying such parts is the caller's responsibility. | |
2687 | * Also, pages which the original bios are pointing to are not copied | |
2688 | * and the cloned bios just point same pages. | |
2689 | * So cloned bios must be completed before original bios, which means | |
2690 | * the caller must complete @rq before @rq_src. | |
2691 | */ | |
2692 | int blk_rq_prep_clone(struct request *rq, struct request *rq_src, | |
2693 | struct bio_set *bs, gfp_t gfp_mask, | |
2694 | int (*bio_ctr)(struct bio *, struct bio *, void *), | |
2695 | void *data) | |
2696 | { | |
2697 | struct bio *bio, *bio_src; | |
2698 | ||
2699 | if (!bs) | |
2700 | bs = fs_bio_set; | |
2701 | ||
2702 | blk_rq_init(NULL, rq); | |
2703 | ||
2704 | __rq_for_each_bio(bio_src, rq_src) { | |
2705 | bio = bio_alloc_bioset(gfp_mask, bio_src->bi_max_vecs, bs); | |
2706 | if (!bio) | |
2707 | goto free_and_out; | |
2708 | ||
2709 | __bio_clone(bio, bio_src); | |
2710 | ||
2711 | if (bio_integrity(bio_src) && | |
7878cba9 | 2712 | bio_integrity_clone(bio, bio_src, gfp_mask, bs)) |
b0fd271d KU |
2713 | goto free_and_out; |
2714 | ||
2715 | if (bio_ctr && bio_ctr(bio, bio_src, data)) | |
2716 | goto free_and_out; | |
2717 | ||
2718 | if (rq->bio) { | |
2719 | rq->biotail->bi_next = bio; | |
2720 | rq->biotail = bio; | |
2721 | } else | |
2722 | rq->bio = rq->biotail = bio; | |
2723 | } | |
2724 | ||
2725 | __blk_rq_prep_clone(rq, rq_src); | |
2726 | ||
2727 | return 0; | |
2728 | ||
2729 | free_and_out: | |
2730 | if (bio) | |
2731 | bio_free(bio, bs); | |
2732 | blk_rq_unprep_clone(rq); | |
2733 | ||
2734 | return -ENOMEM; | |
2735 | } | |
2736 | EXPORT_SYMBOL_GPL(blk_rq_prep_clone); | |
2737 | ||
18887ad9 | 2738 | int kblockd_schedule_work(struct request_queue *q, struct work_struct *work) |
1da177e4 LT |
2739 | { |
2740 | return queue_work(kblockd_workqueue, work); | |
2741 | } | |
1da177e4 LT |
2742 | EXPORT_SYMBOL(kblockd_schedule_work); |
2743 | ||
e43473b7 VG |
2744 | int kblockd_schedule_delayed_work(struct request_queue *q, |
2745 | struct delayed_work *dwork, unsigned long delay) | |
2746 | { | |
2747 | return queue_delayed_work(kblockd_workqueue, dwork, delay); | |
2748 | } | |
2749 | EXPORT_SYMBOL(kblockd_schedule_delayed_work); | |
2750 | ||
73c10101 JA |
2751 | #define PLUG_MAGIC 0x91827364 |
2752 | ||
75df7136 SJ |
2753 | /** |
2754 | * blk_start_plug - initialize blk_plug and track it inside the task_struct | |
2755 | * @plug: The &struct blk_plug that needs to be initialized | |
2756 | * | |
2757 | * Description: | |
2758 | * Tracking blk_plug inside the task_struct will help with auto-flushing the | |
2759 | * pending I/O should the task end up blocking between blk_start_plug() and | |
2760 | * blk_finish_plug(). This is important from a performance perspective, but | |
2761 | * also ensures that we don't deadlock. For instance, if the task is blocking | |
2762 | * for a memory allocation, memory reclaim could end up wanting to free a | |
2763 | * page belonging to that request that is currently residing in our private | |
2764 | * plug. By flushing the pending I/O when the process goes to sleep, we avoid | |
2765 | * this kind of deadlock. | |
2766 | */ | |
73c10101 JA |
2767 | void blk_start_plug(struct blk_plug *plug) |
2768 | { | |
2769 | struct task_struct *tsk = current; | |
2770 | ||
2771 | plug->magic = PLUG_MAGIC; | |
2772 | INIT_LIST_HEAD(&plug->list); | |
048c9374 | 2773 | INIT_LIST_HEAD(&plug->cb_list); |
73c10101 JA |
2774 | plug->should_sort = 0; |
2775 | ||
2776 | /* | |
2777 | * If this is a nested plug, don't actually assign it. It will be | |
2778 | * flushed on its own. | |
2779 | */ | |
2780 | if (!tsk->plug) { | |
2781 | /* | |
2782 | * Store ordering should not be needed here, since a potential | |
2783 | * preempt will imply a full memory barrier | |
2784 | */ | |
2785 | tsk->plug = plug; | |
2786 | } | |
2787 | } | |
2788 | EXPORT_SYMBOL(blk_start_plug); | |
2789 | ||
2790 | static int plug_rq_cmp(void *priv, struct list_head *a, struct list_head *b) | |
2791 | { | |
2792 | struct request *rqa = container_of(a, struct request, queuelist); | |
2793 | struct request *rqb = container_of(b, struct request, queuelist); | |
2794 | ||
f83e8261 | 2795 | return !(rqa->q <= rqb->q); |
73c10101 JA |
2796 | } |
2797 | ||
49cac01e JA |
2798 | /* |
2799 | * If 'from_schedule' is true, then postpone the dispatch of requests | |
2800 | * until a safe kblockd context. We due this to avoid accidental big | |
2801 | * additional stack usage in driver dispatch, in places where the originally | |
2802 | * plugger did not intend it. | |
2803 | */ | |
f6603783 | 2804 | static void queue_unplugged(struct request_queue *q, unsigned int depth, |
49cac01e | 2805 | bool from_schedule) |
99e22598 | 2806 | __releases(q->queue_lock) |
94b5eb28 | 2807 | { |
49cac01e | 2808 | trace_block_unplug(q, depth, !from_schedule); |
99e22598 | 2809 | |
8ba61435 TH |
2810 | /* |
2811 | * Don't mess with dead queue. | |
2812 | */ | |
2813 | if (unlikely(blk_queue_dead(q))) { | |
2814 | spin_unlock(q->queue_lock); | |
2815 | return; | |
2816 | } | |
2817 | ||
99e22598 JA |
2818 | /* |
2819 | * If we are punting this to kblockd, then we can safely drop | |
2820 | * the queue_lock before waking kblockd (which needs to take | |
2821 | * this lock). | |
2822 | */ | |
2823 | if (from_schedule) { | |
2824 | spin_unlock(q->queue_lock); | |
24ecfbe2 | 2825 | blk_run_queue_async(q); |
99e22598 | 2826 | } else { |
24ecfbe2 | 2827 | __blk_run_queue(q); |
99e22598 JA |
2828 | spin_unlock(q->queue_lock); |
2829 | } | |
2830 | ||
94b5eb28 JA |
2831 | } |
2832 | ||
048c9374 N |
2833 | static void flush_plug_callbacks(struct blk_plug *plug) |
2834 | { | |
2835 | LIST_HEAD(callbacks); | |
2836 | ||
2837 | if (list_empty(&plug->cb_list)) | |
2838 | return; | |
2839 | ||
2840 | list_splice_init(&plug->cb_list, &callbacks); | |
2841 | ||
2842 | while (!list_empty(&callbacks)) { | |
2843 | struct blk_plug_cb *cb = list_first_entry(&callbacks, | |
2844 | struct blk_plug_cb, | |
2845 | list); | |
2846 | list_del(&cb->list); | |
2847 | cb->callback(cb); | |
2848 | } | |
2849 | } | |
2850 | ||
49cac01e | 2851 | void blk_flush_plug_list(struct blk_plug *plug, bool from_schedule) |
73c10101 JA |
2852 | { |
2853 | struct request_queue *q; | |
2854 | unsigned long flags; | |
2855 | struct request *rq; | |
109b8129 | 2856 | LIST_HEAD(list); |
94b5eb28 | 2857 | unsigned int depth; |
73c10101 JA |
2858 | |
2859 | BUG_ON(plug->magic != PLUG_MAGIC); | |
2860 | ||
048c9374 | 2861 | flush_plug_callbacks(plug); |
73c10101 JA |
2862 | if (list_empty(&plug->list)) |
2863 | return; | |
2864 | ||
109b8129 N |
2865 | list_splice_init(&plug->list, &list); |
2866 | ||
2867 | if (plug->should_sort) { | |
2868 | list_sort(NULL, &list, plug_rq_cmp); | |
2869 | plug->should_sort = 0; | |
2870 | } | |
73c10101 JA |
2871 | |
2872 | q = NULL; | |
94b5eb28 | 2873 | depth = 0; |
18811272 JA |
2874 | |
2875 | /* | |
2876 | * Save and disable interrupts here, to avoid doing it for every | |
2877 | * queue lock we have to take. | |
2878 | */ | |
73c10101 | 2879 | local_irq_save(flags); |
109b8129 N |
2880 | while (!list_empty(&list)) { |
2881 | rq = list_entry_rq(list.next); | |
73c10101 | 2882 | list_del_init(&rq->queuelist); |
73c10101 JA |
2883 | BUG_ON(!rq->q); |
2884 | if (rq->q != q) { | |
99e22598 JA |
2885 | /* |
2886 | * This drops the queue lock | |
2887 | */ | |
2888 | if (q) | |
49cac01e | 2889 | queue_unplugged(q, depth, from_schedule); |
73c10101 | 2890 | q = rq->q; |
94b5eb28 | 2891 | depth = 0; |
73c10101 JA |
2892 | spin_lock(q->queue_lock); |
2893 | } | |
8ba61435 TH |
2894 | |
2895 | /* | |
2896 | * Short-circuit if @q is dead | |
2897 | */ | |
2898 | if (unlikely(blk_queue_dead(q))) { | |
2899 | __blk_end_request_all(rq, -ENODEV); | |
2900 | continue; | |
2901 | } | |
2902 | ||
73c10101 JA |
2903 | /* |
2904 | * rq is already accounted, so use raw insert | |
2905 | */ | |
401a18e9 JA |
2906 | if (rq->cmd_flags & (REQ_FLUSH | REQ_FUA)) |
2907 | __elv_add_request(q, rq, ELEVATOR_INSERT_FLUSH); | |
2908 | else | |
2909 | __elv_add_request(q, rq, ELEVATOR_INSERT_SORT_MERGE); | |
94b5eb28 JA |
2910 | |
2911 | depth++; | |
73c10101 JA |
2912 | } |
2913 | ||
99e22598 JA |
2914 | /* |
2915 | * This drops the queue lock | |
2916 | */ | |
2917 | if (q) | |
49cac01e | 2918 | queue_unplugged(q, depth, from_schedule); |
73c10101 | 2919 | |
73c10101 JA |
2920 | local_irq_restore(flags); |
2921 | } | |
73c10101 JA |
2922 | |
2923 | void blk_finish_plug(struct blk_plug *plug) | |
2924 | { | |
f6603783 | 2925 | blk_flush_plug_list(plug, false); |
73c10101 | 2926 | |
88b996cd CH |
2927 | if (plug == current->plug) |
2928 | current->plug = NULL; | |
73c10101 | 2929 | } |
88b996cd | 2930 | EXPORT_SYMBOL(blk_finish_plug); |
73c10101 | 2931 | |
1da177e4 LT |
2932 | int __init blk_dev_init(void) |
2933 | { | |
9eb55b03 NK |
2934 | BUILD_BUG_ON(__REQ_NR_BITS > 8 * |
2935 | sizeof(((struct request *)0)->cmd_flags)); | |
2936 | ||
89b90be2 TH |
2937 | /* used for unplugging and affects IO latency/throughput - HIGHPRI */ |
2938 | kblockd_workqueue = alloc_workqueue("kblockd", | |
2939 | WQ_MEM_RECLAIM | WQ_HIGHPRI, 0); | |
1da177e4 LT |
2940 | if (!kblockd_workqueue) |
2941 | panic("Failed to create kblockd\n"); | |
2942 | ||
2943 | request_cachep = kmem_cache_create("blkdev_requests", | |
20c2df83 | 2944 | sizeof(struct request), 0, SLAB_PANIC, NULL); |
1da177e4 | 2945 | |
8324aa91 | 2946 | blk_requestq_cachep = kmem_cache_create("blkdev_queue", |
165125e1 | 2947 | sizeof(struct request_queue), 0, SLAB_PANIC, NULL); |
1da177e4 | 2948 | |
d38ecf93 | 2949 | return 0; |
1da177e4 | 2950 | } |