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