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