ea51d827a0bb5cd7064b485d3b1dbe13e841c34d
[linux-3.10.git] / block / blk-sysfs.c
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
11 #include "blk.h"
12 #include "blk-cgroup.h"
13
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);
18 };
19
20 static ssize_t
21 queue_var_show(unsigned long var, char *page)
22 {
23         return sprintf(page, "%lu\n", var);
24 }
25
26 static ssize_t
27 queue_var_store(unsigned long *var, const char *page, size_t count)
28 {
29         int err;
30         unsigned long v;
31
32         err = strict_strtoul(page, 10, &v);
33         if (err || v > UINT_MAX)
34                 return -EINVAL;
35
36         *var = v;
37
38         return count;
39 }
40
41 static ssize_t queue_requests_show(struct request_queue *q, char *page)
42 {
43         return queue_var_show(q->nr_requests, (page));
44 }
45
46 static ssize_t
47 queue_requests_store(struct request_queue *q, const char *page, size_t count)
48 {
49         struct request_list *rl;
50         unsigned long nr;
51         int ret;
52
53         if (!q->request_fn)
54                 return -EINVAL;
55
56         ret = queue_var_store(&nr, page, count);
57         if (ret < 0)
58                 return ret;
59
60         if (nr < BLKDEV_MIN_RQ)
61                 nr = BLKDEV_MIN_RQ;
62
63         spin_lock_irq(q->queue_lock);
64         q->nr_requests = nr;
65         blk_queue_congestion_threshold(q);
66
67         /* congestion isn't cgroup aware and follows root blkcg for now */
68         rl = &q->root_rl;
69
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);
74
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);
79
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);
83                 } else {
84                         blk_clear_rl_full(rl, BLK_RW_SYNC);
85                         wake_up(&rl->wait[BLK_RW_SYNC]);
86                 }
87
88                 if (rl->count[BLK_RW_ASYNC] >= q->nr_requests) {
89                         blk_set_rl_full(rl, BLK_RW_ASYNC);
90                 } else {
91                         blk_clear_rl_full(rl, BLK_RW_ASYNC);
92                         wake_up(&rl->wait[BLK_RW_ASYNC]);
93                 }
94         }
95
96         spin_unlock_irq(q->queue_lock);
97         return ret;
98 }
99
100 static ssize_t queue_ra_show(struct request_queue *q, char *page)
101 {
102         unsigned long ra_kb = q->backing_dev_info.ra_pages <<
103                                         (PAGE_CACHE_SHIFT - 10);
104
105         return queue_var_show(ra_kb, (page));
106 }
107
108 static ssize_t
109 queue_ra_store(struct request_queue *q, const char *page, size_t count)
110 {
111         unsigned long ra_kb;
112         ssize_t ret = queue_var_store(&ra_kb, page, count);
113
114         if (ret < 0)
115                 return ret;
116
117         q->backing_dev_info.ra_pages = ra_kb >> (PAGE_CACHE_SHIFT - 10);
118
119         return ret;
120 }
121
122 static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
123 {
124         int max_sectors_kb = queue_max_sectors(q) >> 1;
125
126         return queue_var_show(max_sectors_kb, (page));
127 }
128
129 static ssize_t queue_max_segments_show(struct request_queue *q, char *page)
130 {
131         return queue_var_show(queue_max_segments(q), (page));
132 }
133
134 static ssize_t queue_max_integrity_segments_show(struct request_queue *q, char *page)
135 {
136         return queue_var_show(q->limits.max_integrity_segments, (page));
137 }
138
139 static ssize_t queue_max_segment_size_show(struct request_queue *q, char *page)
140 {
141         if (blk_queue_cluster(q))
142                 return queue_var_show(queue_max_segment_size(q), (page));
143
144         return queue_var_show(PAGE_CACHE_SIZE, (page));
145 }
146
147 static ssize_t queue_logical_block_size_show(struct request_queue *q, char *page)
148 {
149         return queue_var_show(queue_logical_block_size(q), page);
150 }
151
152 static ssize_t queue_physical_block_size_show(struct request_queue *q, char *page)
153 {
154         return queue_var_show(queue_physical_block_size(q), page);
155 }
156
157 static ssize_t queue_io_min_show(struct request_queue *q, char *page)
158 {
159         return queue_var_show(queue_io_min(q), page);
160 }
161
162 static ssize_t queue_io_opt_show(struct request_queue *q, char *page)
163 {
164         return queue_var_show(queue_io_opt(q), page);
165 }
166
167 static ssize_t queue_discard_granularity_show(struct request_queue *q, char *page)
168 {
169         return queue_var_show(q->limits.discard_granularity, page);
170 }
171
172 static ssize_t queue_discard_max_show(struct request_queue *q, char *page)
173 {
174         return sprintf(page, "%llu\n",
175                        (unsigned long long)q->limits.max_discard_sectors << 9);
176 }
177
178 static ssize_t queue_discard_zeroes_data_show(struct request_queue *q, char *page)
179 {
180         return queue_var_show(queue_discard_zeroes_data(q), page);
181 }
182
183 static ssize_t
184 queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
185 {
186         unsigned long max_sectors_kb,
187                 max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1,
188                         page_kb = 1 << (PAGE_CACHE_SHIFT - 10);
189         ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
190
191         if (ret < 0)
192                 return ret;
193
194         if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
195                 return -EINVAL;
196
197         spin_lock_irq(q->queue_lock);
198         q->limits.max_sectors = max_sectors_kb << 1;
199         spin_unlock_irq(q->queue_lock);
200
201         return ret;
202 }
203
204 static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
205 {
206         int max_hw_sectors_kb = queue_max_hw_sectors(q) >> 1;
207
208         return queue_var_show(max_hw_sectors_kb, (page));
209 }
210
211 #define QUEUE_SYSFS_BIT_FNS(name, flag, neg)                            \
212 static ssize_t                                                          \
213 queue_show_##name(struct request_queue *q, char *page)                  \
214 {                                                                       \
215         int bit;                                                        \
216         bit = test_bit(QUEUE_FLAG_##flag, &q->queue_flags);             \
217         return queue_var_show(neg ? !bit : bit, page);                  \
218 }                                                                       \
219 static ssize_t                                                          \
220 queue_store_##name(struct request_queue *q, const char *page, size_t count) \
221 {                                                                       \
222         unsigned long val;                                              \
223         ssize_t ret;                                                    \
224         ret = queue_var_store(&val, page, count);                       \
225         if (neg)                                                        \
226                 val = !val;                                             \
227                                                                         \
228         spin_lock_irq(q->queue_lock);                                   \
229         if (val)                                                        \
230                 queue_flag_set(QUEUE_FLAG_##flag, q);                   \
231         else                                                            \
232                 queue_flag_clear(QUEUE_FLAG_##flag, q);                 \
233         spin_unlock_irq(q->queue_lock);                                 \
234         return ret;                                                     \
235 }
236
237 QUEUE_SYSFS_BIT_FNS(nonrot, NONROT, 1);
238 QUEUE_SYSFS_BIT_FNS(random, ADD_RANDOM, 0);
239 QUEUE_SYSFS_BIT_FNS(iostats, IO_STAT, 0);
240 #undef QUEUE_SYSFS_BIT_FNS
241
242 static ssize_t queue_nomerges_show(struct request_queue *q, char *page)
243 {
244         return queue_var_show((blk_queue_nomerges(q) << 1) |
245                                blk_queue_noxmerges(q), page);
246 }
247
248 static ssize_t queue_nomerges_store(struct request_queue *q, const char *page,
249                                     size_t count)
250 {
251         unsigned long nm;
252         ssize_t ret = queue_var_store(&nm, page, count);
253
254         if (ret < 0)
255                 return ret;
256
257         spin_lock_irq(q->queue_lock);
258         queue_flag_clear(QUEUE_FLAG_NOMERGES, q);
259         queue_flag_clear(QUEUE_FLAG_NOXMERGES, q);
260         if (nm == 2)
261                 queue_flag_set(QUEUE_FLAG_NOMERGES, q);
262         else if (nm)
263                 queue_flag_set(QUEUE_FLAG_NOXMERGES, q);
264         spin_unlock_irq(q->queue_lock);
265
266         return ret;
267 }
268
269 static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page)
270 {
271         bool set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags);
272         bool force = test_bit(QUEUE_FLAG_SAME_FORCE, &q->queue_flags);
273
274         return queue_var_show(set << force, page);
275 }
276
277 static ssize_t
278 queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count)
279 {
280         ssize_t ret = -EINVAL;
281 #if defined(CONFIG_USE_GENERIC_SMP_HELPERS)
282         unsigned long val;
283
284         ret = queue_var_store(&val, page, count);
285         if (ret < 0)
286                 return ret;
287
288         spin_lock_irq(q->queue_lock);
289         if (val == 2) {
290                 queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
291                 queue_flag_set(QUEUE_FLAG_SAME_FORCE, q);
292         } else if (val == 1) {
293                 queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
294                 queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
295         } else if (val == 0) {
296                 queue_flag_clear(QUEUE_FLAG_SAME_COMP, q);
297                 queue_flag_clear(QUEUE_FLAG_SAME_FORCE, q);
298         }
299         spin_unlock_irq(q->queue_lock);
300 #endif
301         return ret;
302 }
303
304 static struct queue_sysfs_entry queue_requests_entry = {
305         .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
306         .show = queue_requests_show,
307         .store = queue_requests_store,
308 };
309
310 static struct queue_sysfs_entry queue_ra_entry = {
311         .attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR },
312         .show = queue_ra_show,
313         .store = queue_ra_store,
314 };
315
316 static struct queue_sysfs_entry queue_max_sectors_entry = {
317         .attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR },
318         .show = queue_max_sectors_show,
319         .store = queue_max_sectors_store,
320 };
321
322 static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
323         .attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO },
324         .show = queue_max_hw_sectors_show,
325 };
326
327 static struct queue_sysfs_entry queue_max_segments_entry = {
328         .attr = {.name = "max_segments", .mode = S_IRUGO },
329         .show = queue_max_segments_show,
330 };
331
332 static struct queue_sysfs_entry queue_max_integrity_segments_entry = {
333         .attr = {.name = "max_integrity_segments", .mode = S_IRUGO },
334         .show = queue_max_integrity_segments_show,
335 };
336
337 static struct queue_sysfs_entry queue_max_segment_size_entry = {
338         .attr = {.name = "max_segment_size", .mode = S_IRUGO },
339         .show = queue_max_segment_size_show,
340 };
341
342 static struct queue_sysfs_entry queue_iosched_entry = {
343         .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR },
344         .show = elv_iosched_show,
345         .store = elv_iosched_store,
346 };
347
348 static struct queue_sysfs_entry queue_hw_sector_size_entry = {
349         .attr = {.name = "hw_sector_size", .mode = S_IRUGO },
350         .show = queue_logical_block_size_show,
351 };
352
353 static struct queue_sysfs_entry queue_logical_block_size_entry = {
354         .attr = {.name = "logical_block_size", .mode = S_IRUGO },
355         .show = queue_logical_block_size_show,
356 };
357
358 static struct queue_sysfs_entry queue_physical_block_size_entry = {
359         .attr = {.name = "physical_block_size", .mode = S_IRUGO },
360         .show = queue_physical_block_size_show,
361 };
362
363 static struct queue_sysfs_entry queue_io_min_entry = {
364         .attr = {.name = "minimum_io_size", .mode = S_IRUGO },
365         .show = queue_io_min_show,
366 };
367
368 static struct queue_sysfs_entry queue_io_opt_entry = {
369         .attr = {.name = "optimal_io_size", .mode = S_IRUGO },
370         .show = queue_io_opt_show,
371 };
372
373 static struct queue_sysfs_entry queue_discard_granularity_entry = {
374         .attr = {.name = "discard_granularity", .mode = S_IRUGO },
375         .show = queue_discard_granularity_show,
376 };
377
378 static struct queue_sysfs_entry queue_discard_max_entry = {
379         .attr = {.name = "discard_max_bytes", .mode = S_IRUGO },
380         .show = queue_discard_max_show,
381 };
382
383 static struct queue_sysfs_entry queue_discard_zeroes_data_entry = {
384         .attr = {.name = "discard_zeroes_data", .mode = S_IRUGO },
385         .show = queue_discard_zeroes_data_show,
386 };
387
388 static struct queue_sysfs_entry queue_nonrot_entry = {
389         .attr = {.name = "rotational", .mode = S_IRUGO | S_IWUSR },
390         .show = queue_show_nonrot,
391         .store = queue_store_nonrot,
392 };
393
394 static struct queue_sysfs_entry queue_nomerges_entry = {
395         .attr = {.name = "nomerges", .mode = S_IRUGO | S_IWUSR },
396         .show = queue_nomerges_show,
397         .store = queue_nomerges_store,
398 };
399
400 static struct queue_sysfs_entry queue_rq_affinity_entry = {
401         .attr = {.name = "rq_affinity", .mode = S_IRUGO | S_IWUSR },
402         .show = queue_rq_affinity_show,
403         .store = queue_rq_affinity_store,
404 };
405
406 static struct queue_sysfs_entry queue_iostats_entry = {
407         .attr = {.name = "iostats", .mode = S_IRUGO | S_IWUSR },
408         .show = queue_show_iostats,
409         .store = queue_store_iostats,
410 };
411
412 static struct queue_sysfs_entry queue_random_entry = {
413         .attr = {.name = "add_random", .mode = S_IRUGO | S_IWUSR },
414         .show = queue_show_random,
415         .store = queue_store_random,
416 };
417
418 static struct attribute *default_attrs[] = {
419         &queue_requests_entry.attr,
420         &queue_ra_entry.attr,
421         &queue_max_hw_sectors_entry.attr,
422         &queue_max_sectors_entry.attr,
423         &queue_max_segments_entry.attr,
424         &queue_max_integrity_segments_entry.attr,
425         &queue_max_segment_size_entry.attr,
426         &queue_iosched_entry.attr,
427         &queue_hw_sector_size_entry.attr,
428         &queue_logical_block_size_entry.attr,
429         &queue_physical_block_size_entry.attr,
430         &queue_io_min_entry.attr,
431         &queue_io_opt_entry.attr,
432         &queue_discard_granularity_entry.attr,
433         &queue_discard_max_entry.attr,
434         &queue_discard_zeroes_data_entry.attr,
435         &queue_nonrot_entry.attr,
436         &queue_nomerges_entry.attr,
437         &queue_rq_affinity_entry.attr,
438         &queue_iostats_entry.attr,
439         &queue_random_entry.attr,
440         NULL,
441 };
442
443 #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
444
445 static ssize_t
446 queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
447 {
448         struct queue_sysfs_entry *entry = to_queue(attr);
449         struct request_queue *q =
450                 container_of(kobj, struct request_queue, kobj);
451         ssize_t res;
452
453         if (!entry->show)
454                 return -EIO;
455         mutex_lock(&q->sysfs_lock);
456         if (blk_queue_dead(q)) {
457                 mutex_unlock(&q->sysfs_lock);
458                 return -ENOENT;
459         }
460         res = entry->show(q, page);
461         mutex_unlock(&q->sysfs_lock);
462         return res;
463 }
464
465 static ssize_t
466 queue_attr_store(struct kobject *kobj, struct attribute *attr,
467                     const char *page, size_t length)
468 {
469         struct queue_sysfs_entry *entry = to_queue(attr);
470         struct request_queue *q;
471         ssize_t res;
472
473         if (!entry->store)
474                 return -EIO;
475
476         q = container_of(kobj, struct request_queue, kobj);
477         mutex_lock(&q->sysfs_lock);
478         if (blk_queue_dead(q)) {
479                 mutex_unlock(&q->sysfs_lock);
480                 return -ENOENT;
481         }
482         res = entry->store(q, page, length);
483         mutex_unlock(&q->sysfs_lock);
484         return res;
485 }
486
487 /**
488  * blk_release_queue: - release a &struct request_queue when it is no longer needed
489  * @kobj:    the kobj belonging to the request queue to be released
490  *
491  * Description:
492  *     blk_release_queue is the pair to blk_init_queue() or
493  *     blk_queue_make_request().  It should be called when a request queue is
494  *     being released; typically when a block device is being de-registered.
495  *     Currently, its primary task it to free all the &struct request
496  *     structures that were allocated to the queue and the queue itself.
497  *
498  * Caveat:
499  *     Hopefully the low level driver will have finished any
500  *     outstanding requests first...
501  **/
502 static void blk_release_queue(struct kobject *kobj)
503 {
504         struct request_queue *q =
505                 container_of(kobj, struct request_queue, kobj);
506
507         blk_sync_queue(q);
508
509         blkcg_exit_queue(q);
510
511         if (q->elevator) {
512                 spin_lock_irq(q->queue_lock);
513                 ioc_clear_queue(q);
514                 spin_unlock_irq(q->queue_lock);
515                 elevator_exit(q->elevator);
516         }
517
518         blk_exit_rl(&q->root_rl);
519
520         if (q->queue_tags)
521                 __blk_queue_free_tags(q);
522
523         blk_trace_shutdown(q);
524
525         bdi_destroy(&q->backing_dev_info);
526
527         ida_simple_remove(&blk_queue_ida, q->id);
528         kmem_cache_free(blk_requestq_cachep, q);
529 }
530
531 static const struct sysfs_ops queue_sysfs_ops = {
532         .show   = queue_attr_show,
533         .store  = queue_attr_store,
534 };
535
536 struct kobj_type blk_queue_ktype = {
537         .sysfs_ops      = &queue_sysfs_ops,
538         .default_attrs  = default_attrs,
539         .release        = blk_release_queue,
540 };
541
542 int blk_register_queue(struct gendisk *disk)
543 {
544         int ret;
545         struct device *dev = disk_to_dev(disk);
546         struct request_queue *q = disk->queue;
547
548         if (WARN_ON(!q))
549                 return -ENXIO;
550
551         ret = blk_trace_init_sysfs(dev);
552         if (ret)
553                 return ret;
554
555         ret = kobject_add(&q->kobj, kobject_get(&dev->kobj), "%s", "queue");
556         if (ret < 0) {
557                 blk_trace_remove_sysfs(dev);
558                 return ret;
559         }
560
561         kobject_uevent(&q->kobj, KOBJ_ADD);
562
563         if (!q->request_fn)
564                 return 0;
565
566         ret = elv_register_queue(q);
567         if (ret) {
568                 kobject_uevent(&q->kobj, KOBJ_REMOVE);
569                 kobject_del(&q->kobj);
570                 blk_trace_remove_sysfs(dev);
571                 kobject_put(&dev->kobj);
572                 return ret;
573         }
574
575         return 0;
576 }
577
578 void blk_unregister_queue(struct gendisk *disk)
579 {
580         struct request_queue *q = disk->queue;
581
582         if (WARN_ON(!q))
583                 return;
584
585         if (q->request_fn)
586                 elv_unregister_queue(q);
587
588         kobject_uevent(&q->kobj, KOBJ_REMOVE);
589         kobject_del(&q->kobj);
590         blk_trace_remove_sysfs(disk_to_dev(disk));
591         kobject_put(&disk_to_dev(disk)->kobj);
592 }