2 * Copyright (C) 2005-2016 Junjiro R. Okajima
4 * This program, aufs is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * inode operation (rename entry)
20 * todo: this is crazy monster
25 enum { AuSRC
, AuDST
, AuSrcDst
};
26 enum { AuPARENT
, AuCHILD
, AuParentChild
};
28 #define AuRen_ISDIR_SRC 1
29 #define AuRen_ISDIR_DST (1 << 1)
30 #define AuRen_ISSAMEDIR (1 << 2)
31 #define AuRen_WHSRC (1 << 3)
32 #define AuRen_WHDST (1 << 4)
33 #define AuRen_MNT_WRITE (1 << 5)
34 #define AuRen_DT_DSTDIR (1 << 6)
35 #define AuRen_DIROPQ_SRC (1 << 7)
36 #define AuRen_DIROPQ_DST (1 << 8)
37 #define au_ftest_ren(flags, name) ((flags) & AuRen_##name)
38 #define au_fset_ren(flags, name) \
39 do { (flags) |= AuRen_##name; } while (0)
40 #define au_fclr_ren(flags, name) \
41 do { (flags) &= ~AuRen_##name; } while (0)
45 struct dentry
*dentry
, *h_dentry
, *parent
, *h_parent
,
47 struct inode
*dir
, *inode
;
48 struct au_hinode
*hdir
, *hinode
;
49 struct au_dtime dt
[AuParentChild
];
50 aufs_bindex_t btop
, bdiropq
;
53 #define src_dentry sd[AuSRC].dentry
54 #define src_dir sd[AuSRC].dir
55 #define src_inode sd[AuSRC].inode
56 #define src_h_dentry sd[AuSRC].h_dentry
57 #define src_parent sd[AuSRC].parent
58 #define src_h_parent sd[AuSRC].h_parent
59 #define src_wh_dentry sd[AuSRC].wh_dentry
60 #define src_hdir sd[AuSRC].hdir
61 #define src_hinode sd[AuSRC].hinode
62 #define src_h_dir sd[AuSRC].hdir->hi_inode
63 #define src_dt sd[AuSRC].dt
64 #define src_btop sd[AuSRC].btop
65 #define src_bdiropq sd[AuSRC].bdiropq
67 #define dst_dentry sd[AuDST].dentry
68 #define dst_dir sd[AuDST].dir
69 #define dst_inode sd[AuDST].inode
70 #define dst_h_dentry sd[AuDST].h_dentry
71 #define dst_parent sd[AuDST].parent
72 #define dst_h_parent sd[AuDST].h_parent
73 #define dst_wh_dentry sd[AuDST].wh_dentry
74 #define dst_hdir sd[AuDST].hdir
75 #define dst_hinode sd[AuDST].hinode
76 #define dst_h_dir sd[AuDST].hdir->hi_inode
77 #define dst_dt sd[AuDST].dt
78 #define dst_btop sd[AuDST].btop
79 #define dst_bdiropq sd[AuDST].bdiropq
81 struct dentry
*h_trap
;
84 struct au_nhash whlist
;
85 aufs_bindex_t btgt
, src_bwh
;
88 unsigned short auren_flags
;
89 unsigned char flags
; /* syscall parameter */
90 unsigned char exchange
;
93 struct au_whtmp_rmdir
*thargs
;
97 /* ---------------------------------------------------------------------- */
100 * functions for reverting.
101 * when an error happened in a single rename systemcall, we should revert
102 * everything as if nothing happened.
103 * we don't need to revert the copied-up/down the parent dir since they are
107 #define RevertFailure(fmt, ...) do { \
108 AuIOErr("revert failure: " fmt " (%d, %d)\n", \
109 ##__VA_ARGS__, err, rerr); \
113 static void au_ren_do_rev_diropq(int err
, struct au_ren_args
*a
, int idx
)
117 #define src_or_dst(member) a->sd[idx].member
119 d
= src_or_dst(dentry
); /* {src,dst}_dentry */
120 au_hn_inode_lock_nested(src_or_dst(hinode
), AuLsc_I_CHILD
);
121 rerr
= au_diropq_remove(d
, a
->btgt
);
122 au_hn_inode_unlock(src_or_dst(hinode
));
123 au_set_dbdiropq(d
, src_or_dst(bdiropq
));
125 RevertFailure("remove diropq %pd", d
);
130 static void au_ren_rev_diropq(int err
, struct au_ren_args
*a
)
132 if (au_ftest_ren(a
->auren_flags
, DIROPQ_SRC
))
133 au_ren_do_rev_diropq(err
, a
, AuSRC
);
134 if (au_ftest_ren(a
->auren_flags
, DIROPQ_DST
))
135 au_ren_do_rev_diropq(err
, a
, AuDST
);
138 static void au_ren_rev_rename(int err
, struct au_ren_args
*a
)
141 struct inode
*delegated
;
143 a
->h_path
.dentry
= vfsub_lkup_one(&a
->src_dentry
->d_name
,
145 rerr
= PTR_ERR(a
->h_path
.dentry
);
146 if (IS_ERR(a
->h_path
.dentry
)) {
147 RevertFailure("lkup one %pd", a
->src_dentry
);
152 rerr
= vfsub_rename(a
->dst_h_dir
,
153 au_h_dptr(a
->src_dentry
, a
->btgt
),
154 a
->src_h_dir
, &a
->h_path
, &delegated
, a
->flags
);
155 if (unlikely(rerr
== -EWOULDBLOCK
)) {
156 pr_warn("cannot retry for NFSv4 delegation"
157 " for an internal rename\n");
160 d_drop(a
->h_path
.dentry
);
161 dput(a
->h_path
.dentry
);
162 /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
164 RevertFailure("rename %pd", a
->src_dentry
);
167 static void au_ren_rev_whtmp(int err
, struct au_ren_args
*a
)
170 struct inode
*delegated
;
172 a
->h_path
.dentry
= vfsub_lkup_one(&a
->dst_dentry
->d_name
,
174 rerr
= PTR_ERR(a
->h_path
.dentry
);
175 if (IS_ERR(a
->h_path
.dentry
)) {
176 RevertFailure("lkup one %pd", a
->dst_dentry
);
179 if (d_is_positive(a
->h_path
.dentry
)) {
180 d_drop(a
->h_path
.dentry
);
181 dput(a
->h_path
.dentry
);
186 rerr
= vfsub_rename(a
->dst_h_dir
, a
->h_dst
, a
->dst_h_dir
, &a
->h_path
,
187 &delegated
, a
->flags
);
188 if (unlikely(rerr
== -EWOULDBLOCK
)) {
189 pr_warn("cannot retry for NFSv4 delegation"
190 " for an internal rename\n");
193 d_drop(a
->h_path
.dentry
);
194 dput(a
->h_path
.dentry
);
196 au_set_h_dptr(a
->dst_dentry
, a
->btgt
, dget(a
->h_dst
));
198 RevertFailure("rename %pd", a
->h_dst
);
201 static void au_ren_rev_whsrc(int err
, struct au_ren_args
*a
)
205 a
->h_path
.dentry
= a
->src_wh_dentry
;
206 rerr
= au_wh_unlink_dentry(a
->src_h_dir
, &a
->h_path
, a
->src_dentry
);
207 au_set_dbwh(a
->src_dentry
, a
->src_bwh
);
209 RevertFailure("unlink %pd", a
->src_wh_dentry
);
213 /* ---------------------------------------------------------------------- */
216 * when we have to copyup the renaming entry, do it with the rename-target name
217 * in order to minimize the cost (the later actual rename is unnecessary).
218 * otherwise rename it on the target branch.
220 static int au_ren_or_cpup(struct au_ren_args
*a
)
224 struct inode
*delegated
;
227 if (au_dbtop(d
) == a
->btgt
) {
228 a
->h_path
.dentry
= a
->dst_h_dentry
;
229 AuDebugOn(au_dbtop(d
) != a
->btgt
);
231 err
= vfsub_rename(a
->src_h_dir
, au_h_dptr(d
, a
->btgt
),
232 a
->dst_h_dir
, &a
->h_path
, &delegated
,
234 if (unlikely(err
== -EWOULDBLOCK
)) {
235 pr_warn("cannot retry for NFSv4 delegation"
236 " for an internal rename\n");
242 if (!err
&& a
->h_dst
)
243 /* it will be set to dinfo later */
249 /* cf. aufs_rmdir() */
250 static int au_ren_del_whtmp(struct au_ren_args
*a
)
256 SiMustAnyLock(dir
->i_sb
);
257 if (!au_nhash_test_longer_wh(&a
->whlist
, a
->btgt
,
258 au_sbi(dir
->i_sb
)->si_dirwh
)
259 || au_test_fs_remote(a
->h_dst
->d_sb
)) {
260 err
= au_whtmp_rmdir(dir
, a
->btgt
, a
->h_dst
, &a
->whlist
);
262 pr_warn("failed removing whtmp dir %pd (%d), "
263 "ignored.\n", a
->h_dst
, err
);
265 au_nhash_wh_free(&a
->thargs
->whlist
);
266 a
->thargs
->whlist
= a
->whlist
;
267 a
->whlist
.nh_num
= 0;
268 au_whtmp_kick_rmdir(dir
, a
->btgt
, a
->h_dst
, a
->thargs
);
276 /* make it 'opaque' dir. */
277 static int au_ren_do_diropq(struct au_ren_args
*a
, int idx
)
280 struct dentry
*d
, *diropq
;
281 #define src_or_dst(member) a->sd[idx].member
284 d
= src_or_dst(dentry
); /* {src,dst}_dentry */
285 src_or_dst(bdiropq
) = au_dbdiropq(d
);
286 src_or_dst(hinode
) = au_hi(src_or_dst(inode
), a
->btgt
);
287 au_hn_inode_lock_nested(src_or_dst(hinode
), AuLsc_I_CHILD
);
288 diropq
= au_diropq_create(d
, a
->btgt
);
289 au_hn_inode_unlock(src_or_dst(hinode
));
291 err
= PTR_ERR(diropq
);
299 static int au_ren_diropq(struct au_ren_args
*a
)
302 unsigned char always
;
306 d
= a
->dst_dentry
; /* already renamed on the branch */
307 always
= !!au_opt_test(au_mntflags(d
->d_sb
), ALWAYS_DIROPQ
);
308 if (au_ftest_ren(a
->auren_flags
, ISDIR_SRC
)
309 && a
->btgt
!= au_dbdiropq(a
->src_dentry
)
311 || a
->btgt
<= au_dbdiropq(d
)
312 /* hide the lower to keep xino */
313 /* the lowers may not be a dir, but we hide them anyway */
314 || a
->btgt
< au_dbbot(d
)
317 err
= au_ren_do_diropq(a
, AuSRC
);
320 au_fset_ren(a
->auren_flags
, DIROPQ_SRC
);
323 goto out
; /* success */
325 d
= a
->src_dentry
; /* already renamed on the branch */
326 if (au_ftest_ren(a
->auren_flags
, ISDIR_DST
)
327 && a
->btgt
!= au_dbdiropq(a
->dst_dentry
)
328 && (a
->btgt
< au_dbdiropq(d
)
329 || a
->btgt
< au_dbbot(d
)
331 AuDbgDentry(a
->src_dentry
);
332 AuDbgDentry(a
->dst_dentry
);
333 err
= au_ren_do_diropq(a
, AuDST
);
336 au_fset_ren(a
->auren_flags
, DIROPQ_DST
);
338 goto out
; /* success */
341 AuDbg("err %d, reverting src\n", err
);
342 au_ren_rev_diropq(err
, a
);
347 static int do_rename(struct au_ren_args
*a
)
350 struct dentry
*d
, *h_d
;
353 /* prepare workqueue args for asynchronous rmdir */
354 h_d
= a
->dst_h_dentry
;
355 if (au_ftest_ren(a
->auren_flags
, ISDIR_DST
)
356 && d_is_positive(h_d
)) {
358 a
->thargs
= au_whtmp_rmdir_alloc(a
->src_dentry
->d_sb
,
360 if (unlikely(!a
->thargs
))
362 a
->h_dst
= dget(h_d
);
365 /* create whiteout for src_dentry */
366 if (au_ftest_ren(a
->auren_flags
, WHSRC
)) {
367 a
->src_bwh
= au_dbwh(a
->src_dentry
);
368 AuDebugOn(a
->src_bwh
>= 0);
369 a
->src_wh_dentry
= au_wh_create(a
->src_dentry
, a
->btgt
,
371 err
= PTR_ERR(a
->src_wh_dentry
);
372 if (IS_ERR(a
->src_wh_dentry
))
376 /* lookup whiteout for dentry */
377 if (au_ftest_ren(a
->auren_flags
, WHDST
)) {
378 h_d
= au_wh_lkup(a
->dst_h_parent
,
379 &a
->dst_dentry
->d_name
, a
->br
);
383 if (d_is_negative(h_d
))
386 a
->dst_wh_dentry
= h_d
;
389 /* rename dentry to tmpwh */
391 err
= au_whtmp_ren(a
->dst_h_dentry
, a
->br
);
396 au_set_h_dptr(d
, a
->btgt
, NULL
);
397 err
= au_lkup_neg(d
, a
->btgt
, /*wh*/0);
400 a
->dst_h_dentry
= au_h_dptr(d
, a
->btgt
);
404 BUG_ON(d_is_positive(a
->dst_h_dentry
) && a
->src_btop
!= a
->btgt
);
406 /* rename by vfs_rename or cpup */
407 err
= au_ren_or_cpup(a
);
409 /* leave the copied-up one */
412 /* make dir opaque */
413 err
= au_ren_diropq(a
);
417 /* update target timestamps */
419 AuDebugOn(au_dbtop(a
->dst_dentry
) != a
->btgt
);
420 a
->h_path
.dentry
= au_h_dptr(a
->dst_dentry
, a
->btgt
);
421 vfsub_update_h_iattr(&a
->h_path
, /*did*/NULL
); /*ignore*/
422 a
->dst_inode
->i_ctime
= d_inode(a
->h_path
.dentry
)->i_ctime
;
424 AuDebugOn(au_dbtop(a
->src_dentry
) != a
->btgt
);
425 a
->h_path
.dentry
= au_h_dptr(a
->src_dentry
, a
->btgt
);
426 vfsub_update_h_iattr(&a
->h_path
, /*did*/NULL
); /*ignore*/
427 a
->src_inode
->i_ctime
= d_inode(a
->h_path
.dentry
)->i_ctime
;
430 /* remove whiteout for dentry */
431 if (a
->dst_wh_dentry
) {
432 a
->h_path
.dentry
= a
->dst_wh_dentry
;
433 err
= au_wh_unlink_dentry(a
->dst_h_dir
, &a
->h_path
,
441 au_ren_del_whtmp(a
); /* ignore this error */
443 au_fhsm_wrote(a
->src_dentry
->d_sb
, a
->btgt
, /*force*/0);
449 au_ren_rev_diropq(err
, a
);
451 au_ren_rev_rename(err
, a
);
455 au_ren_rev_whtmp(err
, a
);
457 dput(a
->dst_wh_dentry
);
458 a
->dst_wh_dentry
= NULL
;
460 if (a
->src_wh_dentry
)
461 au_ren_rev_whsrc(err
, a
);
463 dput(a
->src_wh_dentry
);
464 dput(a
->dst_wh_dentry
);
468 au_whtmp_rmdir_free(a
->thargs
);
475 /* ---------------------------------------------------------------------- */
478 * test if @dentry dir can be rename destination or not.
479 * success means, it is a logically empty dir.
481 static int may_rename_dstdir(struct dentry
*dentry
, struct au_nhash
*whlist
)
483 return au_test_empty(dentry
, whlist
);
487 * test if @dentry dir can be rename source or not.
488 * if it can, return 0 and @children is filled.
490 * - it is a logically empty dir.
491 * - or, it exists on writable branch and has no children including whiteouts
492 * on the lower branch.
494 static int may_rename_srcdir(struct dentry
*dentry
, aufs_bindex_t btgt
)
500 btop
= au_dbtop(dentry
);
502 struct au_nhash whlist
;
504 SiMustAnyLock(dentry
->d_sb
);
505 rdhash
= au_sbi(dentry
->d_sb
)->si_rdhash
;
507 rdhash
= au_rdhash_est(au_dir_size(/*file*/NULL
,
509 err
= au_nhash_alloc(&whlist
, rdhash
, GFP_NOFS
);
512 err
= au_test_empty(dentry
, &whlist
);
513 au_nhash_wh_free(&whlist
);
517 if (btop
== au_dbtaildir(dentry
))
518 return 0; /* success */
520 err
= au_test_empty_lower(dentry
);
523 if (err
== -ENOTEMPTY
) {
524 AuWarn1("renaming dir who has child(ren) on multiple branches,"
525 " is not supported\n");
531 /* side effect: sets whlist and h_dentry */
532 static int au_ren_may_dir(struct au_ren_args
*a
)
539 SiMustAnyLock(d
->d_sb
);
542 if (au_ftest_ren(a
->auren_flags
, ISDIR_DST
) && a
->dst_inode
) {
543 rdhash
= au_sbi(d
->d_sb
)->si_rdhash
;
545 rdhash
= au_rdhash_est(au_dir_size(/*file*/NULL
, d
));
546 err
= au_nhash_alloc(&a
->whlist
, rdhash
, GFP_NOFS
);
551 au_set_dbtop(d
, a
->dst_btop
);
552 err
= may_rename_dstdir(d
, &a
->whlist
);
553 au_set_dbtop(d
, a
->btgt
);
555 err
= may_rename_srcdir(d
, a
->btgt
);
557 a
->dst_h_dentry
= au_h_dptr(d
, au_dbtop(d
));
562 a
->src_h_dentry
= au_h_dptr(d
, au_dbtop(d
));
563 if (au_ftest_ren(a
->auren_flags
, ISDIR_SRC
)) {
564 err
= may_rename_srcdir(d
, a
->btgt
);
566 au_nhash_wh_free(&a
->whlist
);
567 a
->whlist
.nh_num
= 0;
574 /* ---------------------------------------------------------------------- */
577 * simple tests for rename.
578 * following the checks in vfs, plus the parent-child relationship.
580 static int au_may_ren(struct au_ren_args
*a
)
583 struct inode
*h_inode
;
585 if (a
->src_btop
== a
->btgt
) {
586 err
= au_may_del(a
->src_dentry
, a
->btgt
, a
->src_h_parent
,
587 au_ftest_ren(a
->auren_flags
, ISDIR_SRC
));
591 if (unlikely(a
->src_h_dentry
== a
->h_trap
))
596 if (a
->dst_btop
!= a
->btgt
)
600 if (unlikely(a
->dst_h_dentry
== a
->h_trap
))
604 isdir
= !!au_ftest_ren(a
->auren_flags
, ISDIR_DST
);
605 if (d_really_is_negative(a
->dst_dentry
)) {
606 if (d_is_negative(a
->dst_h_dentry
))
607 err
= au_may_add(a
->dst_dentry
, a
->btgt
,
608 a
->dst_h_parent
, isdir
);
610 if (unlikely(d_is_negative(a
->dst_h_dentry
)))
612 h_inode
= d_inode(a
->dst_h_dentry
);
613 if (h_inode
->i_nlink
)
614 err
= au_may_del(a
->dst_dentry
, a
->btgt
,
615 a
->dst_h_parent
, isdir
);
619 if (unlikely(err
== -ENOENT
|| err
== -EEXIST
))
625 /* ---------------------------------------------------------------------- */
630 * - src_dir and dir by lock_rename()
634 * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
636 * + di_write_lock2_child()
637 * + di_write_lock_child()
638 * + ii_write_lock_child()
639 * + di_write_lock_child2()
640 * + ii_write_lock_child2()
641 * + src_parent and parent
642 * + di_write_lock_parent()
643 * + ii_write_lock_parent()
644 * + di_write_lock_parent2()
645 * + ii_write_lock_parent2()
646 * + lower src_dir and dir by vfsub_lock_rename()
647 * + verify the every relationships between child and parent. if any
648 * of them failed, unlock all and return -EBUSY.
650 static void au_ren_unlock(struct au_ren_args
*a
)
652 vfsub_unlock_rename(a
->src_h_parent
, a
->src_hdir
,
653 a
->dst_h_parent
, a
->dst_hdir
);
654 if (au_ftest_ren(a
->auren_flags
, MNT_WRITE
))
655 vfsub_mnt_drop_write(au_br_mnt(a
->br
));
658 static int au_ren_lock(struct au_ren_args
*a
)
664 a
->src_h_parent
= au_h_dptr(a
->src_parent
, a
->btgt
);
665 a
->src_hdir
= au_hi(a
->src_dir
, a
->btgt
);
666 a
->dst_h_parent
= au_h_dptr(a
->dst_parent
, a
->btgt
);
667 a
->dst_hdir
= au_hi(a
->dst_dir
, a
->btgt
);
669 err
= vfsub_mnt_want_write(au_br_mnt(a
->br
));
672 au_fset_ren(a
->auren_flags
, MNT_WRITE
);
673 a
->h_trap
= vfsub_lock_rename(a
->src_h_parent
, a
->src_hdir
,
674 a
->dst_h_parent
, a
->dst_hdir
);
675 udba
= au_opt_udba(a
->src_dentry
->d_sb
);
676 if (unlikely(a
->src_hdir
->hi_inode
!= d_inode(a
->src_h_parent
)
677 || a
->dst_hdir
->hi_inode
!= d_inode(a
->dst_h_parent
)))
678 err
= au_busy_or_stale();
679 if (!err
&& au_dbtop(a
->src_dentry
) == a
->btgt
)
680 err
= au_h_verify(a
->src_h_dentry
, udba
,
681 d_inode(a
->src_h_parent
), a
->src_h_parent
,
683 if (!err
&& au_dbtop(a
->dst_dentry
) == a
->btgt
)
684 err
= au_h_verify(a
->dst_h_dentry
, udba
,
685 d_inode(a
->dst_h_parent
), a
->dst_h_parent
,
688 goto out
; /* success */
690 err
= au_busy_or_stale();
697 /* ---------------------------------------------------------------------- */
699 static void au_ren_refresh_dir(struct au_ren_args
*a
)
705 if (au_ftest_ren(a
->auren_flags
, ISDIR_SRC
)) {
706 /* is this updating defined in POSIX? */
707 au_cpup_attr_timesizes(a
->src_inode
);
708 au_cpup_attr_nlink(dir
, /*force*/1);
710 au_dir_ts(dir
, a
->btgt
);
715 if (au_ftest_ren(a
->auren_flags
, ISDIR_DST
)) {
716 /* is this updating defined in POSIX? */
717 au_cpup_attr_timesizes(a
->dst_inode
);
718 au_cpup_attr_nlink(dir
, /*force*/1);
720 au_dir_ts(dir
, a
->btgt
);
723 if (au_ftest_ren(a
->auren_flags
, ISSAMEDIR
))
728 if (au_ftest_ren(a
->auren_flags
, ISDIR_SRC
))
729 au_cpup_attr_nlink(dir
, /*force*/1);
730 au_dir_ts(dir
, a
->btgt
);
733 static void au_ren_refresh(struct au_ren_args
*a
)
735 aufs_bindex_t bbot
, bindex
;
736 struct dentry
*d
, *h_d
;
737 struct inode
*i
, *h_i
;
738 struct super_block
*sb
;
743 /* already dget-ed by au_ren_or_cpup() */
744 au_set_h_dptr(d
, a
->btgt
, a
->h_dst
);
749 if (!au_ftest_ren(a
->auren_flags
, ISDIR_DST
))
753 au_cpup_attr_timesizes(i
);
755 au_update_dbrange(d
, /*do_put_zero*/1);
757 au_cpup_attr_nlink(i
, /*force*/1);
760 for (bindex
= au_dbtop(d
); bindex
< bbot
; bindex
++)
761 au_set_h_dptr(d
, bindex
, NULL
);
763 for (bindex
= a
->btgt
+ 1; bindex
<= bbot
; bindex
++)
764 au_set_h_dptr(d
, bindex
, NULL
);
765 au_update_dbrange(d
, /*do_put_zero*/0);
772 for (bindex
= a
->btgt
+ 1; bindex
<= bbot
; bindex
++) {
773 h_d
= au_h_dptr(d
, bindex
);
775 au_set_h_dptr(d
, bindex
, NULL
);
777 au_set_dbbot(d
, a
->btgt
);
781 if (au_opt_test(au_mntflags(sb
), PLINK
) && au_plink_test(i
))
782 return; /* success */
785 for (bindex
= a
->btgt
+ 1; bindex
<= bbot
; bindex
++) {
786 h_i
= au_h_iptr(i
, bindex
);
788 au_xino_write(sb
, bindex
, h_i
->i_ino
, /*ino*/0);
789 /* ignore this error */
790 au_set_h_iptr(i
, bindex
, NULL
, 0);
793 au_set_ibbot(i
, a
->btgt
);
795 d_drop(a
->src_dentry
);
798 /* ---------------------------------------------------------------------- */
800 /* mainly for link(2) and rename(2) */
801 int au_wbr(struct dentry
*dentry
, aufs_bindex_t btgt
)
803 aufs_bindex_t bdiropq
, bwh
;
804 struct dentry
*parent
;
805 struct au_branch
*br
;
807 parent
= dentry
->d_parent
;
808 IMustLock(d_inode(parent
)); /* dir is locked */
810 bdiropq
= au_dbdiropq(parent
);
811 bwh
= au_dbwh(dentry
);
812 br
= au_sbr(dentry
->d_sb
, btgt
);
814 || (0 <= bdiropq
&& bdiropq
< btgt
)
815 || (0 <= bwh
&& bwh
< btgt
))
818 AuDbg("btgt %d\n", btgt
);
822 /* sets src_btop, dst_btop and btgt */
823 static int au_ren_wbr(struct au_ren_args
*a
)
826 struct au_wr_dir_args wr_dir_args
= {
827 /* .force_btgt = -1, */
828 .flags
= AuWrDir_ADD_ENTRY
831 a
->src_btop
= au_dbtop(a
->src_dentry
);
832 a
->dst_btop
= au_dbtop(a
->dst_dentry
);
833 if (au_ftest_ren(a
->auren_flags
, ISDIR_SRC
)
834 || au_ftest_ren(a
->auren_flags
, ISDIR_DST
))
835 au_fset_wrdir(wr_dir_args
.flags
, ISDIR
);
836 wr_dir_args
.force_btgt
= a
->src_btop
;
837 if (a
->dst_inode
&& a
->dst_btop
< a
->src_btop
)
838 wr_dir_args
.force_btgt
= a
->dst_btop
;
839 wr_dir_args
.force_btgt
= au_wbr(a
->dst_dentry
, wr_dir_args
.force_btgt
);
840 err
= au_wr_dir(a
->dst_dentry
, a
->src_dentry
, &wr_dir_args
);
843 au_update_dbtop(a
->dst_dentry
);
848 static void au_ren_dt(struct au_ren_args
*a
)
850 a
->h_path
.dentry
= a
->src_h_parent
;
851 au_dtime_store(a
->src_dt
+ AuPARENT
, a
->src_parent
, &a
->h_path
);
852 if (!au_ftest_ren(a
->auren_flags
, ISSAMEDIR
)) {
853 a
->h_path
.dentry
= a
->dst_h_parent
;
854 au_dtime_store(a
->dst_dt
+ AuPARENT
, a
->dst_parent
, &a
->h_path
);
857 au_fclr_ren(a
->auren_flags
, DT_DSTDIR
);
858 if (!au_ftest_ren(a
->auren_flags
, ISDIR_SRC
)
862 a
->h_path
.dentry
= a
->src_h_dentry
;
863 au_dtime_store(a
->src_dt
+ AuCHILD
, a
->src_dentry
, &a
->h_path
);
864 if (d_is_positive(a
->dst_h_dentry
)) {
865 au_fset_ren(a
->auren_flags
, DT_DSTDIR
);
866 a
->h_path
.dentry
= a
->dst_h_dentry
;
867 au_dtime_store(a
->dst_dt
+ AuCHILD
, a
->dst_dentry
, &a
->h_path
);
871 static void au_ren_rev_dt(int err
, struct au_ren_args
*a
)
874 struct inode
*h_inode
;
876 au_dtime_revert(a
->src_dt
+ AuPARENT
);
877 if (!au_ftest_ren(a
->auren_flags
, ISSAMEDIR
))
878 au_dtime_revert(a
->dst_dt
+ AuPARENT
);
880 if (au_ftest_ren(a
->auren_flags
, ISDIR_SRC
) && err
!= -EIO
) {
881 h_d
= a
->src_dt
[AuCHILD
].dt_h_path
.dentry
;
882 h_inode
= d_inode(h_d
);
883 inode_lock_nested(h_inode
, AuLsc_I_CHILD
);
884 au_dtime_revert(a
->src_dt
+ AuCHILD
);
885 inode_unlock(h_inode
);
887 if (au_ftest_ren(a
->auren_flags
, DT_DSTDIR
)) {
888 h_d
= a
->dst_dt
[AuCHILD
].dt_h_path
.dentry
;
889 h_inode
= d_inode(h_d
);
890 inode_lock_nested(h_inode
, AuLsc_I_CHILD
);
891 au_dtime_revert(a
->dst_dt
+ AuCHILD
);
892 inode_unlock(h_inode
);
897 /* ---------------------------------------------------------------------- */
899 int aufs_rename(struct inode
*_src_dir
, struct dentry
*_src_dentry
,
900 struct inode
*_dst_dir
, struct dentry
*_dst_dentry
,
904 /* reduce stack space */
905 struct au_ren_args
*a
;
908 AuDbg("%pd, %pd, 0x%x\n", _src_dentry
, _dst_dentry
, _flags
);
913 if (unlikely(_flags
& RENAME_WHITEOUT
))
917 BUILD_BUG_ON(sizeof(*a
) > PAGE_SIZE
);
918 a
= kzalloc(sizeof(*a
), GFP_NOFS
);
923 a
->exchange
= _flags
& RENAME_EXCHANGE
;
924 a
->src_dir
= _src_dir
;
925 a
->src_dentry
= _src_dentry
;
927 if (d_really_is_positive(a
->src_dentry
))
928 a
->src_inode
= d_inode(a
->src_dentry
);
929 a
->src_parent
= a
->src_dentry
->d_parent
; /* dir inode is locked */
930 a
->dst_dir
= _dst_dir
;
931 a
->dst_dentry
= _dst_dentry
;
933 if (d_really_is_positive(a
->dst_dentry
))
934 a
->dst_inode
= d_inode(a
->dst_dentry
);
935 a
->dst_parent
= a
->dst_dentry
->d_parent
; /* dir inode is locked */
938 * if EXCHANGE && src is non-dir && dst is dir,
941 /* IMustLock(a->dst_inode); */
942 au_igrab(a
->dst_inode
);
946 lock_flags
= AuLock_FLUSH
| AuLock_NOPLM
| AuLock_GEN
;
947 if (d_is_dir(a
->src_dentry
)) {
948 au_fset_ren(a
->auren_flags
, ISDIR_SRC
);
949 if (unlikely(!a
->exchange
950 && d_really_is_positive(a
->dst_dentry
)
951 && !d_is_dir(a
->dst_dentry
)))
953 lock_flags
|= AuLock_DIRS
;
955 if (a
->dst_inode
&& d_is_dir(a
->dst_dentry
)) {
956 au_fset_ren(a
->auren_flags
, ISDIR_DST
);
957 if (unlikely(!a
->exchange
958 && d_really_is_positive(a
->src_dentry
)
959 && !d_is_dir(a
->src_dentry
)))
961 lock_flags
|= AuLock_DIRS
;
963 err
= aufs_read_and_write_lock2(a
->dst_dentry
, a
->src_dentry
, lock_flags
);
967 err
= au_d_hashed_positive(a
->src_dentry
);
973 * If it is a dir, VFS unhash it before this
974 * function. It means we cannot rely upon d_unhashed().
976 if (unlikely(!a
->dst_inode
->i_nlink
))
978 if (!au_ftest_ren(a
->auren_flags
, ISDIR_DST
)) {
979 err
= au_d_hashed_positive(a
->dst_dentry
);
980 if (unlikely(err
&& !a
->exchange
))
982 } else if (unlikely(IS_DEADDIR(a
->dst_inode
)))
984 } else if (unlikely(d_unhashed(a
->dst_dentry
)))
989 * yes, it happened (in linux-3.3-rcN) but I don't know why.
990 * there may exist a problem somewhere else.
993 if (unlikely(d_inode(a
->dst_parent
) == d_inode(a
->src_dentry
)))
996 au_fset_ren(a
->auren_flags
, ISSAMEDIR
); /* temporary */
997 di_write_lock_parent(a
->dst_parent
);
999 /* which branch we process */
1000 err
= au_ren_wbr(a
);
1001 if (unlikely(err
< 0))
1003 a
->br
= au_sbr(a
->dst_dentry
->d_sb
, a
->btgt
);
1004 a
->h_path
.mnt
= au_br_mnt(a
->br
);
1006 /* are they available to be renamed */
1007 err
= au_ren_may_dir(a
);
1011 /* prepare the writable parent dir on the same branch */
1012 if (a
->dst_btop
== a
->btgt
) {
1013 au_fset_ren(a
->auren_flags
, WHDST
);
1015 err
= au_cpup_dirs(a
->dst_dentry
, a
->btgt
);
1022 if (a
->src_dir
!= a
->dst_dir
) {
1024 * this temporary unlock is safe,
1025 * because both dir->i_mutex are locked.
1027 di_write_unlock(a
->dst_parent
);
1028 di_write_lock_parent(a
->src_parent
);
1029 err
= au_wr_dir_need_wh(a
->src_dentry
,
1030 au_ftest_ren(a
->auren_flags
,
1033 di_write_unlock(a
->src_parent
);
1034 di_write_lock2_parent(a
->src_parent
, a
->dst_parent
,
1036 au_fclr_ren(a
->auren_flags
, ISSAMEDIR
);
1038 err
= au_wr_dir_need_wh(a
->src_dentry
,
1039 au_ftest_ren(a
->auren_flags
,
1043 if (unlikely(err
< 0))
1046 au_fset_ren(a
->auren_flags
, WHSRC
);
1049 if (a
->src_btop
!= a
->btgt
) {
1050 err
= au_pin(&pin
, a
->src_dentry
, a
->btgt
,
1051 au_opt_udba(a
->src_dentry
->d_sb
),
1052 AuPin_DI_LOCKED
| AuPin_MNT_WRITE
);
1054 struct au_cp_generic cpg
= {
1055 .dentry
= a
->src_dentry
,
1057 .bsrc
= a
->src_btop
,
1060 .flags
= AuCpup_DTIME
| AuCpup_HOPEN
1062 AuDebugOn(au_dbtop(a
->src_dentry
) != a
->src_btop
);
1063 err
= au_sio_cpup_simple(&cpg
);
1068 a
->src_btop
= a
->btgt
;
1069 a
->src_h_dentry
= au_h_dptr(a
->src_dentry
, a
->btgt
);
1071 au_fset_ren(a
->auren_flags
, WHSRC
);
1075 if (a
->exchange
&& a
->dst_inode
1076 && a
->dst_btop
!= a
->btgt
) {
1077 err
= au_pin(&pin
, a
->dst_dentry
, a
->btgt
,
1078 au_opt_udba(a
->dst_dentry
->d_sb
),
1079 AuPin_DI_LOCKED
| AuPin_MNT_WRITE
);
1081 struct au_cp_generic cpg
= {
1082 .dentry
= a
->dst_dentry
,
1084 .bsrc
= a
->dst_btop
,
1087 .flags
= AuCpup_DTIME
| AuCpup_HOPEN
1089 err
= au_sio_cpup_simple(&cpg
);
1094 a
->dst_btop
= a
->btgt
;
1095 a
->dst_h_dentry
= au_h_dptr(a
->dst_dentry
, a
->btgt
);
1099 err
= au_ren_lock(a
);
1101 /* leave the copied-up one */
1105 if (!au_opt_test(au_mntflags(a
->dst_dir
->i_sb
), UDBA_NONE
))
1106 err
= au_may_ren(a
);
1107 else if (unlikely(a
->dst_dentry
->d_name
.len
> AUFS_MAX_NAMELEN
))
1108 err
= -ENAMETOOLONG
;
1113 /* store timestamps to be revertible */
1121 /* update dir attributes */
1122 au_ren_refresh_dir(a
);
1124 /* dput/iput all lower dentries */
1127 goto out_hdir
; /* success */
1130 au_ren_rev_dt(err
, a
);
1134 au_nhash_wh_free(&a
->whlist
);
1135 if (err
&& a
->dst_inode
&& a
->dst_btop
!= a
->btgt
) {
1136 AuDbg("btop %d, btgt %d\n", a
->dst_btop
, a
->btgt
);
1137 au_set_h_dptr(a
->dst_dentry
, a
->btgt
, NULL
);
1138 au_set_dbtop(a
->dst_dentry
, a
->dst_btop
);
1143 d_move(a
->src_dentry
, a
->dst_dentry
);
1145 d_exchange(a
->src_dentry
, a
->dst_dentry
);
1147 au_update_dbtop(a
->dst_dentry
);
1149 d_drop(a
->dst_dentry
);
1151 if (au_ftest_ren(a
->auren_flags
, ISSAMEDIR
))
1152 di_write_unlock(a
->dst_parent
);
1154 di_write_unlock2(a
->src_parent
, a
->dst_parent
);
1156 aufs_read_and_write_unlock2(a
->dst_dentry
, a
->src_dentry
);
1160 au_whtmp_rmdir_free(a
->thargs
);
1161 au_delayed_kfree(a
);