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