MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / fs / jffs2 / background.c
blob889dddacc4ffafd224df48b89c1dec5086f17c29
1 /*
2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001-2003 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@infradead.org>
8 * For licensing information, see the file 'LICENCE' in this directory.
10 * $Id: background.c,v 1.54 2005/05/20 21:37:12 gleixner Exp $
14 #include <linux/kernel.h>
15 #include <linux/jffs2.h>
16 #include <linux/mtd/mtd.h>
17 #include <linux/completion.h>
18 #include <linux/sched.h>
19 #include "nodelist.h"
21 #if 1 // Add by Jared 02-15-2007. Start the gargage collection.
22 #include <linux/proc_fs.h>
23 #include <linux/uaccess.h>
24 static struct proc_dir_entry *proc_startstop_gc, *proc_jffs2, *proc_dirty, *proc_sector_size;
25 int jffs2_bstartstop_gc=0; // flag, 1:start the garbage collection, 0:stop the gargabe collection
27 static int proc_write_startstop_gc(struct file *file, const char *buffer, unsigned long count, void *data)
29 char tmpbuf[4];
30 int len;
31 struct jffs2_sb_info *c=(struct jffs2_sb_info *)data;
33 if ( count > 4 )
34 len = 4;
35 else
36 len =count;
38 if(copy_from_user( tmpbuf, buffer, len)) {
39 return -EFAULT;
41 tmpbuf[len]='\0';
43 sscanf(tmpbuf, "%d", &jffs2_bstartstop_gc);
45 if ( jffs2_bstartstop_gc ) // wake up the sleeping garbage collection thread
46 wake_up_process(c->gc_task);
48 return len;
51 static int proc_read_startstop_gc(char *page, char **start, off_t off, int count, int *eof, void *data)
53 return sprintf(page, "%d\n", jffs2_bstartstop_gc);
56 static int proc_read_jffs2_dirty(char *page, char **start, off_t off, int count, int *eof, void *data)
58 struct jffs2_sb_info *c=(struct jffs2_sb_info *)data;
60 return sprintf(page, "%d\n", c->dirty_size + c->erasing_size - c->nr_erasing_blocks * c->sector_size);
63 static int proc_read_sector_size(char *page, char **start, off_t off, int count, int *eof, void *data)
65 struct jffs2_sb_info *c=(struct jffs2_sb_info *)data;
67 return sprintf(page, "%d\n", c->nospc_dirty_size);
69 #endif // End: add by Jared
71 static int jffs2_garbage_collect_thread(void *);
73 void jffs2_garbage_collect_trigger(struct jffs2_sb_info *c)
75 spin_lock(&c->erase_completion_lock);
76 if (c->gc_task && jffs2_thread_should_wake(c))
77 send_sig(SIGHUP, c->gc_task, 1);
78 spin_unlock(&c->erase_completion_lock);
81 /* This must only ever be called when no GC thread is currently running */
82 int jffs2_start_garbage_collect_thread(struct jffs2_sb_info *c)
84 pid_t pid;
85 int ret = 0;
87 BUG_ON(c->gc_task);
89 init_completion(&c->gc_thread_start);
90 init_completion(&c->gc_thread_exit);
92 pid = kernel_thread(jffs2_garbage_collect_thread, c, CLONE_FS|CLONE_FILES);
93 if (pid < 0) {
94 printk(KERN_WARNING "fork failed for JFFS2 garbage collect thread: %d\n", -pid);
95 complete(&c->gc_thread_exit);
96 ret = pid;
97 } else {
98 /* Wait for it... */
99 D1(printk(KERN_DEBUG "JFFS2: Garbage collect thread is pid %d\n", pid));
100 wait_for_completion(&c->gc_thread_start);
103 return ret;
106 void jffs2_stop_garbage_collect_thread(struct jffs2_sb_info *c)
108 int wait = 0;
109 spin_lock(&c->erase_completion_lock);
110 if (c->gc_task) {
111 D1(printk(KERN_DEBUG "jffs2: Killing GC task %d\n", c->gc_task->pid));
112 send_sig(SIGKILL, c->gc_task, 1);
113 wait = 1;
115 spin_unlock(&c->erase_completion_lock);
116 if (wait)
117 wait_for_completion(&c->gc_thread_exit);
120 static int jffs2_garbage_collect_thread(void *_c)
122 struct jffs2_sb_info *c = _c;
124 daemonize("jffs2_gcd_mtd%d", c->mtd->index);
125 allow_signal(SIGKILL);
126 allow_signal(SIGSTOP);
127 allow_signal(SIGCONT);
129 c->gc_task = current;
130 complete(&c->gc_thread_start);
132 #if 1 // Add by Jared 02-15-2007. Start the gargage collection.
133 proc_jffs2=proc_mkdir("jffs2", NULL);
134 if( proc_jffs2 ==NULL ) {
135 printk("<1>Create /proc/jffs2 fail\n");
136 return -ENOMEM;
139 proc_startstop_gc=create_proc_entry("startstop_gc", 0644, proc_jffs2);
140 if( proc_startstop_gc==NULL ) {
141 printk("<1>Create /proc/jffs2/startstop_gc fail\n");
142 return -ENOMEM;
145 proc_startstop_gc->write_proc=proc_write_startstop_gc;
146 proc_startstop_gc->read_proc=proc_read_startstop_gc;
147 proc_startstop_gc->data=(void*)_c;
149 proc_dirty=create_proc_read_entry("dirty", 0444, proc_jffs2, proc_read_jffs2_dirty, (void*)_c);
150 if( proc_dirty==NULL ) {
151 printk("<1>Create /proc/jffs2/dirty fail\n");
152 return -ENOMEM;
155 proc_sector_size=create_proc_read_entry("sector_size", 0444, proc_jffs2, proc_read_sector_size, (void*)_c);
156 if( proc_sector_size==NULL ) {
157 printk("<1>Create /proc/jffs2/sector_size fail\n");
158 return -ENOMEM;
160 #endif // End: add by Jared
162 set_user_nice(current, 10);
164 for (;;) {
165 allow_signal(SIGHUP);
167 if (!jffs2_thread_should_wake(c)) {
168 set_current_state (TASK_INTERRUPTIBLE);
169 D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread sleeping...\n"));
170 /* Yes, there's a race here; we checked jffs2_thread_should_wake()
171 before setting current->state to TASK_INTERRUPTIBLE. But it doesn't
172 matter - We don't care if we miss a wakeup, because the GC thread
173 is only an optimisation anyway. */
174 schedule();
177 if (try_to_freeze())
178 continue;
180 cond_resched();
182 /* Put_super will send a SIGKILL and then wait on the sem.
184 while (signal_pending(current)) {
185 siginfo_t info;
186 unsigned long signr;
188 signr = dequeue_signal_lock(current, &current->blocked, &info);
190 switch(signr) {
191 case SIGSTOP:
192 D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): SIGSTOP received.\n"));
193 set_current_state(TASK_STOPPED);
194 schedule();
195 break;
197 case SIGKILL:
198 D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): SIGKILL received.\n"));
199 goto die;
201 case SIGHUP:
202 D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): SIGHUP received.\n"));
203 break;
204 default:
205 D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): signal %ld received\n", signr));
208 /* We don't want SIGHUP to interrupt us. STOP and KILL are OK though. */
209 disallow_signal(SIGHUP);
211 D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): pass\n"));
212 if (jffs2_garbage_collect_pass(c) == -ENOSPC) {
213 printk(KERN_NOTICE "No space for garbage collection. Aborting GC thread\n");
214 goto die;
217 die:
218 spin_lock(&c->erase_completion_lock);
219 c->gc_task = NULL;
220 spin_unlock(&c->erase_completion_lock);
221 complete_and_exit(&c->gc_thread_exit, 0);