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