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