1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2005-2019 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 * inode operation (rename entry)
21 * todo: this is crazy monster
24 #include <linux/iversion.h>
27 enum { AuSRC
, AuDST
, AuSrcDst
};
28 enum { AuPARENT
, AuCHILD
, AuParentChild
};
30 #define AuRen_ISDIR_SRC 1
31 #define AuRen_ISDIR_DST (1 << 1)
32 #define AuRen_ISSAMEDIR (1 << 2)
33 #define AuRen_WHSRC (1 << 3)
34 #define AuRen_WHDST (1 << 4)
35 #define AuRen_MNT_WRITE (1 << 5)
36 #define AuRen_DT_DSTDIR (1 << 6)
37 #define AuRen_DIROPQ_SRC (1 << 7)
38 #define AuRen_DIROPQ_DST (1 << 8)
39 #define AuRen_DIRREN (1 << 9)
40 #define AuRen_DROPPED_SRC (1 << 10)
41 #define AuRen_DROPPED_DST (1 << 11)
42 #define au_ftest_ren(flags, name) ((flags) & AuRen_##name)
43 #define au_fset_ren(flags, name) \
44 do { (flags) |= AuRen_##name; } while (0)
45 #define au_fclr_ren(flags, name) \
46 do { (flags) &= ~AuRen_##name; } while (0)
48 #ifndef CONFIG_AUFS_DIRREN
50 #define AuRen_DIRREN 0
55 struct dentry
*dentry
, *h_dentry
, *parent
, *h_parent
,
57 struct inode
*dir
, *inode
;
58 struct au_hinode
*hdir
, *hinode
;
59 struct au_dtime dt
[AuParentChild
];
60 aufs_bindex_t btop
, bdiropq
;
63 #define src_dentry sd[AuSRC].dentry
64 #define src_dir sd[AuSRC].dir
65 #define src_inode sd[AuSRC].inode
66 #define src_h_dentry sd[AuSRC].h_dentry
67 #define src_parent sd[AuSRC].parent
68 #define src_h_parent sd[AuSRC].h_parent
69 #define src_wh_dentry sd[AuSRC].wh_dentry
70 #define src_hdir sd[AuSRC].hdir
71 #define src_hinode sd[AuSRC].hinode
72 #define src_h_dir sd[AuSRC].hdir->hi_inode
73 #define src_dt sd[AuSRC].dt
74 #define src_btop sd[AuSRC].btop
75 #define src_bdiropq sd[AuSRC].bdiropq
77 #define dst_dentry sd[AuDST].dentry
78 #define dst_dir sd[AuDST].dir
79 #define dst_inode sd[AuDST].inode
80 #define dst_h_dentry sd[AuDST].h_dentry
81 #define dst_parent sd[AuDST].parent
82 #define dst_h_parent sd[AuDST].h_parent
83 #define dst_wh_dentry sd[AuDST].wh_dentry
84 #define dst_hdir sd[AuDST].hdir
85 #define dst_hinode sd[AuDST].hinode
86 #define dst_h_dir sd[AuDST].hdir->hi_inode
87 #define dst_dt sd[AuDST].dt
88 #define dst_btop sd[AuDST].btop
89 #define dst_bdiropq sd[AuDST].bdiropq
91 struct dentry
*h_trap
;
94 struct au_nhash whlist
;
95 aufs_bindex_t btgt
, src_bwh
;
98 unsigned short auren_flags
;
99 unsigned char flags
; /* syscall parameter */
100 unsigned char exchange
;
103 struct au_whtmp_rmdir
*thargs
;
104 struct dentry
*h_dst
;
105 struct au_hinode
*h_root
;
108 /* ---------------------------------------------------------------------- */
111 * functions for reverting.
112 * when an error happened in a single rename systemcall, we should revert
113 * everything as if nothing happened.
114 * we don't need to revert the copied-up/down the parent dir since they are
118 #define RevertFailure(fmt, ...) do { \
119 AuIOErr("revert failure: " fmt " (%d, %d)\n", \
120 ##__VA_ARGS__, err, rerr); \
124 static void au_ren_do_rev_diropq(int err
, struct au_ren_args
*a
, int idx
)
128 #define src_or_dst(member) a->sd[idx].member
130 d
= src_or_dst(dentry
); /* {src,dst}_dentry */
131 au_hn_inode_lock_nested(src_or_dst(hinode
), AuLsc_I_CHILD
);
132 rerr
= au_diropq_remove(d
, a
->btgt
);
133 au_hn_inode_unlock(src_or_dst(hinode
));
134 au_set_dbdiropq(d
, src_or_dst(bdiropq
));
136 RevertFailure("remove diropq %pd", d
);
141 static void au_ren_rev_diropq(int err
, struct au_ren_args
*a
)
143 if (au_ftest_ren(a
->auren_flags
, DIROPQ_SRC
))
144 au_ren_do_rev_diropq(err
, a
, AuSRC
);
145 if (au_ftest_ren(a
->auren_flags
, DIROPQ_DST
))
146 au_ren_do_rev_diropq(err
, a
, AuDST
);
149 static void au_ren_rev_rename(int err
, struct au_ren_args
*a
)
152 struct inode
*delegated
;
154 a
->h_path
.dentry
= vfsub_lkup_one(&a
->src_dentry
->d_name
,
156 rerr
= PTR_ERR(a
->h_path
.dentry
);
157 if (IS_ERR(a
->h_path
.dentry
)) {
158 RevertFailure("lkup one %pd", a
->src_dentry
);
163 rerr
= vfsub_rename(a
->dst_h_dir
,
164 au_h_dptr(a
->src_dentry
, a
->btgt
),
165 a
->src_h_dir
, &a
->h_path
, &delegated
, a
->flags
);
166 if (unlikely(rerr
== -EWOULDBLOCK
)) {
167 pr_warn("cannot retry for NFSv4 delegation"
168 " for an internal rename\n");
171 d_drop(a
->h_path
.dentry
);
172 dput(a
->h_path
.dentry
);
173 /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
175 RevertFailure("rename %pd", a
->src_dentry
);
178 static void au_ren_rev_whtmp(int err
, struct au_ren_args
*a
)
181 struct inode
*delegated
;
183 a
->h_path
.dentry
= vfsub_lkup_one(&a
->dst_dentry
->d_name
,
185 rerr
= PTR_ERR(a
->h_path
.dentry
);
186 if (IS_ERR(a
->h_path
.dentry
)) {
187 RevertFailure("lkup one %pd", a
->dst_dentry
);
190 if (d_is_positive(a
->h_path
.dentry
)) {
191 d_drop(a
->h_path
.dentry
);
192 dput(a
->h_path
.dentry
);
197 rerr
= vfsub_rename(a
->dst_h_dir
, a
->h_dst
, a
->dst_h_dir
, &a
->h_path
,
198 &delegated
, a
->flags
);
199 if (unlikely(rerr
== -EWOULDBLOCK
)) {
200 pr_warn("cannot retry for NFSv4 delegation"
201 " for an internal rename\n");
204 d_drop(a
->h_path
.dentry
);
205 dput(a
->h_path
.dentry
);
207 au_set_h_dptr(a
->dst_dentry
, a
->btgt
, dget(a
->h_dst
));
209 RevertFailure("rename %pd", a
->h_dst
);
212 static void au_ren_rev_whsrc(int err
, struct au_ren_args
*a
)
216 a
->h_path
.dentry
= a
->src_wh_dentry
;
217 rerr
= au_wh_unlink_dentry(a
->src_h_dir
, &a
->h_path
, a
->src_dentry
);
218 au_set_dbwh(a
->src_dentry
, a
->src_bwh
);
220 RevertFailure("unlink %pd", a
->src_wh_dentry
);
224 /* ---------------------------------------------------------------------- */
227 * when we have to copyup the renaming entry, do it with the rename-target name
228 * in order to minimize the cost (the later actual rename is unnecessary).
229 * otherwise rename it on the target branch.
231 static int au_ren_or_cpup(struct au_ren_args
*a
)
235 struct inode
*delegated
;
238 if (au_dbtop(d
) == a
->btgt
) {
239 a
->h_path
.dentry
= a
->dst_h_dentry
;
240 AuDebugOn(au_dbtop(d
) != a
->btgt
);
242 err
= vfsub_rename(a
->src_h_dir
, au_h_dptr(d
, a
->btgt
),
243 a
->dst_h_dir
, &a
->h_path
, &delegated
,
245 if (unlikely(err
== -EWOULDBLOCK
)) {
246 pr_warn("cannot retry for NFSv4 delegation"
247 " for an internal rename\n");
253 if (!err
&& a
->h_dst
)
254 /* it will be set to dinfo later */
260 /* cf. aufs_rmdir() */
261 static int au_ren_del_whtmp(struct au_ren_args
*a
)
267 SiMustAnyLock(dir
->i_sb
);
268 if (!au_nhash_test_longer_wh(&a
->whlist
, a
->btgt
,
269 au_sbi(dir
->i_sb
)->si_dirwh
)
270 || au_test_fs_remote(a
->h_dst
->d_sb
)) {
271 err
= au_whtmp_rmdir(dir
, a
->btgt
, a
->h_dst
, &a
->whlist
);
273 pr_warn("failed removing whtmp dir %pd (%d), "
274 "ignored.\n", a
->h_dst
, err
);
276 au_nhash_wh_free(&a
->thargs
->whlist
);
277 a
->thargs
->whlist
= a
->whlist
;
278 a
->whlist
.nh_num
= 0;
279 au_whtmp_kick_rmdir(dir
, a
->btgt
, a
->h_dst
, a
->thargs
);
287 /* make it 'opaque' dir. */
288 static int au_ren_do_diropq(struct au_ren_args
*a
, int idx
)
291 struct dentry
*d
, *diropq
;
292 #define src_or_dst(member) a->sd[idx].member
295 d
= src_or_dst(dentry
); /* {src,dst}_dentry */
296 src_or_dst(bdiropq
) = au_dbdiropq(d
);
297 src_or_dst(hinode
) = au_hi(src_or_dst(inode
), a
->btgt
);
298 au_hn_inode_lock_nested(src_or_dst(hinode
), AuLsc_I_CHILD
);
299 diropq
= au_diropq_create(d
, a
->btgt
);
300 au_hn_inode_unlock(src_or_dst(hinode
));
302 err
= PTR_ERR(diropq
);
310 static int au_ren_diropq(struct au_ren_args
*a
)
313 unsigned char always
;
317 d
= a
->dst_dentry
; /* already renamed on the branch */
318 always
= !!au_opt_test(au_mntflags(d
->d_sb
), ALWAYS_DIROPQ
);
319 if (au_ftest_ren(a
->auren_flags
, ISDIR_SRC
)
320 && !au_ftest_ren(a
->auren_flags
, DIRREN
)
321 && a
->btgt
!= au_dbdiropq(a
->src_dentry
)
323 || a
->btgt
<= au_dbdiropq(d
)
324 /* hide the lower to keep xino */
325 /* the lowers may not be a dir, but we hide them anyway */
326 || a
->btgt
< au_dbbot(d
)
329 err
= au_ren_do_diropq(a
, AuSRC
);
332 au_fset_ren(a
->auren_flags
, DIROPQ_SRC
);
335 goto out
; /* success */
337 d
= a
->src_dentry
; /* already renamed on the branch */
338 if (au_ftest_ren(a
->auren_flags
, ISDIR_DST
)
339 && a
->btgt
!= au_dbdiropq(a
->dst_dentry
)
340 && (a
->btgt
< au_dbdiropq(d
)
341 || a
->btgt
< au_dbbot(d
)
343 AuDbgDentry(a
->src_dentry
);
344 AuDbgDentry(a
->dst_dentry
);
345 err
= au_ren_do_diropq(a
, AuDST
);
348 au_fset_ren(a
->auren_flags
, DIROPQ_DST
);
350 goto out
; /* success */
353 AuDbg("err %d, reverting src\n", err
);
354 au_ren_rev_diropq(err
, a
);
359 static int do_rename(struct au_ren_args
*a
)
362 struct dentry
*d
, *h_d
;
365 /* prepare workqueue args for asynchronous rmdir */
366 h_d
= a
->dst_h_dentry
;
367 if (au_ftest_ren(a
->auren_flags
, ISDIR_DST
)
368 /* && !au_ftest_ren(a->auren_flags, DIRREN) */
369 && d_is_positive(h_d
)) {
371 a
->thargs
= au_whtmp_rmdir_alloc(a
->src_dentry
->d_sb
,
373 if (unlikely(!a
->thargs
))
375 a
->h_dst
= dget(h_d
);
378 /* create whiteout for src_dentry */
379 if (au_ftest_ren(a
->auren_flags
, WHSRC
)) {
380 a
->src_bwh
= au_dbwh(a
->src_dentry
);
381 AuDebugOn(a
->src_bwh
>= 0);
382 a
->src_wh_dentry
= au_wh_create(a
->src_dentry
, a
->btgt
,
384 err
= PTR_ERR(a
->src_wh_dentry
);
385 if (IS_ERR(a
->src_wh_dentry
))
389 /* lookup whiteout for dentry */
390 if (au_ftest_ren(a
->auren_flags
, WHDST
)) {
391 h_d
= au_wh_lkup(a
->dst_h_parent
,
392 &a
->dst_dentry
->d_name
, a
->br
);
396 if (d_is_negative(h_d
))
399 a
->dst_wh_dentry
= h_d
;
402 /* rename dentry to tmpwh */
404 err
= au_whtmp_ren(a
->dst_h_dentry
, a
->br
);
409 au_set_h_dptr(d
, a
->btgt
, NULL
);
410 err
= au_lkup_neg(d
, a
->btgt
, /*wh*/0);
413 a
->dst_h_dentry
= au_h_dptr(d
, a
->btgt
);
417 BUG_ON(d_is_positive(a
->dst_h_dentry
) && a
->src_btop
!= a
->btgt
);
419 BUG_ON(!au_ftest_ren(a
->auren_flags
, DIRREN
)
420 && d_is_positive(a
->dst_h_dentry
)
421 && a
->src_btop
!= a
->btgt
);
424 /* rename by vfs_rename or cpup */
425 err
= au_ren_or_cpup(a
);
427 /* leave the copied-up one */
430 /* make dir opaque */
431 err
= au_ren_diropq(a
);
435 /* update target timestamps */
437 AuDebugOn(au_dbtop(a
->dst_dentry
) != a
->btgt
);
438 a
->h_path
.dentry
= au_h_dptr(a
->dst_dentry
, a
->btgt
);
439 vfsub_update_h_iattr(&a
->h_path
, /*did*/NULL
); /*ignore*/
440 a
->dst_inode
->i_ctime
= d_inode(a
->h_path
.dentry
)->i_ctime
;
442 AuDebugOn(au_dbtop(a
->src_dentry
) != a
->btgt
);
443 a
->h_path
.dentry
= au_h_dptr(a
->src_dentry
, a
->btgt
);
444 vfsub_update_h_iattr(&a
->h_path
, /*did*/NULL
); /*ignore*/
445 a
->src_inode
->i_ctime
= d_inode(a
->h_path
.dentry
)->i_ctime
;
448 /* remove whiteout for dentry */
449 if (a
->dst_wh_dentry
) {
450 a
->h_path
.dentry
= a
->dst_wh_dentry
;
451 err
= au_wh_unlink_dentry(a
->dst_h_dir
, &a
->h_path
,
459 au_ren_del_whtmp(a
); /* ignore this error */
461 au_fhsm_wrote(a
->src_dentry
->d_sb
, a
->btgt
, /*force*/0);
467 au_ren_rev_diropq(err
, a
);
469 au_ren_rev_rename(err
, a
);
473 au_ren_rev_whtmp(err
, a
);
475 dput(a
->dst_wh_dentry
);
476 a
->dst_wh_dentry
= NULL
;
478 if (a
->src_wh_dentry
)
479 au_ren_rev_whsrc(err
, a
);
481 dput(a
->src_wh_dentry
);
482 dput(a
->dst_wh_dentry
);
486 au_whtmp_rmdir_free(a
->thargs
);
493 /* ---------------------------------------------------------------------- */
496 * test if @dentry dir can be rename destination or not.
497 * success means, it is a logically empty dir.
499 static int may_rename_dstdir(struct dentry
*dentry
, struct au_nhash
*whlist
)
501 return au_test_empty(dentry
, whlist
);
505 * test if @a->src_dentry dir can be rename source or not.
506 * if it can, return 0.
508 * - it is a logically empty dir.
509 * - or, it exists on writable branch and has no children including whiteouts
510 * on the lower branch unless DIRREN is on.
512 static int may_rename_srcdir(struct au_ren_args
*a
)
516 aufs_bindex_t btop
, btgt
;
517 struct dentry
*dentry
;
518 struct super_block
*sb
;
519 struct au_sbinfo
*sbinfo
;
521 dentry
= a
->src_dentry
;
524 if (au_opt_test(sbinfo
->si_mntflags
, DIRREN
))
525 au_fset_ren(a
->auren_flags
, DIRREN
);
528 btop
= au_dbtop(dentry
);
530 struct au_nhash whlist
;
533 rdhash
= sbinfo
->si_rdhash
;
535 rdhash
= au_rdhash_est(au_dir_size(/*file*/NULL
,
537 err
= au_nhash_alloc(&whlist
, rdhash
, GFP_NOFS
);
540 err
= au_test_empty(dentry
, &whlist
);
541 au_nhash_wh_free(&whlist
);
545 if (btop
== au_dbtaildir(dentry
))
546 return 0; /* success */
548 err
= au_test_empty_lower(dentry
);
551 if (err
== -ENOTEMPTY
) {
552 if (au_ftest_ren(a
->auren_flags
, DIRREN
)) {
555 AuWarn1("renaming dir who has child(ren) on multiple "
556 "branches, is not supported\n");
563 /* side effect: sets whlist and h_dentry */
564 static int au_ren_may_dir(struct au_ren_args
*a
)
571 SiMustAnyLock(d
->d_sb
);
574 if (au_ftest_ren(a
->auren_flags
, ISDIR_DST
) && a
->dst_inode
) {
575 rdhash
= au_sbi(d
->d_sb
)->si_rdhash
;
577 rdhash
= au_rdhash_est(au_dir_size(/*file*/NULL
, d
));
578 err
= au_nhash_alloc(&a
->whlist
, rdhash
, GFP_NOFS
);
583 au_set_dbtop(d
, a
->dst_btop
);
584 err
= may_rename_dstdir(d
, &a
->whlist
);
585 au_set_dbtop(d
, a
->btgt
);
587 err
= may_rename_srcdir(a
);
589 a
->dst_h_dentry
= au_h_dptr(d
, au_dbtop(d
));
594 a
->src_h_dentry
= au_h_dptr(d
, au_dbtop(d
));
595 if (au_ftest_ren(a
->auren_flags
, ISDIR_SRC
)) {
596 err
= may_rename_srcdir(a
);
598 au_nhash_wh_free(&a
->whlist
);
599 a
->whlist
.nh_num
= 0;
606 /* ---------------------------------------------------------------------- */
609 * simple tests for rename.
610 * following the checks in vfs, plus the parent-child relationship.
612 static int au_may_ren(struct au_ren_args
*a
)
615 struct inode
*h_inode
;
617 if (a
->src_btop
== a
->btgt
) {
618 err
= au_may_del(a
->src_dentry
, a
->btgt
, a
->src_h_parent
,
619 au_ftest_ren(a
->auren_flags
, ISDIR_SRC
));
623 if (unlikely(a
->src_h_dentry
== a
->h_trap
))
628 if (a
->dst_btop
!= a
->btgt
)
632 if (unlikely(a
->dst_h_dentry
== a
->h_trap
))
636 isdir
= !!au_ftest_ren(a
->auren_flags
, ISDIR_DST
);
637 if (d_really_is_negative(a
->dst_dentry
)) {
638 if (d_is_negative(a
->dst_h_dentry
))
639 err
= au_may_add(a
->dst_dentry
, a
->btgt
,
640 a
->dst_h_parent
, isdir
);
642 if (unlikely(d_is_negative(a
->dst_h_dentry
)))
644 h_inode
= d_inode(a
->dst_h_dentry
);
645 if (h_inode
->i_nlink
)
646 err
= au_may_del(a
->dst_dentry
, a
->btgt
,
647 a
->dst_h_parent
, isdir
);
651 if (unlikely(err
== -ENOENT
|| err
== -EEXIST
))
657 /* ---------------------------------------------------------------------- */
662 * - src_dir and dir by lock_rename()
666 * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
668 * + di_write_lock2_child()
669 * + di_write_lock_child()
670 * + ii_write_lock_child()
671 * + di_write_lock_child2()
672 * + ii_write_lock_child2()
673 * + src_parent and parent
674 * + di_write_lock_parent()
675 * + ii_write_lock_parent()
676 * + di_write_lock_parent2()
677 * + ii_write_lock_parent2()
678 * + lower src_dir and dir by vfsub_lock_rename()
679 * + verify the every relationships between child and parent. if any
680 * of them failed, unlock all and return -EBUSY.
682 static void au_ren_unlock(struct au_ren_args
*a
)
684 vfsub_unlock_rename(a
->src_h_parent
, a
->src_hdir
,
685 a
->dst_h_parent
, a
->dst_hdir
);
686 if (au_ftest_ren(a
->auren_flags
, DIRREN
)
688 au_hn_inode_unlock(a
->h_root
);
689 if (au_ftest_ren(a
->auren_flags
, MNT_WRITE
))
690 vfsub_mnt_drop_write(au_br_mnt(a
->br
));
693 static int au_ren_lock(struct au_ren_args
*a
)
699 a
->src_h_parent
= au_h_dptr(a
->src_parent
, a
->btgt
);
700 a
->src_hdir
= au_hi(a
->src_dir
, a
->btgt
);
701 a
->dst_h_parent
= au_h_dptr(a
->dst_parent
, a
->btgt
);
702 a
->dst_hdir
= au_hi(a
->dst_dir
, a
->btgt
);
704 err
= vfsub_mnt_want_write(au_br_mnt(a
->br
));
707 au_fset_ren(a
->auren_flags
, MNT_WRITE
);
708 if (au_ftest_ren(a
->auren_flags
, DIRREN
)) {
713 * sbinfo is already locked, so this ii_read_lock is
714 * unnecessary. but our debugging feature checks it.
716 root
= a
->src_inode
->i_sb
->s_root
;
717 if (root
!= a
->src_parent
&& root
!= a
->dst_parent
) {
719 ii_read_lock_parent3(dir
);
720 a
->h_root
= au_hi(dir
, a
->btgt
);
722 au_hn_inode_lock_nested(a
->h_root
, AuLsc_I_PARENT3
);
725 a
->h_trap
= vfsub_lock_rename(a
->src_h_parent
, a
->src_hdir
,
726 a
->dst_h_parent
, a
->dst_hdir
);
727 udba
= au_opt_udba(a
->src_dentry
->d_sb
);
728 if (unlikely(a
->src_hdir
->hi_inode
!= d_inode(a
->src_h_parent
)
729 || a
->dst_hdir
->hi_inode
!= d_inode(a
->dst_h_parent
)))
730 err
= au_busy_or_stale();
731 if (!err
&& au_dbtop(a
->src_dentry
) == a
->btgt
)
732 err
= au_h_verify(a
->src_h_dentry
, udba
,
733 d_inode(a
->src_h_parent
), a
->src_h_parent
,
735 if (!err
&& au_dbtop(a
->dst_dentry
) == a
->btgt
)
736 err
= au_h_verify(a
->dst_h_dentry
, udba
,
737 d_inode(a
->dst_h_parent
), a
->dst_h_parent
,
740 goto out
; /* success */
742 err
= au_busy_or_stale();
749 /* ---------------------------------------------------------------------- */
751 static void au_ren_refresh_dir(struct au_ren_args
*a
)
756 inode_inc_iversion(dir
);
757 if (au_ftest_ren(a
->auren_flags
, ISDIR_SRC
)) {
758 /* is this updating defined in POSIX? */
759 au_cpup_attr_timesizes(a
->src_inode
);
760 au_cpup_attr_nlink(dir
, /*force*/1);
762 au_dir_ts(dir
, a
->btgt
);
766 inode_inc_iversion(dir
);
767 if (au_ftest_ren(a
->auren_flags
, ISDIR_DST
)) {
768 /* is this updating defined in POSIX? */
769 au_cpup_attr_timesizes(a
->dst_inode
);
770 au_cpup_attr_nlink(dir
, /*force*/1);
772 au_dir_ts(dir
, a
->btgt
);
775 if (au_ftest_ren(a
->auren_flags
, ISSAMEDIR
))
779 inode_inc_iversion(dir
);
780 if (au_ftest_ren(a
->auren_flags
, ISDIR_SRC
))
781 au_cpup_attr_nlink(dir
, /*force*/1);
782 au_dir_ts(dir
, a
->btgt
);
785 static void au_ren_refresh(struct au_ren_args
*a
)
787 aufs_bindex_t bbot
, bindex
;
788 struct dentry
*d
, *h_d
;
789 struct inode
*i
, *h_i
;
790 struct super_block
*sb
;
795 /* already dget-ed by au_ren_or_cpup() */
796 au_set_h_dptr(d
, a
->btgt
, a
->h_dst
);
801 if (!au_ftest_ren(a
->auren_flags
, ISDIR_DST
))
805 au_cpup_attr_timesizes(i
);
807 au_update_dbrange(d
, /*do_put_zero*/1);
809 au_cpup_attr_nlink(i
, /*force*/1);
812 for (bindex
= au_dbtop(d
); bindex
< bbot
; bindex
++)
813 au_set_h_dptr(d
, bindex
, NULL
);
815 for (bindex
= a
->btgt
+ 1; bindex
<= bbot
; bindex
++)
816 au_set_h_dptr(d
, bindex
, NULL
);
817 au_update_dbrange(d
, /*do_put_zero*/0);
821 || au_ftest_ren(a
->auren_flags
, DIRREN
)) {
822 d_drop(a
->src_dentry
);
823 if (au_ftest_ren(a
->auren_flags
, DIRREN
))
824 au_set_dbwh(a
->src_dentry
, -1);
831 for (bindex
= a
->btgt
+ 1; bindex
<= bbot
; bindex
++) {
832 h_d
= au_h_dptr(d
, bindex
);
834 au_set_h_dptr(d
, bindex
, NULL
);
836 au_set_dbbot(d
, a
->btgt
);
840 if (au_opt_test(au_mntflags(sb
), PLINK
) && au_plink_test(i
))
841 return; /* success */
844 for (bindex
= a
->btgt
+ 1; bindex
<= bbot
; bindex
++) {
845 h_i
= au_h_iptr(i
, bindex
);
847 au_xino_write(sb
, bindex
, h_i
->i_ino
, /*ino*/0);
848 /* ignore this error */
849 au_set_h_iptr(i
, bindex
, NULL
, 0);
852 au_set_ibbot(i
, a
->btgt
);
855 /* ---------------------------------------------------------------------- */
857 /* mainly for link(2) and rename(2) */
858 int au_wbr(struct dentry
*dentry
, aufs_bindex_t btgt
)
860 aufs_bindex_t bdiropq
, bwh
;
861 struct dentry
*parent
;
862 struct au_branch
*br
;
864 parent
= dentry
->d_parent
;
865 IMustLock(d_inode(parent
)); /* dir is locked */
867 bdiropq
= au_dbdiropq(parent
);
868 bwh
= au_dbwh(dentry
);
869 br
= au_sbr(dentry
->d_sb
, btgt
);
871 || (0 <= bdiropq
&& bdiropq
< btgt
)
872 || (0 <= bwh
&& bwh
< btgt
))
875 AuDbg("btgt %d\n", btgt
);
879 /* sets src_btop, dst_btop and btgt */
880 static int au_ren_wbr(struct au_ren_args
*a
)
883 struct au_wr_dir_args wr_dir_args
= {
884 /* .force_btgt = -1, */
885 .flags
= AuWrDir_ADD_ENTRY
888 a
->src_btop
= au_dbtop(a
->src_dentry
);
889 a
->dst_btop
= au_dbtop(a
->dst_dentry
);
890 if (au_ftest_ren(a
->auren_flags
, ISDIR_SRC
)
891 || au_ftest_ren(a
->auren_flags
, ISDIR_DST
))
892 au_fset_wrdir(wr_dir_args
.flags
, ISDIR
);
893 wr_dir_args
.force_btgt
= a
->src_btop
;
894 if (a
->dst_inode
&& a
->dst_btop
< a
->src_btop
)
895 wr_dir_args
.force_btgt
= a
->dst_btop
;
896 wr_dir_args
.force_btgt
= au_wbr(a
->dst_dentry
, wr_dir_args
.force_btgt
);
897 err
= au_wr_dir(a
->dst_dentry
, a
->src_dentry
, &wr_dir_args
);
900 au_update_dbtop(a
->dst_dentry
);
905 static void au_ren_dt(struct au_ren_args
*a
)
907 a
->h_path
.dentry
= a
->src_h_parent
;
908 au_dtime_store(a
->src_dt
+ AuPARENT
, a
->src_parent
, &a
->h_path
);
909 if (!au_ftest_ren(a
->auren_flags
, ISSAMEDIR
)) {
910 a
->h_path
.dentry
= a
->dst_h_parent
;
911 au_dtime_store(a
->dst_dt
+ AuPARENT
, a
->dst_parent
, &a
->h_path
);
914 au_fclr_ren(a
->auren_flags
, DT_DSTDIR
);
915 if (!au_ftest_ren(a
->auren_flags
, ISDIR_SRC
)
919 a
->h_path
.dentry
= a
->src_h_dentry
;
920 au_dtime_store(a
->src_dt
+ AuCHILD
, a
->src_dentry
, &a
->h_path
);
921 if (d_is_positive(a
->dst_h_dentry
)) {
922 au_fset_ren(a
->auren_flags
, DT_DSTDIR
);
923 a
->h_path
.dentry
= a
->dst_h_dentry
;
924 au_dtime_store(a
->dst_dt
+ AuCHILD
, a
->dst_dentry
, &a
->h_path
);
928 static void au_ren_rev_dt(int err
, struct au_ren_args
*a
)
931 struct inode
*h_inode
;
933 au_dtime_revert(a
->src_dt
+ AuPARENT
);
934 if (!au_ftest_ren(a
->auren_flags
, ISSAMEDIR
))
935 au_dtime_revert(a
->dst_dt
+ AuPARENT
);
937 if (au_ftest_ren(a
->auren_flags
, ISDIR_SRC
) && err
!= -EIO
) {
938 h_d
= a
->src_dt
[AuCHILD
].dt_h_path
.dentry
;
939 h_inode
= d_inode(h_d
);
940 inode_lock_nested(h_inode
, AuLsc_I_CHILD
);
941 au_dtime_revert(a
->src_dt
+ AuCHILD
);
942 inode_unlock(h_inode
);
944 if (au_ftest_ren(a
->auren_flags
, DT_DSTDIR
)) {
945 h_d
= a
->dst_dt
[AuCHILD
].dt_h_path
.dentry
;
946 h_inode
= d_inode(h_d
);
947 inode_lock_nested(h_inode
, AuLsc_I_CHILD
);
948 au_dtime_revert(a
->dst_dt
+ AuCHILD
);
949 inode_unlock(h_inode
);
954 /* ---------------------------------------------------------------------- */
956 int aufs_rename(struct inode
*_src_dir
, struct dentry
*_src_dentry
,
957 struct inode
*_dst_dir
, struct dentry
*_dst_dentry
,
962 /* reduce stack space */
963 struct au_ren_args
*a
;
966 AuDbg("%pd, %pd, 0x%x\n", _src_dentry
, _dst_dentry
, _flags
);
971 if (unlikely(_flags
& RENAME_WHITEOUT
))
975 BUILD_BUG_ON(sizeof(*a
) > PAGE_SIZE
);
976 a
= kzalloc(sizeof(*a
), GFP_NOFS
);
981 BUILD_BUG_ON(sizeof(a
->exchange
) == sizeof(u8
)
982 && RENAME_EXCHANGE
> U8_MAX
);
983 a
->exchange
= _flags
& RENAME_EXCHANGE
;
984 a
->src_dir
= _src_dir
;
985 a
->src_dentry
= _src_dentry
;
987 if (d_really_is_positive(a
->src_dentry
))
988 a
->src_inode
= d_inode(a
->src_dentry
);
989 a
->src_parent
= a
->src_dentry
->d_parent
; /* dir inode is locked */
990 a
->dst_dir
= _dst_dir
;
991 a
->dst_dentry
= _dst_dentry
;
993 if (d_really_is_positive(a
->dst_dentry
))
994 a
->dst_inode
= d_inode(a
->dst_dentry
);
995 a
->dst_parent
= a
->dst_dentry
->d_parent
; /* dir inode is locked */
998 * if EXCHANGE && src is non-dir && dst is dir,
1001 /* IMustLock(a->dst_inode); */
1002 au_igrab(a
->dst_inode
);
1006 lock_flags
= AuLock_FLUSH
| AuLock_NOPLM
| AuLock_GEN
;
1007 if (d_is_dir(a
->src_dentry
)) {
1008 au_fset_ren(a
->auren_flags
, ISDIR_SRC
);
1009 if (unlikely(!a
->exchange
1010 && d_really_is_positive(a
->dst_dentry
)
1011 && !d_is_dir(a
->dst_dentry
)))
1013 lock_flags
|= AuLock_DIRS
;
1015 if (a
->dst_inode
&& d_is_dir(a
->dst_dentry
)) {
1016 au_fset_ren(a
->auren_flags
, ISDIR_DST
);
1017 if (unlikely(!a
->exchange
1018 && d_really_is_positive(a
->src_dentry
)
1019 && !d_is_dir(a
->src_dentry
)))
1021 lock_flags
|= AuLock_DIRS
;
1023 err
= aufs_read_and_write_lock2(a
->dst_dentry
, a
->src_dentry
,
1028 err
= au_d_hashed_positive(a
->src_dentry
);
1034 * If it is a dir, VFS unhash it before this
1035 * function. It means we cannot rely upon d_unhashed().
1037 if (unlikely(!a
->dst_inode
->i_nlink
))
1039 if (!au_ftest_ren(a
->auren_flags
, ISDIR_DST
)) {
1040 err
= au_d_hashed_positive(a
->dst_dentry
);
1041 if (unlikely(err
&& !a
->exchange
))
1043 } else if (unlikely(IS_DEADDIR(a
->dst_inode
)))
1045 } else if (unlikely(d_unhashed(a
->dst_dentry
)))
1050 * yes, it happened (in linux-3.3-rcN) but I don't know why.
1051 * there may exist a problem somewhere else.
1054 if (unlikely(d_inode(a
->dst_parent
) == d_inode(a
->src_dentry
)))
1057 au_fset_ren(a
->auren_flags
, ISSAMEDIR
); /* temporary */
1058 di_write_lock_parent(a
->dst_parent
);
1060 /* which branch we process */
1061 err
= au_ren_wbr(a
);
1062 if (unlikely(err
< 0))
1064 a
->br
= au_sbr(a
->dst_dentry
->d_sb
, a
->btgt
);
1065 a
->h_path
.mnt
= au_br_mnt(a
->br
);
1067 /* are they available to be renamed */
1068 err
= au_ren_may_dir(a
);
1072 /* prepare the writable parent dir on the same branch */
1073 if (a
->dst_btop
== a
->btgt
) {
1074 au_fset_ren(a
->auren_flags
, WHDST
);
1076 err
= au_cpup_dirs(a
->dst_dentry
, a
->btgt
);
1083 if (a
->src_dir
!= a
->dst_dir
) {
1085 * this temporary unlock is safe,
1086 * because both dir->i_mutex are locked.
1088 di_write_unlock(a
->dst_parent
);
1089 di_write_lock_parent(a
->src_parent
);
1090 err
= au_wr_dir_need_wh(a
->src_dentry
,
1091 au_ftest_ren(a
->auren_flags
,
1094 di_write_unlock(a
->src_parent
);
1095 di_write_lock2_parent(a
->src_parent
, a
->dst_parent
,
1097 au_fclr_ren(a
->auren_flags
, ISSAMEDIR
);
1099 err
= au_wr_dir_need_wh(a
->src_dentry
,
1100 au_ftest_ren(a
->auren_flags
,
1104 if (unlikely(err
< 0))
1107 au_fset_ren(a
->auren_flags
, WHSRC
);
1110 if (a
->src_btop
!= a
->btgt
) {
1111 err
= au_pin(&pin
, a
->src_dentry
, a
->btgt
,
1112 au_opt_udba(a
->src_dentry
->d_sb
),
1113 AuPin_DI_LOCKED
| AuPin_MNT_WRITE
);
1115 struct au_cp_generic cpg
= {
1116 .dentry
= a
->src_dentry
,
1118 .bsrc
= a
->src_btop
,
1121 .flags
= AuCpup_DTIME
| AuCpup_HOPEN
1123 AuDebugOn(au_dbtop(a
->src_dentry
) != a
->src_btop
);
1124 err
= au_sio_cpup_simple(&cpg
);
1129 a
->src_btop
= a
->btgt
;
1130 a
->src_h_dentry
= au_h_dptr(a
->src_dentry
, a
->btgt
);
1132 au_fset_ren(a
->auren_flags
, WHSRC
);
1136 if (a
->exchange
&& a
->dst_inode
1137 && a
->dst_btop
!= a
->btgt
) {
1138 err
= au_pin(&pin
, a
->dst_dentry
, a
->btgt
,
1139 au_opt_udba(a
->dst_dentry
->d_sb
),
1140 AuPin_DI_LOCKED
| AuPin_MNT_WRITE
);
1142 struct au_cp_generic cpg
= {
1143 .dentry
= a
->dst_dentry
,
1145 .bsrc
= a
->dst_btop
,
1148 .flags
= AuCpup_DTIME
| AuCpup_HOPEN
1150 err
= au_sio_cpup_simple(&cpg
);
1155 a
->dst_btop
= a
->btgt
;
1156 a
->dst_h_dentry
= au_h_dptr(a
->dst_dentry
, a
->btgt
);
1160 err
= au_ren_lock(a
);
1162 /* leave the copied-up one */
1166 if (!au_opt_test(au_mntflags(a
->dst_dir
->i_sb
), UDBA_NONE
))
1167 err
= au_may_ren(a
);
1168 else if (unlikely(a
->dst_dentry
->d_name
.len
> AUFS_MAX_NAMELEN
))
1169 err
= -ENAMETOOLONG
;
1174 /* store timestamps to be revertible */
1177 /* store dirren info */
1178 if (au_ftest_ren(a
->auren_flags
, DIRREN
)) {
1179 err
= au_dr_rename(a
->src_dentry
, a
->btgt
,
1180 &a
->dst_dentry
->d_name
, &rev
);
1191 if (au_ftest_ren(a
->auren_flags
, DIRREN
))
1192 au_dr_rename_fin(a
->src_dentry
, a
->btgt
, rev
);
1194 /* update dir attributes */
1195 au_ren_refresh_dir(a
);
1197 /* dput/iput all lower dentries */
1200 goto out_hdir
; /* success */
1203 if (au_ftest_ren(a
->auren_flags
, DIRREN
))
1204 au_dr_rename_rev(a
->src_dentry
, a
->btgt
, rev
);
1206 au_ren_rev_dt(err
, a
);
1210 au_nhash_wh_free(&a
->whlist
);
1211 if (err
&& a
->dst_inode
&& a
->dst_btop
!= a
->btgt
) {
1212 AuDbg("btop %d, btgt %d\n", a
->dst_btop
, a
->btgt
);
1213 au_set_h_dptr(a
->dst_dentry
, a
->btgt
, NULL
);
1214 au_set_dbtop(a
->dst_dentry
, a
->dst_btop
);
1218 if (d_unhashed(a
->src_dentry
))
1219 au_fset_ren(a
->auren_flags
, DROPPED_SRC
);
1220 if (d_unhashed(a
->dst_dentry
))
1221 au_fset_ren(a
->auren_flags
, DROPPED_DST
);
1223 d_move(a
->src_dentry
, a
->dst_dentry
);
1225 d_exchange(a
->src_dentry
, a
->dst_dentry
);
1226 if (au_ftest_ren(a
->auren_flags
, DROPPED_DST
))
1227 d_drop(a
->dst_dentry
);
1229 if (au_ftest_ren(a
->auren_flags
, DROPPED_SRC
))
1230 d_drop(a
->src_dentry
);
1232 au_update_dbtop(a
->dst_dentry
);
1234 d_drop(a
->dst_dentry
);
1236 if (au_ftest_ren(a
->auren_flags
, ISSAMEDIR
))
1237 di_write_unlock(a
->dst_parent
);
1239 di_write_unlock2(a
->src_parent
, a
->dst_parent
);
1241 aufs_read_and_write_unlock2(a
->dst_dentry
, a
->src_dentry
);
1245 au_whtmp_rmdir_free(a
->thargs
);