2 * Functions related to sysfs handling
4 #include <linux/kernel.h>
5 #include <linux/slab.h>
6 #include <linux/module.h>
8 #include <linux/blkdev.h>
9 #include <linux/blktrace_api.h>
12 #include "blk-cgroup.h"
14 struct queue_sysfs_entry
{
15 struct attribute attr
;
16 ssize_t (*show
)(struct request_queue
*, char *);
17 ssize_t (*store
)(struct request_queue
*, const char *, size_t);
21 queue_var_show(unsigned long var
, char *page
)
23 return sprintf(page
, "%lu\n", var
);
27 queue_var_store(unsigned long *var
, const char *page
, size_t count
)
32 err
= strict_strtoul(page
, 10, &v
);
33 if (err
|| v
> UINT_MAX
)
41 static ssize_t
queue_requests_show(struct request_queue
*q
, char *page
)
43 return queue_var_show(q
->nr_requests
, (page
));
47 queue_requests_store(struct request_queue
*q
, const char *page
, size_t count
)
49 struct request_list
*rl
;
56 ret
= queue_var_store(&nr
, page
, count
);
60 if (nr
< BLKDEV_MIN_RQ
)
63 spin_lock_irq(q
->queue_lock
);
65 blk_queue_congestion_threshold(q
);
67 /* congestion isn't cgroup aware and follows root blkcg for now */
70 if (rl
->count
[BLK_RW_SYNC
] >= queue_congestion_on_threshold(q
))
71 blk_set_queue_congested(q
, BLK_RW_SYNC
);
72 else if (rl
->count
[BLK_RW_SYNC
] < queue_congestion_off_threshold(q
))
73 blk_clear_queue_congested(q
, BLK_RW_SYNC
);
75 if (rl
->count
[BLK_RW_ASYNC
] >= queue_congestion_on_threshold(q
))
76 blk_set_queue_congested(q
, BLK_RW_ASYNC
);
77 else if (rl
->count
[BLK_RW_ASYNC
] < queue_congestion_off_threshold(q
))
78 blk_clear_queue_congested(q
, BLK_RW_ASYNC
);
80 blk_queue_for_each_rl(rl
, q
) {
81 if (rl
->count
[BLK_RW_SYNC
] >= q
->nr_requests
) {
82 blk_set_rl_full(rl
, BLK_RW_SYNC
);
84 blk_clear_rl_full(rl
, BLK_RW_SYNC
);
85 wake_up(&rl
->wait
[BLK_RW_SYNC
]);
88 if (rl
->count
[BLK_RW_ASYNC
] >= q
->nr_requests
) {
89 blk_set_rl_full(rl
, BLK_RW_ASYNC
);
91 blk_clear_rl_full(rl
, BLK_RW_ASYNC
);
92 wake_up(&rl
->wait
[BLK_RW_ASYNC
]);
96 spin_unlock_irq(q
->queue_lock
);
100 static ssize_t
queue_ra_show(struct request_queue
*q
, char *page
)
102 unsigned long ra_kb
= q
->backing_dev_info
.ra_pages
<<
103 (PAGE_CACHE_SHIFT
- 10);
105 return queue_var_show(ra_kb
, (page
));
109 queue_ra_store(struct request_queue
*q
, const char *page
, size_t count
)
112 ssize_t ret
= queue_var_store(&ra_kb
, page
, count
);
117 q
->backing_dev_info
.ra_pages
= ra_kb
>> (PAGE_CACHE_SHIFT
- 10);
122 static ssize_t
queue_max_sectors_show(struct request_queue
*q
, char *page
)
124 int max_sectors_kb
= queue_max_sectors(q
) >> 1;
126 return queue_var_show(max_sectors_kb
, (page
));
129 static ssize_t
queue_max_segments_show(struct request_queue
*q
, char *page
)
131 return queue_var_show(queue_max_segments(q
), (page
));
134 static ssize_t
queue_max_integrity_segments_show(struct request_queue
*q
, char *page
)
136 return queue_var_show(q
->limits
.max_integrity_segments
, (page
));
139 static ssize_t
queue_max_segment_size_show(struct request_queue
*q
, char *page
)
141 if (blk_queue_cluster(q
))
142 return queue_var_show(queue_max_segment_size(q
), (page
));
144 return queue_var_show(PAGE_CACHE_SIZE
, (page
));
147 static ssize_t
queue_logical_block_size_show(struct request_queue
*q
, char *page
)
149 return queue_var_show(queue_logical_block_size(q
), page
);
152 static ssize_t
queue_physical_block_size_show(struct request_queue
*q
, char *page
)
154 return queue_var_show(queue_physical_block_size(q
), page
);
157 static ssize_t
queue_io_min_show(struct request_queue
*q
, char *page
)
159 return queue_var_show(queue_io_min(q
), page
);
162 static ssize_t
queue_io_opt_show(struct request_queue
*q
, char *page
)
164 return queue_var_show(queue_io_opt(q
), page
);
167 static ssize_t
queue_discard_granularity_show(struct request_queue
*q
, char *page
)
169 return queue_var_show(q
->limits
.discard_granularity
, page
);
172 static ssize_t
queue_discard_max_show(struct request_queue
*q
, char *page
)
174 return sprintf(page
, "%llu\n",
175 (unsigned long long)q
->limits
.max_discard_sectors
<< 9);
178 static ssize_t
queue_discard_zeroes_data_show(struct request_queue
*q
, char *page
)
180 return queue_var_show(queue_discard_zeroes_data(q
), page
);
183 static ssize_t
queue_write_same_max_show(struct request_queue
*q
, char *page
)
185 return sprintf(page
, "%llu\n",
186 (unsigned long long)q
->limits
.max_write_same_sectors
<< 9);
191 queue_max_sectors_store(struct request_queue
*q
, const char *page
, size_t count
)
193 unsigned long max_sectors_kb
,
194 max_hw_sectors_kb
= queue_max_hw_sectors(q
) >> 1,
195 page_kb
= 1 << (PAGE_CACHE_SHIFT
- 10);
196 ssize_t ret
= queue_var_store(&max_sectors_kb
, page
, count
);
201 if (max_sectors_kb
> max_hw_sectors_kb
|| max_sectors_kb
< page_kb
)
204 spin_lock_irq(q
->queue_lock
);
205 q
->limits
.max_sectors
= max_sectors_kb
<< 1;
206 spin_unlock_irq(q
->queue_lock
);
211 static ssize_t
queue_max_hw_sectors_show(struct request_queue
*q
, char *page
)
213 int max_hw_sectors_kb
= queue_max_hw_sectors(q
) >> 1;
215 return queue_var_show(max_hw_sectors_kb
, (page
));
218 #define QUEUE_SYSFS_BIT_FNS(name, flag, neg) \
220 queue_show_##name(struct request_queue *q, char *page) \
223 bit = test_bit(QUEUE_FLAG_##flag, &q->queue_flags); \
224 return queue_var_show(neg ? !bit : bit, page); \
227 queue_store_##name(struct request_queue *q, const char *page, size_t count) \
231 ret = queue_var_store(&val, page, count); \
235 spin_lock_irq(q->queue_lock); \
237 queue_flag_set(QUEUE_FLAG_##flag, q); \
239 queue_flag_clear(QUEUE_FLAG_##flag, q); \
240 spin_unlock_irq(q->queue_lock); \
244 QUEUE_SYSFS_BIT_FNS(nonrot
, NONROT
, 1);
245 QUEUE_SYSFS_BIT_FNS(random
, ADD_RANDOM
, 0);
246 QUEUE_SYSFS_BIT_FNS(iostats
, IO_STAT
, 0);
247 #undef QUEUE_SYSFS_BIT_FNS
249 static ssize_t
queue_nomerges_show(struct request_queue
*q
, char *page
)
251 return queue_var_show((blk_queue_nomerges(q
) << 1) |
252 blk_queue_noxmerges(q
), page
);
255 static ssize_t
queue_nomerges_store(struct request_queue
*q
, const char *page
,
259 ssize_t ret
= queue_var_store(&nm
, page
, count
);
264 spin_lock_irq(q
->queue_lock
);
265 queue_flag_clear(QUEUE_FLAG_NOMERGES
, q
);
266 queue_flag_clear(QUEUE_FLAG_NOXMERGES
, q
);
268 queue_flag_set(QUEUE_FLAG_NOMERGES
, q
);
270 queue_flag_set(QUEUE_FLAG_NOXMERGES
, q
);
271 spin_unlock_irq(q
->queue_lock
);
276 static ssize_t
queue_rq_affinity_show(struct request_queue
*q
, char *page
)
278 bool set
= test_bit(QUEUE_FLAG_SAME_COMP
, &q
->queue_flags
);
279 bool force
= test_bit(QUEUE_FLAG_SAME_FORCE
, &q
->queue_flags
);
281 return queue_var_show(set
<< force
, page
);
285 queue_rq_affinity_store(struct request_queue
*q
, const char *page
, size_t count
)
287 ssize_t ret
= -EINVAL
;
288 #if defined(CONFIG_USE_GENERIC_SMP_HELPERS)
291 ret
= queue_var_store(&val
, page
, count
);
295 spin_lock_irq(q
->queue_lock
);
297 queue_flag_set(QUEUE_FLAG_SAME_COMP
, q
);
298 queue_flag_set(QUEUE_FLAG_SAME_FORCE
, q
);
299 } else if (val
== 1) {
300 queue_flag_set(QUEUE_FLAG_SAME_COMP
, q
);
301 queue_flag_clear(QUEUE_FLAG_SAME_FORCE
, q
);
302 } else if (val
== 0) {
303 queue_flag_clear(QUEUE_FLAG_SAME_COMP
, q
);
304 queue_flag_clear(QUEUE_FLAG_SAME_FORCE
, q
);
306 spin_unlock_irq(q
->queue_lock
);
311 static struct queue_sysfs_entry queue_requests_entry
= {
312 .attr
= {.name
= "nr_requests", .mode
= S_IRUGO
| S_IWUSR
},
313 .show
= queue_requests_show
,
314 .store
= queue_requests_store
,
317 static struct queue_sysfs_entry queue_ra_entry
= {
318 .attr
= {.name
= "read_ahead_kb", .mode
= S_IRUGO
| S_IWUSR
},
319 .show
= queue_ra_show
,
320 .store
= queue_ra_store
,
323 static struct queue_sysfs_entry queue_max_sectors_entry
= {
324 .attr
= {.name
= "max_sectors_kb", .mode
= S_IRUGO
| S_IWUSR
},
325 .show
= queue_max_sectors_show
,
326 .store
= queue_max_sectors_store
,
329 static struct queue_sysfs_entry queue_max_hw_sectors_entry
= {
330 .attr
= {.name
= "max_hw_sectors_kb", .mode
= S_IRUGO
},
331 .show
= queue_max_hw_sectors_show
,
334 static struct queue_sysfs_entry queue_max_segments_entry
= {
335 .attr
= {.name
= "max_segments", .mode
= S_IRUGO
},
336 .show
= queue_max_segments_show
,
339 static struct queue_sysfs_entry queue_max_integrity_segments_entry
= {
340 .attr
= {.name
= "max_integrity_segments", .mode
= S_IRUGO
},
341 .show
= queue_max_integrity_segments_show
,
344 static struct queue_sysfs_entry queue_max_segment_size_entry
= {
345 .attr
= {.name
= "max_segment_size", .mode
= S_IRUGO
},
346 .show
= queue_max_segment_size_show
,
349 static struct queue_sysfs_entry queue_iosched_entry
= {
350 .attr
= {.name
= "scheduler", .mode
= S_IRUGO
| S_IWUSR
},
351 .show
= elv_iosched_show
,
352 .store
= elv_iosched_store
,
355 static struct queue_sysfs_entry queue_hw_sector_size_entry
= {
356 .attr
= {.name
= "hw_sector_size", .mode
= S_IRUGO
},
357 .show
= queue_logical_block_size_show
,
360 static struct queue_sysfs_entry queue_logical_block_size_entry
= {
361 .attr
= {.name
= "logical_block_size", .mode
= S_IRUGO
},
362 .show
= queue_logical_block_size_show
,
365 static struct queue_sysfs_entry queue_physical_block_size_entry
= {
366 .attr
= {.name
= "physical_block_size", .mode
= S_IRUGO
},
367 .show
= queue_physical_block_size_show
,
370 static struct queue_sysfs_entry queue_io_min_entry
= {
371 .attr
= {.name
= "minimum_io_size", .mode
= S_IRUGO
},
372 .show
= queue_io_min_show
,
375 static struct queue_sysfs_entry queue_io_opt_entry
= {
376 .attr
= {.name
= "optimal_io_size", .mode
= S_IRUGO
},
377 .show
= queue_io_opt_show
,
380 static struct queue_sysfs_entry queue_discard_granularity_entry
= {
381 .attr
= {.name
= "discard_granularity", .mode
= S_IRUGO
},
382 .show
= queue_discard_granularity_show
,
385 static struct queue_sysfs_entry queue_discard_max_entry
= {
386 .attr
= {.name
= "discard_max_bytes", .mode
= S_IRUGO
},
387 .show
= queue_discard_max_show
,
390 static struct queue_sysfs_entry queue_discard_zeroes_data_entry
= {
391 .attr
= {.name
= "discard_zeroes_data", .mode
= S_IRUGO
},
392 .show
= queue_discard_zeroes_data_show
,
395 static struct queue_sysfs_entry queue_write_same_max_entry
= {
396 .attr
= {.name
= "write_same_max_bytes", .mode
= S_IRUGO
},
397 .show
= queue_write_same_max_show
,
400 static struct queue_sysfs_entry queue_nonrot_entry
= {
401 .attr
= {.name
= "rotational", .mode
= S_IRUGO
| S_IWUSR
},
402 .show
= queue_show_nonrot
,
403 .store
= queue_store_nonrot
,
406 static struct queue_sysfs_entry queue_nomerges_entry
= {
407 .attr
= {.name
= "nomerges", .mode
= S_IRUGO
| S_IWUSR
},
408 .show
= queue_nomerges_show
,
409 .store
= queue_nomerges_store
,
412 static struct queue_sysfs_entry queue_rq_affinity_entry
= {
413 .attr
= {.name
= "rq_affinity", .mode
= S_IRUGO
| S_IWUSR
},
414 .show
= queue_rq_affinity_show
,
415 .store
= queue_rq_affinity_store
,
418 static struct queue_sysfs_entry queue_iostats_entry
= {
419 .attr
= {.name
= "iostats", .mode
= S_IRUGO
| S_IWUSR
},
420 .show
= queue_show_iostats
,
421 .store
= queue_store_iostats
,
424 static struct queue_sysfs_entry queue_random_entry
= {
425 .attr
= {.name
= "add_random", .mode
= S_IRUGO
| S_IWUSR
},
426 .show
= queue_show_random
,
427 .store
= queue_store_random
,
430 static struct attribute
*default_attrs
[] = {
431 &queue_requests_entry
.attr
,
432 &queue_ra_entry
.attr
,
433 &queue_max_hw_sectors_entry
.attr
,
434 &queue_max_sectors_entry
.attr
,
435 &queue_max_segments_entry
.attr
,
436 &queue_max_integrity_segments_entry
.attr
,
437 &queue_max_segment_size_entry
.attr
,
438 &queue_iosched_entry
.attr
,
439 &queue_hw_sector_size_entry
.attr
,
440 &queue_logical_block_size_entry
.attr
,
441 &queue_physical_block_size_entry
.attr
,
442 &queue_io_min_entry
.attr
,
443 &queue_io_opt_entry
.attr
,
444 &queue_discard_granularity_entry
.attr
,
445 &queue_discard_max_entry
.attr
,
446 &queue_discard_zeroes_data_entry
.attr
,
447 &queue_write_same_max_entry
.attr
,
448 &queue_nonrot_entry
.attr
,
449 &queue_nomerges_entry
.attr
,
450 &queue_rq_affinity_entry
.attr
,
451 &queue_iostats_entry
.attr
,
452 &queue_random_entry
.attr
,
456 #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
459 queue_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
461 struct queue_sysfs_entry
*entry
= to_queue(attr
);
462 struct request_queue
*q
=
463 container_of(kobj
, struct request_queue
, kobj
);
468 mutex_lock(&q
->sysfs_lock
);
469 if (blk_queue_dying(q
)) {
470 mutex_unlock(&q
->sysfs_lock
);
473 res
= entry
->show(q
, page
);
474 mutex_unlock(&q
->sysfs_lock
);
479 queue_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
480 const char *page
, size_t length
)
482 struct queue_sysfs_entry
*entry
= to_queue(attr
);
483 struct request_queue
*q
;
489 q
= container_of(kobj
, struct request_queue
, kobj
);
490 mutex_lock(&q
->sysfs_lock
);
491 if (blk_queue_dying(q
)) {
492 mutex_unlock(&q
->sysfs_lock
);
495 res
= entry
->store(q
, page
, length
);
496 mutex_unlock(&q
->sysfs_lock
);
500 static void blk_free_queue_rcu(struct rcu_head
*rcu_head
)
502 struct request_queue
*q
= container_of(rcu_head
, struct request_queue
,
504 kmem_cache_free(blk_requestq_cachep
, q
);
508 * blk_release_queue: - release a &struct request_queue when it is no longer needed
509 * @kobj: the kobj belonging to the request queue to be released
512 * blk_release_queue is the pair to blk_init_queue() or
513 * blk_queue_make_request(). It should be called when a request queue is
514 * being released; typically when a block device is being de-registered.
515 * Currently, its primary task it to free all the &struct request
516 * structures that were allocated to the queue and the queue itself.
519 * Hopefully the low level driver will have finished any
520 * outstanding requests first...
522 static void blk_release_queue(struct kobject
*kobj
)
524 struct request_queue
*q
=
525 container_of(kobj
, struct request_queue
, kobj
);
532 spin_lock_irq(q
->queue_lock
);
534 spin_unlock_irq(q
->queue_lock
);
535 elevator_exit(q
->elevator
);
538 blk_exit_rl(&q
->root_rl
);
541 __blk_queue_free_tags(q
);
543 blk_trace_shutdown(q
);
545 bdi_destroy(&q
->backing_dev_info
);
547 ida_simple_remove(&blk_queue_ida
, q
->id
);
548 call_rcu(&q
->rcu_head
, blk_free_queue_rcu
);
551 static const struct sysfs_ops queue_sysfs_ops
= {
552 .show
= queue_attr_show
,
553 .store
= queue_attr_store
,
556 struct kobj_type blk_queue_ktype
= {
557 .sysfs_ops
= &queue_sysfs_ops
,
558 .default_attrs
= default_attrs
,
559 .release
= blk_release_queue
,
562 int blk_register_queue(struct gendisk
*disk
)
565 struct device
*dev
= disk_to_dev(disk
);
566 struct request_queue
*q
= disk
->queue
;
572 * Initialization must be complete by now. Finish the initial
573 * bypass from queue allocation.
575 blk_queue_bypass_end(q
);
577 ret
= blk_trace_init_sysfs(dev
);
581 ret
= kobject_add(&q
->kobj
, kobject_get(&dev
->kobj
), "%s", "queue");
583 blk_trace_remove_sysfs(dev
);
587 kobject_uevent(&q
->kobj
, KOBJ_ADD
);
592 ret
= elv_register_queue(q
);
594 kobject_uevent(&q
->kobj
, KOBJ_REMOVE
);
595 kobject_del(&q
->kobj
);
596 blk_trace_remove_sysfs(dev
);
597 kobject_put(&dev
->kobj
);
604 void blk_unregister_queue(struct gendisk
*disk
)
606 struct request_queue
*q
= disk
->queue
;
612 elv_unregister_queue(q
);
614 kobject_uevent(&q
->kobj
, KOBJ_REMOVE
);
615 kobject_del(&q
->kobj
);
616 blk_trace_remove_sysfs(disk_to_dev(disk
));
617 kobject_put(&disk_to_dev(disk
)->kobj
);