2 * Functions related to sysfs handling
4 #include <linux/kernel.h>
5 #include <linux/module.h>
7 #include <linux/blkdev.h>
8 #include <linux/blktrace_api.h>
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);
19 queue_var_show(unsigned long var, char *page)
21 return sprintf(page, "%lu\n", var);
25 queue_var_store(unsigned long *var, const char *page, size_t count)
27 char *p = (char *) page;
29 *var = simple_strtoul(p, &p, 10);
33 static ssize_t queue_requests_show(struct request_queue *q, char *page)
35 return queue_var_show(q->nr_requests, (page));
39 queue_requests_store(struct request_queue *q, const char *page, size_t count)
41 struct request_list *rl = &q->rq;
48 ret = queue_var_store(&nr, page, count);
49 if (nr < BLKDEV_MIN_RQ)
52 spin_lock_irq(q->queue_lock);
54 blk_queue_congestion_threshold(q);
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);
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);
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]);
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]);
79 spin_unlock_irq(q->queue_lock);
83 static ssize_t queue_ra_show(struct request_queue *q, char *page)
85 unsigned long ra_kb = q->backing_dev_info.ra_pages <<
86 (PAGE_CACHE_SHIFT - 10);
88 return queue_var_show(ra_kb, (page));
92 queue_ra_store(struct request_queue *q, const char *page, size_t count)
95 ssize_t ret = queue_var_store(&ra_kb, page, count);
97 q->backing_dev_info.ra_pages = ra_kb >> (PAGE_CACHE_SHIFT - 10);
102 static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
104 int max_sectors_kb = queue_max_sectors(q) >> 1;
106 return queue_var_show(max_sectors_kb, (page));
109 static ssize_t queue_max_segments_show(struct request_queue *q, char *page)
111 return queue_var_show(queue_max_segments(q), (page));
114 static ssize_t queue_max_segment_size_show(struct request_queue *q, char *page)
116 if (test_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags))
117 return queue_var_show(queue_max_segment_size(q), (page));
119 return queue_var_show(PAGE_CACHE_SIZE, (page));
122 static ssize_t queue_logical_block_size_show(struct request_queue *q, char *page)
124 return queue_var_show(queue_logical_block_size(q), page);
127 static ssize_t queue_physical_block_size_show(struct request_queue *q, char *page)
129 return queue_var_show(queue_physical_block_size(q), page);
132 static ssize_t queue_io_min_show(struct request_queue *q, char *page)
134 return queue_var_show(queue_io_min(q), page);
137 static ssize_t queue_io_opt_show(struct request_queue *q, char *page)
139 return queue_var_show(queue_io_opt(q), page);
142 static ssize_t queue_discard_granularity_show(struct request_queue *q, char *page)
144 return queue_var_show(q->limits.discard_granularity, page);
147 static ssize_t queue_discard_max_show(struct request_queue *q, char *page)
149 return queue_var_show(q->limits.max_discard_sectors << 9, page);
152 static ssize_t queue_discard_zeroes_data_show(struct request_queue *q, char *page)
154 return queue_var_show(queue_discard_zeroes_data(q), page);
158 queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
160 unsigned long max_sectors_kb,
161 max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1,
162 page_kb = 1 << (PAGE_CACHE_SHIFT - 10);
163 ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
165 if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
168 spin_lock_irq(q->queue_lock);
169 q->limits.max_sectors = max_sectors_kb << 1;
170 spin_unlock_irq(q->queue_lock);
175 static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
177 int max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1;
179 return queue_var_show(max_hw_sectors_kb, (page));
182 static ssize_t queue_nonrot_show(struct request_queue *q, char *page)
184 return queue_var_show(!blk_queue_nonrot(q), page);
187 static ssize_t queue_nonrot_store(struct request_queue *q, const char *page,
191 ssize_t ret = queue_var_store(&nm, page, count);
193 spin_lock_irq(q->queue_lock);
195 queue_flag_clear(QUEUE_FLAG_NONROT, q);
197 queue_flag_set(QUEUE_FLAG_NONROT, q);
198 spin_unlock_irq(q->queue_lock);
203 static ssize_t queue_nomerges_show(struct request_queue *q, char *page)
205 return queue_var_show((blk_queue_nomerges(q) << 1) |
206 blk_queue_noxmerges(q), page);
209 static ssize_t queue_nomerges_store(struct request_queue *q, const char *page,
213 ssize_t ret = queue_var_store(&nm, page, count);
215 spin_lock_irq(q->queue_lock);
216 queue_flag_clear(QUEUE_FLAG_NOMERGES, q);
217 queue_flag_clear(QUEUE_FLAG_NOXMERGES, q);
219 queue_flag_set(QUEUE_FLAG_NOMERGES, q);
221 queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
222 spin_unlock_irq(q->queue_lock);
227 static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page)
229 bool set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags);
231 return queue_var_show(set, page);
235 queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count)
237 ssize_t ret = -EINVAL;
238 #if defined(CONFIG_USE_GENERIC_SMP_HELPERS)
241 ret = queue_var_store(&val, page, count);
242 spin_lock_irq(q->queue_lock);
244 queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
246 queue_flag_clear(QUEUE_FLAG_SAME_COMP, q);
247 spin_unlock_irq(q->queue_lock);
252 static ssize_t queue_iostats_show(struct request_queue *q, char *page)
254 return queue_var_show(blk_queue_io_stat(q), page);
257 static ssize_t queue_iostats_store(struct request_queue *q, const char *page,
261 ssize_t ret = queue_var_store(&stats, page, count);
263 spin_lock_irq(q->queue_lock);
265 queue_flag_set(QUEUE_FLAG_IO_STAT, q);
267 queue_flag_clear(QUEUE_FLAG_IO_STAT, q);
268 spin_unlock_irq(q->queue_lock);
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,
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,
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,
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,
296 static struct queue_sysfs_entry queue_max_segments_entry = {
297 .attr = {.name = "max_segments", .mode = S_IRUGO },
298 .show = queue_max_segments_show,
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,
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,
312 static struct queue_sysfs_entry queue_hw_sector_size_entry = {
313 .attr = {.name = "hw_sector_size", .mode = S_IRUGO },
314 .show = queue_logical_block_size_show,
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,
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,
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,
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,
337 static struct queue_sysfs_entry queue_discard_granularity_entry = {
338 .attr = {.name = "discard_granularity", .mode = S_IRUGO },
339 .show = queue_discard_granularity_show,
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,
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,
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,
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,
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,
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,
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,
381 &queue_max_segments_entry.attr,
382 &queue_max_segment_size_entry.attr,
383 &queue_iosched_entry.attr,
384 &queue_hw_sector_size_entry.attr,
385 &queue_logical_block_size_entry.attr,
386 &queue_physical_block_size_entry.attr,
387 &queue_io_min_entry.attr,
388 &queue_io_opt_entry.attr,
389 &queue_discard_granularity_entry.attr,
390 &queue_discard_max_entry.attr,
391 &queue_discard_zeroes_data_entry.attr,
392 &queue_nonrot_entry.attr,
393 &queue_nomerges_entry.attr,
394 &queue_rq_affinity_entry.attr,
395 &queue_iostats_entry.attr,
399 #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
402 queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
404 struct queue_sysfs_entry *entry = to_queue(attr);
405 struct request_queue *q =
406 container_of(kobj, struct request_queue, kobj);
411 mutex_lock(&q->sysfs_lock);
412 if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) {
413 mutex_unlock(&q->sysfs_lock);
416 res = entry->show(q, page);
417 mutex_unlock(&q->sysfs_lock);
422 queue_attr_store(struct kobject *kobj, struct attribute *attr,
423 const char *page, size_t length)
425 struct queue_sysfs_entry *entry = to_queue(attr);
426 struct request_queue *q;
432 q = container_of(kobj, struct request_queue, kobj);
433 mutex_lock(&q->sysfs_lock);
434 if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) {
435 mutex_unlock(&q->sysfs_lock);
438 res = entry->store(q, page, length);
439 mutex_unlock(&q->sysfs_lock);
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
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.
455 * Hopefully the low level driver will have finished any
456 * outstanding requests first...
458 static void blk_release_queue(struct kobject *kobj)
460 struct request_queue *q =
461 container_of(kobj, struct request_queue, kobj);
462 struct request_list *rl = &q->rq;
467 mempool_destroy(rl->rq_pool);
470 __blk_queue_free_tags(q);
472 blk_trace_shutdown(q);
474 bdi_destroy(&q->backing_dev_info);
475 kmem_cache_free(blk_requestq_cachep, q);
478 static struct sysfs_ops queue_sysfs_ops = {
479 .show = queue_attr_show,
480 .store = queue_attr_store,
483 struct kobj_type blk_queue_ktype = {
484 .sysfs_ops = &queue_sysfs_ops,
485 .default_attrs = default_attrs,
486 .release = blk_release_queue,
489 int blk_register_queue(struct gendisk *disk)
492 struct device *dev = disk_to_dev(disk);
494 struct request_queue *q = disk->queue;
499 ret = blk_trace_init_sysfs(dev);
503 ret = kobject_add(&q->kobj, kobject_get(&dev->kobj), "%s", "queue");
507 kobject_uevent(&q->kobj, KOBJ_ADD);
512 ret = elv_register_queue(q);
514 kobject_uevent(&q->kobj, KOBJ_REMOVE);
515 kobject_del(&q->kobj);
516 blk_trace_remove_sysfs(disk_to_dev(disk));
523 void blk_unregister_queue(struct gendisk *disk)
525 struct request_queue *q = disk->queue;
531 elv_unregister_queue(q);
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);