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