2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
8 * The original JFFS, from which the design for JFFS2 was derived,
9 * was designed and implemented by Axis Communications AB.
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/
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.
21 * The Original Code is JFFS2 - Journalling Flash File System, version 2
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.
34 * $Id: background.c,v 1.16 2001/10/08 09:22:38 dwmw2 Exp $
38 #define __KERNEL_SYSCALLS__
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>
50 static int jffs2_garbage_collect_thread(void *);
51 static int thread_should_wake(struct jffs2_sb_info *c);
53 void jffs2_garbage_collect_trigger(struct jffs2_sb_info *c)
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);
61 /* This must only ever be called when no GC thread is currently running */
62 int jffs2_start_garbage_collect_thread(struct jffs2_sb_info *c)
70 init_MUTEX_LOCKED(&c->gc_thread_start);
71 init_completion(&c->gc_thread_exit);
73 pid = kernel_thread(jffs2_garbage_collect_thread, c, CLONE_FS|CLONE_FILES);
75 printk(KERN_WARNING "fork failed for JFFS2 garbage collect thread: %d\n", -pid);
76 complete(&c->gc_thread_exit);
80 D1(printk(KERN_DEBUG "JFFS2: Garbage collect thread is pid %d\n", pid));
81 down(&c->gc_thread_start);
87 void jffs2_stop_garbage_collect_thread(struct jffs2_sb_info *c)
89 spin_lock_bh(&c->erase_completion_lock);
91 D1(printk(KERN_DEBUG "jffs2: Killing GC task %d\n", c->gc_task->pid));
92 send_sig(SIGKILL, c->gc_task, 1);
94 spin_unlock_bh(&c->erase_completion_lock);
95 wait_for_completion(&c->gc_thread_exit);
98 static int jffs2_garbage_collect_thread(void *_c)
100 struct jffs2_sb_info *c = _c;
104 c->gc_task = current;
105 up(&c->gc_thread_start);
107 sprintf(current->comm, "jffs2_gcd_mtd%d", c->mtd->index);
109 /* FIXME in the 2.2 backport */
113 spin_lock_irq(¤t->sigmask_lock);
114 siginitsetinv (¤t->blocked, sigmask(SIGHUP) | sigmask(SIGKILL) | sigmask(SIGSTOP) | sigmask(SIGCONT));
115 recalc_sigpending(current);
116 spin_unlock_irq(¤t->sigmask_lock);
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. */
128 if (current->need_resched)
131 /* Put_super will send a SIGKILL and then wait on the sem.
133 while (signal_pending(current)) {
137 spin_lock_irq(¤t->sigmask_lock);
138 signr = dequeue_signal(¤t->blocked, &info);
139 spin_unlock_irq(¤t->sigmask_lock);
143 D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): SIGSTOP received.\n"));
144 set_current_state(TASK_STOPPED);
149 D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): SIGKILL received.\n"));
150 spin_lock_bh(&c->erase_completion_lock);
152 spin_unlock_bh(&c->erase_completion_lock);
153 complete_and_exit(&c->gc_thread_exit, 0);
156 D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): SIGHUP received.\n"));
159 D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): signal %ld received\n", signr));
163 /* We don't want SIGHUP to interrupt us. STOP and KILL are OK though. */
164 spin_lock_irq(¤t->sigmask_lock);
165 siginitsetinv (¤t->blocked, sigmask(SIGKILL) | sigmask(SIGSTOP) | sigmask(SIGCONT));
166 recalc_sigpending(current);
167 spin_unlock_irq(¤t->sigmask_lock);
169 D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): pass\n"));
170 jffs2_garbage_collect_pass(c);
174 static int thread_should_wake(struct jffs2_sb_info *c)
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)