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