writeback: add some debug inode list counters to bdi stats
[deliverable/linux.git] / mm / backing-dev.c
1
2 #include <linux/wait.h>
3 #include <linux/backing-dev.h>
4 #include <linux/kthread.h>
5 #include <linux/freezer.h>
6 #include <linux/fs.h>
7 #include <linux/pagemap.h>
8 #include <linux/mm.h>
9 #include <linux/sched.h>
10 #include <linux/module.h>
11 #include <linux/writeback.h>
12 #include <linux/device.h>
13
14 void default_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
15 {
16 }
17 EXPORT_SYMBOL(default_unplug_io_fn);
18
19 struct backing_dev_info default_backing_dev_info = {
20 .ra_pages = VM_MAX_READAHEAD * 1024 / PAGE_CACHE_SIZE,
21 .state = 0,
22 .capabilities = BDI_CAP_MAP_COPY,
23 .unplug_io_fn = default_unplug_io_fn,
24 };
25 EXPORT_SYMBOL_GPL(default_backing_dev_info);
26
27 static struct class *bdi_class;
28 DEFINE_SPINLOCK(bdi_lock);
29 LIST_HEAD(bdi_list);
30 LIST_HEAD(bdi_pending_list);
31
32 static struct task_struct *sync_supers_tsk;
33 static struct timer_list sync_supers_timer;
34
35 static int bdi_sync_supers(void *);
36 static void sync_supers_timer_fn(unsigned long);
37 static void arm_supers_timer(void);
38
39 static void bdi_add_default_flusher_task(struct backing_dev_info *bdi);
40
41 #ifdef CONFIG_DEBUG_FS
42 #include <linux/debugfs.h>
43 #include <linux/seq_file.h>
44
45 static struct dentry *bdi_debug_root;
46
47 static void bdi_debug_init(void)
48 {
49 bdi_debug_root = debugfs_create_dir("bdi", NULL);
50 }
51
52 static int bdi_debug_stats_show(struct seq_file *m, void *v)
53 {
54 struct backing_dev_info *bdi = m->private;
55 struct bdi_writeback *wb;
56 unsigned long background_thresh;
57 unsigned long dirty_thresh;
58 unsigned long bdi_thresh;
59 unsigned long nr_dirty, nr_io, nr_more_io, nr_wb;
60 struct inode *inode;
61
62 /*
63 * inode lock is enough here, the bdi->wb_list is protected by
64 * RCU on the reader side
65 */
66 nr_wb = nr_dirty = nr_io = nr_more_io = 0;
67 spin_lock(&inode_lock);
68 list_for_each_entry(wb, &bdi->wb_list, list) {
69 nr_wb++;
70 list_for_each_entry(inode, &wb->b_dirty, i_list)
71 nr_dirty++;
72 list_for_each_entry(inode, &wb->b_io, i_list)
73 nr_io++;
74 list_for_each_entry(inode, &wb->b_more_io, i_list)
75 nr_more_io++;
76 }
77 spin_unlock(&inode_lock);
78
79 get_dirty_limits(&background_thresh, &dirty_thresh, &bdi_thresh, bdi);
80
81 #define K(x) ((x) << (PAGE_SHIFT - 10))
82 seq_printf(m,
83 "BdiWriteback: %8lu kB\n"
84 "BdiReclaimable: %8lu kB\n"
85 "BdiDirtyThresh: %8lu kB\n"
86 "DirtyThresh: %8lu kB\n"
87 "BackgroundThresh: %8lu kB\n"
88 "WriteBack threads:%8lu\n"
89 "b_dirty: %8lu\n"
90 "b_io: %8lu\n"
91 "b_more_io: %8lu\n"
92 "bdi_list: %8u\n"
93 "state: %8lx\n"
94 "wb_mask: %8lx\n"
95 "wb_list: %8u\n"
96 "wb_cnt: %8u\n",
97 (unsigned long) K(bdi_stat(bdi, BDI_WRITEBACK)),
98 (unsigned long) K(bdi_stat(bdi, BDI_RECLAIMABLE)),
99 K(bdi_thresh), K(dirty_thresh),
100 K(background_thresh), nr_wb, nr_dirty, nr_io, nr_more_io,
101 !list_empty(&bdi->bdi_list), bdi->state, bdi->wb_mask,
102 !list_empty(&bdi->wb_list), bdi->wb_cnt);
103 #undef K
104
105 return 0;
106 }
107
108 static int bdi_debug_stats_open(struct inode *inode, struct file *file)
109 {
110 return single_open(file, bdi_debug_stats_show, inode->i_private);
111 }
112
113 static const struct file_operations bdi_debug_stats_fops = {
114 .open = bdi_debug_stats_open,
115 .read = seq_read,
116 .llseek = seq_lseek,
117 .release = single_release,
118 };
119
120 static void bdi_debug_register(struct backing_dev_info *bdi, const char *name)
121 {
122 bdi->debug_dir = debugfs_create_dir(name, bdi_debug_root);
123 bdi->debug_stats = debugfs_create_file("stats", 0444, bdi->debug_dir,
124 bdi, &bdi_debug_stats_fops);
125 }
126
127 static void bdi_debug_unregister(struct backing_dev_info *bdi)
128 {
129 debugfs_remove(bdi->debug_stats);
130 debugfs_remove(bdi->debug_dir);
131 }
132 #else
133 static inline void bdi_debug_init(void)
134 {
135 }
136 static inline void bdi_debug_register(struct backing_dev_info *bdi,
137 const char *name)
138 {
139 }
140 static inline void bdi_debug_unregister(struct backing_dev_info *bdi)
141 {
142 }
143 #endif
144
145 static ssize_t read_ahead_kb_store(struct device *dev,
146 struct device_attribute *attr,
147 const char *buf, size_t count)
148 {
149 struct backing_dev_info *bdi = dev_get_drvdata(dev);
150 char *end;
151 unsigned long read_ahead_kb;
152 ssize_t ret = -EINVAL;
153
154 read_ahead_kb = simple_strtoul(buf, &end, 10);
155 if (*buf && (end[0] == '\0' || (end[0] == '\n' && end[1] == '\0'))) {
156 bdi->ra_pages = read_ahead_kb >> (PAGE_SHIFT - 10);
157 ret = count;
158 }
159 return ret;
160 }
161
162 #define K(pages) ((pages) << (PAGE_SHIFT - 10))
163
164 #define BDI_SHOW(name, expr) \
165 static ssize_t name##_show(struct device *dev, \
166 struct device_attribute *attr, char *page) \
167 { \
168 struct backing_dev_info *bdi = dev_get_drvdata(dev); \
169 \
170 return snprintf(page, PAGE_SIZE-1, "%lld\n", (long long)expr); \
171 }
172
173 BDI_SHOW(read_ahead_kb, K(bdi->ra_pages))
174
175 static ssize_t min_ratio_store(struct device *dev,
176 struct device_attribute *attr, const char *buf, size_t count)
177 {
178 struct backing_dev_info *bdi = dev_get_drvdata(dev);
179 char *end;
180 unsigned int ratio;
181 ssize_t ret = -EINVAL;
182
183 ratio = simple_strtoul(buf, &end, 10);
184 if (*buf && (end[0] == '\0' || (end[0] == '\n' && end[1] == '\0'))) {
185 ret = bdi_set_min_ratio(bdi, ratio);
186 if (!ret)
187 ret = count;
188 }
189 return ret;
190 }
191 BDI_SHOW(min_ratio, bdi->min_ratio)
192
193 static ssize_t max_ratio_store(struct device *dev,
194 struct device_attribute *attr, const char *buf, size_t count)
195 {
196 struct backing_dev_info *bdi = dev_get_drvdata(dev);
197 char *end;
198 unsigned int ratio;
199 ssize_t ret = -EINVAL;
200
201 ratio = simple_strtoul(buf, &end, 10);
202 if (*buf && (end[0] == '\0' || (end[0] == '\n' && end[1] == '\0'))) {
203 ret = bdi_set_max_ratio(bdi, ratio);
204 if (!ret)
205 ret = count;
206 }
207 return ret;
208 }
209 BDI_SHOW(max_ratio, bdi->max_ratio)
210
211 #define __ATTR_RW(attr) __ATTR(attr, 0644, attr##_show, attr##_store)
212
213 static struct device_attribute bdi_dev_attrs[] = {
214 __ATTR_RW(read_ahead_kb),
215 __ATTR_RW(min_ratio),
216 __ATTR_RW(max_ratio),
217 __ATTR_NULL,
218 };
219
220 static __init int bdi_class_init(void)
221 {
222 bdi_class = class_create(THIS_MODULE, "bdi");
223 bdi_class->dev_attrs = bdi_dev_attrs;
224 bdi_debug_init();
225 return 0;
226 }
227 postcore_initcall(bdi_class_init);
228
229 static int __init default_bdi_init(void)
230 {
231 int err;
232
233 sync_supers_tsk = kthread_run(bdi_sync_supers, NULL, "sync_supers");
234 BUG_ON(IS_ERR(sync_supers_tsk));
235
236 init_timer(&sync_supers_timer);
237 setup_timer(&sync_supers_timer, sync_supers_timer_fn, 0);
238 arm_supers_timer();
239
240 err = bdi_init(&default_backing_dev_info);
241 if (!err)
242 bdi_register(&default_backing_dev_info, NULL, "default");
243
244 return err;
245 }
246 subsys_initcall(default_bdi_init);
247
248 static void bdi_wb_init(struct bdi_writeback *wb, struct backing_dev_info *bdi)
249 {
250 memset(wb, 0, sizeof(*wb));
251
252 wb->bdi = bdi;
253 wb->last_old_flush = jiffies;
254 INIT_LIST_HEAD(&wb->b_dirty);
255 INIT_LIST_HEAD(&wb->b_io);
256 INIT_LIST_HEAD(&wb->b_more_io);
257 }
258
259 static void bdi_task_init(struct backing_dev_info *bdi,
260 struct bdi_writeback *wb)
261 {
262 struct task_struct *tsk = current;
263
264 spin_lock(&bdi->wb_lock);
265 list_add_tail_rcu(&wb->list, &bdi->wb_list);
266 spin_unlock(&bdi->wb_lock);
267
268 tsk->flags |= PF_FLUSHER | PF_SWAPWRITE;
269 set_freezable();
270
271 /*
272 * Our parent may run at a different priority, just set us to normal
273 */
274 set_user_nice(tsk, 0);
275 }
276
277 static int bdi_start_fn(void *ptr)
278 {
279 struct bdi_writeback *wb = ptr;
280 struct backing_dev_info *bdi = wb->bdi;
281 int ret;
282
283 /*
284 * Add us to the active bdi_list
285 */
286 spin_lock(&bdi_lock);
287 list_add(&bdi->bdi_list, &bdi_list);
288 spin_unlock(&bdi_lock);
289
290 bdi_task_init(bdi, wb);
291
292 /*
293 * Clear pending bit and wakeup anybody waiting to tear us down
294 */
295 clear_bit(BDI_pending, &bdi->state);
296 smp_mb__after_clear_bit();
297 wake_up_bit(&bdi->state, BDI_pending);
298
299 ret = bdi_writeback_task(wb);
300
301 /*
302 * Remove us from the list
303 */
304 spin_lock(&bdi->wb_lock);
305 list_del_rcu(&wb->list);
306 spin_unlock(&bdi->wb_lock);
307
308 /*
309 * Flush any work that raced with us exiting. No new work
310 * will be added, since this bdi isn't discoverable anymore.
311 */
312 if (!list_empty(&bdi->work_list))
313 wb_do_writeback(wb, 1);
314
315 wb->task = NULL;
316 return ret;
317 }
318
319 int bdi_has_dirty_io(struct backing_dev_info *bdi)
320 {
321 return wb_has_dirty_io(&bdi->wb);
322 }
323
324 static void bdi_flush_io(struct backing_dev_info *bdi)
325 {
326 struct writeback_control wbc = {
327 .bdi = bdi,
328 .sync_mode = WB_SYNC_NONE,
329 .older_than_this = NULL,
330 .range_cyclic = 1,
331 .nr_to_write = 1024,
332 };
333
334 writeback_inodes_wbc(&wbc);
335 }
336
337 /*
338 * kupdated() used to do this. We cannot do it from the bdi_forker_task()
339 * or we risk deadlocking on ->s_umount. The longer term solution would be
340 * to implement sync_supers_bdi() or similar and simply do it from the
341 * bdi writeback tasks individually.
342 */
343 static int bdi_sync_supers(void *unused)
344 {
345 set_user_nice(current, 0);
346
347 while (!kthread_should_stop()) {
348 set_current_state(TASK_INTERRUPTIBLE);
349 schedule();
350
351 /*
352 * Do this periodically, like kupdated() did before.
353 */
354 sync_supers();
355 }
356
357 return 0;
358 }
359
360 static void arm_supers_timer(void)
361 {
362 unsigned long next;
363
364 next = msecs_to_jiffies(dirty_writeback_interval * 10) + jiffies;
365 mod_timer(&sync_supers_timer, round_jiffies_up(next));
366 }
367
368 static void sync_supers_timer_fn(unsigned long unused)
369 {
370 wake_up_process(sync_supers_tsk);
371 arm_supers_timer();
372 }
373
374 static int bdi_forker_task(void *ptr)
375 {
376 struct bdi_writeback *me = ptr;
377
378 bdi_task_init(me->bdi, me);
379
380 for (;;) {
381 struct backing_dev_info *bdi, *tmp;
382 struct bdi_writeback *wb;
383
384 /*
385 * Temporary measure, we want to make sure we don't see
386 * dirty data on the default backing_dev_info
387 */
388 if (wb_has_dirty_io(me) || !list_empty(&me->bdi->work_list))
389 wb_do_writeback(me, 0);
390
391 spin_lock(&bdi_lock);
392
393 /*
394 * Check if any existing bdi's have dirty data without
395 * a thread registered. If so, set that up.
396 */
397 list_for_each_entry_safe(bdi, tmp, &bdi_list, bdi_list) {
398 if (bdi->wb.task)
399 continue;
400 if (list_empty(&bdi->work_list) &&
401 !bdi_has_dirty_io(bdi))
402 continue;
403
404 bdi_add_default_flusher_task(bdi);
405 }
406
407 set_current_state(TASK_INTERRUPTIBLE);
408
409 if (list_empty(&bdi_pending_list)) {
410 unsigned long wait;
411
412 spin_unlock(&bdi_lock);
413 wait = msecs_to_jiffies(dirty_writeback_interval * 10);
414 schedule_timeout(wait);
415 try_to_freeze();
416 continue;
417 }
418
419 __set_current_state(TASK_RUNNING);
420
421 /*
422 * This is our real job - check for pending entries in
423 * bdi_pending_list, and create the tasks that got added
424 */
425 bdi = list_entry(bdi_pending_list.next, struct backing_dev_info,
426 bdi_list);
427 list_del_init(&bdi->bdi_list);
428 spin_unlock(&bdi_lock);
429
430 wb = &bdi->wb;
431 wb->task = kthread_run(bdi_start_fn, wb, "flush-%s",
432 dev_name(bdi->dev));
433 /*
434 * If task creation fails, then readd the bdi to
435 * the pending list and force writeout of the bdi
436 * from this forker thread. That will free some memory
437 * and we can try again.
438 */
439 if (IS_ERR(wb->task)) {
440 wb->task = NULL;
441
442 /*
443 * Add this 'bdi' to the back, so we get
444 * a chance to flush other bdi's to free
445 * memory.
446 */
447 spin_lock(&bdi_lock);
448 list_add_tail(&bdi->bdi_list, &bdi_pending_list);
449 spin_unlock(&bdi_lock);
450
451 bdi_flush_io(bdi);
452 }
453 }
454
455 return 0;
456 }
457
458 /*
459 * Add the default flusher task that gets created for any bdi
460 * that has dirty data pending writeout
461 */
462 void static bdi_add_default_flusher_task(struct backing_dev_info *bdi)
463 {
464 if (!bdi_cap_writeback_dirty(bdi))
465 return;
466
467 /*
468 * Check with the helper whether to proceed adding a task. Will only
469 * abort if we two or more simultanous calls to
470 * bdi_add_default_flusher_task() occured, further additions will block
471 * waiting for previous additions to finish.
472 */
473 if (!test_and_set_bit(BDI_pending, &bdi->state)) {
474 list_move_tail(&bdi->bdi_list, &bdi_pending_list);
475
476 /*
477 * We are now on the pending list, wake up bdi_forker_task()
478 * to finish the job and add us back to the active bdi_list
479 */
480 wake_up_process(default_backing_dev_info.wb.task);
481 }
482 }
483
484 int bdi_register(struct backing_dev_info *bdi, struct device *parent,
485 const char *fmt, ...)
486 {
487 va_list args;
488 int ret = 0;
489 struct device *dev;
490
491 if (bdi->dev) /* The driver needs to use separate queues per device */
492 goto exit;
493
494 va_start(args, fmt);
495 dev = device_create_vargs(bdi_class, parent, MKDEV(0, 0), bdi, fmt, args);
496 va_end(args);
497 if (IS_ERR(dev)) {
498 ret = PTR_ERR(dev);
499 goto exit;
500 }
501
502 spin_lock(&bdi_lock);
503 list_add_tail(&bdi->bdi_list, &bdi_list);
504 spin_unlock(&bdi_lock);
505
506 bdi->dev = dev;
507
508 /*
509 * Just start the forker thread for our default backing_dev_info,
510 * and add other bdi's to the list. They will get a thread created
511 * on-demand when they need it.
512 */
513 if (bdi_cap_flush_forker(bdi)) {
514 struct bdi_writeback *wb = &bdi->wb;
515
516 wb->task = kthread_run(bdi_forker_task, wb, "bdi-%s",
517 dev_name(dev));
518 if (IS_ERR(wb->task)) {
519 wb->task = NULL;
520 ret = -ENOMEM;
521
522 spin_lock(&bdi_lock);
523 list_del(&bdi->bdi_list);
524 spin_unlock(&bdi_lock);
525 goto exit;
526 }
527 }
528
529 bdi_debug_register(bdi, dev_name(dev));
530 exit:
531 return ret;
532 }
533 EXPORT_SYMBOL(bdi_register);
534
535 int bdi_register_dev(struct backing_dev_info *bdi, dev_t dev)
536 {
537 return bdi_register(bdi, NULL, "%u:%u", MAJOR(dev), MINOR(dev));
538 }
539 EXPORT_SYMBOL(bdi_register_dev);
540
541 /*
542 * Remove bdi from the global list and shutdown any threads we have running
543 */
544 static void bdi_wb_shutdown(struct backing_dev_info *bdi)
545 {
546 struct bdi_writeback *wb;
547
548 if (!bdi_cap_writeback_dirty(bdi))
549 return;
550
551 /*
552 * If setup is pending, wait for that to complete first
553 */
554 wait_on_bit(&bdi->state, BDI_pending, bdi_sched_wait,
555 TASK_UNINTERRUPTIBLE);
556
557 /*
558 * Make sure nobody finds us on the bdi_list anymore
559 */
560 spin_lock(&bdi_lock);
561 list_del(&bdi->bdi_list);
562 spin_unlock(&bdi_lock);
563
564 /*
565 * Finally, kill the kernel threads. We don't need to be RCU
566 * safe anymore, since the bdi is gone from visibility.
567 */
568 list_for_each_entry(wb, &bdi->wb_list, list)
569 kthread_stop(wb->task);
570 }
571
572 void bdi_unregister(struct backing_dev_info *bdi)
573 {
574 if (bdi->dev) {
575 if (!bdi_cap_flush_forker(bdi))
576 bdi_wb_shutdown(bdi);
577 bdi_debug_unregister(bdi);
578 device_unregister(bdi->dev);
579 bdi->dev = NULL;
580 }
581 }
582 EXPORT_SYMBOL(bdi_unregister);
583
584 int bdi_init(struct backing_dev_info *bdi)
585 {
586 int i, err;
587
588 bdi->dev = NULL;
589
590 bdi->min_ratio = 0;
591 bdi->max_ratio = 100;
592 bdi->max_prop_frac = PROP_FRAC_BASE;
593 spin_lock_init(&bdi->wb_lock);
594 INIT_LIST_HEAD(&bdi->bdi_list);
595 INIT_LIST_HEAD(&bdi->wb_list);
596 INIT_LIST_HEAD(&bdi->work_list);
597
598 bdi_wb_init(&bdi->wb, bdi);
599
600 /*
601 * Just one thread support for now, hard code mask and count
602 */
603 bdi->wb_mask = 1;
604 bdi->wb_cnt = 1;
605
606 for (i = 0; i < NR_BDI_STAT_ITEMS; i++) {
607 err = percpu_counter_init(&bdi->bdi_stat[i], 0);
608 if (err)
609 goto err;
610 }
611
612 bdi->dirty_exceeded = 0;
613 err = prop_local_init_percpu(&bdi->completions);
614
615 if (err) {
616 err:
617 while (i--)
618 percpu_counter_destroy(&bdi->bdi_stat[i]);
619 }
620
621 return err;
622 }
623 EXPORT_SYMBOL(bdi_init);
624
625 void bdi_destroy(struct backing_dev_info *bdi)
626 {
627 int i;
628
629 WARN_ON(bdi_has_dirty_io(bdi));
630
631 bdi_unregister(bdi);
632
633 for (i = 0; i < NR_BDI_STAT_ITEMS; i++)
634 percpu_counter_destroy(&bdi->bdi_stat[i]);
635
636 prop_local_destroy_percpu(&bdi->completions);
637 }
638 EXPORT_SYMBOL(bdi_destroy);
639
640 static wait_queue_head_t congestion_wqh[2] = {
641 __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[0]),
642 __WAIT_QUEUE_HEAD_INITIALIZER(congestion_wqh[1])
643 };
644
645 void clear_bdi_congested(struct backing_dev_info *bdi, int sync)
646 {
647 enum bdi_state bit;
648 wait_queue_head_t *wqh = &congestion_wqh[sync];
649
650 bit = sync ? BDI_sync_congested : BDI_async_congested;
651 clear_bit(bit, &bdi->state);
652 smp_mb__after_clear_bit();
653 if (waitqueue_active(wqh))
654 wake_up(wqh);
655 }
656 EXPORT_SYMBOL(clear_bdi_congested);
657
658 void set_bdi_congested(struct backing_dev_info *bdi, int sync)
659 {
660 enum bdi_state bit;
661
662 bit = sync ? BDI_sync_congested : BDI_async_congested;
663 set_bit(bit, &bdi->state);
664 }
665 EXPORT_SYMBOL(set_bdi_congested);
666
667 /**
668 * congestion_wait - wait for a backing_dev to become uncongested
669 * @sync: SYNC or ASYNC IO
670 * @timeout: timeout in jiffies
671 *
672 * Waits for up to @timeout jiffies for a backing_dev (any backing_dev) to exit
673 * write congestion. If no backing_devs are congested then just wait for the
674 * next write to be completed.
675 */
676 long congestion_wait(int sync, long timeout)
677 {
678 long ret;
679 DEFINE_WAIT(wait);
680 wait_queue_head_t *wqh = &congestion_wqh[sync];
681
682 prepare_to_wait(wqh, &wait, TASK_UNINTERRUPTIBLE);
683 ret = io_schedule_timeout(timeout);
684 finish_wait(wqh, &wait);
685 return ret;
686 }
687 EXPORT_SYMBOL(congestion_wait);
688
This page took 0.047542 seconds and 5 git commands to generate.