Blame view

fs/jffs2/background.c 4.36 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
  /*
   * JFFS2 -- Journalling Flash File System, Version 2.
   *
c00c310ea   David Woodhouse   [JFFS2] Tidy up l...
4
   * Copyright © 2001-2007 Red Hat, Inc.
6088c0587   David Woodhouse   jffs2: Update cop...
5
   * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
6
7
8
9
10
   *
   * Created by David Woodhouse <dwmw2@infradead.org>
   *
   * For licensing information, see the file 'LICENCE' in this directory.
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
11
12
13
14
15
16
   */
  
  #include <linux/kernel.h>
  #include <linux/jffs2.h>
  #include <linux/mtd/mtd.h>
  #include <linux/completion.h>
4e57b6817   Tim Schmielau   [PATCH] fix missi...
17
  #include <linux/sched.h>
7dfb71030   Nigel Cunningham   [PATCH] Add inclu...
18
  #include <linux/freezer.h>
91e0955b5   Gerard Lledo   jffs2: move jffs2...
19
  #include <linux/kthread.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20
21
22
23
24
25
26
  #include "nodelist.h"
  
  
  static int jffs2_garbage_collect_thread(void *);
  
  void jffs2_garbage_collect_trigger(struct jffs2_sb_info *c)
  {
acb64a43e   David Woodhouse   jffs2: Require jf...
27
  	assert_spin_locked(&c->erase_completion_lock);
ef53cb02f   David Woodhouse   [JFFS2] Whitespac...
28
29
  	if (c->gc_task && jffs2_thread_should_wake(c))
  		send_sig(SIGHUP, c->gc_task, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
30
31
32
33
34
  }
  
  /* This must only ever be called when no GC thread is currently running */
  int jffs2_start_garbage_collect_thread(struct jffs2_sb_info *c)
  {
91e0955b5   Gerard Lledo   jffs2: move jffs2...
35
  	struct task_struct *tsk;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
36
  	int ret = 0;
4b4d1cc73   Eric Sesterhenn   BUG_ON() Conversi...
37
  	BUG_ON(c->gc_task);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
38

fff7afd79   Thomas Gleixner   [JFFS2] Convert t...
39
  	init_completion(&c->gc_thread_start);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
40
  	init_completion(&c->gc_thread_exit);
91e0955b5   Gerard Lledo   jffs2: move jffs2...
41
42
43
44
  	tsk = kthread_run(jffs2_garbage_collect_thread, c, "jffs2_gcd_mtd%d", c->mtd->index);
  	if (IS_ERR(tsk)) {
  		printk(KERN_WARNING "fork failed for JFFS2 garbage collect thread: %ld
  ", -PTR_ERR(tsk));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
45
  		complete(&c->gc_thread_exit);
91e0955b5   Gerard Lledo   jffs2: move jffs2...
46
  		ret = PTR_ERR(tsk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
47
48
  	} else {
  		/* Wait for it... */
91e0955b5   Gerard Lledo   jffs2: move jffs2...
49
50
  		D1(printk(KERN_DEBUG "JFFS2: Garbage collect thread is pid %d
  ", tsk->pid));
fff7afd79   Thomas Gleixner   [JFFS2] Convert t...
51
  		wait_for_completion(&c->gc_thread_start);
91e0955b5   Gerard Lledo   jffs2: move jffs2...
52
  		ret = tsk->pid;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
53
  	}
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
54

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
55
56
57
58
59
  	return ret;
  }
  
  void jffs2_stop_garbage_collect_thread(struct jffs2_sb_info *c)
  {
e2d48b1a9   Thomas Gleixner   [JFFS2] Fix clean...
60
  	int wait = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
61
62
63
64
65
  	spin_lock(&c->erase_completion_lock);
  	if (c->gc_task) {
  		D1(printk(KERN_DEBUG "jffs2: Killing GC task %d
  ", c->gc_task->pid));
  		send_sig(SIGKILL, c->gc_task, 1);
e2d48b1a9   Thomas Gleixner   [JFFS2] Fix clean...
66
  		wait = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
67
68
  	}
  	spin_unlock(&c->erase_completion_lock);
e2d48b1a9   Thomas Gleixner   [JFFS2] Fix clean...
69
70
  	if (wait)
  		wait_for_completion(&c->gc_thread_exit);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
71
72
73
74
75
  }
  
  static int jffs2_garbage_collect_thread(void *_c)
  {
  	struct jffs2_sb_info *c = _c;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
76
77
78
79
80
  	allow_signal(SIGKILL);
  	allow_signal(SIGSTOP);
  	allow_signal(SIGCONT);
  
  	c->gc_task = current;
fff7afd79   Thomas Gleixner   [JFFS2] Convert t...
81
  	complete(&c->gc_thread_start);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
82
83
  
  	set_user_nice(current, 10);
831441862   Rafael J. Wysocki   Freezer: make ker...
84
  	set_freezable();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
85
86
  	for (;;) {
  		allow_signal(SIGHUP);
e716dd364   David Woodhouse   [JFFS2] Fix suspe...
87
  	again:
b27cf88e9   David Woodhouse   [JFFS2] Fix lack ...
88
  		spin_lock(&c->erase_completion_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
89
90
  		if (!jffs2_thread_should_wake(c)) {
  			set_current_state (TASK_INTERRUPTIBLE);
b27cf88e9   David Woodhouse   [JFFS2] Fix lack ...
91
  			spin_unlock(&c->erase_completion_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
92
93
  			D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread sleeping...
  "));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
94
  			schedule();
b27cf88e9   David Woodhouse   [JFFS2] Fix lack ...
95
96
97
  		} else
  			spin_unlock(&c->erase_completion_lock);
  			
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
98

efab0b5d3   Andres Salomon   [JFFS2] force the...
99
100
101
102
103
104
105
106
107
108
109
  		/* Problem - immediately after bootup, the GCD spends a lot
  		 * of time in places like jffs2_kill_fragtree(); so much so
  		 * that userspace processes (like gdm and X) are starved
  		 * despite plenty of cond_resched()s and renicing.  Yield()
  		 * doesn't help, either (presumably because userspace and GCD
  		 * are generally competing for a higher latency resource -
  		 * disk).
  		 * This forces the GCD to slow the hell down.   Pulling an
  		 * inode in with read_inode() is much preferable to having
  		 * the GC thread get there first. */
  		schedule_timeout_interruptible(msecs_to_jiffies(50));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
110

91e0955b5   Gerard Lledo   jffs2: move jffs2...
111
112
113
114
115
  		if (kthread_should_stop()) {
  			D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread():  kthread_stop() called.
  "));
  			goto die;
  		}
182ec4eee   Thomas Gleixner   [JFFS2] Clean up ...
116
  		/* Put_super will send a SIGKILL and then wait on the sem.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
117
  		 */
e136e769d   Rafael J. Wysocki   Freezer: Fix JFFS...
118
  		while (signal_pending(current) || freezing(current)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
119
120
  			siginfo_t info;
  			unsigned long signr;
e716dd364   David Woodhouse   [JFFS2] Fix suspe...
121
122
  			if (try_to_freeze())
  				goto again;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
  			signr = dequeue_signal_lock(current, &current->blocked, &info);
  
  			switch(signr) {
  			case SIGSTOP:
  				D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): SIGSTOP received.
  "));
  				set_current_state(TASK_STOPPED);
  				schedule();
  				break;
  
  			case SIGKILL:
  				D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): SIGKILL received.
  "));
  				goto die;
  
  			case SIGHUP:
  				D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): SIGHUP received.
  "));
  				break;
  			default:
  				D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): signal %ld received
  ", signr));
  			}
  		}
  		/* We don't want SIGHUP to interrupt us. STOP and KILL are OK though. */
  		disallow_signal(SIGHUP);
  
  		D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): pass
  "));
  		if (jffs2_garbage_collect_pass(c) == -ENOSPC) {
  			printk(KERN_NOTICE "No space for garbage collection. Aborting GC thread
  ");
  			goto die;
  		}
  	}
   die:
  	spin_lock(&c->erase_completion_lock);
  	c->gc_task = NULL;
  	spin_unlock(&c->erase_completion_lock);
  	complete_and_exit(&c->gc_thread_exit, 0);
  }