1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * linux/fs/ext4/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/time.h> 24 #include <linux/vmalloc.h> 25 #include <linux/slab.h> 26 #include <linux/init.h> 27 #include <linux/blkdev.h> 28 #include <linux/backing-dev.h> 29 #include <linux/parser.h> 30 #include <linux/buffer_head.h> 31 #include <linux/exportfs.h> 32 #include <linux/vfs.h> 33 #include <linux/random.h> 34 #include <linux/mount.h> 35 #include <linux/namei.h> 36 #include <linux/quotaops.h> 37 #include <linux/seq_file.h> 38 #include <linux/ctype.h> 39 #include <linux/log2.h> 40 #include <linux/crc16.h> 41 #include <linux/dax.h> 42 #include <linux/uaccess.h> 43 #include <linux/iversion.h> 44 #include <linux/unicode.h> 45 #include <linux/part_stat.h> 46 #include <linux/kthread.h> 47 #include <linux/freezer.h> 48 #include <linux/fsnotify.h> 49 #include <linux/fs_context.h> 50 #include <linux/fs_parser.h> 51 #include <linux/fserror.h> 52 53 #include "ext4.h" 54 #include "ext4_extents.h" /* Needed for trace points definition */ 55 #include "ext4_jbd2.h" 56 #include "xattr.h" 57 #include "acl.h" 58 #include "mballoc.h" 59 #include "fsmap.h" 60 61 #define CREATE_TRACE_POINTS 62 #include <trace/events/ext4.h> 63 64 static struct ext4_lazy_init *ext4_li_info; 65 static DEFINE_MUTEX(ext4_li_mtx); 66 static struct ratelimit_state ext4_mount_msg_ratelimit; 67 68 static int ext4_load_journal(struct super_block *, struct ext4_super_block *, 69 unsigned long journal_devnum); 70 static int ext4_show_options(struct seq_file *seq, struct dentry *root); 71 static void ext4_update_super(struct super_block *sb); 72 static int ext4_commit_super(struct super_block *sb); 73 static int ext4_mark_recovery_complete(struct super_block *sb, 74 struct ext4_super_block *es); 75 static int ext4_clear_journal_err(struct super_block *sb, 76 struct ext4_super_block *es); 77 static int ext4_sync_fs(struct super_block *sb, int wait); 78 static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf); 79 static int ext4_unfreeze(struct super_block *sb); 80 static int ext4_freeze(struct super_block *sb); 81 static inline int ext2_feature_set_ok(struct super_block *sb); 82 static inline int ext3_feature_set_ok(struct super_block *sb); 83 static void ext4_unregister_li_request(struct super_block *sb); 84 static void ext4_clear_request_list(void); 85 static struct inode *ext4_get_journal_inode(struct super_block *sb, 86 unsigned int journal_inum); 87 static int ext4_validate_options(struct fs_context *fc); 88 static int ext4_check_opt_consistency(struct fs_context *fc, 89 struct super_block *sb); 90 static void ext4_apply_options(struct fs_context *fc, struct super_block *sb); 91 static int ext4_parse_param(struct fs_context *fc, struct fs_parameter *param); 92 static int ext4_get_tree(struct fs_context *fc); 93 static int ext4_reconfigure(struct fs_context *fc); 94 static void ext4_fc_free(struct fs_context *fc); 95 static int ext4_init_fs_context(struct fs_context *fc); 96 static void ext4_kill_sb(struct super_block *sb); 97 static const struct fs_parameter_spec ext4_param_specs[]; 98 99 /* 100 * Lock ordering 101 * 102 * page fault path: 103 * mmap_lock -> sb_start_pagefault -> invalidate_lock (r) -> transaction start 104 * -> page lock -> i_data_sem (rw) 105 * 106 * buffered write path: 107 * sb_start_write -> i_mutex -> mmap_lock 108 * sb_start_write -> i_mutex -> transaction start -> page lock -> 109 * i_data_sem (rw) 110 * 111 * truncate: 112 * sb_start_write -> i_mutex -> invalidate_lock (w) -> i_mmap_rwsem (w) -> 113 * page lock 114 * sb_start_write -> i_mutex -> invalidate_lock (w) -> transaction start -> 115 * i_data_sem (rw) 116 * 117 * direct IO: 118 * sb_start_write -> i_mutex -> mmap_lock 119 * sb_start_write -> i_mutex -> transaction start -> i_data_sem (rw) 120 * 121 * writepages: 122 * transaction start -> page lock(s) -> i_data_sem (rw) 123 */ 124 125 static const struct fs_context_operations ext4_context_ops = { 126 .parse_param = ext4_parse_param, 127 .get_tree = ext4_get_tree, 128 .reconfigure = ext4_reconfigure, 129 .free = ext4_fc_free, 130 }; 131 132 133 #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2) 134 static struct file_system_type ext2_fs_type = { 135 .owner = THIS_MODULE, 136 .name = "ext2", 137 .init_fs_context = ext4_init_fs_context, 138 .parameters = ext4_param_specs, 139 .kill_sb = ext4_kill_sb, 140 .fs_flags = FS_REQUIRES_DEV, 141 }; 142 MODULE_ALIAS_FS("ext2"); 143 MODULE_ALIAS("ext2"); 144 #define IS_EXT2_SB(sb) ((sb)->s_type == &ext2_fs_type) 145 #else 146 #define IS_EXT2_SB(sb) (0) 147 #endif 148 149 150 static struct file_system_type ext3_fs_type = { 151 .owner = THIS_MODULE, 152 .name = "ext3", 153 .init_fs_context = ext4_init_fs_context, 154 .parameters = ext4_param_specs, 155 .kill_sb = ext4_kill_sb, 156 .fs_flags = FS_REQUIRES_DEV, 157 }; 158 MODULE_ALIAS_FS("ext3"); 159 MODULE_ALIAS("ext3"); 160 #define IS_EXT3_SB(sb) ((sb)->s_type == &ext3_fs_type) 161 162 163 static inline void __ext4_read_bh(struct buffer_head *bh, blk_opf_t op_flags, 164 bio_end_io_t end_io, bool simu_fail) 165 { 166 if (simu_fail) { 167 clear_buffer_uptodate(bh); 168 unlock_buffer(bh); 169 return; 170 } 171 172 /* 173 * buffer's verified bit is no longer valid after reading from 174 * disk again due to write out error, clear it to make sure we 175 * recheck the buffer contents. 176 */ 177 clear_buffer_verified(bh); 178 179 if (!end_io) 180 end_io = bh_end_read; 181 bh_submit(bh, REQ_OP_READ | op_flags, end_io); 182 } 183 184 void ext4_read_bh_nowait(struct buffer_head *bh, blk_opf_t op_flags, 185 bio_end_io_t end_io, bool simu_fail) 186 { 187 BUG_ON(!buffer_locked(bh)); 188 189 if (ext4_buffer_uptodate(bh)) { 190 unlock_buffer(bh); 191 return; 192 } 193 __ext4_read_bh(bh, op_flags, end_io, simu_fail); 194 } 195 196 int ext4_read_bh(struct buffer_head *bh, blk_opf_t op_flags, 197 bio_end_io_t end_io, bool simu_fail) 198 { 199 BUG_ON(!buffer_locked(bh)); 200 201 if (ext4_buffer_uptodate(bh)) { 202 unlock_buffer(bh); 203 return 0; 204 } 205 206 __ext4_read_bh(bh, op_flags, end_io, simu_fail); 207 208 wait_on_buffer(bh); 209 if (buffer_uptodate(bh)) 210 return 0; 211 return -EIO; 212 } 213 214 int ext4_read_bh_lock(struct buffer_head *bh, blk_opf_t op_flags, bool wait) 215 { 216 lock_buffer(bh); 217 if (!wait) { 218 ext4_read_bh_nowait(bh, op_flags, NULL, false); 219 return 0; 220 } 221 return ext4_read_bh(bh, op_flags, NULL, false); 222 } 223 224 /* 225 * This works like __bread_gfp() except it uses ERR_PTR for error 226 * returns. Currently with sb_bread it's impossible to distinguish 227 * between ENOMEM and EIO situations (since both result in a NULL 228 * return. 229 */ 230 static struct buffer_head *__ext4_sb_bread_gfp(struct super_block *sb, 231 sector_t block, 232 blk_opf_t op_flags, gfp_t gfp) 233 { 234 struct buffer_head *bh; 235 int ret; 236 237 bh = sb_getblk_gfp(sb, block, gfp); 238 if (bh == NULL) 239 return ERR_PTR(-ENOMEM); 240 if (ext4_buffer_uptodate(bh)) 241 return bh; 242 243 ret = ext4_read_bh_lock(bh, REQ_META | op_flags, true); 244 if (ret) { 245 put_bh(bh); 246 return ERR_PTR(ret); 247 } 248 return bh; 249 } 250 251 struct buffer_head *ext4_sb_bread(struct super_block *sb, sector_t block, 252 blk_opf_t op_flags) 253 { 254 gfp_t gfp = mapping_gfp_constraint(sb->s_bdev->bd_mapping, 255 ~__GFP_FS) | __GFP_MOVABLE; 256 257 return __ext4_sb_bread_gfp(sb, block, op_flags, gfp); 258 } 259 260 struct buffer_head *ext4_sb_bread_unmovable(struct super_block *sb, 261 sector_t block) 262 { 263 gfp_t gfp = mapping_gfp_constraint(sb->s_bdev->bd_mapping, 264 ~__GFP_FS); 265 266 return __ext4_sb_bread_gfp(sb, block, 0, gfp); 267 } 268 269 struct buffer_head *ext4_sb_bread_nofail(struct super_block *sb, 270 sector_t block) 271 { 272 gfp_t gfp = mapping_gfp_constraint(sb->s_bdev->bd_mapping, 273 ~__GFP_FS) | __GFP_MOVABLE | __GFP_NOFAIL; 274 275 return __ext4_sb_bread_gfp(sb, block, 0, gfp); 276 } 277 278 void ext4_sb_breadahead_unmovable(struct super_block *sb, sector_t block) 279 { 280 struct buffer_head *bh = bdev_getblk(sb->s_bdev, block, 281 sb->s_blocksize, GFP_NOWAIT); 282 283 if (likely(bh)) { 284 if (trylock_buffer(bh)) 285 ext4_read_bh_nowait(bh, REQ_RAHEAD, NULL, false); 286 brelse(bh); 287 } 288 } 289 290 static int ext4_verify_csum_type(struct super_block *sb, 291 struct ext4_super_block *es) 292 { 293 if (!ext4_has_feature_metadata_csum(sb)) 294 return 1; 295 296 return es->s_checksum_type == EXT4_CRC32C_CHKSUM; 297 } 298 299 __le32 ext4_superblock_csum(struct ext4_super_block *es) 300 { 301 int offset = offsetof(struct ext4_super_block, s_checksum); 302 __u32 csum; 303 304 csum = ext4_chksum(~0, (char *)es, offset); 305 306 return cpu_to_le32(csum); 307 } 308 309 static int ext4_superblock_csum_verify(struct super_block *sb, 310 struct ext4_super_block *es) 311 { 312 if (!ext4_has_feature_metadata_csum(sb)) 313 return 1; 314 315 return es->s_checksum == ext4_superblock_csum(es); 316 } 317 318 void ext4_superblock_csum_set(struct super_block *sb) 319 { 320 struct ext4_super_block *es = EXT4_SB(sb)->s_es; 321 322 if (!ext4_has_feature_metadata_csum(sb)) 323 return; 324 325 es->s_checksum = ext4_superblock_csum(es); 326 } 327 328 ext4_fsblk_t ext4_block_bitmap(struct super_block *sb, 329 struct ext4_group_desc *bg) 330 { 331 return le32_to_cpu(bg->bg_block_bitmap_lo) | 332 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? 333 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0); 334 } 335 336 ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb, 337 struct ext4_group_desc *bg) 338 { 339 return le32_to_cpu(bg->bg_inode_bitmap_lo) | 340 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? 341 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0); 342 } 343 344 ext4_fsblk_t ext4_inode_table(struct super_block *sb, 345 struct ext4_group_desc *bg) 346 { 347 return le32_to_cpu(bg->bg_inode_table_lo) | 348 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? 349 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0); 350 } 351 352 __u32 ext4_free_group_clusters(struct super_block *sb, 353 struct ext4_group_desc *bg) 354 { 355 return le16_to_cpu(bg->bg_free_blocks_count_lo) | 356 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? 357 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0); 358 } 359 360 __u32 ext4_free_inodes_count(struct super_block *sb, 361 struct ext4_group_desc *bg) 362 { 363 return le16_to_cpu(READ_ONCE(bg->bg_free_inodes_count_lo)) | 364 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? 365 (__u32)le16_to_cpu(READ_ONCE(bg->bg_free_inodes_count_hi)) << 16 : 0); 366 } 367 368 __u32 ext4_used_dirs_count(struct super_block *sb, 369 struct ext4_group_desc *bg) 370 { 371 return le16_to_cpu(bg->bg_used_dirs_count_lo) | 372 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? 373 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0); 374 } 375 376 __u32 ext4_itable_unused_count(struct super_block *sb, 377 struct ext4_group_desc *bg) 378 { 379 return le16_to_cpu(bg->bg_itable_unused_lo) | 380 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? 381 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0); 382 } 383 384 void ext4_block_bitmap_set(struct super_block *sb, 385 struct ext4_group_desc *bg, ext4_fsblk_t blk) 386 { 387 bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk); 388 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) 389 bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32); 390 } 391 392 void ext4_inode_bitmap_set(struct super_block *sb, 393 struct ext4_group_desc *bg, ext4_fsblk_t blk) 394 { 395 bg->bg_inode_bitmap_lo = cpu_to_le32((u32)blk); 396 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) 397 bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32); 398 } 399 400 void ext4_inode_table_set(struct super_block *sb, 401 struct ext4_group_desc *bg, ext4_fsblk_t blk) 402 { 403 bg->bg_inode_table_lo = cpu_to_le32((u32)blk); 404 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) 405 bg->bg_inode_table_hi = cpu_to_le32(blk >> 32); 406 } 407 408 void ext4_free_group_clusters_set(struct super_block *sb, 409 struct ext4_group_desc *bg, __u32 count) 410 { 411 bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count); 412 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) 413 bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16); 414 } 415 416 void ext4_free_inodes_set(struct super_block *sb, 417 struct ext4_group_desc *bg, __u32 count) 418 { 419 WRITE_ONCE(bg->bg_free_inodes_count_lo, cpu_to_le16((__u16)count)); 420 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) 421 WRITE_ONCE(bg->bg_free_inodes_count_hi, cpu_to_le16(count >> 16)); 422 } 423 424 void ext4_used_dirs_set(struct super_block *sb, 425 struct ext4_group_desc *bg, __u32 count) 426 { 427 bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count); 428 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) 429 bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16); 430 } 431 432 void ext4_itable_unused_set(struct super_block *sb, 433 struct ext4_group_desc *bg, __u32 count) 434 { 435 bg->bg_itable_unused_lo = cpu_to_le16((__u16)count); 436 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) 437 bg->bg_itable_unused_hi = cpu_to_le16(count >> 16); 438 } 439 440 static void __ext4_update_tstamp(__le32 *lo, __u8 *hi, time64_t now) 441 { 442 now = clamp_val(now, 0, (1ull << 40) - 1); 443 444 *lo = cpu_to_le32(lower_32_bits(now)); 445 *hi = upper_32_bits(now); 446 } 447 448 static time64_t __ext4_get_tstamp(__le32 *lo, __u8 *hi) 449 { 450 return ((time64_t)(*hi) << 32) + le32_to_cpu(*lo); 451 } 452 #define ext4_update_tstamp(es, tstamp) \ 453 __ext4_update_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi, \ 454 ktime_get_real_seconds()) 455 #define ext4_get_tstamp(es, tstamp) \ 456 __ext4_get_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi) 457 458 /* 459 * The ext4_maybe_update_superblock() function checks and updates the 460 * superblock if needed. 461 * 462 * This function is designed to update the on-disk superblock only under 463 * certain conditions to prevent excessive disk writes and unnecessary 464 * waking of the disk from sleep. The superblock will be updated if: 465 * 1. More than sbi->s_sb_update_sec (def: 1 hour) has passed since the last 466 * superblock update 467 * 2. More than sbi->s_sb_update_kb (def: 16MB) kbs have been written since the 468 * last superblock update. 469 * 470 * @sb: The superblock 471 */ 472 static void ext4_maybe_update_superblock(struct super_block *sb) 473 { 474 struct ext4_sb_info *sbi = EXT4_SB(sb); 475 struct ext4_super_block *es = sbi->s_es; 476 journal_t *journal = sbi->s_journal; 477 time64_t now; 478 __u64 last_update; 479 __u64 lifetime_write_kbytes; 480 __u64 diff_size; 481 482 if (ext4_emergency_state(sb) || sb_rdonly(sb) || 483 !(sb->s_flags & SB_ACTIVE) || !journal || 484 journal->j_flags & JBD2_UNMOUNT) 485 return; 486 487 now = ktime_get_real_seconds(); 488 last_update = ext4_get_tstamp(es, s_wtime); 489 490 if (likely(now - last_update < sbi->s_sb_update_sec)) 491 return; 492 493 lifetime_write_kbytes = sbi->s_kbytes_written + 494 ((part_stat_read(sb->s_bdev, sectors[STAT_WRITE]) - 495 sbi->s_sectors_written_start) >> 1); 496 497 /* Get the number of kilobytes not written to disk to account 498 * for statistics and compare with a multiple of 16 MB. This 499 * is used to determine when the next superblock commit should 500 * occur (i.e. not more often than once per 16MB if there was 501 * less written in an hour). 502 */ 503 diff_size = lifetime_write_kbytes - le64_to_cpu(es->s_kbytes_written); 504 505 if (diff_size > sbi->s_sb_update_kb) 506 schedule_work(&EXT4_SB(sb)->s_sb_upd_work); 507 } 508 509 static void ext4_journal_commit_callback(journal_t *journal, transaction_t *txn) 510 { 511 struct super_block *sb = journal->j_private; 512 513 BUG_ON(txn->t_state == T_FINISHED); 514 515 ext4_process_freed_data(sb, txn->t_tid); 516 ext4_maybe_update_superblock(sb); 517 } 518 519 static bool ext4_journalled_writepage_needs_redirty(struct jbd2_inode *jinode, 520 struct folio *folio) 521 { 522 struct buffer_head *bh, *head; 523 struct journal_head *jh; 524 transaction_t *trans = READ_ONCE(jinode->i_transaction); 525 526 bh = head = folio_buffers(folio); 527 do { 528 /* 529 * We have to redirty a page in these cases: 530 * 1) If buffer is dirty, it means the page was dirty because it 531 * contains a buffer that needs checkpointing. So the dirty bit 532 * needs to be preserved so that checkpointing writes the buffer 533 * properly. 534 * 2) If buffer is not part of the committing transaction 535 * (we may have just accidentally come across this buffer because 536 * inode range tracking is not exact) or if the currently running 537 * transaction already contains this buffer as well, dirty bit 538 * needs to be preserved so that the buffer gets writeprotected 539 * properly on running transaction's commit. 540 */ 541 jh = bh2jh(bh); 542 if (buffer_dirty(bh) || 543 (jh && (jh->b_transaction != trans || 544 jh->b_next_transaction))) 545 return true; 546 } while ((bh = bh->b_this_page) != head); 547 548 return false; 549 } 550 551 static int ext4_journalled_submit_inode_data_buffers(struct jbd2_inode *jinode) 552 { 553 struct address_space *mapping = jinode->i_vfs_inode->i_mapping; 554 loff_t range_start, range_end; 555 struct writeback_control wbc = { 556 .sync_mode = WB_SYNC_ALL, 557 .nr_to_write = LONG_MAX, 558 }; 559 struct folio *folio = NULL; 560 int error; 561 562 if (!jbd2_jinode_get_dirty_range(jinode, &range_start, &range_end)) 563 return 0; 564 565 wbc.range_start = range_start; 566 wbc.range_end = range_end; 567 568 /* 569 * writeback_iter() already checks for dirty pages and calls 570 * folio_clear_dirty_for_io(), which we want to write protect the 571 * folios. 572 * 573 * However, we may have to redirty a folio sometimes. 574 */ 575 while ((folio = writeback_iter(mapping, &wbc, folio, &error))) { 576 if (ext4_journalled_writepage_needs_redirty(jinode, folio)) 577 folio_redirty_for_writepage(&wbc, folio); 578 folio_unlock(folio); 579 } 580 581 return error; 582 } 583 584 static int ext4_journal_submit_inode_data_buffers(struct jbd2_inode *jinode) 585 { 586 int ret; 587 588 if (ext4_should_journal_data(jinode->i_vfs_inode)) 589 ret = ext4_journalled_submit_inode_data_buffers(jinode); 590 else 591 ret = ext4_normal_submit_inode_data_buffers(jinode); 592 return ret; 593 } 594 595 static int ext4_journal_finish_inode_data_buffers(struct jbd2_inode *jinode) 596 { 597 int ret = 0; 598 599 if (!ext4_should_journal_data(jinode->i_vfs_inode)) 600 ret = jbd2_journal_finish_inode_data_buffers(jinode); 601 602 return ret; 603 } 604 605 static bool system_going_down(void) 606 { 607 return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF 608 || system_state == SYSTEM_RESTART; 609 } 610 611 struct ext4_err_translation { 612 int code; 613 int errno; 614 }; 615 616 #define EXT4_ERR_TRANSLATE(err) { .code = EXT4_ERR_##err, .errno = err } 617 618 static struct ext4_err_translation err_translation[] = { 619 EXT4_ERR_TRANSLATE(EIO), 620 EXT4_ERR_TRANSLATE(ENOMEM), 621 EXT4_ERR_TRANSLATE(EFSBADCRC), 622 EXT4_ERR_TRANSLATE(EFSCORRUPTED), 623 EXT4_ERR_TRANSLATE(ENOSPC), 624 EXT4_ERR_TRANSLATE(ENOKEY), 625 EXT4_ERR_TRANSLATE(EROFS), 626 EXT4_ERR_TRANSLATE(EFBIG), 627 EXT4_ERR_TRANSLATE(EEXIST), 628 EXT4_ERR_TRANSLATE(ERANGE), 629 EXT4_ERR_TRANSLATE(EOVERFLOW), 630 EXT4_ERR_TRANSLATE(EBUSY), 631 EXT4_ERR_TRANSLATE(ENOTDIR), 632 EXT4_ERR_TRANSLATE(ENOTEMPTY), 633 EXT4_ERR_TRANSLATE(ESHUTDOWN), 634 EXT4_ERR_TRANSLATE(EFAULT), 635 }; 636 637 static int ext4_errno_to_code(int errno) 638 { 639 int i; 640 641 for (i = 0; i < ARRAY_SIZE(err_translation); i++) 642 if (err_translation[i].errno == errno) 643 return err_translation[i].code; 644 return EXT4_ERR_UNKNOWN; 645 } 646 647 static void save_error_info(struct super_block *sb, int error, 648 __u32 ino, __u64 block, 649 const char *func, unsigned int line) 650 { 651 struct ext4_sb_info *sbi = EXT4_SB(sb); 652 653 /* We default to EFSCORRUPTED error... */ 654 if (error == 0) 655 error = EFSCORRUPTED; 656 657 spin_lock(&sbi->s_error_lock); 658 sbi->s_add_error_count++; 659 sbi->s_last_error_code = error; 660 sbi->s_last_error_line = line; 661 sbi->s_last_error_ino = ino; 662 sbi->s_last_error_block = block; 663 sbi->s_last_error_func = func; 664 sbi->s_last_error_time = ktime_get_real_seconds(); 665 if (!sbi->s_first_error_time) { 666 sbi->s_first_error_code = error; 667 sbi->s_first_error_line = line; 668 sbi->s_first_error_ino = ino; 669 sbi->s_first_error_block = block; 670 sbi->s_first_error_func = func; 671 sbi->s_first_error_time = sbi->s_last_error_time; 672 } 673 spin_unlock(&sbi->s_error_lock); 674 } 675 676 /* Deal with the reporting of failure conditions on a filesystem such as 677 * inconsistencies detected or read IO failures. 678 * 679 * On ext2, we can store the error state of the filesystem in the 680 * superblock. That is not possible on ext4, because we may have other 681 * write ordering constraints on the superblock which prevent us from 682 * writing it out straight away; and given that the journal is about to 683 * be aborted, we can't rely on the current, or future, transactions to 684 * write out the superblock safely. 685 * 686 * We'll just use the jbd2_journal_abort() error code to record an error in 687 * the journal instead. On recovery, the journal will complain about 688 * that error until we've noted it down and cleared it. 689 * 690 * If force_ro is set, we unconditionally force the filesystem into an 691 * ABORT|READONLY state, unless the error response on the fs has been set to 692 * panic in which case we take the easy way out and panic immediately. This is 693 * used to deal with unrecoverable failures such as journal IO errors or ENOMEM 694 * at a critical moment in log management. 695 */ 696 static void ext4_handle_error(struct super_block *sb, bool force_ro, int error, 697 __u32 ino, __u64 block, 698 const char *func, unsigned int line) 699 { 700 journal_t *journal = EXT4_SB(sb)->s_journal; 701 bool continue_fs = !force_ro && test_opt(sb, ERRORS_CONT); 702 703 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS; 704 if (test_opt(sb, WARN_ON_ERROR)) 705 WARN_ON_ONCE(1); 706 707 if (!continue_fs && !ext4_emergency_ro(sb) && journal) 708 jbd2_journal_abort(journal, -error); 709 710 if (!bdev_read_only(sb->s_bdev)) { 711 save_error_info(sb, error, ino, block, func, line); 712 /* 713 * In case the fs should keep running, we need to writeout 714 * superblock through the journal. Due to lock ordering 715 * constraints, it may not be safe to do it right here so we 716 * defer superblock flushing to a workqueue. We just need to be 717 * careful when the journal is already shutting down. If we get 718 * here in that case, just update the sb directly as the last 719 * transaction won't commit anyway. 720 */ 721 if (continue_fs && journal && 722 !ext4_test_mount_flag(sb, EXT4_MF_JOURNAL_DESTROY)) 723 schedule_work(&EXT4_SB(sb)->s_sb_upd_work); 724 else 725 ext4_commit_super(sb); 726 } 727 728 /* 729 * We force ERRORS_RO behavior when system is rebooting. Otherwise we 730 * could panic during 'reboot -f' as the underlying device got already 731 * disabled. 732 */ 733 if (test_opt(sb, ERRORS_PANIC) && !system_going_down()) { 734 panic("EXT4-fs (device %s): panic forced after error\n", 735 sb->s_id); 736 } 737 738 if (ext4_emergency_ro(sb) || continue_fs) 739 return; 740 741 ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only"); 742 /* 743 * We don't set SB_RDONLY because that requires sb->s_umount 744 * semaphore and setting it without proper remount procedure is 745 * confusing code such as freeze_super() leading to deadlocks 746 * and other problems. 747 */ 748 set_bit(EXT4_FLAGS_EMERGENCY_RO, &EXT4_SB(sb)->s_ext4_flags); 749 } 750 751 static void update_super_work(struct work_struct *work) 752 { 753 struct ext4_sb_info *sbi = container_of(work, struct ext4_sb_info, 754 s_sb_upd_work); 755 journal_t *journal = sbi->s_journal; 756 handle_t *handle; 757 758 /* 759 * If the journal is still running, we have to write out superblock 760 * through the journal to avoid collisions of other journalled sb 761 * updates. 762 * 763 * We use directly jbd2 functions here to avoid recursing back into 764 * ext4 error handling code during handling of previous errors. 765 */ 766 if (!ext4_emergency_state(sbi->s_sb) && 767 !sb_rdonly(sbi->s_sb) && journal) { 768 struct buffer_head *sbh = sbi->s_sbh; 769 bool call_notify_err = false; 770 771 handle = jbd2_journal_start(journal, 1); 772 if (IS_ERR(handle)) 773 goto write_directly; 774 if (jbd2_journal_get_write_access(handle, sbh)) { 775 jbd2_journal_stop(handle); 776 goto write_directly; 777 } 778 779 if (sbi->s_add_error_count > 0) 780 call_notify_err = true; 781 782 ext4_update_super(sbi->s_sb); 783 if (buffer_write_io_error(sbh) || !buffer_uptodate(sbh)) { 784 ext4_msg(sbi->s_sb, KERN_ERR, "previous I/O error to " 785 "superblock detected"); 786 clear_buffer_write_io_error(sbh); 787 set_buffer_uptodate(sbh); 788 } 789 790 if (jbd2_journal_dirty_metadata(handle, sbh)) { 791 jbd2_journal_stop(handle); 792 goto write_directly; 793 } 794 jbd2_journal_stop(handle); 795 796 if (call_notify_err) 797 ext4_notify_error_sysfs(sbi); 798 799 return; 800 } 801 write_directly: 802 /* 803 * Write through journal failed. Write sb directly to get error info 804 * out and hope for the best. 805 */ 806 ext4_commit_super(sbi->s_sb); 807 ext4_notify_error_sysfs(sbi); 808 } 809 810 #define ext4_error_ratelimit(sb) \ 811 ___ratelimit(&(EXT4_SB(sb)->s_err_ratelimit_state), \ 812 "EXT4-fs error") 813 814 void __ext4_error(struct super_block *sb, const char *function, 815 unsigned int line, bool force_ro, int error, __u64 block, 816 const char *fmt, ...) 817 { 818 struct va_format vaf; 819 va_list args; 820 821 if (unlikely(ext4_emergency_state(sb))) 822 return; 823 824 trace_ext4_error(sb, function, line); 825 if (ext4_error_ratelimit(sb)) { 826 va_start(args, fmt); 827 vaf.fmt = fmt; 828 vaf.va = &args; 829 printk(KERN_CRIT 830 "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n", 831 sb->s_id, function, line, current->comm, &vaf); 832 va_end(args); 833 } 834 fserror_report_metadata(sb, error ? -abs(error) : -EFSCORRUPTED, 835 GFP_ATOMIC); 836 837 ext4_handle_error(sb, force_ro, error, 0, block, function, line); 838 } 839 840 void __ext4_error_inode(struct inode *inode, const char *function, 841 unsigned int line, ext4_fsblk_t block, int error, 842 const char *fmt, ...) 843 { 844 va_list args; 845 struct va_format vaf; 846 847 if (unlikely(ext4_emergency_state(inode->i_sb))) 848 return; 849 850 trace_ext4_error(inode->i_sb, function, line); 851 if (ext4_error_ratelimit(inode->i_sb)) { 852 va_start(args, fmt); 853 vaf.fmt = fmt; 854 vaf.va = &args; 855 if (block) 856 printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: " 857 "inode #%llu: block %llu: comm %s: %pV\n", 858 inode->i_sb->s_id, function, line, inode->i_ino, 859 block, current->comm, &vaf); 860 else 861 printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: " 862 "inode #%llu: comm %s: %pV\n", 863 inode->i_sb->s_id, function, line, inode->i_ino, 864 current->comm, &vaf); 865 va_end(args); 866 } 867 fserror_report_file_metadata(inode, 868 error ? -abs(error) : -EFSCORRUPTED, 869 GFP_ATOMIC); 870 871 ext4_handle_error(inode->i_sb, false, error, inode->i_ino, block, 872 function, line); 873 } 874 875 void __ext4_error_file(struct file *file, const char *function, 876 unsigned int line, ext4_fsblk_t block, 877 const char *fmt, ...) 878 { 879 va_list args; 880 struct va_format vaf; 881 struct inode *inode = file_inode(file); 882 char pathname[80], *path; 883 884 if (unlikely(ext4_emergency_state(inode->i_sb))) 885 return; 886 887 trace_ext4_error(inode->i_sb, function, line); 888 if (ext4_error_ratelimit(inode->i_sb)) { 889 path = file_path(file, pathname, sizeof(pathname)); 890 if (IS_ERR(path)) 891 path = "(unknown)"; 892 va_start(args, fmt); 893 vaf.fmt = fmt; 894 vaf.va = &args; 895 if (block) 896 printk(KERN_CRIT 897 "EXT4-fs error (device %s): %s:%d: inode #%llu: " 898 "block %llu: comm %s: path %s: %pV\n", 899 inode->i_sb->s_id, function, line, inode->i_ino, 900 block, current->comm, path, &vaf); 901 else 902 printk(KERN_CRIT 903 "EXT4-fs error (device %s): %s:%d: inode #%llu: " 904 "comm %s: path %s: %pV\n", 905 inode->i_sb->s_id, function, line, inode->i_ino, 906 current->comm, path, &vaf); 907 va_end(args); 908 } 909 fserror_report_file_metadata(inode, -EFSCORRUPTED, GFP_ATOMIC); 910 911 ext4_handle_error(inode->i_sb, false, EFSCORRUPTED, inode->i_ino, block, 912 function, line); 913 } 914 915 const char *ext4_decode_error(struct super_block *sb, int errno, 916 char nbuf[16]) 917 { 918 char *errstr = NULL; 919 920 switch (errno) { 921 case -EFSCORRUPTED: 922 errstr = "Corrupt filesystem"; 923 break; 924 case -EFSBADCRC: 925 errstr = "Filesystem failed CRC"; 926 break; 927 case -EIO: 928 errstr = "IO failure"; 929 break; 930 case -ENOMEM: 931 errstr = "Out of memory"; 932 break; 933 case -EROFS: 934 if (!sb || (EXT4_SB(sb)->s_journal && 935 EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT)) 936 errstr = "Journal has aborted"; 937 else 938 errstr = "Readonly filesystem"; 939 break; 940 default: 941 /* If the caller passed in an extra buffer for unknown 942 * errors, textualise them now. Else we just return 943 * NULL. */ 944 if (nbuf) { 945 /* Check for truncated error codes... */ 946 if (snprintf(nbuf, 16, "error %d", -errno) >= 0) 947 errstr = nbuf; 948 } 949 break; 950 } 951 952 return errstr; 953 } 954 955 /* __ext4_std_error decodes expected errors from journaling functions 956 * automatically and invokes the appropriate error response. */ 957 958 void __ext4_std_error(struct super_block *sb, const char *function, 959 unsigned int line, int errno) 960 { 961 char nbuf[16]; 962 const char *errstr; 963 964 if (unlikely(ext4_emergency_state(sb))) 965 return; 966 967 /* Special case: if the error is EROFS, and we're not already 968 * inside a transaction, then there's really no point in logging 969 * an error. */ 970 if (errno == -EROFS && journal_current_handle() == NULL && sb_rdonly(sb)) 971 return; 972 973 if (ext4_error_ratelimit(sb)) { 974 errstr = ext4_decode_error(sb, errno, nbuf); 975 printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n", 976 sb->s_id, function, line, errstr); 977 } 978 fserror_report_metadata(sb, errno ? -abs(errno) : -EFSCORRUPTED, 979 GFP_ATOMIC); 980 981 ext4_handle_error(sb, false, -errno, 0, 0, function, line); 982 } 983 984 void __ext4_msg(struct super_block *sb, 985 const char *prefix, const char *fmt, ...) 986 { 987 struct va_format vaf; 988 va_list args; 989 990 if (sb) { 991 atomic_inc(&EXT4_SB(sb)->s_msg_count); 992 if (!___ratelimit(&(EXT4_SB(sb)->s_msg_ratelimit_state), 993 "EXT4-fs")) 994 return; 995 } 996 997 va_start(args, fmt); 998 vaf.fmt = fmt; 999 vaf.va = &args; 1000 if (sb) 1001 printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf); 1002 else 1003 printk("%sEXT4-fs: %pV\n", prefix, &vaf); 1004 va_end(args); 1005 } 1006 1007 static int ext4_warning_ratelimit(struct super_block *sb) 1008 { 1009 atomic_inc(&EXT4_SB(sb)->s_warning_count); 1010 return ___ratelimit(&(EXT4_SB(sb)->s_warning_ratelimit_state), 1011 "EXT4-fs warning"); 1012 } 1013 1014 void __ext4_warning(struct super_block *sb, const char *function, 1015 unsigned int line, const char *fmt, ...) 1016 { 1017 struct va_format vaf; 1018 va_list args; 1019 1020 if (!ext4_warning_ratelimit(sb)) 1021 return; 1022 1023 va_start(args, fmt); 1024 vaf.fmt = fmt; 1025 vaf.va = &args; 1026 printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n", 1027 sb->s_id, function, line, &vaf); 1028 va_end(args); 1029 } 1030 1031 void __ext4_warning_inode(const struct inode *inode, const char *function, 1032 unsigned int line, const char *fmt, ...) 1033 { 1034 struct va_format vaf; 1035 va_list args; 1036 1037 if (!ext4_warning_ratelimit(inode->i_sb)) 1038 return; 1039 1040 va_start(args, fmt); 1041 vaf.fmt = fmt; 1042 vaf.va = &args; 1043 printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: " 1044 "inode #%llu: comm %s: %pV\n", inode->i_sb->s_id, 1045 function, line, inode->i_ino, current->comm, &vaf); 1046 va_end(args); 1047 } 1048 1049 void __ext4_grp_locked_error(const char *function, unsigned int line, 1050 struct super_block *sb, ext4_group_t grp, 1051 u64 ino, ext4_fsblk_t block, 1052 const char *fmt, ...) 1053 __releases(bitlock) 1054 __acquires(bitlock) 1055 { 1056 struct va_format vaf; 1057 va_list args; 1058 1059 if (unlikely(ext4_emergency_state(sb))) 1060 return; 1061 1062 trace_ext4_error(sb, function, line); 1063 if (ext4_error_ratelimit(sb)) { 1064 va_start(args, fmt); 1065 vaf.fmt = fmt; 1066 vaf.va = &args; 1067 printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ", 1068 sb->s_id, function, line, grp); 1069 if (ino) 1070 printk(KERN_CONT "inode %llu: ", ino); 1071 if (block) 1072 printk(KERN_CONT "block %llu:", 1073 (unsigned long long) block); 1074 printk(KERN_CONT "%pV\n", &vaf); 1075 va_end(args); 1076 } 1077 1078 if (test_opt(sb, ERRORS_CONT)) { 1079 if (test_opt(sb, WARN_ON_ERROR)) 1080 WARN_ON_ONCE(1); 1081 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS; 1082 if (!bdev_read_only(sb->s_bdev)) { 1083 save_error_info(sb, EFSCORRUPTED, ino, block, function, 1084 line); 1085 schedule_work(&EXT4_SB(sb)->s_sb_upd_work); 1086 } 1087 return; 1088 } 1089 ext4_unlock_group(sb, grp); 1090 ext4_handle_error(sb, false, EFSCORRUPTED, ino, block, function, line); 1091 /* 1092 * We only get here in the ERRORS_RO case; relocking the group 1093 * may be dangerous, but nothing bad will happen since the 1094 * filesystem will have already been marked read/only and the 1095 * journal has been aborted. We return 1 as a hint to callers 1096 * who might what to use the return value from 1097 * ext4_grp_locked_error() to distinguish between the 1098 * ERRORS_CONT and ERRORS_RO case, and perhaps return more 1099 * aggressively from the ext4 function in question, with a 1100 * more appropriate error code. 1101 */ 1102 ext4_lock_group(sb, grp); 1103 return; 1104 } 1105 1106 void ext4_mark_group_bitmap_corrupted(struct super_block *sb, 1107 ext4_group_t group, 1108 unsigned int flags) 1109 { 1110 struct ext4_sb_info *sbi = EXT4_SB(sb); 1111 struct ext4_group_info *grp = ext4_get_group_info(sb, group); 1112 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL); 1113 int ret; 1114 1115 if (!grp || !gdp) 1116 return; 1117 if (flags & EXT4_GROUP_INFO_BBITMAP_CORRUPT) { 1118 ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT, 1119 &grp->bb_state); 1120 if (!ret) 1121 percpu_counter_sub(&sbi->s_freeclusters_counter, 1122 grp->bb_free); 1123 } 1124 1125 if (flags & EXT4_GROUP_INFO_IBITMAP_CORRUPT) { 1126 ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, 1127 &grp->bb_state); 1128 if (!ret && gdp) { 1129 int count; 1130 1131 count = ext4_free_inodes_count(sb, gdp); 1132 percpu_counter_sub(&sbi->s_freeinodes_counter, 1133 count); 1134 } 1135 } 1136 } 1137 1138 void ext4_update_dynamic_rev(struct super_block *sb) 1139 { 1140 struct ext4_super_block *es = EXT4_SB(sb)->s_es; 1141 1142 if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV) 1143 return; 1144 1145 ext4_warning(sb, 1146 "updating to rev %d because of new feature flag, " 1147 "running e2fsck is recommended", 1148 EXT4_DYNAMIC_REV); 1149 1150 es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO); 1151 es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE); 1152 es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV); 1153 /* leave es->s_feature_*compat flags alone */ 1154 /* es->s_uuid will be set by e2fsck if empty */ 1155 1156 /* 1157 * The rest of the superblock fields should be zero, and if not it 1158 * means they are likely already in use, so leave them alone. We 1159 * can leave it up to e2fsck to clean up any inconsistencies there. 1160 */ 1161 } 1162 1163 static inline struct inode *orphan_list_entry(struct list_head *l) 1164 { 1165 return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode; 1166 } 1167 1168 static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi) 1169 { 1170 struct list_head *l; 1171 1172 ext4_msg(sb, KERN_ERR, "sb orphan head is %d", 1173 le32_to_cpu(sbi->s_es->s_last_orphan)); 1174 1175 printk(KERN_ERR "sb_info orphan list:\n"); 1176 list_for_each(l, &sbi->s_orphan) { 1177 struct inode *inode = orphan_list_entry(l); 1178 printk(KERN_ERR " " 1179 "inode %s:%llu at %p: mode %o, nlink %d, next %d\n", 1180 inode->i_sb->s_id, inode->i_ino, inode, 1181 inode->i_mode, inode->i_nlink, 1182 NEXT_ORPHAN(inode)); 1183 } 1184 } 1185 1186 #ifdef CONFIG_QUOTA 1187 static int ext4_quota_off(struct super_block *sb, int type); 1188 1189 static inline void ext4_quotas_off(struct super_block *sb, int type) 1190 { 1191 BUG_ON(type > EXT4_MAXQUOTAS); 1192 1193 /* Use our quota_off function to clear inode flags etc. */ 1194 for (type--; type >= 0; type--) 1195 ext4_quota_off(sb, type); 1196 } 1197 1198 /* 1199 * This is a helper function which is used in the mount/remount 1200 * codepaths (which holds s_umount) to fetch the quota file name. 1201 */ 1202 static inline char *get_qf_name(struct super_block *sb, 1203 struct ext4_sb_info *sbi, 1204 int type) 1205 { 1206 return rcu_dereference_protected(sbi->s_qf_names[type], 1207 lockdep_is_held(&sb->s_umount)); 1208 } 1209 #else 1210 static inline void ext4_quotas_off(struct super_block *sb, int type) 1211 { 1212 } 1213 #endif 1214 1215 static int ext4_percpu_param_init(struct ext4_sb_info *sbi) 1216 { 1217 ext4_fsblk_t block; 1218 int err; 1219 1220 block = ext4_count_free_clusters(sbi->s_sb); 1221 ext4_free_blocks_count_set(sbi->s_es, EXT4_C2B(sbi, block)); 1222 err = percpu_counter_init(&sbi->s_freeclusters_counter, block, 1223 GFP_KERNEL); 1224 if (!err) { 1225 unsigned long freei = ext4_count_free_inodes(sbi->s_sb); 1226 sbi->s_es->s_free_inodes_count = cpu_to_le32(freei); 1227 err = percpu_counter_init(&sbi->s_freeinodes_counter, freei, 1228 GFP_KERNEL); 1229 } 1230 if (!err) 1231 err = percpu_counter_init(&sbi->s_dirs_counter, 1232 ext4_count_dirs(sbi->s_sb), GFP_KERNEL); 1233 if (!err) 1234 err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0, 1235 GFP_KERNEL); 1236 if (!err) 1237 err = percpu_counter_init(&sbi->s_sra_exceeded_retry_limit, 0, 1238 GFP_KERNEL); 1239 if (!err) 1240 err = percpu_init_rwsem(&sbi->s_writepages_rwsem); 1241 1242 if (err) 1243 ext4_msg(sbi->s_sb, KERN_ERR, "insufficient memory"); 1244 1245 return err; 1246 } 1247 1248 static void ext4_percpu_param_destroy(struct ext4_sb_info *sbi) 1249 { 1250 percpu_counter_destroy(&sbi->s_freeclusters_counter); 1251 percpu_counter_destroy(&sbi->s_freeinodes_counter); 1252 percpu_counter_destroy(&sbi->s_dirs_counter); 1253 percpu_counter_destroy(&sbi->s_dirtyclusters_counter); 1254 percpu_counter_destroy(&sbi->s_sra_exceeded_retry_limit); 1255 percpu_free_rwsem(&sbi->s_writepages_rwsem); 1256 } 1257 1258 static void ext4_group_desc_free(struct ext4_sb_info *sbi) 1259 { 1260 struct buffer_head **group_desc; 1261 int i; 1262 1263 group_desc = rcu_access_pointer(sbi->s_group_desc); 1264 for (i = 0; i < sbi->s_gdb_count; i++) 1265 brelse(group_desc[i]); 1266 kvfree(group_desc); 1267 } 1268 1269 static void ext4_flex_groups_free(struct ext4_sb_info *sbi) 1270 { 1271 struct flex_groups **flex_groups; 1272 int i; 1273 1274 flex_groups = rcu_access_pointer(sbi->s_flex_groups); 1275 if (flex_groups) { 1276 for (i = 0; i < sbi->s_flex_groups_allocated; i++) 1277 kvfree(flex_groups[i]); 1278 kvfree(flex_groups); 1279 } 1280 } 1281 1282 static void ext4_put_super(struct super_block *sb) 1283 { 1284 struct ext4_sb_info *sbi = EXT4_SB(sb); 1285 struct ext4_super_block *es = sbi->s_es; 1286 int aborted = 0; 1287 int err; 1288 1289 /* 1290 * Unregister sysfs before destroying jbd2 journal. 1291 * Since we could still access attr_journal_task attribute via sysfs 1292 * path which could have sbi->s_journal->j_task as NULL 1293 * Unregister sysfs before flush sbi->s_sb_upd_work. 1294 * Since user may read /proc/fs/ext4/xx/mb_groups during umount, If 1295 * read metadata verify failed then will queue error work. 1296 * update_super_work will call start_this_handle may trigger 1297 * BUG_ON. 1298 */ 1299 ext4_unregister_sysfs(sb); 1300 1301 if (___ratelimit(&ext4_mount_msg_ratelimit, "EXT4-fs unmount")) 1302 ext4_msg(sb, KERN_INFO, "unmounting filesystem %pU.", 1303 &sb->s_uuid); 1304 1305 ext4_unregister_li_request(sb); 1306 ext4_quotas_off(sb, EXT4_MAXQUOTAS); 1307 1308 destroy_workqueue(sbi->rsv_conversion_wq); 1309 ext4_release_orphan_info(sb); 1310 1311 if (sbi->s_journal) { 1312 aborted = is_journal_aborted(sbi->s_journal); 1313 err = ext4_journal_destroy(sbi, sbi->s_journal); 1314 if ((err < 0) && !aborted) { 1315 ext4_abort(sb, -err, "Couldn't clean up the journal"); 1316 } 1317 } else 1318 flush_work(&sbi->s_sb_upd_work); 1319 1320 ext4_es_unregister_shrinker(sbi); 1321 timer_shutdown_sync(&sbi->s_err_report); 1322 ext4_release_system_zone(sb); 1323 ext4_mb_release(sb); 1324 ext4_ext_release(sb); 1325 1326 if (!ext4_emergency_state(sb) && !sb_rdonly(sb)) { 1327 if (!aborted) { 1328 ext4_clear_feature_journal_needs_recovery(sb); 1329 ext4_clear_feature_orphan_present(sb); 1330 es->s_state = cpu_to_le16(sbi->s_mount_state); 1331 } 1332 ext4_commit_super(sb); 1333 } 1334 1335 ext4_group_desc_free(sbi); 1336 ext4_flex_groups_free(sbi); 1337 1338 WARN_ON_ONCE(!(sbi->s_mount_state & EXT4_ERROR_FS) && 1339 percpu_counter_sum(&sbi->s_dirtyclusters_counter)); 1340 ext4_percpu_param_destroy(sbi); 1341 #ifdef CONFIG_QUOTA 1342 for (int i = 0; i < EXT4_MAXQUOTAS; i++) 1343 kfree(get_qf_name(sb, sbi, i)); 1344 #endif 1345 1346 /* Debugging code just in case the in-memory inode orphan list 1347 * isn't empty. The on-disk one can be non-empty if we've 1348 * detected an error and taken the fs readonly, but the 1349 * in-memory list had better be clean by this point. */ 1350 if (!list_empty(&sbi->s_orphan)) 1351 dump_orphan_list(sb, sbi); 1352 ASSERT(list_empty(&sbi->s_orphan)); 1353 1354 sync_blockdev(sb->s_bdev); 1355 invalidate_bdev(sb->s_bdev); 1356 if (sbi->s_journal_bdev_file) { 1357 /* 1358 * Invalidate the journal device's buffers. We don't want them 1359 * floating about in memory - the physical journal device may 1360 * hotswapped, and it breaks the `ro-after' testing code. 1361 */ 1362 sync_blockdev(file_bdev(sbi->s_journal_bdev_file)); 1363 invalidate_bdev(file_bdev(sbi->s_journal_bdev_file)); 1364 } 1365 1366 ext4_xattr_destroy_cache(sbi->s_ea_inode_cache); 1367 sbi->s_ea_inode_cache = NULL; 1368 1369 ext4_xattr_destroy_cache(sbi->s_ea_block_cache); 1370 sbi->s_ea_block_cache = NULL; 1371 1372 ext4_stop_mmpd(sbi); 1373 1374 brelse(sbi->s_sbh); 1375 sb->s_fs_info = NULL; 1376 /* 1377 * Now that we are completely done shutting down the 1378 * superblock, we need to actually destroy the kobject. 1379 */ 1380 kobject_put(&sbi->s_kobj); 1381 wait_for_completion(&sbi->s_kobj_unregister); 1382 kfree(sbi->s_blockgroup_lock); 1383 fs_put_dax(sbi->s_daxdev, NULL); 1384 fscrypt_free_dummy_policy(&sbi->s_dummy_enc_policy); 1385 #if IS_ENABLED(CONFIG_UNICODE) 1386 utf8_unload(sb->s_encoding); 1387 #endif 1388 kfree(sbi); 1389 } 1390 1391 static struct kmem_cache *ext4_inode_cachep; 1392 1393 /* 1394 * Called inside transaction, so use GFP_NOFS 1395 */ 1396 static struct inode *ext4_alloc_inode(struct super_block *sb) 1397 { 1398 struct ext4_inode_info *ei; 1399 1400 ei = alloc_inode_sb(sb, ext4_inode_cachep, GFP_NOFS); 1401 if (!ei) 1402 return NULL; 1403 1404 inode_set_iversion(&ei->vfs_inode, 1); 1405 ei->i_flags = 0; 1406 ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ 1407 spin_lock_init(&ei->i_raw_lock); 1408 ei->i_prealloc_node = RB_ROOT; 1409 atomic_set(&ei->i_prealloc_active, 0); 1410 rwlock_init(&ei->i_prealloc_lock); 1411 ext4_es_init_tree(&ei->i_es_tree); 1412 rwlock_init(&ei->i_es_lock); 1413 INIT_LIST_HEAD(&ei->i_es_list); 1414 ei->i_es_all_nr = 0; 1415 ei->i_es_shk_nr = 0; 1416 ei->i_es_shrink_lblk = 0; 1417 ei->i_es_seq = 0; 1418 ei->i_reserved_data_blocks = 0; 1419 spin_lock_init(&(ei->i_block_reservation_lock)); 1420 ext4_init_pending_tree(&ei->i_pending_tree); 1421 #ifdef CONFIG_QUOTA 1422 ei->i_reserved_quota = 0; 1423 memset(&ei->i_dquot, 0, sizeof(ei->i_dquot)); 1424 #endif 1425 ei->jinode = NULL; 1426 INIT_LIST_HEAD(&ei->i_rsv_conversion_list); 1427 spin_lock_init(&ei->i_completed_io_lock); 1428 ei->i_sync_tid = 0; 1429 ei->i_datasync_tid = 0; 1430 INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work); 1431 ext4_fc_init_inode(&ei->vfs_inode); 1432 spin_lock_init(&ei->i_fc_lock); 1433 mmb_init(&ei->i_metadata_bhs, &ei->vfs_inode.i_data); 1434 #ifdef CONFIG_LOCKDEP 1435 lockdep_set_subclass(&ei->i_data_sem, I_DATA_SEM_NORMAL); 1436 #endif 1437 return &ei->vfs_inode; 1438 } 1439 1440 static int ext4_drop_inode(struct inode *inode) 1441 { 1442 int drop = inode_generic_drop(inode); 1443 1444 if (!drop) 1445 drop = fscrypt_drop_inode(inode); 1446 1447 trace_ext4_drop_inode(inode, drop); 1448 return drop; 1449 } 1450 1451 static void ext4_free_in_core_inode(struct inode *inode) 1452 { 1453 fscrypt_free_inode(inode); 1454 if (!list_empty(&(EXT4_I(inode)->i_fc_list))) { 1455 pr_warn("%s: inode %llu still in fc list", 1456 __func__, inode->i_ino); 1457 } 1458 kmem_cache_free(ext4_inode_cachep, EXT4_I(inode)); 1459 } 1460 1461 static void ext4_destroy_inode(struct inode *inode) 1462 { 1463 if (ext4_inode_orphan_tracked(inode)) { 1464 ext4_msg(inode->i_sb, KERN_ERR, 1465 "Inode %llu (%p): inode tracked as orphan!", 1466 inode->i_ino, EXT4_I(inode)); 1467 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4, 1468 EXT4_I(inode), sizeof(struct ext4_inode_info), 1469 true); 1470 dump_stack(); 1471 } 1472 1473 if (!(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ERROR_FS) && 1474 WARN_ON_ONCE(EXT4_I(inode)->i_reserved_data_blocks)) 1475 ext4_msg(inode->i_sb, KERN_ERR, 1476 "Inode %llu (%p): i_reserved_data_blocks (%u) not cleared!", 1477 inode->i_ino, EXT4_I(inode), 1478 EXT4_I(inode)->i_reserved_data_blocks); 1479 } 1480 1481 static void ext4_shutdown(struct super_block *sb) 1482 { 1483 ext4_force_shutdown(sb, EXT4_GOING_FLAGS_NOLOGFLUSH); 1484 } 1485 1486 static void init_once(void *foo) 1487 { 1488 struct ext4_inode_info *ei = foo; 1489 1490 INIT_LIST_HEAD(&ei->i_orphan); 1491 init_rwsem(&ei->xattr_sem); 1492 init_rwsem(&ei->i_data_sem); 1493 inode_init_once(&ei->vfs_inode); 1494 ext4_fc_init_inode(&ei->vfs_inode); 1495 #ifdef CONFIG_FS_ENCRYPTION 1496 ei->i_crypt_info = NULL; 1497 #endif 1498 } 1499 1500 static int __init init_inodecache(void) 1501 { 1502 struct kmem_cache_args args = { 1503 .useroffset = offsetof(struct ext4_inode_info, i_data), 1504 .usersize = sizeof_field(struct ext4_inode_info, i_data), 1505 .use_freeptr_offset = true, 1506 .freeptr_offset = offsetof(struct ext4_inode_info, i_flags), 1507 .ctor = init_once, 1508 }; 1509 1510 ext4_inode_cachep = kmem_cache_create("ext4_inode_cache", 1511 sizeof(struct ext4_inode_info), 1512 &args, 1513 SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT); 1514 1515 if (ext4_inode_cachep == NULL) 1516 return -ENOMEM; 1517 return 0; 1518 } 1519 1520 static void destroy_inodecache(void) 1521 { 1522 /* 1523 * Make sure all delayed rcu free inodes are flushed before we 1524 * destroy cache. 1525 */ 1526 rcu_barrier(); 1527 kmem_cache_destroy(ext4_inode_cachep); 1528 } 1529 1530 void ext4_clear_inode(struct inode *inode) 1531 { 1532 ext4_fc_del(inode); 1533 if (!EXT4_SB(inode->i_sb)->s_journal) 1534 mmb_invalidate(&EXT4_I(inode)->i_metadata_bhs); 1535 clear_inode(inode); 1536 ext4_discard_preallocations(inode); 1537 /* 1538 * We must remove the inode from the hash before ext4_free_inode() 1539 * clears the bit in inode bitmap as otherwise another process reusing 1540 * the inode will block in insert_inode_hash() waiting for inode 1541 * eviction to complete while holding transaction handle open, but 1542 * ext4_evict_inode() still running for that inode could block waiting 1543 * for transaction commit if the inode is marked as IS_SYNC => deadlock. 1544 * 1545 * Removing the inode from the hash here is safe. There are two cases 1546 * to consider: 1547 * 1) The inode still has references to it (i_nlink > 0). In that case 1548 * we are keeping the inode and once we remove the inode from the hash, 1549 * iget() can create the new inode structure for the same inode number 1550 * and we are fine with that as all IO on behalf of the inode is 1551 * finished. 1552 * 2) We are deleting the inode (i_nlink == 0). In that case inode 1553 * number cannot be reused until ext4_free_inode() clears the bit in 1554 * the inode bitmap, at which point all IO is done and reuse is fine 1555 * again. 1556 */ 1557 remove_inode_hash(inode); 1558 ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS); 1559 dquot_drop(inode); 1560 if (EXT4_I(inode)->jinode) { 1561 jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode), 1562 EXT4_I(inode)->jinode); 1563 jbd2_free_inode(EXT4_I(inode)->jinode); 1564 EXT4_I(inode)->jinode = NULL; 1565 } 1566 fscrypt_put_encryption_info(inode); 1567 } 1568 1569 static struct inode *ext4_nfs_get_inode(struct super_block *sb, 1570 u64 ino, u32 generation) 1571 { 1572 struct inode *inode; 1573 1574 /* 1575 * Currently we don't know the generation for parent directory, so 1576 * a generation of 0 means "accept any" 1577 */ 1578 inode = ext4_iget(sb, ino, EXT4_IGET_HANDLE); 1579 if (IS_ERR(inode)) 1580 return ERR_CAST(inode); 1581 if (generation && inode->i_generation != generation) { 1582 iput(inode); 1583 return ERR_PTR(-ESTALE); 1584 } 1585 1586 return inode; 1587 } 1588 1589 static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid, 1590 int fh_len, int fh_type) 1591 { 1592 return generic_fh_to_dentry(sb, fid, fh_len, fh_type, 1593 ext4_nfs_get_inode); 1594 } 1595 1596 static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid, 1597 int fh_len, int fh_type) 1598 { 1599 return generic_fh_to_parent(sb, fid, fh_len, fh_type, 1600 ext4_nfs_get_inode); 1601 } 1602 1603 static int ext4_nfs_commit_metadata(struct inode *inode) 1604 { 1605 struct writeback_control wbc = { 1606 .sync_mode = WB_SYNC_ALL 1607 }; 1608 1609 trace_ext4_nfs_commit_metadata(inode); 1610 return ext4_write_inode(inode, &wbc); 1611 } 1612 1613 #ifdef CONFIG_QUOTA 1614 static const char * const quotatypes[] = INITQFNAMES; 1615 #define QTYPE2NAME(t) (quotatypes[t]) 1616 1617 static int ext4_write_dquot(struct dquot *dquot); 1618 static int ext4_acquire_dquot(struct dquot *dquot); 1619 static int ext4_release_dquot(struct dquot *dquot); 1620 static int ext4_mark_dquot_dirty(struct dquot *dquot); 1621 static int ext4_write_info(struct super_block *sb, int type); 1622 static int ext4_quota_on(struct super_block *sb, int type, int format_id, 1623 const struct path *path); 1624 static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data, 1625 size_t len, loff_t off); 1626 static ssize_t ext4_quota_write(struct super_block *sb, int type, 1627 const char *data, size_t len, loff_t off); 1628 static int ext4_quota_enable(struct super_block *sb, int type, int format_id, 1629 unsigned int flags); 1630 1631 static struct dquot __rcu **ext4_get_dquots(struct inode *inode) 1632 { 1633 return EXT4_I(inode)->i_dquot; 1634 } 1635 1636 static const struct dquot_operations ext4_quota_operations = { 1637 .get_reserved_space = ext4_get_reserved_space, 1638 .write_dquot = ext4_write_dquot, 1639 .acquire_dquot = ext4_acquire_dquot, 1640 .release_dquot = ext4_release_dquot, 1641 .mark_dirty = ext4_mark_dquot_dirty, 1642 .write_info = ext4_write_info, 1643 .alloc_dquot = dquot_alloc, 1644 .destroy_dquot = dquot_destroy, 1645 .get_projid = ext4_get_projid, 1646 .get_inode_usage = ext4_get_inode_usage, 1647 .get_next_id = dquot_get_next_id, 1648 }; 1649 1650 static const struct quotactl_ops ext4_qctl_operations = { 1651 .quota_on = ext4_quota_on, 1652 .quota_off = ext4_quota_off, 1653 .quota_sync = dquot_quota_sync, 1654 .get_state = dquot_get_state, 1655 .set_info = dquot_set_dqinfo, 1656 .get_dqblk = dquot_get_dqblk, 1657 .set_dqblk = dquot_set_dqblk, 1658 .get_nextdqblk = dquot_get_next_dqblk, 1659 }; 1660 #endif 1661 1662 static const struct super_operations ext4_sops = { 1663 .alloc_inode = ext4_alloc_inode, 1664 .free_inode = ext4_free_in_core_inode, 1665 .destroy_inode = ext4_destroy_inode, 1666 .write_inode = ext4_write_inode, 1667 .dirty_inode = ext4_dirty_inode, 1668 .drop_inode = ext4_drop_inode, 1669 .evict_inode = ext4_evict_inode, 1670 .put_super = ext4_put_super, 1671 .sync_fs = ext4_sync_fs, 1672 .freeze_fs = ext4_freeze, 1673 .unfreeze_fs = ext4_unfreeze, 1674 .statfs = ext4_statfs, 1675 .show_options = ext4_show_options, 1676 .shutdown = ext4_shutdown, 1677 #ifdef CONFIG_QUOTA 1678 .quota_read = ext4_quota_read, 1679 .quota_write = ext4_quota_write, 1680 .get_dquots = ext4_get_dquots, 1681 #endif 1682 }; 1683 1684 static const struct export_operations ext4_export_ops = { 1685 .encode_fh = generic_encode_ino32_fh, 1686 .fh_to_dentry = ext4_fh_to_dentry, 1687 .fh_to_parent = ext4_fh_to_parent, 1688 .get_parent = ext4_get_parent, 1689 .commit_metadata = ext4_nfs_commit_metadata, 1690 }; 1691 1692 enum { 1693 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid, 1694 Opt_resgid, Opt_resuid, Opt_sb, 1695 Opt_nouid32, Opt_debug, Opt_removed, 1696 Opt_user_xattr, Opt_acl, 1697 Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload, 1698 Opt_commit, Opt_min_batch_time, Opt_max_batch_time, Opt_journal_dev, 1699 Opt_journal_path, Opt_journal_checksum, Opt_journal_async_commit, 1700 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback, 1701 Opt_data_err_abort, Opt_data_err_ignore, Opt_test_dummy_encryption, 1702 Opt_inlinecrypt, 1703 Opt_usrjquota, Opt_grpjquota, Opt_quota, 1704 Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err, 1705 Opt_usrquota, Opt_grpquota, Opt_prjquota, 1706 Opt_dax, Opt_dax_always, Opt_dax_inode, Opt_dax_never, 1707 Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_warn_on_error, 1708 Opt_nowarn_on_error, Opt_mblk_io_submit, Opt_debug_want_extra_isize, 1709 Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity, 1710 Opt_inode_readahead_blks, Opt_journal_ioprio, 1711 Opt_dioread_nolock, Opt_dioread_lock, 1712 Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable, 1713 Opt_max_dir_size_kb, Opt_nojournal_checksum, Opt_nombcache, 1714 Opt_no_prefetch_block_bitmaps, Opt_mb_optimize_scan, 1715 Opt_errors, Opt_data, Opt_data_err, Opt_jqfmt, Opt_dax_type, 1716 #ifdef CONFIG_EXT4_DEBUG 1717 Opt_fc_debug_max_replay, Opt_fc_debug_force 1718 #endif 1719 }; 1720 1721 static const struct constant_table ext4_param_errors[] = { 1722 {"continue", EXT4_MOUNT_ERRORS_CONT}, 1723 {"panic", EXT4_MOUNT_ERRORS_PANIC}, 1724 {"remount-ro", EXT4_MOUNT_ERRORS_RO}, 1725 {} 1726 }; 1727 1728 static const struct constant_table ext4_param_data[] = { 1729 {"journal", EXT4_MOUNT_JOURNAL_DATA}, 1730 {"ordered", EXT4_MOUNT_ORDERED_DATA}, 1731 {"writeback", EXT4_MOUNT_WRITEBACK_DATA}, 1732 {} 1733 }; 1734 1735 static const struct constant_table ext4_param_data_err[] = { 1736 {"abort", Opt_data_err_abort}, 1737 {"ignore", Opt_data_err_ignore}, 1738 {} 1739 }; 1740 1741 static const struct constant_table ext4_param_jqfmt[] = { 1742 {"vfsold", QFMT_VFS_OLD}, 1743 {"vfsv0", QFMT_VFS_V0}, 1744 {"vfsv1", QFMT_VFS_V1}, 1745 {} 1746 }; 1747 1748 static const struct constant_table ext4_param_dax[] = { 1749 {"always", Opt_dax_always}, 1750 {"inode", Opt_dax_inode}, 1751 {"never", Opt_dax_never}, 1752 {} 1753 }; 1754 1755 /* 1756 * Mount option specification 1757 * We don't use fsparam_flag_no because of the way we set the 1758 * options and the way we show them in _ext4_show_options(). To 1759 * keep the changes to a minimum, let's keep the negative options 1760 * separate for now. 1761 */ 1762 static const struct fs_parameter_spec ext4_param_specs[] = { 1763 fsparam_flag ("bsddf", Opt_bsd_df), 1764 fsparam_flag ("minixdf", Opt_minix_df), 1765 fsparam_flag ("grpid", Opt_grpid), 1766 fsparam_flag ("bsdgroups", Opt_grpid), 1767 fsparam_flag ("nogrpid", Opt_nogrpid), 1768 fsparam_flag ("sysvgroups", Opt_nogrpid), 1769 fsparam_gid ("resgid", Opt_resgid), 1770 fsparam_uid ("resuid", Opt_resuid), 1771 fsparam_u32 ("sb", Opt_sb), 1772 fsparam_enum ("errors", Opt_errors, ext4_param_errors), 1773 fsparam_flag ("nouid32", Opt_nouid32), 1774 fsparam_flag ("debug", Opt_debug), 1775 fsparam_flag ("oldalloc", Opt_removed), 1776 fsparam_flag ("orlov", Opt_removed), 1777 fsparam_flag ("user_xattr", Opt_user_xattr), 1778 fsparam_flag ("acl", Opt_acl), 1779 fsparam_flag ("norecovery", Opt_noload), 1780 fsparam_flag ("noload", Opt_noload), 1781 fsparam_flag ("bh", Opt_removed), 1782 fsparam_flag ("nobh", Opt_removed), 1783 fsparam_u32 ("commit", Opt_commit), 1784 fsparam_u32 ("min_batch_time", Opt_min_batch_time), 1785 fsparam_u32 ("max_batch_time", Opt_max_batch_time), 1786 fsparam_u32 ("journal_dev", Opt_journal_dev), 1787 fsparam_bdev ("journal_path", Opt_journal_path), 1788 fsparam_flag ("journal_checksum", Opt_journal_checksum), 1789 fsparam_flag ("nojournal_checksum", Opt_nojournal_checksum), 1790 fsparam_flag ("journal_async_commit",Opt_journal_async_commit), 1791 fsparam_flag ("abort", Opt_abort), 1792 fsparam_enum ("data", Opt_data, ext4_param_data), 1793 fsparam_enum ("data_err", Opt_data_err, 1794 ext4_param_data_err), 1795 fsparam_string_empty 1796 ("usrjquota", Opt_usrjquota), 1797 fsparam_string_empty 1798 ("grpjquota", Opt_grpjquota), 1799 fsparam_enum ("jqfmt", Opt_jqfmt, ext4_param_jqfmt), 1800 fsparam_flag ("grpquota", Opt_grpquota), 1801 fsparam_flag ("quota", Opt_quota), 1802 fsparam_flag ("noquota", Opt_noquota), 1803 fsparam_flag ("usrquota", Opt_usrquota), 1804 fsparam_flag ("prjquota", Opt_prjquota), 1805 fsparam_flag ("barrier", Opt_barrier), 1806 fsparam_u32 ("barrier", Opt_barrier), 1807 fsparam_flag ("nobarrier", Opt_nobarrier), 1808 fsparam_flag ("i_version", Opt_removed), 1809 fsparam_flag ("dax", Opt_dax), 1810 fsparam_enum ("dax", Opt_dax_type, ext4_param_dax), 1811 fsparam_u32 ("stripe", Opt_stripe), 1812 fsparam_flag ("delalloc", Opt_delalloc), 1813 fsparam_flag ("nodelalloc", Opt_nodelalloc), 1814 fsparam_flag ("warn_on_error", Opt_warn_on_error), 1815 fsparam_flag ("nowarn_on_error", Opt_nowarn_on_error), 1816 fsparam_u32 ("debug_want_extra_isize", 1817 Opt_debug_want_extra_isize), 1818 fsparam_flag ("mblk_io_submit", Opt_removed), 1819 fsparam_flag ("nomblk_io_submit", Opt_removed), 1820 fsparam_flag ("block_validity", Opt_block_validity), 1821 fsparam_flag ("noblock_validity", Opt_noblock_validity), 1822 fsparam_u32 ("inode_readahead_blks", 1823 Opt_inode_readahead_blks), 1824 fsparam_u32 ("journal_ioprio", Opt_journal_ioprio), 1825 fsparam_u32 ("auto_da_alloc", Opt_auto_da_alloc), 1826 fsparam_flag ("auto_da_alloc", Opt_auto_da_alloc), 1827 fsparam_flag ("noauto_da_alloc", Opt_noauto_da_alloc), 1828 fsparam_flag ("dioread_nolock", Opt_dioread_nolock), 1829 fsparam_flag ("nodioread_nolock", Opt_dioread_lock), 1830 fsparam_flag ("dioread_lock", Opt_dioread_lock), 1831 fsparam_flag ("discard", Opt_discard), 1832 fsparam_flag ("nodiscard", Opt_nodiscard), 1833 fsparam_u32 ("init_itable", Opt_init_itable), 1834 fsparam_flag ("init_itable", Opt_init_itable), 1835 fsparam_flag ("noinit_itable", Opt_noinit_itable), 1836 #ifdef CONFIG_EXT4_DEBUG 1837 fsparam_flag ("fc_debug_force", Opt_fc_debug_force), 1838 fsparam_u32 ("fc_debug_max_replay", Opt_fc_debug_max_replay), 1839 #endif 1840 fsparam_u32 ("max_dir_size_kb", Opt_max_dir_size_kb), 1841 fsparam_flag ("test_dummy_encryption", 1842 Opt_test_dummy_encryption), 1843 fsparam_string ("test_dummy_encryption", 1844 Opt_test_dummy_encryption), 1845 fsparam_flag ("inlinecrypt", Opt_inlinecrypt), 1846 fsparam_flag ("nombcache", Opt_nombcache), 1847 fsparam_flag ("no_mbcache", Opt_nombcache), /* for backward compatibility */ 1848 fsparam_flag ("prefetch_block_bitmaps", 1849 Opt_removed), 1850 fsparam_flag ("no_prefetch_block_bitmaps", 1851 Opt_no_prefetch_block_bitmaps), 1852 fsparam_s32 ("mb_optimize_scan", Opt_mb_optimize_scan), 1853 fsparam_string ("check", Opt_removed), /* mount option from ext2/3 */ 1854 fsparam_flag ("nocheck", Opt_removed), /* mount option from ext2/3 */ 1855 fsparam_flag ("reservation", Opt_removed), /* mount option from ext2/3 */ 1856 fsparam_flag ("noreservation", Opt_removed), /* mount option from ext2/3 */ 1857 fsparam_u32 ("journal", Opt_removed), /* mount option from ext2/3 */ 1858 {} 1859 }; 1860 1861 1862 #define MOPT_SET 0x0001 1863 #define MOPT_CLEAR 0x0002 1864 #define MOPT_NOSUPPORT 0x0004 1865 #define MOPT_EXPLICIT 0x0008 1866 #ifdef CONFIG_QUOTA 1867 #define MOPT_Q 0 1868 #define MOPT_QFMT 0x0010 1869 #else 1870 #define MOPT_Q MOPT_NOSUPPORT 1871 #define MOPT_QFMT MOPT_NOSUPPORT 1872 #endif 1873 #define MOPT_NO_EXT2 0x0020 1874 #define MOPT_NO_EXT3 0x0040 1875 #define MOPT_EXT4_ONLY (MOPT_NO_EXT2 | MOPT_NO_EXT3) 1876 #define MOPT_SKIP 0x0080 1877 #define MOPT_2 0x0100 1878 1879 static const struct mount_opts { 1880 int token; 1881 int mount_opt; 1882 int flags; 1883 } ext4_mount_opts[] = { 1884 {Opt_minix_df, EXT4_MOUNT_MINIX_DF, MOPT_SET}, 1885 {Opt_bsd_df, EXT4_MOUNT_MINIX_DF, MOPT_CLEAR}, 1886 {Opt_grpid, EXT4_MOUNT_GRPID, MOPT_SET}, 1887 {Opt_nogrpid, EXT4_MOUNT_GRPID, MOPT_CLEAR}, 1888 {Opt_block_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_SET}, 1889 {Opt_noblock_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_CLEAR}, 1890 {Opt_dioread_nolock, EXT4_MOUNT_DIOREAD_NOLOCK, 1891 MOPT_EXT4_ONLY | MOPT_SET}, 1892 {Opt_dioread_lock, EXT4_MOUNT_DIOREAD_NOLOCK, 1893 MOPT_EXT4_ONLY | MOPT_CLEAR}, 1894 {Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET}, 1895 {Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR}, 1896 {Opt_delalloc, EXT4_MOUNT_DELALLOC, 1897 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT}, 1898 {Opt_nodelalloc, EXT4_MOUNT_DELALLOC, 1899 MOPT_EXT4_ONLY | MOPT_CLEAR}, 1900 {Opt_warn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_SET}, 1901 {Opt_nowarn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_CLEAR}, 1902 {Opt_commit, 0, MOPT_NO_EXT2}, 1903 {Opt_nojournal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM, 1904 MOPT_EXT4_ONLY | MOPT_CLEAR}, 1905 {Opt_journal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM, 1906 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT}, 1907 {Opt_journal_async_commit, (EXT4_MOUNT_JOURNAL_ASYNC_COMMIT | 1908 EXT4_MOUNT_JOURNAL_CHECKSUM), 1909 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT}, 1910 {Opt_noload, EXT4_MOUNT_NOLOAD, MOPT_NO_EXT2 | MOPT_SET}, 1911 {Opt_data_err, EXT4_MOUNT_DATA_ERR_ABORT, MOPT_NO_EXT2}, 1912 {Opt_barrier, EXT4_MOUNT_BARRIER, MOPT_SET}, 1913 {Opt_nobarrier, EXT4_MOUNT_BARRIER, MOPT_CLEAR}, 1914 {Opt_noauto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_SET}, 1915 {Opt_auto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_CLEAR}, 1916 {Opt_noinit_itable, EXT4_MOUNT_INIT_INODE_TABLE, MOPT_CLEAR}, 1917 {Opt_dax_type, 0, MOPT_EXT4_ONLY}, 1918 {Opt_journal_dev, 0, MOPT_NO_EXT2}, 1919 {Opt_journal_path, 0, MOPT_NO_EXT2}, 1920 {Opt_journal_ioprio, 0, MOPT_NO_EXT2}, 1921 {Opt_data, 0, MOPT_NO_EXT2}, 1922 {Opt_user_xattr, EXT4_MOUNT_XATTR_USER, MOPT_SET}, 1923 #ifdef CONFIG_EXT4_FS_POSIX_ACL 1924 {Opt_acl, EXT4_MOUNT_POSIX_ACL, MOPT_SET}, 1925 #else 1926 {Opt_acl, 0, MOPT_NOSUPPORT}, 1927 #endif 1928 {Opt_nouid32, EXT4_MOUNT_NO_UID32, MOPT_SET}, 1929 {Opt_debug, EXT4_MOUNT_DEBUG, MOPT_SET}, 1930 {Opt_quota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, MOPT_SET | MOPT_Q}, 1931 {Opt_usrquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, 1932 MOPT_SET | MOPT_Q}, 1933 {Opt_grpquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_GRPQUOTA, 1934 MOPT_SET | MOPT_Q}, 1935 {Opt_prjquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_PRJQUOTA, 1936 MOPT_SET | MOPT_Q}, 1937 {Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA | 1938 EXT4_MOUNT_GRPQUOTA | EXT4_MOUNT_PRJQUOTA), 1939 MOPT_CLEAR | MOPT_Q}, 1940 {Opt_usrjquota, 0, MOPT_Q}, 1941 {Opt_grpjquota, 0, MOPT_Q}, 1942 {Opt_jqfmt, 0, MOPT_QFMT}, 1943 {Opt_nombcache, EXT4_MOUNT_NO_MBCACHE, MOPT_SET}, 1944 {Opt_no_prefetch_block_bitmaps, EXT4_MOUNT_NO_PREFETCH_BLOCK_BITMAPS, 1945 MOPT_SET}, 1946 #ifdef CONFIG_EXT4_DEBUG 1947 {Opt_fc_debug_force, EXT4_MOUNT2_JOURNAL_FAST_COMMIT, 1948 MOPT_SET | MOPT_2 | MOPT_EXT4_ONLY}, 1949 #endif 1950 {Opt_abort, EXT4_MOUNT2_ABORT, MOPT_SET | MOPT_2}, 1951 {Opt_err, 0, 0} 1952 }; 1953 1954 #if IS_ENABLED(CONFIG_UNICODE) 1955 static const struct ext4_sb_encodings { 1956 __u16 magic; 1957 char *name; 1958 unsigned int version; 1959 } ext4_sb_encoding_map[] = { 1960 {EXT4_ENC_UTF8_12_1, "utf8", UNICODE_AGE(12, 1, 0)}, 1961 }; 1962 1963 static const struct ext4_sb_encodings * 1964 ext4_sb_read_encoding(const struct ext4_super_block *es) 1965 { 1966 __u16 magic = le16_to_cpu(es->s_encoding); 1967 int i; 1968 1969 for (i = 0; i < ARRAY_SIZE(ext4_sb_encoding_map); i++) 1970 if (magic == ext4_sb_encoding_map[i].magic) 1971 return &ext4_sb_encoding_map[i]; 1972 1973 return NULL; 1974 } 1975 #endif 1976 1977 #define EXT4_SPEC_JQUOTA (1 << 0) 1978 #define EXT4_SPEC_JQFMT (1 << 1) 1979 #define EXT4_SPEC_DATAJ (1 << 2) 1980 #define EXT4_SPEC_SB_BLOCK (1 << 3) 1981 #define EXT4_SPEC_JOURNAL_DEV (1 << 4) 1982 #define EXT4_SPEC_JOURNAL_IOPRIO (1 << 5) 1983 #define EXT4_SPEC_s_want_extra_isize (1 << 7) 1984 #define EXT4_SPEC_s_max_batch_time (1 << 8) 1985 #define EXT4_SPEC_s_min_batch_time (1 << 9) 1986 #define EXT4_SPEC_s_inode_readahead_blks (1 << 10) 1987 #define EXT4_SPEC_s_li_wait_mult (1 << 11) 1988 #define EXT4_SPEC_s_max_dir_size_kb (1 << 12) 1989 #define EXT4_SPEC_s_stripe (1 << 13) 1990 #define EXT4_SPEC_s_resuid (1 << 14) 1991 #define EXT4_SPEC_s_resgid (1 << 15) 1992 #define EXT4_SPEC_s_commit_interval (1 << 16) 1993 #define EXT4_SPEC_s_fc_debug_max_replay (1 << 17) 1994 #define EXT4_SPEC_s_sb_block (1 << 18) 1995 #define EXT4_SPEC_mb_optimize_scan (1 << 19) 1996 1997 struct ext4_fs_context { 1998 char *s_qf_names[EXT4_MAXQUOTAS]; 1999 struct fscrypt_dummy_policy dummy_enc_policy; 2000 int s_jquota_fmt; /* Format of quota to use */ 2001 #ifdef CONFIG_EXT4_DEBUG 2002 int s_fc_debug_max_replay; 2003 #endif 2004 unsigned short qname_spec; 2005 unsigned long vals_s_flags; /* Bits to set in s_flags */ 2006 unsigned long mask_s_flags; /* Bits changed in s_flags */ 2007 unsigned long journal_devnum; 2008 unsigned long s_commit_interval; 2009 unsigned long s_stripe; 2010 unsigned int s_inode_readahead_blks; 2011 unsigned int s_want_extra_isize; 2012 unsigned int s_li_wait_mult; 2013 unsigned int s_max_dir_size_kb; 2014 unsigned int journal_ioprio; 2015 unsigned int vals_s_mount_opt; 2016 unsigned int mask_s_mount_opt; 2017 unsigned int vals_s_mount_opt2; 2018 unsigned int mask_s_mount_opt2; 2019 unsigned int opt_flags; /* MOPT flags */ 2020 unsigned int spec; 2021 u32 s_max_batch_time; 2022 u32 s_min_batch_time; 2023 kuid_t s_resuid; 2024 kgid_t s_resgid; 2025 ext4_fsblk_t s_sb_block; 2026 }; 2027 2028 static void ext4_fc_free(struct fs_context *fc) 2029 { 2030 struct ext4_fs_context *ctx = fc->fs_private; 2031 int i; 2032 2033 if (!ctx) 2034 return; 2035 2036 for (i = 0; i < EXT4_MAXQUOTAS; i++) 2037 kfree(ctx->s_qf_names[i]); 2038 2039 fscrypt_free_dummy_policy(&ctx->dummy_enc_policy); 2040 kfree(ctx); 2041 } 2042 2043 int ext4_init_fs_context(struct fs_context *fc) 2044 { 2045 struct ext4_fs_context *ctx; 2046 2047 ctx = kzalloc_obj(struct ext4_fs_context); 2048 if (!ctx) 2049 return -ENOMEM; 2050 2051 fc->fs_private = ctx; 2052 fc->ops = &ext4_context_ops; 2053 2054 /* i_version is always enabled now */ 2055 fc->sb_flags |= SB_I_VERSION; 2056 2057 return 0; 2058 } 2059 2060 #ifdef CONFIG_QUOTA 2061 /* 2062 * Note the name of the specified quota file. 2063 */ 2064 static int note_qf_name(struct fs_context *fc, int qtype, 2065 struct fs_parameter *param) 2066 { 2067 struct ext4_fs_context *ctx = fc->fs_private; 2068 char *qname; 2069 2070 if (param->size < 1) { 2071 ext4_msg(NULL, KERN_ERR, "Missing quota name"); 2072 return -EINVAL; 2073 } 2074 if (strchr(param->string, '/')) { 2075 ext4_msg(NULL, KERN_ERR, 2076 "quotafile must be on filesystem root"); 2077 return -EINVAL; 2078 } 2079 if (ctx->s_qf_names[qtype]) { 2080 if (strcmp(ctx->s_qf_names[qtype], param->string) != 0) { 2081 ext4_msg(NULL, KERN_ERR, 2082 "%s quota file already specified", 2083 QTYPE2NAME(qtype)); 2084 return -EINVAL; 2085 } 2086 return 0; 2087 } 2088 2089 qname = kmemdup_nul(param->string, param->size, GFP_KERNEL); 2090 if (!qname) { 2091 ext4_msg(NULL, KERN_ERR, 2092 "Not enough memory for storing quotafile name"); 2093 return -ENOMEM; 2094 } 2095 ctx->s_qf_names[qtype] = qname; 2096 ctx->qname_spec |= 1 << qtype; 2097 ctx->spec |= EXT4_SPEC_JQUOTA; 2098 return 0; 2099 } 2100 2101 /* 2102 * Clear the name of the specified quota file. 2103 */ 2104 static int unnote_qf_name(struct fs_context *fc, int qtype) 2105 { 2106 struct ext4_fs_context *ctx = fc->fs_private; 2107 2108 kfree(ctx->s_qf_names[qtype]); 2109 2110 ctx->s_qf_names[qtype] = NULL; 2111 ctx->qname_spec |= 1 << qtype; 2112 ctx->spec |= EXT4_SPEC_JQUOTA; 2113 return 0; 2114 } 2115 #endif 2116 2117 static int ext4_parse_test_dummy_encryption(const struct fs_parameter *param, 2118 struct ext4_fs_context *ctx) 2119 { 2120 int err; 2121 2122 if (!IS_ENABLED(CONFIG_FS_ENCRYPTION)) { 2123 ext4_msg(NULL, KERN_WARNING, 2124 "test_dummy_encryption option not supported"); 2125 return -EINVAL; 2126 } 2127 err = fscrypt_parse_test_dummy_encryption(param, 2128 &ctx->dummy_enc_policy); 2129 if (err == -EINVAL) { 2130 ext4_msg(NULL, KERN_WARNING, 2131 "Value of option \"%s\" is unrecognized", param->key); 2132 } else if (err == -EEXIST) { 2133 ext4_msg(NULL, KERN_WARNING, 2134 "Conflicting test_dummy_encryption options"); 2135 return -EINVAL; 2136 } 2137 return err; 2138 } 2139 2140 #define EXT4_SET_CTX(name) \ 2141 static inline __maybe_unused \ 2142 void ctx_set_##name(struct ext4_fs_context *ctx, unsigned long flag) \ 2143 { \ 2144 ctx->mask_s_##name |= flag; \ 2145 ctx->vals_s_##name |= flag; \ 2146 } 2147 2148 #define EXT4_CLEAR_CTX(name) \ 2149 static inline __maybe_unused \ 2150 void ctx_clear_##name(struct ext4_fs_context *ctx, unsigned long flag) \ 2151 { \ 2152 ctx->mask_s_##name |= flag; \ 2153 ctx->vals_s_##name &= ~flag; \ 2154 } 2155 2156 #define EXT4_TEST_CTX(name) \ 2157 static inline unsigned long \ 2158 ctx_test_##name(struct ext4_fs_context *ctx, unsigned long flag) \ 2159 { \ 2160 return (ctx->vals_s_##name & flag); \ 2161 } 2162 2163 EXT4_SET_CTX(flags); /* set only */ 2164 EXT4_SET_CTX(mount_opt); 2165 EXT4_CLEAR_CTX(mount_opt); 2166 EXT4_TEST_CTX(mount_opt); 2167 EXT4_SET_CTX(mount_opt2); 2168 EXT4_CLEAR_CTX(mount_opt2); 2169 EXT4_TEST_CTX(mount_opt2); 2170 2171 static int ext4_parse_param(struct fs_context *fc, struct fs_parameter *param) 2172 { 2173 struct ext4_fs_context *ctx = fc->fs_private; 2174 struct fs_parse_result result; 2175 const struct mount_opts *m; 2176 int is_remount; 2177 int token; 2178 2179 token = fs_parse(fc, ext4_param_specs, param, &result); 2180 if (token < 0) 2181 return token; 2182 is_remount = fc->purpose == FS_CONTEXT_FOR_RECONFIGURE; 2183 2184 for (m = ext4_mount_opts; m->token != Opt_err; m++) 2185 if (token == m->token) 2186 break; 2187 2188 ctx->opt_flags |= m->flags; 2189 2190 if (m->flags & MOPT_EXPLICIT) { 2191 if (m->mount_opt & EXT4_MOUNT_DELALLOC) { 2192 ctx_set_mount_opt2(ctx, EXT4_MOUNT2_EXPLICIT_DELALLOC); 2193 } else if (m->mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) { 2194 ctx_set_mount_opt2(ctx, 2195 EXT4_MOUNT2_EXPLICIT_JOURNAL_CHECKSUM); 2196 } else 2197 return -EINVAL; 2198 } 2199 2200 if (m->flags & MOPT_NOSUPPORT) { 2201 ext4_msg(NULL, KERN_ERR, "%s option not supported", 2202 param->key); 2203 return 0; 2204 } 2205 2206 switch (token) { 2207 #ifdef CONFIG_QUOTA 2208 case Opt_usrjquota: 2209 if (!*param->string) 2210 return unnote_qf_name(fc, USRQUOTA); 2211 else 2212 return note_qf_name(fc, USRQUOTA, param); 2213 case Opt_grpjquota: 2214 if (!*param->string) 2215 return unnote_qf_name(fc, GRPQUOTA); 2216 else 2217 return note_qf_name(fc, GRPQUOTA, param); 2218 #endif 2219 case Opt_sb: 2220 if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) { 2221 ext4_msg(NULL, KERN_WARNING, 2222 "Ignoring %s option on remount", param->key); 2223 } else { 2224 ctx->s_sb_block = result.uint_32; 2225 ctx->spec |= EXT4_SPEC_s_sb_block; 2226 } 2227 return 0; 2228 case Opt_removed: 2229 ext4_msg(NULL, KERN_WARNING, "Ignoring removed %s option", 2230 param->key); 2231 return 0; 2232 case Opt_inlinecrypt: 2233 #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT 2234 ctx_set_flags(ctx, SB_INLINECRYPT); 2235 #else 2236 ext4_msg(NULL, KERN_ERR, "inline encryption not supported"); 2237 #endif 2238 return 0; 2239 case Opt_errors: 2240 ctx_clear_mount_opt(ctx, EXT4_MOUNT_ERRORS_MASK); 2241 ctx_set_mount_opt(ctx, result.uint_32); 2242 return 0; 2243 #ifdef CONFIG_QUOTA 2244 case Opt_jqfmt: 2245 ctx->s_jquota_fmt = result.uint_32; 2246 ctx->spec |= EXT4_SPEC_JQFMT; 2247 return 0; 2248 #endif 2249 case Opt_data: 2250 ctx_clear_mount_opt(ctx, EXT4_MOUNT_DATA_FLAGS); 2251 ctx_set_mount_opt(ctx, result.uint_32); 2252 ctx->spec |= EXT4_SPEC_DATAJ; 2253 return 0; 2254 case Opt_commit: 2255 if (result.uint_32 == 0) 2256 result.uint_32 = JBD2_DEFAULT_MAX_COMMIT_AGE; 2257 else if (result.uint_32 > INT_MAX / HZ) { 2258 ext4_msg(NULL, KERN_ERR, 2259 "Invalid commit interval %d, " 2260 "must be smaller than %d", 2261 result.uint_32, INT_MAX / HZ); 2262 return -EINVAL; 2263 } 2264 ctx->s_commit_interval = HZ * result.uint_32; 2265 ctx->spec |= EXT4_SPEC_s_commit_interval; 2266 return 0; 2267 case Opt_debug_want_extra_isize: 2268 if ((result.uint_32 & 1) || (result.uint_32 < 4)) { 2269 ext4_msg(NULL, KERN_ERR, 2270 "Invalid want_extra_isize %d", result.uint_32); 2271 return -EINVAL; 2272 } 2273 ctx->s_want_extra_isize = result.uint_32; 2274 ctx->spec |= EXT4_SPEC_s_want_extra_isize; 2275 return 0; 2276 case Opt_max_batch_time: 2277 ctx->s_max_batch_time = result.uint_32; 2278 ctx->spec |= EXT4_SPEC_s_max_batch_time; 2279 return 0; 2280 case Opt_min_batch_time: 2281 ctx->s_min_batch_time = result.uint_32; 2282 ctx->spec |= EXT4_SPEC_s_min_batch_time; 2283 return 0; 2284 case Opt_inode_readahead_blks: 2285 if (result.uint_32 && 2286 (result.uint_32 > (1 << 30) || 2287 !is_power_of_2(result.uint_32))) { 2288 ext4_msg(NULL, KERN_ERR, 2289 "EXT4-fs: inode_readahead_blks must be " 2290 "0 or a power of 2 smaller than 2^31"); 2291 return -EINVAL; 2292 } 2293 ctx->s_inode_readahead_blks = result.uint_32; 2294 ctx->spec |= EXT4_SPEC_s_inode_readahead_blks; 2295 return 0; 2296 case Opt_init_itable: 2297 ctx_set_mount_opt(ctx, EXT4_MOUNT_INIT_INODE_TABLE); 2298 ctx->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT; 2299 if (param->type == fs_value_is_string) 2300 ctx->s_li_wait_mult = result.uint_32; 2301 ctx->spec |= EXT4_SPEC_s_li_wait_mult; 2302 return 0; 2303 case Opt_max_dir_size_kb: 2304 ctx->s_max_dir_size_kb = result.uint_32; 2305 ctx->spec |= EXT4_SPEC_s_max_dir_size_kb; 2306 return 0; 2307 #ifdef CONFIG_EXT4_DEBUG 2308 case Opt_fc_debug_max_replay: 2309 ctx->s_fc_debug_max_replay = result.uint_32; 2310 ctx->spec |= EXT4_SPEC_s_fc_debug_max_replay; 2311 return 0; 2312 #endif 2313 case Opt_stripe: 2314 ctx->s_stripe = result.uint_32; 2315 ctx->spec |= EXT4_SPEC_s_stripe; 2316 return 0; 2317 case Opt_resuid: 2318 ctx->s_resuid = result.uid; 2319 ctx->spec |= EXT4_SPEC_s_resuid; 2320 return 0; 2321 case Opt_resgid: 2322 ctx->s_resgid = result.gid; 2323 ctx->spec |= EXT4_SPEC_s_resgid; 2324 return 0; 2325 case Opt_journal_dev: 2326 if (is_remount) { 2327 ext4_msg(NULL, KERN_ERR, 2328 "Cannot specify journal on remount"); 2329 return -EINVAL; 2330 } 2331 ctx->journal_devnum = result.uint_32; 2332 ctx->spec |= EXT4_SPEC_JOURNAL_DEV; 2333 return 0; 2334 case Opt_journal_path: 2335 { 2336 struct inode *journal_inode; 2337 struct path path; 2338 int error; 2339 2340 if (is_remount) { 2341 ext4_msg(NULL, KERN_ERR, 2342 "Cannot specify journal on remount"); 2343 return -EINVAL; 2344 } 2345 2346 error = fs_lookup_param(fc, param, 1, LOOKUP_FOLLOW, &path); 2347 if (error) { 2348 ext4_msg(NULL, KERN_ERR, "error: could not find " 2349 "journal device path"); 2350 return -EINVAL; 2351 } 2352 2353 journal_inode = d_inode(path.dentry); 2354 ctx->journal_devnum = new_encode_dev(journal_inode->i_rdev); 2355 ctx->spec |= EXT4_SPEC_JOURNAL_DEV; 2356 path_put(&path); 2357 return 0; 2358 } 2359 case Opt_journal_ioprio: 2360 if (result.uint_32 > 7) { 2361 ext4_msg(NULL, KERN_ERR, "Invalid journal IO priority" 2362 " (must be 0-7)"); 2363 return -EINVAL; 2364 } 2365 ctx->journal_ioprio = 2366 IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, result.uint_32); 2367 ctx->spec |= EXT4_SPEC_JOURNAL_IOPRIO; 2368 return 0; 2369 case Opt_test_dummy_encryption: 2370 return ext4_parse_test_dummy_encryption(param, ctx); 2371 case Opt_dax: 2372 case Opt_dax_type: 2373 #ifdef CONFIG_FS_DAX 2374 { 2375 int type = (token == Opt_dax) ? 2376 Opt_dax : result.uint_32; 2377 2378 switch (type) { 2379 case Opt_dax: 2380 case Opt_dax_always: 2381 ctx_set_mount_opt(ctx, EXT4_MOUNT_DAX_ALWAYS); 2382 ctx_clear_mount_opt2(ctx, EXT4_MOUNT2_DAX_NEVER); 2383 break; 2384 case Opt_dax_never: 2385 ctx_set_mount_opt2(ctx, EXT4_MOUNT2_DAX_NEVER); 2386 ctx_clear_mount_opt(ctx, EXT4_MOUNT_DAX_ALWAYS); 2387 break; 2388 case Opt_dax_inode: 2389 ctx_clear_mount_opt(ctx, EXT4_MOUNT_DAX_ALWAYS); 2390 ctx_clear_mount_opt2(ctx, EXT4_MOUNT2_DAX_NEVER); 2391 /* Strictly for printing options */ 2392 ctx_set_mount_opt2(ctx, EXT4_MOUNT2_DAX_INODE); 2393 break; 2394 } 2395 return 0; 2396 } 2397 #else 2398 ext4_msg(NULL, KERN_INFO, "dax option not supported"); 2399 return -EINVAL; 2400 #endif 2401 case Opt_data_err: 2402 if (result.uint_32 == Opt_data_err_abort) 2403 ctx_set_mount_opt(ctx, m->mount_opt); 2404 else if (result.uint_32 == Opt_data_err_ignore) 2405 ctx_clear_mount_opt(ctx, m->mount_opt); 2406 return 0; 2407 case Opt_mb_optimize_scan: 2408 if (result.int_32 == 1) { 2409 ctx_set_mount_opt2(ctx, EXT4_MOUNT2_MB_OPTIMIZE_SCAN); 2410 ctx->spec |= EXT4_SPEC_mb_optimize_scan; 2411 } else if (result.int_32 == 0) { 2412 ctx_clear_mount_opt2(ctx, EXT4_MOUNT2_MB_OPTIMIZE_SCAN); 2413 ctx->spec |= EXT4_SPEC_mb_optimize_scan; 2414 } else { 2415 ext4_msg(NULL, KERN_WARNING, 2416 "mb_optimize_scan should be set to 0 or 1."); 2417 return -EINVAL; 2418 } 2419 return 0; 2420 } 2421 2422 /* 2423 * At this point we should only be getting options requiring MOPT_SET, 2424 * or MOPT_CLEAR. Anything else is a bug 2425 */ 2426 if (m->token == Opt_err) { 2427 ext4_msg(NULL, KERN_WARNING, "buggy handling of option %s", 2428 param->key); 2429 WARN_ON(1); 2430 return -EINVAL; 2431 } 2432 2433 else { 2434 unsigned int set = 0; 2435 2436 if ((param->type == fs_value_is_flag) || 2437 result.uint_32 > 0) 2438 set = 1; 2439 2440 if (m->flags & MOPT_CLEAR) 2441 set = !set; 2442 else if (unlikely(!(m->flags & MOPT_SET))) { 2443 ext4_msg(NULL, KERN_WARNING, 2444 "buggy handling of option %s", 2445 param->key); 2446 WARN_ON(1); 2447 return -EINVAL; 2448 } 2449 if (m->flags & MOPT_2) { 2450 if (set != 0) 2451 ctx_set_mount_opt2(ctx, m->mount_opt); 2452 else 2453 ctx_clear_mount_opt2(ctx, m->mount_opt); 2454 } else { 2455 if (set != 0) 2456 ctx_set_mount_opt(ctx, m->mount_opt); 2457 else 2458 ctx_clear_mount_opt(ctx, m->mount_opt); 2459 } 2460 } 2461 2462 return 0; 2463 } 2464 2465 static int parse_options(struct fs_context *fc, char *options) 2466 { 2467 struct fs_parameter param; 2468 int ret; 2469 char *key; 2470 2471 if (!options) 2472 return 0; 2473 2474 while ((key = strsep(&options, ",")) != NULL) { 2475 if (*key) { 2476 size_t v_len = 0; 2477 char *value = strchr(key, '='); 2478 2479 param.type = fs_value_is_flag; 2480 param.string = NULL; 2481 2482 if (value) { 2483 if (value == key) 2484 continue; 2485 2486 *value++ = 0; 2487 v_len = strlen(value); 2488 param.string = kmemdup_nul(value, v_len, 2489 GFP_KERNEL); 2490 if (!param.string) 2491 return -ENOMEM; 2492 param.type = fs_value_is_string; 2493 } 2494 2495 param.key = key; 2496 param.size = v_len; 2497 2498 ret = ext4_parse_param(fc, ¶m); 2499 kfree(param.string); 2500 if (ret < 0) 2501 return ret; 2502 } 2503 } 2504 2505 ret = ext4_validate_options(fc); 2506 if (ret < 0) 2507 return ret; 2508 2509 return 0; 2510 } 2511 2512 static int parse_apply_sb_mount_options(struct super_block *sb, 2513 struct ext4_fs_context *m_ctx) 2514 { 2515 struct ext4_sb_info *sbi = EXT4_SB(sb); 2516 char s_mount_opts[64]; 2517 struct ext4_fs_context *s_ctx = NULL; 2518 struct fs_context *fc = NULL; 2519 int ret = -ENOMEM; 2520 2521 if (!sbi->s_es->s_mount_opts[0]) 2522 return 0; 2523 2524 if (strscpy_pad(s_mount_opts, sbi->s_es->s_mount_opts) < 0) 2525 return -E2BIG; 2526 2527 fc = kzalloc_obj(struct fs_context); 2528 if (!fc) 2529 return -ENOMEM; 2530 2531 s_ctx = kzalloc_obj(struct ext4_fs_context); 2532 if (!s_ctx) 2533 goto out_free; 2534 2535 fc->fs_private = s_ctx; 2536 fc->s_fs_info = sbi; 2537 2538 ret = parse_options(fc, s_mount_opts); 2539 if (ret < 0) 2540 goto parse_failed; 2541 2542 ret = ext4_check_opt_consistency(fc, sb); 2543 if (ret < 0) { 2544 parse_failed: 2545 ext4_msg(sb, KERN_WARNING, 2546 "failed to parse options in superblock: %s", 2547 s_mount_opts); 2548 ret = 0; 2549 goto out_free; 2550 } 2551 2552 if (s_ctx->spec & EXT4_SPEC_JOURNAL_DEV) 2553 m_ctx->journal_devnum = s_ctx->journal_devnum; 2554 if (s_ctx->spec & EXT4_SPEC_JOURNAL_IOPRIO) 2555 m_ctx->journal_ioprio = s_ctx->journal_ioprio; 2556 2557 ext4_apply_options(fc, sb); 2558 ret = 0; 2559 2560 out_free: 2561 ext4_fc_free(fc); 2562 kfree(fc); 2563 return ret; 2564 } 2565 2566 static void ext4_apply_quota_options(struct fs_context *fc, 2567 struct super_block *sb) 2568 { 2569 #ifdef CONFIG_QUOTA 2570 bool quota_feature = ext4_has_feature_quota(sb); 2571 struct ext4_fs_context *ctx = fc->fs_private; 2572 struct ext4_sb_info *sbi = EXT4_SB(sb); 2573 char *qname; 2574 int i; 2575 2576 if (quota_feature) 2577 return; 2578 2579 if (ctx->spec & EXT4_SPEC_JQUOTA) { 2580 for (i = 0; i < EXT4_MAXQUOTAS; i++) { 2581 if (!(ctx->qname_spec & (1 << i))) 2582 continue; 2583 2584 qname = ctx->s_qf_names[i]; /* May be NULL */ 2585 if (qname) 2586 set_opt(sb, QUOTA); 2587 ctx->s_qf_names[i] = NULL; 2588 qname = rcu_replace_pointer(sbi->s_qf_names[i], qname, 2589 lockdep_is_held(&sb->s_umount)); 2590 if (qname) 2591 kfree_rcu_mightsleep(qname); 2592 } 2593 } 2594 2595 if (ctx->spec & EXT4_SPEC_JQFMT) 2596 sbi->s_jquota_fmt = ctx->s_jquota_fmt; 2597 #endif 2598 } 2599 2600 /* 2601 * Check quota settings consistency. 2602 */ 2603 static int ext4_check_quota_consistency(struct fs_context *fc, 2604 struct super_block *sb) 2605 { 2606 #ifdef CONFIG_QUOTA 2607 struct ext4_fs_context *ctx = fc->fs_private; 2608 struct ext4_sb_info *sbi = EXT4_SB(sb); 2609 bool quota_feature = ext4_has_feature_quota(sb); 2610 bool quota_loaded = sb_any_quota_loaded(sb); 2611 bool usr_qf_name, grp_qf_name, usrquota, grpquota; 2612 int quota_flags, i; 2613 2614 /* 2615 * We do the test below only for project quotas. 'usrquota' and 2616 * 'grpquota' mount options are allowed even without quota feature 2617 * to support legacy quotas in quota files. 2618 */ 2619 if (ctx_test_mount_opt(ctx, EXT4_MOUNT_PRJQUOTA) && 2620 !ext4_has_feature_project(sb)) { 2621 ext4_msg(NULL, KERN_ERR, "Project quota feature not enabled. " 2622 "Cannot enable project quota enforcement."); 2623 return -EINVAL; 2624 } 2625 2626 quota_flags = EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA | 2627 EXT4_MOUNT_GRPQUOTA | EXT4_MOUNT_PRJQUOTA; 2628 if (quota_loaded && 2629 ctx->mask_s_mount_opt & quota_flags && 2630 !ctx_test_mount_opt(ctx, quota_flags)) 2631 goto err_quota_change; 2632 2633 if (ctx->spec & EXT4_SPEC_JQUOTA) { 2634 2635 for (i = 0; i < EXT4_MAXQUOTAS; i++) { 2636 if (!(ctx->qname_spec & (1 << i))) 2637 continue; 2638 2639 if (quota_loaded && 2640 !!sbi->s_qf_names[i] != !!ctx->s_qf_names[i]) 2641 goto err_jquota_change; 2642 2643 if (sbi->s_qf_names[i] && ctx->s_qf_names[i] && 2644 strcmp(get_qf_name(sb, sbi, i), 2645 ctx->s_qf_names[i]) != 0) 2646 goto err_jquota_specified; 2647 } 2648 2649 if (quota_feature) { 2650 ext4_msg(NULL, KERN_INFO, 2651 "Journaled quota options ignored when " 2652 "QUOTA feature is enabled"); 2653 return 0; 2654 } 2655 } 2656 2657 if (ctx->spec & EXT4_SPEC_JQFMT) { 2658 if (sbi->s_jquota_fmt != ctx->s_jquota_fmt && quota_loaded) 2659 goto err_jquota_change; 2660 if (quota_feature) { 2661 ext4_msg(NULL, KERN_INFO, "Quota format mount options " 2662 "ignored when QUOTA feature is enabled"); 2663 return 0; 2664 } 2665 } 2666 2667 /* Make sure we don't mix old and new quota format */ 2668 usr_qf_name = (get_qf_name(sb, sbi, USRQUOTA) || 2669 ctx->s_qf_names[USRQUOTA]); 2670 grp_qf_name = (get_qf_name(sb, sbi, GRPQUOTA) || 2671 ctx->s_qf_names[GRPQUOTA]); 2672 2673 usrquota = (ctx_test_mount_opt(ctx, EXT4_MOUNT_USRQUOTA) || 2674 test_opt(sb, USRQUOTA)); 2675 2676 grpquota = (ctx_test_mount_opt(ctx, EXT4_MOUNT_GRPQUOTA) || 2677 test_opt(sb, GRPQUOTA)); 2678 2679 if (usr_qf_name) { 2680 ctx_clear_mount_opt(ctx, EXT4_MOUNT_USRQUOTA); 2681 usrquota = false; 2682 } 2683 if (grp_qf_name) { 2684 ctx_clear_mount_opt(ctx, EXT4_MOUNT_GRPQUOTA); 2685 grpquota = false; 2686 } 2687 2688 if (usr_qf_name || grp_qf_name) { 2689 if (usrquota || grpquota) { 2690 ext4_msg(NULL, KERN_ERR, "old and new quota " 2691 "format mixing"); 2692 return -EINVAL; 2693 } 2694 2695 if (!(ctx->spec & EXT4_SPEC_JQFMT || sbi->s_jquota_fmt)) { 2696 ext4_msg(NULL, KERN_ERR, "journaled quota format " 2697 "not specified"); 2698 return -EINVAL; 2699 } 2700 } 2701 2702 return 0; 2703 2704 err_quota_change: 2705 ext4_msg(NULL, KERN_ERR, 2706 "Cannot change quota options when quota turned on"); 2707 return -EINVAL; 2708 err_jquota_change: 2709 ext4_msg(NULL, KERN_ERR, "Cannot change journaled quota " 2710 "options when quota turned on"); 2711 return -EINVAL; 2712 err_jquota_specified: 2713 ext4_msg(NULL, KERN_ERR, "%s quota file already specified", 2714 QTYPE2NAME(i)); 2715 return -EINVAL; 2716 #else 2717 return 0; 2718 #endif 2719 } 2720 2721 static int ext4_check_test_dummy_encryption(const struct fs_context *fc, 2722 struct super_block *sb) 2723 { 2724 const struct ext4_fs_context *ctx = fc->fs_private; 2725 const struct ext4_sb_info *sbi = EXT4_SB(sb); 2726 2727 if (!fscrypt_is_dummy_policy_set(&ctx->dummy_enc_policy)) 2728 return 0; 2729 2730 if (!ext4_has_feature_encrypt(sb)) { 2731 ext4_msg(NULL, KERN_WARNING, 2732 "test_dummy_encryption requires encrypt feature"); 2733 return -EINVAL; 2734 } 2735 /* 2736 * This mount option is just for testing, and it's not worthwhile to 2737 * implement the extra complexity (e.g. RCU protection) that would be 2738 * needed to allow it to be set or changed during remount. We do allow 2739 * it to be specified during remount, but only if there is no change. 2740 */ 2741 if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) { 2742 if (fscrypt_dummy_policies_equal(&sbi->s_dummy_enc_policy, 2743 &ctx->dummy_enc_policy)) 2744 return 0; 2745 ext4_msg(NULL, KERN_WARNING, 2746 "Can't set or change test_dummy_encryption on remount"); 2747 return -EINVAL; 2748 } 2749 /* Also make sure s_mount_opts didn't contain a conflicting value. */ 2750 if (fscrypt_is_dummy_policy_set(&sbi->s_dummy_enc_policy)) { 2751 if (fscrypt_dummy_policies_equal(&sbi->s_dummy_enc_policy, 2752 &ctx->dummy_enc_policy)) 2753 return 0; 2754 ext4_msg(NULL, KERN_WARNING, 2755 "Conflicting test_dummy_encryption options"); 2756 return -EINVAL; 2757 } 2758 return 0; 2759 } 2760 2761 static void ext4_apply_test_dummy_encryption(struct ext4_fs_context *ctx, 2762 struct super_block *sb) 2763 { 2764 if (!fscrypt_is_dummy_policy_set(&ctx->dummy_enc_policy) || 2765 /* if already set, it was already verified to be the same */ 2766 fscrypt_is_dummy_policy_set(&EXT4_SB(sb)->s_dummy_enc_policy)) 2767 return; 2768 EXT4_SB(sb)->s_dummy_enc_policy = ctx->dummy_enc_policy; 2769 memset(&ctx->dummy_enc_policy, 0, sizeof(ctx->dummy_enc_policy)); 2770 ext4_msg(sb, KERN_WARNING, "Test dummy encryption mode enabled"); 2771 } 2772 2773 static int ext4_check_opt_consistency(struct fs_context *fc, 2774 struct super_block *sb) 2775 { 2776 struct ext4_fs_context *ctx = fc->fs_private; 2777 struct ext4_sb_info *sbi = fc->s_fs_info; 2778 int is_remount = fc->purpose == FS_CONTEXT_FOR_RECONFIGURE; 2779 int err; 2780 2781 if ((ctx->opt_flags & MOPT_NO_EXT2) && IS_EXT2_SB(sb)) { 2782 ext4_msg(NULL, KERN_ERR, 2783 "Mount option(s) incompatible with ext2"); 2784 return -EINVAL; 2785 } 2786 if ((ctx->opt_flags & MOPT_NO_EXT3) && IS_EXT3_SB(sb)) { 2787 ext4_msg(NULL, KERN_ERR, 2788 "Mount option(s) incompatible with ext3"); 2789 return -EINVAL; 2790 } 2791 2792 if (ctx->s_want_extra_isize > 2793 (sbi->s_inode_size - EXT4_GOOD_OLD_INODE_SIZE)) { 2794 ext4_msg(NULL, KERN_ERR, 2795 "Invalid want_extra_isize %d", 2796 ctx->s_want_extra_isize); 2797 return -EINVAL; 2798 } 2799 2800 err = ext4_check_test_dummy_encryption(fc, sb); 2801 if (err) 2802 return err; 2803 2804 if ((ctx->spec & EXT4_SPEC_DATAJ) && is_remount) { 2805 if (!sbi->s_journal) { 2806 ext4_msg(NULL, KERN_WARNING, 2807 "Remounting file system with no journal " 2808 "so ignoring journalled data option"); 2809 ctx_clear_mount_opt(ctx, EXT4_MOUNT_DATA_FLAGS); 2810 } else if (ctx_test_mount_opt(ctx, EXT4_MOUNT_DATA_FLAGS) != 2811 test_opt(sb, DATA_FLAGS)) { 2812 ext4_msg(NULL, KERN_ERR, "Cannot change data mode " 2813 "on remount"); 2814 return -EINVAL; 2815 } 2816 } 2817 2818 if (is_remount) { 2819 if (!sbi->s_journal && 2820 ctx_test_mount_opt(ctx, EXT4_MOUNT_DATA_ERR_ABORT)) { 2821 ext4_msg(NULL, KERN_WARNING, 2822 "Remounting fs w/o journal so ignoring data_err option"); 2823 ctx_clear_mount_opt(ctx, EXT4_MOUNT_DATA_ERR_ABORT); 2824 } 2825 2826 if (ctx_test_mount_opt(ctx, EXT4_MOUNT_DAX_ALWAYS) && 2827 (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)) { 2828 ext4_msg(NULL, KERN_ERR, "can't mount with " 2829 "both data=journal and dax"); 2830 return -EINVAL; 2831 } 2832 2833 if (ctx_test_mount_opt(ctx, EXT4_MOUNT_DAX_ALWAYS) && 2834 (!(sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) || 2835 (sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER))) { 2836 fail_dax_change_remount: 2837 ext4_msg(NULL, KERN_ERR, "can't change " 2838 "dax mount option while remounting"); 2839 return -EINVAL; 2840 } else if (ctx_test_mount_opt2(ctx, EXT4_MOUNT2_DAX_NEVER) && 2841 (!(sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER) || 2842 (sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS))) { 2843 goto fail_dax_change_remount; 2844 } else if (ctx_test_mount_opt2(ctx, EXT4_MOUNT2_DAX_INODE) && 2845 ((sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) || 2846 (sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER) || 2847 !(sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_INODE))) { 2848 goto fail_dax_change_remount; 2849 } 2850 } 2851 2852 return ext4_check_quota_consistency(fc, sb); 2853 } 2854 2855 static void ext4_apply_options(struct fs_context *fc, struct super_block *sb) 2856 { 2857 struct ext4_fs_context *ctx = fc->fs_private; 2858 struct ext4_sb_info *sbi = fc->s_fs_info; 2859 2860 sbi->s_mount_opt &= ~ctx->mask_s_mount_opt; 2861 sbi->s_mount_opt |= ctx->vals_s_mount_opt; 2862 sbi->s_mount_opt2 &= ~ctx->mask_s_mount_opt2; 2863 sbi->s_mount_opt2 |= ctx->vals_s_mount_opt2; 2864 sb->s_flags &= ~ctx->mask_s_flags; 2865 sb->s_flags |= ctx->vals_s_flags; 2866 2867 #define APPLY(X) ({ if (ctx->spec & EXT4_SPEC_##X) sbi->X = ctx->X; }) 2868 APPLY(s_commit_interval); 2869 APPLY(s_stripe); 2870 APPLY(s_max_batch_time); 2871 APPLY(s_min_batch_time); 2872 APPLY(s_want_extra_isize); 2873 APPLY(s_inode_readahead_blks); 2874 APPLY(s_max_dir_size_kb); 2875 APPLY(s_li_wait_mult); 2876 APPLY(s_resgid); 2877 APPLY(s_resuid); 2878 2879 #ifdef CONFIG_EXT4_DEBUG 2880 APPLY(s_fc_debug_max_replay); 2881 #endif 2882 2883 ext4_apply_quota_options(fc, sb); 2884 ext4_apply_test_dummy_encryption(ctx, sb); 2885 } 2886 2887 2888 static int ext4_validate_options(struct fs_context *fc) 2889 { 2890 #ifdef CONFIG_QUOTA 2891 struct ext4_fs_context *ctx = fc->fs_private; 2892 char *usr_qf_name, *grp_qf_name; 2893 2894 usr_qf_name = ctx->s_qf_names[USRQUOTA]; 2895 grp_qf_name = ctx->s_qf_names[GRPQUOTA]; 2896 2897 if (usr_qf_name || grp_qf_name) { 2898 if (ctx_test_mount_opt(ctx, EXT4_MOUNT_USRQUOTA) && usr_qf_name) 2899 ctx_clear_mount_opt(ctx, EXT4_MOUNT_USRQUOTA); 2900 2901 if (ctx_test_mount_opt(ctx, EXT4_MOUNT_GRPQUOTA) && grp_qf_name) 2902 ctx_clear_mount_opt(ctx, EXT4_MOUNT_GRPQUOTA); 2903 2904 if (ctx_test_mount_opt(ctx, EXT4_MOUNT_USRQUOTA) || 2905 ctx_test_mount_opt(ctx, EXT4_MOUNT_GRPQUOTA)) { 2906 ext4_msg(NULL, KERN_ERR, "old and new quota " 2907 "format mixing"); 2908 return -EINVAL; 2909 } 2910 } 2911 #endif 2912 return 1; 2913 } 2914 2915 static inline void ext4_show_quota_options(struct seq_file *seq, 2916 struct super_block *sb) 2917 { 2918 #if defined(CONFIG_QUOTA) 2919 struct ext4_sb_info *sbi = EXT4_SB(sb); 2920 char *usr_qf_name, *grp_qf_name; 2921 2922 if (sbi->s_jquota_fmt) { 2923 char *fmtname = ""; 2924 2925 switch (sbi->s_jquota_fmt) { 2926 case QFMT_VFS_OLD: 2927 fmtname = "vfsold"; 2928 break; 2929 case QFMT_VFS_V0: 2930 fmtname = "vfsv0"; 2931 break; 2932 case QFMT_VFS_V1: 2933 fmtname = "vfsv1"; 2934 break; 2935 } 2936 seq_printf(seq, ",jqfmt=%s", fmtname); 2937 } 2938 2939 rcu_read_lock(); 2940 usr_qf_name = rcu_dereference(sbi->s_qf_names[USRQUOTA]); 2941 grp_qf_name = rcu_dereference(sbi->s_qf_names[GRPQUOTA]); 2942 if (usr_qf_name) 2943 seq_show_option(seq, "usrjquota", usr_qf_name); 2944 if (grp_qf_name) 2945 seq_show_option(seq, "grpjquota", grp_qf_name); 2946 rcu_read_unlock(); 2947 #endif 2948 } 2949 2950 static const char *token2str(int token) 2951 { 2952 const struct fs_parameter_spec *spec; 2953 2954 for (spec = ext4_param_specs; spec->name != NULL; spec++) 2955 if (spec->opt == token && !spec->type) 2956 break; 2957 return spec->name; 2958 } 2959 2960 /* 2961 * Show an option if 2962 * - it's set to a non-default value OR 2963 * - if the per-sb default is different from the global default 2964 */ 2965 static int _ext4_show_options(struct seq_file *seq, struct super_block *sb, 2966 int nodefs) 2967 { 2968 struct ext4_sb_info *sbi = EXT4_SB(sb); 2969 struct ext4_super_block *es = sbi->s_es; 2970 int def_errors; 2971 const struct mount_opts *m; 2972 char sep = nodefs ? '\n' : ','; 2973 2974 #define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep) 2975 #define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg) 2976 2977 if (sbi->s_sb_block != 1) 2978 SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block); 2979 2980 for (m = ext4_mount_opts; m->token != Opt_err; m++) { 2981 int want_set = m->flags & MOPT_SET; 2982 int opt_2 = m->flags & MOPT_2; 2983 unsigned int mount_opt, def_mount_opt; 2984 2985 if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) || 2986 m->flags & MOPT_SKIP) 2987 continue; 2988 2989 if (opt_2) { 2990 mount_opt = sbi->s_mount_opt2; 2991 def_mount_opt = sbi->s_def_mount_opt2; 2992 } else { 2993 mount_opt = sbi->s_mount_opt; 2994 def_mount_opt = sbi->s_def_mount_opt; 2995 } 2996 /* skip if same as the default */ 2997 if (!nodefs && !(m->mount_opt & (mount_opt ^ def_mount_opt))) 2998 continue; 2999 /* select Opt_noFoo vs Opt_Foo */ 3000 if ((want_set && 3001 (mount_opt & m->mount_opt) != m->mount_opt) || 3002 (!want_set && (mount_opt & m->mount_opt))) 3003 continue; 3004 SEQ_OPTS_PRINT("%s", token2str(m->token)); 3005 } 3006 3007 if (nodefs || !uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT4_DEF_RESUID)) || 3008 ext4_get_resuid(es) != EXT4_DEF_RESUID) 3009 SEQ_OPTS_PRINT("resuid=%u", 3010 from_kuid_munged(&init_user_ns, sbi->s_resuid)); 3011 if (nodefs || !gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT4_DEF_RESGID)) || 3012 ext4_get_resgid(es) != EXT4_DEF_RESGID) 3013 SEQ_OPTS_PRINT("resgid=%u", 3014 from_kgid_munged(&init_user_ns, sbi->s_resgid)); 3015 def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors); 3016 if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO) 3017 SEQ_OPTS_PUTS("errors=remount-ro"); 3018 if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE) 3019 SEQ_OPTS_PUTS("errors=continue"); 3020 if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC) 3021 SEQ_OPTS_PUTS("errors=panic"); 3022 if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) 3023 SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ); 3024 if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME) 3025 SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time); 3026 if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME) 3027 SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time); 3028 if (nodefs && sb->s_flags & SB_I_VERSION) 3029 SEQ_OPTS_PUTS("i_version"); 3030 if (nodefs || sbi->s_stripe) 3031 SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe); 3032 if (nodefs || EXT4_MOUNT_DATA_FLAGS & 3033 (sbi->s_mount_opt ^ sbi->s_def_mount_opt)) { 3034 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) 3035 SEQ_OPTS_PUTS("data=journal"); 3036 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) 3037 SEQ_OPTS_PUTS("data=ordered"); 3038 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA) 3039 SEQ_OPTS_PUTS("data=writeback"); 3040 } 3041 if (nodefs || 3042 sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS) 3043 SEQ_OPTS_PRINT("inode_readahead_blks=%u", 3044 sbi->s_inode_readahead_blks); 3045 3046 if (test_opt(sb, INIT_INODE_TABLE) && (nodefs || 3047 (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT))) 3048 SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult); 3049 if (nodefs || sbi->s_max_dir_size_kb) 3050 SEQ_OPTS_PRINT("max_dir_size_kb=%u", sbi->s_max_dir_size_kb); 3051 if (test_opt(sb, DATA_ERR_ABORT)) 3052 SEQ_OPTS_PUTS("data_err=abort"); 3053 3054 fscrypt_show_test_dummy_encryption(seq, sep, sb); 3055 3056 if (sb->s_flags & SB_INLINECRYPT) 3057 SEQ_OPTS_PUTS("inlinecrypt"); 3058 3059 if (test_opt(sb, DAX_ALWAYS)) { 3060 if (IS_EXT2_SB(sb)) 3061 SEQ_OPTS_PUTS("dax"); 3062 else 3063 SEQ_OPTS_PUTS("dax=always"); 3064 } else if (test_opt2(sb, DAX_NEVER)) { 3065 SEQ_OPTS_PUTS("dax=never"); 3066 } else if (test_opt2(sb, DAX_INODE)) { 3067 SEQ_OPTS_PUTS("dax=inode"); 3068 } 3069 3070 if (sbi->s_groups_count >= MB_DEFAULT_LINEAR_SCAN_THRESHOLD && 3071 !test_opt2(sb, MB_OPTIMIZE_SCAN)) { 3072 SEQ_OPTS_PUTS("mb_optimize_scan=0"); 3073 } else if (sbi->s_groups_count < MB_DEFAULT_LINEAR_SCAN_THRESHOLD && 3074 test_opt2(sb, MB_OPTIMIZE_SCAN)) { 3075 SEQ_OPTS_PUTS("mb_optimize_scan=1"); 3076 } 3077 3078 if (nodefs && !test_opt(sb, NO_PREFETCH_BLOCK_BITMAPS)) 3079 SEQ_OPTS_PUTS("prefetch_block_bitmaps"); 3080 3081 if (ext4_emergency_ro(sb)) 3082 SEQ_OPTS_PUTS("emergency_ro"); 3083 3084 if (ext4_forced_shutdown(sb)) 3085 SEQ_OPTS_PUTS("shutdown"); 3086 3087 ext4_show_quota_options(seq, sb); 3088 return 0; 3089 } 3090 3091 static int ext4_show_options(struct seq_file *seq, struct dentry *root) 3092 { 3093 return _ext4_show_options(seq, root->d_sb, 0); 3094 } 3095 3096 int ext4_seq_options_show(struct seq_file *seq, void *offset) 3097 { 3098 struct super_block *sb = seq->private; 3099 int rc; 3100 3101 seq_puts(seq, sb_rdonly(sb) ? "ro" : "rw"); 3102 rc = _ext4_show_options(seq, sb, 1); 3103 seq_putc(seq, '\n'); 3104 return rc; 3105 } 3106 3107 static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es, 3108 int read_only) 3109 { 3110 struct ext4_sb_info *sbi = EXT4_SB(sb); 3111 int err = 0; 3112 3113 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) { 3114 ext4_msg(sb, KERN_ERR, "revision level too high, " 3115 "forcing read-only mode"); 3116 err = -EROFS; 3117 goto done; 3118 } 3119 if (read_only) 3120 goto done; 3121 if (!(sbi->s_mount_state & EXT4_VALID_FS)) 3122 ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, " 3123 "running e2fsck is recommended"); 3124 else if (sbi->s_mount_state & EXT4_ERROR_FS) 3125 ext4_msg(sb, KERN_WARNING, 3126 "warning: mounting fs with errors, " 3127 "running e2fsck is recommended"); 3128 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 && 3129 le16_to_cpu(es->s_mnt_count) >= 3130 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count)) 3131 ext4_msg(sb, KERN_WARNING, 3132 "warning: maximal mount count reached, " 3133 "running e2fsck is recommended"); 3134 else if (le32_to_cpu(es->s_checkinterval) && 3135 (ext4_get_tstamp(es, s_lastcheck) + 3136 le32_to_cpu(es->s_checkinterval) <= ktime_get_real_seconds())) 3137 ext4_msg(sb, KERN_WARNING, 3138 "warning: checktime reached, " 3139 "running e2fsck is recommended"); 3140 if (!sbi->s_journal) 3141 es->s_state &= cpu_to_le16(~EXT4_VALID_FS); 3142 if (!(__s16) le16_to_cpu(es->s_max_mnt_count)) 3143 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT); 3144 le16_add_cpu(&es->s_mnt_count, 1); 3145 ext4_update_tstamp(es, s_mtime); 3146 if (sbi->s_journal) { 3147 ext4_set_feature_journal_needs_recovery(sb); 3148 if (ext4_has_feature_orphan_file(sb)) 3149 ext4_set_feature_orphan_present(sb); 3150 } 3151 3152 err = ext4_commit_super(sb); 3153 done: 3154 if (test_opt(sb, DEBUG)) 3155 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, " 3156 "bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n", 3157 sb->s_blocksize, 3158 sbi->s_groups_count, 3159 EXT4_BLOCKS_PER_GROUP(sb), 3160 EXT4_INODES_PER_GROUP(sb), 3161 sbi->s_mount_opt, sbi->s_mount_opt2); 3162 return err; 3163 } 3164 3165 int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup) 3166 { 3167 struct ext4_sb_info *sbi = EXT4_SB(sb); 3168 struct flex_groups **old_groups, **new_groups; 3169 int size, i, j; 3170 3171 if (!sbi->s_log_groups_per_flex) 3172 return 0; 3173 3174 size = ext4_flex_group(sbi, ngroup - 1) + 1; 3175 if (size <= sbi->s_flex_groups_allocated) 3176 return 0; 3177 3178 new_groups = kvzalloc(roundup_pow_of_two(size * 3179 sizeof(*sbi->s_flex_groups)), GFP_KERNEL); 3180 if (!new_groups) { 3181 ext4_msg(sb, KERN_ERR, 3182 "not enough memory for %d flex group pointers", size); 3183 return -ENOMEM; 3184 } 3185 for (i = sbi->s_flex_groups_allocated; i < size; i++) { 3186 new_groups[i] = kvzalloc(roundup_pow_of_two( 3187 sizeof(struct flex_groups)), 3188 GFP_KERNEL); 3189 if (!new_groups[i]) { 3190 for (j = sbi->s_flex_groups_allocated; j < i; j++) 3191 kvfree(new_groups[j]); 3192 kvfree(new_groups); 3193 ext4_msg(sb, KERN_ERR, 3194 "not enough memory for %d flex groups", size); 3195 return -ENOMEM; 3196 } 3197 } 3198 rcu_read_lock(); 3199 old_groups = rcu_dereference(sbi->s_flex_groups); 3200 if (old_groups) 3201 memcpy(new_groups, old_groups, 3202 (sbi->s_flex_groups_allocated * 3203 sizeof(struct flex_groups *))); 3204 rcu_read_unlock(); 3205 rcu_assign_pointer(sbi->s_flex_groups, new_groups); 3206 sbi->s_flex_groups_allocated = size; 3207 if (old_groups) 3208 ext4_kvfree_array_rcu(old_groups); 3209 return 0; 3210 } 3211 3212 static int ext4_fill_flex_info(struct super_block *sb) 3213 { 3214 struct ext4_sb_info *sbi = EXT4_SB(sb); 3215 struct ext4_group_desc *gdp = NULL; 3216 struct flex_groups *fg; 3217 ext4_group_t flex_group; 3218 int i, err; 3219 3220 sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex; 3221 if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) { 3222 sbi->s_log_groups_per_flex = 0; 3223 return 1; 3224 } 3225 3226 err = ext4_alloc_flex_bg_array(sb, sbi->s_groups_count); 3227 if (err) 3228 goto failed; 3229 3230 for (i = 0; i < sbi->s_groups_count; i++) { 3231 gdp = ext4_get_group_desc(sb, i, NULL); 3232 3233 flex_group = ext4_flex_group(sbi, i); 3234 fg = sbi_array_rcu_deref(sbi, s_flex_groups, flex_group); 3235 atomic_add(ext4_free_inodes_count(sb, gdp), &fg->free_inodes); 3236 atomic64_add(ext4_free_group_clusters(sb, gdp), 3237 &fg->free_clusters); 3238 atomic_add(ext4_used_dirs_count(sb, gdp), &fg->used_dirs); 3239 } 3240 3241 return 1; 3242 failed: 3243 return 0; 3244 } 3245 3246 static __le16 ext4_group_desc_csum(struct super_block *sb, __u32 block_group, 3247 struct ext4_group_desc *gdp) 3248 { 3249 int offset = offsetof(struct ext4_group_desc, bg_checksum); 3250 __u16 crc = 0; 3251 __le32 le_group = cpu_to_le32(block_group); 3252 struct ext4_sb_info *sbi = EXT4_SB(sb); 3253 3254 if (ext4_has_feature_metadata_csum(sbi->s_sb)) { 3255 /* Use new metadata_csum algorithm */ 3256 __u32 csum32; 3257 __u16 dummy_csum = 0; 3258 3259 csum32 = ext4_chksum(sbi->s_csum_seed, (__u8 *)&le_group, 3260 sizeof(le_group)); 3261 csum32 = ext4_chksum(csum32, (__u8 *)gdp, offset); 3262 csum32 = ext4_chksum(csum32, (__u8 *)&dummy_csum, 3263 sizeof(dummy_csum)); 3264 offset += sizeof(dummy_csum); 3265 if (offset < sbi->s_desc_size) 3266 csum32 = ext4_chksum(csum32, (__u8 *)gdp + offset, 3267 sbi->s_desc_size - offset); 3268 3269 crc = csum32 & 0xFFFF; 3270 goto out; 3271 } 3272 3273 /* old crc16 code */ 3274 if (!ext4_has_feature_gdt_csum(sb)) 3275 return 0; 3276 3277 crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid)); 3278 crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group)); 3279 crc = crc16(crc, (__u8 *)gdp, offset); 3280 offset += sizeof(gdp->bg_checksum); /* skip checksum */ 3281 /* for checksum of struct ext4_group_desc do the rest...*/ 3282 if (ext4_has_feature_64bit(sb) && offset < sbi->s_desc_size) 3283 crc = crc16(crc, (__u8 *)gdp + offset, 3284 sbi->s_desc_size - offset); 3285 3286 out: 3287 return cpu_to_le16(crc); 3288 } 3289 3290 int ext4_group_desc_csum_verify(struct super_block *sb, __u32 block_group, 3291 struct ext4_group_desc *gdp) 3292 { 3293 if (ext4_has_group_desc_csum(sb) && 3294 (gdp->bg_checksum != ext4_group_desc_csum(sb, block_group, gdp))) 3295 return 0; 3296 3297 return 1; 3298 } 3299 3300 void ext4_group_desc_csum_set(struct super_block *sb, __u32 block_group, 3301 struct ext4_group_desc *gdp) 3302 { 3303 if (!ext4_has_group_desc_csum(sb)) 3304 return; 3305 gdp->bg_checksum = ext4_group_desc_csum(sb, block_group, gdp); 3306 } 3307 3308 /* Called at mount-time, super-block is locked */ 3309 static int ext4_check_descriptors(struct super_block *sb, 3310 ext4_fsblk_t sb_block, 3311 ext4_group_t *first_not_zeroed) 3312 { 3313 struct ext4_sb_info *sbi = EXT4_SB(sb); 3314 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block); 3315 ext4_fsblk_t last_block; 3316 ext4_fsblk_t last_bg_block = sb_block + ext4_bg_num_gdb(sb, 0); 3317 ext4_fsblk_t block_bitmap; 3318 ext4_fsblk_t inode_bitmap; 3319 ext4_fsblk_t inode_table; 3320 int flexbg_flag = 0; 3321 ext4_group_t i, grp = sbi->s_groups_count; 3322 3323 if (ext4_has_feature_flex_bg(sb)) 3324 flexbg_flag = 1; 3325 3326 ext4_debug("Checking group descriptors"); 3327 3328 for (i = 0; i < sbi->s_groups_count; i++) { 3329 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL); 3330 3331 if (i == sbi->s_groups_count - 1 || flexbg_flag) 3332 last_block = ext4_blocks_count(sbi->s_es) - 1; 3333 else 3334 last_block = first_block + 3335 (EXT4_BLOCKS_PER_GROUP(sb) - 1); 3336 3337 if ((grp == sbi->s_groups_count) && 3338 !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED))) 3339 grp = i; 3340 3341 block_bitmap = ext4_block_bitmap(sb, gdp); 3342 if (block_bitmap == sb_block) { 3343 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " 3344 "Block bitmap for group %u overlaps " 3345 "superblock", i); 3346 if (!sb_rdonly(sb)) 3347 return 0; 3348 } 3349 if (block_bitmap >= sb_block + 1 && 3350 block_bitmap <= last_bg_block) { 3351 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " 3352 "Block bitmap for group %u overlaps " 3353 "block group descriptors", i); 3354 if (!sb_rdonly(sb)) 3355 return 0; 3356 } 3357 if (block_bitmap < first_block || block_bitmap > last_block) { 3358 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " 3359 "Block bitmap for group %u not in group " 3360 "(block %llu)!", i, block_bitmap); 3361 return 0; 3362 } 3363 inode_bitmap = ext4_inode_bitmap(sb, gdp); 3364 if (inode_bitmap == sb_block) { 3365 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " 3366 "Inode bitmap for group %u overlaps " 3367 "superblock", i); 3368 if (!sb_rdonly(sb)) 3369 return 0; 3370 } 3371 if (inode_bitmap >= sb_block + 1 && 3372 inode_bitmap <= last_bg_block) { 3373 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " 3374 "Inode bitmap for group %u overlaps " 3375 "block group descriptors", i); 3376 if (!sb_rdonly(sb)) 3377 return 0; 3378 } 3379 if (inode_bitmap < first_block || inode_bitmap > last_block) { 3380 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " 3381 "Inode bitmap for group %u not in group " 3382 "(block %llu)!", i, inode_bitmap); 3383 return 0; 3384 } 3385 inode_table = ext4_inode_table(sb, gdp); 3386 if (inode_table == sb_block) { 3387 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " 3388 "Inode table for group %u overlaps " 3389 "superblock", i); 3390 if (!sb_rdonly(sb)) 3391 return 0; 3392 } 3393 if (inode_table >= sb_block + 1 && 3394 inode_table <= last_bg_block) { 3395 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " 3396 "Inode table for group %u overlaps " 3397 "block group descriptors", i); 3398 if (!sb_rdonly(sb)) 3399 return 0; 3400 } 3401 if (inode_table < first_block || 3402 inode_table + sbi->s_itb_per_group - 1 > last_block) { 3403 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " 3404 "Inode table for group %u not in group " 3405 "(block %llu)!", i, inode_table); 3406 return 0; 3407 } 3408 ext4_lock_group(sb, i); 3409 if (!ext4_group_desc_csum_verify(sb, i, gdp)) { 3410 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " 3411 "Checksum for group %u failed (%u!=%u)", 3412 i, le16_to_cpu(ext4_group_desc_csum(sb, i, 3413 gdp)), le16_to_cpu(gdp->bg_checksum)); 3414 if (!sb_rdonly(sb)) { 3415 ext4_unlock_group(sb, i); 3416 return 0; 3417 } 3418 } 3419 ext4_unlock_group(sb, i); 3420 if (!flexbg_flag) 3421 first_block += EXT4_BLOCKS_PER_GROUP(sb); 3422 } 3423 if (NULL != first_not_zeroed) 3424 *first_not_zeroed = grp; 3425 return 1; 3426 } 3427 3428 /* 3429 * Maximal extent format file size. 3430 * Resulting logical blkno at s_maxbytes must fit in our on-disk 3431 * extent format containers, within a sector_t, and within i_blocks 3432 * in the vfs. ext4 inode has 48 bits of i_block in fsblock units, 3433 * so that won't be a limiting factor. 3434 * 3435 * However there is other limiting factor. We do store extents in the form 3436 * of starting block and length, hence the resulting length of the extent 3437 * covering maximum file size must fit into on-disk format containers as 3438 * well. Given that length is always by 1 unit bigger than max unit (because 3439 * we count 0 as well) we have to lower the s_maxbytes by one fs block. 3440 * 3441 * Note, this does *not* consider any metadata overhead for vfs i_blocks. 3442 */ 3443 static loff_t ext4_max_size(int blkbits, int has_huge_files) 3444 { 3445 loff_t res; 3446 loff_t upper_limit = MAX_LFS_FILESIZE; 3447 3448 BUILD_BUG_ON(sizeof(blkcnt_t) < sizeof(u64)); 3449 3450 if (!has_huge_files) { 3451 upper_limit = (1LL << 32) - 1; 3452 3453 /* total blocks in file system block size */ 3454 upper_limit >>= (blkbits - 9); 3455 upper_limit <<= blkbits; 3456 } 3457 3458 /* 3459 * 32-bit extent-start container, ee_block. We lower the maxbytes 3460 * by one fs block, so ee_len can cover the extent of maximum file 3461 * size 3462 */ 3463 res = (1LL << 32) - 1; 3464 res <<= blkbits; 3465 3466 /* Sanity check against vm- & vfs- imposed limits */ 3467 if (res > upper_limit) 3468 res = upper_limit; 3469 3470 return res; 3471 } 3472 3473 /* 3474 * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect 3475 * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks. 3476 * We need to be 1 filesystem block less than the 2^48 sector limit. 3477 */ 3478 static loff_t ext4_max_bitmap_size(int bits, int has_huge_files) 3479 { 3480 loff_t upper_limit, res = EXT4_NDIR_BLOCKS; 3481 int meta_blocks; 3482 unsigned int ppb = 1 << (bits - 2); 3483 3484 /* 3485 * This is calculated to be the largest file size for a dense, block 3486 * mapped file such that the file's total number of 512-byte sectors, 3487 * including data and all indirect blocks, does not exceed (2^48 - 1). 3488 * 3489 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total 3490 * number of 512-byte sectors of the file. 3491 */ 3492 if (!has_huge_files) { 3493 /* 3494 * !has_huge_files or implies that the inode i_block field 3495 * represents total file blocks in 2^32 512-byte sectors == 3496 * size of vfs inode i_blocks * 8 3497 */ 3498 upper_limit = (1LL << 32) - 1; 3499 3500 /* total blocks in file system block size */ 3501 upper_limit >>= (bits - 9); 3502 3503 } else { 3504 /* 3505 * We use 48 bit ext4_inode i_blocks 3506 * With EXT4_HUGE_FILE_FL set the i_blocks 3507 * represent total number of blocks in 3508 * file system block size 3509 */ 3510 upper_limit = (1LL << 48) - 1; 3511 3512 } 3513 3514 /* Compute how many blocks we can address by block tree */ 3515 res += ppb; 3516 res += ppb * ppb; 3517 res += ((loff_t)ppb) * ppb * ppb; 3518 /* Compute how many metadata blocks are needed */ 3519 meta_blocks = 1; 3520 meta_blocks += 1 + ppb; 3521 meta_blocks += 1 + ppb + ppb * ppb; 3522 /* Does block tree limit file size? */ 3523 if (res + meta_blocks <= upper_limit) 3524 goto check_lfs; 3525 3526 res = upper_limit; 3527 /* How many metadata blocks are needed for addressing upper_limit? */ 3528 upper_limit -= EXT4_NDIR_BLOCKS; 3529 /* indirect blocks */ 3530 meta_blocks = 1; 3531 upper_limit -= ppb; 3532 /* double indirect blocks */ 3533 if (upper_limit < ppb * ppb) { 3534 meta_blocks += 1 + DIV_ROUND_UP_ULL(upper_limit, ppb); 3535 res -= meta_blocks; 3536 goto check_lfs; 3537 } 3538 meta_blocks += 1 + ppb; 3539 upper_limit -= ppb * ppb; 3540 /* tripple indirect blocks for the rest */ 3541 meta_blocks += 1 + DIV_ROUND_UP_ULL(upper_limit, ppb) + 3542 DIV_ROUND_UP_ULL(upper_limit, ppb*ppb); 3543 res -= meta_blocks; 3544 check_lfs: 3545 res <<= bits; 3546 if (res > MAX_LFS_FILESIZE) 3547 res = MAX_LFS_FILESIZE; 3548 3549 return res; 3550 } 3551 3552 static ext4_fsblk_t descriptor_loc(struct super_block *sb, 3553 ext4_fsblk_t logical_sb_block, int nr) 3554 { 3555 struct ext4_sb_info *sbi = EXT4_SB(sb); 3556 ext4_group_t bg, first_meta_bg; 3557 int has_super = 0; 3558 3559 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg); 3560 3561 if (!ext4_has_feature_meta_bg(sb) || nr < first_meta_bg) 3562 return logical_sb_block + nr + 1; 3563 bg = sbi->s_desc_per_block * nr; 3564 if (ext4_bg_has_super(sb, bg)) 3565 has_super = 1; 3566 3567 /* 3568 * If we have a meta_bg fs with 1k blocks, group 0's GDT is at 3569 * block 2, not 1. If s_first_data_block == 0 (bigalloc is enabled 3570 * on modern mke2fs or blksize > 1k on older mke2fs) then we must 3571 * compensate. 3572 */ 3573 if (sb->s_blocksize == 1024 && nr == 0 && 3574 le32_to_cpu(sbi->s_es->s_first_data_block) == 0) 3575 has_super++; 3576 3577 return (has_super + ext4_group_first_block_no(sb, bg)); 3578 } 3579 3580 /** 3581 * ext4_get_stripe_size: Get the stripe size. 3582 * @sbi: In memory super block info 3583 * 3584 * If we have specified it via mount option, then 3585 * use the mount option value. If the value specified at mount time is 3586 * greater than the blocks per group use the super block value. 3587 * If the super block value is greater than blocks per group return 0. 3588 * Allocator needs it be less than blocks per group. 3589 * 3590 */ 3591 static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi) 3592 { 3593 unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride); 3594 unsigned long stripe_width = 3595 le32_to_cpu(sbi->s_es->s_raid_stripe_width); 3596 int ret; 3597 3598 if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group) 3599 ret = sbi->s_stripe; 3600 else if (stripe_width && stripe_width <= sbi->s_blocks_per_group) 3601 ret = stripe_width; 3602 else if (stride && stride <= sbi->s_blocks_per_group) 3603 ret = stride; 3604 else 3605 ret = 0; 3606 3607 /* 3608 * If the stripe width is 1, this makes no sense and 3609 * we set it to 0 to turn off stripe handling code. 3610 */ 3611 if (ret <= 1) 3612 ret = 0; 3613 3614 return ret; 3615 } 3616 3617 /* 3618 * Check whether this filesystem can be mounted based on 3619 * the features present and the RDONLY/RDWR mount requested. 3620 * Returns 1 if this filesystem can be mounted as requested, 3621 * 0 if it cannot be. 3622 */ 3623 int ext4_feature_set_ok(struct super_block *sb, int readonly) 3624 { 3625 if (ext4_has_unknown_ext4_incompat_features(sb)) { 3626 ext4_msg(sb, KERN_ERR, 3627 "Couldn't mount because of " 3628 "unsupported optional features (%x)", 3629 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) & 3630 ~EXT4_FEATURE_INCOMPAT_SUPP)); 3631 return 0; 3632 } 3633 3634 if (!IS_ENABLED(CONFIG_UNICODE) && ext4_has_feature_casefold(sb)) { 3635 ext4_msg(sb, KERN_ERR, 3636 "Filesystem with casefold feature cannot be " 3637 "mounted without CONFIG_UNICODE"); 3638 return 0; 3639 } 3640 3641 if (readonly) 3642 return 1; 3643 3644 if (ext4_has_feature_readonly(sb)) { 3645 ext4_msg(sb, KERN_INFO, "filesystem is read-only"); 3646 sb->s_flags |= SB_RDONLY; 3647 return 1; 3648 } 3649 3650 /* Check that feature set is OK for a read-write mount */ 3651 if (ext4_has_unknown_ext4_ro_compat_features(sb)) { 3652 ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of " 3653 "unsupported optional features (%x)", 3654 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) & 3655 ~EXT4_FEATURE_RO_COMPAT_SUPP)); 3656 return 0; 3657 } 3658 if (ext4_has_feature_bigalloc(sb) && !ext4_has_feature_extents(sb)) { 3659 ext4_msg(sb, KERN_ERR, 3660 "Can't support bigalloc feature without " 3661 "extents feature\n"); 3662 return 0; 3663 } 3664 if (ext4_has_feature_bigalloc(sb) && 3665 le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) { 3666 ext4_msg(sb, KERN_WARNING, 3667 "bad geometry: bigalloc file system with non-zero " 3668 "first_data_block\n"); 3669 return 0; 3670 } 3671 3672 #if !IS_ENABLED(CONFIG_QUOTA) || !IS_ENABLED(CONFIG_QFMT_V2) 3673 if (!readonly && (ext4_has_feature_quota(sb) || 3674 ext4_has_feature_project(sb))) { 3675 ext4_msg(sb, KERN_ERR, 3676 "The kernel was not built with CONFIG_QUOTA and CONFIG_QFMT_V2"); 3677 return 0; 3678 } 3679 #endif /* CONFIG_QUOTA */ 3680 return 1; 3681 } 3682 3683 /* 3684 * This function is called once a day by default if we have errors logged 3685 * on the file system. 3686 * Use the err_report_sec sysfs attribute to disable or adjust its call 3687 * freequency. 3688 */ 3689 void print_daily_error_info(struct timer_list *t) 3690 { 3691 struct ext4_sb_info *sbi = timer_container_of(sbi, t, s_err_report); 3692 struct super_block *sb = sbi->s_sb; 3693 struct ext4_super_block *es = sbi->s_es; 3694 3695 if (es->s_error_count) 3696 /* fsck newer than v1.41.13 is needed to clean this condition. */ 3697 ext4_msg(sb, KERN_NOTICE, "error count since last fsck: %u", 3698 le32_to_cpu(es->s_error_count)); 3699 if (es->s_first_error_time) { 3700 printk(KERN_NOTICE "EXT4-fs (%s): initial error at time %llu: %.*s:%d", 3701 sb->s_id, 3702 ext4_get_tstamp(es, s_first_error_time), 3703 (int) sizeof(es->s_first_error_func), 3704 es->s_first_error_func, 3705 le32_to_cpu(es->s_first_error_line)); 3706 if (es->s_first_error_ino) 3707 printk(KERN_CONT ": inode %u", 3708 le32_to_cpu(es->s_first_error_ino)); 3709 if (es->s_first_error_block) 3710 printk(KERN_CONT ": block %llu", (unsigned long long) 3711 le64_to_cpu(es->s_first_error_block)); 3712 printk(KERN_CONT "\n"); 3713 } 3714 if (es->s_last_error_time) { 3715 printk(KERN_NOTICE "EXT4-fs (%s): last error at time %llu: %.*s:%d", 3716 sb->s_id, 3717 ext4_get_tstamp(es, s_last_error_time), 3718 (int) sizeof(es->s_last_error_func), 3719 es->s_last_error_func, 3720 le32_to_cpu(es->s_last_error_line)); 3721 if (es->s_last_error_ino) 3722 printk(KERN_CONT ": inode %u", 3723 le32_to_cpu(es->s_last_error_ino)); 3724 if (es->s_last_error_block) 3725 printk(KERN_CONT ": block %llu", (unsigned long long) 3726 le64_to_cpu(es->s_last_error_block)); 3727 printk(KERN_CONT "\n"); 3728 } 3729 3730 if (sbi->s_err_report_sec) 3731 mod_timer(&sbi->s_err_report, jiffies + secs_to_jiffies(sbi->s_err_report_sec)); 3732 } 3733 3734 /* Find next suitable group and run ext4_init_inode_table */ 3735 static int ext4_run_li_request(struct ext4_li_request *elr) 3736 { 3737 struct ext4_group_desc *gdp = NULL; 3738 struct super_block *sb = elr->lr_super; 3739 ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count; 3740 ext4_group_t group = elr->lr_next_group; 3741 unsigned int prefetch_ios = 0; 3742 int ret = 0; 3743 int nr = EXT4_SB(sb)->s_mb_prefetch; 3744 u64 start_time; 3745 3746 if (elr->lr_mode == EXT4_LI_MODE_PREFETCH_BBITMAP) { 3747 elr->lr_next_group = ext4_mb_prefetch(sb, group, nr, &prefetch_ios); 3748 ext4_mb_prefetch_fini(sb, elr->lr_next_group, nr); 3749 trace_ext4_prefetch_bitmaps(sb, group, elr->lr_next_group, nr); 3750 if (group >= elr->lr_next_group) { 3751 ret = 1; 3752 if (elr->lr_first_not_zeroed != ngroups && 3753 !ext4_emergency_state(sb) && !sb_rdonly(sb) && 3754 test_opt(sb, INIT_INODE_TABLE)) { 3755 elr->lr_next_group = elr->lr_first_not_zeroed; 3756 elr->lr_mode = EXT4_LI_MODE_ITABLE; 3757 ret = 0; 3758 } 3759 } 3760 return ret; 3761 } 3762 3763 for (; group < ngroups; group++) { 3764 gdp = ext4_get_group_desc(sb, group, NULL); 3765 if (!gdp) { 3766 ret = 1; 3767 break; 3768 } 3769 3770 if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED))) 3771 break; 3772 } 3773 3774 if (group >= ngroups) 3775 ret = 1; 3776 3777 if (!ret) { 3778 start_time = ktime_get_ns(); 3779 ret = ext4_init_inode_table(sb, group, 3780 elr->lr_timeout ? 0 : 1); 3781 trace_ext4_lazy_itable_init(sb, group); 3782 if (elr->lr_timeout == 0) { 3783 elr->lr_timeout = nsecs_to_jiffies((ktime_get_ns() - start_time) * 3784 EXT4_SB(elr->lr_super)->s_li_wait_mult); 3785 } 3786 elr->lr_next_sched = jiffies + elr->lr_timeout; 3787 elr->lr_next_group = group + 1; 3788 } 3789 return ret; 3790 } 3791 3792 /* 3793 * Remove lr_request from the list_request and free the 3794 * request structure. Should be called with li_list_mtx held 3795 */ 3796 static void ext4_remove_li_request(struct ext4_li_request *elr) 3797 { 3798 if (!elr) 3799 return; 3800 3801 list_del(&elr->lr_request); 3802 EXT4_SB(elr->lr_super)->s_li_request = NULL; 3803 kfree(elr); 3804 } 3805 3806 static void ext4_unregister_li_request(struct super_block *sb) 3807 { 3808 mutex_lock(&ext4_li_mtx); 3809 if (!ext4_li_info) { 3810 mutex_unlock(&ext4_li_mtx); 3811 return; 3812 } 3813 3814 mutex_lock(&ext4_li_info->li_list_mtx); 3815 ext4_remove_li_request(EXT4_SB(sb)->s_li_request); 3816 mutex_unlock(&ext4_li_info->li_list_mtx); 3817 mutex_unlock(&ext4_li_mtx); 3818 } 3819 3820 static struct task_struct *ext4_lazyinit_task; 3821 3822 /* 3823 * This is the function where ext4lazyinit thread lives. It walks 3824 * through the request list searching for next scheduled filesystem. 3825 * When such a fs is found, run the lazy initialization request 3826 * (ext4_rn_li_request) and keep track of the time spend in this 3827 * function. Based on that time we compute next schedule time of 3828 * the request. When walking through the list is complete, compute 3829 * next waking time and put itself into sleep. 3830 */ 3831 static int ext4_lazyinit_thread(void *arg) 3832 { 3833 struct ext4_lazy_init *eli = arg; 3834 struct list_head *pos, *n; 3835 struct ext4_li_request *elr; 3836 unsigned long next_wakeup, cur; 3837 3838 BUG_ON(NULL == eli); 3839 set_freezable(); 3840 3841 cont_thread: 3842 while (true) { 3843 bool next_wakeup_initialized = false; 3844 3845 next_wakeup = 0; 3846 mutex_lock(&eli->li_list_mtx); 3847 if (list_empty(&eli->li_request_list)) { 3848 mutex_unlock(&eli->li_list_mtx); 3849 goto exit_thread; 3850 } 3851 list_for_each_safe(pos, n, &eli->li_request_list) { 3852 int err = 0; 3853 int progress = 0; 3854 elr = list_entry(pos, struct ext4_li_request, 3855 lr_request); 3856 3857 if (time_before(jiffies, elr->lr_next_sched)) { 3858 if (!next_wakeup_initialized || 3859 time_before(elr->lr_next_sched, next_wakeup)) { 3860 next_wakeup = elr->lr_next_sched; 3861 next_wakeup_initialized = true; 3862 } 3863 continue; 3864 } 3865 if (down_read_trylock(&elr->lr_super->s_umount)) { 3866 if (sb_start_write_trylock(elr->lr_super)) { 3867 progress = 1; 3868 /* 3869 * We hold sb->s_umount, sb can not 3870 * be removed from the list, it is 3871 * now safe to drop li_list_mtx 3872 */ 3873 mutex_unlock(&eli->li_list_mtx); 3874 err = ext4_run_li_request(elr); 3875 sb_end_write(elr->lr_super); 3876 mutex_lock(&eli->li_list_mtx); 3877 n = pos->next; 3878 } 3879 up_read((&elr->lr_super->s_umount)); 3880 } 3881 /* error, remove the lazy_init job */ 3882 if (err) { 3883 ext4_remove_li_request(elr); 3884 continue; 3885 } 3886 if (!progress) { 3887 elr->lr_next_sched = jiffies + 3888 get_random_u32_below(EXT4_DEF_LI_MAX_START_DELAY * HZ); 3889 } 3890 if (!next_wakeup_initialized || 3891 time_before(elr->lr_next_sched, next_wakeup)) { 3892 next_wakeup = elr->lr_next_sched; 3893 next_wakeup_initialized = true; 3894 } 3895 } 3896 mutex_unlock(&eli->li_list_mtx); 3897 3898 try_to_freeze(); 3899 3900 cur = jiffies; 3901 if (!next_wakeup_initialized || time_after_eq(cur, next_wakeup)) { 3902 cond_resched(); 3903 continue; 3904 } 3905 3906 schedule_timeout_interruptible(next_wakeup - cur); 3907 3908 if (kthread_should_stop()) { 3909 ext4_clear_request_list(); 3910 goto exit_thread; 3911 } 3912 } 3913 3914 exit_thread: 3915 /* 3916 * It looks like the request list is empty, but we need 3917 * to check it under the li_list_mtx lock, to prevent any 3918 * additions into it, and of course we should lock ext4_li_mtx 3919 * to atomically free the list and ext4_li_info, because at 3920 * this point another ext4 filesystem could be registering 3921 * new one. 3922 */ 3923 mutex_lock(&ext4_li_mtx); 3924 mutex_lock(&eli->li_list_mtx); 3925 if (!list_empty(&eli->li_request_list)) { 3926 mutex_unlock(&eli->li_list_mtx); 3927 mutex_unlock(&ext4_li_mtx); 3928 goto cont_thread; 3929 } 3930 mutex_unlock(&eli->li_list_mtx); 3931 kfree(ext4_li_info); 3932 ext4_li_info = NULL; 3933 mutex_unlock(&ext4_li_mtx); 3934 3935 return 0; 3936 } 3937 3938 static void ext4_clear_request_list(void) 3939 { 3940 struct list_head *pos, *n; 3941 struct ext4_li_request *elr; 3942 3943 mutex_lock(&ext4_li_info->li_list_mtx); 3944 list_for_each_safe(pos, n, &ext4_li_info->li_request_list) { 3945 elr = list_entry(pos, struct ext4_li_request, 3946 lr_request); 3947 ext4_remove_li_request(elr); 3948 } 3949 mutex_unlock(&ext4_li_info->li_list_mtx); 3950 } 3951 3952 static int ext4_run_lazyinit_thread(void) 3953 { 3954 ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread, 3955 ext4_li_info, "ext4lazyinit"); 3956 if (IS_ERR(ext4_lazyinit_task)) { 3957 int err = PTR_ERR(ext4_lazyinit_task); 3958 ext4_clear_request_list(); 3959 kfree(ext4_li_info); 3960 ext4_li_info = NULL; 3961 printk(KERN_CRIT "EXT4-fs: error %d creating inode table " 3962 "initialization thread\n", 3963 err); 3964 return err; 3965 } 3966 ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING; 3967 return 0; 3968 } 3969 3970 /* 3971 * Check whether it make sense to run itable init. thread or not. 3972 * If there is at least one uninitialized inode table, return 3973 * corresponding group number, else the loop goes through all 3974 * groups and return total number of groups. 3975 */ 3976 static ext4_group_t ext4_has_uninit_itable(struct super_block *sb) 3977 { 3978 ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count; 3979 struct ext4_group_desc *gdp = NULL; 3980 3981 if (!ext4_has_group_desc_csum(sb)) 3982 return ngroups; 3983 3984 for (group = 0; group < ngroups; group++) { 3985 gdp = ext4_get_group_desc(sb, group, NULL); 3986 if (!gdp) 3987 continue; 3988 3989 if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED))) 3990 break; 3991 } 3992 3993 return group; 3994 } 3995 3996 static int ext4_li_info_new(void) 3997 { 3998 struct ext4_lazy_init *eli = NULL; 3999 4000 eli = kzalloc_obj(*eli); 4001 if (!eli) 4002 return -ENOMEM; 4003 4004 INIT_LIST_HEAD(&eli->li_request_list); 4005 mutex_init(&eli->li_list_mtx); 4006 4007 eli->li_state |= EXT4_LAZYINIT_QUIT; 4008 4009 ext4_li_info = eli; 4010 4011 return 0; 4012 } 4013 4014 static struct ext4_li_request *ext4_li_request_new(struct super_block *sb, 4015 ext4_group_t start) 4016 { 4017 struct ext4_li_request *elr; 4018 4019 elr = kzalloc_obj(*elr); 4020 if (!elr) 4021 return NULL; 4022 4023 elr->lr_super = sb; 4024 elr->lr_first_not_zeroed = start; 4025 if (test_opt(sb, NO_PREFETCH_BLOCK_BITMAPS)) { 4026 elr->lr_mode = EXT4_LI_MODE_ITABLE; 4027 elr->lr_next_group = start; 4028 } else { 4029 elr->lr_mode = EXT4_LI_MODE_PREFETCH_BBITMAP; 4030 } 4031 4032 /* 4033 * Randomize first schedule time of the request to 4034 * spread the inode table initialization requests 4035 * better. 4036 */ 4037 elr->lr_next_sched = jiffies + get_random_u32_below(EXT4_DEF_LI_MAX_START_DELAY * HZ); 4038 return elr; 4039 } 4040 4041 int ext4_register_li_request(struct super_block *sb, 4042 ext4_group_t first_not_zeroed) 4043 { 4044 struct ext4_sb_info *sbi = EXT4_SB(sb); 4045 struct ext4_li_request *elr = NULL; 4046 ext4_group_t ngroups = sbi->s_groups_count; 4047 int ret = 0; 4048 4049 mutex_lock(&ext4_li_mtx); 4050 if (sbi->s_li_request != NULL) { 4051 /* 4052 * Reset timeout so it can be computed again, because 4053 * s_li_wait_mult might have changed. 4054 */ 4055 sbi->s_li_request->lr_timeout = 0; 4056 goto out; 4057 } 4058 4059 if (ext4_emergency_state(sb) || sb_rdonly(sb) || 4060 (test_opt(sb, NO_PREFETCH_BLOCK_BITMAPS) && 4061 (first_not_zeroed == ngroups || !test_opt(sb, INIT_INODE_TABLE)))) 4062 goto out; 4063 4064 elr = ext4_li_request_new(sb, first_not_zeroed); 4065 if (!elr) { 4066 ret = -ENOMEM; 4067 goto out; 4068 } 4069 4070 if (NULL == ext4_li_info) { 4071 ret = ext4_li_info_new(); 4072 if (ret) 4073 goto out; 4074 } 4075 4076 mutex_lock(&ext4_li_info->li_list_mtx); 4077 list_add(&elr->lr_request, &ext4_li_info->li_request_list); 4078 mutex_unlock(&ext4_li_info->li_list_mtx); 4079 4080 sbi->s_li_request = elr; 4081 /* 4082 * set elr to NULL here since it has been inserted to 4083 * the request_list and the removal and free of it is 4084 * handled by ext4_clear_request_list from now on. 4085 */ 4086 elr = NULL; 4087 4088 if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) { 4089 ret = ext4_run_lazyinit_thread(); 4090 if (ret) 4091 goto out; 4092 } 4093 out: 4094 mutex_unlock(&ext4_li_mtx); 4095 if (ret) 4096 kfree(elr); 4097 return ret; 4098 } 4099 4100 /* 4101 * We do not need to lock anything since this is called on 4102 * module unload. 4103 */ 4104 static void ext4_destroy_lazyinit_thread(void) 4105 { 4106 /* 4107 * If thread exited earlier 4108 * there's nothing to be done. 4109 */ 4110 if (!ext4_li_info || !ext4_lazyinit_task) 4111 return; 4112 4113 kthread_stop(ext4_lazyinit_task); 4114 } 4115 4116 static int set_journal_csum_feature_set(struct super_block *sb) 4117 { 4118 int ret = 1; 4119 int compat, incompat; 4120 struct ext4_sb_info *sbi = EXT4_SB(sb); 4121 4122 if (ext4_has_feature_metadata_csum(sb)) { 4123 /* journal checksum v3 */ 4124 compat = 0; 4125 incompat = JBD2_FEATURE_INCOMPAT_CSUM_V3; 4126 } else { 4127 /* journal checksum v1 */ 4128 compat = JBD2_FEATURE_COMPAT_CHECKSUM; 4129 incompat = 0; 4130 } 4131 4132 jbd2_journal_clear_features(sbi->s_journal, 4133 JBD2_FEATURE_COMPAT_CHECKSUM, 0, 4134 JBD2_FEATURE_INCOMPAT_CSUM_V3 | 4135 JBD2_FEATURE_INCOMPAT_CSUM_V2); 4136 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) { 4137 ret = jbd2_journal_set_features(sbi->s_journal, 4138 compat, 0, 4139 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT | 4140 incompat); 4141 } else if (test_opt(sb, JOURNAL_CHECKSUM)) { 4142 ret = jbd2_journal_set_features(sbi->s_journal, 4143 compat, 0, 4144 incompat); 4145 jbd2_journal_clear_features(sbi->s_journal, 0, 0, 4146 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT); 4147 } else { 4148 jbd2_journal_clear_features(sbi->s_journal, 0, 0, 4149 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT); 4150 } 4151 4152 return ret; 4153 } 4154 4155 /* 4156 * Note: calculating the overhead so we can be compatible with 4157 * historical BSD practice is quite difficult in the face of 4158 * clusters/bigalloc. This is because multiple metadata blocks from 4159 * different block group can end up in the same allocation cluster. 4160 * Calculating the exact overhead in the face of clustered allocation 4161 * requires either O(all block bitmaps) in memory or O(number of block 4162 * groups**2) in time. We will still calculate the superblock for 4163 * older file systems --- and if we come across with a bigalloc file 4164 * system with zero in s_overhead_clusters the estimate will be close to 4165 * correct especially for very large cluster sizes --- but for newer 4166 * file systems, it's better to calculate this figure once at mkfs 4167 * time, and store it in the superblock. If the superblock value is 4168 * present (even for non-bigalloc file systems), we will use it. 4169 */ 4170 static int count_overhead(struct super_block *sb, ext4_group_t grp, 4171 char *buf) 4172 { 4173 struct ext4_sb_info *sbi = EXT4_SB(sb); 4174 struct ext4_group_desc *gdp; 4175 ext4_fsblk_t first_block, last_block, b; 4176 ext4_group_t i, ngroups = ext4_get_groups_count(sb); 4177 int s, j, count = 0; 4178 int has_super = ext4_bg_has_super(sb, grp); 4179 4180 if (!ext4_has_feature_bigalloc(sb)) 4181 return (has_super + ext4_bg_num_gdb(sb, grp) + 4182 (has_super ? le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) : 0) + 4183 sbi->s_itb_per_group + 2); 4184 4185 first_block = le32_to_cpu(sbi->s_es->s_first_data_block) + 4186 (grp * EXT4_BLOCKS_PER_GROUP(sb)); 4187 last_block = first_block + EXT4_BLOCKS_PER_GROUP(sb) - 1; 4188 for (i = 0; i < ngroups; i++) { 4189 gdp = ext4_get_group_desc(sb, i, NULL); 4190 b = ext4_block_bitmap(sb, gdp); 4191 if (b >= first_block && b <= last_block) { 4192 ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf); 4193 count++; 4194 } 4195 b = ext4_inode_bitmap(sb, gdp); 4196 if (b >= first_block && b <= last_block) { 4197 ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf); 4198 count++; 4199 } 4200 b = ext4_inode_table(sb, gdp); 4201 if (b >= first_block && b + sbi->s_itb_per_group <= last_block) 4202 for (j = 0; j < sbi->s_itb_per_group; j++, b++) { 4203 int c = EXT4_B2C(sbi, b - first_block); 4204 ext4_set_bit(c, buf); 4205 count++; 4206 } 4207 if (i != grp) 4208 continue; 4209 s = 0; 4210 if (ext4_bg_has_super(sb, grp)) { 4211 ext4_set_bit(s++, buf); 4212 count++; 4213 } 4214 j = ext4_bg_num_gdb(sb, grp); 4215 if (s + j > EXT4_BLOCKS_PER_GROUP(sb)) { 4216 ext4_error(sb, "Invalid number of block group " 4217 "descriptor blocks: %d", j); 4218 j = EXT4_BLOCKS_PER_GROUP(sb) - s; 4219 } 4220 count += j; 4221 for (; j > 0; j--) 4222 ext4_set_bit(EXT4_B2C(sbi, s++), buf); 4223 } 4224 if (!count) 4225 return 0; 4226 return EXT4_CLUSTERS_PER_GROUP(sb) - 4227 ext4_count_free(buf, EXT4_CLUSTERS_PER_GROUP(sb) / 8); 4228 } 4229 4230 /* 4231 * Compute the overhead and stash it in sbi->s_overhead 4232 */ 4233 int ext4_calculate_overhead(struct super_block *sb) 4234 { 4235 struct ext4_sb_info *sbi = EXT4_SB(sb); 4236 struct ext4_super_block *es = sbi->s_es; 4237 struct inode *j_inode; 4238 unsigned int j_blocks, j_inum = le32_to_cpu(es->s_journal_inum); 4239 ext4_group_t i, ngroups = ext4_get_groups_count(sb); 4240 ext4_fsblk_t overhead = 0; 4241 char *buf = kvmalloc(sb->s_blocksize, GFP_NOFS | __GFP_ZERO); 4242 4243 if (!buf) 4244 return -ENOMEM; 4245 4246 /* 4247 * Compute the overhead (FS structures). This is constant 4248 * for a given filesystem unless the number of block groups 4249 * changes so we cache the previous value until it does. 4250 */ 4251 4252 /* 4253 * All of the blocks before first_data_block are overhead 4254 */ 4255 overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block)); 4256 4257 /* 4258 * Add the overhead found in each block group 4259 */ 4260 for (i = 0; i < ngroups; i++) { 4261 int blks; 4262 4263 blks = count_overhead(sb, i, buf); 4264 overhead += blks; 4265 if (blks) 4266 memset(buf, 0, sb->s_blocksize); 4267 cond_resched(); 4268 } 4269 4270 /* 4271 * Add the internal journal blocks whether the journal has been 4272 * loaded or not 4273 */ 4274 if (sbi->s_journal && !sbi->s_journal_bdev_file) 4275 overhead += EXT4_NUM_B2C(sbi, sbi->s_journal->j_total_len); 4276 else if (ext4_has_feature_journal(sb) && !sbi->s_journal && j_inum) { 4277 /* j_inum for internal journal is non-zero */ 4278 j_inode = ext4_get_journal_inode(sb, j_inum); 4279 if (!IS_ERR(j_inode)) { 4280 j_blocks = j_inode->i_size >> sb->s_blocksize_bits; 4281 overhead += EXT4_NUM_B2C(sbi, j_blocks); 4282 iput(j_inode); 4283 } else { 4284 ext4_msg(sb, KERN_ERR, "can't get journal size"); 4285 } 4286 } 4287 sbi->s_overhead = overhead; 4288 smp_wmb(); 4289 kvfree(buf); 4290 return 0; 4291 } 4292 4293 static void ext4_set_resv_clusters(struct super_block *sb) 4294 { 4295 ext4_fsblk_t resv_clusters; 4296 struct ext4_sb_info *sbi = EXT4_SB(sb); 4297 4298 /* 4299 * There's no need to reserve anything when we aren't using extents. 4300 * The space estimates are exact, there are no unwritten extents, 4301 * hole punching doesn't need new metadata... This is needed especially 4302 * to keep ext2/3 backward compatibility. 4303 */ 4304 if (!ext4_has_feature_extents(sb)) 4305 return; 4306 /* 4307 * By default we reserve 2% or 4096 clusters, whichever is smaller. 4308 * This should cover the situations where we can not afford to run 4309 * out of space like for example punch hole, or converting 4310 * unwritten extents in delalloc path. In most cases such 4311 * allocation would require 1, or 2 blocks, higher numbers are 4312 * very rare. 4313 */ 4314 resv_clusters = (ext4_blocks_count(sbi->s_es) >> 4315 sbi->s_cluster_bits); 4316 4317 do_div(resv_clusters, 50); 4318 resv_clusters = min_t(ext4_fsblk_t, resv_clusters, 4096); 4319 4320 atomic64_set(&sbi->s_resv_clusters, resv_clusters); 4321 } 4322 4323 static const char *ext4_quota_mode(struct super_block *sb) 4324 { 4325 #ifdef CONFIG_QUOTA 4326 if (!ext4_quota_capable(sb)) 4327 return "none"; 4328 4329 if (EXT4_SB(sb)->s_journal && ext4_is_quota_journalled(sb)) 4330 return "journalled"; 4331 else 4332 return "writeback"; 4333 #else 4334 return "disabled"; 4335 #endif 4336 } 4337 4338 static void ext4_setup_csum_trigger(struct super_block *sb, 4339 enum ext4_journal_trigger_type type, 4340 void (*trigger)( 4341 struct jbd2_buffer_trigger_type *type, 4342 struct buffer_head *bh, 4343 void *mapped_data, 4344 size_t size)) 4345 { 4346 struct ext4_sb_info *sbi = EXT4_SB(sb); 4347 4348 sbi->s_journal_triggers[type].sb = sb; 4349 sbi->s_journal_triggers[type].tr_triggers.t_frozen = trigger; 4350 } 4351 4352 static void ext4_free_sbi(struct ext4_sb_info *sbi) 4353 { 4354 if (!sbi) 4355 return; 4356 4357 kfree(sbi->s_blockgroup_lock); 4358 fs_put_dax(sbi->s_daxdev, NULL); 4359 kfree(sbi); 4360 } 4361 4362 static struct ext4_sb_info *ext4_alloc_sbi(struct super_block *sb) 4363 { 4364 struct ext4_sb_info *sbi; 4365 4366 sbi = kzalloc_obj(*sbi); 4367 if (!sbi) 4368 return NULL; 4369 4370 sbi->s_daxdev = fs_dax_get_by_bdev(sb->s_bdev, &sbi->s_dax_part_off, 4371 NULL, NULL); 4372 4373 sbi->s_blockgroup_lock = 4374 kzalloc_obj(struct blockgroup_lock); 4375 4376 if (!sbi->s_blockgroup_lock) 4377 goto err_out; 4378 4379 sb->s_fs_info = sbi; 4380 sbi->s_sb = sb; 4381 return sbi; 4382 err_out: 4383 fs_put_dax(sbi->s_daxdev, NULL); 4384 kfree(sbi); 4385 return NULL; 4386 } 4387 4388 static void ext4_set_def_opts(struct super_block *sb, 4389 struct ext4_super_block *es) 4390 { 4391 unsigned long def_mount_opts; 4392 4393 /* Set defaults before we parse the mount options */ 4394 def_mount_opts = le32_to_cpu(es->s_default_mount_opts); 4395 set_opt(sb, INIT_INODE_TABLE); 4396 if (def_mount_opts & EXT4_DEFM_DEBUG) 4397 set_opt(sb, DEBUG); 4398 if (def_mount_opts & EXT4_DEFM_BSDGROUPS) 4399 set_opt(sb, GRPID); 4400 if (def_mount_opts & EXT4_DEFM_UID16) 4401 set_opt(sb, NO_UID32); 4402 /* xattr user namespace & acls are now defaulted on */ 4403 set_opt(sb, XATTR_USER); 4404 #ifdef CONFIG_EXT4_FS_POSIX_ACL 4405 set_opt(sb, POSIX_ACL); 4406 #endif 4407 if (ext4_has_feature_fast_commit(sb)) 4408 set_opt2(sb, JOURNAL_FAST_COMMIT); 4409 /* don't forget to enable journal_csum when metadata_csum is enabled. */ 4410 if (ext4_has_feature_metadata_csum(sb)) 4411 set_opt(sb, JOURNAL_CHECKSUM); 4412 4413 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA) 4414 set_opt(sb, JOURNAL_DATA); 4415 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED) 4416 set_opt(sb, ORDERED_DATA); 4417 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK) 4418 set_opt(sb, WRITEBACK_DATA); 4419 4420 if (le16_to_cpu(es->s_errors) == EXT4_ERRORS_PANIC) 4421 set_opt(sb, ERRORS_PANIC); 4422 else if (le16_to_cpu(es->s_errors) == EXT4_ERRORS_CONTINUE) 4423 set_opt(sb, ERRORS_CONT); 4424 else 4425 set_opt(sb, ERRORS_RO); 4426 /* block_validity enabled by default; disable with noblock_validity */ 4427 set_opt(sb, BLOCK_VALIDITY); 4428 if (def_mount_opts & EXT4_DEFM_DISCARD) 4429 set_opt(sb, DISCARD); 4430 4431 if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0) 4432 set_opt(sb, BARRIER); 4433 4434 /* 4435 * enable delayed allocation by default 4436 * Use -o nodelalloc to turn it off 4437 */ 4438 if (!IS_EXT3_SB(sb) && !IS_EXT2_SB(sb) && 4439 ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0)) 4440 set_opt(sb, DELALLOC); 4441 4442 set_opt(sb, DIOREAD_NOLOCK); 4443 } 4444 4445 static int ext4_handle_clustersize(struct super_block *sb) 4446 { 4447 struct ext4_sb_info *sbi = EXT4_SB(sb); 4448 struct ext4_super_block *es = sbi->s_es; 4449 int clustersize; 4450 4451 /* Handle clustersize */ 4452 clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size); 4453 if (ext4_has_feature_bigalloc(sb)) { 4454 if (clustersize < sb->s_blocksize) { 4455 ext4_msg(sb, KERN_ERR, 4456 "cluster size (%d) smaller than " 4457 "block size (%lu)", clustersize, sb->s_blocksize); 4458 return -EINVAL; 4459 } 4460 sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) - 4461 le32_to_cpu(es->s_log_block_size); 4462 } else { 4463 if (clustersize != sb->s_blocksize) { 4464 ext4_msg(sb, KERN_ERR, 4465 "fragment/cluster size (%d) != " 4466 "block size (%lu)", clustersize, sb->s_blocksize); 4467 return -EINVAL; 4468 } 4469 if (sbi->s_blocks_per_group > sb->s_blocksize * 8) { 4470 ext4_msg(sb, KERN_ERR, 4471 "#blocks per group too big: %lu", 4472 sbi->s_blocks_per_group); 4473 return -EINVAL; 4474 } 4475 sbi->s_cluster_bits = 0; 4476 } 4477 sbi->s_clusters_per_group = le32_to_cpu(es->s_clusters_per_group); 4478 if (sbi->s_clusters_per_group > sb->s_blocksize * 8) { 4479 ext4_msg(sb, KERN_ERR, "#clusters per group too big: %lu", 4480 sbi->s_clusters_per_group); 4481 return -EINVAL; 4482 } 4483 if (sbi->s_blocks_per_group != 4484 (sbi->s_clusters_per_group * (clustersize / sb->s_blocksize))) { 4485 ext4_msg(sb, KERN_ERR, 4486 "blocks per group (%lu) and clusters per group (%lu) inconsistent", 4487 sbi->s_blocks_per_group, sbi->s_clusters_per_group); 4488 return -EINVAL; 4489 } 4490 sbi->s_cluster_ratio = clustersize / sb->s_blocksize; 4491 4492 /* Do we have standard group size of clustersize * 8 blocks ? */ 4493 if (sbi->s_blocks_per_group == clustersize << 3) 4494 set_opt2(sb, STD_GROUP_SIZE); 4495 4496 return 0; 4497 } 4498 4499 /* 4500 * ext4_atomic_write_init: Initializes filesystem min & max atomic write units. 4501 * With non-bigalloc filesystem awu will be based upon filesystem blocksize 4502 * & bdev awu units. 4503 * With bigalloc it will be based upon bigalloc cluster size & bdev awu units. 4504 * @sb: super block 4505 */ 4506 static void ext4_atomic_write_init(struct super_block *sb) 4507 { 4508 struct ext4_sb_info *sbi = EXT4_SB(sb); 4509 struct block_device *bdev = sb->s_bdev; 4510 unsigned int clustersize = EXT4_CLUSTER_SIZE(sb); 4511 4512 if (!bdev_can_atomic_write(bdev)) 4513 return; 4514 4515 if (!ext4_has_feature_extents(sb)) 4516 return; 4517 4518 sbi->s_awu_min = max(sb->s_blocksize, 4519 bdev_atomic_write_unit_min_bytes(bdev)); 4520 sbi->s_awu_max = min(clustersize, 4521 bdev_atomic_write_unit_max_bytes(bdev)); 4522 if (sbi->s_awu_min && sbi->s_awu_max && 4523 sbi->s_awu_min <= sbi->s_awu_max) { 4524 ext4_msg(sb, KERN_NOTICE, "Supports (experimental) DIO atomic writes awu_min: %u, awu_max: %u", 4525 sbi->s_awu_min, sbi->s_awu_max); 4526 } else { 4527 sbi->s_awu_min = 0; 4528 sbi->s_awu_max = 0; 4529 } 4530 } 4531 4532 static void ext4_fast_commit_init(struct super_block *sb) 4533 { 4534 struct ext4_sb_info *sbi = EXT4_SB(sb); 4535 4536 /* Initialize fast commit stuff */ 4537 atomic_set(&sbi->s_fc_subtid, 0); 4538 INIT_LIST_HEAD(&sbi->s_fc_q[FC_Q_MAIN]); 4539 INIT_LIST_HEAD(&sbi->s_fc_q[FC_Q_STAGING]); 4540 INIT_LIST_HEAD(&sbi->s_fc_dentry_q[FC_Q_MAIN]); 4541 INIT_LIST_HEAD(&sbi->s_fc_dentry_q[FC_Q_STAGING]); 4542 sbi->s_fc_bytes = 0; 4543 ext4_clear_mount_flag(sb, EXT4_MF_FC_INELIGIBLE); 4544 sbi->s_fc_ineligible_tid = 0; 4545 mutex_init(&sbi->s_fc_lock); 4546 memset(&sbi->s_fc_stats, 0, sizeof(sbi->s_fc_stats)); 4547 memset(&sbi->s_fc_snap_stats, 0, sizeof(sbi->s_fc_snap_stats)); 4548 sbi->s_fc_replay_state.fc_regions = NULL; 4549 sbi->s_fc_replay_state.fc_regions_size = 0; 4550 sbi->s_fc_replay_state.fc_regions_used = 0; 4551 sbi->s_fc_replay_state.fc_regions_valid = 0; 4552 sbi->s_fc_replay_state.fc_modified_inodes = NULL; 4553 sbi->s_fc_replay_state.fc_modified_inodes_size = 0; 4554 sbi->s_fc_replay_state.fc_modified_inodes_used = 0; 4555 } 4556 4557 static int ext4_inode_info_init(struct super_block *sb, 4558 struct ext4_super_block *es) 4559 { 4560 struct ext4_sb_info *sbi = EXT4_SB(sb); 4561 4562 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) { 4563 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE; 4564 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO; 4565 } else { 4566 sbi->s_inode_size = le16_to_cpu(es->s_inode_size); 4567 sbi->s_first_ino = le32_to_cpu(es->s_first_ino); 4568 if (sbi->s_first_ino < EXT4_GOOD_OLD_FIRST_INO) { 4569 ext4_msg(sb, KERN_ERR, "invalid first ino: %u", 4570 sbi->s_first_ino); 4571 return -EINVAL; 4572 } 4573 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) || 4574 (!is_power_of_2(sbi->s_inode_size)) || 4575 (sbi->s_inode_size > sb->s_blocksize)) { 4576 ext4_msg(sb, KERN_ERR, 4577 "unsupported inode size: %d", 4578 sbi->s_inode_size); 4579 ext4_msg(sb, KERN_ERR, "blocksize: %lu", sb->s_blocksize); 4580 return -EINVAL; 4581 } 4582 /* 4583 * i_atime_extra is the last extra field available for 4584 * [acm]times in struct ext4_inode. Checking for that 4585 * field should suffice to ensure we have extra space 4586 * for all three. 4587 */ 4588 if (sbi->s_inode_size >= offsetof(struct ext4_inode, i_atime_extra) + 4589 sizeof(((struct ext4_inode *)0)->i_atime_extra)) { 4590 sb->s_time_gran = 1; 4591 sb->s_time_max = EXT4_EXTRA_TIMESTAMP_MAX; 4592 } else { 4593 sb->s_time_gran = NSEC_PER_SEC; 4594 sb->s_time_max = EXT4_NON_EXTRA_TIMESTAMP_MAX; 4595 } 4596 sb->s_time_min = EXT4_TIMESTAMP_MIN; 4597 } 4598 4599 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) { 4600 sbi->s_want_extra_isize = sizeof(struct ext4_inode) - 4601 EXT4_GOOD_OLD_INODE_SIZE; 4602 if (ext4_has_feature_extra_isize(sb)) { 4603 unsigned v, max = (sbi->s_inode_size - 4604 EXT4_GOOD_OLD_INODE_SIZE); 4605 4606 v = le16_to_cpu(es->s_want_extra_isize); 4607 if (v > max) { 4608 ext4_msg(sb, KERN_ERR, 4609 "bad s_want_extra_isize: %d", v); 4610 return -EINVAL; 4611 } 4612 if (sbi->s_want_extra_isize < v) 4613 sbi->s_want_extra_isize = v; 4614 4615 v = le16_to_cpu(es->s_min_extra_isize); 4616 if (v > max) { 4617 ext4_msg(sb, KERN_ERR, 4618 "bad s_min_extra_isize: %d", v); 4619 return -EINVAL; 4620 } 4621 if (sbi->s_want_extra_isize < v) 4622 sbi->s_want_extra_isize = v; 4623 } 4624 } 4625 4626 return 0; 4627 } 4628 4629 #if IS_ENABLED(CONFIG_UNICODE) 4630 static int ext4_encoding_init(struct super_block *sb, struct ext4_super_block *es) 4631 { 4632 const struct ext4_sb_encodings *encoding_info; 4633 struct unicode_map *encoding; 4634 __u16 encoding_flags = le16_to_cpu(es->s_encoding_flags); 4635 4636 if (!ext4_has_feature_casefold(sb) || sb->s_encoding) 4637 return 0; 4638 4639 encoding_info = ext4_sb_read_encoding(es); 4640 if (!encoding_info) { 4641 ext4_msg(sb, KERN_ERR, 4642 "Encoding requested by superblock is unknown"); 4643 return -EINVAL; 4644 } 4645 4646 encoding = utf8_load(encoding_info->version); 4647 if (IS_ERR(encoding)) { 4648 ext4_msg(sb, KERN_ERR, 4649 "can't mount with superblock charset: %s-%u.%u.%u " 4650 "not supported by the kernel. flags: 0x%x.", 4651 encoding_info->name, 4652 unicode_major(encoding_info->version), 4653 unicode_minor(encoding_info->version), 4654 unicode_rev(encoding_info->version), 4655 encoding_flags); 4656 return -EINVAL; 4657 } 4658 ext4_msg(sb, KERN_INFO,"Using encoding defined by superblock: " 4659 "%s-%u.%u.%u with flags 0x%hx", encoding_info->name, 4660 unicode_major(encoding_info->version), 4661 unicode_minor(encoding_info->version), 4662 unicode_rev(encoding_info->version), 4663 encoding_flags); 4664 4665 sb->s_encoding = encoding; 4666 sb->s_encoding_flags = encoding_flags; 4667 4668 return 0; 4669 } 4670 #else 4671 static inline int ext4_encoding_init(struct super_block *sb, struct ext4_super_block *es) 4672 { 4673 return 0; 4674 } 4675 #endif 4676 4677 static int ext4_init_metadata_csum(struct super_block *sb, struct ext4_super_block *es) 4678 { 4679 struct ext4_sb_info *sbi = EXT4_SB(sb); 4680 4681 /* Warn if metadata_csum and gdt_csum are both set. */ 4682 if (ext4_has_feature_metadata_csum(sb) && 4683 ext4_has_feature_gdt_csum(sb)) 4684 ext4_warning(sb, "metadata_csum and uninit_bg are " 4685 "redundant flags; please run fsck."); 4686 4687 /* Check for a known checksum algorithm */ 4688 if (!ext4_verify_csum_type(sb, es)) { 4689 ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with " 4690 "unknown checksum algorithm."); 4691 return -EINVAL; 4692 } 4693 ext4_setup_csum_trigger(sb, EXT4_JTR_ORPHAN_FILE, 4694 ext4_orphan_file_block_trigger); 4695 4696 /* Check superblock checksum */ 4697 if (!ext4_superblock_csum_verify(sb, es)) { 4698 ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with " 4699 "invalid superblock checksum. Run e2fsck?"); 4700 return -EFSBADCRC; 4701 } 4702 4703 /* Precompute checksum seed for all metadata */ 4704 if (ext4_has_feature_csum_seed(sb)) 4705 sbi->s_csum_seed = le32_to_cpu(es->s_checksum_seed); 4706 else if (ext4_has_feature_metadata_csum(sb) || 4707 ext4_has_feature_ea_inode(sb)) 4708 sbi->s_csum_seed = ext4_chksum(~0, es->s_uuid, 4709 sizeof(es->s_uuid)); 4710 return 0; 4711 } 4712 4713 static int ext4_check_feature_compatibility(struct super_block *sb, 4714 struct ext4_super_block *es, 4715 int silent) 4716 { 4717 struct ext4_sb_info *sbi = EXT4_SB(sb); 4718 4719 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV && 4720 (ext4_has_compat_features(sb) || 4721 ext4_has_ro_compat_features(sb) || 4722 ext4_has_incompat_features(sb))) 4723 ext4_msg(sb, KERN_WARNING, 4724 "feature flags set on rev 0 fs, " 4725 "running e2fsck is recommended"); 4726 4727 if (es->s_creator_os == cpu_to_le32(EXT4_OS_HURD)) { 4728 set_opt2(sb, HURD_COMPAT); 4729 if (ext4_has_feature_64bit(sb)) { 4730 ext4_msg(sb, KERN_ERR, 4731 "The Hurd can't support 64-bit file systems"); 4732 return -EINVAL; 4733 } 4734 4735 /* 4736 * ea_inode feature uses l_i_version field which is not 4737 * available in HURD_COMPAT mode. 4738 */ 4739 if (ext4_has_feature_ea_inode(sb)) { 4740 ext4_msg(sb, KERN_ERR, 4741 "ea_inode feature is not supported for Hurd"); 4742 return -EINVAL; 4743 } 4744 } 4745 4746 if (IS_EXT2_SB(sb)) { 4747 if (ext2_feature_set_ok(sb)) 4748 ext4_msg(sb, KERN_INFO, "mounting ext2 file system " 4749 "using the ext4 subsystem"); 4750 else { 4751 /* 4752 * If we're probing be silent, if this looks like 4753 * it's actually an ext[34] filesystem. 4754 */ 4755 if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb))) 4756 return -EINVAL; 4757 ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due " 4758 "to feature incompatibilities"); 4759 return -EINVAL; 4760 } 4761 } 4762 4763 if (IS_EXT3_SB(sb)) { 4764 if (ext3_feature_set_ok(sb)) 4765 ext4_msg(sb, KERN_INFO, "mounting ext3 file system " 4766 "using the ext4 subsystem"); 4767 else { 4768 /* 4769 * If we're probing be silent, if this looks like 4770 * it's actually an ext4 filesystem. 4771 */ 4772 if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb))) 4773 return -EINVAL; 4774 ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due " 4775 "to feature incompatibilities"); 4776 return -EINVAL; 4777 } 4778 } 4779 4780 /* 4781 * Check feature flags regardless of the revision level, since we 4782 * previously didn't change the revision level when setting the flags, 4783 * so there is a chance incompat flags are set on a rev 0 filesystem. 4784 */ 4785 if (!ext4_feature_set_ok(sb, (sb_rdonly(sb)))) 4786 return -EINVAL; 4787 4788 if (sbi->s_daxdev) { 4789 if (sb->s_blocksize == PAGE_SIZE) 4790 set_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags); 4791 else 4792 ext4_msg(sb, KERN_ERR, "unsupported blocksize for DAX\n"); 4793 } 4794 4795 if (sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) { 4796 if (ext4_has_feature_inline_data(sb)) { 4797 ext4_msg(sb, KERN_ERR, "Cannot use DAX on a filesystem" 4798 " that may contain inline data"); 4799 return -EINVAL; 4800 } 4801 if (!test_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags)) { 4802 ext4_msg(sb, KERN_ERR, 4803 "DAX unsupported by block device."); 4804 return -EINVAL; 4805 } 4806 } 4807 4808 if (ext4_has_feature_encrypt(sb) && es->s_encryption_level) { 4809 ext4_msg(sb, KERN_ERR, "Unsupported encryption level %d", 4810 es->s_encryption_level); 4811 return -EINVAL; 4812 } 4813 4814 return 0; 4815 } 4816 4817 static int ext4_check_geometry(struct super_block *sb, 4818 struct ext4_super_block *es) 4819 { 4820 struct ext4_sb_info *sbi = EXT4_SB(sb); 4821 __u64 blocks_count; 4822 int err; 4823 4824 if (le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) > (sb->s_blocksize / 4)) { 4825 ext4_msg(sb, KERN_ERR, 4826 "Number of reserved GDT blocks insanely large: %d", 4827 le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks)); 4828 return -EINVAL; 4829 } 4830 /* 4831 * Test whether we have more sectors than will fit in sector_t, 4832 * and whether the max offset is addressable by the page cache. 4833 */ 4834 err = generic_check_addressable(sb->s_blocksize_bits, 4835 ext4_blocks_count(es)); 4836 if (err) { 4837 ext4_msg(sb, KERN_ERR, "filesystem" 4838 " too large to mount safely on this system"); 4839 return err; 4840 } 4841 4842 /* check blocks count against device size */ 4843 blocks_count = sb_bdev_nr_blocks(sb); 4844 if (blocks_count && ext4_blocks_count(es) > blocks_count) { 4845 ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu " 4846 "exceeds size of device (%llu blocks)", 4847 ext4_blocks_count(es), blocks_count); 4848 return -EINVAL; 4849 } 4850 4851 /* 4852 * It makes no sense for the first data block to be beyond the end 4853 * of the filesystem. 4854 */ 4855 if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) { 4856 ext4_msg(sb, KERN_WARNING, "bad geometry: first data " 4857 "block %u is beyond end of filesystem (%llu)", 4858 le32_to_cpu(es->s_first_data_block), 4859 ext4_blocks_count(es)); 4860 return -EINVAL; 4861 } 4862 if ((es->s_first_data_block == 0) && (es->s_log_block_size == 0) && 4863 (sbi->s_cluster_ratio == 1)) { 4864 ext4_msg(sb, KERN_WARNING, "bad geometry: first data " 4865 "block is 0 with a 1k block and cluster size"); 4866 return -EINVAL; 4867 } 4868 4869 blocks_count = (ext4_blocks_count(es) - 4870 le32_to_cpu(es->s_first_data_block) + 4871 EXT4_BLOCKS_PER_GROUP(sb) - 1); 4872 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb)); 4873 if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) { 4874 ext4_msg(sb, KERN_WARNING, "groups count too large: %llu " 4875 "(block count %llu, first data block %u, " 4876 "blocks per group %lu)", blocks_count, 4877 ext4_blocks_count(es), 4878 le32_to_cpu(es->s_first_data_block), 4879 EXT4_BLOCKS_PER_GROUP(sb)); 4880 return -EINVAL; 4881 } 4882 sbi->s_groups_count = blocks_count; 4883 sbi->s_blockfile_groups = min(sbi->s_groups_count, 4884 (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb))); 4885 if (((u64)sbi->s_groups_count * sbi->s_inodes_per_group) != 4886 le32_to_cpu(es->s_inodes_count)) { 4887 ext4_msg(sb, KERN_ERR, "inodes count not valid: %u vs %llu", 4888 le32_to_cpu(es->s_inodes_count), 4889 ((u64)sbi->s_groups_count * sbi->s_inodes_per_group)); 4890 return -EINVAL; 4891 } 4892 4893 return 0; 4894 } 4895 4896 static int ext4_group_desc_init(struct super_block *sb, 4897 struct ext4_super_block *es, 4898 ext4_fsblk_t logical_sb_block, 4899 ext4_group_t *first_not_zeroed) 4900 { 4901 struct ext4_sb_info *sbi = EXT4_SB(sb); 4902 unsigned int db_count; 4903 ext4_fsblk_t block; 4904 int i; 4905 4906 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) / 4907 EXT4_DESC_PER_BLOCK(sb); 4908 if (ext4_has_feature_meta_bg(sb)) { 4909 if (le32_to_cpu(es->s_first_meta_bg) > db_count) { 4910 ext4_msg(sb, KERN_WARNING, 4911 "first meta block group too large: %u " 4912 "(group descriptor block count %u)", 4913 le32_to_cpu(es->s_first_meta_bg), db_count); 4914 return -EINVAL; 4915 } 4916 } 4917 rcu_assign_pointer(sbi->s_group_desc, 4918 kvmalloc_objs(struct buffer_head *, db_count)); 4919 if (sbi->s_group_desc == NULL) { 4920 ext4_msg(sb, KERN_ERR, "not enough memory"); 4921 return -ENOMEM; 4922 } 4923 4924 bgl_lock_init(sbi->s_blockgroup_lock); 4925 4926 /* Pre-read the descriptors into the buffer cache */ 4927 for (i = 0; i < db_count; i++) { 4928 block = descriptor_loc(sb, logical_sb_block, i); 4929 ext4_sb_breadahead_unmovable(sb, block); 4930 } 4931 4932 for (i = 0; i < db_count; i++) { 4933 struct buffer_head *bh; 4934 4935 block = descriptor_loc(sb, logical_sb_block, i); 4936 bh = ext4_sb_bread_unmovable(sb, block); 4937 if (IS_ERR(bh)) { 4938 ext4_msg(sb, KERN_ERR, 4939 "can't read group descriptor %d", i); 4940 sbi->s_gdb_count = i; 4941 return PTR_ERR(bh); 4942 } 4943 rcu_read_lock(); 4944 rcu_dereference(sbi->s_group_desc)[i] = bh; 4945 rcu_read_unlock(); 4946 } 4947 sbi->s_gdb_count = db_count; 4948 if (!ext4_check_descriptors(sb, logical_sb_block, first_not_zeroed)) { 4949 ext4_msg(sb, KERN_ERR, "group descriptors corrupted!"); 4950 return -EFSCORRUPTED; 4951 } 4952 4953 return 0; 4954 } 4955 4956 static int ext4_load_and_init_journal(struct super_block *sb, 4957 struct ext4_super_block *es, 4958 struct ext4_fs_context *ctx) 4959 { 4960 struct ext4_sb_info *sbi = EXT4_SB(sb); 4961 int err; 4962 4963 err = ext4_load_journal(sb, es, ctx->journal_devnum); 4964 if (err) 4965 return err; 4966 4967 if (ext4_has_feature_64bit(sb) && 4968 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0, 4969 JBD2_FEATURE_INCOMPAT_64BIT)) { 4970 ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature"); 4971 goto out; 4972 } 4973 4974 if (!set_journal_csum_feature_set(sb)) { 4975 ext4_msg(sb, KERN_ERR, "Failed to set journal checksum " 4976 "feature set"); 4977 goto out; 4978 } 4979 4980 if (test_opt2(sb, JOURNAL_FAST_COMMIT) && 4981 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0, 4982 JBD2_FEATURE_INCOMPAT_FAST_COMMIT)) { 4983 ext4_msg(sb, KERN_ERR, 4984 "Failed to set fast commit journal feature"); 4985 goto out; 4986 } 4987 4988 /* We have now updated the journal if required, so we can 4989 * validate the data journaling mode. */ 4990 switch (test_opt(sb, DATA_FLAGS)) { 4991 case 0: 4992 /* No mode set, assume a default based on the journal 4993 * capabilities: ORDERED_DATA if the journal can 4994 * cope, else JOURNAL_DATA 4995 */ 4996 if (jbd2_journal_check_available_features 4997 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) { 4998 set_opt(sb, ORDERED_DATA); 4999 sbi->s_def_mount_opt |= EXT4_MOUNT_ORDERED_DATA; 5000 } else { 5001 set_opt(sb, JOURNAL_DATA); 5002 sbi->s_def_mount_opt |= EXT4_MOUNT_JOURNAL_DATA; 5003 } 5004 break; 5005 5006 case EXT4_MOUNT_ORDERED_DATA: 5007 case EXT4_MOUNT_WRITEBACK_DATA: 5008 if (!jbd2_journal_check_available_features 5009 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) { 5010 ext4_msg(sb, KERN_ERR, "Journal does not support " 5011 "requested data journaling mode"); 5012 goto out; 5013 } 5014 break; 5015 default: 5016 break; 5017 } 5018 5019 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA && 5020 test_opt(sb, JOURNAL_ASYNC_COMMIT)) { 5021 ext4_msg(sb, KERN_ERR, "can't mount with " 5022 "journal_async_commit in data=ordered mode"); 5023 goto out; 5024 } 5025 5026 set_task_ioprio(sbi->s_journal->j_task, ctx->journal_ioprio); 5027 5028 sbi->s_journal->j_submit_inode_data_buffers = 5029 ext4_journal_submit_inode_data_buffers; 5030 sbi->s_journal->j_finish_inode_data_buffers = 5031 ext4_journal_finish_inode_data_buffers; 5032 5033 return 0; 5034 5035 out: 5036 ext4_journal_destroy(sbi, sbi->s_journal); 5037 return -EINVAL; 5038 } 5039 5040 static int ext4_check_journal_data_mode(struct super_block *sb) 5041 { 5042 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) { 5043 printk_once(KERN_WARNING "EXT4-fs: Warning: mounting with " 5044 "data=journal disables delayed allocation, " 5045 "dioread_nolock, O_DIRECT and fast_commit support!\n"); 5046 /* can't mount with both data=journal and dioread_nolock. */ 5047 clear_opt(sb, DIOREAD_NOLOCK); 5048 clear_opt2(sb, JOURNAL_FAST_COMMIT); 5049 if (test_opt2(sb, EXPLICIT_DELALLOC)) { 5050 ext4_msg(sb, KERN_ERR, "can't mount with " 5051 "both data=journal and delalloc"); 5052 return -EINVAL; 5053 } 5054 if (test_opt(sb, DAX_ALWAYS)) { 5055 ext4_msg(sb, KERN_ERR, "can't mount with " 5056 "both data=journal and dax"); 5057 return -EINVAL; 5058 } 5059 if (ext4_has_feature_encrypt(sb)) { 5060 ext4_msg(sb, KERN_WARNING, 5061 "encrypted files will use data=ordered " 5062 "instead of data journaling mode"); 5063 } 5064 if (test_opt(sb, DELALLOC)) 5065 clear_opt(sb, DELALLOC); 5066 } else { 5067 sb->s_iflags |= SB_I_CGROUPWB; 5068 } 5069 5070 return 0; 5071 } 5072 5073 static const char *ext4_has_journal_option(struct super_block *sb) 5074 { 5075 struct ext4_sb_info *sbi = EXT4_SB(sb); 5076 5077 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) 5078 return "journal_async_commit"; 5079 if (test_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM)) 5080 return "journal_checksum"; 5081 if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) 5082 return "commit="; 5083 if (EXT4_MOUNT_DATA_FLAGS & 5084 (sbi->s_mount_opt ^ sbi->s_def_mount_opt)) 5085 return "data="; 5086 if (test_opt(sb, DATA_ERR_ABORT)) 5087 return "data_err=abort"; 5088 return NULL; 5089 } 5090 5091 /* 5092 * Limit the maximum folio order to 2048 blocks to prevent overestimation 5093 * of reserve handle credits during the folio writeback in environments 5094 * where the PAGE_SIZE exceeds 4KB. 5095 */ 5096 #define EXT4_MAX_PAGECACHE_ORDER(sb) \ 5097 umin(MAX_PAGECACHE_ORDER, (11 + (sb)->s_blocksize_bits - PAGE_SHIFT)) 5098 static void ext4_set_max_mapping_order(struct super_block *sb) 5099 { 5100 struct ext4_sb_info *sbi = EXT4_SB(sb); 5101 5102 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) 5103 sbi->s_max_folio_order = sbi->s_min_folio_order; 5104 else 5105 sbi->s_max_folio_order = EXT4_MAX_PAGECACHE_ORDER(sb); 5106 } 5107 5108 static int ext4_check_large_folio(struct super_block *sb) 5109 { 5110 const char *err_str = NULL; 5111 5112 if (ext4_has_feature_encrypt(sb)) 5113 err_str = "encrypt"; 5114 5115 if (!err_str) { 5116 ext4_set_max_mapping_order(sb); 5117 } else if (sb->s_blocksize > PAGE_SIZE) { 5118 ext4_msg(sb, KERN_ERR, "bs(%lu) > ps(%lu) unsupported for %s", 5119 sb->s_blocksize, PAGE_SIZE, err_str); 5120 return -EINVAL; 5121 } 5122 5123 return 0; 5124 } 5125 5126 static int ext4_load_super(struct super_block *sb, ext4_fsblk_t *lsb, 5127 int silent) 5128 { 5129 struct ext4_sb_info *sbi = EXT4_SB(sb); 5130 struct ext4_super_block *es; 5131 ext4_fsblk_t logical_sb_block; 5132 unsigned long offset = 0; 5133 struct buffer_head *bh; 5134 int ret = -EINVAL; 5135 int blocksize; 5136 5137 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE); 5138 if (!blocksize) { 5139 ext4_msg(sb, KERN_ERR, "unable to set blocksize"); 5140 return -EINVAL; 5141 } 5142 5143 /* 5144 * The ext4 superblock will not be buffer aligned for other than 1kB 5145 * block sizes. We need to calculate the offset from buffer start. 5146 */ 5147 if (blocksize != EXT4_MIN_BLOCK_SIZE) { 5148 logical_sb_block = sbi->s_sb_block * EXT4_MIN_BLOCK_SIZE; 5149 offset = do_div(logical_sb_block, blocksize); 5150 } else { 5151 logical_sb_block = sbi->s_sb_block; 5152 } 5153 5154 bh = ext4_sb_bread_unmovable(sb, logical_sb_block); 5155 if (IS_ERR(bh)) { 5156 ext4_msg(sb, KERN_ERR, "unable to read superblock"); 5157 return PTR_ERR(bh); 5158 } 5159 /* 5160 * Note: s_es must be initialized as soon as possible because 5161 * some ext4 macro-instructions depend on its value 5162 */ 5163 es = (struct ext4_super_block *) (bh->b_data + offset); 5164 sbi->s_es = es; 5165 sb->s_magic = le16_to_cpu(es->s_magic); 5166 if (sb->s_magic != EXT4_SUPER_MAGIC) { 5167 if (!silent) 5168 ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem"); 5169 goto out; 5170 } 5171 5172 if (le32_to_cpu(es->s_log_block_size) > 5173 (EXT4_MAX_BLOCK_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) { 5174 ext4_msg(sb, KERN_ERR, 5175 "Invalid log block size: %u", 5176 le32_to_cpu(es->s_log_block_size)); 5177 goto out; 5178 } 5179 if (le32_to_cpu(es->s_log_cluster_size) > 5180 (EXT4_MAX_CLUSTER_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) { 5181 ext4_msg(sb, KERN_ERR, 5182 "Invalid log cluster size: %u", 5183 le32_to_cpu(es->s_log_cluster_size)); 5184 goto out; 5185 } 5186 5187 blocksize = EXT4_MIN_BLOCK_SIZE << le32_to_cpu(es->s_log_block_size); 5188 5189 /* 5190 * If the default block size is not the same as the real block size, 5191 * we need to reload it. 5192 */ 5193 if (sb->s_blocksize == blocksize) 5194 goto success; 5195 5196 /* 5197 * bh must be released before kill_bdev(), otherwise 5198 * it won't be freed and its page also. kill_bdev() 5199 * is called by sb_set_blocksize(). 5200 */ 5201 brelse(bh); 5202 /* Validate the filesystem blocksize */ 5203 if (!sb_set_blocksize(sb, blocksize)) { 5204 ext4_msg(sb, KERN_ERR, "bad block size %d", 5205 blocksize); 5206 bh = NULL; 5207 goto out; 5208 } 5209 5210 logical_sb_block = sbi->s_sb_block * EXT4_MIN_BLOCK_SIZE; 5211 offset = do_div(logical_sb_block, blocksize); 5212 bh = ext4_sb_bread_unmovable(sb, logical_sb_block); 5213 if (IS_ERR(bh)) { 5214 ext4_msg(sb, KERN_ERR, "Can't read superblock on 2nd try"); 5215 ret = PTR_ERR(bh); 5216 bh = NULL; 5217 goto out; 5218 } 5219 es = (struct ext4_super_block *)(bh->b_data + offset); 5220 sbi->s_es = es; 5221 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) { 5222 ext4_msg(sb, KERN_ERR, "Magic mismatch, very weird!"); 5223 goto out; 5224 } 5225 5226 success: 5227 sbi->s_min_folio_order = get_order(blocksize); 5228 *lsb = logical_sb_block; 5229 sbi->s_sbh = bh; 5230 return 0; 5231 out: 5232 brelse(bh); 5233 return ret; 5234 } 5235 5236 static int ext4_hash_info_init(struct super_block *sb) 5237 { 5238 struct ext4_sb_info *sbi = EXT4_SB(sb); 5239 struct ext4_super_block *es = sbi->s_es; 5240 unsigned int i; 5241 5242 sbi->s_def_hash_version = es->s_def_hash_version; 5243 5244 if (sbi->s_def_hash_version > DX_HASH_LAST) { 5245 ext4_msg(sb, KERN_ERR, 5246 "Invalid default hash set in the superblock"); 5247 return -EINVAL; 5248 } else if (sbi->s_def_hash_version == DX_HASH_SIPHASH) { 5249 ext4_msg(sb, KERN_ERR, 5250 "SIPHASH is not a valid default hash value"); 5251 return -EINVAL; 5252 } 5253 5254 for (i = 0; i < 4; i++) 5255 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]); 5256 5257 if (ext4_has_feature_dir_index(sb)) { 5258 i = le32_to_cpu(es->s_flags); 5259 if (i & EXT2_FLAGS_UNSIGNED_HASH) 5260 sbi->s_hash_unsigned = 3; 5261 else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) { 5262 #ifdef __CHAR_UNSIGNED__ 5263 if (!sb_rdonly(sb)) 5264 es->s_flags |= 5265 cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH); 5266 sbi->s_hash_unsigned = 3; 5267 #else 5268 if (!sb_rdonly(sb)) 5269 es->s_flags |= 5270 cpu_to_le32(EXT2_FLAGS_SIGNED_HASH); 5271 #endif 5272 } 5273 } 5274 return 0; 5275 } 5276 5277 static int ext4_block_group_meta_init(struct super_block *sb, int silent) 5278 { 5279 struct ext4_sb_info *sbi = EXT4_SB(sb); 5280 struct ext4_super_block *es = sbi->s_es; 5281 int has_huge_files; 5282 5283 has_huge_files = ext4_has_feature_huge_file(sb); 5284 sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits, 5285 has_huge_files); 5286 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files); 5287 5288 sbi->s_desc_size = le16_to_cpu(es->s_desc_size); 5289 if (ext4_has_feature_64bit(sb)) { 5290 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT || 5291 sbi->s_desc_size > EXT4_MAX_DESC_SIZE || 5292 !is_power_of_2(sbi->s_desc_size)) { 5293 ext4_msg(sb, KERN_ERR, 5294 "unsupported descriptor size %lu", 5295 sbi->s_desc_size); 5296 return -EINVAL; 5297 } 5298 } else 5299 sbi->s_desc_size = EXT4_MIN_DESC_SIZE; 5300 5301 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group); 5302 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group); 5303 5304 sbi->s_inodes_per_block = sb->s_blocksize / EXT4_INODE_SIZE(sb); 5305 if (sbi->s_inodes_per_block == 0 || sbi->s_blocks_per_group == 0) { 5306 if (!silent) 5307 ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem"); 5308 return -EINVAL; 5309 } 5310 if (sbi->s_inodes_per_group < sbi->s_inodes_per_block || 5311 sbi->s_inodes_per_group > sb->s_blocksize * 8) { 5312 ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu\n", 5313 sbi->s_inodes_per_group); 5314 return -EINVAL; 5315 } 5316 sbi->s_itb_per_group = sbi->s_inodes_per_group / 5317 sbi->s_inodes_per_block; 5318 sbi->s_desc_per_block = sb->s_blocksize / EXT4_DESC_SIZE(sb); 5319 sbi->s_mount_state = le16_to_cpu(es->s_state) & ~EXT4_FC_REPLAY; 5320 sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb)); 5321 sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb)); 5322 5323 return 0; 5324 } 5325 5326 /* 5327 * It's hard to get stripe aligned blocks if stripe is not aligned with 5328 * cluster, just disable stripe and alert user to simplify code and avoid 5329 * stripe aligned allocation which will rarely succeed. 5330 */ 5331 static bool ext4_is_stripe_incompatible(struct super_block *sb, unsigned long stripe) 5332 { 5333 struct ext4_sb_info *sbi = EXT4_SB(sb); 5334 return (stripe > 0 && sbi->s_cluster_ratio > 1 && 5335 stripe % sbi->s_cluster_ratio != 0); 5336 } 5337 5338 static int __ext4_fill_super(struct fs_context *fc, struct super_block *sb) 5339 { 5340 struct ext4_super_block *es = NULL; 5341 struct ext4_sb_info *sbi = EXT4_SB(sb); 5342 ext4_fsblk_t logical_sb_block; 5343 struct inode *root; 5344 int needs_recovery; 5345 int err; 5346 ext4_group_t first_not_zeroed; 5347 struct ext4_fs_context *ctx = fc->fs_private; 5348 int silent = fc->sb_flags & SB_SILENT; 5349 5350 /* Set defaults for the variables that will be set during parsing */ 5351 if (!(ctx->spec & EXT4_SPEC_JOURNAL_IOPRIO)) 5352 ctx->journal_ioprio = EXT4_DEF_JOURNAL_IOPRIO; 5353 5354 sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS; 5355 sbi->s_sectors_written_start = 5356 part_stat_read(sb->s_bdev, sectors[STAT_WRITE]); 5357 5358 err = ext4_load_super(sb, &logical_sb_block, silent); 5359 if (err) 5360 goto out_fail; 5361 5362 es = sbi->s_es; 5363 sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written); 5364 5365 err = ext4_init_metadata_csum(sb, es); 5366 if (err) 5367 goto failed_mount; 5368 5369 ext4_set_def_opts(sb, es); 5370 5371 sbi->s_resuid = make_kuid(&init_user_ns, ext4_get_resuid(es)); 5372 sbi->s_resgid = make_kgid(&init_user_ns, ext4_get_resuid(es)); 5373 sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ; 5374 sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME; 5375 sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME; 5376 sbi->s_sb_update_kb = EXT4_DEF_SB_UPDATE_INTERVAL_KB; 5377 sbi->s_sb_update_sec = EXT4_DEF_SB_UPDATE_INTERVAL_SEC; 5378 5379 /* 5380 * set default s_li_wait_mult for lazyinit, for the case there is 5381 * no mount option specified. 5382 */ 5383 sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT; 5384 5385 err = ext4_inode_info_init(sb, es); 5386 if (err) 5387 goto failed_mount; 5388 5389 err = parse_apply_sb_mount_options(sb, ctx); 5390 if (err < 0) 5391 goto failed_mount; 5392 5393 sbi->s_def_mount_opt = sbi->s_mount_opt; 5394 sbi->s_def_mount_opt2 = sbi->s_mount_opt2; 5395 5396 err = ext4_check_opt_consistency(fc, sb); 5397 if (err < 0) 5398 goto failed_mount; 5399 5400 ext4_apply_options(fc, sb); 5401 5402 err = ext4_check_large_folio(sb); 5403 if (err < 0) 5404 goto failed_mount; 5405 5406 err = ext4_encoding_init(sb, es); 5407 if (err) 5408 goto failed_mount; 5409 5410 err = ext4_check_journal_data_mode(sb); 5411 if (err) 5412 goto failed_mount; 5413 5414 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) | 5415 (test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0); 5416 5417 /* HSM events are allowed by default. */ 5418 sb->s_iflags |= SB_I_ALLOW_HSM; 5419 5420 err = ext4_check_feature_compatibility(sb, es, silent); 5421 if (err) 5422 goto failed_mount; 5423 5424 err = ext4_block_group_meta_init(sb, silent); 5425 if (err) 5426 goto failed_mount; 5427 5428 err = ext4_hash_info_init(sb); 5429 if (err) 5430 goto failed_mount; 5431 5432 err = ext4_handle_clustersize(sb); 5433 if (err) 5434 goto failed_mount; 5435 5436 err = ext4_check_geometry(sb, es); 5437 if (err) 5438 goto failed_mount; 5439 5440 timer_setup(&sbi->s_err_report, print_daily_error_info, 0); 5441 spin_lock_init(&sbi->s_error_lock); 5442 mutex_init(&sbi->s_error_notify_mutex); 5443 INIT_WORK(&sbi->s_sb_upd_work, update_super_work); 5444 5445 err = ext4_group_desc_init(sb, es, logical_sb_block, &first_not_zeroed); 5446 if (err) 5447 goto failed_mount3; 5448 5449 err = ext4_es_register_shrinker(sbi); 5450 if (err) 5451 goto failed_mount3; 5452 5453 sbi->s_stripe = ext4_get_stripe_size(sbi); 5454 if (ext4_is_stripe_incompatible(sb, sbi->s_stripe)) { 5455 ext4_msg(sb, KERN_WARNING, 5456 "stripe (%lu) is not aligned with cluster size (%u), " 5457 "stripe is disabled", 5458 sbi->s_stripe, sbi->s_cluster_ratio); 5459 sbi->s_stripe = 0; 5460 } 5461 sbi->s_extent_max_zeroout_kb = 32; 5462 5463 /* 5464 * set up enough so that it can read an inode 5465 */ 5466 sb->s_op = &ext4_sops; 5467 sb->s_export_op = &ext4_export_ops; 5468 sb->s_xattr = ext4_xattr_handlers; 5469 #ifdef CONFIG_FS_ENCRYPTION 5470 sb->s_cop = &ext4_cryptops; 5471 #endif 5472 #ifdef CONFIG_FS_VERITY 5473 sb->s_vop = &ext4_verityops; 5474 #endif 5475 #ifdef CONFIG_QUOTA 5476 sb->dq_op = &ext4_quota_operations; 5477 if (ext4_has_feature_quota(sb)) 5478 sb->s_qcop = &dquot_quotactl_sysfile_ops; 5479 else 5480 sb->s_qcop = &ext4_qctl_operations; 5481 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ; 5482 #endif 5483 super_set_uuid(sb, es->s_uuid, sizeof(es->s_uuid)); 5484 super_set_sysfs_name_bdev(sb); 5485 5486 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */ 5487 mutex_init(&sbi->s_orphan_lock); 5488 5489 spin_lock_init(&sbi->s_bdev_wb_lock); 5490 5491 ext4_atomic_write_init(sb); 5492 ext4_fast_commit_init(sb); 5493 5494 sb->s_root = NULL; 5495 5496 needs_recovery = (es->s_last_orphan != 0 || 5497 ext4_has_feature_orphan_present(sb) || 5498 ext4_has_feature_journal_needs_recovery(sb)); 5499 5500 if (ext4_has_feature_mmp(sb) && !sb_rdonly(sb)) { 5501 err = ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)); 5502 if (err) 5503 goto failed_mount3a; 5504 } 5505 5506 err = -EINVAL; 5507 /* 5508 * The first inode we look at is the journal inode. Don't try 5509 * root first: it may be modified in the journal! 5510 */ 5511 if (!test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb)) { 5512 err = ext4_load_and_init_journal(sb, es, ctx); 5513 if (err) 5514 goto failed_mount3a; 5515 if (bdev_read_only(sb->s_bdev)) 5516 needs_recovery = 0; 5517 } else if (test_opt(sb, NOLOAD) && !sb_rdonly(sb) && 5518 ext4_has_feature_journal_needs_recovery(sb)) { 5519 ext4_msg(sb, KERN_ERR, "required journal recovery " 5520 "suppressed and not mounted read-only"); 5521 goto failed_mount3a; 5522 } else { 5523 const char *journal_option; 5524 5525 /* Nojournal mode, all journal mount options are illegal */ 5526 journal_option = ext4_has_journal_option(sb); 5527 if (journal_option != NULL) { 5528 ext4_msg(sb, KERN_ERR, 5529 "can't mount with %s, fs mounted w/o journal", 5530 journal_option); 5531 goto failed_mount3a; 5532 } 5533 5534 sbi->s_def_mount_opt &= ~EXT4_MOUNT_JOURNAL_CHECKSUM; 5535 clear_opt(sb, JOURNAL_CHECKSUM); 5536 clear_opt(sb, DATA_FLAGS); 5537 clear_opt2(sb, JOURNAL_FAST_COMMIT); 5538 sbi->s_journal = NULL; 5539 needs_recovery = 0; 5540 } 5541 5542 if (!test_opt(sb, NO_MBCACHE)) { 5543 sbi->s_ea_block_cache = ext4_xattr_create_cache(); 5544 if (!sbi->s_ea_block_cache) { 5545 ext4_msg(sb, KERN_ERR, 5546 "Failed to create ea_block_cache"); 5547 err = -EINVAL; 5548 goto failed_mount_wq; 5549 } 5550 5551 if (ext4_has_feature_ea_inode(sb)) { 5552 sbi->s_ea_inode_cache = ext4_xattr_create_cache(); 5553 if (!sbi->s_ea_inode_cache) { 5554 ext4_msg(sb, KERN_ERR, 5555 "Failed to create ea_inode_cache"); 5556 err = -EINVAL; 5557 goto failed_mount_wq; 5558 } 5559 } 5560 } 5561 5562 /* 5563 * Get the # of file system overhead blocks from the 5564 * superblock if present. 5565 */ 5566 sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters); 5567 /* ignore the precalculated value if it is ridiculous */ 5568 if (sbi->s_overhead > ext4_blocks_count(es)) 5569 sbi->s_overhead = 0; 5570 /* 5571 * If the bigalloc feature is not enabled recalculating the 5572 * overhead doesn't take long, so we might as well just redo 5573 * it to make sure we are using the correct value. 5574 */ 5575 if (!ext4_has_feature_bigalloc(sb)) 5576 sbi->s_overhead = 0; 5577 if (sbi->s_overhead == 0) { 5578 err = ext4_calculate_overhead(sb); 5579 if (err) 5580 goto failed_mount_wq; 5581 } 5582 5583 /* 5584 * The maximum number of concurrent works can be high and 5585 * concurrency isn't really necessary. Limit it to 1. 5586 */ 5587 EXT4_SB(sb)->rsv_conversion_wq = 5588 alloc_workqueue("ext4-rsv-conversion", WQ_MEM_RECLAIM | WQ_UNBOUND, 1); 5589 if (!EXT4_SB(sb)->rsv_conversion_wq) { 5590 printk(KERN_ERR "EXT4-fs: failed to create workqueue\n"); 5591 err = -ENOMEM; 5592 goto failed_mount4; 5593 } 5594 5595 /* 5596 * The jbd2_journal_load will have done any necessary log recovery, 5597 * so we can safely mount the rest of the filesystem now. 5598 */ 5599 5600 root = ext4_iget(sb, EXT4_ROOT_INO, EXT4_IGET_SPECIAL); 5601 if (IS_ERR(root)) { 5602 ext4_msg(sb, KERN_ERR, "get root inode failed"); 5603 err = PTR_ERR(root); 5604 root = NULL; 5605 goto failed_mount4; 5606 } 5607 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) { 5608 ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck"); 5609 iput(root); 5610 err = -EFSCORRUPTED; 5611 goto failed_mount4; 5612 } 5613 5614 generic_set_sb_d_ops(sb); 5615 sb->s_root = d_make_root(root); 5616 if (!sb->s_root) { 5617 ext4_msg(sb, KERN_ERR, "get root dentry failed"); 5618 err = -ENOMEM; 5619 goto failed_mount4; 5620 } 5621 5622 err = ext4_setup_super(sb, es, sb_rdonly(sb)); 5623 if (err == -EROFS) { 5624 sb->s_flags |= SB_RDONLY; 5625 } else if (err) 5626 goto failed_mount4a; 5627 5628 ext4_set_resv_clusters(sb); 5629 5630 if (test_opt(sb, BLOCK_VALIDITY)) { 5631 err = ext4_setup_system_zone(sb); 5632 if (err) { 5633 ext4_msg(sb, KERN_ERR, "failed to initialize system " 5634 "zone (%d)", err); 5635 goto failed_mount4a; 5636 } 5637 } 5638 ext4_fc_replay_cleanup(sb); 5639 5640 ext4_ext_init(sb); 5641 5642 /* 5643 * Enable optimize_scan if number of groups is > threshold. This can be 5644 * turned off by passing "mb_optimize_scan=0". This can also be 5645 * turned on forcefully by passing "mb_optimize_scan=1". 5646 */ 5647 if (!(ctx->spec & EXT4_SPEC_mb_optimize_scan)) { 5648 if (sbi->s_groups_count >= MB_DEFAULT_LINEAR_SCAN_THRESHOLD) 5649 set_opt2(sb, MB_OPTIMIZE_SCAN); 5650 else 5651 clear_opt2(sb, MB_OPTIMIZE_SCAN); 5652 } 5653 5654 err = ext4_percpu_param_init(sbi); 5655 if (err) 5656 goto failed_mount5; 5657 5658 err = ext4_mb_init(sb); 5659 if (err) { 5660 ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)", 5661 err); 5662 goto failed_mount5; 5663 } 5664 5665 /* 5666 * We can only set up the journal commit callback once 5667 * mballoc is initialized 5668 */ 5669 if (sbi->s_journal) 5670 sbi->s_journal->j_commit_callback = 5671 ext4_journal_commit_callback; 5672 5673 if (ext4_has_feature_flex_bg(sb)) 5674 if (!ext4_fill_flex_info(sb)) { 5675 ext4_msg(sb, KERN_ERR, 5676 "unable to initialize " 5677 "flex_bg meta info!"); 5678 err = -ENOMEM; 5679 goto failed_mount6; 5680 } 5681 5682 err = ext4_register_li_request(sb, first_not_zeroed); 5683 if (err) 5684 goto failed_mount6; 5685 5686 err = ext4_init_orphan_info(sb); 5687 if (err) 5688 goto failed_mount7; 5689 #ifdef CONFIG_QUOTA 5690 /* Enable quota usage during mount. */ 5691 if (ext4_has_feature_quota(sb) && !sb_rdonly(sb)) { 5692 err = ext4_enable_quotas(sb); 5693 if (err) 5694 goto failed_mount8; 5695 } 5696 #endif /* CONFIG_QUOTA */ 5697 5698 /* 5699 * Save the original bdev mapping's wb_err value which could be 5700 * used to detect the metadata async write error. 5701 */ 5702 errseq_check_and_advance(&sb->s_bdev->bd_mapping->wb_err, 5703 &sbi->s_bdev_wb_err); 5704 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS; 5705 ext4_orphan_cleanup(sb, es); 5706 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS; 5707 /* 5708 * Update the checksum after updating free space/inode counters and 5709 * ext4_orphan_cleanup. Otherwise the superblock can have an incorrect 5710 * checksum in the buffer cache until it is written out and 5711 * e2fsprogs programs trying to open a file system immediately 5712 * after it is mounted can fail. 5713 */ 5714 ext4_superblock_csum_set(sb); 5715 if (needs_recovery) { 5716 ext4_msg(sb, KERN_INFO, "recovery complete"); 5717 err = ext4_mark_recovery_complete(sb, es); 5718 if (err) 5719 goto failed_mount9; 5720 } 5721 5722 if (test_opt(sb, DISCARD) && !bdev_max_discard_sectors(sb->s_bdev)) { 5723 ext4_msg(sb, KERN_WARNING, 5724 "mounting with \"discard\" option, but the device does not support discard"); 5725 clear_opt(sb, DISCARD); 5726 } 5727 5728 if (es->s_error_count) { 5729 sbi->s_err_report_sec = 5*60; /* first time 5 minutes */ 5730 mod_timer(&sbi->s_err_report, 5731 jiffies + secs_to_jiffies(sbi->s_err_report_sec)); 5732 } 5733 sbi->s_err_report_sec = 24*60*60; /* Once a day */ 5734 5735 /* Enable message ratelimiting. Default is 10 messages per 5 secs. */ 5736 ratelimit_state_init(&sbi->s_err_ratelimit_state, 5 * HZ, 10); 5737 ratelimit_state_init(&sbi->s_warning_ratelimit_state, 5 * HZ, 10); 5738 ratelimit_state_init(&sbi->s_msg_ratelimit_state, 5 * HZ, 10); 5739 atomic_set(&sbi->s_warning_count, 0); 5740 atomic_set(&sbi->s_msg_count, 0); 5741 5742 /* Register sysfs after all initializations are complete. */ 5743 err = ext4_register_sysfs(sb); 5744 if (err) 5745 goto failed_mount9; 5746 5747 return 0; 5748 5749 failed_mount9: 5750 ext4_quotas_off(sb, EXT4_MAXQUOTAS); 5751 failed_mount8: __maybe_unused 5752 ext4_release_orphan_info(sb); 5753 failed_mount7: 5754 ext4_unregister_li_request(sb); 5755 failed_mount6: 5756 ext4_mb_release(sb); 5757 ext4_flex_groups_free(sbi); 5758 failed_mount5: 5759 ext4_percpu_param_destroy(sbi); 5760 ext4_ext_release(sb); 5761 ext4_release_system_zone(sb); 5762 failed_mount4a: 5763 dput(sb->s_root); 5764 sb->s_root = NULL; 5765 failed_mount4: 5766 ext4_msg(sb, KERN_ERR, "mount failed"); 5767 if (EXT4_SB(sb)->rsv_conversion_wq) 5768 destroy_workqueue(EXT4_SB(sb)->rsv_conversion_wq); 5769 failed_mount_wq: 5770 ext4_xattr_destroy_cache(sbi->s_ea_inode_cache); 5771 sbi->s_ea_inode_cache = NULL; 5772 5773 ext4_xattr_destroy_cache(sbi->s_ea_block_cache); 5774 sbi->s_ea_block_cache = NULL; 5775 5776 if (sbi->s_journal) { 5777 ext4_journal_destroy(sbi, sbi->s_journal); 5778 } 5779 failed_mount3a: 5780 ext4_es_unregister_shrinker(sbi); 5781 failed_mount3: 5782 /* flush s_sb_upd_work before sbi destroy */ 5783 flush_work(&sbi->s_sb_upd_work); 5784 ext4_stop_mmpd(sbi); 5785 timer_delete_sync(&sbi->s_err_report); 5786 ext4_group_desc_free(sbi); 5787 failed_mount: 5788 #if IS_ENABLED(CONFIG_UNICODE) 5789 utf8_unload(sb->s_encoding); 5790 #endif 5791 5792 #ifdef CONFIG_QUOTA 5793 for (unsigned int i = 0; i < EXT4_MAXQUOTAS; i++) 5794 kfree(get_qf_name(sb, sbi, i)); 5795 #endif 5796 fscrypt_free_dummy_policy(&sbi->s_dummy_enc_policy); 5797 brelse(sbi->s_sbh); 5798 if (sbi->s_journal_bdev_file) { 5799 invalidate_bdev(file_bdev(sbi->s_journal_bdev_file)); 5800 bdev_fput(sbi->s_journal_bdev_file); 5801 } 5802 out_fail: 5803 invalidate_bdev(sb->s_bdev); 5804 sb->s_fs_info = NULL; 5805 return err; 5806 } 5807 5808 static int ext4_fill_super(struct super_block *sb, struct fs_context *fc) 5809 { 5810 struct ext4_fs_context *ctx = fc->fs_private; 5811 struct ext4_sb_info *sbi; 5812 const char *descr; 5813 int ret; 5814 5815 sbi = ext4_alloc_sbi(sb); 5816 if (!sbi) 5817 return -ENOMEM; 5818 5819 fc->s_fs_info = sbi; 5820 5821 /* Cleanup superblock name */ 5822 strreplace(sb->s_id, '/', '!'); 5823 5824 sbi->s_sb_block = 1; /* Default super block location */ 5825 if (ctx->spec & EXT4_SPEC_s_sb_block) 5826 sbi->s_sb_block = ctx->s_sb_block; 5827 5828 ret = __ext4_fill_super(fc, sb); 5829 if (ret < 0) 5830 goto free_sbi; 5831 5832 if (sbi->s_journal) { 5833 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) 5834 descr = " journalled data mode"; 5835 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) 5836 descr = " ordered data mode"; 5837 else 5838 descr = " writeback data mode"; 5839 } else 5840 descr = "out journal"; 5841 5842 if (___ratelimit(&ext4_mount_msg_ratelimit, "EXT4-fs mount")) 5843 ext4_msg(sb, KERN_INFO, "mounted filesystem %pU %s with%s. " 5844 "Quota mode: %s.", &sb->s_uuid, 5845 sb_rdonly(sb) ? "ro" : "r/w", descr, 5846 ext4_quota_mode(sb)); 5847 5848 /* Update the s_overhead_clusters if necessary */ 5849 ext4_update_overhead(sb, false); 5850 return 0; 5851 5852 free_sbi: 5853 ext4_free_sbi(sbi); 5854 fc->s_fs_info = NULL; 5855 return ret; 5856 } 5857 5858 static int ext4_get_tree(struct fs_context *fc) 5859 { 5860 return get_tree_bdev(fc, ext4_fill_super); 5861 } 5862 5863 /* 5864 * Setup any per-fs journal parameters now. We'll do this both on 5865 * initial mount, once the journal has been initialised but before we've 5866 * done any recovery; and again on any subsequent remount. 5867 */ 5868 static void ext4_init_journal_params(struct super_block *sb, journal_t *journal) 5869 { 5870 struct ext4_sb_info *sbi = EXT4_SB(sb); 5871 5872 journal->j_commit_interval = sbi->s_commit_interval; 5873 journal->j_min_batch_time = sbi->s_min_batch_time; 5874 journal->j_max_batch_time = sbi->s_max_batch_time; 5875 ext4_fc_init(sb, journal); 5876 5877 write_lock(&journal->j_state_lock); 5878 if (test_opt(sb, BARRIER)) 5879 journal->j_flags |= JBD2_BARRIER; 5880 else 5881 journal->j_flags &= ~JBD2_BARRIER; 5882 /* 5883 * Always enable journal cycle record option, letting the journal 5884 * records log transactions continuously between each mount. 5885 */ 5886 journal->j_flags |= JBD2_CYCLE_RECORD; 5887 write_unlock(&journal->j_state_lock); 5888 } 5889 5890 static struct inode *ext4_get_journal_inode(struct super_block *sb, 5891 unsigned int journal_inum) 5892 { 5893 struct inode *journal_inode; 5894 5895 /* 5896 * Test for the existence of a valid inode on disk. Bad things 5897 * happen if we iget() an unused inode, as the subsequent iput() 5898 * will try to delete it. 5899 */ 5900 journal_inode = ext4_iget(sb, journal_inum, EXT4_IGET_SPECIAL); 5901 if (IS_ERR(journal_inode)) { 5902 ext4_msg(sb, KERN_ERR, "no journal found"); 5903 return ERR_CAST(journal_inode); 5904 } 5905 if (!journal_inode->i_nlink) { 5906 make_bad_inode(journal_inode); 5907 iput(journal_inode); 5908 ext4_msg(sb, KERN_ERR, "journal inode is deleted"); 5909 return ERR_PTR(-EFSCORRUPTED); 5910 } 5911 if (!S_ISREG(journal_inode->i_mode) || IS_ENCRYPTED(journal_inode)) { 5912 ext4_msg(sb, KERN_ERR, "invalid journal inode"); 5913 iput(journal_inode); 5914 return ERR_PTR(-EFSCORRUPTED); 5915 } 5916 5917 #ifdef CONFIG_LOCKDEP 5918 lockdep_set_subclass(&EXT4_I(journal_inode)->i_data_sem, 5919 I_DATA_SEM_JOURNAL); 5920 #endif 5921 5922 ext4_debug("Journal inode found at %p: %lld bytes\n", 5923 journal_inode, journal_inode->i_size); 5924 return journal_inode; 5925 } 5926 5927 static int ext4_journal_bmap(journal_t *journal, sector_t *block) 5928 { 5929 struct ext4_map_blocks map; 5930 int ret; 5931 5932 if (journal->j_inode == NULL) 5933 return 0; 5934 5935 map.m_lblk = *block; 5936 map.m_len = 1; 5937 ret = ext4_map_blocks(NULL, journal->j_inode, &map, 0); 5938 if (ret <= 0) { 5939 ext4_msg(journal->j_inode->i_sb, KERN_CRIT, 5940 "journal bmap failed: block %llu ret %d\n", 5941 *block, ret); 5942 jbd2_journal_abort(journal, ret ? ret : -EFSCORRUPTED); 5943 return ret; 5944 } 5945 *block = map.m_pblk; 5946 return 0; 5947 } 5948 5949 static journal_t *ext4_open_inode_journal(struct super_block *sb, 5950 unsigned int journal_inum) 5951 { 5952 struct inode *journal_inode; 5953 journal_t *journal; 5954 5955 journal_inode = ext4_get_journal_inode(sb, journal_inum); 5956 if (IS_ERR(journal_inode)) 5957 return ERR_CAST(journal_inode); 5958 5959 journal = jbd2_journal_init_inode(journal_inode); 5960 if (IS_ERR(journal)) { 5961 ext4_msg(sb, KERN_ERR, "Could not load journal inode"); 5962 iput(journal_inode); 5963 return ERR_CAST(journal); 5964 } 5965 journal->j_private = sb; 5966 journal->j_bmap = ext4_journal_bmap; 5967 ext4_init_journal_params(sb, journal); 5968 return journal; 5969 } 5970 5971 static struct file *ext4_get_journal_blkdev(struct super_block *sb, 5972 dev_t j_dev, ext4_fsblk_t *j_start, 5973 ext4_fsblk_t *j_len) 5974 { 5975 struct buffer_head *bh; 5976 struct block_device *bdev; 5977 struct file *bdev_file; 5978 int hblock, blocksize; 5979 ext4_fsblk_t sb_block; 5980 unsigned long offset; 5981 struct ext4_super_block *es; 5982 int errno; 5983 5984 bdev_file = bdev_file_open_by_dev(j_dev, 5985 BLK_OPEN_READ | BLK_OPEN_WRITE | BLK_OPEN_RESTRICT_WRITES, 5986 sb, &fs_holder_ops); 5987 if (IS_ERR(bdev_file)) { 5988 ext4_msg(sb, KERN_ERR, 5989 "failed to open journal device unknown-block(%u,%u) %pe", 5990 MAJOR(j_dev), MINOR(j_dev), bdev_file); 5991 return bdev_file; 5992 } 5993 5994 bdev = file_bdev(bdev_file); 5995 blocksize = sb->s_blocksize; 5996 hblock = bdev_logical_block_size(bdev); 5997 if (blocksize < hblock) { 5998 ext4_msg(sb, KERN_ERR, 5999 "blocksize too small for journal device"); 6000 errno = -EINVAL; 6001 goto out_bdev; 6002 } 6003 6004 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize; 6005 offset = EXT4_MIN_BLOCK_SIZE % blocksize; 6006 set_blocksize(bdev_file, blocksize); 6007 bh = __bread(bdev, sb_block, blocksize); 6008 if (!bh) { 6009 ext4_msg(sb, KERN_ERR, "couldn't read superblock of " 6010 "external journal"); 6011 errno = -EINVAL; 6012 goto out_bdev; 6013 } 6014 6015 es = (struct ext4_super_block *) (bh->b_data + offset); 6016 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) || 6017 !(le32_to_cpu(es->s_feature_incompat) & 6018 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) { 6019 ext4_msg(sb, KERN_ERR, "external journal has bad superblock"); 6020 errno = -EFSCORRUPTED; 6021 goto out_bh; 6022 } 6023 6024 if ((le32_to_cpu(es->s_feature_ro_compat) & 6025 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) && 6026 es->s_checksum != ext4_superblock_csum(es)) { 6027 ext4_msg(sb, KERN_ERR, "external journal has corrupt superblock"); 6028 errno = -EFSCORRUPTED; 6029 goto out_bh; 6030 } 6031 6032 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) { 6033 ext4_msg(sb, KERN_ERR, "journal UUID does not match"); 6034 errno = -EFSCORRUPTED; 6035 goto out_bh; 6036 } 6037 6038 *j_start = sb_block + 1; 6039 *j_len = ext4_blocks_count(es); 6040 brelse(bh); 6041 return bdev_file; 6042 6043 out_bh: 6044 brelse(bh); 6045 out_bdev: 6046 bdev_fput(bdev_file); 6047 return ERR_PTR(errno); 6048 } 6049 6050 static journal_t *ext4_open_dev_journal(struct super_block *sb, 6051 dev_t j_dev) 6052 { 6053 journal_t *journal; 6054 ext4_fsblk_t j_start; 6055 ext4_fsblk_t j_len; 6056 struct file *bdev_file; 6057 int errno = 0; 6058 6059 bdev_file = ext4_get_journal_blkdev(sb, j_dev, &j_start, &j_len); 6060 if (IS_ERR(bdev_file)) 6061 return ERR_CAST(bdev_file); 6062 6063 journal = jbd2_journal_init_dev(file_bdev(bdev_file), sb->s_bdev, j_start, 6064 j_len, sb->s_blocksize); 6065 if (IS_ERR(journal)) { 6066 ext4_msg(sb, KERN_ERR, "failed to create device journal"); 6067 errno = PTR_ERR(journal); 6068 goto out_bdev; 6069 } 6070 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) { 6071 ext4_msg(sb, KERN_ERR, "External journal has more than one " 6072 "user (unsupported) - %d", 6073 be32_to_cpu(journal->j_superblock->s_nr_users)); 6074 errno = -EINVAL; 6075 goto out_journal; 6076 } 6077 journal->j_private = sb; 6078 EXT4_SB(sb)->s_journal_bdev_file = bdev_file; 6079 ext4_init_journal_params(sb, journal); 6080 return journal; 6081 6082 out_journal: 6083 ext4_journal_destroy(EXT4_SB(sb), journal); 6084 out_bdev: 6085 bdev_fput(bdev_file); 6086 return ERR_PTR(errno); 6087 } 6088 6089 static int ext4_load_journal(struct super_block *sb, 6090 struct ext4_super_block *es, 6091 unsigned long journal_devnum) 6092 { 6093 journal_t *journal; 6094 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum); 6095 dev_t journal_dev; 6096 int err = 0; 6097 int really_read_only; 6098 int journal_dev_ro; 6099 6100 if (WARN_ON_ONCE(!ext4_has_feature_journal(sb))) 6101 return -EFSCORRUPTED; 6102 6103 if (journal_devnum && 6104 journal_devnum != le32_to_cpu(es->s_journal_dev)) { 6105 ext4_msg(sb, KERN_INFO, "external journal device major/minor " 6106 "numbers have changed"); 6107 journal_dev = new_decode_dev(journal_devnum); 6108 } else 6109 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev)); 6110 6111 if (journal_inum && journal_dev) { 6112 ext4_msg(sb, KERN_ERR, 6113 "filesystem has both journal inode and journal device!"); 6114 return -EINVAL; 6115 } 6116 6117 if (journal_inum) { 6118 journal = ext4_open_inode_journal(sb, journal_inum); 6119 if (IS_ERR(journal)) 6120 return PTR_ERR(journal); 6121 } else { 6122 journal = ext4_open_dev_journal(sb, journal_dev); 6123 if (IS_ERR(journal)) 6124 return PTR_ERR(journal); 6125 } 6126 6127 journal_dev_ro = bdev_read_only(journal->j_dev); 6128 really_read_only = bdev_read_only(sb->s_bdev) | journal_dev_ro; 6129 6130 if (journal_dev_ro && !sb_rdonly(sb)) { 6131 ext4_msg(sb, KERN_ERR, 6132 "journal device read-only, try mounting with '-o ro'"); 6133 err = -EROFS; 6134 goto err_out; 6135 } 6136 6137 /* 6138 * Are we loading a blank journal or performing recovery after a 6139 * crash? For recovery, we need to check in advance whether we 6140 * can get read-write access to the device. 6141 */ 6142 if (ext4_has_feature_journal_needs_recovery(sb)) { 6143 if (sb_rdonly(sb)) { 6144 ext4_msg(sb, KERN_INFO, "INFO: recovery " 6145 "required on readonly filesystem"); 6146 if (really_read_only) { 6147 ext4_msg(sb, KERN_ERR, "write access " 6148 "unavailable, cannot proceed " 6149 "(try mounting with noload)"); 6150 err = -EROFS; 6151 goto err_out; 6152 } 6153 ext4_msg(sb, KERN_INFO, "write access will " 6154 "be enabled during recovery"); 6155 } 6156 } 6157 6158 if (!(journal->j_flags & JBD2_BARRIER)) 6159 ext4_msg(sb, KERN_INFO, "barriers disabled"); 6160 6161 if (!ext4_has_feature_journal_needs_recovery(sb)) 6162 err = jbd2_journal_wipe(journal, !really_read_only); 6163 if (!err) { 6164 char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL); 6165 __le16 orig_state; 6166 bool changed = false; 6167 6168 if (save) 6169 memcpy(save, ((char *) es) + 6170 EXT4_S_ERR_START, EXT4_S_ERR_LEN); 6171 err = jbd2_journal_load(journal); 6172 if (save && memcmp(((char *) es) + EXT4_S_ERR_START, 6173 save, EXT4_S_ERR_LEN)) { 6174 memcpy(((char *) es) + EXT4_S_ERR_START, 6175 save, EXT4_S_ERR_LEN); 6176 changed = true; 6177 } 6178 kfree(save); 6179 orig_state = es->s_state; 6180 es->s_state |= cpu_to_le16(EXT4_SB(sb)->s_mount_state & 6181 EXT4_ERROR_FS); 6182 if (orig_state != es->s_state) 6183 changed = true; 6184 /* Write out restored error information to the superblock */ 6185 if (changed && !really_read_only) { 6186 int err2; 6187 err2 = ext4_commit_super(sb); 6188 err = err ? : err2; 6189 } 6190 } 6191 6192 if (err) { 6193 ext4_msg(sb, KERN_ERR, "error loading journal"); 6194 goto err_out; 6195 } 6196 6197 EXT4_SB(sb)->s_journal = journal; 6198 err = ext4_clear_journal_err(sb, es); 6199 if (err) { 6200 ext4_journal_destroy(EXT4_SB(sb), journal); 6201 return err; 6202 } 6203 6204 if (!really_read_only && journal_devnum && 6205 journal_devnum != le32_to_cpu(es->s_journal_dev)) { 6206 es->s_journal_dev = cpu_to_le32(journal_devnum); 6207 ext4_commit_super(sb); 6208 } 6209 if (!really_read_only && journal_inum && 6210 journal_inum != le32_to_cpu(es->s_journal_inum)) { 6211 es->s_journal_inum = cpu_to_le32(journal_inum); 6212 ext4_commit_super(sb); 6213 } 6214 6215 return 0; 6216 6217 err_out: 6218 ext4_journal_destroy(EXT4_SB(sb), journal); 6219 return err; 6220 } 6221 6222 /* Copy state of EXT4_SB(sb) into buffer for on-disk superblock */ 6223 static void ext4_update_super(struct super_block *sb) 6224 { 6225 struct ext4_sb_info *sbi = EXT4_SB(sb); 6226 struct ext4_super_block *es = sbi->s_es; 6227 struct buffer_head *sbh = sbi->s_sbh; 6228 6229 lock_buffer(sbh); 6230 /* 6231 * If the file system is mounted read-only, don't update the 6232 * superblock write time. This avoids updating the superblock 6233 * write time when we are mounting the root file system 6234 * read/only but we need to replay the journal; at that point, 6235 * for people who are east of GMT and who make their clock 6236 * tick in localtime for Windows bug-for-bug compatibility, 6237 * the clock is set in the future, and this will cause e2fsck 6238 * to complain and force a full file system check. 6239 */ 6240 if (!sb_rdonly(sb)) 6241 ext4_update_tstamp(es, s_wtime); 6242 es->s_kbytes_written = 6243 cpu_to_le64(sbi->s_kbytes_written + 6244 ((part_stat_read(sb->s_bdev, sectors[STAT_WRITE]) - 6245 sbi->s_sectors_written_start) >> 1)); 6246 if (percpu_counter_initialized(&sbi->s_freeclusters_counter)) 6247 ext4_free_blocks_count_set(es, 6248 EXT4_C2B(sbi, percpu_counter_sum_positive( 6249 &sbi->s_freeclusters_counter))); 6250 if (percpu_counter_initialized(&sbi->s_freeinodes_counter)) 6251 es->s_free_inodes_count = 6252 cpu_to_le32(percpu_counter_sum_positive( 6253 &sbi->s_freeinodes_counter)); 6254 /* Copy error information to the on-disk superblock */ 6255 spin_lock(&sbi->s_error_lock); 6256 if (sbi->s_add_error_count > 0) { 6257 es->s_state |= cpu_to_le16(EXT4_ERROR_FS); 6258 if (!es->s_first_error_time && !es->s_first_error_time_hi) { 6259 __ext4_update_tstamp(&es->s_first_error_time, 6260 &es->s_first_error_time_hi, 6261 sbi->s_first_error_time); 6262 strtomem_pad(es->s_first_error_func, 6263 sbi->s_first_error_func, 0); 6264 es->s_first_error_line = 6265 cpu_to_le32(sbi->s_first_error_line); 6266 es->s_first_error_ino = 6267 cpu_to_le32(sbi->s_first_error_ino); 6268 es->s_first_error_block = 6269 cpu_to_le64(sbi->s_first_error_block); 6270 es->s_first_error_errcode = 6271 ext4_errno_to_code(sbi->s_first_error_code); 6272 } 6273 __ext4_update_tstamp(&es->s_last_error_time, 6274 &es->s_last_error_time_hi, 6275 sbi->s_last_error_time); 6276 strtomem_pad(es->s_last_error_func, sbi->s_last_error_func, 0); 6277 es->s_last_error_line = cpu_to_le32(sbi->s_last_error_line); 6278 es->s_last_error_ino = cpu_to_le32(sbi->s_last_error_ino); 6279 es->s_last_error_block = cpu_to_le64(sbi->s_last_error_block); 6280 es->s_last_error_errcode = 6281 ext4_errno_to_code(sbi->s_last_error_code); 6282 /* 6283 * Start the daily error reporting function if it hasn't been 6284 * started already and sbi->s_err_report_sec is not zero 6285 */ 6286 if (!es->s_error_count && !sbi->s_err_report_sec) 6287 mod_timer(&sbi->s_err_report, 6288 jiffies + secs_to_jiffies(sbi->s_err_report_sec)); 6289 le32_add_cpu(&es->s_error_count, sbi->s_add_error_count); 6290 sbi->s_add_error_count = 0; 6291 } 6292 spin_unlock(&sbi->s_error_lock); 6293 6294 ext4_superblock_csum_set(sb); 6295 unlock_buffer(sbh); 6296 } 6297 6298 static int ext4_commit_super(struct super_block *sb) 6299 { 6300 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh; 6301 6302 if (!sbh) 6303 return -EINVAL; 6304 6305 ext4_update_super(sb); 6306 6307 lock_buffer(sbh); 6308 /* Buffer got discarded which means block device got invalidated */ 6309 if (!buffer_mapped(sbh)) { 6310 unlock_buffer(sbh); 6311 return -EIO; 6312 } 6313 6314 if (buffer_write_io_error(sbh) || !buffer_uptodate(sbh)) { 6315 /* 6316 * Oh, dear. A previous attempt to write the 6317 * superblock failed. This could happen because the 6318 * USB device was yanked out. Or it could happen to 6319 * be a transient write error and maybe the block will 6320 * be remapped. Nothing we can do but to retry the 6321 * write and hope for the best. 6322 */ 6323 ext4_msg(sb, KERN_ERR, "previous I/O error to " 6324 "superblock detected"); 6325 clear_buffer_write_io_error(sbh); 6326 set_buffer_uptodate(sbh); 6327 } 6328 /* Clear potential dirty bit if it was journalled update */ 6329 clear_buffer_dirty(sbh); 6330 bh_submit(sbh, REQ_OP_WRITE | REQ_SYNC | 6331 (test_opt(sb, BARRIER) ? REQ_FUA : 0), bh_end_write); 6332 wait_on_buffer(sbh); 6333 if (buffer_write_io_error(sbh)) { 6334 ext4_msg(sb, KERN_ERR, "I/O error while writing " 6335 "superblock"); 6336 clear_buffer_write_io_error(sbh); 6337 set_buffer_uptodate(sbh); 6338 return -EIO; 6339 } 6340 return 0; 6341 } 6342 6343 /* 6344 * Have we just finished recovery? If so, and if we are mounting (or 6345 * remounting) the filesystem readonly, then we will end up with a 6346 * consistent fs on disk. Record that fact. 6347 */ 6348 static int ext4_mark_recovery_complete(struct super_block *sb, 6349 struct ext4_super_block *es) 6350 { 6351 int err; 6352 journal_t *journal = EXT4_SB(sb)->s_journal; 6353 6354 if (!ext4_has_feature_journal(sb)) { 6355 if (journal != NULL) { 6356 ext4_error(sb, "Journal got removed while the fs was " 6357 "mounted!"); 6358 return -EFSCORRUPTED; 6359 } 6360 return 0; 6361 } 6362 jbd2_journal_lock_updates(journal); 6363 err = jbd2_journal_flush(journal, 0); 6364 if (err < 0) 6365 goto out; 6366 6367 if (sb_rdonly(sb) && (ext4_has_feature_journal_needs_recovery(sb) || 6368 ext4_has_feature_orphan_present(sb))) { 6369 if (!ext4_orphan_file_empty(sb)) { 6370 ext4_error(sb, "Orphan file not empty on read-only fs."); 6371 err = -EFSCORRUPTED; 6372 goto out; 6373 } 6374 ext4_clear_feature_journal_needs_recovery(sb); 6375 ext4_clear_feature_orphan_present(sb); 6376 ext4_commit_super(sb); 6377 } 6378 out: 6379 jbd2_journal_unlock_updates(journal); 6380 return err; 6381 } 6382 6383 /* 6384 * If we are mounting (or read-write remounting) a filesystem whose journal 6385 * has recorded an error from a previous lifetime, move that error to the 6386 * main filesystem now. 6387 */ 6388 static int ext4_clear_journal_err(struct super_block *sb, 6389 struct ext4_super_block *es) 6390 { 6391 journal_t *journal; 6392 int j_errno; 6393 const char *errstr; 6394 6395 if (!ext4_has_feature_journal(sb)) { 6396 ext4_error(sb, "Journal got removed while the fs was mounted!"); 6397 return -EFSCORRUPTED; 6398 } 6399 6400 journal = EXT4_SB(sb)->s_journal; 6401 6402 /* 6403 * Now check for any error status which may have been recorded in the 6404 * journal by a prior ext4_error() or ext4_abort() 6405 */ 6406 6407 j_errno = jbd2_journal_errno(journal); 6408 if (j_errno) { 6409 char nbuf[16]; 6410 6411 errstr = ext4_decode_error(sb, j_errno, nbuf); 6412 ext4_warning(sb, "Filesystem error recorded " 6413 "from previous mount: %s", errstr); 6414 6415 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS; 6416 es->s_state |= cpu_to_le16(EXT4_ERROR_FS); 6417 j_errno = ext4_commit_super(sb); 6418 if (j_errno) 6419 return j_errno; 6420 ext4_warning(sb, "Marked fs in need of filesystem check."); 6421 6422 jbd2_journal_clear_err(journal); 6423 jbd2_journal_update_sb_errno(journal); 6424 } 6425 return 0; 6426 } 6427 6428 /* 6429 * Force the running and committing transactions to commit, 6430 * and wait on the commit. 6431 */ 6432 int ext4_force_commit(struct super_block *sb) 6433 { 6434 return ext4_journal_force_commit(EXT4_SB(sb)->s_journal); 6435 } 6436 6437 static int ext4_sync_fs(struct super_block *sb, int wait) 6438 { 6439 int ret = 0; 6440 tid_t target; 6441 bool needs_barrier = false; 6442 struct ext4_sb_info *sbi = EXT4_SB(sb); 6443 6444 ret = ext4_emergency_state(sb); 6445 if (unlikely(ret)) 6446 return ret; 6447 6448 trace_ext4_sync_fs(sb, wait); 6449 flush_workqueue(sbi->rsv_conversion_wq); 6450 /* 6451 * Writeback quota in non-journalled quota case - journalled quota has 6452 * no dirty dquots 6453 */ 6454 dquot_writeback_dquots(sb, -1); 6455 /* 6456 * Data writeback is possible w/o journal transaction, so barrier must 6457 * being sent at the end of the function. But we can skip it if 6458 * transaction_commit will do it for us. 6459 */ 6460 if (sbi->s_journal) { 6461 target = jbd2_get_latest_transaction(sbi->s_journal); 6462 if (wait && sbi->s_journal->j_flags & JBD2_BARRIER && 6463 !jbd2_trans_will_send_data_barrier(sbi->s_journal, target)) 6464 needs_barrier = true; 6465 6466 if (jbd2_journal_start_commit(sbi->s_journal, &target)) { 6467 if (wait) 6468 ret = jbd2_log_wait_commit(sbi->s_journal, 6469 target); 6470 } 6471 } else if (wait && test_opt(sb, BARRIER)) 6472 needs_barrier = true; 6473 if (needs_barrier) { 6474 int err; 6475 err = blkdev_issue_flush(sb->s_bdev); 6476 if (!ret) 6477 ret = err; 6478 } 6479 6480 return ret; 6481 } 6482 6483 /* 6484 * LVM calls this function before a (read-only) snapshot is created. This 6485 * gives us a chance to flush the journal completely and mark the fs clean. 6486 * 6487 * Note that only this function cannot bring a filesystem to be in a clean 6488 * state independently. It relies on upper layer to stop all data & metadata 6489 * modifications. 6490 */ 6491 static int ext4_freeze(struct super_block *sb) 6492 { 6493 int error = 0; 6494 journal_t *journal = EXT4_SB(sb)->s_journal; 6495 6496 if (journal) { 6497 /* Now we set up the journal barrier. */ 6498 jbd2_journal_lock_updates(journal); 6499 6500 /* 6501 * Don't clear the needs_recovery flag if we failed to 6502 * flush the journal. 6503 */ 6504 error = jbd2_journal_flush(journal, 0); 6505 if (error < 0) 6506 goto out; 6507 6508 /* Journal blocked and flushed, clear needs_recovery flag. */ 6509 ext4_clear_feature_journal_needs_recovery(sb); 6510 if (ext4_orphan_file_empty(sb)) 6511 ext4_clear_feature_orphan_present(sb); 6512 } 6513 6514 error = ext4_commit_super(sb); 6515 out: 6516 if (journal) 6517 /* we rely on upper layer to stop further updates */ 6518 jbd2_journal_unlock_updates(journal); 6519 return error; 6520 } 6521 6522 /* 6523 * Called by LVM after the snapshot is done. We need to reset the RECOVER 6524 * flag here, even though the filesystem is not technically dirty yet. 6525 */ 6526 static int ext4_unfreeze(struct super_block *sb) 6527 { 6528 if (ext4_emergency_state(sb)) 6529 return 0; 6530 6531 if (EXT4_SB(sb)->s_journal) { 6532 /* Reset the needs_recovery flag before the fs is unlocked. */ 6533 ext4_set_feature_journal_needs_recovery(sb); 6534 if (ext4_has_feature_orphan_file(sb)) 6535 ext4_set_feature_orphan_present(sb); 6536 } 6537 6538 ext4_commit_super(sb); 6539 return 0; 6540 } 6541 6542 /* 6543 * Structure to save mount options for ext4_remount's benefit 6544 */ 6545 struct ext4_mount_options { 6546 unsigned long s_mount_opt; 6547 unsigned long s_mount_opt2; 6548 kuid_t s_resuid; 6549 kgid_t s_resgid; 6550 unsigned long s_commit_interval; 6551 u32 s_min_batch_time, s_max_batch_time; 6552 #ifdef CONFIG_QUOTA 6553 int s_jquota_fmt; 6554 char *s_qf_names[EXT4_MAXQUOTAS]; 6555 #endif 6556 }; 6557 6558 static int __ext4_remount(struct fs_context *fc, struct super_block *sb) 6559 { 6560 struct ext4_fs_context *ctx = fc->fs_private; 6561 struct ext4_super_block *es; 6562 struct ext4_sb_info *sbi = EXT4_SB(sb); 6563 unsigned long old_sb_flags; 6564 struct ext4_mount_options old_opts; 6565 ext4_group_t g; 6566 int err = 0; 6567 int alloc_ctx; 6568 #ifdef CONFIG_QUOTA 6569 int enable_quota = 0; 6570 int i, j; 6571 char *to_free[EXT4_MAXQUOTAS]; 6572 #endif 6573 6574 6575 /* Store the original options */ 6576 old_sb_flags = sb->s_flags; 6577 old_opts.s_mount_opt = sbi->s_mount_opt; 6578 old_opts.s_mount_opt2 = sbi->s_mount_opt2; 6579 old_opts.s_resuid = sbi->s_resuid; 6580 old_opts.s_resgid = sbi->s_resgid; 6581 old_opts.s_commit_interval = sbi->s_commit_interval; 6582 old_opts.s_min_batch_time = sbi->s_min_batch_time; 6583 old_opts.s_max_batch_time = sbi->s_max_batch_time; 6584 #ifdef CONFIG_QUOTA 6585 old_opts.s_jquota_fmt = sbi->s_jquota_fmt; 6586 for (i = 0; i < EXT4_MAXQUOTAS; i++) 6587 if (sbi->s_qf_names[i]) { 6588 char *qf_name = get_qf_name(sb, sbi, i); 6589 6590 old_opts.s_qf_names[i] = kstrdup(qf_name, GFP_KERNEL); 6591 if (!old_opts.s_qf_names[i]) { 6592 for (j = 0; j < i; j++) 6593 kfree(old_opts.s_qf_names[j]); 6594 return -ENOMEM; 6595 } 6596 } else 6597 old_opts.s_qf_names[i] = NULL; 6598 #endif 6599 if (!(ctx->spec & EXT4_SPEC_JOURNAL_IOPRIO)) { 6600 if (sbi->s_journal && sbi->s_journal->j_task->io_context) 6601 ctx->journal_ioprio = 6602 sbi->s_journal->j_task->io_context->ioprio; 6603 else 6604 ctx->journal_ioprio = EXT4_DEF_JOURNAL_IOPRIO; 6605 6606 } 6607 6608 if ((ctx->spec & EXT4_SPEC_s_stripe) && 6609 ext4_is_stripe_incompatible(sb, ctx->s_stripe)) { 6610 ext4_msg(sb, KERN_WARNING, 6611 "stripe (%lu) is not aligned with cluster size (%u), " 6612 "stripe is disabled", 6613 ctx->s_stripe, sbi->s_cluster_ratio); 6614 ctx->s_stripe = 0; 6615 } 6616 6617 /* 6618 * Changing the DIOREAD_NOLOCK or DELALLOC mount options may cause 6619 * two calls to ext4_should_dioread_nolock() to return inconsistent 6620 * values, triggering WARN_ON in ext4_add_complete_io(). we grab 6621 * here s_writepages_rwsem to avoid race between writepages ops and 6622 * remount. 6623 */ 6624 alloc_ctx = ext4_writepages_down_write(sb); 6625 ext4_apply_options(fc, sb); 6626 ext4_writepages_up_write(sb, alloc_ctx); 6627 6628 if ((old_opts.s_mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) ^ 6629 test_opt(sb, JOURNAL_CHECKSUM)) { 6630 ext4_msg(sb, KERN_ERR, "changing journal_checksum " 6631 "during remount not supported; ignoring"); 6632 sbi->s_mount_opt ^= EXT4_MOUNT_JOURNAL_CHECKSUM; 6633 } 6634 6635 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) { 6636 if (test_opt2(sb, EXPLICIT_DELALLOC)) { 6637 ext4_msg(sb, KERN_ERR, "can't mount with " 6638 "both data=journal and delalloc"); 6639 err = -EINVAL; 6640 goto restore_opts; 6641 } 6642 if (test_opt(sb, DIOREAD_NOLOCK)) { 6643 ext4_msg(sb, KERN_ERR, "can't mount with " 6644 "both data=journal and dioread_nolock"); 6645 err = -EINVAL; 6646 goto restore_opts; 6647 } 6648 } else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) { 6649 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) { 6650 ext4_msg(sb, KERN_ERR, "can't mount with " 6651 "journal_async_commit in data=ordered mode"); 6652 err = -EINVAL; 6653 goto restore_opts; 6654 } 6655 } 6656 6657 if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_NO_MBCACHE) { 6658 ext4_msg(sb, KERN_ERR, "can't enable nombcache during remount"); 6659 err = -EINVAL; 6660 goto restore_opts; 6661 } 6662 6663 if ((old_opts.s_mount_opt & EXT4_MOUNT_DELALLOC) && 6664 !test_opt(sb, DELALLOC)) { 6665 ext4_msg(sb, KERN_ERR, "can't disable delalloc during remount"); 6666 err = -EINVAL; 6667 goto restore_opts; 6668 } 6669 6670 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) | 6671 (test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0); 6672 6673 es = sbi->s_es; 6674 6675 if (sbi->s_journal) { 6676 ext4_init_journal_params(sb, sbi->s_journal); 6677 set_task_ioprio(sbi->s_journal->j_task, ctx->journal_ioprio); 6678 } 6679 6680 /* Flush outstanding errors before changing fs state */ 6681 flush_work(&sbi->s_sb_upd_work); 6682 6683 if ((bool)(fc->sb_flags & SB_RDONLY) != sb_rdonly(sb)) { 6684 if (ext4_emergency_state(sb)) { 6685 err = -EROFS; 6686 goto restore_opts; 6687 } 6688 6689 if (fc->sb_flags & SB_RDONLY) { 6690 err = sync_filesystem(sb); 6691 if (err < 0) 6692 goto restore_opts; 6693 err = dquot_suspend(sb, -1); 6694 if (err < 0) 6695 goto restore_opts; 6696 6697 /* 6698 * First of all, the unconditional stuff we have to do 6699 * to disable replay of the journal when we next remount 6700 */ 6701 sb->s_flags |= SB_RDONLY; 6702 6703 /* 6704 * OK, test if we are remounting a valid rw partition 6705 * readonly, and if so set the rdonly flag and then 6706 * mark the partition as valid again. 6707 */ 6708 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) && 6709 (sbi->s_mount_state & EXT4_VALID_FS)) 6710 es->s_state = cpu_to_le16(sbi->s_mount_state); 6711 6712 if (sbi->s_journal) { 6713 /* 6714 * We let remount-ro finish even if marking fs 6715 * as clean failed... 6716 */ 6717 ext4_mark_recovery_complete(sb, es); 6718 } 6719 } else { 6720 /* Make sure we can mount this feature set readwrite */ 6721 if (ext4_has_feature_readonly(sb) || 6722 !ext4_feature_set_ok(sb, 0)) { 6723 err = -EROFS; 6724 goto restore_opts; 6725 } 6726 /* 6727 * Make sure the group descriptor checksums 6728 * are sane. If they aren't, refuse to remount r/w. 6729 */ 6730 for (g = 0; g < sbi->s_groups_count; g++) { 6731 struct ext4_group_desc *gdp = 6732 ext4_get_group_desc(sb, g, NULL); 6733 6734 if (!ext4_group_desc_csum_verify(sb, g, gdp)) { 6735 ext4_msg(sb, KERN_ERR, 6736 "ext4_remount: Checksum for group %u failed (%u!=%u)", 6737 g, le16_to_cpu(ext4_group_desc_csum(sb, g, gdp)), 6738 le16_to_cpu(gdp->bg_checksum)); 6739 err = -EFSBADCRC; 6740 goto restore_opts; 6741 } 6742 } 6743 6744 /* 6745 * If we have an unprocessed orphan list hanging 6746 * around from a previously readonly bdev mount, 6747 * require a full umount/remount for now. 6748 */ 6749 if (es->s_last_orphan || !ext4_orphan_file_empty(sb)) { 6750 ext4_msg(sb, KERN_WARNING, "Couldn't " 6751 "remount RDWR because of unprocessed " 6752 "orphan inode list. Please " 6753 "umount/remount instead"); 6754 err = -EINVAL; 6755 goto restore_opts; 6756 } 6757 6758 /* 6759 * Mounting a RDONLY partition read-write, so reread 6760 * and store the current valid flag. (It may have 6761 * been changed by e2fsck since we originally mounted 6762 * the partition.) 6763 */ 6764 if (sbi->s_journal) { 6765 err = ext4_clear_journal_err(sb, es); 6766 if (err) 6767 goto restore_opts; 6768 } 6769 sbi->s_mount_state = (le16_to_cpu(es->s_state) & 6770 ~EXT4_FC_REPLAY); 6771 6772 err = ext4_setup_super(sb, es, 0); 6773 if (err) 6774 goto restore_opts; 6775 6776 sb->s_flags &= ~SB_RDONLY; 6777 if (ext4_has_feature_mmp(sb)) { 6778 err = ext4_multi_mount_protect(sb, 6779 le64_to_cpu(es->s_mmp_block)); 6780 if (err) 6781 goto restore_opts; 6782 } 6783 #ifdef CONFIG_QUOTA 6784 enable_quota = 1; 6785 #endif 6786 } 6787 } 6788 6789 /* 6790 * Handle creation of system zone data early because it can fail. 6791 * Releasing of existing data is done when we are sure remount will 6792 * succeed. 6793 */ 6794 if (test_opt(sb, BLOCK_VALIDITY) && !sbi->s_system_blks) { 6795 err = ext4_setup_system_zone(sb); 6796 if (err) 6797 goto restore_opts; 6798 } 6799 6800 if (sbi->s_journal == NULL && !(old_sb_flags & SB_RDONLY)) { 6801 err = ext4_commit_super(sb); 6802 if (err) 6803 goto restore_opts; 6804 } 6805 6806 #ifdef CONFIG_QUOTA 6807 if (enable_quota) { 6808 if (sb_any_quota_suspended(sb)) 6809 dquot_resume(sb, -1); 6810 else if (ext4_has_feature_quota(sb)) { 6811 err = ext4_enable_quotas(sb); 6812 if (err) 6813 goto restore_opts; 6814 } 6815 } 6816 /* Release old quota file names */ 6817 for (i = 0; i < EXT4_MAXQUOTAS; i++) 6818 kfree(old_opts.s_qf_names[i]); 6819 #endif 6820 if (!test_opt(sb, BLOCK_VALIDITY) && sbi->s_system_blks) 6821 ext4_release_system_zone(sb); 6822 6823 /* 6824 * Reinitialize lazy itable initialization thread based on 6825 * current settings 6826 */ 6827 if (sb_rdonly(sb) || !test_opt(sb, INIT_INODE_TABLE)) 6828 ext4_unregister_li_request(sb); 6829 else { 6830 ext4_group_t first_not_zeroed; 6831 first_not_zeroed = ext4_has_uninit_itable(sb); 6832 ext4_register_li_request(sb, first_not_zeroed); 6833 } 6834 6835 if (!ext4_has_feature_mmp(sb) || sb_rdonly(sb)) 6836 ext4_stop_mmpd(sbi); 6837 6838 /* 6839 * Handle aborting the filesystem as the last thing during remount to 6840 * avoid obsure errors during remount when some option changes fail to 6841 * apply due to shutdown filesystem. 6842 */ 6843 if (test_opt2(sb, ABORT)) 6844 ext4_abort(sb, ESHUTDOWN, "Abort forced by user"); 6845 6846 return 0; 6847 6848 restore_opts: 6849 /* 6850 * If there was a failing r/w to ro transition, we may need to 6851 * re-enable quota 6852 */ 6853 if (sb_rdonly(sb) && !(old_sb_flags & SB_RDONLY) && 6854 sb_any_quota_suspended(sb)) 6855 dquot_resume(sb, -1); 6856 6857 alloc_ctx = ext4_writepages_down_write(sb); 6858 sb->s_flags = old_sb_flags; 6859 sbi->s_mount_opt = old_opts.s_mount_opt; 6860 sbi->s_mount_opt2 = old_opts.s_mount_opt2; 6861 sbi->s_resuid = old_opts.s_resuid; 6862 sbi->s_resgid = old_opts.s_resgid; 6863 sbi->s_commit_interval = old_opts.s_commit_interval; 6864 sbi->s_min_batch_time = old_opts.s_min_batch_time; 6865 sbi->s_max_batch_time = old_opts.s_max_batch_time; 6866 ext4_writepages_up_write(sb, alloc_ctx); 6867 6868 if (!test_opt(sb, BLOCK_VALIDITY) && sbi->s_system_blks) 6869 ext4_release_system_zone(sb); 6870 #ifdef CONFIG_QUOTA 6871 sbi->s_jquota_fmt = old_opts.s_jquota_fmt; 6872 for (i = 0; i < EXT4_MAXQUOTAS; i++) { 6873 to_free[i] = get_qf_name(sb, sbi, i); 6874 rcu_assign_pointer(sbi->s_qf_names[i], old_opts.s_qf_names[i]); 6875 } 6876 synchronize_rcu(); 6877 for (i = 0; i < EXT4_MAXQUOTAS; i++) 6878 kfree(to_free[i]); 6879 #endif 6880 if (!ext4_has_feature_mmp(sb) || sb_rdonly(sb)) 6881 ext4_stop_mmpd(sbi); 6882 return err; 6883 } 6884 6885 static int ext4_reconfigure(struct fs_context *fc) 6886 { 6887 struct super_block *sb = fc->root->d_sb; 6888 int ret; 6889 bool old_ro = sb_rdonly(sb); 6890 6891 fc->s_fs_info = EXT4_SB(sb); 6892 6893 ret = ext4_check_opt_consistency(fc, sb); 6894 if (ret < 0) 6895 return ret; 6896 6897 ret = __ext4_remount(fc, sb); 6898 if (ret < 0) 6899 return ret; 6900 6901 ext4_msg(sb, KERN_INFO, "re-mounted %pU%s.", 6902 &sb->s_uuid, 6903 (old_ro != sb_rdonly(sb)) ? (sb_rdonly(sb) ? " ro" : " r/w") : ""); 6904 6905 return 0; 6906 } 6907 6908 #ifdef CONFIG_QUOTA 6909 static int ext4_statfs_project(struct super_block *sb, 6910 kprojid_t projid, struct kstatfs *buf) 6911 { 6912 struct kqid qid; 6913 struct dquot *dquot; 6914 u64 limit; 6915 u64 curblock; 6916 6917 qid = make_kqid_projid(projid); 6918 dquot = dqget(sb, qid); 6919 if (IS_ERR(dquot)) 6920 return PTR_ERR(dquot); 6921 spin_lock(&dquot->dq_dqb_lock); 6922 6923 limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit, 6924 dquot->dq_dqb.dqb_bhardlimit); 6925 limit >>= sb->s_blocksize_bits; 6926 6927 if (limit) { 6928 uint64_t remaining = 0; 6929 6930 curblock = (dquot->dq_dqb.dqb_curspace + 6931 dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits; 6932 if (limit > curblock) 6933 remaining = limit - curblock; 6934 6935 buf->f_blocks = min(buf->f_blocks, limit); 6936 buf->f_bfree = min(buf->f_bfree, remaining); 6937 buf->f_bavail = min(buf->f_bavail, remaining); 6938 } 6939 6940 limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit, 6941 dquot->dq_dqb.dqb_ihardlimit); 6942 if (limit) { 6943 uint64_t remaining = 0; 6944 6945 if (limit > dquot->dq_dqb.dqb_curinodes) 6946 remaining = limit - dquot->dq_dqb.dqb_curinodes; 6947 6948 buf->f_files = min(buf->f_files, limit); 6949 buf->f_ffree = min(buf->f_ffree, remaining); 6950 } 6951 6952 spin_unlock(&dquot->dq_dqb_lock); 6953 dqput(dquot); 6954 return 0; 6955 } 6956 #endif 6957 6958 static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf) 6959 { 6960 struct super_block *sb = dentry->d_sb; 6961 struct ext4_sb_info *sbi = EXT4_SB(sb); 6962 struct ext4_super_block *es = sbi->s_es; 6963 ext4_fsblk_t overhead = 0, resv_blocks; 6964 s64 bfree; 6965 resv_blocks = EXT4_C2B(sbi, atomic64_read(&sbi->s_resv_clusters)); 6966 6967 if (!test_opt(sb, MINIX_DF)) 6968 overhead = sbi->s_overhead; 6969 6970 buf->f_type = EXT4_SUPER_MAGIC; 6971 buf->f_bsize = sb->s_blocksize; 6972 buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, overhead); 6973 bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) - 6974 percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter); 6975 /* prevent underflow in case that few free space is available */ 6976 buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0)); 6977 buf->f_bavail = buf->f_bfree - 6978 (ext4_r_blocks_count(es) + resv_blocks); 6979 if (buf->f_bfree < (ext4_r_blocks_count(es) + resv_blocks)) 6980 buf->f_bavail = 0; 6981 buf->f_files = le32_to_cpu(es->s_inodes_count); 6982 buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter); 6983 buf->f_namelen = EXT4_NAME_LEN; 6984 buf->f_fsid = uuid_to_fsid(es->s_uuid); 6985 6986 #ifdef CONFIG_QUOTA 6987 if (ext4_test_inode_flag(dentry->d_inode, EXT4_INODE_PROJINHERIT) && 6988 sb_has_quota_limits_enabled(sb, PRJQUOTA)) 6989 ext4_statfs_project(sb, EXT4_I(dentry->d_inode)->i_projid, buf); 6990 #endif 6991 return 0; 6992 } 6993 6994 6995 #ifdef CONFIG_QUOTA 6996 6997 /* 6998 * Helper functions so that transaction is started before we acquire dqio_sem 6999 * to keep correct lock ordering of transaction > dqio_sem 7000 */ 7001 static inline struct inode *dquot_to_inode(struct dquot *dquot) 7002 { 7003 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type]; 7004 } 7005 7006 static int ext4_write_dquot(struct dquot *dquot) 7007 { 7008 int ret, err; 7009 handle_t *handle; 7010 struct inode *inode; 7011 7012 inode = dquot_to_inode(dquot); 7013 handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 7014 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb)); 7015 if (IS_ERR(handle)) 7016 return PTR_ERR(handle); 7017 ret = dquot_commit(dquot); 7018 if (ret < 0) 7019 ext4_error_err(dquot->dq_sb, -ret, 7020 "Failed to commit dquot type %d", 7021 dquot->dq_id.type); 7022 err = ext4_journal_stop(handle); 7023 if (!ret) 7024 ret = err; 7025 return ret; 7026 } 7027 7028 static int ext4_acquire_dquot(struct dquot *dquot) 7029 { 7030 int ret, err; 7031 handle_t *handle; 7032 7033 handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA, 7034 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb)); 7035 if (IS_ERR(handle)) 7036 return PTR_ERR(handle); 7037 ret = dquot_acquire(dquot); 7038 if (ret < 0) 7039 ext4_error_err(dquot->dq_sb, -ret, 7040 "Failed to acquire dquot type %d", 7041 dquot->dq_id.type); 7042 err = ext4_journal_stop(handle); 7043 if (!ret) 7044 ret = err; 7045 return ret; 7046 } 7047 7048 static int ext4_release_dquot(struct dquot *dquot) 7049 { 7050 int ret, err; 7051 handle_t *handle; 7052 bool freeze_protected = false; 7053 7054 /* 7055 * Trying to sb_start_intwrite() in a running transaction 7056 * can result in a deadlock. Further, running transactions 7057 * are already protected from freezing. 7058 */ 7059 if (!ext4_journal_current_handle()) { 7060 sb_start_intwrite(dquot->dq_sb); 7061 freeze_protected = true; 7062 } 7063 7064 handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA, 7065 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb)); 7066 if (IS_ERR(handle)) { 7067 /* Release dquot anyway to avoid endless cycle in dqput() */ 7068 dquot_release(dquot); 7069 if (freeze_protected) 7070 sb_end_intwrite(dquot->dq_sb); 7071 return PTR_ERR(handle); 7072 } 7073 ret = dquot_release(dquot); 7074 if (ret < 0) 7075 ext4_error_err(dquot->dq_sb, -ret, 7076 "Failed to release dquot type %d", 7077 dquot->dq_id.type); 7078 err = ext4_journal_stop(handle); 7079 if (!ret) 7080 ret = err; 7081 7082 if (freeze_protected) 7083 sb_end_intwrite(dquot->dq_sb); 7084 7085 return ret; 7086 } 7087 7088 static int ext4_mark_dquot_dirty(struct dquot *dquot) 7089 { 7090 struct super_block *sb = dquot->dq_sb; 7091 7092 if (ext4_is_quota_journalled(sb)) { 7093 dquot_mark_dquot_dirty(dquot); 7094 return ext4_write_dquot(dquot); 7095 } else { 7096 return dquot_mark_dquot_dirty(dquot); 7097 } 7098 } 7099 7100 static int ext4_write_info(struct super_block *sb, int type) 7101 { 7102 int ret, err; 7103 handle_t *handle; 7104 7105 /* Data block + inode block */ 7106 handle = ext4_journal_start_sb(sb, EXT4_HT_QUOTA, 2); 7107 if (IS_ERR(handle)) 7108 return PTR_ERR(handle); 7109 ret = dquot_commit_info(sb, type); 7110 err = ext4_journal_stop(handle); 7111 if (!ret) 7112 ret = err; 7113 return ret; 7114 } 7115 7116 static void lockdep_set_quota_inode(struct inode *inode, int subclass) 7117 { 7118 struct ext4_inode_info *ei = EXT4_I(inode); 7119 7120 /* The first argument of lockdep_set_subclass has to be 7121 * *exactly* the same as the argument to init_rwsem() --- in 7122 * this case, in init_once() --- or lockdep gets unhappy 7123 * because the name of the lock is set using the 7124 * stringification of the argument to init_rwsem(). 7125 */ 7126 (void) ei; /* shut up clang warning if !CONFIG_LOCKDEP */ 7127 lockdep_set_subclass(&ei->i_data_sem, subclass); 7128 } 7129 7130 /* 7131 * Standard function to be called on quota_on 7132 */ 7133 static int ext4_quota_on(struct super_block *sb, int type, int format_id, 7134 const struct path *path) 7135 { 7136 int err; 7137 7138 if (!test_opt(sb, QUOTA)) 7139 return -EINVAL; 7140 7141 /* Quotafile not on the same filesystem? */ 7142 if (path->dentry->d_sb != sb) 7143 return -EXDEV; 7144 7145 /* Quota already enabled for this file? */ 7146 if (IS_NOQUOTA(d_inode(path->dentry))) 7147 return -EBUSY; 7148 7149 /* Journaling quota? */ 7150 if (EXT4_SB(sb)->s_qf_names[type]) { 7151 /* Quotafile not in fs root? */ 7152 if (path->dentry->d_parent != sb->s_root) 7153 ext4_msg(sb, KERN_WARNING, 7154 "Quota file not on filesystem root. " 7155 "Journaled quota will not work"); 7156 sb_dqopt(sb)->flags |= DQUOT_NOLIST_DIRTY; 7157 } else { 7158 /* 7159 * Clear the flag just in case mount options changed since 7160 * last time. 7161 */ 7162 sb_dqopt(sb)->flags &= ~DQUOT_NOLIST_DIRTY; 7163 } 7164 7165 lockdep_set_quota_inode(path->dentry->d_inode, I_DATA_SEM_QUOTA); 7166 err = dquot_quota_on(sb, type, format_id, path); 7167 if (!err) { 7168 struct inode *inode = d_inode(path->dentry); 7169 handle_t *handle; 7170 7171 /* 7172 * Set inode flags to prevent userspace from messing with quota 7173 * files. If this fails, we return success anyway since quotas 7174 * are already enabled and this is not a hard failure. 7175 */ 7176 inode_lock(inode); 7177 handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1); 7178 if (IS_ERR(handle)) 7179 goto unlock_inode; 7180 EXT4_I(inode)->i_flags |= EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL; 7181 inode_set_flags(inode, S_NOATIME | S_IMMUTABLE, 7182 S_NOATIME | S_IMMUTABLE); 7183 err = ext4_mark_inode_dirty(handle, inode); 7184 ext4_journal_stop(handle); 7185 unlock_inode: 7186 inode_unlock(inode); 7187 if (err) 7188 dquot_quota_off(sb, type); 7189 } 7190 if (err) 7191 lockdep_set_quota_inode(path->dentry->d_inode, 7192 I_DATA_SEM_NORMAL); 7193 return err; 7194 } 7195 7196 static inline bool ext4_check_quota_inum(int type, unsigned long qf_inum) 7197 { 7198 switch (type) { 7199 case USRQUOTA: 7200 return qf_inum == EXT4_USR_QUOTA_INO; 7201 case GRPQUOTA: 7202 return qf_inum == EXT4_GRP_QUOTA_INO; 7203 case PRJQUOTA: 7204 return qf_inum >= EXT4_GOOD_OLD_FIRST_INO; 7205 default: 7206 BUG(); 7207 } 7208 } 7209 7210 static int ext4_quota_enable(struct super_block *sb, int type, int format_id, 7211 unsigned int flags) 7212 { 7213 int err; 7214 struct inode *qf_inode; 7215 unsigned long qf_inums[EXT4_MAXQUOTAS] = { 7216 le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum), 7217 le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum), 7218 le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum) 7219 }; 7220 7221 BUG_ON(!ext4_has_feature_quota(sb)); 7222 7223 if (!qf_inums[type]) 7224 return -EPERM; 7225 7226 if (!ext4_check_quota_inum(type, qf_inums[type])) { 7227 ext4_error(sb, "Bad quota inum: %lu, type: %d", 7228 qf_inums[type], type); 7229 return -EUCLEAN; 7230 } 7231 7232 qf_inode = ext4_iget(sb, qf_inums[type], EXT4_IGET_SPECIAL); 7233 if (IS_ERR(qf_inode)) { 7234 ext4_error(sb, "Bad quota inode: %lu, type: %d", 7235 qf_inums[type], type); 7236 return PTR_ERR(qf_inode); 7237 } 7238 7239 /* Don't account quota for quota files to avoid recursion */ 7240 qf_inode->i_flags |= S_NOQUOTA; 7241 lockdep_set_quota_inode(qf_inode, I_DATA_SEM_QUOTA); 7242 err = dquot_load_quota_inode(qf_inode, type, format_id, flags); 7243 if (err) 7244 lockdep_set_quota_inode(qf_inode, I_DATA_SEM_NORMAL); 7245 iput(qf_inode); 7246 7247 return err; 7248 } 7249 7250 /* Enable usage tracking for all quota types. */ 7251 int ext4_enable_quotas(struct super_block *sb) 7252 { 7253 int type, err = 0; 7254 unsigned long qf_inums[EXT4_MAXQUOTAS] = { 7255 le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum), 7256 le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum), 7257 le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum) 7258 }; 7259 bool quota_mopt[EXT4_MAXQUOTAS] = { 7260 test_opt(sb, USRQUOTA), 7261 test_opt(sb, GRPQUOTA), 7262 test_opt(sb, PRJQUOTA), 7263 }; 7264 7265 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY; 7266 for (type = 0; type < EXT4_MAXQUOTAS; type++) { 7267 if (qf_inums[type]) { 7268 err = ext4_quota_enable(sb, type, QFMT_VFS_V1, 7269 DQUOT_USAGE_ENABLED | 7270 (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0)); 7271 if (err) { 7272 ext4_warning(sb, 7273 "Failed to enable quota tracking " 7274 "(type=%d, err=%d, ino=%lu). " 7275 "Please run e2fsck to fix.", type, 7276 err, qf_inums[type]); 7277 7278 ext4_quotas_off(sb, type); 7279 return err; 7280 } 7281 } 7282 } 7283 return 0; 7284 } 7285 7286 static int ext4_quota_off(struct super_block *sb, int type) 7287 { 7288 struct inode *inode = sb_dqopt(sb)->files[type]; 7289 handle_t *handle; 7290 int err; 7291 7292 /* Force all delayed allocation blocks to be allocated. 7293 * Caller already holds s_umount sem */ 7294 if (test_opt(sb, DELALLOC)) 7295 sync_filesystem(sb); 7296 7297 if (!inode || !igrab(inode)) 7298 goto out; 7299 7300 err = dquot_quota_off(sb, type); 7301 if (err || ext4_has_feature_quota(sb)) 7302 goto out_put; 7303 /* 7304 * When the filesystem was remounted read-only first, we cannot cleanup 7305 * inode flags here. Bad luck but people should be using QUOTA feature 7306 * these days anyway. 7307 */ 7308 if (sb_rdonly(sb)) 7309 goto out_put; 7310 7311 inode_lock(inode); 7312 /* 7313 * Update modification times of quota files when userspace can 7314 * start looking at them. If we fail, we return success anyway since 7315 * this is not a hard failure and quotas are already disabled. 7316 */ 7317 handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1); 7318 if (IS_ERR(handle)) { 7319 err = PTR_ERR(handle); 7320 goto out_unlock; 7321 } 7322 EXT4_I(inode)->i_flags &= ~(EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL); 7323 inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE); 7324 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); 7325 err = ext4_mark_inode_dirty(handle, inode); 7326 ext4_journal_stop(handle); 7327 out_unlock: 7328 inode_unlock(inode); 7329 out_put: 7330 lockdep_set_quota_inode(inode, I_DATA_SEM_NORMAL); 7331 iput(inode); 7332 return err; 7333 out: 7334 return dquot_quota_off(sb, type); 7335 } 7336 7337 /* Read data from quotafile - avoid pagecache and such because we cannot afford 7338 * acquiring the locks... As quota files are never truncated and quota code 7339 * itself serializes the operations (and no one else should touch the files) 7340 * we don't have to be afraid of races */ 7341 static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data, 7342 size_t len, loff_t off) 7343 { 7344 struct inode *inode = sb_dqopt(sb)->files[type]; 7345 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb); 7346 int offset = off & (sb->s_blocksize - 1); 7347 int tocopy; 7348 size_t toread; 7349 struct buffer_head *bh; 7350 loff_t i_size = i_size_read(inode); 7351 7352 if (off > i_size) 7353 return 0; 7354 if (off+len > i_size) 7355 len = i_size-off; 7356 toread = len; 7357 while (toread > 0) { 7358 tocopy = min_t(unsigned long, sb->s_blocksize - offset, toread); 7359 bh = ext4_bread(NULL, inode, blk, 0); 7360 if (IS_ERR(bh)) 7361 return PTR_ERR(bh); 7362 if (!bh) /* A hole? */ 7363 memset(data, 0, tocopy); 7364 else 7365 memcpy(data, bh->b_data+offset, tocopy); 7366 brelse(bh); 7367 offset = 0; 7368 toread -= tocopy; 7369 data += tocopy; 7370 blk++; 7371 } 7372 return len; 7373 } 7374 7375 /* Write to quotafile (we know the transaction is already started and has 7376 * enough credits) */ 7377 static ssize_t ext4_quota_write(struct super_block *sb, int type, 7378 const char *data, size_t len, loff_t off) 7379 { 7380 struct inode *inode = sb_dqopt(sb)->files[type]; 7381 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb); 7382 int err = 0, err2 = 0, offset = off & (sb->s_blocksize - 1); 7383 int retries = 0; 7384 struct buffer_head *bh; 7385 handle_t *handle = journal_current_handle(); 7386 7387 if (!handle) { 7388 ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)" 7389 " cancelled because transaction is not started", 7390 (unsigned long long)off, (unsigned long long)len); 7391 return -EIO; 7392 } 7393 /* 7394 * Since we account only one data block in transaction credits, 7395 * then it is impossible to cross a block boundary. 7396 */ 7397 if (sb->s_blocksize - offset < len) { 7398 ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)" 7399 " cancelled because not block aligned", 7400 (unsigned long long)off, (unsigned long long)len); 7401 return -EIO; 7402 } 7403 7404 do { 7405 bh = ext4_bread(handle, inode, blk, 7406 EXT4_GET_BLOCKS_CREATE | 7407 EXT4_GET_BLOCKS_METADATA_NOFAIL); 7408 } while (PTR_ERR(bh) == -ENOSPC && 7409 ext4_should_retry_alloc(inode->i_sb, &retries)); 7410 if (IS_ERR(bh)) 7411 return PTR_ERR(bh); 7412 if (!bh) 7413 goto out; 7414 BUFFER_TRACE(bh, "get write access"); 7415 err = ext4_journal_get_write_access(handle, sb, bh, EXT4_JTR_NONE); 7416 if (err) { 7417 brelse(bh); 7418 return err; 7419 } 7420 lock_buffer(bh); 7421 memcpy(bh->b_data+offset, data, len); 7422 flush_dcache_folio(bh->b_folio); 7423 unlock_buffer(bh); 7424 err = ext4_handle_dirty_metadata(handle, NULL, bh); 7425 brelse(bh); 7426 out: 7427 if (inode->i_size < off + len) { 7428 i_size_write(inode, off + len); 7429 EXT4_I(inode)->i_disksize = inode->i_size; 7430 err2 = ext4_mark_inode_dirty(handle, inode); 7431 if (unlikely(err2 && !err)) 7432 err = err2; 7433 } 7434 return err ? err : len; 7435 } 7436 #endif 7437 7438 #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2) 7439 static inline void register_as_ext2(void) 7440 { 7441 int err = register_filesystem(&ext2_fs_type); 7442 if (err) 7443 printk(KERN_WARNING 7444 "EXT4-fs: Unable to register as ext2 (%d)\n", err); 7445 } 7446 7447 static inline void unregister_as_ext2(void) 7448 { 7449 unregister_filesystem(&ext2_fs_type); 7450 } 7451 7452 static inline int ext2_feature_set_ok(struct super_block *sb) 7453 { 7454 if (ext4_has_unknown_ext2_incompat_features(sb)) 7455 return 0; 7456 if (sb_rdonly(sb)) 7457 return 1; 7458 if (ext4_has_unknown_ext2_ro_compat_features(sb)) 7459 return 0; 7460 return 1; 7461 } 7462 #else 7463 static inline void register_as_ext2(void) { } 7464 static inline void unregister_as_ext2(void) { } 7465 static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; } 7466 #endif 7467 7468 static inline void register_as_ext3(void) 7469 { 7470 int err = register_filesystem(&ext3_fs_type); 7471 if (err) 7472 printk(KERN_WARNING 7473 "EXT4-fs: Unable to register as ext3 (%d)\n", err); 7474 } 7475 7476 static inline void unregister_as_ext3(void) 7477 { 7478 unregister_filesystem(&ext3_fs_type); 7479 } 7480 7481 static inline int ext3_feature_set_ok(struct super_block *sb) 7482 { 7483 if (ext4_has_unknown_ext3_incompat_features(sb)) 7484 return 0; 7485 if (!ext4_has_feature_journal(sb)) 7486 return 0; 7487 if (sb_rdonly(sb)) 7488 return 1; 7489 if (ext4_has_unknown_ext3_ro_compat_features(sb)) 7490 return 0; 7491 return 1; 7492 } 7493 7494 static void ext4_kill_sb(struct super_block *sb) 7495 { 7496 struct ext4_sb_info *sbi = EXT4_SB(sb); 7497 struct file *bdev_file = sbi ? sbi->s_journal_bdev_file : NULL; 7498 7499 kill_block_super(sb); 7500 7501 if (bdev_file) 7502 bdev_fput(bdev_file); 7503 } 7504 7505 static struct file_system_type ext4_fs_type = { 7506 .owner = THIS_MODULE, 7507 .name = "ext4", 7508 .init_fs_context = ext4_init_fs_context, 7509 .parameters = ext4_param_specs, 7510 .kill_sb = ext4_kill_sb, 7511 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP | FS_MGTIME | 7512 FS_LBS, 7513 }; 7514 MODULE_ALIAS_FS("ext4"); 7515 7516 static int __init ext4_init_fs(void) 7517 { 7518 int err; 7519 7520 ratelimit_state_init(&ext4_mount_msg_ratelimit, 30 * HZ, 64); 7521 ext4_li_info = NULL; 7522 7523 /* Build-time check for flags consistency */ 7524 ext4_check_flag_values(); 7525 7526 err = ext4_init_es(); 7527 if (err) 7528 return err; 7529 7530 err = ext4_init_pending(); 7531 if (err) 7532 goto out7; 7533 7534 err = ext4_init_post_read_processing(); 7535 if (err) 7536 goto out6; 7537 7538 err = ext4_init_pageio(); 7539 if (err) 7540 goto out5; 7541 7542 err = ext4_init_system_zone(); 7543 if (err) 7544 goto out4; 7545 7546 err = ext4_init_sysfs(); 7547 if (err) 7548 goto out3; 7549 7550 err = ext4_init_mballoc(); 7551 if (err) 7552 goto out2; 7553 err = init_inodecache(); 7554 if (err) 7555 goto out1; 7556 7557 err = ext4_fc_init_dentry_cache(); 7558 if (err) 7559 goto out05; 7560 7561 register_as_ext3(); 7562 register_as_ext2(); 7563 err = register_filesystem(&ext4_fs_type); 7564 if (err) 7565 goto out; 7566 7567 return 0; 7568 out: 7569 unregister_as_ext2(); 7570 unregister_as_ext3(); 7571 ext4_fc_destroy_dentry_cache(); 7572 out05: 7573 destroy_inodecache(); 7574 out1: 7575 ext4_exit_mballoc(); 7576 out2: 7577 ext4_exit_sysfs(); 7578 out3: 7579 ext4_exit_system_zone(); 7580 out4: 7581 ext4_exit_pageio(); 7582 out5: 7583 ext4_exit_post_read_processing(); 7584 out6: 7585 ext4_exit_pending(); 7586 out7: 7587 ext4_exit_es(); 7588 7589 return err; 7590 } 7591 7592 static void __exit ext4_exit_fs(void) 7593 { 7594 ext4_destroy_lazyinit_thread(); 7595 unregister_as_ext2(); 7596 unregister_as_ext3(); 7597 unregister_filesystem(&ext4_fs_type); 7598 ext4_fc_destroy_dentry_cache(); 7599 destroy_inodecache(); 7600 ext4_exit_mballoc(); 7601 ext4_exit_sysfs(); 7602 ext4_exit_system_zone(); 7603 ext4_exit_pageio(); 7604 ext4_exit_post_read_processing(); 7605 ext4_exit_es(); 7606 ext4_exit_pending(); 7607 } 7608 7609 MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others"); 7610 MODULE_DESCRIPTION("Fourth Extended Filesystem"); 7611 MODULE_LICENSE("GPL"); 7612 module_init(ext4_init_fs) 7613 module_exit(ext4_exit_fs) 7614