background.c 3.9 KB

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