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