2 * Copyright (C) 2005-2017 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 AuRen_DIRREN (1 << 9)
38 #define AuRen_DROPPED_SRC (1 << 10)
39 #define AuRen_DROPPED_DST (1 << 11)
40 #define au_ftest_ren(flags, name) ((flags) & AuRen_##name)
41 #define au_fset_ren(flags, name) \
42 do { (flags) |= AuRen_##name; } while (0)
43 #define au_fclr_ren(flags, name) \
44 do { (flags) &= ~AuRen_##name; } while (0)
46 #ifndef CONFIG_AUFS_DIRREN
48 #define AuRen_DIRREN 0
53 struct dentry
*dentry
, *h_dentry
, *parent
, *h_parent
,
55 struct inode
*dir
, *inode
;
56 struct au_hinode
*hdir
, *hinode
;
57 struct au_dtime dt
[AuParentChild
];
58 aufs_bindex_t btop
, bdiropq
;
61 #define src_dentry sd[AuSRC].dentry
62 #define src_dir sd[AuSRC].dir
63 #define src_inode sd[AuSRC].inode
64 #define src_h_dentry sd[AuSRC].h_dentry
65 #define src_parent sd[AuSRC].parent
66 #define src_h_parent sd[AuSRC].h_parent
67 #define src_wh_dentry sd[AuSRC].wh_dentry
68 #define src_hdir sd[AuSRC].hdir
69 #define src_hinode sd[AuSRC].hinode
70 #define src_h_dir sd[AuSRC].hdir->hi_inode
71 #define src_dt sd[AuSRC].dt
72 #define src_btop sd[AuSRC].btop
73 #define src_bdiropq sd[AuSRC].bdiropq
75 #define dst_dentry sd[AuDST].dentry
76 #define dst_dir sd[AuDST].dir
77 #define dst_inode sd[AuDST].inode
78 #define dst_h_dentry sd[AuDST].h_dentry
79 #define dst_parent sd[AuDST].parent
80 #define dst_h_parent sd[AuDST].h_parent
81 #define dst_wh_dentry sd[AuDST].wh_dentry
82 #define dst_hdir sd[AuDST].hdir
83 #define dst_hinode sd[AuDST].hinode
84 #define dst_h_dir sd[AuDST].hdir->hi_inode
85 #define dst_dt sd[AuDST].dt
86 #define dst_btop sd[AuDST].btop
87 #define dst_bdiropq sd[AuDST].bdiropq
89 struct dentry
*h_trap
;
92 struct au_nhash whlist
;
93 aufs_bindex_t btgt
, src_bwh
;
96 unsigned short auren_flags
;
97 unsigned char flags
; /* syscall parameter */
98 unsigned char exchange
;
101 struct au_whtmp_rmdir
*thargs
;
102 struct dentry
*h_dst
;
103 struct au_hinode
*h_root
;
106 /* ---------------------------------------------------------------------- */
109 * functions for reverting.
110 * when an error happened in a single rename systemcall, we should revert
111 * everything as if nothing happened.
112 * we don't need to revert the copied-up/down the parent dir since they are
116 #define RevertFailure(fmt, ...) do { \
117 AuIOErr("revert failure: " fmt " (%d, %d)\n", \
118 ##__VA_ARGS__, err, rerr); \
122 static void au_ren_do_rev_diropq(int err
, struct au_ren_args
*a
, int idx
)
126 #define src_or_dst(member) a->sd[idx].member
128 d
= src_or_dst(dentry
); /* {src,dst}_dentry */
129 au_hn_inode_lock_nested(src_or_dst(hinode
), AuLsc_I_CHILD
);
130 rerr
= au_diropq_remove(d
, a
->btgt
);
131 au_hn_inode_unlock(src_or_dst(hinode
));
132 au_set_dbdiropq(d
, src_or_dst(bdiropq
));
134 RevertFailure("remove diropq %pd", d
);
139 static void au_ren_rev_diropq(int err
, struct au_ren_args
*a
)
141 if (au_ftest_ren(a
->auren_flags
, DIROPQ_SRC
))
142 au_ren_do_rev_diropq(err
, a
, AuSRC
);
143 if (au_ftest_ren(a
->auren_flags
, DIROPQ_DST
))
144 au_ren_do_rev_diropq(err
, a
, AuDST
);
147 static void au_ren_rev_rename(int err
, struct au_ren_args
*a
)
150 struct inode
*delegated
;
152 a
->h_path
.dentry
= vfsub_lkup_one(&a
->src_dentry
->d_name
,
154 rerr
= PTR_ERR(a
->h_path
.dentry
);
155 if (IS_ERR(a
->h_path
.dentry
)) {
156 RevertFailure("lkup one %pd", a
->src_dentry
);
161 rerr
= vfsub_rename(a
->dst_h_dir
,
162 au_h_dptr(a
->src_dentry
, a
->btgt
),
163 a
->src_h_dir
, &a
->h_path
, &delegated
, a
->flags
);
164 if (unlikely(rerr
== -EWOULDBLOCK
)) {
165 pr_warn("cannot retry for NFSv4 delegation"
166 " for an internal rename\n");
169 d_drop(a
->h_path
.dentry
);
170 dput(a
->h_path
.dentry
);
171 /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
173 RevertFailure("rename %pd", a
->src_dentry
);
176 static void au_ren_rev_whtmp(int err
, struct au_ren_args
*a
)
179 struct inode
*delegated
;
181 a
->h_path
.dentry
= vfsub_lkup_one(&a
->dst_dentry
->d_name
,
183 rerr
= PTR_ERR(a
->h_path
.dentry
);
184 if (IS_ERR(a
->h_path
.dentry
)) {
185 RevertFailure("lkup one %pd", a
->dst_dentry
);
188 if (d_is_positive(a
->h_path
.dentry
)) {
189 d_drop(a
->h_path
.dentry
);
190 dput(a
->h_path
.dentry
);
195 rerr
= vfsub_rename(a
->dst_h_dir
, a
->h_dst
, a
->dst_h_dir
, &a
->h_path
,
196 &delegated
, a
->flags
);
197 if (unlikely(rerr
== -EWOULDBLOCK
)) {
198 pr_warn("cannot retry for NFSv4 delegation"
199 " for an internal rename\n");
202 d_drop(a
->h_path
.dentry
);
203 dput(a
->h_path
.dentry
);
205 au_set_h_dptr(a
->dst_dentry
, a
->btgt
, dget(a
->h_dst
));
207 RevertFailure("rename %pd", a
->h_dst
);
210 static void au_ren_rev_whsrc(int err
, struct au_ren_args
*a
)
214 a
->h_path
.dentry
= a
->src_wh_dentry
;
215 rerr
= au_wh_unlink_dentry(a
->src_h_dir
, &a
->h_path
, a
->src_dentry
);
216 au_set_dbwh(a
->src_dentry
, a
->src_bwh
);
218 RevertFailure("unlink %pd", a
->src_wh_dentry
);
222 /* ---------------------------------------------------------------------- */
225 * when we have to copyup the renaming entry, do it with the rename-target name
226 * in order to minimize the cost (the later actual rename is unnecessary).
227 * otherwise rename it on the target branch.
229 static int au_ren_or_cpup(struct au_ren_args
*a
)
233 struct inode
*delegated
;
236 if (au_dbtop(d
) == a
->btgt
) {
237 a
->h_path
.dentry
= a
->dst_h_dentry
;
238 AuDebugOn(au_dbtop(d
) != a
->btgt
);
240 err
= vfsub_rename(a
->src_h_dir
, au_h_dptr(d
, a
->btgt
),
241 a
->dst_h_dir
, &a
->h_path
, &delegated
,
243 if (unlikely(err
== -EWOULDBLOCK
)) {
244 pr_warn("cannot retry for NFSv4 delegation"
245 " for an internal rename\n");
251 if (!err
&& a
->h_dst
)
252 /* it will be set to dinfo later */
258 /* cf. aufs_rmdir() */
259 static int au_ren_del_whtmp(struct au_ren_args
*a
)
265 SiMustAnyLock(dir
->i_sb
);
266 if (!au_nhash_test_longer_wh(&a
->whlist
, a
->btgt
,
267 au_sbi(dir
->i_sb
)->si_dirwh
)
268 || au_test_fs_remote(a
->h_dst
->d_sb
)) {
269 err
= au_whtmp_rmdir(dir
, a
->btgt
, a
->h_dst
, &a
->whlist
);
271 pr_warn("failed removing whtmp dir %pd (%d), "
272 "ignored.\n", a
->h_dst
, err
);
274 au_nhash_wh_free(&a
->thargs
->whlist
);
275 a
->thargs
->whlist
= a
->whlist
;
276 a
->whlist
.nh_num
= 0;
277 au_whtmp_kick_rmdir(dir
, a
->btgt
, a
->h_dst
, a
->thargs
);
285 /* make it 'opaque' dir. */
286 static int au_ren_do_diropq(struct au_ren_args
*a
, int idx
)
289 struct dentry
*d
, *diropq
;
290 #define src_or_dst(member) a->sd[idx].member
293 d
= src_or_dst(dentry
); /* {src,dst}_dentry */
294 src_or_dst(bdiropq
) = au_dbdiropq(d
);
295 src_or_dst(hinode
) = au_hi(src_or_dst(inode
), a
->btgt
);
296 au_hn_inode_lock_nested(src_or_dst(hinode
), AuLsc_I_CHILD
);
297 diropq
= au_diropq_create(d
, a
->btgt
);
298 au_hn_inode_unlock(src_or_dst(hinode
));
300 err
= PTR_ERR(diropq
);
308 static int au_ren_diropq(struct au_ren_args
*a
)
311 unsigned char always
;
315 d
= a
->dst_dentry
; /* already renamed on the branch */
316 always
= !!au_opt_test(au_mntflags(d
->d_sb
), ALWAYS_DIROPQ
);
317 if (au_ftest_ren(a
->auren_flags
, ISDIR_SRC
)
318 && !au_ftest_ren(a
->auren_flags
, DIRREN
)
319 && a
->btgt
!= au_dbdiropq(a
->src_dentry
)
321 || a
->btgt
<= au_dbdiropq(d
)
322 /* hide the lower to keep xino */
323 /* the lowers may not be a dir, but we hide them anyway */
324 || a
->btgt
< au_dbbot(d
)
327 err
= au_ren_do_diropq(a
, AuSRC
);
330 au_fset_ren(a
->auren_flags
, DIROPQ_SRC
);
333 goto out
; /* success */
335 d
= a
->src_dentry
; /* already renamed on the branch */
336 if (au_ftest_ren(a
->auren_flags
, ISDIR_DST
)
337 && a
->btgt
!= au_dbdiropq(a
->dst_dentry
)
338 && (a
->btgt
< au_dbdiropq(d
)
339 || a
->btgt
< au_dbbot(d
)
341 AuDbgDentry(a
->src_dentry
);
342 AuDbgDentry(a
->dst_dentry
);
343 err
= au_ren_do_diropq(a
, AuDST
);
346 au_fset_ren(a
->auren_flags
, DIROPQ_DST
);
348 goto out
; /* success */
351 AuDbg("err %d, reverting src\n", err
);
352 au_ren_rev_diropq(err
, a
);
357 static int do_rename(struct au_ren_args
*a
)
360 struct dentry
*d
, *h_d
;
363 /* prepare workqueue args for asynchronous rmdir */
364 h_d
= a
->dst_h_dentry
;
365 if (au_ftest_ren(a
->auren_flags
, ISDIR_DST
)
366 /* && !au_ftest_ren(a->auren_flags, DIRREN) */
367 && d_is_positive(h_d
)) {
369 a
->thargs
= au_whtmp_rmdir_alloc(a
->src_dentry
->d_sb
,
371 if (unlikely(!a
->thargs
))
373 a
->h_dst
= dget(h_d
);
376 /* create whiteout for src_dentry */
377 if (au_ftest_ren(a
->auren_flags
, WHSRC
)) {
378 a
->src_bwh
= au_dbwh(a
->src_dentry
);
379 AuDebugOn(a
->src_bwh
>= 0);
380 a
->src_wh_dentry
= au_wh_create(a
->src_dentry
, a
->btgt
,
382 err
= PTR_ERR(a
->src_wh_dentry
);
383 if (IS_ERR(a
->src_wh_dentry
))
387 /* lookup whiteout for dentry */
388 if (au_ftest_ren(a
->auren_flags
, WHDST
)) {
389 h_d
= au_wh_lkup(a
->dst_h_parent
,
390 &a
->dst_dentry
->d_name
, a
->br
);
394 if (d_is_negative(h_d
))
397 a
->dst_wh_dentry
= h_d
;
400 /* rename dentry to tmpwh */
402 err
= au_whtmp_ren(a
->dst_h_dentry
, a
->br
);
407 au_set_h_dptr(d
, a
->btgt
, NULL
);
408 err
= au_lkup_neg(d
, a
->btgt
, /*wh*/0);
411 a
->dst_h_dentry
= au_h_dptr(d
, a
->btgt
);
415 BUG_ON(d_is_positive(a
->dst_h_dentry
) && a
->src_btop
!= a
->btgt
);
417 BUG_ON(!au_ftest_ren(a
->auren_flags
, DIRREN
)
418 && d_is_positive(a
->dst_h_dentry
)
419 && a
->src_btop
!= a
->btgt
);
422 /* rename by vfs_rename or cpup */
423 err
= au_ren_or_cpup(a
);
425 /* leave the copied-up one */
428 /* make dir opaque */
429 err
= au_ren_diropq(a
);
433 /* update target timestamps */
435 AuDebugOn(au_dbtop(a
->dst_dentry
) != a
->btgt
);
436 a
->h_path
.dentry
= au_h_dptr(a
->dst_dentry
, a
->btgt
);
437 vfsub_update_h_iattr(&a
->h_path
, /*did*/NULL
); /*ignore*/
438 a
->dst_inode
->i_ctime
= d_inode(a
->h_path
.dentry
)->i_ctime
;
440 AuDebugOn(au_dbtop(a
->src_dentry
) != a
->btgt
);
441 a
->h_path
.dentry
= au_h_dptr(a
->src_dentry
, a
->btgt
);
442 vfsub_update_h_iattr(&a
->h_path
, /*did*/NULL
); /*ignore*/
443 a
->src_inode
->i_ctime
= d_inode(a
->h_path
.dentry
)->i_ctime
;
446 /* remove whiteout for dentry */
447 if (a
->dst_wh_dentry
) {
448 a
->h_path
.dentry
= a
->dst_wh_dentry
;
449 err
= au_wh_unlink_dentry(a
->dst_h_dir
, &a
->h_path
,
457 au_ren_del_whtmp(a
); /* ignore this error */
459 au_fhsm_wrote(a
->src_dentry
->d_sb
, a
->btgt
, /*force*/0);
465 au_ren_rev_diropq(err
, a
);
467 au_ren_rev_rename(err
, a
);
471 au_ren_rev_whtmp(err
, a
);
473 dput(a
->dst_wh_dentry
);
474 a
->dst_wh_dentry
= NULL
;
476 if (a
->src_wh_dentry
)
477 au_ren_rev_whsrc(err
, a
);
479 dput(a
->src_wh_dentry
);
480 dput(a
->dst_wh_dentry
);
484 au_whtmp_rmdir_free(a
->thargs
);
491 /* ---------------------------------------------------------------------- */
494 * test if @dentry dir can be rename destination or not.
495 * success means, it is a logically empty dir.
497 static int may_rename_dstdir(struct dentry
*dentry
, struct au_nhash
*whlist
)
499 return au_test_empty(dentry
, whlist
);
503 * test if @a->src_dentry dir can be rename source or not.
504 * if it can, return 0.
506 * - it is a logically empty dir.
507 * - or, it exists on writable branch and has no children including whiteouts
508 * on the lower branch unless DIRREN is on.
510 static int may_rename_srcdir(struct au_ren_args
*a
)
514 aufs_bindex_t btop
, btgt
;
515 struct dentry
*dentry
;
516 struct super_block
*sb
;
517 struct au_sbinfo
*sbinfo
;
519 dentry
= a
->src_dentry
;
522 if (au_opt_test(sbinfo
->si_mntflags
, DIRREN
))
523 au_fset_ren(a
->auren_flags
, DIRREN
);
526 btop
= au_dbtop(dentry
);
528 struct au_nhash whlist
;
531 rdhash
= sbinfo
->si_rdhash
;
533 rdhash
= au_rdhash_est(au_dir_size(/*file*/NULL
,
535 err
= au_nhash_alloc(&whlist
, rdhash
, GFP_NOFS
);
538 err
= au_test_empty(dentry
, &whlist
);
539 au_nhash_wh_free(&whlist
);
543 if (btop
== au_dbtaildir(dentry
))
544 return 0; /* success */
546 err
= au_test_empty_lower(dentry
);
549 if (err
== -ENOTEMPTY
) {
550 if (au_ftest_ren(a
->auren_flags
, DIRREN
)) {
553 AuWarn1("renaming dir who has child(ren) on multiple "
554 "branches, is not supported\n");
561 /* side effect: sets whlist and h_dentry */
562 static int au_ren_may_dir(struct au_ren_args
*a
)
569 SiMustAnyLock(d
->d_sb
);
572 if (au_ftest_ren(a
->auren_flags
, ISDIR_DST
) && a
->dst_inode
) {
573 rdhash
= au_sbi(d
->d_sb
)->si_rdhash
;
575 rdhash
= au_rdhash_est(au_dir_size(/*file*/NULL
, d
));
576 err
= au_nhash_alloc(&a
->whlist
, rdhash
, GFP_NOFS
);
581 au_set_dbtop(d
, a
->dst_btop
);
582 err
= may_rename_dstdir(d
, &a
->whlist
);
583 au_set_dbtop(d
, a
->btgt
);
585 err
= may_rename_srcdir(a
);
587 a
->dst_h_dentry
= au_h_dptr(d
, au_dbtop(d
));
592 a
->src_h_dentry
= au_h_dptr(d
, au_dbtop(d
));
593 if (au_ftest_ren(a
->auren_flags
, ISDIR_SRC
)) {
594 err
= may_rename_srcdir(a
);
596 au_nhash_wh_free(&a
->whlist
);
597 a
->whlist
.nh_num
= 0;
604 /* ---------------------------------------------------------------------- */
607 * simple tests for rename.
608 * following the checks in vfs, plus the parent-child relationship.
610 static int au_may_ren(struct au_ren_args
*a
)
613 struct inode
*h_inode
;
615 if (a
->src_btop
== a
->btgt
) {
616 err
= au_may_del(a
->src_dentry
, a
->btgt
, a
->src_h_parent
,
617 au_ftest_ren(a
->auren_flags
, ISDIR_SRC
));
621 if (unlikely(a
->src_h_dentry
== a
->h_trap
))
626 if (a
->dst_btop
!= a
->btgt
)
630 if (unlikely(a
->dst_h_dentry
== a
->h_trap
))
634 isdir
= !!au_ftest_ren(a
->auren_flags
, ISDIR_DST
);
635 if (d_really_is_negative(a
->dst_dentry
)) {
636 if (d_is_negative(a
->dst_h_dentry
))
637 err
= au_may_add(a
->dst_dentry
, a
->btgt
,
638 a
->dst_h_parent
, isdir
);
640 if (unlikely(d_is_negative(a
->dst_h_dentry
)))
642 h_inode
= d_inode(a
->dst_h_dentry
);
643 if (h_inode
->i_nlink
)
644 err
= au_may_del(a
->dst_dentry
, a
->btgt
,
645 a
->dst_h_parent
, isdir
);
649 if (unlikely(err
== -ENOENT
|| err
== -EEXIST
))
655 /* ---------------------------------------------------------------------- */
660 * - src_dir and dir by lock_rename()
664 * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
666 * + di_write_lock2_child()
667 * + di_write_lock_child()
668 * + ii_write_lock_child()
669 * + di_write_lock_child2()
670 * + ii_write_lock_child2()
671 * + src_parent and parent
672 * + di_write_lock_parent()
673 * + ii_write_lock_parent()
674 * + di_write_lock_parent2()
675 * + ii_write_lock_parent2()
676 * + lower src_dir and dir by vfsub_lock_rename()
677 * + verify the every relationships between child and parent. if any
678 * of them failed, unlock all and return -EBUSY.
680 static void au_ren_unlock(struct au_ren_args
*a
)
682 vfsub_unlock_rename(a
->src_h_parent
, a
->src_hdir
,
683 a
->dst_h_parent
, a
->dst_hdir
);
684 if (au_ftest_ren(a
->auren_flags
, DIRREN
)
686 au_hn_inode_unlock(a
->h_root
);
687 if (au_ftest_ren(a
->auren_flags
, MNT_WRITE
))
688 vfsub_mnt_drop_write(au_br_mnt(a
->br
));
691 static int au_ren_lock(struct au_ren_args
*a
)
697 a
->src_h_parent
= au_h_dptr(a
->src_parent
, a
->btgt
);
698 a
->src_hdir
= au_hi(a
->src_dir
, a
->btgt
);
699 a
->dst_h_parent
= au_h_dptr(a
->dst_parent
, a
->btgt
);
700 a
->dst_hdir
= au_hi(a
->dst_dir
, a
->btgt
);
702 err
= vfsub_mnt_want_write(au_br_mnt(a
->br
));
705 au_fset_ren(a
->auren_flags
, MNT_WRITE
);
706 if (au_ftest_ren(a
->auren_flags
, DIRREN
)) {
711 * sbinfo is already locked, so this ii_read_lock is
712 * unnecessary. but our debugging feature checks it.
714 root
= a
->src_inode
->i_sb
->s_root
;
715 if (root
!= a
->src_parent
&& root
!= a
->dst_parent
) {
717 ii_read_lock_parent3(dir
);
718 a
->h_root
= au_hi(dir
, a
->btgt
);
720 au_hn_inode_lock_nested(a
->h_root
, AuLsc_I_PARENT3
);
723 a
->h_trap
= vfsub_lock_rename(a
->src_h_parent
, a
->src_hdir
,
724 a
->dst_h_parent
, a
->dst_hdir
);
725 udba
= au_opt_udba(a
->src_dentry
->d_sb
);
726 if (unlikely(a
->src_hdir
->hi_inode
!= d_inode(a
->src_h_parent
)
727 || a
->dst_hdir
->hi_inode
!= d_inode(a
->dst_h_parent
)))
728 err
= au_busy_or_stale();
729 if (!err
&& au_dbtop(a
->src_dentry
) == a
->btgt
)
730 err
= au_h_verify(a
->src_h_dentry
, udba
,
731 d_inode(a
->src_h_parent
), a
->src_h_parent
,
733 if (!err
&& au_dbtop(a
->dst_dentry
) == a
->btgt
)
734 err
= au_h_verify(a
->dst_h_dentry
, udba
,
735 d_inode(a
->dst_h_parent
), a
->dst_h_parent
,
738 goto out
; /* success */
740 err
= au_busy_or_stale();
747 /* ---------------------------------------------------------------------- */
749 static void au_ren_refresh_dir(struct au_ren_args
*a
)
755 if (au_ftest_ren(a
->auren_flags
, ISDIR_SRC
)) {
756 /* is this updating defined in POSIX? */
757 au_cpup_attr_timesizes(a
->src_inode
);
758 au_cpup_attr_nlink(dir
, /*force*/1);
760 au_dir_ts(dir
, a
->btgt
);
765 if (au_ftest_ren(a
->auren_flags
, ISDIR_DST
)) {
766 /* is this updating defined in POSIX? */
767 au_cpup_attr_timesizes(a
->dst_inode
);
768 au_cpup_attr_nlink(dir
, /*force*/1);
770 au_dir_ts(dir
, a
->btgt
);
773 if (au_ftest_ren(a
->auren_flags
, ISSAMEDIR
))
778 if (au_ftest_ren(a
->auren_flags
, ISDIR_SRC
))
779 au_cpup_attr_nlink(dir
, /*force*/1);
780 au_dir_ts(dir
, a
->btgt
);
783 static void au_ren_refresh(struct au_ren_args
*a
)
785 aufs_bindex_t bbot
, bindex
;
786 struct dentry
*d
, *h_d
;
787 struct inode
*i
, *h_i
;
788 struct super_block
*sb
;
793 /* already dget-ed by au_ren_or_cpup() */
794 au_set_h_dptr(d
, a
->btgt
, a
->h_dst
);
799 if (!au_ftest_ren(a
->auren_flags
, ISDIR_DST
))
803 au_cpup_attr_timesizes(i
);
805 au_update_dbrange(d
, /*do_put_zero*/1);
807 au_cpup_attr_nlink(i
, /*force*/1);
810 for (bindex
= au_dbtop(d
); bindex
< bbot
; bindex
++)
811 au_set_h_dptr(d
, bindex
, NULL
);
813 for (bindex
= a
->btgt
+ 1; bindex
<= bbot
; bindex
++)
814 au_set_h_dptr(d
, bindex
, NULL
);
815 au_update_dbrange(d
, /*do_put_zero*/0);
819 || au_ftest_ren(a
->auren_flags
, DIRREN
)) {
820 d_drop(a
->src_dentry
);
821 if (au_ftest_ren(a
->auren_flags
, DIRREN
))
822 au_set_dbwh(a
->src_dentry
, -1);
829 for (bindex
= a
->btgt
+ 1; bindex
<= bbot
; bindex
++) {
830 h_d
= au_h_dptr(d
, bindex
);
832 au_set_h_dptr(d
, bindex
, NULL
);
834 au_set_dbbot(d
, a
->btgt
);
838 if (au_opt_test(au_mntflags(sb
), PLINK
) && au_plink_test(i
))
839 return; /* success */
842 for (bindex
= a
->btgt
+ 1; bindex
<= bbot
; bindex
++) {
843 h_i
= au_h_iptr(i
, bindex
);
845 au_xino_write(sb
, bindex
, h_i
->i_ino
, /*ino*/0);
846 /* ignore this error */
847 au_set_h_iptr(i
, bindex
, NULL
, 0);
850 au_set_ibbot(i
, a
->btgt
);
853 /* ---------------------------------------------------------------------- */
855 /* mainly for link(2) and rename(2) */
856 int au_wbr(struct dentry
*dentry
, aufs_bindex_t btgt
)
858 aufs_bindex_t bdiropq
, bwh
;
859 struct dentry
*parent
;
860 struct au_branch
*br
;
862 parent
= dentry
->d_parent
;
863 IMustLock(d_inode(parent
)); /* dir is locked */
865 bdiropq
= au_dbdiropq(parent
);
866 bwh
= au_dbwh(dentry
);
867 br
= au_sbr(dentry
->d_sb
, btgt
);
869 || (0 <= bdiropq
&& bdiropq
< btgt
)
870 || (0 <= bwh
&& bwh
< btgt
))
873 AuDbg("btgt %d\n", btgt
);
877 /* sets src_btop, dst_btop and btgt */
878 static int au_ren_wbr(struct au_ren_args
*a
)
881 struct au_wr_dir_args wr_dir_args
= {
882 /* .force_btgt = -1, */
883 .flags
= AuWrDir_ADD_ENTRY
886 a
->src_btop
= au_dbtop(a
->src_dentry
);
887 a
->dst_btop
= au_dbtop(a
->dst_dentry
);
888 if (au_ftest_ren(a
->auren_flags
, ISDIR_SRC
)
889 || au_ftest_ren(a
->auren_flags
, ISDIR_DST
))
890 au_fset_wrdir(wr_dir_args
.flags
, ISDIR
);
891 wr_dir_args
.force_btgt
= a
->src_btop
;
892 if (a
->dst_inode
&& a
->dst_btop
< a
->src_btop
)
893 wr_dir_args
.force_btgt
= a
->dst_btop
;
894 wr_dir_args
.force_btgt
= au_wbr(a
->dst_dentry
, wr_dir_args
.force_btgt
);
895 err
= au_wr_dir(a
->dst_dentry
, a
->src_dentry
, &wr_dir_args
);
898 au_update_dbtop(a
->dst_dentry
);
903 static void au_ren_dt(struct au_ren_args
*a
)
905 a
->h_path
.dentry
= a
->src_h_parent
;
906 au_dtime_store(a
->src_dt
+ AuPARENT
, a
->src_parent
, &a
->h_path
);
907 if (!au_ftest_ren(a
->auren_flags
, ISSAMEDIR
)) {
908 a
->h_path
.dentry
= a
->dst_h_parent
;
909 au_dtime_store(a
->dst_dt
+ AuPARENT
, a
->dst_parent
, &a
->h_path
);
912 au_fclr_ren(a
->auren_flags
, DT_DSTDIR
);
913 if (!au_ftest_ren(a
->auren_flags
, ISDIR_SRC
)
917 a
->h_path
.dentry
= a
->src_h_dentry
;
918 au_dtime_store(a
->src_dt
+ AuCHILD
, a
->src_dentry
, &a
->h_path
);
919 if (d_is_positive(a
->dst_h_dentry
)) {
920 au_fset_ren(a
->auren_flags
, DT_DSTDIR
);
921 a
->h_path
.dentry
= a
->dst_h_dentry
;
922 au_dtime_store(a
->dst_dt
+ AuCHILD
, a
->dst_dentry
, &a
->h_path
);
926 static void au_ren_rev_dt(int err
, struct au_ren_args
*a
)
929 struct inode
*h_inode
;
931 au_dtime_revert(a
->src_dt
+ AuPARENT
);
932 if (!au_ftest_ren(a
->auren_flags
, ISSAMEDIR
))
933 au_dtime_revert(a
->dst_dt
+ AuPARENT
);
935 if (au_ftest_ren(a
->auren_flags
, ISDIR_SRC
) && err
!= -EIO
) {
936 h_d
= a
->src_dt
[AuCHILD
].dt_h_path
.dentry
;
937 h_inode
= d_inode(h_d
);
938 inode_lock_nested(h_inode
, AuLsc_I_CHILD
);
939 au_dtime_revert(a
->src_dt
+ AuCHILD
);
940 inode_unlock(h_inode
);
942 if (au_ftest_ren(a
->auren_flags
, DT_DSTDIR
)) {
943 h_d
= a
->dst_dt
[AuCHILD
].dt_h_path
.dentry
;
944 h_inode
= d_inode(h_d
);
945 inode_lock_nested(h_inode
, AuLsc_I_CHILD
);
946 au_dtime_revert(a
->dst_dt
+ AuCHILD
);
947 inode_unlock(h_inode
);
952 /* ---------------------------------------------------------------------- */
954 int aufs_rename(struct inode
*_src_dir
, struct dentry
*_src_dentry
,
955 struct inode
*_dst_dir
, struct dentry
*_dst_dentry
,
960 /* reduce stack space */
961 struct au_ren_args
*a
;
964 AuDbg("%pd, %pd, 0x%x\n", _src_dentry
, _dst_dentry
, _flags
);
969 if (unlikely(_flags
& RENAME_WHITEOUT
))
973 BUILD_BUG_ON(sizeof(*a
) > PAGE_SIZE
);
974 a
= kzalloc(sizeof(*a
), GFP_NOFS
);
979 a
->exchange
= _flags
& RENAME_EXCHANGE
;
980 a
->src_dir
= _src_dir
;
981 a
->src_dentry
= _src_dentry
;
983 if (d_really_is_positive(a
->src_dentry
))
984 a
->src_inode
= d_inode(a
->src_dentry
);
985 a
->src_parent
= a
->src_dentry
->d_parent
; /* dir inode is locked */
986 a
->dst_dir
= _dst_dir
;
987 a
->dst_dentry
= _dst_dentry
;
989 if (d_really_is_positive(a
->dst_dentry
))
990 a
->dst_inode
= d_inode(a
->dst_dentry
);
991 a
->dst_parent
= a
->dst_dentry
->d_parent
; /* dir inode is locked */
994 * if EXCHANGE && src is non-dir && dst is dir,
997 /* IMustLock(a->dst_inode); */
998 au_igrab(a
->dst_inode
);
1002 lock_flags
= AuLock_FLUSH
| AuLock_NOPLM
| AuLock_GEN
;
1003 if (d_is_dir(a
->src_dentry
)) {
1004 au_fset_ren(a
->auren_flags
, ISDIR_SRC
);
1005 if (unlikely(!a
->exchange
1006 && d_really_is_positive(a
->dst_dentry
)
1007 && !d_is_dir(a
->dst_dentry
)))
1009 lock_flags
|= AuLock_DIRS
;
1011 if (a
->dst_inode
&& d_is_dir(a
->dst_dentry
)) {
1012 au_fset_ren(a
->auren_flags
, ISDIR_DST
);
1013 if (unlikely(!a
->exchange
1014 && d_really_is_positive(a
->src_dentry
)
1015 && !d_is_dir(a
->src_dentry
)))
1017 lock_flags
|= AuLock_DIRS
;
1019 err
= aufs_read_and_write_lock2(a
->dst_dentry
, a
->src_dentry
,
1024 err
= au_d_hashed_positive(a
->src_dentry
);
1030 * If it is a dir, VFS unhash it before this
1031 * function. It means we cannot rely upon d_unhashed().
1033 if (unlikely(!a
->dst_inode
->i_nlink
))
1035 if (!au_ftest_ren(a
->auren_flags
, ISDIR_DST
)) {
1036 err
= au_d_hashed_positive(a
->dst_dentry
);
1037 if (unlikely(err
&& !a
->exchange
))
1039 } else if (unlikely(IS_DEADDIR(a
->dst_inode
)))
1041 } else if (unlikely(d_unhashed(a
->dst_dentry
)))
1046 * yes, it happened (in linux-3.3-rcN) but I don't know why.
1047 * there may exist a problem somewhere else.
1050 if (unlikely(d_inode(a
->dst_parent
) == d_inode(a
->src_dentry
)))
1053 au_fset_ren(a
->auren_flags
, ISSAMEDIR
); /* temporary */
1054 di_write_lock_parent(a
->dst_parent
);
1056 /* which branch we process */
1057 err
= au_ren_wbr(a
);
1058 if (unlikely(err
< 0))
1060 a
->br
= au_sbr(a
->dst_dentry
->d_sb
, a
->btgt
);
1061 a
->h_path
.mnt
= au_br_mnt(a
->br
);
1063 /* are they available to be renamed */
1064 err
= au_ren_may_dir(a
);
1068 /* prepare the writable parent dir on the same branch */
1069 if (a
->dst_btop
== a
->btgt
) {
1070 au_fset_ren(a
->auren_flags
, WHDST
);
1072 err
= au_cpup_dirs(a
->dst_dentry
, a
->btgt
);
1079 if (a
->src_dir
!= a
->dst_dir
) {
1081 * this temporary unlock is safe,
1082 * because both dir->i_mutex are locked.
1084 di_write_unlock(a
->dst_parent
);
1085 di_write_lock_parent(a
->src_parent
);
1086 err
= au_wr_dir_need_wh(a
->src_dentry
,
1087 au_ftest_ren(a
->auren_flags
,
1090 di_write_unlock(a
->src_parent
);
1091 di_write_lock2_parent(a
->src_parent
, a
->dst_parent
,
1093 au_fclr_ren(a
->auren_flags
, ISSAMEDIR
);
1095 err
= au_wr_dir_need_wh(a
->src_dentry
,
1096 au_ftest_ren(a
->auren_flags
,
1100 if (unlikely(err
< 0))
1103 au_fset_ren(a
->auren_flags
, WHSRC
);
1106 if (a
->src_btop
!= a
->btgt
) {
1107 err
= au_pin(&pin
, a
->src_dentry
, a
->btgt
,
1108 au_opt_udba(a
->src_dentry
->d_sb
),
1109 AuPin_DI_LOCKED
| AuPin_MNT_WRITE
);
1111 struct au_cp_generic cpg
= {
1112 .dentry
= a
->src_dentry
,
1114 .bsrc
= a
->src_btop
,
1117 .flags
= AuCpup_DTIME
| AuCpup_HOPEN
1119 AuDebugOn(au_dbtop(a
->src_dentry
) != a
->src_btop
);
1120 err
= au_sio_cpup_simple(&cpg
);
1125 a
->src_btop
= a
->btgt
;
1126 a
->src_h_dentry
= au_h_dptr(a
->src_dentry
, a
->btgt
);
1128 au_fset_ren(a
->auren_flags
, WHSRC
);
1132 if (a
->exchange
&& a
->dst_inode
1133 && a
->dst_btop
!= a
->btgt
) {
1134 err
= au_pin(&pin
, a
->dst_dentry
, a
->btgt
,
1135 au_opt_udba(a
->dst_dentry
->d_sb
),
1136 AuPin_DI_LOCKED
| AuPin_MNT_WRITE
);
1138 struct au_cp_generic cpg
= {
1139 .dentry
= a
->dst_dentry
,
1141 .bsrc
= a
->dst_btop
,
1144 .flags
= AuCpup_DTIME
| AuCpup_HOPEN
1146 err
= au_sio_cpup_simple(&cpg
);
1151 a
->dst_btop
= a
->btgt
;
1152 a
->dst_h_dentry
= au_h_dptr(a
->dst_dentry
, a
->btgt
);
1156 err
= au_ren_lock(a
);
1158 /* leave the copied-up one */
1162 if (!au_opt_test(au_mntflags(a
->dst_dir
->i_sb
), UDBA_NONE
))
1163 err
= au_may_ren(a
);
1164 else if (unlikely(a
->dst_dentry
->d_name
.len
> AUFS_MAX_NAMELEN
))
1165 err
= -ENAMETOOLONG
;
1170 /* store timestamps to be revertible */
1173 /* store dirren info */
1174 if (au_ftest_ren(a
->auren_flags
, DIRREN
)) {
1175 err
= au_dr_rename(a
->src_dentry
, a
->btgt
,
1176 &a
->dst_dentry
->d_name
, &rev
);
1187 if (au_ftest_ren(a
->auren_flags
, DIRREN
))
1188 au_dr_rename_fin(a
->src_dentry
, a
->btgt
, rev
);
1190 /* update dir attributes */
1191 au_ren_refresh_dir(a
);
1193 /* dput/iput all lower dentries */
1196 goto out_hdir
; /* success */
1199 if (au_ftest_ren(a
->auren_flags
, DIRREN
))
1200 au_dr_rename_rev(a
->src_dentry
, a
->btgt
, rev
);
1202 au_ren_rev_dt(err
, a
);
1206 au_nhash_wh_free(&a
->whlist
);
1207 if (err
&& a
->dst_inode
&& a
->dst_btop
!= a
->btgt
) {
1208 AuDbg("btop %d, btgt %d\n", a
->dst_btop
, a
->btgt
);
1209 au_set_h_dptr(a
->dst_dentry
, a
->btgt
, NULL
);
1210 au_set_dbtop(a
->dst_dentry
, a
->dst_btop
);
1214 if (d_unhashed(a
->src_dentry
))
1215 au_fset_ren(a
->auren_flags
, DROPPED_SRC
);
1216 if (d_unhashed(a
->dst_dentry
))
1217 au_fset_ren(a
->auren_flags
, DROPPED_DST
);
1219 d_move(a
->src_dentry
, a
->dst_dentry
);
1221 d_exchange(a
->src_dentry
, a
->dst_dentry
);
1222 if (au_ftest_ren(a
->auren_flags
, DROPPED_DST
))
1223 d_drop(a
->dst_dentry
);
1225 if (au_ftest_ren(a
->auren_flags
, DROPPED_SRC
))
1226 d_drop(a
->src_dentry
);
1228 au_update_dbtop(a
->dst_dentry
);
1230 d_drop(a
->dst_dentry
);
1232 if (au_ftest_ren(a
->auren_flags
, ISSAMEDIR
))
1233 di_write_unlock(a
->dst_parent
);
1235 di_write_unlock2(a
->src_parent
, a
->dst_parent
);
1237 aufs_read_and_write_unlock2(a
->dst_dentry
, a
->src_dentry
);
1241 au_whtmp_rmdir_free(a
->thargs
);