Blame view
fs/jffs2/background.c
4.36 KB
1da177e4c
|
1 2 3 |
/* * JFFS2 -- Journalling Flash File System, Version 2. * |
c00c310ea
|
4 |
* Copyright © 2001-2007 Red Hat, Inc. |
6088c0587
|
5 |
* Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org> |
1da177e4c
|
6 7 8 9 10 |
* * Created by David Woodhouse <dwmw2@infradead.org> * * For licensing information, see the file 'LICENCE' in this directory. * |
1da177e4c
|
11 12 13 14 15 16 |
*/ #include <linux/kernel.h> #include <linux/jffs2.h> #include <linux/mtd/mtd.h> #include <linux/completion.h> |
4e57b6817
|
17 |
#include <linux/sched.h> |
7dfb71030
|
18 |
#include <linux/freezer.h> |
91e0955b5
|
19 |
#include <linux/kthread.h> |
1da177e4c
|
20 21 22 23 24 25 26 |
#include "nodelist.h" static int jffs2_garbage_collect_thread(void *); void jffs2_garbage_collect_trigger(struct jffs2_sb_info *c) { |
acb64a43e
|
27 |
assert_spin_locked(&c->erase_completion_lock); |
ef53cb02f
|
28 29 |
if (c->gc_task && jffs2_thread_should_wake(c)) send_sig(SIGHUP, c->gc_task, 1); |
1da177e4c
|
30 31 32 33 34 |
} /* This must only ever be called when no GC thread is currently running */ int jffs2_start_garbage_collect_thread(struct jffs2_sb_info *c) { |
91e0955b5
|
35 |
struct task_struct *tsk; |
1da177e4c
|
36 |
int ret = 0; |
4b4d1cc73
|
37 |
BUG_ON(c->gc_task); |
1da177e4c
|
38 |
|
fff7afd79
|
39 |
init_completion(&c->gc_thread_start); |
1da177e4c
|
40 |
init_completion(&c->gc_thread_exit); |
91e0955b5
|
41 42 43 44 |
tsk = kthread_run(jffs2_garbage_collect_thread, c, "jffs2_gcd_mtd%d", c->mtd->index); if (IS_ERR(tsk)) { printk(KERN_WARNING "fork failed for JFFS2 garbage collect thread: %ld ", -PTR_ERR(tsk)); |
1da177e4c
|
45 |
complete(&c->gc_thread_exit); |
91e0955b5
|
46 |
ret = PTR_ERR(tsk); |
1da177e4c
|
47 48 |
} else { /* Wait for it... */ |
91e0955b5
|
49 50 |
D1(printk(KERN_DEBUG "JFFS2: Garbage collect thread is pid %d ", tsk->pid)); |
fff7afd79
|
51 |
wait_for_completion(&c->gc_thread_start); |
91e0955b5
|
52 |
ret = tsk->pid; |
1da177e4c
|
53 |
} |
182ec4eee
|
54 |
|
1da177e4c
|
55 56 57 58 59 |
return ret; } void jffs2_stop_garbage_collect_thread(struct jffs2_sb_info *c) { |
e2d48b1a9
|
60 |
int wait = 0; |
1da177e4c
|
61 62 63 64 65 |
spin_lock(&c->erase_completion_lock); if (c->gc_task) { D1(printk(KERN_DEBUG "jffs2: Killing GC task %d ", c->gc_task->pid)); send_sig(SIGKILL, c->gc_task, 1); |
e2d48b1a9
|
66 |
wait = 1; |
1da177e4c
|
67 68 |
} spin_unlock(&c->erase_completion_lock); |
e2d48b1a9
|
69 70 |
if (wait) wait_for_completion(&c->gc_thread_exit); |
1da177e4c
|
71 72 73 74 75 |
} static int jffs2_garbage_collect_thread(void *_c) { struct jffs2_sb_info *c = _c; |
1da177e4c
|
76 77 78 79 80 |
allow_signal(SIGKILL); allow_signal(SIGSTOP); allow_signal(SIGCONT); c->gc_task = current; |
fff7afd79
|
81 |
complete(&c->gc_thread_start); |
1da177e4c
|
82 83 |
set_user_nice(current, 10); |
831441862
|
84 |
set_freezable(); |
1da177e4c
|
85 86 |
for (;;) { allow_signal(SIGHUP); |
e716dd364
|
87 |
again: |
b27cf88e9
|
88 |
spin_lock(&c->erase_completion_lock); |
1da177e4c
|
89 90 |
if (!jffs2_thread_should_wake(c)) { set_current_state (TASK_INTERRUPTIBLE); |
b27cf88e9
|
91 |
spin_unlock(&c->erase_completion_lock); |
1da177e4c
|
92 93 |
D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread sleeping... ")); |
1da177e4c
|
94 |
schedule(); |
b27cf88e9
|
95 96 97 |
} else spin_unlock(&c->erase_completion_lock); |
1da177e4c
|
98 |
|
efab0b5d3
|
99 100 101 102 103 104 105 106 107 108 109 |
/* Problem - immediately after bootup, the GCD spends a lot * of time in places like jffs2_kill_fragtree(); so much so * that userspace processes (like gdm and X) are starved * despite plenty of cond_resched()s and renicing. Yield() * doesn't help, either (presumably because userspace and GCD * are generally competing for a higher latency resource - * disk). * This forces the GCD to slow the hell down. Pulling an * inode in with read_inode() is much preferable to having * the GC thread get there first. */ schedule_timeout_interruptible(msecs_to_jiffies(50)); |
1da177e4c
|
110 |
|
91e0955b5
|
111 112 113 114 115 |
if (kthread_should_stop()) { D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): kthread_stop() called. ")); goto die; } |
182ec4eee
|
116 |
/* Put_super will send a SIGKILL and then wait on the sem. |
1da177e4c
|
117 |
*/ |
e136e769d
|
118 |
while (signal_pending(current) || freezing(current)) { |
1da177e4c
|
119 120 |
siginfo_t info; unsigned long signr; |
e716dd364
|
121 122 |
if (try_to_freeze()) goto again; |
1da177e4c
|
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
signr = dequeue_signal_lock(current, ¤t->blocked, &info); switch(signr) { case SIGSTOP: D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): SIGSTOP received. ")); set_current_state(TASK_STOPPED); schedule(); break; case SIGKILL: D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): SIGKILL received. ")); goto die; case SIGHUP: D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): SIGHUP received. ")); break; default: D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): signal %ld received ", signr)); } } /* We don't want SIGHUP to interrupt us. STOP and KILL are OK though. */ disallow_signal(SIGHUP); D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): pass ")); if (jffs2_garbage_collect_pass(c) == -ENOSPC) { printk(KERN_NOTICE "No space for garbage collection. Aborting GC thread "); goto die; } } die: spin_lock(&c->erase_completion_lock); c->gc_task = NULL; spin_unlock(&c->erase_completion_lock); complete_and_exit(&c->gc_thread_exit, 0); } |