2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001-2003 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@redhat.com>
8 * For licensing information, see the file 'LICENCE' in this directory.
10 * $Id: gcthread.c,v 1.3 2005/01/22 16:01:12 lunn Exp $
13 #include <linux/kernel.h>
15 #include <cyg/kernel/kapi.h>
17 #define GC_THREAD_FLAG_TRIG 1
18 #define GC_THREAD_FLAG_STOP 2
19 #define GC_THREAD_FLAG_HAS_EXIT 4
21 static cyg_thread_entry_t jffs2_garbage_collect_thread;
23 void jffs2_garbage_collect_trigger(struct jffs2_sb_info *c)
25 struct super_block *sb=OFNI_BS_2SFFJ(c);
27 /* Wake up the thread */
28 D1(printk("jffs2_garbage_collect_trigger\n"));
30 cyg_flag_setbits(&sb->s_gc_thread_flags,GC_THREAD_FLAG_TRIG);
35 jffs2_start_garbage_collect_thread(struct jffs2_sb_info *c)
37 struct super_block *sb=OFNI_BS_2SFFJ(c);
40 CYG_ASSERTC(!sb->s_gc_thread_handle);
42 cyg_flag_init(&sb->s_gc_thread_flags);
43 cyg_mutex_init(&sb->s_lock);
45 D1(printk("jffs2_start_garbage_collect_thread\n"));
46 /* Start the thread. Doesn't matter if it fails -- it's only an
47 * optimisation anyway */
48 cyg_thread_create(CYGNUM_JFFS2_GC_THREAD_PRIORITY,
49 jffs2_garbage_collect_thread,
50 (cyg_addrword_t)c,"jffs2 gc thread",
51 (void*)sb->s_gc_thread_stack,
52 sizeof(sb->s_gc_thread_stack),
53 &sb->s_gc_thread_handle,
56 cyg_thread_resume(sb->s_gc_thread_handle);
60 jffs2_stop_garbage_collect_thread(struct jffs2_sb_info *c)
62 struct super_block *sb=OFNI_BS_2SFFJ(c);
64 CYG_ASSERTC(sb->s_gc_thread_handle);
66 D1(printk("jffs2_stop_garbage_collect_thread\n"));
67 /* Stop the thread and wait for it if necessary */
69 cyg_flag_setbits(&sb->s_gc_thread_flags,GC_THREAD_FLAG_STOP);
71 D1(printk("jffs2_stop_garbage_collect_thread wait\n"));
73 cyg_flag_wait(&sb->s_gc_thread_flags,
74 GC_THREAD_FLAG_HAS_EXIT,
75 CYG_FLAG_WAITMODE_OR| CYG_FLAG_WAITMODE_CLR);
77 // Kill and free the resources ... this is safe due to the flag
79 cyg_thread_kill(sb->s_gc_thread_handle);
80 cyg_thread_delete(sb->s_gc_thread_handle);
82 cyg_mutex_destroy(&sb->s_lock);
83 cyg_flag_destroy(&sb->s_gc_thread_flags);
88 jffs2_garbage_collect_thread(cyg_addrword_t data)
90 struct jffs2_sb_info *c=(struct jffs2_sb_info *)data;
91 struct super_block *sb=OFNI_BS_2SFFJ(c);
92 cyg_flag_value_t flag;
95 D1(printk("jffs2_garbage_collect_thread START\n"));
98 flag=cyg_flag_timed_wait(&sb->s_gc_thread_flags,
99 GC_THREAD_FLAG_TRIG|GC_THREAD_FLAG_STOP,
100 CYG_FLAG_WAITMODE_OR| CYG_FLAG_WAITMODE_CLR,
102 CYGNUM_JFFS2_GS_THREAD_TICKS);
104 if (flag & GC_THREAD_FLAG_STOP)
107 D1(printk("jffs2: GC THREAD GC BEGIN\n"));
109 mte=cyg_fs_root_lookup((cyg_dir *) sb->s_root);
110 CYG_ASSERT(mte, "Bad mount point");
111 cyg_fs_lock(mte, mte->fs->syncmode);
113 if (jffs2_garbage_collect_pass(c) == -ENOSPC) {
114 printf("No space for garbage collection. "
115 "Aborting JFFS2 GC thread\n");
118 cyg_fs_unlock(mte, mte->fs->syncmode);
119 D1(printk("jffs2: GC THREAD GC END\n"));
122 D1(printk("jffs2_garbage_collect_thread EXIT\n"));
123 cyg_flag_setbits(&sb->s_gc_thread_flags,GC_THREAD_FLAG_HAS_EXIT);