1ac27a0ecSDave Kleikamp /* 2617ba13bSMingming Cao * linux/fs/ext4/super.c 3ac27a0ecSDave Kleikamp * 4ac27a0ecSDave Kleikamp * Copyright (C) 1992, 1993, 1994, 1995 5ac27a0ecSDave Kleikamp * Remy Card (card@masi.ibp.fr) 6ac27a0ecSDave Kleikamp * Laboratoire MASI - Institut Blaise Pascal 7ac27a0ecSDave Kleikamp * Universite Pierre et Marie Curie (Paris VI) 8ac27a0ecSDave Kleikamp * 9ac27a0ecSDave Kleikamp * from 10ac27a0ecSDave Kleikamp * 11ac27a0ecSDave Kleikamp * linux/fs/minix/inode.c 12ac27a0ecSDave Kleikamp * 13ac27a0ecSDave Kleikamp * Copyright (C) 1991, 1992 Linus Torvalds 14ac27a0ecSDave Kleikamp * 15ac27a0ecSDave Kleikamp * Big-endian to little-endian byte-swapping/bitmaps by 16ac27a0ecSDave Kleikamp * David S. Miller (davem@caip.rutgers.edu), 1995 17ac27a0ecSDave Kleikamp */ 18ac27a0ecSDave Kleikamp 19ac27a0ecSDave Kleikamp #include <linux/module.h> 20ac27a0ecSDave Kleikamp #include <linux/string.h> 21ac27a0ecSDave Kleikamp #include <linux/fs.h> 22ac27a0ecSDave Kleikamp #include <linux/time.h> 23c5ca7c76STheodore Ts'o #include <linux/vmalloc.h> 24dab291afSMingming Cao #include <linux/jbd2.h> 25ac27a0ecSDave Kleikamp #include <linux/slab.h> 26ac27a0ecSDave Kleikamp #include <linux/init.h> 27ac27a0ecSDave Kleikamp #include <linux/blkdev.h> 28ac27a0ecSDave Kleikamp #include <linux/parser.h> 29ac27a0ecSDave Kleikamp #include <linux/buffer_head.h> 30a5694255SChristoph Hellwig #include <linux/exportfs.h> 31ac27a0ecSDave Kleikamp #include <linux/vfs.h> 32ac27a0ecSDave Kleikamp #include <linux/random.h> 33ac27a0ecSDave Kleikamp #include <linux/mount.h> 34ac27a0ecSDave Kleikamp #include <linux/namei.h> 35ac27a0ecSDave Kleikamp #include <linux/quotaops.h> 36ac27a0ecSDave Kleikamp #include <linux/seq_file.h> 379f6200bbSTheodore Ts'o #include <linux/proc_fs.h> 383197ebdbSTheodore Ts'o #include <linux/ctype.h> 391330593eSVignesh Babu #include <linux/log2.h> 40717d50e4SAndreas Dilger #include <linux/crc16.h> 417abc52c2SDan Magenheimer #include <linux/cleancache.h> 42ac27a0ecSDave Kleikamp #include <asm/uaccess.h> 43ac27a0ecSDave Kleikamp 44bfff6873SLukas Czerner #include <linux/kthread.h> 45bfff6873SLukas Czerner #include <linux/freezer.h> 46bfff6873SLukas Czerner 473dcf5451SChristoph Hellwig #include "ext4.h" 486f91bc5fSEric Gouriou #include "ext4_extents.h" 493dcf5451SChristoph Hellwig #include "ext4_jbd2.h" 50ac27a0ecSDave Kleikamp #include "xattr.h" 51ac27a0ecSDave Kleikamp #include "acl.h" 523661d286STheodore Ts'o #include "mballoc.h" 53ac27a0ecSDave Kleikamp 549bffad1eSTheodore Ts'o #define CREATE_TRACE_POINTS 559bffad1eSTheodore Ts'o #include <trace/events/ext4.h> 569bffad1eSTheodore Ts'o 571f109d5aSTheodore Ts'o static struct proc_dir_entry *ext4_proc_root; 583197ebdbSTheodore Ts'o static struct kset *ext4_kset; 590b75a840SLukas Czerner static struct ext4_lazy_init *ext4_li_info; 600b75a840SLukas Czerner static struct mutex ext4_li_mtx; 610b75a840SLukas Czerner static struct ext4_features *ext4_feat; 629f6200bbSTheodore Ts'o 63617ba13bSMingming Cao static int ext4_load_journal(struct super_block *, struct ext4_super_block *, 64ac27a0ecSDave Kleikamp unsigned long journal_devnum); 652adf6da8STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root); 66e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync); 67617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb, 68617ba13bSMingming Cao struct ext4_super_block *es); 69617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb, 70617ba13bSMingming Cao struct ext4_super_block *es); 71617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait); 72617ba13bSMingming Cao static const char *ext4_decode_error(struct super_block *sb, int errno, 73ac27a0ecSDave Kleikamp char nbuf[16]); 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); 77617ba13bSMingming Cao static void ext4_write_super(struct super_block *sb); 78c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb); 79152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags, 80152a0836SAl Viro const char *dev_name, void *data); 812035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb); 822035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb); 83d39195c3SAmir Goldstein static int ext4_feature_set_ok(struct super_block *sb, int readonly); 84bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void); 85bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb); 868f1f7453SEric Sandeen static void ext4_clear_request_list(void); 87ac27a0ecSDave Kleikamp 882035e776STheodore Ts'o #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23) 892035e776STheodore Ts'o static struct file_system_type ext2_fs_type = { 902035e776STheodore Ts'o .owner = THIS_MODULE, 912035e776STheodore Ts'o .name = "ext2", 922035e776STheodore Ts'o .mount = ext4_mount, 932035e776STheodore Ts'o .kill_sb = kill_block_super, 942035e776STheodore Ts'o .fs_flags = FS_REQUIRES_DEV, 952035e776STheodore Ts'o }; 962035e776STheodore Ts'o #define IS_EXT2_SB(sb) ((sb)->s_bdev->bd_holder == &ext2_fs_type) 972035e776STheodore Ts'o #else 982035e776STheodore Ts'o #define IS_EXT2_SB(sb) (0) 992035e776STheodore Ts'o #endif 1002035e776STheodore Ts'o 1012035e776STheodore Ts'o 102ba69f9abSJan Kara #if !defined(CONFIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23) 103ba69f9abSJan Kara static struct file_system_type ext3_fs_type = { 104ba69f9abSJan Kara .owner = THIS_MODULE, 105ba69f9abSJan Kara .name = "ext3", 106152a0836SAl Viro .mount = ext4_mount, 107ba69f9abSJan Kara .kill_sb = kill_block_super, 108ba69f9abSJan Kara .fs_flags = FS_REQUIRES_DEV, 109ba69f9abSJan Kara }; 110ba69f9abSJan Kara #define IS_EXT3_SB(sb) ((sb)->s_bdev->bd_holder == &ext3_fs_type) 111ba69f9abSJan Kara #else 112ba69f9abSJan Kara #define IS_EXT3_SB(sb) (0) 113ba69f9abSJan Kara #endif 114bd81d8eeSLaurent Vivier 1159933fc0aSTheodore Ts'o void *ext4_kvmalloc(size_t size, gfp_t flags) 1169933fc0aSTheodore Ts'o { 1179933fc0aSTheodore Ts'o void *ret; 1189933fc0aSTheodore Ts'o 1199933fc0aSTheodore Ts'o ret = kmalloc(size, flags); 1209933fc0aSTheodore Ts'o if (!ret) 1219933fc0aSTheodore Ts'o ret = __vmalloc(size, flags, PAGE_KERNEL); 1229933fc0aSTheodore Ts'o return ret; 1239933fc0aSTheodore Ts'o } 1249933fc0aSTheodore Ts'o 1259933fc0aSTheodore Ts'o void *ext4_kvzalloc(size_t size, gfp_t flags) 1269933fc0aSTheodore Ts'o { 1279933fc0aSTheodore Ts'o void *ret; 1289933fc0aSTheodore Ts'o 129db9481c0SMathias Krause ret = kzalloc(size, flags); 1309933fc0aSTheodore Ts'o if (!ret) 1319933fc0aSTheodore Ts'o ret = __vmalloc(size, flags | __GFP_ZERO, PAGE_KERNEL); 1329933fc0aSTheodore Ts'o return ret; 1339933fc0aSTheodore Ts'o } 1349933fc0aSTheodore Ts'o 1359933fc0aSTheodore Ts'o void ext4_kvfree(void *ptr) 1369933fc0aSTheodore Ts'o { 1379933fc0aSTheodore Ts'o if (is_vmalloc_addr(ptr)) 1389933fc0aSTheodore Ts'o vfree(ptr); 1399933fc0aSTheodore Ts'o else 1409933fc0aSTheodore Ts'o kfree(ptr); 1419933fc0aSTheodore Ts'o 1429933fc0aSTheodore Ts'o } 1439933fc0aSTheodore Ts'o 1448fadc143SAlexandre Ratchov ext4_fsblk_t ext4_block_bitmap(struct super_block *sb, 1458fadc143SAlexandre Ratchov struct ext4_group_desc *bg) 146bd81d8eeSLaurent Vivier { 1473a14589cSAneesh Kumar K.V return le32_to_cpu(bg->bg_block_bitmap_lo) | 1488fadc143SAlexandre Ratchov (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? 1498fadc143SAlexandre Ratchov (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0); 150bd81d8eeSLaurent Vivier } 151bd81d8eeSLaurent Vivier 1528fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb, 1538fadc143SAlexandre Ratchov struct ext4_group_desc *bg) 154bd81d8eeSLaurent Vivier { 1555272f837SAneesh Kumar K.V return le32_to_cpu(bg->bg_inode_bitmap_lo) | 1568fadc143SAlexandre Ratchov (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? 1578fadc143SAlexandre Ratchov (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0); 158bd81d8eeSLaurent Vivier } 159bd81d8eeSLaurent Vivier 1608fadc143SAlexandre Ratchov ext4_fsblk_t ext4_inode_table(struct super_block *sb, 1618fadc143SAlexandre Ratchov struct ext4_group_desc *bg) 162bd81d8eeSLaurent Vivier { 1635272f837SAneesh Kumar K.V return le32_to_cpu(bg->bg_inode_table_lo) | 1648fadc143SAlexandre Ratchov (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? 1658fadc143SAlexandre Ratchov (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0); 166bd81d8eeSLaurent Vivier } 167bd81d8eeSLaurent Vivier 168021b65bbSTheodore Ts'o __u32 ext4_free_group_clusters(struct super_block *sb, 169560671a0SAneesh Kumar K.V struct ext4_group_desc *bg) 170560671a0SAneesh Kumar K.V { 171560671a0SAneesh Kumar K.V return le16_to_cpu(bg->bg_free_blocks_count_lo) | 172560671a0SAneesh Kumar K.V (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? 173560671a0SAneesh Kumar K.V (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0); 174560671a0SAneesh Kumar K.V } 175560671a0SAneesh Kumar K.V 176560671a0SAneesh Kumar K.V __u32 ext4_free_inodes_count(struct super_block *sb, 177560671a0SAneesh Kumar K.V struct ext4_group_desc *bg) 178560671a0SAneesh Kumar K.V { 179560671a0SAneesh Kumar K.V return le16_to_cpu(bg->bg_free_inodes_count_lo) | 180560671a0SAneesh Kumar K.V (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? 181560671a0SAneesh Kumar K.V (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0); 182560671a0SAneesh Kumar K.V } 183560671a0SAneesh Kumar K.V 184560671a0SAneesh Kumar K.V __u32 ext4_used_dirs_count(struct super_block *sb, 185560671a0SAneesh Kumar K.V struct ext4_group_desc *bg) 186560671a0SAneesh Kumar K.V { 187560671a0SAneesh Kumar K.V return le16_to_cpu(bg->bg_used_dirs_count_lo) | 188560671a0SAneesh Kumar K.V (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? 189560671a0SAneesh Kumar K.V (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0); 190560671a0SAneesh Kumar K.V } 191560671a0SAneesh Kumar K.V 192560671a0SAneesh Kumar K.V __u32 ext4_itable_unused_count(struct super_block *sb, 193560671a0SAneesh Kumar K.V struct ext4_group_desc *bg) 194560671a0SAneesh Kumar K.V { 195560671a0SAneesh Kumar K.V return le16_to_cpu(bg->bg_itable_unused_lo) | 196560671a0SAneesh Kumar K.V (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ? 197560671a0SAneesh Kumar K.V (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0); 198560671a0SAneesh Kumar K.V } 199560671a0SAneesh Kumar K.V 2008fadc143SAlexandre Ratchov void ext4_block_bitmap_set(struct super_block *sb, 2018fadc143SAlexandre Ratchov struct ext4_group_desc *bg, ext4_fsblk_t blk) 202bd81d8eeSLaurent Vivier { 2033a14589cSAneesh Kumar K.V bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk); 2048fadc143SAlexandre Ratchov if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) 2058fadc143SAlexandre Ratchov bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32); 206bd81d8eeSLaurent Vivier } 207bd81d8eeSLaurent Vivier 2088fadc143SAlexandre Ratchov void ext4_inode_bitmap_set(struct super_block *sb, 2098fadc143SAlexandre Ratchov struct ext4_group_desc *bg, ext4_fsblk_t blk) 210bd81d8eeSLaurent Vivier { 2115272f837SAneesh Kumar K.V bg->bg_inode_bitmap_lo = cpu_to_le32((u32)blk); 2128fadc143SAlexandre Ratchov if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) 2138fadc143SAlexandre Ratchov bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32); 214bd81d8eeSLaurent Vivier } 215bd81d8eeSLaurent Vivier 2168fadc143SAlexandre Ratchov void ext4_inode_table_set(struct super_block *sb, 2178fadc143SAlexandre Ratchov struct ext4_group_desc *bg, ext4_fsblk_t blk) 218bd81d8eeSLaurent Vivier { 2195272f837SAneesh Kumar K.V bg->bg_inode_table_lo = cpu_to_le32((u32)blk); 2208fadc143SAlexandre Ratchov if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) 2218fadc143SAlexandre Ratchov bg->bg_inode_table_hi = cpu_to_le32(blk >> 32); 222bd81d8eeSLaurent Vivier } 223bd81d8eeSLaurent Vivier 224021b65bbSTheodore Ts'o void ext4_free_group_clusters_set(struct super_block *sb, 225560671a0SAneesh Kumar K.V struct ext4_group_desc *bg, __u32 count) 226560671a0SAneesh Kumar K.V { 227560671a0SAneesh Kumar K.V bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count); 228560671a0SAneesh Kumar K.V if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) 229560671a0SAneesh Kumar K.V bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16); 230560671a0SAneesh Kumar K.V } 231560671a0SAneesh Kumar K.V 232560671a0SAneesh Kumar K.V void ext4_free_inodes_set(struct super_block *sb, 233560671a0SAneesh Kumar K.V struct ext4_group_desc *bg, __u32 count) 234560671a0SAneesh Kumar K.V { 235560671a0SAneesh Kumar K.V bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count); 236560671a0SAneesh Kumar K.V if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) 237560671a0SAneesh Kumar K.V bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16); 238560671a0SAneesh Kumar K.V } 239560671a0SAneesh Kumar K.V 240560671a0SAneesh Kumar K.V void ext4_used_dirs_set(struct super_block *sb, 241560671a0SAneesh Kumar K.V struct ext4_group_desc *bg, __u32 count) 242560671a0SAneesh Kumar K.V { 243560671a0SAneesh Kumar K.V bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count); 244560671a0SAneesh Kumar K.V if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) 245560671a0SAneesh Kumar K.V bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16); 246560671a0SAneesh Kumar K.V } 247560671a0SAneesh Kumar K.V 248560671a0SAneesh Kumar K.V void ext4_itable_unused_set(struct super_block *sb, 249560671a0SAneesh Kumar K.V struct ext4_group_desc *bg, __u32 count) 250560671a0SAneesh Kumar K.V { 251560671a0SAneesh Kumar K.V bg->bg_itable_unused_lo = cpu_to_le16((__u16)count); 252560671a0SAneesh Kumar K.V if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT) 253560671a0SAneesh Kumar K.V bg->bg_itable_unused_hi = cpu_to_le16(count >> 16); 254560671a0SAneesh Kumar K.V } 255560671a0SAneesh Kumar K.V 256d3d1faf6SCurt Wohlgemuth 257d3d1faf6SCurt Wohlgemuth /* Just increment the non-pointer handle value */ 258d3d1faf6SCurt Wohlgemuth static handle_t *ext4_get_nojournal(void) 259d3d1faf6SCurt Wohlgemuth { 260d3d1faf6SCurt Wohlgemuth handle_t *handle = current->journal_info; 261d3d1faf6SCurt Wohlgemuth unsigned long ref_cnt = (unsigned long)handle; 262d3d1faf6SCurt Wohlgemuth 263d3d1faf6SCurt Wohlgemuth BUG_ON(ref_cnt >= EXT4_NOJOURNAL_MAX_REF_COUNT); 264d3d1faf6SCurt Wohlgemuth 265d3d1faf6SCurt Wohlgemuth ref_cnt++; 266d3d1faf6SCurt Wohlgemuth handle = (handle_t *)ref_cnt; 267d3d1faf6SCurt Wohlgemuth 268d3d1faf6SCurt Wohlgemuth current->journal_info = handle; 269d3d1faf6SCurt Wohlgemuth return handle; 270d3d1faf6SCurt Wohlgemuth } 271d3d1faf6SCurt Wohlgemuth 272d3d1faf6SCurt Wohlgemuth 273d3d1faf6SCurt Wohlgemuth /* Decrement the non-pointer handle value */ 274d3d1faf6SCurt Wohlgemuth static void ext4_put_nojournal(handle_t *handle) 275d3d1faf6SCurt Wohlgemuth { 276d3d1faf6SCurt Wohlgemuth unsigned long ref_cnt = (unsigned long)handle; 277d3d1faf6SCurt Wohlgemuth 278d3d1faf6SCurt Wohlgemuth BUG_ON(ref_cnt == 0); 279d3d1faf6SCurt Wohlgemuth 280d3d1faf6SCurt Wohlgemuth ref_cnt--; 281d3d1faf6SCurt Wohlgemuth handle = (handle_t *)ref_cnt; 282d3d1faf6SCurt Wohlgemuth 283d3d1faf6SCurt Wohlgemuth current->journal_info = handle; 284d3d1faf6SCurt Wohlgemuth } 285d3d1faf6SCurt Wohlgemuth 286ac27a0ecSDave Kleikamp /* 287dab291afSMingming Cao * Wrappers for jbd2_journal_start/end. 288ac27a0ecSDave Kleikamp * 289ac27a0ecSDave Kleikamp * The only special thing we need to do here is to make sure that all 290ac27a0ecSDave Kleikamp * journal_end calls result in the superblock being marked dirty, so 291ac27a0ecSDave Kleikamp * that sync() will call the filesystem's write_super callback if 292ac27a0ecSDave Kleikamp * appropriate. 293be4f27d3SYongqiang Yang * 294be4f27d3SYongqiang Yang * To avoid j_barrier hold in userspace when a user calls freeze(), 295be4f27d3SYongqiang Yang * ext4 prevents a new handle from being started by s_frozen, which 296be4f27d3SYongqiang Yang * is in an upper layer. 297ac27a0ecSDave Kleikamp */ 298617ba13bSMingming Cao handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks) 299ac27a0ecSDave Kleikamp { 300ac27a0ecSDave Kleikamp journal_t *journal; 301be4f27d3SYongqiang Yang handle_t *handle; 302ac27a0ecSDave Kleikamp 30312706394STheodore Ts'o trace_ext4_journal_start(sb, nblocks, _RET_IP_); 304ac27a0ecSDave Kleikamp if (sb->s_flags & MS_RDONLY) 305ac27a0ecSDave Kleikamp return ERR_PTR(-EROFS); 306ac27a0ecSDave Kleikamp 307617ba13bSMingming Cao journal = EXT4_SB(sb)->s_journal; 308be4f27d3SYongqiang Yang handle = ext4_journal_current_handle(); 309be4f27d3SYongqiang Yang 310be4f27d3SYongqiang Yang /* 311be4f27d3SYongqiang Yang * If a handle has been started, it should be allowed to 312be4f27d3SYongqiang Yang * finish, otherwise deadlock could happen between freeze 313be4f27d3SYongqiang Yang * and others(e.g. truncate) due to the restart of the 314be4f27d3SYongqiang Yang * journal handle if the filesystem is forzen and active 315be4f27d3SYongqiang Yang * handles are not stopped. 316be4f27d3SYongqiang Yang */ 317be4f27d3SYongqiang Yang if (!handle) 318be4f27d3SYongqiang Yang vfs_check_frozen(sb, SB_FREEZE_TRANS); 319be4f27d3SYongqiang Yang 320be4f27d3SYongqiang Yang if (!journal) 321be4f27d3SYongqiang Yang return ext4_get_nojournal(); 322be4f27d3SYongqiang Yang /* 323be4f27d3SYongqiang Yang * Special case here: if the journal has aborted behind our 324be4f27d3SYongqiang Yang * backs (eg. EIO in the commit thread), then we still need to 325be4f27d3SYongqiang Yang * take the FS itself readonly cleanly. 326be4f27d3SYongqiang Yang */ 327ac27a0ecSDave Kleikamp if (is_journal_aborted(journal)) { 328c67d859eSTheodore Ts'o ext4_abort(sb, "Detected aborted journal"); 329ac27a0ecSDave Kleikamp return ERR_PTR(-EROFS); 330ac27a0ecSDave Kleikamp } 331dab291afSMingming Cao return jbd2_journal_start(journal, nblocks); 332ac27a0ecSDave Kleikamp } 333ac27a0ecSDave Kleikamp 334ac27a0ecSDave Kleikamp /* 335ac27a0ecSDave Kleikamp * The only special thing we need to do here is to make sure that all 336dab291afSMingming Cao * jbd2_journal_stop calls result in the superblock being marked dirty, so 337ac27a0ecSDave Kleikamp * that sync() will call the filesystem's write_super callback if 338ac27a0ecSDave Kleikamp * appropriate. 339ac27a0ecSDave Kleikamp */ 340c398eda0STheodore Ts'o int __ext4_journal_stop(const char *where, unsigned int line, handle_t *handle) 341ac27a0ecSDave Kleikamp { 342ac27a0ecSDave Kleikamp struct super_block *sb; 343ac27a0ecSDave Kleikamp int err; 344ac27a0ecSDave Kleikamp int rc; 345ac27a0ecSDave Kleikamp 3460390131bSFrank Mayhar if (!ext4_handle_valid(handle)) { 347d3d1faf6SCurt Wohlgemuth ext4_put_nojournal(handle); 3480390131bSFrank Mayhar return 0; 3490390131bSFrank Mayhar } 350ac27a0ecSDave Kleikamp sb = handle->h_transaction->t_journal->j_private; 351ac27a0ecSDave Kleikamp err = handle->h_err; 352dab291afSMingming Cao rc = jbd2_journal_stop(handle); 353ac27a0ecSDave Kleikamp 354ac27a0ecSDave Kleikamp if (!err) 355ac27a0ecSDave Kleikamp err = rc; 356ac27a0ecSDave Kleikamp if (err) 357c398eda0STheodore Ts'o __ext4_std_error(sb, where, line, err); 358ac27a0ecSDave Kleikamp return err; 359ac27a0ecSDave Kleikamp } 360ac27a0ecSDave Kleikamp 36190c7201bSTheodore Ts'o void ext4_journal_abort_handle(const char *caller, unsigned int line, 36290c7201bSTheodore Ts'o const char *err_fn, struct buffer_head *bh, 36390c7201bSTheodore Ts'o handle_t *handle, int err) 364ac27a0ecSDave Kleikamp { 365ac27a0ecSDave Kleikamp char nbuf[16]; 366617ba13bSMingming Cao const char *errstr = ext4_decode_error(NULL, err, nbuf); 367ac27a0ecSDave Kleikamp 3680390131bSFrank Mayhar BUG_ON(!ext4_handle_valid(handle)); 3690390131bSFrank Mayhar 370ac27a0ecSDave Kleikamp if (bh) 371ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "abort"); 372ac27a0ecSDave Kleikamp 373ac27a0ecSDave Kleikamp if (!handle->h_err) 374ac27a0ecSDave Kleikamp handle->h_err = err; 375ac27a0ecSDave Kleikamp 376ac27a0ecSDave Kleikamp if (is_handle_aborted(handle)) 377ac27a0ecSDave Kleikamp return; 378ac27a0ecSDave Kleikamp 37990c7201bSTheodore Ts'o printk(KERN_ERR "%s:%d: aborting transaction: %s in %s\n", 38090c7201bSTheodore Ts'o caller, line, errstr, err_fn); 381ac27a0ecSDave Kleikamp 382dab291afSMingming Cao jbd2_journal_abort_handle(handle); 383ac27a0ecSDave Kleikamp } 384ac27a0ecSDave Kleikamp 3851c13d5c0STheodore Ts'o static void __save_error_info(struct super_block *sb, const char *func, 3861c13d5c0STheodore Ts'o unsigned int line) 3871c13d5c0STheodore Ts'o { 3881c13d5c0STheodore Ts'o struct ext4_super_block *es = EXT4_SB(sb)->s_es; 3891c13d5c0STheodore Ts'o 3901c13d5c0STheodore Ts'o EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS; 3911c13d5c0STheodore Ts'o es->s_state |= cpu_to_le16(EXT4_ERROR_FS); 3921c13d5c0STheodore Ts'o es->s_last_error_time = cpu_to_le32(get_seconds()); 3931c13d5c0STheodore Ts'o strncpy(es->s_last_error_func, func, sizeof(es->s_last_error_func)); 3941c13d5c0STheodore Ts'o es->s_last_error_line = cpu_to_le32(line); 3951c13d5c0STheodore Ts'o if (!es->s_first_error_time) { 3961c13d5c0STheodore Ts'o es->s_first_error_time = es->s_last_error_time; 3971c13d5c0STheodore Ts'o strncpy(es->s_first_error_func, func, 3981c13d5c0STheodore Ts'o sizeof(es->s_first_error_func)); 3991c13d5c0STheodore Ts'o es->s_first_error_line = cpu_to_le32(line); 4001c13d5c0STheodore Ts'o es->s_first_error_ino = es->s_last_error_ino; 4011c13d5c0STheodore Ts'o es->s_first_error_block = es->s_last_error_block; 4021c13d5c0STheodore Ts'o } 40366e61a9eSTheodore Ts'o /* 40466e61a9eSTheodore Ts'o * Start the daily error reporting function if it hasn't been 40566e61a9eSTheodore Ts'o * started already 40666e61a9eSTheodore Ts'o */ 40766e61a9eSTheodore Ts'o if (!es->s_error_count) 40866e61a9eSTheodore Ts'o mod_timer(&EXT4_SB(sb)->s_err_report, jiffies + 24*60*60*HZ); 4091c13d5c0STheodore Ts'o es->s_error_count = cpu_to_le32(le32_to_cpu(es->s_error_count) + 1); 4101c13d5c0STheodore Ts'o } 4111c13d5c0STheodore Ts'o 4121c13d5c0STheodore Ts'o static void save_error_info(struct super_block *sb, const char *func, 4131c13d5c0STheodore Ts'o unsigned int line) 4141c13d5c0STheodore Ts'o { 4151c13d5c0STheodore Ts'o __save_error_info(sb, func, line); 4161c13d5c0STheodore Ts'o ext4_commit_super(sb, 1); 4171c13d5c0STheodore Ts'o } 4181c13d5c0STheodore Ts'o 4197c2e7087STheodore Ts'o /* 4207c2e7087STheodore Ts'o * The del_gendisk() function uninitializes the disk-specific data 4217c2e7087STheodore Ts'o * structures, including the bdi structure, without telling anyone 4227c2e7087STheodore Ts'o * else. Once this happens, any attempt to call mark_buffer_dirty() 4237c2e7087STheodore Ts'o * (for example, by ext4_commit_super), will cause a kernel OOPS. 4247c2e7087STheodore Ts'o * This is a kludge to prevent these oops until we can put in a proper 4257c2e7087STheodore Ts'o * hook in del_gendisk() to inform the VFS and file system layers. 4267c2e7087STheodore Ts'o */ 4277c2e7087STheodore Ts'o static int block_device_ejected(struct super_block *sb) 4287c2e7087STheodore Ts'o { 4297c2e7087STheodore Ts'o struct inode *bd_inode = sb->s_bdev->bd_inode; 4307c2e7087STheodore Ts'o struct backing_dev_info *bdi = bd_inode->i_mapping->backing_dev_info; 4317c2e7087STheodore Ts'o 4327c2e7087STheodore Ts'o return bdi->dev == NULL; 4337c2e7087STheodore Ts'o } 4347c2e7087STheodore Ts'o 43518aadd47SBobi Jam static void ext4_journal_commit_callback(journal_t *journal, transaction_t *txn) 43618aadd47SBobi Jam { 43718aadd47SBobi Jam struct super_block *sb = journal->j_private; 43818aadd47SBobi Jam struct ext4_sb_info *sbi = EXT4_SB(sb); 43918aadd47SBobi Jam int error = is_journal_aborted(journal); 44018aadd47SBobi Jam struct ext4_journal_cb_entry *jce, *tmp; 44118aadd47SBobi Jam 44218aadd47SBobi Jam spin_lock(&sbi->s_md_lock); 44318aadd47SBobi Jam list_for_each_entry_safe(jce, tmp, &txn->t_private_list, jce_list) { 44418aadd47SBobi Jam list_del_init(&jce->jce_list); 44518aadd47SBobi Jam spin_unlock(&sbi->s_md_lock); 44618aadd47SBobi Jam jce->jce_func(sb, jce, error); 44718aadd47SBobi Jam spin_lock(&sbi->s_md_lock); 44818aadd47SBobi Jam } 44918aadd47SBobi Jam spin_unlock(&sbi->s_md_lock); 45018aadd47SBobi Jam } 4511c13d5c0STheodore Ts'o 452ac27a0ecSDave Kleikamp /* Deal with the reporting of failure conditions on a filesystem such as 453ac27a0ecSDave Kleikamp * inconsistencies detected or read IO failures. 454ac27a0ecSDave Kleikamp * 455ac27a0ecSDave Kleikamp * On ext2, we can store the error state of the filesystem in the 456617ba13bSMingming Cao * superblock. That is not possible on ext4, because we may have other 457ac27a0ecSDave Kleikamp * write ordering constraints on the superblock which prevent us from 458ac27a0ecSDave Kleikamp * writing it out straight away; and given that the journal is about to 459ac27a0ecSDave Kleikamp * be aborted, we can't rely on the current, or future, transactions to 460ac27a0ecSDave Kleikamp * write out the superblock safely. 461ac27a0ecSDave Kleikamp * 462dab291afSMingming Cao * We'll just use the jbd2_journal_abort() error code to record an error in 463d6b198bcSThadeu Lima de Souza Cascardo * the journal instead. On recovery, the journal will complain about 464ac27a0ecSDave Kleikamp * that error until we've noted it down and cleared it. 465ac27a0ecSDave Kleikamp */ 466ac27a0ecSDave Kleikamp 467617ba13bSMingming Cao static void ext4_handle_error(struct super_block *sb) 468ac27a0ecSDave Kleikamp { 469ac27a0ecSDave Kleikamp if (sb->s_flags & MS_RDONLY) 470ac27a0ecSDave Kleikamp return; 471ac27a0ecSDave Kleikamp 472ac27a0ecSDave Kleikamp if (!test_opt(sb, ERRORS_CONT)) { 473617ba13bSMingming Cao journal_t *journal = EXT4_SB(sb)->s_journal; 474ac27a0ecSDave Kleikamp 4754ab2f15bSTheodore Ts'o EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED; 476ac27a0ecSDave Kleikamp if (journal) 477dab291afSMingming Cao jbd2_journal_abort(journal, -EIO); 478ac27a0ecSDave Kleikamp } 479ac27a0ecSDave Kleikamp if (test_opt(sb, ERRORS_RO)) { 480b31e1552SEric Sandeen ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only"); 481ac27a0ecSDave Kleikamp sb->s_flags |= MS_RDONLY; 482ac27a0ecSDave Kleikamp } 483ac27a0ecSDave Kleikamp if (test_opt(sb, ERRORS_PANIC)) 484617ba13bSMingming Cao panic("EXT4-fs (device %s): panic forced after error\n", 485ac27a0ecSDave Kleikamp sb->s_id); 486ac27a0ecSDave Kleikamp } 487ac27a0ecSDave Kleikamp 48812062dddSEric Sandeen void __ext4_error(struct super_block *sb, const char *function, 489c398eda0STheodore Ts'o unsigned int line, const char *fmt, ...) 490ac27a0ecSDave Kleikamp { 4910ff2ea7dSJoe Perches struct va_format vaf; 492ac27a0ecSDave Kleikamp va_list args; 493ac27a0ecSDave Kleikamp 494ac27a0ecSDave Kleikamp va_start(args, fmt); 4950ff2ea7dSJoe Perches vaf.fmt = fmt; 4960ff2ea7dSJoe Perches vaf.va = &args; 4970ff2ea7dSJoe Perches printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n", 4980ff2ea7dSJoe Perches sb->s_id, function, line, current->comm, &vaf); 499ac27a0ecSDave Kleikamp va_end(args); 500ac27a0ecSDave Kleikamp 501617ba13bSMingming Cao ext4_handle_error(sb); 502ac27a0ecSDave Kleikamp } 503ac27a0ecSDave Kleikamp 504c398eda0STheodore Ts'o void ext4_error_inode(struct inode *inode, const char *function, 505c398eda0STheodore Ts'o unsigned int line, ext4_fsblk_t block, 506273df556SFrank Mayhar const char *fmt, ...) 507273df556SFrank Mayhar { 508273df556SFrank Mayhar va_list args; 509f7c21177STheodore Ts'o struct va_format vaf; 5101c13d5c0STheodore Ts'o struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es; 511273df556SFrank Mayhar 5121c13d5c0STheodore Ts'o es->s_last_error_ino = cpu_to_le32(inode->i_ino); 5131c13d5c0STheodore Ts'o es->s_last_error_block = cpu_to_le64(block); 5141c13d5c0STheodore Ts'o save_error_info(inode->i_sb, function, line); 515273df556SFrank Mayhar va_start(args, fmt); 516f7c21177STheodore Ts'o vaf.fmt = fmt; 517f7c21177STheodore Ts'o vaf.va = &args; 518c398eda0STheodore Ts'o printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: inode #%lu: ", 519c398eda0STheodore Ts'o inode->i_sb->s_id, function, line, inode->i_ino); 520c398eda0STheodore Ts'o if (block) 521f7c21177STheodore Ts'o printk(KERN_CONT "block %llu: ", block); 522f7c21177STheodore Ts'o printk(KERN_CONT "comm %s: %pV\n", current->comm, &vaf); 523273df556SFrank Mayhar va_end(args); 524273df556SFrank Mayhar 525273df556SFrank Mayhar ext4_handle_error(inode->i_sb); 526273df556SFrank Mayhar } 527273df556SFrank Mayhar 528c398eda0STheodore Ts'o void ext4_error_file(struct file *file, const char *function, 529f7c21177STheodore Ts'o unsigned int line, ext4_fsblk_t block, 530f7c21177STheodore Ts'o const char *fmt, ...) 531273df556SFrank Mayhar { 532273df556SFrank Mayhar va_list args; 533f7c21177STheodore Ts'o struct va_format vaf; 5341c13d5c0STheodore Ts'o struct ext4_super_block *es; 535273df556SFrank Mayhar struct inode *inode = file->f_dentry->d_inode; 536273df556SFrank Mayhar char pathname[80], *path; 537273df556SFrank Mayhar 5381c13d5c0STheodore Ts'o es = EXT4_SB(inode->i_sb)->s_es; 5391c13d5c0STheodore Ts'o es->s_last_error_ino = cpu_to_le32(inode->i_ino); 5401c13d5c0STheodore Ts'o save_error_info(inode->i_sb, function, line); 541273df556SFrank Mayhar path = d_path(&(file->f_path), pathname, sizeof(pathname)); 542f9a62d09SDan Carpenter if (IS_ERR(path)) 543273df556SFrank Mayhar path = "(unknown)"; 544273df556SFrank Mayhar printk(KERN_CRIT 545f7c21177STheodore Ts'o "EXT4-fs error (device %s): %s:%d: inode #%lu: ", 546f7c21177STheodore Ts'o inode->i_sb->s_id, function, line, inode->i_ino); 547f7c21177STheodore Ts'o if (block) 548f7c21177STheodore Ts'o printk(KERN_CONT "block %llu: ", block); 549f7c21177STheodore Ts'o va_start(args, fmt); 550f7c21177STheodore Ts'o vaf.fmt = fmt; 551f7c21177STheodore Ts'o vaf.va = &args; 552f7c21177STheodore Ts'o printk(KERN_CONT "comm %s: path %s: %pV\n", current->comm, path, &vaf); 553273df556SFrank Mayhar va_end(args); 554273df556SFrank Mayhar 555273df556SFrank Mayhar ext4_handle_error(inode->i_sb); 556273df556SFrank Mayhar } 557273df556SFrank Mayhar 558617ba13bSMingming Cao static const char *ext4_decode_error(struct super_block *sb, int errno, 559ac27a0ecSDave Kleikamp char nbuf[16]) 560ac27a0ecSDave Kleikamp { 561ac27a0ecSDave Kleikamp char *errstr = NULL; 562ac27a0ecSDave Kleikamp 563ac27a0ecSDave Kleikamp switch (errno) { 564ac27a0ecSDave Kleikamp case -EIO: 565ac27a0ecSDave Kleikamp errstr = "IO failure"; 566ac27a0ecSDave Kleikamp break; 567ac27a0ecSDave Kleikamp case -ENOMEM: 568ac27a0ecSDave Kleikamp errstr = "Out of memory"; 569ac27a0ecSDave Kleikamp break; 570ac27a0ecSDave Kleikamp case -EROFS: 57178f1ddbbSTheodore Ts'o if (!sb || (EXT4_SB(sb)->s_journal && 57278f1ddbbSTheodore Ts'o EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT)) 573ac27a0ecSDave Kleikamp errstr = "Journal has aborted"; 574ac27a0ecSDave Kleikamp else 575ac27a0ecSDave Kleikamp errstr = "Readonly filesystem"; 576ac27a0ecSDave Kleikamp break; 577ac27a0ecSDave Kleikamp default: 578ac27a0ecSDave Kleikamp /* If the caller passed in an extra buffer for unknown 579ac27a0ecSDave Kleikamp * errors, textualise them now. Else we just return 580ac27a0ecSDave Kleikamp * NULL. */ 581ac27a0ecSDave Kleikamp if (nbuf) { 582ac27a0ecSDave Kleikamp /* Check for truncated error codes... */ 583ac27a0ecSDave Kleikamp if (snprintf(nbuf, 16, "error %d", -errno) >= 0) 584ac27a0ecSDave Kleikamp errstr = nbuf; 585ac27a0ecSDave Kleikamp } 586ac27a0ecSDave Kleikamp break; 587ac27a0ecSDave Kleikamp } 588ac27a0ecSDave Kleikamp 589ac27a0ecSDave Kleikamp return errstr; 590ac27a0ecSDave Kleikamp } 591ac27a0ecSDave Kleikamp 592617ba13bSMingming Cao /* __ext4_std_error decodes expected errors from journaling functions 593ac27a0ecSDave Kleikamp * automatically and invokes the appropriate error response. */ 594ac27a0ecSDave Kleikamp 595c398eda0STheodore Ts'o void __ext4_std_error(struct super_block *sb, const char *function, 596c398eda0STheodore Ts'o unsigned int line, int errno) 597ac27a0ecSDave Kleikamp { 598ac27a0ecSDave Kleikamp char nbuf[16]; 599ac27a0ecSDave Kleikamp const char *errstr; 600ac27a0ecSDave Kleikamp 601ac27a0ecSDave Kleikamp /* Special case: if the error is EROFS, and we're not already 602ac27a0ecSDave Kleikamp * inside a transaction, then there's really no point in logging 603ac27a0ecSDave Kleikamp * an error. */ 604ac27a0ecSDave Kleikamp if (errno == -EROFS && journal_current_handle() == NULL && 605ac27a0ecSDave Kleikamp (sb->s_flags & MS_RDONLY)) 606ac27a0ecSDave Kleikamp return; 607ac27a0ecSDave Kleikamp 608617ba13bSMingming Cao errstr = ext4_decode_error(sb, errno, nbuf); 609c398eda0STheodore Ts'o printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n", 610c398eda0STheodore Ts'o sb->s_id, function, line, errstr); 6111c13d5c0STheodore Ts'o save_error_info(sb, function, line); 612ac27a0ecSDave Kleikamp 613617ba13bSMingming Cao ext4_handle_error(sb); 614ac27a0ecSDave Kleikamp } 615ac27a0ecSDave Kleikamp 616ac27a0ecSDave Kleikamp /* 617617ba13bSMingming Cao * ext4_abort is a much stronger failure handler than ext4_error. The 618ac27a0ecSDave Kleikamp * abort function may be used to deal with unrecoverable failures such 619ac27a0ecSDave Kleikamp * as journal IO errors or ENOMEM at a critical moment in log management. 620ac27a0ecSDave Kleikamp * 621ac27a0ecSDave Kleikamp * We unconditionally force the filesystem into an ABORT|READONLY state, 622ac27a0ecSDave Kleikamp * unless the error response on the fs has been set to panic in which 623ac27a0ecSDave Kleikamp * case we take the easy way out and panic immediately. 624ac27a0ecSDave Kleikamp */ 625ac27a0ecSDave Kleikamp 626c67d859eSTheodore Ts'o void __ext4_abort(struct super_block *sb, const char *function, 627c398eda0STheodore Ts'o unsigned int line, const char *fmt, ...) 628ac27a0ecSDave Kleikamp { 629ac27a0ecSDave Kleikamp va_list args; 630ac27a0ecSDave Kleikamp 6311c13d5c0STheodore Ts'o save_error_info(sb, function, line); 632ac27a0ecSDave Kleikamp va_start(args, fmt); 633c398eda0STheodore Ts'o printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: ", sb->s_id, 634c398eda0STheodore Ts'o function, line); 635ac27a0ecSDave Kleikamp vprintk(fmt, args); 636ac27a0ecSDave Kleikamp printk("\n"); 637ac27a0ecSDave Kleikamp va_end(args); 638ac27a0ecSDave Kleikamp 6391c13d5c0STheodore Ts'o if ((sb->s_flags & MS_RDONLY) == 0) { 640b31e1552SEric Sandeen ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only"); 641ac27a0ecSDave Kleikamp sb->s_flags |= MS_RDONLY; 6424ab2f15bSTheodore Ts'o EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED; 643ef2cabf7SHidehiro Kawai if (EXT4_SB(sb)->s_journal) 644dab291afSMingming Cao jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO); 6451c13d5c0STheodore Ts'o save_error_info(sb, function, line); 6461c13d5c0STheodore Ts'o } 6471c13d5c0STheodore Ts'o if (test_opt(sb, ERRORS_PANIC)) 6481c13d5c0STheodore Ts'o panic("EXT4-fs panic from previous error\n"); 649ac27a0ecSDave Kleikamp } 650ac27a0ecSDave Kleikamp 6510ff2ea7dSJoe Perches void ext4_msg(struct super_block *sb, const char *prefix, const char *fmt, ...) 652b31e1552SEric Sandeen { 6530ff2ea7dSJoe Perches struct va_format vaf; 654b31e1552SEric Sandeen va_list args; 655b31e1552SEric Sandeen 656b31e1552SEric Sandeen va_start(args, fmt); 6570ff2ea7dSJoe Perches vaf.fmt = fmt; 6580ff2ea7dSJoe Perches vaf.va = &args; 6590ff2ea7dSJoe Perches printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf); 660b31e1552SEric Sandeen va_end(args); 661b31e1552SEric Sandeen } 662b31e1552SEric Sandeen 66312062dddSEric Sandeen void __ext4_warning(struct super_block *sb, const char *function, 664c398eda0STheodore Ts'o unsigned int line, const char *fmt, ...) 665ac27a0ecSDave Kleikamp { 6660ff2ea7dSJoe Perches struct va_format vaf; 667ac27a0ecSDave Kleikamp va_list args; 668ac27a0ecSDave Kleikamp 669ac27a0ecSDave Kleikamp va_start(args, fmt); 6700ff2ea7dSJoe Perches vaf.fmt = fmt; 6710ff2ea7dSJoe Perches vaf.va = &args; 6720ff2ea7dSJoe Perches printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n", 6730ff2ea7dSJoe Perches sb->s_id, function, line, &vaf); 674ac27a0ecSDave Kleikamp va_end(args); 675ac27a0ecSDave Kleikamp } 676ac27a0ecSDave Kleikamp 677e29136f8STheodore Ts'o void __ext4_grp_locked_error(const char *function, unsigned int line, 678e29136f8STheodore Ts'o struct super_block *sb, ext4_group_t grp, 679e29136f8STheodore Ts'o unsigned long ino, ext4_fsblk_t block, 680e29136f8STheodore Ts'o const char *fmt, ...) 6815d1b1b3fSAneesh Kumar K.V __releases(bitlock) 6825d1b1b3fSAneesh Kumar K.V __acquires(bitlock) 6835d1b1b3fSAneesh Kumar K.V { 6840ff2ea7dSJoe Perches struct va_format vaf; 6855d1b1b3fSAneesh Kumar K.V va_list args; 6865d1b1b3fSAneesh Kumar K.V struct ext4_super_block *es = EXT4_SB(sb)->s_es; 6875d1b1b3fSAneesh Kumar K.V 6881c13d5c0STheodore Ts'o es->s_last_error_ino = cpu_to_le32(ino); 6891c13d5c0STheodore Ts'o es->s_last_error_block = cpu_to_le64(block); 6901c13d5c0STheodore Ts'o __save_error_info(sb, function, line); 6910ff2ea7dSJoe Perches 6925d1b1b3fSAneesh Kumar K.V va_start(args, fmt); 6930ff2ea7dSJoe Perches 6940ff2ea7dSJoe Perches vaf.fmt = fmt; 6950ff2ea7dSJoe Perches vaf.va = &args; 69621149d61SRobin Dong printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ", 697e29136f8STheodore Ts'o sb->s_id, function, line, grp); 698e29136f8STheodore Ts'o if (ino) 6990ff2ea7dSJoe Perches printk(KERN_CONT "inode %lu: ", ino); 700e29136f8STheodore Ts'o if (block) 7010ff2ea7dSJoe Perches printk(KERN_CONT "block %llu:", (unsigned long long) block); 7020ff2ea7dSJoe Perches printk(KERN_CONT "%pV\n", &vaf); 7035d1b1b3fSAneesh Kumar K.V va_end(args); 7045d1b1b3fSAneesh Kumar K.V 7055d1b1b3fSAneesh Kumar K.V if (test_opt(sb, ERRORS_CONT)) { 706e2d67052STheodore Ts'o ext4_commit_super(sb, 0); 7075d1b1b3fSAneesh Kumar K.V return; 7085d1b1b3fSAneesh Kumar K.V } 7091c13d5c0STheodore Ts'o 7105d1b1b3fSAneesh Kumar K.V ext4_unlock_group(sb, grp); 7115d1b1b3fSAneesh Kumar K.V ext4_handle_error(sb); 7125d1b1b3fSAneesh Kumar K.V /* 7135d1b1b3fSAneesh Kumar K.V * We only get here in the ERRORS_RO case; relocking the group 7145d1b1b3fSAneesh Kumar K.V * may be dangerous, but nothing bad will happen since the 7155d1b1b3fSAneesh Kumar K.V * filesystem will have already been marked read/only and the 7165d1b1b3fSAneesh Kumar K.V * journal has been aborted. We return 1 as a hint to callers 7175d1b1b3fSAneesh Kumar K.V * who might what to use the return value from 71825985edcSLucas De Marchi * ext4_grp_locked_error() to distinguish between the 7195d1b1b3fSAneesh Kumar K.V * ERRORS_CONT and ERRORS_RO case, and perhaps return more 7205d1b1b3fSAneesh Kumar K.V * aggressively from the ext4 function in question, with a 7215d1b1b3fSAneesh Kumar K.V * more appropriate error code. 7225d1b1b3fSAneesh Kumar K.V */ 7235d1b1b3fSAneesh Kumar K.V ext4_lock_group(sb, grp); 7245d1b1b3fSAneesh Kumar K.V return; 7255d1b1b3fSAneesh Kumar K.V } 7265d1b1b3fSAneesh Kumar K.V 727617ba13bSMingming Cao void ext4_update_dynamic_rev(struct super_block *sb) 728ac27a0ecSDave Kleikamp { 729617ba13bSMingming Cao struct ext4_super_block *es = EXT4_SB(sb)->s_es; 730ac27a0ecSDave Kleikamp 731617ba13bSMingming Cao if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV) 732ac27a0ecSDave Kleikamp return; 733ac27a0ecSDave Kleikamp 73412062dddSEric Sandeen ext4_warning(sb, 735ac27a0ecSDave Kleikamp "updating to rev %d because of new feature flag, " 736ac27a0ecSDave Kleikamp "running e2fsck is recommended", 737617ba13bSMingming Cao EXT4_DYNAMIC_REV); 738ac27a0ecSDave Kleikamp 739617ba13bSMingming Cao es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO); 740617ba13bSMingming Cao es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE); 741617ba13bSMingming Cao es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV); 742ac27a0ecSDave Kleikamp /* leave es->s_feature_*compat flags alone */ 743ac27a0ecSDave Kleikamp /* es->s_uuid will be set by e2fsck if empty */ 744ac27a0ecSDave Kleikamp 745ac27a0ecSDave Kleikamp /* 746ac27a0ecSDave Kleikamp * The rest of the superblock fields should be zero, and if not it 747ac27a0ecSDave Kleikamp * means they are likely already in use, so leave them alone. We 748ac27a0ecSDave Kleikamp * can leave it up to e2fsck to clean up any inconsistencies there. 749ac27a0ecSDave Kleikamp */ 750ac27a0ecSDave Kleikamp } 751ac27a0ecSDave Kleikamp 752ac27a0ecSDave Kleikamp /* 753ac27a0ecSDave Kleikamp * Open the external journal device 754ac27a0ecSDave Kleikamp */ 755b31e1552SEric Sandeen static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb) 756ac27a0ecSDave Kleikamp { 757ac27a0ecSDave Kleikamp struct block_device *bdev; 758ac27a0ecSDave Kleikamp char b[BDEVNAME_SIZE]; 759ac27a0ecSDave Kleikamp 760d4d77629STejun Heo bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb); 761ac27a0ecSDave Kleikamp if (IS_ERR(bdev)) 762ac27a0ecSDave Kleikamp goto fail; 763ac27a0ecSDave Kleikamp return bdev; 764ac27a0ecSDave Kleikamp 765ac27a0ecSDave Kleikamp fail: 766b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "failed to open journal device %s: %ld", 767ac27a0ecSDave Kleikamp __bdevname(dev, b), PTR_ERR(bdev)); 768ac27a0ecSDave Kleikamp return NULL; 769ac27a0ecSDave Kleikamp } 770ac27a0ecSDave Kleikamp 771ac27a0ecSDave Kleikamp /* 772ac27a0ecSDave Kleikamp * Release the journal device 773ac27a0ecSDave Kleikamp */ 774617ba13bSMingming Cao static int ext4_blkdev_put(struct block_device *bdev) 775ac27a0ecSDave Kleikamp { 776e525fd89STejun Heo return blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); 777ac27a0ecSDave Kleikamp } 778ac27a0ecSDave Kleikamp 779617ba13bSMingming Cao static int ext4_blkdev_remove(struct ext4_sb_info *sbi) 780ac27a0ecSDave Kleikamp { 781ac27a0ecSDave Kleikamp struct block_device *bdev; 782ac27a0ecSDave Kleikamp int ret = -ENODEV; 783ac27a0ecSDave Kleikamp 784ac27a0ecSDave Kleikamp bdev = sbi->journal_bdev; 785ac27a0ecSDave Kleikamp if (bdev) { 786617ba13bSMingming Cao ret = ext4_blkdev_put(bdev); 787ac27a0ecSDave Kleikamp sbi->journal_bdev = NULL; 788ac27a0ecSDave Kleikamp } 789ac27a0ecSDave Kleikamp return ret; 790ac27a0ecSDave Kleikamp } 791ac27a0ecSDave Kleikamp 792ac27a0ecSDave Kleikamp static inline struct inode *orphan_list_entry(struct list_head *l) 793ac27a0ecSDave Kleikamp { 794617ba13bSMingming Cao return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode; 795ac27a0ecSDave Kleikamp } 796ac27a0ecSDave Kleikamp 797617ba13bSMingming Cao static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi) 798ac27a0ecSDave Kleikamp { 799ac27a0ecSDave Kleikamp struct list_head *l; 800ac27a0ecSDave Kleikamp 801b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "sb orphan head is %d", 802ac27a0ecSDave Kleikamp le32_to_cpu(sbi->s_es->s_last_orphan)); 803ac27a0ecSDave Kleikamp 804ac27a0ecSDave Kleikamp printk(KERN_ERR "sb_info orphan list:\n"); 805ac27a0ecSDave Kleikamp list_for_each(l, &sbi->s_orphan) { 806ac27a0ecSDave Kleikamp struct inode *inode = orphan_list_entry(l); 807ac27a0ecSDave Kleikamp printk(KERN_ERR " " 808ac27a0ecSDave Kleikamp "inode %s:%lu at %p: mode %o, nlink %d, next %d\n", 809ac27a0ecSDave Kleikamp inode->i_sb->s_id, inode->i_ino, inode, 810ac27a0ecSDave Kleikamp inode->i_mode, inode->i_nlink, 811ac27a0ecSDave Kleikamp NEXT_ORPHAN(inode)); 812ac27a0ecSDave Kleikamp } 813ac27a0ecSDave Kleikamp } 814ac27a0ecSDave Kleikamp 815617ba13bSMingming Cao static void ext4_put_super(struct super_block *sb) 816ac27a0ecSDave Kleikamp { 817617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 818617ba13bSMingming Cao struct ext4_super_block *es = sbi->s_es; 819ef2cabf7SHidehiro Kawai int i, err; 820ac27a0ecSDave Kleikamp 821857ac889SLukas Czerner ext4_unregister_li_request(sb); 822e0ccfd95SChristoph Hellwig dquot_disable(sb, -1, DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED); 823e0ccfd95SChristoph Hellwig 8244c0425ffSMingming Cao flush_workqueue(sbi->dio_unwritten_wq); 8254c0425ffSMingming Cao destroy_workqueue(sbi->dio_unwritten_wq); 8264c0425ffSMingming Cao 827a9e220f8SAl Viro lock_super(sb); 8288c85e125SChristoph Hellwig if (sb->s_dirt) 829ebc1ac16SChristoph Hellwig ext4_commit_super(sb, 1); 8308c85e125SChristoph Hellwig 8310390131bSFrank Mayhar if (sbi->s_journal) { 832ef2cabf7SHidehiro Kawai err = jbd2_journal_destroy(sbi->s_journal); 83347b4a50bSJan Kara sbi->s_journal = NULL; 834ef2cabf7SHidehiro Kawai if (err < 0) 835c67d859eSTheodore Ts'o ext4_abort(sb, "Couldn't clean up the journal"); 8360390131bSFrank Mayhar } 837d4edac31SJosef Bacik 838a1c6c569SSergey Senozhatsky del_timer(&sbi->s_err_report); 839d4edac31SJosef Bacik ext4_release_system_zone(sb); 840d4edac31SJosef Bacik ext4_mb_release(sb); 841d4edac31SJosef Bacik ext4_ext_release(sb); 842d4edac31SJosef Bacik ext4_xattr_put_super(sb); 843d4edac31SJosef Bacik 844ac27a0ecSDave Kleikamp if (!(sb->s_flags & MS_RDONLY)) { 845617ba13bSMingming Cao EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER); 846ac27a0ecSDave Kleikamp es->s_state = cpu_to_le16(sbi->s_mount_state); 847e2d67052STheodore Ts'o ext4_commit_super(sb, 1); 848ac27a0ecSDave Kleikamp } 849240799cdSTheodore Ts'o if (sbi->s_proc) { 85066acdcf4STheodore Ts'o remove_proc_entry("options", sbi->s_proc); 8519f6200bbSTheodore Ts'o remove_proc_entry(sb->s_id, ext4_proc_root); 852240799cdSTheodore Ts'o } 8533197ebdbSTheodore Ts'o kobject_del(&sbi->s_kobj); 854ac27a0ecSDave Kleikamp 855ac27a0ecSDave Kleikamp for (i = 0; i < sbi->s_gdb_count; i++) 856ac27a0ecSDave Kleikamp brelse(sbi->s_group_desc[i]); 857f18a5f21STheodore Ts'o ext4_kvfree(sbi->s_group_desc); 8589933fc0aSTheodore Ts'o ext4_kvfree(sbi->s_flex_groups); 85957042651STheodore Ts'o percpu_counter_destroy(&sbi->s_freeclusters_counter); 860ac27a0ecSDave Kleikamp percpu_counter_destroy(&sbi->s_freeinodes_counter); 861ac27a0ecSDave Kleikamp percpu_counter_destroy(&sbi->s_dirs_counter); 86257042651STheodore Ts'o percpu_counter_destroy(&sbi->s_dirtyclusters_counter); 863ac27a0ecSDave Kleikamp brelse(sbi->s_sbh); 864ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 865ac27a0ecSDave Kleikamp for (i = 0; i < MAXQUOTAS; i++) 866ac27a0ecSDave Kleikamp kfree(sbi->s_qf_names[i]); 867ac27a0ecSDave Kleikamp #endif 868ac27a0ecSDave Kleikamp 869ac27a0ecSDave Kleikamp /* Debugging code just in case the in-memory inode orphan list 870ac27a0ecSDave Kleikamp * isn't empty. The on-disk one can be non-empty if we've 871ac27a0ecSDave Kleikamp * detected an error and taken the fs readonly, but the 872ac27a0ecSDave Kleikamp * in-memory list had better be clean by this point. */ 873ac27a0ecSDave Kleikamp if (!list_empty(&sbi->s_orphan)) 874ac27a0ecSDave Kleikamp dump_orphan_list(sb, sbi); 875ac27a0ecSDave Kleikamp J_ASSERT(list_empty(&sbi->s_orphan)); 876ac27a0ecSDave Kleikamp 877f98393a6SPeter Zijlstra invalidate_bdev(sb->s_bdev); 878ac27a0ecSDave Kleikamp if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) { 879ac27a0ecSDave Kleikamp /* 880ac27a0ecSDave Kleikamp * Invalidate the journal device's buffers. We don't want them 881ac27a0ecSDave Kleikamp * floating about in memory - the physical journal device may 882ac27a0ecSDave Kleikamp * hotswapped, and it breaks the `ro-after' testing code. 883ac27a0ecSDave Kleikamp */ 884ac27a0ecSDave Kleikamp sync_blockdev(sbi->journal_bdev); 885f98393a6SPeter Zijlstra invalidate_bdev(sbi->journal_bdev); 886617ba13bSMingming Cao ext4_blkdev_remove(sbi); 887ac27a0ecSDave Kleikamp } 888c5e06d10SJohann Lombardi if (sbi->s_mmp_tsk) 889c5e06d10SJohann Lombardi kthread_stop(sbi->s_mmp_tsk); 890ac27a0ecSDave Kleikamp sb->s_fs_info = NULL; 8913197ebdbSTheodore Ts'o /* 8923197ebdbSTheodore Ts'o * Now that we are completely done shutting down the 8933197ebdbSTheodore Ts'o * superblock, we need to actually destroy the kobject. 8943197ebdbSTheodore Ts'o */ 8953197ebdbSTheodore Ts'o unlock_super(sb); 8963197ebdbSTheodore Ts'o kobject_put(&sbi->s_kobj); 8973197ebdbSTheodore Ts'o wait_for_completion(&sbi->s_kobj_unregister); 898705895b6SPekka Enberg kfree(sbi->s_blockgroup_lock); 899ac27a0ecSDave Kleikamp kfree(sbi); 900ac27a0ecSDave Kleikamp } 901ac27a0ecSDave Kleikamp 902e18b890bSChristoph Lameter static struct kmem_cache *ext4_inode_cachep; 903ac27a0ecSDave Kleikamp 904ac27a0ecSDave Kleikamp /* 905ac27a0ecSDave Kleikamp * Called inside transaction, so use GFP_NOFS 906ac27a0ecSDave Kleikamp */ 907617ba13bSMingming Cao static struct inode *ext4_alloc_inode(struct super_block *sb) 908ac27a0ecSDave Kleikamp { 909617ba13bSMingming Cao struct ext4_inode_info *ei; 910ac27a0ecSDave Kleikamp 911e6b4f8daSChristoph Lameter ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS); 912ac27a0ecSDave Kleikamp if (!ei) 913ac27a0ecSDave Kleikamp return NULL; 9140b8e58a1SAndreas Dilger 915ac27a0ecSDave Kleikamp ei->vfs_inode.i_version = 1; 91691246c00SAneesh Kumar K.V ei->vfs_inode.i_data.writeback_index = 0; 917a86c6181SAlex Tomas memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache)); 918c9de560dSAlex Tomas INIT_LIST_HEAD(&ei->i_prealloc_list); 919c9de560dSAlex Tomas spin_lock_init(&ei->i_prealloc_lock); 920d2a17637SMingming Cao ei->i_reserved_data_blocks = 0; 921d2a17637SMingming Cao ei->i_reserved_meta_blocks = 0; 922d2a17637SMingming Cao ei->i_allocated_meta_blocks = 0; 9239d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 0; 924d2a17637SMingming Cao spin_lock_init(&(ei->i_block_reservation_lock)); 925a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 926a9e7f447SDmitry Monakhov ei->i_reserved_quota = 0; 927a9e7f447SDmitry Monakhov #endif 9288aefcd55STheodore Ts'o ei->jinode = NULL; 929c7064ef1SJiaying Zhang INIT_LIST_HEAD(&ei->i_completed_io_list); 930744692dcSJiaying Zhang spin_lock_init(&ei->i_completed_io_lock); 9318d5d02e6SMingming Cao ei->cur_aio_dio = NULL; 932b436b9beSJan Kara ei->i_sync_tid = 0; 933b436b9beSJan Kara ei->i_datasync_tid = 0; 934f7ad6d2eSTheodore Ts'o atomic_set(&ei->i_ioend_count, 0); 935e9e3bcecSEric Sandeen atomic_set(&ei->i_aiodio_unwritten, 0); 9360b8e58a1SAndreas Dilger 937ac27a0ecSDave Kleikamp return &ei->vfs_inode; 938ac27a0ecSDave Kleikamp } 939ac27a0ecSDave Kleikamp 9407ff9c073STheodore Ts'o static int ext4_drop_inode(struct inode *inode) 9417ff9c073STheodore Ts'o { 9427ff9c073STheodore Ts'o int drop = generic_drop_inode(inode); 9437ff9c073STheodore Ts'o 9447ff9c073STheodore Ts'o trace_ext4_drop_inode(inode, drop); 9457ff9c073STheodore Ts'o return drop; 9467ff9c073STheodore Ts'o } 9477ff9c073STheodore Ts'o 948fa0d7e3dSNick Piggin static void ext4_i_callback(struct rcu_head *head) 949fa0d7e3dSNick Piggin { 950fa0d7e3dSNick Piggin struct inode *inode = container_of(head, struct inode, i_rcu); 951fa0d7e3dSNick Piggin kmem_cache_free(ext4_inode_cachep, EXT4_I(inode)); 952fa0d7e3dSNick Piggin } 953fa0d7e3dSNick Piggin 954617ba13bSMingming Cao static void ext4_destroy_inode(struct inode *inode) 955ac27a0ecSDave Kleikamp { 9569f7dd93dSVasily Averin if (!list_empty(&(EXT4_I(inode)->i_orphan))) { 957b31e1552SEric Sandeen ext4_msg(inode->i_sb, KERN_ERR, 958b31e1552SEric Sandeen "Inode %lu (%p): orphan list check failed!", 959b31e1552SEric Sandeen inode->i_ino, EXT4_I(inode)); 9609f7dd93dSVasily Averin print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4, 9619f7dd93dSVasily Averin EXT4_I(inode), sizeof(struct ext4_inode_info), 9629f7dd93dSVasily Averin true); 9639f7dd93dSVasily Averin dump_stack(); 9649f7dd93dSVasily Averin } 965fa0d7e3dSNick Piggin call_rcu(&inode->i_rcu, ext4_i_callback); 966ac27a0ecSDave Kleikamp } 967ac27a0ecSDave Kleikamp 96851cc5068SAlexey Dobriyan static void init_once(void *foo) 969ac27a0ecSDave Kleikamp { 970617ba13bSMingming Cao struct ext4_inode_info *ei = (struct ext4_inode_info *) foo; 971ac27a0ecSDave Kleikamp 972ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&ei->i_orphan); 97303010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR 974ac27a0ecSDave Kleikamp init_rwsem(&ei->xattr_sem); 975ac27a0ecSDave Kleikamp #endif 9760e855ac8SAneesh Kumar K.V init_rwsem(&ei->i_data_sem); 977ac27a0ecSDave Kleikamp inode_init_once(&ei->vfs_inode); 978ac27a0ecSDave Kleikamp } 979ac27a0ecSDave Kleikamp 980ac27a0ecSDave Kleikamp static int init_inodecache(void) 981ac27a0ecSDave Kleikamp { 982617ba13bSMingming Cao ext4_inode_cachep = kmem_cache_create("ext4_inode_cache", 983617ba13bSMingming Cao sizeof(struct ext4_inode_info), 984ac27a0ecSDave Kleikamp 0, (SLAB_RECLAIM_ACCOUNT| 985ac27a0ecSDave Kleikamp SLAB_MEM_SPREAD), 98620c2df83SPaul Mundt init_once); 987617ba13bSMingming Cao if (ext4_inode_cachep == NULL) 988ac27a0ecSDave Kleikamp return -ENOMEM; 989ac27a0ecSDave Kleikamp return 0; 990ac27a0ecSDave Kleikamp } 991ac27a0ecSDave Kleikamp 992ac27a0ecSDave Kleikamp static void destroy_inodecache(void) 993ac27a0ecSDave Kleikamp { 994617ba13bSMingming Cao kmem_cache_destroy(ext4_inode_cachep); 995ac27a0ecSDave Kleikamp } 996ac27a0ecSDave Kleikamp 9970930fcc1SAl Viro void ext4_clear_inode(struct inode *inode) 998ac27a0ecSDave Kleikamp { 9990930fcc1SAl Viro invalidate_inode_buffers(inode); 10000930fcc1SAl Viro end_writeback(inode); 10019f754758SChristoph Hellwig dquot_drop(inode); 1002c2ea3fdeSTheodore Ts'o ext4_discard_preallocations(inode); 10038aefcd55STheodore Ts'o if (EXT4_I(inode)->jinode) { 10048aefcd55STheodore Ts'o jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode), 10058aefcd55STheodore Ts'o EXT4_I(inode)->jinode); 10068aefcd55STheodore Ts'o jbd2_free_inode(EXT4_I(inode)->jinode); 10078aefcd55STheodore Ts'o EXT4_I(inode)->jinode = NULL; 10088aefcd55STheodore Ts'o } 1009ac27a0ecSDave Kleikamp } 1010ac27a0ecSDave Kleikamp 10111b961ac0SChristoph Hellwig static struct inode *ext4_nfs_get_inode(struct super_block *sb, 10121b961ac0SChristoph Hellwig u64 ino, u32 generation) 1013ac27a0ecSDave Kleikamp { 1014ac27a0ecSDave Kleikamp struct inode *inode; 1015ac27a0ecSDave Kleikamp 1016617ba13bSMingming Cao if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO) 1017ac27a0ecSDave Kleikamp return ERR_PTR(-ESTALE); 1018617ba13bSMingming Cao if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count)) 1019ac27a0ecSDave Kleikamp return ERR_PTR(-ESTALE); 1020ac27a0ecSDave Kleikamp 1021ac27a0ecSDave Kleikamp /* iget isn't really right if the inode is currently unallocated!! 1022ac27a0ecSDave Kleikamp * 1023617ba13bSMingming Cao * ext4_read_inode will return a bad_inode if the inode had been 1024ac27a0ecSDave Kleikamp * deleted, so we should be safe. 1025ac27a0ecSDave Kleikamp * 1026ac27a0ecSDave Kleikamp * Currently we don't know the generation for parent directory, so 1027ac27a0ecSDave Kleikamp * a generation of 0 means "accept any" 1028ac27a0ecSDave Kleikamp */ 10291d1fe1eeSDavid Howells inode = ext4_iget(sb, ino); 10301d1fe1eeSDavid Howells if (IS_ERR(inode)) 10311d1fe1eeSDavid Howells return ERR_CAST(inode); 10321d1fe1eeSDavid Howells if (generation && inode->i_generation != generation) { 1033ac27a0ecSDave Kleikamp iput(inode); 1034ac27a0ecSDave Kleikamp return ERR_PTR(-ESTALE); 1035ac27a0ecSDave Kleikamp } 10361b961ac0SChristoph Hellwig 10371b961ac0SChristoph Hellwig return inode; 1038ac27a0ecSDave Kleikamp } 10391b961ac0SChristoph Hellwig 10401b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid, 10411b961ac0SChristoph Hellwig int fh_len, int fh_type) 10421b961ac0SChristoph Hellwig { 10431b961ac0SChristoph Hellwig return generic_fh_to_dentry(sb, fid, fh_len, fh_type, 10441b961ac0SChristoph Hellwig ext4_nfs_get_inode); 10451b961ac0SChristoph Hellwig } 10461b961ac0SChristoph Hellwig 10471b961ac0SChristoph Hellwig static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid, 10481b961ac0SChristoph Hellwig int fh_len, int fh_type) 10491b961ac0SChristoph Hellwig { 10501b961ac0SChristoph Hellwig return generic_fh_to_parent(sb, fid, fh_len, fh_type, 10511b961ac0SChristoph Hellwig ext4_nfs_get_inode); 1052ac27a0ecSDave Kleikamp } 1053ac27a0ecSDave Kleikamp 1054c39a7f84SToshiyuki Okajima /* 1055c39a7f84SToshiyuki Okajima * Try to release metadata pages (indirect blocks, directories) which are 1056c39a7f84SToshiyuki Okajima * mapped via the block device. Since these pages could have journal heads 1057c39a7f84SToshiyuki Okajima * which would prevent try_to_free_buffers() from freeing them, we must use 1058c39a7f84SToshiyuki Okajima * jbd2 layer's try_to_free_buffers() function to release them. 1059c39a7f84SToshiyuki Okajima */ 10600b8e58a1SAndreas Dilger static int bdev_try_to_free_page(struct super_block *sb, struct page *page, 10610b8e58a1SAndreas Dilger gfp_t wait) 1062c39a7f84SToshiyuki Okajima { 1063c39a7f84SToshiyuki Okajima journal_t *journal = EXT4_SB(sb)->s_journal; 1064c39a7f84SToshiyuki Okajima 1065c39a7f84SToshiyuki Okajima WARN_ON(PageChecked(page)); 1066c39a7f84SToshiyuki Okajima if (!page_has_buffers(page)) 1067c39a7f84SToshiyuki Okajima return 0; 1068c39a7f84SToshiyuki Okajima if (journal) 1069c39a7f84SToshiyuki Okajima return jbd2_journal_try_to_free_buffers(journal, page, 1070c39a7f84SToshiyuki Okajima wait & ~__GFP_WAIT); 1071c39a7f84SToshiyuki Okajima return try_to_free_buffers(page); 1072c39a7f84SToshiyuki Okajima } 1073c39a7f84SToshiyuki Okajima 1074ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 1075ac27a0ecSDave Kleikamp #define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group") 1076ac27a0ecSDave Kleikamp #define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA)) 1077ac27a0ecSDave Kleikamp 1078617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot); 1079617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot); 1080617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot); 1081617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot); 1082617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type); 10836f28e087SJan Kara static int ext4_quota_on(struct super_block *sb, int type, int format_id, 1084f00c9e44SJan Kara struct path *path); 1085ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type); 1086617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type); 1087617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data, 1088ac27a0ecSDave Kleikamp size_t len, loff_t off); 1089617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type, 1090ac27a0ecSDave Kleikamp const char *data, size_t len, loff_t off); 1091ac27a0ecSDave Kleikamp 109261e225dcSAlexey Dobriyan static const struct dquot_operations ext4_quota_operations = { 109360e58e0fSMingming Cao .get_reserved_space = ext4_get_reserved_space, 1094617ba13bSMingming Cao .write_dquot = ext4_write_dquot, 1095617ba13bSMingming Cao .acquire_dquot = ext4_acquire_dquot, 1096617ba13bSMingming Cao .release_dquot = ext4_release_dquot, 1097617ba13bSMingming Cao .mark_dirty = ext4_mark_dquot_dirty, 1098a5b5ee32SJan Kara .write_info = ext4_write_info, 1099a5b5ee32SJan Kara .alloc_dquot = dquot_alloc, 1100a5b5ee32SJan Kara .destroy_dquot = dquot_destroy, 1101ac27a0ecSDave Kleikamp }; 1102ac27a0ecSDave Kleikamp 11030d54b217SAlexey Dobriyan static const struct quotactl_ops ext4_qctl_operations = { 1104617ba13bSMingming Cao .quota_on = ext4_quota_on, 1105ca0e05e4SDmitry Monakhov .quota_off = ext4_quota_off, 1106287a8095SChristoph Hellwig .quota_sync = dquot_quota_sync, 1107287a8095SChristoph Hellwig .get_info = dquot_get_dqinfo, 1108287a8095SChristoph Hellwig .set_info = dquot_set_dqinfo, 1109287a8095SChristoph Hellwig .get_dqblk = dquot_get_dqblk, 1110287a8095SChristoph Hellwig .set_dqblk = dquot_set_dqblk 1111ac27a0ecSDave Kleikamp }; 1112ac27a0ecSDave Kleikamp #endif 1113ac27a0ecSDave Kleikamp 1114ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations ext4_sops = { 1115617ba13bSMingming Cao .alloc_inode = ext4_alloc_inode, 1116617ba13bSMingming Cao .destroy_inode = ext4_destroy_inode, 1117617ba13bSMingming Cao .write_inode = ext4_write_inode, 1118617ba13bSMingming Cao .dirty_inode = ext4_dirty_inode, 11197ff9c073STheodore Ts'o .drop_inode = ext4_drop_inode, 11200930fcc1SAl Viro .evict_inode = ext4_evict_inode, 1121617ba13bSMingming Cao .put_super = ext4_put_super, 1122617ba13bSMingming Cao .sync_fs = ext4_sync_fs, 1123c4be0c1dSTakashi Sato .freeze_fs = ext4_freeze, 1124c4be0c1dSTakashi Sato .unfreeze_fs = ext4_unfreeze, 1125617ba13bSMingming Cao .statfs = ext4_statfs, 1126617ba13bSMingming Cao .remount_fs = ext4_remount, 1127617ba13bSMingming Cao .show_options = ext4_show_options, 1128ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 1129617ba13bSMingming Cao .quota_read = ext4_quota_read, 1130617ba13bSMingming Cao .quota_write = ext4_quota_write, 1131ac27a0ecSDave Kleikamp #endif 1132c39a7f84SToshiyuki Okajima .bdev_try_to_free_page = bdev_try_to_free_page, 1133ac27a0ecSDave Kleikamp }; 1134ac27a0ecSDave Kleikamp 11359ca92389STheodore Ts'o static const struct super_operations ext4_nojournal_sops = { 11369ca92389STheodore Ts'o .alloc_inode = ext4_alloc_inode, 11379ca92389STheodore Ts'o .destroy_inode = ext4_destroy_inode, 11389ca92389STheodore Ts'o .write_inode = ext4_write_inode, 11399ca92389STheodore Ts'o .dirty_inode = ext4_dirty_inode, 11407ff9c073STheodore Ts'o .drop_inode = ext4_drop_inode, 11410930fcc1SAl Viro .evict_inode = ext4_evict_inode, 11429ca92389STheodore Ts'o .write_super = ext4_write_super, 11439ca92389STheodore Ts'o .put_super = ext4_put_super, 11449ca92389STheodore Ts'o .statfs = ext4_statfs, 11459ca92389STheodore Ts'o .remount_fs = ext4_remount, 11469ca92389STheodore Ts'o .show_options = ext4_show_options, 11479ca92389STheodore Ts'o #ifdef CONFIG_QUOTA 11489ca92389STheodore Ts'o .quota_read = ext4_quota_read, 11499ca92389STheodore Ts'o .quota_write = ext4_quota_write, 11509ca92389STheodore Ts'o #endif 11519ca92389STheodore Ts'o .bdev_try_to_free_page = bdev_try_to_free_page, 11529ca92389STheodore Ts'o }; 11539ca92389STheodore Ts'o 115439655164SChristoph Hellwig static const struct export_operations ext4_export_ops = { 11551b961ac0SChristoph Hellwig .fh_to_dentry = ext4_fh_to_dentry, 11561b961ac0SChristoph Hellwig .fh_to_parent = ext4_fh_to_parent, 1157617ba13bSMingming Cao .get_parent = ext4_get_parent, 1158ac27a0ecSDave Kleikamp }; 1159ac27a0ecSDave Kleikamp 1160ac27a0ecSDave Kleikamp enum { 1161ac27a0ecSDave Kleikamp Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid, 1162ac27a0ecSDave Kleikamp Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro, 116372578c33STheodore Ts'o Opt_nouid32, Opt_debug, Opt_removed, 1164ac27a0ecSDave Kleikamp Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl, 116572578c33STheodore Ts'o Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload, 116630773840STheodore Ts'o Opt_commit, Opt_min_batch_time, Opt_max_batch_time, 116743e625d8SEric Sandeen Opt_journal_dev, Opt_journal_checksum, Opt_journal_async_commit, 1168ac27a0ecSDave Kleikamp Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback, 1169296c355cSTheodore Ts'o Opt_data_err_abort, Opt_data_err_ignore, 1170ac27a0ecSDave Kleikamp Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota, 11715a20bdfcSJan Kara Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota, 1172ee4a3fcdSTheodore Ts'o Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err, 1173661aa520SEric Sandeen Opt_usrquota, Opt_grpquota, Opt_i_version, 11741449032bSTheodore Ts'o Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_mblk_io_submit, 11751449032bSTheodore Ts'o Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity, 11765328e635SEric Sandeen Opt_inode_readahead_blks, Opt_journal_ioprio, 1177744692dcSJiaying Zhang Opt_dioread_nolock, Opt_dioread_lock, 1178fc6cb1cdSTheodore Ts'o Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable, 1179ac27a0ecSDave Kleikamp }; 1180ac27a0ecSDave Kleikamp 1181a447c093SSteven Whitehouse static const match_table_t tokens = { 1182ac27a0ecSDave Kleikamp {Opt_bsd_df, "bsddf"}, 1183ac27a0ecSDave Kleikamp {Opt_minix_df, "minixdf"}, 1184ac27a0ecSDave Kleikamp {Opt_grpid, "grpid"}, 1185ac27a0ecSDave Kleikamp {Opt_grpid, "bsdgroups"}, 1186ac27a0ecSDave Kleikamp {Opt_nogrpid, "nogrpid"}, 1187ac27a0ecSDave Kleikamp {Opt_nogrpid, "sysvgroups"}, 1188ac27a0ecSDave Kleikamp {Opt_resgid, "resgid=%u"}, 1189ac27a0ecSDave Kleikamp {Opt_resuid, "resuid=%u"}, 1190ac27a0ecSDave Kleikamp {Opt_sb, "sb=%u"}, 1191ac27a0ecSDave Kleikamp {Opt_err_cont, "errors=continue"}, 1192ac27a0ecSDave Kleikamp {Opt_err_panic, "errors=panic"}, 1193ac27a0ecSDave Kleikamp {Opt_err_ro, "errors=remount-ro"}, 1194ac27a0ecSDave Kleikamp {Opt_nouid32, "nouid32"}, 1195ac27a0ecSDave Kleikamp {Opt_debug, "debug"}, 119672578c33STheodore Ts'o {Opt_removed, "oldalloc"}, 119772578c33STheodore Ts'o {Opt_removed, "orlov"}, 1198ac27a0ecSDave Kleikamp {Opt_user_xattr, "user_xattr"}, 1199ac27a0ecSDave Kleikamp {Opt_nouser_xattr, "nouser_xattr"}, 1200ac27a0ecSDave Kleikamp {Opt_acl, "acl"}, 1201ac27a0ecSDave Kleikamp {Opt_noacl, "noacl"}, 1202e3bb52aeSEric Sandeen {Opt_noload, "norecovery"}, 12035a916be1STheodore Ts'o {Opt_noload, "noload"}, 120472578c33STheodore Ts'o {Opt_removed, "nobh"}, 120572578c33STheodore Ts'o {Opt_removed, "bh"}, 1206ac27a0ecSDave Kleikamp {Opt_commit, "commit=%u"}, 120730773840STheodore Ts'o {Opt_min_batch_time, "min_batch_time=%u"}, 120830773840STheodore Ts'o {Opt_max_batch_time, "max_batch_time=%u"}, 1209ac27a0ecSDave Kleikamp {Opt_journal_dev, "journal_dev=%u"}, 1210818d276cSGirish Shilamkar {Opt_journal_checksum, "journal_checksum"}, 1211818d276cSGirish Shilamkar {Opt_journal_async_commit, "journal_async_commit"}, 1212ac27a0ecSDave Kleikamp {Opt_abort, "abort"}, 1213ac27a0ecSDave Kleikamp {Opt_data_journal, "data=journal"}, 1214ac27a0ecSDave Kleikamp {Opt_data_ordered, "data=ordered"}, 1215ac27a0ecSDave Kleikamp {Opt_data_writeback, "data=writeback"}, 12165bf5683aSHidehiro Kawai {Opt_data_err_abort, "data_err=abort"}, 12175bf5683aSHidehiro Kawai {Opt_data_err_ignore, "data_err=ignore"}, 1218ac27a0ecSDave Kleikamp {Opt_offusrjquota, "usrjquota="}, 1219ac27a0ecSDave Kleikamp {Opt_usrjquota, "usrjquota=%s"}, 1220ac27a0ecSDave Kleikamp {Opt_offgrpjquota, "grpjquota="}, 1221ac27a0ecSDave Kleikamp {Opt_grpjquota, "grpjquota=%s"}, 1222ac27a0ecSDave Kleikamp {Opt_jqfmt_vfsold, "jqfmt=vfsold"}, 1223ac27a0ecSDave Kleikamp {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"}, 12245a20bdfcSJan Kara {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"}, 1225ac27a0ecSDave Kleikamp {Opt_grpquota, "grpquota"}, 1226ac27a0ecSDave Kleikamp {Opt_noquota, "noquota"}, 1227ac27a0ecSDave Kleikamp {Opt_quota, "quota"}, 1228ac27a0ecSDave Kleikamp {Opt_usrquota, "usrquota"}, 1229ac27a0ecSDave Kleikamp {Opt_barrier, "barrier=%u"}, 123006705bffSTheodore Ts'o {Opt_barrier, "barrier"}, 123106705bffSTheodore Ts'o {Opt_nobarrier, "nobarrier"}, 123225ec56b5SJean Noel Cordenner {Opt_i_version, "i_version"}, 1233c9de560dSAlex Tomas {Opt_stripe, "stripe=%u"}, 123464769240SAlex Tomas {Opt_delalloc, "delalloc"}, 1235dd919b98SAneesh Kumar K.V {Opt_nodelalloc, "nodelalloc"}, 12361449032bSTheodore Ts'o {Opt_mblk_io_submit, "mblk_io_submit"}, 12371449032bSTheodore Ts'o {Opt_nomblk_io_submit, "nomblk_io_submit"}, 12386fd058f7STheodore Ts'o {Opt_block_validity, "block_validity"}, 12396fd058f7STheodore Ts'o {Opt_noblock_validity, "noblock_validity"}, 1240240799cdSTheodore Ts'o {Opt_inode_readahead_blks, "inode_readahead_blks=%u"}, 1241b3881f74STheodore Ts'o {Opt_journal_ioprio, "journal_ioprio=%u"}, 1242afd4672dSTheodore Ts'o {Opt_auto_da_alloc, "auto_da_alloc=%u"}, 124306705bffSTheodore Ts'o {Opt_auto_da_alloc, "auto_da_alloc"}, 124406705bffSTheodore Ts'o {Opt_noauto_da_alloc, "noauto_da_alloc"}, 1245744692dcSJiaying Zhang {Opt_dioread_nolock, "dioread_nolock"}, 1246744692dcSJiaying Zhang {Opt_dioread_lock, "dioread_lock"}, 12475328e635SEric Sandeen {Opt_discard, "discard"}, 12485328e635SEric Sandeen {Opt_nodiscard, "nodiscard"}, 1249fc6cb1cdSTheodore Ts'o {Opt_init_itable, "init_itable=%u"}, 1250fc6cb1cdSTheodore Ts'o {Opt_init_itable, "init_itable"}, 1251fc6cb1cdSTheodore Ts'o {Opt_noinit_itable, "noinit_itable"}, 1252*c7198b9cSTheodore Ts'o {Opt_removed, "check=none"}, /* mount option from ext2/3 */ 1253*c7198b9cSTheodore Ts'o {Opt_removed, "nocheck"}, /* mount option from ext2/3 */ 1254*c7198b9cSTheodore Ts'o {Opt_removed, "reservation"}, /* mount option from ext2/3 */ 1255*c7198b9cSTheodore Ts'o {Opt_removed, "noreservation"}, /* mount option from ext2/3 */ 1256*c7198b9cSTheodore Ts'o {Opt_removed, "journal=%u"}, /* mount option from ext2/3 */ 1257f3f12faaSJosef Bacik {Opt_err, NULL}, 1258ac27a0ecSDave Kleikamp }; 1259ac27a0ecSDave Kleikamp 1260617ba13bSMingming Cao static ext4_fsblk_t get_sb_block(void **data) 1261ac27a0ecSDave Kleikamp { 1262617ba13bSMingming Cao ext4_fsblk_t sb_block; 1263ac27a0ecSDave Kleikamp char *options = (char *) *data; 1264ac27a0ecSDave Kleikamp 1265ac27a0ecSDave Kleikamp if (!options || strncmp(options, "sb=", 3) != 0) 1266ac27a0ecSDave Kleikamp return 1; /* Default location */ 12670b8e58a1SAndreas Dilger 1268ac27a0ecSDave Kleikamp options += 3; 12690b8e58a1SAndreas Dilger /* TODO: use simple_strtoll with >32bit ext4 */ 1270ac27a0ecSDave Kleikamp sb_block = simple_strtoul(options, &options, 0); 1271ac27a0ecSDave Kleikamp if (*options && *options != ',') { 12724776004fSTheodore Ts'o printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n", 1273ac27a0ecSDave Kleikamp (char *) *data); 1274ac27a0ecSDave Kleikamp return 1; 1275ac27a0ecSDave Kleikamp } 1276ac27a0ecSDave Kleikamp if (*options == ',') 1277ac27a0ecSDave Kleikamp options++; 1278ac27a0ecSDave Kleikamp *data = (void *) options; 12790b8e58a1SAndreas Dilger 1280ac27a0ecSDave Kleikamp return sb_block; 1281ac27a0ecSDave Kleikamp } 1282ac27a0ecSDave Kleikamp 1283b3881f74STheodore Ts'o #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3)) 1284437ca0fdSDmitry Monakhov static char deprecated_msg[] = "Mount option \"%s\" will be removed by %s\n" 1285437ca0fdSDmitry Monakhov "Contact linux-ext4@vger.kernel.org if you think we should keep it.\n"; 1286b3881f74STheodore Ts'o 128756c50f11SDmitry Monakhov #ifdef CONFIG_QUOTA 128856c50f11SDmitry Monakhov static int set_qf_name(struct super_block *sb, int qtype, substring_t *args) 128956c50f11SDmitry Monakhov { 129056c50f11SDmitry Monakhov struct ext4_sb_info *sbi = EXT4_SB(sb); 129156c50f11SDmitry Monakhov char *qname; 129256c50f11SDmitry Monakhov 129356c50f11SDmitry Monakhov if (sb_any_quota_loaded(sb) && 129456c50f11SDmitry Monakhov !sbi->s_qf_names[qtype]) { 129556c50f11SDmitry Monakhov ext4_msg(sb, KERN_ERR, 129656c50f11SDmitry Monakhov "Cannot change journaled " 129756c50f11SDmitry Monakhov "quota options when quota turned on"); 129856c50f11SDmitry Monakhov return 0; 129956c50f11SDmitry Monakhov } 130056c50f11SDmitry Monakhov qname = match_strdup(args); 130156c50f11SDmitry Monakhov if (!qname) { 130256c50f11SDmitry Monakhov ext4_msg(sb, KERN_ERR, 130356c50f11SDmitry Monakhov "Not enough memory for storing quotafile name"); 130456c50f11SDmitry Monakhov return 0; 130556c50f11SDmitry Monakhov } 130656c50f11SDmitry Monakhov if (sbi->s_qf_names[qtype] && 130756c50f11SDmitry Monakhov strcmp(sbi->s_qf_names[qtype], qname)) { 130856c50f11SDmitry Monakhov ext4_msg(sb, KERN_ERR, 130956c50f11SDmitry Monakhov "%s quota file already specified", QTYPE2NAME(qtype)); 131056c50f11SDmitry Monakhov kfree(qname); 131156c50f11SDmitry Monakhov return 0; 131256c50f11SDmitry Monakhov } 131356c50f11SDmitry Monakhov sbi->s_qf_names[qtype] = qname; 131456c50f11SDmitry Monakhov if (strchr(sbi->s_qf_names[qtype], '/')) { 131556c50f11SDmitry Monakhov ext4_msg(sb, KERN_ERR, 131656c50f11SDmitry Monakhov "quotafile must be on filesystem root"); 131756c50f11SDmitry Monakhov kfree(sbi->s_qf_names[qtype]); 131856c50f11SDmitry Monakhov sbi->s_qf_names[qtype] = NULL; 131956c50f11SDmitry Monakhov return 0; 132056c50f11SDmitry Monakhov } 1321fd8c37ecSTheodore Ts'o set_opt(sb, QUOTA); 132256c50f11SDmitry Monakhov return 1; 132356c50f11SDmitry Monakhov } 132456c50f11SDmitry Monakhov 132556c50f11SDmitry Monakhov static int clear_qf_name(struct super_block *sb, int qtype) 132656c50f11SDmitry Monakhov { 132756c50f11SDmitry Monakhov 132856c50f11SDmitry Monakhov struct ext4_sb_info *sbi = EXT4_SB(sb); 132956c50f11SDmitry Monakhov 133056c50f11SDmitry Monakhov if (sb_any_quota_loaded(sb) && 133156c50f11SDmitry Monakhov sbi->s_qf_names[qtype]) { 133256c50f11SDmitry Monakhov ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options" 133356c50f11SDmitry Monakhov " when quota turned on"); 133456c50f11SDmitry Monakhov return 0; 133556c50f11SDmitry Monakhov } 133656c50f11SDmitry Monakhov /* 133756c50f11SDmitry Monakhov * The space will be released later when all options are confirmed 133856c50f11SDmitry Monakhov * to be correct 133956c50f11SDmitry Monakhov */ 134056c50f11SDmitry Monakhov sbi->s_qf_names[qtype] = NULL; 134156c50f11SDmitry Monakhov return 1; 134256c50f11SDmitry Monakhov } 134356c50f11SDmitry Monakhov #endif 134456c50f11SDmitry Monakhov 134526092bf5STheodore Ts'o #define MOPT_SET 0x0001 134626092bf5STheodore Ts'o #define MOPT_CLEAR 0x0002 134726092bf5STheodore Ts'o #define MOPT_NOSUPPORT 0x0004 134826092bf5STheodore Ts'o #define MOPT_EXPLICIT 0x0008 134926092bf5STheodore Ts'o #define MOPT_CLEAR_ERR 0x0010 135026092bf5STheodore Ts'o #define MOPT_GTE0 0x0020 135126092bf5STheodore Ts'o #ifdef CONFIG_QUOTA 135226092bf5STheodore Ts'o #define MOPT_Q 0 135326092bf5STheodore Ts'o #define MOPT_QFMT 0x0040 135426092bf5STheodore Ts'o #else 135526092bf5STheodore Ts'o #define MOPT_Q MOPT_NOSUPPORT 135626092bf5STheodore Ts'o #define MOPT_QFMT MOPT_NOSUPPORT 135726092bf5STheodore Ts'o #endif 135826092bf5STheodore Ts'o #define MOPT_DATAJ 0x0080 135926092bf5STheodore Ts'o 136026092bf5STheodore Ts'o static const struct mount_opts { 136126092bf5STheodore Ts'o int token; 136226092bf5STheodore Ts'o int mount_opt; 136326092bf5STheodore Ts'o int flags; 136426092bf5STheodore Ts'o } ext4_mount_opts[] = { 136526092bf5STheodore Ts'o {Opt_minix_df, EXT4_MOUNT_MINIX_DF, MOPT_SET}, 136626092bf5STheodore Ts'o {Opt_bsd_df, EXT4_MOUNT_MINIX_DF, MOPT_CLEAR}, 136726092bf5STheodore Ts'o {Opt_grpid, EXT4_MOUNT_GRPID, MOPT_SET}, 136826092bf5STheodore Ts'o {Opt_nogrpid, EXT4_MOUNT_GRPID, MOPT_CLEAR}, 136926092bf5STheodore Ts'o {Opt_mblk_io_submit, EXT4_MOUNT_MBLK_IO_SUBMIT, MOPT_SET}, 137026092bf5STheodore Ts'o {Opt_nomblk_io_submit, EXT4_MOUNT_MBLK_IO_SUBMIT, MOPT_CLEAR}, 137126092bf5STheodore Ts'o {Opt_block_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_SET}, 137226092bf5STheodore Ts'o {Opt_noblock_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_CLEAR}, 137326092bf5STheodore Ts'o {Opt_dioread_nolock, EXT4_MOUNT_DIOREAD_NOLOCK, MOPT_SET}, 137426092bf5STheodore Ts'o {Opt_dioread_lock, EXT4_MOUNT_DIOREAD_NOLOCK, MOPT_CLEAR}, 137526092bf5STheodore Ts'o {Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET}, 137626092bf5STheodore Ts'o {Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR}, 137726092bf5STheodore Ts'o {Opt_delalloc, EXT4_MOUNT_DELALLOC, MOPT_SET | MOPT_EXPLICIT}, 137826092bf5STheodore Ts'o {Opt_nodelalloc, EXT4_MOUNT_DELALLOC, MOPT_CLEAR | MOPT_EXPLICIT}, 137926092bf5STheodore Ts'o {Opt_journal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM, MOPT_SET}, 138026092bf5STheodore Ts'o {Opt_journal_async_commit, (EXT4_MOUNT_JOURNAL_ASYNC_COMMIT | 138126092bf5STheodore Ts'o EXT4_MOUNT_JOURNAL_CHECKSUM), MOPT_SET}, 138226092bf5STheodore Ts'o {Opt_noload, EXT4_MOUNT_NOLOAD, MOPT_SET}, 138326092bf5STheodore Ts'o {Opt_err_panic, EXT4_MOUNT_ERRORS_PANIC, MOPT_SET | MOPT_CLEAR_ERR}, 138426092bf5STheodore Ts'o {Opt_err_ro, EXT4_MOUNT_ERRORS_RO, MOPT_SET | MOPT_CLEAR_ERR}, 138526092bf5STheodore Ts'o {Opt_err_cont, EXT4_MOUNT_ERRORS_CONT, MOPT_SET | MOPT_CLEAR_ERR}, 138626092bf5STheodore Ts'o {Opt_data_err_abort, EXT4_MOUNT_DATA_ERR_ABORT, MOPT_SET}, 138726092bf5STheodore Ts'o {Opt_data_err_ignore, EXT4_MOUNT_DATA_ERR_ABORT, MOPT_CLEAR}, 138826092bf5STheodore Ts'o {Opt_barrier, EXT4_MOUNT_BARRIER, MOPT_SET}, 138926092bf5STheodore Ts'o {Opt_nobarrier, EXT4_MOUNT_BARRIER, MOPT_CLEAR}, 139026092bf5STheodore Ts'o {Opt_noauto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_SET}, 139126092bf5STheodore Ts'o {Opt_auto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_CLEAR}, 139226092bf5STheodore Ts'o {Opt_noinit_itable, EXT4_MOUNT_INIT_INODE_TABLE, MOPT_CLEAR}, 139326092bf5STheodore Ts'o {Opt_commit, 0, MOPT_GTE0}, 139426092bf5STheodore Ts'o {Opt_max_batch_time, 0, MOPT_GTE0}, 139526092bf5STheodore Ts'o {Opt_min_batch_time, 0, MOPT_GTE0}, 139626092bf5STheodore Ts'o {Opt_inode_readahead_blks, 0, MOPT_GTE0}, 139726092bf5STheodore Ts'o {Opt_init_itable, 0, MOPT_GTE0}, 139826092bf5STheodore Ts'o {Opt_stripe, 0, MOPT_GTE0}, 139926092bf5STheodore Ts'o {Opt_data_journal, EXT4_MOUNT_JOURNAL_DATA, MOPT_DATAJ}, 140026092bf5STheodore Ts'o {Opt_data_ordered, EXT4_MOUNT_ORDERED_DATA, MOPT_DATAJ}, 140126092bf5STheodore Ts'o {Opt_data_writeback, EXT4_MOUNT_WRITEBACK_DATA, MOPT_DATAJ}, 140226092bf5STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR 140326092bf5STheodore Ts'o {Opt_user_xattr, EXT4_MOUNT_XATTR_USER, MOPT_SET}, 140426092bf5STheodore Ts'o {Opt_nouser_xattr, EXT4_MOUNT_XATTR_USER, MOPT_CLEAR}, 140526092bf5STheodore Ts'o #else 140626092bf5STheodore Ts'o {Opt_user_xattr, 0, MOPT_NOSUPPORT}, 140726092bf5STheodore Ts'o {Opt_nouser_xattr, 0, MOPT_NOSUPPORT}, 140826092bf5STheodore Ts'o #endif 140926092bf5STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL 141026092bf5STheodore Ts'o {Opt_acl, EXT4_MOUNT_POSIX_ACL, MOPT_SET}, 141126092bf5STheodore Ts'o {Opt_noacl, EXT4_MOUNT_POSIX_ACL, MOPT_CLEAR}, 141226092bf5STheodore Ts'o #else 141326092bf5STheodore Ts'o {Opt_acl, 0, MOPT_NOSUPPORT}, 141426092bf5STheodore Ts'o {Opt_noacl, 0, MOPT_NOSUPPORT}, 141526092bf5STheodore Ts'o #endif 141626092bf5STheodore Ts'o {Opt_nouid32, EXT4_MOUNT_NO_UID32, MOPT_SET}, 141726092bf5STheodore Ts'o {Opt_debug, EXT4_MOUNT_DEBUG, MOPT_SET}, 141826092bf5STheodore Ts'o {Opt_quota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, MOPT_SET | MOPT_Q}, 141926092bf5STheodore Ts'o {Opt_usrquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, 142026092bf5STheodore Ts'o MOPT_SET | MOPT_Q}, 142126092bf5STheodore Ts'o {Opt_grpquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_GRPQUOTA, 142226092bf5STheodore Ts'o MOPT_SET | MOPT_Q}, 142326092bf5STheodore Ts'o {Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA | 142426092bf5STheodore Ts'o EXT4_MOUNT_GRPQUOTA), MOPT_CLEAR | MOPT_Q}, 142526092bf5STheodore Ts'o {Opt_usrjquota, 0, MOPT_Q}, 142626092bf5STheodore Ts'o {Opt_grpjquota, 0, MOPT_Q}, 142726092bf5STheodore Ts'o {Opt_offusrjquota, 0, MOPT_Q}, 142826092bf5STheodore Ts'o {Opt_offgrpjquota, 0, MOPT_Q}, 142926092bf5STheodore Ts'o {Opt_jqfmt_vfsold, QFMT_VFS_OLD, MOPT_QFMT}, 143026092bf5STheodore Ts'o {Opt_jqfmt_vfsv0, QFMT_VFS_V0, MOPT_QFMT}, 143126092bf5STheodore Ts'o {Opt_jqfmt_vfsv1, QFMT_VFS_V1, MOPT_QFMT}, 143226092bf5STheodore Ts'o {Opt_err, 0, 0} 143326092bf5STheodore Ts'o }; 143426092bf5STheodore Ts'o 143526092bf5STheodore Ts'o static int handle_mount_opt(struct super_block *sb, char *opt, int token, 143626092bf5STheodore Ts'o substring_t *args, unsigned long *journal_devnum, 143726092bf5STheodore Ts'o unsigned int *journal_ioprio, int is_remount) 143826092bf5STheodore Ts'o { 143926092bf5STheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(sb); 144026092bf5STheodore Ts'o const struct mount_opts *m; 144126092bf5STheodore Ts'o int arg = 0; 144226092bf5STheodore Ts'o 144326092bf5STheodore Ts'o if (args->from && match_int(args, &arg)) 144426092bf5STheodore Ts'o return -1; 144526092bf5STheodore Ts'o switch (token) { 144626092bf5STheodore Ts'o case Opt_sb: 144726092bf5STheodore Ts'o return 1; /* handled by get_sb_block() */ 144826092bf5STheodore Ts'o case Opt_removed: 144926092bf5STheodore Ts'o ext4_msg(sb, KERN_WARNING, 145026092bf5STheodore Ts'o "Ignoring removed %s option", opt); 145126092bf5STheodore Ts'o return 1; 145226092bf5STheodore Ts'o case Opt_resuid: 145326092bf5STheodore Ts'o sbi->s_resuid = arg; 145426092bf5STheodore Ts'o return 1; 145526092bf5STheodore Ts'o case Opt_resgid: 145626092bf5STheodore Ts'o sbi->s_resgid = arg; 145726092bf5STheodore Ts'o return 1; 145826092bf5STheodore Ts'o case Opt_abort: 145926092bf5STheodore Ts'o sbi->s_mount_flags |= EXT4_MF_FS_ABORTED; 146026092bf5STheodore Ts'o return 1; 146126092bf5STheodore Ts'o case Opt_i_version: 146226092bf5STheodore Ts'o sb->s_flags |= MS_I_VERSION; 146326092bf5STheodore Ts'o return 1; 146426092bf5STheodore Ts'o case Opt_journal_dev: 146526092bf5STheodore Ts'o if (is_remount) { 146626092bf5STheodore Ts'o ext4_msg(sb, KERN_ERR, 146726092bf5STheodore Ts'o "Cannot specify journal on remount"); 146826092bf5STheodore Ts'o return -1; 146926092bf5STheodore Ts'o } 147026092bf5STheodore Ts'o *journal_devnum = arg; 147126092bf5STheodore Ts'o return 1; 147226092bf5STheodore Ts'o case Opt_journal_ioprio: 147326092bf5STheodore Ts'o if (arg < 0 || arg > 7) 147426092bf5STheodore Ts'o return -1; 147526092bf5STheodore Ts'o *journal_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, arg); 147626092bf5STheodore Ts'o return 1; 147726092bf5STheodore Ts'o } 147826092bf5STheodore Ts'o 147926092bf5STheodore Ts'o for (m = ext4_mount_opts; m->token != Opt_err; m++) { 148026092bf5STheodore Ts'o if (token != m->token) 148126092bf5STheodore Ts'o continue; 148226092bf5STheodore Ts'o if (args->from && (m->flags & MOPT_GTE0) && (arg < 0)) 148326092bf5STheodore Ts'o return -1; 148426092bf5STheodore Ts'o if (m->flags & MOPT_EXPLICIT) 148526092bf5STheodore Ts'o set_opt2(sb, EXPLICIT_DELALLOC); 148626092bf5STheodore Ts'o if (m->flags & MOPT_CLEAR_ERR) 148726092bf5STheodore Ts'o clear_opt(sb, ERRORS_MASK); 148826092bf5STheodore Ts'o if (token == Opt_noquota && sb_any_quota_loaded(sb)) { 148926092bf5STheodore Ts'o ext4_msg(sb, KERN_ERR, "Cannot change quota " 149026092bf5STheodore Ts'o "options when quota turned on"); 149126092bf5STheodore Ts'o return -1; 149226092bf5STheodore Ts'o } 149326092bf5STheodore Ts'o 149426092bf5STheodore Ts'o if (m->flags & MOPT_NOSUPPORT) { 149526092bf5STheodore Ts'o ext4_msg(sb, KERN_ERR, "%s option not supported", opt); 149626092bf5STheodore Ts'o } else if (token == Opt_commit) { 149726092bf5STheodore Ts'o if (arg == 0) 149826092bf5STheodore Ts'o arg = JBD2_DEFAULT_MAX_COMMIT_AGE; 149926092bf5STheodore Ts'o sbi->s_commit_interval = HZ * arg; 150026092bf5STheodore Ts'o } else if (token == Opt_max_batch_time) { 150126092bf5STheodore Ts'o if (arg == 0) 150226092bf5STheodore Ts'o arg = EXT4_DEF_MAX_BATCH_TIME; 150326092bf5STheodore Ts'o sbi->s_max_batch_time = arg; 150426092bf5STheodore Ts'o } else if (token == Opt_min_batch_time) { 150526092bf5STheodore Ts'o sbi->s_min_batch_time = arg; 150626092bf5STheodore Ts'o } else if (token == Opt_inode_readahead_blks) { 150726092bf5STheodore Ts'o if (arg > (1 << 30)) 150826092bf5STheodore Ts'o return -1; 150926092bf5STheodore Ts'o if (arg && !is_power_of_2(arg)) { 151026092bf5STheodore Ts'o ext4_msg(sb, KERN_ERR, 151126092bf5STheodore Ts'o "EXT4-fs: inode_readahead_blks" 151226092bf5STheodore Ts'o " must be a power of 2"); 151326092bf5STheodore Ts'o return -1; 151426092bf5STheodore Ts'o } 151526092bf5STheodore Ts'o sbi->s_inode_readahead_blks = arg; 151626092bf5STheodore Ts'o } else if (token == Opt_init_itable) { 151726092bf5STheodore Ts'o set_opt(sb, INIT_INODE_TABLE); 151826092bf5STheodore Ts'o if (!args->from) 151926092bf5STheodore Ts'o arg = EXT4_DEF_LI_WAIT_MULT; 152026092bf5STheodore Ts'o sbi->s_li_wait_mult = arg; 152126092bf5STheodore Ts'o } else if (token == Opt_stripe) { 152226092bf5STheodore Ts'o sbi->s_stripe = arg; 152326092bf5STheodore Ts'o } else if (m->flags & MOPT_DATAJ) { 152426092bf5STheodore Ts'o if (is_remount) { 152526092bf5STheodore Ts'o if (!sbi->s_journal) 152626092bf5STheodore Ts'o ext4_msg(sb, KERN_WARNING, "Remounting file system with no journal so ignoring journalled data option"); 152726092bf5STheodore Ts'o else if (test_opt(sb, DATA_FLAGS) != 152826092bf5STheodore Ts'o m->mount_opt) { 152926092bf5STheodore Ts'o ext4_msg(sb, KERN_ERR, 153026092bf5STheodore Ts'o "Cannot change data mode on remount"); 153126092bf5STheodore Ts'o return -1; 153226092bf5STheodore Ts'o } 153326092bf5STheodore Ts'o } else { 153426092bf5STheodore Ts'o clear_opt(sb, DATA_FLAGS); 153526092bf5STheodore Ts'o sbi->s_mount_opt |= m->mount_opt; 153626092bf5STheodore Ts'o } 153726092bf5STheodore Ts'o #ifdef CONFIG_QUOTA 153826092bf5STheodore Ts'o } else if (token == Opt_usrjquota) { 153926092bf5STheodore Ts'o if (!set_qf_name(sb, USRQUOTA, &args[0])) 154026092bf5STheodore Ts'o return -1; 154126092bf5STheodore Ts'o } else if (token == Opt_grpjquota) { 154226092bf5STheodore Ts'o if (!set_qf_name(sb, GRPQUOTA, &args[0])) 154326092bf5STheodore Ts'o return -1; 154426092bf5STheodore Ts'o } else if (token == Opt_offusrjquota) { 154526092bf5STheodore Ts'o if (!clear_qf_name(sb, USRQUOTA)) 154626092bf5STheodore Ts'o return -1; 154726092bf5STheodore Ts'o } else if (token == Opt_offgrpjquota) { 154826092bf5STheodore Ts'o if (!clear_qf_name(sb, GRPQUOTA)) 154926092bf5STheodore Ts'o return -1; 155026092bf5STheodore Ts'o } else if (m->flags & MOPT_QFMT) { 155126092bf5STheodore Ts'o if (sb_any_quota_loaded(sb) && 155226092bf5STheodore Ts'o sbi->s_jquota_fmt != m->mount_opt) { 155326092bf5STheodore Ts'o ext4_msg(sb, KERN_ERR, "Cannot " 155426092bf5STheodore Ts'o "change journaled quota options " 155526092bf5STheodore Ts'o "when quota turned on"); 155626092bf5STheodore Ts'o return -1; 155726092bf5STheodore Ts'o } 155826092bf5STheodore Ts'o sbi->s_jquota_fmt = m->mount_opt; 155926092bf5STheodore Ts'o #endif 156026092bf5STheodore Ts'o } else { 156126092bf5STheodore Ts'o if (!args->from) 156226092bf5STheodore Ts'o arg = 1; 156326092bf5STheodore Ts'o if (m->flags & MOPT_CLEAR) 156426092bf5STheodore Ts'o arg = !arg; 156526092bf5STheodore Ts'o else if (unlikely(!(m->flags & MOPT_SET))) { 156626092bf5STheodore Ts'o ext4_msg(sb, KERN_WARNING, 156726092bf5STheodore Ts'o "buggy handling of option %s", opt); 156826092bf5STheodore Ts'o WARN_ON(1); 156926092bf5STheodore Ts'o return -1; 157026092bf5STheodore Ts'o } 157126092bf5STheodore Ts'o if (arg != 0) 157226092bf5STheodore Ts'o sbi->s_mount_opt |= m->mount_opt; 157326092bf5STheodore Ts'o else 157426092bf5STheodore Ts'o sbi->s_mount_opt &= ~m->mount_opt; 157526092bf5STheodore Ts'o } 157626092bf5STheodore Ts'o return 1; 157726092bf5STheodore Ts'o } 157826092bf5STheodore Ts'o ext4_msg(sb, KERN_ERR, "Unrecognized mount option \"%s\" " 157926092bf5STheodore Ts'o "or missing value", opt); 158026092bf5STheodore Ts'o return -1; 158126092bf5STheodore Ts'o } 158226092bf5STheodore Ts'o 1583ac27a0ecSDave Kleikamp static int parse_options(char *options, struct super_block *sb, 1584c3191067STheodore Ts'o unsigned long *journal_devnum, 1585b3881f74STheodore Ts'o unsigned int *journal_ioprio, 1586661aa520SEric Sandeen int is_remount) 1587ac27a0ecSDave Kleikamp { 1588617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 1589ac27a0ecSDave Kleikamp char *p; 1590ac27a0ecSDave Kleikamp substring_t args[MAX_OPT_ARGS]; 159126092bf5STheodore Ts'o int token; 1592ac27a0ecSDave Kleikamp 1593ac27a0ecSDave Kleikamp if (!options) 1594ac27a0ecSDave Kleikamp return 1; 1595ac27a0ecSDave Kleikamp 1596ac27a0ecSDave Kleikamp while ((p = strsep(&options, ",")) != NULL) { 1597ac27a0ecSDave Kleikamp if (!*p) 1598ac27a0ecSDave Kleikamp continue; 159915121c18SEric Sandeen /* 160015121c18SEric Sandeen * Initialize args struct so we know whether arg was 160115121c18SEric Sandeen * found; some options take optional arguments. 160215121c18SEric Sandeen */ 160326092bf5STheodore Ts'o args[0].to = args[0].from = 0; 1604ac27a0ecSDave Kleikamp token = match_token(p, tokens, args); 160526092bf5STheodore Ts'o if (handle_mount_opt(sb, p, token, args, journal_devnum, 160626092bf5STheodore Ts'o journal_ioprio, is_remount) < 0) 1607ac27a0ecSDave Kleikamp return 0; 1608ac27a0ecSDave Kleikamp } 1609ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 1610ac27a0ecSDave Kleikamp if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) { 1611482a7425SDmitry Monakhov if (test_opt(sb, USRQUOTA) && sbi->s_qf_names[USRQUOTA]) 1612fd8c37ecSTheodore Ts'o clear_opt(sb, USRQUOTA); 1613ac27a0ecSDave Kleikamp 1614482a7425SDmitry Monakhov if (test_opt(sb, GRPQUOTA) && sbi->s_qf_names[GRPQUOTA]) 1615fd8c37ecSTheodore Ts'o clear_opt(sb, GRPQUOTA); 1616ac27a0ecSDave Kleikamp 161756c50f11SDmitry Monakhov if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) { 1618b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "old and new quota " 1619b31e1552SEric Sandeen "format mixing"); 1620ac27a0ecSDave Kleikamp return 0; 1621ac27a0ecSDave Kleikamp } 1622ac27a0ecSDave Kleikamp 1623ac27a0ecSDave Kleikamp if (!sbi->s_jquota_fmt) { 1624b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "journaled quota format " 1625b31e1552SEric Sandeen "not specified"); 1626ac27a0ecSDave Kleikamp return 0; 1627ac27a0ecSDave Kleikamp } 1628ac27a0ecSDave Kleikamp } else { 1629ac27a0ecSDave Kleikamp if (sbi->s_jquota_fmt) { 1630b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "journaled quota format " 16312c8be6b2SJan Kara "specified with no journaling " 1632b31e1552SEric Sandeen "enabled"); 1633ac27a0ecSDave Kleikamp return 0; 1634ac27a0ecSDave Kleikamp } 1635ac27a0ecSDave Kleikamp } 1636ac27a0ecSDave Kleikamp #endif 1637ac27a0ecSDave Kleikamp return 1; 1638ac27a0ecSDave Kleikamp } 1639ac27a0ecSDave Kleikamp 16402adf6da8STheodore Ts'o static inline void ext4_show_quota_options(struct seq_file *seq, 16412adf6da8STheodore Ts'o struct super_block *sb) 16422adf6da8STheodore Ts'o { 16432adf6da8STheodore Ts'o #if defined(CONFIG_QUOTA) 16442adf6da8STheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(sb); 16452adf6da8STheodore Ts'o 16462adf6da8STheodore Ts'o if (sbi->s_jquota_fmt) { 16472adf6da8STheodore Ts'o char *fmtname = ""; 16482adf6da8STheodore Ts'o 16492adf6da8STheodore Ts'o switch (sbi->s_jquota_fmt) { 16502adf6da8STheodore Ts'o case QFMT_VFS_OLD: 16512adf6da8STheodore Ts'o fmtname = "vfsold"; 16522adf6da8STheodore Ts'o break; 16532adf6da8STheodore Ts'o case QFMT_VFS_V0: 16542adf6da8STheodore Ts'o fmtname = "vfsv0"; 16552adf6da8STheodore Ts'o break; 16562adf6da8STheodore Ts'o case QFMT_VFS_V1: 16572adf6da8STheodore Ts'o fmtname = "vfsv1"; 16582adf6da8STheodore Ts'o break; 16592adf6da8STheodore Ts'o } 16602adf6da8STheodore Ts'o seq_printf(seq, ",jqfmt=%s", fmtname); 16612adf6da8STheodore Ts'o } 16622adf6da8STheodore Ts'o 16632adf6da8STheodore Ts'o if (sbi->s_qf_names[USRQUOTA]) 16642adf6da8STheodore Ts'o seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]); 16652adf6da8STheodore Ts'o 16662adf6da8STheodore Ts'o if (sbi->s_qf_names[GRPQUOTA]) 16672adf6da8STheodore Ts'o seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]); 16682adf6da8STheodore Ts'o 16692adf6da8STheodore Ts'o if (test_opt(sb, USRQUOTA)) 16702adf6da8STheodore Ts'o seq_puts(seq, ",usrquota"); 16712adf6da8STheodore Ts'o 16722adf6da8STheodore Ts'o if (test_opt(sb, GRPQUOTA)) 16732adf6da8STheodore Ts'o seq_puts(seq, ",grpquota"); 16742adf6da8STheodore Ts'o #endif 16752adf6da8STheodore Ts'o } 16762adf6da8STheodore Ts'o 16775a916be1STheodore Ts'o static const char *token2str(int token) 16785a916be1STheodore Ts'o { 16795a916be1STheodore Ts'o static const struct match_token *t; 16805a916be1STheodore Ts'o 16815a916be1STheodore Ts'o for (t = tokens; t->token != Opt_err; t++) 16825a916be1STheodore Ts'o if (t->token == token && !strchr(t->pattern, '=')) 16835a916be1STheodore Ts'o break; 16845a916be1STheodore Ts'o return t->pattern; 16855a916be1STheodore Ts'o } 16865a916be1STheodore Ts'o 16872adf6da8STheodore Ts'o /* 16882adf6da8STheodore Ts'o * Show an option if 16892adf6da8STheodore Ts'o * - it's set to a non-default value OR 16902adf6da8STheodore Ts'o * - if the per-sb default is different from the global default 16912adf6da8STheodore Ts'o */ 169266acdcf4STheodore Ts'o static int _ext4_show_options(struct seq_file *seq, struct super_block *sb, 169366acdcf4STheodore Ts'o int nodefs) 16942adf6da8STheodore Ts'o { 16952adf6da8STheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(sb); 16962adf6da8STheodore Ts'o struct ext4_super_block *es = sbi->s_es; 169766acdcf4STheodore Ts'o int def_errors, def_mount_opt = nodefs ? 0 : sbi->s_def_mount_opt; 16985a916be1STheodore Ts'o const struct mount_opts *m; 169966acdcf4STheodore Ts'o char sep = nodefs ? '\n' : ','; 17002adf6da8STheodore Ts'o 170166acdcf4STheodore Ts'o #define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep) 170266acdcf4STheodore Ts'o #define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg) 17032adf6da8STheodore Ts'o 17042adf6da8STheodore Ts'o if (sbi->s_sb_block != 1) 17055a916be1STheodore Ts'o SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block); 17065a916be1STheodore Ts'o 17075a916be1STheodore Ts'o for (m = ext4_mount_opts; m->token != Opt_err; m++) { 17085a916be1STheodore Ts'o int want_set = m->flags & MOPT_SET; 17095a916be1STheodore Ts'o if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) || 17105a916be1STheodore Ts'o (m->flags & MOPT_CLEAR_ERR)) 17115a916be1STheodore Ts'o continue; 171266acdcf4STheodore Ts'o if (!(m->mount_opt & (sbi->s_mount_opt ^ def_mount_opt))) 17135a916be1STheodore Ts'o continue; /* skip if same as the default */ 17145a916be1STheodore Ts'o if ((want_set && 17155a916be1STheodore Ts'o (sbi->s_mount_opt & m->mount_opt) != m->mount_opt) || 17165a916be1STheodore Ts'o (!want_set && (sbi->s_mount_opt & m->mount_opt))) 17175a916be1STheodore Ts'o continue; /* select Opt_noFoo vs Opt_Foo */ 17185a916be1STheodore Ts'o SEQ_OPTS_PRINT("%s", token2str(m->token)); 17195a916be1STheodore Ts'o } 17205a916be1STheodore Ts'o 172166acdcf4STheodore Ts'o if (nodefs || sbi->s_resuid != EXT4_DEF_RESUID || 17225a916be1STheodore Ts'o le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) 17235a916be1STheodore Ts'o SEQ_OPTS_PRINT("resuid=%u", sbi->s_resuid); 172466acdcf4STheodore Ts'o if (nodefs || sbi->s_resgid != EXT4_DEF_RESGID || 17255a916be1STheodore Ts'o le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) 17265a916be1STheodore Ts'o SEQ_OPTS_PRINT("resgid=%u", sbi->s_resgid); 172766acdcf4STheodore Ts'o def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors); 17285a916be1STheodore Ts'o if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO) 17295a916be1STheodore Ts'o SEQ_OPTS_PUTS("errors=remount-ro"); 17302adf6da8STheodore Ts'o if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE) 17315a916be1STheodore Ts'o SEQ_OPTS_PUTS("errors=continue"); 17322adf6da8STheodore Ts'o if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC) 17335a916be1STheodore Ts'o SEQ_OPTS_PUTS("errors=panic"); 173466acdcf4STheodore Ts'o if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) 17355a916be1STheodore Ts'o SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ); 173666acdcf4STheodore Ts'o if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME) 17375a916be1STheodore Ts'o SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time); 173866acdcf4STheodore Ts'o if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME) 17395a916be1STheodore Ts'o SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time); 17402adf6da8STheodore Ts'o if (sb->s_flags & MS_I_VERSION) 17415a916be1STheodore Ts'o SEQ_OPTS_PUTS("i_version"); 174266acdcf4STheodore Ts'o if (nodefs || sbi->s_stripe) 17435a916be1STheodore Ts'o SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe); 174466acdcf4STheodore Ts'o if (EXT4_MOUNT_DATA_FLAGS & (sbi->s_mount_opt ^ def_mount_opt)) { 17452adf6da8STheodore Ts'o if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) 17465a916be1STheodore Ts'o SEQ_OPTS_PUTS("data=journal"); 17472adf6da8STheodore Ts'o else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) 17485a916be1STheodore Ts'o SEQ_OPTS_PUTS("data=ordered"); 17492adf6da8STheodore Ts'o else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA) 17505a916be1STheodore Ts'o SEQ_OPTS_PUTS("data=writeback"); 17515a916be1STheodore Ts'o } 175266acdcf4STheodore Ts'o if (nodefs || 175366acdcf4STheodore Ts'o sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS) 17545a916be1STheodore Ts'o SEQ_OPTS_PRINT("inode_readahead_blks=%u", 17552adf6da8STheodore Ts'o sbi->s_inode_readahead_blks); 17562adf6da8STheodore Ts'o 175766acdcf4STheodore Ts'o if (nodefs || (test_opt(sb, INIT_INODE_TABLE) && 175866acdcf4STheodore Ts'o (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT))) 17595a916be1STheodore Ts'o SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult); 17602adf6da8STheodore Ts'o 17612adf6da8STheodore Ts'o ext4_show_quota_options(seq, sb); 17622adf6da8STheodore Ts'o return 0; 17632adf6da8STheodore Ts'o } 17642adf6da8STheodore Ts'o 176566acdcf4STheodore Ts'o static int ext4_show_options(struct seq_file *seq, struct dentry *root) 176666acdcf4STheodore Ts'o { 176766acdcf4STheodore Ts'o return _ext4_show_options(seq, root->d_sb, 0); 176866acdcf4STheodore Ts'o } 176966acdcf4STheodore Ts'o 177066acdcf4STheodore Ts'o static int options_seq_show(struct seq_file *seq, void *offset) 177166acdcf4STheodore Ts'o { 177266acdcf4STheodore Ts'o struct super_block *sb = seq->private; 177366acdcf4STheodore Ts'o int rc; 177466acdcf4STheodore Ts'o 177566acdcf4STheodore Ts'o seq_puts(seq, (sb->s_flags & MS_RDONLY) ? "ro" : "rw"); 177666acdcf4STheodore Ts'o rc = _ext4_show_options(seq, sb, 1); 177766acdcf4STheodore Ts'o seq_puts(seq, "\n"); 177866acdcf4STheodore Ts'o return rc; 177966acdcf4STheodore Ts'o } 178066acdcf4STheodore Ts'o 178166acdcf4STheodore Ts'o static int options_open_fs(struct inode *inode, struct file *file) 178266acdcf4STheodore Ts'o { 178366acdcf4STheodore Ts'o return single_open(file, options_seq_show, PDE(inode)->data); 178466acdcf4STheodore Ts'o } 178566acdcf4STheodore Ts'o 178666acdcf4STheodore Ts'o static const struct file_operations ext4_seq_options_fops = { 178766acdcf4STheodore Ts'o .owner = THIS_MODULE, 178866acdcf4STheodore Ts'o .open = options_open_fs, 178966acdcf4STheodore Ts'o .read = seq_read, 179066acdcf4STheodore Ts'o .llseek = seq_lseek, 179166acdcf4STheodore Ts'o .release = single_release, 179266acdcf4STheodore Ts'o }; 179366acdcf4STheodore Ts'o 1794617ba13bSMingming Cao static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es, 1795ac27a0ecSDave Kleikamp int read_only) 1796ac27a0ecSDave Kleikamp { 1797617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 1798ac27a0ecSDave Kleikamp int res = 0; 1799ac27a0ecSDave Kleikamp 1800617ba13bSMingming Cao if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) { 1801b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "revision level too high, " 1802b31e1552SEric Sandeen "forcing read-only mode"); 1803ac27a0ecSDave Kleikamp res = MS_RDONLY; 1804ac27a0ecSDave Kleikamp } 1805ac27a0ecSDave Kleikamp if (read_only) 1806281b5995STheodore Ts'o goto done; 1807617ba13bSMingming Cao if (!(sbi->s_mount_state & EXT4_VALID_FS)) 1808b31e1552SEric Sandeen ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, " 1809b31e1552SEric Sandeen "running e2fsck is recommended"); 1810617ba13bSMingming Cao else if ((sbi->s_mount_state & EXT4_ERROR_FS)) 1811b31e1552SEric Sandeen ext4_msg(sb, KERN_WARNING, 1812b31e1552SEric Sandeen "warning: mounting fs with errors, " 1813b31e1552SEric Sandeen "running e2fsck is recommended"); 1814ed3ce80aSTao Ma else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 && 1815ac27a0ecSDave Kleikamp le16_to_cpu(es->s_mnt_count) >= 1816ac27a0ecSDave Kleikamp (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count)) 1817b31e1552SEric Sandeen ext4_msg(sb, KERN_WARNING, 1818b31e1552SEric Sandeen "warning: maximal mount count reached, " 1819b31e1552SEric Sandeen "running e2fsck is recommended"); 1820ac27a0ecSDave Kleikamp else if (le32_to_cpu(es->s_checkinterval) && 1821ac27a0ecSDave Kleikamp (le32_to_cpu(es->s_lastcheck) + 1822ac27a0ecSDave Kleikamp le32_to_cpu(es->s_checkinterval) <= get_seconds())) 1823b31e1552SEric Sandeen ext4_msg(sb, KERN_WARNING, 1824b31e1552SEric Sandeen "warning: checktime reached, " 1825b31e1552SEric Sandeen "running e2fsck is recommended"); 18260390131bSFrank Mayhar if (!sbi->s_journal) 1827216c34b2SMarcin Slusarz es->s_state &= cpu_to_le16(~EXT4_VALID_FS); 1828ac27a0ecSDave Kleikamp if (!(__s16) le16_to_cpu(es->s_max_mnt_count)) 1829617ba13bSMingming Cao es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT); 1830e8546d06SMarcin Slusarz le16_add_cpu(&es->s_mnt_count, 1); 1831ac27a0ecSDave Kleikamp es->s_mtime = cpu_to_le32(get_seconds()); 1832617ba13bSMingming Cao ext4_update_dynamic_rev(sb); 18330390131bSFrank Mayhar if (sbi->s_journal) 1834617ba13bSMingming Cao EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER); 1835ac27a0ecSDave Kleikamp 1836e2d67052STheodore Ts'o ext4_commit_super(sb, 1); 1837281b5995STheodore Ts'o done: 1838ac27a0ecSDave Kleikamp if (test_opt(sb, DEBUG)) 1839a9df9a49STheodore Ts'o printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, " 1840a2595b8aSTheodore Ts'o "bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n", 1841ac27a0ecSDave Kleikamp sb->s_blocksize, 1842ac27a0ecSDave Kleikamp sbi->s_groups_count, 1843617ba13bSMingming Cao EXT4_BLOCKS_PER_GROUP(sb), 1844617ba13bSMingming Cao EXT4_INODES_PER_GROUP(sb), 1845a2595b8aSTheodore Ts'o sbi->s_mount_opt, sbi->s_mount_opt2); 1846ac27a0ecSDave Kleikamp 18477abc52c2SDan Magenheimer cleancache_init_fs(sb); 1848ac27a0ecSDave Kleikamp return res; 1849ac27a0ecSDave Kleikamp } 1850ac27a0ecSDave Kleikamp 1851772cb7c8SJose R. Santos static int ext4_fill_flex_info(struct super_block *sb) 1852772cb7c8SJose R. Santos { 1853772cb7c8SJose R. Santos struct ext4_sb_info *sbi = EXT4_SB(sb); 1854772cb7c8SJose R. Santos struct ext4_group_desc *gdp = NULL; 1855772cb7c8SJose R. Santos ext4_group_t flex_group_count; 1856772cb7c8SJose R. Santos ext4_group_t flex_group; 1857d50f2ab6SXi Wang unsigned int groups_per_flex = 0; 1858c5ca7c76STheodore Ts'o size_t size; 1859772cb7c8SJose R. Santos int i; 1860772cb7c8SJose R. Santos 1861503358aeSTheodore Ts'o sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex; 1862d50f2ab6SXi Wang if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) { 1863772cb7c8SJose R. Santos sbi->s_log_groups_per_flex = 0; 1864772cb7c8SJose R. Santos return 1; 1865772cb7c8SJose R. Santos } 1866d50f2ab6SXi Wang groups_per_flex = 1 << sbi->s_log_groups_per_flex; 1867772cb7c8SJose R. Santos 1868c62a11fdSFrederic Bohe /* We allocate both existing and potentially added groups */ 1869c62a11fdSFrederic Bohe flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) + 1870d94e99a6SAneesh Kumar K.V ((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) << 1871d94e99a6SAneesh Kumar K.V EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex; 1872c5ca7c76STheodore Ts'o size = flex_group_count * sizeof(struct flex_groups); 18739933fc0aSTheodore Ts'o sbi->s_flex_groups = ext4_kvzalloc(size, GFP_KERNEL); 1874c5ca7c76STheodore Ts'o if (sbi->s_flex_groups == NULL) { 18759933fc0aSTheodore Ts'o ext4_msg(sb, KERN_ERR, "not enough memory for %u flex groups", 187694de56abSJoe Perches flex_group_count); 1877772cb7c8SJose R. Santos goto failed; 1878772cb7c8SJose R. Santos } 1879772cb7c8SJose R. Santos 1880772cb7c8SJose R. Santos for (i = 0; i < sbi->s_groups_count; i++) { 188188b6edd1STheodore Ts'o gdp = ext4_get_group_desc(sb, i, NULL); 1882772cb7c8SJose R. Santos 1883772cb7c8SJose R. Santos flex_group = ext4_flex_group(sbi, i); 18847ad9bb65STheodore Ts'o atomic_add(ext4_free_inodes_count(sb, gdp), 18857ad9bb65STheodore Ts'o &sbi->s_flex_groups[flex_group].free_inodes); 1886021b65bbSTheodore Ts'o atomic_add(ext4_free_group_clusters(sb, gdp), 188724aaa8efSTheodore Ts'o &sbi->s_flex_groups[flex_group].free_clusters); 18887ad9bb65STheodore Ts'o atomic_add(ext4_used_dirs_count(sb, gdp), 18897ad9bb65STheodore Ts'o &sbi->s_flex_groups[flex_group].used_dirs); 1890772cb7c8SJose R. Santos } 1891772cb7c8SJose R. Santos 1892772cb7c8SJose R. Santos return 1; 1893772cb7c8SJose R. Santos failed: 1894772cb7c8SJose R. Santos return 0; 1895772cb7c8SJose R. Santos } 1896772cb7c8SJose R. Santos 1897717d50e4SAndreas Dilger __le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group, 1898717d50e4SAndreas Dilger struct ext4_group_desc *gdp) 1899717d50e4SAndreas Dilger { 1900717d50e4SAndreas Dilger __u16 crc = 0; 1901717d50e4SAndreas Dilger 1902717d50e4SAndreas Dilger if (sbi->s_es->s_feature_ro_compat & 1903717d50e4SAndreas Dilger cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) { 1904717d50e4SAndreas Dilger int offset = offsetof(struct ext4_group_desc, bg_checksum); 1905717d50e4SAndreas Dilger __le32 le_group = cpu_to_le32(block_group); 1906717d50e4SAndreas Dilger 1907717d50e4SAndreas Dilger crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid)); 1908717d50e4SAndreas Dilger crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group)); 1909717d50e4SAndreas Dilger crc = crc16(crc, (__u8 *)gdp, offset); 1910717d50e4SAndreas Dilger offset += sizeof(gdp->bg_checksum); /* skip checksum */ 1911717d50e4SAndreas Dilger /* for checksum of struct ext4_group_desc do the rest...*/ 1912717d50e4SAndreas Dilger if ((sbi->s_es->s_feature_incompat & 1913717d50e4SAndreas Dilger cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) && 1914717d50e4SAndreas Dilger offset < le16_to_cpu(sbi->s_es->s_desc_size)) 1915717d50e4SAndreas Dilger crc = crc16(crc, (__u8 *)gdp + offset, 1916717d50e4SAndreas Dilger le16_to_cpu(sbi->s_es->s_desc_size) - 1917717d50e4SAndreas Dilger offset); 1918717d50e4SAndreas Dilger } 1919717d50e4SAndreas Dilger 1920717d50e4SAndreas Dilger return cpu_to_le16(crc); 1921717d50e4SAndreas Dilger } 1922717d50e4SAndreas Dilger 1923717d50e4SAndreas Dilger int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group, 1924717d50e4SAndreas Dilger struct ext4_group_desc *gdp) 1925717d50e4SAndreas Dilger { 1926717d50e4SAndreas Dilger if ((sbi->s_es->s_feature_ro_compat & 1927717d50e4SAndreas Dilger cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) && 1928717d50e4SAndreas Dilger (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp))) 1929717d50e4SAndreas Dilger return 0; 1930717d50e4SAndreas Dilger 1931717d50e4SAndreas Dilger return 1; 1932717d50e4SAndreas Dilger } 1933717d50e4SAndreas Dilger 1934ac27a0ecSDave Kleikamp /* Called at mount-time, super-block is locked */ 1935bfff6873SLukas Czerner static int ext4_check_descriptors(struct super_block *sb, 1936bfff6873SLukas Czerner ext4_group_t *first_not_zeroed) 1937ac27a0ecSDave Kleikamp { 1938617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 1939617ba13bSMingming Cao ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block); 1940617ba13bSMingming Cao ext4_fsblk_t last_block; 1941bd81d8eeSLaurent Vivier ext4_fsblk_t block_bitmap; 1942bd81d8eeSLaurent Vivier ext4_fsblk_t inode_bitmap; 1943bd81d8eeSLaurent Vivier ext4_fsblk_t inode_table; 1944ce421581SJose R. Santos int flexbg_flag = 0; 1945bfff6873SLukas Czerner ext4_group_t i, grp = sbi->s_groups_count; 1946ac27a0ecSDave Kleikamp 1947ce421581SJose R. Santos if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG)) 1948ce421581SJose R. Santos flexbg_flag = 1; 1949ce421581SJose R. Santos 1950617ba13bSMingming Cao ext4_debug("Checking group descriptors"); 1951ac27a0ecSDave Kleikamp 1952197cd65aSAkinobu Mita for (i = 0; i < sbi->s_groups_count; i++) { 1953197cd65aSAkinobu Mita struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL); 1954197cd65aSAkinobu Mita 1955ce421581SJose R. Santos if (i == sbi->s_groups_count - 1 || flexbg_flag) 1956bd81d8eeSLaurent Vivier last_block = ext4_blocks_count(sbi->s_es) - 1; 1957ac27a0ecSDave Kleikamp else 1958ac27a0ecSDave Kleikamp last_block = first_block + 1959617ba13bSMingming Cao (EXT4_BLOCKS_PER_GROUP(sb) - 1); 1960ac27a0ecSDave Kleikamp 1961bfff6873SLukas Czerner if ((grp == sbi->s_groups_count) && 1962bfff6873SLukas Czerner !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED))) 1963bfff6873SLukas Czerner grp = i; 1964bfff6873SLukas Czerner 19658fadc143SAlexandre Ratchov block_bitmap = ext4_block_bitmap(sb, gdp); 19662b2d6d01STheodore Ts'o if (block_bitmap < first_block || block_bitmap > last_block) { 1967b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " 1968a9df9a49STheodore Ts'o "Block bitmap for group %u not in group " 1969b31e1552SEric Sandeen "(block %llu)!", i, block_bitmap); 1970ac27a0ecSDave Kleikamp return 0; 1971ac27a0ecSDave Kleikamp } 19728fadc143SAlexandre Ratchov inode_bitmap = ext4_inode_bitmap(sb, gdp); 19732b2d6d01STheodore Ts'o if (inode_bitmap < first_block || inode_bitmap > last_block) { 1974b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " 1975a9df9a49STheodore Ts'o "Inode bitmap for group %u not in group " 1976b31e1552SEric Sandeen "(block %llu)!", i, inode_bitmap); 1977ac27a0ecSDave Kleikamp return 0; 1978ac27a0ecSDave Kleikamp } 19798fadc143SAlexandre Ratchov inode_table = ext4_inode_table(sb, gdp); 1980bd81d8eeSLaurent Vivier if (inode_table < first_block || 19812b2d6d01STheodore Ts'o inode_table + sbi->s_itb_per_group - 1 > last_block) { 1982b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " 1983a9df9a49STheodore Ts'o "Inode table for group %u not in group " 1984b31e1552SEric Sandeen "(block %llu)!", i, inode_table); 1985ac27a0ecSDave Kleikamp return 0; 1986ac27a0ecSDave Kleikamp } 1987955ce5f5SAneesh Kumar K.V ext4_lock_group(sb, i); 1988717d50e4SAndreas Dilger if (!ext4_group_desc_csum_verify(sbi, i, gdp)) { 1989b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: " 1990b31e1552SEric Sandeen "Checksum for group %u failed (%u!=%u)", 1991fd2d4291SAvantika Mathur i, le16_to_cpu(ext4_group_desc_csum(sbi, i, 1992fd2d4291SAvantika Mathur gdp)), le16_to_cpu(gdp->bg_checksum)); 19937ee1ec4cSLi Zefan if (!(sb->s_flags & MS_RDONLY)) { 1994955ce5f5SAneesh Kumar K.V ext4_unlock_group(sb, i); 1995717d50e4SAndreas Dilger return 0; 1996717d50e4SAndreas Dilger } 19977ee1ec4cSLi Zefan } 1998955ce5f5SAneesh Kumar K.V ext4_unlock_group(sb, i); 1999ce421581SJose R. Santos if (!flexbg_flag) 2000617ba13bSMingming Cao first_block += EXT4_BLOCKS_PER_GROUP(sb); 2001ac27a0ecSDave Kleikamp } 2002bfff6873SLukas Czerner if (NULL != first_not_zeroed) 2003bfff6873SLukas Czerner *first_not_zeroed = grp; 2004ac27a0ecSDave Kleikamp 20055dee5437STheodore Ts'o ext4_free_blocks_count_set(sbi->s_es, 20065dee5437STheodore Ts'o EXT4_C2B(sbi, ext4_count_free_clusters(sb))); 2007617ba13bSMingming Cao sbi->s_es->s_free_inodes_count =cpu_to_le32(ext4_count_free_inodes(sb)); 2008ac27a0ecSDave Kleikamp return 1; 2009ac27a0ecSDave Kleikamp } 2010ac27a0ecSDave Kleikamp 2011617ba13bSMingming Cao /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at 2012ac27a0ecSDave Kleikamp * the superblock) which were deleted from all directories, but held open by 2013ac27a0ecSDave Kleikamp * a process at the time of a crash. We walk the list and try to delete these 2014ac27a0ecSDave Kleikamp * inodes at recovery time (only with a read-write filesystem). 2015ac27a0ecSDave Kleikamp * 2016ac27a0ecSDave Kleikamp * In order to keep the orphan inode chain consistent during traversal (in 2017ac27a0ecSDave Kleikamp * case of crash during recovery), we link each inode into the superblock 2018ac27a0ecSDave Kleikamp * orphan list_head and handle it the same way as an inode deletion during 2019ac27a0ecSDave Kleikamp * normal operation (which journals the operations for us). 2020ac27a0ecSDave Kleikamp * 2021ac27a0ecSDave Kleikamp * We only do an iget() and an iput() on each inode, which is very safe if we 2022ac27a0ecSDave Kleikamp * accidentally point at an in-use or already deleted inode. The worst that 2023ac27a0ecSDave Kleikamp * can happen in this case is that we get a "bit already cleared" message from 2024617ba13bSMingming Cao * ext4_free_inode(). The only reason we would point at a wrong inode is if 2025ac27a0ecSDave Kleikamp * e2fsck was run on this filesystem, and it must have already done the orphan 2026ac27a0ecSDave Kleikamp * inode cleanup for us, so we can safely abort without any further action. 2027ac27a0ecSDave Kleikamp */ 2028617ba13bSMingming Cao static void ext4_orphan_cleanup(struct super_block *sb, 2029617ba13bSMingming Cao struct ext4_super_block *es) 2030ac27a0ecSDave Kleikamp { 2031ac27a0ecSDave Kleikamp unsigned int s_flags = sb->s_flags; 2032ac27a0ecSDave Kleikamp int nr_orphans = 0, nr_truncates = 0; 2033ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 2034ac27a0ecSDave Kleikamp int i; 2035ac27a0ecSDave Kleikamp #endif 2036ac27a0ecSDave Kleikamp if (!es->s_last_orphan) { 2037ac27a0ecSDave Kleikamp jbd_debug(4, "no orphan inodes to clean up\n"); 2038ac27a0ecSDave Kleikamp return; 2039ac27a0ecSDave Kleikamp } 2040ac27a0ecSDave Kleikamp 2041a8f48a95SEric Sandeen if (bdev_read_only(sb->s_bdev)) { 2042b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "write access " 2043b31e1552SEric Sandeen "unavailable, skipping orphan cleanup"); 2044a8f48a95SEric Sandeen return; 2045a8f48a95SEric Sandeen } 2046a8f48a95SEric Sandeen 2047d39195c3SAmir Goldstein /* Check if feature set would not allow a r/w mount */ 2048d39195c3SAmir Goldstein if (!ext4_feature_set_ok(sb, 0)) { 2049d39195c3SAmir Goldstein ext4_msg(sb, KERN_INFO, "Skipping orphan cleanup due to " 2050d39195c3SAmir Goldstein "unknown ROCOMPAT features"); 2051d39195c3SAmir Goldstein return; 2052d39195c3SAmir Goldstein } 2053d39195c3SAmir Goldstein 2054617ba13bSMingming Cao if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) { 2055ac27a0ecSDave Kleikamp if (es->s_last_orphan) 2056ac27a0ecSDave Kleikamp jbd_debug(1, "Errors on filesystem, " 2057ac27a0ecSDave Kleikamp "clearing orphan list.\n"); 2058ac27a0ecSDave Kleikamp es->s_last_orphan = 0; 2059ac27a0ecSDave Kleikamp jbd_debug(1, "Skipping orphan recovery on fs with errors.\n"); 2060ac27a0ecSDave Kleikamp return; 2061ac27a0ecSDave Kleikamp } 2062ac27a0ecSDave Kleikamp 2063ac27a0ecSDave Kleikamp if (s_flags & MS_RDONLY) { 2064b31e1552SEric Sandeen ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs"); 2065ac27a0ecSDave Kleikamp sb->s_flags &= ~MS_RDONLY; 2066ac27a0ecSDave Kleikamp } 2067ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 2068ac27a0ecSDave Kleikamp /* Needed for iput() to work correctly and not trash data */ 2069ac27a0ecSDave Kleikamp sb->s_flags |= MS_ACTIVE; 2070ac27a0ecSDave Kleikamp /* Turn on quotas so that they are updated correctly */ 2071ac27a0ecSDave Kleikamp for (i = 0; i < MAXQUOTAS; i++) { 2072617ba13bSMingming Cao if (EXT4_SB(sb)->s_qf_names[i]) { 2073617ba13bSMingming Cao int ret = ext4_quota_on_mount(sb, i); 2074ac27a0ecSDave Kleikamp if (ret < 0) 2075b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, 2076b31e1552SEric Sandeen "Cannot turn on journaled " 2077b31e1552SEric Sandeen "quota: error %d", ret); 2078ac27a0ecSDave Kleikamp } 2079ac27a0ecSDave Kleikamp } 2080ac27a0ecSDave Kleikamp #endif 2081ac27a0ecSDave Kleikamp 2082ac27a0ecSDave Kleikamp while (es->s_last_orphan) { 2083ac27a0ecSDave Kleikamp struct inode *inode; 2084ac27a0ecSDave Kleikamp 208597bd42b9SJosef Bacik inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan)); 208697bd42b9SJosef Bacik if (IS_ERR(inode)) { 2087ac27a0ecSDave Kleikamp es->s_last_orphan = 0; 2088ac27a0ecSDave Kleikamp break; 2089ac27a0ecSDave Kleikamp } 2090ac27a0ecSDave Kleikamp 2091617ba13bSMingming Cao list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan); 2092871a2931SChristoph Hellwig dquot_initialize(inode); 2093ac27a0ecSDave Kleikamp if (inode->i_nlink) { 2094b31e1552SEric Sandeen ext4_msg(sb, KERN_DEBUG, 2095b31e1552SEric Sandeen "%s: truncating inode %lu to %lld bytes", 209646e665e9SHarvey Harrison __func__, inode->i_ino, inode->i_size); 2097e5f8eab8STheodore Ts'o jbd_debug(2, "truncating inode %lu to %lld bytes\n", 2098ac27a0ecSDave Kleikamp inode->i_ino, inode->i_size); 2099617ba13bSMingming Cao ext4_truncate(inode); 2100ac27a0ecSDave Kleikamp nr_truncates++; 2101ac27a0ecSDave Kleikamp } else { 2102b31e1552SEric Sandeen ext4_msg(sb, KERN_DEBUG, 2103b31e1552SEric Sandeen "%s: deleting unreferenced inode %lu", 210446e665e9SHarvey Harrison __func__, inode->i_ino); 2105ac27a0ecSDave Kleikamp jbd_debug(2, "deleting unreferenced inode %lu\n", 2106ac27a0ecSDave Kleikamp inode->i_ino); 2107ac27a0ecSDave Kleikamp nr_orphans++; 2108ac27a0ecSDave Kleikamp } 2109ac27a0ecSDave Kleikamp iput(inode); /* The delete magic happens here! */ 2110ac27a0ecSDave Kleikamp } 2111ac27a0ecSDave Kleikamp 2112ac27a0ecSDave Kleikamp #define PLURAL(x) (x), ((x) == 1) ? "" : "s" 2113ac27a0ecSDave Kleikamp 2114ac27a0ecSDave Kleikamp if (nr_orphans) 2115b31e1552SEric Sandeen ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted", 2116b31e1552SEric Sandeen PLURAL(nr_orphans)); 2117ac27a0ecSDave Kleikamp if (nr_truncates) 2118b31e1552SEric Sandeen ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up", 2119b31e1552SEric Sandeen PLURAL(nr_truncates)); 2120ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 2121ac27a0ecSDave Kleikamp /* Turn quotas off */ 2122ac27a0ecSDave Kleikamp for (i = 0; i < MAXQUOTAS; i++) { 2123ac27a0ecSDave Kleikamp if (sb_dqopt(sb)->files[i]) 2124287a8095SChristoph Hellwig dquot_quota_off(sb, i); 2125ac27a0ecSDave Kleikamp } 2126ac27a0ecSDave Kleikamp #endif 2127ac27a0ecSDave Kleikamp sb->s_flags = s_flags; /* Restore MS_RDONLY status */ 2128ac27a0ecSDave Kleikamp } 21290b8e58a1SAndreas Dilger 2130cd2291a4SEric Sandeen /* 2131cd2291a4SEric Sandeen * Maximal extent format file size. 2132cd2291a4SEric Sandeen * Resulting logical blkno at s_maxbytes must fit in our on-disk 2133cd2291a4SEric Sandeen * extent format containers, within a sector_t, and within i_blocks 2134cd2291a4SEric Sandeen * in the vfs. ext4 inode has 48 bits of i_block in fsblock units, 2135cd2291a4SEric Sandeen * so that won't be a limiting factor. 2136cd2291a4SEric Sandeen * 2137f17722f9SLukas Czerner * However there is other limiting factor. We do store extents in the form 2138f17722f9SLukas Czerner * of starting block and length, hence the resulting length of the extent 2139f17722f9SLukas Czerner * covering maximum file size must fit into on-disk format containers as 2140f17722f9SLukas Czerner * well. Given that length is always by 1 unit bigger than max unit (because 2141f17722f9SLukas Czerner * we count 0 as well) we have to lower the s_maxbytes by one fs block. 2142f17722f9SLukas Czerner * 2143cd2291a4SEric Sandeen * Note, this does *not* consider any metadata overhead for vfs i_blocks. 2144cd2291a4SEric Sandeen */ 2145f287a1a5STheodore Ts'o static loff_t ext4_max_size(int blkbits, int has_huge_files) 2146cd2291a4SEric Sandeen { 2147cd2291a4SEric Sandeen loff_t res; 2148cd2291a4SEric Sandeen loff_t upper_limit = MAX_LFS_FILESIZE; 2149cd2291a4SEric Sandeen 2150cd2291a4SEric Sandeen /* small i_blocks in vfs inode? */ 2151f287a1a5STheodore Ts'o if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) { 2152cd2291a4SEric Sandeen /* 215390c699a9SBartlomiej Zolnierkiewicz * CONFIG_LBDAF is not enabled implies the inode 2154cd2291a4SEric Sandeen * i_block represent total blocks in 512 bytes 2155cd2291a4SEric Sandeen * 32 == size of vfs inode i_blocks * 8 2156cd2291a4SEric Sandeen */ 2157cd2291a4SEric Sandeen upper_limit = (1LL << 32) - 1; 2158cd2291a4SEric Sandeen 2159cd2291a4SEric Sandeen /* total blocks in file system block size */ 2160cd2291a4SEric Sandeen upper_limit >>= (blkbits - 9); 2161cd2291a4SEric Sandeen upper_limit <<= blkbits; 2162cd2291a4SEric Sandeen } 2163cd2291a4SEric Sandeen 2164f17722f9SLukas Czerner /* 2165f17722f9SLukas Czerner * 32-bit extent-start container, ee_block. We lower the maxbytes 2166f17722f9SLukas Czerner * by one fs block, so ee_len can cover the extent of maximum file 2167f17722f9SLukas Czerner * size 2168f17722f9SLukas Czerner */ 2169f17722f9SLukas Czerner res = (1LL << 32) - 1; 2170cd2291a4SEric Sandeen res <<= blkbits; 2171cd2291a4SEric Sandeen 2172cd2291a4SEric Sandeen /* Sanity check against vm- & vfs- imposed limits */ 2173cd2291a4SEric Sandeen if (res > upper_limit) 2174cd2291a4SEric Sandeen res = upper_limit; 2175cd2291a4SEric Sandeen 2176cd2291a4SEric Sandeen return res; 2177cd2291a4SEric Sandeen } 2178ac27a0ecSDave Kleikamp 2179ac27a0ecSDave Kleikamp /* 2180cd2291a4SEric Sandeen * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect 21810fc1b451SAneesh Kumar K.V * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks. 21820fc1b451SAneesh Kumar K.V * We need to be 1 filesystem block less than the 2^48 sector limit. 2183ac27a0ecSDave Kleikamp */ 2184f287a1a5STheodore Ts'o static loff_t ext4_max_bitmap_size(int bits, int has_huge_files) 2185ac27a0ecSDave Kleikamp { 2186617ba13bSMingming Cao loff_t res = EXT4_NDIR_BLOCKS; 21870fc1b451SAneesh Kumar K.V int meta_blocks; 21880fc1b451SAneesh Kumar K.V loff_t upper_limit; 21890b8e58a1SAndreas Dilger /* This is calculated to be the largest file size for a dense, block 21900b8e58a1SAndreas Dilger * mapped file such that the file's total number of 512-byte sectors, 21910b8e58a1SAndreas Dilger * including data and all indirect blocks, does not exceed (2^48 - 1). 21920b8e58a1SAndreas Dilger * 21930b8e58a1SAndreas Dilger * __u32 i_blocks_lo and _u16 i_blocks_high represent the total 21940b8e58a1SAndreas Dilger * number of 512-byte sectors of the file. 21950fc1b451SAneesh Kumar K.V */ 21960fc1b451SAneesh Kumar K.V 2197f287a1a5STheodore Ts'o if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) { 21980fc1b451SAneesh Kumar K.V /* 219990c699a9SBartlomiej Zolnierkiewicz * !has_huge_files or CONFIG_LBDAF not enabled implies that 22000b8e58a1SAndreas Dilger * the inode i_block field represents total file blocks in 22010b8e58a1SAndreas Dilger * 2^32 512-byte sectors == size of vfs inode i_blocks * 8 22020fc1b451SAneesh Kumar K.V */ 22030fc1b451SAneesh Kumar K.V upper_limit = (1LL << 32) - 1; 22040fc1b451SAneesh Kumar K.V 22050fc1b451SAneesh Kumar K.V /* total blocks in file system block size */ 22060fc1b451SAneesh Kumar K.V upper_limit >>= (bits - 9); 22070fc1b451SAneesh Kumar K.V 22080fc1b451SAneesh Kumar K.V } else { 22098180a562SAneesh Kumar K.V /* 22108180a562SAneesh Kumar K.V * We use 48 bit ext4_inode i_blocks 22118180a562SAneesh Kumar K.V * With EXT4_HUGE_FILE_FL set the i_blocks 22128180a562SAneesh Kumar K.V * represent total number of blocks in 22138180a562SAneesh Kumar K.V * file system block size 22148180a562SAneesh Kumar K.V */ 22150fc1b451SAneesh Kumar K.V upper_limit = (1LL << 48) - 1; 22160fc1b451SAneesh Kumar K.V 22170fc1b451SAneesh Kumar K.V } 22180fc1b451SAneesh Kumar K.V 22190fc1b451SAneesh Kumar K.V /* indirect blocks */ 22200fc1b451SAneesh Kumar K.V meta_blocks = 1; 22210fc1b451SAneesh Kumar K.V /* double indirect blocks */ 22220fc1b451SAneesh Kumar K.V meta_blocks += 1 + (1LL << (bits-2)); 22230fc1b451SAneesh Kumar K.V /* tripple indirect blocks */ 22240fc1b451SAneesh Kumar K.V meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2))); 22250fc1b451SAneesh Kumar K.V 22260fc1b451SAneesh Kumar K.V upper_limit -= meta_blocks; 22270fc1b451SAneesh Kumar K.V upper_limit <<= bits; 2228ac27a0ecSDave Kleikamp 2229ac27a0ecSDave Kleikamp res += 1LL << (bits-2); 2230ac27a0ecSDave Kleikamp res += 1LL << (2*(bits-2)); 2231ac27a0ecSDave Kleikamp res += 1LL << (3*(bits-2)); 2232ac27a0ecSDave Kleikamp res <<= bits; 2233ac27a0ecSDave Kleikamp if (res > upper_limit) 2234ac27a0ecSDave Kleikamp res = upper_limit; 22350fc1b451SAneesh Kumar K.V 22360fc1b451SAneesh Kumar K.V if (res > MAX_LFS_FILESIZE) 22370fc1b451SAneesh Kumar K.V res = MAX_LFS_FILESIZE; 22380fc1b451SAneesh Kumar K.V 2239ac27a0ecSDave Kleikamp return res; 2240ac27a0ecSDave Kleikamp } 2241ac27a0ecSDave Kleikamp 2242617ba13bSMingming Cao static ext4_fsblk_t descriptor_loc(struct super_block *sb, 224370bbb3e0SAndrew Morton ext4_fsblk_t logical_sb_block, int nr) 2244ac27a0ecSDave Kleikamp { 2245617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 2246fd2d4291SAvantika Mathur ext4_group_t bg, first_meta_bg; 2247ac27a0ecSDave Kleikamp int has_super = 0; 2248ac27a0ecSDave Kleikamp 2249ac27a0ecSDave Kleikamp first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg); 2250ac27a0ecSDave Kleikamp 2251617ba13bSMingming Cao if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) || 2252ac27a0ecSDave Kleikamp nr < first_meta_bg) 225370bbb3e0SAndrew Morton return logical_sb_block + nr + 1; 2254ac27a0ecSDave Kleikamp bg = sbi->s_desc_per_block * nr; 2255617ba13bSMingming Cao if (ext4_bg_has_super(sb, bg)) 2256ac27a0ecSDave Kleikamp has_super = 1; 22570b8e58a1SAndreas Dilger 2258617ba13bSMingming Cao return (has_super + ext4_group_first_block_no(sb, bg)); 2259ac27a0ecSDave Kleikamp } 2260ac27a0ecSDave Kleikamp 2261c9de560dSAlex Tomas /** 2262c9de560dSAlex Tomas * ext4_get_stripe_size: Get the stripe size. 2263c9de560dSAlex Tomas * @sbi: In memory super block info 2264c9de560dSAlex Tomas * 2265c9de560dSAlex Tomas * If we have specified it via mount option, then 2266c9de560dSAlex Tomas * use the mount option value. If the value specified at mount time is 2267c9de560dSAlex Tomas * greater than the blocks per group use the super block value. 2268c9de560dSAlex Tomas * If the super block value is greater than blocks per group return 0. 2269c9de560dSAlex Tomas * Allocator needs it be less than blocks per group. 2270c9de560dSAlex Tomas * 2271c9de560dSAlex Tomas */ 2272c9de560dSAlex Tomas static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi) 2273c9de560dSAlex Tomas { 2274c9de560dSAlex Tomas unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride); 2275c9de560dSAlex Tomas unsigned long stripe_width = 2276c9de560dSAlex Tomas le32_to_cpu(sbi->s_es->s_raid_stripe_width); 22773eb08658SDan Ehrenberg int ret; 2278c9de560dSAlex Tomas 2279c9de560dSAlex Tomas if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group) 22803eb08658SDan Ehrenberg ret = sbi->s_stripe; 22813eb08658SDan Ehrenberg else if (stripe_width <= sbi->s_blocks_per_group) 22823eb08658SDan Ehrenberg ret = stripe_width; 22833eb08658SDan Ehrenberg else if (stride <= sbi->s_blocks_per_group) 22843eb08658SDan Ehrenberg ret = stride; 22853eb08658SDan Ehrenberg else 22863eb08658SDan Ehrenberg ret = 0; 2287c9de560dSAlex Tomas 22883eb08658SDan Ehrenberg /* 22893eb08658SDan Ehrenberg * If the stripe width is 1, this makes no sense and 22903eb08658SDan Ehrenberg * we set it to 0 to turn off stripe handling code. 22913eb08658SDan Ehrenberg */ 22923eb08658SDan Ehrenberg if (ret <= 1) 22933eb08658SDan Ehrenberg ret = 0; 2294c9de560dSAlex Tomas 22953eb08658SDan Ehrenberg return ret; 2296c9de560dSAlex Tomas } 2297ac27a0ecSDave Kleikamp 22983197ebdbSTheodore Ts'o /* sysfs supprt */ 22993197ebdbSTheodore Ts'o 23003197ebdbSTheodore Ts'o struct ext4_attr { 23013197ebdbSTheodore Ts'o struct attribute attr; 23023197ebdbSTheodore Ts'o ssize_t (*show)(struct ext4_attr *, struct ext4_sb_info *, char *); 23033197ebdbSTheodore Ts'o ssize_t (*store)(struct ext4_attr *, struct ext4_sb_info *, 23043197ebdbSTheodore Ts'o const char *, size_t); 23053197ebdbSTheodore Ts'o int offset; 23063197ebdbSTheodore Ts'o }; 23073197ebdbSTheodore Ts'o 23083197ebdbSTheodore Ts'o static int parse_strtoul(const char *buf, 23093197ebdbSTheodore Ts'o unsigned long max, unsigned long *value) 23103197ebdbSTheodore Ts'o { 23113197ebdbSTheodore Ts'o char *endp; 23123197ebdbSTheodore Ts'o 2313e7d2860bSAndré Goddard Rosa *value = simple_strtoul(skip_spaces(buf), &endp, 0); 2314e7d2860bSAndré Goddard Rosa endp = skip_spaces(endp); 23153197ebdbSTheodore Ts'o if (*endp || *value > max) 23163197ebdbSTheodore Ts'o return -EINVAL; 23173197ebdbSTheodore Ts'o 23183197ebdbSTheodore Ts'o return 0; 23193197ebdbSTheodore Ts'o } 23203197ebdbSTheodore Ts'o 23213197ebdbSTheodore Ts'o static ssize_t delayed_allocation_blocks_show(struct ext4_attr *a, 23223197ebdbSTheodore Ts'o struct ext4_sb_info *sbi, 23233197ebdbSTheodore Ts'o char *buf) 23243197ebdbSTheodore Ts'o { 23253197ebdbSTheodore Ts'o return snprintf(buf, PAGE_SIZE, "%llu\n", 23267b415bf6SAditya Kali (s64) EXT4_C2B(sbi, 23277b415bf6SAditya Kali percpu_counter_sum(&sbi->s_dirtyclusters_counter))); 23283197ebdbSTheodore Ts'o } 23293197ebdbSTheodore Ts'o 23303197ebdbSTheodore Ts'o static ssize_t session_write_kbytes_show(struct ext4_attr *a, 23313197ebdbSTheodore Ts'o struct ext4_sb_info *sbi, char *buf) 23323197ebdbSTheodore Ts'o { 23333197ebdbSTheodore Ts'o struct super_block *sb = sbi->s_buddy_cache->i_sb; 23343197ebdbSTheodore Ts'o 2335f613dfcbSTheodore Ts'o if (!sb->s_bdev->bd_part) 2336f613dfcbSTheodore Ts'o return snprintf(buf, PAGE_SIZE, "0\n"); 23373197ebdbSTheodore Ts'o return snprintf(buf, PAGE_SIZE, "%lu\n", 23383197ebdbSTheodore Ts'o (part_stat_read(sb->s_bdev->bd_part, sectors[1]) - 23393197ebdbSTheodore Ts'o sbi->s_sectors_written_start) >> 1); 23403197ebdbSTheodore Ts'o } 23413197ebdbSTheodore Ts'o 23423197ebdbSTheodore Ts'o static ssize_t lifetime_write_kbytes_show(struct ext4_attr *a, 23433197ebdbSTheodore Ts'o struct ext4_sb_info *sbi, char *buf) 23443197ebdbSTheodore Ts'o { 23453197ebdbSTheodore Ts'o struct super_block *sb = sbi->s_buddy_cache->i_sb; 23463197ebdbSTheodore Ts'o 2347f613dfcbSTheodore Ts'o if (!sb->s_bdev->bd_part) 2348f613dfcbSTheodore Ts'o return snprintf(buf, PAGE_SIZE, "0\n"); 23493197ebdbSTheodore Ts'o return snprintf(buf, PAGE_SIZE, "%llu\n", 2350a6b43e38SAndrew Morton (unsigned long long)(sbi->s_kbytes_written + 23513197ebdbSTheodore Ts'o ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) - 2352a6b43e38SAndrew Morton EXT4_SB(sb)->s_sectors_written_start) >> 1))); 23533197ebdbSTheodore Ts'o } 23543197ebdbSTheodore Ts'o 235577f4135fSVivek Haldar static ssize_t extent_cache_hits_show(struct ext4_attr *a, 235677f4135fSVivek Haldar struct ext4_sb_info *sbi, char *buf) 235777f4135fSVivek Haldar { 235877f4135fSVivek Haldar return snprintf(buf, PAGE_SIZE, "%lu\n", sbi->extent_cache_hits); 235977f4135fSVivek Haldar } 236077f4135fSVivek Haldar 236177f4135fSVivek Haldar static ssize_t extent_cache_misses_show(struct ext4_attr *a, 236277f4135fSVivek Haldar struct ext4_sb_info *sbi, char *buf) 236377f4135fSVivek Haldar { 236477f4135fSVivek Haldar return snprintf(buf, PAGE_SIZE, "%lu\n", sbi->extent_cache_misses); 236577f4135fSVivek Haldar } 236677f4135fSVivek Haldar 23673197ebdbSTheodore Ts'o static ssize_t inode_readahead_blks_store(struct ext4_attr *a, 23683197ebdbSTheodore Ts'o struct ext4_sb_info *sbi, 23693197ebdbSTheodore Ts'o const char *buf, size_t count) 23703197ebdbSTheodore Ts'o { 23713197ebdbSTheodore Ts'o unsigned long t; 23723197ebdbSTheodore Ts'o 23733197ebdbSTheodore Ts'o if (parse_strtoul(buf, 0x40000000, &t)) 23743197ebdbSTheodore Ts'o return -EINVAL; 23753197ebdbSTheodore Ts'o 23765dbd571dSAlexander V. Lukyanov if (t && !is_power_of_2(t)) 23773197ebdbSTheodore Ts'o return -EINVAL; 23783197ebdbSTheodore Ts'o 23793197ebdbSTheodore Ts'o sbi->s_inode_readahead_blks = t; 23803197ebdbSTheodore Ts'o return count; 23813197ebdbSTheodore Ts'o } 23823197ebdbSTheodore Ts'o 23833197ebdbSTheodore Ts'o static ssize_t sbi_ui_show(struct ext4_attr *a, 23843197ebdbSTheodore Ts'o struct ext4_sb_info *sbi, char *buf) 23853197ebdbSTheodore Ts'o { 23863197ebdbSTheodore Ts'o unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset); 23873197ebdbSTheodore Ts'o 23883197ebdbSTheodore Ts'o return snprintf(buf, PAGE_SIZE, "%u\n", *ui); 23893197ebdbSTheodore Ts'o } 23903197ebdbSTheodore Ts'o 23913197ebdbSTheodore Ts'o static ssize_t sbi_ui_store(struct ext4_attr *a, 23923197ebdbSTheodore Ts'o struct ext4_sb_info *sbi, 23933197ebdbSTheodore Ts'o const char *buf, size_t count) 23943197ebdbSTheodore Ts'o { 23953197ebdbSTheodore Ts'o unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset); 23963197ebdbSTheodore Ts'o unsigned long t; 23973197ebdbSTheodore Ts'o 23983197ebdbSTheodore Ts'o if (parse_strtoul(buf, 0xffffffff, &t)) 23993197ebdbSTheodore Ts'o return -EINVAL; 24003197ebdbSTheodore Ts'o *ui = t; 24013197ebdbSTheodore Ts'o return count; 24023197ebdbSTheodore Ts'o } 24033197ebdbSTheodore Ts'o 24043197ebdbSTheodore Ts'o #define EXT4_ATTR_OFFSET(_name,_mode,_show,_store,_elname) \ 24053197ebdbSTheodore Ts'o static struct ext4_attr ext4_attr_##_name = { \ 24063197ebdbSTheodore Ts'o .attr = {.name = __stringify(_name), .mode = _mode }, \ 24073197ebdbSTheodore Ts'o .show = _show, \ 24083197ebdbSTheodore Ts'o .store = _store, \ 24093197ebdbSTheodore Ts'o .offset = offsetof(struct ext4_sb_info, _elname), \ 24103197ebdbSTheodore Ts'o } 24113197ebdbSTheodore Ts'o #define EXT4_ATTR(name, mode, show, store) \ 24123197ebdbSTheodore Ts'o static struct ext4_attr ext4_attr_##name = __ATTR(name, mode, show, store) 24133197ebdbSTheodore Ts'o 2414857ac889SLukas Czerner #define EXT4_INFO_ATTR(name) EXT4_ATTR(name, 0444, NULL, NULL) 24153197ebdbSTheodore Ts'o #define EXT4_RO_ATTR(name) EXT4_ATTR(name, 0444, name##_show, NULL) 24163197ebdbSTheodore Ts'o #define EXT4_RW_ATTR(name) EXT4_ATTR(name, 0644, name##_show, name##_store) 24173197ebdbSTheodore Ts'o #define EXT4_RW_ATTR_SBI_UI(name, elname) \ 24183197ebdbSTheodore Ts'o EXT4_ATTR_OFFSET(name, 0644, sbi_ui_show, sbi_ui_store, elname) 24193197ebdbSTheodore Ts'o #define ATTR_LIST(name) &ext4_attr_##name.attr 24203197ebdbSTheodore Ts'o 24213197ebdbSTheodore Ts'o EXT4_RO_ATTR(delayed_allocation_blocks); 24223197ebdbSTheodore Ts'o EXT4_RO_ATTR(session_write_kbytes); 24233197ebdbSTheodore Ts'o EXT4_RO_ATTR(lifetime_write_kbytes); 242477f4135fSVivek Haldar EXT4_RO_ATTR(extent_cache_hits); 242577f4135fSVivek Haldar EXT4_RO_ATTR(extent_cache_misses); 24263197ebdbSTheodore Ts'o EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, sbi_ui_show, 24273197ebdbSTheodore Ts'o inode_readahead_blks_store, s_inode_readahead_blks); 242811013911SAndreas Dilger EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal); 24293197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats); 24303197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan); 24313197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan); 24323197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs); 24333197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request); 24343197ebdbSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc); 243555138e0bSTheodore Ts'o EXT4_RW_ATTR_SBI_UI(max_writeback_mb_bump, s_max_writeback_mb_bump); 24363197ebdbSTheodore Ts'o 24373197ebdbSTheodore Ts'o static struct attribute *ext4_attrs[] = { 24383197ebdbSTheodore Ts'o ATTR_LIST(delayed_allocation_blocks), 24393197ebdbSTheodore Ts'o ATTR_LIST(session_write_kbytes), 24403197ebdbSTheodore Ts'o ATTR_LIST(lifetime_write_kbytes), 244177f4135fSVivek Haldar ATTR_LIST(extent_cache_hits), 244277f4135fSVivek Haldar ATTR_LIST(extent_cache_misses), 24433197ebdbSTheodore Ts'o ATTR_LIST(inode_readahead_blks), 244411013911SAndreas Dilger ATTR_LIST(inode_goal), 24453197ebdbSTheodore Ts'o ATTR_LIST(mb_stats), 24463197ebdbSTheodore Ts'o ATTR_LIST(mb_max_to_scan), 24473197ebdbSTheodore Ts'o ATTR_LIST(mb_min_to_scan), 24483197ebdbSTheodore Ts'o ATTR_LIST(mb_order2_req), 24493197ebdbSTheodore Ts'o ATTR_LIST(mb_stream_req), 24503197ebdbSTheodore Ts'o ATTR_LIST(mb_group_prealloc), 245155138e0bSTheodore Ts'o ATTR_LIST(max_writeback_mb_bump), 24523197ebdbSTheodore Ts'o NULL, 24533197ebdbSTheodore Ts'o }; 24543197ebdbSTheodore Ts'o 2455857ac889SLukas Czerner /* Features this copy of ext4 supports */ 2456857ac889SLukas Czerner EXT4_INFO_ATTR(lazy_itable_init); 245727ee40dfSLukas Czerner EXT4_INFO_ATTR(batched_discard); 2458857ac889SLukas Czerner 2459857ac889SLukas Czerner static struct attribute *ext4_feat_attrs[] = { 2460857ac889SLukas Czerner ATTR_LIST(lazy_itable_init), 246127ee40dfSLukas Czerner ATTR_LIST(batched_discard), 2462857ac889SLukas Czerner NULL, 2463857ac889SLukas Czerner }; 2464857ac889SLukas Czerner 24653197ebdbSTheodore Ts'o static ssize_t ext4_attr_show(struct kobject *kobj, 24663197ebdbSTheodore Ts'o struct attribute *attr, char *buf) 24673197ebdbSTheodore Ts'o { 24683197ebdbSTheodore Ts'o struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info, 24693197ebdbSTheodore Ts'o s_kobj); 24703197ebdbSTheodore Ts'o struct ext4_attr *a = container_of(attr, struct ext4_attr, attr); 24713197ebdbSTheodore Ts'o 24723197ebdbSTheodore Ts'o return a->show ? a->show(a, sbi, buf) : 0; 24733197ebdbSTheodore Ts'o } 24743197ebdbSTheodore Ts'o 24753197ebdbSTheodore Ts'o static ssize_t ext4_attr_store(struct kobject *kobj, 24763197ebdbSTheodore Ts'o struct attribute *attr, 24773197ebdbSTheodore Ts'o const char *buf, size_t len) 24783197ebdbSTheodore Ts'o { 24793197ebdbSTheodore Ts'o struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info, 24803197ebdbSTheodore Ts'o s_kobj); 24813197ebdbSTheodore Ts'o struct ext4_attr *a = container_of(attr, struct ext4_attr, attr); 24823197ebdbSTheodore Ts'o 24833197ebdbSTheodore Ts'o return a->store ? a->store(a, sbi, buf, len) : 0; 24843197ebdbSTheodore Ts'o } 24853197ebdbSTheodore Ts'o 24863197ebdbSTheodore Ts'o static void ext4_sb_release(struct kobject *kobj) 24873197ebdbSTheodore Ts'o { 24883197ebdbSTheodore Ts'o struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info, 24893197ebdbSTheodore Ts'o s_kobj); 24903197ebdbSTheodore Ts'o complete(&sbi->s_kobj_unregister); 24913197ebdbSTheodore Ts'o } 24923197ebdbSTheodore Ts'o 249352cf25d0SEmese Revfy static const struct sysfs_ops ext4_attr_ops = { 24943197ebdbSTheodore Ts'o .show = ext4_attr_show, 24953197ebdbSTheodore Ts'o .store = ext4_attr_store, 24963197ebdbSTheodore Ts'o }; 24973197ebdbSTheodore Ts'o 24983197ebdbSTheodore Ts'o static struct kobj_type ext4_ktype = { 24993197ebdbSTheodore Ts'o .default_attrs = ext4_attrs, 25003197ebdbSTheodore Ts'o .sysfs_ops = &ext4_attr_ops, 25013197ebdbSTheodore Ts'o .release = ext4_sb_release, 25023197ebdbSTheodore Ts'o }; 25033197ebdbSTheodore Ts'o 2504857ac889SLukas Czerner static void ext4_feat_release(struct kobject *kobj) 2505857ac889SLukas Czerner { 2506857ac889SLukas Czerner complete(&ext4_feat->f_kobj_unregister); 2507857ac889SLukas Czerner } 2508857ac889SLukas Czerner 2509857ac889SLukas Czerner static struct kobj_type ext4_feat_ktype = { 2510857ac889SLukas Czerner .default_attrs = ext4_feat_attrs, 2511857ac889SLukas Czerner .sysfs_ops = &ext4_attr_ops, 2512857ac889SLukas Czerner .release = ext4_feat_release, 2513857ac889SLukas Czerner }; 2514857ac889SLukas Czerner 2515a13fb1a4SEric Sandeen /* 2516a13fb1a4SEric Sandeen * Check whether this filesystem can be mounted based on 2517a13fb1a4SEric Sandeen * the features present and the RDONLY/RDWR mount requested. 2518a13fb1a4SEric Sandeen * Returns 1 if this filesystem can be mounted as requested, 2519a13fb1a4SEric Sandeen * 0 if it cannot be. 2520a13fb1a4SEric Sandeen */ 2521a13fb1a4SEric Sandeen static int ext4_feature_set_ok(struct super_block *sb, int readonly) 2522a13fb1a4SEric Sandeen { 2523a13fb1a4SEric Sandeen if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP)) { 2524a13fb1a4SEric Sandeen ext4_msg(sb, KERN_ERR, 2525a13fb1a4SEric Sandeen "Couldn't mount because of " 2526a13fb1a4SEric Sandeen "unsupported optional features (%x)", 2527a13fb1a4SEric Sandeen (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) & 2528a13fb1a4SEric Sandeen ~EXT4_FEATURE_INCOMPAT_SUPP)); 2529a13fb1a4SEric Sandeen return 0; 2530a13fb1a4SEric Sandeen } 2531a13fb1a4SEric Sandeen 2532a13fb1a4SEric Sandeen if (readonly) 2533a13fb1a4SEric Sandeen return 1; 2534a13fb1a4SEric Sandeen 2535a13fb1a4SEric Sandeen /* Check that feature set is OK for a read-write mount */ 2536a13fb1a4SEric Sandeen if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP)) { 2537a13fb1a4SEric Sandeen ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of " 2538a13fb1a4SEric Sandeen "unsupported optional features (%x)", 2539a13fb1a4SEric Sandeen (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) & 2540a13fb1a4SEric Sandeen ~EXT4_FEATURE_RO_COMPAT_SUPP)); 2541a13fb1a4SEric Sandeen return 0; 2542a13fb1a4SEric Sandeen } 2543a13fb1a4SEric Sandeen /* 2544a13fb1a4SEric Sandeen * Large file size enabled file system can only be mounted 2545a13fb1a4SEric Sandeen * read-write on 32-bit systems if kernel is built with CONFIG_LBDAF 2546a13fb1a4SEric Sandeen */ 2547a13fb1a4SEric Sandeen if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) { 2548a13fb1a4SEric Sandeen if (sizeof(blkcnt_t) < sizeof(u64)) { 2549a13fb1a4SEric Sandeen ext4_msg(sb, KERN_ERR, "Filesystem with huge files " 2550a13fb1a4SEric Sandeen "cannot be mounted RDWR without " 2551a13fb1a4SEric Sandeen "CONFIG_LBDAF"); 2552a13fb1a4SEric Sandeen return 0; 2553a13fb1a4SEric Sandeen } 2554a13fb1a4SEric Sandeen } 2555bab08ab9STheodore Ts'o if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_BIGALLOC) && 2556bab08ab9STheodore Ts'o !EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) { 2557bab08ab9STheodore Ts'o ext4_msg(sb, KERN_ERR, 2558bab08ab9STheodore Ts'o "Can't support bigalloc feature without " 2559bab08ab9STheodore Ts'o "extents feature\n"); 2560bab08ab9STheodore Ts'o return 0; 2561bab08ab9STheodore Ts'o } 2562a13fb1a4SEric Sandeen return 1; 2563a13fb1a4SEric Sandeen } 2564a13fb1a4SEric Sandeen 256566e61a9eSTheodore Ts'o /* 256666e61a9eSTheodore Ts'o * This function is called once a day if we have errors logged 256766e61a9eSTheodore Ts'o * on the file system 256866e61a9eSTheodore Ts'o */ 256966e61a9eSTheodore Ts'o static void print_daily_error_info(unsigned long arg) 257066e61a9eSTheodore Ts'o { 257166e61a9eSTheodore Ts'o struct super_block *sb = (struct super_block *) arg; 257266e61a9eSTheodore Ts'o struct ext4_sb_info *sbi; 257366e61a9eSTheodore Ts'o struct ext4_super_block *es; 257466e61a9eSTheodore Ts'o 257566e61a9eSTheodore Ts'o sbi = EXT4_SB(sb); 257666e61a9eSTheodore Ts'o es = sbi->s_es; 257766e61a9eSTheodore Ts'o 257866e61a9eSTheodore Ts'o if (es->s_error_count) 257966e61a9eSTheodore Ts'o ext4_msg(sb, KERN_NOTICE, "error count: %u", 258066e61a9eSTheodore Ts'o le32_to_cpu(es->s_error_count)); 258166e61a9eSTheodore Ts'o if (es->s_first_error_time) { 258266e61a9eSTheodore Ts'o printk(KERN_NOTICE "EXT4-fs (%s): initial error at %u: %.*s:%d", 258366e61a9eSTheodore Ts'o sb->s_id, le32_to_cpu(es->s_first_error_time), 258466e61a9eSTheodore Ts'o (int) sizeof(es->s_first_error_func), 258566e61a9eSTheodore Ts'o es->s_first_error_func, 258666e61a9eSTheodore Ts'o le32_to_cpu(es->s_first_error_line)); 258766e61a9eSTheodore Ts'o if (es->s_first_error_ino) 258866e61a9eSTheodore Ts'o printk(": inode %u", 258966e61a9eSTheodore Ts'o le32_to_cpu(es->s_first_error_ino)); 259066e61a9eSTheodore Ts'o if (es->s_first_error_block) 259166e61a9eSTheodore Ts'o printk(": block %llu", (unsigned long long) 259266e61a9eSTheodore Ts'o le64_to_cpu(es->s_first_error_block)); 259366e61a9eSTheodore Ts'o printk("\n"); 259466e61a9eSTheodore Ts'o } 259566e61a9eSTheodore Ts'o if (es->s_last_error_time) { 259666e61a9eSTheodore Ts'o printk(KERN_NOTICE "EXT4-fs (%s): last error at %u: %.*s:%d", 259766e61a9eSTheodore Ts'o sb->s_id, le32_to_cpu(es->s_last_error_time), 259866e61a9eSTheodore Ts'o (int) sizeof(es->s_last_error_func), 259966e61a9eSTheodore Ts'o es->s_last_error_func, 260066e61a9eSTheodore Ts'o le32_to_cpu(es->s_last_error_line)); 260166e61a9eSTheodore Ts'o if (es->s_last_error_ino) 260266e61a9eSTheodore Ts'o printk(": inode %u", 260366e61a9eSTheodore Ts'o le32_to_cpu(es->s_last_error_ino)); 260466e61a9eSTheodore Ts'o if (es->s_last_error_block) 260566e61a9eSTheodore Ts'o printk(": block %llu", (unsigned long long) 260666e61a9eSTheodore Ts'o le64_to_cpu(es->s_last_error_block)); 260766e61a9eSTheodore Ts'o printk("\n"); 260866e61a9eSTheodore Ts'o } 260966e61a9eSTheodore Ts'o mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ); /* Once a day */ 261066e61a9eSTheodore Ts'o } 261166e61a9eSTheodore Ts'o 2612bfff6873SLukas Czerner /* Find next suitable group and run ext4_init_inode_table */ 2613bfff6873SLukas Czerner static int ext4_run_li_request(struct ext4_li_request *elr) 2614bfff6873SLukas Czerner { 2615bfff6873SLukas Czerner struct ext4_group_desc *gdp = NULL; 2616bfff6873SLukas Czerner ext4_group_t group, ngroups; 2617bfff6873SLukas Czerner struct super_block *sb; 2618bfff6873SLukas Czerner unsigned long timeout = 0; 2619bfff6873SLukas Czerner int ret = 0; 2620bfff6873SLukas Czerner 2621bfff6873SLukas Czerner sb = elr->lr_super; 2622bfff6873SLukas Czerner ngroups = EXT4_SB(sb)->s_groups_count; 2623bfff6873SLukas Czerner 2624bfff6873SLukas Czerner for (group = elr->lr_next_group; group < ngroups; group++) { 2625bfff6873SLukas Czerner gdp = ext4_get_group_desc(sb, group, NULL); 2626bfff6873SLukas Czerner if (!gdp) { 2627bfff6873SLukas Czerner ret = 1; 2628bfff6873SLukas Czerner break; 2629bfff6873SLukas Czerner } 2630bfff6873SLukas Czerner 2631bfff6873SLukas Czerner if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED))) 2632bfff6873SLukas Czerner break; 2633bfff6873SLukas Czerner } 2634bfff6873SLukas Czerner 2635bfff6873SLukas Czerner if (group == ngroups) 2636bfff6873SLukas Czerner ret = 1; 2637bfff6873SLukas Czerner 2638bfff6873SLukas Czerner if (!ret) { 2639bfff6873SLukas Czerner timeout = jiffies; 2640bfff6873SLukas Czerner ret = ext4_init_inode_table(sb, group, 2641bfff6873SLukas Czerner elr->lr_timeout ? 0 : 1); 2642bfff6873SLukas Czerner if (elr->lr_timeout == 0) { 264351ce6511SLukas Czerner timeout = (jiffies - timeout) * 264451ce6511SLukas Czerner elr->lr_sbi->s_li_wait_mult; 2645bfff6873SLukas Czerner elr->lr_timeout = timeout; 2646bfff6873SLukas Czerner } 2647bfff6873SLukas Czerner elr->lr_next_sched = jiffies + elr->lr_timeout; 2648bfff6873SLukas Czerner elr->lr_next_group = group + 1; 2649bfff6873SLukas Czerner } 2650bfff6873SLukas Czerner 2651bfff6873SLukas Czerner return ret; 2652bfff6873SLukas Czerner } 2653bfff6873SLukas Czerner 2654bfff6873SLukas Czerner /* 2655bfff6873SLukas Czerner * Remove lr_request from the list_request and free the 26564ed5c033SLukas Czerner * request structure. Should be called with li_list_mtx held 2657bfff6873SLukas Czerner */ 2658bfff6873SLukas Czerner static void ext4_remove_li_request(struct ext4_li_request *elr) 2659bfff6873SLukas Czerner { 2660bfff6873SLukas Czerner struct ext4_sb_info *sbi; 2661bfff6873SLukas Czerner 2662bfff6873SLukas Czerner if (!elr) 2663bfff6873SLukas Czerner return; 2664bfff6873SLukas Czerner 2665bfff6873SLukas Czerner sbi = elr->lr_sbi; 2666bfff6873SLukas Czerner 2667bfff6873SLukas Czerner list_del(&elr->lr_request); 2668bfff6873SLukas Czerner sbi->s_li_request = NULL; 2669bfff6873SLukas Czerner kfree(elr); 2670bfff6873SLukas Czerner } 2671bfff6873SLukas Czerner 2672bfff6873SLukas Czerner static void ext4_unregister_li_request(struct super_block *sb) 2673bfff6873SLukas Czerner { 26741bb933fbSLukas Czerner mutex_lock(&ext4_li_mtx); 26751bb933fbSLukas Czerner if (!ext4_li_info) { 26761bb933fbSLukas Czerner mutex_unlock(&ext4_li_mtx); 2677bfff6873SLukas Czerner return; 26781bb933fbSLukas Czerner } 2679bfff6873SLukas Czerner 2680bfff6873SLukas Czerner mutex_lock(&ext4_li_info->li_list_mtx); 26811bb933fbSLukas Czerner ext4_remove_li_request(EXT4_SB(sb)->s_li_request); 2682bfff6873SLukas Czerner mutex_unlock(&ext4_li_info->li_list_mtx); 26831bb933fbSLukas Czerner mutex_unlock(&ext4_li_mtx); 2684bfff6873SLukas Czerner } 2685bfff6873SLukas Czerner 26868f1f7453SEric Sandeen static struct task_struct *ext4_lazyinit_task; 26878f1f7453SEric Sandeen 2688bfff6873SLukas Czerner /* 2689bfff6873SLukas Czerner * This is the function where ext4lazyinit thread lives. It walks 2690bfff6873SLukas Czerner * through the request list searching for next scheduled filesystem. 2691bfff6873SLukas Czerner * When such a fs is found, run the lazy initialization request 2692bfff6873SLukas Czerner * (ext4_rn_li_request) and keep track of the time spend in this 2693bfff6873SLukas Czerner * function. Based on that time we compute next schedule time of 2694bfff6873SLukas Czerner * the request. When walking through the list is complete, compute 2695bfff6873SLukas Czerner * next waking time and put itself into sleep. 2696bfff6873SLukas Czerner */ 2697bfff6873SLukas Czerner static int ext4_lazyinit_thread(void *arg) 2698bfff6873SLukas Czerner { 2699bfff6873SLukas Czerner struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg; 2700bfff6873SLukas Czerner struct list_head *pos, *n; 2701bfff6873SLukas Czerner struct ext4_li_request *elr; 27024ed5c033SLukas Czerner unsigned long next_wakeup, cur; 2703bfff6873SLukas Czerner 2704bfff6873SLukas Czerner BUG_ON(NULL == eli); 2705bfff6873SLukas Czerner 2706bfff6873SLukas Czerner cont_thread: 2707bfff6873SLukas Czerner while (true) { 2708bfff6873SLukas Czerner next_wakeup = MAX_JIFFY_OFFSET; 2709bfff6873SLukas Czerner 2710bfff6873SLukas Czerner mutex_lock(&eli->li_list_mtx); 2711bfff6873SLukas Czerner if (list_empty(&eli->li_request_list)) { 2712bfff6873SLukas Czerner mutex_unlock(&eli->li_list_mtx); 2713bfff6873SLukas Czerner goto exit_thread; 2714bfff6873SLukas Czerner } 2715bfff6873SLukas Czerner 2716bfff6873SLukas Czerner list_for_each_safe(pos, n, &eli->li_request_list) { 2717bfff6873SLukas Czerner elr = list_entry(pos, struct ext4_li_request, 2718bfff6873SLukas Czerner lr_request); 2719bfff6873SLukas Czerner 2720b2c78cd0STheodore Ts'o if (time_after_eq(jiffies, elr->lr_next_sched)) { 2721b2c78cd0STheodore Ts'o if (ext4_run_li_request(elr) != 0) { 2722b2c78cd0STheodore Ts'o /* error, remove the lazy_init job */ 2723bfff6873SLukas Czerner ext4_remove_li_request(elr); 2724bfff6873SLukas Czerner continue; 2725bfff6873SLukas Czerner } 2726b2c78cd0STheodore Ts'o } 2727bfff6873SLukas Czerner 2728bfff6873SLukas Czerner if (time_before(elr->lr_next_sched, next_wakeup)) 2729bfff6873SLukas Czerner next_wakeup = elr->lr_next_sched; 2730bfff6873SLukas Czerner } 2731bfff6873SLukas Czerner mutex_unlock(&eli->li_list_mtx); 2732bfff6873SLukas Czerner 2733a0acae0eSTejun Heo try_to_freeze(); 2734bfff6873SLukas Czerner 27354ed5c033SLukas Czerner cur = jiffies; 27364ed5c033SLukas Czerner if ((time_after_eq(cur, next_wakeup)) || 2737f4245bd4SLukas Czerner (MAX_JIFFY_OFFSET == next_wakeup)) { 2738bfff6873SLukas Czerner cond_resched(); 2739bfff6873SLukas Czerner continue; 2740bfff6873SLukas Czerner } 2741bfff6873SLukas Czerner 27424ed5c033SLukas Czerner schedule_timeout_interruptible(next_wakeup - cur); 27434ed5c033SLukas Czerner 27448f1f7453SEric Sandeen if (kthread_should_stop()) { 27458f1f7453SEric Sandeen ext4_clear_request_list(); 27468f1f7453SEric Sandeen goto exit_thread; 27478f1f7453SEric Sandeen } 2748bfff6873SLukas Czerner } 2749bfff6873SLukas Czerner 2750bfff6873SLukas Czerner exit_thread: 2751bfff6873SLukas Czerner /* 2752bfff6873SLukas Czerner * It looks like the request list is empty, but we need 2753bfff6873SLukas Czerner * to check it under the li_list_mtx lock, to prevent any 2754bfff6873SLukas Czerner * additions into it, and of course we should lock ext4_li_mtx 2755bfff6873SLukas Czerner * to atomically free the list and ext4_li_info, because at 2756bfff6873SLukas Czerner * this point another ext4 filesystem could be registering 2757bfff6873SLukas Czerner * new one. 2758bfff6873SLukas Czerner */ 2759bfff6873SLukas Czerner mutex_lock(&ext4_li_mtx); 2760bfff6873SLukas Czerner mutex_lock(&eli->li_list_mtx); 2761bfff6873SLukas Czerner if (!list_empty(&eli->li_request_list)) { 2762bfff6873SLukas Czerner mutex_unlock(&eli->li_list_mtx); 2763bfff6873SLukas Czerner mutex_unlock(&ext4_li_mtx); 2764bfff6873SLukas Czerner goto cont_thread; 2765bfff6873SLukas Czerner } 2766bfff6873SLukas Czerner mutex_unlock(&eli->li_list_mtx); 2767bfff6873SLukas Czerner kfree(ext4_li_info); 2768bfff6873SLukas Czerner ext4_li_info = NULL; 2769bfff6873SLukas Czerner mutex_unlock(&ext4_li_mtx); 2770bfff6873SLukas Czerner 2771bfff6873SLukas Czerner return 0; 2772bfff6873SLukas Czerner } 2773bfff6873SLukas Czerner 2774bfff6873SLukas Czerner static void ext4_clear_request_list(void) 2775bfff6873SLukas Czerner { 2776bfff6873SLukas Czerner struct list_head *pos, *n; 2777bfff6873SLukas Czerner struct ext4_li_request *elr; 2778bfff6873SLukas Czerner 2779bfff6873SLukas Czerner mutex_lock(&ext4_li_info->li_list_mtx); 2780bfff6873SLukas Czerner list_for_each_safe(pos, n, &ext4_li_info->li_request_list) { 2781bfff6873SLukas Czerner elr = list_entry(pos, struct ext4_li_request, 2782bfff6873SLukas Czerner lr_request); 2783bfff6873SLukas Czerner ext4_remove_li_request(elr); 2784bfff6873SLukas Czerner } 2785bfff6873SLukas Czerner mutex_unlock(&ext4_li_info->li_list_mtx); 2786bfff6873SLukas Czerner } 2787bfff6873SLukas Czerner 2788bfff6873SLukas Czerner static int ext4_run_lazyinit_thread(void) 2789bfff6873SLukas Czerner { 27908f1f7453SEric Sandeen ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread, 27918f1f7453SEric Sandeen ext4_li_info, "ext4lazyinit"); 27928f1f7453SEric Sandeen if (IS_ERR(ext4_lazyinit_task)) { 27938f1f7453SEric Sandeen int err = PTR_ERR(ext4_lazyinit_task); 2794bfff6873SLukas Czerner ext4_clear_request_list(); 2795bfff6873SLukas Czerner kfree(ext4_li_info); 2796bfff6873SLukas Czerner ext4_li_info = NULL; 2797bfff6873SLukas Czerner printk(KERN_CRIT "EXT4: error %d creating inode table " 2798bfff6873SLukas Czerner "initialization thread\n", 2799bfff6873SLukas Czerner err); 2800bfff6873SLukas Czerner return err; 2801bfff6873SLukas Czerner } 2802bfff6873SLukas Czerner ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING; 2803bfff6873SLukas Czerner return 0; 2804bfff6873SLukas Czerner } 2805bfff6873SLukas Czerner 2806bfff6873SLukas Czerner /* 2807bfff6873SLukas Czerner * Check whether it make sense to run itable init. thread or not. 2808bfff6873SLukas Czerner * If there is at least one uninitialized inode table, return 2809bfff6873SLukas Czerner * corresponding group number, else the loop goes through all 2810bfff6873SLukas Czerner * groups and return total number of groups. 2811bfff6873SLukas Czerner */ 2812bfff6873SLukas Czerner static ext4_group_t ext4_has_uninit_itable(struct super_block *sb) 2813bfff6873SLukas Czerner { 2814bfff6873SLukas Czerner ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count; 2815bfff6873SLukas Czerner struct ext4_group_desc *gdp = NULL; 2816bfff6873SLukas Czerner 2817bfff6873SLukas Czerner for (group = 0; group < ngroups; group++) { 2818bfff6873SLukas Czerner gdp = ext4_get_group_desc(sb, group, NULL); 2819bfff6873SLukas Czerner if (!gdp) 2820bfff6873SLukas Czerner continue; 2821bfff6873SLukas Czerner 2822bfff6873SLukas Czerner if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED))) 2823bfff6873SLukas Czerner break; 2824bfff6873SLukas Czerner } 2825bfff6873SLukas Czerner 2826bfff6873SLukas Czerner return group; 2827bfff6873SLukas Czerner } 2828bfff6873SLukas Czerner 2829bfff6873SLukas Czerner static int ext4_li_info_new(void) 2830bfff6873SLukas Czerner { 2831bfff6873SLukas Czerner struct ext4_lazy_init *eli = NULL; 2832bfff6873SLukas Czerner 2833bfff6873SLukas Czerner eli = kzalloc(sizeof(*eli), GFP_KERNEL); 2834bfff6873SLukas Czerner if (!eli) 2835bfff6873SLukas Czerner return -ENOMEM; 2836bfff6873SLukas Czerner 2837bfff6873SLukas Czerner INIT_LIST_HEAD(&eli->li_request_list); 2838bfff6873SLukas Czerner mutex_init(&eli->li_list_mtx); 2839bfff6873SLukas Czerner 2840bfff6873SLukas Czerner eli->li_state |= EXT4_LAZYINIT_QUIT; 2841bfff6873SLukas Czerner 2842bfff6873SLukas Czerner ext4_li_info = eli; 2843bfff6873SLukas Czerner 2844bfff6873SLukas Czerner return 0; 2845bfff6873SLukas Czerner } 2846bfff6873SLukas Czerner 2847bfff6873SLukas Czerner static struct ext4_li_request *ext4_li_request_new(struct super_block *sb, 2848bfff6873SLukas Czerner ext4_group_t start) 2849bfff6873SLukas Czerner { 2850bfff6873SLukas Czerner struct ext4_sb_info *sbi = EXT4_SB(sb); 2851bfff6873SLukas Czerner struct ext4_li_request *elr; 2852bfff6873SLukas Czerner unsigned long rnd; 2853bfff6873SLukas Czerner 2854bfff6873SLukas Czerner elr = kzalloc(sizeof(*elr), GFP_KERNEL); 2855bfff6873SLukas Czerner if (!elr) 2856bfff6873SLukas Czerner return NULL; 2857bfff6873SLukas Czerner 2858bfff6873SLukas Czerner elr->lr_super = sb; 2859bfff6873SLukas Czerner elr->lr_sbi = sbi; 2860bfff6873SLukas Czerner elr->lr_next_group = start; 2861bfff6873SLukas Czerner 2862bfff6873SLukas Czerner /* 2863bfff6873SLukas Czerner * Randomize first schedule time of the request to 2864bfff6873SLukas Czerner * spread the inode table initialization requests 2865bfff6873SLukas Czerner * better. 2866bfff6873SLukas Czerner */ 2867bfff6873SLukas Czerner get_random_bytes(&rnd, sizeof(rnd)); 2868bfff6873SLukas Czerner elr->lr_next_sched = jiffies + (unsigned long)rnd % 2869bfff6873SLukas Czerner (EXT4_DEF_LI_MAX_START_DELAY * HZ); 2870bfff6873SLukas Czerner 2871bfff6873SLukas Czerner return elr; 2872bfff6873SLukas Czerner } 2873bfff6873SLukas Czerner 2874bfff6873SLukas Czerner static int ext4_register_li_request(struct super_block *sb, 2875bfff6873SLukas Czerner ext4_group_t first_not_zeroed) 2876bfff6873SLukas Czerner { 2877bfff6873SLukas Czerner struct ext4_sb_info *sbi = EXT4_SB(sb); 2878bfff6873SLukas Czerner struct ext4_li_request *elr; 2879bfff6873SLukas Czerner ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count; 28806c5a6cb9SAndrew Morton int ret = 0; 2881bfff6873SLukas Czerner 288251ce6511SLukas Czerner if (sbi->s_li_request != NULL) { 288351ce6511SLukas Czerner /* 288451ce6511SLukas Czerner * Reset timeout so it can be computed again, because 288551ce6511SLukas Czerner * s_li_wait_mult might have changed. 288651ce6511SLukas Czerner */ 288751ce6511SLukas Czerner sbi->s_li_request->lr_timeout = 0; 2888beed5ecbSNicolas Kaiser return 0; 288951ce6511SLukas Czerner } 2890bfff6873SLukas Czerner 2891bfff6873SLukas Czerner if (first_not_zeroed == ngroups || 2892bfff6873SLukas Czerner (sb->s_flags & MS_RDONLY) || 289355ff3840STao Ma !test_opt(sb, INIT_INODE_TABLE)) 2894beed5ecbSNicolas Kaiser return 0; 2895bfff6873SLukas Czerner 2896bfff6873SLukas Czerner elr = ext4_li_request_new(sb, first_not_zeroed); 2897beed5ecbSNicolas Kaiser if (!elr) 2898beed5ecbSNicolas Kaiser return -ENOMEM; 2899bfff6873SLukas Czerner 2900bfff6873SLukas Czerner mutex_lock(&ext4_li_mtx); 2901bfff6873SLukas Czerner 2902bfff6873SLukas Czerner if (NULL == ext4_li_info) { 2903bfff6873SLukas Czerner ret = ext4_li_info_new(); 2904bfff6873SLukas Czerner if (ret) 2905bfff6873SLukas Czerner goto out; 2906bfff6873SLukas Czerner } 2907bfff6873SLukas Czerner 2908bfff6873SLukas Czerner mutex_lock(&ext4_li_info->li_list_mtx); 2909bfff6873SLukas Czerner list_add(&elr->lr_request, &ext4_li_info->li_request_list); 2910bfff6873SLukas Czerner mutex_unlock(&ext4_li_info->li_list_mtx); 2911bfff6873SLukas Czerner 2912bfff6873SLukas Czerner sbi->s_li_request = elr; 291346e4690bSTao Ma /* 291446e4690bSTao Ma * set elr to NULL here since it has been inserted to 291546e4690bSTao Ma * the request_list and the removal and free of it is 291646e4690bSTao Ma * handled by ext4_clear_request_list from now on. 291746e4690bSTao Ma */ 291846e4690bSTao Ma elr = NULL; 2919bfff6873SLukas Czerner 2920bfff6873SLukas Czerner if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) { 2921bfff6873SLukas Czerner ret = ext4_run_lazyinit_thread(); 2922bfff6873SLukas Czerner if (ret) 2923bfff6873SLukas Czerner goto out; 2924bfff6873SLukas Czerner } 2925bfff6873SLukas Czerner out: 2926bfff6873SLukas Czerner mutex_unlock(&ext4_li_mtx); 2927beed5ecbSNicolas Kaiser if (ret) 2928bfff6873SLukas Czerner kfree(elr); 2929bfff6873SLukas Czerner return ret; 2930bfff6873SLukas Czerner } 2931bfff6873SLukas Czerner 2932bfff6873SLukas Czerner /* 2933bfff6873SLukas Czerner * We do not need to lock anything since this is called on 2934bfff6873SLukas Czerner * module unload. 2935bfff6873SLukas Czerner */ 2936bfff6873SLukas Czerner static void ext4_destroy_lazyinit_thread(void) 2937bfff6873SLukas Czerner { 2938bfff6873SLukas Czerner /* 2939bfff6873SLukas Czerner * If thread exited earlier 2940bfff6873SLukas Czerner * there's nothing to be done. 2941bfff6873SLukas Czerner */ 29428f1f7453SEric Sandeen if (!ext4_li_info || !ext4_lazyinit_task) 2943bfff6873SLukas Czerner return; 2944bfff6873SLukas Czerner 29458f1f7453SEric Sandeen kthread_stop(ext4_lazyinit_task); 2946bfff6873SLukas Czerner } 2947bfff6873SLukas Czerner 2948617ba13bSMingming Cao static int ext4_fill_super(struct super_block *sb, void *data, int silent) 2949ac27a0ecSDave Kleikamp { 2950d4c402d9SCurt Wohlgemuth char *orig_data = kstrdup(data, GFP_KERNEL); 2951ac27a0ecSDave Kleikamp struct buffer_head *bh; 2952617ba13bSMingming Cao struct ext4_super_block *es = NULL; 2953617ba13bSMingming Cao struct ext4_sb_info *sbi; 2954617ba13bSMingming Cao ext4_fsblk_t block; 2955617ba13bSMingming Cao ext4_fsblk_t sb_block = get_sb_block(&data); 295670bbb3e0SAndrew Morton ext4_fsblk_t logical_sb_block; 2957ac27a0ecSDave Kleikamp unsigned long offset = 0; 2958ac27a0ecSDave Kleikamp unsigned long journal_devnum = 0; 2959ac27a0ecSDave Kleikamp unsigned long def_mount_opts; 2960ac27a0ecSDave Kleikamp struct inode *root; 29619f6200bbSTheodore Ts'o char *cp; 29620390131bSFrank Mayhar const char *descr; 2963dcc7dae3SCyrill Gorcunov int ret = -ENOMEM; 2964281b5995STheodore Ts'o int blocksize, clustersize; 29654ec11028STheodore Ts'o unsigned int db_count; 29664ec11028STheodore Ts'o unsigned int i; 2967281b5995STheodore Ts'o int needs_recovery, has_huge_files, has_bigalloc; 2968bd81d8eeSLaurent Vivier __u64 blocks_count; 2969833f4077SPeter Zijlstra int err; 2970b3881f74STheodore Ts'o unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO; 2971bfff6873SLukas Czerner ext4_group_t first_not_zeroed; 2972ac27a0ecSDave Kleikamp 2973ac27a0ecSDave Kleikamp sbi = kzalloc(sizeof(*sbi), GFP_KERNEL); 2974ac27a0ecSDave Kleikamp if (!sbi) 2975dcc7dae3SCyrill Gorcunov goto out_free_orig; 2976705895b6SPekka Enberg 2977705895b6SPekka Enberg sbi->s_blockgroup_lock = 2978705895b6SPekka Enberg kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL); 2979705895b6SPekka Enberg if (!sbi->s_blockgroup_lock) { 2980705895b6SPekka Enberg kfree(sbi); 2981dcc7dae3SCyrill Gorcunov goto out_free_orig; 2982705895b6SPekka Enberg } 2983ac27a0ecSDave Kleikamp sb->s_fs_info = sbi; 2984ac27a0ecSDave Kleikamp sbi->s_mount_opt = 0; 2985617ba13bSMingming Cao sbi->s_resuid = EXT4_DEF_RESUID; 2986617ba13bSMingming Cao sbi->s_resgid = EXT4_DEF_RESGID; 2987240799cdSTheodore Ts'o sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS; 2988d9c9bef1SMiklos Szeredi sbi->s_sb_block = sb_block; 2989f613dfcbSTheodore Ts'o if (sb->s_bdev->bd_part) 2990f613dfcbSTheodore Ts'o sbi->s_sectors_written_start = 2991f613dfcbSTheodore Ts'o part_stat_read(sb->s_bdev->bd_part, sectors[1]); 2992ac27a0ecSDave Kleikamp 29939f6200bbSTheodore Ts'o /* Cleanup superblock name */ 29949f6200bbSTheodore Ts'o for (cp = sb->s_id; (cp = strchr(cp, '/'));) 29959f6200bbSTheodore Ts'o *cp = '!'; 29969f6200bbSTheodore Ts'o 2997dcc7dae3SCyrill Gorcunov ret = -EINVAL; 2998617ba13bSMingming Cao blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE); 2999ac27a0ecSDave Kleikamp if (!blocksize) { 3000b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "unable to set blocksize"); 3001ac27a0ecSDave Kleikamp goto out_fail; 3002ac27a0ecSDave Kleikamp } 3003ac27a0ecSDave Kleikamp 3004ac27a0ecSDave Kleikamp /* 3005617ba13bSMingming Cao * The ext4 superblock will not be buffer aligned for other than 1kB 3006ac27a0ecSDave Kleikamp * block sizes. We need to calculate the offset from buffer start. 3007ac27a0ecSDave Kleikamp */ 3008617ba13bSMingming Cao if (blocksize != EXT4_MIN_BLOCK_SIZE) { 300970bbb3e0SAndrew Morton logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE; 301070bbb3e0SAndrew Morton offset = do_div(logical_sb_block, blocksize); 3011ac27a0ecSDave Kleikamp } else { 301270bbb3e0SAndrew Morton logical_sb_block = sb_block; 3013ac27a0ecSDave Kleikamp } 3014ac27a0ecSDave Kleikamp 301570bbb3e0SAndrew Morton if (!(bh = sb_bread(sb, logical_sb_block))) { 3016b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "unable to read superblock"); 3017ac27a0ecSDave Kleikamp goto out_fail; 3018ac27a0ecSDave Kleikamp } 3019ac27a0ecSDave Kleikamp /* 3020ac27a0ecSDave Kleikamp * Note: s_es must be initialized as soon as possible because 3021617ba13bSMingming Cao * some ext4 macro-instructions depend on its value 3022ac27a0ecSDave Kleikamp */ 3023617ba13bSMingming Cao es = (struct ext4_super_block *) (((char *)bh->b_data) + offset); 3024ac27a0ecSDave Kleikamp sbi->s_es = es; 3025ac27a0ecSDave Kleikamp sb->s_magic = le16_to_cpu(es->s_magic); 3026617ba13bSMingming Cao if (sb->s_magic != EXT4_SUPER_MAGIC) 3027617ba13bSMingming Cao goto cantfind_ext4; 3028afc32f7eSTheodore Ts'o sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written); 3029ac27a0ecSDave Kleikamp 3030ac27a0ecSDave Kleikamp /* Set defaults before we parse the mount options */ 3031ac27a0ecSDave Kleikamp def_mount_opts = le32_to_cpu(es->s_default_mount_opts); 3032fd8c37ecSTheodore Ts'o set_opt(sb, INIT_INODE_TABLE); 3033617ba13bSMingming Cao if (def_mount_opts & EXT4_DEFM_DEBUG) 3034fd8c37ecSTheodore Ts'o set_opt(sb, DEBUG); 303587f26807STheodore Ts'o if (def_mount_opts & EXT4_DEFM_BSDGROUPS) 3036fd8c37ecSTheodore Ts'o set_opt(sb, GRPID); 3037617ba13bSMingming Cao if (def_mount_opts & EXT4_DEFM_UID16) 3038fd8c37ecSTheodore Ts'o set_opt(sb, NO_UID32); 3039ea663336SEric Sandeen /* xattr user namespace & acls are now defaulted on */ 304003010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_XATTR 3041fd8c37ecSTheodore Ts'o set_opt(sb, XATTR_USER); 30422e7842b8SHugh Dickins #endif 304303010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL 3044fd8c37ecSTheodore Ts'o set_opt(sb, POSIX_ACL); 30452e7842b8SHugh Dickins #endif 30466fd7a467STheodore Ts'o set_opt(sb, MBLK_IO_SUBMIT); 3047617ba13bSMingming Cao if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA) 3048fd8c37ecSTheodore Ts'o set_opt(sb, JOURNAL_DATA); 3049617ba13bSMingming Cao else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED) 3050fd8c37ecSTheodore Ts'o set_opt(sb, ORDERED_DATA); 3051617ba13bSMingming Cao else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK) 3052fd8c37ecSTheodore Ts'o set_opt(sb, WRITEBACK_DATA); 3053ac27a0ecSDave Kleikamp 3054617ba13bSMingming Cao if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC) 3055fd8c37ecSTheodore Ts'o set_opt(sb, ERRORS_PANIC); 3056bb4f397aSAneesh Kumar K.V else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE) 3057fd8c37ecSTheodore Ts'o set_opt(sb, ERRORS_CONT); 3058bb4f397aSAneesh Kumar K.V else 3059fd8c37ecSTheodore Ts'o set_opt(sb, ERRORS_RO); 30608b67f04aSTheodore Ts'o if (def_mount_opts & EXT4_DEFM_BLOCK_VALIDITY) 3061fd8c37ecSTheodore Ts'o set_opt(sb, BLOCK_VALIDITY); 30628b67f04aSTheodore Ts'o if (def_mount_opts & EXT4_DEFM_DISCARD) 3063fd8c37ecSTheodore Ts'o set_opt(sb, DISCARD); 3064ac27a0ecSDave Kleikamp 3065ac27a0ecSDave Kleikamp sbi->s_resuid = le16_to_cpu(es->s_def_resuid); 3066ac27a0ecSDave Kleikamp sbi->s_resgid = le16_to_cpu(es->s_def_resgid); 306730773840STheodore Ts'o sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ; 306830773840STheodore Ts'o sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME; 306930773840STheodore Ts'o sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME; 3070ac27a0ecSDave Kleikamp 30718b67f04aSTheodore Ts'o if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0) 3072fd8c37ecSTheodore Ts'o set_opt(sb, BARRIER); 3073ac27a0ecSDave Kleikamp 30741e2462f9SMingming Cao /* 3075dd919b98SAneesh Kumar K.V * enable delayed allocation by default 3076dd919b98SAneesh Kumar K.V * Use -o nodelalloc to turn it off 3077dd919b98SAneesh Kumar K.V */ 30788b67f04aSTheodore Ts'o if (!IS_EXT3_SB(sb) && 30798b67f04aSTheodore Ts'o ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0)) 3080fd8c37ecSTheodore Ts'o set_opt(sb, DELALLOC); 3081dd919b98SAneesh Kumar K.V 308251ce6511SLukas Czerner /* 308351ce6511SLukas Czerner * set default s_li_wait_mult for lazyinit, for the case there is 308451ce6511SLukas Czerner * no mount option specified. 308551ce6511SLukas Czerner */ 308651ce6511SLukas Czerner sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT; 308751ce6511SLukas Czerner 30888b67f04aSTheodore Ts'o if (!parse_options((char *) sbi->s_es->s_mount_opts, sb, 3089661aa520SEric Sandeen &journal_devnum, &journal_ioprio, 0)) { 30908b67f04aSTheodore Ts'o ext4_msg(sb, KERN_WARNING, 30918b67f04aSTheodore Ts'o "failed to parse options in superblock: %s", 30928b67f04aSTheodore Ts'o sbi->s_es->s_mount_opts); 30938b67f04aSTheodore Ts'o } 30945a916be1STheodore Ts'o sbi->s_def_mount_opt = sbi->s_mount_opt; 3095b3881f74STheodore Ts'o if (!parse_options((char *) data, sb, &journal_devnum, 3096661aa520SEric Sandeen &journal_ioprio, 0)) 3097ac27a0ecSDave Kleikamp goto failed_mount; 3098ac27a0ecSDave Kleikamp 309956889787STheodore Ts'o if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) { 310056889787STheodore Ts'o printk_once(KERN_WARNING "EXT4-fs: Warning: mounting " 310156889787STheodore Ts'o "with data=journal disables delayed " 310256889787STheodore Ts'o "allocation and O_DIRECT support!\n"); 310356889787STheodore Ts'o if (test_opt2(sb, EXPLICIT_DELALLOC)) { 310456889787STheodore Ts'o ext4_msg(sb, KERN_ERR, "can't mount with " 310556889787STheodore Ts'o "both data=journal and delalloc"); 310656889787STheodore Ts'o goto failed_mount; 310756889787STheodore Ts'o } 310856889787STheodore Ts'o if (test_opt(sb, DIOREAD_NOLOCK)) { 310956889787STheodore Ts'o ext4_msg(sb, KERN_ERR, "can't mount with " 311056889787STheodore Ts'o "both data=journal and delalloc"); 311156889787STheodore Ts'o goto failed_mount; 311256889787STheodore Ts'o } 311356889787STheodore Ts'o if (test_opt(sb, DELALLOC)) 311456889787STheodore Ts'o clear_opt(sb, DELALLOC); 311556889787STheodore Ts'o } 311656889787STheodore Ts'o 311756889787STheodore Ts'o blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size); 311856889787STheodore Ts'o if (test_opt(sb, DIOREAD_NOLOCK)) { 311956889787STheodore Ts'o if (blocksize < PAGE_SIZE) { 312056889787STheodore Ts'o ext4_msg(sb, KERN_ERR, "can't mount with " 312156889787STheodore Ts'o "dioread_nolock if block size != PAGE_SIZE"); 312256889787STheodore Ts'o goto failed_mount; 312356889787STheodore Ts'o } 312456889787STheodore Ts'o } 312556889787STheodore Ts'o 3126ac27a0ecSDave Kleikamp sb->s_flags = (sb->s_flags & ~MS_POSIXACL) | 3127482a7425SDmitry Monakhov (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0); 3128ac27a0ecSDave Kleikamp 3129617ba13bSMingming Cao if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV && 3130617ba13bSMingming Cao (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) || 3131617ba13bSMingming Cao EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) || 3132617ba13bSMingming Cao EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U))) 3133b31e1552SEric Sandeen ext4_msg(sb, KERN_WARNING, 3134b31e1552SEric Sandeen "feature flags set on rev 0 fs, " 3135b31e1552SEric Sandeen "running e2fsck is recommended"); 3136469108ffSTheodore Tso 31372035e776STheodore Ts'o if (IS_EXT2_SB(sb)) { 31382035e776STheodore Ts'o if (ext2_feature_set_ok(sb)) 31392035e776STheodore Ts'o ext4_msg(sb, KERN_INFO, "mounting ext2 file system " 31402035e776STheodore Ts'o "using the ext4 subsystem"); 31412035e776STheodore Ts'o else { 31422035e776STheodore Ts'o ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due " 31432035e776STheodore Ts'o "to feature incompatibilities"); 31442035e776STheodore Ts'o goto failed_mount; 31452035e776STheodore Ts'o } 31462035e776STheodore Ts'o } 31472035e776STheodore Ts'o 31482035e776STheodore Ts'o if (IS_EXT3_SB(sb)) { 31492035e776STheodore Ts'o if (ext3_feature_set_ok(sb)) 31502035e776STheodore Ts'o ext4_msg(sb, KERN_INFO, "mounting ext3 file system " 31512035e776STheodore Ts'o "using the ext4 subsystem"); 31522035e776STheodore Ts'o else { 31532035e776STheodore Ts'o ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due " 31542035e776STheodore Ts'o "to feature incompatibilities"); 31552035e776STheodore Ts'o goto failed_mount; 31562035e776STheodore Ts'o } 31572035e776STheodore Ts'o } 31582035e776STheodore Ts'o 3159469108ffSTheodore Tso /* 3160ac27a0ecSDave Kleikamp * Check feature flags regardless of the revision level, since we 3161ac27a0ecSDave Kleikamp * previously didn't change the revision level when setting the flags, 3162ac27a0ecSDave Kleikamp * so there is a chance incompat flags are set on a rev 0 filesystem. 3163ac27a0ecSDave Kleikamp */ 3164a13fb1a4SEric Sandeen if (!ext4_feature_set_ok(sb, (sb->s_flags & MS_RDONLY))) 3165ac27a0ecSDave Kleikamp goto failed_mount; 3166a13fb1a4SEric Sandeen 3167617ba13bSMingming Cao if (blocksize < EXT4_MIN_BLOCK_SIZE || 3168617ba13bSMingming Cao blocksize > EXT4_MAX_BLOCK_SIZE) { 3169b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, 3170b31e1552SEric Sandeen "Unsupported filesystem blocksize %d", blocksize); 3171ac27a0ecSDave Kleikamp goto failed_mount; 3172ac27a0ecSDave Kleikamp } 3173ac27a0ecSDave Kleikamp 3174ac27a0ecSDave Kleikamp if (sb->s_blocksize != blocksize) { 3175ce40733cSAneesh Kumar K.V /* Validate the filesystem blocksize */ 3176ce40733cSAneesh Kumar K.V if (!sb_set_blocksize(sb, blocksize)) { 3177b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "bad block size %d", 3178ce40733cSAneesh Kumar K.V blocksize); 3179ac27a0ecSDave Kleikamp goto failed_mount; 3180ac27a0ecSDave Kleikamp } 3181ac27a0ecSDave Kleikamp 3182ac27a0ecSDave Kleikamp brelse(bh); 318370bbb3e0SAndrew Morton logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE; 318470bbb3e0SAndrew Morton offset = do_div(logical_sb_block, blocksize); 318570bbb3e0SAndrew Morton bh = sb_bread(sb, logical_sb_block); 3186ac27a0ecSDave Kleikamp if (!bh) { 3187b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, 3188b31e1552SEric Sandeen "Can't read superblock on 2nd try"); 3189ac27a0ecSDave Kleikamp goto failed_mount; 3190ac27a0ecSDave Kleikamp } 3191617ba13bSMingming Cao es = (struct ext4_super_block *)(((char *)bh->b_data) + offset); 3192ac27a0ecSDave Kleikamp sbi->s_es = es; 3193617ba13bSMingming Cao if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) { 3194b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, 3195b31e1552SEric Sandeen "Magic mismatch, very weird!"); 3196ac27a0ecSDave Kleikamp goto failed_mount; 3197ac27a0ecSDave Kleikamp } 3198ac27a0ecSDave Kleikamp } 3199ac27a0ecSDave Kleikamp 3200a13fb1a4SEric Sandeen has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb, 3201a13fb1a4SEric Sandeen EXT4_FEATURE_RO_COMPAT_HUGE_FILE); 3202f287a1a5STheodore Ts'o sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits, 3203f287a1a5STheodore Ts'o has_huge_files); 3204f287a1a5STheodore Ts'o sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files); 3205ac27a0ecSDave Kleikamp 3206617ba13bSMingming Cao if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) { 3207617ba13bSMingming Cao sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE; 3208617ba13bSMingming Cao sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO; 3209ac27a0ecSDave Kleikamp } else { 3210ac27a0ecSDave Kleikamp sbi->s_inode_size = le16_to_cpu(es->s_inode_size); 3211ac27a0ecSDave Kleikamp sbi->s_first_ino = le32_to_cpu(es->s_first_ino); 3212617ba13bSMingming Cao if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) || 32131330593eSVignesh Babu (!is_power_of_2(sbi->s_inode_size)) || 3214ac27a0ecSDave Kleikamp (sbi->s_inode_size > blocksize)) { 3215b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, 3216b31e1552SEric Sandeen "unsupported inode size: %d", 3217ac27a0ecSDave Kleikamp sbi->s_inode_size); 3218ac27a0ecSDave Kleikamp goto failed_mount; 3219ac27a0ecSDave Kleikamp } 3220ef7f3835SKalpak Shah if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) 3221ef7f3835SKalpak Shah sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2); 3222ac27a0ecSDave Kleikamp } 32230b8e58a1SAndreas Dilger 32240d1ee42fSAlexandre Ratchov sbi->s_desc_size = le16_to_cpu(es->s_desc_size); 32250d1ee42fSAlexandre Ratchov if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) { 32268fadc143SAlexandre Ratchov if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT || 32270d1ee42fSAlexandre Ratchov sbi->s_desc_size > EXT4_MAX_DESC_SIZE || 3228d8ea6cf8Svignesh babu !is_power_of_2(sbi->s_desc_size)) { 3229b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, 3230b31e1552SEric Sandeen "unsupported descriptor size %lu", 32310d1ee42fSAlexandre Ratchov sbi->s_desc_size); 32320d1ee42fSAlexandre Ratchov goto failed_mount; 32330d1ee42fSAlexandre Ratchov } 32340d1ee42fSAlexandre Ratchov } else 32350d1ee42fSAlexandre Ratchov sbi->s_desc_size = EXT4_MIN_DESC_SIZE; 32360b8e58a1SAndreas Dilger 3237ac27a0ecSDave Kleikamp sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group); 3238ac27a0ecSDave Kleikamp sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group); 3239b47b6f38SAndries E. Brouwer if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0) 3240617ba13bSMingming Cao goto cantfind_ext4; 32410b8e58a1SAndreas Dilger 3242617ba13bSMingming Cao sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb); 3243ac27a0ecSDave Kleikamp if (sbi->s_inodes_per_block == 0) 3244617ba13bSMingming Cao goto cantfind_ext4; 3245ac27a0ecSDave Kleikamp sbi->s_itb_per_group = sbi->s_inodes_per_group / 3246ac27a0ecSDave Kleikamp sbi->s_inodes_per_block; 32470d1ee42fSAlexandre Ratchov sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb); 3248ac27a0ecSDave Kleikamp sbi->s_sbh = bh; 3249ac27a0ecSDave Kleikamp sbi->s_mount_state = le16_to_cpu(es->s_state); 3250e57aa839SFengguang Wu sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb)); 3251e57aa839SFengguang Wu sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb)); 32520b8e58a1SAndreas Dilger 3253ac27a0ecSDave Kleikamp for (i = 0; i < 4; i++) 3254ac27a0ecSDave Kleikamp sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]); 3255ac27a0ecSDave Kleikamp sbi->s_def_hash_version = es->s_def_hash_version; 3256f99b2589STheodore Ts'o i = le32_to_cpu(es->s_flags); 3257f99b2589STheodore Ts'o if (i & EXT2_FLAGS_UNSIGNED_HASH) 3258f99b2589STheodore Ts'o sbi->s_hash_unsigned = 3; 3259f99b2589STheodore Ts'o else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) { 3260f99b2589STheodore Ts'o #ifdef __CHAR_UNSIGNED__ 3261f99b2589STheodore Ts'o es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH); 3262f99b2589STheodore Ts'o sbi->s_hash_unsigned = 3; 3263f99b2589STheodore Ts'o #else 3264f99b2589STheodore Ts'o es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH); 3265f99b2589STheodore Ts'o #endif 3266f99b2589STheodore Ts'o sb->s_dirt = 1; 3267f99b2589STheodore Ts'o } 3268ac27a0ecSDave Kleikamp 3269281b5995STheodore Ts'o /* Handle clustersize */ 3270281b5995STheodore Ts'o clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size); 3271281b5995STheodore Ts'o has_bigalloc = EXT4_HAS_RO_COMPAT_FEATURE(sb, 3272281b5995STheodore Ts'o EXT4_FEATURE_RO_COMPAT_BIGALLOC); 3273281b5995STheodore Ts'o if (has_bigalloc) { 3274281b5995STheodore Ts'o if (clustersize < blocksize) { 3275281b5995STheodore Ts'o ext4_msg(sb, KERN_ERR, 3276281b5995STheodore Ts'o "cluster size (%d) smaller than " 3277281b5995STheodore Ts'o "block size (%d)", clustersize, blocksize); 3278281b5995STheodore Ts'o goto failed_mount; 3279281b5995STheodore Ts'o } 3280281b5995STheodore Ts'o sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) - 3281281b5995STheodore Ts'o le32_to_cpu(es->s_log_block_size); 3282281b5995STheodore Ts'o sbi->s_clusters_per_group = 3283281b5995STheodore Ts'o le32_to_cpu(es->s_clusters_per_group); 3284281b5995STheodore Ts'o if (sbi->s_clusters_per_group > blocksize * 8) { 3285281b5995STheodore Ts'o ext4_msg(sb, KERN_ERR, 3286281b5995STheodore Ts'o "#clusters per group too big: %lu", 3287281b5995STheodore Ts'o sbi->s_clusters_per_group); 3288281b5995STheodore Ts'o goto failed_mount; 3289281b5995STheodore Ts'o } 3290281b5995STheodore Ts'o if (sbi->s_blocks_per_group != 3291281b5995STheodore Ts'o (sbi->s_clusters_per_group * (clustersize / blocksize))) { 3292281b5995STheodore Ts'o ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and " 3293281b5995STheodore Ts'o "clusters per group (%lu) inconsistent", 3294281b5995STheodore Ts'o sbi->s_blocks_per_group, 3295281b5995STheodore Ts'o sbi->s_clusters_per_group); 3296281b5995STheodore Ts'o goto failed_mount; 3297281b5995STheodore Ts'o } 3298281b5995STheodore Ts'o } else { 3299281b5995STheodore Ts'o if (clustersize != blocksize) { 3300281b5995STheodore Ts'o ext4_warning(sb, "fragment/cluster size (%d) != " 3301281b5995STheodore Ts'o "block size (%d)", clustersize, 3302281b5995STheodore Ts'o blocksize); 3303281b5995STheodore Ts'o clustersize = blocksize; 3304281b5995STheodore Ts'o } 3305ac27a0ecSDave Kleikamp if (sbi->s_blocks_per_group > blocksize * 8) { 3306b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, 3307b31e1552SEric Sandeen "#blocks per group too big: %lu", 3308ac27a0ecSDave Kleikamp sbi->s_blocks_per_group); 3309ac27a0ecSDave Kleikamp goto failed_mount; 3310ac27a0ecSDave Kleikamp } 3311281b5995STheodore Ts'o sbi->s_clusters_per_group = sbi->s_blocks_per_group; 3312281b5995STheodore Ts'o sbi->s_cluster_bits = 0; 3313281b5995STheodore Ts'o } 3314281b5995STheodore Ts'o sbi->s_cluster_ratio = clustersize / blocksize; 3315281b5995STheodore Ts'o 3316ac27a0ecSDave Kleikamp if (sbi->s_inodes_per_group > blocksize * 8) { 3317b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, 3318b31e1552SEric Sandeen "#inodes per group too big: %lu", 3319ac27a0ecSDave Kleikamp sbi->s_inodes_per_group); 3320ac27a0ecSDave Kleikamp goto failed_mount; 3321ac27a0ecSDave Kleikamp } 3322ac27a0ecSDave Kleikamp 3323bf43d84bSEric Sandeen /* 3324bf43d84bSEric Sandeen * Test whether we have more sectors than will fit in sector_t, 3325bf43d84bSEric Sandeen * and whether the max offset is addressable by the page cache. 3326bf43d84bSEric Sandeen */ 33275a9ae68aSDarrick J. Wong err = generic_check_addressable(sb->s_blocksize_bits, 332830ca22c7SPatrick J. LoPresti ext4_blocks_count(es)); 33295a9ae68aSDarrick J. Wong if (err) { 3330b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "filesystem" 3331bf43d84bSEric Sandeen " too large to mount safely on this system"); 3332ac27a0ecSDave Kleikamp if (sizeof(sector_t) < 8) 333390c699a9SBartlomiej Zolnierkiewicz ext4_msg(sb, KERN_WARNING, "CONFIG_LBDAF not enabled"); 33345a9ae68aSDarrick J. Wong ret = err; 3335ac27a0ecSDave Kleikamp goto failed_mount; 3336ac27a0ecSDave Kleikamp } 3337ac27a0ecSDave Kleikamp 3338617ba13bSMingming Cao if (EXT4_BLOCKS_PER_GROUP(sb) == 0) 3339617ba13bSMingming Cao goto cantfind_ext4; 3340e7c95593SEric Sandeen 33410f2ddca6SFrom: Thiemo Nagel /* check blocks count against device size */ 33420f2ddca6SFrom: Thiemo Nagel blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits; 33430f2ddca6SFrom: Thiemo Nagel if (blocks_count && ext4_blocks_count(es) > blocks_count) { 3344b31e1552SEric Sandeen ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu " 3345b31e1552SEric Sandeen "exceeds size of device (%llu blocks)", 33460f2ddca6SFrom: Thiemo Nagel ext4_blocks_count(es), blocks_count); 33470f2ddca6SFrom: Thiemo Nagel goto failed_mount; 33480f2ddca6SFrom: Thiemo Nagel } 33490f2ddca6SFrom: Thiemo Nagel 33504ec11028STheodore Ts'o /* 33514ec11028STheodore Ts'o * It makes no sense for the first data block to be beyond the end 33524ec11028STheodore Ts'o * of the filesystem. 33534ec11028STheodore Ts'o */ 33544ec11028STheodore Ts'o if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) { 3355b31e1552SEric Sandeen ext4_msg(sb, KERN_WARNING, "bad geometry: first data " 3356b31e1552SEric Sandeen "block %u is beyond end of filesystem (%llu)", 3357e7c95593SEric Sandeen le32_to_cpu(es->s_first_data_block), 33584ec11028STheodore Ts'o ext4_blocks_count(es)); 3359e7c95593SEric Sandeen goto failed_mount; 3360e7c95593SEric Sandeen } 3361bd81d8eeSLaurent Vivier blocks_count = (ext4_blocks_count(es) - 3362bd81d8eeSLaurent Vivier le32_to_cpu(es->s_first_data_block) + 3363bd81d8eeSLaurent Vivier EXT4_BLOCKS_PER_GROUP(sb) - 1); 3364bd81d8eeSLaurent Vivier do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb)); 33654ec11028STheodore Ts'o if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) { 3366b31e1552SEric Sandeen ext4_msg(sb, KERN_WARNING, "groups count too large: %u " 33674ec11028STheodore Ts'o "(block count %llu, first data block %u, " 3368b31e1552SEric Sandeen "blocks per group %lu)", sbi->s_groups_count, 33694ec11028STheodore Ts'o ext4_blocks_count(es), 33704ec11028STheodore Ts'o le32_to_cpu(es->s_first_data_block), 33714ec11028STheodore Ts'o EXT4_BLOCKS_PER_GROUP(sb)); 33724ec11028STheodore Ts'o goto failed_mount; 33734ec11028STheodore Ts'o } 3374bd81d8eeSLaurent Vivier sbi->s_groups_count = blocks_count; 3375fb0a387dSEric Sandeen sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count, 3376fb0a387dSEric Sandeen (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb))); 3377617ba13bSMingming Cao db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) / 3378617ba13bSMingming Cao EXT4_DESC_PER_BLOCK(sb); 3379f18a5f21STheodore Ts'o sbi->s_group_desc = ext4_kvmalloc(db_count * 3380f18a5f21STheodore Ts'o sizeof(struct buffer_head *), 3381ac27a0ecSDave Kleikamp GFP_KERNEL); 3382ac27a0ecSDave Kleikamp if (sbi->s_group_desc == NULL) { 3383b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "not enough memory"); 3384ac27a0ecSDave Kleikamp goto failed_mount; 3385ac27a0ecSDave Kleikamp } 3386ac27a0ecSDave Kleikamp 33879f6200bbSTheodore Ts'o if (ext4_proc_root) 33889f6200bbSTheodore Ts'o sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root); 3389240799cdSTheodore Ts'o 339066acdcf4STheodore Ts'o if (sbi->s_proc) 339166acdcf4STheodore Ts'o proc_create_data("options", S_IRUGO, sbi->s_proc, 339266acdcf4STheodore Ts'o &ext4_seq_options_fops, sb); 339366acdcf4STheodore Ts'o 3394705895b6SPekka Enberg bgl_lock_init(sbi->s_blockgroup_lock); 3395ac27a0ecSDave Kleikamp 3396ac27a0ecSDave Kleikamp for (i = 0; i < db_count; i++) { 339770bbb3e0SAndrew Morton block = descriptor_loc(sb, logical_sb_block, i); 3398ac27a0ecSDave Kleikamp sbi->s_group_desc[i] = sb_bread(sb, block); 3399ac27a0ecSDave Kleikamp if (!sbi->s_group_desc[i]) { 3400b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, 3401b31e1552SEric Sandeen "can't read group descriptor %d", i); 3402ac27a0ecSDave Kleikamp db_count = i; 3403ac27a0ecSDave Kleikamp goto failed_mount2; 3404ac27a0ecSDave Kleikamp } 3405ac27a0ecSDave Kleikamp } 3406bfff6873SLukas Czerner if (!ext4_check_descriptors(sb, &first_not_zeroed)) { 3407b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "group descriptors corrupted!"); 3408ac27a0ecSDave Kleikamp goto failed_mount2; 3409ac27a0ecSDave Kleikamp } 3410772cb7c8SJose R. Santos if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG)) 3411772cb7c8SJose R. Santos if (!ext4_fill_flex_info(sb)) { 3412b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, 3413b31e1552SEric Sandeen "unable to initialize " 3414b31e1552SEric Sandeen "flex_bg meta info!"); 3415772cb7c8SJose R. Santos goto failed_mount2; 3416772cb7c8SJose R. Santos } 3417772cb7c8SJose R. Santos 3418ac27a0ecSDave Kleikamp sbi->s_gdb_count = db_count; 3419ac27a0ecSDave Kleikamp get_random_bytes(&sbi->s_next_generation, sizeof(u32)); 3420ac27a0ecSDave Kleikamp spin_lock_init(&sbi->s_next_gen_lock); 3421ac27a0ecSDave Kleikamp 342204496411STao Ma init_timer(&sbi->s_err_report); 342304496411STao Ma sbi->s_err_report.function = print_daily_error_info; 342404496411STao Ma sbi->s_err_report.data = (unsigned long) sb; 342504496411STao Ma 342657042651STheodore Ts'o err = percpu_counter_init(&sbi->s_freeclusters_counter, 34275dee5437STheodore Ts'o ext4_count_free_clusters(sb)); 3428ce7e010aSTheodore Ts'o if (!err) { 3429ce7e010aSTheodore Ts'o err = percpu_counter_init(&sbi->s_freeinodes_counter, 3430ce7e010aSTheodore Ts'o ext4_count_free_inodes(sb)); 3431ce7e010aSTheodore Ts'o } 3432ce7e010aSTheodore Ts'o if (!err) { 3433ce7e010aSTheodore Ts'o err = percpu_counter_init(&sbi->s_dirs_counter, 3434ce7e010aSTheodore Ts'o ext4_count_dirs(sb)); 3435ce7e010aSTheodore Ts'o } 3436ce7e010aSTheodore Ts'o if (!err) { 343757042651STheodore Ts'o err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0); 3438ce7e010aSTheodore Ts'o } 3439ce7e010aSTheodore Ts'o if (err) { 3440ce7e010aSTheodore Ts'o ext4_msg(sb, KERN_ERR, "insufficient memory"); 3441ce7e010aSTheodore Ts'o goto failed_mount3; 3442ce7e010aSTheodore Ts'o } 3443ce7e010aSTheodore Ts'o 3444c9de560dSAlex Tomas sbi->s_stripe = ext4_get_stripe_size(sbi); 344555138e0bSTheodore Ts'o sbi->s_max_writeback_mb_bump = 128; 3446c9de560dSAlex Tomas 3447ac27a0ecSDave Kleikamp /* 3448ac27a0ecSDave Kleikamp * set up enough so that it can read an inode 3449ac27a0ecSDave Kleikamp */ 34509ca92389STheodore Ts'o if (!test_opt(sb, NOLOAD) && 34519ca92389STheodore Ts'o EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) 3452617ba13bSMingming Cao sb->s_op = &ext4_sops; 34539ca92389STheodore Ts'o else 34549ca92389STheodore Ts'o sb->s_op = &ext4_nojournal_sops; 3455617ba13bSMingming Cao sb->s_export_op = &ext4_export_ops; 3456617ba13bSMingming Cao sb->s_xattr = ext4_xattr_handlers; 3457ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 3458617ba13bSMingming Cao sb->s_qcop = &ext4_qctl_operations; 3459617ba13bSMingming Cao sb->dq_op = &ext4_quota_operations; 3460ac27a0ecSDave Kleikamp #endif 3461f2fa2ffcSAneesh Kumar K.V memcpy(sb->s_uuid, es->s_uuid, sizeof(es->s_uuid)); 3462f2fa2ffcSAneesh Kumar K.V 3463ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */ 34643b9d4ed2STheodore Ts'o mutex_init(&sbi->s_orphan_lock); 34658f82f840SYongqiang Yang sbi->s_resize_flags = 0; 3466ac27a0ecSDave Kleikamp 3467ac27a0ecSDave Kleikamp sb->s_root = NULL; 3468ac27a0ecSDave Kleikamp 3469ac27a0ecSDave Kleikamp needs_recovery = (es->s_last_orphan != 0 || 3470617ba13bSMingming Cao EXT4_HAS_INCOMPAT_FEATURE(sb, 3471617ba13bSMingming Cao EXT4_FEATURE_INCOMPAT_RECOVER)); 3472ac27a0ecSDave Kleikamp 3473c5e06d10SJohann Lombardi if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_MMP) && 3474c5e06d10SJohann Lombardi !(sb->s_flags & MS_RDONLY)) 3475c5e06d10SJohann Lombardi if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block))) 3476c5e06d10SJohann Lombardi goto failed_mount3; 3477c5e06d10SJohann Lombardi 3478ac27a0ecSDave Kleikamp /* 3479ac27a0ecSDave Kleikamp * The first inode we look at is the journal inode. Don't try 3480ac27a0ecSDave Kleikamp * root first: it may be modified in the journal! 3481ac27a0ecSDave Kleikamp */ 3482ac27a0ecSDave Kleikamp if (!test_opt(sb, NOLOAD) && 3483617ba13bSMingming Cao EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) { 3484617ba13bSMingming Cao if (ext4_load_journal(sb, es, journal_devnum)) 3485ac27a0ecSDave Kleikamp goto failed_mount3; 34860390131bSFrank Mayhar } else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) && 34870390131bSFrank Mayhar EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) { 3488b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "required journal recovery " 3489b31e1552SEric Sandeen "suppressed and not mounted read-only"); 3490744692dcSJiaying Zhang goto failed_mount_wq; 3491ac27a0ecSDave Kleikamp } else { 3492fd8c37ecSTheodore Ts'o clear_opt(sb, DATA_FLAGS); 34930390131bSFrank Mayhar sbi->s_journal = NULL; 34940390131bSFrank Mayhar needs_recovery = 0; 34950390131bSFrank Mayhar goto no_journal; 3496ac27a0ecSDave Kleikamp } 3497ac27a0ecSDave Kleikamp 3498eb40a09cSJose R. Santos if (ext4_blocks_count(es) > 0xffffffffULL && 3499eb40a09cSJose R. Santos !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0, 3500eb40a09cSJose R. Santos JBD2_FEATURE_INCOMPAT_64BIT)) { 3501b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature"); 3502744692dcSJiaying Zhang goto failed_mount_wq; 3503eb40a09cSJose R. Santos } 3504eb40a09cSJose R. Santos 3505d4da6c9cSLinus Torvalds if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) { 3506d4da6c9cSLinus Torvalds jbd2_journal_set_features(sbi->s_journal, 3507d4da6c9cSLinus Torvalds JBD2_FEATURE_COMPAT_CHECKSUM, 0, 3508d4da6c9cSLinus Torvalds JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT); 3509d4da6c9cSLinus Torvalds } else if (test_opt(sb, JOURNAL_CHECKSUM)) { 3510818d276cSGirish Shilamkar jbd2_journal_set_features(sbi->s_journal, 3511818d276cSGirish Shilamkar JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0); 3512818d276cSGirish Shilamkar jbd2_journal_clear_features(sbi->s_journal, 0, 0, 3513818d276cSGirish Shilamkar JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT); 3514d4da6c9cSLinus Torvalds } else { 3515d4da6c9cSLinus Torvalds jbd2_journal_clear_features(sbi->s_journal, 3516d4da6c9cSLinus Torvalds JBD2_FEATURE_COMPAT_CHECKSUM, 0, 3517d4da6c9cSLinus Torvalds JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT); 3518d4da6c9cSLinus Torvalds } 3519818d276cSGirish Shilamkar 3520ac27a0ecSDave Kleikamp /* We have now updated the journal if required, so we can 3521ac27a0ecSDave Kleikamp * validate the data journaling mode. */ 3522ac27a0ecSDave Kleikamp switch (test_opt(sb, DATA_FLAGS)) { 3523ac27a0ecSDave Kleikamp case 0: 3524ac27a0ecSDave Kleikamp /* No mode set, assume a default based on the journal 352563f57933SAndrew Morton * capabilities: ORDERED_DATA if the journal can 352663f57933SAndrew Morton * cope, else JOURNAL_DATA 352763f57933SAndrew Morton */ 3528dab291afSMingming Cao if (jbd2_journal_check_available_features 3529dab291afSMingming Cao (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) 3530fd8c37ecSTheodore Ts'o set_opt(sb, ORDERED_DATA); 3531ac27a0ecSDave Kleikamp else 3532fd8c37ecSTheodore Ts'o set_opt(sb, JOURNAL_DATA); 3533ac27a0ecSDave Kleikamp break; 3534ac27a0ecSDave Kleikamp 3535617ba13bSMingming Cao case EXT4_MOUNT_ORDERED_DATA: 3536617ba13bSMingming Cao case EXT4_MOUNT_WRITEBACK_DATA: 3537dab291afSMingming Cao if (!jbd2_journal_check_available_features 3538dab291afSMingming Cao (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) { 3539b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "Journal does not support " 3540b31e1552SEric Sandeen "requested data journaling mode"); 3541744692dcSJiaying Zhang goto failed_mount_wq; 3542ac27a0ecSDave Kleikamp } 3543ac27a0ecSDave Kleikamp default: 3544ac27a0ecSDave Kleikamp break; 3545ac27a0ecSDave Kleikamp } 3546b3881f74STheodore Ts'o set_task_ioprio(sbi->s_journal->j_task, journal_ioprio); 3547ac27a0ecSDave Kleikamp 354818aadd47SBobi Jam sbi->s_journal->j_commit_callback = ext4_journal_commit_callback; 354918aadd47SBobi Jam 3550ce7e010aSTheodore Ts'o /* 3551ce7e010aSTheodore Ts'o * The journal may have updated the bg summary counts, so we 3552ce7e010aSTheodore Ts'o * need to update the global counters. 3553ce7e010aSTheodore Ts'o */ 355457042651STheodore Ts'o percpu_counter_set(&sbi->s_freeclusters_counter, 35555dee5437STheodore Ts'o ext4_count_free_clusters(sb)); 3556ce7e010aSTheodore Ts'o percpu_counter_set(&sbi->s_freeinodes_counter, 355784061e07SDmitry Monakhov ext4_count_free_inodes(sb)); 3558ce7e010aSTheodore Ts'o percpu_counter_set(&sbi->s_dirs_counter, 355984061e07SDmitry Monakhov ext4_count_dirs(sb)); 356057042651STheodore Ts'o percpu_counter_set(&sbi->s_dirtyclusters_counter, 0); 3561206f7ab4SChristoph Hellwig 3562ce7e010aSTheodore Ts'o no_journal: 3563fd89d5f2STejun Heo /* 3564fd89d5f2STejun Heo * The maximum number of concurrent works can be high and 3565fd89d5f2STejun Heo * concurrency isn't really necessary. Limit it to 1. 3566fd89d5f2STejun Heo */ 3567fd89d5f2STejun Heo EXT4_SB(sb)->dio_unwritten_wq = 3568ae005cbeSLinus Torvalds alloc_workqueue("ext4-dio-unwritten", WQ_MEM_RECLAIM | WQ_UNBOUND, 1); 35694c0425ffSMingming Cao if (!EXT4_SB(sb)->dio_unwritten_wq) { 35704c0425ffSMingming Cao printk(KERN_ERR "EXT4-fs: failed to create DIO workqueue\n"); 35714c0425ffSMingming Cao goto failed_mount_wq; 35724c0425ffSMingming Cao } 35734c0425ffSMingming Cao 3574ac27a0ecSDave Kleikamp /* 3575dab291afSMingming Cao * The jbd2_journal_load will have done any necessary log recovery, 3576ac27a0ecSDave Kleikamp * so we can safely mount the rest of the filesystem now. 3577ac27a0ecSDave Kleikamp */ 3578ac27a0ecSDave Kleikamp 35791d1fe1eeSDavid Howells root = ext4_iget(sb, EXT4_ROOT_INO); 35801d1fe1eeSDavid Howells if (IS_ERR(root)) { 3581b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "get root inode failed"); 35821d1fe1eeSDavid Howells ret = PTR_ERR(root); 358332a9bb57SManish Katiyar root = NULL; 3584ac27a0ecSDave Kleikamp goto failed_mount4; 3585ac27a0ecSDave Kleikamp } 3586ac27a0ecSDave Kleikamp if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) { 3587b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck"); 358894bf608aSAl Viro iput(root); 3589ac27a0ecSDave Kleikamp goto failed_mount4; 3590ac27a0ecSDave Kleikamp } 35911d1fe1eeSDavid Howells sb->s_root = d_alloc_root(root); 35921d1fe1eeSDavid Howells if (!sb->s_root) { 359394bf608aSAl Viro iput(root); 3594b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "get root dentry failed"); 35951d1fe1eeSDavid Howells ret = -ENOMEM; 35961d1fe1eeSDavid Howells goto failed_mount4; 35971d1fe1eeSDavid Howells } 3598ac27a0ecSDave Kleikamp 3599617ba13bSMingming Cao ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY); 3600ef7f3835SKalpak Shah 3601ef7f3835SKalpak Shah /* determine the minimum size of new large inodes, if present */ 3602ef7f3835SKalpak Shah if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) { 3603ef7f3835SKalpak Shah sbi->s_want_extra_isize = sizeof(struct ext4_inode) - 3604ef7f3835SKalpak Shah EXT4_GOOD_OLD_INODE_SIZE; 3605ef7f3835SKalpak Shah if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 3606ef7f3835SKalpak Shah EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) { 3607ef7f3835SKalpak Shah if (sbi->s_want_extra_isize < 3608ef7f3835SKalpak Shah le16_to_cpu(es->s_want_extra_isize)) 3609ef7f3835SKalpak Shah sbi->s_want_extra_isize = 3610ef7f3835SKalpak Shah le16_to_cpu(es->s_want_extra_isize); 3611ef7f3835SKalpak Shah if (sbi->s_want_extra_isize < 3612ef7f3835SKalpak Shah le16_to_cpu(es->s_min_extra_isize)) 3613ef7f3835SKalpak Shah sbi->s_want_extra_isize = 3614ef7f3835SKalpak Shah le16_to_cpu(es->s_min_extra_isize); 3615ef7f3835SKalpak Shah } 3616ef7f3835SKalpak Shah } 3617ef7f3835SKalpak Shah /* Check if enough inode space is available */ 3618ef7f3835SKalpak Shah if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize > 3619ef7f3835SKalpak Shah sbi->s_inode_size) { 3620ef7f3835SKalpak Shah sbi->s_want_extra_isize = sizeof(struct ext4_inode) - 3621ef7f3835SKalpak Shah EXT4_GOOD_OLD_INODE_SIZE; 3622b31e1552SEric Sandeen ext4_msg(sb, KERN_INFO, "required extra inode space not" 3623b31e1552SEric Sandeen "available"); 3624ef7f3835SKalpak Shah } 3625ef7f3835SKalpak Shah 36266fd058f7STheodore Ts'o err = ext4_setup_system_zone(sb); 36276fd058f7STheodore Ts'o if (err) { 3628b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "failed to initialize system " 3629fbe845ddSCurt Wohlgemuth "zone (%d)", err); 363094bf608aSAl Viro goto failed_mount4a; 36316fd058f7STheodore Ts'o } 36326fd058f7STheodore Ts'o 3633c2774d84SAneesh Kumar K.V ext4_ext_init(sb); 3634c2774d84SAneesh Kumar K.V err = ext4_mb_init(sb, needs_recovery); 3635c2774d84SAneesh Kumar K.V if (err) { 3636421f91d2SUwe Kleine-König ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)", 3637c2774d84SAneesh Kumar K.V err); 3638dcf2d804STao Ma goto failed_mount5; 3639c2774d84SAneesh Kumar K.V } 3640c2774d84SAneesh Kumar K.V 3641bfff6873SLukas Czerner err = ext4_register_li_request(sb, first_not_zeroed); 3642bfff6873SLukas Czerner if (err) 3643dcf2d804STao Ma goto failed_mount6; 3644bfff6873SLukas Czerner 36453197ebdbSTheodore Ts'o sbi->s_kobj.kset = ext4_kset; 36463197ebdbSTheodore Ts'o init_completion(&sbi->s_kobj_unregister); 36473197ebdbSTheodore Ts'o err = kobject_init_and_add(&sbi->s_kobj, &ext4_ktype, NULL, 36483197ebdbSTheodore Ts'o "%s", sb->s_id); 3649dcf2d804STao Ma if (err) 3650dcf2d804STao Ma goto failed_mount7; 36513197ebdbSTheodore Ts'o 3652617ba13bSMingming Cao EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS; 3653617ba13bSMingming Cao ext4_orphan_cleanup(sb, es); 3654617ba13bSMingming Cao EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS; 36550390131bSFrank Mayhar if (needs_recovery) { 3656b31e1552SEric Sandeen ext4_msg(sb, KERN_INFO, "recovery complete"); 3657617ba13bSMingming Cao ext4_mark_recovery_complete(sb, es); 36580390131bSFrank Mayhar } 36590390131bSFrank Mayhar if (EXT4_SB(sb)->s_journal) { 36600390131bSFrank Mayhar if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) 36610390131bSFrank Mayhar descr = " journalled data mode"; 36620390131bSFrank Mayhar else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) 36630390131bSFrank Mayhar descr = " ordered data mode"; 36640390131bSFrank Mayhar else 36650390131bSFrank Mayhar descr = " writeback data mode"; 36660390131bSFrank Mayhar } else 36670390131bSFrank Mayhar descr = "out journal"; 36680390131bSFrank Mayhar 3669d4c402d9SCurt Wohlgemuth ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. " 36708b67f04aSTheodore Ts'o "Opts: %s%s%s", descr, sbi->s_es->s_mount_opts, 36718b67f04aSTheodore Ts'o *sbi->s_es->s_mount_opts ? "; " : "", orig_data); 3672ac27a0ecSDave Kleikamp 367366e61a9eSTheodore Ts'o if (es->s_error_count) 367466e61a9eSTheodore Ts'o mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */ 3675ac27a0ecSDave Kleikamp 3676d4c402d9SCurt Wohlgemuth kfree(orig_data); 3677ac27a0ecSDave Kleikamp return 0; 3678ac27a0ecSDave Kleikamp 3679617ba13bSMingming Cao cantfind_ext4: 3680ac27a0ecSDave Kleikamp if (!silent) 3681b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem"); 3682ac27a0ecSDave Kleikamp goto failed_mount; 3683ac27a0ecSDave Kleikamp 3684dcf2d804STao Ma failed_mount7: 3685dcf2d804STao Ma ext4_unregister_li_request(sb); 3686dcf2d804STao Ma failed_mount6: 3687dcf2d804STao Ma ext4_mb_release(sb); 368894bf608aSAl Viro failed_mount5: 368994bf608aSAl Viro ext4_ext_release(sb); 3690dcf2d804STao Ma ext4_release_system_zone(sb); 369194bf608aSAl Viro failed_mount4a: 369294bf608aSAl Viro dput(sb->s_root); 369332a9bb57SManish Katiyar sb->s_root = NULL; 369494bf608aSAl Viro failed_mount4: 3695b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "mount failed"); 36964c0425ffSMingming Cao destroy_workqueue(EXT4_SB(sb)->dio_unwritten_wq); 36974c0425ffSMingming Cao failed_mount_wq: 36980390131bSFrank Mayhar if (sbi->s_journal) { 3699dab291afSMingming Cao jbd2_journal_destroy(sbi->s_journal); 370047b4a50bSJan Kara sbi->s_journal = NULL; 37010390131bSFrank Mayhar } 3702ac27a0ecSDave Kleikamp failed_mount3: 370304496411STao Ma del_timer(&sbi->s_err_report); 37049933fc0aSTheodore Ts'o if (sbi->s_flex_groups) 37059933fc0aSTheodore Ts'o ext4_kvfree(sbi->s_flex_groups); 370657042651STheodore Ts'o percpu_counter_destroy(&sbi->s_freeclusters_counter); 3707ce7e010aSTheodore Ts'o percpu_counter_destroy(&sbi->s_freeinodes_counter); 3708ce7e010aSTheodore Ts'o percpu_counter_destroy(&sbi->s_dirs_counter); 370957042651STheodore Ts'o percpu_counter_destroy(&sbi->s_dirtyclusters_counter); 3710c5e06d10SJohann Lombardi if (sbi->s_mmp_tsk) 3711c5e06d10SJohann Lombardi kthread_stop(sbi->s_mmp_tsk); 3712ac27a0ecSDave Kleikamp failed_mount2: 3713ac27a0ecSDave Kleikamp for (i = 0; i < db_count; i++) 3714ac27a0ecSDave Kleikamp brelse(sbi->s_group_desc[i]); 3715f18a5f21STheodore Ts'o ext4_kvfree(sbi->s_group_desc); 3716ac27a0ecSDave Kleikamp failed_mount: 3717240799cdSTheodore Ts'o if (sbi->s_proc) { 371866acdcf4STheodore Ts'o remove_proc_entry("options", sbi->s_proc); 37199f6200bbSTheodore Ts'o remove_proc_entry(sb->s_id, ext4_proc_root); 3720240799cdSTheodore Ts'o } 3721ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 3722ac27a0ecSDave Kleikamp for (i = 0; i < MAXQUOTAS; i++) 3723ac27a0ecSDave Kleikamp kfree(sbi->s_qf_names[i]); 3724ac27a0ecSDave Kleikamp #endif 3725617ba13bSMingming Cao ext4_blkdev_remove(sbi); 3726ac27a0ecSDave Kleikamp brelse(bh); 3727ac27a0ecSDave Kleikamp out_fail: 3728ac27a0ecSDave Kleikamp sb->s_fs_info = NULL; 3729f6830165SManish Katiyar kfree(sbi->s_blockgroup_lock); 3730ac27a0ecSDave Kleikamp kfree(sbi); 3731dcc7dae3SCyrill Gorcunov out_free_orig: 3732d4c402d9SCurt Wohlgemuth kfree(orig_data); 37331d1fe1eeSDavid Howells return ret; 3734ac27a0ecSDave Kleikamp } 3735ac27a0ecSDave Kleikamp 3736ac27a0ecSDave Kleikamp /* 3737ac27a0ecSDave Kleikamp * Setup any per-fs journal parameters now. We'll do this both on 3738ac27a0ecSDave Kleikamp * initial mount, once the journal has been initialised but before we've 3739ac27a0ecSDave Kleikamp * done any recovery; and again on any subsequent remount. 3740ac27a0ecSDave Kleikamp */ 3741617ba13bSMingming Cao static void ext4_init_journal_params(struct super_block *sb, journal_t *journal) 3742ac27a0ecSDave Kleikamp { 3743617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 3744ac27a0ecSDave Kleikamp 3745ac27a0ecSDave Kleikamp journal->j_commit_interval = sbi->s_commit_interval; 374630773840STheodore Ts'o journal->j_min_batch_time = sbi->s_min_batch_time; 374730773840STheodore Ts'o journal->j_max_batch_time = sbi->s_max_batch_time; 3748ac27a0ecSDave Kleikamp 3749a931da6aSTheodore Ts'o write_lock(&journal->j_state_lock); 3750ac27a0ecSDave Kleikamp if (test_opt(sb, BARRIER)) 3751dab291afSMingming Cao journal->j_flags |= JBD2_BARRIER; 3752ac27a0ecSDave Kleikamp else 3753dab291afSMingming Cao journal->j_flags &= ~JBD2_BARRIER; 37545bf5683aSHidehiro Kawai if (test_opt(sb, DATA_ERR_ABORT)) 37555bf5683aSHidehiro Kawai journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR; 37565bf5683aSHidehiro Kawai else 37575bf5683aSHidehiro Kawai journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR; 3758a931da6aSTheodore Ts'o write_unlock(&journal->j_state_lock); 3759ac27a0ecSDave Kleikamp } 3760ac27a0ecSDave Kleikamp 3761617ba13bSMingming Cao static journal_t *ext4_get_journal(struct super_block *sb, 3762ac27a0ecSDave Kleikamp unsigned int journal_inum) 3763ac27a0ecSDave Kleikamp { 3764ac27a0ecSDave Kleikamp struct inode *journal_inode; 3765ac27a0ecSDave Kleikamp journal_t *journal; 3766ac27a0ecSDave Kleikamp 37670390131bSFrank Mayhar BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)); 37680390131bSFrank Mayhar 3769ac27a0ecSDave Kleikamp /* First, test for the existence of a valid inode on disk. Bad 3770ac27a0ecSDave Kleikamp * things happen if we iget() an unused inode, as the subsequent 3771ac27a0ecSDave Kleikamp * iput() will try to delete it. */ 3772ac27a0ecSDave Kleikamp 37731d1fe1eeSDavid Howells journal_inode = ext4_iget(sb, journal_inum); 37741d1fe1eeSDavid Howells if (IS_ERR(journal_inode)) { 3775b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "no journal found"); 3776ac27a0ecSDave Kleikamp return NULL; 3777ac27a0ecSDave Kleikamp } 3778ac27a0ecSDave Kleikamp if (!journal_inode->i_nlink) { 3779ac27a0ecSDave Kleikamp make_bad_inode(journal_inode); 3780ac27a0ecSDave Kleikamp iput(journal_inode); 3781b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "journal inode is deleted"); 3782ac27a0ecSDave Kleikamp return NULL; 3783ac27a0ecSDave Kleikamp } 3784ac27a0ecSDave Kleikamp 3785e5f8eab8STheodore Ts'o jbd_debug(2, "Journal inode found at %p: %lld bytes\n", 3786ac27a0ecSDave Kleikamp journal_inode, journal_inode->i_size); 37871d1fe1eeSDavid Howells if (!S_ISREG(journal_inode->i_mode)) { 3788b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "invalid journal inode"); 3789ac27a0ecSDave Kleikamp iput(journal_inode); 3790ac27a0ecSDave Kleikamp return NULL; 3791ac27a0ecSDave Kleikamp } 3792ac27a0ecSDave Kleikamp 3793dab291afSMingming Cao journal = jbd2_journal_init_inode(journal_inode); 3794ac27a0ecSDave Kleikamp if (!journal) { 3795b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "Could not load journal inode"); 3796ac27a0ecSDave Kleikamp iput(journal_inode); 3797ac27a0ecSDave Kleikamp return NULL; 3798ac27a0ecSDave Kleikamp } 3799ac27a0ecSDave Kleikamp journal->j_private = sb; 3800617ba13bSMingming Cao ext4_init_journal_params(sb, journal); 3801ac27a0ecSDave Kleikamp return journal; 3802ac27a0ecSDave Kleikamp } 3803ac27a0ecSDave Kleikamp 3804617ba13bSMingming Cao static journal_t *ext4_get_dev_journal(struct super_block *sb, 3805ac27a0ecSDave Kleikamp dev_t j_dev) 3806ac27a0ecSDave Kleikamp { 3807ac27a0ecSDave Kleikamp struct buffer_head *bh; 3808ac27a0ecSDave Kleikamp journal_t *journal; 3809617ba13bSMingming Cao ext4_fsblk_t start; 3810617ba13bSMingming Cao ext4_fsblk_t len; 3811ac27a0ecSDave Kleikamp int hblock, blocksize; 3812617ba13bSMingming Cao ext4_fsblk_t sb_block; 3813ac27a0ecSDave Kleikamp unsigned long offset; 3814617ba13bSMingming Cao struct ext4_super_block *es; 3815ac27a0ecSDave Kleikamp struct block_device *bdev; 3816ac27a0ecSDave Kleikamp 38170390131bSFrank Mayhar BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)); 38180390131bSFrank Mayhar 3819b31e1552SEric Sandeen bdev = ext4_blkdev_get(j_dev, sb); 3820ac27a0ecSDave Kleikamp if (bdev == NULL) 3821ac27a0ecSDave Kleikamp return NULL; 3822ac27a0ecSDave Kleikamp 3823ac27a0ecSDave Kleikamp blocksize = sb->s_blocksize; 3824e1defc4fSMartin K. Petersen hblock = bdev_logical_block_size(bdev); 3825ac27a0ecSDave Kleikamp if (blocksize < hblock) { 3826b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, 3827b31e1552SEric Sandeen "blocksize too small for journal device"); 3828ac27a0ecSDave Kleikamp goto out_bdev; 3829ac27a0ecSDave Kleikamp } 3830ac27a0ecSDave Kleikamp 3831617ba13bSMingming Cao sb_block = EXT4_MIN_BLOCK_SIZE / blocksize; 3832617ba13bSMingming Cao offset = EXT4_MIN_BLOCK_SIZE % blocksize; 3833ac27a0ecSDave Kleikamp set_blocksize(bdev, blocksize); 3834ac27a0ecSDave Kleikamp if (!(bh = __bread(bdev, sb_block, blocksize))) { 3835b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "couldn't read superblock of " 3836b31e1552SEric Sandeen "external journal"); 3837ac27a0ecSDave Kleikamp goto out_bdev; 3838ac27a0ecSDave Kleikamp } 3839ac27a0ecSDave Kleikamp 3840617ba13bSMingming Cao es = (struct ext4_super_block *) (((char *)bh->b_data) + offset); 3841617ba13bSMingming Cao if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) || 3842ac27a0ecSDave Kleikamp !(le32_to_cpu(es->s_feature_incompat) & 3843617ba13bSMingming Cao EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) { 3844b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "external journal has " 3845b31e1552SEric Sandeen "bad superblock"); 3846ac27a0ecSDave Kleikamp brelse(bh); 3847ac27a0ecSDave Kleikamp goto out_bdev; 3848ac27a0ecSDave Kleikamp } 3849ac27a0ecSDave Kleikamp 3850617ba13bSMingming Cao if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) { 3851b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "journal UUID does not match"); 3852ac27a0ecSDave Kleikamp brelse(bh); 3853ac27a0ecSDave Kleikamp goto out_bdev; 3854ac27a0ecSDave Kleikamp } 3855ac27a0ecSDave Kleikamp 3856bd81d8eeSLaurent Vivier len = ext4_blocks_count(es); 3857ac27a0ecSDave Kleikamp start = sb_block + 1; 3858ac27a0ecSDave Kleikamp brelse(bh); /* we're done with the superblock */ 3859ac27a0ecSDave Kleikamp 3860dab291afSMingming Cao journal = jbd2_journal_init_dev(bdev, sb->s_bdev, 3861ac27a0ecSDave Kleikamp start, len, blocksize); 3862ac27a0ecSDave Kleikamp if (!journal) { 3863b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "failed to create device journal"); 3864ac27a0ecSDave Kleikamp goto out_bdev; 3865ac27a0ecSDave Kleikamp } 3866ac27a0ecSDave Kleikamp journal->j_private = sb; 3867ac27a0ecSDave Kleikamp ll_rw_block(READ, 1, &journal->j_sb_buffer); 3868ac27a0ecSDave Kleikamp wait_on_buffer(journal->j_sb_buffer); 3869ac27a0ecSDave Kleikamp if (!buffer_uptodate(journal->j_sb_buffer)) { 3870b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "I/O error on journal device"); 3871ac27a0ecSDave Kleikamp goto out_journal; 3872ac27a0ecSDave Kleikamp } 3873ac27a0ecSDave Kleikamp if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) { 3874b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "External journal has more than one " 3875b31e1552SEric Sandeen "user (unsupported) - %d", 3876ac27a0ecSDave Kleikamp be32_to_cpu(journal->j_superblock->s_nr_users)); 3877ac27a0ecSDave Kleikamp goto out_journal; 3878ac27a0ecSDave Kleikamp } 3879617ba13bSMingming Cao EXT4_SB(sb)->journal_bdev = bdev; 3880617ba13bSMingming Cao ext4_init_journal_params(sb, journal); 3881ac27a0ecSDave Kleikamp return journal; 38820b8e58a1SAndreas Dilger 3883ac27a0ecSDave Kleikamp out_journal: 3884dab291afSMingming Cao jbd2_journal_destroy(journal); 3885ac27a0ecSDave Kleikamp out_bdev: 3886617ba13bSMingming Cao ext4_blkdev_put(bdev); 3887ac27a0ecSDave Kleikamp return NULL; 3888ac27a0ecSDave Kleikamp } 3889ac27a0ecSDave Kleikamp 3890617ba13bSMingming Cao static int ext4_load_journal(struct super_block *sb, 3891617ba13bSMingming Cao struct ext4_super_block *es, 3892ac27a0ecSDave Kleikamp unsigned long journal_devnum) 3893ac27a0ecSDave Kleikamp { 3894ac27a0ecSDave Kleikamp journal_t *journal; 3895ac27a0ecSDave Kleikamp unsigned int journal_inum = le32_to_cpu(es->s_journal_inum); 3896ac27a0ecSDave Kleikamp dev_t journal_dev; 3897ac27a0ecSDave Kleikamp int err = 0; 3898ac27a0ecSDave Kleikamp int really_read_only; 3899ac27a0ecSDave Kleikamp 39000390131bSFrank Mayhar BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)); 39010390131bSFrank Mayhar 3902ac27a0ecSDave Kleikamp if (journal_devnum && 3903ac27a0ecSDave Kleikamp journal_devnum != le32_to_cpu(es->s_journal_dev)) { 3904b31e1552SEric Sandeen ext4_msg(sb, KERN_INFO, "external journal device major/minor " 3905b31e1552SEric Sandeen "numbers have changed"); 3906ac27a0ecSDave Kleikamp journal_dev = new_decode_dev(journal_devnum); 3907ac27a0ecSDave Kleikamp } else 3908ac27a0ecSDave Kleikamp journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev)); 3909ac27a0ecSDave Kleikamp 3910ac27a0ecSDave Kleikamp really_read_only = bdev_read_only(sb->s_bdev); 3911ac27a0ecSDave Kleikamp 3912ac27a0ecSDave Kleikamp /* 3913ac27a0ecSDave Kleikamp * Are we loading a blank journal or performing recovery after a 3914ac27a0ecSDave Kleikamp * crash? For recovery, we need to check in advance whether we 3915ac27a0ecSDave Kleikamp * can get read-write access to the device. 3916ac27a0ecSDave Kleikamp */ 3917617ba13bSMingming Cao if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) { 3918ac27a0ecSDave Kleikamp if (sb->s_flags & MS_RDONLY) { 3919b31e1552SEric Sandeen ext4_msg(sb, KERN_INFO, "INFO: recovery " 3920b31e1552SEric Sandeen "required on readonly filesystem"); 3921ac27a0ecSDave Kleikamp if (really_read_only) { 3922b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "write access " 3923b31e1552SEric Sandeen "unavailable, cannot proceed"); 3924ac27a0ecSDave Kleikamp return -EROFS; 3925ac27a0ecSDave Kleikamp } 3926b31e1552SEric Sandeen ext4_msg(sb, KERN_INFO, "write access will " 3927b31e1552SEric Sandeen "be enabled during recovery"); 3928ac27a0ecSDave Kleikamp } 3929ac27a0ecSDave Kleikamp } 3930ac27a0ecSDave Kleikamp 3931ac27a0ecSDave Kleikamp if (journal_inum && journal_dev) { 3932b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "filesystem has both journal " 3933b31e1552SEric Sandeen "and inode journals!"); 3934ac27a0ecSDave Kleikamp return -EINVAL; 3935ac27a0ecSDave Kleikamp } 3936ac27a0ecSDave Kleikamp 3937ac27a0ecSDave Kleikamp if (journal_inum) { 3938617ba13bSMingming Cao if (!(journal = ext4_get_journal(sb, journal_inum))) 3939ac27a0ecSDave Kleikamp return -EINVAL; 3940ac27a0ecSDave Kleikamp } else { 3941617ba13bSMingming Cao if (!(journal = ext4_get_dev_journal(sb, journal_dev))) 3942ac27a0ecSDave Kleikamp return -EINVAL; 3943ac27a0ecSDave Kleikamp } 3944ac27a0ecSDave Kleikamp 394590576c0bSTheodore Ts'o if (!(journal->j_flags & JBD2_BARRIER)) 3946b31e1552SEric Sandeen ext4_msg(sb, KERN_INFO, "barriers disabled"); 39474776004fSTheodore Ts'o 3948617ba13bSMingming Cao if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) 3949dab291afSMingming Cao err = jbd2_journal_wipe(journal, !really_read_only); 39501c13d5c0STheodore Ts'o if (!err) { 39511c13d5c0STheodore Ts'o char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL); 39521c13d5c0STheodore Ts'o if (save) 39531c13d5c0STheodore Ts'o memcpy(save, ((char *) es) + 39541c13d5c0STheodore Ts'o EXT4_S_ERR_START, EXT4_S_ERR_LEN); 3955dab291afSMingming Cao err = jbd2_journal_load(journal); 39561c13d5c0STheodore Ts'o if (save) 39571c13d5c0STheodore Ts'o memcpy(((char *) es) + EXT4_S_ERR_START, 39581c13d5c0STheodore Ts'o save, EXT4_S_ERR_LEN); 39591c13d5c0STheodore Ts'o kfree(save); 39601c13d5c0STheodore Ts'o } 3961ac27a0ecSDave Kleikamp 3962ac27a0ecSDave Kleikamp if (err) { 3963b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "error loading journal"); 3964dab291afSMingming Cao jbd2_journal_destroy(journal); 3965ac27a0ecSDave Kleikamp return err; 3966ac27a0ecSDave Kleikamp } 3967ac27a0ecSDave Kleikamp 3968617ba13bSMingming Cao EXT4_SB(sb)->s_journal = journal; 3969617ba13bSMingming Cao ext4_clear_journal_err(sb, es); 3970ac27a0ecSDave Kleikamp 3971c41303ceSMaciej Żenczykowski if (!really_read_only && journal_devnum && 3972ac27a0ecSDave Kleikamp journal_devnum != le32_to_cpu(es->s_journal_dev)) { 3973ac27a0ecSDave Kleikamp es->s_journal_dev = cpu_to_le32(journal_devnum); 3974ac27a0ecSDave Kleikamp 3975ac27a0ecSDave Kleikamp /* Make sure we flush the recovery flag to disk. */ 3976e2d67052STheodore Ts'o ext4_commit_super(sb, 1); 3977ac27a0ecSDave Kleikamp } 3978ac27a0ecSDave Kleikamp 3979ac27a0ecSDave Kleikamp return 0; 3980ac27a0ecSDave Kleikamp } 3981ac27a0ecSDave Kleikamp 3982e2d67052STheodore Ts'o static int ext4_commit_super(struct super_block *sb, int sync) 3983ac27a0ecSDave Kleikamp { 3984e2d67052STheodore Ts'o struct ext4_super_block *es = EXT4_SB(sb)->s_es; 3985617ba13bSMingming Cao struct buffer_head *sbh = EXT4_SB(sb)->s_sbh; 3986c4be0c1dSTakashi Sato int error = 0; 3987ac27a0ecSDave Kleikamp 39887c2e7087STheodore Ts'o if (!sbh || block_device_ejected(sb)) 3989c4be0c1dSTakashi Sato return error; 3990914258bfSTheodore Ts'o if (buffer_write_io_error(sbh)) { 3991914258bfSTheodore Ts'o /* 3992914258bfSTheodore Ts'o * Oh, dear. A previous attempt to write the 3993914258bfSTheodore Ts'o * superblock failed. This could happen because the 3994914258bfSTheodore Ts'o * USB device was yanked out. Or it could happen to 3995914258bfSTheodore Ts'o * be a transient write error and maybe the block will 3996914258bfSTheodore Ts'o * be remapped. Nothing we can do but to retry the 3997914258bfSTheodore Ts'o * write and hope for the best. 3998914258bfSTheodore Ts'o */ 3999b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "previous I/O error to " 4000b31e1552SEric Sandeen "superblock detected"); 4001914258bfSTheodore Ts'o clear_buffer_write_io_error(sbh); 4002914258bfSTheodore Ts'o set_buffer_uptodate(sbh); 4003914258bfSTheodore Ts'o } 400471290b36STheodore Ts'o /* 400571290b36STheodore Ts'o * If the file system is mounted read-only, don't update the 400671290b36STheodore Ts'o * superblock write time. This avoids updating the superblock 400771290b36STheodore Ts'o * write time when we are mounting the root file system 400871290b36STheodore Ts'o * read/only but we need to replay the journal; at that point, 400971290b36STheodore Ts'o * for people who are east of GMT and who make their clock 401071290b36STheodore Ts'o * tick in localtime for Windows bug-for-bug compatibility, 401171290b36STheodore Ts'o * the clock is set in the future, and this will cause e2fsck 401271290b36STheodore Ts'o * to complain and force a full file system check. 401371290b36STheodore Ts'o */ 401471290b36STheodore Ts'o if (!(sb->s_flags & MS_RDONLY)) 4015ac27a0ecSDave Kleikamp es->s_wtime = cpu_to_le32(get_seconds()); 4016f613dfcbSTheodore Ts'o if (sb->s_bdev->bd_part) 4017afc32f7eSTheodore Ts'o es->s_kbytes_written = 4018afc32f7eSTheodore Ts'o cpu_to_le64(EXT4_SB(sb)->s_kbytes_written + 4019afc32f7eSTheodore Ts'o ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) - 4020afc32f7eSTheodore Ts'o EXT4_SB(sb)->s_sectors_written_start) >> 1)); 4021f613dfcbSTheodore Ts'o else 4022f613dfcbSTheodore Ts'o es->s_kbytes_written = 4023f613dfcbSTheodore Ts'o cpu_to_le64(EXT4_SB(sb)->s_kbytes_written); 402457042651STheodore Ts'o ext4_free_blocks_count_set(es, 402557042651STheodore Ts'o EXT4_C2B(EXT4_SB(sb), percpu_counter_sum_positive( 402657042651STheodore Ts'o &EXT4_SB(sb)->s_freeclusters_counter))); 40277f93cff9STheodore Ts'o es->s_free_inodes_count = 40287f93cff9STheodore Ts'o cpu_to_le32(percpu_counter_sum_positive( 40295d1b1b3fSAneesh Kumar K.V &EXT4_SB(sb)->s_freeinodes_counter)); 40307234ab2aSTheodore Ts'o sb->s_dirt = 0; 4031ac27a0ecSDave Kleikamp BUFFER_TRACE(sbh, "marking dirty"); 4032ac27a0ecSDave Kleikamp mark_buffer_dirty(sbh); 4033914258bfSTheodore Ts'o if (sync) { 4034c4be0c1dSTakashi Sato error = sync_dirty_buffer(sbh); 4035c4be0c1dSTakashi Sato if (error) 4036c4be0c1dSTakashi Sato return error; 4037c4be0c1dSTakashi Sato 4038c4be0c1dSTakashi Sato error = buffer_write_io_error(sbh); 4039c4be0c1dSTakashi Sato if (error) { 4040b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, "I/O error while writing " 4041b31e1552SEric Sandeen "superblock"); 4042914258bfSTheodore Ts'o clear_buffer_write_io_error(sbh); 4043914258bfSTheodore Ts'o set_buffer_uptodate(sbh); 4044914258bfSTheodore Ts'o } 4045914258bfSTheodore Ts'o } 4046c4be0c1dSTakashi Sato return error; 4047ac27a0ecSDave Kleikamp } 4048ac27a0ecSDave Kleikamp 4049ac27a0ecSDave Kleikamp /* 4050ac27a0ecSDave Kleikamp * Have we just finished recovery? If so, and if we are mounting (or 4051ac27a0ecSDave Kleikamp * remounting) the filesystem readonly, then we will end up with a 4052ac27a0ecSDave Kleikamp * consistent fs on disk. Record that fact. 4053ac27a0ecSDave Kleikamp */ 4054617ba13bSMingming Cao static void ext4_mark_recovery_complete(struct super_block *sb, 4055617ba13bSMingming Cao struct ext4_super_block *es) 4056ac27a0ecSDave Kleikamp { 4057617ba13bSMingming Cao journal_t *journal = EXT4_SB(sb)->s_journal; 4058ac27a0ecSDave Kleikamp 40590390131bSFrank Mayhar if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) { 40600390131bSFrank Mayhar BUG_ON(journal != NULL); 40610390131bSFrank Mayhar return; 40620390131bSFrank Mayhar } 4063dab291afSMingming Cao jbd2_journal_lock_updates(journal); 40647ffe1ea8SHidehiro Kawai if (jbd2_journal_flush(journal) < 0) 40657ffe1ea8SHidehiro Kawai goto out; 40667ffe1ea8SHidehiro Kawai 4067617ba13bSMingming Cao if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) && 4068ac27a0ecSDave Kleikamp sb->s_flags & MS_RDONLY) { 4069617ba13bSMingming Cao EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER); 4070e2d67052STheodore Ts'o ext4_commit_super(sb, 1); 4071ac27a0ecSDave Kleikamp } 40727ffe1ea8SHidehiro Kawai 40737ffe1ea8SHidehiro Kawai out: 4074dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 4075ac27a0ecSDave Kleikamp } 4076ac27a0ecSDave Kleikamp 4077ac27a0ecSDave Kleikamp /* 4078ac27a0ecSDave Kleikamp * If we are mounting (or read-write remounting) a filesystem whose journal 4079ac27a0ecSDave Kleikamp * has recorded an error from a previous lifetime, move that error to the 4080ac27a0ecSDave Kleikamp * main filesystem now. 4081ac27a0ecSDave Kleikamp */ 4082617ba13bSMingming Cao static void ext4_clear_journal_err(struct super_block *sb, 4083617ba13bSMingming Cao struct ext4_super_block *es) 4084ac27a0ecSDave Kleikamp { 4085ac27a0ecSDave Kleikamp journal_t *journal; 4086ac27a0ecSDave Kleikamp int j_errno; 4087ac27a0ecSDave Kleikamp const char *errstr; 4088ac27a0ecSDave Kleikamp 40890390131bSFrank Mayhar BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)); 40900390131bSFrank Mayhar 4091617ba13bSMingming Cao journal = EXT4_SB(sb)->s_journal; 4092ac27a0ecSDave Kleikamp 4093ac27a0ecSDave Kleikamp /* 4094ac27a0ecSDave Kleikamp * Now check for any error status which may have been recorded in the 4095617ba13bSMingming Cao * journal by a prior ext4_error() or ext4_abort() 4096ac27a0ecSDave Kleikamp */ 4097ac27a0ecSDave Kleikamp 4098dab291afSMingming Cao j_errno = jbd2_journal_errno(journal); 4099ac27a0ecSDave Kleikamp if (j_errno) { 4100ac27a0ecSDave Kleikamp char nbuf[16]; 4101ac27a0ecSDave Kleikamp 4102617ba13bSMingming Cao errstr = ext4_decode_error(sb, j_errno, nbuf); 410312062dddSEric Sandeen ext4_warning(sb, "Filesystem error recorded " 4104ac27a0ecSDave Kleikamp "from previous mount: %s", errstr); 410512062dddSEric Sandeen ext4_warning(sb, "Marking fs in need of filesystem check."); 4106ac27a0ecSDave Kleikamp 4107617ba13bSMingming Cao EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS; 4108617ba13bSMingming Cao es->s_state |= cpu_to_le16(EXT4_ERROR_FS); 4109e2d67052STheodore Ts'o ext4_commit_super(sb, 1); 4110ac27a0ecSDave Kleikamp 4111dab291afSMingming Cao jbd2_journal_clear_err(journal); 4112ac27a0ecSDave Kleikamp } 4113ac27a0ecSDave Kleikamp } 4114ac27a0ecSDave Kleikamp 4115ac27a0ecSDave Kleikamp /* 4116ac27a0ecSDave Kleikamp * Force the running and committing transactions to commit, 4117ac27a0ecSDave Kleikamp * and wait on the commit. 4118ac27a0ecSDave Kleikamp */ 4119617ba13bSMingming Cao int ext4_force_commit(struct super_block *sb) 4120ac27a0ecSDave Kleikamp { 4121ac27a0ecSDave Kleikamp journal_t *journal; 41220390131bSFrank Mayhar int ret = 0; 4123ac27a0ecSDave Kleikamp 4124ac27a0ecSDave Kleikamp if (sb->s_flags & MS_RDONLY) 4125ac27a0ecSDave Kleikamp return 0; 4126ac27a0ecSDave Kleikamp 4127617ba13bSMingming Cao journal = EXT4_SB(sb)->s_journal; 41286b0310fbSEric Sandeen if (journal) { 4129437f88ccSEric Sandeen vfs_check_frozen(sb, SB_FREEZE_TRANS); 4130617ba13bSMingming Cao ret = ext4_journal_force_commit(journal); 41316b0310fbSEric Sandeen } 41320390131bSFrank Mayhar 4133ac27a0ecSDave Kleikamp return ret; 4134ac27a0ecSDave Kleikamp } 4135ac27a0ecSDave Kleikamp 4136617ba13bSMingming Cao static void ext4_write_super(struct super_block *sb) 4137ac27a0ecSDave Kleikamp { 4138ebc1ac16SChristoph Hellwig lock_super(sb); 4139e2d67052STheodore Ts'o ext4_commit_super(sb, 1); 4140ebc1ac16SChristoph Hellwig unlock_super(sb); 41410390131bSFrank Mayhar } 4142ac27a0ecSDave Kleikamp 4143617ba13bSMingming Cao static int ext4_sync_fs(struct super_block *sb, int wait) 4144ac27a0ecSDave Kleikamp { 414514ce0cb4STheodore Ts'o int ret = 0; 41469eddacf9SJan Kara tid_t target; 41478d5d02e6SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 4148ac27a0ecSDave Kleikamp 41499bffad1eSTheodore Ts'o trace_ext4_sync_fs(sb, wait); 41508d5d02e6SMingming Cao flush_workqueue(sbi->dio_unwritten_wq); 41518d5d02e6SMingming Cao if (jbd2_journal_start_commit(sbi->s_journal, &target)) { 4152ac27a0ecSDave Kleikamp if (wait) 41538d5d02e6SMingming Cao jbd2_log_wait_commit(sbi->s_journal, target); 41540390131bSFrank Mayhar } 415514ce0cb4STheodore Ts'o return ret; 4156ac27a0ecSDave Kleikamp } 4157ac27a0ecSDave Kleikamp 4158ac27a0ecSDave Kleikamp /* 4159ac27a0ecSDave Kleikamp * LVM calls this function before a (read-only) snapshot is created. This 4160ac27a0ecSDave Kleikamp * gives us a chance to flush the journal completely and mark the fs clean. 4161be4f27d3SYongqiang Yang * 4162be4f27d3SYongqiang Yang * Note that only this function cannot bring a filesystem to be in a clean 4163be4f27d3SYongqiang Yang * state independently, because ext4 prevents a new handle from being started 4164be4f27d3SYongqiang Yang * by @sb->s_frozen, which stays in an upper layer. It thus needs help from 4165be4f27d3SYongqiang Yang * the upper layer. 4166ac27a0ecSDave Kleikamp */ 4167c4be0c1dSTakashi Sato static int ext4_freeze(struct super_block *sb) 4168ac27a0ecSDave Kleikamp { 4169c4be0c1dSTakashi Sato int error = 0; 4170c4be0c1dSTakashi Sato journal_t *journal; 4171ac27a0ecSDave Kleikamp 41729ca92389STheodore Ts'o if (sb->s_flags & MS_RDONLY) 41739ca92389STheodore Ts'o return 0; 41749ca92389STheodore Ts'o 4175c4be0c1dSTakashi Sato journal = EXT4_SB(sb)->s_journal; 4176ac27a0ecSDave Kleikamp 4177ac27a0ecSDave Kleikamp /* Now we set up the journal barrier. */ 4178dab291afSMingming Cao jbd2_journal_lock_updates(journal); 41797ffe1ea8SHidehiro Kawai 41807ffe1ea8SHidehiro Kawai /* 41819ca92389STheodore Ts'o * Don't clear the needs_recovery flag if we failed to flush 41829ca92389STheodore Ts'o * the journal. 41837ffe1ea8SHidehiro Kawai */ 4184c4be0c1dSTakashi Sato error = jbd2_journal_flush(journal); 41856b0310fbSEric Sandeen if (error < 0) 41866b0310fbSEric Sandeen goto out; 4187ac27a0ecSDave Kleikamp 4188ac27a0ecSDave Kleikamp /* Journal blocked and flushed, clear needs_recovery flag. */ 4189617ba13bSMingming Cao EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER); 4190e2d67052STheodore Ts'o error = ext4_commit_super(sb, 1); 41916b0310fbSEric Sandeen out: 41926b0310fbSEric Sandeen /* we rely on s_frozen to stop further updates */ 41936b0310fbSEric Sandeen jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); 41946b0310fbSEric Sandeen return error; 4195ac27a0ecSDave Kleikamp } 4196ac27a0ecSDave Kleikamp 4197ac27a0ecSDave Kleikamp /* 4198ac27a0ecSDave Kleikamp * Called by LVM after the snapshot is done. We need to reset the RECOVER 4199ac27a0ecSDave Kleikamp * flag here, even though the filesystem is not technically dirty yet. 4200ac27a0ecSDave Kleikamp */ 4201c4be0c1dSTakashi Sato static int ext4_unfreeze(struct super_block *sb) 4202ac27a0ecSDave Kleikamp { 42039ca92389STheodore Ts'o if (sb->s_flags & MS_RDONLY) 42049ca92389STheodore Ts'o return 0; 42059ca92389STheodore Ts'o 4206ac27a0ecSDave Kleikamp lock_super(sb); 42079ca92389STheodore Ts'o /* Reset the needs_recovery flag before the fs is unlocked. */ 4208617ba13bSMingming Cao EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER); 4209e2d67052STheodore Ts'o ext4_commit_super(sb, 1); 4210ac27a0ecSDave Kleikamp unlock_super(sb); 4211c4be0c1dSTakashi Sato return 0; 4212ac27a0ecSDave Kleikamp } 4213ac27a0ecSDave Kleikamp 4214673c6100STheodore Ts'o /* 4215673c6100STheodore Ts'o * Structure to save mount options for ext4_remount's benefit 4216673c6100STheodore Ts'o */ 4217673c6100STheodore Ts'o struct ext4_mount_options { 4218673c6100STheodore Ts'o unsigned long s_mount_opt; 4219a2595b8aSTheodore Ts'o unsigned long s_mount_opt2; 4220673c6100STheodore Ts'o uid_t s_resuid; 4221673c6100STheodore Ts'o gid_t s_resgid; 4222673c6100STheodore Ts'o unsigned long s_commit_interval; 4223673c6100STheodore Ts'o u32 s_min_batch_time, s_max_batch_time; 4224673c6100STheodore Ts'o #ifdef CONFIG_QUOTA 4225673c6100STheodore Ts'o int s_jquota_fmt; 4226673c6100STheodore Ts'o char *s_qf_names[MAXQUOTAS]; 4227673c6100STheodore Ts'o #endif 4228673c6100STheodore Ts'o }; 4229673c6100STheodore Ts'o 4230617ba13bSMingming Cao static int ext4_remount(struct super_block *sb, int *flags, char *data) 4231ac27a0ecSDave Kleikamp { 4232617ba13bSMingming Cao struct ext4_super_block *es; 4233617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 4234ac27a0ecSDave Kleikamp unsigned long old_sb_flags; 4235617ba13bSMingming Cao struct ext4_mount_options old_opts; 4236c79d967dSChristoph Hellwig int enable_quota = 0; 42378a266467STheodore Ts'o ext4_group_t g; 4238b3881f74STheodore Ts'o unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO; 4239c5e06d10SJohann Lombardi int err = 0; 4240ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 4241ac27a0ecSDave Kleikamp int i; 4242ac27a0ecSDave Kleikamp #endif 4243d4c402d9SCurt Wohlgemuth char *orig_data = kstrdup(data, GFP_KERNEL); 4244ac27a0ecSDave Kleikamp 4245ac27a0ecSDave Kleikamp /* Store the original options */ 4246bbd6851aSAl Viro lock_super(sb); 4247ac27a0ecSDave Kleikamp old_sb_flags = sb->s_flags; 4248ac27a0ecSDave Kleikamp old_opts.s_mount_opt = sbi->s_mount_opt; 4249a2595b8aSTheodore Ts'o old_opts.s_mount_opt2 = sbi->s_mount_opt2; 4250ac27a0ecSDave Kleikamp old_opts.s_resuid = sbi->s_resuid; 4251ac27a0ecSDave Kleikamp old_opts.s_resgid = sbi->s_resgid; 4252ac27a0ecSDave Kleikamp old_opts.s_commit_interval = sbi->s_commit_interval; 425330773840STheodore Ts'o old_opts.s_min_batch_time = sbi->s_min_batch_time; 425430773840STheodore Ts'o old_opts.s_max_batch_time = sbi->s_max_batch_time; 4255ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 4256ac27a0ecSDave Kleikamp old_opts.s_jquota_fmt = sbi->s_jquota_fmt; 4257ac27a0ecSDave Kleikamp for (i = 0; i < MAXQUOTAS; i++) 4258ac27a0ecSDave Kleikamp old_opts.s_qf_names[i] = sbi->s_qf_names[i]; 4259ac27a0ecSDave Kleikamp #endif 4260b3881f74STheodore Ts'o if (sbi->s_journal && sbi->s_journal->j_task->io_context) 4261b3881f74STheodore Ts'o journal_ioprio = sbi->s_journal->j_task->io_context->ioprio; 4262ac27a0ecSDave Kleikamp 4263ac27a0ecSDave Kleikamp /* 4264ac27a0ecSDave Kleikamp * Allow the "check" option to be passed as a remount option. 4265ac27a0ecSDave Kleikamp */ 4266661aa520SEric Sandeen if (!parse_options(data, sb, NULL, &journal_ioprio, 1)) { 4267ac27a0ecSDave Kleikamp err = -EINVAL; 4268ac27a0ecSDave Kleikamp goto restore_opts; 4269ac27a0ecSDave Kleikamp } 4270ac27a0ecSDave Kleikamp 42714ab2f15bSTheodore Ts'o if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) 4272c67d859eSTheodore Ts'o ext4_abort(sb, "Abort forced by user"); 4273ac27a0ecSDave Kleikamp 4274ac27a0ecSDave Kleikamp sb->s_flags = (sb->s_flags & ~MS_POSIXACL) | 4275482a7425SDmitry Monakhov (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0); 4276ac27a0ecSDave Kleikamp 4277ac27a0ecSDave Kleikamp es = sbi->s_es; 4278ac27a0ecSDave Kleikamp 4279b3881f74STheodore Ts'o if (sbi->s_journal) { 4280617ba13bSMingming Cao ext4_init_journal_params(sb, sbi->s_journal); 4281b3881f74STheodore Ts'o set_task_ioprio(sbi->s_journal->j_task, journal_ioprio); 4282b3881f74STheodore Ts'o } 4283ac27a0ecSDave Kleikamp 4284661aa520SEric Sandeen if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) { 42854ab2f15bSTheodore Ts'o if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) { 4286ac27a0ecSDave Kleikamp err = -EROFS; 4287ac27a0ecSDave Kleikamp goto restore_opts; 4288ac27a0ecSDave Kleikamp } 4289ac27a0ecSDave Kleikamp 4290ac27a0ecSDave Kleikamp if (*flags & MS_RDONLY) { 42910f0dd62fSChristoph Hellwig err = dquot_suspend(sb, -1); 42920f0dd62fSChristoph Hellwig if (err < 0) 4293c79d967dSChristoph Hellwig goto restore_opts; 4294c79d967dSChristoph Hellwig 4295ac27a0ecSDave Kleikamp /* 4296ac27a0ecSDave Kleikamp * First of all, the unconditional stuff we have to do 4297ac27a0ecSDave Kleikamp * to disable replay of the journal when we next remount 4298ac27a0ecSDave Kleikamp */ 4299ac27a0ecSDave Kleikamp sb->s_flags |= MS_RDONLY; 4300ac27a0ecSDave Kleikamp 4301ac27a0ecSDave Kleikamp /* 4302ac27a0ecSDave Kleikamp * OK, test if we are remounting a valid rw partition 4303ac27a0ecSDave Kleikamp * readonly, and if so set the rdonly flag and then 4304ac27a0ecSDave Kleikamp * mark the partition as valid again. 4305ac27a0ecSDave Kleikamp */ 4306617ba13bSMingming Cao if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) && 4307617ba13bSMingming Cao (sbi->s_mount_state & EXT4_VALID_FS)) 4308ac27a0ecSDave Kleikamp es->s_state = cpu_to_le16(sbi->s_mount_state); 4309ac27a0ecSDave Kleikamp 4310a63c9eb2STheodore Ts'o if (sbi->s_journal) 4311617ba13bSMingming Cao ext4_mark_recovery_complete(sb, es); 4312ac27a0ecSDave Kleikamp } else { 4313a13fb1a4SEric Sandeen /* Make sure we can mount this feature set readwrite */ 4314a13fb1a4SEric Sandeen if (!ext4_feature_set_ok(sb, 0)) { 4315ac27a0ecSDave Kleikamp err = -EROFS; 4316ac27a0ecSDave Kleikamp goto restore_opts; 4317ac27a0ecSDave Kleikamp } 4318ead6596bSEric Sandeen /* 43198a266467STheodore Ts'o * Make sure the group descriptor checksums 43200b8e58a1SAndreas Dilger * are sane. If they aren't, refuse to remount r/w. 43218a266467STheodore Ts'o */ 43228a266467STheodore Ts'o for (g = 0; g < sbi->s_groups_count; g++) { 43238a266467STheodore Ts'o struct ext4_group_desc *gdp = 43248a266467STheodore Ts'o ext4_get_group_desc(sb, g, NULL); 43258a266467STheodore Ts'o 43268a266467STheodore Ts'o if (!ext4_group_desc_csum_verify(sbi, g, gdp)) { 4327b31e1552SEric Sandeen ext4_msg(sb, KERN_ERR, 4328b31e1552SEric Sandeen "ext4_remount: Checksum for group %u failed (%u!=%u)", 43298a266467STheodore Ts'o g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)), 43308a266467STheodore Ts'o le16_to_cpu(gdp->bg_checksum)); 43318a266467STheodore Ts'o err = -EINVAL; 43328a266467STheodore Ts'o goto restore_opts; 43338a266467STheodore Ts'o } 43348a266467STheodore Ts'o } 43358a266467STheodore Ts'o 43368a266467STheodore Ts'o /* 4337ead6596bSEric Sandeen * If we have an unprocessed orphan list hanging 4338ead6596bSEric Sandeen * around from a previously readonly bdev mount, 4339ead6596bSEric Sandeen * require a full umount/remount for now. 4340ead6596bSEric Sandeen */ 4341ead6596bSEric Sandeen if (es->s_last_orphan) { 4342b31e1552SEric Sandeen ext4_msg(sb, KERN_WARNING, "Couldn't " 4343ead6596bSEric Sandeen "remount RDWR because of unprocessed " 4344ead6596bSEric Sandeen "orphan inode list. Please " 4345b31e1552SEric Sandeen "umount/remount instead"); 4346ead6596bSEric Sandeen err = -EINVAL; 4347ead6596bSEric Sandeen goto restore_opts; 4348ead6596bSEric Sandeen } 4349ead6596bSEric Sandeen 4350ac27a0ecSDave Kleikamp /* 4351ac27a0ecSDave Kleikamp * Mounting a RDONLY partition read-write, so reread 4352ac27a0ecSDave Kleikamp * and store the current valid flag. (It may have 4353ac27a0ecSDave Kleikamp * been changed by e2fsck since we originally mounted 4354ac27a0ecSDave Kleikamp * the partition.) 4355ac27a0ecSDave Kleikamp */ 43560390131bSFrank Mayhar if (sbi->s_journal) 4357617ba13bSMingming Cao ext4_clear_journal_err(sb, es); 4358ac27a0ecSDave Kleikamp sbi->s_mount_state = le16_to_cpu(es->s_state); 4359617ba13bSMingming Cao if (!ext4_setup_super(sb, es, 0)) 4360ac27a0ecSDave Kleikamp sb->s_flags &= ~MS_RDONLY; 4361c5e06d10SJohann Lombardi if (EXT4_HAS_INCOMPAT_FEATURE(sb, 4362c5e06d10SJohann Lombardi EXT4_FEATURE_INCOMPAT_MMP)) 4363c5e06d10SJohann Lombardi if (ext4_multi_mount_protect(sb, 4364c5e06d10SJohann Lombardi le64_to_cpu(es->s_mmp_block))) { 4365c5e06d10SJohann Lombardi err = -EROFS; 4366c5e06d10SJohann Lombardi goto restore_opts; 4367c5e06d10SJohann Lombardi } 4368c79d967dSChristoph Hellwig enable_quota = 1; 4369ac27a0ecSDave Kleikamp } 4370ac27a0ecSDave Kleikamp } 4371bfff6873SLukas Czerner 4372bfff6873SLukas Czerner /* 4373bfff6873SLukas Czerner * Reinitialize lazy itable initialization thread based on 4374bfff6873SLukas Czerner * current settings 4375bfff6873SLukas Czerner */ 4376bfff6873SLukas Czerner if ((sb->s_flags & MS_RDONLY) || !test_opt(sb, INIT_INODE_TABLE)) 4377bfff6873SLukas Czerner ext4_unregister_li_request(sb); 4378bfff6873SLukas Czerner else { 4379bfff6873SLukas Czerner ext4_group_t first_not_zeroed; 4380bfff6873SLukas Czerner first_not_zeroed = ext4_has_uninit_itable(sb); 4381bfff6873SLukas Czerner ext4_register_li_request(sb, first_not_zeroed); 4382bfff6873SLukas Czerner } 4383bfff6873SLukas Czerner 43846fd058f7STheodore Ts'o ext4_setup_system_zone(sb); 43850390131bSFrank Mayhar if (sbi->s_journal == NULL) 4386e2d67052STheodore Ts'o ext4_commit_super(sb, 1); 43870390131bSFrank Mayhar 4388ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 4389ac27a0ecSDave Kleikamp /* Release old quota file names */ 4390ac27a0ecSDave Kleikamp for (i = 0; i < MAXQUOTAS; i++) 4391ac27a0ecSDave Kleikamp if (old_opts.s_qf_names[i] && 4392ac27a0ecSDave Kleikamp old_opts.s_qf_names[i] != sbi->s_qf_names[i]) 4393ac27a0ecSDave Kleikamp kfree(old_opts.s_qf_names[i]); 4394ac27a0ecSDave Kleikamp #endif 4395bbd6851aSAl Viro unlock_super(sb); 4396c79d967dSChristoph Hellwig if (enable_quota) 43970f0dd62fSChristoph Hellwig dquot_resume(sb, -1); 4398d4c402d9SCurt Wohlgemuth 4399d4c402d9SCurt Wohlgemuth ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data); 4400d4c402d9SCurt Wohlgemuth kfree(orig_data); 4401ac27a0ecSDave Kleikamp return 0; 44020b8e58a1SAndreas Dilger 4403ac27a0ecSDave Kleikamp restore_opts: 4404ac27a0ecSDave Kleikamp sb->s_flags = old_sb_flags; 4405ac27a0ecSDave Kleikamp sbi->s_mount_opt = old_opts.s_mount_opt; 4406a2595b8aSTheodore Ts'o sbi->s_mount_opt2 = old_opts.s_mount_opt2; 4407ac27a0ecSDave Kleikamp sbi->s_resuid = old_opts.s_resuid; 4408ac27a0ecSDave Kleikamp sbi->s_resgid = old_opts.s_resgid; 4409ac27a0ecSDave Kleikamp sbi->s_commit_interval = old_opts.s_commit_interval; 441030773840STheodore Ts'o sbi->s_min_batch_time = old_opts.s_min_batch_time; 441130773840STheodore Ts'o sbi->s_max_batch_time = old_opts.s_max_batch_time; 4412ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 4413ac27a0ecSDave Kleikamp sbi->s_jquota_fmt = old_opts.s_jquota_fmt; 4414ac27a0ecSDave Kleikamp for (i = 0; i < MAXQUOTAS; i++) { 4415ac27a0ecSDave Kleikamp if (sbi->s_qf_names[i] && 4416ac27a0ecSDave Kleikamp old_opts.s_qf_names[i] != sbi->s_qf_names[i]) 4417ac27a0ecSDave Kleikamp kfree(sbi->s_qf_names[i]); 4418ac27a0ecSDave Kleikamp sbi->s_qf_names[i] = old_opts.s_qf_names[i]; 4419ac27a0ecSDave Kleikamp } 4420ac27a0ecSDave Kleikamp #endif 4421bbd6851aSAl Viro unlock_super(sb); 4422d4c402d9SCurt Wohlgemuth kfree(orig_data); 4423ac27a0ecSDave Kleikamp return err; 4424ac27a0ecSDave Kleikamp } 4425ac27a0ecSDave Kleikamp 4426f975d6bcSTheodore Ts'o /* 4427f975d6bcSTheodore Ts'o * Note: calculating the overhead so we can be compatible with 4428f975d6bcSTheodore Ts'o * historical BSD practice is quite difficult in the face of 4429f975d6bcSTheodore Ts'o * clusters/bigalloc. This is because multiple metadata blocks from 4430f975d6bcSTheodore Ts'o * different block group can end up in the same allocation cluster. 4431f975d6bcSTheodore Ts'o * Calculating the exact overhead in the face of clustered allocation 4432f975d6bcSTheodore Ts'o * requires either O(all block bitmaps) in memory or O(number of block 4433f975d6bcSTheodore Ts'o * groups**2) in time. We will still calculate the superblock for 4434f975d6bcSTheodore Ts'o * older file systems --- and if we come across with a bigalloc file 4435f975d6bcSTheodore Ts'o * system with zero in s_overhead_clusters the estimate will be close to 4436f975d6bcSTheodore Ts'o * correct especially for very large cluster sizes --- but for newer 4437f975d6bcSTheodore Ts'o * file systems, it's better to calculate this figure once at mkfs 4438f975d6bcSTheodore Ts'o * time, and store it in the superblock. If the superblock value is 4439f975d6bcSTheodore Ts'o * present (even for non-bigalloc file systems), we will use it. 4440f975d6bcSTheodore Ts'o */ 4441617ba13bSMingming Cao static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf) 4442ac27a0ecSDave Kleikamp { 4443ac27a0ecSDave Kleikamp struct super_block *sb = dentry->d_sb; 4444617ba13bSMingming Cao struct ext4_sb_info *sbi = EXT4_SB(sb); 4445617ba13bSMingming Cao struct ext4_super_block *es = sbi->s_es; 4446f975d6bcSTheodore Ts'o struct ext4_group_desc *gdp; 4447960cc398SPekka Enberg u64 fsid; 4448d02a9391SKazuya Mio s64 bfree; 4449ac27a0ecSDave Kleikamp 44505e70030dSBadari Pulavarty if (test_opt(sb, MINIX_DF)) { 44515e70030dSBadari Pulavarty sbi->s_overhead_last = 0; 4452f975d6bcSTheodore Ts'o } else if (es->s_overhead_clusters) { 4453f975d6bcSTheodore Ts'o sbi->s_overhead_last = le32_to_cpu(es->s_overhead_clusters); 44546bc9feffSAneesh Kumar K.V } else if (sbi->s_blocks_last != ext4_blocks_count(es)) { 44558df9675fSTheodore Ts'o ext4_group_t i, ngroups = ext4_get_groups_count(sb); 44565e70030dSBadari Pulavarty ext4_fsblk_t overhead = 0; 4457ac27a0ecSDave Kleikamp 4458ac27a0ecSDave Kleikamp /* 44595e70030dSBadari Pulavarty * Compute the overhead (FS structures). This is constant 44605e70030dSBadari Pulavarty * for a given filesystem unless the number of block groups 44615e70030dSBadari Pulavarty * changes so we cache the previous value until it does. 4462ac27a0ecSDave Kleikamp */ 4463ac27a0ecSDave Kleikamp 4464ac27a0ecSDave Kleikamp /* 4465ac27a0ecSDave Kleikamp * All of the blocks before first_data_block are 4466ac27a0ecSDave Kleikamp * overhead 4467ac27a0ecSDave Kleikamp */ 4468f975d6bcSTheodore Ts'o overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block)); 4469ac27a0ecSDave Kleikamp 4470ac27a0ecSDave Kleikamp /* 4471f975d6bcSTheodore Ts'o * Add the overhead found in each block group 4472ac27a0ecSDave Kleikamp */ 4473ac27a0ecSDave Kleikamp for (i = 0; i < ngroups; i++) { 4474f975d6bcSTheodore Ts'o gdp = ext4_get_group_desc(sb, i, NULL); 4475f975d6bcSTheodore Ts'o overhead += ext4_num_overhead_clusters(sb, i, gdp); 4476ac27a0ecSDave Kleikamp cond_resched(); 4477ac27a0ecSDave Kleikamp } 44785e70030dSBadari Pulavarty sbi->s_overhead_last = overhead; 44795e70030dSBadari Pulavarty smp_wmb(); 44806bc9feffSAneesh Kumar K.V sbi->s_blocks_last = ext4_blocks_count(es); 4481ac27a0ecSDave Kleikamp } 4482ac27a0ecSDave Kleikamp 4483617ba13bSMingming Cao buf->f_type = EXT4_SUPER_MAGIC; 4484ac27a0ecSDave Kleikamp buf->f_bsize = sb->s_blocksize; 4485f975d6bcSTheodore Ts'o buf->f_blocks = (ext4_blocks_count(es) - 4486f975d6bcSTheodore Ts'o EXT4_C2B(sbi, sbi->s_overhead_last)); 448757042651STheodore Ts'o bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) - 448857042651STheodore Ts'o percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter); 4489d02a9391SKazuya Mio /* prevent underflow in case that few free space is available */ 449057042651STheodore Ts'o buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0)); 4491bd81d8eeSLaurent Vivier buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es); 4492bd81d8eeSLaurent Vivier if (buf->f_bfree < ext4_r_blocks_count(es)) 4493ac27a0ecSDave Kleikamp buf->f_bavail = 0; 4494ac27a0ecSDave Kleikamp buf->f_files = le32_to_cpu(es->s_inodes_count); 449552d9f3b4SPeter Zijlstra buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter); 4496617ba13bSMingming Cao buf->f_namelen = EXT4_NAME_LEN; 4497960cc398SPekka Enberg fsid = le64_to_cpup((void *)es->s_uuid) ^ 4498960cc398SPekka Enberg le64_to_cpup((void *)es->s_uuid + sizeof(u64)); 4499960cc398SPekka Enberg buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL; 4500960cc398SPekka Enberg buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL; 45010b8e58a1SAndreas Dilger 4502ac27a0ecSDave Kleikamp return 0; 4503ac27a0ecSDave Kleikamp } 4504ac27a0ecSDave Kleikamp 45050b8e58a1SAndreas Dilger /* Helper function for writing quotas on sync - we need to start transaction 45060b8e58a1SAndreas Dilger * before quota file is locked for write. Otherwise the are possible deadlocks: 4507ac27a0ecSDave Kleikamp * Process 1 Process 2 4508617ba13bSMingming Cao * ext4_create() quota_sync() 4509dab291afSMingming Cao * jbd2_journal_start() write_dquot() 4510871a2931SChristoph Hellwig * dquot_initialize() down(dqio_mutex) 4511dab291afSMingming Cao * down(dqio_mutex) jbd2_journal_start() 4512ac27a0ecSDave Kleikamp * 4513ac27a0ecSDave Kleikamp */ 4514ac27a0ecSDave Kleikamp 4515ac27a0ecSDave Kleikamp #ifdef CONFIG_QUOTA 4516ac27a0ecSDave Kleikamp 4517ac27a0ecSDave Kleikamp static inline struct inode *dquot_to_inode(struct dquot *dquot) 4518ac27a0ecSDave Kleikamp { 4519ac27a0ecSDave Kleikamp return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type]; 4520ac27a0ecSDave Kleikamp } 4521ac27a0ecSDave Kleikamp 4522617ba13bSMingming Cao static int ext4_write_dquot(struct dquot *dquot) 4523ac27a0ecSDave Kleikamp { 4524ac27a0ecSDave Kleikamp int ret, err; 4525ac27a0ecSDave Kleikamp handle_t *handle; 4526ac27a0ecSDave Kleikamp struct inode *inode; 4527ac27a0ecSDave Kleikamp 4528ac27a0ecSDave Kleikamp inode = dquot_to_inode(dquot); 4529617ba13bSMingming Cao handle = ext4_journal_start(inode, 4530617ba13bSMingming Cao EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb)); 4531ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 4532ac27a0ecSDave Kleikamp return PTR_ERR(handle); 4533ac27a0ecSDave Kleikamp ret = dquot_commit(dquot); 4534617ba13bSMingming Cao err = ext4_journal_stop(handle); 4535ac27a0ecSDave Kleikamp if (!ret) 4536ac27a0ecSDave Kleikamp ret = err; 4537ac27a0ecSDave Kleikamp return ret; 4538ac27a0ecSDave Kleikamp } 4539ac27a0ecSDave Kleikamp 4540617ba13bSMingming Cao static int ext4_acquire_dquot(struct dquot *dquot) 4541ac27a0ecSDave Kleikamp { 4542ac27a0ecSDave Kleikamp int ret, err; 4543ac27a0ecSDave Kleikamp handle_t *handle; 4544ac27a0ecSDave Kleikamp 4545617ba13bSMingming Cao handle = ext4_journal_start(dquot_to_inode(dquot), 4546617ba13bSMingming Cao EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb)); 4547ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 4548ac27a0ecSDave Kleikamp return PTR_ERR(handle); 4549ac27a0ecSDave Kleikamp ret = dquot_acquire(dquot); 4550617ba13bSMingming Cao err = ext4_journal_stop(handle); 4551ac27a0ecSDave Kleikamp if (!ret) 4552ac27a0ecSDave Kleikamp ret = err; 4553ac27a0ecSDave Kleikamp return ret; 4554ac27a0ecSDave Kleikamp } 4555ac27a0ecSDave Kleikamp 4556617ba13bSMingming Cao static int ext4_release_dquot(struct dquot *dquot) 4557ac27a0ecSDave Kleikamp { 4558ac27a0ecSDave Kleikamp int ret, err; 4559ac27a0ecSDave Kleikamp handle_t *handle; 4560ac27a0ecSDave Kleikamp 4561617ba13bSMingming Cao handle = ext4_journal_start(dquot_to_inode(dquot), 4562617ba13bSMingming Cao EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb)); 45639c3013e9SJan Kara if (IS_ERR(handle)) { 45649c3013e9SJan Kara /* Release dquot anyway to avoid endless cycle in dqput() */ 45659c3013e9SJan Kara dquot_release(dquot); 4566ac27a0ecSDave Kleikamp return PTR_ERR(handle); 45679c3013e9SJan Kara } 4568ac27a0ecSDave Kleikamp ret = dquot_release(dquot); 4569617ba13bSMingming Cao err = ext4_journal_stop(handle); 4570ac27a0ecSDave Kleikamp if (!ret) 4571ac27a0ecSDave Kleikamp ret = err; 4572ac27a0ecSDave Kleikamp return ret; 4573ac27a0ecSDave Kleikamp } 4574ac27a0ecSDave Kleikamp 4575617ba13bSMingming Cao static int ext4_mark_dquot_dirty(struct dquot *dquot) 4576ac27a0ecSDave Kleikamp { 45772c8be6b2SJan Kara /* Are we journaling quotas? */ 4578617ba13bSMingming Cao if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] || 4579617ba13bSMingming Cao EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) { 4580ac27a0ecSDave Kleikamp dquot_mark_dquot_dirty(dquot); 4581617ba13bSMingming Cao return ext4_write_dquot(dquot); 4582ac27a0ecSDave Kleikamp } else { 4583ac27a0ecSDave Kleikamp return dquot_mark_dquot_dirty(dquot); 4584ac27a0ecSDave Kleikamp } 4585ac27a0ecSDave Kleikamp } 4586ac27a0ecSDave Kleikamp 4587617ba13bSMingming Cao static int ext4_write_info(struct super_block *sb, int type) 4588ac27a0ecSDave Kleikamp { 4589ac27a0ecSDave Kleikamp int ret, err; 4590ac27a0ecSDave Kleikamp handle_t *handle; 4591ac27a0ecSDave Kleikamp 4592ac27a0ecSDave Kleikamp /* Data block + inode block */ 4593617ba13bSMingming Cao handle = ext4_journal_start(sb->s_root->d_inode, 2); 4594ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 4595ac27a0ecSDave Kleikamp return PTR_ERR(handle); 4596ac27a0ecSDave Kleikamp ret = dquot_commit_info(sb, type); 4597617ba13bSMingming Cao err = ext4_journal_stop(handle); 4598ac27a0ecSDave Kleikamp if (!ret) 4599ac27a0ecSDave Kleikamp ret = err; 4600ac27a0ecSDave Kleikamp return ret; 4601ac27a0ecSDave Kleikamp } 4602ac27a0ecSDave Kleikamp 4603ac27a0ecSDave Kleikamp /* 4604ac27a0ecSDave Kleikamp * Turn on quotas during mount time - we need to find 4605ac27a0ecSDave Kleikamp * the quota file and such... 4606ac27a0ecSDave Kleikamp */ 4607617ba13bSMingming Cao static int ext4_quota_on_mount(struct super_block *sb, int type) 4608ac27a0ecSDave Kleikamp { 4609287a8095SChristoph Hellwig return dquot_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type], 4610617ba13bSMingming Cao EXT4_SB(sb)->s_jquota_fmt, type); 4611ac27a0ecSDave Kleikamp } 4612ac27a0ecSDave Kleikamp 4613ac27a0ecSDave Kleikamp /* 4614ac27a0ecSDave Kleikamp * Standard function to be called on quota_on 4615ac27a0ecSDave Kleikamp */ 4616617ba13bSMingming Cao static int ext4_quota_on(struct super_block *sb, int type, int format_id, 4617f00c9e44SJan Kara struct path *path) 4618ac27a0ecSDave Kleikamp { 4619ac27a0ecSDave Kleikamp int err; 4620ac27a0ecSDave Kleikamp 4621ac27a0ecSDave Kleikamp if (!test_opt(sb, QUOTA)) 4622ac27a0ecSDave Kleikamp return -EINVAL; 46230623543bSJan Kara 4624ac27a0ecSDave Kleikamp /* Quotafile not on the same filesystem? */ 4625d8c9584eSAl Viro if (path->dentry->d_sb != sb) 4626ac27a0ecSDave Kleikamp return -EXDEV; 46270623543bSJan Kara /* Journaling quota? */ 46280623543bSJan Kara if (EXT4_SB(sb)->s_qf_names[type]) { 46292b2d6d01STheodore Ts'o /* Quotafile not in fs root? */ 4630f00c9e44SJan Kara if (path->dentry->d_parent != sb->s_root) 4631b31e1552SEric Sandeen ext4_msg(sb, KERN_WARNING, 4632b31e1552SEric Sandeen "Quota file not on filesystem root. " 4633b31e1552SEric Sandeen "Journaled quota will not work"); 46340623543bSJan Kara } 46350623543bSJan Kara 46360623543bSJan Kara /* 46370623543bSJan Kara * When we journal data on quota file, we have to flush journal to see 46380623543bSJan Kara * all updates to the file when we bypass pagecache... 46390623543bSJan Kara */ 46400390131bSFrank Mayhar if (EXT4_SB(sb)->s_journal && 4641f00c9e44SJan Kara ext4_should_journal_data(path->dentry->d_inode)) { 46420623543bSJan Kara /* 46430623543bSJan Kara * We don't need to lock updates but journal_flush() could 46440623543bSJan Kara * otherwise be livelocked... 46450623543bSJan Kara */ 46460623543bSJan Kara jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); 46477ffe1ea8SHidehiro Kawai err = jbd2_journal_flush(EXT4_SB(sb)->s_journal); 46480623543bSJan Kara jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); 4649f00c9e44SJan Kara if (err) 46507ffe1ea8SHidehiro Kawai return err; 46517ffe1ea8SHidehiro Kawai } 46520623543bSJan Kara 4653f00c9e44SJan Kara return dquot_quota_on(sb, type, format_id, path); 4654ac27a0ecSDave Kleikamp } 4655ac27a0ecSDave Kleikamp 4656ca0e05e4SDmitry Monakhov static int ext4_quota_off(struct super_block *sb, int type) 4657ca0e05e4SDmitry Monakhov { 465821f97697SJan Kara struct inode *inode = sb_dqopt(sb)->files[type]; 465921f97697SJan Kara handle_t *handle; 466021f97697SJan Kara 466187009d86SDmitry Monakhov /* Force all delayed allocation blocks to be allocated. 466287009d86SDmitry Monakhov * Caller already holds s_umount sem */ 466387009d86SDmitry Monakhov if (test_opt(sb, DELALLOC)) 4664ca0e05e4SDmitry Monakhov sync_filesystem(sb); 4665ca0e05e4SDmitry Monakhov 46660b268590SAmir Goldstein if (!inode) 46670b268590SAmir Goldstein goto out; 46680b268590SAmir Goldstein 466921f97697SJan Kara /* Update modification times of quota files when userspace can 467021f97697SJan Kara * start looking at them */ 467121f97697SJan Kara handle = ext4_journal_start(inode, 1); 467221f97697SJan Kara if (IS_ERR(handle)) 467321f97697SJan Kara goto out; 467421f97697SJan Kara inode->i_mtime = inode->i_ctime = CURRENT_TIME; 467521f97697SJan Kara ext4_mark_inode_dirty(handle, inode); 467621f97697SJan Kara ext4_journal_stop(handle); 467721f97697SJan Kara 467821f97697SJan Kara out: 4679ca0e05e4SDmitry Monakhov return dquot_quota_off(sb, type); 4680ca0e05e4SDmitry Monakhov } 4681ca0e05e4SDmitry Monakhov 4682ac27a0ecSDave Kleikamp /* Read data from quotafile - avoid pagecache and such because we cannot afford 4683ac27a0ecSDave Kleikamp * acquiring the locks... As quota files are never truncated and quota code 4684ac27a0ecSDave Kleikamp * itself serializes the operations (and no one else should touch the files) 4685ac27a0ecSDave Kleikamp * we don't have to be afraid of races */ 4686617ba13bSMingming Cao static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data, 4687ac27a0ecSDave Kleikamp size_t len, loff_t off) 4688ac27a0ecSDave Kleikamp { 4689ac27a0ecSDave Kleikamp struct inode *inode = sb_dqopt(sb)->files[type]; 4690725d26d3SAneesh Kumar K.V ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb); 4691ac27a0ecSDave Kleikamp int err = 0; 4692ac27a0ecSDave Kleikamp int offset = off & (sb->s_blocksize - 1); 4693ac27a0ecSDave Kleikamp int tocopy; 4694ac27a0ecSDave Kleikamp size_t toread; 4695ac27a0ecSDave Kleikamp struct buffer_head *bh; 4696ac27a0ecSDave Kleikamp loff_t i_size = i_size_read(inode); 4697ac27a0ecSDave Kleikamp 4698ac27a0ecSDave Kleikamp if (off > i_size) 4699ac27a0ecSDave Kleikamp return 0; 4700ac27a0ecSDave Kleikamp if (off+len > i_size) 4701ac27a0ecSDave Kleikamp len = i_size-off; 4702ac27a0ecSDave Kleikamp toread = len; 4703ac27a0ecSDave Kleikamp while (toread > 0) { 4704ac27a0ecSDave Kleikamp tocopy = sb->s_blocksize - offset < toread ? 4705ac27a0ecSDave Kleikamp sb->s_blocksize - offset : toread; 4706617ba13bSMingming Cao bh = ext4_bread(NULL, inode, blk, 0, &err); 4707ac27a0ecSDave Kleikamp if (err) 4708ac27a0ecSDave Kleikamp return err; 4709ac27a0ecSDave Kleikamp if (!bh) /* A hole? */ 4710ac27a0ecSDave Kleikamp memset(data, 0, tocopy); 4711ac27a0ecSDave Kleikamp else 4712ac27a0ecSDave Kleikamp memcpy(data, bh->b_data+offset, tocopy); 4713ac27a0ecSDave Kleikamp brelse(bh); 4714ac27a0ecSDave Kleikamp offset = 0; 4715ac27a0ecSDave Kleikamp toread -= tocopy; 4716ac27a0ecSDave Kleikamp data += tocopy; 4717ac27a0ecSDave Kleikamp blk++; 4718ac27a0ecSDave Kleikamp } 4719ac27a0ecSDave Kleikamp return len; 4720ac27a0ecSDave Kleikamp } 4721ac27a0ecSDave Kleikamp 4722ac27a0ecSDave Kleikamp /* Write to quotafile (we know the transaction is already started and has 4723ac27a0ecSDave Kleikamp * enough credits) */ 4724617ba13bSMingming Cao static ssize_t ext4_quota_write(struct super_block *sb, int type, 4725ac27a0ecSDave Kleikamp const char *data, size_t len, loff_t off) 4726ac27a0ecSDave Kleikamp { 4727ac27a0ecSDave Kleikamp struct inode *inode = sb_dqopt(sb)->files[type]; 4728725d26d3SAneesh Kumar K.V ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb); 4729ac27a0ecSDave Kleikamp int err = 0; 4730ac27a0ecSDave Kleikamp int offset = off & (sb->s_blocksize - 1); 4731ac27a0ecSDave Kleikamp struct buffer_head *bh; 4732ac27a0ecSDave Kleikamp handle_t *handle = journal_current_handle(); 4733ac27a0ecSDave Kleikamp 47340390131bSFrank Mayhar if (EXT4_SB(sb)->s_journal && !handle) { 4735b31e1552SEric Sandeen ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)" 4736b31e1552SEric Sandeen " cancelled because transaction is not started", 47379c3013e9SJan Kara (unsigned long long)off, (unsigned long long)len); 47389c3013e9SJan Kara return -EIO; 47399c3013e9SJan Kara } 474067eeb568SDmitry Monakhov /* 474167eeb568SDmitry Monakhov * Since we account only one data block in transaction credits, 474267eeb568SDmitry Monakhov * then it is impossible to cross a block boundary. 474367eeb568SDmitry Monakhov */ 474467eeb568SDmitry Monakhov if (sb->s_blocksize - offset < len) { 474567eeb568SDmitry Monakhov ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)" 474667eeb568SDmitry Monakhov " cancelled because not block aligned", 474767eeb568SDmitry Monakhov (unsigned long long)off, (unsigned long long)len); 474867eeb568SDmitry Monakhov return -EIO; 474967eeb568SDmitry Monakhov } 475067eeb568SDmitry Monakhov 4751ac27a0ecSDave Kleikamp mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA); 4752617ba13bSMingming Cao bh = ext4_bread(handle, inode, blk, 1, &err); 4753ac27a0ecSDave Kleikamp if (!bh) 4754ac27a0ecSDave Kleikamp goto out; 4755617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, bh); 4756ac27a0ecSDave Kleikamp if (err) { 4757ac27a0ecSDave Kleikamp brelse(bh); 4758ac27a0ecSDave Kleikamp goto out; 4759ac27a0ecSDave Kleikamp } 4760ac27a0ecSDave Kleikamp lock_buffer(bh); 476167eeb568SDmitry Monakhov memcpy(bh->b_data+offset, data, len); 4762ac27a0ecSDave Kleikamp flush_dcache_page(bh->b_page); 4763ac27a0ecSDave Kleikamp unlock_buffer(bh); 47640390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, NULL, bh); 4765ac27a0ecSDave Kleikamp brelse(bh); 4766ac27a0ecSDave Kleikamp out: 476767eeb568SDmitry Monakhov if (err) { 47684d04e4fbSJan Kara mutex_unlock(&inode->i_mutex); 4769ac27a0ecSDave Kleikamp return err; 47704d04e4fbSJan Kara } 477167eeb568SDmitry Monakhov if (inode->i_size < off + len) { 477267eeb568SDmitry Monakhov i_size_write(inode, off + len); 4773617ba13bSMingming Cao EXT4_I(inode)->i_disksize = inode->i_size; 4774617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 477521f97697SJan Kara } 4776ac27a0ecSDave Kleikamp mutex_unlock(&inode->i_mutex); 477767eeb568SDmitry Monakhov return len; 4778ac27a0ecSDave Kleikamp } 4779ac27a0ecSDave Kleikamp 4780ac27a0ecSDave Kleikamp #endif 4781ac27a0ecSDave Kleikamp 4782152a0836SAl Viro static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags, 4783152a0836SAl Viro const char *dev_name, void *data) 4784ac27a0ecSDave Kleikamp { 4785152a0836SAl Viro return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super); 4786ac27a0ecSDave Kleikamp } 4787ac27a0ecSDave Kleikamp 478837f328ebSTheodore Ts'o #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23) 478924b58424STheodore Ts'o static inline void register_as_ext2(void) 479024b58424STheodore Ts'o { 479124b58424STheodore Ts'o int err = register_filesystem(&ext2_fs_type); 479224b58424STheodore Ts'o if (err) 479324b58424STheodore Ts'o printk(KERN_WARNING 479424b58424STheodore Ts'o "EXT4-fs: Unable to register as ext2 (%d)\n", err); 479524b58424STheodore Ts'o } 479624b58424STheodore Ts'o 479724b58424STheodore Ts'o static inline void unregister_as_ext2(void) 479824b58424STheodore Ts'o { 479924b58424STheodore Ts'o unregister_filesystem(&ext2_fs_type); 480024b58424STheodore Ts'o } 48012035e776STheodore Ts'o 48022035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb) 48032035e776STheodore Ts'o { 48042035e776STheodore Ts'o if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP)) 48052035e776STheodore Ts'o return 0; 48062035e776STheodore Ts'o if (sb->s_flags & MS_RDONLY) 48072035e776STheodore Ts'o return 1; 48082035e776STheodore Ts'o if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP)) 48092035e776STheodore Ts'o return 0; 48102035e776STheodore Ts'o return 1; 48112035e776STheodore Ts'o } 481251b7e3c9STheodore Ts'o MODULE_ALIAS("ext2"); 481324b58424STheodore Ts'o #else 481424b58424STheodore Ts'o static inline void register_as_ext2(void) { } 481524b58424STheodore Ts'o static inline void unregister_as_ext2(void) { } 48162035e776STheodore Ts'o static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; } 481724b58424STheodore Ts'o #endif 481824b58424STheodore Ts'o 481937f328ebSTheodore Ts'o #if !defined(CONFIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23) 482024b58424STheodore Ts'o static inline void register_as_ext3(void) 482124b58424STheodore Ts'o { 482224b58424STheodore Ts'o int err = register_filesystem(&ext3_fs_type); 482324b58424STheodore Ts'o if (err) 482424b58424STheodore Ts'o printk(KERN_WARNING 482524b58424STheodore Ts'o "EXT4-fs: Unable to register as ext3 (%d)\n", err); 482624b58424STheodore Ts'o } 482724b58424STheodore Ts'o 482824b58424STheodore Ts'o static inline void unregister_as_ext3(void) 482924b58424STheodore Ts'o { 483024b58424STheodore Ts'o unregister_filesystem(&ext3_fs_type); 483124b58424STheodore Ts'o } 48322035e776STheodore Ts'o 48332035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb) 48342035e776STheodore Ts'o { 48352035e776STheodore Ts'o if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT3_FEATURE_INCOMPAT_SUPP)) 48362035e776STheodore Ts'o return 0; 48372035e776STheodore Ts'o if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) 48382035e776STheodore Ts'o return 0; 48392035e776STheodore Ts'o if (sb->s_flags & MS_RDONLY) 48402035e776STheodore Ts'o return 1; 48412035e776STheodore Ts'o if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT3_FEATURE_RO_COMPAT_SUPP)) 48422035e776STheodore Ts'o return 0; 48432035e776STheodore Ts'o return 1; 48442035e776STheodore Ts'o } 484551b7e3c9STheodore Ts'o MODULE_ALIAS("ext3"); 484624b58424STheodore Ts'o #else 484724b58424STheodore Ts'o static inline void register_as_ext3(void) { } 484824b58424STheodore Ts'o static inline void unregister_as_ext3(void) { } 48492035e776STheodore Ts'o static inline int ext3_feature_set_ok(struct super_block *sb) { return 0; } 485024b58424STheodore Ts'o #endif 485124b58424STheodore Ts'o 485203010a33STheodore Ts'o static struct file_system_type ext4_fs_type = { 4853ac27a0ecSDave Kleikamp .owner = THIS_MODULE, 485403010a33STheodore Ts'o .name = "ext4", 4855152a0836SAl Viro .mount = ext4_mount, 4856ac27a0ecSDave Kleikamp .kill_sb = kill_block_super, 4857ac27a0ecSDave Kleikamp .fs_flags = FS_REQUIRES_DEV, 4858ac27a0ecSDave Kleikamp }; 4859ac27a0ecSDave Kleikamp 48608f021222SLukas Czerner static int __init ext4_init_feat_adverts(void) 4861857ac889SLukas Czerner { 4862857ac889SLukas Czerner struct ext4_features *ef; 4863857ac889SLukas Czerner int ret = -ENOMEM; 4864857ac889SLukas Czerner 4865857ac889SLukas Czerner ef = kzalloc(sizeof(struct ext4_features), GFP_KERNEL); 4866857ac889SLukas Czerner if (!ef) 4867857ac889SLukas Czerner goto out; 4868857ac889SLukas Czerner 4869857ac889SLukas Czerner ef->f_kobj.kset = ext4_kset; 4870857ac889SLukas Czerner init_completion(&ef->f_kobj_unregister); 4871857ac889SLukas Czerner ret = kobject_init_and_add(&ef->f_kobj, &ext4_feat_ktype, NULL, 4872857ac889SLukas Czerner "features"); 4873857ac889SLukas Czerner if (ret) { 4874857ac889SLukas Czerner kfree(ef); 4875857ac889SLukas Czerner goto out; 4876857ac889SLukas Czerner } 4877857ac889SLukas Czerner 4878857ac889SLukas Czerner ext4_feat = ef; 4879857ac889SLukas Czerner ret = 0; 4880857ac889SLukas Czerner out: 4881857ac889SLukas Czerner return ret; 4882857ac889SLukas Czerner } 4883857ac889SLukas Czerner 48848f021222SLukas Czerner static void ext4_exit_feat_adverts(void) 48858f021222SLukas Czerner { 48868f021222SLukas Czerner kobject_put(&ext4_feat->f_kobj); 48878f021222SLukas Czerner wait_for_completion(&ext4_feat->f_kobj_unregister); 48888f021222SLukas Czerner kfree(ext4_feat); 48898f021222SLukas Czerner } 48908f021222SLukas Czerner 4891e9e3bcecSEric Sandeen /* Shared across all ext4 file systems */ 4892e9e3bcecSEric Sandeen wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ]; 4893e9e3bcecSEric Sandeen struct mutex ext4__aio_mutex[EXT4_WQ_HASH_SZ]; 4894e9e3bcecSEric Sandeen 48955dabfc78STheodore Ts'o static int __init ext4_init_fs(void) 4896ac27a0ecSDave Kleikamp { 4897e9e3bcecSEric Sandeen int i, err; 4898c9de560dSAlex Tomas 489912e9b892SDmitry Monakhov ext4_check_flag_values(); 4900e9e3bcecSEric Sandeen 4901e9e3bcecSEric Sandeen for (i = 0; i < EXT4_WQ_HASH_SZ; i++) { 4902e9e3bcecSEric Sandeen mutex_init(&ext4__aio_mutex[i]); 4903e9e3bcecSEric Sandeen init_waitqueue_head(&ext4__ioend_wq[i]); 4904e9e3bcecSEric Sandeen } 4905e9e3bcecSEric Sandeen 49065dabfc78STheodore Ts'o err = ext4_init_pageio(); 49076fd058f7STheodore Ts'o if (err) 49086fd058f7STheodore Ts'o return err; 49095dabfc78STheodore Ts'o err = ext4_init_system_zone(); 4910bd2d0210STheodore Ts'o if (err) 4911d44651d0SFabrice Jouhaud goto out6; 49123197ebdbSTheodore Ts'o ext4_kset = kset_create_and_add("ext4", NULL, fs_kobj); 49133197ebdbSTheodore Ts'o if (!ext4_kset) 4914dd68314cSTheodore Ts'o goto out5; 4915d44651d0SFabrice Jouhaud ext4_proc_root = proc_mkdir("fs/ext4", NULL); 4916857ac889SLukas Czerner 4917857ac889SLukas Czerner err = ext4_init_feat_adverts(); 4918dd68314cSTheodore Ts'o if (err) 4919dd68314cSTheodore Ts'o goto out4; 4920857ac889SLukas Czerner 49215dabfc78STheodore Ts'o err = ext4_init_mballoc(); 4922ac27a0ecSDave Kleikamp if (err) 49236fd058f7STheodore Ts'o goto out3; 4924c9de560dSAlex Tomas 49255dabfc78STheodore Ts'o err = ext4_init_xattr(); 4926c9de560dSAlex Tomas if (err) 4927c9de560dSAlex Tomas goto out2; 4928ac27a0ecSDave Kleikamp err = init_inodecache(); 4929ac27a0ecSDave Kleikamp if (err) 4930ac27a0ecSDave Kleikamp goto out1; 493124b58424STheodore Ts'o register_as_ext3(); 49322035e776STheodore Ts'o register_as_ext2(); 493303010a33STheodore Ts'o err = register_filesystem(&ext4_fs_type); 4934ac27a0ecSDave Kleikamp if (err) 4935ac27a0ecSDave Kleikamp goto out; 4936bfff6873SLukas Czerner 4937bfff6873SLukas Czerner ext4_li_info = NULL; 4938bfff6873SLukas Czerner mutex_init(&ext4_li_mtx); 4939ac27a0ecSDave Kleikamp return 0; 4940ac27a0ecSDave Kleikamp out: 494124b58424STheodore Ts'o unregister_as_ext2(); 494224b58424STheodore Ts'o unregister_as_ext3(); 4943ac27a0ecSDave Kleikamp destroy_inodecache(); 4944ac27a0ecSDave Kleikamp out1: 49455dabfc78STheodore Ts'o ext4_exit_xattr(); 4946c9de560dSAlex Tomas out2: 49475dabfc78STheodore Ts'o ext4_exit_mballoc(); 49486fd058f7STheodore Ts'o out3: 49498f021222SLukas Czerner ext4_exit_feat_adverts(); 49506fd058f7STheodore Ts'o out4: 4951d44651d0SFabrice Jouhaud if (ext4_proc_root) 4952dd68314cSTheodore Ts'o remove_proc_entry("fs/ext4", NULL); 4953dd68314cSTheodore Ts'o kset_unregister(ext4_kset); 4954d44651d0SFabrice Jouhaud out5: 4955dd68314cSTheodore Ts'o ext4_exit_system_zone(); 4956d44651d0SFabrice Jouhaud out6: 49575dabfc78STheodore Ts'o ext4_exit_pageio(); 4958ac27a0ecSDave Kleikamp return err; 4959ac27a0ecSDave Kleikamp } 4960ac27a0ecSDave Kleikamp 49615dabfc78STheodore Ts'o static void __exit ext4_exit_fs(void) 4962ac27a0ecSDave Kleikamp { 4963bfff6873SLukas Czerner ext4_destroy_lazyinit_thread(); 496424b58424STheodore Ts'o unregister_as_ext2(); 496524b58424STheodore Ts'o unregister_as_ext3(); 496603010a33STheodore Ts'o unregister_filesystem(&ext4_fs_type); 4967ac27a0ecSDave Kleikamp destroy_inodecache(); 49685dabfc78STheodore Ts'o ext4_exit_xattr(); 49695dabfc78STheodore Ts'o ext4_exit_mballoc(); 49708f021222SLukas Czerner ext4_exit_feat_adverts(); 49719f6200bbSTheodore Ts'o remove_proc_entry("fs/ext4", NULL); 49723197ebdbSTheodore Ts'o kset_unregister(ext4_kset); 49735dabfc78STheodore Ts'o ext4_exit_system_zone(); 49745dabfc78STheodore Ts'o ext4_exit_pageio(); 4975ac27a0ecSDave Kleikamp } 4976ac27a0ecSDave Kleikamp 4977ac27a0ecSDave Kleikamp MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others"); 497883982b6fSTheodore Ts'o MODULE_DESCRIPTION("Fourth Extended Filesystem"); 4979ac27a0ecSDave Kleikamp MODULE_LICENSE("GPL"); 49805dabfc78STheodore Ts'o module_init(ext4_init_fs) 49815dabfc78STheodore Ts'o module_exit(ext4_exit_fs) 4982