Merge branch 'stable-3.2' into pandora-3.2
[pandora-kernel.git] / fs / aufs / export.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  * export via nfs
21  */
22
23 #include <linux/exportfs.h>
24 #include <linux/mnt_namespace.h>
25 #include <linux/namei.h>
26 #include <linux/nsproxy.h>
27 #include <linux/random.h>
28 #include <linux/writeback.h>
29 #include "aufs.h"
30
31 union conv {
32 #ifdef CONFIG_AUFS_INO_T_64
33         __u32 a[2];
34 #else
35         __u32 a[1];
36 #endif
37         ino_t ino;
38 };
39
40 static ino_t decode_ino(__u32 *a)
41 {
42         union conv u;
43
44         BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
45         u.a[0] = a[0];
46 #ifdef CONFIG_AUFS_INO_T_64
47         u.a[1] = a[1];
48 #endif
49         return u.ino;
50 }
51
52 static void encode_ino(__u32 *a, ino_t ino)
53 {
54         union conv u;
55
56         u.ino = ino;
57         a[0] = u.a[0];
58 #ifdef CONFIG_AUFS_INO_T_64
59         a[1] = u.a[1];
60 #endif
61 }
62
63 /* NFS file handle */
64 enum {
65         Fh_br_id,
66         Fh_sigen,
67 #ifdef CONFIG_AUFS_INO_T_64
68         /* support 64bit inode number */
69         Fh_ino1,
70         Fh_ino2,
71         Fh_dir_ino1,
72         Fh_dir_ino2,
73 #else
74         Fh_ino1,
75         Fh_dir_ino1,
76 #endif
77         Fh_igen,
78         Fh_h_type,
79         Fh_tail,
80
81         Fh_ino = Fh_ino1,
82         Fh_dir_ino = Fh_dir_ino1
83 };
84
85 static int au_test_anon(struct dentry *dentry)
86 {
87         /* note: read d_flags without d_lock */
88         return !!(dentry->d_flags & DCACHE_DISCONNECTED);
89 }
90
91 int au_test_nfsd(void)
92 {
93         int ret;
94         struct task_struct *tsk = current;
95         char comm[sizeof(tsk->comm)];
96
97         ret = 0;
98         if (tsk->flags & PF_KTHREAD) {
99                 get_task_comm(comm, tsk);
100                 ret = !strcmp(comm, "nfsd");
101         }
102
103         return ret;
104 }
105
106 /* ---------------------------------------------------------------------- */
107 /* inode generation external table */
108
109 void au_xigen_inc(struct inode *inode)
110 {
111         loff_t pos;
112         ssize_t sz;
113         __u32 igen;
114         struct super_block *sb;
115         struct au_sbinfo *sbinfo;
116
117         sb = inode->i_sb;
118         AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
119
120         sbinfo = au_sbi(sb);
121         pos = inode->i_ino;
122         pos *= sizeof(igen);
123         igen = inode->i_generation + 1;
124         sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
125                          sizeof(igen), &pos);
126         if (sz == sizeof(igen))
127                 return; /* success */
128
129         if (unlikely(sz >= 0))
130                 AuIOErr("xigen error (%zd)\n", sz);
131 }
132
133 int au_xigen_new(struct inode *inode)
134 {
135         int err;
136         loff_t pos;
137         ssize_t sz;
138         struct super_block *sb;
139         struct au_sbinfo *sbinfo;
140         struct file *file;
141
142         err = 0;
143         /* todo: dirty, at mount time */
144         if (inode->i_ino == AUFS_ROOT_INO)
145                 goto out;
146         sb = inode->i_sb;
147         SiMustAnyLock(sb);
148         if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
149                 goto out;
150
151         err = -EFBIG;
152         pos = inode->i_ino;
153         if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
154                 AuIOErr1("too large i%lld\n", pos);
155                 goto out;
156         }
157         pos *= sizeof(inode->i_generation);
158
159         err = 0;
160         sbinfo = au_sbi(sb);
161         file = sbinfo->si_xigen;
162         BUG_ON(!file);
163
164         if (i_size_read(file->f_dentry->d_inode)
165             < pos + sizeof(inode->i_generation)) {
166                 inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
167                 sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
168                                  sizeof(inode->i_generation), &pos);
169         } else
170                 sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
171                                 sizeof(inode->i_generation), &pos);
172         if (sz == sizeof(inode->i_generation))
173                 goto out; /* success */
174
175         err = sz;
176         if (unlikely(sz >= 0)) {
177                 err = -EIO;
178                 AuIOErr("xigen error (%zd)\n", sz);
179         }
180
181 out:
182         return err;
183 }
184
185 int au_xigen_set(struct super_block *sb, struct file *base)
186 {
187         int err;
188         struct au_sbinfo *sbinfo;
189         struct file *file;
190
191         SiMustWriteLock(sb);
192
193         sbinfo = au_sbi(sb);
194         file = au_xino_create2(base, sbinfo->si_xigen);
195         err = PTR_ERR(file);
196         if (IS_ERR(file))
197                 goto out;
198         err = 0;
199         if (sbinfo->si_xigen)
200                 fput(sbinfo->si_xigen);
201         sbinfo->si_xigen = file;
202
203 out:
204         return err;
205 }
206
207 void au_xigen_clr(struct super_block *sb)
208 {
209         struct au_sbinfo *sbinfo;
210
211         SiMustWriteLock(sb);
212
213         sbinfo = au_sbi(sb);
214         if (sbinfo->si_xigen) {
215                 fput(sbinfo->si_xigen);
216                 sbinfo->si_xigen = NULL;
217         }
218 }
219
220 /* ---------------------------------------------------------------------- */
221
222 static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
223                                     ino_t dir_ino)
224 {
225         struct dentry *dentry, *d;
226         struct inode *inode;
227         unsigned int sigen;
228
229         dentry = NULL;
230         inode = ilookup(sb, ino);
231         if (!inode)
232                 goto out;
233
234         dentry = ERR_PTR(-ESTALE);
235         sigen = au_sigen(sb);
236         if (unlikely(is_bad_inode(inode)
237                      || IS_DEADDIR(inode)
238                      || sigen != au_iigen(inode, NULL)))
239                 goto out_iput;
240
241         dentry = NULL;
242         if (!dir_ino || S_ISDIR(inode->i_mode))
243                 dentry = d_find_alias(inode);
244         else {
245                 spin_lock(&inode->i_lock);
246                 list_for_each_entry(d, &inode->i_dentry, d_alias) {
247                         spin_lock(&d->d_lock);
248                         if (!au_test_anon(d)
249                             && d->d_parent->d_inode->i_ino == dir_ino) {
250                                 dentry = dget_dlock(d);
251                                 spin_unlock(&d->d_lock);
252                                 break;
253                         }
254                         spin_unlock(&d->d_lock);
255                 }
256                 spin_unlock(&inode->i_lock);
257         }
258         if (unlikely(dentry && au_digen_test(dentry, sigen))) {
259                 /* need to refresh */
260                 dput(dentry);
261                 dentry = NULL;
262         }
263
264 out_iput:
265         iput(inode);
266 out:
267         AuTraceErrPtr(dentry);
268         return dentry;
269 }
270
271 /* ---------------------------------------------------------------------- */
272
273 /* todo: dirty? */
274 /* if exportfs_decode_fh() passed vfsmount*, we could be happy */
275
276 struct au_compare_mnt_args {
277         /* input */
278         struct super_block *sb;
279
280         /* output */
281         struct vfsmount *mnt;
282 };
283
284 static int au_compare_mnt(struct vfsmount *mnt, void *arg)
285 {
286         struct au_compare_mnt_args *a = arg;
287
288         if (mnt->mnt_sb != a->sb)
289                 return 0;
290         a->mnt = mntget(mnt);
291         return 1;
292 }
293
294 static struct vfsmount *au_mnt_get(struct super_block *sb)
295 {
296         int err;
297         struct au_compare_mnt_args args = {
298                 .sb = sb
299         };
300         struct mnt_namespace *ns;
301
302         br_read_lock(vfsmount_lock);
303         /* no get/put ?? */
304         AuDebugOn(!current->nsproxy);
305         ns = current->nsproxy->mnt_ns;
306         AuDebugOn(!ns);
307         err = iterate_mounts(au_compare_mnt, &args, ns->root);
308         br_read_unlock(vfsmount_lock);
309         AuDebugOn(!err);
310         AuDebugOn(!args.mnt);
311         return args.mnt;
312 }
313
314 struct au_nfsd_si_lock {
315         unsigned int sigen;
316         aufs_bindex_t bindex, br_id;
317         unsigned char force_lock;
318 };
319
320 static int si_nfsd_read_lock(struct super_block *sb,
321                              struct au_nfsd_si_lock *nsi_lock)
322 {
323         int err;
324         aufs_bindex_t bindex;
325
326         si_read_lock(sb, AuLock_FLUSH);
327
328         /* branch id may be wrapped around */
329         err = 0;
330         bindex = au_br_index(sb, nsi_lock->br_id);
331         if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
332                 goto out; /* success */
333
334         err = -ESTALE;
335         bindex = -1;
336         if (!nsi_lock->force_lock)
337                 si_read_unlock(sb);
338
339 out:
340         nsi_lock->bindex = bindex;
341         return err;
342 }
343
344 struct find_name_by_ino {
345         int called, found;
346         ino_t ino;
347         char *name;
348         int namelen;
349 };
350
351 static int
352 find_name_by_ino(void *arg, const char *name, int namelen, loff_t offset,
353                  u64 ino, unsigned int d_type)
354 {
355         struct find_name_by_ino *a = arg;
356
357         a->called++;
358         if (a->ino != ino)
359                 return 0;
360
361         memcpy(a->name, name, namelen);
362         a->namelen = namelen;
363         a->found = 1;
364         return 1;
365 }
366
367 static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
368                                      struct au_nfsd_si_lock *nsi_lock)
369 {
370         struct dentry *dentry, *parent;
371         struct file *file;
372         struct inode *dir;
373         struct find_name_by_ino arg;
374         int err;
375
376         parent = path->dentry;
377         if (nsi_lock)
378                 si_read_unlock(parent->d_sb);
379         file = vfsub_dentry_open(path, au_dir_roflags);
380         dentry = (void *)file;
381         if (IS_ERR(file))
382                 goto out;
383
384         dentry = ERR_PTR(-ENOMEM);
385         arg.name = __getname_gfp(GFP_NOFS);
386         if (unlikely(!arg.name))
387                 goto out_file;
388         arg.ino = ino;
389         arg.found = 0;
390         do {
391                 arg.called = 0;
392                 /* smp_mb(); */
393                 err = vfsub_readdir(file, find_name_by_ino, &arg);
394         } while (!err && !arg.found && arg.called);
395         dentry = ERR_PTR(err);
396         if (unlikely(err))
397                 goto out_name;
398         /* instead of ENOENT */
399         dentry = ERR_PTR(-ESTALE);
400         if (!arg.found)
401                 goto out_name;
402
403         /* do not call au_lkup_one() */
404         dir = parent->d_inode;
405         mutex_lock(&dir->i_mutex);
406         dentry = vfsub_lookup_one_len(arg.name, parent, arg.namelen);
407         mutex_unlock(&dir->i_mutex);
408         AuTraceErrPtr(dentry);
409         if (IS_ERR(dentry))
410                 goto out_name;
411         AuDebugOn(au_test_anon(dentry));
412         if (unlikely(!dentry->d_inode)) {
413                 dput(dentry);
414                 dentry = ERR_PTR(-ENOENT);
415         }
416
417 out_name:
418         __putname(arg.name);
419 out_file:
420         fput(file);
421 out:
422         if (unlikely(nsi_lock
423                      && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
424                 if (!IS_ERR(dentry)) {
425                         dput(dentry);
426                         dentry = ERR_PTR(-ESTALE);
427                 }
428         AuTraceErrPtr(dentry);
429         return dentry;
430 }
431
432 static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
433                                         ino_t dir_ino,
434                                         struct au_nfsd_si_lock *nsi_lock)
435 {
436         struct dentry *dentry;
437         struct path path;
438
439         if (dir_ino != AUFS_ROOT_INO) {
440                 path.dentry = decode_by_ino(sb, dir_ino, 0);
441                 dentry = path.dentry;
442                 if (!path.dentry || IS_ERR(path.dentry))
443                         goto out;
444                 AuDebugOn(au_test_anon(path.dentry));
445         } else
446                 path.dentry = dget(sb->s_root);
447
448         path.mnt = au_mnt_get(sb);
449         dentry = au_lkup_by_ino(&path, ino, nsi_lock);
450         path_put(&path);
451
452 out:
453         AuTraceErrPtr(dentry);
454         return dentry;
455 }
456
457 /* ---------------------------------------------------------------------- */
458
459 static int h_acceptable(void *expv, struct dentry *dentry)
460 {
461         return 1;
462 }
463
464 static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
465                            char *buf, int len, struct super_block *sb)
466 {
467         char *p;
468         int n;
469         struct path path;
470
471         p = d_path(h_rootpath, buf, len);
472         if (IS_ERR(p))
473                 goto out;
474         n = strlen(p);
475
476         path.mnt = h_rootpath->mnt;
477         path.dentry = h_parent;
478         p = d_path(&path, buf, len);
479         if (IS_ERR(p))
480                 goto out;
481         if (n != 1)
482                 p += n;
483
484         path.mnt = au_mnt_get(sb);
485         path.dentry = sb->s_root;
486         p = d_path(&path, buf, len - strlen(p));
487         mntput(path.mnt);
488         if (IS_ERR(p))
489                 goto out;
490         if (n != 1)
491                 p[strlen(p)] = '/';
492
493 out:
494         AuTraceErrPtr(p);
495         return p;
496 }
497
498 static
499 struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
500                               int fh_len, struct au_nfsd_si_lock *nsi_lock)
501 {
502         struct dentry *dentry, *h_parent, *root;
503         struct super_block *h_sb;
504         char *pathname, *p;
505         struct vfsmount *h_mnt;
506         struct au_branch *br;
507         int err;
508         struct path path;
509
510         br = au_sbr(sb, nsi_lock->bindex);
511         h_mnt = au_br_mnt(br);
512         h_sb = h_mnt->mnt_sb;
513         /* todo: call lower fh_to_dentry()? fh_to_parent()? */
514         h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
515                                       fh_len - Fh_tail, fh[Fh_h_type],
516                                       h_acceptable, /*context*/NULL);
517         dentry = h_parent;
518         if (unlikely(!h_parent || IS_ERR(h_parent))) {
519                 AuWarn1("%s decode_fh failed, %ld\n",
520                         au_sbtype(h_sb), PTR_ERR(h_parent));
521                 goto out;
522         }
523         dentry = NULL;
524         if (unlikely(au_test_anon(h_parent))) {
525                 AuWarn1("%s decode_fh returned a disconnected dentry\n",
526                         au_sbtype(h_sb));
527                 goto out_h_parent;
528         }
529
530         dentry = ERR_PTR(-ENOMEM);
531         pathname = (void *)__get_free_page(GFP_NOFS);
532         if (unlikely(!pathname))
533                 goto out_h_parent;
534
535         root = sb->s_root;
536         path.mnt = h_mnt;
537         di_read_lock_parent(root, !AuLock_IR);
538         path.dentry = au_h_dptr(root, nsi_lock->bindex);
539         di_read_unlock(root, !AuLock_IR);
540         p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
541         dentry = (void *)p;
542         if (IS_ERR(p))
543                 goto out_pathname;
544
545         si_read_unlock(sb);
546         err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
547         dentry = ERR_PTR(err);
548         if (unlikely(err))
549                 goto out_relock;
550
551         dentry = ERR_PTR(-ENOENT);
552         AuDebugOn(au_test_anon(path.dentry));
553         if (unlikely(!path.dentry->d_inode))
554                 goto out_path;
555
556         if (ino != path.dentry->d_inode->i_ino)
557                 dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
558         else
559                 dentry = dget(path.dentry);
560
561 out_path:
562         path_put(&path);
563 out_relock:
564         if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
565                 if (!IS_ERR(dentry)) {
566                         dput(dentry);
567                         dentry = ERR_PTR(-ESTALE);
568                 }
569 out_pathname:
570         free_page((unsigned long)pathname);
571 out_h_parent:
572         dput(h_parent);
573 out:
574         AuTraceErrPtr(dentry);
575         return dentry;
576 }
577
578 /* ---------------------------------------------------------------------- */
579
580 static struct dentry *
581 aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
582                   int fh_type)
583 {
584         struct dentry *dentry;
585         __u32 *fh = fid->raw;
586         struct au_branch *br;
587         ino_t ino, dir_ino;
588         struct au_nfsd_si_lock nsi_lock = {
589                 .force_lock     = 0
590         };
591
592         dentry = ERR_PTR(-ESTALE);
593         /* it should never happen, but the file handle is unreliable */
594         if (unlikely(fh_len < Fh_tail))
595                 goto out;
596         nsi_lock.sigen = fh[Fh_sigen];
597         nsi_lock.br_id = fh[Fh_br_id];
598
599         /* branch id may be wrapped around */
600         br = NULL;
601         if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
602                 goto out;
603         nsi_lock.force_lock = 1;
604
605         /* is this inode still cached? */
606         ino = decode_ino(fh + Fh_ino);
607         /* it should never happen */
608         if (unlikely(ino == AUFS_ROOT_INO))
609                 goto out;
610
611         dir_ino = decode_ino(fh + Fh_dir_ino);
612         dentry = decode_by_ino(sb, ino, dir_ino);
613         if (IS_ERR(dentry))
614                 goto out_unlock;
615         if (dentry)
616                 goto accept;
617
618         /* is the parent dir cached? */
619         br = au_sbr(sb, nsi_lock.bindex);
620         atomic_inc(&br->br_count);
621         dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
622         if (IS_ERR(dentry))
623                 goto out_unlock;
624         if (dentry)
625                 goto accept;
626
627         /* lookup path */
628         dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
629         if (IS_ERR(dentry))
630                 goto out_unlock;
631         if (unlikely(!dentry))
632                 /* todo?: make it ESTALE */
633                 goto out_unlock;
634
635 accept:
636         if (!au_digen_test(dentry, au_sigen(sb))
637             && dentry->d_inode->i_generation == fh[Fh_igen])
638                 goto out_unlock; /* success */
639
640         dput(dentry);
641         dentry = ERR_PTR(-ESTALE);
642 out_unlock:
643         if (br)
644                 atomic_dec(&br->br_count);
645         si_read_unlock(sb);
646 out:
647         AuTraceErrPtr(dentry);
648         return dentry;
649 }
650
651 #if 0 /* reserved for future use */
652 /* support subtreecheck option */
653 static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
654                                         int fh_len, int fh_type)
655 {
656         struct dentry *parent;
657         __u32 *fh = fid->raw;
658         ino_t dir_ino;
659
660         dir_ino = decode_ino(fh + Fh_dir_ino);
661         parent = decode_by_ino(sb, dir_ino, 0);
662         if (IS_ERR(parent))
663                 goto out;
664         if (!parent)
665                 parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
666                                         dir_ino, fh, fh_len);
667
668 out:
669         AuTraceErrPtr(parent);
670         return parent;
671 }
672 #endif
673
674 /* ---------------------------------------------------------------------- */
675
676 static int aufs_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len,
677                           int connectable)
678 {
679         int err;
680         aufs_bindex_t bindex, bend;
681         struct super_block *sb, *h_sb;
682         struct inode *inode;
683         struct dentry *parent, *h_parent;
684         struct au_branch *br;
685
686         AuDebugOn(au_test_anon(dentry));
687
688         parent = NULL;
689         err = -ENOSPC;
690         if (unlikely(*max_len <= Fh_tail)) {
691                 AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
692                 goto out;
693         }
694
695         err = FILEID_ROOT;
696         if (IS_ROOT(dentry)) {
697                 AuDebugOn(dentry->d_inode->i_ino != AUFS_ROOT_INO);
698                 goto out;
699         }
700
701         h_parent = NULL;
702         err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_IR | AuLock_GEN);
703         if (unlikely(err))
704                 goto out;
705
706         inode = dentry->d_inode;
707         AuDebugOn(!inode);
708         sb = dentry->d_sb;
709 #ifdef CONFIG_AUFS_DEBUG
710         if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
711                 AuWarn1("NFS-exporting requires xino\n");
712 #endif
713         err = -EIO;
714         parent = dget_parent(dentry);
715         di_read_lock_parent(parent, !AuLock_IR);
716         bend = au_dbtaildir(parent);
717         for (bindex = au_dbstart(parent); bindex <= bend; bindex++) {
718                 h_parent = au_h_dptr(parent, bindex);
719                 if (h_parent) {
720                         dget(h_parent);
721                         break;
722                 }
723         }
724         if (unlikely(!h_parent))
725                 goto out_unlock;
726
727         err = -EPERM;
728         br = au_sbr(sb, bindex);
729         h_sb = au_br_sb(br);
730         if (unlikely(!h_sb->s_export_op)) {
731                 AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
732                 goto out_dput;
733         }
734
735         fh[Fh_br_id] = br->br_id;
736         fh[Fh_sigen] = au_sigen(sb);
737         encode_ino(fh + Fh_ino, inode->i_ino);
738         encode_ino(fh + Fh_dir_ino, parent->d_inode->i_ino);
739         fh[Fh_igen] = inode->i_generation;
740
741         *max_len -= Fh_tail;
742         fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
743                                            max_len,
744                                            /*connectable or subtreecheck*/0);
745         err = fh[Fh_h_type];
746         *max_len += Fh_tail;
747         /* todo: macros? */
748         if (err != 255)
749                 err = 99;
750         else
751                 AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
752
753 out_dput:
754         dput(h_parent);
755 out_unlock:
756         di_read_unlock(parent, !AuLock_IR);
757         dput(parent);
758         aufs_read_unlock(dentry, AuLock_IR);
759 out:
760         if (unlikely(err < 0))
761                 err = 255;
762         return err;
763 }
764
765 /* ---------------------------------------------------------------------- */
766
767 static int aufs_commit_metadata(struct inode *inode)
768 {
769         int err;
770         aufs_bindex_t bindex;
771         struct super_block *sb;
772         struct inode *h_inode;
773         int (*f)(struct inode *inode);
774
775         sb = inode->i_sb;
776         si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
777         ii_write_lock_child(inode);
778         bindex = au_ibstart(inode);
779         AuDebugOn(bindex < 0);
780         h_inode = au_h_iptr(inode, bindex);
781
782         f = h_inode->i_sb->s_export_op->commit_metadata;
783         if (f)
784                 err = f(h_inode);
785         else {
786                 struct writeback_control wbc = {
787                         .sync_mode      = WB_SYNC_ALL,
788                         .nr_to_write    = 0 /* metadata only */
789                 };
790
791                 err = sync_inode(h_inode, &wbc);
792         }
793
794         au_cpup_attr_timesizes(inode);
795         ii_write_unlock(inode);
796         si_read_unlock(sb);
797         return err;
798 }
799
800 /* ---------------------------------------------------------------------- */
801
802 static struct export_operations aufs_export_op = {
803         .fh_to_dentry           = aufs_fh_to_dentry,
804         /* .fh_to_parent        = aufs_fh_to_parent, */
805         .encode_fh              = aufs_encode_fh,
806         .commit_metadata        = aufs_commit_metadata
807 };
808
809 void au_export_init(struct super_block *sb)
810 {
811         struct au_sbinfo *sbinfo;
812         __u32 u;
813
814         sb->s_export_op = &aufs_export_op;
815         sbinfo = au_sbi(sb);
816         sbinfo->si_xigen = NULL;
817         get_random_bytes(&u, sizeof(u));
818         BUILD_BUG_ON(sizeof(u) != sizeof(int));
819         atomic_set(&sbinfo->si_xigen_next, u);
820 }