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