xref: /OK3568_Linux_fs/kernel/fs/jffs2/background.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * JFFS2 -- Journalling Flash File System, Version 2.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright © 2001-2007 Red Hat, Inc.
5*4882a593Smuzhiyun  * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Created by David Woodhouse <dwmw2@infradead.org>
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * For licensing information, see the file 'LICENCE' in this directory.
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  */
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #include <linux/kernel.h>
16*4882a593Smuzhiyun #include <linux/jffs2.h>
17*4882a593Smuzhiyun #include <linux/mtd/mtd.h>
18*4882a593Smuzhiyun #include <linux/completion.h>
19*4882a593Smuzhiyun #include <linux/sched/signal.h>
20*4882a593Smuzhiyun #include <linux/freezer.h>
21*4882a593Smuzhiyun #include <linux/kthread.h>
22*4882a593Smuzhiyun #include "nodelist.h"
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun static int jffs2_garbage_collect_thread(void *);
26*4882a593Smuzhiyun 
jffs2_garbage_collect_trigger(struct jffs2_sb_info * c)27*4882a593Smuzhiyun void jffs2_garbage_collect_trigger(struct jffs2_sb_info *c)
28*4882a593Smuzhiyun {
29*4882a593Smuzhiyun 	assert_spin_locked(&c->erase_completion_lock);
30*4882a593Smuzhiyun 	if (c->gc_task && jffs2_thread_should_wake(c))
31*4882a593Smuzhiyun 		send_sig(SIGHUP, c->gc_task, 1);
32*4882a593Smuzhiyun }
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun /* This must only ever be called when no GC thread is currently running */
jffs2_start_garbage_collect_thread(struct jffs2_sb_info * c)35*4882a593Smuzhiyun int jffs2_start_garbage_collect_thread(struct jffs2_sb_info *c)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun 	struct task_struct *tsk;
38*4882a593Smuzhiyun 	int ret = 0;
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun 	BUG_ON(c->gc_task);
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	init_completion(&c->gc_thread_start);
43*4882a593Smuzhiyun 	init_completion(&c->gc_thread_exit);
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun 	tsk = kthread_run(jffs2_garbage_collect_thread, c, "jffs2_gcd_mtd%d", c->mtd->index);
46*4882a593Smuzhiyun 	if (IS_ERR(tsk)) {
47*4882a593Smuzhiyun 		pr_warn("fork failed for JFFS2 garbage collect thread: %ld\n",
48*4882a593Smuzhiyun 			-PTR_ERR(tsk));
49*4882a593Smuzhiyun 		complete(&c->gc_thread_exit);
50*4882a593Smuzhiyun 		ret = PTR_ERR(tsk);
51*4882a593Smuzhiyun 	} else {
52*4882a593Smuzhiyun 		/* Wait for it... */
53*4882a593Smuzhiyun 		jffs2_dbg(1, "Garbage collect thread is pid %d\n", tsk->pid);
54*4882a593Smuzhiyun 		wait_for_completion(&c->gc_thread_start);
55*4882a593Smuzhiyun 		ret = tsk->pid;
56*4882a593Smuzhiyun 	}
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	return ret;
59*4882a593Smuzhiyun }
60*4882a593Smuzhiyun 
jffs2_stop_garbage_collect_thread(struct jffs2_sb_info * c)61*4882a593Smuzhiyun void jffs2_stop_garbage_collect_thread(struct jffs2_sb_info *c)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun 	int wait = 0;
64*4882a593Smuzhiyun 	spin_lock(&c->erase_completion_lock);
65*4882a593Smuzhiyun 	if (c->gc_task) {
66*4882a593Smuzhiyun 		jffs2_dbg(1, "Killing GC task %d\n", c->gc_task->pid);
67*4882a593Smuzhiyun 		send_sig(SIGKILL, c->gc_task, 1);
68*4882a593Smuzhiyun 		wait = 1;
69*4882a593Smuzhiyun 	}
70*4882a593Smuzhiyun 	spin_unlock(&c->erase_completion_lock);
71*4882a593Smuzhiyun 	if (wait)
72*4882a593Smuzhiyun 		wait_for_completion(&c->gc_thread_exit);
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun 
jffs2_garbage_collect_thread(void * _c)75*4882a593Smuzhiyun static int jffs2_garbage_collect_thread(void *_c)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun 	struct jffs2_sb_info *c = _c;
78*4882a593Smuzhiyun 	sigset_t hupmask;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	siginitset(&hupmask, sigmask(SIGHUP));
81*4882a593Smuzhiyun 	allow_signal(SIGKILL);
82*4882a593Smuzhiyun 	allow_signal(SIGSTOP);
83*4882a593Smuzhiyun 	allow_signal(SIGHUP);
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	c->gc_task = current;
86*4882a593Smuzhiyun 	complete(&c->gc_thread_start);
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	set_user_nice(current, 10);
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	set_freezable();
91*4882a593Smuzhiyun 	for (;;) {
92*4882a593Smuzhiyun 		sigprocmask(SIG_UNBLOCK, &hupmask, NULL);
93*4882a593Smuzhiyun 	again:
94*4882a593Smuzhiyun 		spin_lock(&c->erase_completion_lock);
95*4882a593Smuzhiyun 		if (!jffs2_thread_should_wake(c)) {
96*4882a593Smuzhiyun 			set_current_state (TASK_INTERRUPTIBLE);
97*4882a593Smuzhiyun 			spin_unlock(&c->erase_completion_lock);
98*4882a593Smuzhiyun 			jffs2_dbg(1, "%s(): sleeping...\n", __func__);
99*4882a593Smuzhiyun 			schedule();
100*4882a593Smuzhiyun 		} else {
101*4882a593Smuzhiyun 			spin_unlock(&c->erase_completion_lock);
102*4882a593Smuzhiyun 		}
103*4882a593Smuzhiyun 		/* Problem - immediately after bootup, the GCD spends a lot
104*4882a593Smuzhiyun 		 * of time in places like jffs2_kill_fragtree(); so much so
105*4882a593Smuzhiyun 		 * that userspace processes (like gdm and X) are starved
106*4882a593Smuzhiyun 		 * despite plenty of cond_resched()s and renicing.  Yield()
107*4882a593Smuzhiyun 		 * doesn't help, either (presumably because userspace and GCD
108*4882a593Smuzhiyun 		 * are generally competing for a higher latency resource -
109*4882a593Smuzhiyun 		 * disk).
110*4882a593Smuzhiyun 		 * This forces the GCD to slow the hell down.   Pulling an
111*4882a593Smuzhiyun 		 * inode in with read_inode() is much preferable to having
112*4882a593Smuzhiyun 		 * the GC thread get there first. */
113*4882a593Smuzhiyun 		schedule_timeout_interruptible(msecs_to_jiffies(50));
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 		if (kthread_should_stop()) {
116*4882a593Smuzhiyun 			jffs2_dbg(1, "%s(): kthread_stop() called\n", __func__);
117*4882a593Smuzhiyun 			goto die;
118*4882a593Smuzhiyun 		}
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 		/* Put_super will send a SIGKILL and then wait on the sem.
121*4882a593Smuzhiyun 		 */
122*4882a593Smuzhiyun 		while (signal_pending(current) || freezing(current)) {
123*4882a593Smuzhiyun 			unsigned long signr;
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 			if (try_to_freeze())
126*4882a593Smuzhiyun 				goto again;
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 			signr = kernel_dequeue_signal();
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 			switch(signr) {
131*4882a593Smuzhiyun 			case SIGSTOP:
132*4882a593Smuzhiyun 				jffs2_dbg(1, "%s(): SIGSTOP received\n",
133*4882a593Smuzhiyun 					  __func__);
134*4882a593Smuzhiyun 				kernel_signal_stop();
135*4882a593Smuzhiyun 				break;
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 			case SIGKILL:
138*4882a593Smuzhiyun 				jffs2_dbg(1, "%s(): SIGKILL received\n",
139*4882a593Smuzhiyun 					  __func__);
140*4882a593Smuzhiyun 				goto die;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 			case SIGHUP:
143*4882a593Smuzhiyun 				jffs2_dbg(1, "%s(): SIGHUP received\n",
144*4882a593Smuzhiyun 					  __func__);
145*4882a593Smuzhiyun 				break;
146*4882a593Smuzhiyun 			default:
147*4882a593Smuzhiyun 				jffs2_dbg(1, "%s(): signal %ld received\n",
148*4882a593Smuzhiyun 					  __func__, signr);
149*4882a593Smuzhiyun 			}
150*4882a593Smuzhiyun 		}
151*4882a593Smuzhiyun 		/* We don't want SIGHUP to interrupt us. STOP and KILL are OK though. */
152*4882a593Smuzhiyun 		sigprocmask(SIG_BLOCK, &hupmask, NULL);
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 		jffs2_dbg(1, "%s(): pass\n", __func__);
155*4882a593Smuzhiyun 		if (jffs2_garbage_collect_pass(c) == -ENOSPC) {
156*4882a593Smuzhiyun 			pr_notice("No space for garbage collection. Aborting GC thread\n");
157*4882a593Smuzhiyun 			goto die;
158*4882a593Smuzhiyun 		}
159*4882a593Smuzhiyun 	}
160*4882a593Smuzhiyun  die:
161*4882a593Smuzhiyun 	spin_lock(&c->erase_completion_lock);
162*4882a593Smuzhiyun 	c->gc_task = NULL;
163*4882a593Smuzhiyun 	spin_unlock(&c->erase_completion_lock);
164*4882a593Smuzhiyun 	complete_and_exit(&c->gc_thread_exit, 0);
165*4882a593Smuzhiyun }
166