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/iversion.h> 38 #include "ext2.h" 39 #include "xattr.h" 40 #include "acl.h" 41 42 static void ext2_write_super(struct super_block *sb); 43 static int ext2_statfs (struct dentry * dentry, struct kstatfs * buf); 44 static int ext2_sync_fs(struct super_block *sb, int wait); 45 static int ext2_freeze(struct super_block *sb); 46 static int ext2_unfreeze(struct super_block *sb); 47 48 void 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 83 static 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 110 void 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 */ 129 void 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 156 static int ext2_quota_off(struct super_block *sb, int type); 157 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 166 static inline void ext2_quota_off_umount(struct super_block *sb) 167 { 168 } 169 #endif 170 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 kfree(sbi); 202 } 203 204 static struct kmem_cache * ext2_inode_cachep; 205 206 static 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 222 static void ext2_free_in_core_inode(struct inode *inode) 223 { 224 kmem_cache_free(ext2_inode_cachep, EXT2_I(inode)); 225 } 226 227 static 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 239 static 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 252 static 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 262 static 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 341 static ssize_t ext2_quota_read(struct super_block *sb, int type, char *data, size_t len, loff_t off); 342 static ssize_t ext2_quota_write(struct super_block *sb, int type, const char *data, size_t len, loff_t off); 343 static int ext2_quota_on(struct super_block *sb, int type, int format_id, 344 const struct path *path); 345 static struct dquot __rcu **ext2_get_dquots(struct inode *inode) 346 { 347 return EXT2_I(inode)->i_dquot; 348 } 349 350 static 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 362 static 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 381 static 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 407 static 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 414 static 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 421 static 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 428 enum { 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 435 static 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 442 static 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 473 struct 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 485 static 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 492 static 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 499 static inline unsigned long 500 ctx_test_mount_opt(struct ext2_fs_context *ctx, unsigned long flag) 501 { 502 return (ctx->vals_s_mount_opt & flag); 503 } 504 505 static inline bool 506 ctx_parsed_mount_opt(struct ext2_fs_context *ctx, unsigned long flag) 507 { 508 return (ctx->mask_s_mount_opt & flag); 509 } 510 511 static void ext2_free_fc(struct fs_context *fc) 512 { 513 kfree(fc->fs_private); 514 } 515 516 static 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 634 static 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 685 static 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 */ 734 static 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; 783 check_lfs: 784 res <<= bits; 785 if (res > MAX_LFS_FILESIZE) 786 res = MAX_LFS_FILESIZE; 787 788 return res; 789 } 790 791 static 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 */ 812 static 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 867 static 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 sb->s_fs_info = kzalloc_obj(*sbi); 887 if (!sb->s_fs_info) 888 return -ENOMEM; 889 sbi = EXT2_SB(sb); 890 891 sbi->s_blockgroup_lock = 892 kzalloc_obj(struct blockgroup_lock); 893 if (!sbi->s_blockgroup_lock) { 894 kfree(sb->s_fs_info); 895 sb->s_fs_info = NULL; 896 return -ENOMEM; 897 } 898 sbi->s_sb_block = sb_block; 899 900 guard(spinlock_init)(&EXT2_SB(sb)->s_lock); 901 ret = -EINVAL; 902 903 /* 904 * See what the current blocksize for the device is, and 905 * use that as the blocksize. Otherwise (or if the blocksize 906 * is smaller than the default) use the default. 907 * This is important for devices that have a hardware 908 * sectorsize that is larger than the default. 909 */ 910 blocksize = sb_min_blocksize(sb, BLOCK_SIZE); 911 if (!blocksize) { 912 ext2_msg(sb, KERN_ERR, "error: unable to set blocksize"); 913 goto failed_sbi; 914 } 915 916 /* 917 * If the superblock doesn't start on a hardware sector boundary, 918 * calculate the offset. 919 */ 920 if (blocksize != BLOCK_SIZE) { 921 logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize; 922 offset = (sb_block*BLOCK_SIZE) % blocksize; 923 } else { 924 logic_sb_block = sb_block; 925 } 926 927 if (!(bh = sb_bread(sb, logic_sb_block))) { 928 ext2_msg(sb, KERN_ERR, "error: unable to read superblock"); 929 goto failed_sbi; 930 } 931 /* 932 * Note: s_es must be initialized as soon as possible because 933 * some ext2 macro-instructions depend on its value 934 */ 935 es = (struct ext2_super_block *) (((char *)bh->b_data) + offset); 936 sbi->s_es = es; 937 sb->s_magic = le16_to_cpu(es->s_magic); 938 939 if (sb->s_magic != EXT2_SUPER_MAGIC) 940 goto cantfind_ext2; 941 942 ext2_set_options(fc, sbi); 943 944 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) | 945 (test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0); 946 sb->s_iflags |= SB_I_CGROUPWB; 947 948 if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV && 949 (EXT2_HAS_COMPAT_FEATURE(sb, ~0U) || 950 EXT2_HAS_RO_COMPAT_FEATURE(sb, ~0U) || 951 EXT2_HAS_INCOMPAT_FEATURE(sb, ~0U))) 952 ext2_msg(sb, KERN_WARNING, 953 "warning: feature flags set on rev 0 fs, " 954 "running e2fsck is recommended"); 955 /* 956 * Check feature flags regardless of the revision level, since we 957 * previously didn't change the revision level when setting the flags, 958 * so there is a chance incompat flags are set on a rev 0 filesystem. 959 */ 960 features = EXT2_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP); 961 if (features) { 962 ext2_msg(sb, KERN_ERR, "error: couldn't mount because of " 963 "unsupported optional features (%x)", 964 le32_to_cpu(features)); 965 goto failed_mount; 966 } 967 if (!sb_rdonly(sb) && (features = EXT2_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))){ 968 ext2_msg(sb, KERN_ERR, "error: couldn't mount RDWR because of " 969 "unsupported optional features (%x)", 970 le32_to_cpu(features)); 971 goto failed_mount; 972 } 973 974 if (le32_to_cpu(es->s_log_block_size) > 975 (EXT2_MAX_BLOCK_LOG_SIZE - BLOCK_SIZE_BITS)) { 976 ext2_msg(sb, KERN_ERR, 977 "Invalid log block size: %u", 978 le32_to_cpu(es->s_log_block_size)); 979 goto failed_mount; 980 } 981 blocksize = BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size); 982 983 /* If the blocksize doesn't match, re-read the thing.. */ 984 if (sb->s_blocksize != blocksize) { 985 brelse(bh); 986 987 if (!sb_set_blocksize(sb, blocksize)) { 988 ext2_msg(sb, KERN_ERR, 989 "error: bad blocksize %d", blocksize); 990 goto failed_sbi; 991 } 992 993 logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize; 994 offset = (sb_block*BLOCK_SIZE) % blocksize; 995 bh = sb_bread(sb, logic_sb_block); 996 if(!bh) { 997 ext2_msg(sb, KERN_ERR, "error: couldn't read" 998 "superblock on 2nd try"); 999 goto failed_sbi; 1000 } 1001 es = (struct ext2_super_block *) (((char *)bh->b_data) + offset); 1002 sbi->s_es = es; 1003 if (es->s_magic != cpu_to_le16(EXT2_SUPER_MAGIC)) { 1004 ext2_msg(sb, KERN_ERR, "error: magic mismatch"); 1005 goto failed_mount; 1006 } 1007 } 1008 1009 sb->s_maxbytes = ext2_max_size(sb->s_blocksize_bits); 1010 sb->s_max_links = EXT2_LINK_MAX; 1011 sb->s_time_min = S32_MIN; 1012 sb->s_time_max = S32_MAX; 1013 1014 if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV) { 1015 sbi->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE; 1016 sbi->s_first_ino = EXT2_GOOD_OLD_FIRST_INO; 1017 } else { 1018 sbi->s_inode_size = le16_to_cpu(es->s_inode_size); 1019 sbi->s_first_ino = le32_to_cpu(es->s_first_ino); 1020 if ((sbi->s_inode_size < EXT2_GOOD_OLD_INODE_SIZE) || 1021 !is_power_of_2(sbi->s_inode_size) || 1022 (sbi->s_inode_size > blocksize)) { 1023 ext2_msg(sb, KERN_ERR, 1024 "error: unsupported inode size: %d", 1025 sbi->s_inode_size); 1026 goto failed_mount; 1027 } 1028 } 1029 1030 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group); 1031 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group); 1032 1033 sbi->s_inodes_per_block = sb->s_blocksize / EXT2_INODE_SIZE(sb); 1034 if (sbi->s_inodes_per_block == 0 || sbi->s_inodes_per_group == 0) 1035 goto cantfind_ext2; 1036 sbi->s_itb_per_group = sbi->s_inodes_per_group / 1037 sbi->s_inodes_per_block; 1038 sbi->s_desc_per_block = sb->s_blocksize / 1039 sizeof (struct ext2_group_desc); 1040 sbi->s_sbh = bh; 1041 sbi->s_mount_state = le16_to_cpu(es->s_state); 1042 sbi->s_addr_per_block_bits = 1043 ilog2 (EXT2_ADDR_PER_BLOCK(sb)); 1044 sbi->s_desc_per_block_bits = 1045 ilog2 (EXT2_DESC_PER_BLOCK(sb)); 1046 1047 if (sb->s_magic != EXT2_SUPER_MAGIC) 1048 goto cantfind_ext2; 1049 1050 if (sb->s_blocksize != bh->b_size) { 1051 if (!silent) 1052 ext2_msg(sb, KERN_ERR, "error: unsupported blocksize"); 1053 goto failed_mount; 1054 } 1055 1056 if (es->s_log_frag_size != es->s_log_block_size) { 1057 ext2_msg(sb, KERN_ERR, 1058 "error: fragsize log %u != blocksize log %u", 1059 le32_to_cpu(es->s_log_frag_size), sb->s_blocksize_bits); 1060 goto failed_mount; 1061 } 1062 1063 if (sbi->s_blocks_per_group > sb->s_blocksize * 8) { 1064 ext2_msg(sb, KERN_ERR, 1065 "error: #blocks per group too big: %lu", 1066 sbi->s_blocks_per_group); 1067 goto failed_mount; 1068 } 1069 /* At least inode table, bitmaps, and sb have to fit in one group */ 1070 if (sbi->s_blocks_per_group <= sbi->s_itb_per_group + 3) { 1071 ext2_msg(sb, KERN_ERR, 1072 "error: #blocks per group smaller than metadata size: %lu <= %lu", 1073 sbi->s_blocks_per_group, sbi->s_inodes_per_group + 3); 1074 goto failed_mount; 1075 } 1076 if (sbi->s_inodes_per_group < sbi->s_inodes_per_block || 1077 sbi->s_inodes_per_group > sb->s_blocksize * 8) { 1078 ext2_msg(sb, KERN_ERR, 1079 "error: invalid #inodes per group: %lu", 1080 sbi->s_inodes_per_group); 1081 goto failed_mount; 1082 } 1083 if (sb_bdev_nr_blocks(sb) < le32_to_cpu(es->s_blocks_count)) { 1084 ext2_msg(sb, KERN_ERR, 1085 "bad geometry: block count %u exceeds size of device (%u blocks)", 1086 le32_to_cpu(es->s_blocks_count), 1087 (unsigned)sb_bdev_nr_blocks(sb)); 1088 goto failed_mount; 1089 } 1090 1091 sbi->s_groups_count = ((le32_to_cpu(es->s_blocks_count) - 1092 le32_to_cpu(es->s_first_data_block) - 1) 1093 / EXT2_BLOCKS_PER_GROUP(sb)) + 1; 1094 if ((u64)sbi->s_groups_count * sbi->s_inodes_per_group != 1095 le32_to_cpu(es->s_inodes_count)) { 1096 ext2_msg(sb, KERN_ERR, "error: invalid #inodes: %u vs computed %llu", 1097 le32_to_cpu(es->s_inodes_count), 1098 (u64)sbi->s_groups_count * sbi->s_inodes_per_group); 1099 goto failed_mount; 1100 } 1101 db_count = (sbi->s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) / 1102 EXT2_DESC_PER_BLOCK(sb); 1103 sbi->s_group_desc = kvmalloc_objs(struct buffer_head *, db_count); 1104 if (sbi->s_group_desc == NULL) { 1105 ret = -ENOMEM; 1106 ext2_msg(sb, KERN_ERR, "error: not enough memory"); 1107 goto failed_mount; 1108 } 1109 bgl_lock_init(sbi->s_blockgroup_lock); 1110 sbi->s_debts = kzalloc_objs(*sbi->s_debts, sbi->s_groups_count); 1111 if (!sbi->s_debts) { 1112 ret = -ENOMEM; 1113 ext2_msg(sb, KERN_ERR, "error: not enough memory"); 1114 goto failed_mount_group_desc; 1115 } 1116 for (i = 0; i < db_count; i++) { 1117 block = descriptor_loc(sb, logic_sb_block, i); 1118 sbi->s_group_desc[i] = sb_bread(sb, block); 1119 if (!sbi->s_group_desc[i]) { 1120 for (j = 0; j < i; j++) 1121 brelse (sbi->s_group_desc[j]); 1122 ext2_msg(sb, KERN_ERR, 1123 "error: unable to read group descriptors"); 1124 goto failed_mount_group_desc; 1125 } 1126 } 1127 if (!ext2_check_descriptors (sb)) { 1128 ext2_msg(sb, KERN_ERR, "group descriptors corrupted"); 1129 goto failed_mount2; 1130 } 1131 sbi->s_gdb_count = db_count; 1132 spin_lock_init(&sbi->s_next_gen_lock); 1133 scoped_guard(spinlock, &sbi->s_next_gen_lock) 1134 sbi->s_next_generation = get_random_u32(); 1135 1136 /* per filesystem reservation list head & lock */ 1137 spin_lock_init(&sbi->s_rsv_window_lock); 1138 scoped_guard(spinlock, &EXT2_SB(sb)->s_rsv_window_lock) { 1139 sbi->s_rsv_window_root = RB_ROOT; 1140 /* 1141 * Add a single, static dummy reservation to the start of the 1142 * reservation window list --- it gives us a placeholder for 1143 * append-at-start-of-list which makes the allocation logic 1144 * _much_ simpler. 1145 */ 1146 sbi->s_rsv_window_head.rsv_start = EXT2_RESERVE_WINDOW_NOT_ALLOCATED; 1147 sbi->s_rsv_window_head.rsv_end = EXT2_RESERVE_WINDOW_NOT_ALLOCATED; 1148 sbi->s_rsv_window_head.rsv_alloc_hit = 0; 1149 sbi->s_rsv_window_head.rsv_goal_size = 0; 1150 ext2_rsv_window_add(sb, &sbi->s_rsv_window_head); 1151 } 1152 1153 err = percpu_counter_init(&sbi->s_freeblocks_counter, 1154 ext2_count_free_blocks(sb), GFP_KERNEL); 1155 if (!err) { 1156 err = percpu_counter_init(&sbi->s_freeinodes_counter, 1157 ext2_count_free_inodes(sb), GFP_KERNEL); 1158 } 1159 if (!err) { 1160 err = percpu_counter_init(&sbi->s_dirs_counter, 1161 ext2_count_dirs(sb), GFP_KERNEL); 1162 } 1163 if (err) { 1164 ret = err; 1165 ext2_msg(sb, KERN_ERR, "error: insufficient memory"); 1166 goto failed_mount3; 1167 } 1168 1169 #ifdef CONFIG_EXT2_FS_XATTR 1170 sbi->s_ea_block_cache = ext2_xattr_create_cache(); 1171 if (!sbi->s_ea_block_cache) { 1172 ret = -ENOMEM; 1173 ext2_msg(sb, KERN_ERR, "Failed to create ea_block_cache"); 1174 goto failed_mount3; 1175 } 1176 #endif 1177 /* 1178 * set up enough so that it can read an inode 1179 */ 1180 sb->s_op = &ext2_sops; 1181 sb->s_export_op = &ext2_export_ops; 1182 sb->s_xattr = ext2_xattr_handlers; 1183 1184 #ifdef CONFIG_QUOTA 1185 sb->dq_op = &dquot_operations; 1186 sb->s_qcop = &ext2_quotactl_ops; 1187 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP; 1188 #endif 1189 1190 root = ext2_iget(sb, EXT2_ROOT_INO); 1191 if (IS_ERR(root)) { 1192 ret = PTR_ERR(root); 1193 goto failed_mount3; 1194 } 1195 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) { 1196 iput(root); 1197 ext2_msg(sb, KERN_ERR, "error: corrupt root inode, run e2fsck"); 1198 goto failed_mount3; 1199 } 1200 1201 sb->s_root = d_make_root(root); 1202 if (!sb->s_root) { 1203 ext2_msg(sb, KERN_ERR, "error: get root inode failed"); 1204 ret = -ENOMEM; 1205 goto failed_mount3; 1206 } 1207 if (EXT2_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) 1208 ext2_msg(sb, KERN_WARNING, 1209 "warning: mounting ext3 filesystem as ext2"); 1210 if (ext2_setup_super (sb, es, sb_rdonly(sb))) 1211 sb->s_flags |= SB_RDONLY; 1212 ext2_write_super(sb); 1213 return 0; 1214 1215 cantfind_ext2: 1216 if (!silent) 1217 ext2_msg(sb, KERN_ERR, 1218 "error: can't find an ext2 filesystem on dev %s.", 1219 sb->s_id); 1220 goto failed_mount; 1221 failed_mount3: 1222 ext2_xattr_destroy_cache(sbi->s_ea_block_cache); 1223 percpu_counter_destroy(&sbi->s_freeblocks_counter); 1224 percpu_counter_destroy(&sbi->s_freeinodes_counter); 1225 percpu_counter_destroy(&sbi->s_dirs_counter); 1226 failed_mount2: 1227 for (i = 0; i < db_count; i++) 1228 brelse(sbi->s_group_desc[i]); 1229 failed_mount_group_desc: 1230 kvfree(sbi->s_group_desc); 1231 kfree(sbi->s_debts); 1232 failed_mount: 1233 brelse(bh); 1234 failed_sbi: 1235 sb->s_fs_info = NULL; 1236 kfree(sbi->s_blockgroup_lock); 1237 kfree(sbi); 1238 return ret; 1239 } 1240 1241 static void ext2_clear_super_error(struct super_block *sb) 1242 { 1243 struct buffer_head *sbh = EXT2_SB(sb)->s_sbh; 1244 1245 if (buffer_write_io_error(sbh)) { 1246 /* 1247 * Oh, dear. A previous attempt to write the 1248 * superblock failed. This could happen because the 1249 * USB device was yanked out. Or it could happen to 1250 * be a transient write error and maybe the block will 1251 * be remapped. Nothing we can do but to retry the 1252 * write and hope for the best. 1253 */ 1254 ext2_msg(sb, KERN_ERR, 1255 "previous I/O error to superblock detected"); 1256 clear_buffer_write_io_error(sbh); 1257 set_buffer_uptodate(sbh); 1258 } 1259 } 1260 1261 void ext2_sync_super(struct super_block *sb, struct ext2_super_block *es, 1262 int wait) 1263 { 1264 ext2_clear_super_error(sb); 1265 spin_lock(&EXT2_SB(sb)->s_lock); 1266 es->s_free_blocks_count = cpu_to_le32(ext2_count_free_blocks(sb)); 1267 es->s_free_inodes_count = cpu_to_le32(ext2_count_free_inodes(sb)); 1268 es->s_wtime = cpu_to_le32(ktime_get_real_seconds()); 1269 /* unlock before we do IO */ 1270 spin_unlock(&EXT2_SB(sb)->s_lock); 1271 mark_buffer_dirty(EXT2_SB(sb)->s_sbh); 1272 if (wait) 1273 sync_dirty_buffer(EXT2_SB(sb)->s_sbh); 1274 } 1275 1276 /* 1277 * In the second extended file system, it is not necessary to 1278 * write the super block since we use a mapping of the 1279 * disk super block in a buffer. 1280 * 1281 * However, this function is still used to set the fs valid 1282 * flags to 0. We need to set this flag to 0 since the fs 1283 * may have been checked while mounted and e2fsck may have 1284 * set s_state to EXT2_VALID_FS after some corrections. 1285 */ 1286 static int ext2_sync_fs(struct super_block *sb, int wait) 1287 { 1288 struct ext2_sb_info *sbi = EXT2_SB(sb); 1289 struct ext2_super_block *es = EXT2_SB(sb)->s_es; 1290 1291 /* 1292 * Write quota structures to quota file, sync_blockdev() will write 1293 * them to disk later 1294 */ 1295 dquot_writeback_dquots(sb, -1); 1296 1297 spin_lock(&sbi->s_lock); 1298 if (es->s_state & cpu_to_le16(EXT2_VALID_FS)) { 1299 ext2_debug("setting valid to 0\n"); 1300 es->s_state &= cpu_to_le16(~EXT2_VALID_FS); 1301 } 1302 spin_unlock(&sbi->s_lock); 1303 ext2_sync_super(sb, es, wait); 1304 return 0; 1305 } 1306 1307 static int ext2_freeze(struct super_block *sb) 1308 { 1309 struct ext2_sb_info *sbi = EXT2_SB(sb); 1310 1311 /* 1312 * Open but unlinked files present? Keep EXT2_VALID_FS flag cleared 1313 * because we have unattached inodes and thus filesystem is not fully 1314 * consistent. 1315 */ 1316 if (atomic_long_read(&sb->s_remove_count)) { 1317 ext2_sync_fs(sb, 1); 1318 return 0; 1319 } 1320 /* Set EXT2_FS_VALID flag */ 1321 spin_lock(&sbi->s_lock); 1322 sbi->s_es->s_state = cpu_to_le16(sbi->s_mount_state); 1323 spin_unlock(&sbi->s_lock); 1324 ext2_sync_super(sb, sbi->s_es, 1); 1325 1326 return 0; 1327 } 1328 1329 static int ext2_unfreeze(struct super_block *sb) 1330 { 1331 /* Just write sb to clear EXT2_VALID_FS flag */ 1332 ext2_write_super(sb); 1333 1334 return 0; 1335 } 1336 1337 static void ext2_write_super(struct super_block *sb) 1338 { 1339 if (!sb_rdonly(sb)) 1340 ext2_sync_fs(sb, 1); 1341 } 1342 1343 static int ext2_reconfigure(struct fs_context *fc) 1344 { 1345 struct ext2_fs_context *ctx = fc->fs_private; 1346 struct super_block *sb = fc->root->d_sb; 1347 struct ext2_sb_info * sbi = EXT2_SB(sb); 1348 struct ext2_super_block * es; 1349 struct ext2_mount_options new_opts; 1350 int flags = fc->sb_flags; 1351 int err; 1352 1353 sync_filesystem(sb); 1354 1355 new_opts.s_mount_opt = ctx->vals_s_mount_opt; 1356 new_opts.s_resuid = ctx->s_resuid; 1357 new_opts.s_resgid = ctx->s_resgid; 1358 1359 spin_lock(&sbi->s_lock); 1360 es = sbi->s_es; 1361 if ((bool)(flags & SB_RDONLY) == sb_rdonly(sb)) 1362 goto out_set; 1363 if (flags & SB_RDONLY) { 1364 if (le16_to_cpu(es->s_state) & EXT2_VALID_FS || 1365 !(sbi->s_mount_state & EXT2_VALID_FS)) 1366 goto out_set; 1367 1368 /* 1369 * OK, we are remounting a valid rw partition rdonly, so set 1370 * the rdonly flag and then mark the partition as valid again. 1371 */ 1372 es->s_state = cpu_to_le16(sbi->s_mount_state); 1373 es->s_mtime = cpu_to_le32(ktime_get_real_seconds()); 1374 spin_unlock(&sbi->s_lock); 1375 1376 err = dquot_suspend(sb, -1); 1377 if (err < 0) 1378 return err; 1379 1380 ext2_sync_super(sb, es, 1); 1381 } else { 1382 __le32 ret = EXT2_HAS_RO_COMPAT_FEATURE(sb, 1383 ~EXT2_FEATURE_RO_COMPAT_SUPP); 1384 if (ret) { 1385 spin_unlock(&sbi->s_lock); 1386 ext2_msg(sb, KERN_WARNING, 1387 "warning: couldn't remount RDWR because of " 1388 "unsupported optional features (%x).", 1389 le32_to_cpu(ret)); 1390 return -EROFS; 1391 } 1392 /* 1393 * Mounting a RDONLY partition read-write, so reread and 1394 * store the current valid flag. (It may have been changed 1395 * by e2fsck since we originally mounted the partition.) 1396 */ 1397 sbi->s_mount_state = le16_to_cpu(es->s_state); 1398 if (!ext2_setup_super (sb, es, 0)) 1399 sb->s_flags &= ~SB_RDONLY; 1400 spin_unlock(&sbi->s_lock); 1401 1402 ext2_write_super(sb); 1403 1404 dquot_resume(sb, -1); 1405 } 1406 1407 spin_lock(&sbi->s_lock); 1408 out_set: 1409 sbi->s_mount_opt = new_opts.s_mount_opt; 1410 sbi->s_resuid = new_opts.s_resuid; 1411 sbi->s_resgid = new_opts.s_resgid; 1412 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) | 1413 (test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0); 1414 spin_unlock(&sbi->s_lock); 1415 1416 return 0; 1417 } 1418 1419 static int ext2_statfs (struct dentry * dentry, struct kstatfs * buf) 1420 { 1421 struct super_block *sb = dentry->d_sb; 1422 struct ext2_sb_info *sbi = EXT2_SB(sb); 1423 struct ext2_super_block *es = sbi->s_es; 1424 1425 spin_lock(&sbi->s_lock); 1426 1427 if (test_opt (sb, MINIX_DF)) 1428 sbi->s_overhead_last = 0; 1429 else if (sbi->s_blocks_last != le32_to_cpu(es->s_blocks_count)) { 1430 unsigned long i, overhead = 0; 1431 smp_rmb(); 1432 1433 /* 1434 * Compute the overhead (FS structures). This is constant 1435 * for a given filesystem unless the number of block groups 1436 * changes so we cache the previous value until it does. 1437 */ 1438 1439 /* 1440 * All of the blocks before first_data_block are 1441 * overhead 1442 */ 1443 overhead = le32_to_cpu(es->s_first_data_block); 1444 1445 /* 1446 * Add the overhead attributed to the superblock and 1447 * block group descriptors. If the sparse superblocks 1448 * feature is turned on, then not all groups have this. 1449 */ 1450 for (i = 0; i < sbi->s_groups_count; i++) 1451 overhead += ext2_bg_has_super(sb, i) + 1452 ext2_bg_num_gdb(sb, i); 1453 1454 /* 1455 * Every block group has an inode bitmap, a block 1456 * bitmap, and an inode table. 1457 */ 1458 overhead += (sbi->s_groups_count * 1459 (2 + sbi->s_itb_per_group)); 1460 sbi->s_overhead_last = overhead; 1461 smp_wmb(); 1462 sbi->s_blocks_last = le32_to_cpu(es->s_blocks_count); 1463 } 1464 1465 buf->f_type = EXT2_SUPER_MAGIC; 1466 buf->f_bsize = sb->s_blocksize; 1467 buf->f_blocks = le32_to_cpu(es->s_blocks_count) - sbi->s_overhead_last; 1468 buf->f_bfree = ext2_count_free_blocks(sb); 1469 es->s_free_blocks_count = cpu_to_le32(buf->f_bfree); 1470 buf->f_bavail = buf->f_bfree - le32_to_cpu(es->s_r_blocks_count); 1471 if (buf->f_bfree < le32_to_cpu(es->s_r_blocks_count)) 1472 buf->f_bavail = 0; 1473 buf->f_files = le32_to_cpu(es->s_inodes_count); 1474 buf->f_ffree = ext2_count_free_inodes(sb); 1475 es->s_free_inodes_count = cpu_to_le32(buf->f_ffree); 1476 buf->f_namelen = EXT2_NAME_LEN; 1477 buf->f_fsid = uuid_to_fsid(es->s_uuid); 1478 spin_unlock(&sbi->s_lock); 1479 return 0; 1480 } 1481 1482 static int ext2_get_tree(struct fs_context *fc) 1483 { 1484 return get_tree_bdev(fc, ext2_fill_super); 1485 } 1486 1487 #ifdef CONFIG_QUOTA 1488 1489 /* Read data from quotafile - avoid pagecache and such because we cannot afford 1490 * acquiring the locks... As quota files are never truncated and quota code 1491 * itself serializes the operations (and no one else should touch the files) 1492 * we don't have to be afraid of races */ 1493 static ssize_t ext2_quota_read(struct super_block *sb, int type, char *data, 1494 size_t len, loff_t off) 1495 { 1496 struct inode *inode = sb_dqopt(sb)->files[type]; 1497 sector_t blk = off >> EXT2_BLOCK_SIZE_BITS(sb); 1498 int err = 0; 1499 int offset = off & (sb->s_blocksize - 1); 1500 int tocopy; 1501 size_t toread; 1502 struct buffer_head tmp_bh; 1503 struct buffer_head *bh; 1504 loff_t i_size = i_size_read(inode); 1505 1506 if (off > i_size) 1507 return 0; 1508 if (off+len > i_size) 1509 len = i_size-off; 1510 toread = len; 1511 while (toread > 0) { 1512 tocopy = min_t(size_t, sb->s_blocksize - offset, toread); 1513 1514 tmp_bh.b_state = 0; 1515 tmp_bh.b_size = sb->s_blocksize; 1516 err = ext2_get_block(inode, blk, &tmp_bh, 0); 1517 if (err < 0) 1518 return err; 1519 if (!buffer_mapped(&tmp_bh)) /* A hole? */ 1520 memset(data, 0, tocopy); 1521 else { 1522 bh = sb_bread(sb, tmp_bh.b_blocknr); 1523 if (!bh) 1524 return -EIO; 1525 memcpy(data, bh->b_data+offset, tocopy); 1526 brelse(bh); 1527 } 1528 offset = 0; 1529 toread -= tocopy; 1530 data += tocopy; 1531 blk++; 1532 } 1533 return len; 1534 } 1535 1536 /* Write to quotafile */ 1537 static ssize_t ext2_quota_write(struct super_block *sb, int type, 1538 const char *data, size_t len, loff_t off) 1539 { 1540 struct inode *inode = sb_dqopt(sb)->files[type]; 1541 sector_t blk = off >> EXT2_BLOCK_SIZE_BITS(sb); 1542 int err = 0; 1543 int offset = off & (sb->s_blocksize - 1); 1544 int tocopy; 1545 size_t towrite = len; 1546 struct buffer_head tmp_bh; 1547 struct buffer_head *bh; 1548 1549 while (towrite > 0) { 1550 tocopy = min_t(size_t, sb->s_blocksize - offset, towrite); 1551 1552 tmp_bh.b_state = 0; 1553 tmp_bh.b_size = sb->s_blocksize; 1554 err = ext2_get_block(inode, blk, &tmp_bh, 1); 1555 if (err < 0) 1556 goto out; 1557 if (offset || tocopy != EXT2_BLOCK_SIZE(sb)) 1558 bh = sb_bread(sb, tmp_bh.b_blocknr); 1559 else 1560 bh = sb_getblk(sb, tmp_bh.b_blocknr); 1561 if (unlikely(!bh)) { 1562 err = -EIO; 1563 goto out; 1564 } 1565 lock_buffer(bh); 1566 memcpy(bh->b_data+offset, data, tocopy); 1567 flush_dcache_folio(bh->b_folio); 1568 set_buffer_uptodate(bh); 1569 mark_buffer_dirty(bh); 1570 unlock_buffer(bh); 1571 brelse(bh); 1572 offset = 0; 1573 towrite -= tocopy; 1574 data += tocopy; 1575 blk++; 1576 } 1577 out: 1578 if (len == towrite) 1579 return err; 1580 if (inode->i_size < off+len-towrite) 1581 i_size_write(inode, off+len-towrite); 1582 inode_inc_iversion(inode); 1583 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); 1584 mark_inode_dirty(inode); 1585 return len - towrite; 1586 } 1587 1588 static int ext2_quota_on(struct super_block *sb, int type, int format_id, 1589 const struct path *path) 1590 { 1591 int err; 1592 struct inode *inode; 1593 1594 err = dquot_quota_on(sb, type, format_id, path); 1595 if (err) 1596 return err; 1597 1598 inode = d_inode(path->dentry); 1599 inode_lock(inode); 1600 EXT2_I(inode)->i_flags |= EXT2_NOATIME_FL | EXT2_IMMUTABLE_FL; 1601 inode_set_flags(inode, S_NOATIME | S_IMMUTABLE, 1602 S_NOATIME | S_IMMUTABLE); 1603 inode_unlock(inode); 1604 mark_inode_dirty(inode); 1605 1606 return 0; 1607 } 1608 1609 static int ext2_quota_off(struct super_block *sb, int type) 1610 { 1611 struct inode *inode = sb_dqopt(sb)->files[type]; 1612 int err; 1613 1614 if (!inode || !igrab(inode)) 1615 goto out; 1616 1617 err = dquot_quota_off(sb, type); 1618 if (err) 1619 goto out_put; 1620 1621 inode_lock(inode); 1622 EXT2_I(inode)->i_flags &= ~(EXT2_NOATIME_FL | EXT2_IMMUTABLE_FL); 1623 inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE); 1624 inode_unlock(inode); 1625 mark_inode_dirty(inode); 1626 out_put: 1627 iput(inode); 1628 return err; 1629 out: 1630 return dquot_quota_off(sb, type); 1631 } 1632 1633 #endif 1634 1635 static const struct fs_context_operations ext2_context_ops = { 1636 .parse_param = ext2_parse_param, 1637 .get_tree = ext2_get_tree, 1638 .reconfigure = ext2_reconfigure, 1639 .free = ext2_free_fc, 1640 }; 1641 1642 static int ext2_init_fs_context(struct fs_context *fc) 1643 { 1644 struct ext2_fs_context *ctx; 1645 1646 ctx = kzalloc_obj(*ctx); 1647 if (!ctx) 1648 return -ENOMEM; 1649 1650 if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) { 1651 struct super_block *sb = fc->root->d_sb; 1652 struct ext2_sb_info *sbi = EXT2_SB(sb); 1653 1654 spin_lock(&sbi->s_lock); 1655 ctx->vals_s_mount_opt = sbi->s_mount_opt; 1656 ctx->vals_s_flags = sb->s_flags; 1657 ctx->s_resuid = sbi->s_resuid; 1658 ctx->s_resgid = sbi->s_resgid; 1659 spin_unlock(&sbi->s_lock); 1660 } else { 1661 ctx->s_sb_block = 1; 1662 ctx_set_mount_opt(ctx, EXT2_MOUNT_RESERVATION); 1663 } 1664 1665 fc->fs_private = ctx; 1666 fc->ops = &ext2_context_ops; 1667 1668 return 0; 1669 } 1670 1671 static struct file_system_type ext2_fs_type = { 1672 .owner = THIS_MODULE, 1673 .name = "ext2", 1674 .kill_sb = kill_block_super, 1675 .fs_flags = FS_REQUIRES_DEV, 1676 .init_fs_context = ext2_init_fs_context, 1677 .parameters = ext2_param_spec, 1678 }; 1679 MODULE_ALIAS_FS("ext2"); 1680 1681 static int __init init_ext2_fs(void) 1682 { 1683 int err; 1684 1685 err = init_inodecache(); 1686 if (err) 1687 return err; 1688 err = register_filesystem(&ext2_fs_type); 1689 if (err) 1690 goto out; 1691 return 0; 1692 out: 1693 destroy_inodecache(); 1694 return err; 1695 } 1696 1697 static void __exit exit_ext2_fs(void) 1698 { 1699 unregister_filesystem(&ext2_fs_type); 1700 destroy_inodecache(); 1701 } 1702 1703 MODULE_AUTHOR("Remy Card and others"); 1704 MODULE_DESCRIPTION("Second Extended Filesystem"); 1705 MODULE_LICENSE("GPL"); 1706 module_init(init_ext2_fs) 1707 module_exit(exit_ext2_fs) 1708