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