Merge branch 'stable-3.2' into pandora-3.2
[pandora-kernel.git] / fs / aufs / i_op_add.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 operations (add entry)
21  */
22
23 #include "aufs.h"
24
25 /*
26  * final procedure of adding a new entry, except link(2).
27  * remove whiteout, instantiate, copyup the parent dir's times and size
28  * and update version.
29  * if it failed, re-create the removed whiteout.
30  */
31 static int epilog(struct inode *dir, aufs_bindex_t bindex,
32                   struct dentry *wh_dentry, struct dentry *dentry)
33 {
34         int err, rerr;
35         aufs_bindex_t bwh;
36         struct path h_path;
37         struct inode *inode, *h_dir;
38         struct dentry *wh;
39
40         bwh = -1;
41         if (wh_dentry) {
42                 h_dir = wh_dentry->d_parent->d_inode; /* dir inode is locked */
43                 IMustLock(h_dir);
44                 AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
45                 bwh = au_dbwh(dentry);
46                 h_path.dentry = wh_dentry;
47                 h_path.mnt = au_sbr_mnt(dir->i_sb, bindex);
48                 err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
49                                           dentry);
50                 if (unlikely(err))
51                         goto out;
52         }
53
54         inode = au_new_inode(dentry, /*must_new*/1);
55         if (!IS_ERR(inode)) {
56                 d_instantiate(dentry, inode);
57                 dir = dentry->d_parent->d_inode; /* dir inode is locked */
58                 IMustLock(dir);
59                 if (au_ibstart(dir) == au_dbstart(dentry))
60                         au_cpup_attr_timesizes(dir);
61                 dir->i_version++;
62                 return 0; /* success */
63         }
64
65         err = PTR_ERR(inode);
66         if (!wh_dentry)
67                 goto out;
68
69         /* revert */
70         /* dir inode is locked */
71         wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
72         rerr = PTR_ERR(wh);
73         if (IS_ERR(wh)) {
74                 AuIOErr("%.*s reverting whiteout failed(%d, %d)\n",
75                         AuDLNPair(dentry), err, rerr);
76                 err = -EIO;
77         } else
78                 dput(wh);
79
80 out:
81         return err;
82 }
83
84 static int au_d_may_add(struct dentry *dentry)
85 {
86         int err;
87
88         err = 0;
89         if (unlikely(d_unhashed(dentry)))
90                 err = -ENOENT;
91         if (unlikely(dentry->d_inode))
92                 err = -EEXIST;
93         return err;
94 }
95
96 /*
97  * simple tests for the adding inode operations.
98  * following the checks in vfs, plus the parent-child relationship.
99  */
100 int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
101                struct dentry *h_parent, int isdir)
102 {
103         int err;
104         umode_t h_mode;
105         struct dentry *h_dentry;
106         struct inode *h_inode;
107
108         err = -ENAMETOOLONG;
109         if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
110                 goto out;
111
112         h_dentry = au_h_dptr(dentry, bindex);
113         h_inode = h_dentry->d_inode;
114         if (!dentry->d_inode) {
115                 err = -EEXIST;
116                 if (unlikely(h_inode))
117                         goto out;
118         } else {
119                 /* rename(2) case */
120                 err = -EIO;
121                 if (unlikely(!h_inode || !h_inode->i_nlink))
122                         goto out;
123
124                 h_mode = h_inode->i_mode;
125                 if (!isdir) {
126                         err = -EISDIR;
127                         if (unlikely(S_ISDIR(h_mode)))
128                                 goto out;
129                 } else if (unlikely(!S_ISDIR(h_mode))) {
130                         err = -ENOTDIR;
131                         goto out;
132                 }
133         }
134
135         err = 0;
136         /* expected parent dir is locked */
137         if (unlikely(h_parent != h_dentry->d_parent))
138                 err = -EIO;
139
140 out:
141         AuTraceErr(err);
142         return err;
143 }
144
145 /*
146  * initial procedure of adding a new entry.
147  * prepare writable branch and the parent dir, lock it,
148  * and lookup whiteout for the new entry.
149  */
150 static struct dentry*
151 lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
152                   struct dentry *src_dentry, struct au_pin *pin,
153                   struct au_wr_dir_args *wr_dir_args)
154 {
155         struct dentry *wh_dentry, *h_parent;
156         struct super_block *sb;
157         struct au_branch *br;
158         int err;
159         unsigned int udba;
160         aufs_bindex_t bcpup;
161
162         AuDbg("%.*s\n", AuDLNPair(dentry));
163
164         err = au_wr_dir(dentry, src_dentry, wr_dir_args);
165         bcpup = err;
166         wh_dentry = ERR_PTR(err);
167         if (unlikely(err < 0))
168                 goto out;
169
170         sb = dentry->d_sb;
171         udba = au_opt_udba(sb);
172         err = au_pin(pin, dentry, bcpup, udba,
173                      AuPin_DI_LOCKED | AuPin_MNT_WRITE);
174         wh_dentry = ERR_PTR(err);
175         if (unlikely(err))
176                 goto out;
177
178         h_parent = au_pinned_h_parent(pin);
179         if (udba != AuOpt_UDBA_NONE
180             && au_dbstart(dentry) == bcpup)
181                 err = au_may_add(dentry, bcpup, h_parent,
182                                  au_ftest_wrdir(wr_dir_args->flags, ISDIR));
183         else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
184                 err = -ENAMETOOLONG;
185         wh_dentry = ERR_PTR(err);
186         if (unlikely(err))
187                 goto out_unpin;
188
189         br = au_sbr(sb, bcpup);
190         if (dt) {
191                 struct path tmp = {
192                         .dentry = h_parent,
193                         .mnt    = au_br_mnt(br)
194                 };
195                 au_dtime_store(dt, au_pinned_parent(pin), &tmp);
196         }
197
198         wh_dentry = NULL;
199         if (bcpup != au_dbwh(dentry))
200                 goto out; /* success */
201
202         wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
203
204 out_unpin:
205         if (IS_ERR(wh_dentry))
206                 au_unpin(pin);
207 out:
208         return wh_dentry;
209 }
210
211 /* ---------------------------------------------------------------------- */
212
213 enum { Mknod, Symlink, Creat };
214 struct simple_arg {
215         int type;
216         union {
217                 struct {
218                         int mode;
219                         struct nameidata *nd;
220                 } c;
221                 struct {
222                         const char *symname;
223                 } s;
224                 struct {
225                         int mode;
226                         dev_t dev;
227                 } m;
228         } u;
229 };
230
231 static int add_simple(struct inode *dir, struct dentry *dentry,
232                       struct simple_arg *arg)
233 {
234         int err;
235         aufs_bindex_t bstart;
236         unsigned char created;
237         struct dentry *wh_dentry, *parent;
238         struct inode *h_dir;
239         /* to reuduce stack size */
240         struct {
241                 struct au_dtime dt;
242                 struct au_pin pin;
243                 struct path h_path;
244                 struct au_wr_dir_args wr_dir_args;
245         } *a;
246
247         AuDbg("%.*s\n", AuDLNPair(dentry));
248         IMustLock(dir);
249
250         err = -ENOMEM;
251         a = kmalloc(sizeof(*a), GFP_NOFS);
252         if (unlikely(!a))
253                 goto out;
254         a->wr_dir_args.force_btgt = -1;
255         a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
256
257         parent = dentry->d_parent; /* dir inode is locked */
258         err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
259         if (unlikely(err))
260                 goto out_free;
261         err = au_d_may_add(dentry);
262         if (unlikely(err))
263                 goto out_unlock;
264         di_write_lock_parent(parent);
265         wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
266                                       &a->pin, &a->wr_dir_args);
267         err = PTR_ERR(wh_dentry);
268         if (IS_ERR(wh_dentry))
269                 goto out_parent;
270
271         bstart = au_dbstart(dentry);
272         a->h_path.dentry = au_h_dptr(dentry, bstart);
273         a->h_path.mnt = au_sbr_mnt(dentry->d_sb, bstart);
274         h_dir = au_pinned_h_dir(&a->pin);
275         switch (arg->type) {
276         case Creat:
277                 err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode);
278                 break;
279         case Symlink:
280                 err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
281                 break;
282         case Mknod:
283                 err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
284                                   arg->u.m.dev);
285                 break;
286         default:
287                 BUG();
288         }
289         created = !err;
290         if (!err)
291                 err = epilog(dir, bstart, wh_dentry, dentry);
292
293         /* revert */
294         if (unlikely(created && err && a->h_path.dentry->d_inode)) {
295                 int rerr;
296                 rerr = vfsub_unlink(h_dir, &a->h_path, /*force*/0);
297                 if (rerr) {
298                         AuIOErr("%.*s revert failure(%d, %d)\n",
299                                 AuDLNPair(dentry), err, rerr);
300                         err = -EIO;
301                 }
302                 au_dtime_revert(&a->dt);
303         }
304
305         au_unpin(&a->pin);
306         dput(wh_dentry);
307
308 out_parent:
309         di_write_unlock(parent);
310 out_unlock:
311         if (unlikely(err)) {
312                 au_update_dbstart(dentry);
313                 d_drop(dentry);
314         }
315         aufs_read_unlock(dentry, AuLock_DW);
316 out_free:
317         kfree(a);
318 out:
319         return err;
320 }
321
322 int aufs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
323 {
324         struct simple_arg arg = {
325                 .type = Mknod,
326                 .u.m = {
327                         .mode   = mode,
328                         .dev    = dev
329                 }
330         };
331         return add_simple(dir, dentry, &arg);
332 }
333
334 int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
335 {
336         struct simple_arg arg = {
337                 .type = Symlink,
338                 .u.s.symname = symname
339         };
340         return add_simple(dir, dentry, &arg);
341 }
342
343 int aufs_create(struct inode *dir, struct dentry *dentry, int mode,
344                 struct nameidata *nd)
345 {
346         struct simple_arg arg = {
347                 .type = Creat,
348                 .u.c = {
349                         .mode   = mode,
350                         .nd     = nd
351                 }
352         };
353         return add_simple(dir, dentry, &arg);
354 }
355
356 /* ---------------------------------------------------------------------- */
357
358 struct au_link_args {
359         aufs_bindex_t bdst, bsrc;
360         struct au_pin pin;
361         struct path h_path;
362         struct dentry *src_parent, *parent;
363 };
364
365 static int au_cpup_before_link(struct dentry *src_dentry,
366                                struct au_link_args *a)
367 {
368         int err;
369         struct dentry *h_src_dentry;
370         struct au_cp_generic cpg = {
371                 .dentry = src_dentry,
372                 .bdst   = a->bdst,
373                 .bsrc   = a->bsrc,
374                 .len    = -1,
375                 .pin    = &a->pin,
376                 .flags  = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
377         };
378
379         di_read_lock_parent(a->src_parent, AuLock_IR);
380         err = au_test_and_cpup_dirs(src_dentry, a->bdst);
381         if (unlikely(err))
382                 goto out;
383
384         h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
385         err = au_pin(&a->pin, src_dentry, a->bdst,
386                      au_opt_udba(src_dentry->d_sb),
387                      AuPin_DI_LOCKED | AuPin_MNT_WRITE);
388         if (unlikely(err))
389                 goto out;
390
391         err = au_sio_cpup_simple(&cpg);
392         au_unpin(&a->pin);
393
394 out:
395         di_read_unlock(a->src_parent, AuLock_IR);
396         return err;
397 }
398
399 static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
400                            struct au_link_args *a)
401 {
402         int err;
403         unsigned char plink;
404         aufs_bindex_t bend;
405         struct dentry *h_src_dentry;
406         struct inode *h_inode, *inode;
407         struct super_block *sb;
408         struct file *h_file;
409
410         plink = 0;
411         h_inode = NULL;
412         sb = src_dentry->d_sb;
413         inode = src_dentry->d_inode;
414         if (au_ibstart(inode) <= a->bdst)
415                 h_inode = au_h_iptr(inode, a->bdst);
416         if (!h_inode || !h_inode->i_nlink) {
417                 /* copyup src_dentry as the name of dentry. */
418                 bend = au_dbend(dentry);
419                 if (bend < a->bsrc)
420                         au_set_dbend(dentry, a->bsrc);
421                 au_set_h_dptr(dentry, a->bsrc,
422                               dget(au_h_dptr(src_dentry, a->bsrc)));
423                 dget(a->h_path.dentry);
424                 au_set_h_dptr(dentry, a->bdst, NULL);
425                 dentry->d_inode = src_dentry->d_inode; /* tmp */
426                 h_file = au_h_open_pre(dentry, a->bsrc);
427                 if (IS_ERR(h_file))
428                         err = PTR_ERR(h_file);
429                 else {
430                         struct au_cp_generic cpg = {
431                                 .dentry = dentry,
432                                 .bdst   = a->bdst,
433                                 .bsrc   = -1,
434                                 .len    = -1,
435                                 .pin    = &a->pin,
436                                 .flags  = AuCpup_KEEPLINO
437                         };
438                         err = au_sio_cpup_simple(&cpg);
439                         au_h_open_post(dentry, a->bsrc, h_file);
440                         if (!err) {
441                                 dput(a->h_path.dentry);
442                                 a->h_path.dentry = au_h_dptr(dentry, a->bdst);
443                         } else
444                                 au_set_h_dptr(dentry, a->bdst,
445                                               a->h_path.dentry);
446                 }
447                 dentry->d_inode = NULL; /* restore */
448                 au_set_h_dptr(dentry, a->bsrc, NULL);
449                 au_set_dbend(dentry, bend);
450         } else {
451                 /* the inode of src_dentry already exists on a.bdst branch */
452                 h_src_dentry = d_find_alias(h_inode);
453                 if (!h_src_dentry && au_plink_test(inode)) {
454                         plink = 1;
455                         h_src_dentry = au_plink_lkup(inode, a->bdst);
456                         err = PTR_ERR(h_src_dentry);
457                         if (IS_ERR(h_src_dentry))
458                                 goto out;
459
460                         if (unlikely(!h_src_dentry->d_inode)) {
461                                 dput(h_src_dentry);
462                                 h_src_dentry = NULL;
463                         }
464
465                 }
466                 if (h_src_dentry) {
467                         err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
468                                          &a->h_path);
469                         dput(h_src_dentry);
470                 } else {
471                         AuIOErr("no dentry found for hi%lu on b%d\n",
472                                 h_inode->i_ino, a->bdst);
473                         err = -EIO;
474                 }
475         }
476
477         if (!err && !plink)
478                 au_plink_append(inode, a->bdst, a->h_path.dentry);
479
480 out:
481         AuTraceErr(err);
482         return err;
483 }
484
485 int aufs_link(struct dentry *src_dentry, struct inode *dir,
486               struct dentry *dentry)
487 {
488         int err, rerr;
489         struct au_dtime dt;
490         struct au_link_args *a;
491         struct dentry *wh_dentry, *h_src_dentry;
492         struct inode *inode;
493         struct super_block *sb;
494         struct au_wr_dir_args wr_dir_args = {
495                 /* .force_btgt  = -1, */
496                 .flags          = AuWrDir_ADD_ENTRY
497         };
498
499         IMustLock(dir);
500         inode = src_dentry->d_inode;
501         IMustLock(inode);
502
503         err = -ENOMEM;
504         a = kzalloc(sizeof(*a), GFP_NOFS);
505         if (unlikely(!a))
506                 goto out;
507
508         a->parent = dentry->d_parent; /* dir inode is locked */
509         err = aufs_read_and_write_lock2(dentry, src_dentry,
510                                         AuLock_NOPLM | AuLock_GEN);
511         if (unlikely(err))
512                 goto out_kfree;
513         err = au_d_hashed_positive(src_dentry);
514         if (unlikely(err))
515                 goto out_unlock;
516         err = au_d_may_add(dentry);
517         if (unlikely(err))
518                 goto out_unlock;
519
520         a->src_parent = dget_parent(src_dentry);
521         wr_dir_args.force_btgt = au_ibstart(inode);
522
523         di_write_lock_parent(a->parent);
524         wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
525         wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
526                                       &wr_dir_args);
527         err = PTR_ERR(wh_dentry);
528         if (IS_ERR(wh_dentry))
529                 goto out_parent;
530
531         err = 0;
532         sb = dentry->d_sb;
533         a->bdst = au_dbstart(dentry);
534         a->h_path.dentry = au_h_dptr(dentry, a->bdst);
535         a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
536         a->bsrc = au_ibstart(inode);
537         h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
538         if (!h_src_dentry) {
539                 a->bsrc = au_dbstart(src_dentry);
540                 h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
541                 AuDebugOn(!h_src_dentry);
542         } else if (IS_ERR(h_src_dentry))
543                 goto out_parent;
544
545         if (au_opt_test(au_mntflags(sb), PLINK)) {
546                 if (a->bdst < a->bsrc
547                     /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
548                         err = au_cpup_or_link(src_dentry, dentry, a);
549                 else
550                         err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
551                                          &a->h_path);
552                 dput(h_src_dentry);
553         } else {
554                 /*
555                  * copyup src_dentry to the branch we process,
556                  * and then link(2) to it.
557                  */
558                 dput(h_src_dentry);
559                 if (a->bdst < a->bsrc
560                     /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
561                         au_unpin(&a->pin);
562                         di_write_unlock(a->parent);
563                         err = au_cpup_before_link(src_dentry, a);
564                         di_write_lock_parent(a->parent);
565                         if (!err)
566                                 err = au_pin(&a->pin, dentry, a->bdst,
567                                              au_opt_udba(sb),
568                                              AuPin_DI_LOCKED | AuPin_MNT_WRITE);
569                         if (unlikely(err))
570                                 goto out_wh;
571                 }
572                 if (!err) {
573                         h_src_dentry = au_h_dptr(src_dentry, a->bdst);
574                         err = -ENOENT;
575                         if (h_src_dentry && h_src_dentry->d_inode)
576                                 err = vfsub_link(h_src_dentry,
577                                                  au_pinned_h_dir(&a->pin),
578                                                  &a->h_path);
579                 }
580         }
581         if (unlikely(err))
582                 goto out_unpin;
583
584         if (wh_dentry) {
585                 a->h_path.dentry = wh_dentry;
586                 err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
587                                           dentry);
588                 if (unlikely(err))
589                         goto out_revert;
590         }
591
592         dir->i_version++;
593         if (au_ibstart(dir) == au_dbstart(dentry))
594                 au_cpup_attr_timesizes(dir);
595         inc_nlink(inode);
596         inode->i_ctime = dir->i_ctime;
597         d_instantiate(dentry, au_igrab(inode));
598         if (d_unhashed(a->h_path.dentry))
599                 /* some filesystem calls d_drop() */
600                 d_drop(dentry);
601         goto out_unpin; /* success */
602
603 out_revert:
604         rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path, /*force*/0);
605         if (unlikely(rerr)) {
606                 AuIOErr("%.*s reverting failed(%d, %d)\n",
607                         AuDLNPair(dentry), err, rerr);
608                 err = -EIO;
609         }
610         au_dtime_revert(&dt);
611 out_unpin:
612         au_unpin(&a->pin);
613 out_wh:
614         dput(wh_dentry);
615 out_parent:
616         di_write_unlock(a->parent);
617         dput(a->src_parent);
618 out_unlock:
619         if (unlikely(err)) {
620                 au_update_dbstart(dentry);
621                 d_drop(dentry);
622         }
623         aufs_read_and_write_unlock2(dentry, src_dentry);
624 out_kfree:
625         kfree(a);
626 out:
627         AuTraceErr(err);
628         return err;
629 }
630
631 int aufs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
632 {
633         int err, rerr;
634         aufs_bindex_t bindex;
635         unsigned char diropq;
636         struct path h_path;
637         struct dentry *wh_dentry, *parent, *opq_dentry;
638         struct mutex *h_mtx;
639         struct super_block *sb;
640         struct {
641                 struct au_pin pin;
642                 struct au_dtime dt;
643         } *a; /* reduce the stack usage */
644         struct au_wr_dir_args wr_dir_args = {
645                 .force_btgt     = -1,
646                 .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
647         };
648
649         IMustLock(dir);
650
651         err = -ENOMEM;
652         a = kmalloc(sizeof(*a), GFP_NOFS);
653         if (unlikely(!a))
654                 goto out;
655
656         err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
657         if (unlikely(err))
658                 goto out_free;
659         err = au_d_may_add(dentry);
660         if (unlikely(err))
661                 goto out_unlock;
662
663         parent = dentry->d_parent; /* dir inode is locked */
664         di_write_lock_parent(parent);
665         wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
666                                       &a->pin, &wr_dir_args);
667         err = PTR_ERR(wh_dentry);
668         if (IS_ERR(wh_dentry))
669                 goto out_parent;
670
671         sb = dentry->d_sb;
672         bindex = au_dbstart(dentry);
673         h_path.dentry = au_h_dptr(dentry, bindex);
674         h_path.mnt = au_sbr_mnt(sb, bindex);
675         err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
676         if (unlikely(err))
677                 goto out_unpin;
678
679         /* make the dir opaque */
680         diropq = 0;
681         h_mtx = &h_path.dentry->d_inode->i_mutex;
682         if (wh_dentry
683             || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
684                 mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
685                 opq_dentry = au_diropq_create(dentry, bindex);
686                 mutex_unlock(h_mtx);
687                 err = PTR_ERR(opq_dentry);
688                 if (IS_ERR(opq_dentry))
689                         goto out_dir;
690                 dput(opq_dentry);
691                 diropq = 1;
692         }
693
694         err = epilog(dir, bindex, wh_dentry, dentry);
695         if (!err) {
696                 inc_nlink(dir);
697                 goto out_unpin; /* success */
698         }
699
700         /* revert */
701         if (diropq) {
702                 AuLabel(revert opq);
703                 mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
704                 rerr = au_diropq_remove(dentry, bindex);
705                 mutex_unlock(h_mtx);
706                 if (rerr) {
707                         AuIOErr("%.*s reverting diropq failed(%d, %d)\n",
708                                 AuDLNPair(dentry), err, rerr);
709                         err = -EIO;
710                 }
711         }
712
713 out_dir:
714         AuLabel(revert dir);
715         rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
716         if (rerr) {
717                 AuIOErr("%.*s reverting dir failed(%d, %d)\n",
718                         AuDLNPair(dentry), err, rerr);
719                 err = -EIO;
720         }
721         au_dtime_revert(&a->dt);
722 out_unpin:
723         au_unpin(&a->pin);
724         dput(wh_dentry);
725 out_parent:
726         di_write_unlock(parent);
727 out_unlock:
728         if (unlikely(err)) {
729                 au_update_dbstart(dentry);
730                 d_drop(dentry);
731         }
732         aufs_read_unlock(dentry, AuLock_DW);
733 out_free:
734         kfree(a);
735 out:
736         return err;
737 }