1
2
3
4
5
6
7
8
9
10
11
12
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/fault-inject.h>
30
31#define CREATE_TRACE_POINTS
32#include <trace/events/block.h>
33
34#include "blk.h"
35
36EXPORT_TRACEPOINT_SYMBOL_GPL(block_remap);
37EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap);
38EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete);
39
40static int __make_request(struct request_queue *q, struct bio *bio);
41
42
43
44
45static struct kmem_cache *request_cachep;
46
47
48
49
50struct kmem_cache *blk_requestq_cachep;
51
52
53
54
55static struct workqueue_struct *kblockd_workqueue;
56
57static void drive_stat_acct(struct request *rq, int new_io)
58{
59 struct hd_struct *part;
60 int rw = rq_data_dir(rq);
61 int cpu;
62
63 if (!blk_do_io_stat(rq))
64 return;
65
66 cpu = part_stat_lock();
67 part = disk_map_sector_rcu(rq->rq_disk, blk_rq_pos(rq));
68
69 if (!new_io)
70 part_stat_inc(cpu, part, merges[rw]);
71 else {
72 part_round_stats(cpu, part);
73 part_inc_in_flight(part, rw);
74 }
75
76 part_stat_unlock();
77}
78
79void blk_queue_congestion_threshold(struct request_queue *q)
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
94
95
96
97
98
99
100
101
102
103struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
104{
105 struct backing_dev_info *ret = NULL;
106 struct request_queue *q = bdev_get_queue(bdev);
107
108 if (q)
109 ret = &q->backing_dev_info;
110 return ret;
111}
112EXPORT_SYMBOL(blk_get_backing_dev_info);
113
114void blk_rq_init(struct request_queue *q, struct request *rq)
115{
116 memset(rq, 0, sizeof(*rq));
117
118 INIT_LIST_HEAD(&rq->queuelist);
119 INIT_LIST_HEAD(&rq->timeout_list);
120 rq->cpu = -1;
121 rq->q = q;
122 rq->__sector = (sector_t) -1;
123 INIT_HLIST_NODE(&rq->hash);
124 RB_CLEAR_NODE(&rq->rb_node);
125 rq->cmd = rq->__cmd;
126 rq->cmd_len = BLK_MAX_CDB;
127 rq->tag = -1;
128 rq->ref_count = 1;
129 rq->start_time = jiffies;
130}
131EXPORT_SYMBOL(blk_rq_init);
132
133static void req_bio_endio(struct request *rq, struct bio *bio,
134 unsigned int nbytes, int error)
135{
136 struct request_queue *q = rq->q;
137
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;
143
144 if (unlikely(nbytes > bio->bi_size)) {
145 printk(KERN_ERR "%s: want %u bytes done, %u left\n",
146 __func__, nbytes, bio->bi_size);
147 nbytes = bio->bi_size;
148 }
149
150 if (unlikely(rq->cmd_flags & REQ_QUIET))
151 set_bit(BIO_QUIET, &bio->bi_flags);
152
153 bio->bi_size -= nbytes;
154 bio->bi_sector += (nbytes >> 9);
155
156 if (bio_integrity(bio))
157 bio_integrity_advance(bio, nbytes);
158
159 if (bio->bi_size == 0)
160 bio_endio(bio, error);
161 } else {
162
163
164
165
166
167 if (error && !q->orderr)
168 q->orderr = error;
169 }
170}
171
172void blk_dump_rq_flags(struct request *rq, char *msg)
173{
174 int bit;
175
176 printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg,
177 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
178 rq->cmd_flags);
179
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));
183 printk(KERN_INFO " bio %p, biotail %p, buffer %p, len %u\n",
184 rq->bio, rq->biotail, rq->buffer, blk_rq_bytes(rq));
185
186 if (blk_pc_request(rq)) {
187 printk(KERN_INFO " cdb: ");
188 for (bit = 0; bit < BLK_MAX_CDB; bit++)
189 printk("%02x ", rq->cmd[bit]);
190 printk("\n");
191 }
192}
193EXPORT_SYMBOL(blk_dump_rq_flags);
194
195
196
197
198
199
200
201
202
203void blk_plug_device(struct request_queue *q)
204{
205 WARN_ON(!irqs_disabled());
206
207
208
209
210
211 if (blk_queue_stopped(q))
212 return;
213
214 if (!queue_flag_test_and_set(QUEUE_FLAG_PLUGGED, q)) {
215 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
216 trace_block_plug(q);
217 }
218}
219EXPORT_SYMBOL(blk_plug_device);
220
221
222
223
224
225
226
227
228
229void 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}
237EXPORT_SYMBOL(blk_plug_device_unlocked);
238
239
240
241
242
243int blk_remove_plug(struct request_queue *q)
244{
245 WARN_ON(!irqs_disabled());
246
247 if (!queue_flag_test_and_clear(QUEUE_FLAG_PLUGGED, q))
248 return 0;
249
250 del_timer(&q->unplug_timer);
251 return 1;
252}
253EXPORT_SYMBOL(blk_remove_plug);
254
255
256
257
258void __generic_unplug_device(struct request_queue *q)
259{
260 if (unlikely(blk_queue_stopped(q)))
261 return;
262 if (!blk_remove_plug(q) && !blk_queue_nonrot(q))
263 return;
264
265 q->request_fn(q);
266}
267
268
269
270
271
272
273
274
275
276
277
278
279void generic_unplug_device(struct request_queue *q)
280{
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 }
286}
287EXPORT_SYMBOL(generic_unplug_device);
288
289static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
290 struct page *page)
291{
292 struct request_queue *q = bdi->unplug_io_data;
293
294 blk_unplug(q);
295}
296
297void blk_unplug_work(struct work_struct *work)
298{
299 struct request_queue *q =
300 container_of(work, struct request_queue, unplug_work);
301
302 trace_block_unplug_io(q);
303 q->unplug_fn(q);
304}
305
306void blk_unplug_timeout(unsigned long data)
307{
308 struct request_queue *q = (struct request_queue *)data;
309
310 trace_block_unplug_timer(q);
311 kblockd_schedule_work(q, &q->unplug_work);
312}
313
314void blk_unplug(struct request_queue *q)
315{
316
317
318
319 if (q->unplug_fn) {
320 trace_block_unplug_io(q);
321 q->unplug_fn(q);
322 }
323}
324EXPORT_SYMBOL(blk_unplug);
325
326
327
328
329
330
331
332
333
334
335void blk_start_queue(struct request_queue *q)
336{
337 WARN_ON(!irqs_disabled());
338
339 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
340 __blk_run_queue(q);
341}
342EXPORT_SYMBOL(blk_start_queue);
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358void blk_stop_queue(struct request_queue *q)
359{
360 blk_remove_plug(q);
361 queue_flag_set(QUEUE_FLAG_STOPPED, q);
362}
363EXPORT_SYMBOL(blk_stop_queue);
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379void blk_sync_queue(struct request_queue *q)
380{
381 del_timer_sync(&q->unplug_timer);
382 del_timer_sync(&q->timeout);
383 cancel_work_sync(&q->unplug_work);
384}
385EXPORT_SYMBOL(blk_sync_queue);
386
387
388
389
390
391
392
393
394
395
396void __blk_run_queue(struct request_queue *q)
397{
398 blk_remove_plug(q);
399
400 if (unlikely(blk_queue_stopped(q)))
401 return;
402
403 if (elv_queue_empty(q))
404 return;
405
406
407
408
409
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 }
417}
418EXPORT_SYMBOL(__blk_run_queue);
419
420
421
422
423
424
425
426
427
428void 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);
434 spin_unlock_irqrestore(q->queue_lock, flags);
435}
436EXPORT_SYMBOL(blk_run_queue);
437
438void blk_put_queue(struct request_queue *q)
439{
440 kobject_put(&q->kobj);
441}
442
443void blk_cleanup_queue(struct request_queue *q)
444{
445
446
447
448
449
450
451 blk_sync_queue(q);
452
453 mutex_lock(&q->sysfs_lock);
454 queue_flag_set_unlocked(QUEUE_FLAG_DEAD, q);
455 mutex_unlock(&q->sysfs_lock);
456
457 if (q->elevator)
458 elevator_exit(q->elevator);
459
460 blk_put_queue(q);
461}
462EXPORT_SYMBOL(blk_cleanup_queue);
463
464static int blk_init_free_list(struct request_queue *q)
465{
466 struct request_list *rl = &q->rq;
467
468 rl->count[BLK_RW_SYNC] = rl->count[BLK_RW_ASYNC] = 0;
469 rl->starved[BLK_RW_SYNC] = rl->starved[BLK_RW_ASYNC] = 0;
470 rl->elvpriv = 0;
471 init_waitqueue_head(&rl->wait[BLK_RW_SYNC]);
472 init_waitqueue_head(&rl->wait[BLK_RW_ASYNC]);
473
474 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
475 mempool_free_slab, request_cachep, q->node);
476
477 if (!rl->rq_pool)
478 return -ENOMEM;
479
480 return 0;
481}
482
483struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
484{
485 return blk_alloc_queue_node(gfp_mask, -1);
486}
487EXPORT_SYMBOL(blk_alloc_queue);
488
489struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
490{
491 struct request_queue *q;
492 int err;
493
494 q = kmem_cache_alloc_node(blk_requestq_cachep,
495 gfp_mask | __GFP_ZERO, node_id);
496 if (!q)
497 return NULL;
498
499 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
500 q->backing_dev_info.unplug_io_data = q;
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;
505 q->backing_dev_info.name = "block";
506
507 err = bdi_init(&q->backing_dev_info);
508 if (err) {
509 kmem_cache_free(blk_requestq_cachep, q);
510 return NULL;
511 }
512
513 init_timer(&q->unplug_timer);
514 setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
515 INIT_LIST_HEAD(&q->timeout_list);
516 INIT_WORK(&q->unplug_work, blk_unplug_work);
517
518 kobject_init(&q->kobj, &blk_queue_ktype);
519
520 mutex_init(&q->sysfs_lock);
521 spin_lock_init(&q->__queue_lock);
522
523 return q;
524}
525EXPORT_SYMBOL(blk_alloc_queue_node);
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
561{
562 return blk_init_queue_node(rfn, lock, -1);
563}
564EXPORT_SYMBOL(blk_init_queue);
565
566struct request_queue *
567blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
568{
569 struct request_queue *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
570
571 if (!q)
572 return NULL;
573
574 q->node = node_id;
575 if (blk_init_free_list(q)) {
576 kmem_cache_free(blk_requestq_cachep, q);
577 return NULL;
578 }
579
580 q->request_fn = rfn;
581 q->prep_rq_fn = NULL;
582 q->unplug_fn = generic_unplug_device;
583 q->queue_flags = QUEUE_FLAG_DEFAULT;
584 q->queue_lock = lock;
585
586
587
588
589 blk_queue_make_request(q, __make_request);
590
591 q->sg_reserved_size = INT_MAX;
592
593
594
595
596 if (!elevator_init(q, NULL)) {
597 blk_queue_congestion_threshold(q);
598 return q;
599 }
600
601 blk_put_queue(q);
602 return NULL;
603}
604EXPORT_SYMBOL(blk_init_queue_node);
605
606int blk_get_queue(struct request_queue *q)
607{
608 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
609 kobject_get(&q->kobj);
610 return 0;
611 }
612
613 return 1;
614}
615
616static inline void blk_free_request(struct request_queue *q, struct request *rq)
617{
618 if (rq->cmd_flags & REQ_ELVPRIV)
619 elv_put_request(q, rq);
620 mempool_free(rq, q->rq.rq_pool);
621}
622
623static struct request *
624blk_alloc_request(struct request_queue *q, int flags, int priv, gfp_t gfp_mask)
625{
626 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
627
628 if (!rq)
629 return NULL;
630
631 blk_rq_init(q, rq);
632
633 rq->cmd_flags = flags | REQ_ALLOCED;
634
635 if (priv) {
636 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
637 mempool_free(rq, q->rq.rq_pool);
638 return NULL;
639 }
640 rq->cmd_flags |= REQ_ELVPRIV;
641 }
642
643 return rq;
644}
645
646
647
648
649
650static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
651{
652 if (!ioc)
653 return 0;
654
655
656
657
658
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
667
668
669
670
671static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
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
680static void __freed_request(struct request_queue *q, int sync)
681{
682 struct request_list *rl = &q->rq;
683
684 if (rl->count[sync] < queue_congestion_off_threshold(q))
685 blk_clear_queue_congested(q, sync);
686
687 if (rl->count[sync] + 1 <= q->nr_requests) {
688 if (waitqueue_active(&rl->wait[sync]))
689 wake_up(&rl->wait[sync]);
690
691 blk_clear_queue_full(q, sync);
692 }
693}
694
695
696
697
698
699static void freed_request(struct request_queue *q, int sync, int priv)
700{
701 struct request_list *rl = &q->rq;
702
703 rl->count[sync]--;
704 if (priv)
705 rl->elvpriv--;
706
707 __freed_request(q, sync);
708
709 if (unlikely(rl->starved[sync ^ 1]))
710 __freed_request(q, sync ^ 1);
711}
712
713
714
715
716
717
718static struct request *get_request(struct request_queue *q, int rw_flags,
719 struct bio *bio, gfp_t gfp_mask)
720{
721 struct request *rq = NULL;
722 struct request_list *rl = &q->rq;
723 struct io_context *ioc = NULL;
724 const bool is_sync = rw_is_sync(rw_flags) != 0;
725 int may_queue, priv;
726
727 may_queue = elv_may_queue(q, rw_flags);
728 if (may_queue == ELV_MQUEUE_NO)
729 goto rq_starved;
730
731 if (rl->count[is_sync]+1 >= queue_congestion_on_threshold(q)) {
732 if (rl->count[is_sync]+1 >= q->nr_requests) {
733 ioc = current_io_context(GFP_ATOMIC, q->node);
734
735
736
737
738
739
740 if (!blk_queue_full(q, is_sync)) {
741 ioc_set_batching(q, ioc);
742 blk_set_queue_full(q, is_sync);
743 } else {
744 if (may_queue != ELV_MQUEUE_MUST
745 && !ioc_batching(q, ioc)) {
746
747
748
749
750
751 goto out;
752 }
753 }
754 }
755 blk_set_queue_congested(q, is_sync);
756 }
757
758
759
760
761
762
763 if (rl->count[is_sync] >= (3 * q->nr_requests / 2))
764 goto out;
765
766 rl->count[is_sync]++;
767 rl->starved[is_sync] = 0;
768
769 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
770 if (priv)
771 rl->elvpriv++;
772
773 if (blk_queue_io_stat(q))
774 rw_flags |= REQ_IO_STAT;
775 spin_unlock_irq(q->queue_lock);
776
777 rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
778 if (unlikely(!rq)) {
779
780
781
782
783
784
785
786 spin_lock_irq(q->queue_lock);
787 freed_request(q, is_sync, priv);
788
789
790
791
792
793
794
795
796rq_starved:
797 if (unlikely(rl->count[is_sync] == 0))
798 rl->starved[is_sync] = 1;
799
800 goto out;
801 }
802
803
804
805
806
807
808
809 if (ioc_batching(q, ioc))
810 ioc->nr_batch_requests--;
811
812 trace_block_getrq(q, bio, rw_flags & 1);
813out:
814 return rq;
815}
816
817
818
819
820
821
822
823static struct request *get_request_wait(struct request_queue *q, int rw_flags,
824 struct bio *bio)
825{
826 const bool is_sync = rw_is_sync(rw_flags) != 0;
827 struct request *rq;
828
829 rq = get_request(q, rw_flags, bio, GFP_NOIO);
830 while (!rq) {
831 DEFINE_WAIT(wait);
832 struct io_context *ioc;
833 struct request_list *rl = &q->rq;
834
835 prepare_to_wait_exclusive(&rl->wait[is_sync], &wait,
836 TASK_UNINTERRUPTIBLE);
837
838 trace_block_sleeprq(q, bio, rw_flags & 1);
839
840 __generic_unplug_device(q);
841 spin_unlock_irq(q->queue_lock);
842 io_schedule();
843
844
845
846
847
848
849
850 ioc = current_io_context(GFP_NOIO, q->node);
851 ioc_set_batching(q, ioc);
852
853 spin_lock_irq(q->queue_lock);
854 finish_wait(&rl->wait[is_sync], &wait);
855
856 rq = get_request(q, rw_flags, bio, GFP_NOIO);
857 };
858
859 return rq;
860}
861
862struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
863{
864 struct request *rq;
865
866 BUG_ON(rw != READ && rw != WRITE);
867
868 spin_lock_irq(q->queue_lock);
869 if (gfp_mask & __GFP_WAIT) {
870 rq = get_request_wait(q, rw, NULL);
871 } else {
872 rq = get_request(q, rw, NULL, gfp_mask);
873 if (!rq)
874 spin_unlock_irq(q->queue_lock);
875 }
876
877
878 return rq;
879}
880EXPORT_SYMBOL(blk_get_request);
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913struct request *blk_make_request(struct request_queue *q, struct bio *bio,
914 gfp_t gfp_mask)
915{
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;
934}
935EXPORT_SYMBOL(blk_make_request);
936
937
938
939
940
941
942
943
944
945
946
947void blk_requeue_request(struct request_queue *q, struct request *rq)
948{
949 blk_delete_timer(rq);
950 blk_clear_rq_complete(rq);
951 trace_block_rq_requeue(q, rq);
952
953 if (blk_rq_tagged(rq))
954 blk_queue_end_tag(q, rq);
955
956 BUG_ON(blk_queued_rq(rq));
957
958 elv_requeue_request(q, rq);
959}
960EXPORT_SYMBOL(blk_requeue_request);
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981void blk_insert_request(struct request_queue *q, struct request *rq,
982 int at_head, void *data)
983{
984 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
985 unsigned long flags;
986
987
988
989
990
991
992 rq->cmd_type = REQ_TYPE_SPECIAL;
993
994 rq->special = data;
995
996 spin_lock_irqsave(q->queue_lock, flags);
997
998
999
1000
1001 if (blk_rq_tagged(rq))
1002 blk_queue_end_tag(q, rq);
1003
1004 drive_stat_acct(rq, 1);
1005 __elv_add_request(q, rq, where, 0);
1006 __blk_run_queue(q);
1007 spin_unlock_irqrestore(q->queue_lock, flags);
1008}
1009EXPORT_SYMBOL(blk_insert_request);
1010
1011
1012
1013
1014
1015
1016static inline void add_request(struct request_queue *q, struct request *req)
1017{
1018 drive_stat_acct(req, 1);
1019
1020
1021
1022
1023
1024 __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
1025}
1026
1027static void part_round_stats_single(int cpu, struct hd_struct *part,
1028 unsigned long now)
1029{
1030 if (now == part->stamp)
1031 return;
1032
1033 if (part_in_flight(part)) {
1034 __part_stat_add(cpu, part, time_in_queue,
1035 part_in_flight(part) * (now - part->stamp));
1036 __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
1037 }
1038 part->stamp = now;
1039}
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057void part_round_stats(int cpu, struct hd_struct *part)
1058{
1059 unsigned long now = jiffies;
1060
1061 if (part->partno)
1062 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
1063 part_round_stats_single(cpu, part, now);
1064}
1065EXPORT_SYMBOL_GPL(part_round_stats);
1066
1067
1068
1069
1070void __blk_put_request(struct request_queue *q, struct request *req)
1071{
1072 if (unlikely(!q))
1073 return;
1074 if (unlikely(--req->ref_count))
1075 return;
1076
1077 elv_completed_request(q, req);
1078
1079
1080 WARN_ON(req->bio != NULL);
1081
1082
1083
1084
1085
1086 if (req->cmd_flags & REQ_ALLOCED) {
1087 int is_sync = rq_is_sync(req) != 0;
1088 int priv = req->cmd_flags & REQ_ELVPRIV;
1089
1090 BUG_ON(!list_empty(&req->queuelist));
1091 BUG_ON(!hlist_unhashed(&req->hash));
1092
1093 blk_free_request(q, req);
1094 freed_request(q, is_sync, priv);
1095 }
1096}
1097EXPORT_SYMBOL_GPL(__blk_put_request);
1098
1099void blk_put_request(struct request *req)
1100{
1101 unsigned long flags;
1102 struct request_queue *q = req->q;
1103
1104 spin_lock_irqsave(q->queue_lock, flags);
1105 __blk_put_request(q, req);
1106 spin_unlock_irqrestore(q->queue_lock, flags);
1107}
1108EXPORT_SYMBOL(blk_put_request);
1109
1110void init_request_from_bio(struct request *req, struct bio *bio)
1111{
1112 req->cpu = bio->bi_comp_cpu;
1113 req->cmd_type = REQ_TYPE_FS;
1114
1115
1116
1117
1118
1119 if (bio_rw_flagged(bio, BIO_RW_AHEAD))
1120 req->cmd_flags |= REQ_FAILFAST_MASK;
1121 else
1122 req->cmd_flags |= bio->bi_rw & REQ_FAILFAST_MASK;
1123
1124 if (unlikely(bio_rw_flagged(bio, BIO_RW_DISCARD))) {
1125 req->cmd_flags |= REQ_DISCARD;
1126 if (bio_rw_flagged(bio, BIO_RW_BARRIER))
1127 req->cmd_flags |= REQ_SOFTBARRIER;
1128 } else if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER)))
1129 req->cmd_flags |= REQ_HARDBARRIER;
1130
1131 if (bio_rw_flagged(bio, BIO_RW_SYNCIO))
1132 req->cmd_flags |= REQ_RW_SYNC;
1133 if (bio_rw_flagged(bio, BIO_RW_META))
1134 req->cmd_flags |= REQ_RW_META;
1135 if (bio_rw_flagged(bio, BIO_RW_NOIDLE))
1136 req->cmd_flags |= REQ_NOIDLE;
1137
1138 req->errors = 0;
1139 req->__sector = bio->bi_sector;
1140 req->ioprio = bio_prio(bio);
1141 blk_rq_bio_prep(req->q, req, bio);
1142}
1143
1144
1145
1146
1147
1148static inline bool queue_should_plug(struct request_queue *q)
1149{
1150 return !(blk_queue_nonrot(q) && blk_queue_tagged(q));
1151}
1152
1153static int __make_request(struct request_queue *q, struct bio *bio)
1154{
1155 struct request *req;
1156 int el_ret;
1157 unsigned int bytes = bio->bi_size;
1158 const unsigned short prio = bio_prio(bio);
1159 const bool sync = bio_rw_flagged(bio, BIO_RW_SYNCIO);
1160 const bool unplug = bio_rw_flagged(bio, BIO_RW_UNPLUG);
1161 const unsigned int ff = bio->bi_rw & REQ_FAILFAST_MASK;
1162 int rw_flags;
1163
1164 if (bio_rw_flagged(bio, BIO_RW_BARRIER) &&
1165 (q->next_ordered == QUEUE_ORDERED_NONE)) {
1166 bio_endio(bio, -EOPNOTSUPP);
1167 return 0;
1168 }
1169
1170
1171
1172
1173
1174 blk_queue_bounce(q, &bio);
1175
1176 spin_lock_irq(q->queue_lock);
1177
1178 if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER)) || elv_queue_empty(q))
1179 goto get_rq;
1180
1181 el_ret = elv_merge(q, &req, bio);
1182 switch (el_ret) {
1183 case ELEVATOR_BACK_MERGE:
1184 BUG_ON(!rq_mergeable(req));
1185
1186 if (!ll_back_merge_fn(q, req, bio))
1187 break;
1188
1189 trace_block_bio_backmerge(q, bio);
1190
1191 if ((req->cmd_flags & REQ_FAILFAST_MASK) != ff)
1192 blk_rq_set_mixed_merge(req);
1193
1194 req->biotail->bi_next = bio;
1195 req->biotail = bio;
1196 req->__data_len += bytes;
1197 req->ioprio = ioprio_best(req->ioprio, prio);
1198 if (!blk_rq_cpu_valid(req))
1199 req->cpu = bio->bi_comp_cpu;
1200 drive_stat_acct(req, 0);
1201 if (!attempt_back_merge(q, req))
1202 elv_merged_request(q, req, el_ret);
1203 goto out;
1204
1205 case ELEVATOR_FRONT_MERGE:
1206 BUG_ON(!rq_mergeable(req));
1207
1208 if (!ll_front_merge_fn(q, req, bio))
1209 break;
1210
1211 trace_block_bio_frontmerge(q, bio);
1212
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
1219 bio->bi_next = req->bio;
1220 req->bio = bio;
1221
1222
1223
1224
1225
1226
1227 req->buffer = bio_data(bio);
1228 req->__sector = bio->bi_sector;
1229 req->__data_len += bytes;
1230 req->ioprio = ioprio_best(req->ioprio, prio);
1231 if (!blk_rq_cpu_valid(req))
1232 req->cpu = bio->bi_comp_cpu;
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
1239 default:
1240 ;
1241 }
1242
1243get_rq:
1244
1245
1246
1247
1248
1249 rw_flags = bio_data_dir(bio);
1250 if (sync)
1251 rw_flags |= REQ_RW_SYNC;
1252
1253
1254
1255
1256
1257 req = get_request_wait(q, rw_flags, bio);
1258
1259
1260
1261
1262
1263
1264
1265 init_request_from_bio(req, bio);
1266
1267 spin_lock_irq(q->queue_lock);
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());
1271 if (queue_should_plug(q) && elv_queue_empty(q))
1272 blk_plug_device(q);
1273 add_request(q, req);
1274out:
1275 if (unplug || !queue_should_plug(q))
1276 __generic_unplug_device(q);
1277 spin_unlock_irq(q->queue_lock);
1278 return 0;
1279}
1280
1281
1282
1283
1284static inline void blk_partition_remap(struct bio *bio)
1285{
1286 struct block_device *bdev = bio->bi_bdev;
1287
1288 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
1289 struct hd_struct *p = bdev->bd_part;
1290
1291 bio->bi_sector += p->start_sect;
1292 bio->bi_bdev = bdev->bd_contains;
1293
1294 trace_block_remap(bdev_get_queue(bio->bi_bdev), bio,
1295 bdev->bd_dev,
1296 bio->bi_sector - p->start_sect);
1297 }
1298}
1299
1300static 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
1314#ifdef CONFIG_FAIL_MAKE_REQUEST
1315
1316static DECLARE_FAULT_ATTR(fail_make_request);
1317
1318static 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
1324static int should_fail_request(struct bio *bio)
1325{
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)
1329 return should_fail(&fail_make_request, bio->bi_size);
1330
1331 return 0;
1332}
1333
1334static int __init fail_make_request_debugfs(void)
1335{
1336 return init_fault_attr_dentries(&fail_make_request,
1337 "fail_make_request");
1338}
1339
1340late_initcall(fail_make_request_debugfs);
1341
1342#else
1343
1344static inline int should_fail_request(struct bio *bio)
1345{
1346 return 0;
1347}
1348
1349#endif
1350
1351
1352
1353
1354static 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
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
1369
1370
1371
1372 handle_bad_sector(bio);
1373 return 1;
1374 }
1375 }
1376
1377 return 0;
1378}
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404static inline void __generic_make_request(struct bio *bio)
1405{
1406 struct request_queue *q;
1407 sector_t old_sector;
1408 int ret, nr_sectors = bio_sectors(bio);
1409 dev_t old_dev;
1410 int err = -EIO;
1411
1412 might_sleep();
1413
1414 if (bio_check_eod(bio, nr_sectors))
1415 goto end_io;
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425 old_sector = -1;
1426 old_dev = 0;
1427 do {
1428 char b[BDEVNAME_SIZE];
1429
1430 q = bdev_get_queue(bio->bi_bdev);
1431 if (unlikely(!q)) {
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);
1437 goto end_io;
1438 }
1439
1440 if (unlikely(!bio_rw_flagged(bio, BIO_RW_DISCARD) &&
1441 nr_sectors > queue_max_hw_sectors(q))) {
1442 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
1443 bdevname(bio->bi_bdev, b),
1444 bio_sectors(bio),
1445 queue_max_hw_sectors(q));
1446 goto end_io;
1447 }
1448
1449 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
1450 goto end_io;
1451
1452 if (should_fail_request(bio))
1453 goto end_io;
1454
1455
1456
1457
1458
1459 blk_partition_remap(bio);
1460
1461 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio))
1462 goto end_io;
1463
1464 if (old_sector != -1)
1465 trace_block_remap(q, bio, old_dev, old_sector);
1466
1467 old_sector = bio->bi_sector;
1468 old_dev = bio->bi_bdev->bd_dev;
1469
1470 if (bio_check_eod(bio, nr_sectors))
1471 goto end_io;
1472
1473 if (bio_rw_flagged(bio, BIO_RW_DISCARD) &&
1474 !blk_queue_discard(q)) {
1475 err = -EOPNOTSUPP;
1476 goto end_io;
1477 }
1478
1479 trace_block_bio_queue(q, bio);
1480
1481 ret = q->make_request_fn(q, bio);
1482 } while (ret);
1483
1484 return;
1485
1486end_io:
1487 bio_endio(bio, err);
1488}
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501void generic_make_request(struct bio *bio)
1502{
1503 if (current->bio_tail) {
1504
1505 *(current->bio_tail) = bio;
1506 bio->bi_next = NULL;
1507 current->bio_tail = &bio->bi_next;
1508 return;
1509 }
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
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;
1539}
1540EXPORT_SYMBOL(generic_make_request);
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552void submit_bio(int rw, struct bio *bio)
1553{
1554 int count = bio_sectors(bio);
1555
1556 bio->bi_rw |= rw;
1557
1558
1559
1560
1561
1562 if (bio_has_data(bio)) {
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",
1573 current->comm, task_pid_nr(current),
1574 (rw & WRITE) ? "WRITE" : "READ",
1575 (unsigned long long)bio->bi_sector,
1576 bdevname(bio->bi_bdev, b));
1577 }
1578 }
1579
1580 generic_make_request(bio);
1581}
1582EXPORT_SYMBOL(submit_bio);
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605int blk_rq_check_limits(struct request_queue *q, struct request *rq)
1606{
1607 if (blk_rq_sectors(rq) > queue_max_sectors(q) ||
1608 blk_rq_bytes(rq) > queue_max_hw_sectors(q) << 9) {
1609 printk(KERN_ERR "%s: over max size limit.\n", __func__);
1610 return -EIO;
1611 }
1612
1613
1614
1615
1616
1617
1618
1619 blk_recalc_rq_segments(rq);
1620 if (rq->nr_phys_segments > queue_max_phys_segments(q) ||
1621 rq->nr_phys_segments > queue_max_hw_segments(q)) {
1622 printk(KERN_ERR "%s: over max segments limit.\n", __func__);
1623 return -EIO;
1624 }
1625
1626 return 0;
1627}
1628EXPORT_SYMBOL_GPL(blk_rq_check_limits);
1629
1630
1631
1632
1633
1634
1635int 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
1652
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}
1663EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681unsigned 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
1692
1693
1694
1695
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
1704 BUG_ON(blk_rq_bytes(rq) && !bytes);
1705 return bytes;
1706}
1707EXPORT_SYMBOL_GPL(blk_rq_err_bytes);
1708
1709static void blk_account_io_completion(struct request *req, unsigned int bytes)
1710{
1711 if (blk_do_io_stat(req)) {
1712 const int rw = rq_data_dir(req);
1713 struct hd_struct *part;
1714 int cpu;
1715
1716 cpu = part_stat_lock();
1717 part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
1718 part_stat_add(cpu, part, sectors[rw], bytes >> 9);
1719 part_stat_unlock();
1720 }
1721}
1722
1723static void blk_account_io_done(struct request *req)
1724{
1725
1726
1727
1728
1729
1730 if (blk_do_io_stat(req) && req != &req->q->bar_rq) {
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();
1737 part = disk_map_sector_rcu(req->rq_disk, blk_rq_pos(req));
1738
1739 part_stat_inc(cpu, part, ios[rw]);
1740 part_stat_add(cpu, part, ticks[rw], duration);
1741 part_round_stats(cpu, part);
1742 part_dec_in_flight(part, rw);
1743
1744 part_stat_unlock();
1745 }
1746}
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764struct request *blk_peek_request(struct request_queue *q)
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
1773
1774
1775
1776 if (blk_sorted_rq(rq))
1777 elv_activate_rq(q, rq);
1778
1779
1780
1781
1782
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
1796 if (q->dma_drain_size && blk_rq_bytes(rq)) {
1797
1798
1799
1800
1801
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
1815
1816
1817
1818
1819 if (q->dma_drain_size && blk_rq_bytes(rq) &&
1820 !(rq->cmd_flags & REQ_DONTPREP)) {
1821
1822
1823
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;
1832
1833
1834
1835
1836 blk_start_request(rq);
1837 __blk_end_request_all(rq, -EIO);
1838 } else {
1839 printk(KERN_ERR "%s: bad return=%d\n", __func__, ret);
1840 break;
1841 }
1842 }
1843
1844 return rq;
1845}
1846EXPORT_SYMBOL(blk_peek_request);
1847
1848void blk_dequeue_request(struct request *rq)
1849{
1850 struct request_queue *q = rq->q;
1851
1852 BUG_ON(list_empty(&rq->queuelist));
1853 BUG_ON(ELV_ON_HASH(rq));
1854
1855 list_del_init(&rq->queuelist);
1856
1857
1858
1859
1860
1861
1862 if (blk_account_rq(rq))
1863 q->in_flight[rq_is_sync(rq)]++;
1864}
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880void blk_start_request(struct request *req)
1881{
1882 blk_dequeue_request(req);
1883
1884
1885
1886
1887
1888 req->resid_len = blk_rq_bytes(req);
1889 if (unlikely(blk_bidi_rq(req)))
1890 req->next_rq->resid_len = blk_rq_bytes(req->next_rq);
1891
1892 blk_add_timer(req);
1893}
1894EXPORT_SYMBOL(blk_start_request);
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911struct 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}
1920EXPORT_SYMBOL(blk_fetch_request);
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944bool blk_update_request(struct request *req, int error, unsigned int nr_bytes)
1945{
1946 int total_bytes, bio_nbytes, next_idx = 0;
1947 struct bio *bio;
1948
1949 if (!req->bio)
1950 return false;
1951
1952 trace_block_rq_complete(req->q, req);
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962 if (blk_fs_request(req))
1963 req->errors = 0;
1964
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",
1967 req->rq_disk ? req->rq_disk->disk_name : "?",
1968 (unsigned long long)blk_rq_pos(req));
1969 }
1970
1971 blk_account_io_completion(req, nr_bytes);
1972
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;
1980 req_bio_endio(req, bio, nbytes, error);
1981 next_idx = 0;
1982 bio_nbytes = 0;
1983 } else {
1984 int idx = bio->bi_idx + next_idx;
1985
1986 if (unlikely(idx >= bio->bi_vcnt)) {
1987 blk_dump_rq_flags(req, "__end_that");
1988 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
1989 __func__, idx, bio->bi_vcnt);
1990 break;
1991 }
1992
1993 nbytes = bio_iovec_idx(bio, idx)->bv_len;
1994 BIO_BUG_ON(nbytes > bio->bi_size);
1995
1996
1997
1998
1999 if (unlikely(nbytes > nr_bytes)) {
2000 bio_nbytes += nr_bytes;
2001 total_bytes += nr_bytes;
2002 break;
2003 }
2004
2005
2006
2007
2008 next_idx++;
2009 bio_nbytes += nbytes;
2010 }
2011
2012 total_bytes += nbytes;
2013 nr_bytes -= nbytes;
2014
2015 bio = req->bio;
2016 if (bio) {
2017
2018
2019
2020 if (unlikely(nr_bytes <= 0))
2021 break;
2022 }
2023 }
2024
2025
2026
2027
2028 if (!req->bio) {
2029
2030
2031
2032
2033
2034 req->__data_len = 0;
2035 return false;
2036 }
2037
2038
2039
2040
2041 if (bio_nbytes) {
2042 req_bio_endio(req, bio, bio_nbytes, error);
2043 bio->bi_idx += next_idx;
2044 bio_iovec(bio)->bv_offset += nr_bytes;
2045 bio_iovec(bio)->bv_len -= nr_bytes;
2046 }
2047
2048 req->__data_len -= total_bytes;
2049 req->buffer = bio_data(req->bio);
2050
2051
2052 if (blk_fs_request(req) || blk_discard_rq(req))
2053 req->__sector += total_bytes >> 9;
2054
2055
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
2061
2062
2063
2064
2065 if (blk_rq_bytes(req) < blk_rq_cur_bytes(req)) {
2066 printk(KERN_ERR "blk: request botched\n");
2067 req->__data_len = blk_rq_cur_bytes(req);
2068 }
2069
2070
2071 blk_recalc_rq_segments(req);
2072
2073 return true;
2074}
2075EXPORT_SYMBOL_GPL(blk_update_request);
2076
2077static bool blk_update_bidi_request(struct request *rq, int error,
2078 unsigned int nr_bytes,
2079 unsigned int bidi_bytes)
2080{
2081 if (blk_update_request(rq, error, nr_bytes))
2082 return true;
2083
2084
2085 if (unlikely(blk_bidi_rq(rq)) &&
2086 blk_update_request(rq->next_rq, error, bidi_bytes))
2087 return true;
2088
2089 add_disk_randomness(rq->rq_disk);
2090
2091 return false;
2092}
2093
2094
2095
2096
2097static void blk_finish_request(struct request *req, int error)
2098{
2099 if (blk_rq_tagged(req))
2100 blk_queue_end_tag(req->q, req);
2101
2102 BUG_ON(blk_queued_rq(req));
2103
2104 if (unlikely(laptop_mode) && blk_fs_request(req))
2105 laptop_io_completion();
2106
2107 blk_delete_timer(req);
2108
2109 blk_account_io_done(req);
2110
2111 if (req->end_io)
2112 req->end_io(req, error);
2113 else {
2114 if (blk_bidi_rq(req))
2115 __blk_put_request(req->next_rq->q, req->next_rq);
2116
2117 __blk_put_request(req->q, req);
2118 }
2119}
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138static bool blk_end_bidi_request(struct request *rq, int error,
2139 unsigned int nr_bytes, unsigned int bidi_bytes)
2140{
2141 struct request_queue *q = rq->q;
2142 unsigned long flags;
2143
2144 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2145 return true;
2146
2147 spin_lock_irqsave(q->queue_lock, flags);
2148 blk_finish_request(rq, error);
2149 spin_unlock_irqrestore(q->queue_lock, flags);
2150
2151 return false;
2152}
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169static bool __blk_end_bidi_request(struct request *rq, int error,
2170 unsigned int nr_bytes, unsigned int bidi_bytes)
2171{
2172 if (blk_update_bidi_request(rq, error, nr_bytes, bidi_bytes))
2173 return true;
2174
2175 blk_finish_request(rq, error);
2176
2177 return false;
2178}
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194bool blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
2195{
2196 return blk_end_bidi_request(rq, error, nr_bytes, 0);
2197}
2198EXPORT_SYMBOL(blk_end_request);
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208void blk_end_request_all(struct request *rq, int error)
2209{
2210 bool pending;
2211 unsigned int bidi_bytes = 0;
2212
2213 if (unlikely(blk_bidi_rq(rq)))
2214 bidi_bytes = blk_rq_bytes(rq->next_rq);
2215
2216 pending = blk_end_bidi_request(rq, error, blk_rq_bytes(rq), bidi_bytes);
2217 BUG_ON(pending);
2218}
2219EXPORT_SYMBOL(blk_end_request_all);
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233bool blk_end_request_cur(struct request *rq, int error)
2234{
2235 return blk_end_request(rq, error, blk_rq_cur_bytes(rq));
2236}
2237EXPORT_SYMBOL(blk_end_request_cur);
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251bool 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}
2256EXPORT_SYMBOL_GPL(blk_end_request_err);
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271bool __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
2272{
2273 return __blk_end_bidi_request(rq, error, nr_bytes, 0);
2274}
2275EXPORT_SYMBOL(__blk_end_request);
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285void __blk_end_request_all(struct request *rq, int error)
2286{
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);
2295}
2296EXPORT_SYMBOL(__blk_end_request_all);
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311bool __blk_end_request_cur(struct request *rq, int error)
2312{
2313 return __blk_end_request(rq, error, blk_rq_cur_bytes(rq));
2314}
2315EXPORT_SYMBOL(__blk_end_request_cur);
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330bool __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}
2335EXPORT_SYMBOL_GPL(__blk_end_request_err);
2336
2337void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
2338 struct bio *bio)
2339{
2340
2341 rq->cmd_flags |= bio->bi_rw & REQ_RW;
2342
2343 if (bio_has_data(bio)) {
2344 rq->nr_phys_segments = bio_phys_segments(q, bio);
2345 rq->buffer = bio_data(bio);
2346 }
2347 rq->__data_len = bio->bi_size;
2348 rq->bio = rq->biotail = bio;
2349
2350 if (bio->bi_bdev)
2351 rq->rq_disk = bio->bi_bdev->bd_disk;
2352}
2353
2354#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
2355
2356
2357
2358
2359
2360
2361
2362void 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}
2370EXPORT_SYMBOL_GPL(rq_flush_dcache_pages);
2371#endif
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392int 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}
2399EXPORT_SYMBOL_GPL(blk_lld_busy);
2400
2401
2402
2403
2404
2405
2406
2407
2408void 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}
2418EXPORT_SYMBOL_GPL(blk_rq_unprep_clone);
2419
2420
2421
2422
2423
2424static 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
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455int 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) &&
2475 bio_integrity_clone(bio, bio_src, gfp_mask, bs))
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
2492free_and_out:
2493 if (bio)
2494 bio_free(bio, bs);
2495 blk_rq_unprep_clone(rq);
2496
2497 return -ENOMEM;
2498}
2499EXPORT_SYMBOL_GPL(blk_rq_prep_clone);
2500
2501int kblockd_schedule_work(struct request_queue *q, struct work_struct *work)
2502{
2503 return queue_work(kblockd_workqueue, work);
2504}
2505EXPORT_SYMBOL(kblockd_schedule_work);
2506
2507int __init blk_dev_init(void)
2508{
2509 BUILD_BUG_ON(__REQ_NR_BITS > 8 *
2510 sizeof(((struct request *)0)->cmd_flags));
2511
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",
2517 sizeof(struct request), 0, SLAB_PANIC, NULL);
2518
2519 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
2520 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
2521
2522 return 0;
2523}
2524
2525