UBUNTU: Ubuntu-2.6.38-12.51
[linux-flexiantxendom0-natty.git] / ubuntu / aufs / super.c
1 /*
2  * Copyright (C) 2005-2011 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  * mount and super_block operations
21  */
22
23 #include <linux/buffer_head.h>
24 #include <linux/jiffies.h>
25 #include <linux/module.h>
26 #include <linux/seq_file.h>
27 #include <linux/statfs.h>
28 #include <linux/vmalloc.h>
29 #include <linux/writeback.h>
30 #include "aufs.h"
31
32 /*
33  * super_operations
34  */
35 static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
36 {
37         struct au_icntnr *c;
38
39         c = au_cache_alloc_icntnr();
40         if (c) {
41                 au_icntnr_init(c);
42                 c->vfs_inode.i_version = 1; /* sigen(sb); */
43                 c->iinfo.ii_hinode = NULL;
44                 return &c->vfs_inode;
45         }
46         return NULL;
47 }
48
49 static void aufs_destroy_inode_cb(struct rcu_head *head)
50 {
51         struct inode *inode = container_of(head, struct inode, i_rcu);
52
53         INIT_LIST_HEAD(&inode->i_dentry);
54         au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
55 }
56
57 static void aufs_destroy_inode(struct inode *inode)
58 {
59         au_iinfo_fin(inode);
60         call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
61 }
62
63 struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
64 {
65         struct inode *inode;
66         int err;
67
68         inode = iget_locked(sb, ino);
69         if (unlikely(!inode)) {
70                 inode = ERR_PTR(-ENOMEM);
71                 goto out;
72         }
73         if (!(inode->i_state & I_NEW))
74                 goto out;
75
76         err = au_xigen_new(inode);
77         if (!err)
78                 err = au_iinfo_init(inode);
79         if (!err)
80                 inode->i_version++;
81         else {
82                 iget_failed(inode);
83                 inode = ERR_PTR(err);
84         }
85
86 out:
87         /* never return NULL */
88         AuDebugOn(!inode);
89         AuTraceErrPtr(inode);
90         return inode;
91 }
92
93 /* lock free root dinfo */
94 static int au_show_brs(struct seq_file *seq, struct super_block *sb)
95 {
96         int err;
97         aufs_bindex_t bindex, bend;
98         struct path path;
99         struct au_hdentry *hdp;
100         struct au_branch *br;
101
102         err = 0;
103         bend = au_sbend(sb);
104         hdp = au_di(sb->s_root)->di_hdentry;
105         for (bindex = 0; !err && bindex <= bend; bindex++) {
106                 br = au_sbr(sb, bindex);
107                 path.mnt = br->br_mnt;
108                 path.dentry = hdp[bindex].hd_dentry;
109                 err = au_seq_path(seq, &path);
110                 if (err > 0)
111                         err = seq_printf(seq, "=%s",
112                                          au_optstr_br_perm(br->br_perm));
113                 if (!err && bindex != bend)
114                         err = seq_putc(seq, ':');
115         }
116
117         return err;
118 }
119
120 static void au_show_wbr_create(struct seq_file *m, int v,
121                                struct au_sbinfo *sbinfo)
122 {
123         const char *pat;
124
125         AuRwMustAnyLock(&sbinfo->si_rwsem);
126
127         seq_printf(m, ",create=");
128         pat = au_optstr_wbr_create(v);
129         switch (v) {
130         case AuWbrCreate_TDP:
131         case AuWbrCreate_RR:
132         case AuWbrCreate_MFS:
133         case AuWbrCreate_PMFS:
134                 seq_printf(m, pat);
135                 break;
136         case AuWbrCreate_MFSV:
137                 seq_printf(m, /*pat*/"mfs:%lu",
138                            jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
139                            / MSEC_PER_SEC);
140                 break;
141         case AuWbrCreate_PMFSV:
142                 seq_printf(m, /*pat*/"pmfs:%lu",
143                            jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
144                            / MSEC_PER_SEC);
145                 break;
146         case AuWbrCreate_MFSRR:
147                 seq_printf(m, /*pat*/"mfsrr:%llu",
148                            sbinfo->si_wbr_mfs.mfsrr_watermark);
149                 break;
150         case AuWbrCreate_MFSRRV:
151                 seq_printf(m, /*pat*/"mfsrr:%llu:%lu",
152                            sbinfo->si_wbr_mfs.mfsrr_watermark,
153                            jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
154                            / MSEC_PER_SEC);
155                 break;
156         }
157 }
158
159 static int au_show_xino(struct seq_file *seq, struct vfsmount *mnt)
160 {
161 #ifdef CONFIG_SYSFS
162         return 0;
163 #else
164         int err;
165         const int len = sizeof(AUFS_XINO_FNAME) - 1;
166         aufs_bindex_t bindex, brid;
167         struct super_block *sb;
168         struct qstr *name;
169         struct file *f;
170         struct dentry *d, *h_root;
171         struct au_hdentry *hdp;
172
173         AuRwMustAnyLock(&sbinfo->si_rwsem);
174
175         err = 0;
176         sb = mnt->mnt_sb;
177         f = au_sbi(sb)->si_xib;
178         if (!f)
179                 goto out;
180
181         /* stop printing the default xino path on the first writable branch */
182         h_root = NULL;
183         brid = au_xino_brid(sb);
184         if (brid >= 0) {
185                 bindex = au_br_index(sb, brid);
186                 hdp = au_di(sb->s_root)->di_hdentry;
187                 h_root = hdp[0 + bindex].hd_dentry;
188         }
189         d = f->f_dentry;
190         name = &d->d_name;
191         /* safe ->d_parent because the file is unlinked */
192         if (d->d_parent == h_root
193             && name->len == len
194             && !memcmp(name->name, AUFS_XINO_FNAME, len))
195                 goto out;
196
197         seq_puts(seq, ",xino=");
198         err = au_xino_path(seq, f);
199
200 out:
201         return err;
202 #endif
203 }
204
205 /* seq_file will re-call me in case of too long string */
206 static int aufs_show_options(struct seq_file *m, struct vfsmount *mnt)
207 {
208         int err;
209         unsigned int mnt_flags, v;
210         struct super_block *sb;
211         struct au_sbinfo *sbinfo;
212
213 #define AuBool(name, str) do { \
214         v = au_opt_test(mnt_flags, name); \
215         if (v != au_opt_test(AuOpt_Def, name)) \
216                 seq_printf(m, ",%s" #str, v ? "" : "no"); \
217 } while (0)
218
219 #define AuStr(name, str) do { \
220         v = mnt_flags & AuOptMask_##name; \
221         if (v != (AuOpt_Def & AuOptMask_##name)) \
222                 seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
223 } while (0)
224
225 #define AuUInt(name, str, val) do { \
226         if (val != AUFS_##name##_DEF) \
227                 seq_printf(m, "," #str "=%u", val); \
228 } while (0)
229
230         /* lock free root dinfo */
231         sb = mnt->mnt_sb;
232         si_noflush_read_lock(sb);
233         sbinfo = au_sbi(sb);
234         seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
235
236         mnt_flags = au_mntflags(sb);
237         if (au_opt_test(mnt_flags, XINO)) {
238                 err = au_show_xino(m, mnt);
239                 if (unlikely(err))
240                         goto out;
241         } else
242                 seq_puts(m, ",noxino");
243
244         AuBool(TRUNC_XINO, trunc_xino);
245         AuStr(UDBA, udba);
246         AuBool(SHWH, shwh);
247         AuBool(PLINK, plink);
248         AuBool(DIO, dio);
249         /* AuBool(DIRPERM1, dirperm1); */
250         /* AuBool(REFROF, refrof); */
251
252         v = sbinfo->si_wbr_create;
253         if (v != AuWbrCreate_Def)
254                 au_show_wbr_create(m, v, sbinfo);
255
256         v = sbinfo->si_wbr_copyup;
257         if (v != AuWbrCopyup_Def)
258                 seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
259
260         v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
261         if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
262                 seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
263
264         AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
265
266         v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
267         AuUInt(RDCACHE, rdcache, v);
268
269         AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
270         AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
271
272         AuBool(SUM, sum);
273         /* AuBool(SUM_W, wsum); */
274         AuBool(WARN_PERM, warn_perm);
275         AuBool(VERBOSE, verbose);
276
277 out:
278         /* be sure to print "br:" last */
279         if (!sysaufs_brs) {
280                 seq_puts(m, ",br:");
281                 au_show_brs(m, sb);
282         }
283         si_read_unlock(sb);
284         return 0;
285
286 #undef AuBool
287 #undef AuStr
288 #undef AuUInt
289 }
290
291 /* ---------------------------------------------------------------------- */
292
293 /* sum mode which returns the summation for statfs(2) */
294
295 static u64 au_add_till_max(u64 a, u64 b)
296 {
297         u64 old;
298
299         old = a;
300         a += b;
301         if (old < a)
302                 return a;
303         return ULLONG_MAX;
304 }
305
306 static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
307 {
308         int err;
309         u64 blocks, bfree, bavail, files, ffree;
310         aufs_bindex_t bend, bindex, i;
311         unsigned char shared;
312         struct path h_path;
313         struct super_block *h_sb;
314
315         blocks = 0;
316         bfree = 0;
317         bavail = 0;
318         files = 0;
319         ffree = 0;
320
321         err = 0;
322         bend = au_sbend(sb);
323         for (bindex = bend; bindex >= 0; bindex--) {
324                 h_path.mnt = au_sbr_mnt(sb, bindex);
325                 h_sb = h_path.mnt->mnt_sb;
326                 shared = 0;
327                 for (i = bindex + 1; !shared && i <= bend; i++)
328                         shared = (au_sbr_sb(sb, i) == h_sb);
329                 if (shared)
330                         continue;
331
332                 /* sb->s_root for NFS is unreliable */
333                 h_path.dentry = h_path.mnt->mnt_root;
334                 err = vfs_statfs(&h_path, buf);
335                 if (unlikely(err))
336                         goto out;
337
338                 blocks = au_add_till_max(blocks, buf->f_blocks);
339                 bfree = au_add_till_max(bfree, buf->f_bfree);
340                 bavail = au_add_till_max(bavail, buf->f_bavail);
341                 files = au_add_till_max(files, buf->f_files);
342                 ffree = au_add_till_max(ffree, buf->f_ffree);
343         }
344
345         buf->f_blocks = blocks;
346         buf->f_bfree = bfree;
347         buf->f_bavail = bavail;
348         buf->f_files = files;
349         buf->f_ffree = ffree;
350
351 out:
352         return err;
353 }
354
355 static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
356 {
357         int err;
358         struct path h_path;
359         struct super_block *sb;
360
361         /* lock free root dinfo */
362         sb = dentry->d_sb;
363         si_noflush_read_lock(sb);
364         if (!au_opt_test(au_mntflags(sb), SUM)) {
365                 /* sb->s_root for NFS is unreliable */
366                 h_path.mnt = au_sbr_mnt(sb, 0);
367                 h_path.dentry = h_path.mnt->mnt_root;
368                 err = vfs_statfs(&h_path, buf);
369         } else
370                 err = au_statfs_sum(sb, buf);
371         si_read_unlock(sb);
372
373         if (!err) {
374                 buf->f_type = AUFS_SUPER_MAGIC;
375                 buf->f_namelen = AUFS_MAX_NAMELEN;
376                 memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
377         }
378         /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
379
380         return err;
381 }
382
383 /* ---------------------------------------------------------------------- */
384
385 /* final actions when unmounting a file system */
386 static void aufs_put_super(struct super_block *sb)
387 {
388         struct au_sbinfo *sbinfo;
389
390         sbinfo = au_sbi(sb);
391         if (!sbinfo)
392                 return;
393
394         dbgaufs_si_fin(sbinfo);
395         kobject_put(&sbinfo->si_kobj);
396 }
397
398 /* ---------------------------------------------------------------------- */
399
400 void au_array_free(void *array)
401 {
402         if (array) {
403                 if (!is_vmalloc_addr(array))
404                         kfree(array);
405                 else
406                         vfree(array);
407         }
408 }
409
410 void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb, void *arg)
411 {
412         void *array;
413         unsigned long long n;
414
415         array = NULL;
416         n = 0;
417         if (!*hint)
418                 goto out;
419
420         if (*hint > ULLONG_MAX / sizeof(array)) {
421                 array = ERR_PTR(-EMFILE);
422                 pr_err("hint %llu\n", *hint);
423                 goto out;
424         }
425
426         array = kmalloc(sizeof(array) * *hint, GFP_NOFS);
427         if (unlikely(!array))
428                 array = vmalloc(sizeof(array) * *hint);
429         if (unlikely(!array)) {
430                 array = ERR_PTR(-ENOMEM);
431                 goto out;
432         }
433
434         n = cb(array, *hint, arg);
435         AuDebugOn(n > *hint);
436
437 out:
438         *hint = n;
439         return array;
440 }
441
442 static unsigned long long au_iarray_cb(void *a,
443                                        unsigned long long max __maybe_unused,
444                                        void *arg)
445 {
446         unsigned long long n;
447         struct inode **p, *inode;
448         struct list_head *head;
449
450         n = 0;
451         p = a;
452         head = arg;
453         spin_lock(&inode_lock);
454         list_for_each_entry(inode, head, i_sb_list) {
455                 if (!is_bad_inode(inode)
456                     && au_ii(inode)->ii_bstart >= 0) {
457                         au_igrab(inode);
458                         *p++ = inode;
459                         n++;
460                         AuDebugOn(n > max);
461                 }
462         }
463         spin_unlock(&inode_lock);
464
465         return n;
466 }
467
468 struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
469 {
470         *max = atomic_long_read(&au_sbi(sb)->si_ninodes);
471         return au_array_alloc(max, au_iarray_cb, &sb->s_inodes);
472 }
473
474 void au_iarray_free(struct inode **a, unsigned long long max)
475 {
476         unsigned long long ull;
477
478         for (ull = 0; ull < max; ull++)
479                 iput(a[ull]);
480         au_array_free(a);
481 }
482
483 /* ---------------------------------------------------------------------- */
484
485 /*
486  * refresh dentry and inode at remount time.
487  */
488 /* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
489 static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
490                       struct dentry *parent)
491 {
492         int err;
493
494         di_write_lock_child(dentry);
495         di_read_lock_parent(parent, AuLock_IR);
496         err = au_refresh_dentry(dentry, parent);
497         if (!err && dir_flags)
498                 au_hn_reset(dentry->d_inode, dir_flags);
499         di_read_unlock(parent, AuLock_IR);
500         di_write_unlock(dentry);
501
502         return err;
503 }
504
505 static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
506                            struct au_sbinfo *sbinfo,
507                            const unsigned int dir_flags)
508 {
509         int err;
510         struct dentry *parent;
511         struct inode *inode;
512
513         err = 0;
514         parent = dget_parent(dentry);
515         if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
516                 inode = dentry->d_inode;
517                 if (inode) {
518                         if (!S_ISDIR(inode->i_mode))
519                                 err = au_do_refresh(dentry, /*dir_flags*/0,
520                                                  parent);
521                         else {
522                                 err = au_do_refresh(dentry, dir_flags, parent);
523                                 if (unlikely(err))
524                                         au_fset_si(sbinfo, FAILED_REFRESH_DIR);
525                         }
526                 } else
527                         err = au_do_refresh(dentry, /*dir_flags*/0, parent);
528                 AuDbgDentry(dentry);
529         }
530         dput(parent);
531
532         AuTraceErr(err);
533         return err;
534 }
535
536 static int au_refresh_d(struct super_block *sb)
537 {
538         int err, i, j, ndentry, e;
539         unsigned int sigen;
540         struct au_dcsub_pages dpages;
541         struct au_dpage *dpage;
542         struct dentry **dentries, *d;
543         struct au_sbinfo *sbinfo;
544         struct dentry *root = sb->s_root;
545         const unsigned int dir_flags = au_hi_flags(root->d_inode, /*isdir*/1);
546
547         err = au_dpages_init(&dpages, GFP_NOFS);
548         if (unlikely(err))
549                 goto out;
550         err = au_dcsub_pages(&dpages, root, NULL, NULL);
551         if (unlikely(err))
552                 goto out_dpages;
553
554         sigen = au_sigen(sb);
555         sbinfo = au_sbi(sb);
556         for (i = 0; i < dpages.ndpage; i++) {
557                 dpage = dpages.dpages + i;
558                 dentries = dpage->dentries;
559                 ndentry = dpage->ndentry;
560                 for (j = 0; j < ndentry; j++) {
561                         d = dentries[j];
562                         e = au_do_refresh_d(d, sigen, sbinfo, dir_flags);
563                         if (unlikely(e && !err))
564                                 err = e;
565                         /* go on even err */
566                 }
567         }
568
569 out_dpages:
570         au_dpages_free(&dpages);
571 out:
572         return err;
573 }
574
575 static int au_refresh_i(struct super_block *sb)
576 {
577         int err, e;
578         unsigned int sigen;
579         unsigned long long max, ull;
580         struct inode *inode, **array;
581
582         array = au_iarray_alloc(sb, &max);
583         err = PTR_ERR(array);
584         if (IS_ERR(array))
585                 goto out;
586
587         err = 0;
588         sigen = au_sigen(sb);
589         for (ull = 0; ull < max; ull++) {
590                 inode = array[ull];
591                 if (au_iigen(inode) != sigen) {
592                         ii_write_lock_child(inode);
593                         e = au_refresh_hinode_self(inode);
594                         ii_write_unlock(inode);
595                         if (unlikely(e)) {
596                                 pr_err("error %d, i%lu\n", e, inode->i_ino);
597                                 if (!err)
598                                         err = e;
599                                 /* go on even if err */
600                         }
601                 }
602         }
603
604         au_iarray_free(array, max);
605
606 out:
607         return err;
608 }
609
610 static void au_remount_refresh(struct super_block *sb)
611 {
612         int err, e;
613         unsigned int udba;
614         aufs_bindex_t bindex, bend;
615         struct dentry *root;
616         struct inode *inode;
617         struct au_branch *br;
618
619         au_sigen_inc(sb);
620         au_fclr_si(au_sbi(sb), FAILED_REFRESH_DIR);
621
622         root = sb->s_root;
623         DiMustNoWaiters(root);
624         inode = root->d_inode;
625         IiMustNoWaiters(inode);
626
627         udba = au_opt_udba(sb);
628         bend = au_sbend(sb);
629         for (bindex = 0; bindex <= bend; bindex++) {
630                 br = au_sbr(sb, bindex);
631                 err = au_hnotify_reset_br(udba, br, br->br_perm);
632                 if (unlikely(err))
633                         AuIOErr("hnotify failed on br %d, %d, ignored\n",
634                                 bindex, err);
635                 /* go on even if err */
636         }
637         au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
638
639         di_write_unlock(root);
640         err = au_refresh_d(sb);
641         e = au_refresh_i(sb);
642         if (unlikely(e && !err))
643                 err = e;
644         /* aufs_write_lock() calls ..._child() */
645         di_write_lock_child(root);
646
647         au_cpup_attr_all(inode, /*force*/1);
648
649         if (unlikely(err))
650                 AuIOErr("refresh failed, ignored, %d\n", err);
651 }
652
653 /* stop extra interpretation of errno in mount(8), and strange error messages */
654 static int cvt_err(int err)
655 {
656         AuTraceErr(err);
657
658         switch (err) {
659         case -ENOENT:
660         case -ENOTDIR:
661         case -EEXIST:
662         case -EIO:
663                 err = -EINVAL;
664         }
665         return err;
666 }
667
668 static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
669 {
670         int err, do_dx;
671         unsigned int mntflags;
672         struct au_opts opts;
673         struct dentry *root;
674         struct inode *inode;
675         struct au_sbinfo *sbinfo;
676
677         err = 0;
678         root = sb->s_root;
679         if (!data || !*data) {
680                 err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
681                 if (!err) {
682                         di_write_lock_child(root);
683                         err = au_opts_verify(sb, *flags, /*pending*/0);
684                         aufs_write_unlock(root);
685                 }
686                 goto out;
687         }
688
689         err = -ENOMEM;
690         memset(&opts, 0, sizeof(opts));
691         opts.opt = (void *)__get_free_page(GFP_NOFS);
692         if (unlikely(!opts.opt))
693                 goto out;
694         opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
695         opts.flags = AuOpts_REMOUNT;
696         opts.sb_flags = *flags;
697
698         /* parse it before aufs lock */
699         err = au_opts_parse(sb, data, &opts);
700         if (unlikely(err))
701                 goto out_opts;
702
703         sbinfo = au_sbi(sb);
704         inode = root->d_inode;
705         mutex_lock(&inode->i_mutex);
706         err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
707         if (unlikely(err))
708                 goto out_mtx;
709         di_write_lock_child(root);
710
711         /* au_opts_remount() may return an error */
712         err = au_opts_remount(sb, &opts);
713         au_opts_free(&opts);
714
715         if (au_ftest_opts(opts.flags, REFRESH))
716                 au_remount_refresh(sb);
717
718         if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
719                 mntflags = au_mntflags(sb);
720                 do_dx = !!au_opt_test(mntflags, DIO);
721                 au_dy_arefresh(do_dx);
722         }
723
724         aufs_write_unlock(root);
725
726 out_mtx:
727         mutex_unlock(&inode->i_mutex);
728 out_opts:
729         free_page((unsigned long)opts.opt);
730 out:
731         err = cvt_err(err);
732         AuTraceErr(err);
733         return err;
734 }
735
736 static const struct super_operations aufs_sop = {
737         .alloc_inode    = aufs_alloc_inode,
738         .destroy_inode  = aufs_destroy_inode,
739         /* always deleting, no clearing */
740         .drop_inode     = generic_delete_inode,
741         .show_options   = aufs_show_options,
742         .statfs         = aufs_statfs,
743         .put_super      = aufs_put_super,
744         .remount_fs     = aufs_remount_fs
745 };
746
747 /* ---------------------------------------------------------------------- */
748
749 static int alloc_root(struct super_block *sb)
750 {
751         int err;
752         struct inode *inode;
753         struct dentry *root;
754
755         err = -ENOMEM;
756         inode = au_iget_locked(sb, AUFS_ROOT_INO);
757         err = PTR_ERR(inode);
758         if (IS_ERR(inode))
759                 goto out;
760
761         inode->i_op = &aufs_dir_iop;
762         inode->i_fop = &aufs_dir_fop;
763         inode->i_mode = S_IFDIR;
764         inode->i_nlink = 2;
765         unlock_new_inode(inode);
766
767         root = d_alloc_root(inode);
768         if (unlikely(!root))
769                 goto out_iput;
770         err = PTR_ERR(root);
771         if (IS_ERR(root))
772                 goto out_iput;
773
774         err = au_di_init(root);
775         if (!err) {
776                 sb->s_root = root;
777                 return 0; /* success */
778         }
779         dput(root);
780         goto out; /* do not iput */
781
782 out_iput:
783         iget_failed(inode);
784 out:
785         return err;
786
787 }
788
789 static int aufs_fill_super(struct super_block *sb, void *raw_data,
790                            int silent __maybe_unused)
791 {
792         int err;
793         struct au_opts opts;
794         struct dentry *root;
795         struct inode *inode;
796         char *arg = raw_data;
797
798         if (unlikely(!arg || !*arg)) {
799                 err = -EINVAL;
800                 pr_err("no arg\n");
801                 goto out;
802         }
803
804         err = -ENOMEM;
805         memset(&opts, 0, sizeof(opts));
806         opts.opt = (void *)__get_free_page(GFP_NOFS);
807         if (unlikely(!opts.opt))
808                 goto out;
809         opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
810         opts.sb_flags = sb->s_flags;
811
812         err = au_si_alloc(sb);
813         if (unlikely(err))
814                 goto out_opts;
815
816         /* all timestamps always follow the ones on the branch */
817         sb->s_flags |= MS_NOATIME | MS_NODIRATIME;
818         sb->s_op = &aufs_sop;
819         sb->s_d_op = &aufs_dop;
820         sb->s_magic = AUFS_SUPER_MAGIC;
821         sb->s_maxbytes = 0;
822         au_export_init(sb);
823
824         err = alloc_root(sb);
825         if (unlikely(err)) {
826                 si_write_unlock(sb);
827                 goto out_info;
828         }
829         root = sb->s_root;
830         inode = root->d_inode;
831
832         /*
833          * actually we can parse options regardless aufs lock here.
834          * but at remount time, parsing must be done before aufs lock.
835          * so we follow the same rule.
836          */
837         ii_write_lock_parent(inode);
838         aufs_write_unlock(root);
839         err = au_opts_parse(sb, arg, &opts);
840         if (unlikely(err))
841                 goto out_root;
842
843         /* lock vfs_inode first, then aufs. */
844         mutex_lock(&inode->i_mutex);
845         aufs_write_lock(root);
846         err = au_opts_mount(sb, &opts);
847         au_opts_free(&opts);
848         aufs_write_unlock(root);
849         mutex_unlock(&inode->i_mutex);
850         if (!err)
851                 goto out_opts; /* success */
852
853 out_root:
854         dput(root);
855         sb->s_root = NULL;
856 out_info:
857         kobject_put(&au_sbi(sb)->si_kobj);
858         sb->s_fs_info = NULL;
859 out_opts:
860         free_page((unsigned long)opts.opt);
861 out:
862         AuTraceErr(err);
863         err = cvt_err(err);
864         AuTraceErr(err);
865         return err;
866 }
867
868 /* ---------------------------------------------------------------------- */
869
870 static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
871                                  const char *dev_name __maybe_unused,
872                                  void *raw_data)
873 {
874         struct dentry *root;
875         struct super_block *sb;
876
877         /* all timestamps always follow the ones on the branch */
878         /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
879         root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
880         if (IS_ERR(root))
881                 goto out;
882
883         sb = root->d_sb;
884         si_write_lock(sb, !AuLock_FLUSH);
885         sysaufs_brs_add(sb, 0);
886         si_write_unlock(sb);
887         au_sbilist_add(sb);
888
889 out:
890         return root;
891 }
892
893 static void aufs_kill_sb(struct super_block *sb)
894 {
895         struct au_sbinfo *sbinfo;
896
897         sbinfo = au_sbi(sb);
898         if (sbinfo) {
899                 au_sbilist_del(sb);
900                 aufs_write_lock(sb->s_root);
901                 if (sbinfo->si_wbr_create_ops->fin)
902                         sbinfo->si_wbr_create_ops->fin(sb);
903                 if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
904                         au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
905                         au_remount_refresh(sb);
906                 }
907                 if (au_opt_test(sbinfo->si_mntflags, PLINK))
908                         au_plink_put(sb, /*verbose*/1);
909                 au_xino_clr(sb);
910                 aufs_write_unlock(sb->s_root);
911                 au_nwt_flush(&sbinfo->si_nowait);
912         }
913         generic_shutdown_super(sb);
914 }
915
916 struct file_system_type aufs_fs_type = {
917         .name           = AUFS_FSTYPE,
918         .fs_flags       =
919                 FS_RENAME_DOES_D_MOVE   /* a race between rename and others */
920                 | FS_REVAL_DOT,         /* for NFS branch and udba */
921         .mount          = aufs_mount,
922         .kill_sb        = aufs_kill_sb,
923         /* no need to __module_get() and module_put(). */
924         .owner          = THIS_MODULE,
925 };