2
* Copyright (C) 2005-2010 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
* abstraction to notify the direct changes on lower directories
25
int au_hn_alloc(struct au_hinode *hinode, struct inode *inode,
26
struct inode *h_inode)
29
struct au_hnotify *hn;
32
hn = au_cache_alloc_hnotify();
34
hn->hn_aufs_inode = inode;
35
err = au_hnotify_op.alloc(hn, h_inode);
37
hinode->hi_notify = hn;
39
au_cache_free_hnotify(hn);
41
* The upper dir was removed by udba, but the same named
42
* dir left. In this case, aufs assignes a new inode
43
* number and set the monitor again.
44
* For the lower dir, the old monitnor is still left.
54
void au_hn_free(struct au_hinode *hinode)
56
struct au_hnotify *hn;
58
hn = hinode->hi_notify;
60
au_hnotify_op.free(hn);
61
au_cache_free_hnotify(hn);
62
hinode->hi_notify = NULL;
66
/* ---------------------------------------------------------------------- */
68
void au_hn_ctl(struct au_hinode *hinode, int do_set)
70
if (hinode->hi_notify)
71
au_hnotify_op.ctl(hinode, do_set);
74
void au_hn_reset(struct inode *inode, unsigned int flags)
76
aufs_bindex_t bindex, bend;
78
struct dentry *iwhdentry;
80
bend = au_ibend(inode);
81
for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
82
hi = au_h_iptr(inode, bindex);
86
/* mutex_lock_nested(&hi->i_mutex, AuLsc_I_CHILD); */
87
iwhdentry = au_hi_wh(inode, bindex);
91
au_set_h_iptr(inode, bindex, NULL, 0);
92
au_set_h_iptr(inode, bindex, au_igrab(hi),
96
/* mutex_unlock(&hi->i_mutex); */
100
/* ---------------------------------------------------------------------- */
102
static int hn_xino(struct inode *inode, struct inode *h_inode)
105
aufs_bindex_t bindex, bend, bfound, bstart;
109
if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
110
pr_warning("branch root dir was changed\n");
115
bend = au_ibend(inode);
116
bstart = au_ibstart(inode);
117
#if 0 /* reserved for future use */
118
if (bindex == bend) {
119
/* keep this ino in rename case */
123
for (bindex = bstart; bindex <= bend; bindex++)
124
if (au_h_iptr(inode, bindex) == h_inode) {
131
for (bindex = bstart; bindex <= bend; bindex++) {
132
h_i = au_h_iptr(inode, bindex);
136
err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
137
/* ignore this error */
141
/* children inode number will be broken */
148
static int hn_gen_tree(struct dentry *dentry)
150
int err, i, j, ndentry;
151
struct au_dcsub_pages dpages;
152
struct au_dpage *dpage;
153
struct dentry **dentries;
155
err = au_dpages_init(&dpages, GFP_NOFS);
158
err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
162
for (i = 0; i < dpages.ndpage; i++) {
163
dpage = dpages.dpages + i;
164
dentries = dpage->dentries;
165
ndentry = dpage->ndentry;
166
for (j = 0; j < ndentry; j++) {
176
/* todo: reset children xino?
177
cached children only? */
178
au_iigen_dec(d->d_inode);
183
au_dpages_free(&dpages);
185
/* discard children */
186
dentry_unhash(dentry);
193
* return 0 if processed.
195
static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
196
const unsigned int isdir)
203
if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
204
pr_warning("branch root dir was changed\n");
212
spin_lock(&dcache_lock);
213
list_for_each_entry(d, &inode->i_dentry, d_alias) {
215
if (dname->len != nlen
216
&& memcmp(dname->name, name, nlen))
219
spin_lock(&d->d_lock);
222
spin_unlock(&d->d_lock);
225
spin_unlock(&dcache_lock);
227
au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIRS);
228
d = d_find_alias(inode);
235
if (dname->len == nlen && !memcmp(dname->name, name, nlen))
236
err = hn_gen_tree(d);
245
static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
250
inode = dentry->d_inode;
252
/* || (inode && inode->i_ino == AUFS_ROOT_INO) */
254
pr_warning("branch root dir was changed\n");
261
au_digen_dec(dentry);
265
au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIRS);
267
err = hn_gen_tree(dentry);
274
/* ---------------------------------------------------------------------- */
276
/* hnotify job flags */
277
#define AuHnJob_XINO0 1
278
#define AuHnJob_GEN (1 << 1)
279
#define AuHnJob_DIRENT (1 << 2)
280
#define AuHnJob_ISDIR (1 << 3)
281
#define AuHnJob_TRYXINO0 (1 << 4)
282
#define AuHnJob_MNTPNT (1 << 5)
283
#define au_ftest_hnjob(flags, name) ((flags) & AuHnJob_##name)
284
#define au_fset_hnjob(flags, name) { (flags) |= AuHnJob_##name; }
285
#define au_fclr_hnjob(flags, name) { (flags) &= ~AuHnJob_##name; }
293
struct au_hnotify_args {
294
struct inode *h_dir, *dir, *h_child_inode;
296
unsigned int flags[AuHnLast];
297
unsigned int h_child_nlen;
303
struct inode *inode, *h_inode, *dir, *h_dir;
304
struct dentry *dentry;
309
static int hn_job(struct hn_job_args *a)
311
const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
314
if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
315
hn_xino(a->inode, a->h_inode); /* ignore this error */
317
if (au_ftest_hnjob(a->flags, TRYXINO0)
320
mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
321
if (!a->h_inode->i_nlink)
322
hn_xino(a->inode, a->h_inode); /* ignore this error */
323
mutex_unlock(&a->h_inode->i_mutex);
326
/* make the generation obsolete */
327
if (au_ftest_hnjob(a->flags, GEN)) {
330
err = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
332
if (err && a->dentry)
333
hn_gen_by_name(a->dentry, isdir);
334
/* ignore this error */
337
/* make dir entries obsolete */
338
if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
339
struct au_vdir *vdir;
341
vdir = au_ivdir(a->inode);
344
/* IMustLock(a->inode); */
345
/* a->inode->i_version++; */
348
/* can do nothing but warn */
349
if (au_ftest_hnjob(a->flags, MNTPNT)
351
&& d_mountpoint(a->dentry))
352
pr_warning("mount-point %.*s is removed or renamed\n",
353
AuDLNPair(a->dentry));
358
/* ---------------------------------------------------------------------- */
360
static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
363
struct dentry *dentry, *d, *parent;
366
parent = d_find_alias(dir);
371
spin_lock(&dcache_lock);
372
list_for_each_entry(d, &parent->d_subdirs, d_u.d_child) {
373
/* AuDbg("%.*s\n", AuDLNPair(d)); */
375
if (dname->len != nlen || memcmp(dname->name, name, nlen))
377
if (!atomic_read(&d->d_count) || !d->d_fsdata) {
378
spin_lock(&d->d_lock);
380
spin_unlock(&d->d_lock);
387
spin_unlock(&dcache_lock);
391
di_write_lock_child(dentry);
396
static struct inode *lookup_wlock_by_ino(struct super_block *sb,
397
aufs_bindex_t bindex, ino_t h_ino)
404
err = au_xino_read(sb, bindex, h_ino, &ino);
406
inode = ilookup(sb, ino);
410
if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
411
pr_warning("wrong root branch\n");
417
ii_write_lock_child(inode);
423
static void au_hn_bh(void *_args)
425
struct au_hnotify_args *a = _args;
426
struct super_block *sb;
427
aufs_bindex_t bindex, bend, bfound;
428
unsigned char xino, try_iput;
432
struct hn_job_args args;
433
struct dentry *dentry;
434
struct au_sbinfo *sbinfo;
437
AuDebugOn(!a->h_dir);
440
AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
441
a->mask, a->dir->i_ino, a->h_dir->i_ino,
442
a->h_child_inode ? a->h_child_inode->i_ino : 0);
447
* do not lock a->dir->i_mutex here
448
* because of d_revalidate() may cause a deadlock.
455
si_noflush_write_lock(sb);
457
ii_read_lock_parent(a->dir);
459
bend = au_ibend(a->dir);
460
for (bindex = au_ibstart(a->dir); bindex <= bend; bindex++)
461
if (au_h_iptr(a->dir, bindex) == a->h_dir) {
465
ii_read_unlock(a->dir);
466
if (unlikely(bfound < 0))
469
xino = !!au_opt_test(au_mntflags(sb), XINO);
471
if (a->h_child_inode)
472
h_ino = a->h_child_inode->i_ino;
475
&& (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
476
|| au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
477
dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
481
inode = dentry->d_inode;
482
if (xino && !inode && h_ino
483
&& (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
484
|| au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
485
|| au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
486
inode = lookup_wlock_by_ino(sb, bfound, h_ino);
490
args.flags = a->flags[AuHn_CHILD];
491
args.dentry = dentry;
493
args.h_inode = a->h_child_inode;
495
args.h_dir = a->h_dir;
496
args.h_name = a->h_child_name;
497
args.h_nlen = a->h_child_nlen;
500
if (dentry->d_fsdata)
501
di_write_unlock(dentry);
504
if (inode && try_iput) {
505
ii_write_unlock(inode);
509
ii_write_lock_parent(a->dir);
510
args.flags = a->flags[AuHn_PARENT];
513
args.h_inode = a->h_dir;
519
ii_write_unlock(a->dir);
522
au_nwt_done(&sbinfo->si_nowait);
525
iput(a->h_child_inode);
531
/* ---------------------------------------------------------------------- */
533
int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
534
struct qstr *h_child_qstr, struct inode *h_child_inode)
537
unsigned int flags[AuHnLast];
538
unsigned char isdir, isroot, wh;
540
struct au_hnotify_args *args;
541
char *p, *h_child_name;
544
AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
545
dir = igrab(hnotify->hn_aufs_inode);
549
isroot = (dir->i_ino == AUFS_ROOT_INO);
551
h_child_name = (void *)h_child_qstr->name;
552
len = h_child_qstr->len;
554
if (len > AUFS_WH_PFX_LEN
555
&& !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
556
h_child_name += AUFS_WH_PFX_LEN;
557
len -= AUFS_WH_PFX_LEN;
564
isdir = !!S_ISDIR(h_child_inode->i_mode);
565
flags[AuHn_PARENT] = AuHnJob_ISDIR;
566
flags[AuHn_CHILD] = 0;
568
flags[AuHn_CHILD] = AuHnJob_ISDIR;
569
au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
570
au_fset_hnjob(flags[AuHn_CHILD], GEN);
571
switch (mask & FS_EVENTS_POSS_ON_CHILD) {
574
au_fset_hnjob(flags[AuHn_CHILD], XINO0);
575
au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
578
AuDebugOn(!h_child_name || !h_child_inode);
583
* aufs never be able to get this child inode.
584
* revalidation should be in d_revalidate()
585
* by checking i_nlink, i_generation or d_unhashed().
587
AuDebugOn(!h_child_name);
588
au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
589
au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
597
h_child_inode = NULL;
600
/* iput() and kfree() will be called in au_hnotify() */
602
* inotify_mutex is already acquired and kmalloc/prune_icache may lock
603
* iprune_mutex. strange.
606
args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
608
if (unlikely(!args)) {
609
AuErr1("no memory\n");
613
args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
614
args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
617
args->h_dir = igrab(h_dir);
619
h_child_inode = igrab(h_child_inode); /* can be NULL */
620
args->h_child_inode = h_child_inode;
621
args->h_child_nlen = len;
625
memcpy(p, h_child_name, len);
630
err = au_wkq_nowait(au_hn_bh, args, dir->i_sb);
633
pr_err("wkq %d\n", err);
634
iput(args->h_child_inode);
644
static void au_hn_destroy_cache(void)
646
kmem_cache_destroy(au_cachep[AuCache_HNOTIFY]);
647
au_cachep[AuCache_HNOTIFY] = NULL;
650
int __init au_hnotify_init(void)
655
au_cachep[AuCache_HNOTIFY] = AuCache(au_hnotify);
656
if (au_cachep[AuCache_HNOTIFY]) {
657
err = au_hnotify_op.init();
659
au_hn_destroy_cache();
665
void au_hnotify_fin(void)
668
/* cf. au_cache_fin() */
669
if (au_cachep[AuCache_HNOTIFY])
670
au_hn_destroy_cache();