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