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