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