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.16 2001/10/08 09:22:38 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/completion.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 
jffs2_garbage_collect_trigger(struct jffs2_sb_info * c)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 /* This must only ever be called when no GC thread is currently running */
jffs2_start_garbage_collect_thread(struct jffs2_sb_info * c)62 int jffs2_start_garbage_collect_thread(struct jffs2_sb_info *c)
63 {
64 	pid_t pid;
65 	int ret = 0;
66 
67 	if (c->gc_task)
68 		BUG();
69 
70 	init_MUTEX_LOCKED(&c->gc_thread_start);
71 	init_completion(&c->gc_thread_exit);
72 
73 	pid = kernel_thread(jffs2_garbage_collect_thread, c, CLONE_FS|CLONE_FILES);
74 	if (pid < 0) {
75 		printk(KERN_WARNING "fork failed for JFFS2 garbage collect thread: %d\n", -pid);
76 		complete(&c->gc_thread_exit);
77 		ret = pid;
78 	} else {
79 		/* Wait for it... */
80 		D1(printk(KERN_DEBUG "JFFS2: Garbage collect thread is pid %d\n", pid));
81 		down(&c->gc_thread_start);
82 	}
83 
84 	return ret;
85 }
86 
jffs2_stop_garbage_collect_thread(struct jffs2_sb_info * c)87 void jffs2_stop_garbage_collect_thread(struct jffs2_sb_info *c)
88 {
89 	spin_lock_bh(&c->erase_completion_lock);
90 	if (c->gc_task) {
91 		D1(printk(KERN_DEBUG "jffs2: Killing GC task %d\n", c->gc_task->pid));
92 		send_sig(SIGKILL, c->gc_task, 1);
93 	}
94 	spin_unlock_bh(&c->erase_completion_lock);
95 	wait_for_completion(&c->gc_thread_exit);
96 }
97 
jffs2_garbage_collect_thread(void * _c)98 static int jffs2_garbage_collect_thread(void *_c)
99 {
100 	struct jffs2_sb_info *c = _c;
101 
102 	daemonize();
103 	current->tty = NULL;
104 	c->gc_task = current;
105 	up(&c->gc_thread_start);
106 
107         sprintf(current->comm, "jffs2_gcd_mtd%d", c->mtd->index);
108 
109 	/* FIXME in the 2.2 backport */
110 	current->nice = 10;
111 
112 	for (;;) {
113 		spin_lock_irq(&current->sigmask_lock);
114 		siginitsetinv (&current->blocked, sigmask(SIGHUP) | sigmask(SIGKILL) | sigmask(SIGSTOP) | sigmask(SIGCONT));
115 		recalc_sigpending(current);
116 		spin_unlock_irq(&current->sigmask_lock);
117 
118 		if (!thread_should_wake(c)) {
119                         set_current_state (TASK_INTERRUPTIBLE);
120 			D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread sleeping...\n"));
121 			/* Yes, there's a race here; we checked thread_should_wake() before
122 			   setting current->state to TASK_INTERRUPTIBLE. But it doesn't
123 			   matter - We don't care if we miss a wakeup, because the GC thread
124 			   is only an optimisation anyway. */
125 			schedule();
126 		}
127 
128 		if (current->need_resched)
129 			schedule();
130 
131                 /* Put_super will send a SIGKILL and then wait on the sem.
132                  */
133                 while (signal_pending(current)) {
134                         siginfo_t info;
135                         unsigned long signr;
136 
137                         spin_lock_irq(&current->sigmask_lock);
138                         signr = dequeue_signal(&current->blocked, &info);
139                         spin_unlock_irq(&current->sigmask_lock);
140 
141                         switch(signr) {
142                         case SIGSTOP:
143                                 D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): SIGSTOP received.\n"));
144                                 set_current_state(TASK_STOPPED);
145                                 schedule();
146                                 break;
147 
148                         case SIGKILL:
149                                 D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): SIGKILL received.\n"));
150 				spin_lock_bh(&c->erase_completion_lock);
151                                 c->gc_task = NULL;
152 				spin_unlock_bh(&c->erase_completion_lock);
153 				complete_and_exit(&c->gc_thread_exit, 0);
154 
155 			case SIGHUP:
156 				D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): SIGHUP received.\n"));
157 				break;
158 			default:
159 				D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): signal %ld received\n", signr));
160 
161                         }
162                 }
163 		/* We don't want SIGHUP to interrupt us. STOP and KILL are OK though. */
164 		spin_lock_irq(&current->sigmask_lock);
165 		siginitsetinv (&current->blocked, sigmask(SIGKILL) | sigmask(SIGSTOP) | sigmask(SIGCONT));
166 		recalc_sigpending(current);
167 		spin_unlock_irq(&current->sigmask_lock);
168 
169 		D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): pass\n"));
170 		jffs2_garbage_collect_pass(c);
171 	}
172 }
173 
thread_should_wake(struct jffs2_sb_info * c)174 static int thread_should_wake(struct jffs2_sb_info *c)
175 {
176 	D1(printk(KERN_DEBUG "thread_should_wake(): nr_free_blocks %d, nr_erasing_blocks %d, dirty_size 0x%x\n",
177 		  c->nr_free_blocks, c->nr_erasing_blocks, c->dirty_size));
178 	if (c->nr_free_blocks + c->nr_erasing_blocks < JFFS2_RESERVED_BLOCKS_GCTRIGGER &&
179 	    c->dirty_size > c->sector_size)
180 		return 1;
181 	else
182 		return 0;
183 }
184