ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / jffs2 / background.c
1 /*
2  * JFFS2 -- Journalling Flash File System, Version 2.
3  *
4  * Copyright (C) 2001-2003 Red Hat, Inc.
5  *
6  * Created by David Woodhouse <dwmw2@redhat.com>
7  *
8  * For licensing information, see the file 'LICENCE' in this directory.
9  *
10  * $Id: background.c,v 1.44 2003/10/08 13:29:55 dwmw2 Exp $
11  *
12  */
13
14 #include <linux/kernel.h>
15 #include <linux/jffs2.h>
16 #include <linux/mtd/mtd.h>
17 #include <linux/completion.h>
18 #include <linux/suspend.h>
19 #include "nodelist.h"
20
21
22 static int jffs2_garbage_collect_thread(void *);
23 static int thread_should_wake(struct jffs2_sb_info *c);
24
25 void jffs2_garbage_collect_trigger(struct jffs2_sb_info *c)
26 {
27         spin_lock(&c->erase_completion_lock);
28         if (c->gc_task && thread_should_wake(c))
29                 send_sig(SIGHUP, c->gc_task, 1);
30         spin_unlock(&c->erase_completion_lock);
31 }
32
33 /* This must only ever be called when no GC thread is currently running */
34 int jffs2_start_garbage_collect_thread(struct jffs2_sb_info *c)
35 {
36         pid_t pid;
37         int ret = 0;
38
39         if (c->gc_task)
40                 BUG();
41
42         init_MUTEX_LOCKED(&c->gc_thread_start);
43         init_completion(&c->gc_thread_exit);
44
45         pid = kernel_thread(jffs2_garbage_collect_thread, c, CLONE_FS|CLONE_FILES);
46         if (pid < 0) {
47                 printk(KERN_WARNING "fork failed for JFFS2 garbage collect thread: %d\n", -pid);
48                 complete(&c->gc_thread_exit);
49                 ret = pid;
50         } else {
51                 /* Wait for it... */
52                 D1(printk(KERN_DEBUG "JFFS2: Garbage collect thread is pid %d\n", pid));
53                 down(&c->gc_thread_start);
54         }
55  
56         return ret;
57 }
58
59 void jffs2_stop_garbage_collect_thread(struct jffs2_sb_info *c)
60 {
61         spin_lock(&c->erase_completion_lock);
62         if (c->gc_task) {
63                 D1(printk(KERN_DEBUG "jffs2: Killing GC task %d\n", c->gc_task->pid));
64                 send_sig(SIGKILL, c->gc_task, 1);
65         }
66         spin_unlock(&c->erase_completion_lock);
67         wait_for_completion(&c->gc_thread_exit);
68 }
69
70 static int jffs2_garbage_collect_thread(void *_c)
71 {
72         struct jffs2_sb_info *c = _c;
73
74         daemonize("jffs2_gcd_mtd%d", c->mtd->index);
75         allow_signal(SIGKILL);
76         allow_signal(SIGSTOP);
77         allow_signal(SIGCONT);
78
79         c->gc_task = current;
80         up(&c->gc_thread_start);
81
82         set_user_nice(current, 10);
83
84         for (;;) {
85                 allow_signal(SIGHUP);
86
87                 if (!thread_should_wake(c)) {
88                         set_current_state (TASK_INTERRUPTIBLE);
89                         D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread sleeping...\n"));
90                         /* Yes, there's a race here; we checked thread_should_wake() before
91                            setting current->state to TASK_INTERRUPTIBLE. But it doesn't
92                            matter - We don't care if we miss a wakeup, because the GC thread
93                            is only an optimisation anyway. */
94                         schedule();
95                 }
96
97                 if (current->flags & PF_FREEZE) {
98                         refrigerator(0);
99                         /* refrigerator() should recalc sigpending for us
100                            but doesn't. No matter - allow_signal() will. */
101                         continue;
102                 }
103
104                 cond_resched();
105
106                 /* Put_super will send a SIGKILL and then wait on the sem. 
107                  */
108                 while (signal_pending(current)) {
109                         siginfo_t info;
110                         unsigned long signr;
111
112                         signr = dequeue_signal_lock(current, &current->blocked, &info);
113
114                         switch(signr) {
115                         case SIGSTOP:
116                                 D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): SIGSTOP received.\n"));
117                                 set_current_state(TASK_STOPPED);
118                                 schedule();
119                                 break;
120
121                         case SIGKILL:
122                                 D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): SIGKILL received.\n"));
123                                 goto die;
124
125                         case SIGHUP:
126                                 D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): SIGHUP received.\n"));
127                                 break;
128                         default:
129                                 D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): signal %ld received\n", signr));
130                         }
131                 }
132                 /* We don't want SIGHUP to interrupt us. STOP and KILL are OK though. */
133                 disallow_signal(SIGHUP);
134
135                 D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): pass\n"));
136                 if (jffs2_garbage_collect_pass(c) == -ENOSPC) {
137                         printk(KERN_NOTICE "No space for garbage collection. Aborting GC thread\n");
138                         goto die;
139                 }
140         }
141  die:
142         spin_lock(&c->erase_completion_lock);
143         c->gc_task = NULL;
144         spin_unlock(&c->erase_completion_lock);
145         complete_and_exit(&c->gc_thread_exit, 0);
146 }
147
148 static int thread_should_wake(struct jffs2_sb_info *c)
149 {
150         int ret = 0;
151         uint32_t dirty;
152
153         if (c->unchecked_size) {
154                 D1(printk(KERN_DEBUG "thread_should_wake(): unchecked_size %d, checked_ino #%d\n",
155                           c->unchecked_size, c->checked_ino));
156                 return 1;
157         }
158
159         /* dirty_size contains blocks on erase_pending_list
160          * those blocks are counted in c->nr_erasing_blocks.
161          * If one block is actually erased, it is not longer counted as dirty_space
162          * but it is counted in c->nr_erasing_blocks, so we add it and subtract it
163          * with c->nr_erasing_blocks * c->sector_size again.
164          * Blocks on erasable_list are counted as dirty_size, but not in c->nr_erasing_blocks
165          * This helps us to force gc and pick eventually a clean block to spread the load.
166          */
167         dirty = c->dirty_size + c->erasing_size - c->nr_erasing_blocks * c->sector_size;
168
169         if (c->nr_free_blocks + c->nr_erasing_blocks < c->resv_blocks_gctrigger && 
170                         (dirty > c->nospc_dirty_size)) 
171                 ret = 1;
172
173         D1(printk(KERN_DEBUG "thread_should_wake(): nr_free_blocks %d, nr_erasing_blocks %d, dirty_size 0x%x: %s\n", 
174                   c->nr_free_blocks, c->nr_erasing_blocks, c->dirty_size, ret?"yes":"no"));
175
176         return ret;
177 }