pandora: update backlight name
[pandora-kernel.git] / fs / aufs / dentry.c
1 /*
2  * Copyright (C) 2005-2012 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  * lookup and dentry operations
21  */
22
23 #include <linux/namei.h>
24 #include "aufs.h"
25
26 static void au_h_nd(struct nameidata *h_nd, struct nameidata *nd)
27 {
28         if (nd) {
29                 *h_nd = *nd;
30
31                 /*
32                  * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
33                  * due to whiteout and branch permission.
34                  */
35                 h_nd->flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
36                                  | LOOKUP_FOLLOW | LOOKUP_EXCL);
37                 /* unnecessary? */
38                 h_nd->intent.open.file = NULL;
39         } else
40                 memset(h_nd, 0, sizeof(*h_nd));
41 }
42
43 struct au_lkup_one_args {
44         struct dentry **errp;
45         struct qstr *name;
46         struct dentry *h_parent;
47         struct au_branch *br;
48         struct nameidata *nd;
49 };
50
51 struct dentry *au_lkup_one(struct qstr *name, struct dentry *h_parent,
52                            struct au_branch *br, struct nameidata *nd)
53 {
54         struct dentry *h_dentry;
55         int err;
56         struct nameidata h_nd;
57
58         if (au_test_fs_null_nd(h_parent->d_sb))
59                 return vfsub_lookup_one_len(name->name, h_parent, name->len);
60
61         au_h_nd(&h_nd, nd);
62         h_nd.path.dentry = h_parent;
63         h_nd.path.mnt = br->br_mnt;
64
65         err = vfsub_name_hash(name->name, &h_nd.last, name->len);
66         h_dentry = ERR_PTR(err);
67         if (!err) {
68                 path_get(&h_nd.path);
69                 h_dentry = vfsub_lookup_hash(&h_nd);
70                 path_put(&h_nd.path);
71         }
72
73         AuTraceErrPtr(h_dentry);
74         return h_dentry;
75 }
76
77 static void au_call_lkup_one(void *args)
78 {
79         struct au_lkup_one_args *a = args;
80         *a->errp = au_lkup_one(a->name, a->h_parent, a->br, a->nd);
81 }
82
83 #define AuLkup_ALLOW_NEG        1
84 #define au_ftest_lkup(flags, name)      ((flags) & AuLkup_##name)
85 #define au_fset_lkup(flags, name) \
86         do { (flags) |= AuLkup_##name; } while (0)
87 #define au_fclr_lkup(flags, name) \
88         do { (flags) &= ~AuLkup_##name; } while (0)
89
90 struct au_do_lookup_args {
91         unsigned int            flags;
92         mode_t                  type;
93         struct nameidata        *nd;
94 };
95
96 /*
97  * returns positive/negative dentry, NULL or an error.
98  * NULL means whiteout-ed or not-found.
99  */
100 static struct dentry*
101 au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
102              aufs_bindex_t bindex, struct qstr *wh_name,
103              struct au_do_lookup_args *args)
104 {
105         struct dentry *h_dentry;
106         struct inode *h_inode, *inode;
107         struct au_branch *br;
108         int wh_found, opq;
109         unsigned char wh_able;
110         const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
111
112         wh_found = 0;
113         br = au_sbr(dentry->d_sb, bindex);
114         wh_able = !!au_br_whable(br->br_perm);
115         if (wh_able)
116                 wh_found = au_wh_test(h_parent, wh_name, br, /*try_sio*/0);
117         h_dentry = ERR_PTR(wh_found);
118         if (!wh_found)
119                 goto real_lookup;
120         if (unlikely(wh_found < 0))
121                 goto out;
122
123         /* We found a whiteout */
124         /* au_set_dbend(dentry, bindex); */
125         au_set_dbwh(dentry, bindex);
126         if (!allow_neg)
127                 return NULL; /* success */
128
129 real_lookup:
130         h_dentry = au_lkup_one(&dentry->d_name, h_parent, br, args->nd);
131         if (IS_ERR(h_dentry))
132                 goto out;
133
134         h_inode = h_dentry->d_inode;
135         if (!h_inode) {
136                 if (!allow_neg)
137                         goto out_neg;
138         } else if (wh_found
139                    || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
140                 goto out_neg;
141
142         if (au_dbend(dentry) <= bindex)
143                 au_set_dbend(dentry, bindex);
144         if (au_dbstart(dentry) < 0 || bindex < au_dbstart(dentry))
145                 au_set_dbstart(dentry, bindex);
146         au_set_h_dptr(dentry, bindex, h_dentry);
147
148         inode = dentry->d_inode;
149         if (!h_inode || !S_ISDIR(h_inode->i_mode) || !wh_able
150             || (inode && !S_ISDIR(inode->i_mode)))
151                 goto out; /* success */
152
153         mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
154         opq = au_diropq_test(h_dentry, br);
155         mutex_unlock(&h_inode->i_mutex);
156         if (opq > 0)
157                 au_set_dbdiropq(dentry, bindex);
158         else if (unlikely(opq < 0)) {
159                 au_set_h_dptr(dentry, bindex, NULL);
160                 h_dentry = ERR_PTR(opq);
161         }
162         goto out;
163
164 out_neg:
165         dput(h_dentry);
166         h_dentry = NULL;
167 out:
168         return h_dentry;
169 }
170
171 static int au_test_shwh(struct super_block *sb, const struct qstr *name)
172 {
173         if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
174                      && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
175                 return -EPERM;
176         return 0;
177 }
178
179 /*
180  * returns the number of lower positive dentries,
181  * otherwise an error.
182  * can be called at unlinking with @type is zero.
183  */
184 int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t bstart, mode_t type,
185                    struct nameidata *nd)
186 {
187         int npositive, err;
188         aufs_bindex_t bindex, btail, bdiropq;
189         unsigned char isdir;
190         struct qstr whname;
191         struct au_do_lookup_args args = {
192                 .flags  = 0,
193                 .type   = type,
194                 .nd     = nd
195         };
196         const struct qstr *name = &dentry->d_name;
197         struct dentry *parent;
198         struct inode *inode;
199
200         err = au_test_shwh(dentry->d_sb, name);
201         if (unlikely(err))
202                 goto out;
203
204         err = au_wh_name_alloc(&whname, name);
205         if (unlikely(err))
206                 goto out;
207
208         inode = dentry->d_inode;
209         isdir = !!(inode && S_ISDIR(inode->i_mode));
210         if (!type)
211                 au_fset_lkup(args.flags, ALLOW_NEG);
212
213         npositive = 0;
214         parent = dget_parent(dentry);
215         btail = au_dbtaildir(parent);
216         for (bindex = bstart; bindex <= btail; bindex++) {
217                 struct dentry *h_parent, *h_dentry;
218                 struct inode *h_inode, *h_dir;
219
220                 h_dentry = au_h_dptr(dentry, bindex);
221                 if (h_dentry) {
222                         if (h_dentry->d_inode)
223                                 npositive++;
224                         if (type != S_IFDIR)
225                                 break;
226                         continue;
227                 }
228                 h_parent = au_h_dptr(parent, bindex);
229                 if (!h_parent)
230                         continue;
231                 h_dir = h_parent->d_inode;
232                 if (!h_dir || !S_ISDIR(h_dir->i_mode))
233                         continue;
234
235                 mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
236                 h_dentry = au_do_lookup(h_parent, dentry, bindex, &whname,
237                                         &args);
238                 mutex_unlock(&h_dir->i_mutex);
239                 err = PTR_ERR(h_dentry);
240                 if (IS_ERR(h_dentry))
241                         goto out_parent;
242                 au_fclr_lkup(args.flags, ALLOW_NEG);
243
244                 if (au_dbwh(dentry) >= 0)
245                         break;
246                 if (!h_dentry)
247                         continue;
248                 h_inode = h_dentry->d_inode;
249                 if (!h_inode)
250                         continue;
251                 npositive++;
252                 if (!args.type)
253                         args.type = h_inode->i_mode & S_IFMT;
254                 if (args.type != S_IFDIR)
255                         break;
256                 else if (isdir) {
257                         /* the type of lower may be different */
258                         bdiropq = au_dbdiropq(dentry);
259                         if (bdiropq >= 0 && bdiropq <= bindex)
260                                 break;
261                 }
262         }
263
264         if (npositive) {
265                 AuLabel(positive);
266                 au_update_dbstart(dentry);
267         }
268         err = npositive;
269         if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
270                      && au_dbstart(dentry) < 0)) {
271                 err = -EIO;
272                 AuIOErr("both of real entry and whiteout found, %.*s, err %d\n",
273                         AuDLNPair(dentry), err);
274         }
275
276 out_parent:
277         dput(parent);
278         kfree(whname.name);
279 out:
280         return err;
281 }
282
283 struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent,
284                                struct au_branch *br)
285 {
286         struct dentry *dentry;
287         int wkq_err;
288
289         if (!au_test_h_perm_sio(parent->d_inode, MAY_EXEC))
290                 dentry = au_lkup_one(name, parent, br, /*nd*/NULL);
291         else {
292                 struct au_lkup_one_args args = {
293                         .errp           = &dentry,
294                         .name           = name,
295                         .h_parent       = parent,
296                         .br             = br,
297                         .nd             = NULL
298                 };
299
300                 wkq_err = au_wkq_wait(au_call_lkup_one, &args);
301                 if (unlikely(wkq_err))
302                         dentry = ERR_PTR(wkq_err);
303         }
304
305         return dentry;
306 }
307
308 /*
309  * lookup @dentry on @bindex which should be negative.
310  */
311 int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex)
312 {
313         int err;
314         struct dentry *parent, *h_parent, *h_dentry;
315
316         parent = dget_parent(dentry);
317         h_parent = au_h_dptr(parent, bindex);
318         h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent,
319                                    au_sbr(dentry->d_sb, bindex));
320         err = PTR_ERR(h_dentry);
321         if (IS_ERR(h_dentry))
322                 goto out;
323         if (unlikely(h_dentry->d_inode)) {
324                 err = -EIO;
325                 AuIOErr("%.*s should be negative on b%d.\n",
326                         AuDLNPair(h_dentry), bindex);
327                 dput(h_dentry);
328                 goto out;
329         }
330
331         err = 0;
332         if (bindex < au_dbstart(dentry))
333                 au_set_dbstart(dentry, bindex);
334         if (au_dbend(dentry) < bindex)
335                 au_set_dbend(dentry, bindex);
336         au_set_h_dptr(dentry, bindex, h_dentry);
337
338 out:
339         dput(parent);
340         return err;
341 }
342
343 /* ---------------------------------------------------------------------- */
344
345 /* subset of struct inode */
346 struct au_iattr {
347         unsigned long           i_ino;
348         /* unsigned int         i_nlink; */
349         uid_t                   i_uid;
350         gid_t                   i_gid;
351         u64                     i_version;
352 /*
353         loff_t                  i_size;
354         blkcnt_t                i_blocks;
355 */
356         umode_t                 i_mode;
357 };
358
359 static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
360 {
361         ia->i_ino = h_inode->i_ino;
362         /* ia->i_nlink = h_inode->i_nlink; */
363         ia->i_uid = h_inode->i_uid;
364         ia->i_gid = h_inode->i_gid;
365         ia->i_version = h_inode->i_version;
366 /*
367         ia->i_size = h_inode->i_size;
368         ia->i_blocks = h_inode->i_blocks;
369 */
370         ia->i_mode = (h_inode->i_mode & S_IFMT);
371 }
372
373 static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
374 {
375         return ia->i_ino != h_inode->i_ino
376                 /* || ia->i_nlink != h_inode->i_nlink */
377                 || ia->i_uid != h_inode->i_uid
378                 || ia->i_gid != h_inode->i_gid
379                 || ia->i_version != h_inode->i_version
380 /*
381                 || ia->i_size != h_inode->i_size
382                 || ia->i_blocks != h_inode->i_blocks
383 */
384                 || ia->i_mode != (h_inode->i_mode & S_IFMT);
385 }
386
387 static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
388                               struct au_branch *br)
389 {
390         int err;
391         struct au_iattr ia;
392         struct inode *h_inode;
393         struct dentry *h_d;
394         struct super_block *h_sb;
395
396         err = 0;
397         memset(&ia, -1, sizeof(ia));
398         h_sb = h_dentry->d_sb;
399         h_inode = h_dentry->d_inode;
400         if (h_inode)
401                 au_iattr_save(&ia, h_inode);
402         else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
403                 /* nfs d_revalidate may return 0 for negative dentry */
404                 /* fuse d_revalidate always return 0 for negative dentry */
405                 goto out;
406
407         /* main purpose is namei.c:cached_lookup() and d_revalidate */
408         h_d = au_lkup_one(&h_dentry->d_name, h_parent, br, /*nd*/NULL);
409         err = PTR_ERR(h_d);
410         if (IS_ERR(h_d))
411                 goto out;
412
413         err = 0;
414         if (unlikely(h_d != h_dentry
415                      || h_d->d_inode != h_inode
416                      || (h_inode && au_iattr_test(&ia, h_inode))))
417                 err = au_busy_or_stale();
418         dput(h_d);
419
420 out:
421         AuTraceErr(err);
422         return err;
423 }
424
425 int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
426                 struct dentry *h_parent, struct au_branch *br)
427 {
428         int err;
429
430         err = 0;
431         if (udba == AuOpt_UDBA_REVAL
432             && !au_test_fs_remote(h_dentry->d_sb)) {
433                 IMustLock(h_dir);
434                 err = (h_dentry->d_parent->d_inode != h_dir);
435         } else if (udba != AuOpt_UDBA_NONE)
436                 err = au_h_verify_dentry(h_dentry, h_parent, br);
437
438         return err;
439 }
440
441 /* ---------------------------------------------------------------------- */
442
443 static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
444 {
445         int err;
446         aufs_bindex_t new_bindex, bindex, bend, bwh, bdiropq;
447         struct au_hdentry tmp, *p, *q;
448         struct au_dinfo *dinfo;
449         struct super_block *sb;
450
451         DiMustWriteLock(dentry);
452
453         sb = dentry->d_sb;
454         dinfo = au_di(dentry);
455         bend = dinfo->di_bend;
456         bwh = dinfo->di_bwh;
457         bdiropq = dinfo->di_bdiropq;
458         p = dinfo->di_hdentry + dinfo->di_bstart;
459         for (bindex = dinfo->di_bstart; bindex <= bend; bindex++, p++) {
460                 if (!p->hd_dentry)
461                         continue;
462
463                 new_bindex = au_br_index(sb, p->hd_id);
464                 if (new_bindex == bindex)
465                         continue;
466
467                 if (dinfo->di_bwh == bindex)
468                         bwh = new_bindex;
469                 if (dinfo->di_bdiropq == bindex)
470                         bdiropq = new_bindex;
471                 if (new_bindex < 0) {
472                         au_hdput(p);
473                         p->hd_dentry = NULL;
474                         continue;
475                 }
476
477                 /* swap two lower dentries, and loop again */
478                 q = dinfo->di_hdentry + new_bindex;
479                 tmp = *q;
480                 *q = *p;
481                 *p = tmp;
482                 if (tmp.hd_dentry) {
483                         bindex--;
484                         p--;
485                 }
486         }
487
488         dinfo->di_bwh = -1;
489         if (bwh >= 0 && bwh <= au_sbend(sb) && au_sbr_whable(sb, bwh))
490                 dinfo->di_bwh = bwh;
491
492         dinfo->di_bdiropq = -1;
493         if (bdiropq >= 0
494             && bdiropq <= au_sbend(sb)
495             && au_sbr_whable(sb, bdiropq))
496                 dinfo->di_bdiropq = bdiropq;
497
498         err = -EIO;
499         dinfo->di_bstart = -1;
500         dinfo->di_bend = -1;
501         bend = au_dbend(parent);
502         p = dinfo->di_hdentry;
503         for (bindex = 0; bindex <= bend; bindex++, p++)
504                 if (p->hd_dentry) {
505                         dinfo->di_bstart = bindex;
506                         break;
507                 }
508
509         if (dinfo->di_bstart >= 0) {
510                 p = dinfo->di_hdentry + bend;
511                 for (bindex = bend; bindex >= 0; bindex--, p--)
512                         if (p->hd_dentry) {
513                                 dinfo->di_bend = bindex;
514                                 err = 0;
515                                 break;
516                         }
517         }
518
519         return err;
520 }
521
522 static void au_do_hide(struct dentry *dentry)
523 {
524         struct inode *inode;
525
526         inode = dentry->d_inode;
527         if (inode) {
528                 if (!S_ISDIR(inode->i_mode)) {
529                         if (inode->i_nlink && !d_unhashed(dentry))
530                                 drop_nlink(inode);
531                 } else {
532                         clear_nlink(inode);
533                         /* stop next lookup */
534                         inode->i_flags |= S_DEAD;
535                 }
536                 smp_mb(); /* necessary? */
537         }
538         d_drop(dentry);
539 }
540
541 static int au_hide_children(struct dentry *parent)
542 {
543         int err, i, j, ndentry;
544         struct au_dcsub_pages dpages;
545         struct au_dpage *dpage;
546         struct dentry *dentry;
547
548         err = au_dpages_init(&dpages, GFP_NOFS);
549         if (unlikely(err))
550                 goto out;
551         err = au_dcsub_pages(&dpages, parent, NULL, NULL);
552         if (unlikely(err))
553                 goto out_dpages;
554
555         /* in reverse order */
556         for (i = dpages.ndpage - 1; i >= 0; i--) {
557                 dpage = dpages.dpages + i;
558                 ndentry = dpage->ndentry;
559                 for (j = ndentry - 1; j >= 0; j--) {
560                         dentry = dpage->dentries[j];
561                         if (dentry != parent)
562                                 au_do_hide(dentry);
563                 }
564         }
565
566 out_dpages:
567         au_dpages_free(&dpages);
568 out:
569         return err;
570 }
571
572 static void au_hide(struct dentry *dentry)
573 {
574         int err;
575         struct inode *inode;
576
577         AuDbgDentry(dentry);
578         inode = dentry->d_inode;
579         if (inode && S_ISDIR(inode->i_mode)) {
580                 /* shrink_dcache_parent(dentry); */
581                 err = au_hide_children(dentry);
582                 if (unlikely(err))
583                         AuIOErr("%.*s, failed hiding children, ignored %d\n",
584                                 AuDLNPair(dentry), err);
585         }
586         au_do_hide(dentry);
587 }
588
589 /*
590  * By adding a dirty branch, a cached dentry may be affected in various ways.
591  *
592  * a dirty branch is added
593  * - on the top of layers
594  * - in the middle of layers
595  * - to the bottom of layers
596  *
597  * on the added branch there exists
598  * - a whiteout
599  * - a diropq
600  * - a same named entry
601  *   + exist
602  *     * negative --> positive
603  *     * positive --> positive
604  *       - type is unchanged
605  *       - type is changed
606  *   + doesn't exist
607  *     * negative --> negative
608  *     * positive --> negative (rejected by au_br_del() for non-dir case)
609  * - none
610  */
611 static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
612                                struct au_dinfo *tmp)
613 {
614         int err;
615         aufs_bindex_t bindex, bend;
616         struct {
617                 struct dentry *dentry;
618                 struct inode *inode;
619                 mode_t mode;
620         } orig_h, tmp_h;
621         struct au_hdentry *hd;
622         struct inode *inode, *h_inode;
623         struct dentry *h_dentry;
624
625         err = 0;
626         AuDebugOn(dinfo->di_bstart < 0);
627         orig_h.dentry = dinfo->di_hdentry[dinfo->di_bstart].hd_dentry;
628         orig_h.inode = orig_h.dentry->d_inode;
629         orig_h.mode = 0;
630         if (orig_h.inode)
631                 orig_h.mode = orig_h.inode->i_mode & S_IFMT;
632         memset(&tmp_h, 0, sizeof(tmp_h));
633         if (tmp->di_bstart >= 0) {
634                 tmp_h.dentry = tmp->di_hdentry[tmp->di_bstart].hd_dentry;
635                 tmp_h.inode = tmp_h.dentry->d_inode;
636                 if (tmp_h.inode)
637                         tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
638         }
639
640         inode = dentry->d_inode;
641         if (!orig_h.inode) {
642                 AuDbg("nagative originally\n");
643                 if (inode) {
644                         au_hide(dentry);
645                         goto out;
646                 }
647                 AuDebugOn(inode);
648                 AuDebugOn(dinfo->di_bstart != dinfo->di_bend);
649                 AuDebugOn(dinfo->di_bdiropq != -1);
650
651                 if (!tmp_h.inode) {
652                         AuDbg("negative --> negative\n");
653                         /* should have only one negative lower */
654                         if (tmp->di_bstart >= 0
655                             && tmp->di_bstart < dinfo->di_bstart) {
656                                 AuDebugOn(tmp->di_bstart != tmp->di_bend);
657                                 AuDebugOn(dinfo->di_bstart != dinfo->di_bend);
658                                 au_set_h_dptr(dentry, dinfo->di_bstart, NULL);
659                                 au_di_cp(dinfo, tmp);
660                                 hd = tmp->di_hdentry + tmp->di_bstart;
661                                 au_set_h_dptr(dentry, tmp->di_bstart,
662                                               dget(hd->hd_dentry));
663                         }
664                         au_dbg_verify_dinode(dentry);
665                 } else {
666                         AuDbg("negative --> positive\n");
667                         /*
668                          * similar to the behaviour of creating with bypassing
669                          * aufs.
670                          * unhash it in order to force an error in the
671                          * succeeding create operation.
672                          * we should not set S_DEAD here.
673                          */
674                         d_drop(dentry);
675                         /* au_di_swap(tmp, dinfo); */
676                         au_dbg_verify_dinode(dentry);
677                 }
678         } else {
679                 AuDbg("positive originally\n");
680                 /* inode may be NULL */
681                 AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
682                 if (!tmp_h.inode) {
683                         AuDbg("positive --> negative\n");
684                         /* or bypassing aufs */
685                         au_hide(dentry);
686                         if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_bstart)
687                                 dinfo->di_bwh = tmp->di_bwh;
688                         if (inode)
689                                 err = au_refresh_hinode_self(inode);
690                         au_dbg_verify_dinode(dentry);
691                 } else if (orig_h.mode == tmp_h.mode) {
692                         AuDbg("positive --> positive, same type\n");
693                         if (!S_ISDIR(orig_h.mode)
694                             && dinfo->di_bstart > tmp->di_bstart) {
695                                 /*
696                                  * similar to the behaviour of removing and
697                                  * creating.
698                                  */
699                                 au_hide(dentry);
700                                 if (inode)
701                                         err = au_refresh_hinode_self(inode);
702                                 au_dbg_verify_dinode(dentry);
703                         } else {
704                                 /* fill empty slots */
705                                 if (dinfo->di_bstart > tmp->di_bstart)
706                                         dinfo->di_bstart = tmp->di_bstart;
707                                 if (dinfo->di_bend < tmp->di_bend)
708                                         dinfo->di_bend = tmp->di_bend;
709                                 dinfo->di_bwh = tmp->di_bwh;
710                                 dinfo->di_bdiropq = tmp->di_bdiropq;
711                                 hd = tmp->di_hdentry;
712                                 bend = dinfo->di_bend;
713                                 for (bindex = tmp->di_bstart; bindex <= bend;
714                                      bindex++) {
715                                         if (au_h_dptr(dentry, bindex))
716                                                 continue;
717                                         h_dentry = hd[bindex].hd_dentry;
718                                         if (!h_dentry)
719                                                 continue;
720                                         h_inode = h_dentry->d_inode;
721                                         AuDebugOn(!h_inode);
722                                         AuDebugOn(orig_h.mode
723                                                   != (h_inode->i_mode
724                                                       & S_IFMT));
725                                         au_set_h_dptr(dentry, bindex,
726                                                       dget(h_dentry));
727                                 }
728                                 err = au_refresh_hinode(inode, dentry);
729                                 au_dbg_verify_dinode(dentry);
730                         }
731                 } else {
732                         AuDbg("positive --> positive, different type\n");
733                         /* similar to the behaviour of removing and creating */
734                         au_hide(dentry);
735                         if (inode)
736                                 err = au_refresh_hinode_self(inode);
737                         au_dbg_verify_dinode(dentry);
738                 }
739         }
740
741 out:
742         return err;
743 }
744
745 int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
746 {
747         int err, ebrange;
748         unsigned int sigen;
749         struct au_dinfo *dinfo, *tmp;
750         struct super_block *sb;
751         struct inode *inode;
752
753         DiMustWriteLock(dentry);
754         AuDebugOn(IS_ROOT(dentry));
755         AuDebugOn(!parent->d_inode);
756
757         sb = dentry->d_sb;
758         inode = dentry->d_inode;
759         sigen = au_sigen(sb);
760         err = au_digen_test(parent, sigen);
761         if (unlikely(err))
762                 goto out;
763
764         dinfo = au_di(dentry);
765         err = au_di_realloc(dinfo, au_sbend(sb) + 1);
766         if (unlikely(err))
767                 goto out;
768         ebrange = au_dbrange_test(dentry);
769         if (!ebrange)
770                 ebrange = au_do_refresh_hdentry(dentry, parent);
771
772         if (d_unhashed(dentry) || ebrange) {
773                 AuDebugOn(au_dbstart(dentry) < 0 && au_dbend(dentry) >= 0);
774                 if (inode)
775                         err = au_refresh_hinode_self(inode);
776                 au_dbg_verify_dinode(dentry);
777                 if (!err)
778                         goto out_dgen; /* success */
779                 goto out;
780         }
781
782         /* temporary dinfo */
783         AuDbgDentry(dentry);
784         err = -ENOMEM;
785         tmp = au_di_alloc(sb, AuLsc_DI_TMP);
786         if (unlikely(!tmp))
787                 goto out;
788         au_di_swap(tmp, dinfo);
789         /* returns the number of positive dentries */
790         /*
791          * if current working dir is removed, it returns an error.
792          * but the dentry is legal.
793          */
794         err = au_lkup_dentry(dentry, /*bstart*/0, /*type*/0, /*nd*/NULL);
795         AuDbgDentry(dentry);
796         au_di_swap(tmp, dinfo);
797         if (err == -ENOENT)
798                 err = 0;
799         if (err >= 0) {
800                 /* compare/refresh by dinfo */
801                 AuDbgDentry(dentry);
802                 err = au_refresh_by_dinfo(dentry, dinfo, tmp);
803                 au_dbg_verify_dinode(dentry);
804                 AuTraceErr(err);
805         }
806         au_rw_write_unlock(&tmp->di_rwsem);
807         au_di_free(tmp);
808         if (unlikely(err))
809                 goto out;
810
811 out_dgen:
812         au_update_digen(dentry);
813 out:
814         if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
815                 AuIOErr("failed refreshing %.*s, %d\n",
816                         AuDLNPair(dentry), err);
817                 AuDbgDentry(dentry);
818         }
819         AuTraceErr(err);
820         return err;
821 }
822
823 static noinline_for_stack
824 int au_do_h_d_reval(struct dentry *h_dentry, struct nameidata *nd,
825                     struct dentry *dentry, aufs_bindex_t bindex)
826 {
827         int err, valid;
828         int (*reval)(struct dentry *, struct nameidata *);
829
830         err = 0;
831         if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
832                 goto out;
833         reval = h_dentry->d_op->d_revalidate;
834
835         AuDbg("b%d\n", bindex);
836         if (au_test_fs_null_nd(h_dentry->d_sb))
837                 /* it may return tri-state */
838                 valid = reval(h_dentry, NULL);
839         else {
840                 struct nameidata h_nd;
841                 int locked;
842                 struct dentry *parent;
843
844                 au_h_nd(&h_nd, nd);
845                 parent = nd->path.dentry;
846                 locked = (nd && nd->path.dentry != dentry);
847                 if (locked)
848                         di_read_lock_parent(parent, AuLock_IR);
849                 BUG_ON(bindex > au_dbend(parent));
850                 h_nd.path.dentry = au_h_dptr(parent, bindex);
851                 BUG_ON(!h_nd.path.dentry);
852                 h_nd.path.mnt = au_sbr(parent->d_sb, bindex)->br_mnt;
853                 path_get(&h_nd.path);
854                 valid = reval(h_dentry, &h_nd);
855                 path_put(&h_nd.path);
856                 if (locked)
857                         di_read_unlock(parent, AuLock_IR);
858         }
859
860         if (unlikely(valid < 0))
861                 err = valid;
862         else if (!valid)
863                 err = -EINVAL;
864
865 out:
866         AuTraceErr(err);
867         return err;
868 }
869
870 /* todo: remove this */
871 static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
872                           struct nameidata *nd, int do_udba)
873 {
874         int err;
875         umode_t mode, h_mode;
876         aufs_bindex_t bindex, btail, bstart, ibs, ibe;
877         unsigned char plus, unhashed, is_root, h_plus;
878         struct inode *h_inode, *h_cached_inode;
879         struct dentry *h_dentry;
880         struct qstr *name, *h_name;
881
882         err = 0;
883         plus = 0;
884         mode = 0;
885         ibs = -1;
886         ibe = -1;
887         unhashed = !!d_unhashed(dentry);
888         is_root = !!IS_ROOT(dentry);
889         name = &dentry->d_name;
890
891         /*
892          * Theoretically, REVAL test should be unnecessary in case of
893          * {FS,I}NOTIFY.
894          * But {fs,i}notify doesn't fire some necessary events,
895          *      IN_ATTRIB for atime/nlink/pageio
896          *      IN_DELETE for NFS dentry
897          * Let's do REVAL test too.
898          */
899         if (do_udba && inode) {
900                 mode = (inode->i_mode & S_IFMT);
901                 plus = (inode->i_nlink > 0);
902                 ibs = au_ibstart(inode);
903                 ibe = au_ibend(inode);
904         }
905
906         bstart = au_dbstart(dentry);
907         btail = bstart;
908         if (inode && S_ISDIR(inode->i_mode))
909                 btail = au_dbtaildir(dentry);
910         for (bindex = bstart; bindex <= btail; bindex++) {
911                 h_dentry = au_h_dptr(dentry, bindex);
912                 if (!h_dentry)
913                         continue;
914
915                 AuDbg("b%d, %.*s\n", bindex, AuDLNPair(h_dentry));
916                 spin_lock(&h_dentry->d_lock);
917                 h_name = &h_dentry->d_name;
918                 if (unlikely(do_udba
919                              && !is_root
920                              && (unhashed != !!d_unhashed(h_dentry)
921                                  || name->len != h_name->len
922                                  || memcmp(name->name, h_name->name, name->len))
923                             )) {
924                         AuDbg("unhash 0x%x 0x%x, %.*s %.*s\n",
925                                   unhashed, d_unhashed(h_dentry),
926                                   AuDLNPair(dentry), AuDLNPair(h_dentry));
927                         spin_unlock(&h_dentry->d_lock);
928                         goto err;
929                 }
930                 spin_unlock(&h_dentry->d_lock);
931
932                 err = au_do_h_d_reval(h_dentry, nd, dentry, bindex);
933                 if (unlikely(err))
934                         /* do not goto err, to keep the errno */
935                         break;
936
937                 /* todo: plink too? */
938                 if (!do_udba)
939                         continue;
940
941                 /* UDBA tests */
942                 h_inode = h_dentry->d_inode;
943                 if (unlikely(!!inode != !!h_inode))
944                         goto err;
945
946                 h_plus = plus;
947                 h_mode = mode;
948                 h_cached_inode = h_inode;
949                 if (h_inode) {
950                         h_mode = (h_inode->i_mode & S_IFMT);
951                         h_plus = (h_inode->i_nlink > 0);
952                 }
953                 if (inode && ibs <= bindex && bindex <= ibe)
954                         h_cached_inode = au_h_iptr(inode, bindex);
955
956                 if (unlikely(plus != h_plus
957                              || mode != h_mode
958                              || h_cached_inode != h_inode))
959                         goto err;
960                 continue;
961
962         err:
963                 err = -EINVAL;
964                 break;
965         }
966
967         return err;
968 }
969
970 /* todo: consolidate with do_refresh() and au_reval_for_attr() */
971 static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
972 {
973         int err;
974         struct dentry *parent;
975
976         if (!au_digen_test(dentry, sigen))
977                 return 0;
978
979         parent = dget_parent(dentry);
980         di_read_lock_parent(parent, AuLock_IR);
981         AuDebugOn(au_digen_test(parent, sigen));
982         au_dbg_verify_gen(parent, sigen);
983         err = au_refresh_dentry(dentry, parent);
984         di_read_unlock(parent, AuLock_IR);
985         dput(parent);
986         AuTraceErr(err);
987         return err;
988 }
989
990 int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
991 {
992         int err;
993         struct dentry *d, *parent;
994         struct inode *inode;
995
996         if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
997                 return simple_reval_dpath(dentry, sigen);
998
999         /* slow loop, keep it simple and stupid */
1000         /* cf: au_cpup_dirs() */
1001         err = 0;
1002         parent = NULL;
1003         while (au_digen_test(dentry, sigen)) {
1004                 d = dentry;
1005                 while (1) {
1006                         dput(parent);
1007                         parent = dget_parent(d);
1008                         if (!au_digen_test(parent, sigen))
1009                                 break;
1010                         d = parent;
1011                 }
1012
1013                 inode = d->d_inode;
1014                 if (d != dentry)
1015                         di_write_lock_child2(d);
1016
1017                 /* someone might update our dentry while we were sleeping */
1018                 if (au_digen_test(d, sigen)) {
1019                         /*
1020                          * todo: consolidate with simple_reval_dpath(),
1021                          * do_refresh() and au_reval_for_attr().
1022                          */
1023                         di_read_lock_parent(parent, AuLock_IR);
1024                         err = au_refresh_dentry(d, parent);
1025                         di_read_unlock(parent, AuLock_IR);
1026                 }
1027
1028                 if (d != dentry)
1029                         di_write_unlock(d);
1030                 dput(parent);
1031                 if (unlikely(err))
1032                         break;
1033         }
1034
1035         return err;
1036 }
1037
1038 /*
1039  * if valid returns 1, otherwise 0.
1040  */
1041 static int aufs_d_revalidate(struct dentry *dentry, struct nameidata *nd)
1042 {
1043         int valid, err;
1044         unsigned int sigen;
1045         unsigned char do_udba;
1046         struct super_block *sb;
1047         struct inode *inode;
1048
1049         /* todo: support rcu-walk? */
1050         if (nd && (nd->flags & LOOKUP_RCU))
1051                 return -ECHILD;
1052
1053         valid = 0;
1054         if (unlikely(!au_di(dentry)))
1055                 goto out;
1056
1057         inode = dentry->d_inode;
1058         if (inode && is_bad_inode(inode))
1059                 goto out;
1060
1061         valid = 1;
1062         sb = dentry->d_sb;
1063         /*
1064          * todo: very ugly
1065          * i_mutex of parent dir may be held,
1066          * but we should not return 'invalid' due to busy.
1067          */
1068         err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
1069         if (unlikely(err)) {
1070                 valid = err;
1071                 AuTraceErr(err);
1072                 goto out;
1073         }
1074         if (unlikely(au_dbrange_test(dentry))) {
1075                 err = -EINVAL;
1076                 AuTraceErr(err);
1077                 goto out_dgrade;
1078         }
1079
1080         sigen = au_sigen(sb);
1081         if (au_digen_test(dentry, sigen)) {
1082                 AuDebugOn(IS_ROOT(dentry));
1083                 err = au_reval_dpath(dentry, sigen);
1084                 if (unlikely(err)) {
1085                         AuTraceErr(err);
1086                         goto out_dgrade;
1087                 }
1088         }
1089         di_downgrade_lock(dentry, AuLock_IR);
1090
1091         err = -EINVAL;
1092         if (inode && (IS_DEADDIR(inode) || !inode->i_nlink))
1093                 goto out_inval;
1094
1095         do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
1096         if (do_udba && inode) {
1097                 aufs_bindex_t bstart = au_ibstart(inode);
1098                 struct inode *h_inode;
1099
1100                 if (bstart >= 0) {
1101                         h_inode = au_h_iptr(inode, bstart);
1102                         if (h_inode && au_test_higen(inode, h_inode))
1103                                 goto out_inval;
1104                 }
1105         }
1106
1107         err = h_d_revalidate(dentry, inode, nd, do_udba);
1108         if (unlikely(!err && do_udba && au_dbstart(dentry) < 0)) {
1109                 err = -EIO;
1110                 AuDbg("both of real entry and whiteout found, %.*s, err %d\n",
1111                       AuDLNPair(dentry), err);
1112         }
1113         goto out_inval;
1114
1115 out_dgrade:
1116         di_downgrade_lock(dentry, AuLock_IR);
1117 out_inval:
1118         aufs_read_unlock(dentry, AuLock_IR);
1119         AuTraceErr(err);
1120         valid = !err;
1121 out:
1122         if (!valid) {
1123                 AuDbg("%.*s invalid, %d\n", AuDLNPair(dentry), valid);
1124                 d_drop(dentry);
1125         }
1126         return valid;
1127 }
1128
1129 static void aufs_d_release(struct dentry *dentry)
1130 {
1131         if (au_di(dentry)) {
1132                 au_di_fin(dentry);
1133                 au_hn_di_reinit(dentry);
1134         }
1135 }
1136
1137 const struct dentry_operations aufs_dop = {
1138         .d_revalidate   = aufs_d_revalidate,
1139         .d_release      = aufs_d_release
1140 };