]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - fs/aufs/hfsnotify.c
UBUNTU: SAUCE: Import aufs driver
[mirror_ubuntu-zesty-kernel.git] / fs / aufs / hfsnotify.c
1 /*
2 * Copyright (C) 2005-2016 Junjiro R. Okajima
3 *
4 * This program, aufs is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /*
19 * fsnotify for the lower directories
20 */
21
22 #include "aufs.h"
23
24 /* FS_IN_IGNORED is unnecessary */
25 static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
26 | FS_CREATE | FS_EVENT_ON_CHILD);
27 static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
28 static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
29
30 static void au_hfsn_free_mark(struct fsnotify_mark *mark)
31 {
32 struct au_hnotify *hn = container_of(mark, struct au_hnotify,
33 hn_mark);
34 /* AuDbg("here\n"); */
35 au_cache_dfree_hnotify(hn);
36 smp_mb__before_atomic();
37 if (atomic64_dec_and_test(&au_hfsn_ifree))
38 wake_up(&au_hfsn_wq);
39 }
40
41 static int au_hfsn_alloc(struct au_hinode *hinode)
42 {
43 int err;
44 struct au_hnotify *hn;
45 struct super_block *sb;
46 struct au_branch *br;
47 struct fsnotify_mark *mark;
48 aufs_bindex_t bindex;
49
50 hn = hinode->hi_notify;
51 sb = hn->hn_aufs_inode->i_sb;
52 bindex = au_br_index(sb, hinode->hi_id);
53 br = au_sbr(sb, bindex);
54 AuDebugOn(!br->br_hfsn);
55
56 mark = &hn->hn_mark;
57 fsnotify_init_mark(mark, au_hfsn_free_mark);
58 mark->mask = AuHfsnMask;
59 /*
60 * by udba rename or rmdir, aufs assign a new inode to the known
61 * h_inode, so specify 1 to allow dups.
62 */
63 lockdep_off();
64 err = fsnotify_add_mark(mark, br->br_hfsn->hfsn_group, hinode->hi_inode,
65 /*mnt*/NULL, /*allow_dups*/1);
66 lockdep_on();
67
68 return err;
69 }
70
71 static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
72 {
73 struct fsnotify_mark *mark;
74 unsigned long long ull;
75 struct fsnotify_group *group;
76
77 ull = atomic64_inc_return(&au_hfsn_ifree);
78 BUG_ON(!ull);
79
80 mark = &hn->hn_mark;
81 spin_lock(&mark->lock);
82 group = mark->group;
83 fsnotify_get_group(group);
84 spin_unlock(&mark->lock);
85 lockdep_off();
86 fsnotify_destroy_mark(mark, group);
87 fsnotify_put_mark(mark);
88 fsnotify_put_group(group);
89 lockdep_on();
90
91 /* free hn by myself */
92 return 0;
93 }
94
95 /* ---------------------------------------------------------------------- */
96
97 static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
98 {
99 struct fsnotify_mark *mark;
100
101 mark = &hinode->hi_notify->hn_mark;
102 spin_lock(&mark->lock);
103 if (do_set) {
104 AuDebugOn(mark->mask & AuHfsnMask);
105 mark->mask |= AuHfsnMask;
106 } else {
107 AuDebugOn(!(mark->mask & AuHfsnMask));
108 mark->mask &= ~AuHfsnMask;
109 }
110 spin_unlock(&mark->lock);
111 /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
112 }
113
114 /* ---------------------------------------------------------------------- */
115
116 /* #define AuDbgHnotify */
117 #ifdef AuDbgHnotify
118 static char *au_hfsn_name(u32 mask)
119 {
120 #ifdef CONFIG_AUFS_DEBUG
121 #define test_ret(flag) \
122 do { \
123 if (mask & flag) \
124 return #flag; \
125 } while (0)
126 test_ret(FS_ACCESS);
127 test_ret(FS_MODIFY);
128 test_ret(FS_ATTRIB);
129 test_ret(FS_CLOSE_WRITE);
130 test_ret(FS_CLOSE_NOWRITE);
131 test_ret(FS_OPEN);
132 test_ret(FS_MOVED_FROM);
133 test_ret(FS_MOVED_TO);
134 test_ret(FS_CREATE);
135 test_ret(FS_DELETE);
136 test_ret(FS_DELETE_SELF);
137 test_ret(FS_MOVE_SELF);
138 test_ret(FS_UNMOUNT);
139 test_ret(FS_Q_OVERFLOW);
140 test_ret(FS_IN_IGNORED);
141 test_ret(FS_ISDIR);
142 test_ret(FS_IN_ONESHOT);
143 test_ret(FS_EVENT_ON_CHILD);
144 return "";
145 #undef test_ret
146 #else
147 return "??";
148 #endif
149 }
150 #endif
151
152 /* ---------------------------------------------------------------------- */
153
154 static void au_hfsn_free_group(struct fsnotify_group *group)
155 {
156 struct au_br_hfsnotify *hfsn = group->private;
157
158 /* AuDbg("here\n"); */
159 au_delayed_kfree(hfsn);
160 }
161
162 static int au_hfsn_handle_event(struct fsnotify_group *group,
163 struct inode *inode,
164 struct fsnotify_mark *inode_mark,
165 struct fsnotify_mark *vfsmount_mark,
166 u32 mask, const void *data, int data_type,
167 const unsigned char *file_name, u32 cookie)
168 {
169 int err;
170 struct au_hnotify *hnotify;
171 struct inode *h_dir, *h_inode;
172 struct qstr h_child_qstr = QSTR_INIT(file_name, strlen(file_name));
173
174 AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
175
176 err = 0;
177 /* if FS_UNMOUNT happens, there must be another bug */
178 AuDebugOn(mask & FS_UNMOUNT);
179 if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
180 goto out;
181
182 h_dir = inode;
183 h_inode = NULL;
184 #ifdef AuDbgHnotify
185 au_debug_on();
186 if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
187 || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
188 AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
189 h_dir->i_ino, mask, au_hfsn_name(mask),
190 AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
191 /* WARN_ON(1); */
192 }
193 au_debug_off();
194 #endif
195
196 AuDebugOn(!inode_mark);
197 hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
198 err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
199
200 out:
201 return err;
202 }
203
204 static struct fsnotify_ops au_hfsn_ops = {
205 .handle_event = au_hfsn_handle_event,
206 .free_group_priv = au_hfsn_free_group
207 };
208
209 /* ---------------------------------------------------------------------- */
210
211 static void au_hfsn_fin_br(struct au_branch *br)
212 {
213 struct au_br_hfsnotify *hfsn;
214
215 hfsn = br->br_hfsn;
216 if (hfsn) {
217 lockdep_off();
218 fsnotify_put_group(hfsn->hfsn_group);
219 lockdep_on();
220 }
221 }
222
223 static int au_hfsn_init_br(struct au_branch *br, int perm)
224 {
225 int err;
226 struct fsnotify_group *group;
227 struct au_br_hfsnotify *hfsn;
228
229 err = 0;
230 br->br_hfsn = NULL;
231 if (!au_br_hnotifyable(perm))
232 goto out;
233
234 err = -ENOMEM;
235 hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
236 if (unlikely(!hfsn))
237 goto out;
238
239 err = 0;
240 group = fsnotify_alloc_group(&au_hfsn_ops);
241 if (IS_ERR(group)) {
242 err = PTR_ERR(group);
243 pr_err("fsnotify_alloc_group() failed, %d\n", err);
244 goto out_hfsn;
245 }
246
247 group->private = hfsn;
248 hfsn->hfsn_group = group;
249 br->br_hfsn = hfsn;
250 goto out; /* success */
251
252 out_hfsn:
253 au_delayed_kfree(hfsn);
254 out:
255 return err;
256 }
257
258 static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
259 {
260 int err;
261
262 err = 0;
263 if (!br->br_hfsn)
264 err = au_hfsn_init_br(br, perm);
265
266 return err;
267 }
268
269 /* ---------------------------------------------------------------------- */
270
271 static void au_hfsn_fin(void)
272 {
273 AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
274 wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
275 }
276
277 const struct au_hnotify_op au_hnotify_op = {
278 .ctl = au_hfsn_ctl,
279 .alloc = au_hfsn_alloc,
280 .free = au_hfsn_free,
281
282 .fin = au_hfsn_fin,
283
284 .reset_br = au_hfsn_reset_br,
285 .fin_br = au_hfsn_fin_br,
286 .init_br = au_hfsn_init_br
287 };