]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - fs/aufs/hfsnotify.c
UBUNTU: SAUCE: Import aufs driver
[mirror_ubuntu-artful-kernel.git] / fs / aufs / hfsnotify.c
1 /*
2 * Copyright (C) 2005-2017 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_free_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, br->br_hfsn->hfsn_group);
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, hinode->hi_inode, /*mnt*/NULL,
65 /*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 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 struct fsnotify_iter_info *iter_info)
169 {
170 int err;
171 struct au_hnotify *hnotify;
172 struct inode *h_dir, *h_inode;
173 struct qstr h_child_qstr = QSTR_INIT(file_name, strlen(file_name));
174
175 AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
176
177 err = 0;
178 /* if FS_UNMOUNT happens, there must be another bug */
179 AuDebugOn(mask & FS_UNMOUNT);
180 if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
181 goto out;
182
183 h_dir = inode;
184 h_inode = NULL;
185 #ifdef AuDbgHnotify
186 au_debug_on();
187 if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
188 || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
189 AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
190 h_dir->i_ino, mask, au_hfsn_name(mask),
191 AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
192 /* WARN_ON(1); */
193 }
194 au_debug_off();
195 #endif
196
197 AuDebugOn(!inode_mark);
198 hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
199 err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
200
201 out:
202 return err;
203 }
204
205 static struct fsnotify_ops au_hfsn_ops = {
206 .handle_event = au_hfsn_handle_event,
207 .free_group_priv = au_hfsn_free_group,
208 .free_mark = au_hfsn_free_mark
209 };
210
211 /* ---------------------------------------------------------------------- */
212
213 static void au_hfsn_fin_br(struct au_branch *br)
214 {
215 struct au_br_hfsnotify *hfsn;
216
217 hfsn = br->br_hfsn;
218 if (hfsn) {
219 lockdep_off();
220 fsnotify_put_group(hfsn->hfsn_group);
221 lockdep_on();
222 }
223 }
224
225 static int au_hfsn_init_br(struct au_branch *br, int perm)
226 {
227 int err;
228 struct fsnotify_group *group;
229 struct au_br_hfsnotify *hfsn;
230
231 err = 0;
232 br->br_hfsn = NULL;
233 if (!au_br_hnotifyable(perm))
234 goto out;
235
236 err = -ENOMEM;
237 hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
238 if (unlikely(!hfsn))
239 goto out;
240
241 err = 0;
242 group = fsnotify_alloc_group(&au_hfsn_ops);
243 if (IS_ERR(group)) {
244 err = PTR_ERR(group);
245 pr_err("fsnotify_alloc_group() failed, %d\n", err);
246 goto out_hfsn;
247 }
248
249 group->private = hfsn;
250 hfsn->hfsn_group = group;
251 br->br_hfsn = hfsn;
252 goto out; /* success */
253
254 out_hfsn:
255 kfree(hfsn);
256 out:
257 return err;
258 }
259
260 static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
261 {
262 int err;
263
264 err = 0;
265 if (!br->br_hfsn)
266 err = au_hfsn_init_br(br, perm);
267
268 return err;
269 }
270
271 /* ---------------------------------------------------------------------- */
272
273 static void au_hfsn_fin(void)
274 {
275 AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
276 wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
277 }
278
279 const struct au_hnotify_op au_hnotify_op = {
280 .ctl = au_hfsn_ctl,
281 .alloc = au_hfsn_alloc,
282 .free = au_hfsn_free,
283
284 .fin = au_hfsn_fin,
285
286 .reset_br = au_hfsn_reset_br,
287 .fin_br = au_hfsn_fin_br,
288 .init_br = au_hfsn_init_br
289 };