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