xref: /OK3568_Linux_fs/kernel/fs/ext2/super.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *  linux/fs/ext2/super.c
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 1992, 1993, 1994, 1995
6*4882a593Smuzhiyun  * Remy Card (card@masi.ibp.fr)
7*4882a593Smuzhiyun  * Laboratoire MASI - Institut Blaise Pascal
8*4882a593Smuzhiyun  * Universite Pierre et Marie Curie (Paris VI)
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  *  from
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  *  linux/fs/minix/inode.c
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  *  Copyright (C) 1991, 1992  Linus Torvalds
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  *  Big-endian to little-endian byte-swapping/bitmaps by
17*4882a593Smuzhiyun  *        David S. Miller (davem@caip.rutgers.edu), 1995
18*4882a593Smuzhiyun  */
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #include <linux/module.h>
21*4882a593Smuzhiyun #include <linux/string.h>
22*4882a593Smuzhiyun #include <linux/fs.h>
23*4882a593Smuzhiyun #include <linux/slab.h>
24*4882a593Smuzhiyun #include <linux/init.h>
25*4882a593Smuzhiyun #include <linux/blkdev.h>
26*4882a593Smuzhiyun #include <linux/parser.h>
27*4882a593Smuzhiyun #include <linux/random.h>
28*4882a593Smuzhiyun #include <linux/buffer_head.h>
29*4882a593Smuzhiyun #include <linux/exportfs.h>
30*4882a593Smuzhiyun #include <linux/vfs.h>
31*4882a593Smuzhiyun #include <linux/seq_file.h>
32*4882a593Smuzhiyun #include <linux/mount.h>
33*4882a593Smuzhiyun #include <linux/log2.h>
34*4882a593Smuzhiyun #include <linux/quotaops.h>
35*4882a593Smuzhiyun #include <linux/uaccess.h>
36*4882a593Smuzhiyun #include <linux/dax.h>
37*4882a593Smuzhiyun #include <linux/iversion.h>
38*4882a593Smuzhiyun #include "ext2.h"
39*4882a593Smuzhiyun #include "xattr.h"
40*4882a593Smuzhiyun #include "acl.h"
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun static void ext2_write_super(struct super_block *sb);
43*4882a593Smuzhiyun static int ext2_remount (struct super_block * sb, int * flags, char * data);
44*4882a593Smuzhiyun static int ext2_statfs (struct dentry * dentry, struct kstatfs * buf);
45*4882a593Smuzhiyun static int ext2_sync_fs(struct super_block *sb, int wait);
46*4882a593Smuzhiyun static int ext2_freeze(struct super_block *sb);
47*4882a593Smuzhiyun static int ext2_unfreeze(struct super_block *sb);
48*4882a593Smuzhiyun 
ext2_error(struct super_block * sb,const char * function,const char * fmt,...)49*4882a593Smuzhiyun void ext2_error(struct super_block *sb, const char *function,
50*4882a593Smuzhiyun 		const char *fmt, ...)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun 	struct va_format vaf;
53*4882a593Smuzhiyun 	va_list args;
54*4882a593Smuzhiyun 	struct ext2_sb_info *sbi = EXT2_SB(sb);
55*4882a593Smuzhiyun 	struct ext2_super_block *es = sbi->s_es;
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	if (!sb_rdonly(sb)) {
58*4882a593Smuzhiyun 		spin_lock(&sbi->s_lock);
59*4882a593Smuzhiyun 		sbi->s_mount_state |= EXT2_ERROR_FS;
60*4882a593Smuzhiyun 		es->s_state |= cpu_to_le16(EXT2_ERROR_FS);
61*4882a593Smuzhiyun 		spin_unlock(&sbi->s_lock);
62*4882a593Smuzhiyun 		ext2_sync_super(sb, es, 1);
63*4882a593Smuzhiyun 	}
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	va_start(args, fmt);
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	vaf.fmt = fmt;
68*4882a593Smuzhiyun 	vaf.va = &args;
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	printk(KERN_CRIT "EXT2-fs (%s): error: %s: %pV\n",
71*4882a593Smuzhiyun 	       sb->s_id, function, &vaf);
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	va_end(args);
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	if (test_opt(sb, ERRORS_PANIC))
76*4882a593Smuzhiyun 		panic("EXT2-fs: panic from previous error\n");
77*4882a593Smuzhiyun 	if (!sb_rdonly(sb) && test_opt(sb, ERRORS_RO)) {
78*4882a593Smuzhiyun 		ext2_msg(sb, KERN_CRIT,
79*4882a593Smuzhiyun 			     "error: remounting filesystem read-only");
80*4882a593Smuzhiyun 		sb->s_flags |= SB_RDONLY;
81*4882a593Smuzhiyun 	}
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun 
ext2_msg(struct super_block * sb,const char * prefix,const char * fmt,...)84*4882a593Smuzhiyun void ext2_msg(struct super_block *sb, const char *prefix,
85*4882a593Smuzhiyun 		const char *fmt, ...)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun 	struct va_format vaf;
88*4882a593Smuzhiyun 	va_list args;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	va_start(args, fmt);
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	vaf.fmt = fmt;
93*4882a593Smuzhiyun 	vaf.va = &args;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	printk("%sEXT2-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	va_end(args);
98*4882a593Smuzhiyun }
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun /*
101*4882a593Smuzhiyun  * This must be called with sbi->s_lock held.
102*4882a593Smuzhiyun  */
ext2_update_dynamic_rev(struct super_block * sb)103*4882a593Smuzhiyun void ext2_update_dynamic_rev(struct super_block *sb)
104*4882a593Smuzhiyun {
105*4882a593Smuzhiyun 	struct ext2_super_block *es = EXT2_SB(sb)->s_es;
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	if (le32_to_cpu(es->s_rev_level) > EXT2_GOOD_OLD_REV)
108*4882a593Smuzhiyun 		return;
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	ext2_msg(sb, KERN_WARNING,
111*4882a593Smuzhiyun 		     "warning: updating to rev %d because of "
112*4882a593Smuzhiyun 		     "new feature flag, running e2fsck is recommended",
113*4882a593Smuzhiyun 		     EXT2_DYNAMIC_REV);
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	es->s_first_ino = cpu_to_le32(EXT2_GOOD_OLD_FIRST_INO);
116*4882a593Smuzhiyun 	es->s_inode_size = cpu_to_le16(EXT2_GOOD_OLD_INODE_SIZE);
117*4882a593Smuzhiyun 	es->s_rev_level = cpu_to_le32(EXT2_DYNAMIC_REV);
118*4882a593Smuzhiyun 	/* leave es->s_feature_*compat flags alone */
119*4882a593Smuzhiyun 	/* es->s_uuid will be set by e2fsck if empty */
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	/*
122*4882a593Smuzhiyun 	 * The rest of the superblock fields should be zero, and if not it
123*4882a593Smuzhiyun 	 * means they are likely already in use, so leave them alone.  We
124*4882a593Smuzhiyun 	 * can leave it up to e2fsck to clean up any inconsistencies there.
125*4882a593Smuzhiyun 	 */
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun #ifdef CONFIG_QUOTA
129*4882a593Smuzhiyun static int ext2_quota_off(struct super_block *sb, int type);
130*4882a593Smuzhiyun 
ext2_quota_off_umount(struct super_block * sb)131*4882a593Smuzhiyun static void ext2_quota_off_umount(struct super_block *sb)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun 	int type;
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	for (type = 0; type < MAXQUOTAS; type++)
136*4882a593Smuzhiyun 		ext2_quota_off(sb, type);
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun #else
ext2_quota_off_umount(struct super_block * sb)139*4882a593Smuzhiyun static inline void ext2_quota_off_umount(struct super_block *sb)
140*4882a593Smuzhiyun {
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun #endif
143*4882a593Smuzhiyun 
ext2_put_super(struct super_block * sb)144*4882a593Smuzhiyun static void ext2_put_super (struct super_block * sb)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun 	int db_count;
147*4882a593Smuzhiyun 	int i;
148*4882a593Smuzhiyun 	struct ext2_sb_info *sbi = EXT2_SB(sb);
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	ext2_quota_off_umount(sb);
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	ext2_xattr_destroy_cache(sbi->s_ea_block_cache);
153*4882a593Smuzhiyun 	sbi->s_ea_block_cache = NULL;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	if (!sb_rdonly(sb)) {
156*4882a593Smuzhiyun 		struct ext2_super_block *es = sbi->s_es;
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 		spin_lock(&sbi->s_lock);
159*4882a593Smuzhiyun 		es->s_state = cpu_to_le16(sbi->s_mount_state);
160*4882a593Smuzhiyun 		spin_unlock(&sbi->s_lock);
161*4882a593Smuzhiyun 		ext2_sync_super(sb, es, 1);
162*4882a593Smuzhiyun 	}
163*4882a593Smuzhiyun 	db_count = sbi->s_gdb_count;
164*4882a593Smuzhiyun 	for (i = 0; i < db_count; i++)
165*4882a593Smuzhiyun 		brelse(sbi->s_group_desc[i]);
166*4882a593Smuzhiyun 	kfree(sbi->s_group_desc);
167*4882a593Smuzhiyun 	kfree(sbi->s_debts);
168*4882a593Smuzhiyun 	percpu_counter_destroy(&sbi->s_freeblocks_counter);
169*4882a593Smuzhiyun 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
170*4882a593Smuzhiyun 	percpu_counter_destroy(&sbi->s_dirs_counter);
171*4882a593Smuzhiyun 	brelse (sbi->s_sbh);
172*4882a593Smuzhiyun 	sb->s_fs_info = NULL;
173*4882a593Smuzhiyun 	kfree(sbi->s_blockgroup_lock);
174*4882a593Smuzhiyun 	fs_put_dax(sbi->s_daxdev);
175*4882a593Smuzhiyun 	kfree(sbi);
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun static struct kmem_cache * ext2_inode_cachep;
179*4882a593Smuzhiyun 
ext2_alloc_inode(struct super_block * sb)180*4882a593Smuzhiyun static struct inode *ext2_alloc_inode(struct super_block *sb)
181*4882a593Smuzhiyun {
182*4882a593Smuzhiyun 	struct ext2_inode_info *ei;
183*4882a593Smuzhiyun 	ei = kmem_cache_alloc(ext2_inode_cachep, GFP_KERNEL);
184*4882a593Smuzhiyun 	if (!ei)
185*4882a593Smuzhiyun 		return NULL;
186*4882a593Smuzhiyun 	ei->i_block_alloc_info = NULL;
187*4882a593Smuzhiyun 	inode_set_iversion(&ei->vfs_inode, 1);
188*4882a593Smuzhiyun #ifdef CONFIG_QUOTA
189*4882a593Smuzhiyun 	memset(&ei->i_dquot, 0, sizeof(ei->i_dquot));
190*4882a593Smuzhiyun #endif
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	return &ei->vfs_inode;
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun 
ext2_free_in_core_inode(struct inode * inode)195*4882a593Smuzhiyun static void ext2_free_in_core_inode(struct inode *inode)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun 	kmem_cache_free(ext2_inode_cachep, EXT2_I(inode));
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun 
init_once(void * foo)200*4882a593Smuzhiyun static void init_once(void *foo)
201*4882a593Smuzhiyun {
202*4882a593Smuzhiyun 	struct ext2_inode_info *ei = (struct ext2_inode_info *) foo;
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	rwlock_init(&ei->i_meta_lock);
205*4882a593Smuzhiyun #ifdef CONFIG_EXT2_FS_XATTR
206*4882a593Smuzhiyun 	init_rwsem(&ei->xattr_sem);
207*4882a593Smuzhiyun #endif
208*4882a593Smuzhiyun 	mutex_init(&ei->truncate_mutex);
209*4882a593Smuzhiyun #ifdef CONFIG_FS_DAX
210*4882a593Smuzhiyun 	init_rwsem(&ei->dax_sem);
211*4882a593Smuzhiyun #endif
212*4882a593Smuzhiyun 	inode_init_once(&ei->vfs_inode);
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun 
init_inodecache(void)215*4882a593Smuzhiyun static int __init init_inodecache(void)
216*4882a593Smuzhiyun {
217*4882a593Smuzhiyun 	ext2_inode_cachep = kmem_cache_create_usercopy("ext2_inode_cache",
218*4882a593Smuzhiyun 				sizeof(struct ext2_inode_info), 0,
219*4882a593Smuzhiyun 				(SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|
220*4882a593Smuzhiyun 					SLAB_ACCOUNT),
221*4882a593Smuzhiyun 				offsetof(struct ext2_inode_info, i_data),
222*4882a593Smuzhiyun 				sizeof_field(struct ext2_inode_info, i_data),
223*4882a593Smuzhiyun 				init_once);
224*4882a593Smuzhiyun 	if (ext2_inode_cachep == NULL)
225*4882a593Smuzhiyun 		return -ENOMEM;
226*4882a593Smuzhiyun 	return 0;
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun 
destroy_inodecache(void)229*4882a593Smuzhiyun static void destroy_inodecache(void)
230*4882a593Smuzhiyun {
231*4882a593Smuzhiyun 	/*
232*4882a593Smuzhiyun 	 * Make sure all delayed rcu free inodes are flushed before we
233*4882a593Smuzhiyun 	 * destroy cache.
234*4882a593Smuzhiyun 	 */
235*4882a593Smuzhiyun 	rcu_barrier();
236*4882a593Smuzhiyun 	kmem_cache_destroy(ext2_inode_cachep);
237*4882a593Smuzhiyun }
238*4882a593Smuzhiyun 
ext2_show_options(struct seq_file * seq,struct dentry * root)239*4882a593Smuzhiyun static int ext2_show_options(struct seq_file *seq, struct dentry *root)
240*4882a593Smuzhiyun {
241*4882a593Smuzhiyun 	struct super_block *sb = root->d_sb;
242*4882a593Smuzhiyun 	struct ext2_sb_info *sbi = EXT2_SB(sb);
243*4882a593Smuzhiyun 	struct ext2_super_block *es = sbi->s_es;
244*4882a593Smuzhiyun 	unsigned long def_mount_opts;
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	spin_lock(&sbi->s_lock);
247*4882a593Smuzhiyun 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	if (sbi->s_sb_block != 1)
250*4882a593Smuzhiyun 		seq_printf(seq, ",sb=%lu", sbi->s_sb_block);
251*4882a593Smuzhiyun 	if (test_opt(sb, MINIX_DF))
252*4882a593Smuzhiyun 		seq_puts(seq, ",minixdf");
253*4882a593Smuzhiyun 	if (test_opt(sb, GRPID))
254*4882a593Smuzhiyun 		seq_puts(seq, ",grpid");
255*4882a593Smuzhiyun 	if (!test_opt(sb, GRPID) && (def_mount_opts & EXT2_DEFM_BSDGROUPS))
256*4882a593Smuzhiyun 		seq_puts(seq, ",nogrpid");
257*4882a593Smuzhiyun 	if (!uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT2_DEF_RESUID)) ||
258*4882a593Smuzhiyun 	    le16_to_cpu(es->s_def_resuid) != EXT2_DEF_RESUID) {
259*4882a593Smuzhiyun 		seq_printf(seq, ",resuid=%u",
260*4882a593Smuzhiyun 				from_kuid_munged(&init_user_ns, sbi->s_resuid));
261*4882a593Smuzhiyun 	}
262*4882a593Smuzhiyun 	if (!gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT2_DEF_RESGID)) ||
263*4882a593Smuzhiyun 	    le16_to_cpu(es->s_def_resgid) != EXT2_DEF_RESGID) {
264*4882a593Smuzhiyun 		seq_printf(seq, ",resgid=%u",
265*4882a593Smuzhiyun 				from_kgid_munged(&init_user_ns, sbi->s_resgid));
266*4882a593Smuzhiyun 	}
267*4882a593Smuzhiyun 	if (test_opt(sb, ERRORS_RO)) {
268*4882a593Smuzhiyun 		int def_errors = le16_to_cpu(es->s_errors);
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 		if (def_errors == EXT2_ERRORS_PANIC ||
271*4882a593Smuzhiyun 		    def_errors == EXT2_ERRORS_CONTINUE) {
272*4882a593Smuzhiyun 			seq_puts(seq, ",errors=remount-ro");
273*4882a593Smuzhiyun 		}
274*4882a593Smuzhiyun 	}
275*4882a593Smuzhiyun 	if (test_opt(sb, ERRORS_CONT))
276*4882a593Smuzhiyun 		seq_puts(seq, ",errors=continue");
277*4882a593Smuzhiyun 	if (test_opt(sb, ERRORS_PANIC))
278*4882a593Smuzhiyun 		seq_puts(seq, ",errors=panic");
279*4882a593Smuzhiyun 	if (test_opt(sb, NO_UID32))
280*4882a593Smuzhiyun 		seq_puts(seq, ",nouid32");
281*4882a593Smuzhiyun 	if (test_opt(sb, DEBUG))
282*4882a593Smuzhiyun 		seq_puts(seq, ",debug");
283*4882a593Smuzhiyun 	if (test_opt(sb, OLDALLOC))
284*4882a593Smuzhiyun 		seq_puts(seq, ",oldalloc");
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun #ifdef CONFIG_EXT2_FS_XATTR
287*4882a593Smuzhiyun 	if (test_opt(sb, XATTR_USER))
288*4882a593Smuzhiyun 		seq_puts(seq, ",user_xattr");
289*4882a593Smuzhiyun 	if (!test_opt(sb, XATTR_USER) &&
290*4882a593Smuzhiyun 	    (def_mount_opts & EXT2_DEFM_XATTR_USER)) {
291*4882a593Smuzhiyun 		seq_puts(seq, ",nouser_xattr");
292*4882a593Smuzhiyun 	}
293*4882a593Smuzhiyun #endif
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun #ifdef CONFIG_EXT2_FS_POSIX_ACL
296*4882a593Smuzhiyun 	if (test_opt(sb, POSIX_ACL))
297*4882a593Smuzhiyun 		seq_puts(seq, ",acl");
298*4882a593Smuzhiyun 	if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT2_DEFM_ACL))
299*4882a593Smuzhiyun 		seq_puts(seq, ",noacl");
300*4882a593Smuzhiyun #endif
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 	if (test_opt(sb, NOBH))
303*4882a593Smuzhiyun 		seq_puts(seq, ",nobh");
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun 	if (test_opt(sb, USRQUOTA))
306*4882a593Smuzhiyun 		seq_puts(seq, ",usrquota");
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 	if (test_opt(sb, GRPQUOTA))
309*4882a593Smuzhiyun 		seq_puts(seq, ",grpquota");
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 	if (test_opt(sb, XIP))
312*4882a593Smuzhiyun 		seq_puts(seq, ",xip");
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 	if (test_opt(sb, DAX))
315*4882a593Smuzhiyun 		seq_puts(seq, ",dax");
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 	if (!test_opt(sb, RESERVATION))
318*4882a593Smuzhiyun 		seq_puts(seq, ",noreservation");
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 	spin_unlock(&sbi->s_lock);
321*4882a593Smuzhiyun 	return 0;
322*4882a593Smuzhiyun }
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun #ifdef CONFIG_QUOTA
325*4882a593Smuzhiyun static ssize_t ext2_quota_read(struct super_block *sb, int type, char *data, size_t len, loff_t off);
326*4882a593Smuzhiyun static ssize_t ext2_quota_write(struct super_block *sb, int type, const char *data, size_t len, loff_t off);
327*4882a593Smuzhiyun static int ext2_quota_on(struct super_block *sb, int type, int format_id,
328*4882a593Smuzhiyun 			 const struct path *path);
ext2_get_dquots(struct inode * inode)329*4882a593Smuzhiyun static struct dquot **ext2_get_dquots(struct inode *inode)
330*4882a593Smuzhiyun {
331*4882a593Smuzhiyun 	return EXT2_I(inode)->i_dquot;
332*4882a593Smuzhiyun }
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun static const struct quotactl_ops ext2_quotactl_ops = {
335*4882a593Smuzhiyun 	.quota_on	= ext2_quota_on,
336*4882a593Smuzhiyun 	.quota_off	= ext2_quota_off,
337*4882a593Smuzhiyun 	.quota_sync	= dquot_quota_sync,
338*4882a593Smuzhiyun 	.get_state	= dquot_get_state,
339*4882a593Smuzhiyun 	.set_info	= dquot_set_dqinfo,
340*4882a593Smuzhiyun 	.get_dqblk	= dquot_get_dqblk,
341*4882a593Smuzhiyun 	.set_dqblk	= dquot_set_dqblk,
342*4882a593Smuzhiyun 	.get_nextdqblk	= dquot_get_next_dqblk,
343*4882a593Smuzhiyun };
344*4882a593Smuzhiyun #endif
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun static const struct super_operations ext2_sops = {
347*4882a593Smuzhiyun 	.alloc_inode	= ext2_alloc_inode,
348*4882a593Smuzhiyun 	.free_inode	= ext2_free_in_core_inode,
349*4882a593Smuzhiyun 	.write_inode	= ext2_write_inode,
350*4882a593Smuzhiyun 	.evict_inode	= ext2_evict_inode,
351*4882a593Smuzhiyun 	.put_super	= ext2_put_super,
352*4882a593Smuzhiyun 	.sync_fs	= ext2_sync_fs,
353*4882a593Smuzhiyun 	.freeze_fs	= ext2_freeze,
354*4882a593Smuzhiyun 	.unfreeze_fs	= ext2_unfreeze,
355*4882a593Smuzhiyun 	.statfs		= ext2_statfs,
356*4882a593Smuzhiyun 	.remount_fs	= ext2_remount,
357*4882a593Smuzhiyun 	.show_options	= ext2_show_options,
358*4882a593Smuzhiyun #ifdef CONFIG_QUOTA
359*4882a593Smuzhiyun 	.quota_read	= ext2_quota_read,
360*4882a593Smuzhiyun 	.quota_write	= ext2_quota_write,
361*4882a593Smuzhiyun 	.get_dquots	= ext2_get_dquots,
362*4882a593Smuzhiyun #endif
363*4882a593Smuzhiyun };
364*4882a593Smuzhiyun 
ext2_nfs_get_inode(struct super_block * sb,u64 ino,u32 generation)365*4882a593Smuzhiyun static struct inode *ext2_nfs_get_inode(struct super_block *sb,
366*4882a593Smuzhiyun 		u64 ino, u32 generation)
367*4882a593Smuzhiyun {
368*4882a593Smuzhiyun 	struct inode *inode;
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun 	if (ino < EXT2_FIRST_INO(sb) && ino != EXT2_ROOT_INO)
371*4882a593Smuzhiyun 		return ERR_PTR(-ESTALE);
372*4882a593Smuzhiyun 	if (ino > le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count))
373*4882a593Smuzhiyun 		return ERR_PTR(-ESTALE);
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 	/*
376*4882a593Smuzhiyun 	 * ext2_iget isn't quite right if the inode is currently unallocated!
377*4882a593Smuzhiyun 	 * However ext2_iget currently does appropriate checks to handle stale
378*4882a593Smuzhiyun 	 * inodes so everything is OK.
379*4882a593Smuzhiyun 	 */
380*4882a593Smuzhiyun 	inode = ext2_iget(sb, ino);
381*4882a593Smuzhiyun 	if (IS_ERR(inode))
382*4882a593Smuzhiyun 		return ERR_CAST(inode);
383*4882a593Smuzhiyun 	if (generation && inode->i_generation != generation) {
384*4882a593Smuzhiyun 		/* we didn't find the right inode.. */
385*4882a593Smuzhiyun 		iput(inode);
386*4882a593Smuzhiyun 		return ERR_PTR(-ESTALE);
387*4882a593Smuzhiyun 	}
388*4882a593Smuzhiyun 	return inode;
389*4882a593Smuzhiyun }
390*4882a593Smuzhiyun 
ext2_fh_to_dentry(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)391*4882a593Smuzhiyun static struct dentry *ext2_fh_to_dentry(struct super_block *sb, struct fid *fid,
392*4882a593Smuzhiyun 		int fh_len, int fh_type)
393*4882a593Smuzhiyun {
394*4882a593Smuzhiyun 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
395*4882a593Smuzhiyun 				    ext2_nfs_get_inode);
396*4882a593Smuzhiyun }
397*4882a593Smuzhiyun 
ext2_fh_to_parent(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)398*4882a593Smuzhiyun static struct dentry *ext2_fh_to_parent(struct super_block *sb, struct fid *fid,
399*4882a593Smuzhiyun 		int fh_len, int fh_type)
400*4882a593Smuzhiyun {
401*4882a593Smuzhiyun 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
402*4882a593Smuzhiyun 				    ext2_nfs_get_inode);
403*4882a593Smuzhiyun }
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun static const struct export_operations ext2_export_ops = {
406*4882a593Smuzhiyun 	.fh_to_dentry = ext2_fh_to_dentry,
407*4882a593Smuzhiyun 	.fh_to_parent = ext2_fh_to_parent,
408*4882a593Smuzhiyun 	.get_parent = ext2_get_parent,
409*4882a593Smuzhiyun };
410*4882a593Smuzhiyun 
get_sb_block(void ** data)411*4882a593Smuzhiyun static unsigned long get_sb_block(void **data)
412*4882a593Smuzhiyun {
413*4882a593Smuzhiyun 	unsigned long 	sb_block;
414*4882a593Smuzhiyun 	char 		*options = (char *) *data;
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun 	if (!options || strncmp(options, "sb=", 3) != 0)
417*4882a593Smuzhiyun 		return 1;	/* Default location */
418*4882a593Smuzhiyun 	options += 3;
419*4882a593Smuzhiyun 	sb_block = simple_strtoul(options, &options, 0);
420*4882a593Smuzhiyun 	if (*options && *options != ',') {
421*4882a593Smuzhiyun 		printk("EXT2-fs: Invalid sb specification: %s\n",
422*4882a593Smuzhiyun 		       (char *) *data);
423*4882a593Smuzhiyun 		return 1;
424*4882a593Smuzhiyun 	}
425*4882a593Smuzhiyun 	if (*options == ',')
426*4882a593Smuzhiyun 		options++;
427*4882a593Smuzhiyun 	*data = (void *) options;
428*4882a593Smuzhiyun 	return sb_block;
429*4882a593Smuzhiyun }
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun enum {
432*4882a593Smuzhiyun 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
433*4882a593Smuzhiyun 	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic,
434*4882a593Smuzhiyun 	Opt_err_ro, Opt_nouid32, Opt_debug,
435*4882a593Smuzhiyun 	Opt_oldalloc, Opt_orlov, Opt_nobh, Opt_user_xattr, Opt_nouser_xattr,
436*4882a593Smuzhiyun 	Opt_acl, Opt_noacl, Opt_xip, Opt_dax, Opt_ignore, Opt_err, Opt_quota,
437*4882a593Smuzhiyun 	Opt_usrquota, Opt_grpquota, Opt_reservation, Opt_noreservation
438*4882a593Smuzhiyun };
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun static const match_table_t tokens = {
441*4882a593Smuzhiyun 	{Opt_bsd_df, "bsddf"},
442*4882a593Smuzhiyun 	{Opt_minix_df, "minixdf"},
443*4882a593Smuzhiyun 	{Opt_grpid, "grpid"},
444*4882a593Smuzhiyun 	{Opt_grpid, "bsdgroups"},
445*4882a593Smuzhiyun 	{Opt_nogrpid, "nogrpid"},
446*4882a593Smuzhiyun 	{Opt_nogrpid, "sysvgroups"},
447*4882a593Smuzhiyun 	{Opt_resgid, "resgid=%u"},
448*4882a593Smuzhiyun 	{Opt_resuid, "resuid=%u"},
449*4882a593Smuzhiyun 	{Opt_sb, "sb=%u"},
450*4882a593Smuzhiyun 	{Opt_err_cont, "errors=continue"},
451*4882a593Smuzhiyun 	{Opt_err_panic, "errors=panic"},
452*4882a593Smuzhiyun 	{Opt_err_ro, "errors=remount-ro"},
453*4882a593Smuzhiyun 	{Opt_nouid32, "nouid32"},
454*4882a593Smuzhiyun 	{Opt_debug, "debug"},
455*4882a593Smuzhiyun 	{Opt_oldalloc, "oldalloc"},
456*4882a593Smuzhiyun 	{Opt_orlov, "orlov"},
457*4882a593Smuzhiyun 	{Opt_nobh, "nobh"},
458*4882a593Smuzhiyun 	{Opt_user_xattr, "user_xattr"},
459*4882a593Smuzhiyun 	{Opt_nouser_xattr, "nouser_xattr"},
460*4882a593Smuzhiyun 	{Opt_acl, "acl"},
461*4882a593Smuzhiyun 	{Opt_noacl, "noacl"},
462*4882a593Smuzhiyun 	{Opt_xip, "xip"},
463*4882a593Smuzhiyun 	{Opt_dax, "dax"},
464*4882a593Smuzhiyun 	{Opt_grpquota, "grpquota"},
465*4882a593Smuzhiyun 	{Opt_ignore, "noquota"},
466*4882a593Smuzhiyun 	{Opt_quota, "quota"},
467*4882a593Smuzhiyun 	{Opt_usrquota, "usrquota"},
468*4882a593Smuzhiyun 	{Opt_reservation, "reservation"},
469*4882a593Smuzhiyun 	{Opt_noreservation, "noreservation"},
470*4882a593Smuzhiyun 	{Opt_err, NULL}
471*4882a593Smuzhiyun };
472*4882a593Smuzhiyun 
parse_options(char * options,struct super_block * sb,struct ext2_mount_options * opts)473*4882a593Smuzhiyun static int parse_options(char *options, struct super_block *sb,
474*4882a593Smuzhiyun 			 struct ext2_mount_options *opts)
475*4882a593Smuzhiyun {
476*4882a593Smuzhiyun 	char *p;
477*4882a593Smuzhiyun 	substring_t args[MAX_OPT_ARGS];
478*4882a593Smuzhiyun 	int option;
479*4882a593Smuzhiyun 	kuid_t uid;
480*4882a593Smuzhiyun 	kgid_t gid;
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 	if (!options)
483*4882a593Smuzhiyun 		return 1;
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 	while ((p = strsep (&options, ",")) != NULL) {
486*4882a593Smuzhiyun 		int token;
487*4882a593Smuzhiyun 		if (!*p)
488*4882a593Smuzhiyun 			continue;
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun 		token = match_token(p, tokens, args);
491*4882a593Smuzhiyun 		switch (token) {
492*4882a593Smuzhiyun 		case Opt_bsd_df:
493*4882a593Smuzhiyun 			clear_opt (opts->s_mount_opt, MINIX_DF);
494*4882a593Smuzhiyun 			break;
495*4882a593Smuzhiyun 		case Opt_minix_df:
496*4882a593Smuzhiyun 			set_opt (opts->s_mount_opt, MINIX_DF);
497*4882a593Smuzhiyun 			break;
498*4882a593Smuzhiyun 		case Opt_grpid:
499*4882a593Smuzhiyun 			set_opt (opts->s_mount_opt, GRPID);
500*4882a593Smuzhiyun 			break;
501*4882a593Smuzhiyun 		case Opt_nogrpid:
502*4882a593Smuzhiyun 			clear_opt (opts->s_mount_opt, GRPID);
503*4882a593Smuzhiyun 			break;
504*4882a593Smuzhiyun 		case Opt_resuid:
505*4882a593Smuzhiyun 			if (match_int(&args[0], &option))
506*4882a593Smuzhiyun 				return 0;
507*4882a593Smuzhiyun 			uid = make_kuid(current_user_ns(), option);
508*4882a593Smuzhiyun 			if (!uid_valid(uid)) {
509*4882a593Smuzhiyun 				ext2_msg(sb, KERN_ERR, "Invalid uid value %d", option);
510*4882a593Smuzhiyun 				return 0;
511*4882a593Smuzhiyun 
512*4882a593Smuzhiyun 			}
513*4882a593Smuzhiyun 			opts->s_resuid = uid;
514*4882a593Smuzhiyun 			break;
515*4882a593Smuzhiyun 		case Opt_resgid:
516*4882a593Smuzhiyun 			if (match_int(&args[0], &option))
517*4882a593Smuzhiyun 				return 0;
518*4882a593Smuzhiyun 			gid = make_kgid(current_user_ns(), option);
519*4882a593Smuzhiyun 			if (!gid_valid(gid)) {
520*4882a593Smuzhiyun 				ext2_msg(sb, KERN_ERR, "Invalid gid value %d", option);
521*4882a593Smuzhiyun 				return 0;
522*4882a593Smuzhiyun 			}
523*4882a593Smuzhiyun 			opts->s_resgid = gid;
524*4882a593Smuzhiyun 			break;
525*4882a593Smuzhiyun 		case Opt_sb:
526*4882a593Smuzhiyun 			/* handled by get_sb_block() instead of here */
527*4882a593Smuzhiyun 			/* *sb_block = match_int(&args[0]); */
528*4882a593Smuzhiyun 			break;
529*4882a593Smuzhiyun 		case Opt_err_panic:
530*4882a593Smuzhiyun 			clear_opt (opts->s_mount_opt, ERRORS_CONT);
531*4882a593Smuzhiyun 			clear_opt (opts->s_mount_opt, ERRORS_RO);
532*4882a593Smuzhiyun 			set_opt (opts->s_mount_opt, ERRORS_PANIC);
533*4882a593Smuzhiyun 			break;
534*4882a593Smuzhiyun 		case Opt_err_ro:
535*4882a593Smuzhiyun 			clear_opt (opts->s_mount_opt, ERRORS_CONT);
536*4882a593Smuzhiyun 			clear_opt (opts->s_mount_opt, ERRORS_PANIC);
537*4882a593Smuzhiyun 			set_opt (opts->s_mount_opt, ERRORS_RO);
538*4882a593Smuzhiyun 			break;
539*4882a593Smuzhiyun 		case Opt_err_cont:
540*4882a593Smuzhiyun 			clear_opt (opts->s_mount_opt, ERRORS_RO);
541*4882a593Smuzhiyun 			clear_opt (opts->s_mount_opt, ERRORS_PANIC);
542*4882a593Smuzhiyun 			set_opt (opts->s_mount_opt, ERRORS_CONT);
543*4882a593Smuzhiyun 			break;
544*4882a593Smuzhiyun 		case Opt_nouid32:
545*4882a593Smuzhiyun 			set_opt (opts->s_mount_opt, NO_UID32);
546*4882a593Smuzhiyun 			break;
547*4882a593Smuzhiyun 		case Opt_debug:
548*4882a593Smuzhiyun 			set_opt (opts->s_mount_opt, DEBUG);
549*4882a593Smuzhiyun 			break;
550*4882a593Smuzhiyun 		case Opt_oldalloc:
551*4882a593Smuzhiyun 			set_opt (opts->s_mount_opt, OLDALLOC);
552*4882a593Smuzhiyun 			break;
553*4882a593Smuzhiyun 		case Opt_orlov:
554*4882a593Smuzhiyun 			clear_opt (opts->s_mount_opt, OLDALLOC);
555*4882a593Smuzhiyun 			break;
556*4882a593Smuzhiyun 		case Opt_nobh:
557*4882a593Smuzhiyun 			set_opt (opts->s_mount_opt, NOBH);
558*4882a593Smuzhiyun 			break;
559*4882a593Smuzhiyun #ifdef CONFIG_EXT2_FS_XATTR
560*4882a593Smuzhiyun 		case Opt_user_xattr:
561*4882a593Smuzhiyun 			set_opt (opts->s_mount_opt, XATTR_USER);
562*4882a593Smuzhiyun 			break;
563*4882a593Smuzhiyun 		case Opt_nouser_xattr:
564*4882a593Smuzhiyun 			clear_opt (opts->s_mount_opt, XATTR_USER);
565*4882a593Smuzhiyun 			break;
566*4882a593Smuzhiyun #else
567*4882a593Smuzhiyun 		case Opt_user_xattr:
568*4882a593Smuzhiyun 		case Opt_nouser_xattr:
569*4882a593Smuzhiyun 			ext2_msg(sb, KERN_INFO, "(no)user_xattr options"
570*4882a593Smuzhiyun 				"not supported");
571*4882a593Smuzhiyun 			break;
572*4882a593Smuzhiyun #endif
573*4882a593Smuzhiyun #ifdef CONFIG_EXT2_FS_POSIX_ACL
574*4882a593Smuzhiyun 		case Opt_acl:
575*4882a593Smuzhiyun 			set_opt(opts->s_mount_opt, POSIX_ACL);
576*4882a593Smuzhiyun 			break;
577*4882a593Smuzhiyun 		case Opt_noacl:
578*4882a593Smuzhiyun 			clear_opt(opts->s_mount_opt, POSIX_ACL);
579*4882a593Smuzhiyun 			break;
580*4882a593Smuzhiyun #else
581*4882a593Smuzhiyun 		case Opt_acl:
582*4882a593Smuzhiyun 		case Opt_noacl:
583*4882a593Smuzhiyun 			ext2_msg(sb, KERN_INFO,
584*4882a593Smuzhiyun 				"(no)acl options not supported");
585*4882a593Smuzhiyun 			break;
586*4882a593Smuzhiyun #endif
587*4882a593Smuzhiyun 		case Opt_xip:
588*4882a593Smuzhiyun 			ext2_msg(sb, KERN_INFO, "use dax instead of xip");
589*4882a593Smuzhiyun 			set_opt(opts->s_mount_opt, XIP);
590*4882a593Smuzhiyun 			fallthrough;
591*4882a593Smuzhiyun 		case Opt_dax:
592*4882a593Smuzhiyun #ifdef CONFIG_FS_DAX
593*4882a593Smuzhiyun 			ext2_msg(sb, KERN_WARNING,
594*4882a593Smuzhiyun 		"DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
595*4882a593Smuzhiyun 			set_opt(opts->s_mount_opt, DAX);
596*4882a593Smuzhiyun #else
597*4882a593Smuzhiyun 			ext2_msg(sb, KERN_INFO, "dax option not supported");
598*4882a593Smuzhiyun #endif
599*4882a593Smuzhiyun 			break;
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun #if defined(CONFIG_QUOTA)
602*4882a593Smuzhiyun 		case Opt_quota:
603*4882a593Smuzhiyun 		case Opt_usrquota:
604*4882a593Smuzhiyun 			set_opt(opts->s_mount_opt, USRQUOTA);
605*4882a593Smuzhiyun 			break;
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun 		case Opt_grpquota:
608*4882a593Smuzhiyun 			set_opt(opts->s_mount_opt, GRPQUOTA);
609*4882a593Smuzhiyun 			break;
610*4882a593Smuzhiyun #else
611*4882a593Smuzhiyun 		case Opt_quota:
612*4882a593Smuzhiyun 		case Opt_usrquota:
613*4882a593Smuzhiyun 		case Opt_grpquota:
614*4882a593Smuzhiyun 			ext2_msg(sb, KERN_INFO,
615*4882a593Smuzhiyun 				"quota operations not supported");
616*4882a593Smuzhiyun 			break;
617*4882a593Smuzhiyun #endif
618*4882a593Smuzhiyun 
619*4882a593Smuzhiyun 		case Opt_reservation:
620*4882a593Smuzhiyun 			set_opt(opts->s_mount_opt, RESERVATION);
621*4882a593Smuzhiyun 			ext2_msg(sb, KERN_INFO, "reservations ON");
622*4882a593Smuzhiyun 			break;
623*4882a593Smuzhiyun 		case Opt_noreservation:
624*4882a593Smuzhiyun 			clear_opt(opts->s_mount_opt, RESERVATION);
625*4882a593Smuzhiyun 			ext2_msg(sb, KERN_INFO, "reservations OFF");
626*4882a593Smuzhiyun 			break;
627*4882a593Smuzhiyun 		case Opt_ignore:
628*4882a593Smuzhiyun 			break;
629*4882a593Smuzhiyun 		default:
630*4882a593Smuzhiyun 			return 0;
631*4882a593Smuzhiyun 		}
632*4882a593Smuzhiyun 	}
633*4882a593Smuzhiyun 	return 1;
634*4882a593Smuzhiyun }
635*4882a593Smuzhiyun 
ext2_setup_super(struct super_block * sb,struct ext2_super_block * es,int read_only)636*4882a593Smuzhiyun static int ext2_setup_super (struct super_block * sb,
637*4882a593Smuzhiyun 			      struct ext2_super_block * es,
638*4882a593Smuzhiyun 			      int read_only)
639*4882a593Smuzhiyun {
640*4882a593Smuzhiyun 	int res = 0;
641*4882a593Smuzhiyun 	struct ext2_sb_info *sbi = EXT2_SB(sb);
642*4882a593Smuzhiyun 
643*4882a593Smuzhiyun 	if (le32_to_cpu(es->s_rev_level) > EXT2_MAX_SUPP_REV) {
644*4882a593Smuzhiyun 		ext2_msg(sb, KERN_ERR,
645*4882a593Smuzhiyun 			"error: revision level too high, "
646*4882a593Smuzhiyun 			"forcing read-only mode");
647*4882a593Smuzhiyun 		res = SB_RDONLY;
648*4882a593Smuzhiyun 	}
649*4882a593Smuzhiyun 	if (read_only)
650*4882a593Smuzhiyun 		return res;
651*4882a593Smuzhiyun 	if (!(sbi->s_mount_state & EXT2_VALID_FS))
652*4882a593Smuzhiyun 		ext2_msg(sb, KERN_WARNING,
653*4882a593Smuzhiyun 			"warning: mounting unchecked fs, "
654*4882a593Smuzhiyun 			"running e2fsck is recommended");
655*4882a593Smuzhiyun 	else if ((sbi->s_mount_state & EXT2_ERROR_FS))
656*4882a593Smuzhiyun 		ext2_msg(sb, KERN_WARNING,
657*4882a593Smuzhiyun 			"warning: mounting fs with errors, "
658*4882a593Smuzhiyun 			"running e2fsck is recommended");
659*4882a593Smuzhiyun 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
660*4882a593Smuzhiyun 		 le16_to_cpu(es->s_mnt_count) >=
661*4882a593Smuzhiyun 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
662*4882a593Smuzhiyun 		ext2_msg(sb, KERN_WARNING,
663*4882a593Smuzhiyun 			"warning: maximal mount count reached, "
664*4882a593Smuzhiyun 			"running e2fsck is recommended");
665*4882a593Smuzhiyun 	else if (le32_to_cpu(es->s_checkinterval) &&
666*4882a593Smuzhiyun 		(le32_to_cpu(es->s_lastcheck) +
667*4882a593Smuzhiyun 			le32_to_cpu(es->s_checkinterval) <=
668*4882a593Smuzhiyun 			ktime_get_real_seconds()))
669*4882a593Smuzhiyun 		ext2_msg(sb, KERN_WARNING,
670*4882a593Smuzhiyun 			"warning: checktime reached, "
671*4882a593Smuzhiyun 			"running e2fsck is recommended");
672*4882a593Smuzhiyun 	if (!le16_to_cpu(es->s_max_mnt_count))
673*4882a593Smuzhiyun 		es->s_max_mnt_count = cpu_to_le16(EXT2_DFL_MAX_MNT_COUNT);
674*4882a593Smuzhiyun 	le16_add_cpu(&es->s_mnt_count, 1);
675*4882a593Smuzhiyun 	if (test_opt (sb, DEBUG))
676*4882a593Smuzhiyun 		ext2_msg(sb, KERN_INFO, "%s, %s, bs=%lu, fs=%lu, gc=%lu, "
677*4882a593Smuzhiyun 			"bpg=%lu, ipg=%lu, mo=%04lx]",
678*4882a593Smuzhiyun 			EXT2FS_VERSION, EXT2FS_DATE, sb->s_blocksize,
679*4882a593Smuzhiyun 			sbi->s_frag_size,
680*4882a593Smuzhiyun 			sbi->s_groups_count,
681*4882a593Smuzhiyun 			EXT2_BLOCKS_PER_GROUP(sb),
682*4882a593Smuzhiyun 			EXT2_INODES_PER_GROUP(sb),
683*4882a593Smuzhiyun 			sbi->s_mount_opt);
684*4882a593Smuzhiyun 	return res;
685*4882a593Smuzhiyun }
686*4882a593Smuzhiyun 
ext2_check_descriptors(struct super_block * sb)687*4882a593Smuzhiyun static int ext2_check_descriptors(struct super_block *sb)
688*4882a593Smuzhiyun {
689*4882a593Smuzhiyun 	int i;
690*4882a593Smuzhiyun 	struct ext2_sb_info *sbi = EXT2_SB(sb);
691*4882a593Smuzhiyun 
692*4882a593Smuzhiyun 	ext2_debug ("Checking group descriptors");
693*4882a593Smuzhiyun 
694*4882a593Smuzhiyun 	for (i = 0; i < sbi->s_groups_count; i++) {
695*4882a593Smuzhiyun 		struct ext2_group_desc *gdp = ext2_get_group_desc(sb, i, NULL);
696*4882a593Smuzhiyun 		ext2_fsblk_t first_block = ext2_group_first_block_no(sb, i);
697*4882a593Smuzhiyun 		ext2_fsblk_t last_block = ext2_group_last_block_no(sb, i);
698*4882a593Smuzhiyun 
699*4882a593Smuzhiyun 		if (le32_to_cpu(gdp->bg_block_bitmap) < first_block ||
700*4882a593Smuzhiyun 		    le32_to_cpu(gdp->bg_block_bitmap) > last_block)
701*4882a593Smuzhiyun 		{
702*4882a593Smuzhiyun 			ext2_error (sb, "ext2_check_descriptors",
703*4882a593Smuzhiyun 				    "Block bitmap for group %d"
704*4882a593Smuzhiyun 				    " not in group (block %lu)!",
705*4882a593Smuzhiyun 				    i, (unsigned long) le32_to_cpu(gdp->bg_block_bitmap));
706*4882a593Smuzhiyun 			return 0;
707*4882a593Smuzhiyun 		}
708*4882a593Smuzhiyun 		if (le32_to_cpu(gdp->bg_inode_bitmap) < first_block ||
709*4882a593Smuzhiyun 		    le32_to_cpu(gdp->bg_inode_bitmap) > last_block)
710*4882a593Smuzhiyun 		{
711*4882a593Smuzhiyun 			ext2_error (sb, "ext2_check_descriptors",
712*4882a593Smuzhiyun 				    "Inode bitmap for group %d"
713*4882a593Smuzhiyun 				    " not in group (block %lu)!",
714*4882a593Smuzhiyun 				    i, (unsigned long) le32_to_cpu(gdp->bg_inode_bitmap));
715*4882a593Smuzhiyun 			return 0;
716*4882a593Smuzhiyun 		}
717*4882a593Smuzhiyun 		if (le32_to_cpu(gdp->bg_inode_table) < first_block ||
718*4882a593Smuzhiyun 		    le32_to_cpu(gdp->bg_inode_table) + sbi->s_itb_per_group - 1 >
719*4882a593Smuzhiyun 		    last_block)
720*4882a593Smuzhiyun 		{
721*4882a593Smuzhiyun 			ext2_error (sb, "ext2_check_descriptors",
722*4882a593Smuzhiyun 				    "Inode table for group %d"
723*4882a593Smuzhiyun 				    " not in group (block %lu)!",
724*4882a593Smuzhiyun 				    i, (unsigned long) le32_to_cpu(gdp->bg_inode_table));
725*4882a593Smuzhiyun 			return 0;
726*4882a593Smuzhiyun 		}
727*4882a593Smuzhiyun 	}
728*4882a593Smuzhiyun 	return 1;
729*4882a593Smuzhiyun }
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun /*
732*4882a593Smuzhiyun  * Maximal file size.  There is a direct, and {,double-,triple-}indirect
733*4882a593Smuzhiyun  * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
734*4882a593Smuzhiyun  * We need to be 1 filesystem block less than the 2^32 sector limit.
735*4882a593Smuzhiyun  */
ext2_max_size(int bits)736*4882a593Smuzhiyun static loff_t ext2_max_size(int bits)
737*4882a593Smuzhiyun {
738*4882a593Smuzhiyun 	loff_t res = EXT2_NDIR_BLOCKS;
739*4882a593Smuzhiyun 	int meta_blocks;
740*4882a593Smuzhiyun 	unsigned int upper_limit;
741*4882a593Smuzhiyun 	unsigned int ppb = 1 << (bits-2);
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun 	/* This is calculated to be the largest file size for a
744*4882a593Smuzhiyun 	 * dense, file such that the total number of
745*4882a593Smuzhiyun 	 * sectors in the file, including data and all indirect blocks,
746*4882a593Smuzhiyun 	 * does not exceed 2^32 -1
747*4882a593Smuzhiyun 	 * __u32 i_blocks representing the total number of
748*4882a593Smuzhiyun 	 * 512 bytes blocks of the file
749*4882a593Smuzhiyun 	 */
750*4882a593Smuzhiyun 	upper_limit = (1LL << 32) - 1;
751*4882a593Smuzhiyun 
752*4882a593Smuzhiyun 	/* total blocks in file system block size */
753*4882a593Smuzhiyun 	upper_limit >>= (bits - 9);
754*4882a593Smuzhiyun 
755*4882a593Smuzhiyun 	/* Compute how many blocks we can address by block tree */
756*4882a593Smuzhiyun 	res += 1LL << (bits-2);
757*4882a593Smuzhiyun 	res += 1LL << (2*(bits-2));
758*4882a593Smuzhiyun 	res += 1LL << (3*(bits-2));
759*4882a593Smuzhiyun 	/* Compute how many metadata blocks are needed */
760*4882a593Smuzhiyun 	meta_blocks = 1;
761*4882a593Smuzhiyun 	meta_blocks += 1 + ppb;
762*4882a593Smuzhiyun 	meta_blocks += 1 + ppb + ppb * ppb;
763*4882a593Smuzhiyun 	/* Does block tree limit file size? */
764*4882a593Smuzhiyun 	if (res + meta_blocks <= upper_limit)
765*4882a593Smuzhiyun 		goto check_lfs;
766*4882a593Smuzhiyun 
767*4882a593Smuzhiyun 	res = upper_limit;
768*4882a593Smuzhiyun 	/* How many metadata blocks are needed for addressing upper_limit? */
769*4882a593Smuzhiyun 	upper_limit -= EXT2_NDIR_BLOCKS;
770*4882a593Smuzhiyun 	/* indirect blocks */
771*4882a593Smuzhiyun 	meta_blocks = 1;
772*4882a593Smuzhiyun 	upper_limit -= ppb;
773*4882a593Smuzhiyun 	/* double indirect blocks */
774*4882a593Smuzhiyun 	if (upper_limit < ppb * ppb) {
775*4882a593Smuzhiyun 		meta_blocks += 1 + DIV_ROUND_UP(upper_limit, ppb);
776*4882a593Smuzhiyun 		res -= meta_blocks;
777*4882a593Smuzhiyun 		goto check_lfs;
778*4882a593Smuzhiyun 	}
779*4882a593Smuzhiyun 	meta_blocks += 1 + ppb;
780*4882a593Smuzhiyun 	upper_limit -= ppb * ppb;
781*4882a593Smuzhiyun 	/* tripple indirect blocks for the rest */
782*4882a593Smuzhiyun 	meta_blocks += 1 + DIV_ROUND_UP(upper_limit, ppb) +
783*4882a593Smuzhiyun 		DIV_ROUND_UP(upper_limit, ppb*ppb);
784*4882a593Smuzhiyun 	res -= meta_blocks;
785*4882a593Smuzhiyun check_lfs:
786*4882a593Smuzhiyun 	res <<= bits;
787*4882a593Smuzhiyun 	if (res > MAX_LFS_FILESIZE)
788*4882a593Smuzhiyun 		res = MAX_LFS_FILESIZE;
789*4882a593Smuzhiyun 
790*4882a593Smuzhiyun 	return res;
791*4882a593Smuzhiyun }
792*4882a593Smuzhiyun 
descriptor_loc(struct super_block * sb,unsigned long logic_sb_block,int nr)793*4882a593Smuzhiyun static unsigned long descriptor_loc(struct super_block *sb,
794*4882a593Smuzhiyun 				    unsigned long logic_sb_block,
795*4882a593Smuzhiyun 				    int nr)
796*4882a593Smuzhiyun {
797*4882a593Smuzhiyun 	struct ext2_sb_info *sbi = EXT2_SB(sb);
798*4882a593Smuzhiyun 	unsigned long bg, first_meta_bg;
799*4882a593Smuzhiyun 
800*4882a593Smuzhiyun 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun 	if (!EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_META_BG) ||
803*4882a593Smuzhiyun 	    nr < first_meta_bg)
804*4882a593Smuzhiyun 		return (logic_sb_block + nr + 1);
805*4882a593Smuzhiyun 	bg = sbi->s_desc_per_block * nr;
806*4882a593Smuzhiyun 
807*4882a593Smuzhiyun 	return ext2_group_first_block_no(sb, bg) + ext2_bg_has_super(sb, bg);
808*4882a593Smuzhiyun }
809*4882a593Smuzhiyun 
ext2_fill_super(struct super_block * sb,void * data,int silent)810*4882a593Smuzhiyun static int ext2_fill_super(struct super_block *sb, void *data, int silent)
811*4882a593Smuzhiyun {
812*4882a593Smuzhiyun 	struct dax_device *dax_dev = fs_dax_get_by_bdev(sb->s_bdev);
813*4882a593Smuzhiyun 	struct buffer_head * bh;
814*4882a593Smuzhiyun 	struct ext2_sb_info * sbi;
815*4882a593Smuzhiyun 	struct ext2_super_block * es;
816*4882a593Smuzhiyun 	struct inode *root;
817*4882a593Smuzhiyun 	unsigned long block;
818*4882a593Smuzhiyun 	unsigned long sb_block = get_sb_block(&data);
819*4882a593Smuzhiyun 	unsigned long logic_sb_block;
820*4882a593Smuzhiyun 	unsigned long offset = 0;
821*4882a593Smuzhiyun 	unsigned long def_mount_opts;
822*4882a593Smuzhiyun 	long ret = -ENOMEM;
823*4882a593Smuzhiyun 	int blocksize = BLOCK_SIZE;
824*4882a593Smuzhiyun 	int db_count;
825*4882a593Smuzhiyun 	int i, j;
826*4882a593Smuzhiyun 	__le32 features;
827*4882a593Smuzhiyun 	int err;
828*4882a593Smuzhiyun 	struct ext2_mount_options opts;
829*4882a593Smuzhiyun 
830*4882a593Smuzhiyun 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
831*4882a593Smuzhiyun 	if (!sbi)
832*4882a593Smuzhiyun 		goto failed;
833*4882a593Smuzhiyun 
834*4882a593Smuzhiyun 	sbi->s_blockgroup_lock =
835*4882a593Smuzhiyun 		kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
836*4882a593Smuzhiyun 	if (!sbi->s_blockgroup_lock) {
837*4882a593Smuzhiyun 		kfree(sbi);
838*4882a593Smuzhiyun 		goto failed;
839*4882a593Smuzhiyun 	}
840*4882a593Smuzhiyun 	sb->s_fs_info = sbi;
841*4882a593Smuzhiyun 	sbi->s_sb_block = sb_block;
842*4882a593Smuzhiyun 	sbi->s_daxdev = dax_dev;
843*4882a593Smuzhiyun 
844*4882a593Smuzhiyun 	spin_lock_init(&sbi->s_lock);
845*4882a593Smuzhiyun 	ret = -EINVAL;
846*4882a593Smuzhiyun 
847*4882a593Smuzhiyun 	/*
848*4882a593Smuzhiyun 	 * See what the current blocksize for the device is, and
849*4882a593Smuzhiyun 	 * use that as the blocksize.  Otherwise (or if the blocksize
850*4882a593Smuzhiyun 	 * is smaller than the default) use the default.
851*4882a593Smuzhiyun 	 * This is important for devices that have a hardware
852*4882a593Smuzhiyun 	 * sectorsize that is larger than the default.
853*4882a593Smuzhiyun 	 */
854*4882a593Smuzhiyun 	blocksize = sb_min_blocksize(sb, BLOCK_SIZE);
855*4882a593Smuzhiyun 	if (!blocksize) {
856*4882a593Smuzhiyun 		ext2_msg(sb, KERN_ERR, "error: unable to set blocksize");
857*4882a593Smuzhiyun 		goto failed_sbi;
858*4882a593Smuzhiyun 	}
859*4882a593Smuzhiyun 
860*4882a593Smuzhiyun 	/*
861*4882a593Smuzhiyun 	 * If the superblock doesn't start on a hardware sector boundary,
862*4882a593Smuzhiyun 	 * calculate the offset.
863*4882a593Smuzhiyun 	 */
864*4882a593Smuzhiyun 	if (blocksize != BLOCK_SIZE) {
865*4882a593Smuzhiyun 		logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
866*4882a593Smuzhiyun 		offset = (sb_block*BLOCK_SIZE) % blocksize;
867*4882a593Smuzhiyun 	} else {
868*4882a593Smuzhiyun 		logic_sb_block = sb_block;
869*4882a593Smuzhiyun 	}
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun 	if (!(bh = sb_bread(sb, logic_sb_block))) {
872*4882a593Smuzhiyun 		ext2_msg(sb, KERN_ERR, "error: unable to read superblock");
873*4882a593Smuzhiyun 		goto failed_sbi;
874*4882a593Smuzhiyun 	}
875*4882a593Smuzhiyun 	/*
876*4882a593Smuzhiyun 	 * Note: s_es must be initialized as soon as possible because
877*4882a593Smuzhiyun 	 *       some ext2 macro-instructions depend on its value
878*4882a593Smuzhiyun 	 */
879*4882a593Smuzhiyun 	es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
880*4882a593Smuzhiyun 	sbi->s_es = es;
881*4882a593Smuzhiyun 	sb->s_magic = le16_to_cpu(es->s_magic);
882*4882a593Smuzhiyun 
883*4882a593Smuzhiyun 	if (sb->s_magic != EXT2_SUPER_MAGIC)
884*4882a593Smuzhiyun 		goto cantfind_ext2;
885*4882a593Smuzhiyun 
886*4882a593Smuzhiyun 	opts.s_mount_opt = 0;
887*4882a593Smuzhiyun 	/* Set defaults before we parse the mount options */
888*4882a593Smuzhiyun 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
889*4882a593Smuzhiyun 	if (def_mount_opts & EXT2_DEFM_DEBUG)
890*4882a593Smuzhiyun 		set_opt(opts.s_mount_opt, DEBUG);
891*4882a593Smuzhiyun 	if (def_mount_opts & EXT2_DEFM_BSDGROUPS)
892*4882a593Smuzhiyun 		set_opt(opts.s_mount_opt, GRPID);
893*4882a593Smuzhiyun 	if (def_mount_opts & EXT2_DEFM_UID16)
894*4882a593Smuzhiyun 		set_opt(opts.s_mount_opt, NO_UID32);
895*4882a593Smuzhiyun #ifdef CONFIG_EXT2_FS_XATTR
896*4882a593Smuzhiyun 	if (def_mount_opts & EXT2_DEFM_XATTR_USER)
897*4882a593Smuzhiyun 		set_opt(opts.s_mount_opt, XATTR_USER);
898*4882a593Smuzhiyun #endif
899*4882a593Smuzhiyun #ifdef CONFIG_EXT2_FS_POSIX_ACL
900*4882a593Smuzhiyun 	if (def_mount_opts & EXT2_DEFM_ACL)
901*4882a593Smuzhiyun 		set_opt(opts.s_mount_opt, POSIX_ACL);
902*4882a593Smuzhiyun #endif
903*4882a593Smuzhiyun 
904*4882a593Smuzhiyun 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_PANIC)
905*4882a593Smuzhiyun 		set_opt(opts.s_mount_opt, ERRORS_PANIC);
906*4882a593Smuzhiyun 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_CONTINUE)
907*4882a593Smuzhiyun 		set_opt(opts.s_mount_opt, ERRORS_CONT);
908*4882a593Smuzhiyun 	else
909*4882a593Smuzhiyun 		set_opt(opts.s_mount_opt, ERRORS_RO);
910*4882a593Smuzhiyun 
911*4882a593Smuzhiyun 	opts.s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
912*4882a593Smuzhiyun 	opts.s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
913*4882a593Smuzhiyun 
914*4882a593Smuzhiyun 	set_opt(opts.s_mount_opt, RESERVATION);
915*4882a593Smuzhiyun 
916*4882a593Smuzhiyun 	if (!parse_options((char *) data, sb, &opts))
917*4882a593Smuzhiyun 		goto failed_mount;
918*4882a593Smuzhiyun 
919*4882a593Smuzhiyun 	sbi->s_mount_opt = opts.s_mount_opt;
920*4882a593Smuzhiyun 	sbi->s_resuid = opts.s_resuid;
921*4882a593Smuzhiyun 	sbi->s_resgid = opts.s_resgid;
922*4882a593Smuzhiyun 
923*4882a593Smuzhiyun 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
924*4882a593Smuzhiyun 		(test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
925*4882a593Smuzhiyun 	sb->s_iflags |= SB_I_CGROUPWB;
926*4882a593Smuzhiyun 
927*4882a593Smuzhiyun 	if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV &&
928*4882a593Smuzhiyun 	    (EXT2_HAS_COMPAT_FEATURE(sb, ~0U) ||
929*4882a593Smuzhiyun 	     EXT2_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
930*4882a593Smuzhiyun 	     EXT2_HAS_INCOMPAT_FEATURE(sb, ~0U)))
931*4882a593Smuzhiyun 		ext2_msg(sb, KERN_WARNING,
932*4882a593Smuzhiyun 			"warning: feature flags set on rev 0 fs, "
933*4882a593Smuzhiyun 			"running e2fsck is recommended");
934*4882a593Smuzhiyun 	/*
935*4882a593Smuzhiyun 	 * Check feature flags regardless of the revision level, since we
936*4882a593Smuzhiyun 	 * previously didn't change the revision level when setting the flags,
937*4882a593Smuzhiyun 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
938*4882a593Smuzhiyun 	 */
939*4882a593Smuzhiyun 	features = EXT2_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP);
940*4882a593Smuzhiyun 	if (features) {
941*4882a593Smuzhiyun 		ext2_msg(sb, KERN_ERR,	"error: couldn't mount because of "
942*4882a593Smuzhiyun 		       "unsupported optional features (%x)",
943*4882a593Smuzhiyun 			le32_to_cpu(features));
944*4882a593Smuzhiyun 		goto failed_mount;
945*4882a593Smuzhiyun 	}
946*4882a593Smuzhiyun 	if (!sb_rdonly(sb) && (features = EXT2_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))){
947*4882a593Smuzhiyun 		ext2_msg(sb, KERN_ERR, "error: couldn't mount RDWR because of "
948*4882a593Smuzhiyun 		       "unsupported optional features (%x)",
949*4882a593Smuzhiyun 		       le32_to_cpu(features));
950*4882a593Smuzhiyun 		goto failed_mount;
951*4882a593Smuzhiyun 	}
952*4882a593Smuzhiyun 
953*4882a593Smuzhiyun 	blocksize = BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
954*4882a593Smuzhiyun 
955*4882a593Smuzhiyun 	if (test_opt(sb, DAX)) {
956*4882a593Smuzhiyun 		if (!bdev_dax_supported(sb->s_bdev, blocksize)) {
957*4882a593Smuzhiyun 			ext2_msg(sb, KERN_ERR,
958*4882a593Smuzhiyun 				"DAX unsupported by block device. Turning off DAX.");
959*4882a593Smuzhiyun 			clear_opt(sbi->s_mount_opt, DAX);
960*4882a593Smuzhiyun 		}
961*4882a593Smuzhiyun 	}
962*4882a593Smuzhiyun 
963*4882a593Smuzhiyun 	/* If the blocksize doesn't match, re-read the thing.. */
964*4882a593Smuzhiyun 	if (sb->s_blocksize != blocksize) {
965*4882a593Smuzhiyun 		brelse(bh);
966*4882a593Smuzhiyun 
967*4882a593Smuzhiyun 		if (!sb_set_blocksize(sb, blocksize)) {
968*4882a593Smuzhiyun 			ext2_msg(sb, KERN_ERR,
969*4882a593Smuzhiyun 				"error: bad blocksize %d", blocksize);
970*4882a593Smuzhiyun 			goto failed_sbi;
971*4882a593Smuzhiyun 		}
972*4882a593Smuzhiyun 
973*4882a593Smuzhiyun 		logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
974*4882a593Smuzhiyun 		offset = (sb_block*BLOCK_SIZE) % blocksize;
975*4882a593Smuzhiyun 		bh = sb_bread(sb, logic_sb_block);
976*4882a593Smuzhiyun 		if(!bh) {
977*4882a593Smuzhiyun 			ext2_msg(sb, KERN_ERR, "error: couldn't read"
978*4882a593Smuzhiyun 				"superblock on 2nd try");
979*4882a593Smuzhiyun 			goto failed_sbi;
980*4882a593Smuzhiyun 		}
981*4882a593Smuzhiyun 		es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
982*4882a593Smuzhiyun 		sbi->s_es = es;
983*4882a593Smuzhiyun 		if (es->s_magic != cpu_to_le16(EXT2_SUPER_MAGIC)) {
984*4882a593Smuzhiyun 			ext2_msg(sb, KERN_ERR, "error: magic mismatch");
985*4882a593Smuzhiyun 			goto failed_mount;
986*4882a593Smuzhiyun 		}
987*4882a593Smuzhiyun 	}
988*4882a593Smuzhiyun 
989*4882a593Smuzhiyun 	sb->s_maxbytes = ext2_max_size(sb->s_blocksize_bits);
990*4882a593Smuzhiyun 	sb->s_max_links = EXT2_LINK_MAX;
991*4882a593Smuzhiyun 	sb->s_time_min = S32_MIN;
992*4882a593Smuzhiyun 	sb->s_time_max = S32_MAX;
993*4882a593Smuzhiyun 
994*4882a593Smuzhiyun 	if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV) {
995*4882a593Smuzhiyun 		sbi->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
996*4882a593Smuzhiyun 		sbi->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
997*4882a593Smuzhiyun 	} else {
998*4882a593Smuzhiyun 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
999*4882a593Smuzhiyun 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
1000*4882a593Smuzhiyun 		if ((sbi->s_inode_size < EXT2_GOOD_OLD_INODE_SIZE) ||
1001*4882a593Smuzhiyun 		    !is_power_of_2(sbi->s_inode_size) ||
1002*4882a593Smuzhiyun 		    (sbi->s_inode_size > blocksize)) {
1003*4882a593Smuzhiyun 			ext2_msg(sb, KERN_ERR,
1004*4882a593Smuzhiyun 				"error: unsupported inode size: %d",
1005*4882a593Smuzhiyun 				sbi->s_inode_size);
1006*4882a593Smuzhiyun 			goto failed_mount;
1007*4882a593Smuzhiyun 		}
1008*4882a593Smuzhiyun 	}
1009*4882a593Smuzhiyun 
1010*4882a593Smuzhiyun 	sbi->s_frag_size = EXT2_MIN_FRAG_SIZE <<
1011*4882a593Smuzhiyun 				   le32_to_cpu(es->s_log_frag_size);
1012*4882a593Smuzhiyun 	if (sbi->s_frag_size == 0)
1013*4882a593Smuzhiyun 		goto cantfind_ext2;
1014*4882a593Smuzhiyun 	sbi->s_frags_per_block = sb->s_blocksize / sbi->s_frag_size;
1015*4882a593Smuzhiyun 
1016*4882a593Smuzhiyun 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
1017*4882a593Smuzhiyun 	sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
1018*4882a593Smuzhiyun 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
1019*4882a593Smuzhiyun 
1020*4882a593Smuzhiyun 	sbi->s_inodes_per_block = sb->s_blocksize / EXT2_INODE_SIZE(sb);
1021*4882a593Smuzhiyun 	if (sbi->s_inodes_per_block == 0 || sbi->s_inodes_per_group == 0)
1022*4882a593Smuzhiyun 		goto cantfind_ext2;
1023*4882a593Smuzhiyun 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
1024*4882a593Smuzhiyun 					sbi->s_inodes_per_block;
1025*4882a593Smuzhiyun 	sbi->s_desc_per_block = sb->s_blocksize /
1026*4882a593Smuzhiyun 					sizeof (struct ext2_group_desc);
1027*4882a593Smuzhiyun 	sbi->s_sbh = bh;
1028*4882a593Smuzhiyun 	sbi->s_mount_state = le16_to_cpu(es->s_state);
1029*4882a593Smuzhiyun 	sbi->s_addr_per_block_bits =
1030*4882a593Smuzhiyun 		ilog2 (EXT2_ADDR_PER_BLOCK(sb));
1031*4882a593Smuzhiyun 	sbi->s_desc_per_block_bits =
1032*4882a593Smuzhiyun 		ilog2 (EXT2_DESC_PER_BLOCK(sb));
1033*4882a593Smuzhiyun 
1034*4882a593Smuzhiyun 	if (sb->s_magic != EXT2_SUPER_MAGIC)
1035*4882a593Smuzhiyun 		goto cantfind_ext2;
1036*4882a593Smuzhiyun 
1037*4882a593Smuzhiyun 	if (sb->s_blocksize != bh->b_size) {
1038*4882a593Smuzhiyun 		if (!silent)
1039*4882a593Smuzhiyun 			ext2_msg(sb, KERN_ERR, "error: unsupported blocksize");
1040*4882a593Smuzhiyun 		goto failed_mount;
1041*4882a593Smuzhiyun 	}
1042*4882a593Smuzhiyun 
1043*4882a593Smuzhiyun 	if (sb->s_blocksize != sbi->s_frag_size) {
1044*4882a593Smuzhiyun 		ext2_msg(sb, KERN_ERR,
1045*4882a593Smuzhiyun 			"error: fragsize %lu != blocksize %lu"
1046*4882a593Smuzhiyun 			"(not supported yet)",
1047*4882a593Smuzhiyun 			sbi->s_frag_size, sb->s_blocksize);
1048*4882a593Smuzhiyun 		goto failed_mount;
1049*4882a593Smuzhiyun 	}
1050*4882a593Smuzhiyun 
1051*4882a593Smuzhiyun 	if (sbi->s_blocks_per_group > sb->s_blocksize * 8) {
1052*4882a593Smuzhiyun 		ext2_msg(sb, KERN_ERR,
1053*4882a593Smuzhiyun 			"error: #blocks per group too big: %lu",
1054*4882a593Smuzhiyun 			sbi->s_blocks_per_group);
1055*4882a593Smuzhiyun 		goto failed_mount;
1056*4882a593Smuzhiyun 	}
1057*4882a593Smuzhiyun 	if (sbi->s_frags_per_group > sb->s_blocksize * 8) {
1058*4882a593Smuzhiyun 		ext2_msg(sb, KERN_ERR,
1059*4882a593Smuzhiyun 			"error: #fragments per group too big: %lu",
1060*4882a593Smuzhiyun 			sbi->s_frags_per_group);
1061*4882a593Smuzhiyun 		goto failed_mount;
1062*4882a593Smuzhiyun 	}
1063*4882a593Smuzhiyun 	if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
1064*4882a593Smuzhiyun 	    sbi->s_inodes_per_group > sb->s_blocksize * 8) {
1065*4882a593Smuzhiyun 		ext2_msg(sb, KERN_ERR,
1066*4882a593Smuzhiyun 			"error: invalid #inodes per group: %lu",
1067*4882a593Smuzhiyun 			sbi->s_inodes_per_group);
1068*4882a593Smuzhiyun 		goto failed_mount;
1069*4882a593Smuzhiyun 	}
1070*4882a593Smuzhiyun 
1071*4882a593Smuzhiyun 	if (EXT2_BLOCKS_PER_GROUP(sb) == 0)
1072*4882a593Smuzhiyun 		goto cantfind_ext2;
1073*4882a593Smuzhiyun 	sbi->s_groups_count = ((le32_to_cpu(es->s_blocks_count) -
1074*4882a593Smuzhiyun 				le32_to_cpu(es->s_first_data_block) - 1)
1075*4882a593Smuzhiyun 					/ EXT2_BLOCKS_PER_GROUP(sb)) + 1;
1076*4882a593Smuzhiyun 	if ((u64)sbi->s_groups_count * sbi->s_inodes_per_group !=
1077*4882a593Smuzhiyun 	    le32_to_cpu(es->s_inodes_count)) {
1078*4882a593Smuzhiyun 		ext2_msg(sb, KERN_ERR, "error: invalid #inodes: %u vs computed %llu",
1079*4882a593Smuzhiyun 			 le32_to_cpu(es->s_inodes_count),
1080*4882a593Smuzhiyun 			 (u64)sbi->s_groups_count * sbi->s_inodes_per_group);
1081*4882a593Smuzhiyun 		goto failed_mount;
1082*4882a593Smuzhiyun 	}
1083*4882a593Smuzhiyun 	db_count = (sbi->s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
1084*4882a593Smuzhiyun 		   EXT2_DESC_PER_BLOCK(sb);
1085*4882a593Smuzhiyun 	sbi->s_group_desc = kmalloc_array (db_count,
1086*4882a593Smuzhiyun 					   sizeof(struct buffer_head *),
1087*4882a593Smuzhiyun 					   GFP_KERNEL);
1088*4882a593Smuzhiyun 	if (sbi->s_group_desc == NULL) {
1089*4882a593Smuzhiyun 		ret = -ENOMEM;
1090*4882a593Smuzhiyun 		ext2_msg(sb, KERN_ERR, "error: not enough memory");
1091*4882a593Smuzhiyun 		goto failed_mount;
1092*4882a593Smuzhiyun 	}
1093*4882a593Smuzhiyun 	bgl_lock_init(sbi->s_blockgroup_lock);
1094*4882a593Smuzhiyun 	sbi->s_debts = kcalloc(sbi->s_groups_count, sizeof(*sbi->s_debts), GFP_KERNEL);
1095*4882a593Smuzhiyun 	if (!sbi->s_debts) {
1096*4882a593Smuzhiyun 		ret = -ENOMEM;
1097*4882a593Smuzhiyun 		ext2_msg(sb, KERN_ERR, "error: not enough memory");
1098*4882a593Smuzhiyun 		goto failed_mount_group_desc;
1099*4882a593Smuzhiyun 	}
1100*4882a593Smuzhiyun 	for (i = 0; i < db_count; i++) {
1101*4882a593Smuzhiyun 		block = descriptor_loc(sb, logic_sb_block, i);
1102*4882a593Smuzhiyun 		sbi->s_group_desc[i] = sb_bread(sb, block);
1103*4882a593Smuzhiyun 		if (!sbi->s_group_desc[i]) {
1104*4882a593Smuzhiyun 			for (j = 0; j < i; j++)
1105*4882a593Smuzhiyun 				brelse (sbi->s_group_desc[j]);
1106*4882a593Smuzhiyun 			ext2_msg(sb, KERN_ERR,
1107*4882a593Smuzhiyun 				"error: unable to read group descriptors");
1108*4882a593Smuzhiyun 			goto failed_mount_group_desc;
1109*4882a593Smuzhiyun 		}
1110*4882a593Smuzhiyun 	}
1111*4882a593Smuzhiyun 	if (!ext2_check_descriptors (sb)) {
1112*4882a593Smuzhiyun 		ext2_msg(sb, KERN_ERR, "group descriptors corrupted");
1113*4882a593Smuzhiyun 		goto failed_mount2;
1114*4882a593Smuzhiyun 	}
1115*4882a593Smuzhiyun 	sbi->s_gdb_count = db_count;
1116*4882a593Smuzhiyun 	get_random_bytes(&sbi->s_next_generation, sizeof(u32));
1117*4882a593Smuzhiyun 	spin_lock_init(&sbi->s_next_gen_lock);
1118*4882a593Smuzhiyun 
1119*4882a593Smuzhiyun 	/* per fileystem reservation list head & lock */
1120*4882a593Smuzhiyun 	spin_lock_init(&sbi->s_rsv_window_lock);
1121*4882a593Smuzhiyun 	sbi->s_rsv_window_root = RB_ROOT;
1122*4882a593Smuzhiyun 	/*
1123*4882a593Smuzhiyun 	 * Add a single, static dummy reservation to the start of the
1124*4882a593Smuzhiyun 	 * reservation window list --- it gives us a placeholder for
1125*4882a593Smuzhiyun 	 * append-at-start-of-list which makes the allocation logic
1126*4882a593Smuzhiyun 	 * _much_ simpler.
1127*4882a593Smuzhiyun 	 */
1128*4882a593Smuzhiyun 	sbi->s_rsv_window_head.rsv_start = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
1129*4882a593Smuzhiyun 	sbi->s_rsv_window_head.rsv_end = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
1130*4882a593Smuzhiyun 	sbi->s_rsv_window_head.rsv_alloc_hit = 0;
1131*4882a593Smuzhiyun 	sbi->s_rsv_window_head.rsv_goal_size = 0;
1132*4882a593Smuzhiyun 	ext2_rsv_window_add(sb, &sbi->s_rsv_window_head);
1133*4882a593Smuzhiyun 
1134*4882a593Smuzhiyun 	err = percpu_counter_init(&sbi->s_freeblocks_counter,
1135*4882a593Smuzhiyun 				ext2_count_free_blocks(sb), GFP_KERNEL);
1136*4882a593Smuzhiyun 	if (!err) {
1137*4882a593Smuzhiyun 		err = percpu_counter_init(&sbi->s_freeinodes_counter,
1138*4882a593Smuzhiyun 				ext2_count_free_inodes(sb), GFP_KERNEL);
1139*4882a593Smuzhiyun 	}
1140*4882a593Smuzhiyun 	if (!err) {
1141*4882a593Smuzhiyun 		err = percpu_counter_init(&sbi->s_dirs_counter,
1142*4882a593Smuzhiyun 				ext2_count_dirs(sb), GFP_KERNEL);
1143*4882a593Smuzhiyun 	}
1144*4882a593Smuzhiyun 	if (err) {
1145*4882a593Smuzhiyun 		ret = err;
1146*4882a593Smuzhiyun 		ext2_msg(sb, KERN_ERR, "error: insufficient memory");
1147*4882a593Smuzhiyun 		goto failed_mount3;
1148*4882a593Smuzhiyun 	}
1149*4882a593Smuzhiyun 
1150*4882a593Smuzhiyun #ifdef CONFIG_EXT2_FS_XATTR
1151*4882a593Smuzhiyun 	sbi->s_ea_block_cache = ext2_xattr_create_cache();
1152*4882a593Smuzhiyun 	if (!sbi->s_ea_block_cache) {
1153*4882a593Smuzhiyun 		ret = -ENOMEM;
1154*4882a593Smuzhiyun 		ext2_msg(sb, KERN_ERR, "Failed to create ea_block_cache");
1155*4882a593Smuzhiyun 		goto failed_mount3;
1156*4882a593Smuzhiyun 	}
1157*4882a593Smuzhiyun #endif
1158*4882a593Smuzhiyun 	/*
1159*4882a593Smuzhiyun 	 * set up enough so that it can read an inode
1160*4882a593Smuzhiyun 	 */
1161*4882a593Smuzhiyun 	sb->s_op = &ext2_sops;
1162*4882a593Smuzhiyun 	sb->s_export_op = &ext2_export_ops;
1163*4882a593Smuzhiyun 	sb->s_xattr = ext2_xattr_handlers;
1164*4882a593Smuzhiyun 
1165*4882a593Smuzhiyun #ifdef CONFIG_QUOTA
1166*4882a593Smuzhiyun 	sb->dq_op = &dquot_operations;
1167*4882a593Smuzhiyun 	sb->s_qcop = &ext2_quotactl_ops;
1168*4882a593Smuzhiyun 	sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP;
1169*4882a593Smuzhiyun #endif
1170*4882a593Smuzhiyun 
1171*4882a593Smuzhiyun 	root = ext2_iget(sb, EXT2_ROOT_INO);
1172*4882a593Smuzhiyun 	if (IS_ERR(root)) {
1173*4882a593Smuzhiyun 		ret = PTR_ERR(root);
1174*4882a593Smuzhiyun 		goto failed_mount3;
1175*4882a593Smuzhiyun 	}
1176*4882a593Smuzhiyun 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
1177*4882a593Smuzhiyun 		iput(root);
1178*4882a593Smuzhiyun 		ext2_msg(sb, KERN_ERR, "error: corrupt root inode, run e2fsck");
1179*4882a593Smuzhiyun 		goto failed_mount3;
1180*4882a593Smuzhiyun 	}
1181*4882a593Smuzhiyun 
1182*4882a593Smuzhiyun 	sb->s_root = d_make_root(root);
1183*4882a593Smuzhiyun 	if (!sb->s_root) {
1184*4882a593Smuzhiyun 		ext2_msg(sb, KERN_ERR, "error: get root inode failed");
1185*4882a593Smuzhiyun 		ret = -ENOMEM;
1186*4882a593Smuzhiyun 		goto failed_mount3;
1187*4882a593Smuzhiyun 	}
1188*4882a593Smuzhiyun 	if (EXT2_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL))
1189*4882a593Smuzhiyun 		ext2_msg(sb, KERN_WARNING,
1190*4882a593Smuzhiyun 			"warning: mounting ext3 filesystem as ext2");
1191*4882a593Smuzhiyun 	if (ext2_setup_super (sb, es, sb_rdonly(sb)))
1192*4882a593Smuzhiyun 		sb->s_flags |= SB_RDONLY;
1193*4882a593Smuzhiyun 	ext2_write_super(sb);
1194*4882a593Smuzhiyun 	return 0;
1195*4882a593Smuzhiyun 
1196*4882a593Smuzhiyun cantfind_ext2:
1197*4882a593Smuzhiyun 	if (!silent)
1198*4882a593Smuzhiyun 		ext2_msg(sb, KERN_ERR,
1199*4882a593Smuzhiyun 			"error: can't find an ext2 filesystem on dev %s.",
1200*4882a593Smuzhiyun 			sb->s_id);
1201*4882a593Smuzhiyun 	goto failed_mount;
1202*4882a593Smuzhiyun failed_mount3:
1203*4882a593Smuzhiyun 	ext2_xattr_destroy_cache(sbi->s_ea_block_cache);
1204*4882a593Smuzhiyun 	percpu_counter_destroy(&sbi->s_freeblocks_counter);
1205*4882a593Smuzhiyun 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
1206*4882a593Smuzhiyun 	percpu_counter_destroy(&sbi->s_dirs_counter);
1207*4882a593Smuzhiyun failed_mount2:
1208*4882a593Smuzhiyun 	for (i = 0; i < db_count; i++)
1209*4882a593Smuzhiyun 		brelse(sbi->s_group_desc[i]);
1210*4882a593Smuzhiyun failed_mount_group_desc:
1211*4882a593Smuzhiyun 	kfree(sbi->s_group_desc);
1212*4882a593Smuzhiyun 	kfree(sbi->s_debts);
1213*4882a593Smuzhiyun failed_mount:
1214*4882a593Smuzhiyun 	brelse(bh);
1215*4882a593Smuzhiyun failed_sbi:
1216*4882a593Smuzhiyun 	sb->s_fs_info = NULL;
1217*4882a593Smuzhiyun 	kfree(sbi->s_blockgroup_lock);
1218*4882a593Smuzhiyun 	kfree(sbi);
1219*4882a593Smuzhiyun failed:
1220*4882a593Smuzhiyun 	fs_put_dax(dax_dev);
1221*4882a593Smuzhiyun 	return ret;
1222*4882a593Smuzhiyun }
1223*4882a593Smuzhiyun 
ext2_clear_super_error(struct super_block * sb)1224*4882a593Smuzhiyun static void ext2_clear_super_error(struct super_block *sb)
1225*4882a593Smuzhiyun {
1226*4882a593Smuzhiyun 	struct buffer_head *sbh = EXT2_SB(sb)->s_sbh;
1227*4882a593Smuzhiyun 
1228*4882a593Smuzhiyun 	if (buffer_write_io_error(sbh)) {
1229*4882a593Smuzhiyun 		/*
1230*4882a593Smuzhiyun 		 * Oh, dear.  A previous attempt to write the
1231*4882a593Smuzhiyun 		 * superblock failed.  This could happen because the
1232*4882a593Smuzhiyun 		 * USB device was yanked out.  Or it could happen to
1233*4882a593Smuzhiyun 		 * be a transient write error and maybe the block will
1234*4882a593Smuzhiyun 		 * be remapped.  Nothing we can do but to retry the
1235*4882a593Smuzhiyun 		 * write and hope for the best.
1236*4882a593Smuzhiyun 		 */
1237*4882a593Smuzhiyun 		ext2_msg(sb, KERN_ERR,
1238*4882a593Smuzhiyun 		       "previous I/O error to superblock detected");
1239*4882a593Smuzhiyun 		clear_buffer_write_io_error(sbh);
1240*4882a593Smuzhiyun 		set_buffer_uptodate(sbh);
1241*4882a593Smuzhiyun 	}
1242*4882a593Smuzhiyun }
1243*4882a593Smuzhiyun 
ext2_sync_super(struct super_block * sb,struct ext2_super_block * es,int wait)1244*4882a593Smuzhiyun void ext2_sync_super(struct super_block *sb, struct ext2_super_block *es,
1245*4882a593Smuzhiyun 		     int wait)
1246*4882a593Smuzhiyun {
1247*4882a593Smuzhiyun 	ext2_clear_super_error(sb);
1248*4882a593Smuzhiyun 	spin_lock(&EXT2_SB(sb)->s_lock);
1249*4882a593Smuzhiyun 	es->s_free_blocks_count = cpu_to_le32(ext2_count_free_blocks(sb));
1250*4882a593Smuzhiyun 	es->s_free_inodes_count = cpu_to_le32(ext2_count_free_inodes(sb));
1251*4882a593Smuzhiyun 	es->s_wtime = cpu_to_le32(ktime_get_real_seconds());
1252*4882a593Smuzhiyun 	/* unlock before we do IO */
1253*4882a593Smuzhiyun 	spin_unlock(&EXT2_SB(sb)->s_lock);
1254*4882a593Smuzhiyun 	mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
1255*4882a593Smuzhiyun 	if (wait)
1256*4882a593Smuzhiyun 		sync_dirty_buffer(EXT2_SB(sb)->s_sbh);
1257*4882a593Smuzhiyun }
1258*4882a593Smuzhiyun 
1259*4882a593Smuzhiyun /*
1260*4882a593Smuzhiyun  * In the second extended file system, it is not necessary to
1261*4882a593Smuzhiyun  * write the super block since we use a mapping of the
1262*4882a593Smuzhiyun  * disk super block in a buffer.
1263*4882a593Smuzhiyun  *
1264*4882a593Smuzhiyun  * However, this function is still used to set the fs valid
1265*4882a593Smuzhiyun  * flags to 0.  We need to set this flag to 0 since the fs
1266*4882a593Smuzhiyun  * may have been checked while mounted and e2fsck may have
1267*4882a593Smuzhiyun  * set s_state to EXT2_VALID_FS after some corrections.
1268*4882a593Smuzhiyun  */
ext2_sync_fs(struct super_block * sb,int wait)1269*4882a593Smuzhiyun static int ext2_sync_fs(struct super_block *sb, int wait)
1270*4882a593Smuzhiyun {
1271*4882a593Smuzhiyun 	struct ext2_sb_info *sbi = EXT2_SB(sb);
1272*4882a593Smuzhiyun 	struct ext2_super_block *es = EXT2_SB(sb)->s_es;
1273*4882a593Smuzhiyun 
1274*4882a593Smuzhiyun 	/*
1275*4882a593Smuzhiyun 	 * Write quota structures to quota file, sync_blockdev() will write
1276*4882a593Smuzhiyun 	 * them to disk later
1277*4882a593Smuzhiyun 	 */
1278*4882a593Smuzhiyun 	dquot_writeback_dquots(sb, -1);
1279*4882a593Smuzhiyun 
1280*4882a593Smuzhiyun 	spin_lock(&sbi->s_lock);
1281*4882a593Smuzhiyun 	if (es->s_state & cpu_to_le16(EXT2_VALID_FS)) {
1282*4882a593Smuzhiyun 		ext2_debug("setting valid to 0\n");
1283*4882a593Smuzhiyun 		es->s_state &= cpu_to_le16(~EXT2_VALID_FS);
1284*4882a593Smuzhiyun 	}
1285*4882a593Smuzhiyun 	spin_unlock(&sbi->s_lock);
1286*4882a593Smuzhiyun 	ext2_sync_super(sb, es, wait);
1287*4882a593Smuzhiyun 	return 0;
1288*4882a593Smuzhiyun }
1289*4882a593Smuzhiyun 
ext2_freeze(struct super_block * sb)1290*4882a593Smuzhiyun static int ext2_freeze(struct super_block *sb)
1291*4882a593Smuzhiyun {
1292*4882a593Smuzhiyun 	struct ext2_sb_info *sbi = EXT2_SB(sb);
1293*4882a593Smuzhiyun 
1294*4882a593Smuzhiyun 	/*
1295*4882a593Smuzhiyun 	 * Open but unlinked files present? Keep EXT2_VALID_FS flag cleared
1296*4882a593Smuzhiyun 	 * because we have unattached inodes and thus filesystem is not fully
1297*4882a593Smuzhiyun 	 * consistent.
1298*4882a593Smuzhiyun 	 */
1299*4882a593Smuzhiyun 	if (atomic_long_read(&sb->s_remove_count)) {
1300*4882a593Smuzhiyun 		ext2_sync_fs(sb, 1);
1301*4882a593Smuzhiyun 		return 0;
1302*4882a593Smuzhiyun 	}
1303*4882a593Smuzhiyun 	/* Set EXT2_FS_VALID flag */
1304*4882a593Smuzhiyun 	spin_lock(&sbi->s_lock);
1305*4882a593Smuzhiyun 	sbi->s_es->s_state = cpu_to_le16(sbi->s_mount_state);
1306*4882a593Smuzhiyun 	spin_unlock(&sbi->s_lock);
1307*4882a593Smuzhiyun 	ext2_sync_super(sb, sbi->s_es, 1);
1308*4882a593Smuzhiyun 
1309*4882a593Smuzhiyun 	return 0;
1310*4882a593Smuzhiyun }
1311*4882a593Smuzhiyun 
ext2_unfreeze(struct super_block * sb)1312*4882a593Smuzhiyun static int ext2_unfreeze(struct super_block *sb)
1313*4882a593Smuzhiyun {
1314*4882a593Smuzhiyun 	/* Just write sb to clear EXT2_VALID_FS flag */
1315*4882a593Smuzhiyun 	ext2_write_super(sb);
1316*4882a593Smuzhiyun 
1317*4882a593Smuzhiyun 	return 0;
1318*4882a593Smuzhiyun }
1319*4882a593Smuzhiyun 
ext2_write_super(struct super_block * sb)1320*4882a593Smuzhiyun static void ext2_write_super(struct super_block *sb)
1321*4882a593Smuzhiyun {
1322*4882a593Smuzhiyun 	if (!sb_rdonly(sb))
1323*4882a593Smuzhiyun 		ext2_sync_fs(sb, 1);
1324*4882a593Smuzhiyun }
1325*4882a593Smuzhiyun 
ext2_remount(struct super_block * sb,int * flags,char * data)1326*4882a593Smuzhiyun static int ext2_remount (struct super_block * sb, int * flags, char * data)
1327*4882a593Smuzhiyun {
1328*4882a593Smuzhiyun 	struct ext2_sb_info * sbi = EXT2_SB(sb);
1329*4882a593Smuzhiyun 	struct ext2_super_block * es;
1330*4882a593Smuzhiyun 	struct ext2_mount_options new_opts;
1331*4882a593Smuzhiyun 	int err;
1332*4882a593Smuzhiyun 
1333*4882a593Smuzhiyun 	sync_filesystem(sb);
1334*4882a593Smuzhiyun 
1335*4882a593Smuzhiyun 	spin_lock(&sbi->s_lock);
1336*4882a593Smuzhiyun 	new_opts.s_mount_opt = sbi->s_mount_opt;
1337*4882a593Smuzhiyun 	new_opts.s_resuid = sbi->s_resuid;
1338*4882a593Smuzhiyun 	new_opts.s_resgid = sbi->s_resgid;
1339*4882a593Smuzhiyun 	spin_unlock(&sbi->s_lock);
1340*4882a593Smuzhiyun 
1341*4882a593Smuzhiyun 	if (!parse_options(data, sb, &new_opts))
1342*4882a593Smuzhiyun 		return -EINVAL;
1343*4882a593Smuzhiyun 
1344*4882a593Smuzhiyun 	spin_lock(&sbi->s_lock);
1345*4882a593Smuzhiyun 	es = sbi->s_es;
1346*4882a593Smuzhiyun 	if ((sbi->s_mount_opt ^ new_opts.s_mount_opt) & EXT2_MOUNT_DAX) {
1347*4882a593Smuzhiyun 		ext2_msg(sb, KERN_WARNING, "warning: refusing change of "
1348*4882a593Smuzhiyun 			 "dax flag with busy inodes while remounting");
1349*4882a593Smuzhiyun 		new_opts.s_mount_opt ^= EXT2_MOUNT_DAX;
1350*4882a593Smuzhiyun 	}
1351*4882a593Smuzhiyun 	if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
1352*4882a593Smuzhiyun 		goto out_set;
1353*4882a593Smuzhiyun 	if (*flags & SB_RDONLY) {
1354*4882a593Smuzhiyun 		if (le16_to_cpu(es->s_state) & EXT2_VALID_FS ||
1355*4882a593Smuzhiyun 		    !(sbi->s_mount_state & EXT2_VALID_FS))
1356*4882a593Smuzhiyun 			goto out_set;
1357*4882a593Smuzhiyun 
1358*4882a593Smuzhiyun 		/*
1359*4882a593Smuzhiyun 		 * OK, we are remounting a valid rw partition rdonly, so set
1360*4882a593Smuzhiyun 		 * the rdonly flag and then mark the partition as valid again.
1361*4882a593Smuzhiyun 		 */
1362*4882a593Smuzhiyun 		es->s_state = cpu_to_le16(sbi->s_mount_state);
1363*4882a593Smuzhiyun 		es->s_mtime = cpu_to_le32(ktime_get_real_seconds());
1364*4882a593Smuzhiyun 		spin_unlock(&sbi->s_lock);
1365*4882a593Smuzhiyun 
1366*4882a593Smuzhiyun 		err = dquot_suspend(sb, -1);
1367*4882a593Smuzhiyun 		if (err < 0)
1368*4882a593Smuzhiyun 			return err;
1369*4882a593Smuzhiyun 
1370*4882a593Smuzhiyun 		ext2_sync_super(sb, es, 1);
1371*4882a593Smuzhiyun 	} else {
1372*4882a593Smuzhiyun 		__le32 ret = EXT2_HAS_RO_COMPAT_FEATURE(sb,
1373*4882a593Smuzhiyun 					       ~EXT2_FEATURE_RO_COMPAT_SUPP);
1374*4882a593Smuzhiyun 		if (ret) {
1375*4882a593Smuzhiyun 			spin_unlock(&sbi->s_lock);
1376*4882a593Smuzhiyun 			ext2_msg(sb, KERN_WARNING,
1377*4882a593Smuzhiyun 				"warning: couldn't remount RDWR because of "
1378*4882a593Smuzhiyun 				"unsupported optional features (%x).",
1379*4882a593Smuzhiyun 				le32_to_cpu(ret));
1380*4882a593Smuzhiyun 			return -EROFS;
1381*4882a593Smuzhiyun 		}
1382*4882a593Smuzhiyun 		/*
1383*4882a593Smuzhiyun 		 * Mounting a RDONLY partition read-write, so reread and
1384*4882a593Smuzhiyun 		 * store the current valid flag.  (It may have been changed
1385*4882a593Smuzhiyun 		 * by e2fsck since we originally mounted the partition.)
1386*4882a593Smuzhiyun 		 */
1387*4882a593Smuzhiyun 		sbi->s_mount_state = le16_to_cpu(es->s_state);
1388*4882a593Smuzhiyun 		if (!ext2_setup_super (sb, es, 0))
1389*4882a593Smuzhiyun 			sb->s_flags &= ~SB_RDONLY;
1390*4882a593Smuzhiyun 		spin_unlock(&sbi->s_lock);
1391*4882a593Smuzhiyun 
1392*4882a593Smuzhiyun 		ext2_write_super(sb);
1393*4882a593Smuzhiyun 
1394*4882a593Smuzhiyun 		dquot_resume(sb, -1);
1395*4882a593Smuzhiyun 	}
1396*4882a593Smuzhiyun 
1397*4882a593Smuzhiyun 	spin_lock(&sbi->s_lock);
1398*4882a593Smuzhiyun out_set:
1399*4882a593Smuzhiyun 	sbi->s_mount_opt = new_opts.s_mount_opt;
1400*4882a593Smuzhiyun 	sbi->s_resuid = new_opts.s_resuid;
1401*4882a593Smuzhiyun 	sbi->s_resgid = new_opts.s_resgid;
1402*4882a593Smuzhiyun 	sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
1403*4882a593Smuzhiyun 		(test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
1404*4882a593Smuzhiyun 	spin_unlock(&sbi->s_lock);
1405*4882a593Smuzhiyun 
1406*4882a593Smuzhiyun 	return 0;
1407*4882a593Smuzhiyun }
1408*4882a593Smuzhiyun 
ext2_statfs(struct dentry * dentry,struct kstatfs * buf)1409*4882a593Smuzhiyun static int ext2_statfs (struct dentry * dentry, struct kstatfs * buf)
1410*4882a593Smuzhiyun {
1411*4882a593Smuzhiyun 	struct super_block *sb = dentry->d_sb;
1412*4882a593Smuzhiyun 	struct ext2_sb_info *sbi = EXT2_SB(sb);
1413*4882a593Smuzhiyun 	struct ext2_super_block *es = sbi->s_es;
1414*4882a593Smuzhiyun 	u64 fsid;
1415*4882a593Smuzhiyun 
1416*4882a593Smuzhiyun 	spin_lock(&sbi->s_lock);
1417*4882a593Smuzhiyun 
1418*4882a593Smuzhiyun 	if (test_opt (sb, MINIX_DF))
1419*4882a593Smuzhiyun 		sbi->s_overhead_last = 0;
1420*4882a593Smuzhiyun 	else if (sbi->s_blocks_last != le32_to_cpu(es->s_blocks_count)) {
1421*4882a593Smuzhiyun 		unsigned long i, overhead = 0;
1422*4882a593Smuzhiyun 		smp_rmb();
1423*4882a593Smuzhiyun 
1424*4882a593Smuzhiyun 		/*
1425*4882a593Smuzhiyun 		 * Compute the overhead (FS structures). This is constant
1426*4882a593Smuzhiyun 		 * for a given filesystem unless the number of block groups
1427*4882a593Smuzhiyun 		 * changes so we cache the previous value until it does.
1428*4882a593Smuzhiyun 		 */
1429*4882a593Smuzhiyun 
1430*4882a593Smuzhiyun 		/*
1431*4882a593Smuzhiyun 		 * All of the blocks before first_data_block are
1432*4882a593Smuzhiyun 		 * overhead
1433*4882a593Smuzhiyun 		 */
1434*4882a593Smuzhiyun 		overhead = le32_to_cpu(es->s_first_data_block);
1435*4882a593Smuzhiyun 
1436*4882a593Smuzhiyun 		/*
1437*4882a593Smuzhiyun 		 * Add the overhead attributed to the superblock and
1438*4882a593Smuzhiyun 		 * block group descriptors.  If the sparse superblocks
1439*4882a593Smuzhiyun 		 * feature is turned on, then not all groups have this.
1440*4882a593Smuzhiyun 		 */
1441*4882a593Smuzhiyun 		for (i = 0; i < sbi->s_groups_count; i++)
1442*4882a593Smuzhiyun 			overhead += ext2_bg_has_super(sb, i) +
1443*4882a593Smuzhiyun 				ext2_bg_num_gdb(sb, i);
1444*4882a593Smuzhiyun 
1445*4882a593Smuzhiyun 		/*
1446*4882a593Smuzhiyun 		 * Every block group has an inode bitmap, a block
1447*4882a593Smuzhiyun 		 * bitmap, and an inode table.
1448*4882a593Smuzhiyun 		 */
1449*4882a593Smuzhiyun 		overhead += (sbi->s_groups_count *
1450*4882a593Smuzhiyun 			     (2 + sbi->s_itb_per_group));
1451*4882a593Smuzhiyun 		sbi->s_overhead_last = overhead;
1452*4882a593Smuzhiyun 		smp_wmb();
1453*4882a593Smuzhiyun 		sbi->s_blocks_last = le32_to_cpu(es->s_blocks_count);
1454*4882a593Smuzhiyun 	}
1455*4882a593Smuzhiyun 
1456*4882a593Smuzhiyun 	buf->f_type = EXT2_SUPER_MAGIC;
1457*4882a593Smuzhiyun 	buf->f_bsize = sb->s_blocksize;
1458*4882a593Smuzhiyun 	buf->f_blocks = le32_to_cpu(es->s_blocks_count) - sbi->s_overhead_last;
1459*4882a593Smuzhiyun 	buf->f_bfree = ext2_count_free_blocks(sb);
1460*4882a593Smuzhiyun 	es->s_free_blocks_count = cpu_to_le32(buf->f_bfree);
1461*4882a593Smuzhiyun 	buf->f_bavail = buf->f_bfree - le32_to_cpu(es->s_r_blocks_count);
1462*4882a593Smuzhiyun 	if (buf->f_bfree < le32_to_cpu(es->s_r_blocks_count))
1463*4882a593Smuzhiyun 		buf->f_bavail = 0;
1464*4882a593Smuzhiyun 	buf->f_files = le32_to_cpu(es->s_inodes_count);
1465*4882a593Smuzhiyun 	buf->f_ffree = ext2_count_free_inodes(sb);
1466*4882a593Smuzhiyun 	es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
1467*4882a593Smuzhiyun 	buf->f_namelen = EXT2_NAME_LEN;
1468*4882a593Smuzhiyun 	fsid = le64_to_cpup((void *)es->s_uuid) ^
1469*4882a593Smuzhiyun 	       le64_to_cpup((void *)es->s_uuid + sizeof(u64));
1470*4882a593Smuzhiyun 	buf->f_fsid = u64_to_fsid(fsid);
1471*4882a593Smuzhiyun 	spin_unlock(&sbi->s_lock);
1472*4882a593Smuzhiyun 	return 0;
1473*4882a593Smuzhiyun }
1474*4882a593Smuzhiyun 
ext2_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * data)1475*4882a593Smuzhiyun static struct dentry *ext2_mount(struct file_system_type *fs_type,
1476*4882a593Smuzhiyun 	int flags, const char *dev_name, void *data)
1477*4882a593Smuzhiyun {
1478*4882a593Smuzhiyun 	return mount_bdev(fs_type, flags, dev_name, data, ext2_fill_super);
1479*4882a593Smuzhiyun }
1480*4882a593Smuzhiyun 
1481*4882a593Smuzhiyun #ifdef CONFIG_QUOTA
1482*4882a593Smuzhiyun 
1483*4882a593Smuzhiyun /* Read data from quotafile - avoid pagecache and such because we cannot afford
1484*4882a593Smuzhiyun  * acquiring the locks... As quota files are never truncated and quota code
1485*4882a593Smuzhiyun  * itself serializes the operations (and no one else should touch the files)
1486*4882a593Smuzhiyun  * we don't have to be afraid of races */
ext2_quota_read(struct super_block * sb,int type,char * data,size_t len,loff_t off)1487*4882a593Smuzhiyun static ssize_t ext2_quota_read(struct super_block *sb, int type, char *data,
1488*4882a593Smuzhiyun 			       size_t len, loff_t off)
1489*4882a593Smuzhiyun {
1490*4882a593Smuzhiyun 	struct inode *inode = sb_dqopt(sb)->files[type];
1491*4882a593Smuzhiyun 	sector_t blk = off >> EXT2_BLOCK_SIZE_BITS(sb);
1492*4882a593Smuzhiyun 	int err = 0;
1493*4882a593Smuzhiyun 	int offset = off & (sb->s_blocksize - 1);
1494*4882a593Smuzhiyun 	int tocopy;
1495*4882a593Smuzhiyun 	size_t toread;
1496*4882a593Smuzhiyun 	struct buffer_head tmp_bh;
1497*4882a593Smuzhiyun 	struct buffer_head *bh;
1498*4882a593Smuzhiyun 	loff_t i_size = i_size_read(inode);
1499*4882a593Smuzhiyun 
1500*4882a593Smuzhiyun 	if (off > i_size)
1501*4882a593Smuzhiyun 		return 0;
1502*4882a593Smuzhiyun 	if (off+len > i_size)
1503*4882a593Smuzhiyun 		len = i_size-off;
1504*4882a593Smuzhiyun 	toread = len;
1505*4882a593Smuzhiyun 	while (toread > 0) {
1506*4882a593Smuzhiyun 		tocopy = sb->s_blocksize - offset < toread ?
1507*4882a593Smuzhiyun 				sb->s_blocksize - offset : toread;
1508*4882a593Smuzhiyun 
1509*4882a593Smuzhiyun 		tmp_bh.b_state = 0;
1510*4882a593Smuzhiyun 		tmp_bh.b_size = sb->s_blocksize;
1511*4882a593Smuzhiyun 		err = ext2_get_block(inode, blk, &tmp_bh, 0);
1512*4882a593Smuzhiyun 		if (err < 0)
1513*4882a593Smuzhiyun 			return err;
1514*4882a593Smuzhiyun 		if (!buffer_mapped(&tmp_bh))	/* A hole? */
1515*4882a593Smuzhiyun 			memset(data, 0, tocopy);
1516*4882a593Smuzhiyun 		else {
1517*4882a593Smuzhiyun 			bh = sb_bread(sb, tmp_bh.b_blocknr);
1518*4882a593Smuzhiyun 			if (!bh)
1519*4882a593Smuzhiyun 				return -EIO;
1520*4882a593Smuzhiyun 			memcpy(data, bh->b_data+offset, tocopy);
1521*4882a593Smuzhiyun 			brelse(bh);
1522*4882a593Smuzhiyun 		}
1523*4882a593Smuzhiyun 		offset = 0;
1524*4882a593Smuzhiyun 		toread -= tocopy;
1525*4882a593Smuzhiyun 		data += tocopy;
1526*4882a593Smuzhiyun 		blk++;
1527*4882a593Smuzhiyun 	}
1528*4882a593Smuzhiyun 	return len;
1529*4882a593Smuzhiyun }
1530*4882a593Smuzhiyun 
1531*4882a593Smuzhiyun /* Write to quotafile */
ext2_quota_write(struct super_block * sb,int type,const char * data,size_t len,loff_t off)1532*4882a593Smuzhiyun static ssize_t ext2_quota_write(struct super_block *sb, int type,
1533*4882a593Smuzhiyun 				const char *data, size_t len, loff_t off)
1534*4882a593Smuzhiyun {
1535*4882a593Smuzhiyun 	struct inode *inode = sb_dqopt(sb)->files[type];
1536*4882a593Smuzhiyun 	sector_t blk = off >> EXT2_BLOCK_SIZE_BITS(sb);
1537*4882a593Smuzhiyun 	int err = 0;
1538*4882a593Smuzhiyun 	int offset = off & (sb->s_blocksize - 1);
1539*4882a593Smuzhiyun 	int tocopy;
1540*4882a593Smuzhiyun 	size_t towrite = len;
1541*4882a593Smuzhiyun 	struct buffer_head tmp_bh;
1542*4882a593Smuzhiyun 	struct buffer_head *bh;
1543*4882a593Smuzhiyun 
1544*4882a593Smuzhiyun 	while (towrite > 0) {
1545*4882a593Smuzhiyun 		tocopy = sb->s_blocksize - offset < towrite ?
1546*4882a593Smuzhiyun 				sb->s_blocksize - offset : towrite;
1547*4882a593Smuzhiyun 
1548*4882a593Smuzhiyun 		tmp_bh.b_state = 0;
1549*4882a593Smuzhiyun 		tmp_bh.b_size = sb->s_blocksize;
1550*4882a593Smuzhiyun 		err = ext2_get_block(inode, blk, &tmp_bh, 1);
1551*4882a593Smuzhiyun 		if (err < 0)
1552*4882a593Smuzhiyun 			goto out;
1553*4882a593Smuzhiyun 		if (offset || tocopy != EXT2_BLOCK_SIZE(sb))
1554*4882a593Smuzhiyun 			bh = sb_bread(sb, tmp_bh.b_blocknr);
1555*4882a593Smuzhiyun 		else
1556*4882a593Smuzhiyun 			bh = sb_getblk(sb, tmp_bh.b_blocknr);
1557*4882a593Smuzhiyun 		if (unlikely(!bh)) {
1558*4882a593Smuzhiyun 			err = -EIO;
1559*4882a593Smuzhiyun 			goto out;
1560*4882a593Smuzhiyun 		}
1561*4882a593Smuzhiyun 		lock_buffer(bh);
1562*4882a593Smuzhiyun 		memcpy(bh->b_data+offset, data, tocopy);
1563*4882a593Smuzhiyun 		flush_dcache_page(bh->b_page);
1564*4882a593Smuzhiyun 		set_buffer_uptodate(bh);
1565*4882a593Smuzhiyun 		mark_buffer_dirty(bh);
1566*4882a593Smuzhiyun 		unlock_buffer(bh);
1567*4882a593Smuzhiyun 		brelse(bh);
1568*4882a593Smuzhiyun 		offset = 0;
1569*4882a593Smuzhiyun 		towrite -= tocopy;
1570*4882a593Smuzhiyun 		data += tocopy;
1571*4882a593Smuzhiyun 		blk++;
1572*4882a593Smuzhiyun 	}
1573*4882a593Smuzhiyun out:
1574*4882a593Smuzhiyun 	if (len == towrite)
1575*4882a593Smuzhiyun 		return err;
1576*4882a593Smuzhiyun 	if (inode->i_size < off+len-towrite)
1577*4882a593Smuzhiyun 		i_size_write(inode, off+len-towrite);
1578*4882a593Smuzhiyun 	inode_inc_iversion(inode);
1579*4882a593Smuzhiyun 	inode->i_mtime = inode->i_ctime = current_time(inode);
1580*4882a593Smuzhiyun 	mark_inode_dirty(inode);
1581*4882a593Smuzhiyun 	return len - towrite;
1582*4882a593Smuzhiyun }
1583*4882a593Smuzhiyun 
ext2_quota_on(struct super_block * sb,int type,int format_id,const struct path * path)1584*4882a593Smuzhiyun static int ext2_quota_on(struct super_block *sb, int type, int format_id,
1585*4882a593Smuzhiyun 			 const struct path *path)
1586*4882a593Smuzhiyun {
1587*4882a593Smuzhiyun 	int err;
1588*4882a593Smuzhiyun 	struct inode *inode;
1589*4882a593Smuzhiyun 
1590*4882a593Smuzhiyun 	err = dquot_quota_on(sb, type, format_id, path);
1591*4882a593Smuzhiyun 	if (err)
1592*4882a593Smuzhiyun 		return err;
1593*4882a593Smuzhiyun 
1594*4882a593Smuzhiyun 	inode = d_inode(path->dentry);
1595*4882a593Smuzhiyun 	inode_lock(inode);
1596*4882a593Smuzhiyun 	EXT2_I(inode)->i_flags |= EXT2_NOATIME_FL | EXT2_IMMUTABLE_FL;
1597*4882a593Smuzhiyun 	inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
1598*4882a593Smuzhiyun 			S_NOATIME | S_IMMUTABLE);
1599*4882a593Smuzhiyun 	inode_unlock(inode);
1600*4882a593Smuzhiyun 	mark_inode_dirty(inode);
1601*4882a593Smuzhiyun 
1602*4882a593Smuzhiyun 	return 0;
1603*4882a593Smuzhiyun }
1604*4882a593Smuzhiyun 
ext2_quota_off(struct super_block * sb,int type)1605*4882a593Smuzhiyun static int ext2_quota_off(struct super_block *sb, int type)
1606*4882a593Smuzhiyun {
1607*4882a593Smuzhiyun 	struct inode *inode = sb_dqopt(sb)->files[type];
1608*4882a593Smuzhiyun 	int err;
1609*4882a593Smuzhiyun 
1610*4882a593Smuzhiyun 	if (!inode || !igrab(inode))
1611*4882a593Smuzhiyun 		goto out;
1612*4882a593Smuzhiyun 
1613*4882a593Smuzhiyun 	err = dquot_quota_off(sb, type);
1614*4882a593Smuzhiyun 	if (err)
1615*4882a593Smuzhiyun 		goto out_put;
1616*4882a593Smuzhiyun 
1617*4882a593Smuzhiyun 	inode_lock(inode);
1618*4882a593Smuzhiyun 	EXT2_I(inode)->i_flags &= ~(EXT2_NOATIME_FL | EXT2_IMMUTABLE_FL);
1619*4882a593Smuzhiyun 	inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
1620*4882a593Smuzhiyun 	inode_unlock(inode);
1621*4882a593Smuzhiyun 	mark_inode_dirty(inode);
1622*4882a593Smuzhiyun out_put:
1623*4882a593Smuzhiyun 	iput(inode);
1624*4882a593Smuzhiyun 	return err;
1625*4882a593Smuzhiyun out:
1626*4882a593Smuzhiyun 	return dquot_quota_off(sb, type);
1627*4882a593Smuzhiyun }
1628*4882a593Smuzhiyun 
1629*4882a593Smuzhiyun #endif
1630*4882a593Smuzhiyun 
1631*4882a593Smuzhiyun static struct file_system_type ext2_fs_type = {
1632*4882a593Smuzhiyun 	.owner		= THIS_MODULE,
1633*4882a593Smuzhiyun 	.name		= "ext2",
1634*4882a593Smuzhiyun 	.mount		= ext2_mount,
1635*4882a593Smuzhiyun 	.kill_sb	= kill_block_super,
1636*4882a593Smuzhiyun 	.fs_flags	= FS_REQUIRES_DEV,
1637*4882a593Smuzhiyun };
1638*4882a593Smuzhiyun MODULE_ALIAS_FS("ext2");
1639*4882a593Smuzhiyun 
init_ext2_fs(void)1640*4882a593Smuzhiyun static int __init init_ext2_fs(void)
1641*4882a593Smuzhiyun {
1642*4882a593Smuzhiyun 	int err;
1643*4882a593Smuzhiyun 
1644*4882a593Smuzhiyun 	err = init_inodecache();
1645*4882a593Smuzhiyun 	if (err)
1646*4882a593Smuzhiyun 		return err;
1647*4882a593Smuzhiyun         err = register_filesystem(&ext2_fs_type);
1648*4882a593Smuzhiyun 	if (err)
1649*4882a593Smuzhiyun 		goto out;
1650*4882a593Smuzhiyun 	return 0;
1651*4882a593Smuzhiyun out:
1652*4882a593Smuzhiyun 	destroy_inodecache();
1653*4882a593Smuzhiyun 	return err;
1654*4882a593Smuzhiyun }
1655*4882a593Smuzhiyun 
exit_ext2_fs(void)1656*4882a593Smuzhiyun static void __exit exit_ext2_fs(void)
1657*4882a593Smuzhiyun {
1658*4882a593Smuzhiyun 	unregister_filesystem(&ext2_fs_type);
1659*4882a593Smuzhiyun 	destroy_inodecache();
1660*4882a593Smuzhiyun }
1661*4882a593Smuzhiyun 
1662*4882a593Smuzhiyun MODULE_AUTHOR("Remy Card and others");
1663*4882a593Smuzhiyun MODULE_DESCRIPTION("Second Extended Filesystem");
1664*4882a593Smuzhiyun MODULE_LICENSE("GPL");
1665*4882a593Smuzhiyun MODULE_IMPORT_NS(ANDROID_GKI_VFS_EXPORT_ONLY);
1666*4882a593Smuzhiyun module_init(init_ext2_fs)
1667*4882a593Smuzhiyun module_exit(exit_ext2_fs)
1668