11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * JFFS2 -- Journalling Flash File System, Version 2. 31da177e4SLinus Torvalds * 4c00c310eSDavid Woodhouse * Copyright © 2001-2007 Red Hat, Inc. 56088c058SDavid Woodhouse * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org> 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Created by David Woodhouse <dwmw2@infradead.org> 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * For licensing information, see the file 'LICENCE' in this directory. 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds */ 121da177e4SLinus Torvalds 135a528957SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 145a528957SJoe Perches 151da177e4SLinus Torvalds #include <linux/kernel.h> 161da177e4SLinus Torvalds #include <linux/jffs2.h> 171da177e4SLinus Torvalds #include <linux/mtd/mtd.h> 181da177e4SLinus Torvalds #include <linux/completion.h> 194e57b681STim Schmielau #include <linux/sched.h> 207dfb7103SNigel Cunningham #include <linux/freezer.h> 2191e0955bSGerard Lledo #include <linux/kthread.h> 221da177e4SLinus Torvalds #include "nodelist.h" 231da177e4SLinus Torvalds 241da177e4SLinus Torvalds 251da177e4SLinus Torvalds static int jffs2_garbage_collect_thread(void *); 261da177e4SLinus Torvalds 271da177e4SLinus Torvalds void jffs2_garbage_collect_trigger(struct jffs2_sb_info *c) 281da177e4SLinus Torvalds { 29acb64a43SDavid Woodhouse assert_spin_locked(&c->erase_completion_lock); 301da177e4SLinus Torvalds if (c->gc_task && jffs2_thread_should_wake(c)) 311da177e4SLinus Torvalds send_sig(SIGHUP, c->gc_task, 1); 321da177e4SLinus Torvalds } 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds /* This must only ever be called when no GC thread is currently running */ 351da177e4SLinus Torvalds int jffs2_start_garbage_collect_thread(struct jffs2_sb_info *c) 361da177e4SLinus Torvalds { 3791e0955bSGerard Lledo struct task_struct *tsk; 381da177e4SLinus Torvalds int ret = 0; 391da177e4SLinus Torvalds 404b4d1cc7SEric Sesterhenn BUG_ON(c->gc_task); 411da177e4SLinus Torvalds 42fff7afd7SThomas Gleixner init_completion(&c->gc_thread_start); 431da177e4SLinus Torvalds init_completion(&c->gc_thread_exit); 441da177e4SLinus Torvalds 4591e0955bSGerard Lledo tsk = kthread_run(jffs2_garbage_collect_thread, c, "jffs2_gcd_mtd%d", c->mtd->index); 4691e0955bSGerard Lledo if (IS_ERR(tsk)) { 47da320f05SJoe Perches pr_warn("fork failed for JFFS2 garbage collect thread: %ld\n", 48da320f05SJoe Perches -PTR_ERR(tsk)); 491da177e4SLinus Torvalds complete(&c->gc_thread_exit); 5091e0955bSGerard Lledo ret = PTR_ERR(tsk); 511da177e4SLinus Torvalds } else { 521da177e4SLinus Torvalds /* Wait for it... */ 535a528957SJoe Perches jffs2_dbg(1, "Garbage collect thread is pid %d\n", tsk->pid); 54fff7afd7SThomas Gleixner wait_for_completion(&c->gc_thread_start); 5591e0955bSGerard Lledo ret = tsk->pid; 561da177e4SLinus Torvalds } 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds return ret; 591da177e4SLinus Torvalds } 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds void jffs2_stop_garbage_collect_thread(struct jffs2_sb_info *c) 621da177e4SLinus Torvalds { 63e2d48b1aSThomas Gleixner int wait = 0; 641da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 651da177e4SLinus Torvalds if (c->gc_task) { 665a528957SJoe Perches jffs2_dbg(1, "Killing GC task %d\n", c->gc_task->pid); 671da177e4SLinus Torvalds send_sig(SIGKILL, c->gc_task, 1); 68e2d48b1aSThomas Gleixner wait = 1; 691da177e4SLinus Torvalds } 701da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 71e2d48b1aSThomas Gleixner if (wait) 721da177e4SLinus Torvalds wait_for_completion(&c->gc_thread_exit); 731da177e4SLinus Torvalds } 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds static int jffs2_garbage_collect_thread(void *_c) 761da177e4SLinus Torvalds { 771da177e4SLinus Torvalds struct jffs2_sb_info *c = _c; 78c240837fSOleg Nesterov sigset_t hupmask; 791da177e4SLinus Torvalds 80c240837fSOleg Nesterov siginitset(&hupmask, sigmask(SIGHUP)); 811da177e4SLinus Torvalds allow_signal(SIGKILL); 821da177e4SLinus Torvalds allow_signal(SIGSTOP); 831da177e4SLinus Torvalds allow_signal(SIGCONT); 84c240837fSOleg Nesterov allow_signal(SIGHUP); 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds c->gc_task = current; 87fff7afd7SThomas Gleixner complete(&c->gc_thread_start); 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds set_user_nice(current, 10); 901da177e4SLinus Torvalds 9183144186SRafael J. Wysocki set_freezable(); 921da177e4SLinus Torvalds for (;;) { 93c240837fSOleg Nesterov sigprocmask(SIG_UNBLOCK, &hupmask, NULL); 94e716dd36SDavid Woodhouse again: 95b27cf88eSDavid Woodhouse spin_lock(&c->erase_completion_lock); 961da177e4SLinus Torvalds if (!jffs2_thread_should_wake(c)) { 971da177e4SLinus Torvalds set_current_state (TASK_INTERRUPTIBLE); 98b27cf88eSDavid Woodhouse spin_unlock(&c->erase_completion_lock); 999c261b33SJoe Perches jffs2_dbg(1, "%s(): sleeping...\n", __func__); 1001da177e4SLinus Torvalds schedule(); 101c240837fSOleg Nesterov } else { 102b27cf88eSDavid Woodhouse spin_unlock(&c->erase_completion_lock); 103c240837fSOleg Nesterov } 104efab0b5dSAndres Salomon /* Problem - immediately after bootup, the GCD spends a lot 105efab0b5dSAndres Salomon * of time in places like jffs2_kill_fragtree(); so much so 106efab0b5dSAndres Salomon * that userspace processes (like gdm and X) are starved 107efab0b5dSAndres Salomon * despite plenty of cond_resched()s and renicing. Yield() 108efab0b5dSAndres Salomon * doesn't help, either (presumably because userspace and GCD 109efab0b5dSAndres Salomon * are generally competing for a higher latency resource - 110efab0b5dSAndres Salomon * disk). 111efab0b5dSAndres Salomon * This forces the GCD to slow the hell down. Pulling an 112efab0b5dSAndres Salomon * inode in with read_inode() is much preferable to having 113efab0b5dSAndres Salomon * the GC thread get there first. */ 114efab0b5dSAndres Salomon schedule_timeout_interruptible(msecs_to_jiffies(50)); 1151da177e4SLinus Torvalds 11691e0955bSGerard Lledo if (kthread_should_stop()) { 1179c261b33SJoe Perches jffs2_dbg(1, "%s(): kthread_stop() called\n", __func__); 11891e0955bSGerard Lledo goto die; 11991e0955bSGerard Lledo } 12091e0955bSGerard Lledo 1211da177e4SLinus Torvalds /* Put_super will send a SIGKILL and then wait on the sem. 1221da177e4SLinus Torvalds */ 123e136e769SRafael J. Wysocki while (signal_pending(current) || freezing(current)) { 1241da177e4SLinus Torvalds siginfo_t info; 1251da177e4SLinus Torvalds unsigned long signr; 1261da177e4SLinus Torvalds 127e716dd36SDavid Woodhouse if (try_to_freeze()) 128e716dd36SDavid Woodhouse goto again; 129e716dd36SDavid Woodhouse 1301da177e4SLinus Torvalds signr = dequeue_signal_lock(current, ¤t->blocked, &info); 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds switch(signr) { 1331da177e4SLinus Torvalds case SIGSTOP: 1349c261b33SJoe Perches jffs2_dbg(1, "%s(): SIGSTOP received\n", 1359c261b33SJoe Perches __func__); 1361da177e4SLinus Torvalds set_current_state(TASK_STOPPED); 1371da177e4SLinus Torvalds schedule(); 1381da177e4SLinus Torvalds break; 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds case SIGKILL: 1419c261b33SJoe Perches jffs2_dbg(1, "%s(): SIGKILL received\n", 1429c261b33SJoe Perches __func__); 1431da177e4SLinus Torvalds goto die; 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds case SIGHUP: 1469c261b33SJoe Perches jffs2_dbg(1, "%s(): SIGHUP received\n", 1479c261b33SJoe Perches __func__); 1481da177e4SLinus Torvalds break; 1491da177e4SLinus Torvalds default: 1509c261b33SJoe Perches jffs2_dbg(1, "%s(): signal %ld received\n", 1519c261b33SJoe Perches __func__, signr); 1521da177e4SLinus Torvalds } 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds /* We don't want SIGHUP to interrupt us. STOP and KILL are OK though. */ 155c240837fSOleg Nesterov sigprocmask(SIG_BLOCK, &hupmask, NULL); 1561da177e4SLinus Torvalds 1579c261b33SJoe Perches jffs2_dbg(1, "%s(): pass\n", __func__); 1581da177e4SLinus Torvalds if (jffs2_garbage_collect_pass(c) == -ENOSPC) { 159da320f05SJoe Perches pr_notice("No space for garbage collection. Aborting GC thread\n"); 1601da177e4SLinus Torvalds goto die; 1611da177e4SLinus Torvalds } 1621da177e4SLinus Torvalds } 1631da177e4SLinus Torvalds die: 1641da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 1651da177e4SLinus Torvalds c->gc_task = NULL; 1661da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 1671da177e4SLinus Torvalds complete_and_exit(&c->gc_thread_exit, 0); 1681da177e4SLinus Torvalds } 169