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