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