Commit | Line | Data |
---|---|---|
8324aa91 JA |
1 | /* |
2 | * Functions related to sysfs handling | |
3 | */ | |
4 | #include <linux/kernel.h> | |
5a0e3ad6 | 5 | #include <linux/slab.h> |
8324aa91 JA |
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 | |
9cb308ce | 20 | queue_var_show(unsigned long var, char *page) |
8324aa91 | 21 | { |
9cb308ce | 22 | return sprintf(page, "%lu\n", var); |
8324aa91 JA |
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; | |
b8a9ae77 JA |
44 | int ret; |
45 | ||
46 | if (!q->request_fn) | |
47 | return -EINVAL; | |
48 | ||
49 | ret = queue_var_store(&nr, page, count); | |
8324aa91 JA |
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 | ||
1faa16d2 JA |
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); | |
61 | ||
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); | |
66 | ||
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]); | |
8324aa91 JA |
72 | } |
73 | ||
1faa16d2 JA |
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]); | |
8324aa91 JA |
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 | { | |
9cb308ce XF |
86 | unsigned long ra_kb = q->backing_dev_info.ra_pages << |
87 | (PAGE_CACHE_SHIFT - 10); | |
8324aa91 JA |
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 | ||
8324aa91 | 98 | q->backing_dev_info.ra_pages = ra_kb >> (PAGE_CACHE_SHIFT - 10); |
8324aa91 JA |
99 | |
100 | return ret; | |
101 | } | |
102 | ||
103 | static ssize_t queue_max_sectors_show(struct request_queue *q, char *page) | |
104 | { | |
ae03bf63 | 105 | int max_sectors_kb = queue_max_sectors(q) >> 1; |
8324aa91 JA |
106 | |
107 | return queue_var_show(max_sectors_kb, (page)); | |
108 | } | |
109 | ||
e1defc4f | 110 | static ssize_t queue_logical_block_size_show(struct request_queue *q, char *page) |
e68b903c | 111 | { |
e1defc4f | 112 | return queue_var_show(queue_logical_block_size(q), page); |
e68b903c MP |
113 | } |
114 | ||
c72758f3 MP |
115 | static ssize_t queue_physical_block_size_show(struct request_queue *q, char *page) |
116 | { | |
117 | return queue_var_show(queue_physical_block_size(q), page); | |
118 | } | |
119 | ||
120 | static ssize_t queue_io_min_show(struct request_queue *q, char *page) | |
121 | { | |
122 | return queue_var_show(queue_io_min(q), page); | |
123 | } | |
124 | ||
125 | static ssize_t queue_io_opt_show(struct request_queue *q, char *page) | |
126 | { | |
127 | return queue_var_show(queue_io_opt(q), page); | |
e68b903c MP |
128 | } |
129 | ||
86b37281 MP |
130 | static ssize_t queue_discard_granularity_show(struct request_queue *q, char *page) |
131 | { | |
132 | return queue_var_show(q->limits.discard_granularity, page); | |
133 | } | |
134 | ||
135 | static ssize_t queue_discard_max_show(struct request_queue *q, char *page) | |
136 | { | |
137 | return queue_var_show(q->limits.max_discard_sectors << 9, page); | |
138 | } | |
139 | ||
98262f27 MP |
140 | static ssize_t queue_discard_zeroes_data_show(struct request_queue *q, char *page) |
141 | { | |
142 | return queue_var_show(queue_discard_zeroes_data(q), page); | |
143 | } | |
144 | ||
8324aa91 JA |
145 | static ssize_t |
146 | queue_max_sectors_store(struct request_queue *q, const char *page, size_t count) | |
147 | { | |
148 | unsigned long max_sectors_kb, | |
ae03bf63 | 149 | max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1, |
8324aa91 JA |
150 | page_kb = 1 << (PAGE_CACHE_SHIFT - 10); |
151 | ssize_t ret = queue_var_store(&max_sectors_kb, page, count); | |
152 | ||
153 | if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb) | |
154 | return -EINVAL; | |
7c239517 | 155 | |
8324aa91 | 156 | spin_lock_irq(q->queue_lock); |
c295fc05 | 157 | q->limits.max_sectors = max_sectors_kb << 1; |
8324aa91 JA |
158 | spin_unlock_irq(q->queue_lock); |
159 | ||
160 | return ret; | |
161 | } | |
162 | ||
163 | static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page) | |
164 | { | |
ae03bf63 | 165 | int max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1; |
8324aa91 JA |
166 | |
167 | return queue_var_show(max_hw_sectors_kb, (page)); | |
168 | } | |
169 | ||
1308835f BZ |
170 | static ssize_t queue_nonrot_show(struct request_queue *q, char *page) |
171 | { | |
172 | return queue_var_show(!blk_queue_nonrot(q), page); | |
173 | } | |
174 | ||
175 | static ssize_t queue_nonrot_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 | ||
181 | spin_lock_irq(q->queue_lock); | |
182 | if (nm) | |
183 | queue_flag_clear(QUEUE_FLAG_NONROT, q); | |
184 | else | |
185 | queue_flag_set(QUEUE_FLAG_NONROT, q); | |
186 | spin_unlock_irq(q->queue_lock); | |
187 | ||
188 | return ret; | |
189 | } | |
190 | ||
ac9fafa1 AB |
191 | static ssize_t queue_nomerges_show(struct request_queue *q, char *page) |
192 | { | |
488991e2 AB |
193 | return queue_var_show((blk_queue_nomerges(q) << 1) | |
194 | blk_queue_noxmerges(q), page); | |
ac9fafa1 AB |
195 | } |
196 | ||
197 | static ssize_t queue_nomerges_store(struct request_queue *q, const char *page, | |
198 | size_t count) | |
199 | { | |
200 | unsigned long nm; | |
201 | ssize_t ret = queue_var_store(&nm, page, count); | |
202 | ||
bf0f9702 | 203 | spin_lock_irq(q->queue_lock); |
488991e2 AB |
204 | queue_flag_clear(QUEUE_FLAG_NOMERGES, q); |
205 | queue_flag_clear(QUEUE_FLAG_NOXMERGES, q); | |
206 | if (nm == 2) | |
bf0f9702 | 207 | queue_flag_set(QUEUE_FLAG_NOMERGES, q); |
488991e2 AB |
208 | else if (nm) |
209 | queue_flag_set(QUEUE_FLAG_NOXMERGES, q); | |
bf0f9702 | 210 | spin_unlock_irq(q->queue_lock); |
1308835f | 211 | |
ac9fafa1 AB |
212 | return ret; |
213 | } | |
214 | ||
c7c22e4d JA |
215 | static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page) |
216 | { | |
9cb308ce | 217 | bool set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags); |
c7c22e4d | 218 | |
9cb308ce | 219 | return queue_var_show(set, page); |
c7c22e4d JA |
220 | } |
221 | ||
222 | static ssize_t | |
223 | queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count) | |
224 | { | |
225 | ssize_t ret = -EINVAL; | |
226 | #if defined(CONFIG_USE_GENERIC_SMP_HELPERS) | |
227 | unsigned long val; | |
228 | ||
229 | ret = queue_var_store(&val, page, count); | |
230 | spin_lock_irq(q->queue_lock); | |
231 | if (val) | |
232 | queue_flag_set(QUEUE_FLAG_SAME_COMP, q); | |
233 | else | |
234 | queue_flag_clear(QUEUE_FLAG_SAME_COMP, q); | |
235 | spin_unlock_irq(q->queue_lock); | |
236 | #endif | |
237 | return ret; | |
238 | } | |
8324aa91 | 239 | |
bc58ba94 JA |
240 | static ssize_t queue_iostats_show(struct request_queue *q, char *page) |
241 | { | |
242 | return queue_var_show(blk_queue_io_stat(q), page); | |
243 | } | |
244 | ||
245 | static ssize_t queue_iostats_store(struct request_queue *q, const char *page, | |
246 | size_t count) | |
247 | { | |
248 | unsigned long stats; | |
249 | ssize_t ret = queue_var_store(&stats, page, count); | |
250 | ||
251 | spin_lock_irq(q->queue_lock); | |
252 | if (stats) | |
253 | queue_flag_set(QUEUE_FLAG_IO_STAT, q); | |
254 | else | |
255 | queue_flag_clear(QUEUE_FLAG_IO_STAT, q); | |
256 | spin_unlock_irq(q->queue_lock); | |
257 | ||
258 | return ret; | |
259 | } | |
260 | ||
8324aa91 JA |
261 | static struct queue_sysfs_entry queue_requests_entry = { |
262 | .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR }, | |
263 | .show = queue_requests_show, | |
264 | .store = queue_requests_store, | |
265 | }; | |
266 | ||
267 | static struct queue_sysfs_entry queue_ra_entry = { | |
268 | .attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR }, | |
269 | .show = queue_ra_show, | |
270 | .store = queue_ra_store, | |
271 | }; | |
272 | ||
273 | static struct queue_sysfs_entry queue_max_sectors_entry = { | |
274 | .attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR }, | |
275 | .show = queue_max_sectors_show, | |
276 | .store = queue_max_sectors_store, | |
277 | }; | |
278 | ||
279 | static struct queue_sysfs_entry queue_max_hw_sectors_entry = { | |
280 | .attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO }, | |
281 | .show = queue_max_hw_sectors_show, | |
282 | }; | |
283 | ||
284 | static struct queue_sysfs_entry queue_iosched_entry = { | |
285 | .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR }, | |
286 | .show = elv_iosched_show, | |
287 | .store = elv_iosched_store, | |
288 | }; | |
289 | ||
e68b903c MP |
290 | static struct queue_sysfs_entry queue_hw_sector_size_entry = { |
291 | .attr = {.name = "hw_sector_size", .mode = S_IRUGO }, | |
e1defc4f MP |
292 | .show = queue_logical_block_size_show, |
293 | }; | |
294 | ||
295 | static struct queue_sysfs_entry queue_logical_block_size_entry = { | |
296 | .attr = {.name = "logical_block_size", .mode = S_IRUGO }, | |
297 | .show = queue_logical_block_size_show, | |
e68b903c MP |
298 | }; |
299 | ||
c72758f3 MP |
300 | static struct queue_sysfs_entry queue_physical_block_size_entry = { |
301 | .attr = {.name = "physical_block_size", .mode = S_IRUGO }, | |
302 | .show = queue_physical_block_size_show, | |
303 | }; | |
304 | ||
305 | static struct queue_sysfs_entry queue_io_min_entry = { | |
306 | .attr = {.name = "minimum_io_size", .mode = S_IRUGO }, | |
307 | .show = queue_io_min_show, | |
308 | }; | |
309 | ||
310 | static struct queue_sysfs_entry queue_io_opt_entry = { | |
311 | .attr = {.name = "optimal_io_size", .mode = S_IRUGO }, | |
312 | .show = queue_io_opt_show, | |
e68b903c MP |
313 | }; |
314 | ||
86b37281 MP |
315 | static struct queue_sysfs_entry queue_discard_granularity_entry = { |
316 | .attr = {.name = "discard_granularity", .mode = S_IRUGO }, | |
317 | .show = queue_discard_granularity_show, | |
318 | }; | |
319 | ||
320 | static struct queue_sysfs_entry queue_discard_max_entry = { | |
321 | .attr = {.name = "discard_max_bytes", .mode = S_IRUGO }, | |
322 | .show = queue_discard_max_show, | |
323 | }; | |
324 | ||
98262f27 MP |
325 | static struct queue_sysfs_entry queue_discard_zeroes_data_entry = { |
326 | .attr = {.name = "discard_zeroes_data", .mode = S_IRUGO }, | |
327 | .show = queue_discard_zeroes_data_show, | |
328 | }; | |
329 | ||
1308835f BZ |
330 | static struct queue_sysfs_entry queue_nonrot_entry = { |
331 | .attr = {.name = "rotational", .mode = S_IRUGO | S_IWUSR }, | |
332 | .show = queue_nonrot_show, | |
333 | .store = queue_nonrot_store, | |
334 | }; | |
335 | ||
ac9fafa1 AB |
336 | static struct queue_sysfs_entry queue_nomerges_entry = { |
337 | .attr = {.name = "nomerges", .mode = S_IRUGO | S_IWUSR }, | |
338 | .show = queue_nomerges_show, | |
339 | .store = queue_nomerges_store, | |
340 | }; | |
341 | ||
c7c22e4d JA |
342 | static struct queue_sysfs_entry queue_rq_affinity_entry = { |
343 | .attr = {.name = "rq_affinity", .mode = S_IRUGO | S_IWUSR }, | |
344 | .show = queue_rq_affinity_show, | |
345 | .store = queue_rq_affinity_store, | |
346 | }; | |
347 | ||
bc58ba94 JA |
348 | static struct queue_sysfs_entry queue_iostats_entry = { |
349 | .attr = {.name = "iostats", .mode = S_IRUGO | S_IWUSR }, | |
350 | .show = queue_iostats_show, | |
351 | .store = queue_iostats_store, | |
352 | }; | |
353 | ||
8324aa91 JA |
354 | static struct attribute *default_attrs[] = { |
355 | &queue_requests_entry.attr, | |
356 | &queue_ra_entry.attr, | |
357 | &queue_max_hw_sectors_entry.attr, | |
358 | &queue_max_sectors_entry.attr, | |
359 | &queue_iosched_entry.attr, | |
e68b903c | 360 | &queue_hw_sector_size_entry.attr, |
e1defc4f | 361 | &queue_logical_block_size_entry.attr, |
c72758f3 MP |
362 | &queue_physical_block_size_entry.attr, |
363 | &queue_io_min_entry.attr, | |
364 | &queue_io_opt_entry.attr, | |
86b37281 MP |
365 | &queue_discard_granularity_entry.attr, |
366 | &queue_discard_max_entry.attr, | |
98262f27 | 367 | &queue_discard_zeroes_data_entry.attr, |
1308835f | 368 | &queue_nonrot_entry.attr, |
ac9fafa1 | 369 | &queue_nomerges_entry.attr, |
c7c22e4d | 370 | &queue_rq_affinity_entry.attr, |
bc58ba94 | 371 | &queue_iostats_entry.attr, |
8324aa91 JA |
372 | NULL, |
373 | }; | |
374 | ||
375 | #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr) | |
376 | ||
377 | static ssize_t | |
378 | queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page) | |
379 | { | |
380 | struct queue_sysfs_entry *entry = to_queue(attr); | |
381 | struct request_queue *q = | |
382 | container_of(kobj, struct request_queue, kobj); | |
383 | ssize_t res; | |
384 | ||
385 | if (!entry->show) | |
386 | return -EIO; | |
387 | mutex_lock(&q->sysfs_lock); | |
388 | if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) { | |
389 | mutex_unlock(&q->sysfs_lock); | |
390 | return -ENOENT; | |
391 | } | |
392 | res = entry->show(q, page); | |
393 | mutex_unlock(&q->sysfs_lock); | |
394 | return res; | |
395 | } | |
396 | ||
397 | static ssize_t | |
398 | queue_attr_store(struct kobject *kobj, struct attribute *attr, | |
399 | const char *page, size_t length) | |
400 | { | |
401 | struct queue_sysfs_entry *entry = to_queue(attr); | |
6728cb0e | 402 | struct request_queue *q; |
8324aa91 JA |
403 | ssize_t res; |
404 | ||
405 | if (!entry->store) | |
406 | return -EIO; | |
6728cb0e JA |
407 | |
408 | q = container_of(kobj, struct request_queue, kobj); | |
8324aa91 JA |
409 | mutex_lock(&q->sysfs_lock); |
410 | if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) { | |
411 | mutex_unlock(&q->sysfs_lock); | |
412 | return -ENOENT; | |
413 | } | |
414 | res = entry->store(q, page, length); | |
415 | mutex_unlock(&q->sysfs_lock); | |
416 | return res; | |
417 | } | |
418 | ||
419 | /** | |
420 | * blk_cleanup_queue: - release a &struct request_queue when it is no longer needed | |
421 | * @kobj: the kobj belonging of the request queue to be released | |
422 | * | |
423 | * Description: | |
424 | * blk_cleanup_queue is the pair to blk_init_queue() or | |
425 | * blk_queue_make_request(). It should be called when a request queue is | |
426 | * being released; typically when a block device is being de-registered. | |
427 | * Currently, its primary task it to free all the &struct request | |
428 | * structures that were allocated to the queue and the queue itself. | |
429 | * | |
430 | * Caveat: | |
431 | * Hopefully the low level driver will have finished any | |
432 | * outstanding requests first... | |
433 | **/ | |
434 | static void blk_release_queue(struct kobject *kobj) | |
435 | { | |
436 | struct request_queue *q = | |
437 | container_of(kobj, struct request_queue, kobj); | |
438 | struct request_list *rl = &q->rq; | |
439 | ||
440 | blk_sync_queue(q); | |
441 | ||
442 | if (rl->rq_pool) | |
443 | mempool_destroy(rl->rq_pool); | |
444 | ||
445 | if (q->queue_tags) | |
446 | __blk_queue_free_tags(q); | |
447 | ||
448 | blk_trace_shutdown(q); | |
449 | ||
450 | bdi_destroy(&q->backing_dev_info); | |
451 | kmem_cache_free(blk_requestq_cachep, q); | |
452 | } | |
453 | ||
52cf25d0 | 454 | static const struct sysfs_ops queue_sysfs_ops = { |
8324aa91 JA |
455 | .show = queue_attr_show, |
456 | .store = queue_attr_store, | |
457 | }; | |
458 | ||
459 | struct kobj_type blk_queue_ktype = { | |
460 | .sysfs_ops = &queue_sysfs_ops, | |
461 | .default_attrs = default_attrs, | |
462 | .release = blk_release_queue, | |
463 | }; | |
464 | ||
465 | int blk_register_queue(struct gendisk *disk) | |
466 | { | |
467 | int ret; | |
1d54ad6d | 468 | struct device *dev = disk_to_dev(disk); |
8324aa91 JA |
469 | |
470 | struct request_queue *q = disk->queue; | |
471 | ||
fb199746 | 472 | if (WARN_ON(!q)) |
8324aa91 JA |
473 | return -ENXIO; |
474 | ||
1d54ad6d LZ |
475 | ret = blk_trace_init_sysfs(dev); |
476 | if (ret) | |
477 | return ret; | |
478 | ||
c9059598 | 479 | ret = kobject_add(&q->kobj, kobject_get(&dev->kobj), "%s", "queue"); |
8324aa91 JA |
480 | if (ret < 0) |
481 | return ret; | |
482 | ||
483 | kobject_uevent(&q->kobj, KOBJ_ADD); | |
484 | ||
cd43e26f MP |
485 | if (!q->request_fn) |
486 | return 0; | |
487 | ||
8324aa91 JA |
488 | ret = elv_register_queue(q); |
489 | if (ret) { | |
490 | kobject_uevent(&q->kobj, KOBJ_REMOVE); | |
491 | kobject_del(&q->kobj); | |
48c0d4d4 | 492 | blk_trace_remove_sysfs(disk_to_dev(disk)); |
8324aa91 JA |
493 | return ret; |
494 | } | |
495 | ||
496 | return 0; | |
497 | } | |
498 | ||
499 | void blk_unregister_queue(struct gendisk *disk) | |
500 | { | |
501 | struct request_queue *q = disk->queue; | |
502 | ||
fb199746 AM |
503 | if (WARN_ON(!q)) |
504 | return; | |
505 | ||
48c0d4d4 | 506 | if (q->request_fn) |
8324aa91 JA |
507 | elv_unregister_queue(q); |
508 | ||
48c0d4d4 ZK |
509 | kobject_uevent(&q->kobj, KOBJ_REMOVE); |
510 | kobject_del(&q->kobj); | |
511 | blk_trace_remove_sysfs(disk_to_dev(disk)); | |
512 | kobject_put(&disk_to_dev(disk)->kobj); | |
8324aa91 | 513 | } |