Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Al Viro | efb170c | 2014-08-07 08:39:04 -0400 | [diff] [blame] | 2 | #include <linux/fs.h> |
Al Viro | 59eda0e | 2015-01-10 17:53:21 -0500 | [diff] [blame] | 3 | #include <linux/sched.h> |
Al Viro | efb170c | 2014-08-07 08:39:04 -0400 | [diff] [blame] | 4 | #include <linux/slab.h> |
Al Viro | 8fa1f1c | 2014-05-21 18:22:52 -0400 | [diff] [blame] | 5 | #include "internal.h" |
Al Viro | efb170c | 2014-08-07 08:39:04 -0400 | [diff] [blame] | 6 | #include "mount.h" |
| 7 | |
Al Viro | efb170c | 2014-08-07 08:39:04 -0400 | [diff] [blame] | 8 | static DEFINE_SPINLOCK(pin_lock); |
| 9 | |
Al Viro | efb170c | 2014-08-07 08:39:04 -0400 | [diff] [blame] | 10 | void pin_remove(struct fs_pin *pin) |
| 11 | { |
| 12 | spin_lock(&pin_lock); |
Eric W. Biederman | 820f9f1 | 2015-04-02 16:35:48 -0500 | [diff] [blame] | 13 | hlist_del_init(&pin->m_list); |
| 14 | hlist_del_init(&pin->s_list); |
Al Viro | efb170c | 2014-08-07 08:39:04 -0400 | [diff] [blame] | 15 | spin_unlock(&pin_lock); |
Al Viro | 59eda0e | 2015-01-10 17:53:21 -0500 | [diff] [blame] | 16 | spin_lock_irq(&pin->wait.lock); |
| 17 | pin->done = 1; |
| 18 | wake_up_locked(&pin->wait); |
| 19 | spin_unlock_irq(&pin->wait.lock); |
Al Viro | efb170c | 2014-08-07 08:39:04 -0400 | [diff] [blame] | 20 | } |
| 21 | |
Al Viro | fdab684 | 2015-01-11 10:57:27 -0500 | [diff] [blame] | 22 | void pin_insert(struct fs_pin *pin, struct vfsmount *m) |
| 23 | { |
Al Viro | 56cbb42 | 2019-07-04 16:57:51 -0400 | [diff] [blame] | 24 | spin_lock(&pin_lock); |
| 25 | hlist_add_head(&pin->s_list, &m->mnt_sb->s_pins); |
| 26 | hlist_add_head(&pin->m_list, &real_mount(m)->mnt_pins); |
| 27 | spin_unlock(&pin_lock); |
Al Viro | fdab684 | 2015-01-11 10:57:27 -0500 | [diff] [blame] | 28 | } |
| 29 | |
Al Viro | 59eda0e | 2015-01-10 17:53:21 -0500 | [diff] [blame] | 30 | void pin_kill(struct fs_pin *p) |
| 31 | { |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 32 | wait_queue_entry_t wait; |
Al Viro | 59eda0e | 2015-01-10 17:53:21 -0500 | [diff] [blame] | 33 | |
| 34 | if (!p) { |
| 35 | rcu_read_unlock(); |
| 36 | return; |
| 37 | } |
| 38 | init_wait(&wait); |
| 39 | spin_lock_irq(&p->wait.lock); |
| 40 | if (likely(!p->done)) { |
| 41 | p->done = -1; |
| 42 | spin_unlock_irq(&p->wait.lock); |
| 43 | rcu_read_unlock(); |
| 44 | p->kill(p); |
| 45 | return; |
| 46 | } |
| 47 | if (p->done > 0) { |
| 48 | spin_unlock_irq(&p->wait.lock); |
| 49 | rcu_read_unlock(); |
| 50 | return; |
| 51 | } |
| 52 | __add_wait_queue(&p->wait, &wait); |
| 53 | while (1) { |
| 54 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 55 | spin_unlock_irq(&p->wait.lock); |
| 56 | rcu_read_unlock(); |
| 57 | schedule(); |
| 58 | rcu_read_lock(); |
Ingo Molnar | 2055da9 | 2017-06-20 12:06:46 +0200 | [diff] [blame] | 59 | if (likely(list_empty(&wait.entry))) |
Al Viro | 59eda0e | 2015-01-10 17:53:21 -0500 | [diff] [blame] | 60 | break; |
| 61 | /* OK, we know p couldn't have been freed yet */ |
| 62 | spin_lock_irq(&p->wait.lock); |
| 63 | if (p->done > 0) { |
| 64 | spin_unlock_irq(&p->wait.lock); |
| 65 | break; |
| 66 | } |
| 67 | } |
| 68 | rcu_read_unlock(); |
| 69 | } |
| 70 | |
Al Viro | 8fa1f1c | 2014-05-21 18:22:52 -0400 | [diff] [blame] | 71 | void mnt_pin_kill(struct mount *m) |
Al Viro | efb170c | 2014-08-07 08:39:04 -0400 | [diff] [blame] | 72 | { |
| 73 | while (1) { |
| 74 | struct hlist_node *p; |
Al Viro | efb170c | 2014-08-07 08:39:04 -0400 | [diff] [blame] | 75 | rcu_read_lock(); |
Mark Rutland | 6aa7de0 | 2017-10-23 14:07:29 -0700 | [diff] [blame] | 76 | p = READ_ONCE(m->mnt_pins.first); |
Al Viro | efb170c | 2014-08-07 08:39:04 -0400 | [diff] [blame] | 77 | if (!p) { |
| 78 | rcu_read_unlock(); |
| 79 | break; |
| 80 | } |
Al Viro | 59eda0e | 2015-01-10 17:53:21 -0500 | [diff] [blame] | 81 | pin_kill(hlist_entry(p, struct fs_pin, m_list)); |
Al Viro | efb170c | 2014-08-07 08:39:04 -0400 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
Al Viro | fdab684 | 2015-01-11 10:57:27 -0500 | [diff] [blame] | 85 | void group_pin_kill(struct hlist_head *p) |
Al Viro | efb170c | 2014-08-07 08:39:04 -0400 | [diff] [blame] | 86 | { |
| 87 | while (1) { |
Al Viro | fdab684 | 2015-01-11 10:57:27 -0500 | [diff] [blame] | 88 | struct hlist_node *q; |
Al Viro | efb170c | 2014-08-07 08:39:04 -0400 | [diff] [blame] | 89 | rcu_read_lock(); |
Mark Rutland | 6aa7de0 | 2017-10-23 14:07:29 -0700 | [diff] [blame] | 90 | q = READ_ONCE(p->first); |
Al Viro | fdab684 | 2015-01-11 10:57:27 -0500 | [diff] [blame] | 91 | if (!q) { |
Al Viro | efb170c | 2014-08-07 08:39:04 -0400 | [diff] [blame] | 92 | rcu_read_unlock(); |
| 93 | break; |
| 94 | } |
Al Viro | 59eda0e | 2015-01-10 17:53:21 -0500 | [diff] [blame] | 95 | pin_kill(hlist_entry(q, struct fs_pin, s_list)); |
Al Viro | efb170c | 2014-08-07 08:39:04 -0400 | [diff] [blame] | 96 | } |
| 97 | } |