block: no need to initialize rq->cmd with blk_get_request
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / block / blk-core.c
1 /*
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>
6 * kernel-doc documentation started by NeilBrown <neilb@cse.unsw.edu.au>
7 * - July2000
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 */
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>
24 #include <linux/completion.h>
25 #include <linux/slab.h>
26 #include <linux/swap.h>
27 #include <linux/writeback.h>
28 #include <linux/task_io_accounting_ops.h>
29 #include <linux/interrupt.h>
30 #include <linux/cpu.h>
31 #include <linux/blktrace_api.h>
32 #include <linux/fault-inject.h>
33
34 #include "blk.h"
35
36 static int __make_request(struct request_queue *q, struct bio *bio);
37
38 /*
39 * For the allocated request tables
40 */
41 static struct kmem_cache *request_cachep;
42
43 /*
44 * For queue allocation
45 */
46 struct kmem_cache *blk_requestq_cachep;
47
48 /*
49 * Controlling structure to kblockd
50 */
51 static struct workqueue_struct *kblockd_workqueue;
52
53 static DEFINE_PER_CPU(struct list_head, blk_cpu_done);
54
55 static void drive_stat_acct(struct request *rq, int new_io)
56 {
57 int rw = rq_data_dir(rq);
58
59 if (!blk_fs_request(rq) || !rq->rq_disk)
60 return;
61
62 if (!new_io) {
63 __all_stat_inc(rq->rq_disk, merges[rw], rq->sector);
64 } else {
65 struct hd_struct *part = get_part(rq->rq_disk, rq->sector);
66 disk_round_stats(rq->rq_disk);
67 rq->rq_disk->in_flight++;
68 if (part) {
69 part_round_stats(part);
70 part->in_flight++;
71 }
72 }
73 }
74
75 void blk_queue_congestion_threshold(struct request_queue *q)
76 {
77 int nr;
78
79 nr = q->nr_requests - (q->nr_requests / 8) + 1;
80 if (nr > q->nr_requests)
81 nr = q->nr_requests;
82 q->nr_congestion_on = nr;
83
84 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
85 if (nr < 1)
86 nr = 1;
87 q->nr_congestion_off = nr;
88 }
89
90 /**
91 * blk_get_backing_dev_info - get the address of a queue's backing_dev_info
92 * @bdev: device
93 *
94 * Locates the passed device's request queue and returns the address of its
95 * backing_dev_info
96 *
97 * Will return NULL if the request queue cannot be located.
98 */
99 struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
100 {
101 struct backing_dev_info *ret = NULL;
102 struct request_queue *q = bdev_get_queue(bdev);
103
104 if (q)
105 ret = &q->backing_dev_info;
106 return ret;
107 }
108 EXPORT_SYMBOL(blk_get_backing_dev_info);
109
110 void rq_init(struct request_queue *q, struct request *rq)
111 {
112 memset(rq, 0, sizeof(*rq));
113
114 INIT_LIST_HEAD(&rq->queuelist);
115 INIT_LIST_HEAD(&rq->donelist);
116 rq->q = q;
117 rq->sector = rq->hard_sector = (sector_t) -1;
118 INIT_HLIST_NODE(&rq->hash);
119 RB_CLEAR_NODE(&rq->rb_node);
120 rq->tag = -1;
121 rq->ref_count = 1;
122 }
123
124 static void req_bio_endio(struct request *rq, struct bio *bio,
125 unsigned int nbytes, int error)
126 {
127 struct request_queue *q = rq->q;
128
129 if (&q->bar_rq != rq) {
130 if (error)
131 clear_bit(BIO_UPTODATE, &bio->bi_flags);
132 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
133 error = -EIO;
134
135 if (unlikely(nbytes > bio->bi_size)) {
136 printk(KERN_ERR "%s: want %u bytes done, %u left\n",
137 __FUNCTION__, nbytes, bio->bi_size);
138 nbytes = bio->bi_size;
139 }
140
141 bio->bi_size -= nbytes;
142 bio->bi_sector += (nbytes >> 9);
143 if (bio->bi_size == 0)
144 bio_endio(bio, error);
145 } else {
146
147 /*
148 * Okay, this is the barrier request in progress, just
149 * record the error;
150 */
151 if (error && !q->orderr)
152 q->orderr = error;
153 }
154 }
155
156 void blk_dump_rq_flags(struct request *rq, char *msg)
157 {
158 int bit;
159
160 printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg,
161 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
162 rq->cmd_flags);
163
164 printk(KERN_INFO " sector %llu, nr/cnr %lu/%u\n",
165 (unsigned long long)rq->sector,
166 rq->nr_sectors,
167 rq->current_nr_sectors);
168 printk(KERN_INFO " bio %p, biotail %p, buffer %p, data %p, len %u\n",
169 rq->bio, rq->biotail,
170 rq->buffer, rq->data,
171 rq->data_len);
172
173 if (blk_pc_request(rq)) {
174 printk(KERN_INFO " cdb: ");
175 for (bit = 0; bit < sizeof(rq->cmd); bit++)
176 printk("%02x ", rq->cmd[bit]);
177 printk("\n");
178 }
179 }
180 EXPORT_SYMBOL(blk_dump_rq_flags);
181
182 /*
183 * "plug" the device if there are no outstanding requests: this will
184 * force the transfer to start only after we have put all the requests
185 * on the list.
186 *
187 * This is called with interrupts off and no requests on the queue and
188 * with the queue lock held.
189 */
190 void blk_plug_device(struct request_queue *q)
191 {
192 WARN_ON(!irqs_disabled());
193
194 /*
195 * don't plug a stopped queue, it must be paired with blk_start_queue()
196 * which will restart the queueing
197 */
198 if (blk_queue_stopped(q))
199 return;
200
201 if (!test_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags)) {
202 __set_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags);
203 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
204 blk_add_trace_generic(q, NULL, 0, BLK_TA_PLUG);
205 }
206 }
207 EXPORT_SYMBOL(blk_plug_device);
208
209 /*
210 * remove the queue from the plugged list, if present. called with
211 * queue lock held and interrupts disabled.
212 */
213 int blk_remove_plug(struct request_queue *q)
214 {
215 WARN_ON(!irqs_disabled());
216
217 if (!test_bit(QUEUE_FLAG_PLUGGED, &q->queue_flags))
218 return 0;
219
220 queue_flag_clear(QUEUE_FLAG_PLUGGED, q);
221 del_timer(&q->unplug_timer);
222 return 1;
223 }
224 EXPORT_SYMBOL(blk_remove_plug);
225
226 /*
227 * remove the plug and let it rip..
228 */
229 void __generic_unplug_device(struct request_queue *q)
230 {
231 if (unlikely(blk_queue_stopped(q)))
232 return;
233
234 if (!blk_remove_plug(q))
235 return;
236
237 q->request_fn(q);
238 }
239 EXPORT_SYMBOL(__generic_unplug_device);
240
241 /**
242 * generic_unplug_device - fire a request queue
243 * @q: The &struct request_queue in question
244 *
245 * Description:
246 * Linux uses plugging to build bigger requests queues before letting
247 * the device have at them. If a queue is plugged, the I/O scheduler
248 * is still adding and merging requests on the queue. Once the queue
249 * gets unplugged, the request_fn defined for the queue is invoked and
250 * transfers started.
251 **/
252 void generic_unplug_device(struct request_queue *q)
253 {
254 spin_lock_irq(q->queue_lock);
255 __generic_unplug_device(q);
256 spin_unlock_irq(q->queue_lock);
257 }
258 EXPORT_SYMBOL(generic_unplug_device);
259
260 static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
261 struct page *page)
262 {
263 struct request_queue *q = bdi->unplug_io_data;
264
265 blk_unplug(q);
266 }
267
268 void blk_unplug_work(struct work_struct *work)
269 {
270 struct request_queue *q =
271 container_of(work, struct request_queue, unplug_work);
272
273 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
274 q->rq.count[READ] + q->rq.count[WRITE]);
275
276 q->unplug_fn(q);
277 }
278
279 void blk_unplug_timeout(unsigned long data)
280 {
281 struct request_queue *q = (struct request_queue *)data;
282
283 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_TIMER, NULL,
284 q->rq.count[READ] + q->rq.count[WRITE]);
285
286 kblockd_schedule_work(&q->unplug_work);
287 }
288
289 void blk_unplug(struct request_queue *q)
290 {
291 /*
292 * devices don't necessarily have an ->unplug_fn defined
293 */
294 if (q->unplug_fn) {
295 blk_add_trace_pdu_int(q, BLK_TA_UNPLUG_IO, NULL,
296 q->rq.count[READ] + q->rq.count[WRITE]);
297
298 q->unplug_fn(q);
299 }
300 }
301 EXPORT_SYMBOL(blk_unplug);
302
303 /**
304 * blk_start_queue - restart a previously stopped queue
305 * @q: The &struct request_queue in question
306 *
307 * Description:
308 * blk_start_queue() will clear the stop flag on the queue, and call
309 * the request_fn for the queue if it was in a stopped state when
310 * entered. Also see blk_stop_queue(). Queue lock must be held.
311 **/
312 void blk_start_queue(struct request_queue *q)
313 {
314 WARN_ON(!irqs_disabled());
315
316 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
317
318 /*
319 * one level of recursion is ok and is much faster than kicking
320 * the unplug handling
321 */
322 if (!test_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) {
323 queue_flag_set(QUEUE_FLAG_REENTER, q);
324 q->request_fn(q);
325 queue_flag_clear(QUEUE_FLAG_REENTER, q);
326 } else {
327 blk_plug_device(q);
328 kblockd_schedule_work(&q->unplug_work);
329 }
330 }
331 EXPORT_SYMBOL(blk_start_queue);
332
333 /**
334 * blk_stop_queue - stop a queue
335 * @q: The &struct request_queue in question
336 *
337 * Description:
338 * The Linux block layer assumes that a block driver will consume all
339 * entries on the request queue when the request_fn strategy is called.
340 * Often this will not happen, because of hardware limitations (queue
341 * depth settings). If a device driver gets a 'queue full' response,
342 * or if it simply chooses not to queue more I/O at one point, it can
343 * call this function to prevent the request_fn from being called until
344 * the driver has signalled it's ready to go again. This happens by calling
345 * blk_start_queue() to restart queue operations. Queue lock must be held.
346 **/
347 void blk_stop_queue(struct request_queue *q)
348 {
349 blk_remove_plug(q);
350 queue_flag_set(QUEUE_FLAG_STOPPED, q);
351 }
352 EXPORT_SYMBOL(blk_stop_queue);
353
354 /**
355 * blk_sync_queue - cancel any pending callbacks on a queue
356 * @q: the queue
357 *
358 * Description:
359 * The block layer may perform asynchronous callback activity
360 * on a queue, such as calling the unplug function after a timeout.
361 * A block device may call blk_sync_queue to ensure that any
362 * such activity is cancelled, thus allowing it to release resources
363 * that the callbacks might use. The caller must already have made sure
364 * that its ->make_request_fn will not re-add plugging prior to calling
365 * this function.
366 *
367 */
368 void blk_sync_queue(struct request_queue *q)
369 {
370 del_timer_sync(&q->unplug_timer);
371 kblockd_flush_work(&q->unplug_work);
372 }
373 EXPORT_SYMBOL(blk_sync_queue);
374
375 /**
376 * blk_run_queue - run a single device queue
377 * @q: The queue to run
378 */
379 void __blk_run_queue(struct request_queue *q)
380 {
381 blk_remove_plug(q);
382
383 /*
384 * Only recurse once to avoid overrunning the stack, let the unplug
385 * handling reinvoke the handler shortly if we already got there.
386 */
387 if (!elv_queue_empty(q)) {
388 if (!test_bit(QUEUE_FLAG_REENTER, &q->queue_flags)) {
389 queue_flag_set(QUEUE_FLAG_REENTER, q);
390 q->request_fn(q);
391 queue_flag_clear(QUEUE_FLAG_REENTER, q);
392 } else {
393 blk_plug_device(q);
394 kblockd_schedule_work(&q->unplug_work);
395 }
396 }
397 }
398 EXPORT_SYMBOL(__blk_run_queue);
399
400 /**
401 * blk_run_queue - run a single device queue
402 * @q: The queue to run
403 */
404 void blk_run_queue(struct request_queue *q)
405 {
406 unsigned long flags;
407
408 spin_lock_irqsave(q->queue_lock, flags);
409 __blk_run_queue(q);
410 spin_unlock_irqrestore(q->queue_lock, flags);
411 }
412 EXPORT_SYMBOL(blk_run_queue);
413
414 void blk_put_queue(struct request_queue *q)
415 {
416 kobject_put(&q->kobj);
417 }
418
419 void blk_cleanup_queue(struct request_queue *q)
420 {
421 mutex_lock(&q->sysfs_lock);
422 queue_flag_set_unlocked(QUEUE_FLAG_DEAD, q);
423 mutex_unlock(&q->sysfs_lock);
424
425 if (q->elevator)
426 elevator_exit(q->elevator);
427
428 blk_put_queue(q);
429 }
430 EXPORT_SYMBOL(blk_cleanup_queue);
431
432 static int blk_init_free_list(struct request_queue *q)
433 {
434 struct request_list *rl = &q->rq;
435
436 rl->count[READ] = rl->count[WRITE] = 0;
437 rl->starved[READ] = rl->starved[WRITE] = 0;
438 rl->elvpriv = 0;
439 init_waitqueue_head(&rl->wait[READ]);
440 init_waitqueue_head(&rl->wait[WRITE]);
441
442 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
443 mempool_free_slab, request_cachep, q->node);
444
445 if (!rl->rq_pool)
446 return -ENOMEM;
447
448 return 0;
449 }
450
451 struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
452 {
453 return blk_alloc_queue_node(gfp_mask, -1);
454 }
455 EXPORT_SYMBOL(blk_alloc_queue);
456
457 struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
458 {
459 struct request_queue *q;
460 int err;
461
462 q = kmem_cache_alloc_node(blk_requestq_cachep,
463 gfp_mask | __GFP_ZERO, node_id);
464 if (!q)
465 return NULL;
466
467 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
468 q->backing_dev_info.unplug_io_data = q;
469 err = bdi_init(&q->backing_dev_info);
470 if (err) {
471 kmem_cache_free(blk_requestq_cachep, q);
472 return NULL;
473 }
474
475 init_timer(&q->unplug_timer);
476
477 kobject_init(&q->kobj, &blk_queue_ktype);
478
479 mutex_init(&q->sysfs_lock);
480
481 return q;
482 }
483 EXPORT_SYMBOL(blk_alloc_queue_node);
484
485 /**
486 * blk_init_queue - prepare a request queue for use with a block device
487 * @rfn: The function to be called to process requests that have been
488 * placed on the queue.
489 * @lock: Request queue spin lock
490 *
491 * Description:
492 * If a block device wishes to use the standard request handling procedures,
493 * which sorts requests and coalesces adjacent requests, then it must
494 * call blk_init_queue(). The function @rfn will be called when there
495 * are requests on the queue that need to be processed. If the device
496 * supports plugging, then @rfn may not be called immediately when requests
497 * are available on the queue, but may be called at some time later instead.
498 * Plugged queues are generally unplugged when a buffer belonging to one
499 * of the requests on the queue is needed, or due to memory pressure.
500 *
501 * @rfn is not required, or even expected, to remove all requests off the
502 * queue, but only as many as it can handle at a time. If it does leave
503 * requests on the queue, it is responsible for arranging that the requests
504 * get dealt with eventually.
505 *
506 * The queue spin lock must be held while manipulating the requests on the
507 * request queue; this lock will be taken also from interrupt context, so irq
508 * disabling is needed for it.
509 *
510 * Function returns a pointer to the initialized request queue, or NULL if
511 * it didn't succeed.
512 *
513 * Note:
514 * blk_init_queue() must be paired with a blk_cleanup_queue() call
515 * when the block device is deactivated (such as at module unload).
516 **/
517
518 struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
519 {
520 return blk_init_queue_node(rfn, lock, -1);
521 }
522 EXPORT_SYMBOL(blk_init_queue);
523
524 struct request_queue *
525 blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
526 {
527 struct request_queue *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
528
529 if (!q)
530 return NULL;
531
532 q->node = node_id;
533 if (blk_init_free_list(q)) {
534 kmem_cache_free(blk_requestq_cachep, q);
535 return NULL;
536 }
537
538 /*
539 * if caller didn't supply a lock, they get per-queue locking with
540 * our embedded lock
541 */
542 if (!lock) {
543 spin_lock_init(&q->__queue_lock);
544 lock = &q->__queue_lock;
545 }
546
547 q->request_fn = rfn;
548 q->prep_rq_fn = NULL;
549 q->unplug_fn = generic_unplug_device;
550 q->queue_flags = (1 << QUEUE_FLAG_CLUSTER);
551 q->queue_lock = lock;
552
553 blk_queue_segment_boundary(q, 0xffffffff);
554
555 blk_queue_make_request(q, __make_request);
556 blk_queue_max_segment_size(q, MAX_SEGMENT_SIZE);
557
558 blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS);
559 blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS);
560
561 q->sg_reserved_size = INT_MAX;
562
563 /*
564 * all done
565 */
566 if (!elevator_init(q, NULL)) {
567 blk_queue_congestion_threshold(q);
568 return q;
569 }
570
571 blk_put_queue(q);
572 return NULL;
573 }
574 EXPORT_SYMBOL(blk_init_queue_node);
575
576 int blk_get_queue(struct request_queue *q)
577 {
578 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
579 kobject_get(&q->kobj);
580 return 0;
581 }
582
583 return 1;
584 }
585
586 static inline void blk_free_request(struct request_queue *q, struct request *rq)
587 {
588 if (rq->cmd_flags & REQ_ELVPRIV)
589 elv_put_request(q, rq);
590 mempool_free(rq, q->rq.rq_pool);
591 }
592
593 static struct request *
594 blk_alloc_request(struct request_queue *q, int rw, int priv, gfp_t gfp_mask)
595 {
596 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
597
598 if (!rq)
599 return NULL;
600
601 rq_init(q, rq);
602
603 /*
604 * first three bits are identical in rq->cmd_flags and bio->bi_rw,
605 * see bio.h and blkdev.h
606 */
607 rq->cmd_flags = rw | REQ_ALLOCED;
608
609 if (priv) {
610 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
611 mempool_free(rq, q->rq.rq_pool);
612 return NULL;
613 }
614 rq->cmd_flags |= REQ_ELVPRIV;
615 }
616
617 return rq;
618 }
619
620 /*
621 * ioc_batching returns true if the ioc is a valid batching request and
622 * should be given priority access to a request.
623 */
624 static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
625 {
626 if (!ioc)
627 return 0;
628
629 /*
630 * Make sure the process is able to allocate at least 1 request
631 * even if the batch times out, otherwise we could theoretically
632 * lose wakeups.
633 */
634 return ioc->nr_batch_requests == q->nr_batching ||
635 (ioc->nr_batch_requests > 0
636 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
637 }
638
639 /*
640 * ioc_set_batching sets ioc to be a new "batcher" if it is not one. This
641 * will cause the process to be a "batcher" on all queues in the system. This
642 * is the behaviour we want though - once it gets a wakeup it should be given
643 * a nice run.
644 */
645 static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
646 {
647 if (!ioc || ioc_batching(q, ioc))
648 return;
649
650 ioc->nr_batch_requests = q->nr_batching;
651 ioc->last_waited = jiffies;
652 }
653
654 static void __freed_request(struct request_queue *q, int rw)
655 {
656 struct request_list *rl = &q->rq;
657
658 if (rl->count[rw] < queue_congestion_off_threshold(q))
659 blk_clear_queue_congested(q, rw);
660
661 if (rl->count[rw] + 1 <= q->nr_requests) {
662 if (waitqueue_active(&rl->wait[rw]))
663 wake_up(&rl->wait[rw]);
664
665 blk_clear_queue_full(q, rw);
666 }
667 }
668
669 /*
670 * A request has just been released. Account for it, update the full and
671 * congestion status, wake up any waiters. Called under q->queue_lock.
672 */
673 static void freed_request(struct request_queue *q, int rw, int priv)
674 {
675 struct request_list *rl = &q->rq;
676
677 rl->count[rw]--;
678 if (priv)
679 rl->elvpriv--;
680
681 __freed_request(q, rw);
682
683 if (unlikely(rl->starved[rw ^ 1]))
684 __freed_request(q, rw ^ 1);
685 }
686
687 #define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist)
688 /*
689 * Get a free request, queue_lock must be held.
690 * Returns NULL on failure, with queue_lock held.
691 * Returns !NULL on success, with queue_lock *not held*.
692 */
693 static struct request *get_request(struct request_queue *q, int rw_flags,
694 struct bio *bio, gfp_t gfp_mask)
695 {
696 struct request *rq = NULL;
697 struct request_list *rl = &q->rq;
698 struct io_context *ioc = NULL;
699 const int rw = rw_flags & 0x01;
700 int may_queue, priv;
701
702 may_queue = elv_may_queue(q, rw_flags);
703 if (may_queue == ELV_MQUEUE_NO)
704 goto rq_starved;
705
706 if (rl->count[rw]+1 >= queue_congestion_on_threshold(q)) {
707 if (rl->count[rw]+1 >= q->nr_requests) {
708 ioc = current_io_context(GFP_ATOMIC, q->node);
709 /*
710 * The queue will fill after this allocation, so set
711 * it as full, and mark this process as "batching".
712 * This process will be allowed to complete a batch of
713 * requests, others will be blocked.
714 */
715 if (!blk_queue_full(q, rw)) {
716 ioc_set_batching(q, ioc);
717 blk_set_queue_full(q, rw);
718 } else {
719 if (may_queue != ELV_MQUEUE_MUST
720 && !ioc_batching(q, ioc)) {
721 /*
722 * The queue is full and the allocating
723 * process is not a "batcher", and not
724 * exempted by the IO scheduler
725 */
726 goto out;
727 }
728 }
729 }
730 blk_set_queue_congested(q, rw);
731 }
732
733 /*
734 * Only allow batching queuers to allocate up to 50% over the defined
735 * limit of requests, otherwise we could have thousands of requests
736 * allocated with any setting of ->nr_requests
737 */
738 if (rl->count[rw] >= (3 * q->nr_requests / 2))
739 goto out;
740
741 rl->count[rw]++;
742 rl->starved[rw] = 0;
743
744 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
745 if (priv)
746 rl->elvpriv++;
747
748 spin_unlock_irq(q->queue_lock);
749
750 rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
751 if (unlikely(!rq)) {
752 /*
753 * Allocation failed presumably due to memory. Undo anything
754 * we might have messed up.
755 *
756 * Allocating task should really be put onto the front of the
757 * wait queue, but this is pretty rare.
758 */
759 spin_lock_irq(q->queue_lock);
760 freed_request(q, rw, priv);
761
762 /*
763 * in the very unlikely event that allocation failed and no
764 * requests for this direction was pending, mark us starved
765 * so that freeing of a request in the other direction will
766 * notice us. another possible fix would be to split the
767 * rq mempool into READ and WRITE
768 */
769 rq_starved:
770 if (unlikely(rl->count[rw] == 0))
771 rl->starved[rw] = 1;
772
773 goto out;
774 }
775
776 /*
777 * ioc may be NULL here, and ioc_batching will be false. That's
778 * OK, if the queue is under the request limit then requests need
779 * not count toward the nr_batch_requests limit. There will always
780 * be some limit enforced by BLK_BATCH_TIME.
781 */
782 if (ioc_batching(q, ioc))
783 ioc->nr_batch_requests--;
784
785 blk_add_trace_generic(q, bio, rw, BLK_TA_GETRQ);
786 out:
787 return rq;
788 }
789
790 /*
791 * No available requests for this queue, unplug the device and wait for some
792 * requests to become available.
793 *
794 * Called with q->queue_lock held, and returns with it unlocked.
795 */
796 static struct request *get_request_wait(struct request_queue *q, int rw_flags,
797 struct bio *bio)
798 {
799 const int rw = rw_flags & 0x01;
800 struct request *rq;
801
802 rq = get_request(q, rw_flags, bio, GFP_NOIO);
803 while (!rq) {
804 DEFINE_WAIT(wait);
805 struct request_list *rl = &q->rq;
806
807 prepare_to_wait_exclusive(&rl->wait[rw], &wait,
808 TASK_UNINTERRUPTIBLE);
809
810 rq = get_request(q, rw_flags, bio, GFP_NOIO);
811
812 if (!rq) {
813 struct io_context *ioc;
814
815 blk_add_trace_generic(q, bio, rw, BLK_TA_SLEEPRQ);
816
817 __generic_unplug_device(q);
818 spin_unlock_irq(q->queue_lock);
819 io_schedule();
820
821 /*
822 * After sleeping, we become a "batching" process and
823 * will be able to allocate at least one request, and
824 * up to a big batch of them for a small period time.
825 * See ioc_batching, ioc_set_batching
826 */
827 ioc = current_io_context(GFP_NOIO, q->node);
828 ioc_set_batching(q, ioc);
829
830 spin_lock_irq(q->queue_lock);
831 }
832 finish_wait(&rl->wait[rw], &wait);
833 }
834
835 return rq;
836 }
837
838 struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
839 {
840 struct request *rq;
841
842 BUG_ON(rw != READ && rw != WRITE);
843
844 spin_lock_irq(q->queue_lock);
845 if (gfp_mask & __GFP_WAIT) {
846 rq = get_request_wait(q, rw, NULL);
847 } else {
848 rq = get_request(q, rw, NULL, gfp_mask);
849 if (!rq)
850 spin_unlock_irq(q->queue_lock);
851 }
852 /* q->queue_lock is unlocked at this point */
853
854 return rq;
855 }
856 EXPORT_SYMBOL(blk_get_request);
857
858 /**
859 * blk_start_queueing - initiate dispatch of requests to device
860 * @q: request queue to kick into gear
861 *
862 * This is basically a helper to remove the need to know whether a queue
863 * is plugged or not if someone just wants to initiate dispatch of requests
864 * for this queue.
865 *
866 * The queue lock must be held with interrupts disabled.
867 */
868 void blk_start_queueing(struct request_queue *q)
869 {
870 if (!blk_queue_plugged(q))
871 q->request_fn(q);
872 else
873 __generic_unplug_device(q);
874 }
875 EXPORT_SYMBOL(blk_start_queueing);
876
877 /**
878 * blk_requeue_request - put a request back on queue
879 * @q: request queue where request should be inserted
880 * @rq: request to be inserted
881 *
882 * Description:
883 * Drivers often keep queueing requests until the hardware cannot accept
884 * more, when that condition happens we need to put the request back
885 * on the queue. Must be called with queue lock held.
886 */
887 void blk_requeue_request(struct request_queue *q, struct request *rq)
888 {
889 blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
890
891 if (blk_rq_tagged(rq))
892 blk_queue_end_tag(q, rq);
893
894 elv_requeue_request(q, rq);
895 }
896 EXPORT_SYMBOL(blk_requeue_request);
897
898 /**
899 * blk_insert_request - insert a special request in to a request queue
900 * @q: request queue where request should be inserted
901 * @rq: request to be inserted
902 * @at_head: insert request at head or tail of queue
903 * @data: private data
904 *
905 * Description:
906 * Many block devices need to execute commands asynchronously, so they don't
907 * block the whole kernel from preemption during request execution. This is
908 * accomplished normally by inserting aritficial requests tagged as
909 * REQ_SPECIAL in to the corresponding request queue, and letting them be
910 * scheduled for actual execution by the request queue.
911 *
912 * We have the option of inserting the head or the tail of the queue.
913 * Typically we use the tail for new ioctls and so forth. We use the head
914 * of the queue for things like a QUEUE_FULL message from a device, or a
915 * host that is unable to accept a particular command.
916 */
917 void blk_insert_request(struct request_queue *q, struct request *rq,
918 int at_head, void *data)
919 {
920 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
921 unsigned long flags;
922
923 /*
924 * tell I/O scheduler that this isn't a regular read/write (ie it
925 * must not attempt merges on this) and that it acts as a soft
926 * barrier
927 */
928 rq->cmd_type = REQ_TYPE_SPECIAL;
929 rq->cmd_flags |= REQ_SOFTBARRIER;
930
931 rq->special = data;
932
933 spin_lock_irqsave(q->queue_lock, flags);
934
935 /*
936 * If command is tagged, release the tag
937 */
938 if (blk_rq_tagged(rq))
939 blk_queue_end_tag(q, rq);
940
941 drive_stat_acct(rq, 1);
942 __elv_add_request(q, rq, where, 0);
943 blk_start_queueing(q);
944 spin_unlock_irqrestore(q->queue_lock, flags);
945 }
946 EXPORT_SYMBOL(blk_insert_request);
947
948 /*
949 * add-request adds a request to the linked list.
950 * queue lock is held and interrupts disabled, as we muck with the
951 * request queue list.
952 */
953 static inline void add_request(struct request_queue *q, struct request *req)
954 {
955 drive_stat_acct(req, 1);
956
957 /*
958 * elevator indicated where it wants this request to be
959 * inserted at elevator_merge time
960 */
961 __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
962 }
963
964 /*
965 * disk_round_stats() - Round off the performance stats on a struct
966 * disk_stats.
967 *
968 * The average IO queue length and utilisation statistics are maintained
969 * by observing the current state of the queue length and the amount of
970 * time it has been in this state for.
971 *
972 * Normally, that accounting is done on IO completion, but that can result
973 * in more than a second's worth of IO being accounted for within any one
974 * second, leading to >100% utilisation. To deal with that, we call this
975 * function to do a round-off before returning the results when reading
976 * /proc/diskstats. This accounts immediately for all queue usage up to
977 * the current jiffies and restarts the counters again.
978 */
979 void disk_round_stats(struct gendisk *disk)
980 {
981 unsigned long now = jiffies;
982
983 if (now == disk->stamp)
984 return;
985
986 if (disk->in_flight) {
987 __disk_stat_add(disk, time_in_queue,
988 disk->in_flight * (now - disk->stamp));
989 __disk_stat_add(disk, io_ticks, (now - disk->stamp));
990 }
991 disk->stamp = now;
992 }
993 EXPORT_SYMBOL_GPL(disk_round_stats);
994
995 void part_round_stats(struct hd_struct *part)
996 {
997 unsigned long now = jiffies;
998
999 if (now == part->stamp)
1000 return;
1001
1002 if (part->in_flight) {
1003 __part_stat_add(part, time_in_queue,
1004 part->in_flight * (now - part->stamp));
1005 __part_stat_add(part, io_ticks, (now - part->stamp));
1006 }
1007 part->stamp = now;
1008 }
1009
1010 /*
1011 * queue lock must be held
1012 */
1013 void __blk_put_request(struct request_queue *q, struct request *req)
1014 {
1015 if (unlikely(!q))
1016 return;
1017 if (unlikely(--req->ref_count))
1018 return;
1019
1020 elv_completed_request(q, req);
1021
1022 /*
1023 * Request may not have originated from ll_rw_blk. if not,
1024 * it didn't come out of our reserved rq pools
1025 */
1026 if (req->cmd_flags & REQ_ALLOCED) {
1027 int rw = rq_data_dir(req);
1028 int priv = req->cmd_flags & REQ_ELVPRIV;
1029
1030 BUG_ON(!list_empty(&req->queuelist));
1031 BUG_ON(!hlist_unhashed(&req->hash));
1032
1033 blk_free_request(q, req);
1034 freed_request(q, rw, priv);
1035 }
1036 }
1037 EXPORT_SYMBOL_GPL(__blk_put_request);
1038
1039 void blk_put_request(struct request *req)
1040 {
1041 unsigned long flags;
1042 struct request_queue *q = req->q;
1043
1044 /*
1045 * Gee, IDE calls in w/ NULL q. Fix IDE and remove the
1046 * following if (q) test.
1047 */
1048 if (q) {
1049 spin_lock_irqsave(q->queue_lock, flags);
1050 __blk_put_request(q, req);
1051 spin_unlock_irqrestore(q->queue_lock, flags);
1052 }
1053 }
1054 EXPORT_SYMBOL(blk_put_request);
1055
1056 void init_request_from_bio(struct request *req, struct bio *bio)
1057 {
1058 req->cmd_type = REQ_TYPE_FS;
1059
1060 /*
1061 * inherit FAILFAST from bio (for read-ahead, and explicit FAILFAST)
1062 */
1063 if (bio_rw_ahead(bio) || bio_failfast(bio))
1064 req->cmd_flags |= REQ_FAILFAST;
1065
1066 /*
1067 * REQ_BARRIER implies no merging, but lets make it explicit
1068 */
1069 if (unlikely(bio_barrier(bio)))
1070 req->cmd_flags |= (REQ_HARDBARRIER | REQ_NOMERGE);
1071
1072 if (bio_sync(bio))
1073 req->cmd_flags |= REQ_RW_SYNC;
1074 if (bio_rw_meta(bio))
1075 req->cmd_flags |= REQ_RW_META;
1076
1077 req->errors = 0;
1078 req->hard_sector = req->sector = bio->bi_sector;
1079 req->ioprio = bio_prio(bio);
1080 req->start_time = jiffies;
1081 blk_rq_bio_prep(req->q, req, bio);
1082 }
1083
1084 static int __make_request(struct request_queue *q, struct bio *bio)
1085 {
1086 struct request *req;
1087 int el_ret, nr_sectors, barrier, err;
1088 const unsigned short prio = bio_prio(bio);
1089 const int sync = bio_sync(bio);
1090 int rw_flags;
1091
1092 nr_sectors = bio_sectors(bio);
1093
1094 /*
1095 * low level driver can indicate that it wants pages above a
1096 * certain limit bounced to low memory (ie for highmem, or even
1097 * ISA dma in theory)
1098 */
1099 blk_queue_bounce(q, &bio);
1100
1101 barrier = bio_barrier(bio);
1102 if (unlikely(barrier) && (q->next_ordered == QUEUE_ORDERED_NONE)) {
1103 err = -EOPNOTSUPP;
1104 goto end_io;
1105 }
1106
1107 spin_lock_irq(q->queue_lock);
1108
1109 if (unlikely(barrier) || elv_queue_empty(q))
1110 goto get_rq;
1111
1112 el_ret = elv_merge(q, &req, bio);
1113 switch (el_ret) {
1114 case ELEVATOR_BACK_MERGE:
1115 BUG_ON(!rq_mergeable(req));
1116
1117 if (!ll_back_merge_fn(q, req, bio))
1118 break;
1119
1120 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
1121
1122 req->biotail->bi_next = bio;
1123 req->biotail = bio;
1124 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
1125 req->ioprio = ioprio_best(req->ioprio, prio);
1126 drive_stat_acct(req, 0);
1127 if (!attempt_back_merge(q, req))
1128 elv_merged_request(q, req, el_ret);
1129 goto out;
1130
1131 case ELEVATOR_FRONT_MERGE:
1132 BUG_ON(!rq_mergeable(req));
1133
1134 if (!ll_front_merge_fn(q, req, bio))
1135 break;
1136
1137 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
1138
1139 bio->bi_next = req->bio;
1140 req->bio = bio;
1141
1142 /*
1143 * may not be valid. if the low level driver said
1144 * it didn't need a bounce buffer then it better
1145 * not touch req->buffer either...
1146 */
1147 req->buffer = bio_data(bio);
1148 req->current_nr_sectors = bio_cur_sectors(bio);
1149 req->hard_cur_sectors = req->current_nr_sectors;
1150 req->sector = req->hard_sector = bio->bi_sector;
1151 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
1152 req->ioprio = ioprio_best(req->ioprio, prio);
1153 drive_stat_acct(req, 0);
1154 if (!attempt_front_merge(q, req))
1155 elv_merged_request(q, req, el_ret);
1156 goto out;
1157
1158 /* ELV_NO_MERGE: elevator says don't/can't merge. */
1159 default:
1160 ;
1161 }
1162
1163 get_rq:
1164 /*
1165 * This sync check and mask will be re-done in init_request_from_bio(),
1166 * but we need to set it earlier to expose the sync flag to the
1167 * rq allocator and io schedulers.
1168 */
1169 rw_flags = bio_data_dir(bio);
1170 if (sync)
1171 rw_flags |= REQ_RW_SYNC;
1172
1173 /*
1174 * Grab a free request. This is might sleep but can not fail.
1175 * Returns with the queue unlocked.
1176 */
1177 req = get_request_wait(q, rw_flags, bio);
1178
1179 /*
1180 * After dropping the lock and possibly sleeping here, our request
1181 * may now be mergeable after it had proven unmergeable (above).
1182 * We don't worry about that case for efficiency. It won't happen
1183 * often, and the elevators are able to handle it.
1184 */
1185 init_request_from_bio(req, bio);
1186
1187 spin_lock_irq(q->queue_lock);
1188 if (elv_queue_empty(q))
1189 blk_plug_device(q);
1190 add_request(q, req);
1191 out:
1192 if (sync)
1193 __generic_unplug_device(q);
1194
1195 spin_unlock_irq(q->queue_lock);
1196 return 0;
1197
1198 end_io:
1199 bio_endio(bio, err);
1200 return 0;
1201 }
1202
1203 /*
1204 * If bio->bi_dev is a partition, remap the location
1205 */
1206 static inline void blk_partition_remap(struct bio *bio)
1207 {
1208 struct block_device *bdev = bio->bi_bdev;
1209
1210 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
1211 struct hd_struct *p = bdev->bd_part;
1212
1213 bio->bi_sector += p->start_sect;
1214 bio->bi_bdev = bdev->bd_contains;
1215
1216 blk_add_trace_remap(bdev_get_queue(bio->bi_bdev), bio,
1217 bdev->bd_dev, bio->bi_sector,
1218 bio->bi_sector - p->start_sect);
1219 }
1220 }
1221
1222 static void handle_bad_sector(struct bio *bio)
1223 {
1224 char b[BDEVNAME_SIZE];
1225
1226 printk(KERN_INFO "attempt to access beyond end of device\n");
1227 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
1228 bdevname(bio->bi_bdev, b),
1229 bio->bi_rw,
1230 (unsigned long long)bio->bi_sector + bio_sectors(bio),
1231 (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
1232
1233 set_bit(BIO_EOF, &bio->bi_flags);
1234 }
1235
1236 #ifdef CONFIG_FAIL_MAKE_REQUEST
1237
1238 static DECLARE_FAULT_ATTR(fail_make_request);
1239
1240 static int __init setup_fail_make_request(char *str)
1241 {
1242 return setup_fault_attr(&fail_make_request, str);
1243 }
1244 __setup("fail_make_request=", setup_fail_make_request);
1245
1246 static int should_fail_request(struct bio *bio)
1247 {
1248 if ((bio->bi_bdev->bd_disk->flags & GENHD_FL_FAIL) ||
1249 (bio->bi_bdev->bd_part && bio->bi_bdev->bd_part->make_it_fail))
1250 return should_fail(&fail_make_request, bio->bi_size);
1251
1252 return 0;
1253 }
1254
1255 static int __init fail_make_request_debugfs(void)
1256 {
1257 return init_fault_attr_dentries(&fail_make_request,
1258 "fail_make_request");
1259 }
1260
1261 late_initcall(fail_make_request_debugfs);
1262
1263 #else /* CONFIG_FAIL_MAKE_REQUEST */
1264
1265 static inline int should_fail_request(struct bio *bio)
1266 {
1267 return 0;
1268 }
1269
1270 #endif /* CONFIG_FAIL_MAKE_REQUEST */
1271
1272 /*
1273 * Check whether this bio extends beyond the end of the device.
1274 */
1275 static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
1276 {
1277 sector_t maxsector;
1278
1279 if (!nr_sectors)
1280 return 0;
1281
1282 /* Test device or partition size, when known. */
1283 maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
1284 if (maxsector) {
1285 sector_t sector = bio->bi_sector;
1286
1287 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
1288 /*
1289 * This may well happen - the kernel calls bread()
1290 * without checking the size of the device, e.g., when
1291 * mounting a device.
1292 */
1293 handle_bad_sector(bio);
1294 return 1;
1295 }
1296 }
1297
1298 return 0;
1299 }
1300
1301 /**
1302 * generic_make_request: hand a buffer to its device driver for I/O
1303 * @bio: The bio describing the location in memory and on the device.
1304 *
1305 * generic_make_request() is used to make I/O requests of block
1306 * devices. It is passed a &struct bio, which describes the I/O that needs
1307 * to be done.
1308 *
1309 * generic_make_request() does not return any status. The
1310 * success/failure status of the request, along with notification of
1311 * completion, is delivered asynchronously through the bio->bi_end_io
1312 * function described (one day) else where.
1313 *
1314 * The caller of generic_make_request must make sure that bi_io_vec
1315 * are set to describe the memory buffer, and that bi_dev and bi_sector are
1316 * set to describe the device address, and the
1317 * bi_end_io and optionally bi_private are set to describe how
1318 * completion notification should be signaled.
1319 *
1320 * generic_make_request and the drivers it calls may use bi_next if this
1321 * bio happens to be merged with someone else, and may change bi_dev and
1322 * bi_sector for remaps as it sees fit. So the values of these fields
1323 * should NOT be depended on after the call to generic_make_request.
1324 */
1325 static inline void __generic_make_request(struct bio *bio)
1326 {
1327 struct request_queue *q;
1328 sector_t old_sector;
1329 int ret, nr_sectors = bio_sectors(bio);
1330 dev_t old_dev;
1331 int err = -EIO;
1332
1333 might_sleep();
1334
1335 if (bio_check_eod(bio, nr_sectors))
1336 goto end_io;
1337
1338 /*
1339 * Resolve the mapping until finished. (drivers are
1340 * still free to implement/resolve their own stacking
1341 * by explicitly returning 0)
1342 *
1343 * NOTE: we don't repeat the blk_size check for each new device.
1344 * Stacking drivers are expected to know what they are doing.
1345 */
1346 old_sector = -1;
1347 old_dev = 0;
1348 do {
1349 char b[BDEVNAME_SIZE];
1350
1351 q = bdev_get_queue(bio->bi_bdev);
1352 if (!q) {
1353 printk(KERN_ERR
1354 "generic_make_request: Trying to access "
1355 "nonexistent block-device %s (%Lu)\n",
1356 bdevname(bio->bi_bdev, b),
1357 (long long) bio->bi_sector);
1358 end_io:
1359 bio_endio(bio, err);
1360 break;
1361 }
1362
1363 if (unlikely(nr_sectors > q->max_hw_sectors)) {
1364 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
1365 bdevname(bio->bi_bdev, b),
1366 bio_sectors(bio),
1367 q->max_hw_sectors);
1368 goto end_io;
1369 }
1370
1371 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
1372 goto end_io;
1373
1374 if (should_fail_request(bio))
1375 goto end_io;
1376
1377 /*
1378 * If this device has partitions, remap block n
1379 * of partition p to block n+start(p) of the disk.
1380 */
1381 blk_partition_remap(bio);
1382
1383 if (old_sector != -1)
1384 blk_add_trace_remap(q, bio, old_dev, bio->bi_sector,
1385 old_sector);
1386
1387 blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
1388
1389 old_sector = bio->bi_sector;
1390 old_dev = bio->bi_bdev->bd_dev;
1391
1392 if (bio_check_eod(bio, nr_sectors))
1393 goto end_io;
1394 if (bio_empty_barrier(bio) && !q->prepare_flush_fn) {
1395 err = -EOPNOTSUPP;
1396 goto end_io;
1397 }
1398
1399 ret = q->make_request_fn(q, bio);
1400 } while (ret);
1401 }
1402
1403 /*
1404 * We only want one ->make_request_fn to be active at a time,
1405 * else stack usage with stacked devices could be a problem.
1406 * So use current->bio_{list,tail} to keep a list of requests
1407 * submited by a make_request_fn function.
1408 * current->bio_tail is also used as a flag to say if
1409 * generic_make_request is currently active in this task or not.
1410 * If it is NULL, then no make_request is active. If it is non-NULL,
1411 * then a make_request is active, and new requests should be added
1412 * at the tail
1413 */
1414 void generic_make_request(struct bio *bio)
1415 {
1416 if (current->bio_tail) {
1417 /* make_request is active */
1418 *(current->bio_tail) = bio;
1419 bio->bi_next = NULL;
1420 current->bio_tail = &bio->bi_next;
1421 return;
1422 }
1423 /* following loop may be a bit non-obvious, and so deserves some
1424 * explanation.
1425 * Before entering the loop, bio->bi_next is NULL (as all callers
1426 * ensure that) so we have a list with a single bio.
1427 * We pretend that we have just taken it off a longer list, so
1428 * we assign bio_list to the next (which is NULL) and bio_tail
1429 * to &bio_list, thus initialising the bio_list of new bios to be
1430 * added. __generic_make_request may indeed add some more bios
1431 * through a recursive call to generic_make_request. If it
1432 * did, we find a non-NULL value in bio_list and re-enter the loop
1433 * from the top. In this case we really did just take the bio
1434 * of the top of the list (no pretending) and so fixup bio_list and
1435 * bio_tail or bi_next, and call into __generic_make_request again.
1436 *
1437 * The loop was structured like this to make only one call to
1438 * __generic_make_request (which is important as it is large and
1439 * inlined) and to keep the structure simple.
1440 */
1441 BUG_ON(bio->bi_next);
1442 do {
1443 current->bio_list = bio->bi_next;
1444 if (bio->bi_next == NULL)
1445 current->bio_tail = &current->bio_list;
1446 else
1447 bio->bi_next = NULL;
1448 __generic_make_request(bio);
1449 bio = current->bio_list;
1450 } while (bio);
1451 current->bio_tail = NULL; /* deactivate */
1452 }
1453 EXPORT_SYMBOL(generic_make_request);
1454
1455 /**
1456 * submit_bio: submit a bio to the block device layer for I/O
1457 * @rw: whether to %READ or %WRITE, or maybe to %READA (read ahead)
1458 * @bio: The &struct bio which describes the I/O
1459 *
1460 * submit_bio() is very similar in purpose to generic_make_request(), and
1461 * uses that function to do most of the work. Both are fairly rough
1462 * interfaces, @bio must be presetup and ready for I/O.
1463 *
1464 */
1465 void submit_bio(int rw, struct bio *bio)
1466 {
1467 int count = bio_sectors(bio);
1468
1469 bio->bi_rw |= rw;
1470
1471 /*
1472 * If it's a regular read/write or a barrier with data attached,
1473 * go through the normal accounting stuff before submission.
1474 */
1475 if (!bio_empty_barrier(bio)) {
1476
1477 BIO_BUG_ON(!bio->bi_size);
1478 BIO_BUG_ON(!bio->bi_io_vec);
1479
1480 if (rw & WRITE) {
1481 count_vm_events(PGPGOUT, count);
1482 } else {
1483 task_io_account_read(bio->bi_size);
1484 count_vm_events(PGPGIN, count);
1485 }
1486
1487 if (unlikely(block_dump)) {
1488 char b[BDEVNAME_SIZE];
1489 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
1490 current->comm, task_pid_nr(current),
1491 (rw & WRITE) ? "WRITE" : "READ",
1492 (unsigned long long)bio->bi_sector,
1493 bdevname(bio->bi_bdev, b));
1494 }
1495 }
1496
1497 generic_make_request(bio);
1498 }
1499 EXPORT_SYMBOL(submit_bio);
1500
1501 /**
1502 * __end_that_request_first - end I/O on a request
1503 * @req: the request being processed
1504 * @error: 0 for success, < 0 for error
1505 * @nr_bytes: number of bytes to complete
1506 *
1507 * Description:
1508 * Ends I/O on a number of bytes attached to @req, and sets it up
1509 * for the next range of segments (if any) in the cluster.
1510 *
1511 * Return:
1512 * 0 - we are done with this request, call end_that_request_last()
1513 * 1 - still buffers pending for this request
1514 **/
1515 static int __end_that_request_first(struct request *req, int error,
1516 int nr_bytes)
1517 {
1518 int total_bytes, bio_nbytes, next_idx = 0;
1519 struct bio *bio;
1520
1521 blk_add_trace_rq(req->q, req, BLK_TA_COMPLETE);
1522
1523 /*
1524 * for a REQ_BLOCK_PC request, we want to carry any eventual
1525 * sense key with us all the way through
1526 */
1527 if (!blk_pc_request(req))
1528 req->errors = 0;
1529
1530 if (error && (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))) {
1531 printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n",
1532 req->rq_disk ? req->rq_disk->disk_name : "?",
1533 (unsigned long long)req->sector);
1534 }
1535
1536 if (blk_fs_request(req) && req->rq_disk) {
1537 const int rw = rq_data_dir(req);
1538
1539 all_stat_add(req->rq_disk, sectors[rw],
1540 nr_bytes >> 9, req->sector);
1541 }
1542
1543 total_bytes = bio_nbytes = 0;
1544 while ((bio = req->bio) != NULL) {
1545 int nbytes;
1546
1547 /*
1548 * For an empty barrier request, the low level driver must
1549 * store a potential error location in ->sector. We pass
1550 * that back up in ->bi_sector.
1551 */
1552 if (blk_empty_barrier(req))
1553 bio->bi_sector = req->sector;
1554
1555 if (nr_bytes >= bio->bi_size) {
1556 req->bio = bio->bi_next;
1557 nbytes = bio->bi_size;
1558 req_bio_endio(req, bio, nbytes, error);
1559 next_idx = 0;
1560 bio_nbytes = 0;
1561 } else {
1562 int idx = bio->bi_idx + next_idx;
1563
1564 if (unlikely(bio->bi_idx >= bio->bi_vcnt)) {
1565 blk_dump_rq_flags(req, "__end_that");
1566 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
1567 __FUNCTION__, bio->bi_idx,
1568 bio->bi_vcnt);
1569 break;
1570 }
1571
1572 nbytes = bio_iovec_idx(bio, idx)->bv_len;
1573 BIO_BUG_ON(nbytes > bio->bi_size);
1574
1575 /*
1576 * not a complete bvec done
1577 */
1578 if (unlikely(nbytes > nr_bytes)) {
1579 bio_nbytes += nr_bytes;
1580 total_bytes += nr_bytes;
1581 break;
1582 }
1583
1584 /*
1585 * advance to the next vector
1586 */
1587 next_idx++;
1588 bio_nbytes += nbytes;
1589 }
1590
1591 total_bytes += nbytes;
1592 nr_bytes -= nbytes;
1593
1594 bio = req->bio;
1595 if (bio) {
1596 /*
1597 * end more in this run, or just return 'not-done'
1598 */
1599 if (unlikely(nr_bytes <= 0))
1600 break;
1601 }
1602 }
1603
1604 /*
1605 * completely done
1606 */
1607 if (!req->bio)
1608 return 0;
1609
1610 /*
1611 * if the request wasn't completed, update state
1612 */
1613 if (bio_nbytes) {
1614 req_bio_endio(req, bio, bio_nbytes, error);
1615 bio->bi_idx += next_idx;
1616 bio_iovec(bio)->bv_offset += nr_bytes;
1617 bio_iovec(bio)->bv_len -= nr_bytes;
1618 }
1619
1620 blk_recalc_rq_sectors(req, total_bytes >> 9);
1621 blk_recalc_rq_segments(req);
1622 return 1;
1623 }
1624
1625 /*
1626 * splice the completion data to a local structure and hand off to
1627 * process_completion_queue() to complete the requests
1628 */
1629 static void blk_done_softirq(struct softirq_action *h)
1630 {
1631 struct list_head *cpu_list, local_list;
1632
1633 local_irq_disable();
1634 cpu_list = &__get_cpu_var(blk_cpu_done);
1635 list_replace_init(cpu_list, &local_list);
1636 local_irq_enable();
1637
1638 while (!list_empty(&local_list)) {
1639 struct request *rq;
1640
1641 rq = list_entry(local_list.next, struct request, donelist);
1642 list_del_init(&rq->donelist);
1643 rq->q->softirq_done_fn(rq);
1644 }
1645 }
1646
1647 static int __cpuinit blk_cpu_notify(struct notifier_block *self,
1648 unsigned long action, void *hcpu)
1649 {
1650 /*
1651 * If a CPU goes away, splice its entries to the current CPU
1652 * and trigger a run of the softirq
1653 */
1654 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
1655 int cpu = (unsigned long) hcpu;
1656
1657 local_irq_disable();
1658 list_splice_init(&per_cpu(blk_cpu_done, cpu),
1659 &__get_cpu_var(blk_cpu_done));
1660 raise_softirq_irqoff(BLOCK_SOFTIRQ);
1661 local_irq_enable();
1662 }
1663
1664 return NOTIFY_OK;
1665 }
1666
1667
1668 static struct notifier_block blk_cpu_notifier __cpuinitdata = {
1669 .notifier_call = blk_cpu_notify,
1670 };
1671
1672 /**
1673 * blk_complete_request - end I/O on a request
1674 * @req: the request being processed
1675 *
1676 * Description:
1677 * Ends all I/O on a request. It does not handle partial completions,
1678 * unless the driver actually implements this in its completion callback
1679 * through requeueing. The actual completion happens out-of-order,
1680 * through a softirq handler. The user must have registered a completion
1681 * callback through blk_queue_softirq_done().
1682 **/
1683
1684 void blk_complete_request(struct request *req)
1685 {
1686 struct list_head *cpu_list;
1687 unsigned long flags;
1688
1689 BUG_ON(!req->q->softirq_done_fn);
1690
1691 local_irq_save(flags);
1692
1693 cpu_list = &__get_cpu_var(blk_cpu_done);
1694 list_add_tail(&req->donelist, cpu_list);
1695 raise_softirq_irqoff(BLOCK_SOFTIRQ);
1696
1697 local_irq_restore(flags);
1698 }
1699 EXPORT_SYMBOL(blk_complete_request);
1700
1701 /*
1702 * queue lock must be held
1703 */
1704 static void end_that_request_last(struct request *req, int error)
1705 {
1706 struct gendisk *disk = req->rq_disk;
1707
1708 if (blk_rq_tagged(req))
1709 blk_queue_end_tag(req->q, req);
1710
1711 if (blk_queued_rq(req))
1712 blkdev_dequeue_request(req);
1713
1714 if (unlikely(laptop_mode) && blk_fs_request(req))
1715 laptop_io_completion();
1716
1717 /*
1718 * Account IO completion. bar_rq isn't accounted as a normal
1719 * IO on queueing nor completion. Accounting the containing
1720 * request is enough.
1721 */
1722 if (disk && blk_fs_request(req) && req != &req->q->bar_rq) {
1723 unsigned long duration = jiffies - req->start_time;
1724 const int rw = rq_data_dir(req);
1725 struct hd_struct *part = get_part(disk, req->sector);
1726
1727 __all_stat_inc(disk, ios[rw], req->sector);
1728 __all_stat_add(disk, ticks[rw], duration, req->sector);
1729 disk_round_stats(disk);
1730 disk->in_flight--;
1731 if (part) {
1732 part_round_stats(part);
1733 part->in_flight--;
1734 }
1735 }
1736
1737 if (req->end_io)
1738 req->end_io(req, error);
1739 else {
1740 if (blk_bidi_rq(req))
1741 __blk_put_request(req->next_rq->q, req->next_rq);
1742
1743 __blk_put_request(req->q, req);
1744 }
1745 }
1746
1747 static inline void __end_request(struct request *rq, int uptodate,
1748 unsigned int nr_bytes)
1749 {
1750 int error = 0;
1751
1752 if (uptodate <= 0)
1753 error = uptodate ? uptodate : -EIO;
1754
1755 __blk_end_request(rq, error, nr_bytes);
1756 }
1757
1758 /**
1759 * blk_rq_bytes - Returns bytes left to complete in the entire request
1760 * @rq: the request being processed
1761 **/
1762 unsigned int blk_rq_bytes(struct request *rq)
1763 {
1764 if (blk_fs_request(rq))
1765 return rq->hard_nr_sectors << 9;
1766
1767 return rq->data_len;
1768 }
1769 EXPORT_SYMBOL_GPL(blk_rq_bytes);
1770
1771 /**
1772 * blk_rq_cur_bytes - Returns bytes left to complete in the current segment
1773 * @rq: the request being processed
1774 **/
1775 unsigned int blk_rq_cur_bytes(struct request *rq)
1776 {
1777 if (blk_fs_request(rq))
1778 return rq->current_nr_sectors << 9;
1779
1780 if (rq->bio)
1781 return rq->bio->bi_size;
1782
1783 return rq->data_len;
1784 }
1785 EXPORT_SYMBOL_GPL(blk_rq_cur_bytes);
1786
1787 /**
1788 * end_queued_request - end all I/O on a queued request
1789 * @rq: the request being processed
1790 * @uptodate: error value or 0/1 uptodate flag
1791 *
1792 * Description:
1793 * Ends all I/O on a request, and removes it from the block layer queues.
1794 * Not suitable for normal IO completion, unless the driver still has
1795 * the request attached to the block layer.
1796 *
1797 **/
1798 void end_queued_request(struct request *rq, int uptodate)
1799 {
1800 __end_request(rq, uptodate, blk_rq_bytes(rq));
1801 }
1802 EXPORT_SYMBOL(end_queued_request);
1803
1804 /**
1805 * end_dequeued_request - end all I/O on a dequeued request
1806 * @rq: the request being processed
1807 * @uptodate: error value or 0/1 uptodate flag
1808 *
1809 * Description:
1810 * Ends all I/O on a request. The request must already have been
1811 * dequeued using blkdev_dequeue_request(), as is normally the case
1812 * for most drivers.
1813 *
1814 **/
1815 void end_dequeued_request(struct request *rq, int uptodate)
1816 {
1817 __end_request(rq, uptodate, blk_rq_bytes(rq));
1818 }
1819 EXPORT_SYMBOL(end_dequeued_request);
1820
1821
1822 /**
1823 * end_request - end I/O on the current segment of the request
1824 * @req: the request being processed
1825 * @uptodate: error value or 0/1 uptodate flag
1826 *
1827 * Description:
1828 * Ends I/O on the current segment of a request. If that is the only
1829 * remaining segment, the request is also completed and freed.
1830 *
1831 * This is a remnant of how older block drivers handled IO completions.
1832 * Modern drivers typically end IO on the full request in one go, unless
1833 * they have a residual value to account for. For that case this function
1834 * isn't really useful, unless the residual just happens to be the
1835 * full current segment. In other words, don't use this function in new
1836 * code. Either use end_request_completely(), or the
1837 * end_that_request_chunk() (along with end_that_request_last()) for
1838 * partial completions.
1839 *
1840 **/
1841 void end_request(struct request *req, int uptodate)
1842 {
1843 __end_request(req, uptodate, req->hard_cur_sectors << 9);
1844 }
1845 EXPORT_SYMBOL(end_request);
1846
1847 /**
1848 * blk_end_io - Generic end_io function to complete a request.
1849 * @rq: the request being processed
1850 * @error: 0 for success, < 0 for error
1851 * @nr_bytes: number of bytes to complete @rq
1852 * @bidi_bytes: number of bytes to complete @rq->next_rq
1853 * @drv_callback: function called between completion of bios in the request
1854 * and completion of the request.
1855 * If the callback returns non 0, this helper returns without
1856 * completion of the request.
1857 *
1858 * Description:
1859 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
1860 * If @rq has leftover, sets it up for the next range of segments.
1861 *
1862 * Return:
1863 * 0 - we are done with this request
1864 * 1 - this request is not freed yet, it still has pending buffers.
1865 **/
1866 static int blk_end_io(struct request *rq, int error, unsigned int nr_bytes,
1867 unsigned int bidi_bytes,
1868 int (drv_callback)(struct request *))
1869 {
1870 struct request_queue *q = rq->q;
1871 unsigned long flags = 0UL;
1872
1873 if (blk_fs_request(rq) || blk_pc_request(rq)) {
1874 if (__end_that_request_first(rq, error, nr_bytes))
1875 return 1;
1876
1877 /* Bidi request must be completed as a whole */
1878 if (blk_bidi_rq(rq) &&
1879 __end_that_request_first(rq->next_rq, error, bidi_bytes))
1880 return 1;
1881 }
1882
1883 /* Special feature for tricky drivers */
1884 if (drv_callback && drv_callback(rq))
1885 return 1;
1886
1887 add_disk_randomness(rq->rq_disk);
1888
1889 spin_lock_irqsave(q->queue_lock, flags);
1890 end_that_request_last(rq, error);
1891 spin_unlock_irqrestore(q->queue_lock, flags);
1892
1893 return 0;
1894 }
1895
1896 /**
1897 * blk_end_request - Helper function for drivers to complete the request.
1898 * @rq: the request being processed
1899 * @error: 0 for success, < 0 for error
1900 * @nr_bytes: number of bytes to complete
1901 *
1902 * Description:
1903 * Ends I/O on a number of bytes attached to @rq.
1904 * If @rq has leftover, sets it up for the next range of segments.
1905 *
1906 * Return:
1907 * 0 - we are done with this request
1908 * 1 - still buffers pending for this request
1909 **/
1910 int blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
1911 {
1912 return blk_end_io(rq, error, nr_bytes, 0, NULL);
1913 }
1914 EXPORT_SYMBOL_GPL(blk_end_request);
1915
1916 /**
1917 * __blk_end_request - Helper function for drivers to complete the request.
1918 * @rq: the request being processed
1919 * @error: 0 for success, < 0 for error
1920 * @nr_bytes: number of bytes to complete
1921 *
1922 * Description:
1923 * Must be called with queue lock held unlike blk_end_request().
1924 *
1925 * Return:
1926 * 0 - we are done with this request
1927 * 1 - still buffers pending for this request
1928 **/
1929 int __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
1930 {
1931 if (blk_fs_request(rq) || blk_pc_request(rq)) {
1932 if (__end_that_request_first(rq, error, nr_bytes))
1933 return 1;
1934 }
1935
1936 add_disk_randomness(rq->rq_disk);
1937
1938 end_that_request_last(rq, error);
1939
1940 return 0;
1941 }
1942 EXPORT_SYMBOL_GPL(__blk_end_request);
1943
1944 /**
1945 * blk_end_bidi_request - Helper function for drivers to complete bidi request.
1946 * @rq: the bidi request being processed
1947 * @error: 0 for success, < 0 for error
1948 * @nr_bytes: number of bytes to complete @rq
1949 * @bidi_bytes: number of bytes to complete @rq->next_rq
1950 *
1951 * Description:
1952 * Ends I/O on a number of bytes attached to @rq and @rq->next_rq.
1953 *
1954 * Return:
1955 * 0 - we are done with this request
1956 * 1 - still buffers pending for this request
1957 **/
1958 int blk_end_bidi_request(struct request *rq, int error, unsigned int nr_bytes,
1959 unsigned int bidi_bytes)
1960 {
1961 return blk_end_io(rq, error, nr_bytes, bidi_bytes, NULL);
1962 }
1963 EXPORT_SYMBOL_GPL(blk_end_bidi_request);
1964
1965 /**
1966 * blk_end_request_callback - Special helper function for tricky drivers
1967 * @rq: the request being processed
1968 * @error: 0 for success, < 0 for error
1969 * @nr_bytes: number of bytes to complete
1970 * @drv_callback: function called between completion of bios in the request
1971 * and completion of the request.
1972 * If the callback returns non 0, this helper returns without
1973 * completion of the request.
1974 *
1975 * Description:
1976 * Ends I/O on a number of bytes attached to @rq.
1977 * If @rq has leftover, sets it up for the next range of segments.
1978 *
1979 * This special helper function is used only for existing tricky drivers.
1980 * (e.g. cdrom_newpc_intr() of ide-cd)
1981 * This interface will be removed when such drivers are rewritten.
1982 * Don't use this interface in other places anymore.
1983 *
1984 * Return:
1985 * 0 - we are done with this request
1986 * 1 - this request is not freed yet.
1987 * this request still has pending buffers or
1988 * the driver doesn't want to finish this request yet.
1989 **/
1990 int blk_end_request_callback(struct request *rq, int error,
1991 unsigned int nr_bytes,
1992 int (drv_callback)(struct request *))
1993 {
1994 return blk_end_io(rq, error, nr_bytes, 0, drv_callback);
1995 }
1996 EXPORT_SYMBOL_GPL(blk_end_request_callback);
1997
1998 void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
1999 struct bio *bio)
2000 {
2001 /* first two bits are identical in rq->cmd_flags and bio->bi_rw */
2002 rq->cmd_flags |= (bio->bi_rw & 3);
2003
2004 rq->nr_phys_segments = bio_phys_segments(q, bio);
2005 rq->nr_hw_segments = bio_hw_segments(q, bio);
2006 rq->current_nr_sectors = bio_cur_sectors(bio);
2007 rq->hard_cur_sectors = rq->current_nr_sectors;
2008 rq->hard_nr_sectors = rq->nr_sectors = bio_sectors(bio);
2009 rq->buffer = bio_data(bio);
2010 rq->data_len = bio->bi_size;
2011
2012 rq->bio = rq->biotail = bio;
2013
2014 if (bio->bi_bdev)
2015 rq->rq_disk = bio->bi_bdev->bd_disk;
2016 }
2017
2018 int kblockd_schedule_work(struct work_struct *work)
2019 {
2020 return queue_work(kblockd_workqueue, work);
2021 }
2022 EXPORT_SYMBOL(kblockd_schedule_work);
2023
2024 void kblockd_flush_work(struct work_struct *work)
2025 {
2026 cancel_work_sync(work);
2027 }
2028 EXPORT_SYMBOL(kblockd_flush_work);
2029
2030 int __init blk_dev_init(void)
2031 {
2032 int i;
2033
2034 kblockd_workqueue = create_workqueue("kblockd");
2035 if (!kblockd_workqueue)
2036 panic("Failed to create kblockd\n");
2037
2038 request_cachep = kmem_cache_create("blkdev_requests",
2039 sizeof(struct request), 0, SLAB_PANIC, NULL);
2040
2041 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
2042 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
2043
2044 for_each_possible_cpu(i)
2045 INIT_LIST_HEAD(&per_cpu(blk_cpu_done, i));
2046
2047 open_softirq(BLOCK_SOFTIRQ, blk_done_softirq, NULL);
2048 register_hotcpu_notifier(&blk_cpu_notifier);
2049
2050 return 0;
2051 }
2052