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