Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Functions related to sysfs handling |
| 3 | */ |
| 4 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 5 | #include <linux/slab.h> |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 6 | #include <linux/module.h> |
| 7 | #include <linux/bio.h> |
| 8 | #include <linux/blkdev.h> |
| 9 | #include <linux/blktrace_api.h> |
| 10 | |
| 11 | #include "blk.h" |
Tejun Heo | 5efd611 | 2012-03-05 13:15:12 -0800 | [diff] [blame] | 12 | #include "blk-cgroup.h" |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 13 | |
| 14 | struct queue_sysfs_entry { |
| 15 | struct attribute attr; |
| 16 | ssize_t (*show)(struct request_queue *, char *); |
| 17 | ssize_t (*store)(struct request_queue *, const char *, size_t); |
| 18 | }; |
| 19 | |
| 20 | static ssize_t |
Xiaotian Feng | 9cb308c | 2009-07-17 15:26:26 +0800 | [diff] [blame] | 21 | queue_var_show(unsigned long var, char *page) |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 22 | { |
Xiaotian Feng | 9cb308c | 2009-07-17 15:26:26 +0800 | [diff] [blame] | 23 | return sprintf(page, "%lu\n", var); |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | static ssize_t |
| 27 | queue_var_store(unsigned long *var, const char *page, size_t count) |
| 28 | { |
Dave Reisner | b1f3b64 | 2012-09-08 11:55:45 -0400 | [diff] [blame] | 29 | int err; |
| 30 | unsigned long v; |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 31 | |
Dave Reisner | b1f3b64 | 2012-09-08 11:55:45 -0400 | [diff] [blame] | 32 | err = strict_strtoul(page, 10, &v); |
| 33 | if (err || v > UINT_MAX) |
| 34 | return -EINVAL; |
| 35 | |
| 36 | *var = v; |
| 37 | |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 38 | return count; |
| 39 | } |
| 40 | |
| 41 | static ssize_t queue_requests_show(struct request_queue *q, char *page) |
| 42 | { |
| 43 | return queue_var_show(q->nr_requests, (page)); |
| 44 | } |
| 45 | |
| 46 | static ssize_t |
| 47 | queue_requests_store(struct request_queue *q, const char *page, size_t count) |
| 48 | { |
Tejun Heo | a051661 | 2012-06-26 15:05:44 -0700 | [diff] [blame] | 49 | struct request_list *rl; |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 50 | unsigned long nr; |
Jens Axboe | b8a9ae7 | 2009-09-11 22:44:29 +0200 | [diff] [blame] | 51 | int ret; |
| 52 | |
| 53 | if (!q->request_fn) |
| 54 | return -EINVAL; |
| 55 | |
| 56 | ret = queue_var_store(&nr, page, count); |
Dave Reisner | b1f3b64 | 2012-09-08 11:55:45 -0400 | [diff] [blame] | 57 | if (ret < 0) |
| 58 | return ret; |
| 59 | |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 60 | if (nr < BLKDEV_MIN_RQ) |
| 61 | nr = BLKDEV_MIN_RQ; |
| 62 | |
| 63 | spin_lock_irq(q->queue_lock); |
| 64 | q->nr_requests = nr; |
| 65 | blk_queue_congestion_threshold(q); |
| 66 | |
Tejun Heo | a051661 | 2012-06-26 15:05:44 -0700 | [diff] [blame] | 67 | /* congestion isn't cgroup aware and follows root blkcg for now */ |
| 68 | rl = &q->root_rl; |
| 69 | |
Jens Axboe | 1faa16d | 2009-04-06 14:48:01 +0200 | [diff] [blame] | 70 | if (rl->count[BLK_RW_SYNC] >= queue_congestion_on_threshold(q)) |
| 71 | blk_set_queue_congested(q, BLK_RW_SYNC); |
| 72 | else if (rl->count[BLK_RW_SYNC] < queue_congestion_off_threshold(q)) |
| 73 | blk_clear_queue_congested(q, BLK_RW_SYNC); |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 74 | |
Jens Axboe | 1faa16d | 2009-04-06 14:48:01 +0200 | [diff] [blame] | 75 | if (rl->count[BLK_RW_ASYNC] >= queue_congestion_on_threshold(q)) |
| 76 | blk_set_queue_congested(q, BLK_RW_ASYNC); |
| 77 | else if (rl->count[BLK_RW_ASYNC] < queue_congestion_off_threshold(q)) |
| 78 | blk_clear_queue_congested(q, BLK_RW_ASYNC); |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 79 | |
Tejun Heo | a051661 | 2012-06-26 15:05:44 -0700 | [diff] [blame] | 80 | blk_queue_for_each_rl(rl, q) { |
| 81 | if (rl->count[BLK_RW_SYNC] >= q->nr_requests) { |
| 82 | blk_set_rl_full(rl, BLK_RW_SYNC); |
| 83 | } else { |
| 84 | blk_clear_rl_full(rl, BLK_RW_SYNC); |
| 85 | wake_up(&rl->wait[BLK_RW_SYNC]); |
| 86 | } |
| 87 | |
| 88 | if (rl->count[BLK_RW_ASYNC] >= q->nr_requests) { |
| 89 | blk_set_rl_full(rl, BLK_RW_ASYNC); |
| 90 | } else { |
| 91 | blk_clear_rl_full(rl, BLK_RW_ASYNC); |
| 92 | wake_up(&rl->wait[BLK_RW_ASYNC]); |
| 93 | } |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 94 | } |
| 95 | |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 96 | spin_unlock_irq(q->queue_lock); |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | static ssize_t queue_ra_show(struct request_queue *q, char *page) |
| 101 | { |
Xiaotian Feng | 9cb308c | 2009-07-17 15:26:26 +0800 | [diff] [blame] | 102 | unsigned long ra_kb = q->backing_dev_info.ra_pages << |
| 103 | (PAGE_CACHE_SHIFT - 10); |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 104 | |
| 105 | return queue_var_show(ra_kb, (page)); |
| 106 | } |
| 107 | |
| 108 | static ssize_t |
| 109 | queue_ra_store(struct request_queue *q, const char *page, size_t count) |
| 110 | { |
| 111 | unsigned long ra_kb; |
| 112 | ssize_t ret = queue_var_store(&ra_kb, page, count); |
| 113 | |
Dave Reisner | b1f3b64 | 2012-09-08 11:55:45 -0400 | [diff] [blame] | 114 | if (ret < 0) |
| 115 | return ret; |
| 116 | |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 117 | q->backing_dev_info.ra_pages = ra_kb >> (PAGE_CACHE_SHIFT - 10); |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 118 | |
| 119 | return ret; |
| 120 | } |
| 121 | |
| 122 | static ssize_t queue_max_sectors_show(struct request_queue *q, char *page) |
| 123 | { |
Martin K. Petersen | ae03bf6 | 2009-05-22 17:17:50 -0400 | [diff] [blame] | 124 | int max_sectors_kb = queue_max_sectors(q) >> 1; |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 125 | |
| 126 | return queue_var_show(max_sectors_kb, (page)); |
| 127 | } |
| 128 | |
Martin K. Petersen | c77a571 | 2010-03-10 00:48:33 -0500 | [diff] [blame] | 129 | static ssize_t queue_max_segments_show(struct request_queue *q, char *page) |
| 130 | { |
| 131 | return queue_var_show(queue_max_segments(q), (page)); |
| 132 | } |
| 133 | |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 134 | static ssize_t queue_max_integrity_segments_show(struct request_queue *q, char *page) |
| 135 | { |
| 136 | return queue_var_show(q->limits.max_integrity_segments, (page)); |
| 137 | } |
| 138 | |
Martin K. Petersen | c77a571 | 2010-03-10 00:48:33 -0500 | [diff] [blame] | 139 | static ssize_t queue_max_segment_size_show(struct request_queue *q, char *page) |
| 140 | { |
Martin K. Petersen | e692cb6 | 2010-12-01 19:41:49 +0100 | [diff] [blame] | 141 | if (blk_queue_cluster(q)) |
Martin K. Petersen | c77a571 | 2010-03-10 00:48:33 -0500 | [diff] [blame] | 142 | return queue_var_show(queue_max_segment_size(q), (page)); |
| 143 | |
| 144 | return queue_var_show(PAGE_CACHE_SIZE, (page)); |
| 145 | } |
| 146 | |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 147 | static ssize_t queue_logical_block_size_show(struct request_queue *q, char *page) |
Martin K. Petersen | e68b903 | 2008-01-29 19:14:08 +0100 | [diff] [blame] | 148 | { |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 149 | return queue_var_show(queue_logical_block_size(q), page); |
Martin K. Petersen | e68b903 | 2008-01-29 19:14:08 +0100 | [diff] [blame] | 150 | } |
| 151 | |
Martin K. Petersen | c72758f | 2009-05-22 17:17:53 -0400 | [diff] [blame] | 152 | static ssize_t queue_physical_block_size_show(struct request_queue *q, char *page) |
| 153 | { |
| 154 | return queue_var_show(queue_physical_block_size(q), page); |
| 155 | } |
| 156 | |
| 157 | static ssize_t queue_io_min_show(struct request_queue *q, char *page) |
| 158 | { |
| 159 | return queue_var_show(queue_io_min(q), page); |
| 160 | } |
| 161 | |
| 162 | static ssize_t queue_io_opt_show(struct request_queue *q, char *page) |
| 163 | { |
| 164 | return queue_var_show(queue_io_opt(q), page); |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 165 | } |
| 166 | |
Martin K. Petersen | 86b3728 | 2009-11-10 11:50:21 +0100 | [diff] [blame] | 167 | static ssize_t queue_discard_granularity_show(struct request_queue *q, char *page) |
| 168 | { |
| 169 | return queue_var_show(q->limits.discard_granularity, page); |
| 170 | } |
| 171 | |
| 172 | static ssize_t queue_discard_max_show(struct request_queue *q, char *page) |
| 173 | { |
Martin K. Petersen | a934a00 | 2011-05-18 10:37:35 +0200 | [diff] [blame] | 174 | return sprintf(page, "%llu\n", |
| 175 | (unsigned long long)q->limits.max_discard_sectors << 9); |
Martin K. Petersen | 86b3728 | 2009-11-10 11:50:21 +0100 | [diff] [blame] | 176 | } |
| 177 | |
Martin K. Petersen | 98262f2 | 2009-12-03 09:24:48 +0100 | [diff] [blame] | 178 | static ssize_t queue_discard_zeroes_data_show(struct request_queue *q, char *page) |
| 179 | { |
| 180 | return queue_var_show(queue_discard_zeroes_data(q), page); |
| 181 | } |
| 182 | |
Martin K. Petersen | 4363ac7 | 2012-09-18 12:19:27 -0400 | [diff] [blame] | 183 | static ssize_t queue_write_same_max_show(struct request_queue *q, char *page) |
| 184 | { |
| 185 | return sprintf(page, "%llu\n", |
| 186 | (unsigned long long)q->limits.max_write_same_sectors << 9); |
| 187 | } |
| 188 | |
| 189 | |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 190 | static ssize_t |
| 191 | queue_max_sectors_store(struct request_queue *q, const char *page, size_t count) |
| 192 | { |
| 193 | unsigned long max_sectors_kb, |
Martin K. Petersen | ae03bf6 | 2009-05-22 17:17:50 -0400 | [diff] [blame] | 194 | max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1, |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 195 | page_kb = 1 << (PAGE_CACHE_SHIFT - 10); |
| 196 | ssize_t ret = queue_var_store(&max_sectors_kb, page, count); |
| 197 | |
Dave Reisner | b1f3b64 | 2012-09-08 11:55:45 -0400 | [diff] [blame] | 198 | if (ret < 0) |
| 199 | return ret; |
| 200 | |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 201 | if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb) |
| 202 | return -EINVAL; |
Wu Fengguang | 7c23951 | 2008-11-25 09:08:39 +0100 | [diff] [blame] | 203 | |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 204 | spin_lock_irq(q->queue_lock); |
Nikanth Karthikesan | c295fc0 | 2009-09-01 22:40:15 +0200 | [diff] [blame] | 205 | q->limits.max_sectors = max_sectors_kb << 1; |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 206 | spin_unlock_irq(q->queue_lock); |
| 207 | |
| 208 | return ret; |
| 209 | } |
| 210 | |
| 211 | static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page) |
| 212 | { |
Martin K. Petersen | ae03bf6 | 2009-05-22 17:17:50 -0400 | [diff] [blame] | 213 | int max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1; |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 214 | |
| 215 | return queue_var_show(max_hw_sectors_kb, (page)); |
| 216 | } |
| 217 | |
Jens Axboe | 956bcb7 | 2010-08-07 18:13:50 +0200 | [diff] [blame] | 218 | #define QUEUE_SYSFS_BIT_FNS(name, flag, neg) \ |
| 219 | static ssize_t \ |
| 220 | queue_show_##name(struct request_queue *q, char *page) \ |
| 221 | { \ |
| 222 | int bit; \ |
| 223 | bit = test_bit(QUEUE_FLAG_##flag, &q->queue_flags); \ |
| 224 | return queue_var_show(neg ? !bit : bit, page); \ |
| 225 | } \ |
| 226 | static ssize_t \ |
| 227 | queue_store_##name(struct request_queue *q, const char *page, size_t count) \ |
| 228 | { \ |
| 229 | unsigned long val; \ |
| 230 | ssize_t ret; \ |
| 231 | ret = queue_var_store(&val, page, count); \ |
Arnd Bergmann | c678ef5 | 2013-04-03 21:53:57 +0200 | [diff] [blame] | 232 | if (ret < 0) \ |
| 233 | return ret; \ |
Jens Axboe | 956bcb7 | 2010-08-07 18:13:50 +0200 | [diff] [blame] | 234 | if (neg) \ |
| 235 | val = !val; \ |
| 236 | \ |
| 237 | spin_lock_irq(q->queue_lock); \ |
| 238 | if (val) \ |
| 239 | queue_flag_set(QUEUE_FLAG_##flag, q); \ |
| 240 | else \ |
| 241 | queue_flag_clear(QUEUE_FLAG_##flag, q); \ |
| 242 | spin_unlock_irq(q->queue_lock); \ |
| 243 | return ret; \ |
Bartlomiej Zolnierkiewicz | 1308835f | 2009-01-07 12:22:39 +0100 | [diff] [blame] | 244 | } |
| 245 | |
Jens Axboe | 956bcb7 | 2010-08-07 18:13:50 +0200 | [diff] [blame] | 246 | QUEUE_SYSFS_BIT_FNS(nonrot, NONROT, 1); |
| 247 | QUEUE_SYSFS_BIT_FNS(random, ADD_RANDOM, 0); |
| 248 | QUEUE_SYSFS_BIT_FNS(iostats, IO_STAT, 0); |
| 249 | #undef QUEUE_SYSFS_BIT_FNS |
Bartlomiej Zolnierkiewicz | 1308835f | 2009-01-07 12:22:39 +0100 | [diff] [blame] | 250 | |
Alan D. Brunelle | ac9fafa | 2008-04-29 14:44:19 +0200 | [diff] [blame] | 251 | static ssize_t queue_nomerges_show(struct request_queue *q, char *page) |
| 252 | { |
Alan D. Brunelle | 488991e | 2010-01-29 09:04:08 +0100 | [diff] [blame] | 253 | return queue_var_show((blk_queue_nomerges(q) << 1) | |
| 254 | blk_queue_noxmerges(q), page); |
Alan D. Brunelle | ac9fafa | 2008-04-29 14:44:19 +0200 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | static ssize_t queue_nomerges_store(struct request_queue *q, const char *page, |
| 258 | size_t count) |
| 259 | { |
| 260 | unsigned long nm; |
| 261 | ssize_t ret = queue_var_store(&nm, page, count); |
| 262 | |
Dave Reisner | b1f3b64 | 2012-09-08 11:55:45 -0400 | [diff] [blame] | 263 | if (ret < 0) |
| 264 | return ret; |
| 265 | |
Jens Axboe | bf0f970 | 2008-05-07 09:09:39 +0200 | [diff] [blame] | 266 | spin_lock_irq(q->queue_lock); |
Alan D. Brunelle | 488991e | 2010-01-29 09:04:08 +0100 | [diff] [blame] | 267 | queue_flag_clear(QUEUE_FLAG_NOMERGES, q); |
| 268 | queue_flag_clear(QUEUE_FLAG_NOXMERGES, q); |
| 269 | if (nm == 2) |
Jens Axboe | bf0f970 | 2008-05-07 09:09:39 +0200 | [diff] [blame] | 270 | queue_flag_set(QUEUE_FLAG_NOMERGES, q); |
Alan D. Brunelle | 488991e | 2010-01-29 09:04:08 +0100 | [diff] [blame] | 271 | else if (nm) |
| 272 | queue_flag_set(QUEUE_FLAG_NOXMERGES, q); |
Jens Axboe | bf0f970 | 2008-05-07 09:09:39 +0200 | [diff] [blame] | 273 | spin_unlock_irq(q->queue_lock); |
Bartlomiej Zolnierkiewicz | 1308835f | 2009-01-07 12:22:39 +0100 | [diff] [blame] | 274 | |
Alan D. Brunelle | ac9fafa | 2008-04-29 14:44:19 +0200 | [diff] [blame] | 275 | return ret; |
| 276 | } |
| 277 | |
Jens Axboe | c7c22e4 | 2008-09-13 20:26:01 +0200 | [diff] [blame] | 278 | static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page) |
| 279 | { |
Xiaotian Feng | 9cb308c | 2009-07-17 15:26:26 +0800 | [diff] [blame] | 280 | bool set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags); |
Dan Williams | 5757a6d | 2011-07-23 20:44:25 +0200 | [diff] [blame] | 281 | bool force = test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags); |
Jens Axboe | c7c22e4 | 2008-09-13 20:26:01 +0200 | [diff] [blame] | 282 | |
Dan Williams | 5757a6d | 2011-07-23 20:44:25 +0200 | [diff] [blame] | 283 | return queue_var_show(set << force, page); |
Jens Axboe | c7c22e4 | 2008-09-13 20:26:01 +0200 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | static ssize_t |
| 287 | queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count) |
| 288 | { |
| 289 | ssize_t ret = -EINVAL; |
| 290 | #if defined(CONFIG_USE_GENERIC_SMP_HELPERS) |
| 291 | unsigned long val; |
| 292 | |
| 293 | ret = queue_var_store(&val, page, count); |
Dave Reisner | b1f3b64 | 2012-09-08 11:55:45 -0400 | [diff] [blame] | 294 | if (ret < 0) |
| 295 | return ret; |
| 296 | |
Jens Axboe | c7c22e4 | 2008-09-13 20:26:01 +0200 | [diff] [blame] | 297 | spin_lock_irq(q->queue_lock); |
Eric Seppanen | e8037d4 | 2011-08-23 21:25:12 +0200 | [diff] [blame] | 298 | if (val == 2) { |
Jens Axboe | c7c22e4 | 2008-09-13 20:26:01 +0200 | [diff] [blame] | 299 | queue_flag_set(QUEUE_FLAG_SAME_COMP, q); |
Eric Seppanen | e8037d4 | 2011-08-23 21:25:12 +0200 | [diff] [blame] | 300 | queue_flag_set(QUEUE_FLAG_SAME_FORCE, q); |
| 301 | } else if (val == 1) { |
| 302 | queue_flag_set(QUEUE_FLAG_SAME_COMP, q); |
| 303 | queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q); |
| 304 | } else if (val == 0) { |
Dan Williams | 5757a6d | 2011-07-23 20:44:25 +0200 | [diff] [blame] | 305 | queue_flag_clear(QUEUE_FLAG_SAME_COMP, q); |
| 306 | queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q); |
| 307 | } |
Jens Axboe | c7c22e4 | 2008-09-13 20:26:01 +0200 | [diff] [blame] | 308 | spin_unlock_irq(q->queue_lock); |
| 309 | #endif |
| 310 | return ret; |
| 311 | } |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 312 | |
| 313 | static struct queue_sysfs_entry queue_requests_entry = { |
| 314 | .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR }, |
| 315 | .show = queue_requests_show, |
| 316 | .store = queue_requests_store, |
| 317 | }; |
| 318 | |
| 319 | static struct queue_sysfs_entry queue_ra_entry = { |
| 320 | .attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR }, |
| 321 | .show = queue_ra_show, |
| 322 | .store = queue_ra_store, |
| 323 | }; |
| 324 | |
| 325 | static struct queue_sysfs_entry queue_max_sectors_entry = { |
| 326 | .attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR }, |
| 327 | .show = queue_max_sectors_show, |
| 328 | .store = queue_max_sectors_store, |
| 329 | }; |
| 330 | |
| 331 | static struct queue_sysfs_entry queue_max_hw_sectors_entry = { |
| 332 | .attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO }, |
| 333 | .show = queue_max_hw_sectors_show, |
| 334 | }; |
| 335 | |
Martin K. Petersen | c77a571 | 2010-03-10 00:48:33 -0500 | [diff] [blame] | 336 | static struct queue_sysfs_entry queue_max_segments_entry = { |
| 337 | .attr = {.name = "max_segments", .mode = S_IRUGO }, |
| 338 | .show = queue_max_segments_show, |
| 339 | }; |
| 340 | |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 341 | static struct queue_sysfs_entry queue_max_integrity_segments_entry = { |
| 342 | .attr = {.name = "max_integrity_segments", .mode = S_IRUGO }, |
| 343 | .show = queue_max_integrity_segments_show, |
| 344 | }; |
| 345 | |
Martin K. Petersen | c77a571 | 2010-03-10 00:48:33 -0500 | [diff] [blame] | 346 | static struct queue_sysfs_entry queue_max_segment_size_entry = { |
| 347 | .attr = {.name = "max_segment_size", .mode = S_IRUGO }, |
| 348 | .show = queue_max_segment_size_show, |
| 349 | }; |
| 350 | |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 351 | static struct queue_sysfs_entry queue_iosched_entry = { |
| 352 | .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR }, |
| 353 | .show = elv_iosched_show, |
| 354 | .store = elv_iosched_store, |
| 355 | }; |
| 356 | |
Martin K. Petersen | e68b903 | 2008-01-29 19:14:08 +0100 | [diff] [blame] | 357 | static struct queue_sysfs_entry queue_hw_sector_size_entry = { |
| 358 | .attr = {.name = "hw_sector_size", .mode = S_IRUGO }, |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 359 | .show = queue_logical_block_size_show, |
| 360 | }; |
| 361 | |
| 362 | static struct queue_sysfs_entry queue_logical_block_size_entry = { |
| 363 | .attr = {.name = "logical_block_size", .mode = S_IRUGO }, |
| 364 | .show = queue_logical_block_size_show, |
Martin K. Petersen | e68b903 | 2008-01-29 19:14:08 +0100 | [diff] [blame] | 365 | }; |
| 366 | |
Martin K. Petersen | c72758f | 2009-05-22 17:17:53 -0400 | [diff] [blame] | 367 | static struct queue_sysfs_entry queue_physical_block_size_entry = { |
| 368 | .attr = {.name = "physical_block_size", .mode = S_IRUGO }, |
| 369 | .show = queue_physical_block_size_show, |
| 370 | }; |
| 371 | |
| 372 | static struct queue_sysfs_entry queue_io_min_entry = { |
| 373 | .attr = {.name = "minimum_io_size", .mode = S_IRUGO }, |
| 374 | .show = queue_io_min_show, |
| 375 | }; |
| 376 | |
| 377 | static struct queue_sysfs_entry queue_io_opt_entry = { |
| 378 | .attr = {.name = "optimal_io_size", .mode = S_IRUGO }, |
| 379 | .show = queue_io_opt_show, |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 380 | }; |
| 381 | |
Martin K. Petersen | 86b3728 | 2009-11-10 11:50:21 +0100 | [diff] [blame] | 382 | static struct queue_sysfs_entry queue_discard_granularity_entry = { |
| 383 | .attr = {.name = "discard_granularity", .mode = S_IRUGO }, |
| 384 | .show = queue_discard_granularity_show, |
| 385 | }; |
| 386 | |
| 387 | static struct queue_sysfs_entry queue_discard_max_entry = { |
| 388 | .attr = {.name = "discard_max_bytes", .mode = S_IRUGO }, |
| 389 | .show = queue_discard_max_show, |
| 390 | }; |
| 391 | |
Martin K. Petersen | 98262f2 | 2009-12-03 09:24:48 +0100 | [diff] [blame] | 392 | static struct queue_sysfs_entry queue_discard_zeroes_data_entry = { |
| 393 | .attr = {.name = "discard_zeroes_data", .mode = S_IRUGO }, |
| 394 | .show = queue_discard_zeroes_data_show, |
| 395 | }; |
| 396 | |
Martin K. Petersen | 4363ac7 | 2012-09-18 12:19:27 -0400 | [diff] [blame] | 397 | static struct queue_sysfs_entry queue_write_same_max_entry = { |
| 398 | .attr = {.name = "write_same_max_bytes", .mode = S_IRUGO }, |
| 399 | .show = queue_write_same_max_show, |
| 400 | }; |
| 401 | |
Bartlomiej Zolnierkiewicz | 1308835f | 2009-01-07 12:22:39 +0100 | [diff] [blame] | 402 | static struct queue_sysfs_entry queue_nonrot_entry = { |
| 403 | .attr = {.name = "rotational", .mode = S_IRUGO | S_IWUSR }, |
Jens Axboe | 956bcb7 | 2010-08-07 18:13:50 +0200 | [diff] [blame] | 404 | .show = queue_show_nonrot, |
| 405 | .store = queue_store_nonrot, |
Bartlomiej Zolnierkiewicz | 1308835f | 2009-01-07 12:22:39 +0100 | [diff] [blame] | 406 | }; |
| 407 | |
Alan D. Brunelle | ac9fafa | 2008-04-29 14:44:19 +0200 | [diff] [blame] | 408 | static struct queue_sysfs_entry queue_nomerges_entry = { |
| 409 | .attr = {.name = "nomerges", .mode = S_IRUGO | S_IWUSR }, |
| 410 | .show = queue_nomerges_show, |
| 411 | .store = queue_nomerges_store, |
| 412 | }; |
| 413 | |
Jens Axboe | c7c22e4 | 2008-09-13 20:26:01 +0200 | [diff] [blame] | 414 | static struct queue_sysfs_entry queue_rq_affinity_entry = { |
| 415 | .attr = {.name = "rq_affinity", .mode = S_IRUGO | S_IWUSR }, |
| 416 | .show = queue_rq_affinity_show, |
| 417 | .store = queue_rq_affinity_store, |
| 418 | }; |
| 419 | |
Jens Axboe | bc58ba9 | 2009-01-23 10:54:44 +0100 | [diff] [blame] | 420 | static struct queue_sysfs_entry queue_iostats_entry = { |
| 421 | .attr = {.name = "iostats", .mode = S_IRUGO | S_IWUSR }, |
Jens Axboe | 956bcb7 | 2010-08-07 18:13:50 +0200 | [diff] [blame] | 422 | .show = queue_show_iostats, |
| 423 | .store = queue_store_iostats, |
Jens Axboe | bc58ba9 | 2009-01-23 10:54:44 +0100 | [diff] [blame] | 424 | }; |
| 425 | |
Jens Axboe | e2e1a14 | 2010-06-09 10:42:09 +0200 | [diff] [blame] | 426 | static struct queue_sysfs_entry queue_random_entry = { |
| 427 | .attr = {.name = "add_random", .mode = S_IRUGO | S_IWUSR }, |
Jens Axboe | 956bcb7 | 2010-08-07 18:13:50 +0200 | [diff] [blame] | 428 | .show = queue_show_random, |
| 429 | .store = queue_store_random, |
Jens Axboe | e2e1a14 | 2010-06-09 10:42:09 +0200 | [diff] [blame] | 430 | }; |
| 431 | |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 432 | static struct attribute *default_attrs[] = { |
| 433 | &queue_requests_entry.attr, |
| 434 | &queue_ra_entry.attr, |
| 435 | &queue_max_hw_sectors_entry.attr, |
| 436 | &queue_max_sectors_entry.attr, |
Martin K. Petersen | c77a571 | 2010-03-10 00:48:33 -0500 | [diff] [blame] | 437 | &queue_max_segments_entry.attr, |
Martin K. Petersen | 13f05c8 | 2010-09-10 20:50:10 +0200 | [diff] [blame] | 438 | &queue_max_integrity_segments_entry.attr, |
Martin K. Petersen | c77a571 | 2010-03-10 00:48:33 -0500 | [diff] [blame] | 439 | &queue_max_segment_size_entry.attr, |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 440 | &queue_iosched_entry.attr, |
Martin K. Petersen | e68b903 | 2008-01-29 19:14:08 +0100 | [diff] [blame] | 441 | &queue_hw_sector_size_entry.attr, |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 442 | &queue_logical_block_size_entry.attr, |
Martin K. Petersen | c72758f | 2009-05-22 17:17:53 -0400 | [diff] [blame] | 443 | &queue_physical_block_size_entry.attr, |
| 444 | &queue_io_min_entry.attr, |
| 445 | &queue_io_opt_entry.attr, |
Martin K. Petersen | 86b3728 | 2009-11-10 11:50:21 +0100 | [diff] [blame] | 446 | &queue_discard_granularity_entry.attr, |
| 447 | &queue_discard_max_entry.attr, |
Martin K. Petersen | 98262f2 | 2009-12-03 09:24:48 +0100 | [diff] [blame] | 448 | &queue_discard_zeroes_data_entry.attr, |
Martin K. Petersen | 4363ac7 | 2012-09-18 12:19:27 -0400 | [diff] [blame] | 449 | &queue_write_same_max_entry.attr, |
Bartlomiej Zolnierkiewicz | 1308835f | 2009-01-07 12:22:39 +0100 | [diff] [blame] | 450 | &queue_nonrot_entry.attr, |
Alan D. Brunelle | ac9fafa | 2008-04-29 14:44:19 +0200 | [diff] [blame] | 451 | &queue_nomerges_entry.attr, |
Jens Axboe | c7c22e4 | 2008-09-13 20:26:01 +0200 | [diff] [blame] | 452 | &queue_rq_affinity_entry.attr, |
Jens Axboe | bc58ba9 | 2009-01-23 10:54:44 +0100 | [diff] [blame] | 453 | &queue_iostats_entry.attr, |
Jens Axboe | e2e1a14 | 2010-06-09 10:42:09 +0200 | [diff] [blame] | 454 | &queue_random_entry.attr, |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 455 | NULL, |
| 456 | }; |
| 457 | |
| 458 | #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr) |
| 459 | |
| 460 | static ssize_t |
| 461 | queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 462 | { |
| 463 | struct queue_sysfs_entry *entry = to_queue(attr); |
| 464 | struct request_queue *q = |
| 465 | container_of(kobj, struct request_queue, kobj); |
| 466 | ssize_t res; |
| 467 | |
| 468 | if (!entry->show) |
| 469 | return -EIO; |
| 470 | mutex_lock(&q->sysfs_lock); |
Bart Van Assche | 3f3299d | 2012-11-28 13:42:38 +0100 | [diff] [blame] | 471 | if (blk_queue_dying(q)) { |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 472 | mutex_unlock(&q->sysfs_lock); |
| 473 | return -ENOENT; |
| 474 | } |
| 475 | res = entry->show(q, page); |
| 476 | mutex_unlock(&q->sysfs_lock); |
| 477 | return res; |
| 478 | } |
| 479 | |
| 480 | static ssize_t |
| 481 | queue_attr_store(struct kobject *kobj, struct attribute *attr, |
| 482 | const char *page, size_t length) |
| 483 | { |
| 484 | struct queue_sysfs_entry *entry = to_queue(attr); |
Jens Axboe | 6728cb0 | 2008-01-31 13:03:55 +0100 | [diff] [blame] | 485 | struct request_queue *q; |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 486 | ssize_t res; |
| 487 | |
| 488 | if (!entry->store) |
| 489 | return -EIO; |
Jens Axboe | 6728cb0 | 2008-01-31 13:03:55 +0100 | [diff] [blame] | 490 | |
| 491 | q = container_of(kobj, struct request_queue, kobj); |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 492 | mutex_lock(&q->sysfs_lock); |
Bart Van Assche | 3f3299d | 2012-11-28 13:42:38 +0100 | [diff] [blame] | 493 | if (blk_queue_dying(q)) { |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 494 | mutex_unlock(&q->sysfs_lock); |
| 495 | return -ENOENT; |
| 496 | } |
| 497 | res = entry->store(q, page, length); |
| 498 | mutex_unlock(&q->sysfs_lock); |
| 499 | return res; |
| 500 | } |
| 501 | |
Tejun Heo | 548bc8e | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 502 | static void blk_free_queue_rcu(struct rcu_head *rcu_head) |
| 503 | { |
| 504 | struct request_queue *q = container_of(rcu_head, struct request_queue, |
| 505 | rcu_head); |
| 506 | kmem_cache_free(blk_requestq_cachep, q); |
| 507 | } |
| 508 | |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 509 | /** |
Andrew Morton | 499337b | 2011-09-21 10:01:22 +0200 | [diff] [blame] | 510 | * blk_release_queue: - release a &struct request_queue when it is no longer needed |
| 511 | * @kobj: the kobj belonging to the request queue to be released |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 512 | * |
| 513 | * Description: |
Andrew Morton | 499337b | 2011-09-21 10:01:22 +0200 | [diff] [blame] | 514 | * blk_release_queue is the pair to blk_init_queue() or |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 515 | * blk_queue_make_request(). It should be called when a request queue is |
| 516 | * being released; typically when a block device is being de-registered. |
| 517 | * Currently, its primary task it to free all the &struct request |
| 518 | * structures that were allocated to the queue and the queue itself. |
| 519 | * |
| 520 | * Caveat: |
| 521 | * Hopefully the low level driver will have finished any |
| 522 | * outstanding requests first... |
| 523 | **/ |
| 524 | static void blk_release_queue(struct kobject *kobj) |
| 525 | { |
| 526 | struct request_queue *q = |
| 527 | container_of(kobj, struct request_queue, kobj); |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 528 | |
| 529 | blk_sync_queue(q); |
| 530 | |
Tejun Heo | e8989fa | 2012-03-05 13:15:20 -0800 | [diff] [blame] | 531 | blkcg_exit_queue(q); |
| 532 | |
Tejun Heo | 7e5a879 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 533 | if (q->elevator) { |
| 534 | spin_lock_irq(q->queue_lock); |
| 535 | ioc_clear_queue(q); |
| 536 | spin_unlock_irq(q->queue_lock); |
Hannes Reinecke | 777eb1b | 2011-09-28 08:07:01 -0600 | [diff] [blame] | 537 | elevator_exit(q->elevator); |
Tejun Heo | 7e5a879 | 2011-12-14 00:33:42 +0100 | [diff] [blame] | 538 | } |
Hannes Reinecke | 777eb1b | 2011-09-28 08:07:01 -0600 | [diff] [blame] | 539 | |
Tejun Heo | a051661 | 2012-06-26 15:05:44 -0700 | [diff] [blame] | 540 | blk_exit_rl(&q->root_rl); |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 541 | |
| 542 | if (q->queue_tags) |
| 543 | __blk_queue_free_tags(q); |
| 544 | |
| 545 | blk_trace_shutdown(q); |
| 546 | |
| 547 | bdi_destroy(&q->backing_dev_info); |
Tejun Heo | a73f730 | 2011-12-14 00:33:37 +0100 | [diff] [blame] | 548 | |
| 549 | ida_simple_remove(&blk_queue_ida, q->id); |
Tejun Heo | 548bc8e | 2013-01-09 08:05:13 -0800 | [diff] [blame] | 550 | call_rcu(&q->rcu_head, blk_free_queue_rcu); |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 551 | } |
| 552 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 553 | static const struct sysfs_ops queue_sysfs_ops = { |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 554 | .show = queue_attr_show, |
| 555 | .store = queue_attr_store, |
| 556 | }; |
| 557 | |
| 558 | struct kobj_type blk_queue_ktype = { |
| 559 | .sysfs_ops = &queue_sysfs_ops, |
| 560 | .default_attrs = default_attrs, |
| 561 | .release = blk_release_queue, |
| 562 | }; |
| 563 | |
| 564 | int blk_register_queue(struct gendisk *disk) |
| 565 | { |
| 566 | int ret; |
Li Zefan | 1d54ad6 | 2009-04-14 14:00:05 +0800 | [diff] [blame] | 567 | struct device *dev = disk_to_dev(disk); |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 568 | struct request_queue *q = disk->queue; |
| 569 | |
Akinobu Mita | fb19974 | 2008-04-21 09:51:06 +0200 | [diff] [blame] | 570 | if (WARN_ON(!q)) |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 571 | return -ENXIO; |
| 572 | |
Tejun Heo | 749fefe | 2012-09-20 14:08:52 -0700 | [diff] [blame] | 573 | /* |
| 574 | * Initialization must be complete by now. Finish the initial |
| 575 | * bypass from queue allocation. |
| 576 | */ |
| 577 | blk_queue_bypass_end(q); |
| 578 | |
Li Zefan | 1d54ad6 | 2009-04-14 14:00:05 +0800 | [diff] [blame] | 579 | ret = blk_trace_init_sysfs(dev); |
| 580 | if (ret) |
| 581 | return ret; |
| 582 | |
Linus Torvalds | c905959 | 2009-06-11 10:52:27 -0700 | [diff] [blame] | 583 | ret = kobject_add(&q->kobj, kobject_get(&dev->kobj), "%s", "queue"); |
Liu Yuan | ed5302d | 2011-04-19 13:47:58 +0200 | [diff] [blame] | 584 | if (ret < 0) { |
| 585 | blk_trace_remove_sysfs(dev); |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 586 | return ret; |
Liu Yuan | ed5302d | 2011-04-19 13:47:58 +0200 | [diff] [blame] | 587 | } |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 588 | |
| 589 | kobject_uevent(&q->kobj, KOBJ_ADD); |
| 590 | |
Martin K. Petersen | cd43e26 | 2009-05-22 17:17:52 -0400 | [diff] [blame] | 591 | if (!q->request_fn) |
| 592 | return 0; |
| 593 | |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 594 | ret = elv_register_queue(q); |
| 595 | if (ret) { |
| 596 | kobject_uevent(&q->kobj, KOBJ_REMOVE); |
| 597 | kobject_del(&q->kobj); |
Liu Yuan | 80656b6 | 2011-04-13 22:14:54 +0200 | [diff] [blame] | 598 | blk_trace_remove_sysfs(dev); |
Xiaotian Feng | c87ffbb | 2010-08-23 12:30:29 +0200 | [diff] [blame] | 599 | kobject_put(&dev->kobj); |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 600 | return ret; |
| 601 | } |
| 602 | |
| 603 | return 0; |
| 604 | } |
| 605 | |
| 606 | void blk_unregister_queue(struct gendisk *disk) |
| 607 | { |
| 608 | struct request_queue *q = disk->queue; |
| 609 | |
Akinobu Mita | fb19974 | 2008-04-21 09:51:06 +0200 | [diff] [blame] | 610 | if (WARN_ON(!q)) |
| 611 | return; |
| 612 | |
Zdenek Kabelac | 48c0d4d | 2009-09-25 06:19:26 +0200 | [diff] [blame] | 613 | if (q->request_fn) |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 614 | elv_unregister_queue(q); |
| 615 | |
Zdenek Kabelac | 48c0d4d | 2009-09-25 06:19:26 +0200 | [diff] [blame] | 616 | kobject_uevent(&q->kobj, KOBJ_REMOVE); |
| 617 | kobject_del(&q->kobj); |
| 618 | blk_trace_remove_sysfs(disk_to_dev(disk)); |
| 619 | kobject_put(&disk_to_dev(disk)->kobj); |
Jens Axboe | 8324aa9 | 2008-01-29 14:51:59 +0100 | [diff] [blame] | 620 | } |