Merge branch 'stable-3.2' into pandora-3.2
[pandora-kernel.git] / fs / aufs / i_op_ren.c
1 /*
2  * Copyright (C) 2005-2013 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                 BUG();
213
214         if (!err && a->h_dst)
215                 /* it will be set to dinfo later */
216                 dget(a->h_dst);
217
218         return err;
219 }
220
221 /* cf. aufs_rmdir() */
222 static int au_ren_del_whtmp(struct au_ren_args *a)
223 {
224         int err;
225         struct inode *dir;
226
227         dir = a->dst_dir;
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);
233                 if (unlikely(err))
234                         pr_warn("failed removing whtmp dir %.*s (%d), "
235                                 "ignored.\n", AuDLNPair(a->h_dst), err);
236         } else {
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);
241                 dput(a->h_dst);
242                 a->thargs = NULL;
243         }
244
245         return 0;
246 }
247
248 /* make it 'opaque' dir. */
249 static int au_ren_diropq(struct au_ren_args *a)
250 {
251         int err;
252         struct dentry *diropq;
253
254         err = 0;
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);
260         if (IS_ERR(diropq))
261                 err = PTR_ERR(diropq);
262         dput(diropq);
263
264         return err;
265 }
266
267 static int do_rename(struct au_ren_args *a)
268 {
269         int err;
270         struct dentry *d, *h_d;
271
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) {
275                 err = -ENOMEM;
276                 a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb, GFP_NOFS);
277                 if (unlikely(!a->thargs))
278                         goto out;
279                 a->h_dst = dget(h_d);
280         }
281
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);
286                 a->src_wh_dentry
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))
290                         goto out_thargs;
291         }
292
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,
296                                  a->br);
297                 err = PTR_ERR(h_d);
298                 if (IS_ERR(h_d))
299                         goto out_whsrc;
300                 if (!h_d->d_inode)
301                         dput(h_d);
302                 else
303                         a->dst_wh_dentry = h_d;
304         }
305
306         /* rename dentry to tmpwh */
307         if (a->thargs) {
308                 err = au_whtmp_ren(a->dst_h_dentry, a->br);
309                 if (unlikely(err))
310                         goto out_whdst;
311
312                 d = a->dst_dentry;
313                 au_set_h_dptr(d, a->btgt, NULL);
314                 err = au_lkup_neg(d, a->btgt, /*wh*/0);
315                 if (unlikely(err))
316                         goto out_whtmp;
317                 a->dst_h_dentry = au_h_dptr(d, a->btgt);
318         }
319
320         BUG_ON(a->dst_h_dentry->d_inode && a->src_bstart != a->btgt);
321
322         /* rename by vfs_rename or cpup */
323         d = a->dst_dentry;
324         if (au_ftest_ren(a->flags, ISDIR)
325             && (a->dst_wh_dentry
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);
332         if (unlikely(err))
333                 /* leave the copied-up one */
334                 goto out_whtmp;
335
336         /* make dir opaque */
337         if (au_ftest_ren(a->flags, DIROPQ)) {
338                 err = au_ren_diropq(a);
339                 if (unlikely(err))
340                         goto out_rename;
341         }
342
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;
348
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,
353                                           a->dst_dentry);
354                 if (unlikely(err))
355                         goto out_diropq;
356         }
357
358         /* remove whtmp */
359         if (a->thargs)
360                 au_ren_del_whtmp(a); /* ignore this error */
361
362         err = 0;
363         goto out_success;
364
365 out_diropq:
366         if (au_ftest_ren(a->flags, DIROPQ))
367                 au_ren_rev_diropq(err, a);
368 out_rename:
369         if (!au_ftest_ren(a->flags, CPUP))
370                 au_ren_rev_rename(err, a);
371         else
372                 au_ren_rev_cpup(err, a);
373         dput(a->h_dst);
374 out_whtmp:
375         if (a->thargs)
376                 au_ren_rev_whtmp(err, a);
377 out_whdst:
378         dput(a->dst_wh_dentry);
379         a->dst_wh_dentry = NULL;
380 out_whsrc:
381         if (a->src_wh_dentry)
382                 au_ren_rev_whsrc(err, a);
383 out_success:
384         dput(a->src_wh_dentry);
385         dput(a->dst_wh_dentry);
386 out_thargs:
387         if (a->thargs) {
388                 dput(a->h_dst);
389                 au_whtmp_rmdir_free(a->thargs);
390                 a->thargs = NULL;
391         }
392 out:
393         return err;
394 }
395
396 /* ---------------------------------------------------------------------- */
397
398 /*
399  * test if @dentry dir can be rename destination or not.
400  * success means, it is a logically empty dir.
401  */
402 static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
403 {
404         return au_test_empty(dentry, whlist);
405 }
406
407 /*
408  * test if @dentry dir can be rename source or not.
409  * if it can, return 0 and @children is filled.
410  * success means,
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.
414  */
415 static int may_rename_srcdir(struct dentry *dentry, aufs_bindex_t btgt)
416 {
417         int err;
418         unsigned int rdhash;
419         aufs_bindex_t bstart;
420
421         bstart = au_dbstart(dentry);
422         if (bstart != btgt) {
423                 struct au_nhash whlist;
424
425                 SiMustAnyLock(dentry->d_sb);
426                 rdhash = au_sbi(dentry->d_sb)->si_rdhash;
427                 if (!rdhash)
428                         rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
429                                                            dentry));
430                 err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
431                 if (unlikely(err))
432                         goto out;
433                 err = au_test_empty(dentry, &whlist);
434                 au_nhash_wh_free(&whlist);
435                 goto out;
436         }
437
438         if (bstart == au_dbtaildir(dentry))
439                 return 0; /* success */
440
441         err = au_test_empty_lower(dentry);
442
443 out:
444         if (err == -ENOTEMPTY) {
445                 AuWarn1("renaming dir who has child(ren) on multiple branches,"
446                         " is not supported\n");
447                 err = -EXDEV;
448         }
449         return err;
450 }
451
452 /* side effect: sets whlist and h_dentry */
453 static int au_ren_may_dir(struct au_ren_args *a)
454 {
455         int err;
456         unsigned int rdhash;
457         struct dentry *d;
458
459         d = a->dst_dentry;
460         SiMustAnyLock(d->d_sb);
461
462         err = 0;
463         if (au_ftest_ren(a->flags, ISDIR) && a->dst_inode) {
464                 rdhash = au_sbi(d->d_sb)->si_rdhash;
465                 if (!rdhash)
466                         rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
467                 err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
468                 if (unlikely(err))
469                         goto out;
470
471                 au_set_dbstart(d, a->dst_bstart);
472                 err = may_rename_dstdir(d, &a->whlist);
473                 au_set_dbstart(d, a->btgt);
474         }
475         a->dst_h_dentry = au_h_dptr(d, au_dbstart(d));
476         if (unlikely(err))
477                 goto out;
478
479         d = a->src_dentry;
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);
483                 if (unlikely(err)) {
484                         au_nhash_wh_free(&a->whlist);
485                         a->whlist.nh_num = 0;
486                 }
487         }
488 out:
489         return err;
490 }
491
492 /* ---------------------------------------------------------------------- */
493
494 /*
495  * simple tests for rename.
496  * following the checks in vfs, plus the parent-child relationship.
497  */
498 static int au_may_ren(struct au_ren_args *a)
499 {
500         int err, isdir;
501         struct inode *h_inode;
502
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));
506                 if (unlikely(err))
507                         goto out;
508                 err = -EINVAL;
509                 if (unlikely(a->src_h_dentry == a->h_trap))
510                         goto out;
511         }
512
513         err = 0;
514         if (a->dst_bstart != a->btgt)
515                 goto out;
516
517         err = -ENOTEMPTY;
518         if (unlikely(a->dst_h_dentry == a->h_trap))
519                 goto out;
520
521         err = -EIO;
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))
526                         goto out;
527                 err = au_may_add(a->dst_dentry, a->btgt, a->dst_h_parent,
528                                  isdir);
529         } else {
530                 if (unlikely(!h_inode || !h_inode->i_nlink))
531                         goto out;
532                 err = au_may_del(a->dst_dentry, a->btgt, a->dst_h_parent,
533                                  isdir);
534                 if (unlikely(err))
535                         goto out;
536         }
537
538 out:
539         if (unlikely(err == -ENOENT || err == -EEXIST))
540                 err = -EIO;
541         AuTraceErr(err);
542         return err;
543 }
544
545 /* ---------------------------------------------------------------------- */
546
547 /*
548  * locking order
549  * (VFS)
550  * - src_dir and dir by lock_rename()
551  * - inode if exitsts
552  * (aufs)
553  * - lock all
554  *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
555  *     + si_read_lock
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.
569  */
570 static void au_ren_unlock(struct au_ren_args *a)
571 {
572         struct super_block *sb;
573
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);
579 }
580
581 static int au_ren_lock(struct au_ren_args *a)
582 {
583         int err;
584         unsigned int udba;
585
586         err = 0;
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,
600                                   a->br);
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,
604                                   a->br);
605         if (!err) {
606                 err = mnt_want_write(au_br_mnt(a->br));
607                 if (unlikely(err))
608                         goto out_unlock;
609                 au_fset_ren(a->flags, MNT_WRITE);
610                 goto out; /* success */
611         }
612
613         err = au_busy_or_stale();
614
615 out_unlock:
616         au_ren_unlock(a);
617 out:
618         return err;
619 }
620
621 /* ---------------------------------------------------------------------- */
622
623 static void au_ren_refresh_dir(struct au_ren_args *a)
624 {
625         struct inode *dir;
626
627         dir = a->dst_dir;
628         dir->i_version++;
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);
633         }
634
635         if (au_ibstart(dir) == a->btgt)
636                 au_cpup_attr_timesizes(dir);
637
638         if (au_ftest_ren(a->flags, ISSAMEDIR))
639                 return;
640
641         dir = a->src_dir;
642         dir->i_version++;
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);
647 }
648
649 static void au_ren_refresh(struct au_ren_args *a)
650 {
651         aufs_bindex_t bend, bindex;
652         struct dentry *d, *h_d;
653         struct inode *i, *h_i;
654         struct super_block *sb;
655
656         d = a->dst_dentry;
657         d_drop(d);
658         if (a->h_dst)
659                 /* already dget-ed by au_ren_or_cpup() */
660                 au_set_h_dptr(d, a->btgt, a->h_dst);
661
662         i = a->dst_inode;
663         if (i) {
664                 if (!au_ftest_ren(a->flags, ISDIR))
665                         vfsub_drop_nlink(i);
666                 else {
667                         vfsub_dead_dir(i);
668                         au_cpup_attr_timesizes(i);
669                 }
670                 au_update_dbrange(d, /*do_put_zero*/1);
671         } else {
672                 bend = a->btgt;
673                 for (bindex = au_dbstart(d); bindex < bend; bindex++)
674                         au_set_h_dptr(d, bindex, NULL);
675                 bend = au_dbend(d);
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);
679         }
680
681         d = a->src_dentry;
682         au_set_dbwh(d, -1);
683         bend = au_dbend(d);
684         for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
685                 h_d = au_h_dptr(d, bindex);
686                 if (h_d)
687                         au_set_h_dptr(d, bindex, NULL);
688         }
689         au_set_dbend(d, a->btgt);
690
691         sb = d->d_sb;
692         i = a->src_inode;
693         if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
694                 return; /* success */
695
696         bend = au_ibend(i);
697         for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
698                 h_i = au_h_iptr(i, bindex);
699                 if (h_i) {
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);
703                 }
704         }
705         au_set_ibend(i, a->btgt);
706 }
707
708 /* ---------------------------------------------------------------------- */
709
710 /* mainly for link(2) and rename(2) */
711 int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
712 {
713         aufs_bindex_t bdiropq, bwh;
714         struct dentry *parent;
715         struct au_branch *br;
716
717         parent = dentry->d_parent;
718         IMustLock(parent->d_inode); /* dir is locked */
719
720         bdiropq = au_dbdiropq(parent);
721         bwh = au_dbwh(dentry);
722         br = au_sbr(dentry->d_sb, btgt);
723         if (au_br_rdonly(br)
724             || (0 <= bdiropq && bdiropq < btgt)
725             || (0 <= bwh && bwh < btgt))
726                 btgt = -1;
727
728         AuDbg("btgt %d\n", btgt);
729         return btgt;
730 }
731
732 /* sets src_bstart, dst_bstart and btgt */
733 static int au_ren_wbr(struct au_ren_args *a)
734 {
735         int err;
736         struct au_wr_dir_args wr_dir_args = {
737                 /* .force_btgt  = -1, */
738                 .flags          = AuWrDir_ADD_ENTRY
739         };
740
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);
750         a->btgt = err;
751
752         return err;
753 }
754
755 static void au_ren_dt(struct au_ren_args *a)
756 {
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);
762         }
763
764         au_fclr_ren(a->flags, DT_DSTDIR);
765         if (!au_ftest_ren(a->flags, ISDIR))
766                 return;
767
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);
774         }
775 }
776
777 static void au_ren_rev_dt(int err, struct au_ren_args *a)
778 {
779         struct dentry *h_d;
780         struct mutex *h_mtx;
781
782         au_dtime_revert(a->src_dt + AuPARENT);
783         if (!au_ftest_ren(a->flags, ISSAMEDIR))
784                 au_dtime_revert(a->dst_dt + AuPARENT);
785
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);
791                 mutex_unlock(h_mtx);
792
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);
798                         mutex_unlock(h_mtx);
799                 }
800         }
801 }
802
803 /* ---------------------------------------------------------------------- */
804
805 int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
806                 struct inode *_dst_dir, struct dentry *_dst_dentry)
807 {
808         int err, flags;
809         /* reduce stack space */
810         struct au_ren_args *a;
811
812         AuDbg("%.*s, %.*s\n", AuDLNPair(_src_dentry), AuDLNPair(_dst_dentry));
813         IMustLock(_src_dir);
814         IMustLock(_dst_dir);
815
816         err = -ENOMEM;
817         BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
818         a = kzalloc(sizeof(*a), GFP_NOFS);
819         if (unlikely(!a))
820                 goto out;
821
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 */
830         if (a->dst_inode) {
831                 IMustLock(a->dst_inode);
832                 au_igrab(a->dst_inode);
833         }
834
835         err = -ENOTDIR;
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)))
840                         goto out_free;
841                 err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
842                                                 AuLock_DIR | flags);
843         } else
844                 err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
845                                                 flags);
846         if (unlikely(err))
847                 goto out_free;
848
849         err = au_d_hashed_positive(a->src_dentry);
850         if (unlikely(err))
851                 goto out_unlock;
852         err = -ENOENT;
853         if (a->dst_inode) {
854                 /*
855                  * If it is a dir, VFS unhash dst_dentry before this
856                  * function. It means we cannot rely upon d_unhashed().
857                  */
858                 if (unlikely(!a->dst_inode->i_nlink))
859                         goto out_unlock;
860                 if (!S_ISDIR(a->dst_inode->i_mode)) {
861                         err = au_d_hashed_positive(a->dst_dentry);
862                         if (unlikely(err))
863                                 goto out_unlock;
864                 } else if (unlikely(IS_DEADDIR(a->dst_inode)))
865                         goto out_unlock;
866         } else if (unlikely(d_unhashed(a->dst_dentry)))
867                 goto out_unlock;
868
869         /*
870          * is it possible?
871          * yes, it happend (in linux-3.3-rcN) but I don't know why.
872          * there may exist a problem somewhere else.
873          */
874         err = -EINVAL;
875         if (unlikely(a->dst_parent->d_inode == a->src_dentry->d_inode))
876                 goto out_unlock;
877
878         au_fset_ren(a->flags, ISSAMEDIR); /* temporary */
879         di_write_lock_parent(a->dst_parent);
880
881         /* which branch we process */
882         err = au_ren_wbr(a);
883         if (unlikely(err < 0))
884                 goto out_parent;
885         a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
886         a->h_path.mnt = au_br_mnt(a->br);
887
888         /* are they available to be renamed */
889         err = au_ren_may_dir(a);
890         if (unlikely(err))
891                 goto out_children;
892
893         /* prepare the writable parent dir on the same branch */
894         if (a->dst_bstart == a->btgt) {
895                 au_fset_ren(a->flags, WHDST);
896         } else {
897                 err = au_cpup_dirs(a->dst_dentry, a->btgt);
898                 if (unlikely(err))
899                         goto out_children;
900         }
901
902         if (a->src_dir != a->dst_dir) {
903                 /*
904                  * this temporary unlock is safe,
905                  * because both dir->i_mutex are locked.
906                  */
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),
911                                         &a->btgt);
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);
915         } else
916                 err = au_wr_dir_need_wh(a->src_dentry,
917                                         au_ftest_ren(a->flags, ISDIR),
918                                         &a->btgt);
919         if (unlikely(err < 0))
920                 goto out_children;
921         if (err)
922                 au_fset_ren(a->flags, WHSRC);
923
924         /* cpup src */
925         if (a->src_bstart != a->btgt) {
926                 struct au_pin pin;
927
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);
931                 if (!err) {
932                         struct au_cp_generic cpg = {
933                                 .dentry = a->src_dentry,
934                                 .bdst   = a->btgt,
935                                 .bsrc   = a->src_bstart,
936                                 .len    = -1,
937                                 .pin    = &pin,
938                                 .flags  = AuCpup_DTIME | AuCpup_HOPEN
939                         };
940                         AuDebugOn(au_dbstart(a->src_dentry) != a->src_bstart);
941                         err = au_sio_cpup_simple(&cpg);
942                         au_unpin(&pin);
943                 }
944                 if (unlikely(err))
945                         goto out_children;
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);
949         }
950
951         /* lock them all */
952         err = au_ren_lock(a);
953         if (unlikely(err))
954                 /* leave the copied-up one */
955                 goto out_children;
956
957         if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
958                 err = au_may_ren(a);
959         else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
960                 err = -ENAMETOOLONG;
961         if (unlikely(err))
962                 goto out_hdir;
963
964         /* store timestamps to be revertible */
965         au_ren_dt(a);
966
967         /* here we go */
968         err = do_rename(a);
969         if (unlikely(err))
970                 goto out_dt;
971
972         /* update dir attributes */
973         au_ren_refresh_dir(a);
974
975         /* dput/iput all lower dentries */
976         au_ren_refresh(a);
977
978         goto out_hdir; /* success */
979
980 out_dt:
981         au_ren_rev_dt(err, a);
982 out_hdir:
983         au_ren_unlock(a);
984 out_children:
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);
990         }
991 out_parent:
992         if (!err)
993                 d_move(a->src_dentry, a->dst_dentry);
994         else {
995                 au_update_dbstart(a->dst_dentry);
996                 if (!a->dst_inode)
997                         d_drop(a->dst_dentry);
998         }
999         if (au_ftest_ren(a->flags, ISSAMEDIR))
1000                 di_write_unlock(a->dst_parent);
1001         else
1002                 di_write_unlock2(a->src_parent, a->dst_parent);
1003 out_unlock:
1004         aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
1005 out_free:
1006         iput(a->dst_inode);
1007         if (a->thargs)
1008                 au_whtmp_rmdir_free(a->thargs);
1009         kfree(a);
1010 out:
1011         AuTraceErr(err);
1012         return err;
1013 }