Linux 3.0.74
[linux/fpc-iii.git] / block / blk-sysfs.c
blobcb0f1a093e1300ee57da3790d9f6a15b63d41aef
1 /*
2 * Functions related to sysfs handling
3 */
4 #include <linux/kernel.h>
5 #include <linux/slab.h>
6 #include <linux/module.h>
7 #include <linux/bio.h>
8 #include <linux/blkdev.h>
9 #include <linux/blktrace_api.h>
11 #include "blk.h"
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);
19 static ssize_t
20 queue_var_show(unsigned long var, char *page)
22 return sprintf(page, "%lu\n", var);
25 static ssize_t
26 queue_var_store(unsigned long *var, const char *page, size_t count)
28 char *p = (char *) page;
30 *var = simple_strtoul(p, &p, 10);
31 return count;
34 static ssize_t queue_requests_show(struct request_queue *q, char *page)
36 return queue_var_show(q->nr_requests, (page));
39 static ssize_t
40 queue_requests_store(struct request_queue *q, const char *page, size_t count)
42 struct request_list *rl = &q->rq;
43 unsigned long nr;
44 int ret;
46 if (!q->request_fn)
47 return -EINVAL;
49 ret = queue_var_store(&nr, page, count);
50 if (nr < BLKDEV_MIN_RQ)
51 nr = BLKDEV_MIN_RQ;
53 spin_lock_irq(q->queue_lock);
54 q->nr_requests = nr;
55 blk_queue_congestion_threshold(q);
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);
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);
67 if (rl->count[BLK_RW_SYNC] >= q->nr_requests) {
68 blk_set_queue_full(q, BLK_RW_SYNC);
69 } else {
70 blk_clear_queue_full(q, BLK_RW_SYNC);
71 wake_up(&rl->wait[BLK_RW_SYNC]);
74 if (rl->count[BLK_RW_ASYNC] >= q->nr_requests) {
75 blk_set_queue_full(q, BLK_RW_ASYNC);
76 } else {
77 blk_clear_queue_full(q, BLK_RW_ASYNC);
78 wake_up(&rl->wait[BLK_RW_ASYNC]);
80 spin_unlock_irq(q->queue_lock);
81 return ret;
84 static ssize_t queue_ra_show(struct request_queue *q, char *page)
86 unsigned long ra_kb = q->backing_dev_info.ra_pages <<
87 (PAGE_CACHE_SHIFT - 10);
89 return queue_var_show(ra_kb, (page));
92 static ssize_t
93 queue_ra_store(struct request_queue *q, const char *page, size_t count)
95 unsigned long ra_kb;
96 ssize_t ret = queue_var_store(&ra_kb, page, count);
98 q->backing_dev_info.ra_pages = ra_kb >> (PAGE_CACHE_SHIFT - 10);
100 return ret;
103 static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
105 int max_sectors_kb = queue_max_sectors(q) >> 1;
107 return queue_var_show(max_sectors_kb, (page));
110 static ssize_t queue_max_segments_show(struct request_queue *q, char *page)
112 return queue_var_show(queue_max_segments(q), (page));
115 static ssize_t queue_max_integrity_segments_show(struct request_queue *q, char *page)
117 return queue_var_show(q->limits.max_integrity_segments, (page));
120 static ssize_t queue_max_segment_size_show(struct request_queue *q, char *page)
122 if (blk_queue_cluster(q))
123 return queue_var_show(queue_max_segment_size(q), (page));
125 return queue_var_show(PAGE_CACHE_SIZE, (page));
128 static ssize_t queue_logical_block_size_show(struct request_queue *q, char *page)
130 return queue_var_show(queue_logical_block_size(q), page);
133 static ssize_t queue_physical_block_size_show(struct request_queue *q, char *page)
135 return queue_var_show(queue_physical_block_size(q), page);
138 static ssize_t queue_io_min_show(struct request_queue *q, char *page)
140 return queue_var_show(queue_io_min(q), page);
143 static ssize_t queue_io_opt_show(struct request_queue *q, char *page)
145 return queue_var_show(queue_io_opt(q), page);
148 static ssize_t queue_discard_granularity_show(struct request_queue *q, char *page)
150 return queue_var_show(q->limits.discard_granularity, page);
153 static ssize_t queue_discard_max_show(struct request_queue *q, char *page)
155 return sprintf(page, "%llu\n",
156 (unsigned long long)q->limits.max_discard_sectors << 9);
159 static ssize_t queue_discard_zeroes_data_show(struct request_queue *q, char *page)
161 return queue_var_show(queue_discard_zeroes_data(q), page);
164 static ssize_t
165 queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
167 unsigned long max_sectors_kb,
168 max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1,
169 page_kb = 1 << (PAGE_CACHE_SHIFT - 10);
170 ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
172 if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
173 return -EINVAL;
175 spin_lock_irq(q->queue_lock);
176 q->limits.max_sectors = max_sectors_kb << 1;
177 spin_unlock_irq(q->queue_lock);
179 return ret;
182 static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
184 int max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1;
186 return queue_var_show(max_hw_sectors_kb, (page));
189 #define QUEUE_SYSFS_BIT_FNS(name, flag, neg) \
190 static ssize_t \
191 queue_show_##name(struct request_queue *q, char *page) \
193 int bit; \
194 bit = test_bit(QUEUE_FLAG_##flag, &q->queue_flags); \
195 return queue_var_show(neg ? !bit : bit, page); \
197 static ssize_t \
198 queue_store_##name(struct request_queue *q, const char *page, size_t count) \
200 unsigned long val; \
201 ssize_t ret; \
202 ret = queue_var_store(&val, page, count); \
203 if (ret < 0) \
204 return ret; \
205 if (neg) \
206 val = !val; \
208 spin_lock_irq(q->queue_lock); \
209 if (val) \
210 queue_flag_set(QUEUE_FLAG_##flag, q); \
211 else \
212 queue_flag_clear(QUEUE_FLAG_##flag, q); \
213 spin_unlock_irq(q->queue_lock); \
214 return ret; \
217 QUEUE_SYSFS_BIT_FNS(nonrot, NONROT, 1);
218 QUEUE_SYSFS_BIT_FNS(random, ADD_RANDOM, 0);
219 QUEUE_SYSFS_BIT_FNS(iostats, IO_STAT, 0);
220 #undef QUEUE_SYSFS_BIT_FNS
222 static ssize_t queue_nomerges_show(struct request_queue *q, char *page)
224 return queue_var_show((blk_queue_nomerges(q) << 1) |
225 blk_queue_noxmerges(q), page);
228 static ssize_t queue_nomerges_store(struct request_queue *q, const char *page,
229 size_t count)
231 unsigned long nm;
232 ssize_t ret = queue_var_store(&nm, page, count);
234 spin_lock_irq(q->queue_lock);
235 queue_flag_clear(QUEUE_FLAG_NOMERGES, q);
236 queue_flag_clear(QUEUE_FLAG_NOXMERGES, q);
237 if (nm == 2)
238 queue_flag_set(QUEUE_FLAG_NOMERGES, q);
239 else if (nm)
240 queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
241 spin_unlock_irq(q->queue_lock);
243 return ret;
246 static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page)
248 bool set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags);
250 return queue_var_show(set, page);
253 static ssize_t
254 queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count)
256 ssize_t ret = -EINVAL;
257 #if defined(CONFIG_USE_GENERIC_SMP_HELPERS)
258 unsigned long val;
260 ret = queue_var_store(&val, page, count);
261 spin_lock_irq(q->queue_lock);
262 if (val)
263 queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
264 else
265 queue_flag_clear(QUEUE_FLAG_SAME_COMP, q);
266 spin_unlock_irq(q->queue_lock);
267 #endif
268 return ret;
271 static struct queue_sysfs_entry queue_requests_entry = {
272 .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
273 .show = queue_requests_show,
274 .store = queue_requests_store,
277 static struct queue_sysfs_entry queue_ra_entry = {
278 .attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR },
279 .show = queue_ra_show,
280 .store = queue_ra_store,
283 static struct queue_sysfs_entry queue_max_sectors_entry = {
284 .attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR },
285 .show = queue_max_sectors_show,
286 .store = queue_max_sectors_store,
289 static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
290 .attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO },
291 .show = queue_max_hw_sectors_show,
294 static struct queue_sysfs_entry queue_max_segments_entry = {
295 .attr = {.name = "max_segments", .mode = S_IRUGO },
296 .show = queue_max_segments_show,
299 static struct queue_sysfs_entry queue_max_integrity_segments_entry = {
300 .attr = {.name = "max_integrity_segments", .mode = S_IRUGO },
301 .show = queue_max_integrity_segments_show,
304 static struct queue_sysfs_entry queue_max_segment_size_entry = {
305 .attr = {.name = "max_segment_size", .mode = S_IRUGO },
306 .show = queue_max_segment_size_show,
309 static struct queue_sysfs_entry queue_iosched_entry = {
310 .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR },
311 .show = elv_iosched_show,
312 .store = elv_iosched_store,
315 static struct queue_sysfs_entry queue_hw_sector_size_entry = {
316 .attr = {.name = "hw_sector_size", .mode = S_IRUGO },
317 .show = queue_logical_block_size_show,
320 static struct queue_sysfs_entry queue_logical_block_size_entry = {
321 .attr = {.name = "logical_block_size", .mode = S_IRUGO },
322 .show = queue_logical_block_size_show,
325 static struct queue_sysfs_entry queue_physical_block_size_entry = {
326 .attr = {.name = "physical_block_size", .mode = S_IRUGO },
327 .show = queue_physical_block_size_show,
330 static struct queue_sysfs_entry queue_io_min_entry = {
331 .attr = {.name = "minimum_io_size", .mode = S_IRUGO },
332 .show = queue_io_min_show,
335 static struct queue_sysfs_entry queue_io_opt_entry = {
336 .attr = {.name = "optimal_io_size", .mode = S_IRUGO },
337 .show = queue_io_opt_show,
340 static struct queue_sysfs_entry queue_discard_granularity_entry = {
341 .attr = {.name = "discard_granularity", .mode = S_IRUGO },
342 .show = queue_discard_granularity_show,
345 static struct queue_sysfs_entry queue_discard_max_entry = {
346 .attr = {.name = "discard_max_bytes", .mode = S_IRUGO },
347 .show = queue_discard_max_show,
350 static struct queue_sysfs_entry queue_discard_zeroes_data_entry = {
351 .attr = {.name = "discard_zeroes_data", .mode = S_IRUGO },
352 .show = queue_discard_zeroes_data_show,
355 static struct queue_sysfs_entry queue_nonrot_entry = {
356 .attr = {.name = "rotational", .mode = S_IRUGO | S_IWUSR },
357 .show = queue_show_nonrot,
358 .store = queue_store_nonrot,
361 static struct queue_sysfs_entry queue_nomerges_entry = {
362 .attr = {.name = "nomerges", .mode = S_IRUGO | S_IWUSR },
363 .show = queue_nomerges_show,
364 .store = queue_nomerges_store,
367 static struct queue_sysfs_entry queue_rq_affinity_entry = {
368 .attr = {.name = "rq_affinity", .mode = S_IRUGO | S_IWUSR },
369 .show = queue_rq_affinity_show,
370 .store = queue_rq_affinity_store,
373 static struct queue_sysfs_entry queue_iostats_entry = {
374 .attr = {.name = "iostats", .mode = S_IRUGO | S_IWUSR },
375 .show = queue_show_iostats,
376 .store = queue_store_iostats,
379 static struct queue_sysfs_entry queue_random_entry = {
380 .attr = {.name = "add_random", .mode = S_IRUGO | S_IWUSR },
381 .show = queue_show_random,
382 .store = queue_store_random,
385 static struct attribute *default_attrs[] = {
386 &queue_requests_entry.attr,
387 &queue_ra_entry.attr,
388 &queue_max_hw_sectors_entry.attr,
389 &queue_max_sectors_entry.attr,
390 &queue_max_segments_entry.attr,
391 &queue_max_integrity_segments_entry.attr,
392 &queue_max_segment_size_entry.attr,
393 &queue_iosched_entry.attr,
394 &queue_hw_sector_size_entry.attr,
395 &queue_logical_block_size_entry.attr,
396 &queue_physical_block_size_entry.attr,
397 &queue_io_min_entry.attr,
398 &queue_io_opt_entry.attr,
399 &queue_discard_granularity_entry.attr,
400 &queue_discard_max_entry.attr,
401 &queue_discard_zeroes_data_entry.attr,
402 &queue_nonrot_entry.attr,
403 &queue_nomerges_entry.attr,
404 &queue_rq_affinity_entry.attr,
405 &queue_iostats_entry.attr,
406 &queue_random_entry.attr,
407 NULL,
410 #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
412 static ssize_t
413 queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
415 struct queue_sysfs_entry *entry = to_queue(attr);
416 struct request_queue *q =
417 container_of(kobj, struct request_queue, kobj);
418 ssize_t res;
420 if (!entry->show)
421 return -EIO;
422 mutex_lock(&q->sysfs_lock);
423 if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) {
424 mutex_unlock(&q->sysfs_lock);
425 return -ENOENT;
427 res = entry->show(q, page);
428 mutex_unlock(&q->sysfs_lock);
429 return res;
432 static ssize_t
433 queue_attr_store(struct kobject *kobj, struct attribute *attr,
434 const char *page, size_t length)
436 struct queue_sysfs_entry *entry = to_queue(attr);
437 struct request_queue *q;
438 ssize_t res;
440 if (!entry->store)
441 return -EIO;
443 q = container_of(kobj, struct request_queue, kobj);
444 mutex_lock(&q->sysfs_lock);
445 if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) {
446 mutex_unlock(&q->sysfs_lock);
447 return -ENOENT;
449 res = entry->store(q, page, length);
450 mutex_unlock(&q->sysfs_lock);
451 return res;
455 * blk_cleanup_queue: - release a &struct request_queue when it is no longer needed
456 * @kobj: the kobj belonging of the request queue to be released
458 * Description:
459 * blk_cleanup_queue is the pair to blk_init_queue() or
460 * blk_queue_make_request(). It should be called when a request queue is
461 * being released; typically when a block device is being de-registered.
462 * Currently, its primary task it to free all the &struct request
463 * structures that were allocated to the queue and the queue itself.
465 * Caveat:
466 * Hopefully the low level driver will have finished any
467 * outstanding requests first...
469 static void blk_release_queue(struct kobject *kobj)
471 struct request_queue *q =
472 container_of(kobj, struct request_queue, kobj);
473 struct request_list *rl = &q->rq;
475 blk_sync_queue(q);
477 if (q->elevator)
478 elevator_exit(q->elevator);
480 blk_throtl_exit(q);
482 if (rl->rq_pool)
483 mempool_destroy(rl->rq_pool);
485 if (q->queue_tags)
486 __blk_queue_free_tags(q);
488 blk_trace_shutdown(q);
490 bdi_destroy(&q->backing_dev_info);
491 kmem_cache_free(blk_requestq_cachep, q);
494 static const struct sysfs_ops queue_sysfs_ops = {
495 .show = queue_attr_show,
496 .store = queue_attr_store,
499 struct kobj_type blk_queue_ktype = {
500 .sysfs_ops = &queue_sysfs_ops,
501 .default_attrs = default_attrs,
502 .release = blk_release_queue,
505 int blk_register_queue(struct gendisk *disk)
507 int ret;
508 struct device *dev = disk_to_dev(disk);
509 struct request_queue *q = disk->queue;
511 if (WARN_ON(!q))
512 return -ENXIO;
514 ret = blk_trace_init_sysfs(dev);
515 if (ret)
516 return ret;
518 ret = kobject_add(&q->kobj, kobject_get(&dev->kobj), "%s", "queue");
519 if (ret < 0) {
520 blk_trace_remove_sysfs(dev);
521 return ret;
524 kobject_uevent(&q->kobj, KOBJ_ADD);
526 if (!q->request_fn)
527 return 0;
529 ret = elv_register_queue(q);
530 if (ret) {
531 kobject_uevent(&q->kobj, KOBJ_REMOVE);
532 kobject_del(&q->kobj);
533 blk_trace_remove_sysfs(dev);
534 kobject_put(&dev->kobj);
535 return ret;
538 return 0;
541 void blk_unregister_queue(struct gendisk *disk)
543 struct request_queue *q = disk->queue;
545 if (WARN_ON(!q))
546 return;
548 if (q->request_fn)
549 elv_unregister_queue(q);
551 kobject_uevent(&q->kobj, KOBJ_REMOVE);
552 kobject_del(&q->kobj);
553 blk_trace_remove_sysfs(disk_to_dev(disk));
554 kobject_put(&disk_to_dev(disk)->kobj);