background.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 "nodelist.h"
  19. static int jffs2_garbage_collect_thread(void *);
  20. void jffs2_garbage_collect_trigger(struct jffs2_sb_info *c)
  21. {
  22. spin_lock(&c->erase_completion_lock);
  23. if (c->gc_task && jffs2_thread_should_wake(c))
  24. send_sig(SIGHUP, c->gc_task, 1);
  25. spin_unlock(&c->erase_completion_lock);
  26. }
  27. /* This must only ever be called when no GC thread is currently running */
  28. int jffs2_start_garbage_collect_thread(struct jffs2_sb_info *c)
  29. {
  30. pid_t pid;
  31. int ret = 0;
  32. BUG_ON(c->gc_task);
  33. init_completion(&c->gc_thread_start);
  34. init_completion(&c->gc_thread_exit);
  35. pid = kernel_thread(jffs2_garbage_collect_thread, c, CLONE_FS|CLONE_FILES);
  36. if (pid < 0) {
  37. printk(KERN_WARNING "fork failed for JFFS2 garbage collect thread: %d\n", -pid);
  38. complete(&c->gc_thread_exit);
  39. ret = pid;
  40. } else {
  41. /* Wait for it... */
  42. D1(printk(KERN_DEBUG "JFFS2: Garbage collect thread is pid %d\n", pid));
  43. wait_for_completion(&c->gc_thread_start);
  44. }
  45. return ret;
  46. }
  47. void jffs2_stop_garbage_collect_thread(struct jffs2_sb_info *c)
  48. {
  49. int wait = 0;
  50. spin_lock(&c->erase_completion_lock);
  51. if (c->gc_task) {
  52. D1(printk(KERN_DEBUG "jffs2: Killing GC task %d\n", c->gc_task->pid));
  53. send_sig(SIGKILL, c->gc_task, 1);
  54. wait = 1;
  55. }
  56. spin_unlock(&c->erase_completion_lock);
  57. if (wait)
  58. wait_for_completion(&c->gc_thread_exit);
  59. }
  60. static int jffs2_garbage_collect_thread(void *_c)
  61. {
  62. struct jffs2_sb_info *c = _c;
  63. daemonize("jffs2_gcd_mtd%d", c->mtd->index);
  64. allow_signal(SIGKILL);
  65. allow_signal(SIGSTOP);
  66. allow_signal(SIGCONT);
  67. c->gc_task = current;
  68. complete(&c->gc_thread_start);
  69. set_user_nice(current, 10);
  70. for (;;) {
  71. allow_signal(SIGHUP);
  72. if (!jffs2_thread_should_wake(c)) {
  73. set_current_state (TASK_INTERRUPTIBLE);
  74. D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread sleeping...\n"));
  75. /* Yes, there's a race here; we checked jffs2_thread_should_wake()
  76. before setting current->state to TASK_INTERRUPTIBLE. But it doesn't
  77. matter - We don't care if we miss a wakeup, because the GC thread
  78. is only an optimisation anyway. */
  79. schedule();
  80. }
  81. if (try_to_freeze())
  82. continue;
  83. cond_resched();
  84. /* Put_super will send a SIGKILL and then wait on the sem.
  85. */
  86. while (signal_pending(current)) {
  87. siginfo_t info;
  88. unsigned long signr;
  89. signr = dequeue_signal_lock(current, &current->blocked, &info);
  90. switch(signr) {
  91. case SIGSTOP:
  92. D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): SIGSTOP received.\n"));
  93. set_current_state(TASK_STOPPED);
  94. schedule();
  95. break;
  96. case SIGKILL:
  97. D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): SIGKILL received.\n"));
  98. goto die;
  99. case SIGHUP:
  100. D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): SIGHUP received.\n"));
  101. break;
  102. default:
  103. D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): signal %ld received\n", signr));
  104. }
  105. }
  106. /* We don't want SIGHUP to interrupt us. STOP and KILL are OK though. */
  107. disallow_signal(SIGHUP);
  108. D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): pass\n"));
  109. if (jffs2_garbage_collect_pass(c) == -ENOSPC) {
  110. printk(KERN_NOTICE "No space for garbage collection. Aborting GC thread\n");
  111. goto die;
  112. }
  113. }
  114. die:
  115. spin_lock(&c->erase_completion_lock);
  116. c->gc_task = NULL;
  117. spin_unlock(&c->erase_completion_lock);
  118. complete_and_exit(&c->gc_thread_exit, 0);
  119. }