blob: a29cb788e408e720b0b8023fca95a9f3d26a3e8c [file] [log] [blame]
Jens Axboe8324aa92008-01-29 14:51:59 +01001/*
2 * Functions related to sysfs handling
3 */
4#include <linux/kernel.h>
5#include <linux/module.h>
6#include <linux/bio.h>
7#include <linux/blkdev.h>
8#include <linux/blktrace_api.h>
9
10#include "blk.h"
11
12struct 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);
16};
17
18static ssize_t
19queue_var_show(unsigned int var, char *page)
20{
21 return sprintf(page, "%d\n", var);
22}
23
24static ssize_t
25queue_var_store(unsigned long *var, const char *page, size_t count)
26{
27 char *p = (char *) page;
28
29 *var = simple_strtoul(p, &p, 10);
30 return count;
31}
32
33static ssize_t queue_requests_show(struct request_queue *q, char *page)
34{
35 return queue_var_show(q->nr_requests, (page));
36}
37
38static ssize_t
39queue_requests_store(struct request_queue *q, const char *page, size_t count)
40{
41 struct request_list *rl = &q->rq;
42 unsigned long nr;
43 int ret = queue_var_store(&nr, page, count);
44 if (nr < BLKDEV_MIN_RQ)
45 nr = BLKDEV_MIN_RQ;
46
47 spin_lock_irq(q->queue_lock);
48 q->nr_requests = nr;
49 blk_queue_congestion_threshold(q);
50
51 if (rl->count[READ] >= queue_congestion_on_threshold(q))
52 blk_set_queue_congested(q, READ);
53 else if (rl->count[READ] < queue_congestion_off_threshold(q))
54 blk_clear_queue_congested(q, READ);
55
56 if (rl->count[WRITE] >= queue_congestion_on_threshold(q))
57 blk_set_queue_congested(q, WRITE);
58 else if (rl->count[WRITE] < queue_congestion_off_threshold(q))
59 blk_clear_queue_congested(q, WRITE);
60
61 if (rl->count[READ] >= q->nr_requests) {
62 blk_set_queue_full(q, READ);
63 } else if (rl->count[READ]+1 <= q->nr_requests) {
64 blk_clear_queue_full(q, READ);
65 wake_up(&rl->wait[READ]);
66 }
67
68 if (rl->count[WRITE] >= q->nr_requests) {
69 blk_set_queue_full(q, WRITE);
70 } else if (rl->count[WRITE]+1 <= q->nr_requests) {
71 blk_clear_queue_full(q, WRITE);
72 wake_up(&rl->wait[WRITE]);
73 }
74 spin_unlock_irq(q->queue_lock);
75 return ret;
76}
77
78static ssize_t queue_ra_show(struct request_queue *q, char *page)
79{
80 int ra_kb = q->backing_dev_info.ra_pages << (PAGE_CACHE_SHIFT - 10);
81
82 return queue_var_show(ra_kb, (page));
83}
84
85static ssize_t
86queue_ra_store(struct request_queue *q, const char *page, size_t count)
87{
88 unsigned long ra_kb;
89 ssize_t ret = queue_var_store(&ra_kb, page, count);
90
Jens Axboe8324aa92008-01-29 14:51:59 +010091 q->backing_dev_info.ra_pages = ra_kb >> (PAGE_CACHE_SHIFT - 10);
Jens Axboe8324aa92008-01-29 14:51:59 +010092
93 return ret;
94}
95
96static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
97{
98 int max_sectors_kb = q->max_sectors >> 1;
99
100 return queue_var_show(max_sectors_kb, (page));
101}
102
Martin K. Petersene68b9032008-01-29 19:14:08 +0100103static ssize_t queue_hw_sector_size_show(struct request_queue *q, char *page)
104{
105 return queue_var_show(q->hardsect_size, page);
106}
107
Jens Axboe8324aa92008-01-29 14:51:59 +0100108static ssize_t
109queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
110{
111 unsigned long max_sectors_kb,
112 max_hw_sectors_kb = q->max_hw_sectors >> 1,
113 page_kb = 1 << (PAGE_CACHE_SHIFT - 10);
114 ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
115
116 if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
117 return -EINVAL;
Wu Fengguang7c239512008-11-25 09:08:39 +0100118
Jens Axboe8324aa92008-01-29 14:51:59 +0100119 spin_lock_irq(q->queue_lock);
120 q->max_sectors = max_sectors_kb << 1;
121 spin_unlock_irq(q->queue_lock);
122
123 return ret;
124}
125
126static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
127{
128 int max_hw_sectors_kb = q->max_hw_sectors >> 1;
129
130 return queue_var_show(max_hw_sectors_kb, (page));
131}
132
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200133static ssize_t queue_nomerges_show(struct request_queue *q, char *page)
134{
135 return queue_var_show(blk_queue_nomerges(q), page);
136}
137
138static ssize_t queue_nomerges_store(struct request_queue *q, const char *page,
139 size_t count)
140{
141 unsigned long nm;
142 ssize_t ret = queue_var_store(&nm, page, count);
143
Jens Axboebf0f9702008-05-07 09:09:39 +0200144 spin_lock_irq(q->queue_lock);
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200145 if (nm)
Jens Axboebf0f9702008-05-07 09:09:39 +0200146 queue_flag_set(QUEUE_FLAG_NOMERGES, q);
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200147 else
Jens Axboebf0f9702008-05-07 09:09:39 +0200148 queue_flag_clear(QUEUE_FLAG_NOMERGES, q);
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200149
Jens Axboebf0f9702008-05-07 09:09:39 +0200150 spin_unlock_irq(q->queue_lock);
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200151 return ret;
152}
153
Jens Axboec7c22e42008-09-13 20:26:01 +0200154static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page)
155{
156 unsigned int set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags);
157
158 return queue_var_show(set != 0, page);
159}
160
161static ssize_t
162queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count)
163{
164 ssize_t ret = -EINVAL;
165#if defined(CONFIG_USE_GENERIC_SMP_HELPERS)
166 unsigned long val;
167
168 ret = queue_var_store(&val, page, count);
169 spin_lock_irq(q->queue_lock);
170 if (val)
171 queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
172 else
173 queue_flag_clear(QUEUE_FLAG_SAME_COMP, q);
174 spin_unlock_irq(q->queue_lock);
175#endif
176 return ret;
177}
Jens Axboe8324aa92008-01-29 14:51:59 +0100178
179static struct queue_sysfs_entry queue_requests_entry = {
180 .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
181 .show = queue_requests_show,
182 .store = queue_requests_store,
183};
184
185static struct queue_sysfs_entry queue_ra_entry = {
186 .attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR },
187 .show = queue_ra_show,
188 .store = queue_ra_store,
189};
190
191static struct queue_sysfs_entry queue_max_sectors_entry = {
192 .attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR },
193 .show = queue_max_sectors_show,
194 .store = queue_max_sectors_store,
195};
196
197static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
198 .attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO },
199 .show = queue_max_hw_sectors_show,
200};
201
202static struct queue_sysfs_entry queue_iosched_entry = {
203 .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR },
204 .show = elv_iosched_show,
205 .store = elv_iosched_store,
206};
207
Martin K. Petersene68b9032008-01-29 19:14:08 +0100208static struct queue_sysfs_entry queue_hw_sector_size_entry = {
209 .attr = {.name = "hw_sector_size", .mode = S_IRUGO },
210 .show = queue_hw_sector_size_show,
211};
212
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200213static struct queue_sysfs_entry queue_nomerges_entry = {
214 .attr = {.name = "nomerges", .mode = S_IRUGO | S_IWUSR },
215 .show = queue_nomerges_show,
216 .store = queue_nomerges_store,
217};
218
Jens Axboec7c22e42008-09-13 20:26:01 +0200219static struct queue_sysfs_entry queue_rq_affinity_entry = {
220 .attr = {.name = "rq_affinity", .mode = S_IRUGO | S_IWUSR },
221 .show = queue_rq_affinity_show,
222 .store = queue_rq_affinity_store,
223};
224
Jens Axboe8324aa92008-01-29 14:51:59 +0100225static struct attribute *default_attrs[] = {
226 &queue_requests_entry.attr,
227 &queue_ra_entry.attr,
228 &queue_max_hw_sectors_entry.attr,
229 &queue_max_sectors_entry.attr,
230 &queue_iosched_entry.attr,
Martin K. Petersene68b9032008-01-29 19:14:08 +0100231 &queue_hw_sector_size_entry.attr,
Alan D. Brunelleac9fafa2008-04-29 14:44:19 +0200232 &queue_nomerges_entry.attr,
Jens Axboec7c22e42008-09-13 20:26:01 +0200233 &queue_rq_affinity_entry.attr,
Jens Axboe8324aa92008-01-29 14:51:59 +0100234 NULL,
235};
236
237#define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
238
239static ssize_t
240queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
241{
242 struct queue_sysfs_entry *entry = to_queue(attr);
243 struct request_queue *q =
244 container_of(kobj, struct request_queue, kobj);
245 ssize_t res;
246
247 if (!entry->show)
248 return -EIO;
249 mutex_lock(&q->sysfs_lock);
250 if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) {
251 mutex_unlock(&q->sysfs_lock);
252 return -ENOENT;
253 }
254 res = entry->show(q, page);
255 mutex_unlock(&q->sysfs_lock);
256 return res;
257}
258
259static ssize_t
260queue_attr_store(struct kobject *kobj, struct attribute *attr,
261 const char *page, size_t length)
262{
263 struct queue_sysfs_entry *entry = to_queue(attr);
Jens Axboe6728cb02008-01-31 13:03:55 +0100264 struct request_queue *q;
Jens Axboe8324aa92008-01-29 14:51:59 +0100265 ssize_t res;
266
267 if (!entry->store)
268 return -EIO;
Jens Axboe6728cb02008-01-31 13:03:55 +0100269
270 q = container_of(kobj, struct request_queue, kobj);
Jens Axboe8324aa92008-01-29 14:51:59 +0100271 mutex_lock(&q->sysfs_lock);
272 if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) {
273 mutex_unlock(&q->sysfs_lock);
274 return -ENOENT;
275 }
276 res = entry->store(q, page, length);
277 mutex_unlock(&q->sysfs_lock);
278 return res;
279}
280
281/**
282 * blk_cleanup_queue: - release a &struct request_queue when it is no longer needed
283 * @kobj: the kobj belonging of the request queue to be released
284 *
285 * Description:
286 * blk_cleanup_queue is the pair to blk_init_queue() or
287 * blk_queue_make_request(). It should be called when a request queue is
288 * being released; typically when a block device is being de-registered.
289 * Currently, its primary task it to free all the &struct request
290 * structures that were allocated to the queue and the queue itself.
291 *
292 * Caveat:
293 * Hopefully the low level driver will have finished any
294 * outstanding requests first...
295 **/
296static void blk_release_queue(struct kobject *kobj)
297{
298 struct request_queue *q =
299 container_of(kobj, struct request_queue, kobj);
300 struct request_list *rl = &q->rq;
301
302 blk_sync_queue(q);
303
304 if (rl->rq_pool)
305 mempool_destroy(rl->rq_pool);
306
307 if (q->queue_tags)
308 __blk_queue_free_tags(q);
309
310 blk_trace_shutdown(q);
311
312 bdi_destroy(&q->backing_dev_info);
313 kmem_cache_free(blk_requestq_cachep, q);
314}
315
316static struct sysfs_ops queue_sysfs_ops = {
317 .show = queue_attr_show,
318 .store = queue_attr_store,
319};
320
321struct kobj_type blk_queue_ktype = {
322 .sysfs_ops = &queue_sysfs_ops,
323 .default_attrs = default_attrs,
324 .release = blk_release_queue,
325};
326
327int blk_register_queue(struct gendisk *disk)
328{
329 int ret;
330
331 struct request_queue *q = disk->queue;
332
Akinobu Mitafb199742008-04-21 09:51:06 +0200333 if (WARN_ON(!q))
Jens Axboe8324aa92008-01-29 14:51:59 +0100334 return -ENXIO;
335
Akinobu Mitafb199742008-04-21 09:51:06 +0200336 if (!q->request_fn)
337 return 0;
338
Tejun Heoed9e1982008-08-25 19:56:05 +0900339 ret = kobject_add(&q->kobj, kobject_get(&disk_to_dev(disk)->kobj),
Jens Axboe8324aa92008-01-29 14:51:59 +0100340 "%s", "queue");
341 if (ret < 0)
342 return ret;
343
344 kobject_uevent(&q->kobj, KOBJ_ADD);
345
346 ret = elv_register_queue(q);
347 if (ret) {
348 kobject_uevent(&q->kobj, KOBJ_REMOVE);
349 kobject_del(&q->kobj);
350 return ret;
351 }
352
353 return 0;
354}
355
356void blk_unregister_queue(struct gendisk *disk)
357{
358 struct request_queue *q = disk->queue;
359
Akinobu Mitafb199742008-04-21 09:51:06 +0200360 if (WARN_ON(!q))
361 return;
362
363 if (q->request_fn) {
Jens Axboe8324aa92008-01-29 14:51:59 +0100364 elv_unregister_queue(q);
365
366 kobject_uevent(&q->kobj, KOBJ_REMOVE);
367 kobject_del(&q->kobj);
Tejun Heoed9e1982008-08-25 19:56:05 +0900368 kobject_put(&disk_to_dev(disk)->kobj);
Jens Axboe8324aa92008-01-29 14:51:59 +0100369 }
370}