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