HID: add compat support
[linux-2.6/next.git] / block / blk-sysfs.c
blob21e275d7eed9444ab876834db2345eb78f4ae183
1 /*
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>
10 #include "blk.h"
12 struct 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);
18 static ssize_t
19 queue_var_show(unsigned int var, char *page)
21 return sprintf(page, "%d\n", var);
24 static ssize_t
25 queue_var_store(unsigned long *var, const char *page, size_t count)
27 char *p = (char *) page;
29 *var = simple_strtoul(p, &p, 10);
30 return count;
33 static ssize_t queue_requests_show(struct request_queue *q, char *page)
35 return queue_var_show(q->nr_requests, (page));
38 static ssize_t
39 queue_requests_store(struct request_queue *q, const char *page, size_t count)
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;
47 spin_lock_irq(q->queue_lock);
48 q->nr_requests = nr;
49 blk_queue_congestion_threshold(q);
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);
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);
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]);
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]);
74 spin_unlock_irq(q->queue_lock);
75 return ret;
78 static ssize_t queue_ra_show(struct request_queue *q, char *page)
80 int ra_kb = q->backing_dev_info.ra_pages << (PAGE_CACHE_SHIFT - 10);
82 return queue_var_show(ra_kb, (page));
85 static ssize_t
86 queue_ra_store(struct request_queue *q, const char *page, size_t count)
88 unsigned long ra_kb;
89 ssize_t ret = queue_var_store(&ra_kb, page, count);
91 spin_lock_irq(q->queue_lock);
92 q->backing_dev_info.ra_pages = ra_kb >> (PAGE_CACHE_SHIFT - 10);
93 spin_unlock_irq(q->queue_lock);
95 return ret;
98 static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
100 int max_sectors_kb = q->max_sectors >> 1;
102 return queue_var_show(max_sectors_kb, (page));
105 static ssize_t queue_hw_sector_size_show(struct request_queue *q, char *page)
107 return queue_var_show(q->hardsect_size, page);
110 static ssize_t
111 queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
113 unsigned long max_sectors_kb,
114 max_hw_sectors_kb = q->max_hw_sectors >> 1,
115 page_kb = 1 << (PAGE_CACHE_SHIFT - 10);
116 ssize_t ret = queue_var_store(&max_sectors_kb, page, count);
118 if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
119 return -EINVAL;
121 * Take the queue lock to update the readahead and max_sectors
122 * values synchronously:
124 spin_lock_irq(q->queue_lock);
125 q->max_sectors = max_sectors_kb << 1;
126 spin_unlock_irq(q->queue_lock);
128 return ret;
131 static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
133 int max_hw_sectors_kb = q->max_hw_sectors >> 1;
135 return queue_var_show(max_hw_sectors_kb, (page));
138 static ssize_t queue_nomerges_show(struct request_queue *q, char *page)
140 return queue_var_show(blk_queue_nomerges(q), page);
143 static ssize_t queue_nomerges_store(struct request_queue *q, const char *page,
144 size_t count)
146 unsigned long nm;
147 ssize_t ret = queue_var_store(&nm, page, count);
149 spin_lock_irq(q->queue_lock);
150 if (nm)
151 queue_flag_set(QUEUE_FLAG_NOMERGES, q);
152 else
153 queue_flag_clear(QUEUE_FLAG_NOMERGES, q);
155 spin_unlock_irq(q->queue_lock);
156 return ret;
159 static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page)
161 unsigned int set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags);
163 return queue_var_show(set != 0, page);
166 static ssize_t
167 queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count)
169 ssize_t ret = -EINVAL;
170 #if defined(CONFIG_USE_GENERIC_SMP_HELPERS)
171 unsigned long val;
173 ret = queue_var_store(&val, page, count);
174 spin_lock_irq(q->queue_lock);
175 if (val)
176 queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
177 else
178 queue_flag_clear(QUEUE_FLAG_SAME_COMP, q);
179 spin_unlock_irq(q->queue_lock);
180 #endif
181 return ret;
184 static struct queue_sysfs_entry queue_requests_entry = {
185 .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
186 .show = queue_requests_show,
187 .store = queue_requests_store,
190 static struct queue_sysfs_entry queue_ra_entry = {
191 .attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR },
192 .show = queue_ra_show,
193 .store = queue_ra_store,
196 static struct queue_sysfs_entry queue_max_sectors_entry = {
197 .attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR },
198 .show = queue_max_sectors_show,
199 .store = queue_max_sectors_store,
202 static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
203 .attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO },
204 .show = queue_max_hw_sectors_show,
207 static struct queue_sysfs_entry queue_iosched_entry = {
208 .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR },
209 .show = elv_iosched_show,
210 .store = elv_iosched_store,
213 static struct queue_sysfs_entry queue_hw_sector_size_entry = {
214 .attr = {.name = "hw_sector_size", .mode = S_IRUGO },
215 .show = queue_hw_sector_size_show,
218 static struct queue_sysfs_entry queue_nomerges_entry = {
219 .attr = {.name = "nomerges", .mode = S_IRUGO | S_IWUSR },
220 .show = queue_nomerges_show,
221 .store = queue_nomerges_store,
224 static struct queue_sysfs_entry queue_rq_affinity_entry = {
225 .attr = {.name = "rq_affinity", .mode = S_IRUGO | S_IWUSR },
226 .show = queue_rq_affinity_show,
227 .store = queue_rq_affinity_store,
230 static struct attribute *default_attrs[] = {
231 &queue_requests_entry.attr,
232 &queue_ra_entry.attr,
233 &queue_max_hw_sectors_entry.attr,
234 &queue_max_sectors_entry.attr,
235 &queue_iosched_entry.attr,
236 &queue_hw_sector_size_entry.attr,
237 &queue_nomerges_entry.attr,
238 &queue_rq_affinity_entry.attr,
239 NULL,
242 #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
244 static ssize_t
245 queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
247 struct queue_sysfs_entry *entry = to_queue(attr);
248 struct request_queue *q =
249 container_of(kobj, struct request_queue, kobj);
250 ssize_t res;
252 if (!entry->show)
253 return -EIO;
254 mutex_lock(&q->sysfs_lock);
255 if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) {
256 mutex_unlock(&q->sysfs_lock);
257 return -ENOENT;
259 res = entry->show(q, page);
260 mutex_unlock(&q->sysfs_lock);
261 return res;
264 static ssize_t
265 queue_attr_store(struct kobject *kobj, struct attribute *attr,
266 const char *page, size_t length)
268 struct queue_sysfs_entry *entry = to_queue(attr);
269 struct request_queue *q;
270 ssize_t res;
272 if (!entry->store)
273 return -EIO;
275 q = container_of(kobj, struct request_queue, kobj);
276 mutex_lock(&q->sysfs_lock);
277 if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) {
278 mutex_unlock(&q->sysfs_lock);
279 return -ENOENT;
281 res = entry->store(q, page, length);
282 mutex_unlock(&q->sysfs_lock);
283 return res;
287 * blk_cleanup_queue: - release a &struct request_queue when it is no longer needed
288 * @kobj: the kobj belonging of the request queue to be released
290 * Description:
291 * blk_cleanup_queue is the pair to blk_init_queue() or
292 * blk_queue_make_request(). It should be called when a request queue is
293 * being released; typically when a block device is being de-registered.
294 * Currently, its primary task it to free all the &struct request
295 * structures that were allocated to the queue and the queue itself.
297 * Caveat:
298 * Hopefully the low level driver will have finished any
299 * outstanding requests first...
301 static void blk_release_queue(struct kobject *kobj)
303 struct request_queue *q =
304 container_of(kobj, struct request_queue, kobj);
305 struct request_list *rl = &q->rq;
307 blk_sync_queue(q);
309 if (rl->rq_pool)
310 mempool_destroy(rl->rq_pool);
312 if (q->queue_tags)
313 __blk_queue_free_tags(q);
315 blk_trace_shutdown(q);
317 bdi_destroy(&q->backing_dev_info);
318 kmem_cache_free(blk_requestq_cachep, q);
321 static struct sysfs_ops queue_sysfs_ops = {
322 .show = queue_attr_show,
323 .store = queue_attr_store,
326 struct kobj_type blk_queue_ktype = {
327 .sysfs_ops = &queue_sysfs_ops,
328 .default_attrs = default_attrs,
329 .release = blk_release_queue,
332 int blk_register_queue(struct gendisk *disk)
334 int ret;
336 struct request_queue *q = disk->queue;
338 if (WARN_ON(!q))
339 return -ENXIO;
341 if (!q->request_fn)
342 return 0;
344 ret = kobject_add(&q->kobj, kobject_get(&disk_to_dev(disk)->kobj),
345 "%s", "queue");
346 if (ret < 0)
347 return ret;
349 kobject_uevent(&q->kobj, KOBJ_ADD);
351 ret = elv_register_queue(q);
352 if (ret) {
353 kobject_uevent(&q->kobj, KOBJ_REMOVE);
354 kobject_del(&q->kobj);
355 return ret;
358 return 0;
361 void blk_unregister_queue(struct gendisk *disk)
363 struct request_queue *q = disk->queue;
365 if (WARN_ON(!q))
366 return;
368 if (q->request_fn) {
369 elv_unregister_queue(q);
371 kobject_uevent(&q->kobj, KOBJ_REMOVE);
372 kobject_del(&q->kobj);
373 kobject_put(&disk_to_dev(disk)->kobj);