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