2 * Copyright (C) 2005-2012 Junjiro R. Okajima
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.
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * fsnotify for the lower directories
25 /* FS_IN_IGNORED is unnecessary */
26 static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
27 | FS_CREATE | FS_EVENT_ON_CHILD);
28 static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
30 static void au_hfsn_free_mark(struct fsnotify_mark *mark)
32 struct au_hnotify *hn = container_of(mark, struct au_hnotify,
37 wake_up_all(&au_hfsn_wq);
40 static int au_hfsn_alloc(struct au_hinode *hinode)
42 struct au_hnotify *hn;
43 struct super_block *sb;
45 struct fsnotify_mark *mark;
48 hn = hinode->hi_notify;
49 sb = hn->hn_aufs_inode->i_sb;
50 bindex = au_br_index(sb, hinode->hi_id);
51 br = au_sbr(sb, bindex);
54 fsnotify_init_mark(mark, au_hfsn_free_mark);
55 mark->mask = AuHfsnMask;
57 * by udba rename or rmdir, aufs assign a new inode to the known
58 * h_inode, so specify 1 to allow dups.
60 return fsnotify_add_mark(mark, br->br_hfsn_group, hinode->hi_inode,
61 /*mnt*/NULL, /*allow_dups*/1);
64 static void au_hfsn_free(struct au_hinode *hinode)
66 struct au_hnotify *hn;
67 struct fsnotify_mark *mark;
69 hn = hinode->hi_notify;
71 fsnotify_destroy_mark(mark);
72 fsnotify_put_mark(mark);
74 /* TODO: bad approach */
75 wait_event(au_hfsn_wq, hn->hn_mark_dead);
78 /* ---------------------------------------------------------------------- */
80 static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
82 struct fsnotify_mark *mark;
84 mark = &hinode->hi_notify->hn_mark;
85 spin_lock(&mark->lock);
87 AuDebugOn(mark->mask & AuHfsnMask);
88 mark->mask |= AuHfsnMask;
90 AuDebugOn(!(mark->mask & AuHfsnMask));
91 mark->mask &= ~AuHfsnMask;
93 spin_unlock(&mark->lock);
94 /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
97 /* ---------------------------------------------------------------------- */
99 /* #define AuDbgHnotify */
101 static char *au_hfsn_name(u32 mask)
103 #ifdef CONFIG_AUFS_DEBUG
104 #define test_ret(flag) if (mask & flag) \
109 test_ret(FS_CLOSE_WRITE);
110 test_ret(FS_CLOSE_NOWRITE);
112 test_ret(FS_MOVED_FROM);
113 test_ret(FS_MOVED_TO);
116 test_ret(FS_DELETE_SELF);
117 test_ret(FS_MOVE_SELF);
118 test_ret(FS_UNMOUNT);
119 test_ret(FS_Q_OVERFLOW);
120 test_ret(FS_IN_IGNORED);
121 test_ret(FS_IN_ISDIR);
122 test_ret(FS_IN_ONESHOT);
123 test_ret(FS_EVENT_ON_CHILD);
132 /* ---------------------------------------------------------------------- */
134 static int au_hfsn_handle_event(struct fsnotify_group *group,
135 struct fsnotify_mark *inode_mark,
136 struct fsnotify_mark *vfsmount_mark,
137 struct fsnotify_event *event)
140 struct au_hnotify *hnotify;
141 struct inode *h_dir, *h_inode;
143 struct qstr h_child_qstr = {
144 .name = event->file_name,
145 .len = event->name_len
148 AuDebugOn(event->data_type != FSNOTIFY_EVENT_INODE);
151 /* if FS_UNMOUNT happens, there must be another bug */
153 AuDebugOn(mask & FS_UNMOUNT);
154 if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
157 h_dir = event->to_tell;
158 h_inode = event->inode;
161 if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
162 || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
163 AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
164 h_dir->i_ino, mask, au_hfsn_name(mask),
165 AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
171 AuDebugOn(!inode_mark);
172 hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
173 err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
179 /* isn't it waste to ask every registered 'group'? */
180 /* copied from linux/fs/notify/inotify/inotify_fsnotiry.c */
181 /* it should be exported to modules */
182 static bool au_hfsn_should_send_event(struct fsnotify_group *group,
183 struct inode *h_inode,
184 struct fsnotify_mark *inode_mark,
185 struct fsnotify_mark *vfsmount_mark,
186 __u32 mask, void *data, int data_type)
188 mask = (mask & ~FS_EVENT_ON_CHILD);
189 return inode_mark->mask & mask;
192 static struct fsnotify_ops au_hfsn_ops = {
193 .should_send_event = au_hfsn_should_send_event,
194 .handle_event = au_hfsn_handle_event
197 /* ---------------------------------------------------------------------- */
199 static void au_hfsn_fin_br(struct au_branch *br)
201 if (br->br_hfsn_group)
202 fsnotify_put_group(br->br_hfsn_group);
205 static int au_hfsn_init_br(struct au_branch *br, int perm)
207 br->br_hfsn_group = NULL;
208 br->br_hfsn_ops = au_hfsn_ops;
212 static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
217 if (udba != AuOpt_UDBA_HNOTIFY
218 || !au_br_hnotifyable(perm)) {
220 br->br_hfsn_group = NULL;
224 if (br->br_hfsn_group)
227 br->br_hfsn_group = fsnotify_alloc_group(&br->br_hfsn_ops);
228 if (IS_ERR(br->br_hfsn_group)) {
229 err = PTR_ERR(br->br_hfsn_group);
230 pr_err("fsnotify_alloc_group() failed, %d\n", err);
231 br->br_hfsn_group = NULL;
239 const struct au_hnotify_op au_hnotify_op = {
241 .alloc = au_hfsn_alloc,
242 .free = au_hfsn_free,
244 .reset_br = au_hfsn_reset_br,
245 .fin_br = au_hfsn_fin_br,
246 .init_br = au_hfsn_init_br