rndis_wlan: convert scan to cfg80211
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / block / blk-sysfs.c
blob73f36beff5cd58c85ca0111df6ea4061ab2d27c0
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[BLK_RW_SYNC] >= queue_congestion_on_threshold(q))
52 blk_set_queue_congested(q, BLK_RW_SYNC);
53 else if (rl->count[BLK_RW_SYNC] < queue_congestion_off_threshold(q))
54 blk_clear_queue_congested(q, BLK_RW_SYNC);
56 if (rl->count[BLK_RW_ASYNC] >= queue_congestion_on_threshold(q))
57 blk_set_queue_congested(q, BLK_RW_ASYNC);
58 else if (rl->count[BLK_RW_ASYNC] < queue_congestion_off_threshold(q))
59 blk_clear_queue_congested(q, BLK_RW_ASYNC);
61 if (rl->count[BLK_RW_SYNC] >= q->nr_requests) {
62 blk_set_queue_full(q, BLK_RW_SYNC);
63 } else if (rl->count[BLK_RW_SYNC]+1 <= q->nr_requests) {
64 blk_clear_queue_full(q, BLK_RW_SYNC);
65 wake_up(&rl->wait[BLK_RW_SYNC]);
68 if (rl->count[BLK_RW_ASYNC] >= q->nr_requests) {
69 blk_set_queue_full(q, BLK_RW_ASYNC);
70 } else if (rl->count[BLK_RW_ASYNC]+1 <= q->nr_requests) {
71 blk_clear_queue_full(q, BLK_RW_ASYNC);
72 wake_up(&rl->wait[BLK_RW_ASYNC]);
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 q->backing_dev_info.ra_pages = ra_kb >> (PAGE_CACHE_SHIFT - 10);
93 return ret;
96 static ssize_t queue_max_sectors_show(struct request_queue *q, char *page)
98 int max_sectors_kb = q->max_sectors >> 1;
100 return queue_var_show(max_sectors_kb, (page));
103 static ssize_t queue_hw_sector_size_show(struct request_queue *q, char *page)
105 return queue_var_show(q->hardsect_size, page);
108 static ssize_t
109 queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
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);
116 if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
117 return -EINVAL;
119 spin_lock_irq(q->queue_lock);
120 q->max_sectors = max_sectors_kb << 1;
121 spin_unlock_irq(q->queue_lock);
123 return ret;
126 static ssize_t queue_max_hw_sectors_show(struct request_queue *q, char *page)
128 int max_hw_sectors_kb = q->max_hw_sectors >> 1;
130 return queue_var_show(max_hw_sectors_kb, (page));
133 static ssize_t queue_nonrot_show(struct request_queue *q, char *page)
135 return queue_var_show(!blk_queue_nonrot(q), page);
138 static ssize_t queue_nonrot_store(struct request_queue *q, const char *page,
139 size_t count)
141 unsigned long nm;
142 ssize_t ret = queue_var_store(&nm, page, count);
144 spin_lock_irq(q->queue_lock);
145 if (nm)
146 queue_flag_clear(QUEUE_FLAG_NONROT, q);
147 else
148 queue_flag_set(QUEUE_FLAG_NONROT, q);
149 spin_unlock_irq(q->queue_lock);
151 return ret;
154 static ssize_t queue_nomerges_show(struct request_queue *q, char *page)
156 return queue_var_show(blk_queue_nomerges(q), page);
159 static ssize_t queue_nomerges_store(struct request_queue *q, const char *page,
160 size_t count)
162 unsigned long nm;
163 ssize_t ret = queue_var_store(&nm, page, count);
165 spin_lock_irq(q->queue_lock);
166 if (nm)
167 queue_flag_set(QUEUE_FLAG_NOMERGES, q);
168 else
169 queue_flag_clear(QUEUE_FLAG_NOMERGES, q);
170 spin_unlock_irq(q->queue_lock);
172 return ret;
175 static ssize_t queue_rq_affinity_show(struct request_queue *q, char *page)
177 unsigned int set = test_bit(QUEUE_FLAG_SAME_COMP, &q->queue_flags);
179 return queue_var_show(set != 0, page);
182 static ssize_t
183 queue_rq_affinity_store(struct request_queue *q, const char *page, size_t count)
185 ssize_t ret = -EINVAL;
186 #if defined(CONFIG_USE_GENERIC_SMP_HELPERS)
187 unsigned long val;
189 ret = queue_var_store(&val, page, count);
190 spin_lock_irq(q->queue_lock);
191 if (val)
192 queue_flag_set(QUEUE_FLAG_SAME_COMP, q);
193 else
194 queue_flag_clear(QUEUE_FLAG_SAME_COMP, q);
195 spin_unlock_irq(q->queue_lock);
196 #endif
197 return ret;
200 static ssize_t queue_iostats_show(struct request_queue *q, char *page)
202 return queue_var_show(blk_queue_io_stat(q), page);
205 static ssize_t queue_iostats_store(struct request_queue *q, const char *page,
206 size_t count)
208 unsigned long stats;
209 ssize_t ret = queue_var_store(&stats, page, count);
211 spin_lock_irq(q->queue_lock);
212 elv_quisce_start(q);
214 if (stats)
215 queue_flag_set(QUEUE_FLAG_IO_STAT, q);
216 else
217 queue_flag_clear(QUEUE_FLAG_IO_STAT, q);
219 elv_quisce_end(q);
220 spin_unlock_irq(q->queue_lock);
222 return ret;
225 static struct queue_sysfs_entry queue_requests_entry = {
226 .attr = {.name = "nr_requests", .mode = S_IRUGO | S_IWUSR },
227 .show = queue_requests_show,
228 .store = queue_requests_store,
231 static struct queue_sysfs_entry queue_ra_entry = {
232 .attr = {.name = "read_ahead_kb", .mode = S_IRUGO | S_IWUSR },
233 .show = queue_ra_show,
234 .store = queue_ra_store,
237 static struct queue_sysfs_entry queue_max_sectors_entry = {
238 .attr = {.name = "max_sectors_kb", .mode = S_IRUGO | S_IWUSR },
239 .show = queue_max_sectors_show,
240 .store = queue_max_sectors_store,
243 static struct queue_sysfs_entry queue_max_hw_sectors_entry = {
244 .attr = {.name = "max_hw_sectors_kb", .mode = S_IRUGO },
245 .show = queue_max_hw_sectors_show,
248 static struct queue_sysfs_entry queue_iosched_entry = {
249 .attr = {.name = "scheduler", .mode = S_IRUGO | S_IWUSR },
250 .show = elv_iosched_show,
251 .store = elv_iosched_store,
254 static struct queue_sysfs_entry queue_hw_sector_size_entry = {
255 .attr = {.name = "hw_sector_size", .mode = S_IRUGO },
256 .show = queue_hw_sector_size_show,
259 static struct queue_sysfs_entry queue_nonrot_entry = {
260 .attr = {.name = "rotational", .mode = S_IRUGO | S_IWUSR },
261 .show = queue_nonrot_show,
262 .store = queue_nonrot_store,
265 static struct queue_sysfs_entry queue_nomerges_entry = {
266 .attr = {.name = "nomerges", .mode = S_IRUGO | S_IWUSR },
267 .show = queue_nomerges_show,
268 .store = queue_nomerges_store,
271 static struct queue_sysfs_entry queue_rq_affinity_entry = {
272 .attr = {.name = "rq_affinity", .mode = S_IRUGO | S_IWUSR },
273 .show = queue_rq_affinity_show,
274 .store = queue_rq_affinity_store,
277 static struct queue_sysfs_entry queue_iostats_entry = {
278 .attr = {.name = "iostats", .mode = S_IRUGO | S_IWUSR },
279 .show = queue_iostats_show,
280 .store = queue_iostats_store,
283 static struct attribute *default_attrs[] = {
284 &queue_requests_entry.attr,
285 &queue_ra_entry.attr,
286 &queue_max_hw_sectors_entry.attr,
287 &queue_max_sectors_entry.attr,
288 &queue_iosched_entry.attr,
289 &queue_hw_sector_size_entry.attr,
290 &queue_nonrot_entry.attr,
291 &queue_nomerges_entry.attr,
292 &queue_rq_affinity_entry.attr,
293 &queue_iostats_entry.attr,
294 NULL,
297 #define to_queue(atr) container_of((atr), struct queue_sysfs_entry, attr)
299 static ssize_t
300 queue_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
302 struct queue_sysfs_entry *entry = to_queue(attr);
303 struct request_queue *q =
304 container_of(kobj, struct request_queue, kobj);
305 ssize_t res;
307 if (!entry->show)
308 return -EIO;
309 mutex_lock(&q->sysfs_lock);
310 if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) {
311 mutex_unlock(&q->sysfs_lock);
312 return -ENOENT;
314 res = entry->show(q, page);
315 mutex_unlock(&q->sysfs_lock);
316 return res;
319 static ssize_t
320 queue_attr_store(struct kobject *kobj, struct attribute *attr,
321 const char *page, size_t length)
323 struct queue_sysfs_entry *entry = to_queue(attr);
324 struct request_queue *q;
325 ssize_t res;
327 if (!entry->store)
328 return -EIO;
330 q = container_of(kobj, struct request_queue, kobj);
331 mutex_lock(&q->sysfs_lock);
332 if (test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)) {
333 mutex_unlock(&q->sysfs_lock);
334 return -ENOENT;
336 res = entry->store(q, page, length);
337 mutex_unlock(&q->sysfs_lock);
338 return res;
342 * blk_cleanup_queue: - release a &struct request_queue when it is no longer needed
343 * @kobj: the kobj belonging of the request queue to be released
345 * Description:
346 * blk_cleanup_queue is the pair to blk_init_queue() or
347 * blk_queue_make_request(). It should be called when a request queue is
348 * being released; typically when a block device is being de-registered.
349 * Currently, its primary task it to free all the &struct request
350 * structures that were allocated to the queue and the queue itself.
352 * Caveat:
353 * Hopefully the low level driver will have finished any
354 * outstanding requests first...
356 static void blk_release_queue(struct kobject *kobj)
358 struct request_queue *q =
359 container_of(kobj, struct request_queue, kobj);
360 struct request_list *rl = &q->rq;
362 blk_sync_queue(q);
364 if (rl->rq_pool)
365 mempool_destroy(rl->rq_pool);
367 if (q->queue_tags)
368 __blk_queue_free_tags(q);
370 blk_trace_shutdown(q);
372 bdi_destroy(&q->backing_dev_info);
373 kmem_cache_free(blk_requestq_cachep, q);
376 static struct sysfs_ops queue_sysfs_ops = {
377 .show = queue_attr_show,
378 .store = queue_attr_store,
381 struct kobj_type blk_queue_ktype = {
382 .sysfs_ops = &queue_sysfs_ops,
383 .default_attrs = default_attrs,
384 .release = blk_release_queue,
387 int blk_register_queue(struct gendisk *disk)
389 int ret;
391 struct request_queue *q = disk->queue;
393 if (WARN_ON(!q))
394 return -ENXIO;
396 if (!q->request_fn)
397 return 0;
399 ret = kobject_add(&q->kobj, kobject_get(&disk_to_dev(disk)->kobj),
400 "%s", "queue");
401 if (ret < 0)
402 return ret;
404 kobject_uevent(&q->kobj, KOBJ_ADD);
406 ret = elv_register_queue(q);
407 if (ret) {
408 kobject_uevent(&q->kobj, KOBJ_REMOVE);
409 kobject_del(&q->kobj);
410 return ret;
413 return 0;
416 void blk_unregister_queue(struct gendisk *disk)
418 struct request_queue *q = disk->queue;
420 if (WARN_ON(!q))
421 return;
423 if (q->request_fn) {
424 elv_unregister_queue(q);
426 kobject_uevent(&q->kobj, KOBJ_REMOVE);
427 kobject_del(&q->kobj);
428 kobject_put(&disk_to_dev(disk)->kobj);