Merge branch 'stable-3.2' into pandora-3.2
[pandora-kernel.git] / fs / aufs / hnotify.c
1 /*
2  * Copyright (C) 2005-2013 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, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 /*
20  * abstraction to notify the direct changes on lower directories
21  */
22
23 #include "aufs.h"
24
25 int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
26 {
27         int err;
28         struct au_hnotify *hn;
29
30         err = -ENOMEM;
31         hn = au_cache_alloc_hnotify();
32         if (hn) {
33                 hn->hn_aufs_inode = inode;
34                 hinode->hi_notify = hn;
35                 err = au_hnotify_op.alloc(hinode);
36                 AuTraceErr(err);
37                 if (unlikely(err)) {
38                         hinode->hi_notify = NULL;
39                         au_cache_free_hnotify(hn);
40                         /*
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.
45                          */
46                         if (err == -EEXIST)
47                                 err = 0;
48                 }
49         }
50
51         AuTraceErr(err);
52         return err;
53 }
54
55 void au_hn_free(struct au_hinode *hinode)
56 {
57         struct au_hnotify *hn;
58
59         hn = hinode->hi_notify;
60         if (hn) {
61                 hinode->hi_notify = NULL;
62                 if (au_hnotify_op.free(hinode, hn))
63                         au_cache_free_hnotify(hn);
64         }
65 }
66
67 /* ---------------------------------------------------------------------- */
68
69 void au_hn_ctl(struct au_hinode *hinode, int do_set)
70 {
71         if (hinode->hi_notify)
72                 au_hnotify_op.ctl(hinode, do_set);
73 }
74
75 void au_hn_reset(struct inode *inode, unsigned int flags)
76 {
77         aufs_bindex_t bindex, bend;
78         struct inode *hi;
79         struct dentry *iwhdentry;
80
81         bend = au_ibend(inode);
82         for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
83                 hi = au_h_iptr(inode, bindex);
84                 if (!hi)
85                         continue;
86
87                 /* mutex_lock_nested(&hi->i_mutex, AuLsc_I_CHILD); */
88                 iwhdentry = au_hi_wh(inode, bindex);
89                 if (iwhdentry)
90                         dget(iwhdentry);
91                 au_igrab(hi);
92                 au_set_h_iptr(inode, bindex, NULL, 0);
93                 au_set_h_iptr(inode, bindex, au_igrab(hi),
94                               flags & ~AuHi_XINO);
95                 iput(hi);
96                 dput(iwhdentry);
97                 /* mutex_unlock(&hi->i_mutex); */
98         }
99 }
100
101 /* ---------------------------------------------------------------------- */
102
103 static int hn_xino(struct inode *inode, struct inode *h_inode)
104 {
105         int err;
106         aufs_bindex_t bindex, bend, bfound, bstart;
107         struct inode *h_i;
108
109         err = 0;
110         if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
111                 pr_warn("branch root dir was changed\n");
112                 goto out;
113         }
114
115         bfound = -1;
116         bend = au_ibend(inode);
117         bstart = au_ibstart(inode);
118 #if 0 /* reserved for future use */
119         if (bindex == bend) {
120                 /* keep this ino in rename case */
121                 goto out;
122         }
123 #endif
124         for (bindex = bstart; bindex <= bend; bindex++)
125                 if (au_h_iptr(inode, bindex) == h_inode) {
126                         bfound = bindex;
127                         break;
128                 }
129         if (bfound < 0)
130                 goto out;
131
132         for (bindex = bstart; bindex <= bend; bindex++) {
133                 h_i = au_h_iptr(inode, bindex);
134                 if (!h_i)
135                         continue;
136
137                 err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
138                 /* ignore this error */
139                 /* bad action? */
140         }
141
142         /* children inode number will be broken */
143
144 out:
145         AuTraceErr(err);
146         return err;
147 }
148
149 static int hn_gen_tree(struct dentry *dentry)
150 {
151         int err, i, j, ndentry;
152         struct au_dcsub_pages dpages;
153         struct au_dpage *dpage;
154         struct dentry **dentries;
155
156         err = au_dpages_init(&dpages, GFP_NOFS);
157         if (unlikely(err))
158                 goto out;
159         err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
160         if (unlikely(err))
161                 goto out_dpages;
162
163         for (i = 0; i < dpages.ndpage; i++) {
164                 dpage = dpages.dpages + i;
165                 dentries = dpage->dentries;
166                 ndentry = dpage->ndentry;
167                 for (j = 0; j < ndentry; j++) {
168                         struct dentry *d;
169
170                         d = dentries[j];
171                         if (IS_ROOT(d))
172                                 continue;
173
174                         au_digen_dec(d);
175                         if (d->d_inode)
176                                 /* todo: reset children xino?
177                                    cached children only? */
178                                 au_iigen_dec(d->d_inode);
179                 }
180         }
181
182 out_dpages:
183         au_dpages_free(&dpages);
184
185 #if 0
186         /* discard children */
187         dentry_unhash(dentry);
188         dput(dentry);
189 #endif
190 out:
191         return err;
192 }
193
194 /*
195  * return 0 if processed.
196  */
197 static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
198                            const unsigned int isdir)
199 {
200         int err;
201         struct dentry *d;
202         struct qstr *dname;
203
204         err = 1;
205         if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
206                 pr_warn("branch root dir was changed\n");
207                 err = 0;
208                 goto out;
209         }
210
211         if (!isdir) {
212                 AuDebugOn(!name);
213                 au_iigen_dec(inode);
214                 spin_lock(&inode->i_lock);
215                 list_for_each_entry(d, &inode->i_dentry, d_alias) {
216                         spin_lock(&d->d_lock);
217                         dname = &d->d_name;
218                         if (dname->len != nlen
219                             && memcmp(dname->name, name, nlen)) {
220                                 spin_unlock(&d->d_lock);
221                                 continue;
222                         }
223                         err = 0;
224                         au_digen_dec(d);
225                         spin_unlock(&d->d_lock);
226                         break;
227                 }
228                 spin_unlock(&inode->i_lock);
229         } else {
230                 au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
231                 d = d_find_alias(inode);
232                 if (!d) {
233                         au_iigen_dec(inode);
234                         goto out;
235                 }
236
237                 spin_lock(&d->d_lock);
238                 dname = &d->d_name;
239                 if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
240                         spin_unlock(&d->d_lock);
241                         err = hn_gen_tree(d);
242                         spin_lock(&d->d_lock);
243                 }
244                 spin_unlock(&d->d_lock);
245                 dput(d);
246         }
247
248 out:
249         AuTraceErr(err);
250         return err;
251 }
252
253 static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
254 {
255         int err;
256         struct inode *inode;
257
258         inode = dentry->d_inode;
259         if (IS_ROOT(dentry)
260             /* || (inode && inode->i_ino == AUFS_ROOT_INO) */
261                 ) {
262                 pr_warn("branch root dir was changed\n");
263                 return 0;
264         }
265
266         err = 0;
267         if (!isdir) {
268                 au_digen_dec(dentry);
269                 if (inode)
270                         au_iigen_dec(inode);
271         } else {
272                 au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
273                 if (inode)
274                         err = hn_gen_tree(dentry);
275         }
276
277         AuTraceErr(err);
278         return err;
279 }
280
281 /* ---------------------------------------------------------------------- */
282
283 /* hnotify job flags */
284 #define AuHnJob_XINO0           1
285 #define AuHnJob_GEN             (1 << 1)
286 #define AuHnJob_DIRENT          (1 << 2)
287 #define AuHnJob_ISDIR           (1 << 3)
288 #define AuHnJob_TRYXINO0        (1 << 4)
289 #define AuHnJob_MNTPNT          (1 << 5)
290 #define au_ftest_hnjob(flags, name)     ((flags) & AuHnJob_##name)
291 #define au_fset_hnjob(flags, name) \
292         do { (flags) |= AuHnJob_##name; } while (0)
293 #define au_fclr_hnjob(flags, name) \
294         do { (flags) &= ~AuHnJob_##name; } while (0)
295
296 enum {
297         AuHn_CHILD,
298         AuHn_PARENT,
299         AuHnLast
300 };
301
302 struct au_hnotify_args {
303         struct inode *h_dir, *dir, *h_child_inode;
304         u32 mask;
305         unsigned int flags[AuHnLast];
306         unsigned int h_child_nlen;
307         char h_child_name[];
308 };
309
310 struct hn_job_args {
311         unsigned int flags;
312         struct inode *inode, *h_inode, *dir, *h_dir;
313         struct dentry *dentry;
314         char *h_name;
315         int h_nlen;
316 };
317
318 static int hn_job(struct hn_job_args *a)
319 {
320         const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
321
322         /* reset xino */
323         if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
324                 hn_xino(a->inode, a->h_inode); /* ignore this error */
325
326         if (au_ftest_hnjob(a->flags, TRYXINO0)
327             && a->inode
328             && a->h_inode) {
329                 mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
330                 if (!a->h_inode->i_nlink)
331                         hn_xino(a->inode, a->h_inode); /* ignore this error */
332                 mutex_unlock(&a->h_inode->i_mutex);
333         }
334
335         /* make the generation obsolete */
336         if (au_ftest_hnjob(a->flags, GEN)) {
337                 int err = -1;
338                 if (a->inode)
339                         err = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
340                                               isdir);
341                 if (err && a->dentry)
342                         hn_gen_by_name(a->dentry, isdir);
343                 /* ignore this error */
344         }
345
346         /* make dir entries obsolete */
347         if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
348                 struct au_vdir *vdir;
349
350                 vdir = au_ivdir(a->inode);
351                 if (vdir)
352                         vdir->vd_jiffy = 0;
353                 /* IMustLock(a->inode); */
354                 /* a->inode->i_version++; */
355         }
356
357         /* can do nothing but warn */
358         if (au_ftest_hnjob(a->flags, MNTPNT)
359             && a->dentry
360             && d_mountpoint(a->dentry))
361                 pr_warn("mount-point %.*s is removed or renamed\n",
362                         AuDLNPair(a->dentry));
363
364         return 0;
365 }
366
367 /* ---------------------------------------------------------------------- */
368
369 static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
370                                            struct inode *dir)
371 {
372         struct dentry *dentry, *d, *parent;
373         struct qstr *dname;
374
375         parent = d_find_alias(dir);
376         if (!parent)
377                 return NULL;
378
379         dentry = NULL;
380         spin_lock(&parent->d_lock);
381         list_for_each_entry(d, &parent->d_subdirs, d_u.d_child) {
382                 /* AuDbg("%.*s\n", AuDLNPair(d)); */
383                 spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
384                 dname = &d->d_name;
385                 if (dname->len != nlen || memcmp(dname->name, name, nlen))
386                         goto cont_unlock;
387                 if (au_di(d))
388                         au_digen_dec(d);
389                 else
390                         goto cont_unlock;
391                 if (d->d_count) {
392                         dentry = dget_dlock(d);
393                         spin_unlock(&d->d_lock);
394                         break;
395                 }
396
397         cont_unlock:
398                 spin_unlock(&d->d_lock);
399         }
400         spin_unlock(&parent->d_lock);
401         dput(parent);
402
403         if (dentry)
404                 di_write_lock_child(dentry);
405
406         return dentry;
407 }
408
409 static struct inode *lookup_wlock_by_ino(struct super_block *sb,
410                                          aufs_bindex_t bindex, ino_t h_ino)
411 {
412         struct inode *inode;
413         ino_t ino;
414         int err;
415
416         inode = NULL;
417         err = au_xino_read(sb, bindex, h_ino, &ino);
418         if (!err && ino)
419                 inode = ilookup(sb, ino);
420         if (!inode)
421                 goto out;
422
423         if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
424                 pr_warn("wrong root branch\n");
425                 iput(inode);
426                 inode = NULL;
427                 goto out;
428         }
429
430         ii_write_lock_child(inode);
431
432 out:
433         return inode;
434 }
435
436 static void au_hn_bh(void *_args)
437 {
438         struct au_hnotify_args *a = _args;
439         struct super_block *sb;
440         aufs_bindex_t bindex, bend, bfound;
441         unsigned char xino, try_iput;
442         int err;
443         struct inode *inode;
444         ino_t h_ino;
445         struct hn_job_args args;
446         struct dentry *dentry;
447         struct au_sbinfo *sbinfo;
448
449         AuDebugOn(!_args);
450         AuDebugOn(!a->h_dir);
451         AuDebugOn(!a->dir);
452         AuDebugOn(!a->mask);
453         AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
454               a->mask, a->dir->i_ino, a->h_dir->i_ino,
455               a->h_child_inode ? a->h_child_inode->i_ino : 0);
456
457         inode = NULL;
458         dentry = NULL;
459         /*
460          * do not lock a->dir->i_mutex here
461          * because of d_revalidate() may cause a deadlock.
462          */
463         sb = a->dir->i_sb;
464         AuDebugOn(!sb);
465         sbinfo = au_sbi(sb);
466         AuDebugOn(!sbinfo);
467         si_write_lock(sb, AuLock_NOPLMW);
468
469         ii_read_lock_parent(a->dir);
470         bfound = -1;
471         bend = au_ibend(a->dir);
472         for (bindex = au_ibstart(a->dir); bindex <= bend; bindex++)
473                 if (au_h_iptr(a->dir, bindex) == a->h_dir) {
474                         bfound = bindex;
475                         break;
476                 }
477         ii_read_unlock(a->dir);
478         if (unlikely(bfound < 0))
479                 goto out;
480
481         xino = !!au_opt_test(au_mntflags(sb), XINO);
482         h_ino = 0;
483         if (a->h_child_inode)
484                 h_ino = a->h_child_inode->i_ino;
485
486         if (a->h_child_nlen
487             && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
488                 || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
489                 dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
490                                               a->dir);
491         try_iput = 0;
492         if (dentry)
493                 inode = dentry->d_inode;
494         if (xino && !inode && h_ino
495             && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
496                 || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
497                 || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
498                 inode = lookup_wlock_by_ino(sb, bfound, h_ino);
499                 try_iput = 1;
500             }
501
502         args.flags = a->flags[AuHn_CHILD];
503         args.dentry = dentry;
504         args.inode = inode;
505         args.h_inode = a->h_child_inode;
506         args.dir = a->dir;
507         args.h_dir = a->h_dir;
508         args.h_name = a->h_child_name;
509         args.h_nlen = a->h_child_nlen;
510         err = hn_job(&args);
511         if (dentry) {
512                 if (au_di(dentry))
513                         di_write_unlock(dentry);
514                 dput(dentry);
515         }
516         if (inode && try_iput) {
517                 ii_write_unlock(inode);
518                 iput(inode);
519         }
520
521         ii_write_lock_parent(a->dir);
522         args.flags = a->flags[AuHn_PARENT];
523         args.dentry = NULL;
524         args.inode = a->dir;
525         args.h_inode = a->h_dir;
526         args.dir = NULL;
527         args.h_dir = NULL;
528         args.h_name = NULL;
529         args.h_nlen = 0;
530         err = hn_job(&args);
531         ii_write_unlock(a->dir);
532
533 out:
534         iput(a->h_child_inode);
535         iput(a->h_dir);
536         iput(a->dir);
537         si_write_unlock(sb);
538         au_nwt_done(&sbinfo->si_nowait);
539         kfree(a);
540 }
541
542 /* ---------------------------------------------------------------------- */
543
544 int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
545                struct qstr *h_child_qstr, struct inode *h_child_inode)
546 {
547         int err, len;
548         unsigned int flags[AuHnLast], f;
549         unsigned char isdir, isroot, wh;
550         struct inode *dir;
551         struct au_hnotify_args *args;
552         char *p, *h_child_name;
553
554         err = 0;
555         AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
556         dir = igrab(hnotify->hn_aufs_inode);
557         if (!dir)
558                 goto out;
559
560         isroot = (dir->i_ino == AUFS_ROOT_INO);
561         wh = 0;
562         h_child_name = (void *)h_child_qstr->name;
563         len = h_child_qstr->len;
564         if (h_child_name) {
565                 if (len > AUFS_WH_PFX_LEN
566                     && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
567                         h_child_name += AUFS_WH_PFX_LEN;
568                         len -= AUFS_WH_PFX_LEN;
569                         wh = 1;
570                 }
571         }
572
573         isdir = 0;
574         if (h_child_inode)
575                 isdir = !!S_ISDIR(h_child_inode->i_mode);
576         flags[AuHn_PARENT] = AuHnJob_ISDIR;
577         flags[AuHn_CHILD] = 0;
578         if (isdir)
579                 flags[AuHn_CHILD] = AuHnJob_ISDIR;
580         au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
581         au_fset_hnjob(flags[AuHn_CHILD], GEN);
582         switch (mask & FS_EVENTS_POSS_ON_CHILD) {
583         case FS_MOVED_FROM:
584         case FS_MOVED_TO:
585                 au_fset_hnjob(flags[AuHn_CHILD], XINO0);
586                 au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
587                 /*FALLTHROUGH*/
588         case FS_CREATE:
589                 AuDebugOn(!h_child_name || !h_child_inode);
590                 break;
591
592         case FS_DELETE:
593                 /*
594                  * aufs never be able to get this child inode.
595                  * revalidation should be in d_revalidate()
596                  * by checking i_nlink, i_generation or d_unhashed().
597                  */
598                 AuDebugOn(!h_child_name);
599                 au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
600                 au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
601                 break;
602
603         default:
604                 AuDebugOn(1);
605         }
606
607         if (wh)
608                 h_child_inode = NULL;
609
610         err = -ENOMEM;
611         /* iput() and kfree() will be called in au_hnotify() */
612         args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
613         if (unlikely(!args)) {
614                 AuErr1("no memory\n");
615                 iput(dir);
616                 goto out;
617         }
618         args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
619         args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
620         args->mask = mask;
621         args->dir = dir;
622         args->h_dir = igrab(h_dir);
623         if (h_child_inode)
624                 h_child_inode = igrab(h_child_inode); /* can be NULL */
625         args->h_child_inode = h_child_inode;
626         args->h_child_nlen = len;
627         if (len) {
628                 p = (void *)args;
629                 p += sizeof(*args);
630                 memcpy(p, h_child_name, len);
631                 p[len] = 0;
632         }
633
634         f = 0;
635         if (!dir->i_nlink)
636                 f = AuWkq_NEST;
637         err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
638         if (unlikely(err)) {
639                 pr_err("wkq %d\n", err);
640                 iput(args->h_child_inode);
641                 iput(args->h_dir);
642                 iput(args->dir);
643                 kfree(args);
644         }
645
646 out:
647         return err;
648 }
649
650 /* ---------------------------------------------------------------------- */
651
652 int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
653 {
654         int err;
655
656         AuDebugOn(!(udba & AuOptMask_UDBA));
657
658         err = 0;
659         if (au_hnotify_op.reset_br)
660                 err = au_hnotify_op.reset_br(udba, br, perm);
661
662         return err;
663 }
664
665 int au_hnotify_init_br(struct au_branch *br, int perm)
666 {
667         int err;
668
669         err = 0;
670         if (au_hnotify_op.init_br)
671                 err = au_hnotify_op.init_br(br, perm);
672
673         return err;
674 }
675
676 void au_hnotify_fin_br(struct au_branch *br)
677 {
678         if (au_hnotify_op.fin_br)
679                 au_hnotify_op.fin_br(br);
680 }
681
682 static void au_hn_destroy_cache(void)
683 {
684         kmem_cache_destroy(au_cachep[AuCache_HNOTIFY]);
685         au_cachep[AuCache_HNOTIFY] = NULL;
686 }
687
688 int __init au_hnotify_init(void)
689 {
690         int err;
691
692         err = -ENOMEM;
693         au_cachep[AuCache_HNOTIFY] = AuCache(au_hnotify);
694         if (au_cachep[AuCache_HNOTIFY]) {
695                 err = 0;
696                 if (au_hnotify_op.init)
697                         err = au_hnotify_op.init();
698                 if (unlikely(err))
699                         au_hn_destroy_cache();
700         }
701         AuTraceErr(err);
702         return err;
703 }
704
705 void au_hnotify_fin(void)
706 {
707         if (au_hnotify_op.fin)
708                 au_hnotify_op.fin();
709         /* cf. au_cache_fin() */
710         if (au_cachep[AuCache_HNOTIFY])
711                 au_hn_destroy_cache();
712 }