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>
10 #include <linux/blk-mq.h>
13 #include "blk-cgroup.h"
16 struct queue_sysfs_entry
{
17 struct attribute attr
;
18 ssize_t (*show
)(struct request_queue
*, char *);
19 ssize_t (*store
)(struct request_queue
*, const char *, size_t);
23 queue_var_show(unsigned long var
, char *page
)
25 return sprintf(page
, "%lu\n", var
);
29 queue_var_store(unsigned long *var
, const char *page
, size_t count
)
34 err
= kstrtoul(page
, 10, &v
);
35 if (err
|| v
> UINT_MAX
)
43 static ssize_t
queue_requests_show(struct request_queue
*q
, char *page
)
45 return queue_var_show(q
->nr_requests
, (page
));
49 queue_requests_store(struct request_queue
*q
, const char *page
, size_t count
)
54 if (!q
->request_fn
&& !q
->mq_ops
)
57 ret
= queue_var_store(&nr
, page
, count
);
61 if (nr
< BLKDEV_MIN_RQ
)
65 err
= blk_update_nr_requests(q
, nr
);
67 err
= blk_mq_update_nr_requests(q
, nr
);
75 static ssize_t
queue_ra_show(struct request_queue
*q
, char *page
)
77 unsigned long ra_kb
= q
->backing_dev_info
.ra_pages
<<
78 (PAGE_CACHE_SHIFT
- 10);
80 return queue_var_show(ra_kb
, (page
));
84 queue_ra_store(struct request_queue
*q
, const char *page
, size_t count
)
87 ssize_t ret
= queue_var_store(&ra_kb
, page
, count
);
92 q
->backing_dev_info
.ra_pages
= ra_kb
>> (PAGE_CACHE_SHIFT
- 10);
97 static ssize_t
queue_max_sectors_show(struct request_queue
*q
, char *page
)
99 int max_sectors_kb
= queue_max_sectors(q
) >> 1;
101 return queue_var_show(max_sectors_kb
, (page
));
104 static ssize_t
queue_max_segments_show(struct request_queue
*q
, char *page
)
106 return queue_var_show(queue_max_segments(q
), (page
));
109 static ssize_t
queue_max_integrity_segments_show(struct request_queue
*q
, char *page
)
111 return queue_var_show(q
->limits
.max_integrity_segments
, (page
));
114 static ssize_t
queue_max_segment_size_show(struct request_queue
*q
, char *page
)
116 if (blk_queue_cluster(q
))
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 sprintf(page
, "%llu\n",
150 (unsigned long long)q
->limits
.max_discard_sectors
<< 9);
153 static ssize_t
queue_discard_zeroes_data_show(struct request_queue
*q
, char *page
)
155 return queue_var_show(queue_discard_zeroes_data(q
), page
);
158 static ssize_t
queue_write_same_max_show(struct request_queue
*q
, char *page
)
160 return sprintf(page
, "%llu\n",
161 (unsigned long long)q
->limits
.max_write_same_sectors
<< 9);
166 queue_max_sectors_store(struct request_queue
*q
, const char *page
, size_t count
)
168 unsigned long max_sectors_kb
,
169 max_hw_sectors_kb
= queue_max_hw_sectors(q
) >> 1,
170 page_kb
= 1 << (PAGE_CACHE_SHIFT
- 10);
171 ssize_t ret
= queue_var_store(&max_sectors_kb
, page
, count
);
176 if (max_sectors_kb
> max_hw_sectors_kb
|| max_sectors_kb
< page_kb
)
179 spin_lock_irq(q
->queue_lock
);
180 q
->limits
.max_sectors
= max_sectors_kb
<< 1;
181 spin_unlock_irq(q
->queue_lock
);
186 static ssize_t
queue_max_hw_sectors_show(struct request_queue
*q
, char *page
)
188 int max_hw_sectors_kb
= queue_max_hw_sectors(q
) >> 1;
190 return queue_var_show(max_hw_sectors_kb
, (page
));
193 #define QUEUE_SYSFS_BIT_FNS(name, flag, neg) \
195 queue_show_##name(struct request_queue *q, char *page) \
198 bit = test_bit(QUEUE_FLAG_##flag, &q->queue_flags); \
199 return queue_var_show(neg ? !bit : bit, page); \
202 queue_store_##name(struct request_queue *q, const char *page, size_t count) \
206 ret = queue_var_store(&val, page, count); \
212 spin_lock_irq(q->queue_lock); \
214 queue_flag_set(QUEUE_FLAG_##flag, q); \
216 queue_flag_clear(QUEUE_FLAG_##flag, q); \
217 spin_unlock_irq(q->queue_lock); \
221 QUEUE_SYSFS_BIT_FNS(nonrot
, NONROT
, 1);
222 QUEUE_SYSFS_BIT_FNS(random
, ADD_RANDOM
, 0);
223 QUEUE_SYSFS_BIT_FNS(iostats
, IO_STAT
, 0);
224 #undef QUEUE_SYSFS_BIT_FNS
226 static ssize_t
queue_nomerges_show(struct request_queue
*q
, char *page
)
228 return queue_var_show((blk_queue_nomerges(q
) << 1) |
229 blk_queue_noxmerges(q
), page
);
232 static ssize_t
queue_nomerges_store(struct request_queue
*q
, const char *page
,
236 ssize_t ret
= queue_var_store(&nm
, page
, count
);
241 spin_lock_irq(q
->queue_lock
);
242 queue_flag_clear(QUEUE_FLAG_NOMERGES
, q
);
243 queue_flag_clear(QUEUE_FLAG_NOXMERGES
, q
);
245 queue_flag_set(QUEUE_FLAG_NOMERGES
, q
);
247 queue_flag_set(QUEUE_FLAG_NOXMERGES
, q
);
248 spin_unlock_irq(q
->queue_lock
);
253 static ssize_t
queue_rq_affinity_show(struct request_queue
*q
, char *page
)
255 bool set
= test_bit(QUEUE_FLAG_SAME_COMP
, &q
->queue_flags
);
256 bool force
= test_bit(QUEUE_FLAG_SAME_FORCE
, &q
->queue_flags
);
258 return queue_var_show(set
<< force
, page
);
262 queue_rq_affinity_store(struct request_queue
*q
, const char *page
, size_t count
)
264 ssize_t ret
= -EINVAL
;
268 ret
= queue_var_store(&val
, page
, count
);
272 spin_lock_irq(q
->queue_lock
);
274 queue_flag_set(QUEUE_FLAG_SAME_COMP
, q
);
275 queue_flag_set(QUEUE_FLAG_SAME_FORCE
, q
);
276 } else if (val
== 1) {
277 queue_flag_set(QUEUE_FLAG_SAME_COMP
, q
);
278 queue_flag_clear(QUEUE_FLAG_SAME_FORCE
, q
);
279 } else if (val
== 0) {
280 queue_flag_clear(QUEUE_FLAG_SAME_COMP
, q
);
281 queue_flag_clear(QUEUE_FLAG_SAME_FORCE
, q
);
283 spin_unlock_irq(q
->queue_lock
);
288 static struct queue_sysfs_entry queue_requests_entry
= {
289 .attr
= {.name
= "nr_requests", .mode
= S_IRUGO
| S_IWUSR
},
290 .show
= queue_requests_show
,
291 .store
= queue_requests_store
,
294 static struct queue_sysfs_entry queue_ra_entry
= {
295 .attr
= {.name
= "read_ahead_kb", .mode
= S_IRUGO
| S_IWUSR
},
296 .show
= queue_ra_show
,
297 .store
= queue_ra_store
,
300 static struct queue_sysfs_entry queue_max_sectors_entry
= {
301 .attr
= {.name
= "max_sectors_kb", .mode
= S_IRUGO
| S_IWUSR
},
302 .show
= queue_max_sectors_show
,
303 .store
= queue_max_sectors_store
,
306 static struct queue_sysfs_entry queue_max_hw_sectors_entry
= {
307 .attr
= {.name
= "max_hw_sectors_kb", .mode
= S_IRUGO
},
308 .show
= queue_max_hw_sectors_show
,
311 static struct queue_sysfs_entry queue_max_segments_entry
= {
312 .attr
= {.name
= "max_segments", .mode
= S_IRUGO
},
313 .show
= queue_max_segments_show
,
316 static struct queue_sysfs_entry queue_max_integrity_segments_entry
= {
317 .attr
= {.name
= "max_integrity_segments", .mode
= S_IRUGO
},
318 .show
= queue_max_integrity_segments_show
,
321 static struct queue_sysfs_entry queue_max_segment_size_entry
= {
322 .attr
= {.name
= "max_segment_size", .mode
= S_IRUGO
},
323 .show
= queue_max_segment_size_show
,
326 static struct queue_sysfs_entry queue_iosched_entry
= {
327 .attr
= {.name
= "scheduler", .mode
= S_IRUGO
| S_IWUSR
},
328 .show
= elv_iosched_show
,
329 .store
= elv_iosched_store
,
332 static struct queue_sysfs_entry queue_hw_sector_size_entry
= {
333 .attr
= {.name
= "hw_sector_size", .mode
= S_IRUGO
},
334 .show
= queue_logical_block_size_show
,
337 static struct queue_sysfs_entry queue_logical_block_size_entry
= {
338 .attr
= {.name
= "logical_block_size", .mode
= S_IRUGO
},
339 .show
= queue_logical_block_size_show
,
342 static struct queue_sysfs_entry queue_physical_block_size_entry
= {
343 .attr
= {.name
= "physical_block_size", .mode
= S_IRUGO
},
344 .show
= queue_physical_block_size_show
,
347 static struct queue_sysfs_entry queue_io_min_entry
= {
348 .attr
= {.name
= "minimum_io_size", .mode
= S_IRUGO
},
349 .show
= queue_io_min_show
,
352 static struct queue_sysfs_entry queue_io_opt_entry
= {
353 .attr
= {.name
= "optimal_io_size", .mode
= S_IRUGO
},
354 .show
= queue_io_opt_show
,
357 static struct queue_sysfs_entry queue_discard_granularity_entry
= {
358 .attr
= {.name
= "discard_granularity", .mode
= S_IRUGO
},
359 .show
= queue_discard_granularity_show
,
362 static struct queue_sysfs_entry queue_discard_max_entry
= {
363 .attr
= {.name
= "discard_max_bytes", .mode
= S_IRUGO
},
364 .show
= queue_discard_max_show
,
367 static struct queue_sysfs_entry queue_discard_zeroes_data_entry
= {
368 .attr
= {.name
= "discard_zeroes_data", .mode
= S_IRUGO
},
369 .show
= queue_discard_zeroes_data_show
,
372 static struct queue_sysfs_entry queue_write_same_max_entry
= {
373 .attr
= {.name
= "write_same_max_bytes", .mode
= S_IRUGO
},
374 .show
= queue_write_same_max_show
,
377 static struct queue_sysfs_entry queue_nonrot_entry
= {
378 .attr
= {.name
= "rotational", .mode
= S_IRUGO
| S_IWUSR
},
379 .show
= queue_show_nonrot
,
380 .store
= queue_store_nonrot
,
383 static struct queue_sysfs_entry queue_nomerges_entry
= {
384 .attr
= {.name
= "nomerges", .mode
= S_IRUGO
| S_IWUSR
},
385 .show
= queue_nomerges_show
,
386 .store
= queue_nomerges_store
,
389 static struct queue_sysfs_entry queue_rq_affinity_entry
= {
390 .attr
= {.name
= "rq_affinity", .mode
= S_IRUGO
| S_IWUSR
},
391 .show
= queue_rq_affinity_show
,
392 .store
= queue_rq_affinity_store
,
395 static struct queue_sysfs_entry queue_iostats_entry
= {
396 .attr
= {.name
= "iostats", .mode
= S_IRUGO
| S_IWUSR
},
397 .show
= queue_show_iostats
,
398 .store
= queue_store_iostats
,
401 static struct queue_sysfs_entry queue_random_entry
= {
402 .attr
= {.name
= "add_random", .mode
= S_IRUGO
| S_IWUSR
},
403 .show
= queue_show_random
,
404 .store
= queue_store_random
,
407 static struct attribute
*default_attrs
[] = {
408 &queue_requests_entry
.attr
,
409 &queue_ra_entry
.attr
,
410 &queue_max_hw_sectors_entry
.attr
,
411 &queue_max_sectors_entry
.attr
,
412 &queue_max_segments_entry
.attr
,
413 &queue_max_integrity_segments_entry
.attr
,
414 &queue_max_segment_size_entry
.attr
,
415 &queue_iosched_entry
.attr
,
416 &queue_hw_sector_size_entry
.attr
,
417 &queue_logical_block_size_entry
.attr
,
418 &queue_physical_block_size_entry
.attr
,
419 &queue_io_min_entry
.attr
,
420 &queue_io_opt_entry
.attr
,
421 &queue_discard_granularity_entry
.attr
,
422 &queue_discard_max_entry
.attr
,
423 &queue_discard_zeroes_data_entry
.attr
,
424 &queue_write_same_max_entry
.attr
,
425 &queue_nonrot_entry
.attr
,
426 &queue_nomerges_entry
.attr
,
427 &queue_rq_affinity_entry
.attr
,
428 &queue_iostats_entry
.attr
,
429 &queue_random_entry
.attr
,
433 #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
436 queue_attr_show(struct kobject
*kobj
, struct attribute
*attr
, char *page
)
438 struct queue_sysfs_entry
*entry
= to_queue(attr
);
439 struct request_queue
*q
=
440 container_of(kobj
, struct request_queue
, kobj
);
445 mutex_lock(&q
->sysfs_lock
);
446 if (blk_queue_dying(q
)) {
447 mutex_unlock(&q
->sysfs_lock
);
450 res
= entry
->show(q
, page
);
451 mutex_unlock(&q
->sysfs_lock
);
456 queue_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
457 const char *page
, size_t length
)
459 struct queue_sysfs_entry
*entry
= to_queue(attr
);
460 struct request_queue
*q
;
466 q
= container_of(kobj
, struct request_queue
, kobj
);
467 mutex_lock(&q
->sysfs_lock
);
468 if (blk_queue_dying(q
)) {
469 mutex_unlock(&q
->sysfs_lock
);
472 res
= entry
->store(q
, page
, length
);
473 mutex_unlock(&q
->sysfs_lock
);
477 static void blk_free_queue_rcu(struct rcu_head
*rcu_head
)
479 struct request_queue
*q
= container_of(rcu_head
, struct request_queue
,
481 kmem_cache_free(blk_requestq_cachep
, q
);
485 * blk_release_queue: - release a &struct request_queue when it is no longer needed
486 * @kobj: the kobj belonging to the request queue to be released
489 * blk_release_queue is the pair to blk_init_queue() or
490 * blk_queue_make_request(). It should be called when a request queue is
491 * being released; typically when a block device is being de-registered.
492 * Currently, its primary task it to free all the &struct request
493 * structures that were allocated to the queue and the queue itself.
496 * Hopefully the low level driver will have finished any
497 * outstanding requests first...
499 static void blk_release_queue(struct kobject
*kobj
)
501 struct request_queue
*q
=
502 container_of(kobj
, struct request_queue
, kobj
);
509 spin_lock_irq(q
->queue_lock
);
511 spin_unlock_irq(q
->queue_lock
);
512 elevator_exit(q
->elevator
);
515 blk_exit_rl(&q
->root_rl
);
518 __blk_queue_free_tags(q
);
521 blk_mq_free_queue(q
);
525 blk_trace_shutdown(q
);
527 bdi_destroy(&q
->backing_dev_info
);
529 ida_simple_remove(&blk_queue_ida
, q
->id
);
530 call_rcu(&q
->rcu_head
, blk_free_queue_rcu
);
533 static const struct sysfs_ops queue_sysfs_ops
= {
534 .show
= queue_attr_show
,
535 .store
= queue_attr_store
,
538 struct kobj_type blk_queue_ktype
= {
539 .sysfs_ops
= &queue_sysfs_ops
,
540 .default_attrs
= default_attrs
,
541 .release
= blk_release_queue
,
544 int blk_register_queue(struct gendisk
*disk
)
547 struct device
*dev
= disk_to_dev(disk
);
548 struct request_queue
*q
= disk
->queue
;
554 * Initialization must be complete by now. Finish the initial
555 * bypass from queue allocation.
557 queue_flag_set_unlocked(QUEUE_FLAG_INIT_DONE
, q
);
558 blk_queue_bypass_end(q
);
560 ret
= blk_trace_init_sysfs(dev
);
564 ret
= kobject_add(&q
->kobj
, kobject_get(&dev
->kobj
), "%s", "queue");
566 blk_trace_remove_sysfs(dev
);
570 kobject_uevent(&q
->kobj
, KOBJ_ADD
);
573 blk_mq_register_disk(disk
);
578 ret
= elv_register_queue(q
);
580 kobject_uevent(&q
->kobj
, KOBJ_REMOVE
);
581 kobject_del(&q
->kobj
);
582 blk_trace_remove_sysfs(dev
);
583 kobject_put(&dev
->kobj
);
590 void blk_unregister_queue(struct gendisk
*disk
)
592 struct request_queue
*q
= disk
->queue
;
598 blk_mq_unregister_disk(disk
);
601 elv_unregister_queue(q
);
603 kobject_uevent(&q
->kobj
, KOBJ_REMOVE
);
604 kobject_del(&q
->kobj
);
605 blk_trace_remove_sysfs(disk_to_dev(disk
));
606 kobject_put(&disk_to_dev(disk
)->kobj
);