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