1f5166768STheodore Ts'o // SPDX-License-Identifier: GPL-2.0 2ac27a0ecSDave Kleikamp /* 3617ba13bSMingming Cao * linux/fs/ext4/super.c 4ac27a0ecSDave Kleikamp * 5ac27a0ecSDave Kleikamp * Copyright (C) 1992, 1993, 1994, 1995 6ac27a0ecSDave Kleikamp * Remy Card (card@masi.ibp.fr) 7ac27a0ecSDave Kleikamp * Laboratoire MASI - Institut Blaise Pascal 8ac27a0ecSDave Kleikamp * Universite Pierre et Marie Curie (Paris VI) 9ac27a0ecSDave Kleikamp * 10ac27a0ecSDave Kleikamp * from 11ac27a0ecSDave Kleikamp * 12ac27a0ecSDave Kleikamp * linux/fs/minix/inode.c 13ac27a0ecSDave Kleikamp * 14ac27a0ecSDave Kleikamp * Copyright (C) 1991, 1992 Linus Torvalds 15ac27a0ecSDave Kleikamp * 16ac27a0ecSDave Kleikamp * Big-endian to little-endian byte-swapping/bitmaps by 17ac27a0ecSDave Kleikamp * David S. Miller (davem@caip.rutgers.edu), 1995 18ac27a0ecSDave Kleikamp */ 19ac27a0ecSDave Kleikamp 20ac27a0ecSDave Kleikamp #include <linux/module.h> 21ac27a0ecSDave Kleikamp #include <linux/string.h> 22ac27a0ecSDave Kleikamp #include <linux/fs.h> 23ac27a0ecSDave Kleikamp #include <linux/time.h> 24c5ca7c76STheodore Ts'o #include <linux/vmalloc.h> 25ac27a0ecSDave Kleikamp #include <linux/slab.h> 26ac27a0ecSDave Kleikamp #include <linux/init.h> 27ac27a0ecSDave Kleikamp #include <linux/blkdev.h> 2866114cadSTejun Heo #include <linux/backing-dev.h> 29ac27a0ecSDave Kleikamp #include <linux/parser.h> 30ac27a0ecSDave Kleikamp #include <linux/buffer_head.h> 31a5694255SChristoph Hellwig #include <linux/exportfs.h> 32ac27a0ecSDave Kleikamp #include <linux/vfs.h> 33ac27a0ecSDave Kleikamp #include <linux/random.h> 34ac27a0ecSDave Kleikamp #include <linux/mount.h> 35ac27a0ecSDave Kleikamp #include <linux/namei.h> 36ac27a0ecSDave Kleikamp #include <linux/quotaops.h> 37ac27a0ecSDave Kleikamp #include <linux/seq_file.h> 383197ebdbSTheodore Ts'o #include <linux/ctype.h> 391330593eSVignesh Babu #include <linux/log2.h> 40717d50e4SAndreas Dilger #include <linux/crc16.h> 41ef510424SDan Williams #include <linux/dax.h> 427abc52c2SDan Magenheimer #include <linux/cleancache.h> 437c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 44ee73f9a5SJeff Layton #include <linux/iversion.h> 45c83ad55eSGabriel Krisman Bertazi #include <linux/unicode.h> 46c6a564ffSChristoph Hellwig #include <linux/part_stat.h> 47bfff6873SLukas Czerner #include <linux/kthread.h> 48bfff6873SLukas Czerner #include <linux/freezer.h> 49bfff6873SLukas Czerner 503dcf5451SChristoph Hellwig #include "ext4.h" 514a092d73STheodore Ts'o #include "ext4_extents.h" /* Needed for trace points definition */ 523dcf5451SChristoph Hellwig #include "ext4_jbd2.h" 53ac27a0ecSDave Kleikamp #include "xattr.h" 54ac27a0ecSDave Kleikamp #include "acl.h" 553661d286STheodore Ts'o #include "mballoc.h" 560c9ec4beSDarrick J. Wong #include "fsmap.h" 57ac27a0ecSDave Kleikamp 589bffad1eSTheodore Ts'o #define CREATE_TRACE_POINTS 599bffad1eSTheodore Ts'o #include <trace/events/ext4.h> 609bffad1eSTheodore Ts'o 610b75a840SLukas Czerner static struct ext4_lazy_init *ext4_li_info; 620b75a840SLukas Czerner static struct mutex ext4_li_mtx; 63e294a537STheodore Ts'o static struct ratelimit_state ext4_mount_msg_ratelimit; 649f6200bbSTheodore Ts'o 65617ba13bSMingming Cao static int ext4_load_journal(struct super_block *, struct ext4_super_block *, 66ac27a0ecSDave Kleikamp unsigned long journal_devnum); 672adf6da8STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root); 68e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync); 6911215630SJan Kara static int ext4_mark_recovery_complete(struct super_block *sb, 70617ba13bSMingming Cao struct ext4_super_block *es); 7111215630SJan Kara static int ext4_clear_journal_err(struct super_block *sb, 72617ba13bSMingming Cao struct ext4_super_block *es); 73617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait); 74617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data); 75617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf); 76c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb); 77c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb); 78152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags, 79152a0836SAl Viro const char *dev_name, void *data); 802035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb); 812035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb); 82d39195c3SAmir Goldstein static int ext4_feature_set_ok(struct super_block *sb, int readonly); 83bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void); 84bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb); 858f1f7453SEric Sandeen static void ext4_clear_request_list(void); 86c6cb7e77SEric Whitney static struct inode *ext4_get_journal_inode(struct super_block *sb, 87c6cb7e77SEric Whitney unsigned int journal_inum); 88ac27a0ecSDave Kleikamp 89e74031fdSJan Kara /* 90e74031fdSJan Kara * Lock ordering 91e74031fdSJan Kara * 92e74031fdSJan Kara * Note the difference between i_mmap_sem (EXT4_I(inode)->i_mmap_sem) and 93e74031fdSJan Kara * i_mmap_rwsem (inode->i_mmap_rwsem)! 94e74031fdSJan Kara * 95e74031fdSJan Kara * page fault path: 96c1e8d7c6SMichel Lespinasse * mmap_lock -> sb_start_pagefault -> i_mmap_sem (r) -> transaction start -> 97e74031fdSJan Kara * page lock -> i_data_sem (rw) 98e74031fdSJan Kara * 99e74031fdSJan Kara * buffered write path: 100c1e8d7c6SMichel Lespinasse * sb_start_write -> i_mutex -> mmap_lock 101e74031fdSJan Kara * sb_start_write -> i_mutex -> transaction start -> page lock -> 102e74031fdSJan Kara * i_data_sem (rw) 103e74031fdSJan Kara * 104e74031fdSJan Kara * truncate: 1051d39834fSNikolay Borisov * sb_start_write -> i_mutex -> i_mmap_sem (w) -> i_mmap_rwsem (w) -> page lock 1061d39834fSNikolay Borisov * sb_start_write -> i_mutex -> i_mmap_sem (w) -> transaction start -> 1071d39834fSNikolay Borisov * i_data_sem (rw) 108e74031fdSJan Kara * 109e74031fdSJan Kara * direct IO: 110c1e8d7c6SMichel Lespinasse * sb_start_write -> i_mutex -> mmap_lock 1111d39834fSNikolay Borisov * sb_start_write -> i_mutex -> transaction start -> i_data_sem (rw) 112e74031fdSJan Kara * 113e74031fdSJan Kara * writepages: 114e74031fdSJan Kara * transaction start -> page lock(s) -> i_data_sem (rw) 115e74031fdSJan Kara */ 116e74031fdSJan Kara 117c290ea01SJan Kara #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2) 1182035e776STheodore Ts'o static struct file_system_type ext2_fs_type = { 1192035e776STheodore Ts'o .owner = THIS_MODULE, 1202035e776STheodore Ts'o .name = "ext2", 1212035e776STheodore Ts'o .mount = ext4_mount, 1222035e776STheodore Ts'o .kill_sb = kill_block_super, 1232035e776STheodore Ts'o .fs_flags = FS_REQUIRES_DEV, 1242035e776STheodore Ts'o }; 1257f78e035SEric W. Biederman MODULE_ALIAS_FS("ext2"); 126fa7614ddSEric W. Biederman MODULE_ALIAS("ext2"); 1272035e776STheodore Ts'o #define IS_EXT2_SB(sb) ((sb)->s_bdev->bd_holder == &ext2_fs_type) 1282035e776STheodore Ts'o #else 1292035e776STheodore Ts'o #define IS_EXT2_SB(sb) (0) 1302035e776STheodore Ts'o #endif 1312035e776STheodore Ts'o 1322035e776STheodore Ts'o 133ba69f9abSJan Kara static struct file_system_type ext3_fs_type = { 134ba69f9abSJan Kara .owner = THIS_MODULE, 135ba69f9abSJan Kara .name = "ext3", 136152a0836SAl Viro .mount = ext4_mount, 137ba69f9abSJan Kara .kill_sb = kill_block_super, 138ba69f9abSJan Kara .fs_flags = FS_REQUIRES_DEV, 139ba69f9abSJan Kara }; 1407f78e035SEric W. Biederman MODULE_ALIAS_FS("ext3"); 141fa7614ddSEric W. Biederman MODULE_ALIAS("ext3"); 142ba69f9abSJan Kara #define IS_EXT3_SB(sb) ((sb)->s_bdev->bd_holder == &ext3_fs_type) 143bd81d8eeSLaurent Vivier 144fa491b14Szhangyi (F) 145fa491b14Szhangyi (F) static inline void __ext4_read_bh(struct buffer_head *bh, int op_flags, 146fa491b14Szhangyi (F) bh_end_io_t *end_io) 147fa491b14Szhangyi (F) { 148fa491b14Szhangyi (F) /* 149fa491b14Szhangyi (F) * buffer's verified bit is no longer valid after reading from 150fa491b14Szhangyi (F) * disk again due to write out error, clear it to make sure we 151fa491b14Szhangyi (F) * recheck the buffer contents. 152fa491b14Szhangyi (F) */ 153fa491b14Szhangyi (F) clear_buffer_verified(bh); 154fa491b14Szhangyi (F) 155fa491b14Szhangyi (F) bh->b_end_io = end_io ? end_io : end_buffer_read_sync; 156fa491b14Szhangyi (F) get_bh(bh); 157fa491b14Szhangyi (F) submit_bh(REQ_OP_READ, op_flags, bh); 158fa491b14Szhangyi (F) } 159fa491b14Szhangyi (F) 160fa491b14Szhangyi (F) void ext4_read_bh_nowait(struct buffer_head *bh, int op_flags, 161fa491b14Szhangyi (F) bh_end_io_t *end_io) 162fa491b14Szhangyi (F) { 163fa491b14Szhangyi (F) BUG_ON(!buffer_locked(bh)); 164fa491b14Szhangyi (F) 165fa491b14Szhangyi (F) if (ext4_buffer_uptodate(bh)) { 166fa491b14Szhangyi (F) unlock_buffer(bh); 167fa491b14Szhangyi (F) return; 168fa491b14Szhangyi (F) } 169fa491b14Szhangyi (F) __ext4_read_bh(bh, op_flags, end_io); 170fa491b14Szhangyi (F) } 171fa491b14Szhangyi (F) 172fa491b14Szhangyi (F) int ext4_read_bh(struct buffer_head *bh, int op_flags, bh_end_io_t *end_io) 173fa491b14Szhangyi (F) { 174fa491b14Szhangyi (F) BUG_ON(!buffer_locked(bh)); 175fa491b14Szhangyi (F) 176fa491b14Szhangyi (F) if (ext4_buffer_uptodate(bh)) { 177fa491b14Szhangyi (F) unlock_buffer(bh); 178fa491b14Szhangyi (F) return 0; 179fa491b14Szhangyi (F) } 180fa491b14Szhangyi (F) 181fa491b14Szhangyi (F) __ext4_read_bh(bh, op_flags, end_io); 182fa491b14Szhangyi (F) 183fa491b14Szhangyi (F) wait_on_buffer(bh); 184fa491b14Szhangyi (F) if (buffer_uptodate(bh)) 185fa491b14Szhangyi (F) return 0; 186fa491b14Szhangyi (F) return -EIO; 187fa491b14Szhangyi (F) } 188fa491b14Szhangyi (F) 189fa491b14Szhangyi (F) int ext4_read_bh_lock(struct buffer_head *bh, int op_flags, bool wait) 190fa491b14Szhangyi (F) { 191fa491b14Szhangyi (F) if (trylock_buffer(bh)) { 192fa491b14Szhangyi (F) if (wait) 193fa491b14Szhangyi (F) return ext4_read_bh(bh, op_flags, NULL); 194fa491b14Szhangyi (F) ext4_read_bh_nowait(bh, op_flags, NULL); 195fa491b14Szhangyi (F) return 0; 196fa491b14Szhangyi (F) } 197fa491b14Szhangyi (F) if (wait) { 198fa491b14Szhangyi (F) wait_on_buffer(bh); 199fa491b14Szhangyi (F) if (buffer_uptodate(bh)) 200fa491b14Szhangyi (F) return 0; 201fa491b14Szhangyi (F) return -EIO; 202fa491b14Szhangyi (F) } 203fa491b14Szhangyi (F) return 0; 204fa491b14Szhangyi (F) } 205fa491b14Szhangyi (F) 206fb265c9cSTheodore Ts'o /* 2078394a6abSzhangyi (F) * This works like __bread_gfp() except it uses ERR_PTR for error 208fb265c9cSTheodore Ts'o * returns. Currently with sb_bread it's impossible to distinguish 209fb265c9cSTheodore Ts'o * between ENOMEM and EIO situations (since both result in a NULL 210fb265c9cSTheodore Ts'o * return. 211fb265c9cSTheodore Ts'o */ 2128394a6abSzhangyi (F) static struct buffer_head *__ext4_sb_bread_gfp(struct super_block *sb, 2138394a6abSzhangyi (F) sector_t block, int op_flags, 2148394a6abSzhangyi (F) gfp_t gfp) 215fb265c9cSTheodore Ts'o { 2162d069c08Szhangyi (F) struct buffer_head *bh; 2172d069c08Szhangyi (F) int ret; 218fb265c9cSTheodore Ts'o 2198394a6abSzhangyi (F) bh = sb_getblk_gfp(sb, block, gfp); 220fb265c9cSTheodore Ts'o if (bh == NULL) 221fb265c9cSTheodore Ts'o return ERR_PTR(-ENOMEM); 222cf2834a5STheodore Ts'o if (ext4_buffer_uptodate(bh)) 223fb265c9cSTheodore Ts'o return bh; 2242d069c08Szhangyi (F) 2252d069c08Szhangyi (F) ret = ext4_read_bh_lock(bh, REQ_META | op_flags, true); 2262d069c08Szhangyi (F) if (ret) { 227fb265c9cSTheodore Ts'o put_bh(bh); 2282d069c08Szhangyi (F) return ERR_PTR(ret); 2292d069c08Szhangyi (F) } 2302d069c08Szhangyi (F) return bh; 231fb265c9cSTheodore Ts'o } 232fb265c9cSTheodore Ts'o 2338394a6abSzhangyi (F) struct buffer_head *ext4_sb_bread(struct super_block *sb, sector_t block, 2348394a6abSzhangyi (F) int op_flags) 2358394a6abSzhangyi (F) { 2368394a6abSzhangyi (F) return __ext4_sb_bread_gfp(sb, block, op_flags, __GFP_MOVABLE); 2378394a6abSzhangyi (F) } 2388394a6abSzhangyi (F) 2398394a6abSzhangyi (F) struct buffer_head *ext4_sb_bread_unmovable(struct super_block *sb, 2408394a6abSzhangyi (F) sector_t block) 2418394a6abSzhangyi (F) { 2428394a6abSzhangyi (F) return __ext4_sb_bread_gfp(sb, block, 0, 0); 2438394a6abSzhangyi (F) } 2448394a6abSzhangyi (F) 2455df1d412Szhangyi (F) void ext4_sb_breadahead_unmovable(struct super_block *sb, sector_t block) 2465df1d412Szhangyi (F) { 2475df1d412Szhangyi (F) struct buffer_head *bh = sb_getblk_gfp(sb, block, 0); 2485df1d412Szhangyi (F) 2495df1d412Szhangyi (F) if (likely(bh)) { 2505df1d412Szhangyi (F) ext4_read_bh_lock(bh, REQ_RAHEAD, false); 2515df1d412Szhangyi (F) brelse(bh); 2525df1d412Szhangyi (F) } 2535df1d412Szhangyi (F) } 2545df1d412Szhangyi (F) 255d25425f8SDarrick J. Wong static int ext4_verify_csum_type(struct super_block *sb, 256d25425f8SDarrick J. Wong struct ext4_super_block *es) 257d25425f8SDarrick J. Wong { 258e2b911c5SDarrick J. Wong if (!ext4_has_feature_metadata_csum(sb)) 259d25425f8SDarrick J. Wong return 1; 260d25425f8SDarrick J. Wong 261d25425f8SDarrick J. Wong return es->s_checksum_type == EXT4_CRC32C_CHKSUM; 262d25425f8SDarrick J. Wong } 263d25425f8SDarrick J. Wong 264a9c47317SDarrick J. Wong static __le32 ext4_superblock_csum(struct super_block *sb, 265a9c47317SDarrick J. Wong struct ext4_super_block *es) 266a9c47317SDarrick J. Wong { 267a9c47317SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(sb); 268a9c47317SDarrick J. Wong int offset = offsetof(struct ext4_super_block, s_checksum); 269a9c47317SDarrick J. Wong __u32 csum; 270a9c47317SDarrick J. Wong 271a9c47317SDarrick J. Wong csum = ext4_chksum(sbi, ~0, (char *)es, offset); 272a9c47317SDarrick J. Wong 273a9c47317SDarrick J. Wong return cpu_to_le32(csum); 274a9c47317SDarrick J. Wong } 275a9c47317SDarrick J. Wong 276c197855eSStephen Hemminger static int ext4_superblock_csum_verify(struct super_block *sb, 277a9c47317SDarrick J. Wong struct ext4_super_block *es) 278a9c47317SDarrick J. Wong { 2799aa5d32bSDmitry Monakhov if (!ext4_has_metadata_csum(sb)) 280a9c47317SDarrick J. Wong return 1; 281a9c47317SDarrick J. Wong 282a9c47317SDarrick J. Wong return es->s_checksum == ext4_superblock_csum(sb, es); 283a9c47317SDarrick J. Wong } 284a9c47317SDarrick J. Wong 28506db49e6STheodore Ts'o void ext4_superblock_csum_set(struct super_block *sb) 286a9c47317SDarrick J. Wong { 28706db49e6STheodore Ts'o struct ext4_super_block *es = EXT4_SB(sb)->s_es; 28806db49e6STheodore Ts'o 2899aa5d32bSDmitry Monakhov if (!ext4_has_metadata_csum(sb)) 290a9c47317SDarrick J. Wong return; 291a9c47317SDarrick J. Wong 292acaa5326SConstantine Sapuntzakis /* 293acaa5326SConstantine Sapuntzakis * Locking the superblock prevents the scenario 294acaa5326SConstantine Sapuntzakis * where: 295acaa5326SConstantine Sapuntzakis * 1) a first thread pauses during checksum calculation. 296acaa5326SConstantine Sapuntzakis * 2) a second thread updates the superblock, recalculates 297acaa5326SConstantine Sapuntzakis * the checksum, and updates s_checksum 298acaa5326SConstantine Sapuntzakis * 3) the first thread resumes and finishes its checksum calculation 299acaa5326SConstantine Sapuntzakis * and updates s_checksum with a potentially stale or torn value. 300acaa5326SConstantine Sapuntzakis */ 301acaa5326SConstantine Sapuntzakis lock_buffer(EXT4_SB(sb)->s_sbh); 302a9c47317SDarrick J. Wong es->s_checksum = ext4_superblock_csum(sb, es); 303acaa5326SConstantine Sapuntzakis unlock_buffer(EXT4_SB(sb)->s_sbh); 304a9c47317SDarrick J. Wong } 305a9c47317SDarrick J. Wong 3068fadc143SAlexandre Ratchov ext4_fsblk_t ext4_block_bitmap(struct super_block *sb, 3078fadc143SAlexandre Ratchov struct ext4_group_desc *bg) 308bd81d8eeSLaurent Vivier { 3093a14589cSAneesh Kumar K.V return le32_to_cpu(bg->bg_block_bitmap_lo) | 3108fadc143SAlexandre Ratchov (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? 3118fadc143SAlexandre Ratchov (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0); 312bd81d8eeSLaurent Vivier } 313bd81d8eeSLaurent Vivier 3148fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb, 3158fadc143SAlexandre Ratchov struct ext4_group_desc *bg) 316bd81d8eeSLaurent Vivier { 3175272f837SAneesh Kumar K.V return le32_to_cpu(bg->bg_inode_bitmap_lo) | 3188fadc143SAlexandre Ratchov (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? 3198fadc143SAlexandre Ratchov (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0); 320bd81d8eeSLaurent Vivier } 321bd81d8eeSLaurent Vivier 3228fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_table(struct super_block *sb, 3238fadc143SAlexandre Ratchov struct ext4_group_desc *bg) 324bd81d8eeSLaurent Vivier { 3255272f837SAneesh Kumar K.V return le32_to_cpu(bg->bg_inode_table_lo) | 3268fadc143SAlexandre Ratchov (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? 3278fadc143SAlexandre Ratchov (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0); 328bd81d8eeSLaurent Vivier } 329bd81d8eeSLaurent Vivier 330021b65bbSTheodore Ts'o __u32 ext4_free_group_clusters(struct super_block *sb, 331560671a0SAneesh Kumar K.V struct ext4_group_desc *bg) 332560671a0SAneesh Kumar K.V { 333560671a0SAneesh Kumar K.V return le16_to_cpu(bg->bg_free_blocks_count_lo) | 334560671a0SAneesh Kumar K.V (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? 335560671a0SAneesh Kumar K.V (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0); 336560671a0SAneesh Kumar K.V } 337560671a0SAneesh Kumar K.V 338560671a0SAneesh Kumar K.V __u32 ext4_free_inodes_count(struct super_block *sb, 339560671a0SAneesh Kumar K.V struct ext4_group_desc *bg) 340560671a0SAneesh Kumar K.V { 341560671a0SAneesh Kumar K.V return le16_to_cpu(bg->bg_free_inodes_count_lo) | 342560671a0SAneesh Kumar K.V (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? 343560671a0SAneesh Kumar K.V (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0); 344560671a0SAneesh Kumar K.V } 345560671a0SAneesh Kumar K.V 346560671a0SAneesh Kumar K.V __u32 ext4_used_dirs_count(struct super_block *sb, 347560671a0SAneesh Kumar K.V struct ext4_group_desc *bg) 348560671a0SAneesh Kumar K.V { 349560671a0SAneesh Kumar K.V return le16_to_cpu(bg->bg_used_dirs_count_lo) | 350560671a0SAneesh Kumar K.V (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? 351560671a0SAneesh Kumar K.V (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0); 352560671a0SAneesh Kumar K.V } 353560671a0SAneesh Kumar K.V 354560671a0SAneesh Kumar K.V __u32 ext4_itable_unused_count(struct super_block *sb, 355560671a0SAneesh Kumar K.V struct ext4_group_desc *bg) 356560671a0SAneesh Kumar K.V { 357560671a0SAneesh Kumar K.V return le16_to_cpu(bg->bg_itable_unused_lo) | 358560671a0SAneesh Kumar K.V (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? 359560671a0SAneesh Kumar K.V (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0); 360560671a0SAneesh Kumar K.V } 361560671a0SAneesh Kumar K.V 3628fadc143SAlexandre Ratchov void ext4_block_bitmap_set(struct super_block *sb, 3638fadc143SAlexandre Ratchov struct ext4_group_desc *bg, ext4_fsblk_t blk) 364bd81d8eeSLaurent Vivier { 3653a14589cSAneesh Kumar K.V bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk); 3668fadc143SAlexandre Ratchov if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) 3678fadc143SAlexandre Ratchov bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32); 368bd81d8eeSLaurent Vivier } 369bd81d8eeSLaurent Vivier 3708fadc143SAlexandre Ratchov void ext4_inode_bitmap_set(struct super_block *sb, 3718fadc143SAlexandre Ratchov struct ext4_group_desc *bg, ext4_fsblk_t blk) 372bd81d8eeSLaurent Vivier { 3735272f837SAneesh Kumar K.V bg->bg_inode_bitmap_lo = cpu_to_le32((u32)blk); 3748fadc143SAlexandre Ratchov if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) 3758fadc143SAlexandre Ratchov bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32); 376bd81d8eeSLaurent Vivier } 377bd81d8eeSLaurent Vivier 3788fadc143SAlexandre Ratchov void ext4_inode_table_set(struct super_block *sb, 3798fadc143SAlexandre Ratchov struct ext4_group_desc *bg, ext4_fsblk_t blk) 380bd81d8eeSLaurent Vivier { 3815272f837SAneesh Kumar K.V bg->bg_inode_table_lo = cpu_to_le32((u32)blk); 3828fadc143SAlexandre Ratchov if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) 3838fadc143SAlexandre Ratchov bg->bg_inode_table_hi = cpu_to_le32(blk >> 32); 384bd81d8eeSLaurent Vivier } 385bd81d8eeSLaurent Vivier 386021b65bbSTheodore Ts'o void ext4_free_group_clusters_set(struct super_block *sb, 387560671a0SAneesh Kumar K.V struct ext4_group_desc *bg, __u32 count) 388560671a0SAneesh Kumar K.V { 389560671a0SAneesh Kumar K.V bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count); 390560671a0SAneesh Kumar K.V if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) 391560671a0SAneesh Kumar K.V bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16); 392560671a0SAneesh Kumar K.V } 393560671a0SAneesh Kumar K.V 394560671a0SAneesh Kumar K.V void ext4_free_inodes_set(struct super_block *sb, 395560671a0SAneesh Kumar K.V struct ext4_group_desc *bg, __u32 count) 396560671a0SAneesh Kumar K.V { 397560671a0SAneesh Kumar K.V bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count); 398560671a0SAneesh Kumar K.V if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) 399560671a0SAneesh Kumar K.V bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16); 400560671a0SAneesh Kumar K.V } 401560671a0SAneesh Kumar K.V 402560671a0SAneesh Kumar K.V void ext4_used_dirs_set(struct super_block *sb, 403560671a0SAneesh Kumar K.V struct ext4_group_desc *bg, __u32 count) 404560671a0SAneesh Kumar K.V { 405560671a0SAneesh Kumar K.V bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count); 406560671a0SAneesh Kumar K.V if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) 407560671a0SAneesh Kumar K.V bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16); 408560671a0SAneesh Kumar K.V } 409560671a0SAneesh Kumar K.V 410560671a0SAneesh Kumar K.V void ext4_itable_unused_set(struct super_block *sb, 411560671a0SAneesh Kumar K.V struct ext4_group_desc *bg, __u32 count) 412560671a0SAneesh Kumar K.V { 413560671a0SAneesh Kumar K.V bg->bg_itable_unused_lo = cpu_to_le16((__u16)count); 414560671a0SAneesh Kumar K.V if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) 415560671a0SAneesh Kumar K.V bg->bg_itable_unused_hi = cpu_to_le16(count >> 16); 416560671a0SAneesh Kumar K.V } 417560671a0SAneesh Kumar K.V 4186a0678a7SArnd Bergmann static void __ext4_update_tstamp(__le32 *lo, __u8 *hi) 4196a0678a7SArnd Bergmann { 4206a0678a7SArnd Bergmann time64_t now = ktime_get_real_seconds(); 4216a0678a7SArnd Bergmann 4226a0678a7SArnd Bergmann now = clamp_val(now, 0, (1ull << 40) - 1); 4236a0678a7SArnd Bergmann 4246a0678a7SArnd Bergmann *lo = cpu_to_le32(lower_32_bits(now)); 4256a0678a7SArnd Bergmann *hi = upper_32_bits(now); 4266a0678a7SArnd Bergmann } 4276a0678a7SArnd Bergmann 4286a0678a7SArnd Bergmann static time64_t __ext4_get_tstamp(__le32 *lo, __u8 *hi) 4296a0678a7SArnd Bergmann { 4306a0678a7SArnd Bergmann return ((time64_t)(*hi) << 32) + le32_to_cpu(*lo); 4316a0678a7SArnd Bergmann } 4326a0678a7SArnd Bergmann #define ext4_update_tstamp(es, tstamp) \ 4336a0678a7SArnd Bergmann __ext4_update_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi) 4346a0678a7SArnd Bergmann #define ext4_get_tstamp(es, tstamp) \ 4356a0678a7SArnd Bergmann __ext4_get_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi) 436d3d1faf6SCurt Wohlgemuth 43754d3adbcSTheodore Ts'o static void __save_error_info(struct super_block *sb, int error, 43854d3adbcSTheodore Ts'o __u32 ino, __u64 block, 43954d3adbcSTheodore Ts'o const char *func, unsigned int line) 4401c13d5c0STheodore Ts'o { 4411c13d5c0STheodore Ts'o struct ext4_super_block *es = EXT4_SB(sb)->s_es; 44254d3adbcSTheodore Ts'o int err; 4431c13d5c0STheodore Ts'o 4441c13d5c0STheodore Ts'o EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS; 4451b46617bSTheodore Ts'o if (bdev_read_only(sb->s_bdev)) 4461b46617bSTheodore Ts'o return; 4471c13d5c0STheodore Ts'o es->s_state |= cpu_to_le16(EXT4_ERROR_FS); 4486a0678a7SArnd Bergmann ext4_update_tstamp(es, s_last_error_time); 4491c13d5c0STheodore Ts'o strncpy(es->s_last_error_func, func, sizeof(es->s_last_error_func)); 4501c13d5c0STheodore Ts'o es->s_last_error_line = cpu_to_le32(line); 45154d3adbcSTheodore Ts'o es->s_last_error_ino = cpu_to_le32(ino); 45254d3adbcSTheodore Ts'o es->s_last_error_block = cpu_to_le64(block); 45354d3adbcSTheodore Ts'o switch (error) { 45454d3adbcSTheodore Ts'o case EIO: 45554d3adbcSTheodore Ts'o err = EXT4_ERR_EIO; 45654d3adbcSTheodore Ts'o break; 45754d3adbcSTheodore Ts'o case ENOMEM: 45854d3adbcSTheodore Ts'o err = EXT4_ERR_ENOMEM; 45954d3adbcSTheodore Ts'o break; 46054d3adbcSTheodore Ts'o case EFSBADCRC: 46154d3adbcSTheodore Ts'o err = EXT4_ERR_EFSBADCRC; 46254d3adbcSTheodore Ts'o break; 46354d3adbcSTheodore Ts'o case 0: 46454d3adbcSTheodore Ts'o case EFSCORRUPTED: 46554d3adbcSTheodore Ts'o err = EXT4_ERR_EFSCORRUPTED; 46654d3adbcSTheodore Ts'o break; 46754d3adbcSTheodore Ts'o case ENOSPC: 46854d3adbcSTheodore Ts'o err = EXT4_ERR_ENOSPC; 46954d3adbcSTheodore Ts'o break; 47054d3adbcSTheodore Ts'o case ENOKEY: 47154d3adbcSTheodore Ts'o err = EXT4_ERR_ENOKEY; 47254d3adbcSTheodore Ts'o break; 47354d3adbcSTheodore Ts'o case EROFS: 47454d3adbcSTheodore Ts'o err = EXT4_ERR_EROFS; 47554d3adbcSTheodore Ts'o break; 47654d3adbcSTheodore Ts'o case EFBIG: 47754d3adbcSTheodore Ts'o err = EXT4_ERR_EFBIG; 47854d3adbcSTheodore Ts'o break; 47954d3adbcSTheodore Ts'o case EEXIST: 48054d3adbcSTheodore Ts'o err = EXT4_ERR_EEXIST; 48154d3adbcSTheodore Ts'o break; 48254d3adbcSTheodore Ts'o case ERANGE: 48354d3adbcSTheodore Ts'o err = EXT4_ERR_ERANGE; 48454d3adbcSTheodore Ts'o break; 48554d3adbcSTheodore Ts'o case EOVERFLOW: 48654d3adbcSTheodore Ts'o err = EXT4_ERR_EOVERFLOW; 48754d3adbcSTheodore Ts'o break; 48854d3adbcSTheodore Ts'o case EBUSY: 48954d3adbcSTheodore Ts'o err = EXT4_ERR_EBUSY; 49054d3adbcSTheodore Ts'o break; 49154d3adbcSTheodore Ts'o case ENOTDIR: 49254d3adbcSTheodore Ts'o err = EXT4_ERR_ENOTDIR; 49354d3adbcSTheodore Ts'o break; 49454d3adbcSTheodore Ts'o case ENOTEMPTY: 49554d3adbcSTheodore Ts'o err = EXT4_ERR_ENOTEMPTY; 49654d3adbcSTheodore Ts'o break; 49754d3adbcSTheodore Ts'o case ESHUTDOWN: 49854d3adbcSTheodore Ts'o err = EXT4_ERR_ESHUTDOWN; 49954d3adbcSTheodore Ts'o break; 50054d3adbcSTheodore Ts'o case EFAULT: 50154d3adbcSTheodore Ts'o err = EXT4_ERR_EFAULT; 50254d3adbcSTheodore Ts'o break; 50354d3adbcSTheodore Ts'o default: 50454d3adbcSTheodore Ts'o err = EXT4_ERR_UNKNOWN; 50554d3adbcSTheodore Ts'o } 50654d3adbcSTheodore Ts'o es->s_last_error_errcode = err; 5071c13d5c0STheodore Ts'o if (!es->s_first_error_time) { 5081c13d5c0STheodore Ts'o es->s_first_error_time = es->s_last_error_time; 5096a0678a7SArnd Bergmann es->s_first_error_time_hi = es->s_last_error_time_hi; 5101c13d5c0STheodore Ts'o strncpy(es->s_first_error_func, func, 5111c13d5c0STheodore Ts'o sizeof(es->s_first_error_func)); 5121c13d5c0STheodore Ts'o es->s_first_error_line = cpu_to_le32(line); 5131c13d5c0STheodore Ts'o es->s_first_error_ino = es->s_last_error_ino; 5141c13d5c0STheodore Ts'o es->s_first_error_block = es->s_last_error_block; 515878520acSTheodore Ts'o es->s_first_error_errcode = es->s_last_error_errcode; 5161c13d5c0STheodore Ts'o } 51766e61a9eSTheodore Ts'o /* 51866e61a9eSTheodore Ts'o * Start the daily error reporting function if it hasn't been 51966e61a9eSTheodore Ts'o * started already 52066e61a9eSTheodore Ts'o */ 52166e61a9eSTheodore Ts'o if (!es->s_error_count) 52266e61a9eSTheodore Ts'o mod_timer(&EXT4_SB(sb)->s_err_report, jiffies + 24*60*60*HZ); 523ba39ebb6SWei Yongjun le32_add_cpu(&es->s_error_count, 1); 5241c13d5c0STheodore Ts'o } 5251c13d5c0STheodore Ts'o 52654d3adbcSTheodore Ts'o static void save_error_info(struct super_block *sb, int error, 52754d3adbcSTheodore Ts'o __u32 ino, __u64 block, 52854d3adbcSTheodore Ts'o const char *func, unsigned int line) 5291c13d5c0STheodore Ts'o { 53054d3adbcSTheodore Ts'o __save_error_info(sb, error, ino, block, func, line); 531c96e2b85SEric Sandeen if (!bdev_read_only(sb->s_bdev)) 5321c13d5c0STheodore Ts'o ext4_commit_super(sb, 1); 5331c13d5c0STheodore Ts'o } 5341c13d5c0STheodore Ts'o 535bdfe0cbdSTheodore Ts'o /* 536bdfe0cbdSTheodore Ts'o * The del_gendisk() function uninitializes the disk-specific data 537bdfe0cbdSTheodore Ts'o * structures, including the bdi structure, without telling anyone 538bdfe0cbdSTheodore Ts'o * else. Once this happens, any attempt to call mark_buffer_dirty() 539bdfe0cbdSTheodore Ts'o * (for example, by ext4_commit_super), will cause a kernel OOPS. 540bdfe0cbdSTheodore Ts'o * This is a kludge to prevent these oops until we can put in a proper 541bdfe0cbdSTheodore Ts'o * hook in del_gendisk() to inform the VFS and file system layers. 542bdfe0cbdSTheodore Ts'o */ 543bdfe0cbdSTheodore Ts'o static int block_device_ejected(struct super_block *sb) 544bdfe0cbdSTheodore Ts'o { 545bdfe0cbdSTheodore Ts'o struct inode *bd_inode = sb->s_bdev->bd_inode; 546bdfe0cbdSTheodore Ts'o struct backing_dev_info *bdi = inode_to_bdi(bd_inode); 547bdfe0cbdSTheodore Ts'o 548bdfe0cbdSTheodore Ts'o return bdi->dev == NULL; 549bdfe0cbdSTheodore Ts'o } 550bdfe0cbdSTheodore Ts'o 55118aadd47SBobi Jam static void ext4_journal_commit_callback(journal_t *journal, transaction_t *txn) 55218aadd47SBobi Jam { 55318aadd47SBobi Jam struct super_block *sb = journal->j_private; 55418aadd47SBobi Jam struct ext4_sb_info *sbi = EXT4_SB(sb); 55518aadd47SBobi Jam int error = is_journal_aborted(journal); 5565d3ee208SDmitry Monakhov struct ext4_journal_cb_entry *jce; 55718aadd47SBobi Jam 5585d3ee208SDmitry Monakhov BUG_ON(txn->t_state == T_FINISHED); 559a0154344SDaeho Jeong 560a0154344SDaeho Jeong ext4_process_freed_data(sb, txn->t_tid); 561a0154344SDaeho Jeong 56218aadd47SBobi Jam spin_lock(&sbi->s_md_lock); 5635d3ee208SDmitry Monakhov while (!list_empty(&txn->t_private_list)) { 5645d3ee208SDmitry Monakhov jce = list_entry(txn->t_private_list.next, 5655d3ee208SDmitry Monakhov struct ext4_journal_cb_entry, jce_list); 56618aadd47SBobi Jam list_del_init(&jce->jce_list); 56718aadd47SBobi Jam spin_unlock(&sbi->s_md_lock); 56818aadd47SBobi Jam jce->jce_func(sb, jce, error); 56918aadd47SBobi Jam spin_lock(&sbi->s_md_lock); 57018aadd47SBobi Jam } 57118aadd47SBobi Jam spin_unlock(&sbi->s_md_lock); 57218aadd47SBobi Jam } 5731c13d5c0STheodore Ts'o 574afb585a9SMauricio Faria de Oliveira /* 575afb585a9SMauricio Faria de Oliveira * This writepage callback for write_cache_pages() 576afb585a9SMauricio Faria de Oliveira * takes care of a few cases after page cleaning. 577afb585a9SMauricio Faria de Oliveira * 578afb585a9SMauricio Faria de Oliveira * write_cache_pages() already checks for dirty pages 579afb585a9SMauricio Faria de Oliveira * and calls clear_page_dirty_for_io(), which we want, 580afb585a9SMauricio Faria de Oliveira * to write protect the pages. 581afb585a9SMauricio Faria de Oliveira * 582afb585a9SMauricio Faria de Oliveira * However, we may have to redirty a page (see below.) 583afb585a9SMauricio Faria de Oliveira */ 584afb585a9SMauricio Faria de Oliveira static int ext4_journalled_writepage_callback(struct page *page, 585afb585a9SMauricio Faria de Oliveira struct writeback_control *wbc, 586afb585a9SMauricio Faria de Oliveira void *data) 587afb585a9SMauricio Faria de Oliveira { 588afb585a9SMauricio Faria de Oliveira transaction_t *transaction = (transaction_t *) data; 589afb585a9SMauricio Faria de Oliveira struct buffer_head *bh, *head; 590afb585a9SMauricio Faria de Oliveira struct journal_head *jh; 591afb585a9SMauricio Faria de Oliveira 592afb585a9SMauricio Faria de Oliveira bh = head = page_buffers(page); 593afb585a9SMauricio Faria de Oliveira do { 594afb585a9SMauricio Faria de Oliveira /* 595afb585a9SMauricio Faria de Oliveira * We have to redirty a page in these cases: 596afb585a9SMauricio Faria de Oliveira * 1) If buffer is dirty, it means the page was dirty because it 597afb585a9SMauricio Faria de Oliveira * contains a buffer that needs checkpointing. So the dirty bit 598afb585a9SMauricio Faria de Oliveira * needs to be preserved so that checkpointing writes the buffer 599afb585a9SMauricio Faria de Oliveira * properly. 600afb585a9SMauricio Faria de Oliveira * 2) If buffer is not part of the committing transaction 601afb585a9SMauricio Faria de Oliveira * (we may have just accidentally come across this buffer because 602afb585a9SMauricio Faria de Oliveira * inode range tracking is not exact) or if the currently running 603afb585a9SMauricio Faria de Oliveira * transaction already contains this buffer as well, dirty bit 604afb585a9SMauricio Faria de Oliveira * needs to be preserved so that the buffer gets writeprotected 605afb585a9SMauricio Faria de Oliveira * properly on running transaction's commit. 606afb585a9SMauricio Faria de Oliveira */ 607afb585a9SMauricio Faria de Oliveira jh = bh2jh(bh); 608afb585a9SMauricio Faria de Oliveira if (buffer_dirty(bh) || 609afb585a9SMauricio Faria de Oliveira (jh && (jh->b_transaction != transaction || 610afb585a9SMauricio Faria de Oliveira jh->b_next_transaction))) { 611afb585a9SMauricio Faria de Oliveira redirty_page_for_writepage(wbc, page); 612afb585a9SMauricio Faria de Oliveira goto out; 613afb585a9SMauricio Faria de Oliveira } 614afb585a9SMauricio Faria de Oliveira } while ((bh = bh->b_this_page) != head); 615afb585a9SMauricio Faria de Oliveira 616afb585a9SMauricio Faria de Oliveira out: 617afb585a9SMauricio Faria de Oliveira return AOP_WRITEPAGE_ACTIVATE; 618afb585a9SMauricio Faria de Oliveira } 619afb585a9SMauricio Faria de Oliveira 620afb585a9SMauricio Faria de Oliveira static int ext4_journalled_submit_inode_data_buffers(struct jbd2_inode *jinode) 621afb585a9SMauricio Faria de Oliveira { 622afb585a9SMauricio Faria de Oliveira struct address_space *mapping = jinode->i_vfs_inode->i_mapping; 623afb585a9SMauricio Faria de Oliveira struct writeback_control wbc = { 624afb585a9SMauricio Faria de Oliveira .sync_mode = WB_SYNC_ALL, 625afb585a9SMauricio Faria de Oliveira .nr_to_write = LONG_MAX, 626afb585a9SMauricio Faria de Oliveira .range_start = jinode->i_dirty_start, 627afb585a9SMauricio Faria de Oliveira .range_end = jinode->i_dirty_end, 628afb585a9SMauricio Faria de Oliveira }; 629afb585a9SMauricio Faria de Oliveira 630afb585a9SMauricio Faria de Oliveira return write_cache_pages(mapping, &wbc, 631afb585a9SMauricio Faria de Oliveira ext4_journalled_writepage_callback, 632afb585a9SMauricio Faria de Oliveira jinode->i_transaction); 633afb585a9SMauricio Faria de Oliveira } 634afb585a9SMauricio Faria de Oliveira 635afb585a9SMauricio Faria de Oliveira static int ext4_journal_submit_inode_data_buffers(struct jbd2_inode *jinode) 636afb585a9SMauricio Faria de Oliveira { 637afb585a9SMauricio Faria de Oliveira int ret; 638afb585a9SMauricio Faria de Oliveira 639afb585a9SMauricio Faria de Oliveira if (ext4_should_journal_data(jinode->i_vfs_inode)) 640afb585a9SMauricio Faria de Oliveira ret = ext4_journalled_submit_inode_data_buffers(jinode); 641afb585a9SMauricio Faria de Oliveira else 642afb585a9SMauricio Faria de Oliveira ret = jbd2_journal_submit_inode_data_buffers(jinode); 643afb585a9SMauricio Faria de Oliveira 644afb585a9SMauricio Faria de Oliveira return ret; 645afb585a9SMauricio Faria de Oliveira } 646afb585a9SMauricio Faria de Oliveira 647afb585a9SMauricio Faria de Oliveira static int ext4_journal_finish_inode_data_buffers(struct jbd2_inode *jinode) 648afb585a9SMauricio Faria de Oliveira { 649afb585a9SMauricio Faria de Oliveira int ret = 0; 650afb585a9SMauricio Faria de Oliveira 651afb585a9SMauricio Faria de Oliveira if (!ext4_should_journal_data(jinode->i_vfs_inode)) 652afb585a9SMauricio Faria de Oliveira ret = jbd2_journal_finish_inode_data_buffers(jinode); 653afb585a9SMauricio Faria de Oliveira 654afb585a9SMauricio Faria de Oliveira return ret; 655afb585a9SMauricio Faria de Oliveira } 656afb585a9SMauricio Faria de Oliveira 6571dc1097fSJan Kara static bool system_going_down(void) 6581dc1097fSJan Kara { 6591dc1097fSJan Kara return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF 6601dc1097fSJan Kara || system_state == SYSTEM_RESTART; 6611dc1097fSJan Kara } 6621dc1097fSJan Kara 663ac27a0ecSDave Kleikamp /* Deal with the reporting of failure conditions on a filesystem such as 664ac27a0ecSDave Kleikamp * inconsistencies detected or read IO failures. 665ac27a0ecSDave Kleikamp * 666ac27a0ecSDave Kleikamp * On ext2, we can store the error state of the filesystem in the 667617ba13bSMingming Cao * superblock. That is not possible on ext4, because we may have other 668ac27a0ecSDave Kleikamp * write ordering constraints on the superblock which prevent us from 669ac27a0ecSDave Kleikamp * writing it out straight away; and given that the journal is about to 670ac27a0ecSDave Kleikamp * be aborted, we can't rely on the current, or future, transactions to 671ac27a0ecSDave Kleikamp * write out the superblock safely. 672ac27a0ecSDave Kleikamp * 673dab291afSMingming Cao * We'll just use the jbd2_journal_abort() error code to record an error in 674d6b198bcSThadeu Lima de Souza Cascardo * the journal instead. On recovery, the journal will complain about 675ac27a0ecSDave Kleikamp * that error until we've noted it down and cleared it. 676ac27a0ecSDave Kleikamp */ 677ac27a0ecSDave Kleikamp 678617ba13bSMingming Cao static void ext4_handle_error(struct super_block *sb) 679ac27a0ecSDave Kleikamp { 680327eaf73STheodore Ts'o if (test_opt(sb, WARN_ON_ERROR)) 681327eaf73STheodore Ts'o WARN_ON_ONCE(1); 682327eaf73STheodore Ts'o 683bc98a42cSDavid Howells if (sb_rdonly(sb)) 684ac27a0ecSDave Kleikamp return; 685ac27a0ecSDave Kleikamp 686ac27a0ecSDave Kleikamp if (!test_opt(sb, ERRORS_CONT)) { 687617ba13bSMingming Cao journal_t *journal = EXT4_SB(sb)->s_journal; 688ac27a0ecSDave Kleikamp 6894ab2f15bSTheodore Ts'o EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED; 690ac27a0ecSDave Kleikamp if (journal) 691dab291afSMingming Cao jbd2_journal_abort(journal, -EIO); 692ac27a0ecSDave Kleikamp } 6931dc1097fSJan Kara /* 6941dc1097fSJan Kara * We force ERRORS_RO behavior when system is rebooting. Otherwise we 6951dc1097fSJan Kara * could panic during 'reboot -f' as the underlying device got already 6961dc1097fSJan Kara * disabled. 6971dc1097fSJan Kara */ 6981dc1097fSJan Kara if (test_opt(sb, ERRORS_RO) || system_going_down()) { 699b31e1552SEric Sandeen ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only"); 7004418e141SDmitry Monakhov /* 7014418e141SDmitry Monakhov * Make sure updated value of ->s_mount_flags will be visible 7024418e141SDmitry Monakhov * before ->s_flags update 7034418e141SDmitry Monakhov */ 7044418e141SDmitry Monakhov smp_wmb(); 7051751e8a6SLinus Torvalds sb->s_flags |= SB_RDONLY; 7061dc1097fSJan Kara } else if (test_opt(sb, ERRORS_PANIC)) { 707617ba13bSMingming Cao panic("EXT4-fs (device %s): panic forced after error\n", 708ac27a0ecSDave Kleikamp sb->s_id); 709ac27a0ecSDave Kleikamp } 7104327ba52SDaeho Jeong } 711ac27a0ecSDave Kleikamp 712efbed4dcSTheodore Ts'o #define ext4_error_ratelimit(sb) \ 713efbed4dcSTheodore Ts'o ___ratelimit(&(EXT4_SB(sb)->s_err_ratelimit_state), \ 714efbed4dcSTheodore Ts'o "EXT4-fs error") 715efbed4dcSTheodore Ts'o 71612062dddSEric Sandeen void __ext4_error(struct super_block *sb, const char *function, 71754d3adbcSTheodore Ts'o unsigned int line, int error, __u64 block, 71854d3adbcSTheodore Ts'o const char *fmt, ...) 719ac27a0ecSDave Kleikamp { 7200ff2ea7dSJoe Perches struct va_format vaf; 721ac27a0ecSDave Kleikamp va_list args; 722ac27a0ecSDave Kleikamp 7230db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(sb)))) 7240db1ff22STheodore Ts'o return; 7250db1ff22STheodore Ts'o 726ccf0f32aSTheodore Ts'o trace_ext4_error(sb, function, line); 727efbed4dcSTheodore Ts'o if (ext4_error_ratelimit(sb)) { 728ac27a0ecSDave Kleikamp va_start(args, fmt); 7290ff2ea7dSJoe Perches vaf.fmt = fmt; 7300ff2ea7dSJoe Perches vaf.va = &args; 731efbed4dcSTheodore Ts'o printk(KERN_CRIT 732efbed4dcSTheodore Ts'o "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n", 7330ff2ea7dSJoe Perches sb->s_id, function, line, current->comm, &vaf); 734ac27a0ecSDave Kleikamp va_end(args); 735efbed4dcSTheodore Ts'o } 73654d3adbcSTheodore Ts'o save_error_info(sb, error, 0, block, function, line); 737617ba13bSMingming Cao ext4_handle_error(sb); 738ac27a0ecSDave Kleikamp } 739ac27a0ecSDave Kleikamp 740e7c96e8eSJoe Perches void __ext4_error_inode(struct inode *inode, const char *function, 74154d3adbcSTheodore Ts'o unsigned int line, ext4_fsblk_t block, int error, 742273df556SFrank Mayhar const char *fmt, ...) 743273df556SFrank Mayhar { 744273df556SFrank Mayhar va_list args; 745f7c21177STheodore Ts'o struct va_format vaf; 746273df556SFrank Mayhar 7470db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 7480db1ff22STheodore Ts'o return; 7490db1ff22STheodore Ts'o 750ccf0f32aSTheodore Ts'o trace_ext4_error(inode->i_sb, function, line); 751efbed4dcSTheodore Ts'o if (ext4_error_ratelimit(inode->i_sb)) { 752273df556SFrank Mayhar va_start(args, fmt); 753f7c21177STheodore Ts'o vaf.fmt = fmt; 754f7c21177STheodore Ts'o vaf.va = &args; 755c398eda0STheodore Ts'o if (block) 756d9ee81daSJoe Perches printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: " 757d9ee81daSJoe Perches "inode #%lu: block %llu: comm %s: %pV\n", 758d9ee81daSJoe Perches inode->i_sb->s_id, function, line, inode->i_ino, 759d9ee81daSJoe Perches block, current->comm, &vaf); 760d9ee81daSJoe Perches else 761d9ee81daSJoe Perches printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: " 762d9ee81daSJoe Perches "inode #%lu: comm %s: %pV\n", 763d9ee81daSJoe Perches inode->i_sb->s_id, function, line, inode->i_ino, 764d9ee81daSJoe Perches current->comm, &vaf); 765273df556SFrank Mayhar va_end(args); 766efbed4dcSTheodore Ts'o } 76754d3adbcSTheodore Ts'o save_error_info(inode->i_sb, error, inode->i_ino, block, 76854d3adbcSTheodore Ts'o function, line); 769273df556SFrank Mayhar ext4_handle_error(inode->i_sb); 770273df556SFrank Mayhar } 771273df556SFrank Mayhar 772e7c96e8eSJoe Perches void __ext4_error_file(struct file *file, const char *function, 773f7c21177STheodore Ts'o unsigned int line, ext4_fsblk_t block, 774f7c21177STheodore Ts'o const char *fmt, ...) 775273df556SFrank Mayhar { 776273df556SFrank Mayhar va_list args; 777f7c21177STheodore Ts'o struct va_format vaf; 778496ad9aaSAl Viro struct inode *inode = file_inode(file); 779273df556SFrank Mayhar char pathname[80], *path; 780273df556SFrank Mayhar 7810db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 7820db1ff22STheodore Ts'o return; 7830db1ff22STheodore Ts'o 784ccf0f32aSTheodore Ts'o trace_ext4_error(inode->i_sb, function, line); 785efbed4dcSTheodore Ts'o if (ext4_error_ratelimit(inode->i_sb)) { 7869bf39ab2SMiklos Szeredi path = file_path(file, pathname, sizeof(pathname)); 787f9a62d09SDan Carpenter if (IS_ERR(path)) 788273df556SFrank Mayhar path = "(unknown)"; 789f7c21177STheodore Ts'o va_start(args, fmt); 790f7c21177STheodore Ts'o vaf.fmt = fmt; 791f7c21177STheodore Ts'o vaf.va = &args; 792d9ee81daSJoe Perches if (block) 793d9ee81daSJoe Perches printk(KERN_CRIT 794d9ee81daSJoe Perches "EXT4-fs error (device %s): %s:%d: inode #%lu: " 795d9ee81daSJoe Perches "block %llu: comm %s: path %s: %pV\n", 796d9ee81daSJoe Perches inode->i_sb->s_id, function, line, inode->i_ino, 797d9ee81daSJoe Perches block, current->comm, path, &vaf); 798d9ee81daSJoe Perches else 799d9ee81daSJoe Perches printk(KERN_CRIT 800d9ee81daSJoe Perches "EXT4-fs error (device %s): %s:%d: inode #%lu: " 801d9ee81daSJoe Perches "comm %s: path %s: %pV\n", 802d9ee81daSJoe Perches inode->i_sb->s_id, function, line, inode->i_ino, 803d9ee81daSJoe Perches current->comm, path, &vaf); 804273df556SFrank Mayhar va_end(args); 805efbed4dcSTheodore Ts'o } 80654d3adbcSTheodore Ts'o save_error_info(inode->i_sb, EFSCORRUPTED, inode->i_ino, block, 80754d3adbcSTheodore Ts'o function, line); 808273df556SFrank Mayhar ext4_handle_error(inode->i_sb); 809273df556SFrank Mayhar } 810273df556SFrank Mayhar 811722887ddSTheodore Ts'o const char *ext4_decode_error(struct super_block *sb, int errno, 812ac27a0ecSDave Kleikamp char nbuf[16]) 813ac27a0ecSDave Kleikamp { 814ac27a0ecSDave Kleikamp char *errstr = NULL; 815ac27a0ecSDave Kleikamp 816ac27a0ecSDave Kleikamp switch (errno) { 8176a797d27SDarrick J. Wong case -EFSCORRUPTED: 8186a797d27SDarrick J. Wong errstr = "Corrupt filesystem"; 8196a797d27SDarrick J. Wong break; 8206a797d27SDarrick J. Wong case -EFSBADCRC: 8216a797d27SDarrick J. Wong errstr = "Filesystem failed CRC"; 8226a797d27SDarrick J. Wong break; 823ac27a0ecSDave Kleikamp case -EIO: 824ac27a0ecSDave Kleikamp errstr = "IO failure"; 825ac27a0ecSDave Kleikamp break; 826ac27a0ecSDave Kleikamp case -ENOMEM: 827ac27a0ecSDave Kleikamp errstr = "Out of memory"; 828ac27a0ecSDave Kleikamp break; 829ac27a0ecSDave Kleikamp case -EROFS: 83078f1ddbbSTheodore Ts'o if (!sb || (EXT4_SB(sb)->s_journal && 83178f1ddbbSTheodore Ts'o EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT)) 832ac27a0ecSDave Kleikamp errstr = "Journal has aborted"; 833ac27a0ecSDave Kleikamp else 834ac27a0ecSDave Kleikamp errstr = "Readonly filesystem"; 835ac27a0ecSDave Kleikamp break; 836ac27a0ecSDave Kleikamp default: 837ac27a0ecSDave Kleikamp /* If the caller passed in an extra buffer for unknown 838ac27a0ecSDave Kleikamp * errors, textualise them now. Else we just return 839ac27a0ecSDave Kleikamp * NULL. */ 840ac27a0ecSDave Kleikamp if (nbuf) { 841ac27a0ecSDave Kleikamp /* Check for truncated error codes... */ 842ac27a0ecSDave Kleikamp if (snprintf(nbuf, 16, "error %d", -errno) >= 0) 843ac27a0ecSDave Kleikamp errstr = nbuf; 844ac27a0ecSDave Kleikamp } 845ac27a0ecSDave Kleikamp break; 846ac27a0ecSDave Kleikamp } 847ac27a0ecSDave Kleikamp 848ac27a0ecSDave Kleikamp return errstr; 849ac27a0ecSDave Kleikamp } 850ac27a0ecSDave Kleikamp 851617ba13bSMingming Cao /* __ext4_std_error decodes expected errors from journaling functions 852ac27a0ecSDave Kleikamp * automatically and invokes the appropriate error response. */ 853ac27a0ecSDave Kleikamp 854c398eda0STheodore Ts'o void __ext4_std_error(struct super_block *sb, const char *function, 855c398eda0STheodore Ts'o unsigned int line, int errno) 856ac27a0ecSDave Kleikamp { 857ac27a0ecSDave Kleikamp char nbuf[16]; 858ac27a0ecSDave Kleikamp const char *errstr; 859ac27a0ecSDave Kleikamp 8600db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(sb)))) 8610db1ff22STheodore Ts'o return; 8620db1ff22STheodore Ts'o 863ac27a0ecSDave Kleikamp /* Special case: if the error is EROFS, and we're not already 864ac27a0ecSDave Kleikamp * inside a transaction, then there's really no point in logging 865ac27a0ecSDave Kleikamp * an error. */ 866bc98a42cSDavid Howells if (errno == -EROFS && journal_current_handle() == NULL && sb_rdonly(sb)) 867ac27a0ecSDave Kleikamp return; 868ac27a0ecSDave Kleikamp 869efbed4dcSTheodore Ts'o if (ext4_error_ratelimit(sb)) { 870617ba13bSMingming Cao errstr = ext4_decode_error(sb, errno, nbuf); 871c398eda0STheodore Ts'o printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n", 872c398eda0STheodore Ts'o sb->s_id, function, line, errstr); 873efbed4dcSTheodore Ts'o } 874ac27a0ecSDave Kleikamp 87554d3adbcSTheodore Ts'o save_error_info(sb, -errno, 0, 0, function, line); 876617ba13bSMingming Cao ext4_handle_error(sb); 877ac27a0ecSDave Kleikamp } 878ac27a0ecSDave Kleikamp 879ac27a0ecSDave Kleikamp /* 880617ba13bSMingming Cao * ext4_abort is a much stronger failure handler than ext4_error. The 881ac27a0ecSDave Kleikamp * abort function may be used to deal with unrecoverable failures such 882ac27a0ecSDave Kleikamp * as journal IO errors or ENOMEM at a critical moment in log management. 883ac27a0ecSDave Kleikamp * 884ac27a0ecSDave Kleikamp * We unconditionally force the filesystem into an ABORT|READONLY state, 885ac27a0ecSDave Kleikamp * unless the error response on the fs has been set to panic in which 886ac27a0ecSDave Kleikamp * case we take the easy way out and panic immediately. 887ac27a0ecSDave Kleikamp */ 888ac27a0ecSDave Kleikamp 889c67d859eSTheodore Ts'o void __ext4_abort(struct super_block *sb, const char *function, 89054d3adbcSTheodore Ts'o unsigned int line, int error, const char *fmt, ...) 891ac27a0ecSDave Kleikamp { 892651e1c3bSJoe Perches struct va_format vaf; 893ac27a0ecSDave Kleikamp va_list args; 894ac27a0ecSDave Kleikamp 8950db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(sb)))) 8960db1ff22STheodore Ts'o return; 8970db1ff22STheodore Ts'o 89854d3adbcSTheodore Ts'o save_error_info(sb, error, 0, 0, function, line); 899ac27a0ecSDave Kleikamp va_start(args, fmt); 900651e1c3bSJoe Perches vaf.fmt = fmt; 901651e1c3bSJoe Perches vaf.va = &args; 902651e1c3bSJoe Perches printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: %pV\n", 903651e1c3bSJoe Perches sb->s_id, function, line, &vaf); 904ac27a0ecSDave Kleikamp va_end(args); 905ac27a0ecSDave Kleikamp 906bc98a42cSDavid Howells if (sb_rdonly(sb) == 0) { 9074ab2f15bSTheodore Ts'o EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED; 9087b97d868Szhangyi (F) if (EXT4_SB(sb)->s_journal) 9097b97d868Szhangyi (F) jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO); 9107b97d868Szhangyi (F) 9117b97d868Szhangyi (F) ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only"); 9124418e141SDmitry Monakhov /* 9134418e141SDmitry Monakhov * Make sure updated value of ->s_mount_flags will be visible 9144418e141SDmitry Monakhov * before ->s_flags update 9154418e141SDmitry Monakhov */ 9164418e141SDmitry Monakhov smp_wmb(); 9171751e8a6SLinus Torvalds sb->s_flags |= SB_RDONLY; 9181c13d5c0STheodore Ts'o } 9197b97d868Szhangyi (F) if (test_opt(sb, ERRORS_PANIC) && !system_going_down()) 9201c13d5c0STheodore Ts'o panic("EXT4-fs panic from previous error\n"); 921ac27a0ecSDave Kleikamp } 922ac27a0ecSDave Kleikamp 923e7c96e8eSJoe Perches void __ext4_msg(struct super_block *sb, 924e7c96e8eSJoe Perches const char *prefix, const char *fmt, ...) 925b31e1552SEric Sandeen { 9260ff2ea7dSJoe Perches struct va_format vaf; 927b31e1552SEric Sandeen va_list args; 928b31e1552SEric Sandeen 9291cf006edSDmitry Monakhov atomic_inc(&EXT4_SB(sb)->s_msg_count); 930efbed4dcSTheodore Ts'o if (!___ratelimit(&(EXT4_SB(sb)->s_msg_ratelimit_state), "EXT4-fs")) 931efbed4dcSTheodore Ts'o return; 932efbed4dcSTheodore Ts'o 933b31e1552SEric Sandeen va_start(args, fmt); 9340ff2ea7dSJoe Perches vaf.fmt = fmt; 9350ff2ea7dSJoe Perches vaf.va = &args; 9360ff2ea7dSJoe Perches printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf); 937b31e1552SEric Sandeen va_end(args); 938b31e1552SEric Sandeen } 939b31e1552SEric Sandeen 9401cf006edSDmitry Monakhov static int ext4_warning_ratelimit(struct super_block *sb) 9411cf006edSDmitry Monakhov { 9421cf006edSDmitry Monakhov atomic_inc(&EXT4_SB(sb)->s_warning_count); 9431cf006edSDmitry Monakhov return ___ratelimit(&(EXT4_SB(sb)->s_warning_ratelimit_state), 9441cf006edSDmitry Monakhov "EXT4-fs warning"); 9451cf006edSDmitry Monakhov } 946b03a2f7eSAndreas Dilger 94712062dddSEric Sandeen void __ext4_warning(struct super_block *sb, const char *function, 948c398eda0STheodore Ts'o unsigned int line, const char *fmt, ...) 949ac27a0ecSDave Kleikamp { 9500ff2ea7dSJoe Perches struct va_format vaf; 951ac27a0ecSDave Kleikamp va_list args; 952ac27a0ecSDave Kleikamp 953b03a2f7eSAndreas Dilger if (!ext4_warning_ratelimit(sb)) 954efbed4dcSTheodore Ts'o return; 955efbed4dcSTheodore Ts'o 956ac27a0ecSDave Kleikamp va_start(args, fmt); 9570ff2ea7dSJoe Perches vaf.fmt = fmt; 9580ff2ea7dSJoe Perches vaf.va = &args; 9590ff2ea7dSJoe Perches printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n", 9600ff2ea7dSJoe Perches sb->s_id, function, line, &vaf); 961ac27a0ecSDave Kleikamp va_end(args); 962ac27a0ecSDave Kleikamp } 963ac27a0ecSDave Kleikamp 964b03a2f7eSAndreas Dilger void __ext4_warning_inode(const struct inode *inode, const char *function, 965b03a2f7eSAndreas Dilger unsigned int line, const char *fmt, ...) 966b03a2f7eSAndreas Dilger { 967b03a2f7eSAndreas Dilger struct va_format vaf; 968b03a2f7eSAndreas Dilger va_list args; 969b03a2f7eSAndreas Dilger 970b03a2f7eSAndreas Dilger if (!ext4_warning_ratelimit(inode->i_sb)) 971b03a2f7eSAndreas Dilger return; 972b03a2f7eSAndreas Dilger 973b03a2f7eSAndreas Dilger va_start(args, fmt); 974b03a2f7eSAndreas Dilger vaf.fmt = fmt; 975b03a2f7eSAndreas Dilger vaf.va = &args; 976b03a2f7eSAndreas Dilger printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: " 977b03a2f7eSAndreas Dilger "inode #%lu: comm %s: %pV\n", inode->i_sb->s_id, 978b03a2f7eSAndreas Dilger function, line, inode->i_ino, current->comm, &vaf); 979b03a2f7eSAndreas Dilger va_end(args); 980b03a2f7eSAndreas Dilger } 981b03a2f7eSAndreas Dilger 982e29136f8STheodore Ts'o void __ext4_grp_locked_error(const char *function, unsigned int line, 983e29136f8STheodore Ts'o struct super_block *sb, ext4_group_t grp, 984e29136f8STheodore Ts'o unsigned long ino, ext4_fsblk_t block, 985e29136f8STheodore Ts'o const char *fmt, ...) 9865d1b1b3fSAneesh Kumar K.V __releases(bitlock) 9875d1b1b3fSAneesh Kumar K.V __acquires(bitlock) 9885d1b1b3fSAneesh Kumar K.V { 9890ff2ea7dSJoe Perches struct va_format vaf; 9905d1b1b3fSAneesh Kumar K.V va_list args; 9915d1b1b3fSAneesh Kumar K.V 9920db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(sb)))) 9930db1ff22STheodore Ts'o return; 9940db1ff22STheodore Ts'o 995ccf0f32aSTheodore Ts'o trace_ext4_error(sb, function, line); 99654d3adbcSTheodore Ts'o __save_error_info(sb, EFSCORRUPTED, ino, block, function, line); 9970ff2ea7dSJoe Perches 998efbed4dcSTheodore Ts'o if (ext4_error_ratelimit(sb)) { 9995d1b1b3fSAneesh Kumar K.V va_start(args, fmt); 10000ff2ea7dSJoe Perches vaf.fmt = fmt; 10010ff2ea7dSJoe Perches vaf.va = &args; 100221149d61SRobin Dong printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ", 1003e29136f8STheodore Ts'o sb->s_id, function, line, grp); 1004e29136f8STheodore Ts'o if (ino) 10050ff2ea7dSJoe Perches printk(KERN_CONT "inode %lu: ", ino); 1006e29136f8STheodore Ts'o if (block) 1007efbed4dcSTheodore Ts'o printk(KERN_CONT "block %llu:", 1008efbed4dcSTheodore Ts'o (unsigned long long) block); 10090ff2ea7dSJoe Perches printk(KERN_CONT "%pV\n", &vaf); 10105d1b1b3fSAneesh Kumar K.V va_end(args); 1011efbed4dcSTheodore Ts'o } 10125d1b1b3fSAneesh Kumar K.V 1013327eaf73STheodore Ts'o if (test_opt(sb, WARN_ON_ERROR)) 1014327eaf73STheodore Ts'o WARN_ON_ONCE(1); 1015327eaf73STheodore Ts'o 10165d1b1b3fSAneesh Kumar K.V if (test_opt(sb, ERRORS_CONT)) { 1017e2d67052STheodore Ts'o ext4_commit_super(sb, 0); 10185d1b1b3fSAneesh Kumar K.V return; 10195d1b1b3fSAneesh Kumar K.V } 10201c13d5c0STheodore Ts'o 10215d1b1b3fSAneesh Kumar K.V ext4_unlock_group(sb, grp); 102206f29cc8SZhouyi Zhou ext4_commit_super(sb, 1); 10235d1b1b3fSAneesh Kumar K.V ext4_handle_error(sb); 10245d1b1b3fSAneesh Kumar K.V /* 10255d1b1b3fSAneesh Kumar K.V * We only get here in the ERRORS_RO case; relocking the group 10265d1b1b3fSAneesh Kumar K.V * may be dangerous, but nothing bad will happen since the 10275d1b1b3fSAneesh Kumar K.V * filesystem will have already been marked read/only and the 10285d1b1b3fSAneesh Kumar K.V * journal has been aborted. We return 1 as a hint to callers 10295d1b1b3fSAneesh Kumar K.V * who might what to use the return value from 103025985edcSLucas De Marchi * ext4_grp_locked_error() to distinguish between the 10315d1b1b3fSAneesh Kumar K.V * ERRORS_CONT and ERRORS_RO case, and perhaps return more 10325d1b1b3fSAneesh Kumar K.V * aggressively from the ext4 function in question, with a 10335d1b1b3fSAneesh Kumar K.V * more appropriate error code. 10345d1b1b3fSAneesh Kumar K.V */ 10355d1b1b3fSAneesh Kumar K.V ext4_lock_group(sb, grp); 10365d1b1b3fSAneesh Kumar K.V return; 10375d1b1b3fSAneesh Kumar K.V } 10385d1b1b3fSAneesh Kumar K.V 1039db79e6d1SWang Shilong void ext4_mark_group_bitmap_corrupted(struct super_block *sb, 1040db79e6d1SWang Shilong ext4_group_t group, 1041db79e6d1SWang Shilong unsigned int flags) 1042db79e6d1SWang Shilong { 1043db79e6d1SWang Shilong struct ext4_sb_info *sbi = EXT4_SB(sb); 1044db79e6d1SWang Shilong struct ext4_group_info *grp = ext4_get_group_info(sb, group); 1045db79e6d1SWang Shilong struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL); 10469af0b3d1SWang Shilong int ret; 1047db79e6d1SWang Shilong 10489af0b3d1SWang Shilong if (flags & EXT4_GROUP_INFO_BBITMAP_CORRUPT) { 10499af0b3d1SWang Shilong ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT, 10509af0b3d1SWang Shilong &grp->bb_state); 10519af0b3d1SWang Shilong if (!ret) 1052db79e6d1SWang Shilong percpu_counter_sub(&sbi->s_freeclusters_counter, 1053db79e6d1SWang Shilong grp->bb_free); 1054db79e6d1SWang Shilong } 1055db79e6d1SWang Shilong 10569af0b3d1SWang Shilong if (flags & EXT4_GROUP_INFO_IBITMAP_CORRUPT) { 10579af0b3d1SWang Shilong ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, 10589af0b3d1SWang Shilong &grp->bb_state); 10599af0b3d1SWang Shilong if (!ret && gdp) { 1060db79e6d1SWang Shilong int count; 1061db79e6d1SWang Shilong 1062db79e6d1SWang Shilong count = ext4_free_inodes_count(sb, gdp); 1063db79e6d1SWang Shilong percpu_counter_sub(&sbi->s_freeinodes_counter, 1064db79e6d1SWang Shilong count); 1065db79e6d1SWang Shilong } 1066db79e6d1SWang Shilong } 1067db79e6d1SWang Shilong } 1068db79e6d1SWang Shilong 1069617ba13bSMingming Cao void ext4_update_dynamic_rev(struct super_block *sb) 1070ac27a0ecSDave Kleikamp { 1071617ba13bSMingming Cao struct ext4_super_block *es = EXT4_SB(sb)->s_es; 1072ac27a0ecSDave Kleikamp 1073617ba13bSMingming Cao if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV) 1074ac27a0ecSDave Kleikamp return; 1075ac27a0ecSDave Kleikamp 107612062dddSEric Sandeen ext4_warning(sb, 1077ac27a0ecSDave Kleikamp "updating to rev %d because of new feature flag, " 1078ac27a0ecSDave Kleikamp "running e2fsck is recommended", 1079617ba13bSMingming Cao EXT4_DYNAMIC_REV); 1080ac27a0ecSDave Kleikamp 1081617ba13bSMingming Cao es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO); 1082617ba13bSMingming Cao es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE); 1083617ba13bSMingming Cao es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV); 1084ac27a0ecSDave Kleikamp /* leave es->s_feature_*compat flags alone */ 1085ac27a0ecSDave Kleikamp /* es->s_uuid will be set by e2fsck if empty */ 1086ac27a0ecSDave Kleikamp 1087ac27a0ecSDave Kleikamp /* 1088ac27a0ecSDave Kleikamp * The rest of the superblock fields should be zero, and if not it 1089ac27a0ecSDave Kleikamp * means they are likely already in use, so leave them alone. We 1090ac27a0ecSDave Kleikamp * can leave it up to e2fsck to clean up any inconsistencies there. 1091ac27a0ecSDave Kleikamp */ 1092ac27a0ecSDave Kleikamp } 1093ac27a0ecSDave Kleikamp 1094ac27a0ecSDave Kleikamp /* 1095ac27a0ecSDave Kleikamp * Open the external journal device 1096ac27a0ecSDave Kleikamp */ 1097b31e1552SEric Sandeen static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb) 1098ac27a0ecSDave Kleikamp { 1099ac27a0ecSDave Kleikamp struct block_device *bdev; 1100ac27a0ecSDave Kleikamp 1101d4d77629STejun Heo bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb); 1102ac27a0ecSDave Kleikamp if (IS_ERR(bdev)) 1103ac27a0ecSDave Kleikamp goto fail; 1104ac27a0ecSDave Kleikamp return bdev; 1105ac27a0ecSDave Kleikamp 1106ac27a0ecSDave Kleikamp fail: 1107ea3edd4dSChristoph Hellwig ext4_msg(sb, KERN_ERR, 1108ea3edd4dSChristoph Hellwig "failed to open journal device unknown-block(%u,%u) %ld", 1109ea3edd4dSChristoph Hellwig MAJOR(dev), MINOR(dev), PTR_ERR(bdev)); 1110ac27a0ecSDave Kleikamp return NULL; 1111ac27a0ecSDave Kleikamp } 1112ac27a0ecSDave Kleikamp 1113ac27a0ecSDave Kleikamp /* 1114ac27a0ecSDave Kleikamp * Release the journal device 1115ac27a0ecSDave Kleikamp */ 11164385bab1SAl Viro static void ext4_blkdev_put(struct block_device *bdev) 1117ac27a0ecSDave Kleikamp { 11184385bab1SAl Viro blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); 1119ac27a0ecSDave Kleikamp } 1120ac27a0ecSDave Kleikamp 11214385bab1SAl Viro static void ext4_blkdev_remove(struct ext4_sb_info *sbi) 1122ac27a0ecSDave Kleikamp { 1123ac27a0ecSDave Kleikamp struct block_device *bdev; 1124ee7ed3aaSChunguang Xu bdev = sbi->s_journal_bdev; 1125ac27a0ecSDave Kleikamp if (bdev) { 11264385bab1SAl Viro ext4_blkdev_put(bdev); 1127ee7ed3aaSChunguang Xu sbi->s_journal_bdev = NULL; 1128ac27a0ecSDave Kleikamp } 1129ac27a0ecSDave Kleikamp } 1130ac27a0ecSDave Kleikamp 1131ac27a0ecSDave Kleikamp static inline struct inode *orphan_list_entry(struct list_head *l) 1132ac27a0ecSDave Kleikamp { 1133617ba13bSMingming Cao return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode; 1134ac27a0ecSDave Kleikamp } 1135ac27a0ecSDave Kleikamp 1136617ba13bSMingming Cao static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi) 1137ac27a0ecSDave Kleikamp { 1138ac27a0ecSDave Kleikamp struct list_head *l; 1139ac27a0ecSDave Kleikamp 1140b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "sb orphan head is %d", 1141ac27a0ecSDave Kleikamp le32_to_cpu(sbi->s_es->s_last_orphan)); 1142ac27a0ecSDave Kleikamp 1143ac27a0ecSDave Kleikamp printk(KERN_ERR "sb_info orphan list:\n"); 1144ac27a0ecSDave Kleikamp list_for_each(l, &sbi->s_orphan) { 1145ac27a0ecSDave Kleikamp struct inode *inode = orphan_list_entry(l); 1146ac27a0ecSDave Kleikamp printk(KERN_ERR " " 1147ac27a0ecSDave Kleikamp "inode %s:%lu at %p: mode %o, nlink %d, next %d\n", 1148ac27a0ecSDave Kleikamp inode->i_sb->s_id, inode->i_ino, inode, 1149ac27a0ecSDave Kleikamp inode->i_mode, inode->i_nlink, 1150ac27a0ecSDave Kleikamp NEXT_ORPHAN(inode)); 1151ac27a0ecSDave Kleikamp } 1152ac27a0ecSDave Kleikamp } 1153ac27a0ecSDave Kleikamp 1154957153fcSJan Kara #ifdef CONFIG_QUOTA 1155957153fcSJan Kara static int ext4_quota_off(struct super_block *sb, int type); 1156957153fcSJan Kara 1157957153fcSJan Kara static inline void ext4_quota_off_umount(struct super_block *sb) 1158957153fcSJan Kara { 1159957153fcSJan Kara int type; 1160957153fcSJan Kara 1161957153fcSJan Kara /* Use our quota_off function to clear inode flags etc. */ 1162957153fcSJan Kara for (type = 0; type < EXT4_MAXQUOTAS; type++) 1163957153fcSJan Kara ext4_quota_off(sb, type); 1164957153fcSJan Kara } 116533458eabSTheodore Ts'o 116633458eabSTheodore Ts'o /* 116733458eabSTheodore Ts'o * This is a helper function which is used in the mount/remount 116833458eabSTheodore Ts'o * codepaths (which holds s_umount) to fetch the quota file name. 116933458eabSTheodore Ts'o */ 117033458eabSTheodore Ts'o static inline char *get_qf_name(struct super_block *sb, 117133458eabSTheodore Ts'o struct ext4_sb_info *sbi, 117233458eabSTheodore Ts'o int type) 117333458eabSTheodore Ts'o { 117433458eabSTheodore Ts'o return rcu_dereference_protected(sbi->s_qf_names[type], 117533458eabSTheodore Ts'o lockdep_is_held(&sb->s_umount)); 117633458eabSTheodore Ts'o } 1177957153fcSJan Kara #else 1178957153fcSJan Kara static inline void ext4_quota_off_umount(struct super_block *sb) 1179957153fcSJan Kara { 1180957153fcSJan Kara } 1181957153fcSJan Kara #endif 1182957153fcSJan Kara 1183617ba13bSMingming Cao static void ext4_put_super(struct super_block *sb) 1184ac27a0ecSDave Kleikamp { 1185617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 1186617ba13bSMingming Cao struct ext4_super_block *es = sbi->s_es; 11871d0c3924STheodore Ts'o struct buffer_head **group_desc; 11887c990728SSuraj Jitindar Singh struct flex_groups **flex_groups; 118997abd7d4STheodore Ts'o int aborted = 0; 1190ef2cabf7SHidehiro Kawai int i, err; 1191ac27a0ecSDave Kleikamp 1192857ac889SLukas Czerner ext4_unregister_li_request(sb); 1193957153fcSJan Kara ext4_quota_off_umount(sb); 1194e0ccfd95SChristoph Hellwig 11952e8fa54eSJan Kara destroy_workqueue(sbi->rsv_conversion_wq); 11964c0425ffSMingming Cao 11975e47868fSRitesh Harjani /* 11985e47868fSRitesh Harjani * Unregister sysfs before destroying jbd2 journal. 11995e47868fSRitesh Harjani * Since we could still access attr_journal_task attribute via sysfs 12005e47868fSRitesh Harjani * path which could have sbi->s_journal->j_task as NULL 12015e47868fSRitesh Harjani */ 12025e47868fSRitesh Harjani ext4_unregister_sysfs(sb); 12035e47868fSRitesh Harjani 12040390131bSFrank Mayhar if (sbi->s_journal) { 120597abd7d4STheodore Ts'o aborted = is_journal_aborted(sbi->s_journal); 1206ef2cabf7SHidehiro Kawai err = jbd2_journal_destroy(sbi->s_journal); 120747b4a50bSJan Kara sbi->s_journal = NULL; 1208878520acSTheodore Ts'o if ((err < 0) && !aborted) { 120954d3adbcSTheodore Ts'o ext4_abort(sb, -err, "Couldn't clean up the journal"); 12100390131bSFrank Mayhar } 1211878520acSTheodore Ts'o } 1212d4edac31SJosef Bacik 1213d3922a77SZheng Liu ext4_es_unregister_shrinker(sbi); 12149105bb14SAl Viro del_timer_sync(&sbi->s_err_report); 1215d4edac31SJosef Bacik ext4_release_system_zone(sb); 1216d4edac31SJosef Bacik ext4_mb_release(sb); 1217d4edac31SJosef Bacik ext4_ext_release(sb); 1218d4edac31SJosef Bacik 1219bc98a42cSDavid Howells if (!sb_rdonly(sb) && !aborted) { 1220e2b911c5SDarrick J. Wong ext4_clear_feature_journal_needs_recovery(sb); 1221ac27a0ecSDave Kleikamp es->s_state = cpu_to_le16(sbi->s_mount_state); 1222ac27a0ecSDave Kleikamp } 1223bc98a42cSDavid Howells if (!sb_rdonly(sb)) 1224a8e25a83SArtem Bityutskiy ext4_commit_super(sb, 1); 1225a8e25a83SArtem Bityutskiy 12261d0c3924STheodore Ts'o rcu_read_lock(); 12271d0c3924STheodore Ts'o group_desc = rcu_dereference(sbi->s_group_desc); 1228ac27a0ecSDave Kleikamp for (i = 0; i < sbi->s_gdb_count; i++) 12291d0c3924STheodore Ts'o brelse(group_desc[i]); 12301d0c3924STheodore Ts'o kvfree(group_desc); 12317c990728SSuraj Jitindar Singh flex_groups = rcu_dereference(sbi->s_flex_groups); 12327c990728SSuraj Jitindar Singh if (flex_groups) { 12337c990728SSuraj Jitindar Singh for (i = 0; i < sbi->s_flex_groups_allocated; i++) 12347c990728SSuraj Jitindar Singh kvfree(flex_groups[i]); 12357c990728SSuraj Jitindar Singh kvfree(flex_groups); 12367c990728SSuraj Jitindar Singh } 12371d0c3924STheodore Ts'o rcu_read_unlock(); 123857042651STheodore Ts'o percpu_counter_destroy(&sbi->s_freeclusters_counter); 1239ac27a0ecSDave Kleikamp percpu_counter_destroy(&sbi->s_freeinodes_counter); 1240ac27a0ecSDave Kleikamp percpu_counter_destroy(&sbi->s_dirs_counter); 124157042651STheodore Ts'o percpu_counter_destroy(&sbi->s_dirtyclusters_counter); 1242bbd55937SEric Biggers percpu_free_rwsem(&sbi->s_writepages_rwsem); 1243ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 1244a2d4a646SJan Kara for (i = 0; i < EXT4_MAXQUOTAS; i++) 124533458eabSTheodore Ts'o kfree(get_qf_name(sb, sbi, i)); 1246ac27a0ecSDave Kleikamp #endif 1247ac27a0ecSDave Kleikamp 1248ac27a0ecSDave Kleikamp /* Debugging code just in case the in-memory inode orphan list 1249ac27a0ecSDave Kleikamp * isn't empty. The on-disk one can be non-empty if we've 1250ac27a0ecSDave Kleikamp * detected an error and taken the fs readonly, but the 1251ac27a0ecSDave Kleikamp * in-memory list had better be clean by this point. */ 1252ac27a0ecSDave Kleikamp if (!list_empty(&sbi->s_orphan)) 1253ac27a0ecSDave Kleikamp dump_orphan_list(sb, sbi); 1254ac27a0ecSDave Kleikamp J_ASSERT(list_empty(&sbi->s_orphan)); 1255ac27a0ecSDave Kleikamp 125689d96a6fSTheodore Ts'o sync_blockdev(sb->s_bdev); 1257f98393a6SPeter Zijlstra invalidate_bdev(sb->s_bdev); 1258ee7ed3aaSChunguang Xu if (sbi->s_journal_bdev && sbi->s_journal_bdev != sb->s_bdev) { 1259ac27a0ecSDave Kleikamp /* 1260ac27a0ecSDave Kleikamp * Invalidate the journal device's buffers. We don't want them 1261ac27a0ecSDave Kleikamp * floating about in memory - the physical journal device may 1262ac27a0ecSDave Kleikamp * hotswapped, and it breaks the `ro-after' testing code. 1263ac27a0ecSDave Kleikamp */ 1264ee7ed3aaSChunguang Xu sync_blockdev(sbi->s_journal_bdev); 1265ee7ed3aaSChunguang Xu invalidate_bdev(sbi->s_journal_bdev); 1266617ba13bSMingming Cao ext4_blkdev_remove(sbi); 1267ac27a0ecSDave Kleikamp } 126850c15df6SChengguang Xu 1269dec214d0STahsin Erdogan ext4_xattr_destroy_cache(sbi->s_ea_inode_cache); 1270dec214d0STahsin Erdogan sbi->s_ea_inode_cache = NULL; 127150c15df6SChengguang Xu 127247387409STahsin Erdogan ext4_xattr_destroy_cache(sbi->s_ea_block_cache); 127347387409STahsin Erdogan sbi->s_ea_block_cache = NULL; 127450c15df6SChengguang Xu 1275c5e06d10SJohann Lombardi if (sbi->s_mmp_tsk) 1276c5e06d10SJohann Lombardi kthread_stop(sbi->s_mmp_tsk); 12779060dd2cSEric Sandeen brelse(sbi->s_sbh); 1278ac27a0ecSDave Kleikamp sb->s_fs_info = NULL; 12793197ebdbSTheodore Ts'o /* 12803197ebdbSTheodore Ts'o * Now that we are completely done shutting down the 12813197ebdbSTheodore Ts'o * superblock, we need to actually destroy the kobject. 12823197ebdbSTheodore Ts'o */ 12833197ebdbSTheodore Ts'o kobject_put(&sbi->s_kobj); 12843197ebdbSTheodore Ts'o wait_for_completion(&sbi->s_kobj_unregister); 12850441984aSDarrick J. Wong if (sbi->s_chksum_driver) 12860441984aSDarrick J. Wong crypto_free_shash(sbi->s_chksum_driver); 1287705895b6SPekka Enberg kfree(sbi->s_blockgroup_lock); 12885e405595SDan Williams fs_put_dax(sbi->s_daxdev); 1289ed318a6cSEric Biggers fscrypt_free_dummy_context(&sbi->s_dummy_enc_ctx); 1290c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE 1291c83ad55eSGabriel Krisman Bertazi utf8_unload(sbi->s_encoding); 1292c83ad55eSGabriel Krisman Bertazi #endif 1293ac27a0ecSDave Kleikamp kfree(sbi); 1294ac27a0ecSDave Kleikamp } 1295ac27a0ecSDave Kleikamp 1296e18b890bSChristoph Lameter static struct kmem_cache *ext4_inode_cachep; 1297ac27a0ecSDave Kleikamp 1298ac27a0ecSDave Kleikamp /* 1299ac27a0ecSDave Kleikamp * Called inside transaction, so use GFP_NOFS 1300ac27a0ecSDave Kleikamp */ 1301617ba13bSMingming Cao static struct inode *ext4_alloc_inode(struct super_block *sb) 1302ac27a0ecSDave Kleikamp { 1303617ba13bSMingming Cao struct ext4_inode_info *ei; 1304ac27a0ecSDave Kleikamp 1305e6b4f8daSChristoph Lameter ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS); 1306ac27a0ecSDave Kleikamp if (!ei) 1307ac27a0ecSDave Kleikamp return NULL; 13080b8e58a1SAndreas Dilger 1309ee73f9a5SJeff Layton inode_set_iversion(&ei->vfs_inode, 1); 1310202ee5dfSTheodore Ts'o spin_lock_init(&ei->i_raw_lock); 1311c9de560dSAlex Tomas INIT_LIST_HEAD(&ei->i_prealloc_list); 131227bc446eSbrookxu atomic_set(&ei->i_prealloc_active, 0); 1313c9de560dSAlex Tomas spin_lock_init(&ei->i_prealloc_lock); 13149a26b661SZheng Liu ext4_es_init_tree(&ei->i_es_tree); 13159a26b661SZheng Liu rwlock_init(&ei->i_es_lock); 1316edaa53caSZheng Liu INIT_LIST_HEAD(&ei->i_es_list); 1317eb68d0e2SZheng Liu ei->i_es_all_nr = 0; 1318edaa53caSZheng Liu ei->i_es_shk_nr = 0; 1319dd475925SJan Kara ei->i_es_shrink_lblk = 0; 1320d2a17637SMingming Cao ei->i_reserved_data_blocks = 0; 1321d2a17637SMingming Cao spin_lock_init(&(ei->i_block_reservation_lock)); 13221dc0aa46SEric Whitney ext4_init_pending_tree(&ei->i_pending_tree); 1323a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 1324a9e7f447SDmitry Monakhov ei->i_reserved_quota = 0; 132596c7e0d9SJan Kara memset(&ei->i_dquot, 0, sizeof(ei->i_dquot)); 1326a9e7f447SDmitry Monakhov #endif 13278aefcd55STheodore Ts'o ei->jinode = NULL; 13282e8fa54eSJan Kara INIT_LIST_HEAD(&ei->i_rsv_conversion_list); 1329744692dcSJiaying Zhang spin_lock_init(&ei->i_completed_io_lock); 1330b436b9beSJan Kara ei->i_sync_tid = 0; 1331b436b9beSJan Kara ei->i_datasync_tid = 0; 1332e27f41e1SDmitry Monakhov atomic_set(&ei->i_unwritten, 0); 13332e8fa54eSJan Kara INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work); 1334*aa75f4d3SHarshad Shirwadkar ext4_fc_init_inode(&ei->vfs_inode); 1335*aa75f4d3SHarshad Shirwadkar mutex_init(&ei->i_fc_lock); 1336ac27a0ecSDave Kleikamp return &ei->vfs_inode; 1337ac27a0ecSDave Kleikamp } 1338ac27a0ecSDave Kleikamp 13397ff9c073STheodore Ts'o static int ext4_drop_inode(struct inode *inode) 13407ff9c073STheodore Ts'o { 13417ff9c073STheodore Ts'o int drop = generic_drop_inode(inode); 13427ff9c073STheodore Ts'o 134329b3692eSEric Biggers if (!drop) 134429b3692eSEric Biggers drop = fscrypt_drop_inode(inode); 134529b3692eSEric Biggers 13467ff9c073STheodore Ts'o trace_ext4_drop_inode(inode, drop); 13477ff9c073STheodore Ts'o return drop; 13487ff9c073STheodore Ts'o } 13497ff9c073STheodore Ts'o 135094053139SAl Viro static void ext4_free_in_core_inode(struct inode *inode) 1351fa0d7e3dSNick Piggin { 13522c58d548SEric Biggers fscrypt_free_inode(inode); 1353*aa75f4d3SHarshad Shirwadkar if (!list_empty(&(EXT4_I(inode)->i_fc_list))) { 1354*aa75f4d3SHarshad Shirwadkar pr_warn("%s: inode %ld still in fc list", 1355*aa75f4d3SHarshad Shirwadkar __func__, inode->i_ino); 1356*aa75f4d3SHarshad Shirwadkar } 1357fa0d7e3dSNick Piggin kmem_cache_free(ext4_inode_cachep, EXT4_I(inode)); 1358fa0d7e3dSNick Piggin } 1359fa0d7e3dSNick Piggin 1360617ba13bSMingming Cao static void ext4_destroy_inode(struct inode *inode) 1361ac27a0ecSDave Kleikamp { 13629f7dd93dSVasily Averin if (!list_empty(&(EXT4_I(inode)->i_orphan))) { 1363b31e1552SEric Sandeen ext4_msg(inode->i_sb, KERN_ERR, 1364b31e1552SEric Sandeen "Inode %lu (%p): orphan list check failed!", 1365b31e1552SEric Sandeen inode->i_ino, EXT4_I(inode)); 13669f7dd93dSVasily Averin print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4, 13679f7dd93dSVasily Averin EXT4_I(inode), sizeof(struct ext4_inode_info), 13689f7dd93dSVasily Averin true); 13699f7dd93dSVasily Averin dump_stack(); 13709f7dd93dSVasily Averin } 1371ac27a0ecSDave Kleikamp } 1372ac27a0ecSDave Kleikamp 137351cc5068SAlexey Dobriyan static void init_once(void *foo) 1374ac27a0ecSDave Kleikamp { 1375617ba13bSMingming Cao struct ext4_inode_info *ei = (struct ext4_inode_info *) foo; 1376ac27a0ecSDave Kleikamp 1377ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&ei->i_orphan); 1378ac27a0ecSDave Kleikamp init_rwsem(&ei->xattr_sem); 13790e855ac8SAneesh Kumar K.V init_rwsem(&ei->i_data_sem); 1380ea3d7209SJan Kara init_rwsem(&ei->i_mmap_sem); 1381ac27a0ecSDave Kleikamp inode_init_once(&ei->vfs_inode); 1382*aa75f4d3SHarshad Shirwadkar ext4_fc_init_inode(&ei->vfs_inode); 1383ac27a0ecSDave Kleikamp } 1384ac27a0ecSDave Kleikamp 1385e67bc2b3SFabian Frederick static int __init init_inodecache(void) 1386ac27a0ecSDave Kleikamp { 1387f8dd7c70SDavid Windsor ext4_inode_cachep = kmem_cache_create_usercopy("ext4_inode_cache", 1388f8dd7c70SDavid Windsor sizeof(struct ext4_inode_info), 0, 1389f8dd7c70SDavid Windsor (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD| 1390f8dd7c70SDavid Windsor SLAB_ACCOUNT), 1391f8dd7c70SDavid Windsor offsetof(struct ext4_inode_info, i_data), 1392f8dd7c70SDavid Windsor sizeof_field(struct ext4_inode_info, i_data), 139320c2df83SPaul Mundt init_once); 1394617ba13bSMingming Cao if (ext4_inode_cachep == NULL) 1395ac27a0ecSDave Kleikamp return -ENOMEM; 1396ac27a0ecSDave Kleikamp return 0; 1397ac27a0ecSDave Kleikamp } 1398ac27a0ecSDave Kleikamp 1399ac27a0ecSDave Kleikamp static void destroy_inodecache(void) 1400ac27a0ecSDave Kleikamp { 14018c0a8537SKirill A. Shutemov /* 14028c0a8537SKirill A. Shutemov * Make sure all delayed rcu free inodes are flushed before we 14038c0a8537SKirill A. Shutemov * destroy cache. 14048c0a8537SKirill A. Shutemov */ 14058c0a8537SKirill A. Shutemov rcu_barrier(); 1406617ba13bSMingming Cao kmem_cache_destroy(ext4_inode_cachep); 1407ac27a0ecSDave Kleikamp } 1408ac27a0ecSDave Kleikamp 14090930fcc1SAl Viro void ext4_clear_inode(struct inode *inode) 1410ac27a0ecSDave Kleikamp { 1411*aa75f4d3SHarshad Shirwadkar ext4_fc_del(inode); 14120930fcc1SAl Viro invalidate_inode_buffers(inode); 1413dbd5768fSJan Kara clear_inode(inode); 141427bc446eSbrookxu ext4_discard_preallocations(inode, 0); 141551865fdaSZheng Liu ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS); 1416f4c2d372SJan Kara dquot_drop(inode); 14178aefcd55STheodore Ts'o if (EXT4_I(inode)->jinode) { 14188aefcd55STheodore Ts'o jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode), 14198aefcd55STheodore Ts'o EXT4_I(inode)->jinode); 14208aefcd55STheodore Ts'o jbd2_free_inode(EXT4_I(inode)->jinode); 14218aefcd55STheodore Ts'o EXT4_I(inode)->jinode = NULL; 14228aefcd55STheodore Ts'o } 14233d204e24SEric Biggers fscrypt_put_encryption_info(inode); 1424c93d8f88SEric Biggers fsverity_cleanup_inode(inode); 1425ac27a0ecSDave Kleikamp } 1426ac27a0ecSDave Kleikamp 14271b961ac0SChristoph Hellwig static struct inode *ext4_nfs_get_inode(struct super_block *sb, 14281b961ac0SChristoph Hellwig u64 ino, u32 generation) 1429ac27a0ecSDave Kleikamp { 1430ac27a0ecSDave Kleikamp struct inode *inode; 1431ac27a0ecSDave Kleikamp 14328a363970STheodore Ts'o /* 1433ac27a0ecSDave Kleikamp * Currently we don't know the generation for parent directory, so 1434ac27a0ecSDave Kleikamp * a generation of 0 means "accept any" 1435ac27a0ecSDave Kleikamp */ 14368a363970STheodore Ts'o inode = ext4_iget(sb, ino, EXT4_IGET_HANDLE); 14371d1fe1eeSDavid Howells if (IS_ERR(inode)) 14381d1fe1eeSDavid Howells return ERR_CAST(inode); 14391d1fe1eeSDavid Howells if (generation && inode->i_generation != generation) { 1440ac27a0ecSDave Kleikamp iput(inode); 1441ac27a0ecSDave Kleikamp return ERR_PTR(-ESTALE); 1442ac27a0ecSDave Kleikamp } 14431b961ac0SChristoph Hellwig 14441b961ac0SChristoph Hellwig return inode; 1445ac27a0ecSDave Kleikamp } 14461b961ac0SChristoph Hellwig 14471b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid, 14481b961ac0SChristoph Hellwig int fh_len, int fh_type) 14491b961ac0SChristoph Hellwig { 14501b961ac0SChristoph Hellwig return generic_fh_to_dentry(sb, fid, fh_len, fh_type, 14511b961ac0SChristoph Hellwig ext4_nfs_get_inode); 14521b961ac0SChristoph Hellwig } 14531b961ac0SChristoph Hellwig 14541b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid, 14551b961ac0SChristoph Hellwig int fh_len, int fh_type) 14561b961ac0SChristoph Hellwig { 14571b961ac0SChristoph Hellwig return generic_fh_to_parent(sb, fid, fh_len, fh_type, 14581b961ac0SChristoph Hellwig ext4_nfs_get_inode); 1459ac27a0ecSDave Kleikamp } 1460ac27a0ecSDave Kleikamp 1461fde87268STheodore Ts'o static int ext4_nfs_commit_metadata(struct inode *inode) 1462fde87268STheodore Ts'o { 1463fde87268STheodore Ts'o struct writeback_control wbc = { 1464fde87268STheodore Ts'o .sync_mode = WB_SYNC_ALL 1465fde87268STheodore Ts'o }; 1466fde87268STheodore Ts'o 1467fde87268STheodore Ts'o trace_ext4_nfs_commit_metadata(inode); 1468fde87268STheodore Ts'o return ext4_write_inode(inode, &wbc); 1469fde87268STheodore Ts'o } 1470fde87268STheodore Ts'o 1471c39a7f84SToshiyuki Okajima /* 1472c39a7f84SToshiyuki Okajima * Try to release metadata pages (indirect blocks, directories) which are 1473c39a7f84SToshiyuki Okajima * mapped via the block device. Since these pages could have journal heads 1474c39a7f84SToshiyuki Okajima * which would prevent try_to_free_buffers() from freeing them, we must use 1475c39a7f84SToshiyuki Okajima * jbd2 layer's try_to_free_buffers() function to release them. 1476c39a7f84SToshiyuki Okajima */ 14770b8e58a1SAndreas Dilger static int bdev_try_to_free_page(struct super_block *sb, struct page *page, 14780b8e58a1SAndreas Dilger gfp_t wait) 1479c39a7f84SToshiyuki Okajima { 1480c39a7f84SToshiyuki Okajima journal_t *journal = EXT4_SB(sb)->s_journal; 1481c39a7f84SToshiyuki Okajima 1482c39a7f84SToshiyuki Okajima WARN_ON(PageChecked(page)); 1483c39a7f84SToshiyuki Okajima if (!page_has_buffers(page)) 1484c39a7f84SToshiyuki Okajima return 0; 1485c39a7f84SToshiyuki Okajima if (journal) 1486529a781eSzhangyi (F) return jbd2_journal_try_to_free_buffers(journal, page); 1487529a781eSzhangyi (F) 1488c39a7f84SToshiyuki Okajima return try_to_free_buffers(page); 1489c39a7f84SToshiyuki Okajima } 1490c39a7f84SToshiyuki Okajima 1491643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 1492a7550b30SJaegeuk Kim static int ext4_get_context(struct inode *inode, void *ctx, size_t len) 1493a7550b30SJaegeuk Kim { 1494a7550b30SJaegeuk Kim return ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION, 1495a7550b30SJaegeuk Kim EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, ctx, len); 1496a7550b30SJaegeuk Kim } 1497a7550b30SJaegeuk Kim 1498a7550b30SJaegeuk Kim static int ext4_set_context(struct inode *inode, const void *ctx, size_t len, 1499a7550b30SJaegeuk Kim void *fs_data) 1500a7550b30SJaegeuk Kim { 15012f8f5e76SEric Biggers handle_t *handle = fs_data; 1502c1a5d5f6STahsin Erdogan int res, res2, credits, retries = 0; 1503a7550b30SJaegeuk Kim 15049ce0151aSEric Biggers /* 15059ce0151aSEric Biggers * Encrypting the root directory is not allowed because e2fsck expects 15069ce0151aSEric Biggers * lost+found to exist and be unencrypted, and encrypting the root 15079ce0151aSEric Biggers * directory would imply encrypting the lost+found directory as well as 15089ce0151aSEric Biggers * the filename "lost+found" itself. 15099ce0151aSEric Biggers */ 15109ce0151aSEric Biggers if (inode->i_ino == EXT4_ROOT_INO) 15119ce0151aSEric Biggers return -EPERM; 1512a7550b30SJaegeuk Kim 15137d3e06a8SRoss Zwisler if (WARN_ON_ONCE(IS_DAX(inode) && i_size_read(inode))) 15147d3e06a8SRoss Zwisler return -EINVAL; 15157d3e06a8SRoss Zwisler 1516b383a73fSIra Weiny if (ext4_test_inode_flag(inode, EXT4_INODE_DAX)) 1517b383a73fSIra Weiny return -EOPNOTSUPP; 1518b383a73fSIra Weiny 151994840e3cSEric Biggers res = ext4_convert_inline_data(inode); 152094840e3cSEric Biggers if (res) 152194840e3cSEric Biggers return res; 152294840e3cSEric Biggers 15232f8f5e76SEric Biggers /* 15242f8f5e76SEric Biggers * If a journal handle was specified, then the encryption context is 15252f8f5e76SEric Biggers * being set on a new inode via inheritance and is part of a larger 15262f8f5e76SEric Biggers * transaction to create the inode. Otherwise the encryption context is 15272f8f5e76SEric Biggers * being set on an existing inode in its own transaction. Only in the 15282f8f5e76SEric Biggers * latter case should the "retry on ENOSPC" logic be used. 15292f8f5e76SEric Biggers */ 15302f8f5e76SEric Biggers 15312f8f5e76SEric Biggers if (handle) { 15322f8f5e76SEric Biggers res = ext4_xattr_set_handle(handle, inode, 15332f8f5e76SEric Biggers EXT4_XATTR_INDEX_ENCRYPTION, 15342f8f5e76SEric Biggers EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, 15352f8f5e76SEric Biggers ctx, len, 0); 1536a7550b30SJaegeuk Kim if (!res) { 1537a7550b30SJaegeuk Kim ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT); 1538a7550b30SJaegeuk Kim ext4_clear_inode_state(inode, 1539a7550b30SJaegeuk Kim EXT4_STATE_MAY_INLINE_DATA); 1540a3caa24bSJan Kara /* 15412ee6a576SEric Biggers * Update inode->i_flags - S_ENCRYPTED will be enabled, 15422ee6a576SEric Biggers * S_DAX may be disabled 1543a3caa24bSJan Kara */ 1544043546e4SIra Weiny ext4_set_inode_flags(inode, false); 1545a7550b30SJaegeuk Kim } 1546a7550b30SJaegeuk Kim return res; 1547a7550b30SJaegeuk Kim } 1548a7550b30SJaegeuk Kim 1549b8cb5a54STahsin Erdogan res = dquot_initialize(inode); 1550b8cb5a54STahsin Erdogan if (res) 1551b8cb5a54STahsin Erdogan return res; 15522f8f5e76SEric Biggers retry: 1553af65207cSTahsin Erdogan res = ext4_xattr_set_credits(inode, len, false /* is_create */, 1554af65207cSTahsin Erdogan &credits); 1555dec214d0STahsin Erdogan if (res) 1556dec214d0STahsin Erdogan return res; 1557dec214d0STahsin Erdogan 1558c1a5d5f6STahsin Erdogan handle = ext4_journal_start(inode, EXT4_HT_MISC, credits); 1559a7550b30SJaegeuk Kim if (IS_ERR(handle)) 1560a7550b30SJaegeuk Kim return PTR_ERR(handle); 1561a7550b30SJaegeuk Kim 15622f8f5e76SEric Biggers res = ext4_xattr_set_handle(handle, inode, EXT4_XATTR_INDEX_ENCRYPTION, 15632f8f5e76SEric Biggers EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, 15642f8f5e76SEric Biggers ctx, len, 0); 1565a7550b30SJaegeuk Kim if (!res) { 1566a7550b30SJaegeuk Kim ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT); 15672ee6a576SEric Biggers /* 15682ee6a576SEric Biggers * Update inode->i_flags - S_ENCRYPTED will be enabled, 15692ee6a576SEric Biggers * S_DAX may be disabled 15702ee6a576SEric Biggers */ 1571043546e4SIra Weiny ext4_set_inode_flags(inode, false); 1572a7550b30SJaegeuk Kim res = ext4_mark_inode_dirty(handle, inode); 1573a7550b30SJaegeuk Kim if (res) 1574a7550b30SJaegeuk Kim EXT4_ERROR_INODE(inode, "Failed to mark inode dirty"); 1575a7550b30SJaegeuk Kim } 1576a7550b30SJaegeuk Kim res2 = ext4_journal_stop(handle); 15772f8f5e76SEric Biggers 15782f8f5e76SEric Biggers if (res == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 15792f8f5e76SEric Biggers goto retry; 1580a7550b30SJaegeuk Kim if (!res) 1581a7550b30SJaegeuk Kim res = res2; 1582a7550b30SJaegeuk Kim return res; 1583a7550b30SJaegeuk Kim } 1584a7550b30SJaegeuk Kim 1585ed318a6cSEric Biggers static const union fscrypt_context * 1586ed318a6cSEric Biggers ext4_get_dummy_context(struct super_block *sb) 1587a7550b30SJaegeuk Kim { 1588ed318a6cSEric Biggers return EXT4_SB(sb)->s_dummy_enc_ctx.ctx; 1589a7550b30SJaegeuk Kim } 1590a7550b30SJaegeuk Kim 1591b925acb8SEric Biggers static bool ext4_has_stable_inodes(struct super_block *sb) 1592b925acb8SEric Biggers { 1593b925acb8SEric Biggers return ext4_has_feature_stable_inodes(sb); 1594b925acb8SEric Biggers } 1595b925acb8SEric Biggers 1596b925acb8SEric Biggers static void ext4_get_ino_and_lblk_bits(struct super_block *sb, 1597b925acb8SEric Biggers int *ino_bits_ret, int *lblk_bits_ret) 1598b925acb8SEric Biggers { 1599b925acb8SEric Biggers *ino_bits_ret = 8 * sizeof(EXT4_SB(sb)->s_es->s_inodes_count); 1600b925acb8SEric Biggers *lblk_bits_ret = 8 * sizeof(ext4_lblk_t); 1601b925acb8SEric Biggers } 1602b925acb8SEric Biggers 16036f69f0edSEric Biggers static const struct fscrypt_operations ext4_cryptops = { 1604a5d431efSEric Biggers .key_prefix = "ext4:", 1605a7550b30SJaegeuk Kim .get_context = ext4_get_context, 1606a7550b30SJaegeuk Kim .set_context = ext4_set_context, 1607ed318a6cSEric Biggers .get_dummy_context = ext4_get_dummy_context, 1608a7550b30SJaegeuk Kim .empty_dir = ext4_empty_dir, 1609e12ee683SEric Biggers .max_namelen = EXT4_NAME_LEN, 1610b925acb8SEric Biggers .has_stable_inodes = ext4_has_stable_inodes, 1611b925acb8SEric Biggers .get_ino_and_lblk_bits = ext4_get_ino_and_lblk_bits, 1612a7550b30SJaegeuk Kim }; 1613a7550b30SJaegeuk Kim #endif 1614a7550b30SJaegeuk Kim 1615ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 1616d6006186SEric Biggers static const char * const quotatypes[] = INITQFNAMES; 1617689c958cSLi Xi #define QTYPE2NAME(t) (quotatypes[t]) 1618ac27a0ecSDave Kleikamp 1619617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot); 1620617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot); 1621617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot); 1622617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot); 1623617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type); 16246f28e087SJan Kara static int ext4_quota_on(struct super_block *sb, int type, int format_id, 16258c54ca9cSAl Viro const struct path *path); 1626617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type); 1627617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data, 1628ac27a0ecSDave Kleikamp size_t len, loff_t off); 1629617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type, 1630ac27a0ecSDave Kleikamp const char *data, size_t len, loff_t off); 16317c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id, 16327c319d32SAditya Kali unsigned int flags); 16337c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb); 1634ac27a0ecSDave Kleikamp 163596c7e0d9SJan Kara static struct dquot **ext4_get_dquots(struct inode *inode) 163696c7e0d9SJan Kara { 163796c7e0d9SJan Kara return EXT4_I(inode)->i_dquot; 163896c7e0d9SJan Kara } 163996c7e0d9SJan Kara 164061e225dcSAlexey Dobriyan static const struct dquot_operations ext4_quota_operations = { 164160e58e0fSMingming Cao .get_reserved_space = ext4_get_reserved_space, 1642617ba13bSMingming Cao .write_dquot = ext4_write_dquot, 1643617ba13bSMingming Cao .acquire_dquot = ext4_acquire_dquot, 1644617ba13bSMingming Cao .release_dquot = ext4_release_dquot, 1645617ba13bSMingming Cao .mark_dirty = ext4_mark_dquot_dirty, 1646a5b5ee32SJan Kara .write_info = ext4_write_info, 1647a5b5ee32SJan Kara .alloc_dquot = dquot_alloc, 1648a5b5ee32SJan Kara .destroy_dquot = dquot_destroy, 1649040cb378SLi Xi .get_projid = ext4_get_projid, 16507a9ca53aSTahsin Erdogan .get_inode_usage = ext4_get_inode_usage, 1651ebc11f7bSChengguang Xu .get_next_id = dquot_get_next_id, 1652ac27a0ecSDave Kleikamp }; 1653ac27a0ecSDave Kleikamp 16540d54b217SAlexey Dobriyan static const struct quotactl_ops ext4_qctl_operations = { 1655617ba13bSMingming Cao .quota_on = ext4_quota_on, 1656ca0e05e4SDmitry Monakhov .quota_off = ext4_quota_off, 1657287a8095SChristoph Hellwig .quota_sync = dquot_quota_sync, 16580a240339SJan Kara .get_state = dquot_get_state, 1659287a8095SChristoph Hellwig .set_info = dquot_set_dqinfo, 1660287a8095SChristoph Hellwig .get_dqblk = dquot_get_dqblk, 16616332b9b5SEric Sandeen .set_dqblk = dquot_set_dqblk, 16626332b9b5SEric Sandeen .get_nextdqblk = dquot_get_next_dqblk, 1663ac27a0ecSDave Kleikamp }; 1664ac27a0ecSDave Kleikamp #endif 1665ac27a0ecSDave Kleikamp 1666ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations ext4_sops = { 1667617ba13bSMingming Cao .alloc_inode = ext4_alloc_inode, 166894053139SAl Viro .free_inode = ext4_free_in_core_inode, 1669617ba13bSMingming Cao .destroy_inode = ext4_destroy_inode, 1670617ba13bSMingming Cao .write_inode = ext4_write_inode, 1671617ba13bSMingming Cao .dirty_inode = ext4_dirty_inode, 16727ff9c073STheodore Ts'o .drop_inode = ext4_drop_inode, 16730930fcc1SAl Viro .evict_inode = ext4_evict_inode, 1674617ba13bSMingming Cao .put_super = ext4_put_super, 1675617ba13bSMingming Cao .sync_fs = ext4_sync_fs, 1676c4be0c1dSTakashi Sato .freeze_fs = ext4_freeze, 1677c4be0c1dSTakashi Sato .unfreeze_fs = ext4_unfreeze, 1678617ba13bSMingming Cao .statfs = ext4_statfs, 1679617ba13bSMingming Cao .remount_fs = ext4_remount, 1680617ba13bSMingming Cao .show_options = ext4_show_options, 1681ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 1682617ba13bSMingming Cao .quota_read = ext4_quota_read, 1683617ba13bSMingming Cao .quota_write = ext4_quota_write, 168496c7e0d9SJan Kara .get_dquots = ext4_get_dquots, 1685ac27a0ecSDave Kleikamp #endif 1686c39a7f84SToshiyuki Okajima .bdev_try_to_free_page = bdev_try_to_free_page, 1687ac27a0ecSDave Kleikamp }; 1688ac27a0ecSDave Kleikamp 168939655164SChristoph Hellwig static const struct export_operations ext4_export_ops = { 16901b961ac0SChristoph Hellwig .fh_to_dentry = ext4_fh_to_dentry, 16911b961ac0SChristoph Hellwig .fh_to_parent = ext4_fh_to_parent, 1692617ba13bSMingming Cao .get_parent = ext4_get_parent, 1693fde87268STheodore Ts'o .commit_metadata = ext4_nfs_commit_metadata, 1694ac27a0ecSDave Kleikamp }; 1695ac27a0ecSDave Kleikamp 1696ac27a0ecSDave Kleikamp enum { 1697ac27a0ecSDave Kleikamp Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid, 1698ac27a0ecSDave Kleikamp Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro, 169972578c33STheodore Ts'o Opt_nouid32, Opt_debug, Opt_removed, 1700ac27a0ecSDave Kleikamp Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl, 170172578c33STheodore Ts'o Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload, 1702ad4eec61SEric Sandeen Opt_commit, Opt_min_batch_time, Opt_max_batch_time, Opt_journal_dev, 1703ad4eec61SEric Sandeen Opt_journal_path, Opt_journal_checksum, Opt_journal_async_commit, 1704ac27a0ecSDave Kleikamp Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback, 17056ddb2447STheodore Ts'o Opt_data_err_abort, Opt_data_err_ignore, Opt_test_dummy_encryption, 17064f74d15fSEric Biggers Opt_inlinecrypt, 1707ac27a0ecSDave Kleikamp Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota, 17085a20bdfcSJan Kara Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota, 1709ee4a3fcdSTheodore Ts'o Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err, 17109cb20f94SIra Weiny Opt_usrquota, Opt_grpquota, Opt_prjquota, Opt_i_version, 17119cb20f94SIra Weiny Opt_dax, Opt_dax_always, Opt_dax_inode, Opt_dax_never, 1712327eaf73STheodore Ts'o Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_warn_on_error, 1713327eaf73STheodore Ts'o Opt_nowarn_on_error, Opt_mblk_io_submit, 1714670e9875STheodore Ts'o Opt_lazytime, Opt_nolazytime, Opt_debug_want_extra_isize, 17151449032bSTheodore Ts'o Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity, 17165328e635SEric Sandeen Opt_inode_readahead_blks, Opt_journal_ioprio, 1717744692dcSJiaying Zhang Opt_dioread_nolock, Opt_dioread_lock, 1718fc6cb1cdSTheodore Ts'o Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable, 1719cdb7ee4cSTahsin Erdogan Opt_max_dir_size_kb, Opt_nojournal_checksum, Opt_nombcache, 1720995a3ed6SHarshad Shirwadkar Opt_prefetch_block_bitmaps, Opt_no_fc, 1721ac27a0ecSDave Kleikamp }; 1722ac27a0ecSDave Kleikamp 1723a447c093SSteven Whitehouse static const match_table_t tokens = { 1724ac27a0ecSDave Kleikamp {Opt_bsd_df, "bsddf"}, 1725ac27a0ecSDave Kleikamp {Opt_minix_df, "minixdf"}, 1726ac27a0ecSDave Kleikamp {Opt_grpid, "grpid"}, 1727ac27a0ecSDave Kleikamp {Opt_grpid, "bsdgroups"}, 1728ac27a0ecSDave Kleikamp {Opt_nogrpid, "nogrpid"}, 1729ac27a0ecSDave Kleikamp {Opt_nogrpid, "sysvgroups"}, 1730ac27a0ecSDave Kleikamp {Opt_resgid, "resgid=%u"}, 1731ac27a0ecSDave Kleikamp {Opt_resuid, "resuid=%u"}, 1732ac27a0ecSDave Kleikamp {Opt_sb, "sb=%u"}, 1733ac27a0ecSDave Kleikamp {Opt_err_cont, "errors=continue"}, 1734ac27a0ecSDave Kleikamp {Opt_err_panic, "errors=panic"}, 1735ac27a0ecSDave Kleikamp {Opt_err_ro, "errors=remount-ro"}, 1736ac27a0ecSDave Kleikamp {Opt_nouid32, "nouid32"}, 1737ac27a0ecSDave Kleikamp {Opt_debug, "debug"}, 173872578c33STheodore Ts'o {Opt_removed, "oldalloc"}, 173972578c33STheodore Ts'o {Opt_removed, "orlov"}, 1740ac27a0ecSDave Kleikamp {Opt_user_xattr, "user_xattr"}, 1741ac27a0ecSDave Kleikamp {Opt_nouser_xattr, "nouser_xattr"}, 1742ac27a0ecSDave Kleikamp {Opt_acl, "acl"}, 1743ac27a0ecSDave Kleikamp {Opt_noacl, "noacl"}, 1744e3bb52aeSEric Sandeen {Opt_noload, "norecovery"}, 17455a916be1STheodore Ts'o {Opt_noload, "noload"}, 174672578c33STheodore Ts'o {Opt_removed, "nobh"}, 174772578c33STheodore Ts'o {Opt_removed, "bh"}, 1748ac27a0ecSDave Kleikamp {Opt_commit, "commit=%u"}, 174930773840STheodore Ts'o {Opt_min_batch_time, "min_batch_time=%u"}, 175030773840STheodore Ts'o {Opt_max_batch_time, "max_batch_time=%u"}, 1751ac27a0ecSDave Kleikamp {Opt_journal_dev, "journal_dev=%u"}, 1752ad4eec61SEric Sandeen {Opt_journal_path, "journal_path=%s"}, 1753818d276cSGirish Shilamkar {Opt_journal_checksum, "journal_checksum"}, 1754c6d3d56dSDarrick J. Wong {Opt_nojournal_checksum, "nojournal_checksum"}, 1755818d276cSGirish Shilamkar {Opt_journal_async_commit, "journal_async_commit"}, 1756ac27a0ecSDave Kleikamp {Opt_abort, "abort"}, 1757ac27a0ecSDave Kleikamp {Opt_data_journal, "data=journal"}, 1758ac27a0ecSDave Kleikamp {Opt_data_ordered, "data=ordered"}, 1759ac27a0ecSDave Kleikamp {Opt_data_writeback, "data=writeback"}, 17605bf5683aSHidehiro Kawai {Opt_data_err_abort, "data_err=abort"}, 17615bf5683aSHidehiro Kawai {Opt_data_err_ignore, "data_err=ignore"}, 1762ac27a0ecSDave Kleikamp {Opt_offusrjquota, "usrjquota="}, 1763ac27a0ecSDave Kleikamp {Opt_usrjquota, "usrjquota=%s"}, 1764ac27a0ecSDave Kleikamp {Opt_offgrpjquota, "grpjquota="}, 1765ac27a0ecSDave Kleikamp {Opt_grpjquota, "grpjquota=%s"}, 1766ac27a0ecSDave Kleikamp {Opt_jqfmt_vfsold, "jqfmt=vfsold"}, 1767ac27a0ecSDave Kleikamp {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"}, 17685a20bdfcSJan Kara {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"}, 1769ac27a0ecSDave Kleikamp {Opt_grpquota, "grpquota"}, 1770ac27a0ecSDave Kleikamp {Opt_noquota, "noquota"}, 1771ac27a0ecSDave Kleikamp {Opt_quota, "quota"}, 1772ac27a0ecSDave Kleikamp {Opt_usrquota, "usrquota"}, 177349da9392SJan Kara {Opt_prjquota, "prjquota"}, 1774ac27a0ecSDave Kleikamp {Opt_barrier, "barrier=%u"}, 177506705bffSTheodore Ts'o {Opt_barrier, "barrier"}, 177606705bffSTheodore Ts'o {Opt_nobarrier, "nobarrier"}, 177725ec56b5SJean Noel Cordenner {Opt_i_version, "i_version"}, 1778923ae0ffSRoss Zwisler {Opt_dax, "dax"}, 17799cb20f94SIra Weiny {Opt_dax_always, "dax=always"}, 17809cb20f94SIra Weiny {Opt_dax_inode, "dax=inode"}, 17819cb20f94SIra Weiny {Opt_dax_never, "dax=never"}, 1782c9de560dSAlex Tomas {Opt_stripe, "stripe=%u"}, 178364769240SAlex Tomas {Opt_delalloc, "delalloc"}, 1784327eaf73STheodore Ts'o {Opt_warn_on_error, "warn_on_error"}, 1785327eaf73STheodore Ts'o {Opt_nowarn_on_error, "nowarn_on_error"}, 1786a26f4992STheodore Ts'o {Opt_lazytime, "lazytime"}, 1787a26f4992STheodore Ts'o {Opt_nolazytime, "nolazytime"}, 1788670e9875STheodore Ts'o {Opt_debug_want_extra_isize, "debug_want_extra_isize=%u"}, 1789dd919b98SAneesh Kumar K.V {Opt_nodelalloc, "nodelalloc"}, 179036ade451SJan Kara {Opt_removed, "mblk_io_submit"}, 179136ade451SJan Kara {Opt_removed, "nomblk_io_submit"}, 17926fd058f7STheodore Ts'o {Opt_block_validity, "block_validity"}, 17936fd058f7STheodore Ts'o {Opt_noblock_validity, "noblock_validity"}, 1794240799cdSTheodore Ts'o {Opt_inode_readahead_blks, "inode_readahead_blks=%u"}, 1795b3881f74STheodore Ts'o {Opt_journal_ioprio, "journal_ioprio=%u"}, 1796afd4672dSTheodore Ts'o {Opt_auto_da_alloc, "auto_da_alloc=%u"}, 179706705bffSTheodore Ts'o {Opt_auto_da_alloc, "auto_da_alloc"}, 179806705bffSTheodore Ts'o {Opt_noauto_da_alloc, "noauto_da_alloc"}, 1799744692dcSJiaying Zhang {Opt_dioread_nolock, "dioread_nolock"}, 1800244adf64STheodore Ts'o {Opt_dioread_lock, "nodioread_nolock"}, 1801744692dcSJiaying Zhang {Opt_dioread_lock, "dioread_lock"}, 18025328e635SEric Sandeen {Opt_discard, "discard"}, 18035328e635SEric Sandeen {Opt_nodiscard, "nodiscard"}, 1804fc6cb1cdSTheodore Ts'o {Opt_init_itable, "init_itable=%u"}, 1805fc6cb1cdSTheodore Ts'o {Opt_init_itable, "init_itable"}, 1806fc6cb1cdSTheodore Ts'o {Opt_noinit_itable, "noinit_itable"}, 1807995a3ed6SHarshad Shirwadkar {Opt_no_fc, "no_fc"}, 1808df981d03STheodore Ts'o {Opt_max_dir_size_kb, "max_dir_size_kb=%u"}, 1809ed318a6cSEric Biggers {Opt_test_dummy_encryption, "test_dummy_encryption=%s"}, 18106ddb2447STheodore Ts'o {Opt_test_dummy_encryption, "test_dummy_encryption"}, 18114f74d15fSEric Biggers {Opt_inlinecrypt, "inlinecrypt"}, 1812cdb7ee4cSTahsin Erdogan {Opt_nombcache, "nombcache"}, 1813cdb7ee4cSTahsin Erdogan {Opt_nombcache, "no_mbcache"}, /* for backward compatibility */ 18143d392b26STheodore Ts'o {Opt_prefetch_block_bitmaps, "prefetch_block_bitmaps"}, 1815c7198b9cSTheodore Ts'o {Opt_removed, "check=none"}, /* mount option from ext2/3 */ 1816c7198b9cSTheodore Ts'o {Opt_removed, "nocheck"}, /* mount option from ext2/3 */ 1817c7198b9cSTheodore Ts'o {Opt_removed, "reservation"}, /* mount option from ext2/3 */ 1818c7198b9cSTheodore Ts'o {Opt_removed, "noreservation"}, /* mount option from ext2/3 */ 1819c7198b9cSTheodore Ts'o {Opt_removed, "journal=%u"}, /* mount option from ext2/3 */ 1820f3f12faaSJosef Bacik {Opt_err, NULL}, 1821ac27a0ecSDave Kleikamp }; 1822ac27a0ecSDave Kleikamp 1823617ba13bSMingming Cao static ext4_fsblk_t get_sb_block(void **data) 1824ac27a0ecSDave Kleikamp { 1825617ba13bSMingming Cao ext4_fsblk_t sb_block; 1826ac27a0ecSDave Kleikamp char *options = (char *) *data; 1827ac27a0ecSDave Kleikamp 1828ac27a0ecSDave Kleikamp if (!options || strncmp(options, "sb=", 3) != 0) 1829ac27a0ecSDave Kleikamp return 1; /* Default location */ 18300b8e58a1SAndreas Dilger 1831ac27a0ecSDave Kleikamp options += 3; 18320b8e58a1SAndreas Dilger /* TODO: use simple_strtoll with >32bit ext4 */ 1833ac27a0ecSDave Kleikamp sb_block = simple_strtoul(options, &options, 0); 1834ac27a0ecSDave Kleikamp if (*options && *options != ',') { 18354776004fSTheodore Ts'o printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n", 1836ac27a0ecSDave Kleikamp (char *) *data); 1837ac27a0ecSDave Kleikamp return 1; 1838ac27a0ecSDave Kleikamp } 1839ac27a0ecSDave Kleikamp if (*options == ',') 1840ac27a0ecSDave Kleikamp options++; 1841ac27a0ecSDave Kleikamp *data = (void *) options; 18420b8e58a1SAndreas Dilger 1843ac27a0ecSDave Kleikamp return sb_block; 1844ac27a0ecSDave Kleikamp } 1845ac27a0ecSDave Kleikamp 1846b3881f74STheodore Ts'o #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3)) 1847d6006186SEric Biggers static const char deprecated_msg[] = 1848d6006186SEric Biggers "Mount option \"%s\" will be removed by %s\n" 1849437ca0fdSDmitry Monakhov "Contact linux-ext4@vger.kernel.org if you think we should keep it.\n"; 1850b3881f74STheodore Ts'o 185156c50f11SDmitry Monakhov #ifdef CONFIG_QUOTA 185256c50f11SDmitry Monakhov static int set_qf_name(struct super_block *sb, int qtype, substring_t *args) 185356c50f11SDmitry Monakhov { 185456c50f11SDmitry Monakhov struct ext4_sb_info *sbi = EXT4_SB(sb); 185533458eabSTheodore Ts'o char *qname, *old_qname = get_qf_name(sb, sbi, qtype); 185603dafb5fSChen Gang int ret = -1; 185756c50f11SDmitry Monakhov 185833458eabSTheodore Ts'o if (sb_any_quota_loaded(sb) && !old_qname) { 185956c50f11SDmitry Monakhov ext4_msg(sb, KERN_ERR, 186056c50f11SDmitry Monakhov "Cannot change journaled " 186156c50f11SDmitry Monakhov "quota options when quota turned on"); 186257f73c2cSTheodore Ts'o return -1; 186356c50f11SDmitry Monakhov } 1864e2b911c5SDarrick J. Wong if (ext4_has_feature_quota(sb)) { 1865c325a67cSTheodore Ts'o ext4_msg(sb, KERN_INFO, "Journaled quota options " 1866c325a67cSTheodore Ts'o "ignored when QUOTA feature is enabled"); 1867c325a67cSTheodore Ts'o return 1; 1868262b4662SJan Kara } 186956c50f11SDmitry Monakhov qname = match_strdup(args); 187056c50f11SDmitry Monakhov if (!qname) { 187156c50f11SDmitry Monakhov ext4_msg(sb, KERN_ERR, 187256c50f11SDmitry Monakhov "Not enough memory for storing quotafile name"); 187357f73c2cSTheodore Ts'o return -1; 187456c50f11SDmitry Monakhov } 187533458eabSTheodore Ts'o if (old_qname) { 187633458eabSTheodore Ts'o if (strcmp(old_qname, qname) == 0) 187703dafb5fSChen Gang ret = 1; 187803dafb5fSChen Gang else 187956c50f11SDmitry Monakhov ext4_msg(sb, KERN_ERR, 188003dafb5fSChen Gang "%s quota file already specified", 188103dafb5fSChen Gang QTYPE2NAME(qtype)); 188203dafb5fSChen Gang goto errout; 188356c50f11SDmitry Monakhov } 188403dafb5fSChen Gang if (strchr(qname, '/')) { 188556c50f11SDmitry Monakhov ext4_msg(sb, KERN_ERR, 188656c50f11SDmitry Monakhov "quotafile must be on filesystem root"); 188703dafb5fSChen Gang goto errout; 188856c50f11SDmitry Monakhov } 188933458eabSTheodore Ts'o rcu_assign_pointer(sbi->s_qf_names[qtype], qname); 1890fd8c37ecSTheodore Ts'o set_opt(sb, QUOTA); 189156c50f11SDmitry Monakhov return 1; 189203dafb5fSChen Gang errout: 189303dafb5fSChen Gang kfree(qname); 189403dafb5fSChen Gang return ret; 189556c50f11SDmitry Monakhov } 189656c50f11SDmitry Monakhov 189756c50f11SDmitry Monakhov static int clear_qf_name(struct super_block *sb, int qtype) 189856c50f11SDmitry Monakhov { 189956c50f11SDmitry Monakhov 190056c50f11SDmitry Monakhov struct ext4_sb_info *sbi = EXT4_SB(sb); 190133458eabSTheodore Ts'o char *old_qname = get_qf_name(sb, sbi, qtype); 190256c50f11SDmitry Monakhov 190333458eabSTheodore Ts'o if (sb_any_quota_loaded(sb) && old_qname) { 190456c50f11SDmitry Monakhov ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options" 190556c50f11SDmitry Monakhov " when quota turned on"); 190657f73c2cSTheodore Ts'o return -1; 190756c50f11SDmitry Monakhov } 190833458eabSTheodore Ts'o rcu_assign_pointer(sbi->s_qf_names[qtype], NULL); 190933458eabSTheodore Ts'o synchronize_rcu(); 191033458eabSTheodore Ts'o kfree(old_qname); 191156c50f11SDmitry Monakhov return 1; 191256c50f11SDmitry Monakhov } 191356c50f11SDmitry Monakhov #endif 191456c50f11SDmitry Monakhov 191526092bf5STheodore Ts'o #define MOPT_SET 0x0001 191626092bf5STheodore Ts'o #define MOPT_CLEAR 0x0002 191726092bf5STheodore Ts'o #define MOPT_NOSUPPORT 0x0004 191826092bf5STheodore Ts'o #define MOPT_EXPLICIT 0x0008 191926092bf5STheodore Ts'o #define MOPT_CLEAR_ERR 0x0010 192026092bf5STheodore Ts'o #define MOPT_GTE0 0x0020 192126092bf5STheodore Ts'o #ifdef CONFIG_QUOTA 192226092bf5STheodore Ts'o #define MOPT_Q 0 192326092bf5STheodore Ts'o #define MOPT_QFMT 0x0040 192426092bf5STheodore Ts'o #else 192526092bf5STheodore Ts'o #define MOPT_Q MOPT_NOSUPPORT 192626092bf5STheodore Ts'o #define MOPT_QFMT MOPT_NOSUPPORT 192726092bf5STheodore Ts'o #endif 192826092bf5STheodore Ts'o #define MOPT_DATAJ 0x0080 19298dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT2 0x0100 19308dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT3 0x0200 19318dc0aa8cSTheodore Ts'o #define MOPT_EXT4_ONLY (MOPT_NO_EXT2 | MOPT_NO_EXT3) 1932ad4eec61SEric Sandeen #define MOPT_STRING 0x0400 19339cb20f94SIra Weiny #define MOPT_SKIP 0x0800 1934995a3ed6SHarshad Shirwadkar #define MOPT_2 0x1000 193526092bf5STheodore Ts'o 193626092bf5STheodore Ts'o static const struct mount_opts { 193726092bf5STheodore Ts'o int token; 193826092bf5STheodore Ts'o int mount_opt; 193926092bf5STheodore Ts'o int flags; 194026092bf5STheodore Ts'o } ext4_mount_opts[] = { 194126092bf5STheodore Ts'o {Opt_minix_df, EXT4_MOUNT_MINIX_DF, MOPT_SET}, 194226092bf5STheodore Ts'o {Opt_bsd_df, EXT4_MOUNT_MINIX_DF, MOPT_CLEAR}, 194326092bf5STheodore Ts'o {Opt_grpid, EXT4_MOUNT_GRPID, MOPT_SET}, 194426092bf5STheodore Ts'o {Opt_nogrpid, EXT4_MOUNT_GRPID, MOPT_CLEAR}, 194526092bf5STheodore Ts'o {Opt_block_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_SET}, 194626092bf5STheodore Ts'o {Opt_noblock_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_CLEAR}, 19478dc0aa8cSTheodore Ts'o {Opt_dioread_nolock, EXT4_MOUNT_DIOREAD_NOLOCK, 19488dc0aa8cSTheodore Ts'o MOPT_EXT4_ONLY | MOPT_SET}, 19498dc0aa8cSTheodore Ts'o {Opt_dioread_lock, EXT4_MOUNT_DIOREAD_NOLOCK, 19508dc0aa8cSTheodore Ts'o MOPT_EXT4_ONLY | MOPT_CLEAR}, 195126092bf5STheodore Ts'o {Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET}, 195226092bf5STheodore Ts'o {Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR}, 19538dc0aa8cSTheodore Ts'o {Opt_delalloc, EXT4_MOUNT_DELALLOC, 19548dc0aa8cSTheodore Ts'o MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT}, 19558dc0aa8cSTheodore Ts'o {Opt_nodelalloc, EXT4_MOUNT_DELALLOC, 195659d9fa5cSTheodore Ts'o MOPT_EXT4_ONLY | MOPT_CLEAR}, 1957327eaf73STheodore Ts'o {Opt_warn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_SET}, 1958327eaf73STheodore Ts'o {Opt_nowarn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_CLEAR}, 1959c6d3d56dSDarrick J. Wong {Opt_nojournal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM, 1960c6d3d56dSDarrick J. Wong MOPT_EXT4_ONLY | MOPT_CLEAR}, 19618dc0aa8cSTheodore Ts'o {Opt_journal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM, 19621e381f60SDmitry Monakhov MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT}, 196326092bf5STheodore Ts'o {Opt_journal_async_commit, (EXT4_MOUNT_JOURNAL_ASYNC_COMMIT | 19648dc0aa8cSTheodore Ts'o EXT4_MOUNT_JOURNAL_CHECKSUM), 19651e381f60SDmitry Monakhov MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT}, 19668dc0aa8cSTheodore Ts'o {Opt_noload, EXT4_MOUNT_NOLOAD, MOPT_NO_EXT2 | MOPT_SET}, 196726092bf5STheodore Ts'o {Opt_err_panic, EXT4_MOUNT_ERRORS_PANIC, MOPT_SET | MOPT_CLEAR_ERR}, 196826092bf5STheodore Ts'o {Opt_err_ro, EXT4_MOUNT_ERRORS_RO, MOPT_SET | MOPT_CLEAR_ERR}, 196926092bf5STheodore Ts'o {Opt_err_cont, EXT4_MOUNT_ERRORS_CONT, MOPT_SET | MOPT_CLEAR_ERR}, 19708dc0aa8cSTheodore Ts'o {Opt_data_err_abort, EXT4_MOUNT_DATA_ERR_ABORT, 19717915a861SAles Novak MOPT_NO_EXT2}, 19728dc0aa8cSTheodore Ts'o {Opt_data_err_ignore, EXT4_MOUNT_DATA_ERR_ABORT, 19737915a861SAles Novak MOPT_NO_EXT2}, 197426092bf5STheodore Ts'o {Opt_barrier, EXT4_MOUNT_BARRIER, MOPT_SET}, 197526092bf5STheodore Ts'o {Opt_nobarrier, EXT4_MOUNT_BARRIER, MOPT_CLEAR}, 197626092bf5STheodore Ts'o {Opt_noauto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_SET}, 197726092bf5STheodore Ts'o {Opt_auto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_CLEAR}, 197826092bf5STheodore Ts'o {Opt_noinit_itable, EXT4_MOUNT_INIT_INODE_TABLE, MOPT_CLEAR}, 197926092bf5STheodore Ts'o {Opt_commit, 0, MOPT_GTE0}, 198026092bf5STheodore Ts'o {Opt_max_batch_time, 0, MOPT_GTE0}, 198126092bf5STheodore Ts'o {Opt_min_batch_time, 0, MOPT_GTE0}, 198226092bf5STheodore Ts'o {Opt_inode_readahead_blks, 0, MOPT_GTE0}, 198326092bf5STheodore Ts'o {Opt_init_itable, 0, MOPT_GTE0}, 19849cb20f94SIra Weiny {Opt_dax, EXT4_MOUNT_DAX_ALWAYS, MOPT_SET | MOPT_SKIP}, 19859cb20f94SIra Weiny {Opt_dax_always, EXT4_MOUNT_DAX_ALWAYS, 19869cb20f94SIra Weiny MOPT_EXT4_ONLY | MOPT_SET | MOPT_SKIP}, 19879cb20f94SIra Weiny {Opt_dax_inode, EXT4_MOUNT2_DAX_INODE, 19889cb20f94SIra Weiny MOPT_EXT4_ONLY | MOPT_SET | MOPT_SKIP}, 19899cb20f94SIra Weiny {Opt_dax_never, EXT4_MOUNT2_DAX_NEVER, 19909cb20f94SIra Weiny MOPT_EXT4_ONLY | MOPT_SET | MOPT_SKIP}, 199126092bf5STheodore Ts'o {Opt_stripe, 0, MOPT_GTE0}, 19920efb3b23SJan Kara {Opt_resuid, 0, MOPT_GTE0}, 19930efb3b23SJan Kara {Opt_resgid, 0, MOPT_GTE0}, 19945ba92bcfSCarlos Maiolino {Opt_journal_dev, 0, MOPT_NO_EXT2 | MOPT_GTE0}, 19955ba92bcfSCarlos Maiolino {Opt_journal_path, 0, MOPT_NO_EXT2 | MOPT_STRING}, 19965ba92bcfSCarlos Maiolino {Opt_journal_ioprio, 0, MOPT_NO_EXT2 | MOPT_GTE0}, 19978dc0aa8cSTheodore Ts'o {Opt_data_journal, EXT4_MOUNT_JOURNAL_DATA, MOPT_NO_EXT2 | MOPT_DATAJ}, 19988dc0aa8cSTheodore Ts'o {Opt_data_ordered, EXT4_MOUNT_ORDERED_DATA, MOPT_NO_EXT2 | MOPT_DATAJ}, 19998dc0aa8cSTheodore Ts'o {Opt_data_writeback, EXT4_MOUNT_WRITEBACK_DATA, 20008dc0aa8cSTheodore Ts'o MOPT_NO_EXT2 | MOPT_DATAJ}, 200126092bf5STheodore Ts'o {Opt_user_xattr, EXT4_MOUNT_XATTR_USER, MOPT_SET}, 200226092bf5STheodore Ts'o {Opt_nouser_xattr, EXT4_MOUNT_XATTR_USER, MOPT_CLEAR}, 200326092bf5STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL 200426092bf5STheodore Ts'o {Opt_acl, EXT4_MOUNT_POSIX_ACL, MOPT_SET}, 200526092bf5STheodore Ts'o {Opt_noacl, EXT4_MOUNT_POSIX_ACL, MOPT_CLEAR}, 200626092bf5STheodore Ts'o #else 200726092bf5STheodore Ts'o {Opt_acl, 0, MOPT_NOSUPPORT}, 200826092bf5STheodore Ts'o {Opt_noacl, 0, MOPT_NOSUPPORT}, 200926092bf5STheodore Ts'o #endif 201026092bf5STheodore Ts'o {Opt_nouid32, EXT4_MOUNT_NO_UID32, MOPT_SET}, 201126092bf5STheodore Ts'o {Opt_debug, EXT4_MOUNT_DEBUG, MOPT_SET}, 2012670e9875STheodore Ts'o {Opt_debug_want_extra_isize, 0, MOPT_GTE0}, 201326092bf5STheodore Ts'o {Opt_quota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, MOPT_SET | MOPT_Q}, 201426092bf5STheodore Ts'o {Opt_usrquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, 201526092bf5STheodore Ts'o MOPT_SET | MOPT_Q}, 201626092bf5STheodore Ts'o {Opt_grpquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_GRPQUOTA, 201726092bf5STheodore Ts'o MOPT_SET | MOPT_Q}, 201849da9392SJan Kara {Opt_prjquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_PRJQUOTA, 201949da9392SJan Kara MOPT_SET | MOPT_Q}, 202026092bf5STheodore Ts'o {Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA | 202149da9392SJan Kara EXT4_MOUNT_GRPQUOTA | EXT4_MOUNT_PRJQUOTA), 202249da9392SJan Kara MOPT_CLEAR | MOPT_Q}, 202326092bf5STheodore Ts'o {Opt_usrjquota, 0, MOPT_Q}, 202426092bf5STheodore Ts'o {Opt_grpjquota, 0, MOPT_Q}, 202526092bf5STheodore Ts'o {Opt_offusrjquota, 0, MOPT_Q}, 202626092bf5STheodore Ts'o {Opt_offgrpjquota, 0, MOPT_Q}, 202726092bf5STheodore Ts'o {Opt_jqfmt_vfsold, QFMT_VFS_OLD, MOPT_QFMT}, 202826092bf5STheodore Ts'o {Opt_jqfmt_vfsv0, QFMT_VFS_V0, MOPT_QFMT}, 202926092bf5STheodore Ts'o {Opt_jqfmt_vfsv1, QFMT_VFS_V1, MOPT_QFMT}, 2030df981d03STheodore Ts'o {Opt_max_dir_size_kb, 0, MOPT_GTE0}, 2031ed318a6cSEric Biggers {Opt_test_dummy_encryption, 0, MOPT_STRING}, 2032cdb7ee4cSTahsin Erdogan {Opt_nombcache, EXT4_MOUNT_NO_MBCACHE, MOPT_SET}, 20333d392b26STheodore Ts'o {Opt_prefetch_block_bitmaps, EXT4_MOUNT_PREFETCH_BLOCK_BITMAPS, 20343d392b26STheodore Ts'o MOPT_SET}, 2035995a3ed6SHarshad Shirwadkar {Opt_no_fc, EXT4_MOUNT2_JOURNAL_FAST_COMMIT, 2036995a3ed6SHarshad Shirwadkar MOPT_CLEAR | MOPT_2 | MOPT_EXT4_ONLY}, 203726092bf5STheodore Ts'o {Opt_err, 0, 0} 203826092bf5STheodore Ts'o }; 203926092bf5STheodore Ts'o 2040c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE 2041c83ad55eSGabriel Krisman Bertazi static const struct ext4_sb_encodings { 2042c83ad55eSGabriel Krisman Bertazi __u16 magic; 2043c83ad55eSGabriel Krisman Bertazi char *name; 2044c83ad55eSGabriel Krisman Bertazi char *version; 2045c83ad55eSGabriel Krisman Bertazi } ext4_sb_encoding_map[] = { 2046c83ad55eSGabriel Krisman Bertazi {EXT4_ENC_UTF8_12_1, "utf8", "12.1.0"}, 2047c83ad55eSGabriel Krisman Bertazi }; 2048c83ad55eSGabriel Krisman Bertazi 2049c83ad55eSGabriel Krisman Bertazi static int ext4_sb_read_encoding(const struct ext4_super_block *es, 2050c83ad55eSGabriel Krisman Bertazi const struct ext4_sb_encodings **encoding, 2051c83ad55eSGabriel Krisman Bertazi __u16 *flags) 2052c83ad55eSGabriel Krisman Bertazi { 2053c83ad55eSGabriel Krisman Bertazi __u16 magic = le16_to_cpu(es->s_encoding); 2054c83ad55eSGabriel Krisman Bertazi int i; 2055c83ad55eSGabriel Krisman Bertazi 2056c83ad55eSGabriel Krisman Bertazi for (i = 0; i < ARRAY_SIZE(ext4_sb_encoding_map); i++) 2057c83ad55eSGabriel Krisman Bertazi if (magic == ext4_sb_encoding_map[i].magic) 2058c83ad55eSGabriel Krisman Bertazi break; 2059c83ad55eSGabriel Krisman Bertazi 2060c83ad55eSGabriel Krisman Bertazi if (i >= ARRAY_SIZE(ext4_sb_encoding_map)) 2061c83ad55eSGabriel Krisman Bertazi return -EINVAL; 2062c83ad55eSGabriel Krisman Bertazi 2063c83ad55eSGabriel Krisman Bertazi *encoding = &ext4_sb_encoding_map[i]; 2064c83ad55eSGabriel Krisman Bertazi *flags = le16_to_cpu(es->s_encoding_flags); 2065c83ad55eSGabriel Krisman Bertazi 2066c83ad55eSGabriel Krisman Bertazi return 0; 2067c83ad55eSGabriel Krisman Bertazi } 2068c83ad55eSGabriel Krisman Bertazi #endif 2069c83ad55eSGabriel Krisman Bertazi 2070ed318a6cSEric Biggers static int ext4_set_test_dummy_encryption(struct super_block *sb, 2071ed318a6cSEric Biggers const char *opt, 2072ed318a6cSEric Biggers const substring_t *arg, 2073ed318a6cSEric Biggers bool is_remount) 2074ed318a6cSEric Biggers { 2075ed318a6cSEric Biggers #ifdef CONFIG_FS_ENCRYPTION 2076ed318a6cSEric Biggers struct ext4_sb_info *sbi = EXT4_SB(sb); 2077ed318a6cSEric Biggers int err; 2078ed318a6cSEric Biggers 2079ed318a6cSEric Biggers /* 2080ed318a6cSEric Biggers * This mount option is just for testing, and it's not worthwhile to 2081ed318a6cSEric Biggers * implement the extra complexity (e.g. RCU protection) that would be 2082ed318a6cSEric Biggers * needed to allow it to be set or changed during remount. We do allow 2083ed318a6cSEric Biggers * it to be specified during remount, but only if there is no change. 2084ed318a6cSEric Biggers */ 2085ed318a6cSEric Biggers if (is_remount && !sbi->s_dummy_enc_ctx.ctx) { 2086ed318a6cSEric Biggers ext4_msg(sb, KERN_WARNING, 2087ed318a6cSEric Biggers "Can't set test_dummy_encryption on remount"); 2088ed318a6cSEric Biggers return -1; 2089ed318a6cSEric Biggers } 2090ed318a6cSEric Biggers err = fscrypt_set_test_dummy_encryption(sb, arg, &sbi->s_dummy_enc_ctx); 2091ed318a6cSEric Biggers if (err) { 2092ed318a6cSEric Biggers if (err == -EEXIST) 2093ed318a6cSEric Biggers ext4_msg(sb, KERN_WARNING, 2094ed318a6cSEric Biggers "Can't change test_dummy_encryption on remount"); 2095ed318a6cSEric Biggers else if (err == -EINVAL) 2096ed318a6cSEric Biggers ext4_msg(sb, KERN_WARNING, 2097ed318a6cSEric Biggers "Value of option \"%s\" is unrecognized", opt); 2098ed318a6cSEric Biggers else 2099ed318a6cSEric Biggers ext4_msg(sb, KERN_WARNING, 2100ed318a6cSEric Biggers "Error processing option \"%s\" [%d]", 2101ed318a6cSEric Biggers opt, err); 2102ed318a6cSEric Biggers return -1; 2103ed318a6cSEric Biggers } 2104ed318a6cSEric Biggers ext4_msg(sb, KERN_WARNING, "Test dummy encryption mode enabled"); 2105ed318a6cSEric Biggers #else 2106ed318a6cSEric Biggers ext4_msg(sb, KERN_WARNING, 2107ed318a6cSEric Biggers "Test dummy encryption mount option ignored"); 2108ed318a6cSEric Biggers #endif 2109ed318a6cSEric Biggers return 1; 2110ed318a6cSEric Biggers } 2111ed318a6cSEric Biggers 211226092bf5STheodore Ts'o static int handle_mount_opt(struct super_block *sb, char *opt, int token, 211326092bf5STheodore Ts'o substring_t *args, unsigned long *journal_devnum, 211426092bf5STheodore Ts'o unsigned int *journal_ioprio, int is_remount) 211526092bf5STheodore Ts'o { 211626092bf5STheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(sb); 211726092bf5STheodore Ts'o const struct mount_opts *m; 211808cefc7aSEric W. Biederman kuid_t uid; 211908cefc7aSEric W. Biederman kgid_t gid; 212026092bf5STheodore Ts'o int arg = 0; 212126092bf5STheodore Ts'o 212257f73c2cSTheodore Ts'o #ifdef CONFIG_QUOTA 212357f73c2cSTheodore Ts'o if (token == Opt_usrjquota) 212457f73c2cSTheodore Ts'o return set_qf_name(sb, USRQUOTA, &args[0]); 212557f73c2cSTheodore Ts'o else if (token == Opt_grpjquota) 212657f73c2cSTheodore Ts'o return set_qf_name(sb, GRPQUOTA, &args[0]); 212757f73c2cSTheodore Ts'o else if (token == Opt_offusrjquota) 212857f73c2cSTheodore Ts'o return clear_qf_name(sb, USRQUOTA); 212957f73c2cSTheodore Ts'o else if (token == Opt_offgrpjquota) 213057f73c2cSTheodore Ts'o return clear_qf_name(sb, GRPQUOTA); 213157f73c2cSTheodore Ts'o #endif 213226092bf5STheodore Ts'o switch (token) { 2133f7048605STheodore Ts'o case Opt_noacl: 2134f7048605STheodore Ts'o case Opt_nouser_xattr: 2135f7048605STheodore Ts'o ext4_msg(sb, KERN_WARNING, deprecated_msg, opt, "3.5"); 2136f7048605STheodore Ts'o break; 213726092bf5STheodore Ts'o case Opt_sb: 213826092bf5STheodore Ts'o return 1; /* handled by get_sb_block() */ 213926092bf5STheodore Ts'o case Opt_removed: 21405f3633e3SJan Kara ext4_msg(sb, KERN_WARNING, "Ignoring removed %s option", opt); 214126092bf5STheodore Ts'o return 1; 214226092bf5STheodore Ts'o case Opt_abort: 214326092bf5STheodore Ts'o sbi->s_mount_flags |= EXT4_MF_FS_ABORTED; 214426092bf5STheodore Ts'o return 1; 214526092bf5STheodore Ts'o case Opt_i_version: 2146357fdad0SMatthew Garrett sb->s_flags |= SB_I_VERSION; 214726092bf5STheodore Ts'o return 1; 2148a26f4992STheodore Ts'o case Opt_lazytime: 21491751e8a6SLinus Torvalds sb->s_flags |= SB_LAZYTIME; 2150a26f4992STheodore Ts'o return 1; 2151a26f4992STheodore Ts'o case Opt_nolazytime: 21521751e8a6SLinus Torvalds sb->s_flags &= ~SB_LAZYTIME; 2153a26f4992STheodore Ts'o return 1; 21544f74d15fSEric Biggers case Opt_inlinecrypt: 21554f74d15fSEric Biggers #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT 21564f74d15fSEric Biggers sb->s_flags |= SB_INLINECRYPT; 21574f74d15fSEric Biggers #else 21584f74d15fSEric Biggers ext4_msg(sb, KERN_ERR, "inline encryption not supported"); 21594f74d15fSEric Biggers #endif 21604f74d15fSEric Biggers return 1; 216126092bf5STheodore Ts'o } 216226092bf5STheodore Ts'o 21635f3633e3SJan Kara for (m = ext4_mount_opts; m->token != Opt_err; m++) 21645f3633e3SJan Kara if (token == m->token) 21655f3633e3SJan Kara break; 21665f3633e3SJan Kara 21675f3633e3SJan Kara if (m->token == Opt_err) { 21685f3633e3SJan Kara ext4_msg(sb, KERN_ERR, "Unrecognized mount option \"%s\" " 21695f3633e3SJan Kara "or missing value", opt); 21705f3633e3SJan Kara return -1; 21715f3633e3SJan Kara } 21725f3633e3SJan Kara 21738dc0aa8cSTheodore Ts'o if ((m->flags & MOPT_NO_EXT2) && IS_EXT2_SB(sb)) { 21748dc0aa8cSTheodore Ts'o ext4_msg(sb, KERN_ERR, 21758dc0aa8cSTheodore Ts'o "Mount option \"%s\" incompatible with ext2", opt); 21768dc0aa8cSTheodore Ts'o return -1; 21778dc0aa8cSTheodore Ts'o } 21788dc0aa8cSTheodore Ts'o if ((m->flags & MOPT_NO_EXT3) && IS_EXT3_SB(sb)) { 21798dc0aa8cSTheodore Ts'o ext4_msg(sb, KERN_ERR, 21808dc0aa8cSTheodore Ts'o "Mount option \"%s\" incompatible with ext3", opt); 21818dc0aa8cSTheodore Ts'o return -1; 21828dc0aa8cSTheodore Ts'o } 21838dc0aa8cSTheodore Ts'o 2184ad4eec61SEric Sandeen if (args->from && !(m->flags & MOPT_STRING) && match_int(args, &arg)) 21850efb3b23SJan Kara return -1; 218626092bf5STheodore Ts'o if (args->from && (m->flags & MOPT_GTE0) && (arg < 0)) 218726092bf5STheodore Ts'o return -1; 2188c93cf2d7SDmitry Monakhov if (m->flags & MOPT_EXPLICIT) { 2189c93cf2d7SDmitry Monakhov if (m->mount_opt & EXT4_MOUNT_DELALLOC) { 219026092bf5STheodore Ts'o set_opt2(sb, EXPLICIT_DELALLOC); 21911e381f60SDmitry Monakhov } else if (m->mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) { 21921e381f60SDmitry Monakhov set_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM); 2193c93cf2d7SDmitry Monakhov } else 2194c93cf2d7SDmitry Monakhov return -1; 2195c93cf2d7SDmitry Monakhov } 219626092bf5STheodore Ts'o if (m->flags & MOPT_CLEAR_ERR) 219726092bf5STheodore Ts'o clear_opt(sb, ERRORS_MASK); 219826092bf5STheodore Ts'o if (token == Opt_noquota && sb_any_quota_loaded(sb)) { 219926092bf5STheodore Ts'o ext4_msg(sb, KERN_ERR, "Cannot change quota " 220026092bf5STheodore Ts'o "options when quota turned on"); 220126092bf5STheodore Ts'o return -1; 220226092bf5STheodore Ts'o } 220326092bf5STheodore Ts'o 220426092bf5STheodore Ts'o if (m->flags & MOPT_NOSUPPORT) { 220526092bf5STheodore Ts'o ext4_msg(sb, KERN_ERR, "%s option not supported", opt); 220626092bf5STheodore Ts'o } else if (token == Opt_commit) { 220726092bf5STheodore Ts'o if (arg == 0) 220826092bf5STheodore Ts'o arg = JBD2_DEFAULT_MAX_COMMIT_AGE; 22099ba55543Szhangyi (F) else if (arg > INT_MAX / HZ) { 22109ba55543Szhangyi (F) ext4_msg(sb, KERN_ERR, 22119ba55543Szhangyi (F) "Invalid commit interval %d, " 22129ba55543Szhangyi (F) "must be smaller than %d", 22139ba55543Szhangyi (F) arg, INT_MAX / HZ); 22149ba55543Szhangyi (F) return -1; 22159ba55543Szhangyi (F) } 221626092bf5STheodore Ts'o sbi->s_commit_interval = HZ * arg; 2217670e9875STheodore Ts'o } else if (token == Opt_debug_want_extra_isize) { 22189803387cSTheodore Ts'o if ((arg & 1) || 22199803387cSTheodore Ts'o (arg < 4) || 22209803387cSTheodore Ts'o (arg > (sbi->s_inode_size - EXT4_GOOD_OLD_INODE_SIZE))) { 22219803387cSTheodore Ts'o ext4_msg(sb, KERN_ERR, 22229803387cSTheodore Ts'o "Invalid want_extra_isize %d", arg); 22239803387cSTheodore Ts'o return -1; 22249803387cSTheodore Ts'o } 2225670e9875STheodore Ts'o sbi->s_want_extra_isize = arg; 222626092bf5STheodore Ts'o } else if (token == Opt_max_batch_time) { 222726092bf5STheodore Ts'o sbi->s_max_batch_time = arg; 222826092bf5STheodore Ts'o } else if (token == Opt_min_batch_time) { 222926092bf5STheodore Ts'o sbi->s_min_batch_time = arg; 223026092bf5STheodore Ts'o } else if (token == Opt_inode_readahead_blks) { 2231e33e60eaSJan Kara if (arg && (arg > (1 << 30) || !is_power_of_2(arg))) { 2232e33e60eaSJan Kara ext4_msg(sb, KERN_ERR, 2233e33e60eaSJan Kara "EXT4-fs: inode_readahead_blks must be " 2234e33e60eaSJan Kara "0 or a power of 2 smaller than 2^31"); 223526092bf5STheodore Ts'o return -1; 223626092bf5STheodore Ts'o } 223726092bf5STheodore Ts'o sbi->s_inode_readahead_blks = arg; 223826092bf5STheodore Ts'o } else if (token == Opt_init_itable) { 223926092bf5STheodore Ts'o set_opt(sb, INIT_INODE_TABLE); 224026092bf5STheodore Ts'o if (!args->from) 224126092bf5STheodore Ts'o arg = EXT4_DEF_LI_WAIT_MULT; 224226092bf5STheodore Ts'o sbi->s_li_wait_mult = arg; 2243df981d03STheodore Ts'o } else if (token == Opt_max_dir_size_kb) { 2244df981d03STheodore Ts'o sbi->s_max_dir_size_kb = arg; 224526092bf5STheodore Ts'o } else if (token == Opt_stripe) { 224626092bf5STheodore Ts'o sbi->s_stripe = arg; 22470efb3b23SJan Kara } else if (token == Opt_resuid) { 22480efb3b23SJan Kara uid = make_kuid(current_user_ns(), arg); 22490efb3b23SJan Kara if (!uid_valid(uid)) { 22505f3633e3SJan Kara ext4_msg(sb, KERN_ERR, "Invalid uid value %d", arg); 22510efb3b23SJan Kara return -1; 22520efb3b23SJan Kara } 22530efb3b23SJan Kara sbi->s_resuid = uid; 22540efb3b23SJan Kara } else if (token == Opt_resgid) { 22550efb3b23SJan Kara gid = make_kgid(current_user_ns(), arg); 22560efb3b23SJan Kara if (!gid_valid(gid)) { 22575f3633e3SJan Kara ext4_msg(sb, KERN_ERR, "Invalid gid value %d", arg); 22580efb3b23SJan Kara return -1; 22590efb3b23SJan Kara } 22600efb3b23SJan Kara sbi->s_resgid = gid; 22610efb3b23SJan Kara } else if (token == Opt_journal_dev) { 22620efb3b23SJan Kara if (is_remount) { 22630efb3b23SJan Kara ext4_msg(sb, KERN_ERR, 22640efb3b23SJan Kara "Cannot specify journal on remount"); 22650efb3b23SJan Kara return -1; 22660efb3b23SJan Kara } 22670efb3b23SJan Kara *journal_devnum = arg; 2268ad4eec61SEric Sandeen } else if (token == Opt_journal_path) { 2269ad4eec61SEric Sandeen char *journal_path; 2270ad4eec61SEric Sandeen struct inode *journal_inode; 2271ad4eec61SEric Sandeen struct path path; 2272ad4eec61SEric Sandeen int error; 2273ad4eec61SEric Sandeen 2274ad4eec61SEric Sandeen if (is_remount) { 2275ad4eec61SEric Sandeen ext4_msg(sb, KERN_ERR, 2276ad4eec61SEric Sandeen "Cannot specify journal on remount"); 2277ad4eec61SEric Sandeen return -1; 2278ad4eec61SEric Sandeen } 2279ad4eec61SEric Sandeen journal_path = match_strdup(&args[0]); 2280ad4eec61SEric Sandeen if (!journal_path) { 2281ad4eec61SEric Sandeen ext4_msg(sb, KERN_ERR, "error: could not dup " 2282ad4eec61SEric Sandeen "journal device string"); 2283ad4eec61SEric Sandeen return -1; 2284ad4eec61SEric Sandeen } 2285ad4eec61SEric Sandeen 2286ad4eec61SEric Sandeen error = kern_path(journal_path, LOOKUP_FOLLOW, &path); 2287ad4eec61SEric Sandeen if (error) { 2288ad4eec61SEric Sandeen ext4_msg(sb, KERN_ERR, "error: could not find " 2289ad4eec61SEric Sandeen "journal device path: error %d", error); 2290ad4eec61SEric Sandeen kfree(journal_path); 2291ad4eec61SEric Sandeen return -1; 2292ad4eec61SEric Sandeen } 2293ad4eec61SEric Sandeen 22942b0143b5SDavid Howells journal_inode = d_inode(path.dentry); 2295ad4eec61SEric Sandeen if (!S_ISBLK(journal_inode->i_mode)) { 2296ad4eec61SEric Sandeen ext4_msg(sb, KERN_ERR, "error: journal path %s " 2297ad4eec61SEric Sandeen "is not a block device", journal_path); 2298ad4eec61SEric Sandeen path_put(&path); 2299ad4eec61SEric Sandeen kfree(journal_path); 2300ad4eec61SEric Sandeen return -1; 2301ad4eec61SEric Sandeen } 2302ad4eec61SEric Sandeen 2303ad4eec61SEric Sandeen *journal_devnum = new_encode_dev(journal_inode->i_rdev); 2304ad4eec61SEric Sandeen path_put(&path); 2305ad4eec61SEric Sandeen kfree(journal_path); 23060efb3b23SJan Kara } else if (token == Opt_journal_ioprio) { 23070efb3b23SJan Kara if (arg > 7) { 23085f3633e3SJan Kara ext4_msg(sb, KERN_ERR, "Invalid journal IO priority" 23090efb3b23SJan Kara " (must be 0-7)"); 23100efb3b23SJan Kara return -1; 23110efb3b23SJan Kara } 23120efb3b23SJan Kara *journal_ioprio = 23130efb3b23SJan Kara IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, arg); 23146ddb2447STheodore Ts'o } else if (token == Opt_test_dummy_encryption) { 2315ed318a6cSEric Biggers return ext4_set_test_dummy_encryption(sb, opt, &args[0], 2316ed318a6cSEric Biggers is_remount); 231726092bf5STheodore Ts'o } else if (m->flags & MOPT_DATAJ) { 231826092bf5STheodore Ts'o if (is_remount) { 231926092bf5STheodore Ts'o if (!sbi->s_journal) 232026092bf5STheodore Ts'o ext4_msg(sb, KERN_WARNING, "Remounting file system with no journal so ignoring journalled data option"); 23215f3633e3SJan Kara else if (test_opt(sb, DATA_FLAGS) != m->mount_opt) { 232226092bf5STheodore Ts'o ext4_msg(sb, KERN_ERR, 232326092bf5STheodore Ts'o "Cannot change data mode on remount"); 232426092bf5STheodore Ts'o return -1; 232526092bf5STheodore Ts'o } 232626092bf5STheodore Ts'o } else { 232726092bf5STheodore Ts'o clear_opt(sb, DATA_FLAGS); 232826092bf5STheodore Ts'o sbi->s_mount_opt |= m->mount_opt; 232926092bf5STheodore Ts'o } 233026092bf5STheodore Ts'o #ifdef CONFIG_QUOTA 233126092bf5STheodore Ts'o } else if (m->flags & MOPT_QFMT) { 233226092bf5STheodore Ts'o if (sb_any_quota_loaded(sb) && 233326092bf5STheodore Ts'o sbi->s_jquota_fmt != m->mount_opt) { 23345f3633e3SJan Kara ext4_msg(sb, KERN_ERR, "Cannot change journaled " 23355f3633e3SJan Kara "quota options when quota turned on"); 233626092bf5STheodore Ts'o return -1; 233726092bf5STheodore Ts'o } 2338e2b911c5SDarrick J. Wong if (ext4_has_feature_quota(sb)) { 2339c325a67cSTheodore Ts'o ext4_msg(sb, KERN_INFO, 2340c325a67cSTheodore Ts'o "Quota format mount options ignored " 2341262b4662SJan Kara "when QUOTA feature is enabled"); 2342c325a67cSTheodore Ts'o return 1; 2343262b4662SJan Kara } 234426092bf5STheodore Ts'o sbi->s_jquota_fmt = m->mount_opt; 234526092bf5STheodore Ts'o #endif 23469cb20f94SIra Weiny } else if (token == Opt_dax || token == Opt_dax_always || 23479cb20f94SIra Weiny token == Opt_dax_inode || token == Opt_dax_never) { 2348ef83b6e8SDan Williams #ifdef CONFIG_FS_DAX 23499cb20f94SIra Weiny switch (token) { 23509cb20f94SIra Weiny case Opt_dax: 23519cb20f94SIra Weiny case Opt_dax_always: 2352829b37b8STheodore Ts'o if (is_remount && 2353829b37b8STheodore Ts'o (!(sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) || 2354829b37b8STheodore Ts'o (sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER))) { 2355829b37b8STheodore Ts'o fail_dax_change_remount: 2356829b37b8STheodore Ts'o ext4_msg(sb, KERN_ERR, "can't change " 2357829b37b8STheodore Ts'o "dax mount option while remounting"); 2358829b37b8STheodore Ts'o return -1; 2359829b37b8STheodore Ts'o } 2360829b37b8STheodore Ts'o if (is_remount && 2361829b37b8STheodore Ts'o (test_opt(sb, DATA_FLAGS) == 2362829b37b8STheodore Ts'o EXT4_MOUNT_JOURNAL_DATA)) { 2363829b37b8STheodore Ts'o ext4_msg(sb, KERN_ERR, "can't mount with " 2364829b37b8STheodore Ts'o "both data=journal and dax"); 2365829b37b8STheodore Ts'o return -1; 2366829b37b8STheodore Ts'o } 2367ef83b6e8SDan Williams ext4_msg(sb, KERN_WARNING, 2368ef83b6e8SDan Williams "DAX enabled. Warning: EXPERIMENTAL, use at your own risk"); 23699cb20f94SIra Weiny sbi->s_mount_opt |= EXT4_MOUNT_DAX_ALWAYS; 23709cb20f94SIra Weiny sbi->s_mount_opt2 &= ~EXT4_MOUNT2_DAX_NEVER; 23719cb20f94SIra Weiny break; 23729cb20f94SIra Weiny case Opt_dax_never: 2373829b37b8STheodore Ts'o if (is_remount && 2374829b37b8STheodore Ts'o (!(sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER) || 2375829b37b8STheodore Ts'o (sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS))) 2376829b37b8STheodore Ts'o goto fail_dax_change_remount; 23779cb20f94SIra Weiny sbi->s_mount_opt2 |= EXT4_MOUNT2_DAX_NEVER; 23789cb20f94SIra Weiny sbi->s_mount_opt &= ~EXT4_MOUNT_DAX_ALWAYS; 23799cb20f94SIra Weiny break; 23809cb20f94SIra Weiny case Opt_dax_inode: 2381829b37b8STheodore Ts'o if (is_remount && 2382829b37b8STheodore Ts'o ((sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) || 2383829b37b8STheodore Ts'o (sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER) || 2384829b37b8STheodore Ts'o !(sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_INODE))) 2385829b37b8STheodore Ts'o goto fail_dax_change_remount; 23869cb20f94SIra Weiny sbi->s_mount_opt &= ~EXT4_MOUNT_DAX_ALWAYS; 23879cb20f94SIra Weiny sbi->s_mount_opt2 &= ~EXT4_MOUNT2_DAX_NEVER; 23889cb20f94SIra Weiny /* Strictly for printing options */ 23899cb20f94SIra Weiny sbi->s_mount_opt2 |= EXT4_MOUNT2_DAX_INODE; 23909cb20f94SIra Weiny break; 23919cb20f94SIra Weiny } 2392ef83b6e8SDan Williams #else 2393923ae0ffSRoss Zwisler ext4_msg(sb, KERN_INFO, "dax option not supported"); 23949cb20f94SIra Weiny sbi->s_mount_opt2 |= EXT4_MOUNT2_DAX_NEVER; 23959cb20f94SIra Weiny sbi->s_mount_opt &= ~EXT4_MOUNT_DAX_ALWAYS; 2396923ae0ffSRoss Zwisler return -1; 2397923ae0ffSRoss Zwisler #endif 23987915a861SAles Novak } else if (token == Opt_data_err_abort) { 23997915a861SAles Novak sbi->s_mount_opt |= m->mount_opt; 24007915a861SAles Novak } else if (token == Opt_data_err_ignore) { 24017915a861SAles Novak sbi->s_mount_opt &= ~m->mount_opt; 240226092bf5STheodore Ts'o } else { 240326092bf5STheodore Ts'o if (!args->from) 240426092bf5STheodore Ts'o arg = 1; 240526092bf5STheodore Ts'o if (m->flags & MOPT_CLEAR) 240626092bf5STheodore Ts'o arg = !arg; 240726092bf5STheodore Ts'o else if (unlikely(!(m->flags & MOPT_SET))) { 240826092bf5STheodore Ts'o ext4_msg(sb, KERN_WARNING, 240926092bf5STheodore Ts'o "buggy handling of option %s", opt); 241026092bf5STheodore Ts'o WARN_ON(1); 241126092bf5STheodore Ts'o return -1; 241226092bf5STheodore Ts'o } 2413995a3ed6SHarshad Shirwadkar if (m->flags & MOPT_2) { 2414995a3ed6SHarshad Shirwadkar if (arg != 0) 2415995a3ed6SHarshad Shirwadkar sbi->s_mount_opt2 |= m->mount_opt; 2416995a3ed6SHarshad Shirwadkar else 2417995a3ed6SHarshad Shirwadkar sbi->s_mount_opt2 &= ~m->mount_opt; 2418995a3ed6SHarshad Shirwadkar } else { 241926092bf5STheodore Ts'o if (arg != 0) 242026092bf5STheodore Ts'o sbi->s_mount_opt |= m->mount_opt; 242126092bf5STheodore Ts'o else 242226092bf5STheodore Ts'o sbi->s_mount_opt &= ~m->mount_opt; 242326092bf5STheodore Ts'o } 2424995a3ed6SHarshad Shirwadkar } 242526092bf5STheodore Ts'o return 1; 242626092bf5STheodore Ts'o } 242726092bf5STheodore Ts'o 2428ac27a0ecSDave Kleikamp static int parse_options(char *options, struct super_block *sb, 2429c3191067STheodore Ts'o unsigned long *journal_devnum, 2430b3881f74STheodore Ts'o unsigned int *journal_ioprio, 2431661aa520SEric Sandeen int is_remount) 2432ac27a0ecSDave Kleikamp { 24331e1a76edSOlof Johansson struct ext4_sb_info __maybe_unused *sbi = EXT4_SB(sb); 243433458eabSTheodore Ts'o char *p, __maybe_unused *usr_qf_name, __maybe_unused *grp_qf_name; 2435ac27a0ecSDave Kleikamp substring_t args[MAX_OPT_ARGS]; 243626092bf5STheodore Ts'o int token; 2437ac27a0ecSDave Kleikamp 2438ac27a0ecSDave Kleikamp if (!options) 2439ac27a0ecSDave Kleikamp return 1; 2440ac27a0ecSDave Kleikamp 2441ac27a0ecSDave Kleikamp while ((p = strsep(&options, ",")) != NULL) { 2442ac27a0ecSDave Kleikamp if (!*p) 2443ac27a0ecSDave Kleikamp continue; 244415121c18SEric Sandeen /* 244515121c18SEric Sandeen * Initialize args struct so we know whether arg was 244615121c18SEric Sandeen * found; some options take optional arguments. 244715121c18SEric Sandeen */ 2448caecd0afSSachin Kamat args[0].to = args[0].from = NULL; 2449ac27a0ecSDave Kleikamp token = match_token(p, tokens, args); 245026092bf5STheodore Ts'o if (handle_mount_opt(sb, p, token, args, journal_devnum, 245126092bf5STheodore Ts'o journal_ioprio, is_remount) < 0) 2452ac27a0ecSDave Kleikamp return 0; 2453ac27a0ecSDave Kleikamp } 2454ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 245549da9392SJan Kara /* 245649da9392SJan Kara * We do the test below only for project quotas. 'usrquota' and 245749da9392SJan Kara * 'grpquota' mount options are allowed even without quota feature 245849da9392SJan Kara * to support legacy quotas in quota files. 245949da9392SJan Kara */ 246049da9392SJan Kara if (test_opt(sb, PRJQUOTA) && !ext4_has_feature_project(sb)) { 246149da9392SJan Kara ext4_msg(sb, KERN_ERR, "Project quota feature not enabled. " 246249da9392SJan Kara "Cannot enable project quota enforcement."); 246349da9392SJan Kara return 0; 246449da9392SJan Kara } 246533458eabSTheodore Ts'o usr_qf_name = get_qf_name(sb, sbi, USRQUOTA); 246633458eabSTheodore Ts'o grp_qf_name = get_qf_name(sb, sbi, GRPQUOTA); 246733458eabSTheodore Ts'o if (usr_qf_name || grp_qf_name) { 246833458eabSTheodore Ts'o if (test_opt(sb, USRQUOTA) && usr_qf_name) 2469fd8c37ecSTheodore Ts'o clear_opt(sb, USRQUOTA); 2470ac27a0ecSDave Kleikamp 247133458eabSTheodore Ts'o if (test_opt(sb, GRPQUOTA) && grp_qf_name) 2472fd8c37ecSTheodore Ts'o clear_opt(sb, GRPQUOTA); 2473ac27a0ecSDave Kleikamp 247456c50f11SDmitry Monakhov if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) { 2475b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "old and new quota " 2476b31e1552SEric Sandeen "format mixing"); 2477ac27a0ecSDave Kleikamp return 0; 2478ac27a0ecSDave Kleikamp } 2479ac27a0ecSDave Kleikamp 2480ac27a0ecSDave Kleikamp if (!sbi->s_jquota_fmt) { 2481b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "journaled quota format " 2482b31e1552SEric Sandeen "not specified"); 2483ac27a0ecSDave Kleikamp return 0; 2484ac27a0ecSDave Kleikamp } 2485ac27a0ecSDave Kleikamp } 2486ac27a0ecSDave Kleikamp #endif 2487626b035bSRitesh Harjani if (test_opt(sb, DIOREAD_NOLOCK)) { 2488626b035bSRitesh Harjani int blocksize = 2489626b035bSRitesh Harjani BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size); 2490626b035bSRitesh Harjani if (blocksize < PAGE_SIZE) 2491626b035bSRitesh Harjani ext4_msg(sb, KERN_WARNING, "Warning: mounting with an " 2492626b035bSRitesh Harjani "experimental mount option 'dioread_nolock' " 2493626b035bSRitesh Harjani "for blocksize < PAGE_SIZE"); 2494626b035bSRitesh Harjani } 2495ac27a0ecSDave Kleikamp return 1; 2496ac27a0ecSDave Kleikamp } 2497ac27a0ecSDave Kleikamp 24982adf6da8STheodore Ts'o static inline void ext4_show_quota_options(struct seq_file *seq, 24992adf6da8STheodore Ts'o struct super_block *sb) 25002adf6da8STheodore Ts'o { 25012adf6da8STheodore Ts'o #if defined(CONFIG_QUOTA) 25022adf6da8STheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(sb); 250333458eabSTheodore Ts'o char *usr_qf_name, *grp_qf_name; 25042adf6da8STheodore Ts'o 25052adf6da8STheodore Ts'o if (sbi->s_jquota_fmt) { 25062adf6da8STheodore Ts'o char *fmtname = ""; 25072adf6da8STheodore Ts'o 25082adf6da8STheodore Ts'o switch (sbi->s_jquota_fmt) { 25092adf6da8STheodore Ts'o case QFMT_VFS_OLD: 25102adf6da8STheodore Ts'o fmtname = "vfsold"; 25112adf6da8STheodore Ts'o break; 25122adf6da8STheodore Ts'o case QFMT_VFS_V0: 25132adf6da8STheodore Ts'o fmtname = "vfsv0"; 25142adf6da8STheodore Ts'o break; 25152adf6da8STheodore Ts'o case QFMT_VFS_V1: 25162adf6da8STheodore Ts'o fmtname = "vfsv1"; 25172adf6da8STheodore Ts'o break; 25182adf6da8STheodore Ts'o } 25192adf6da8STheodore Ts'o seq_printf(seq, ",jqfmt=%s", fmtname); 25202adf6da8STheodore Ts'o } 25212adf6da8STheodore Ts'o 252233458eabSTheodore Ts'o rcu_read_lock(); 252333458eabSTheodore Ts'o usr_qf_name = rcu_dereference(sbi->s_qf_names[USRQUOTA]); 252433458eabSTheodore Ts'o grp_qf_name = rcu_dereference(sbi->s_qf_names[GRPQUOTA]); 252533458eabSTheodore Ts'o if (usr_qf_name) 252633458eabSTheodore Ts'o seq_show_option(seq, "usrjquota", usr_qf_name); 252733458eabSTheodore Ts'o if (grp_qf_name) 252833458eabSTheodore Ts'o seq_show_option(seq, "grpjquota", grp_qf_name); 252933458eabSTheodore Ts'o rcu_read_unlock(); 25302adf6da8STheodore Ts'o #endif 25312adf6da8STheodore Ts'o } 25322adf6da8STheodore Ts'o 25335a916be1STheodore Ts'o static const char *token2str(int token) 25345a916be1STheodore Ts'o { 253550df9fd5SHerton Ronaldo Krzesinski const struct match_token *t; 25365a916be1STheodore Ts'o 25375a916be1STheodore Ts'o for (t = tokens; t->token != Opt_err; t++) 25385a916be1STheodore Ts'o if (t->token == token && !strchr(t->pattern, '=')) 25395a916be1STheodore Ts'o break; 25405a916be1STheodore Ts'o return t->pattern; 25415a916be1STheodore Ts'o } 25425a916be1STheodore Ts'o 25432adf6da8STheodore Ts'o /* 25442adf6da8STheodore Ts'o * Show an option if 25452adf6da8STheodore Ts'o * - it's set to a non-default value OR 25462adf6da8STheodore Ts'o * - if the per-sb default is different from the global default 25472adf6da8STheodore Ts'o */ 254866acdcf4STheodore Ts'o static int _ext4_show_options(struct seq_file *seq, struct super_block *sb, 254966acdcf4STheodore Ts'o int nodefs) 25502adf6da8STheodore Ts'o { 25512adf6da8STheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(sb); 25522adf6da8STheodore Ts'o struct ext4_super_block *es = sbi->s_es; 255368afa7e0STyson Nottingham int def_errors, def_mount_opt = sbi->s_def_mount_opt; 25545a916be1STheodore Ts'o const struct mount_opts *m; 255566acdcf4STheodore Ts'o char sep = nodefs ? '\n' : ','; 25562adf6da8STheodore Ts'o 255766acdcf4STheodore Ts'o #define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep) 255866acdcf4STheodore Ts'o #define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg) 25592adf6da8STheodore Ts'o 25602adf6da8STheodore Ts'o if (sbi->s_sb_block != 1) 25615a916be1STheodore Ts'o SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block); 25625a916be1STheodore Ts'o 25635a916be1STheodore Ts'o for (m = ext4_mount_opts; m->token != Opt_err; m++) { 25645a916be1STheodore Ts'o int want_set = m->flags & MOPT_SET; 25655a916be1STheodore Ts'o if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) || 25669cb20f94SIra Weiny (m->flags & MOPT_CLEAR_ERR) || m->flags & MOPT_SKIP) 25675a916be1STheodore Ts'o continue; 256868afa7e0STyson Nottingham if (!nodefs && !(m->mount_opt & (sbi->s_mount_opt ^ def_mount_opt))) 25695a916be1STheodore Ts'o continue; /* skip if same as the default */ 25705a916be1STheodore Ts'o if ((want_set && 25715a916be1STheodore Ts'o (sbi->s_mount_opt & m->mount_opt) != m->mount_opt) || 25725a916be1STheodore Ts'o (!want_set && (sbi->s_mount_opt & m->mount_opt))) 25735a916be1STheodore Ts'o continue; /* select Opt_noFoo vs Opt_Foo */ 25745a916be1STheodore Ts'o SEQ_OPTS_PRINT("%s", token2str(m->token)); 25755a916be1STheodore Ts'o } 25765a916be1STheodore Ts'o 257708cefc7aSEric W. Biederman if (nodefs || !uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT4_DEF_RESUID)) || 25785a916be1STheodore Ts'o le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) 257908cefc7aSEric W. Biederman SEQ_OPTS_PRINT("resuid=%u", 258008cefc7aSEric W. Biederman from_kuid_munged(&init_user_ns, sbi->s_resuid)); 258108cefc7aSEric W. Biederman if (nodefs || !gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT4_DEF_RESGID)) || 25825a916be1STheodore Ts'o le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) 258308cefc7aSEric W. Biederman SEQ_OPTS_PRINT("resgid=%u", 258408cefc7aSEric W. Biederman from_kgid_munged(&init_user_ns, sbi->s_resgid)); 258566acdcf4STheodore Ts'o def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors); 25865a916be1STheodore Ts'o if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO) 25875a916be1STheodore Ts'o SEQ_OPTS_PUTS("errors=remount-ro"); 25882adf6da8STheodore Ts'o if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE) 25895a916be1STheodore Ts'o SEQ_OPTS_PUTS("errors=continue"); 25902adf6da8STheodore Ts'o if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC) 25915a916be1STheodore Ts'o SEQ_OPTS_PUTS("errors=panic"); 259266acdcf4STheodore Ts'o if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) 25935a916be1STheodore Ts'o SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ); 259466acdcf4STheodore Ts'o if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME) 25955a916be1STheodore Ts'o SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time); 259666acdcf4STheodore Ts'o if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME) 25975a916be1STheodore Ts'o SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time); 2598357fdad0SMatthew Garrett if (sb->s_flags & SB_I_VERSION) 25995a916be1STheodore Ts'o SEQ_OPTS_PUTS("i_version"); 260066acdcf4STheodore Ts'o if (nodefs || sbi->s_stripe) 26015a916be1STheodore Ts'o SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe); 260268afa7e0STyson Nottingham if (nodefs || EXT4_MOUNT_DATA_FLAGS & 260368afa7e0STyson Nottingham (sbi->s_mount_opt ^ def_mount_opt)) { 26042adf6da8STheodore Ts'o if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) 26055a916be1STheodore Ts'o SEQ_OPTS_PUTS("data=journal"); 26062adf6da8STheodore Ts'o else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) 26075a916be1STheodore Ts'o SEQ_OPTS_PUTS("data=ordered"); 26082adf6da8STheodore Ts'o else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA) 26095a916be1STheodore Ts'o SEQ_OPTS_PUTS("data=writeback"); 26105a916be1STheodore Ts'o } 261166acdcf4STheodore Ts'o if (nodefs || 261266acdcf4STheodore Ts'o sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS) 26135a916be1STheodore Ts'o SEQ_OPTS_PRINT("inode_readahead_blks=%u", 26142adf6da8STheodore Ts'o sbi->s_inode_readahead_blks); 26152adf6da8STheodore Ts'o 2616ceec0376STyson Nottingham if (test_opt(sb, INIT_INODE_TABLE) && (nodefs || 261766acdcf4STheodore Ts'o (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT))) 26185a916be1STheodore Ts'o SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult); 2619df981d03STheodore Ts'o if (nodefs || sbi->s_max_dir_size_kb) 2620df981d03STheodore Ts'o SEQ_OPTS_PRINT("max_dir_size_kb=%u", sbi->s_max_dir_size_kb); 26217915a861SAles Novak if (test_opt(sb, DATA_ERR_ABORT)) 26227915a861SAles Novak SEQ_OPTS_PUTS("data_err=abort"); 2623ed318a6cSEric Biggers 2624ed318a6cSEric Biggers fscrypt_show_test_dummy_encryption(seq, sep, sb); 26252adf6da8STheodore Ts'o 26264f74d15fSEric Biggers if (sb->s_flags & SB_INLINECRYPT) 26274f74d15fSEric Biggers SEQ_OPTS_PUTS("inlinecrypt"); 26284f74d15fSEric Biggers 26299cb20f94SIra Weiny if (test_opt(sb, DAX_ALWAYS)) { 26309cb20f94SIra Weiny if (IS_EXT2_SB(sb)) 26319cb20f94SIra Weiny SEQ_OPTS_PUTS("dax"); 26329cb20f94SIra Weiny else 26339cb20f94SIra Weiny SEQ_OPTS_PUTS("dax=always"); 26349cb20f94SIra Weiny } else if (test_opt2(sb, DAX_NEVER)) { 26359cb20f94SIra Weiny SEQ_OPTS_PUTS("dax=never"); 26369cb20f94SIra Weiny } else if (test_opt2(sb, DAX_INODE)) { 26379cb20f94SIra Weiny SEQ_OPTS_PUTS("dax=inode"); 26389cb20f94SIra Weiny } 26399cb20f94SIra Weiny 2640995a3ed6SHarshad Shirwadkar if (test_opt2(sb, JOURNAL_FAST_COMMIT)) 2641995a3ed6SHarshad Shirwadkar SEQ_OPTS_PUTS("fast_commit"); 2642995a3ed6SHarshad Shirwadkar 26432adf6da8STheodore Ts'o ext4_show_quota_options(seq, sb); 26442adf6da8STheodore Ts'o return 0; 26452adf6da8STheodore Ts'o } 26462adf6da8STheodore Ts'o 264766acdcf4STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root) 264866acdcf4STheodore Ts'o { 264966acdcf4STheodore Ts'o return _ext4_show_options(seq, root->d_sb, 0); 265066acdcf4STheodore Ts'o } 265166acdcf4STheodore Ts'o 2652ebd173beSTheodore Ts'o int ext4_seq_options_show(struct seq_file *seq, void *offset) 265366acdcf4STheodore Ts'o { 265466acdcf4STheodore Ts'o struct super_block *sb = seq->private; 265566acdcf4STheodore Ts'o int rc; 265666acdcf4STheodore Ts'o 2657bc98a42cSDavid Howells seq_puts(seq, sb_rdonly(sb) ? "ro" : "rw"); 265866acdcf4STheodore Ts'o rc = _ext4_show_options(seq, sb, 1); 265966acdcf4STheodore Ts'o seq_puts(seq, "\n"); 266066acdcf4STheodore Ts'o return rc; 266166acdcf4STheodore Ts'o } 266266acdcf4STheodore Ts'o 2663617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es, 2664ac27a0ecSDave Kleikamp int read_only) 2665ac27a0ecSDave Kleikamp { 2666617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 2667c89128a0SJaegeuk Kim int err = 0; 2668ac27a0ecSDave Kleikamp 2669617ba13bSMingming Cao if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) { 2670b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "revision level too high, " 2671b31e1552SEric Sandeen "forcing read-only mode"); 2672c89128a0SJaegeuk Kim err = -EROFS; 26735adaccacSyangerkun goto done; 2674ac27a0ecSDave Kleikamp } 2675ac27a0ecSDave Kleikamp if (read_only) 2676281b5995STheodore Ts'o goto done; 2677617ba13bSMingming Cao if (!(sbi->s_mount_state & EXT4_VALID_FS)) 2678b31e1552SEric Sandeen ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, " 2679b31e1552SEric Sandeen "running e2fsck is recommended"); 2680c8b459f4SLukas Czerner else if (sbi->s_mount_state & EXT4_ERROR_FS) 2681b31e1552SEric Sandeen ext4_msg(sb, KERN_WARNING, 2682b31e1552SEric Sandeen "warning: mounting fs with errors, " 2683b31e1552SEric Sandeen "running e2fsck is recommended"); 2684ed3ce80aSTao Ma else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 && 2685ac27a0ecSDave Kleikamp le16_to_cpu(es->s_mnt_count) >= 2686ac27a0ecSDave Kleikamp (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count)) 2687b31e1552SEric Sandeen ext4_msg(sb, KERN_WARNING, 2688b31e1552SEric Sandeen "warning: maximal mount count reached, " 2689b31e1552SEric Sandeen "running e2fsck is recommended"); 2690ac27a0ecSDave Kleikamp else if (le32_to_cpu(es->s_checkinterval) && 26916a0678a7SArnd Bergmann (ext4_get_tstamp(es, s_lastcheck) + 26926a0678a7SArnd Bergmann le32_to_cpu(es->s_checkinterval) <= ktime_get_real_seconds())) 2693b31e1552SEric Sandeen ext4_msg(sb, KERN_WARNING, 2694b31e1552SEric Sandeen "warning: checktime reached, " 2695b31e1552SEric Sandeen "running e2fsck is recommended"); 26960390131bSFrank Mayhar if (!sbi->s_journal) 2697216c34b2SMarcin Slusarz es->s_state &= cpu_to_le16(~EXT4_VALID_FS); 2698ac27a0ecSDave Kleikamp if (!(__s16) le16_to_cpu(es->s_max_mnt_count)) 2699617ba13bSMingming Cao es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT); 2700e8546d06SMarcin Slusarz le16_add_cpu(&es->s_mnt_count, 1); 27016a0678a7SArnd Bergmann ext4_update_tstamp(es, s_mtime); 27020390131bSFrank Mayhar if (sbi->s_journal) 2703e2b911c5SDarrick J. Wong ext4_set_feature_journal_needs_recovery(sb); 2704ac27a0ecSDave Kleikamp 2705c89128a0SJaegeuk Kim err = ext4_commit_super(sb, 1); 2706281b5995STheodore Ts'o done: 2707ac27a0ecSDave Kleikamp if (test_opt(sb, DEBUG)) 2708a9df9a49STheodore Ts'o printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, " 2709a2595b8aSTheodore Ts'o "bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n", 2710ac27a0ecSDave Kleikamp sb->s_blocksize, 2711ac27a0ecSDave Kleikamp sbi->s_groups_count, 2712617ba13bSMingming Cao EXT4_BLOCKS_PER_GROUP(sb), 2713617ba13bSMingming Cao EXT4_INODES_PER_GROUP(sb), 2714a2595b8aSTheodore Ts'o sbi->s_mount_opt, sbi->s_mount_opt2); 2715ac27a0ecSDave Kleikamp 27167abc52c2SDan Magenheimer cleancache_init_fs(sb); 2717c89128a0SJaegeuk Kim return err; 2718ac27a0ecSDave Kleikamp } 2719ac27a0ecSDave Kleikamp 2720117fff10STheodore Ts'o int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup) 2721117fff10STheodore Ts'o { 2722117fff10STheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(sb); 27237c990728SSuraj Jitindar Singh struct flex_groups **old_groups, **new_groups; 272437b0b6b8SDan Carpenter int size, i, j; 2725117fff10STheodore Ts'o 2726117fff10STheodore Ts'o if (!sbi->s_log_groups_per_flex) 2727117fff10STheodore Ts'o return 0; 2728117fff10STheodore Ts'o 2729117fff10STheodore Ts'o size = ext4_flex_group(sbi, ngroup - 1) + 1; 2730117fff10STheodore Ts'o if (size <= sbi->s_flex_groups_allocated) 2731117fff10STheodore Ts'o return 0; 2732117fff10STheodore Ts'o 27337c990728SSuraj Jitindar Singh new_groups = kvzalloc(roundup_pow_of_two(size * 27347c990728SSuraj Jitindar Singh sizeof(*sbi->s_flex_groups)), GFP_KERNEL); 2735117fff10STheodore Ts'o if (!new_groups) { 27367c990728SSuraj Jitindar Singh ext4_msg(sb, KERN_ERR, 27377c990728SSuraj Jitindar Singh "not enough memory for %d flex group pointers", size); 2738117fff10STheodore Ts'o return -ENOMEM; 2739117fff10STheodore Ts'o } 27407c990728SSuraj Jitindar Singh for (i = sbi->s_flex_groups_allocated; i < size; i++) { 27417c990728SSuraj Jitindar Singh new_groups[i] = kvzalloc(roundup_pow_of_two( 27427c990728SSuraj Jitindar Singh sizeof(struct flex_groups)), 27437c990728SSuraj Jitindar Singh GFP_KERNEL); 27447c990728SSuraj Jitindar Singh if (!new_groups[i]) { 274537b0b6b8SDan Carpenter for (j = sbi->s_flex_groups_allocated; j < i; j++) 274637b0b6b8SDan Carpenter kvfree(new_groups[j]); 27477c990728SSuraj Jitindar Singh kvfree(new_groups); 27487c990728SSuraj Jitindar Singh ext4_msg(sb, KERN_ERR, 27497c990728SSuraj Jitindar Singh "not enough memory for %d flex groups", size); 27507c990728SSuraj Jitindar Singh return -ENOMEM; 2751117fff10STheodore Ts'o } 27527c990728SSuraj Jitindar Singh } 27537c990728SSuraj Jitindar Singh rcu_read_lock(); 27547c990728SSuraj Jitindar Singh old_groups = rcu_dereference(sbi->s_flex_groups); 27557c990728SSuraj Jitindar Singh if (old_groups) 27567c990728SSuraj Jitindar Singh memcpy(new_groups, old_groups, 27577c990728SSuraj Jitindar Singh (sbi->s_flex_groups_allocated * 27587c990728SSuraj Jitindar Singh sizeof(struct flex_groups *))); 27597c990728SSuraj Jitindar Singh rcu_read_unlock(); 27607c990728SSuraj Jitindar Singh rcu_assign_pointer(sbi->s_flex_groups, new_groups); 27617c990728SSuraj Jitindar Singh sbi->s_flex_groups_allocated = size; 27627c990728SSuraj Jitindar Singh if (old_groups) 27637c990728SSuraj Jitindar Singh ext4_kvfree_array_rcu(old_groups); 2764117fff10STheodore Ts'o return 0; 2765117fff10STheodore Ts'o } 2766117fff10STheodore Ts'o 2767772cb7c8SJose R. Santos static int ext4_fill_flex_info(struct super_block *sb) 2768772cb7c8SJose R. Santos { 2769772cb7c8SJose R. Santos struct ext4_sb_info *sbi = EXT4_SB(sb); 2770772cb7c8SJose R. Santos struct ext4_group_desc *gdp = NULL; 27717c990728SSuraj Jitindar Singh struct flex_groups *fg; 2772772cb7c8SJose R. Santos ext4_group_t flex_group; 2773117fff10STheodore Ts'o int i, err; 2774772cb7c8SJose R. Santos 2775503358aeSTheodore Ts'o sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex; 2776d50f2ab6SXi Wang if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) { 2777772cb7c8SJose R. Santos sbi->s_log_groups_per_flex = 0; 2778772cb7c8SJose R. Santos return 1; 2779772cb7c8SJose R. Santos } 2780772cb7c8SJose R. Santos 2781117fff10STheodore Ts'o err = ext4_alloc_flex_bg_array(sb, sbi->s_groups_count); 2782117fff10STheodore Ts'o if (err) 2783772cb7c8SJose R. Santos goto failed; 2784772cb7c8SJose R. Santos 2785772cb7c8SJose R. Santos for (i = 0; i < sbi->s_groups_count; i++) { 278688b6edd1STheodore Ts'o gdp = ext4_get_group_desc(sb, i, NULL); 2787772cb7c8SJose R. Santos 2788772cb7c8SJose R. Santos flex_group = ext4_flex_group(sbi, i); 27897c990728SSuraj Jitindar Singh fg = sbi_array_rcu_deref(sbi, s_flex_groups, flex_group); 27907c990728SSuraj Jitindar Singh atomic_add(ext4_free_inodes_count(sb, gdp), &fg->free_inodes); 279190ba983fSTheodore Ts'o atomic64_add(ext4_free_group_clusters(sb, gdp), 27927c990728SSuraj Jitindar Singh &fg->free_clusters); 27937c990728SSuraj Jitindar Singh atomic_add(ext4_used_dirs_count(sb, gdp), &fg->used_dirs); 2794772cb7c8SJose R. Santos } 2795772cb7c8SJose R. Santos 2796772cb7c8SJose R. Santos return 1; 2797772cb7c8SJose R. Santos failed: 2798772cb7c8SJose R. Santos return 0; 2799772cb7c8SJose R. Santos } 2800772cb7c8SJose R. Santos 2801e2b911c5SDarrick J. Wong static __le16 ext4_group_desc_csum(struct super_block *sb, __u32 block_group, 2802717d50e4SAndreas Dilger struct ext4_group_desc *gdp) 2803717d50e4SAndreas Dilger { 2804b47820edSDaeho Jeong int offset = offsetof(struct ext4_group_desc, bg_checksum); 2805717d50e4SAndreas Dilger __u16 crc = 0; 2806717d50e4SAndreas Dilger __le32 le_group = cpu_to_le32(block_group); 2807e2b911c5SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(sb); 2808717d50e4SAndreas Dilger 28099aa5d32bSDmitry Monakhov if (ext4_has_metadata_csum(sbi->s_sb)) { 2810feb0ab32SDarrick J. Wong /* Use new metadata_csum algorithm */ 2811feb0ab32SDarrick J. Wong __u32 csum32; 2812b47820edSDaeho Jeong __u16 dummy_csum = 0; 2813feb0ab32SDarrick J. Wong 2814feb0ab32SDarrick J. Wong csum32 = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&le_group, 2815feb0ab32SDarrick J. Wong sizeof(le_group)); 2816b47820edSDaeho Jeong csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp, offset); 2817b47820edSDaeho Jeong csum32 = ext4_chksum(sbi, csum32, (__u8 *)&dummy_csum, 2818b47820edSDaeho Jeong sizeof(dummy_csum)); 2819b47820edSDaeho Jeong offset += sizeof(dummy_csum); 2820b47820edSDaeho Jeong if (offset < sbi->s_desc_size) 2821b47820edSDaeho Jeong csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp + offset, 2822b47820edSDaeho Jeong sbi->s_desc_size - offset); 2823feb0ab32SDarrick J. Wong 2824feb0ab32SDarrick J. Wong crc = csum32 & 0xFFFF; 2825feb0ab32SDarrick J. Wong goto out; 2826feb0ab32SDarrick J. Wong } 2827feb0ab32SDarrick J. Wong 2828feb0ab32SDarrick J. Wong /* old crc16 code */ 2829e2b911c5SDarrick J. Wong if (!ext4_has_feature_gdt_csum(sb)) 2830813d32f9SDarrick J. Wong return 0; 2831813d32f9SDarrick J. Wong 2832717d50e4SAndreas Dilger crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid)); 2833717d50e4SAndreas Dilger crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group)); 2834717d50e4SAndreas Dilger crc = crc16(crc, (__u8 *)gdp, offset); 2835717d50e4SAndreas Dilger offset += sizeof(gdp->bg_checksum); /* skip checksum */ 2836717d50e4SAndreas Dilger /* for checksum of struct ext4_group_desc do the rest...*/ 2837e2b911c5SDarrick J. Wong if (ext4_has_feature_64bit(sb) && 2838717d50e4SAndreas Dilger offset < le16_to_cpu(sbi->s_es->s_desc_size)) 2839717d50e4SAndreas Dilger crc = crc16(crc, (__u8 *)gdp + offset, 2840717d50e4SAndreas Dilger le16_to_cpu(sbi->s_es->s_desc_size) - 2841717d50e4SAndreas Dilger offset); 2842717d50e4SAndreas Dilger 2843feb0ab32SDarrick J. Wong out: 2844717d50e4SAndreas Dilger return cpu_to_le16(crc); 2845717d50e4SAndreas Dilger } 2846717d50e4SAndreas Dilger 2847feb0ab32SDarrick J. Wong int ext4_group_desc_csum_verify(struct super_block *sb, __u32 block_group, 2848717d50e4SAndreas Dilger struct ext4_group_desc *gdp) 2849717d50e4SAndreas Dilger { 2850feb0ab32SDarrick J. Wong if (ext4_has_group_desc_csum(sb) && 2851e2b911c5SDarrick J. Wong (gdp->bg_checksum != ext4_group_desc_csum(sb, block_group, gdp))) 2852717d50e4SAndreas Dilger return 0; 2853717d50e4SAndreas Dilger 2854717d50e4SAndreas Dilger return 1; 2855717d50e4SAndreas Dilger } 2856717d50e4SAndreas Dilger 2857feb0ab32SDarrick J. Wong void ext4_group_desc_csum_set(struct super_block *sb, __u32 block_group, 2858feb0ab32SDarrick J. Wong struct ext4_group_desc *gdp) 2859feb0ab32SDarrick J. Wong { 2860feb0ab32SDarrick J. Wong if (!ext4_has_group_desc_csum(sb)) 2861feb0ab32SDarrick J. Wong return; 2862e2b911c5SDarrick J. Wong gdp->bg_checksum = ext4_group_desc_csum(sb, block_group, gdp); 2863feb0ab32SDarrick J. Wong } 2864feb0ab32SDarrick J. Wong 2865ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */ 2866bfff6873SLukas Czerner static int ext4_check_descriptors(struct super_block *sb, 2867829fa70dSTheodore Ts'o ext4_fsblk_t sb_block, 2868bfff6873SLukas Czerner ext4_group_t *first_not_zeroed) 2869ac27a0ecSDave Kleikamp { 2870617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 2871617ba13bSMingming Cao ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block); 2872617ba13bSMingming Cao ext4_fsblk_t last_block; 287344de022cSTheodore Ts'o ext4_fsblk_t last_bg_block = sb_block + ext4_bg_num_gdb(sb, 0); 2874bd81d8eeSLaurent Vivier ext4_fsblk_t block_bitmap; 2875bd81d8eeSLaurent Vivier ext4_fsblk_t inode_bitmap; 2876bd81d8eeSLaurent Vivier ext4_fsblk_t inode_table; 2877ce421581SJose R. Santos int flexbg_flag = 0; 2878bfff6873SLukas Czerner ext4_group_t i, grp = sbi->s_groups_count; 2879ac27a0ecSDave Kleikamp 2880e2b911c5SDarrick J. Wong if (ext4_has_feature_flex_bg(sb)) 2881ce421581SJose R. Santos flexbg_flag = 1; 2882ce421581SJose R. Santos 2883617ba13bSMingming Cao ext4_debug("Checking group descriptors"); 2884ac27a0ecSDave Kleikamp 2885197cd65aSAkinobu Mita for (i = 0; i < sbi->s_groups_count; i++) { 2886197cd65aSAkinobu Mita struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL); 2887197cd65aSAkinobu Mita 2888ce421581SJose R. Santos if (i == sbi->s_groups_count - 1 || flexbg_flag) 2889bd81d8eeSLaurent Vivier last_block = ext4_blocks_count(sbi->s_es) - 1; 2890ac27a0ecSDave Kleikamp else 2891ac27a0ecSDave Kleikamp last_block = first_block + 2892617ba13bSMingming Cao (EXT4_BLOCKS_PER_GROUP(sb) - 1); 2893ac27a0ecSDave Kleikamp 2894bfff6873SLukas Czerner if ((grp == sbi->s_groups_count) && 2895bfff6873SLukas Czerner !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED))) 2896bfff6873SLukas Czerner grp = i; 2897bfff6873SLukas Czerner 28988fadc143SAlexandre Ratchov block_bitmap = ext4_block_bitmap(sb, gdp); 2899829fa70dSTheodore Ts'o if (block_bitmap == sb_block) { 2900829fa70dSTheodore Ts'o ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " 2901829fa70dSTheodore Ts'o "Block bitmap for group %u overlaps " 2902829fa70dSTheodore Ts'o "superblock", i); 290318db4b4eSTheodore Ts'o if (!sb_rdonly(sb)) 290418db4b4eSTheodore Ts'o return 0; 2905829fa70dSTheodore Ts'o } 290677260807STheodore Ts'o if (block_bitmap >= sb_block + 1 && 290777260807STheodore Ts'o block_bitmap <= last_bg_block) { 290877260807STheodore Ts'o ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " 290977260807STheodore Ts'o "Block bitmap for group %u overlaps " 291077260807STheodore Ts'o "block group descriptors", i); 291177260807STheodore Ts'o if (!sb_rdonly(sb)) 291277260807STheodore Ts'o return 0; 291377260807STheodore Ts'o } 29142b2d6d01STheodore Ts'o if (block_bitmap < first_block || block_bitmap > last_block) { 2915b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " 2916a9df9a49STheodore Ts'o "Block bitmap for group %u not in group " 2917b31e1552SEric Sandeen "(block %llu)!", i, block_bitmap); 2918ac27a0ecSDave Kleikamp return 0; 2919ac27a0ecSDave Kleikamp } 29208fadc143SAlexandre Ratchov inode_bitmap = ext4_inode_bitmap(sb, gdp); 2921829fa70dSTheodore Ts'o if (inode_bitmap == sb_block) { 2922829fa70dSTheodore Ts'o ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " 2923829fa70dSTheodore Ts'o "Inode bitmap for group %u overlaps " 2924829fa70dSTheodore Ts'o "superblock", i); 292518db4b4eSTheodore Ts'o if (!sb_rdonly(sb)) 292618db4b4eSTheodore Ts'o return 0; 2927829fa70dSTheodore Ts'o } 292877260807STheodore Ts'o if (inode_bitmap >= sb_block + 1 && 292977260807STheodore Ts'o inode_bitmap <= last_bg_block) { 293077260807STheodore Ts'o ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " 293177260807STheodore Ts'o "Inode bitmap for group %u overlaps " 293277260807STheodore Ts'o "block group descriptors", i); 293377260807STheodore Ts'o if (!sb_rdonly(sb)) 293477260807STheodore Ts'o return 0; 293577260807STheodore Ts'o } 29362b2d6d01STheodore Ts'o if (inode_bitmap < first_block || inode_bitmap > last_block) { 2937b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " 2938a9df9a49STheodore Ts'o "Inode bitmap for group %u not in group " 2939b31e1552SEric Sandeen "(block %llu)!", i, inode_bitmap); 2940ac27a0ecSDave Kleikamp return 0; 2941ac27a0ecSDave Kleikamp } 29428fadc143SAlexandre Ratchov inode_table = ext4_inode_table(sb, gdp); 2943829fa70dSTheodore Ts'o if (inode_table == sb_block) { 2944829fa70dSTheodore Ts'o ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " 2945829fa70dSTheodore Ts'o "Inode table for group %u overlaps " 2946829fa70dSTheodore Ts'o "superblock", i); 294718db4b4eSTheodore Ts'o if (!sb_rdonly(sb)) 294818db4b4eSTheodore Ts'o return 0; 2949829fa70dSTheodore Ts'o } 295077260807STheodore Ts'o if (inode_table >= sb_block + 1 && 295177260807STheodore Ts'o inode_table <= last_bg_block) { 295277260807STheodore Ts'o ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " 295377260807STheodore Ts'o "Inode table for group %u overlaps " 295477260807STheodore Ts'o "block group descriptors", i); 295577260807STheodore Ts'o if (!sb_rdonly(sb)) 295677260807STheodore Ts'o return 0; 295777260807STheodore Ts'o } 2958bd81d8eeSLaurent Vivier if (inode_table < first_block || 29592b2d6d01STheodore Ts'o inode_table + sbi->s_itb_per_group - 1 > last_block) { 2960b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " 2961a9df9a49STheodore Ts'o "Inode table for group %u not in group " 2962b31e1552SEric Sandeen "(block %llu)!", i, inode_table); 2963ac27a0ecSDave Kleikamp return 0; 2964ac27a0ecSDave Kleikamp } 2965955ce5f5SAneesh Kumar K.V ext4_lock_group(sb, i); 2966feb0ab32SDarrick J. Wong if (!ext4_group_desc_csum_verify(sb, i, gdp)) { 2967b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " 2968b31e1552SEric Sandeen "Checksum for group %u failed (%u!=%u)", 2969e2b911c5SDarrick J. Wong i, le16_to_cpu(ext4_group_desc_csum(sb, i, 2970fd2d4291SAvantika Mathur gdp)), le16_to_cpu(gdp->bg_checksum)); 2971bc98a42cSDavid Howells if (!sb_rdonly(sb)) { 2972955ce5f5SAneesh Kumar K.V ext4_unlock_group(sb, i); 2973717d50e4SAndreas Dilger return 0; 2974717d50e4SAndreas Dilger } 29757ee1ec4cSLi Zefan } 2976955ce5f5SAneesh Kumar K.V ext4_unlock_group(sb, i); 2977ce421581SJose R. Santos if (!flexbg_flag) 2978617ba13bSMingming Cao first_block += EXT4_BLOCKS_PER_GROUP(sb); 2979ac27a0ecSDave Kleikamp } 2980bfff6873SLukas Czerner if (NULL != first_not_zeroed) 2981bfff6873SLukas Czerner *first_not_zeroed = grp; 2982ac27a0ecSDave Kleikamp return 1; 2983ac27a0ecSDave Kleikamp } 2984ac27a0ecSDave Kleikamp 2985617ba13bSMingming Cao /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at 2986ac27a0ecSDave Kleikamp * the superblock) which were deleted from all directories, but held open by 2987ac27a0ecSDave Kleikamp * a process at the time of a crash. We walk the list and try to delete these 2988ac27a0ecSDave Kleikamp * inodes at recovery time (only with a read-write filesystem). 2989ac27a0ecSDave Kleikamp * 2990ac27a0ecSDave Kleikamp * In order to keep the orphan inode chain consistent during traversal (in 2991ac27a0ecSDave Kleikamp * case of crash during recovery), we link each inode into the superblock 2992ac27a0ecSDave Kleikamp * orphan list_head and handle it the same way as an inode deletion during 2993ac27a0ecSDave Kleikamp * normal operation (which journals the operations for us). 2994ac27a0ecSDave Kleikamp * 2995ac27a0ecSDave Kleikamp * We only do an iget() and an iput() on each inode, which is very safe if we 2996ac27a0ecSDave Kleikamp * accidentally point at an in-use or already deleted inode. The worst that 2997ac27a0ecSDave Kleikamp * can happen in this case is that we get a "bit already cleared" message from 2998617ba13bSMingming Cao * ext4_free_inode(). The only reason we would point at a wrong inode is if 2999ac27a0ecSDave Kleikamp * e2fsck was run on this filesystem, and it must have already done the orphan 3000ac27a0ecSDave Kleikamp * inode cleanup for us, so we can safely abort without any further action. 3001ac27a0ecSDave Kleikamp */ 3002617ba13bSMingming Cao static void ext4_orphan_cleanup(struct super_block *sb, 3003617ba13bSMingming Cao struct ext4_super_block *es) 3004ac27a0ecSDave Kleikamp { 3005ac27a0ecSDave Kleikamp unsigned int s_flags = sb->s_flags; 30062c98eb5eSTheodore Ts'o int ret, nr_orphans = 0, nr_truncates = 0; 3007ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 300895f1fda4Szhangyi (F) int quota_update = 0; 3009ac27a0ecSDave Kleikamp int i; 3010ac27a0ecSDave Kleikamp #endif 3011ac27a0ecSDave Kleikamp if (!es->s_last_orphan) { 3012ac27a0ecSDave Kleikamp jbd_debug(4, "no orphan inodes to clean up\n"); 3013ac27a0ecSDave Kleikamp return; 3014ac27a0ecSDave Kleikamp } 3015ac27a0ecSDave Kleikamp 3016a8f48a95SEric Sandeen if (bdev_read_only(sb->s_bdev)) { 3017b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "write access " 3018b31e1552SEric Sandeen "unavailable, skipping orphan cleanup"); 3019a8f48a95SEric Sandeen return; 3020a8f48a95SEric Sandeen } 3021a8f48a95SEric Sandeen 3022d39195c3SAmir Goldstein /* Check if feature set would not allow a r/w mount */ 3023d39195c3SAmir Goldstein if (!ext4_feature_set_ok(sb, 0)) { 3024d39195c3SAmir Goldstein ext4_msg(sb, KERN_INFO, "Skipping orphan cleanup due to " 3025d39195c3SAmir Goldstein "unknown ROCOMPAT features"); 3026d39195c3SAmir Goldstein return; 3027d39195c3SAmir Goldstein } 3028d39195c3SAmir Goldstein 3029617ba13bSMingming Cao if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) { 3030c25f9bc6SEric Sandeen /* don't clear list on RO mount w/ errors */ 30311751e8a6SLinus Torvalds if (es->s_last_orphan && !(s_flags & SB_RDONLY)) { 303284474976SDmitry Monakhov ext4_msg(sb, KERN_INFO, "Errors on filesystem, " 3033ac27a0ecSDave Kleikamp "clearing orphan list.\n"); 3034ac27a0ecSDave Kleikamp es->s_last_orphan = 0; 3035c25f9bc6SEric Sandeen } 3036ac27a0ecSDave Kleikamp jbd_debug(1, "Skipping orphan recovery on fs with errors.\n"); 3037ac27a0ecSDave Kleikamp return; 3038ac27a0ecSDave Kleikamp } 3039ac27a0ecSDave Kleikamp 30401751e8a6SLinus Torvalds if (s_flags & SB_RDONLY) { 3041b31e1552SEric Sandeen ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs"); 30421751e8a6SLinus Torvalds sb->s_flags &= ~SB_RDONLY; 3043ac27a0ecSDave Kleikamp } 3044ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 3045ac27a0ecSDave Kleikamp /* Needed for iput() to work correctly and not trash data */ 30461751e8a6SLinus Torvalds sb->s_flags |= SB_ACTIVE; 304795f1fda4Szhangyi (F) 304895f1fda4Szhangyi (F) /* 304995f1fda4Szhangyi (F) * Turn on quotas which were not enabled for read-only mounts if 305095f1fda4Szhangyi (F) * filesystem has quota feature, so that they are updated correctly. 305195f1fda4Szhangyi (F) */ 30521751e8a6SLinus Torvalds if (ext4_has_feature_quota(sb) && (s_flags & SB_RDONLY)) { 305395f1fda4Szhangyi (F) int ret = ext4_enable_quotas(sb); 305495f1fda4Szhangyi (F) 305595f1fda4Szhangyi (F) if (!ret) 305695f1fda4Szhangyi (F) quota_update = 1; 305795f1fda4Szhangyi (F) else 305895f1fda4Szhangyi (F) ext4_msg(sb, KERN_ERR, 305995f1fda4Szhangyi (F) "Cannot turn on quotas: error %d", ret); 306095f1fda4Szhangyi (F) } 306195f1fda4Szhangyi (F) 306295f1fda4Szhangyi (F) /* Turn on journaled quotas used for old sytle */ 3063a2d4a646SJan Kara for (i = 0; i < EXT4_MAXQUOTAS; i++) { 3064617ba13bSMingming Cao if (EXT4_SB(sb)->s_qf_names[i]) { 3065617ba13bSMingming Cao int ret = ext4_quota_on_mount(sb, i); 306695f1fda4Szhangyi (F) 306795f1fda4Szhangyi (F) if (!ret) 306895f1fda4Szhangyi (F) quota_update = 1; 306995f1fda4Szhangyi (F) else 3070b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, 3071b31e1552SEric Sandeen "Cannot turn on journaled " 307295f1fda4Szhangyi (F) "quota: type %d: error %d", i, ret); 3073ac27a0ecSDave Kleikamp } 3074ac27a0ecSDave Kleikamp } 3075ac27a0ecSDave Kleikamp #endif 3076ac27a0ecSDave Kleikamp 3077ac27a0ecSDave Kleikamp while (es->s_last_orphan) { 3078ac27a0ecSDave Kleikamp struct inode *inode; 3079ac27a0ecSDave Kleikamp 3080c65d5c6cSVegard Nossum /* 3081c65d5c6cSVegard Nossum * We may have encountered an error during cleanup; if 3082c65d5c6cSVegard Nossum * so, skip the rest. 3083c65d5c6cSVegard Nossum */ 3084c65d5c6cSVegard Nossum if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) { 3085c65d5c6cSVegard Nossum jbd_debug(1, "Skipping orphan recovery on fs with errors.\n"); 3086c65d5c6cSVegard Nossum es->s_last_orphan = 0; 3087c65d5c6cSVegard Nossum break; 3088c65d5c6cSVegard Nossum } 3089c65d5c6cSVegard Nossum 309097bd42b9SJosef Bacik inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan)); 309197bd42b9SJosef Bacik if (IS_ERR(inode)) { 3092ac27a0ecSDave Kleikamp es->s_last_orphan = 0; 3093ac27a0ecSDave Kleikamp break; 3094ac27a0ecSDave Kleikamp } 3095ac27a0ecSDave Kleikamp 3096617ba13bSMingming Cao list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan); 3097871a2931SChristoph Hellwig dquot_initialize(inode); 3098ac27a0ecSDave Kleikamp if (inode->i_nlink) { 3099566370a2SPaul Taysom if (test_opt(sb, DEBUG)) 3100b31e1552SEric Sandeen ext4_msg(sb, KERN_DEBUG, 3101b31e1552SEric Sandeen "%s: truncating inode %lu to %lld bytes", 310246e665e9SHarvey Harrison __func__, inode->i_ino, inode->i_size); 3103e5f8eab8STheodore Ts'o jbd_debug(2, "truncating inode %lu to %lld bytes\n", 3104ac27a0ecSDave Kleikamp inode->i_ino, inode->i_size); 31055955102cSAl Viro inode_lock(inode); 310655f252c9SLukas Czerner truncate_inode_pages(inode->i_mapping, inode->i_size); 31072c98eb5eSTheodore Ts'o ret = ext4_truncate(inode); 31082c98eb5eSTheodore Ts'o if (ret) 31092c98eb5eSTheodore Ts'o ext4_std_error(inode->i_sb, ret); 31105955102cSAl Viro inode_unlock(inode); 3111ac27a0ecSDave Kleikamp nr_truncates++; 3112ac27a0ecSDave Kleikamp } else { 3113566370a2SPaul Taysom if (test_opt(sb, DEBUG)) 3114b31e1552SEric Sandeen ext4_msg(sb, KERN_DEBUG, 3115b31e1552SEric Sandeen "%s: deleting unreferenced inode %lu", 311646e665e9SHarvey Harrison __func__, inode->i_ino); 3117ac27a0ecSDave Kleikamp jbd_debug(2, "deleting unreferenced inode %lu\n", 3118ac27a0ecSDave Kleikamp inode->i_ino); 3119ac27a0ecSDave Kleikamp nr_orphans++; 3120ac27a0ecSDave Kleikamp } 3121ac27a0ecSDave Kleikamp iput(inode); /* The delete magic happens here! */ 3122ac27a0ecSDave Kleikamp } 3123ac27a0ecSDave Kleikamp 3124ac27a0ecSDave Kleikamp #define PLURAL(x) (x), ((x) == 1) ? "" : "s" 3125ac27a0ecSDave Kleikamp 3126ac27a0ecSDave Kleikamp if (nr_orphans) 3127b31e1552SEric Sandeen ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted", 3128b31e1552SEric Sandeen PLURAL(nr_orphans)); 3129ac27a0ecSDave Kleikamp if (nr_truncates) 3130b31e1552SEric Sandeen ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up", 3131b31e1552SEric Sandeen PLURAL(nr_truncates)); 3132ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 313395f1fda4Szhangyi (F) /* Turn off quotas if they were enabled for orphan cleanup */ 313495f1fda4Szhangyi (F) if (quota_update) { 3135a2d4a646SJan Kara for (i = 0; i < EXT4_MAXQUOTAS; i++) { 313695f1fda4Szhangyi (F) if (sb_dqopt(sb)->files[i]) 3137287a8095SChristoph Hellwig dquot_quota_off(sb, i); 3138ac27a0ecSDave Kleikamp } 313995f1fda4Szhangyi (F) } 3140ac27a0ecSDave Kleikamp #endif 31411751e8a6SLinus Torvalds sb->s_flags = s_flags; /* Restore SB_RDONLY status */ 3142ac27a0ecSDave Kleikamp } 31430b8e58a1SAndreas Dilger 3144cd2291a4SEric Sandeen /* 3145cd2291a4SEric Sandeen * Maximal extent format file size. 3146cd2291a4SEric Sandeen * Resulting logical blkno at s_maxbytes must fit in our on-disk 3147cd2291a4SEric Sandeen * extent format containers, within a sector_t, and within i_blocks 3148cd2291a4SEric Sandeen * in the vfs. ext4 inode has 48 bits of i_block in fsblock units, 3149cd2291a4SEric Sandeen * so that won't be a limiting factor. 3150cd2291a4SEric Sandeen * 3151f17722f9SLukas Czerner * However there is other limiting factor. We do store extents in the form 3152f17722f9SLukas Czerner * of starting block and length, hence the resulting length of the extent 3153f17722f9SLukas Czerner * covering maximum file size must fit into on-disk format containers as 3154f17722f9SLukas Czerner * well. Given that length is always by 1 unit bigger than max unit (because 3155f17722f9SLukas Czerner * we count 0 as well) we have to lower the s_maxbytes by one fs block. 3156f17722f9SLukas Czerner * 3157cd2291a4SEric Sandeen * Note, this does *not* consider any metadata overhead for vfs i_blocks. 3158cd2291a4SEric Sandeen */ 3159f287a1a5STheodore Ts'o static loff_t ext4_max_size(int blkbits, int has_huge_files) 3160cd2291a4SEric Sandeen { 3161cd2291a4SEric Sandeen loff_t res; 3162cd2291a4SEric Sandeen loff_t upper_limit = MAX_LFS_FILESIZE; 3163cd2291a4SEric Sandeen 316472deb455SChristoph Hellwig BUILD_BUG_ON(sizeof(blkcnt_t) < sizeof(u64)); 316572deb455SChristoph Hellwig 316672deb455SChristoph Hellwig if (!has_huge_files) { 3167cd2291a4SEric Sandeen upper_limit = (1LL << 32) - 1; 3168cd2291a4SEric Sandeen 3169cd2291a4SEric Sandeen /* total blocks in file system block size */ 3170cd2291a4SEric Sandeen upper_limit >>= (blkbits - 9); 3171cd2291a4SEric Sandeen upper_limit <<= blkbits; 3172cd2291a4SEric Sandeen } 3173cd2291a4SEric Sandeen 3174f17722f9SLukas Czerner /* 3175f17722f9SLukas Czerner * 32-bit extent-start container, ee_block. We lower the maxbytes 3176f17722f9SLukas Czerner * by one fs block, so ee_len can cover the extent of maximum file 3177f17722f9SLukas Czerner * size 3178f17722f9SLukas Czerner */ 3179f17722f9SLukas Czerner res = (1LL << 32) - 1; 3180cd2291a4SEric Sandeen res <<= blkbits; 3181cd2291a4SEric Sandeen 3182cd2291a4SEric Sandeen /* Sanity check against vm- & vfs- imposed limits */ 3183cd2291a4SEric Sandeen if (res > upper_limit) 3184cd2291a4SEric Sandeen res = upper_limit; 3185cd2291a4SEric Sandeen 3186cd2291a4SEric Sandeen return res; 3187cd2291a4SEric Sandeen } 3188ac27a0ecSDave Kleikamp 3189ac27a0ecSDave Kleikamp /* 3190cd2291a4SEric Sandeen * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect 31910fc1b451SAneesh Kumar K.V * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks. 31920fc1b451SAneesh Kumar K.V * We need to be 1 filesystem block less than the 2^48 sector limit. 3193ac27a0ecSDave Kleikamp */ 3194f287a1a5STheodore Ts'o static loff_t ext4_max_bitmap_size(int bits, int has_huge_files) 3195ac27a0ecSDave Kleikamp { 3196617ba13bSMingming Cao loff_t res = EXT4_NDIR_BLOCKS; 31970fc1b451SAneesh Kumar K.V int meta_blocks; 31980fc1b451SAneesh Kumar K.V loff_t upper_limit; 31990b8e58a1SAndreas Dilger /* This is calculated to be the largest file size for a dense, block 32000b8e58a1SAndreas Dilger * mapped file such that the file's total number of 512-byte sectors, 32010b8e58a1SAndreas Dilger * including data and all indirect blocks, does not exceed (2^48 - 1). 32020b8e58a1SAndreas Dilger * 32030b8e58a1SAndreas Dilger * __u32 i_blocks_lo and _u16 i_blocks_high represent the total 32040b8e58a1SAndreas Dilger * number of 512-byte sectors of the file. 32050fc1b451SAneesh Kumar K.V */ 32060fc1b451SAneesh Kumar K.V 320772deb455SChristoph Hellwig if (!has_huge_files) { 32080fc1b451SAneesh Kumar K.V /* 320972deb455SChristoph Hellwig * !has_huge_files or implies that the inode i_block field 321072deb455SChristoph Hellwig * represents total file blocks in 2^32 512-byte sectors == 321172deb455SChristoph Hellwig * size of vfs inode i_blocks * 8 32120fc1b451SAneesh Kumar K.V */ 32130fc1b451SAneesh Kumar K.V upper_limit = (1LL << 32) - 1; 32140fc1b451SAneesh Kumar K.V 32150fc1b451SAneesh Kumar K.V /* total blocks in file system block size */ 32160fc1b451SAneesh Kumar K.V upper_limit >>= (bits - 9); 32170fc1b451SAneesh Kumar K.V 32180fc1b451SAneesh Kumar K.V } else { 32198180a562SAneesh Kumar K.V /* 32208180a562SAneesh Kumar K.V * We use 48 bit ext4_inode i_blocks 32218180a562SAneesh Kumar K.V * With EXT4_HUGE_FILE_FL set the i_blocks 32228180a562SAneesh Kumar K.V * represent total number of blocks in 32238180a562SAneesh Kumar K.V * file system block size 32248180a562SAneesh Kumar K.V */ 32250fc1b451SAneesh Kumar K.V upper_limit = (1LL << 48) - 1; 32260fc1b451SAneesh Kumar K.V 32270fc1b451SAneesh Kumar K.V } 32280fc1b451SAneesh Kumar K.V 32290fc1b451SAneesh Kumar K.V /* indirect blocks */ 32300fc1b451SAneesh Kumar K.V meta_blocks = 1; 32310fc1b451SAneesh Kumar K.V /* double indirect blocks */ 32320fc1b451SAneesh Kumar K.V meta_blocks += 1 + (1LL << (bits-2)); 32330fc1b451SAneesh Kumar K.V /* tripple indirect blocks */ 32340fc1b451SAneesh Kumar K.V meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2))); 32350fc1b451SAneesh Kumar K.V 32360fc1b451SAneesh Kumar K.V upper_limit -= meta_blocks; 32370fc1b451SAneesh Kumar K.V upper_limit <<= bits; 3238ac27a0ecSDave Kleikamp 3239ac27a0ecSDave Kleikamp res += 1LL << (bits-2); 3240ac27a0ecSDave Kleikamp res += 1LL << (2*(bits-2)); 3241ac27a0ecSDave Kleikamp res += 1LL << (3*(bits-2)); 3242ac27a0ecSDave Kleikamp res <<= bits; 3243ac27a0ecSDave Kleikamp if (res > upper_limit) 3244ac27a0ecSDave Kleikamp res = upper_limit; 32450fc1b451SAneesh Kumar K.V 32460fc1b451SAneesh Kumar K.V if (res > MAX_LFS_FILESIZE) 32470fc1b451SAneesh Kumar K.V res = MAX_LFS_FILESIZE; 32480fc1b451SAneesh Kumar K.V 3249ac27a0ecSDave Kleikamp return res; 3250ac27a0ecSDave Kleikamp } 3251ac27a0ecSDave Kleikamp 3252617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb, 325370bbb3e0SAndrew Morton ext4_fsblk_t logical_sb_block, int nr) 3254ac27a0ecSDave Kleikamp { 3255617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 3256fd2d4291SAvantika Mathur ext4_group_t bg, first_meta_bg; 3257ac27a0ecSDave Kleikamp int has_super = 0; 3258ac27a0ecSDave Kleikamp 3259ac27a0ecSDave Kleikamp first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg); 3260ac27a0ecSDave Kleikamp 3261e2b911c5SDarrick J. Wong if (!ext4_has_feature_meta_bg(sb) || nr < first_meta_bg) 326270bbb3e0SAndrew Morton return logical_sb_block + nr + 1; 3263ac27a0ecSDave Kleikamp bg = sbi->s_desc_per_block * nr; 3264617ba13bSMingming Cao if (ext4_bg_has_super(sb, bg)) 3265ac27a0ecSDave Kleikamp has_super = 1; 32660b8e58a1SAndreas Dilger 3267bd63f6b0SDarrick J. Wong /* 3268bd63f6b0SDarrick J. Wong * If we have a meta_bg fs with 1k blocks, group 0's GDT is at 3269bd63f6b0SDarrick J. Wong * block 2, not 1. If s_first_data_block == 0 (bigalloc is enabled 3270bd63f6b0SDarrick J. Wong * on modern mke2fs or blksize > 1k on older mke2fs) then we must 3271bd63f6b0SDarrick J. Wong * compensate. 3272bd63f6b0SDarrick J. Wong */ 3273bd63f6b0SDarrick J. Wong if (sb->s_blocksize == 1024 && nr == 0 && 327449598e04SJun Piao le32_to_cpu(sbi->s_es->s_first_data_block) == 0) 3275bd63f6b0SDarrick J. Wong has_super++; 3276bd63f6b0SDarrick J. Wong 3277617ba13bSMingming Cao return (has_super + ext4_group_first_block_no(sb, bg)); 3278ac27a0ecSDave Kleikamp } 3279ac27a0ecSDave Kleikamp 3280c9de560dSAlex Tomas /** 3281c9de560dSAlex Tomas * ext4_get_stripe_size: Get the stripe size. 3282c9de560dSAlex Tomas * @sbi: In memory super block info 3283c9de560dSAlex Tomas * 3284c9de560dSAlex Tomas * If we have specified it via mount option, then 3285c9de560dSAlex Tomas * use the mount option value. If the value specified at mount time is 3286c9de560dSAlex Tomas * greater than the blocks per group use the super block value. 3287c9de560dSAlex Tomas * If the super block value is greater than blocks per group return 0. 3288c9de560dSAlex Tomas * Allocator needs it be less than blocks per group. 3289c9de560dSAlex Tomas * 3290c9de560dSAlex Tomas */ 3291c9de560dSAlex Tomas static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi) 3292c9de560dSAlex Tomas { 3293c9de560dSAlex Tomas unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride); 3294c9de560dSAlex Tomas unsigned long stripe_width = 3295c9de560dSAlex Tomas le32_to_cpu(sbi->s_es->s_raid_stripe_width); 32963eb08658SDan Ehrenberg int ret; 3297c9de560dSAlex Tomas 3298c9de560dSAlex Tomas if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group) 32993eb08658SDan Ehrenberg ret = sbi->s_stripe; 33005469d7c3SJan Kara else if (stripe_width && stripe_width <= sbi->s_blocks_per_group) 33013eb08658SDan Ehrenberg ret = stripe_width; 33025469d7c3SJan Kara else if (stride && stride <= sbi->s_blocks_per_group) 33033eb08658SDan Ehrenberg ret = stride; 33043eb08658SDan Ehrenberg else 33053eb08658SDan Ehrenberg ret = 0; 3306c9de560dSAlex Tomas 33073eb08658SDan Ehrenberg /* 33083eb08658SDan Ehrenberg * If the stripe width is 1, this makes no sense and 33093eb08658SDan Ehrenberg * we set it to 0 to turn off stripe handling code. 33103eb08658SDan Ehrenberg */ 33113eb08658SDan Ehrenberg if (ret <= 1) 33123eb08658SDan Ehrenberg ret = 0; 3313c9de560dSAlex Tomas 33143eb08658SDan Ehrenberg return ret; 3315c9de560dSAlex Tomas } 3316ac27a0ecSDave Kleikamp 3317a13fb1a4SEric Sandeen /* 3318a13fb1a4SEric Sandeen * Check whether this filesystem can be mounted based on 3319a13fb1a4SEric Sandeen * the features present and the RDONLY/RDWR mount requested. 3320a13fb1a4SEric Sandeen * Returns 1 if this filesystem can be mounted as requested, 3321a13fb1a4SEric Sandeen * 0 if it cannot be. 3322a13fb1a4SEric Sandeen */ 3323a13fb1a4SEric Sandeen static int ext4_feature_set_ok(struct super_block *sb, int readonly) 3324a13fb1a4SEric Sandeen { 3325e2b911c5SDarrick J. Wong if (ext4_has_unknown_ext4_incompat_features(sb)) { 3326a13fb1a4SEric Sandeen ext4_msg(sb, KERN_ERR, 3327a13fb1a4SEric Sandeen "Couldn't mount because of " 3328a13fb1a4SEric Sandeen "unsupported optional features (%x)", 3329a13fb1a4SEric Sandeen (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) & 3330a13fb1a4SEric Sandeen ~EXT4_FEATURE_INCOMPAT_SUPP)); 3331a13fb1a4SEric Sandeen return 0; 3332a13fb1a4SEric Sandeen } 3333a13fb1a4SEric Sandeen 3334c83ad55eSGabriel Krisman Bertazi #ifndef CONFIG_UNICODE 3335c83ad55eSGabriel Krisman Bertazi if (ext4_has_feature_casefold(sb)) { 3336c83ad55eSGabriel Krisman Bertazi ext4_msg(sb, KERN_ERR, 3337c83ad55eSGabriel Krisman Bertazi "Filesystem with casefold feature cannot be " 3338c83ad55eSGabriel Krisman Bertazi "mounted without CONFIG_UNICODE"); 3339c83ad55eSGabriel Krisman Bertazi return 0; 3340c83ad55eSGabriel Krisman Bertazi } 3341c83ad55eSGabriel Krisman Bertazi #endif 3342c83ad55eSGabriel Krisman Bertazi 3343a13fb1a4SEric Sandeen if (readonly) 3344a13fb1a4SEric Sandeen return 1; 3345a13fb1a4SEric Sandeen 3346e2b911c5SDarrick J. Wong if (ext4_has_feature_readonly(sb)) { 33472cb5cc8bSDarrick J. Wong ext4_msg(sb, KERN_INFO, "filesystem is read-only"); 33481751e8a6SLinus Torvalds sb->s_flags |= SB_RDONLY; 33492cb5cc8bSDarrick J. Wong return 1; 33502cb5cc8bSDarrick J. Wong } 33512cb5cc8bSDarrick J. Wong 3352a13fb1a4SEric Sandeen /* Check that feature set is OK for a read-write mount */ 3353e2b911c5SDarrick J. Wong if (ext4_has_unknown_ext4_ro_compat_features(sb)) { 3354a13fb1a4SEric Sandeen ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of " 3355a13fb1a4SEric Sandeen "unsupported optional features (%x)", 3356a13fb1a4SEric Sandeen (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) & 3357a13fb1a4SEric Sandeen ~EXT4_FEATURE_RO_COMPAT_SUPP)); 3358a13fb1a4SEric Sandeen return 0; 3359a13fb1a4SEric Sandeen } 3360e2b911c5SDarrick J. Wong if (ext4_has_feature_bigalloc(sb) && !ext4_has_feature_extents(sb)) { 3361bab08ab9STheodore Ts'o ext4_msg(sb, KERN_ERR, 3362bab08ab9STheodore Ts'o "Can't support bigalloc feature without " 3363bab08ab9STheodore Ts'o "extents feature\n"); 3364bab08ab9STheodore Ts'o return 0; 3365bab08ab9STheodore Ts'o } 33667c319d32SAditya Kali 33679db176bcSJan Kara #if !IS_ENABLED(CONFIG_QUOTA) || !IS_ENABLED(CONFIG_QFMT_V2) 3368d65d87a0STheodore Ts'o if (!readonly && (ext4_has_feature_quota(sb) || 3369d65d87a0STheodore Ts'o ext4_has_feature_project(sb))) { 33707c319d32SAditya Kali ext4_msg(sb, KERN_ERR, 3371d65d87a0STheodore Ts'o "The kernel was not built with CONFIG_QUOTA and CONFIG_QFMT_V2"); 3372689c958cSLi Xi return 0; 3373689c958cSLi Xi } 33747c319d32SAditya Kali #endif /* CONFIG_QUOTA */ 3375a13fb1a4SEric Sandeen return 1; 3376a13fb1a4SEric Sandeen } 3377a13fb1a4SEric Sandeen 337866e61a9eSTheodore Ts'o /* 337966e61a9eSTheodore Ts'o * This function is called once a day if we have errors logged 338066e61a9eSTheodore Ts'o * on the file system 338166e61a9eSTheodore Ts'o */ 3382235699a8SKees Cook static void print_daily_error_info(struct timer_list *t) 338366e61a9eSTheodore Ts'o { 3384235699a8SKees Cook struct ext4_sb_info *sbi = from_timer(sbi, t, s_err_report); 3385235699a8SKees Cook struct super_block *sb = sbi->s_sb; 3386235699a8SKees Cook struct ext4_super_block *es = sbi->s_es; 338766e61a9eSTheodore Ts'o 338866e61a9eSTheodore Ts'o if (es->s_error_count) 3389ae0f78deSTheodore Ts'o /* fsck newer than v1.41.13 is needed to clean this condition. */ 3390ae0f78deSTheodore Ts'o ext4_msg(sb, KERN_NOTICE, "error count since last fsck: %u", 339166e61a9eSTheodore Ts'o le32_to_cpu(es->s_error_count)); 339266e61a9eSTheodore Ts'o if (es->s_first_error_time) { 33936a0678a7SArnd Bergmann printk(KERN_NOTICE "EXT4-fs (%s): initial error at time %llu: %.*s:%d", 33946a0678a7SArnd Bergmann sb->s_id, 33956a0678a7SArnd Bergmann ext4_get_tstamp(es, s_first_error_time), 339666e61a9eSTheodore Ts'o (int) sizeof(es->s_first_error_func), 339766e61a9eSTheodore Ts'o es->s_first_error_func, 339866e61a9eSTheodore Ts'o le32_to_cpu(es->s_first_error_line)); 339966e61a9eSTheodore Ts'o if (es->s_first_error_ino) 3400651e1c3bSJoe Perches printk(KERN_CONT ": inode %u", 340166e61a9eSTheodore Ts'o le32_to_cpu(es->s_first_error_ino)); 340266e61a9eSTheodore Ts'o if (es->s_first_error_block) 3403651e1c3bSJoe Perches printk(KERN_CONT ": block %llu", (unsigned long long) 340466e61a9eSTheodore Ts'o le64_to_cpu(es->s_first_error_block)); 3405651e1c3bSJoe Perches printk(KERN_CONT "\n"); 340666e61a9eSTheodore Ts'o } 340766e61a9eSTheodore Ts'o if (es->s_last_error_time) { 34086a0678a7SArnd Bergmann printk(KERN_NOTICE "EXT4-fs (%s): last error at time %llu: %.*s:%d", 34096a0678a7SArnd Bergmann sb->s_id, 34106a0678a7SArnd Bergmann ext4_get_tstamp(es, s_last_error_time), 341166e61a9eSTheodore Ts'o (int) sizeof(es->s_last_error_func), 341266e61a9eSTheodore Ts'o es->s_last_error_func, 341366e61a9eSTheodore Ts'o le32_to_cpu(es->s_last_error_line)); 341466e61a9eSTheodore Ts'o if (es->s_last_error_ino) 3415651e1c3bSJoe Perches printk(KERN_CONT ": inode %u", 341666e61a9eSTheodore Ts'o le32_to_cpu(es->s_last_error_ino)); 341766e61a9eSTheodore Ts'o if (es->s_last_error_block) 3418651e1c3bSJoe Perches printk(KERN_CONT ": block %llu", (unsigned long long) 341966e61a9eSTheodore Ts'o le64_to_cpu(es->s_last_error_block)); 3420651e1c3bSJoe Perches printk(KERN_CONT "\n"); 342166e61a9eSTheodore Ts'o } 342266e61a9eSTheodore Ts'o mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ); /* Once a day */ 342366e61a9eSTheodore Ts'o } 342466e61a9eSTheodore Ts'o 3425bfff6873SLukas Czerner /* Find next suitable group and run ext4_init_inode_table */ 3426bfff6873SLukas Czerner static int ext4_run_li_request(struct ext4_li_request *elr) 3427bfff6873SLukas Czerner { 3428bfff6873SLukas Czerner struct ext4_group_desc *gdp = NULL; 34293d392b26STheodore Ts'o struct super_block *sb = elr->lr_super; 34303d392b26STheodore Ts'o ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count; 34313d392b26STheodore Ts'o ext4_group_t group = elr->lr_next_group; 3432bfff6873SLukas Czerner unsigned long timeout = 0; 34333d392b26STheodore Ts'o unsigned int prefetch_ios = 0; 3434bfff6873SLukas Czerner int ret = 0; 3435bfff6873SLukas Czerner 34363d392b26STheodore Ts'o if (elr->lr_mode == EXT4_LI_MODE_PREFETCH_BBITMAP) { 34373d392b26STheodore Ts'o elr->lr_next_group = ext4_mb_prefetch(sb, group, 34383d392b26STheodore Ts'o EXT4_SB(sb)->s_mb_prefetch, &prefetch_ios); 34393d392b26STheodore Ts'o if (prefetch_ios) 34403d392b26STheodore Ts'o ext4_mb_prefetch_fini(sb, elr->lr_next_group, 34413d392b26STheodore Ts'o prefetch_ios); 34423d392b26STheodore Ts'o trace_ext4_prefetch_bitmaps(sb, group, elr->lr_next_group, 34433d392b26STheodore Ts'o prefetch_ios); 34443d392b26STheodore Ts'o if (group >= elr->lr_next_group) { 34453d392b26STheodore Ts'o ret = 1; 34463d392b26STheodore Ts'o if (elr->lr_first_not_zeroed != ngroups && 34473d392b26STheodore Ts'o !sb_rdonly(sb) && test_opt(sb, INIT_INODE_TABLE)) { 34483d392b26STheodore Ts'o elr->lr_next_group = elr->lr_first_not_zeroed; 34493d392b26STheodore Ts'o elr->lr_mode = EXT4_LI_MODE_ITABLE; 34503d392b26STheodore Ts'o ret = 0; 34513d392b26STheodore Ts'o } 34523d392b26STheodore Ts'o } 34533d392b26STheodore Ts'o return ret; 34543d392b26STheodore Ts'o } 3455bfff6873SLukas Czerner 34563d392b26STheodore Ts'o for (; group < ngroups; group++) { 3457bfff6873SLukas Czerner gdp = ext4_get_group_desc(sb, group, NULL); 3458bfff6873SLukas Czerner if (!gdp) { 3459bfff6873SLukas Czerner ret = 1; 3460bfff6873SLukas Czerner break; 3461bfff6873SLukas Czerner } 3462bfff6873SLukas Czerner 3463bfff6873SLukas Czerner if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED))) 3464bfff6873SLukas Czerner break; 3465bfff6873SLukas Czerner } 3466bfff6873SLukas Czerner 34677f511862STheodore Ts'o if (group >= ngroups) 3468bfff6873SLukas Czerner ret = 1; 3469bfff6873SLukas Czerner 3470bfff6873SLukas Czerner if (!ret) { 3471bfff6873SLukas Czerner timeout = jiffies; 3472bfff6873SLukas Czerner ret = ext4_init_inode_table(sb, group, 3473bfff6873SLukas Czerner elr->lr_timeout ? 0 : 1); 34743d392b26STheodore Ts'o trace_ext4_lazy_itable_init(sb, group); 3475bfff6873SLukas Czerner if (elr->lr_timeout == 0) { 347651ce6511SLukas Czerner timeout = (jiffies - timeout) * 34773d392b26STheodore Ts'o EXT4_SB(elr->lr_super)->s_li_wait_mult; 3478bfff6873SLukas Czerner elr->lr_timeout = timeout; 3479bfff6873SLukas Czerner } 3480bfff6873SLukas Czerner elr->lr_next_sched = jiffies + elr->lr_timeout; 3481bfff6873SLukas Czerner elr->lr_next_group = group + 1; 3482bfff6873SLukas Czerner } 3483bfff6873SLukas Czerner return ret; 3484bfff6873SLukas Czerner } 3485bfff6873SLukas Czerner 3486bfff6873SLukas Czerner /* 3487bfff6873SLukas Czerner * Remove lr_request from the list_request and free the 34884ed5c033SLukas Czerner * request structure. Should be called with li_list_mtx held 3489bfff6873SLukas Czerner */ 3490bfff6873SLukas Czerner static void ext4_remove_li_request(struct ext4_li_request *elr) 3491bfff6873SLukas Czerner { 3492bfff6873SLukas Czerner if (!elr) 3493bfff6873SLukas Czerner return; 3494bfff6873SLukas Czerner 3495bfff6873SLukas Czerner list_del(&elr->lr_request); 34963d392b26STheodore Ts'o EXT4_SB(elr->lr_super)->s_li_request = NULL; 3497bfff6873SLukas Czerner kfree(elr); 3498bfff6873SLukas Czerner } 3499bfff6873SLukas Czerner 3500bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb) 3501bfff6873SLukas Czerner { 35021bb933fbSLukas Czerner mutex_lock(&ext4_li_mtx); 35031bb933fbSLukas Czerner if (!ext4_li_info) { 35041bb933fbSLukas Czerner mutex_unlock(&ext4_li_mtx); 3505bfff6873SLukas Czerner return; 35061bb933fbSLukas Czerner } 3507bfff6873SLukas Czerner 3508bfff6873SLukas Czerner mutex_lock(&ext4_li_info->li_list_mtx); 35091bb933fbSLukas Czerner ext4_remove_li_request(EXT4_SB(sb)->s_li_request); 3510bfff6873SLukas Czerner mutex_unlock(&ext4_li_info->li_list_mtx); 35111bb933fbSLukas Czerner mutex_unlock(&ext4_li_mtx); 3512bfff6873SLukas Czerner } 3513bfff6873SLukas Czerner 35148f1f7453SEric Sandeen static struct task_struct *ext4_lazyinit_task; 35158f1f7453SEric Sandeen 3516bfff6873SLukas Czerner /* 3517bfff6873SLukas Czerner * This is the function where ext4lazyinit thread lives. It walks 3518bfff6873SLukas Czerner * through the request list searching for next scheduled filesystem. 3519bfff6873SLukas Czerner * When such a fs is found, run the lazy initialization request 3520bfff6873SLukas Czerner * (ext4_rn_li_request) and keep track of the time spend in this 3521bfff6873SLukas Czerner * function. Based on that time we compute next schedule time of 3522bfff6873SLukas Czerner * the request. When walking through the list is complete, compute 3523bfff6873SLukas Czerner * next waking time and put itself into sleep. 3524bfff6873SLukas Czerner */ 3525bfff6873SLukas Czerner static int ext4_lazyinit_thread(void *arg) 3526bfff6873SLukas Czerner { 3527bfff6873SLukas Czerner struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg; 3528bfff6873SLukas Czerner struct list_head *pos, *n; 3529bfff6873SLukas Czerner struct ext4_li_request *elr; 35304ed5c033SLukas Czerner unsigned long next_wakeup, cur; 3531bfff6873SLukas Czerner 3532bfff6873SLukas Czerner BUG_ON(NULL == eli); 3533bfff6873SLukas Czerner 3534bfff6873SLukas Czerner cont_thread: 3535bfff6873SLukas Czerner while (true) { 3536bfff6873SLukas Czerner next_wakeup = MAX_JIFFY_OFFSET; 3537bfff6873SLukas Czerner 3538bfff6873SLukas Czerner mutex_lock(&eli->li_list_mtx); 3539bfff6873SLukas Czerner if (list_empty(&eli->li_request_list)) { 3540bfff6873SLukas Czerner mutex_unlock(&eli->li_list_mtx); 3541bfff6873SLukas Czerner goto exit_thread; 3542bfff6873SLukas Czerner } 3543bfff6873SLukas Czerner list_for_each_safe(pos, n, &eli->li_request_list) { 3544e22834f0SDmitry Monakhov int err = 0; 3545e22834f0SDmitry Monakhov int progress = 0; 3546bfff6873SLukas Czerner elr = list_entry(pos, struct ext4_li_request, 3547bfff6873SLukas Czerner lr_request); 3548bfff6873SLukas Czerner 3549e22834f0SDmitry Monakhov if (time_before(jiffies, elr->lr_next_sched)) { 3550e22834f0SDmitry Monakhov if (time_before(elr->lr_next_sched, next_wakeup)) 3551e22834f0SDmitry Monakhov next_wakeup = elr->lr_next_sched; 3552e22834f0SDmitry Monakhov continue; 3553e22834f0SDmitry Monakhov } 3554e22834f0SDmitry Monakhov if (down_read_trylock(&elr->lr_super->s_umount)) { 3555e22834f0SDmitry Monakhov if (sb_start_write_trylock(elr->lr_super)) { 3556e22834f0SDmitry Monakhov progress = 1; 3557e22834f0SDmitry Monakhov /* 3558e22834f0SDmitry Monakhov * We hold sb->s_umount, sb can not 3559e22834f0SDmitry Monakhov * be removed from the list, it is 3560e22834f0SDmitry Monakhov * now safe to drop li_list_mtx 3561e22834f0SDmitry Monakhov */ 3562e22834f0SDmitry Monakhov mutex_unlock(&eli->li_list_mtx); 3563e22834f0SDmitry Monakhov err = ext4_run_li_request(elr); 3564e22834f0SDmitry Monakhov sb_end_write(elr->lr_super); 3565e22834f0SDmitry Monakhov mutex_lock(&eli->li_list_mtx); 3566e22834f0SDmitry Monakhov n = pos->next; 3567e22834f0SDmitry Monakhov } 3568e22834f0SDmitry Monakhov up_read((&elr->lr_super->s_umount)); 3569e22834f0SDmitry Monakhov } 3570b2c78cd0STheodore Ts'o /* error, remove the lazy_init job */ 3571e22834f0SDmitry Monakhov if (err) { 3572bfff6873SLukas Czerner ext4_remove_li_request(elr); 3573bfff6873SLukas Czerner continue; 3574bfff6873SLukas Czerner } 3575e22834f0SDmitry Monakhov if (!progress) { 3576e22834f0SDmitry Monakhov elr->lr_next_sched = jiffies + 3577e22834f0SDmitry Monakhov (prandom_u32() 3578e22834f0SDmitry Monakhov % (EXT4_DEF_LI_MAX_START_DELAY * HZ)); 3579b2c78cd0STheodore Ts'o } 3580bfff6873SLukas Czerner if (time_before(elr->lr_next_sched, next_wakeup)) 3581bfff6873SLukas Czerner next_wakeup = elr->lr_next_sched; 3582bfff6873SLukas Czerner } 3583bfff6873SLukas Czerner mutex_unlock(&eli->li_list_mtx); 3584bfff6873SLukas Czerner 3585a0acae0eSTejun Heo try_to_freeze(); 3586bfff6873SLukas Czerner 35874ed5c033SLukas Czerner cur = jiffies; 35884ed5c033SLukas Czerner if ((time_after_eq(cur, next_wakeup)) || 3589f4245bd4SLukas Czerner (MAX_JIFFY_OFFSET == next_wakeup)) { 3590bfff6873SLukas Czerner cond_resched(); 3591bfff6873SLukas Czerner continue; 3592bfff6873SLukas Czerner } 3593bfff6873SLukas Czerner 35944ed5c033SLukas Czerner schedule_timeout_interruptible(next_wakeup - cur); 35954ed5c033SLukas Czerner 35968f1f7453SEric Sandeen if (kthread_should_stop()) { 35978f1f7453SEric Sandeen ext4_clear_request_list(); 35988f1f7453SEric Sandeen goto exit_thread; 35998f1f7453SEric Sandeen } 3600bfff6873SLukas Czerner } 3601bfff6873SLukas Czerner 3602bfff6873SLukas Czerner exit_thread: 3603bfff6873SLukas Czerner /* 3604bfff6873SLukas Czerner * It looks like the request list is empty, but we need 3605bfff6873SLukas Czerner * to check it under the li_list_mtx lock, to prevent any 3606bfff6873SLukas Czerner * additions into it, and of course we should lock ext4_li_mtx 3607bfff6873SLukas Czerner * to atomically free the list and ext4_li_info, because at 3608bfff6873SLukas Czerner * this point another ext4 filesystem could be registering 3609bfff6873SLukas Czerner * new one. 3610bfff6873SLukas Czerner */ 3611bfff6873SLukas Czerner mutex_lock(&ext4_li_mtx); 3612bfff6873SLukas Czerner mutex_lock(&eli->li_list_mtx); 3613bfff6873SLukas Czerner if (!list_empty(&eli->li_request_list)) { 3614bfff6873SLukas Czerner mutex_unlock(&eli->li_list_mtx); 3615bfff6873SLukas Czerner mutex_unlock(&ext4_li_mtx); 3616bfff6873SLukas Czerner goto cont_thread; 3617bfff6873SLukas Czerner } 3618bfff6873SLukas Czerner mutex_unlock(&eli->li_list_mtx); 3619bfff6873SLukas Czerner kfree(ext4_li_info); 3620bfff6873SLukas Czerner ext4_li_info = NULL; 3621bfff6873SLukas Czerner mutex_unlock(&ext4_li_mtx); 3622bfff6873SLukas Czerner 3623bfff6873SLukas Czerner return 0; 3624bfff6873SLukas Czerner } 3625bfff6873SLukas Czerner 3626bfff6873SLukas Czerner static void ext4_clear_request_list(void) 3627bfff6873SLukas Czerner { 3628bfff6873SLukas Czerner struct list_head *pos, *n; 3629bfff6873SLukas Czerner struct ext4_li_request *elr; 3630bfff6873SLukas Czerner 3631bfff6873SLukas Czerner mutex_lock(&ext4_li_info->li_list_mtx); 3632bfff6873SLukas Czerner list_for_each_safe(pos, n, &ext4_li_info->li_request_list) { 3633bfff6873SLukas Czerner elr = list_entry(pos, struct ext4_li_request, 3634bfff6873SLukas Czerner lr_request); 3635bfff6873SLukas Czerner ext4_remove_li_request(elr); 3636bfff6873SLukas Czerner } 3637bfff6873SLukas Czerner mutex_unlock(&ext4_li_info->li_list_mtx); 3638bfff6873SLukas Czerner } 3639bfff6873SLukas Czerner 3640bfff6873SLukas Czerner static int ext4_run_lazyinit_thread(void) 3641bfff6873SLukas Czerner { 36428f1f7453SEric Sandeen ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread, 36438f1f7453SEric Sandeen ext4_li_info, "ext4lazyinit"); 36448f1f7453SEric Sandeen if (IS_ERR(ext4_lazyinit_task)) { 36458f1f7453SEric Sandeen int err = PTR_ERR(ext4_lazyinit_task); 3646bfff6873SLukas Czerner ext4_clear_request_list(); 3647bfff6873SLukas Czerner kfree(ext4_li_info); 3648bfff6873SLukas Czerner ext4_li_info = NULL; 364992b97816STheodore Ts'o printk(KERN_CRIT "EXT4-fs: error %d creating inode table " 3650bfff6873SLukas Czerner "initialization thread\n", 3651bfff6873SLukas Czerner err); 3652bfff6873SLukas Czerner return err; 3653bfff6873SLukas Czerner } 3654bfff6873SLukas Czerner ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING; 3655bfff6873SLukas Czerner return 0; 3656bfff6873SLukas Czerner } 3657bfff6873SLukas Czerner 3658bfff6873SLukas Czerner /* 3659bfff6873SLukas Czerner * Check whether it make sense to run itable init. thread or not. 3660bfff6873SLukas Czerner * If there is at least one uninitialized inode table, return 3661bfff6873SLukas Czerner * corresponding group number, else the loop goes through all 3662bfff6873SLukas Czerner * groups and return total number of groups. 3663bfff6873SLukas Czerner */ 3664bfff6873SLukas Czerner static ext4_group_t ext4_has_uninit_itable(struct super_block *sb) 3665bfff6873SLukas Czerner { 3666bfff6873SLukas Czerner ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count; 3667bfff6873SLukas Czerner struct ext4_group_desc *gdp = NULL; 3668bfff6873SLukas Czerner 36698844618dSTheodore Ts'o if (!ext4_has_group_desc_csum(sb)) 36708844618dSTheodore Ts'o return ngroups; 36718844618dSTheodore Ts'o 3672bfff6873SLukas Czerner for (group = 0; group < ngroups; group++) { 3673bfff6873SLukas Czerner gdp = ext4_get_group_desc(sb, group, NULL); 3674bfff6873SLukas Czerner if (!gdp) 3675bfff6873SLukas Czerner continue; 3676bfff6873SLukas Czerner 367750122847STheodore Ts'o if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED))) 3678bfff6873SLukas Czerner break; 3679bfff6873SLukas Czerner } 3680bfff6873SLukas Czerner 3681bfff6873SLukas Czerner return group; 3682bfff6873SLukas Czerner } 3683bfff6873SLukas Czerner 3684bfff6873SLukas Czerner static int ext4_li_info_new(void) 3685bfff6873SLukas Czerner { 3686bfff6873SLukas Czerner struct ext4_lazy_init *eli = NULL; 3687bfff6873SLukas Czerner 3688bfff6873SLukas Czerner eli = kzalloc(sizeof(*eli), GFP_KERNEL); 3689bfff6873SLukas Czerner if (!eli) 3690bfff6873SLukas Czerner return -ENOMEM; 3691bfff6873SLukas Czerner 3692bfff6873SLukas Czerner INIT_LIST_HEAD(&eli->li_request_list); 3693bfff6873SLukas Czerner mutex_init(&eli->li_list_mtx); 3694bfff6873SLukas Czerner 3695bfff6873SLukas Czerner eli->li_state |= EXT4_LAZYINIT_QUIT; 3696bfff6873SLukas Czerner 3697bfff6873SLukas Czerner ext4_li_info = eli; 3698bfff6873SLukas Czerner 3699bfff6873SLukas Czerner return 0; 3700bfff6873SLukas Czerner } 3701bfff6873SLukas Czerner 3702bfff6873SLukas Czerner static struct ext4_li_request *ext4_li_request_new(struct super_block *sb, 3703bfff6873SLukas Czerner ext4_group_t start) 3704bfff6873SLukas Czerner { 3705bfff6873SLukas Czerner struct ext4_li_request *elr; 3706bfff6873SLukas Czerner 3707bfff6873SLukas Czerner elr = kzalloc(sizeof(*elr), GFP_KERNEL); 3708bfff6873SLukas Czerner if (!elr) 3709bfff6873SLukas Czerner return NULL; 3710bfff6873SLukas Czerner 3711bfff6873SLukas Czerner elr->lr_super = sb; 37123d392b26STheodore Ts'o elr->lr_first_not_zeroed = start; 37133d392b26STheodore Ts'o if (test_opt(sb, PREFETCH_BLOCK_BITMAPS)) 37143d392b26STheodore Ts'o elr->lr_mode = EXT4_LI_MODE_PREFETCH_BBITMAP; 37153d392b26STheodore Ts'o else { 37163d392b26STheodore Ts'o elr->lr_mode = EXT4_LI_MODE_ITABLE; 3717bfff6873SLukas Czerner elr->lr_next_group = start; 37183d392b26STheodore Ts'o } 3719bfff6873SLukas Czerner 3720bfff6873SLukas Czerner /* 3721bfff6873SLukas Czerner * Randomize first schedule time of the request to 3722bfff6873SLukas Czerner * spread the inode table initialization requests 3723bfff6873SLukas Czerner * better. 3724bfff6873SLukas Czerner */ 3725dd1f723bSTheodore Ts'o elr->lr_next_sched = jiffies + (prandom_u32() % 3726dd1f723bSTheodore Ts'o (EXT4_DEF_LI_MAX_START_DELAY * HZ)); 3727bfff6873SLukas Czerner return elr; 3728bfff6873SLukas Czerner } 3729bfff6873SLukas Czerner 37307f511862STheodore Ts'o int ext4_register_li_request(struct super_block *sb, 3731bfff6873SLukas Czerner ext4_group_t first_not_zeroed) 3732bfff6873SLukas Czerner { 3733bfff6873SLukas Czerner struct ext4_sb_info *sbi = EXT4_SB(sb); 37347f511862STheodore Ts'o struct ext4_li_request *elr = NULL; 373549598e04SJun Piao ext4_group_t ngroups = sbi->s_groups_count; 37366c5a6cb9SAndrew Morton int ret = 0; 3737bfff6873SLukas Czerner 37387f511862STheodore Ts'o mutex_lock(&ext4_li_mtx); 373951ce6511SLukas Czerner if (sbi->s_li_request != NULL) { 374051ce6511SLukas Czerner /* 374151ce6511SLukas Czerner * Reset timeout so it can be computed again, because 374251ce6511SLukas Czerner * s_li_wait_mult might have changed. 374351ce6511SLukas Czerner */ 374451ce6511SLukas Czerner sbi->s_li_request->lr_timeout = 0; 37457f511862STheodore Ts'o goto out; 374651ce6511SLukas Czerner } 3747bfff6873SLukas Czerner 37483d392b26STheodore Ts'o if (!test_opt(sb, PREFETCH_BLOCK_BITMAPS) && 37493d392b26STheodore Ts'o (first_not_zeroed == ngroups || sb_rdonly(sb) || 37503d392b26STheodore Ts'o !test_opt(sb, INIT_INODE_TABLE))) 37517f511862STheodore Ts'o goto out; 3752bfff6873SLukas Czerner 3753bfff6873SLukas Czerner elr = ext4_li_request_new(sb, first_not_zeroed); 37547f511862STheodore Ts'o if (!elr) { 37557f511862STheodore Ts'o ret = -ENOMEM; 37567f511862STheodore Ts'o goto out; 37577f511862STheodore Ts'o } 3758bfff6873SLukas Czerner 3759bfff6873SLukas Czerner if (NULL == ext4_li_info) { 3760bfff6873SLukas Czerner ret = ext4_li_info_new(); 3761bfff6873SLukas Czerner if (ret) 3762bfff6873SLukas Czerner goto out; 3763bfff6873SLukas Czerner } 3764bfff6873SLukas Czerner 3765bfff6873SLukas Czerner mutex_lock(&ext4_li_info->li_list_mtx); 3766bfff6873SLukas Czerner list_add(&elr->lr_request, &ext4_li_info->li_request_list); 3767bfff6873SLukas Czerner mutex_unlock(&ext4_li_info->li_list_mtx); 3768bfff6873SLukas Czerner 3769bfff6873SLukas Czerner sbi->s_li_request = elr; 377046e4690bSTao Ma /* 377146e4690bSTao Ma * set elr to NULL here since it has been inserted to 377246e4690bSTao Ma * the request_list and the removal and free of it is 377346e4690bSTao Ma * handled by ext4_clear_request_list from now on. 377446e4690bSTao Ma */ 377546e4690bSTao Ma elr = NULL; 3776bfff6873SLukas Czerner 3777bfff6873SLukas Czerner if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) { 3778bfff6873SLukas Czerner ret = ext4_run_lazyinit_thread(); 3779bfff6873SLukas Czerner if (ret) 3780bfff6873SLukas Czerner goto out; 3781bfff6873SLukas Czerner } 3782bfff6873SLukas Czerner out: 3783bfff6873SLukas Czerner mutex_unlock(&ext4_li_mtx); 3784beed5ecbSNicolas Kaiser if (ret) 3785bfff6873SLukas Czerner kfree(elr); 3786bfff6873SLukas Czerner return ret; 3787bfff6873SLukas Czerner } 3788bfff6873SLukas Czerner 3789bfff6873SLukas Czerner /* 3790bfff6873SLukas Czerner * We do not need to lock anything since this is called on 3791bfff6873SLukas Czerner * module unload. 3792bfff6873SLukas Czerner */ 3793bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void) 3794bfff6873SLukas Czerner { 3795bfff6873SLukas Czerner /* 3796bfff6873SLukas Czerner * If thread exited earlier 3797bfff6873SLukas Czerner * there's nothing to be done. 3798bfff6873SLukas Czerner */ 37998f1f7453SEric Sandeen if (!ext4_li_info || !ext4_lazyinit_task) 3800bfff6873SLukas Czerner return; 3801bfff6873SLukas Czerner 38028f1f7453SEric Sandeen kthread_stop(ext4_lazyinit_task); 3803bfff6873SLukas Czerner } 3804bfff6873SLukas Czerner 380525ed6e8aSDarrick J. Wong static int set_journal_csum_feature_set(struct super_block *sb) 380625ed6e8aSDarrick J. Wong { 380725ed6e8aSDarrick J. Wong int ret = 1; 380825ed6e8aSDarrick J. Wong int compat, incompat; 380925ed6e8aSDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(sb); 381025ed6e8aSDarrick J. Wong 38119aa5d32bSDmitry Monakhov if (ext4_has_metadata_csum(sb)) { 3812db9ee220SDarrick J. Wong /* journal checksum v3 */ 381325ed6e8aSDarrick J. Wong compat = 0; 3814db9ee220SDarrick J. Wong incompat = JBD2_FEATURE_INCOMPAT_CSUM_V3; 381525ed6e8aSDarrick J. Wong } else { 381625ed6e8aSDarrick J. Wong /* journal checksum v1 */ 381725ed6e8aSDarrick J. Wong compat = JBD2_FEATURE_COMPAT_CHECKSUM; 381825ed6e8aSDarrick J. Wong incompat = 0; 381925ed6e8aSDarrick J. Wong } 382025ed6e8aSDarrick J. Wong 3821feb8c6d3SDarrick J. Wong jbd2_journal_clear_features(sbi->s_journal, 3822feb8c6d3SDarrick J. Wong JBD2_FEATURE_COMPAT_CHECKSUM, 0, 3823feb8c6d3SDarrick J. Wong JBD2_FEATURE_INCOMPAT_CSUM_V3 | 3824feb8c6d3SDarrick J. Wong JBD2_FEATURE_INCOMPAT_CSUM_V2); 382525ed6e8aSDarrick J. Wong if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) { 382625ed6e8aSDarrick J. Wong ret = jbd2_journal_set_features(sbi->s_journal, 382725ed6e8aSDarrick J. Wong compat, 0, 382825ed6e8aSDarrick J. Wong JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT | 382925ed6e8aSDarrick J. Wong incompat); 383025ed6e8aSDarrick J. Wong } else if (test_opt(sb, JOURNAL_CHECKSUM)) { 383125ed6e8aSDarrick J. Wong ret = jbd2_journal_set_features(sbi->s_journal, 383225ed6e8aSDarrick J. Wong compat, 0, 383325ed6e8aSDarrick J. Wong incompat); 383425ed6e8aSDarrick J. Wong jbd2_journal_clear_features(sbi->s_journal, 0, 0, 383525ed6e8aSDarrick J. Wong JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT); 383625ed6e8aSDarrick J. Wong } else { 3837feb8c6d3SDarrick J. Wong jbd2_journal_clear_features(sbi->s_journal, 0, 0, 3838feb8c6d3SDarrick J. Wong JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT); 383925ed6e8aSDarrick J. Wong } 384025ed6e8aSDarrick J. Wong 384125ed6e8aSDarrick J. Wong return ret; 384225ed6e8aSDarrick J. Wong } 384325ed6e8aSDarrick J. Wong 3844952fc18eSTheodore Ts'o /* 3845952fc18eSTheodore Ts'o * Note: calculating the overhead so we can be compatible with 3846952fc18eSTheodore Ts'o * historical BSD practice is quite difficult in the face of 3847952fc18eSTheodore Ts'o * clusters/bigalloc. This is because multiple metadata blocks from 3848952fc18eSTheodore Ts'o * different block group can end up in the same allocation cluster. 3849952fc18eSTheodore Ts'o * Calculating the exact overhead in the face of clustered allocation 3850952fc18eSTheodore Ts'o * requires either O(all block bitmaps) in memory or O(number of block 3851952fc18eSTheodore Ts'o * groups**2) in time. We will still calculate the superblock for 3852952fc18eSTheodore Ts'o * older file systems --- and if we come across with a bigalloc file 3853952fc18eSTheodore Ts'o * system with zero in s_overhead_clusters the estimate will be close to 3854952fc18eSTheodore Ts'o * correct especially for very large cluster sizes --- but for newer 3855952fc18eSTheodore Ts'o * file systems, it's better to calculate this figure once at mkfs 3856952fc18eSTheodore Ts'o * time, and store it in the superblock. If the superblock value is 3857952fc18eSTheodore Ts'o * present (even for non-bigalloc file systems), we will use it. 3858952fc18eSTheodore Ts'o */ 3859952fc18eSTheodore Ts'o static int count_overhead(struct super_block *sb, ext4_group_t grp, 3860952fc18eSTheodore Ts'o char *buf) 3861952fc18eSTheodore Ts'o { 3862952fc18eSTheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(sb); 3863952fc18eSTheodore Ts'o struct ext4_group_desc *gdp; 3864952fc18eSTheodore Ts'o ext4_fsblk_t first_block, last_block, b; 3865952fc18eSTheodore Ts'o ext4_group_t i, ngroups = ext4_get_groups_count(sb); 3866952fc18eSTheodore Ts'o int s, j, count = 0; 3867952fc18eSTheodore Ts'o 3868e2b911c5SDarrick J. Wong if (!ext4_has_feature_bigalloc(sb)) 38690548bbb8STheodore Ts'o return (ext4_bg_has_super(sb, grp) + ext4_bg_num_gdb(sb, grp) + 38700548bbb8STheodore Ts'o sbi->s_itb_per_group + 2); 38710548bbb8STheodore Ts'o 3872952fc18eSTheodore Ts'o first_block = le32_to_cpu(sbi->s_es->s_first_data_block) + 3873952fc18eSTheodore Ts'o (grp * EXT4_BLOCKS_PER_GROUP(sb)); 3874952fc18eSTheodore Ts'o last_block = first_block + EXT4_BLOCKS_PER_GROUP(sb) - 1; 3875952fc18eSTheodore Ts'o for (i = 0; i < ngroups; i++) { 3876952fc18eSTheodore Ts'o gdp = ext4_get_group_desc(sb, i, NULL); 3877952fc18eSTheodore Ts'o b = ext4_block_bitmap(sb, gdp); 3878952fc18eSTheodore Ts'o if (b >= first_block && b <= last_block) { 3879952fc18eSTheodore Ts'o ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf); 3880952fc18eSTheodore Ts'o count++; 3881952fc18eSTheodore Ts'o } 3882952fc18eSTheodore Ts'o b = ext4_inode_bitmap(sb, gdp); 3883952fc18eSTheodore Ts'o if (b >= first_block && b <= last_block) { 3884952fc18eSTheodore Ts'o ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf); 3885952fc18eSTheodore Ts'o count++; 3886952fc18eSTheodore Ts'o } 3887952fc18eSTheodore Ts'o b = ext4_inode_table(sb, gdp); 3888952fc18eSTheodore Ts'o if (b >= first_block && b + sbi->s_itb_per_group <= last_block) 3889952fc18eSTheodore Ts'o for (j = 0; j < sbi->s_itb_per_group; j++, b++) { 3890952fc18eSTheodore Ts'o int c = EXT4_B2C(sbi, b - first_block); 3891952fc18eSTheodore Ts'o ext4_set_bit(c, buf); 3892952fc18eSTheodore Ts'o count++; 3893952fc18eSTheodore Ts'o } 3894952fc18eSTheodore Ts'o if (i != grp) 3895952fc18eSTheodore Ts'o continue; 3896952fc18eSTheodore Ts'o s = 0; 3897952fc18eSTheodore Ts'o if (ext4_bg_has_super(sb, grp)) { 3898952fc18eSTheodore Ts'o ext4_set_bit(s++, buf); 3899952fc18eSTheodore Ts'o count++; 3900952fc18eSTheodore Ts'o } 3901c48ae41bSTheodore Ts'o j = ext4_bg_num_gdb(sb, grp); 3902c48ae41bSTheodore Ts'o if (s + j > EXT4_BLOCKS_PER_GROUP(sb)) { 3903c48ae41bSTheodore Ts'o ext4_error(sb, "Invalid number of block group " 3904c48ae41bSTheodore Ts'o "descriptor blocks: %d", j); 3905c48ae41bSTheodore Ts'o j = EXT4_BLOCKS_PER_GROUP(sb) - s; 3906952fc18eSTheodore Ts'o } 3907c48ae41bSTheodore Ts'o count += j; 3908c48ae41bSTheodore Ts'o for (; j > 0; j--) 3909c48ae41bSTheodore Ts'o ext4_set_bit(EXT4_B2C(sbi, s++), buf); 3910952fc18eSTheodore Ts'o } 3911952fc18eSTheodore Ts'o if (!count) 3912952fc18eSTheodore Ts'o return 0; 3913952fc18eSTheodore Ts'o return EXT4_CLUSTERS_PER_GROUP(sb) - 3914952fc18eSTheodore Ts'o ext4_count_free(buf, EXT4_CLUSTERS_PER_GROUP(sb) / 8); 3915952fc18eSTheodore Ts'o } 3916952fc18eSTheodore Ts'o 3917952fc18eSTheodore Ts'o /* 3918952fc18eSTheodore Ts'o * Compute the overhead and stash it in sbi->s_overhead 3919952fc18eSTheodore Ts'o */ 3920952fc18eSTheodore Ts'o int ext4_calculate_overhead(struct super_block *sb) 3921952fc18eSTheodore Ts'o { 3922952fc18eSTheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(sb); 3923952fc18eSTheodore Ts'o struct ext4_super_block *es = sbi->s_es; 39243c816dedSEric Whitney struct inode *j_inode; 39253c816dedSEric Whitney unsigned int j_blocks, j_inum = le32_to_cpu(es->s_journal_inum); 3926952fc18eSTheodore Ts'o ext4_group_t i, ngroups = ext4_get_groups_count(sb); 3927952fc18eSTheodore Ts'o ext4_fsblk_t overhead = 0; 39284fdb5543SDmitry Monakhov char *buf = (char *) get_zeroed_page(GFP_NOFS); 3929952fc18eSTheodore Ts'o 3930952fc18eSTheodore Ts'o if (!buf) 3931952fc18eSTheodore Ts'o return -ENOMEM; 3932952fc18eSTheodore Ts'o 3933952fc18eSTheodore Ts'o /* 3934952fc18eSTheodore Ts'o * Compute the overhead (FS structures). This is constant 3935952fc18eSTheodore Ts'o * for a given filesystem unless the number of block groups 3936952fc18eSTheodore Ts'o * changes so we cache the previous value until it does. 3937952fc18eSTheodore Ts'o */ 3938952fc18eSTheodore Ts'o 3939952fc18eSTheodore Ts'o /* 3940952fc18eSTheodore Ts'o * All of the blocks before first_data_block are overhead 3941952fc18eSTheodore Ts'o */ 3942952fc18eSTheodore Ts'o overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block)); 3943952fc18eSTheodore Ts'o 3944952fc18eSTheodore Ts'o /* 3945952fc18eSTheodore Ts'o * Add the overhead found in each block group 3946952fc18eSTheodore Ts'o */ 3947952fc18eSTheodore Ts'o for (i = 0; i < ngroups; i++) { 3948952fc18eSTheodore Ts'o int blks; 3949952fc18eSTheodore Ts'o 3950952fc18eSTheodore Ts'o blks = count_overhead(sb, i, buf); 3951952fc18eSTheodore Ts'o overhead += blks; 3952952fc18eSTheodore Ts'o if (blks) 3953952fc18eSTheodore Ts'o memset(buf, 0, PAGE_SIZE); 3954952fc18eSTheodore Ts'o cond_resched(); 3955952fc18eSTheodore Ts'o } 39563c816dedSEric Whitney 39573c816dedSEric Whitney /* 39583c816dedSEric Whitney * Add the internal journal blocks whether the journal has been 39593c816dedSEric Whitney * loaded or not 39603c816dedSEric Whitney */ 3961ee7ed3aaSChunguang Xu if (sbi->s_journal && !sbi->s_journal_bdev) 3962810da240SLukas Czerner overhead += EXT4_NUM_B2C(sbi, sbi->s_journal->j_maxlen); 3963f1eec3b0SRitesh Harjani else if (ext4_has_feature_journal(sb) && !sbi->s_journal && j_inum) { 3964f1eec3b0SRitesh Harjani /* j_inum for internal journal is non-zero */ 39653c816dedSEric Whitney j_inode = ext4_get_journal_inode(sb, j_inum); 39663c816dedSEric Whitney if (j_inode) { 39673c816dedSEric Whitney j_blocks = j_inode->i_size >> sb->s_blocksize_bits; 39683c816dedSEric Whitney overhead += EXT4_NUM_B2C(sbi, j_blocks); 39693c816dedSEric Whitney iput(j_inode); 39703c816dedSEric Whitney } else { 39713c816dedSEric Whitney ext4_msg(sb, KERN_ERR, "can't get journal size"); 39723c816dedSEric Whitney } 39733c816dedSEric Whitney } 3974952fc18eSTheodore Ts'o sbi->s_overhead = overhead; 3975952fc18eSTheodore Ts'o smp_wmb(); 3976952fc18eSTheodore Ts'o free_page((unsigned long) buf); 3977952fc18eSTheodore Ts'o return 0; 3978952fc18eSTheodore Ts'o } 3979952fc18eSTheodore Ts'o 3980b5799018STheodore Ts'o static void ext4_set_resv_clusters(struct super_block *sb) 398127dd4385SLukas Czerner { 398227dd4385SLukas Czerner ext4_fsblk_t resv_clusters; 3983b5799018STheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(sb); 398427dd4385SLukas Czerner 398527dd4385SLukas Czerner /* 398630fac0f7SJan Kara * There's no need to reserve anything when we aren't using extents. 398730fac0f7SJan Kara * The space estimates are exact, there are no unwritten extents, 398830fac0f7SJan Kara * hole punching doesn't need new metadata... This is needed especially 398930fac0f7SJan Kara * to keep ext2/3 backward compatibility. 399030fac0f7SJan Kara */ 3991e2b911c5SDarrick J. Wong if (!ext4_has_feature_extents(sb)) 3992b5799018STheodore Ts'o return; 399330fac0f7SJan Kara /* 399427dd4385SLukas Czerner * By default we reserve 2% or 4096 clusters, whichever is smaller. 399527dd4385SLukas Czerner * This should cover the situations where we can not afford to run 399627dd4385SLukas Czerner * out of space like for example punch hole, or converting 3997556615dcSLukas Czerner * unwritten extents in delalloc path. In most cases such 399827dd4385SLukas Czerner * allocation would require 1, or 2 blocks, higher numbers are 399927dd4385SLukas Czerner * very rare. 400027dd4385SLukas Czerner */ 4001b5799018STheodore Ts'o resv_clusters = (ext4_blocks_count(sbi->s_es) >> 4002b5799018STheodore Ts'o sbi->s_cluster_bits); 400327dd4385SLukas Czerner 400427dd4385SLukas Czerner do_div(resv_clusters, 50); 400527dd4385SLukas Czerner resv_clusters = min_t(ext4_fsblk_t, resv_clusters, 4096); 400627dd4385SLukas Czerner 4007b5799018STheodore Ts'o atomic64_set(&sbi->s_resv_clusters, resv_clusters); 400827dd4385SLukas Czerner } 400927dd4385SLukas Czerner 4010617ba13bSMingming Cao static int ext4_fill_super(struct super_block *sb, void *data, int silent) 4011ac27a0ecSDave Kleikamp { 40125e405595SDan Williams struct dax_device *dax_dev = fs_dax_get_by_bdev(sb->s_bdev); 4013d4c402d9SCurt Wohlgemuth char *orig_data = kstrdup(data, GFP_KERNEL); 40141d0c3924STheodore Ts'o struct buffer_head *bh, **group_desc; 4015617ba13bSMingming Cao struct ext4_super_block *es = NULL; 40165aee0f8aSTheodore Ts'o struct ext4_sb_info *sbi = kzalloc(sizeof(*sbi), GFP_KERNEL); 40177c990728SSuraj Jitindar Singh struct flex_groups **flex_groups; 4018617ba13bSMingming Cao ext4_fsblk_t block; 4019617ba13bSMingming Cao ext4_fsblk_t sb_block = get_sb_block(&data); 402070bbb3e0SAndrew Morton ext4_fsblk_t logical_sb_block; 4021ac27a0ecSDave Kleikamp unsigned long offset = 0; 4022ac27a0ecSDave Kleikamp unsigned long journal_devnum = 0; 4023ac27a0ecSDave Kleikamp unsigned long def_mount_opts; 4024ac27a0ecSDave Kleikamp struct inode *root; 40250390131bSFrank Mayhar const char *descr; 4026dcc7dae3SCyrill Gorcunov int ret = -ENOMEM; 4027281b5995STheodore Ts'o int blocksize, clustersize; 40284ec11028STheodore Ts'o unsigned int db_count; 40294ec11028STheodore Ts'o unsigned int i; 4030ef5fd681SKaixu Xia int needs_recovery, has_huge_files; 4031bd81d8eeSLaurent Vivier __u64 blocks_count; 403207aa2ea1SLukas Czerner int err = 0; 4033b3881f74STheodore Ts'o unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO; 4034bfff6873SLukas Czerner ext4_group_t first_not_zeroed; 4035ac27a0ecSDave Kleikamp 40365aee0f8aSTheodore Ts'o if ((data && !orig_data) || !sbi) 40375aee0f8aSTheodore Ts'o goto out_free_base; 4038705895b6SPekka Enberg 4039aed9eb1bSColin Ian King sbi->s_daxdev = dax_dev; 4040705895b6SPekka Enberg sbi->s_blockgroup_lock = 4041705895b6SPekka Enberg kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL); 40425aee0f8aSTheodore Ts'o if (!sbi->s_blockgroup_lock) 40435aee0f8aSTheodore Ts'o goto out_free_base; 40445aee0f8aSTheodore Ts'o 4045ac27a0ecSDave Kleikamp sb->s_fs_info = sbi; 40462c0544b2STheodore Ts'o sbi->s_sb = sb; 4047240799cdSTheodore Ts'o sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS; 4048d9c9bef1SMiklos Szeredi sbi->s_sb_block = sb_block; 4049f613dfcbSTheodore Ts'o if (sb->s_bdev->bd_part) 4050f613dfcbSTheodore Ts'o sbi->s_sectors_written_start = 4051dbae2c55SMichael Callahan part_stat_read(sb->s_bdev->bd_part, sectors[STAT_WRITE]); 4052ac27a0ecSDave Kleikamp 40539f6200bbSTheodore Ts'o /* Cleanup superblock name */ 4054ec3904dcSRasmus Villemoes strreplace(sb->s_id, '/', '!'); 40559f6200bbSTheodore Ts'o 405607aa2ea1SLukas Czerner /* -EINVAL is default */ 4057dcc7dae3SCyrill Gorcunov ret = -EINVAL; 4058617ba13bSMingming Cao blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE); 4059ac27a0ecSDave Kleikamp if (!blocksize) { 4060b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "unable to set blocksize"); 4061ac27a0ecSDave Kleikamp goto out_fail; 4062ac27a0ecSDave Kleikamp } 4063ac27a0ecSDave Kleikamp 4064ac27a0ecSDave Kleikamp /* 4065617ba13bSMingming Cao * The ext4 superblock will not be buffer aligned for other than 1kB 4066ac27a0ecSDave Kleikamp * block sizes. We need to calculate the offset from buffer start. 4067ac27a0ecSDave Kleikamp */ 4068617ba13bSMingming Cao if (blocksize != EXT4_MIN_BLOCK_SIZE) { 406970bbb3e0SAndrew Morton logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE; 407070bbb3e0SAndrew Morton offset = do_div(logical_sb_block, blocksize); 4071ac27a0ecSDave Kleikamp } else { 407270bbb3e0SAndrew Morton logical_sb_block = sb_block; 4073ac27a0ecSDave Kleikamp } 4074ac27a0ecSDave Kleikamp 40758394a6abSzhangyi (F) bh = ext4_sb_bread_unmovable(sb, logical_sb_block); 40768394a6abSzhangyi (F) if (IS_ERR(bh)) { 4077b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "unable to read superblock"); 40788394a6abSzhangyi (F) ret = PTR_ERR(bh); 40798394a6abSzhangyi (F) bh = NULL; 4080ac27a0ecSDave Kleikamp goto out_fail; 4081ac27a0ecSDave Kleikamp } 4082ac27a0ecSDave Kleikamp /* 4083ac27a0ecSDave Kleikamp * Note: s_es must be initialized as soon as possible because 4084617ba13bSMingming Cao * some ext4 macro-instructions depend on its value 4085ac27a0ecSDave Kleikamp */ 40862716b802STheodore Ts'o es = (struct ext4_super_block *) (bh->b_data + offset); 4087ac27a0ecSDave Kleikamp sbi->s_es = es; 4088ac27a0ecSDave Kleikamp sb->s_magic = le16_to_cpu(es->s_magic); 4089617ba13bSMingming Cao if (sb->s_magic != EXT4_SUPER_MAGIC) 4090617ba13bSMingming Cao goto cantfind_ext4; 4091afc32f7eSTheodore Ts'o sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written); 4092ac27a0ecSDave Kleikamp 4093feb0ab32SDarrick J. Wong /* Warn if metadata_csum and gdt_csum are both set. */ 4094e2b911c5SDarrick J. Wong if (ext4_has_feature_metadata_csum(sb) && 4095e2b911c5SDarrick J. Wong ext4_has_feature_gdt_csum(sb)) 4096363307e6SJakub Wilk ext4_warning(sb, "metadata_csum and uninit_bg are " 4097feb0ab32SDarrick J. Wong "redundant flags; please run fsck."); 4098feb0ab32SDarrick J. Wong 4099d25425f8SDarrick J. Wong /* Check for a known checksum algorithm */ 4100d25425f8SDarrick J. Wong if (!ext4_verify_csum_type(sb, es)) { 4101d25425f8SDarrick J. Wong ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with " 4102d25425f8SDarrick J. Wong "unknown checksum algorithm."); 4103d25425f8SDarrick J. Wong silent = 1; 4104d25425f8SDarrick J. Wong goto cantfind_ext4; 4105d25425f8SDarrick J. Wong } 4106d25425f8SDarrick J. Wong 41070441984aSDarrick J. Wong /* Load the checksum driver */ 41080441984aSDarrick J. Wong sbi->s_chksum_driver = crypto_alloc_shash("crc32c", 0, 0); 41090441984aSDarrick J. Wong if (IS_ERR(sbi->s_chksum_driver)) { 41100441984aSDarrick J. Wong ext4_msg(sb, KERN_ERR, "Cannot load crc32c driver."); 41110441984aSDarrick J. Wong ret = PTR_ERR(sbi->s_chksum_driver); 41120441984aSDarrick J. Wong sbi->s_chksum_driver = NULL; 41130441984aSDarrick J. Wong goto failed_mount; 41140441984aSDarrick J. Wong } 41150441984aSDarrick J. Wong 4116a9c47317SDarrick J. Wong /* Check superblock checksum */ 4117a9c47317SDarrick J. Wong if (!ext4_superblock_csum_verify(sb, es)) { 4118a9c47317SDarrick J. Wong ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with " 4119a9c47317SDarrick J. Wong "invalid superblock checksum. Run e2fsck?"); 4120a9c47317SDarrick J. Wong silent = 1; 41216a797d27SDarrick J. Wong ret = -EFSBADCRC; 4122a9c47317SDarrick J. Wong goto cantfind_ext4; 4123a9c47317SDarrick J. Wong } 4124a9c47317SDarrick J. Wong 4125a9c47317SDarrick J. Wong /* Precompute checksum seed for all metadata */ 4126e2b911c5SDarrick J. Wong if (ext4_has_feature_csum_seed(sb)) 41278c81bd8fSDarrick J. Wong sbi->s_csum_seed = le32_to_cpu(es->s_checksum_seed); 4128dec214d0STahsin Erdogan else if (ext4_has_metadata_csum(sb) || ext4_has_feature_ea_inode(sb)) 4129a9c47317SDarrick J. Wong sbi->s_csum_seed = ext4_chksum(sbi, ~0, es->s_uuid, 4130a9c47317SDarrick J. Wong sizeof(es->s_uuid)); 4131a9c47317SDarrick J. Wong 4132ac27a0ecSDave Kleikamp /* Set defaults before we parse the mount options */ 4133ac27a0ecSDave Kleikamp def_mount_opts = le32_to_cpu(es->s_default_mount_opts); 4134fd8c37ecSTheodore Ts'o set_opt(sb, INIT_INODE_TABLE); 4135617ba13bSMingming Cao if (def_mount_opts & EXT4_DEFM_DEBUG) 4136fd8c37ecSTheodore Ts'o set_opt(sb, DEBUG); 413787f26807STheodore Ts'o if (def_mount_opts & EXT4_DEFM_BSDGROUPS) 4138fd8c37ecSTheodore Ts'o set_opt(sb, GRPID); 4139617ba13bSMingming Cao if (def_mount_opts & EXT4_DEFM_UID16) 4140fd8c37ecSTheodore Ts'o set_opt(sb, NO_UID32); 4141ea663336SEric Sandeen /* xattr user namespace & acls are now defaulted on */ 4142fd8c37ecSTheodore Ts'o set_opt(sb, XATTR_USER); 414303010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL 4144fd8c37ecSTheodore Ts'o set_opt(sb, POSIX_ACL); 41452e7842b8SHugh Dickins #endif 4146995a3ed6SHarshad Shirwadkar if (ext4_has_feature_fast_commit(sb)) 4147995a3ed6SHarshad Shirwadkar set_opt2(sb, JOURNAL_FAST_COMMIT); 414898c1a759SDarrick J. Wong /* don't forget to enable journal_csum when metadata_csum is enabled. */ 414998c1a759SDarrick J. Wong if (ext4_has_metadata_csum(sb)) 415098c1a759SDarrick J. Wong set_opt(sb, JOURNAL_CHECKSUM); 415198c1a759SDarrick J. Wong 4152617ba13bSMingming Cao if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA) 4153fd8c37ecSTheodore Ts'o set_opt(sb, JOURNAL_DATA); 4154617ba13bSMingming Cao else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED) 4155fd8c37ecSTheodore Ts'o set_opt(sb, ORDERED_DATA); 4156617ba13bSMingming Cao else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK) 4157fd8c37ecSTheodore Ts'o set_opt(sb, WRITEBACK_DATA); 4158ac27a0ecSDave Kleikamp 4159617ba13bSMingming Cao if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC) 4160fd8c37ecSTheodore Ts'o set_opt(sb, ERRORS_PANIC); 4161bb4f397aSAneesh Kumar K.V else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE) 4162fd8c37ecSTheodore Ts'o set_opt(sb, ERRORS_CONT); 4163bb4f397aSAneesh Kumar K.V else 4164fd8c37ecSTheodore Ts'o set_opt(sb, ERRORS_RO); 416545f1a9c3SDarrick J. Wong /* block_validity enabled by default; disable with noblock_validity */ 4166fd8c37ecSTheodore Ts'o set_opt(sb, BLOCK_VALIDITY); 41678b67f04aSTheodore Ts'o if (def_mount_opts & EXT4_DEFM_DISCARD) 4168fd8c37ecSTheodore Ts'o set_opt(sb, DISCARD); 4169ac27a0ecSDave Kleikamp 417008cefc7aSEric W. Biederman sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid)); 417108cefc7aSEric W. Biederman sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid)); 417230773840STheodore Ts'o sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ; 417330773840STheodore Ts'o sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME; 417430773840STheodore Ts'o sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME; 4175ac27a0ecSDave Kleikamp 41768b67f04aSTheodore Ts'o if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0) 4177fd8c37ecSTheodore Ts'o set_opt(sb, BARRIER); 4178ac27a0ecSDave Kleikamp 41791e2462f9SMingming Cao /* 4180dd919b98SAneesh Kumar K.V * enable delayed allocation by default 4181dd919b98SAneesh Kumar K.V * Use -o nodelalloc to turn it off 4182dd919b98SAneesh Kumar K.V */ 4183bc0b75f7STheodore Ts'o if (!IS_EXT3_SB(sb) && !IS_EXT2_SB(sb) && 41848b67f04aSTheodore Ts'o ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0)) 4185fd8c37ecSTheodore Ts'o set_opt(sb, DELALLOC); 4186dd919b98SAneesh Kumar K.V 418751ce6511SLukas Czerner /* 418851ce6511SLukas Czerner * set default s_li_wait_mult for lazyinit, for the case there is 418951ce6511SLukas Czerner * no mount option specified. 419051ce6511SLukas Czerner */ 419151ce6511SLukas Czerner sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT; 419251ce6511SLukas Czerner 41934f97a681STheodore Ts'o blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size); 4194626b035bSRitesh Harjani 4195626b035bSRitesh Harjani if (blocksize == PAGE_SIZE) 4196626b035bSRitesh Harjani set_opt(sb, DIOREAD_NOLOCK); 4197626b035bSRitesh Harjani 41984f97a681STheodore Ts'o if (blocksize < EXT4_MIN_BLOCK_SIZE || 41994f97a681STheodore Ts'o blocksize > EXT4_MAX_BLOCK_SIZE) { 42004f97a681STheodore Ts'o ext4_msg(sb, KERN_ERR, 42014f97a681STheodore Ts'o "Unsupported filesystem blocksize %d (%d log_block_size)", 42024f97a681STheodore Ts'o blocksize, le32_to_cpu(es->s_log_block_size)); 42034f97a681STheodore Ts'o goto failed_mount; 42044f97a681STheodore Ts'o } 42054f97a681STheodore Ts'o 42069803387cSTheodore Ts'o if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) { 42079803387cSTheodore Ts'o sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE; 42089803387cSTheodore Ts'o sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO; 42099803387cSTheodore Ts'o } else { 42109803387cSTheodore Ts'o sbi->s_inode_size = le16_to_cpu(es->s_inode_size); 42119803387cSTheodore Ts'o sbi->s_first_ino = le32_to_cpu(es->s_first_ino); 42129803387cSTheodore Ts'o if (sbi->s_first_ino < EXT4_GOOD_OLD_FIRST_INO) { 42139803387cSTheodore Ts'o ext4_msg(sb, KERN_ERR, "invalid first ino: %u", 42149803387cSTheodore Ts'o sbi->s_first_ino); 42159803387cSTheodore Ts'o goto failed_mount; 42169803387cSTheodore Ts'o } 42179803387cSTheodore Ts'o if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) || 42189803387cSTheodore Ts'o (!is_power_of_2(sbi->s_inode_size)) || 42199803387cSTheodore Ts'o (sbi->s_inode_size > blocksize)) { 42209803387cSTheodore Ts'o ext4_msg(sb, KERN_ERR, 42219803387cSTheodore Ts'o "unsupported inode size: %d", 42229803387cSTheodore Ts'o sbi->s_inode_size); 42234f97a681STheodore Ts'o ext4_msg(sb, KERN_ERR, "blocksize: %d", blocksize); 42249803387cSTheodore Ts'o goto failed_mount; 42259803387cSTheodore Ts'o } 42269803387cSTheodore Ts'o /* 42279803387cSTheodore Ts'o * i_atime_extra is the last extra field available for 42289803387cSTheodore Ts'o * [acm]times in struct ext4_inode. Checking for that 42299803387cSTheodore Ts'o * field should suffice to ensure we have extra space 42309803387cSTheodore Ts'o * for all three. 42319803387cSTheodore Ts'o */ 42329803387cSTheodore Ts'o if (sbi->s_inode_size >= offsetof(struct ext4_inode, i_atime_extra) + 42339803387cSTheodore Ts'o sizeof(((struct ext4_inode *)0)->i_atime_extra)) { 42349803387cSTheodore Ts'o sb->s_time_gran = 1; 42359803387cSTheodore Ts'o sb->s_time_max = EXT4_EXTRA_TIMESTAMP_MAX; 42369803387cSTheodore Ts'o } else { 42379803387cSTheodore Ts'o sb->s_time_gran = NSEC_PER_SEC; 42389803387cSTheodore Ts'o sb->s_time_max = EXT4_NON_EXTRA_TIMESTAMP_MAX; 42399803387cSTheodore Ts'o } 42409803387cSTheodore Ts'o sb->s_time_min = EXT4_TIMESTAMP_MIN; 42419803387cSTheodore Ts'o } 42429803387cSTheodore Ts'o if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) { 42439803387cSTheodore Ts'o sbi->s_want_extra_isize = sizeof(struct ext4_inode) - 42449803387cSTheodore Ts'o EXT4_GOOD_OLD_INODE_SIZE; 42459803387cSTheodore Ts'o if (ext4_has_feature_extra_isize(sb)) { 42469803387cSTheodore Ts'o unsigned v, max = (sbi->s_inode_size - 42479803387cSTheodore Ts'o EXT4_GOOD_OLD_INODE_SIZE); 42489803387cSTheodore Ts'o 42499803387cSTheodore Ts'o v = le16_to_cpu(es->s_want_extra_isize); 42509803387cSTheodore Ts'o if (v > max) { 42519803387cSTheodore Ts'o ext4_msg(sb, KERN_ERR, 42529803387cSTheodore Ts'o "bad s_want_extra_isize: %d", v); 42539803387cSTheodore Ts'o goto failed_mount; 42549803387cSTheodore Ts'o } 42559803387cSTheodore Ts'o if (sbi->s_want_extra_isize < v) 42569803387cSTheodore Ts'o sbi->s_want_extra_isize = v; 42579803387cSTheodore Ts'o 42589803387cSTheodore Ts'o v = le16_to_cpu(es->s_min_extra_isize); 42599803387cSTheodore Ts'o if (v > max) { 42609803387cSTheodore Ts'o ext4_msg(sb, KERN_ERR, 42619803387cSTheodore Ts'o "bad s_min_extra_isize: %d", v); 42629803387cSTheodore Ts'o goto failed_mount; 42639803387cSTheodore Ts'o } 42649803387cSTheodore Ts'o if (sbi->s_want_extra_isize < v) 42659803387cSTheodore Ts'o sbi->s_want_extra_isize = v; 42669803387cSTheodore Ts'o } 42679803387cSTheodore Ts'o } 42689803387cSTheodore Ts'o 42695aee0f8aSTheodore Ts'o if (sbi->s_es->s_mount_opts[0]) { 42705aee0f8aSTheodore Ts'o char *s_mount_opts = kstrndup(sbi->s_es->s_mount_opts, 42715aee0f8aSTheodore Ts'o sizeof(sbi->s_es->s_mount_opts), 42725aee0f8aSTheodore Ts'o GFP_KERNEL); 42735aee0f8aSTheodore Ts'o if (!s_mount_opts) 42745aee0f8aSTheodore Ts'o goto failed_mount; 42755aee0f8aSTheodore Ts'o if (!parse_options(s_mount_opts, sb, &journal_devnum, 42765aee0f8aSTheodore Ts'o &journal_ioprio, 0)) { 42778b67f04aSTheodore Ts'o ext4_msg(sb, KERN_WARNING, 42788b67f04aSTheodore Ts'o "failed to parse options in superblock: %s", 42795aee0f8aSTheodore Ts'o s_mount_opts); 42805aee0f8aSTheodore Ts'o } 42815aee0f8aSTheodore Ts'o kfree(s_mount_opts); 42828b67f04aSTheodore Ts'o } 42835a916be1STheodore Ts'o sbi->s_def_mount_opt = sbi->s_mount_opt; 4284b3881f74STheodore Ts'o if (!parse_options((char *) data, sb, &journal_devnum, 4285661aa520SEric Sandeen &journal_ioprio, 0)) 4286ac27a0ecSDave Kleikamp goto failed_mount; 4287ac27a0ecSDave Kleikamp 4288c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE 4289c83ad55eSGabriel Krisman Bertazi if (ext4_has_feature_casefold(sb) && !sbi->s_encoding) { 4290c83ad55eSGabriel Krisman Bertazi const struct ext4_sb_encodings *encoding_info; 4291c83ad55eSGabriel Krisman Bertazi struct unicode_map *encoding; 4292c83ad55eSGabriel Krisman Bertazi __u16 encoding_flags; 4293c83ad55eSGabriel Krisman Bertazi 4294c83ad55eSGabriel Krisman Bertazi if (ext4_has_feature_encrypt(sb)) { 4295c83ad55eSGabriel Krisman Bertazi ext4_msg(sb, KERN_ERR, 4296c83ad55eSGabriel Krisman Bertazi "Can't mount with encoding and encryption"); 4297c83ad55eSGabriel Krisman Bertazi goto failed_mount; 4298c83ad55eSGabriel Krisman Bertazi } 4299c83ad55eSGabriel Krisman Bertazi 4300c83ad55eSGabriel Krisman Bertazi if (ext4_sb_read_encoding(es, &encoding_info, 4301c83ad55eSGabriel Krisman Bertazi &encoding_flags)) { 4302c83ad55eSGabriel Krisman Bertazi ext4_msg(sb, KERN_ERR, 4303c83ad55eSGabriel Krisman Bertazi "Encoding requested by superblock is unknown"); 4304c83ad55eSGabriel Krisman Bertazi goto failed_mount; 4305c83ad55eSGabriel Krisman Bertazi } 4306c83ad55eSGabriel Krisman Bertazi 4307c83ad55eSGabriel Krisman Bertazi encoding = utf8_load(encoding_info->version); 4308c83ad55eSGabriel Krisman Bertazi if (IS_ERR(encoding)) { 4309c83ad55eSGabriel Krisman Bertazi ext4_msg(sb, KERN_ERR, 4310c83ad55eSGabriel Krisman Bertazi "can't mount with superblock charset: %s-%s " 4311c83ad55eSGabriel Krisman Bertazi "not supported by the kernel. flags: 0x%x.", 4312c83ad55eSGabriel Krisman Bertazi encoding_info->name, encoding_info->version, 4313c83ad55eSGabriel Krisman Bertazi encoding_flags); 4314c83ad55eSGabriel Krisman Bertazi goto failed_mount; 4315c83ad55eSGabriel Krisman Bertazi } 4316c83ad55eSGabriel Krisman Bertazi ext4_msg(sb, KERN_INFO,"Using encoding defined by superblock: " 4317c83ad55eSGabriel Krisman Bertazi "%s-%s with flags 0x%hx", encoding_info->name, 4318c83ad55eSGabriel Krisman Bertazi encoding_info->version?:"\b", encoding_flags); 4319c83ad55eSGabriel Krisman Bertazi 4320c83ad55eSGabriel Krisman Bertazi sbi->s_encoding = encoding; 4321c83ad55eSGabriel Krisman Bertazi sbi->s_encoding_flags = encoding_flags; 4322c83ad55eSGabriel Krisman Bertazi } 4323c83ad55eSGabriel Krisman Bertazi #endif 4324c83ad55eSGabriel Krisman Bertazi 432556889787STheodore Ts'o if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) { 4326244adf64STheodore Ts'o printk_once(KERN_WARNING "EXT4-fs: Warning: mounting with data=journal disables delayed allocation, dioread_nolock, and O_DIRECT support!\n"); 4327781c036bSKaixu Xia /* can't mount with both data=journal and dioread_nolock. */ 4328244adf64STheodore Ts'o clear_opt(sb, DIOREAD_NOLOCK); 432956889787STheodore Ts'o if (test_opt2(sb, EXPLICIT_DELALLOC)) { 433056889787STheodore Ts'o ext4_msg(sb, KERN_ERR, "can't mount with " 433156889787STheodore Ts'o "both data=journal and delalloc"); 433256889787STheodore Ts'o goto failed_mount; 433356889787STheodore Ts'o } 4334fc626fe3SIra Weiny if (test_opt(sb, DAX_ALWAYS)) { 4335923ae0ffSRoss Zwisler ext4_msg(sb, KERN_ERR, "can't mount with " 4336923ae0ffSRoss Zwisler "both data=journal and dax"); 4337923ae0ffSRoss Zwisler goto failed_mount; 4338923ae0ffSRoss Zwisler } 433973b92a2aSSergey Karamov if (ext4_has_feature_encrypt(sb)) { 434073b92a2aSSergey Karamov ext4_msg(sb, KERN_WARNING, 434173b92a2aSSergey Karamov "encrypted files will use data=ordered " 434273b92a2aSSergey Karamov "instead of data journaling mode"); 434373b92a2aSSergey Karamov } 434456889787STheodore Ts'o if (test_opt(sb, DELALLOC)) 434556889787STheodore Ts'o clear_opt(sb, DELALLOC); 4346001e4a87STejun Heo } else { 4347001e4a87STejun Heo sb->s_iflags |= SB_I_CGROUPWB; 434856889787STheodore Ts'o } 434956889787STheodore Ts'o 43501751e8a6SLinus Torvalds sb->s_flags = (sb->s_flags & ~SB_POSIXACL) | 43511751e8a6SLinus Torvalds (test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0); 4352ac27a0ecSDave Kleikamp 4353617ba13bSMingming Cao if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV && 4354e2b911c5SDarrick J. Wong (ext4_has_compat_features(sb) || 4355e2b911c5SDarrick J. Wong ext4_has_ro_compat_features(sb) || 4356e2b911c5SDarrick J. Wong ext4_has_incompat_features(sb))) 4357b31e1552SEric Sandeen ext4_msg(sb, KERN_WARNING, 4358b31e1552SEric Sandeen "feature flags set on rev 0 fs, " 4359b31e1552SEric Sandeen "running e2fsck is recommended"); 4360469108ffSTheodore Tso 4361ed3654ebSTheodore Ts'o if (es->s_creator_os == cpu_to_le32(EXT4_OS_HURD)) { 4362ed3654ebSTheodore Ts'o set_opt2(sb, HURD_COMPAT); 4363e2b911c5SDarrick J. Wong if (ext4_has_feature_64bit(sb)) { 4364ed3654ebSTheodore Ts'o ext4_msg(sb, KERN_ERR, 4365ed3654ebSTheodore Ts'o "The Hurd can't support 64-bit file systems"); 4366ed3654ebSTheodore Ts'o goto failed_mount; 4367ed3654ebSTheodore Ts'o } 4368dec214d0STahsin Erdogan 4369dec214d0STahsin Erdogan /* 4370dec214d0STahsin Erdogan * ea_inode feature uses l_i_version field which is not 4371dec214d0STahsin Erdogan * available in HURD_COMPAT mode. 4372dec214d0STahsin Erdogan */ 4373dec214d0STahsin Erdogan if (ext4_has_feature_ea_inode(sb)) { 4374dec214d0STahsin Erdogan ext4_msg(sb, KERN_ERR, 4375dec214d0STahsin Erdogan "ea_inode feature is not supported for Hurd"); 4376dec214d0STahsin Erdogan goto failed_mount; 4377dec214d0STahsin Erdogan } 4378ed3654ebSTheodore Ts'o } 4379ed3654ebSTheodore Ts'o 43802035e776STheodore Ts'o if (IS_EXT2_SB(sb)) { 43812035e776STheodore Ts'o if (ext2_feature_set_ok(sb)) 43822035e776STheodore Ts'o ext4_msg(sb, KERN_INFO, "mounting ext2 file system " 43832035e776STheodore Ts'o "using the ext4 subsystem"); 43842035e776STheodore Ts'o else { 43850d9366d6SEric Sandeen /* 43860d9366d6SEric Sandeen * If we're probing be silent, if this looks like 43870d9366d6SEric Sandeen * it's actually an ext[34] filesystem. 43880d9366d6SEric Sandeen */ 43890d9366d6SEric Sandeen if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb))) 43900d9366d6SEric Sandeen goto failed_mount; 43912035e776STheodore Ts'o ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due " 43922035e776STheodore Ts'o "to feature incompatibilities"); 43932035e776STheodore Ts'o goto failed_mount; 43942035e776STheodore Ts'o } 43952035e776STheodore Ts'o } 43962035e776STheodore Ts'o 43972035e776STheodore Ts'o if (IS_EXT3_SB(sb)) { 43982035e776STheodore Ts'o if (ext3_feature_set_ok(sb)) 43992035e776STheodore Ts'o ext4_msg(sb, KERN_INFO, "mounting ext3 file system " 44002035e776STheodore Ts'o "using the ext4 subsystem"); 44012035e776STheodore Ts'o else { 44020d9366d6SEric Sandeen /* 44030d9366d6SEric Sandeen * If we're probing be silent, if this looks like 44040d9366d6SEric Sandeen * it's actually an ext4 filesystem. 44050d9366d6SEric Sandeen */ 44060d9366d6SEric Sandeen if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb))) 44070d9366d6SEric Sandeen goto failed_mount; 44082035e776STheodore Ts'o ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due " 44092035e776STheodore Ts'o "to feature incompatibilities"); 44102035e776STheodore Ts'o goto failed_mount; 44112035e776STheodore Ts'o } 44122035e776STheodore Ts'o } 44132035e776STheodore Ts'o 4414469108ffSTheodore Tso /* 4415ac27a0ecSDave Kleikamp * Check feature flags regardless of the revision level, since we 4416ac27a0ecSDave Kleikamp * previously didn't change the revision level when setting the flags, 4417ac27a0ecSDave Kleikamp * so there is a chance incompat flags are set on a rev 0 filesystem. 4418ac27a0ecSDave Kleikamp */ 4419bc98a42cSDavid Howells if (!ext4_feature_set_ok(sb, (sb_rdonly(sb)))) 4420ac27a0ecSDave Kleikamp goto failed_mount; 4421a13fb1a4SEric Sandeen 44228cdf3372STheodore Ts'o if (le32_to_cpu(es->s_log_block_size) > 44238cdf3372STheodore Ts'o (EXT4_MAX_BLOCK_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) { 44248cdf3372STheodore Ts'o ext4_msg(sb, KERN_ERR, 44258cdf3372STheodore Ts'o "Invalid log block size: %u", 44268cdf3372STheodore Ts'o le32_to_cpu(es->s_log_block_size)); 4427ac27a0ecSDave Kleikamp goto failed_mount; 4428ac27a0ecSDave Kleikamp } 4429bfe0a5f4STheodore Ts'o if (le32_to_cpu(es->s_log_cluster_size) > 4430bfe0a5f4STheodore Ts'o (EXT4_MAX_CLUSTER_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) { 4431bfe0a5f4STheodore Ts'o ext4_msg(sb, KERN_ERR, 4432bfe0a5f4STheodore Ts'o "Invalid log cluster size: %u", 4433bfe0a5f4STheodore Ts'o le32_to_cpu(es->s_log_cluster_size)); 4434bfe0a5f4STheodore Ts'o goto failed_mount; 4435bfe0a5f4STheodore Ts'o } 4436ac27a0ecSDave Kleikamp 44375b9554dcSTheodore Ts'o if (le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) > (blocksize / 4)) { 44385b9554dcSTheodore Ts'o ext4_msg(sb, KERN_ERR, 44395b9554dcSTheodore Ts'o "Number of reserved GDT blocks insanely large: %d", 44405b9554dcSTheodore Ts'o le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks)); 44415b9554dcSTheodore Ts'o goto failed_mount; 44425b9554dcSTheodore Ts'o } 44435b9554dcSTheodore Ts'o 4444a8ab6d38SIra Weiny if (bdev_dax_supported(sb->s_bdev, blocksize)) 4445a8ab6d38SIra Weiny set_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags); 4446a8ab6d38SIra Weiny 4447fc626fe3SIra Weiny if (sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) { 4448559db4c6SRoss Zwisler if (ext4_has_feature_inline_data(sb)) { 4449559db4c6SRoss Zwisler ext4_msg(sb, KERN_ERR, "Cannot use DAX on a filesystem" 4450559db4c6SRoss Zwisler " that may contain inline data"); 4451361d24d4SEric Sandeen goto failed_mount; 4452559db4c6SRoss Zwisler } 4453a8ab6d38SIra Weiny if (!test_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags)) { 445424f3478dSDan Williams ext4_msg(sb, KERN_ERR, 4455361d24d4SEric Sandeen "DAX unsupported by block device."); 4456361d24d4SEric Sandeen goto failed_mount; 445724f3478dSDan Williams } 4458923ae0ffSRoss Zwisler } 4459923ae0ffSRoss Zwisler 4460e2b911c5SDarrick J. Wong if (ext4_has_feature_encrypt(sb) && es->s_encryption_level) { 44616ddb2447STheodore Ts'o ext4_msg(sb, KERN_ERR, "Unsupported encryption level %d", 44626ddb2447STheodore Ts'o es->s_encryption_level); 44636ddb2447STheodore Ts'o goto failed_mount; 44646ddb2447STheodore Ts'o } 44656ddb2447STheodore Ts'o 4466ac27a0ecSDave Kleikamp if (sb->s_blocksize != blocksize) { 4467ce40733cSAneesh Kumar K.V /* Validate the filesystem blocksize */ 4468ce40733cSAneesh Kumar K.V if (!sb_set_blocksize(sb, blocksize)) { 4469b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "bad block size %d", 4470ce40733cSAneesh Kumar K.V blocksize); 4471ac27a0ecSDave Kleikamp goto failed_mount; 4472ac27a0ecSDave Kleikamp } 4473ac27a0ecSDave Kleikamp 4474ac27a0ecSDave Kleikamp brelse(bh); 447570bbb3e0SAndrew Morton logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE; 447670bbb3e0SAndrew Morton offset = do_div(logical_sb_block, blocksize); 44778394a6abSzhangyi (F) bh = ext4_sb_bread_unmovable(sb, logical_sb_block); 44788394a6abSzhangyi (F) if (IS_ERR(bh)) { 4479b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, 4480b31e1552SEric Sandeen "Can't read superblock on 2nd try"); 44818394a6abSzhangyi (F) ret = PTR_ERR(bh); 44828394a6abSzhangyi (F) bh = NULL; 4483ac27a0ecSDave Kleikamp goto failed_mount; 4484ac27a0ecSDave Kleikamp } 44852716b802STheodore Ts'o es = (struct ext4_super_block *)(bh->b_data + offset); 4486ac27a0ecSDave Kleikamp sbi->s_es = es; 4487617ba13bSMingming Cao if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) { 4488b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, 4489b31e1552SEric Sandeen "Magic mismatch, very weird!"); 4490ac27a0ecSDave Kleikamp goto failed_mount; 4491ac27a0ecSDave Kleikamp } 4492ac27a0ecSDave Kleikamp } 4493ac27a0ecSDave Kleikamp 4494e2b911c5SDarrick J. Wong has_huge_files = ext4_has_feature_huge_file(sb); 4495f287a1a5STheodore Ts'o sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits, 4496f287a1a5STheodore Ts'o has_huge_files); 4497f287a1a5STheodore Ts'o sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files); 4498ac27a0ecSDave Kleikamp 44990d1ee42fSAlexandre Ratchov sbi->s_desc_size = le16_to_cpu(es->s_desc_size); 4500e2b911c5SDarrick J. Wong if (ext4_has_feature_64bit(sb)) { 45018fadc143SAlexandre Ratchov if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT || 45020d1ee42fSAlexandre Ratchov sbi->s_desc_size > EXT4_MAX_DESC_SIZE || 4503d8ea6cf8Svignesh babu !is_power_of_2(sbi->s_desc_size)) { 4504b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, 4505b31e1552SEric Sandeen "unsupported descriptor size %lu", 45060d1ee42fSAlexandre Ratchov sbi->s_desc_size); 45070d1ee42fSAlexandre Ratchov goto failed_mount; 45080d1ee42fSAlexandre Ratchov } 45090d1ee42fSAlexandre Ratchov } else 45100d1ee42fSAlexandre Ratchov sbi->s_desc_size = EXT4_MIN_DESC_SIZE; 45110b8e58a1SAndreas Dilger 4512ac27a0ecSDave Kleikamp sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group); 4513ac27a0ecSDave Kleikamp sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group); 45140b8e58a1SAndreas Dilger 4515617ba13bSMingming Cao sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb); 4516ac27a0ecSDave Kleikamp if (sbi->s_inodes_per_block == 0) 4517617ba13bSMingming Cao goto cantfind_ext4; 4518cd6bb35bSTheodore Ts'o if (sbi->s_inodes_per_group < sbi->s_inodes_per_block || 4519cd6bb35bSTheodore Ts'o sbi->s_inodes_per_group > blocksize * 8) { 4520cd6bb35bSTheodore Ts'o ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu\n", 4521b9c538daSJosh Triplett sbi->s_inodes_per_group); 4522cd6bb35bSTheodore Ts'o goto failed_mount; 4523cd6bb35bSTheodore Ts'o } 4524ac27a0ecSDave Kleikamp sbi->s_itb_per_group = sbi->s_inodes_per_group / 4525ac27a0ecSDave Kleikamp sbi->s_inodes_per_block; 45260d1ee42fSAlexandre Ratchov sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb); 4527ac27a0ecSDave Kleikamp sbi->s_sbh = bh; 4528ac27a0ecSDave Kleikamp sbi->s_mount_state = le16_to_cpu(es->s_state); 4529e57aa839SFengguang Wu sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb)); 4530e57aa839SFengguang Wu sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb)); 45310b8e58a1SAndreas Dilger 4532ac27a0ecSDave Kleikamp for (i = 0; i < 4; i++) 4533ac27a0ecSDave Kleikamp sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]); 4534ac27a0ecSDave Kleikamp sbi->s_def_hash_version = es->s_def_hash_version; 4535e2b911c5SDarrick J. Wong if (ext4_has_feature_dir_index(sb)) { 4536f99b2589STheodore Ts'o i = le32_to_cpu(es->s_flags); 4537f99b2589STheodore Ts'o if (i & EXT2_FLAGS_UNSIGNED_HASH) 4538f99b2589STheodore Ts'o sbi->s_hash_unsigned = 3; 4539f99b2589STheodore Ts'o else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) { 4540f99b2589STheodore Ts'o #ifdef __CHAR_UNSIGNED__ 4541bc98a42cSDavid Howells if (!sb_rdonly(sb)) 454223301410STheodore Ts'o es->s_flags |= 454323301410STheodore Ts'o cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH); 4544f99b2589STheodore Ts'o sbi->s_hash_unsigned = 3; 4545f99b2589STheodore Ts'o #else 4546bc98a42cSDavid Howells if (!sb_rdonly(sb)) 454723301410STheodore Ts'o es->s_flags |= 454823301410STheodore Ts'o cpu_to_le32(EXT2_FLAGS_SIGNED_HASH); 4549f99b2589STheodore Ts'o #endif 4550f99b2589STheodore Ts'o } 455123301410STheodore Ts'o } 4552ac27a0ecSDave Kleikamp 4553281b5995STheodore Ts'o /* Handle clustersize */ 4554281b5995STheodore Ts'o clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size); 4555ef5fd681SKaixu Xia if (ext4_has_feature_bigalloc(sb)) { 4556281b5995STheodore Ts'o if (clustersize < blocksize) { 4557281b5995STheodore Ts'o ext4_msg(sb, KERN_ERR, 4558281b5995STheodore Ts'o "cluster size (%d) smaller than " 4559281b5995STheodore Ts'o "block size (%d)", clustersize, blocksize); 4560281b5995STheodore Ts'o goto failed_mount; 4561281b5995STheodore Ts'o } 4562281b5995STheodore Ts'o sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) - 4563281b5995STheodore Ts'o le32_to_cpu(es->s_log_block_size); 4564281b5995STheodore Ts'o sbi->s_clusters_per_group = 4565281b5995STheodore Ts'o le32_to_cpu(es->s_clusters_per_group); 4566281b5995STheodore Ts'o if (sbi->s_clusters_per_group > blocksize * 8) { 4567281b5995STheodore Ts'o ext4_msg(sb, KERN_ERR, 4568281b5995STheodore Ts'o "#clusters per group too big: %lu", 4569281b5995STheodore Ts'o sbi->s_clusters_per_group); 4570281b5995STheodore Ts'o goto failed_mount; 4571281b5995STheodore Ts'o } 4572281b5995STheodore Ts'o if (sbi->s_blocks_per_group != 4573281b5995STheodore Ts'o (sbi->s_clusters_per_group * (clustersize / blocksize))) { 4574281b5995STheodore Ts'o ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and " 4575281b5995STheodore Ts'o "clusters per group (%lu) inconsistent", 4576281b5995STheodore Ts'o sbi->s_blocks_per_group, 4577281b5995STheodore Ts'o sbi->s_clusters_per_group); 4578281b5995STheodore Ts'o goto failed_mount; 4579281b5995STheodore Ts'o } 4580281b5995STheodore Ts'o } else { 4581281b5995STheodore Ts'o if (clustersize != blocksize) { 4582bfe0a5f4STheodore Ts'o ext4_msg(sb, KERN_ERR, 4583bfe0a5f4STheodore Ts'o "fragment/cluster size (%d) != " 4584bfe0a5f4STheodore Ts'o "block size (%d)", clustersize, blocksize); 4585bfe0a5f4STheodore Ts'o goto failed_mount; 4586281b5995STheodore Ts'o } 4587ac27a0ecSDave Kleikamp if (sbi->s_blocks_per_group > blocksize * 8) { 4588b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, 4589b31e1552SEric Sandeen "#blocks per group too big: %lu", 4590ac27a0ecSDave Kleikamp sbi->s_blocks_per_group); 4591ac27a0ecSDave Kleikamp goto failed_mount; 4592ac27a0ecSDave Kleikamp } 4593281b5995STheodore Ts'o sbi->s_clusters_per_group = sbi->s_blocks_per_group; 4594281b5995STheodore Ts'o sbi->s_cluster_bits = 0; 4595281b5995STheodore Ts'o } 4596281b5995STheodore Ts'o sbi->s_cluster_ratio = clustersize / blocksize; 4597281b5995STheodore Ts'o 4598960fd856STheodore Ts'o /* Do we have standard group size of clustersize * 8 blocks ? */ 4599960fd856STheodore Ts'o if (sbi->s_blocks_per_group == clustersize << 3) 4600960fd856STheodore Ts'o set_opt2(sb, STD_GROUP_SIZE); 4601960fd856STheodore Ts'o 4602bf43d84bSEric Sandeen /* 4603bf43d84bSEric Sandeen * Test whether we have more sectors than will fit in sector_t, 4604bf43d84bSEric Sandeen * and whether the max offset is addressable by the page cache. 4605bf43d84bSEric Sandeen */ 46065a9ae68aSDarrick J. Wong err = generic_check_addressable(sb->s_blocksize_bits, 460730ca22c7SPatrick J. LoPresti ext4_blocks_count(es)); 46085a9ae68aSDarrick J. Wong if (err) { 4609b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "filesystem" 4610bf43d84bSEric Sandeen " too large to mount safely on this system"); 4611ac27a0ecSDave Kleikamp goto failed_mount; 4612ac27a0ecSDave Kleikamp } 4613ac27a0ecSDave Kleikamp 4614617ba13bSMingming Cao if (EXT4_BLOCKS_PER_GROUP(sb) == 0) 4615617ba13bSMingming Cao goto cantfind_ext4; 4616e7c95593SEric Sandeen 46170f2ddca6SFrom: Thiemo Nagel /* check blocks count against device size */ 46180f2ddca6SFrom: Thiemo Nagel blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits; 46190f2ddca6SFrom: Thiemo Nagel if (blocks_count && ext4_blocks_count(es) > blocks_count) { 4620b31e1552SEric Sandeen ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu " 4621b31e1552SEric Sandeen "exceeds size of device (%llu blocks)", 46220f2ddca6SFrom: Thiemo Nagel ext4_blocks_count(es), blocks_count); 46230f2ddca6SFrom: Thiemo Nagel goto failed_mount; 46240f2ddca6SFrom: Thiemo Nagel } 46250f2ddca6SFrom: Thiemo Nagel 46264ec11028STheodore Ts'o /* 46274ec11028STheodore Ts'o * It makes no sense for the first data block to be beyond the end 46284ec11028STheodore Ts'o * of the filesystem. 46294ec11028STheodore Ts'o */ 46304ec11028STheodore Ts'o if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) { 4631b31e1552SEric Sandeen ext4_msg(sb, KERN_WARNING, "bad geometry: first data " 4632b31e1552SEric Sandeen "block %u is beyond end of filesystem (%llu)", 4633e7c95593SEric Sandeen le32_to_cpu(es->s_first_data_block), 46344ec11028STheodore Ts'o ext4_blocks_count(es)); 4635e7c95593SEric Sandeen goto failed_mount; 4636e7c95593SEric Sandeen } 4637bfe0a5f4STheodore Ts'o if ((es->s_first_data_block == 0) && (es->s_log_block_size == 0) && 4638bfe0a5f4STheodore Ts'o (sbi->s_cluster_ratio == 1)) { 4639bfe0a5f4STheodore Ts'o ext4_msg(sb, KERN_WARNING, "bad geometry: first data " 4640bfe0a5f4STheodore Ts'o "block is 0 with a 1k block and cluster size"); 4641bfe0a5f4STheodore Ts'o goto failed_mount; 4642bfe0a5f4STheodore Ts'o } 4643bfe0a5f4STheodore Ts'o 4644bd81d8eeSLaurent Vivier blocks_count = (ext4_blocks_count(es) - 4645bd81d8eeSLaurent Vivier le32_to_cpu(es->s_first_data_block) + 4646bd81d8eeSLaurent Vivier EXT4_BLOCKS_PER_GROUP(sb) - 1); 4647bd81d8eeSLaurent Vivier do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb)); 46484ec11028STheodore Ts'o if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) { 4649df41460aSJosh Triplett ext4_msg(sb, KERN_WARNING, "groups count too large: %llu " 46504ec11028STheodore Ts'o "(block count %llu, first data block %u, " 4651df41460aSJosh Triplett "blocks per group %lu)", blocks_count, 46524ec11028STheodore Ts'o ext4_blocks_count(es), 46534ec11028STheodore Ts'o le32_to_cpu(es->s_first_data_block), 46544ec11028STheodore Ts'o EXT4_BLOCKS_PER_GROUP(sb)); 46554ec11028STheodore Ts'o goto failed_mount; 46564ec11028STheodore Ts'o } 4657bd81d8eeSLaurent Vivier sbi->s_groups_count = blocks_count; 4658fb0a387dSEric Sandeen sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count, 4659fb0a387dSEric Sandeen (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb))); 46609e463084STheodore Ts'o if (((u64)sbi->s_groups_count * sbi->s_inodes_per_group) != 46619e463084STheodore Ts'o le32_to_cpu(es->s_inodes_count)) { 46629e463084STheodore Ts'o ext4_msg(sb, KERN_ERR, "inodes count not valid: %u vs %llu", 46639e463084STheodore Ts'o le32_to_cpu(es->s_inodes_count), 46649e463084STheodore Ts'o ((u64)sbi->s_groups_count * sbi->s_inodes_per_group)); 46659e463084STheodore Ts'o ret = -EINVAL; 46669e463084STheodore Ts'o goto failed_mount; 46679e463084STheodore Ts'o } 4668617ba13bSMingming Cao db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) / 4669617ba13bSMingming Cao EXT4_DESC_PER_BLOCK(sb); 46703a4b77cdSEryu Guan if (ext4_has_feature_meta_bg(sb)) { 46712ba3e6e8STheodore Ts'o if (le32_to_cpu(es->s_first_meta_bg) > db_count) { 46723a4b77cdSEryu Guan ext4_msg(sb, KERN_WARNING, 46733a4b77cdSEryu Guan "first meta block group too large: %u " 46743a4b77cdSEryu Guan "(group descriptor block count %u)", 46753a4b77cdSEryu Guan le32_to_cpu(es->s_first_meta_bg), db_count); 46763a4b77cdSEryu Guan goto failed_mount; 46773a4b77cdSEryu Guan } 46783a4b77cdSEryu Guan } 46791d0c3924STheodore Ts'o rcu_assign_pointer(sbi->s_group_desc, 46801d0c3924STheodore Ts'o kvmalloc_array(db_count, 4681f18a5f21STheodore Ts'o sizeof(struct buffer_head *), 46821d0c3924STheodore Ts'o GFP_KERNEL)); 4683ac27a0ecSDave Kleikamp if (sbi->s_group_desc == NULL) { 4684b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "not enough memory"); 46852cde417dSTheodore Ts'o ret = -ENOMEM; 4686ac27a0ecSDave Kleikamp goto failed_mount; 4687ac27a0ecSDave Kleikamp } 4688ac27a0ecSDave Kleikamp 4689705895b6SPekka Enberg bgl_lock_init(sbi->s_blockgroup_lock); 4690ac27a0ecSDave Kleikamp 469185c8f176SAndrew Perepechko /* Pre-read the descriptors into the buffer cache */ 469285c8f176SAndrew Perepechko for (i = 0; i < db_count; i++) { 469385c8f176SAndrew Perepechko block = descriptor_loc(sb, logical_sb_block, i); 46945df1d412Szhangyi (F) ext4_sb_breadahead_unmovable(sb, block); 469585c8f176SAndrew Perepechko } 469685c8f176SAndrew Perepechko 4697ac27a0ecSDave Kleikamp for (i = 0; i < db_count; i++) { 46981d0c3924STheodore Ts'o struct buffer_head *bh; 46991d0c3924STheodore Ts'o 470070bbb3e0SAndrew Morton block = descriptor_loc(sb, logical_sb_block, i); 47018394a6abSzhangyi (F) bh = ext4_sb_bread_unmovable(sb, block); 47028394a6abSzhangyi (F) if (IS_ERR(bh)) { 4703b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, 4704b31e1552SEric Sandeen "can't read group descriptor %d", i); 4705ac27a0ecSDave Kleikamp db_count = i; 47068394a6abSzhangyi (F) ret = PTR_ERR(bh); 47078394a6abSzhangyi (F) bh = NULL; 4708ac27a0ecSDave Kleikamp goto failed_mount2; 4709ac27a0ecSDave Kleikamp } 47101d0c3924STheodore Ts'o rcu_read_lock(); 47111d0c3924STheodore Ts'o rcu_dereference(sbi->s_group_desc)[i] = bh; 47121d0c3924STheodore Ts'o rcu_read_unlock(); 4713ac27a0ecSDave Kleikamp } 471444de022cSTheodore Ts'o sbi->s_gdb_count = db_count; 4715829fa70dSTheodore Ts'o if (!ext4_check_descriptors(sb, logical_sb_block, &first_not_zeroed)) { 4716b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "group descriptors corrupted!"); 47176a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 4718f9ae9cf5STheodore Ts'o goto failed_mount2; 4719ac27a0ecSDave Kleikamp } 4720772cb7c8SJose R. Santos 4721235699a8SKees Cook timer_setup(&sbi->s_err_report, print_daily_error_info, 0); 472204496411STao Ma 4723a75ae78fSDmitry Monakhov /* Register extent status tree shrinker */ 4724eb68d0e2SZheng Liu if (ext4_es_register_shrinker(sbi)) 4725ce7e010aSTheodore Ts'o goto failed_mount3; 4726ce7e010aSTheodore Ts'o 4727c9de560dSAlex Tomas sbi->s_stripe = ext4_get_stripe_size(sbi); 472867a5da56SZheng Liu sbi->s_extent_max_zeroout_kb = 32; 4729c9de560dSAlex Tomas 4730f9ae9cf5STheodore Ts'o /* 4731f9ae9cf5STheodore Ts'o * set up enough so that it can read an inode 4732f9ae9cf5STheodore Ts'o */ 4733f9ae9cf5STheodore Ts'o sb->s_op = &ext4_sops; 4734617ba13bSMingming Cao sb->s_export_op = &ext4_export_ops; 4735617ba13bSMingming Cao sb->s_xattr = ext4_xattr_handlers; 4736643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 4737a7550b30SJaegeuk Kim sb->s_cop = &ext4_cryptops; 4738ffcc4182SEric Biggers #endif 4739c93d8f88SEric Biggers #ifdef CONFIG_FS_VERITY 4740c93d8f88SEric Biggers sb->s_vop = &ext4_verityops; 4741c93d8f88SEric Biggers #endif 4742ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 4743617ba13bSMingming Cao sb->dq_op = &ext4_quota_operations; 4744e2b911c5SDarrick J. Wong if (ext4_has_feature_quota(sb)) 47451fa5efe3SJan Kara sb->s_qcop = &dquot_quotactl_sysfile_ops; 4746262b4662SJan Kara else 4747262b4662SJan Kara sb->s_qcop = &ext4_qctl_operations; 4748689c958cSLi Xi sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ; 4749ac27a0ecSDave Kleikamp #endif 475085787090SChristoph Hellwig memcpy(&sb->s_uuid, es->s_uuid, sizeof(es->s_uuid)); 4751f2fa2ffcSAneesh Kumar K.V 4752ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */ 47533b9d4ed2STheodore Ts'o mutex_init(&sbi->s_orphan_lock); 4754ac27a0ecSDave Kleikamp 4755*aa75f4d3SHarshad Shirwadkar /* Initialize fast commit stuff */ 4756*aa75f4d3SHarshad Shirwadkar atomic_set(&sbi->s_fc_subtid, 0); 4757*aa75f4d3SHarshad Shirwadkar atomic_set(&sbi->s_fc_ineligible_updates, 0); 4758*aa75f4d3SHarshad Shirwadkar INIT_LIST_HEAD(&sbi->s_fc_q[FC_Q_MAIN]); 4759*aa75f4d3SHarshad Shirwadkar INIT_LIST_HEAD(&sbi->s_fc_q[FC_Q_STAGING]); 4760*aa75f4d3SHarshad Shirwadkar INIT_LIST_HEAD(&sbi->s_fc_dentry_q[FC_Q_MAIN]); 4761*aa75f4d3SHarshad Shirwadkar INIT_LIST_HEAD(&sbi->s_fc_dentry_q[FC_Q_STAGING]); 4762*aa75f4d3SHarshad Shirwadkar sbi->s_fc_bytes = 0; 4763*aa75f4d3SHarshad Shirwadkar sbi->s_mount_state &= ~EXT4_FC_INELIGIBLE; 4764*aa75f4d3SHarshad Shirwadkar sbi->s_mount_state &= ~EXT4_FC_COMMITTING; 4765*aa75f4d3SHarshad Shirwadkar spin_lock_init(&sbi->s_fc_lock); 4766*aa75f4d3SHarshad Shirwadkar memset(&sbi->s_fc_stats, 0, sizeof(sbi->s_fc_stats)); 4767*aa75f4d3SHarshad Shirwadkar 4768ac27a0ecSDave Kleikamp sb->s_root = NULL; 4769ac27a0ecSDave Kleikamp 4770ac27a0ecSDave Kleikamp needs_recovery = (es->s_last_orphan != 0 || 4771e2b911c5SDarrick J. Wong ext4_has_feature_journal_needs_recovery(sb)); 4772ac27a0ecSDave Kleikamp 4773bc98a42cSDavid Howells if (ext4_has_feature_mmp(sb) && !sb_rdonly(sb)) 4774c5e06d10SJohann Lombardi if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block))) 477550460fe8SDarrick J. Wong goto failed_mount3a; 4776c5e06d10SJohann Lombardi 4777ac27a0ecSDave Kleikamp /* 4778ac27a0ecSDave Kleikamp * The first inode we look at is the journal inode. Don't try 4779ac27a0ecSDave Kleikamp * root first: it may be modified in the journal! 4780ac27a0ecSDave Kleikamp */ 4781e2b911c5SDarrick J. Wong if (!test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb)) { 47824753d8a2STheodore Ts'o err = ext4_load_journal(sb, es, journal_devnum); 47834753d8a2STheodore Ts'o if (err) 478450460fe8SDarrick J. Wong goto failed_mount3a; 4785bc98a42cSDavid Howells } else if (test_opt(sb, NOLOAD) && !sb_rdonly(sb) && 4786e2b911c5SDarrick J. Wong ext4_has_feature_journal_needs_recovery(sb)) { 4787b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "required journal recovery " 4788b31e1552SEric Sandeen "suppressed and not mounted read-only"); 4789744692dcSJiaying Zhang goto failed_mount_wq; 4790ac27a0ecSDave Kleikamp } else { 47911e381f60SDmitry Monakhov /* Nojournal mode, all journal mount options are illegal */ 47921e381f60SDmitry Monakhov if (test_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM)) { 47931e381f60SDmitry Monakhov ext4_msg(sb, KERN_ERR, "can't mount with " 47941e381f60SDmitry Monakhov "journal_checksum, fs mounted w/o journal"); 47951e381f60SDmitry Monakhov goto failed_mount_wq; 47961e381f60SDmitry Monakhov } 47971e381f60SDmitry Monakhov if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) { 47981e381f60SDmitry Monakhov ext4_msg(sb, KERN_ERR, "can't mount with " 47991e381f60SDmitry Monakhov "journal_async_commit, fs mounted w/o journal"); 48001e381f60SDmitry Monakhov goto failed_mount_wq; 48011e381f60SDmitry Monakhov } 48021e381f60SDmitry Monakhov if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) { 48031e381f60SDmitry Monakhov ext4_msg(sb, KERN_ERR, "can't mount with " 48041e381f60SDmitry Monakhov "commit=%lu, fs mounted w/o journal", 48051e381f60SDmitry Monakhov sbi->s_commit_interval / HZ); 48061e381f60SDmitry Monakhov goto failed_mount_wq; 48071e381f60SDmitry Monakhov } 48081e381f60SDmitry Monakhov if (EXT4_MOUNT_DATA_FLAGS & 48091e381f60SDmitry Monakhov (sbi->s_mount_opt ^ sbi->s_def_mount_opt)) { 48101e381f60SDmitry Monakhov ext4_msg(sb, KERN_ERR, "can't mount with " 48111e381f60SDmitry Monakhov "data=, fs mounted w/o journal"); 48121e381f60SDmitry Monakhov goto failed_mount_wq; 48131e381f60SDmitry Monakhov } 481450b29d8fSDebabrata Banerjee sbi->s_def_mount_opt &= ~EXT4_MOUNT_JOURNAL_CHECKSUM; 48151e381f60SDmitry Monakhov clear_opt(sb, JOURNAL_CHECKSUM); 4816fd8c37ecSTheodore Ts'o clear_opt(sb, DATA_FLAGS); 4817995a3ed6SHarshad Shirwadkar clear_opt2(sb, JOURNAL_FAST_COMMIT); 48180390131bSFrank Mayhar sbi->s_journal = NULL; 48190390131bSFrank Mayhar needs_recovery = 0; 48200390131bSFrank Mayhar goto no_journal; 4821ac27a0ecSDave Kleikamp } 4822ac27a0ecSDave Kleikamp 4823e2b911c5SDarrick J. Wong if (ext4_has_feature_64bit(sb) && 4824eb40a09cSJose R. Santos !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0, 4825eb40a09cSJose R. Santos JBD2_FEATURE_INCOMPAT_64BIT)) { 4826b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature"); 4827744692dcSJiaying Zhang goto failed_mount_wq; 4828eb40a09cSJose R. Santos } 4829eb40a09cSJose R. Santos 483025ed6e8aSDarrick J. Wong if (!set_journal_csum_feature_set(sb)) { 483125ed6e8aSDarrick J. Wong ext4_msg(sb, KERN_ERR, "Failed to set journal checksum " 483225ed6e8aSDarrick J. Wong "feature set"); 483325ed6e8aSDarrick J. Wong goto failed_mount_wq; 4834d4da6c9cSLinus Torvalds } 4835818d276cSGirish Shilamkar 4836ac27a0ecSDave Kleikamp /* We have now updated the journal if required, so we can 4837ac27a0ecSDave Kleikamp * validate the data journaling mode. */ 4838ac27a0ecSDave Kleikamp switch (test_opt(sb, DATA_FLAGS)) { 4839ac27a0ecSDave Kleikamp case 0: 4840ac27a0ecSDave Kleikamp /* No mode set, assume a default based on the journal 484163f57933SAndrew Morton * capabilities: ORDERED_DATA if the journal can 484263f57933SAndrew Morton * cope, else JOURNAL_DATA 484363f57933SAndrew Morton */ 4844dab291afSMingming Cao if (jbd2_journal_check_available_features 484527f394a7STyson Nottingham (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) { 4846fd8c37ecSTheodore Ts'o set_opt(sb, ORDERED_DATA); 484727f394a7STyson Nottingham sbi->s_def_mount_opt |= EXT4_MOUNT_ORDERED_DATA; 484827f394a7STyson Nottingham } else { 4849fd8c37ecSTheodore Ts'o set_opt(sb, JOURNAL_DATA); 485027f394a7STyson Nottingham sbi->s_def_mount_opt |= EXT4_MOUNT_JOURNAL_DATA; 485127f394a7STyson Nottingham } 4852ac27a0ecSDave Kleikamp break; 4853ac27a0ecSDave Kleikamp 4854617ba13bSMingming Cao case EXT4_MOUNT_ORDERED_DATA: 4855617ba13bSMingming Cao case EXT4_MOUNT_WRITEBACK_DATA: 4856dab291afSMingming Cao if (!jbd2_journal_check_available_features 4857dab291afSMingming Cao (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) { 4858b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "Journal does not support " 4859b31e1552SEric Sandeen "requested data journaling mode"); 4860744692dcSJiaying Zhang goto failed_mount_wq; 4861ac27a0ecSDave Kleikamp } 4862ac27a0ecSDave Kleikamp default: 4863ac27a0ecSDave Kleikamp break; 4864ac27a0ecSDave Kleikamp } 4865ab04df78SJan Kara 4866ab04df78SJan Kara if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA && 4867ab04df78SJan Kara test_opt(sb, JOURNAL_ASYNC_COMMIT)) { 4868ab04df78SJan Kara ext4_msg(sb, KERN_ERR, "can't mount with " 4869ab04df78SJan Kara "journal_async_commit in data=ordered mode"); 4870ab04df78SJan Kara goto failed_mount_wq; 4871ab04df78SJan Kara } 4872ab04df78SJan Kara 4873b3881f74STheodore Ts'o set_task_ioprio(sbi->s_journal->j_task, journal_ioprio); 4874ac27a0ecSDave Kleikamp 487518aadd47SBobi Jam sbi->s_journal->j_commit_callback = ext4_journal_commit_callback; 4876342af94eSMauricio Faria de Oliveira sbi->s_journal->j_submit_inode_data_buffers = 4877afb585a9SMauricio Faria de Oliveira ext4_journal_submit_inode_data_buffers; 4878342af94eSMauricio Faria de Oliveira sbi->s_journal->j_finish_inode_data_buffers = 4879afb585a9SMauricio Faria de Oliveira ext4_journal_finish_inode_data_buffers; 488018aadd47SBobi Jam 4881ce7e010aSTheodore Ts'o no_journal: 4882cdb7ee4cSTahsin Erdogan if (!test_opt(sb, NO_MBCACHE)) { 488347387409STahsin Erdogan sbi->s_ea_block_cache = ext4_xattr_create_cache(); 488447387409STahsin Erdogan if (!sbi->s_ea_block_cache) { 4885cdb7ee4cSTahsin Erdogan ext4_msg(sb, KERN_ERR, 4886cdb7ee4cSTahsin Erdogan "Failed to create ea_block_cache"); 48879c191f70ST Makphaibulchoke goto failed_mount_wq; 48889c191f70ST Makphaibulchoke } 48899c191f70ST Makphaibulchoke 4890dec214d0STahsin Erdogan if (ext4_has_feature_ea_inode(sb)) { 4891dec214d0STahsin Erdogan sbi->s_ea_inode_cache = ext4_xattr_create_cache(); 4892dec214d0STahsin Erdogan if (!sbi->s_ea_inode_cache) { 4893dec214d0STahsin Erdogan ext4_msg(sb, KERN_ERR, 4894dec214d0STahsin Erdogan "Failed to create ea_inode_cache"); 4895dec214d0STahsin Erdogan goto failed_mount_wq; 4896dec214d0STahsin Erdogan } 4897dec214d0STahsin Erdogan } 4898cdb7ee4cSTahsin Erdogan } 4899dec214d0STahsin Erdogan 4900c93d8f88SEric Biggers if (ext4_has_feature_verity(sb) && blocksize != PAGE_SIZE) { 4901c93d8f88SEric Biggers ext4_msg(sb, KERN_ERR, "Unsupported blocksize for fs-verity"); 4902c93d8f88SEric Biggers goto failed_mount_wq; 4903c93d8f88SEric Biggers } 4904c93d8f88SEric Biggers 4905bc98a42cSDavid Howells if (DUMMY_ENCRYPTION_ENABLED(sbi) && !sb_rdonly(sb) && 4906e2b911c5SDarrick J. Wong !ext4_has_feature_encrypt(sb)) { 4907e2b911c5SDarrick J. Wong ext4_set_feature_encrypt(sb); 49086ddb2447STheodore Ts'o ext4_commit_super(sb, 1); 49096ddb2447STheodore Ts'o } 49106ddb2447STheodore Ts'o 4911fd89d5f2STejun Heo /* 4912952fc18eSTheodore Ts'o * Get the # of file system overhead blocks from the 4913952fc18eSTheodore Ts'o * superblock if present. 4914952fc18eSTheodore Ts'o */ 4915952fc18eSTheodore Ts'o if (es->s_overhead_clusters) 4916952fc18eSTheodore Ts'o sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters); 4917952fc18eSTheodore Ts'o else { 491807aa2ea1SLukas Czerner err = ext4_calculate_overhead(sb); 491907aa2ea1SLukas Czerner if (err) 4920952fc18eSTheodore Ts'o goto failed_mount_wq; 4921952fc18eSTheodore Ts'o } 4922952fc18eSTheodore Ts'o 4923952fc18eSTheodore Ts'o /* 4924fd89d5f2STejun Heo * The maximum number of concurrent works can be high and 4925fd89d5f2STejun Heo * concurrency isn't really necessary. Limit it to 1. 4926fd89d5f2STejun Heo */ 49272e8fa54eSJan Kara EXT4_SB(sb)->rsv_conversion_wq = 49282e8fa54eSJan Kara alloc_workqueue("ext4-rsv-conversion", WQ_MEM_RECLAIM | WQ_UNBOUND, 1); 49292e8fa54eSJan Kara if (!EXT4_SB(sb)->rsv_conversion_wq) { 49302e8fa54eSJan Kara printk(KERN_ERR "EXT4-fs: failed to create workqueue\n"); 493107aa2ea1SLukas Czerner ret = -ENOMEM; 49322e8fa54eSJan Kara goto failed_mount4; 49332e8fa54eSJan Kara } 49342e8fa54eSJan Kara 4935ac27a0ecSDave Kleikamp /* 4936dab291afSMingming Cao * The jbd2_journal_load will have done any necessary log recovery, 4937ac27a0ecSDave Kleikamp * so we can safely mount the rest of the filesystem now. 4938ac27a0ecSDave Kleikamp */ 4939ac27a0ecSDave Kleikamp 49408a363970STheodore Ts'o root = ext4_iget(sb, EXT4_ROOT_INO, EXT4_IGET_SPECIAL); 49411d1fe1eeSDavid Howells if (IS_ERR(root)) { 4942b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "get root inode failed"); 49431d1fe1eeSDavid Howells ret = PTR_ERR(root); 494432a9bb57SManish Katiyar root = NULL; 4945ac27a0ecSDave Kleikamp goto failed_mount4; 4946ac27a0ecSDave Kleikamp } 4947ac27a0ecSDave Kleikamp if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) { 4948b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck"); 494994bf608aSAl Viro iput(root); 4950ac27a0ecSDave Kleikamp goto failed_mount4; 4951ac27a0ecSDave Kleikamp } 4952b886ee3eSGabriel Krisman Bertazi 4953b886ee3eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE 4954b886ee3eSGabriel Krisman Bertazi if (sbi->s_encoding) 4955b886ee3eSGabriel Krisman Bertazi sb->s_d_op = &ext4_dentry_ops; 4956b886ee3eSGabriel Krisman Bertazi #endif 4957b886ee3eSGabriel Krisman Bertazi 495848fde701SAl Viro sb->s_root = d_make_root(root); 49591d1fe1eeSDavid Howells if (!sb->s_root) { 4960b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "get root dentry failed"); 49611d1fe1eeSDavid Howells ret = -ENOMEM; 49621d1fe1eeSDavid Howells goto failed_mount4; 49631d1fe1eeSDavid Howells } 4964ac27a0ecSDave Kleikamp 4965c89128a0SJaegeuk Kim ret = ext4_setup_super(sb, es, sb_rdonly(sb)); 4966c89128a0SJaegeuk Kim if (ret == -EROFS) { 49671751e8a6SLinus Torvalds sb->s_flags |= SB_RDONLY; 4968c89128a0SJaegeuk Kim ret = 0; 4969c89128a0SJaegeuk Kim } else if (ret) 4970c89128a0SJaegeuk Kim goto failed_mount4a; 4971ef7f3835SKalpak Shah 4972b5799018STheodore Ts'o ext4_set_resv_clusters(sb); 497327dd4385SLukas Czerner 49740f5bde1dSJan Kara if (test_opt(sb, BLOCK_VALIDITY)) { 49756fd058f7STheodore Ts'o err = ext4_setup_system_zone(sb); 49766fd058f7STheodore Ts'o if (err) { 4977b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "failed to initialize system " 4978fbe845ddSCurt Wohlgemuth "zone (%d)", err); 4979f9ae9cf5STheodore Ts'o goto failed_mount4a; 4980f9ae9cf5STheodore Ts'o } 49810f5bde1dSJan Kara } 4982f9ae9cf5STheodore Ts'o 4983f9ae9cf5STheodore Ts'o ext4_ext_init(sb); 4984f9ae9cf5STheodore Ts'o err = ext4_mb_init(sb); 4985f9ae9cf5STheodore Ts'o if (err) { 4986f9ae9cf5STheodore Ts'o ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)", 4987f9ae9cf5STheodore Ts'o err); 4988dcf2d804STao Ma goto failed_mount5; 4989c2774d84SAneesh Kumar K.V } 4990c2774d84SAneesh Kumar K.V 4991d5e03cbbSTheodore Ts'o block = ext4_count_free_clusters(sb); 4992d5e03cbbSTheodore Ts'o ext4_free_blocks_count_set(sbi->s_es, 4993d5e03cbbSTheodore Ts'o EXT4_C2B(sbi, block)); 49944274f516STheodore Ts'o ext4_superblock_csum_set(sb); 4995908c7f19STejun Heo err = percpu_counter_init(&sbi->s_freeclusters_counter, block, 4996908c7f19STejun Heo GFP_KERNEL); 4997d5e03cbbSTheodore Ts'o if (!err) { 4998d5e03cbbSTheodore Ts'o unsigned long freei = ext4_count_free_inodes(sb); 4999d5e03cbbSTheodore Ts'o sbi->s_es->s_free_inodes_count = cpu_to_le32(freei); 50004274f516STheodore Ts'o ext4_superblock_csum_set(sb); 5001908c7f19STejun Heo err = percpu_counter_init(&sbi->s_freeinodes_counter, freei, 5002908c7f19STejun Heo GFP_KERNEL); 5003d5e03cbbSTheodore Ts'o } 5004d5e03cbbSTheodore Ts'o if (!err) 5005d5e03cbbSTheodore Ts'o err = percpu_counter_init(&sbi->s_dirs_counter, 5006908c7f19STejun Heo ext4_count_dirs(sb), GFP_KERNEL); 5007d5e03cbbSTheodore Ts'o if (!err) 5008908c7f19STejun Heo err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0, 5009908c7f19STejun Heo GFP_KERNEL); 5010c8585c6fSDaeho Jeong if (!err) 5011bbd55937SEric Biggers err = percpu_init_rwsem(&sbi->s_writepages_rwsem); 5012c8585c6fSDaeho Jeong 5013d5e03cbbSTheodore Ts'o if (err) { 5014d5e03cbbSTheodore Ts'o ext4_msg(sb, KERN_ERR, "insufficient memory"); 5015d5e03cbbSTheodore Ts'o goto failed_mount6; 5016d5e03cbbSTheodore Ts'o } 5017d5e03cbbSTheodore Ts'o 5018e2b911c5SDarrick J. Wong if (ext4_has_feature_flex_bg(sb)) 5019d5e03cbbSTheodore Ts'o if (!ext4_fill_flex_info(sb)) { 5020d5e03cbbSTheodore Ts'o ext4_msg(sb, KERN_ERR, 5021d5e03cbbSTheodore Ts'o "unable to initialize " 5022d5e03cbbSTheodore Ts'o "flex_bg meta info!"); 5023d5e03cbbSTheodore Ts'o goto failed_mount6; 5024d5e03cbbSTheodore Ts'o } 5025d5e03cbbSTheodore Ts'o 5026bfff6873SLukas Czerner err = ext4_register_li_request(sb, first_not_zeroed); 5027bfff6873SLukas Czerner if (err) 5028dcf2d804STao Ma goto failed_mount6; 5029bfff6873SLukas Czerner 5030b5799018STheodore Ts'o err = ext4_register_sysfs(sb); 5031dcf2d804STao Ma if (err) 5032dcf2d804STao Ma goto failed_mount7; 50333197ebdbSTheodore Ts'o 50349b2ff357SJan Kara #ifdef CONFIG_QUOTA 50359b2ff357SJan Kara /* Enable quota usage during mount. */ 5036bc98a42cSDavid Howells if (ext4_has_feature_quota(sb) && !sb_rdonly(sb)) { 50379b2ff357SJan Kara err = ext4_enable_quotas(sb); 50389b2ff357SJan Kara if (err) 50399b2ff357SJan Kara goto failed_mount8; 50409b2ff357SJan Kara } 50419b2ff357SJan Kara #endif /* CONFIG_QUOTA */ 50429b2ff357SJan Kara 5043bc71726cSzhangyi (F) /* 5044bc71726cSzhangyi (F) * Save the original bdev mapping's wb_err value which could be 5045bc71726cSzhangyi (F) * used to detect the metadata async write error. 5046bc71726cSzhangyi (F) */ 5047bc71726cSzhangyi (F) spin_lock_init(&sbi->s_bdev_wb_lock); 5048bc71726cSzhangyi (F) errseq_check_and_advance(&sb->s_bdev->bd_inode->i_mapping->wb_err, 5049bc71726cSzhangyi (F) &sbi->s_bdev_wb_err); 5050bc71726cSzhangyi (F) sb->s_bdev->bd_super = sb; 5051617ba13bSMingming Cao EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS; 5052617ba13bSMingming Cao ext4_orphan_cleanup(sb, es); 5053617ba13bSMingming Cao EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS; 50540390131bSFrank Mayhar if (needs_recovery) { 5055b31e1552SEric Sandeen ext4_msg(sb, KERN_INFO, "recovery complete"); 505611215630SJan Kara err = ext4_mark_recovery_complete(sb, es); 505711215630SJan Kara if (err) 505811215630SJan Kara goto failed_mount8; 50590390131bSFrank Mayhar } 50600390131bSFrank Mayhar if (EXT4_SB(sb)->s_journal) { 50610390131bSFrank Mayhar if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) 50620390131bSFrank Mayhar descr = " journalled data mode"; 50630390131bSFrank Mayhar else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) 50640390131bSFrank Mayhar descr = " ordered data mode"; 50650390131bSFrank Mayhar else 50660390131bSFrank Mayhar descr = " writeback data mode"; 50670390131bSFrank Mayhar } else 50680390131bSFrank Mayhar descr = "out journal"; 50690390131bSFrank Mayhar 507079add3a3SLukas Czerner if (test_opt(sb, DISCARD)) { 507179add3a3SLukas Czerner struct request_queue *q = bdev_get_queue(sb->s_bdev); 507279add3a3SLukas Czerner if (!blk_queue_discard(q)) 507379add3a3SLukas Czerner ext4_msg(sb, KERN_WARNING, 507479add3a3SLukas Czerner "mounting with \"discard\" option, but " 507579add3a3SLukas Czerner "the device does not support discard"); 507679add3a3SLukas Czerner } 507779add3a3SLukas Czerner 5078e294a537STheodore Ts'o if (___ratelimit(&ext4_mount_msg_ratelimit, "EXT4-fs mount")) 5079d4c402d9SCurt Wohlgemuth ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. " 50805aee0f8aSTheodore Ts'o "Opts: %.*s%s%s", descr, 50815aee0f8aSTheodore Ts'o (int) sizeof(sbi->s_es->s_mount_opts), 50825aee0f8aSTheodore Ts'o sbi->s_es->s_mount_opts, 50838b67f04aSTheodore Ts'o *sbi->s_es->s_mount_opts ? "; " : "", orig_data); 5084ac27a0ecSDave Kleikamp 508566e61a9eSTheodore Ts'o if (es->s_error_count) 508666e61a9eSTheodore Ts'o mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */ 5087ac27a0ecSDave Kleikamp 5088efbed4dcSTheodore Ts'o /* Enable message ratelimiting. Default is 10 messages per 5 secs. */ 5089efbed4dcSTheodore Ts'o ratelimit_state_init(&sbi->s_err_ratelimit_state, 5 * HZ, 10); 5090efbed4dcSTheodore Ts'o ratelimit_state_init(&sbi->s_warning_ratelimit_state, 5 * HZ, 10); 5091efbed4dcSTheodore Ts'o ratelimit_state_init(&sbi->s_msg_ratelimit_state, 5 * HZ, 10); 50921cf006edSDmitry Monakhov atomic_set(&sbi->s_warning_count, 0); 50931cf006edSDmitry Monakhov atomic_set(&sbi->s_msg_count, 0); 5094efbed4dcSTheodore Ts'o 5095d4c402d9SCurt Wohlgemuth kfree(orig_data); 5096ac27a0ecSDave Kleikamp return 0; 5097ac27a0ecSDave Kleikamp 5098617ba13bSMingming Cao cantfind_ext4: 5099ac27a0ecSDave Kleikamp if (!silent) 5100b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem"); 5101ac27a0ecSDave Kleikamp goto failed_mount; 5102ac27a0ecSDave Kleikamp 510372ba7450STheodore Ts'o failed_mount8: 5104ebd173beSTheodore Ts'o ext4_unregister_sysfs(sb); 5105cb8d53d2SEric Biggers kobject_put(&sbi->s_kobj); 5106dcf2d804STao Ma failed_mount7: 5107dcf2d804STao Ma ext4_unregister_li_request(sb); 5108dcf2d804STao Ma failed_mount6: 5109f9ae9cf5STheodore Ts'o ext4_mb_release(sb); 51107c990728SSuraj Jitindar Singh rcu_read_lock(); 51117c990728SSuraj Jitindar Singh flex_groups = rcu_dereference(sbi->s_flex_groups); 51127c990728SSuraj Jitindar Singh if (flex_groups) { 51137c990728SSuraj Jitindar Singh for (i = 0; i < sbi->s_flex_groups_allocated; i++) 51147c990728SSuraj Jitindar Singh kvfree(flex_groups[i]); 51157c990728SSuraj Jitindar Singh kvfree(flex_groups); 51167c990728SSuraj Jitindar Singh } 51177c990728SSuraj Jitindar Singh rcu_read_unlock(); 5118d5e03cbbSTheodore Ts'o percpu_counter_destroy(&sbi->s_freeclusters_counter); 5119d5e03cbbSTheodore Ts'o percpu_counter_destroy(&sbi->s_freeinodes_counter); 5120d5e03cbbSTheodore Ts'o percpu_counter_destroy(&sbi->s_dirs_counter); 5121d5e03cbbSTheodore Ts'o percpu_counter_destroy(&sbi->s_dirtyclusters_counter); 5122bbd55937SEric Biggers percpu_free_rwsem(&sbi->s_writepages_rwsem); 512300764937SAzat Khuzhin failed_mount5: 5124f9ae9cf5STheodore Ts'o ext4_ext_release(sb); 5125f9ae9cf5STheodore Ts'o ext4_release_system_zone(sb); 5126f9ae9cf5STheodore Ts'o failed_mount4a: 512794bf608aSAl Viro dput(sb->s_root); 512832a9bb57SManish Katiyar sb->s_root = NULL; 512994bf608aSAl Viro failed_mount4: 5130b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "mount failed"); 51312e8fa54eSJan Kara if (EXT4_SB(sb)->rsv_conversion_wq) 51322e8fa54eSJan Kara destroy_workqueue(EXT4_SB(sb)->rsv_conversion_wq); 51334c0425ffSMingming Cao failed_mount_wq: 5134dec214d0STahsin Erdogan ext4_xattr_destroy_cache(sbi->s_ea_inode_cache); 5135dec214d0STahsin Erdogan sbi->s_ea_inode_cache = NULL; 513650c15df6SChengguang Xu 513747387409STahsin Erdogan ext4_xattr_destroy_cache(sbi->s_ea_block_cache); 513847387409STahsin Erdogan sbi->s_ea_block_cache = NULL; 513950c15df6SChengguang Xu 51400390131bSFrank Mayhar if (sbi->s_journal) { 5141dab291afSMingming Cao jbd2_journal_destroy(sbi->s_journal); 514247b4a50bSJan Kara sbi->s_journal = NULL; 51430390131bSFrank Mayhar } 514450460fe8SDarrick J. Wong failed_mount3a: 5145d3922a77SZheng Liu ext4_es_unregister_shrinker(sbi); 5146eb68d0e2SZheng Liu failed_mount3: 51479105bb14SAl Viro del_timer_sync(&sbi->s_err_report); 5148c5e06d10SJohann Lombardi if (sbi->s_mmp_tsk) 5149c5e06d10SJohann Lombardi kthread_stop(sbi->s_mmp_tsk); 5150ac27a0ecSDave Kleikamp failed_mount2: 51511d0c3924STheodore Ts'o rcu_read_lock(); 51521d0c3924STheodore Ts'o group_desc = rcu_dereference(sbi->s_group_desc); 5153ac27a0ecSDave Kleikamp for (i = 0; i < db_count; i++) 51541d0c3924STheodore Ts'o brelse(group_desc[i]); 51551d0c3924STheodore Ts'o kvfree(group_desc); 51561d0c3924STheodore Ts'o rcu_read_unlock(); 5157ac27a0ecSDave Kleikamp failed_mount: 51580441984aSDarrick J. Wong if (sbi->s_chksum_driver) 51590441984aSDarrick J. Wong crypto_free_shash(sbi->s_chksum_driver); 5160c83ad55eSGabriel Krisman Bertazi 5161c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE 5162c83ad55eSGabriel Krisman Bertazi utf8_unload(sbi->s_encoding); 5163c83ad55eSGabriel Krisman Bertazi #endif 5164c83ad55eSGabriel Krisman Bertazi 5165ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 5166a2d4a646SJan Kara for (i = 0; i < EXT4_MAXQUOTAS; i++) 51670ba33facSTheodore Ts'o kfree(get_qf_name(sb, sbi, i)); 5168ac27a0ecSDave Kleikamp #endif 5169ed318a6cSEric Biggers fscrypt_free_dummy_context(&sbi->s_dummy_enc_ctx); 5170617ba13bSMingming Cao ext4_blkdev_remove(sbi); 5171ac27a0ecSDave Kleikamp brelse(bh); 5172ac27a0ecSDave Kleikamp out_fail: 5173ac27a0ecSDave Kleikamp sb->s_fs_info = NULL; 5174f6830165SManish Katiyar kfree(sbi->s_blockgroup_lock); 51755aee0f8aSTheodore Ts'o out_free_base: 5176ac27a0ecSDave Kleikamp kfree(sbi); 5177d4c402d9SCurt Wohlgemuth kfree(orig_data); 51785e405595SDan Williams fs_put_dax(dax_dev); 517907aa2ea1SLukas Czerner return err ? err : ret; 5180ac27a0ecSDave Kleikamp } 5181ac27a0ecSDave Kleikamp 5182ac27a0ecSDave Kleikamp /* 5183ac27a0ecSDave Kleikamp * Setup any per-fs journal parameters now. We'll do this both on 5184ac27a0ecSDave Kleikamp * initial mount, once the journal has been initialised but before we've 5185ac27a0ecSDave Kleikamp * done any recovery; and again on any subsequent remount. 5186ac27a0ecSDave Kleikamp */ 5187617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal) 5188ac27a0ecSDave Kleikamp { 5189617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 5190ac27a0ecSDave Kleikamp 5191ac27a0ecSDave Kleikamp journal->j_commit_interval = sbi->s_commit_interval; 519230773840STheodore Ts'o journal->j_min_batch_time = sbi->s_min_batch_time; 519330773840STheodore Ts'o journal->j_max_batch_time = sbi->s_max_batch_time; 51946866d7b3SHarshad Shirwadkar ext4_fc_init(sb, journal); 5195ac27a0ecSDave Kleikamp 5196a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 5197ac27a0ecSDave Kleikamp if (test_opt(sb, BARRIER)) 5198dab291afSMingming Cao journal->j_flags |= JBD2_BARRIER; 5199ac27a0ecSDave Kleikamp else 5200dab291afSMingming Cao journal->j_flags &= ~JBD2_BARRIER; 52015bf5683aSHidehiro Kawai if (test_opt(sb, DATA_ERR_ABORT)) 52025bf5683aSHidehiro Kawai journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR; 52035bf5683aSHidehiro Kawai else 52045bf5683aSHidehiro Kawai journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR; 5205a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 5206ac27a0ecSDave Kleikamp } 5207ac27a0ecSDave Kleikamp 5208c6cb7e77SEric Whitney static struct inode *ext4_get_journal_inode(struct super_block *sb, 5209ac27a0ecSDave Kleikamp unsigned int journal_inum) 5210ac27a0ecSDave Kleikamp { 5211ac27a0ecSDave Kleikamp struct inode *journal_inode; 5212ac27a0ecSDave Kleikamp 5213c6cb7e77SEric Whitney /* 5214c6cb7e77SEric Whitney * Test for the existence of a valid inode on disk. Bad things 5215c6cb7e77SEric Whitney * happen if we iget() an unused inode, as the subsequent iput() 5216c6cb7e77SEric Whitney * will try to delete it. 5217c6cb7e77SEric Whitney */ 52188a363970STheodore Ts'o journal_inode = ext4_iget(sb, journal_inum, EXT4_IGET_SPECIAL); 52191d1fe1eeSDavid Howells if (IS_ERR(journal_inode)) { 5220b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "no journal found"); 5221ac27a0ecSDave Kleikamp return NULL; 5222ac27a0ecSDave Kleikamp } 5223ac27a0ecSDave Kleikamp if (!journal_inode->i_nlink) { 5224ac27a0ecSDave Kleikamp make_bad_inode(journal_inode); 5225ac27a0ecSDave Kleikamp iput(journal_inode); 5226b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "journal inode is deleted"); 5227ac27a0ecSDave Kleikamp return NULL; 5228ac27a0ecSDave Kleikamp } 5229ac27a0ecSDave Kleikamp 5230e5f8eab8STheodore Ts'o jbd_debug(2, "Journal inode found at %p: %lld bytes\n", 5231ac27a0ecSDave Kleikamp journal_inode, journal_inode->i_size); 52321d1fe1eeSDavid Howells if (!S_ISREG(journal_inode->i_mode)) { 5233b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "invalid journal inode"); 5234ac27a0ecSDave Kleikamp iput(journal_inode); 5235ac27a0ecSDave Kleikamp return NULL; 5236ac27a0ecSDave Kleikamp } 5237c6cb7e77SEric Whitney return journal_inode; 5238c6cb7e77SEric Whitney } 5239c6cb7e77SEric Whitney 5240c6cb7e77SEric Whitney static journal_t *ext4_get_journal(struct super_block *sb, 5241c6cb7e77SEric Whitney unsigned int journal_inum) 5242c6cb7e77SEric Whitney { 5243c6cb7e77SEric Whitney struct inode *journal_inode; 5244c6cb7e77SEric Whitney journal_t *journal; 5245c6cb7e77SEric Whitney 524611215630SJan Kara if (WARN_ON_ONCE(!ext4_has_feature_journal(sb))) 524711215630SJan Kara return NULL; 5248c6cb7e77SEric Whitney 5249c6cb7e77SEric Whitney journal_inode = ext4_get_journal_inode(sb, journal_inum); 5250c6cb7e77SEric Whitney if (!journal_inode) 5251c6cb7e77SEric Whitney return NULL; 5252ac27a0ecSDave Kleikamp 5253dab291afSMingming Cao journal = jbd2_journal_init_inode(journal_inode); 5254ac27a0ecSDave Kleikamp if (!journal) { 5255b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "Could not load journal inode"); 5256ac27a0ecSDave Kleikamp iput(journal_inode); 5257ac27a0ecSDave Kleikamp return NULL; 5258ac27a0ecSDave Kleikamp } 5259ac27a0ecSDave Kleikamp journal->j_private = sb; 5260617ba13bSMingming Cao ext4_init_journal_params(sb, journal); 5261ac27a0ecSDave Kleikamp return journal; 5262ac27a0ecSDave Kleikamp } 5263ac27a0ecSDave Kleikamp 5264617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb, 5265ac27a0ecSDave Kleikamp dev_t j_dev) 5266ac27a0ecSDave Kleikamp { 5267ac27a0ecSDave Kleikamp struct buffer_head *bh; 5268ac27a0ecSDave Kleikamp journal_t *journal; 5269617ba13bSMingming Cao ext4_fsblk_t start; 5270617ba13bSMingming Cao ext4_fsblk_t len; 5271ac27a0ecSDave Kleikamp int hblock, blocksize; 5272617ba13bSMingming Cao ext4_fsblk_t sb_block; 5273ac27a0ecSDave Kleikamp unsigned long offset; 5274617ba13bSMingming Cao struct ext4_super_block *es; 5275ac27a0ecSDave Kleikamp struct block_device *bdev; 5276ac27a0ecSDave Kleikamp 527711215630SJan Kara if (WARN_ON_ONCE(!ext4_has_feature_journal(sb))) 527811215630SJan Kara return NULL; 52790390131bSFrank Mayhar 5280b31e1552SEric Sandeen bdev = ext4_blkdev_get(j_dev, sb); 5281ac27a0ecSDave Kleikamp if (bdev == NULL) 5282ac27a0ecSDave Kleikamp return NULL; 5283ac27a0ecSDave Kleikamp 5284ac27a0ecSDave Kleikamp blocksize = sb->s_blocksize; 5285e1defc4fSMartin K. Petersen hblock = bdev_logical_block_size(bdev); 5286ac27a0ecSDave Kleikamp if (blocksize < hblock) { 5287b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, 5288b31e1552SEric Sandeen "blocksize too small for journal device"); 5289ac27a0ecSDave Kleikamp goto out_bdev; 5290ac27a0ecSDave Kleikamp } 5291ac27a0ecSDave Kleikamp 5292617ba13bSMingming Cao sb_block = EXT4_MIN_BLOCK_SIZE / blocksize; 5293617ba13bSMingming Cao offset = EXT4_MIN_BLOCK_SIZE % blocksize; 5294ac27a0ecSDave Kleikamp set_blocksize(bdev, blocksize); 5295ac27a0ecSDave Kleikamp if (!(bh = __bread(bdev, sb_block, blocksize))) { 5296b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "couldn't read superblock of " 5297b31e1552SEric Sandeen "external journal"); 5298ac27a0ecSDave Kleikamp goto out_bdev; 5299ac27a0ecSDave Kleikamp } 5300ac27a0ecSDave Kleikamp 53012716b802STheodore Ts'o es = (struct ext4_super_block *) (bh->b_data + offset); 5302617ba13bSMingming Cao if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) || 5303ac27a0ecSDave Kleikamp !(le32_to_cpu(es->s_feature_incompat) & 5304617ba13bSMingming Cao EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) { 5305b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "external journal has " 5306b31e1552SEric Sandeen "bad superblock"); 5307ac27a0ecSDave Kleikamp brelse(bh); 5308ac27a0ecSDave Kleikamp goto out_bdev; 5309ac27a0ecSDave Kleikamp } 5310ac27a0ecSDave Kleikamp 5311df4763beSDarrick J. Wong if ((le32_to_cpu(es->s_feature_ro_compat) & 5312df4763beSDarrick J. Wong EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) && 5313df4763beSDarrick J. Wong es->s_checksum != ext4_superblock_csum(sb, es)) { 5314df4763beSDarrick J. Wong ext4_msg(sb, KERN_ERR, "external journal has " 5315df4763beSDarrick J. Wong "corrupt superblock"); 5316df4763beSDarrick J. Wong brelse(bh); 5317df4763beSDarrick J. Wong goto out_bdev; 5318df4763beSDarrick J. Wong } 5319df4763beSDarrick J. Wong 5320617ba13bSMingming Cao if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) { 5321b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "journal UUID does not match"); 5322ac27a0ecSDave Kleikamp brelse(bh); 5323ac27a0ecSDave Kleikamp goto out_bdev; 5324ac27a0ecSDave Kleikamp } 5325ac27a0ecSDave Kleikamp 5326bd81d8eeSLaurent Vivier len = ext4_blocks_count(es); 5327ac27a0ecSDave Kleikamp start = sb_block + 1; 5328ac27a0ecSDave Kleikamp brelse(bh); /* we're done with the superblock */ 5329ac27a0ecSDave Kleikamp 5330dab291afSMingming Cao journal = jbd2_journal_init_dev(bdev, sb->s_bdev, 5331ac27a0ecSDave Kleikamp start, len, blocksize); 5332ac27a0ecSDave Kleikamp if (!journal) { 5333b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "failed to create device journal"); 5334ac27a0ecSDave Kleikamp goto out_bdev; 5335ac27a0ecSDave Kleikamp } 5336ac27a0ecSDave Kleikamp journal->j_private = sb; 53372d069c08Szhangyi (F) if (ext4_read_bh_lock(journal->j_sb_buffer, REQ_META | REQ_PRIO, true)) { 5338b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "I/O error on journal device"); 5339ac27a0ecSDave Kleikamp goto out_journal; 5340ac27a0ecSDave Kleikamp } 5341ac27a0ecSDave Kleikamp if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) { 5342b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "External journal has more than one " 5343b31e1552SEric Sandeen "user (unsupported) - %d", 5344ac27a0ecSDave Kleikamp be32_to_cpu(journal->j_superblock->s_nr_users)); 5345ac27a0ecSDave Kleikamp goto out_journal; 5346ac27a0ecSDave Kleikamp } 5347ee7ed3aaSChunguang Xu EXT4_SB(sb)->s_journal_bdev = bdev; 5348617ba13bSMingming Cao ext4_init_journal_params(sb, journal); 5349ac27a0ecSDave Kleikamp return journal; 53500b8e58a1SAndreas Dilger 5351ac27a0ecSDave Kleikamp out_journal: 5352dab291afSMingming Cao jbd2_journal_destroy(journal); 5353ac27a0ecSDave Kleikamp out_bdev: 5354617ba13bSMingming Cao ext4_blkdev_put(bdev); 5355ac27a0ecSDave Kleikamp return NULL; 5356ac27a0ecSDave Kleikamp } 5357ac27a0ecSDave Kleikamp 5358617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb, 5359617ba13bSMingming Cao struct ext4_super_block *es, 5360ac27a0ecSDave Kleikamp unsigned long journal_devnum) 5361ac27a0ecSDave Kleikamp { 5362ac27a0ecSDave Kleikamp journal_t *journal; 5363ac27a0ecSDave Kleikamp unsigned int journal_inum = le32_to_cpu(es->s_journal_inum); 5364ac27a0ecSDave Kleikamp dev_t journal_dev; 5365ac27a0ecSDave Kleikamp int err = 0; 5366ac27a0ecSDave Kleikamp int really_read_only; 5367273108faSLukas Czerner int journal_dev_ro; 5368ac27a0ecSDave Kleikamp 536911215630SJan Kara if (WARN_ON_ONCE(!ext4_has_feature_journal(sb))) 537011215630SJan Kara return -EFSCORRUPTED; 53710390131bSFrank Mayhar 5372ac27a0ecSDave Kleikamp if (journal_devnum && 5373ac27a0ecSDave Kleikamp journal_devnum != le32_to_cpu(es->s_journal_dev)) { 5374b31e1552SEric Sandeen ext4_msg(sb, KERN_INFO, "external journal device major/minor " 5375b31e1552SEric Sandeen "numbers have changed"); 5376ac27a0ecSDave Kleikamp journal_dev = new_decode_dev(journal_devnum); 5377ac27a0ecSDave Kleikamp } else 5378ac27a0ecSDave Kleikamp journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev)); 5379ac27a0ecSDave Kleikamp 5380273108faSLukas Czerner if (journal_inum && journal_dev) { 5381273108faSLukas Czerner ext4_msg(sb, KERN_ERR, 5382273108faSLukas Czerner "filesystem has both journal inode and journal device!"); 5383273108faSLukas Czerner return -EINVAL; 5384273108faSLukas Czerner } 5385273108faSLukas Czerner 5386273108faSLukas Czerner if (journal_inum) { 5387273108faSLukas Czerner journal = ext4_get_journal(sb, journal_inum); 5388273108faSLukas Czerner if (!journal) 5389273108faSLukas Czerner return -EINVAL; 5390273108faSLukas Czerner } else { 5391273108faSLukas Czerner journal = ext4_get_dev_journal(sb, journal_dev); 5392273108faSLukas Czerner if (!journal) 5393273108faSLukas Czerner return -EINVAL; 5394273108faSLukas Czerner } 5395273108faSLukas Czerner 5396273108faSLukas Czerner journal_dev_ro = bdev_read_only(journal->j_dev); 5397273108faSLukas Czerner really_read_only = bdev_read_only(sb->s_bdev) | journal_dev_ro; 5398273108faSLukas Czerner 5399273108faSLukas Czerner if (journal_dev_ro && !sb_rdonly(sb)) { 5400273108faSLukas Czerner ext4_msg(sb, KERN_ERR, 5401273108faSLukas Czerner "journal device read-only, try mounting with '-o ro'"); 5402273108faSLukas Czerner err = -EROFS; 5403273108faSLukas Czerner goto err_out; 5404273108faSLukas Czerner } 5405ac27a0ecSDave Kleikamp 5406ac27a0ecSDave Kleikamp /* 5407ac27a0ecSDave Kleikamp * Are we loading a blank journal or performing recovery after a 5408ac27a0ecSDave Kleikamp * crash? For recovery, we need to check in advance whether we 5409ac27a0ecSDave Kleikamp * can get read-write access to the device. 5410ac27a0ecSDave Kleikamp */ 5411e2b911c5SDarrick J. Wong if (ext4_has_feature_journal_needs_recovery(sb)) { 5412bc98a42cSDavid Howells if (sb_rdonly(sb)) { 5413b31e1552SEric Sandeen ext4_msg(sb, KERN_INFO, "INFO: recovery " 5414b31e1552SEric Sandeen "required on readonly filesystem"); 5415ac27a0ecSDave Kleikamp if (really_read_only) { 5416b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "write access " 5417d98bf8cdSSimon Ruderich "unavailable, cannot proceed " 5418d98bf8cdSSimon Ruderich "(try mounting with noload)"); 5419273108faSLukas Czerner err = -EROFS; 5420273108faSLukas Czerner goto err_out; 5421ac27a0ecSDave Kleikamp } 5422b31e1552SEric Sandeen ext4_msg(sb, KERN_INFO, "write access will " 5423b31e1552SEric Sandeen "be enabled during recovery"); 5424ac27a0ecSDave Kleikamp } 5425ac27a0ecSDave Kleikamp } 5426ac27a0ecSDave Kleikamp 542790576c0bSTheodore Ts'o if (!(journal->j_flags & JBD2_BARRIER)) 5428b31e1552SEric Sandeen ext4_msg(sb, KERN_INFO, "barriers disabled"); 54294776004fSTheodore Ts'o 5430e2b911c5SDarrick J. Wong if (!ext4_has_feature_journal_needs_recovery(sb)) 5431dab291afSMingming Cao err = jbd2_journal_wipe(journal, !really_read_only); 54321c13d5c0STheodore Ts'o if (!err) { 54331c13d5c0STheodore Ts'o char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL); 54341c13d5c0STheodore Ts'o if (save) 54351c13d5c0STheodore Ts'o memcpy(save, ((char *) es) + 54361c13d5c0STheodore Ts'o EXT4_S_ERR_START, EXT4_S_ERR_LEN); 5437dab291afSMingming Cao err = jbd2_journal_load(journal); 54381c13d5c0STheodore Ts'o if (save) 54391c13d5c0STheodore Ts'o memcpy(((char *) es) + EXT4_S_ERR_START, 54401c13d5c0STheodore Ts'o save, EXT4_S_ERR_LEN); 54411c13d5c0STheodore Ts'o kfree(save); 54421c13d5c0STheodore Ts'o } 5443ac27a0ecSDave Kleikamp 5444ac27a0ecSDave Kleikamp if (err) { 5445b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "error loading journal"); 5446273108faSLukas Czerner goto err_out; 5447ac27a0ecSDave Kleikamp } 5448ac27a0ecSDave Kleikamp 5449617ba13bSMingming Cao EXT4_SB(sb)->s_journal = journal; 545011215630SJan Kara err = ext4_clear_journal_err(sb, es); 545111215630SJan Kara if (err) { 545211215630SJan Kara EXT4_SB(sb)->s_journal = NULL; 545311215630SJan Kara jbd2_journal_destroy(journal); 545411215630SJan Kara return err; 545511215630SJan Kara } 5456ac27a0ecSDave Kleikamp 5457c41303ceSMaciej Żenczykowski if (!really_read_only && journal_devnum && 5458ac27a0ecSDave Kleikamp journal_devnum != le32_to_cpu(es->s_journal_dev)) { 5459ac27a0ecSDave Kleikamp es->s_journal_dev = cpu_to_le32(journal_devnum); 5460ac27a0ecSDave Kleikamp 5461ac27a0ecSDave Kleikamp /* Make sure we flush the recovery flag to disk. */ 5462e2d67052STheodore Ts'o ext4_commit_super(sb, 1); 5463ac27a0ecSDave Kleikamp } 5464ac27a0ecSDave Kleikamp 5465ac27a0ecSDave Kleikamp return 0; 5466273108faSLukas Czerner 5467273108faSLukas Czerner err_out: 5468273108faSLukas Czerner jbd2_journal_destroy(journal); 5469273108faSLukas Czerner return err; 5470ac27a0ecSDave Kleikamp } 5471ac27a0ecSDave Kleikamp 5472e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync) 5473ac27a0ecSDave Kleikamp { 5474e2d67052STheodore Ts'o struct ext4_super_block *es = EXT4_SB(sb)->s_es; 5475617ba13bSMingming Cao struct buffer_head *sbh = EXT4_SB(sb)->s_sbh; 5476c4be0c1dSTakashi Sato int error = 0; 5477ac27a0ecSDave Kleikamp 5478bdfe0cbdSTheodore Ts'o if (!sbh || block_device_ejected(sb)) 5479c4be0c1dSTakashi Sato return error; 5480a17712c8SJon Derrick 5481a17712c8SJon Derrick /* 548271290b36STheodore Ts'o * If the file system is mounted read-only, don't update the 548371290b36STheodore Ts'o * superblock write time. This avoids updating the superblock 548471290b36STheodore Ts'o * write time when we are mounting the root file system 548571290b36STheodore Ts'o * read/only but we need to replay the journal; at that point, 548671290b36STheodore Ts'o * for people who are east of GMT and who make their clock 548771290b36STheodore Ts'o * tick in localtime for Windows bug-for-bug compatibility, 548871290b36STheodore Ts'o * the clock is set in the future, and this will cause e2fsck 548971290b36STheodore Ts'o * to complain and force a full file system check. 549071290b36STheodore Ts'o */ 54911751e8a6SLinus Torvalds if (!(sb->s_flags & SB_RDONLY)) 54926a0678a7SArnd Bergmann ext4_update_tstamp(es, s_wtime); 5493f613dfcbSTheodore Ts'o if (sb->s_bdev->bd_part) 5494afc32f7eSTheodore Ts'o es->s_kbytes_written = 5495afc32f7eSTheodore Ts'o cpu_to_le64(EXT4_SB(sb)->s_kbytes_written + 5496dbae2c55SMichael Callahan ((part_stat_read(sb->s_bdev->bd_part, 5497dbae2c55SMichael Callahan sectors[STAT_WRITE]) - 5498afc32f7eSTheodore Ts'o EXT4_SB(sb)->s_sectors_written_start) >> 1)); 5499f613dfcbSTheodore Ts'o else 5500f613dfcbSTheodore Ts'o es->s_kbytes_written = 5501f613dfcbSTheodore Ts'o cpu_to_le64(EXT4_SB(sb)->s_kbytes_written); 5502d5e03cbbSTheodore Ts'o if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeclusters_counter)) 550357042651STheodore Ts'o ext4_free_blocks_count_set(es, 550457042651STheodore Ts'o EXT4_C2B(EXT4_SB(sb), percpu_counter_sum_positive( 550557042651STheodore Ts'o &EXT4_SB(sb)->s_freeclusters_counter))); 5506d5e03cbbSTheodore Ts'o if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeinodes_counter)) 55077f93cff9STheodore Ts'o es->s_free_inodes_count = 55087f93cff9STheodore Ts'o cpu_to_le32(percpu_counter_sum_positive( 55095d1b1b3fSAneesh Kumar K.V &EXT4_SB(sb)->s_freeinodes_counter)); 5510ac27a0ecSDave Kleikamp BUFFER_TRACE(sbh, "marking dirty"); 551106db49e6STheodore Ts'o ext4_superblock_csum_set(sb); 55121566a48aSTheodore Ts'o if (sync) 55134743f839SPranay Kr. Srivastava lock_buffer(sbh); 5514e8680786STheodore Ts'o if (buffer_write_io_error(sbh) || !buffer_uptodate(sbh)) { 55154743f839SPranay Kr. Srivastava /* 55164743f839SPranay Kr. Srivastava * Oh, dear. A previous attempt to write the 55174743f839SPranay Kr. Srivastava * superblock failed. This could happen because the 55184743f839SPranay Kr. Srivastava * USB device was yanked out. Or it could happen to 55194743f839SPranay Kr. Srivastava * be a transient write error and maybe the block will 55204743f839SPranay Kr. Srivastava * be remapped. Nothing we can do but to retry the 55214743f839SPranay Kr. Srivastava * write and hope for the best. 55224743f839SPranay Kr. Srivastava */ 55234743f839SPranay Kr. Srivastava ext4_msg(sb, KERN_ERR, "previous I/O error to " 55244743f839SPranay Kr. Srivastava "superblock detected"); 55254743f839SPranay Kr. Srivastava clear_buffer_write_io_error(sbh); 55264743f839SPranay Kr. Srivastava set_buffer_uptodate(sbh); 55274743f839SPranay Kr. Srivastava } 5528ac27a0ecSDave Kleikamp mark_buffer_dirty(sbh); 5529914258bfSTheodore Ts'o if (sync) { 55301566a48aSTheodore Ts'o unlock_buffer(sbh); 5531564bc402SDaeho Jeong error = __sync_dirty_buffer(sbh, 553200473374SJan Kara REQ_SYNC | (test_opt(sb, BARRIER) ? REQ_FUA : 0)); 5533c89128a0SJaegeuk Kim if (buffer_write_io_error(sbh)) { 5534b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "I/O error while writing " 5535b31e1552SEric Sandeen "superblock"); 5536914258bfSTheodore Ts'o clear_buffer_write_io_error(sbh); 5537914258bfSTheodore Ts'o set_buffer_uptodate(sbh); 5538914258bfSTheodore Ts'o } 5539914258bfSTheodore Ts'o } 5540c4be0c1dSTakashi Sato return error; 5541ac27a0ecSDave Kleikamp } 5542ac27a0ecSDave Kleikamp 5543ac27a0ecSDave Kleikamp /* 5544ac27a0ecSDave Kleikamp * Have we just finished recovery? If so, and if we are mounting (or 5545ac27a0ecSDave Kleikamp * remounting) the filesystem readonly, then we will end up with a 5546ac27a0ecSDave Kleikamp * consistent fs on disk. Record that fact. 5547ac27a0ecSDave Kleikamp */ 554811215630SJan Kara static int ext4_mark_recovery_complete(struct super_block *sb, 5549617ba13bSMingming Cao struct ext4_super_block *es) 5550ac27a0ecSDave Kleikamp { 555111215630SJan Kara int err; 5552617ba13bSMingming Cao journal_t *journal = EXT4_SB(sb)->s_journal; 5553ac27a0ecSDave Kleikamp 5554e2b911c5SDarrick J. Wong if (!ext4_has_feature_journal(sb)) { 555511215630SJan Kara if (journal != NULL) { 555611215630SJan Kara ext4_error(sb, "Journal got removed while the fs was " 555711215630SJan Kara "mounted!"); 555811215630SJan Kara return -EFSCORRUPTED; 555911215630SJan Kara } 556011215630SJan Kara return 0; 55610390131bSFrank Mayhar } 5562dab291afSMingming Cao jbd2_journal_lock_updates(journal); 556311215630SJan Kara err = jbd2_journal_flush(journal); 556411215630SJan Kara if (err < 0) 55657ffe1ea8SHidehiro Kawai goto out; 55667ffe1ea8SHidehiro Kawai 5567bc98a42cSDavid Howells if (ext4_has_feature_journal_needs_recovery(sb) && sb_rdonly(sb)) { 5568e2b911c5SDarrick J. Wong ext4_clear_feature_journal_needs_recovery(sb); 5569e2d67052STheodore Ts'o ext4_commit_super(sb, 1); 5570ac27a0ecSDave Kleikamp } 55717ffe1ea8SHidehiro Kawai out: 5572dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 557311215630SJan Kara return err; 5574ac27a0ecSDave Kleikamp } 5575ac27a0ecSDave Kleikamp 5576ac27a0ecSDave Kleikamp /* 5577ac27a0ecSDave Kleikamp * If we are mounting (or read-write remounting) a filesystem whose journal 5578ac27a0ecSDave Kleikamp * has recorded an error from a previous lifetime, move that error to the 5579ac27a0ecSDave Kleikamp * main filesystem now. 5580ac27a0ecSDave Kleikamp */ 558111215630SJan Kara static int ext4_clear_journal_err(struct super_block *sb, 5582617ba13bSMingming Cao struct ext4_super_block *es) 5583ac27a0ecSDave Kleikamp { 5584ac27a0ecSDave Kleikamp journal_t *journal; 5585ac27a0ecSDave Kleikamp int j_errno; 5586ac27a0ecSDave Kleikamp const char *errstr; 5587ac27a0ecSDave Kleikamp 558811215630SJan Kara if (!ext4_has_feature_journal(sb)) { 558911215630SJan Kara ext4_error(sb, "Journal got removed while the fs was mounted!"); 559011215630SJan Kara return -EFSCORRUPTED; 559111215630SJan Kara } 55920390131bSFrank Mayhar 5593617ba13bSMingming Cao journal = EXT4_SB(sb)->s_journal; 5594ac27a0ecSDave Kleikamp 5595ac27a0ecSDave Kleikamp /* 5596ac27a0ecSDave Kleikamp * Now check for any error status which may have been recorded in the 5597617ba13bSMingming Cao * journal by a prior ext4_error() or ext4_abort() 5598ac27a0ecSDave Kleikamp */ 5599ac27a0ecSDave Kleikamp 5600dab291afSMingming Cao j_errno = jbd2_journal_errno(journal); 5601ac27a0ecSDave Kleikamp if (j_errno) { 5602ac27a0ecSDave Kleikamp char nbuf[16]; 5603ac27a0ecSDave Kleikamp 5604617ba13bSMingming Cao errstr = ext4_decode_error(sb, j_errno, nbuf); 560512062dddSEric Sandeen ext4_warning(sb, "Filesystem error recorded " 5606ac27a0ecSDave Kleikamp "from previous mount: %s", errstr); 560712062dddSEric Sandeen ext4_warning(sb, "Marking fs in need of filesystem check."); 5608ac27a0ecSDave Kleikamp 5609617ba13bSMingming Cao EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS; 5610617ba13bSMingming Cao es->s_state |= cpu_to_le16(EXT4_ERROR_FS); 5611e2d67052STheodore Ts'o ext4_commit_super(sb, 1); 5612ac27a0ecSDave Kleikamp 5613dab291afSMingming Cao jbd2_journal_clear_err(journal); 5614d796c52eSTheodore Ts'o jbd2_journal_update_sb_errno(journal); 5615ac27a0ecSDave Kleikamp } 561611215630SJan Kara return 0; 5617ac27a0ecSDave Kleikamp } 5618ac27a0ecSDave Kleikamp 5619ac27a0ecSDave Kleikamp /* 5620ac27a0ecSDave Kleikamp * Force the running and committing transactions to commit, 5621ac27a0ecSDave Kleikamp * and wait on the commit. 5622ac27a0ecSDave Kleikamp */ 5623617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb) 5624ac27a0ecSDave Kleikamp { 5625ac27a0ecSDave Kleikamp journal_t *journal; 5626ac27a0ecSDave Kleikamp 5627bc98a42cSDavid Howells if (sb_rdonly(sb)) 5628ac27a0ecSDave Kleikamp return 0; 5629ac27a0ecSDave Kleikamp 5630617ba13bSMingming Cao journal = EXT4_SB(sb)->s_journal; 5631b1deefc9SGuo Chao return ext4_journal_force_commit(journal); 5632ac27a0ecSDave Kleikamp } 5633ac27a0ecSDave Kleikamp 5634617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait) 5635ac27a0ecSDave Kleikamp { 563614ce0cb4STheodore Ts'o int ret = 0; 56379eddacf9SJan Kara tid_t target; 563806a407f1SDmitry Monakhov bool needs_barrier = false; 56398d5d02e6SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 5640ac27a0ecSDave Kleikamp 564149598e04SJun Piao if (unlikely(ext4_forced_shutdown(sbi))) 56420db1ff22STheodore Ts'o return 0; 56430db1ff22STheodore Ts'o 56449bffad1eSTheodore Ts'o trace_ext4_sync_fs(sb, wait); 56452e8fa54eSJan Kara flush_workqueue(sbi->rsv_conversion_wq); 5646a1177825SJan Kara /* 5647a1177825SJan Kara * Writeback quota in non-journalled quota case - journalled quota has 5648a1177825SJan Kara * no dirty dquots 5649a1177825SJan Kara */ 5650a1177825SJan Kara dquot_writeback_dquots(sb, -1); 565106a407f1SDmitry Monakhov /* 565206a407f1SDmitry Monakhov * Data writeback is possible w/o journal transaction, so barrier must 565306a407f1SDmitry Monakhov * being sent at the end of the function. But we can skip it if 565406a407f1SDmitry Monakhov * transaction_commit will do it for us. 565506a407f1SDmitry Monakhov */ 5656bda32530STheodore Ts'o if (sbi->s_journal) { 565706a407f1SDmitry Monakhov target = jbd2_get_latest_transaction(sbi->s_journal); 565806a407f1SDmitry Monakhov if (wait && sbi->s_journal->j_flags & JBD2_BARRIER && 565906a407f1SDmitry Monakhov !jbd2_trans_will_send_data_barrier(sbi->s_journal, target)) 566006a407f1SDmitry Monakhov needs_barrier = true; 566106a407f1SDmitry Monakhov 56628d5d02e6SMingming Cao if (jbd2_journal_start_commit(sbi->s_journal, &target)) { 5663ac27a0ecSDave Kleikamp if (wait) 5664bda32530STheodore Ts'o ret = jbd2_log_wait_commit(sbi->s_journal, 5665bda32530STheodore Ts'o target); 56660390131bSFrank Mayhar } 5667bda32530STheodore Ts'o } else if (wait && test_opt(sb, BARRIER)) 5668bda32530STheodore Ts'o needs_barrier = true; 566906a407f1SDmitry Monakhov if (needs_barrier) { 567006a407f1SDmitry Monakhov int err; 56719398554fSChristoph Hellwig err = blkdev_issue_flush(sb->s_bdev, GFP_KERNEL); 567206a407f1SDmitry Monakhov if (!ret) 567306a407f1SDmitry Monakhov ret = err; 567406a407f1SDmitry Monakhov } 567506a407f1SDmitry Monakhov 567606a407f1SDmitry Monakhov return ret; 567706a407f1SDmitry Monakhov } 567806a407f1SDmitry Monakhov 5679ac27a0ecSDave Kleikamp /* 5680ac27a0ecSDave Kleikamp * LVM calls this function before a (read-only) snapshot is created. This 5681ac27a0ecSDave Kleikamp * gives us a chance to flush the journal completely and mark the fs clean. 5682be4f27d3SYongqiang Yang * 5683be4f27d3SYongqiang Yang * Note that only this function cannot bring a filesystem to be in a clean 56848e8ad8a5SJan Kara * state independently. It relies on upper layer to stop all data & metadata 56858e8ad8a5SJan Kara * modifications. 5686ac27a0ecSDave Kleikamp */ 5687c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb) 5688ac27a0ecSDave Kleikamp { 5689c4be0c1dSTakashi Sato int error = 0; 5690c4be0c1dSTakashi Sato journal_t *journal; 5691ac27a0ecSDave Kleikamp 5692bc98a42cSDavid Howells if (sb_rdonly(sb)) 56939ca92389STheodore Ts'o return 0; 56949ca92389STheodore Ts'o 5695c4be0c1dSTakashi Sato journal = EXT4_SB(sb)->s_journal; 5696ac27a0ecSDave Kleikamp 5697bb044576STheodore Ts'o if (journal) { 5698ac27a0ecSDave Kleikamp /* Now we set up the journal barrier. */ 5699dab291afSMingming Cao jbd2_journal_lock_updates(journal); 57007ffe1ea8SHidehiro Kawai 57017ffe1ea8SHidehiro Kawai /* 5702bb044576STheodore Ts'o * Don't clear the needs_recovery flag if we failed to 5703bb044576STheodore Ts'o * flush the journal. 57047ffe1ea8SHidehiro Kawai */ 5705c4be0c1dSTakashi Sato error = jbd2_journal_flush(journal); 57066b0310fbSEric Sandeen if (error < 0) 57076b0310fbSEric Sandeen goto out; 5708ac27a0ecSDave Kleikamp 5709ac27a0ecSDave Kleikamp /* Journal blocked and flushed, clear needs_recovery flag. */ 5710e2b911c5SDarrick J. Wong ext4_clear_feature_journal_needs_recovery(sb); 5711c642dc9eSEric Sandeen } 5712c642dc9eSEric Sandeen 5713e2d67052STheodore Ts'o error = ext4_commit_super(sb, 1); 57146b0310fbSEric Sandeen out: 5715bb044576STheodore Ts'o if (journal) 57168e8ad8a5SJan Kara /* we rely on upper layer to stop further updates */ 5717bb044576STheodore Ts'o jbd2_journal_unlock_updates(journal); 57186b0310fbSEric Sandeen return error; 5719ac27a0ecSDave Kleikamp } 5720ac27a0ecSDave Kleikamp 5721ac27a0ecSDave Kleikamp /* 5722ac27a0ecSDave Kleikamp * Called by LVM after the snapshot is done. We need to reset the RECOVER 5723ac27a0ecSDave Kleikamp * flag here, even though the filesystem is not technically dirty yet. 5724ac27a0ecSDave Kleikamp */ 5725c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb) 5726ac27a0ecSDave Kleikamp { 5727bc98a42cSDavid Howells if (sb_rdonly(sb) || ext4_forced_shutdown(EXT4_SB(sb))) 57289ca92389STheodore Ts'o return 0; 57299ca92389STheodore Ts'o 5730c642dc9eSEric Sandeen if (EXT4_SB(sb)->s_journal) { 57319ca92389STheodore Ts'o /* Reset the needs_recovery flag before the fs is unlocked. */ 5732e2b911c5SDarrick J. Wong ext4_set_feature_journal_needs_recovery(sb); 5733c642dc9eSEric Sandeen } 5734c642dc9eSEric Sandeen 5735e2d67052STheodore Ts'o ext4_commit_super(sb, 1); 5736c4be0c1dSTakashi Sato return 0; 5737ac27a0ecSDave Kleikamp } 5738ac27a0ecSDave Kleikamp 5739673c6100STheodore Ts'o /* 5740673c6100STheodore Ts'o * Structure to save mount options for ext4_remount's benefit 5741673c6100STheodore Ts'o */ 5742673c6100STheodore Ts'o struct ext4_mount_options { 5743673c6100STheodore Ts'o unsigned long s_mount_opt; 5744a2595b8aSTheodore Ts'o unsigned long s_mount_opt2; 574508cefc7aSEric W. Biederman kuid_t s_resuid; 574608cefc7aSEric W. Biederman kgid_t s_resgid; 5747673c6100STheodore Ts'o unsigned long s_commit_interval; 5748673c6100STheodore Ts'o u32 s_min_batch_time, s_max_batch_time; 5749673c6100STheodore Ts'o #ifdef CONFIG_QUOTA 5750673c6100STheodore Ts'o int s_jquota_fmt; 5751a2d4a646SJan Kara char *s_qf_names[EXT4_MAXQUOTAS]; 5752673c6100STheodore Ts'o #endif 5753673c6100STheodore Ts'o }; 5754673c6100STheodore Ts'o 5755617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data) 5756ac27a0ecSDave Kleikamp { 5757617ba13bSMingming Cao struct ext4_super_block *es; 5758617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 5759f25391ebSLukas Czerner unsigned long old_sb_flags, vfs_flags; 5760617ba13bSMingming Cao struct ext4_mount_options old_opts; 5761c79d967dSChristoph Hellwig int enable_quota = 0; 57628a266467STheodore Ts'o ext4_group_t g; 5763b3881f74STheodore Ts'o unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO; 5764c5e06d10SJohann Lombardi int err = 0; 5765ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 576603dafb5fSChen Gang int i, j; 576733458eabSTheodore Ts'o char *to_free[EXT4_MAXQUOTAS]; 5768ac27a0ecSDave Kleikamp #endif 5769d4c402d9SCurt Wohlgemuth char *orig_data = kstrdup(data, GFP_KERNEL); 5770ac27a0ecSDave Kleikamp 577121ac738eSChengguang Xu if (data && !orig_data) 577221ac738eSChengguang Xu return -ENOMEM; 577321ac738eSChengguang Xu 5774ac27a0ecSDave Kleikamp /* Store the original options */ 5775ac27a0ecSDave Kleikamp old_sb_flags = sb->s_flags; 5776ac27a0ecSDave Kleikamp old_opts.s_mount_opt = sbi->s_mount_opt; 5777a2595b8aSTheodore Ts'o old_opts.s_mount_opt2 = sbi->s_mount_opt2; 5778ac27a0ecSDave Kleikamp old_opts.s_resuid = sbi->s_resuid; 5779ac27a0ecSDave Kleikamp old_opts.s_resgid = sbi->s_resgid; 5780ac27a0ecSDave Kleikamp old_opts.s_commit_interval = sbi->s_commit_interval; 578130773840STheodore Ts'o old_opts.s_min_batch_time = sbi->s_min_batch_time; 578230773840STheodore Ts'o old_opts.s_max_batch_time = sbi->s_max_batch_time; 5783ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 5784ac27a0ecSDave Kleikamp old_opts.s_jquota_fmt = sbi->s_jquota_fmt; 5785a2d4a646SJan Kara for (i = 0; i < EXT4_MAXQUOTAS; i++) 578603dafb5fSChen Gang if (sbi->s_qf_names[i]) { 578733458eabSTheodore Ts'o char *qf_name = get_qf_name(sb, sbi, i); 578833458eabSTheodore Ts'o 578933458eabSTheodore Ts'o old_opts.s_qf_names[i] = kstrdup(qf_name, GFP_KERNEL); 579003dafb5fSChen Gang if (!old_opts.s_qf_names[i]) { 579103dafb5fSChen Gang for (j = 0; j < i; j++) 579203dafb5fSChen Gang kfree(old_opts.s_qf_names[j]); 57933e36a163SWei Yongjun kfree(orig_data); 579403dafb5fSChen Gang return -ENOMEM; 579503dafb5fSChen Gang } 579603dafb5fSChen Gang } else 579703dafb5fSChen Gang old_opts.s_qf_names[i] = NULL; 5798ac27a0ecSDave Kleikamp #endif 5799b3881f74STheodore Ts'o if (sbi->s_journal && sbi->s_journal->j_task->io_context) 5800b3881f74STheodore Ts'o journal_ioprio = sbi->s_journal->j_task->io_context->ioprio; 5801ac27a0ecSDave Kleikamp 5802f25391ebSLukas Czerner /* 5803f25391ebSLukas Czerner * Some options can be enabled by ext4 and/or by VFS mount flag 5804f25391ebSLukas Czerner * either way we need to make sure it matches in both *flags and 5805f25391ebSLukas Czerner * s_flags. Copy those selected flags from *flags to s_flags 5806f25391ebSLukas Czerner */ 5807f25391ebSLukas Czerner vfs_flags = SB_LAZYTIME | SB_I_VERSION; 5808f25391ebSLukas Czerner sb->s_flags = (sb->s_flags & ~vfs_flags) | (*flags & vfs_flags); 5809f25391ebSLukas Czerner 5810661aa520SEric Sandeen if (!parse_options(data, sb, NULL, &journal_ioprio, 1)) { 5811ac27a0ecSDave Kleikamp err = -EINVAL; 5812ac27a0ecSDave Kleikamp goto restore_opts; 5813ac27a0ecSDave Kleikamp } 5814ac27a0ecSDave Kleikamp 58156b992ff2SDarrick J. Wong if ((old_opts.s_mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) ^ 58166b992ff2SDarrick J. Wong test_opt(sb, JOURNAL_CHECKSUM)) { 58176b992ff2SDarrick J. Wong ext4_msg(sb, KERN_ERR, "changing journal_checksum " 58182d5b86e0SEric Sandeen "during remount not supported; ignoring"); 58192d5b86e0SEric Sandeen sbi->s_mount_opt ^= EXT4_MOUNT_JOURNAL_CHECKSUM; 5820c6d3d56dSDarrick J. Wong } 5821c6d3d56dSDarrick J. Wong 58226ae6514bSPiotr Sarna if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) { 58236ae6514bSPiotr Sarna if (test_opt2(sb, EXPLICIT_DELALLOC)) { 58246ae6514bSPiotr Sarna ext4_msg(sb, KERN_ERR, "can't mount with " 58256ae6514bSPiotr Sarna "both data=journal and delalloc"); 58266ae6514bSPiotr Sarna err = -EINVAL; 58276ae6514bSPiotr Sarna goto restore_opts; 58286ae6514bSPiotr Sarna } 58296ae6514bSPiotr Sarna if (test_opt(sb, DIOREAD_NOLOCK)) { 58306ae6514bSPiotr Sarna ext4_msg(sb, KERN_ERR, "can't mount with " 58316ae6514bSPiotr Sarna "both data=journal and dioread_nolock"); 58326ae6514bSPiotr Sarna err = -EINVAL; 58336ae6514bSPiotr Sarna goto restore_opts; 58346ae6514bSPiotr Sarna } 5835ab04df78SJan Kara } else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) { 5836ab04df78SJan Kara if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) { 5837ab04df78SJan Kara ext4_msg(sb, KERN_ERR, "can't mount with " 5838ab04df78SJan Kara "journal_async_commit in data=ordered mode"); 5839ab04df78SJan Kara err = -EINVAL; 5840ab04df78SJan Kara goto restore_opts; 5841ab04df78SJan Kara } 5842923ae0ffSRoss Zwisler } 5843923ae0ffSRoss Zwisler 5844cdb7ee4cSTahsin Erdogan if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_NO_MBCACHE) { 5845cdb7ee4cSTahsin Erdogan ext4_msg(sb, KERN_ERR, "can't enable nombcache during remount"); 5846cdb7ee4cSTahsin Erdogan err = -EINVAL; 5847cdb7ee4cSTahsin Erdogan goto restore_opts; 5848cdb7ee4cSTahsin Erdogan } 5849cdb7ee4cSTahsin Erdogan 58504ab2f15bSTheodore Ts'o if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) 585154d3adbcSTheodore Ts'o ext4_abort(sb, EXT4_ERR_ESHUTDOWN, "Abort forced by user"); 5852ac27a0ecSDave Kleikamp 58531751e8a6SLinus Torvalds sb->s_flags = (sb->s_flags & ~SB_POSIXACL) | 58541751e8a6SLinus Torvalds (test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0); 5855ac27a0ecSDave Kleikamp 5856ac27a0ecSDave Kleikamp es = sbi->s_es; 5857ac27a0ecSDave Kleikamp 5858b3881f74STheodore Ts'o if (sbi->s_journal) { 5859617ba13bSMingming Cao ext4_init_journal_params(sb, sbi->s_journal); 5860b3881f74STheodore Ts'o set_task_ioprio(sbi->s_journal->j_task, journal_ioprio); 5861b3881f74STheodore Ts'o } 5862ac27a0ecSDave Kleikamp 58631751e8a6SLinus Torvalds if ((bool)(*flags & SB_RDONLY) != sb_rdonly(sb)) { 58644ab2f15bSTheodore Ts'o if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) { 5865ac27a0ecSDave Kleikamp err = -EROFS; 5866ac27a0ecSDave Kleikamp goto restore_opts; 5867ac27a0ecSDave Kleikamp } 5868ac27a0ecSDave Kleikamp 58691751e8a6SLinus Torvalds if (*flags & SB_RDONLY) { 587038c03b34STheodore Ts'o err = sync_filesystem(sb); 587138c03b34STheodore Ts'o if (err < 0) 587238c03b34STheodore Ts'o goto restore_opts; 58730f0dd62fSChristoph Hellwig err = dquot_suspend(sb, -1); 58740f0dd62fSChristoph Hellwig if (err < 0) 5875c79d967dSChristoph Hellwig goto restore_opts; 5876c79d967dSChristoph Hellwig 5877ac27a0ecSDave Kleikamp /* 5878ac27a0ecSDave Kleikamp * First of all, the unconditional stuff we have to do 5879ac27a0ecSDave Kleikamp * to disable replay of the journal when we next remount 5880ac27a0ecSDave Kleikamp */ 58811751e8a6SLinus Torvalds sb->s_flags |= SB_RDONLY; 5882ac27a0ecSDave Kleikamp 5883ac27a0ecSDave Kleikamp /* 5884ac27a0ecSDave Kleikamp * OK, test if we are remounting a valid rw partition 5885ac27a0ecSDave Kleikamp * readonly, and if so set the rdonly flag and then 5886ac27a0ecSDave Kleikamp * mark the partition as valid again. 5887ac27a0ecSDave Kleikamp */ 5888617ba13bSMingming Cao if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) && 5889617ba13bSMingming Cao (sbi->s_mount_state & EXT4_VALID_FS)) 5890ac27a0ecSDave Kleikamp es->s_state = cpu_to_le16(sbi->s_mount_state); 5891ac27a0ecSDave Kleikamp 589211215630SJan Kara if (sbi->s_journal) { 589311215630SJan Kara /* 589411215630SJan Kara * We let remount-ro finish even if marking fs 589511215630SJan Kara * as clean failed... 589611215630SJan Kara */ 5897617ba13bSMingming Cao ext4_mark_recovery_complete(sb, es); 589811215630SJan Kara } 58992dca60d9STheodore Ts'o if (sbi->s_mmp_tsk) 59002dca60d9STheodore Ts'o kthread_stop(sbi->s_mmp_tsk); 5901ac27a0ecSDave Kleikamp } else { 5902a13fb1a4SEric Sandeen /* Make sure we can mount this feature set readwrite */ 5903e2b911c5SDarrick J. Wong if (ext4_has_feature_readonly(sb) || 59042cb5cc8bSDarrick J. Wong !ext4_feature_set_ok(sb, 0)) { 5905ac27a0ecSDave Kleikamp err = -EROFS; 5906ac27a0ecSDave Kleikamp goto restore_opts; 5907ac27a0ecSDave Kleikamp } 5908ead6596bSEric Sandeen /* 59098a266467STheodore Ts'o * Make sure the group descriptor checksums 59100b8e58a1SAndreas Dilger * are sane. If they aren't, refuse to remount r/w. 59118a266467STheodore Ts'o */ 59128a266467STheodore Ts'o for (g = 0; g < sbi->s_groups_count; g++) { 59138a266467STheodore Ts'o struct ext4_group_desc *gdp = 59148a266467STheodore Ts'o ext4_get_group_desc(sb, g, NULL); 59158a266467STheodore Ts'o 5916feb0ab32SDarrick J. Wong if (!ext4_group_desc_csum_verify(sb, g, gdp)) { 5917b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, 5918b31e1552SEric Sandeen "ext4_remount: Checksum for group %u failed (%u!=%u)", 5919e2b911c5SDarrick J. Wong g, le16_to_cpu(ext4_group_desc_csum(sb, g, gdp)), 59208a266467STheodore Ts'o le16_to_cpu(gdp->bg_checksum)); 59216a797d27SDarrick J. Wong err = -EFSBADCRC; 59228a266467STheodore Ts'o goto restore_opts; 59238a266467STheodore Ts'o } 59248a266467STheodore Ts'o } 59258a266467STheodore Ts'o 59268a266467STheodore Ts'o /* 5927ead6596bSEric Sandeen * If we have an unprocessed orphan list hanging 5928ead6596bSEric Sandeen * around from a previously readonly bdev mount, 5929ead6596bSEric Sandeen * require a full umount/remount for now. 5930ead6596bSEric Sandeen */ 5931ead6596bSEric Sandeen if (es->s_last_orphan) { 5932b31e1552SEric Sandeen ext4_msg(sb, KERN_WARNING, "Couldn't " 5933ead6596bSEric Sandeen "remount RDWR because of unprocessed " 5934ead6596bSEric Sandeen "orphan inode list. Please " 5935b31e1552SEric Sandeen "umount/remount instead"); 5936ead6596bSEric Sandeen err = -EINVAL; 5937ead6596bSEric Sandeen goto restore_opts; 5938ead6596bSEric Sandeen } 5939ead6596bSEric Sandeen 5940ac27a0ecSDave Kleikamp /* 5941ac27a0ecSDave Kleikamp * Mounting a RDONLY partition read-write, so reread 5942ac27a0ecSDave Kleikamp * and store the current valid flag. (It may have 5943ac27a0ecSDave Kleikamp * been changed by e2fsck since we originally mounted 5944ac27a0ecSDave Kleikamp * the partition.) 5945ac27a0ecSDave Kleikamp */ 594611215630SJan Kara if (sbi->s_journal) { 594711215630SJan Kara err = ext4_clear_journal_err(sb, es); 594811215630SJan Kara if (err) 594911215630SJan Kara goto restore_opts; 595011215630SJan Kara } 5951ac27a0ecSDave Kleikamp sbi->s_mount_state = le16_to_cpu(es->s_state); 5952c89128a0SJaegeuk Kim 5953c89128a0SJaegeuk Kim err = ext4_setup_super(sb, es, 0); 5954c89128a0SJaegeuk Kim if (err) 5955c89128a0SJaegeuk Kim goto restore_opts; 5956c89128a0SJaegeuk Kim 59571751e8a6SLinus Torvalds sb->s_flags &= ~SB_RDONLY; 5958e2b911c5SDarrick J. Wong if (ext4_has_feature_mmp(sb)) 5959c5e06d10SJohann Lombardi if (ext4_multi_mount_protect(sb, 5960c5e06d10SJohann Lombardi le64_to_cpu(es->s_mmp_block))) { 5961c5e06d10SJohann Lombardi err = -EROFS; 5962c5e06d10SJohann Lombardi goto restore_opts; 5963c5e06d10SJohann Lombardi } 5964c79d967dSChristoph Hellwig enable_quota = 1; 5965ac27a0ecSDave Kleikamp } 5966ac27a0ecSDave Kleikamp } 5967bfff6873SLukas Czerner 5968bfff6873SLukas Czerner /* 5969bfff6873SLukas Czerner * Reinitialize lazy itable initialization thread based on 5970bfff6873SLukas Czerner * current settings 5971bfff6873SLukas Czerner */ 5972bc98a42cSDavid Howells if (sb_rdonly(sb) || !test_opt(sb, INIT_INODE_TABLE)) 5973bfff6873SLukas Czerner ext4_unregister_li_request(sb); 5974bfff6873SLukas Czerner else { 5975bfff6873SLukas Czerner ext4_group_t first_not_zeroed; 5976bfff6873SLukas Czerner first_not_zeroed = ext4_has_uninit_itable(sb); 5977bfff6873SLukas Czerner ext4_register_li_request(sb, first_not_zeroed); 5978bfff6873SLukas Czerner } 5979bfff6873SLukas Czerner 59800f5bde1dSJan Kara /* 59810f5bde1dSJan Kara * Handle creation of system zone data early because it can fail. 59820f5bde1dSJan Kara * Releasing of existing data is done when we are sure remount will 59830f5bde1dSJan Kara * succeed. 59840f5bde1dSJan Kara */ 5985dd0db94fSChunguang Xu if (test_opt(sb, BLOCK_VALIDITY) && !sbi->s_system_blks) { 5986d176b1f6SJan Kara err = ext4_setup_system_zone(sb); 5987d176b1f6SJan Kara if (err) 5988d176b1f6SJan Kara goto restore_opts; 59890f5bde1dSJan Kara } 5990d176b1f6SJan Kara 5991c89128a0SJaegeuk Kim if (sbi->s_journal == NULL && !(old_sb_flags & SB_RDONLY)) { 5992c89128a0SJaegeuk Kim err = ext4_commit_super(sb, 1); 5993c89128a0SJaegeuk Kim if (err) 5994c89128a0SJaegeuk Kim goto restore_opts; 5995c89128a0SJaegeuk Kim } 59960390131bSFrank Mayhar 5997ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 5998ac27a0ecSDave Kleikamp /* Release old quota file names */ 5999a2d4a646SJan Kara for (i = 0; i < EXT4_MAXQUOTAS; i++) 6000ac27a0ecSDave Kleikamp kfree(old_opts.s_qf_names[i]); 60017c319d32SAditya Kali if (enable_quota) { 60027c319d32SAditya Kali if (sb_any_quota_suspended(sb)) 60030f0dd62fSChristoph Hellwig dquot_resume(sb, -1); 6004e2b911c5SDarrick J. Wong else if (ext4_has_feature_quota(sb)) { 60057c319d32SAditya Kali err = ext4_enable_quotas(sb); 600607724f98STheodore Ts'o if (err) 60077c319d32SAditya Kali goto restore_opts; 60087c319d32SAditya Kali } 60097c319d32SAditya Kali } 60107c319d32SAditya Kali #endif 6011dd0db94fSChunguang Xu if (!test_opt(sb, BLOCK_VALIDITY) && sbi->s_system_blks) 60120f5bde1dSJan Kara ext4_release_system_zone(sb); 6013d4c402d9SCurt Wohlgemuth 6014f25391ebSLukas Czerner /* 6015f25391ebSLukas Czerner * Some options can be enabled by ext4 and/or by VFS mount flag 6016f25391ebSLukas Czerner * either way we need to make sure it matches in both *flags and 6017f25391ebSLukas Czerner * s_flags. Copy those selected flags from s_flags to *flags 6018f25391ebSLukas Czerner */ 6019f25391ebSLukas Czerner *flags = (*flags & ~vfs_flags) | (sb->s_flags & vfs_flags); 6020f25391ebSLukas Czerner 6021d4c402d9SCurt Wohlgemuth ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data); 6022d4c402d9SCurt Wohlgemuth kfree(orig_data); 6023ac27a0ecSDave Kleikamp return 0; 60240b8e58a1SAndreas Dilger 6025ac27a0ecSDave Kleikamp restore_opts: 6026ac27a0ecSDave Kleikamp sb->s_flags = old_sb_flags; 6027ac27a0ecSDave Kleikamp sbi->s_mount_opt = old_opts.s_mount_opt; 6028a2595b8aSTheodore Ts'o sbi->s_mount_opt2 = old_opts.s_mount_opt2; 6029ac27a0ecSDave Kleikamp sbi->s_resuid = old_opts.s_resuid; 6030ac27a0ecSDave Kleikamp sbi->s_resgid = old_opts.s_resgid; 6031ac27a0ecSDave Kleikamp sbi->s_commit_interval = old_opts.s_commit_interval; 603230773840STheodore Ts'o sbi->s_min_batch_time = old_opts.s_min_batch_time; 603330773840STheodore Ts'o sbi->s_max_batch_time = old_opts.s_max_batch_time; 6034dd0db94fSChunguang Xu if (!test_opt(sb, BLOCK_VALIDITY) && sbi->s_system_blks) 60350f5bde1dSJan Kara ext4_release_system_zone(sb); 6036ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 6037ac27a0ecSDave Kleikamp sbi->s_jquota_fmt = old_opts.s_jquota_fmt; 6038a2d4a646SJan Kara for (i = 0; i < EXT4_MAXQUOTAS; i++) { 603933458eabSTheodore Ts'o to_free[i] = get_qf_name(sb, sbi, i); 604033458eabSTheodore Ts'o rcu_assign_pointer(sbi->s_qf_names[i], old_opts.s_qf_names[i]); 6041ac27a0ecSDave Kleikamp } 604233458eabSTheodore Ts'o synchronize_rcu(); 604333458eabSTheodore Ts'o for (i = 0; i < EXT4_MAXQUOTAS; i++) 604433458eabSTheodore Ts'o kfree(to_free[i]); 6045ac27a0ecSDave Kleikamp #endif 6046d4c402d9SCurt Wohlgemuth kfree(orig_data); 6047ac27a0ecSDave Kleikamp return err; 6048ac27a0ecSDave Kleikamp } 6049ac27a0ecSDave Kleikamp 6050689c958cSLi Xi #ifdef CONFIG_QUOTA 6051689c958cSLi Xi static int ext4_statfs_project(struct super_block *sb, 6052689c958cSLi Xi kprojid_t projid, struct kstatfs *buf) 6053689c958cSLi Xi { 6054689c958cSLi Xi struct kqid qid; 6055689c958cSLi Xi struct dquot *dquot; 6056689c958cSLi Xi u64 limit; 6057689c958cSLi Xi u64 curblock; 6058689c958cSLi Xi 6059689c958cSLi Xi qid = make_kqid_projid(projid); 6060689c958cSLi Xi dquot = dqget(sb, qid); 6061689c958cSLi Xi if (IS_ERR(dquot)) 6062689c958cSLi Xi return PTR_ERR(dquot); 60637b9ca4c6SJan Kara spin_lock(&dquot->dq_dqb_lock); 6064689c958cSLi Xi 6065a08fe66eSChengguang Xu limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit, 6066a08fe66eSChengguang Xu dquot->dq_dqb.dqb_bhardlimit); 606757c32ea4SChengguang Xu limit >>= sb->s_blocksize_bits; 606857c32ea4SChengguang Xu 6069689c958cSLi Xi if (limit && buf->f_blocks > limit) { 6070f06925c7SKonstantin Khlebnikov curblock = (dquot->dq_dqb.dqb_curspace + 6071f06925c7SKonstantin Khlebnikov dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits; 6072689c958cSLi Xi buf->f_blocks = limit; 6073689c958cSLi Xi buf->f_bfree = buf->f_bavail = 6074689c958cSLi Xi (buf->f_blocks > curblock) ? 6075689c958cSLi Xi (buf->f_blocks - curblock) : 0; 6076689c958cSLi Xi } 6077689c958cSLi Xi 6078a08fe66eSChengguang Xu limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit, 6079a08fe66eSChengguang Xu dquot->dq_dqb.dqb_ihardlimit); 6080689c958cSLi Xi if (limit && buf->f_files > limit) { 6081689c958cSLi Xi buf->f_files = limit; 6082689c958cSLi Xi buf->f_ffree = 6083689c958cSLi Xi (buf->f_files > dquot->dq_dqb.dqb_curinodes) ? 6084689c958cSLi Xi (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0; 6085689c958cSLi Xi } 6086689c958cSLi Xi 60877b9ca4c6SJan Kara spin_unlock(&dquot->dq_dqb_lock); 6088689c958cSLi Xi dqput(dquot); 6089689c958cSLi Xi return 0; 6090689c958cSLi Xi } 6091689c958cSLi Xi #endif 6092689c958cSLi Xi 6093617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf) 6094ac27a0ecSDave Kleikamp { 6095ac27a0ecSDave Kleikamp struct super_block *sb = dentry->d_sb; 6096617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 6097617ba13bSMingming Cao struct ext4_super_block *es = sbi->s_es; 609827dd4385SLukas Czerner ext4_fsblk_t overhead = 0, resv_blocks; 6099960cc398SPekka Enberg u64 fsid; 6100d02a9391SKazuya Mio s64 bfree; 610127dd4385SLukas Czerner resv_blocks = EXT4_C2B(sbi, atomic64_read(&sbi->s_resv_clusters)); 6102ac27a0ecSDave Kleikamp 6103952fc18eSTheodore Ts'o if (!test_opt(sb, MINIX_DF)) 6104952fc18eSTheodore Ts'o overhead = sbi->s_overhead; 6105ac27a0ecSDave Kleikamp 6106617ba13bSMingming Cao buf->f_type = EXT4_SUPER_MAGIC; 6107ac27a0ecSDave Kleikamp buf->f_bsize = sb->s_blocksize; 6108b72f78cbSEric Sandeen buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, overhead); 610957042651STheodore Ts'o bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) - 611057042651STheodore Ts'o percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter); 6111d02a9391SKazuya Mio /* prevent underflow in case that few free space is available */ 611257042651STheodore Ts'o buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0)); 611327dd4385SLukas Czerner buf->f_bavail = buf->f_bfree - 611427dd4385SLukas Czerner (ext4_r_blocks_count(es) + resv_blocks); 611527dd4385SLukas Czerner if (buf->f_bfree < (ext4_r_blocks_count(es) + resv_blocks)) 6116ac27a0ecSDave Kleikamp buf->f_bavail = 0; 6117ac27a0ecSDave Kleikamp buf->f_files = le32_to_cpu(es->s_inodes_count); 611852d9f3b4SPeter Zijlstra buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter); 6119617ba13bSMingming Cao buf->f_namelen = EXT4_NAME_LEN; 6120960cc398SPekka Enberg fsid = le64_to_cpup((void *)es->s_uuid) ^ 6121960cc398SPekka Enberg le64_to_cpup((void *)es->s_uuid + sizeof(u64)); 6122960cc398SPekka Enberg buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL; 6123960cc398SPekka Enberg buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL; 61240b8e58a1SAndreas Dilger 6125689c958cSLi Xi #ifdef CONFIG_QUOTA 6126689c958cSLi Xi if (ext4_test_inode_flag(dentry->d_inode, EXT4_INODE_PROJINHERIT) && 6127689c958cSLi Xi sb_has_quota_limits_enabled(sb, PRJQUOTA)) 6128689c958cSLi Xi ext4_statfs_project(sb, EXT4_I(dentry->d_inode)->i_projid, buf); 6129689c958cSLi Xi #endif 6130ac27a0ecSDave Kleikamp return 0; 6131ac27a0ecSDave Kleikamp } 6132ac27a0ecSDave Kleikamp 6133ac27a0ecSDave Kleikamp 6134ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 6135ac27a0ecSDave Kleikamp 6136bc8230eeSJan Kara /* 6137bc8230eeSJan Kara * Helper functions so that transaction is started before we acquire dqio_sem 6138bc8230eeSJan Kara * to keep correct lock ordering of transaction > dqio_sem 6139bc8230eeSJan Kara */ 6140ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot) 6141ac27a0ecSDave Kleikamp { 61424c376dcaSEric W. Biederman return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type]; 6143ac27a0ecSDave Kleikamp } 6144ac27a0ecSDave Kleikamp 6145617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot) 6146ac27a0ecSDave Kleikamp { 6147ac27a0ecSDave Kleikamp int ret, err; 6148ac27a0ecSDave Kleikamp handle_t *handle; 6149ac27a0ecSDave Kleikamp struct inode *inode; 6150ac27a0ecSDave Kleikamp 6151ac27a0ecSDave Kleikamp inode = dquot_to_inode(dquot); 61529924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 6153617ba13bSMingming Cao EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb)); 6154ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 6155ac27a0ecSDave Kleikamp return PTR_ERR(handle); 6156ac27a0ecSDave Kleikamp ret = dquot_commit(dquot); 6157617ba13bSMingming Cao err = ext4_journal_stop(handle); 6158ac27a0ecSDave Kleikamp if (!ret) 6159ac27a0ecSDave Kleikamp ret = err; 6160ac27a0ecSDave Kleikamp return ret; 6161ac27a0ecSDave Kleikamp } 6162ac27a0ecSDave Kleikamp 6163617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot) 6164ac27a0ecSDave Kleikamp { 6165ac27a0ecSDave Kleikamp int ret, err; 6166ac27a0ecSDave Kleikamp handle_t *handle; 6167ac27a0ecSDave Kleikamp 61689924a92aSTheodore Ts'o handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA, 6169617ba13bSMingming Cao EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb)); 6170ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 6171ac27a0ecSDave Kleikamp return PTR_ERR(handle); 6172ac27a0ecSDave Kleikamp ret = dquot_acquire(dquot); 6173617ba13bSMingming Cao err = ext4_journal_stop(handle); 6174ac27a0ecSDave Kleikamp if (!ret) 6175ac27a0ecSDave Kleikamp ret = err; 6176ac27a0ecSDave Kleikamp return ret; 6177ac27a0ecSDave Kleikamp } 6178ac27a0ecSDave Kleikamp 6179617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot) 6180ac27a0ecSDave Kleikamp { 6181ac27a0ecSDave Kleikamp int ret, err; 6182ac27a0ecSDave Kleikamp handle_t *handle; 6183ac27a0ecSDave Kleikamp 61849924a92aSTheodore Ts'o handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA, 6185617ba13bSMingming Cao EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb)); 61869c3013e9SJan Kara if (IS_ERR(handle)) { 61879c3013e9SJan Kara /* Release dquot anyway to avoid endless cycle in dqput() */ 61889c3013e9SJan Kara dquot_release(dquot); 6189ac27a0ecSDave Kleikamp return PTR_ERR(handle); 61909c3013e9SJan Kara } 6191ac27a0ecSDave Kleikamp ret = dquot_release(dquot); 6192617ba13bSMingming Cao err = ext4_journal_stop(handle); 6193ac27a0ecSDave Kleikamp if (!ret) 6194ac27a0ecSDave Kleikamp ret = err; 6195ac27a0ecSDave Kleikamp return ret; 6196ac27a0ecSDave Kleikamp } 6197ac27a0ecSDave Kleikamp 6198617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot) 6199ac27a0ecSDave Kleikamp { 6200262b4662SJan Kara struct super_block *sb = dquot->dq_sb; 6201262b4662SJan Kara struct ext4_sb_info *sbi = EXT4_SB(sb); 6202262b4662SJan Kara 62032c8be6b2SJan Kara /* Are we journaling quotas? */ 6204e2b911c5SDarrick J. Wong if (ext4_has_feature_quota(sb) || 6205262b4662SJan Kara sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) { 6206ac27a0ecSDave Kleikamp dquot_mark_dquot_dirty(dquot); 6207617ba13bSMingming Cao return ext4_write_dquot(dquot); 6208ac27a0ecSDave Kleikamp } else { 6209ac27a0ecSDave Kleikamp return dquot_mark_dquot_dirty(dquot); 6210ac27a0ecSDave Kleikamp } 6211ac27a0ecSDave Kleikamp } 6212ac27a0ecSDave Kleikamp 6213617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type) 6214ac27a0ecSDave Kleikamp { 6215ac27a0ecSDave Kleikamp int ret, err; 6216ac27a0ecSDave Kleikamp handle_t *handle; 6217ac27a0ecSDave Kleikamp 6218ac27a0ecSDave Kleikamp /* Data block + inode block */ 62192b0143b5SDavid Howells handle = ext4_journal_start(d_inode(sb->s_root), EXT4_HT_QUOTA, 2); 6220ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 6221ac27a0ecSDave Kleikamp return PTR_ERR(handle); 6222ac27a0ecSDave Kleikamp ret = dquot_commit_info(sb, type); 6223617ba13bSMingming Cao err = ext4_journal_stop(handle); 6224ac27a0ecSDave Kleikamp if (!ret) 6225ac27a0ecSDave Kleikamp ret = err; 6226ac27a0ecSDave Kleikamp return ret; 6227ac27a0ecSDave Kleikamp } 6228ac27a0ecSDave Kleikamp 6229ac27a0ecSDave Kleikamp /* 6230ac27a0ecSDave Kleikamp * Turn on quotas during mount time - we need to find 6231ac27a0ecSDave Kleikamp * the quota file and such... 6232ac27a0ecSDave Kleikamp */ 6233617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type) 6234ac27a0ecSDave Kleikamp { 623533458eabSTheodore Ts'o return dquot_quota_on_mount(sb, get_qf_name(sb, EXT4_SB(sb), type), 6236617ba13bSMingming Cao EXT4_SB(sb)->s_jquota_fmt, type); 6237ac27a0ecSDave Kleikamp } 6238ac27a0ecSDave Kleikamp 6239daf647d2STheodore Ts'o static void lockdep_set_quota_inode(struct inode *inode, int subclass) 6240daf647d2STheodore Ts'o { 6241daf647d2STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 6242daf647d2STheodore Ts'o 6243daf647d2STheodore Ts'o /* The first argument of lockdep_set_subclass has to be 6244daf647d2STheodore Ts'o * *exactly* the same as the argument to init_rwsem() --- in 6245daf647d2STheodore Ts'o * this case, in init_once() --- or lockdep gets unhappy 6246daf647d2STheodore Ts'o * because the name of the lock is set using the 6247daf647d2STheodore Ts'o * stringification of the argument to init_rwsem(). 6248daf647d2STheodore Ts'o */ 6249daf647d2STheodore Ts'o (void) ei; /* shut up clang warning if !CONFIG_LOCKDEP */ 6250daf647d2STheodore Ts'o lockdep_set_subclass(&ei->i_data_sem, subclass); 6251daf647d2STheodore Ts'o } 6252daf647d2STheodore Ts'o 6253ac27a0ecSDave Kleikamp /* 6254ac27a0ecSDave Kleikamp * Standard function to be called on quota_on 6255ac27a0ecSDave Kleikamp */ 6256617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id, 62578c54ca9cSAl Viro const struct path *path) 6258ac27a0ecSDave Kleikamp { 6259ac27a0ecSDave Kleikamp int err; 6260ac27a0ecSDave Kleikamp 6261ac27a0ecSDave Kleikamp if (!test_opt(sb, QUOTA)) 6262ac27a0ecSDave Kleikamp return -EINVAL; 62630623543bSJan Kara 6264ac27a0ecSDave Kleikamp /* Quotafile not on the same filesystem? */ 6265d8c9584eSAl Viro if (path->dentry->d_sb != sb) 6266ac27a0ecSDave Kleikamp return -EXDEV; 6267e0770e91SJan Kara 6268e0770e91SJan Kara /* Quota already enabled for this file? */ 6269e0770e91SJan Kara if (IS_NOQUOTA(d_inode(path->dentry))) 6270e0770e91SJan Kara return -EBUSY; 6271e0770e91SJan Kara 62720623543bSJan Kara /* Journaling quota? */ 62730623543bSJan Kara if (EXT4_SB(sb)->s_qf_names[type]) { 62742b2d6d01STheodore Ts'o /* Quotafile not in fs root? */ 6275f00c9e44SJan Kara if (path->dentry->d_parent != sb->s_root) 6276b31e1552SEric Sandeen ext4_msg(sb, KERN_WARNING, 6277b31e1552SEric Sandeen "Quota file not on filesystem root. " 6278b31e1552SEric Sandeen "Journaled quota will not work"); 627991389240SJan Kara sb_dqopt(sb)->flags |= DQUOT_NOLIST_DIRTY; 628091389240SJan Kara } else { 628191389240SJan Kara /* 628291389240SJan Kara * Clear the flag just in case mount options changed since 628391389240SJan Kara * last time. 628491389240SJan Kara */ 628591389240SJan Kara sb_dqopt(sb)->flags &= ~DQUOT_NOLIST_DIRTY; 62860623543bSJan Kara } 62870623543bSJan Kara 62880623543bSJan Kara /* 62890623543bSJan Kara * When we journal data on quota file, we have to flush journal to see 62900623543bSJan Kara * all updates to the file when we bypass pagecache... 62910623543bSJan Kara */ 62920390131bSFrank Mayhar if (EXT4_SB(sb)->s_journal && 62932b0143b5SDavid Howells ext4_should_journal_data(d_inode(path->dentry))) { 62940623543bSJan Kara /* 62950623543bSJan Kara * We don't need to lock updates but journal_flush() could 62960623543bSJan Kara * otherwise be livelocked... 62970623543bSJan Kara */ 62980623543bSJan Kara jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); 62997ffe1ea8SHidehiro Kawai err = jbd2_journal_flush(EXT4_SB(sb)->s_journal); 63000623543bSJan Kara jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); 6301f00c9e44SJan Kara if (err) 63027ffe1ea8SHidehiro Kawai return err; 63037ffe1ea8SHidehiro Kawai } 6304957153fcSJan Kara 6305daf647d2STheodore Ts'o lockdep_set_quota_inode(path->dentry->d_inode, I_DATA_SEM_QUOTA); 6306daf647d2STheodore Ts'o err = dquot_quota_on(sb, type, format_id, path); 6307957153fcSJan Kara if (err) { 6308daf647d2STheodore Ts'o lockdep_set_quota_inode(path->dentry->d_inode, 6309daf647d2STheodore Ts'o I_DATA_SEM_NORMAL); 6310957153fcSJan Kara } else { 6311957153fcSJan Kara struct inode *inode = d_inode(path->dentry); 6312957153fcSJan Kara handle_t *handle; 6313957153fcSJan Kara 631461a92987SJan Kara /* 631561a92987SJan Kara * Set inode flags to prevent userspace from messing with quota 631661a92987SJan Kara * files. If this fails, we return success anyway since quotas 631761a92987SJan Kara * are already enabled and this is not a hard failure. 631861a92987SJan Kara */ 6319957153fcSJan Kara inode_lock(inode); 6320957153fcSJan Kara handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1); 6321957153fcSJan Kara if (IS_ERR(handle)) 6322957153fcSJan Kara goto unlock_inode; 6323957153fcSJan Kara EXT4_I(inode)->i_flags |= EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL; 6324957153fcSJan Kara inode_set_flags(inode, S_NOATIME | S_IMMUTABLE, 6325957153fcSJan Kara S_NOATIME | S_IMMUTABLE); 63264209ae12SHarshad Shirwadkar err = ext4_mark_inode_dirty(handle, inode); 6327957153fcSJan Kara ext4_journal_stop(handle); 6328957153fcSJan Kara unlock_inode: 6329957153fcSJan Kara inode_unlock(inode); 6330957153fcSJan Kara } 6331daf647d2STheodore Ts'o return err; 6332ac27a0ecSDave Kleikamp } 6333ac27a0ecSDave Kleikamp 63347c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id, 63357c319d32SAditya Kali unsigned int flags) 63367c319d32SAditya Kali { 63377c319d32SAditya Kali int err; 63387c319d32SAditya Kali struct inode *qf_inode; 6339a2d4a646SJan Kara unsigned long qf_inums[EXT4_MAXQUOTAS] = { 63407c319d32SAditya Kali le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum), 6341689c958cSLi Xi le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum), 6342689c958cSLi Xi le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum) 63437c319d32SAditya Kali }; 63447c319d32SAditya Kali 6345e2b911c5SDarrick J. Wong BUG_ON(!ext4_has_feature_quota(sb)); 63467c319d32SAditya Kali 63477c319d32SAditya Kali if (!qf_inums[type]) 63487c319d32SAditya Kali return -EPERM; 63497c319d32SAditya Kali 63508a363970STheodore Ts'o qf_inode = ext4_iget(sb, qf_inums[type], EXT4_IGET_SPECIAL); 63517c319d32SAditya Kali if (IS_ERR(qf_inode)) { 63527c319d32SAditya Kali ext4_error(sb, "Bad quota inode # %lu", qf_inums[type]); 63537c319d32SAditya Kali return PTR_ERR(qf_inode); 63547c319d32SAditya Kali } 63557c319d32SAditya Kali 6356bcb13850SJan Kara /* Don't account quota for quota files to avoid recursion */ 6357bcb13850SJan Kara qf_inode->i_flags |= S_NOQUOTA; 6358daf647d2STheodore Ts'o lockdep_set_quota_inode(qf_inode, I_DATA_SEM_QUOTA); 63597212b95eSJan Kara err = dquot_load_quota_inode(qf_inode, type, format_id, flags); 6360daf647d2STheodore Ts'o if (err) 6361daf647d2STheodore Ts'o lockdep_set_quota_inode(qf_inode, I_DATA_SEM_NORMAL); 636261157b24SPan Bian iput(qf_inode); 63637c319d32SAditya Kali 63647c319d32SAditya Kali return err; 63657c319d32SAditya Kali } 63667c319d32SAditya Kali 63677c319d32SAditya Kali /* Enable usage tracking for all quota types. */ 63687c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb) 63697c319d32SAditya Kali { 63707c319d32SAditya Kali int type, err = 0; 6371a2d4a646SJan Kara unsigned long qf_inums[EXT4_MAXQUOTAS] = { 63727c319d32SAditya Kali le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum), 6373689c958cSLi Xi le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum), 6374689c958cSLi Xi le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum) 63757c319d32SAditya Kali }; 637649da9392SJan Kara bool quota_mopt[EXT4_MAXQUOTAS] = { 637749da9392SJan Kara test_opt(sb, USRQUOTA), 637849da9392SJan Kara test_opt(sb, GRPQUOTA), 637949da9392SJan Kara test_opt(sb, PRJQUOTA), 638049da9392SJan Kara }; 63817c319d32SAditya Kali 638291389240SJan Kara sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY; 6383a2d4a646SJan Kara for (type = 0; type < EXT4_MAXQUOTAS; type++) { 63847c319d32SAditya Kali if (qf_inums[type]) { 63857c319d32SAditya Kali err = ext4_quota_enable(sb, type, QFMT_VFS_V1, 638649da9392SJan Kara DQUOT_USAGE_ENABLED | 638749da9392SJan Kara (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0)); 63887c319d32SAditya Kali if (err) { 63897c319d32SAditya Kali ext4_warning(sb, 639072ba7450STheodore Ts'o "Failed to enable quota tracking " 639172ba7450STheodore Ts'o "(type=%d, err=%d). Please run " 639272ba7450STheodore Ts'o "e2fsck to fix.", type, err); 63937f144fd0SJunichi Uekawa for (type--; type >= 0; type--) 63947f144fd0SJunichi Uekawa dquot_quota_off(sb, type); 63957f144fd0SJunichi Uekawa 63967c319d32SAditya Kali return err; 63977c319d32SAditya Kali } 63987c319d32SAditya Kali } 63997c319d32SAditya Kali } 64007c319d32SAditya Kali return 0; 64017c319d32SAditya Kali } 64027c319d32SAditya Kali 6403ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type) 6404ca0e05e4SDmitry Monakhov { 640521f97697SJan Kara struct inode *inode = sb_dqopt(sb)->files[type]; 640621f97697SJan Kara handle_t *handle; 6407957153fcSJan Kara int err; 640821f97697SJan Kara 640987009d86SDmitry Monakhov /* Force all delayed allocation blocks to be allocated. 641087009d86SDmitry Monakhov * Caller already holds s_umount sem */ 641187009d86SDmitry Monakhov if (test_opt(sb, DELALLOC)) 6412ca0e05e4SDmitry Monakhov sync_filesystem(sb); 6413ca0e05e4SDmitry Monakhov 6414957153fcSJan Kara if (!inode || !igrab(inode)) 64150b268590SAmir Goldstein goto out; 64160b268590SAmir Goldstein 6417957153fcSJan Kara err = dquot_quota_off(sb, type); 6418964edf66SJan Kara if (err || ext4_has_feature_quota(sb)) 6419957153fcSJan Kara goto out_put; 6420957153fcSJan Kara 6421957153fcSJan Kara inode_lock(inode); 642261a92987SJan Kara /* 642361a92987SJan Kara * Update modification times of quota files when userspace can 642461a92987SJan Kara * start looking at them. If we fail, we return success anyway since 642561a92987SJan Kara * this is not a hard failure and quotas are already disabled. 642661a92987SJan Kara */ 64279924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1); 64284209ae12SHarshad Shirwadkar if (IS_ERR(handle)) { 64294209ae12SHarshad Shirwadkar err = PTR_ERR(handle); 6430957153fcSJan Kara goto out_unlock; 64314209ae12SHarshad Shirwadkar } 6432957153fcSJan Kara EXT4_I(inode)->i_flags &= ~(EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL); 6433957153fcSJan Kara inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE); 6434eeca7ea1SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_time(inode); 64354209ae12SHarshad Shirwadkar err = ext4_mark_inode_dirty(handle, inode); 643621f97697SJan Kara ext4_journal_stop(handle); 6437957153fcSJan Kara out_unlock: 6438957153fcSJan Kara inode_unlock(inode); 6439957153fcSJan Kara out_put: 6440964edf66SJan Kara lockdep_set_quota_inode(inode, I_DATA_SEM_NORMAL); 6441957153fcSJan Kara iput(inode); 6442957153fcSJan Kara return err; 644321f97697SJan Kara out: 6444ca0e05e4SDmitry Monakhov return dquot_quota_off(sb, type); 6445ca0e05e4SDmitry Monakhov } 6446ca0e05e4SDmitry Monakhov 6447ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford 6448ac27a0ecSDave Kleikamp * acquiring the locks... As quota files are never truncated and quota code 6449ac27a0ecSDave Kleikamp * itself serializes the operations (and no one else should touch the files) 6450ac27a0ecSDave Kleikamp * we don't have to be afraid of races */ 6451617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data, 6452ac27a0ecSDave Kleikamp size_t len, loff_t off) 6453ac27a0ecSDave Kleikamp { 6454ac27a0ecSDave Kleikamp struct inode *inode = sb_dqopt(sb)->files[type]; 6455725d26d3SAneesh Kumar K.V ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb); 6456ac27a0ecSDave Kleikamp int offset = off & (sb->s_blocksize - 1); 6457ac27a0ecSDave Kleikamp int tocopy; 6458ac27a0ecSDave Kleikamp size_t toread; 6459ac27a0ecSDave Kleikamp struct buffer_head *bh; 6460ac27a0ecSDave Kleikamp loff_t i_size = i_size_read(inode); 6461ac27a0ecSDave Kleikamp 6462ac27a0ecSDave Kleikamp if (off > i_size) 6463ac27a0ecSDave Kleikamp return 0; 6464ac27a0ecSDave Kleikamp if (off+len > i_size) 6465ac27a0ecSDave Kleikamp len = i_size-off; 6466ac27a0ecSDave Kleikamp toread = len; 6467ac27a0ecSDave Kleikamp while (toread > 0) { 6468ac27a0ecSDave Kleikamp tocopy = sb->s_blocksize - offset < toread ? 6469ac27a0ecSDave Kleikamp sb->s_blocksize - offset : toread; 64701c215028STheodore Ts'o bh = ext4_bread(NULL, inode, blk, 0); 64711c215028STheodore Ts'o if (IS_ERR(bh)) 64721c215028STheodore Ts'o return PTR_ERR(bh); 6473ac27a0ecSDave Kleikamp if (!bh) /* A hole? */ 6474ac27a0ecSDave Kleikamp memset(data, 0, tocopy); 6475ac27a0ecSDave Kleikamp else 6476ac27a0ecSDave Kleikamp memcpy(data, bh->b_data+offset, tocopy); 6477ac27a0ecSDave Kleikamp brelse(bh); 6478ac27a0ecSDave Kleikamp offset = 0; 6479ac27a0ecSDave Kleikamp toread -= tocopy; 6480ac27a0ecSDave Kleikamp data += tocopy; 6481ac27a0ecSDave Kleikamp blk++; 6482ac27a0ecSDave Kleikamp } 6483ac27a0ecSDave Kleikamp return len; 6484ac27a0ecSDave Kleikamp } 6485ac27a0ecSDave Kleikamp 6486ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has 6487ac27a0ecSDave Kleikamp * enough credits) */ 6488617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type, 6489ac27a0ecSDave Kleikamp const char *data, size_t len, loff_t off) 6490ac27a0ecSDave Kleikamp { 6491ac27a0ecSDave Kleikamp struct inode *inode = sb_dqopt(sb)->files[type]; 6492725d26d3SAneesh Kumar K.V ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb); 64934209ae12SHarshad Shirwadkar int err = 0, err2 = 0, offset = off & (sb->s_blocksize - 1); 6494c5e298aeSTheodore Ts'o int retries = 0; 6495ac27a0ecSDave Kleikamp struct buffer_head *bh; 6496ac27a0ecSDave Kleikamp handle_t *handle = journal_current_handle(); 6497ac27a0ecSDave Kleikamp 64980390131bSFrank Mayhar if (EXT4_SB(sb)->s_journal && !handle) { 6499b31e1552SEric Sandeen ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)" 6500b31e1552SEric Sandeen " cancelled because transaction is not started", 65019c3013e9SJan Kara (unsigned long long)off, (unsigned long long)len); 65029c3013e9SJan Kara return -EIO; 65039c3013e9SJan Kara } 650467eeb568SDmitry Monakhov /* 650567eeb568SDmitry Monakhov * Since we account only one data block in transaction credits, 650667eeb568SDmitry Monakhov * then it is impossible to cross a block boundary. 650767eeb568SDmitry Monakhov */ 650867eeb568SDmitry Monakhov if (sb->s_blocksize - offset < len) { 650967eeb568SDmitry Monakhov ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)" 651067eeb568SDmitry Monakhov " cancelled because not block aligned", 651167eeb568SDmitry Monakhov (unsigned long long)off, (unsigned long long)len); 651267eeb568SDmitry Monakhov return -EIO; 651367eeb568SDmitry Monakhov } 651467eeb568SDmitry Monakhov 6515c5e298aeSTheodore Ts'o do { 6516c5e298aeSTheodore Ts'o bh = ext4_bread(handle, inode, blk, 6517c5e298aeSTheodore Ts'o EXT4_GET_BLOCKS_CREATE | 6518c5e298aeSTheodore Ts'o EXT4_GET_BLOCKS_METADATA_NOFAIL); 651945586c70SMasahiro Yamada } while (PTR_ERR(bh) == -ENOSPC && 6520c5e298aeSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)); 65211c215028STheodore Ts'o if (IS_ERR(bh)) 65221c215028STheodore Ts'o return PTR_ERR(bh); 6523ac27a0ecSDave Kleikamp if (!bh) 6524ac27a0ecSDave Kleikamp goto out; 65255d601255Sliang xie BUFFER_TRACE(bh, "get write access"); 6526617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, bh); 6527ac27a0ecSDave Kleikamp if (err) { 6528ac27a0ecSDave Kleikamp brelse(bh); 65291c215028STheodore Ts'o return err; 6530ac27a0ecSDave Kleikamp } 6531ac27a0ecSDave Kleikamp lock_buffer(bh); 653267eeb568SDmitry Monakhov memcpy(bh->b_data+offset, data, len); 6533ac27a0ecSDave Kleikamp flush_dcache_page(bh->b_page); 6534ac27a0ecSDave Kleikamp unlock_buffer(bh); 65350390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, NULL, bh); 6536ac27a0ecSDave Kleikamp brelse(bh); 6537ac27a0ecSDave Kleikamp out: 653867eeb568SDmitry Monakhov if (inode->i_size < off + len) { 6539*aa75f4d3SHarshad Shirwadkar ext4_fc_track_range(inode, 6540*aa75f4d3SHarshad Shirwadkar (inode->i_size > 0 ? inode->i_size - 1 : 0) 6541*aa75f4d3SHarshad Shirwadkar >> inode->i_sb->s_blocksize_bits, 6542*aa75f4d3SHarshad Shirwadkar (off + len) >> inode->i_sb->s_blocksize_bits); 654367eeb568SDmitry Monakhov i_size_write(inode, off + len); 6544617ba13bSMingming Cao EXT4_I(inode)->i_disksize = inode->i_size; 65454209ae12SHarshad Shirwadkar err2 = ext4_mark_inode_dirty(handle, inode); 65464209ae12SHarshad Shirwadkar if (unlikely(err2 && !err)) 65474209ae12SHarshad Shirwadkar err = err2; 654821f97697SJan Kara } 65494209ae12SHarshad Shirwadkar return err ? err : len; 6550ac27a0ecSDave Kleikamp } 6551ac27a0ecSDave Kleikamp #endif 6552ac27a0ecSDave Kleikamp 6553152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags, 6554152a0836SAl Viro const char *dev_name, void *data) 6555ac27a0ecSDave Kleikamp { 6556152a0836SAl Viro return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super); 6557ac27a0ecSDave Kleikamp } 6558ac27a0ecSDave Kleikamp 6559c290ea01SJan Kara #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2) 656024b58424STheodore Ts'o static inline void register_as_ext2(void) 656124b58424STheodore Ts'o { 656224b58424STheodore Ts'o int err = register_filesystem(&ext2_fs_type); 656324b58424STheodore Ts'o if (err) 656424b58424STheodore Ts'o printk(KERN_WARNING 656524b58424STheodore Ts'o "EXT4-fs: Unable to register as ext2 (%d)\n", err); 656624b58424STheodore Ts'o } 656724b58424STheodore Ts'o 656824b58424STheodore Ts'o static inline void unregister_as_ext2(void) 656924b58424STheodore Ts'o { 657024b58424STheodore Ts'o unregister_filesystem(&ext2_fs_type); 657124b58424STheodore Ts'o } 65722035e776STheodore Ts'o 65732035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb) 65742035e776STheodore Ts'o { 6575e2b911c5SDarrick J. Wong if (ext4_has_unknown_ext2_incompat_features(sb)) 65762035e776STheodore Ts'o return 0; 6577bc98a42cSDavid Howells if (sb_rdonly(sb)) 65782035e776STheodore Ts'o return 1; 6579e2b911c5SDarrick J. Wong if (ext4_has_unknown_ext2_ro_compat_features(sb)) 65802035e776STheodore Ts'o return 0; 65812035e776STheodore Ts'o return 1; 65822035e776STheodore Ts'o } 658324b58424STheodore Ts'o #else 658424b58424STheodore Ts'o static inline void register_as_ext2(void) { } 658524b58424STheodore Ts'o static inline void unregister_as_ext2(void) { } 65862035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; } 658724b58424STheodore Ts'o #endif 658824b58424STheodore Ts'o 658924b58424STheodore Ts'o static inline void register_as_ext3(void) 659024b58424STheodore Ts'o { 659124b58424STheodore Ts'o int err = register_filesystem(&ext3_fs_type); 659224b58424STheodore Ts'o if (err) 659324b58424STheodore Ts'o printk(KERN_WARNING 659424b58424STheodore Ts'o "EXT4-fs: Unable to register as ext3 (%d)\n", err); 659524b58424STheodore Ts'o } 659624b58424STheodore Ts'o 659724b58424STheodore Ts'o static inline void unregister_as_ext3(void) 659824b58424STheodore Ts'o { 659924b58424STheodore Ts'o unregister_filesystem(&ext3_fs_type); 660024b58424STheodore Ts'o } 66012035e776STheodore Ts'o 66022035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb) 66032035e776STheodore Ts'o { 6604e2b911c5SDarrick J. Wong if (ext4_has_unknown_ext3_incompat_features(sb)) 66052035e776STheodore Ts'o return 0; 6606e2b911c5SDarrick J. Wong if (!ext4_has_feature_journal(sb)) 66072035e776STheodore Ts'o return 0; 6608bc98a42cSDavid Howells if (sb_rdonly(sb)) 66092035e776STheodore Ts'o return 1; 6610e2b911c5SDarrick J. Wong if (ext4_has_unknown_ext3_ro_compat_features(sb)) 66112035e776STheodore Ts'o return 0; 66122035e776STheodore Ts'o return 1; 66132035e776STheodore Ts'o } 661424b58424STheodore Ts'o 661503010a33STheodore Ts'o static struct file_system_type ext4_fs_type = { 6616ac27a0ecSDave Kleikamp .owner = THIS_MODULE, 661703010a33STheodore Ts'o .name = "ext4", 6618152a0836SAl Viro .mount = ext4_mount, 6619ac27a0ecSDave Kleikamp .kill_sb = kill_block_super, 6620ac27a0ecSDave Kleikamp .fs_flags = FS_REQUIRES_DEV, 6621ac27a0ecSDave Kleikamp }; 66227f78e035SEric W. Biederman MODULE_ALIAS_FS("ext4"); 6623ac27a0ecSDave Kleikamp 6624e9e3bcecSEric Sandeen /* Shared across all ext4 file systems */ 6625e9e3bcecSEric Sandeen wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ]; 6626e9e3bcecSEric Sandeen 66275dabfc78STheodore Ts'o static int __init ext4_init_fs(void) 6628ac27a0ecSDave Kleikamp { 6629e9e3bcecSEric Sandeen int i, err; 6630c9de560dSAlex Tomas 6631e294a537STheodore Ts'o ratelimit_state_init(&ext4_mount_msg_ratelimit, 30 * HZ, 64); 663207c0c5d8SAl Viro ext4_li_info = NULL; 663307c0c5d8SAl Viro mutex_init(&ext4_li_mtx); 663407c0c5d8SAl Viro 66359a4c8019SCarlos Maiolino /* Build-time check for flags consistency */ 663612e9b892SDmitry Monakhov ext4_check_flag_values(); 6637e9e3bcecSEric Sandeen 6638e142d052SJan Kara for (i = 0; i < EXT4_WQ_HASH_SZ; i++) 6639e9e3bcecSEric Sandeen init_waitqueue_head(&ext4__ioend_wq[i]); 6640e9e3bcecSEric Sandeen 664151865fdaSZheng Liu err = ext4_init_es(); 66426fd058f7STheodore Ts'o if (err) 66436fd058f7STheodore Ts'o return err; 664451865fdaSZheng Liu 66451dc0aa46SEric Whitney err = ext4_init_pending(); 66461dc0aa46SEric Whitney if (err) 664722cfe4b4SEric Biggers goto out7; 664822cfe4b4SEric Biggers 664922cfe4b4SEric Biggers err = ext4_init_post_read_processing(); 665022cfe4b4SEric Biggers if (err) 66511dc0aa46SEric Whitney goto out6; 66521dc0aa46SEric Whitney 665351865fdaSZheng Liu err = ext4_init_pageio(); 665451865fdaSZheng Liu if (err) 6655b5799018STheodore Ts'o goto out5; 665651865fdaSZheng Liu 66575dabfc78STheodore Ts'o err = ext4_init_system_zone(); 6658bd2d0210STheodore Ts'o if (err) 6659b5799018STheodore Ts'o goto out4; 6660857ac889SLukas Czerner 6661b5799018STheodore Ts'o err = ext4_init_sysfs(); 6662dd68314cSTheodore Ts'o if (err) 6663b5799018STheodore Ts'o goto out3; 6664857ac889SLukas Czerner 66655dabfc78STheodore Ts'o err = ext4_init_mballoc(); 6666ac27a0ecSDave Kleikamp if (err) 6667c9de560dSAlex Tomas goto out2; 6668ac27a0ecSDave Kleikamp err = init_inodecache(); 6669ac27a0ecSDave Kleikamp if (err) 6670ac27a0ecSDave Kleikamp goto out1; 6671*aa75f4d3SHarshad Shirwadkar 6672*aa75f4d3SHarshad Shirwadkar err = ext4_fc_init_dentry_cache(); 6673*aa75f4d3SHarshad Shirwadkar if (err) 6674*aa75f4d3SHarshad Shirwadkar goto out05; 6675*aa75f4d3SHarshad Shirwadkar 667624b58424STheodore Ts'o register_as_ext3(); 66772035e776STheodore Ts'o register_as_ext2(); 667803010a33STheodore Ts'o err = register_filesystem(&ext4_fs_type); 6679ac27a0ecSDave Kleikamp if (err) 6680ac27a0ecSDave Kleikamp goto out; 6681bfff6873SLukas Czerner 6682ac27a0ecSDave Kleikamp return 0; 6683ac27a0ecSDave Kleikamp out: 668424b58424STheodore Ts'o unregister_as_ext2(); 668524b58424STheodore Ts'o unregister_as_ext3(); 6686*aa75f4d3SHarshad Shirwadkar out05: 6687ac27a0ecSDave Kleikamp destroy_inodecache(); 6688ac27a0ecSDave Kleikamp out1: 66895dabfc78STheodore Ts'o ext4_exit_mballoc(); 66909c191f70ST Makphaibulchoke out2: 6691b5799018STheodore Ts'o ext4_exit_sysfs(); 6692b5799018STheodore Ts'o out3: 6693dd68314cSTheodore Ts'o ext4_exit_system_zone(); 6694b5799018STheodore Ts'o out4: 66955dabfc78STheodore Ts'o ext4_exit_pageio(); 6696b5799018STheodore Ts'o out5: 669722cfe4b4SEric Biggers ext4_exit_post_read_processing(); 66981dc0aa46SEric Whitney out6: 669922cfe4b4SEric Biggers ext4_exit_pending(); 670022cfe4b4SEric Biggers out7: 670151865fdaSZheng Liu ext4_exit_es(); 670251865fdaSZheng Liu 6703ac27a0ecSDave Kleikamp return err; 6704ac27a0ecSDave Kleikamp } 6705ac27a0ecSDave Kleikamp 67065dabfc78STheodore Ts'o static void __exit ext4_exit_fs(void) 6707ac27a0ecSDave Kleikamp { 6708bfff6873SLukas Czerner ext4_destroy_lazyinit_thread(); 670924b58424STheodore Ts'o unregister_as_ext2(); 671024b58424STheodore Ts'o unregister_as_ext3(); 671103010a33STheodore Ts'o unregister_filesystem(&ext4_fs_type); 6712ac27a0ecSDave Kleikamp destroy_inodecache(); 67135dabfc78STheodore Ts'o ext4_exit_mballoc(); 6714b5799018STheodore Ts'o ext4_exit_sysfs(); 67155dabfc78STheodore Ts'o ext4_exit_system_zone(); 67165dabfc78STheodore Ts'o ext4_exit_pageio(); 671722cfe4b4SEric Biggers ext4_exit_post_read_processing(); 6718dd12ed14SEric Sandeen ext4_exit_es(); 67191dc0aa46SEric Whitney ext4_exit_pending(); 6720ac27a0ecSDave Kleikamp } 6721ac27a0ecSDave Kleikamp 6722ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others"); 672383982b6fSTheodore Ts'o MODULE_DESCRIPTION("Fourth Extended Filesystem"); 6724ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL"); 67257ef79ad5STheodore Ts'o MODULE_SOFTDEP("pre: crc32c"); 67265dabfc78STheodore Ts'o module_init(ext4_init_fs) 67275dabfc78STheodore Ts'o module_exit(ext4_exit_fs) 6728