2 * Copyright (C) 2005-2017 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, see <http://www.gnu.org/licenses/>.
19 * fsnotify for the lower directories
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);
30 static void au_hfsn_free_mark(struct fsnotify_mark
*mark
)
32 struct au_hnotify
*hn
= container_of(mark
, struct au_hnotify
,
34 /* AuDbg("here\n"); */
35 au_cache_free_hnotify(hn
);
36 smp_mb__before_atomic(); /* for atomic64_dec */
37 if (atomic64_dec_and_test(&au_hfsn_ifree
))
41 static int au_hfsn_alloc(struct au_hinode
*hinode
)
44 struct au_hnotify
*hn
;
45 struct super_block
*sb
;
47 struct fsnotify_mark
*mark
;
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
);
57 fsnotify_init_mark(mark
, br
->br_hfsn
->hfsn_group
);
58 mark
->mask
= AuHfsnMask
;
60 * by udba rename or rmdir, aufs assign a new inode to the known
61 * h_inode, so specify 1 to allow dups.
64 err
= fsnotify_add_mark(mark
, hinode
->hi_inode
, /*mnt*/NULL
,
71 static int au_hfsn_free(struct au_hinode
*hinode
, struct au_hnotify
*hn
)
73 struct fsnotify_mark
*mark
;
74 unsigned long long ull
;
75 struct fsnotify_group
*group
;
77 ull
= atomic64_inc_return(&au_hfsn_ifree
);
81 spin_lock(&mark
->lock
);
83 fsnotify_get_group(group
);
84 spin_unlock(&mark
->lock
);
86 fsnotify_destroy_mark(mark
, group
);
87 fsnotify_put_mark(mark
);
88 fsnotify_put_group(group
);
91 /* free hn by myself */
95 /* ---------------------------------------------------------------------- */
97 static void au_hfsn_ctl(struct au_hinode
*hinode
, int do_set
)
99 struct fsnotify_mark
*mark
;
101 mark
= &hinode
->hi_notify
->hn_mark
;
102 spin_lock(&mark
->lock
);
104 AuDebugOn(mark
->mask
& AuHfsnMask
);
105 mark
->mask
|= AuHfsnMask
;
107 AuDebugOn(!(mark
->mask
& AuHfsnMask
));
108 mark
->mask
&= ~AuHfsnMask
;
110 spin_unlock(&mark
->lock
);
111 /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
114 /* ---------------------------------------------------------------------- */
116 /* #define AuDbgHnotify */
118 static char *au_hfsn_name(u32 mask
)
120 #ifdef CONFIG_AUFS_DEBUG
121 #define test_ret(flag) \
129 test_ret(FS_CLOSE_WRITE
);
130 test_ret(FS_CLOSE_NOWRITE
);
132 test_ret(FS_MOVED_FROM
);
133 test_ret(FS_MOVED_TO
);
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
);
142 test_ret(FS_IN_ONESHOT
);
143 test_ret(FS_EVENT_ON_CHILD
);
152 /* ---------------------------------------------------------------------- */
154 static void au_hfsn_free_group(struct fsnotify_group
*group
)
156 struct au_br_hfsnotify
*hfsn
= group
->private;
158 /* AuDbg("here\n"); */
162 static int au_hfsn_handle_event(struct fsnotify_group
*group
,
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
)
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
));
175 AuDebugOn(data_type
!= FSNOTIFY_EVENT_INODE
);
178 /* if FS_UNMOUNT happens, there must be another bug */
179 AuDebugOn(mask
& FS_UNMOUNT
);
180 if (mask
& (FS_IN_IGNORED
| FS_UNMOUNT
))
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);
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
);
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
211 /* ---------------------------------------------------------------------- */
213 static void au_hfsn_fin_br(struct au_branch
*br
)
215 struct au_br_hfsnotify
*hfsn
;
220 fsnotify_put_group(hfsn
->hfsn_group
);
225 static int au_hfsn_init_br(struct au_branch
*br
, int perm
)
228 struct fsnotify_group
*group
;
229 struct au_br_hfsnotify
*hfsn
;
233 if (!au_br_hnotifyable(perm
))
237 hfsn
= kmalloc(sizeof(*hfsn
), GFP_NOFS
);
242 group
= fsnotify_alloc_group(&au_hfsn_ops
);
244 err
= PTR_ERR(group
);
245 pr_err("fsnotify_alloc_group() failed, %d\n", err
);
249 group
->private = hfsn
;
250 hfsn
->hfsn_group
= group
;
252 goto out
; /* success */
260 static int au_hfsn_reset_br(unsigned int udba
, struct au_branch
*br
, int perm
)
266 err
= au_hfsn_init_br(br
, perm
);
271 /* ---------------------------------------------------------------------- */
273 static void au_hfsn_fin(void)
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
));
279 const struct au_hnotify_op au_hnotify_op
= {
281 .alloc
= au_hfsn_alloc
,
282 .free
= au_hfsn_free
,
286 .reset_br
= au_hfsn_reset_br
,
287 .fin_br
= au_hfsn_fin_br
,
288 .init_br
= au_hfsn_init_br