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