fs_pin.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/fs.h>
  3. #include <linux/sched.h>
  4. #include <linux/slab.h>
  5. #include "internal.h"
  6. #include "mount.h"
  7. static DEFINE_SPINLOCK(pin_lock);
  8. void pin_remove(struct fs_pin *pin)
  9. {
  10. spin_lock(&pin_lock);
  11. hlist_del_init(&pin->m_list);
  12. hlist_del_init(&pin->s_list);
  13. spin_unlock(&pin_lock);
  14. spin_lock_irq(&pin->wait.lock);
  15. pin->done = 1;
  16. wake_up_locked(&pin->wait);
  17. spin_unlock_irq(&pin->wait.lock);
  18. }
  19. void pin_insert(struct fs_pin *pin, struct vfsmount *m)
  20. {
  21. spin_lock(&pin_lock);
  22. hlist_add_head(&pin->s_list, &m->mnt_sb->s_pins);
  23. hlist_add_head(&pin->m_list, &real_mount(m)->mnt_pins);
  24. spin_unlock(&pin_lock);
  25. }
  26. void pin_kill(struct fs_pin *p)
  27. {
  28. wait_queue_entry_t wait;
  29. if (!p) {
  30. rcu_read_unlock();
  31. return;
  32. }
  33. init_wait(&wait);
  34. spin_lock_irq(&p->wait.lock);
  35. if (likely(!p->done)) {
  36. p->done = -1;
  37. spin_unlock_irq(&p->wait.lock);
  38. rcu_read_unlock();
  39. p->kill(p);
  40. return;
  41. }
  42. if (p->done > 0) {
  43. spin_unlock_irq(&p->wait.lock);
  44. rcu_read_unlock();
  45. return;
  46. }
  47. __add_wait_queue(&p->wait, &wait);
  48. while (1) {
  49. set_current_state(TASK_UNINTERRUPTIBLE);
  50. spin_unlock_irq(&p->wait.lock);
  51. rcu_read_unlock();
  52. schedule();
  53. rcu_read_lock();
  54. if (likely(list_empty(&wait.entry)))
  55. break;
  56. /* OK, we know p couldn't have been freed yet */
  57. spin_lock_irq(&p->wait.lock);
  58. if (p->done > 0) {
  59. spin_unlock_irq(&p->wait.lock);
  60. break;
  61. }
  62. }
  63. rcu_read_unlock();
  64. }
  65. void mnt_pin_kill(struct mount *m)
  66. {
  67. while (1) {
  68. struct hlist_node *p;
  69. rcu_read_lock();
  70. p = READ_ONCE(m->mnt_pins.first);
  71. if (!p) {
  72. rcu_read_unlock();
  73. break;
  74. }
  75. pin_kill(hlist_entry(p, struct fs_pin, m_list));
  76. }
  77. }
  78. void group_pin_kill(struct hlist_head *p)
  79. {
  80. while (1) {
  81. struct hlist_node *q;
  82. rcu_read_lock();
  83. q = READ_ONCE(p->first);
  84. if (!q) {
  85. rcu_read_unlock();
  86. break;
  87. }
  88. pin_kill(hlist_entry(q, struct fs_pin, s_list));
  89. }
  90. }