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