ARM: lib: delay: align loop
[pandora-kernel.git] / fs / aufs / i_op_ren.c
1 /*
2  * Copyright (C) 2005-2012 Junjiro R. Okajima
3  *
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.
8  *
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.
13  *
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
17  */
18
19 /*
20  * inode operation (rename entry)
21  * todo: this is crazy monster
22  */
23
24 #include "aufs.h"
25
26 enum { AuSRC, AuDST, AuSrcDst };
27 enum { AuPARENT, AuCHILD, AuParentChild };
28
29 #define AuRen_ISDIR     1
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)
42
43 struct au_ren_args {
44         struct {
45                 struct dentry *dentry, *h_dentry, *parent, *h_parent,
46                         *wh_dentry;
47                 struct inode *dir, *inode;
48                 struct au_hinode *hdir;
49                 struct au_dtime dt[AuParentChild];
50                 aufs_bindex_t bstart;
51         } sd[AuSrcDst];
52
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
64
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
76
77         struct dentry *h_trap;
78         struct au_branch *br;
79         struct au_hinode *src_hinode;
80         struct path h_path;
81         struct au_nhash whlist;
82         aufs_bindex_t btgt, src_bwh, src_bdiropq;
83
84         unsigned int flags;
85
86         struct au_whtmp_rmdir *thargs;
87         struct dentry *h_dst;
88 };
89
90 /* ---------------------------------------------------------------------- */
91
92 /*
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
97  * harmless.
98  */
99
100 #define RevertFailure(fmt, ...) do { \
101         AuIOErr("revert failure: " fmt " (%d, %d)\n", \
102                 ##__VA_ARGS__, err, rerr); \
103         err = -EIO; \
104 } while (0)
105
106 static void au_ren_rev_diropq(int err, struct au_ren_args *a)
107 {
108         int rerr;
109
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);
114         if (rerr)
115                 RevertFailure("remove diropq %.*s", AuDLNPair(a->src_dentry));
116 }
117
118 static void au_ren_rev_rename(int err, struct au_ren_args *a)
119 {
120         int rerr;
121
122         a->h_path.dentry = au_lkup_one(&a->src_dentry->d_name, a->src_h_parent,
123                                        a->br, /*nd*/NULL);
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));
127                 return;
128         }
129
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); */
136         if (rerr)
137                 RevertFailure("rename %.*s", AuDLNPair(a->src_dentry));
138 }
139
140 static void au_ren_rev_cpup(int err, struct au_ren_args *a)
141 {
142         int rerr;
143
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);
148         if (rerr)
149                 RevertFailure("unlink %.*s", AuDLNPair(a->dst_h_dentry));
150 }
151
152 static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
153 {
154         int rerr;
155
156         a->h_path.dentry = au_lkup_one(&a->dst_dentry->d_name, a->dst_h_parent,
157                                        a->br, /*nd*/NULL);
158         rerr = PTR_ERR(a->h_path.dentry);
159         if (IS_ERR(a->h_path.dentry)) {
160                 RevertFailure("lookup %.*s", AuDLNPair(a->dst_dentry));
161                 return;
162         }
163         if (a->h_path.dentry->d_inode) {
164                 d_drop(a->h_path.dentry);
165                 dput(a->h_path.dentry);
166                 return;
167         }
168
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);
172         if (!rerr)
173                 au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
174         else
175                 RevertFailure("rename %.*s", AuDLNPair(a->h_dst));
176 }
177
178 static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
179 {
180         int rerr;
181
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);
185         if (rerr)
186                 RevertFailure("unlink %.*s", AuDLNPair(a->src_wh_dentry));
187 }
188 #undef RevertFailure
189
190 /* ---------------------------------------------------------------------- */
191
192 /*
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.
196  */
197 static int au_ren_or_cpup(struct au_ren_args *a)
198 {
199         int err;
200         struct dentry *d;
201
202         d = a->src_dentry;
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);
211         } else {
212                 struct mutex *h_mtx = &a->src_h_dentry->d_inode->i_mutex;
213                 struct file *h_file;
214
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);
222                         h_file = NULL;
223                 } else
224                         err = au_sio_cpup_single(d, a->btgt, a->src_bstart, -1,
225                                                  !AuCpup_DTIME, a->dst_parent);
226                 mutex_unlock(h_mtx);
227                 au_h_open_post(d, a->src_bstart, h_file);
228                 if (!err) {
229                         d = a->dst_dentry;
230                         au_set_h_dptr(d, a->btgt, NULL);
231                         au_update_dbstart(d);
232                 } else {
233                         au_set_h_dptr(d, a->btgt, NULL);
234                         au_set_dbstart(d, a->src_bstart);
235                 }
236         }
237         if (!err && a->h_dst)
238                 /* it will be set to dinfo later */
239                 dget(a->h_dst);
240
241         return err;
242 }
243
244 /* cf. aufs_rmdir() */
245 static int au_ren_del_whtmp(struct au_ren_args *a)
246 {
247         int err;
248         struct inode *dir;
249
250         dir = a->dst_dir;
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);
256                 if (unlikely(err))
257                         pr_warning("failed removing whtmp dir %.*s (%d), "
258                                    "ignored.\n", AuDLNPair(a->h_dst), err);
259         } else {
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);
264                 dput(a->h_dst);
265                 a->thargs = NULL;
266         }
267
268         return 0;
269 }
270
271 /* make it 'opaque' dir. */
272 static int au_ren_diropq(struct au_ren_args *a)
273 {
274         int err;
275         struct dentry *diropq;
276
277         err = 0;
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);
283         if (IS_ERR(diropq))
284                 err = PTR_ERR(diropq);
285         dput(diropq);
286
287         return err;
288 }
289
290 static int do_rename(struct au_ren_args *a)
291 {
292         int err;
293         struct dentry *d, *h_d;
294
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) {
298                 err = -ENOMEM;
299                 a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb, GFP_NOFS);
300                 if (unlikely(!a->thargs))
301                         goto out;
302                 a->h_dst = dget(h_d);
303         }
304
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);
309                 a->src_wh_dentry
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))
313                         goto out_thargs;
314         }
315
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,
319                                  a->br);
320                 err = PTR_ERR(h_d);
321                 if (IS_ERR(h_d))
322                         goto out_whsrc;
323                 if (!h_d->d_inode)
324                         dput(h_d);
325                 else
326                         a->dst_wh_dentry = h_d;
327         }
328
329         /* rename dentry to tmpwh */
330         if (a->thargs) {
331                 err = au_whtmp_ren(a->dst_h_dentry, a->br);
332                 if (unlikely(err))
333                         goto out_whdst;
334
335                 d = a->dst_dentry;
336                 au_set_h_dptr(d, a->btgt, NULL);
337                 err = au_lkup_neg(d, a->btgt);
338                 if (unlikely(err))
339                         goto out_whtmp;
340                 a->dst_h_dentry = au_h_dptr(d, a->btgt);
341         }
342
343         /* cpup src */
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;
346                 struct file *h_file;
347
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);
353                         h_file = NULL;
354                 } else
355                         err = au_sio_cpup_simple(a->src_dentry, a->btgt, -1,
356                                                  !AuCpup_DTIME);
357                 mutex_unlock(h_mtx);
358                 au_h_open_post(a->src_dentry, a->src_bstart, h_file);
359                 if (unlikely(err))
360                         goto out_whtmp;
361         }
362
363         /* rename by vfs_rename or cpup */
364         d = a->dst_dentry;
365         if (au_ftest_ren(a->flags, ISDIR)
366             && (a->dst_wh_dentry
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);
373         if (unlikely(err))
374                 /* leave the copied-up one */
375                 goto out_whtmp;
376
377         /* make dir opaque */
378         if (au_ftest_ren(a->flags, DIROPQ)) {
379                 err = au_ren_diropq(a);
380                 if (unlikely(err))
381                         goto out_rename;
382         }
383
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;
389
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,
394                                           a->dst_dentry);
395                 if (unlikely(err))
396                         goto out_diropq;
397         }
398
399         /* remove whtmp */
400         if (a->thargs)
401                 au_ren_del_whtmp(a); /* ignore this error */
402
403         err = 0;
404         goto out_success;
405
406 out_diropq:
407         if (au_ftest_ren(a->flags, DIROPQ))
408                 au_ren_rev_diropq(err, a);
409 out_rename:
410         if (!au_ftest_ren(a->flags, CPUP))
411                 au_ren_rev_rename(err, a);
412         else
413                 au_ren_rev_cpup(err, a);
414         dput(a->h_dst);
415 out_whtmp:
416         if (a->thargs)
417                 au_ren_rev_whtmp(err, a);
418 out_whdst:
419         dput(a->dst_wh_dentry);
420         a->dst_wh_dentry = NULL;
421 out_whsrc:
422         if (a->src_wh_dentry)
423                 au_ren_rev_whsrc(err, a);
424 out_success:
425         dput(a->src_wh_dentry);
426         dput(a->dst_wh_dentry);
427 out_thargs:
428         if (a->thargs) {
429                 dput(a->h_dst);
430                 au_whtmp_rmdir_free(a->thargs);
431                 a->thargs = NULL;
432         }
433 out:
434         return err;
435 }
436
437 /* ---------------------------------------------------------------------- */
438
439 /*
440  * test if @dentry dir can be rename destination or not.
441  * success means, it is a logically empty dir.
442  */
443 static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
444 {
445         return au_test_empty(dentry, whlist);
446 }
447
448 /*
449  * test if @dentry dir can be rename source or not.
450  * if it can, return 0 and @children is filled.
451  * success means,
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.
455  */
456 static int may_rename_srcdir(struct dentry *dentry, aufs_bindex_t btgt)
457 {
458         int err;
459         unsigned int rdhash;
460         aufs_bindex_t bstart;
461
462         bstart = au_dbstart(dentry);
463         if (bstart != btgt) {
464                 struct au_nhash whlist;
465
466                 SiMustAnyLock(dentry->d_sb);
467                 rdhash = au_sbi(dentry->d_sb)->si_rdhash;
468                 if (!rdhash)
469                         rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
470                                                            dentry));
471                 err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
472                 if (unlikely(err))
473                         goto out;
474                 err = au_test_empty(dentry, &whlist);
475                 au_nhash_wh_free(&whlist);
476                 goto out;
477         }
478
479         if (bstart == au_dbtaildir(dentry))
480                 return 0; /* success */
481
482         err = au_test_empty_lower(dentry);
483
484 out:
485         if (err == -ENOTEMPTY) {
486                 AuWarn1("renaming dir who has child(ren) on multiple branches,"
487                         " is not supported\n");
488                 err = -EXDEV;
489         }
490         return err;
491 }
492
493 /* side effect: sets whlist and h_dentry */
494 static int au_ren_may_dir(struct au_ren_args *a)
495 {
496         int err;
497         unsigned int rdhash;
498         struct dentry *d;
499
500         d = a->dst_dentry;
501         SiMustAnyLock(d->d_sb);
502
503         err = 0;
504         if (au_ftest_ren(a->flags, ISDIR) && a->dst_inode) {
505                 rdhash = au_sbi(d->d_sb)->si_rdhash;
506                 if (!rdhash)
507                         rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
508                 err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
509                 if (unlikely(err))
510                         goto out;
511
512                 au_set_dbstart(d, a->dst_bstart);
513                 err = may_rename_dstdir(d, &a->whlist);
514                 au_set_dbstart(d, a->btgt);
515         }
516         a->dst_h_dentry = au_h_dptr(d, au_dbstart(d));
517         if (unlikely(err))
518                 goto out;
519
520         d = a->src_dentry;
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);
524                 if (unlikely(err)) {
525                         au_nhash_wh_free(&a->whlist);
526                         a->whlist.nh_num = 0;
527                 }
528         }
529 out:
530         return err;
531 }
532
533 /* ---------------------------------------------------------------------- */
534
535 /*
536  * simple tests for rename.
537  * following the checks in vfs, plus the parent-child relationship.
538  */
539 static int au_may_ren(struct au_ren_args *a)
540 {
541         int err, isdir;
542         struct inode *h_inode;
543
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));
547                 if (unlikely(err))
548                         goto out;
549                 err = -EINVAL;
550                 if (unlikely(a->src_h_dentry == a->h_trap))
551                         goto out;
552         }
553
554         err = 0;
555         if (a->dst_bstart != a->btgt)
556                 goto out;
557
558         err = -ENOTEMPTY;
559         if (unlikely(a->dst_h_dentry == a->h_trap))
560                 goto out;
561
562         err = -EIO;
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))
567                         goto out;
568                 err = au_may_add(a->dst_dentry, a->btgt, a->dst_h_parent,
569                                  isdir);
570         } else {
571                 if (unlikely(!h_inode || !h_inode->i_nlink))
572                         goto out;
573                 err = au_may_del(a->dst_dentry, a->btgt, a->dst_h_parent,
574                                  isdir);
575                 if (unlikely(err))
576                         goto out;
577         }
578
579 out:
580         if (unlikely(err == -ENOENT || err == -EEXIST))
581                 err = -EIO;
582         AuTraceErr(err);
583         return err;
584 }
585
586 /* ---------------------------------------------------------------------- */
587
588 /*
589  * locking order
590  * (VFS)
591  * - src_dir and dir by lock_rename()
592  * - inode if exitsts
593  * (aufs)
594  * - lock all
595  *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
596  *     + si_read_lock
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.
610  */
611 static void au_ren_unlock(struct au_ren_args *a)
612 {
613         struct super_block *sb;
614
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);
620 }
621
622 static int au_ren_lock(struct au_ren_args *a)
623 {
624         int err;
625         unsigned int udba;
626
627         err = 0;
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,
641                                   a->br);
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,
645                                   a->br);
646         if (!err) {
647                 err = mnt_want_write(a->br->br_mnt);
648                 if (unlikely(err))
649                         goto out_unlock;
650                 au_fset_ren(a->flags, MNT_WRITE);
651                 goto out; /* success */
652         }
653
654         err = au_busy_or_stale();
655
656 out_unlock:
657         au_ren_unlock(a);
658 out:
659         return err;
660 }
661
662 /* ---------------------------------------------------------------------- */
663
664 static void au_ren_refresh_dir(struct au_ren_args *a)
665 {
666         struct inode *dir;
667
668         dir = a->dst_dir;
669         dir->i_version++;
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);
674         }
675
676         if (au_ibstart(dir) == a->btgt)
677                 au_cpup_attr_timesizes(dir);
678
679         if (au_ftest_ren(a->flags, ISSAMEDIR))
680                 return;
681
682         dir = a->src_dir;
683         dir->i_version++;
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);
688 }
689
690 static void au_ren_refresh(struct au_ren_args *a)
691 {
692         aufs_bindex_t bend, bindex;
693         struct dentry *d, *h_d;
694         struct inode *i, *h_i;
695         struct super_block *sb;
696
697         d = a->dst_dentry;
698         d_drop(d);
699         if (a->h_dst)
700                 /* already dget-ed by au_ren_or_cpup() */
701                 au_set_h_dptr(d, a->btgt, a->h_dst);
702
703         i = a->dst_inode;
704         if (i) {
705                 if (!au_ftest_ren(a->flags, ISDIR))
706                         vfsub_drop_nlink(i);
707                 else {
708                         vfsub_dead_dir(i);
709                         au_cpup_attr_timesizes(i);
710                 }
711                 au_update_dbrange(d, /*do_put_zero*/1);
712         } else {
713                 bend = a->btgt;
714                 for (bindex = au_dbstart(d); bindex < bend; bindex++)
715                         au_set_h_dptr(d, bindex, NULL);
716                 bend = au_dbend(d);
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);
720         }
721
722         d = a->src_dentry;
723         au_set_dbwh(d, -1);
724         bend = au_dbend(d);
725         for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
726                 h_d = au_h_dptr(d, bindex);
727                 if (h_d)
728                         au_set_h_dptr(d, bindex, NULL);
729         }
730         au_set_dbend(d, a->btgt);
731
732         sb = d->d_sb;
733         i = a->src_inode;
734         if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
735                 return; /* success */
736
737         bend = au_ibend(i);
738         for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
739                 h_i = au_h_iptr(i, bindex);
740                 if (h_i) {
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);
744                 }
745         }
746         au_set_ibend(i, a->btgt);
747 }
748
749 /* ---------------------------------------------------------------------- */
750
751 /* mainly for link(2) and rename(2) */
752 int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
753 {
754         aufs_bindex_t bdiropq, bwh;
755         struct dentry *parent;
756         struct au_branch *br;
757
758         parent = dentry->d_parent;
759         IMustLock(parent->d_inode); /* dir is locked */
760
761         bdiropq = au_dbdiropq(parent);
762         bwh = au_dbwh(dentry);
763         br = au_sbr(dentry->d_sb, btgt);
764         if (au_br_rdonly(br)
765             || (0 <= bdiropq && bdiropq < btgt)
766             || (0 <= bwh && bwh < btgt))
767                 btgt = -1;
768
769         AuDbg("btgt %d\n", btgt);
770         return btgt;
771 }
772
773 /* sets src_bstart, dst_bstart and btgt */
774 static int au_ren_wbr(struct au_ren_args *a)
775 {
776         int err;
777         struct au_wr_dir_args wr_dir_args = {
778                 /* .force_btgt  = -1, */
779                 .flags          = AuWrDir_ADD_ENTRY
780         };
781
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);
791         a->btgt = err;
792
793         return err;
794 }
795
796 static void au_ren_dt(struct au_ren_args *a)
797 {
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);
803         }
804
805         au_fclr_ren(a->flags, DT_DSTDIR);
806         if (!au_ftest_ren(a->flags, ISDIR))
807                 return;
808
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);
815         }
816 }
817
818 static void au_ren_rev_dt(int err, struct au_ren_args *a)
819 {
820         struct dentry *h_d;
821         struct mutex *h_mtx;
822
823         au_dtime_revert(a->src_dt + AuPARENT);
824         if (!au_ftest_ren(a->flags, ISSAMEDIR))
825                 au_dtime_revert(a->dst_dt + AuPARENT);
826
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);
832                 mutex_unlock(h_mtx);
833
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);
839                         mutex_unlock(h_mtx);
840                 }
841         }
842 }
843
844 /* ---------------------------------------------------------------------- */
845
846 int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
847                 struct inode *_dst_dir, struct dentry *_dst_dentry)
848 {
849         int err, flags;
850         /* reduce stack space */
851         struct au_ren_args *a;
852
853         AuDbg("%.*s, %.*s\n", AuDLNPair(_src_dentry), AuDLNPair(_dst_dentry));
854         IMustLock(_src_dir);
855         IMustLock(_dst_dir);
856
857         err = -ENOMEM;
858         BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
859         a = kzalloc(sizeof(*a), GFP_NOFS);
860         if (unlikely(!a))
861                 goto out;
862
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 */
871         if (a->dst_inode) {
872                 IMustLock(a->dst_inode);
873                 au_igrab(a->dst_inode);
874         }
875
876         err = -ENOTDIR;
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)))
881                         goto out_free;
882                 err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
883                                                 AuLock_DIR | flags);
884         } else
885                 err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
886                                                 flags);
887         if (unlikely(err))
888                 goto out_free;
889
890         err = au_d_hashed_positive(a->src_dentry);
891         if (unlikely(err))
892                 goto out_unlock;
893         err = -ENOENT;
894         if (a->dst_inode) {
895                 /*
896                  * If it is a dir, VFS unhash dst_dentry before this
897                  * function. It means we cannot rely upon d_unhashed().
898                  */
899                 if (unlikely(!a->dst_inode->i_nlink))
900                         goto out_unlock;
901                 if (!S_ISDIR(a->dst_inode->i_mode)) {
902                         err = au_d_hashed_positive(a->dst_dentry);
903                         if (unlikely(err))
904                                 goto out_unlock;
905                 } else if (unlikely(IS_DEADDIR(a->dst_inode)))
906                         goto out_unlock;
907         } else if (unlikely(d_unhashed(a->dst_dentry)))
908                 goto out_unlock;
909
910         au_fset_ren(a->flags, ISSAMEDIR); /* temporary */
911         di_write_lock_parent(a->dst_parent);
912
913         /* which branch we process */
914         err = au_ren_wbr(a);
915         if (unlikely(err < 0))
916                 goto out_parent;
917         a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
918         a->h_path.mnt = a->br->br_mnt;
919
920         /* are they available to be renamed */
921         err = au_ren_may_dir(a);
922         if (unlikely(err))
923                 goto out_children;
924
925         /* prepare the writable parent dir on the same branch */
926         if (a->dst_bstart == a->btgt) {
927                 au_fset_ren(a->flags, WHDST);
928         } else {
929                 err = au_cpup_dirs(a->dst_dentry, a->btgt);
930                 if (unlikely(err))
931                         goto out_children;
932         }
933
934         if (a->src_dir != a->dst_dir) {
935                 /*
936                  * this temporary unlock is safe,
937                  * because both dir->i_mutex are locked.
938                  */
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),
943                                         &a->btgt);
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);
947         } else
948                 err = au_wr_dir_need_wh(a->src_dentry,
949                                         au_ftest_ren(a->flags, ISDIR),
950                                         &a->btgt);
951         if (unlikely(err < 0))
952                 goto out_children;
953         if (err)
954                 au_fset_ren(a->flags, WHSRC);
955
956         /* lock them all */
957         err = au_ren_lock(a);
958         if (unlikely(err))
959                 goto out_children;
960
961         if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
962                 err = au_may_ren(a);
963         else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
964                 err = -ENAMETOOLONG;
965         if (unlikely(err))
966                 goto out_hdir;
967
968         /* store timestamps to be revertible */
969         au_ren_dt(a);
970
971         /* here we go */
972         err = do_rename(a);
973         if (unlikely(err))
974                 goto out_dt;
975
976         /* update dir attributes */
977         au_ren_refresh_dir(a);
978
979         /* dput/iput all lower dentries */
980         au_ren_refresh(a);
981
982         goto out_hdir; /* success */
983
984 out_dt:
985         au_ren_rev_dt(err, a);
986 out_hdir:
987         au_ren_unlock(a);
988 out_children:
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);
994         }
995 out_parent:
996         if (!err)
997                 d_move(a->src_dentry, a->dst_dentry);
998         else {
999                 au_update_dbstart(a->dst_dentry);
1000                 if (!a->dst_inode)
1001                         d_drop(a->dst_dentry);
1002         }
1003         if (au_ftest_ren(a->flags, ISSAMEDIR))
1004                 di_write_unlock(a->dst_parent);
1005         else
1006                 di_write_unlock2(a->src_parent, a->dst_parent);
1007 out_unlock:
1008         aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
1009 out_free:
1010         iput(a->dst_inode);
1011         if (a->thargs)
1012                 au_whtmp_rmdir_free(a->thargs);
1013         kfree(a);
1014 out:
1015         AuTraceErr(err);
1016         return err;
1017 }