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