projects/linux·blob master · refs

1706 lines · 46.0 KiB

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