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