1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2005-2021 Junjiro R. Okajima
5 * This program, aufs is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * lookup and dentry operations
23 #include <linux/iversion.h>
24 #include <linux/namei.h>
28 * returns positive/negative dentry, NULL or an error.
29 * NULL means whiteout-ed or not-found.
32 au_do_lookup(struct dentry
*h_parent
, struct dentry
*dentry
,
33 aufs_bindex_t bindex
, struct au_do_lookup_args
*args
)
35 struct dentry
*h_dentry
;
36 struct inode
*h_inode
;
38 struct user_namespace
*h_userns
;
41 unsigned char wh_able
;
42 const unsigned char allow_neg
= !!au_ftest_lkup(args
->flags
, ALLOW_NEG
);
43 const unsigned char ignore_perm
= !!au_ftest_lkup(args
->flags
,
47 br
= au_sbr(dentry
->d_sb
, bindex
);
48 h_path
.dentry
= h_parent
;
49 h_path
.mnt
= au_br_mnt(br
);
50 h_userns
= au_br_userns(br
);
51 wh_able
= !!au_br_whable(br
->br_perm
);
53 wh_found
= au_wh_test(h_userns
, &h_path
, &args
->whname
,
55 h_dentry
= ERR_PTR(wh_found
);
58 if (unlikely(wh_found
< 0))
61 /* We found a whiteout */
62 /* au_set_dbbot(dentry, bindex); */
63 au_set_dbwh(dentry
, bindex
);
65 return NULL
; /* success */
69 h_dentry
= vfsub_lkup_one(args
->name
, &h_path
);
71 h_dentry
= au_sio_lkup_one(h_userns
, args
->name
, &h_path
);
72 if (IS_ERR(h_dentry
)) {
73 if (PTR_ERR(h_dentry
) == -ENAMETOOLONG
79 h_inode
= d_inode(h_dentry
);
80 if (d_is_negative(h_dentry
)) {
84 || (args
->type
&& args
->type
!= (h_inode
->i_mode
& S_IFMT
)))
86 else if (au_ftest_lkup(args
->flags
, DIRREN
)
88 && !au_dr_lkup_h_ino(args
, bindex
, h_inode
->i_ino
)) {
89 AuDbg("b%d %pd ignored hi%llu\n", bindex
, h_dentry
,
90 (unsigned long long)h_inode
->i_ino
);
94 if (au_dbbot(dentry
) <= bindex
)
95 au_set_dbbot(dentry
, bindex
);
96 if (au_dbtop(dentry
) < 0 || bindex
< au_dbtop(dentry
))
97 au_set_dbtop(dentry
, bindex
);
98 au_set_h_dptr(dentry
, bindex
, h_dentry
);
100 if (!d_is_dir(h_dentry
)
102 || (d_really_is_positive(dentry
) && !d_is_dir(dentry
)))
103 goto out
; /* success */
105 h_path
.dentry
= h_dentry
;
106 inode_lock_shared_nested(h_inode
, AuLsc_I_CHILD
);
107 opq
= au_diropq_test(h_userns
, &h_path
);
108 inode_unlock_shared(h_inode
);
110 au_set_dbdiropq(dentry
, bindex
);
111 else if (unlikely(opq
< 0)) {
112 au_set_h_dptr(dentry
, bindex
, NULL
);
113 h_dentry
= ERR_PTR(opq
);
124 static int au_test_shwh(struct super_block
*sb
, const struct qstr
*name
)
126 if (unlikely(!au_opt_test(au_mntflags(sb
), SHWH
)
127 && !strncmp(name
->name
, AUFS_WH_PFX
, AUFS_WH_PFX_LEN
)))
133 * returns the number of lower positive dentries,
134 * otherwise an error.
135 * can be called at unlinking with @type is zero.
137 int au_lkup_dentry(struct dentry
*dentry
, aufs_bindex_t btop
,
141 aufs_bindex_t bindex
, btail
, bdiropq
;
142 unsigned char isdir
, dirperm1
, dirren
;
143 struct au_do_lookup_args args
= {
145 .name
= &dentry
->d_name
147 struct dentry
*parent
;
148 struct super_block
*sb
;
151 err
= au_test_shwh(sb
, args
.name
);
155 err
= au_wh_name_alloc(&args
.whname
, args
.name
);
159 isdir
= !!d_is_dir(dentry
);
160 dirperm1
= !!au_opt_test(au_mntflags(sb
), DIRPERM1
);
161 dirren
= !!au_opt_test(au_mntflags(sb
), DIRREN
);
163 au_fset_lkup(args
.flags
, DIRREN
);
166 parent
= dget_parent(dentry
);
167 btail
= au_dbtaildir(parent
);
168 for (bindex
= btop
; bindex
<= btail
; bindex
++) {
169 struct dentry
*h_parent
, *h_dentry
;
170 struct inode
*h_inode
, *h_dir
;
171 struct au_branch
*br
;
173 h_dentry
= au_h_dptr(dentry
, bindex
);
175 if (d_is_positive(h_dentry
))
179 h_parent
= au_h_dptr(parent
, bindex
);
180 if (!h_parent
|| !d_is_dir(h_parent
))
184 /* if the inum matches, then use the prepared name */
185 err
= au_dr_lkup_name(&args
, bindex
);
190 h_dir
= d_inode(h_parent
);
191 inode_lock_shared_nested(h_dir
, AuLsc_I_PARENT
);
192 h_dentry
= au_do_lookup(h_parent
, dentry
, bindex
, &args
);
193 inode_unlock_shared(h_dir
);
194 err
= PTR_ERR(h_dentry
);
195 if (IS_ERR(h_dentry
))
198 au_fclr_lkup(args
.flags
, ALLOW_NEG
);
200 au_fset_lkup(args
.flags
, IGNORE_PERM
);
202 if (au_dbwh(dentry
) == bindex
)
206 if (d_is_negative(h_dentry
))
208 h_inode
= d_inode(h_dentry
);
211 args
.type
= h_inode
->i_mode
& S_IFMT
;
212 if (args
.type
!= S_IFDIR
)
215 /* the type of lower may be different */
216 bdiropq
= au_dbdiropq(dentry
);
217 if (bdiropq
>= 0 && bdiropq
<= bindex
)
220 br
= au_sbr(sb
, bindex
);
222 && au_dr_hino_test_add(&br
->br_dirren
, h_inode
->i_ino
,
224 /* prepare next name to lookup */
225 err
= au_dr_lkup(&args
, dentry
, bindex
);
233 au_update_dbtop(dentry
);
236 if (unlikely(!au_opt_test(au_mntflags(sb
), UDBA_NONE
)
237 && au_dbtop(dentry
) < 0)) {
239 AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
245 au_kfree_try_rcu(args
.whname
.name
);
247 au_dr_lkup_fin(&args
);
252 struct dentry
*au_sio_lkup_one(struct user_namespace
*userns
, struct qstr
*name
,
255 struct dentry
*dentry
;
258 if (!au_test_h_perm_sio(userns
, d_inode(ppath
->dentry
), MAY_EXEC
))
259 dentry
= vfsub_lkup_one(name
, ppath
);
261 struct vfsub_lkup_one_args args
= {
267 wkq_err
= au_wkq_wait(vfsub_call_lkup_one
, &args
);
268 if (unlikely(wkq_err
))
269 dentry
= ERR_PTR(wkq_err
);
276 * lookup @dentry on @bindex which should be negative.
278 int au_lkup_neg(struct dentry
*dentry
, aufs_bindex_t bindex
, int wh
)
281 struct dentry
*parent
, *h_dentry
;
282 struct au_branch
*br
;
283 struct user_namespace
*h_userns
;
286 parent
= dget_parent(dentry
);
287 br
= au_sbr(dentry
->d_sb
, bindex
);
288 h_ppath
.dentry
= au_h_dptr(parent
, bindex
);
289 h_ppath
.mnt
= au_br_mnt(br
);
290 h_userns
= au_br_userns(br
);
292 h_dentry
= au_whtmp_lkup(h_ppath
.dentry
, br
, &dentry
->d_name
);
294 h_dentry
= au_sio_lkup_one(h_userns
, &dentry
->d_name
, &h_ppath
);
295 err
= PTR_ERR(h_dentry
);
296 if (IS_ERR(h_dentry
))
298 if (unlikely(d_is_positive(h_dentry
))) {
300 AuIOErr("%pd should be negative on b%d.\n", h_dentry
, bindex
);
306 if (bindex
< au_dbtop(dentry
))
307 au_set_dbtop(dentry
, bindex
);
308 if (au_dbbot(dentry
) < bindex
)
309 au_set_dbbot(dentry
, bindex
);
310 au_set_h_dptr(dentry
, bindex
, h_dentry
);
317 /* ---------------------------------------------------------------------- */
319 /* subset of struct inode */
322 /* unsigned int i_nlink; */
333 static void au_iattr_save(struct au_iattr
*ia
, struct inode
*h_inode
)
335 ia
->i_ino
= h_inode
->i_ino
;
336 /* ia->i_nlink = h_inode->i_nlink; */
337 ia
->i_uid
= h_inode
->i_uid
;
338 ia
->i_gid
= h_inode
->i_gid
;
339 ia
->i_version
= inode_query_iversion(h_inode
);
341 ia->i_size = h_inode->i_size;
342 ia->i_blocks = h_inode->i_blocks;
344 ia
->i_mode
= (h_inode
->i_mode
& S_IFMT
);
347 static int au_iattr_test(struct au_iattr
*ia
, struct inode
*h_inode
)
349 return ia
->i_ino
!= h_inode
->i_ino
350 /* || ia->i_nlink != h_inode->i_nlink */
351 || !uid_eq(ia
->i_uid
, h_inode
->i_uid
)
352 || !gid_eq(ia
->i_gid
, h_inode
->i_gid
)
353 || !inode_eq_iversion(h_inode
, ia
->i_version
)
355 || ia->i_size != h_inode->i_size
356 || ia->i_blocks != h_inode->i_blocks
358 || ia
->i_mode
!= (h_inode
->i_mode
& S_IFMT
);
361 static int au_h_verify_dentry(struct dentry
*h_dentry
, struct dentry
*h_parent
,
362 struct au_branch
*br
)
366 struct inode
*h_inode
;
368 struct super_block
*h_sb
;
372 memset(&ia
, -1, sizeof(ia
));
373 h_sb
= h_dentry
->d_sb
;
375 if (d_is_positive(h_dentry
)) {
376 h_inode
= d_inode(h_dentry
);
377 au_iattr_save(&ia
, h_inode
);
378 } else if (au_test_nfs(h_sb
) || au_test_fuse(h_sb
))
379 /* nfs d_revalidate may return 0 for negative dentry */
380 /* fuse d_revalidate always return 0 for negative dentry */
383 /* main purpose is namei.c:cached_lookup() and d_revalidate */
384 h_ppath
.dentry
= h_parent
;
385 h_ppath
.mnt
= au_br_mnt(br
);
386 h_d
= vfsub_lkup_one(&h_dentry
->d_name
, &h_ppath
);
392 if (unlikely(h_d
!= h_dentry
393 || d_inode(h_d
) != h_inode
394 || (h_inode
&& au_iattr_test(&ia
, h_inode
))))
395 err
= au_busy_or_stale();
403 int au_h_verify(struct dentry
*h_dentry
, unsigned int udba
, struct inode
*h_dir
,
404 struct dentry
*h_parent
, struct au_branch
*br
)
409 if (udba
== AuOpt_UDBA_REVAL
410 && !au_test_fs_remote(h_dentry
->d_sb
)) {
412 err
= (d_inode(h_dentry
->d_parent
) != h_dir
);
413 } else if (udba
!= AuOpt_UDBA_NONE
)
414 err
= au_h_verify_dentry(h_dentry
, h_parent
, br
);
419 /* ---------------------------------------------------------------------- */
421 static int au_do_refresh_hdentry(struct dentry
*dentry
, struct dentry
*parent
)
424 aufs_bindex_t new_bindex
, bindex
, bbot
, bwh
, bdiropq
;
425 struct au_hdentry tmp
, *p
, *q
;
426 struct au_dinfo
*dinfo
;
427 struct super_block
*sb
;
429 DiMustWriteLock(dentry
);
432 dinfo
= au_di(dentry
);
433 bbot
= dinfo
->di_bbot
;
435 bdiropq
= dinfo
->di_bdiropq
;
436 bindex
= dinfo
->di_btop
;
437 p
= au_hdentry(dinfo
, bindex
);
438 for (; bindex
<= bbot
; bindex
++, p
++) {
442 new_bindex
= au_br_index(sb
, p
->hd_id
);
443 if (new_bindex
== bindex
)
446 if (dinfo
->di_bwh
== bindex
)
448 if (dinfo
->di_bdiropq
== bindex
)
449 bdiropq
= new_bindex
;
450 if (new_bindex
< 0) {
456 /* swap two lower dentries, and loop again */
457 q
= au_hdentry(dinfo
, new_bindex
);
468 if (bwh
>= 0 && bwh
<= au_sbbot(sb
) && au_sbr_whable(sb
, bwh
))
471 dinfo
->di_bdiropq
= -1;
473 && bdiropq
<= au_sbbot(sb
)
474 && au_sbr_whable(sb
, bdiropq
))
475 dinfo
->di_bdiropq
= bdiropq
;
480 bbot
= au_dbbot(parent
);
482 p
= au_hdentry(dinfo
, bindex
);
483 for (; bindex
<= bbot
; bindex
++, p
++)
485 dinfo
->di_btop
= bindex
;
489 if (dinfo
->di_btop
>= 0) {
491 p
= au_hdentry(dinfo
, bindex
);
492 for (; bindex
>= 0; bindex
--, p
--)
494 dinfo
->di_bbot
= bindex
;
503 static void au_do_hide(struct dentry
*dentry
)
507 if (d_really_is_positive(dentry
)) {
508 inode
= d_inode(dentry
);
509 if (!d_is_dir(dentry
)) {
510 if (inode
->i_nlink
&& !d_unhashed(dentry
))
514 /* stop next lookup */
515 inode
->i_flags
|= S_DEAD
;
517 smp_mb(); /* necessary? */
522 static int au_hide_children(struct dentry
*parent
)
524 int err
, i
, j
, ndentry
;
525 struct au_dcsub_pages dpages
;
526 struct au_dpage
*dpage
;
527 struct dentry
*dentry
;
529 err
= au_dpages_init(&dpages
, GFP_NOFS
);
532 err
= au_dcsub_pages(&dpages
, parent
, NULL
, NULL
);
536 /* in reverse order */
537 for (i
= dpages
.ndpage
- 1; i
>= 0; i
--) {
538 dpage
= dpages
.dpages
+ i
;
539 ndentry
= dpage
->ndentry
;
540 for (j
= ndentry
- 1; j
>= 0; j
--) {
541 dentry
= dpage
->dentries
[j
];
542 if (dentry
!= parent
)
548 au_dpages_free(&dpages
);
553 static void au_hide(struct dentry
*dentry
)
558 if (d_is_dir(dentry
)) {
559 /* shrink_dcache_parent(dentry); */
560 err
= au_hide_children(dentry
);
562 AuIOErr("%pd, failed hiding children, ignored %d\n",
569 * By adding a dirty branch, a cached dentry may be affected in various ways.
571 * a dirty branch is added
572 * - on the top of layers
573 * - in the middle of layers
574 * - to the bottom of layers
576 * on the added branch there exists
579 * - a same named entry
581 * * negative --> positive
582 * * positive --> positive
583 * - type is unchanged
586 * * negative --> negative
587 * * positive --> negative (rejected by au_br_del() for non-dir case)
590 static int au_refresh_by_dinfo(struct dentry
*dentry
, struct au_dinfo
*dinfo
,
591 struct au_dinfo
*tmp
)
594 aufs_bindex_t bindex
, bbot
;
596 struct dentry
*dentry
;
602 struct au_hdentry
*hd
;
603 struct inode
*inode
, *h_inode
;
604 struct dentry
*h_dentry
;
607 AuDebugOn(dinfo
->di_btop
< 0);
609 orig_h
.dentry
= au_hdentry(dinfo
, dinfo
->di_btop
)->hd_dentry
;
611 if (d_is_positive(orig_h
.dentry
)) {
612 orig_h
.inode
= d_inode(orig_h
.dentry
);
613 orig_h
.mode
= orig_h
.inode
->i_mode
& S_IFMT
;
615 if (tmp
->di_btop
>= 0) {
616 tmp_h
.dentry
= au_hdentry(tmp
, tmp
->di_btop
)->hd_dentry
;
617 if (d_is_positive(tmp_h
.dentry
)) {
618 tmp_h
.inode
= d_inode(tmp_h
.dentry
);
619 tmp_h
.mode
= tmp_h
.inode
->i_mode
& S_IFMT
;
624 if (d_really_is_positive(dentry
))
625 inode
= d_inode(dentry
);
627 AuDbg("negative originally\n");
633 AuDebugOn(dinfo
->di_btop
!= dinfo
->di_bbot
);
634 AuDebugOn(dinfo
->di_bdiropq
!= -1);
637 AuDbg("negative --> negative\n");
638 /* should have only one negative lower */
639 if (tmp
->di_btop
>= 0
640 && tmp
->di_btop
< dinfo
->di_btop
) {
641 AuDebugOn(tmp
->di_btop
!= tmp
->di_bbot
);
642 AuDebugOn(dinfo
->di_btop
!= dinfo
->di_bbot
);
643 au_set_h_dptr(dentry
, dinfo
->di_btop
, NULL
);
644 au_di_cp(dinfo
, tmp
);
645 hd
= au_hdentry(tmp
, tmp
->di_btop
);
646 au_set_h_dptr(dentry
, tmp
->di_btop
,
647 dget(hd
->hd_dentry
));
649 au_dbg_verify_dinode(dentry
);
651 AuDbg("negative --> positive\n");
653 * similar to the behaviour of creating with bypassing
655 * unhash it in order to force an error in the
656 * succeeding create operation.
657 * we should not set S_DEAD here.
660 /* au_di_swap(tmp, dinfo); */
661 au_dbg_verify_dinode(dentry
);
664 AuDbg("positive originally\n");
665 /* inode may be NULL */
666 AuDebugOn(inode
&& (inode
->i_mode
& S_IFMT
) != orig_h
.mode
);
668 AuDbg("positive --> negative\n");
669 /* or bypassing aufs */
671 if (tmp
->di_bwh
>= 0 && tmp
->di_bwh
<= dinfo
->di_btop
)
672 dinfo
->di_bwh
= tmp
->di_bwh
;
674 err
= au_refresh_hinode_self(inode
);
675 au_dbg_verify_dinode(dentry
);
676 } else if (orig_h
.mode
== tmp_h
.mode
) {
677 AuDbg("positive --> positive, same type\n");
678 if (!S_ISDIR(orig_h
.mode
)
679 && dinfo
->di_btop
> tmp
->di_btop
) {
681 * similar to the behaviour of removing and
686 err
= au_refresh_hinode_self(inode
);
687 au_dbg_verify_dinode(dentry
);
689 /* fill empty slots */
690 if (dinfo
->di_btop
> tmp
->di_btop
)
691 dinfo
->di_btop
= tmp
->di_btop
;
692 if (dinfo
->di_bbot
< tmp
->di_bbot
)
693 dinfo
->di_bbot
= tmp
->di_bbot
;
694 dinfo
->di_bwh
= tmp
->di_bwh
;
695 dinfo
->di_bdiropq
= tmp
->di_bdiropq
;
696 bbot
= dinfo
->di_bbot
;
697 bindex
= tmp
->di_btop
;
698 hd
= au_hdentry(tmp
, bindex
);
699 for (; bindex
<= bbot
; bindex
++, hd
++) {
700 if (au_h_dptr(dentry
, bindex
))
702 h_dentry
= hd
->hd_dentry
;
705 AuDebugOn(d_is_negative(h_dentry
));
706 h_inode
= d_inode(h_dentry
);
707 AuDebugOn(orig_h
.mode
710 au_set_h_dptr(dentry
, bindex
,
714 err
= au_refresh_hinode(inode
, dentry
);
715 au_dbg_verify_dinode(dentry
);
718 AuDbg("positive --> positive, different type\n");
719 /* similar to the behaviour of removing and creating */
722 err
= au_refresh_hinode_self(inode
);
723 au_dbg_verify_dinode(dentry
);
731 void au_refresh_dop(struct dentry
*dentry
, int force_reval
)
733 const struct dentry_operations
*dop
734 = force_reval
? &aufs_dop
: dentry
->d_sb
->s_d_op
;
735 static const unsigned int mask
736 = DCACHE_OP_REVALIDATE
| DCACHE_OP_WEAK_REVALIDATE
;
738 BUILD_BUG_ON(sizeof(mask
) != sizeof(dentry
->d_flags
));
740 if (dentry
->d_op
== dop
)
743 AuDbg("%pd\n", dentry
);
744 spin_lock(&dentry
->d_lock
);
745 if (dop
== &aufs_dop
)
746 dentry
->d_flags
|= mask
;
748 dentry
->d_flags
&= ~mask
;
750 spin_unlock(&dentry
->d_lock
);
753 int au_refresh_dentry(struct dentry
*dentry
, struct dentry
*parent
)
755 int err
, ebrange
, nbr
;
757 struct au_dinfo
*dinfo
, *tmp
;
758 struct super_block
*sb
;
761 DiMustWriteLock(dentry
);
762 AuDebugOn(IS_ROOT(dentry
));
763 AuDebugOn(d_really_is_negative(parent
));
766 sigen
= au_sigen(sb
);
767 err
= au_digen_test(parent
, sigen
);
771 nbr
= au_sbbot(sb
) + 1;
772 dinfo
= au_di(dentry
);
773 err
= au_di_realloc(dinfo
, nbr
, /*may_shrink*/0);
776 ebrange
= au_dbrange_test(dentry
);
778 ebrange
= au_do_refresh_hdentry(dentry
, parent
);
780 if (d_unhashed(dentry
) || ebrange
/* || dinfo->di_tmpfile */) {
781 AuDebugOn(au_dbtop(dentry
) < 0 && au_dbbot(dentry
) >= 0);
782 if (d_really_is_positive(dentry
)) {
783 inode
= d_inode(dentry
);
784 err
= au_refresh_hinode_self(inode
);
786 au_dbg_verify_dinode(dentry
);
788 goto out_dgen
; /* success */
792 /* temporary dinfo */
795 tmp
= au_di_alloc(sb
, AuLsc_DI_TMP
);
798 au_di_swap(tmp
, dinfo
);
799 /* returns the number of positive dentries */
801 * if current working dir is removed, it returns an error.
802 * but the dentry is legal.
804 err
= au_lkup_dentry(dentry
, /*btop*/0, AuLkup_ALLOW_NEG
);
806 au_di_swap(tmp
, dinfo
);
810 /* compare/refresh by dinfo */
812 err
= au_refresh_by_dinfo(dentry
, dinfo
, tmp
);
813 au_dbg_verify_dinode(dentry
);
816 au_di_realloc(dinfo
, nbr
, /*may_shrink*/1); /* harmless if err */
817 au_rw_write_unlock(&tmp
->di_rwsem
);
823 au_update_digen(dentry
);
825 if (unlikely(err
&& !(dentry
->d_flags
& DCACHE_NFSFS_RENAMED
))) {
826 AuIOErr("failed refreshing %pd, %d\n", dentry
, err
);
833 static int au_do_h_d_reval(struct dentry
*h_dentry
, unsigned int flags
,
834 struct dentry
*dentry
, aufs_bindex_t bindex
)
839 if (!(h_dentry
->d_flags
& DCACHE_OP_REVALIDATE
))
842 AuDbg("b%d\n", bindex
);
844 * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
845 * due to whiteout and branch permission.
847 flags
&= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN
| LOOKUP_CREATE
848 | LOOKUP_FOLLOW
| LOOKUP_EXCL
);
849 /* it may return tri-state */
850 valid
= h_dentry
->d_op
->d_revalidate(h_dentry
, flags
);
852 if (unlikely(valid
< 0))
862 /* todo: remove this */
863 static int h_d_revalidate(struct dentry
*dentry
, struct inode
*inode
,
864 unsigned int flags
, int do_udba
, int dirren
)
867 umode_t mode
, h_mode
;
868 aufs_bindex_t bindex
, btail
, btop
, ibs
, ibe
;
869 unsigned char plus
, unhashed
, is_root
, h_plus
, h_nfs
, tmpfile
;
870 struct inode
*h_inode
, *h_cached_inode
;
871 struct dentry
*h_dentry
;
872 struct qstr
*name
, *h_name
;
879 unhashed
= !!d_unhashed(dentry
);
880 is_root
= !!IS_ROOT(dentry
);
881 name
= &dentry
->d_name
;
882 tmpfile
= au_di(dentry
)->di_tmpfile
;
885 * Theoretically, REVAL test should be unnecessary in case of
887 * But {fs,i}notify doesn't fire some necessary events,
888 * IN_ATTRIB for atime/nlink/pageio
889 * Let's do REVAL test too.
891 if (do_udba
&& inode
) {
892 mode
= (inode
->i_mode
& S_IFMT
);
893 plus
= (inode
->i_nlink
> 0);
894 ibs
= au_ibtop(inode
);
895 ibe
= au_ibbot(inode
);
898 btop
= au_dbtop(dentry
);
900 if (inode
&& S_ISDIR(inode
->i_mode
))
901 btail
= au_dbtaildir(dentry
);
902 for (bindex
= btop
; bindex
<= btail
; bindex
++) {
903 h_dentry
= au_h_dptr(dentry
, bindex
);
907 AuDbg("b%d, %pd\n", bindex
, h_dentry
);
908 h_nfs
= !!au_test_nfs(h_dentry
->d_sb
);
909 spin_lock(&h_dentry
->d_lock
);
910 h_name
= &h_dentry
->d_name
;
914 && (unhashed
!= !!d_unhashed(h_dentry
)
915 || (!tmpfile
&& !dirren
916 && !au_qstreq(name
, h_name
))
919 && !(flags
& LOOKUP_OPEN
)
920 && (h_dentry
->d_flags
921 & DCACHE_NFSFS_RENAMED
)))
925 h_unhashed
= d_unhashed(h_dentry
);
926 spin_unlock(&h_dentry
->d_lock
);
927 AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
928 unhashed
, h_unhashed
, dentry
, h_dentry
);
931 spin_unlock(&h_dentry
->d_lock
);
933 err
= au_do_h_d_reval(h_dentry
, flags
, dentry
, bindex
);
935 /* do not goto err, to keep the errno */
938 /* todo: plink too? */
943 if (unlikely(!!inode
!= d_is_positive(h_dentry
)))
947 if (d_is_positive(h_dentry
))
948 h_inode
= d_inode(h_dentry
);
951 h_cached_inode
= h_inode
;
953 h_mode
= (h_inode
->i_mode
& S_IFMT
);
954 h_plus
= (h_inode
->i_nlink
> 0);
956 if (inode
&& ibs
<= bindex
&& bindex
<= ibe
)
957 h_cached_inode
= au_h_iptr(inode
, bindex
);
960 if (unlikely(plus
!= h_plus
&& !tmpfile
))
963 if (unlikely(!(h_dentry
->d_flags
& DCACHE_NFSFS_RENAMED
)
965 && !IS_ROOT(h_dentry
)
966 && unhashed
!= d_unhashed(h_dentry
)))
969 if (unlikely(mode
!= h_mode
970 || h_cached_inode
!= h_inode
))
983 /* todo: consolidate with do_refresh() and au_reval_for_attr() */
984 static int simple_reval_dpath(struct dentry
*dentry
, unsigned int sigen
)
987 struct dentry
*parent
;
989 if (!au_digen_test(dentry
, sigen
))
992 parent
= dget_parent(dentry
);
993 di_read_lock_parent(parent
, AuLock_IR
);
994 AuDebugOn(au_digen_test(parent
, sigen
));
995 au_dbg_verify_gen(parent
, sigen
);
996 err
= au_refresh_dentry(dentry
, parent
);
997 di_read_unlock(parent
, AuLock_IR
);
1003 int au_reval_dpath(struct dentry
*dentry
, unsigned int sigen
)
1006 struct dentry
*d
, *parent
;
1008 if (!au_ftest_si(au_sbi(dentry
->d_sb
), FAILED_REFRESH_DIR
))
1009 return simple_reval_dpath(dentry
, sigen
);
1011 /* slow loop, keep it simple and stupid */
1012 /* cf: au_cpup_dirs() */
1015 while (au_digen_test(dentry
, sigen
)) {
1019 parent
= dget_parent(d
);
1020 if (!au_digen_test(parent
, sigen
))
1026 di_write_lock_child2(d
);
1028 /* someone might update our dentry while we were sleeping */
1029 if (au_digen_test(d
, sigen
)) {
1031 * todo: consolidate with simple_reval_dpath(),
1032 * do_refresh() and au_reval_for_attr().
1034 di_read_lock_parent(parent
, AuLock_IR
);
1035 err
= au_refresh_dentry(d
, parent
);
1036 di_read_unlock(parent
, AuLock_IR
);
1050 * if valid returns 1, otherwise 0.
1052 static int aufs_d_revalidate(struct dentry
*dentry
, unsigned int flags
)
1056 unsigned char do_udba
, dirren
;
1057 struct super_block
*sb
;
1058 struct inode
*inode
;
1060 /* todo: support rcu-walk? */
1061 if (flags
& LOOKUP_RCU
)
1065 if (unlikely(!au_di(dentry
)))
1072 * i_mutex of parent dir may be held,
1073 * but we should not return 'invalid' due to busy.
1075 err
= aufs_read_lock(dentry
, AuLock_FLUSH
| AuLock_DW
| AuLock_NOPLM
);
1076 if (unlikely(err
)) {
1082 if (d_really_is_positive(dentry
))
1083 inode
= d_inode(dentry
);
1084 if (unlikely(inode
&& au_is_bad_inode(inode
))) {
1089 if (unlikely(au_dbrange_test(dentry
))) {
1095 sigen
= au_sigen(sb
);
1096 if (au_digen_test(dentry
, sigen
)) {
1097 AuDebugOn(IS_ROOT(dentry
));
1098 err
= au_reval_dpath(dentry
, sigen
);
1099 if (unlikely(err
)) {
1104 di_downgrade_lock(dentry
, AuLock_IR
);
1107 if (!(flags
& (LOOKUP_OPEN
| LOOKUP_EMPTY
))
1109 && !(inode
->i_state
&& I_LINKABLE
)
1110 && (IS_DEADDIR(inode
) || !inode
->i_nlink
)) {
1115 do_udba
= !au_opt_test(au_mntflags(sb
), UDBA_NONE
);
1116 if (do_udba
&& inode
) {
1117 aufs_bindex_t btop
= au_ibtop(inode
);
1118 struct inode
*h_inode
;
1121 h_inode
= au_h_iptr(inode
, btop
);
1122 if (h_inode
&& au_test_higen(inode
, h_inode
)) {
1129 dirren
= !!au_opt_test(au_mntflags(sb
), DIRREN
);
1130 err
= h_d_revalidate(dentry
, inode
, flags
, do_udba
, dirren
);
1131 if (unlikely(!err
&& do_udba
&& au_dbtop(dentry
) < 0)) {
1133 AuDbg("both of real entry and whiteout found, %p, err %d\n",
1139 di_downgrade_lock(dentry
, AuLock_IR
);
1141 aufs_read_unlock(dentry
, AuLock_IR
);
1146 AuDbg("%pd invalid, %d\n", dentry
, valid
);
1152 static void aufs_d_release(struct dentry
*dentry
)
1154 if (au_di(dentry
)) {
1156 au_hn_di_reinit(dentry
);
1160 const struct dentry_operations aufs_dop
= {
1161 .d_revalidate
= aufs_d_revalidate
,
1162 .d_weak_revalidate
= aufs_d_revalidate
,
1163 .d_release
= aufs_d_release
1166 /* aufs_dop without d_revalidate */
1167 const struct dentry_operations aufs_dop_noreval
= {
1168 .d_release
= aufs_d_release