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> 46ac27a0ecSDave Kleikamp 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); 69617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb, 70617ba13bSMingming Cao struct ext4_super_block *es); 71617ba13bSMingming Cao static void 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: 96e74031fdSJan Kara * mmap_sem -> sb_start_pagefault -> i_mmap_sem (r) -> transaction start -> 97e74031fdSJan Kara * page lock -> i_data_sem (rw) 98e74031fdSJan Kara * 99e74031fdSJan Kara * buffered write path: 100e74031fdSJan Kara * sb_start_write -> i_mutex -> mmap_sem 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: 1101d39834fSNikolay Borisov * sb_start_write -> i_mutex -> mmap_sem 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 144fb265c9cSTheodore Ts'o /* 145fb265c9cSTheodore Ts'o * This works like sb_bread() except it uses ERR_PTR for error 146fb265c9cSTheodore Ts'o * returns. Currently with sb_bread it's impossible to distinguish 147fb265c9cSTheodore Ts'o * between ENOMEM and EIO situations (since both result in a NULL 148fb265c9cSTheodore Ts'o * return. 149fb265c9cSTheodore Ts'o */ 150fb265c9cSTheodore Ts'o struct buffer_head * 151fb265c9cSTheodore Ts'o ext4_sb_bread(struct super_block *sb, sector_t block, int op_flags) 152fb265c9cSTheodore Ts'o { 153fb265c9cSTheodore Ts'o struct buffer_head *bh = sb_getblk(sb, block); 154fb265c9cSTheodore Ts'o 155fb265c9cSTheodore Ts'o if (bh == NULL) 156fb265c9cSTheodore Ts'o return ERR_PTR(-ENOMEM); 157fb265c9cSTheodore Ts'o if (buffer_uptodate(bh)) 158fb265c9cSTheodore Ts'o return bh; 159fb265c9cSTheodore Ts'o ll_rw_block(REQ_OP_READ, REQ_META | op_flags, 1, &bh); 160fb265c9cSTheodore Ts'o wait_on_buffer(bh); 161fb265c9cSTheodore Ts'o if (buffer_uptodate(bh)) 162fb265c9cSTheodore Ts'o return bh; 163fb265c9cSTheodore Ts'o put_bh(bh); 164fb265c9cSTheodore Ts'o return ERR_PTR(-EIO); 165fb265c9cSTheodore Ts'o } 166fb265c9cSTheodore Ts'o 167d25425f8SDarrick J. Wong static int ext4_verify_csum_type(struct super_block *sb, 168d25425f8SDarrick J. Wong struct ext4_super_block *es) 169d25425f8SDarrick J. Wong { 170e2b911c5SDarrick J. Wong if (!ext4_has_feature_metadata_csum(sb)) 171d25425f8SDarrick J. Wong return 1; 172d25425f8SDarrick J. Wong 173d25425f8SDarrick J. Wong return es->s_checksum_type == EXT4_CRC32C_CHKSUM; 174d25425f8SDarrick J. Wong } 175d25425f8SDarrick J. Wong 176a9c47317SDarrick J. Wong static __le32 ext4_superblock_csum(struct super_block *sb, 177a9c47317SDarrick J. Wong struct ext4_super_block *es) 178a9c47317SDarrick J. Wong { 179a9c47317SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(sb); 180a9c47317SDarrick J. Wong int offset = offsetof(struct ext4_super_block, s_checksum); 181a9c47317SDarrick J. Wong __u32 csum; 182a9c47317SDarrick J. Wong 183a9c47317SDarrick J. Wong csum = ext4_chksum(sbi, ~0, (char *)es, offset); 184a9c47317SDarrick J. Wong 185a9c47317SDarrick J. Wong return cpu_to_le32(csum); 186a9c47317SDarrick J. Wong } 187a9c47317SDarrick J. Wong 188c197855eSStephen Hemminger static int ext4_superblock_csum_verify(struct super_block *sb, 189a9c47317SDarrick J. Wong struct ext4_super_block *es) 190a9c47317SDarrick J. Wong { 1919aa5d32bSDmitry Monakhov if (!ext4_has_metadata_csum(sb)) 192a9c47317SDarrick J. Wong return 1; 193a9c47317SDarrick J. Wong 194a9c47317SDarrick J. Wong return es->s_checksum == ext4_superblock_csum(sb, es); 195a9c47317SDarrick J. Wong } 196a9c47317SDarrick J. Wong 19706db49e6STheodore Ts'o void ext4_superblock_csum_set(struct super_block *sb) 198a9c47317SDarrick J. Wong { 19906db49e6STheodore Ts'o struct ext4_super_block *es = EXT4_SB(sb)->s_es; 20006db49e6STheodore Ts'o 2019aa5d32bSDmitry Monakhov if (!ext4_has_metadata_csum(sb)) 202a9c47317SDarrick J. Wong return; 203a9c47317SDarrick J. Wong 204a9c47317SDarrick J. Wong es->s_checksum = ext4_superblock_csum(sb, es); 205a9c47317SDarrick J. Wong } 206a9c47317SDarrick J. Wong 2079933fc0aSTheodore Ts'o void *ext4_kvmalloc(size_t size, gfp_t flags) 2089933fc0aSTheodore Ts'o { 2099933fc0aSTheodore Ts'o void *ret; 2109933fc0aSTheodore Ts'o 2118be04b93SJoe Perches ret = kmalloc(size, flags | __GFP_NOWARN); 2129933fc0aSTheodore Ts'o if (!ret) 2139933fc0aSTheodore Ts'o ret = __vmalloc(size, flags, PAGE_KERNEL); 2149933fc0aSTheodore Ts'o return ret; 2159933fc0aSTheodore Ts'o } 2169933fc0aSTheodore Ts'o 2179933fc0aSTheodore Ts'o void *ext4_kvzalloc(size_t size, gfp_t flags) 2189933fc0aSTheodore Ts'o { 2199933fc0aSTheodore Ts'o void *ret; 2209933fc0aSTheodore Ts'o 2218be04b93SJoe Perches ret = kzalloc(size, flags | __GFP_NOWARN); 2229933fc0aSTheodore Ts'o if (!ret) 2239933fc0aSTheodore Ts'o ret = __vmalloc(size, flags | __GFP_ZERO, PAGE_KERNEL); 2249933fc0aSTheodore Ts'o return ret; 2259933fc0aSTheodore Ts'o } 2269933fc0aSTheodore Ts'o 2278fadc143SAlexandre Ratchov ext4_fsblk_t ext4_block_bitmap(struct super_block *sb, 2288fadc143SAlexandre Ratchov struct ext4_group_desc *bg) 229bd81d8eeSLaurent Vivier { 2303a14589cSAneesh Kumar K.V return le32_to_cpu(bg->bg_block_bitmap_lo) | 2318fadc143SAlexandre Ratchov (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? 2328fadc143SAlexandre Ratchov (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0); 233bd81d8eeSLaurent Vivier } 234bd81d8eeSLaurent Vivier 2358fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb, 2368fadc143SAlexandre Ratchov struct ext4_group_desc *bg) 237bd81d8eeSLaurent Vivier { 2385272f837SAneesh Kumar K.V return le32_to_cpu(bg->bg_inode_bitmap_lo) | 2398fadc143SAlexandre Ratchov (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? 2408fadc143SAlexandre Ratchov (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0); 241bd81d8eeSLaurent Vivier } 242bd81d8eeSLaurent Vivier 2438fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_table(struct super_block *sb, 2448fadc143SAlexandre Ratchov struct ext4_group_desc *bg) 245bd81d8eeSLaurent Vivier { 2465272f837SAneesh Kumar K.V return le32_to_cpu(bg->bg_inode_table_lo) | 2478fadc143SAlexandre Ratchov (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? 2488fadc143SAlexandre Ratchov (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0); 249bd81d8eeSLaurent Vivier } 250bd81d8eeSLaurent Vivier 251021b65bbSTheodore Ts'o __u32 ext4_free_group_clusters(struct super_block *sb, 252560671a0SAneesh Kumar K.V struct ext4_group_desc *bg) 253560671a0SAneesh Kumar K.V { 254560671a0SAneesh Kumar K.V return le16_to_cpu(bg->bg_free_blocks_count_lo) | 255560671a0SAneesh Kumar K.V (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? 256560671a0SAneesh Kumar K.V (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0); 257560671a0SAneesh Kumar K.V } 258560671a0SAneesh Kumar K.V 259560671a0SAneesh Kumar K.V __u32 ext4_free_inodes_count(struct super_block *sb, 260560671a0SAneesh Kumar K.V struct ext4_group_desc *bg) 261560671a0SAneesh Kumar K.V { 262560671a0SAneesh Kumar K.V return le16_to_cpu(bg->bg_free_inodes_count_lo) | 263560671a0SAneesh Kumar K.V (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? 264560671a0SAneesh Kumar K.V (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0); 265560671a0SAneesh Kumar K.V } 266560671a0SAneesh Kumar K.V 267560671a0SAneesh Kumar K.V __u32 ext4_used_dirs_count(struct super_block *sb, 268560671a0SAneesh Kumar K.V struct ext4_group_desc *bg) 269560671a0SAneesh Kumar K.V { 270560671a0SAneesh Kumar K.V return le16_to_cpu(bg->bg_used_dirs_count_lo) | 271560671a0SAneesh Kumar K.V (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? 272560671a0SAneesh Kumar K.V (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0); 273560671a0SAneesh Kumar K.V } 274560671a0SAneesh Kumar K.V 275560671a0SAneesh Kumar K.V __u32 ext4_itable_unused_count(struct super_block *sb, 276560671a0SAneesh Kumar K.V struct ext4_group_desc *bg) 277560671a0SAneesh Kumar K.V { 278560671a0SAneesh Kumar K.V return le16_to_cpu(bg->bg_itable_unused_lo) | 279560671a0SAneesh Kumar K.V (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? 280560671a0SAneesh Kumar K.V (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0); 281560671a0SAneesh Kumar K.V } 282560671a0SAneesh Kumar K.V 2838fadc143SAlexandre Ratchov void ext4_block_bitmap_set(struct super_block *sb, 2848fadc143SAlexandre Ratchov struct ext4_group_desc *bg, ext4_fsblk_t blk) 285bd81d8eeSLaurent Vivier { 2863a14589cSAneesh Kumar K.V bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk); 2878fadc143SAlexandre Ratchov if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) 2888fadc143SAlexandre Ratchov bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32); 289bd81d8eeSLaurent Vivier } 290bd81d8eeSLaurent Vivier 2918fadc143SAlexandre Ratchov void ext4_inode_bitmap_set(struct super_block *sb, 2928fadc143SAlexandre Ratchov struct ext4_group_desc *bg, ext4_fsblk_t blk) 293bd81d8eeSLaurent Vivier { 2945272f837SAneesh Kumar K.V bg->bg_inode_bitmap_lo = cpu_to_le32((u32)blk); 2958fadc143SAlexandre Ratchov if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) 2968fadc143SAlexandre Ratchov bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32); 297bd81d8eeSLaurent Vivier } 298bd81d8eeSLaurent Vivier 2998fadc143SAlexandre Ratchov void ext4_inode_table_set(struct super_block *sb, 3008fadc143SAlexandre Ratchov struct ext4_group_desc *bg, ext4_fsblk_t blk) 301bd81d8eeSLaurent Vivier { 3025272f837SAneesh Kumar K.V bg->bg_inode_table_lo = cpu_to_le32((u32)blk); 3038fadc143SAlexandre Ratchov if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) 3048fadc143SAlexandre Ratchov bg->bg_inode_table_hi = cpu_to_le32(blk >> 32); 305bd81d8eeSLaurent Vivier } 306bd81d8eeSLaurent Vivier 307021b65bbSTheodore Ts'o void ext4_free_group_clusters_set(struct super_block *sb, 308560671a0SAneesh Kumar K.V struct ext4_group_desc *bg, __u32 count) 309560671a0SAneesh Kumar K.V { 310560671a0SAneesh Kumar K.V bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count); 311560671a0SAneesh Kumar K.V if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) 312560671a0SAneesh Kumar K.V bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16); 313560671a0SAneesh Kumar K.V } 314560671a0SAneesh Kumar K.V 315560671a0SAneesh Kumar K.V void ext4_free_inodes_set(struct super_block *sb, 316560671a0SAneesh Kumar K.V struct ext4_group_desc *bg, __u32 count) 317560671a0SAneesh Kumar K.V { 318560671a0SAneesh Kumar K.V bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count); 319560671a0SAneesh Kumar K.V if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) 320560671a0SAneesh Kumar K.V bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16); 321560671a0SAneesh Kumar K.V } 322560671a0SAneesh Kumar K.V 323560671a0SAneesh Kumar K.V void ext4_used_dirs_set(struct super_block *sb, 324560671a0SAneesh Kumar K.V struct ext4_group_desc *bg, __u32 count) 325560671a0SAneesh Kumar K.V { 326560671a0SAneesh Kumar K.V bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count); 327560671a0SAneesh Kumar K.V if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) 328560671a0SAneesh Kumar K.V bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16); 329560671a0SAneesh Kumar K.V } 330560671a0SAneesh Kumar K.V 331560671a0SAneesh Kumar K.V void ext4_itable_unused_set(struct super_block *sb, 332560671a0SAneesh Kumar K.V struct ext4_group_desc *bg, __u32 count) 333560671a0SAneesh Kumar K.V { 334560671a0SAneesh Kumar K.V bg->bg_itable_unused_lo = cpu_to_le16((__u16)count); 335560671a0SAneesh Kumar K.V if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) 336560671a0SAneesh Kumar K.V bg->bg_itable_unused_hi = cpu_to_le16(count >> 16); 337560671a0SAneesh Kumar K.V } 338560671a0SAneesh Kumar K.V 3396a0678a7SArnd Bergmann static void __ext4_update_tstamp(__le32 *lo, __u8 *hi) 3406a0678a7SArnd Bergmann { 3416a0678a7SArnd Bergmann time64_t now = ktime_get_real_seconds(); 3426a0678a7SArnd Bergmann 3436a0678a7SArnd Bergmann now = clamp_val(now, 0, (1ull << 40) - 1); 3446a0678a7SArnd Bergmann 3456a0678a7SArnd Bergmann *lo = cpu_to_le32(lower_32_bits(now)); 3466a0678a7SArnd Bergmann *hi = upper_32_bits(now); 3476a0678a7SArnd Bergmann } 3486a0678a7SArnd Bergmann 3496a0678a7SArnd Bergmann static time64_t __ext4_get_tstamp(__le32 *lo, __u8 *hi) 3506a0678a7SArnd Bergmann { 3516a0678a7SArnd Bergmann return ((time64_t)(*hi) << 32) + le32_to_cpu(*lo); 3526a0678a7SArnd Bergmann } 3536a0678a7SArnd Bergmann #define ext4_update_tstamp(es, tstamp) \ 3546a0678a7SArnd Bergmann __ext4_update_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi) 3556a0678a7SArnd Bergmann #define ext4_get_tstamp(es, tstamp) \ 3566a0678a7SArnd Bergmann __ext4_get_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi) 357d3d1faf6SCurt Wohlgemuth 3581c13d5c0STheodore Ts'o static void __save_error_info(struct super_block *sb, const char *func, 3591c13d5c0STheodore Ts'o unsigned int line) 3601c13d5c0STheodore Ts'o { 3611c13d5c0STheodore Ts'o struct ext4_super_block *es = EXT4_SB(sb)->s_es; 3621c13d5c0STheodore Ts'o 3631c13d5c0STheodore Ts'o EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS; 3641b46617bSTheodore Ts'o if (bdev_read_only(sb->s_bdev)) 3651b46617bSTheodore Ts'o return; 3661c13d5c0STheodore Ts'o es->s_state |= cpu_to_le16(EXT4_ERROR_FS); 3676a0678a7SArnd Bergmann ext4_update_tstamp(es, s_last_error_time); 3681c13d5c0STheodore Ts'o strncpy(es->s_last_error_func, func, sizeof(es->s_last_error_func)); 3691c13d5c0STheodore Ts'o es->s_last_error_line = cpu_to_le32(line); 3701c13d5c0STheodore Ts'o if (!es->s_first_error_time) { 3711c13d5c0STheodore Ts'o es->s_first_error_time = es->s_last_error_time; 3726a0678a7SArnd Bergmann es->s_first_error_time_hi = es->s_last_error_time_hi; 3731c13d5c0STheodore Ts'o strncpy(es->s_first_error_func, func, 3741c13d5c0STheodore Ts'o sizeof(es->s_first_error_func)); 3751c13d5c0STheodore Ts'o es->s_first_error_line = cpu_to_le32(line); 3761c13d5c0STheodore Ts'o es->s_first_error_ino = es->s_last_error_ino; 3771c13d5c0STheodore Ts'o es->s_first_error_block = es->s_last_error_block; 3781c13d5c0STheodore Ts'o } 37966e61a9eSTheodore Ts'o /* 38066e61a9eSTheodore Ts'o * Start the daily error reporting function if it hasn't been 38166e61a9eSTheodore Ts'o * started already 38266e61a9eSTheodore Ts'o */ 38366e61a9eSTheodore Ts'o if (!es->s_error_count) 38466e61a9eSTheodore Ts'o mod_timer(&EXT4_SB(sb)->s_err_report, jiffies + 24*60*60*HZ); 385ba39ebb6SWei Yongjun le32_add_cpu(&es->s_error_count, 1); 3861c13d5c0STheodore Ts'o } 3871c13d5c0STheodore Ts'o 3881c13d5c0STheodore Ts'o static void save_error_info(struct super_block *sb, const char *func, 3891c13d5c0STheodore Ts'o unsigned int line) 3901c13d5c0STheodore Ts'o { 3911c13d5c0STheodore Ts'o __save_error_info(sb, func, line); 3921c13d5c0STheodore Ts'o ext4_commit_super(sb, 1); 3931c13d5c0STheodore Ts'o } 3941c13d5c0STheodore Ts'o 395bdfe0cbdSTheodore Ts'o /* 396bdfe0cbdSTheodore Ts'o * The del_gendisk() function uninitializes the disk-specific data 397bdfe0cbdSTheodore Ts'o * structures, including the bdi structure, without telling anyone 398bdfe0cbdSTheodore Ts'o * else. Once this happens, any attempt to call mark_buffer_dirty() 399bdfe0cbdSTheodore Ts'o * (for example, by ext4_commit_super), will cause a kernel OOPS. 400bdfe0cbdSTheodore Ts'o * This is a kludge to prevent these oops until we can put in a proper 401bdfe0cbdSTheodore Ts'o * hook in del_gendisk() to inform the VFS and file system layers. 402bdfe0cbdSTheodore Ts'o */ 403bdfe0cbdSTheodore Ts'o static int block_device_ejected(struct super_block *sb) 404bdfe0cbdSTheodore Ts'o { 405bdfe0cbdSTheodore Ts'o struct inode *bd_inode = sb->s_bdev->bd_inode; 406bdfe0cbdSTheodore Ts'o struct backing_dev_info *bdi = inode_to_bdi(bd_inode); 407bdfe0cbdSTheodore Ts'o 408bdfe0cbdSTheodore Ts'o return bdi->dev == NULL; 409bdfe0cbdSTheodore Ts'o } 410bdfe0cbdSTheodore Ts'o 41118aadd47SBobi Jam static void ext4_journal_commit_callback(journal_t *journal, transaction_t *txn) 41218aadd47SBobi Jam { 41318aadd47SBobi Jam struct super_block *sb = journal->j_private; 41418aadd47SBobi Jam struct ext4_sb_info *sbi = EXT4_SB(sb); 41518aadd47SBobi Jam int error = is_journal_aborted(journal); 4165d3ee208SDmitry Monakhov struct ext4_journal_cb_entry *jce; 41718aadd47SBobi Jam 4185d3ee208SDmitry Monakhov BUG_ON(txn->t_state == T_FINISHED); 419a0154344SDaeho Jeong 420a0154344SDaeho Jeong ext4_process_freed_data(sb, txn->t_tid); 421a0154344SDaeho Jeong 42218aadd47SBobi Jam spin_lock(&sbi->s_md_lock); 4235d3ee208SDmitry Monakhov while (!list_empty(&txn->t_private_list)) { 4245d3ee208SDmitry Monakhov jce = list_entry(txn->t_private_list.next, 4255d3ee208SDmitry Monakhov struct ext4_journal_cb_entry, jce_list); 42618aadd47SBobi Jam list_del_init(&jce->jce_list); 42718aadd47SBobi Jam spin_unlock(&sbi->s_md_lock); 42818aadd47SBobi Jam jce->jce_func(sb, jce, error); 42918aadd47SBobi Jam spin_lock(&sbi->s_md_lock); 43018aadd47SBobi Jam } 43118aadd47SBobi Jam spin_unlock(&sbi->s_md_lock); 43218aadd47SBobi Jam } 4331c13d5c0STheodore Ts'o 4341dc1097fSJan Kara static bool system_going_down(void) 4351dc1097fSJan Kara { 4361dc1097fSJan Kara return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF 4371dc1097fSJan Kara || system_state == SYSTEM_RESTART; 4381dc1097fSJan Kara } 4391dc1097fSJan Kara 440ac27a0ecSDave Kleikamp /* Deal with the reporting of failure conditions on a filesystem such as 441ac27a0ecSDave Kleikamp * inconsistencies detected or read IO failures. 442ac27a0ecSDave Kleikamp * 443ac27a0ecSDave Kleikamp * On ext2, we can store the error state of the filesystem in the 444617ba13bSMingming Cao * superblock. That is not possible on ext4, because we may have other 445ac27a0ecSDave Kleikamp * write ordering constraints on the superblock which prevent us from 446ac27a0ecSDave Kleikamp * writing it out straight away; and given that the journal is about to 447ac27a0ecSDave Kleikamp * be aborted, we can't rely on the current, or future, transactions to 448ac27a0ecSDave Kleikamp * write out the superblock safely. 449ac27a0ecSDave Kleikamp * 450dab291afSMingming Cao * We'll just use the jbd2_journal_abort() error code to record an error in 451d6b198bcSThadeu Lima de Souza Cascardo * the journal instead. On recovery, the journal will complain about 452ac27a0ecSDave Kleikamp * that error until we've noted it down and cleared it. 453ac27a0ecSDave Kleikamp */ 454ac27a0ecSDave Kleikamp 455617ba13bSMingming Cao static void ext4_handle_error(struct super_block *sb) 456ac27a0ecSDave Kleikamp { 457327eaf73STheodore Ts'o if (test_opt(sb, WARN_ON_ERROR)) 458327eaf73STheodore Ts'o WARN_ON_ONCE(1); 459327eaf73STheodore Ts'o 460bc98a42cSDavid Howells if (sb_rdonly(sb)) 461ac27a0ecSDave Kleikamp return; 462ac27a0ecSDave Kleikamp 463ac27a0ecSDave Kleikamp if (!test_opt(sb, ERRORS_CONT)) { 464617ba13bSMingming Cao journal_t *journal = EXT4_SB(sb)->s_journal; 465ac27a0ecSDave Kleikamp 4664ab2f15bSTheodore Ts'o EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED; 467ac27a0ecSDave Kleikamp if (journal) 468dab291afSMingming Cao jbd2_journal_abort(journal, -EIO); 469ac27a0ecSDave Kleikamp } 4701dc1097fSJan Kara /* 4711dc1097fSJan Kara * We force ERRORS_RO behavior when system is rebooting. Otherwise we 4721dc1097fSJan Kara * could panic during 'reboot -f' as the underlying device got already 4731dc1097fSJan Kara * disabled. 4741dc1097fSJan Kara */ 4751dc1097fSJan Kara if (test_opt(sb, ERRORS_RO) || system_going_down()) { 476b31e1552SEric Sandeen ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only"); 4774418e141SDmitry Monakhov /* 4784418e141SDmitry Monakhov * Make sure updated value of ->s_mount_flags will be visible 4794418e141SDmitry Monakhov * before ->s_flags update 4804418e141SDmitry Monakhov */ 4814418e141SDmitry Monakhov smp_wmb(); 4821751e8a6SLinus Torvalds sb->s_flags |= SB_RDONLY; 4831dc1097fSJan Kara } else if (test_opt(sb, ERRORS_PANIC)) { 4844327ba52SDaeho Jeong if (EXT4_SB(sb)->s_journal && 4854327ba52SDaeho Jeong !(EXT4_SB(sb)->s_journal->j_flags & JBD2_REC_ERR)) 4864327ba52SDaeho Jeong return; 487617ba13bSMingming Cao panic("EXT4-fs (device %s): panic forced after error\n", 488ac27a0ecSDave Kleikamp sb->s_id); 489ac27a0ecSDave Kleikamp } 4904327ba52SDaeho Jeong } 491ac27a0ecSDave Kleikamp 492efbed4dcSTheodore Ts'o #define ext4_error_ratelimit(sb) \ 493efbed4dcSTheodore Ts'o ___ratelimit(&(EXT4_SB(sb)->s_err_ratelimit_state), \ 494efbed4dcSTheodore Ts'o "EXT4-fs error") 495efbed4dcSTheodore Ts'o 49612062dddSEric Sandeen void __ext4_error(struct super_block *sb, const char *function, 497c398eda0STheodore Ts'o unsigned int line, const char *fmt, ...) 498ac27a0ecSDave Kleikamp { 4990ff2ea7dSJoe Perches struct va_format vaf; 500ac27a0ecSDave Kleikamp va_list args; 501ac27a0ecSDave Kleikamp 5020db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(sb)))) 5030db1ff22STheodore Ts'o return; 5040db1ff22STheodore Ts'o 505ccf0f32aSTheodore Ts'o trace_ext4_error(sb, function, line); 506efbed4dcSTheodore Ts'o if (ext4_error_ratelimit(sb)) { 507ac27a0ecSDave Kleikamp va_start(args, fmt); 5080ff2ea7dSJoe Perches vaf.fmt = fmt; 5090ff2ea7dSJoe Perches vaf.va = &args; 510efbed4dcSTheodore Ts'o printk(KERN_CRIT 511efbed4dcSTheodore Ts'o "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n", 5120ff2ea7dSJoe Perches sb->s_id, function, line, current->comm, &vaf); 513ac27a0ecSDave Kleikamp va_end(args); 514efbed4dcSTheodore Ts'o } 515f3fc0210STheodore Ts'o save_error_info(sb, function, line); 516617ba13bSMingming Cao ext4_handle_error(sb); 517ac27a0ecSDave Kleikamp } 518ac27a0ecSDave Kleikamp 519e7c96e8eSJoe Perches void __ext4_error_inode(struct inode *inode, const char *function, 520c398eda0STheodore Ts'o unsigned int line, ext4_fsblk_t block, 521273df556SFrank Mayhar const char *fmt, ...) 522273df556SFrank Mayhar { 523273df556SFrank Mayhar va_list args; 524f7c21177STheodore Ts'o struct va_format vaf; 5251c13d5c0STheodore Ts'o struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es; 526273df556SFrank Mayhar 5270db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 5280db1ff22STheodore Ts'o return; 5290db1ff22STheodore Ts'o 530ccf0f32aSTheodore Ts'o trace_ext4_error(inode->i_sb, function, line); 5311c13d5c0STheodore Ts'o es->s_last_error_ino = cpu_to_le32(inode->i_ino); 5321c13d5c0STheodore Ts'o es->s_last_error_block = cpu_to_le64(block); 533efbed4dcSTheodore Ts'o if (ext4_error_ratelimit(inode->i_sb)) { 534273df556SFrank Mayhar va_start(args, fmt); 535f7c21177STheodore Ts'o vaf.fmt = fmt; 536f7c21177STheodore Ts'o vaf.va = &args; 537c398eda0STheodore Ts'o if (block) 538d9ee81daSJoe Perches printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: " 539d9ee81daSJoe Perches "inode #%lu: block %llu: comm %s: %pV\n", 540d9ee81daSJoe Perches inode->i_sb->s_id, function, line, inode->i_ino, 541d9ee81daSJoe Perches block, current->comm, &vaf); 542d9ee81daSJoe Perches else 543d9ee81daSJoe Perches printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: " 544d9ee81daSJoe Perches "inode #%lu: comm %s: %pV\n", 545d9ee81daSJoe Perches inode->i_sb->s_id, function, line, inode->i_ino, 546d9ee81daSJoe Perches current->comm, &vaf); 547273df556SFrank Mayhar va_end(args); 548efbed4dcSTheodore Ts'o } 549efbed4dcSTheodore Ts'o save_error_info(inode->i_sb, function, line); 550273df556SFrank Mayhar ext4_handle_error(inode->i_sb); 551273df556SFrank Mayhar } 552273df556SFrank Mayhar 553e7c96e8eSJoe Perches void __ext4_error_file(struct file *file, const char *function, 554f7c21177STheodore Ts'o unsigned int line, ext4_fsblk_t block, 555f7c21177STheodore Ts'o const char *fmt, ...) 556273df556SFrank Mayhar { 557273df556SFrank Mayhar va_list args; 558f7c21177STheodore Ts'o struct va_format vaf; 5591c13d5c0STheodore Ts'o struct ext4_super_block *es; 560496ad9aaSAl Viro struct inode *inode = file_inode(file); 561273df556SFrank Mayhar char pathname[80], *path; 562273df556SFrank Mayhar 5630db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 5640db1ff22STheodore Ts'o return; 5650db1ff22STheodore Ts'o 566ccf0f32aSTheodore Ts'o trace_ext4_error(inode->i_sb, function, line); 5671c13d5c0STheodore Ts'o es = EXT4_SB(inode->i_sb)->s_es; 5681c13d5c0STheodore Ts'o es->s_last_error_ino = cpu_to_le32(inode->i_ino); 569efbed4dcSTheodore Ts'o if (ext4_error_ratelimit(inode->i_sb)) { 5709bf39ab2SMiklos Szeredi path = file_path(file, pathname, sizeof(pathname)); 571f9a62d09SDan Carpenter if (IS_ERR(path)) 572273df556SFrank Mayhar path = "(unknown)"; 573f7c21177STheodore Ts'o va_start(args, fmt); 574f7c21177STheodore Ts'o vaf.fmt = fmt; 575f7c21177STheodore Ts'o vaf.va = &args; 576d9ee81daSJoe Perches if (block) 577d9ee81daSJoe Perches printk(KERN_CRIT 578d9ee81daSJoe Perches "EXT4-fs error (device %s): %s:%d: inode #%lu: " 579d9ee81daSJoe Perches "block %llu: comm %s: path %s: %pV\n", 580d9ee81daSJoe Perches inode->i_sb->s_id, function, line, inode->i_ino, 581d9ee81daSJoe Perches block, current->comm, path, &vaf); 582d9ee81daSJoe Perches else 583d9ee81daSJoe Perches printk(KERN_CRIT 584d9ee81daSJoe Perches "EXT4-fs error (device %s): %s:%d: inode #%lu: " 585d9ee81daSJoe Perches "comm %s: path %s: %pV\n", 586d9ee81daSJoe Perches inode->i_sb->s_id, function, line, inode->i_ino, 587d9ee81daSJoe Perches current->comm, path, &vaf); 588273df556SFrank Mayhar va_end(args); 589efbed4dcSTheodore Ts'o } 590efbed4dcSTheodore Ts'o save_error_info(inode->i_sb, function, line); 591273df556SFrank Mayhar ext4_handle_error(inode->i_sb); 592273df556SFrank Mayhar } 593273df556SFrank Mayhar 594722887ddSTheodore Ts'o const char *ext4_decode_error(struct super_block *sb, int errno, 595ac27a0ecSDave Kleikamp char nbuf[16]) 596ac27a0ecSDave Kleikamp { 597ac27a0ecSDave Kleikamp char *errstr = NULL; 598ac27a0ecSDave Kleikamp 599ac27a0ecSDave Kleikamp switch (errno) { 6006a797d27SDarrick J. Wong case -EFSCORRUPTED: 6016a797d27SDarrick J. Wong errstr = "Corrupt filesystem"; 6026a797d27SDarrick J. Wong break; 6036a797d27SDarrick J. Wong case -EFSBADCRC: 6046a797d27SDarrick J. Wong errstr = "Filesystem failed CRC"; 6056a797d27SDarrick J. Wong break; 606ac27a0ecSDave Kleikamp case -EIO: 607ac27a0ecSDave Kleikamp errstr = "IO failure"; 608ac27a0ecSDave Kleikamp break; 609ac27a0ecSDave Kleikamp case -ENOMEM: 610ac27a0ecSDave Kleikamp errstr = "Out of memory"; 611ac27a0ecSDave Kleikamp break; 612ac27a0ecSDave Kleikamp case -EROFS: 61378f1ddbbSTheodore Ts'o if (!sb || (EXT4_SB(sb)->s_journal && 61478f1ddbbSTheodore Ts'o EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT)) 615ac27a0ecSDave Kleikamp errstr = "Journal has aborted"; 616ac27a0ecSDave Kleikamp else 617ac27a0ecSDave Kleikamp errstr = "Readonly filesystem"; 618ac27a0ecSDave Kleikamp break; 619ac27a0ecSDave Kleikamp default: 620ac27a0ecSDave Kleikamp /* If the caller passed in an extra buffer for unknown 621ac27a0ecSDave Kleikamp * errors, textualise them now. Else we just return 622ac27a0ecSDave Kleikamp * NULL. */ 623ac27a0ecSDave Kleikamp if (nbuf) { 624ac27a0ecSDave Kleikamp /* Check for truncated error codes... */ 625ac27a0ecSDave Kleikamp if (snprintf(nbuf, 16, "error %d", -errno) >= 0) 626ac27a0ecSDave Kleikamp errstr = nbuf; 627ac27a0ecSDave Kleikamp } 628ac27a0ecSDave Kleikamp break; 629ac27a0ecSDave Kleikamp } 630ac27a0ecSDave Kleikamp 631ac27a0ecSDave Kleikamp return errstr; 632ac27a0ecSDave Kleikamp } 633ac27a0ecSDave Kleikamp 634617ba13bSMingming Cao /* __ext4_std_error decodes expected errors from journaling functions 635ac27a0ecSDave Kleikamp * automatically and invokes the appropriate error response. */ 636ac27a0ecSDave Kleikamp 637c398eda0STheodore Ts'o void __ext4_std_error(struct super_block *sb, const char *function, 638c398eda0STheodore Ts'o unsigned int line, int errno) 639ac27a0ecSDave Kleikamp { 640ac27a0ecSDave Kleikamp char nbuf[16]; 641ac27a0ecSDave Kleikamp const char *errstr; 642ac27a0ecSDave Kleikamp 6430db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(sb)))) 6440db1ff22STheodore Ts'o return; 6450db1ff22STheodore Ts'o 646ac27a0ecSDave Kleikamp /* Special case: if the error is EROFS, and we're not already 647ac27a0ecSDave Kleikamp * inside a transaction, then there's really no point in logging 648ac27a0ecSDave Kleikamp * an error. */ 649bc98a42cSDavid Howells if (errno == -EROFS && journal_current_handle() == NULL && sb_rdonly(sb)) 650ac27a0ecSDave Kleikamp return; 651ac27a0ecSDave Kleikamp 652efbed4dcSTheodore Ts'o if (ext4_error_ratelimit(sb)) { 653617ba13bSMingming Cao errstr = ext4_decode_error(sb, errno, nbuf); 654c398eda0STheodore Ts'o printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n", 655c398eda0STheodore Ts'o sb->s_id, function, line, errstr); 656efbed4dcSTheodore Ts'o } 657ac27a0ecSDave Kleikamp 658efbed4dcSTheodore Ts'o save_error_info(sb, function, line); 659617ba13bSMingming Cao ext4_handle_error(sb); 660ac27a0ecSDave Kleikamp } 661ac27a0ecSDave Kleikamp 662ac27a0ecSDave Kleikamp /* 663617ba13bSMingming Cao * ext4_abort is a much stronger failure handler than ext4_error. The 664ac27a0ecSDave Kleikamp * abort function may be used to deal with unrecoverable failures such 665ac27a0ecSDave Kleikamp * as journal IO errors or ENOMEM at a critical moment in log management. 666ac27a0ecSDave Kleikamp * 667ac27a0ecSDave Kleikamp * We unconditionally force the filesystem into an ABORT|READONLY state, 668ac27a0ecSDave Kleikamp * unless the error response on the fs has been set to panic in which 669ac27a0ecSDave Kleikamp * case we take the easy way out and panic immediately. 670ac27a0ecSDave Kleikamp */ 671ac27a0ecSDave Kleikamp 672c67d859eSTheodore Ts'o void __ext4_abort(struct super_block *sb, const char *function, 673c398eda0STheodore Ts'o unsigned int line, const char *fmt, ...) 674ac27a0ecSDave Kleikamp { 675651e1c3bSJoe Perches struct va_format vaf; 676ac27a0ecSDave Kleikamp va_list args; 677ac27a0ecSDave Kleikamp 6780db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(sb)))) 6790db1ff22STheodore Ts'o return; 6800db1ff22STheodore Ts'o 6811c13d5c0STheodore Ts'o save_error_info(sb, function, line); 682ac27a0ecSDave Kleikamp va_start(args, fmt); 683651e1c3bSJoe Perches vaf.fmt = fmt; 684651e1c3bSJoe Perches vaf.va = &args; 685651e1c3bSJoe Perches printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: %pV\n", 686651e1c3bSJoe Perches sb->s_id, function, line, &vaf); 687ac27a0ecSDave Kleikamp va_end(args); 688ac27a0ecSDave Kleikamp 689bc98a42cSDavid Howells if (sb_rdonly(sb) == 0) { 690b31e1552SEric Sandeen ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only"); 6914ab2f15bSTheodore Ts'o EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED; 6924418e141SDmitry Monakhov /* 6934418e141SDmitry Monakhov * Make sure updated value of ->s_mount_flags will be visible 6944418e141SDmitry Monakhov * before ->s_flags update 6954418e141SDmitry Monakhov */ 6964418e141SDmitry Monakhov smp_wmb(); 6971751e8a6SLinus Torvalds sb->s_flags |= SB_RDONLY; 698ef2cabf7SHidehiro Kawai if (EXT4_SB(sb)->s_journal) 699dab291afSMingming Cao jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO); 7001c13d5c0STheodore Ts'o save_error_info(sb, function, line); 7011c13d5c0STheodore Ts'o } 7022c1d0e36SJan Kara if (test_opt(sb, ERRORS_PANIC) && !system_going_down()) { 7034327ba52SDaeho Jeong if (EXT4_SB(sb)->s_journal && 7044327ba52SDaeho Jeong !(EXT4_SB(sb)->s_journal->j_flags & JBD2_REC_ERR)) 7054327ba52SDaeho Jeong return; 7061c13d5c0STheodore Ts'o panic("EXT4-fs panic from previous error\n"); 707ac27a0ecSDave Kleikamp } 7084327ba52SDaeho Jeong } 709ac27a0ecSDave Kleikamp 710e7c96e8eSJoe Perches void __ext4_msg(struct super_block *sb, 711e7c96e8eSJoe Perches const char *prefix, const char *fmt, ...) 712b31e1552SEric Sandeen { 7130ff2ea7dSJoe Perches struct va_format vaf; 714b31e1552SEric Sandeen va_list args; 715b31e1552SEric Sandeen 716efbed4dcSTheodore Ts'o if (!___ratelimit(&(EXT4_SB(sb)->s_msg_ratelimit_state), "EXT4-fs")) 717efbed4dcSTheodore Ts'o return; 718efbed4dcSTheodore Ts'o 719b31e1552SEric Sandeen va_start(args, fmt); 7200ff2ea7dSJoe Perches vaf.fmt = fmt; 7210ff2ea7dSJoe Perches vaf.va = &args; 7220ff2ea7dSJoe Perches printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf); 723b31e1552SEric Sandeen va_end(args); 724b31e1552SEric Sandeen } 725b31e1552SEric Sandeen 726b03a2f7eSAndreas Dilger #define ext4_warning_ratelimit(sb) \ 727b03a2f7eSAndreas Dilger ___ratelimit(&(EXT4_SB(sb)->s_warning_ratelimit_state), \ 728b03a2f7eSAndreas Dilger "EXT4-fs warning") 729b03a2f7eSAndreas Dilger 73012062dddSEric Sandeen void __ext4_warning(struct super_block *sb, const char *function, 731c398eda0STheodore Ts'o unsigned int line, const char *fmt, ...) 732ac27a0ecSDave Kleikamp { 7330ff2ea7dSJoe Perches struct va_format vaf; 734ac27a0ecSDave Kleikamp va_list args; 735ac27a0ecSDave Kleikamp 736b03a2f7eSAndreas Dilger if (!ext4_warning_ratelimit(sb)) 737efbed4dcSTheodore Ts'o return; 738efbed4dcSTheodore Ts'o 739ac27a0ecSDave Kleikamp va_start(args, fmt); 7400ff2ea7dSJoe Perches vaf.fmt = fmt; 7410ff2ea7dSJoe Perches vaf.va = &args; 7420ff2ea7dSJoe Perches printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n", 7430ff2ea7dSJoe Perches sb->s_id, function, line, &vaf); 744ac27a0ecSDave Kleikamp va_end(args); 745ac27a0ecSDave Kleikamp } 746ac27a0ecSDave Kleikamp 747b03a2f7eSAndreas Dilger void __ext4_warning_inode(const struct inode *inode, const char *function, 748b03a2f7eSAndreas Dilger unsigned int line, const char *fmt, ...) 749b03a2f7eSAndreas Dilger { 750b03a2f7eSAndreas Dilger struct va_format vaf; 751b03a2f7eSAndreas Dilger va_list args; 752b03a2f7eSAndreas Dilger 753b03a2f7eSAndreas Dilger if (!ext4_warning_ratelimit(inode->i_sb)) 754b03a2f7eSAndreas Dilger return; 755b03a2f7eSAndreas Dilger 756b03a2f7eSAndreas Dilger va_start(args, fmt); 757b03a2f7eSAndreas Dilger vaf.fmt = fmt; 758b03a2f7eSAndreas Dilger vaf.va = &args; 759b03a2f7eSAndreas Dilger printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: " 760b03a2f7eSAndreas Dilger "inode #%lu: comm %s: %pV\n", inode->i_sb->s_id, 761b03a2f7eSAndreas Dilger function, line, inode->i_ino, current->comm, &vaf); 762b03a2f7eSAndreas Dilger va_end(args); 763b03a2f7eSAndreas Dilger } 764b03a2f7eSAndreas Dilger 765e29136f8STheodore Ts'o void __ext4_grp_locked_error(const char *function, unsigned int line, 766e29136f8STheodore Ts'o struct super_block *sb, ext4_group_t grp, 767e29136f8STheodore Ts'o unsigned long ino, ext4_fsblk_t block, 768e29136f8STheodore Ts'o const char *fmt, ...) 7695d1b1b3fSAneesh Kumar K.V __releases(bitlock) 7705d1b1b3fSAneesh Kumar K.V __acquires(bitlock) 7715d1b1b3fSAneesh Kumar K.V { 7720ff2ea7dSJoe Perches struct va_format vaf; 7735d1b1b3fSAneesh Kumar K.V va_list args; 7745d1b1b3fSAneesh Kumar K.V struct ext4_super_block *es = EXT4_SB(sb)->s_es; 7755d1b1b3fSAneesh Kumar K.V 7760db1ff22STheodore Ts'o if (unlikely(ext4_forced_shutdown(EXT4_SB(sb)))) 7770db1ff22STheodore Ts'o return; 7780db1ff22STheodore Ts'o 779ccf0f32aSTheodore Ts'o trace_ext4_error(sb, function, line); 7801c13d5c0STheodore Ts'o es->s_last_error_ino = cpu_to_le32(ino); 7811c13d5c0STheodore Ts'o es->s_last_error_block = cpu_to_le64(block); 7821c13d5c0STheodore Ts'o __save_error_info(sb, function, line); 7830ff2ea7dSJoe Perches 784efbed4dcSTheodore Ts'o if (ext4_error_ratelimit(sb)) { 7855d1b1b3fSAneesh Kumar K.V va_start(args, fmt); 7860ff2ea7dSJoe Perches vaf.fmt = fmt; 7870ff2ea7dSJoe Perches vaf.va = &args; 78821149d61SRobin Dong printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ", 789e29136f8STheodore Ts'o sb->s_id, function, line, grp); 790e29136f8STheodore Ts'o if (ino) 7910ff2ea7dSJoe Perches printk(KERN_CONT "inode %lu: ", ino); 792e29136f8STheodore Ts'o if (block) 793efbed4dcSTheodore Ts'o printk(KERN_CONT "block %llu:", 794efbed4dcSTheodore Ts'o (unsigned long long) block); 7950ff2ea7dSJoe Perches printk(KERN_CONT "%pV\n", &vaf); 7965d1b1b3fSAneesh Kumar K.V va_end(args); 797efbed4dcSTheodore Ts'o } 7985d1b1b3fSAneesh Kumar K.V 799327eaf73STheodore Ts'o if (test_opt(sb, WARN_ON_ERROR)) 800327eaf73STheodore Ts'o WARN_ON_ONCE(1); 801327eaf73STheodore Ts'o 8025d1b1b3fSAneesh Kumar K.V if (test_opt(sb, ERRORS_CONT)) { 803e2d67052STheodore Ts'o ext4_commit_super(sb, 0); 8045d1b1b3fSAneesh Kumar K.V return; 8055d1b1b3fSAneesh Kumar K.V } 8061c13d5c0STheodore Ts'o 8075d1b1b3fSAneesh Kumar K.V ext4_unlock_group(sb, grp); 80806f29cc8SZhouyi Zhou ext4_commit_super(sb, 1); 8095d1b1b3fSAneesh Kumar K.V ext4_handle_error(sb); 8105d1b1b3fSAneesh Kumar K.V /* 8115d1b1b3fSAneesh Kumar K.V * We only get here in the ERRORS_RO case; relocking the group 8125d1b1b3fSAneesh Kumar K.V * may be dangerous, but nothing bad will happen since the 8135d1b1b3fSAneesh Kumar K.V * filesystem will have already been marked read/only and the 8145d1b1b3fSAneesh Kumar K.V * journal has been aborted. We return 1 as a hint to callers 8155d1b1b3fSAneesh Kumar K.V * who might what to use the return value from 81625985edcSLucas De Marchi * ext4_grp_locked_error() to distinguish between the 8175d1b1b3fSAneesh Kumar K.V * ERRORS_CONT and ERRORS_RO case, and perhaps return more 8185d1b1b3fSAneesh Kumar K.V * aggressively from the ext4 function in question, with a 8195d1b1b3fSAneesh Kumar K.V * more appropriate error code. 8205d1b1b3fSAneesh Kumar K.V */ 8215d1b1b3fSAneesh Kumar K.V ext4_lock_group(sb, grp); 8225d1b1b3fSAneesh Kumar K.V return; 8235d1b1b3fSAneesh Kumar K.V } 8245d1b1b3fSAneesh Kumar K.V 825db79e6d1SWang Shilong void ext4_mark_group_bitmap_corrupted(struct super_block *sb, 826db79e6d1SWang Shilong ext4_group_t group, 827db79e6d1SWang Shilong unsigned int flags) 828db79e6d1SWang Shilong { 829db79e6d1SWang Shilong struct ext4_sb_info *sbi = EXT4_SB(sb); 830db79e6d1SWang Shilong struct ext4_group_info *grp = ext4_get_group_info(sb, group); 831db79e6d1SWang Shilong struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL); 8329af0b3d1SWang Shilong int ret; 833db79e6d1SWang Shilong 8349af0b3d1SWang Shilong if (flags & EXT4_GROUP_INFO_BBITMAP_CORRUPT) { 8359af0b3d1SWang Shilong ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT, 8369af0b3d1SWang Shilong &grp->bb_state); 8379af0b3d1SWang Shilong if (!ret) 838db79e6d1SWang Shilong percpu_counter_sub(&sbi->s_freeclusters_counter, 839db79e6d1SWang Shilong grp->bb_free); 840db79e6d1SWang Shilong } 841db79e6d1SWang Shilong 8429af0b3d1SWang Shilong if (flags & EXT4_GROUP_INFO_IBITMAP_CORRUPT) { 8439af0b3d1SWang Shilong ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, 8449af0b3d1SWang Shilong &grp->bb_state); 8459af0b3d1SWang Shilong if (!ret && gdp) { 846db79e6d1SWang Shilong int count; 847db79e6d1SWang Shilong 848db79e6d1SWang Shilong count = ext4_free_inodes_count(sb, gdp); 849db79e6d1SWang Shilong percpu_counter_sub(&sbi->s_freeinodes_counter, 850db79e6d1SWang Shilong count); 851db79e6d1SWang Shilong } 852db79e6d1SWang Shilong } 853db79e6d1SWang Shilong } 854db79e6d1SWang Shilong 855617ba13bSMingming Cao void ext4_update_dynamic_rev(struct super_block *sb) 856ac27a0ecSDave Kleikamp { 857617ba13bSMingming Cao struct ext4_super_block *es = EXT4_SB(sb)->s_es; 858ac27a0ecSDave Kleikamp 859617ba13bSMingming Cao if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV) 860ac27a0ecSDave Kleikamp return; 861ac27a0ecSDave Kleikamp 86212062dddSEric Sandeen ext4_warning(sb, 863ac27a0ecSDave Kleikamp "updating to rev %d because of new feature flag, " 864ac27a0ecSDave Kleikamp "running e2fsck is recommended", 865617ba13bSMingming Cao EXT4_DYNAMIC_REV); 866ac27a0ecSDave Kleikamp 867617ba13bSMingming Cao es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO); 868617ba13bSMingming Cao es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE); 869617ba13bSMingming Cao es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV); 870ac27a0ecSDave Kleikamp /* leave es->s_feature_*compat flags alone */ 871ac27a0ecSDave Kleikamp /* es->s_uuid will be set by e2fsck if empty */ 872ac27a0ecSDave Kleikamp 873ac27a0ecSDave Kleikamp /* 874ac27a0ecSDave Kleikamp * The rest of the superblock fields should be zero, and if not it 875ac27a0ecSDave Kleikamp * means they are likely already in use, so leave them alone. We 876ac27a0ecSDave Kleikamp * can leave it up to e2fsck to clean up any inconsistencies there. 877ac27a0ecSDave Kleikamp */ 878ac27a0ecSDave Kleikamp } 879ac27a0ecSDave Kleikamp 880ac27a0ecSDave Kleikamp /* 881ac27a0ecSDave Kleikamp * Open the external journal device 882ac27a0ecSDave Kleikamp */ 883b31e1552SEric Sandeen static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb) 884ac27a0ecSDave Kleikamp { 885ac27a0ecSDave Kleikamp struct block_device *bdev; 886ac27a0ecSDave Kleikamp char b[BDEVNAME_SIZE]; 887ac27a0ecSDave Kleikamp 888d4d77629STejun Heo bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb); 889ac27a0ecSDave Kleikamp if (IS_ERR(bdev)) 890ac27a0ecSDave Kleikamp goto fail; 891ac27a0ecSDave Kleikamp return bdev; 892ac27a0ecSDave Kleikamp 893ac27a0ecSDave Kleikamp fail: 894b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "failed to open journal device %s: %ld", 895ac27a0ecSDave Kleikamp __bdevname(dev, b), PTR_ERR(bdev)); 896ac27a0ecSDave Kleikamp return NULL; 897ac27a0ecSDave Kleikamp } 898ac27a0ecSDave Kleikamp 899ac27a0ecSDave Kleikamp /* 900ac27a0ecSDave Kleikamp * Release the journal device 901ac27a0ecSDave Kleikamp */ 9024385bab1SAl Viro static void ext4_blkdev_put(struct block_device *bdev) 903ac27a0ecSDave Kleikamp { 9044385bab1SAl Viro blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); 905ac27a0ecSDave Kleikamp } 906ac27a0ecSDave Kleikamp 9074385bab1SAl Viro static void ext4_blkdev_remove(struct ext4_sb_info *sbi) 908ac27a0ecSDave Kleikamp { 909ac27a0ecSDave Kleikamp struct block_device *bdev; 910ac27a0ecSDave Kleikamp bdev = sbi->journal_bdev; 911ac27a0ecSDave Kleikamp if (bdev) { 9124385bab1SAl Viro ext4_blkdev_put(bdev); 913ac27a0ecSDave Kleikamp sbi->journal_bdev = NULL; 914ac27a0ecSDave Kleikamp } 915ac27a0ecSDave Kleikamp } 916ac27a0ecSDave Kleikamp 917ac27a0ecSDave Kleikamp static inline struct inode *orphan_list_entry(struct list_head *l) 918ac27a0ecSDave Kleikamp { 919617ba13bSMingming Cao return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode; 920ac27a0ecSDave Kleikamp } 921ac27a0ecSDave Kleikamp 922617ba13bSMingming Cao static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi) 923ac27a0ecSDave Kleikamp { 924ac27a0ecSDave Kleikamp struct list_head *l; 925ac27a0ecSDave Kleikamp 926b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "sb orphan head is %d", 927ac27a0ecSDave Kleikamp le32_to_cpu(sbi->s_es->s_last_orphan)); 928ac27a0ecSDave Kleikamp 929ac27a0ecSDave Kleikamp printk(KERN_ERR "sb_info orphan list:\n"); 930ac27a0ecSDave Kleikamp list_for_each(l, &sbi->s_orphan) { 931ac27a0ecSDave Kleikamp struct inode *inode = orphan_list_entry(l); 932ac27a0ecSDave Kleikamp printk(KERN_ERR " " 933ac27a0ecSDave Kleikamp "inode %s:%lu at %p: mode %o, nlink %d, next %d\n", 934ac27a0ecSDave Kleikamp inode->i_sb->s_id, inode->i_ino, inode, 935ac27a0ecSDave Kleikamp inode->i_mode, inode->i_nlink, 936ac27a0ecSDave Kleikamp NEXT_ORPHAN(inode)); 937ac27a0ecSDave Kleikamp } 938ac27a0ecSDave Kleikamp } 939ac27a0ecSDave Kleikamp 940957153fcSJan Kara #ifdef CONFIG_QUOTA 941957153fcSJan Kara static int ext4_quota_off(struct super_block *sb, int type); 942957153fcSJan Kara 943957153fcSJan Kara static inline void ext4_quota_off_umount(struct super_block *sb) 944957153fcSJan Kara { 945957153fcSJan Kara int type; 946957153fcSJan Kara 947957153fcSJan Kara /* Use our quota_off function to clear inode flags etc. */ 948957153fcSJan Kara for (type = 0; type < EXT4_MAXQUOTAS; type++) 949957153fcSJan Kara ext4_quota_off(sb, type); 950957153fcSJan Kara } 95133458eabSTheodore Ts'o 95233458eabSTheodore Ts'o /* 95333458eabSTheodore Ts'o * This is a helper function which is used in the mount/remount 95433458eabSTheodore Ts'o * codepaths (which holds s_umount) to fetch the quota file name. 95533458eabSTheodore Ts'o */ 95633458eabSTheodore Ts'o static inline char *get_qf_name(struct super_block *sb, 95733458eabSTheodore Ts'o struct ext4_sb_info *sbi, 95833458eabSTheodore Ts'o int type) 95933458eabSTheodore Ts'o { 96033458eabSTheodore Ts'o return rcu_dereference_protected(sbi->s_qf_names[type], 96133458eabSTheodore Ts'o lockdep_is_held(&sb->s_umount)); 96233458eabSTheodore Ts'o } 963957153fcSJan Kara #else 964957153fcSJan Kara static inline void ext4_quota_off_umount(struct super_block *sb) 965957153fcSJan Kara { 966957153fcSJan Kara } 967957153fcSJan Kara #endif 968957153fcSJan Kara 969617ba13bSMingming Cao static void ext4_put_super(struct super_block *sb) 970ac27a0ecSDave Kleikamp { 971617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 972617ba13bSMingming Cao struct ext4_super_block *es = sbi->s_es; 97397abd7d4STheodore Ts'o int aborted = 0; 974ef2cabf7SHidehiro Kawai int i, err; 975ac27a0ecSDave Kleikamp 976857ac889SLukas Czerner ext4_unregister_li_request(sb); 977957153fcSJan Kara ext4_quota_off_umount(sb); 978e0ccfd95SChristoph Hellwig 9792e8fa54eSJan Kara destroy_workqueue(sbi->rsv_conversion_wq); 9804c0425ffSMingming Cao 9810390131bSFrank Mayhar if (sbi->s_journal) { 98297abd7d4STheodore Ts'o aborted = is_journal_aborted(sbi->s_journal); 983ef2cabf7SHidehiro Kawai err = jbd2_journal_destroy(sbi->s_journal); 98447b4a50bSJan Kara sbi->s_journal = NULL; 98597abd7d4STheodore Ts'o if ((err < 0) && !aborted) 986c67d859eSTheodore Ts'o ext4_abort(sb, "Couldn't clean up the journal"); 9870390131bSFrank Mayhar } 988d4edac31SJosef Bacik 989ebd173beSTheodore Ts'o ext4_unregister_sysfs(sb); 990d3922a77SZheng Liu ext4_es_unregister_shrinker(sbi); 9919105bb14SAl Viro del_timer_sync(&sbi->s_err_report); 992d4edac31SJosef Bacik ext4_release_system_zone(sb); 993d4edac31SJosef Bacik ext4_mb_release(sb); 994d4edac31SJosef Bacik ext4_ext_release(sb); 995d4edac31SJosef Bacik 996bc98a42cSDavid Howells if (!sb_rdonly(sb) && !aborted) { 997e2b911c5SDarrick J. Wong ext4_clear_feature_journal_needs_recovery(sb); 998ac27a0ecSDave Kleikamp es->s_state = cpu_to_le16(sbi->s_mount_state); 999ac27a0ecSDave Kleikamp } 1000bc98a42cSDavid Howells if (!sb_rdonly(sb)) 1001a8e25a83SArtem Bityutskiy ext4_commit_super(sb, 1); 1002a8e25a83SArtem Bityutskiy 1003ac27a0ecSDave Kleikamp for (i = 0; i < sbi->s_gdb_count; i++) 1004ac27a0ecSDave Kleikamp brelse(sbi->s_group_desc[i]); 1005b93b41d4SAl Viro kvfree(sbi->s_group_desc); 1006b93b41d4SAl Viro kvfree(sbi->s_flex_groups); 100757042651STheodore Ts'o percpu_counter_destroy(&sbi->s_freeclusters_counter); 1008ac27a0ecSDave Kleikamp percpu_counter_destroy(&sbi->s_freeinodes_counter); 1009ac27a0ecSDave Kleikamp percpu_counter_destroy(&sbi->s_dirs_counter); 101057042651STheodore Ts'o percpu_counter_destroy(&sbi->s_dirtyclusters_counter); 1011c8585c6fSDaeho Jeong percpu_free_rwsem(&sbi->s_journal_flag_rwsem); 1012ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 1013a2d4a646SJan Kara for (i = 0; i < EXT4_MAXQUOTAS; i++) 101433458eabSTheodore Ts'o kfree(get_qf_name(sb, sbi, i)); 1015ac27a0ecSDave Kleikamp #endif 1016ac27a0ecSDave Kleikamp 1017ac27a0ecSDave Kleikamp /* Debugging code just in case the in-memory inode orphan list 1018ac27a0ecSDave Kleikamp * isn't empty. The on-disk one can be non-empty if we've 1019ac27a0ecSDave Kleikamp * detected an error and taken the fs readonly, but the 1020ac27a0ecSDave Kleikamp * in-memory list had better be clean by this point. */ 1021ac27a0ecSDave Kleikamp if (!list_empty(&sbi->s_orphan)) 1022ac27a0ecSDave Kleikamp dump_orphan_list(sb, sbi); 1023ac27a0ecSDave Kleikamp J_ASSERT(list_empty(&sbi->s_orphan)); 1024ac27a0ecSDave Kleikamp 102589d96a6fSTheodore Ts'o sync_blockdev(sb->s_bdev); 1026f98393a6SPeter Zijlstra invalidate_bdev(sb->s_bdev); 1027ac27a0ecSDave Kleikamp if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) { 1028ac27a0ecSDave Kleikamp /* 1029ac27a0ecSDave Kleikamp * Invalidate the journal device's buffers. We don't want them 1030ac27a0ecSDave Kleikamp * floating about in memory - the physical journal device may 1031ac27a0ecSDave Kleikamp * hotswapped, and it breaks the `ro-after' testing code. 1032ac27a0ecSDave Kleikamp */ 1033ac27a0ecSDave Kleikamp sync_blockdev(sbi->journal_bdev); 1034f98393a6SPeter Zijlstra invalidate_bdev(sbi->journal_bdev); 1035617ba13bSMingming Cao ext4_blkdev_remove(sbi); 1036ac27a0ecSDave Kleikamp } 103750c15df6SChengguang Xu 1038dec214d0STahsin Erdogan ext4_xattr_destroy_cache(sbi->s_ea_inode_cache); 1039dec214d0STahsin Erdogan sbi->s_ea_inode_cache = NULL; 104050c15df6SChengguang Xu 104147387409STahsin Erdogan ext4_xattr_destroy_cache(sbi->s_ea_block_cache); 104247387409STahsin Erdogan sbi->s_ea_block_cache = NULL; 104350c15df6SChengguang Xu 1044c5e06d10SJohann Lombardi if (sbi->s_mmp_tsk) 1045c5e06d10SJohann Lombardi kthread_stop(sbi->s_mmp_tsk); 10469060dd2cSEric Sandeen brelse(sbi->s_sbh); 1047ac27a0ecSDave Kleikamp sb->s_fs_info = NULL; 10483197ebdbSTheodore Ts'o /* 10493197ebdbSTheodore Ts'o * Now that we are completely done shutting down the 10503197ebdbSTheodore Ts'o * superblock, we need to actually destroy the kobject. 10513197ebdbSTheodore Ts'o */ 10523197ebdbSTheodore Ts'o kobject_put(&sbi->s_kobj); 10533197ebdbSTheodore Ts'o wait_for_completion(&sbi->s_kobj_unregister); 10540441984aSDarrick J. Wong if (sbi->s_chksum_driver) 10550441984aSDarrick J. Wong crypto_free_shash(sbi->s_chksum_driver); 1056705895b6SPekka Enberg kfree(sbi->s_blockgroup_lock); 10575e405595SDan Williams fs_put_dax(sbi->s_daxdev); 1058c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE 1059c83ad55eSGabriel Krisman Bertazi utf8_unload(sbi->s_encoding); 1060c83ad55eSGabriel Krisman Bertazi #endif 1061ac27a0ecSDave Kleikamp kfree(sbi); 1062ac27a0ecSDave Kleikamp } 1063ac27a0ecSDave Kleikamp 1064e18b890bSChristoph Lameter static struct kmem_cache *ext4_inode_cachep; 1065ac27a0ecSDave Kleikamp 1066ac27a0ecSDave Kleikamp /* 1067ac27a0ecSDave Kleikamp * Called inside transaction, so use GFP_NOFS 1068ac27a0ecSDave Kleikamp */ 1069617ba13bSMingming Cao static struct inode *ext4_alloc_inode(struct super_block *sb) 1070ac27a0ecSDave Kleikamp { 1071617ba13bSMingming Cao struct ext4_inode_info *ei; 1072ac27a0ecSDave Kleikamp 1073e6b4f8daSChristoph Lameter ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS); 1074ac27a0ecSDave Kleikamp if (!ei) 1075ac27a0ecSDave Kleikamp return NULL; 10760b8e58a1SAndreas Dilger 1077ee73f9a5SJeff Layton inode_set_iversion(&ei->vfs_inode, 1); 1078202ee5dfSTheodore Ts'o spin_lock_init(&ei->i_raw_lock); 1079c9de560dSAlex Tomas INIT_LIST_HEAD(&ei->i_prealloc_list); 1080c9de560dSAlex Tomas spin_lock_init(&ei->i_prealloc_lock); 10819a26b661SZheng Liu ext4_es_init_tree(&ei->i_es_tree); 10829a26b661SZheng Liu rwlock_init(&ei->i_es_lock); 1083edaa53caSZheng Liu INIT_LIST_HEAD(&ei->i_es_list); 1084eb68d0e2SZheng Liu ei->i_es_all_nr = 0; 1085edaa53caSZheng Liu ei->i_es_shk_nr = 0; 1086dd475925SJan Kara ei->i_es_shrink_lblk = 0; 1087d2a17637SMingming Cao ei->i_reserved_data_blocks = 0; 10889d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 0; 10897e731bc9STheodore Ts'o ei->i_da_metadata_calc_last_lblock = 0; 1090d2a17637SMingming Cao spin_lock_init(&(ei->i_block_reservation_lock)); 10911dc0aa46SEric Whitney ext4_init_pending_tree(&ei->i_pending_tree); 1092a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 1093a9e7f447SDmitry Monakhov ei->i_reserved_quota = 0; 109496c7e0d9SJan Kara memset(&ei->i_dquot, 0, sizeof(ei->i_dquot)); 1095a9e7f447SDmitry Monakhov #endif 10968aefcd55STheodore Ts'o ei->jinode = NULL; 10972e8fa54eSJan Kara INIT_LIST_HEAD(&ei->i_rsv_conversion_list); 1098744692dcSJiaying Zhang spin_lock_init(&ei->i_completed_io_lock); 1099b436b9beSJan Kara ei->i_sync_tid = 0; 1100b436b9beSJan Kara ei->i_datasync_tid = 0; 1101e27f41e1SDmitry Monakhov atomic_set(&ei->i_unwritten, 0); 11022e8fa54eSJan Kara INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work); 1103ac27a0ecSDave Kleikamp return &ei->vfs_inode; 1104ac27a0ecSDave Kleikamp } 1105ac27a0ecSDave Kleikamp 11067ff9c073STheodore Ts'o static int ext4_drop_inode(struct inode *inode) 11077ff9c073STheodore Ts'o { 11087ff9c073STheodore Ts'o int drop = generic_drop_inode(inode); 11097ff9c073STheodore Ts'o 111029b3692eSEric Biggers if (!drop) 111129b3692eSEric Biggers drop = fscrypt_drop_inode(inode); 111229b3692eSEric Biggers 11137ff9c073STheodore Ts'o trace_ext4_drop_inode(inode, drop); 11147ff9c073STheodore Ts'o return drop; 11157ff9c073STheodore Ts'o } 11167ff9c073STheodore Ts'o 111794053139SAl Viro static void ext4_free_in_core_inode(struct inode *inode) 1118fa0d7e3dSNick Piggin { 11192c58d548SEric Biggers fscrypt_free_inode(inode); 1120fa0d7e3dSNick Piggin kmem_cache_free(ext4_inode_cachep, EXT4_I(inode)); 1121fa0d7e3dSNick Piggin } 1122fa0d7e3dSNick Piggin 1123617ba13bSMingming Cao static void ext4_destroy_inode(struct inode *inode) 1124ac27a0ecSDave Kleikamp { 11259f7dd93dSVasily Averin if (!list_empty(&(EXT4_I(inode)->i_orphan))) { 1126b31e1552SEric Sandeen ext4_msg(inode->i_sb, KERN_ERR, 1127b31e1552SEric Sandeen "Inode %lu (%p): orphan list check failed!", 1128b31e1552SEric Sandeen inode->i_ino, EXT4_I(inode)); 11299f7dd93dSVasily Averin print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4, 11309f7dd93dSVasily Averin EXT4_I(inode), sizeof(struct ext4_inode_info), 11319f7dd93dSVasily Averin true); 11329f7dd93dSVasily Averin dump_stack(); 11339f7dd93dSVasily Averin } 1134ac27a0ecSDave Kleikamp } 1135ac27a0ecSDave Kleikamp 113651cc5068SAlexey Dobriyan static void init_once(void *foo) 1137ac27a0ecSDave Kleikamp { 1138617ba13bSMingming Cao struct ext4_inode_info *ei = (struct ext4_inode_info *) foo; 1139ac27a0ecSDave Kleikamp 1140ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&ei->i_orphan); 1141ac27a0ecSDave Kleikamp init_rwsem(&ei->xattr_sem); 11420e855ac8SAneesh Kumar K.V init_rwsem(&ei->i_data_sem); 1143ea3d7209SJan Kara init_rwsem(&ei->i_mmap_sem); 1144ac27a0ecSDave Kleikamp inode_init_once(&ei->vfs_inode); 1145ac27a0ecSDave Kleikamp } 1146ac27a0ecSDave Kleikamp 1147e67bc2b3SFabian Frederick static int __init init_inodecache(void) 1148ac27a0ecSDave Kleikamp { 1149f8dd7c70SDavid Windsor ext4_inode_cachep = kmem_cache_create_usercopy("ext4_inode_cache", 1150f8dd7c70SDavid Windsor sizeof(struct ext4_inode_info), 0, 1151f8dd7c70SDavid Windsor (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD| 1152f8dd7c70SDavid Windsor SLAB_ACCOUNT), 1153f8dd7c70SDavid Windsor offsetof(struct ext4_inode_info, i_data), 1154f8dd7c70SDavid Windsor sizeof_field(struct ext4_inode_info, i_data), 115520c2df83SPaul Mundt init_once); 1156617ba13bSMingming Cao if (ext4_inode_cachep == NULL) 1157ac27a0ecSDave Kleikamp return -ENOMEM; 1158ac27a0ecSDave Kleikamp return 0; 1159ac27a0ecSDave Kleikamp } 1160ac27a0ecSDave Kleikamp 1161ac27a0ecSDave Kleikamp static void destroy_inodecache(void) 1162ac27a0ecSDave Kleikamp { 11638c0a8537SKirill A. Shutemov /* 11648c0a8537SKirill A. Shutemov * Make sure all delayed rcu free inodes are flushed before we 11658c0a8537SKirill A. Shutemov * destroy cache. 11668c0a8537SKirill A. Shutemov */ 11678c0a8537SKirill A. Shutemov rcu_barrier(); 1168617ba13bSMingming Cao kmem_cache_destroy(ext4_inode_cachep); 1169ac27a0ecSDave Kleikamp } 1170ac27a0ecSDave Kleikamp 11710930fcc1SAl Viro void ext4_clear_inode(struct inode *inode) 1172ac27a0ecSDave Kleikamp { 11730930fcc1SAl Viro invalidate_inode_buffers(inode); 1174dbd5768fSJan Kara clear_inode(inode); 1175c2ea3fdeSTheodore Ts'o ext4_discard_preallocations(inode); 117651865fdaSZheng Liu ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS); 1177f4c2d372SJan Kara dquot_drop(inode); 11788aefcd55STheodore Ts'o if (EXT4_I(inode)->jinode) { 11798aefcd55STheodore Ts'o jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode), 11808aefcd55STheodore Ts'o EXT4_I(inode)->jinode); 11818aefcd55STheodore Ts'o jbd2_free_inode(EXT4_I(inode)->jinode); 11828aefcd55STheodore Ts'o EXT4_I(inode)->jinode = NULL; 11838aefcd55STheodore Ts'o } 11843d204e24SEric Biggers fscrypt_put_encryption_info(inode); 1185c93d8f88SEric Biggers fsverity_cleanup_inode(inode); 1186ac27a0ecSDave Kleikamp } 1187ac27a0ecSDave Kleikamp 11881b961ac0SChristoph Hellwig static struct inode *ext4_nfs_get_inode(struct super_block *sb, 11891b961ac0SChristoph Hellwig u64 ino, u32 generation) 1190ac27a0ecSDave Kleikamp { 1191ac27a0ecSDave Kleikamp struct inode *inode; 1192ac27a0ecSDave Kleikamp 11938a363970STheodore Ts'o /* 1194ac27a0ecSDave Kleikamp * Currently we don't know the generation for parent directory, so 1195ac27a0ecSDave Kleikamp * a generation of 0 means "accept any" 1196ac27a0ecSDave Kleikamp */ 11978a363970STheodore Ts'o inode = ext4_iget(sb, ino, EXT4_IGET_HANDLE); 11981d1fe1eeSDavid Howells if (IS_ERR(inode)) 11991d1fe1eeSDavid Howells return ERR_CAST(inode); 12001d1fe1eeSDavid Howells if (generation && inode->i_generation != generation) { 1201ac27a0ecSDave Kleikamp iput(inode); 1202ac27a0ecSDave Kleikamp return ERR_PTR(-ESTALE); 1203ac27a0ecSDave Kleikamp } 12041b961ac0SChristoph Hellwig 12051b961ac0SChristoph Hellwig return inode; 1206ac27a0ecSDave Kleikamp } 12071b961ac0SChristoph Hellwig 12081b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid, 12091b961ac0SChristoph Hellwig int fh_len, int fh_type) 12101b961ac0SChristoph Hellwig { 12111b961ac0SChristoph Hellwig return generic_fh_to_dentry(sb, fid, fh_len, fh_type, 12121b961ac0SChristoph Hellwig ext4_nfs_get_inode); 12131b961ac0SChristoph Hellwig } 12141b961ac0SChristoph Hellwig 12151b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid, 12161b961ac0SChristoph Hellwig int fh_len, int fh_type) 12171b961ac0SChristoph Hellwig { 12181b961ac0SChristoph Hellwig return generic_fh_to_parent(sb, fid, fh_len, fh_type, 12191b961ac0SChristoph Hellwig ext4_nfs_get_inode); 1220ac27a0ecSDave Kleikamp } 1221ac27a0ecSDave Kleikamp 1222fde87268STheodore Ts'o static int ext4_nfs_commit_metadata(struct inode *inode) 1223fde87268STheodore Ts'o { 1224fde87268STheodore Ts'o struct writeback_control wbc = { 1225fde87268STheodore Ts'o .sync_mode = WB_SYNC_ALL 1226fde87268STheodore Ts'o }; 1227fde87268STheodore Ts'o 1228fde87268STheodore Ts'o trace_ext4_nfs_commit_metadata(inode); 1229fde87268STheodore Ts'o return ext4_write_inode(inode, &wbc); 1230fde87268STheodore Ts'o } 1231fde87268STheodore Ts'o 1232c39a7f84SToshiyuki Okajima /* 1233c39a7f84SToshiyuki Okajima * Try to release metadata pages (indirect blocks, directories) which are 1234c39a7f84SToshiyuki Okajima * mapped via the block device. Since these pages could have journal heads 1235c39a7f84SToshiyuki Okajima * which would prevent try_to_free_buffers() from freeing them, we must use 1236c39a7f84SToshiyuki Okajima * jbd2 layer's try_to_free_buffers() function to release them. 1237c39a7f84SToshiyuki Okajima */ 12380b8e58a1SAndreas Dilger static int bdev_try_to_free_page(struct super_block *sb, struct page *page, 12390b8e58a1SAndreas Dilger gfp_t wait) 1240c39a7f84SToshiyuki Okajima { 1241c39a7f84SToshiyuki Okajima journal_t *journal = EXT4_SB(sb)->s_journal; 1242c39a7f84SToshiyuki Okajima 1243c39a7f84SToshiyuki Okajima WARN_ON(PageChecked(page)); 1244c39a7f84SToshiyuki Okajima if (!page_has_buffers(page)) 1245c39a7f84SToshiyuki Okajima return 0; 1246c39a7f84SToshiyuki Okajima if (journal) 1247c39a7f84SToshiyuki Okajima return jbd2_journal_try_to_free_buffers(journal, page, 1248d0164adcSMel Gorman wait & ~__GFP_DIRECT_RECLAIM); 1249c39a7f84SToshiyuki Okajima return try_to_free_buffers(page); 1250c39a7f84SToshiyuki Okajima } 1251c39a7f84SToshiyuki Okajima 1252643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 1253a7550b30SJaegeuk Kim static int ext4_get_context(struct inode *inode, void *ctx, size_t len) 1254a7550b30SJaegeuk Kim { 1255a7550b30SJaegeuk Kim return ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION, 1256a7550b30SJaegeuk Kim EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, ctx, len); 1257a7550b30SJaegeuk Kim } 1258a7550b30SJaegeuk Kim 1259a7550b30SJaegeuk Kim static int ext4_set_context(struct inode *inode, const void *ctx, size_t len, 1260a7550b30SJaegeuk Kim void *fs_data) 1261a7550b30SJaegeuk Kim { 12622f8f5e76SEric Biggers handle_t *handle = fs_data; 1263c1a5d5f6STahsin Erdogan int res, res2, credits, retries = 0; 1264a7550b30SJaegeuk Kim 12659ce0151aSEric Biggers /* 12669ce0151aSEric Biggers * Encrypting the root directory is not allowed because e2fsck expects 12679ce0151aSEric Biggers * lost+found to exist and be unencrypted, and encrypting the root 12689ce0151aSEric Biggers * directory would imply encrypting the lost+found directory as well as 12699ce0151aSEric Biggers * the filename "lost+found" itself. 12709ce0151aSEric Biggers */ 12719ce0151aSEric Biggers if (inode->i_ino == EXT4_ROOT_INO) 12729ce0151aSEric Biggers return -EPERM; 1273a7550b30SJaegeuk Kim 12747d3e06a8SRoss Zwisler if (WARN_ON_ONCE(IS_DAX(inode) && i_size_read(inode))) 12757d3e06a8SRoss Zwisler return -EINVAL; 12767d3e06a8SRoss Zwisler 127794840e3cSEric Biggers res = ext4_convert_inline_data(inode); 127894840e3cSEric Biggers if (res) 127994840e3cSEric Biggers return res; 128094840e3cSEric Biggers 12812f8f5e76SEric Biggers /* 12822f8f5e76SEric Biggers * If a journal handle was specified, then the encryption context is 12832f8f5e76SEric Biggers * being set on a new inode via inheritance and is part of a larger 12842f8f5e76SEric Biggers * transaction to create the inode. Otherwise the encryption context is 12852f8f5e76SEric Biggers * being set on an existing inode in its own transaction. Only in the 12862f8f5e76SEric Biggers * latter case should the "retry on ENOSPC" logic be used. 12872f8f5e76SEric Biggers */ 12882f8f5e76SEric Biggers 12892f8f5e76SEric Biggers if (handle) { 12902f8f5e76SEric Biggers res = ext4_xattr_set_handle(handle, inode, 12912f8f5e76SEric Biggers EXT4_XATTR_INDEX_ENCRYPTION, 12922f8f5e76SEric Biggers EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, 12932f8f5e76SEric Biggers ctx, len, 0); 1294a7550b30SJaegeuk Kim if (!res) { 1295a7550b30SJaegeuk Kim ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT); 1296a7550b30SJaegeuk Kim ext4_clear_inode_state(inode, 1297a7550b30SJaegeuk Kim EXT4_STATE_MAY_INLINE_DATA); 1298a3caa24bSJan Kara /* 12992ee6a576SEric Biggers * Update inode->i_flags - S_ENCRYPTED will be enabled, 13002ee6a576SEric Biggers * S_DAX may be disabled 1301a3caa24bSJan Kara */ 1302a3caa24bSJan Kara ext4_set_inode_flags(inode); 1303a7550b30SJaegeuk Kim } 1304a7550b30SJaegeuk Kim return res; 1305a7550b30SJaegeuk Kim } 1306a7550b30SJaegeuk Kim 1307b8cb5a54STahsin Erdogan res = dquot_initialize(inode); 1308b8cb5a54STahsin Erdogan if (res) 1309b8cb5a54STahsin Erdogan return res; 13102f8f5e76SEric Biggers retry: 1311af65207cSTahsin Erdogan res = ext4_xattr_set_credits(inode, len, false /* is_create */, 1312af65207cSTahsin Erdogan &credits); 1313dec214d0STahsin Erdogan if (res) 1314dec214d0STahsin Erdogan return res; 1315dec214d0STahsin Erdogan 1316c1a5d5f6STahsin Erdogan handle = ext4_journal_start(inode, EXT4_HT_MISC, credits); 1317a7550b30SJaegeuk Kim if (IS_ERR(handle)) 1318a7550b30SJaegeuk Kim return PTR_ERR(handle); 1319a7550b30SJaegeuk Kim 13202f8f5e76SEric Biggers res = ext4_xattr_set_handle(handle, inode, EXT4_XATTR_INDEX_ENCRYPTION, 13212f8f5e76SEric Biggers EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, 13222f8f5e76SEric Biggers ctx, len, 0); 1323a7550b30SJaegeuk Kim if (!res) { 1324a7550b30SJaegeuk Kim ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT); 13252ee6a576SEric Biggers /* 13262ee6a576SEric Biggers * Update inode->i_flags - S_ENCRYPTED will be enabled, 13272ee6a576SEric Biggers * S_DAX may be disabled 13282ee6a576SEric Biggers */ 1329a3caa24bSJan Kara ext4_set_inode_flags(inode); 1330a7550b30SJaegeuk Kim res = ext4_mark_inode_dirty(handle, inode); 1331a7550b30SJaegeuk Kim if (res) 1332a7550b30SJaegeuk Kim EXT4_ERROR_INODE(inode, "Failed to mark inode dirty"); 1333a7550b30SJaegeuk Kim } 1334a7550b30SJaegeuk Kim res2 = ext4_journal_stop(handle); 13352f8f5e76SEric Biggers 13362f8f5e76SEric Biggers if (res == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 13372f8f5e76SEric Biggers goto retry; 1338a7550b30SJaegeuk Kim if (!res) 1339a7550b30SJaegeuk Kim res = res2; 1340a7550b30SJaegeuk Kim return res; 1341a7550b30SJaegeuk Kim } 1342a7550b30SJaegeuk Kim 1343c250b7ddSEric Biggers static bool ext4_dummy_context(struct inode *inode) 1344a7550b30SJaegeuk Kim { 1345a7550b30SJaegeuk Kim return DUMMY_ENCRYPTION_ENABLED(EXT4_SB(inode->i_sb)); 1346a7550b30SJaegeuk Kim } 1347a7550b30SJaegeuk Kim 13486f69f0edSEric Biggers static const struct fscrypt_operations ext4_cryptops = { 1349a5d431efSEric Biggers .key_prefix = "ext4:", 1350a7550b30SJaegeuk Kim .get_context = ext4_get_context, 1351a7550b30SJaegeuk Kim .set_context = ext4_set_context, 1352a7550b30SJaegeuk Kim .dummy_context = ext4_dummy_context, 1353a7550b30SJaegeuk Kim .empty_dir = ext4_empty_dir, 1354e12ee683SEric Biggers .max_namelen = EXT4_NAME_LEN, 1355a7550b30SJaegeuk Kim }; 1356a7550b30SJaegeuk Kim #endif 1357a7550b30SJaegeuk Kim 1358ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 1359d6006186SEric Biggers static const char * const quotatypes[] = INITQFNAMES; 1360689c958cSLi Xi #define QTYPE2NAME(t) (quotatypes[t]) 1361ac27a0ecSDave Kleikamp 1362617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot); 1363617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot); 1364617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot); 1365617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot); 1366617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type); 13676f28e087SJan Kara static int ext4_quota_on(struct super_block *sb, int type, int format_id, 13688c54ca9cSAl Viro const struct path *path); 1369617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type); 1370617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data, 1371ac27a0ecSDave Kleikamp size_t len, loff_t off); 1372617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type, 1373ac27a0ecSDave Kleikamp const char *data, size_t len, loff_t off); 13747c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id, 13757c319d32SAditya Kali unsigned int flags); 13767c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb); 1377ac27a0ecSDave Kleikamp 137896c7e0d9SJan Kara static struct dquot **ext4_get_dquots(struct inode *inode) 137996c7e0d9SJan Kara { 138096c7e0d9SJan Kara return EXT4_I(inode)->i_dquot; 138196c7e0d9SJan Kara } 138296c7e0d9SJan Kara 138361e225dcSAlexey Dobriyan static const struct dquot_operations ext4_quota_operations = { 138460e58e0fSMingming Cao .get_reserved_space = ext4_get_reserved_space, 1385617ba13bSMingming Cao .write_dquot = ext4_write_dquot, 1386617ba13bSMingming Cao .acquire_dquot = ext4_acquire_dquot, 1387617ba13bSMingming Cao .release_dquot = ext4_release_dquot, 1388617ba13bSMingming Cao .mark_dirty = ext4_mark_dquot_dirty, 1389a5b5ee32SJan Kara .write_info = ext4_write_info, 1390a5b5ee32SJan Kara .alloc_dquot = dquot_alloc, 1391a5b5ee32SJan Kara .destroy_dquot = dquot_destroy, 1392040cb378SLi Xi .get_projid = ext4_get_projid, 13937a9ca53aSTahsin Erdogan .get_inode_usage = ext4_get_inode_usage, 1394*ebc11f7bSChengguang Xu .get_next_id = dquot_get_next_id, 1395ac27a0ecSDave Kleikamp }; 1396ac27a0ecSDave Kleikamp 13970d54b217SAlexey Dobriyan static const struct quotactl_ops ext4_qctl_operations = { 1398617ba13bSMingming Cao .quota_on = ext4_quota_on, 1399ca0e05e4SDmitry Monakhov .quota_off = ext4_quota_off, 1400287a8095SChristoph Hellwig .quota_sync = dquot_quota_sync, 14010a240339SJan Kara .get_state = dquot_get_state, 1402287a8095SChristoph Hellwig .set_info = dquot_set_dqinfo, 1403287a8095SChristoph Hellwig .get_dqblk = dquot_get_dqblk, 14046332b9b5SEric Sandeen .set_dqblk = dquot_set_dqblk, 14056332b9b5SEric Sandeen .get_nextdqblk = dquot_get_next_dqblk, 1406ac27a0ecSDave Kleikamp }; 1407ac27a0ecSDave Kleikamp #endif 1408ac27a0ecSDave Kleikamp 1409ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations ext4_sops = { 1410617ba13bSMingming Cao .alloc_inode = ext4_alloc_inode, 141194053139SAl Viro .free_inode = ext4_free_in_core_inode, 1412617ba13bSMingming Cao .destroy_inode = ext4_destroy_inode, 1413617ba13bSMingming Cao .write_inode = ext4_write_inode, 1414617ba13bSMingming Cao .dirty_inode = ext4_dirty_inode, 14157ff9c073STheodore Ts'o .drop_inode = ext4_drop_inode, 14160930fcc1SAl Viro .evict_inode = ext4_evict_inode, 1417617ba13bSMingming Cao .put_super = ext4_put_super, 1418617ba13bSMingming Cao .sync_fs = ext4_sync_fs, 1419c4be0c1dSTakashi Sato .freeze_fs = ext4_freeze, 1420c4be0c1dSTakashi Sato .unfreeze_fs = ext4_unfreeze, 1421617ba13bSMingming Cao .statfs = ext4_statfs, 1422617ba13bSMingming Cao .remount_fs = ext4_remount, 1423617ba13bSMingming Cao .show_options = ext4_show_options, 1424ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 1425617ba13bSMingming Cao .quota_read = ext4_quota_read, 1426617ba13bSMingming Cao .quota_write = ext4_quota_write, 142796c7e0d9SJan Kara .get_dquots = ext4_get_dquots, 1428ac27a0ecSDave Kleikamp #endif 1429c39a7f84SToshiyuki Okajima .bdev_try_to_free_page = bdev_try_to_free_page, 1430ac27a0ecSDave Kleikamp }; 1431ac27a0ecSDave Kleikamp 143239655164SChristoph Hellwig static const struct export_operations ext4_export_ops = { 14331b961ac0SChristoph Hellwig .fh_to_dentry = ext4_fh_to_dentry, 14341b961ac0SChristoph Hellwig .fh_to_parent = ext4_fh_to_parent, 1435617ba13bSMingming Cao .get_parent = ext4_get_parent, 1436fde87268STheodore Ts'o .commit_metadata = ext4_nfs_commit_metadata, 1437ac27a0ecSDave Kleikamp }; 1438ac27a0ecSDave Kleikamp 1439ac27a0ecSDave Kleikamp enum { 1440ac27a0ecSDave Kleikamp Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid, 1441ac27a0ecSDave Kleikamp Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro, 144272578c33STheodore Ts'o Opt_nouid32, Opt_debug, Opt_removed, 1443ac27a0ecSDave Kleikamp Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl, 144472578c33STheodore Ts'o Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload, 1445ad4eec61SEric Sandeen Opt_commit, Opt_min_batch_time, Opt_max_batch_time, Opt_journal_dev, 1446ad4eec61SEric Sandeen Opt_journal_path, Opt_journal_checksum, Opt_journal_async_commit, 1447ac27a0ecSDave Kleikamp Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback, 14486ddb2447STheodore Ts'o Opt_data_err_abort, Opt_data_err_ignore, Opt_test_dummy_encryption, 1449ac27a0ecSDave Kleikamp Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota, 14505a20bdfcSJan Kara Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota, 1451ee4a3fcdSTheodore Ts'o Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err, 145249da9392SJan Kara Opt_usrquota, Opt_grpquota, Opt_prjquota, Opt_i_version, Opt_dax, 1453327eaf73STheodore Ts'o Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_warn_on_error, 1454327eaf73STheodore Ts'o Opt_nowarn_on_error, Opt_mblk_io_submit, 1455670e9875STheodore Ts'o Opt_lazytime, Opt_nolazytime, Opt_debug_want_extra_isize, 14561449032bSTheodore Ts'o Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity, 14575328e635SEric Sandeen Opt_inode_readahead_blks, Opt_journal_ioprio, 1458744692dcSJiaying Zhang Opt_dioread_nolock, Opt_dioread_lock, 1459fc6cb1cdSTheodore Ts'o Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable, 1460cdb7ee4cSTahsin Erdogan Opt_max_dir_size_kb, Opt_nojournal_checksum, Opt_nombcache, 1461ac27a0ecSDave Kleikamp }; 1462ac27a0ecSDave Kleikamp 1463a447c093SSteven Whitehouse static const match_table_t tokens = { 1464ac27a0ecSDave Kleikamp {Opt_bsd_df, "bsddf"}, 1465ac27a0ecSDave Kleikamp {Opt_minix_df, "minixdf"}, 1466ac27a0ecSDave Kleikamp {Opt_grpid, "grpid"}, 1467ac27a0ecSDave Kleikamp {Opt_grpid, "bsdgroups"}, 1468ac27a0ecSDave Kleikamp {Opt_nogrpid, "nogrpid"}, 1469ac27a0ecSDave Kleikamp {Opt_nogrpid, "sysvgroups"}, 1470ac27a0ecSDave Kleikamp {Opt_resgid, "resgid=%u"}, 1471ac27a0ecSDave Kleikamp {Opt_resuid, "resuid=%u"}, 1472ac27a0ecSDave Kleikamp {Opt_sb, "sb=%u"}, 1473ac27a0ecSDave Kleikamp {Opt_err_cont, "errors=continue"}, 1474ac27a0ecSDave Kleikamp {Opt_err_panic, "errors=panic"}, 1475ac27a0ecSDave Kleikamp {Opt_err_ro, "errors=remount-ro"}, 1476ac27a0ecSDave Kleikamp {Opt_nouid32, "nouid32"}, 1477ac27a0ecSDave Kleikamp {Opt_debug, "debug"}, 147872578c33STheodore Ts'o {Opt_removed, "oldalloc"}, 147972578c33STheodore Ts'o {Opt_removed, "orlov"}, 1480ac27a0ecSDave Kleikamp {Opt_user_xattr, "user_xattr"}, 1481ac27a0ecSDave Kleikamp {Opt_nouser_xattr, "nouser_xattr"}, 1482ac27a0ecSDave Kleikamp {Opt_acl, "acl"}, 1483ac27a0ecSDave Kleikamp {Opt_noacl, "noacl"}, 1484e3bb52aeSEric Sandeen {Opt_noload, "norecovery"}, 14855a916be1STheodore Ts'o {Opt_noload, "noload"}, 148672578c33STheodore Ts'o {Opt_removed, "nobh"}, 148772578c33STheodore Ts'o {Opt_removed, "bh"}, 1488ac27a0ecSDave Kleikamp {Opt_commit, "commit=%u"}, 148930773840STheodore Ts'o {Opt_min_batch_time, "min_batch_time=%u"}, 149030773840STheodore Ts'o {Opt_max_batch_time, "max_batch_time=%u"}, 1491ac27a0ecSDave Kleikamp {Opt_journal_dev, "journal_dev=%u"}, 1492ad4eec61SEric Sandeen {Opt_journal_path, "journal_path=%s"}, 1493818d276cSGirish Shilamkar {Opt_journal_checksum, "journal_checksum"}, 1494c6d3d56dSDarrick J. Wong {Opt_nojournal_checksum, "nojournal_checksum"}, 1495818d276cSGirish Shilamkar {Opt_journal_async_commit, "journal_async_commit"}, 1496ac27a0ecSDave Kleikamp {Opt_abort, "abort"}, 1497ac27a0ecSDave Kleikamp {Opt_data_journal, "data=journal"}, 1498ac27a0ecSDave Kleikamp {Opt_data_ordered, "data=ordered"}, 1499ac27a0ecSDave Kleikamp {Opt_data_writeback, "data=writeback"}, 15005bf5683aSHidehiro Kawai {Opt_data_err_abort, "data_err=abort"}, 15015bf5683aSHidehiro Kawai {Opt_data_err_ignore, "data_err=ignore"}, 1502ac27a0ecSDave Kleikamp {Opt_offusrjquota, "usrjquota="}, 1503ac27a0ecSDave Kleikamp {Opt_usrjquota, "usrjquota=%s"}, 1504ac27a0ecSDave Kleikamp {Opt_offgrpjquota, "grpjquota="}, 1505ac27a0ecSDave Kleikamp {Opt_grpjquota, "grpjquota=%s"}, 1506ac27a0ecSDave Kleikamp {Opt_jqfmt_vfsold, "jqfmt=vfsold"}, 1507ac27a0ecSDave Kleikamp {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"}, 15085a20bdfcSJan Kara {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"}, 1509ac27a0ecSDave Kleikamp {Opt_grpquota, "grpquota"}, 1510ac27a0ecSDave Kleikamp {Opt_noquota, "noquota"}, 1511ac27a0ecSDave Kleikamp {Opt_quota, "quota"}, 1512ac27a0ecSDave Kleikamp {Opt_usrquota, "usrquota"}, 151349da9392SJan Kara {Opt_prjquota, "prjquota"}, 1514ac27a0ecSDave Kleikamp {Opt_barrier, "barrier=%u"}, 151506705bffSTheodore Ts'o {Opt_barrier, "barrier"}, 151606705bffSTheodore Ts'o {Opt_nobarrier, "nobarrier"}, 151725ec56b5SJean Noel Cordenner {Opt_i_version, "i_version"}, 1518923ae0ffSRoss Zwisler {Opt_dax, "dax"}, 1519c9de560dSAlex Tomas {Opt_stripe, "stripe=%u"}, 152064769240SAlex Tomas {Opt_delalloc, "delalloc"}, 1521327eaf73STheodore Ts'o {Opt_warn_on_error, "warn_on_error"}, 1522327eaf73STheodore Ts'o {Opt_nowarn_on_error, "nowarn_on_error"}, 1523a26f4992STheodore Ts'o {Opt_lazytime, "lazytime"}, 1524a26f4992STheodore Ts'o {Opt_nolazytime, "nolazytime"}, 1525670e9875STheodore Ts'o {Opt_debug_want_extra_isize, "debug_want_extra_isize=%u"}, 1526dd919b98SAneesh Kumar K.V {Opt_nodelalloc, "nodelalloc"}, 152736ade451SJan Kara {Opt_removed, "mblk_io_submit"}, 152836ade451SJan Kara {Opt_removed, "nomblk_io_submit"}, 15296fd058f7STheodore Ts'o {Opt_block_validity, "block_validity"}, 15306fd058f7STheodore Ts'o {Opt_noblock_validity, "noblock_validity"}, 1531240799cdSTheodore Ts'o {Opt_inode_readahead_blks, "inode_readahead_blks=%u"}, 1532b3881f74STheodore Ts'o {Opt_journal_ioprio, "journal_ioprio=%u"}, 1533afd4672dSTheodore Ts'o {Opt_auto_da_alloc, "auto_da_alloc=%u"}, 153406705bffSTheodore Ts'o {Opt_auto_da_alloc, "auto_da_alloc"}, 153506705bffSTheodore Ts'o {Opt_noauto_da_alloc, "noauto_da_alloc"}, 1536744692dcSJiaying Zhang {Opt_dioread_nolock, "dioread_nolock"}, 1537744692dcSJiaying Zhang {Opt_dioread_lock, "dioread_lock"}, 15385328e635SEric Sandeen {Opt_discard, "discard"}, 15395328e635SEric Sandeen {Opt_nodiscard, "nodiscard"}, 1540fc6cb1cdSTheodore Ts'o {Opt_init_itable, "init_itable=%u"}, 1541fc6cb1cdSTheodore Ts'o {Opt_init_itable, "init_itable"}, 1542fc6cb1cdSTheodore Ts'o {Opt_noinit_itable, "noinit_itable"}, 1543df981d03STheodore Ts'o {Opt_max_dir_size_kb, "max_dir_size_kb=%u"}, 15446ddb2447STheodore Ts'o {Opt_test_dummy_encryption, "test_dummy_encryption"}, 1545cdb7ee4cSTahsin Erdogan {Opt_nombcache, "nombcache"}, 1546cdb7ee4cSTahsin Erdogan {Opt_nombcache, "no_mbcache"}, /* for backward compatibility */ 1547c7198b9cSTheodore Ts'o {Opt_removed, "check=none"}, /* mount option from ext2/3 */ 1548c7198b9cSTheodore Ts'o {Opt_removed, "nocheck"}, /* mount option from ext2/3 */ 1549c7198b9cSTheodore Ts'o {Opt_removed, "reservation"}, /* mount option from ext2/3 */ 1550c7198b9cSTheodore Ts'o {Opt_removed, "noreservation"}, /* mount option from ext2/3 */ 1551c7198b9cSTheodore Ts'o {Opt_removed, "journal=%u"}, /* mount option from ext2/3 */ 1552f3f12faaSJosef Bacik {Opt_err, NULL}, 1553ac27a0ecSDave Kleikamp }; 1554ac27a0ecSDave Kleikamp 1555617ba13bSMingming Cao static ext4_fsblk_t get_sb_block(void **data) 1556ac27a0ecSDave Kleikamp { 1557617ba13bSMingming Cao ext4_fsblk_t sb_block; 1558ac27a0ecSDave Kleikamp char *options = (char *) *data; 1559ac27a0ecSDave Kleikamp 1560ac27a0ecSDave Kleikamp if (!options || strncmp(options, "sb=", 3) != 0) 1561ac27a0ecSDave Kleikamp return 1; /* Default location */ 15620b8e58a1SAndreas Dilger 1563ac27a0ecSDave Kleikamp options += 3; 15640b8e58a1SAndreas Dilger /* TODO: use simple_strtoll with >32bit ext4 */ 1565ac27a0ecSDave Kleikamp sb_block = simple_strtoul(options, &options, 0); 1566ac27a0ecSDave Kleikamp if (*options && *options != ',') { 15674776004fSTheodore Ts'o printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n", 1568ac27a0ecSDave Kleikamp (char *) *data); 1569ac27a0ecSDave Kleikamp return 1; 1570ac27a0ecSDave Kleikamp } 1571ac27a0ecSDave Kleikamp if (*options == ',') 1572ac27a0ecSDave Kleikamp options++; 1573ac27a0ecSDave Kleikamp *data = (void *) options; 15740b8e58a1SAndreas Dilger 1575ac27a0ecSDave Kleikamp return sb_block; 1576ac27a0ecSDave Kleikamp } 1577ac27a0ecSDave Kleikamp 1578b3881f74STheodore Ts'o #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3)) 1579d6006186SEric Biggers static const char deprecated_msg[] = 1580d6006186SEric Biggers "Mount option \"%s\" will be removed by %s\n" 1581437ca0fdSDmitry Monakhov "Contact linux-ext4@vger.kernel.org if you think we should keep it.\n"; 1582b3881f74STheodore Ts'o 158356c50f11SDmitry Monakhov #ifdef CONFIG_QUOTA 158456c50f11SDmitry Monakhov static int set_qf_name(struct super_block *sb, int qtype, substring_t *args) 158556c50f11SDmitry Monakhov { 158656c50f11SDmitry Monakhov struct ext4_sb_info *sbi = EXT4_SB(sb); 158733458eabSTheodore Ts'o char *qname, *old_qname = get_qf_name(sb, sbi, qtype); 158803dafb5fSChen Gang int ret = -1; 158956c50f11SDmitry Monakhov 159033458eabSTheodore Ts'o if (sb_any_quota_loaded(sb) && !old_qname) { 159156c50f11SDmitry Monakhov ext4_msg(sb, KERN_ERR, 159256c50f11SDmitry Monakhov "Cannot change journaled " 159356c50f11SDmitry Monakhov "quota options when quota turned on"); 159457f73c2cSTheodore Ts'o return -1; 159556c50f11SDmitry Monakhov } 1596e2b911c5SDarrick J. Wong if (ext4_has_feature_quota(sb)) { 1597c325a67cSTheodore Ts'o ext4_msg(sb, KERN_INFO, "Journaled quota options " 1598c325a67cSTheodore Ts'o "ignored when QUOTA feature is enabled"); 1599c325a67cSTheodore Ts'o return 1; 1600262b4662SJan Kara } 160156c50f11SDmitry Monakhov qname = match_strdup(args); 160256c50f11SDmitry Monakhov if (!qname) { 160356c50f11SDmitry Monakhov ext4_msg(sb, KERN_ERR, 160456c50f11SDmitry Monakhov "Not enough memory for storing quotafile name"); 160557f73c2cSTheodore Ts'o return -1; 160656c50f11SDmitry Monakhov } 160733458eabSTheodore Ts'o if (old_qname) { 160833458eabSTheodore Ts'o if (strcmp(old_qname, qname) == 0) 160903dafb5fSChen Gang ret = 1; 161003dafb5fSChen Gang else 161156c50f11SDmitry Monakhov ext4_msg(sb, KERN_ERR, 161203dafb5fSChen Gang "%s quota file already specified", 161303dafb5fSChen Gang QTYPE2NAME(qtype)); 161403dafb5fSChen Gang goto errout; 161556c50f11SDmitry Monakhov } 161603dafb5fSChen Gang if (strchr(qname, '/')) { 161756c50f11SDmitry Monakhov ext4_msg(sb, KERN_ERR, 161856c50f11SDmitry Monakhov "quotafile must be on filesystem root"); 161903dafb5fSChen Gang goto errout; 162056c50f11SDmitry Monakhov } 162133458eabSTheodore Ts'o rcu_assign_pointer(sbi->s_qf_names[qtype], qname); 1622fd8c37ecSTheodore Ts'o set_opt(sb, QUOTA); 162356c50f11SDmitry Monakhov return 1; 162403dafb5fSChen Gang errout: 162503dafb5fSChen Gang kfree(qname); 162603dafb5fSChen Gang return ret; 162756c50f11SDmitry Monakhov } 162856c50f11SDmitry Monakhov 162956c50f11SDmitry Monakhov static int clear_qf_name(struct super_block *sb, int qtype) 163056c50f11SDmitry Monakhov { 163156c50f11SDmitry Monakhov 163256c50f11SDmitry Monakhov struct ext4_sb_info *sbi = EXT4_SB(sb); 163333458eabSTheodore Ts'o char *old_qname = get_qf_name(sb, sbi, qtype); 163456c50f11SDmitry Monakhov 163533458eabSTheodore Ts'o if (sb_any_quota_loaded(sb) && old_qname) { 163656c50f11SDmitry Monakhov ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options" 163756c50f11SDmitry Monakhov " when quota turned on"); 163857f73c2cSTheodore Ts'o return -1; 163956c50f11SDmitry Monakhov } 164033458eabSTheodore Ts'o rcu_assign_pointer(sbi->s_qf_names[qtype], NULL); 164133458eabSTheodore Ts'o synchronize_rcu(); 164233458eabSTheodore Ts'o kfree(old_qname); 164356c50f11SDmitry Monakhov return 1; 164456c50f11SDmitry Monakhov } 164556c50f11SDmitry Monakhov #endif 164656c50f11SDmitry Monakhov 164726092bf5STheodore Ts'o #define MOPT_SET 0x0001 164826092bf5STheodore Ts'o #define MOPT_CLEAR 0x0002 164926092bf5STheodore Ts'o #define MOPT_NOSUPPORT 0x0004 165026092bf5STheodore Ts'o #define MOPT_EXPLICIT 0x0008 165126092bf5STheodore Ts'o #define MOPT_CLEAR_ERR 0x0010 165226092bf5STheodore Ts'o #define MOPT_GTE0 0x0020 165326092bf5STheodore Ts'o #ifdef CONFIG_QUOTA 165426092bf5STheodore Ts'o #define MOPT_Q 0 165526092bf5STheodore Ts'o #define MOPT_QFMT 0x0040 165626092bf5STheodore Ts'o #else 165726092bf5STheodore Ts'o #define MOPT_Q MOPT_NOSUPPORT 165826092bf5STheodore Ts'o #define MOPT_QFMT MOPT_NOSUPPORT 165926092bf5STheodore Ts'o #endif 166026092bf5STheodore Ts'o #define MOPT_DATAJ 0x0080 16618dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT2 0x0100 16628dc0aa8cSTheodore Ts'o #define MOPT_NO_EXT3 0x0200 16638dc0aa8cSTheodore Ts'o #define MOPT_EXT4_ONLY (MOPT_NO_EXT2 | MOPT_NO_EXT3) 1664ad4eec61SEric Sandeen #define MOPT_STRING 0x0400 166526092bf5STheodore Ts'o 166626092bf5STheodore Ts'o static const struct mount_opts { 166726092bf5STheodore Ts'o int token; 166826092bf5STheodore Ts'o int mount_opt; 166926092bf5STheodore Ts'o int flags; 167026092bf5STheodore Ts'o } ext4_mount_opts[] = { 167126092bf5STheodore Ts'o {Opt_minix_df, EXT4_MOUNT_MINIX_DF, MOPT_SET}, 167226092bf5STheodore Ts'o {Opt_bsd_df, EXT4_MOUNT_MINIX_DF, MOPT_CLEAR}, 167326092bf5STheodore Ts'o {Opt_grpid, EXT4_MOUNT_GRPID, MOPT_SET}, 167426092bf5STheodore Ts'o {Opt_nogrpid, EXT4_MOUNT_GRPID, MOPT_CLEAR}, 167526092bf5STheodore Ts'o {Opt_block_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_SET}, 167626092bf5STheodore Ts'o {Opt_noblock_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_CLEAR}, 16778dc0aa8cSTheodore Ts'o {Opt_dioread_nolock, EXT4_MOUNT_DIOREAD_NOLOCK, 16788dc0aa8cSTheodore Ts'o MOPT_EXT4_ONLY | MOPT_SET}, 16798dc0aa8cSTheodore Ts'o {Opt_dioread_lock, EXT4_MOUNT_DIOREAD_NOLOCK, 16808dc0aa8cSTheodore Ts'o MOPT_EXT4_ONLY | MOPT_CLEAR}, 168126092bf5STheodore Ts'o {Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET}, 168226092bf5STheodore Ts'o {Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR}, 16838dc0aa8cSTheodore Ts'o {Opt_delalloc, EXT4_MOUNT_DELALLOC, 16848dc0aa8cSTheodore Ts'o MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT}, 16858dc0aa8cSTheodore Ts'o {Opt_nodelalloc, EXT4_MOUNT_DELALLOC, 168659d9fa5cSTheodore Ts'o MOPT_EXT4_ONLY | MOPT_CLEAR}, 1687327eaf73STheodore Ts'o {Opt_warn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_SET}, 1688327eaf73STheodore Ts'o {Opt_nowarn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_CLEAR}, 1689c6d3d56dSDarrick J. Wong {Opt_nojournal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM, 1690c6d3d56dSDarrick J. Wong MOPT_EXT4_ONLY | MOPT_CLEAR}, 16918dc0aa8cSTheodore Ts'o {Opt_journal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM, 16921e381f60SDmitry Monakhov MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT}, 169326092bf5STheodore Ts'o {Opt_journal_async_commit, (EXT4_MOUNT_JOURNAL_ASYNC_COMMIT | 16948dc0aa8cSTheodore Ts'o EXT4_MOUNT_JOURNAL_CHECKSUM), 16951e381f60SDmitry Monakhov MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT}, 16968dc0aa8cSTheodore Ts'o {Opt_noload, EXT4_MOUNT_NOLOAD, MOPT_NO_EXT2 | MOPT_SET}, 169726092bf5STheodore Ts'o {Opt_err_panic, EXT4_MOUNT_ERRORS_PANIC, MOPT_SET | MOPT_CLEAR_ERR}, 169826092bf5STheodore Ts'o {Opt_err_ro, EXT4_MOUNT_ERRORS_RO, MOPT_SET | MOPT_CLEAR_ERR}, 169926092bf5STheodore Ts'o {Opt_err_cont, EXT4_MOUNT_ERRORS_CONT, MOPT_SET | MOPT_CLEAR_ERR}, 17008dc0aa8cSTheodore Ts'o {Opt_data_err_abort, EXT4_MOUNT_DATA_ERR_ABORT, 17017915a861SAles Novak MOPT_NO_EXT2}, 17028dc0aa8cSTheodore Ts'o {Opt_data_err_ignore, EXT4_MOUNT_DATA_ERR_ABORT, 17037915a861SAles Novak MOPT_NO_EXT2}, 170426092bf5STheodore Ts'o {Opt_barrier, EXT4_MOUNT_BARRIER, MOPT_SET}, 170526092bf5STheodore Ts'o {Opt_nobarrier, EXT4_MOUNT_BARRIER, MOPT_CLEAR}, 170626092bf5STheodore Ts'o {Opt_noauto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_SET}, 170726092bf5STheodore Ts'o {Opt_auto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_CLEAR}, 170826092bf5STheodore Ts'o {Opt_noinit_itable, EXT4_MOUNT_INIT_INODE_TABLE, MOPT_CLEAR}, 170926092bf5STheodore Ts'o {Opt_commit, 0, MOPT_GTE0}, 171026092bf5STheodore Ts'o {Opt_max_batch_time, 0, MOPT_GTE0}, 171126092bf5STheodore Ts'o {Opt_min_batch_time, 0, MOPT_GTE0}, 171226092bf5STheodore Ts'o {Opt_inode_readahead_blks, 0, MOPT_GTE0}, 171326092bf5STheodore Ts'o {Opt_init_itable, 0, MOPT_GTE0}, 1714923ae0ffSRoss Zwisler {Opt_dax, EXT4_MOUNT_DAX, MOPT_SET}, 171526092bf5STheodore Ts'o {Opt_stripe, 0, MOPT_GTE0}, 17160efb3b23SJan Kara {Opt_resuid, 0, MOPT_GTE0}, 17170efb3b23SJan Kara {Opt_resgid, 0, MOPT_GTE0}, 17185ba92bcfSCarlos Maiolino {Opt_journal_dev, 0, MOPT_NO_EXT2 | MOPT_GTE0}, 17195ba92bcfSCarlos Maiolino {Opt_journal_path, 0, MOPT_NO_EXT2 | MOPT_STRING}, 17205ba92bcfSCarlos Maiolino {Opt_journal_ioprio, 0, MOPT_NO_EXT2 | MOPT_GTE0}, 17218dc0aa8cSTheodore Ts'o {Opt_data_journal, EXT4_MOUNT_JOURNAL_DATA, MOPT_NO_EXT2 | MOPT_DATAJ}, 17228dc0aa8cSTheodore Ts'o {Opt_data_ordered, EXT4_MOUNT_ORDERED_DATA, MOPT_NO_EXT2 | MOPT_DATAJ}, 17238dc0aa8cSTheodore Ts'o {Opt_data_writeback, EXT4_MOUNT_WRITEBACK_DATA, 17248dc0aa8cSTheodore Ts'o MOPT_NO_EXT2 | MOPT_DATAJ}, 172526092bf5STheodore Ts'o {Opt_user_xattr, EXT4_MOUNT_XATTR_USER, MOPT_SET}, 172626092bf5STheodore Ts'o {Opt_nouser_xattr, EXT4_MOUNT_XATTR_USER, MOPT_CLEAR}, 172726092bf5STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL 172826092bf5STheodore Ts'o {Opt_acl, EXT4_MOUNT_POSIX_ACL, MOPT_SET}, 172926092bf5STheodore Ts'o {Opt_noacl, EXT4_MOUNT_POSIX_ACL, MOPT_CLEAR}, 173026092bf5STheodore Ts'o #else 173126092bf5STheodore Ts'o {Opt_acl, 0, MOPT_NOSUPPORT}, 173226092bf5STheodore Ts'o {Opt_noacl, 0, MOPT_NOSUPPORT}, 173326092bf5STheodore Ts'o #endif 173426092bf5STheodore Ts'o {Opt_nouid32, EXT4_MOUNT_NO_UID32, MOPT_SET}, 173526092bf5STheodore Ts'o {Opt_debug, EXT4_MOUNT_DEBUG, MOPT_SET}, 1736670e9875STheodore Ts'o {Opt_debug_want_extra_isize, 0, MOPT_GTE0}, 173726092bf5STheodore Ts'o {Opt_quota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, MOPT_SET | MOPT_Q}, 173826092bf5STheodore Ts'o {Opt_usrquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, 173926092bf5STheodore Ts'o MOPT_SET | MOPT_Q}, 174026092bf5STheodore Ts'o {Opt_grpquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_GRPQUOTA, 174126092bf5STheodore Ts'o MOPT_SET | MOPT_Q}, 174249da9392SJan Kara {Opt_prjquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_PRJQUOTA, 174349da9392SJan Kara MOPT_SET | MOPT_Q}, 174426092bf5STheodore Ts'o {Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA | 174549da9392SJan Kara EXT4_MOUNT_GRPQUOTA | EXT4_MOUNT_PRJQUOTA), 174649da9392SJan Kara MOPT_CLEAR | MOPT_Q}, 174726092bf5STheodore Ts'o {Opt_usrjquota, 0, MOPT_Q}, 174826092bf5STheodore Ts'o {Opt_grpjquota, 0, MOPT_Q}, 174926092bf5STheodore Ts'o {Opt_offusrjquota, 0, MOPT_Q}, 175026092bf5STheodore Ts'o {Opt_offgrpjquota, 0, MOPT_Q}, 175126092bf5STheodore Ts'o {Opt_jqfmt_vfsold, QFMT_VFS_OLD, MOPT_QFMT}, 175226092bf5STheodore Ts'o {Opt_jqfmt_vfsv0, QFMT_VFS_V0, MOPT_QFMT}, 175326092bf5STheodore Ts'o {Opt_jqfmt_vfsv1, QFMT_VFS_V1, MOPT_QFMT}, 1754df981d03STheodore Ts'o {Opt_max_dir_size_kb, 0, MOPT_GTE0}, 17556ddb2447STheodore Ts'o {Opt_test_dummy_encryption, 0, MOPT_GTE0}, 1756cdb7ee4cSTahsin Erdogan {Opt_nombcache, EXT4_MOUNT_NO_MBCACHE, MOPT_SET}, 175726092bf5STheodore Ts'o {Opt_err, 0, 0} 175826092bf5STheodore Ts'o }; 175926092bf5STheodore Ts'o 1760c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE 1761c83ad55eSGabriel Krisman Bertazi static const struct ext4_sb_encodings { 1762c83ad55eSGabriel Krisman Bertazi __u16 magic; 1763c83ad55eSGabriel Krisman Bertazi char *name; 1764c83ad55eSGabriel Krisman Bertazi char *version; 1765c83ad55eSGabriel Krisman Bertazi } ext4_sb_encoding_map[] = { 1766c83ad55eSGabriel Krisman Bertazi {EXT4_ENC_UTF8_12_1, "utf8", "12.1.0"}, 1767c83ad55eSGabriel Krisman Bertazi }; 1768c83ad55eSGabriel Krisman Bertazi 1769c83ad55eSGabriel Krisman Bertazi static int ext4_sb_read_encoding(const struct ext4_super_block *es, 1770c83ad55eSGabriel Krisman Bertazi const struct ext4_sb_encodings **encoding, 1771c83ad55eSGabriel Krisman Bertazi __u16 *flags) 1772c83ad55eSGabriel Krisman Bertazi { 1773c83ad55eSGabriel Krisman Bertazi __u16 magic = le16_to_cpu(es->s_encoding); 1774c83ad55eSGabriel Krisman Bertazi int i; 1775c83ad55eSGabriel Krisman Bertazi 1776c83ad55eSGabriel Krisman Bertazi for (i = 0; i < ARRAY_SIZE(ext4_sb_encoding_map); i++) 1777c83ad55eSGabriel Krisman Bertazi if (magic == ext4_sb_encoding_map[i].magic) 1778c83ad55eSGabriel Krisman Bertazi break; 1779c83ad55eSGabriel Krisman Bertazi 1780c83ad55eSGabriel Krisman Bertazi if (i >= ARRAY_SIZE(ext4_sb_encoding_map)) 1781c83ad55eSGabriel Krisman Bertazi return -EINVAL; 1782c83ad55eSGabriel Krisman Bertazi 1783c83ad55eSGabriel Krisman Bertazi *encoding = &ext4_sb_encoding_map[i]; 1784c83ad55eSGabriel Krisman Bertazi *flags = le16_to_cpu(es->s_encoding_flags); 1785c83ad55eSGabriel Krisman Bertazi 1786c83ad55eSGabriel Krisman Bertazi return 0; 1787c83ad55eSGabriel Krisman Bertazi } 1788c83ad55eSGabriel Krisman Bertazi #endif 1789c83ad55eSGabriel Krisman Bertazi 179026092bf5STheodore Ts'o static int handle_mount_opt(struct super_block *sb, char *opt, int token, 179126092bf5STheodore Ts'o substring_t *args, unsigned long *journal_devnum, 179226092bf5STheodore Ts'o unsigned int *journal_ioprio, int is_remount) 179326092bf5STheodore Ts'o { 179426092bf5STheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(sb); 179526092bf5STheodore Ts'o const struct mount_opts *m; 179608cefc7aSEric W. Biederman kuid_t uid; 179708cefc7aSEric W. Biederman kgid_t gid; 179826092bf5STheodore Ts'o int arg = 0; 179926092bf5STheodore Ts'o 180057f73c2cSTheodore Ts'o #ifdef CONFIG_QUOTA 180157f73c2cSTheodore Ts'o if (token == Opt_usrjquota) 180257f73c2cSTheodore Ts'o return set_qf_name(sb, USRQUOTA, &args[0]); 180357f73c2cSTheodore Ts'o else if (token == Opt_grpjquota) 180457f73c2cSTheodore Ts'o return set_qf_name(sb, GRPQUOTA, &args[0]); 180557f73c2cSTheodore Ts'o else if (token == Opt_offusrjquota) 180657f73c2cSTheodore Ts'o return clear_qf_name(sb, USRQUOTA); 180757f73c2cSTheodore Ts'o else if (token == Opt_offgrpjquota) 180857f73c2cSTheodore Ts'o return clear_qf_name(sb, GRPQUOTA); 180957f73c2cSTheodore Ts'o #endif 181026092bf5STheodore Ts'o switch (token) { 1811f7048605STheodore Ts'o case Opt_noacl: 1812f7048605STheodore Ts'o case Opt_nouser_xattr: 1813f7048605STheodore Ts'o ext4_msg(sb, KERN_WARNING, deprecated_msg, opt, "3.5"); 1814f7048605STheodore Ts'o break; 181526092bf5STheodore Ts'o case Opt_sb: 181626092bf5STheodore Ts'o return 1; /* handled by get_sb_block() */ 181726092bf5STheodore Ts'o case Opt_removed: 18185f3633e3SJan Kara ext4_msg(sb, KERN_WARNING, "Ignoring removed %s option", opt); 181926092bf5STheodore Ts'o return 1; 182026092bf5STheodore Ts'o case Opt_abort: 182126092bf5STheodore Ts'o sbi->s_mount_flags |= EXT4_MF_FS_ABORTED; 182226092bf5STheodore Ts'o return 1; 182326092bf5STheodore Ts'o case Opt_i_version: 1824357fdad0SMatthew Garrett sb->s_flags |= SB_I_VERSION; 182526092bf5STheodore Ts'o return 1; 1826a26f4992STheodore Ts'o case Opt_lazytime: 18271751e8a6SLinus Torvalds sb->s_flags |= SB_LAZYTIME; 1828a26f4992STheodore Ts'o return 1; 1829a26f4992STheodore Ts'o case Opt_nolazytime: 18301751e8a6SLinus Torvalds sb->s_flags &= ~SB_LAZYTIME; 1831a26f4992STheodore Ts'o return 1; 183226092bf5STheodore Ts'o } 183326092bf5STheodore Ts'o 18345f3633e3SJan Kara for (m = ext4_mount_opts; m->token != Opt_err; m++) 18355f3633e3SJan Kara if (token == m->token) 18365f3633e3SJan Kara break; 18375f3633e3SJan Kara 18385f3633e3SJan Kara if (m->token == Opt_err) { 18395f3633e3SJan Kara ext4_msg(sb, KERN_ERR, "Unrecognized mount option \"%s\" " 18405f3633e3SJan Kara "or missing value", opt); 18415f3633e3SJan Kara return -1; 18425f3633e3SJan Kara } 18435f3633e3SJan Kara 18448dc0aa8cSTheodore Ts'o if ((m->flags & MOPT_NO_EXT2) && IS_EXT2_SB(sb)) { 18458dc0aa8cSTheodore Ts'o ext4_msg(sb, KERN_ERR, 18468dc0aa8cSTheodore Ts'o "Mount option \"%s\" incompatible with ext2", opt); 18478dc0aa8cSTheodore Ts'o return -1; 18488dc0aa8cSTheodore Ts'o } 18498dc0aa8cSTheodore Ts'o if ((m->flags & MOPT_NO_EXT3) && IS_EXT3_SB(sb)) { 18508dc0aa8cSTheodore Ts'o ext4_msg(sb, KERN_ERR, 18518dc0aa8cSTheodore Ts'o "Mount option \"%s\" incompatible with ext3", opt); 18528dc0aa8cSTheodore Ts'o return -1; 18538dc0aa8cSTheodore Ts'o } 18548dc0aa8cSTheodore Ts'o 1855ad4eec61SEric Sandeen if (args->from && !(m->flags & MOPT_STRING) && match_int(args, &arg)) 18560efb3b23SJan Kara return -1; 185726092bf5STheodore Ts'o if (args->from && (m->flags & MOPT_GTE0) && (arg < 0)) 185826092bf5STheodore Ts'o return -1; 1859c93cf2d7SDmitry Monakhov if (m->flags & MOPT_EXPLICIT) { 1860c93cf2d7SDmitry Monakhov if (m->mount_opt & EXT4_MOUNT_DELALLOC) { 186126092bf5STheodore Ts'o set_opt2(sb, EXPLICIT_DELALLOC); 18621e381f60SDmitry Monakhov } else if (m->mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) { 18631e381f60SDmitry Monakhov set_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM); 1864c93cf2d7SDmitry Monakhov } else 1865c93cf2d7SDmitry Monakhov return -1; 1866c93cf2d7SDmitry Monakhov } 186726092bf5STheodore Ts'o if (m->flags & MOPT_CLEAR_ERR) 186826092bf5STheodore Ts'o clear_opt(sb, ERRORS_MASK); 186926092bf5STheodore Ts'o if (token == Opt_noquota && sb_any_quota_loaded(sb)) { 187026092bf5STheodore Ts'o ext4_msg(sb, KERN_ERR, "Cannot change quota " 187126092bf5STheodore Ts'o "options when quota turned on"); 187226092bf5STheodore Ts'o return -1; 187326092bf5STheodore Ts'o } 187426092bf5STheodore Ts'o 187526092bf5STheodore Ts'o if (m->flags & MOPT_NOSUPPORT) { 187626092bf5STheodore Ts'o ext4_msg(sb, KERN_ERR, "%s option not supported", opt); 187726092bf5STheodore Ts'o } else if (token == Opt_commit) { 187826092bf5STheodore Ts'o if (arg == 0) 187926092bf5STheodore Ts'o arg = JBD2_DEFAULT_MAX_COMMIT_AGE; 18809ba55543Szhangyi (F) else if (arg > INT_MAX / HZ) { 18819ba55543Szhangyi (F) ext4_msg(sb, KERN_ERR, 18829ba55543Szhangyi (F) "Invalid commit interval %d, " 18839ba55543Szhangyi (F) "must be smaller than %d", 18849ba55543Szhangyi (F) arg, INT_MAX / HZ); 18859ba55543Szhangyi (F) return -1; 18869ba55543Szhangyi (F) } 188726092bf5STheodore Ts'o sbi->s_commit_interval = HZ * arg; 1888670e9875STheodore Ts'o } else if (token == Opt_debug_want_extra_isize) { 1889670e9875STheodore Ts'o sbi->s_want_extra_isize = arg; 189026092bf5STheodore Ts'o } else if (token == Opt_max_batch_time) { 189126092bf5STheodore Ts'o sbi->s_max_batch_time = arg; 189226092bf5STheodore Ts'o } else if (token == Opt_min_batch_time) { 189326092bf5STheodore Ts'o sbi->s_min_batch_time = arg; 189426092bf5STheodore Ts'o } else if (token == Opt_inode_readahead_blks) { 1895e33e60eaSJan Kara if (arg && (arg > (1 << 30) || !is_power_of_2(arg))) { 1896e33e60eaSJan Kara ext4_msg(sb, KERN_ERR, 1897e33e60eaSJan Kara "EXT4-fs: inode_readahead_blks must be " 1898e33e60eaSJan Kara "0 or a power of 2 smaller than 2^31"); 189926092bf5STheodore Ts'o return -1; 190026092bf5STheodore Ts'o } 190126092bf5STheodore Ts'o sbi->s_inode_readahead_blks = arg; 190226092bf5STheodore Ts'o } else if (token == Opt_init_itable) { 190326092bf5STheodore Ts'o set_opt(sb, INIT_INODE_TABLE); 190426092bf5STheodore Ts'o if (!args->from) 190526092bf5STheodore Ts'o arg = EXT4_DEF_LI_WAIT_MULT; 190626092bf5STheodore Ts'o sbi->s_li_wait_mult = arg; 1907df981d03STheodore Ts'o } else if (token == Opt_max_dir_size_kb) { 1908df981d03STheodore Ts'o sbi->s_max_dir_size_kb = arg; 190926092bf5STheodore Ts'o } else if (token == Opt_stripe) { 191026092bf5STheodore Ts'o sbi->s_stripe = arg; 19110efb3b23SJan Kara } else if (token == Opt_resuid) { 19120efb3b23SJan Kara uid = make_kuid(current_user_ns(), arg); 19130efb3b23SJan Kara if (!uid_valid(uid)) { 19145f3633e3SJan Kara ext4_msg(sb, KERN_ERR, "Invalid uid value %d", arg); 19150efb3b23SJan Kara return -1; 19160efb3b23SJan Kara } 19170efb3b23SJan Kara sbi->s_resuid = uid; 19180efb3b23SJan Kara } else if (token == Opt_resgid) { 19190efb3b23SJan Kara gid = make_kgid(current_user_ns(), arg); 19200efb3b23SJan Kara if (!gid_valid(gid)) { 19215f3633e3SJan Kara ext4_msg(sb, KERN_ERR, "Invalid gid value %d", arg); 19220efb3b23SJan Kara return -1; 19230efb3b23SJan Kara } 19240efb3b23SJan Kara sbi->s_resgid = gid; 19250efb3b23SJan Kara } else if (token == Opt_journal_dev) { 19260efb3b23SJan Kara if (is_remount) { 19270efb3b23SJan Kara ext4_msg(sb, KERN_ERR, 19280efb3b23SJan Kara "Cannot specify journal on remount"); 19290efb3b23SJan Kara return -1; 19300efb3b23SJan Kara } 19310efb3b23SJan Kara *journal_devnum = arg; 1932ad4eec61SEric Sandeen } else if (token == Opt_journal_path) { 1933ad4eec61SEric Sandeen char *journal_path; 1934ad4eec61SEric Sandeen struct inode *journal_inode; 1935ad4eec61SEric Sandeen struct path path; 1936ad4eec61SEric Sandeen int error; 1937ad4eec61SEric Sandeen 1938ad4eec61SEric Sandeen if (is_remount) { 1939ad4eec61SEric Sandeen ext4_msg(sb, KERN_ERR, 1940ad4eec61SEric Sandeen "Cannot specify journal on remount"); 1941ad4eec61SEric Sandeen return -1; 1942ad4eec61SEric Sandeen } 1943ad4eec61SEric Sandeen journal_path = match_strdup(&args[0]); 1944ad4eec61SEric Sandeen if (!journal_path) { 1945ad4eec61SEric Sandeen ext4_msg(sb, KERN_ERR, "error: could not dup " 1946ad4eec61SEric Sandeen "journal device string"); 1947ad4eec61SEric Sandeen return -1; 1948ad4eec61SEric Sandeen } 1949ad4eec61SEric Sandeen 1950ad4eec61SEric Sandeen error = kern_path(journal_path, LOOKUP_FOLLOW, &path); 1951ad4eec61SEric Sandeen if (error) { 1952ad4eec61SEric Sandeen ext4_msg(sb, KERN_ERR, "error: could not find " 1953ad4eec61SEric Sandeen "journal device path: error %d", error); 1954ad4eec61SEric Sandeen kfree(journal_path); 1955ad4eec61SEric Sandeen return -1; 1956ad4eec61SEric Sandeen } 1957ad4eec61SEric Sandeen 19582b0143b5SDavid Howells journal_inode = d_inode(path.dentry); 1959ad4eec61SEric Sandeen if (!S_ISBLK(journal_inode->i_mode)) { 1960ad4eec61SEric Sandeen ext4_msg(sb, KERN_ERR, "error: journal path %s " 1961ad4eec61SEric Sandeen "is not a block device", journal_path); 1962ad4eec61SEric Sandeen path_put(&path); 1963ad4eec61SEric Sandeen kfree(journal_path); 1964ad4eec61SEric Sandeen return -1; 1965ad4eec61SEric Sandeen } 1966ad4eec61SEric Sandeen 1967ad4eec61SEric Sandeen *journal_devnum = new_encode_dev(journal_inode->i_rdev); 1968ad4eec61SEric Sandeen path_put(&path); 1969ad4eec61SEric Sandeen kfree(journal_path); 19700efb3b23SJan Kara } else if (token == Opt_journal_ioprio) { 19710efb3b23SJan Kara if (arg > 7) { 19725f3633e3SJan Kara ext4_msg(sb, KERN_ERR, "Invalid journal IO priority" 19730efb3b23SJan Kara " (must be 0-7)"); 19740efb3b23SJan Kara return -1; 19750efb3b23SJan Kara } 19760efb3b23SJan Kara *journal_ioprio = 19770efb3b23SJan Kara IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, arg); 19786ddb2447STheodore Ts'o } else if (token == Opt_test_dummy_encryption) { 1979643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 19806ddb2447STheodore Ts'o sbi->s_mount_flags |= EXT4_MF_TEST_DUMMY_ENCRYPTION; 19816ddb2447STheodore Ts'o ext4_msg(sb, KERN_WARNING, 19826ddb2447STheodore Ts'o "Test dummy encryption mode enabled"); 19836ddb2447STheodore Ts'o #else 19846ddb2447STheodore Ts'o ext4_msg(sb, KERN_WARNING, 19856ddb2447STheodore Ts'o "Test dummy encryption mount option ignored"); 19866ddb2447STheodore Ts'o #endif 198726092bf5STheodore Ts'o } else if (m->flags & MOPT_DATAJ) { 198826092bf5STheodore Ts'o if (is_remount) { 198926092bf5STheodore Ts'o if (!sbi->s_journal) 199026092bf5STheodore Ts'o ext4_msg(sb, KERN_WARNING, "Remounting file system with no journal so ignoring journalled data option"); 19915f3633e3SJan Kara else if (test_opt(sb, DATA_FLAGS) != m->mount_opt) { 199226092bf5STheodore Ts'o ext4_msg(sb, KERN_ERR, 199326092bf5STheodore Ts'o "Cannot change data mode on remount"); 199426092bf5STheodore Ts'o return -1; 199526092bf5STheodore Ts'o } 199626092bf5STheodore Ts'o } else { 199726092bf5STheodore Ts'o clear_opt(sb, DATA_FLAGS); 199826092bf5STheodore Ts'o sbi->s_mount_opt |= m->mount_opt; 199926092bf5STheodore Ts'o } 200026092bf5STheodore Ts'o #ifdef CONFIG_QUOTA 200126092bf5STheodore Ts'o } else if (m->flags & MOPT_QFMT) { 200226092bf5STheodore Ts'o if (sb_any_quota_loaded(sb) && 200326092bf5STheodore Ts'o sbi->s_jquota_fmt != m->mount_opt) { 20045f3633e3SJan Kara ext4_msg(sb, KERN_ERR, "Cannot change journaled " 20055f3633e3SJan Kara "quota options when quota turned on"); 200626092bf5STheodore Ts'o return -1; 200726092bf5STheodore Ts'o } 2008e2b911c5SDarrick J. Wong if (ext4_has_feature_quota(sb)) { 2009c325a67cSTheodore Ts'o ext4_msg(sb, KERN_INFO, 2010c325a67cSTheodore Ts'o "Quota format mount options ignored " 2011262b4662SJan Kara "when QUOTA feature is enabled"); 2012c325a67cSTheodore Ts'o return 1; 2013262b4662SJan Kara } 201426092bf5STheodore Ts'o sbi->s_jquota_fmt = m->mount_opt; 201526092bf5STheodore Ts'o #endif 2016923ae0ffSRoss Zwisler } else if (token == Opt_dax) { 2017ef83b6e8SDan Williams #ifdef CONFIG_FS_DAX 2018ef83b6e8SDan Williams ext4_msg(sb, KERN_WARNING, 2019ef83b6e8SDan Williams "DAX enabled. Warning: EXPERIMENTAL, use at your own risk"); 2020ef83b6e8SDan Williams sbi->s_mount_opt |= m->mount_opt; 2021ef83b6e8SDan Williams #else 2022923ae0ffSRoss Zwisler ext4_msg(sb, KERN_INFO, "dax option not supported"); 2023923ae0ffSRoss Zwisler return -1; 2024923ae0ffSRoss Zwisler #endif 20257915a861SAles Novak } else if (token == Opt_data_err_abort) { 20267915a861SAles Novak sbi->s_mount_opt |= m->mount_opt; 20277915a861SAles Novak } else if (token == Opt_data_err_ignore) { 20287915a861SAles Novak sbi->s_mount_opt &= ~m->mount_opt; 202926092bf5STheodore Ts'o } else { 203026092bf5STheodore Ts'o if (!args->from) 203126092bf5STheodore Ts'o arg = 1; 203226092bf5STheodore Ts'o if (m->flags & MOPT_CLEAR) 203326092bf5STheodore Ts'o arg = !arg; 203426092bf5STheodore Ts'o else if (unlikely(!(m->flags & MOPT_SET))) { 203526092bf5STheodore Ts'o ext4_msg(sb, KERN_WARNING, 203626092bf5STheodore Ts'o "buggy handling of option %s", opt); 203726092bf5STheodore Ts'o WARN_ON(1); 203826092bf5STheodore Ts'o return -1; 203926092bf5STheodore Ts'o } 204026092bf5STheodore Ts'o if (arg != 0) 204126092bf5STheodore Ts'o sbi->s_mount_opt |= m->mount_opt; 204226092bf5STheodore Ts'o else 204326092bf5STheodore Ts'o sbi->s_mount_opt &= ~m->mount_opt; 204426092bf5STheodore Ts'o } 204526092bf5STheodore Ts'o return 1; 204626092bf5STheodore Ts'o } 204726092bf5STheodore Ts'o 2048ac27a0ecSDave Kleikamp static int parse_options(char *options, struct super_block *sb, 2049c3191067STheodore Ts'o unsigned long *journal_devnum, 2050b3881f74STheodore Ts'o unsigned int *journal_ioprio, 2051661aa520SEric Sandeen int is_remount) 2052ac27a0ecSDave Kleikamp { 20531e1a76edSOlof Johansson struct ext4_sb_info __maybe_unused *sbi = EXT4_SB(sb); 205433458eabSTheodore Ts'o char *p, __maybe_unused *usr_qf_name, __maybe_unused *grp_qf_name; 2055ac27a0ecSDave Kleikamp substring_t args[MAX_OPT_ARGS]; 205626092bf5STheodore Ts'o int token; 2057ac27a0ecSDave Kleikamp 2058ac27a0ecSDave Kleikamp if (!options) 2059ac27a0ecSDave Kleikamp return 1; 2060ac27a0ecSDave Kleikamp 2061ac27a0ecSDave Kleikamp while ((p = strsep(&options, ",")) != NULL) { 2062ac27a0ecSDave Kleikamp if (!*p) 2063ac27a0ecSDave Kleikamp continue; 206415121c18SEric Sandeen /* 206515121c18SEric Sandeen * Initialize args struct so we know whether arg was 206615121c18SEric Sandeen * found; some options take optional arguments. 206715121c18SEric Sandeen */ 2068caecd0afSSachin Kamat args[0].to = args[0].from = NULL; 2069ac27a0ecSDave Kleikamp token = match_token(p, tokens, args); 207026092bf5STheodore Ts'o if (handle_mount_opt(sb, p, token, args, journal_devnum, 207126092bf5STheodore Ts'o journal_ioprio, is_remount) < 0) 2072ac27a0ecSDave Kleikamp return 0; 2073ac27a0ecSDave Kleikamp } 2074ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 207549da9392SJan Kara /* 207649da9392SJan Kara * We do the test below only for project quotas. 'usrquota' and 207749da9392SJan Kara * 'grpquota' mount options are allowed even without quota feature 207849da9392SJan Kara * to support legacy quotas in quota files. 207949da9392SJan Kara */ 208049da9392SJan Kara if (test_opt(sb, PRJQUOTA) && !ext4_has_feature_project(sb)) { 208149da9392SJan Kara ext4_msg(sb, KERN_ERR, "Project quota feature not enabled. " 208249da9392SJan Kara "Cannot enable project quota enforcement."); 208349da9392SJan Kara return 0; 208449da9392SJan Kara } 208533458eabSTheodore Ts'o usr_qf_name = get_qf_name(sb, sbi, USRQUOTA); 208633458eabSTheodore Ts'o grp_qf_name = get_qf_name(sb, sbi, GRPQUOTA); 208733458eabSTheodore Ts'o if (usr_qf_name || grp_qf_name) { 208833458eabSTheodore Ts'o if (test_opt(sb, USRQUOTA) && usr_qf_name) 2089fd8c37ecSTheodore Ts'o clear_opt(sb, USRQUOTA); 2090ac27a0ecSDave Kleikamp 209133458eabSTheodore Ts'o if (test_opt(sb, GRPQUOTA) && grp_qf_name) 2092fd8c37ecSTheodore Ts'o clear_opt(sb, GRPQUOTA); 2093ac27a0ecSDave Kleikamp 209456c50f11SDmitry Monakhov if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) { 2095b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "old and new quota " 2096b31e1552SEric Sandeen "format mixing"); 2097ac27a0ecSDave Kleikamp return 0; 2098ac27a0ecSDave Kleikamp } 2099ac27a0ecSDave Kleikamp 2100ac27a0ecSDave Kleikamp if (!sbi->s_jquota_fmt) { 2101b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "journaled quota format " 2102b31e1552SEric Sandeen "not specified"); 2103ac27a0ecSDave Kleikamp return 0; 2104ac27a0ecSDave Kleikamp } 2105ac27a0ecSDave Kleikamp } 2106ac27a0ecSDave Kleikamp #endif 2107ac27a0ecSDave Kleikamp return 1; 2108ac27a0ecSDave Kleikamp } 2109ac27a0ecSDave Kleikamp 21102adf6da8STheodore Ts'o static inline void ext4_show_quota_options(struct seq_file *seq, 21112adf6da8STheodore Ts'o struct super_block *sb) 21122adf6da8STheodore Ts'o { 21132adf6da8STheodore Ts'o #if defined(CONFIG_QUOTA) 21142adf6da8STheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(sb); 211533458eabSTheodore Ts'o char *usr_qf_name, *grp_qf_name; 21162adf6da8STheodore Ts'o 21172adf6da8STheodore Ts'o if (sbi->s_jquota_fmt) { 21182adf6da8STheodore Ts'o char *fmtname = ""; 21192adf6da8STheodore Ts'o 21202adf6da8STheodore Ts'o switch (sbi->s_jquota_fmt) { 21212adf6da8STheodore Ts'o case QFMT_VFS_OLD: 21222adf6da8STheodore Ts'o fmtname = "vfsold"; 21232adf6da8STheodore Ts'o break; 21242adf6da8STheodore Ts'o case QFMT_VFS_V0: 21252adf6da8STheodore Ts'o fmtname = "vfsv0"; 21262adf6da8STheodore Ts'o break; 21272adf6da8STheodore Ts'o case QFMT_VFS_V1: 21282adf6da8STheodore Ts'o fmtname = "vfsv1"; 21292adf6da8STheodore Ts'o break; 21302adf6da8STheodore Ts'o } 21312adf6da8STheodore Ts'o seq_printf(seq, ",jqfmt=%s", fmtname); 21322adf6da8STheodore Ts'o } 21332adf6da8STheodore Ts'o 213433458eabSTheodore Ts'o rcu_read_lock(); 213533458eabSTheodore Ts'o usr_qf_name = rcu_dereference(sbi->s_qf_names[USRQUOTA]); 213633458eabSTheodore Ts'o grp_qf_name = rcu_dereference(sbi->s_qf_names[GRPQUOTA]); 213733458eabSTheodore Ts'o if (usr_qf_name) 213833458eabSTheodore Ts'o seq_show_option(seq, "usrjquota", usr_qf_name); 213933458eabSTheodore Ts'o if (grp_qf_name) 214033458eabSTheodore Ts'o seq_show_option(seq, "grpjquota", grp_qf_name); 214133458eabSTheodore Ts'o rcu_read_unlock(); 21422adf6da8STheodore Ts'o #endif 21432adf6da8STheodore Ts'o } 21442adf6da8STheodore Ts'o 21455a916be1STheodore Ts'o static const char *token2str(int token) 21465a916be1STheodore Ts'o { 214750df9fd5SHerton Ronaldo Krzesinski const struct match_token *t; 21485a916be1STheodore Ts'o 21495a916be1STheodore Ts'o for (t = tokens; t->token != Opt_err; t++) 21505a916be1STheodore Ts'o if (t->token == token && !strchr(t->pattern, '=')) 21515a916be1STheodore Ts'o break; 21525a916be1STheodore Ts'o return t->pattern; 21535a916be1STheodore Ts'o } 21545a916be1STheodore Ts'o 21552adf6da8STheodore Ts'o /* 21562adf6da8STheodore Ts'o * Show an option if 21572adf6da8STheodore Ts'o * - it's set to a non-default value OR 21582adf6da8STheodore Ts'o * - if the per-sb default is different from the global default 21592adf6da8STheodore Ts'o */ 216066acdcf4STheodore Ts'o static int _ext4_show_options(struct seq_file *seq, struct super_block *sb, 216166acdcf4STheodore Ts'o int nodefs) 21622adf6da8STheodore Ts'o { 21632adf6da8STheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(sb); 21642adf6da8STheodore Ts'o struct ext4_super_block *es = sbi->s_es; 216568afa7e0STyson Nottingham int def_errors, def_mount_opt = sbi->s_def_mount_opt; 21665a916be1STheodore Ts'o const struct mount_opts *m; 216766acdcf4STheodore Ts'o char sep = nodefs ? '\n' : ','; 21682adf6da8STheodore Ts'o 216966acdcf4STheodore Ts'o #define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep) 217066acdcf4STheodore Ts'o #define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg) 21712adf6da8STheodore Ts'o 21722adf6da8STheodore Ts'o if (sbi->s_sb_block != 1) 21735a916be1STheodore Ts'o SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block); 21745a916be1STheodore Ts'o 21755a916be1STheodore Ts'o for (m = ext4_mount_opts; m->token != Opt_err; m++) { 21765a916be1STheodore Ts'o int want_set = m->flags & MOPT_SET; 21775a916be1STheodore Ts'o if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) || 21785a916be1STheodore Ts'o (m->flags & MOPT_CLEAR_ERR)) 21795a916be1STheodore Ts'o continue; 218068afa7e0STyson Nottingham if (!nodefs && !(m->mount_opt & (sbi->s_mount_opt ^ def_mount_opt))) 21815a916be1STheodore Ts'o continue; /* skip if same as the default */ 21825a916be1STheodore Ts'o if ((want_set && 21835a916be1STheodore Ts'o (sbi->s_mount_opt & m->mount_opt) != m->mount_opt) || 21845a916be1STheodore Ts'o (!want_set && (sbi->s_mount_opt & m->mount_opt))) 21855a916be1STheodore Ts'o continue; /* select Opt_noFoo vs Opt_Foo */ 21865a916be1STheodore Ts'o SEQ_OPTS_PRINT("%s", token2str(m->token)); 21875a916be1STheodore Ts'o } 21885a916be1STheodore Ts'o 218908cefc7aSEric W. Biederman if (nodefs || !uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT4_DEF_RESUID)) || 21905a916be1STheodore Ts'o le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) 219108cefc7aSEric W. Biederman SEQ_OPTS_PRINT("resuid=%u", 219208cefc7aSEric W. Biederman from_kuid_munged(&init_user_ns, sbi->s_resuid)); 219308cefc7aSEric W. Biederman if (nodefs || !gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT4_DEF_RESGID)) || 21945a916be1STheodore Ts'o le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) 219508cefc7aSEric W. Biederman SEQ_OPTS_PRINT("resgid=%u", 219608cefc7aSEric W. Biederman from_kgid_munged(&init_user_ns, sbi->s_resgid)); 219766acdcf4STheodore Ts'o def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors); 21985a916be1STheodore Ts'o if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO) 21995a916be1STheodore Ts'o SEQ_OPTS_PUTS("errors=remount-ro"); 22002adf6da8STheodore Ts'o if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE) 22015a916be1STheodore Ts'o SEQ_OPTS_PUTS("errors=continue"); 22022adf6da8STheodore Ts'o if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC) 22035a916be1STheodore Ts'o SEQ_OPTS_PUTS("errors=panic"); 220466acdcf4STheodore Ts'o if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) 22055a916be1STheodore Ts'o SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ); 220666acdcf4STheodore Ts'o if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME) 22075a916be1STheodore Ts'o SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time); 220866acdcf4STheodore Ts'o if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME) 22095a916be1STheodore Ts'o SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time); 2210357fdad0SMatthew Garrett if (sb->s_flags & SB_I_VERSION) 22115a916be1STheodore Ts'o SEQ_OPTS_PUTS("i_version"); 221266acdcf4STheodore Ts'o if (nodefs || sbi->s_stripe) 22135a916be1STheodore Ts'o SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe); 221468afa7e0STyson Nottingham if (nodefs || EXT4_MOUNT_DATA_FLAGS & 221568afa7e0STyson Nottingham (sbi->s_mount_opt ^ def_mount_opt)) { 22162adf6da8STheodore Ts'o if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) 22175a916be1STheodore Ts'o SEQ_OPTS_PUTS("data=journal"); 22182adf6da8STheodore Ts'o else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) 22195a916be1STheodore Ts'o SEQ_OPTS_PUTS("data=ordered"); 22202adf6da8STheodore Ts'o else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA) 22215a916be1STheodore Ts'o SEQ_OPTS_PUTS("data=writeback"); 22225a916be1STheodore Ts'o } 222366acdcf4STheodore Ts'o if (nodefs || 222466acdcf4STheodore Ts'o sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS) 22255a916be1STheodore Ts'o SEQ_OPTS_PRINT("inode_readahead_blks=%u", 22262adf6da8STheodore Ts'o sbi->s_inode_readahead_blks); 22272adf6da8STheodore Ts'o 2228ceec0376STyson Nottingham if (test_opt(sb, INIT_INODE_TABLE) && (nodefs || 222966acdcf4STheodore Ts'o (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT))) 22305a916be1STheodore Ts'o SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult); 2231df981d03STheodore Ts'o if (nodefs || sbi->s_max_dir_size_kb) 2232df981d03STheodore Ts'o SEQ_OPTS_PRINT("max_dir_size_kb=%u", sbi->s_max_dir_size_kb); 22337915a861SAles Novak if (test_opt(sb, DATA_ERR_ABORT)) 22347915a861SAles Novak SEQ_OPTS_PUTS("data_err=abort"); 2235338affb5SEric Biggers if (DUMMY_ENCRYPTION_ENABLED(sbi)) 2236338affb5SEric Biggers SEQ_OPTS_PUTS("test_dummy_encryption"); 22372adf6da8STheodore Ts'o 22382adf6da8STheodore Ts'o ext4_show_quota_options(seq, sb); 22392adf6da8STheodore Ts'o return 0; 22402adf6da8STheodore Ts'o } 22412adf6da8STheodore Ts'o 224266acdcf4STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root) 224366acdcf4STheodore Ts'o { 224466acdcf4STheodore Ts'o return _ext4_show_options(seq, root->d_sb, 0); 224566acdcf4STheodore Ts'o } 224666acdcf4STheodore Ts'o 2247ebd173beSTheodore Ts'o int ext4_seq_options_show(struct seq_file *seq, void *offset) 224866acdcf4STheodore Ts'o { 224966acdcf4STheodore Ts'o struct super_block *sb = seq->private; 225066acdcf4STheodore Ts'o int rc; 225166acdcf4STheodore Ts'o 2252bc98a42cSDavid Howells seq_puts(seq, sb_rdonly(sb) ? "ro" : "rw"); 225366acdcf4STheodore Ts'o rc = _ext4_show_options(seq, sb, 1); 225466acdcf4STheodore Ts'o seq_puts(seq, "\n"); 225566acdcf4STheodore Ts'o return rc; 225666acdcf4STheodore Ts'o } 225766acdcf4STheodore Ts'o 2258617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es, 2259ac27a0ecSDave Kleikamp int read_only) 2260ac27a0ecSDave Kleikamp { 2261617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 2262c89128a0SJaegeuk Kim int err = 0; 2263ac27a0ecSDave Kleikamp 2264617ba13bSMingming Cao if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) { 2265b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "revision level too high, " 2266b31e1552SEric Sandeen "forcing read-only mode"); 2267c89128a0SJaegeuk Kim err = -EROFS; 2268ac27a0ecSDave Kleikamp } 2269ac27a0ecSDave Kleikamp if (read_only) 2270281b5995STheodore Ts'o goto done; 2271617ba13bSMingming Cao if (!(sbi->s_mount_state & EXT4_VALID_FS)) 2272b31e1552SEric Sandeen ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, " 2273b31e1552SEric Sandeen "running e2fsck is recommended"); 2274c8b459f4SLukas Czerner else if (sbi->s_mount_state & EXT4_ERROR_FS) 2275b31e1552SEric Sandeen ext4_msg(sb, KERN_WARNING, 2276b31e1552SEric Sandeen "warning: mounting fs with errors, " 2277b31e1552SEric Sandeen "running e2fsck is recommended"); 2278ed3ce80aSTao Ma else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 && 2279ac27a0ecSDave Kleikamp le16_to_cpu(es->s_mnt_count) >= 2280ac27a0ecSDave Kleikamp (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count)) 2281b31e1552SEric Sandeen ext4_msg(sb, KERN_WARNING, 2282b31e1552SEric Sandeen "warning: maximal mount count reached, " 2283b31e1552SEric Sandeen "running e2fsck is recommended"); 2284ac27a0ecSDave Kleikamp else if (le32_to_cpu(es->s_checkinterval) && 22856a0678a7SArnd Bergmann (ext4_get_tstamp(es, s_lastcheck) + 22866a0678a7SArnd Bergmann le32_to_cpu(es->s_checkinterval) <= ktime_get_real_seconds())) 2287b31e1552SEric Sandeen ext4_msg(sb, KERN_WARNING, 2288b31e1552SEric Sandeen "warning: checktime reached, " 2289b31e1552SEric Sandeen "running e2fsck is recommended"); 22900390131bSFrank Mayhar if (!sbi->s_journal) 2291216c34b2SMarcin Slusarz es->s_state &= cpu_to_le16(~EXT4_VALID_FS); 2292ac27a0ecSDave Kleikamp if (!(__s16) le16_to_cpu(es->s_max_mnt_count)) 2293617ba13bSMingming Cao es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT); 2294e8546d06SMarcin Slusarz le16_add_cpu(&es->s_mnt_count, 1); 22956a0678a7SArnd Bergmann ext4_update_tstamp(es, s_mtime); 22960390131bSFrank Mayhar if (sbi->s_journal) 2297e2b911c5SDarrick J. Wong ext4_set_feature_journal_needs_recovery(sb); 2298ac27a0ecSDave Kleikamp 2299c89128a0SJaegeuk Kim err = ext4_commit_super(sb, 1); 2300281b5995STheodore Ts'o done: 2301ac27a0ecSDave Kleikamp if (test_opt(sb, DEBUG)) 2302a9df9a49STheodore Ts'o printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, " 2303a2595b8aSTheodore Ts'o "bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n", 2304ac27a0ecSDave Kleikamp sb->s_blocksize, 2305ac27a0ecSDave Kleikamp sbi->s_groups_count, 2306617ba13bSMingming Cao EXT4_BLOCKS_PER_GROUP(sb), 2307617ba13bSMingming Cao EXT4_INODES_PER_GROUP(sb), 2308a2595b8aSTheodore Ts'o sbi->s_mount_opt, sbi->s_mount_opt2); 2309ac27a0ecSDave Kleikamp 23107abc52c2SDan Magenheimer cleancache_init_fs(sb); 2311c89128a0SJaegeuk Kim return err; 2312ac27a0ecSDave Kleikamp } 2313ac27a0ecSDave Kleikamp 2314117fff10STheodore Ts'o int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup) 2315117fff10STheodore Ts'o { 2316117fff10STheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(sb); 2317117fff10STheodore Ts'o struct flex_groups *new_groups; 2318117fff10STheodore Ts'o int size; 2319117fff10STheodore Ts'o 2320117fff10STheodore Ts'o if (!sbi->s_log_groups_per_flex) 2321117fff10STheodore Ts'o return 0; 2322117fff10STheodore Ts'o 2323117fff10STheodore Ts'o size = ext4_flex_group(sbi, ngroup - 1) + 1; 2324117fff10STheodore Ts'o if (size <= sbi->s_flex_groups_allocated) 2325117fff10STheodore Ts'o return 0; 2326117fff10STheodore Ts'o 2327117fff10STheodore Ts'o size = roundup_pow_of_two(size * sizeof(struct flex_groups)); 2328a7c3e901SMichal Hocko new_groups = kvzalloc(size, GFP_KERNEL); 2329117fff10STheodore Ts'o if (!new_groups) { 2330117fff10STheodore Ts'o ext4_msg(sb, KERN_ERR, "not enough memory for %d flex groups", 2331117fff10STheodore Ts'o size / (int) sizeof(struct flex_groups)); 2332117fff10STheodore Ts'o return -ENOMEM; 2333117fff10STheodore Ts'o } 2334117fff10STheodore Ts'o 2335117fff10STheodore Ts'o if (sbi->s_flex_groups) { 2336117fff10STheodore Ts'o memcpy(new_groups, sbi->s_flex_groups, 2337117fff10STheodore Ts'o (sbi->s_flex_groups_allocated * 2338117fff10STheodore Ts'o sizeof(struct flex_groups))); 2339b93b41d4SAl Viro kvfree(sbi->s_flex_groups); 2340117fff10STheodore Ts'o } 2341117fff10STheodore Ts'o sbi->s_flex_groups = new_groups; 2342117fff10STheodore Ts'o sbi->s_flex_groups_allocated = size / sizeof(struct flex_groups); 2343117fff10STheodore Ts'o return 0; 2344117fff10STheodore Ts'o } 2345117fff10STheodore Ts'o 2346772cb7c8SJose R. Santos static int ext4_fill_flex_info(struct super_block *sb) 2347772cb7c8SJose R. Santos { 2348772cb7c8SJose R. Santos struct ext4_sb_info *sbi = EXT4_SB(sb); 2349772cb7c8SJose R. Santos struct ext4_group_desc *gdp = NULL; 2350772cb7c8SJose R. Santos ext4_group_t flex_group; 2351117fff10STheodore Ts'o int i, err; 2352772cb7c8SJose R. Santos 2353503358aeSTheodore Ts'o sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex; 2354d50f2ab6SXi Wang if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) { 2355772cb7c8SJose R. Santos sbi->s_log_groups_per_flex = 0; 2356772cb7c8SJose R. Santos return 1; 2357772cb7c8SJose R. Santos } 2358772cb7c8SJose R. Santos 2359117fff10STheodore Ts'o err = ext4_alloc_flex_bg_array(sb, sbi->s_groups_count); 2360117fff10STheodore Ts'o if (err) 2361772cb7c8SJose R. Santos goto failed; 2362772cb7c8SJose R. Santos 2363772cb7c8SJose R. Santos for (i = 0; i < sbi->s_groups_count; i++) { 236488b6edd1STheodore Ts'o gdp = ext4_get_group_desc(sb, i, NULL); 2365772cb7c8SJose R. Santos 2366772cb7c8SJose R. Santos flex_group = ext4_flex_group(sbi, i); 23677ad9bb65STheodore Ts'o atomic_add(ext4_free_inodes_count(sb, gdp), 23687ad9bb65STheodore Ts'o &sbi->s_flex_groups[flex_group].free_inodes); 236990ba983fSTheodore Ts'o atomic64_add(ext4_free_group_clusters(sb, gdp), 237024aaa8efSTheodore Ts'o &sbi->s_flex_groups[flex_group].free_clusters); 23717ad9bb65STheodore Ts'o atomic_add(ext4_used_dirs_count(sb, gdp), 23727ad9bb65STheodore Ts'o &sbi->s_flex_groups[flex_group].used_dirs); 2373772cb7c8SJose R. Santos } 2374772cb7c8SJose R. Santos 2375772cb7c8SJose R. Santos return 1; 2376772cb7c8SJose R. Santos failed: 2377772cb7c8SJose R. Santos return 0; 2378772cb7c8SJose R. Santos } 2379772cb7c8SJose R. Santos 2380e2b911c5SDarrick J. Wong static __le16 ext4_group_desc_csum(struct super_block *sb, __u32 block_group, 2381717d50e4SAndreas Dilger struct ext4_group_desc *gdp) 2382717d50e4SAndreas Dilger { 2383b47820edSDaeho Jeong int offset = offsetof(struct ext4_group_desc, bg_checksum); 2384717d50e4SAndreas Dilger __u16 crc = 0; 2385717d50e4SAndreas Dilger __le32 le_group = cpu_to_le32(block_group); 2386e2b911c5SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(sb); 2387717d50e4SAndreas Dilger 23889aa5d32bSDmitry Monakhov if (ext4_has_metadata_csum(sbi->s_sb)) { 2389feb0ab32SDarrick J. Wong /* Use new metadata_csum algorithm */ 2390feb0ab32SDarrick J. Wong __u32 csum32; 2391b47820edSDaeho Jeong __u16 dummy_csum = 0; 2392feb0ab32SDarrick J. Wong 2393feb0ab32SDarrick J. Wong csum32 = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&le_group, 2394feb0ab32SDarrick J. Wong sizeof(le_group)); 2395b47820edSDaeho Jeong csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp, offset); 2396b47820edSDaeho Jeong csum32 = ext4_chksum(sbi, csum32, (__u8 *)&dummy_csum, 2397b47820edSDaeho Jeong sizeof(dummy_csum)); 2398b47820edSDaeho Jeong offset += sizeof(dummy_csum); 2399b47820edSDaeho Jeong if (offset < sbi->s_desc_size) 2400b47820edSDaeho Jeong csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp + offset, 2401b47820edSDaeho Jeong sbi->s_desc_size - offset); 2402feb0ab32SDarrick J. Wong 2403feb0ab32SDarrick J. Wong crc = csum32 & 0xFFFF; 2404feb0ab32SDarrick J. Wong goto out; 2405feb0ab32SDarrick J. Wong } 2406feb0ab32SDarrick J. Wong 2407feb0ab32SDarrick J. Wong /* old crc16 code */ 2408e2b911c5SDarrick J. Wong if (!ext4_has_feature_gdt_csum(sb)) 2409813d32f9SDarrick J. Wong return 0; 2410813d32f9SDarrick J. Wong 2411717d50e4SAndreas Dilger crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid)); 2412717d50e4SAndreas Dilger crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group)); 2413717d50e4SAndreas Dilger crc = crc16(crc, (__u8 *)gdp, offset); 2414717d50e4SAndreas Dilger offset += sizeof(gdp->bg_checksum); /* skip checksum */ 2415717d50e4SAndreas Dilger /* for checksum of struct ext4_group_desc do the rest...*/ 2416e2b911c5SDarrick J. Wong if (ext4_has_feature_64bit(sb) && 2417717d50e4SAndreas Dilger offset < le16_to_cpu(sbi->s_es->s_desc_size)) 2418717d50e4SAndreas Dilger crc = crc16(crc, (__u8 *)gdp + offset, 2419717d50e4SAndreas Dilger le16_to_cpu(sbi->s_es->s_desc_size) - 2420717d50e4SAndreas Dilger offset); 2421717d50e4SAndreas Dilger 2422feb0ab32SDarrick J. Wong out: 2423717d50e4SAndreas Dilger return cpu_to_le16(crc); 2424717d50e4SAndreas Dilger } 2425717d50e4SAndreas Dilger 2426feb0ab32SDarrick J. Wong int ext4_group_desc_csum_verify(struct super_block *sb, __u32 block_group, 2427717d50e4SAndreas Dilger struct ext4_group_desc *gdp) 2428717d50e4SAndreas Dilger { 2429feb0ab32SDarrick J. Wong if (ext4_has_group_desc_csum(sb) && 2430e2b911c5SDarrick J. Wong (gdp->bg_checksum != ext4_group_desc_csum(sb, block_group, gdp))) 2431717d50e4SAndreas Dilger return 0; 2432717d50e4SAndreas Dilger 2433717d50e4SAndreas Dilger return 1; 2434717d50e4SAndreas Dilger } 2435717d50e4SAndreas Dilger 2436feb0ab32SDarrick J. Wong void ext4_group_desc_csum_set(struct super_block *sb, __u32 block_group, 2437feb0ab32SDarrick J. Wong struct ext4_group_desc *gdp) 2438feb0ab32SDarrick J. Wong { 2439feb0ab32SDarrick J. Wong if (!ext4_has_group_desc_csum(sb)) 2440feb0ab32SDarrick J. Wong return; 2441e2b911c5SDarrick J. Wong gdp->bg_checksum = ext4_group_desc_csum(sb, block_group, gdp); 2442feb0ab32SDarrick J. Wong } 2443feb0ab32SDarrick J. Wong 2444ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */ 2445bfff6873SLukas Czerner static int ext4_check_descriptors(struct super_block *sb, 2446829fa70dSTheodore Ts'o ext4_fsblk_t sb_block, 2447bfff6873SLukas Czerner ext4_group_t *first_not_zeroed) 2448ac27a0ecSDave Kleikamp { 2449617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 2450617ba13bSMingming Cao ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block); 2451617ba13bSMingming Cao ext4_fsblk_t last_block; 245244de022cSTheodore Ts'o ext4_fsblk_t last_bg_block = sb_block + ext4_bg_num_gdb(sb, 0); 2453bd81d8eeSLaurent Vivier ext4_fsblk_t block_bitmap; 2454bd81d8eeSLaurent Vivier ext4_fsblk_t inode_bitmap; 2455bd81d8eeSLaurent Vivier ext4_fsblk_t inode_table; 2456ce421581SJose R. Santos int flexbg_flag = 0; 2457bfff6873SLukas Czerner ext4_group_t i, grp = sbi->s_groups_count; 2458ac27a0ecSDave Kleikamp 2459e2b911c5SDarrick J. Wong if (ext4_has_feature_flex_bg(sb)) 2460ce421581SJose R. Santos flexbg_flag = 1; 2461ce421581SJose R. Santos 2462617ba13bSMingming Cao ext4_debug("Checking group descriptors"); 2463ac27a0ecSDave Kleikamp 2464197cd65aSAkinobu Mita for (i = 0; i < sbi->s_groups_count; i++) { 2465197cd65aSAkinobu Mita struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL); 2466197cd65aSAkinobu Mita 2467ce421581SJose R. Santos if (i == sbi->s_groups_count - 1 || flexbg_flag) 2468bd81d8eeSLaurent Vivier last_block = ext4_blocks_count(sbi->s_es) - 1; 2469ac27a0ecSDave Kleikamp else 2470ac27a0ecSDave Kleikamp last_block = first_block + 2471617ba13bSMingming Cao (EXT4_BLOCKS_PER_GROUP(sb) - 1); 2472ac27a0ecSDave Kleikamp 2473bfff6873SLukas Czerner if ((grp == sbi->s_groups_count) && 2474bfff6873SLukas Czerner !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED))) 2475bfff6873SLukas Czerner grp = i; 2476bfff6873SLukas Czerner 24778fadc143SAlexandre Ratchov block_bitmap = ext4_block_bitmap(sb, gdp); 2478829fa70dSTheodore Ts'o if (block_bitmap == sb_block) { 2479829fa70dSTheodore Ts'o ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " 2480829fa70dSTheodore Ts'o "Block bitmap for group %u overlaps " 2481829fa70dSTheodore Ts'o "superblock", i); 248218db4b4eSTheodore Ts'o if (!sb_rdonly(sb)) 248318db4b4eSTheodore Ts'o return 0; 2484829fa70dSTheodore Ts'o } 248577260807STheodore Ts'o if (block_bitmap >= sb_block + 1 && 248677260807STheodore Ts'o block_bitmap <= last_bg_block) { 248777260807STheodore Ts'o ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " 248877260807STheodore Ts'o "Block bitmap for group %u overlaps " 248977260807STheodore Ts'o "block group descriptors", i); 249077260807STheodore Ts'o if (!sb_rdonly(sb)) 249177260807STheodore Ts'o return 0; 249277260807STheodore Ts'o } 24932b2d6d01STheodore Ts'o if (block_bitmap < first_block || block_bitmap > last_block) { 2494b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " 2495a9df9a49STheodore Ts'o "Block bitmap for group %u not in group " 2496b31e1552SEric Sandeen "(block %llu)!", i, block_bitmap); 2497ac27a0ecSDave Kleikamp return 0; 2498ac27a0ecSDave Kleikamp } 24998fadc143SAlexandre Ratchov inode_bitmap = ext4_inode_bitmap(sb, gdp); 2500829fa70dSTheodore Ts'o if (inode_bitmap == sb_block) { 2501829fa70dSTheodore Ts'o ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " 2502829fa70dSTheodore Ts'o "Inode bitmap for group %u overlaps " 2503829fa70dSTheodore Ts'o "superblock", i); 250418db4b4eSTheodore Ts'o if (!sb_rdonly(sb)) 250518db4b4eSTheodore Ts'o return 0; 2506829fa70dSTheodore Ts'o } 250777260807STheodore Ts'o if (inode_bitmap >= sb_block + 1 && 250877260807STheodore Ts'o inode_bitmap <= last_bg_block) { 250977260807STheodore Ts'o ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " 251077260807STheodore Ts'o "Inode bitmap for group %u overlaps " 251177260807STheodore Ts'o "block group descriptors", i); 251277260807STheodore Ts'o if (!sb_rdonly(sb)) 251377260807STheodore Ts'o return 0; 251477260807STheodore Ts'o } 25152b2d6d01STheodore Ts'o if (inode_bitmap < first_block || inode_bitmap > last_block) { 2516b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " 2517a9df9a49STheodore Ts'o "Inode bitmap for group %u not in group " 2518b31e1552SEric Sandeen "(block %llu)!", i, inode_bitmap); 2519ac27a0ecSDave Kleikamp return 0; 2520ac27a0ecSDave Kleikamp } 25218fadc143SAlexandre Ratchov inode_table = ext4_inode_table(sb, gdp); 2522829fa70dSTheodore Ts'o if (inode_table == sb_block) { 2523829fa70dSTheodore Ts'o ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " 2524829fa70dSTheodore Ts'o "Inode table for group %u overlaps " 2525829fa70dSTheodore Ts'o "superblock", i); 252618db4b4eSTheodore Ts'o if (!sb_rdonly(sb)) 252718db4b4eSTheodore Ts'o return 0; 2528829fa70dSTheodore Ts'o } 252977260807STheodore Ts'o if (inode_table >= sb_block + 1 && 253077260807STheodore Ts'o inode_table <= last_bg_block) { 253177260807STheodore Ts'o ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " 253277260807STheodore Ts'o "Inode table for group %u overlaps " 253377260807STheodore Ts'o "block group descriptors", i); 253477260807STheodore Ts'o if (!sb_rdonly(sb)) 253577260807STheodore Ts'o return 0; 253677260807STheodore Ts'o } 2537bd81d8eeSLaurent Vivier if (inode_table < first_block || 25382b2d6d01STheodore Ts'o inode_table + sbi->s_itb_per_group - 1 > last_block) { 2539b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " 2540a9df9a49STheodore Ts'o "Inode table for group %u not in group " 2541b31e1552SEric Sandeen "(block %llu)!", i, inode_table); 2542ac27a0ecSDave Kleikamp return 0; 2543ac27a0ecSDave Kleikamp } 2544955ce5f5SAneesh Kumar K.V ext4_lock_group(sb, i); 2545feb0ab32SDarrick J. Wong if (!ext4_group_desc_csum_verify(sb, i, gdp)) { 2546b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " 2547b31e1552SEric Sandeen "Checksum for group %u failed (%u!=%u)", 2548e2b911c5SDarrick J. Wong i, le16_to_cpu(ext4_group_desc_csum(sb, i, 2549fd2d4291SAvantika Mathur gdp)), le16_to_cpu(gdp->bg_checksum)); 2550bc98a42cSDavid Howells if (!sb_rdonly(sb)) { 2551955ce5f5SAneesh Kumar K.V ext4_unlock_group(sb, i); 2552717d50e4SAndreas Dilger return 0; 2553717d50e4SAndreas Dilger } 25547ee1ec4cSLi Zefan } 2555955ce5f5SAneesh Kumar K.V ext4_unlock_group(sb, i); 2556ce421581SJose R. Santos if (!flexbg_flag) 2557617ba13bSMingming Cao first_block += EXT4_BLOCKS_PER_GROUP(sb); 2558ac27a0ecSDave Kleikamp } 2559bfff6873SLukas Czerner if (NULL != first_not_zeroed) 2560bfff6873SLukas Czerner *first_not_zeroed = grp; 2561ac27a0ecSDave Kleikamp return 1; 2562ac27a0ecSDave Kleikamp } 2563ac27a0ecSDave Kleikamp 2564617ba13bSMingming Cao /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at 2565ac27a0ecSDave Kleikamp * the superblock) which were deleted from all directories, but held open by 2566ac27a0ecSDave Kleikamp * a process at the time of a crash. We walk the list and try to delete these 2567ac27a0ecSDave Kleikamp * inodes at recovery time (only with a read-write filesystem). 2568ac27a0ecSDave Kleikamp * 2569ac27a0ecSDave Kleikamp * In order to keep the orphan inode chain consistent during traversal (in 2570ac27a0ecSDave Kleikamp * case of crash during recovery), we link each inode into the superblock 2571ac27a0ecSDave Kleikamp * orphan list_head and handle it the same way as an inode deletion during 2572ac27a0ecSDave Kleikamp * normal operation (which journals the operations for us). 2573ac27a0ecSDave Kleikamp * 2574ac27a0ecSDave Kleikamp * We only do an iget() and an iput() on each inode, which is very safe if we 2575ac27a0ecSDave Kleikamp * accidentally point at an in-use or already deleted inode. The worst that 2576ac27a0ecSDave Kleikamp * can happen in this case is that we get a "bit already cleared" message from 2577617ba13bSMingming Cao * ext4_free_inode(). The only reason we would point at a wrong inode is if 2578ac27a0ecSDave Kleikamp * e2fsck was run on this filesystem, and it must have already done the orphan 2579ac27a0ecSDave Kleikamp * inode cleanup for us, so we can safely abort without any further action. 2580ac27a0ecSDave Kleikamp */ 2581617ba13bSMingming Cao static void ext4_orphan_cleanup(struct super_block *sb, 2582617ba13bSMingming Cao struct ext4_super_block *es) 2583ac27a0ecSDave Kleikamp { 2584ac27a0ecSDave Kleikamp unsigned int s_flags = sb->s_flags; 25852c98eb5eSTheodore Ts'o int ret, nr_orphans = 0, nr_truncates = 0; 2586ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 258795f1fda4Szhangyi (F) int quota_update = 0; 2588ac27a0ecSDave Kleikamp int i; 2589ac27a0ecSDave Kleikamp #endif 2590ac27a0ecSDave Kleikamp if (!es->s_last_orphan) { 2591ac27a0ecSDave Kleikamp jbd_debug(4, "no orphan inodes to clean up\n"); 2592ac27a0ecSDave Kleikamp return; 2593ac27a0ecSDave Kleikamp } 2594ac27a0ecSDave Kleikamp 2595a8f48a95SEric Sandeen if (bdev_read_only(sb->s_bdev)) { 2596b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "write access " 2597b31e1552SEric Sandeen "unavailable, skipping orphan cleanup"); 2598a8f48a95SEric Sandeen return; 2599a8f48a95SEric Sandeen } 2600a8f48a95SEric Sandeen 2601d39195c3SAmir Goldstein /* Check if feature set would not allow a r/w mount */ 2602d39195c3SAmir Goldstein if (!ext4_feature_set_ok(sb, 0)) { 2603d39195c3SAmir Goldstein ext4_msg(sb, KERN_INFO, "Skipping orphan cleanup due to " 2604d39195c3SAmir Goldstein "unknown ROCOMPAT features"); 2605d39195c3SAmir Goldstein return; 2606d39195c3SAmir Goldstein } 2607d39195c3SAmir Goldstein 2608617ba13bSMingming Cao if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) { 2609c25f9bc6SEric Sandeen /* don't clear list on RO mount w/ errors */ 26101751e8a6SLinus Torvalds if (es->s_last_orphan && !(s_flags & SB_RDONLY)) { 261184474976SDmitry Monakhov ext4_msg(sb, KERN_INFO, "Errors on filesystem, " 2612ac27a0ecSDave Kleikamp "clearing orphan list.\n"); 2613ac27a0ecSDave Kleikamp es->s_last_orphan = 0; 2614c25f9bc6SEric Sandeen } 2615ac27a0ecSDave Kleikamp jbd_debug(1, "Skipping orphan recovery on fs with errors.\n"); 2616ac27a0ecSDave Kleikamp return; 2617ac27a0ecSDave Kleikamp } 2618ac27a0ecSDave Kleikamp 26191751e8a6SLinus Torvalds if (s_flags & SB_RDONLY) { 2620b31e1552SEric Sandeen ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs"); 26211751e8a6SLinus Torvalds sb->s_flags &= ~SB_RDONLY; 2622ac27a0ecSDave Kleikamp } 2623ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 2624ac27a0ecSDave Kleikamp /* Needed for iput() to work correctly and not trash data */ 26251751e8a6SLinus Torvalds sb->s_flags |= SB_ACTIVE; 262695f1fda4Szhangyi (F) 262795f1fda4Szhangyi (F) /* 262895f1fda4Szhangyi (F) * Turn on quotas which were not enabled for read-only mounts if 262995f1fda4Szhangyi (F) * filesystem has quota feature, so that they are updated correctly. 263095f1fda4Szhangyi (F) */ 26311751e8a6SLinus Torvalds if (ext4_has_feature_quota(sb) && (s_flags & SB_RDONLY)) { 263295f1fda4Szhangyi (F) int ret = ext4_enable_quotas(sb); 263395f1fda4Szhangyi (F) 263495f1fda4Szhangyi (F) if (!ret) 263595f1fda4Szhangyi (F) quota_update = 1; 263695f1fda4Szhangyi (F) else 263795f1fda4Szhangyi (F) ext4_msg(sb, KERN_ERR, 263895f1fda4Szhangyi (F) "Cannot turn on quotas: error %d", ret); 263995f1fda4Szhangyi (F) } 264095f1fda4Szhangyi (F) 264195f1fda4Szhangyi (F) /* Turn on journaled quotas used for old sytle */ 2642a2d4a646SJan Kara for (i = 0; i < EXT4_MAXQUOTAS; i++) { 2643617ba13bSMingming Cao if (EXT4_SB(sb)->s_qf_names[i]) { 2644617ba13bSMingming Cao int ret = ext4_quota_on_mount(sb, i); 264595f1fda4Szhangyi (F) 264695f1fda4Szhangyi (F) if (!ret) 264795f1fda4Szhangyi (F) quota_update = 1; 264895f1fda4Szhangyi (F) else 2649b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, 2650b31e1552SEric Sandeen "Cannot turn on journaled " 265195f1fda4Szhangyi (F) "quota: type %d: error %d", i, ret); 2652ac27a0ecSDave Kleikamp } 2653ac27a0ecSDave Kleikamp } 2654ac27a0ecSDave Kleikamp #endif 2655ac27a0ecSDave Kleikamp 2656ac27a0ecSDave Kleikamp while (es->s_last_orphan) { 2657ac27a0ecSDave Kleikamp struct inode *inode; 2658ac27a0ecSDave Kleikamp 2659c65d5c6cSVegard Nossum /* 2660c65d5c6cSVegard Nossum * We may have encountered an error during cleanup; if 2661c65d5c6cSVegard Nossum * so, skip the rest. 2662c65d5c6cSVegard Nossum */ 2663c65d5c6cSVegard Nossum if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) { 2664c65d5c6cSVegard Nossum jbd_debug(1, "Skipping orphan recovery on fs with errors.\n"); 2665c65d5c6cSVegard Nossum es->s_last_orphan = 0; 2666c65d5c6cSVegard Nossum break; 2667c65d5c6cSVegard Nossum } 2668c65d5c6cSVegard Nossum 266997bd42b9SJosef Bacik inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan)); 267097bd42b9SJosef Bacik if (IS_ERR(inode)) { 2671ac27a0ecSDave Kleikamp es->s_last_orphan = 0; 2672ac27a0ecSDave Kleikamp break; 2673ac27a0ecSDave Kleikamp } 2674ac27a0ecSDave Kleikamp 2675617ba13bSMingming Cao list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan); 2676871a2931SChristoph Hellwig dquot_initialize(inode); 2677ac27a0ecSDave Kleikamp if (inode->i_nlink) { 2678566370a2SPaul Taysom if (test_opt(sb, DEBUG)) 2679b31e1552SEric Sandeen ext4_msg(sb, KERN_DEBUG, 2680b31e1552SEric Sandeen "%s: truncating inode %lu to %lld bytes", 268146e665e9SHarvey Harrison __func__, inode->i_ino, inode->i_size); 2682e5f8eab8STheodore Ts'o jbd_debug(2, "truncating inode %lu to %lld bytes\n", 2683ac27a0ecSDave Kleikamp inode->i_ino, inode->i_size); 26845955102cSAl Viro inode_lock(inode); 268555f252c9SLukas Czerner truncate_inode_pages(inode->i_mapping, inode->i_size); 26862c98eb5eSTheodore Ts'o ret = ext4_truncate(inode); 26872c98eb5eSTheodore Ts'o if (ret) 26882c98eb5eSTheodore Ts'o ext4_std_error(inode->i_sb, ret); 26895955102cSAl Viro inode_unlock(inode); 2690ac27a0ecSDave Kleikamp nr_truncates++; 2691ac27a0ecSDave Kleikamp } else { 2692566370a2SPaul Taysom if (test_opt(sb, DEBUG)) 2693b31e1552SEric Sandeen ext4_msg(sb, KERN_DEBUG, 2694b31e1552SEric Sandeen "%s: deleting unreferenced inode %lu", 269546e665e9SHarvey Harrison __func__, inode->i_ino); 2696ac27a0ecSDave Kleikamp jbd_debug(2, "deleting unreferenced inode %lu\n", 2697ac27a0ecSDave Kleikamp inode->i_ino); 2698ac27a0ecSDave Kleikamp nr_orphans++; 2699ac27a0ecSDave Kleikamp } 2700ac27a0ecSDave Kleikamp iput(inode); /* The delete magic happens here! */ 2701ac27a0ecSDave Kleikamp } 2702ac27a0ecSDave Kleikamp 2703ac27a0ecSDave Kleikamp #define PLURAL(x) (x), ((x) == 1) ? "" : "s" 2704ac27a0ecSDave Kleikamp 2705ac27a0ecSDave Kleikamp if (nr_orphans) 2706b31e1552SEric Sandeen ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted", 2707b31e1552SEric Sandeen PLURAL(nr_orphans)); 2708ac27a0ecSDave Kleikamp if (nr_truncates) 2709b31e1552SEric Sandeen ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up", 2710b31e1552SEric Sandeen PLURAL(nr_truncates)); 2711ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 271295f1fda4Szhangyi (F) /* Turn off quotas if they were enabled for orphan cleanup */ 271395f1fda4Szhangyi (F) if (quota_update) { 2714a2d4a646SJan Kara for (i = 0; i < EXT4_MAXQUOTAS; i++) { 271595f1fda4Szhangyi (F) if (sb_dqopt(sb)->files[i]) 2716287a8095SChristoph Hellwig dquot_quota_off(sb, i); 2717ac27a0ecSDave Kleikamp } 271895f1fda4Szhangyi (F) } 2719ac27a0ecSDave Kleikamp #endif 27201751e8a6SLinus Torvalds sb->s_flags = s_flags; /* Restore SB_RDONLY status */ 2721ac27a0ecSDave Kleikamp } 27220b8e58a1SAndreas Dilger 2723cd2291a4SEric Sandeen /* 2724cd2291a4SEric Sandeen * Maximal extent format file size. 2725cd2291a4SEric Sandeen * Resulting logical blkno at s_maxbytes must fit in our on-disk 2726cd2291a4SEric Sandeen * extent format containers, within a sector_t, and within i_blocks 2727cd2291a4SEric Sandeen * in the vfs. ext4 inode has 48 bits of i_block in fsblock units, 2728cd2291a4SEric Sandeen * so that won't be a limiting factor. 2729cd2291a4SEric Sandeen * 2730f17722f9SLukas Czerner * However there is other limiting factor. We do store extents in the form 2731f17722f9SLukas Czerner * of starting block and length, hence the resulting length of the extent 2732f17722f9SLukas Czerner * covering maximum file size must fit into on-disk format containers as 2733f17722f9SLukas Czerner * well. Given that length is always by 1 unit bigger than max unit (because 2734f17722f9SLukas Czerner * we count 0 as well) we have to lower the s_maxbytes by one fs block. 2735f17722f9SLukas Czerner * 2736cd2291a4SEric Sandeen * Note, this does *not* consider any metadata overhead for vfs i_blocks. 2737cd2291a4SEric Sandeen */ 2738f287a1a5STheodore Ts'o static loff_t ext4_max_size(int blkbits, int has_huge_files) 2739cd2291a4SEric Sandeen { 2740cd2291a4SEric Sandeen loff_t res; 2741cd2291a4SEric Sandeen loff_t upper_limit = MAX_LFS_FILESIZE; 2742cd2291a4SEric Sandeen 274372deb455SChristoph Hellwig BUILD_BUG_ON(sizeof(blkcnt_t) < sizeof(u64)); 274472deb455SChristoph Hellwig 274572deb455SChristoph Hellwig if (!has_huge_files) { 2746cd2291a4SEric Sandeen upper_limit = (1LL << 32) - 1; 2747cd2291a4SEric Sandeen 2748cd2291a4SEric Sandeen /* total blocks in file system block size */ 2749cd2291a4SEric Sandeen upper_limit >>= (blkbits - 9); 2750cd2291a4SEric Sandeen upper_limit <<= blkbits; 2751cd2291a4SEric Sandeen } 2752cd2291a4SEric Sandeen 2753f17722f9SLukas Czerner /* 2754f17722f9SLukas Czerner * 32-bit extent-start container, ee_block. We lower the maxbytes 2755f17722f9SLukas Czerner * by one fs block, so ee_len can cover the extent of maximum file 2756f17722f9SLukas Czerner * size 2757f17722f9SLukas Czerner */ 2758f17722f9SLukas Czerner res = (1LL << 32) - 1; 2759cd2291a4SEric Sandeen res <<= blkbits; 2760cd2291a4SEric Sandeen 2761cd2291a4SEric Sandeen /* Sanity check against vm- & vfs- imposed limits */ 2762cd2291a4SEric Sandeen if (res > upper_limit) 2763cd2291a4SEric Sandeen res = upper_limit; 2764cd2291a4SEric Sandeen 2765cd2291a4SEric Sandeen return res; 2766cd2291a4SEric Sandeen } 2767ac27a0ecSDave Kleikamp 2768ac27a0ecSDave Kleikamp /* 2769cd2291a4SEric Sandeen * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect 27700fc1b451SAneesh Kumar K.V * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks. 27710fc1b451SAneesh Kumar K.V * We need to be 1 filesystem block less than the 2^48 sector limit. 2772ac27a0ecSDave Kleikamp */ 2773f287a1a5STheodore Ts'o static loff_t ext4_max_bitmap_size(int bits, int has_huge_files) 2774ac27a0ecSDave Kleikamp { 2775617ba13bSMingming Cao loff_t res = EXT4_NDIR_BLOCKS; 27760fc1b451SAneesh Kumar K.V int meta_blocks; 27770fc1b451SAneesh Kumar K.V loff_t upper_limit; 27780b8e58a1SAndreas Dilger /* This is calculated to be the largest file size for a dense, block 27790b8e58a1SAndreas Dilger * mapped file such that the file's total number of 512-byte sectors, 27800b8e58a1SAndreas Dilger * including data and all indirect blocks, does not exceed (2^48 - 1). 27810b8e58a1SAndreas Dilger * 27820b8e58a1SAndreas Dilger * __u32 i_blocks_lo and _u16 i_blocks_high represent the total 27830b8e58a1SAndreas Dilger * number of 512-byte sectors of the file. 27840fc1b451SAneesh Kumar K.V */ 27850fc1b451SAneesh Kumar K.V 278672deb455SChristoph Hellwig if (!has_huge_files) { 27870fc1b451SAneesh Kumar K.V /* 278872deb455SChristoph Hellwig * !has_huge_files or implies that the inode i_block field 278972deb455SChristoph Hellwig * represents total file blocks in 2^32 512-byte sectors == 279072deb455SChristoph Hellwig * size of vfs inode i_blocks * 8 27910fc1b451SAneesh Kumar K.V */ 27920fc1b451SAneesh Kumar K.V upper_limit = (1LL << 32) - 1; 27930fc1b451SAneesh Kumar K.V 27940fc1b451SAneesh Kumar K.V /* total blocks in file system block size */ 27950fc1b451SAneesh Kumar K.V upper_limit >>= (bits - 9); 27960fc1b451SAneesh Kumar K.V 27970fc1b451SAneesh Kumar K.V } else { 27988180a562SAneesh Kumar K.V /* 27998180a562SAneesh Kumar K.V * We use 48 bit ext4_inode i_blocks 28008180a562SAneesh Kumar K.V * With EXT4_HUGE_FILE_FL set the i_blocks 28018180a562SAneesh Kumar K.V * represent total number of blocks in 28028180a562SAneesh Kumar K.V * file system block size 28038180a562SAneesh Kumar K.V */ 28040fc1b451SAneesh Kumar K.V upper_limit = (1LL << 48) - 1; 28050fc1b451SAneesh Kumar K.V 28060fc1b451SAneesh Kumar K.V } 28070fc1b451SAneesh Kumar K.V 28080fc1b451SAneesh Kumar K.V /* indirect blocks */ 28090fc1b451SAneesh Kumar K.V meta_blocks = 1; 28100fc1b451SAneesh Kumar K.V /* double indirect blocks */ 28110fc1b451SAneesh Kumar K.V meta_blocks += 1 + (1LL << (bits-2)); 28120fc1b451SAneesh Kumar K.V /* tripple indirect blocks */ 28130fc1b451SAneesh Kumar K.V meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2))); 28140fc1b451SAneesh Kumar K.V 28150fc1b451SAneesh Kumar K.V upper_limit -= meta_blocks; 28160fc1b451SAneesh Kumar K.V upper_limit <<= bits; 2817ac27a0ecSDave Kleikamp 2818ac27a0ecSDave Kleikamp res += 1LL << (bits-2); 2819ac27a0ecSDave Kleikamp res += 1LL << (2*(bits-2)); 2820ac27a0ecSDave Kleikamp res += 1LL << (3*(bits-2)); 2821ac27a0ecSDave Kleikamp res <<= bits; 2822ac27a0ecSDave Kleikamp if (res > upper_limit) 2823ac27a0ecSDave Kleikamp res = upper_limit; 28240fc1b451SAneesh Kumar K.V 28250fc1b451SAneesh Kumar K.V if (res > MAX_LFS_FILESIZE) 28260fc1b451SAneesh Kumar K.V res = MAX_LFS_FILESIZE; 28270fc1b451SAneesh Kumar K.V 2828ac27a0ecSDave Kleikamp return res; 2829ac27a0ecSDave Kleikamp } 2830ac27a0ecSDave Kleikamp 2831617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb, 283270bbb3e0SAndrew Morton ext4_fsblk_t logical_sb_block, int nr) 2833ac27a0ecSDave Kleikamp { 2834617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 2835fd2d4291SAvantika Mathur ext4_group_t bg, first_meta_bg; 2836ac27a0ecSDave Kleikamp int has_super = 0; 2837ac27a0ecSDave Kleikamp 2838ac27a0ecSDave Kleikamp first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg); 2839ac27a0ecSDave Kleikamp 2840e2b911c5SDarrick J. Wong if (!ext4_has_feature_meta_bg(sb) || nr < first_meta_bg) 284170bbb3e0SAndrew Morton return logical_sb_block + nr + 1; 2842ac27a0ecSDave Kleikamp bg = sbi->s_desc_per_block * nr; 2843617ba13bSMingming Cao if (ext4_bg_has_super(sb, bg)) 2844ac27a0ecSDave Kleikamp has_super = 1; 28450b8e58a1SAndreas Dilger 2846bd63f6b0SDarrick J. Wong /* 2847bd63f6b0SDarrick J. Wong * If we have a meta_bg fs with 1k blocks, group 0's GDT is at 2848bd63f6b0SDarrick J. Wong * block 2, not 1. If s_first_data_block == 0 (bigalloc is enabled 2849bd63f6b0SDarrick J. Wong * on modern mke2fs or blksize > 1k on older mke2fs) then we must 2850bd63f6b0SDarrick J. Wong * compensate. 2851bd63f6b0SDarrick J. Wong */ 2852bd63f6b0SDarrick J. Wong if (sb->s_blocksize == 1024 && nr == 0 && 285349598e04SJun Piao le32_to_cpu(sbi->s_es->s_first_data_block) == 0) 2854bd63f6b0SDarrick J. Wong has_super++; 2855bd63f6b0SDarrick J. Wong 2856617ba13bSMingming Cao return (has_super + ext4_group_first_block_no(sb, bg)); 2857ac27a0ecSDave Kleikamp } 2858ac27a0ecSDave Kleikamp 2859c9de560dSAlex Tomas /** 2860c9de560dSAlex Tomas * ext4_get_stripe_size: Get the stripe size. 2861c9de560dSAlex Tomas * @sbi: In memory super block info 2862c9de560dSAlex Tomas * 2863c9de560dSAlex Tomas * If we have specified it via mount option, then 2864c9de560dSAlex Tomas * use the mount option value. If the value specified at mount time is 2865c9de560dSAlex Tomas * greater than the blocks per group use the super block value. 2866c9de560dSAlex Tomas * If the super block value is greater than blocks per group return 0. 2867c9de560dSAlex Tomas * Allocator needs it be less than blocks per group. 2868c9de560dSAlex Tomas * 2869c9de560dSAlex Tomas */ 2870c9de560dSAlex Tomas static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi) 2871c9de560dSAlex Tomas { 2872c9de560dSAlex Tomas unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride); 2873c9de560dSAlex Tomas unsigned long stripe_width = 2874c9de560dSAlex Tomas le32_to_cpu(sbi->s_es->s_raid_stripe_width); 28753eb08658SDan Ehrenberg int ret; 2876c9de560dSAlex Tomas 2877c9de560dSAlex Tomas if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group) 28783eb08658SDan Ehrenberg ret = sbi->s_stripe; 28795469d7c3SJan Kara else if (stripe_width && stripe_width <= sbi->s_blocks_per_group) 28803eb08658SDan Ehrenberg ret = stripe_width; 28815469d7c3SJan Kara else if (stride && stride <= sbi->s_blocks_per_group) 28823eb08658SDan Ehrenberg ret = stride; 28833eb08658SDan Ehrenberg else 28843eb08658SDan Ehrenberg ret = 0; 2885c9de560dSAlex Tomas 28863eb08658SDan Ehrenberg /* 28873eb08658SDan Ehrenberg * If the stripe width is 1, this makes no sense and 28883eb08658SDan Ehrenberg * we set it to 0 to turn off stripe handling code. 28893eb08658SDan Ehrenberg */ 28903eb08658SDan Ehrenberg if (ret <= 1) 28913eb08658SDan Ehrenberg ret = 0; 2892c9de560dSAlex Tomas 28933eb08658SDan Ehrenberg return ret; 2894c9de560dSAlex Tomas } 2895ac27a0ecSDave Kleikamp 2896a13fb1a4SEric Sandeen /* 2897a13fb1a4SEric Sandeen * Check whether this filesystem can be mounted based on 2898a13fb1a4SEric Sandeen * the features present and the RDONLY/RDWR mount requested. 2899a13fb1a4SEric Sandeen * Returns 1 if this filesystem can be mounted as requested, 2900a13fb1a4SEric Sandeen * 0 if it cannot be. 2901a13fb1a4SEric Sandeen */ 2902a13fb1a4SEric Sandeen static int ext4_feature_set_ok(struct super_block *sb, int readonly) 2903a13fb1a4SEric Sandeen { 2904e2b911c5SDarrick J. Wong if (ext4_has_unknown_ext4_incompat_features(sb)) { 2905a13fb1a4SEric Sandeen ext4_msg(sb, KERN_ERR, 2906a13fb1a4SEric Sandeen "Couldn't mount because of " 2907a13fb1a4SEric Sandeen "unsupported optional features (%x)", 2908a13fb1a4SEric Sandeen (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) & 2909a13fb1a4SEric Sandeen ~EXT4_FEATURE_INCOMPAT_SUPP)); 2910a13fb1a4SEric Sandeen return 0; 2911a13fb1a4SEric Sandeen } 2912a13fb1a4SEric Sandeen 2913c83ad55eSGabriel Krisman Bertazi #ifndef CONFIG_UNICODE 2914c83ad55eSGabriel Krisman Bertazi if (ext4_has_feature_casefold(sb)) { 2915c83ad55eSGabriel Krisman Bertazi ext4_msg(sb, KERN_ERR, 2916c83ad55eSGabriel Krisman Bertazi "Filesystem with casefold feature cannot be " 2917c83ad55eSGabriel Krisman Bertazi "mounted without CONFIG_UNICODE"); 2918c83ad55eSGabriel Krisman Bertazi return 0; 2919c83ad55eSGabriel Krisman Bertazi } 2920c83ad55eSGabriel Krisman Bertazi #endif 2921c83ad55eSGabriel Krisman Bertazi 2922a13fb1a4SEric Sandeen if (readonly) 2923a13fb1a4SEric Sandeen return 1; 2924a13fb1a4SEric Sandeen 2925e2b911c5SDarrick J. Wong if (ext4_has_feature_readonly(sb)) { 29262cb5cc8bSDarrick J. Wong ext4_msg(sb, KERN_INFO, "filesystem is read-only"); 29271751e8a6SLinus Torvalds sb->s_flags |= SB_RDONLY; 29282cb5cc8bSDarrick J. Wong return 1; 29292cb5cc8bSDarrick J. Wong } 29302cb5cc8bSDarrick J. Wong 2931a13fb1a4SEric Sandeen /* Check that feature set is OK for a read-write mount */ 2932e2b911c5SDarrick J. Wong if (ext4_has_unknown_ext4_ro_compat_features(sb)) { 2933a13fb1a4SEric Sandeen ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of " 2934a13fb1a4SEric Sandeen "unsupported optional features (%x)", 2935a13fb1a4SEric Sandeen (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) & 2936a13fb1a4SEric Sandeen ~EXT4_FEATURE_RO_COMPAT_SUPP)); 2937a13fb1a4SEric Sandeen return 0; 2938a13fb1a4SEric Sandeen } 2939e2b911c5SDarrick J. Wong if (ext4_has_feature_bigalloc(sb) && !ext4_has_feature_extents(sb)) { 2940bab08ab9STheodore Ts'o ext4_msg(sb, KERN_ERR, 2941bab08ab9STheodore Ts'o "Can't support bigalloc feature without " 2942bab08ab9STheodore Ts'o "extents feature\n"); 2943bab08ab9STheodore Ts'o return 0; 2944bab08ab9STheodore Ts'o } 29457c319d32SAditya Kali 29467c319d32SAditya Kali #ifndef CONFIG_QUOTA 2947e2b911c5SDarrick J. Wong if (ext4_has_feature_quota(sb) && !readonly) { 29487c319d32SAditya Kali ext4_msg(sb, KERN_ERR, 29497c319d32SAditya Kali "Filesystem with quota feature cannot be mounted RDWR " 29507c319d32SAditya Kali "without CONFIG_QUOTA"); 29517c319d32SAditya Kali return 0; 29527c319d32SAditya Kali } 2953689c958cSLi Xi if (ext4_has_feature_project(sb) && !readonly) { 2954689c958cSLi Xi ext4_msg(sb, KERN_ERR, 2955689c958cSLi Xi "Filesystem with project quota feature cannot be mounted RDWR " 2956689c958cSLi Xi "without CONFIG_QUOTA"); 2957689c958cSLi Xi return 0; 2958689c958cSLi Xi } 29597c319d32SAditya Kali #endif /* CONFIG_QUOTA */ 2960a13fb1a4SEric Sandeen return 1; 2961a13fb1a4SEric Sandeen } 2962a13fb1a4SEric Sandeen 296366e61a9eSTheodore Ts'o /* 296466e61a9eSTheodore Ts'o * This function is called once a day if we have errors logged 296566e61a9eSTheodore Ts'o * on the file system 296666e61a9eSTheodore Ts'o */ 2967235699a8SKees Cook static void print_daily_error_info(struct timer_list *t) 296866e61a9eSTheodore Ts'o { 2969235699a8SKees Cook struct ext4_sb_info *sbi = from_timer(sbi, t, s_err_report); 2970235699a8SKees Cook struct super_block *sb = sbi->s_sb; 2971235699a8SKees Cook struct ext4_super_block *es = sbi->s_es; 297266e61a9eSTheodore Ts'o 297366e61a9eSTheodore Ts'o if (es->s_error_count) 2974ae0f78deSTheodore Ts'o /* fsck newer than v1.41.13 is needed to clean this condition. */ 2975ae0f78deSTheodore Ts'o ext4_msg(sb, KERN_NOTICE, "error count since last fsck: %u", 297666e61a9eSTheodore Ts'o le32_to_cpu(es->s_error_count)); 297766e61a9eSTheodore Ts'o if (es->s_first_error_time) { 29786a0678a7SArnd Bergmann printk(KERN_NOTICE "EXT4-fs (%s): initial error at time %llu: %.*s:%d", 29796a0678a7SArnd Bergmann sb->s_id, 29806a0678a7SArnd Bergmann ext4_get_tstamp(es, s_first_error_time), 298166e61a9eSTheodore Ts'o (int) sizeof(es->s_first_error_func), 298266e61a9eSTheodore Ts'o es->s_first_error_func, 298366e61a9eSTheodore Ts'o le32_to_cpu(es->s_first_error_line)); 298466e61a9eSTheodore Ts'o if (es->s_first_error_ino) 2985651e1c3bSJoe Perches printk(KERN_CONT ": inode %u", 298666e61a9eSTheodore Ts'o le32_to_cpu(es->s_first_error_ino)); 298766e61a9eSTheodore Ts'o if (es->s_first_error_block) 2988651e1c3bSJoe Perches printk(KERN_CONT ": block %llu", (unsigned long long) 298966e61a9eSTheodore Ts'o le64_to_cpu(es->s_first_error_block)); 2990651e1c3bSJoe Perches printk(KERN_CONT "\n"); 299166e61a9eSTheodore Ts'o } 299266e61a9eSTheodore Ts'o if (es->s_last_error_time) { 29936a0678a7SArnd Bergmann printk(KERN_NOTICE "EXT4-fs (%s): last error at time %llu: %.*s:%d", 29946a0678a7SArnd Bergmann sb->s_id, 29956a0678a7SArnd Bergmann ext4_get_tstamp(es, s_last_error_time), 299666e61a9eSTheodore Ts'o (int) sizeof(es->s_last_error_func), 299766e61a9eSTheodore Ts'o es->s_last_error_func, 299866e61a9eSTheodore Ts'o le32_to_cpu(es->s_last_error_line)); 299966e61a9eSTheodore Ts'o if (es->s_last_error_ino) 3000651e1c3bSJoe Perches printk(KERN_CONT ": inode %u", 300166e61a9eSTheodore Ts'o le32_to_cpu(es->s_last_error_ino)); 300266e61a9eSTheodore Ts'o if (es->s_last_error_block) 3003651e1c3bSJoe Perches printk(KERN_CONT ": block %llu", (unsigned long long) 300466e61a9eSTheodore Ts'o le64_to_cpu(es->s_last_error_block)); 3005651e1c3bSJoe Perches printk(KERN_CONT "\n"); 300666e61a9eSTheodore Ts'o } 300766e61a9eSTheodore Ts'o mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ); /* Once a day */ 300866e61a9eSTheodore Ts'o } 300966e61a9eSTheodore Ts'o 3010bfff6873SLukas Czerner /* Find next suitable group and run ext4_init_inode_table */ 3011bfff6873SLukas Czerner static int ext4_run_li_request(struct ext4_li_request *elr) 3012bfff6873SLukas Czerner { 3013bfff6873SLukas Czerner struct ext4_group_desc *gdp = NULL; 3014bfff6873SLukas Czerner ext4_group_t group, ngroups; 3015bfff6873SLukas Czerner struct super_block *sb; 3016bfff6873SLukas Czerner unsigned long timeout = 0; 3017bfff6873SLukas Czerner int ret = 0; 3018bfff6873SLukas Czerner 3019bfff6873SLukas Czerner sb = elr->lr_super; 3020bfff6873SLukas Czerner ngroups = EXT4_SB(sb)->s_groups_count; 3021bfff6873SLukas Czerner 3022bfff6873SLukas Czerner for (group = elr->lr_next_group; group < ngroups; group++) { 3023bfff6873SLukas Czerner gdp = ext4_get_group_desc(sb, group, NULL); 3024bfff6873SLukas Czerner if (!gdp) { 3025bfff6873SLukas Czerner ret = 1; 3026bfff6873SLukas Czerner break; 3027bfff6873SLukas Czerner } 3028bfff6873SLukas Czerner 3029bfff6873SLukas Czerner if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED))) 3030bfff6873SLukas Czerner break; 3031bfff6873SLukas Czerner } 3032bfff6873SLukas Czerner 30337f511862STheodore Ts'o if (group >= ngroups) 3034bfff6873SLukas Czerner ret = 1; 3035bfff6873SLukas Czerner 3036bfff6873SLukas Czerner if (!ret) { 3037bfff6873SLukas Czerner timeout = jiffies; 3038bfff6873SLukas Czerner ret = ext4_init_inode_table(sb, group, 3039bfff6873SLukas Czerner elr->lr_timeout ? 0 : 1); 3040bfff6873SLukas Czerner if (elr->lr_timeout == 0) { 304151ce6511SLukas Czerner timeout = (jiffies - timeout) * 304251ce6511SLukas Czerner elr->lr_sbi->s_li_wait_mult; 3043bfff6873SLukas Czerner elr->lr_timeout = timeout; 3044bfff6873SLukas Czerner } 3045bfff6873SLukas Czerner elr->lr_next_sched = jiffies + elr->lr_timeout; 3046bfff6873SLukas Czerner elr->lr_next_group = group + 1; 3047bfff6873SLukas Czerner } 3048bfff6873SLukas Czerner return ret; 3049bfff6873SLukas Czerner } 3050bfff6873SLukas Czerner 3051bfff6873SLukas Czerner /* 3052bfff6873SLukas Czerner * Remove lr_request from the list_request and free the 30534ed5c033SLukas Czerner * request structure. Should be called with li_list_mtx held 3054bfff6873SLukas Czerner */ 3055bfff6873SLukas Czerner static void ext4_remove_li_request(struct ext4_li_request *elr) 3056bfff6873SLukas Czerner { 3057bfff6873SLukas Czerner struct ext4_sb_info *sbi; 3058bfff6873SLukas Czerner 3059bfff6873SLukas Czerner if (!elr) 3060bfff6873SLukas Czerner return; 3061bfff6873SLukas Czerner 3062bfff6873SLukas Czerner sbi = elr->lr_sbi; 3063bfff6873SLukas Czerner 3064bfff6873SLukas Czerner list_del(&elr->lr_request); 3065bfff6873SLukas Czerner sbi->s_li_request = NULL; 3066bfff6873SLukas Czerner kfree(elr); 3067bfff6873SLukas Czerner } 3068bfff6873SLukas Czerner 3069bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb) 3070bfff6873SLukas Czerner { 30711bb933fbSLukas Czerner mutex_lock(&ext4_li_mtx); 30721bb933fbSLukas Czerner if (!ext4_li_info) { 30731bb933fbSLukas Czerner mutex_unlock(&ext4_li_mtx); 3074bfff6873SLukas Czerner return; 30751bb933fbSLukas Czerner } 3076bfff6873SLukas Czerner 3077bfff6873SLukas Czerner mutex_lock(&ext4_li_info->li_list_mtx); 30781bb933fbSLukas Czerner ext4_remove_li_request(EXT4_SB(sb)->s_li_request); 3079bfff6873SLukas Czerner mutex_unlock(&ext4_li_info->li_list_mtx); 30801bb933fbSLukas Czerner mutex_unlock(&ext4_li_mtx); 3081bfff6873SLukas Czerner } 3082bfff6873SLukas Czerner 30838f1f7453SEric Sandeen static struct task_struct *ext4_lazyinit_task; 30848f1f7453SEric Sandeen 3085bfff6873SLukas Czerner /* 3086bfff6873SLukas Czerner * This is the function where ext4lazyinit thread lives. It walks 3087bfff6873SLukas Czerner * through the request list searching for next scheduled filesystem. 3088bfff6873SLukas Czerner * When such a fs is found, run the lazy initialization request 3089bfff6873SLukas Czerner * (ext4_rn_li_request) and keep track of the time spend in this 3090bfff6873SLukas Czerner * function. Based on that time we compute next schedule time of 3091bfff6873SLukas Czerner * the request. When walking through the list is complete, compute 3092bfff6873SLukas Czerner * next waking time and put itself into sleep. 3093bfff6873SLukas Czerner */ 3094bfff6873SLukas Czerner static int ext4_lazyinit_thread(void *arg) 3095bfff6873SLukas Czerner { 3096bfff6873SLukas Czerner struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg; 3097bfff6873SLukas Czerner struct list_head *pos, *n; 3098bfff6873SLukas Czerner struct ext4_li_request *elr; 30994ed5c033SLukas Czerner unsigned long next_wakeup, cur; 3100bfff6873SLukas Czerner 3101bfff6873SLukas Czerner BUG_ON(NULL == eli); 3102bfff6873SLukas Czerner 3103bfff6873SLukas Czerner cont_thread: 3104bfff6873SLukas Czerner while (true) { 3105bfff6873SLukas Czerner next_wakeup = MAX_JIFFY_OFFSET; 3106bfff6873SLukas Czerner 3107bfff6873SLukas Czerner mutex_lock(&eli->li_list_mtx); 3108bfff6873SLukas Czerner if (list_empty(&eli->li_request_list)) { 3109bfff6873SLukas Czerner mutex_unlock(&eli->li_list_mtx); 3110bfff6873SLukas Czerner goto exit_thread; 3111bfff6873SLukas Czerner } 3112bfff6873SLukas Czerner list_for_each_safe(pos, n, &eli->li_request_list) { 3113e22834f0SDmitry Monakhov int err = 0; 3114e22834f0SDmitry Monakhov int progress = 0; 3115bfff6873SLukas Czerner elr = list_entry(pos, struct ext4_li_request, 3116bfff6873SLukas Czerner lr_request); 3117bfff6873SLukas Czerner 3118e22834f0SDmitry Monakhov if (time_before(jiffies, elr->lr_next_sched)) { 3119e22834f0SDmitry Monakhov if (time_before(elr->lr_next_sched, next_wakeup)) 3120e22834f0SDmitry Monakhov next_wakeup = elr->lr_next_sched; 3121e22834f0SDmitry Monakhov continue; 3122e22834f0SDmitry Monakhov } 3123e22834f0SDmitry Monakhov if (down_read_trylock(&elr->lr_super->s_umount)) { 3124e22834f0SDmitry Monakhov if (sb_start_write_trylock(elr->lr_super)) { 3125e22834f0SDmitry Monakhov progress = 1; 3126e22834f0SDmitry Monakhov /* 3127e22834f0SDmitry Monakhov * We hold sb->s_umount, sb can not 3128e22834f0SDmitry Monakhov * be removed from the list, it is 3129e22834f0SDmitry Monakhov * now safe to drop li_list_mtx 3130e22834f0SDmitry Monakhov */ 3131e22834f0SDmitry Monakhov mutex_unlock(&eli->li_list_mtx); 3132e22834f0SDmitry Monakhov err = ext4_run_li_request(elr); 3133e22834f0SDmitry Monakhov sb_end_write(elr->lr_super); 3134e22834f0SDmitry Monakhov mutex_lock(&eli->li_list_mtx); 3135e22834f0SDmitry Monakhov n = pos->next; 3136e22834f0SDmitry Monakhov } 3137e22834f0SDmitry Monakhov up_read((&elr->lr_super->s_umount)); 3138e22834f0SDmitry Monakhov } 3139b2c78cd0STheodore Ts'o /* error, remove the lazy_init job */ 3140e22834f0SDmitry Monakhov if (err) { 3141bfff6873SLukas Czerner ext4_remove_li_request(elr); 3142bfff6873SLukas Czerner continue; 3143bfff6873SLukas Czerner } 3144e22834f0SDmitry Monakhov if (!progress) { 3145e22834f0SDmitry Monakhov elr->lr_next_sched = jiffies + 3146e22834f0SDmitry Monakhov (prandom_u32() 3147e22834f0SDmitry Monakhov % (EXT4_DEF_LI_MAX_START_DELAY * HZ)); 3148b2c78cd0STheodore Ts'o } 3149bfff6873SLukas Czerner if (time_before(elr->lr_next_sched, next_wakeup)) 3150bfff6873SLukas Czerner next_wakeup = elr->lr_next_sched; 3151bfff6873SLukas Czerner } 3152bfff6873SLukas Czerner mutex_unlock(&eli->li_list_mtx); 3153bfff6873SLukas Czerner 3154a0acae0eSTejun Heo try_to_freeze(); 3155bfff6873SLukas Czerner 31564ed5c033SLukas Czerner cur = jiffies; 31574ed5c033SLukas Czerner if ((time_after_eq(cur, next_wakeup)) || 3158f4245bd4SLukas Czerner (MAX_JIFFY_OFFSET == next_wakeup)) { 3159bfff6873SLukas Czerner cond_resched(); 3160bfff6873SLukas Czerner continue; 3161bfff6873SLukas Czerner } 3162bfff6873SLukas Czerner 31634ed5c033SLukas Czerner schedule_timeout_interruptible(next_wakeup - cur); 31644ed5c033SLukas Czerner 31658f1f7453SEric Sandeen if (kthread_should_stop()) { 31668f1f7453SEric Sandeen ext4_clear_request_list(); 31678f1f7453SEric Sandeen goto exit_thread; 31688f1f7453SEric Sandeen } 3169bfff6873SLukas Czerner } 3170bfff6873SLukas Czerner 3171bfff6873SLukas Czerner exit_thread: 3172bfff6873SLukas Czerner /* 3173bfff6873SLukas Czerner * It looks like the request list is empty, but we need 3174bfff6873SLukas Czerner * to check it under the li_list_mtx lock, to prevent any 3175bfff6873SLukas Czerner * additions into it, and of course we should lock ext4_li_mtx 3176bfff6873SLukas Czerner * to atomically free the list and ext4_li_info, because at 3177bfff6873SLukas Czerner * this point another ext4 filesystem could be registering 3178bfff6873SLukas Czerner * new one. 3179bfff6873SLukas Czerner */ 3180bfff6873SLukas Czerner mutex_lock(&ext4_li_mtx); 3181bfff6873SLukas Czerner mutex_lock(&eli->li_list_mtx); 3182bfff6873SLukas Czerner if (!list_empty(&eli->li_request_list)) { 3183bfff6873SLukas Czerner mutex_unlock(&eli->li_list_mtx); 3184bfff6873SLukas Czerner mutex_unlock(&ext4_li_mtx); 3185bfff6873SLukas Czerner goto cont_thread; 3186bfff6873SLukas Czerner } 3187bfff6873SLukas Czerner mutex_unlock(&eli->li_list_mtx); 3188bfff6873SLukas Czerner kfree(ext4_li_info); 3189bfff6873SLukas Czerner ext4_li_info = NULL; 3190bfff6873SLukas Czerner mutex_unlock(&ext4_li_mtx); 3191bfff6873SLukas Czerner 3192bfff6873SLukas Czerner return 0; 3193bfff6873SLukas Czerner } 3194bfff6873SLukas Czerner 3195bfff6873SLukas Czerner static void ext4_clear_request_list(void) 3196bfff6873SLukas Czerner { 3197bfff6873SLukas Czerner struct list_head *pos, *n; 3198bfff6873SLukas Czerner struct ext4_li_request *elr; 3199bfff6873SLukas Czerner 3200bfff6873SLukas Czerner mutex_lock(&ext4_li_info->li_list_mtx); 3201bfff6873SLukas Czerner list_for_each_safe(pos, n, &ext4_li_info->li_request_list) { 3202bfff6873SLukas Czerner elr = list_entry(pos, struct ext4_li_request, 3203bfff6873SLukas Czerner lr_request); 3204bfff6873SLukas Czerner ext4_remove_li_request(elr); 3205bfff6873SLukas Czerner } 3206bfff6873SLukas Czerner mutex_unlock(&ext4_li_info->li_list_mtx); 3207bfff6873SLukas Czerner } 3208bfff6873SLukas Czerner 3209bfff6873SLukas Czerner static int ext4_run_lazyinit_thread(void) 3210bfff6873SLukas Czerner { 32118f1f7453SEric Sandeen ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread, 32128f1f7453SEric Sandeen ext4_li_info, "ext4lazyinit"); 32138f1f7453SEric Sandeen if (IS_ERR(ext4_lazyinit_task)) { 32148f1f7453SEric Sandeen int err = PTR_ERR(ext4_lazyinit_task); 3215bfff6873SLukas Czerner ext4_clear_request_list(); 3216bfff6873SLukas Czerner kfree(ext4_li_info); 3217bfff6873SLukas Czerner ext4_li_info = NULL; 321892b97816STheodore Ts'o printk(KERN_CRIT "EXT4-fs: error %d creating inode table " 3219bfff6873SLukas Czerner "initialization thread\n", 3220bfff6873SLukas Czerner err); 3221bfff6873SLukas Czerner return err; 3222bfff6873SLukas Czerner } 3223bfff6873SLukas Czerner ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING; 3224bfff6873SLukas Czerner return 0; 3225bfff6873SLukas Czerner } 3226bfff6873SLukas Czerner 3227bfff6873SLukas Czerner /* 3228bfff6873SLukas Czerner * Check whether it make sense to run itable init. thread or not. 3229bfff6873SLukas Czerner * If there is at least one uninitialized inode table, return 3230bfff6873SLukas Czerner * corresponding group number, else the loop goes through all 3231bfff6873SLukas Czerner * groups and return total number of groups. 3232bfff6873SLukas Czerner */ 3233bfff6873SLukas Czerner static ext4_group_t ext4_has_uninit_itable(struct super_block *sb) 3234bfff6873SLukas Czerner { 3235bfff6873SLukas Czerner ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count; 3236bfff6873SLukas Czerner struct ext4_group_desc *gdp = NULL; 3237bfff6873SLukas Czerner 32388844618dSTheodore Ts'o if (!ext4_has_group_desc_csum(sb)) 32398844618dSTheodore Ts'o return ngroups; 32408844618dSTheodore Ts'o 3241bfff6873SLukas Czerner for (group = 0; group < ngroups; group++) { 3242bfff6873SLukas Czerner gdp = ext4_get_group_desc(sb, group, NULL); 3243bfff6873SLukas Czerner if (!gdp) 3244bfff6873SLukas Czerner continue; 3245bfff6873SLukas Czerner 324650122847STheodore Ts'o if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED))) 3247bfff6873SLukas Czerner break; 3248bfff6873SLukas Czerner } 3249bfff6873SLukas Czerner 3250bfff6873SLukas Czerner return group; 3251bfff6873SLukas Czerner } 3252bfff6873SLukas Czerner 3253bfff6873SLukas Czerner static int ext4_li_info_new(void) 3254bfff6873SLukas Czerner { 3255bfff6873SLukas Czerner struct ext4_lazy_init *eli = NULL; 3256bfff6873SLukas Czerner 3257bfff6873SLukas Czerner eli = kzalloc(sizeof(*eli), GFP_KERNEL); 3258bfff6873SLukas Czerner if (!eli) 3259bfff6873SLukas Czerner return -ENOMEM; 3260bfff6873SLukas Czerner 3261bfff6873SLukas Czerner INIT_LIST_HEAD(&eli->li_request_list); 3262bfff6873SLukas Czerner mutex_init(&eli->li_list_mtx); 3263bfff6873SLukas Czerner 3264bfff6873SLukas Czerner eli->li_state |= EXT4_LAZYINIT_QUIT; 3265bfff6873SLukas Czerner 3266bfff6873SLukas Czerner ext4_li_info = eli; 3267bfff6873SLukas Czerner 3268bfff6873SLukas Czerner return 0; 3269bfff6873SLukas Czerner } 3270bfff6873SLukas Czerner 3271bfff6873SLukas Czerner static struct ext4_li_request *ext4_li_request_new(struct super_block *sb, 3272bfff6873SLukas Czerner ext4_group_t start) 3273bfff6873SLukas Czerner { 3274bfff6873SLukas Czerner struct ext4_sb_info *sbi = EXT4_SB(sb); 3275bfff6873SLukas Czerner struct ext4_li_request *elr; 3276bfff6873SLukas Czerner 3277bfff6873SLukas Czerner elr = kzalloc(sizeof(*elr), GFP_KERNEL); 3278bfff6873SLukas Czerner if (!elr) 3279bfff6873SLukas Czerner return NULL; 3280bfff6873SLukas Czerner 3281bfff6873SLukas Czerner elr->lr_super = sb; 3282bfff6873SLukas Czerner elr->lr_sbi = sbi; 3283bfff6873SLukas Czerner elr->lr_next_group = start; 3284bfff6873SLukas Czerner 3285bfff6873SLukas Czerner /* 3286bfff6873SLukas Czerner * Randomize first schedule time of the request to 3287bfff6873SLukas Czerner * spread the inode table initialization requests 3288bfff6873SLukas Czerner * better. 3289bfff6873SLukas Czerner */ 3290dd1f723bSTheodore Ts'o elr->lr_next_sched = jiffies + (prandom_u32() % 3291dd1f723bSTheodore Ts'o (EXT4_DEF_LI_MAX_START_DELAY * HZ)); 3292bfff6873SLukas Czerner return elr; 3293bfff6873SLukas Czerner } 3294bfff6873SLukas Czerner 32957f511862STheodore Ts'o int ext4_register_li_request(struct super_block *sb, 3296bfff6873SLukas Czerner ext4_group_t first_not_zeroed) 3297bfff6873SLukas Czerner { 3298bfff6873SLukas Czerner struct ext4_sb_info *sbi = EXT4_SB(sb); 32997f511862STheodore Ts'o struct ext4_li_request *elr = NULL; 330049598e04SJun Piao ext4_group_t ngroups = sbi->s_groups_count; 33016c5a6cb9SAndrew Morton int ret = 0; 3302bfff6873SLukas Czerner 33037f511862STheodore Ts'o mutex_lock(&ext4_li_mtx); 330451ce6511SLukas Czerner if (sbi->s_li_request != NULL) { 330551ce6511SLukas Czerner /* 330651ce6511SLukas Czerner * Reset timeout so it can be computed again, because 330751ce6511SLukas Czerner * s_li_wait_mult might have changed. 330851ce6511SLukas Czerner */ 330951ce6511SLukas Czerner sbi->s_li_request->lr_timeout = 0; 33107f511862STheodore Ts'o goto out; 331151ce6511SLukas Czerner } 3312bfff6873SLukas Czerner 3313bc98a42cSDavid Howells if (first_not_zeroed == ngroups || sb_rdonly(sb) || 331455ff3840STao Ma !test_opt(sb, INIT_INODE_TABLE)) 33157f511862STheodore Ts'o goto out; 3316bfff6873SLukas Czerner 3317bfff6873SLukas Czerner elr = ext4_li_request_new(sb, first_not_zeroed); 33187f511862STheodore Ts'o if (!elr) { 33197f511862STheodore Ts'o ret = -ENOMEM; 33207f511862STheodore Ts'o goto out; 33217f511862STheodore Ts'o } 3322bfff6873SLukas Czerner 3323bfff6873SLukas Czerner if (NULL == ext4_li_info) { 3324bfff6873SLukas Czerner ret = ext4_li_info_new(); 3325bfff6873SLukas Czerner if (ret) 3326bfff6873SLukas Czerner goto out; 3327bfff6873SLukas Czerner } 3328bfff6873SLukas Czerner 3329bfff6873SLukas Czerner mutex_lock(&ext4_li_info->li_list_mtx); 3330bfff6873SLukas Czerner list_add(&elr->lr_request, &ext4_li_info->li_request_list); 3331bfff6873SLukas Czerner mutex_unlock(&ext4_li_info->li_list_mtx); 3332bfff6873SLukas Czerner 3333bfff6873SLukas Czerner sbi->s_li_request = elr; 333446e4690bSTao Ma /* 333546e4690bSTao Ma * set elr to NULL here since it has been inserted to 333646e4690bSTao Ma * the request_list and the removal and free of it is 333746e4690bSTao Ma * handled by ext4_clear_request_list from now on. 333846e4690bSTao Ma */ 333946e4690bSTao Ma elr = NULL; 3340bfff6873SLukas Czerner 3341bfff6873SLukas Czerner if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) { 3342bfff6873SLukas Czerner ret = ext4_run_lazyinit_thread(); 3343bfff6873SLukas Czerner if (ret) 3344bfff6873SLukas Czerner goto out; 3345bfff6873SLukas Czerner } 3346bfff6873SLukas Czerner out: 3347bfff6873SLukas Czerner mutex_unlock(&ext4_li_mtx); 3348beed5ecbSNicolas Kaiser if (ret) 3349bfff6873SLukas Czerner kfree(elr); 3350bfff6873SLukas Czerner return ret; 3351bfff6873SLukas Czerner } 3352bfff6873SLukas Czerner 3353bfff6873SLukas Czerner /* 3354bfff6873SLukas Czerner * We do not need to lock anything since this is called on 3355bfff6873SLukas Czerner * module unload. 3356bfff6873SLukas Czerner */ 3357bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void) 3358bfff6873SLukas Czerner { 3359bfff6873SLukas Czerner /* 3360bfff6873SLukas Czerner * If thread exited earlier 3361bfff6873SLukas Czerner * there's nothing to be done. 3362bfff6873SLukas Czerner */ 33638f1f7453SEric Sandeen if (!ext4_li_info || !ext4_lazyinit_task) 3364bfff6873SLukas Czerner return; 3365bfff6873SLukas Czerner 33668f1f7453SEric Sandeen kthread_stop(ext4_lazyinit_task); 3367bfff6873SLukas Czerner } 3368bfff6873SLukas Czerner 336925ed6e8aSDarrick J. Wong static int set_journal_csum_feature_set(struct super_block *sb) 337025ed6e8aSDarrick J. Wong { 337125ed6e8aSDarrick J. Wong int ret = 1; 337225ed6e8aSDarrick J. Wong int compat, incompat; 337325ed6e8aSDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(sb); 337425ed6e8aSDarrick J. Wong 33759aa5d32bSDmitry Monakhov if (ext4_has_metadata_csum(sb)) { 3376db9ee220SDarrick J. Wong /* journal checksum v3 */ 337725ed6e8aSDarrick J. Wong compat = 0; 3378db9ee220SDarrick J. Wong incompat = JBD2_FEATURE_INCOMPAT_CSUM_V3; 337925ed6e8aSDarrick J. Wong } else { 338025ed6e8aSDarrick J. Wong /* journal checksum v1 */ 338125ed6e8aSDarrick J. Wong compat = JBD2_FEATURE_COMPAT_CHECKSUM; 338225ed6e8aSDarrick J. Wong incompat = 0; 338325ed6e8aSDarrick J. Wong } 338425ed6e8aSDarrick J. Wong 3385feb8c6d3SDarrick J. Wong jbd2_journal_clear_features(sbi->s_journal, 3386feb8c6d3SDarrick J. Wong JBD2_FEATURE_COMPAT_CHECKSUM, 0, 3387feb8c6d3SDarrick J. Wong JBD2_FEATURE_INCOMPAT_CSUM_V3 | 3388feb8c6d3SDarrick J. Wong JBD2_FEATURE_INCOMPAT_CSUM_V2); 338925ed6e8aSDarrick J. Wong if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) { 339025ed6e8aSDarrick J. Wong ret = jbd2_journal_set_features(sbi->s_journal, 339125ed6e8aSDarrick J. Wong compat, 0, 339225ed6e8aSDarrick J. Wong JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT | 339325ed6e8aSDarrick J. Wong incompat); 339425ed6e8aSDarrick J. Wong } else if (test_opt(sb, JOURNAL_CHECKSUM)) { 339525ed6e8aSDarrick J. Wong ret = jbd2_journal_set_features(sbi->s_journal, 339625ed6e8aSDarrick J. Wong compat, 0, 339725ed6e8aSDarrick J. Wong incompat); 339825ed6e8aSDarrick J. Wong jbd2_journal_clear_features(sbi->s_journal, 0, 0, 339925ed6e8aSDarrick J. Wong JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT); 340025ed6e8aSDarrick J. Wong } else { 3401feb8c6d3SDarrick J. Wong jbd2_journal_clear_features(sbi->s_journal, 0, 0, 3402feb8c6d3SDarrick J. Wong JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT); 340325ed6e8aSDarrick J. Wong } 340425ed6e8aSDarrick J. Wong 340525ed6e8aSDarrick J. Wong return ret; 340625ed6e8aSDarrick J. Wong } 340725ed6e8aSDarrick J. Wong 3408952fc18eSTheodore Ts'o /* 3409952fc18eSTheodore Ts'o * Note: calculating the overhead so we can be compatible with 3410952fc18eSTheodore Ts'o * historical BSD practice is quite difficult in the face of 3411952fc18eSTheodore Ts'o * clusters/bigalloc. This is because multiple metadata blocks from 3412952fc18eSTheodore Ts'o * different block group can end up in the same allocation cluster. 3413952fc18eSTheodore Ts'o * Calculating the exact overhead in the face of clustered allocation 3414952fc18eSTheodore Ts'o * requires either O(all block bitmaps) in memory or O(number of block 3415952fc18eSTheodore Ts'o * groups**2) in time. We will still calculate the superblock for 3416952fc18eSTheodore Ts'o * older file systems --- and if we come across with a bigalloc file 3417952fc18eSTheodore Ts'o * system with zero in s_overhead_clusters the estimate will be close to 3418952fc18eSTheodore Ts'o * correct especially for very large cluster sizes --- but for newer 3419952fc18eSTheodore Ts'o * file systems, it's better to calculate this figure once at mkfs 3420952fc18eSTheodore Ts'o * time, and store it in the superblock. If the superblock value is 3421952fc18eSTheodore Ts'o * present (even for non-bigalloc file systems), we will use it. 3422952fc18eSTheodore Ts'o */ 3423952fc18eSTheodore Ts'o static int count_overhead(struct super_block *sb, ext4_group_t grp, 3424952fc18eSTheodore Ts'o char *buf) 3425952fc18eSTheodore Ts'o { 3426952fc18eSTheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(sb); 3427952fc18eSTheodore Ts'o struct ext4_group_desc *gdp; 3428952fc18eSTheodore Ts'o ext4_fsblk_t first_block, last_block, b; 3429952fc18eSTheodore Ts'o ext4_group_t i, ngroups = ext4_get_groups_count(sb); 3430952fc18eSTheodore Ts'o int s, j, count = 0; 3431952fc18eSTheodore Ts'o 3432e2b911c5SDarrick J. Wong if (!ext4_has_feature_bigalloc(sb)) 34330548bbb8STheodore Ts'o return (ext4_bg_has_super(sb, grp) + ext4_bg_num_gdb(sb, grp) + 34340548bbb8STheodore Ts'o sbi->s_itb_per_group + 2); 34350548bbb8STheodore Ts'o 3436952fc18eSTheodore Ts'o first_block = le32_to_cpu(sbi->s_es->s_first_data_block) + 3437952fc18eSTheodore Ts'o (grp * EXT4_BLOCKS_PER_GROUP(sb)); 3438952fc18eSTheodore Ts'o last_block = first_block + EXT4_BLOCKS_PER_GROUP(sb) - 1; 3439952fc18eSTheodore Ts'o for (i = 0; i < ngroups; i++) { 3440952fc18eSTheodore Ts'o gdp = ext4_get_group_desc(sb, i, NULL); 3441952fc18eSTheodore Ts'o b = ext4_block_bitmap(sb, gdp); 3442952fc18eSTheodore Ts'o if (b >= first_block && b <= last_block) { 3443952fc18eSTheodore Ts'o ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf); 3444952fc18eSTheodore Ts'o count++; 3445952fc18eSTheodore Ts'o } 3446952fc18eSTheodore Ts'o b = ext4_inode_bitmap(sb, gdp); 3447952fc18eSTheodore Ts'o if (b >= first_block && b <= last_block) { 3448952fc18eSTheodore Ts'o ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf); 3449952fc18eSTheodore Ts'o count++; 3450952fc18eSTheodore Ts'o } 3451952fc18eSTheodore Ts'o b = ext4_inode_table(sb, gdp); 3452952fc18eSTheodore Ts'o if (b >= first_block && b + sbi->s_itb_per_group <= last_block) 3453952fc18eSTheodore Ts'o for (j = 0; j < sbi->s_itb_per_group; j++, b++) { 3454952fc18eSTheodore Ts'o int c = EXT4_B2C(sbi, b - first_block); 3455952fc18eSTheodore Ts'o ext4_set_bit(c, buf); 3456952fc18eSTheodore Ts'o count++; 3457952fc18eSTheodore Ts'o } 3458952fc18eSTheodore Ts'o if (i != grp) 3459952fc18eSTheodore Ts'o continue; 3460952fc18eSTheodore Ts'o s = 0; 3461952fc18eSTheodore Ts'o if (ext4_bg_has_super(sb, grp)) { 3462952fc18eSTheodore Ts'o ext4_set_bit(s++, buf); 3463952fc18eSTheodore Ts'o count++; 3464952fc18eSTheodore Ts'o } 3465c48ae41bSTheodore Ts'o j = ext4_bg_num_gdb(sb, grp); 3466c48ae41bSTheodore Ts'o if (s + j > EXT4_BLOCKS_PER_GROUP(sb)) { 3467c48ae41bSTheodore Ts'o ext4_error(sb, "Invalid number of block group " 3468c48ae41bSTheodore Ts'o "descriptor blocks: %d", j); 3469c48ae41bSTheodore Ts'o j = EXT4_BLOCKS_PER_GROUP(sb) - s; 3470952fc18eSTheodore Ts'o } 3471c48ae41bSTheodore Ts'o count += j; 3472c48ae41bSTheodore Ts'o for (; j > 0; j--) 3473c48ae41bSTheodore Ts'o ext4_set_bit(EXT4_B2C(sbi, s++), buf); 3474952fc18eSTheodore Ts'o } 3475952fc18eSTheodore Ts'o if (!count) 3476952fc18eSTheodore Ts'o return 0; 3477952fc18eSTheodore Ts'o return EXT4_CLUSTERS_PER_GROUP(sb) - 3478952fc18eSTheodore Ts'o ext4_count_free(buf, EXT4_CLUSTERS_PER_GROUP(sb) / 8); 3479952fc18eSTheodore Ts'o } 3480952fc18eSTheodore Ts'o 3481952fc18eSTheodore Ts'o /* 3482952fc18eSTheodore Ts'o * Compute the overhead and stash it in sbi->s_overhead 3483952fc18eSTheodore Ts'o */ 3484952fc18eSTheodore Ts'o int ext4_calculate_overhead(struct super_block *sb) 3485952fc18eSTheodore Ts'o { 3486952fc18eSTheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(sb); 3487952fc18eSTheodore Ts'o struct ext4_super_block *es = sbi->s_es; 34883c816dedSEric Whitney struct inode *j_inode; 34893c816dedSEric Whitney unsigned int j_blocks, j_inum = le32_to_cpu(es->s_journal_inum); 3490952fc18eSTheodore Ts'o ext4_group_t i, ngroups = ext4_get_groups_count(sb); 3491952fc18eSTheodore Ts'o ext4_fsblk_t overhead = 0; 34924fdb5543SDmitry Monakhov char *buf = (char *) get_zeroed_page(GFP_NOFS); 3493952fc18eSTheodore Ts'o 3494952fc18eSTheodore Ts'o if (!buf) 3495952fc18eSTheodore Ts'o return -ENOMEM; 3496952fc18eSTheodore Ts'o 3497952fc18eSTheodore Ts'o /* 3498952fc18eSTheodore Ts'o * Compute the overhead (FS structures). This is constant 3499952fc18eSTheodore Ts'o * for a given filesystem unless the number of block groups 3500952fc18eSTheodore Ts'o * changes so we cache the previous value until it does. 3501952fc18eSTheodore Ts'o */ 3502952fc18eSTheodore Ts'o 3503952fc18eSTheodore Ts'o /* 3504952fc18eSTheodore Ts'o * All of the blocks before first_data_block are overhead 3505952fc18eSTheodore Ts'o */ 3506952fc18eSTheodore Ts'o overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block)); 3507952fc18eSTheodore Ts'o 3508952fc18eSTheodore Ts'o /* 3509952fc18eSTheodore Ts'o * Add the overhead found in each block group 3510952fc18eSTheodore Ts'o */ 3511952fc18eSTheodore Ts'o for (i = 0; i < ngroups; i++) { 3512952fc18eSTheodore Ts'o int blks; 3513952fc18eSTheodore Ts'o 3514952fc18eSTheodore Ts'o blks = count_overhead(sb, i, buf); 3515952fc18eSTheodore Ts'o overhead += blks; 3516952fc18eSTheodore Ts'o if (blks) 3517952fc18eSTheodore Ts'o memset(buf, 0, PAGE_SIZE); 3518952fc18eSTheodore Ts'o cond_resched(); 3519952fc18eSTheodore Ts'o } 35203c816dedSEric Whitney 35213c816dedSEric Whitney /* 35223c816dedSEric Whitney * Add the internal journal blocks whether the journal has been 35233c816dedSEric Whitney * loaded or not 35243c816dedSEric Whitney */ 3525b003b524SEric Sandeen if (sbi->s_journal && !sbi->journal_bdev) 3526810da240SLukas Czerner overhead += EXT4_NUM_B2C(sbi, sbi->s_journal->j_maxlen); 35273c816dedSEric Whitney else if (ext4_has_feature_journal(sb) && !sbi->s_journal) { 35283c816dedSEric Whitney j_inode = ext4_get_journal_inode(sb, j_inum); 35293c816dedSEric Whitney if (j_inode) { 35303c816dedSEric Whitney j_blocks = j_inode->i_size >> sb->s_blocksize_bits; 35313c816dedSEric Whitney overhead += EXT4_NUM_B2C(sbi, j_blocks); 35323c816dedSEric Whitney iput(j_inode); 35333c816dedSEric Whitney } else { 35343c816dedSEric Whitney ext4_msg(sb, KERN_ERR, "can't get journal size"); 35353c816dedSEric Whitney } 35363c816dedSEric Whitney } 3537952fc18eSTheodore Ts'o sbi->s_overhead = overhead; 3538952fc18eSTheodore Ts'o smp_wmb(); 3539952fc18eSTheodore Ts'o free_page((unsigned long) buf); 3540952fc18eSTheodore Ts'o return 0; 3541952fc18eSTheodore Ts'o } 3542952fc18eSTheodore Ts'o 35437bc04c5cSBarret Rhoden static void ext4_clamp_want_extra_isize(struct super_block *sb) 35447bc04c5cSBarret Rhoden { 35457bc04c5cSBarret Rhoden struct ext4_sb_info *sbi = EXT4_SB(sb); 35467bc04c5cSBarret Rhoden struct ext4_super_block *es = sbi->s_es; 35477bc04c5cSBarret Rhoden 35487bc04c5cSBarret Rhoden /* determine the minimum size of new large inodes, if present */ 35497bc04c5cSBarret Rhoden if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE && 35507bc04c5cSBarret Rhoden sbi->s_want_extra_isize == 0) { 35517bc04c5cSBarret Rhoden sbi->s_want_extra_isize = sizeof(struct ext4_inode) - 35527bc04c5cSBarret Rhoden EXT4_GOOD_OLD_INODE_SIZE; 35537bc04c5cSBarret Rhoden if (ext4_has_feature_extra_isize(sb)) { 35547bc04c5cSBarret Rhoden if (sbi->s_want_extra_isize < 35557bc04c5cSBarret Rhoden le16_to_cpu(es->s_want_extra_isize)) 35567bc04c5cSBarret Rhoden sbi->s_want_extra_isize = 35577bc04c5cSBarret Rhoden le16_to_cpu(es->s_want_extra_isize); 35587bc04c5cSBarret Rhoden if (sbi->s_want_extra_isize < 35597bc04c5cSBarret Rhoden le16_to_cpu(es->s_min_extra_isize)) 35607bc04c5cSBarret Rhoden sbi->s_want_extra_isize = 35617bc04c5cSBarret Rhoden le16_to_cpu(es->s_min_extra_isize); 35627bc04c5cSBarret Rhoden } 35637bc04c5cSBarret Rhoden } 35647bc04c5cSBarret Rhoden /* Check if enough inode space is available */ 35657bc04c5cSBarret Rhoden if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize > 35667bc04c5cSBarret Rhoden sbi->s_inode_size) { 35677bc04c5cSBarret Rhoden sbi->s_want_extra_isize = sizeof(struct ext4_inode) - 35687bc04c5cSBarret Rhoden EXT4_GOOD_OLD_INODE_SIZE; 35697bc04c5cSBarret Rhoden ext4_msg(sb, KERN_INFO, 35707bc04c5cSBarret Rhoden "required extra inode space not available"); 35717bc04c5cSBarret Rhoden } 35727bc04c5cSBarret Rhoden } 35737bc04c5cSBarret Rhoden 3574b5799018STheodore Ts'o static void ext4_set_resv_clusters(struct super_block *sb) 357527dd4385SLukas Czerner { 357627dd4385SLukas Czerner ext4_fsblk_t resv_clusters; 3577b5799018STheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(sb); 357827dd4385SLukas Czerner 357927dd4385SLukas Czerner /* 358030fac0f7SJan Kara * There's no need to reserve anything when we aren't using extents. 358130fac0f7SJan Kara * The space estimates are exact, there are no unwritten extents, 358230fac0f7SJan Kara * hole punching doesn't need new metadata... This is needed especially 358330fac0f7SJan Kara * to keep ext2/3 backward compatibility. 358430fac0f7SJan Kara */ 3585e2b911c5SDarrick J. Wong if (!ext4_has_feature_extents(sb)) 3586b5799018STheodore Ts'o return; 358730fac0f7SJan Kara /* 358827dd4385SLukas Czerner * By default we reserve 2% or 4096 clusters, whichever is smaller. 358927dd4385SLukas Czerner * This should cover the situations where we can not afford to run 359027dd4385SLukas Czerner * out of space like for example punch hole, or converting 3591556615dcSLukas Czerner * unwritten extents in delalloc path. In most cases such 359227dd4385SLukas Czerner * allocation would require 1, or 2 blocks, higher numbers are 359327dd4385SLukas Czerner * very rare. 359427dd4385SLukas Czerner */ 3595b5799018STheodore Ts'o resv_clusters = (ext4_blocks_count(sbi->s_es) >> 3596b5799018STheodore Ts'o sbi->s_cluster_bits); 359727dd4385SLukas Czerner 359827dd4385SLukas Czerner do_div(resv_clusters, 50); 359927dd4385SLukas Czerner resv_clusters = min_t(ext4_fsblk_t, resv_clusters, 4096); 360027dd4385SLukas Czerner 3601b5799018STheodore Ts'o atomic64_set(&sbi->s_resv_clusters, resv_clusters); 360227dd4385SLukas Czerner } 360327dd4385SLukas Czerner 3604617ba13bSMingming Cao static int ext4_fill_super(struct super_block *sb, void *data, int silent) 3605ac27a0ecSDave Kleikamp { 36065e405595SDan Williams struct dax_device *dax_dev = fs_dax_get_by_bdev(sb->s_bdev); 3607d4c402d9SCurt Wohlgemuth char *orig_data = kstrdup(data, GFP_KERNEL); 3608ac27a0ecSDave Kleikamp struct buffer_head *bh; 3609617ba13bSMingming Cao struct ext4_super_block *es = NULL; 36105aee0f8aSTheodore Ts'o struct ext4_sb_info *sbi = kzalloc(sizeof(*sbi), GFP_KERNEL); 3611617ba13bSMingming Cao ext4_fsblk_t block; 3612617ba13bSMingming Cao ext4_fsblk_t sb_block = get_sb_block(&data); 361370bbb3e0SAndrew Morton ext4_fsblk_t logical_sb_block; 3614ac27a0ecSDave Kleikamp unsigned long offset = 0; 3615ac27a0ecSDave Kleikamp unsigned long journal_devnum = 0; 3616ac27a0ecSDave Kleikamp unsigned long def_mount_opts; 3617ac27a0ecSDave Kleikamp struct inode *root; 36180390131bSFrank Mayhar const char *descr; 3619dcc7dae3SCyrill Gorcunov int ret = -ENOMEM; 3620281b5995STheodore Ts'o int blocksize, clustersize; 36214ec11028STheodore Ts'o unsigned int db_count; 36224ec11028STheodore Ts'o unsigned int i; 3623281b5995STheodore Ts'o int needs_recovery, has_huge_files, has_bigalloc; 3624bd81d8eeSLaurent Vivier __u64 blocks_count; 362507aa2ea1SLukas Czerner int err = 0; 3626b3881f74STheodore Ts'o unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO; 3627bfff6873SLukas Czerner ext4_group_t first_not_zeroed; 3628ac27a0ecSDave Kleikamp 36295aee0f8aSTheodore Ts'o if ((data && !orig_data) || !sbi) 36305aee0f8aSTheodore Ts'o goto out_free_base; 3631705895b6SPekka Enberg 3632aed9eb1bSColin Ian King sbi->s_daxdev = dax_dev; 3633705895b6SPekka Enberg sbi->s_blockgroup_lock = 3634705895b6SPekka Enberg kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL); 36355aee0f8aSTheodore Ts'o if (!sbi->s_blockgroup_lock) 36365aee0f8aSTheodore Ts'o goto out_free_base; 36375aee0f8aSTheodore Ts'o 3638ac27a0ecSDave Kleikamp sb->s_fs_info = sbi; 36392c0544b2STheodore Ts'o sbi->s_sb = sb; 3640240799cdSTheodore Ts'o sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS; 3641d9c9bef1SMiklos Szeredi sbi->s_sb_block = sb_block; 3642f613dfcbSTheodore Ts'o if (sb->s_bdev->bd_part) 3643f613dfcbSTheodore Ts'o sbi->s_sectors_written_start = 3644dbae2c55SMichael Callahan part_stat_read(sb->s_bdev->bd_part, sectors[STAT_WRITE]); 3645ac27a0ecSDave Kleikamp 36469f6200bbSTheodore Ts'o /* Cleanup superblock name */ 3647ec3904dcSRasmus Villemoes strreplace(sb->s_id, '/', '!'); 36489f6200bbSTheodore Ts'o 364907aa2ea1SLukas Czerner /* -EINVAL is default */ 3650dcc7dae3SCyrill Gorcunov ret = -EINVAL; 3651617ba13bSMingming Cao blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE); 3652ac27a0ecSDave Kleikamp if (!blocksize) { 3653b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "unable to set blocksize"); 3654ac27a0ecSDave Kleikamp goto out_fail; 3655ac27a0ecSDave Kleikamp } 3656ac27a0ecSDave Kleikamp 3657ac27a0ecSDave Kleikamp /* 3658617ba13bSMingming Cao * The ext4 superblock will not be buffer aligned for other than 1kB 3659ac27a0ecSDave Kleikamp * block sizes. We need to calculate the offset from buffer start. 3660ac27a0ecSDave Kleikamp */ 3661617ba13bSMingming Cao if (blocksize != EXT4_MIN_BLOCK_SIZE) { 366270bbb3e0SAndrew Morton logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE; 366370bbb3e0SAndrew Morton offset = do_div(logical_sb_block, blocksize); 3664ac27a0ecSDave Kleikamp } else { 366570bbb3e0SAndrew Morton logical_sb_block = sb_block; 3666ac27a0ecSDave Kleikamp } 3667ac27a0ecSDave Kleikamp 3668a8ac900bSGioh Kim if (!(bh = sb_bread_unmovable(sb, logical_sb_block))) { 3669b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "unable to read superblock"); 3670ac27a0ecSDave Kleikamp goto out_fail; 3671ac27a0ecSDave Kleikamp } 3672ac27a0ecSDave Kleikamp /* 3673ac27a0ecSDave Kleikamp * Note: s_es must be initialized as soon as possible because 3674617ba13bSMingming Cao * some ext4 macro-instructions depend on its value 3675ac27a0ecSDave Kleikamp */ 36762716b802STheodore Ts'o es = (struct ext4_super_block *) (bh->b_data + offset); 3677ac27a0ecSDave Kleikamp sbi->s_es = es; 3678ac27a0ecSDave Kleikamp sb->s_magic = le16_to_cpu(es->s_magic); 3679617ba13bSMingming Cao if (sb->s_magic != EXT4_SUPER_MAGIC) 3680617ba13bSMingming Cao goto cantfind_ext4; 3681afc32f7eSTheodore Ts'o sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written); 3682ac27a0ecSDave Kleikamp 3683feb0ab32SDarrick J. Wong /* Warn if metadata_csum and gdt_csum are both set. */ 3684e2b911c5SDarrick J. Wong if (ext4_has_feature_metadata_csum(sb) && 3685e2b911c5SDarrick J. Wong ext4_has_feature_gdt_csum(sb)) 3686363307e6SJakub Wilk ext4_warning(sb, "metadata_csum and uninit_bg are " 3687feb0ab32SDarrick J. Wong "redundant flags; please run fsck."); 3688feb0ab32SDarrick J. Wong 3689d25425f8SDarrick J. Wong /* Check for a known checksum algorithm */ 3690d25425f8SDarrick J. Wong if (!ext4_verify_csum_type(sb, es)) { 3691d25425f8SDarrick J. Wong ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with " 3692d25425f8SDarrick J. Wong "unknown checksum algorithm."); 3693d25425f8SDarrick J. Wong silent = 1; 3694d25425f8SDarrick J. Wong goto cantfind_ext4; 3695d25425f8SDarrick J. Wong } 3696d25425f8SDarrick J. Wong 36970441984aSDarrick J. Wong /* Load the checksum driver */ 36980441984aSDarrick J. Wong sbi->s_chksum_driver = crypto_alloc_shash("crc32c", 0, 0); 36990441984aSDarrick J. Wong if (IS_ERR(sbi->s_chksum_driver)) { 37000441984aSDarrick J. Wong ext4_msg(sb, KERN_ERR, "Cannot load crc32c driver."); 37010441984aSDarrick J. Wong ret = PTR_ERR(sbi->s_chksum_driver); 37020441984aSDarrick J. Wong sbi->s_chksum_driver = NULL; 37030441984aSDarrick J. Wong goto failed_mount; 37040441984aSDarrick J. Wong } 37050441984aSDarrick J. Wong 3706a9c47317SDarrick J. Wong /* Check superblock checksum */ 3707a9c47317SDarrick J. Wong if (!ext4_superblock_csum_verify(sb, es)) { 3708a9c47317SDarrick J. Wong ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with " 3709a9c47317SDarrick J. Wong "invalid superblock checksum. Run e2fsck?"); 3710a9c47317SDarrick J. Wong silent = 1; 37116a797d27SDarrick J. Wong ret = -EFSBADCRC; 3712a9c47317SDarrick J. Wong goto cantfind_ext4; 3713a9c47317SDarrick J. Wong } 3714a9c47317SDarrick J. Wong 3715a9c47317SDarrick J. Wong /* Precompute checksum seed for all metadata */ 3716e2b911c5SDarrick J. Wong if (ext4_has_feature_csum_seed(sb)) 37178c81bd8fSDarrick J. Wong sbi->s_csum_seed = le32_to_cpu(es->s_checksum_seed); 3718dec214d0STahsin Erdogan else if (ext4_has_metadata_csum(sb) || ext4_has_feature_ea_inode(sb)) 3719a9c47317SDarrick J. Wong sbi->s_csum_seed = ext4_chksum(sbi, ~0, es->s_uuid, 3720a9c47317SDarrick J. Wong sizeof(es->s_uuid)); 3721a9c47317SDarrick J. Wong 3722ac27a0ecSDave Kleikamp /* Set defaults before we parse the mount options */ 3723ac27a0ecSDave Kleikamp def_mount_opts = le32_to_cpu(es->s_default_mount_opts); 3724fd8c37ecSTheodore Ts'o set_opt(sb, INIT_INODE_TABLE); 3725617ba13bSMingming Cao if (def_mount_opts & EXT4_DEFM_DEBUG) 3726fd8c37ecSTheodore Ts'o set_opt(sb, DEBUG); 372787f26807STheodore Ts'o if (def_mount_opts & EXT4_DEFM_BSDGROUPS) 3728fd8c37ecSTheodore Ts'o set_opt(sb, GRPID); 3729617ba13bSMingming Cao if (def_mount_opts & EXT4_DEFM_UID16) 3730fd8c37ecSTheodore Ts'o set_opt(sb, NO_UID32); 3731ea663336SEric Sandeen /* xattr user namespace & acls are now defaulted on */ 3732fd8c37ecSTheodore Ts'o set_opt(sb, XATTR_USER); 373303010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL 3734fd8c37ecSTheodore Ts'o set_opt(sb, POSIX_ACL); 37352e7842b8SHugh Dickins #endif 373698c1a759SDarrick J. Wong /* don't forget to enable journal_csum when metadata_csum is enabled. */ 373798c1a759SDarrick J. Wong if (ext4_has_metadata_csum(sb)) 373898c1a759SDarrick J. Wong set_opt(sb, JOURNAL_CHECKSUM); 373998c1a759SDarrick J. Wong 3740617ba13bSMingming Cao if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA) 3741fd8c37ecSTheodore Ts'o set_opt(sb, JOURNAL_DATA); 3742617ba13bSMingming Cao else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED) 3743fd8c37ecSTheodore Ts'o set_opt(sb, ORDERED_DATA); 3744617ba13bSMingming Cao else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK) 3745fd8c37ecSTheodore Ts'o set_opt(sb, WRITEBACK_DATA); 3746ac27a0ecSDave Kleikamp 3747617ba13bSMingming Cao if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC) 3748fd8c37ecSTheodore Ts'o set_opt(sb, ERRORS_PANIC); 3749bb4f397aSAneesh Kumar K.V else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE) 3750fd8c37ecSTheodore Ts'o set_opt(sb, ERRORS_CONT); 3751bb4f397aSAneesh Kumar K.V else 3752fd8c37ecSTheodore Ts'o set_opt(sb, ERRORS_RO); 375345f1a9c3SDarrick J. Wong /* block_validity enabled by default; disable with noblock_validity */ 3754fd8c37ecSTheodore Ts'o set_opt(sb, BLOCK_VALIDITY); 37558b67f04aSTheodore Ts'o if (def_mount_opts & EXT4_DEFM_DISCARD) 3756fd8c37ecSTheodore Ts'o set_opt(sb, DISCARD); 3757ac27a0ecSDave Kleikamp 375808cefc7aSEric W. Biederman sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid)); 375908cefc7aSEric W. Biederman sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid)); 376030773840STheodore Ts'o sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ; 376130773840STheodore Ts'o sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME; 376230773840STheodore Ts'o sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME; 3763ac27a0ecSDave Kleikamp 37648b67f04aSTheodore Ts'o if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0) 3765fd8c37ecSTheodore Ts'o set_opt(sb, BARRIER); 3766ac27a0ecSDave Kleikamp 37671e2462f9SMingming Cao /* 3768dd919b98SAneesh Kumar K.V * enable delayed allocation by default 3769dd919b98SAneesh Kumar K.V * Use -o nodelalloc to turn it off 3770dd919b98SAneesh Kumar K.V */ 3771bc0b75f7STheodore Ts'o if (!IS_EXT3_SB(sb) && !IS_EXT2_SB(sb) && 37728b67f04aSTheodore Ts'o ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0)) 3773fd8c37ecSTheodore Ts'o set_opt(sb, DELALLOC); 3774dd919b98SAneesh Kumar K.V 377551ce6511SLukas Czerner /* 377651ce6511SLukas Czerner * set default s_li_wait_mult for lazyinit, for the case there is 377751ce6511SLukas Czerner * no mount option specified. 377851ce6511SLukas Czerner */ 377951ce6511SLukas Czerner sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT; 378051ce6511SLukas Czerner 37815aee0f8aSTheodore Ts'o if (sbi->s_es->s_mount_opts[0]) { 37825aee0f8aSTheodore Ts'o char *s_mount_opts = kstrndup(sbi->s_es->s_mount_opts, 37835aee0f8aSTheodore Ts'o sizeof(sbi->s_es->s_mount_opts), 37845aee0f8aSTheodore Ts'o GFP_KERNEL); 37855aee0f8aSTheodore Ts'o if (!s_mount_opts) 37865aee0f8aSTheodore Ts'o goto failed_mount; 37875aee0f8aSTheodore Ts'o if (!parse_options(s_mount_opts, sb, &journal_devnum, 37885aee0f8aSTheodore Ts'o &journal_ioprio, 0)) { 37898b67f04aSTheodore Ts'o ext4_msg(sb, KERN_WARNING, 37908b67f04aSTheodore Ts'o "failed to parse options in superblock: %s", 37915aee0f8aSTheodore Ts'o s_mount_opts); 37925aee0f8aSTheodore Ts'o } 37935aee0f8aSTheodore Ts'o kfree(s_mount_opts); 37948b67f04aSTheodore Ts'o } 37955a916be1STheodore Ts'o sbi->s_def_mount_opt = sbi->s_mount_opt; 3796b3881f74STheodore Ts'o if (!parse_options((char *) data, sb, &journal_devnum, 3797661aa520SEric Sandeen &journal_ioprio, 0)) 3798ac27a0ecSDave Kleikamp goto failed_mount; 3799ac27a0ecSDave Kleikamp 3800c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE 3801c83ad55eSGabriel Krisman Bertazi if (ext4_has_feature_casefold(sb) && !sbi->s_encoding) { 3802c83ad55eSGabriel Krisman Bertazi const struct ext4_sb_encodings *encoding_info; 3803c83ad55eSGabriel Krisman Bertazi struct unicode_map *encoding; 3804c83ad55eSGabriel Krisman Bertazi __u16 encoding_flags; 3805c83ad55eSGabriel Krisman Bertazi 3806c83ad55eSGabriel Krisman Bertazi if (ext4_has_feature_encrypt(sb)) { 3807c83ad55eSGabriel Krisman Bertazi ext4_msg(sb, KERN_ERR, 3808c83ad55eSGabriel Krisman Bertazi "Can't mount with encoding and encryption"); 3809c83ad55eSGabriel Krisman Bertazi goto failed_mount; 3810c83ad55eSGabriel Krisman Bertazi } 3811c83ad55eSGabriel Krisman Bertazi 3812c83ad55eSGabriel Krisman Bertazi if (ext4_sb_read_encoding(es, &encoding_info, 3813c83ad55eSGabriel Krisman Bertazi &encoding_flags)) { 3814c83ad55eSGabriel Krisman Bertazi ext4_msg(sb, KERN_ERR, 3815c83ad55eSGabriel Krisman Bertazi "Encoding requested by superblock is unknown"); 3816c83ad55eSGabriel Krisman Bertazi goto failed_mount; 3817c83ad55eSGabriel Krisman Bertazi } 3818c83ad55eSGabriel Krisman Bertazi 3819c83ad55eSGabriel Krisman Bertazi encoding = utf8_load(encoding_info->version); 3820c83ad55eSGabriel Krisman Bertazi if (IS_ERR(encoding)) { 3821c83ad55eSGabriel Krisman Bertazi ext4_msg(sb, KERN_ERR, 3822c83ad55eSGabriel Krisman Bertazi "can't mount with superblock charset: %s-%s " 3823c83ad55eSGabriel Krisman Bertazi "not supported by the kernel. flags: 0x%x.", 3824c83ad55eSGabriel Krisman Bertazi encoding_info->name, encoding_info->version, 3825c83ad55eSGabriel Krisman Bertazi encoding_flags); 3826c83ad55eSGabriel Krisman Bertazi goto failed_mount; 3827c83ad55eSGabriel Krisman Bertazi } 3828c83ad55eSGabriel Krisman Bertazi ext4_msg(sb, KERN_INFO,"Using encoding defined by superblock: " 3829c83ad55eSGabriel Krisman Bertazi "%s-%s with flags 0x%hx", encoding_info->name, 3830c83ad55eSGabriel Krisman Bertazi encoding_info->version?:"\b", encoding_flags); 3831c83ad55eSGabriel Krisman Bertazi 3832c83ad55eSGabriel Krisman Bertazi sbi->s_encoding = encoding; 3833c83ad55eSGabriel Krisman Bertazi sbi->s_encoding_flags = encoding_flags; 3834c83ad55eSGabriel Krisman Bertazi } 3835c83ad55eSGabriel Krisman Bertazi #endif 3836c83ad55eSGabriel Krisman Bertazi 383756889787STheodore Ts'o if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) { 383856889787STheodore Ts'o printk_once(KERN_WARNING "EXT4-fs: Warning: mounting " 383956889787STheodore Ts'o "with data=journal disables delayed " 384056889787STheodore Ts'o "allocation and O_DIRECT support!\n"); 384156889787STheodore Ts'o if (test_opt2(sb, EXPLICIT_DELALLOC)) { 384256889787STheodore Ts'o ext4_msg(sb, KERN_ERR, "can't mount with " 384356889787STheodore Ts'o "both data=journal and delalloc"); 384456889787STheodore Ts'o goto failed_mount; 384556889787STheodore Ts'o } 384656889787STheodore Ts'o if (test_opt(sb, DIOREAD_NOLOCK)) { 384756889787STheodore Ts'o ext4_msg(sb, KERN_ERR, "can't mount with " 38486ae6514bSPiotr Sarna "both data=journal and dioread_nolock"); 384956889787STheodore Ts'o goto failed_mount; 385056889787STheodore Ts'o } 3851923ae0ffSRoss Zwisler if (test_opt(sb, DAX)) { 3852923ae0ffSRoss Zwisler ext4_msg(sb, KERN_ERR, "can't mount with " 3853923ae0ffSRoss Zwisler "both data=journal and dax"); 3854923ae0ffSRoss Zwisler goto failed_mount; 3855923ae0ffSRoss Zwisler } 385673b92a2aSSergey Karamov if (ext4_has_feature_encrypt(sb)) { 385773b92a2aSSergey Karamov ext4_msg(sb, KERN_WARNING, 385873b92a2aSSergey Karamov "encrypted files will use data=ordered " 385973b92a2aSSergey Karamov "instead of data journaling mode"); 386073b92a2aSSergey Karamov } 386156889787STheodore Ts'o if (test_opt(sb, DELALLOC)) 386256889787STheodore Ts'o clear_opt(sb, DELALLOC); 3863001e4a87STejun Heo } else { 3864001e4a87STejun Heo sb->s_iflags |= SB_I_CGROUPWB; 386556889787STheodore Ts'o } 386656889787STheodore Ts'o 38671751e8a6SLinus Torvalds sb->s_flags = (sb->s_flags & ~SB_POSIXACL) | 38681751e8a6SLinus Torvalds (test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0); 3869ac27a0ecSDave Kleikamp 3870617ba13bSMingming Cao if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV && 3871e2b911c5SDarrick J. Wong (ext4_has_compat_features(sb) || 3872e2b911c5SDarrick J. Wong ext4_has_ro_compat_features(sb) || 3873e2b911c5SDarrick J. Wong ext4_has_incompat_features(sb))) 3874b31e1552SEric Sandeen ext4_msg(sb, KERN_WARNING, 3875b31e1552SEric Sandeen "feature flags set on rev 0 fs, " 3876b31e1552SEric Sandeen "running e2fsck is recommended"); 3877469108ffSTheodore Tso 3878ed3654ebSTheodore Ts'o if (es->s_creator_os == cpu_to_le32(EXT4_OS_HURD)) { 3879ed3654ebSTheodore Ts'o set_opt2(sb, HURD_COMPAT); 3880e2b911c5SDarrick J. Wong if (ext4_has_feature_64bit(sb)) { 3881ed3654ebSTheodore Ts'o ext4_msg(sb, KERN_ERR, 3882ed3654ebSTheodore Ts'o "The Hurd can't support 64-bit file systems"); 3883ed3654ebSTheodore Ts'o goto failed_mount; 3884ed3654ebSTheodore Ts'o } 3885dec214d0STahsin Erdogan 3886dec214d0STahsin Erdogan /* 3887dec214d0STahsin Erdogan * ea_inode feature uses l_i_version field which is not 3888dec214d0STahsin Erdogan * available in HURD_COMPAT mode. 3889dec214d0STahsin Erdogan */ 3890dec214d0STahsin Erdogan if (ext4_has_feature_ea_inode(sb)) { 3891dec214d0STahsin Erdogan ext4_msg(sb, KERN_ERR, 3892dec214d0STahsin Erdogan "ea_inode feature is not supported for Hurd"); 3893dec214d0STahsin Erdogan goto failed_mount; 3894dec214d0STahsin Erdogan } 3895ed3654ebSTheodore Ts'o } 3896ed3654ebSTheodore Ts'o 38972035e776STheodore Ts'o if (IS_EXT2_SB(sb)) { 38982035e776STheodore Ts'o if (ext2_feature_set_ok(sb)) 38992035e776STheodore Ts'o ext4_msg(sb, KERN_INFO, "mounting ext2 file system " 39002035e776STheodore Ts'o "using the ext4 subsystem"); 39012035e776STheodore Ts'o else { 39020d9366d6SEric Sandeen /* 39030d9366d6SEric Sandeen * If we're probing be silent, if this looks like 39040d9366d6SEric Sandeen * it's actually an ext[34] filesystem. 39050d9366d6SEric Sandeen */ 39060d9366d6SEric Sandeen if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb))) 39070d9366d6SEric Sandeen goto failed_mount; 39082035e776STheodore Ts'o ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due " 39092035e776STheodore Ts'o "to feature incompatibilities"); 39102035e776STheodore Ts'o goto failed_mount; 39112035e776STheodore Ts'o } 39122035e776STheodore Ts'o } 39132035e776STheodore Ts'o 39142035e776STheodore Ts'o if (IS_EXT3_SB(sb)) { 39152035e776STheodore Ts'o if (ext3_feature_set_ok(sb)) 39162035e776STheodore Ts'o ext4_msg(sb, KERN_INFO, "mounting ext3 file system " 39172035e776STheodore Ts'o "using the ext4 subsystem"); 39182035e776STheodore Ts'o else { 39190d9366d6SEric Sandeen /* 39200d9366d6SEric Sandeen * If we're probing be silent, if this looks like 39210d9366d6SEric Sandeen * it's actually an ext4 filesystem. 39220d9366d6SEric Sandeen */ 39230d9366d6SEric Sandeen if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb))) 39240d9366d6SEric Sandeen goto failed_mount; 39252035e776STheodore Ts'o ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due " 39262035e776STheodore Ts'o "to feature incompatibilities"); 39272035e776STheodore Ts'o goto failed_mount; 39282035e776STheodore Ts'o } 39292035e776STheodore Ts'o } 39302035e776STheodore Ts'o 3931469108ffSTheodore Tso /* 3932ac27a0ecSDave Kleikamp * Check feature flags regardless of the revision level, since we 3933ac27a0ecSDave Kleikamp * previously didn't change the revision level when setting the flags, 3934ac27a0ecSDave Kleikamp * so there is a chance incompat flags are set on a rev 0 filesystem. 3935ac27a0ecSDave Kleikamp */ 3936bc98a42cSDavid Howells if (!ext4_feature_set_ok(sb, (sb_rdonly(sb)))) 3937ac27a0ecSDave Kleikamp goto failed_mount; 3938a13fb1a4SEric Sandeen 3939261cb20cSJan Kara blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size); 3940617ba13bSMingming Cao if (blocksize < EXT4_MIN_BLOCK_SIZE || 3941617ba13bSMingming Cao blocksize > EXT4_MAX_BLOCK_SIZE) { 3942b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, 39438cdf3372STheodore Ts'o "Unsupported filesystem blocksize %d (%d log_block_size)", 39448cdf3372STheodore Ts'o blocksize, le32_to_cpu(es->s_log_block_size)); 39458cdf3372STheodore Ts'o goto failed_mount; 39468cdf3372STheodore Ts'o } 39478cdf3372STheodore Ts'o if (le32_to_cpu(es->s_log_block_size) > 39488cdf3372STheodore Ts'o (EXT4_MAX_BLOCK_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) { 39498cdf3372STheodore Ts'o ext4_msg(sb, KERN_ERR, 39508cdf3372STheodore Ts'o "Invalid log block size: %u", 39518cdf3372STheodore Ts'o le32_to_cpu(es->s_log_block_size)); 3952ac27a0ecSDave Kleikamp goto failed_mount; 3953ac27a0ecSDave Kleikamp } 3954bfe0a5f4STheodore Ts'o if (le32_to_cpu(es->s_log_cluster_size) > 3955bfe0a5f4STheodore Ts'o (EXT4_MAX_CLUSTER_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) { 3956bfe0a5f4STheodore Ts'o ext4_msg(sb, KERN_ERR, 3957bfe0a5f4STheodore Ts'o "Invalid log cluster size: %u", 3958bfe0a5f4STheodore Ts'o le32_to_cpu(es->s_log_cluster_size)); 3959bfe0a5f4STheodore Ts'o goto failed_mount; 3960bfe0a5f4STheodore Ts'o } 3961ac27a0ecSDave Kleikamp 39625b9554dcSTheodore Ts'o if (le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) > (blocksize / 4)) { 39635b9554dcSTheodore Ts'o ext4_msg(sb, KERN_ERR, 39645b9554dcSTheodore Ts'o "Number of reserved GDT blocks insanely large: %d", 39655b9554dcSTheodore Ts'o le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks)); 39665b9554dcSTheodore Ts'o goto failed_mount; 39675b9554dcSTheodore Ts'o } 39685b9554dcSTheodore Ts'o 3969923ae0ffSRoss Zwisler if (sbi->s_mount_opt & EXT4_MOUNT_DAX) { 3970559db4c6SRoss Zwisler if (ext4_has_feature_inline_data(sb)) { 3971559db4c6SRoss Zwisler ext4_msg(sb, KERN_ERR, "Cannot use DAX on a filesystem" 3972559db4c6SRoss Zwisler " that may contain inline data"); 3973361d24d4SEric Sandeen goto failed_mount; 3974559db4c6SRoss Zwisler } 397580660f20SDave Jiang if (!bdev_dax_supported(sb->s_bdev, blocksize)) { 397624f3478dSDan Williams ext4_msg(sb, KERN_ERR, 3977361d24d4SEric Sandeen "DAX unsupported by block device."); 3978361d24d4SEric Sandeen goto failed_mount; 397924f3478dSDan Williams } 3980923ae0ffSRoss Zwisler } 3981923ae0ffSRoss Zwisler 3982e2b911c5SDarrick J. Wong if (ext4_has_feature_encrypt(sb) && es->s_encryption_level) { 39836ddb2447STheodore Ts'o ext4_msg(sb, KERN_ERR, "Unsupported encryption level %d", 39846ddb2447STheodore Ts'o es->s_encryption_level); 39856ddb2447STheodore Ts'o goto failed_mount; 39866ddb2447STheodore Ts'o } 39876ddb2447STheodore Ts'o 3988ac27a0ecSDave Kleikamp if (sb->s_blocksize != blocksize) { 3989ce40733cSAneesh Kumar K.V /* Validate the filesystem blocksize */ 3990ce40733cSAneesh Kumar K.V if (!sb_set_blocksize(sb, blocksize)) { 3991b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "bad block size %d", 3992ce40733cSAneesh Kumar K.V blocksize); 3993ac27a0ecSDave Kleikamp goto failed_mount; 3994ac27a0ecSDave Kleikamp } 3995ac27a0ecSDave Kleikamp 3996ac27a0ecSDave Kleikamp brelse(bh); 399770bbb3e0SAndrew Morton logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE; 399870bbb3e0SAndrew Morton offset = do_div(logical_sb_block, blocksize); 3999a8ac900bSGioh Kim bh = sb_bread_unmovable(sb, logical_sb_block); 4000ac27a0ecSDave Kleikamp if (!bh) { 4001b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, 4002b31e1552SEric Sandeen "Can't read superblock on 2nd try"); 4003ac27a0ecSDave Kleikamp goto failed_mount; 4004ac27a0ecSDave Kleikamp } 40052716b802STheodore Ts'o es = (struct ext4_super_block *)(bh->b_data + offset); 4006ac27a0ecSDave Kleikamp sbi->s_es = es; 4007617ba13bSMingming Cao if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) { 4008b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, 4009b31e1552SEric Sandeen "Magic mismatch, very weird!"); 4010ac27a0ecSDave Kleikamp goto failed_mount; 4011ac27a0ecSDave Kleikamp } 4012ac27a0ecSDave Kleikamp } 4013ac27a0ecSDave Kleikamp 4014e2b911c5SDarrick J. Wong has_huge_files = ext4_has_feature_huge_file(sb); 4015f287a1a5STheodore Ts'o sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits, 4016f287a1a5STheodore Ts'o has_huge_files); 4017f287a1a5STheodore Ts'o sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files); 4018ac27a0ecSDave Kleikamp 4019617ba13bSMingming Cao if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) { 4020617ba13bSMingming Cao sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE; 4021617ba13bSMingming Cao sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO; 4022ac27a0ecSDave Kleikamp } else { 4023ac27a0ecSDave Kleikamp sbi->s_inode_size = le16_to_cpu(es->s_inode_size); 4024ac27a0ecSDave Kleikamp sbi->s_first_ino = le32_to_cpu(es->s_first_ino); 4025c37e9e01STheodore Ts'o if (sbi->s_first_ino < EXT4_GOOD_OLD_FIRST_INO) { 4026c37e9e01STheodore Ts'o ext4_msg(sb, KERN_ERR, "invalid first ino: %u", 4027c37e9e01STheodore Ts'o sbi->s_first_ino); 4028c37e9e01STheodore Ts'o goto failed_mount; 4029c37e9e01STheodore Ts'o } 4030617ba13bSMingming Cao if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) || 40311330593eSVignesh Babu (!is_power_of_2(sbi->s_inode_size)) || 4032ac27a0ecSDave Kleikamp (sbi->s_inode_size > blocksize)) { 4033b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, 4034b31e1552SEric Sandeen "unsupported inode size: %d", 4035ac27a0ecSDave Kleikamp sbi->s_inode_size); 4036ac27a0ecSDave Kleikamp goto failed_mount; 4037ac27a0ecSDave Kleikamp } 40384881c497SDeepa Dinamani /* 40394881c497SDeepa Dinamani * i_atime_extra is the last extra field available for [acm]times in 40404881c497SDeepa Dinamani * struct ext4_inode. Checking for that field should suffice to ensure 40414881c497SDeepa Dinamani * we have extra space for all three. 40424881c497SDeepa Dinamani */ 40434881c497SDeepa Dinamani if (sbi->s_inode_size >= offsetof(struct ext4_inode, i_atime_extra) + 40444881c497SDeepa Dinamani sizeof(((struct ext4_inode *)0)->i_atime_extra)) { 40454881c497SDeepa Dinamani sb->s_time_gran = 1; 40464881c497SDeepa Dinamani sb->s_time_max = EXT4_EXTRA_TIMESTAMP_MAX; 40474881c497SDeepa Dinamani } else { 40484881c497SDeepa Dinamani sb->s_time_gran = NSEC_PER_SEC; 40494881c497SDeepa Dinamani sb->s_time_max = EXT4_NON_EXTRA_TIMESTAMP_MAX; 40504881c497SDeepa Dinamani } 40514881c497SDeepa Dinamani 40524881c497SDeepa Dinamani sb->s_time_min = EXT4_TIMESTAMP_MIN; 4053ac27a0ecSDave Kleikamp } 40540b8e58a1SAndreas Dilger 40550d1ee42fSAlexandre Ratchov sbi->s_desc_size = le16_to_cpu(es->s_desc_size); 4056e2b911c5SDarrick J. Wong if (ext4_has_feature_64bit(sb)) { 40578fadc143SAlexandre Ratchov if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT || 40580d1ee42fSAlexandre Ratchov sbi->s_desc_size > EXT4_MAX_DESC_SIZE || 4059d8ea6cf8Svignesh babu !is_power_of_2(sbi->s_desc_size)) { 4060b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, 4061b31e1552SEric Sandeen "unsupported descriptor size %lu", 40620d1ee42fSAlexandre Ratchov sbi->s_desc_size); 40630d1ee42fSAlexandre Ratchov goto failed_mount; 40640d1ee42fSAlexandre Ratchov } 40650d1ee42fSAlexandre Ratchov } else 40660d1ee42fSAlexandre Ratchov sbi->s_desc_size = EXT4_MIN_DESC_SIZE; 40670b8e58a1SAndreas Dilger 4068ac27a0ecSDave Kleikamp sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group); 4069ac27a0ecSDave Kleikamp sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group); 40700b8e58a1SAndreas Dilger 4071617ba13bSMingming Cao sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb); 4072ac27a0ecSDave Kleikamp if (sbi->s_inodes_per_block == 0) 4073617ba13bSMingming Cao goto cantfind_ext4; 4074cd6bb35bSTheodore Ts'o if (sbi->s_inodes_per_group < sbi->s_inodes_per_block || 4075cd6bb35bSTheodore Ts'o sbi->s_inodes_per_group > blocksize * 8) { 4076cd6bb35bSTheodore Ts'o ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu\n", 4077cd6bb35bSTheodore Ts'o sbi->s_blocks_per_group); 4078cd6bb35bSTheodore Ts'o goto failed_mount; 4079cd6bb35bSTheodore Ts'o } 4080ac27a0ecSDave Kleikamp sbi->s_itb_per_group = sbi->s_inodes_per_group / 4081ac27a0ecSDave Kleikamp sbi->s_inodes_per_block; 40820d1ee42fSAlexandre Ratchov sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb); 4083ac27a0ecSDave Kleikamp sbi->s_sbh = bh; 4084ac27a0ecSDave Kleikamp sbi->s_mount_state = le16_to_cpu(es->s_state); 4085e57aa839SFengguang Wu sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb)); 4086e57aa839SFengguang Wu sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb)); 40870b8e58a1SAndreas Dilger 4088ac27a0ecSDave Kleikamp for (i = 0; i < 4; i++) 4089ac27a0ecSDave Kleikamp sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]); 4090ac27a0ecSDave Kleikamp sbi->s_def_hash_version = es->s_def_hash_version; 4091e2b911c5SDarrick J. Wong if (ext4_has_feature_dir_index(sb)) { 4092f99b2589STheodore Ts'o i = le32_to_cpu(es->s_flags); 4093f99b2589STheodore Ts'o if (i & EXT2_FLAGS_UNSIGNED_HASH) 4094f99b2589STheodore Ts'o sbi->s_hash_unsigned = 3; 4095f99b2589STheodore Ts'o else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) { 4096f99b2589STheodore Ts'o #ifdef __CHAR_UNSIGNED__ 4097bc98a42cSDavid Howells if (!sb_rdonly(sb)) 409823301410STheodore Ts'o es->s_flags |= 409923301410STheodore Ts'o cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH); 4100f99b2589STheodore Ts'o sbi->s_hash_unsigned = 3; 4101f99b2589STheodore Ts'o #else 4102bc98a42cSDavid Howells if (!sb_rdonly(sb)) 410323301410STheodore Ts'o es->s_flags |= 410423301410STheodore Ts'o cpu_to_le32(EXT2_FLAGS_SIGNED_HASH); 4105f99b2589STheodore Ts'o #endif 4106f99b2589STheodore Ts'o } 410723301410STheodore Ts'o } 4108ac27a0ecSDave Kleikamp 4109281b5995STheodore Ts'o /* Handle clustersize */ 4110281b5995STheodore Ts'o clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size); 4111e2b911c5SDarrick J. Wong has_bigalloc = ext4_has_feature_bigalloc(sb); 4112281b5995STheodore Ts'o if (has_bigalloc) { 4113281b5995STheodore Ts'o if (clustersize < blocksize) { 4114281b5995STheodore Ts'o ext4_msg(sb, KERN_ERR, 4115281b5995STheodore Ts'o "cluster size (%d) smaller than " 4116281b5995STheodore Ts'o "block size (%d)", clustersize, blocksize); 4117281b5995STheodore Ts'o goto failed_mount; 4118281b5995STheodore Ts'o } 4119281b5995STheodore Ts'o sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) - 4120281b5995STheodore Ts'o le32_to_cpu(es->s_log_block_size); 4121281b5995STheodore Ts'o sbi->s_clusters_per_group = 4122281b5995STheodore Ts'o le32_to_cpu(es->s_clusters_per_group); 4123281b5995STheodore Ts'o if (sbi->s_clusters_per_group > blocksize * 8) { 4124281b5995STheodore Ts'o ext4_msg(sb, KERN_ERR, 4125281b5995STheodore Ts'o "#clusters per group too big: %lu", 4126281b5995STheodore Ts'o sbi->s_clusters_per_group); 4127281b5995STheodore Ts'o goto failed_mount; 4128281b5995STheodore Ts'o } 4129281b5995STheodore Ts'o if (sbi->s_blocks_per_group != 4130281b5995STheodore Ts'o (sbi->s_clusters_per_group * (clustersize / blocksize))) { 4131281b5995STheodore Ts'o ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and " 4132281b5995STheodore Ts'o "clusters per group (%lu) inconsistent", 4133281b5995STheodore Ts'o sbi->s_blocks_per_group, 4134281b5995STheodore Ts'o sbi->s_clusters_per_group); 4135281b5995STheodore Ts'o goto failed_mount; 4136281b5995STheodore Ts'o } 4137281b5995STheodore Ts'o } else { 4138281b5995STheodore Ts'o if (clustersize != blocksize) { 4139bfe0a5f4STheodore Ts'o ext4_msg(sb, KERN_ERR, 4140bfe0a5f4STheodore Ts'o "fragment/cluster size (%d) != " 4141bfe0a5f4STheodore Ts'o "block size (%d)", clustersize, blocksize); 4142bfe0a5f4STheodore Ts'o goto failed_mount; 4143281b5995STheodore Ts'o } 4144ac27a0ecSDave Kleikamp if (sbi->s_blocks_per_group > blocksize * 8) { 4145b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, 4146b31e1552SEric Sandeen "#blocks per group too big: %lu", 4147ac27a0ecSDave Kleikamp sbi->s_blocks_per_group); 4148ac27a0ecSDave Kleikamp goto failed_mount; 4149ac27a0ecSDave Kleikamp } 4150281b5995STheodore Ts'o sbi->s_clusters_per_group = sbi->s_blocks_per_group; 4151281b5995STheodore Ts'o sbi->s_cluster_bits = 0; 4152281b5995STheodore Ts'o } 4153281b5995STheodore Ts'o sbi->s_cluster_ratio = clustersize / blocksize; 4154281b5995STheodore Ts'o 4155960fd856STheodore Ts'o /* Do we have standard group size of clustersize * 8 blocks ? */ 4156960fd856STheodore Ts'o if (sbi->s_blocks_per_group == clustersize << 3) 4157960fd856STheodore Ts'o set_opt2(sb, STD_GROUP_SIZE); 4158960fd856STheodore Ts'o 4159bf43d84bSEric Sandeen /* 4160bf43d84bSEric Sandeen * Test whether we have more sectors than will fit in sector_t, 4161bf43d84bSEric Sandeen * and whether the max offset is addressable by the page cache. 4162bf43d84bSEric Sandeen */ 41635a9ae68aSDarrick J. Wong err = generic_check_addressable(sb->s_blocksize_bits, 416430ca22c7SPatrick J. LoPresti ext4_blocks_count(es)); 41655a9ae68aSDarrick J. Wong if (err) { 4166b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "filesystem" 4167bf43d84bSEric Sandeen " too large to mount safely on this system"); 4168ac27a0ecSDave Kleikamp goto failed_mount; 4169ac27a0ecSDave Kleikamp } 4170ac27a0ecSDave Kleikamp 4171617ba13bSMingming Cao if (EXT4_BLOCKS_PER_GROUP(sb) == 0) 4172617ba13bSMingming Cao goto cantfind_ext4; 4173e7c95593SEric Sandeen 41740f2ddca6SFrom: Thiemo Nagel /* check blocks count against device size */ 41750f2ddca6SFrom: Thiemo Nagel blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits; 41760f2ddca6SFrom: Thiemo Nagel if (blocks_count && ext4_blocks_count(es) > blocks_count) { 4177b31e1552SEric Sandeen ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu " 4178b31e1552SEric Sandeen "exceeds size of device (%llu blocks)", 41790f2ddca6SFrom: Thiemo Nagel ext4_blocks_count(es), blocks_count); 41800f2ddca6SFrom: Thiemo Nagel goto failed_mount; 41810f2ddca6SFrom: Thiemo Nagel } 41820f2ddca6SFrom: Thiemo Nagel 41834ec11028STheodore Ts'o /* 41844ec11028STheodore Ts'o * It makes no sense for the first data block to be beyond the end 41854ec11028STheodore Ts'o * of the filesystem. 41864ec11028STheodore Ts'o */ 41874ec11028STheodore Ts'o if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) { 4188b31e1552SEric Sandeen ext4_msg(sb, KERN_WARNING, "bad geometry: first data " 4189b31e1552SEric Sandeen "block %u is beyond end of filesystem (%llu)", 4190e7c95593SEric Sandeen le32_to_cpu(es->s_first_data_block), 41914ec11028STheodore Ts'o ext4_blocks_count(es)); 4192e7c95593SEric Sandeen goto failed_mount; 4193e7c95593SEric Sandeen } 4194bfe0a5f4STheodore Ts'o if ((es->s_first_data_block == 0) && (es->s_log_block_size == 0) && 4195bfe0a5f4STheodore Ts'o (sbi->s_cluster_ratio == 1)) { 4196bfe0a5f4STheodore Ts'o ext4_msg(sb, KERN_WARNING, "bad geometry: first data " 4197bfe0a5f4STheodore Ts'o "block is 0 with a 1k block and cluster size"); 4198bfe0a5f4STheodore Ts'o goto failed_mount; 4199bfe0a5f4STheodore Ts'o } 4200bfe0a5f4STheodore Ts'o 4201bd81d8eeSLaurent Vivier blocks_count = (ext4_blocks_count(es) - 4202bd81d8eeSLaurent Vivier le32_to_cpu(es->s_first_data_block) + 4203bd81d8eeSLaurent Vivier EXT4_BLOCKS_PER_GROUP(sb) - 1); 4204bd81d8eeSLaurent Vivier do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb)); 42054ec11028STheodore Ts'o if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) { 4206b31e1552SEric Sandeen ext4_msg(sb, KERN_WARNING, "groups count too large: %u " 42074ec11028STheodore Ts'o "(block count %llu, first data block %u, " 4208b31e1552SEric Sandeen "blocks per group %lu)", sbi->s_groups_count, 42094ec11028STheodore Ts'o ext4_blocks_count(es), 42104ec11028STheodore Ts'o le32_to_cpu(es->s_first_data_block), 42114ec11028STheodore Ts'o EXT4_BLOCKS_PER_GROUP(sb)); 42124ec11028STheodore Ts'o goto failed_mount; 42134ec11028STheodore Ts'o } 4214bd81d8eeSLaurent Vivier sbi->s_groups_count = blocks_count; 4215fb0a387dSEric Sandeen sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count, 4216fb0a387dSEric Sandeen (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb))); 42179e463084STheodore Ts'o if (((u64)sbi->s_groups_count * sbi->s_inodes_per_group) != 42189e463084STheodore Ts'o le32_to_cpu(es->s_inodes_count)) { 42199e463084STheodore Ts'o ext4_msg(sb, KERN_ERR, "inodes count not valid: %u vs %llu", 42209e463084STheodore Ts'o le32_to_cpu(es->s_inodes_count), 42219e463084STheodore Ts'o ((u64)sbi->s_groups_count * sbi->s_inodes_per_group)); 42229e463084STheodore Ts'o ret = -EINVAL; 42239e463084STheodore Ts'o goto failed_mount; 42249e463084STheodore Ts'o } 4225617ba13bSMingming Cao db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) / 4226617ba13bSMingming Cao EXT4_DESC_PER_BLOCK(sb); 42273a4b77cdSEryu Guan if (ext4_has_feature_meta_bg(sb)) { 42282ba3e6e8STheodore Ts'o if (le32_to_cpu(es->s_first_meta_bg) > db_count) { 42293a4b77cdSEryu Guan ext4_msg(sb, KERN_WARNING, 42303a4b77cdSEryu Guan "first meta block group too large: %u " 42313a4b77cdSEryu Guan "(group descriptor block count %u)", 42323a4b77cdSEryu Guan le32_to_cpu(es->s_first_meta_bg), db_count); 42333a4b77cdSEryu Guan goto failed_mount; 42343a4b77cdSEryu Guan } 42353a4b77cdSEryu Guan } 4236344476e1SKees Cook sbi->s_group_desc = kvmalloc_array(db_count, 4237f18a5f21STheodore Ts'o sizeof(struct buffer_head *), 4238ac27a0ecSDave Kleikamp GFP_KERNEL); 4239ac27a0ecSDave Kleikamp if (sbi->s_group_desc == NULL) { 4240b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "not enough memory"); 42412cde417dSTheodore Ts'o ret = -ENOMEM; 4242ac27a0ecSDave Kleikamp goto failed_mount; 4243ac27a0ecSDave Kleikamp } 4244ac27a0ecSDave Kleikamp 4245705895b6SPekka Enberg bgl_lock_init(sbi->s_blockgroup_lock); 4246ac27a0ecSDave Kleikamp 424785c8f176SAndrew Perepechko /* Pre-read the descriptors into the buffer cache */ 424885c8f176SAndrew Perepechko for (i = 0; i < db_count; i++) { 424985c8f176SAndrew Perepechko block = descriptor_loc(sb, logical_sb_block, i); 425085c8f176SAndrew Perepechko sb_breadahead(sb, block); 425185c8f176SAndrew Perepechko } 425285c8f176SAndrew Perepechko 4253ac27a0ecSDave Kleikamp for (i = 0; i < db_count; i++) { 425470bbb3e0SAndrew Morton block = descriptor_loc(sb, logical_sb_block, i); 4255a8ac900bSGioh Kim sbi->s_group_desc[i] = sb_bread_unmovable(sb, block); 4256ac27a0ecSDave Kleikamp if (!sbi->s_group_desc[i]) { 4257b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, 4258b31e1552SEric Sandeen "can't read group descriptor %d", i); 4259ac27a0ecSDave Kleikamp db_count = i; 4260ac27a0ecSDave Kleikamp goto failed_mount2; 4261ac27a0ecSDave Kleikamp } 4262ac27a0ecSDave Kleikamp } 426344de022cSTheodore Ts'o sbi->s_gdb_count = db_count; 4264829fa70dSTheodore Ts'o if (!ext4_check_descriptors(sb, logical_sb_block, &first_not_zeroed)) { 4265b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "group descriptors corrupted!"); 42666a797d27SDarrick J. Wong ret = -EFSCORRUPTED; 4267f9ae9cf5STheodore Ts'o goto failed_mount2; 4268ac27a0ecSDave Kleikamp } 4269772cb7c8SJose R. Santos 4270235699a8SKees Cook timer_setup(&sbi->s_err_report, print_daily_error_info, 0); 427104496411STao Ma 4272a75ae78fSDmitry Monakhov /* Register extent status tree shrinker */ 4273eb68d0e2SZheng Liu if (ext4_es_register_shrinker(sbi)) 4274ce7e010aSTheodore Ts'o goto failed_mount3; 4275ce7e010aSTheodore Ts'o 4276c9de560dSAlex Tomas sbi->s_stripe = ext4_get_stripe_size(sbi); 427767a5da56SZheng Liu sbi->s_extent_max_zeroout_kb = 32; 4278c9de560dSAlex Tomas 4279f9ae9cf5STheodore Ts'o /* 4280f9ae9cf5STheodore Ts'o * set up enough so that it can read an inode 4281f9ae9cf5STheodore Ts'o */ 4282f9ae9cf5STheodore Ts'o sb->s_op = &ext4_sops; 4283617ba13bSMingming Cao sb->s_export_op = &ext4_export_ops; 4284617ba13bSMingming Cao sb->s_xattr = ext4_xattr_handlers; 4285643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION 4286a7550b30SJaegeuk Kim sb->s_cop = &ext4_cryptops; 4287ffcc4182SEric Biggers #endif 4288c93d8f88SEric Biggers #ifdef CONFIG_FS_VERITY 4289c93d8f88SEric Biggers sb->s_vop = &ext4_verityops; 4290c93d8f88SEric Biggers #endif 4291ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 4292617ba13bSMingming Cao sb->dq_op = &ext4_quota_operations; 4293e2b911c5SDarrick J. Wong if (ext4_has_feature_quota(sb)) 42941fa5efe3SJan Kara sb->s_qcop = &dquot_quotactl_sysfile_ops; 4295262b4662SJan Kara else 4296262b4662SJan Kara sb->s_qcop = &ext4_qctl_operations; 4297689c958cSLi Xi sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ; 4298ac27a0ecSDave Kleikamp #endif 429985787090SChristoph Hellwig memcpy(&sb->s_uuid, es->s_uuid, sizeof(es->s_uuid)); 4300f2fa2ffcSAneesh Kumar K.V 4301ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */ 43023b9d4ed2STheodore Ts'o mutex_init(&sbi->s_orphan_lock); 4303ac27a0ecSDave Kleikamp 4304ac27a0ecSDave Kleikamp sb->s_root = NULL; 4305ac27a0ecSDave Kleikamp 4306ac27a0ecSDave Kleikamp needs_recovery = (es->s_last_orphan != 0 || 4307e2b911c5SDarrick J. Wong ext4_has_feature_journal_needs_recovery(sb)); 4308ac27a0ecSDave Kleikamp 4309bc98a42cSDavid Howells if (ext4_has_feature_mmp(sb) && !sb_rdonly(sb)) 4310c5e06d10SJohann Lombardi if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block))) 431150460fe8SDarrick J. Wong goto failed_mount3a; 4312c5e06d10SJohann Lombardi 4313ac27a0ecSDave Kleikamp /* 4314ac27a0ecSDave Kleikamp * The first inode we look at is the journal inode. Don't try 4315ac27a0ecSDave Kleikamp * root first: it may be modified in the journal! 4316ac27a0ecSDave Kleikamp */ 4317e2b911c5SDarrick J. Wong if (!test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb)) { 43184753d8a2STheodore Ts'o err = ext4_load_journal(sb, es, journal_devnum); 43194753d8a2STheodore Ts'o if (err) 432050460fe8SDarrick J. Wong goto failed_mount3a; 4321bc98a42cSDavid Howells } else if (test_opt(sb, NOLOAD) && !sb_rdonly(sb) && 4322e2b911c5SDarrick J. Wong ext4_has_feature_journal_needs_recovery(sb)) { 4323b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "required journal recovery " 4324b31e1552SEric Sandeen "suppressed and not mounted read-only"); 4325744692dcSJiaying Zhang goto failed_mount_wq; 4326ac27a0ecSDave Kleikamp } else { 43271e381f60SDmitry Monakhov /* Nojournal mode, all journal mount options are illegal */ 43281e381f60SDmitry Monakhov if (test_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM)) { 43291e381f60SDmitry Monakhov ext4_msg(sb, KERN_ERR, "can't mount with " 43301e381f60SDmitry Monakhov "journal_checksum, fs mounted w/o journal"); 43311e381f60SDmitry Monakhov goto failed_mount_wq; 43321e381f60SDmitry Monakhov } 43331e381f60SDmitry Monakhov if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) { 43341e381f60SDmitry Monakhov ext4_msg(sb, KERN_ERR, "can't mount with " 43351e381f60SDmitry Monakhov "journal_async_commit, fs mounted w/o journal"); 43361e381f60SDmitry Monakhov goto failed_mount_wq; 43371e381f60SDmitry Monakhov } 43381e381f60SDmitry Monakhov if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) { 43391e381f60SDmitry Monakhov ext4_msg(sb, KERN_ERR, "can't mount with " 43401e381f60SDmitry Monakhov "commit=%lu, fs mounted w/o journal", 43411e381f60SDmitry Monakhov sbi->s_commit_interval / HZ); 43421e381f60SDmitry Monakhov goto failed_mount_wq; 43431e381f60SDmitry Monakhov } 43441e381f60SDmitry Monakhov if (EXT4_MOUNT_DATA_FLAGS & 43451e381f60SDmitry Monakhov (sbi->s_mount_opt ^ sbi->s_def_mount_opt)) { 43461e381f60SDmitry Monakhov ext4_msg(sb, KERN_ERR, "can't mount with " 43471e381f60SDmitry Monakhov "data=, fs mounted w/o journal"); 43481e381f60SDmitry Monakhov goto failed_mount_wq; 43491e381f60SDmitry Monakhov } 435050b29d8fSDebabrata Banerjee sbi->s_def_mount_opt &= ~EXT4_MOUNT_JOURNAL_CHECKSUM; 43511e381f60SDmitry Monakhov clear_opt(sb, JOURNAL_CHECKSUM); 4352fd8c37ecSTheodore Ts'o clear_opt(sb, DATA_FLAGS); 43530390131bSFrank Mayhar sbi->s_journal = NULL; 43540390131bSFrank Mayhar needs_recovery = 0; 43550390131bSFrank Mayhar goto no_journal; 4356ac27a0ecSDave Kleikamp } 4357ac27a0ecSDave Kleikamp 4358e2b911c5SDarrick J. Wong if (ext4_has_feature_64bit(sb) && 4359eb40a09cSJose R. Santos !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0, 4360eb40a09cSJose R. Santos JBD2_FEATURE_INCOMPAT_64BIT)) { 4361b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature"); 4362744692dcSJiaying Zhang goto failed_mount_wq; 4363eb40a09cSJose R. Santos } 4364eb40a09cSJose R. Santos 436525ed6e8aSDarrick J. Wong if (!set_journal_csum_feature_set(sb)) { 436625ed6e8aSDarrick J. Wong ext4_msg(sb, KERN_ERR, "Failed to set journal checksum " 436725ed6e8aSDarrick J. Wong "feature set"); 436825ed6e8aSDarrick J. Wong goto failed_mount_wq; 4369d4da6c9cSLinus Torvalds } 4370818d276cSGirish Shilamkar 4371ac27a0ecSDave Kleikamp /* We have now updated the journal if required, so we can 4372ac27a0ecSDave Kleikamp * validate the data journaling mode. */ 4373ac27a0ecSDave Kleikamp switch (test_opt(sb, DATA_FLAGS)) { 4374ac27a0ecSDave Kleikamp case 0: 4375ac27a0ecSDave Kleikamp /* No mode set, assume a default based on the journal 437663f57933SAndrew Morton * capabilities: ORDERED_DATA if the journal can 437763f57933SAndrew Morton * cope, else JOURNAL_DATA 437863f57933SAndrew Morton */ 4379dab291afSMingming Cao if (jbd2_journal_check_available_features 438027f394a7STyson Nottingham (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) { 4381fd8c37ecSTheodore Ts'o set_opt(sb, ORDERED_DATA); 438227f394a7STyson Nottingham sbi->s_def_mount_opt |= EXT4_MOUNT_ORDERED_DATA; 438327f394a7STyson Nottingham } else { 4384fd8c37ecSTheodore Ts'o set_opt(sb, JOURNAL_DATA); 438527f394a7STyson Nottingham sbi->s_def_mount_opt |= EXT4_MOUNT_JOURNAL_DATA; 438627f394a7STyson Nottingham } 4387ac27a0ecSDave Kleikamp break; 4388ac27a0ecSDave Kleikamp 4389617ba13bSMingming Cao case EXT4_MOUNT_ORDERED_DATA: 4390617ba13bSMingming Cao case EXT4_MOUNT_WRITEBACK_DATA: 4391dab291afSMingming Cao if (!jbd2_journal_check_available_features 4392dab291afSMingming Cao (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) { 4393b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "Journal does not support " 4394b31e1552SEric Sandeen "requested data journaling mode"); 4395744692dcSJiaying Zhang goto failed_mount_wq; 4396ac27a0ecSDave Kleikamp } 4397ac27a0ecSDave Kleikamp default: 4398ac27a0ecSDave Kleikamp break; 4399ac27a0ecSDave Kleikamp } 4400ab04df78SJan Kara 4401ab04df78SJan Kara if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA && 4402ab04df78SJan Kara test_opt(sb, JOURNAL_ASYNC_COMMIT)) { 4403ab04df78SJan Kara ext4_msg(sb, KERN_ERR, "can't mount with " 4404ab04df78SJan Kara "journal_async_commit in data=ordered mode"); 4405ab04df78SJan Kara goto failed_mount_wq; 4406ab04df78SJan Kara } 4407ab04df78SJan Kara 4408b3881f74STheodore Ts'o set_task_ioprio(sbi->s_journal->j_task, journal_ioprio); 4409ac27a0ecSDave Kleikamp 441018aadd47SBobi Jam sbi->s_journal->j_commit_callback = ext4_journal_commit_callback; 441118aadd47SBobi Jam 4412ce7e010aSTheodore Ts'o no_journal: 4413cdb7ee4cSTahsin Erdogan if (!test_opt(sb, NO_MBCACHE)) { 441447387409STahsin Erdogan sbi->s_ea_block_cache = ext4_xattr_create_cache(); 441547387409STahsin Erdogan if (!sbi->s_ea_block_cache) { 4416cdb7ee4cSTahsin Erdogan ext4_msg(sb, KERN_ERR, 4417cdb7ee4cSTahsin Erdogan "Failed to create ea_block_cache"); 44189c191f70ST Makphaibulchoke goto failed_mount_wq; 44199c191f70ST Makphaibulchoke } 44209c191f70ST Makphaibulchoke 4421dec214d0STahsin Erdogan if (ext4_has_feature_ea_inode(sb)) { 4422dec214d0STahsin Erdogan sbi->s_ea_inode_cache = ext4_xattr_create_cache(); 4423dec214d0STahsin Erdogan if (!sbi->s_ea_inode_cache) { 4424dec214d0STahsin Erdogan ext4_msg(sb, KERN_ERR, 4425dec214d0STahsin Erdogan "Failed to create ea_inode_cache"); 4426dec214d0STahsin Erdogan goto failed_mount_wq; 4427dec214d0STahsin Erdogan } 4428dec214d0STahsin Erdogan } 4429cdb7ee4cSTahsin Erdogan } 4430dec214d0STahsin Erdogan 4431c93d8f88SEric Biggers if (ext4_has_feature_verity(sb) && blocksize != PAGE_SIZE) { 4432c93d8f88SEric Biggers ext4_msg(sb, KERN_ERR, "Unsupported blocksize for fs-verity"); 4433c93d8f88SEric Biggers goto failed_mount_wq; 4434c93d8f88SEric Biggers } 4435c93d8f88SEric Biggers 4436bc98a42cSDavid Howells if (DUMMY_ENCRYPTION_ENABLED(sbi) && !sb_rdonly(sb) && 4437e2b911c5SDarrick J. Wong !ext4_has_feature_encrypt(sb)) { 4438e2b911c5SDarrick J. Wong ext4_set_feature_encrypt(sb); 44396ddb2447STheodore Ts'o ext4_commit_super(sb, 1); 44406ddb2447STheodore Ts'o } 44416ddb2447STheodore Ts'o 4442fd89d5f2STejun Heo /* 4443952fc18eSTheodore Ts'o * Get the # of file system overhead blocks from the 4444952fc18eSTheodore Ts'o * superblock if present. 4445952fc18eSTheodore Ts'o */ 4446952fc18eSTheodore Ts'o if (es->s_overhead_clusters) 4447952fc18eSTheodore Ts'o sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters); 4448952fc18eSTheodore Ts'o else { 444907aa2ea1SLukas Czerner err = ext4_calculate_overhead(sb); 445007aa2ea1SLukas Czerner if (err) 4451952fc18eSTheodore Ts'o goto failed_mount_wq; 4452952fc18eSTheodore Ts'o } 4453952fc18eSTheodore Ts'o 4454952fc18eSTheodore Ts'o /* 4455fd89d5f2STejun Heo * The maximum number of concurrent works can be high and 4456fd89d5f2STejun Heo * concurrency isn't really necessary. Limit it to 1. 4457fd89d5f2STejun Heo */ 44582e8fa54eSJan Kara EXT4_SB(sb)->rsv_conversion_wq = 44592e8fa54eSJan Kara alloc_workqueue("ext4-rsv-conversion", WQ_MEM_RECLAIM | WQ_UNBOUND, 1); 44602e8fa54eSJan Kara if (!EXT4_SB(sb)->rsv_conversion_wq) { 44612e8fa54eSJan Kara printk(KERN_ERR "EXT4-fs: failed to create workqueue\n"); 446207aa2ea1SLukas Czerner ret = -ENOMEM; 44632e8fa54eSJan Kara goto failed_mount4; 44642e8fa54eSJan Kara } 44652e8fa54eSJan Kara 4466ac27a0ecSDave Kleikamp /* 4467dab291afSMingming Cao * The jbd2_journal_load will have done any necessary log recovery, 4468ac27a0ecSDave Kleikamp * so we can safely mount the rest of the filesystem now. 4469ac27a0ecSDave Kleikamp */ 4470ac27a0ecSDave Kleikamp 44718a363970STheodore Ts'o root = ext4_iget(sb, EXT4_ROOT_INO, EXT4_IGET_SPECIAL); 44721d1fe1eeSDavid Howells if (IS_ERR(root)) { 4473b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "get root inode failed"); 44741d1fe1eeSDavid Howells ret = PTR_ERR(root); 447532a9bb57SManish Katiyar root = NULL; 4476ac27a0ecSDave Kleikamp goto failed_mount4; 4477ac27a0ecSDave Kleikamp } 4478ac27a0ecSDave Kleikamp if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) { 4479b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck"); 448094bf608aSAl Viro iput(root); 4481ac27a0ecSDave Kleikamp goto failed_mount4; 4482ac27a0ecSDave Kleikamp } 4483b886ee3eSGabriel Krisman Bertazi 4484b886ee3eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE 4485b886ee3eSGabriel Krisman Bertazi if (sbi->s_encoding) 4486b886ee3eSGabriel Krisman Bertazi sb->s_d_op = &ext4_dentry_ops; 4487b886ee3eSGabriel Krisman Bertazi #endif 4488b886ee3eSGabriel Krisman Bertazi 448948fde701SAl Viro sb->s_root = d_make_root(root); 44901d1fe1eeSDavid Howells if (!sb->s_root) { 4491b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "get root dentry failed"); 44921d1fe1eeSDavid Howells ret = -ENOMEM; 44931d1fe1eeSDavid Howells goto failed_mount4; 44941d1fe1eeSDavid Howells } 4495ac27a0ecSDave Kleikamp 4496c89128a0SJaegeuk Kim ret = ext4_setup_super(sb, es, sb_rdonly(sb)); 4497c89128a0SJaegeuk Kim if (ret == -EROFS) { 44981751e8a6SLinus Torvalds sb->s_flags |= SB_RDONLY; 4499c89128a0SJaegeuk Kim ret = 0; 4500c89128a0SJaegeuk Kim } else if (ret) 4501c89128a0SJaegeuk Kim goto failed_mount4a; 4502ef7f3835SKalpak Shah 45037bc04c5cSBarret Rhoden ext4_clamp_want_extra_isize(sb); 4504ef7f3835SKalpak Shah 4505b5799018STheodore Ts'o ext4_set_resv_clusters(sb); 450627dd4385SLukas Czerner 45076fd058f7STheodore Ts'o err = ext4_setup_system_zone(sb); 45086fd058f7STheodore Ts'o if (err) { 4509b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "failed to initialize system " 4510fbe845ddSCurt Wohlgemuth "zone (%d)", err); 4511f9ae9cf5STheodore Ts'o goto failed_mount4a; 4512f9ae9cf5STheodore Ts'o } 4513f9ae9cf5STheodore Ts'o 4514f9ae9cf5STheodore Ts'o ext4_ext_init(sb); 4515f9ae9cf5STheodore Ts'o err = ext4_mb_init(sb); 4516f9ae9cf5STheodore Ts'o if (err) { 4517f9ae9cf5STheodore Ts'o ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)", 4518f9ae9cf5STheodore Ts'o err); 4519dcf2d804STao Ma goto failed_mount5; 4520c2774d84SAneesh Kumar K.V } 4521c2774d84SAneesh Kumar K.V 4522d5e03cbbSTheodore Ts'o block = ext4_count_free_clusters(sb); 4523d5e03cbbSTheodore Ts'o ext4_free_blocks_count_set(sbi->s_es, 4524d5e03cbbSTheodore Ts'o EXT4_C2B(sbi, block)); 45254274f516STheodore Ts'o ext4_superblock_csum_set(sb); 4526908c7f19STejun Heo err = percpu_counter_init(&sbi->s_freeclusters_counter, block, 4527908c7f19STejun Heo GFP_KERNEL); 4528d5e03cbbSTheodore Ts'o if (!err) { 4529d5e03cbbSTheodore Ts'o unsigned long freei = ext4_count_free_inodes(sb); 4530d5e03cbbSTheodore Ts'o sbi->s_es->s_free_inodes_count = cpu_to_le32(freei); 45314274f516STheodore Ts'o ext4_superblock_csum_set(sb); 4532908c7f19STejun Heo err = percpu_counter_init(&sbi->s_freeinodes_counter, freei, 4533908c7f19STejun Heo GFP_KERNEL); 4534d5e03cbbSTheodore Ts'o } 4535d5e03cbbSTheodore Ts'o if (!err) 4536d5e03cbbSTheodore Ts'o err = percpu_counter_init(&sbi->s_dirs_counter, 4537908c7f19STejun Heo ext4_count_dirs(sb), GFP_KERNEL); 4538d5e03cbbSTheodore Ts'o if (!err) 4539908c7f19STejun Heo err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0, 4540908c7f19STejun Heo GFP_KERNEL); 4541c8585c6fSDaeho Jeong if (!err) 4542c8585c6fSDaeho Jeong err = percpu_init_rwsem(&sbi->s_journal_flag_rwsem); 4543c8585c6fSDaeho Jeong 4544d5e03cbbSTheodore Ts'o if (err) { 4545d5e03cbbSTheodore Ts'o ext4_msg(sb, KERN_ERR, "insufficient memory"); 4546d5e03cbbSTheodore Ts'o goto failed_mount6; 4547d5e03cbbSTheodore Ts'o } 4548d5e03cbbSTheodore Ts'o 4549e2b911c5SDarrick J. Wong if (ext4_has_feature_flex_bg(sb)) 4550d5e03cbbSTheodore Ts'o if (!ext4_fill_flex_info(sb)) { 4551d5e03cbbSTheodore Ts'o ext4_msg(sb, KERN_ERR, 4552d5e03cbbSTheodore Ts'o "unable to initialize " 4553d5e03cbbSTheodore Ts'o "flex_bg meta info!"); 4554d5e03cbbSTheodore Ts'o goto failed_mount6; 4555d5e03cbbSTheodore Ts'o } 4556d5e03cbbSTheodore Ts'o 4557bfff6873SLukas Czerner err = ext4_register_li_request(sb, first_not_zeroed); 4558bfff6873SLukas Czerner if (err) 4559dcf2d804STao Ma goto failed_mount6; 4560bfff6873SLukas Czerner 4561b5799018STheodore Ts'o err = ext4_register_sysfs(sb); 4562dcf2d804STao Ma if (err) 4563dcf2d804STao Ma goto failed_mount7; 45643197ebdbSTheodore Ts'o 45659b2ff357SJan Kara #ifdef CONFIG_QUOTA 45669b2ff357SJan Kara /* Enable quota usage during mount. */ 4567bc98a42cSDavid Howells if (ext4_has_feature_quota(sb) && !sb_rdonly(sb)) { 45689b2ff357SJan Kara err = ext4_enable_quotas(sb); 45699b2ff357SJan Kara if (err) 45709b2ff357SJan Kara goto failed_mount8; 45719b2ff357SJan Kara } 45729b2ff357SJan Kara #endif /* CONFIG_QUOTA */ 45739b2ff357SJan Kara 4574617ba13bSMingming Cao EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS; 4575617ba13bSMingming Cao ext4_orphan_cleanup(sb, es); 4576617ba13bSMingming Cao EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS; 45770390131bSFrank Mayhar if (needs_recovery) { 4578b31e1552SEric Sandeen ext4_msg(sb, KERN_INFO, "recovery complete"); 4579617ba13bSMingming Cao ext4_mark_recovery_complete(sb, es); 45800390131bSFrank Mayhar } 45810390131bSFrank Mayhar if (EXT4_SB(sb)->s_journal) { 45820390131bSFrank Mayhar if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) 45830390131bSFrank Mayhar descr = " journalled data mode"; 45840390131bSFrank Mayhar else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) 45850390131bSFrank Mayhar descr = " ordered data mode"; 45860390131bSFrank Mayhar else 45870390131bSFrank Mayhar descr = " writeback data mode"; 45880390131bSFrank Mayhar } else 45890390131bSFrank Mayhar descr = "out journal"; 45900390131bSFrank Mayhar 459179add3a3SLukas Czerner if (test_opt(sb, DISCARD)) { 459279add3a3SLukas Czerner struct request_queue *q = bdev_get_queue(sb->s_bdev); 459379add3a3SLukas Czerner if (!blk_queue_discard(q)) 459479add3a3SLukas Czerner ext4_msg(sb, KERN_WARNING, 459579add3a3SLukas Czerner "mounting with \"discard\" option, but " 459679add3a3SLukas Czerner "the device does not support discard"); 459779add3a3SLukas Czerner } 459879add3a3SLukas Czerner 4599e294a537STheodore Ts'o if (___ratelimit(&ext4_mount_msg_ratelimit, "EXT4-fs mount")) 4600d4c402d9SCurt Wohlgemuth ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. " 46015aee0f8aSTheodore Ts'o "Opts: %.*s%s%s", descr, 46025aee0f8aSTheodore Ts'o (int) sizeof(sbi->s_es->s_mount_opts), 46035aee0f8aSTheodore Ts'o sbi->s_es->s_mount_opts, 46048b67f04aSTheodore Ts'o *sbi->s_es->s_mount_opts ? "; " : "", orig_data); 4605ac27a0ecSDave Kleikamp 460666e61a9eSTheodore Ts'o if (es->s_error_count) 460766e61a9eSTheodore Ts'o mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */ 4608ac27a0ecSDave Kleikamp 4609efbed4dcSTheodore Ts'o /* Enable message ratelimiting. Default is 10 messages per 5 secs. */ 4610efbed4dcSTheodore Ts'o ratelimit_state_init(&sbi->s_err_ratelimit_state, 5 * HZ, 10); 4611efbed4dcSTheodore Ts'o ratelimit_state_init(&sbi->s_warning_ratelimit_state, 5 * HZ, 10); 4612efbed4dcSTheodore Ts'o ratelimit_state_init(&sbi->s_msg_ratelimit_state, 5 * HZ, 10); 4613efbed4dcSTheodore Ts'o 4614d4c402d9SCurt Wohlgemuth kfree(orig_data); 4615ac27a0ecSDave Kleikamp return 0; 4616ac27a0ecSDave Kleikamp 4617617ba13bSMingming Cao cantfind_ext4: 4618ac27a0ecSDave Kleikamp if (!silent) 4619b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem"); 4620ac27a0ecSDave Kleikamp goto failed_mount; 4621ac27a0ecSDave Kleikamp 462272ba7450STheodore Ts'o #ifdef CONFIG_QUOTA 462372ba7450STheodore Ts'o failed_mount8: 4624ebd173beSTheodore Ts'o ext4_unregister_sysfs(sb); 462572ba7450STheodore Ts'o #endif 4626dcf2d804STao Ma failed_mount7: 4627dcf2d804STao Ma ext4_unregister_li_request(sb); 4628dcf2d804STao Ma failed_mount6: 4629f9ae9cf5STheodore Ts'o ext4_mb_release(sb); 4630d5e03cbbSTheodore Ts'o if (sbi->s_flex_groups) 4631b93b41d4SAl Viro kvfree(sbi->s_flex_groups); 4632d5e03cbbSTheodore Ts'o percpu_counter_destroy(&sbi->s_freeclusters_counter); 4633d5e03cbbSTheodore Ts'o percpu_counter_destroy(&sbi->s_freeinodes_counter); 4634d5e03cbbSTheodore Ts'o percpu_counter_destroy(&sbi->s_dirs_counter); 4635d5e03cbbSTheodore Ts'o percpu_counter_destroy(&sbi->s_dirtyclusters_counter); 4636af18e35bSVasily Averin percpu_free_rwsem(&sbi->s_journal_flag_rwsem); 463700764937SAzat Khuzhin failed_mount5: 4638f9ae9cf5STheodore Ts'o ext4_ext_release(sb); 4639f9ae9cf5STheodore Ts'o ext4_release_system_zone(sb); 4640f9ae9cf5STheodore Ts'o failed_mount4a: 464194bf608aSAl Viro dput(sb->s_root); 464232a9bb57SManish Katiyar sb->s_root = NULL; 464394bf608aSAl Viro failed_mount4: 4644b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "mount failed"); 46452e8fa54eSJan Kara if (EXT4_SB(sb)->rsv_conversion_wq) 46462e8fa54eSJan Kara destroy_workqueue(EXT4_SB(sb)->rsv_conversion_wq); 46474c0425ffSMingming Cao failed_mount_wq: 4648dec214d0STahsin Erdogan ext4_xattr_destroy_cache(sbi->s_ea_inode_cache); 4649dec214d0STahsin Erdogan sbi->s_ea_inode_cache = NULL; 465050c15df6SChengguang Xu 465147387409STahsin Erdogan ext4_xattr_destroy_cache(sbi->s_ea_block_cache); 465247387409STahsin Erdogan sbi->s_ea_block_cache = NULL; 465350c15df6SChengguang Xu 46540390131bSFrank Mayhar if (sbi->s_journal) { 4655dab291afSMingming Cao jbd2_journal_destroy(sbi->s_journal); 465647b4a50bSJan Kara sbi->s_journal = NULL; 46570390131bSFrank Mayhar } 465850460fe8SDarrick J. Wong failed_mount3a: 4659d3922a77SZheng Liu ext4_es_unregister_shrinker(sbi); 4660eb68d0e2SZheng Liu failed_mount3: 46619105bb14SAl Viro del_timer_sync(&sbi->s_err_report); 4662c5e06d10SJohann Lombardi if (sbi->s_mmp_tsk) 4663c5e06d10SJohann Lombardi kthread_stop(sbi->s_mmp_tsk); 4664ac27a0ecSDave Kleikamp failed_mount2: 4665ac27a0ecSDave Kleikamp for (i = 0; i < db_count; i++) 4666ac27a0ecSDave Kleikamp brelse(sbi->s_group_desc[i]); 4667b93b41d4SAl Viro kvfree(sbi->s_group_desc); 4668ac27a0ecSDave Kleikamp failed_mount: 46690441984aSDarrick J. Wong if (sbi->s_chksum_driver) 46700441984aSDarrick J. Wong crypto_free_shash(sbi->s_chksum_driver); 4671c83ad55eSGabriel Krisman Bertazi 4672c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE 4673c83ad55eSGabriel Krisman Bertazi utf8_unload(sbi->s_encoding); 4674c83ad55eSGabriel Krisman Bertazi #endif 4675c83ad55eSGabriel Krisman Bertazi 4676ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 4677a2d4a646SJan Kara for (i = 0; i < EXT4_MAXQUOTAS; i++) 46780ba33facSTheodore Ts'o kfree(get_qf_name(sb, sbi, i)); 4679ac27a0ecSDave Kleikamp #endif 4680617ba13bSMingming Cao ext4_blkdev_remove(sbi); 4681ac27a0ecSDave Kleikamp brelse(bh); 4682ac27a0ecSDave Kleikamp out_fail: 4683ac27a0ecSDave Kleikamp sb->s_fs_info = NULL; 4684f6830165SManish Katiyar kfree(sbi->s_blockgroup_lock); 46855aee0f8aSTheodore Ts'o out_free_base: 4686ac27a0ecSDave Kleikamp kfree(sbi); 4687d4c402d9SCurt Wohlgemuth kfree(orig_data); 46885e405595SDan Williams fs_put_dax(dax_dev); 468907aa2ea1SLukas Czerner return err ? err : ret; 4690ac27a0ecSDave Kleikamp } 4691ac27a0ecSDave Kleikamp 4692ac27a0ecSDave Kleikamp /* 4693ac27a0ecSDave Kleikamp * Setup any per-fs journal parameters now. We'll do this both on 4694ac27a0ecSDave Kleikamp * initial mount, once the journal has been initialised but before we've 4695ac27a0ecSDave Kleikamp * done any recovery; and again on any subsequent remount. 4696ac27a0ecSDave Kleikamp */ 4697617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal) 4698ac27a0ecSDave Kleikamp { 4699617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 4700ac27a0ecSDave Kleikamp 4701ac27a0ecSDave Kleikamp journal->j_commit_interval = sbi->s_commit_interval; 470230773840STheodore Ts'o journal->j_min_batch_time = sbi->s_min_batch_time; 470330773840STheodore Ts'o journal->j_max_batch_time = sbi->s_max_batch_time; 4704ac27a0ecSDave Kleikamp 4705a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 4706ac27a0ecSDave Kleikamp if (test_opt(sb, BARRIER)) 4707dab291afSMingming Cao journal->j_flags |= JBD2_BARRIER; 4708ac27a0ecSDave Kleikamp else 4709dab291afSMingming Cao journal->j_flags &= ~JBD2_BARRIER; 47105bf5683aSHidehiro Kawai if (test_opt(sb, DATA_ERR_ABORT)) 47115bf5683aSHidehiro Kawai journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR; 47125bf5683aSHidehiro Kawai else 47135bf5683aSHidehiro Kawai journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR; 4714a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 4715ac27a0ecSDave Kleikamp } 4716ac27a0ecSDave Kleikamp 4717c6cb7e77SEric Whitney static struct inode *ext4_get_journal_inode(struct super_block *sb, 4718ac27a0ecSDave Kleikamp unsigned int journal_inum) 4719ac27a0ecSDave Kleikamp { 4720ac27a0ecSDave Kleikamp struct inode *journal_inode; 4721ac27a0ecSDave Kleikamp 4722c6cb7e77SEric Whitney /* 4723c6cb7e77SEric Whitney * Test for the existence of a valid inode on disk. Bad things 4724c6cb7e77SEric Whitney * happen if we iget() an unused inode, as the subsequent iput() 4725c6cb7e77SEric Whitney * will try to delete it. 4726c6cb7e77SEric Whitney */ 47278a363970STheodore Ts'o journal_inode = ext4_iget(sb, journal_inum, EXT4_IGET_SPECIAL); 47281d1fe1eeSDavid Howells if (IS_ERR(journal_inode)) { 4729b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "no journal found"); 4730ac27a0ecSDave Kleikamp return NULL; 4731ac27a0ecSDave Kleikamp } 4732ac27a0ecSDave Kleikamp if (!journal_inode->i_nlink) { 4733ac27a0ecSDave Kleikamp make_bad_inode(journal_inode); 4734ac27a0ecSDave Kleikamp iput(journal_inode); 4735b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "journal inode is deleted"); 4736ac27a0ecSDave Kleikamp return NULL; 4737ac27a0ecSDave Kleikamp } 4738ac27a0ecSDave Kleikamp 4739e5f8eab8STheodore Ts'o jbd_debug(2, "Journal inode found at %p: %lld bytes\n", 4740ac27a0ecSDave Kleikamp journal_inode, journal_inode->i_size); 47411d1fe1eeSDavid Howells if (!S_ISREG(journal_inode->i_mode)) { 4742b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "invalid journal inode"); 4743ac27a0ecSDave Kleikamp iput(journal_inode); 4744ac27a0ecSDave Kleikamp return NULL; 4745ac27a0ecSDave Kleikamp } 4746c6cb7e77SEric Whitney return journal_inode; 4747c6cb7e77SEric Whitney } 4748c6cb7e77SEric Whitney 4749c6cb7e77SEric Whitney static journal_t *ext4_get_journal(struct super_block *sb, 4750c6cb7e77SEric Whitney unsigned int journal_inum) 4751c6cb7e77SEric Whitney { 4752c6cb7e77SEric Whitney struct inode *journal_inode; 4753c6cb7e77SEric Whitney journal_t *journal; 4754c6cb7e77SEric Whitney 4755c6cb7e77SEric Whitney BUG_ON(!ext4_has_feature_journal(sb)); 4756c6cb7e77SEric Whitney 4757c6cb7e77SEric Whitney journal_inode = ext4_get_journal_inode(sb, journal_inum); 4758c6cb7e77SEric Whitney if (!journal_inode) 4759c6cb7e77SEric Whitney return NULL; 4760ac27a0ecSDave Kleikamp 4761dab291afSMingming Cao journal = jbd2_journal_init_inode(journal_inode); 4762ac27a0ecSDave Kleikamp if (!journal) { 4763b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "Could not load journal inode"); 4764ac27a0ecSDave Kleikamp iput(journal_inode); 4765ac27a0ecSDave Kleikamp return NULL; 4766ac27a0ecSDave Kleikamp } 4767ac27a0ecSDave Kleikamp journal->j_private = sb; 4768617ba13bSMingming Cao ext4_init_journal_params(sb, journal); 4769ac27a0ecSDave Kleikamp return journal; 4770ac27a0ecSDave Kleikamp } 4771ac27a0ecSDave Kleikamp 4772617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb, 4773ac27a0ecSDave Kleikamp dev_t j_dev) 4774ac27a0ecSDave Kleikamp { 4775ac27a0ecSDave Kleikamp struct buffer_head *bh; 4776ac27a0ecSDave Kleikamp journal_t *journal; 4777617ba13bSMingming Cao ext4_fsblk_t start; 4778617ba13bSMingming Cao ext4_fsblk_t len; 4779ac27a0ecSDave Kleikamp int hblock, blocksize; 4780617ba13bSMingming Cao ext4_fsblk_t sb_block; 4781ac27a0ecSDave Kleikamp unsigned long offset; 4782617ba13bSMingming Cao struct ext4_super_block *es; 4783ac27a0ecSDave Kleikamp struct block_device *bdev; 4784ac27a0ecSDave Kleikamp 4785e2b911c5SDarrick J. Wong BUG_ON(!ext4_has_feature_journal(sb)); 47860390131bSFrank Mayhar 4787b31e1552SEric Sandeen bdev = ext4_blkdev_get(j_dev, sb); 4788ac27a0ecSDave Kleikamp if (bdev == NULL) 4789ac27a0ecSDave Kleikamp return NULL; 4790ac27a0ecSDave Kleikamp 4791ac27a0ecSDave Kleikamp blocksize = sb->s_blocksize; 4792e1defc4fSMartin K. Petersen hblock = bdev_logical_block_size(bdev); 4793ac27a0ecSDave Kleikamp if (blocksize < hblock) { 4794b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, 4795b31e1552SEric Sandeen "blocksize too small for journal device"); 4796ac27a0ecSDave Kleikamp goto out_bdev; 4797ac27a0ecSDave Kleikamp } 4798ac27a0ecSDave Kleikamp 4799617ba13bSMingming Cao sb_block = EXT4_MIN_BLOCK_SIZE / blocksize; 4800617ba13bSMingming Cao offset = EXT4_MIN_BLOCK_SIZE % blocksize; 4801ac27a0ecSDave Kleikamp set_blocksize(bdev, blocksize); 4802ac27a0ecSDave Kleikamp if (!(bh = __bread(bdev, sb_block, blocksize))) { 4803b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "couldn't read superblock of " 4804b31e1552SEric Sandeen "external journal"); 4805ac27a0ecSDave Kleikamp goto out_bdev; 4806ac27a0ecSDave Kleikamp } 4807ac27a0ecSDave Kleikamp 48082716b802STheodore Ts'o es = (struct ext4_super_block *) (bh->b_data + offset); 4809617ba13bSMingming Cao if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) || 4810ac27a0ecSDave Kleikamp !(le32_to_cpu(es->s_feature_incompat) & 4811617ba13bSMingming Cao EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) { 4812b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "external journal has " 4813b31e1552SEric Sandeen "bad superblock"); 4814ac27a0ecSDave Kleikamp brelse(bh); 4815ac27a0ecSDave Kleikamp goto out_bdev; 4816ac27a0ecSDave Kleikamp } 4817ac27a0ecSDave Kleikamp 4818df4763beSDarrick J. Wong if ((le32_to_cpu(es->s_feature_ro_compat) & 4819df4763beSDarrick J. Wong EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) && 4820df4763beSDarrick J. Wong es->s_checksum != ext4_superblock_csum(sb, es)) { 4821df4763beSDarrick J. Wong ext4_msg(sb, KERN_ERR, "external journal has " 4822df4763beSDarrick J. Wong "corrupt superblock"); 4823df4763beSDarrick J. Wong brelse(bh); 4824df4763beSDarrick J. Wong goto out_bdev; 4825df4763beSDarrick J. Wong } 4826df4763beSDarrick J. Wong 4827617ba13bSMingming Cao if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) { 4828b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "journal UUID does not match"); 4829ac27a0ecSDave Kleikamp brelse(bh); 4830ac27a0ecSDave Kleikamp goto out_bdev; 4831ac27a0ecSDave Kleikamp } 4832ac27a0ecSDave Kleikamp 4833bd81d8eeSLaurent Vivier len = ext4_blocks_count(es); 4834ac27a0ecSDave Kleikamp start = sb_block + 1; 4835ac27a0ecSDave Kleikamp brelse(bh); /* we're done with the superblock */ 4836ac27a0ecSDave Kleikamp 4837dab291afSMingming Cao journal = jbd2_journal_init_dev(bdev, sb->s_bdev, 4838ac27a0ecSDave Kleikamp start, len, blocksize); 4839ac27a0ecSDave Kleikamp if (!journal) { 4840b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "failed to create device journal"); 4841ac27a0ecSDave Kleikamp goto out_bdev; 4842ac27a0ecSDave Kleikamp } 4843ac27a0ecSDave Kleikamp journal->j_private = sb; 4844dfec8a14SMike Christie ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1, &journal->j_sb_buffer); 4845ac27a0ecSDave Kleikamp wait_on_buffer(journal->j_sb_buffer); 4846ac27a0ecSDave Kleikamp if (!buffer_uptodate(journal->j_sb_buffer)) { 4847b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "I/O error on journal device"); 4848ac27a0ecSDave Kleikamp goto out_journal; 4849ac27a0ecSDave Kleikamp } 4850ac27a0ecSDave Kleikamp if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) { 4851b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "External journal has more than one " 4852b31e1552SEric Sandeen "user (unsupported) - %d", 4853ac27a0ecSDave Kleikamp be32_to_cpu(journal->j_superblock->s_nr_users)); 4854ac27a0ecSDave Kleikamp goto out_journal; 4855ac27a0ecSDave Kleikamp } 4856617ba13bSMingming Cao EXT4_SB(sb)->journal_bdev = bdev; 4857617ba13bSMingming Cao ext4_init_journal_params(sb, journal); 4858ac27a0ecSDave Kleikamp return journal; 48590b8e58a1SAndreas Dilger 4860ac27a0ecSDave Kleikamp out_journal: 4861dab291afSMingming Cao jbd2_journal_destroy(journal); 4862ac27a0ecSDave Kleikamp out_bdev: 4863617ba13bSMingming Cao ext4_blkdev_put(bdev); 4864ac27a0ecSDave Kleikamp return NULL; 4865ac27a0ecSDave Kleikamp } 4866ac27a0ecSDave Kleikamp 4867617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb, 4868617ba13bSMingming Cao struct ext4_super_block *es, 4869ac27a0ecSDave Kleikamp unsigned long journal_devnum) 4870ac27a0ecSDave Kleikamp { 4871ac27a0ecSDave Kleikamp journal_t *journal; 4872ac27a0ecSDave Kleikamp unsigned int journal_inum = le32_to_cpu(es->s_journal_inum); 4873ac27a0ecSDave Kleikamp dev_t journal_dev; 4874ac27a0ecSDave Kleikamp int err = 0; 4875ac27a0ecSDave Kleikamp int really_read_only; 4876ac27a0ecSDave Kleikamp 4877e2b911c5SDarrick J. Wong BUG_ON(!ext4_has_feature_journal(sb)); 48780390131bSFrank Mayhar 4879ac27a0ecSDave Kleikamp if (journal_devnum && 4880ac27a0ecSDave Kleikamp journal_devnum != le32_to_cpu(es->s_journal_dev)) { 4881b31e1552SEric Sandeen ext4_msg(sb, KERN_INFO, "external journal device major/minor " 4882b31e1552SEric Sandeen "numbers have changed"); 4883ac27a0ecSDave Kleikamp journal_dev = new_decode_dev(journal_devnum); 4884ac27a0ecSDave Kleikamp } else 4885ac27a0ecSDave Kleikamp journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev)); 4886ac27a0ecSDave Kleikamp 4887ac27a0ecSDave Kleikamp really_read_only = bdev_read_only(sb->s_bdev); 4888ac27a0ecSDave Kleikamp 4889ac27a0ecSDave Kleikamp /* 4890ac27a0ecSDave Kleikamp * Are we loading a blank journal or performing recovery after a 4891ac27a0ecSDave Kleikamp * crash? For recovery, we need to check in advance whether we 4892ac27a0ecSDave Kleikamp * can get read-write access to the device. 4893ac27a0ecSDave Kleikamp */ 4894e2b911c5SDarrick J. Wong if (ext4_has_feature_journal_needs_recovery(sb)) { 4895bc98a42cSDavid Howells if (sb_rdonly(sb)) { 4896b31e1552SEric Sandeen ext4_msg(sb, KERN_INFO, "INFO: recovery " 4897b31e1552SEric Sandeen "required on readonly filesystem"); 4898ac27a0ecSDave Kleikamp if (really_read_only) { 4899b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "write access " 4900d98bf8cdSSimon Ruderich "unavailable, cannot proceed " 4901d98bf8cdSSimon Ruderich "(try mounting with noload)"); 4902ac27a0ecSDave Kleikamp return -EROFS; 4903ac27a0ecSDave Kleikamp } 4904b31e1552SEric Sandeen ext4_msg(sb, KERN_INFO, "write access will " 4905b31e1552SEric Sandeen "be enabled during recovery"); 4906ac27a0ecSDave Kleikamp } 4907ac27a0ecSDave Kleikamp } 4908ac27a0ecSDave Kleikamp 4909ac27a0ecSDave Kleikamp if (journal_inum && journal_dev) { 4910b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "filesystem has both journal " 4911b31e1552SEric Sandeen "and inode journals!"); 4912ac27a0ecSDave Kleikamp return -EINVAL; 4913ac27a0ecSDave Kleikamp } 4914ac27a0ecSDave Kleikamp 4915ac27a0ecSDave Kleikamp if (journal_inum) { 4916617ba13bSMingming Cao if (!(journal = ext4_get_journal(sb, journal_inum))) 4917ac27a0ecSDave Kleikamp return -EINVAL; 4918ac27a0ecSDave Kleikamp } else { 4919617ba13bSMingming Cao if (!(journal = ext4_get_dev_journal(sb, journal_dev))) 4920ac27a0ecSDave Kleikamp return -EINVAL; 4921ac27a0ecSDave Kleikamp } 4922ac27a0ecSDave Kleikamp 492390576c0bSTheodore Ts'o if (!(journal->j_flags & JBD2_BARRIER)) 4924b31e1552SEric Sandeen ext4_msg(sb, KERN_INFO, "barriers disabled"); 49254776004fSTheodore Ts'o 4926e2b911c5SDarrick J. Wong if (!ext4_has_feature_journal_needs_recovery(sb)) 4927dab291afSMingming Cao err = jbd2_journal_wipe(journal, !really_read_only); 49281c13d5c0STheodore Ts'o if (!err) { 49291c13d5c0STheodore Ts'o char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL); 49301c13d5c0STheodore Ts'o if (save) 49311c13d5c0STheodore Ts'o memcpy(save, ((char *) es) + 49321c13d5c0STheodore Ts'o EXT4_S_ERR_START, EXT4_S_ERR_LEN); 4933dab291afSMingming Cao err = jbd2_journal_load(journal); 49341c13d5c0STheodore Ts'o if (save) 49351c13d5c0STheodore Ts'o memcpy(((char *) es) + EXT4_S_ERR_START, 49361c13d5c0STheodore Ts'o save, EXT4_S_ERR_LEN); 49371c13d5c0STheodore Ts'o kfree(save); 49381c13d5c0STheodore Ts'o } 4939ac27a0ecSDave Kleikamp 4940ac27a0ecSDave Kleikamp if (err) { 4941b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "error loading journal"); 4942dab291afSMingming Cao jbd2_journal_destroy(journal); 4943ac27a0ecSDave Kleikamp return err; 4944ac27a0ecSDave Kleikamp } 4945ac27a0ecSDave Kleikamp 4946617ba13bSMingming Cao EXT4_SB(sb)->s_journal = journal; 4947617ba13bSMingming Cao ext4_clear_journal_err(sb, es); 4948ac27a0ecSDave Kleikamp 4949c41303ceSMaciej Żenczykowski if (!really_read_only && journal_devnum && 4950ac27a0ecSDave Kleikamp journal_devnum != le32_to_cpu(es->s_journal_dev)) { 4951ac27a0ecSDave Kleikamp es->s_journal_dev = cpu_to_le32(journal_devnum); 4952ac27a0ecSDave Kleikamp 4953ac27a0ecSDave Kleikamp /* Make sure we flush the recovery flag to disk. */ 4954e2d67052STheodore Ts'o ext4_commit_super(sb, 1); 4955ac27a0ecSDave Kleikamp } 4956ac27a0ecSDave Kleikamp 4957ac27a0ecSDave Kleikamp return 0; 4958ac27a0ecSDave Kleikamp } 4959ac27a0ecSDave Kleikamp 4960e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync) 4961ac27a0ecSDave Kleikamp { 4962e2d67052STheodore Ts'o struct ext4_super_block *es = EXT4_SB(sb)->s_es; 4963617ba13bSMingming Cao struct buffer_head *sbh = EXT4_SB(sb)->s_sbh; 4964c4be0c1dSTakashi Sato int error = 0; 4965ac27a0ecSDave Kleikamp 4966bdfe0cbdSTheodore Ts'o if (!sbh || block_device_ejected(sb)) 4967c4be0c1dSTakashi Sato return error; 4968a17712c8SJon Derrick 4969a17712c8SJon Derrick /* 4970a17712c8SJon Derrick * The superblock bh should be mapped, but it might not be if the 4971a17712c8SJon Derrick * device was hot-removed. Not much we can do but fail the I/O. 4972a17712c8SJon Derrick */ 4973a17712c8SJon Derrick if (!buffer_mapped(sbh)) 4974a17712c8SJon Derrick return error; 4975a17712c8SJon Derrick 497671290b36STheodore Ts'o /* 497771290b36STheodore Ts'o * If the file system is mounted read-only, don't update the 497871290b36STheodore Ts'o * superblock write time. This avoids updating the superblock 497971290b36STheodore Ts'o * write time when we are mounting the root file system 498071290b36STheodore Ts'o * read/only but we need to replay the journal; at that point, 498171290b36STheodore Ts'o * for people who are east of GMT and who make their clock 498271290b36STheodore Ts'o * tick in localtime for Windows bug-for-bug compatibility, 498371290b36STheodore Ts'o * the clock is set in the future, and this will cause e2fsck 498471290b36STheodore Ts'o * to complain and force a full file system check. 498571290b36STheodore Ts'o */ 49861751e8a6SLinus Torvalds if (!(sb->s_flags & SB_RDONLY)) 49876a0678a7SArnd Bergmann ext4_update_tstamp(es, s_wtime); 4988f613dfcbSTheodore Ts'o if (sb->s_bdev->bd_part) 4989afc32f7eSTheodore Ts'o es->s_kbytes_written = 4990afc32f7eSTheodore Ts'o cpu_to_le64(EXT4_SB(sb)->s_kbytes_written + 4991dbae2c55SMichael Callahan ((part_stat_read(sb->s_bdev->bd_part, 4992dbae2c55SMichael Callahan sectors[STAT_WRITE]) - 4993afc32f7eSTheodore Ts'o EXT4_SB(sb)->s_sectors_written_start) >> 1)); 4994f613dfcbSTheodore Ts'o else 4995f613dfcbSTheodore Ts'o es->s_kbytes_written = 4996f613dfcbSTheodore Ts'o cpu_to_le64(EXT4_SB(sb)->s_kbytes_written); 4997d5e03cbbSTheodore Ts'o if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeclusters_counter)) 499857042651STheodore Ts'o ext4_free_blocks_count_set(es, 499957042651STheodore Ts'o EXT4_C2B(EXT4_SB(sb), percpu_counter_sum_positive( 500057042651STheodore Ts'o &EXT4_SB(sb)->s_freeclusters_counter))); 5001d5e03cbbSTheodore Ts'o if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeinodes_counter)) 50027f93cff9STheodore Ts'o es->s_free_inodes_count = 50037f93cff9STheodore Ts'o cpu_to_le32(percpu_counter_sum_positive( 50045d1b1b3fSAneesh Kumar K.V &EXT4_SB(sb)->s_freeinodes_counter)); 5005ac27a0ecSDave Kleikamp BUFFER_TRACE(sbh, "marking dirty"); 500606db49e6STheodore Ts'o ext4_superblock_csum_set(sb); 50071566a48aSTheodore Ts'o if (sync) 50084743f839SPranay Kr. Srivastava lock_buffer(sbh); 5009e8680786STheodore Ts'o if (buffer_write_io_error(sbh) || !buffer_uptodate(sbh)) { 50104743f839SPranay Kr. Srivastava /* 50114743f839SPranay Kr. Srivastava * Oh, dear. A previous attempt to write the 50124743f839SPranay Kr. Srivastava * superblock failed. This could happen because the 50134743f839SPranay Kr. Srivastava * USB device was yanked out. Or it could happen to 50144743f839SPranay Kr. Srivastava * be a transient write error and maybe the block will 50154743f839SPranay Kr. Srivastava * be remapped. Nothing we can do but to retry the 50164743f839SPranay Kr. Srivastava * write and hope for the best. 50174743f839SPranay Kr. Srivastava */ 50184743f839SPranay Kr. Srivastava ext4_msg(sb, KERN_ERR, "previous I/O error to " 50194743f839SPranay Kr. Srivastava "superblock detected"); 50204743f839SPranay Kr. Srivastava clear_buffer_write_io_error(sbh); 50214743f839SPranay Kr. Srivastava set_buffer_uptodate(sbh); 50224743f839SPranay Kr. Srivastava } 5023ac27a0ecSDave Kleikamp mark_buffer_dirty(sbh); 5024914258bfSTheodore Ts'o if (sync) { 50251566a48aSTheodore Ts'o unlock_buffer(sbh); 5026564bc402SDaeho Jeong error = __sync_dirty_buffer(sbh, 502700473374SJan Kara REQ_SYNC | (test_opt(sb, BARRIER) ? REQ_FUA : 0)); 5028c89128a0SJaegeuk Kim if (buffer_write_io_error(sbh)) { 5029b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "I/O error while writing " 5030b31e1552SEric Sandeen "superblock"); 5031914258bfSTheodore Ts'o clear_buffer_write_io_error(sbh); 5032914258bfSTheodore Ts'o set_buffer_uptodate(sbh); 5033914258bfSTheodore Ts'o } 5034914258bfSTheodore Ts'o } 5035c4be0c1dSTakashi Sato return error; 5036ac27a0ecSDave Kleikamp } 5037ac27a0ecSDave Kleikamp 5038ac27a0ecSDave Kleikamp /* 5039ac27a0ecSDave Kleikamp * Have we just finished recovery? If so, and if we are mounting (or 5040ac27a0ecSDave Kleikamp * remounting) the filesystem readonly, then we will end up with a 5041ac27a0ecSDave Kleikamp * consistent fs on disk. Record that fact. 5042ac27a0ecSDave Kleikamp */ 5043617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb, 5044617ba13bSMingming Cao struct ext4_super_block *es) 5045ac27a0ecSDave Kleikamp { 5046617ba13bSMingming Cao journal_t *journal = EXT4_SB(sb)->s_journal; 5047ac27a0ecSDave Kleikamp 5048e2b911c5SDarrick J. Wong if (!ext4_has_feature_journal(sb)) { 50490390131bSFrank Mayhar BUG_ON(journal != NULL); 50500390131bSFrank Mayhar return; 50510390131bSFrank Mayhar } 5052dab291afSMingming Cao jbd2_journal_lock_updates(journal); 50537ffe1ea8SHidehiro Kawai if (jbd2_journal_flush(journal) < 0) 50547ffe1ea8SHidehiro Kawai goto out; 50557ffe1ea8SHidehiro Kawai 5056bc98a42cSDavid Howells if (ext4_has_feature_journal_needs_recovery(sb) && sb_rdonly(sb)) { 5057e2b911c5SDarrick J. Wong ext4_clear_feature_journal_needs_recovery(sb); 5058e2d67052STheodore Ts'o ext4_commit_super(sb, 1); 5059ac27a0ecSDave Kleikamp } 50607ffe1ea8SHidehiro Kawai 50617ffe1ea8SHidehiro Kawai out: 5062dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 5063ac27a0ecSDave Kleikamp } 5064ac27a0ecSDave Kleikamp 5065ac27a0ecSDave Kleikamp /* 5066ac27a0ecSDave Kleikamp * If we are mounting (or read-write remounting) a filesystem whose journal 5067ac27a0ecSDave Kleikamp * has recorded an error from a previous lifetime, move that error to the 5068ac27a0ecSDave Kleikamp * main filesystem now. 5069ac27a0ecSDave Kleikamp */ 5070617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb, 5071617ba13bSMingming Cao struct ext4_super_block *es) 5072ac27a0ecSDave Kleikamp { 5073ac27a0ecSDave Kleikamp journal_t *journal; 5074ac27a0ecSDave Kleikamp int j_errno; 5075ac27a0ecSDave Kleikamp const char *errstr; 5076ac27a0ecSDave Kleikamp 5077e2b911c5SDarrick J. Wong BUG_ON(!ext4_has_feature_journal(sb)); 50780390131bSFrank Mayhar 5079617ba13bSMingming Cao journal = EXT4_SB(sb)->s_journal; 5080ac27a0ecSDave Kleikamp 5081ac27a0ecSDave Kleikamp /* 5082ac27a0ecSDave Kleikamp * Now check for any error status which may have been recorded in the 5083617ba13bSMingming Cao * journal by a prior ext4_error() or ext4_abort() 5084ac27a0ecSDave Kleikamp */ 5085ac27a0ecSDave Kleikamp 5086dab291afSMingming Cao j_errno = jbd2_journal_errno(journal); 5087ac27a0ecSDave Kleikamp if (j_errno) { 5088ac27a0ecSDave Kleikamp char nbuf[16]; 5089ac27a0ecSDave Kleikamp 5090617ba13bSMingming Cao errstr = ext4_decode_error(sb, j_errno, nbuf); 509112062dddSEric Sandeen ext4_warning(sb, "Filesystem error recorded " 5092ac27a0ecSDave Kleikamp "from previous mount: %s", errstr); 509312062dddSEric Sandeen ext4_warning(sb, "Marking fs in need of filesystem check."); 5094ac27a0ecSDave Kleikamp 5095617ba13bSMingming Cao EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS; 5096617ba13bSMingming Cao es->s_state |= cpu_to_le16(EXT4_ERROR_FS); 5097e2d67052STheodore Ts'o ext4_commit_super(sb, 1); 5098ac27a0ecSDave Kleikamp 5099dab291afSMingming Cao jbd2_journal_clear_err(journal); 5100d796c52eSTheodore Ts'o jbd2_journal_update_sb_errno(journal); 5101ac27a0ecSDave Kleikamp } 5102ac27a0ecSDave Kleikamp } 5103ac27a0ecSDave Kleikamp 5104ac27a0ecSDave Kleikamp /* 5105ac27a0ecSDave Kleikamp * Force the running and committing transactions to commit, 5106ac27a0ecSDave Kleikamp * and wait on the commit. 5107ac27a0ecSDave Kleikamp */ 5108617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb) 5109ac27a0ecSDave Kleikamp { 5110ac27a0ecSDave Kleikamp journal_t *journal; 5111ac27a0ecSDave Kleikamp 5112bc98a42cSDavid Howells if (sb_rdonly(sb)) 5113ac27a0ecSDave Kleikamp return 0; 5114ac27a0ecSDave Kleikamp 5115617ba13bSMingming Cao journal = EXT4_SB(sb)->s_journal; 5116b1deefc9SGuo Chao return ext4_journal_force_commit(journal); 5117ac27a0ecSDave Kleikamp } 5118ac27a0ecSDave Kleikamp 5119617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait) 5120ac27a0ecSDave Kleikamp { 512114ce0cb4STheodore Ts'o int ret = 0; 51229eddacf9SJan Kara tid_t target; 512306a407f1SDmitry Monakhov bool needs_barrier = false; 51248d5d02e6SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 5125ac27a0ecSDave Kleikamp 512649598e04SJun Piao if (unlikely(ext4_forced_shutdown(sbi))) 51270db1ff22STheodore Ts'o return 0; 51280db1ff22STheodore Ts'o 51299bffad1eSTheodore Ts'o trace_ext4_sync_fs(sb, wait); 51302e8fa54eSJan Kara flush_workqueue(sbi->rsv_conversion_wq); 5131a1177825SJan Kara /* 5132a1177825SJan Kara * Writeback quota in non-journalled quota case - journalled quota has 5133a1177825SJan Kara * no dirty dquots 5134a1177825SJan Kara */ 5135a1177825SJan Kara dquot_writeback_dquots(sb, -1); 513606a407f1SDmitry Monakhov /* 513706a407f1SDmitry Monakhov * Data writeback is possible w/o journal transaction, so barrier must 513806a407f1SDmitry Monakhov * being sent at the end of the function. But we can skip it if 513906a407f1SDmitry Monakhov * transaction_commit will do it for us. 514006a407f1SDmitry Monakhov */ 5141bda32530STheodore Ts'o if (sbi->s_journal) { 514206a407f1SDmitry Monakhov target = jbd2_get_latest_transaction(sbi->s_journal); 514306a407f1SDmitry Monakhov if (wait && sbi->s_journal->j_flags & JBD2_BARRIER && 514406a407f1SDmitry Monakhov !jbd2_trans_will_send_data_barrier(sbi->s_journal, target)) 514506a407f1SDmitry Monakhov needs_barrier = true; 514606a407f1SDmitry Monakhov 51478d5d02e6SMingming Cao if (jbd2_journal_start_commit(sbi->s_journal, &target)) { 5148ac27a0ecSDave Kleikamp if (wait) 5149bda32530STheodore Ts'o ret = jbd2_log_wait_commit(sbi->s_journal, 5150bda32530STheodore Ts'o target); 51510390131bSFrank Mayhar } 5152bda32530STheodore Ts'o } else if (wait && test_opt(sb, BARRIER)) 5153bda32530STheodore Ts'o needs_barrier = true; 515406a407f1SDmitry Monakhov if (needs_barrier) { 515506a407f1SDmitry Monakhov int err; 515606a407f1SDmitry Monakhov err = blkdev_issue_flush(sb->s_bdev, GFP_KERNEL, NULL); 515706a407f1SDmitry Monakhov if (!ret) 515806a407f1SDmitry Monakhov ret = err; 515906a407f1SDmitry Monakhov } 516006a407f1SDmitry Monakhov 516106a407f1SDmitry Monakhov return ret; 516206a407f1SDmitry Monakhov } 516306a407f1SDmitry Monakhov 5164ac27a0ecSDave Kleikamp /* 5165ac27a0ecSDave Kleikamp * LVM calls this function before a (read-only) snapshot is created. This 5166ac27a0ecSDave Kleikamp * gives us a chance to flush the journal completely and mark the fs clean. 5167be4f27d3SYongqiang Yang * 5168be4f27d3SYongqiang Yang * Note that only this function cannot bring a filesystem to be in a clean 51698e8ad8a5SJan Kara * state independently. It relies on upper layer to stop all data & metadata 51708e8ad8a5SJan Kara * modifications. 5171ac27a0ecSDave Kleikamp */ 5172c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb) 5173ac27a0ecSDave Kleikamp { 5174c4be0c1dSTakashi Sato int error = 0; 5175c4be0c1dSTakashi Sato journal_t *journal; 5176ac27a0ecSDave Kleikamp 5177bc98a42cSDavid Howells if (sb_rdonly(sb)) 51789ca92389STheodore Ts'o return 0; 51799ca92389STheodore Ts'o 5180c4be0c1dSTakashi Sato journal = EXT4_SB(sb)->s_journal; 5181ac27a0ecSDave Kleikamp 5182bb044576STheodore Ts'o if (journal) { 5183ac27a0ecSDave Kleikamp /* Now we set up the journal barrier. */ 5184dab291afSMingming Cao jbd2_journal_lock_updates(journal); 51857ffe1ea8SHidehiro Kawai 51867ffe1ea8SHidehiro Kawai /* 5187bb044576STheodore Ts'o * Don't clear the needs_recovery flag if we failed to 5188bb044576STheodore Ts'o * flush the journal. 51897ffe1ea8SHidehiro Kawai */ 5190c4be0c1dSTakashi Sato error = jbd2_journal_flush(journal); 51916b0310fbSEric Sandeen if (error < 0) 51926b0310fbSEric Sandeen goto out; 5193ac27a0ecSDave Kleikamp 5194ac27a0ecSDave Kleikamp /* Journal blocked and flushed, clear needs_recovery flag. */ 5195e2b911c5SDarrick J. Wong ext4_clear_feature_journal_needs_recovery(sb); 5196c642dc9eSEric Sandeen } 5197c642dc9eSEric Sandeen 5198e2d67052STheodore Ts'o error = ext4_commit_super(sb, 1); 51996b0310fbSEric Sandeen out: 5200bb044576STheodore Ts'o if (journal) 52018e8ad8a5SJan Kara /* we rely on upper layer to stop further updates */ 5202bb044576STheodore Ts'o jbd2_journal_unlock_updates(journal); 52036b0310fbSEric Sandeen return error; 5204ac27a0ecSDave Kleikamp } 5205ac27a0ecSDave Kleikamp 5206ac27a0ecSDave Kleikamp /* 5207ac27a0ecSDave Kleikamp * Called by LVM after the snapshot is done. We need to reset the RECOVER 5208ac27a0ecSDave Kleikamp * flag here, even though the filesystem is not technically dirty yet. 5209ac27a0ecSDave Kleikamp */ 5210c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb) 5211ac27a0ecSDave Kleikamp { 5212bc98a42cSDavid Howells if (sb_rdonly(sb) || ext4_forced_shutdown(EXT4_SB(sb))) 52139ca92389STheodore Ts'o return 0; 52149ca92389STheodore Ts'o 5215c642dc9eSEric Sandeen if (EXT4_SB(sb)->s_journal) { 52169ca92389STheodore Ts'o /* Reset the needs_recovery flag before the fs is unlocked. */ 5217e2b911c5SDarrick J. Wong ext4_set_feature_journal_needs_recovery(sb); 5218c642dc9eSEric Sandeen } 5219c642dc9eSEric Sandeen 5220e2d67052STheodore Ts'o ext4_commit_super(sb, 1); 5221c4be0c1dSTakashi Sato return 0; 5222ac27a0ecSDave Kleikamp } 5223ac27a0ecSDave Kleikamp 5224673c6100STheodore Ts'o /* 5225673c6100STheodore Ts'o * Structure to save mount options for ext4_remount's benefit 5226673c6100STheodore Ts'o */ 5227673c6100STheodore Ts'o struct ext4_mount_options { 5228673c6100STheodore Ts'o unsigned long s_mount_opt; 5229a2595b8aSTheodore Ts'o unsigned long s_mount_opt2; 523008cefc7aSEric W. Biederman kuid_t s_resuid; 523108cefc7aSEric W. Biederman kgid_t s_resgid; 5232673c6100STheodore Ts'o unsigned long s_commit_interval; 5233673c6100STheodore Ts'o u32 s_min_batch_time, s_max_batch_time; 5234673c6100STheodore Ts'o #ifdef CONFIG_QUOTA 5235673c6100STheodore Ts'o int s_jquota_fmt; 5236a2d4a646SJan Kara char *s_qf_names[EXT4_MAXQUOTAS]; 5237673c6100STheodore Ts'o #endif 5238673c6100STheodore Ts'o }; 5239673c6100STheodore Ts'o 5240617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data) 5241ac27a0ecSDave Kleikamp { 5242617ba13bSMingming Cao struct ext4_super_block *es; 5243617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 5244ac27a0ecSDave Kleikamp unsigned long old_sb_flags; 5245617ba13bSMingming Cao struct ext4_mount_options old_opts; 5246c79d967dSChristoph Hellwig int enable_quota = 0; 52478a266467STheodore Ts'o ext4_group_t g; 5248b3881f74STheodore Ts'o unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO; 5249c5e06d10SJohann Lombardi int err = 0; 5250ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 525103dafb5fSChen Gang int i, j; 525233458eabSTheodore Ts'o char *to_free[EXT4_MAXQUOTAS]; 5253ac27a0ecSDave Kleikamp #endif 5254d4c402d9SCurt Wohlgemuth char *orig_data = kstrdup(data, GFP_KERNEL); 5255ac27a0ecSDave Kleikamp 525621ac738eSChengguang Xu if (data && !orig_data) 525721ac738eSChengguang Xu return -ENOMEM; 525821ac738eSChengguang Xu 5259ac27a0ecSDave Kleikamp /* Store the original options */ 5260ac27a0ecSDave Kleikamp old_sb_flags = sb->s_flags; 5261ac27a0ecSDave Kleikamp old_opts.s_mount_opt = sbi->s_mount_opt; 5262a2595b8aSTheodore Ts'o old_opts.s_mount_opt2 = sbi->s_mount_opt2; 5263ac27a0ecSDave Kleikamp old_opts.s_resuid = sbi->s_resuid; 5264ac27a0ecSDave Kleikamp old_opts.s_resgid = sbi->s_resgid; 5265ac27a0ecSDave Kleikamp old_opts.s_commit_interval = sbi->s_commit_interval; 526630773840STheodore Ts'o old_opts.s_min_batch_time = sbi->s_min_batch_time; 526730773840STheodore Ts'o old_opts.s_max_batch_time = sbi->s_max_batch_time; 5268ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 5269ac27a0ecSDave Kleikamp old_opts.s_jquota_fmt = sbi->s_jquota_fmt; 5270a2d4a646SJan Kara for (i = 0; i < EXT4_MAXQUOTAS; i++) 527103dafb5fSChen Gang if (sbi->s_qf_names[i]) { 527233458eabSTheodore Ts'o char *qf_name = get_qf_name(sb, sbi, i); 527333458eabSTheodore Ts'o 527433458eabSTheodore Ts'o old_opts.s_qf_names[i] = kstrdup(qf_name, GFP_KERNEL); 527503dafb5fSChen Gang if (!old_opts.s_qf_names[i]) { 527603dafb5fSChen Gang for (j = 0; j < i; j++) 527703dafb5fSChen Gang kfree(old_opts.s_qf_names[j]); 52783e36a163SWei Yongjun kfree(orig_data); 527903dafb5fSChen Gang return -ENOMEM; 528003dafb5fSChen Gang } 528103dafb5fSChen Gang } else 528203dafb5fSChen Gang old_opts.s_qf_names[i] = NULL; 5283ac27a0ecSDave Kleikamp #endif 5284b3881f74STheodore Ts'o if (sbi->s_journal && sbi->s_journal->j_task->io_context) 5285b3881f74STheodore Ts'o journal_ioprio = sbi->s_journal->j_task->io_context->ioprio; 5286ac27a0ecSDave Kleikamp 5287661aa520SEric Sandeen if (!parse_options(data, sb, NULL, &journal_ioprio, 1)) { 5288ac27a0ecSDave Kleikamp err = -EINVAL; 5289ac27a0ecSDave Kleikamp goto restore_opts; 5290ac27a0ecSDave Kleikamp } 5291ac27a0ecSDave Kleikamp 52927bc04c5cSBarret Rhoden ext4_clamp_want_extra_isize(sb); 52937bc04c5cSBarret Rhoden 52946b992ff2SDarrick J. Wong if ((old_opts.s_mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) ^ 52956b992ff2SDarrick J. Wong test_opt(sb, JOURNAL_CHECKSUM)) { 52966b992ff2SDarrick J. Wong ext4_msg(sb, KERN_ERR, "changing journal_checksum " 52972d5b86e0SEric Sandeen "during remount not supported; ignoring"); 52982d5b86e0SEric Sandeen sbi->s_mount_opt ^= EXT4_MOUNT_JOURNAL_CHECKSUM; 5299c6d3d56dSDarrick J. Wong } 5300c6d3d56dSDarrick J. Wong 53016ae6514bSPiotr Sarna if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) { 53026ae6514bSPiotr Sarna if (test_opt2(sb, EXPLICIT_DELALLOC)) { 53036ae6514bSPiotr Sarna ext4_msg(sb, KERN_ERR, "can't mount with " 53046ae6514bSPiotr Sarna "both data=journal and delalloc"); 53056ae6514bSPiotr Sarna err = -EINVAL; 53066ae6514bSPiotr Sarna goto restore_opts; 53076ae6514bSPiotr Sarna } 53086ae6514bSPiotr Sarna if (test_opt(sb, DIOREAD_NOLOCK)) { 53096ae6514bSPiotr Sarna ext4_msg(sb, KERN_ERR, "can't mount with " 53106ae6514bSPiotr Sarna "both data=journal and dioread_nolock"); 53116ae6514bSPiotr Sarna err = -EINVAL; 53126ae6514bSPiotr Sarna goto restore_opts; 53136ae6514bSPiotr Sarna } 5314923ae0ffSRoss Zwisler if (test_opt(sb, DAX)) { 5315923ae0ffSRoss Zwisler ext4_msg(sb, KERN_ERR, "can't mount with " 5316923ae0ffSRoss Zwisler "both data=journal and dax"); 5317923ae0ffSRoss Zwisler err = -EINVAL; 5318923ae0ffSRoss Zwisler goto restore_opts; 5319923ae0ffSRoss Zwisler } 5320ab04df78SJan Kara } else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) { 5321ab04df78SJan Kara if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) { 5322ab04df78SJan Kara ext4_msg(sb, KERN_ERR, "can't mount with " 5323ab04df78SJan Kara "journal_async_commit in data=ordered mode"); 5324ab04df78SJan Kara err = -EINVAL; 5325ab04df78SJan Kara goto restore_opts; 5326ab04df78SJan Kara } 5327923ae0ffSRoss Zwisler } 5328923ae0ffSRoss Zwisler 5329cdb7ee4cSTahsin Erdogan if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_NO_MBCACHE) { 5330cdb7ee4cSTahsin Erdogan ext4_msg(sb, KERN_ERR, "can't enable nombcache during remount"); 5331cdb7ee4cSTahsin Erdogan err = -EINVAL; 5332cdb7ee4cSTahsin Erdogan goto restore_opts; 5333cdb7ee4cSTahsin Erdogan } 5334cdb7ee4cSTahsin Erdogan 5335923ae0ffSRoss Zwisler if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_DAX) { 5336923ae0ffSRoss Zwisler ext4_msg(sb, KERN_WARNING, "warning: refusing change of " 5337923ae0ffSRoss Zwisler "dax flag with busy inodes while remounting"); 5338923ae0ffSRoss Zwisler sbi->s_mount_opt ^= EXT4_MOUNT_DAX; 53396ae6514bSPiotr Sarna } 53406ae6514bSPiotr Sarna 53414ab2f15bSTheodore Ts'o if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) 5342c67d859eSTheodore Ts'o ext4_abort(sb, "Abort forced by user"); 5343ac27a0ecSDave Kleikamp 53441751e8a6SLinus Torvalds sb->s_flags = (sb->s_flags & ~SB_POSIXACL) | 53451751e8a6SLinus Torvalds (test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0); 5346ac27a0ecSDave Kleikamp 5347ac27a0ecSDave Kleikamp es = sbi->s_es; 5348ac27a0ecSDave Kleikamp 5349b3881f74STheodore Ts'o if (sbi->s_journal) { 5350617ba13bSMingming Cao ext4_init_journal_params(sb, sbi->s_journal); 5351b3881f74STheodore Ts'o set_task_ioprio(sbi->s_journal->j_task, journal_ioprio); 5352b3881f74STheodore Ts'o } 5353ac27a0ecSDave Kleikamp 53541751e8a6SLinus Torvalds if (*flags & SB_LAZYTIME) 53551751e8a6SLinus Torvalds sb->s_flags |= SB_LAZYTIME; 5356a2fd66d0STheodore Ts'o 53571751e8a6SLinus Torvalds if ((bool)(*flags & SB_RDONLY) != sb_rdonly(sb)) { 53584ab2f15bSTheodore Ts'o if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) { 5359ac27a0ecSDave Kleikamp err = -EROFS; 5360ac27a0ecSDave Kleikamp goto restore_opts; 5361ac27a0ecSDave Kleikamp } 5362ac27a0ecSDave Kleikamp 53631751e8a6SLinus Torvalds if (*flags & SB_RDONLY) { 536438c03b34STheodore Ts'o err = sync_filesystem(sb); 536538c03b34STheodore Ts'o if (err < 0) 536638c03b34STheodore Ts'o goto restore_opts; 53670f0dd62fSChristoph Hellwig err = dquot_suspend(sb, -1); 53680f0dd62fSChristoph Hellwig if (err < 0) 5369c79d967dSChristoph Hellwig goto restore_opts; 5370c79d967dSChristoph Hellwig 5371ac27a0ecSDave Kleikamp /* 5372ac27a0ecSDave Kleikamp * First of all, the unconditional stuff we have to do 5373ac27a0ecSDave Kleikamp * to disable replay of the journal when we next remount 5374ac27a0ecSDave Kleikamp */ 53751751e8a6SLinus Torvalds sb->s_flags |= SB_RDONLY; 5376ac27a0ecSDave Kleikamp 5377ac27a0ecSDave Kleikamp /* 5378ac27a0ecSDave Kleikamp * OK, test if we are remounting a valid rw partition 5379ac27a0ecSDave Kleikamp * readonly, and if so set the rdonly flag and then 5380ac27a0ecSDave Kleikamp * mark the partition as valid again. 5381ac27a0ecSDave Kleikamp */ 5382617ba13bSMingming Cao if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) && 5383617ba13bSMingming Cao (sbi->s_mount_state & EXT4_VALID_FS)) 5384ac27a0ecSDave Kleikamp es->s_state = cpu_to_le16(sbi->s_mount_state); 5385ac27a0ecSDave Kleikamp 5386a63c9eb2STheodore Ts'o if (sbi->s_journal) 5387617ba13bSMingming Cao ext4_mark_recovery_complete(sb, es); 53882dca60d9STheodore Ts'o if (sbi->s_mmp_tsk) 53892dca60d9STheodore Ts'o kthread_stop(sbi->s_mmp_tsk); 5390ac27a0ecSDave Kleikamp } else { 5391a13fb1a4SEric Sandeen /* Make sure we can mount this feature set readwrite */ 5392e2b911c5SDarrick J. Wong if (ext4_has_feature_readonly(sb) || 53932cb5cc8bSDarrick J. Wong !ext4_feature_set_ok(sb, 0)) { 5394ac27a0ecSDave Kleikamp err = -EROFS; 5395ac27a0ecSDave Kleikamp goto restore_opts; 5396ac27a0ecSDave Kleikamp } 5397ead6596bSEric Sandeen /* 53988a266467STheodore Ts'o * Make sure the group descriptor checksums 53990b8e58a1SAndreas Dilger * are sane. If they aren't, refuse to remount r/w. 54008a266467STheodore Ts'o */ 54018a266467STheodore Ts'o for (g = 0; g < sbi->s_groups_count; g++) { 54028a266467STheodore Ts'o struct ext4_group_desc *gdp = 54038a266467STheodore Ts'o ext4_get_group_desc(sb, g, NULL); 54048a266467STheodore Ts'o 5405feb0ab32SDarrick J. Wong if (!ext4_group_desc_csum_verify(sb, g, gdp)) { 5406b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, 5407b31e1552SEric Sandeen "ext4_remount: Checksum for group %u failed (%u!=%u)", 5408e2b911c5SDarrick J. Wong g, le16_to_cpu(ext4_group_desc_csum(sb, g, gdp)), 54098a266467STheodore Ts'o le16_to_cpu(gdp->bg_checksum)); 54106a797d27SDarrick J. Wong err = -EFSBADCRC; 54118a266467STheodore Ts'o goto restore_opts; 54128a266467STheodore Ts'o } 54138a266467STheodore Ts'o } 54148a266467STheodore Ts'o 54158a266467STheodore Ts'o /* 5416ead6596bSEric Sandeen * If we have an unprocessed orphan list hanging 5417ead6596bSEric Sandeen * around from a previously readonly bdev mount, 5418ead6596bSEric Sandeen * require a full umount/remount for now. 5419ead6596bSEric Sandeen */ 5420ead6596bSEric Sandeen if (es->s_last_orphan) { 5421b31e1552SEric Sandeen ext4_msg(sb, KERN_WARNING, "Couldn't " 5422ead6596bSEric Sandeen "remount RDWR because of unprocessed " 5423ead6596bSEric Sandeen "orphan inode list. Please " 5424b31e1552SEric Sandeen "umount/remount instead"); 5425ead6596bSEric Sandeen err = -EINVAL; 5426ead6596bSEric Sandeen goto restore_opts; 5427ead6596bSEric Sandeen } 5428ead6596bSEric Sandeen 5429ac27a0ecSDave Kleikamp /* 5430ac27a0ecSDave Kleikamp * Mounting a RDONLY partition read-write, so reread 5431ac27a0ecSDave Kleikamp * and store the current valid flag. (It may have 5432ac27a0ecSDave Kleikamp * been changed by e2fsck since we originally mounted 5433ac27a0ecSDave Kleikamp * the partition.) 5434ac27a0ecSDave Kleikamp */ 54350390131bSFrank Mayhar if (sbi->s_journal) 5436617ba13bSMingming Cao ext4_clear_journal_err(sb, es); 5437ac27a0ecSDave Kleikamp sbi->s_mount_state = le16_to_cpu(es->s_state); 5438c89128a0SJaegeuk Kim 5439c89128a0SJaegeuk Kim err = ext4_setup_super(sb, es, 0); 5440c89128a0SJaegeuk Kim if (err) 5441c89128a0SJaegeuk Kim goto restore_opts; 5442c89128a0SJaegeuk Kim 54431751e8a6SLinus Torvalds sb->s_flags &= ~SB_RDONLY; 5444e2b911c5SDarrick J. Wong if (ext4_has_feature_mmp(sb)) 5445c5e06d10SJohann Lombardi if (ext4_multi_mount_protect(sb, 5446c5e06d10SJohann Lombardi le64_to_cpu(es->s_mmp_block))) { 5447c5e06d10SJohann Lombardi err = -EROFS; 5448c5e06d10SJohann Lombardi goto restore_opts; 5449c5e06d10SJohann Lombardi } 5450c79d967dSChristoph Hellwig enable_quota = 1; 5451ac27a0ecSDave Kleikamp } 5452ac27a0ecSDave Kleikamp } 5453bfff6873SLukas Czerner 5454bfff6873SLukas Czerner /* 5455bfff6873SLukas Czerner * Reinitialize lazy itable initialization thread based on 5456bfff6873SLukas Czerner * current settings 5457bfff6873SLukas Czerner */ 5458bc98a42cSDavid Howells if (sb_rdonly(sb) || !test_opt(sb, INIT_INODE_TABLE)) 5459bfff6873SLukas Czerner ext4_unregister_li_request(sb); 5460bfff6873SLukas Czerner else { 5461bfff6873SLukas Czerner ext4_group_t first_not_zeroed; 5462bfff6873SLukas Czerner first_not_zeroed = ext4_has_uninit_itable(sb); 5463bfff6873SLukas Czerner ext4_register_li_request(sb, first_not_zeroed); 5464bfff6873SLukas Czerner } 5465bfff6873SLukas Czerner 54666fd058f7STheodore Ts'o ext4_setup_system_zone(sb); 5467c89128a0SJaegeuk Kim if (sbi->s_journal == NULL && !(old_sb_flags & SB_RDONLY)) { 5468c89128a0SJaegeuk Kim err = ext4_commit_super(sb, 1); 5469c89128a0SJaegeuk Kim if (err) 5470c89128a0SJaegeuk Kim goto restore_opts; 5471c89128a0SJaegeuk Kim } 54720390131bSFrank Mayhar 5473ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 5474ac27a0ecSDave Kleikamp /* Release old quota file names */ 5475a2d4a646SJan Kara for (i = 0; i < EXT4_MAXQUOTAS; i++) 5476ac27a0ecSDave Kleikamp kfree(old_opts.s_qf_names[i]); 54777c319d32SAditya Kali if (enable_quota) { 54787c319d32SAditya Kali if (sb_any_quota_suspended(sb)) 54790f0dd62fSChristoph Hellwig dquot_resume(sb, -1); 5480e2b911c5SDarrick J. Wong else if (ext4_has_feature_quota(sb)) { 54817c319d32SAditya Kali err = ext4_enable_quotas(sb); 548207724f98STheodore Ts'o if (err) 54837c319d32SAditya Kali goto restore_opts; 54847c319d32SAditya Kali } 54857c319d32SAditya Kali } 54867c319d32SAditya Kali #endif 5487d4c402d9SCurt Wohlgemuth 54881751e8a6SLinus Torvalds *flags = (*flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME); 5489d4c402d9SCurt Wohlgemuth ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data); 5490d4c402d9SCurt Wohlgemuth kfree(orig_data); 5491ac27a0ecSDave Kleikamp return 0; 54920b8e58a1SAndreas Dilger 5493ac27a0ecSDave Kleikamp restore_opts: 5494ac27a0ecSDave Kleikamp sb->s_flags = old_sb_flags; 5495ac27a0ecSDave Kleikamp sbi->s_mount_opt = old_opts.s_mount_opt; 5496a2595b8aSTheodore Ts'o sbi->s_mount_opt2 = old_opts.s_mount_opt2; 5497ac27a0ecSDave Kleikamp sbi->s_resuid = old_opts.s_resuid; 5498ac27a0ecSDave Kleikamp sbi->s_resgid = old_opts.s_resgid; 5499ac27a0ecSDave Kleikamp sbi->s_commit_interval = old_opts.s_commit_interval; 550030773840STheodore Ts'o sbi->s_min_batch_time = old_opts.s_min_batch_time; 550130773840STheodore Ts'o sbi->s_max_batch_time = old_opts.s_max_batch_time; 5502ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 5503ac27a0ecSDave Kleikamp sbi->s_jquota_fmt = old_opts.s_jquota_fmt; 5504a2d4a646SJan Kara for (i = 0; i < EXT4_MAXQUOTAS; i++) { 550533458eabSTheodore Ts'o to_free[i] = get_qf_name(sb, sbi, i); 550633458eabSTheodore Ts'o rcu_assign_pointer(sbi->s_qf_names[i], old_opts.s_qf_names[i]); 5507ac27a0ecSDave Kleikamp } 550833458eabSTheodore Ts'o synchronize_rcu(); 550933458eabSTheodore Ts'o for (i = 0; i < EXT4_MAXQUOTAS; i++) 551033458eabSTheodore Ts'o kfree(to_free[i]); 5511ac27a0ecSDave Kleikamp #endif 5512d4c402d9SCurt Wohlgemuth kfree(orig_data); 5513ac27a0ecSDave Kleikamp return err; 5514ac27a0ecSDave Kleikamp } 5515ac27a0ecSDave Kleikamp 5516689c958cSLi Xi #ifdef CONFIG_QUOTA 5517689c958cSLi Xi static int ext4_statfs_project(struct super_block *sb, 5518689c958cSLi Xi kprojid_t projid, struct kstatfs *buf) 5519689c958cSLi Xi { 5520689c958cSLi Xi struct kqid qid; 5521689c958cSLi Xi struct dquot *dquot; 5522689c958cSLi Xi u64 limit; 5523689c958cSLi Xi u64 curblock; 5524689c958cSLi Xi 5525689c958cSLi Xi qid = make_kqid_projid(projid); 5526689c958cSLi Xi dquot = dqget(sb, qid); 5527689c958cSLi Xi if (IS_ERR(dquot)) 5528689c958cSLi Xi return PTR_ERR(dquot); 55297b9ca4c6SJan Kara spin_lock(&dquot->dq_dqb_lock); 5530689c958cSLi Xi 5531689c958cSLi Xi limit = (dquot->dq_dqb.dqb_bsoftlimit ? 5532689c958cSLi Xi dquot->dq_dqb.dqb_bsoftlimit : 5533689c958cSLi Xi dquot->dq_dqb.dqb_bhardlimit) >> sb->s_blocksize_bits; 5534689c958cSLi Xi if (limit && buf->f_blocks > limit) { 5535f06925c7SKonstantin Khlebnikov curblock = (dquot->dq_dqb.dqb_curspace + 5536f06925c7SKonstantin Khlebnikov dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits; 5537689c958cSLi Xi buf->f_blocks = limit; 5538689c958cSLi Xi buf->f_bfree = buf->f_bavail = 5539689c958cSLi Xi (buf->f_blocks > curblock) ? 5540689c958cSLi Xi (buf->f_blocks - curblock) : 0; 5541689c958cSLi Xi } 5542689c958cSLi Xi 5543689c958cSLi Xi limit = dquot->dq_dqb.dqb_isoftlimit ? 5544689c958cSLi Xi dquot->dq_dqb.dqb_isoftlimit : 5545689c958cSLi Xi dquot->dq_dqb.dqb_ihardlimit; 5546689c958cSLi Xi if (limit && buf->f_files > limit) { 5547689c958cSLi Xi buf->f_files = limit; 5548689c958cSLi Xi buf->f_ffree = 5549689c958cSLi Xi (buf->f_files > dquot->dq_dqb.dqb_curinodes) ? 5550689c958cSLi Xi (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0; 5551689c958cSLi Xi } 5552689c958cSLi Xi 55537b9ca4c6SJan Kara spin_unlock(&dquot->dq_dqb_lock); 5554689c958cSLi Xi dqput(dquot); 5555689c958cSLi Xi return 0; 5556689c958cSLi Xi } 5557689c958cSLi Xi #endif 5558689c958cSLi Xi 5559617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf) 5560ac27a0ecSDave Kleikamp { 5561ac27a0ecSDave Kleikamp struct super_block *sb = dentry->d_sb; 5562617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 5563617ba13bSMingming Cao struct ext4_super_block *es = sbi->s_es; 556427dd4385SLukas Czerner ext4_fsblk_t overhead = 0, resv_blocks; 5565960cc398SPekka Enberg u64 fsid; 5566d02a9391SKazuya Mio s64 bfree; 556727dd4385SLukas Czerner resv_blocks = EXT4_C2B(sbi, atomic64_read(&sbi->s_resv_clusters)); 5568ac27a0ecSDave Kleikamp 5569952fc18eSTheodore Ts'o if (!test_opt(sb, MINIX_DF)) 5570952fc18eSTheodore Ts'o overhead = sbi->s_overhead; 5571ac27a0ecSDave Kleikamp 5572617ba13bSMingming Cao buf->f_type = EXT4_SUPER_MAGIC; 5573ac27a0ecSDave Kleikamp buf->f_bsize = sb->s_blocksize; 5574b72f78cbSEric Sandeen buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, overhead); 557557042651STheodore Ts'o bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) - 557657042651STheodore Ts'o percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter); 5577d02a9391SKazuya Mio /* prevent underflow in case that few free space is available */ 557857042651STheodore Ts'o buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0)); 557927dd4385SLukas Czerner buf->f_bavail = buf->f_bfree - 558027dd4385SLukas Czerner (ext4_r_blocks_count(es) + resv_blocks); 558127dd4385SLukas Czerner if (buf->f_bfree < (ext4_r_blocks_count(es) + resv_blocks)) 5582ac27a0ecSDave Kleikamp buf->f_bavail = 0; 5583ac27a0ecSDave Kleikamp buf->f_files = le32_to_cpu(es->s_inodes_count); 558452d9f3b4SPeter Zijlstra buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter); 5585617ba13bSMingming Cao buf->f_namelen = EXT4_NAME_LEN; 5586960cc398SPekka Enberg fsid = le64_to_cpup((void *)es->s_uuid) ^ 5587960cc398SPekka Enberg le64_to_cpup((void *)es->s_uuid + sizeof(u64)); 5588960cc398SPekka Enberg buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL; 5589960cc398SPekka Enberg buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL; 55900b8e58a1SAndreas Dilger 5591689c958cSLi Xi #ifdef CONFIG_QUOTA 5592689c958cSLi Xi if (ext4_test_inode_flag(dentry->d_inode, EXT4_INODE_PROJINHERIT) && 5593689c958cSLi Xi sb_has_quota_limits_enabled(sb, PRJQUOTA)) 5594689c958cSLi Xi ext4_statfs_project(sb, EXT4_I(dentry->d_inode)->i_projid, buf); 5595689c958cSLi Xi #endif 5596ac27a0ecSDave Kleikamp return 0; 5597ac27a0ecSDave Kleikamp } 5598ac27a0ecSDave Kleikamp 5599ac27a0ecSDave Kleikamp 5600ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 5601ac27a0ecSDave Kleikamp 5602bc8230eeSJan Kara /* 5603bc8230eeSJan Kara * Helper functions so that transaction is started before we acquire dqio_sem 5604bc8230eeSJan Kara * to keep correct lock ordering of transaction > dqio_sem 5605bc8230eeSJan Kara */ 5606ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot) 5607ac27a0ecSDave Kleikamp { 56084c376dcaSEric W. Biederman return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type]; 5609ac27a0ecSDave Kleikamp } 5610ac27a0ecSDave Kleikamp 5611617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot) 5612ac27a0ecSDave Kleikamp { 5613ac27a0ecSDave Kleikamp int ret, err; 5614ac27a0ecSDave Kleikamp handle_t *handle; 5615ac27a0ecSDave Kleikamp struct inode *inode; 5616ac27a0ecSDave Kleikamp 5617ac27a0ecSDave Kleikamp inode = dquot_to_inode(dquot); 56189924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 5619617ba13bSMingming Cao EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb)); 5620ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 5621ac27a0ecSDave Kleikamp return PTR_ERR(handle); 5622ac27a0ecSDave Kleikamp ret = dquot_commit(dquot); 5623617ba13bSMingming Cao err = ext4_journal_stop(handle); 5624ac27a0ecSDave Kleikamp if (!ret) 5625ac27a0ecSDave Kleikamp ret = err; 5626ac27a0ecSDave Kleikamp return ret; 5627ac27a0ecSDave Kleikamp } 5628ac27a0ecSDave Kleikamp 5629617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot) 5630ac27a0ecSDave Kleikamp { 5631ac27a0ecSDave Kleikamp int ret, err; 5632ac27a0ecSDave Kleikamp handle_t *handle; 5633ac27a0ecSDave Kleikamp 56349924a92aSTheodore Ts'o handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA, 5635617ba13bSMingming Cao EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb)); 5636ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 5637ac27a0ecSDave Kleikamp return PTR_ERR(handle); 5638ac27a0ecSDave Kleikamp ret = dquot_acquire(dquot); 5639617ba13bSMingming Cao err = ext4_journal_stop(handle); 5640ac27a0ecSDave Kleikamp if (!ret) 5641ac27a0ecSDave Kleikamp ret = err; 5642ac27a0ecSDave Kleikamp return ret; 5643ac27a0ecSDave Kleikamp } 5644ac27a0ecSDave Kleikamp 5645617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot) 5646ac27a0ecSDave Kleikamp { 5647ac27a0ecSDave Kleikamp int ret, err; 5648ac27a0ecSDave Kleikamp handle_t *handle; 5649ac27a0ecSDave Kleikamp 56509924a92aSTheodore Ts'o handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA, 5651617ba13bSMingming Cao EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb)); 56529c3013e9SJan Kara if (IS_ERR(handle)) { 56539c3013e9SJan Kara /* Release dquot anyway to avoid endless cycle in dqput() */ 56549c3013e9SJan Kara dquot_release(dquot); 5655ac27a0ecSDave Kleikamp return PTR_ERR(handle); 56569c3013e9SJan Kara } 5657ac27a0ecSDave Kleikamp ret = dquot_release(dquot); 5658617ba13bSMingming Cao err = ext4_journal_stop(handle); 5659ac27a0ecSDave Kleikamp if (!ret) 5660ac27a0ecSDave Kleikamp ret = err; 5661ac27a0ecSDave Kleikamp return ret; 5662ac27a0ecSDave Kleikamp } 5663ac27a0ecSDave Kleikamp 5664617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot) 5665ac27a0ecSDave Kleikamp { 5666262b4662SJan Kara struct super_block *sb = dquot->dq_sb; 5667262b4662SJan Kara struct ext4_sb_info *sbi = EXT4_SB(sb); 5668262b4662SJan Kara 56692c8be6b2SJan Kara /* Are we journaling quotas? */ 5670e2b911c5SDarrick J. Wong if (ext4_has_feature_quota(sb) || 5671262b4662SJan Kara sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) { 5672ac27a0ecSDave Kleikamp dquot_mark_dquot_dirty(dquot); 5673617ba13bSMingming Cao return ext4_write_dquot(dquot); 5674ac27a0ecSDave Kleikamp } else { 5675ac27a0ecSDave Kleikamp return dquot_mark_dquot_dirty(dquot); 5676ac27a0ecSDave Kleikamp } 5677ac27a0ecSDave Kleikamp } 5678ac27a0ecSDave Kleikamp 5679617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type) 5680ac27a0ecSDave Kleikamp { 5681ac27a0ecSDave Kleikamp int ret, err; 5682ac27a0ecSDave Kleikamp handle_t *handle; 5683ac27a0ecSDave Kleikamp 5684ac27a0ecSDave Kleikamp /* Data block + inode block */ 56852b0143b5SDavid Howells handle = ext4_journal_start(d_inode(sb->s_root), EXT4_HT_QUOTA, 2); 5686ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 5687ac27a0ecSDave Kleikamp return PTR_ERR(handle); 5688ac27a0ecSDave Kleikamp ret = dquot_commit_info(sb, type); 5689617ba13bSMingming Cao err = ext4_journal_stop(handle); 5690ac27a0ecSDave Kleikamp if (!ret) 5691ac27a0ecSDave Kleikamp ret = err; 5692ac27a0ecSDave Kleikamp return ret; 5693ac27a0ecSDave Kleikamp } 5694ac27a0ecSDave Kleikamp 5695ac27a0ecSDave Kleikamp /* 5696ac27a0ecSDave Kleikamp * Turn on quotas during mount time - we need to find 5697ac27a0ecSDave Kleikamp * the quota file and such... 5698ac27a0ecSDave Kleikamp */ 5699617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type) 5700ac27a0ecSDave Kleikamp { 570133458eabSTheodore Ts'o return dquot_quota_on_mount(sb, get_qf_name(sb, EXT4_SB(sb), type), 5702617ba13bSMingming Cao EXT4_SB(sb)->s_jquota_fmt, type); 5703ac27a0ecSDave Kleikamp } 5704ac27a0ecSDave Kleikamp 5705daf647d2STheodore Ts'o static void lockdep_set_quota_inode(struct inode *inode, int subclass) 5706daf647d2STheodore Ts'o { 5707daf647d2STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 5708daf647d2STheodore Ts'o 5709daf647d2STheodore Ts'o /* The first argument of lockdep_set_subclass has to be 5710daf647d2STheodore Ts'o * *exactly* the same as the argument to init_rwsem() --- in 5711daf647d2STheodore Ts'o * this case, in init_once() --- or lockdep gets unhappy 5712daf647d2STheodore Ts'o * because the name of the lock is set using the 5713daf647d2STheodore Ts'o * stringification of the argument to init_rwsem(). 5714daf647d2STheodore Ts'o */ 5715daf647d2STheodore Ts'o (void) ei; /* shut up clang warning if !CONFIG_LOCKDEP */ 5716daf647d2STheodore Ts'o lockdep_set_subclass(&ei->i_data_sem, subclass); 5717daf647d2STheodore Ts'o } 5718daf647d2STheodore Ts'o 5719ac27a0ecSDave Kleikamp /* 5720ac27a0ecSDave Kleikamp * Standard function to be called on quota_on 5721ac27a0ecSDave Kleikamp */ 5722617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id, 57238c54ca9cSAl Viro const struct path *path) 5724ac27a0ecSDave Kleikamp { 5725ac27a0ecSDave Kleikamp int err; 5726ac27a0ecSDave Kleikamp 5727ac27a0ecSDave Kleikamp if (!test_opt(sb, QUOTA)) 5728ac27a0ecSDave Kleikamp return -EINVAL; 57290623543bSJan Kara 5730ac27a0ecSDave Kleikamp /* Quotafile not on the same filesystem? */ 5731d8c9584eSAl Viro if (path->dentry->d_sb != sb) 5732ac27a0ecSDave Kleikamp return -EXDEV; 57330623543bSJan Kara /* Journaling quota? */ 57340623543bSJan Kara if (EXT4_SB(sb)->s_qf_names[type]) { 57352b2d6d01STheodore Ts'o /* Quotafile not in fs root? */ 5736f00c9e44SJan Kara if (path->dentry->d_parent != sb->s_root) 5737b31e1552SEric Sandeen ext4_msg(sb, KERN_WARNING, 5738b31e1552SEric Sandeen "Quota file not on filesystem root. " 5739b31e1552SEric Sandeen "Journaled quota will not work"); 574091389240SJan Kara sb_dqopt(sb)->flags |= DQUOT_NOLIST_DIRTY; 574191389240SJan Kara } else { 574291389240SJan Kara /* 574391389240SJan Kara * Clear the flag just in case mount options changed since 574491389240SJan Kara * last time. 574591389240SJan Kara */ 574691389240SJan Kara sb_dqopt(sb)->flags &= ~DQUOT_NOLIST_DIRTY; 57470623543bSJan Kara } 57480623543bSJan Kara 57490623543bSJan Kara /* 57500623543bSJan Kara * When we journal data on quota file, we have to flush journal to see 57510623543bSJan Kara * all updates to the file when we bypass pagecache... 57520623543bSJan Kara */ 57530390131bSFrank Mayhar if (EXT4_SB(sb)->s_journal && 57542b0143b5SDavid Howells ext4_should_journal_data(d_inode(path->dentry))) { 57550623543bSJan Kara /* 57560623543bSJan Kara * We don't need to lock updates but journal_flush() could 57570623543bSJan Kara * otherwise be livelocked... 57580623543bSJan Kara */ 57590623543bSJan Kara jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); 57607ffe1ea8SHidehiro Kawai err = jbd2_journal_flush(EXT4_SB(sb)->s_journal); 57610623543bSJan Kara jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); 5762f00c9e44SJan Kara if (err) 57637ffe1ea8SHidehiro Kawai return err; 57647ffe1ea8SHidehiro Kawai } 5765957153fcSJan Kara 5766daf647d2STheodore Ts'o lockdep_set_quota_inode(path->dentry->d_inode, I_DATA_SEM_QUOTA); 5767daf647d2STheodore Ts'o err = dquot_quota_on(sb, type, format_id, path); 5768957153fcSJan Kara if (err) { 5769daf647d2STheodore Ts'o lockdep_set_quota_inode(path->dentry->d_inode, 5770daf647d2STheodore Ts'o I_DATA_SEM_NORMAL); 5771957153fcSJan Kara } else { 5772957153fcSJan Kara struct inode *inode = d_inode(path->dentry); 5773957153fcSJan Kara handle_t *handle; 5774957153fcSJan Kara 577561a92987SJan Kara /* 577661a92987SJan Kara * Set inode flags to prevent userspace from messing with quota 577761a92987SJan Kara * files. If this fails, we return success anyway since quotas 577861a92987SJan Kara * are already enabled and this is not a hard failure. 577961a92987SJan Kara */ 5780957153fcSJan Kara inode_lock(inode); 5781957153fcSJan Kara handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1); 5782957153fcSJan Kara if (IS_ERR(handle)) 5783957153fcSJan Kara goto unlock_inode; 5784957153fcSJan Kara EXT4_I(inode)->i_flags |= EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL; 5785957153fcSJan Kara inode_set_flags(inode, S_NOATIME | S_IMMUTABLE, 5786957153fcSJan Kara S_NOATIME | S_IMMUTABLE); 5787957153fcSJan Kara ext4_mark_inode_dirty(handle, inode); 5788957153fcSJan Kara ext4_journal_stop(handle); 5789957153fcSJan Kara unlock_inode: 5790957153fcSJan Kara inode_unlock(inode); 5791957153fcSJan Kara } 5792daf647d2STheodore Ts'o return err; 5793ac27a0ecSDave Kleikamp } 5794ac27a0ecSDave Kleikamp 57957c319d32SAditya Kali static int ext4_quota_enable(struct super_block *sb, int type, int format_id, 57967c319d32SAditya Kali unsigned int flags) 57977c319d32SAditya Kali { 57987c319d32SAditya Kali int err; 57997c319d32SAditya Kali struct inode *qf_inode; 5800a2d4a646SJan Kara unsigned long qf_inums[EXT4_MAXQUOTAS] = { 58017c319d32SAditya Kali le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum), 5802689c958cSLi Xi le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum), 5803689c958cSLi Xi le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum) 58047c319d32SAditya Kali }; 58057c319d32SAditya Kali 5806e2b911c5SDarrick J. Wong BUG_ON(!ext4_has_feature_quota(sb)); 58077c319d32SAditya Kali 58087c319d32SAditya Kali if (!qf_inums[type]) 58097c319d32SAditya Kali return -EPERM; 58107c319d32SAditya Kali 58118a363970STheodore Ts'o qf_inode = ext4_iget(sb, qf_inums[type], EXT4_IGET_SPECIAL); 58127c319d32SAditya Kali if (IS_ERR(qf_inode)) { 58137c319d32SAditya Kali ext4_error(sb, "Bad quota inode # %lu", qf_inums[type]); 58147c319d32SAditya Kali return PTR_ERR(qf_inode); 58157c319d32SAditya Kali } 58167c319d32SAditya Kali 5817bcb13850SJan Kara /* Don't account quota for quota files to avoid recursion */ 5818bcb13850SJan Kara qf_inode->i_flags |= S_NOQUOTA; 5819daf647d2STheodore Ts'o lockdep_set_quota_inode(qf_inode, I_DATA_SEM_QUOTA); 58207c319d32SAditya Kali err = dquot_enable(qf_inode, type, format_id, flags); 5821daf647d2STheodore Ts'o if (err) 5822daf647d2STheodore Ts'o lockdep_set_quota_inode(qf_inode, I_DATA_SEM_NORMAL); 582361157b24SPan Bian iput(qf_inode); 58247c319d32SAditya Kali 58257c319d32SAditya Kali return err; 58267c319d32SAditya Kali } 58277c319d32SAditya Kali 58287c319d32SAditya Kali /* Enable usage tracking for all quota types. */ 58297c319d32SAditya Kali static int ext4_enable_quotas(struct super_block *sb) 58307c319d32SAditya Kali { 58317c319d32SAditya Kali int type, err = 0; 5832a2d4a646SJan Kara unsigned long qf_inums[EXT4_MAXQUOTAS] = { 58337c319d32SAditya Kali le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum), 5834689c958cSLi Xi le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum), 5835689c958cSLi Xi le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum) 58367c319d32SAditya Kali }; 583749da9392SJan Kara bool quota_mopt[EXT4_MAXQUOTAS] = { 583849da9392SJan Kara test_opt(sb, USRQUOTA), 583949da9392SJan Kara test_opt(sb, GRPQUOTA), 584049da9392SJan Kara test_opt(sb, PRJQUOTA), 584149da9392SJan Kara }; 58427c319d32SAditya Kali 584391389240SJan Kara sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY; 5844a2d4a646SJan Kara for (type = 0; type < EXT4_MAXQUOTAS; type++) { 58457c319d32SAditya Kali if (qf_inums[type]) { 58467c319d32SAditya Kali err = ext4_quota_enable(sb, type, QFMT_VFS_V1, 584749da9392SJan Kara DQUOT_USAGE_ENABLED | 584849da9392SJan Kara (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0)); 58497c319d32SAditya Kali if (err) { 58507c319d32SAditya Kali ext4_warning(sb, 585172ba7450STheodore Ts'o "Failed to enable quota tracking " 585272ba7450STheodore Ts'o "(type=%d, err=%d). Please run " 585372ba7450STheodore Ts'o "e2fsck to fix.", type, err); 58547f144fd0SJunichi Uekawa for (type--; type >= 0; type--) 58557f144fd0SJunichi Uekawa dquot_quota_off(sb, type); 58567f144fd0SJunichi Uekawa 58577c319d32SAditya Kali return err; 58587c319d32SAditya Kali } 58597c319d32SAditya Kali } 58607c319d32SAditya Kali } 58617c319d32SAditya Kali return 0; 58627c319d32SAditya Kali } 58637c319d32SAditya Kali 5864ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type) 5865ca0e05e4SDmitry Monakhov { 586621f97697SJan Kara struct inode *inode = sb_dqopt(sb)->files[type]; 586721f97697SJan Kara handle_t *handle; 5868957153fcSJan Kara int err; 586921f97697SJan Kara 587087009d86SDmitry Monakhov /* Force all delayed allocation blocks to be allocated. 587187009d86SDmitry Monakhov * Caller already holds s_umount sem */ 587287009d86SDmitry Monakhov if (test_opt(sb, DELALLOC)) 5873ca0e05e4SDmitry Monakhov sync_filesystem(sb); 5874ca0e05e4SDmitry Monakhov 5875957153fcSJan Kara if (!inode || !igrab(inode)) 58760b268590SAmir Goldstein goto out; 58770b268590SAmir Goldstein 5878957153fcSJan Kara err = dquot_quota_off(sb, type); 5879964edf66SJan Kara if (err || ext4_has_feature_quota(sb)) 5880957153fcSJan Kara goto out_put; 5881957153fcSJan Kara 5882957153fcSJan Kara inode_lock(inode); 588361a92987SJan Kara /* 588461a92987SJan Kara * Update modification times of quota files when userspace can 588561a92987SJan Kara * start looking at them. If we fail, we return success anyway since 588661a92987SJan Kara * this is not a hard failure and quotas are already disabled. 588761a92987SJan Kara */ 58889924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1); 588921f97697SJan Kara if (IS_ERR(handle)) 5890957153fcSJan Kara goto out_unlock; 5891957153fcSJan Kara EXT4_I(inode)->i_flags &= ~(EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL); 5892957153fcSJan Kara inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE); 5893eeca7ea1SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_time(inode); 589421f97697SJan Kara ext4_mark_inode_dirty(handle, inode); 589521f97697SJan Kara ext4_journal_stop(handle); 5896957153fcSJan Kara out_unlock: 5897957153fcSJan Kara inode_unlock(inode); 5898957153fcSJan Kara out_put: 5899964edf66SJan Kara lockdep_set_quota_inode(inode, I_DATA_SEM_NORMAL); 5900957153fcSJan Kara iput(inode); 5901957153fcSJan Kara return err; 590221f97697SJan Kara out: 5903ca0e05e4SDmitry Monakhov return dquot_quota_off(sb, type); 5904ca0e05e4SDmitry Monakhov } 5905ca0e05e4SDmitry Monakhov 5906ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford 5907ac27a0ecSDave Kleikamp * acquiring the locks... As quota files are never truncated and quota code 5908ac27a0ecSDave Kleikamp * itself serializes the operations (and no one else should touch the files) 5909ac27a0ecSDave Kleikamp * we don't have to be afraid of races */ 5910617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data, 5911ac27a0ecSDave Kleikamp size_t len, loff_t off) 5912ac27a0ecSDave Kleikamp { 5913ac27a0ecSDave Kleikamp struct inode *inode = sb_dqopt(sb)->files[type]; 5914725d26d3SAneesh Kumar K.V ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb); 5915ac27a0ecSDave Kleikamp int offset = off & (sb->s_blocksize - 1); 5916ac27a0ecSDave Kleikamp int tocopy; 5917ac27a0ecSDave Kleikamp size_t toread; 5918ac27a0ecSDave Kleikamp struct buffer_head *bh; 5919ac27a0ecSDave Kleikamp loff_t i_size = i_size_read(inode); 5920ac27a0ecSDave Kleikamp 5921ac27a0ecSDave Kleikamp if (off > i_size) 5922ac27a0ecSDave Kleikamp return 0; 5923ac27a0ecSDave Kleikamp if (off+len > i_size) 5924ac27a0ecSDave Kleikamp len = i_size-off; 5925ac27a0ecSDave Kleikamp toread = len; 5926ac27a0ecSDave Kleikamp while (toread > 0) { 5927ac27a0ecSDave Kleikamp tocopy = sb->s_blocksize - offset < toread ? 5928ac27a0ecSDave Kleikamp sb->s_blocksize - offset : toread; 59291c215028STheodore Ts'o bh = ext4_bread(NULL, inode, blk, 0); 59301c215028STheodore Ts'o if (IS_ERR(bh)) 59311c215028STheodore Ts'o return PTR_ERR(bh); 5932ac27a0ecSDave Kleikamp if (!bh) /* A hole? */ 5933ac27a0ecSDave Kleikamp memset(data, 0, tocopy); 5934ac27a0ecSDave Kleikamp else 5935ac27a0ecSDave Kleikamp memcpy(data, bh->b_data+offset, tocopy); 5936ac27a0ecSDave Kleikamp brelse(bh); 5937ac27a0ecSDave Kleikamp offset = 0; 5938ac27a0ecSDave Kleikamp toread -= tocopy; 5939ac27a0ecSDave Kleikamp data += tocopy; 5940ac27a0ecSDave Kleikamp blk++; 5941ac27a0ecSDave Kleikamp } 5942ac27a0ecSDave Kleikamp return len; 5943ac27a0ecSDave Kleikamp } 5944ac27a0ecSDave Kleikamp 5945ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has 5946ac27a0ecSDave Kleikamp * enough credits) */ 5947617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type, 5948ac27a0ecSDave Kleikamp const char *data, size_t len, loff_t off) 5949ac27a0ecSDave Kleikamp { 5950ac27a0ecSDave Kleikamp struct inode *inode = sb_dqopt(sb)->files[type]; 5951725d26d3SAneesh Kumar K.V ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb); 59521c215028STheodore Ts'o int err, offset = off & (sb->s_blocksize - 1); 5953c5e298aeSTheodore Ts'o int retries = 0; 5954ac27a0ecSDave Kleikamp struct buffer_head *bh; 5955ac27a0ecSDave Kleikamp handle_t *handle = journal_current_handle(); 5956ac27a0ecSDave Kleikamp 59570390131bSFrank Mayhar if (EXT4_SB(sb)->s_journal && !handle) { 5958b31e1552SEric Sandeen ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)" 5959b31e1552SEric Sandeen " cancelled because transaction is not started", 59609c3013e9SJan Kara (unsigned long long)off, (unsigned long long)len); 59619c3013e9SJan Kara return -EIO; 59629c3013e9SJan Kara } 596367eeb568SDmitry Monakhov /* 596467eeb568SDmitry Monakhov * Since we account only one data block in transaction credits, 596567eeb568SDmitry Monakhov * then it is impossible to cross a block boundary. 596667eeb568SDmitry Monakhov */ 596767eeb568SDmitry Monakhov if (sb->s_blocksize - offset < len) { 596867eeb568SDmitry Monakhov ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)" 596967eeb568SDmitry Monakhov " cancelled because not block aligned", 597067eeb568SDmitry Monakhov (unsigned long long)off, (unsigned long long)len); 597167eeb568SDmitry Monakhov return -EIO; 597267eeb568SDmitry Monakhov } 597367eeb568SDmitry Monakhov 5974c5e298aeSTheodore Ts'o do { 5975c5e298aeSTheodore Ts'o bh = ext4_bread(handle, inode, blk, 5976c5e298aeSTheodore Ts'o EXT4_GET_BLOCKS_CREATE | 5977c5e298aeSTheodore Ts'o EXT4_GET_BLOCKS_METADATA_NOFAIL); 5978c5e298aeSTheodore Ts'o } while (IS_ERR(bh) && (PTR_ERR(bh) == -ENOSPC) && 5979c5e298aeSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)); 59801c215028STheodore Ts'o if (IS_ERR(bh)) 59811c215028STheodore Ts'o return PTR_ERR(bh); 5982ac27a0ecSDave Kleikamp if (!bh) 5983ac27a0ecSDave Kleikamp goto out; 59845d601255Sliang xie BUFFER_TRACE(bh, "get write access"); 5985617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, bh); 5986ac27a0ecSDave Kleikamp if (err) { 5987ac27a0ecSDave Kleikamp brelse(bh); 59881c215028STheodore Ts'o return err; 5989ac27a0ecSDave Kleikamp } 5990ac27a0ecSDave Kleikamp lock_buffer(bh); 599167eeb568SDmitry Monakhov memcpy(bh->b_data+offset, data, len); 5992ac27a0ecSDave Kleikamp flush_dcache_page(bh->b_page); 5993ac27a0ecSDave Kleikamp unlock_buffer(bh); 59940390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, NULL, bh); 5995ac27a0ecSDave Kleikamp brelse(bh); 5996ac27a0ecSDave Kleikamp out: 599767eeb568SDmitry Monakhov if (inode->i_size < off + len) { 599867eeb568SDmitry Monakhov i_size_write(inode, off + len); 5999617ba13bSMingming Cao EXT4_I(inode)->i_disksize = inode->i_size; 6000617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 600121f97697SJan Kara } 600267eeb568SDmitry Monakhov return len; 6003ac27a0ecSDave Kleikamp } 6004ac27a0ecSDave Kleikamp #endif 6005ac27a0ecSDave Kleikamp 6006152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags, 6007152a0836SAl Viro const char *dev_name, void *data) 6008ac27a0ecSDave Kleikamp { 6009152a0836SAl Viro return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super); 6010ac27a0ecSDave Kleikamp } 6011ac27a0ecSDave Kleikamp 6012c290ea01SJan Kara #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2) 601324b58424STheodore Ts'o static inline void register_as_ext2(void) 601424b58424STheodore Ts'o { 601524b58424STheodore Ts'o int err = register_filesystem(&ext2_fs_type); 601624b58424STheodore Ts'o if (err) 601724b58424STheodore Ts'o printk(KERN_WARNING 601824b58424STheodore Ts'o "EXT4-fs: Unable to register as ext2 (%d)\n", err); 601924b58424STheodore Ts'o } 602024b58424STheodore Ts'o 602124b58424STheodore Ts'o static inline void unregister_as_ext2(void) 602224b58424STheodore Ts'o { 602324b58424STheodore Ts'o unregister_filesystem(&ext2_fs_type); 602424b58424STheodore Ts'o } 60252035e776STheodore Ts'o 60262035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb) 60272035e776STheodore Ts'o { 6028e2b911c5SDarrick J. Wong if (ext4_has_unknown_ext2_incompat_features(sb)) 60292035e776STheodore Ts'o return 0; 6030bc98a42cSDavid Howells if (sb_rdonly(sb)) 60312035e776STheodore Ts'o return 1; 6032e2b911c5SDarrick J. Wong if (ext4_has_unknown_ext2_ro_compat_features(sb)) 60332035e776STheodore Ts'o return 0; 60342035e776STheodore Ts'o return 1; 60352035e776STheodore Ts'o } 603624b58424STheodore Ts'o #else 603724b58424STheodore Ts'o static inline void register_as_ext2(void) { } 603824b58424STheodore Ts'o static inline void unregister_as_ext2(void) { } 60392035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; } 604024b58424STheodore Ts'o #endif 604124b58424STheodore Ts'o 604224b58424STheodore Ts'o static inline void register_as_ext3(void) 604324b58424STheodore Ts'o { 604424b58424STheodore Ts'o int err = register_filesystem(&ext3_fs_type); 604524b58424STheodore Ts'o if (err) 604624b58424STheodore Ts'o printk(KERN_WARNING 604724b58424STheodore Ts'o "EXT4-fs: Unable to register as ext3 (%d)\n", err); 604824b58424STheodore Ts'o } 604924b58424STheodore Ts'o 605024b58424STheodore Ts'o static inline void unregister_as_ext3(void) 605124b58424STheodore Ts'o { 605224b58424STheodore Ts'o unregister_filesystem(&ext3_fs_type); 605324b58424STheodore Ts'o } 60542035e776STheodore Ts'o 60552035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb) 60562035e776STheodore Ts'o { 6057e2b911c5SDarrick J. Wong if (ext4_has_unknown_ext3_incompat_features(sb)) 60582035e776STheodore Ts'o return 0; 6059e2b911c5SDarrick J. Wong if (!ext4_has_feature_journal(sb)) 60602035e776STheodore Ts'o return 0; 6061bc98a42cSDavid Howells if (sb_rdonly(sb)) 60622035e776STheodore Ts'o return 1; 6063e2b911c5SDarrick J. Wong if (ext4_has_unknown_ext3_ro_compat_features(sb)) 60642035e776STheodore Ts'o return 0; 60652035e776STheodore Ts'o return 1; 60662035e776STheodore Ts'o } 606724b58424STheodore Ts'o 606803010a33STheodore Ts'o static struct file_system_type ext4_fs_type = { 6069ac27a0ecSDave Kleikamp .owner = THIS_MODULE, 607003010a33STheodore Ts'o .name = "ext4", 6071152a0836SAl Viro .mount = ext4_mount, 6072ac27a0ecSDave Kleikamp .kill_sb = kill_block_super, 6073ac27a0ecSDave Kleikamp .fs_flags = FS_REQUIRES_DEV, 6074ac27a0ecSDave Kleikamp }; 60757f78e035SEric W. Biederman MODULE_ALIAS_FS("ext4"); 6076ac27a0ecSDave Kleikamp 6077e9e3bcecSEric Sandeen /* Shared across all ext4 file systems */ 6078e9e3bcecSEric Sandeen wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ]; 6079e9e3bcecSEric Sandeen 60805dabfc78STheodore Ts'o static int __init ext4_init_fs(void) 6081ac27a0ecSDave Kleikamp { 6082e9e3bcecSEric Sandeen int i, err; 6083c9de560dSAlex Tomas 6084e294a537STheodore Ts'o ratelimit_state_init(&ext4_mount_msg_ratelimit, 30 * HZ, 64); 608507c0c5d8SAl Viro ext4_li_info = NULL; 608607c0c5d8SAl Viro mutex_init(&ext4_li_mtx); 608707c0c5d8SAl Viro 60889a4c8019SCarlos Maiolino /* Build-time check for flags consistency */ 608912e9b892SDmitry Monakhov ext4_check_flag_values(); 6090e9e3bcecSEric Sandeen 6091e142d052SJan Kara for (i = 0; i < EXT4_WQ_HASH_SZ; i++) 6092e9e3bcecSEric Sandeen init_waitqueue_head(&ext4__ioend_wq[i]); 6093e9e3bcecSEric Sandeen 609451865fdaSZheng Liu err = ext4_init_es(); 60956fd058f7STheodore Ts'o if (err) 60966fd058f7STheodore Ts'o return err; 609751865fdaSZheng Liu 60981dc0aa46SEric Whitney err = ext4_init_pending(); 60991dc0aa46SEric Whitney if (err) 610022cfe4b4SEric Biggers goto out7; 610122cfe4b4SEric Biggers 610222cfe4b4SEric Biggers err = ext4_init_post_read_processing(); 610322cfe4b4SEric Biggers if (err) 61041dc0aa46SEric Whitney goto out6; 61051dc0aa46SEric Whitney 610651865fdaSZheng Liu err = ext4_init_pageio(); 610751865fdaSZheng Liu if (err) 6108b5799018STheodore Ts'o goto out5; 610951865fdaSZheng Liu 61105dabfc78STheodore Ts'o err = ext4_init_system_zone(); 6111bd2d0210STheodore Ts'o if (err) 6112b5799018STheodore Ts'o goto out4; 6113857ac889SLukas Czerner 6114b5799018STheodore Ts'o err = ext4_init_sysfs(); 6115dd68314cSTheodore Ts'o if (err) 6116b5799018STheodore Ts'o goto out3; 6117857ac889SLukas Czerner 61185dabfc78STheodore Ts'o err = ext4_init_mballoc(); 6119ac27a0ecSDave Kleikamp if (err) 6120c9de560dSAlex Tomas goto out2; 6121ac27a0ecSDave Kleikamp err = init_inodecache(); 6122ac27a0ecSDave Kleikamp if (err) 6123ac27a0ecSDave Kleikamp goto out1; 612424b58424STheodore Ts'o register_as_ext3(); 61252035e776STheodore Ts'o register_as_ext2(); 612603010a33STheodore Ts'o err = register_filesystem(&ext4_fs_type); 6127ac27a0ecSDave Kleikamp if (err) 6128ac27a0ecSDave Kleikamp goto out; 6129bfff6873SLukas Czerner 6130ac27a0ecSDave Kleikamp return 0; 6131ac27a0ecSDave Kleikamp out: 613224b58424STheodore Ts'o unregister_as_ext2(); 613324b58424STheodore Ts'o unregister_as_ext3(); 6134ac27a0ecSDave Kleikamp destroy_inodecache(); 6135ac27a0ecSDave Kleikamp out1: 61365dabfc78STheodore Ts'o ext4_exit_mballoc(); 61379c191f70ST Makphaibulchoke out2: 6138b5799018STheodore Ts'o ext4_exit_sysfs(); 6139b5799018STheodore Ts'o out3: 6140dd68314cSTheodore Ts'o ext4_exit_system_zone(); 6141b5799018STheodore Ts'o out4: 61425dabfc78STheodore Ts'o ext4_exit_pageio(); 6143b5799018STheodore Ts'o out5: 614422cfe4b4SEric Biggers ext4_exit_post_read_processing(); 61451dc0aa46SEric Whitney out6: 614622cfe4b4SEric Biggers ext4_exit_pending(); 614722cfe4b4SEric Biggers out7: 614851865fdaSZheng Liu ext4_exit_es(); 614951865fdaSZheng Liu 6150ac27a0ecSDave Kleikamp return err; 6151ac27a0ecSDave Kleikamp } 6152ac27a0ecSDave Kleikamp 61535dabfc78STheodore Ts'o static void __exit ext4_exit_fs(void) 6154ac27a0ecSDave Kleikamp { 6155bfff6873SLukas Czerner ext4_destroy_lazyinit_thread(); 615624b58424STheodore Ts'o unregister_as_ext2(); 615724b58424STheodore Ts'o unregister_as_ext3(); 615803010a33STheodore Ts'o unregister_filesystem(&ext4_fs_type); 6159ac27a0ecSDave Kleikamp destroy_inodecache(); 61605dabfc78STheodore Ts'o ext4_exit_mballoc(); 6161b5799018STheodore Ts'o ext4_exit_sysfs(); 61625dabfc78STheodore Ts'o ext4_exit_system_zone(); 61635dabfc78STheodore Ts'o ext4_exit_pageio(); 616422cfe4b4SEric Biggers ext4_exit_post_read_processing(); 6165dd12ed14SEric Sandeen ext4_exit_es(); 61661dc0aa46SEric Whitney ext4_exit_pending(); 6167ac27a0ecSDave Kleikamp } 6168ac27a0ecSDave Kleikamp 6169ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others"); 617083982b6fSTheodore Ts'o MODULE_DESCRIPTION("Fourth Extended Filesystem"); 6171ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL"); 61727ef79ad5STheodore Ts'o MODULE_SOFTDEP("pre: crc32c"); 61735dabfc78STheodore Ts'o module_init(ext4_init_fs) 61745dabfc78STheodore Ts'o module_exit(ext4_exit_fs) 6175