]>
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 <[email protected]> SuSE | |
5 | * Queue request tables / lock, selectable elevator, Jens Axboe <[email protected]> | |
6728cb0e JA |
6 | * kernel-doc documentation started by NeilBrown <[email protected]> |
7 | * - July2000 | |
1da177e4 LT |
8 | * bio rewrite, highmem i/o, etc, Jens Axboe <[email protected]> - 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> |
55782138 LZ |
30 | |
31 | #define CREATE_TRACE_POINTS | |
32 | #include <trace/events/block.h> | |
1da177e4 | 33 | |
8324aa91 JA |
34 | #include "blk.h" |
35 | ||
0bfc2455 | 36 | EXPORT_TRACEPOINT_SYMBOL_GPL(block_remap); |
55782138 | 37 | EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete); |
0bfc2455 | 38 | |
165125e1 | 39 | static int __make_request(struct request_queue *q, struct bio *bio); |
1da177e4 LT |
40 | |
41 | /* | |
42 | * For the allocated request tables | |
43 | */ | |
5ece6c52 | 44 | static struct kmem_cache *request_cachep; |
1da177e4 LT |
45 | |
46 | /* | |
47 | * For queue allocation | |
48 | */ | |
6728cb0e | 49 | struct kmem_cache *blk_requestq_cachep; |
1da177e4 | 50 | |
1da177e4 LT |
51 | /* |
52 | * Controlling structure to kblockd | |
53 | */ | |
ff856bad | 54 | static struct workqueue_struct *kblockd_workqueue; |
1da177e4 | 55 | |
26b8256e JA |
56 | static void drive_stat_acct(struct request *rq, int new_io) |
57 | { | |
28f13702 | 58 | struct hd_struct *part; |
26b8256e | 59 | int rw = rq_data_dir(rq); |
c9959059 | 60 | int cpu; |
26b8256e | 61 | |
c2553b58 | 62 | if (!blk_do_io_stat(rq)) |
26b8256e JA |
63 | return; |
64 | ||
074a7aca | 65 | cpu = part_stat_lock(); |
83096ebf | 66 | part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq)); |
c9959059 | 67 | |
28f13702 | 68 | if (!new_io) |
074a7aca | 69 | part_stat_inc(cpu, part, merges[rw]); |
28f13702 | 70 | else { |
074a7aca TH |
71 | part_round_stats(cpu, part); |
72 | part_inc_in_flight(part); | |
26b8256e | 73 | } |
e71bf0d0 | 74 | |
074a7aca | 75 | part_stat_unlock(); |
26b8256e JA |
76 | } |
77 | ||
8324aa91 | 78 | void blk_queue_congestion_threshold(struct request_queue *q) |
1da177e4 LT |
79 | { |
80 | int nr; | |
81 | ||
82 | nr = q->nr_requests - (q->nr_requests / 8) + 1; | |
83 | if (nr > q->nr_requests) | |
84 | nr = q->nr_requests; | |
85 | q->nr_congestion_on = nr; | |
86 | ||
87 | nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1; | |
88 | if (nr < 1) | |
89 | nr = 1; | |
90 | q->nr_congestion_off = nr; | |
91 | } | |
92 | ||
1da177e4 LT |
93 | /** |
94 | * blk_get_backing_dev_info - get the address of a queue's backing_dev_info | |
95 | * @bdev: device | |
96 | * | |
97 | * Locates the passed device's request queue and returns the address of its | |
98 | * backing_dev_info | |
99 | * | |
100 | * Will return NULL if the request queue cannot be located. | |
101 | */ | |
102 | struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev) | |
103 | { | |
104 | struct backing_dev_info *ret = NULL; | |
165125e1 | 105 | struct request_queue *q = bdev_get_queue(bdev); |
1da177e4 LT |
106 | |
107 | if (q) | |
108 | ret = &q->backing_dev_info; | |
109 | return ret; | |
110 | } | |
1da177e4 LT |
111 | EXPORT_SYMBOL(blk_get_backing_dev_info); |
112 | ||
2a4aa30c | 113 | void blk_rq_init(struct request_queue *q, struct request *rq) |
1da177e4 | 114 | { |
1afb20f3 FT |
115 | memset(rq, 0, sizeof(*rq)); |
116 | ||
1da177e4 | 117 | INIT_LIST_HEAD(&rq->queuelist); |
242f9dcb | 118 | INIT_LIST_HEAD(&rq->timeout_list); |
c7c22e4d | 119 | rq->cpu = -1; |
63a71386 | 120 | rq->q = q; |
a2dec7b3 | 121 | rq->__sector = (sector_t) -1; |
2e662b65 JA |
122 | INIT_HLIST_NODE(&rq->hash); |
123 | RB_CLEAR_NODE(&rq->rb_node); | |
d7e3c324 | 124 | rq->cmd = rq->__cmd; |
e2494e1b | 125 | rq->cmd_len = BLK_MAX_CDB; |
63a71386 | 126 | rq->tag = -1; |
1da177e4 | 127 | rq->ref_count = 1; |
b243ddcb | 128 | rq->start_time = jiffies; |
1da177e4 | 129 | } |
2a4aa30c | 130 | EXPORT_SYMBOL(blk_rq_init); |
1da177e4 | 131 | |
5bb23a68 N |
132 | static void req_bio_endio(struct request *rq, struct bio *bio, |
133 | unsigned int nbytes, int error) | |
1da177e4 | 134 | { |
165125e1 | 135 | struct request_queue *q = rq->q; |
797e7dbb | 136 | |
5bb23a68 N |
137 | if (&q->bar_rq != rq) { |
138 | if (error) | |
139 | clear_bit(BIO_UPTODATE, &bio->bi_flags); | |
140 | else if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) | |
141 | error = -EIO; | |
797e7dbb | 142 | |
5bb23a68 | 143 | if (unlikely(nbytes > bio->bi_size)) { |
6728cb0e | 144 | printk(KERN_ERR "%s: want %u bytes done, %u left\n", |
24c03d47 | 145 | __func__, nbytes, bio->bi_size); |
5bb23a68 N |
146 | nbytes = bio->bi_size; |
147 | } | |
797e7dbb | 148 | |
08bafc03 KM |
149 | if (unlikely(rq->cmd_flags & REQ_QUIET)) |
150 | set_bit(BIO_QUIET, &bio->bi_flags); | |
151 | ||
5bb23a68 N |
152 | bio->bi_size -= nbytes; |
153 | bio->bi_sector += (nbytes >> 9); | |
7ba1ba12 MP |
154 | |
155 | if (bio_integrity(bio)) | |
156 | bio_integrity_advance(bio, nbytes); | |
157 | ||
5bb23a68 | 158 | if (bio->bi_size == 0) |
6712ecf8 | 159 | bio_endio(bio, error); |
5bb23a68 N |
160 | } else { |
161 | ||
162 | /* | |
163 | * Okay, this is the barrier request in progress, just | |
164 | * record the error; | |
165 | */ | |
166 | if (error && !q->orderr) | |
167 | q->orderr = error; | |
168 | } | |
1da177e4 | 169 | } |
1da177e4 | 170 | |
1da177e4 LT |
171 | void blk_dump_rq_flags(struct request *rq, char *msg) |
172 | { | |
173 | int bit; | |
174 | ||
6728cb0e | 175 | printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg, |
4aff5e23 JA |
176 | rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type, |
177 | rq->cmd_flags); | |
1da177e4 | 178 | |
83096ebf TH |
179 | printk(KERN_INFO " sector %llu, nr/cnr %u/%u\n", |
180 | (unsigned long long)blk_rq_pos(rq), | |
181 | blk_rq_sectors(rq), blk_rq_cur_sectors(rq)); | |
731ec497 | 182 | printk(KERN_INFO " bio %p, biotail %p, buffer %p, len %u\n", |
2e46e8b2 | 183 | rq->bio, rq->biotail, rq->buffer, blk_rq_bytes(rq)); |
1da177e4 | 184 | |
4aff5e23 | 185 | if (blk_pc_request(rq)) { |
6728cb0e | 186 | printk(KERN_INFO " cdb: "); |
d34c87e4 | 187 | for (bit = 0; bit < BLK_MAX_CDB; bit++) |
1da177e4 LT |
188 | printk("%02x ", rq->cmd[bit]); |
189 | printk("\n"); | |
190 | } | |
191 | } | |
1da177e4 LT |
192 | EXPORT_SYMBOL(blk_dump_rq_flags); |
193 | ||
1da177e4 LT |
194 | /* |
195 | * "plug" the device if there are no outstanding requests: this will | |
196 | * force the transfer to start only after we have put all the requests | |
197 | * on the list. | |
198 | * | |
199 | * This is called with interrupts off and no requests on the queue and | |
200 | * with the queue lock held. | |
201 | */ | |
165125e1 | 202 | void blk_plug_device(struct request_queue *q) |
1da177e4 LT |
203 | { |
204 | WARN_ON(!irqs_disabled()); | |
205 | ||
206 | /* | |
207 | * don't plug a stopped queue, it must be paired with blk_start_queue() | |
208 | * which will restart the queueing | |
209 | */ | |
7daac490 | 210 | if (blk_queue_stopped(q)) |
1da177e4 LT |
211 | return; |
212 | ||
e48ec690 | 213 | if (!queue_flag_test_and_set(QUEUE_FLAG_PLUGGED, q)) { |
1da177e4 | 214 | mod_timer(&q->unplug_timer, jiffies + q->unplug_delay); |
5f3ea37c | 215 | trace_block_plug(q); |
2056a782 | 216 | } |
1da177e4 | 217 | } |
1da177e4 LT |
218 | EXPORT_SYMBOL(blk_plug_device); |
219 | ||
6c5e0c4d JA |
220 | /** |
221 | * blk_plug_device_unlocked - plug a device without queue lock held | |
222 | * @q: The &struct request_queue to plug | |
223 | * | |
224 | * Description: | |
225 | * Like @blk_plug_device(), but grabs the queue lock and disables | |
226 | * interrupts. | |
227 | **/ | |
228 | void blk_plug_device_unlocked(struct request_queue *q) | |
229 | { | |
230 | unsigned long flags; | |
231 | ||
232 | spin_lock_irqsave(q->queue_lock, flags); | |
233 | blk_plug_device(q); | |
234 | spin_unlock_irqrestore(q->queue_lock, flags); | |
235 | } | |
236 | EXPORT_SYMBOL(blk_plug_device_unlocked); | |
237 | ||
1da177e4 LT |
238 | /* |
239 | * remove the queue from the plugged list, if present. called with | |
240 | * queue lock held and interrupts disabled. | |
241 | */ | |
165125e1 | 242 | int blk_remove_plug(struct request_queue *q) |
1da177e4 LT |
243 | { |
244 | WARN_ON(!irqs_disabled()); | |
245 | ||
e48ec690 | 246 | if (!queue_flag_test_and_clear(QUEUE_FLAG_PLUGGED, q)) |
1da177e4 LT |
247 | return 0; |
248 | ||
249 | del_timer(&q->unplug_timer); | |
250 | return 1; | |
251 | } | |
1da177e4 LT |
252 | EXPORT_SYMBOL(blk_remove_plug); |
253 | ||
254 | /* | |
255 | * remove the plug and let it rip.. | |
256 | */ | |
165125e1 | 257 | void __generic_unplug_device(struct request_queue *q) |
1da177e4 | 258 | { |
7daac490 | 259 | if (unlikely(blk_queue_stopped(q))) |
1da177e4 | 260 | return; |
a31a9738 | 261 | if (!blk_remove_plug(q) && !blk_queue_nonrot(q)) |
1da177e4 LT |
262 | return; |
263 | ||
22e2c507 | 264 | q->request_fn(q); |
1da177e4 | 265 | } |
1da177e4 LT |
266 | |
267 | /** | |
268 | * generic_unplug_device - fire a request queue | |
165125e1 | 269 | * @q: The &struct request_queue in question |
1da177e4 LT |
270 | * |
271 | * Description: | |
272 | * Linux uses plugging to build bigger requests queues before letting | |
273 | * the device have at them. If a queue is plugged, the I/O scheduler | |
274 | * is still adding and merging requests on the queue. Once the queue | |
275 | * gets unplugged, the request_fn defined for the queue is invoked and | |
276 | * transfers started. | |
277 | **/ | |
165125e1 | 278 | void generic_unplug_device(struct request_queue *q) |
1da177e4 | 279 | { |
dbaf2c00 JA |
280 | if (blk_queue_plugged(q)) { |
281 | spin_lock_irq(q->queue_lock); | |
282 | __generic_unplug_device(q); | |
283 | spin_unlock_irq(q->queue_lock); | |
284 | } | |
1da177e4 LT |
285 | } |
286 | EXPORT_SYMBOL(generic_unplug_device); | |
287 | ||
288 | static void blk_backing_dev_unplug(struct backing_dev_info *bdi, | |
289 | struct page *page) | |
290 | { | |
165125e1 | 291 | struct request_queue *q = bdi->unplug_io_data; |
1da177e4 | 292 | |
2ad8b1ef | 293 | blk_unplug(q); |
1da177e4 LT |
294 | } |
295 | ||
86db1e29 | 296 | void blk_unplug_work(struct work_struct *work) |
1da177e4 | 297 | { |
165125e1 JA |
298 | struct request_queue *q = |
299 | container_of(work, struct request_queue, unplug_work); | |
1da177e4 | 300 | |
5f3ea37c | 301 | trace_block_unplug_io(q); |
1da177e4 LT |
302 | q->unplug_fn(q); |
303 | } | |
304 | ||
86db1e29 | 305 | void blk_unplug_timeout(unsigned long data) |
1da177e4 | 306 | { |
165125e1 | 307 | struct request_queue *q = (struct request_queue *)data; |
1da177e4 | 308 | |
5f3ea37c | 309 | trace_block_unplug_timer(q); |
18887ad9 | 310 | kblockd_schedule_work(q, &q->unplug_work); |
1da177e4 LT |
311 | } |
312 | ||
2ad8b1ef AB |
313 | void blk_unplug(struct request_queue *q) |
314 | { | |
315 | /* | |
316 | * devices don't necessarily have an ->unplug_fn defined | |
317 | */ | |
318 | if (q->unplug_fn) { | |
5f3ea37c | 319 | trace_block_unplug_io(q); |
2ad8b1ef AB |
320 | q->unplug_fn(q); |
321 | } | |
322 | } | |
323 | EXPORT_SYMBOL(blk_unplug); | |
324 | ||
1da177e4 LT |
325 | /** |
326 | * blk_start_queue - restart a previously stopped queue | |
165125e1 | 327 | * @q: The &struct request_queue in question |
1da177e4 LT |
328 | * |
329 | * Description: | |
330 | * blk_start_queue() will clear the stop flag on the queue, and call | |
331 | * the request_fn for the queue if it was in a stopped state when | |
332 | * entered. Also see blk_stop_queue(). Queue lock must be held. | |
333 | **/ | |
165125e1 | 334 | void blk_start_queue(struct request_queue *q) |
1da177e4 | 335 | { |
a038e253 PBG |
336 | WARN_ON(!irqs_disabled()); |
337 | ||
75ad23bc | 338 | queue_flag_clear(QUEUE_FLAG_STOPPED, q); |
a538cd03 | 339 | __blk_run_queue(q); |
1da177e4 | 340 | } |
1da177e4 LT |
341 | EXPORT_SYMBOL(blk_start_queue); |
342 | ||
343 | /** | |
344 | * blk_stop_queue - stop a queue | |
165125e1 | 345 | * @q: The &struct request_queue in question |
1da177e4 LT |
346 | * |
347 | * Description: | |
348 | * The Linux block layer assumes that a block driver will consume all | |
349 | * entries on the request queue when the request_fn strategy is called. | |
350 | * Often this will not happen, because of hardware limitations (queue | |
351 | * depth settings). If a device driver gets a 'queue full' response, | |
352 | * or if it simply chooses not to queue more I/O at one point, it can | |
353 | * call this function to prevent the request_fn from being called until | |
354 | * the driver has signalled it's ready to go again. This happens by calling | |
355 | * blk_start_queue() to restart queue operations. Queue lock must be held. | |
356 | **/ | |
165125e1 | 357 | void blk_stop_queue(struct request_queue *q) |
1da177e4 LT |
358 | { |
359 | blk_remove_plug(q); | |
75ad23bc | 360 | queue_flag_set(QUEUE_FLAG_STOPPED, q); |
1da177e4 LT |
361 | } |
362 | EXPORT_SYMBOL(blk_stop_queue); | |
363 | ||
364 | /** | |
365 | * blk_sync_queue - cancel any pending callbacks on a queue | |
366 | * @q: the queue | |
367 | * | |
368 | * Description: | |
369 | * The block layer may perform asynchronous callback activity | |
370 | * on a queue, such as calling the unplug function after a timeout. | |
371 | * A block device may call blk_sync_queue to ensure that any | |
372 | * such activity is cancelled, thus allowing it to release resources | |
59c51591 | 373 | * that the callbacks might use. The caller must already have made sure |
1da177e4 LT |
374 | * that its ->make_request_fn will not re-add plugging prior to calling |
375 | * this function. | |
376 | * | |
377 | */ | |
378 | void blk_sync_queue(struct request_queue *q) | |
379 | { | |
380 | del_timer_sync(&q->unplug_timer); | |
70ed28b9 | 381 | del_timer_sync(&q->timeout); |
64d01dc9 | 382 | cancel_work_sync(&q->unplug_work); |
1da177e4 LT |
383 | } |
384 | EXPORT_SYMBOL(blk_sync_queue); | |
385 | ||
386 | /** | |
80a4b58e | 387 | * __blk_run_queue - run a single device queue |
1da177e4 | 388 | * @q: The queue to run |
80a4b58e JA |
389 | * |
390 | * Description: | |
391 | * See @blk_run_queue. This variant must be called with the queue lock | |
392 | * held and interrupts disabled. | |
393 | * | |
1da177e4 | 394 | */ |
75ad23bc | 395 | void __blk_run_queue(struct request_queue *q) |
1da177e4 | 396 | { |
1da177e4 | 397 | blk_remove_plug(q); |
dac07ec1 | 398 | |
a538cd03 TH |
399 | if (unlikely(blk_queue_stopped(q))) |
400 | return; | |
401 | ||
402 | if (elv_queue_empty(q)) | |
403 | return; | |
404 | ||
dac07ec1 JA |
405 | /* |
406 | * Only recurse once to avoid overrunning the stack, let the unplug | |
407 | * handling reinvoke the handler shortly if we already got there. | |
408 | */ | |
a538cd03 TH |
409 | if (!queue_flag_test_and_set(QUEUE_FLAG_REENTER, q)) { |
410 | q->request_fn(q); | |
411 | queue_flag_clear(QUEUE_FLAG_REENTER, q); | |
412 | } else { | |
413 | queue_flag_set(QUEUE_FLAG_PLUGGED, q); | |
414 | kblockd_schedule_work(q, &q->unplug_work); | |
415 | } | |
75ad23bc NP |
416 | } |
417 | EXPORT_SYMBOL(__blk_run_queue); | |
dac07ec1 | 418 | |
75ad23bc NP |
419 | /** |
420 | * blk_run_queue - run a single device queue | |
421 | * @q: The queue to run | |
80a4b58e JA |
422 | * |
423 | * Description: | |
424 | * Invoke request handling on this queue, if it has pending work to do. | |
a7f55792 | 425 | * May be used to restart queueing when a request has completed. |
75ad23bc NP |
426 | */ |
427 | void blk_run_queue(struct request_queue *q) | |
428 | { | |
429 | unsigned long flags; | |
430 | ||
431 | spin_lock_irqsave(q->queue_lock, flags); | |
432 | __blk_run_queue(q); | |
1da177e4 LT |
433 | spin_unlock_irqrestore(q->queue_lock, flags); |
434 | } | |
435 | EXPORT_SYMBOL(blk_run_queue); | |
436 | ||
165125e1 | 437 | void blk_put_queue(struct request_queue *q) |
483f4afc AV |
438 | { |
439 | kobject_put(&q->kobj); | |
440 | } | |
483f4afc | 441 | |
6728cb0e | 442 | void blk_cleanup_queue(struct request_queue *q) |
483f4afc | 443 | { |
e3335de9 JA |
444 | /* |
445 | * We know we have process context here, so we can be a little | |
446 | * cautious and ensure that pending block actions on this device | |
447 | * are done before moving on. Going into this function, we should | |
448 | * not have processes doing IO to this device. | |
449 | */ | |
450 | blk_sync_queue(q); | |
451 | ||
483f4afc | 452 | mutex_lock(&q->sysfs_lock); |
75ad23bc | 453 | queue_flag_set_unlocked(QUEUE_FLAG_DEAD, q); |
483f4afc AV |
454 | mutex_unlock(&q->sysfs_lock); |
455 | ||
456 | if (q->elevator) | |
457 | elevator_exit(q->elevator); | |
458 | ||
459 | blk_put_queue(q); | |
460 | } | |
1da177e4 LT |
461 | EXPORT_SYMBOL(blk_cleanup_queue); |
462 | ||
165125e1 | 463 | static int blk_init_free_list(struct request_queue *q) |
1da177e4 LT |
464 | { |
465 | struct request_list *rl = &q->rq; | |
466 | ||
1faa16d2 JA |
467 | rl->count[BLK_RW_SYNC] = rl->count[BLK_RW_ASYNC] = 0; |
468 | rl->starved[BLK_RW_SYNC] = rl->starved[BLK_RW_ASYNC] = 0; | |
cb98fc8b | 469 | rl->elvpriv = 0; |
1faa16d2 JA |
470 | init_waitqueue_head(&rl->wait[BLK_RW_SYNC]); |
471 | init_waitqueue_head(&rl->wait[BLK_RW_ASYNC]); | |
1da177e4 | 472 | |
1946089a CL |
473 | rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab, |
474 | mempool_free_slab, request_cachep, q->node); | |
1da177e4 LT |
475 | |
476 | if (!rl->rq_pool) | |
477 | return -ENOMEM; | |
478 | ||
479 | return 0; | |
480 | } | |
481 | ||
165125e1 | 482 | struct request_queue *blk_alloc_queue(gfp_t gfp_mask) |
1da177e4 | 483 | { |
1946089a CL |
484 | return blk_alloc_queue_node(gfp_mask, -1); |
485 | } | |
486 | EXPORT_SYMBOL(blk_alloc_queue); | |
1da177e4 | 487 | |
165125e1 | 488 | struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id) |
1946089a | 489 | { |
165125e1 | 490 | struct request_queue *q; |
e0bf68dd | 491 | int err; |
1946089a | 492 | |
8324aa91 | 493 | q = kmem_cache_alloc_node(blk_requestq_cachep, |
94f6030c | 494 | gfp_mask | __GFP_ZERO, node_id); |
1da177e4 LT |
495 | if (!q) |
496 | return NULL; | |
497 | ||
e0bf68dd PZ |
498 | q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug; |
499 | q->backing_dev_info.unplug_io_data = q; | |
0989a025 JA |
500 | q->backing_dev_info.ra_pages = |
501 | (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE; | |
502 | q->backing_dev_info.state = 0; | |
503 | q->backing_dev_info.capabilities = BDI_CAP_MAP_COPY; | |
504 | ||
e0bf68dd PZ |
505 | err = bdi_init(&q->backing_dev_info); |
506 | if (err) { | |
8324aa91 | 507 | kmem_cache_free(blk_requestq_cachep, q); |
e0bf68dd PZ |
508 | return NULL; |
509 | } | |
510 | ||
1da177e4 | 511 | init_timer(&q->unplug_timer); |
242f9dcb JA |
512 | setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q); |
513 | INIT_LIST_HEAD(&q->timeout_list); | |
713ada9b | 514 | INIT_WORK(&q->unplug_work, blk_unplug_work); |
483f4afc | 515 | |
8324aa91 | 516 | kobject_init(&q->kobj, &blk_queue_ktype); |
1da177e4 | 517 | |
483f4afc | 518 | mutex_init(&q->sysfs_lock); |
e7e72bf6 | 519 | spin_lock_init(&q->__queue_lock); |
483f4afc | 520 | |
1da177e4 LT |
521 | return q; |
522 | } | |
1946089a | 523 | EXPORT_SYMBOL(blk_alloc_queue_node); |
1da177e4 LT |
524 | |
525 | /** | |
526 | * blk_init_queue - prepare a request queue for use with a block device | |
527 | * @rfn: The function to be called to process requests that have been | |
528 | * placed on the queue. | |
529 | * @lock: Request queue spin lock | |
530 | * | |
531 | * Description: | |
532 | * If a block device wishes to use the standard request handling procedures, | |
533 | * which sorts requests and coalesces adjacent requests, then it must | |
534 | * call blk_init_queue(). The function @rfn will be called when there | |
535 | * are requests on the queue that need to be processed. If the device | |
536 | * supports plugging, then @rfn may not be called immediately when requests | |
537 | * are available on the queue, but may be called at some time later instead. | |
538 | * Plugged queues are generally unplugged when a buffer belonging to one | |
539 | * of the requests on the queue is needed, or due to memory pressure. | |
540 | * | |
541 | * @rfn is not required, or even expected, to remove all requests off the | |
542 | * queue, but only as many as it can handle at a time. If it does leave | |
543 | * requests on the queue, it is responsible for arranging that the requests | |
544 | * get dealt with eventually. | |
545 | * | |
546 | * The queue spin lock must be held while manipulating the requests on the | |
a038e253 PBG |
547 | * request queue; this lock will be taken also from interrupt context, so irq |
548 | * disabling is needed for it. | |
1da177e4 | 549 | * |
710027a4 | 550 | * Function returns a pointer to the initialized request queue, or %NULL if |
1da177e4 LT |
551 | * it didn't succeed. |
552 | * | |
553 | * Note: | |
554 | * blk_init_queue() must be paired with a blk_cleanup_queue() call | |
555 | * when the block device is deactivated (such as at module unload). | |
556 | **/ | |
1946089a | 557 | |
165125e1 | 558 | struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock) |
1da177e4 | 559 | { |
1946089a CL |
560 | return blk_init_queue_node(rfn, lock, -1); |
561 | } | |
562 | EXPORT_SYMBOL(blk_init_queue); | |
563 | ||
165125e1 | 564 | struct request_queue * |
1946089a CL |
565 | blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id) |
566 | { | |
165125e1 | 567 | struct request_queue *q = blk_alloc_queue_node(GFP_KERNEL, node_id); |
1da177e4 LT |
568 | |
569 | if (!q) | |
570 | return NULL; | |
571 | ||
1946089a | 572 | q->node = node_id; |
8669aafd | 573 | if (blk_init_free_list(q)) { |
8324aa91 | 574 | kmem_cache_free(blk_requestq_cachep, q); |
8669aafd AV |
575 | return NULL; |
576 | } | |
1da177e4 LT |
577 | |
578 | q->request_fn = rfn; | |
1da177e4 LT |
579 | q->prep_rq_fn = NULL; |
580 | q->unplug_fn = generic_unplug_device; | |
bc58ba94 | 581 | q->queue_flags = QUEUE_FLAG_DEFAULT; |
1da177e4 LT |
582 | q->queue_lock = lock; |
583 | ||
f3b144aa JA |
584 | /* |
585 | * This also sets hw/phys segments, boundary and size | |
586 | */ | |
1da177e4 | 587 | blk_queue_make_request(q, __make_request); |
1da177e4 | 588 | |
44ec9542 AS |
589 | q->sg_reserved_size = INT_MAX; |
590 | ||
1da177e4 LT |
591 | /* |
592 | * all done | |
593 | */ | |
594 | if (!elevator_init(q, NULL)) { | |
595 | blk_queue_congestion_threshold(q); | |
596 | return q; | |
597 | } | |
598 | ||
8669aafd | 599 | blk_put_queue(q); |
1da177e4 LT |
600 | return NULL; |
601 | } | |
1946089a | 602 | EXPORT_SYMBOL(blk_init_queue_node); |
1da177e4 | 603 | |
165125e1 | 604 | int blk_get_queue(struct request_queue *q) |
1da177e4 | 605 | { |
fde6ad22 | 606 | if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) { |
483f4afc | 607 | kobject_get(&q->kobj); |
1da177e4 LT |
608 | return 0; |
609 | } | |
610 | ||
611 | return 1; | |
612 | } | |
1da177e4 | 613 | |
165125e1 | 614 | static inline void blk_free_request(struct request_queue *q, struct request *rq) |
1da177e4 | 615 | { |
4aff5e23 | 616 | if (rq->cmd_flags & REQ_ELVPRIV) |
cb98fc8b | 617 | elv_put_request(q, rq); |
1da177e4 LT |
618 | mempool_free(rq, q->rq.rq_pool); |
619 | } | |
620 | ||
1ea25ecb | 621 | static struct request * |
42dad764 | 622 | blk_alloc_request(struct request_queue *q, int flags, int priv, gfp_t gfp_mask) |
1da177e4 LT |
623 | { |
624 | struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask); | |
625 | ||
626 | if (!rq) | |
627 | return NULL; | |
628 | ||
2a4aa30c | 629 | blk_rq_init(q, rq); |
1afb20f3 | 630 | |
42dad764 | 631 | rq->cmd_flags = flags | REQ_ALLOCED; |
1da177e4 | 632 | |
cb98fc8b | 633 | if (priv) { |
cb78b285 | 634 | if (unlikely(elv_set_request(q, rq, gfp_mask))) { |
cb98fc8b TH |
635 | mempool_free(rq, q->rq.rq_pool); |
636 | return NULL; | |
637 | } | |
4aff5e23 | 638 | rq->cmd_flags |= REQ_ELVPRIV; |
cb98fc8b | 639 | } |
1da177e4 | 640 | |
cb98fc8b | 641 | return rq; |
1da177e4 LT |
642 | } |
643 | ||
644 | /* | |
645 | * ioc_batching returns true if the ioc is a valid batching request and | |
646 | * should be given priority access to a request. | |
647 | */ | |
165125e1 | 648 | static inline int ioc_batching(struct request_queue *q, struct io_context *ioc) |
1da177e4 LT |
649 | { |
650 | if (!ioc) | |
651 | return 0; | |
652 | ||
653 | /* | |
654 | * Make sure the process is able to allocate at least 1 request | |
655 | * even if the batch times out, otherwise we could theoretically | |
656 | * lose wakeups. | |
657 | */ | |
658 | return ioc->nr_batch_requests == q->nr_batching || | |
659 | (ioc->nr_batch_requests > 0 | |
660 | && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME)); | |
661 | } | |
662 | ||
663 | /* | |
664 | * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This | |
665 | * will cause the process to be a "batcher" on all queues in the system. This | |
666 | * is the behaviour we want though - once it gets a wakeup it should be given | |
667 | * a nice run. | |
668 | */ | |
165125e1 | 669 | static void ioc_set_batching(struct request_queue *q, struct io_context *ioc) |
1da177e4 LT |
670 | { |
671 | if (!ioc || ioc_batching(q, ioc)) | |
672 | return; | |
673 | ||
674 | ioc->nr_batch_requests = q->nr_batching; | |
675 | ioc->last_waited = jiffies; | |
676 | } | |
677 | ||
1faa16d2 | 678 | static void __freed_request(struct request_queue *q, int sync) |
1da177e4 LT |
679 | { |
680 | struct request_list *rl = &q->rq; | |
681 | ||
1faa16d2 JA |
682 | if (rl->count[sync] < queue_congestion_off_threshold(q)) |
683 | blk_clear_queue_congested(q, sync); | |
1da177e4 | 684 | |
1faa16d2 JA |
685 | if (rl->count[sync] + 1 <= q->nr_requests) { |
686 | if (waitqueue_active(&rl->wait[sync])) | |
687 | wake_up(&rl->wait[sync]); | |
1da177e4 | 688 | |
1faa16d2 | 689 | blk_clear_queue_full(q, sync); |
1da177e4 LT |
690 | } |
691 | } | |
692 | ||
693 | /* | |
694 | * A request has just been released. Account for it, update the full and | |
695 | * congestion status, wake up any waiters. Called under q->queue_lock. | |
696 | */ | |
1faa16d2 | 697 | static void freed_request(struct request_queue *q, int sync, int priv) |
1da177e4 LT |
698 | { |
699 | struct request_list *rl = &q->rq; | |
700 | ||
1faa16d2 | 701 | rl->count[sync]--; |
cb98fc8b TH |
702 | if (priv) |
703 | rl->elvpriv--; | |
1da177e4 | 704 | |
1faa16d2 | 705 | __freed_request(q, sync); |
1da177e4 | 706 | |
1faa16d2 JA |
707 | if (unlikely(rl->starved[sync ^ 1])) |
708 | __freed_request(q, sync ^ 1); | |
1da177e4 LT |
709 | } |
710 | ||
1da177e4 | 711 | /* |
d6344532 NP |
712 | * Get a free request, queue_lock must be held. |
713 | * Returns NULL on failure, with queue_lock held. | |
714 | * Returns !NULL on success, with queue_lock *not held*. | |
1da177e4 | 715 | */ |
165125e1 | 716 | static struct request *get_request(struct request_queue *q, int rw_flags, |
7749a8d4 | 717 | struct bio *bio, gfp_t gfp_mask) |
1da177e4 LT |
718 | { |
719 | struct request *rq = NULL; | |
720 | struct request_list *rl = &q->rq; | |
88ee5ef1 | 721 | struct io_context *ioc = NULL; |
1faa16d2 | 722 | const bool is_sync = rw_is_sync(rw_flags) != 0; |
88ee5ef1 JA |
723 | int may_queue, priv; |
724 | ||
7749a8d4 | 725 | may_queue = elv_may_queue(q, rw_flags); |
88ee5ef1 JA |
726 | if (may_queue == ELV_MQUEUE_NO) |
727 | goto rq_starved; | |
728 | ||
1faa16d2 JA |
729 | if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) { |
730 | if (rl->count[is_sync]+1 >= q->nr_requests) { | |
b5deef90 | 731 | ioc = current_io_context(GFP_ATOMIC, q->node); |
88ee5ef1 JA |
732 | /* |
733 | * The queue will fill after this allocation, so set | |
734 | * it as full, and mark this process as "batching". | |
735 | * This process will be allowed to complete a batch of | |
736 | * requests, others will be blocked. | |
737 | */ | |
1faa16d2 | 738 | if (!blk_queue_full(q, is_sync)) { |
88ee5ef1 | 739 | ioc_set_batching(q, ioc); |
1faa16d2 | 740 | blk_set_queue_full(q, is_sync); |
88ee5ef1 JA |
741 | } else { |
742 | if (may_queue != ELV_MQUEUE_MUST | |
743 | && !ioc_batching(q, ioc)) { | |
744 | /* | |
745 | * The queue is full and the allocating | |
746 | * process is not a "batcher", and not | |
747 | * exempted by the IO scheduler | |
748 | */ | |
749 | goto out; | |
750 | } | |
751 | } | |
1da177e4 | 752 | } |
1faa16d2 | 753 | blk_set_queue_congested(q, is_sync); |
1da177e4 LT |
754 | } |
755 | ||
082cf69e JA |
756 | /* |
757 | * Only allow batching queuers to allocate up to 50% over the defined | |
758 | * limit of requests, otherwise we could have thousands of requests | |
759 | * allocated with any setting of ->nr_requests | |
760 | */ | |
1faa16d2 | 761 | if (rl->count[is_sync] >= (3 * q->nr_requests / 2)) |
082cf69e | 762 | goto out; |
fd782a4a | 763 | |
1faa16d2 JA |
764 | rl->count[is_sync]++; |
765 | rl->starved[is_sync] = 0; | |
cb98fc8b | 766 | |
64521d1a | 767 | priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags); |
cb98fc8b TH |
768 | if (priv) |
769 | rl->elvpriv++; | |
770 | ||
42dad764 JM |
771 | if (blk_queue_io_stat(q)) |
772 | rw_flags |= REQ_IO_STAT; | |
1da177e4 LT |
773 | spin_unlock_irq(q->queue_lock); |
774 | ||
7749a8d4 | 775 | rq = blk_alloc_request(q, rw_flags, priv, gfp_mask); |
88ee5ef1 | 776 | if (unlikely(!rq)) { |
1da177e4 LT |
777 | /* |
778 | * Allocation failed presumably due to memory. Undo anything | |
779 | * we might have messed up. | |
780 | * | |
781 | * Allocating task should really be put onto the front of the | |
782 | * wait queue, but this is pretty rare. | |
783 | */ | |
784 | spin_lock_irq(q->queue_lock); | |
1faa16d2 | 785 | freed_request(q, is_sync, priv); |
1da177e4 LT |
786 | |
787 | /* | |
788 | * in the very unlikely event that allocation failed and no | |
789 | * requests for this direction was pending, mark us starved | |
790 | * so that freeing of a request in the other direction will | |
791 | * notice us. another possible fix would be to split the | |
792 | * rq mempool into READ and WRITE | |
793 | */ | |
794 | rq_starved: | |
1faa16d2 JA |
795 | if (unlikely(rl->count[is_sync] == 0)) |
796 | rl->starved[is_sync] = 1; | |
1da177e4 | 797 | |
1da177e4 LT |
798 | goto out; |
799 | } | |
800 | ||
88ee5ef1 JA |
801 | /* |
802 | * ioc may be NULL here, and ioc_batching will be false. That's | |
803 | * OK, if the queue is under the request limit then requests need | |
804 | * not count toward the nr_batch_requests limit. There will always | |
805 | * be some limit enforced by BLK_BATCH_TIME. | |
806 | */ | |
1da177e4 LT |
807 | if (ioc_batching(q, ioc)) |
808 | ioc->nr_batch_requests--; | |
6728cb0e | 809 | |
1faa16d2 | 810 | trace_block_getrq(q, bio, rw_flags & 1); |
1da177e4 | 811 | out: |
1da177e4 LT |
812 | return rq; |
813 | } | |
814 | ||
815 | /* | |
816 | * No available requests for this queue, unplug the device and wait for some | |
817 | * requests to become available. | |
d6344532 NP |
818 | * |
819 | * Called with q->queue_lock held, and returns with it unlocked. | |
1da177e4 | 820 | */ |
165125e1 | 821 | static struct request *get_request_wait(struct request_queue *q, int rw_flags, |
22e2c507 | 822 | struct bio *bio) |
1da177e4 | 823 | { |
1faa16d2 | 824 | const bool is_sync = rw_is_sync(rw_flags) != 0; |
1da177e4 LT |
825 | struct request *rq; |
826 | ||
7749a8d4 | 827 | rq = get_request(q, rw_flags, bio, GFP_NOIO); |
450991bc NP |
828 | while (!rq) { |
829 | DEFINE_WAIT(wait); | |
05caf8db | 830 | struct io_context *ioc; |
1da177e4 LT |
831 | struct request_list *rl = &q->rq; |
832 | ||
1faa16d2 | 833 | prepare_to_wait_exclusive(&rl->wait[is_sync], &wait, |
1da177e4 LT |
834 | TASK_UNINTERRUPTIBLE); |
835 | ||
1faa16d2 | 836 | trace_block_sleeprq(q, bio, rw_flags & 1); |
1da177e4 | 837 | |
05caf8db ZY |
838 | __generic_unplug_device(q); |
839 | spin_unlock_irq(q->queue_lock); | |
840 | io_schedule(); | |
1da177e4 | 841 | |
05caf8db ZY |
842 | /* |
843 | * After sleeping, we become a "batching" process and | |
844 | * will be able to allocate at least one request, and | |
845 | * up to a big batch of them for a small period time. | |
846 | * See ioc_batching, ioc_set_batching | |
847 | */ | |
848 | ioc = current_io_context(GFP_NOIO, q->node); | |
849 | ioc_set_batching(q, ioc); | |
d6344532 | 850 | |
05caf8db | 851 | spin_lock_irq(q->queue_lock); |
1faa16d2 | 852 | finish_wait(&rl->wait[is_sync], &wait); |
05caf8db ZY |
853 | |
854 | rq = get_request(q, rw_flags, bio, GFP_NOIO); | |
855 | }; | |
1da177e4 LT |
856 | |
857 | return rq; | |
858 | } | |
859 | ||
165125e1 | 860 | struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask) |
1da177e4 LT |
861 | { |
862 | struct request *rq; | |
863 | ||
864 | BUG_ON(rw != READ && rw != WRITE); | |
865 | ||
d6344532 NP |
866 | spin_lock_irq(q->queue_lock); |
867 | if (gfp_mask & __GFP_WAIT) { | |
22e2c507 | 868 | rq = get_request_wait(q, rw, NULL); |
d6344532 | 869 | } else { |
22e2c507 | 870 | rq = get_request(q, rw, NULL, gfp_mask); |
d6344532 NP |
871 | if (!rq) |
872 | spin_unlock_irq(q->queue_lock); | |
873 | } | |
874 | /* q->queue_lock is unlocked at this point */ | |
1da177e4 LT |
875 | |
876 | return rq; | |
877 | } | |
1da177e4 LT |
878 | EXPORT_SYMBOL(blk_get_request); |
879 | ||
dc72ef4a | 880 | /** |
79eb63e9 | 881 | * blk_make_request - given a bio, allocate a corresponding struct request. |
8ebf9756 | 882 | * @q: target request queue |
79eb63e9 BH |
883 | * @bio: The bio describing the memory mappings that will be submitted for IO. |
884 | * It may be a chained-bio properly constructed by block/bio layer. | |
8ebf9756 | 885 | * @gfp_mask: gfp flags to be used for memory allocation |
dc72ef4a | 886 | * |
79eb63e9 BH |
887 | * blk_make_request is the parallel of generic_make_request for BLOCK_PC |
888 | * type commands. Where the struct request needs to be farther initialized by | |
889 | * the caller. It is passed a &struct bio, which describes the memory info of | |
890 | * the I/O transfer. | |
dc72ef4a | 891 | * |
79eb63e9 BH |
892 | * The caller of blk_make_request must make sure that bi_io_vec |
893 | * are set to describe the memory buffers. That bio_data_dir() will return | |
894 | * the needed direction of the request. (And all bio's in the passed bio-chain | |
895 | * are properly set accordingly) | |
896 | * | |
897 | * If called under none-sleepable conditions, mapped bio buffers must not | |
898 | * need bouncing, by calling the appropriate masked or flagged allocator, | |
899 | * suitable for the target device. Otherwise the call to blk_queue_bounce will | |
900 | * BUG. | |
53674ac5 JA |
901 | * |
902 | * WARNING: When allocating/cloning a bio-chain, careful consideration should be | |
903 | * given to how you allocate bios. In particular, you cannot use __GFP_WAIT for | |
904 | * anything but the first bio in the chain. Otherwise you risk waiting for IO | |
905 | * completion of a bio that hasn't been submitted yet, thus resulting in a | |
906 | * deadlock. Alternatively bios should be allocated using bio_kmalloc() instead | |
907 | * of bio_alloc(), as that avoids the mempool deadlock. | |
908 | * If possible a big IO should be split into smaller parts when allocation | |
909 | * fails. Partial allocation should not be an error, or you risk a live-lock. | |
dc72ef4a | 910 | */ |
79eb63e9 BH |
911 | struct request *blk_make_request(struct request_queue *q, struct bio *bio, |
912 | gfp_t gfp_mask) | |
dc72ef4a | 913 | { |
79eb63e9 BH |
914 | struct request *rq = blk_get_request(q, bio_data_dir(bio), gfp_mask); |
915 | ||
916 | if (unlikely(!rq)) | |
917 | return ERR_PTR(-ENOMEM); | |
918 | ||
919 | for_each_bio(bio) { | |
920 | struct bio *bounce_bio = bio; | |
921 | int ret; | |
922 | ||
923 | blk_queue_bounce(q, &bounce_bio); | |
924 | ret = blk_rq_append_bio(q, rq, bounce_bio); | |
925 | if (unlikely(ret)) { | |
926 | blk_put_request(rq); | |
927 | return ERR_PTR(ret); | |
928 | } | |
929 | } | |
930 | ||
931 | return rq; | |
dc72ef4a | 932 | } |
79eb63e9 | 933 | EXPORT_SYMBOL(blk_make_request); |
dc72ef4a | 934 | |
1da177e4 LT |
935 | /** |
936 | * blk_requeue_request - put a request back on queue | |
937 | * @q: request queue where request should be inserted | |
938 | * @rq: request to be inserted | |
939 | * | |
940 | * Description: | |
941 | * Drivers often keep queueing requests until the hardware cannot accept | |
942 | * more, when that condition happens we need to put the request back | |
943 | * on the queue. Must be called with queue lock held. | |
944 | */ | |
165125e1 | 945 | void blk_requeue_request(struct request_queue *q, struct request *rq) |
1da177e4 | 946 | { |
242f9dcb JA |
947 | blk_delete_timer(rq); |
948 | blk_clear_rq_complete(rq); | |
5f3ea37c | 949 | trace_block_rq_requeue(q, rq); |
2056a782 | 950 | |
1da177e4 LT |
951 | if (blk_rq_tagged(rq)) |
952 | blk_queue_end_tag(q, rq); | |
953 | ||
ba396a6c JB |
954 | BUG_ON(blk_queued_rq(rq)); |
955 | ||
1da177e4 LT |
956 | elv_requeue_request(q, rq); |
957 | } | |
1da177e4 LT |
958 | EXPORT_SYMBOL(blk_requeue_request); |
959 | ||
960 | /** | |
710027a4 | 961 | * blk_insert_request - insert a special request into a request queue |
1da177e4 LT |
962 | * @q: request queue where request should be inserted |
963 | * @rq: request to be inserted | |
964 | * @at_head: insert request at head or tail of queue | |
965 | * @data: private data | |
1da177e4 LT |
966 | * |
967 | * Description: | |
968 | * Many block devices need to execute commands asynchronously, so they don't | |
969 | * block the whole kernel from preemption during request execution. This is | |
970 | * accomplished normally by inserting aritficial requests tagged as | |
710027a4 RD |
971 | * REQ_TYPE_SPECIAL in to the corresponding request queue, and letting them |
972 | * be scheduled for actual execution by the request queue. | |
1da177e4 LT |
973 | * |
974 | * We have the option of inserting the head or the tail of the queue. | |
975 | * Typically we use the tail for new ioctls and so forth. We use the head | |
976 | * of the queue for things like a QUEUE_FULL message from a device, or a | |
977 | * host that is unable to accept a particular command. | |
978 | */ | |
165125e1 | 979 | void blk_insert_request(struct request_queue *q, struct request *rq, |
867d1191 | 980 | int at_head, void *data) |
1da177e4 | 981 | { |
867d1191 | 982 | int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK; |
1da177e4 LT |
983 | unsigned long flags; |
984 | ||
985 | /* | |
986 | * tell I/O scheduler that this isn't a regular read/write (ie it | |
987 | * must not attempt merges on this) and that it acts as a soft | |
988 | * barrier | |
989 | */ | |
4aff5e23 | 990 | rq->cmd_type = REQ_TYPE_SPECIAL; |
1da177e4 LT |
991 | |
992 | rq->special = data; | |
993 | ||
994 | spin_lock_irqsave(q->queue_lock, flags); | |
995 | ||
996 | /* | |
997 | * If command is tagged, release the tag | |
998 | */ | |
867d1191 TH |
999 | if (blk_rq_tagged(rq)) |
1000 | blk_queue_end_tag(q, rq); | |
1da177e4 | 1001 | |
b238b3d4 | 1002 | drive_stat_acct(rq, 1); |
867d1191 | 1003 | __elv_add_request(q, rq, where, 0); |
a7f55792 | 1004 | __blk_run_queue(q); |
1da177e4 LT |
1005 | spin_unlock_irqrestore(q->queue_lock, flags); |
1006 | } | |
1da177e4 LT |
1007 | EXPORT_SYMBOL(blk_insert_request); |
1008 | ||
1da177e4 LT |
1009 | /* |
1010 | * add-request adds a request to the linked list. | |
1011 | * queue lock is held and interrupts disabled, as we muck with the | |
1012 | * request queue list. | |
1013 | */ | |
6728cb0e | 1014 | static inline void add_request(struct request_queue *q, struct request *req) |
1da177e4 | 1015 | { |
b238b3d4 | 1016 | drive_stat_acct(req, 1); |
1da177e4 | 1017 | |
1da177e4 LT |
1018 | /* |
1019 | * elevator indicated where it wants this request to be | |
1020 | * inserted at elevator_merge time | |
1021 | */ | |
1022 | __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0); | |
1023 | } | |
6728cb0e | 1024 | |
074a7aca TH |
1025 | static void part_round_stats_single(int cpu, struct hd_struct *part, |
1026 | unsigned long now) | |
1027 | { | |
1028 | if (now == part->stamp) | |
1029 | return; | |
1030 | ||
1031 | if (part->in_flight) { | |
1032 | __part_stat_add(cpu, part, time_in_queue, | |
1033 | part->in_flight * (now - part->stamp)); | |
1034 | __part_stat_add(cpu, part, io_ticks, (now - part->stamp)); | |
1035 | } | |
1036 | part->stamp = now; | |
1037 | } | |
1038 | ||
1039 | /** | |
496aa8a9 RD |
1040 | * part_round_stats() - Round off the performance stats on a struct disk_stats. |
1041 | * @cpu: cpu number for stats access | |
1042 | * @part: target partition | |
1da177e4 LT |
1043 | * |
1044 | * The average IO queue length and utilisation statistics are maintained | |
1045 | * by observing the current state of the queue length and the amount of | |
1046 | * time it has been in this state for. | |
1047 | * | |
1048 | * Normally, that accounting is done on IO completion, but that can result | |
1049 | * in more than a second's worth of IO being accounted for within any one | |
1050 | * second, leading to >100% utilisation. To deal with that, we call this | |
1051 | * function to do a round-off before returning the results when reading | |
1052 | * /proc/diskstats. This accounts immediately for all queue usage up to | |
1053 | * the current jiffies and restarts the counters again. | |
1054 | */ | |
c9959059 | 1055 | void part_round_stats(int cpu, struct hd_struct *part) |
6f2576af JM |
1056 | { |
1057 | unsigned long now = jiffies; | |
1058 | ||
074a7aca TH |
1059 | if (part->partno) |
1060 | part_round_stats_single(cpu, &part_to_disk(part)->part0, now); | |
1061 | part_round_stats_single(cpu, part, now); | |
6f2576af | 1062 | } |
074a7aca | 1063 | EXPORT_SYMBOL_GPL(part_round_stats); |
6f2576af | 1064 | |
1da177e4 LT |
1065 | /* |
1066 | * queue lock must be held | |
1067 | */ | |
165125e1 | 1068 | void __blk_put_request(struct request_queue *q, struct request *req) |
1da177e4 | 1069 | { |
1da177e4 LT |
1070 | if (unlikely(!q)) |
1071 | return; | |
1072 | if (unlikely(--req->ref_count)) | |
1073 | return; | |
1074 | ||
8922e16c TH |
1075 | elv_completed_request(q, req); |
1076 | ||
1cd96c24 BH |
1077 | /* this is a bio leak */ |
1078 | WARN_ON(req->bio != NULL); | |
1079 | ||
1da177e4 LT |
1080 | /* |
1081 | * Request may not have originated from ll_rw_blk. if not, | |
1082 | * it didn't come out of our reserved rq pools | |
1083 | */ | |
49171e5c | 1084 | if (req->cmd_flags & REQ_ALLOCED) { |
1faa16d2 | 1085 | int is_sync = rq_is_sync(req) != 0; |
4aff5e23 | 1086 | int priv = req->cmd_flags & REQ_ELVPRIV; |
1da177e4 | 1087 | |
1da177e4 | 1088 | BUG_ON(!list_empty(&req->queuelist)); |
9817064b | 1089 | BUG_ON(!hlist_unhashed(&req->hash)); |
1da177e4 LT |
1090 | |
1091 | blk_free_request(q, req); | |
1faa16d2 | 1092 | freed_request(q, is_sync, priv); |
1da177e4 LT |
1093 | } |
1094 | } | |
6e39b69e MC |
1095 | EXPORT_SYMBOL_GPL(__blk_put_request); |
1096 | ||
1da177e4 LT |
1097 | void blk_put_request(struct request *req) |
1098 | { | |
8922e16c | 1099 | unsigned long flags; |
165125e1 | 1100 | struct request_queue *q = req->q; |
8922e16c | 1101 | |
52a93ba8 FT |
1102 | spin_lock_irqsave(q->queue_lock, flags); |
1103 | __blk_put_request(q, req); | |
1104 | spin_unlock_irqrestore(q->queue_lock, flags); | |
1da177e4 | 1105 | } |
1da177e4 LT |
1106 | EXPORT_SYMBOL(blk_put_request); |
1107 | ||
86db1e29 | 1108 | void init_request_from_bio(struct request *req, struct bio *bio) |
52d9e675 | 1109 | { |
c7c22e4d | 1110 | req->cpu = bio->bi_comp_cpu; |
4aff5e23 | 1111 | req->cmd_type = REQ_TYPE_FS; |
52d9e675 TH |
1112 | |
1113 | /* | |
1114 | * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST) | |
1115 | */ | |
6000a368 MC |
1116 | if (bio_rw_ahead(bio)) |
1117 | req->cmd_flags |= (REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT | | |
1118 | REQ_FAILFAST_DRIVER); | |
1119 | if (bio_failfast_dev(bio)) | |
1120 | req->cmd_flags |= REQ_FAILFAST_DEV; | |
1121 | if (bio_failfast_transport(bio)) | |
1122 | req->cmd_flags |= REQ_FAILFAST_TRANSPORT; | |
1123 | if (bio_failfast_driver(bio)) | |
1124 | req->cmd_flags |= REQ_FAILFAST_DRIVER; | |
52d9e675 | 1125 | |
fb2dce86 | 1126 | if (unlikely(bio_discard(bio))) { |
e17fc0a1 DW |
1127 | req->cmd_flags |= REQ_DISCARD; |
1128 | if (bio_barrier(bio)) | |
1129 | req->cmd_flags |= REQ_SOFTBARRIER; | |
fb2dce86 | 1130 | req->q->prepare_discard_fn(req->q, req); |
e17fc0a1 | 1131 | } else if (unlikely(bio_barrier(bio))) |
e4025f6c | 1132 | req->cmd_flags |= REQ_HARDBARRIER; |
52d9e675 | 1133 | |
b31dc66a | 1134 | if (bio_sync(bio)) |
4aff5e23 | 1135 | req->cmd_flags |= REQ_RW_SYNC; |
5404bc7a JA |
1136 | if (bio_rw_meta(bio)) |
1137 | req->cmd_flags |= REQ_RW_META; | |
aeb6fafb JA |
1138 | if (bio_noidle(bio)) |
1139 | req->cmd_flags |= REQ_NOIDLE; | |
b31dc66a | 1140 | |
52d9e675 | 1141 | req->errors = 0; |
a2dec7b3 | 1142 | req->__sector = bio->bi_sector; |
52d9e675 | 1143 | req->ioprio = bio_prio(bio); |
bc1c56fd | 1144 | blk_rq_bio_prep(req->q, req, bio); |
52d9e675 TH |
1145 | } |
1146 | ||
644b2d99 JA |
1147 | /* |
1148 | * Only disabling plugging for non-rotational devices if it does tagging | |
1149 | * as well, otherwise we do need the proper merging | |
1150 | */ | |
1151 | static inline bool queue_should_plug(struct request_queue *q) | |
1152 | { | |
1153 | return !(blk_queue_nonrot(q) && blk_queue_tagged(q)); | |
1154 | } | |
1155 | ||
165125e1 | 1156 | static int __make_request(struct request_queue *q, struct bio *bio) |
1da177e4 | 1157 | { |
450991bc | 1158 | struct request *req; |
2e46e8b2 TH |
1159 | int el_ret; |
1160 | unsigned int bytes = bio->bi_size; | |
51da90fc JA |
1161 | const unsigned short prio = bio_prio(bio); |
1162 | const int sync = bio_sync(bio); | |
213d9417 | 1163 | const int unplug = bio_unplug(bio); |
7749a8d4 | 1164 | int rw_flags; |
1da177e4 | 1165 | |
db64f680 N |
1166 | if (bio_barrier(bio) && bio_has_data(bio) && |
1167 | (q->next_ordered == QUEUE_ORDERED_NONE)) { | |
1168 | bio_endio(bio, -EOPNOTSUPP); | |
1169 | return 0; | |
1170 | } | |
1da177e4 LT |
1171 | /* |
1172 | * low level driver can indicate that it wants pages above a | |
1173 | * certain limit bounced to low memory (ie for highmem, or even | |
1174 | * ISA dma in theory) | |
1175 | */ | |
1176 | blk_queue_bounce(q, &bio); | |
1177 | ||
1da177e4 LT |
1178 | spin_lock_irq(q->queue_lock); |
1179 | ||
a7384677 | 1180 | if (unlikely(bio_barrier(bio)) || elv_queue_empty(q)) |
1da177e4 LT |
1181 | goto get_rq; |
1182 | ||
1183 | el_ret = elv_merge(q, &req, bio); | |
1184 | switch (el_ret) { | |
6728cb0e JA |
1185 | case ELEVATOR_BACK_MERGE: |
1186 | BUG_ON(!rq_mergeable(req)); | |
1da177e4 | 1187 | |
6728cb0e JA |
1188 | if (!ll_back_merge_fn(q, req, bio)) |
1189 | break; | |
1da177e4 | 1190 | |
5f3ea37c | 1191 | trace_block_bio_backmerge(q, bio); |
2056a782 | 1192 | |
6728cb0e JA |
1193 | req->biotail->bi_next = bio; |
1194 | req->biotail = bio; | |
a2dec7b3 | 1195 | req->__data_len += bytes; |
6728cb0e | 1196 | req->ioprio = ioprio_best(req->ioprio, prio); |
ab780f1e JA |
1197 | if (!blk_rq_cpu_valid(req)) |
1198 | req->cpu = bio->bi_comp_cpu; | |
6728cb0e JA |
1199 | drive_stat_acct(req, 0); |
1200 | if (!attempt_back_merge(q, req)) | |
1201 | elv_merged_request(q, req, el_ret); | |
1202 | goto out; | |
1da177e4 | 1203 | |
6728cb0e JA |
1204 | case ELEVATOR_FRONT_MERGE: |
1205 | BUG_ON(!rq_mergeable(req)); | |
1da177e4 | 1206 | |
6728cb0e JA |
1207 | if (!ll_front_merge_fn(q, req, bio)) |
1208 | break; | |
1da177e4 | 1209 | |
5f3ea37c | 1210 | trace_block_bio_frontmerge(q, bio); |
2056a782 | 1211 | |
6728cb0e JA |
1212 | bio->bi_next = req->bio; |
1213 | req->bio = bio; | |
1da177e4 | 1214 | |
6728cb0e JA |
1215 | /* |
1216 | * may not be valid. if the low level driver said | |
1217 | * it didn't need a bounce buffer then it better | |
1218 | * not touch req->buffer either... | |
1219 | */ | |
1220 | req->buffer = bio_data(bio); | |
a2dec7b3 TH |
1221 | req->__sector = bio->bi_sector; |
1222 | req->__data_len += bytes; | |
6728cb0e | 1223 | req->ioprio = ioprio_best(req->ioprio, prio); |
ab780f1e JA |
1224 | if (!blk_rq_cpu_valid(req)) |
1225 | req->cpu = bio->bi_comp_cpu; | |
6728cb0e JA |
1226 | drive_stat_acct(req, 0); |
1227 | if (!attempt_front_merge(q, req)) | |
1228 | elv_merged_request(q, req, el_ret); | |
1229 | goto out; | |
1230 | ||
1231 | /* ELV_NO_MERGE: elevator says don't/can't merge. */ | |
1232 | default: | |
1233 | ; | |
1da177e4 LT |
1234 | } |
1235 | ||
450991bc | 1236 | get_rq: |
7749a8d4 JA |
1237 | /* |
1238 | * This sync check and mask will be re-done in init_request_from_bio(), | |
1239 | * but we need to set it earlier to expose the sync flag to the | |
1240 | * rq allocator and io schedulers. | |
1241 | */ | |
1242 | rw_flags = bio_data_dir(bio); | |
1243 | if (sync) | |
1244 | rw_flags |= REQ_RW_SYNC; | |
1245 | ||
1da177e4 | 1246 | /* |
450991bc | 1247 | * Grab a free request. This is might sleep but can not fail. |
d6344532 | 1248 | * Returns with the queue unlocked. |
450991bc | 1249 | */ |
7749a8d4 | 1250 | req = get_request_wait(q, rw_flags, bio); |
d6344532 | 1251 | |
450991bc NP |
1252 | /* |
1253 | * After dropping the lock and possibly sleeping here, our request | |
1254 | * may now be mergeable after it had proven unmergeable (above). | |
1255 | * We don't worry about that case for efficiency. It won't happen | |
1256 | * often, and the elevators are able to handle it. | |
1da177e4 | 1257 | */ |
52d9e675 | 1258 | init_request_from_bio(req, bio); |
1da177e4 | 1259 | |
450991bc | 1260 | spin_lock_irq(q->queue_lock); |
c7c22e4d JA |
1261 | if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags) || |
1262 | bio_flagged(bio, BIO_CPU_AFFINE)) | |
1263 | req->cpu = blk_cpu_to_group(smp_processor_id()); | |
644b2d99 | 1264 | if (queue_should_plug(q) && elv_queue_empty(q)) |
450991bc | 1265 | blk_plug_device(q); |
1da177e4 LT |
1266 | add_request(q, req); |
1267 | out: | |
644b2d99 | 1268 | if (unplug || !queue_should_plug(q)) |
1da177e4 | 1269 | __generic_unplug_device(q); |
1da177e4 LT |
1270 | spin_unlock_irq(q->queue_lock); |
1271 | return 0; | |
1da177e4 LT |
1272 | } |
1273 | ||
1274 | /* | |
1275 | * If bio->bi_dev is a partition, remap the location | |
1276 | */ | |
1277 | static inline void blk_partition_remap(struct bio *bio) | |
1278 | { | |
1279 | struct block_device *bdev = bio->bi_bdev; | |
1280 | ||
bf2de6f5 | 1281 | if (bio_sectors(bio) && bdev != bdev->bd_contains) { |
1da177e4 LT |
1282 | struct hd_struct *p = bdev->bd_part; |
1283 | ||
1da177e4 LT |
1284 | bio->bi_sector += p->start_sect; |
1285 | bio->bi_bdev = bdev->bd_contains; | |
c7149d6b | 1286 | |
5f3ea37c | 1287 | trace_block_remap(bdev_get_queue(bio->bi_bdev), bio, |
22a7c31a | 1288 | bdev->bd_dev, |
c7149d6b | 1289 | bio->bi_sector - p->start_sect); |
1da177e4 LT |
1290 | } |
1291 | } | |
1292 | ||
1da177e4 LT |
1293 | static void handle_bad_sector(struct bio *bio) |
1294 | { | |
1295 | char b[BDEVNAME_SIZE]; | |
1296 | ||
1297 | printk(KERN_INFO "attempt to access beyond end of device\n"); | |
1298 | printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n", | |
1299 | bdevname(bio->bi_bdev, b), | |
1300 | bio->bi_rw, | |
1301 | (unsigned long long)bio->bi_sector + bio_sectors(bio), | |
1302 | (long long)(bio->bi_bdev->bd_inode->i_size >> 9)); | |
1303 | ||
1304 | set_bit(BIO_EOF, &bio->bi_flags); | |
1305 | } | |
1306 | ||
c17bb495 AM |
1307 | #ifdef CONFIG_FAIL_MAKE_REQUEST |
1308 | ||
1309 | static DECLARE_FAULT_ATTR(fail_make_request); | |
1310 | ||
1311 | static int __init setup_fail_make_request(char *str) | |
1312 | { | |
1313 | return setup_fault_attr(&fail_make_request, str); | |
1314 | } | |
1315 | __setup("fail_make_request=", setup_fail_make_request); | |
1316 | ||
1317 | static int should_fail_request(struct bio *bio) | |
1318 | { | |
eddb2e26 TH |
1319 | struct hd_struct *part = bio->bi_bdev->bd_part; |
1320 | ||
1321 | if (part_to_disk(part)->part0.make_it_fail || part->make_it_fail) | |
c17bb495 AM |
1322 | return should_fail(&fail_make_request, bio->bi_size); |
1323 | ||
1324 | return 0; | |
1325 | } | |
1326 | ||
1327 | static int __init fail_make_request_debugfs(void) | |
1328 | { | |
1329 | return init_fault_attr_dentries(&fail_make_request, | |
1330 | "fail_make_request"); | |
1331 | } | |
1332 | ||
1333 | late_initcall(fail_make_request_debugfs); | |
1334 | ||
1335 | #else /* CONFIG_FAIL_MAKE_REQUEST */ | |
1336 | ||
1337 | static inline int should_fail_request(struct bio *bio) | |
1338 | { | |
1339 | return 0; | |
1340 | } | |
1341 | ||
1342 | #endif /* CONFIG_FAIL_MAKE_REQUEST */ | |
1343 | ||
c07e2b41 JA |
1344 | /* |
1345 | * Check whether this bio extends beyond the end of the device. | |
1346 | */ | |
1347 | static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors) | |
1348 | { | |
1349 | sector_t maxsector; | |
1350 | ||
1351 | if (!nr_sectors) | |
1352 | return 0; | |
1353 | ||
1354 | /* Test device or partition size, when known. */ | |
1355 | maxsector = bio->bi_bdev->bd_inode->i_size >> 9; | |
1356 | if (maxsector) { | |
1357 | sector_t sector = bio->bi_sector; | |
1358 | ||
1359 | if (maxsector < nr_sectors || maxsector - nr_sectors < sector) { | |
1360 | /* | |
1361 | * This may well happen - the kernel calls bread() | |
1362 | * without checking the size of the device, e.g., when | |
1363 | * mounting a device. | |
1364 | */ | |
1365 | handle_bad_sector(bio); | |
1366 | return 1; | |
1367 | } | |
1368 | } | |
1369 | ||
1370 | return 0; | |
1371 | } | |
1372 | ||
1da177e4 | 1373 | /** |
710027a4 | 1374 | * generic_make_request - hand a buffer to its device driver for I/O |
1da177e4 LT |
1375 | * @bio: The bio describing the location in memory and on the device. |
1376 | * | |
1377 | * generic_make_request() is used to make I/O requests of block | |
1378 | * devices. It is passed a &struct bio, which describes the I/O that needs | |
1379 | * to be done. | |
1380 | * | |
1381 | * generic_make_request() does not return any status. The | |
1382 | * success/failure status of the request, along with notification of | |
1383 | * completion, is delivered asynchronously through the bio->bi_end_io | |
1384 | * function described (one day) else where. | |
1385 | * | |
1386 | * The caller of generic_make_request must make sure that bi_io_vec | |
1387 | * are set to describe the memory buffer, and that bi_dev and bi_sector are | |
1388 | * set to describe the device address, and the | |
1389 | * bi_end_io and optionally bi_private are set to describe how | |
1390 | * completion notification should be signaled. | |
1391 | * | |
1392 | * generic_make_request and the drivers it calls may use bi_next if this | |
1393 | * bio happens to be merged with someone else, and may change bi_dev and | |
1394 | * bi_sector for remaps as it sees fit. So the values of these fields | |
1395 | * should NOT be depended on after the call to generic_make_request. | |
1396 | */ | |
d89d8796 | 1397 | static inline void __generic_make_request(struct bio *bio) |
1da177e4 | 1398 | { |
165125e1 | 1399 | struct request_queue *q; |
5ddfe969 | 1400 | sector_t old_sector; |
1da177e4 | 1401 | int ret, nr_sectors = bio_sectors(bio); |
2056a782 | 1402 | dev_t old_dev; |
51fd77bd | 1403 | int err = -EIO; |
1da177e4 LT |
1404 | |
1405 | might_sleep(); | |
1da177e4 | 1406 | |
c07e2b41 JA |
1407 | if (bio_check_eod(bio, nr_sectors)) |
1408 | goto end_io; | |
1da177e4 LT |
1409 | |
1410 | /* | |
1411 | * Resolve the mapping until finished. (drivers are | |
1412 | * still free to implement/resolve their own stacking | |
1413 | * by explicitly returning 0) | |
1414 | * | |
1415 | * NOTE: we don't repeat the blk_size check for each new device. | |
1416 | * Stacking drivers are expected to know what they are doing. | |
1417 | */ | |
5ddfe969 | 1418 | old_sector = -1; |
2056a782 | 1419 | old_dev = 0; |
1da177e4 LT |
1420 | do { |
1421 | char b[BDEVNAME_SIZE]; | |
1422 | ||
1423 | q = bdev_get_queue(bio->bi_bdev); | |
a7384677 | 1424 | if (unlikely(!q)) { |
1da177e4 LT |
1425 | printk(KERN_ERR |
1426 | "generic_make_request: Trying to access " | |
1427 | "nonexistent block-device %s (%Lu)\n", | |
1428 | bdevname(bio->bi_bdev, b), | |
1429 | (long long) bio->bi_sector); | |
a7384677 | 1430 | goto end_io; |
1da177e4 LT |
1431 | } |
1432 | ||
ae03bf63 | 1433 | if (unlikely(nr_sectors > queue_max_hw_sectors(q))) { |
6728cb0e | 1434 | printk(KERN_ERR "bio too big device %s (%u > %u)\n", |
ae03bf63 MP |
1435 | bdevname(bio->bi_bdev, b), |
1436 | bio_sectors(bio), | |
1437 | queue_max_hw_sectors(q)); | |
1da177e4 LT |
1438 | goto end_io; |
1439 | } | |
1440 | ||
fde6ad22 | 1441 | if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) |
1da177e4 LT |
1442 | goto end_io; |
1443 | ||
c17bb495 AM |
1444 | if (should_fail_request(bio)) |
1445 | goto end_io; | |
1446 | ||
1da177e4 LT |
1447 | /* |
1448 | * If this device has partitions, remap block n | |
1449 | * of partition p to block n+start(p) of the disk. | |
1450 | */ | |
1451 | blk_partition_remap(bio); | |
1452 | ||
7ba1ba12 MP |
1453 | if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) |
1454 | goto end_io; | |
1455 | ||
5ddfe969 | 1456 | if (old_sector != -1) |
22a7c31a | 1457 | trace_block_remap(q, bio, old_dev, old_sector); |
2056a782 | 1458 | |
5f3ea37c | 1459 | trace_block_bio_queue(q, bio); |
2056a782 | 1460 | |
5ddfe969 | 1461 | old_sector = bio->bi_sector; |
2056a782 JA |
1462 | old_dev = bio->bi_bdev->bd_dev; |
1463 | ||
c07e2b41 JA |
1464 | if (bio_check_eod(bio, nr_sectors)) |
1465 | goto end_io; | |
a7384677 TH |
1466 | |
1467 | if (bio_discard(bio) && !q->prepare_discard_fn) { | |
51fd77bd JA |
1468 | err = -EOPNOTSUPP; |
1469 | goto end_io; | |
1470 | } | |
5ddfe969 | 1471 | |
1da177e4 LT |
1472 | ret = q->make_request_fn(q, bio); |
1473 | } while (ret); | |
a7384677 TH |
1474 | |
1475 | return; | |
1476 | ||
1477 | end_io: | |
1478 | bio_endio(bio, err); | |
1da177e4 LT |
1479 | } |
1480 | ||
d89d8796 NB |
1481 | /* |
1482 | * We only want one ->make_request_fn to be active at a time, | |
1483 | * else stack usage with stacked devices could be a problem. | |
1484 | * So use current->bio_{list,tail} to keep a list of requests | |
1485 | * submited by a make_request_fn function. | |
1486 | * current->bio_tail is also used as a flag to say if | |
1487 | * generic_make_request is currently active in this task or not. | |
1488 | * If it is NULL, then no make_request is active. If it is non-NULL, | |
1489 | * then a make_request is active, and new requests should be added | |
1490 | * at the tail | |
1491 | */ | |
1492 | void generic_make_request(struct bio *bio) | |
1493 | { | |
1494 | if (current->bio_tail) { | |
1495 | /* make_request is active */ | |
1496 | *(current->bio_tail) = bio; | |
1497 | bio->bi_next = NULL; | |
1498 | current->bio_tail = &bio->bi_next; | |
1499 | return; | |
1500 | } | |
1501 | /* following loop may be a bit non-obvious, and so deserves some | |
1502 | * explanation. | |
1503 | * Before entering the loop, bio->bi_next is NULL (as all callers | |
1504 | * ensure that) so we have a list with a single bio. | |
1505 | * We pretend that we have just taken it off a longer list, so | |
1506 | * we assign bio_list to the next (which is NULL) and bio_tail | |
1507 | * to &bio_list, thus initialising the bio_list of new bios to be | |
1508 | * added. __generic_make_request may indeed add some more bios | |
1509 | * through a recursive call to generic_make_request. If it | |
1510 | * did, we find a non-NULL value in bio_list and re-enter the loop | |
1511 | * from the top. In this case we really did just take the bio | |
1512 | * of the top of the list (no pretending) and so fixup bio_list and | |
1513 | * bio_tail or bi_next, and call into __generic_make_request again. | |
1514 | * | |
1515 | * The loop was structured like this to make only one call to | |
1516 | * __generic_make_request (which is important as it is large and | |
1517 | * inlined) and to keep the structure simple. | |
1518 | */ | |
1519 | BUG_ON(bio->bi_next); | |
1520 | do { | |
1521 | current->bio_list = bio->bi_next; | |
1522 | if (bio->bi_next == NULL) | |
1523 | current->bio_tail = ¤t->bio_list; | |
1524 | else | |
1525 | bio->bi_next = NULL; | |
1526 | __generic_make_request(bio); | |
1527 | bio = current->bio_list; | |
1528 | } while (bio); | |
1529 | current->bio_tail = NULL; /* deactivate */ | |
1530 | } | |
1da177e4 LT |
1531 | EXPORT_SYMBOL(generic_make_request); |
1532 | ||
1533 | /** | |
710027a4 | 1534 | * submit_bio - submit a bio to the block device layer for I/O |
1da177e4 LT |
1535 | * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead) |
1536 | * @bio: The &struct bio which describes the I/O | |
1537 | * | |
1538 | * submit_bio() is very similar in purpose to generic_make_request(), and | |
1539 | * uses that function to do most of the work. Both are fairly rough | |
710027a4 | 1540 | * interfaces; @bio must be presetup and ready for I/O. |
1da177e4 LT |
1541 | * |
1542 | */ | |
1543 | void submit_bio(int rw, struct bio *bio) | |
1544 | { | |
1545 | int count = bio_sectors(bio); | |
1546 | ||
22e2c507 | 1547 | bio->bi_rw |= rw; |
1da177e4 | 1548 | |
bf2de6f5 JA |
1549 | /* |
1550 | * If it's a regular read/write or a barrier with data attached, | |
1551 | * go through the normal accounting stuff before submission. | |
1552 | */ | |
a9c701e5 | 1553 | if (bio_has_data(bio)) { |
bf2de6f5 JA |
1554 | if (rw & WRITE) { |
1555 | count_vm_events(PGPGOUT, count); | |
1556 | } else { | |
1557 | task_io_account_read(bio->bi_size); | |
1558 | count_vm_events(PGPGIN, count); | |
1559 | } | |
1560 | ||
1561 | if (unlikely(block_dump)) { | |
1562 | char b[BDEVNAME_SIZE]; | |
1563 | printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n", | |
ba25f9dc | 1564 | current->comm, task_pid_nr(current), |
bf2de6f5 JA |
1565 | (rw & WRITE) ? "WRITE" : "READ", |
1566 | (unsigned long long)bio->bi_sector, | |
6728cb0e | 1567 | bdevname(bio->bi_bdev, b)); |
bf2de6f5 | 1568 | } |
1da177e4 LT |
1569 | } |
1570 | ||
1571 | generic_make_request(bio); | |
1572 | } | |
1da177e4 LT |
1573 | EXPORT_SYMBOL(submit_bio); |
1574 | ||
82124d60 KU |
1575 | /** |
1576 | * blk_rq_check_limits - Helper function to check a request for the queue limit | |
1577 | * @q: the queue | |
1578 | * @rq: the request being checked | |
1579 | * | |
1580 | * Description: | |
1581 | * @rq may have been made based on weaker limitations of upper-level queues | |
1582 | * in request stacking drivers, and it may violate the limitation of @q. | |
1583 | * Since the block layer and the underlying device driver trust @rq | |
1584 | * after it is inserted to @q, it should be checked against @q before | |
1585 | * the insertion using this generic function. | |
1586 | * | |
1587 | * This function should also be useful for request stacking drivers | |
1588 | * in some cases below, so export this fuction. | |
1589 | * Request stacking drivers like request-based dm may change the queue | |
1590 | * limits while requests are in the queue (e.g. dm's table swapping). | |
1591 | * Such request stacking drivers should check those requests agaist | |
1592 | * the new queue limits again when they dispatch those requests, | |
1593 | * although such checkings are also done against the old queue limits | |
1594 | * when submitting requests. | |
1595 | */ | |
1596 | int blk_rq_check_limits(struct request_queue *q, struct request *rq) | |
1597 | { | |
ae03bf63 MP |
1598 | if (blk_rq_sectors(rq) > queue_max_sectors(q) || |
1599 | blk_rq_bytes(rq) > queue_max_hw_sectors(q) << 9) { | |
82124d60 KU |
1600 | printk(KERN_ERR "%s: over max size limit.\n", __func__); |
1601 | return -EIO; | |
1602 | } | |
1603 | ||
1604 | /* | |
1605 | * queue's settings related to segment counting like q->bounce_pfn | |
1606 | * may differ from that of other stacking queues. | |
1607 | * Recalculate it to check the request correctly on this queue's | |
1608 | * limitation. | |
1609 | */ | |
1610 | blk_recalc_rq_segments(rq); | |
ae03bf63 MP |
1611 | if (rq->nr_phys_segments > queue_max_phys_segments(q) || |
1612 | rq->nr_phys_segments > queue_max_hw_segments(q)) { | |
82124d60 KU |
1613 | printk(KERN_ERR "%s: over max segments limit.\n", __func__); |
1614 | return -EIO; | |
1615 | } | |
1616 | ||
1617 | return 0; | |
1618 | } | |
1619 | EXPORT_SYMBOL_GPL(blk_rq_check_limits); | |
1620 | ||
1621 | /** | |
1622 | * blk_insert_cloned_request - Helper for stacking drivers to submit a request | |
1623 | * @q: the queue to submit the request | |
1624 | * @rq: the request being queued | |
1625 | */ | |
1626 | int blk_insert_cloned_request(struct request_queue *q, struct request *rq) | |
1627 | { | |
1628 | unsigned long flags; | |
1629 | ||
1630 | if (blk_rq_check_limits(q, rq)) | |
1631 | return -EIO; | |
1632 | ||
1633 | #ifdef CONFIG_FAIL_MAKE_REQUEST | |
1634 | if (rq->rq_disk && rq->rq_disk->part0.make_it_fail && | |
1635 | should_fail(&fail_make_request, blk_rq_bytes(rq))) | |
1636 | return -EIO; | |
1637 | #endif | |
1638 | ||
1639 | spin_lock_irqsave(q->queue_lock, flags); | |
1640 | ||
1641 | /* | |
1642 | * Submitting request must be dequeued before calling this function | |
1643 | * because it will be linked to another request_queue | |
1644 | */ | |
1645 | BUG_ON(blk_queued_rq(rq)); | |
1646 | ||
1647 | drive_stat_acct(rq, 1); | |
1648 | __elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 0); | |
1649 | ||
1650 | spin_unlock_irqrestore(q->queue_lock, flags); | |
1651 | ||
1652 | return 0; | |
1653 | } | |
1654 | EXPORT_SYMBOL_GPL(blk_insert_cloned_request); | |
1655 | ||
bc58ba94 JA |
1656 | static void blk_account_io_completion(struct request *req, unsigned int bytes) |
1657 | { | |
c2553b58 | 1658 | if (blk_do_io_stat(req)) { |
bc58ba94 JA |
1659 | const int rw = rq_data_dir(req); |
1660 | struct hd_struct *part; | |
1661 | int cpu; | |
1662 | ||
1663 | cpu = part_stat_lock(); | |
83096ebf | 1664 | part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req)); |
bc58ba94 JA |
1665 | part_stat_add(cpu, part, sectors[rw], bytes >> 9); |
1666 | part_stat_unlock(); | |
1667 | } | |
1668 | } | |
1669 | ||
1670 | static void blk_account_io_done(struct request *req) | |
1671 | { | |
bc58ba94 JA |
1672 | /* |
1673 | * Account IO completion. bar_rq isn't accounted as a normal | |
1674 | * IO on queueing nor completion. Accounting the containing | |
1675 | * request is enough. | |
1676 | */ | |
c2553b58 | 1677 | if (blk_do_io_stat(req) && req != &req->q->bar_rq) { |
bc58ba94 JA |
1678 | unsigned long duration = jiffies - req->start_time; |
1679 | const int rw = rq_data_dir(req); | |
1680 | struct hd_struct *part; | |
1681 | int cpu; | |
1682 | ||
1683 | cpu = part_stat_lock(); | |
83096ebf | 1684 | part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req)); |
bc58ba94 JA |
1685 | |
1686 | part_stat_inc(cpu, part, ios[rw]); | |
1687 | part_stat_add(cpu, part, ticks[rw], duration); | |
1688 | part_round_stats(cpu, part); | |
1689 | part_dec_in_flight(part); | |
1690 | ||
1691 | part_stat_unlock(); | |
1692 | } | |
1693 | } | |
1694 | ||
3bcddeac | 1695 | /** |
9934c8c0 TH |
1696 | * blk_peek_request - peek at the top of a request queue |
1697 | * @q: request queue to peek at | |
1698 | * | |
1699 | * Description: | |
1700 | * Return the request at the top of @q. The returned request | |
1701 | * should be started using blk_start_request() before LLD starts | |
1702 | * processing it. | |
1703 | * | |
1704 | * Return: | |
1705 | * Pointer to the request at the top of @q if available. Null | |
1706 | * otherwise. | |
1707 | * | |
1708 | * Context: | |
1709 | * queue_lock must be held. | |
1710 | */ | |
1711 | struct request *blk_peek_request(struct request_queue *q) | |
158dbda0 TH |
1712 | { |
1713 | struct request *rq; | |
1714 | int ret; | |
1715 | ||
1716 | while ((rq = __elv_next_request(q)) != NULL) { | |
1717 | if (!(rq->cmd_flags & REQ_STARTED)) { | |
1718 | /* | |
1719 | * This is the first time the device driver | |
1720 | * sees this request (possibly after | |
1721 | * requeueing). Notify IO scheduler. | |
1722 | */ | |
1723 | if (blk_sorted_rq(rq)) | |
1724 | elv_activate_rq(q, rq); | |
1725 | ||
1726 | /* | |
1727 | * just mark as started even if we don't start | |
1728 | * it, a request that has been delayed should | |
1729 | * not be passed by new incoming requests | |
1730 | */ | |
1731 | rq->cmd_flags |= REQ_STARTED; | |
1732 | trace_block_rq_issue(q, rq); | |
1733 | } | |
1734 | ||
1735 | if (!q->boundary_rq || q->boundary_rq == rq) { | |
1736 | q->end_sector = rq_end_sector(rq); | |
1737 | q->boundary_rq = NULL; | |
1738 | } | |
1739 | ||
1740 | if (rq->cmd_flags & REQ_DONTPREP) | |
1741 | break; | |
1742 | ||
2e46e8b2 | 1743 | if (q->dma_drain_size && blk_rq_bytes(rq)) { |
158dbda0 TH |
1744 | /* |
1745 | * make sure space for the drain appears we | |
1746 | * know we can do this because max_hw_segments | |
1747 | * has been adjusted to be one fewer than the | |
1748 | * device can handle | |
1749 | */ | |
1750 | rq->nr_phys_segments++; | |
1751 | } | |
1752 | ||
1753 | if (!q->prep_rq_fn) | |
1754 | break; | |
1755 | ||
1756 | ret = q->prep_rq_fn(q, rq); | |
1757 | if (ret == BLKPREP_OK) { | |
1758 | break; | |
1759 | } else if (ret == BLKPREP_DEFER) { | |
1760 | /* | |
1761 | * the request may have been (partially) prepped. | |
1762 | * we need to keep this request in the front to | |
1763 | * avoid resource deadlock. REQ_STARTED will | |
1764 | * prevent other fs requests from passing this one. | |
1765 | */ | |
2e46e8b2 | 1766 | if (q->dma_drain_size && blk_rq_bytes(rq) && |
158dbda0 TH |
1767 | !(rq->cmd_flags & REQ_DONTPREP)) { |
1768 | /* | |
1769 | * remove the space for the drain we added | |
1770 | * so that we don't add it again | |
1771 | */ | |
1772 | --rq->nr_phys_segments; | |
1773 | } | |
1774 | ||
1775 | rq = NULL; | |
1776 | break; | |
1777 | } else if (ret == BLKPREP_KILL) { | |
1778 | rq->cmd_flags |= REQ_QUIET; | |
c143dc90 JB |
1779 | /* |
1780 | * Mark this request as started so we don't trigger | |
1781 | * any debug logic in the end I/O path. | |
1782 | */ | |
1783 | blk_start_request(rq); | |
40cbbb78 | 1784 | __blk_end_request_all(rq, -EIO); |
158dbda0 TH |
1785 | } else { |
1786 | printk(KERN_ERR "%s: bad return=%d\n", __func__, ret); | |
1787 | break; | |
1788 | } | |
1789 | } | |
1790 | ||
1791 | return rq; | |
1792 | } | |
9934c8c0 | 1793 | EXPORT_SYMBOL(blk_peek_request); |
158dbda0 | 1794 | |
9934c8c0 | 1795 | void blk_dequeue_request(struct request *rq) |
158dbda0 | 1796 | { |
9934c8c0 TH |
1797 | struct request_queue *q = rq->q; |
1798 | ||
158dbda0 TH |
1799 | BUG_ON(list_empty(&rq->queuelist)); |
1800 | BUG_ON(ELV_ON_HASH(rq)); | |
1801 | ||
1802 | list_del_init(&rq->queuelist); | |
1803 | ||
1804 | /* | |
1805 | * the time frame between a request being removed from the lists | |
1806 | * and to it is freed is accounted as io that is in progress at | |
1807 | * the driver side. | |
1808 | */ | |
1809 | if (blk_account_rq(rq)) | |
0a7ae2ff | 1810 | q->in_flight[rq_is_sync(rq)]++; |
158dbda0 TH |
1811 | } |
1812 | ||
9934c8c0 TH |
1813 | /** |
1814 | * blk_start_request - start request processing on the driver | |
1815 | * @req: request to dequeue | |
1816 | * | |
1817 | * Description: | |
1818 | * Dequeue @req and start timeout timer on it. This hands off the | |
1819 | * request to the driver. | |
1820 | * | |
1821 | * Block internal functions which don't want to start timer should | |
1822 | * call blk_dequeue_request(). | |
1823 | * | |
1824 | * Context: | |
1825 | * queue_lock must be held. | |
1826 | */ | |
1827 | void blk_start_request(struct request *req) | |
1828 | { | |
1829 | blk_dequeue_request(req); | |
1830 | ||
1831 | /* | |
5f49f631 TH |
1832 | * We are now handing the request to the hardware, initialize |
1833 | * resid_len to full count and add the timeout handler. | |
9934c8c0 | 1834 | */ |
5f49f631 | 1835 | req->resid_len = blk_rq_bytes(req); |
dbb66c4b FT |
1836 | if (unlikely(blk_bidi_rq(req))) |
1837 | req->next_rq->resid_len = blk_rq_bytes(req->next_rq); | |
1838 | ||
9934c8c0 TH |
1839 | blk_add_timer(req); |
1840 | } | |
1841 | EXPORT_SYMBOL(blk_start_request); | |
1842 | ||
1843 | /** | |
1844 | * blk_fetch_request - fetch a request from a request queue | |
1845 | * @q: request queue to fetch a request from | |
1846 | * | |
1847 | * Description: | |
1848 | * Return the request at the top of @q. The request is started on | |
1849 | * return and LLD can start processing it immediately. | |
1850 | * | |
1851 | * Return: | |
1852 | * Pointer to the request at the top of @q if available. Null | |
1853 | * otherwise. | |
1854 | * | |
1855 | * Context: | |
1856 | * queue_lock must be held. | |
1857 | */ | |
1858 | struct request *blk_fetch_request(struct request_queue *q) | |
1859 | { | |
1860 | struct request *rq; | |
1861 | ||
1862 | rq = blk_peek_request(q); | |
1863 | if (rq) | |
1864 | blk_start_request(rq); | |
1865 | return rq; | |
1866 | } | |
1867 | EXPORT_SYMBOL(blk_fetch_request); | |
1868 | ||
3bcddeac | 1869 | /** |
2e60e022 | 1870 | * blk_update_request - Special helper function for request stacking drivers |
8ebf9756 | 1871 | * @req: the request being processed |
710027a4 | 1872 | * @error: %0 for success, < %0 for error |
8ebf9756 | 1873 | * @nr_bytes: number of bytes to complete @req |
3bcddeac KU |
1874 | * |
1875 | * Description: | |
8ebf9756 RD |
1876 | * Ends I/O on a number of bytes attached to @req, but doesn't complete |
1877 | * the request structure even if @req doesn't have leftover. | |
1878 | * If @req has leftover, sets it up for the next range of segments. | |
2e60e022 TH |
1879 | * |
1880 | * This special helper function is only for request stacking drivers | |
1881 | * (e.g. request-based dm) so that they can handle partial completion. | |
1882 | * Actual device drivers should use blk_end_request instead. | |
1883 | * | |
1884 | * Passing the result of blk_rq_bytes() as @nr_bytes guarantees | |
1885 | * %false return from this function. | |
3bcddeac KU |
1886 | * |
1887 | * Return: | |
2e60e022 TH |
1888 | * %false - this request doesn't have any more data |
1889 | * %true - this request has more data | |
3bcddeac | 1890 | **/ |
2e60e022 | 1891 | bool blk_update_request(struct request *req, int error, unsigned int nr_bytes) |
1da177e4 | 1892 | { |
5450d3e1 | 1893 | int total_bytes, bio_nbytes, next_idx = 0; |
1da177e4 LT |
1894 | struct bio *bio; |
1895 | ||
2e60e022 TH |
1896 | if (!req->bio) |
1897 | return false; | |
1898 | ||
5f3ea37c | 1899 | trace_block_rq_complete(req->q, req); |
2056a782 | 1900 | |
1da177e4 | 1901 | /* |
6f41469c TH |
1902 | * For fs requests, rq is just carrier of independent bio's |
1903 | * and each partial completion should be handled separately. | |
1904 | * Reset per-request error on each partial completion. | |
1905 | * | |
1906 | * TODO: tj: This is too subtle. It would be better to let | |
1907 | * low level drivers do what they see fit. | |
1da177e4 | 1908 | */ |
6f41469c | 1909 | if (blk_fs_request(req)) |
1da177e4 LT |
1910 | req->errors = 0; |
1911 | ||
6728cb0e JA |
1912 | if (error && (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))) { |
1913 | printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n", | |
1da177e4 | 1914 | req->rq_disk ? req->rq_disk->disk_name : "?", |
83096ebf | 1915 | (unsigned long long)blk_rq_pos(req)); |
1da177e4 LT |
1916 | } |
1917 | ||
bc58ba94 | 1918 | blk_account_io_completion(req, nr_bytes); |
d72d904a | 1919 | |
1da177e4 LT |
1920 | total_bytes = bio_nbytes = 0; |
1921 | while ((bio = req->bio) != NULL) { | |
1922 | int nbytes; | |
1923 | ||
1924 | if (nr_bytes >= bio->bi_size) { | |
1925 | req->bio = bio->bi_next; | |
1926 | nbytes = bio->bi_size; | |
5bb23a68 | 1927 | req_bio_endio(req, bio, nbytes, error); |
1da177e4 LT |
1928 | next_idx = 0; |
1929 | bio_nbytes = 0; | |
1930 | } else { | |
1931 | int idx = bio->bi_idx + next_idx; | |
1932 | ||
af498d7f | 1933 | if (unlikely(idx >= bio->bi_vcnt)) { |
1da177e4 | 1934 | blk_dump_rq_flags(req, "__end_that"); |
6728cb0e | 1935 | printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n", |
af498d7f | 1936 | __func__, idx, bio->bi_vcnt); |
1da177e4 LT |
1937 | break; |
1938 | } | |
1939 | ||
1940 | nbytes = bio_iovec_idx(bio, idx)->bv_len; | |
1941 | BIO_BUG_ON(nbytes > bio->bi_size); | |
1942 | ||
1943 | /* | |
1944 | * not a complete bvec done | |
1945 | */ | |
1946 | if (unlikely(nbytes > nr_bytes)) { | |
1947 | bio_nbytes += nr_bytes; | |
1948 | total_bytes += nr_bytes; | |
1949 | break; | |
1950 | } | |
1951 | ||
1952 | /* | |
1953 | * advance to the next vector | |
1954 | */ | |
1955 | next_idx++; | |
1956 | bio_nbytes += nbytes; | |
1957 | } | |
1958 | ||
1959 | total_bytes += nbytes; | |
1960 | nr_bytes -= nbytes; | |
1961 | ||
6728cb0e JA |
1962 | bio = req->bio; |
1963 | if (bio) { | |
1da177e4 LT |
1964 | /* |
1965 | * end more in this run, or just return 'not-done' | |
1966 | */ | |
1967 | if (unlikely(nr_bytes <= 0)) | |
1968 | break; | |
1969 | } | |
1970 | } | |
1971 | ||
1972 | /* | |
1973 | * completely done | |
1974 | */ | |
2e60e022 TH |
1975 | if (!req->bio) { |
1976 | /* | |
1977 | * Reset counters so that the request stacking driver | |
1978 | * can find how many bytes remain in the request | |
1979 | * later. | |
1980 | */ | |
a2dec7b3 | 1981 | req->__data_len = 0; |
2e60e022 TH |
1982 | return false; |
1983 | } | |
1da177e4 LT |
1984 | |
1985 | /* | |
1986 | * if the request wasn't completed, update state | |
1987 | */ | |
1988 | if (bio_nbytes) { | |
5bb23a68 | 1989 | req_bio_endio(req, bio, bio_nbytes, error); |
1da177e4 LT |
1990 | bio->bi_idx += next_idx; |
1991 | bio_iovec(bio)->bv_offset += nr_bytes; | |
1992 | bio_iovec(bio)->bv_len -= nr_bytes; | |
1993 | } | |
1994 | ||
a2dec7b3 | 1995 | req->__data_len -= total_bytes; |
2e46e8b2 TH |
1996 | req->buffer = bio_data(req->bio); |
1997 | ||
1998 | /* update sector only for requests with clear definition of sector */ | |
1999 | if (blk_fs_request(req) || blk_discard_rq(req)) | |
a2dec7b3 | 2000 | req->__sector += total_bytes >> 9; |
2e46e8b2 TH |
2001 | |
2002 | /* | |
2003 | * If total number of sectors is less than the first segment | |
2004 | * size, something has gone terribly wrong. | |
2005 | */ | |
2006 | if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) { | |
2007 | printk(KERN_ERR "blk: request botched\n"); | |
a2dec7b3 | 2008 | req->__data_len = blk_rq_cur_bytes(req); |
2e46e8b2 TH |
2009 | } |
2010 | ||
2011 | /* recalculate the number of segments */ | |
1da177e4 | 2012 | blk_recalc_rq_segments(req); |
2e46e8b2 | 2013 | |
2e60e022 | 2014 | return true; |
1da177e4 | 2015 | } |
2e60e022 | 2016 | EXPORT_SYMBOL_GPL(blk_update_request); |
1da177e4 | 2017 | |
2e60e022 TH |
2018 | static bool blk_update_bidi_request(struct request *rq, int error, |
2019 | unsigned int nr_bytes, | |
2020 | unsigned int bidi_bytes) | |
5efccd17 | 2021 | { |
2e60e022 TH |
2022 | if (blk_update_request(rq, error, nr_bytes)) |
2023 | return true; | |
5efccd17 | 2024 | |
2e60e022 TH |
2025 | /* Bidi request must be completed as a whole */ |
2026 | if (unlikely(blk_bidi_rq(rq)) && | |
2027 | blk_update_request(rq->next_rq, error, bidi_bytes)) | |
2028 | return true; | |
5efccd17 | 2029 | |
2e60e022 TH |
2030 | add_disk_randomness(rq->rq_disk); |
2031 | ||
2032 | return false; | |
1da177e4 LT |
2033 | } |
2034 | ||
1da177e4 LT |
2035 | /* |
2036 | * queue lock must be held | |
2037 | */ | |
2e60e022 | 2038 | static void blk_finish_request(struct request *req, int error) |
1da177e4 | 2039 | { |
b8286239 KU |
2040 | if (blk_rq_tagged(req)) |
2041 | blk_queue_end_tag(req->q, req); | |
2042 | ||
ba396a6c | 2043 | BUG_ON(blk_queued_rq(req)); |
1da177e4 LT |
2044 | |
2045 | if (unlikely(laptop_mode) && blk_fs_request(req)) | |
2046 | laptop_io_completion(); | |
2047 | ||
e78042e5 MA |
2048 | blk_delete_timer(req); |
2049 | ||
bc58ba94 | 2050 | blk_account_io_done(req); |
b8286239 | 2051 | |
1da177e4 | 2052 | if (req->end_io) |
8ffdc655 | 2053 | req->end_io(req, error); |
b8286239 KU |
2054 | else { |
2055 | if (blk_bidi_rq(req)) | |
2056 | __blk_put_request(req->next_rq->q, req->next_rq); | |
2057 | ||
1da177e4 | 2058 | __blk_put_request(req->q, req); |
b8286239 | 2059 | } |
1da177e4 LT |
2060 | } |
2061 | ||
3b11313a | 2062 | /** |
2e60e022 TH |
2063 | * blk_end_bidi_request - Complete a bidi request |
2064 | * @rq: the request to complete | |
2065 | * @error: %0 for success, < %0 for error | |
2066 | * @nr_bytes: number of bytes to complete @rq | |
2067 | * @bidi_bytes: number of bytes to complete @rq->next_rq | |
a0cd1285 JA |
2068 | * |
2069 | * Description: | |
e3a04fe3 | 2070 | * Ends I/O on a number of bytes attached to @rq and @rq->next_rq. |
2e60e022 TH |
2071 | * Drivers that supports bidi can safely call this member for any |
2072 | * type of request, bidi or uni. In the later case @bidi_bytes is | |
2073 | * just ignored. | |
336cdb40 KU |
2074 | * |
2075 | * Return: | |
2e60e022 TH |
2076 | * %false - we are done with this request |
2077 | * %true - still buffers pending for this request | |
a0cd1285 | 2078 | **/ |
b1f74493 | 2079 | static bool blk_end_bidi_request(struct request *rq, int error, |
32fab448 KU |
2080 | unsigned int nr_bytes, unsigned int bidi_bytes) |
2081 | { | |
336cdb40 | 2082 | struct request_queue *q = rq->q; |
2e60e022 | 2083 | unsigned long flags; |
32fab448 | 2084 | |
2e60e022 TH |
2085 | if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes)) |
2086 | return true; | |
32fab448 | 2087 | |
336cdb40 | 2088 | spin_lock_irqsave(q->queue_lock, flags); |
2e60e022 | 2089 | blk_finish_request(rq, error); |
336cdb40 KU |
2090 | spin_unlock_irqrestore(q->queue_lock, flags); |
2091 | ||
2e60e022 | 2092 | return false; |
32fab448 KU |
2093 | } |
2094 | ||
336cdb40 | 2095 | /** |
2e60e022 TH |
2096 | * __blk_end_bidi_request - Complete a bidi request with queue lock held |
2097 | * @rq: the request to complete | |
710027a4 | 2098 | * @error: %0 for success, < %0 for error |
e3a04fe3 KU |
2099 | * @nr_bytes: number of bytes to complete @rq |
2100 | * @bidi_bytes: number of bytes to complete @rq->next_rq | |
336cdb40 KU |
2101 | * |
2102 | * Description: | |
2e60e022 TH |
2103 | * Identical to blk_end_bidi_request() except that queue lock is |
2104 | * assumed to be locked on entry and remains so on return. | |
336cdb40 KU |
2105 | * |
2106 | * Return: | |
2e60e022 TH |
2107 | * %false - we are done with this request |
2108 | * %true - still buffers pending for this request | |
336cdb40 | 2109 | **/ |
b1f74493 FT |
2110 | static bool __blk_end_bidi_request(struct request *rq, int error, |
2111 | unsigned int nr_bytes, unsigned int bidi_bytes) | |
336cdb40 | 2112 | { |
2e60e022 TH |
2113 | if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes)) |
2114 | return true; | |
336cdb40 | 2115 | |
2e60e022 | 2116 | blk_finish_request(rq, error); |
336cdb40 | 2117 | |
2e60e022 | 2118 | return false; |
336cdb40 | 2119 | } |
e19a3ab0 KU |
2120 | |
2121 | /** | |
2122 | * blk_end_request - Helper function for drivers to complete the request. | |
2123 | * @rq: the request being processed | |
710027a4 | 2124 | * @error: %0 for success, < %0 for error |
e19a3ab0 KU |
2125 | * @nr_bytes: number of bytes to complete |
2126 | * | |
2127 | * Description: | |
2128 | * Ends I/O on a number of bytes attached to @rq. | |
2129 | * If @rq has leftover, sets it up for the next range of segments. | |
2130 | * | |
2131 | * Return: | |
b1f74493 FT |
2132 | * %false - we are done with this request |
2133 | * %true - still buffers pending for this request | |
e19a3ab0 | 2134 | **/ |
b1f74493 | 2135 | bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes) |
e19a3ab0 | 2136 | { |
b1f74493 | 2137 | return blk_end_bidi_request(rq, error, nr_bytes, 0); |
e19a3ab0 | 2138 | } |
56ad1740 | 2139 | EXPORT_SYMBOL(blk_end_request); |
336cdb40 KU |
2140 | |
2141 | /** | |
b1f74493 FT |
2142 | * blk_end_request_all - Helper function for drives to finish the request. |
2143 | * @rq: the request to finish | |
8ebf9756 | 2144 | * @error: %0 for success, < %0 for error |
336cdb40 KU |
2145 | * |
2146 | * Description: | |
b1f74493 FT |
2147 | * Completely finish @rq. |
2148 | */ | |
2149 | void blk_end_request_all(struct request *rq, int error) | |
336cdb40 | 2150 | { |
b1f74493 FT |
2151 | bool pending; |
2152 | unsigned int bidi_bytes = 0; | |
336cdb40 | 2153 | |
b1f74493 FT |
2154 | if (unlikely(blk_bidi_rq(rq))) |
2155 | bidi_bytes = blk_rq_bytes(rq->next_rq); | |
336cdb40 | 2156 | |
b1f74493 FT |
2157 | pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes); |
2158 | BUG_ON(pending); | |
2159 | } | |
56ad1740 | 2160 | EXPORT_SYMBOL(blk_end_request_all); |
336cdb40 | 2161 | |
b1f74493 FT |
2162 | /** |
2163 | * blk_end_request_cur - Helper function to finish the current request chunk. | |
2164 | * @rq: the request to finish the current chunk for | |
8ebf9756 | 2165 | * @error: %0 for success, < %0 for error |
b1f74493 FT |
2166 | * |
2167 | * Description: | |
2168 | * Complete the current consecutively mapped chunk from @rq. | |
2169 | * | |
2170 | * Return: | |
2171 | * %false - we are done with this request | |
2172 | * %true - still buffers pending for this request | |
2173 | */ | |
2174 | bool blk_end_request_cur(struct request *rq, int error) | |
2175 | { | |
2176 | return blk_end_request(rq, error, blk_rq_cur_bytes(rq)); | |
336cdb40 | 2177 | } |
56ad1740 | 2178 | EXPORT_SYMBOL(blk_end_request_cur); |
336cdb40 | 2179 | |
e3a04fe3 | 2180 | /** |
b1f74493 FT |
2181 | * __blk_end_request - Helper function for drivers to complete the request. |
2182 | * @rq: the request being processed | |
2183 | * @error: %0 for success, < %0 for error | |
2184 | * @nr_bytes: number of bytes to complete | |
e3a04fe3 KU |
2185 | * |
2186 | * Description: | |
b1f74493 | 2187 | * Must be called with queue lock held unlike blk_end_request(). |
e3a04fe3 KU |
2188 | * |
2189 | * Return: | |
b1f74493 FT |
2190 | * %false - we are done with this request |
2191 | * %true - still buffers pending for this request | |
e3a04fe3 | 2192 | **/ |
b1f74493 | 2193 | bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes) |
e3a04fe3 | 2194 | { |
b1f74493 | 2195 | return __blk_end_bidi_request(rq, error, nr_bytes, 0); |
e3a04fe3 | 2196 | } |
56ad1740 | 2197 | EXPORT_SYMBOL(__blk_end_request); |
e3a04fe3 | 2198 | |
32fab448 | 2199 | /** |
b1f74493 FT |
2200 | * __blk_end_request_all - Helper function for drives to finish the request. |
2201 | * @rq: the request to finish | |
8ebf9756 | 2202 | * @error: %0 for success, < %0 for error |
32fab448 KU |
2203 | * |
2204 | * Description: | |
b1f74493 | 2205 | * Completely finish @rq. Must be called with queue lock held. |
32fab448 | 2206 | */ |
b1f74493 | 2207 | void __blk_end_request_all(struct request *rq, int error) |
32fab448 | 2208 | { |
b1f74493 FT |
2209 | bool pending; |
2210 | unsigned int bidi_bytes = 0; | |
2211 | ||
2212 | if (unlikely(blk_bidi_rq(rq))) | |
2213 | bidi_bytes = blk_rq_bytes(rq->next_rq); | |
2214 | ||
2215 | pending = __blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes); | |
2216 | BUG_ON(pending); | |
32fab448 | 2217 | } |
56ad1740 | 2218 | EXPORT_SYMBOL(__blk_end_request_all); |
32fab448 | 2219 | |
e19a3ab0 | 2220 | /** |
b1f74493 FT |
2221 | * __blk_end_request_cur - Helper function to finish the current request chunk. |
2222 | * @rq: the request to finish the current chunk for | |
8ebf9756 | 2223 | * @error: %0 for success, < %0 for error |
e19a3ab0 KU |
2224 | * |
2225 | * Description: | |
b1f74493 FT |
2226 | * Complete the current consecutively mapped chunk from @rq. Must |
2227 | * be called with queue lock held. | |
e19a3ab0 KU |
2228 | * |
2229 | * Return: | |
b1f74493 FT |
2230 | * %false - we are done with this request |
2231 | * %true - still buffers pending for this request | |
2232 | */ | |
2233 | bool __blk_end_request_cur(struct request *rq, int error) | |
e19a3ab0 | 2234 | { |
b1f74493 | 2235 | return __blk_end_request(rq, error, blk_rq_cur_bytes(rq)); |
e19a3ab0 | 2236 | } |
56ad1740 | 2237 | EXPORT_SYMBOL(__blk_end_request_cur); |
e19a3ab0 | 2238 | |
86db1e29 JA |
2239 | void blk_rq_bio_prep(struct request_queue *q, struct request *rq, |
2240 | struct bio *bio) | |
1da177e4 | 2241 | { |
d628eaef DW |
2242 | /* Bit 0 (R/W) is identical in rq->cmd_flags and bio->bi_rw, and |
2243 | we want BIO_RW_AHEAD (bit 1) to imply REQ_FAILFAST (bit 1). */ | |
4aff5e23 | 2244 | rq->cmd_flags |= (bio->bi_rw & 3); |
1da177e4 | 2245 | |
fb2dce86 DW |
2246 | if (bio_has_data(bio)) { |
2247 | rq->nr_phys_segments = bio_phys_segments(q, bio); | |
fb2dce86 DW |
2248 | rq->buffer = bio_data(bio); |
2249 | } | |
a2dec7b3 | 2250 | rq->__data_len = bio->bi_size; |
1da177e4 | 2251 | rq->bio = rq->biotail = bio; |
1da177e4 | 2252 | |
66846572 N |
2253 | if (bio->bi_bdev) |
2254 | rq->rq_disk = bio->bi_bdev->bd_disk; | |
2255 | } | |
1da177e4 | 2256 | |
ef9e3fac KU |
2257 | /** |
2258 | * blk_lld_busy - Check if underlying low-level drivers of a device are busy | |
2259 | * @q : the queue of the device being checked | |
2260 | * | |
2261 | * Description: | |
2262 | * Check if underlying low-level drivers of a device are busy. | |
2263 | * If the drivers want to export their busy state, they must set own | |
2264 | * exporting function using blk_queue_lld_busy() first. | |
2265 | * | |
2266 | * Basically, this function is used only by request stacking drivers | |
2267 | * to stop dispatching requests to underlying devices when underlying | |
2268 | * devices are busy. This behavior helps more I/O merging on the queue | |
2269 | * of the request stacking driver and prevents I/O throughput regression | |
2270 | * on burst I/O load. | |
2271 | * | |
2272 | * Return: | |
2273 | * 0 - Not busy (The request stacking driver should dispatch request) | |
2274 | * 1 - Busy (The request stacking driver should stop dispatching request) | |
2275 | */ | |
2276 | int blk_lld_busy(struct request_queue *q) | |
2277 | { | |
2278 | if (q->lld_busy_fn) | |
2279 | return q->lld_busy_fn(q); | |
2280 | ||
2281 | return 0; | |
2282 | } | |
2283 | EXPORT_SYMBOL_GPL(blk_lld_busy); | |
2284 | ||
b0fd271d KU |
2285 | /** |
2286 | * blk_rq_unprep_clone - Helper function to free all bios in a cloned request | |
2287 | * @rq: the clone request to be cleaned up | |
2288 | * | |
2289 | * Description: | |
2290 | * Free all bios in @rq for a cloned request. | |
2291 | */ | |
2292 | void blk_rq_unprep_clone(struct request *rq) | |
2293 | { | |
2294 | struct bio *bio; | |
2295 | ||
2296 | while ((bio = rq->bio) != NULL) { | |
2297 | rq->bio = bio->bi_next; | |
2298 | ||
2299 | bio_put(bio); | |
2300 | } | |
2301 | } | |
2302 | EXPORT_SYMBOL_GPL(blk_rq_unprep_clone); | |
2303 | ||
2304 | /* | |
2305 | * Copy attributes of the original request to the clone request. | |
2306 | * The actual data parts (e.g. ->cmd, ->buffer, ->sense) are not copied. | |
2307 | */ | |
2308 | static void __blk_rq_prep_clone(struct request *dst, struct request *src) | |
2309 | { | |
2310 | dst->cpu = src->cpu; | |
2311 | dst->cmd_flags = (rq_data_dir(src) | REQ_NOMERGE); | |
2312 | dst->cmd_type = src->cmd_type; | |
2313 | dst->__sector = blk_rq_pos(src); | |
2314 | dst->__data_len = blk_rq_bytes(src); | |
2315 | dst->nr_phys_segments = src->nr_phys_segments; | |
2316 | dst->ioprio = src->ioprio; | |
2317 | dst->extra_len = src->extra_len; | |
2318 | } | |
2319 | ||
2320 | /** | |
2321 | * blk_rq_prep_clone - Helper function to setup clone request | |
2322 | * @rq: the request to be setup | |
2323 | * @rq_src: original request to be cloned | |
2324 | * @bs: bio_set that bios for clone are allocated from | |
2325 | * @gfp_mask: memory allocation mask for bio | |
2326 | * @bio_ctr: setup function to be called for each clone bio. | |
2327 | * Returns %0 for success, non %0 for failure. | |
2328 | * @data: private data to be passed to @bio_ctr | |
2329 | * | |
2330 | * Description: | |
2331 | * Clones bios in @rq_src to @rq, and copies attributes of @rq_src to @rq. | |
2332 | * The actual data parts of @rq_src (e.g. ->cmd, ->buffer, ->sense) | |
2333 | * are not copied, and copying such parts is the caller's responsibility. | |
2334 | * Also, pages which the original bios are pointing to are not copied | |
2335 | * and the cloned bios just point same pages. | |
2336 | * So cloned bios must be completed before original bios, which means | |
2337 | * the caller must complete @rq before @rq_src. | |
2338 | */ | |
2339 | int blk_rq_prep_clone(struct request *rq, struct request *rq_src, | |
2340 | struct bio_set *bs, gfp_t gfp_mask, | |
2341 | int (*bio_ctr)(struct bio *, struct bio *, void *), | |
2342 | void *data) | |
2343 | { | |
2344 | struct bio *bio, *bio_src; | |
2345 | ||
2346 | if (!bs) | |
2347 | bs = fs_bio_set; | |
2348 | ||
2349 | blk_rq_init(NULL, rq); | |
2350 | ||
2351 | __rq_for_each_bio(bio_src, rq_src) { | |
2352 | bio = bio_alloc_bioset(gfp_mask, bio_src->bi_max_vecs, bs); | |
2353 | if (!bio) | |
2354 | goto free_and_out; | |
2355 | ||
2356 | __bio_clone(bio, bio_src); | |
2357 | ||
2358 | if (bio_integrity(bio_src) && | |
7878cba9 | 2359 | bio_integrity_clone(bio, bio_src, gfp_mask, bs)) |
b0fd271d KU |
2360 | goto free_and_out; |
2361 | ||
2362 | if (bio_ctr && bio_ctr(bio, bio_src, data)) | |
2363 | goto free_and_out; | |
2364 | ||
2365 | if (rq->bio) { | |
2366 | rq->biotail->bi_next = bio; | |
2367 | rq->biotail = bio; | |
2368 | } else | |
2369 | rq->bio = rq->biotail = bio; | |
2370 | } | |
2371 | ||
2372 | __blk_rq_prep_clone(rq, rq_src); | |
2373 | ||
2374 | return 0; | |
2375 | ||
2376 | free_and_out: | |
2377 | if (bio) | |
2378 | bio_free(bio, bs); | |
2379 | blk_rq_unprep_clone(rq); | |
2380 | ||
2381 | return -ENOMEM; | |
2382 | } | |
2383 | EXPORT_SYMBOL_GPL(blk_rq_prep_clone); | |
2384 | ||
18887ad9 | 2385 | int kblockd_schedule_work(struct request_queue *q, struct work_struct *work) |
1da177e4 LT |
2386 | { |
2387 | return queue_work(kblockd_workqueue, work); | |
2388 | } | |
1da177e4 LT |
2389 | EXPORT_SYMBOL(kblockd_schedule_work); |
2390 | ||
1da177e4 LT |
2391 | int __init blk_dev_init(void) |
2392 | { | |
9eb55b03 NK |
2393 | BUILD_BUG_ON(__REQ_NR_BITS > 8 * |
2394 | sizeof(((struct request *)0)->cmd_flags)); | |
2395 | ||
1da177e4 LT |
2396 | kblockd_workqueue = create_workqueue("kblockd"); |
2397 | if (!kblockd_workqueue) | |
2398 | panic("Failed to create kblockd\n"); | |
2399 | ||
2400 | request_cachep = kmem_cache_create("blkdev_requests", | |
20c2df83 | 2401 | sizeof(struct request), 0, SLAB_PANIC, NULL); |
1da177e4 | 2402 | |
8324aa91 | 2403 | blk_requestq_cachep = kmem_cache_create("blkdev_queue", |
165125e1 | 2404 | sizeof(struct request_queue), 0, SLAB_PANIC, NULL); |
1da177e4 | 2405 | |
d38ecf93 | 2406 | return 0; |
1da177e4 | 2407 | } |
1da177e4 | 2408 |