background.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * JFFS2 -- Journalling Flash File System, Version 2.
  3. *
  4. * Copyright (C) 2001-2003 Red Hat, Inc.
  5. *
  6. * Created by David Woodhouse <dwmw2@infradead.org>
  7. *
  8. * For licensing information, see the file 'LICENCE' in this directory.
  9. *
  10. * $Id: background.c,v 1.54 2005/05/20 21:37:12 gleixner Exp $
  11. *
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/jffs2.h>
  15. #include <linux/mtd/mtd.h>
  16. #include <linux/completion.h>
  17. #include <linux/sched.h>
  18. #include <linux/freezer.h>
  19. #include "nodelist.h"
  20. static int jffs2_garbage_collect_thread(void *);
  21. void jffs2_garbage_collect_trigger(struct jffs2_sb_info *c)
  22. {
  23. spin_lock(&c->erase_completion_lock);
  24. if (c->gc_task && jffs2_thread_should_wake(c))
  25. send_sig(SIGHUP, c->gc_task, 1);
  26. spin_unlock(&c->erase_completion_lock);
  27. }
  28. /* This must only ever be called when no GC thread is currently running */
  29. int jffs2_start_garbage_collect_thread(struct jffs2_sb_info *c)
  30. {
  31. pid_t pid;
  32. int ret = 0;
  33. BUG_ON(c->gc_task);
  34. init_completion(&c->gc_thread_start);
  35. init_completion(&c->gc_thread_exit);
  36. pid = kernel_thread(jffs2_garbage_collect_thread, c, CLONE_FS|CLONE_FILES);
  37. if (pid < 0) {
  38. printk(KERN_WARNING "fork failed for JFFS2 garbage collect thread: %d\n", -pid);
  39. complete(&c->gc_thread_exit);
  40. ret = pid;
  41. } else {
  42. /* Wait for it... */
  43. D1(printk(KERN_DEBUG "JFFS2: Garbage collect thread is pid %d\n", pid));
  44. wait_for_completion(&c->gc_thread_start);
  45. }
  46. return ret;
  47. }
  48. void jffs2_stop_garbage_collect_thread(struct jffs2_sb_info *c)
  49. {
  50. int wait = 0;
  51. spin_lock(&c->erase_completion_lock);
  52. if (c->gc_task) {
  53. D1(printk(KERN_DEBUG "jffs2: Killing GC task %d\n", c->gc_task->pid));
  54. send_sig(SIGKILL, c->gc_task, 1);
  55. wait = 1;
  56. }
  57. spin_unlock(&c->erase_completion_lock);
  58. if (wait)
  59. wait_for_completion(&c->gc_thread_exit);
  60. }
  61. static int jffs2_garbage_collect_thread(void *_c)
  62. {
  63. struct jffs2_sb_info *c = _c;
  64. daemonize("jffs2_gcd_mtd%d", c->mtd->index);
  65. allow_signal(SIGKILL);
  66. allow_signal(SIGSTOP);
  67. allow_signal(SIGCONT);
  68. c->gc_task = current;
  69. complete(&c->gc_thread_start);
  70. set_user_nice(current, 10);
  71. for (;;) {
  72. allow_signal(SIGHUP);
  73. if (!jffs2_thread_should_wake(c)) {
  74. set_current_state (TASK_INTERRUPTIBLE);
  75. D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread sleeping...\n"));
  76. /* Yes, there's a race here; we checked jffs2_thread_should_wake()
  77. before setting current->state to TASK_INTERRUPTIBLE. But it doesn't
  78. matter - We don't care if we miss a wakeup, because the GC thread
  79. is only an optimisation anyway. */
  80. schedule();
  81. }
  82. if (try_to_freeze())
  83. continue;
  84. /* This thread is purely an optimisation. But if it runs when
  85. other things could be running, it actually makes things a
  86. lot worse. Use yield() and put it at the back of the runqueue
  87. every time. Especially during boot, pulling an inode in
  88. with read_inode() is much preferable to having the GC thread
  89. get there first. */
  90. yield();
  91. /* Put_super will send a SIGKILL and then wait on the sem.
  92. */
  93. while (signal_pending(current)) {
  94. siginfo_t info;
  95. unsigned long signr;
  96. signr = dequeue_signal_lock(current, &current->blocked, &info);
  97. switch(signr) {
  98. case SIGSTOP:
  99. D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): SIGSTOP received.\n"));
  100. set_current_state(TASK_STOPPED);
  101. schedule();
  102. break;
  103. case SIGKILL:
  104. D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): SIGKILL received.\n"));
  105. goto die;
  106. case SIGHUP:
  107. D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): SIGHUP received.\n"));
  108. break;
  109. default:
  110. D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): signal %ld received\n", signr));
  111. }
  112. }
  113. /* We don't want SIGHUP to interrupt us. STOP and KILL are OK though. */
  114. disallow_signal(SIGHUP);
  115. D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): pass\n"));
  116. if (jffs2_garbage_collect_pass(c) == -ENOSPC) {
  117. printk(KERN_NOTICE "No space for garbage collection. Aborting GC thread\n");
  118. goto die;
  119. }
  120. }
  121. die:
  122. spin_lock(&c->erase_completion_lock);
  123. c->gc_task = NULL;
  124. spin_unlock(&c->erase_completion_lock);
  125. complete_and_exit(&c->gc_thread_exit, 0);
  126. }