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