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