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