blob: 47ef3c71ce907789994f73e49b2d6b072022463e [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Al Viroefb170c2014-08-07 08:39:04 -04002#include <linux/fs.h>
Al Viro59eda0e2015-01-10 17:53:21 -05003#include <linux/sched.h>
Al Viroefb170c2014-08-07 08:39:04 -04004#include <linux/slab.h>
Al Viro8fa1f1c2014-05-21 18:22:52 -04005#include "internal.h"
Al Viroefb170c2014-08-07 08:39:04 -04006#include "mount.h"
7
Al Viroefb170c2014-08-07 08:39:04 -04008static DEFINE_SPINLOCK(pin_lock);
9
Al Viroefb170c2014-08-07 08:39:04 -040010void pin_remove(struct fs_pin *pin)
11{
12 spin_lock(&pin_lock);
Eric W. Biederman820f9f12015-04-02 16:35:48 -050013 hlist_del_init(&pin->m_list);
14 hlist_del_init(&pin->s_list);
Al Viroefb170c2014-08-07 08:39:04 -040015 spin_unlock(&pin_lock);
Al Viro59eda0e2015-01-10 17:53:21 -050016 spin_lock_irq(&pin->wait.lock);
17 pin->done = 1;
18 wake_up_locked(&pin->wait);
19 spin_unlock_irq(&pin->wait.lock);
Al Viroefb170c2014-08-07 08:39:04 -040020}
21
Al Virofdab6842015-01-11 10:57:27 -050022void pin_insert(struct fs_pin *pin, struct vfsmount *m)
23{
Al Viro56cbb422019-07-04 16:57:51 -040024 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 Virofdab6842015-01-11 10:57:27 -050028}
29
Al Viro59eda0e2015-01-10 17:53:21 -050030void pin_kill(struct fs_pin *p)
31{
Ingo Molnarac6424b2017-06-20 12:06:13 +020032 wait_queue_entry_t wait;
Al Viro59eda0e2015-01-10 17:53:21 -050033
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 Molnar2055da92017-06-20 12:06:46 +020059 if (likely(list_empty(&wait.entry)))
Al Viro59eda0e2015-01-10 17:53:21 -050060 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 Viro8fa1f1c2014-05-21 18:22:52 -040071void mnt_pin_kill(struct mount *m)
Al Viroefb170c2014-08-07 08:39:04 -040072{
73 while (1) {
74 struct hlist_node *p;
Al Viroefb170c2014-08-07 08:39:04 -040075 rcu_read_lock();
Mark Rutland6aa7de02017-10-23 14:07:29 -070076 p = READ_ONCE(m->mnt_pins.first);
Al Viroefb170c2014-08-07 08:39:04 -040077 if (!p) {
78 rcu_read_unlock();
79 break;
80 }
Al Viro59eda0e2015-01-10 17:53:21 -050081 pin_kill(hlist_entry(p, struct fs_pin, m_list));
Al Viroefb170c2014-08-07 08:39:04 -040082 }
83}
84
Al Virofdab6842015-01-11 10:57:27 -050085void group_pin_kill(struct hlist_head *p)
Al Viroefb170c2014-08-07 08:39:04 -040086{
87 while (1) {
Al Virofdab6842015-01-11 10:57:27 -050088 struct hlist_node *q;
Al Viroefb170c2014-08-07 08:39:04 -040089 rcu_read_lock();
Mark Rutland6aa7de02017-10-23 14:07:29 -070090 q = READ_ONCE(p->first);
Al Virofdab6842015-01-11 10:57:27 -050091 if (!q) {
Al Viroefb170c2014-08-07 08:39:04 -040092 rcu_read_unlock();
93 break;
94 }
Al Viro59eda0e2015-01-10 17:53:21 -050095 pin_kill(hlist_entry(q, struct fs_pin, s_list));
Al Viroefb170c2014-08-07 08:39:04 -040096 }
97}