v2.4.9.9 -> v2.4.9.10
[opensuse:kernel.git] / fs / jffs2 / background.c
1 /*
2  * JFFS2 -- Journalling Flash File System, Version 2.
3  *
4  * Copyright (C) 2001 Red Hat, Inc.
5  *
6  * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
7  *
8  * The original JFFS, from which the design for JFFS2 was derived,
9  * was designed and implemented by Axis Communications AB.
10  *
11  * The contents of this file are subject to the Red Hat eCos Public
12  * License Version 1.1 (the "Licence"); you may not use this file
13  * except in compliance with the Licence.  You may obtain a copy of
14  * the Licence at http://www.redhat.com/
15  *
16  * Software distributed under the Licence is distributed on an "AS IS"
17  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
18  * See the Licence for the specific language governing rights and
19  * limitations under the Licence.
20  *
21  * The Original Code is JFFS2 - Journalling Flash File System, version 2
22  *
23  * Alternatively, the contents of this file may be used under the
24  * terms of the GNU General Public License version 2 (the "GPL"), in
25  * which case the provisions of the GPL are applicable instead of the
26  * above.  If you wish to allow the use of your version of this file
27  * only under the terms of the GPL and not to allow others to use your
28  * version of this file under the RHEPL, indicate your decision by
29  * deleting the provisions above and replace them with the notice and
30  * other provisions required by the GPL.  If you do not delete the
31  * provisions above, a recipient may use your version of this file
32  * under either the RHEPL or the GPL.
33  *
34  * $Id: background.c,v 1.10 2001/03/15 15:38:23 dwmw2 Exp $
35  *
36  */
37
38 #define __KERNEL_SYSCALLS__
39
40 #include <linux/kernel.h>
41 #include <linux/sched.h>
42 #include <linux/unistd.h>
43 #include <linux/jffs2.h>
44 #include <linux/mtd/mtd.h>
45 #include <linux/interrupt.h>
46 #include <linux/smp_lock.h>
47 #include "nodelist.h"
48
49
50 static int jffs2_garbage_collect_thread(void *);
51 static int thread_should_wake(struct jffs2_sb_info *c);
52
53 void jffs2_garbage_collect_trigger(struct jffs2_sb_info *c)
54 {
55         spin_lock_bh(&c->erase_completion_lock);
56         if (c->gc_task && thread_should_wake(c))
57                 send_sig(SIGHUP, c->gc_task, 1);
58         spin_unlock_bh(&c->erase_completion_lock);
59 }
60
61 int jffs2_start_garbage_collect_thread(struct jffs2_sb_info *c)
62 {
63         pid_t pid;
64         int ret = 0;
65         init_MUTEX_LOCKED(&c->gc_thread_sem);
66         init_completion(&c->gc_thread_exit);
67         
68         pid = kernel_thread(jffs2_garbage_collect_thread, c, CLONE_FS|CLONE_FILES);
69         if (pid < 0) {
70                 printk(KERN_WARNING "fork failed for JFFS2 garbage collect thread: %d\n", -pid);
71                 ret = pid;
72         } else {
73                 /* Wait for it... */
74                 D1(printk(KERN_DEBUG "JFFS2: Garbage collect thread is pid %d\n", pid));
75                 down(&c->gc_thread_sem);
76         }
77         up(&c->gc_thread_sem);
78  
79         return ret;
80 }
81
82 void jffs2_stop_garbage_collect_thread(struct jffs2_sb_info *c)
83 {
84         spin_lock_bh(&c->erase_completion_lock);
85         if (c->gc_task) {
86                 D1(printk(KERN_DEBUG "jffs2: Killing GC task %d\n", c->gc_task->pid));
87                 send_sig(SIGKILL, c->gc_task, 1);
88         }
89         spin_unlock_bh(&c->erase_completion_lock);
90         down(&c->gc_thread_sem);
91         wait_for_completion(&c->gc_thread_exit);
92 }
93
94 static int jffs2_garbage_collect_thread(void *_c)
95 {
96         struct jffs2_sb_info *c = _c;
97
98         daemonize();
99         current->tty = NULL;
100         c->gc_task = current;
101         up(&c->gc_thread_sem);
102
103         sprintf(current->comm, "jffs2_gcd_mtd%d", c->mtd->index);
104
105         /* FIXME in the 2.2 backport */
106         current->nice = 10;
107
108         for (;;) {
109                 spin_lock_irq(&current->sigmask_lock);
110                 siginitsetinv (&current->blocked, sigmask(SIGHUP) | sigmask(SIGKILL) | sigmask(SIGSTOP) | sigmask(SIGCONT));
111                 recalc_sigpending(current);
112                 spin_unlock_irq(&current->sigmask_lock);
113
114                 if (!thread_should_wake(c)) {
115                         set_current_state (TASK_INTERRUPTIBLE);
116                         D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread sleeping...\n"));
117                 }
118                 
119                 schedule(); /* Yes, we do this even if we want to go
120                                on immediately - we're a low priority 
121                                background task. */
122
123                 /* Put_super will send a SIGKILL and then wait on the sem. 
124                  */
125                 while (signal_pending(current)) {
126                         siginfo_t info;
127                         unsigned long signr;
128
129                         spin_lock_irq(&current->sigmask_lock);
130                         signr = dequeue_signal(&current->blocked, &info);
131                         spin_unlock_irq(&current->sigmask_lock);
132
133                         switch(signr) {
134                         case SIGSTOP:
135                                 D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): SIGSTOP received.\n"));
136                                 set_current_state(TASK_STOPPED);
137                                 schedule();
138                                 break;
139
140                         case SIGKILL:
141                                 D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): SIGKILL received.\n"));
142                                 spin_lock_bh(&c->erase_completion_lock);
143                                 c->gc_task = NULL;
144                                 spin_unlock_bh(&c->erase_completion_lock);
145                                 up(&c->gc_thread_sem);
146                                 complete_and_exit(&c->gc_thread_exit,0 );
147
148                         case SIGHUP:
149                                 D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): SIGHUP received.\n"));
150                                 break;
151                         default:
152                                 D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): signal %ld received\n", signr));
153
154                         }
155                 }
156                 /* We don't want SIGHUP to interrupt us. STOP and KILL are OK though. */
157                 spin_lock_irq(&current->sigmask_lock);
158                 siginitsetinv (&current->blocked, sigmask(SIGKILL) | sigmask(SIGSTOP) | sigmask(SIGCONT));
159                 recalc_sigpending(current);
160                 spin_unlock_irq(&current->sigmask_lock);
161
162                 D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): pass\n"));
163                 jffs2_garbage_collect_pass(c);
164         }
165 }
166
167 static int thread_should_wake(struct jffs2_sb_info *c)
168 {
169         D1(printk(KERN_DEBUG "thread_should_wake(): nr_free_blocks %d, nr_erasing_blocks %d, dirty_size 0x%x\n", 
170                   c->nr_free_blocks, c->nr_erasing_blocks, c->dirty_size));
171         if (c->nr_free_blocks + c->nr_erasing_blocks < JFFS2_RESERVED_BLOCKS_GCTRIGGER &&
172             c->dirty_size > c->sector_size)
173                 return 1;
174         else 
175                 return 0;
176 }