2
* Copyright (C) 2005-2008 Junjiro 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
22
* $Id: super.c,v 1.4 2008/05/04 23:53:02 sfjro Exp $
25
#include <linux/module.h>
26
#include <linux/buffer_head.h>
27
#include <linux/seq_file.h>
28
#include <linux/smp_lock.h>
29
#include <linux/statfs.h>
36
static struct inode *aufs_alloc_inode(struct super_block *sb)
38
struct aufs_icntnr *c;
42
c = au_cache_alloc_icntnr();
43
//if (LktrCond) {au_cache_free_icntnr(c); c = NULL;}
45
inode_init_once(&c->vfs_inode);
46
c->vfs_inode.i_version = 1; //sigen(sb);
47
c->iinfo.ii_hinode = NULL;
53
static void aufs_destroy_inode(struct inode *inode)
55
LKTRTrace("i%lu\n", inode->i_ino);
57
au_cache_free_icntnr(container_of(inode, struct aufs_icntnr,
61
struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
66
static struct backing_dev_info bdi = {
67
.ra_pages = 0, /* No readahead */
68
.capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK
72
LKTRTrace("i%lu\n", ino);
74
inode = iget_locked(sb, ino);
75
if (unlikely(!inode)) {
76
inode = ERR_PTR(-ENOMEM);
79
AuDebugOn(IS_ERR(inode));
80
if (unlikely(!(inode->i_state & I_NEW)))
83
err = au_iinfo_init(inode);
84
//if (LktrCond) err = -1;
87
inode->i_op = &aufs_iop;
88
inode->i_fop = &aufs_file_fop;
89
inode->i_mapping->a_ops = &aufs_aop;
90
//inode->i_mapping->backing_dev_info = &bdi;
97
/* never return NULL */
103
static int au_show_brs(struct seq_file *seq, struct super_block *sb)
106
aufs_bindex_t bindex, bend;
115
for (bindex = 0; !err && bindex <= bend; bindex++) {
116
path.mnt = au_sbr_mnt(sb, bindex);
117
path.dentry = au_h_dptr(root, bindex);
118
err = seq_path(seq, &path, au_esc_chars);
122
au_optstr_br_perm(au_sbr_perm(sb, bindex)));
123
if (!err && bindex != bend)
124
err = seq_putc(seq, ':');
131
static void au_show_wbr_create(struct seq_file *m, int v,
132
struct au_sbinfo *sbinfo)
136
AuDebugOn(v == AuWbrCreate_Def);
138
seq_printf(m, ",create=");
139
pat = au_optstr_wbr_create(v);
141
case AuWbrCreate_TDP:
143
case AuWbrCreate_MFS:
144
case AuWbrCreate_PMFS:
147
case AuWbrCreate_MFSV:
148
seq_printf(m, /*pat*/"mfs:%lu",
149
sbinfo->si_wbr_mfs.mfs_expire / HZ);
151
case AuWbrCreate_PMFSV:
152
seq_printf(m, /*pat*/"pmfs:%lu",
153
sbinfo->si_wbr_mfs.mfs_expire / HZ);
155
case AuWbrCreate_MFSRR:
156
seq_printf(m, /*pat*/"mfsrr:%Lu",
157
sbinfo->si_wbr_mfs.mfsrr_watermark);
159
case AuWbrCreate_MFSRRV:
160
seq_printf(m, /*pat*/"mfsrr:%Lu:%lu",
161
sbinfo->si_wbr_mfs.mfsrr_watermark,
162
sbinfo->si_wbr_mfs.mfs_expire / HZ);
167
static int aufs_show_options(struct seq_file *m, struct vfsmount *mnt)
170
struct super_block *sb;
171
struct au_sbinfo *sbinfo;
174
unsigned int mnt_flags, v;
182
aufs_read_lock(root, !AuLock_IR);
184
mnt_flags = au_mntflags(sb);
185
if (au_opt_test(mnt_flags, XINO)) {
186
err = seq_puts(m, ",xino=");
189
xino = sbinfo->si_xib;
190
path.mnt = xino->f_vfsmnt;
191
path.dentry = xino->f_dentry;
192
err = seq_path(m, &path, au_esc_chars);
193
if (unlikely(err <= 0))
197
#define Deleted "\\040(deleted)"
198
m->count -= sizeof(Deleted) - 1;
199
AuDebugOn(memcmp(m->buf + m->count, Deleted,
200
sizeof(Deleted) - 1));
203
err = seq_puts(m, ",noxino");
205
#define AuBool(name, str) do { \
206
v = au_opt_test(mnt_flags, name); \
207
if (!err && v != au_opt_test(AuOpt_Def, name)) \
208
err = seq_printf(m, ",%s" #str, v ? "" : "no"); \
211
#define AuStr(name, str) do { \
212
v = mnt_flags & AuOptMask_##name; \
213
if (!err && v != (AuOpt_Def & AuOptMask_##name)) \
214
err = seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
217
#ifdef CONFIG_AUFS_COMPAT
218
#define AuStr_BrOpt "dirs="
220
#define AuStr_BrOpt "br:"
223
AuBool(TRUNC_XINO, trunc_xino);
224
AuBool(DIRPERM1, dirperm1);
226
AuBool(PLINK, plink);
229
v = sbinfo->si_wbr_create;
230
if (v != AuWbrCreate_Def)
231
au_show_wbr_create(m, v, sbinfo);
233
v = sbinfo->si_wbr_copyup;
234
if (v != AuWbrCopyup_Def)
235
err = seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
237
v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
238
if (!err && v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
239
err = seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
240
AuBool(REFROF, refrof);
242
AuBool(WARN_PERM, warn_perm);
243
AuBool(VERBOSE, verbose);
245
n = sbinfo->si_dirwh;
246
if (!err && n != AUFS_DIRWH_DEF)
247
err = seq_printf(m, ",dirwh=%d", n);
248
n = sbinfo->si_rdcache / HZ;
249
if (!err && n != AUFS_RDCACHE_DEF)
250
err = seq_printf(m, ",rdcache=%d", n);
254
if (!err && !sysaufs_brs) {
255
err = seq_puts(m, "," AuStr_BrOpt);
257
err = au_show_brs(m, sb);
260
/* seq_file will re-call me in case of too long string */
264
aufs_read_unlock(root, !AuLock_IR);
277
static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
283
aufs_read_lock(dentry->d_sb->s_root, 0);
284
err = vfsub_statfs(au_h_dptr(dentry->d_sb->s_root, 0), buf,
285
!!au_opt_test_dlgt(au_mntflags(dentry->d_sb)));
286
//if (LktrCond) err = -1;
287
aufs_read_unlock(dentry->d_sb->s_root, 0);
289
buf->f_type = AUFS_SUPER_MAGIC;
290
buf->f_namelen -= AUFS_WH_PFX_LEN;
291
//todo: support uuid?
292
memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
294
/* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
300
static void au_fsync_br(struct super_block *sb)
302
#ifdef CONFIG_AUFS_FSYNC_SUPER_PATCH
303
aufs_bindex_t bend, bindex;
305
struct super_block *h_sb;
311
for (bindex = 0; bindex < bend; bindex++) {
312
brperm = au_sbr_perm(sb, bindex);
313
if (brperm == AuBr_RR || brperm == AuBr_RRWH)
315
h_sb = au_sbr_sb(sb, bindex);
316
if (bdev_read_only(h_sb->s_bdev))
320
down_write(&h_sb->s_umount);
321
shrink_dcache_sb(h_sb);
323
up_write(&h_sb->s_umount);
330
static void aufs_umount_begin(struct vfsmount *mnt, int flags)
332
struct super_block *sb = mnt->mnt_sb;
333
struct au_sbinfo *sbinfo;
338
if (unlikely(!sbinfo))
344
if (au_opt_test(au_mntflags(sb), PLINK))
347
if (unlikely(au_opt_test(au_mntflags(sb), UDBA_INOTIFY)))
348
shrink_dcache_sb(sb);
350
sbinfo->si_mntcache = NULL;
352
if (sbinfo->si_wbr_create_ops->fin)
353
sbinfo->si_wbr_create_ops->fin(sb);
358
/* final actions when unmounting a file system */
359
static void aufs_put_super(struct super_block *sb)
361
struct au_sbinfo *sbinfo;
366
if (unlikely(!sbinfo))
368
kobject_put(&sbinfo->si_kobj);
371
/* ---------------------------------------------------------------------- */
374
* refresh dentry and inode at remount time.
376
static int do_refresh(struct dentry *dentry, mode_t type,
377
unsigned int dir_flags)
380
struct dentry *parent;
383
LKTRTrace("%.*s, 0%o\n", AuDLNPair(dentry), type);
384
inode = dentry->d_inode;
387
di_write_lock_child(dentry);
388
parent = dget_parent(dentry);
389
di_read_lock_parent(parent, AuLock_IR);
390
/* returns a number of positive dentries */
391
err = au_refresh_hdentry(dentry, type);
394
err = au_refresh_hinode(inode, dentry);
396
if (!err && type == S_IFDIR)
397
au_reset_hinotify(inode, dir_flags);
400
AuErr("unrecoverable error %d, %.*s\n", err, AuDLNPair(dentry));
401
di_read_unlock(parent, AuLock_IR);
403
di_write_unlock(dentry);
409
static int test_dir(struct dentry *dentry, void *arg)
411
return S_ISDIR(dentry->d_inode->i_mode);
414
//todo: merge with refresh_nondir()
415
static int refresh_dir(struct dentry *root, au_gen_t sgen)
417
int err, i, j, ndentry, e;
418
const unsigned int flags = au_hi_flags(root->d_inode, /*isdir*/1);
419
struct au_dcsub_pages dpages;
420
struct au_dpage *dpage;
421
struct dentry **dentries;
424
LKTRTrace("sgen %d\n", sgen);
425
SiMustWriteLock(root->d_sb);
426
AuDebugOn(au_digen(root) != sgen || !kernel_locked());
429
list_for_each_entry(inode, &root->d_sb->s_inodes, i_sb_list)
430
if (S_ISDIR(inode->i_mode) && au_iigen(inode) != sgen) {
431
ii_write_lock_child(inode);
432
e = au_refresh_hinode_self(inode);
434
ii_write_unlock(inode);
436
LKTRTrace("e %d, i%lu\n", e, inode->i_ino);
439
/* go on even if err */
443
e = au_dpages_init(&dpages, GFP_TEMPORARY);
449
e = au_dcsub_pages(&dpages, root, test_dir, NULL);
456
for (i = 0; !e && i < dpages.ndpage; i++) {
457
dpage = dpages.dpages + i;
458
dentries = dpage->dentries;
459
ndentry = dpage->ndentry;
460
for (j = 0; !e && j < ndentry; j++) {
463
#ifdef CONFIG_AUFS_DEBUG
465
struct dentry *parent;
466
parent = dget_parent(d);
467
AuDebugOn(!S_ISDIR(d->d_inode->i_mode)
469
|| au_digen(parent) != sgen);
473
if (au_digen(d) != sgen) {
474
e = do_refresh(d, S_IFDIR, flags);
476
if (unlikely(e && !err))
484
au_dpages_free(&dpages);
490
static int test_nondir(struct dentry *dentry, void *arg)
492
return !S_ISDIR(dentry->d_inode->i_mode);
495
static int refresh_nondir(struct dentry *root, au_gen_t sgen, int do_dentry)
497
int err, i, j, ndentry, e;
498
struct au_dcsub_pages dpages;
499
struct au_dpage *dpage;
500
struct dentry **dentries;
503
LKTRTrace("sgen %d\n", sgen);
504
SiMustWriteLock(root->d_sb);
505
AuDebugOn(au_digen(root) != sgen || !kernel_locked());
508
list_for_each_entry(inode, &root->d_sb->s_inodes, i_sb_list)
509
if (!S_ISDIR(inode->i_mode) && au_iigen(inode) != sgen) {
510
ii_write_lock_child(inode);
511
e = au_refresh_hinode_self(inode);
513
ii_write_unlock(inode);
515
LKTRTrace("e %d, i%lu\n", e, inode->i_ino);
518
/* go on even if err */
525
e = au_dpages_init(&dpages, GFP_TEMPORARY);
531
e = au_dcsub_pages(&dpages, root, test_nondir, NULL);
538
for (i = 0; i < dpages.ndpage; i++) {
539
dpage = dpages.dpages + i;
540
dentries = dpage->dentries;
541
ndentry = dpage->ndentry;
542
for (j = 0; j < ndentry; j++) {
545
#ifdef CONFIG_AUFS_DEBUG
547
struct dentry *parent;
548
parent = dget_parent(d);
549
AuDebugOn(S_ISDIR(d->d_inode->i_mode)
550
|| au_digen(parent) != sgen);
555
if (inode && au_digen(d) != sgen) {
556
e = do_refresh(d, inode->i_mode & S_IFMT, 0);
558
if (unlikely(e && !err))
566
au_dpages_free(&dpages);
572
/* stop extra interpretation of errno in mount(8), and strange error messages */
573
static int cvt_err(int err)
587
/* protected by s_umount */
588
static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
595
struct au_sbinfo *sbinfo;
598
LKTRTrace("flags 0x%x, data %s, len %lu\n",
599
*flags, data ? data : "NULL",
600
(unsigned long)(data ? strlen(data) : 0));
605
goto out; /* success */
608
memset(&opts, 0, sizeof(opts));
609
opts.opt = (void *)__get_free_page(GFP_TEMPORARY);
610
//if (LktrCond) {free_page((unsigned long)opts.opt); opts.opt = NULL;}
611
if (unlikely(!opts.opt))
613
opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
614
opts.flags = AuOpts_REMOUNT;
616
/* parse it before aufs lock */
617
err = au_opts_parse(sb, *flags, data, &opts);
618
//if (LktrCond) {au_free_opts(&opts); err = -1;}
624
inode = root->d_inode;
625
mutex_lock(&inode->i_mutex);
626
aufs_write_lock(root);
630
/* au_do_opts() may return an error */
631
err = au_opts_remount(sb, &opts);
632
//if (LktrCond) err = -1;
635
if (au_ftest_opts(opts.flags, REFRESH_DIR)
636
|| au_ftest_opts(opts.flags, REFRESH_NONDIR)) {
640
dlgt = !!au_opt_test_dlgt(sbinfo->si_mntflags);
641
au_opt_clr(sbinfo->si_mntflags, DLGT);
643
au_reset_hinotify(inode, au_hi_flags(inode, /*isdir*/1));
644
sigen = au_sigen(sb);
645
au_fclr_si(sbinfo, FAILED_REFRESH_DIRS);
647
DiMustNoWaiters(root);
648
IiMustNoWaiters(root->d_inode);
649
di_write_unlock(root);
651
rerr = refresh_dir(root, sigen);
652
if (unlikely(rerr)) {
653
au_fset_si(sbinfo, FAILED_REFRESH_DIRS);
654
AuWarn("Refreshing directories failed, ignores (%d)\n",
658
if (unlikely(au_ftest_opts(opts.flags, REFRESH_NONDIR))) {
660
rerr = refresh_nondir(root, sigen, !rerr);
662
AuWarn("Refreshing non-directories failed,"
663
" ignores (%d)\n", rerr);
667
/* aufs_write_lock() calls ..._child() */
668
di_write_lock_child(root);
670
au_cpup_attr_all(inode);
672
au_opt_set(sbinfo->si_mntflags, DLGT);
675
aufs_write_unlock(root);
676
mutex_unlock(&inode->i_mutex);
679
free_page((unsigned long)opts.opt);
687
static struct super_operations aufs_sop = {
688
.alloc_inode = aufs_alloc_inode,
689
.destroy_inode = aufs_destroy_inode,
690
//.dirty_inode = aufs_dirty_inode,
691
//.write_inode = aufs_write_inode,
692
//void (*put_inode) (struct inode *);
693
.drop_inode = generic_delete_inode,
694
//.delete_inode = aufs_delete_inode,
695
//.clear_inode = aufs_clear_inode,
697
.show_options = aufs_show_options,
698
.statfs = aufs_statfs,
700
.put_super = aufs_put_super,
701
//void (*write_super) (struct super_block *);
702
//int (*sync_fs)(struct super_block *sb, int wait);
703
//void (*write_super_lockfs) (struct super_block *);
704
//void (*unlockfs) (struct super_block *);
705
.remount_fs = aufs_remount_fs,
706
/* depends upon umount flags. also use put_super() (< 2.6.18) */
707
.umount_begin = aufs_umount_begin
710
/* ---------------------------------------------------------------------- */
712
static int alloc_root(struct super_block *sb)
721
inode = au_iget_locked(sb, AUFS_ROOT_INO);
722
//if (LktrCond) {iput(inode); inode = NULL;}
723
err = PTR_ERR(inode);
726
unlock_new_inode(inode);
727
inode->i_mode = S_IFDIR;
728
root = d_alloc_root(inode);
729
//if (LktrCond) {igrab(inode); dput(root); root = NULL;}
736
err = au_alloc_dinfo(root);
737
//if (LktrCond){au_rw_write_unlock(&au_di(root)->di_rwsem);err=-1;}
740
return 0; /* success */
743
goto out; /* do not iput */
754
static int aufs_fill_super(struct super_block *sb, void *raw_data, int silent)
760
char *arg = raw_data;
763
if (unlikely(!arg || !*arg)) {
768
LKTRTrace("%s, silent %d\n", arg, silent);
771
memset(&opts, 0, sizeof(opts));
772
opts.opt = (void *)__get_free_page(GFP_TEMPORARY);
773
//if (LktrCond) {free_page((unsigned long)opts.opt); opts.opt = NULL;}
774
if (unlikely(!opts.opt))
776
opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
778
err = au_si_alloc(sb);
779
//if (LktrCond) {si_write_unlock(sb);free_sbinfo(sb);err=-1;}
783
/* all timestamps always follow the ones on the branch */
784
sb->s_flags |= MS_NOATIME | MS_NODIRATIME;
785
sb->s_op = &aufs_sop;
786
sb->s_magic = AUFS_SUPER_MAGIC;
787
au_init_export_op(sb);
789
err = alloc_root(sb);
790
//if (LktrCond) {au_rw_write_unlock(&au_di(sb->s_root)->di_rwsem);
791
//dput(sb->s_root);sb->s_root=NULL;err=-1;}
793
AuDebugOn(sb->s_root);
798
DiMustWriteLock(root);
799
inode = root->d_inode;
803
* actually we can parse options regardless aufs lock here.
804
* but at remount time, parsing must be done before aufs lock.
805
* so we follow the same rule.
807
ii_write_lock_parent(inode);
808
aufs_write_unlock(root);
809
err = au_opts_parse(sb, sb->s_flags, arg, &opts);
810
//if (LktrCond) {au_opts_free(&opts); err = -1;}
814
/* lock vfs_inode first, then aufs. */
815
mutex_lock(&inode->i_mutex);
816
inode->i_op = &aufs_dir_iop;
817
inode->i_fop = &aufs_dir_fop;
818
aufs_write_lock(root);
821
err = au_opts_mount(sb, &opts);
822
//if (LktrCond) err = -1;
826
AuDebugOn(!sb->s_maxbytes);
829
aufs_write_unlock(root);
830
mutex_unlock(&inode->i_mutex);
832
goto out_opts; /* success */
835
aufs_write_unlock(root);
836
mutex_unlock(&inode->i_mutex);
841
au_fset_si(au_sbi(sb), FAILED_INIT);
842
kobject_put(&au_sbi(sb)->si_kobj);
843
sb->s_fs_info = NULL;
845
free_page((unsigned long)opts.opt);
854
/* ---------------------------------------------------------------------- */
856
static int aufs_get_sb(struct file_system_type *fs_type, int flags,
857
const char *dev_name, void *raw_data,
858
struct vfsmount *mnt)
862
/* all timestamps always follow the ones on the branch */
863
/* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
864
err = get_sb_nodev(fs_type, flags, raw_data, aufs_fill_super, mnt);
866
struct super_block *sb = mnt->mnt_sb;
867
struct au_sbinfo *sbinfo = au_sbi(sb);
870
sbinfo->si_mntcache = mnt;
874
au_sbilist_add(sbinfo);
876
sysaufs_brs_add(sb, 0);
883
struct file_system_type aufs_fs_type = {
885
.fs_flags = FS_REVAL_DOT, /* for UDBA and NFS branch */
886
.get_sb = aufs_get_sb,
887
.kill_sb = generic_shutdown_super,
888
//.kill_sb = kill_anon_super,
889
/* no need to __module_get() and module_put(). */
890
.owner = THIS_MODULE,