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