11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * JFFS2 -- Journalling Flash File System, Version 2. 31da177e4SLinus Torvalds * 4c00c310eSDavid Woodhouse * Copyright © 2001-2007 Red Hat, Inc. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Created by David Woodhouse <dwmw2@infradead.org> 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * For licensing information, see the file 'LICENCE' in this directory. 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds #include <linux/kernel.h> 131da177e4SLinus Torvalds #include <linux/jffs2.h> 141da177e4SLinus Torvalds #include <linux/mtd/mtd.h> 151da177e4SLinus Torvalds #include <linux/completion.h> 164e57b681STim Schmielau #include <linux/sched.h> 177dfb7103SNigel Cunningham #include <linux/freezer.h> 1891e0955bSGerard Lledo #include <linux/kthread.h> 191da177e4SLinus Torvalds #include "nodelist.h" 201da177e4SLinus Torvalds 211da177e4SLinus Torvalds 221da177e4SLinus Torvalds static int jffs2_garbage_collect_thread(void *); 231da177e4SLinus Torvalds 241da177e4SLinus Torvalds void jffs2_garbage_collect_trigger(struct jffs2_sb_info *c) 251da177e4SLinus Torvalds { 26acb64a43SDavid Woodhouse assert_spin_locked(&c->erase_completion_lock); 271da177e4SLinus Torvalds if (c->gc_task && jffs2_thread_should_wake(c)) 281da177e4SLinus Torvalds send_sig(SIGHUP, c->gc_task, 1); 291da177e4SLinus Torvalds } 301da177e4SLinus Torvalds 311da177e4SLinus Torvalds /* This must only ever be called when no GC thread is currently running */ 321da177e4SLinus Torvalds int jffs2_start_garbage_collect_thread(struct jffs2_sb_info *c) 331da177e4SLinus Torvalds { 3491e0955bSGerard Lledo struct task_struct *tsk; 351da177e4SLinus Torvalds int ret = 0; 361da177e4SLinus Torvalds 374b4d1cc7SEric Sesterhenn BUG_ON(c->gc_task); 381da177e4SLinus Torvalds 39fff7afd7SThomas Gleixner init_completion(&c->gc_thread_start); 401da177e4SLinus Torvalds init_completion(&c->gc_thread_exit); 411da177e4SLinus Torvalds 4291e0955bSGerard Lledo tsk = kthread_run(jffs2_garbage_collect_thread, c, "jffs2_gcd_mtd%d", c->mtd->index); 4391e0955bSGerard Lledo if (IS_ERR(tsk)) { 4491e0955bSGerard Lledo printk(KERN_WARNING "fork failed for JFFS2 garbage collect thread: %ld\n", -PTR_ERR(tsk)); 451da177e4SLinus Torvalds complete(&c->gc_thread_exit); 4691e0955bSGerard Lledo ret = PTR_ERR(tsk); 471da177e4SLinus Torvalds } else { 481da177e4SLinus Torvalds /* Wait for it... */ 4991e0955bSGerard Lledo D1(printk(KERN_DEBUG "JFFS2: Garbage collect thread is pid %d\n", tsk->pid)); 50fff7afd7SThomas Gleixner wait_for_completion(&c->gc_thread_start); 5191e0955bSGerard Lledo ret = tsk->pid; 521da177e4SLinus Torvalds } 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds return ret; 551da177e4SLinus Torvalds } 561da177e4SLinus Torvalds 571da177e4SLinus Torvalds void jffs2_stop_garbage_collect_thread(struct jffs2_sb_info *c) 581da177e4SLinus Torvalds { 59e2d48b1aSThomas Gleixner int wait = 0; 601da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 611da177e4SLinus Torvalds if (c->gc_task) { 621da177e4SLinus Torvalds D1(printk(KERN_DEBUG "jffs2: Killing GC task %d\n", c->gc_task->pid)); 631da177e4SLinus Torvalds send_sig(SIGKILL, c->gc_task, 1); 64e2d48b1aSThomas Gleixner wait = 1; 651da177e4SLinus Torvalds } 661da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 67e2d48b1aSThomas Gleixner if (wait) 681da177e4SLinus Torvalds wait_for_completion(&c->gc_thread_exit); 691da177e4SLinus Torvalds } 701da177e4SLinus Torvalds 711da177e4SLinus Torvalds static int jffs2_garbage_collect_thread(void *_c) 721da177e4SLinus Torvalds { 731da177e4SLinus Torvalds struct jffs2_sb_info *c = _c; 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds allow_signal(SIGKILL); 761da177e4SLinus Torvalds allow_signal(SIGSTOP); 771da177e4SLinus Torvalds allow_signal(SIGCONT); 781da177e4SLinus Torvalds 791da177e4SLinus Torvalds c->gc_task = current; 80fff7afd7SThomas Gleixner complete(&c->gc_thread_start); 811da177e4SLinus Torvalds 821da177e4SLinus Torvalds set_user_nice(current, 10); 831da177e4SLinus Torvalds 8483144186SRafael J. Wysocki set_freezable(); 851da177e4SLinus Torvalds for (;;) { 861da177e4SLinus Torvalds allow_signal(SIGHUP); 87e716dd36SDavid Woodhouse again: 88b27cf88eSDavid Woodhouse spin_lock(&c->erase_completion_lock); 891da177e4SLinus Torvalds if (!jffs2_thread_should_wake(c)) { 901da177e4SLinus Torvalds set_current_state (TASK_INTERRUPTIBLE); 91b27cf88eSDavid Woodhouse spin_unlock(&c->erase_completion_lock); 921da177e4SLinus Torvalds D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread sleeping...\n")); 931da177e4SLinus Torvalds schedule(); 94b27cf88eSDavid Woodhouse } else 95b27cf88eSDavid Woodhouse spin_unlock(&c->erase_completion_lock); 96b27cf88eSDavid Woodhouse 971da177e4SLinus Torvalds 98efab0b5dSAndres Salomon /* Problem - immediately after bootup, the GCD spends a lot 99efab0b5dSAndres Salomon * of time in places like jffs2_kill_fragtree(); so much so 100efab0b5dSAndres Salomon * that userspace processes (like gdm and X) are starved 101efab0b5dSAndres Salomon * despite plenty of cond_resched()s and renicing. Yield() 102efab0b5dSAndres Salomon * doesn't help, either (presumably because userspace and GCD 103efab0b5dSAndres Salomon * are generally competing for a higher latency resource - 104efab0b5dSAndres Salomon * disk). 105efab0b5dSAndres Salomon * This forces the GCD to slow the hell down. Pulling an 106efab0b5dSAndres Salomon * inode in with read_inode() is much preferable to having 107efab0b5dSAndres Salomon * the GC thread get there first. */ 108efab0b5dSAndres Salomon schedule_timeout_interruptible(msecs_to_jiffies(50)); 1091da177e4SLinus Torvalds 11091e0955bSGerard Lledo if (kthread_should_stop()) { 11191e0955bSGerard Lledo D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): kthread_stop() called.\n")); 11291e0955bSGerard Lledo goto die; 11391e0955bSGerard Lledo } 11491e0955bSGerard Lledo 1151da177e4SLinus Torvalds /* Put_super will send a SIGKILL and then wait on the sem. 1161da177e4SLinus Torvalds */ 117e136e769SRafael J. Wysocki while (signal_pending(current) || freezing(current)) { 1181da177e4SLinus Torvalds siginfo_t info; 1191da177e4SLinus Torvalds unsigned long signr; 1201da177e4SLinus Torvalds 121e716dd36SDavid Woodhouse if (try_to_freeze()) 122e716dd36SDavid Woodhouse goto again; 123e716dd36SDavid Woodhouse 1241da177e4SLinus Torvalds signr = dequeue_signal_lock(current, ¤t->blocked, &info); 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds switch(signr) { 1271da177e4SLinus Torvalds case SIGSTOP: 1281da177e4SLinus Torvalds D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): SIGSTOP received.\n")); 1291da177e4SLinus Torvalds set_current_state(TASK_STOPPED); 1301da177e4SLinus Torvalds schedule(); 1311da177e4SLinus Torvalds break; 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds case SIGKILL: 1341da177e4SLinus Torvalds D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): SIGKILL received.\n")); 1351da177e4SLinus Torvalds goto die; 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds case SIGHUP: 1381da177e4SLinus Torvalds D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): SIGHUP received.\n")); 1391da177e4SLinus Torvalds break; 1401da177e4SLinus Torvalds default: 1411da177e4SLinus Torvalds D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): signal %ld received\n", signr)); 1421da177e4SLinus Torvalds } 1431da177e4SLinus Torvalds } 1441da177e4SLinus Torvalds /* We don't want SIGHUP to interrupt us. STOP and KILL are OK though. */ 1451da177e4SLinus Torvalds disallow_signal(SIGHUP); 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): pass\n")); 1481da177e4SLinus Torvalds if (jffs2_garbage_collect_pass(c) == -ENOSPC) { 1491da177e4SLinus Torvalds printk(KERN_NOTICE "No space for garbage collection. Aborting GC thread\n"); 1501da177e4SLinus Torvalds goto die; 1511da177e4SLinus Torvalds } 1521da177e4SLinus Torvalds } 1531da177e4SLinus Torvalds die: 1541da177e4SLinus Torvalds spin_lock(&c->erase_completion_lock); 1551da177e4SLinus Torvalds c->gc_task = NULL; 1561da177e4SLinus Torvalds spin_unlock(&c->erase_completion_lock); 1571da177e4SLinus Torvalds complete_and_exit(&c->gc_thread_exit, 0); 1581da177e4SLinus Torvalds } 159