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