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