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