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