2 * Copyright (C) 2005-2013 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, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * inode operation (rename entry)
21 * todo: this is crazy monster
26 enum { AuSRC, AuDST, AuSrcDst };
27 enum { AuPARENT, AuCHILD, AuParentChild };
30 #define AuRen_ISSAMEDIR (1 << 1)
31 #define AuRen_WHSRC (1 << 2)
32 #define AuRen_WHDST (1 << 3)
33 #define AuRen_MNT_WRITE (1 << 4)
34 #define AuRen_DT_DSTDIR (1 << 5)
35 #define AuRen_DIROPQ (1 << 6)
36 #define AuRen_CPUP (1 << 7)
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;
49 struct au_dtime dt[AuParentChild];
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_h_dir sd[AuSRC].hdir->hi_inode
62 #define src_dt sd[AuSRC].dt
63 #define src_bstart sd[AuSRC].bstart
65 #define dst_dentry sd[AuDST].dentry
66 #define dst_dir sd[AuDST].dir
67 #define dst_inode sd[AuDST].inode
68 #define dst_h_dentry sd[AuDST].h_dentry
69 #define dst_parent sd[AuDST].parent
70 #define dst_h_parent sd[AuDST].h_parent
71 #define dst_wh_dentry sd[AuDST].wh_dentry
72 #define dst_hdir sd[AuDST].hdir
73 #define dst_h_dir sd[AuDST].hdir->hi_inode
74 #define dst_dt sd[AuDST].dt
75 #define dst_bstart sd[AuDST].bstart
77 struct dentry *h_trap;
79 struct au_hinode *src_hinode;
81 struct au_nhash whlist;
82 aufs_bindex_t btgt, src_bwh, src_bdiropq;
86 struct au_whtmp_rmdir *thargs;
90 /* ---------------------------------------------------------------------- */
93 * functions for reverting.
94 * when an error happened in a single rename systemcall, we should revert
95 * everything as if nothing happend.
96 * we don't need to revert the copied-up/down the parent dir since they are
100 #define RevertFailure(fmt, ...) do { \
101 AuIOErr("revert failure: " fmt " (%d, %d)\n", \
102 ##__VA_ARGS__, err, rerr); \
106 static void au_ren_rev_diropq(int err, struct au_ren_args *a)
110 au_hn_imtx_lock_nested(a->src_hinode, AuLsc_I_CHILD);
111 rerr = au_diropq_remove(a->src_dentry, a->btgt);
112 au_hn_imtx_unlock(a->src_hinode);
113 au_set_dbdiropq(a->src_dentry, a->src_bdiropq);
115 RevertFailure("remove diropq %.*s", AuDLNPair(a->src_dentry));
118 static void au_ren_rev_rename(int err, struct au_ren_args *a)
122 a->h_path.dentry = au_lkup_one(&a->src_dentry->d_name, a->src_h_parent,
124 rerr = PTR_ERR(a->h_path.dentry);
125 if (IS_ERR(a->h_path.dentry)) {
126 RevertFailure("au_lkup_one %.*s", AuDLNPair(a->src_dentry));
130 rerr = vfsub_rename(a->dst_h_dir,
131 au_h_dptr(a->src_dentry, a->btgt),
132 a->src_h_dir, &a->h_path);
133 d_drop(a->h_path.dentry);
134 dput(a->h_path.dentry);
135 /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
137 RevertFailure("rename %.*s", AuDLNPair(a->src_dentry));
140 static void au_ren_rev_cpup(int err, struct au_ren_args *a)
144 a->h_path.dentry = a->dst_h_dentry;
145 rerr = vfsub_unlink(a->dst_h_dir, &a->h_path, /*force*/0);
146 au_set_h_dptr(a->src_dentry, a->btgt, NULL);
147 au_set_dbstart(a->src_dentry, a->src_bstart);
149 RevertFailure("unlink %.*s", AuDLNPair(a->dst_h_dentry));
152 static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
156 a->h_path.dentry = au_lkup_one(&a->dst_dentry->d_name, a->dst_h_parent,
158 rerr = PTR_ERR(a->h_path.dentry);
159 if (IS_ERR(a->h_path.dentry)) {
160 RevertFailure("lookup %.*s", AuDLNPair(a->dst_dentry));
163 if (a->h_path.dentry->d_inode) {
164 d_drop(a->h_path.dentry);
165 dput(a->h_path.dentry);
169 rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path);
170 d_drop(a->h_path.dentry);
171 dput(a->h_path.dentry);
173 au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
175 RevertFailure("rename %.*s", AuDLNPair(a->h_dst));
178 static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
182 a->h_path.dentry = a->src_wh_dentry;
183 rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
184 au_set_dbwh(a->src_dentry, a->src_bwh);
186 RevertFailure("unlink %.*s", AuDLNPair(a->src_wh_dentry));
190 /* ---------------------------------------------------------------------- */
193 * when we have to copyup the renaming entry, do it with the rename-target name
194 * in order to minimize the cost (the later actual rename is unnecessary).
195 * otherwise rename it on the target branch.
197 static int au_ren_or_cpup(struct au_ren_args *a)
203 if (au_dbstart(d) == a->btgt) {
204 a->h_path.dentry = a->dst_h_dentry;
205 if (au_ftest_ren(a->flags, DIROPQ)
206 && au_dbdiropq(d) == a->btgt)
207 au_fclr_ren(a->flags, DIROPQ);
208 AuDebugOn(au_dbstart(d) != a->btgt);
209 err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
210 a->dst_h_dir, &a->h_path);
214 if (!err && a->h_dst)
215 /* it will be set to dinfo later */
221 /* cf. aufs_rmdir() */
222 static int au_ren_del_whtmp(struct au_ren_args *a)
228 SiMustAnyLock(dir->i_sb);
229 if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
230 au_sbi(dir->i_sb)->si_dirwh)
231 || au_test_fs_remote(a->h_dst->d_sb)) {
232 err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
234 pr_warn("failed removing whtmp dir %.*s (%d), "
235 "ignored.\n", AuDLNPair(a->h_dst), err);
237 au_nhash_wh_free(&a->thargs->whlist);
238 a->thargs->whlist = a->whlist;
239 a->whlist.nh_num = 0;
240 au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
248 /* make it 'opaque' dir. */
249 static int au_ren_diropq(struct au_ren_args *a)
252 struct dentry *diropq;
255 a->src_bdiropq = au_dbdiropq(a->src_dentry);
256 a->src_hinode = au_hi(a->src_inode, a->btgt);
257 au_hn_imtx_lock_nested(a->src_hinode, AuLsc_I_CHILD);
258 diropq = au_diropq_create(a->src_dentry, a->btgt);
259 au_hn_imtx_unlock(a->src_hinode);
261 err = PTR_ERR(diropq);
267 static int do_rename(struct au_ren_args *a)
270 struct dentry *d, *h_d;
272 /* prepare workqueue args for asynchronous rmdir */
273 h_d = a->dst_h_dentry;
274 if (au_ftest_ren(a->flags, ISDIR) && h_d->d_inode) {
276 a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb, GFP_NOFS);
277 if (unlikely(!a->thargs))
279 a->h_dst = dget(h_d);
282 /* create whiteout for src_dentry */
283 if (au_ftest_ren(a->flags, WHSRC)) {
284 a->src_bwh = au_dbwh(a->src_dentry);
285 AuDebugOn(a->src_bwh >= 0);
287 = au_wh_create(a->src_dentry, a->btgt, a->src_h_parent);
288 err = PTR_ERR(a->src_wh_dentry);
289 if (IS_ERR(a->src_wh_dentry))
293 /* lookup whiteout for dentry */
294 if (au_ftest_ren(a->flags, WHDST)) {
295 h_d = au_wh_lkup(a->dst_h_parent, &a->dst_dentry->d_name,
303 a->dst_wh_dentry = h_d;
306 /* rename dentry to tmpwh */
308 err = au_whtmp_ren(a->dst_h_dentry, a->br);
313 au_set_h_dptr(d, a->btgt, NULL);
314 err = au_lkup_neg(d, a->btgt, /*wh*/0);
317 a->dst_h_dentry = au_h_dptr(d, a->btgt);
320 BUG_ON(a->dst_h_dentry->d_inode && a->src_bstart != a->btgt);
322 /* rename by vfs_rename or cpup */
324 if (au_ftest_ren(a->flags, ISDIR)
326 || au_dbdiropq(d) == a->btgt
327 /* hide the lower to keep xino */
328 || a->btgt < au_dbend(d)
329 || au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ)))
330 au_fset_ren(a->flags, DIROPQ);
331 err = au_ren_or_cpup(a);
333 /* leave the copied-up one */
336 /* make dir opaque */
337 if (au_ftest_ren(a->flags, DIROPQ)) {
338 err = au_ren_diropq(a);
343 /* update target timestamps */
344 AuDebugOn(au_dbstart(a->src_dentry) != a->btgt);
345 a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
346 vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
347 a->src_inode->i_ctime = a->h_path.dentry->d_inode->i_ctime;
349 /* remove whiteout for dentry */
350 if (a->dst_wh_dentry) {
351 a->h_path.dentry = a->dst_wh_dentry;
352 err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
360 au_ren_del_whtmp(a); /* ignore this error */
366 if (au_ftest_ren(a->flags, DIROPQ))
367 au_ren_rev_diropq(err, a);
369 if (!au_ftest_ren(a->flags, CPUP))
370 au_ren_rev_rename(err, a);
372 au_ren_rev_cpup(err, a);
376 au_ren_rev_whtmp(err, a);
378 dput(a->dst_wh_dentry);
379 a->dst_wh_dentry = NULL;
381 if (a->src_wh_dentry)
382 au_ren_rev_whsrc(err, a);
384 dput(a->src_wh_dentry);
385 dput(a->dst_wh_dentry);
389 au_whtmp_rmdir_free(a->thargs);
396 /* ---------------------------------------------------------------------- */
399 * test if @dentry dir can be rename destination or not.
400 * success means, it is a logically empty dir.
402 static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
404 return au_test_empty(dentry, whlist);
408 * test if @dentry dir can be rename source or not.
409 * if it can, return 0 and @children is filled.
411 * - it is a logically empty dir.
412 * - or, it exists on writable branch and has no children including whiteouts
413 * on the lower branch.
415 static int may_rename_srcdir(struct dentry *dentry, aufs_bindex_t btgt)
419 aufs_bindex_t bstart;
421 bstart = au_dbstart(dentry);
422 if (bstart != btgt) {
423 struct au_nhash whlist;
425 SiMustAnyLock(dentry->d_sb);
426 rdhash = au_sbi(dentry->d_sb)->si_rdhash;
428 rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
430 err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
433 err = au_test_empty(dentry, &whlist);
434 au_nhash_wh_free(&whlist);
438 if (bstart == au_dbtaildir(dentry))
439 return 0; /* success */
441 err = au_test_empty_lower(dentry);
444 if (err == -ENOTEMPTY) {
445 AuWarn1("renaming dir who has child(ren) on multiple branches,"
446 " is not supported\n");
452 /* side effect: sets whlist and h_dentry */
453 static int au_ren_may_dir(struct au_ren_args *a)
460 SiMustAnyLock(d->d_sb);
463 if (au_ftest_ren(a->flags, ISDIR) && a->dst_inode) {
464 rdhash = au_sbi(d->d_sb)->si_rdhash;
466 rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
467 err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
471 au_set_dbstart(d, a->dst_bstart);
472 err = may_rename_dstdir(d, &a->whlist);
473 au_set_dbstart(d, a->btgt);
475 a->dst_h_dentry = au_h_dptr(d, au_dbstart(d));
480 a->src_h_dentry = au_h_dptr(d, au_dbstart(d));
481 if (au_ftest_ren(a->flags, ISDIR)) {
482 err = may_rename_srcdir(d, a->btgt);
484 au_nhash_wh_free(&a->whlist);
485 a->whlist.nh_num = 0;
492 /* ---------------------------------------------------------------------- */
495 * simple tests for rename.
496 * following the checks in vfs, plus the parent-child relationship.
498 static int au_may_ren(struct au_ren_args *a)
501 struct inode *h_inode;
503 if (a->src_bstart == a->btgt) {
504 err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
505 au_ftest_ren(a->flags, ISDIR));
509 if (unlikely(a->src_h_dentry == a->h_trap))
514 if (a->dst_bstart != a->btgt)
518 if (unlikely(a->dst_h_dentry == a->h_trap))
522 h_inode = a->dst_h_dentry->d_inode;
523 isdir = !!au_ftest_ren(a->flags, ISDIR);
524 if (!a->dst_dentry->d_inode) {
525 if (unlikely(h_inode))
527 err = au_may_add(a->dst_dentry, a->btgt, a->dst_h_parent,
530 if (unlikely(!h_inode || !h_inode->i_nlink))
532 err = au_may_del(a->dst_dentry, a->btgt, a->dst_h_parent,
539 if (unlikely(err == -ENOENT || err == -EEXIST))
545 /* ---------------------------------------------------------------------- */
550 * - src_dir and dir by lock_rename()
554 * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
556 * + di_write_lock2_child()
557 * + di_write_lock_child()
558 * + ii_write_lock_child()
559 * + di_write_lock_child2()
560 * + ii_write_lock_child2()
561 * + src_parent and parent
562 * + di_write_lock_parent()
563 * + ii_write_lock_parent()
564 * + di_write_lock_parent2()
565 * + ii_write_lock_parent2()
566 * + lower src_dir and dir by vfsub_lock_rename()
567 * + verify the every relationships between child and parent. if any
568 * of them failed, unlock all and return -EBUSY.
570 static void au_ren_unlock(struct au_ren_args *a)
572 struct super_block *sb;
574 sb = a->dst_dentry->d_sb;
575 if (au_ftest_ren(a->flags, MNT_WRITE))
576 mnt_drop_write(au_br_mnt(a->br));
577 vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
578 a->dst_h_parent, a->dst_hdir);
581 static int au_ren_lock(struct au_ren_args *a)
587 a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
588 a->src_hdir = au_hi(a->src_dir, a->btgt);
589 a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
590 a->dst_hdir = au_hi(a->dst_dir, a->btgt);
591 a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
592 a->dst_h_parent, a->dst_hdir);
593 udba = au_opt_udba(a->src_dentry->d_sb);
594 if (unlikely(a->src_hdir->hi_inode != a->src_h_parent->d_inode
595 || a->dst_hdir->hi_inode != a->dst_h_parent->d_inode))
596 err = au_busy_or_stale();
597 if (!err && au_dbstart(a->src_dentry) == a->btgt)
598 err = au_h_verify(a->src_h_dentry, udba,
599 a->src_h_parent->d_inode, a->src_h_parent,
601 if (!err && au_dbstart(a->dst_dentry) == a->btgt)
602 err = au_h_verify(a->dst_h_dentry, udba,
603 a->dst_h_parent->d_inode, a->dst_h_parent,
606 err = mnt_want_write(au_br_mnt(a->br));
609 au_fset_ren(a->flags, MNT_WRITE);
610 goto out; /* success */
613 err = au_busy_or_stale();
621 /* ---------------------------------------------------------------------- */
623 static void au_ren_refresh_dir(struct au_ren_args *a)
629 if (au_ftest_ren(a->flags, ISDIR)) {
630 /* is this updating defined in POSIX? */
631 au_cpup_attr_timesizes(a->src_inode);
632 au_cpup_attr_nlink(dir, /*force*/1);
635 if (au_ibstart(dir) == a->btgt)
636 au_cpup_attr_timesizes(dir);
638 if (au_ftest_ren(a->flags, ISSAMEDIR))
643 if (au_ftest_ren(a->flags, ISDIR))
644 au_cpup_attr_nlink(dir, /*force*/1);
645 if (au_ibstart(dir) == a->btgt)
646 au_cpup_attr_timesizes(dir);
649 static void au_ren_refresh(struct au_ren_args *a)
651 aufs_bindex_t bend, bindex;
652 struct dentry *d, *h_d;
653 struct inode *i, *h_i;
654 struct super_block *sb;
659 /* already dget-ed by au_ren_or_cpup() */
660 au_set_h_dptr(d, a->btgt, a->h_dst);
664 if (!au_ftest_ren(a->flags, ISDIR))
668 au_cpup_attr_timesizes(i);
670 au_update_dbrange(d, /*do_put_zero*/1);
673 for (bindex = au_dbstart(d); bindex < bend; bindex++)
674 au_set_h_dptr(d, bindex, NULL);
676 for (bindex = a->btgt + 1; bindex <= bend; bindex++)
677 au_set_h_dptr(d, bindex, NULL);
678 au_update_dbrange(d, /*do_put_zero*/0);
684 for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
685 h_d = au_h_dptr(d, bindex);
687 au_set_h_dptr(d, bindex, NULL);
689 au_set_dbend(d, a->btgt);
693 if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
694 return; /* success */
697 for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
698 h_i = au_h_iptr(i, bindex);
700 au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
701 /* ignore this error */
702 au_set_h_iptr(i, bindex, NULL, 0);
705 au_set_ibend(i, a->btgt);
708 /* ---------------------------------------------------------------------- */
710 /* mainly for link(2) and rename(2) */
711 int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
713 aufs_bindex_t bdiropq, bwh;
714 struct dentry *parent;
715 struct au_branch *br;
717 parent = dentry->d_parent;
718 IMustLock(parent->d_inode); /* dir is locked */
720 bdiropq = au_dbdiropq(parent);
721 bwh = au_dbwh(dentry);
722 br = au_sbr(dentry->d_sb, btgt);
724 || (0 <= bdiropq && bdiropq < btgt)
725 || (0 <= bwh && bwh < btgt))
728 AuDbg("btgt %d\n", btgt);
732 /* sets src_bstart, dst_bstart and btgt */
733 static int au_ren_wbr(struct au_ren_args *a)
736 struct au_wr_dir_args wr_dir_args = {
737 /* .force_btgt = -1, */
738 .flags = AuWrDir_ADD_ENTRY
741 a->src_bstart = au_dbstart(a->src_dentry);
742 a->dst_bstart = au_dbstart(a->dst_dentry);
743 if (au_ftest_ren(a->flags, ISDIR))
744 au_fset_wrdir(wr_dir_args.flags, ISDIR);
745 wr_dir_args.force_btgt = a->src_bstart;
746 if (a->dst_inode && a->dst_bstart < a->src_bstart)
747 wr_dir_args.force_btgt = a->dst_bstart;
748 wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
749 err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
755 static void au_ren_dt(struct au_ren_args *a)
757 a->h_path.dentry = a->src_h_parent;
758 au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
759 if (!au_ftest_ren(a->flags, ISSAMEDIR)) {
760 a->h_path.dentry = a->dst_h_parent;
761 au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
764 au_fclr_ren(a->flags, DT_DSTDIR);
765 if (!au_ftest_ren(a->flags, ISDIR))
768 a->h_path.dentry = a->src_h_dentry;
769 au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
770 if (a->dst_h_dentry->d_inode) {
771 au_fset_ren(a->flags, DT_DSTDIR);
772 a->h_path.dentry = a->dst_h_dentry;
773 au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
777 static void au_ren_rev_dt(int err, struct au_ren_args *a)
782 au_dtime_revert(a->src_dt + AuPARENT);
783 if (!au_ftest_ren(a->flags, ISSAMEDIR))
784 au_dtime_revert(a->dst_dt + AuPARENT);
786 if (au_ftest_ren(a->flags, ISDIR) && err != -EIO) {
787 h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
788 h_mtx = &h_d->d_inode->i_mutex;
789 mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
790 au_dtime_revert(a->src_dt + AuCHILD);
793 if (au_ftest_ren(a->flags, DT_DSTDIR)) {
794 h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
795 h_mtx = &h_d->d_inode->i_mutex;
796 mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
797 au_dtime_revert(a->dst_dt + AuCHILD);
803 /* ---------------------------------------------------------------------- */
805 int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
806 struct inode *_dst_dir, struct dentry *_dst_dentry)
809 /* reduce stack space */
810 struct au_ren_args *a;
812 AuDbg("%.*s, %.*s\n", AuDLNPair(_src_dentry), AuDLNPair(_dst_dentry));
817 BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
818 a = kzalloc(sizeof(*a), GFP_NOFS);
822 a->src_dir = _src_dir;
823 a->src_dentry = _src_dentry;
824 a->src_inode = a->src_dentry->d_inode;
825 a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
826 a->dst_dir = _dst_dir;
827 a->dst_dentry = _dst_dentry;
828 a->dst_inode = a->dst_dentry->d_inode;
829 a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
831 IMustLock(a->dst_inode);
832 au_igrab(a->dst_inode);
836 flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
837 if (S_ISDIR(a->src_inode->i_mode)) {
838 au_fset_ren(a->flags, ISDIR);
839 if (unlikely(a->dst_inode && !S_ISDIR(a->dst_inode->i_mode)))
841 err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
844 err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
849 err = au_d_hashed_positive(a->src_dentry);
855 * If it is a dir, VFS unhash dst_dentry before this
856 * function. It means we cannot rely upon d_unhashed().
858 if (unlikely(!a->dst_inode->i_nlink))
860 if (!S_ISDIR(a->dst_inode->i_mode)) {
861 err = au_d_hashed_positive(a->dst_dentry);
864 } else if (unlikely(IS_DEADDIR(a->dst_inode)))
866 } else if (unlikely(d_unhashed(a->dst_dentry)))
871 * yes, it happend (in linux-3.3-rcN) but I don't know why.
872 * there may exist a problem somewhere else.
875 if (unlikely(a->dst_parent->d_inode == a->src_dentry->d_inode))
878 au_fset_ren(a->flags, ISSAMEDIR); /* temporary */
879 di_write_lock_parent(a->dst_parent);
881 /* which branch we process */
883 if (unlikely(err < 0))
885 a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
886 a->h_path.mnt = au_br_mnt(a->br);
888 /* are they available to be renamed */
889 err = au_ren_may_dir(a);
893 /* prepare the writable parent dir on the same branch */
894 if (a->dst_bstart == a->btgt) {
895 au_fset_ren(a->flags, WHDST);
897 err = au_cpup_dirs(a->dst_dentry, a->btgt);
902 if (a->src_dir != a->dst_dir) {
904 * this temporary unlock is safe,
905 * because both dir->i_mutex are locked.
907 di_write_unlock(a->dst_parent);
908 di_write_lock_parent(a->src_parent);
909 err = au_wr_dir_need_wh(a->src_dentry,
910 au_ftest_ren(a->flags, ISDIR),
912 di_write_unlock(a->src_parent);
913 di_write_lock2_parent(a->src_parent, a->dst_parent, /*isdir*/1);
914 au_fclr_ren(a->flags, ISSAMEDIR);
916 err = au_wr_dir_need_wh(a->src_dentry,
917 au_ftest_ren(a->flags, ISDIR),
919 if (unlikely(err < 0))
922 au_fset_ren(a->flags, WHSRC);
925 if (a->src_bstart != a->btgt) {
928 err = au_pin(&pin, a->src_dentry, a->btgt,
929 au_opt_udba(a->src_dentry->d_sb),
930 AuPin_DI_LOCKED | AuPin_MNT_WRITE);
932 struct au_cp_generic cpg = {
933 .dentry = a->src_dentry,
935 .bsrc = a->src_bstart,
938 .flags = AuCpup_DTIME | AuCpup_HOPEN
940 AuDebugOn(au_dbstart(a->src_dentry) != a->src_bstart);
941 err = au_sio_cpup_simple(&cpg);
946 a->src_bstart = a->btgt;
947 a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
948 au_fset_ren(a->flags, WHSRC);
952 err = au_ren_lock(a);
954 /* leave the copied-up one */
957 if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
959 else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
964 /* store timestamps to be revertible */
972 /* update dir attributes */
973 au_ren_refresh_dir(a);
975 /* dput/iput all lower dentries */
978 goto out_hdir; /* success */
981 au_ren_rev_dt(err, a);
985 au_nhash_wh_free(&a->whlist);
986 if (err && a->dst_inode && a->dst_bstart != a->btgt) {
987 AuDbg("bstart %d, btgt %d\n", a->dst_bstart, a->btgt);
988 au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
989 au_set_dbstart(a->dst_dentry, a->dst_bstart);
993 d_move(a->src_dentry, a->dst_dentry);
995 au_update_dbstart(a->dst_dentry);
997 d_drop(a->dst_dentry);
999 if (au_ftest_ren(a->flags, ISSAMEDIR))
1000 di_write_unlock(a->dst_parent);
1002 di_write_unlock2(a->src_parent, a->dst_parent);
1004 aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
1008 au_whtmp_rmdir_free(a->thargs);