Merge tag 'locks-v3.16-2' of git://git.samba.org/jlayton/linux
[linux/fpc-iii.git] / block / blk-sysfs.c
blob23321fbab29318ae5b550216c66eb9ae2d026c52
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>
10 #include <linux/blk-mq.h>
12 #include "blk.h"
13 #include "blk-cgroup.h"
14 #include "blk-mq.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);
22 static ssize_t
23 queue_var_show(unsigned long var, char *page)
25 return sprintf(page, "%lu\n", var);
28 static ssize_t
29 queue_var_store(unsigned long *var, const char *page, size_t count)
31 int err;
32 unsigned long v;
34 err = kstrtoul(page, 10, &v);
35 if (err || v > UINT_MAX)
36 return -EINVAL;
38 *var = v;
40 return count;
43 static ssize_t queue_requests_show(struct request_queue *q, char *page)
45 return queue_var_show(q->nr_requests, (page));
48 static ssize_t
49 queue_requests_store(struct request_queue *q, const char *page, size_t count)
51 unsigned long nr;
52 int ret, err;
54 if (!q->request_fn && !q->mq_ops)
55 return -EINVAL;
57 ret = queue_var_store(&nr, page, count);
58 if (ret < 0)
59 return ret;
61 if (nr < BLKDEV_MIN_RQ)
62 nr = BLKDEV_MIN_RQ;
64 if (q->request_fn)
65 err = blk_update_nr_requests(q, nr);
66 else
67 err = blk_mq_update_nr_requests(q, nr);
69 if (err)
70 return err;
72 return ret;
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));
83 static ssize_t
84 queue_ra_store(struct request_queue *q, const char *page, size_t count)
86 unsigned long ra_kb;
87 ssize_t ret = queue_var_store(&ra_kb, page, count);
89 if (ret < 0)
90 return ret;
92 q->backing_dev_info.ra_pages = ra_kb >> (PAGE_CACHE_SHIFT - 10);
94 return ret;
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);
165 static ssize_t
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);
173 if (ret < 0)
174 return ret;
176 if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
177 return -EINVAL;
179 spin_lock_irq(q->queue_lock);
180 q->limits.max_sectors = max_sectors_kb << 1;
181 spin_unlock_irq(q->queue_lock);
183 return ret;
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) \
194 static ssize_t \
195 queue_show_##name(struct request_queue *q, char *page) \
197 int bit; \
198 bit = test_bit(QUEUE_FLAG_##flag, &q->queue_flags); \
199 return queue_var_show(neg ? !bit : bit, page); \
201 static ssize_t \
202 queue_store_##name(struct request_queue *q, const char *page, size_t count) \
204 unsigned long val; \
205 ssize_t ret; \
206 ret = queue_var_store(&val, page, count); \
207 if (ret < 0) \
208 return ret; \
209 if (neg) \
210 val = !val; \
212 spin_lock_irq(q->queue_lock); \
213 if (val) \
214 queue_flag_set(QUEUE_FLAG_##flag, q); \
215 else \
216 queue_flag_clear(QUEUE_FLAG_##flag, q); \
217 spin_unlock_irq(q->queue_lock); \
218 return ret; \
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,
233 size_t count)
235 unsigned long nm;
236 ssize_t ret = queue_var_store(&nm, page, count);
238 if (ret < 0)
239 return ret;
241 spin_lock_irq(q->queue_lock);
242 queue_flag_clear(QUEUE_FLAG_NOMERGES, q);
243 queue_flag_clear(QUEUE_FLAG_NOXMERGES, q);
244 if (nm == 2)
245 queue_flag_set(QUEUE_FLAG_NOMERGES, q);
246 else if (nm)
247 queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
248 spin_unlock_irq(q->queue_lock);
250 return ret;
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);
261 static ssize_t
262 queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count)
264 ssize_t ret = -EINVAL;
265 #ifdef CONFIG_SMP
266 unsigned long val;
268 ret = queue_var_store(&val, page, count);
269 if (ret < 0)
270 return ret;
272 spin_lock_irq(q->queue_lock);
273 if (val == 2) {
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);
284 #endif
285 return ret;
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,
430 NULL,
433 #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
435 static ssize_t
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);
441 ssize_t res;
443 if (!entry->show)
444 return -EIO;
445 mutex_lock(&q->sysfs_lock);
446 if (blk_queue_dying(q)) {
447 mutex_unlock(&q->sysfs_lock);
448 return -ENOENT;
450 res = entry->show(q, page);
451 mutex_unlock(&q->sysfs_lock);
452 return res;
455 static ssize_t
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;
461 ssize_t res;
463 if (!entry->store)
464 return -EIO;
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);
470 return -ENOENT;
472 res = entry->store(q, page, length);
473 mutex_unlock(&q->sysfs_lock);
474 return res;
477 static void blk_free_queue_rcu(struct rcu_head *rcu_head)
479 struct request_queue *q = container_of(rcu_head, struct request_queue,
480 rcu_head);
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
488 * Description:
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.
495 * Caveat:
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);
504 blk_sync_queue(q);
506 blkcg_exit_queue(q);
508 if (q->elevator) {
509 spin_lock_irq(q->queue_lock);
510 ioc_clear_queue(q);
511 spin_unlock_irq(q->queue_lock);
512 elevator_exit(q->elevator);
515 blk_exit_rl(&q->root_rl);
517 if (q->queue_tags)
518 __blk_queue_free_tags(q);
520 if (q->mq_ops)
521 blk_mq_free_queue(q);
523 kfree(q->flush_rq);
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)
546 int ret;
547 struct device *dev = disk_to_dev(disk);
548 struct request_queue *q = disk->queue;
550 if (WARN_ON(!q))
551 return -ENXIO;
554 * Initialization must be complete by now. Finish the initial
555 * bypass from queue allocation.
557 blk_queue_bypass_end(q);
558 queue_flag_set_unlocked(QUEUE_FLAG_INIT_DONE, q);
560 ret = blk_trace_init_sysfs(dev);
561 if (ret)
562 return ret;
564 ret = kobject_add(&q->kobj, kobject_get(&dev->kobj), "%s", "queue");
565 if (ret < 0) {
566 blk_trace_remove_sysfs(dev);
567 return ret;
570 kobject_uevent(&q->kobj, KOBJ_ADD);
572 if (q->mq_ops)
573 blk_mq_register_disk(disk);
575 if (!q->request_fn)
576 return 0;
578 ret = elv_register_queue(q);
579 if (ret) {
580 kobject_uevent(&q->kobj, KOBJ_REMOVE);
581 kobject_del(&q->kobj);
582 blk_trace_remove_sysfs(dev);
583 kobject_put(&dev->kobj);
584 return ret;
587 return 0;
590 void blk_unregister_queue(struct gendisk *disk)
592 struct request_queue *q = disk->queue;
594 if (WARN_ON(!q))
595 return;
597 if (q->mq_ops)
598 blk_mq_unregister_disk(disk);
600 if (q->request_fn)
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);