blob: a6497cf8ae53aa2bc7dcc2996887fcb92f1e9820 [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_group(struct fs_pin *pin, struct vfsmount *m, struct hlist_head *p)
Al Viroefb170c2014-08-07 08:39:04 -040023{
24 spin_lock(&pin_lock);
Al Virofdab6842015-01-11 10:57:27 -050025 if (p)
26 hlist_add_head(&pin->s_list, p);
Al Viroefb170c2014-08-07 08:39:04 -040027 hlist_add_head(&pin->m_list, &real_mount(m)->mnt_pins);
28 spin_unlock(&pin_lock);
29}
30
Al Virofdab6842015-01-11 10:57:27 -050031void pin_insert(struct fs_pin *pin, struct vfsmount *m)
32{
33 pin_insert_group(pin, m, &m->mnt_sb->s_pins);
34}
35
Al Viro59eda0e2015-01-10 17:53:21 -050036void pin_kill(struct fs_pin *p)
37{
Ingo Molnarac6424b2017-06-20 12:06:13 +020038 wait_queue_entry_t wait;
Al Viro59eda0e2015-01-10 17:53:21 -050039
40 if (!p) {
41 rcu_read_unlock();
42 return;
43 }
44 init_wait(&wait);
45 spin_lock_irq(&p->wait.lock);
46 if (likely(!p->done)) {
47 p->done = -1;
48 spin_unlock_irq(&p->wait.lock);
49 rcu_read_unlock();
50 p->kill(p);
51 return;
52 }
53 if (p->done > 0) {
54 spin_unlock_irq(&p->wait.lock);
55 rcu_read_unlock();
56 return;
57 }
58 __add_wait_queue(&p->wait, &wait);
59 while (1) {
60 set_current_state(TASK_UNINTERRUPTIBLE);
61 spin_unlock_irq(&p->wait.lock);
62 rcu_read_unlock();
63 schedule();
64 rcu_read_lock();
Ingo Molnar2055da92017-06-20 12:06:46 +020065 if (likely(list_empty(&wait.entry)))
Al Viro59eda0e2015-01-10 17:53:21 -050066 break;
67 /* OK, we know p couldn't have been freed yet */
68 spin_lock_irq(&p->wait.lock);
69 if (p->done > 0) {
70 spin_unlock_irq(&p->wait.lock);
71 break;
72 }
73 }
74 rcu_read_unlock();
75}
76
Al Viro8fa1f1c2014-05-21 18:22:52 -040077void mnt_pin_kill(struct mount *m)
Al Viroefb170c2014-08-07 08:39:04 -040078{
79 while (1) {
80 struct hlist_node *p;
Al Viroefb170c2014-08-07 08:39:04 -040081 rcu_read_lock();
Mark Rutland6aa7de02017-10-23 14:07:29 -070082 p = READ_ONCE(m->mnt_pins.first);
Al Viroefb170c2014-08-07 08:39:04 -040083 if (!p) {
84 rcu_read_unlock();
85 break;
86 }
Al Viro59eda0e2015-01-10 17:53:21 -050087 pin_kill(hlist_entry(p, struct fs_pin, m_list));
Al Viroefb170c2014-08-07 08:39:04 -040088 }
89}
90
Al Virofdab6842015-01-11 10:57:27 -050091void group_pin_kill(struct hlist_head *p)
Al Viroefb170c2014-08-07 08:39:04 -040092{
93 while (1) {
Al Virofdab6842015-01-11 10:57:27 -050094 struct hlist_node *q;
Al Viroefb170c2014-08-07 08:39:04 -040095 rcu_read_lock();
Mark Rutland6aa7de02017-10-23 14:07:29 -070096 q = READ_ONCE(p->first);
Al Virofdab6842015-01-11 10:57:27 -050097 if (!q) {
Al Viroefb170c2014-08-07 08:39:04 -040098 rcu_read_unlock();
99 break;
100 }
Al Viro59eda0e2015-01-10 17:53:21 -0500101 pin_kill(hlist_entry(q, struct fs_pin, s_list));
Al Viroefb170c2014-08-07 08:39:04 -0400102 }
103}