00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <linux/kernel.h>
00015 #include <linux/module.h>
00016 #include <linux/backing-dev.h>
00017 #include <linux/bio.h>
00018 #include <linux/blkdev.h>
00019 #include <linux/highmem.h>
00020 #include <linux/mm.h>
00021 #include <linux/kernel_stat.h>
00022 #include <linux/string.h>
00023 #include <linux/init.h>
00024 #include <linux/completion.h>
00025 #include <linux/slab.h>
00026 #include <linux/swap.h>
00027 #include <linux/writeback.h>
00028 #include <linux/task_io_accounting_ops.h>
00029 #include <linux/blktrace_api.h>
00030 #include <linux/fault-inject.h>
00031 #include <trace/block.h>
00032
00033 #include "blk.h"
00034
00035 DEFINE_TRACE(block_plug);
00036 DEFINE_TRACE(block_unplug_io);
00037 DEFINE_TRACE(block_unplug_timer);
00038 DEFINE_TRACE(block_getrq);
00039 DEFINE_TRACE(block_sleeprq);
00040 DEFINE_TRACE(block_rq_requeue);
00041 DEFINE_TRACE(block_bio_backmerge);
00042 DEFINE_TRACE(block_bio_frontmerge);
00043 DEFINE_TRACE(block_bio_queue);
00044 DEFINE_TRACE(block_rq_complete);
00045 DEFINE_TRACE(block_remap);
00046 EXPORT_TRACEPOINT_SYMBOL_GPL(block_remap);
00047
00048 static int __make_request(struct request_queue *q, struct bio *bio);
00049
00050
00051
00052
00053 static struct kmem_cache *request_cachep;
00054
00055
00056
00057
00058 struct kmem_cache *blk_requestq_cachep;
00059
00060
00061
00062
00063 static struct workqueue_struct *kblockd_workqueue;
00064
00065 static void drive_stat_acct(struct request *rq, int new_io)
00066 {
00067 struct gendisk *disk = rq->rq_disk;
00068 struct hd_struct *part;
00069 int rw = rq_data_dir(rq);
00070 int cpu;
00071
00072 if (!blk_fs_request(rq) || !disk || !blk_do_io_stat(disk->queue))
00073 return;
00074
00075 cpu = part_stat_lock();
00076 part = disk_map_sector_rcu(rq->rq_disk, rq->sector);
00077
00078 if (!new_io)
00079 part_stat_inc(cpu, part, merges[rw]);
00080 else {
00081 part_round_stats(cpu, part);
00082 part_inc_in_flight(part);
00083 }
00084
00085 part_stat_unlock();
00086 }
00087
00088 void blk_queue_congestion_threshold(struct request_queue *q)
00089 {
00090 int nr;
00091
00092 nr = q->nr_requests - (q->nr_requests / 8) + 1;
00093 if (nr > q->nr_requests)
00094 nr = q->nr_requests;
00095 q->nr_congestion_on = nr;
00096
00097 nr = q->nr_requests - (q->nr_requests / 8) - (q->nr_requests / 16) - 1;
00098 if (nr < 1)
00099 nr = 1;
00100 q->nr_congestion_off = nr;
00101 }
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112 struct backing_dev_info *blk_get_backing_dev_info(struct block_device *bdev)
00113 {
00114 struct backing_dev_info *ret = NULL;
00115 struct request_queue *q = bdev_get_queue(bdev);
00116
00117 if (q)
00118 ret = &q->backing_dev_info;
00119 return ret;
00120 }
00121 EXPORT_SYMBOL(blk_get_backing_dev_info);
00122
00123 void blk_rq_init(struct request_queue *q, struct request *rq)
00124 {
00125 memset(rq, 0, sizeof(*rq));
00126
00127 INIT_LIST_HEAD(&rq->queuelist);
00128 INIT_LIST_HEAD(&rq->timeout_list);
00129 rq->cpu = -1;
00130 rq->q = q;
00131 rq->sector = rq->hard_sector = (sector_t) -1;
00132 INIT_HLIST_NODE(&rq->hash);
00133 RB_CLEAR_NODE(&rq->rb_node);
00134 rq->cmd = rq->__cmd;
00135 rq->tag = -1;
00136 rq->ref_count = 1;
00137 }
00138 EXPORT_SYMBOL(blk_rq_init);
00139
00140 static void req_bio_endio(struct request *rq, struct bio *bio,
00141 unsigned int nbytes, int error)
00142 {
00143 struct request_queue *q = rq->q;
00144
00145 if (&q->bar_rq != rq) {
00146 if (error)
00147 clear_bit(BIO_UPTODATE, &bio->bi_flags);
00148 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
00149 error = -EIO;
00150
00151 if (unlikely(nbytes > bio->bi_size)) {
00152 printk(KERN_ERR "%s: want %u bytes done, %u left\n",
00153 __func__, nbytes, bio->bi_size);
00154 nbytes = bio->bi_size;
00155 }
00156
00157 if (unlikely(rq->cmd_flags & REQ_QUIET))
00158 set_bit(BIO_QUIET, &bio->bi_flags);
00159
00160 bio->bi_size -= nbytes;
00161 bio->bi_sector += (nbytes >> 9);
00162
00163 if (bio_integrity(bio))
00164 bio_integrity_advance(bio, nbytes);
00165
00166 if (bio->bi_size == 0)
00167 bio_endio(bio, error);
00168 } else {
00169
00170
00171
00172
00173
00174 if (error && !q->orderr)
00175 q->orderr = error;
00176 }
00177 }
00178
00179 void blk_dump_rq_flags(struct request *rq, char *msg)
00180 {
00181 int bit;
00182
00183 printk(KERN_INFO "%s: dev %s: type=%x, flags=%x\n", msg,
00184 rq->rq_disk ? rq->rq_disk->disk_name : "?", rq->cmd_type,
00185 rq->cmd_flags);
00186
00187 printk(KERN_INFO " sector %llu, nr/cnr %lu/%u\n",
00188 (unsigned long long)rq->sector,
00189 rq->nr_sectors,
00190 rq->current_nr_sectors);
00191 printk(KERN_INFO " bio %p, biotail %p, buffer %p, data %p, len %u\n",
00192 rq->bio, rq->biotail,
00193 rq->buffer, rq->data,
00194 rq->data_len);
00195
00196 if (blk_pc_request(rq)) {
00197 printk(KERN_INFO " cdb: ");
00198 for (bit = 0; bit < BLK_MAX_CDB; bit++)
00199 printk("%02x ", rq->cmd[bit]);
00200 printk("\n");
00201 }
00202 }
00203 EXPORT_SYMBOL(blk_dump_rq_flags);
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213 void blk_plug_device(struct request_queue *q)
00214 {
00215 WARN_ON(!irqs_disabled());
00216
00217
00218
00219
00220
00221 if (blk_queue_stopped(q))
00222 return;
00223
00224 if (!queue_flag_test_and_set(QUEUE_FLAG_PLUGGED, q)) {
00225 mod_timer(&q->unplug_timer, jiffies + q->unplug_delay);
00226 trace_block_plug(q);
00227 }
00228 }
00229 EXPORT_SYMBOL(blk_plug_device);
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239 void blk_plug_device_unlocked(struct request_queue *q)
00240 {
00241 unsigned long flags;
00242
00243 spin_lock_irqsave(q->queue_lock, flags);
00244 blk_plug_device(q);
00245 spin_unlock_irqrestore(q->queue_lock, flags);
00246 }
00247 EXPORT_SYMBOL(blk_plug_device_unlocked);
00248
00249
00250
00251
00252
00253 int blk_remove_plug(struct request_queue *q)
00254 {
00255 WARN_ON(!irqs_disabled());
00256
00257 if (!queue_flag_test_and_clear(QUEUE_FLAG_PLUGGED, q))
00258 return 0;
00259
00260 del_timer(&q->unplug_timer);
00261 return 1;
00262 }
00263 EXPORT_SYMBOL(blk_remove_plug);
00264
00265
00266
00267
00268 void __generic_unplug_device(struct request_queue *q)
00269 {
00270 if (unlikely(blk_queue_stopped(q)))
00271 return;
00272 if (!blk_remove_plug(q) && !blk_queue_nonrot(q))
00273 return;
00274
00275 q->request_fn(q);
00276 }
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289 void generic_unplug_device(struct request_queue *q)
00290 {
00291 if (blk_queue_plugged(q)) {
00292 spin_lock_irq(q->queue_lock);
00293 __generic_unplug_device(q);
00294 spin_unlock_irq(q->queue_lock);
00295 }
00296 }
00297 EXPORT_SYMBOL(generic_unplug_device);
00298
00299 static void blk_backing_dev_unplug(struct backing_dev_info *bdi,
00300 struct page *page)
00301 {
00302 struct request_queue *q = bdi->unplug_io_data;
00303
00304 blk_unplug(q);
00305 }
00306
00307 void blk_unplug_work(struct work_struct *work)
00308 {
00309 struct request_queue *q =
00310 container_of(work, struct request_queue, unplug_work);
00311
00312 trace_block_unplug_io(q);
00313 q->unplug_fn(q);
00314 }
00315
00316 void blk_unplug_timeout(unsigned long data)
00317 {
00318 struct request_queue *q = (struct request_queue *)data;
00319
00320 trace_block_unplug_timer(q);
00321 kblockd_schedule_work(q, &q->unplug_work);
00322 }
00323
00324 void blk_unplug(struct request_queue *q)
00325 {
00326
00327
00328
00329 if (q->unplug_fn) {
00330 trace_block_unplug_io(q);
00331 q->unplug_fn(q);
00332 }
00333 }
00334 EXPORT_SYMBOL(blk_unplug);
00335
00336 static void blk_invoke_request_fn(struct request_queue *q)
00337 {
00338 if (unlikely(blk_queue_stopped(q)))
00339 return;
00340
00341
00342
00343
00344
00345 if (!queue_flag_test_and_set(QUEUE_FLAG_REENTER, q)) {
00346 q->request_fn(q);
00347 queue_flag_clear(QUEUE_FLAG_REENTER, q);
00348 } else {
00349 queue_flag_set(QUEUE_FLAG_PLUGGED, q);
00350 kblockd_schedule_work(q, &q->unplug_work);
00351 }
00352 }
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363 void blk_start_queue(struct request_queue *q)
00364 {
00365 WARN_ON(!irqs_disabled());
00366
00367 queue_flag_clear(QUEUE_FLAG_STOPPED, q);
00368 blk_invoke_request_fn(q);
00369 }
00370 EXPORT_SYMBOL(blk_start_queue);
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386 void blk_stop_queue(struct request_queue *q)
00387 {
00388 blk_remove_plug(q);
00389 queue_flag_set(QUEUE_FLAG_STOPPED, q);
00390 }
00391 EXPORT_SYMBOL(blk_stop_queue);
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407 void blk_sync_queue(struct request_queue *q)
00408 {
00409 del_timer_sync(&q->unplug_timer);
00410 del_timer_sync(&q->timeout);
00411 cancel_work_sync(&q->unplug_work);
00412 }
00413 EXPORT_SYMBOL(blk_sync_queue);
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424 void __blk_run_queue(struct request_queue *q)
00425 {
00426 blk_remove_plug(q);
00427
00428
00429
00430
00431
00432 if (!elv_queue_empty(q))
00433 blk_invoke_request_fn(q);
00434 }
00435 EXPORT_SYMBOL(__blk_run_queue);
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447 void blk_run_queue(struct request_queue *q)
00448 {
00449 unsigned long flags;
00450
00451 spin_lock_irqsave(q->queue_lock, flags);
00452 __blk_run_queue(q);
00453 spin_unlock_irqrestore(q->queue_lock, flags);
00454 }
00455 EXPORT_SYMBOL(blk_run_queue);
00456
00457 void blk_put_queue(struct request_queue *q)
00458 {
00459 kobject_put(&q->kobj);
00460 }
00461
00462 void blk_cleanup_queue(struct request_queue *q)
00463 {
00464
00465
00466
00467
00468
00469
00470 blk_sync_queue(q);
00471
00472 mutex_lock(&q->sysfs_lock);
00473 queue_flag_set_unlocked(QUEUE_FLAG_DEAD, q);
00474 mutex_unlock(&q->sysfs_lock);
00475
00476 if (q->elevator)
00477 elevator_exit(q->elevator);
00478
00479 blk_put_queue(q);
00480 }
00481 EXPORT_SYMBOL(blk_cleanup_queue);
00482
00483 static int blk_init_free_list(struct request_queue *q)
00484 {
00485 struct request_list *rl = &q->rq;
00486
00487 rl->count[READ] = rl->count[WRITE] = 0;
00488 rl->starved[READ] = rl->starved[WRITE] = 0;
00489 rl->elvpriv = 0;
00490 init_waitqueue_head(&rl->wait[READ]);
00491 init_waitqueue_head(&rl->wait[WRITE]);
00492
00493 rl->rq_pool = mempool_create_node(BLKDEV_MIN_RQ, mempool_alloc_slab,
00494 mempool_free_slab, request_cachep, q->node);
00495
00496 if (!rl->rq_pool)
00497 return -ENOMEM;
00498
00499 return 0;
00500 }
00501
00502 struct request_queue *blk_alloc_queue(gfp_t gfp_mask)
00503 {
00504 return blk_alloc_queue_node(gfp_mask, -1);
00505 }
00506 EXPORT_SYMBOL(blk_alloc_queue);
00507
00508 struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id)
00509 {
00510 struct request_queue *q;
00511 int err;
00512
00513 q = kmem_cache_alloc_node(blk_requestq_cachep,
00514 gfp_mask | __GFP_ZERO, node_id);
00515 if (!q)
00516 return NULL;
00517
00518 q->backing_dev_info.unplug_io_fn = blk_backing_dev_unplug;
00519 q->backing_dev_info.unplug_io_data = q;
00520 err = bdi_init(&q->backing_dev_info);
00521 if (err) {
00522 kmem_cache_free(blk_requestq_cachep, q);
00523 return NULL;
00524 }
00525
00526 init_timer(&q->unplug_timer);
00527 setup_timer(&q->timeout, blk_rq_timed_out_timer, (unsigned long) q);
00528 INIT_LIST_HEAD(&q->timeout_list);
00529 INIT_WORK(&q->unplug_work, blk_unplug_work);
00530
00531 kobject_init(&q->kobj, &blk_queue_ktype);
00532
00533 mutex_init(&q->sysfs_lock);
00534 spin_lock_init(&q->__queue_lock);
00535
00536 return q;
00537 }
00538 EXPORT_SYMBOL(blk_alloc_queue_node);
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573 struct request_queue *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)
00574 {
00575 return blk_init_queue_node(rfn, lock, -1);
00576 }
00577 EXPORT_SYMBOL(blk_init_queue);
00578
00579 struct request_queue *
00580 blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)
00581 {
00582 struct request_queue *q = blk_alloc_queue_node(GFP_KERNEL, node_id);
00583
00584 if (!q)
00585 return NULL;
00586
00587 q->node = node_id;
00588 if (blk_init_free_list(q)) {
00589 kmem_cache_free(blk_requestq_cachep, q);
00590 return NULL;
00591 }
00592
00593
00594
00595
00596
00597 if (!lock)
00598 lock = &q->__queue_lock;
00599
00600 q->request_fn = rfn;
00601 q->prep_rq_fn = NULL;
00602 q->unplug_fn = generic_unplug_device;
00603 q->queue_flags = QUEUE_FLAG_DEFAULT;
00604 q->queue_lock = lock;
00605
00606 blk_queue_segment_boundary(q, BLK_SEG_BOUNDARY_MASK);
00607
00608 blk_queue_make_request(q, __make_request);
00609 blk_queue_max_segment_size(q, MAX_SEGMENT_SIZE);
00610
00611 blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS);
00612 blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS);
00613
00614 q->sg_reserved_size = INT_MAX;
00615
00616 blk_set_cmd_filter_defaults(&q->cmd_filter);
00617
00618
00619
00620
00621 if (!elevator_init(q, NULL)) {
00622 blk_queue_congestion_threshold(q);
00623 return q;
00624 }
00625
00626 blk_put_queue(q);
00627 return NULL;
00628 }
00629 EXPORT_SYMBOL(blk_init_queue_node);
00630
00631 int blk_get_queue(struct request_queue *q)
00632 {
00633 if (likely(!test_bit(QUEUE_FLAG_DEAD, &q->queue_flags))) {
00634 kobject_get(&q->kobj);
00635 return 0;
00636 }
00637
00638 return 1;
00639 }
00640
00641 static inline void blk_free_request(struct request_queue *q, struct request *rq)
00642 {
00643 if (rq->cmd_flags & REQ_ELVPRIV)
00644 elv_put_request(q, rq);
00645 mempool_free(rq, q->rq.rq_pool);
00646 }
00647
00648 static struct request *
00649 blk_alloc_request(struct request_queue *q, int rw, int priv, gfp_t gfp_mask)
00650 {
00651 struct request *rq = mempool_alloc(q->rq.rq_pool, gfp_mask);
00652
00653 if (!rq)
00654 return NULL;
00655
00656 blk_rq_init(q, rq);
00657
00658 rq->cmd_flags = rw | REQ_ALLOCED;
00659
00660 if (priv) {
00661 if (unlikely(elv_set_request(q, rq, gfp_mask))) {
00662 mempool_free(rq, q->rq.rq_pool);
00663 return NULL;
00664 }
00665 rq->cmd_flags |= REQ_ELVPRIV;
00666 }
00667
00668 return rq;
00669 }
00670
00671
00672
00673
00674
00675 static inline int ioc_batching(struct request_queue *q, struct io_context *ioc)
00676 {
00677 if (!ioc)
00678 return 0;
00679
00680
00681
00682
00683
00684
00685 return ioc->nr_batch_requests == q->nr_batching ||
00686 (ioc->nr_batch_requests > 0
00687 && time_before(jiffies, ioc->last_waited + BLK_BATCH_TIME));
00688 }
00689
00690
00691
00692
00693
00694
00695
00696 static void ioc_set_batching(struct request_queue *q, struct io_context *ioc)
00697 {
00698 if (!ioc || ioc_batching(q, ioc))
00699 return;
00700
00701 ioc->nr_batch_requests = q->nr_batching;
00702 ioc->last_waited = jiffies;
00703 }
00704
00705 static void __freed_request(struct request_queue *q, int rw)
00706 {
00707 struct request_list *rl = &q->rq;
00708
00709 if (rl->count[rw] < queue_congestion_off_threshold(q))
00710 blk_clear_queue_congested(q, rw);
00711
00712 if (rl->count[rw] + 1 <= q->nr_requests) {
00713 if (waitqueue_active(&rl->wait[rw]))
00714 wake_up(&rl->wait[rw]);
00715
00716 blk_clear_queue_full(q, rw);
00717 }
00718 }
00719
00720
00721
00722
00723
00724 static void freed_request(struct request_queue *q, int rw, int priv)
00725 {
00726 struct request_list *rl = &q->rq;
00727
00728 rl->count[rw]--;
00729 if (priv)
00730 rl->elvpriv--;
00731
00732 __freed_request(q, rw);
00733
00734 if (unlikely(rl->starved[rw ^ 1]))
00735 __freed_request(q, rw ^ 1);
00736 }
00737
00738 #define blkdev_free_rq(list) list_entry((list)->next, struct request, queuelist)
00739
00740
00741
00742
00743
00744 static struct request *get_request(struct request_queue *q, int rw_flags,
00745 struct bio *bio, gfp_t gfp_mask)
00746 {
00747 struct request *rq = NULL;
00748 struct request_list *rl = &q->rq;
00749 struct io_context *ioc = NULL;
00750 const int rw = rw_flags & 0x01;
00751 int may_queue, priv;
00752
00753 may_queue = elv_may_queue(q, rw_flags);
00754 if (may_queue == ELV_MQUEUE_NO)
00755 goto rq_starved;
00756
00757 if (rl->count[rw]+1 >= queue_congestion_on_threshold(q)) {
00758 if (rl->count[rw]+1 >= q->nr_requests) {
00759 ioc = current_io_context(GFP_ATOMIC, q->node);
00760
00761
00762
00763
00764
00765
00766 if (!blk_queue_full(q, rw)) {
00767 ioc_set_batching(q, ioc);
00768 blk_set_queue_full(q, rw);
00769 } else {
00770 if (may_queue != ELV_MQUEUE_MUST
00771 && !ioc_batching(q, ioc)) {
00772
00773
00774
00775
00776
00777 goto out;
00778 }
00779 }
00780 }
00781 blk_set_queue_congested(q, rw);
00782 }
00783
00784
00785
00786
00787
00788
00789 if (rl->count[rw] >= (3 * q->nr_requests / 2))
00790 goto out;
00791
00792 rl->count[rw]++;
00793 rl->starved[rw] = 0;
00794
00795 priv = !test_bit(QUEUE_FLAG_ELVSWITCH, &q->queue_flags);
00796 if (priv)
00797 rl->elvpriv++;
00798
00799 spin_unlock_irq(q->queue_lock);
00800
00801 rq = blk_alloc_request(q, rw_flags, priv, gfp_mask);
00802 if (unlikely(!rq)) {
00803
00804
00805
00806
00807
00808
00809
00810 spin_lock_irq(q->queue_lock);
00811 freed_request(q, rw, priv);
00812
00813
00814
00815
00816
00817
00818
00819
00820 rq_starved:
00821 if (unlikely(rl->count[rw] == 0))
00822 rl->starved[rw] = 1;
00823
00824 goto out;
00825 }
00826
00827
00828
00829
00830
00831
00832
00833 if (ioc_batching(q, ioc))
00834 ioc->nr_batch_requests--;
00835
00836 trace_block_getrq(q, bio, rw);
00837 out:
00838 return rq;
00839 }
00840
00841
00842
00843
00844
00845
00846
00847 static struct request *get_request_wait(struct request_queue *q, int rw_flags,
00848 struct bio *bio)
00849 {
00850 const int rw = rw_flags & 0x01;
00851 struct request *rq;
00852
00853 rq = get_request(q, rw_flags, bio, GFP_NOIO);
00854 while (!rq) {
00855 DEFINE_WAIT(wait);
00856 struct io_context *ioc;
00857 struct request_list *rl = &q->rq;
00858
00859 prepare_to_wait_exclusive(&rl->wait[rw], &wait,
00860 TASK_UNINTERRUPTIBLE);
00861
00862 trace_block_sleeprq(q, bio, rw);
00863
00864 __generic_unplug_device(q);
00865 spin_unlock_irq(q->queue_lock);
00866 io_schedule();
00867
00868
00869
00870
00871
00872
00873
00874 ioc = current_io_context(GFP_NOIO, q->node);
00875 ioc_set_batching(q, ioc);
00876
00877 spin_lock_irq(q->queue_lock);
00878 finish_wait(&rl->wait[rw], &wait);
00879
00880 rq = get_request(q, rw_flags, bio, GFP_NOIO);
00881 };
00882
00883 return rq;
00884 }
00885
00886 struct request *blk_get_request(struct request_queue *q, int rw, gfp_t gfp_mask)
00887 {
00888 struct request *rq;
00889
00890 BUG_ON(rw != READ && rw != WRITE);
00891
00892 spin_lock_irq(q->queue_lock);
00893 if (gfp_mask & __GFP_WAIT) {
00894 rq = get_request_wait(q, rw, NULL);
00895 } else {
00896 rq = get_request(q, rw, NULL, gfp_mask);
00897 if (!rq)
00898 spin_unlock_irq(q->queue_lock);
00899 }
00900
00901
00902 return rq;
00903 }
00904 EXPORT_SYMBOL(blk_get_request);
00905
00906
00907
00908
00909
00910
00911
00912
00913
00914
00915
00916
00917 void blk_start_queueing(struct request_queue *q)
00918 {
00919 if (!blk_queue_plugged(q)) {
00920 if (unlikely(blk_queue_stopped(q)))
00921 return;
00922 q->request_fn(q);
00923 } else
00924 __generic_unplug_device(q);
00925 }
00926 EXPORT_SYMBOL(blk_start_queueing);
00927
00928
00929
00930
00931
00932
00933
00934
00935
00936
00937
00938 void blk_requeue_request(struct request_queue *q, struct request *rq)
00939 {
00940 blk_delete_timer(rq);
00941 blk_clear_rq_complete(rq);
00942 trace_block_rq_requeue(q, rq);
00943
00944 if (blk_rq_tagged(rq))
00945 blk_queue_end_tag(q, rq);
00946
00947 elv_requeue_request(q, rq);
00948 }
00949 EXPORT_SYMBOL(blk_requeue_request);
00950
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970 void blk_insert_request(struct request_queue *q, struct request *rq,
00971 int at_head, void *data)
00972 {
00973 int where = at_head ? ELEVATOR_INSERT_FRONT : ELEVATOR_INSERT_BACK;
00974 unsigned long flags;
00975
00976
00977
00978
00979
00980
00981 rq->cmd_type = REQ_TYPE_SPECIAL;
00982 rq->cmd_flags |= REQ_SOFTBARRIER;
00983
00984 rq->special = data;
00985
00986 spin_lock_irqsave(q->queue_lock, flags);
00987
00988
00989
00990
00991 if (blk_rq_tagged(rq))
00992 blk_queue_end_tag(q, rq);
00993
00994 drive_stat_acct(rq, 1);
00995 __elv_add_request(q, rq, where, 0);
00996 blk_start_queueing(q);
00997 spin_unlock_irqrestore(q->queue_lock, flags);
00998 }
00999 EXPORT_SYMBOL(blk_insert_request);
01000
01001
01002
01003
01004
01005
01006 static inline void add_request(struct request_queue *q, struct request *req)
01007 {
01008 drive_stat_acct(req, 1);
01009
01010
01011
01012
01013
01014 __elv_add_request(q, req, ELEVATOR_INSERT_SORT, 0);
01015 }
01016
01017 static void part_round_stats_single(int cpu, struct hd_struct *part,
01018 unsigned long now)
01019 {
01020 if (now == part->stamp)
01021 return;
01022
01023 if (part->in_flight) {
01024 __part_stat_add(cpu, part, time_in_queue,
01025 part->in_flight * (now - part->stamp));
01026 __part_stat_add(cpu, part, io_ticks, (now - part->stamp));
01027 }
01028 part->stamp = now;
01029 }
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047 void part_round_stats(int cpu, struct hd_struct *part)
01048 {
01049 unsigned long now = jiffies;
01050
01051 if (part->partno)
01052 part_round_stats_single(cpu, &part_to_disk(part)->part0, now);
01053 part_round_stats_single(cpu, part, now);
01054 }
01055 EXPORT_SYMBOL_GPL(part_round_stats);
01056
01057
01058
01059
01060 void __blk_put_request(struct request_queue *q, struct request *req)
01061 {
01062 if (unlikely(!q))
01063 return;
01064 if (unlikely(--req->ref_count))
01065 return;
01066
01067 elv_completed_request(q, req);
01068
01069
01070
01071
01072
01073 if (req->cmd_flags & REQ_ALLOCED) {
01074 int rw = rq_data_dir(req);
01075 int priv = req->cmd_flags & REQ_ELVPRIV;
01076
01077 BUG_ON(!list_empty(&req->queuelist));
01078 BUG_ON(!hlist_unhashed(&req->hash));
01079
01080 blk_free_request(q, req);
01081 freed_request(q, rw, priv);
01082 }
01083 }
01084 EXPORT_SYMBOL_GPL(__blk_put_request);
01085
01086 void blk_put_request(struct request *req)
01087 {
01088 unsigned long flags;
01089 struct request_queue *q = req->q;
01090
01091 spin_lock_irqsave(q->queue_lock, flags);
01092 __blk_put_request(q, req);
01093 spin_unlock_irqrestore(q->queue_lock, flags);
01094 }
01095 EXPORT_SYMBOL(blk_put_request);
01096
01097 void init_request_from_bio(struct request *req, struct bio *bio)
01098 {
01099 req->cpu = bio->bi_comp_cpu;
01100 req->cmd_type = REQ_TYPE_FS;
01101
01102
01103
01104
01105 if (bio_rw_ahead(bio))
01106 req->cmd_flags |= (REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
01107 REQ_FAILFAST_DRIVER);
01108 if (bio_failfast_dev(bio))
01109 req->cmd_flags |= REQ_FAILFAST_DEV;
01110 if (bio_failfast_transport(bio))
01111 req->cmd_flags |= REQ_FAILFAST_TRANSPORT;
01112 if (bio_failfast_driver(bio))
01113 req->cmd_flags |= REQ_FAILFAST_DRIVER;
01114
01115
01116
01117
01118 if (unlikely(bio_discard(bio))) {
01119 req->cmd_flags |= REQ_DISCARD;
01120 if (bio_barrier(bio))
01121 req->cmd_flags |= REQ_SOFTBARRIER;
01122 req->q->prepare_discard_fn(req->q, req);
01123 } else if (unlikely(bio_barrier(bio)))
01124 req->cmd_flags |= (REQ_HARDBARRIER | REQ_NOMERGE);
01125
01126 if (bio_sync(bio))
01127 req->cmd_flags |= REQ_RW_SYNC;
01128 if (bio_unplug(bio))
01129 req->cmd_flags |= REQ_UNPLUG;
01130 if (bio_rw_meta(bio))
01131 req->cmd_flags |= REQ_RW_META;
01132
01133 req->errors = 0;
01134 req->hard_sector = req->sector = bio->bi_sector;
01135 req->ioprio = bio_prio(bio);
01136 req->start_time = jiffies;
01137 blk_rq_bio_prep(req->q, req, bio);
01138 }
01139
01140 static int __make_request(struct request_queue *q, struct bio *bio)
01141 {
01142 struct request *req;
01143 int el_ret, nr_sectors;
01144 const unsigned short prio = bio_prio(bio);
01145 const int sync = bio_sync(bio);
01146 const int unplug = bio_unplug(bio);
01147 int rw_flags;
01148
01149 nr_sectors = bio_sectors(bio);
01150
01151
01152
01153
01154
01155
01156 blk_queue_bounce(q, &bio);
01157
01158 spin_lock_irq(q->queue_lock);
01159
01160 if (unlikely(bio_barrier(bio)) || elv_queue_empty(q))
01161 goto get_rq;
01162
01163 el_ret = elv_merge(q, &req, bio);
01164 switch (el_ret) {
01165 case ELEVATOR_BACK_MERGE:
01166 BUG_ON(!rq_mergeable(req));
01167
01168 if (!ll_back_merge_fn(q, req, bio))
01169 break;
01170
01171 trace_block_bio_backmerge(q, bio);
01172
01173 req->biotail->bi_next = bio;
01174 req->biotail = bio;
01175 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
01176 req->ioprio = ioprio_best(req->ioprio, prio);
01177 if (!blk_rq_cpu_valid(req))
01178 req->cpu = bio->bi_comp_cpu;
01179 drive_stat_acct(req, 0);
01180 if (!attempt_back_merge(q, req))
01181 elv_merged_request(q, req, el_ret);
01182 goto out;
01183
01184 case ELEVATOR_FRONT_MERGE:
01185 BUG_ON(!rq_mergeable(req));
01186
01187 if (!ll_front_merge_fn(q, req, bio))
01188 break;
01189
01190 trace_block_bio_frontmerge(q, bio);
01191
01192 bio->bi_next = req->bio;
01193 req->bio = bio;
01194
01195
01196
01197
01198
01199
01200 req->buffer = bio_data(bio);
01201 req->current_nr_sectors = bio_cur_sectors(bio);
01202 req->hard_cur_sectors = req->current_nr_sectors;
01203 req->sector = req->hard_sector = bio->bi_sector;
01204 req->nr_sectors = req->hard_nr_sectors += nr_sectors;
01205 req->ioprio = ioprio_best(req->ioprio, prio);
01206 if (!blk_rq_cpu_valid(req))
01207 req->cpu = bio->bi_comp_cpu;
01208 drive_stat_acct(req, 0);
01209 if (!attempt_front_merge(q, req))
01210 elv_merged_request(q, req, el_ret);
01211 goto out;
01212
01213
01214 default:
01215 ;
01216 }
01217
01218 get_rq:
01219
01220
01221
01222
01223
01224 rw_flags = bio_data_dir(bio);
01225 if (sync)
01226 rw_flags |= REQ_RW_SYNC;
01227
01228
01229
01230
01231
01232 req = get_request_wait(q, rw_flags, bio);
01233
01234
01235
01236
01237
01238
01239
01240 init_request_from_bio(req, bio);
01241
01242 spin_lock_irq(q->queue_lock);
01243 if (test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags) ||
01244 bio_flagged(bio, BIO_CPU_AFFINE))
01245 req->cpu = blk_cpu_to_group(smp_processor_id());
01246 if (!blk_queue_nonrot(q) && elv_queue_empty(q))
01247 blk_plug_device(q);
01248 add_request(q, req);
01249 out:
01250 if (unplug || blk_queue_nonrot(q))
01251 __generic_unplug_device(q);
01252 spin_unlock_irq(q->queue_lock);
01253 return 0;
01254 }
01255
01256
01257
01258
01259 static inline void blk_partition_remap(struct bio *bio)
01260 {
01261 struct block_device *bdev = bio->bi_bdev;
01262
01263 if (bio_sectors(bio) && bdev != bdev->bd_contains) {
01264 struct hd_struct *p = bdev->bd_part;
01265
01266 bio->bi_sector += p->start_sect;
01267 bio->bi_bdev = bdev->bd_contains;
01268
01269 trace_block_remap(bdev_get_queue(bio->bi_bdev), bio,
01270 bdev->bd_dev, bio->bi_sector,
01271 bio->bi_sector - p->start_sect);
01272 }
01273 }
01274
01275 static void handle_bad_sector(struct bio *bio)
01276 {
01277 char b[BDEVNAME_SIZE];
01278
01279 printk(KERN_INFO "attempt to access beyond end of device\n");
01280 printk(KERN_INFO "%s: rw=%ld, want=%Lu, limit=%Lu\n",
01281 bdevname(bio->bi_bdev, b),
01282 bio->bi_rw,
01283 (unsigned long long)bio->bi_sector + bio_sectors(bio),
01284 (long long)(bio->bi_bdev->bd_inode->i_size >> 9));
01285
01286 set_bit(BIO_EOF, &bio->bi_flags);
01287 }
01288
01289 #ifdef CONFIG_FAIL_MAKE_REQUEST
01290
01291 static DECLARE_FAULT_ATTR(fail_make_request);
01292
01293 static int __init setup_fail_make_request(char *str)
01294 {
01295 return setup_fault_attr(&fail_make_request, str);
01296 }
01297 __setup("fail_make_request=", setup_fail_make_request);
01298
01299 static int should_fail_request(struct bio *bio)
01300 {
01301 struct hd_struct *part = bio->bi_bdev->bd_part;
01302
01303 if (part_to_disk(part)->part0.make_it_fail || part->make_it_fail)
01304 return should_fail(&fail_make_request, bio->bi_size);
01305
01306 return 0;
01307 }
01308
01309 static int __init fail_make_request_debugfs(void)
01310 {
01311 return init_fault_attr_dentries(&fail_make_request,
01312 "fail_make_request");
01313 }
01314
01315 late_initcall(fail_make_request_debugfs);
01316
01317 #else
01318
01319 static inline int should_fail_request(struct bio *bio)
01320 {
01321 return 0;
01322 }
01323
01324 #endif
01325
01326
01327
01328
01329 static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors)
01330 {
01331 sector_t maxsector;
01332
01333 if (!nr_sectors)
01334 return 0;
01335
01336
01337 maxsector = bio->bi_bdev->bd_inode->i_size >> 9;
01338 if (maxsector) {
01339 sector_t sector = bio->bi_sector;
01340
01341 if (maxsector < nr_sectors || maxsector - nr_sectors < sector) {
01342
01343
01344
01345
01346
01347 handle_bad_sector(bio);
01348 return 1;
01349 }
01350 }
01351
01352 return 0;
01353 }
01354
01355
01356
01357
01358
01359
01360
01361
01362
01363
01364
01365
01366
01367
01368
01369
01370
01371
01372
01373
01374
01375
01376
01377
01378
01379 static inline void __generic_make_request(struct bio *bio)
01380 {
01381 struct request_queue *q;
01382 sector_t old_sector;
01383 int ret, nr_sectors = bio_sectors(bio);
01384 dev_t old_dev;
01385 int err = -EIO;
01386
01387 might_sleep();
01388
01389 if (bio_check_eod(bio, nr_sectors))
01390 goto end_io;
01391
01392
01393
01394
01395
01396
01397
01398
01399
01400 old_sector = -1;
01401 old_dev = 0;
01402 do {
01403 char b[BDEVNAME_SIZE];
01404
01405 q = bdev_get_queue(bio->bi_bdev);
01406 if (unlikely(!q)) {
01407 printk(KERN_ERR
01408 "generic_make_request: Trying to access "
01409 "nonexistent block-device %s (%Lu)\n",
01410 bdevname(bio->bi_bdev, b),
01411 (long long) bio->bi_sector);
01412 goto end_io;
01413 }
01414
01415 if (unlikely(nr_sectors > q->max_hw_sectors)) {
01416 printk(KERN_ERR "bio too big device %s (%u > %u)\n",
01417 bdevname(bio->bi_bdev, b),
01418 bio_sectors(bio),
01419 q->max_hw_sectors);
01420 goto end_io;
01421 }
01422
01423 if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
01424 goto end_io;
01425
01426 if (should_fail_request(bio))
01427 goto end_io;
01428
01429
01430
01431
01432
01433 blk_partition_remap(bio);
01434
01435 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio))
01436 goto end_io;
01437
01438 if (old_sector != -1)
01439 trace_block_remap(q, bio, old_dev, bio->bi_sector,
01440 old_sector);
01441
01442 trace_block_bio_queue(q, bio);
01443
01444 old_sector = bio->bi_sector;
01445 old_dev = bio->bi_bdev->bd_dev;
01446
01447 if (bio_check_eod(bio, nr_sectors))
01448 goto end_io;
01449
01450 if (bio_discard(bio) && !q->prepare_discard_fn) {
01451 err = -EOPNOTSUPP;
01452 goto end_io;
01453 }
01454 if (bio_barrier(bio) && bio_has_data(bio) &&
01455 (q->next_ordered == QUEUE_ORDERED_NONE)) {
01456 err = -EOPNOTSUPP;
01457 goto end_io;
01458 }
01459
01460 ret = q->make_request_fn(q, bio);
01461 } while (ret);
01462
01463 return;
01464
01465 end_io:
01466 bio_endio(bio, err);
01467 }
01468
01469
01470
01471
01472
01473
01474
01475
01476
01477
01478
01479
01480 void generic_make_request(struct bio *bio)
01481 {
01482 if (current->bio_tail) {
01483
01484 *(current->bio_tail) = bio;
01485 bio->bi_next = NULL;
01486 current->bio_tail = &bio->bi_next;
01487 return;
01488 }
01489
01490
01491
01492
01493
01494
01495
01496
01497
01498
01499
01500
01501
01502
01503
01504
01505
01506
01507 BUG_ON(bio->bi_next);
01508 do {
01509 current->bio_list = bio->bi_next;
01510 if (bio->bi_next == NULL)
01511 current->bio_tail = ¤t->bio_list;
01512 else
01513 bio->bi_next = NULL;
01514 __generic_make_request(bio);
01515 bio = current->bio_list;
01516 } while (bio);
01517 current->bio_tail = NULL;
01518 }
01519 EXPORT_SYMBOL(generic_make_request);
01520
01521
01522
01523
01524
01525
01526
01527
01528
01529
01530
01531 void submit_bio(int rw, struct bio *bio)
01532 {
01533 int count = bio_sectors(bio);
01534
01535 bio->bi_rw |= rw;
01536
01537
01538
01539
01540
01541 if (bio_has_data(bio)) {
01542 if (rw & WRITE) {
01543 count_vm_events(PGPGOUT, count);
01544 } else {
01545 task_io_account_read(bio->bi_size);
01546 count_vm_events(PGPGIN, count);
01547 }
01548
01549 if (unlikely(block_dump)) {
01550 char b[BDEVNAME_SIZE];
01551 printk(KERN_DEBUG "%s(%d): %s block %Lu on %s\n",
01552 current->comm, task_pid_nr(current),
01553 (rw & WRITE) ? "WRITE" : "READ",
01554 (unsigned long long)bio->bi_sector,
01555 bdevname(bio->bi_bdev, b));
01556 }
01557 }
01558
01559 generic_make_request(bio);
01560 }
01561 EXPORT_SYMBOL(submit_bio);
01562
01563
01564
01565
01566
01567
01568
01569
01570
01571
01572
01573
01574
01575
01576
01577
01578
01579
01580
01581
01582
01583
01584 int blk_rq_check_limits(struct request_queue *q, struct request *rq)
01585 {
01586 if (rq->nr_sectors > q->max_sectors ||
01587 rq->data_len > q->max_hw_sectors << 9) {
01588 printk(KERN_ERR "%s: over max size limit.\n", __func__);
01589 return -EIO;
01590 }
01591
01592
01593
01594
01595
01596
01597
01598 blk_recalc_rq_segments(rq);
01599 if (rq->nr_phys_segments > q->max_phys_segments ||
01600 rq->nr_phys_segments > q->max_hw_segments) {
01601 printk(KERN_ERR "%s: over max segments limit.\n", __func__);
01602 return -EIO;
01603 }
01604
01605 return 0;
01606 }
01607 EXPORT_SYMBOL_GPL(blk_rq_check_limits);
01608
01609
01610
01611
01612
01613
01614 int blk_insert_cloned_request(struct request_queue *q, struct request *rq)
01615 {
01616 unsigned long flags;
01617
01618 if (blk_rq_check_limits(q, rq))
01619 return -EIO;
01620
01621 #ifdef CONFIG_FAIL_MAKE_REQUEST
01622 if (rq->rq_disk && rq->rq_disk->part0.make_it_fail &&
01623 should_fail(&fail_make_request, blk_rq_bytes(rq)))
01624 return -EIO;
01625 #endif
01626
01627 spin_lock_irqsave(q->queue_lock, flags);
01628
01629
01630
01631
01632
01633 BUG_ON(blk_queued_rq(rq));
01634
01635 drive_stat_acct(rq, 1);
01636 __elv_add_request(q, rq, ELEVATOR_INSERT_BACK, 0);
01637
01638 spin_unlock_irqrestore(q->queue_lock, flags);
01639
01640 return 0;
01641 }
01642 EXPORT_SYMBOL_GPL(blk_insert_cloned_request);
01643
01644
01645
01646
01647
01648
01649
01650
01651
01652
01653
01654 void blkdev_dequeue_request(struct request *req)
01655 {
01656 elv_dequeue_request(req->q, req);
01657
01658
01659
01660
01661
01662 blk_add_timer(req);
01663 }
01664 EXPORT_SYMBOL(blkdev_dequeue_request);
01665
01666 static void blk_account_io_completion(struct request *req, unsigned int bytes)
01667 {
01668 struct gendisk *disk = req->rq_disk;
01669
01670 if (!disk || !blk_do_io_stat(disk->queue))
01671 return;
01672
01673 if (blk_fs_request(req)) {
01674 const int rw = rq_data_dir(req);
01675 struct hd_struct *part;
01676 int cpu;
01677
01678 cpu = part_stat_lock();
01679 part = disk_map_sector_rcu(req->rq_disk, req->sector);
01680 part_stat_add(cpu, part, sectors[rw], bytes >> 9);
01681 part_stat_unlock();
01682 }
01683 }
01684
01685 static void blk_account_io_done(struct request *req)
01686 {
01687 struct gendisk *disk = req->rq_disk;
01688
01689 if (!disk || !blk_do_io_stat(disk->queue))
01690 return;
01691
01692
01693
01694
01695
01696
01697 if (blk_fs_request(req) && req != &req->q->bar_rq) {
01698 unsigned long duration = jiffies - req->start_time;
01699 const int rw = rq_data_dir(req);
01700 struct hd_struct *part;
01701 int cpu;
01702
01703 cpu = part_stat_lock();
01704 part = disk_map_sector_rcu(disk, req->sector);
01705
01706 part_stat_inc(cpu, part, ios[rw]);
01707 part_stat_add(cpu, part, ticks[rw], duration);
01708 part_round_stats(cpu, part);
01709 part_dec_in_flight(part);
01710
01711 part_stat_unlock();
01712 }
01713 }
01714
01715
01716
01717
01718
01719
01720
01721
01722
01723
01724
01725
01726
01727
01728
01729 static int __end_that_request_first(struct request *req, int error,
01730 int nr_bytes)
01731 {
01732 int total_bytes, bio_nbytes, next_idx = 0;
01733 struct bio *bio;
01734
01735 trace_block_rq_complete(req->q, req);
01736
01737
01738
01739
01740
01741 if (!blk_pc_request(req))
01742 req->errors = 0;
01743
01744 if (error && (blk_fs_request(req) && !(req->cmd_flags & REQ_QUIET))) {
01745 printk(KERN_ERR "end_request: I/O error, dev %s, sector %llu\n",
01746 req->rq_disk ? req->rq_disk->disk_name : "?",
01747 (unsigned long long)req->sector);
01748 }
01749
01750 blk_account_io_completion(req, nr_bytes);
01751
01752 total_bytes = bio_nbytes = 0;
01753 while ((bio = req->bio) != NULL) {
01754 int nbytes;
01755
01756 if (nr_bytes >= bio->bi_size) {
01757 req->bio = bio->bi_next;
01758 nbytes = bio->bi_size;
01759 req_bio_endio(req, bio, nbytes, error);
01760 next_idx = 0;
01761 bio_nbytes = 0;
01762 } else {
01763 int idx = bio->bi_idx + next_idx;
01764
01765 if (unlikely(bio->bi_idx >= bio->bi_vcnt)) {
01766 blk_dump_rq_flags(req, "__end_that");
01767 printk(KERN_ERR "%s: bio idx %d >= vcnt %d\n",
01768 __func__, bio->bi_idx, bio->bi_vcnt);
01769 break;
01770 }
01771
01772 nbytes = bio_iovec_idx(bio, idx)->bv_len;
01773 BIO_BUG_ON(nbytes > bio->bi_size);
01774
01775
01776
01777
01778 if (unlikely(nbytes > nr_bytes)) {
01779 bio_nbytes += nr_bytes;
01780 total_bytes += nr_bytes;
01781 break;
01782 }
01783
01784
01785
01786
01787 next_idx++;
01788 bio_nbytes += nbytes;
01789 }
01790
01791 total_bytes += nbytes;
01792 nr_bytes -= nbytes;
01793
01794 bio = req->bio;
01795 if (bio) {
01796
01797
01798
01799 if (unlikely(nr_bytes <= 0))
01800 break;
01801 }
01802 }
01803
01804
01805
01806
01807 if (!req->bio)
01808 return 0;
01809
01810
01811
01812
01813 if (bio_nbytes) {
01814 req_bio_endio(req, bio, bio_nbytes, error);
01815 bio->bi_idx += next_idx;
01816 bio_iovec(bio)->bv_offset += nr_bytes;
01817 bio_iovec(bio)->bv_len -= nr_bytes;
01818 }
01819
01820 blk_recalc_rq_sectors(req, total_bytes >> 9);
01821 blk_recalc_rq_segments(req);
01822 return 1;
01823 }
01824
01825
01826
01827
01828 static void end_that_request_last(struct request *req, int error)
01829 {
01830 if (blk_rq_tagged(req))
01831 blk_queue_end_tag(req->q, req);
01832
01833 if (blk_queued_rq(req))
01834 elv_dequeue_request(req->q, req);
01835
01836 #ifndef DDE_LINUX
01837 if (unlikely(laptop_mode) && blk_fs_request(req))
01838 laptop_io_completion();
01839 #endif
01840
01841 blk_delete_timer(req);
01842
01843 blk_account_io_done(req);
01844
01845 if (req->end_io)
01846 req->end_io(req, error);
01847 else {
01848 if (blk_bidi_rq(req))
01849 __blk_put_request(req->next_rq->q, req->next_rq);
01850
01851 __blk_put_request(req->q, req);
01852 }
01853 }
01854
01855
01856
01857
01858
01859 unsigned int blk_rq_bytes(struct request *rq)
01860 {
01861 if (blk_fs_request(rq))
01862 return rq->hard_nr_sectors << 9;
01863
01864 return rq->data_len;
01865 }
01866 EXPORT_SYMBOL_GPL(blk_rq_bytes);
01867
01868
01869
01870
01871
01872 unsigned int blk_rq_cur_bytes(struct request *rq)
01873 {
01874 if (blk_fs_request(rq))
01875 return rq->current_nr_sectors << 9;
01876
01877 if (rq->bio)
01878 return rq->bio->bi_size;
01879
01880 return rq->data_len;
01881 }
01882 EXPORT_SYMBOL_GPL(blk_rq_cur_bytes);
01883
01884
01885
01886
01887
01888
01889
01890
01891
01892
01893
01894
01895
01896
01897
01898
01899
01900 void end_request(struct request *req, int uptodate)
01901 {
01902 int error = 0;
01903
01904 if (uptodate <= 0)
01905 error = uptodate ? uptodate : -EIO;
01906
01907 __blk_end_request(req, error, req->hard_cur_sectors << 9);
01908 }
01909 EXPORT_SYMBOL(end_request);
01910
01911 static int end_that_request_data(struct request *rq, int error,
01912 unsigned int nr_bytes, unsigned int bidi_bytes)
01913 {
01914 if (rq->bio) {
01915 if (__end_that_request_first(rq, error, nr_bytes))
01916 return 1;
01917
01918
01919 if (blk_bidi_rq(rq) &&
01920 __end_that_request_first(rq->next_rq, error, bidi_bytes))
01921 return 1;
01922 }
01923
01924 return 0;
01925 }
01926
01927
01928
01929
01930
01931
01932
01933
01934
01935
01936
01937
01938
01939
01940
01941
01942
01943
01944
01945
01946 static int blk_end_io(struct request *rq, int error, unsigned int nr_bytes,
01947 unsigned int bidi_bytes,
01948 int (drv_callback)(struct request *))
01949 {
01950 struct request_queue *q = rq->q;
01951 unsigned long flags = 0UL;
01952
01953 if (end_that_request_data(rq, error, nr_bytes, bidi_bytes))
01954 return 1;
01955
01956
01957 if (drv_callback && drv_callback(rq))
01958 return 1;
01959
01960 #ifndef DDE_LINUX
01961 add_disk_randomness(rq->rq_disk);
01962 #endif
01963
01964 spin_lock_irqsave(q->queue_lock, flags);
01965 end_that_request_last(rq, error);
01966 spin_unlock_irqrestore(q->queue_lock, flags);
01967
01968 return 0;
01969 }
01970
01971
01972
01973
01974
01975
01976
01977
01978
01979
01980
01981
01982
01983
01984
01985 int blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
01986 {
01987 return blk_end_io(rq, error, nr_bytes, 0, NULL);
01988 }
01989 EXPORT_SYMBOL_GPL(blk_end_request);
01990
01991
01992
01993
01994
01995
01996
01997
01998
01999
02000
02001
02002
02003
02004 int __blk_end_request(struct request *rq, int error, unsigned int nr_bytes)
02005 {
02006 if (rq->bio && __end_that_request_first(rq, error, nr_bytes))
02007 return 1;
02008
02009 #ifndef DDE_LINUX
02010 add_disk_randomness(rq->rq_disk);
02011 #endif
02012
02013 end_that_request_last(rq, error);
02014
02015 return 0;
02016 }
02017 EXPORT_SYMBOL_GPL(__blk_end_request);
02018
02019
02020
02021
02022
02023
02024
02025
02026
02027
02028
02029
02030
02031
02032
02033 int blk_end_bidi_request(struct request *rq, int error, unsigned int nr_bytes,
02034 unsigned int bidi_bytes)
02035 {
02036 return blk_end_io(rq, error, nr_bytes, bidi_bytes, NULL);
02037 }
02038 EXPORT_SYMBOL_GPL(blk_end_bidi_request);
02039
02040
02041
02042
02043
02044
02045
02046
02047
02048
02049
02050
02051
02052
02053
02054
02055 void blk_update_request(struct request *rq, int error, unsigned int nr_bytes)
02056 {
02057 if (!end_that_request_data(rq, error, nr_bytes, 0)) {
02058
02059
02060
02061
02062
02063
02064 rq->nr_sectors = rq->hard_nr_sectors = 0;
02065 rq->current_nr_sectors = rq->hard_cur_sectors = 0;
02066 }
02067 }
02068 EXPORT_SYMBOL_GPL(blk_update_request);
02069
02070
02071
02072
02073
02074
02075
02076
02077
02078
02079
02080
02081
02082
02083
02084
02085
02086
02087
02088
02089
02090
02091
02092
02093
02094
02095 int blk_end_request_callback(struct request *rq, int error,
02096 unsigned int nr_bytes,
02097 int (drv_callback)(struct request *))
02098 {
02099 return blk_end_io(rq, error, nr_bytes, 0, drv_callback);
02100 }
02101 EXPORT_SYMBOL_GPL(blk_end_request_callback);
02102
02103 void blk_rq_bio_prep(struct request_queue *q, struct request *rq,
02104 struct bio *bio)
02105 {
02106
02107
02108 rq->cmd_flags |= (bio->bi_rw & 3);
02109
02110 if (bio_has_data(bio)) {
02111 rq->nr_phys_segments = bio_phys_segments(q, bio);
02112 rq->buffer = bio_data(bio);
02113 }
02114 rq->current_nr_sectors = bio_cur_sectors(bio);
02115 rq->hard_cur_sectors = rq->current_nr_sectors;
02116 rq->hard_nr_sectors = rq->nr_sectors = bio_sectors(bio);
02117 rq->data_len = bio->bi_size;
02118
02119 rq->bio = rq->biotail = bio;
02120
02121 if (bio->bi_bdev)
02122 rq->rq_disk = bio->bi_bdev->bd_disk;
02123 }
02124
02125
02126
02127
02128
02129
02130
02131
02132
02133
02134
02135
02136
02137
02138
02139
02140
02141
02142
02143
02144 int blk_lld_busy(struct request_queue *q)
02145 {
02146 if (q->lld_busy_fn)
02147 return q->lld_busy_fn(q);
02148
02149 return 0;
02150 }
02151 EXPORT_SYMBOL_GPL(blk_lld_busy);
02152
02153 int kblockd_schedule_work(struct request_queue *q, struct work_struct *work)
02154 {
02155 return queue_work(kblockd_workqueue, work);
02156 }
02157 EXPORT_SYMBOL(kblockd_schedule_work);
02158
02159 int __init blk_dev_init(void)
02160 {
02161 kblockd_workqueue = create_workqueue("kblockd");
02162 if (!kblockd_workqueue)
02163 panic("Failed to create kblockd\n");
02164
02165 request_cachep = kmem_cache_create("blkdev_requests",
02166 sizeof(struct request), 0, SLAB_PANIC, NULL);
02167
02168 blk_requestq_cachep = kmem_cache_create("blkdev_queue",
02169 sizeof(struct request_queue), 0, SLAB_PANIC, NULL);
02170
02171 return 0;
02172 }
02173