2 * Copyright (C) 2005-2011 Junjiro R. Okajima
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.
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.
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
20 * mount and super_block operations
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>
35 static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
39 c = au_cache_alloc_icntnr();
42 c->vfs_inode.i_version = 1; /* sigen(sb); */
43 c->iinfo.ii_hinode = NULL;
49 static void aufs_destroy_inode_cb(struct rcu_head *head)
51 struct inode *inode = container_of(head, struct inode, i_rcu);
53 INIT_LIST_HEAD(&inode->i_dentry);
54 au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
57 static void aufs_destroy_inode(struct inode *inode)
60 call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
63 struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
68 inode = iget_locked(sb, ino);
69 if (unlikely(!inode)) {
70 inode = ERR_PTR(-ENOMEM);
73 if (!(inode->i_state & I_NEW))
76 err = au_xigen_new(inode);
78 err = au_iinfo_init(inode);
87 /* never return NULL */
93 /* lock free root dinfo */
94 static int au_show_brs(struct seq_file *seq, struct super_block *sb)
97 aufs_bindex_t bindex, bend;
99 struct au_hdentry *hdp;
100 struct au_branch *br;
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);
111 err = seq_printf(seq, "=%s",
112 au_optstr_br_perm(br->br_perm));
113 if (!err && bindex != bend)
114 err = seq_putc(seq, ':');
120 static void au_show_wbr_create(struct seq_file *m, int v,
121 struct au_sbinfo *sbinfo)
125 AuRwMustAnyLock(&sbinfo->si_rwsem);
127 seq_printf(m, ",create=");
128 pat = au_optstr_wbr_create(v);
130 case AuWbrCreate_TDP:
132 case AuWbrCreate_MFS:
133 case AuWbrCreate_PMFS:
136 case AuWbrCreate_MFSV:
137 seq_printf(m, /*pat*/"mfs:%lu",
138 jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
141 case AuWbrCreate_PMFSV:
142 seq_printf(m, /*pat*/"pmfs:%lu",
143 jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
146 case AuWbrCreate_MFSRR:
147 seq_printf(m, /*pat*/"mfsrr:%llu",
148 sbinfo->si_wbr_mfs.mfsrr_watermark);
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)
159 static int au_show_xino(struct seq_file *seq, struct vfsmount *mnt)
165 const int len = sizeof(AUFS_XINO_FNAME) - 1;
166 aufs_bindex_t bindex, brid;
167 struct super_block *sb;
170 struct dentry *d, *h_root;
171 struct au_hdentry *hdp;
173 AuRwMustAnyLock(&sbinfo->si_rwsem);
177 f = au_sbi(sb)->si_xib;
181 /* stop printing the default xino path on the first writable branch */
183 brid = au_xino_brid(sb);
185 bindex = au_br_index(sb, brid);
186 hdp = au_di(sb->s_root)->di_hdentry;
187 h_root = hdp[0 + bindex].hd_dentry;
191 /* safe ->d_parent because the file is unlinked */
192 if (d->d_parent == h_root
194 && !memcmp(name->name, AUFS_XINO_FNAME, len))
197 seq_puts(seq, ",xino=");
198 err = au_xino_path(seq, f);
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)
209 unsigned int mnt_flags, v;
210 struct super_block *sb;
211 struct au_sbinfo *sbinfo;
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"); \
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)); \
225 #define AuUInt(name, str, val) do { \
226 if (val != AUFS_##name##_DEF) \
227 seq_printf(m, "," #str "=%u", val); \
230 /* lock free root dinfo */
232 si_noflush_read_lock(sb);
234 seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
236 mnt_flags = au_mntflags(sb);
237 if (au_opt_test(mnt_flags, XINO)) {
238 err = au_show_xino(m, mnt);
242 seq_puts(m, ",noxino");
244 AuBool(TRUNC_XINO, trunc_xino);
247 AuBool(PLINK, plink);
249 /* AuBool(DIRPERM1, dirperm1); */
250 /* AuBool(REFROF, refrof); */
252 v = sbinfo->si_wbr_create;
253 if (v != AuWbrCreate_Def)
254 au_show_wbr_create(m, v, sbinfo);
256 v = sbinfo->si_wbr_copyup;
257 if (v != AuWbrCopyup_Def)
258 seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
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');
264 AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
266 v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
267 AuUInt(RDCACHE, rdcache, v);
269 AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
270 AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
273 /* AuBool(SUM_W, wsum); */
274 AuBool(WARN_PERM, warn_perm);
275 AuBool(VERBOSE, verbose);
278 /* be sure to print "br:" last */
291 /* ---------------------------------------------------------------------- */
293 /* sum mode which returns the summation for statfs(2) */
295 static u64 au_add_till_max(u64 a, u64 b)
306 static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
309 u64 blocks, bfree, bavail, files, ffree;
310 aufs_bindex_t bend, bindex, i;
311 unsigned char shared;
313 struct super_block *h_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;
327 for (i = bindex + 1; !shared && i <= bend; i++)
328 shared = (au_sbr_sb(sb, i) == h_sb);
332 /* sb->s_root for NFS is unreliable */
333 h_path.dentry = h_path.mnt->mnt_root;
334 err = vfs_statfs(&h_path, buf);
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);
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;
355 static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
359 struct super_block *sb;
361 /* lock free root dinfo */
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);
370 err = au_statfs_sum(sb, buf);
374 buf->f_type = AUFS_SUPER_MAGIC;
375 buf->f_namelen = AUFS_MAX_NAMELEN;
376 memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
378 /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
383 /* ---------------------------------------------------------------------- */
385 /* final actions when unmounting a file system */
386 static void aufs_put_super(struct super_block *sb)
388 struct au_sbinfo *sbinfo;
394 dbgaufs_si_fin(sbinfo);
395 kobject_put(&sbinfo->si_kobj);
398 /* ---------------------------------------------------------------------- */
400 void au_array_free(void *array)
403 if (!is_vmalloc_addr(array))
410 void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb, void *arg)
413 unsigned long long n;
420 if (*hint > ULLONG_MAX / sizeof(array)) {
421 array = ERR_PTR(-EMFILE);
422 pr_err("hint %llu\n", *hint);
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);
434 n = cb(array, *hint, arg);
435 AuDebugOn(n > *hint);
442 static unsigned long long au_iarray_cb(void *a,
443 unsigned long long max __maybe_unused,
446 unsigned long long n;
447 struct inode **p, *inode;
448 struct list_head *head;
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) {
463 spin_unlock(&inode_lock);
468 struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
470 *max = atomic_long_read(&au_sbi(sb)->si_ninodes);
471 return au_array_alloc(max, au_iarray_cb, &sb->s_inodes);
474 void au_iarray_free(struct inode **a, unsigned long long max)
476 unsigned long long ull;
478 for (ull = 0; ull < max; ull++)
483 /* ---------------------------------------------------------------------- */
486 * refresh dentry and inode at remount time.
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)
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);
505 static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
506 struct au_sbinfo *sbinfo,
507 const unsigned int dir_flags)
510 struct dentry *parent;
514 parent = dget_parent(dentry);
515 if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
516 inode = dentry->d_inode;
518 if (!S_ISDIR(inode->i_mode))
519 err = au_do_refresh(dentry, /*dir_flags*/0,
522 err = au_do_refresh(dentry, dir_flags, parent);
524 au_fset_si(sbinfo, FAILED_REFRESH_DIR);
527 err = au_do_refresh(dentry, /*dir_flags*/0, parent);
536 static int au_refresh_d(struct super_block *sb)
538 int err, i, j, ndentry, e;
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);
547 err = au_dpages_init(&dpages, GFP_NOFS);
550 err = au_dcsub_pages(&dpages, root, NULL, NULL);
554 sigen = au_sigen(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++) {
562 e = au_do_refresh_d(d, sigen, sbinfo, dir_flags);
563 if (unlikely(e && !err))
570 au_dpages_free(&dpages);
575 static int au_refresh_i(struct super_block *sb)
579 unsigned long long max, ull;
580 struct inode *inode, **array;
582 array = au_iarray_alloc(sb, &max);
583 err = PTR_ERR(array);
588 sigen = au_sigen(sb);
589 for (ull = 0; ull < max; 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);
596 pr_err("error %d, i%lu\n", e, inode->i_ino);
599 /* go on even if err */
604 au_iarray_free(array, max);
610 static void au_remount_refresh(struct super_block *sb)
614 aufs_bindex_t bindex, bend;
617 struct au_branch *br;
620 au_fclr_si(au_sbi(sb), FAILED_REFRESH_DIR);
623 DiMustNoWaiters(root);
624 inode = root->d_inode;
625 IiMustNoWaiters(inode);
627 udba = au_opt_udba(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);
633 AuIOErr("hnotify failed on br %d, %d, ignored\n",
635 /* go on even if err */
637 au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
639 di_write_unlock(root);
640 err = au_refresh_d(sb);
641 e = au_refresh_i(sb);
642 if (unlikely(e && !err))
644 /* aufs_write_lock() calls ..._child() */
645 di_write_lock_child(root);
647 au_cpup_attr_all(inode, /*force*/1);
650 AuIOErr("refresh failed, ignored, %d\n", err);
653 /* stop extra interpretation of errno in mount(8), and strange error messages */
654 static int cvt_err(int err)
668 static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
671 unsigned int mntflags;
675 struct au_sbinfo *sbinfo;
679 if (!data || !*data) {
680 err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
682 di_write_lock_child(root);
683 err = au_opts_verify(sb, *flags, /*pending*/0);
684 aufs_write_unlock(root);
690 memset(&opts, 0, sizeof(opts));
691 opts.opt = (void *)__get_free_page(GFP_NOFS);
692 if (unlikely(!opts.opt))
694 opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
695 opts.flags = AuOpts_REMOUNT;
696 opts.sb_flags = *flags;
698 /* parse it before aufs lock */
699 err = au_opts_parse(sb, data, &opts);
704 inode = root->d_inode;
705 mutex_lock(&inode->i_mutex);
706 err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
709 di_write_lock_child(root);
711 /* au_opts_remount() may return an error */
712 err = au_opts_remount(sb, &opts);
715 if (au_ftest_opts(opts.flags, REFRESH))
716 au_remount_refresh(sb);
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);
724 aufs_write_unlock(root);
727 mutex_unlock(&inode->i_mutex);
729 free_page((unsigned long)opts.opt);
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
747 /* ---------------------------------------------------------------------- */
749 static int alloc_root(struct super_block *sb)
756 inode = au_iget_locked(sb, AUFS_ROOT_INO);
757 err = PTR_ERR(inode);
761 inode->i_op = &aufs_dir_iop;
762 inode->i_fop = &aufs_dir_fop;
763 inode->i_mode = S_IFDIR;
765 unlock_new_inode(inode);
767 root = d_alloc_root(inode);
774 err = au_di_init(root);
777 return 0; /* success */
780 goto out; /* do not iput */
789 static int aufs_fill_super(struct super_block *sb, void *raw_data,
790 int silent __maybe_unused)
796 char *arg = raw_data;
798 if (unlikely(!arg || !*arg)) {
805 memset(&opts, 0, sizeof(opts));
806 opts.opt = (void *)__get_free_page(GFP_NOFS);
807 if (unlikely(!opts.opt))
809 opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
810 opts.sb_flags = sb->s_flags;
812 err = au_si_alloc(sb);
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;
824 err = alloc_root(sb);
830 inode = root->d_inode;
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.
837 ii_write_lock_parent(inode);
838 aufs_write_unlock(root);
839 err = au_opts_parse(sb, arg, &opts);
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);
848 aufs_write_unlock(root);
849 mutex_unlock(&inode->i_mutex);
851 goto out_opts; /* success */
857 kobject_put(&au_sbi(sb)->si_kobj);
858 sb->s_fs_info = NULL;
860 free_page((unsigned long)opts.opt);
868 /* ---------------------------------------------------------------------- */
870 static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
871 const char *dev_name __maybe_unused,
875 struct super_block *sb;
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);
884 si_write_lock(sb, !AuLock_FLUSH);
885 sysaufs_brs_add(sb, 0);
893 static void aufs_kill_sb(struct super_block *sb)
895 struct au_sbinfo *sbinfo;
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);
907 if (au_opt_test(sbinfo->si_mntflags, PLINK))
908 au_plink_put(sb, /*verbose*/1);
910 aufs_write_unlock(sb->s_root);
911 au_nwt_flush(&sbinfo->si_nowait);
913 generic_shutdown_super(sb);
916 struct file_system_type aufs_fs_type = {
919 FS_RENAME_DOES_D_MOVE /* a race between rename and others */
920 | FS_REVAL_DOT, /* for NFS branch and udba */
922 .kill_sb = aufs_kill_sb,
923 /* no need to __module_get() and module_put(). */
924 .owner = THIS_MODULE,