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(sizeof(*sbi), GFP_KERNEL); 897 if (!sbi) 898 return -ENOMEM; 899 900 sbi->s_blockgroup_lock = 901 kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL); 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_array(db_count, 1126 sizeof(struct buffer_head *), 1127 GFP_KERNEL); 1128 if (sbi->s_group_desc == NULL) { 1129 ret = -ENOMEM; 1130 ext2_msg(sb, KERN_ERR, "error: not enough memory"); 1131 goto failed_mount; 1132 } 1133 bgl_lock_init(sbi->s_blockgroup_lock); 1134 sbi->s_debts = kcalloc(sbi->s_groups_count, sizeof(*sbi->s_debts), GFP_KERNEL); 1135 if (!sbi->s_debts) { 1136 ret = -ENOMEM; 1137 ext2_msg(sb, KERN_ERR, "error: not enough memory"); 1138 goto failed_mount_group_desc; 1139 } 1140 for (i = 0; i < db_count; i++) { 1141 block = descriptor_loc(sb, logic_sb_block, i); 1142 sbi->s_group_desc[i] = sb_bread(sb, block); 1143 if (!sbi->s_group_desc[i]) { 1144 for (j = 0; j < i; j++) 1145 brelse (sbi->s_group_desc[j]); 1146 ext2_msg(sb, KERN_ERR, 1147 "error: unable to read group descriptors"); 1148 goto failed_mount_group_desc; 1149 } 1150 } 1151 if (!ext2_check_descriptors (sb)) { 1152 ext2_msg(sb, KERN_ERR, "group descriptors corrupted"); 1153 goto failed_mount2; 1154 } 1155 sbi->s_gdb_count = db_count; 1156 get_random_bytes(&sbi->s_next_generation, sizeof(u32)); 1157 spin_lock_init(&sbi->s_next_gen_lock); 1158 1159 /* per filesystem reservation list head & lock */ 1160 spin_lock_init(&sbi->s_rsv_window_lock); 1161 sbi->s_rsv_window_root = RB_ROOT; 1162 /* 1163 * Add a single, static dummy reservation to the start of the 1164 * reservation window list --- it gives us a placeholder for 1165 * append-at-start-of-list which makes the allocation logic 1166 * _much_ simpler. 1167 */ 1168 sbi->s_rsv_window_head.rsv_start = EXT2_RESERVE_WINDOW_NOT_ALLOCATED; 1169 sbi->s_rsv_window_head.rsv_end = EXT2_RESERVE_WINDOW_NOT_ALLOCATED; 1170 sbi->s_rsv_window_head.rsv_alloc_hit = 0; 1171 sbi->s_rsv_window_head.rsv_goal_size = 0; 1172 ext2_rsv_window_add(sb, &sbi->s_rsv_window_head); 1173 1174 err = percpu_counter_init(&sbi->s_freeblocks_counter, 1175 ext2_count_free_blocks(sb), GFP_KERNEL); 1176 if (!err) { 1177 err = percpu_counter_init(&sbi->s_freeinodes_counter, 1178 ext2_count_free_inodes(sb), GFP_KERNEL); 1179 } 1180 if (!err) { 1181 err = percpu_counter_init(&sbi->s_dirs_counter, 1182 ext2_count_dirs(sb), GFP_KERNEL); 1183 } 1184 if (err) { 1185 ret = err; 1186 ext2_msg(sb, KERN_ERR, "error: insufficient memory"); 1187 goto failed_mount3; 1188 } 1189 1190 #ifdef CONFIG_EXT2_FS_XATTR 1191 sbi->s_ea_block_cache = ext2_xattr_create_cache(); 1192 if (!sbi->s_ea_block_cache) { 1193 ret = -ENOMEM; 1194 ext2_msg(sb, KERN_ERR, "Failed to create ea_block_cache"); 1195 goto failed_mount3; 1196 } 1197 #endif 1198 /* 1199 * set up enough so that it can read an inode 1200 */ 1201 sb->s_op = &ext2_sops; 1202 sb->s_export_op = &ext2_export_ops; 1203 sb->s_xattr = ext2_xattr_handlers; 1204 1205 #ifdef CONFIG_QUOTA 1206 sb->dq_op = &dquot_operations; 1207 sb->s_qcop = &ext2_quotactl_ops; 1208 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP; 1209 #endif 1210 1211 root = ext2_iget(sb, EXT2_ROOT_INO); 1212 if (IS_ERR(root)) { 1213 ret = PTR_ERR(root); 1214 goto failed_mount3; 1215 } 1216 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) { 1217 iput(root); 1218 ext2_msg(sb, KERN_ERR, "error: corrupt root inode, run e2fsck"); 1219 goto failed_mount3; 1220 } 1221 1222 sb->s_root = d_make_root(root); 1223 if (!sb->s_root) { 1224 ext2_msg(sb, KERN_ERR, "error: get root inode failed"); 1225 ret = -ENOMEM; 1226 goto failed_mount3; 1227 } 1228 if (EXT2_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) 1229 ext2_msg(sb, KERN_WARNING, 1230 "warning: mounting ext3 filesystem as ext2"); 1231 if (ext2_setup_super (sb, es, sb_rdonly(sb))) 1232 sb->s_flags |= SB_RDONLY; 1233 ext2_write_super(sb); 1234 return 0; 1235 1236 cantfind_ext2: 1237 if (!silent) 1238 ext2_msg(sb, KERN_ERR, 1239 "error: can't find an ext2 filesystem on dev %s.", 1240 sb->s_id); 1241 goto failed_mount; 1242 failed_mount3: 1243 ext2_xattr_destroy_cache(sbi->s_ea_block_cache); 1244 percpu_counter_destroy(&sbi->s_freeblocks_counter); 1245 percpu_counter_destroy(&sbi->s_freeinodes_counter); 1246 percpu_counter_destroy(&sbi->s_dirs_counter); 1247 failed_mount2: 1248 for (i = 0; i < db_count; i++) 1249 brelse(sbi->s_group_desc[i]); 1250 failed_mount_group_desc: 1251 kvfree(sbi->s_group_desc); 1252 kfree(sbi->s_debts); 1253 failed_mount: 1254 brelse(bh); 1255 failed_sbi: 1256 fs_put_dax(sbi->s_daxdev, NULL); 1257 sb->s_fs_info = NULL; 1258 kfree(sbi->s_blockgroup_lock); 1259 kfree(sbi); 1260 return ret; 1261 } 1262 1263 static void ext2_clear_super_error(struct super_block *sb) 1264 { 1265 struct buffer_head *sbh = EXT2_SB(sb)->s_sbh; 1266 1267 if (buffer_write_io_error(sbh)) { 1268 /* 1269 * Oh, dear. A previous attempt to write the 1270 * superblock failed. This could happen because the 1271 * USB device was yanked out. Or it could happen to 1272 * be a transient write error and maybe the block will 1273 * be remapped. Nothing we can do but to retry the 1274 * write and hope for the best. 1275 */ 1276 ext2_msg(sb, KERN_ERR, 1277 "previous I/O error to superblock detected"); 1278 clear_buffer_write_io_error(sbh); 1279 set_buffer_uptodate(sbh); 1280 } 1281 } 1282 1283 void ext2_sync_super(struct super_block *sb, struct ext2_super_block *es, 1284 int wait) 1285 { 1286 ext2_clear_super_error(sb); 1287 spin_lock(&EXT2_SB(sb)->s_lock); 1288 es->s_free_blocks_count = cpu_to_le32(ext2_count_free_blocks(sb)); 1289 es->s_free_inodes_count = cpu_to_le32(ext2_count_free_inodes(sb)); 1290 es->s_wtime = cpu_to_le32(ktime_get_real_seconds()); 1291 /* unlock before we do IO */ 1292 spin_unlock(&EXT2_SB(sb)->s_lock); 1293 mark_buffer_dirty(EXT2_SB(sb)->s_sbh); 1294 if (wait) 1295 sync_dirty_buffer(EXT2_SB(sb)->s_sbh); 1296 } 1297 1298 /* 1299 * In the second extended file system, it is not necessary to 1300 * write the super block since we use a mapping of the 1301 * disk super block in a buffer. 1302 * 1303 * However, this function is still used to set the fs valid 1304 * flags to 0. We need to set this flag to 0 since the fs 1305 * may have been checked while mounted and e2fsck may have 1306 * set s_state to EXT2_VALID_FS after some corrections. 1307 */ 1308 static int ext2_sync_fs(struct super_block *sb, int wait) 1309 { 1310 struct ext2_sb_info *sbi = EXT2_SB(sb); 1311 struct ext2_super_block *es = EXT2_SB(sb)->s_es; 1312 1313 /* 1314 * Write quota structures to quota file, sync_blockdev() will write 1315 * them to disk later 1316 */ 1317 dquot_writeback_dquots(sb, -1); 1318 1319 spin_lock(&sbi->s_lock); 1320 if (es->s_state & cpu_to_le16(EXT2_VALID_FS)) { 1321 ext2_debug("setting valid to 0\n"); 1322 es->s_state &= cpu_to_le16(~EXT2_VALID_FS); 1323 } 1324 spin_unlock(&sbi->s_lock); 1325 ext2_sync_super(sb, es, wait); 1326 return 0; 1327 } 1328 1329 static int ext2_freeze(struct super_block *sb) 1330 { 1331 struct ext2_sb_info *sbi = EXT2_SB(sb); 1332 1333 /* 1334 * Open but unlinked files present? Keep EXT2_VALID_FS flag cleared 1335 * because we have unattached inodes and thus filesystem is not fully 1336 * consistent. 1337 */ 1338 if (atomic_long_read(&sb->s_remove_count)) { 1339 ext2_sync_fs(sb, 1); 1340 return 0; 1341 } 1342 /* Set EXT2_FS_VALID flag */ 1343 spin_lock(&sbi->s_lock); 1344 sbi->s_es->s_state = cpu_to_le16(sbi->s_mount_state); 1345 spin_unlock(&sbi->s_lock); 1346 ext2_sync_super(sb, sbi->s_es, 1); 1347 1348 return 0; 1349 } 1350 1351 static int ext2_unfreeze(struct super_block *sb) 1352 { 1353 /* Just write sb to clear EXT2_VALID_FS flag */ 1354 ext2_write_super(sb); 1355 1356 return 0; 1357 } 1358 1359 static void ext2_write_super(struct super_block *sb) 1360 { 1361 if (!sb_rdonly(sb)) 1362 ext2_sync_fs(sb, 1); 1363 } 1364 1365 static int ext2_reconfigure(struct fs_context *fc) 1366 { 1367 struct ext2_fs_context *ctx = fc->fs_private; 1368 struct super_block *sb = fc->root->d_sb; 1369 struct ext2_sb_info * sbi = EXT2_SB(sb); 1370 struct ext2_super_block * es; 1371 struct ext2_mount_options new_opts; 1372 int flags = fc->sb_flags; 1373 int err; 1374 1375 sync_filesystem(sb); 1376 1377 new_opts.s_mount_opt = ctx->vals_s_mount_opt; 1378 new_opts.s_resuid = ctx->s_resuid; 1379 new_opts.s_resgid = ctx->s_resgid; 1380 1381 spin_lock(&sbi->s_lock); 1382 es = sbi->s_es; 1383 if ((sbi->s_mount_opt ^ new_opts.s_mount_opt) & EXT2_MOUNT_DAX) { 1384 ext2_msg(sb, KERN_WARNING, "warning: refusing change of " 1385 "dax flag with busy inodes while remounting"); 1386 new_opts.s_mount_opt ^= EXT2_MOUNT_DAX; 1387 } 1388 if ((bool)(flags & SB_RDONLY) == sb_rdonly(sb)) 1389 goto out_set; 1390 if (flags & SB_RDONLY) { 1391 if (le16_to_cpu(es->s_state) & EXT2_VALID_FS || 1392 !(sbi->s_mount_state & EXT2_VALID_FS)) 1393 goto out_set; 1394 1395 /* 1396 * OK, we are remounting a valid rw partition rdonly, so set 1397 * the rdonly flag and then mark the partition as valid again. 1398 */ 1399 es->s_state = cpu_to_le16(sbi->s_mount_state); 1400 es->s_mtime = cpu_to_le32(ktime_get_real_seconds()); 1401 spin_unlock(&sbi->s_lock); 1402 1403 err = dquot_suspend(sb, -1); 1404 if (err < 0) 1405 return err; 1406 1407 ext2_sync_super(sb, es, 1); 1408 } else { 1409 __le32 ret = EXT2_HAS_RO_COMPAT_FEATURE(sb, 1410 ~EXT2_FEATURE_RO_COMPAT_SUPP); 1411 if (ret) { 1412 spin_unlock(&sbi->s_lock); 1413 ext2_msg(sb, KERN_WARNING, 1414 "warning: couldn't remount RDWR because of " 1415 "unsupported optional features (%x).", 1416 le32_to_cpu(ret)); 1417 return -EROFS; 1418 } 1419 /* 1420 * Mounting a RDONLY partition read-write, so reread and 1421 * store the current valid flag. (It may have been changed 1422 * by e2fsck since we originally mounted the partition.) 1423 */ 1424 sbi->s_mount_state = le16_to_cpu(es->s_state); 1425 if (!ext2_setup_super (sb, es, 0)) 1426 sb->s_flags &= ~SB_RDONLY; 1427 spin_unlock(&sbi->s_lock); 1428 1429 ext2_write_super(sb); 1430 1431 dquot_resume(sb, -1); 1432 } 1433 1434 spin_lock(&sbi->s_lock); 1435 out_set: 1436 sbi->s_mount_opt = new_opts.s_mount_opt; 1437 sbi->s_resuid = new_opts.s_resuid; 1438 sbi->s_resgid = new_opts.s_resgid; 1439 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) | 1440 (test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0); 1441 spin_unlock(&sbi->s_lock); 1442 1443 return 0; 1444 } 1445 1446 static int ext2_statfs (struct dentry * dentry, struct kstatfs * buf) 1447 { 1448 struct super_block *sb = dentry->d_sb; 1449 struct ext2_sb_info *sbi = EXT2_SB(sb); 1450 struct ext2_super_block *es = sbi->s_es; 1451 1452 spin_lock(&sbi->s_lock); 1453 1454 if (test_opt (sb, MINIX_DF)) 1455 sbi->s_overhead_last = 0; 1456 else if (sbi->s_blocks_last != le32_to_cpu(es->s_blocks_count)) { 1457 unsigned long i, overhead = 0; 1458 smp_rmb(); 1459 1460 /* 1461 * Compute the overhead (FS structures). This is constant 1462 * for a given filesystem unless the number of block groups 1463 * changes so we cache the previous value until it does. 1464 */ 1465 1466 /* 1467 * All of the blocks before first_data_block are 1468 * overhead 1469 */ 1470 overhead = le32_to_cpu(es->s_first_data_block); 1471 1472 /* 1473 * Add the overhead attributed to the superblock and 1474 * block group descriptors. If the sparse superblocks 1475 * feature is turned on, then not all groups have this. 1476 */ 1477 for (i = 0; i < sbi->s_groups_count; i++) 1478 overhead += ext2_bg_has_super(sb, i) + 1479 ext2_bg_num_gdb(sb, i); 1480 1481 /* 1482 * Every block group has an inode bitmap, a block 1483 * bitmap, and an inode table. 1484 */ 1485 overhead += (sbi->s_groups_count * 1486 (2 + sbi->s_itb_per_group)); 1487 sbi->s_overhead_last = overhead; 1488 smp_wmb(); 1489 sbi->s_blocks_last = le32_to_cpu(es->s_blocks_count); 1490 } 1491 1492 buf->f_type = EXT2_SUPER_MAGIC; 1493 buf->f_bsize = sb->s_blocksize; 1494 buf->f_blocks = le32_to_cpu(es->s_blocks_count) - sbi->s_overhead_last; 1495 buf->f_bfree = ext2_count_free_blocks(sb); 1496 es->s_free_blocks_count = cpu_to_le32(buf->f_bfree); 1497 buf->f_bavail = buf->f_bfree - le32_to_cpu(es->s_r_blocks_count); 1498 if (buf->f_bfree < le32_to_cpu(es->s_r_blocks_count)) 1499 buf->f_bavail = 0; 1500 buf->f_files = le32_to_cpu(es->s_inodes_count); 1501 buf->f_ffree = ext2_count_free_inodes(sb); 1502 es->s_free_inodes_count = cpu_to_le32(buf->f_ffree); 1503 buf->f_namelen = EXT2_NAME_LEN; 1504 buf->f_fsid = uuid_to_fsid(es->s_uuid); 1505 spin_unlock(&sbi->s_lock); 1506 return 0; 1507 } 1508 1509 static int ext2_get_tree(struct fs_context *fc) 1510 { 1511 return get_tree_bdev(fc, ext2_fill_super); 1512 } 1513 1514 #ifdef CONFIG_QUOTA 1515 1516 /* Read data from quotafile - avoid pagecache and such because we cannot afford 1517 * acquiring the locks... As quota files are never truncated and quota code 1518 * itself serializes the operations (and no one else should touch the files) 1519 * we don't have to be afraid of races */ 1520 static ssize_t ext2_quota_read(struct super_block *sb, int type, char *data, 1521 size_t len, loff_t off) 1522 { 1523 struct inode *inode = sb_dqopt(sb)->files[type]; 1524 sector_t blk = off >> EXT2_BLOCK_SIZE_BITS(sb); 1525 int err = 0; 1526 int offset = off & (sb->s_blocksize - 1); 1527 int tocopy; 1528 size_t toread; 1529 struct buffer_head tmp_bh; 1530 struct buffer_head *bh; 1531 loff_t i_size = i_size_read(inode); 1532 1533 if (off > i_size) 1534 return 0; 1535 if (off+len > i_size) 1536 len = i_size-off; 1537 toread = len; 1538 while (toread > 0) { 1539 tocopy = min_t(size_t, sb->s_blocksize - offset, toread); 1540 1541 tmp_bh.b_state = 0; 1542 tmp_bh.b_size = sb->s_blocksize; 1543 err = ext2_get_block(inode, blk, &tmp_bh, 0); 1544 if (err < 0) 1545 return err; 1546 if (!buffer_mapped(&tmp_bh)) /* A hole? */ 1547 memset(data, 0, tocopy); 1548 else { 1549 bh = sb_bread(sb, tmp_bh.b_blocknr); 1550 if (!bh) 1551 return -EIO; 1552 memcpy(data, bh->b_data+offset, tocopy); 1553 brelse(bh); 1554 } 1555 offset = 0; 1556 toread -= tocopy; 1557 data += tocopy; 1558 blk++; 1559 } 1560 return len; 1561 } 1562 1563 /* Write to quotafile */ 1564 static ssize_t ext2_quota_write(struct super_block *sb, int type, 1565 const char *data, size_t len, loff_t off) 1566 { 1567 struct inode *inode = sb_dqopt(sb)->files[type]; 1568 sector_t blk = off >> EXT2_BLOCK_SIZE_BITS(sb); 1569 int err = 0; 1570 int offset = off & (sb->s_blocksize - 1); 1571 int tocopy; 1572 size_t towrite = len; 1573 struct buffer_head tmp_bh; 1574 struct buffer_head *bh; 1575 1576 while (towrite > 0) { 1577 tocopy = min_t(size_t, sb->s_blocksize - offset, towrite); 1578 1579 tmp_bh.b_state = 0; 1580 tmp_bh.b_size = sb->s_blocksize; 1581 err = ext2_get_block(inode, blk, &tmp_bh, 1); 1582 if (err < 0) 1583 goto out; 1584 if (offset || tocopy != EXT2_BLOCK_SIZE(sb)) 1585 bh = sb_bread(sb, tmp_bh.b_blocknr); 1586 else 1587 bh = sb_getblk(sb, tmp_bh.b_blocknr); 1588 if (unlikely(!bh)) { 1589 err = -EIO; 1590 goto out; 1591 } 1592 lock_buffer(bh); 1593 memcpy(bh->b_data+offset, data, tocopy); 1594 flush_dcache_folio(bh->b_folio); 1595 set_buffer_uptodate(bh); 1596 mark_buffer_dirty(bh); 1597 unlock_buffer(bh); 1598 brelse(bh); 1599 offset = 0; 1600 towrite -= tocopy; 1601 data += tocopy; 1602 blk++; 1603 } 1604 out: 1605 if (len == towrite) 1606 return err; 1607 if (inode->i_size < off+len-towrite) 1608 i_size_write(inode, off+len-towrite); 1609 inode_inc_iversion(inode); 1610 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); 1611 mark_inode_dirty(inode); 1612 return len - towrite; 1613 } 1614 1615 static int ext2_quota_on(struct super_block *sb, int type, int format_id, 1616 const struct path *path) 1617 { 1618 int err; 1619 struct inode *inode; 1620 1621 err = dquot_quota_on(sb, type, format_id, path); 1622 if (err) 1623 return err; 1624 1625 inode = d_inode(path->dentry); 1626 inode_lock(inode); 1627 EXT2_I(inode)->i_flags |= EXT2_NOATIME_FL | EXT2_IMMUTABLE_FL; 1628 inode_set_flags(inode, S_NOATIME | S_IMMUTABLE, 1629 S_NOATIME | S_IMMUTABLE); 1630 inode_unlock(inode); 1631 mark_inode_dirty(inode); 1632 1633 return 0; 1634 } 1635 1636 static int ext2_quota_off(struct super_block *sb, int type) 1637 { 1638 struct inode *inode = sb_dqopt(sb)->files[type]; 1639 int err; 1640 1641 if (!inode || !igrab(inode)) 1642 goto out; 1643 1644 err = dquot_quota_off(sb, type); 1645 if (err) 1646 goto out_put; 1647 1648 inode_lock(inode); 1649 EXT2_I(inode)->i_flags &= ~(EXT2_NOATIME_FL | EXT2_IMMUTABLE_FL); 1650 inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE); 1651 inode_unlock(inode); 1652 mark_inode_dirty(inode); 1653 out_put: 1654 iput(inode); 1655 return err; 1656 out: 1657 return dquot_quota_off(sb, type); 1658 } 1659 1660 #endif 1661 1662 static const struct fs_context_operations ext2_context_ops = { 1663 .parse_param = ext2_parse_param, 1664 .get_tree = ext2_get_tree, 1665 .reconfigure = ext2_reconfigure, 1666 .free = ext2_free_fc, 1667 }; 1668 1669 static int ext2_init_fs_context(struct fs_context *fc) 1670 { 1671 struct ext2_fs_context *ctx; 1672 1673 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 1674 if (!ctx) 1675 return -ENOMEM; 1676 1677 if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) { 1678 struct super_block *sb = fc->root->d_sb; 1679 struct ext2_sb_info *sbi = EXT2_SB(sb); 1680 1681 spin_lock(&sbi->s_lock); 1682 ctx->vals_s_mount_opt = sbi->s_mount_opt; 1683 ctx->vals_s_flags = sb->s_flags; 1684 ctx->s_resuid = sbi->s_resuid; 1685 ctx->s_resgid = sbi->s_resgid; 1686 spin_unlock(&sbi->s_lock); 1687 } else { 1688 ctx->s_sb_block = 1; 1689 ctx_set_mount_opt(ctx, EXT2_MOUNT_RESERVATION); 1690 } 1691 1692 fc->fs_private = ctx; 1693 fc->ops = &ext2_context_ops; 1694 1695 return 0; 1696 } 1697 1698 static struct file_system_type ext2_fs_type = { 1699 .owner = THIS_MODULE, 1700 .name = "ext2", 1701 .kill_sb = kill_block_super, 1702 .fs_flags = FS_REQUIRES_DEV, 1703 .init_fs_context = ext2_init_fs_context, 1704 .parameters = ext2_param_spec, 1705 }; 1706 MODULE_ALIAS_FS("ext2"); 1707 1708 static int __init init_ext2_fs(void) 1709 { 1710 int err; 1711 1712 err = init_inodecache(); 1713 if (err) 1714 return err; 1715 err = register_filesystem(&ext2_fs_type); 1716 if (err) 1717 goto out; 1718 return 0; 1719 out: 1720 destroy_inodecache(); 1721 return err; 1722 } 1723 1724 static void __exit exit_ext2_fs(void) 1725 { 1726 unregister_filesystem(&ext2_fs_type); 1727 destroy_inodecache(); 1728 } 1729 1730 MODULE_AUTHOR("Remy Card and others"); 1731 MODULE_DESCRIPTION("Second Extended Filesystem"); 1732 MODULE_LICENSE("GPL"); 1733 module_init(init_ext2_fs) 1734 module_exit(exit_ext2_fs) 1735