2
* Copyright (C) 2005-2009 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
* inotify for the lower directories
25
static const __u32 AuHinMask = (IN_MOVE | IN_DELETE | IN_CREATE);
26
static struct inotify_handle *au_hin_handle;
28
AuCacheFuncs(hinotify, HINOTIFY);
30
int au_hin_alloc(struct au_hinode *hinode, struct inode *inode,
31
struct inode *h_inode)
34
struct au_hinotify *hin;
38
hin = au_cache_alloc_hinotify();
40
AuDebugOn(hinode->hi_notify);
41
hinode->hi_notify = hin;
42
hin->hin_aufs_inode = inode;
44
inotify_init_watch(&hin->hin_watch);
45
wd = inotify_add_watch(au_hin_handle, &hin->hin_watch, h_inode,
48
return 0; /* success */
51
put_inotify_watch(&hin->hin_watch);
52
au_cache_free_hinotify(hin);
53
hinode->hi_notify = NULL;
59
void au_hin_free(struct au_hinode *hinode)
62
struct au_hinotify *hin;
64
hin = hinode->hi_notify;
67
if (atomic_read(&hin->hin_watch.count))
68
err = inotify_rm_watch(au_hin_handle, &hin->hin_watch);
70
/* it means the watch is already removed */
71
AuWarn("failed inotify_rm_watch() %d\n", err);
72
au_cache_free_hinotify(hin);
73
hinode->hi_notify = NULL;
77
/* ---------------------------------------------------------------------- */
79
void au_hin_ctl(struct au_hinode *hinode, int do_set)
81
struct inode *h_inode;
82
struct inotify_watch *watch;
84
if (!hinode->hi_notify)
87
h_inode = hinode->hi_inode;
90
/* todo: try inotify_find_update_watch()? */
91
watch = &hinode->hi_notify->hin_watch;
92
mutex_lock(&h_inode->inotify_mutex);
93
/* mutex_lock(&watch->ih->mutex); */
95
AuDebugOn(watch->mask & AuHinMask);
96
watch->mask |= AuHinMask;
98
AuDebugOn(!(watch->mask & AuHinMask));
99
watch->mask &= ~AuHinMask;
101
/* mutex_unlock(&watch->ih->mutex); */
102
mutex_unlock(&h_inode->inotify_mutex);
105
void au_reset_hinotify(struct inode *inode, unsigned int flags)
107
aufs_bindex_t bindex, bend;
109
struct dentry *iwhdentry;
111
bend = au_ibend(inode);
112
for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
113
hi = au_h_iptr(inode, bindex);
117
/* mutex_lock_nested(&hi->i_mutex, AuLsc_I_CHILD); */
118
iwhdentry = au_hi_wh(inode, bindex);
122
au_set_h_iptr(inode, bindex, NULL, 0);
123
au_set_h_iptr(inode, bindex, au_igrab(hi),
127
/* mutex_unlock(&hi->i_mutex); */
131
/* ---------------------------------------------------------------------- */
133
static int hin_xino(struct inode *inode, struct inode *h_inode)
136
aufs_bindex_t bindex, bend, bfound, bstart;
140
if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
141
AuWarn("branch root dir was changed\n");
146
bend = au_ibend(inode);
147
bstart = au_ibstart(inode);
148
#if 0 /* reserved for future use */
149
if (bindex == bend) {
150
/* keep this ino in rename case */
154
for (bindex = bstart; bindex <= bend; bindex++) {
155
if (au_h_iptr(inode, bindex) == h_inode) {
163
for (bindex = bstart; bindex <= bend; bindex++) {
164
h_i = au_h_iptr(inode, bindex);
168
err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
169
/* ignore this error */
173
/* children inode number will be broken */
180
static int hin_gen_tree(struct dentry *dentry)
182
int err, i, j, ndentry;
183
struct au_dcsub_pages dpages;
184
struct au_dpage *dpage;
185
struct dentry **dentries;
187
err = au_dpages_init(&dpages, GFP_NOFS);
190
err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
194
for (i = 0; i < dpages.ndpage; i++) {
195
dpage = dpages.dpages + i;
196
dentries = dpage->dentries;
197
ndentry = dpage->ndentry;
198
for (j = 0; j < ndentry; j++) {
208
/* todo: reset children xino?
209
cached children only? */
210
au_iigen_dec(d->d_inode);
215
au_dpages_free(&dpages);
217
/* discard children */
218
dentry_unhash(dentry);
225
* return 0 if processed.
227
static int hin_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
228
const unsigned int isdir)
235
if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
236
AuWarn("branch root dir was changed\n");
244
spin_lock(&dcache_lock);
245
list_for_each_entry(d, &inode->i_dentry, d_alias) {
247
if (dname->len != nlen
248
&& memcmp(dname->name, name, nlen))
251
spin_lock(&d->d_lock);
254
spin_unlock(&d->d_lock);
257
spin_unlock(&dcache_lock);
259
au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIRS);
260
d = d_find_alias(inode);
267
if (dname->len == nlen && !memcmp(dname->name, name, nlen))
268
err = hin_gen_tree(d);
277
static int hin_gen_by_name(struct dentry *dentry, const unsigned int isdir)
282
inode = dentry->d_inode;
284
/* || (inode && inode->i_ino == AUFS_ROOT_INO) */
286
AuWarn("branch root dir was changed\n");
293
au_digen_dec(dentry);
297
au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIRS);
299
err = hin_gen_tree(dentry);
306
/* ---------------------------------------------------------------------- */
308
/* hinotify job flags */
309
#define AuHinJob_XINO0 1
310
#define AuHinJob_GEN (1 << 1)
311
#define AuHinJob_DIRENT (1 << 2)
312
#define AuHinJob_ISDIR (1 << 3)
313
#define AuHinJob_TRYXINO0 (1 << 4)
314
#define AuHinJob_MNTPNT (1 << 5)
315
#define au_ftest_hinjob(flags, name) ((flags) & AuHinJob_##name)
316
#define au_fset_hinjob(flags, name) { (flags) |= AuHinJob_##name; }
317
#define au_fclr_hinjob(flags, name) { (flags) &= ~AuHinJob_##name; }
319
struct hin_job_args {
321
struct inode *inode, *h_inode, *dir, *h_dir;
322
struct dentry *dentry;
327
static int hin_job(struct hin_job_args *a)
329
const unsigned int isdir = au_ftest_hinjob(a->flags, ISDIR);
332
if (au_ftest_hinjob(a->flags, XINO0) && a->inode)
333
hin_xino(a->inode, a->h_inode); /* ignore this error */
335
if (au_ftest_hinjob(a->flags, TRYXINO0)
338
mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
339
if (!a->h_inode->i_nlink)
340
hin_xino(a->inode, a->h_inode); /* ignore this error */
341
mutex_unlock(&a->h_inode->i_mutex);
344
/* make the generation obsolete */
345
if (au_ftest_hinjob(a->flags, GEN)) {
348
err = hin_gen_by_inode(a->h_name, a->h_nlen, a->inode,
350
if (err && a->dentry)
351
hin_gen_by_name(a->dentry, isdir);
352
/* ignore this error */
355
/* make dir entries obsolete */
356
if (au_ftest_hinjob(a->flags, DIRENT) && a->inode) {
357
struct au_vdir *vdir;
359
vdir = au_ivdir(a->inode);
362
/* IMustLock(a->inode); */
363
/* a->inode->i_version++; */
366
/* can do nothing but warn */
367
if (au_ftest_hinjob(a->flags, MNTPNT)
369
&& d_mountpoint(a->dentry))
370
AuWarn("mount-point %.*s is removed or renamed\n",
371
AuDLNPair(a->dentry));
376
/* ---------------------------------------------------------------------- */
378
static char *in_name(u32 mask)
380
#ifdef CONFIG_AUFS_DEBUG
381
#define test_ret(flag) if (mask & flag) \
386
test_ret(IN_CLOSE_WRITE);
387
test_ret(IN_CLOSE_NOWRITE);
389
test_ret(IN_MOVED_FROM);
390
test_ret(IN_MOVED_TO);
393
test_ret(IN_DELETE_SELF);
394
test_ret(IN_MOVE_SELF);
395
test_ret(IN_UNMOUNT);
396
test_ret(IN_Q_OVERFLOW);
397
test_ret(IN_IGNORED);
405
static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
408
struct dentry *dentry, *d, *parent;
411
parent = d_find_alias(dir);
416
spin_lock(&dcache_lock);
417
list_for_each_entry(d, &parent->d_subdirs, d_u.d_child) {
418
/* AuDbg("%.*s\n", AuDLNPair(d)); */
420
if (dname->len != nlen || memcmp(dname->name, name, nlen))
422
if (!atomic_read(&d->d_count) || !d->d_fsdata) {
423
spin_lock(&d->d_lock);
425
spin_unlock(&d->d_lock);
432
spin_unlock(&dcache_lock);
436
di_write_lock_child(dentry);
441
static struct inode *lookup_wlock_by_ino(struct super_block *sb,
442
aufs_bindex_t bindex, ino_t h_ino)
449
err = au_xino_read(sb, bindex, h_ino, &ino);
451
inode = ilookup(sb, ino);
455
if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
456
AuWarn("wrong root branch\n");
462
ii_write_lock_child(inode);
468
enum { CHILD, PARENT };
469
struct postproc_args {
470
struct inode *h_dir, *dir, *h_child_inode;
472
unsigned int flags[2];
473
unsigned int h_child_nlen;
477
static void postproc(void *_args)
479
struct postproc_args *a = _args;
480
struct super_block *sb;
481
aufs_bindex_t bindex, bend, bfound;
482
unsigned char xino, try_iput;
486
struct hin_job_args args;
487
struct dentry *dentry;
488
struct au_sbinfo *sbinfo;
491
AuDebugOn(!a->h_dir);
494
AuDbg("mask 0x%x %s, i%lu, hi%lu, hci%lu\n",
495
a->mask, in_name(a->mask), a->dir->i_ino, a->h_dir->i_ino,
496
a->h_child_inode ? a->h_child_inode->i_ino : 0);
501
* do not lock a->dir->i_mutex here
502
* because of d_revalidate() may cause a deadlock.
509
si_noflush_write_lock(sb);
511
ii_read_lock_parent(a->dir);
513
bend = au_ibend(a->dir);
514
for (bindex = au_ibstart(a->dir); bindex <= bend; bindex++)
515
if (au_h_iptr(a->dir, bindex) == a->h_dir) {
519
ii_read_unlock(a->dir);
520
if (unlikely(bfound < 0))
523
xino = !!au_opt_test(au_mntflags(sb), XINO);
525
if (a->h_child_inode)
526
h_ino = a->h_child_inode->i_ino;
529
&& (au_ftest_hinjob(a->flags[CHILD], GEN)
530
|| au_ftest_hinjob(a->flags[CHILD], MNTPNT)))
531
dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
535
inode = dentry->d_inode;
536
if (xino && !inode && h_ino
537
&& (au_ftest_hinjob(a->flags[CHILD], XINO0)
538
|| au_ftest_hinjob(a->flags[CHILD], TRYXINO0)
539
|| au_ftest_hinjob(a->flags[CHILD], GEN))) {
540
inode = lookup_wlock_by_ino(sb, bfound, h_ino);
544
args.flags = a->flags[CHILD];
545
args.dentry = dentry;
547
args.h_inode = a->h_child_inode;
549
args.h_dir = a->h_dir;
550
args.h_name = a->h_child_name;
551
args.h_nlen = a->h_child_nlen;
552
err = hin_job(&args);
554
if (dentry->d_fsdata)
555
di_write_unlock(dentry);
558
if (inode && try_iput) {
559
ii_write_unlock(inode);
563
ii_write_lock_parent(a->dir);
564
args.flags = a->flags[PARENT];
567
args.h_inode = a->h_dir;
572
err = hin_job(&args);
573
ii_write_unlock(a->dir);
576
au_nwt_done(&sbinfo->si_nowait);
579
iput(a->h_child_inode);
585
/* ---------------------------------------------------------------------- */
587
static void aufs_inotify(struct inotify_watch *watch, u32 wd __maybe_unused,
588
u32 mask, u32 cookie __maybe_unused,
589
const char *h_child_name, struct inode *h_child_inode)
591
struct au_hinotify *hinotify;
592
struct postproc_args *args;
594
unsigned char isdir, isroot, wh;
597
unsigned int flags[2];
599
/* if IN_UNMOUNT happens, there must be another bug */
600
AuDebugOn(mask & IN_UNMOUNT);
601
if (mask & (IN_IGNORED | IN_UNMOUNT)) {
602
put_inotify_watch(watch);
607
if (1 || !h_child_name || strcmp(h_child_name, AUFS_XINO_FNAME)) {
608
AuDbg("i%lu, wd %d, mask 0x%x %s, cookie 0x%x, hcname %s,"
610
watch->inode->i_ino, wd, mask, in_name(mask), cookie,
611
h_child_name ? h_child_name : "",
612
h_child_inode ? h_child_inode->i_ino : 0);
618
hinotify = container_of(watch, struct au_hinotify, hin_watch);
619
AuDebugOn(!hinotify || !hinotify->hin_aufs_inode);
620
dir = igrab(hinotify->hin_aufs_inode);
624
isroot = (dir->i_ino == AUFS_ROOT_INO);
628
len = strlen(h_child_name);
629
if (!memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
630
h_child_name += AUFS_WH_PFX_LEN;
631
len -= AUFS_WH_PFX_LEN;
638
isdir = !!S_ISDIR(h_child_inode->i_mode);
639
flags[PARENT] = AuHinJob_ISDIR;
642
flags[CHILD] = AuHinJob_ISDIR;
643
switch (mask & IN_ALL_EVENTS) {
646
AuDebugOn(!h_child_name || !h_child_inode);
647
au_fset_hinjob(flags[CHILD], GEN);
648
au_fset_hinjob(flags[CHILD], XINO0);
649
au_fset_hinjob(flags[CHILD], MNTPNT);
650
au_fset_hinjob(flags[PARENT], DIRENT);
654
AuDebugOn(!h_child_name || !h_child_inode);
655
au_fset_hinjob(flags[PARENT], DIRENT);
656
au_fset_hinjob(flags[CHILD], GEN);
661
* aufs never be able to get this child inode.
662
* revalidation should be in d_revalidate()
663
* by checking i_nlink, i_generation or d_unhashed().
665
AuDebugOn(!h_child_name);
666
au_fset_hinjob(flags[PARENT], DIRENT);
667
au_fset_hinjob(flags[CHILD], GEN);
668
au_fset_hinjob(flags[CHILD], TRYXINO0);
669
au_fset_hinjob(flags[CHILD], MNTPNT);
677
h_child_inode = NULL;
679
/* iput() and kfree() will be called in postproc() */
681
* inotify_mutex is already acquired and kmalloc/prune_icache may lock
682
* iprune_mutex. strange.
685
args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
687
if (unlikely(!args)) {
688
AuErr1("no memory\n");
692
args->flags[PARENT] = flags[PARENT];
693
args->flags[CHILD] = flags[CHILD];
696
args->h_dir = igrab(watch->inode);
698
h_child_inode = igrab(h_child_inode); /* can be NULL */
699
args->h_child_inode = h_child_inode;
700
args->h_child_nlen = len;
704
memcpy(p, h_child_name, len + 1);
708
wkq_err = au_wkq_nowait(postproc, args, dir->i_sb);
710
if (unlikely(wkq_err))
711
AuErr("wkq %d\n", wkq_err);
714
static void aufs_inotify_destroy(struct inotify_watch *watch __maybe_unused)
719
static struct inotify_operations aufs_inotify_ops = {
720
.handle_event = aufs_inotify,
721
.destroy_watch = aufs_inotify_destroy
724
/* ---------------------------------------------------------------------- */
726
static void au_hin_destroy_cache(void)
728
kmem_cache_destroy(au_cachep[AuCache_HINOTIFY]);
729
au_cachep[AuCache_HINOTIFY] = NULL;
732
int __init au_hinotify_init(void)
737
au_cachep[AuCache_HINOTIFY] = AuCache(au_hinotify);
738
if (au_cachep[AuCache_HINOTIFY]) {
740
au_hin_handle = inotify_init(&aufs_inotify_ops);
741
if (IS_ERR(au_hin_handle)) {
742
err = PTR_ERR(au_hin_handle);
743
au_hin_destroy_cache();
750
void au_hinotify_fin(void)
752
inotify_destroy(au_hin_handle);
753
if (au_cachep[AuCache_HINOTIFY])
754
au_hin_destroy_cache();