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