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