xref: /openbmc/linux/fs/ext4/ext4.h (revision ababea77)
1f5166768STheodore Ts'o // SPDX-License-Identifier: GPL-2.0
23dcf5451SChristoph Hellwig /*
33dcf5451SChristoph Hellwig  *  ext4.h
43dcf5451SChristoph Hellwig  *
53dcf5451SChristoph Hellwig  * Copyright (C) 1992, 1993, 1994, 1995
63dcf5451SChristoph Hellwig  * Remy Card (card@masi.ibp.fr)
73dcf5451SChristoph Hellwig  * Laboratoire MASI - Institut Blaise Pascal
83dcf5451SChristoph Hellwig  * Universite Pierre et Marie Curie (Paris VI)
93dcf5451SChristoph Hellwig  *
103dcf5451SChristoph Hellwig  *  from
113dcf5451SChristoph Hellwig  *
123dcf5451SChristoph Hellwig  *  linux/include/linux/minix_fs.h
133dcf5451SChristoph Hellwig  *
143dcf5451SChristoph Hellwig  *  Copyright (C) 1991, 1992  Linus Torvalds
153dcf5451SChristoph Hellwig  */
163dcf5451SChristoph Hellwig 
173dcf5451SChristoph Hellwig #ifndef _EXT4_H
183dcf5451SChristoph Hellwig #define _EXT4_H
193dcf5451SChristoph Hellwig 
203dcf5451SChristoph Hellwig #include <linux/types.h>
213dcf5451SChristoph Hellwig #include <linux/blkdev.h>
223dcf5451SChristoph Hellwig #include <linux/magic.h>
232ccb5fb9SAneesh Kumar K.V #include <linux/jbd2.h>
2460e58e0fSMingming Cao #include <linux/quota.h>
25d444c3c3STheodore Ts'o #include <linux/rwsem.h>
26d444c3c3STheodore Ts'o #include <linux/rbtree.h>
27d444c3c3STheodore Ts'o #include <linux/seqlock.h>
28d444c3c3STheodore Ts'o #include <linux/mutex.h>
29ca0faba0STheodore Ts'o #include <linux/timer.h>
30ca0faba0STheodore Ts'o #include <linux/wait.h>
31174cd4b1SIngo Molnar #include <linux/sched/signal.h>
32ca0faba0STheodore Ts'o #include <linux/blockgroup_lock.h>
33ca0faba0STheodore Ts'o #include <linux/percpu_counter.h>
34efbed4dcSTheodore Ts'o #include <linux/ratelimit.h>
350441984aSDarrick J. Wong #include <crypto/hash.h>
36a633f5a3SLukas Czerner #include <linux/falloc.h>
37c8585c6fSDaeho Jeong #include <linux/percpu-rwsem.h>
3810c5db28SChristoph Hellwig #include <linux/fiemap.h>
394d92dc0fSBen Hutchings #ifdef __KERNEL__
404d92dc0fSBen Hutchings #include <linux/compat.h>
414d92dc0fSBen Hutchings #endif
423dcf5451SChristoph Hellwig 
43734f0d24SDave Chinner #include <linux/fscrypt.h>
44c93d8f88SEric Biggers #include <linux/fsverity.h>
45734f0d24SDave Chinner 
46072ebb3bSTheodore Ts'o #include <linux/compiler.h>
47072ebb3bSTheodore Ts'o 
483dcf5451SChristoph Hellwig /*
498a35694eSShen Feng  * The fourth extended filesystem constants/structures
503dcf5451SChristoph Hellwig  */
513dcf5451SChristoph Hellwig 
523dcf5451SChristoph Hellwig /*
53a2821e34SAihua Zhang  * with AGGRESSIVE_CHECK allocator runs consistency checks over
54a2821e34SAihua Zhang  * structures. these checks slow things down a lot
55a2821e34SAihua Zhang  */
56a2821e34SAihua Zhang #define AGGRESSIVE_CHECK__
57a2821e34SAihua Zhang 
58a2821e34SAihua Zhang /*
59a2821e34SAihua Zhang  * with DOUBLE_CHECK defined mballoc creates persistent in-core
60a2821e34SAihua Zhang  * bitmaps, maintains and uses them to check for double allocations
61a2821e34SAihua Zhang  */
62a2821e34SAihua Zhang #define DOUBLE_CHECK__
63a2821e34SAihua Zhang 
64a2821e34SAihua Zhang /*
653dcf5451SChristoph Hellwig  * Define EXT4FS_DEBUG to produce debug messages
663dcf5451SChristoph Hellwig  */
673dcf5451SChristoph Hellwig #undef EXT4FS_DEBUG
683dcf5451SChristoph Hellwig 
693dcf5451SChristoph Hellwig /*
703dcf5451SChristoph Hellwig  * Debug code
713dcf5451SChristoph Hellwig  */
723dcf5451SChristoph Hellwig #ifdef EXT4FS_DEBUG
733dcf5451SChristoph Hellwig #define ext4_debug(f, a...)						\
743dcf5451SChristoph Hellwig 	do {								\
753dcf5451SChristoph Hellwig 		printk(KERN_DEBUG "EXT4-fs DEBUG (%s, %d): %s:",	\
764db9c54aSStoyan Gaydarov 			__FILE__, __LINE__, __func__);			\
773dcf5451SChristoph Hellwig 		printk(KERN_DEBUG f, ## a);				\
783dcf5451SChristoph Hellwig 	} while (0)
793dcf5451SChristoph Hellwig #else
80ace36ad4SJoe Perches #define ext4_debug(fmt, ...)	no_printk(fmt, ##__VA_ARGS__)
813dcf5451SChristoph Hellwig #endif
823dcf5451SChristoph Hellwig 
834a092d73STheodore Ts'o  /*
8470aa1554SRitesh Harjani   * Turn on EXT_DEBUG to enable ext4_ext_show_path/leaf/move in extents.c
854a092d73STheodore Ts'o   */
864a092d73STheodore Ts'o #define EXT_DEBUG__
8770aa1554SRitesh Harjani 
8870aa1554SRitesh Harjani /*
8970aa1554SRitesh Harjani  * Dynamic printk for controlled extents debugging.
9070aa1554SRitesh Harjani  */
9170aa1554SRitesh Harjani #ifdef CONFIG_EXT4_DEBUG
9270aa1554SRitesh Harjani #define ext_debug(ino, fmt, ...)					\
9370aa1554SRitesh Harjani 	pr_debug("[%s/%d] EXT4-fs (%s): ino %lu: (%s, %d): %s:" fmt,	\
9470aa1554SRitesh Harjani 		 current->comm, task_pid_nr(current),			\
9570aa1554SRitesh Harjani 		 ino->i_sb->s_id, ino->i_ino, __FILE__, __LINE__,	\
9670aa1554SRitesh Harjani 		 __func__, ##__VA_ARGS__)
974a092d73STheodore Ts'o #else
9870aa1554SRitesh Harjani #define ext_debug(ino, fmt, ...)	no_printk(fmt, ##__VA_ARGS__)
994a092d73STheodore Ts'o #endif
1004a092d73STheodore Ts'o 
101d444c3c3STheodore Ts'o /* data type for block offset of block group */
102d444c3c3STheodore Ts'o typedef int ext4_grpblk_t;
103d444c3c3STheodore Ts'o 
104d444c3c3STheodore Ts'o /* data type for filesystem-wide blocks number */
105d444c3c3STheodore Ts'o typedef unsigned long long ext4_fsblk_t;
106d444c3c3STheodore Ts'o 
107d444c3c3STheodore Ts'o /* data type for file logical block number */
108d444c3c3STheodore Ts'o typedef __u32 ext4_lblk_t;
109d444c3c3STheodore Ts'o 
110d444c3c3STheodore Ts'o /* data type for block group number */
111d444c3c3STheodore Ts'o typedef unsigned int ext4_group_t;
112d444c3c3STheodore Ts'o 
113331573feSNamjae Jeon enum SHIFT_DIRECTION {
114331573feSNamjae Jeon 	SHIFT_LEFT = 0,
115331573feSNamjae Jeon 	SHIFT_RIGHT,
116331573feSNamjae Jeon };
117331573feSNamjae Jeon 
118296c355cSTheodore Ts'o /*
119296c355cSTheodore Ts'o  * Flags used in mballoc's allocation_context flags field.
120296c355cSTheodore Ts'o  *
121296c355cSTheodore Ts'o  * Also used to show what's going on for debugging purposes when the
122296c355cSTheodore Ts'o  * flag field is exported via the traceport interface
123296c355cSTheodore Ts'o  */
124d444c3c3STheodore Ts'o 
1253dcf5451SChristoph Hellwig /* prefer goal again. length */
1260ef90db9STheodore Ts'o #define EXT4_MB_HINT_MERGE		0x0001
1273dcf5451SChristoph Hellwig /* blocks already reserved */
1280ef90db9STheodore Ts'o #define EXT4_MB_HINT_RESERVED		0x0002
1293dcf5451SChristoph Hellwig /* metadata is being allocated */
1300ef90db9STheodore Ts'o #define EXT4_MB_HINT_METADATA		0x0004
1313dcf5451SChristoph Hellwig /* first blocks in the file */
1320ef90db9STheodore Ts'o #define EXT4_MB_HINT_FIRST		0x0008
1333dcf5451SChristoph Hellwig /* search for the best chunk */
1340ef90db9STheodore Ts'o #define EXT4_MB_HINT_BEST		0x0010
1353dcf5451SChristoph Hellwig /* data is being allocated */
1360ef90db9STheodore Ts'o #define EXT4_MB_HINT_DATA		0x0020
1373dcf5451SChristoph Hellwig /* don't preallocate (for tails) */
1380ef90db9STheodore Ts'o #define EXT4_MB_HINT_NOPREALLOC		0x0040
1393dcf5451SChristoph Hellwig /* allocate for locality group */
1400ef90db9STheodore Ts'o #define EXT4_MB_HINT_GROUP_ALLOC	0x0080
1413dcf5451SChristoph Hellwig /* allocate goal blocks or none */
1420ef90db9STheodore Ts'o #define EXT4_MB_HINT_GOAL_ONLY		0x0100
1433dcf5451SChristoph Hellwig /* goal is meaningful */
1440ef90db9STheodore Ts'o #define EXT4_MB_HINT_TRY_GOAL		0x0200
145d2a17637SMingming Cao /* blocks already pre-reserved by delayed allocation */
1460ef90db9STheodore Ts'o #define EXT4_MB_DELALLOC_RESERVED	0x0400
1474ba74d00STheodore Ts'o /* We are doing stream allocation */
1484ba74d00STheodore Ts'o #define EXT4_MB_STREAM_ALLOC		0x0800
14955f020dbSAllison Henderson /* Use reserved root blocks if needed */
15055f020dbSAllison Henderson #define EXT4_MB_USE_ROOT_BLOCKS		0x1000
15127dd4385SLukas Czerner /* Use blocks from reserved pool */
15227dd4385SLukas Czerner #define EXT4_MB_USE_RESERVED		0x2000
15399377830SRitesh Harjani /* Do strict check for free blocks while retrying block allocation */
15499377830SRitesh Harjani #define EXT4_MB_STRICT_CHECK		0x4000
1553dcf5451SChristoph Hellwig 
1563dcf5451SChristoph Hellwig struct ext4_allocation_request {
1573dcf5451SChristoph Hellwig 	/* target inode for block we're allocating */
1583dcf5451SChristoph Hellwig 	struct inode *inode;
1593dcf5451SChristoph Hellwig 	/* how many blocks we want to allocate */
160498e5f24STheodore Ts'o 	unsigned int len;
161726447d8SEric Sandeen 	/* logical block in target inode */
162726447d8SEric Sandeen 	ext4_lblk_t logical;
163726447d8SEric Sandeen 	/* the closest logical allocated block to the left */
164726447d8SEric Sandeen 	ext4_lblk_t lleft;
165726447d8SEric Sandeen 	/* the closest logical allocated block to the right */
166726447d8SEric Sandeen 	ext4_lblk_t lright;
167726447d8SEric Sandeen 	/* phys. target (a hint) */
168726447d8SEric Sandeen 	ext4_fsblk_t goal;
169726447d8SEric Sandeen 	/* phys. block for the closest logical allocated block to the left */
170726447d8SEric Sandeen 	ext4_fsblk_t pleft;
171726447d8SEric Sandeen 	/* phys. block for the closest logical allocated block to the right */
172726447d8SEric Sandeen 	ext4_fsblk_t pright;
1733dcf5451SChristoph Hellwig 	/* flags. see above EXT4_MB_HINT_* */
174498e5f24STheodore Ts'o 	unsigned int flags;
1753dcf5451SChristoph Hellwig };
1763dcf5451SChristoph Hellwig 
1773dcf5451SChristoph Hellwig /*
178e35fd660STheodore Ts'o  * Logical to physical block mapping, used by ext4_map_blocks()
179e35fd660STheodore Ts'o  *
180e35fd660STheodore Ts'o  * This structure is used to pass requests into ext4_map_blocks() as
181e35fd660STheodore Ts'o  * well as to store the information returned by ext4_map_blocks().  It
182e35fd660STheodore Ts'o  * takes less room on the stack than a struct buffer_head.
183e35fd660STheodore Ts'o  */
1846db07461SRitesh Harjani #define EXT4_MAP_NEW		BIT(BH_New)
1856db07461SRitesh Harjani #define EXT4_MAP_MAPPED		BIT(BH_Mapped)
1866db07461SRitesh Harjani #define EXT4_MAP_UNWRITTEN	BIT(BH_Unwritten)
1876db07461SRitesh Harjani #define EXT4_MAP_BOUNDARY	BIT(BH_Boundary)
188e35fd660STheodore Ts'o #define EXT4_MAP_FLAGS		(EXT4_MAP_NEW | EXT4_MAP_MAPPED |\
189cbd7584eSJan Kara 				 EXT4_MAP_UNWRITTEN | EXT4_MAP_BOUNDARY)
190e35fd660STheodore Ts'o 
191e35fd660STheodore Ts'o struct ext4_map_blocks {
192e35fd660STheodore Ts'o 	ext4_fsblk_t m_pblk;
193e35fd660STheodore Ts'o 	ext4_lblk_t m_lblk;
194e35fd660STheodore Ts'o 	unsigned int m_len;
195e35fd660STheodore Ts'o 	unsigned int m_flags;
196e35fd660STheodore Ts'o };
197e35fd660STheodore Ts'o 
198e35fd660STheodore Ts'o /*
1997727ae52Szhangyi (F)  * Block validity checking, system zone rbtree.
2007727ae52Szhangyi (F)  */
2017727ae52Szhangyi (F) struct ext4_system_blocks {
2027727ae52Szhangyi (F) 	struct rb_root root;
2037727ae52Szhangyi (F) 	struct rcu_head rcu;
2047727ae52Szhangyi (F) };
2057727ae52Szhangyi (F) 
2067727ae52Szhangyi (F) /*
207bd2d0210STheodore Ts'o  * Flags for ext4_io_end->flags
208bd2d0210STheodore Ts'o  */
209bd2d0210STheodore Ts'o #define	EXT4_IO_END_UNWRITTEN	0x0001
210bd2d0210STheodore Ts'o 
211c8cc8816SRitesh Harjani struct ext4_io_end_vec {
212c8cc8816SRitesh Harjani 	struct list_head list;		/* list of io_end_vec */
213c8cc8816SRitesh Harjani 	loff_t offset;			/* offset in the file */
214c8cc8816SRitesh Harjani 	ssize_t size;			/* size of the extent */
215c8cc8816SRitesh Harjani };
216c8cc8816SRitesh Harjani 
2174188188bSCurt Wohlgemuth /*
218556615dcSLukas Czerner  * For converting unwritten extents on a work queue. 'handle' is used for
2196b523df4SJan Kara  * buffered writeback.
2204188188bSCurt Wohlgemuth  */
2210031462bSMingming Cao typedef struct ext4_io_end {
2225b3ff237Sjiayingz@google.com (Jiaying Zhang) 	struct list_head	list;		/* per-file finished IO list */
2236b523df4SJan Kara 	handle_t		*handle;	/* handle reserved for extent
2246b523df4SJan Kara 						 * conversion */
2250031462bSMingming Cao 	struct inode		*inode;		/* file being written to */
226b0857d30SJan Kara 	struct bio		*bio;		/* Linked list of completed
227b0857d30SJan Kara 						 * bios covering the extent */
2288d5d02e6SMingming Cao 	unsigned int		flag;		/* unwritten or not */
2293bd6ad7bSJan Kara 	atomic_t		count;		/* reference counter */
230c8cc8816SRitesh Harjani 	struct list_head	list_vec;	/* list of ext4_io_end_vec */
2310031462bSMingming Cao } ext4_io_end_t;
2320031462bSMingming Cao 
233bd2d0210STheodore Ts'o struct ext4_io_submit {
2345a33911fSTejun Heo 	struct writeback_control *io_wbc;
235bd2d0210STheodore Ts'o 	struct bio		*io_bio;
236bd2d0210STheodore Ts'o 	ext4_io_end_t		*io_end;
237bd2d0210STheodore Ts'o 	sector_t		io_next_block;
238bd2d0210STheodore Ts'o };
239bd2d0210STheodore Ts'o 
240b3a3ca8cSTheodore Ts'o /*
2413dcf5451SChristoph Hellwig  * Special inodes numbers
2423dcf5451SChristoph Hellwig  */
2433dcf5451SChristoph Hellwig #define	EXT4_BAD_INO		 1	/* Bad blocks inode */
2443dcf5451SChristoph Hellwig #define EXT4_ROOT_INO		 2	/* Root inode */
245ae812306SAditya Kali #define EXT4_USR_QUOTA_INO	 3	/* User quota inode */
246ae812306SAditya Kali #define EXT4_GRP_QUOTA_INO	 4	/* Group quota inode */
2473dcf5451SChristoph Hellwig #define EXT4_BOOT_LOADER_INO	 5	/* Boot loader inode */
2483dcf5451SChristoph Hellwig #define EXT4_UNDEL_DIR_INO	 6	/* Undelete directory inode */
2493dcf5451SChristoph Hellwig #define EXT4_RESIZE_INO		 7	/* Reserved group descriptors inode */
2503dcf5451SChristoph Hellwig #define EXT4_JOURNAL_INO	 8	/* Journal inode */
2513dcf5451SChristoph Hellwig 
2523dcf5451SChristoph Hellwig /* First non-reserved inode for old ext4 filesystems */
2533dcf5451SChristoph Hellwig #define EXT4_GOOD_OLD_FIRST_INO	11
2543dcf5451SChristoph Hellwig 
2553dcf5451SChristoph Hellwig /*
2563dcf5451SChristoph Hellwig  * Maximal count of links to a file
2573dcf5451SChristoph Hellwig  */
2583dcf5451SChristoph Hellwig #define EXT4_LINK_MAX		65000
2593dcf5451SChristoph Hellwig 
2603dcf5451SChristoph Hellwig /*
2613dcf5451SChristoph Hellwig  * Macro-instructions used to manage several block sizes
2623dcf5451SChristoph Hellwig  */
2633dcf5451SChristoph Hellwig #define EXT4_MIN_BLOCK_SIZE		1024
2643dcf5451SChristoph Hellwig #define	EXT4_MAX_BLOCK_SIZE		65536
2653dcf5451SChristoph Hellwig #define EXT4_MIN_BLOCK_LOG_SIZE		10
266fb1813f4SCurt Wohlgemuth #define EXT4_MAX_BLOCK_LOG_SIZE		16
2679e47a4c9STheodore Ts'o #define EXT4_MAX_CLUSTER_LOG_SIZE	30
2683dcf5451SChristoph Hellwig #ifdef __KERNEL__
2693dcf5451SChristoph Hellwig # define EXT4_BLOCK_SIZE(s)		((s)->s_blocksize)
2703dcf5451SChristoph Hellwig #else
2713dcf5451SChristoph Hellwig # define EXT4_BLOCK_SIZE(s)		(EXT4_MIN_BLOCK_SIZE << (s)->s_log_block_size)
2723dcf5451SChristoph Hellwig #endif
2733dcf5451SChristoph Hellwig #define	EXT4_ADDR_PER_BLOCK(s)		(EXT4_BLOCK_SIZE(s) / sizeof(__u32))
274281b5995STheodore Ts'o #define EXT4_CLUSTER_SIZE(s)		(EXT4_BLOCK_SIZE(s) << \
275281b5995STheodore Ts'o 					 EXT4_SB(s)->s_cluster_bits)
2763dcf5451SChristoph Hellwig #ifdef __KERNEL__
2773dcf5451SChristoph Hellwig # define EXT4_BLOCK_SIZE_BITS(s)	((s)->s_blocksize_bits)
278281b5995STheodore Ts'o # define EXT4_CLUSTER_BITS(s)		(EXT4_SB(s)->s_cluster_bits)
2793dcf5451SChristoph Hellwig #else
2803dcf5451SChristoph Hellwig # define EXT4_BLOCK_SIZE_BITS(s)	((s)->s_log_block_size + 10)
2813dcf5451SChristoph Hellwig #endif
2823dcf5451SChristoph Hellwig #ifdef __KERNEL__
2833dcf5451SChristoph Hellwig #define	EXT4_ADDR_PER_BLOCK_BITS(s)	(EXT4_SB(s)->s_addr_per_block_bits)
2843dcf5451SChristoph Hellwig #define EXT4_INODE_SIZE(s)		(EXT4_SB(s)->s_inode_size)
2853dcf5451SChristoph Hellwig #define EXT4_FIRST_INO(s)		(EXT4_SB(s)->s_first_ino)
2863dcf5451SChristoph Hellwig #else
2873dcf5451SChristoph Hellwig #define EXT4_INODE_SIZE(s)	(((s)->s_rev_level == EXT4_GOOD_OLD_REV) ? \
2883dcf5451SChristoph Hellwig 				 EXT4_GOOD_OLD_INODE_SIZE : \
2893dcf5451SChristoph Hellwig 				 (s)->s_inode_size)
2903dcf5451SChristoph Hellwig #define EXT4_FIRST_INO(s)	(((s)->s_rev_level == EXT4_GOOD_OLD_REV) ? \
2913dcf5451SChristoph Hellwig 				 EXT4_GOOD_OLD_FIRST_INO : \
2923dcf5451SChristoph Hellwig 				 (s)->s_first_ino)
2933dcf5451SChristoph Hellwig #endif
2943dcf5451SChristoph Hellwig #define EXT4_BLOCK_ALIGN(size, blkbits)		ALIGN((size), (1 << (blkbits)))
295518eaa63SFabian Frederick #define EXT4_MAX_BLOCKS(size, offset, blkbits) \
296518eaa63SFabian Frederick 	((EXT4_BLOCK_ALIGN(size + offset, blkbits) >> blkbits) - (offset >> \
297518eaa63SFabian Frederick 								  blkbits))
2983dcf5451SChristoph Hellwig 
299d5b8f310STheodore Ts'o /* Translate a block number to a cluster number */
300d5b8f310STheodore Ts'o #define EXT4_B2C(sbi, blk)	((blk) >> (sbi)->s_cluster_bits)
301d5b8f310STheodore Ts'o /* Translate a cluster number to a block number */
302d5b8f310STheodore Ts'o #define EXT4_C2B(sbi, cluster)	((cluster) << (sbi)->s_cluster_bits)
303d5b8f310STheodore Ts'o /* Translate # of blks to # of clusters */
304d5b8f310STheodore Ts'o #define EXT4_NUM_B2C(sbi, blks)	(((blks) + (sbi)->s_cluster_ratio - 1) >> \
305d5b8f310STheodore Ts'o 				 (sbi)->s_cluster_bits)
306f5a44db5STheodore Ts'o /* Mask out the low bits to get the starting block of the cluster */
307f5a44db5STheodore Ts'o #define EXT4_PBLK_CMASK(s, pblk) ((pblk) &				\
308f5a44db5STheodore Ts'o 				  ~((ext4_fsblk_t) (s)->s_cluster_ratio - 1))
309f5a44db5STheodore Ts'o #define EXT4_LBLK_CMASK(s, lblk) ((lblk) &				\
310f5a44db5STheodore Ts'o 				  ~((ext4_lblk_t) (s)->s_cluster_ratio - 1))
3118fcc3a58SEric Whitney /* Fill in the low bits to get the last block of the cluster */
3128fcc3a58SEric Whitney #define EXT4_LBLK_CFILL(sbi, lblk) ((lblk) |				\
3138fcc3a58SEric Whitney 				    ((ext4_lblk_t) (sbi)->s_cluster_ratio - 1))
314f5a44db5STheodore Ts'o /* Get the cluster offset */
315f5a44db5STheodore Ts'o #define EXT4_PBLK_COFF(s, pblk) ((pblk) &				\
316f5a44db5STheodore Ts'o 				 ((ext4_fsblk_t) (s)->s_cluster_ratio - 1))
317f5a44db5STheodore Ts'o #define EXT4_LBLK_COFF(s, lblk) ((lblk) &				\
318f5a44db5STheodore Ts'o 				 ((ext4_lblk_t) (s)->s_cluster_ratio - 1))
319d5b8f310STheodore Ts'o 
3203dcf5451SChristoph Hellwig /*
3213dcf5451SChristoph Hellwig  * Structure of a blocks group descriptor
3223dcf5451SChristoph Hellwig  */
3233dcf5451SChristoph Hellwig struct ext4_group_desc
3243dcf5451SChristoph Hellwig {
3253dcf5451SChristoph Hellwig 	__le32	bg_block_bitmap_lo;	/* Blocks bitmap block */
3263dcf5451SChristoph Hellwig 	__le32	bg_inode_bitmap_lo;	/* Inodes bitmap block */
3273dcf5451SChristoph Hellwig 	__le32	bg_inode_table_lo;	/* Inodes table block */
328560671a0SAneesh Kumar K.V 	__le16	bg_free_blocks_count_lo;/* Free blocks count */
329560671a0SAneesh Kumar K.V 	__le16	bg_free_inodes_count_lo;/* Free inodes count */
330560671a0SAneesh Kumar K.V 	__le16	bg_used_dirs_count_lo;	/* Directories count */
3313dcf5451SChristoph Hellwig 	__le16	bg_flags;		/* EXT4_BG_flags (INODE_UNINIT, etc) */
332e6153918SDarrick J. Wong 	__le32  bg_exclude_bitmap_lo;   /* Exclude bitmap for snapshots */
333e6153918SDarrick J. Wong 	__le16  bg_block_bitmap_csum_lo;/* crc32c(s_uuid+grp_num+bbitmap) LE */
334e6153918SDarrick J. Wong 	__le16  bg_inode_bitmap_csum_lo;/* crc32c(s_uuid+grp_num+ibitmap) LE */
335560671a0SAneesh Kumar K.V 	__le16  bg_itable_unused_lo;	/* Unused inodes count */
3363dcf5451SChristoph Hellwig 	__le16  bg_checksum;		/* crc16(sb_uuid+group+desc) */
3373dcf5451SChristoph Hellwig 	__le32	bg_block_bitmap_hi;	/* Blocks bitmap block MSB */
3383dcf5451SChristoph Hellwig 	__le32	bg_inode_bitmap_hi;	/* Inodes bitmap block MSB */
3393dcf5451SChristoph Hellwig 	__le32	bg_inode_table_hi;	/* Inodes table block MSB */
3403dcf5451SChristoph Hellwig 	__le16	bg_free_blocks_count_hi;/* Free blocks count MSB */
3413dcf5451SChristoph Hellwig 	__le16	bg_free_inodes_count_hi;/* Free inodes count MSB */
3423dcf5451SChristoph Hellwig 	__le16	bg_used_dirs_count_hi;	/* Directories count MSB */
3433dcf5451SChristoph Hellwig 	__le16  bg_itable_unused_hi;    /* Unused inodes count MSB */
344e6153918SDarrick J. Wong 	__le32  bg_exclude_bitmap_hi;   /* Exclude bitmap block MSB */
345e6153918SDarrick J. Wong 	__le16  bg_block_bitmap_csum_hi;/* crc32c(s_uuid+grp_num+bbitmap) BE */
346e6153918SDarrick J. Wong 	__le16  bg_inode_bitmap_csum_hi;/* crc32c(s_uuid+grp_num+ibitmap) BE */
347e6153918SDarrick J. Wong 	__u32   bg_reserved;
3483dcf5451SChristoph Hellwig };
3493dcf5451SChristoph Hellwig 
35041a246d1SDarrick J. Wong #define EXT4_BG_INODE_BITMAP_CSUM_HI_END	\
35141a246d1SDarrick J. Wong 	(offsetof(struct ext4_group_desc, bg_inode_bitmap_csum_hi) + \
35241a246d1SDarrick J. Wong 	 sizeof(__le16))
35341a246d1SDarrick J. Wong #define EXT4_BG_BLOCK_BITMAP_CSUM_HI_END	\
35441a246d1SDarrick J. Wong 	(offsetof(struct ext4_group_desc, bg_block_bitmap_csum_hi) + \
35541a246d1SDarrick J. Wong 	 sizeof(__le16))
35641a246d1SDarrick J. Wong 
357772cb7c8SJose R. Santos /*
358772cb7c8SJose R. Santos  * Structure of a flex block group info
359772cb7c8SJose R. Santos  */
360772cb7c8SJose R. Santos 
361772cb7c8SJose R. Santos struct flex_groups {
36290ba983fSTheodore Ts'o 	atomic64_t	free_clusters;
3639f24e420STheodore Ts'o 	atomic_t	free_inodes;
3647d39db14STheodore Ts'o 	atomic_t	used_dirs;
365772cb7c8SJose R. Santos };
366772cb7c8SJose R. Santos 
3673dcf5451SChristoph Hellwig #define EXT4_BG_INODE_UNINIT	0x0001 /* Inode table/bitmap not in use */
3683dcf5451SChristoph Hellwig #define EXT4_BG_BLOCK_UNINIT	0x0002 /* Block bitmap not in use */
3693dcf5451SChristoph Hellwig #define EXT4_BG_INODE_ZEROED	0x0004 /* On-disk itable initialized to zero */
3703dcf5451SChristoph Hellwig 
3713dcf5451SChristoph Hellwig /*
3723dcf5451SChristoph Hellwig  * Macro-instructions used to manage group descriptors
3733dcf5451SChristoph Hellwig  */
3743dcf5451SChristoph Hellwig #define EXT4_MIN_DESC_SIZE		32
3753dcf5451SChristoph Hellwig #define EXT4_MIN_DESC_SIZE_64BIT	64
3763dcf5451SChristoph Hellwig #define	EXT4_MAX_DESC_SIZE		EXT4_MIN_BLOCK_SIZE
3773dcf5451SChristoph Hellwig #define EXT4_DESC_SIZE(s)		(EXT4_SB(s)->s_desc_size)
3783dcf5451SChristoph Hellwig #ifdef __KERNEL__
3793dcf5451SChristoph Hellwig # define EXT4_BLOCKS_PER_GROUP(s)	(EXT4_SB(s)->s_blocks_per_group)
380281b5995STheodore Ts'o # define EXT4_CLUSTERS_PER_GROUP(s)	(EXT4_SB(s)->s_clusters_per_group)
3813dcf5451SChristoph Hellwig # define EXT4_DESC_PER_BLOCK(s)		(EXT4_SB(s)->s_desc_per_block)
3823dcf5451SChristoph Hellwig # define EXT4_INODES_PER_GROUP(s)	(EXT4_SB(s)->s_inodes_per_group)
3833dcf5451SChristoph Hellwig # define EXT4_DESC_PER_BLOCK_BITS(s)	(EXT4_SB(s)->s_desc_per_block_bits)
3843dcf5451SChristoph Hellwig #else
3853dcf5451SChristoph Hellwig # define EXT4_BLOCKS_PER_GROUP(s)	((s)->s_blocks_per_group)
3863dcf5451SChristoph Hellwig # define EXT4_DESC_PER_BLOCK(s)		(EXT4_BLOCK_SIZE(s) / EXT4_DESC_SIZE(s))
3873dcf5451SChristoph Hellwig # define EXT4_INODES_PER_GROUP(s)	((s)->s_inodes_per_group)
3883dcf5451SChristoph Hellwig #endif
3893dcf5451SChristoph Hellwig 
3903dcf5451SChristoph Hellwig /*
3913dcf5451SChristoph Hellwig  * Constants relative to the data blocks
3923dcf5451SChristoph Hellwig  */
3933dcf5451SChristoph Hellwig #define	EXT4_NDIR_BLOCKS		12
3943dcf5451SChristoph Hellwig #define	EXT4_IND_BLOCK			EXT4_NDIR_BLOCKS
3953dcf5451SChristoph Hellwig #define	EXT4_DIND_BLOCK			(EXT4_IND_BLOCK + 1)
3963dcf5451SChristoph Hellwig #define	EXT4_TIND_BLOCK			(EXT4_DIND_BLOCK + 1)
3973dcf5451SChristoph Hellwig #define	EXT4_N_BLOCKS			(EXT4_TIND_BLOCK + 1)
3983dcf5451SChristoph Hellwig 
3993dcf5451SChristoph Hellwig /*
4003dcf5451SChristoph Hellwig  * Inode flags
4013dcf5451SChristoph Hellwig  */
4023dcf5451SChristoph Hellwig #define	EXT4_SECRM_FL			0x00000001 /* Secure deletion */
4033dcf5451SChristoph Hellwig #define	EXT4_UNRM_FL			0x00000002 /* Undelete */
4043dcf5451SChristoph Hellwig #define	EXT4_COMPR_FL			0x00000004 /* Compress file */
4053dcf5451SChristoph Hellwig #define EXT4_SYNC_FL			0x00000008 /* Synchronous updates */
4063dcf5451SChristoph Hellwig #define EXT4_IMMUTABLE_FL		0x00000010 /* Immutable file */
4073dcf5451SChristoph Hellwig #define EXT4_APPEND_FL			0x00000020 /* writes to file may only append */
4083dcf5451SChristoph Hellwig #define EXT4_NODUMP_FL			0x00000040 /* do not dump file */
4093dcf5451SChristoph Hellwig #define EXT4_NOATIME_FL			0x00000080 /* do not update atime */
4103dcf5451SChristoph Hellwig /* Reserved for compression usage... */
4113dcf5451SChristoph Hellwig #define EXT4_DIRTY_FL			0x00000100
4123dcf5451SChristoph Hellwig #define EXT4_COMPRBLK_FL		0x00000200 /* One or more compressed clusters */
4133dcf5451SChristoph Hellwig #define EXT4_NOCOMPR_FL			0x00000400 /* Don't compress */
4143edc18d8STheodore Ts'o 	/* nb: was previously EXT2_ECOMPR_FL */
4153edc18d8STheodore Ts'o #define EXT4_ENCRYPT_FL			0x00000800 /* encrypted file */
4163dcf5451SChristoph Hellwig /* End compression flags --- maybe not all used */
4173dcf5451SChristoph Hellwig #define EXT4_INDEX_FL			0x00001000 /* hash-indexed directory */
4183dcf5451SChristoph Hellwig #define EXT4_IMAGIC_FL			0x00002000 /* AFS directory */
4193dcf5451SChristoph Hellwig #define EXT4_JOURNAL_DATA_FL		0x00004000 /* file data should be journaled */
4203dcf5451SChristoph Hellwig #define EXT4_NOTAIL_FL			0x00008000 /* file tail should not be merged */
4213dcf5451SChristoph Hellwig #define EXT4_DIRSYNC_FL			0x00010000 /* dirsync behaviour (directories only) */
4223dcf5451SChristoph Hellwig #define EXT4_TOPDIR_FL			0x00020000 /* Top of directory hierarchies*/
4233dcf5451SChristoph Hellwig #define EXT4_HUGE_FILE_FL               0x00040000 /* Set to each huge file */
4243dcf5451SChristoph Hellwig #define EXT4_EXTENTS_FL			0x00080000 /* Inode uses extents */
425c93d8f88SEric Biggers #define EXT4_VERITY_FL			0x00100000 /* Verity protected inode */
426f710b4b9STheodore Ts'o #define EXT4_EA_INODE_FL	        0x00200000 /* Inode used for large EA */
4274337ecd1SEric Whitney /* 0x00400000 was formerly EXT4_EOFBLOCKS_FL */
428b383a73fSIra Weiny 
429b383a73fSIra Weiny #define EXT4_DAX_FL			0x02000000 /* Inode is DAX */
430b383a73fSIra Weiny 
43167cf5b09STao Ma #define EXT4_INLINE_DATA_FL		0x10000000 /* Inode has inline data. */
4328b4953e1STheodore Ts'o #define EXT4_PROJINHERIT_FL		0x20000000 /* Create with parents projid */
433de8ff14cSEric Biggers #define EXT4_CASEFOLD_FL		0x40000000 /* Casefolded directory */
4343dcf5451SChristoph Hellwig #define EXT4_RESERVED_FL		0x80000000 /* reserved for ext4 lib */
4353dcf5451SChristoph Hellwig 
4362a12e147SEric Biggers /* User modifiable flags */
4372a12e147SEric Biggers #define EXT4_FL_USER_MODIFIABLE		(EXT4_SECRM_FL | \
4382a12e147SEric Biggers 					 EXT4_UNRM_FL | \
4392a12e147SEric Biggers 					 EXT4_COMPR_FL | \
4402a12e147SEric Biggers 					 EXT4_SYNC_FL | \
4412a12e147SEric Biggers 					 EXT4_IMMUTABLE_FL | \
4422a12e147SEric Biggers 					 EXT4_APPEND_FL | \
4432a12e147SEric Biggers 					 EXT4_NODUMP_FL | \
4442a12e147SEric Biggers 					 EXT4_NOATIME_FL | \
4452a12e147SEric Biggers 					 EXT4_JOURNAL_DATA_FL | \
4462a12e147SEric Biggers 					 EXT4_NOTAIL_FL | \
4472a12e147SEric Biggers 					 EXT4_DIRSYNC_FL | \
4482a12e147SEric Biggers 					 EXT4_TOPDIR_FL | \
4492a12e147SEric Biggers 					 EXT4_EXTENTS_FL | \
4502a12e147SEric Biggers 					 0x00400000 /* EXT4_EOFBLOCKS_FL */ | \
4512a12e147SEric Biggers 					 EXT4_DAX_FL | \
4522a12e147SEric Biggers 					 EXT4_PROJINHERIT_FL | \
4532a12e147SEric Biggers 					 EXT4_CASEFOLD_FL)
4542a12e147SEric Biggers 
4552a12e147SEric Biggers /* User visible flags */
4562a12e147SEric Biggers #define EXT4_FL_USER_VISIBLE		(EXT4_FL_USER_MODIFIABLE | \
4572a12e147SEric Biggers 					 EXT4_DIRTY_FL | \
4582a12e147SEric Biggers 					 EXT4_COMPRBLK_FL | \
4592a12e147SEric Biggers 					 EXT4_NOCOMPR_FL | \
4602a12e147SEric Biggers 					 EXT4_ENCRYPT_FL | \
4612a12e147SEric Biggers 					 EXT4_INDEX_FL | \
4622a12e147SEric Biggers 					 EXT4_VERITY_FL | \
4632a12e147SEric Biggers 					 EXT4_INLINE_DATA_FL)
4643dcf5451SChristoph Hellwig 
465cb29a02dSEric Biggers /* Flags we can manipulate with through FS_IOC_FSSETXATTR */
4669b7365fcSLi Xi #define EXT4_FL_XFLAG_VISIBLE		(EXT4_SYNC_FL | \
4679b7365fcSLi Xi 					 EXT4_IMMUTABLE_FL | \
4689b7365fcSLi Xi 					 EXT4_APPEND_FL | \
4699b7365fcSLi Xi 					 EXT4_NODUMP_FL | \
4709b7365fcSLi Xi 					 EXT4_NOATIME_FL | \
471b383a73fSIra Weiny 					 EXT4_PROJINHERIT_FL | \
472b383a73fSIra Weiny 					 EXT4_DAX_FL)
4739b7365fcSLi Xi 
4748fa43a81SDuane Griffin /* Flags that should be inherited by new inodes from their parent. */
4758fa43a81SDuane Griffin #define EXT4_FL_INHERITED (EXT4_SECRM_FL | EXT4_UNRM_FL | EXT4_COMPR_FL |\
4761cd9f097STheodore Ts'o 			   EXT4_SYNC_FL | EXT4_NODUMP_FL | EXT4_NOATIME_FL |\
4778fa43a81SDuane Griffin 			   EXT4_NOCOMPR_FL | EXT4_JOURNAL_DATA_FL |\
478040cb378SLi Xi 			   EXT4_NOTAIL_FL | EXT4_DIRSYNC_FL |\
479b383a73fSIra Weiny 			   EXT4_PROJINHERIT_FL | EXT4_CASEFOLD_FL |\
480b383a73fSIra Weiny 			   EXT4_DAX_FL)
4818fa43a81SDuane Griffin 
4822dc6b0d4SDuane Griffin /* Flags that are appropriate for regular files (all but dir-specific ones). */
4837ddf79a1SWang Shilong #define EXT4_REG_FLMASK (~(EXT4_DIRSYNC_FL | EXT4_TOPDIR_FL | EXT4_CASEFOLD_FL |\
4847ddf79a1SWang Shilong 			   EXT4_PROJINHERIT_FL))
4852dc6b0d4SDuane Griffin 
4862dc6b0d4SDuane Griffin /* Flags that are appropriate for non-directories/regular files. */
4872dc6b0d4SDuane Griffin #define EXT4_OTHER_FLMASK (EXT4_NODUMP_FL | EXT4_NOATIME_FL)
4882dc6b0d4SDuane Griffin 
489abdc644eSyangerkun /* The only flags that should be swapped */
490abdc644eSyangerkun #define EXT4_FL_SHOULD_SWAP (EXT4_HUGE_FILE_FL | EXT4_EXTENTS_FL)
491abdc644eSyangerkun 
492b383a73fSIra Weiny /* Flags which are mutually exclusive to DAX */
493b383a73fSIra Weiny #define EXT4_DAX_MUT_EXCL (EXT4_VERITY_FL | EXT4_ENCRYPT_FL |\
494aa2f7792SXiao Yang 			   EXT4_JOURNAL_DATA_FL | EXT4_INLINE_DATA_FL)
495b383a73fSIra Weiny 
4962dc6b0d4SDuane Griffin /* Mask out flags that are inappropriate for the given type of inode. */
4972dc6b0d4SDuane Griffin static inline __u32 ext4_mask_flags(umode_t mode, __u32 flags)
4982dc6b0d4SDuane Griffin {
4992dc6b0d4SDuane Griffin 	if (S_ISDIR(mode))
5002dc6b0d4SDuane Griffin 		return flags;
5012dc6b0d4SDuane Griffin 	else if (S_ISREG(mode))
5022dc6b0d4SDuane Griffin 		return flags & EXT4_REG_FLMASK;
5032dc6b0d4SDuane Griffin 	else
5042dc6b0d4SDuane Griffin 		return flags & EXT4_OTHER_FLMASK;
5052dc6b0d4SDuane Griffin }
5062dc6b0d4SDuane Griffin 
50712e9b892SDmitry Monakhov /*
50812e9b892SDmitry Monakhov  * Inode flags used for atomic set/get
50912e9b892SDmitry Monakhov  */
51012e9b892SDmitry Monakhov enum {
51112e9b892SDmitry Monakhov 	EXT4_INODE_SECRM	= 0,	/* Secure deletion */
51212e9b892SDmitry Monakhov 	EXT4_INODE_UNRM		= 1,	/* Undelete */
51312e9b892SDmitry Monakhov 	EXT4_INODE_COMPR	= 2,	/* Compress file */
51412e9b892SDmitry Monakhov 	EXT4_INODE_SYNC		= 3,	/* Synchronous updates */
51512e9b892SDmitry Monakhov 	EXT4_INODE_IMMUTABLE	= 4,	/* Immutable file */
51612e9b892SDmitry Monakhov 	EXT4_INODE_APPEND	= 5,	/* writes to file may only append */
51712e9b892SDmitry Monakhov 	EXT4_INODE_NODUMP	= 6,	/* do not dump file */
51812e9b892SDmitry Monakhov 	EXT4_INODE_NOATIME	= 7,	/* do not update atime */
51912e9b892SDmitry Monakhov /* Reserved for compression usage... */
52012e9b892SDmitry Monakhov 	EXT4_INODE_DIRTY	= 8,
52112e9b892SDmitry Monakhov 	EXT4_INODE_COMPRBLK	= 9,	/* One or more compressed clusters */
52212e9b892SDmitry Monakhov 	EXT4_INODE_NOCOMPR	= 10,	/* Don't compress */
523f542fbe8STheodore Ts'o 	EXT4_INODE_ENCRYPT	= 11,	/* Encrypted file */
52412e9b892SDmitry Monakhov /* End compression flags --- maybe not all used */
52512e9b892SDmitry Monakhov 	EXT4_INODE_INDEX	= 12,	/* hash-indexed directory */
52612e9b892SDmitry Monakhov 	EXT4_INODE_IMAGIC	= 13,	/* AFS directory */
52712e9b892SDmitry Monakhov 	EXT4_INODE_JOURNAL_DATA	= 14,	/* file data should be journaled */
52812e9b892SDmitry Monakhov 	EXT4_INODE_NOTAIL	= 15,	/* file tail should not be merged */
52912e9b892SDmitry Monakhov 	EXT4_INODE_DIRSYNC	= 16,	/* dirsync behaviour (directories only) */
53012e9b892SDmitry Monakhov 	EXT4_INODE_TOPDIR	= 17,	/* Top of directory hierarchies*/
53112e9b892SDmitry Monakhov 	EXT4_INODE_HUGE_FILE	= 18,	/* Set to each huge file */
53212e9b892SDmitry Monakhov 	EXT4_INODE_EXTENTS	= 19,	/* Inode uses extents */
533c93d8f88SEric Biggers 	EXT4_INODE_VERITY	= 20,	/* Verity protected inode */
53412e9b892SDmitry Monakhov 	EXT4_INODE_EA_INODE	= 21,	/* Inode used for large EA */
5354337ecd1SEric Whitney /* 22 was formerly EXT4_INODE_EOFBLOCKS */
536b383a73fSIra Weiny 	EXT4_INODE_DAX		= 25,	/* Inode is DAX */
53767cf5b09STao Ma 	EXT4_INODE_INLINE_DATA	= 28,	/* Data in inode. */
5388b4953e1STheodore Ts'o 	EXT4_INODE_PROJINHERIT	= 29,	/* Create with parents projid */
539de8ff14cSEric Biggers 	EXT4_INODE_CASEFOLD	= 30,	/* Casefolded directory */
54012e9b892SDmitry Monakhov 	EXT4_INODE_RESERVED	= 31,	/* reserved for ext4 lib */
54112e9b892SDmitry Monakhov };
54212e9b892SDmitry Monakhov 
54312e9b892SDmitry Monakhov /*
5449a4c8019SCarlos Maiolino  * Since it's pretty easy to mix up bit numbers and hex values, we use a
5459a4c8019SCarlos Maiolino  * build-time check to make sure that EXT4_XXX_FL is consistent with respect to
5469a4c8019SCarlos Maiolino  * EXT4_INODE_XXX. If all is well, the macros will be dropped, so, it won't cost
5479a4c8019SCarlos Maiolino  * any extra space in the compiled kernel image, otherwise, the build will fail.
5489a4c8019SCarlos Maiolino  * It's important that these values are the same, since we are using
5499a4c8019SCarlos Maiolino  * EXT4_INODE_XXX to test for flag values, but EXT4_XXX_FL must be consistent
5509a4c8019SCarlos Maiolino  * with the values of FS_XXX_FL defined in include/linux/fs.h and the on-disk
5519a4c8019SCarlos Maiolino  * values found in ext2, ext3 and ext4 filesystems, and of course the values
5529a4c8019SCarlos Maiolino  * defined in e2fsprogs.
55312e9b892SDmitry Monakhov  *
55412e9b892SDmitry Monakhov  * It's not paranoia if the Murphy's Law really *is* out to get you.  :-)
55512e9b892SDmitry Monakhov  */
5569a4c8019SCarlos Maiolino #define TEST_FLAG_VALUE(FLAG) (EXT4_##FLAG##_FL == (1 << EXT4_INODE_##FLAG))
5579a4c8019SCarlos Maiolino #define CHECK_FLAG_VALUE(FLAG) BUILD_BUG_ON(!TEST_FLAG_VALUE(FLAG))
5589a4c8019SCarlos Maiolino 
55912e9b892SDmitry Monakhov static inline void ext4_check_flag_values(void)
56012e9b892SDmitry Monakhov {
56112e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(SECRM);
56212e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(UNRM);
56312e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(COMPR);
56412e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(SYNC);
56512e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(IMMUTABLE);
56612e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(APPEND);
56712e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(NODUMP);
56812e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(NOATIME);
56912e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(DIRTY);
57012e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(COMPRBLK);
57112e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(NOCOMPR);
5723edc18d8STheodore Ts'o 	CHECK_FLAG_VALUE(ENCRYPT);
57312e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(INDEX);
57412e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(IMAGIC);
57512e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(JOURNAL_DATA);
57612e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(NOTAIL);
57712e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(DIRSYNC);
57812e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(TOPDIR);
57912e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(HUGE_FILE);
58012e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(EXTENTS);
581c93d8f88SEric Biggers 	CHECK_FLAG_VALUE(VERITY);
58212e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(EA_INODE);
58367cf5b09STao Ma 	CHECK_FLAG_VALUE(INLINE_DATA);
5848b4953e1STheodore Ts'o 	CHECK_FLAG_VALUE(PROJINHERIT);
585de8ff14cSEric Biggers 	CHECK_FLAG_VALUE(CASEFOLD);
58612e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(RESERVED);
58712e9b892SDmitry Monakhov }
58812e9b892SDmitry Monakhov 
5893dcf5451SChristoph Hellwig /* Used to pass group descriptor data when online resize is done */
5903dcf5451SChristoph Hellwig struct ext4_new_group_input {
5913dcf5451SChristoph Hellwig 	__u32 group;		/* Group number for this data */
5923dcf5451SChristoph Hellwig 	__u64 block_bitmap;	/* Absolute block number of block bitmap */
5933dcf5451SChristoph Hellwig 	__u64 inode_bitmap;	/* Absolute block number of inode bitmap */
5943dcf5451SChristoph Hellwig 	__u64 inode_table;	/* Absolute block number of inode table start */
5953dcf5451SChristoph Hellwig 	__u32 blocks_count;	/* Total number of blocks in this group */
5963dcf5451SChristoph Hellwig 	__u16 reserved_blocks;	/* Number of reserved blocks in this group */
5973dcf5451SChristoph Hellwig 	__u16 unused;
5983dcf5451SChristoph Hellwig };
5993dcf5451SChristoph Hellwig 
6004d92dc0fSBen Hutchings #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
6014d92dc0fSBen Hutchings struct compat_ext4_new_group_input {
6024d92dc0fSBen Hutchings 	u32 group;
6034d92dc0fSBen Hutchings 	compat_u64 block_bitmap;
6044d92dc0fSBen Hutchings 	compat_u64 inode_bitmap;
6054d92dc0fSBen Hutchings 	compat_u64 inode_table;
6064d92dc0fSBen Hutchings 	u32 blocks_count;
6074d92dc0fSBen Hutchings 	u16 reserved_blocks;
6084d92dc0fSBen Hutchings 	u16 unused;
6094d92dc0fSBen Hutchings };
6104d92dc0fSBen Hutchings #endif
6114d92dc0fSBen Hutchings 
6123dcf5451SChristoph Hellwig /* The struct ext4_new_group_input in kernel space, with free_blocks_count */
6133dcf5451SChristoph Hellwig struct ext4_new_group_data {
6143dcf5451SChristoph Hellwig 	__u32 group;
6153dcf5451SChristoph Hellwig 	__u64 block_bitmap;
6163dcf5451SChristoph Hellwig 	__u64 inode_bitmap;
6173dcf5451SChristoph Hellwig 	__u64 inode_table;
6183dcf5451SChristoph Hellwig 	__u32 blocks_count;
6193dcf5451SChristoph Hellwig 	__u16 reserved_blocks;
620d77147ffSharshads 	__u16 mdata_blocks;
621d77147ffSharshads 	__u32 free_clusters_count;
6223dcf5451SChristoph Hellwig };
6233dcf5451SChristoph Hellwig 
62433afdcc5SYongqiang Yang /* Indexes used to index group tables in ext4_new_group_data */
62533afdcc5SYongqiang Yang enum {
62633afdcc5SYongqiang Yang 	BLOCK_BITMAP = 0,	/* block bitmap */
62733afdcc5SYongqiang Yang 	INODE_BITMAP,		/* inode bitmap */
62833afdcc5SYongqiang Yang 	INODE_TABLE,		/* inode tables */
62933afdcc5SYongqiang Yang 	GROUP_TABLE_COUNT,
63033afdcc5SYongqiang Yang };
63133afdcc5SYongqiang Yang 
6323dcf5451SChristoph Hellwig /*
63379e83036SEric Sandeen  * Flags used by ext4_map_blocks()
6343dcf5451SChristoph Hellwig  */
635556615dcSLukas Czerner 	/* Allocate any needed blocks and/or convert an unwritten
636c2177057STheodore Ts'o 	   extent to be an initialized ext4 */
6372ac3b6e0STheodore Ts'o #define EXT4_GET_BLOCKS_CREATE			0x0001
638556615dcSLukas Czerner 	/* Request the creation of an unwritten extent */
639556615dcSLukas Czerner #define EXT4_GET_BLOCKS_UNWRIT_EXT		0x0002
640556615dcSLukas Czerner #define EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT	(EXT4_GET_BLOCKS_UNWRIT_EXT|\
641c2177057STheodore Ts'o 						 EXT4_GET_BLOCKS_CREATE)
64227dd4385SLukas Czerner 	/* Caller is from the delayed allocation writeout path
64327dd4385SLukas Czerner 	 * finally doing the actual allocation of delayed blocks */
64403f5d8bcSJan Kara #define EXT4_GET_BLOCKS_DELALLOC_RESERVE	0x0004
6450031462bSMingming Cao 	/* caller is from the direct IO path, request to creation of an
646556615dcSLukas Czerner 	unwritten extents if not allocated, split the unwritten
6470031462bSMingming Cao 	extent if blocks has been preallocated already*/
648c7064ef1SJiaying Zhang #define EXT4_GET_BLOCKS_PRE_IO			0x0008
6491296cc85SAneesh Kumar K.V #define EXT4_GET_BLOCKS_CONVERT			0x0010
650c7064ef1SJiaying Zhang #define EXT4_GET_BLOCKS_IO_CREATE_EXT		(EXT4_GET_BLOCKS_PRE_IO|\
651556615dcSLukas Czerner 					 EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT)
652c7064ef1SJiaying Zhang 	/* Convert extent to initialized after IO complete */
653c7064ef1SJiaying Zhang #define EXT4_GET_BLOCKS_IO_CONVERT_EXT		(EXT4_GET_BLOCKS_CONVERT|\
654556615dcSLukas Czerner 					 EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT)
65527dd4385SLukas Czerner 	/* Eventual metadata allocation (due to growing extent tree)
65627dd4385SLukas Czerner 	 * should not fail, so try to use reserved blocks for that.*/
65727dd4385SLukas Czerner #define EXT4_GET_BLOCKS_METADATA_NOFAIL		0x0020
658556b27abSVivek Haldar 	/* Don't normalize allocation size (used for fallocate) */
659556b27abSVivek Haldar #define EXT4_GET_BLOCKS_NO_NORMALIZE		0x0040
660b8a86845SLukas Czerner 	/* Convert written extents to unwritten */
6612dcba478SJan Kara #define EXT4_GET_BLOCKS_CONVERT_UNWRITTEN	0x0100
662c86d8db3SJan Kara 	/* Write zeros to newly created written extents */
663c86d8db3SJan Kara #define EXT4_GET_BLOCKS_ZERO			0x0200
664c86d8db3SJan Kara #define EXT4_GET_BLOCKS_CREATE_ZERO		(EXT4_GET_BLOCKS_CREATE |\
665c86d8db3SJan Kara 					EXT4_GET_BLOCKS_ZERO)
666ee0876bcSJan Kara 	/* Caller will submit data before dropping transaction handle. This
667ee0876bcSJan Kara 	 * allows jbd2 to avoid submitting data before commit. */
668ee0876bcSJan Kara #define EXT4_GET_BLOCKS_IO_SUBMIT		0x0400
6693dcf5451SChristoph Hellwig 
6703dcf5451SChristoph Hellwig /*
6717869a4a6STheodore Ts'o  * The bit position of these flags must not overlap with any of the
672ed8a1a76STheodore Ts'o  * EXT4_GET_BLOCKS_*.  They are used by ext4_find_extent(),
673107a7bd3STheodore Ts'o  * read_extent_tree_block(), ext4_split_extent_at(),
6747869a4a6STheodore Ts'o  * ext4_ext_insert_extent(), and ext4_ext_create_new_leaf().
6757869a4a6STheodore Ts'o  * EXT4_EX_NOCACHE is used to indicate that the we shouldn't be
6767869a4a6STheodore Ts'o  * caching the extents when reading from the extent tree while a
6777869a4a6STheodore Ts'o  * truncate or punch hole operation is in progress.
678107a7bd3STheodore Ts'o  */
679d26e2c4dSTheodore Ts'o #define EXT4_EX_NOCACHE				0x40000000
680d26e2c4dSTheodore Ts'o #define EXT4_EX_FORCE_CACHE			0x20000000
68173c384c0STheodore Ts'o #define EXT4_EX_NOFAIL				0x10000000
682107a7bd3STheodore Ts'o 
683107a7bd3STheodore Ts'o /*
684e6362609STheodore Ts'o  * Flags used by ext4_free_blocks
685e6362609STheodore Ts'o  */
686e6362609STheodore Ts'o #define EXT4_FREE_BLOCKS_METADATA		0x0001
687e6362609STheodore Ts'o #define EXT4_FREE_BLOCKS_FORGET			0x0002
6881f2acb60STheodore Ts'o #define EXT4_FREE_BLOCKS_VALIDATED		0x0004
6897132de74SMaxim Patlasov #define EXT4_FREE_BLOCKS_NO_QUOT_UPDATE		0x0008
69084130193STheodore Ts'o #define EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER	0x0010
69184130193STheodore Ts'o #define EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER	0x0020
6929fe67149SEric Whitney #define EXT4_FREE_BLOCKS_RERESERVE_CLUSTER      0x0040
693e6362609STheodore Ts'o 
694e6362609STheodore Ts'o /*
6953dcf5451SChristoph Hellwig  * ioctl commands
6963dcf5451SChristoph Hellwig  */
6973dcf5451SChristoph Hellwig #define	EXT4_IOC_GETVERSION		_IOR('f', 3, long)
6983dcf5451SChristoph Hellwig #define	EXT4_IOC_SETVERSION		_IOW('f', 4, long)
6993dcf5451SChristoph Hellwig #define	EXT4_IOC_GETVERSION_OLD		FS_IOC_GETVERSION
7003dcf5451SChristoph Hellwig #define	EXT4_IOC_SETVERSION_OLD		FS_IOC_SETVERSION
7013dcf5451SChristoph Hellwig #define EXT4_IOC_GETRSVSZ		_IOR('f', 5, long)
7023dcf5451SChristoph Hellwig #define EXT4_IOC_SETRSVSZ		_IOW('f', 6, long)
7038eea80d5STheodore Ts'o #define EXT4_IOC_GROUP_EXTEND		_IOW('f', 7, unsigned long)
7048eea80d5STheodore Ts'o #define EXT4_IOC_GROUP_ADD		_IOW('f', 8, struct ext4_new_group_input)
7058eea80d5STheodore Ts'o #define EXT4_IOC_MIGRATE		_IO('f', 9)
706ccd2506bSTheodore Ts'o  /* note ioctl 10 reserved for an early version of the FIEMAP ioctl */
7078eea80d5STheodore Ts'o  /* note ioctl 11 reserved for filesystem-independent FIEMAP ioctl */
708ccd2506bSTheodore Ts'o #define EXT4_IOC_ALLOC_DA_BLKS		_IO('f', 12)
709748de673SAkira Fujita #define EXT4_IOC_MOVE_EXT		_IOWR('f', 15, struct move_extent)
71019c5246dSYongqiang Yang #define EXT4_IOC_RESIZE_FS		_IOW('f', 16, __u64)
711393d1d1dSDr. Tilmann Bubeck #define EXT4_IOC_SWAP_BOOT		_IO('f', 17)
7127869a4a6STheodore Ts'o #define EXT4_IOC_PRECACHE_EXTENTS	_IO('f', 18)
713b0c013e2STheodore Ts'o /* ioctl codes 19--39 are reserved for fscrypt */
714b0c013e2STheodore Ts'o #define EXT4_IOC_CLEAR_ES_CACHE		_IO('f', 40)
7151ad3ea6eSTheodore Ts'o #define EXT4_IOC_GETSTATE		_IOW('f', 41, __u32)
716bb5835edSTheodore Ts'o #define EXT4_IOC_GET_ES_CACHE		_IOWR('f', 42, struct fiemap)
7173dcf5451SChristoph Hellwig 
718e9be2ac7STheodore Ts'o #define EXT4_IOC_SHUTDOWN _IOR ('X', 125, __u32)
719783d9485STheodore Ts'o 
720783d9485STheodore Ts'o /*
721783d9485STheodore Ts'o  * Flags for going down operation
722783d9485STheodore Ts'o  */
723783d9485STheodore Ts'o #define EXT4_GOING_FLAGS_DEFAULT		0x0	/* going down */
724783d9485STheodore Ts'o #define EXT4_GOING_FLAGS_LOGFLUSH		0x1	/* flush log but not data */
725783d9485STheodore Ts'o #define EXT4_GOING_FLAGS_NOLOGFLUSH		0x2	/* don't flush log nor data */
726783d9485STheodore Ts'o 
7271ad3ea6eSTheodore Ts'o /*
7281ad3ea6eSTheodore Ts'o  * Flags returned by EXT4_IOC_GETSTATE
7291ad3ea6eSTheodore Ts'o  *
7301ad3ea6eSTheodore Ts'o  * We only expose to userspace a subset of the state flags in
7311ad3ea6eSTheodore Ts'o  * i_state_flags
7321ad3ea6eSTheodore Ts'o  */
7331ad3ea6eSTheodore Ts'o #define EXT4_STATE_FLAG_EXT_PRECACHED	0x00000001
7341ad3ea6eSTheodore Ts'o #define EXT4_STATE_FLAG_NEW		0x00000002
7351ad3ea6eSTheodore Ts'o #define EXT4_STATE_FLAG_NEWENTRY	0x00000004
7361ad3ea6eSTheodore Ts'o #define EXT4_STATE_FLAG_DA_ALLOC_CLOSE	0x00000008
737783d9485STheodore Ts'o 
738899ad0ceSBen Hutchings #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
7393dcf5451SChristoph Hellwig /*
7403dcf5451SChristoph Hellwig  * ioctl commands in 32 bit emulation
7413dcf5451SChristoph Hellwig  */
7423dcf5451SChristoph Hellwig #define EXT4_IOC32_GETVERSION		_IOR('f', 3, int)
7433dcf5451SChristoph Hellwig #define EXT4_IOC32_SETVERSION		_IOW('f', 4, int)
7443dcf5451SChristoph Hellwig #define EXT4_IOC32_GETRSVSZ		_IOR('f', 5, int)
7453dcf5451SChristoph Hellwig #define EXT4_IOC32_SETRSVSZ		_IOW('f', 6, int)
7463dcf5451SChristoph Hellwig #define EXT4_IOC32_GROUP_EXTEND		_IOW('f', 7, unsigned int)
7474d92dc0fSBen Hutchings #define EXT4_IOC32_GROUP_ADD		_IOW('f', 8, struct compat_ext4_new_group_input)
7483dcf5451SChristoph Hellwig #define EXT4_IOC32_GETVERSION_OLD	FS_IOC32_GETVERSION
7493dcf5451SChristoph Hellwig #define EXT4_IOC32_SETVERSION_OLD	FS_IOC32_SETVERSION
750899ad0ceSBen Hutchings #endif
7513dcf5451SChristoph Hellwig 
752bb5835edSTheodore Ts'o /*
753bb5835edSTheodore Ts'o  * Returned by EXT4_IOC_GET_ES_CACHE as an additional possible flag.
754bb5835edSTheodore Ts'o  * It indicates that the entry in extent status cache is for a hole.
755bb5835edSTheodore Ts'o  */
756bb5835edSTheodore Ts'o #define EXT4_FIEMAP_EXTENT_HOLE		0x08000000
757bb5835edSTheodore Ts'o 
758b595076aSUwe Kleine-König /* Max physical block we can address w/o extents */
759fb0a387dSEric Sandeen #define EXT4_MAX_BLOCK_FILE_PHYS	0xFFFFFFFF
760fb0a387dSEric Sandeen 
761bcd8e91fSTheodore Ts'o /* Max logical block we can support */
7629f44eda1SRitesh Harjani #define EXT4_MAX_LOGICAL_BLOCK		0xFFFFFFFE
763bcd8e91fSTheodore Ts'o 
7643dcf5451SChristoph Hellwig /*
7653dcf5451SChristoph Hellwig  * Structure of an inode on the disk
7663dcf5451SChristoph Hellwig  */
7673dcf5451SChristoph Hellwig struct ext4_inode {
7683dcf5451SChristoph Hellwig 	__le16	i_mode;		/* File mode */
7693dcf5451SChristoph Hellwig 	__le16	i_uid;		/* Low 16 bits of Owner Uid */
7703dcf5451SChristoph Hellwig 	__le32	i_size_lo;	/* Size in bytes */
7713dcf5451SChristoph Hellwig 	__le32	i_atime;	/* Access time */
7723dcf5451SChristoph Hellwig 	__le32	i_ctime;	/* Inode Change time */
7733dcf5451SChristoph Hellwig 	__le32	i_mtime;	/* Modification time */
7743dcf5451SChristoph Hellwig 	__le32	i_dtime;	/* Deletion Time */
7753dcf5451SChristoph Hellwig 	__le16	i_gid;		/* Low 16 bits of Group Id */
7763dcf5451SChristoph Hellwig 	__le16	i_links_count;	/* Links count */
7773dcf5451SChristoph Hellwig 	__le32	i_blocks_lo;	/* Blocks count */
7783dcf5451SChristoph Hellwig 	__le32	i_flags;	/* File flags */
7793dcf5451SChristoph Hellwig 	union {
7803dcf5451SChristoph Hellwig 		struct {
7813dcf5451SChristoph Hellwig 			__le32  l_i_version;
7823dcf5451SChristoph Hellwig 		} linux1;
7833dcf5451SChristoph Hellwig 		struct {
7843dcf5451SChristoph Hellwig 			__u32  h_i_translator;
7853dcf5451SChristoph Hellwig 		} hurd1;
7863dcf5451SChristoph Hellwig 		struct {
7873dcf5451SChristoph Hellwig 			__u32  m_i_reserved1;
7883dcf5451SChristoph Hellwig 		} masix1;
7893dcf5451SChristoph Hellwig 	} osd1;				/* OS dependent 1 */
7903dcf5451SChristoph Hellwig 	__le32	i_block[EXT4_N_BLOCKS];/* Pointers to blocks */
7913dcf5451SChristoph Hellwig 	__le32	i_generation;	/* File version (for NFS) */
7923dcf5451SChristoph Hellwig 	__le32	i_file_acl_lo;	/* File ACL */
7933dcf5451SChristoph Hellwig 	__le32	i_size_high;
7943dcf5451SChristoph Hellwig 	__le32	i_obso_faddr;	/* Obsoleted fragment address */
7953dcf5451SChristoph Hellwig 	union {
7963dcf5451SChristoph Hellwig 		struct {
7973dcf5451SChristoph Hellwig 			__le16	l_i_blocks_high; /* were l_i_reserved1 */
7983dcf5451SChristoph Hellwig 			__le16	l_i_file_acl_high;
7993dcf5451SChristoph Hellwig 			__le16	l_i_uid_high;	/* these 2 fields */
8003dcf5451SChristoph Hellwig 			__le16	l_i_gid_high;	/* were reserved2[0] */
801e6153918SDarrick J. Wong 			__le16	l_i_checksum_lo;/* crc32c(uuid+inum+inode) LE */
802e6153918SDarrick J. Wong 			__le16	l_i_reserved;
8033dcf5451SChristoph Hellwig 		} linux2;
8043dcf5451SChristoph Hellwig 		struct {
8053dcf5451SChristoph Hellwig 			__le16	h_i_reserved1;	/* Obsoleted fragment number/size which are removed in ext4 */
8063dcf5451SChristoph Hellwig 			__u16	h_i_mode_high;
8073dcf5451SChristoph Hellwig 			__u16	h_i_uid_high;
8083dcf5451SChristoph Hellwig 			__u16	h_i_gid_high;
8093dcf5451SChristoph Hellwig 			__u32	h_i_author;
8103dcf5451SChristoph Hellwig 		} hurd2;
8113dcf5451SChristoph Hellwig 		struct {
8123dcf5451SChristoph Hellwig 			__le16	h_i_reserved1;	/* Obsoleted fragment number/size which are removed in ext4 */
8133dcf5451SChristoph Hellwig 			__le16	m_i_file_acl_high;
8143dcf5451SChristoph Hellwig 			__u32	m_i_reserved2[2];
8153dcf5451SChristoph Hellwig 		} masix2;
8163dcf5451SChristoph Hellwig 	} osd2;				/* OS dependent 2 */
8173dcf5451SChristoph Hellwig 	__le16	i_extra_isize;
818e6153918SDarrick J. Wong 	__le16	i_checksum_hi;	/* crc32c(uuid+inum+inode) BE */
8193dcf5451SChristoph Hellwig 	__le32  i_ctime_extra;  /* extra Change time      (nsec << 2 | epoch) */
8203dcf5451SChristoph Hellwig 	__le32  i_mtime_extra;  /* extra Modification time(nsec << 2 | epoch) */
8213dcf5451SChristoph Hellwig 	__le32  i_atime_extra;  /* extra Access time      (nsec << 2 | epoch) */
8223dcf5451SChristoph Hellwig 	__le32  i_crtime;       /* File Creation time */
8233dcf5451SChristoph Hellwig 	__le32  i_crtime_extra; /* extra FileCreationtime (nsec << 2 | epoch) */
8243dcf5451SChristoph Hellwig 	__le32  i_version_hi;	/* high 32 bits for 64-bit version */
8258b4953e1STheodore Ts'o 	__le32	i_projid;	/* Project ID */
8263dcf5451SChristoph Hellwig };
8273dcf5451SChristoph Hellwig 
828748de673SAkira Fujita struct move_extent {
829748de673SAkira Fujita 	__u32 reserved;		/* should be zero */
830748de673SAkira Fujita 	__u32 donor_fd;		/* donor file descriptor */
831748de673SAkira Fujita 	__u64 orig_start;	/* logical start offset in block for orig */
832748de673SAkira Fujita 	__u64 donor_start;	/* logical start offset in block for donor */
833748de673SAkira Fujita 	__u64 len;		/* block length to be moved */
834748de673SAkira Fujita 	__u64 moved_len;	/* moved block length */
835748de673SAkira Fujita };
8363dcf5451SChristoph Hellwig 
8373dcf5451SChristoph Hellwig #define EXT4_EPOCH_BITS 2
8383dcf5451SChristoph Hellwig #define EXT4_EPOCH_MASK ((1 << EXT4_EPOCH_BITS) - 1)
8393dcf5451SChristoph Hellwig #define EXT4_NSEC_MASK  (~0UL << EXT4_EPOCH_BITS)
8403dcf5451SChristoph Hellwig 
8413dcf5451SChristoph Hellwig /*
8423dcf5451SChristoph Hellwig  * Extended fields will fit into an inode if the filesystem was formatted
8433dcf5451SChristoph Hellwig  * with large inodes (-I 256 or larger) and there are not currently any EAs
8443dcf5451SChristoph Hellwig  * consuming all of the available space. For new inodes we always reserve
8453dcf5451SChristoph Hellwig  * enough space for the kernel's known extended fields, but for inodes
8463dcf5451SChristoph Hellwig  * created with an old kernel this might not have been the case. None of
8473dcf5451SChristoph Hellwig  * the extended inode fields is critical for correct filesystem operation.
8483dcf5451SChristoph Hellwig  * This macro checks if a certain field fits in the inode. Note that
8493dcf5451SChristoph Hellwig  * inode-size = GOOD_OLD_INODE_SIZE + i_extra_isize
8503dcf5451SChristoph Hellwig  */
8513dcf5451SChristoph Hellwig #define EXT4_FITS_IN_INODE(ext4_inode, einode, field)	\
8523dcf5451SChristoph Hellwig 	((offsetof(typeof(*ext4_inode), field) +	\
8533dcf5451SChristoph Hellwig 	  sizeof((ext4_inode)->field))			\
8543dcf5451SChristoph Hellwig 	<= (EXT4_GOOD_OLD_INODE_SIZE +			\
8553dcf5451SChristoph Hellwig 	    (einode)->i_extra_isize))			\
8563dcf5451SChristoph Hellwig 
857a4dad1aeSDavid Turner /*
858a4dad1aeSDavid Turner  * We use an encoding that preserves the times for extra epoch "00":
859a4dad1aeSDavid Turner  *
860a4dad1aeSDavid Turner  * extra  msb of                         adjust for signed
861a4dad1aeSDavid Turner  * epoch  32-bit                         32-bit tv_sec to
862a4dad1aeSDavid Turner  * bits   time    decoded 64-bit tv_sec  64-bit tv_sec      valid time range
863a4dad1aeSDavid Turner  * 0 0    1    -0x80000000..-0x00000001  0x000000000 1901-12-13..1969-12-31
864a4dad1aeSDavid Turner  * 0 0    0    0x000000000..0x07fffffff  0x000000000 1970-01-01..2038-01-19
865a4dad1aeSDavid Turner  * 0 1    1    0x080000000..0x0ffffffff  0x100000000 2038-01-19..2106-02-07
866a4dad1aeSDavid Turner  * 0 1    0    0x100000000..0x17fffffff  0x100000000 2106-02-07..2174-02-25
867a4dad1aeSDavid Turner  * 1 0    1    0x180000000..0x1ffffffff  0x200000000 2174-02-25..2242-03-16
868a4dad1aeSDavid Turner  * 1 0    0    0x200000000..0x27fffffff  0x200000000 2242-03-16..2310-04-04
869a4dad1aeSDavid Turner  * 1 1    1    0x280000000..0x2ffffffff  0x300000000 2310-04-04..2378-04-22
870a4dad1aeSDavid Turner  * 1 1    0    0x300000000..0x37fffffff  0x300000000 2378-04-22..2446-05-10
871a4dad1aeSDavid Turner  *
872a4dad1aeSDavid Turner  * Note that previous versions of the kernel on 64-bit systems would
873a4dad1aeSDavid Turner  * incorrectly use extra epoch bits 1,1 for dates between 1901 and
874a4dad1aeSDavid Turner  * 1970.  e2fsck will correct this, assuming that it is run on the
875a4dad1aeSDavid Turner  * affected filesystem before 2242.
876a4dad1aeSDavid Turner  */
877a4dad1aeSDavid Turner 
8787b62b293SArnd Bergmann static inline __le32 ext4_encode_extra_time(struct timespec64 *time)
8793dcf5451SChristoph Hellwig {
8807b62b293SArnd Bergmann 	u32 extra =((time->tv_sec - (s32)time->tv_sec) >> 32) & EXT4_EPOCH_MASK;
881a4dad1aeSDavid Turner 	return cpu_to_le32(extra | (time->tv_nsec << EXT4_EPOCH_BITS));
8823dcf5451SChristoph Hellwig }
8833dcf5451SChristoph Hellwig 
8847b62b293SArnd Bergmann static inline void ext4_decode_extra_time(struct timespec64 *time,
8857b62b293SArnd Bergmann 					  __le32 extra)
8863dcf5451SChristoph Hellwig {
887cd2d9922STheodore Ts'o 	if (unlikely(extra & cpu_to_le32(EXT4_EPOCH_MASK)))
888a4dad1aeSDavid Turner 		time->tv_sec += (u64)(le32_to_cpu(extra) & EXT4_EPOCH_MASK) << 32;
889c1fccc06STheodore Ts'o 	time->tv_nsec = (le32_to_cpu(extra) & EXT4_NSEC_MASK) >> EXT4_EPOCH_BITS;
8903dcf5451SChristoph Hellwig }
8913dcf5451SChristoph Hellwig 
8923dcf5451SChristoph Hellwig #define EXT4_INODE_SET_XTIME(xtime, inode, raw_inode)				\
8933dcf5451SChristoph Hellwig do {										\
89495582b00SDeepa Dinamani 	if (EXT4_FITS_IN_INODE(raw_inode, EXT4_I(inode), xtime ## _extra))     {\
8954881c497SDeepa Dinamani 		(raw_inode)->xtime = cpu_to_le32((inode)->xtime.tv_sec);	\
8963dcf5451SChristoph Hellwig 		(raw_inode)->xtime ## _extra =					\
8977b62b293SArnd Bergmann 				ext4_encode_extra_time(&(inode)->xtime);	\
89895582b00SDeepa Dinamani 		}								\
899cba465b4SDeepa Dinamani 	else	\
9004881c497SDeepa Dinamani 		(raw_inode)->xtime = cpu_to_le32(clamp_t(int32_t, (inode)->xtime.tv_sec, S32_MIN, S32_MAX));	\
9013dcf5451SChristoph Hellwig } while (0)
9023dcf5451SChristoph Hellwig 
9033dcf5451SChristoph Hellwig #define EXT4_EINODE_SET_XTIME(xtime, einode, raw_inode)			       \
9043dcf5451SChristoph Hellwig do {									       \
9053dcf5451SChristoph Hellwig 	if (EXT4_FITS_IN_INODE(raw_inode, einode, xtime))		       \
9063dcf5451SChristoph Hellwig 		(raw_inode)->xtime = cpu_to_le32((einode)->xtime.tv_sec);      \
9073dcf5451SChristoph Hellwig 	if (EXT4_FITS_IN_INODE(raw_inode, einode, xtime ## _extra))	       \
9083dcf5451SChristoph Hellwig 		(raw_inode)->xtime ## _extra =				       \
9093dcf5451SChristoph Hellwig 				ext4_encode_extra_time(&(einode)->xtime);      \
9103dcf5451SChristoph Hellwig } while (0)
9113dcf5451SChristoph Hellwig 
9123dcf5451SChristoph Hellwig #define EXT4_INODE_GET_XTIME(xtime, inode, raw_inode)				\
9133dcf5451SChristoph Hellwig do {										\
9143dcf5451SChristoph Hellwig 	(inode)->xtime.tv_sec = (signed)le32_to_cpu((raw_inode)->xtime);	\
91595582b00SDeepa Dinamani 	if (EXT4_FITS_IN_INODE(raw_inode, EXT4_I(inode), xtime ## _extra)) {	\
9167b62b293SArnd Bergmann 		ext4_decode_extra_time(&(inode)->xtime,				\
9173dcf5451SChristoph Hellwig 				       raw_inode->xtime ## _extra);		\
91895582b00SDeepa Dinamani 		}								\
919af0b44a1SEric Sandeen 	else									\
920af0b44a1SEric Sandeen 		(inode)->xtime.tv_nsec = 0;					\
9213dcf5451SChristoph Hellwig } while (0)
9223dcf5451SChristoph Hellwig 
92395582b00SDeepa Dinamani 
9243dcf5451SChristoph Hellwig #define EXT4_EINODE_GET_XTIME(xtime, einode, raw_inode)			       \
9253dcf5451SChristoph Hellwig do {									       \
9263dcf5451SChristoph Hellwig 	if (EXT4_FITS_IN_INODE(raw_inode, einode, xtime))		       \
9273dcf5451SChristoph Hellwig 		(einode)->xtime.tv_sec = 				       \
9283dcf5451SChristoph Hellwig 			(signed)le32_to_cpu((raw_inode)->xtime);	       \
92919ea8060STheodore Ts'o 	else								       \
93019ea8060STheodore Ts'o 		(einode)->xtime.tv_sec = 0;				       \
9313dcf5451SChristoph Hellwig 	if (EXT4_FITS_IN_INODE(raw_inode, einode, xtime ## _extra))	       \
9323dcf5451SChristoph Hellwig 		ext4_decode_extra_time(&(einode)->xtime,		       \
9333dcf5451SChristoph Hellwig 				       raw_inode->xtime ## _extra);	       \
934af0b44a1SEric Sandeen 	else								       \
935af0b44a1SEric Sandeen 		(einode)->xtime.tv_nsec = 0;				       \
9363dcf5451SChristoph Hellwig } while (0)
9373dcf5451SChristoph Hellwig 
9383dcf5451SChristoph Hellwig #define i_disk_version osd1.linux1.l_i_version
9393dcf5451SChristoph Hellwig 
9403dcf5451SChristoph Hellwig #if defined(__KERNEL__) || defined(__linux__)
9413dcf5451SChristoph Hellwig #define i_reserved1	osd1.linux1.l_i_reserved1
9423dcf5451SChristoph Hellwig #define i_file_acl_high	osd2.linux2.l_i_file_acl_high
9433dcf5451SChristoph Hellwig #define i_blocks_high	osd2.linux2.l_i_blocks_high
9443dcf5451SChristoph Hellwig #define i_uid_low	i_uid
9453dcf5451SChristoph Hellwig #define i_gid_low	i_gid
9463dcf5451SChristoph Hellwig #define i_uid_high	osd2.linux2.l_i_uid_high
9473dcf5451SChristoph Hellwig #define i_gid_high	osd2.linux2.l_i_gid_high
948e6153918SDarrick J. Wong #define i_checksum_lo	osd2.linux2.l_i_checksum_lo
9493dcf5451SChristoph Hellwig 
9503dcf5451SChristoph Hellwig #elif defined(__GNU__)
9513dcf5451SChristoph Hellwig 
9523dcf5451SChristoph Hellwig #define i_translator	osd1.hurd1.h_i_translator
9533dcf5451SChristoph Hellwig #define i_uid_high	osd2.hurd2.h_i_uid_high
9543dcf5451SChristoph Hellwig #define i_gid_high	osd2.hurd2.h_i_gid_high
9553dcf5451SChristoph Hellwig #define i_author	osd2.hurd2.h_i_author
9563dcf5451SChristoph Hellwig 
9573dcf5451SChristoph Hellwig #elif defined(__masix__)
9583dcf5451SChristoph Hellwig 
9593dcf5451SChristoph Hellwig #define i_reserved1	osd1.masix1.m_i_reserved1
9603dcf5451SChristoph Hellwig #define i_file_acl_high	osd2.masix2.m_i_file_acl_high
9613dcf5451SChristoph Hellwig #define i_reserved2	osd2.masix2.m_i_reserved2
9623dcf5451SChristoph Hellwig 
9633dcf5451SChristoph Hellwig #endif /* defined(__KERNEL__) || defined(__linux__) */
9643dcf5451SChristoph Hellwig 
965c0677e6dSZheng Liu #include "extents_status.h"
9666866d7b3SHarshad Shirwadkar #include "fast_commit.h"
967c0677e6dSZheng Liu 
968d444c3c3STheodore Ts'o /*
969daf647d2STheodore Ts'o  * Lock subclasses for i_data_sem in the ext4_inode_info structure.
970daf647d2STheodore Ts'o  *
971daf647d2STheodore Ts'o  * These are needed to avoid lockdep false positives when we need to
972daf647d2STheodore Ts'o  * allocate blocks to the quota inode during ext4_map_blocks(), while
973daf647d2STheodore Ts'o  * holding i_data_sem for a normal (non-quota) inode.  Since we don't
974daf647d2STheodore Ts'o  * do quota tracking for the quota inode, this avoids deadlock (as
975daf647d2STheodore Ts'o  * well as infinite recursion, since it isn't turtles all the way
976daf647d2STheodore Ts'o  * down...)
977daf647d2STheodore Ts'o  *
978daf647d2STheodore Ts'o  *  I_DATA_SEM_NORMAL - Used for most inodes
979daf647d2STheodore Ts'o  *  I_DATA_SEM_OTHER  - Used by move_inode.c for the second normal inode
980daf647d2STheodore Ts'o  *			  where the second inode has larger inode number
981daf647d2STheodore Ts'o  *			  than the first
982daf647d2STheodore Ts'o  *  I_DATA_SEM_QUOTA  - Used for quota inodes only
983daf647d2STheodore Ts'o  */
984daf647d2STheodore Ts'o enum {
985daf647d2STheodore Ts'o 	I_DATA_SEM_NORMAL = 0,
986daf647d2STheodore Ts'o 	I_DATA_SEM_OTHER,
987daf647d2STheodore Ts'o 	I_DATA_SEM_QUOTA,
988daf647d2STheodore Ts'o };
989daf647d2STheodore Ts'o 
990daf647d2STheodore Ts'o 
991daf647d2STheodore Ts'o /*
992d444c3c3STheodore Ts'o  * fourth extended file system inode data in memory
993d444c3c3STheodore Ts'o  */
994d444c3c3STheodore Ts'o struct ext4_inode_info {
995d444c3c3STheodore Ts'o 	__le32	i_data[15];	/* unconverted */
996d444c3c3STheodore Ts'o 	__u32	i_dtime;
99712e9b892SDmitry Monakhov 	ext4_fsblk_t	i_file_acl;
998d444c3c3STheodore Ts'o 
999d444c3c3STheodore Ts'o 	/*
1000d444c3c3STheodore Ts'o 	 * i_block_group is the number of the block group which contains
1001d444c3c3STheodore Ts'o 	 * this file's inode.  Constant across the lifetime of the inode,
1002a627b0a7SEric Whitney 	 * it is used for making block allocation decisions - we try to
1003d444c3c3STheodore Ts'o 	 * place a file's data blocks near its inode block, and new inodes
1004d444c3c3STheodore Ts'o 	 * near to their parent directory's inode.
1005d444c3c3STheodore Ts'o 	 */
1006d444c3c3STheodore Ts'o 	ext4_group_t	i_block_group;
10078a2005d3STheodore Ts'o 	ext4_lblk_t	i_dir_start_lookup;
1008353eb83cSTheodore Ts'o #if (BITS_PER_LONG < 64)
100919f5fb7aSTheodore Ts'o 	unsigned long	i_state_flags;		/* Dynamic state flags */
1010353eb83cSTheodore Ts'o #endif
101112e9b892SDmitry Monakhov 	unsigned long	i_flags;
1012d444c3c3STheodore Ts'o 
1013d444c3c3STheodore Ts'o 	/*
1014d444c3c3STheodore Ts'o 	 * Extended attributes can be read independently of the main file
1015d444c3c3STheodore Ts'o 	 * data. Taking i_mutex even when reading would cause contention
1016d444c3c3STheodore Ts'o 	 * between readers of EAs and writers of regular file data, so
1017d444c3c3STheodore Ts'o 	 * instead we synchronize on xattr_sem when reading or changing
1018d444c3c3STheodore Ts'o 	 * EAs.
1019d444c3c3STheodore Ts'o 	 */
1020d444c3c3STheodore Ts'o 	struct rw_semaphore xattr_sem;
1021d444c3c3STheodore Ts'o 
1022d444c3c3STheodore Ts'o 	struct list_head i_orphan;	/* unlinked but open inodes */
1023d444c3c3STheodore Ts'o 
1024aa75f4d3SHarshad Shirwadkar 	/* Fast commit related info */
1025aa75f4d3SHarshad Shirwadkar 
1026aa75f4d3SHarshad Shirwadkar 	struct list_head i_fc_list;	/*
1027aa75f4d3SHarshad Shirwadkar 					 * inodes that need fast commit
1028aa75f4d3SHarshad Shirwadkar 					 * protected by sbi->s_fc_lock.
1029aa75f4d3SHarshad Shirwadkar 					 */
1030aa75f4d3SHarshad Shirwadkar 
1031aa75f4d3SHarshad Shirwadkar 	/* Fast commit subtid when this inode was committed */
1032aa75f4d3SHarshad Shirwadkar 	unsigned int i_fc_committed_subtid;
1033aa75f4d3SHarshad Shirwadkar 
1034aa75f4d3SHarshad Shirwadkar 	/* Start of lblk range that needs to be committed in this fast commit */
1035aa75f4d3SHarshad Shirwadkar 	ext4_lblk_t i_fc_lblk_start;
1036aa75f4d3SHarshad Shirwadkar 
1037aa75f4d3SHarshad Shirwadkar 	/* End of lblk range that needs to be committed in this fast commit */
1038aa75f4d3SHarshad Shirwadkar 	ext4_lblk_t i_fc_lblk_len;
1039aa75f4d3SHarshad Shirwadkar 
1040aa75f4d3SHarshad Shirwadkar 	/* Number of ongoing updates on this inode */
1041aa75f4d3SHarshad Shirwadkar 	atomic_t  i_fc_updates;
1042aa75f4d3SHarshad Shirwadkar 
1043aa75f4d3SHarshad Shirwadkar 	/* Fast commit wait queue for this inode */
1044aa75f4d3SHarshad Shirwadkar 	wait_queue_head_t i_fc_wait;
1045aa75f4d3SHarshad Shirwadkar 
1046aa75f4d3SHarshad Shirwadkar 	/* Protect concurrent accesses on i_fc_lblk_start, i_fc_lblk_len */
1047aa75f4d3SHarshad Shirwadkar 	struct mutex i_fc_lock;
1048aa75f4d3SHarshad Shirwadkar 
1049d444c3c3STheodore Ts'o 	/*
1050d444c3c3STheodore Ts'o 	 * i_disksize keeps track of what the inode size is ON DISK, not
1051d444c3c3STheodore Ts'o 	 * in memory.  During truncate, i_size is set to the new size by
1052d444c3c3STheodore Ts'o 	 * the VFS prior to calling ext4_truncate(), but the filesystem won't
1053d444c3c3STheodore Ts'o 	 * set i_disksize to 0 until the truncate is actually under way.
1054d444c3c3STheodore Ts'o 	 *
1055d444c3c3STheodore Ts'o 	 * The intent is that i_disksize always represents the blocks which
1056d444c3c3STheodore Ts'o 	 * are used by this file.  This allows recovery to restart truncate
1057d444c3c3STheodore Ts'o 	 * on orphans if we crash during truncate.  We actually write i_disksize
1058d444c3c3STheodore Ts'o 	 * into the on-disk inode when writing inodes out, instead of i_size.
1059d444c3c3STheodore Ts'o 	 *
1060d444c3c3STheodore Ts'o 	 * The only time when i_disksize and i_size may be different is when
1061d444c3c3STheodore Ts'o 	 * a truncate is in progress.  The only things which change i_disksize
1062d444c3c3STheodore Ts'o 	 * are ext4_get_block (growth) and ext4_truncate (shrinkth).
1063d444c3c3STheodore Ts'o 	 */
1064d444c3c3STheodore Ts'o 	loff_t	i_disksize;
1065d444c3c3STheodore Ts'o 
1066d444c3c3STheodore Ts'o 	/*
1067d444c3c3STheodore Ts'o 	 * i_data_sem is for serialising ext4_truncate() against
1068d444c3c3STheodore Ts'o 	 * ext4_getblock().  In the 2.4 ext2 design, great chunks of inode's
1069d444c3c3STheodore Ts'o 	 * data tree are chopped off during truncate. We can't do that in
1070d444c3c3STheodore Ts'o 	 * ext4 because whenever we perform intermediate commits during
1071d444c3c3STheodore Ts'o 	 * truncate, the inode and all the metadata blocks *must* be in a
1072d444c3c3STheodore Ts'o 	 * consistent state which allows truncation of the orphans to restart
1073d444c3c3STheodore Ts'o 	 * during recovery.  Hence we must fix the get_block-vs-truncate race
1074d444c3c3STheodore Ts'o 	 * by other means, so we have i_data_sem.
1075d444c3c3STheodore Ts'o 	 */
1076d444c3c3STheodore Ts'o 	struct rw_semaphore i_data_sem;
1077ea3d7209SJan Kara 	/*
1078ea3d7209SJan Kara 	 * i_mmap_sem is for serializing page faults with truncate / punch hole
1079ea3d7209SJan Kara 	 * operations. We have to make sure that new page cannot be faulted in
1080ea3d7209SJan Kara 	 * a section of the inode that is being punched. We cannot easily use
1081ea3d7209SJan Kara 	 * i_data_sem for this since we need protection for the whole punch
1082ea3d7209SJan Kara 	 * operation and i_data_sem ranks below transaction start so we have
1083ea3d7209SJan Kara 	 * to occasionally drop it.
1084ea3d7209SJan Kara 	 */
1085ea3d7209SJan Kara 	struct rw_semaphore i_mmap_sem;
1086d444c3c3STheodore Ts'o 	struct inode vfs_inode;
10878aefcd55STheodore Ts'o 	struct jbd2_inode *jinode;
1088d444c3c3STheodore Ts'o 
1089202ee5dfSTheodore Ts'o 	spinlock_t i_raw_lock;	/* protects updates to the raw inode */
1090202ee5dfSTheodore Ts'o 
1091d444c3c3STheodore Ts'o 	/*
1092d444c3c3STheodore Ts'o 	 * File creation time. Its function is same as that of
10937b62b293SArnd Bergmann 	 * struct timespec64 i_{a,c,m}time in the generic inode.
1094d444c3c3STheodore Ts'o 	 */
10957b62b293SArnd Bergmann 	struct timespec64 i_crtime;
1096d444c3c3STheodore Ts'o 
1097d444c3c3STheodore Ts'o 	/* mballoc */
109827bc446eSbrookxu 	atomic_t i_prealloc_active;
1099d444c3c3STheodore Ts'o 	struct list_head i_prealloc_list;
1100d444c3c3STheodore Ts'o 	spinlock_t i_prealloc_lock;
1101d444c3c3STheodore Ts'o 
1102c0677e6dSZheng Liu 	/* extents status tree */
1103c0677e6dSZheng Liu 	struct ext4_es_tree i_es_tree;
1104c0677e6dSZheng Liu 	rwlock_t i_es_lock;
1105edaa53caSZheng Liu 	struct list_head i_es_list;
1106eb68d0e2SZheng Liu 	unsigned int i_es_all_nr;	/* protected by i_es_lock */
1107edaa53caSZheng Liu 	unsigned int i_es_shk_nr;	/* protected by i_es_lock */
1108dd475925SJan Kara 	ext4_lblk_t i_es_shrink_lblk;	/* Offset where we start searching for
1109dd475925SJan Kara 					   extents to shrink. Protected by
1110dd475925SJan Kara 					   i_es_lock  */
1111c0677e6dSZheng Liu 
1112d444c3c3STheodore Ts'o 	/* ialloc */
1113d444c3c3STheodore Ts'o 	ext4_group_t	i_last_alloc_group;
1114d444c3c3STheodore Ts'o 
1115d444c3c3STheodore Ts'o 	/* allocation reservation info for delalloc */
1116a627b0a7SEric Whitney 	/* In case of bigalloc, this refer to clusters rather than blocks */
1117d444c3c3STheodore Ts'o 	unsigned int i_reserved_data_blocks;
1118d444c3c3STheodore Ts'o 
11191dc0aa46SEric Whitney 	/* pending cluster reservations for bigalloc file systems */
11201dc0aa46SEric Whitney 	struct ext4_pending_tree i_pending_tree;
11211dc0aa46SEric Whitney 
1122d444c3c3STheodore Ts'o 	/* on-disk additional length */
1123d444c3c3STheodore Ts'o 	__u16 i_extra_isize;
1124d444c3c3STheodore Ts'o 
112567cf5b09STao Ma 	/* Indicate the inline data space. */
112667cf5b09STao Ma 	u16 i_inline_off;
112767cf5b09STao Ma 	u16 i_inline_size;
112867cf5b09STao Ma 
1129a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA
1130a9e7f447SDmitry Monakhov 	/* quota space reservation, managed internally by quota code */
1131a9e7f447SDmitry Monakhov 	qsize_t i_reserved_quota;
1132a9e7f447SDmitry Monakhov #endif
11338d5d02e6SMingming Cao 
11342e8fa54eSJan Kara 	/* Lock protecting lists below */
1135744692dcSJiaying Zhang 	spinlock_t i_completed_io_lock;
11362e8fa54eSJan Kara 	/*
11372e8fa54eSJan Kara 	 * Completed IOs that need unwritten extents handling and have
11382e8fa54eSJan Kara 	 * transaction reserved
11392e8fa54eSJan Kara 	 */
11402e8fa54eSJan Kara 	struct list_head i_rsv_conversion_list;
11412e8fa54eSJan Kara 	struct work_struct i_rsv_conversion_work;
1142600be30aSJan Kara 	atomic_t i_unwritten; /* Nr. of inflight conversions pending */
11438a2005d3STheodore Ts'o 
11448a2005d3STheodore Ts'o 	spinlock_t i_block_reservation_lock;
1145b436b9beSJan Kara 
1146b436b9beSJan Kara 	/*
1147b436b9beSJan Kara 	 * Transactions that contain inode's metadata needed to complete
1148b436b9beSJan Kara 	 * fsync and fdatasync, respectively.
1149b436b9beSJan Kara 	 */
1150b436b9beSJan Kara 	tid_t i_sync_tid;
1151b436b9beSJan Kara 	tid_t i_datasync_tid;
1152814525f4SDarrick J. Wong 
115396c7e0d9SJan Kara #ifdef CONFIG_QUOTA
115496c7e0d9SJan Kara 	struct dquot *i_dquot[MAXQUOTAS];
115596c7e0d9SJan Kara #endif
115696c7e0d9SJan Kara 
1157814525f4SDarrick J. Wong 	/* Precomputed uuid+inum+igen checksum for seeding inode checksums */
1158814525f4SDarrick J. Wong 	__u32 i_csum_seed;
1159b30ab0e0SMichael Halcrow 
1160040cb378SLi Xi 	kprojid_t i_projid;
1161d444c3c3STheodore Ts'o };
1162d444c3c3STheodore Ts'o 
1163d444c3c3STheodore Ts'o /*
11643dcf5451SChristoph Hellwig  * File system states
11653dcf5451SChristoph Hellwig  */
11663dcf5451SChristoph Hellwig #define	EXT4_VALID_FS			0x0001	/* Unmounted cleanly */
11673dcf5451SChristoph Hellwig #define	EXT4_ERROR_FS			0x0002	/* Errors detected */
11683dcf5451SChristoph Hellwig #define	EXT4_ORPHAN_FS			0x0004	/* Orphans being recovered */
11698016e29fSHarshad Shirwadkar #define EXT4_FC_REPLAY			0x0020	/* Fast commit replay ongoing */
11703dcf5451SChristoph Hellwig 
11713dcf5451SChristoph Hellwig /*
11723dcf5451SChristoph Hellwig  * Misc. filesystem flags
11733dcf5451SChristoph Hellwig  */
11743dcf5451SChristoph Hellwig #define EXT2_FLAGS_SIGNED_HASH		0x0001  /* Signed dirhash in use */
11753dcf5451SChristoph Hellwig #define EXT2_FLAGS_UNSIGNED_HASH	0x0002  /* Unsigned dirhash in use */
11763dcf5451SChristoph Hellwig #define EXT2_FLAGS_TEST_FILESYS		0x0004	/* to test development code */
11773dcf5451SChristoph Hellwig 
11783dcf5451SChristoph Hellwig /*
117968911009SLukas Czerner  * Mount flags set via mount options or defaults
11803dcf5451SChristoph Hellwig  */
1181cdb7ee4cSTahsin Erdogan #define EXT4_MOUNT_NO_MBCACHE		0x00001 /* Do not use mbcache */
11823dcf5451SChristoph Hellwig #define EXT4_MOUNT_GRPID		0x00004	/* Create files with directory's group */
11833dcf5451SChristoph Hellwig #define EXT4_MOUNT_DEBUG		0x00008	/* Some debugging messages */
11843dcf5451SChristoph Hellwig #define EXT4_MOUNT_ERRORS_CONT		0x00010	/* Continue on errors */
11853dcf5451SChristoph Hellwig #define EXT4_MOUNT_ERRORS_RO		0x00020	/* Remount fs ro on errors */
11863dcf5451SChristoph Hellwig #define EXT4_MOUNT_ERRORS_PANIC		0x00040	/* Panic on errors */
118739ef17f1STheodore Ts'o #define EXT4_MOUNT_ERRORS_MASK		0x00070
11883dcf5451SChristoph Hellwig #define EXT4_MOUNT_MINIX_DF		0x00080	/* Mimics the Minix statfs */
11893dcf5451SChristoph Hellwig #define EXT4_MOUNT_NOLOAD		0x00100	/* Don't use existing journal*/
1190923ae0ffSRoss Zwisler #ifdef CONFIG_FS_DAX
1191fc626fe3SIra Weiny #define EXT4_MOUNT_DAX_ALWAYS		0x00200	/* Direct Access */
1192923ae0ffSRoss Zwisler #else
1193fc626fe3SIra Weiny #define EXT4_MOUNT_DAX_ALWAYS		0
1194923ae0ffSRoss Zwisler #endif
11953dcf5451SChristoph Hellwig #define EXT4_MOUNT_DATA_FLAGS		0x00C00	/* Mode for data writes: */
11963dcf5451SChristoph Hellwig #define EXT4_MOUNT_JOURNAL_DATA		0x00400	/* Write data to journal */
11973dcf5451SChristoph Hellwig #define EXT4_MOUNT_ORDERED_DATA		0x00800	/* Flush data before commit */
11983dcf5451SChristoph Hellwig #define EXT4_MOUNT_WRITEBACK_DATA	0x00C00	/* No data ordering */
11993dcf5451SChristoph Hellwig #define EXT4_MOUNT_UPDATE_JOURNAL	0x01000	/* Update the journal format */
12003dcf5451SChristoph Hellwig #define EXT4_MOUNT_NO_UID32		0x02000  /* Disable 32-bit UIDs */
12013dcf5451SChristoph Hellwig #define EXT4_MOUNT_XATTR_USER		0x04000	/* Extended user attributes */
12023dcf5451SChristoph Hellwig #define EXT4_MOUNT_POSIX_ACL		0x08000	/* POSIX Access Control Lists */
1203afd4672dSTheodore Ts'o #define EXT4_MOUNT_NO_AUTO_DA_ALLOC	0x10000	/* No auto delalloc mapping */
12043dcf5451SChristoph Hellwig #define EXT4_MOUNT_BARRIER		0x20000 /* Use block barriers */
120549da9392SJan Kara #define EXT4_MOUNT_QUOTA		0x40000 /* Some quota option set */
120649da9392SJan Kara #define EXT4_MOUNT_USRQUOTA		0x80000 /* "old" user quota,
120749da9392SJan Kara 						 * enable enforcement for hidden
120849da9392SJan Kara 						 * quota files */
120949da9392SJan Kara #define EXT4_MOUNT_GRPQUOTA		0x100000 /* "old" group quota, enable
121049da9392SJan Kara 						  * enforcement for hidden quota
121149da9392SJan Kara 						  * files */
121249da9392SJan Kara #define EXT4_MOUNT_PRJQUOTA		0x200000 /* Enable project quota
121349da9392SJan Kara 						  * enforcement */
1214744692dcSJiaying Zhang #define EXT4_MOUNT_DIOREAD_NOLOCK	0x400000 /* Enable support for dio read nolocking */
1215d4da6c9cSLinus Torvalds #define EXT4_MOUNT_JOURNAL_CHECKSUM	0x800000 /* Journal checksums */
12163dcf5451SChristoph Hellwig #define EXT4_MOUNT_JOURNAL_ASYNC_COMMIT	0x1000000 /* Journal Async Commit */
1217327eaf73STheodore Ts'o #define EXT4_MOUNT_WARN_ON_ERROR	0x2000000 /* Trigger WARN_ON on error */
12183d392b26STheodore Ts'o #define EXT4_MOUNT_PREFETCH_BLOCK_BITMAPS 0x4000000
121964769240SAlex Tomas #define EXT4_MOUNT_DELALLOC		0x8000000 /* Delalloc support */
12205bf5683aSHidehiro Kawai #define EXT4_MOUNT_DATA_ERR_ABORT	0x10000000 /* Abort on file data write */
12216fd058f7STheodore Ts'o #define EXT4_MOUNT_BLOCK_VALIDITY	0x20000000 /* Block validity checking */
12225328e635SEric Sandeen #define EXT4_MOUNT_DISCARD		0x40000000 /* Issue DISCARD requests */
1223bfff6873SLukas Czerner #define EXT4_MOUNT_INIT_INODE_TABLE	0x80000000 /* Initialize uninitialized itables */
12245bf5683aSHidehiro Kawai 
122568911009SLukas Czerner /*
122668911009SLukas Czerner  * Mount flags set either automatically (could not be set by mount option)
122768911009SLukas Czerner  * based on per file system feature or property or in special cases such as
122868911009SLukas Czerner  * distinguishing between explicit mount option definition and default.
122968911009SLukas Czerner  */
123056889787STheodore Ts'o #define EXT4_MOUNT2_EXPLICIT_DELALLOC	0x00000001 /* User explicitly
123156889787STheodore Ts'o 						      specified delalloc */
123268911009SLukas Czerner #define EXT4_MOUNT2_STD_GROUP_SIZE	0x00000002 /* We have standard group
123368911009SLukas Czerner 						      size of blocksize * 8
123468911009SLukas Czerner 						      blocks */
1235ed3654ebSTheodore Ts'o #define EXT4_MOUNT2_HURD_COMPAT		0x00000004 /* Support HURD-castrated
1236ed3654ebSTheodore Ts'o 						      file systems */
12379cb20f94SIra Weiny #define EXT4_MOUNT2_DAX_NEVER		0x00000008 /* Do not allow Direct Access */
12389cb20f94SIra Weiny #define EXT4_MOUNT2_DAX_INODE		0x00000010 /* For printing options only */
123956889787STheodore Ts'o 
12401e381f60SDmitry Monakhov #define EXT4_MOUNT2_EXPLICIT_JOURNAL_CHECKSUM	0x00000008 /* User explicitly
12411e381f60SDmitry Monakhov 						specified journal checksum */
12421e381f60SDmitry Monakhov 
1243995a3ed6SHarshad Shirwadkar #define EXT4_MOUNT2_JOURNAL_FAST_COMMIT	0x00000010 /* Journal fast commit */
1244995a3ed6SHarshad Shirwadkar 
1245fd8c37ecSTheodore Ts'o #define clear_opt(sb, opt)		EXT4_SB(sb)->s_mount_opt &= \
1246fd8c37ecSTheodore Ts'o 						~EXT4_MOUNT_##opt
1247fd8c37ecSTheodore Ts'o #define set_opt(sb, opt)		EXT4_SB(sb)->s_mount_opt |= \
1248fd8c37ecSTheodore Ts'o 						EXT4_MOUNT_##opt
12493dcf5451SChristoph Hellwig #define test_opt(sb, opt)		(EXT4_SB(sb)->s_mount_opt & \
12503dcf5451SChristoph Hellwig 					 EXT4_MOUNT_##opt)
12513dcf5451SChristoph Hellwig 
1252a2595b8aSTheodore Ts'o #define clear_opt2(sb, opt)		EXT4_SB(sb)->s_mount_opt2 &= \
1253a2595b8aSTheodore Ts'o 						~EXT4_MOUNT2_##opt
1254a2595b8aSTheodore Ts'o #define set_opt2(sb, opt)		EXT4_SB(sb)->s_mount_opt2 |= \
1255a2595b8aSTheodore Ts'o 						EXT4_MOUNT2_##opt
1256a2595b8aSTheodore Ts'o #define test_opt2(sb, opt)		(EXT4_SB(sb)->s_mount_opt2 & \
1257a2595b8aSTheodore Ts'o 					 EXT4_MOUNT2_##opt)
1258a2595b8aSTheodore Ts'o 
1259597d508cSAkinobu Mita #define ext4_test_and_set_bit		__test_and_set_bit_le
1260597d508cSAkinobu Mita #define ext4_set_bit			__set_bit_le
12613dcf5451SChristoph Hellwig #define ext4_set_bit_atomic		ext2_set_bit_atomic
1262597d508cSAkinobu Mita #define ext4_test_and_clear_bit		__test_and_clear_bit_le
1263597d508cSAkinobu Mita #define ext4_clear_bit			__clear_bit_le
12643dcf5451SChristoph Hellwig #define ext4_clear_bit_atomic		ext2_clear_bit_atomic
126550e0168cSAkinobu Mita #define ext4_test_bit			test_bit_le
126650e0168cSAkinobu Mita #define ext4_find_next_zero_bit		find_next_zero_bit_le
126750e0168cSAkinobu Mita #define ext4_find_next_bit		find_next_bit_le
12683dcf5451SChristoph Hellwig 
1269c3e94d1dSYongqiang Yang extern void ext4_set_bits(void *bm, int cur, int len);
1270c3e94d1dSYongqiang Yang 
12713dcf5451SChristoph Hellwig /*
12723dcf5451SChristoph Hellwig  * Maximal mount counts between two filesystem checks
12733dcf5451SChristoph Hellwig  */
12743dcf5451SChristoph Hellwig #define EXT4_DFL_MAX_MNT_COUNT		20	/* Allow 20 mounts */
12753dcf5451SChristoph Hellwig #define EXT4_DFL_CHECKINTERVAL		0	/* Don't use interval check */
12763dcf5451SChristoph Hellwig 
12773dcf5451SChristoph Hellwig /*
12783dcf5451SChristoph Hellwig  * Behaviour when detecting errors
12793dcf5451SChristoph Hellwig  */
12803dcf5451SChristoph Hellwig #define EXT4_ERRORS_CONTINUE		1	/* Continue execution */
12813dcf5451SChristoph Hellwig #define EXT4_ERRORS_RO			2	/* Remount fs read-only */
12823dcf5451SChristoph Hellwig #define EXT4_ERRORS_PANIC		3	/* Panic */
12833dcf5451SChristoph Hellwig #define EXT4_ERRORS_DEFAULT		EXT4_ERRORS_CONTINUE
12843dcf5451SChristoph Hellwig 
1285e6153918SDarrick J. Wong /* Metadata checksum algorithm codes */
1286e6153918SDarrick J. Wong #define EXT4_CRC32C_CHKSUM		1
1287e6153918SDarrick J. Wong 
12883dcf5451SChristoph Hellwig /*
12893dcf5451SChristoph Hellwig  * Structure of the super block
12903dcf5451SChristoph Hellwig  */
12913dcf5451SChristoph Hellwig struct ext4_super_block {
12923dcf5451SChristoph Hellwig /*00*/	__le32	s_inodes_count;		/* Inodes count */
12933dcf5451SChristoph Hellwig 	__le32	s_blocks_count_lo;	/* Blocks count */
12943dcf5451SChristoph Hellwig 	__le32	s_r_blocks_count_lo;	/* Reserved blocks count */
12953dcf5451SChristoph Hellwig 	__le32	s_free_blocks_count_lo;	/* Free blocks count */
12963dcf5451SChristoph Hellwig /*10*/	__le32	s_free_inodes_count;	/* Free inodes count */
12973dcf5451SChristoph Hellwig 	__le32	s_first_data_block;	/* First Data Block */
12983dcf5451SChristoph Hellwig 	__le32	s_log_block_size;	/* Block size */
1299281b5995STheodore Ts'o 	__le32	s_log_cluster_size;	/* Allocation cluster size */
13003dcf5451SChristoph Hellwig /*20*/	__le32	s_blocks_per_group;	/* # Blocks per group */
1301281b5995STheodore Ts'o 	__le32	s_clusters_per_group;	/* # Clusters per group */
13023dcf5451SChristoph Hellwig 	__le32	s_inodes_per_group;	/* # Inodes per group */
13033dcf5451SChristoph Hellwig 	__le32	s_mtime;		/* Mount time */
13043dcf5451SChristoph Hellwig /*30*/	__le32	s_wtime;		/* Write time */
13053dcf5451SChristoph Hellwig 	__le16	s_mnt_count;		/* Mount count */
13063dcf5451SChristoph Hellwig 	__le16	s_max_mnt_count;	/* Maximal mount count */
13073dcf5451SChristoph Hellwig 	__le16	s_magic;		/* Magic signature */
13083dcf5451SChristoph Hellwig 	__le16	s_state;		/* File system state */
13093dcf5451SChristoph Hellwig 	__le16	s_errors;		/* Behaviour when detecting errors */
13103dcf5451SChristoph Hellwig 	__le16	s_minor_rev_level;	/* minor revision level */
13113dcf5451SChristoph Hellwig /*40*/	__le32	s_lastcheck;		/* time of last check */
13123dcf5451SChristoph Hellwig 	__le32	s_checkinterval;	/* max. time between checks */
13133dcf5451SChristoph Hellwig 	__le32	s_creator_os;		/* OS */
13143dcf5451SChristoph Hellwig 	__le32	s_rev_level;		/* Revision level */
13153dcf5451SChristoph Hellwig /*50*/	__le16	s_def_resuid;		/* Default uid for reserved blocks */
13163dcf5451SChristoph Hellwig 	__le16	s_def_resgid;		/* Default gid for reserved blocks */
13173dcf5451SChristoph Hellwig 	/*
13183dcf5451SChristoph Hellwig 	 * These fields are for EXT4_DYNAMIC_REV superblocks only.
13193dcf5451SChristoph Hellwig 	 *
13203dcf5451SChristoph Hellwig 	 * Note: the difference between the compatible feature set and
13213dcf5451SChristoph Hellwig 	 * the incompatible feature set is that if there is a bit set
13223dcf5451SChristoph Hellwig 	 * in the incompatible feature set that the kernel doesn't
13233dcf5451SChristoph Hellwig 	 * know about, it should refuse to mount the filesystem.
13243dcf5451SChristoph Hellwig 	 *
13253dcf5451SChristoph Hellwig 	 * e2fsck's requirements are more strict; if it doesn't know
13263dcf5451SChristoph Hellwig 	 * about a feature in either the compatible or incompatible
13273dcf5451SChristoph Hellwig 	 * feature set, it must abort and not try to meddle with
13283dcf5451SChristoph Hellwig 	 * things it doesn't understand...
13293dcf5451SChristoph Hellwig 	 */
13303dcf5451SChristoph Hellwig 	__le32	s_first_ino;		/* First non-reserved inode */
13313dcf5451SChristoph Hellwig 	__le16  s_inode_size;		/* size of inode structure */
13323dcf5451SChristoph Hellwig 	__le16	s_block_group_nr;	/* block group # of this superblock */
13333dcf5451SChristoph Hellwig 	__le32	s_feature_compat;	/* compatible feature set */
13343dcf5451SChristoph Hellwig /*60*/	__le32	s_feature_incompat;	/* incompatible feature set */
13353dcf5451SChristoph Hellwig 	__le32	s_feature_ro_compat;	/* readonly-compatible feature set */
13363dcf5451SChristoph Hellwig /*68*/	__u8	s_uuid[16];		/* 128-bit uuid for volume */
13373dcf5451SChristoph Hellwig /*78*/	char	s_volume_name[16];	/* volume name */
1338072ebb3bSTheodore Ts'o /*88*/	char	s_last_mounted[64] __nonstring;	/* directory where last mounted */
13393dcf5451SChristoph Hellwig /*C8*/	__le32	s_algorithm_usage_bitmap; /* For compression */
13403dcf5451SChristoph Hellwig 	/*
13413dcf5451SChristoph Hellwig 	 * Performance hints.  Directory preallocation should only
13423dcf5451SChristoph Hellwig 	 * happen if the EXT4_FEATURE_COMPAT_DIR_PREALLOC flag is on.
13433dcf5451SChristoph Hellwig 	 */
13443dcf5451SChristoph Hellwig 	__u8	s_prealloc_blocks;	/* Nr of blocks to try to preallocate*/
13453dcf5451SChristoph Hellwig 	__u8	s_prealloc_dir_blocks;	/* Nr to preallocate for dirs */
13463dcf5451SChristoph Hellwig 	__le16	s_reserved_gdt_blocks;	/* Per group desc for online growth */
13473dcf5451SChristoph Hellwig 	/*
13483dcf5451SChristoph Hellwig 	 * Journaling support valid if EXT4_FEATURE_COMPAT_HAS_JOURNAL set.
13493dcf5451SChristoph Hellwig 	 */
13503dcf5451SChristoph Hellwig /*D0*/	__u8	s_journal_uuid[16];	/* uuid of journal superblock */
13513dcf5451SChristoph Hellwig /*E0*/	__le32	s_journal_inum;		/* inode number of journal file */
13523dcf5451SChristoph Hellwig 	__le32	s_journal_dev;		/* device number of journal file */
13533dcf5451SChristoph Hellwig 	__le32	s_last_orphan;		/* start of list of inodes to delete */
13543dcf5451SChristoph Hellwig 	__le32	s_hash_seed[4];		/* HTREE hash seed */
13553dcf5451SChristoph Hellwig 	__u8	s_def_hash_version;	/* Default hash version to use */
135689eeddf0STheodore Ts'o 	__u8	s_jnl_backup_type;
13573dcf5451SChristoph Hellwig 	__le16  s_desc_size;		/* size of group descriptor */
13583dcf5451SChristoph Hellwig /*100*/	__le32	s_default_mount_opts;
13593dcf5451SChristoph Hellwig 	__le32	s_first_meta_bg;	/* First metablock block group */
13603dcf5451SChristoph Hellwig 	__le32	s_mkfs_time;		/* When the filesystem was created */
13613dcf5451SChristoph Hellwig 	__le32	s_jnl_blocks[17];	/* Backup of the journal inode */
13623dcf5451SChristoph Hellwig 	/* 64bit support valid if EXT4_FEATURE_COMPAT_64BIT */
13633dcf5451SChristoph Hellwig /*150*/	__le32	s_blocks_count_hi;	/* Blocks count */
13643dcf5451SChristoph Hellwig 	__le32	s_r_blocks_count_hi;	/* Reserved blocks count */
13653dcf5451SChristoph Hellwig 	__le32	s_free_blocks_count_hi;	/* Free blocks count */
13663dcf5451SChristoph Hellwig 	__le16	s_min_extra_isize;	/* All inodes have at least # bytes */
13673dcf5451SChristoph Hellwig 	__le16	s_want_extra_isize; 	/* New inodes should reserve # bytes */
13683dcf5451SChristoph Hellwig 	__le32	s_flags;		/* Miscellaneous flags */
13693dcf5451SChristoph Hellwig 	__le16  s_raid_stride;		/* RAID stride */
1370c5e06d10SJohann Lombardi 	__le16  s_mmp_update_interval;  /* # seconds to wait in MMP checking */
13713dcf5451SChristoph Hellwig 	__le64  s_mmp_block;            /* Block for multi-mount protection */
13723dcf5451SChristoph Hellwig 	__le32  s_raid_stripe_width;    /* blocks on all data disks (N*stride)*/
1373772cb7c8SJose R. Santos 	__u8	s_log_groups_per_flex;  /* FLEX_BG group size */
1374e6153918SDarrick J. Wong 	__u8	s_checksum_type;	/* metadata checksum algorithm used */
1375f542fbe8STheodore Ts'o 	__u8	s_encryption_level;	/* versioning level for encryption */
1376f542fbe8STheodore Ts'o 	__u8	s_reserved_pad;		/* Padding to next 32bits */
1377afc32f7eSTheodore Ts'o 	__le64	s_kbytes_written;	/* nr of lifetime kilobytes written */
13784a9cdec7STheodore Ts'o 	__le32	s_snapshot_inum;	/* Inode number of active snapshot */
13794a9cdec7STheodore Ts'o 	__le32	s_snapshot_id;		/* sequential ID of active snapshot */
13804a9cdec7STheodore Ts'o 	__le64	s_snapshot_r_blocks_count; /* reserved blocks for active
13814a9cdec7STheodore Ts'o 					      snapshot's future use */
13824a9cdec7STheodore Ts'o 	__le32	s_snapshot_list;	/* inode number of the head of the
13834a9cdec7STheodore Ts'o 					   on-disk snapshot list */
13841c13d5c0STheodore Ts'o #define EXT4_S_ERR_START offsetof(struct ext4_super_block, s_error_count)
13851c13d5c0STheodore Ts'o 	__le32	s_error_count;		/* number of fs errors */
13861c13d5c0STheodore Ts'o 	__le32	s_first_error_time;	/* first time an error happened */
13871c13d5c0STheodore Ts'o 	__le32	s_first_error_ino;	/* inode involved in first error */
13881c13d5c0STheodore Ts'o 	__le64	s_first_error_block;	/* block involved of first error */
1389072ebb3bSTheodore Ts'o 	__u8	s_first_error_func[32] __nonstring;	/* function where the error happened */
13901c13d5c0STheodore Ts'o 	__le32	s_first_error_line;	/* line number where error happened */
13911c13d5c0STheodore Ts'o 	__le32	s_last_error_time;	/* most recent time of an error */
13921c13d5c0STheodore Ts'o 	__le32	s_last_error_ino;	/* inode involved in last error */
13931c13d5c0STheodore Ts'o 	__le32	s_last_error_line;	/* line number where error happened */
13941c13d5c0STheodore Ts'o 	__le64	s_last_error_block;	/* block involved of last error */
1395072ebb3bSTheodore Ts'o 	__u8	s_last_error_func[32] __nonstring;	/* function where the error happened */
13968b67f04aSTheodore Ts'o #define EXT4_S_ERR_END offsetof(struct ext4_super_block, s_mount_opts)
13978b67f04aSTheodore Ts'o 	__u8	s_mount_opts[64];
1398281b5995STheodore Ts'o 	__le32	s_usr_quota_inum;	/* inode for tracking user quota */
1399281b5995STheodore Ts'o 	__le32	s_grp_quota_inum;	/* inode for tracking group quota */
1400281b5995STheodore Ts'o 	__le32	s_overhead_clusters;	/* overhead blocks/clusters in fs */
14011beeef1bSDarrick J. Wong 	__le32	s_backup_bgs[2];	/* groups with sparse_super2 SBs */
14023edc18d8STheodore Ts'o 	__u8	s_encrypt_algos[4];	/* Encryption algorithms in use  */
1403f542fbe8STheodore Ts'o 	__u8	s_encrypt_pw_salt[16];	/* Salt used for string2key algorithm */
1404f542fbe8STheodore Ts'o 	__le32	s_lpf_ino;		/* Location of the lost+found inode */
14058b4953e1STheodore Ts'o 	__le32	s_prj_quota_inum;	/* inode for tracking project quota */
14068c81bd8fSDarrick J. Wong 	__le32	s_checksum_seed;	/* crc32c(uuid) if csum_seed set */
14076a0678a7SArnd Bergmann 	__u8	s_wtime_hi;
14086a0678a7SArnd Bergmann 	__u8	s_mtime_hi;
14096a0678a7SArnd Bergmann 	__u8	s_mkfs_time_hi;
14106a0678a7SArnd Bergmann 	__u8	s_lastcheck_hi;
14116a0678a7SArnd Bergmann 	__u8	s_first_error_time_hi;
14126a0678a7SArnd Bergmann 	__u8	s_last_error_time_hi;
1413878520acSTheodore Ts'o 	__u8	s_first_error_errcode;
1414878520acSTheodore Ts'o 	__u8    s_last_error_errcode;
1415c83ad55eSGabriel Krisman Bertazi 	__le16  s_encoding;		/* Filename charset encoding */
1416c83ad55eSGabriel Krisman Bertazi 	__le16  s_encoding_flags;	/* Filename charset encoding flags */
1417c83ad55eSGabriel Krisman Bertazi 	__le32	s_reserved[95];		/* Padding to the end of the block */
1418e6153918SDarrick J. Wong 	__le32	s_checksum;		/* crc32c(superblock) */
14193dcf5451SChristoph Hellwig };
14203dcf5451SChristoph Hellwig 
14211c13d5c0STheodore Ts'o #define EXT4_S_ERR_LEN (EXT4_S_ERR_END - EXT4_S_ERR_START)
14221c13d5c0STheodore Ts'o 
14233dcf5451SChristoph Hellwig #ifdef __KERNEL__
1424bc0b0d6dSTheodore Ts'o 
1425bc0b0d6dSTheodore Ts'o /*
14264ab2f15bSTheodore Ts'o  * run-time mount flags
1427bc0b0d6dSTheodore Ts'o  */
1428bc0b0d6dSTheodore Ts'o #define EXT4_MF_MNTDIR_SAMPLED		0x0001
14294ab2f15bSTheodore Ts'o #define EXT4_MF_FS_ABORTED		0x0002	/* Fatal error detected */
1430ababea77SHarshad Shirwadkar #define EXT4_MF_FC_INELIGIBLE		0x0004	/* Fast commit ineligible */
1431ababea77SHarshad Shirwadkar #define EXT4_MF_FC_COMMITTING		0x0008	/* File system underoing a fast
1432ababea77SHarshad Shirwadkar 						 * commit.
1433ababea77SHarshad Shirwadkar 						 */
14346ddb2447STheodore Ts'o 
1435643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
1436ac4acb1fSEric Biggers #define DUMMY_ENCRYPTION_ENABLED(sbi) ((sbi)->s_dummy_enc_policy.policy != NULL)
14376ddb2447STheodore Ts'o #else
14386ddb2447STheodore Ts'o #define DUMMY_ENCRYPTION_ENABLED(sbi) (0)
14396ddb2447STheodore Ts'o #endif
1440bc0b0d6dSTheodore Ts'o 
1441a2d4a646SJan Kara /* Number of quota types we support */
1442689c958cSLi Xi #define EXT4_MAXQUOTAS 3
1443a2d4a646SJan Kara 
1444c83ad55eSGabriel Krisman Bertazi #define EXT4_ENC_UTF8_12_1	1
1445c83ad55eSGabriel Krisman Bertazi 
1446c83ad55eSGabriel Krisman Bertazi /*
1447c83ad55eSGabriel Krisman Bertazi  * Flags for ext4_sb_info.s_encoding_flags.
1448c83ad55eSGabriel Krisman Bertazi  */
1449c83ad55eSGabriel Krisman Bertazi #define EXT4_ENC_STRICT_MODE_FL	(1 << 0)
1450c83ad55eSGabriel Krisman Bertazi 
1451c83ad55eSGabriel Krisman Bertazi #define ext4_has_strict_mode(sbi) \
1452c83ad55eSGabriel Krisman Bertazi 	(sbi->s_encoding_flags & EXT4_ENC_STRICT_MODE_FL)
1453c83ad55eSGabriel Krisman Bertazi 
1454ca0faba0STheodore Ts'o /*
1455ca0faba0STheodore Ts'o  * fourth extended-fs super-block data in memory
1456ca0faba0STheodore Ts'o  */
1457ca0faba0STheodore Ts'o struct ext4_sb_info {
1458ca0faba0STheodore Ts'o 	unsigned long s_desc_size;	/* Size of a group descriptor in bytes */
1459ca0faba0STheodore Ts'o 	unsigned long s_inodes_per_block;/* Number of inodes per block */
1460ca0faba0STheodore Ts'o 	unsigned long s_blocks_per_group;/* Number of blocks in a group */
1461281b5995STheodore Ts'o 	unsigned long s_clusters_per_group; /* Number of clusters in a group */
1462ca0faba0STheodore Ts'o 	unsigned long s_inodes_per_group;/* Number of inodes in a group */
1463ca0faba0STheodore Ts'o 	unsigned long s_itb_per_group;	/* Number of inode table blocks per group */
1464ca0faba0STheodore Ts'o 	unsigned long s_gdb_count;	/* Number of group descriptor blocks */
1465ca0faba0STheodore Ts'o 	unsigned long s_desc_per_block;	/* Number of group descriptors per block */
1466ca0faba0STheodore Ts'o 	ext4_group_t s_groups_count;	/* Number of groups in the fs */
1467fb0a387dSEric Sandeen 	ext4_group_t s_blockfile_groups;/* Groups acceptable for non-extent files */
1468952fc18eSTheodore Ts'o 	unsigned long s_overhead;  /* # of fs overhead clusters */
1469281b5995STheodore Ts'o 	unsigned int s_cluster_ratio;	/* Number of blocks per cluster */
1470281b5995STheodore Ts'o 	unsigned int s_cluster_bits;	/* log2 of s_cluster_ratio */
1471ca0faba0STheodore Ts'o 	loff_t s_bitmap_maxbytes;	/* max bytes for bitmap files */
1472ca0faba0STheodore Ts'o 	struct buffer_head * s_sbh;	/* Buffer containing the super block */
1473ca0faba0STheodore Ts'o 	struct ext4_super_block *s_es;	/* Pointer to the super block in the buffer */
14741d0c3924STheodore Ts'o 	struct buffer_head * __rcu *s_group_desc;
14757f4520ccSTheodore Ts'o 	unsigned int s_mount_opt;
1476a2595b8aSTheodore Ts'o 	unsigned int s_mount_opt2;
1477bc0b0d6dSTheodore Ts'o 	unsigned int s_mount_flags;
14785a916be1STheodore Ts'o 	unsigned int s_def_mount_opt;
1479ca0faba0STheodore Ts'o 	ext4_fsblk_t s_sb_block;
148027dd4385SLukas Czerner 	atomic64_t s_resv_clusters;
148108cefc7aSEric W. Biederman 	kuid_t s_resuid;
148208cefc7aSEric W. Biederman 	kgid_t s_resgid;
1483ca0faba0STheodore Ts'o 	unsigned short s_mount_state;
1484ca0faba0STheodore Ts'o 	unsigned short s_pad;
1485ca0faba0STheodore Ts'o 	int s_addr_per_block_bits;
1486ca0faba0STheodore Ts'o 	int s_desc_per_block_bits;
1487ca0faba0STheodore Ts'o 	int s_inode_size;
1488ca0faba0STheodore Ts'o 	int s_first_ino;
1489ca0faba0STheodore Ts'o 	unsigned int s_inode_readahead_blks;
149011013911SAndreas Dilger 	unsigned int s_inode_goal;
1491ca0faba0STheodore Ts'o 	u32 s_hash_seed[4];
1492ca0faba0STheodore Ts'o 	int s_def_hash_version;
1493ca0faba0STheodore Ts'o 	int s_hash_unsigned;	/* 3 if hash should be signed, 0 if not */
149457042651STheodore Ts'o 	struct percpu_counter s_freeclusters_counter;
1495ca0faba0STheodore Ts'o 	struct percpu_counter s_freeinodes_counter;
1496ca0faba0STheodore Ts'o 	struct percpu_counter s_dirs_counter;
149757042651STheodore Ts'o 	struct percpu_counter s_dirtyclusters_counter;
1498ca0faba0STheodore Ts'o 	struct blockgroup_lock *s_blockgroup_lock;
1499ca0faba0STheodore Ts'o 	struct proc_dir_entry *s_proc;
1500ca0faba0STheodore Ts'o 	struct kobject s_kobj;
1501ca0faba0STheodore Ts'o 	struct completion s_kobj_unregister;
15022c0544b2STheodore Ts'o 	struct super_block *s_sb;
1503c83ad55eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
1504c83ad55eSGabriel Krisman Bertazi 	struct unicode_map *s_encoding;
1505c83ad55eSGabriel Krisman Bertazi 	__u16 s_encoding_flags;
1506c83ad55eSGabriel Krisman Bertazi #endif
1507ca0faba0STheodore Ts'o 
1508ca0faba0STheodore Ts'o 	/* Journaling */
1509ca0faba0STheodore Ts'o 	struct journal_s *s_journal;
1510ca0faba0STheodore Ts'o 	struct list_head s_orphan;
1511ca0faba0STheodore Ts'o 	struct mutex s_orphan_lock;
15129549a168STheodore Ts'o 	unsigned long s_ext4_flags;		/* Ext4 superblock flags */
1513ca0faba0STheodore Ts'o 	unsigned long s_commit_interval;
1514ca0faba0STheodore Ts'o 	u32 s_max_batch_time;
1515ca0faba0STheodore Ts'o 	u32 s_min_batch_time;
1516ee7ed3aaSChunguang Xu 	struct block_device *s_journal_bdev;
1517ca0faba0STheodore Ts'o #ifdef CONFIG_QUOTA
151833458eabSTheodore Ts'o 	/* Names of quota files with journalled quota */
151933458eabSTheodore Ts'o 	char __rcu *s_qf_names[EXT4_MAXQUOTAS];
1520ca0faba0STheodore Ts'o 	int s_jquota_fmt;			/* Format of quota to use */
1521ca0faba0STheodore Ts'o #endif
1522ca0faba0STheodore Ts'o 	unsigned int s_want_extra_isize; /* New inodes should reserve # bytes */
1523dd0db94fSChunguang Xu 	struct ext4_system_blocks __rcu *s_system_blks;
1524ca0faba0STheodore Ts'o 
1525ca0faba0STheodore Ts'o #ifdef EXTENTS_STATS
1526ca0faba0STheodore Ts'o 	/* ext4 extents stats */
1527ca0faba0STheodore Ts'o 	unsigned long s_ext_min;
1528ca0faba0STheodore Ts'o 	unsigned long s_ext_max;
1529ca0faba0STheodore Ts'o 	unsigned long s_depth_max;
1530ca0faba0STheodore Ts'o 	spinlock_t s_ext_stats_lock;
1531ca0faba0STheodore Ts'o 	unsigned long s_ext_blocks;
1532ca0faba0STheodore Ts'o 	unsigned long s_ext_extents;
1533ca0faba0STheodore Ts'o #endif
1534ca0faba0STheodore Ts'o 
1535ca0faba0STheodore Ts'o 	/* for buddy allocator */
1536df3da4eaSSuraj Jitindar Singh 	struct ext4_group_info ** __rcu *s_group_info;
1537ca0faba0STheodore Ts'o 	struct inode *s_buddy_cache;
1538ca0faba0STheodore Ts'o 	spinlock_t s_md_lock;
1539ca0faba0STheodore Ts'o 	unsigned short *s_mb_offsets;
1540ca0faba0STheodore Ts'o 	unsigned int *s_mb_maxs;
154128623c2fSTheodore Ts'o 	unsigned int s_group_info_size;
1542d08854f5STheodore Ts'o 	unsigned int s_mb_free_pending;
1543a0154344SDaeho Jeong 	struct list_head s_freed_data_list;	/* List of blocks to be freed
1544a0154344SDaeho Jeong 						   after commit completed */
1545ca0faba0STheodore Ts'o 
1546ca0faba0STheodore Ts'o 	/* tunables */
1547ca0faba0STheodore Ts'o 	unsigned long s_stripe;
1548ca0faba0STheodore Ts'o 	unsigned int s_mb_stream_request;
1549ca0faba0STheodore Ts'o 	unsigned int s_mb_max_to_scan;
1550ca0faba0STheodore Ts'o 	unsigned int s_mb_min_to_scan;
1551ca0faba0STheodore Ts'o 	unsigned int s_mb_stats;
1552ca0faba0STheodore Ts'o 	unsigned int s_mb_order2_reqs;
1553ca0faba0STheodore Ts'o 	unsigned int s_mb_group_prealloc;
155427bc446eSbrookxu 	unsigned int s_mb_max_inode_prealloc;
1555df981d03STheodore Ts'o 	unsigned int s_max_dir_size_kb;
1556ca0faba0STheodore Ts'o 	/* where last allocation was done - for stream allocation */
1557ca0faba0STheodore Ts'o 	unsigned long s_mb_last_group;
1558ca0faba0STheodore Ts'o 	unsigned long s_mb_last_start;
1559cfd73237SAlex Zhuravlev 	unsigned int s_mb_prefetch;
1560cfd73237SAlex Zhuravlev 	unsigned int s_mb_prefetch_limit;
1561ca0faba0STheodore Ts'o 
1562ca0faba0STheodore Ts'o 	/* stats for buddy allocator */
1563ca0faba0STheodore Ts'o 	atomic_t s_bal_reqs;	/* number of reqs with len > 1 */
1564ca0faba0STheodore Ts'o 	atomic_t s_bal_success;	/* we found long enough chunks */
1565ca0faba0STheodore Ts'o 	atomic_t s_bal_allocated;	/* in blocks */
1566ca0faba0STheodore Ts'o 	atomic_t s_bal_ex_scanned;	/* total extents scanned */
1567ca0faba0STheodore Ts'o 	atomic_t s_bal_goals;	/* goal hits */
1568ca0faba0STheodore Ts'o 	atomic_t s_bal_breaks;	/* too long searches */
1569ca0faba0STheodore Ts'o 	atomic_t s_bal_2orders;	/* 2^order hits */
1570ca0faba0STheodore Ts'o 	spinlock_t s_bal_lock;
1571ca0faba0STheodore Ts'o 	unsigned long s_mb_buddies_generated;
1572ca0faba0STheodore Ts'o 	unsigned long long s_mb_generation_time;
1573ca0faba0STheodore Ts'o 	atomic_t s_mb_lost_chunks;
1574ca0faba0STheodore Ts'o 	atomic_t s_mb_preallocated;
1575ca0faba0STheodore Ts'o 	atomic_t s_mb_discarded;
157650797481STheodore Ts'o 	atomic_t s_lock_busy;
1577ca0faba0STheodore Ts'o 
1578ca0faba0STheodore Ts'o 	/* locality groups */
1579003cb608STejun Heo 	struct ext4_locality_group __percpu *s_locality_groups;
1580ca0faba0STheodore Ts'o 
1581ca0faba0STheodore Ts'o 	/* for write statistics */
1582ca0faba0STheodore Ts'o 	unsigned long s_sectors_written_start;
1583ca0faba0STheodore Ts'o 	u64 s_kbytes_written;
1584ca0faba0STheodore Ts'o 
158567a5da56SZheng Liu 	/* the size of zero-out chunk */
158667a5da56SZheng Liu 	unsigned int s_extent_max_zeroout_kb;
158767a5da56SZheng Liu 
1588ca0faba0STheodore Ts'o 	unsigned int s_log_groups_per_flex;
15897c990728SSuraj Jitindar Singh 	struct flex_groups * __rcu *s_flex_groups;
1590117fff10STheodore Ts'o 	ext4_group_t s_flex_groups_allocated;
15914c0425ffSMingming Cao 
15922e8fa54eSJan Kara 	/* workqueue for reserved extent conversions (buffered io) */
15932e8fa54eSJan Kara 	struct workqueue_struct *rsv_conversion_wq;
159466e61a9eSTheodore Ts'o 
159566e61a9eSTheodore Ts'o 	/* timer for periodic error stats printing */
159666e61a9eSTheodore Ts'o 	struct timer_list s_err_report;
1597bfff6873SLukas Czerner 
1598bfff6873SLukas Czerner 	/* Lazy inode table initialization info */
1599bfff6873SLukas Czerner 	struct ext4_li_request *s_li_request;
1600bfff6873SLukas Czerner 	/* Wait multiplier for lazy initialization thread */
1601bfff6873SLukas Czerner 	unsigned int s_li_wait_mult;
1602c5e06d10SJohann Lombardi 
1603c5e06d10SJohann Lombardi 	/* Kernel thread for multiple mount protection */
1604c5e06d10SJohann Lombardi 	struct task_struct *s_mmp_tsk;
16053d56b8d2STao Ma 
16063d56b8d2STao Ma 	/* record the last minlen when FITRIM is called. */
16073d56b8d2STao Ma 	atomic_t s_last_trim_minblks;
16080441984aSDarrick J. Wong 
16090441984aSDarrick J. Wong 	/* Reference to checksum algorithm driver via cryptoapi */
16100441984aSDarrick J. Wong 	struct crypto_shash *s_chksum_driver;
1611a9c47317SDarrick J. Wong 
1612a9c47317SDarrick J. Wong 	/* Precomputed FS UUID checksum for seeding other checksums */
1613a9c47317SDarrick J. Wong 	__u32 s_csum_seed;
161474cd15cdSZheng Liu 
161574cd15cdSZheng Liu 	/* Reclaim extents from extent status tree */
161674cd15cdSZheng Liu 	struct shrinker s_es_shrinker;
1617dd475925SJan Kara 	struct list_head s_es_list;	/* List of inodes with reclaimable extents */
1618edaa53caSZheng Liu 	long s_es_nr_inode;
1619eb68d0e2SZheng Liu 	struct ext4_es_stats s_es_stats;
162047387409STahsin Erdogan 	struct mb_cache *s_ea_block_cache;
1621dec214d0STahsin Erdogan 	struct mb_cache *s_ea_inode_cache;
1622edaa53caSZheng Liu 	spinlock_t s_es_lock ____cacheline_aligned_in_smp;
1623efbed4dcSTheodore Ts'o 
1624efbed4dcSTheodore Ts'o 	/* Ratelimit ext4 messages. */
1625efbed4dcSTheodore Ts'o 	struct ratelimit_state s_err_ratelimit_state;
1626efbed4dcSTheodore Ts'o 	struct ratelimit_state s_warning_ratelimit_state;
1627efbed4dcSTheodore Ts'o 	struct ratelimit_state s_msg_ratelimit_state;
16281cf006edSDmitry Monakhov 	atomic_t s_warning_count;
16291cf006edSDmitry Monakhov 	atomic_t s_msg_count;
1630c8585c6fSDaeho Jeong 
1631ac4acb1fSEric Biggers 	/* Encryption policy for '-o test_dummy_encryption' */
1632ac4acb1fSEric Biggers 	struct fscrypt_dummy_policy s_dummy_enc_policy;
1633ed318a6cSEric Biggers 
1634cb85f4d2SEric Biggers 	/*
1635cb85f4d2SEric Biggers 	 * Barrier between writepages ops and changing any inode's JOURNAL_DATA
1636cb85f4d2SEric Biggers 	 * or EXTENTS flag.
1637cb85f4d2SEric Biggers 	 */
1638bbd55937SEric Biggers 	struct percpu_rw_semaphore s_writepages_rwsem;
16395e405595SDan Williams 	struct dax_device *s_daxdev;
164046f870d6STheodore Ts'o #ifdef CONFIG_EXT4_DEBUG
164146f870d6STheodore Ts'o 	unsigned long s_simulate_fail;
164246f870d6STheodore Ts'o #endif
1643bc71726cSzhangyi (F) 	/* Record the errseq of the backing block device */
1644bc71726cSzhangyi (F) 	errseq_t s_bdev_wb_err;
1645bc71726cSzhangyi (F) 	spinlock_t s_bdev_wb_lock;
1646aa75f4d3SHarshad Shirwadkar 
1647aa75f4d3SHarshad Shirwadkar 	/* Ext4 fast commit stuff */
1648aa75f4d3SHarshad Shirwadkar 	atomic_t s_fc_subtid;
1649aa75f4d3SHarshad Shirwadkar 	atomic_t s_fc_ineligible_updates;
1650aa75f4d3SHarshad Shirwadkar 	/*
1651aa75f4d3SHarshad Shirwadkar 	 * After commit starts, the main queue gets locked, and the further
1652aa75f4d3SHarshad Shirwadkar 	 * updates get added in the staging queue.
1653aa75f4d3SHarshad Shirwadkar 	 */
1654aa75f4d3SHarshad Shirwadkar #define FC_Q_MAIN	0
1655aa75f4d3SHarshad Shirwadkar #define FC_Q_STAGING	1
1656aa75f4d3SHarshad Shirwadkar 	struct list_head s_fc_q[2];	/* Inodes staged for fast commit
1657aa75f4d3SHarshad Shirwadkar 					 * that have data changes in them.
1658aa75f4d3SHarshad Shirwadkar 					 */
1659aa75f4d3SHarshad Shirwadkar 	struct list_head s_fc_dentry_q[2];	/* directory entry updates */
1660aa75f4d3SHarshad Shirwadkar 	unsigned int s_fc_bytes;
1661aa75f4d3SHarshad Shirwadkar 	/*
1662aa75f4d3SHarshad Shirwadkar 	 * Main fast commit lock. This lock protects accesses to the
1663aa75f4d3SHarshad Shirwadkar 	 * following fields:
1664aa75f4d3SHarshad Shirwadkar 	 * ei->i_fc_list, s_fc_dentry_q, s_fc_q, s_fc_bytes, s_fc_bh.
1665aa75f4d3SHarshad Shirwadkar 	 */
1666aa75f4d3SHarshad Shirwadkar 	spinlock_t s_fc_lock;
1667aa75f4d3SHarshad Shirwadkar 	struct buffer_head *s_fc_bh;
1668aa75f4d3SHarshad Shirwadkar 	struct ext4_fc_stats s_fc_stats;
1669aa75f4d3SHarshad Shirwadkar 	u64 s_fc_avg_commit_time;
16708016e29fSHarshad Shirwadkar #ifdef CONFIG_EXT4_DEBUG
16718016e29fSHarshad Shirwadkar 	int s_fc_debug_max_replay;
16728016e29fSHarshad Shirwadkar #endif
16738016e29fSHarshad Shirwadkar 	struct ext4_fc_replay_state s_fc_replay_state;
1674ca0faba0STheodore Ts'o };
1675ca0faba0STheodore Ts'o 
16763dcf5451SChristoph Hellwig static inline struct ext4_sb_info *EXT4_SB(struct super_block *sb)
16773dcf5451SChristoph Hellwig {
16783dcf5451SChristoph Hellwig 	return sb->s_fs_info;
16793dcf5451SChristoph Hellwig }
16803dcf5451SChristoph Hellwig static inline struct ext4_inode_info *EXT4_I(struct inode *inode)
16813dcf5451SChristoph Hellwig {
16823dcf5451SChristoph Hellwig 	return container_of(inode, struct ext4_inode_info, vfs_inode);
16833dcf5451SChristoph Hellwig }
16843dcf5451SChristoph Hellwig 
16853dcf5451SChristoph Hellwig static inline int ext4_valid_inum(struct super_block *sb, unsigned long ino)
16863dcf5451SChristoph Hellwig {
16873dcf5451SChristoph Hellwig 	return ino == EXT4_ROOT_INO ||
16883dcf5451SChristoph Hellwig 		(ino >= EXT4_FIRST_INO(sb) &&
16893dcf5451SChristoph Hellwig 		 ino <= le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count));
16903dcf5451SChristoph Hellwig }
169119f5fb7aSTheodore Ts'o 
169219f5fb7aSTheodore Ts'o /*
16931d0c3924STheodore Ts'o  * Returns: sbi->field[index]
16941d0c3924STheodore Ts'o  * Used to access an array element from the following sbi fields which require
16951d0c3924STheodore Ts'o  * rcu protection to avoid dereferencing an invalid pointer due to reassignment
16961d0c3924STheodore Ts'o  * - s_group_desc
16971d0c3924STheodore Ts'o  * - s_group_info
16981d0c3924STheodore Ts'o  * - s_flex_group
16991d0c3924STheodore Ts'o  */
17001d0c3924STheodore Ts'o #define sbi_array_rcu_deref(sbi, field, index)				   \
17011d0c3924STheodore Ts'o ({									   \
17021d0c3924STheodore Ts'o 	typeof(*((sbi)->field)) _v;					   \
17031d0c3924STheodore Ts'o 	rcu_read_lock();						   \
17041d0c3924STheodore Ts'o 	_v = ((typeof(_v)*)rcu_dereference((sbi)->field))[index];	   \
17051d0c3924STheodore Ts'o 	rcu_read_unlock();						   \
17061d0c3924STheodore Ts'o 	_v;								   \
17071d0c3924STheodore Ts'o })
17081d0c3924STheodore Ts'o 
17091d0c3924STheodore Ts'o /*
171046f870d6STheodore Ts'o  * Simulate_fail codes
171146f870d6STheodore Ts'o  */
171246f870d6STheodore Ts'o #define EXT4_SIM_BBITMAP_EIO	1
171346f870d6STheodore Ts'o #define EXT4_SIM_BBITMAP_CRC	2
171446f870d6STheodore Ts'o #define EXT4_SIM_IBITMAP_EIO	3
171546f870d6STheodore Ts'o #define EXT4_SIM_IBITMAP_CRC	4
171646f870d6STheodore Ts'o #define EXT4_SIM_INODE_EIO	5
171746f870d6STheodore Ts'o #define EXT4_SIM_INODE_CRC	6
171846f870d6STheodore Ts'o #define EXT4_SIM_DIRBLOCK_EIO	7
171946f870d6STheodore Ts'o #define EXT4_SIM_DIRBLOCK_CRC	8
172046f870d6STheodore Ts'o 
172146f870d6STheodore Ts'o static inline bool ext4_simulate_fail(struct super_block *sb,
172246f870d6STheodore Ts'o 				     unsigned long code)
172346f870d6STheodore Ts'o {
172446f870d6STheodore Ts'o #ifdef CONFIG_EXT4_DEBUG
172546f870d6STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
172646f870d6STheodore Ts'o 
172746f870d6STheodore Ts'o 	if (unlikely(sbi->s_simulate_fail == code)) {
172846f870d6STheodore Ts'o 		sbi->s_simulate_fail = 0;
172946f870d6STheodore Ts'o 		return true;
173046f870d6STheodore Ts'o 	}
173146f870d6STheodore Ts'o #endif
173246f870d6STheodore Ts'o 	return false;
173346f870d6STheodore Ts'o }
173446f870d6STheodore Ts'o 
173546f870d6STheodore Ts'o static inline void ext4_simulate_fail_bh(struct super_block *sb,
173646f870d6STheodore Ts'o 					 struct buffer_head *bh,
173746f870d6STheodore Ts'o 					 unsigned long code)
173846f870d6STheodore Ts'o {
173946f870d6STheodore Ts'o 	if (!IS_ERR(bh) && ext4_simulate_fail(sb, code))
174046f870d6STheodore Ts'o 		clear_buffer_uptodate(bh);
174146f870d6STheodore Ts'o }
174246f870d6STheodore Ts'o 
174346f870d6STheodore Ts'o /*
1744878520acSTheodore Ts'o  * Error number codes for s_{first,last}_error_errno
1745878520acSTheodore Ts'o  *
1746878520acSTheodore Ts'o  * Linux errno numbers are architecture specific, so we need to translate
1747878520acSTheodore Ts'o  * them into something which is architecture independent.   We don't define
1748878520acSTheodore Ts'o  * codes for all errno's; just the ones which are most likely to be the cause
1749878520acSTheodore Ts'o  * of an ext4_error() call.
1750878520acSTheodore Ts'o  */
1751878520acSTheodore Ts'o #define EXT4_ERR_UNKNOWN	 1
1752878520acSTheodore Ts'o #define EXT4_ERR_EIO		 2
1753878520acSTheodore Ts'o #define EXT4_ERR_ENOMEM		 3
1754878520acSTheodore Ts'o #define EXT4_ERR_EFSBADCRC	 4
1755878520acSTheodore Ts'o #define EXT4_ERR_EFSCORRUPTED	 5
1756878520acSTheodore Ts'o #define EXT4_ERR_ENOSPC		 6
1757878520acSTheodore Ts'o #define EXT4_ERR_ENOKEY		 7
1758878520acSTheodore Ts'o #define EXT4_ERR_EROFS		 8
1759878520acSTheodore Ts'o #define EXT4_ERR_EFBIG		 9
1760878520acSTheodore Ts'o #define EXT4_ERR_EEXIST		10
1761878520acSTheodore Ts'o #define EXT4_ERR_ERANGE		11
1762878520acSTheodore Ts'o #define EXT4_ERR_EOVERFLOW	12
1763878520acSTheodore Ts'o #define EXT4_ERR_EBUSY		13
1764878520acSTheodore Ts'o #define EXT4_ERR_ENOTDIR	14
1765878520acSTheodore Ts'o #define EXT4_ERR_ENOTEMPTY	15
1766878520acSTheodore Ts'o #define EXT4_ERR_ESHUTDOWN	16
1767878520acSTheodore Ts'o #define EXT4_ERR_EFAULT		17
1768878520acSTheodore Ts'o 
1769878520acSTheodore Ts'o /*
177019f5fb7aSTheodore Ts'o  * Inode dynamic state flags
177119f5fb7aSTheodore Ts'o  */
177219f5fb7aSTheodore Ts'o enum {
177319f5fb7aSTheodore Ts'o 	EXT4_STATE_JDATA,		/* journaled data exists */
177419f5fb7aSTheodore Ts'o 	EXT4_STATE_NEW,			/* inode is newly created */
177519f5fb7aSTheodore Ts'o 	EXT4_STATE_XATTR,		/* has in-inode xattrs */
177619f5fb7aSTheodore Ts'o 	EXT4_STATE_NO_EXPAND,		/* No space for expansion */
177719f5fb7aSTheodore Ts'o 	EXT4_STATE_DA_ALLOC_CLOSE,	/* Alloc DA blks on close */
177819f5fb7aSTheodore Ts'o 	EXT4_STATE_EXT_MIGRATE,		/* Inode is migrating */
177914ece102SFrank Mayhar 	EXT4_STATE_NEWENTRY,		/* File just added to dir */
178067cf5b09STao Ma 	EXT4_STATE_MAY_INLINE_DATA,	/* may have in-inode data */
17817869a4a6STheodore Ts'o 	EXT4_STATE_EXT_PRECACHED,	/* extents have been precached */
1782a6d05676STahsin Erdogan 	EXT4_STATE_LUSTRE_EA_INODE,	/* Lustre-style ea_inode */
1783c93d8f88SEric Biggers 	EXT4_STATE_VERITY_IN_PROGRESS,	/* building fs-verity Merkle tree */
1784aa75f4d3SHarshad Shirwadkar 	EXT4_STATE_FC_COMMITTING,	/* Fast commit ongoing */
178519f5fb7aSTheodore Ts'o };
178619f5fb7aSTheodore Ts'o 
1787353eb83cSTheodore Ts'o #define EXT4_INODE_BIT_FNS(name, field, offset)				\
178812e9b892SDmitry Monakhov static inline int ext4_test_inode_##name(struct inode *inode, int bit)	\
178912e9b892SDmitry Monakhov {									\
1790353eb83cSTheodore Ts'o 	return test_bit(bit + (offset), &EXT4_I(inode)->i_##field);	\
179112e9b892SDmitry Monakhov }									\
179212e9b892SDmitry Monakhov static inline void ext4_set_inode_##name(struct inode *inode, int bit)	\
179312e9b892SDmitry Monakhov {									\
1794353eb83cSTheodore Ts'o 	set_bit(bit + (offset), &EXT4_I(inode)->i_##field);		\
179512e9b892SDmitry Monakhov }									\
179612e9b892SDmitry Monakhov static inline void ext4_clear_inode_##name(struct inode *inode, int bit) \
179712e9b892SDmitry Monakhov {									\
1798353eb83cSTheodore Ts'o 	clear_bit(bit + (offset), &EXT4_I(inode)->i_##field);		\
179919f5fb7aSTheodore Ts'o }
180019f5fb7aSTheodore Ts'o 
18013f61c0ccSAndreas Dilger /* Add these declarations here only so that these functions can be
18023f61c0ccSAndreas Dilger  * found by name.  Otherwise, they are very hard to locate. */
18033f61c0ccSAndreas Dilger static inline int ext4_test_inode_flag(struct inode *inode, int bit);
18043f61c0ccSAndreas Dilger static inline void ext4_set_inode_flag(struct inode *inode, int bit);
18053f61c0ccSAndreas Dilger static inline void ext4_clear_inode_flag(struct inode *inode, int bit);
1806353eb83cSTheodore Ts'o EXT4_INODE_BIT_FNS(flag, flags, 0)
18073f61c0ccSAndreas Dilger 
18083f61c0ccSAndreas Dilger /* Add these declarations here only so that these functions can be
18093f61c0ccSAndreas Dilger  * found by name.  Otherwise, they are very hard to locate. */
18103f61c0ccSAndreas Dilger static inline int ext4_test_inode_state(struct inode *inode, int bit);
18113f61c0ccSAndreas Dilger static inline void ext4_set_inode_state(struct inode *inode, int bit);
18123f61c0ccSAndreas Dilger static inline void ext4_clear_inode_state(struct inode *inode, int bit);
1813353eb83cSTheodore Ts'o #if (BITS_PER_LONG < 64)
1814353eb83cSTheodore Ts'o EXT4_INODE_BIT_FNS(state, state_flags, 0)
1815353eb83cSTheodore Ts'o 
1816353eb83cSTheodore Ts'o static inline void ext4_clear_state_flags(struct ext4_inode_info *ei)
1817353eb83cSTheodore Ts'o {
1818353eb83cSTheodore Ts'o 	(ei)->i_state_flags = 0;
1819353eb83cSTheodore Ts'o }
1820353eb83cSTheodore Ts'o #else
1821353eb83cSTheodore Ts'o EXT4_INODE_BIT_FNS(state, flags, 32)
1822353eb83cSTheodore Ts'o 
1823353eb83cSTheodore Ts'o static inline void ext4_clear_state_flags(struct ext4_inode_info *ei)
1824353eb83cSTheodore Ts'o {
1825353eb83cSTheodore Ts'o 	/* We depend on the fact that callers will set i_flags */
1826353eb83cSTheodore Ts'o }
1827353eb83cSTheodore Ts'o #endif
18283dcf5451SChristoph Hellwig #else
18293dcf5451SChristoph Hellwig /* Assume that user mode programs are passing in an ext4fs superblock, not
18303dcf5451SChristoph Hellwig  * a kernel struct super_block.  This will allow us to call the feature-test
18313dcf5451SChristoph Hellwig  * macros from user land. */
18323dcf5451SChristoph Hellwig #define EXT4_SB(sb)	(sb)
18333dcf5451SChristoph Hellwig #endif
18343dcf5451SChristoph Hellwig 
1835c93d8f88SEric Biggers static inline bool ext4_verity_in_progress(struct inode *inode)
1836c93d8f88SEric Biggers {
1837c93d8f88SEric Biggers 	return IS_ENABLED(CONFIG_FS_VERITY) &&
1838c93d8f88SEric Biggers 	       ext4_test_inode_state(inode, EXT4_STATE_VERITY_IN_PROGRESS);
1839c93d8f88SEric Biggers }
1840c93d8f88SEric Biggers 
18413dcf5451SChristoph Hellwig #define NEXT_ORPHAN(inode) EXT4_I(inode)->i_dtime
18423dcf5451SChristoph Hellwig 
18433dcf5451SChristoph Hellwig /*
18443dcf5451SChristoph Hellwig  * Codes for operating systems
18453dcf5451SChristoph Hellwig  */
18463dcf5451SChristoph Hellwig #define EXT4_OS_LINUX		0
18473dcf5451SChristoph Hellwig #define EXT4_OS_HURD		1
18483dcf5451SChristoph Hellwig #define EXT4_OS_MASIX		2
18493dcf5451SChristoph Hellwig #define EXT4_OS_FREEBSD		3
18503dcf5451SChristoph Hellwig #define EXT4_OS_LITES		4
18513dcf5451SChristoph Hellwig 
18523dcf5451SChristoph Hellwig /*
18533dcf5451SChristoph Hellwig  * Revision levels
18543dcf5451SChristoph Hellwig  */
18553dcf5451SChristoph Hellwig #define EXT4_GOOD_OLD_REV	0	/* The good old (original) format */
18563dcf5451SChristoph Hellwig #define EXT4_DYNAMIC_REV	1	/* V2 format w/ dynamic inode sizes */
18573dcf5451SChristoph Hellwig 
18583dcf5451SChristoph Hellwig #define EXT4_CURRENT_REV	EXT4_GOOD_OLD_REV
18593dcf5451SChristoph Hellwig #define EXT4_MAX_SUPP_REV	EXT4_DYNAMIC_REV
18603dcf5451SChristoph Hellwig 
18613dcf5451SChristoph Hellwig #define EXT4_GOOD_OLD_INODE_SIZE 128
18623dcf5451SChristoph Hellwig 
18634881c497SDeepa Dinamani #define EXT4_EXTRA_TIMESTAMP_MAX	(((s64)1 << 34) - 1  + S32_MIN)
18644881c497SDeepa Dinamani #define EXT4_NON_EXTRA_TIMESTAMP_MAX	S32_MAX
18654881c497SDeepa Dinamani #define EXT4_TIMESTAMP_MIN		S32_MIN
18664881c497SDeepa Dinamani 
18673dcf5451SChristoph Hellwig /*
18683dcf5451SChristoph Hellwig  * Feature set definitions
18693dcf5451SChristoph Hellwig  */
18703dcf5451SChristoph Hellwig 
18713dcf5451SChristoph Hellwig #define EXT4_FEATURE_COMPAT_DIR_PREALLOC	0x0001
18723dcf5451SChristoph Hellwig #define EXT4_FEATURE_COMPAT_IMAGIC_INODES	0x0002
18733dcf5451SChristoph Hellwig #define EXT4_FEATURE_COMPAT_HAS_JOURNAL		0x0004
18743dcf5451SChristoph Hellwig #define EXT4_FEATURE_COMPAT_EXT_ATTR		0x0008
18753dcf5451SChristoph Hellwig #define EXT4_FEATURE_COMPAT_RESIZE_INODE	0x0010
18763dcf5451SChristoph Hellwig #define EXT4_FEATURE_COMPAT_DIR_INDEX		0x0020
18771beeef1bSDarrick J. Wong #define EXT4_FEATURE_COMPAT_SPARSE_SUPER2	0x0200
1878995a3ed6SHarshad Shirwadkar #define EXT4_FEATURE_COMPAT_FAST_COMMIT		0x0400
1879b925acb8SEric Biggers #define EXT4_FEATURE_COMPAT_STABLE_INODES	0x0800
18803dcf5451SChristoph Hellwig 
18813dcf5451SChristoph Hellwig #define EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER	0x0001
18823dcf5451SChristoph Hellwig #define EXT4_FEATURE_RO_COMPAT_LARGE_FILE	0x0002
18833dcf5451SChristoph Hellwig #define EXT4_FEATURE_RO_COMPAT_BTREE_DIR	0x0004
18843dcf5451SChristoph Hellwig #define EXT4_FEATURE_RO_COMPAT_HUGE_FILE        0x0008
18853dcf5451SChristoph Hellwig #define EXT4_FEATURE_RO_COMPAT_GDT_CSUM		0x0010
18863dcf5451SChristoph Hellwig #define EXT4_FEATURE_RO_COMPAT_DIR_NLINK	0x0020
18873dcf5451SChristoph Hellwig #define EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE	0x0040
1888ae812306SAditya Kali #define EXT4_FEATURE_RO_COMPAT_QUOTA		0x0100
1889281b5995STheodore Ts'o #define EXT4_FEATURE_RO_COMPAT_BIGALLOC		0x0200
1890e6153918SDarrick J. Wong /*
1891e6153918SDarrick J. Wong  * METADATA_CSUM also enables group descriptor checksums (GDT_CSUM).  When
1892e6153918SDarrick J. Wong  * METADATA_CSUM is set, group descriptor checksums use the same algorithm as
1893e6153918SDarrick J. Wong  * all other data structures' checksums.  However, the METADATA_CSUM and
1894e6153918SDarrick J. Wong  * GDT_CSUM bits are mutually exclusive.
1895e6153918SDarrick J. Wong  */
18969b90e5e0STheodore Ts'o #define EXT4_FEATURE_RO_COMPAT_METADATA_CSUM	0x0400
18972cb5cc8bSDarrick J. Wong #define EXT4_FEATURE_RO_COMPAT_READONLY		0x1000
18988b4953e1STheodore Ts'o #define EXT4_FEATURE_RO_COMPAT_PROJECT		0x2000
1899c93d8f88SEric Biggers #define EXT4_FEATURE_RO_COMPAT_VERITY		0x8000
19003dcf5451SChristoph Hellwig 
19013dcf5451SChristoph Hellwig #define EXT4_FEATURE_INCOMPAT_COMPRESSION	0x0001
19023dcf5451SChristoph Hellwig #define EXT4_FEATURE_INCOMPAT_FILETYPE		0x0002
19033dcf5451SChristoph Hellwig #define EXT4_FEATURE_INCOMPAT_RECOVER		0x0004 /* Needs recovery */
19043dcf5451SChristoph Hellwig #define EXT4_FEATURE_INCOMPAT_JOURNAL_DEV	0x0008 /* Journal device */
19053dcf5451SChristoph Hellwig #define EXT4_FEATURE_INCOMPAT_META_BG		0x0010
19063dcf5451SChristoph Hellwig #define EXT4_FEATURE_INCOMPAT_EXTENTS		0x0040 /* extents support */
19073dcf5451SChristoph Hellwig #define EXT4_FEATURE_INCOMPAT_64BIT		0x0080
19083dcf5451SChristoph Hellwig #define EXT4_FEATURE_INCOMPAT_MMP               0x0100
19093dcf5451SChristoph Hellwig #define EXT4_FEATURE_INCOMPAT_FLEX_BG		0x0200
1910f710b4b9STheodore Ts'o #define EXT4_FEATURE_INCOMPAT_EA_INODE		0x0400 /* EA in inode */
1911f710b4b9STheodore Ts'o #define EXT4_FEATURE_INCOMPAT_DIRDATA		0x1000 /* data in dirent */
19128c81bd8fSDarrick J. Wong #define EXT4_FEATURE_INCOMPAT_CSUM_SEED		0x2000
19139b90e5e0STheodore Ts'o #define EXT4_FEATURE_INCOMPAT_LARGEDIR		0x4000 /* >2GB or 3-lvl htree */
191467cf5b09STao Ma #define EXT4_FEATURE_INCOMPAT_INLINE_DATA	0x8000 /* data in inode */
19153edc18d8STheodore Ts'o #define EXT4_FEATURE_INCOMPAT_ENCRYPT		0x10000
1916c83ad55eSGabriel Krisman Bertazi #define EXT4_FEATURE_INCOMPAT_CASEFOLD		0x20000
19173dcf5451SChristoph Hellwig 
1918c9e716ebSAndreas Dilger extern void ext4_update_dynamic_rev(struct super_block *sb);
1919c9e716ebSAndreas Dilger 
1920e2b911c5SDarrick J. Wong #define EXT4_FEATURE_COMPAT_FUNCS(name, flagname) \
1921e2b911c5SDarrick J. Wong static inline bool ext4_has_feature_##name(struct super_block *sb) \
1922e2b911c5SDarrick J. Wong { \
1923e2b911c5SDarrick J. Wong 	return ((EXT4_SB(sb)->s_es->s_feature_compat & \
1924e2b911c5SDarrick J. Wong 		cpu_to_le32(EXT4_FEATURE_COMPAT_##flagname)) != 0); \
1925e2b911c5SDarrick J. Wong } \
1926e2b911c5SDarrick J. Wong static inline void ext4_set_feature_##name(struct super_block *sb) \
1927e2b911c5SDarrick J. Wong { \
1928c9e716ebSAndreas Dilger 	ext4_update_dynamic_rev(sb); \
1929e2b911c5SDarrick J. Wong 	EXT4_SB(sb)->s_es->s_feature_compat |= \
1930e2b911c5SDarrick J. Wong 		cpu_to_le32(EXT4_FEATURE_COMPAT_##flagname); \
1931e2b911c5SDarrick J. Wong } \
1932e2b911c5SDarrick J. Wong static inline void ext4_clear_feature_##name(struct super_block *sb) \
1933e2b911c5SDarrick J. Wong { \
1934e2b911c5SDarrick J. Wong 	EXT4_SB(sb)->s_es->s_feature_compat &= \
1935e2b911c5SDarrick J. Wong 		~cpu_to_le32(EXT4_FEATURE_COMPAT_##flagname); \
1936e2b911c5SDarrick J. Wong }
1937e2b911c5SDarrick J. Wong 
1938e2b911c5SDarrick J. Wong #define EXT4_FEATURE_RO_COMPAT_FUNCS(name, flagname) \
1939e2b911c5SDarrick J. Wong static inline bool ext4_has_feature_##name(struct super_block *sb) \
1940e2b911c5SDarrick J. Wong { \
1941e2b911c5SDarrick J. Wong 	return ((EXT4_SB(sb)->s_es->s_feature_ro_compat & \
1942e2b911c5SDarrick J. Wong 		cpu_to_le32(EXT4_FEATURE_RO_COMPAT_##flagname)) != 0); \
1943e2b911c5SDarrick J. Wong } \
1944e2b911c5SDarrick J. Wong static inline void ext4_set_feature_##name(struct super_block *sb) \
1945e2b911c5SDarrick J. Wong { \
1946c9e716ebSAndreas Dilger 	ext4_update_dynamic_rev(sb); \
1947e2b911c5SDarrick J. Wong 	EXT4_SB(sb)->s_es->s_feature_ro_compat |= \
1948e2b911c5SDarrick J. Wong 		cpu_to_le32(EXT4_FEATURE_RO_COMPAT_##flagname); \
1949e2b911c5SDarrick J. Wong } \
1950e2b911c5SDarrick J. Wong static inline void ext4_clear_feature_##name(struct super_block *sb) \
1951e2b911c5SDarrick J. Wong { \
1952e2b911c5SDarrick J. Wong 	EXT4_SB(sb)->s_es->s_feature_ro_compat &= \
1953e2b911c5SDarrick J. Wong 		~cpu_to_le32(EXT4_FEATURE_RO_COMPAT_##flagname); \
1954e2b911c5SDarrick J. Wong }
1955e2b911c5SDarrick J. Wong 
1956e2b911c5SDarrick J. Wong #define EXT4_FEATURE_INCOMPAT_FUNCS(name, flagname) \
1957e2b911c5SDarrick J. Wong static inline bool ext4_has_feature_##name(struct super_block *sb) \
1958e2b911c5SDarrick J. Wong { \
1959e2b911c5SDarrick J. Wong 	return ((EXT4_SB(sb)->s_es->s_feature_incompat & \
1960e2b911c5SDarrick J. Wong 		cpu_to_le32(EXT4_FEATURE_INCOMPAT_##flagname)) != 0); \
1961e2b911c5SDarrick J. Wong } \
1962e2b911c5SDarrick J. Wong static inline void ext4_set_feature_##name(struct super_block *sb) \
1963e2b911c5SDarrick J. Wong { \
1964c9e716ebSAndreas Dilger 	ext4_update_dynamic_rev(sb); \
1965e2b911c5SDarrick J. Wong 	EXT4_SB(sb)->s_es->s_feature_incompat |= \
1966e2b911c5SDarrick J. Wong 		cpu_to_le32(EXT4_FEATURE_INCOMPAT_##flagname); \
1967e2b911c5SDarrick J. Wong } \
1968e2b911c5SDarrick J. Wong static inline void ext4_clear_feature_##name(struct super_block *sb) \
1969e2b911c5SDarrick J. Wong { \
1970e2b911c5SDarrick J. Wong 	EXT4_SB(sb)->s_es->s_feature_incompat &= \
1971e2b911c5SDarrick J. Wong 		~cpu_to_le32(EXT4_FEATURE_INCOMPAT_##flagname); \
1972e2b911c5SDarrick J. Wong }
1973e2b911c5SDarrick J. Wong 
1974e2b911c5SDarrick J. Wong EXT4_FEATURE_COMPAT_FUNCS(dir_prealloc,		DIR_PREALLOC)
1975e2b911c5SDarrick J. Wong EXT4_FEATURE_COMPAT_FUNCS(imagic_inodes,	IMAGIC_INODES)
1976e2b911c5SDarrick J. Wong EXT4_FEATURE_COMPAT_FUNCS(journal,		HAS_JOURNAL)
1977e2b911c5SDarrick J. Wong EXT4_FEATURE_COMPAT_FUNCS(xattr,		EXT_ATTR)
1978e2b911c5SDarrick J. Wong EXT4_FEATURE_COMPAT_FUNCS(resize_inode,		RESIZE_INODE)
1979e2b911c5SDarrick J. Wong EXT4_FEATURE_COMPAT_FUNCS(dir_index,		DIR_INDEX)
1980e2b911c5SDarrick J. Wong EXT4_FEATURE_COMPAT_FUNCS(sparse_super2,	SPARSE_SUPER2)
1981995a3ed6SHarshad Shirwadkar EXT4_FEATURE_COMPAT_FUNCS(fast_commit,		FAST_COMMIT)
1982b925acb8SEric Biggers EXT4_FEATURE_COMPAT_FUNCS(stable_inodes,	STABLE_INODES)
1983e2b911c5SDarrick J. Wong 
1984e2b911c5SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_FUNCS(sparse_super,	SPARSE_SUPER)
1985e2b911c5SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_FUNCS(large_file,	LARGE_FILE)
1986e2b911c5SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_FUNCS(btree_dir,		BTREE_DIR)
1987e2b911c5SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_FUNCS(huge_file,		HUGE_FILE)
1988e2b911c5SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_FUNCS(gdt_csum,		GDT_CSUM)
1989e2b911c5SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_FUNCS(dir_nlink,		DIR_NLINK)
1990e2b911c5SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_FUNCS(extra_isize,	EXTRA_ISIZE)
1991e2b911c5SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_FUNCS(quota,		QUOTA)
1992e2b911c5SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_FUNCS(bigalloc,		BIGALLOC)
1993e2b911c5SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_FUNCS(metadata_csum,	METADATA_CSUM)
1994e2b911c5SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_FUNCS(readonly,		READONLY)
1995e2b911c5SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_FUNCS(project,		PROJECT)
1996c93d8f88SEric Biggers EXT4_FEATURE_RO_COMPAT_FUNCS(verity,		VERITY)
1997e2b911c5SDarrick J. Wong 
1998e2b911c5SDarrick J. Wong EXT4_FEATURE_INCOMPAT_FUNCS(compression,	COMPRESSION)
1999e2b911c5SDarrick J. Wong EXT4_FEATURE_INCOMPAT_FUNCS(filetype,		FILETYPE)
2000e2b911c5SDarrick J. Wong EXT4_FEATURE_INCOMPAT_FUNCS(journal_needs_recovery,	RECOVER)
2001e2b911c5SDarrick J. Wong EXT4_FEATURE_INCOMPAT_FUNCS(journal_dev,	JOURNAL_DEV)
2002e2b911c5SDarrick J. Wong EXT4_FEATURE_INCOMPAT_FUNCS(meta_bg,		META_BG)
2003e2b911c5SDarrick J. Wong EXT4_FEATURE_INCOMPAT_FUNCS(extents,		EXTENTS)
2004e2b911c5SDarrick J. Wong EXT4_FEATURE_INCOMPAT_FUNCS(64bit,		64BIT)
2005e2b911c5SDarrick J. Wong EXT4_FEATURE_INCOMPAT_FUNCS(mmp,		MMP)
2006e2b911c5SDarrick J. Wong EXT4_FEATURE_INCOMPAT_FUNCS(flex_bg,		FLEX_BG)
2007e2b911c5SDarrick J. Wong EXT4_FEATURE_INCOMPAT_FUNCS(ea_inode,		EA_INODE)
2008e2b911c5SDarrick J. Wong EXT4_FEATURE_INCOMPAT_FUNCS(dirdata,		DIRDATA)
2009e2b911c5SDarrick J. Wong EXT4_FEATURE_INCOMPAT_FUNCS(csum_seed,		CSUM_SEED)
2010e2b911c5SDarrick J. Wong EXT4_FEATURE_INCOMPAT_FUNCS(largedir,		LARGEDIR)
2011e2b911c5SDarrick J. Wong EXT4_FEATURE_INCOMPAT_FUNCS(inline_data,	INLINE_DATA)
2012e2b911c5SDarrick J. Wong EXT4_FEATURE_INCOMPAT_FUNCS(encrypt,		ENCRYPT)
2013c83ad55eSGabriel Krisman Bertazi EXT4_FEATURE_INCOMPAT_FUNCS(casefold,		CASEFOLD)
2014e2b911c5SDarrick J. Wong 
20152035e776STheodore Ts'o #define EXT2_FEATURE_COMPAT_SUPP	EXT4_FEATURE_COMPAT_EXT_ATTR
20162035e776STheodore Ts'o #define EXT2_FEATURE_INCOMPAT_SUPP	(EXT4_FEATURE_INCOMPAT_FILETYPE| \
20172035e776STheodore Ts'o 					 EXT4_FEATURE_INCOMPAT_META_BG)
20182035e776STheodore Ts'o #define EXT2_FEATURE_RO_COMPAT_SUPP	(EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER| \
20192035e776STheodore Ts'o 					 EXT4_FEATURE_RO_COMPAT_LARGE_FILE| \
20202035e776STheodore Ts'o 					 EXT4_FEATURE_RO_COMPAT_BTREE_DIR)
20212035e776STheodore Ts'o 
20222035e776STheodore Ts'o #define EXT3_FEATURE_COMPAT_SUPP	EXT4_FEATURE_COMPAT_EXT_ATTR
20232035e776STheodore Ts'o #define EXT3_FEATURE_INCOMPAT_SUPP	(EXT4_FEATURE_INCOMPAT_FILETYPE| \
20242035e776STheodore Ts'o 					 EXT4_FEATURE_INCOMPAT_RECOVER| \
20252035e776STheodore Ts'o 					 EXT4_FEATURE_INCOMPAT_META_BG)
20262035e776STheodore Ts'o #define EXT3_FEATURE_RO_COMPAT_SUPP	(EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER| \
20272035e776STheodore Ts'o 					 EXT4_FEATURE_RO_COMPAT_LARGE_FILE| \
20282035e776STheodore Ts'o 					 EXT4_FEATURE_RO_COMPAT_BTREE_DIR)
20292035e776STheodore Ts'o 
2030e2b911c5SDarrick J. Wong #define EXT4_FEATURE_COMPAT_SUPP	EXT4_FEATURE_COMPAT_EXT_ATTR
20313dcf5451SChristoph Hellwig #define EXT4_FEATURE_INCOMPAT_SUPP	(EXT4_FEATURE_INCOMPAT_FILETYPE| \
20323dcf5451SChristoph Hellwig 					 EXT4_FEATURE_INCOMPAT_RECOVER| \
20333dcf5451SChristoph Hellwig 					 EXT4_FEATURE_INCOMPAT_META_BG| \
20343dcf5451SChristoph Hellwig 					 EXT4_FEATURE_INCOMPAT_EXTENTS| \
20353dcf5451SChristoph Hellwig 					 EXT4_FEATURE_INCOMPAT_64BIT| \
2036c5e06d10SJohann Lombardi 					 EXT4_FEATURE_INCOMPAT_FLEX_BG| \
2037e50e5129SAndreas Dilger 					 EXT4_FEATURE_INCOMPAT_EA_INODE| \
2038f08225d1STao Ma 					 EXT4_FEATURE_INCOMPAT_MMP | \
20396ddb2447STheodore Ts'o 					 EXT4_FEATURE_INCOMPAT_INLINE_DATA | \
20408c81bd8fSDarrick J. Wong 					 EXT4_FEATURE_INCOMPAT_ENCRYPT | \
2041c83ad55eSGabriel Krisman Bertazi 					 EXT4_FEATURE_INCOMPAT_CASEFOLD | \
2042e08ac99fSArtem Blagodarenko 					 EXT4_FEATURE_INCOMPAT_CSUM_SEED | \
2043e08ac99fSArtem Blagodarenko 					 EXT4_FEATURE_INCOMPAT_LARGEDIR)
20443dcf5451SChristoph Hellwig #define EXT4_FEATURE_RO_COMPAT_SUPP	(EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER| \
20453dcf5451SChristoph Hellwig 					 EXT4_FEATURE_RO_COMPAT_LARGE_FILE| \
20463dcf5451SChristoph Hellwig 					 EXT4_FEATURE_RO_COMPAT_GDT_CSUM| \
20473dcf5451SChristoph Hellwig 					 EXT4_FEATURE_RO_COMPAT_DIR_NLINK | \
20483dcf5451SChristoph Hellwig 					 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE | \
20493dcf5451SChristoph Hellwig 					 EXT4_FEATURE_RO_COMPAT_BTREE_DIR |\
20506f16b606STheodore Ts'o 					 EXT4_FEATURE_RO_COMPAT_HUGE_FILE |\
2051e93376c2SDarrick J. Wong 					 EXT4_FEATURE_RO_COMPAT_BIGALLOC |\
20527c319d32SAditya Kali 					 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM|\
2053040cb378SLi Xi 					 EXT4_FEATURE_RO_COMPAT_QUOTA |\
2054c93d8f88SEric Biggers 					 EXT4_FEATURE_RO_COMPAT_PROJECT |\
2055c93d8f88SEric Biggers 					 EXT4_FEATURE_RO_COMPAT_VERITY)
20563dcf5451SChristoph Hellwig 
2057e2b911c5SDarrick J. Wong #define EXTN_FEATURE_FUNCS(ver) \
2058e2b911c5SDarrick J. Wong static inline bool ext4_has_unknown_ext##ver##_compat_features(struct super_block *sb) \
2059e2b911c5SDarrick J. Wong { \
2060e2b911c5SDarrick J. Wong 	return ((EXT4_SB(sb)->s_es->s_feature_compat & \
2061e2b911c5SDarrick J. Wong 		cpu_to_le32(~EXT##ver##_FEATURE_COMPAT_SUPP)) != 0); \
2062e2b911c5SDarrick J. Wong } \
2063e2b911c5SDarrick J. Wong static inline bool ext4_has_unknown_ext##ver##_ro_compat_features(struct super_block *sb) \
2064e2b911c5SDarrick J. Wong { \
2065e2b911c5SDarrick J. Wong 	return ((EXT4_SB(sb)->s_es->s_feature_ro_compat & \
2066e2b911c5SDarrick J. Wong 		cpu_to_le32(~EXT##ver##_FEATURE_RO_COMPAT_SUPP)) != 0); \
2067e2b911c5SDarrick J. Wong } \
2068e2b911c5SDarrick J. Wong static inline bool ext4_has_unknown_ext##ver##_incompat_features(struct super_block *sb) \
2069e2b911c5SDarrick J. Wong { \
2070e2b911c5SDarrick J. Wong 	return ((EXT4_SB(sb)->s_es->s_feature_incompat & \
2071e2b911c5SDarrick J. Wong 		cpu_to_le32(~EXT##ver##_FEATURE_INCOMPAT_SUPP)) != 0); \
2072e2b911c5SDarrick J. Wong }
2073e2b911c5SDarrick J. Wong 
2074e2b911c5SDarrick J. Wong EXTN_FEATURE_FUNCS(2)
2075e2b911c5SDarrick J. Wong EXTN_FEATURE_FUNCS(3)
2076e2b911c5SDarrick J. Wong EXTN_FEATURE_FUNCS(4)
2077e2b911c5SDarrick J. Wong 
2078e2b911c5SDarrick J. Wong static inline bool ext4_has_compat_features(struct super_block *sb)
2079e2b911c5SDarrick J. Wong {
2080e2b911c5SDarrick J. Wong 	return (EXT4_SB(sb)->s_es->s_feature_compat != 0);
2081e2b911c5SDarrick J. Wong }
2082e2b911c5SDarrick J. Wong static inline bool ext4_has_ro_compat_features(struct super_block *sb)
2083e2b911c5SDarrick J. Wong {
2084e2b911c5SDarrick J. Wong 	return (EXT4_SB(sb)->s_es->s_feature_ro_compat != 0);
2085e2b911c5SDarrick J. Wong }
2086e2b911c5SDarrick J. Wong static inline bool ext4_has_incompat_features(struct super_block *sb)
2087e2b911c5SDarrick J. Wong {
2088e2b911c5SDarrick J. Wong 	return (EXT4_SB(sb)->s_es->s_feature_incompat != 0);
2089e2b911c5SDarrick J. Wong }
2090e2b911c5SDarrick J. Wong 
20913dcf5451SChristoph Hellwig /*
20929549a168STheodore Ts'o  * Superblock flags
20939549a168STheodore Ts'o  */
20949549a168STheodore Ts'o #define EXT4_FLAGS_RESIZING	0
20950db1ff22STheodore Ts'o #define EXT4_FLAGS_SHUTDOWN	1
2096a8ab6d38SIra Weiny #define EXT4_FLAGS_BDEV_IS_DAX	2
20970db1ff22STheodore Ts'o 
20980db1ff22STheodore Ts'o static inline int ext4_forced_shutdown(struct ext4_sb_info *sbi)
20990db1ff22STheodore Ts'o {
21000db1ff22STheodore Ts'o 	return test_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
21010db1ff22STheodore Ts'o }
21029549a168STheodore Ts'o 
21039549a168STheodore Ts'o 
21049549a168STheodore Ts'o /*
21053dcf5451SChristoph Hellwig  * Default values for user and/or group using reserved blocks
21063dcf5451SChristoph Hellwig  */
21073dcf5451SChristoph Hellwig #define	EXT4_DEF_RESUID		0
21083dcf5451SChristoph Hellwig #define	EXT4_DEF_RESGID		0
21093dcf5451SChristoph Hellwig 
2110040cb378SLi Xi /*
2111040cb378SLi Xi  * Default project ID
2112040cb378SLi Xi  */
2113040cb378SLi Xi #define	EXT4_DEF_PROJID		0
2114040cb378SLi Xi 
2115240799cdSTheodore Ts'o #define EXT4_DEF_INODE_READAHEAD_BLKS	32
2116240799cdSTheodore Ts'o 
21173dcf5451SChristoph Hellwig /*
21183dcf5451SChristoph Hellwig  * Default mount options
21193dcf5451SChristoph Hellwig  */
21203dcf5451SChristoph Hellwig #define EXT4_DEFM_DEBUG		0x0001
21213dcf5451SChristoph Hellwig #define EXT4_DEFM_BSDGROUPS	0x0002
21223dcf5451SChristoph Hellwig #define EXT4_DEFM_XATTR_USER	0x0004
21233dcf5451SChristoph Hellwig #define EXT4_DEFM_ACL		0x0008
21243dcf5451SChristoph Hellwig #define EXT4_DEFM_UID16		0x0010
21253dcf5451SChristoph Hellwig #define EXT4_DEFM_JMODE		0x0060
21263dcf5451SChristoph Hellwig #define EXT4_DEFM_JMODE_DATA	0x0020
21273dcf5451SChristoph Hellwig #define EXT4_DEFM_JMODE_ORDERED	0x0040
21283dcf5451SChristoph Hellwig #define EXT4_DEFM_JMODE_WBACK	0x0060
21298b67f04aSTheodore Ts'o #define EXT4_DEFM_NOBARRIER	0x0100
21308b67f04aSTheodore Ts'o #define EXT4_DEFM_BLOCK_VALIDITY 0x0200
21318b67f04aSTheodore Ts'o #define EXT4_DEFM_DISCARD	0x0400
21328b67f04aSTheodore Ts'o #define EXT4_DEFM_NODELALLOC	0x0800
21333dcf5451SChristoph Hellwig 
21343dcf5451SChristoph Hellwig /*
213530773840STheodore Ts'o  * Default journal batch times
213630773840STheodore Ts'o  */
213730773840STheodore Ts'o #define EXT4_DEF_MIN_BATCH_TIME	0
213830773840STheodore Ts'o #define EXT4_DEF_MAX_BATCH_TIME	15000 /* 15ms */
213930773840STheodore Ts'o 
214030773840STheodore Ts'o /*
2141a4912123STheodore Ts'o  * Minimum number of groups in a flexgroup before we separate out
2142a4912123STheodore Ts'o  * directories into the first block group of a flexgroup
2143a4912123STheodore Ts'o  */
2144a4912123STheodore Ts'o #define EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME	4
2145a4912123STheodore Ts'o 
2146a4912123STheodore Ts'o /*
21473dcf5451SChristoph Hellwig  * Structure of a directory entry
21483dcf5451SChristoph Hellwig  */
21493dcf5451SChristoph Hellwig #define EXT4_NAME_LEN 255
21503dcf5451SChristoph Hellwig 
21513dcf5451SChristoph Hellwig struct ext4_dir_entry {
21523dcf5451SChristoph Hellwig 	__le32	inode;			/* Inode number */
21533dcf5451SChristoph Hellwig 	__le16	rec_len;		/* Directory entry length */
21543dcf5451SChristoph Hellwig 	__le16	name_len;		/* Name length */
21553dcf5451SChristoph Hellwig 	char	name[EXT4_NAME_LEN];	/* File name */
21563dcf5451SChristoph Hellwig };
21573dcf5451SChristoph Hellwig 
21583dcf5451SChristoph Hellwig /*
21593dcf5451SChristoph Hellwig  * The new version of the directory entry.  Since EXT4 structures are
21603dcf5451SChristoph Hellwig  * stored in intel byte order, and the name_len field could never be
21613dcf5451SChristoph Hellwig  * bigger than 255 chars, it's safe to reclaim the extra byte for the
21623dcf5451SChristoph Hellwig  * file_type field.
21633dcf5451SChristoph Hellwig  */
21643dcf5451SChristoph Hellwig struct ext4_dir_entry_2 {
21653dcf5451SChristoph Hellwig 	__le32	inode;			/* Inode number */
21663dcf5451SChristoph Hellwig 	__le16	rec_len;		/* Directory entry length */
21673dcf5451SChristoph Hellwig 	__u8	name_len;		/* Name length */
21689f364e1dSJonathan Grant 	__u8	file_type;		/* See file type macros EXT4_FT_* below */
21693dcf5451SChristoph Hellwig 	char	name[EXT4_NAME_LEN];	/* File name */
21703dcf5451SChristoph Hellwig };
21713dcf5451SChristoph Hellwig 
21723dcf5451SChristoph Hellwig /*
2173e6153918SDarrick J. Wong  * This is a bogus directory entry at the end of each leaf block that
2174e6153918SDarrick J. Wong  * records checksums.
2175e6153918SDarrick J. Wong  */
2176e6153918SDarrick J. Wong struct ext4_dir_entry_tail {
2177e6153918SDarrick J. Wong 	__le32	det_reserved_zero1;	/* Pretend to be unused */
2178e6153918SDarrick J. Wong 	__le16	det_rec_len;		/* 12 */
2179e6153918SDarrick J. Wong 	__u8	det_reserved_zero2;	/* Zero name length */
2180e6153918SDarrick J. Wong 	__u8	det_reserved_ft;	/* 0xDE, fake file type */
2181e6153918SDarrick J. Wong 	__le32	det_checksum;		/* crc32c(uuid+inum+dirblock) */
2182e6153918SDarrick J. Wong };
2183e6153918SDarrick J. Wong 
21843c47d541STao Ma #define EXT4_DIRENT_TAIL(block, blocksize) \
21853c47d541STao Ma 	((struct ext4_dir_entry_tail *)(((void *)(block)) + \
21863c47d541STao Ma 					((blocksize) - \
21873c47d541STao Ma 					 sizeof(struct ext4_dir_entry_tail))))
21883c47d541STao Ma 
2189e6153918SDarrick J. Wong /*
21903dcf5451SChristoph Hellwig  * Ext4 directory file types.  Only the low 3 bits are used.  The
21913dcf5451SChristoph Hellwig  * other bits are reserved for now.
21923dcf5451SChristoph Hellwig  */
21933dcf5451SChristoph Hellwig #define EXT4_FT_UNKNOWN		0
21943dcf5451SChristoph Hellwig #define EXT4_FT_REG_FILE	1
21953dcf5451SChristoph Hellwig #define EXT4_FT_DIR		2
21963dcf5451SChristoph Hellwig #define EXT4_FT_CHRDEV		3
21973dcf5451SChristoph Hellwig #define EXT4_FT_BLKDEV		4
21983dcf5451SChristoph Hellwig #define EXT4_FT_FIFO		5
21993dcf5451SChristoph Hellwig #define EXT4_FT_SOCK		6
22003dcf5451SChristoph Hellwig #define EXT4_FT_SYMLINK		7
22013dcf5451SChristoph Hellwig 
22023dcf5451SChristoph Hellwig #define EXT4_FT_MAX		8
22033dcf5451SChristoph Hellwig 
2204e6153918SDarrick J. Wong #define EXT4_FT_DIR_CSUM	0xDE
2205e6153918SDarrick J. Wong 
22063dcf5451SChristoph Hellwig /*
22073dcf5451SChristoph Hellwig  * EXT4_DIR_PAD defines the directory entries boundaries
22083dcf5451SChristoph Hellwig  *
22093dcf5451SChristoph Hellwig  * NOTE: It must be a multiple of 4
22103dcf5451SChristoph Hellwig  */
22113dcf5451SChristoph Hellwig #define EXT4_DIR_PAD			4
22123dcf5451SChristoph Hellwig #define EXT4_DIR_ROUND			(EXT4_DIR_PAD - 1)
22133dcf5451SChristoph Hellwig #define EXT4_DIR_REC_LEN(name_len)	(((name_len) + 8 + EXT4_DIR_ROUND) & \
22143dcf5451SChristoph Hellwig 					 ~EXT4_DIR_ROUND)
22153dcf5451SChristoph Hellwig #define EXT4_MAX_REC_LEN		((1<<16)-1)
22163dcf5451SChristoph Hellwig 
22173dcf5451SChristoph Hellwig /*
22180cfc9255SEric Sandeen  * If we ever get support for fs block sizes > page_size, we'll need
22190cfc9255SEric Sandeen  * to remove the #if statements in the next two functions...
22200cfc9255SEric Sandeen  */
22210cfc9255SEric Sandeen static inline unsigned int
22220cfc9255SEric Sandeen ext4_rec_len_from_disk(__le16 dlen, unsigned blocksize)
22230cfc9255SEric Sandeen {
22240cfc9255SEric Sandeen 	unsigned len = le16_to_cpu(dlen);
22250cfc9255SEric Sandeen 
2226ea1754a0SKirill A. Shutemov #if (PAGE_SIZE >= 65536)
22270cfc9255SEric Sandeen 	if (len == EXT4_MAX_REC_LEN || len == 0)
22280cfc9255SEric Sandeen 		return blocksize;
22290cfc9255SEric Sandeen 	return (len & 65532) | ((len & 3) << 16);
22300cfc9255SEric Sandeen #else
22310cfc9255SEric Sandeen 	return len;
22320cfc9255SEric Sandeen #endif
22330cfc9255SEric Sandeen }
22340cfc9255SEric Sandeen 
22350cfc9255SEric Sandeen static inline __le16 ext4_rec_len_to_disk(unsigned len, unsigned blocksize)
22360cfc9255SEric Sandeen {
22370cfc9255SEric Sandeen 	if ((len > blocksize) || (blocksize > (1 << 18)) || (len & 3))
22380cfc9255SEric Sandeen 		BUG();
2239ea1754a0SKirill A. Shutemov #if (PAGE_SIZE >= 65536)
22400cfc9255SEric Sandeen 	if (len < 65536)
22410cfc9255SEric Sandeen 		return cpu_to_le16(len);
22420cfc9255SEric Sandeen 	if (len == blocksize) {
22430cfc9255SEric Sandeen 		if (blocksize == 65536)
22440cfc9255SEric Sandeen 			return cpu_to_le16(EXT4_MAX_REC_LEN);
22450cfc9255SEric Sandeen 		else
22460cfc9255SEric Sandeen 			return cpu_to_le16(0);
22470cfc9255SEric Sandeen 	}
22480cfc9255SEric Sandeen 	return cpu_to_le16((len & 65532) | ((len >> 16) & 3));
22490cfc9255SEric Sandeen #else
22500cfc9255SEric Sandeen 	return cpu_to_le16(len);
22510cfc9255SEric Sandeen #endif
22520cfc9255SEric Sandeen }
22530cfc9255SEric Sandeen 
22540cfc9255SEric Sandeen /*
22553dcf5451SChristoph Hellwig  * Hash Tree Directory indexing
22563dcf5451SChristoph Hellwig  * (c) Daniel Phillips, 2001
22573dcf5451SChristoph Hellwig  */
22583dcf5451SChristoph Hellwig 
2259e2b911c5SDarrick J. Wong #define is_dx(dir) (ext4_has_feature_dir_index((dir)->i_sb) && \
226012e9b892SDmitry Monakhov 		    ext4_test_inode_flag((dir), EXT4_INODE_INDEX))
2261c7414892SAndreas Dilger #define EXT4_DIR_LINK_MAX(dir) unlikely((dir)->i_nlink >= EXT4_LINK_MAX && \
2262c7414892SAndreas Dilger 		    !(ext4_has_feature_dir_nlink((dir)->i_sb) && is_dx(dir)))
22633dcf5451SChristoph Hellwig #define EXT4_DIR_LINK_EMPTY(dir) ((dir)->i_nlink == 2 || (dir)->i_nlink == 1)
22643dcf5451SChristoph Hellwig 
22653dcf5451SChristoph Hellwig /* Legal values for the dx_root hash_version field: */
22663dcf5451SChristoph Hellwig 
22673dcf5451SChristoph Hellwig #define DX_HASH_LEGACY			0
22683dcf5451SChristoph Hellwig #define DX_HASH_HALF_MD4		1
22693dcf5451SChristoph Hellwig #define DX_HASH_TEA			2
2270f99b2589STheodore Ts'o #define DX_HASH_LEGACY_UNSIGNED		3
2271f99b2589STheodore Ts'o #define DX_HASH_HALF_MD4_UNSIGNED	4
2272f99b2589STheodore Ts'o #define DX_HASH_TEA_UNSIGNED		5
22733dcf5451SChristoph Hellwig 
22740441984aSDarrick J. Wong static inline u32 ext4_chksum(struct ext4_sb_info *sbi, u32 crc,
22750441984aSDarrick J. Wong 			      const void *address, unsigned int length)
22760441984aSDarrick J. Wong {
22770441984aSDarrick J. Wong 	struct {
22780441984aSDarrick J. Wong 		struct shash_desc shash;
22793108b54bSTheodore Ts'o 		char ctx[4];
22800441984aSDarrick J. Wong 	} desc;
22810441984aSDarrick J. Wong 
22823108b54bSTheodore Ts'o 	BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver)!=sizeof(desc.ctx));
22833108b54bSTheodore Ts'o 
22840441984aSDarrick J. Wong 	desc.shash.tfm = sbi->s_chksum_driver;
22850441984aSDarrick J. Wong 	*(u32 *)desc.ctx = crc;
22860441984aSDarrick J. Wong 
2287e7093f0dSPetros Koutoupis 	BUG_ON(crypto_shash_update(&desc.shash, address, length));
22880441984aSDarrick J. Wong 
22890441984aSDarrick J. Wong 	return *(u32 *)desc.ctx;
22900441984aSDarrick J. Wong }
22910441984aSDarrick J. Wong 
22923dcf5451SChristoph Hellwig #ifdef __KERNEL__
22933dcf5451SChristoph Hellwig 
22943dcf5451SChristoph Hellwig /* hash info structure used by the directory hash */
22953dcf5451SChristoph Hellwig struct dx_hash_info
22963dcf5451SChristoph Hellwig {
22973dcf5451SChristoph Hellwig 	u32		hash;
22983dcf5451SChristoph Hellwig 	u32		minor_hash;
22993dcf5451SChristoph Hellwig 	int		hash_version;
23003dcf5451SChristoph Hellwig 	u32		*seed;
23013dcf5451SChristoph Hellwig };
23023dcf5451SChristoph Hellwig 
2303d1f5273eSFan Yong 
2304d1f5273eSFan Yong /* 32 and 64 bit signed EOF for dx directories */
2305d1f5273eSFan Yong #define EXT4_HTREE_EOF_32BIT   ((1UL  << (32 - 1)) - 1)
2306d1f5273eSFan Yong #define EXT4_HTREE_EOF_64BIT   ((1ULL << (64 - 1)) - 1)
2307d1f5273eSFan Yong 
23083dcf5451SChristoph Hellwig 
23093dcf5451SChristoph Hellwig /*
23103dcf5451SChristoph Hellwig  * Control parameters used by ext4_htree_next_block
23113dcf5451SChristoph Hellwig  */
23123dcf5451SChristoph Hellwig #define HASH_NB_ALWAYS		1
23133dcf5451SChristoph Hellwig 
23145b643f9cSTheodore Ts'o struct ext4_filename {
23155b643f9cSTheodore Ts'o 	const struct qstr *usr_fname;
2316a7550b30SJaegeuk Kim 	struct fscrypt_str disk_name;
23175b643f9cSTheodore Ts'o 	struct dx_hash_info hinfo;
2318643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
2319a7550b30SJaegeuk Kim 	struct fscrypt_str crypto_buf;
23205b643f9cSTheodore Ts'o #endif
23213ae72562SGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
23223ae72562SGabriel Krisman Bertazi 	struct fscrypt_str cf_name;
23233ae72562SGabriel Krisman Bertazi #endif
23245b643f9cSTheodore Ts'o };
23255b643f9cSTheodore Ts'o 
23265b643f9cSTheodore Ts'o #define fname_name(p) ((p)->disk_name.name)
23275b643f9cSTheodore Ts'o #define fname_len(p)  ((p)->disk_name.len)
23283dcf5451SChristoph Hellwig 
23293dcf5451SChristoph Hellwig /*
23303dcf5451SChristoph Hellwig  * Describe an inode's exact location on disk and in memory
23313dcf5451SChristoph Hellwig  */
23323dcf5451SChristoph Hellwig struct ext4_iloc
23333dcf5451SChristoph Hellwig {
23343dcf5451SChristoph Hellwig 	struct buffer_head *bh;
23353dcf5451SChristoph Hellwig 	unsigned long offset;
23363dcf5451SChristoph Hellwig 	ext4_group_t block_group;
23373dcf5451SChristoph Hellwig };
23383dcf5451SChristoph Hellwig 
23393dcf5451SChristoph Hellwig static inline struct ext4_inode *ext4_raw_inode(struct ext4_iloc *iloc)
23403dcf5451SChristoph Hellwig {
23413dcf5451SChristoph Hellwig 	return (struct ext4_inode *) (iloc->bh->b_data + iloc->offset);
23423dcf5451SChristoph Hellwig }
23433dcf5451SChristoph Hellwig 
2344dec214d0STahsin Erdogan static inline bool ext4_is_quota_file(struct inode *inode)
2345dec214d0STahsin Erdogan {
2346dec214d0STahsin Erdogan 	return IS_NOQUOTA(inode) &&
2347dec214d0STahsin Erdogan 	       !(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL);
2348dec214d0STahsin Erdogan }
234902749a4cSTahsin Erdogan 
23503dcf5451SChristoph Hellwig /*
23513dcf5451SChristoph Hellwig  * This structure is stuffed into the struct file's private_data field
23523dcf5451SChristoph Hellwig  * for directories.  It is where we put information so that we can do
23533dcf5451SChristoph Hellwig  * readdir operations in hash tree order.
23543dcf5451SChristoph Hellwig  */
23553dcf5451SChristoph Hellwig struct dir_private_info {
23563dcf5451SChristoph Hellwig 	struct rb_root	root;
23573dcf5451SChristoph Hellwig 	struct rb_node	*curr_node;
23583dcf5451SChristoph Hellwig 	struct fname	*extra_fname;
23593dcf5451SChristoph Hellwig 	loff_t		last_pos;
23603dcf5451SChristoph Hellwig 	__u32		curr_hash;
23613dcf5451SChristoph Hellwig 	__u32		curr_minor_hash;
23623dcf5451SChristoph Hellwig 	__u32		next_hash;
23633dcf5451SChristoph Hellwig };
23643dcf5451SChristoph Hellwig 
23653dcf5451SChristoph Hellwig /* calculate the first block number of the group */
23663dcf5451SChristoph Hellwig static inline ext4_fsblk_t
23673dcf5451SChristoph Hellwig ext4_group_first_block_no(struct super_block *sb, ext4_group_t group_no)
23683dcf5451SChristoph Hellwig {
23693dcf5451SChristoph Hellwig 	return group_no * (ext4_fsblk_t)EXT4_BLOCKS_PER_GROUP(sb) +
23703dcf5451SChristoph Hellwig 		le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
23713dcf5451SChristoph Hellwig }
23723dcf5451SChristoph Hellwig 
23733dcf5451SChristoph Hellwig /*
23743dcf5451SChristoph Hellwig  * Special error return code only used by dx_probe() and its callers.
23753dcf5451SChristoph Hellwig  */
237636de9286STheodore Ts'o #define ERR_BAD_DX_DIR	(-(MAX_ERRNO - 1))
23773dcf5451SChristoph Hellwig 
2378e08ac99fSArtem Blagodarenko /* htree levels for ext4 */
2379e08ac99fSArtem Blagodarenko #define	EXT4_HTREE_LEVEL_COMPAT	2
2380e08ac99fSArtem Blagodarenko #define	EXT4_HTREE_LEVEL	3
2381e08ac99fSArtem Blagodarenko 
2382e08ac99fSArtem Blagodarenko static inline int ext4_dir_htree_level(struct super_block *sb)
2383e08ac99fSArtem Blagodarenko {
2384e08ac99fSArtem Blagodarenko 	return ext4_has_feature_largedir(sb) ?
2385e08ac99fSArtem Blagodarenko 		EXT4_HTREE_LEVEL : EXT4_HTREE_LEVEL_COMPAT;
2386e08ac99fSArtem Blagodarenko }
2387e08ac99fSArtem Blagodarenko 
23883dcf5451SChristoph Hellwig /*
2389bfff6873SLukas Czerner  * Timeout and state flag for lazy initialization inode thread.
2390bfff6873SLukas Czerner  */
2391bfff6873SLukas Czerner #define EXT4_DEF_LI_WAIT_MULT			10
2392bfff6873SLukas Czerner #define EXT4_DEF_LI_MAX_START_DELAY		5
2393bfff6873SLukas Czerner #define EXT4_LAZYINIT_QUIT			0x0001
2394bfff6873SLukas Czerner #define EXT4_LAZYINIT_RUNNING			0x0002
2395bfff6873SLukas Czerner 
2396bfff6873SLukas Czerner /*
2397bfff6873SLukas Czerner  * Lazy inode table initialization info
2398bfff6873SLukas Czerner  */
2399bfff6873SLukas Czerner struct ext4_lazy_init {
2400bfff6873SLukas Czerner 	unsigned long		li_state;
2401bfff6873SLukas Czerner 	struct list_head	li_request_list;
2402bfff6873SLukas Czerner 	struct mutex		li_list_mtx;
2403bfff6873SLukas Czerner };
2404bfff6873SLukas Czerner 
24053d392b26STheodore Ts'o enum ext4_li_mode {
24063d392b26STheodore Ts'o 	EXT4_LI_MODE_PREFETCH_BBITMAP,
24073d392b26STheodore Ts'o 	EXT4_LI_MODE_ITABLE,
24083d392b26STheodore Ts'o };
24093d392b26STheodore Ts'o 
2410bfff6873SLukas Czerner struct ext4_li_request {
2411bfff6873SLukas Czerner 	struct super_block	*lr_super;
24123d392b26STheodore Ts'o 	enum ext4_li_mode	lr_mode;
24133d392b26STheodore Ts'o 	ext4_group_t		lr_first_not_zeroed;
2414bfff6873SLukas Czerner 	ext4_group_t		lr_next_group;
2415bfff6873SLukas Czerner 	struct list_head	lr_request;
2416bfff6873SLukas Czerner 	unsigned long		lr_next_sched;
2417bfff6873SLukas Czerner 	unsigned long		lr_timeout;
2418bfff6873SLukas Czerner };
2419bfff6873SLukas Czerner 
2420857ac889SLukas Czerner struct ext4_features {
2421857ac889SLukas Czerner 	struct kobject f_kobj;
2422857ac889SLukas Czerner 	struct completion f_kobj_unregister;
2423857ac889SLukas Czerner };
2424857ac889SLukas Czerner 
2425bfff6873SLukas Czerner /*
2426c5e06d10SJohann Lombardi  * This structure will be used for multiple mount protection. It will be
2427c5e06d10SJohann Lombardi  * written into the block number saved in the s_mmp_block field in the
2428c5e06d10SJohann Lombardi  * superblock. Programs that check MMP should assume that if
2429c5e06d10SJohann Lombardi  * SEQ_FSCK (or any unknown code above SEQ_MAX) is present then it is NOT safe
2430c5e06d10SJohann Lombardi  * to use the filesystem, regardless of how old the timestamp is.
2431c5e06d10SJohann Lombardi  */
2432c5e06d10SJohann Lombardi #define EXT4_MMP_MAGIC     0x004D4D50U /* ASCII for MMP */
2433c5e06d10SJohann Lombardi #define EXT4_MMP_SEQ_CLEAN 0xFF4D4D50U /* mmp_seq value for clean unmount */
2434c5e06d10SJohann Lombardi #define EXT4_MMP_SEQ_FSCK  0xE24D4D50U /* mmp_seq value when being fscked */
2435c5e06d10SJohann Lombardi #define EXT4_MMP_SEQ_MAX   0xE24D4D4FU /* maximum valid mmp_seq value */
2436c5e06d10SJohann Lombardi 
2437c5e06d10SJohann Lombardi struct mmp_struct {
2438c5e06d10SJohann Lombardi 	__le32	mmp_magic;		/* Magic number for MMP */
2439c5e06d10SJohann Lombardi 	__le32	mmp_seq;		/* Sequence no. updated periodically */
2440c5e06d10SJohann Lombardi 
2441c5e06d10SJohann Lombardi 	/*
2442c5e06d10SJohann Lombardi 	 * mmp_time, mmp_nodename & mmp_bdevname are only used for information
2443c5e06d10SJohann Lombardi 	 * purposes and do not affect the correctness of the algorithm
2444c5e06d10SJohann Lombardi 	 */
2445c5e06d10SJohann Lombardi 	__le64	mmp_time;		/* Time last updated */
2446c5e06d10SJohann Lombardi 	char	mmp_nodename[64];	/* Node which last updated MMP block */
2447c5e06d10SJohann Lombardi 	char	mmp_bdevname[32];	/* Bdev which last updated MMP block */
2448c5e06d10SJohann Lombardi 
2449c5e06d10SJohann Lombardi 	/*
2450c5e06d10SJohann Lombardi 	 * mmp_check_interval is used to verify if the MMP block has been
2451c5e06d10SJohann Lombardi 	 * updated on the block device. The value is updated based on the
2452c5e06d10SJohann Lombardi 	 * maximum time to write the MMP block during an update cycle.
2453c5e06d10SJohann Lombardi 	 */
2454c5e06d10SJohann Lombardi 	__le16	mmp_check_interval;
2455c5e06d10SJohann Lombardi 
2456c5e06d10SJohann Lombardi 	__le16	mmp_pad1;
2457e6153918SDarrick J. Wong 	__le32	mmp_pad2[226];
2458e6153918SDarrick J. Wong 	__le32	mmp_checksum;		/* crc32c(uuid+mmp_block) */
2459c5e06d10SJohann Lombardi };
2460c5e06d10SJohann Lombardi 
2461c5e06d10SJohann Lombardi /* arguments passed to the mmp thread */
2462c5e06d10SJohann Lombardi struct mmpd_data {
2463c5e06d10SJohann Lombardi 	struct buffer_head *bh; /* bh from initial read_mmp_block() */
2464c5e06d10SJohann Lombardi 	struct super_block *sb;  /* super block of the fs */
2465c5e06d10SJohann Lombardi };
2466c5e06d10SJohann Lombardi 
2467c5e06d10SJohann Lombardi /*
2468c5e06d10SJohann Lombardi  * Check interval multiplier
2469c5e06d10SJohann Lombardi  * The MMP block is written every update interval and initially checked every
2470c5e06d10SJohann Lombardi  * update interval x the multiplier (the value is then adapted based on the
2471c5e06d10SJohann Lombardi  * write latency). The reason is that writes can be delayed under load and we
2472c5e06d10SJohann Lombardi  * don't want readers to incorrectly assume that the filesystem is no longer
2473c5e06d10SJohann Lombardi  * in use.
2474c5e06d10SJohann Lombardi  */
2475c5e06d10SJohann Lombardi #define EXT4_MMP_CHECK_MULT		2UL
2476c5e06d10SJohann Lombardi 
2477c5e06d10SJohann Lombardi /*
2478c5e06d10SJohann Lombardi  * Minimum interval for MMP checking in seconds.
2479c5e06d10SJohann Lombardi  */
2480c5e06d10SJohann Lombardi #define EXT4_MMP_MIN_CHECK_INTERVAL	5UL
2481c5e06d10SJohann Lombardi 
2482c5e06d10SJohann Lombardi /*
2483c5e06d10SJohann Lombardi  * Maximum interval for MMP checking in seconds.
2484c5e06d10SJohann Lombardi  */
2485c5e06d10SJohann Lombardi #define EXT4_MMP_MAX_CHECK_INTERVAL	300UL
2486c5e06d10SJohann Lombardi 
2487c5e06d10SJohann Lombardi /*
24883dcf5451SChristoph Hellwig  * Function prototypes
24893dcf5451SChristoph Hellwig  */
24903dcf5451SChristoph Hellwig 
24913dcf5451SChristoph Hellwig /*
24923dcf5451SChristoph Hellwig  * Ok, these declarations are also in <linux/kernel.h> but none of the
24933dcf5451SChristoph Hellwig  * ext4 source programs needs to include it so they are duplicated here.
24943dcf5451SChristoph Hellwig  */
24953dcf5451SChristoph Hellwig # define NORET_TYPE	/**/
24963dcf5451SChristoph Hellwig # define ATTRIB_NORET	__attribute__((noreturn))
24973dcf5451SChristoph Hellwig # define NORET_AND	noreturn,
24983dcf5451SChristoph Hellwig 
2499498e5f24STheodore Ts'o /* bitmap.c */
2500f6fb99caSTheodore Ts'o extern unsigned int ext4_count_free(char *bitmap, unsigned numchars);
250141a246d1SDarrick J. Wong void ext4_inode_bitmap_csum_set(struct super_block *sb, ext4_group_t group,
250241a246d1SDarrick J. Wong 				struct ext4_group_desc *gdp,
250341a246d1SDarrick J. Wong 				struct buffer_head *bh, int sz);
250441a246d1SDarrick J. Wong int ext4_inode_bitmap_csum_verify(struct super_block *sb, ext4_group_t group,
250541a246d1SDarrick J. Wong 				  struct ext4_group_desc *gdp,
250641a246d1SDarrick J. Wong 				  struct buffer_head *bh, int sz);
2507fa77dcfaSDarrick J. Wong void ext4_block_bitmap_csum_set(struct super_block *sb, ext4_group_t group,
2508fa77dcfaSDarrick J. Wong 				struct ext4_group_desc *gdp,
250979f1ba49STao Ma 				struct buffer_head *bh);
2510fa77dcfaSDarrick J. Wong int ext4_block_bitmap_csum_verify(struct super_block *sb, ext4_group_t group,
2511fa77dcfaSDarrick J. Wong 				  struct ext4_group_desc *gdp,
251279f1ba49STao Ma 				  struct buffer_head *bh);
2513498e5f24STheodore Ts'o 
25143dcf5451SChristoph Hellwig /* balloc.c */
2515bd86298eSLukas Czerner extern void ext4_get_group_no_and_offset(struct super_block *sb,
2516bd86298eSLukas Czerner 					 ext4_fsblk_t blocknr,
2517bd86298eSLukas Czerner 					 ext4_group_t *blockgrpp,
2518bd86298eSLukas Czerner 					 ext4_grpblk_t *offsetp);
2519bd86298eSLukas Czerner extern ext4_group_t ext4_get_group_number(struct super_block *sb,
2520bd86298eSLukas Czerner 					  ext4_fsblk_t block);
2521bd86298eSLukas Czerner 
25223dcf5451SChristoph Hellwig extern unsigned int ext4_block_group(struct super_block *sb,
25233dcf5451SChristoph Hellwig 			ext4_fsblk_t blocknr);
25243dcf5451SChristoph Hellwig extern ext4_grpblk_t ext4_block_group_offset(struct super_block *sb,
25253dcf5451SChristoph Hellwig 			ext4_fsblk_t blocknr);
25263dcf5451SChristoph Hellwig extern int ext4_bg_has_super(struct super_block *sb, ext4_group_t group);
25273dcf5451SChristoph Hellwig extern unsigned long ext4_bg_num_gdb(struct super_block *sb,
25283dcf5451SChristoph Hellwig 			ext4_group_t group);
25297061eba7SAneesh Kumar K.V extern ext4_fsblk_t ext4_new_meta_blocks(handle_t *handle, struct inode *inode,
253055f020dbSAllison Henderson 					 ext4_fsblk_t goal,
253155f020dbSAllison Henderson 					 unsigned int flags,
253255f020dbSAllison Henderson 					 unsigned long *count,
253355f020dbSAllison Henderson 					 int *errp);
2534e7d5f315STheodore Ts'o extern int ext4_claim_free_clusters(struct ext4_sb_info *sbi,
2535e7d5f315STheodore Ts'o 				    s64 nclusters, unsigned int flags);
25365dee5437STheodore Ts'o extern ext4_fsblk_t ext4_count_free_clusters(struct super_block *);
25373dcf5451SChristoph Hellwig extern void ext4_check_blocks_bitmap(struct super_block *);
25383dcf5451SChristoph Hellwig extern struct ext4_group_desc * ext4_get_group_desc(struct super_block * sb,
25393dcf5451SChristoph Hellwig 						    ext4_group_t block_group,
25403dcf5451SChristoph Hellwig 						    struct buffer_head ** bh);
25413dcf5451SChristoph Hellwig extern int ext4_should_retry_alloc(struct super_block *sb, int *retries);
2542813e5727STheodore Ts'o 
2543813e5727STheodore Ts'o extern struct buffer_head *ext4_read_block_bitmap_nowait(struct super_block *sb,
2544cfd73237SAlex Zhuravlev 						ext4_group_t block_group,
2545cfd73237SAlex Zhuravlev 						bool ignore_locked);
2546813e5727STheodore Ts'o extern int ext4_wait_block_bitmap(struct super_block *sb,
2547813e5727STheodore Ts'o 				  ext4_group_t block_group,
2548813e5727STheodore Ts'o 				  struct buffer_head *bh);
2549813e5727STheodore Ts'o extern struct buffer_head *ext4_read_block_bitmap(struct super_block *sb,
2550bb23c20aSTheodore Ts'o 						  ext4_group_t block_group);
2551cff1dfd7STheodore Ts'o extern unsigned ext4_free_clusters_after_init(struct super_block *sb,
2552fd034a84STheodore Ts'o 					      ext4_group_t block_group,
2553fd034a84STheodore Ts'o 					      struct ext4_group_desc *gdp);
2554f86186b4SEric Sandeen ext4_fsblk_t ext4_inode_to_goal_block(struct inode *);
25553dcf5451SChristoph Hellwig 
25563ae72562SGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
25573ae72562SGabriel Krisman Bertazi extern void ext4_fname_setup_ci_filename(struct inode *dir,
25583ae72562SGabriel Krisman Bertazi 					 const struct qstr *iname,
25593ae72562SGabriel Krisman Bertazi 					 struct fscrypt_str *fname);
25603ae72562SGabriel Krisman Bertazi #endif
25613ae72562SGabriel Krisman Bertazi 
2562643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
2563b01531dbSEric Biggers static inline void ext4_fname_from_fscrypt_name(struct ext4_filename *dst,
2564b01531dbSEric Biggers 						const struct fscrypt_name *src)
2565b01531dbSEric Biggers {
2566b01531dbSEric Biggers 	memset(dst, 0, sizeof(*dst));
2567b01531dbSEric Biggers 
2568b01531dbSEric Biggers 	dst->usr_fname = src->usr_fname;
2569b01531dbSEric Biggers 	dst->disk_name = src->disk_name;
2570b01531dbSEric Biggers 	dst->hinfo.hash = src->hash;
2571b01531dbSEric Biggers 	dst->hinfo.minor_hash = src->minor_hash;
2572b01531dbSEric Biggers 	dst->crypto_buf = src->crypto_buf;
2573b01531dbSEric Biggers }
2574b01531dbSEric Biggers 
2575a7550b30SJaegeuk Kim static inline int ext4_fname_setup_filename(struct inode *dir,
2576a7550b30SJaegeuk Kim 					    const struct qstr *iname,
2577b01531dbSEric Biggers 					    int lookup,
2578b01531dbSEric Biggers 					    struct ext4_filename *fname)
2579a7550b30SJaegeuk Kim {
2580a7550b30SJaegeuk Kim 	struct fscrypt_name name;
2581a7550b30SJaegeuk Kim 	int err;
2582a7550b30SJaegeuk Kim 
2583a7550b30SJaegeuk Kim 	err = fscrypt_setup_filename(dir, iname, lookup, &name);
2584b01531dbSEric Biggers 	if (err)
2585a7550b30SJaegeuk Kim 		return err;
2586b01531dbSEric Biggers 
2587b01531dbSEric Biggers 	ext4_fname_from_fscrypt_name(fname, &name);
25883ae72562SGabriel Krisman Bertazi 
25893ae72562SGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
25903ae72562SGabriel Krisman Bertazi 	ext4_fname_setup_ci_filename(dir, iname, &fname->cf_name);
25913ae72562SGabriel Krisman Bertazi #endif
2592b01531dbSEric Biggers 	return 0;
2593b01531dbSEric Biggers }
2594b01531dbSEric Biggers 
2595b01531dbSEric Biggers static inline int ext4_fname_prepare_lookup(struct inode *dir,
2596b01531dbSEric Biggers 					    struct dentry *dentry,
2597b01531dbSEric Biggers 					    struct ext4_filename *fname)
2598b01531dbSEric Biggers {
2599b01531dbSEric Biggers 	struct fscrypt_name name;
2600b01531dbSEric Biggers 	int err;
2601b01531dbSEric Biggers 
2602b01531dbSEric Biggers 	err = fscrypt_prepare_lookup(dir, dentry, &name);
2603b01531dbSEric Biggers 	if (err)
2604b01531dbSEric Biggers 		return err;
2605b01531dbSEric Biggers 
2606b01531dbSEric Biggers 	ext4_fname_from_fscrypt_name(fname, &name);
26073ae72562SGabriel Krisman Bertazi 
26083ae72562SGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
26093ae72562SGabriel Krisman Bertazi 	ext4_fname_setup_ci_filename(dir, &dentry->d_name, &fname->cf_name);
26103ae72562SGabriel Krisman Bertazi #endif
2611b01531dbSEric Biggers 	return 0;
2612a7550b30SJaegeuk Kim }
2613a7550b30SJaegeuk Kim 
2614a7550b30SJaegeuk Kim static inline void ext4_fname_free_filename(struct ext4_filename *fname)
2615a7550b30SJaegeuk Kim {
2616a7550b30SJaegeuk Kim 	struct fscrypt_name name;
2617a7550b30SJaegeuk Kim 
2618a7550b30SJaegeuk Kim 	name.crypto_buf = fname->crypto_buf;
2619a7550b30SJaegeuk Kim 	fscrypt_free_filename(&name);
2620a7550b30SJaegeuk Kim 
2621a7550b30SJaegeuk Kim 	fname->crypto_buf.name = NULL;
2622a7550b30SJaegeuk Kim 	fname->usr_fname = NULL;
2623a7550b30SJaegeuk Kim 	fname->disk_name.name = NULL;
26243ae72562SGabriel Krisman Bertazi 
26253ae72562SGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
26263ae72562SGabriel Krisman Bertazi 	kfree(fname->cf_name.name);
26273ae72562SGabriel Krisman Bertazi 	fname->cf_name.name = NULL;
26283ae72562SGabriel Krisman Bertazi #endif
2629a7550b30SJaegeuk Kim }
2630b01531dbSEric Biggers #else /* !CONFIG_FS_ENCRYPTION */
26315b643f9cSTheodore Ts'o static inline int ext4_fname_setup_filename(struct inode *dir,
26325b643f9cSTheodore Ts'o 					    const struct qstr *iname,
2633b01531dbSEric Biggers 					    int lookup,
2634b01531dbSEric Biggers 					    struct ext4_filename *fname)
26355b643f9cSTheodore Ts'o {
26365b643f9cSTheodore Ts'o 	fname->usr_fname = iname;
26375b643f9cSTheodore Ts'o 	fname->disk_name.name = (unsigned char *) iname->name;
26385b643f9cSTheodore Ts'o 	fname->disk_name.len = iname->len;
26393ae72562SGabriel Krisman Bertazi 
26403ae72562SGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
26413ae72562SGabriel Krisman Bertazi 	ext4_fname_setup_ci_filename(dir, iname, &fname->cf_name);
26423ae72562SGabriel Krisman Bertazi #endif
26433ae72562SGabriel Krisman Bertazi 
26445b643f9cSTheodore Ts'o 	return 0;
26455b643f9cSTheodore Ts'o }
2646a7550b30SJaegeuk Kim 
2647b01531dbSEric Biggers static inline int ext4_fname_prepare_lookup(struct inode *dir,
2648b01531dbSEric Biggers 					    struct dentry *dentry,
2649b01531dbSEric Biggers 					    struct ext4_filename *fname)
2650b01531dbSEric Biggers {
2651b01531dbSEric Biggers 	return ext4_fname_setup_filename(dir, &dentry->d_name, 1, fname);
2652b01531dbSEric Biggers }
2653b01531dbSEric Biggers 
26543ae72562SGabriel Krisman Bertazi static inline void ext4_fname_free_filename(struct ext4_filename *fname)
26553ae72562SGabriel Krisman Bertazi {
26563ae72562SGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
26573ae72562SGabriel Krisman Bertazi 	kfree(fname->cf_name.name);
26583ae72562SGabriel Krisman Bertazi 	fname->cf_name.name = NULL;
26593ae72562SGabriel Krisman Bertazi #endif
26603ae72562SGabriel Krisman Bertazi }
2661b01531dbSEric Biggers #endif /* !CONFIG_FS_ENCRYPTION */
2662d5d0e8c7SMichael Halcrow 
26633dcf5451SChristoph Hellwig /* dir.c */
266460fd4da3STheodore Ts'o extern int __ext4_check_dir_entry(const char *, unsigned int, struct inode *,
2665f7c21177STheodore Ts'o 				  struct file *,
26663dcf5451SChristoph Hellwig 				  struct ext4_dir_entry_2 *,
2667226ba972STao Ma 				  struct buffer_head *, char *, int,
2668226ba972STao Ma 				  unsigned int);
2669226ba972STao Ma #define ext4_check_dir_entry(dir, filp, de, bh, buf, size, offset)	\
2670f7c21177STheodore Ts'o 	unlikely(__ext4_check_dir_entry(__func__, __LINE__, (dir), (filp), \
2671226ba972STao Ma 					(de), (bh), (buf), (size), (offset)))
26723dcf5451SChristoph Hellwig extern int ext4_htree_store_dirent(struct file *dir_file, __u32 hash,
26733dcf5451SChristoph Hellwig 				__u32 minor_hash,
26742f61830aSTheodore Ts'o 				struct ext4_dir_entry_2 *dirent,
2675a7550b30SJaegeuk Kim 				struct fscrypt_str *ent_name);
26763dcf5451SChristoph Hellwig extern void ext4_htree_free_dir_info(struct dir_private_info *p);
2677978fef91STao Ma extern int ext4_find_dest_de(struct inode *dir, struct inode *inode,
2678978fef91STao Ma 			     struct buffer_head *bh,
2679978fef91STao Ma 			     void *buf, int buf_size,
26805b643f9cSTheodore Ts'o 			     struct ext4_filename *fname,
2681978fef91STao Ma 			     struct ext4_dir_entry_2 **dest_de);
26821bc0af60SEric Biggers void ext4_insert_dentry(struct inode *inode,
2683978fef91STao Ma 			struct ext4_dir_entry_2 *de,
2684978fef91STao Ma 			int buf_size,
26855b643f9cSTheodore Ts'o 			struct ext4_filename *fname);
2686978fef91STao Ma static inline void ext4_update_dx_flag(struct inode *inode)
2687978fef91STao Ma {
268848a34311SJan Kara 	if (!ext4_has_feature_dir_index(inode->i_sb)) {
268948a34311SJan Kara 		/* ext4_iget() should have caught this... */
269048a34311SJan Kara 		WARN_ON_ONCE(ext4_has_feature_metadata_csum(inode->i_sb));
2691978fef91STao Ma 		ext4_clear_inode_flag(inode, EXT4_INODE_INDEX);
2692978fef91STao Ma 	}
269348a34311SJan Kara }
2694d6006186SEric Biggers static const unsigned char ext4_filetype_table[] = {
269565d165d9STao Ma 	DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
269665d165d9STao Ma };
269765d165d9STao Ma 
269865d165d9STao Ma static inline  unsigned char get_dtype(struct super_block *sb, int filetype)
269965d165d9STao Ma {
2700e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_filetype(sb) || filetype >= EXT4_FT_MAX)
270165d165d9STao Ma 		return DT_UNKNOWN;
270265d165d9STao Ma 
270365d165d9STao Ma 	return ext4_filetype_table[filetype];
270465d165d9STao Ma }
270540b163f1SDarrick J. Wong extern int ext4_check_all_de(struct inode *dir, struct buffer_head *bh,
270640b163f1SDarrick J. Wong 			     void *buf, int buf_size);
27073dcf5451SChristoph Hellwig 
27083dcf5451SChristoph Hellwig /* fsync.c */
270902c24a82SJosef Bacik extern int ext4_sync_file(struct file *, loff_t, loff_t, int);
27103dcf5451SChristoph Hellwig 
27113dcf5451SChristoph Hellwig /* hash.c */
2712b886ee3eSGabriel Krisman Bertazi extern int ext4fs_dirhash(const struct inode *dir, const char *name, int len,
2713b886ee3eSGabriel Krisman Bertazi 			  struct dx_hash_info *hinfo);
27143dcf5451SChristoph Hellwig 
27153dcf5451SChristoph Hellwig /* ialloc.c */
27168016e29fSHarshad Shirwadkar extern int ext4_mark_inode_used(struct super_block *sb, int ino);
27171139575aSTheodore Ts'o extern struct inode *__ext4_new_inode(handle_t *, struct inode *, umode_t,
27185cb81dabSDmitry Monakhov 				      const struct qstr *qstr, __u32 goal,
27191b917ed8STahsin Erdogan 				      uid_t *owner, __u32 i_flags,
27201b917ed8STahsin Erdogan 				      int handle_type, unsigned int line_no,
27211b917ed8STahsin Erdogan 				      int nblocks);
27221139575aSTheodore Ts'o 
27231b917ed8STahsin Erdogan #define ext4_new_inode(handle, dir, mode, qstr, goal, owner, i_flags) \
27241139575aSTheodore Ts'o 	__ext4_new_inode((handle), (dir), (mode), (qstr), (goal), (owner), \
27251b917ed8STahsin Erdogan 			 i_flags, 0, 0, 0)
27261139575aSTheodore Ts'o #define ext4_new_inode_start_handle(dir, mode, qstr, goal, owner, \
27271139575aSTheodore Ts'o 				    type, nblocks)		    \
27281139575aSTheodore Ts'o 	__ext4_new_inode(NULL, (dir), (mode), (qstr), (goal), (owner), \
27291b917ed8STahsin Erdogan 			 0, (type), __LINE__, (nblocks))
27301139575aSTheodore Ts'o 
27311139575aSTheodore Ts'o 
27323dcf5451SChristoph Hellwig extern void ext4_free_inode(handle_t *, struct inode *);
27333dcf5451SChristoph Hellwig extern struct inode * ext4_orphan_get(struct super_block *, unsigned long);
27343dcf5451SChristoph Hellwig extern unsigned long ext4_count_free_inodes(struct super_block *);
27353dcf5451SChristoph Hellwig extern unsigned long ext4_count_dirs(struct super_block *);
27363dcf5451SChristoph Hellwig extern void ext4_check_inodes_bitmap(struct super_block *);
273761d08673STheodore Ts'o extern void ext4_mark_bitmap_end(int start_bit, int end_bit, char *bitmap);
2738bfff6873SLukas Czerner extern int ext4_init_inode_table(struct super_block *sb,
2739bfff6873SLukas Czerner 				 ext4_group_t group, int barrier);
2740813e5727STheodore Ts'o extern void ext4_end_bitmap_read(struct buffer_head *bh, int uptodate);
27413dcf5451SChristoph Hellwig 
27426866d7b3SHarshad Shirwadkar /* fast_commit.c */
2743ce8c59d1SHarshad Shirwadkar int ext4_fc_info_show(struct seq_file *seq, void *v);
27446866d7b3SHarshad Shirwadkar void ext4_fc_init(struct super_block *sb, journal_t *journal);
2745aa75f4d3SHarshad Shirwadkar void ext4_fc_init_inode(struct inode *inode);
2746aa75f4d3SHarshad Shirwadkar void ext4_fc_track_range(struct inode *inode, ext4_lblk_t start,
2747aa75f4d3SHarshad Shirwadkar 			 ext4_lblk_t end);
2748aa75f4d3SHarshad Shirwadkar void ext4_fc_track_unlink(struct inode *inode, struct dentry *dentry);
2749aa75f4d3SHarshad Shirwadkar void ext4_fc_track_link(struct inode *inode, struct dentry *dentry);
2750aa75f4d3SHarshad Shirwadkar void ext4_fc_track_create(struct inode *inode, struct dentry *dentry);
2751aa75f4d3SHarshad Shirwadkar void ext4_fc_track_inode(struct inode *inode);
2752aa75f4d3SHarshad Shirwadkar void ext4_fc_mark_ineligible(struct super_block *sb, int reason);
2753aa75f4d3SHarshad Shirwadkar void ext4_fc_start_ineligible(struct super_block *sb, int reason);
2754aa75f4d3SHarshad Shirwadkar void ext4_fc_stop_ineligible(struct super_block *sb);
2755aa75f4d3SHarshad Shirwadkar void ext4_fc_start_update(struct inode *inode);
2756aa75f4d3SHarshad Shirwadkar void ext4_fc_stop_update(struct inode *inode);
2757aa75f4d3SHarshad Shirwadkar void ext4_fc_del(struct inode *inode);
27588016e29fSHarshad Shirwadkar bool ext4_fc_replay_check_excluded(struct super_block *sb, ext4_fsblk_t block);
27598016e29fSHarshad Shirwadkar void ext4_fc_replay_cleanup(struct super_block *sb);
2760aa75f4d3SHarshad Shirwadkar int ext4_fc_commit(journal_t *journal, tid_t commit_tid);
2761aa75f4d3SHarshad Shirwadkar int __init ext4_fc_init_dentry_cache(void);
2762aa75f4d3SHarshad Shirwadkar 
27633dcf5451SChristoph Hellwig /* mballoc.c */
2764247dbed8SChristoph Hellwig extern const struct seq_operations ext4_mb_seq_groups_ops;
27653dcf5451SChristoph Hellwig extern long ext4_mb_stats;
27663dcf5451SChristoph Hellwig extern long ext4_mb_max_to_scan;
27679d99012fSAkira Fujita extern int ext4_mb_init(struct super_block *);
27683dcf5451SChristoph Hellwig extern int ext4_mb_release(struct super_block *);
27693dcf5451SChristoph Hellwig extern ext4_fsblk_t ext4_mb_new_blocks(handle_t *,
27703dcf5451SChristoph Hellwig 				struct ext4_allocation_request *, int *);
27713dcf5451SChristoph Hellwig extern int ext4_mb_reserve_blocks(struct super_block *, int);
277227bc446eSbrookxu extern void ext4_discard_preallocations(struct inode *, unsigned int);
27735dabfc78STheodore Ts'o extern int __init ext4_init_mballoc(void);
27745dabfc78STheodore Ts'o extern void ext4_exit_mballoc(void);
27753d392b26STheodore Ts'o extern ext4_group_t ext4_mb_prefetch(struct super_block *sb,
27763d392b26STheodore Ts'o 				     ext4_group_t group,
27773d392b26STheodore Ts'o 				     unsigned int nr, int *cnt);
27783d392b26STheodore Ts'o extern void ext4_mb_prefetch_fini(struct super_block *sb, ext4_group_t group,
27793d392b26STheodore Ts'o 				  unsigned int nr);
27803d392b26STheodore Ts'o 
278144338711STheodore Ts'o extern void ext4_free_blocks(handle_t *handle, struct inode *inode,
2782e6362609STheodore Ts'o 			     struct buffer_head *bh, ext4_fsblk_t block,
2783e6362609STheodore Ts'o 			     unsigned long count, int flags);
278428623c2fSTheodore Ts'o extern int ext4_mb_alloc_groupinfo(struct super_block *sb,
278528623c2fSTheodore Ts'o 				   ext4_group_t ngroups);
2786920313a7SAneesh Kumar K.V extern int ext4_mb_add_groupinfo(struct super_block *sb,
27875f21b0e6SFrederic Bohe 		ext4_group_t i, struct ext4_group_desc *desc);
2788cc7365dfSYongqiang Yang extern int ext4_group_add_blocks(handle_t *handle, struct super_block *sb,
27892846e820SAmir Goldstein 				ext4_fsblk_t block, unsigned long count);
27907360d173SLukas Czerner extern int ext4_trim_fs(struct super_block *, struct fstrim_range *);
2791a0154344SDaeho Jeong extern void ext4_process_freed_data(struct super_block *sb, tid_t commit_tid);
27928016e29fSHarshad Shirwadkar extern void ext4_mb_mark_bb(struct super_block *sb, ext4_fsblk_t block,
27938016e29fSHarshad Shirwadkar 		       int len, int state);
27947360d173SLukas Czerner 
27953dcf5451SChristoph Hellwig /* inode.c */
27968016e29fSHarshad Shirwadkar void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw,
27978016e29fSHarshad Shirwadkar 			 struct ext4_inode_info *ei);
2798f348c252STheodore Ts'o int ext4_inode_is_fast_symlink(struct inode *inode);
279910560082STheodore Ts'o struct buffer_head *ext4_getblk(handle_t *, struct inode *, ext4_lblk_t, int);
28001c215028STheodore Ts'o struct buffer_head *ext4_bread(handle_t *, struct inode *, ext4_lblk_t, int);
28019699d4f9STahsin Erdogan int ext4_bread_batch(struct inode *inode, ext4_lblk_t block, int bh_count,
28029699d4f9STahsin Erdogan 		     bool wait, struct buffer_head **bhs);
2803705965bdSJan Kara int ext4_get_block_unwritten(struct inode *inode, sector_t iblock,
2804f19d5870STao Ma 			     struct buffer_head *bh_result, int create);
28056873fa0dSEric Sandeen int ext4_get_block(struct inode *inode, sector_t iblock,
28066873fa0dSEric Sandeen 		   struct buffer_head *bh_result, int create);
28079c3569b5STao Ma int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
28089c3569b5STao Ma 			   struct buffer_head *bh, int create);
2809f19d5870STao Ma int ext4_walk_page_buffers(handle_t *handle,
2810f19d5870STao Ma 			   struct buffer_head *head,
2811f19d5870STao Ma 			   unsigned from,
2812f19d5870STao Ma 			   unsigned to,
2813f19d5870STao Ma 			   int *partial,
2814f19d5870STao Ma 			   int (*fn)(handle_t *handle,
2815f19d5870STao Ma 				     struct buffer_head *bh));
2816f19d5870STao Ma int do_journal_get_write_access(handle_t *handle,
2817f19d5870STao Ma 				struct buffer_head *bh);
28189c3569b5STao Ma #define FALL_BACK_TO_NONDELALLOC 1
28199c3569b5STao Ma #define CONVERT_INLINE_DATA	 2
28203dcf5451SChristoph Hellwig 
28218a363970STheodore Ts'o typedef enum {
28228a363970STheodore Ts'o 	EXT4_IGET_NORMAL =	0,
28238a363970STheodore Ts'o 	EXT4_IGET_SPECIAL =	0x0001, /* OK to iget a system inode */
28248a363970STheodore Ts'o 	EXT4_IGET_HANDLE = 	0x0002	/* Inode # is from a handle */
28258a363970STheodore Ts'o } ext4_iget_flags;
28268a363970STheodore Ts'o 
28278a363970STheodore Ts'o extern struct inode *__ext4_iget(struct super_block *sb, unsigned long ino,
28288a363970STheodore Ts'o 				 ext4_iget_flags flags, const char *function,
28298a363970STheodore Ts'o 				 unsigned int line);
28308a363970STheodore Ts'o 
28318a363970STheodore Ts'o #define ext4_iget(sb, ino, flags) \
28328a363970STheodore Ts'o 	__ext4_iget((sb), (ino), (flags), __func__, __LINE__)
28338a363970STheodore Ts'o 
2834a9185b41SChristoph Hellwig extern int  ext4_write_inode(struct inode *, struct writeback_control *);
28353dcf5451SChristoph Hellwig extern int  ext4_setattr(struct dentry *, struct iattr *);
2836a528d35eSDavid Howells extern int  ext4_getattr(const struct path *, struct kstat *, u32, unsigned int);
28370930fcc1SAl Viro extern void ext4_evict_inode(struct inode *);
28380930fcc1SAl Viro extern void ext4_clear_inode(struct inode *);
283999652ea5SDavid Howells extern int  ext4_file_getattr(const struct path *, struct kstat *, u32, unsigned int);
28403dcf5451SChristoph Hellwig extern int  ext4_sync_inode(handle_t *, struct inode *);
2841aa385729SChristoph Hellwig extern void ext4_dirty_inode(struct inode *, int);
28423dcf5451SChristoph Hellwig extern int ext4_change_inode_journal_flag(struct inode *, int);
28433dcf5451SChristoph Hellwig extern int ext4_get_inode_loc(struct inode *, struct ext4_iloc *);
28448016e29fSHarshad Shirwadkar extern int ext4_get_fc_inode_loc(struct super_block *sb, unsigned long ino,
28458016e29fSHarshad Shirwadkar 			  struct ext4_iloc *iloc);
2846a361293fSJan Kara extern int ext4_inode_attach_jinode(struct inode *inode);
284791ef4cafSDuane Griffin extern int ext4_can_truncate(struct inode *inode);
28482c98eb5eSTheodore Ts'o extern int ext4_truncate(struct inode *);
2849430657b6SRoss Zwisler extern int ext4_break_layouts(struct inode *);
2850aeb2817aSAshish Sangwan extern int ext4_punch_hole(struct inode *inode, loff_t offset, loff_t length);
2851043546e4SIra Weiny extern void ext4_set_inode_flags(struct inode *, bool init);
2852ccd2506bSTheodore Ts'o extern int ext4_alloc_da_blocks(struct inode *inode);
28533dcf5451SChristoph Hellwig extern void ext4_set_aops(struct inode *inode);
28543dcf5451SChristoph Hellwig extern int ext4_writepage_trans_blocks(struct inode *);
2855f3bd1f3fSMingming Cao extern int ext4_chunk_trans_blocks(struct inode *, int nrblocks);
2856a87dd18cSLukas Czerner extern int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode,
2857a87dd18cSLukas Czerner 			     loff_t lstart, loff_t lend);
2858401b25aaSSouptick Joarder extern vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf);
2859401b25aaSSouptick Joarder extern vm_fault_t ext4_filemap_fault(struct vm_fault *vmf);
2860a9e7f447SDmitry Monakhov extern qsize_t *ext4_get_reserved_space(struct inode *inode);
2861040cb378SLi Xi extern int ext4_get_projid(struct inode *inode, kprojid_t *projid);
2862f456767dSEric Whitney extern void ext4_da_release_space(struct inode *inode, int to_free);
28635f634d06SAneesh Kumar K.V extern void ext4_da_update_reserve_space(struct inode *inode,
28645f634d06SAneesh Kumar K.V 					int used, int quota_claim);
286553085facSJan Kara extern int ext4_issue_zeroout(struct inode *inode, ext4_lblk_t lblk,
286653085facSJan Kara 			      ext4_fsblk_t pblk, ext4_lblk_t len);
2867dae1e52cSAmir Goldstein 
2868dae1e52cSAmir Goldstein /* indirect.c */
2869dae1e52cSAmir Goldstein extern int ext4_ind_map_blocks(handle_t *handle, struct inode *inode,
2870dae1e52cSAmir Goldstein 				struct ext4_map_blocks *map, int flags);
2871fa55a0edSJan Kara extern int ext4_ind_trans_blocks(struct inode *inode, int nrblocks);
2872819c4920STheodore Ts'o extern void ext4_ind_truncate(handle_t *, struct inode *inode);
28734f579ae7SLukas Czerner extern int ext4_ind_remove_space(handle_t *handle, struct inode *inode,
28744f579ae7SLukas Czerner 				 ext4_lblk_t start, ext4_lblk_t end);
2875ff9893dcSAmir Goldstein 
28763dcf5451SChristoph Hellwig /* ioctl.c */
28773dcf5451SChristoph Hellwig extern long ext4_ioctl(struct file *, unsigned int, unsigned long);
28783dcf5451SChristoph Hellwig extern long ext4_compat_ioctl(struct file *, unsigned int, unsigned long);
28798016e29fSHarshad Shirwadkar extern void ext4_reset_inode_seed(struct inode *inode);
28803dcf5451SChristoph Hellwig 
28813dcf5451SChristoph Hellwig /* migrate.c */
28822a43a878SAneesh Kumar K.V extern int ext4_ext_migrate(struct inode *);
28830d14b098SLukas Czerner extern int ext4_ind_migrate(struct inode *inode);
28843d0518f4SWei Yongjun 
28853dcf5451SChristoph Hellwig /* namei.c */
28868016e29fSHarshad Shirwadkar extern int ext4_init_new_dir(handle_t *handle, struct inode *dir,
28878016e29fSHarshad Shirwadkar 			     struct inode *inode);
2888f036adb3STheodore Ts'o extern int ext4_dirblock_csum_verify(struct inode *inode,
2889f036adb3STheodore Ts'o 				     struct buffer_head *bh);
28903dcf5451SChristoph Hellwig extern int ext4_orphan_add(handle_t *, struct inode *);
28913dcf5451SChristoph Hellwig extern int ext4_orphan_del(handle_t *, struct inode *);
28923dcf5451SChristoph Hellwig extern int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
28933dcf5451SChristoph Hellwig 				__u32 start_minor_hash, __u32 *next_hash);
28945b643f9cSTheodore Ts'o extern int ext4_search_dir(struct buffer_head *bh,
28957335cd3bSTao Ma 			   char *search_buf,
28967335cd3bSTao Ma 			   int buf_size,
28977335cd3bSTao Ma 			   struct inode *dir,
28985b643f9cSTheodore Ts'o 			   struct ext4_filename *fname,
28997335cd3bSTao Ma 			   unsigned int offset,
29007335cd3bSTao Ma 			   struct ext4_dir_entry_2 **res_dir);
29012fe34d29SKyoungho Koo extern int ext4_generic_delete_entry(struct inode *dir,
290205019a9eSTao Ma 				     struct ext4_dir_entry_2 *de_del,
290305019a9eSTao Ma 				     struct buffer_head *bh,
290405019a9eSTao Ma 				     void *entry_buf,
290505019a9eSTao Ma 				     int buf_size,
290605019a9eSTao Ma 				     int csum_size);
2907a7550b30SJaegeuk Kim extern bool ext4_empty_dir(struct inode *inode);
29083dcf5451SChristoph Hellwig 
29093dcf5451SChristoph Hellwig /* resize.c */
29101d0c3924STheodore Ts'o extern void ext4_kvfree_array_rcu(void *to_free);
29113dcf5451SChristoph Hellwig extern int ext4_group_add(struct super_block *sb,
29123dcf5451SChristoph Hellwig 				struct ext4_new_group_data *input);
29133dcf5451SChristoph Hellwig extern int ext4_group_extend(struct super_block *sb,
29143dcf5451SChristoph Hellwig 				struct ext4_super_block *es,
29153dcf5451SChristoph Hellwig 				ext4_fsblk_t n_blocks_count);
291619c5246dSYongqiang Yang extern int ext4_resize_fs(struct super_block *sb, ext4_fsblk_t n_blocks_count);
29173dcf5451SChristoph Hellwig 
29183dcf5451SChristoph Hellwig /* super.c */
2919fb265c9cSTheodore Ts'o extern struct buffer_head *ext4_sb_bread(struct super_block *sb,
2920fb265c9cSTheodore Ts'o 					 sector_t block, int op_flags);
29218394a6abSzhangyi (F) extern struct buffer_head *ext4_sb_bread_unmovable(struct super_block *sb,
29228394a6abSzhangyi (F) 						   sector_t block);
2923fa491b14Szhangyi (F) extern void ext4_read_bh_nowait(struct buffer_head *bh, int op_flags,
2924fa491b14Szhangyi (F) 				bh_end_io_t *end_io);
2925fa491b14Szhangyi (F) extern int ext4_read_bh(struct buffer_head *bh, int op_flags,
2926fa491b14Szhangyi (F) 			bh_end_io_t *end_io);
2927fa491b14Szhangyi (F) extern int ext4_read_bh_lock(struct buffer_head *bh, int op_flags, bool wait);
29285df1d412Szhangyi (F) extern void ext4_sb_breadahead_unmovable(struct super_block *sb, sector_t block);
2929ebd173beSTheodore Ts'o extern int ext4_seq_options_show(struct seq_file *seq, void *offset);
2930952fc18eSTheodore Ts'o extern int ext4_calculate_overhead(struct super_block *sb);
293106db49e6STheodore Ts'o extern void ext4_superblock_csum_set(struct super_block *sb);
2932117fff10STheodore Ts'o extern int ext4_alloc_flex_bg_array(struct super_block *sb,
2933117fff10STheodore Ts'o 				    ext4_group_t ngroup);
2934722887ddSTheodore Ts'o extern const char *ext4_decode_error(struct super_block *sb, int errno,
2935722887ddSTheodore Ts'o 				     char nbuf[16]);
2936db79e6d1SWang Shilong extern void ext4_mark_group_bitmap_corrupted(struct super_block *sb,
2937db79e6d1SWang Shilong 					     ext4_group_t block_group,
2938db79e6d1SWang Shilong 					     unsigned int flags);
2939e7c96e8eSJoe Perches 
294054d3adbcSTheodore Ts'o extern __printf(6, 7)
294154d3adbcSTheodore Ts'o void __ext4_error(struct super_block *, const char *, unsigned int, int, __u64,
2942b9075fa9SJoe Perches 		  const char *, ...);
294354d3adbcSTheodore Ts'o extern __printf(6, 7)
294454d3adbcSTheodore Ts'o void __ext4_error_inode(struct inode *, const char *, unsigned int,
294554d3adbcSTheodore Ts'o 			ext4_fsblk_t, int, const char *, ...);
2946b9075fa9SJoe Perches extern __printf(5, 6)
2947e7c96e8eSJoe Perches void __ext4_error_file(struct file *, const char *, unsigned int, ext4_fsblk_t,
2948b9075fa9SJoe Perches 		     const char *, ...);
2949c398eda0STheodore Ts'o extern void __ext4_std_error(struct super_block *, const char *,
2950c398eda0STheodore Ts'o 			     unsigned int, int);
295154d3adbcSTheodore Ts'o extern __printf(5, 6)
295254d3adbcSTheodore Ts'o void __ext4_abort(struct super_block *, const char *, unsigned int, int,
2953b9075fa9SJoe Perches 		  const char *, ...);
2954b9075fa9SJoe Perches extern __printf(4, 5)
2955b9075fa9SJoe Perches void __ext4_warning(struct super_block *, const char *, unsigned int,
2956b9075fa9SJoe Perches 		    const char *, ...);
2957b03a2f7eSAndreas Dilger extern __printf(4, 5)
2958b03a2f7eSAndreas Dilger void __ext4_warning_inode(const struct inode *inode, const char *function,
2959b03a2f7eSAndreas Dilger 			  unsigned int line, const char *fmt, ...);
2960b9075fa9SJoe Perches extern __printf(3, 4)
2961e7c96e8eSJoe Perches void __ext4_msg(struct super_block *, const char *, const char *, ...);
2962c5e06d10SJohann Lombardi extern void __dump_mmp_msg(struct super_block *, struct mmp_struct *mmp,
2963c5e06d10SJohann Lombardi 			   const char *, unsigned int, const char *);
2964b9075fa9SJoe Perches extern __printf(7, 8)
2965b9075fa9SJoe Perches void __ext4_grp_locked_error(const char *, unsigned int,
2966b9075fa9SJoe Perches 			     struct super_block *, ext4_group_t,
2967b9075fa9SJoe Perches 			     unsigned long, ext4_fsblk_t,
2968b9075fa9SJoe Perches 			     const char *, ...);
2969e7c96e8eSJoe Perches 
2970b03a2f7eSAndreas Dilger #define EXT4_ERROR_INODE(inode, fmt, a...) \
2971b03a2f7eSAndreas Dilger 	ext4_error_inode((inode), __func__, __LINE__, 0, (fmt), ## a)
2972b03a2f7eSAndreas Dilger 
297354d3adbcSTheodore Ts'o #define EXT4_ERROR_INODE_ERR(inode, err, fmt, a...)			\
297454d3adbcSTheodore Ts'o 	__ext4_error_inode((inode), __func__, __LINE__, 0, (err), (fmt), ## a)
297554d3adbcSTheodore Ts'o 
297654d3adbcSTheodore Ts'o #define ext4_error_inode_block(inode, block, err, fmt, a...)		\
297754d3adbcSTheodore Ts'o 	__ext4_error_inode((inode), __func__, __LINE__, (block), (err),	\
297854d3adbcSTheodore Ts'o 			   (fmt), ## a)
2979b03a2f7eSAndreas Dilger 
2980b03a2f7eSAndreas Dilger #define EXT4_ERROR_FILE(file, block, fmt, a...)				\
2981b03a2f7eSAndreas Dilger 	ext4_error_file((file), __func__, __LINE__, (block), (fmt), ## a)
2982b03a2f7eSAndreas Dilger 
2983e7c96e8eSJoe Perches #ifdef CONFIG_PRINTK
2984e7c96e8eSJoe Perches 
2985e7c96e8eSJoe Perches #define ext4_error_inode(inode, func, line, block, fmt, ...)		\
298654d3adbcSTheodore Ts'o 	__ext4_error_inode(inode, func, line, block, 0, fmt, ##__VA_ARGS__)
298754d3adbcSTheodore Ts'o #define ext4_error_inode_err(inode, func, line, block, err, fmt, ...)	\
298854d3adbcSTheodore Ts'o 	__ext4_error_inode((inode), (func), (line), (block), 		\
298954d3adbcSTheodore Ts'o 			   (err), (fmt), ##__VA_ARGS__)
2990e7c96e8eSJoe Perches #define ext4_error_file(file, func, line, block, fmt, ...)		\
2991e7c96e8eSJoe Perches 	__ext4_error_file(file, func, line, block, fmt, ##__VA_ARGS__)
2992e7c96e8eSJoe Perches #define ext4_error(sb, fmt, ...)					\
299354d3adbcSTheodore Ts'o 	__ext4_error((sb), __func__, __LINE__, 0, 0, (fmt), ##__VA_ARGS__)
299454d3adbcSTheodore Ts'o #define ext4_error_err(sb, err, fmt, ...)				\
299554d3adbcSTheodore Ts'o 	__ext4_error((sb), __func__, __LINE__, (err), 0, (fmt), ##__VA_ARGS__)
299654d3adbcSTheodore Ts'o #define ext4_abort(sb, err, fmt, ...)					\
299754d3adbcSTheodore Ts'o 	__ext4_abort((sb), __func__, __LINE__, (err), (fmt), ##__VA_ARGS__)
2998e7c96e8eSJoe Perches #define ext4_warning(sb, fmt, ...)					\
2999e7c96e8eSJoe Perches 	__ext4_warning(sb, __func__, __LINE__, fmt, ##__VA_ARGS__)
3000b03a2f7eSAndreas Dilger #define ext4_warning_inode(inode, fmt, ...)				\
3001b03a2f7eSAndreas Dilger 	__ext4_warning_inode(inode, __func__, __LINE__, fmt, ##__VA_ARGS__)
3002e7c96e8eSJoe Perches #define ext4_msg(sb, level, fmt, ...)				\
3003e7c96e8eSJoe Perches 	__ext4_msg(sb, level, fmt, ##__VA_ARGS__)
3004e7c96e8eSJoe Perches #define dump_mmp_msg(sb, mmp, msg)					\
3005e7c96e8eSJoe Perches 	__dump_mmp_msg(sb, mmp, __func__, __LINE__, msg)
3006e7c96e8eSJoe Perches #define ext4_grp_locked_error(sb, grp, ino, block, fmt, ...)		\
3007e7c96e8eSJoe Perches 	__ext4_grp_locked_error(__func__, __LINE__, sb, grp, ino, block, \
3008e7c96e8eSJoe Perches 				fmt, ##__VA_ARGS__)
3009e7c96e8eSJoe Perches 
3010e7c96e8eSJoe Perches #else
3011e7c96e8eSJoe Perches 
3012e7c96e8eSJoe Perches #define ext4_error_inode(inode, func, line, block, fmt, ...)		\
3013e7c96e8eSJoe Perches do {									\
3014e7c96e8eSJoe Perches 	no_printk(fmt, ##__VA_ARGS__);					\
301554d3adbcSTheodore Ts'o 	__ext4_error_inode(inode, "", 0, block, 0, " ");		\
301654d3adbcSTheodore Ts'o } while (0)
301754d3adbcSTheodore Ts'o #define ext4_error_inode_err(inode, func, line, block, err, fmt, ...)	\
301854d3adbcSTheodore Ts'o do {									\
301954d3adbcSTheodore Ts'o 	no_printk(fmt, ##__VA_ARGS__);					\
302054d3adbcSTheodore Ts'o 	__ext4_error_inode(inode, "", 0, block, err, " ");		\
3021e7c96e8eSJoe Perches } while (0)
3022e7c96e8eSJoe Perches #define ext4_error_file(file, func, line, block, fmt, ...)		\
3023e7c96e8eSJoe Perches do {									\
3024e7c96e8eSJoe Perches 	no_printk(fmt, ##__VA_ARGS__);					\
3025e7c96e8eSJoe Perches 	__ext4_error_file(file, "", 0, block, " ");			\
3026e7c96e8eSJoe Perches } while (0)
3027e7c96e8eSJoe Perches #define ext4_error(sb, fmt, ...)					\
3028e7c96e8eSJoe Perches do {									\
3029e7c96e8eSJoe Perches 	no_printk(fmt, ##__VA_ARGS__);					\
303054d3adbcSTheodore Ts'o 	__ext4_error(sb, "", 0, 0, 0, " ");				\
3031e7c96e8eSJoe Perches } while (0)
303254d3adbcSTheodore Ts'o #define ext4_error_err(sb, err, fmt, ...)				\
3033e7c96e8eSJoe Perches do {									\
3034e7c96e8eSJoe Perches 	no_printk(fmt, ##__VA_ARGS__);					\
303554d3adbcSTheodore Ts'o 	__ext4_error(sb, "", 0, err, 0, " ");				\
303654d3adbcSTheodore Ts'o } while (0)
303754d3adbcSTheodore Ts'o #define ext4_abort(sb, err, fmt, ...)					\
303854d3adbcSTheodore Ts'o do {									\
303954d3adbcSTheodore Ts'o 	no_printk(fmt, ##__VA_ARGS__);					\
304054d3adbcSTheodore Ts'o 	__ext4_abort(sb, "", 0, err, " ");				\
3041e7c96e8eSJoe Perches } while (0)
3042e7c96e8eSJoe Perches #define ext4_warning(sb, fmt, ...)					\
3043e7c96e8eSJoe Perches do {									\
3044e7c96e8eSJoe Perches 	no_printk(fmt, ##__VA_ARGS__);					\
3045e7c96e8eSJoe Perches 	__ext4_warning(sb, "", 0, " ");					\
3046e7c96e8eSJoe Perches } while (0)
3047b03a2f7eSAndreas Dilger #define ext4_warning_inode(inode, fmt, ...)				\
3048b03a2f7eSAndreas Dilger do {									\
3049b03a2f7eSAndreas Dilger 	no_printk(fmt, ##__VA_ARGS__);					\
3050b03a2f7eSAndreas Dilger 	__ext4_warning_inode(inode, "", 0, " ");			\
3051b03a2f7eSAndreas Dilger } while (0)
3052e7c96e8eSJoe Perches #define ext4_msg(sb, level, fmt, ...)					\
3053e7c96e8eSJoe Perches do {									\
3054e7c96e8eSJoe Perches 	no_printk(fmt, ##__VA_ARGS__);					\
3055e7c96e8eSJoe Perches 	__ext4_msg(sb, "", " ");					\
3056e7c96e8eSJoe Perches } while (0)
3057e7c96e8eSJoe Perches #define dump_mmp_msg(sb, mmp, msg)					\
3058e7c96e8eSJoe Perches 	__dump_mmp_msg(sb, mmp, "", 0, "")
3059e7c96e8eSJoe Perches #define ext4_grp_locked_error(sb, grp, ino, block, fmt, ...)		\
3060e7c96e8eSJoe Perches do {									\
3061e7c96e8eSJoe Perches 	no_printk(fmt, ##__VA_ARGS__);				\
3062e7c96e8eSJoe Perches 	__ext4_grp_locked_error("", 0, sb, grp, ino, block, " ");	\
3063e7c96e8eSJoe Perches } while (0)
3064e7c96e8eSJoe Perches 
3065e7c96e8eSJoe Perches #endif
3066e7c96e8eSJoe Perches 
30673dcf5451SChristoph Hellwig extern ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
30683dcf5451SChristoph Hellwig 				      struct ext4_group_desc *bg);
30693dcf5451SChristoph Hellwig extern ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
30703dcf5451SChristoph Hellwig 				      struct ext4_group_desc *bg);
30713dcf5451SChristoph Hellwig extern ext4_fsblk_t ext4_inode_table(struct super_block *sb,
30723dcf5451SChristoph Hellwig 				     struct ext4_group_desc *bg);
3073021b65bbSTheodore Ts'o extern __u32 ext4_free_group_clusters(struct super_block *sb,
3074560671a0SAneesh Kumar K.V 				      struct ext4_group_desc *bg);
3075560671a0SAneesh Kumar K.V extern __u32 ext4_free_inodes_count(struct super_block *sb,
3076560671a0SAneesh Kumar K.V 				 struct ext4_group_desc *bg);
3077560671a0SAneesh Kumar K.V extern __u32 ext4_used_dirs_count(struct super_block *sb,
3078560671a0SAneesh Kumar K.V 				struct ext4_group_desc *bg);
3079560671a0SAneesh Kumar K.V extern __u32 ext4_itable_unused_count(struct super_block *sb,
3080560671a0SAneesh Kumar K.V 				   struct ext4_group_desc *bg);
30813dcf5451SChristoph Hellwig extern void ext4_block_bitmap_set(struct super_block *sb,
30823dcf5451SChristoph Hellwig 				  struct ext4_group_desc *bg, ext4_fsblk_t blk);
30833dcf5451SChristoph Hellwig extern void ext4_inode_bitmap_set(struct super_block *sb,
30843dcf5451SChristoph Hellwig 				  struct ext4_group_desc *bg, ext4_fsblk_t blk);
30853dcf5451SChristoph Hellwig extern void ext4_inode_table_set(struct super_block *sb,
30863dcf5451SChristoph Hellwig 				 struct ext4_group_desc *bg, ext4_fsblk_t blk);
3087021b65bbSTheodore Ts'o extern void ext4_free_group_clusters_set(struct super_block *sb,
3088021b65bbSTheodore Ts'o 					 struct ext4_group_desc *bg,
3089021b65bbSTheodore Ts'o 					 __u32 count);
3090560671a0SAneesh Kumar K.V extern void ext4_free_inodes_set(struct super_block *sb,
3091560671a0SAneesh Kumar K.V 				struct ext4_group_desc *bg, __u32 count);
3092560671a0SAneesh Kumar K.V extern void ext4_used_dirs_set(struct super_block *sb,
3093560671a0SAneesh Kumar K.V 				struct ext4_group_desc *bg, __u32 count);
3094560671a0SAneesh Kumar K.V extern void ext4_itable_unused_set(struct super_block *sb,
3095560671a0SAneesh Kumar K.V 				   struct ext4_group_desc *bg, __u32 count);
3096feb0ab32SDarrick J. Wong extern int ext4_group_desc_csum_verify(struct super_block *sb, __u32 group,
3097bb23c20aSTheodore Ts'o 				       struct ext4_group_desc *gdp);
3098feb0ab32SDarrick J. Wong extern void ext4_group_desc_csum_set(struct super_block *sb, __u32 group,
3099bb23c20aSTheodore Ts'o 				     struct ext4_group_desc *gdp);
31007f511862STheodore Ts'o extern int ext4_register_li_request(struct super_block *sb,
31017f511862STheodore Ts'o 				    ext4_group_t first_not_zeroed);
31023dcf5451SChristoph Hellwig 
31039aa5d32bSDmitry Monakhov static inline int ext4_has_metadata_csum(struct super_block *sb)
31049aa5d32bSDmitry Monakhov {
3105e2b911c5SDarrick J. Wong 	WARN_ON_ONCE(ext4_has_feature_metadata_csum(sb) &&
31069aa5d32bSDmitry Monakhov 		     !EXT4_SB(sb)->s_chksum_driver);
31079aa5d32bSDmitry Monakhov 
3108dec214d0STahsin Erdogan 	return ext4_has_feature_metadata_csum(sb) &&
3109dec214d0STahsin Erdogan 	       (EXT4_SB(sb)->s_chksum_driver != NULL);
31109aa5d32bSDmitry Monakhov }
3111dec214d0STahsin Erdogan 
3112dec214d0STahsin Erdogan static inline int ext4_has_group_desc_csum(struct super_block *sb)
3113dec214d0STahsin Erdogan {
3114dec214d0STahsin Erdogan 	return ext4_has_feature_gdt_csum(sb) || ext4_has_metadata_csum(sb);
3115dec214d0STahsin Erdogan }
3116dec214d0STahsin Erdogan 
311781e8c3c5SPetr Malat #define ext4_read_incompat_64bit_val(es, name) \
311881e8c3c5SPetr Malat 	(((es)->s_feature_incompat & cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT) \
311981e8c3c5SPetr Malat 		? (ext4_fsblk_t)le32_to_cpu(es->name##_hi) << 32 : 0) | \
312081e8c3c5SPetr Malat 		le32_to_cpu(es->name##_lo))
312181e8c3c5SPetr Malat 
31223dcf5451SChristoph Hellwig static inline ext4_fsblk_t ext4_blocks_count(struct ext4_super_block *es)
31233dcf5451SChristoph Hellwig {
312481e8c3c5SPetr Malat 	return ext4_read_incompat_64bit_val(es, s_blocks_count);
31253dcf5451SChristoph Hellwig }
31263dcf5451SChristoph Hellwig 
31273dcf5451SChristoph Hellwig static inline ext4_fsblk_t ext4_r_blocks_count(struct ext4_super_block *es)
31283dcf5451SChristoph Hellwig {
312981e8c3c5SPetr Malat 	return ext4_read_incompat_64bit_val(es, s_r_blocks_count);
31303dcf5451SChristoph Hellwig }
31313dcf5451SChristoph Hellwig 
31323dcf5451SChristoph Hellwig static inline ext4_fsblk_t ext4_free_blocks_count(struct ext4_super_block *es)
31333dcf5451SChristoph Hellwig {
313481e8c3c5SPetr Malat 	return ext4_read_incompat_64bit_val(es, s_free_blocks_count);
31353dcf5451SChristoph Hellwig }
31363dcf5451SChristoph Hellwig 
31373dcf5451SChristoph Hellwig static inline void ext4_blocks_count_set(struct ext4_super_block *es,
31383dcf5451SChristoph Hellwig 					 ext4_fsblk_t blk)
31393dcf5451SChristoph Hellwig {
31403dcf5451SChristoph Hellwig 	es->s_blocks_count_lo = cpu_to_le32((u32)blk);
31413dcf5451SChristoph Hellwig 	es->s_blocks_count_hi = cpu_to_le32(blk >> 32);
31423dcf5451SChristoph Hellwig }
31433dcf5451SChristoph Hellwig 
31443dcf5451SChristoph Hellwig static inline void ext4_free_blocks_count_set(struct ext4_super_block *es,
31453dcf5451SChristoph Hellwig 					      ext4_fsblk_t blk)
31463dcf5451SChristoph Hellwig {
31473dcf5451SChristoph Hellwig 	es->s_free_blocks_count_lo = cpu_to_le32((u32)blk);
31483dcf5451SChristoph Hellwig 	es->s_free_blocks_count_hi = cpu_to_le32(blk >> 32);
31493dcf5451SChristoph Hellwig }
31503dcf5451SChristoph Hellwig 
31513dcf5451SChristoph Hellwig static inline void ext4_r_blocks_count_set(struct ext4_super_block *es,
31523dcf5451SChristoph Hellwig 					   ext4_fsblk_t blk)
31533dcf5451SChristoph Hellwig {
31543dcf5451SChristoph Hellwig 	es->s_r_blocks_count_lo = cpu_to_le32((u32)blk);
31553dcf5451SChristoph Hellwig 	es->s_r_blocks_count_hi = cpu_to_le32(blk >> 32);
31563dcf5451SChristoph Hellwig }
31573dcf5451SChristoph Hellwig 
3158e08ac99fSArtem Blagodarenko static inline loff_t ext4_isize(struct super_block *sb,
3159e08ac99fSArtem Blagodarenko 				struct ext4_inode *raw_inode)
31603dcf5451SChristoph Hellwig {
3161e08ac99fSArtem Blagodarenko 	if (ext4_has_feature_largedir(sb) ||
3162e08ac99fSArtem Blagodarenko 	    S_ISREG(le16_to_cpu(raw_inode->i_mode)))
31633dcf5451SChristoph Hellwig 		return ((loff_t)le32_to_cpu(raw_inode->i_size_high) << 32) |
31643dcf5451SChristoph Hellwig 			le32_to_cpu(raw_inode->i_size_lo);
3165e08ac99fSArtem Blagodarenko 
316606a279d6STheodore Ts'o 	return (loff_t) le32_to_cpu(raw_inode->i_size_lo);
31673dcf5451SChristoph Hellwig }
31683dcf5451SChristoph Hellwig 
31693dcf5451SChristoph Hellwig static inline void ext4_isize_set(struct ext4_inode *raw_inode, loff_t i_size)
31703dcf5451SChristoph Hellwig {
31713dcf5451SChristoph Hellwig 	raw_inode->i_size_lo = cpu_to_le32(i_size);
31723dcf5451SChristoph Hellwig 	raw_inode->i_size_high = cpu_to_le32(i_size >> 32);
31733dcf5451SChristoph Hellwig }
31743dcf5451SChristoph Hellwig 
31753dcf5451SChristoph Hellwig static inline
31763dcf5451SChristoph Hellwig struct ext4_group_info *ext4_get_group_info(struct super_block *sb,
31773dcf5451SChristoph Hellwig 					    ext4_group_t group)
31783dcf5451SChristoph Hellwig {
3179df3da4eaSSuraj Jitindar Singh 	 struct ext4_group_info **grp_info;
31803dcf5451SChristoph Hellwig 	 long indexv, indexh;
31812f2e09ebSTheodore Ts'o 	 BUG_ON(group >= EXT4_SB(sb)->s_groups_count);
31823dcf5451SChristoph Hellwig 	 indexv = group >> (EXT4_DESC_PER_BLOCK_BITS(sb));
31833dcf5451SChristoph Hellwig 	 indexh = group & ((EXT4_DESC_PER_BLOCK(sb)) - 1);
3184df3da4eaSSuraj Jitindar Singh 	 grp_info = sbi_array_rcu_deref(EXT4_SB(sb), s_group_info, indexv);
3185df3da4eaSSuraj Jitindar Singh 	 return grp_info[indexh];
31863dcf5451SChristoph Hellwig }
31873dcf5451SChristoph Hellwig 
31888df9675fSTheodore Ts'o /*
31898df9675fSTheodore Ts'o  * Reading s_groups_count requires using smp_rmb() afterwards.  See
31908df9675fSTheodore Ts'o  * the locking protocol documented in the comments of ext4_group_add()
31918df9675fSTheodore Ts'o  * in resize.c
31928df9675fSTheodore Ts'o  */
31938df9675fSTheodore Ts'o static inline ext4_group_t ext4_get_groups_count(struct super_block *sb)
31948df9675fSTheodore Ts'o {
31958df9675fSTheodore Ts'o 	ext4_group_t	ngroups = EXT4_SB(sb)->s_groups_count;
31968df9675fSTheodore Ts'o 
31978df9675fSTheodore Ts'o 	smp_rmb();
31988df9675fSTheodore Ts'o 	return ngroups;
31998df9675fSTheodore Ts'o }
32003dcf5451SChristoph Hellwig 
3201772cb7c8SJose R. Santos static inline ext4_group_t ext4_flex_group(struct ext4_sb_info *sbi,
3202772cb7c8SJose R. Santos 					     ext4_group_t block_group)
3203772cb7c8SJose R. Santos {
3204772cb7c8SJose R. Santos 	return block_group >> sbi->s_log_groups_per_flex;
3205772cb7c8SJose R. Santos }
3206772cb7c8SJose R. Santos 
3207772cb7c8SJose R. Santos static inline unsigned int ext4_flex_bg_size(struct ext4_sb_info *sbi)
3208772cb7c8SJose R. Santos {
3209772cb7c8SJose R. Santos 	return 1 << sbi->s_log_groups_per_flex;
3210772cb7c8SJose R. Santos }
3211772cb7c8SJose R. Santos 
32123dcf5451SChristoph Hellwig #define ext4_std_error(sb, errno)				\
32133dcf5451SChristoph Hellwig do {								\
32143dcf5451SChristoph Hellwig 	if ((errno))						\
3215c398eda0STheodore Ts'o 		__ext4_std_error((sb), __func__, __LINE__, (errno));	\
32163dcf5451SChristoph Hellwig } while (0)
32173dcf5451SChristoph Hellwig 
3218a30d542aSAneesh Kumar K.V #ifdef CONFIG_SMP
3219df55c99dSTheodore Ts'o /* Each CPU can accumulate percpu_counter_batch clusters in their local
3220df55c99dSTheodore Ts'o  * counters. So we need to make sure we have free clusters more
3221179f7ebfSEric Dumazet  * than percpu_counter_batch  * nr_cpu_ids. Also add a window of 4 times.
3222a30d542aSAneesh Kumar K.V  */
3223df55c99dSTheodore Ts'o #define EXT4_FREECLUSTERS_WATERMARK (4 * (percpu_counter_batch * nr_cpu_ids))
3224a30d542aSAneesh Kumar K.V #else
3225df55c99dSTheodore Ts'o #define EXT4_FREECLUSTERS_WATERMARK 0
3226a30d542aSAneesh Kumar K.V #endif
3227a30d542aSAneesh Kumar K.V 
322890e775b7SJan Kara /* Update i_disksize. Requires i_mutex to avoid races with truncate */
3229cf17fea6SAneesh Kumar K.V static inline void ext4_update_i_disksize(struct inode *inode, loff_t newsize)
3230cf17fea6SAneesh Kumar K.V {
323190e775b7SJan Kara 	WARN_ON_ONCE(S_ISREG(inode->i_mode) &&
32325955102cSAl Viro 		     !inode_is_locked(inode));
3233cf17fea6SAneesh Kumar K.V 	down_write(&EXT4_I(inode)->i_data_sem);
3234cf17fea6SAneesh Kumar K.V 	if (newsize > EXT4_I(inode)->i_disksize)
323535df4299SQian Cai 		WRITE_ONCE(EXT4_I(inode)->i_disksize, newsize);
3236cf17fea6SAneesh Kumar K.V 	up_write(&EXT4_I(inode)->i_data_sem);
323790e775b7SJan Kara }
323890e775b7SJan Kara 
32394631dbf6SDmitry Monakhov /* Update i_size, i_disksize. Requires i_mutex to avoid races with truncate */
32404631dbf6SDmitry Monakhov static inline int ext4_update_inode_size(struct inode *inode, loff_t newsize)
32414631dbf6SDmitry Monakhov {
32424631dbf6SDmitry Monakhov 	int changed = 0;
32434631dbf6SDmitry Monakhov 
32444631dbf6SDmitry Monakhov 	if (newsize > inode->i_size) {
32454631dbf6SDmitry Monakhov 		i_size_write(inode, newsize);
32464631dbf6SDmitry Monakhov 		changed = 1;
32474631dbf6SDmitry Monakhov 	}
32484631dbf6SDmitry Monakhov 	if (newsize > EXT4_I(inode)->i_disksize) {
32494631dbf6SDmitry Monakhov 		ext4_update_i_disksize(inode, newsize);
32504631dbf6SDmitry Monakhov 		changed |= 2;
32514631dbf6SDmitry Monakhov 	}
32524631dbf6SDmitry Monakhov 	return changed;
32534631dbf6SDmitry Monakhov }
32544631dbf6SDmitry Monakhov 
325501127848SJan Kara int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset,
325601127848SJan Kara 				      loff_t len);
325701127848SJan Kara 
32585d1b1b3fSAneesh Kumar K.V struct ext4_group_info {
32595d1b1b3fSAneesh Kumar K.V 	unsigned long   bb_state;
3260addd752cSChunguang Xu #ifdef AGGRESSIVE_CHECK
3261addd752cSChunguang Xu 	unsigned long	bb_check_counter;
3262addd752cSChunguang Xu #endif
32635d1b1b3fSAneesh Kumar K.V 	struct rb_root  bb_free_root;
3264a36b4498SEric Sandeen 	ext4_grpblk_t	bb_first_free;	/* first free block */
3265a36b4498SEric Sandeen 	ext4_grpblk_t	bb_free;	/* total free blocks */
3266a36b4498SEric Sandeen 	ext4_grpblk_t	bb_fragments;	/* nr of freespace fragments */
32678a57d9d6SCurt Wohlgemuth 	ext4_grpblk_t	bb_largest_free_order;/* order of largest frag in BG */
32685d1b1b3fSAneesh Kumar K.V 	struct          list_head bb_prealloc_list;
32695d1b1b3fSAneesh Kumar K.V #ifdef DOUBLE_CHECK
32705d1b1b3fSAneesh Kumar K.V 	void            *bb_bitmap;
32715d1b1b3fSAneesh Kumar K.V #endif
32725d1b1b3fSAneesh Kumar K.V 	struct rw_semaphore alloc_sem;
3273a36b4498SEric Sandeen 	ext4_grpblk_t	bb_counters[];	/* Nr of free power-of-two-block
3274a36b4498SEric Sandeen 					 * regions, index is order.
3275a36b4498SEric Sandeen 					 * bb_counters[3] = 5 means
3276a36b4498SEric Sandeen 					 * 5 free 8-block regions. */
32775d1b1b3fSAneesh Kumar K.V };
32785d1b1b3fSAneesh Kumar K.V 
32795d1b1b3fSAneesh Kumar K.V #define EXT4_GROUP_INFO_NEED_INIT_BIT		0
32803d56b8d2STao Ma #define EXT4_GROUP_INFO_WAS_TRIMMED_BIT		1
3281163a203dSDarrick J. Wong #define EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT	2
328287a39389SDarrick J. Wong #define EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT	3
3283db79e6d1SWang Shilong #define EXT4_GROUP_INFO_BBITMAP_CORRUPT		\
3284db79e6d1SWang Shilong 	(1 << EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT)
3285db79e6d1SWang Shilong #define EXT4_GROUP_INFO_IBITMAP_CORRUPT		\
3286db79e6d1SWang Shilong 	(1 << EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT)
3287cfd73237SAlex Zhuravlev #define EXT4_GROUP_INFO_BBITMAP_READ_BIT	4
32885d1b1b3fSAneesh Kumar K.V 
32895d1b1b3fSAneesh Kumar K.V #define EXT4_MB_GRP_NEED_INIT(grp)	\
32905d1b1b3fSAneesh Kumar K.V 	(test_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &((grp)->bb_state)))
3291163a203dSDarrick J. Wong #define EXT4_MB_GRP_BBITMAP_CORRUPT(grp)	\
3292163a203dSDarrick J. Wong 	(test_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT, &((grp)->bb_state)))
329387a39389SDarrick J. Wong #define EXT4_MB_GRP_IBITMAP_CORRUPT(grp)	\
329487a39389SDarrick J. Wong 	(test_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &((grp)->bb_state)))
32955d1b1b3fSAneesh Kumar K.V 
32963d56b8d2STao Ma #define EXT4_MB_GRP_WAS_TRIMMED(grp)	\
32973d56b8d2STao Ma 	(test_bit(EXT4_GROUP_INFO_WAS_TRIMMED_BIT, &((grp)->bb_state)))
32983d56b8d2STao Ma #define EXT4_MB_GRP_SET_TRIMMED(grp)	\
32993d56b8d2STao Ma 	(set_bit(EXT4_GROUP_INFO_WAS_TRIMMED_BIT, &((grp)->bb_state)))
33003d56b8d2STao Ma #define EXT4_MB_GRP_CLEAR_TRIMMED(grp)	\
33013d56b8d2STao Ma 	(clear_bit(EXT4_GROUP_INFO_WAS_TRIMMED_BIT, &((grp)->bb_state)))
3302cfd73237SAlex Zhuravlev #define EXT4_MB_GRP_TEST_AND_SET_READ(grp)	\
3303cfd73237SAlex Zhuravlev 	(test_and_set_bit(EXT4_GROUP_INFO_BBITMAP_READ_BIT, &((grp)->bb_state)))
33043d56b8d2STao Ma 
330550797481STheodore Ts'o #define EXT4_MAX_CONTENTION		8
330650797481STheodore Ts'o #define EXT4_CONTENTION_THRESHOLD	2
330750797481STheodore Ts'o 
3308955ce5f5SAneesh Kumar K.V static inline spinlock_t *ext4_group_lock_ptr(struct super_block *sb,
3309955ce5f5SAneesh Kumar K.V 					      ext4_group_t group)
3310955ce5f5SAneesh Kumar K.V {
3311955ce5f5SAneesh Kumar K.V 	return bgl_lock_ptr(EXT4_SB(sb)->s_blockgroup_lock, group);
3312955ce5f5SAneesh Kumar K.V }
3313955ce5f5SAneesh Kumar K.V 
331450797481STheodore Ts'o /*
331550797481STheodore Ts'o  * Returns true if the filesystem is busy enough that attempts to
331650797481STheodore Ts'o  * access the block group locks has run into contention.
331750797481STheodore Ts'o  */
331850797481STheodore Ts'o static inline int ext4_fs_is_busy(struct ext4_sb_info *sbi)
331950797481STheodore Ts'o {
332050797481STheodore Ts'o 	return (atomic_read(&sbi->s_lock_busy) > EXT4_CONTENTION_THRESHOLD);
332150797481STheodore Ts'o }
332250797481STheodore Ts'o 
33235d1b1b3fSAneesh Kumar K.V static inline void ext4_lock_group(struct super_block *sb, ext4_group_t group)
33245d1b1b3fSAneesh Kumar K.V {
332550797481STheodore Ts'o 	spinlock_t *lock = ext4_group_lock_ptr(sb, group);
332650797481STheodore Ts'o 	if (spin_trylock(lock))
332750797481STheodore Ts'o 		/*
332850797481STheodore Ts'o 		 * We're able to grab the lock right away, so drop the
332950797481STheodore Ts'o 		 * lock contention counter.
333050797481STheodore Ts'o 		 */
333150797481STheodore Ts'o 		atomic_add_unless(&EXT4_SB(sb)->s_lock_busy, -1, 0);
333250797481STheodore Ts'o 	else {
333350797481STheodore Ts'o 		/*
333450797481STheodore Ts'o 		 * The lock is busy, so bump the contention counter,
333550797481STheodore Ts'o 		 * and then wait on the spin lock.
333650797481STheodore Ts'o 		 */
333750797481STheodore Ts'o 		atomic_add_unless(&EXT4_SB(sb)->s_lock_busy, 1,
333850797481STheodore Ts'o 				  EXT4_MAX_CONTENTION);
333950797481STheodore Ts'o 		spin_lock(lock);
334050797481STheodore Ts'o 	}
33415d1b1b3fSAneesh Kumar K.V }
33425d1b1b3fSAneesh Kumar K.V 
33435d1b1b3fSAneesh Kumar K.V static inline void ext4_unlock_group(struct super_block *sb,
33445d1b1b3fSAneesh Kumar K.V 					ext4_group_t group)
33455d1b1b3fSAneesh Kumar K.V {
3346955ce5f5SAneesh Kumar K.V 	spin_unlock(ext4_group_lock_ptr(sb, group));
33475d1b1b3fSAneesh Kumar K.V }
33485d1b1b3fSAneesh Kumar K.V 
33493dcf5451SChristoph Hellwig /*
33501f7d1e77STheodore Ts'o  * Block validity checking
33511f7d1e77STheodore Ts'o  */
33521f7d1e77STheodore Ts'o #define ext4_check_indirect_blockref(inode, bh)				\
33531f7d1e77STheodore Ts'o 	ext4_check_blockref(__func__, __LINE__, inode,			\
33541f7d1e77STheodore Ts'o 			    (__le32 *)(bh)->b_data,			\
33551f7d1e77STheodore Ts'o 			    EXT4_ADDR_PER_BLOCK((inode)->i_sb))
33561f7d1e77STheodore Ts'o 
33571f7d1e77STheodore Ts'o #define ext4_ind_check_inode(inode)					\
33581f7d1e77STheodore Ts'o 	ext4_check_blockref(__func__, __LINE__, inode,			\
33591f7d1e77STheodore Ts'o 			    EXT4_I(inode)->i_data,			\
33601f7d1e77STheodore Ts'o 			    EXT4_NDIR_BLOCKS)
33611f7d1e77STheodore Ts'o 
33621f7d1e77STheodore Ts'o /*
33633dcf5451SChristoph Hellwig  * Inodes and files operations
33643dcf5451SChristoph Hellwig  */
33653dcf5451SChristoph Hellwig 
33663dcf5451SChristoph Hellwig /* dir.c */
33673dcf5451SChristoph Hellwig extern const struct file_operations ext4_dir_operations;
33683dcf5451SChristoph Hellwig 
3369b886ee3eSGabriel Krisman Bertazi #ifdef CONFIG_UNICODE
3370b886ee3eSGabriel Krisman Bertazi extern const struct dentry_operations ext4_dentry_ops;
3371b886ee3eSGabriel Krisman Bertazi #endif
3372b886ee3eSGabriel Krisman Bertazi 
33733dcf5451SChristoph Hellwig /* file.c */
33743dcf5451SChristoph Hellwig extern const struct inode_operations ext4_file_inode_operations;
33753dcf5451SChristoph Hellwig extern const struct file_operations ext4_file_operations;
3376e0d10bfaSToshiyuki Okajima extern loff_t ext4_llseek(struct file *file, loff_t offset, int origin);
33773dcf5451SChristoph Hellwig 
337895eaefbdSTheodore Ts'o /* inline.c */
337995eaefbdSTheodore Ts'o extern int ext4_get_max_inline_size(struct inode *inode);
338095eaefbdSTheodore Ts'o extern int ext4_find_inline_data_nolock(struct inode *inode);
338195eaefbdSTheodore Ts'o extern int ext4_init_inline_data(handle_t *handle, struct inode *inode,
338295eaefbdSTheodore Ts'o 				 unsigned int len);
338395eaefbdSTheodore Ts'o extern int ext4_destroy_inline_data(handle_t *handle, struct inode *inode);
338495eaefbdSTheodore Ts'o 
338595eaefbdSTheodore Ts'o extern int ext4_readpage_inline(struct inode *inode, struct page *page);
338695eaefbdSTheodore Ts'o extern int ext4_try_to_write_inline_data(struct address_space *mapping,
338795eaefbdSTheodore Ts'o 					 struct inode *inode,
338895eaefbdSTheodore Ts'o 					 loff_t pos, unsigned len,
338995eaefbdSTheodore Ts'o 					 unsigned flags,
339095eaefbdSTheodore Ts'o 					 struct page **pagep);
339195eaefbdSTheodore Ts'o extern int ext4_write_inline_data_end(struct inode *inode,
339295eaefbdSTheodore Ts'o 				      loff_t pos, unsigned len,
339395eaefbdSTheodore Ts'o 				      unsigned copied,
339495eaefbdSTheodore Ts'o 				      struct page *page);
339595eaefbdSTheodore Ts'o extern struct buffer_head *
339695eaefbdSTheodore Ts'o ext4_journalled_write_inline_data(struct inode *inode,
339795eaefbdSTheodore Ts'o 				  unsigned len,
339895eaefbdSTheodore Ts'o 				  struct page *page);
339995eaefbdSTheodore Ts'o extern int ext4_da_write_inline_data_begin(struct address_space *mapping,
340095eaefbdSTheodore Ts'o 					   struct inode *inode,
340195eaefbdSTheodore Ts'o 					   loff_t pos, unsigned len,
340295eaefbdSTheodore Ts'o 					   unsigned flags,
340395eaefbdSTheodore Ts'o 					   struct page **pagep,
340495eaefbdSTheodore Ts'o 					   void **fsdata);
340595eaefbdSTheodore Ts'o extern int ext4_da_write_inline_data_end(struct inode *inode, loff_t pos,
340695eaefbdSTheodore Ts'o 					 unsigned len, unsigned copied,
340795eaefbdSTheodore Ts'o 					 struct page *page);
34085b643f9cSTheodore Ts'o extern int ext4_try_add_inline_entry(handle_t *handle,
34095b643f9cSTheodore Ts'o 				     struct ext4_filename *fname,
341056a04915STheodore Ts'o 				     struct inode *dir, struct inode *inode);
341195eaefbdSTheodore Ts'o extern int ext4_try_create_inline_dir(handle_t *handle,
341295eaefbdSTheodore Ts'o 				      struct inode *parent,
341395eaefbdSTheodore Ts'o 				      struct inode *inode);
341495eaefbdSTheodore Ts'o extern int ext4_read_inline_dir(struct file *filp,
3415725bebb2SAl Viro 				struct dir_context *ctx,
341695eaefbdSTheodore Ts'o 				int *has_inline_data);
34177633b08bSTheodore Ts'o extern int ext4_inlinedir_to_tree(struct file *dir_file,
34188af0f082STao Ma 				  struct inode *dir, ext4_lblk_t block,
34198af0f082STao Ma 				  struct dx_hash_info *hinfo,
34208af0f082STao Ma 				  __u32 start_hash, __u32 start_minor_hash,
34218af0f082STao Ma 				  int *has_inline_data);
342295eaefbdSTheodore Ts'o extern struct buffer_head *ext4_find_inline_entry(struct inode *dir,
34235b643f9cSTheodore Ts'o 					struct ext4_filename *fname,
342495eaefbdSTheodore Ts'o 					struct ext4_dir_entry_2 **res_dir,
342595eaefbdSTheodore Ts'o 					int *has_inline_data);
342695eaefbdSTheodore Ts'o extern int ext4_delete_inline_entry(handle_t *handle,
342795eaefbdSTheodore Ts'o 				    struct inode *dir,
342895eaefbdSTheodore Ts'o 				    struct ext4_dir_entry_2 *de_del,
342995eaefbdSTheodore Ts'o 				    struct buffer_head *bh,
343095eaefbdSTheodore Ts'o 				    int *has_inline_data);
3431a7550b30SJaegeuk Kim extern bool empty_inline_dir(struct inode *dir, int *has_inline_data);
343295eaefbdSTheodore Ts'o extern struct buffer_head *ext4_get_first_inline_block(struct inode *inode,
343395eaefbdSTheodore Ts'o 					struct ext4_dir_entry_2 **parent_de,
343495eaefbdSTheodore Ts'o 					int *retval);
343595eaefbdSTheodore Ts'o extern int ext4_inline_data_fiemap(struct inode *inode,
343695eaefbdSTheodore Ts'o 				   struct fiemap_extent_info *fieinfo,
3437d952d69eSDmitry Monakhov 				   int *has_inline, __u64 start, __u64 len);
34387046ae35SAndreas Gruenbacher 
34397046ae35SAndreas Gruenbacher struct iomap;
34407046ae35SAndreas Gruenbacher extern int ext4_inline_data_iomap(struct inode *inode, struct iomap *iomap);
34417046ae35SAndreas Gruenbacher 
344201daf945STheodore Ts'o extern int ext4_inline_data_truncate(struct inode *inode, int *has_inline);
344395eaefbdSTheodore Ts'o 
344495eaefbdSTheodore Ts'o extern int ext4_convert_inline_data(struct inode *inode);
344595eaefbdSTheodore Ts'o 
344683447ccbSZheng Liu static inline int ext4_has_inline_data(struct inode *inode)
344783447ccbSZheng Liu {
344883447ccbSZheng Liu 	return ext4_test_inode_flag(inode, EXT4_INODE_INLINE_DATA) &&
344983447ccbSZheng Liu 	       EXT4_I(inode)->i_inline_off;
345083447ccbSZheng Liu }
345183447ccbSZheng Liu 
34523dcf5451SChristoph Hellwig /* namei.c */
34533dcf5451SChristoph Hellwig extern const struct inode_operations ext4_dir_inode_operations;
34543dcf5451SChristoph Hellwig extern const struct inode_operations ext4_special_inode_operations;
3455596397b7STheodore Ts'o extern struct dentry *ext4_get_parent(struct dentry *child);
3456a774f9c2STao Ma extern struct ext4_dir_entry_2 *ext4_init_dot_dotdot(struct inode *inode,
3457a774f9c2STao Ma 				 struct ext4_dir_entry_2 *de,
3458a774f9c2STao Ma 				 int blocksize, int csum_size,
3459a774f9c2STao Ma 				 unsigned int parent_ino, int dotdot_real_len);
3460ddce3b94STheodore Ts'o extern void ext4_initialize_dirent_tail(struct buffer_head *bh,
34613c47d541STao Ma 					unsigned int blocksize);
3462f036adb3STheodore Ts'o extern int ext4_handle_dirty_dirblock(handle_t *handle, struct inode *inode,
34633c47d541STao Ma 				      struct buffer_head *bh);
3464b886ee3eSGabriel Krisman Bertazi extern int ext4_ci_compare(const struct inode *parent,
34653ae72562SGabriel Krisman Bertazi 			   const struct qstr *fname,
34663ae72562SGabriel Krisman Bertazi 			   const struct qstr *entry, bool quick);
34678016e29fSHarshad Shirwadkar extern int __ext4_unlink(struct inode *dir, const struct qstr *d_name,
34688016e29fSHarshad Shirwadkar 			 struct inode *inode);
34698016e29fSHarshad Shirwadkar extern int __ext4_link(struct inode *dir, struct inode *inode,
34708016e29fSHarshad Shirwadkar 		       struct dentry *dentry);
3471b886ee3eSGabriel Krisman Bertazi 
34728af0f082STao Ma #define S_SHIFT 12
3473381cebfeSDan Carpenter static const unsigned char ext4_type_by_mode[(S_IFMT >> S_SHIFT) + 1] = {
34748af0f082STao Ma 	[S_IFREG >> S_SHIFT]	= EXT4_FT_REG_FILE,
34758af0f082STao Ma 	[S_IFDIR >> S_SHIFT]	= EXT4_FT_DIR,
34768af0f082STao Ma 	[S_IFCHR >> S_SHIFT]	= EXT4_FT_CHRDEV,
34778af0f082STao Ma 	[S_IFBLK >> S_SHIFT]	= EXT4_FT_BLKDEV,
34788af0f082STao Ma 	[S_IFIFO >> S_SHIFT]	= EXT4_FT_FIFO,
34798af0f082STao Ma 	[S_IFSOCK >> S_SHIFT]	= EXT4_FT_SOCK,
34808af0f082STao Ma 	[S_IFLNK >> S_SHIFT]	= EXT4_FT_SYMLINK,
34818af0f082STao Ma };
34828af0f082STao Ma 
34838af0f082STao Ma static inline void ext4_set_de_type(struct super_block *sb,
34848af0f082STao Ma 				struct ext4_dir_entry_2 *de,
34858af0f082STao Ma 				umode_t mode) {
3486e2b911c5SDarrick J. Wong 	if (ext4_has_feature_filetype(sb))
34878af0f082STao Ma 		de->file_type = ext4_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
34888af0f082STao Ma }
34898af0f082STao Ma 
3490f64e02feSTheodore Ts'o /* readpages.c */
3491a07f624bSMatthew Wilcox (Oracle) extern int ext4_mpage_readpages(struct inode *inode,
34926311f91fSMatthew Wilcox (Oracle) 		struct readahead_control *rac, struct page *page);
349322cfe4b4SEric Biggers extern int __init ext4_init_post_read_processing(void);
349422cfe4b4SEric Biggers extern void ext4_exit_post_read_processing(void);
34953dcf5451SChristoph Hellwig 
34963dcf5451SChristoph Hellwig /* symlink.c */
3497a7a67e8aSAl Viro extern const struct inode_operations ext4_encrypted_symlink_inode_operations;
34983dcf5451SChristoph Hellwig extern const struct inode_operations ext4_symlink_inode_operations;
34993dcf5451SChristoph Hellwig extern const struct inode_operations ext4_fast_symlink_inode_operations;
35003dcf5451SChristoph Hellwig 
3501b5799018STheodore Ts'o /* sysfs.c */
3502b5799018STheodore Ts'o extern int ext4_register_sysfs(struct super_block *sb);
3503ebd173beSTheodore Ts'o extern void ext4_unregister_sysfs(struct super_block *sb);
3504b5799018STheodore Ts'o extern int __init ext4_init_sysfs(void);
3505b5799018STheodore Ts'o extern void ext4_exit_sysfs(void);
3506b5799018STheodore Ts'o 
35076fd058f7STheodore Ts'o /* block_validity */
35086fd058f7STheodore Ts'o extern void ext4_release_system_zone(struct super_block *sb);
35096fd058f7STheodore Ts'o extern int ext4_setup_system_zone(struct super_block *sb);
35105dabfc78STheodore Ts'o extern int __init ext4_init_system_zone(void);
35115dabfc78STheodore Ts'o extern void ext4_exit_system_zone(void);
3512ce9f24ccSJan Kara extern int ext4_inode_block_valid(struct inode *inode,
35136fd058f7STheodore Ts'o 				  ext4_fsblk_t start_blk,
35146fd058f7STheodore Ts'o 				  unsigned int count);
35151f7d1e77STheodore Ts'o extern int ext4_check_blockref(const char *, unsigned int,
35161f7d1e77STheodore Ts'o 			       struct inode *, __le32 *, unsigned int);
35176fd058f7STheodore Ts'o 
35183dcf5451SChristoph Hellwig /* extents.c */
35194a092d73STheodore Ts'o struct ext4_ext_path;
35204a092d73STheodore Ts'o struct ext4_extent;
35214a092d73STheodore Ts'o 
3522d7b2a00cSZheng Liu /*
3523d7b2a00cSZheng Liu  * Maximum number of logical blocks in a file; ext4_extent's ee_block is
3524d7b2a00cSZheng Liu  * __le32.
3525d7b2a00cSZheng Liu  */
3526d7b2a00cSZheng Liu #define EXT_MAX_BLOCKS	0xffffffff
3527d7b2a00cSZheng Liu 
35284209ae12SHarshad Shirwadkar extern void ext4_ext_tree_init(handle_t *handle, struct inode *inode);
3529fffb2739SJan Kara extern int ext4_ext_index_trans_blocks(struct inode *inode, int extents);
3530e35fd660STheodore Ts'o extern int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
3531e35fd660STheodore Ts'o 			       struct ext4_map_blocks *map, int flags);
3532d0abb36dSTheodore Ts'o extern int ext4_ext_truncate(handle_t *, struct inode *);
353326a4c0c6STheodore Ts'o extern int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
353426a4c0c6STheodore Ts'o 				 ext4_lblk_t end);
35353dcf5451SChristoph Hellwig extern void ext4_ext_init(struct super_block *);
35363dcf5451SChristoph Hellwig extern void ext4_ext_release(struct super_block *);
35372fe17c10SChristoph Hellwig extern long ext4_fallocate(struct file *file, int mode, loff_t offset,
35383dcf5451SChristoph Hellwig 			  loff_t len);
35396b523df4SJan Kara extern int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode,
35406b523df4SJan Kara 					  loff_t offset, ssize_t len);
3541a00713eaSRitesh Harjani extern int ext4_convert_unwritten_io_end_vec(handle_t *handle,
3542a00713eaSRitesh Harjani 					     ext4_io_end_t *io_end);
3543e35fd660STheodore Ts'o extern int ext4_map_blocks(handle_t *handle, struct inode *inode,
3544e35fd660STheodore Ts'o 			   struct ext4_map_blocks *map, int flags);
35454a092d73STheodore Ts'o extern int ext4_ext_calc_credits_for_single_extent(struct inode *inode,
35464a092d73STheodore Ts'o 						   int num,
35474a092d73STheodore Ts'o 						   struct ext4_ext_path *path);
35484a092d73STheodore Ts'o extern int ext4_ext_insert_extent(handle_t *, struct inode *,
3549dfe50809STheodore Ts'o 				  struct ext4_ext_path **,
35504a092d73STheodore Ts'o 				  struct ext4_extent *, int);
3551ed8a1a76STheodore Ts'o extern struct ext4_ext_path *ext4_find_extent(struct inode *, ext4_lblk_t,
3552705912caSTheodore Ts'o 					      struct ext4_ext_path **,
3553107a7bd3STheodore Ts'o 					      int flags);
35544a092d73STheodore Ts'o extern void ext4_ext_drop_refs(struct ext4_ext_path *);
35554a092d73STheodore Ts'o extern int ext4_ext_check_inode(struct inode *inode);
3556fcf6b1b7SDmitry Monakhov extern ext4_lblk_t ext4_ext_next_allocated_block(struct ext4_ext_path *path);
35573a06d778SAneesh Kumar K.V extern int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
35583a06d778SAneesh Kumar K.V 			__u64 start, __u64 len);
3559bb5835edSTheodore Ts'o extern int ext4_get_es_cache(struct inode *inode,
3560bb5835edSTheodore Ts'o 			     struct fiemap_extent_info *fieinfo,
3561bb5835edSTheodore Ts'o 			     __u64 start, __u64 len);
35627869a4a6STheodore Ts'o extern int ext4_ext_precache(struct inode *inode);
3563fcf6b1b7SDmitry Monakhov extern int ext4_swap_extents(handle_t *handle, struct inode *inode1,
3564fcf6b1b7SDmitry Monakhov 				struct inode *inode2, ext4_lblk_t lblk1,
3565fcf6b1b7SDmitry Monakhov 			     ext4_lblk_t lblk2,  ext4_lblk_t count,
3566fcf6b1b7SDmitry Monakhov 			     int mark_unwritten,int *err);
35670b02f4c0SEric Whitney extern int ext4_clu_mapped(struct inode *inode, ext4_lblk_t lclu);
3568a4130367SJan Kara extern int ext4_datasem_ensure_credits(handle_t *handle, struct inode *inode,
356983448bdfSJan Kara 				       int check_cred, int restart_cred,
357083448bdfSJan Kara 				       int revoke_cred);
35718016e29fSHarshad Shirwadkar extern void ext4_ext_replay_shrink_inode(struct inode *inode, ext4_lblk_t end);
35728016e29fSHarshad Shirwadkar extern int ext4_ext_replay_set_iblocks(struct inode *inode);
35738016e29fSHarshad Shirwadkar extern int ext4_ext_replay_update_ex(struct inode *inode, ext4_lblk_t start,
35748016e29fSHarshad Shirwadkar 		int len, int unwritten, ext4_fsblk_t pblk);
35758016e29fSHarshad Shirwadkar extern int ext4_ext_clear_bb(struct inode *inode);
3576a4130367SJan Kara 
35774a092d73STheodore Ts'o 
3578748de673SAkira Fujita /* move_extent.c */
3579393d1d1dSDr. Tilmann Bubeck extern void ext4_double_down_write_data_sem(struct inode *first,
3580393d1d1dSDr. Tilmann Bubeck 					    struct inode *second);
3581393d1d1dSDr. Tilmann Bubeck extern void ext4_double_up_write_data_sem(struct inode *orig_inode,
3582393d1d1dSDr. Tilmann Bubeck 					  struct inode *donor_inode);
3583748de673SAkira Fujita extern int ext4_move_extents(struct file *o_filp, struct file *d_filp,
3584748de673SAkira Fujita 			     __u64 start_orig, __u64 start_donor,
3585748de673SAkira Fujita 			     __u64 len, __u64 *moved_len);
3586748de673SAkira Fujita 
3587bd2d0210STheodore Ts'o /* page-io.c */
35885dabfc78STheodore Ts'o extern int __init ext4_init_pageio(void);
35895dabfc78STheodore Ts'o extern void ext4_exit_pageio(void);
3590bd2d0210STheodore Ts'o extern ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags);
359197a851edSJan Kara extern ext4_io_end_t *ext4_get_io_end(ext4_io_end_t *io_end);
359297a851edSJan Kara extern int ext4_put_io_end(ext4_io_end_t *io_end);
359397a851edSJan Kara extern void ext4_put_io_end_defer(ext4_io_end_t *io_end);
359497a851edSJan Kara extern void ext4_io_submit_init(struct ext4_io_submit *io,
359597a851edSJan Kara 				struct writeback_control *wbc);
35962e8fa54eSJan Kara extern void ext4_end_io_rsv_work(struct work_struct *work);
3597bd2d0210STheodore Ts'o extern void ext4_io_submit(struct ext4_io_submit *io);
3598bd2d0210STheodore Ts'o extern int ext4_bio_write_page(struct ext4_io_submit *io,
3599bd2d0210STheodore Ts'o 			       struct page *page,
3600bd2d0210STheodore Ts'o 			       int len,
36011c8349a1SNamjae Jeon 			       struct writeback_control *wbc,
36021c8349a1SNamjae Jeon 			       bool keep_towrite);
3603c8cc8816SRitesh Harjani extern struct ext4_io_end_vec *ext4_alloc_io_end_vec(ext4_io_end_t *io_end);
3604c8cc8816SRitesh Harjani extern struct ext4_io_end_vec *ext4_last_io_end_vec(ext4_io_end_t *io_end);
36053a06d778SAneesh Kumar K.V 
3606c5e06d10SJohann Lombardi /* mmp.c */
3607c5e06d10SJohann Lombardi extern int ext4_multi_mount_protect(struct super_block *, ext4_fsblk_t);
3608c5e06d10SJohann Lombardi 
3609c93d8f88SEric Biggers /* verity.c */
3610c93d8f88SEric Biggers extern const struct fsverity_operations ext4_verityops;
3611c93d8f88SEric Biggers 
36123613d228SJan Kara /*
361348fc7f7eSAdam Buchbinder  * Add new method to test whether block and inode bitmaps are properly
36142ccb5fb9SAneesh Kumar K.V  * initialized. With uninit_bg reading the block from disk is not enough
36152ccb5fb9SAneesh Kumar K.V  * to mark the bitmap uptodate. We need to also zero-out the bitmap
36162ccb5fb9SAneesh Kumar K.V  */
36172ccb5fb9SAneesh Kumar K.V #define BH_BITMAP_UPTODATE BH_JBDPrivateStart
36182ccb5fb9SAneesh Kumar K.V 
36192ccb5fb9SAneesh Kumar K.V static inline int bitmap_uptodate(struct buffer_head *bh)
36202ccb5fb9SAneesh Kumar K.V {
36212ccb5fb9SAneesh Kumar K.V 	return (buffer_uptodate(bh) &&
36222ccb5fb9SAneesh Kumar K.V 			test_bit(BH_BITMAP_UPTODATE, &(bh)->b_state));
36232ccb5fb9SAneesh Kumar K.V }
36242ccb5fb9SAneesh Kumar K.V static inline void set_bitmap_uptodate(struct buffer_head *bh)
36252ccb5fb9SAneesh Kumar K.V {
36262ccb5fb9SAneesh Kumar K.V 	set_bit(BH_BITMAP_UPTODATE, &(bh)->b_state);
36272ccb5fb9SAneesh Kumar K.V }
36282ccb5fb9SAneesh Kumar K.V 
3629731eb1a0SAkinobu Mita #define in_range(b, first, len)	((b) >= (first) && (b) <= (first) + (len) - 1)
3630731eb1a0SAkinobu Mita 
3631e9e3bcecSEric Sandeen /* For ioend & aio unwritten conversion wait queues */
3632e9e3bcecSEric Sandeen #define EXT4_WQ_HASH_SZ		37
3633e9e3bcecSEric Sandeen #define ext4_ioend_wq(v)   (&ext4__ioend_wq[((unsigned long)(v)) %\
3634e9e3bcecSEric Sandeen 					    EXT4_WQ_HASH_SZ])
3635e9e3bcecSEric Sandeen extern wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
3636e9e3bcecSEric Sandeen 
36378f82f840SYongqiang Yang extern int ext4_resize_begin(struct super_block *sb);
36388f82f840SYongqiang Yang extern void ext4_resize_end(struct super_block *sb);
36398f82f840SYongqiang Yang 
364074c66bcbSJan Kara static inline void ext4_set_io_unwritten_flag(struct inode *inode,
364174c66bcbSJan Kara 					      struct ext4_io_end *io_end)
364274c66bcbSJan Kara {
364374c66bcbSJan Kara 	if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) {
364474c66bcbSJan Kara 		io_end->flag |= EXT4_IO_END_UNWRITTEN;
364574c66bcbSJan Kara 		atomic_inc(&EXT4_I(inode)->i_unwritten);
364674c66bcbSJan Kara 	}
364774c66bcbSJan Kara }
364874c66bcbSJan Kara 
364974c66bcbSJan Kara static inline void ext4_clear_io_unwritten_flag(ext4_io_end_t *io_end)
365074c66bcbSJan Kara {
365174c66bcbSJan Kara 	struct inode *inode = io_end->inode;
365274c66bcbSJan Kara 
365374c66bcbSJan Kara 	if (io_end->flag & EXT4_IO_END_UNWRITTEN) {
365474c66bcbSJan Kara 		io_end->flag &= ~EXT4_IO_END_UNWRITTEN;
365574c66bcbSJan Kara 		/* Wake up anyone waiting on unwritten extent conversion */
365674c66bcbSJan Kara 		if (atomic_dec_and_test(&EXT4_I(inode)->i_unwritten))
365774c66bcbSJan Kara 			wake_up_all(ext4_ioend_wq(inode));
365874c66bcbSJan Kara 	}
365974c66bcbSJan Kara }
366074c66bcbSJan Kara 
36618ff6daa1SChristoph Hellwig extern const struct iomap_ops ext4_iomap_ops;
36628cd115bdSJan Kara extern const struct iomap_ops ext4_iomap_overwrite_ops;
366309edf4d3SMatthew Bobrowski extern const struct iomap_ops ext4_iomap_report_ops;
3664364443cbSJan Kara 
36657963e5acSZhangXiaoxu static inline int ext4_buffer_uptodate(struct buffer_head *bh)
36667963e5acSZhangXiaoxu {
36677963e5acSZhangXiaoxu 	/*
36687963e5acSZhangXiaoxu 	 * If the buffer has the write error flag, we have failed
36697963e5acSZhangXiaoxu 	 * to write out data in the block.  In this  case, we don't
36707963e5acSZhangXiaoxu 	 * have to read the block because we may read the old data
36717963e5acSZhangXiaoxu 	 * successfully.
36727963e5acSZhangXiaoxu 	 */
36737963e5acSZhangXiaoxu 	if (!buffer_uptodate(bh) && buffer_write_io_error(bh))
36747963e5acSZhangXiaoxu 		set_buffer_uptodate(bh);
36757963e5acSZhangXiaoxu 	return buffer_uptodate(bh);
36767963e5acSZhangXiaoxu }
36777963e5acSZhangXiaoxu 
36783dcf5451SChristoph Hellwig #endif	/* __KERNEL__ */
36793dcf5451SChristoph Hellwig 
36806a797d27SDarrick J. Wong #define EFSBADCRC	EBADMSG		/* Bad CRC detected */
36816a797d27SDarrick J. Wong #define EFSCORRUPTED	EUCLEAN		/* Filesystem is corrupted */
36826a797d27SDarrick J. Wong 
36833dcf5451SChristoph Hellwig #endif	/* _EXT4_H */
3684