2 * Copyright (C) 2005-2012 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);
212 struct mutex *h_mtx = &a->src_h_dentry->d_inode->i_mutex;
215 au_fset_ren(a->flags, CPUP);
216 mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
217 au_set_dbstart(d, a->btgt);
218 au_set_h_dptr(d, a->btgt, dget(a->dst_h_dentry));
219 h_file = au_h_open_pre(d, a->src_bstart);
220 if (IS_ERR(h_file)) {
221 err = PTR_ERR(h_file);
224 err = au_sio_cpup_single(d, a->btgt, a->src_bstart, -1,
225 !AuCpup_DTIME, a->dst_parent);
227 au_h_open_post(d, a->src_bstart, h_file);
230 au_set_h_dptr(d, a->btgt, NULL);
231 au_update_dbstart(d);
233 au_set_h_dptr(d, a->btgt, NULL);
234 au_set_dbstart(d, a->src_bstart);
237 if (!err && a->h_dst)
238 /* it will be set to dinfo later */
244 /* cf. aufs_rmdir() */
245 static int au_ren_del_whtmp(struct au_ren_args *a)
251 SiMustAnyLock(dir->i_sb);
252 if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
253 au_sbi(dir->i_sb)->si_dirwh)
254 || au_test_fs_remote(a->h_dst->d_sb)) {
255 err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
257 pr_warning("failed removing whtmp dir %.*s (%d), "
258 "ignored.\n", AuDLNPair(a->h_dst), err);
260 au_nhash_wh_free(&a->thargs->whlist);
261 a->thargs->whlist = a->whlist;
262 a->whlist.nh_num = 0;
263 au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
271 /* make it 'opaque' dir. */
272 static int au_ren_diropq(struct au_ren_args *a)
275 struct dentry *diropq;
278 a->src_bdiropq = au_dbdiropq(a->src_dentry);
279 a->src_hinode = au_hi(a->src_inode, a->btgt);
280 au_hn_imtx_lock_nested(a->src_hinode, AuLsc_I_CHILD);
281 diropq = au_diropq_create(a->src_dentry, a->btgt);
282 au_hn_imtx_unlock(a->src_hinode);
284 err = PTR_ERR(diropq);
290 static int do_rename(struct au_ren_args *a)
293 struct dentry *d, *h_d;
295 /* prepare workqueue args for asynchronous rmdir */
296 h_d = a->dst_h_dentry;
297 if (au_ftest_ren(a->flags, ISDIR) && h_d->d_inode) {
299 a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb, GFP_NOFS);
300 if (unlikely(!a->thargs))
302 a->h_dst = dget(h_d);
305 /* create whiteout for src_dentry */
306 if (au_ftest_ren(a->flags, WHSRC)) {
307 a->src_bwh = au_dbwh(a->src_dentry);
308 AuDebugOn(a->src_bwh >= 0);
310 = au_wh_create(a->src_dentry, a->btgt, a->src_h_parent);
311 err = PTR_ERR(a->src_wh_dentry);
312 if (IS_ERR(a->src_wh_dentry))
316 /* lookup whiteout for dentry */
317 if (au_ftest_ren(a->flags, WHDST)) {
318 h_d = au_wh_lkup(a->dst_h_parent, &a->dst_dentry->d_name,
326 a->dst_wh_dentry = h_d;
329 /* rename dentry to tmpwh */
331 err = au_whtmp_ren(a->dst_h_dentry, a->br);
336 au_set_h_dptr(d, a->btgt, NULL);
337 err = au_lkup_neg(d, a->btgt);
340 a->dst_h_dentry = au_h_dptr(d, a->btgt);
344 if (a->dst_h_dentry->d_inode && a->src_bstart != a->btgt) {
345 struct mutex *h_mtx = &a->src_h_dentry->d_inode->i_mutex;
348 mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
349 AuDebugOn(au_dbstart(a->src_dentry) != a->src_bstart);
350 h_file = au_h_open_pre(a->src_dentry, a->src_bstart);
351 if (IS_ERR(h_file)) {
352 err = PTR_ERR(h_file);
355 err = au_sio_cpup_simple(a->src_dentry, a->btgt, -1,
358 au_h_open_post(a->src_dentry, a->src_bstart, h_file);
363 /* rename by vfs_rename or cpup */
365 if (au_ftest_ren(a->flags, ISDIR)
367 || au_dbdiropq(d) == a->btgt
368 /* hide the lower to keep xino */
369 || a->btgt < au_dbend(d)
370 || au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ)))
371 au_fset_ren(a->flags, DIROPQ);
372 err = au_ren_or_cpup(a);
374 /* leave the copied-up one */
377 /* make dir opaque */
378 if (au_ftest_ren(a->flags, DIROPQ)) {
379 err = au_ren_diropq(a);
384 /* update target timestamps */
385 AuDebugOn(au_dbstart(a->src_dentry) != a->btgt);
386 a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
387 vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
388 a->src_inode->i_ctime = a->h_path.dentry->d_inode->i_ctime;
390 /* remove whiteout for dentry */
391 if (a->dst_wh_dentry) {
392 a->h_path.dentry = a->dst_wh_dentry;
393 err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
401 au_ren_del_whtmp(a); /* ignore this error */
407 if (au_ftest_ren(a->flags, DIROPQ))
408 au_ren_rev_diropq(err, a);
410 if (!au_ftest_ren(a->flags, CPUP))
411 au_ren_rev_rename(err, a);
413 au_ren_rev_cpup(err, a);
417 au_ren_rev_whtmp(err, a);
419 dput(a->dst_wh_dentry);
420 a->dst_wh_dentry = NULL;
422 if (a->src_wh_dentry)
423 au_ren_rev_whsrc(err, a);
425 dput(a->src_wh_dentry);
426 dput(a->dst_wh_dentry);
430 au_whtmp_rmdir_free(a->thargs);
437 /* ---------------------------------------------------------------------- */
440 * test if @dentry dir can be rename destination or not.
441 * success means, it is a logically empty dir.
443 static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
445 return au_test_empty(dentry, whlist);
449 * test if @dentry dir can be rename source or not.
450 * if it can, return 0 and @children is filled.
452 * - it is a logically empty dir.
453 * - or, it exists on writable branch and has no children including whiteouts
454 * on the lower branch.
456 static int may_rename_srcdir(struct dentry *dentry, aufs_bindex_t btgt)
460 aufs_bindex_t bstart;
462 bstart = au_dbstart(dentry);
463 if (bstart != btgt) {
464 struct au_nhash whlist;
466 SiMustAnyLock(dentry->d_sb);
467 rdhash = au_sbi(dentry->d_sb)->si_rdhash;
469 rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
471 err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
474 err = au_test_empty(dentry, &whlist);
475 au_nhash_wh_free(&whlist);
479 if (bstart == au_dbtaildir(dentry))
480 return 0; /* success */
482 err = au_test_empty_lower(dentry);
485 if (err == -ENOTEMPTY) {
486 AuWarn1("renaming dir who has child(ren) on multiple branches,"
487 " is not supported\n");
493 /* side effect: sets whlist and h_dentry */
494 static int au_ren_may_dir(struct au_ren_args *a)
501 SiMustAnyLock(d->d_sb);
504 if (au_ftest_ren(a->flags, ISDIR) && a->dst_inode) {
505 rdhash = au_sbi(d->d_sb)->si_rdhash;
507 rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
508 err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
512 au_set_dbstart(d, a->dst_bstart);
513 err = may_rename_dstdir(d, &a->whlist);
514 au_set_dbstart(d, a->btgt);
516 a->dst_h_dentry = au_h_dptr(d, au_dbstart(d));
521 a->src_h_dentry = au_h_dptr(d, au_dbstart(d));
522 if (au_ftest_ren(a->flags, ISDIR)) {
523 err = may_rename_srcdir(d, a->btgt);
525 au_nhash_wh_free(&a->whlist);
526 a->whlist.nh_num = 0;
533 /* ---------------------------------------------------------------------- */
536 * simple tests for rename.
537 * following the checks in vfs, plus the parent-child relationship.
539 static int au_may_ren(struct au_ren_args *a)
542 struct inode *h_inode;
544 if (a->src_bstart == a->btgt) {
545 err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
546 au_ftest_ren(a->flags, ISDIR));
550 if (unlikely(a->src_h_dentry == a->h_trap))
555 if (a->dst_bstart != a->btgt)
559 if (unlikely(a->dst_h_dentry == a->h_trap))
563 h_inode = a->dst_h_dentry->d_inode;
564 isdir = !!au_ftest_ren(a->flags, ISDIR);
565 if (!a->dst_dentry->d_inode) {
566 if (unlikely(h_inode))
568 err = au_may_add(a->dst_dentry, a->btgt, a->dst_h_parent,
571 if (unlikely(!h_inode || !h_inode->i_nlink))
573 err = au_may_del(a->dst_dentry, a->btgt, a->dst_h_parent,
580 if (unlikely(err == -ENOENT || err == -EEXIST))
586 /* ---------------------------------------------------------------------- */
591 * - src_dir and dir by lock_rename()
595 * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
597 * + di_write_lock2_child()
598 * + di_write_lock_child()
599 * + ii_write_lock_child()
600 * + di_write_lock_child2()
601 * + ii_write_lock_child2()
602 * + src_parent and parent
603 * + di_write_lock_parent()
604 * + ii_write_lock_parent()
605 * + di_write_lock_parent2()
606 * + ii_write_lock_parent2()
607 * + lower src_dir and dir by vfsub_lock_rename()
608 * + verify the every relationships between child and parent. if any
609 * of them failed, unlock all and return -EBUSY.
611 static void au_ren_unlock(struct au_ren_args *a)
613 struct super_block *sb;
615 sb = a->dst_dentry->d_sb;
616 if (au_ftest_ren(a->flags, MNT_WRITE))
617 mnt_drop_write(a->br->br_mnt);
618 vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
619 a->dst_h_parent, a->dst_hdir);
622 static int au_ren_lock(struct au_ren_args *a)
628 a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
629 a->src_hdir = au_hi(a->src_dir, a->btgt);
630 a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
631 a->dst_hdir = au_hi(a->dst_dir, a->btgt);
632 a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
633 a->dst_h_parent, a->dst_hdir);
634 udba = au_opt_udba(a->src_dentry->d_sb);
635 if (unlikely(a->src_hdir->hi_inode != a->src_h_parent->d_inode
636 || a->dst_hdir->hi_inode != a->dst_h_parent->d_inode))
637 err = au_busy_or_stale();
638 if (!err && au_dbstart(a->src_dentry) == a->btgt)
639 err = au_h_verify(a->src_h_dentry, udba,
640 a->src_h_parent->d_inode, a->src_h_parent,
642 if (!err && au_dbstart(a->dst_dentry) == a->btgt)
643 err = au_h_verify(a->dst_h_dentry, udba,
644 a->dst_h_parent->d_inode, a->dst_h_parent,
647 err = mnt_want_write(a->br->br_mnt);
650 au_fset_ren(a->flags, MNT_WRITE);
651 goto out; /* success */
654 err = au_busy_or_stale();
662 /* ---------------------------------------------------------------------- */
664 static void au_ren_refresh_dir(struct au_ren_args *a)
670 if (au_ftest_ren(a->flags, ISDIR)) {
671 /* is this updating defined in POSIX? */
672 au_cpup_attr_timesizes(a->src_inode);
673 au_cpup_attr_nlink(dir, /*force*/1);
676 if (au_ibstart(dir) == a->btgt)
677 au_cpup_attr_timesizes(dir);
679 if (au_ftest_ren(a->flags, ISSAMEDIR))
684 if (au_ftest_ren(a->flags, ISDIR))
685 au_cpup_attr_nlink(dir, /*force*/1);
686 if (au_ibstart(dir) == a->btgt)
687 au_cpup_attr_timesizes(dir);
690 static void au_ren_refresh(struct au_ren_args *a)
692 aufs_bindex_t bend, bindex;
693 struct dentry *d, *h_d;
694 struct inode *i, *h_i;
695 struct super_block *sb;
700 /* already dget-ed by au_ren_or_cpup() */
701 au_set_h_dptr(d, a->btgt, a->h_dst);
705 if (!au_ftest_ren(a->flags, ISDIR))
709 au_cpup_attr_timesizes(i);
711 au_update_dbrange(d, /*do_put_zero*/1);
714 for (bindex = au_dbstart(d); bindex < bend; bindex++)
715 au_set_h_dptr(d, bindex, NULL);
717 for (bindex = a->btgt + 1; bindex <= bend; bindex++)
718 au_set_h_dptr(d, bindex, NULL);
719 au_update_dbrange(d, /*do_put_zero*/0);
725 for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
726 h_d = au_h_dptr(d, bindex);
728 au_set_h_dptr(d, bindex, NULL);
730 au_set_dbend(d, a->btgt);
734 if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
735 return; /* success */
738 for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
739 h_i = au_h_iptr(i, bindex);
741 au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
742 /* ignore this error */
743 au_set_h_iptr(i, bindex, NULL, 0);
746 au_set_ibend(i, a->btgt);
749 /* ---------------------------------------------------------------------- */
751 /* mainly for link(2) and rename(2) */
752 int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
754 aufs_bindex_t bdiropq, bwh;
755 struct dentry *parent;
756 struct au_branch *br;
758 parent = dentry->d_parent;
759 IMustLock(parent->d_inode); /* dir is locked */
761 bdiropq = au_dbdiropq(parent);
762 bwh = au_dbwh(dentry);
763 br = au_sbr(dentry->d_sb, btgt);
765 || (0 <= bdiropq && bdiropq < btgt)
766 || (0 <= bwh && bwh < btgt))
769 AuDbg("btgt %d\n", btgt);
773 /* sets src_bstart, dst_bstart and btgt */
774 static int au_ren_wbr(struct au_ren_args *a)
777 struct au_wr_dir_args wr_dir_args = {
778 /* .force_btgt = -1, */
779 .flags = AuWrDir_ADD_ENTRY
782 a->src_bstart = au_dbstart(a->src_dentry);
783 a->dst_bstart = au_dbstart(a->dst_dentry);
784 if (au_ftest_ren(a->flags, ISDIR))
785 au_fset_wrdir(wr_dir_args.flags, ISDIR);
786 wr_dir_args.force_btgt = a->src_bstart;
787 if (a->dst_inode && a->dst_bstart < a->src_bstart)
788 wr_dir_args.force_btgt = a->dst_bstart;
789 wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
790 err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
796 static void au_ren_dt(struct au_ren_args *a)
798 a->h_path.dentry = a->src_h_parent;
799 au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
800 if (!au_ftest_ren(a->flags, ISSAMEDIR)) {
801 a->h_path.dentry = a->dst_h_parent;
802 au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
805 au_fclr_ren(a->flags, DT_DSTDIR);
806 if (!au_ftest_ren(a->flags, ISDIR))
809 a->h_path.dentry = a->src_h_dentry;
810 au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
811 if (a->dst_h_dentry->d_inode) {
812 au_fset_ren(a->flags, DT_DSTDIR);
813 a->h_path.dentry = a->dst_h_dentry;
814 au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
818 static void au_ren_rev_dt(int err, struct au_ren_args *a)
823 au_dtime_revert(a->src_dt + AuPARENT);
824 if (!au_ftest_ren(a->flags, ISSAMEDIR))
825 au_dtime_revert(a->dst_dt + AuPARENT);
827 if (au_ftest_ren(a->flags, ISDIR) && err != -EIO) {
828 h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
829 h_mtx = &h_d->d_inode->i_mutex;
830 mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
831 au_dtime_revert(a->src_dt + AuCHILD);
834 if (au_ftest_ren(a->flags, DT_DSTDIR)) {
835 h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
836 h_mtx = &h_d->d_inode->i_mutex;
837 mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
838 au_dtime_revert(a->dst_dt + AuCHILD);
844 /* ---------------------------------------------------------------------- */
846 int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
847 struct inode *_dst_dir, struct dentry *_dst_dentry)
850 /* reduce stack space */
851 struct au_ren_args *a;
853 AuDbg("%.*s, %.*s\n", AuDLNPair(_src_dentry), AuDLNPair(_dst_dentry));
858 BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
859 a = kzalloc(sizeof(*a), GFP_NOFS);
863 a->src_dir = _src_dir;
864 a->src_dentry = _src_dentry;
865 a->src_inode = a->src_dentry->d_inode;
866 a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
867 a->dst_dir = _dst_dir;
868 a->dst_dentry = _dst_dentry;
869 a->dst_inode = a->dst_dentry->d_inode;
870 a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
872 IMustLock(a->dst_inode);
873 au_igrab(a->dst_inode);
877 flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
878 if (S_ISDIR(a->src_inode->i_mode)) {
879 au_fset_ren(a->flags, ISDIR);
880 if (unlikely(a->dst_inode && !S_ISDIR(a->dst_inode->i_mode)))
882 err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
885 err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
890 err = au_d_hashed_positive(a->src_dentry);
896 * If it is a dir, VFS unhash dst_dentry before this
897 * function. It means we cannot rely upon d_unhashed().
899 if (unlikely(!a->dst_inode->i_nlink))
901 if (!S_ISDIR(a->dst_inode->i_mode)) {
902 err = au_d_hashed_positive(a->dst_dentry);
905 } else if (unlikely(IS_DEADDIR(a->dst_inode)))
907 } else if (unlikely(d_unhashed(a->dst_dentry)))
910 au_fset_ren(a->flags, ISSAMEDIR); /* temporary */
911 di_write_lock_parent(a->dst_parent);
913 /* which branch we process */
915 if (unlikely(err < 0))
917 a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
918 a->h_path.mnt = a->br->br_mnt;
920 /* are they available to be renamed */
921 err = au_ren_may_dir(a);
925 /* prepare the writable parent dir on the same branch */
926 if (a->dst_bstart == a->btgt) {
927 au_fset_ren(a->flags, WHDST);
929 err = au_cpup_dirs(a->dst_dentry, a->btgt);
934 if (a->src_dir != a->dst_dir) {
936 * this temporary unlock is safe,
937 * because both dir->i_mutex are locked.
939 di_write_unlock(a->dst_parent);
940 di_write_lock_parent(a->src_parent);
941 err = au_wr_dir_need_wh(a->src_dentry,
942 au_ftest_ren(a->flags, ISDIR),
944 di_write_unlock(a->src_parent);
945 di_write_lock2_parent(a->src_parent, a->dst_parent, /*isdir*/1);
946 au_fclr_ren(a->flags, ISSAMEDIR);
948 err = au_wr_dir_need_wh(a->src_dentry,
949 au_ftest_ren(a->flags, ISDIR),
951 if (unlikely(err < 0))
954 au_fset_ren(a->flags, WHSRC);
957 err = au_ren_lock(a);
961 if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
963 else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
968 /* store timestamps to be revertible */
976 /* update dir attributes */
977 au_ren_refresh_dir(a);
979 /* dput/iput all lower dentries */
982 goto out_hdir; /* success */
985 au_ren_rev_dt(err, a);
989 au_nhash_wh_free(&a->whlist);
990 if (err && a->dst_inode && a->dst_bstart != a->btgt) {
991 AuDbg("bstart %d, btgt %d\n", a->dst_bstart, a->btgt);
992 au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
993 au_set_dbstart(a->dst_dentry, a->dst_bstart);
997 d_move(a->src_dentry, a->dst_dentry);
999 au_update_dbstart(a->dst_dentry);
1001 d_drop(a->dst_dentry);
1003 if (au_ftest_ren(a->flags, ISSAMEDIR))
1004 di_write_unlock(a->dst_parent);
1006 di_write_unlock2(a->src_parent, a->dst_parent);
1008 aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
1012 au_whtmp_rmdir_free(a->thargs);