xref: /openbmc/linux/fs/ext4/ext4.h (revision 42954c37)
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 
2031d21d21SXiyu Yang #include <linux/refcount.h>
213dcf5451SChristoph Hellwig #include <linux/types.h>
223dcf5451SChristoph Hellwig #include <linux/blkdev.h>
233dcf5451SChristoph Hellwig #include <linux/magic.h>
242ccb5fb9SAneesh Kumar K.V #include <linux/jbd2.h>
2560e58e0fSMingming Cao #include <linux/quota.h>
26d444c3c3STheodore Ts'o #include <linux/rwsem.h>
27d444c3c3STheodore Ts'o #include <linux/rbtree.h>
28d444c3c3STheodore Ts'o #include <linux/seqlock.h>
29d444c3c3STheodore Ts'o #include <linux/mutex.h>
30ca0faba0STheodore Ts'o #include <linux/timer.h>
31ca0faba0STheodore Ts'o #include <linux/wait.h>
32174cd4b1SIngo Molnar #include <linux/sched/signal.h>
33ca0faba0STheodore Ts'o #include <linux/blockgroup_lock.h>
34ca0faba0STheodore Ts'o #include <linux/percpu_counter.h>
35efbed4dcSTheodore Ts'o #include <linux/ratelimit.h>
360441984aSDarrick J. Wong #include <crypto/hash.h>
37a633f5a3SLukas Czerner #include <linux/falloc.h>
38c8585c6fSDaeho Jeong #include <linux/percpu-rwsem.h>
3910c5db28SChristoph Hellwig #include <linux/fiemap.h>
404d92dc0fSBen Hutchings #ifdef __KERNEL__
414d92dc0fSBen Hutchings #include <linux/compat.h>
424d92dc0fSBen Hutchings #endif
43519fe1baSJosh Triplett #include <uapi/linux/ext4.h>
443dcf5451SChristoph Hellwig 
45734f0d24SDave Chinner #include <linux/fscrypt.h>
46c93d8f88SEric Biggers #include <linux/fsverity.h>
47734f0d24SDave Chinner 
48072ebb3bSTheodore Ts'o #include <linux/compiler.h>
49072ebb3bSTheodore Ts'o 
503dcf5451SChristoph Hellwig /*
518a35694eSShen Feng  * The fourth extended filesystem constants/structures
523dcf5451SChristoph Hellwig  */
533dcf5451SChristoph Hellwig 
543dcf5451SChristoph Hellwig /*
55a2821e34SAihua Zhang  * with AGGRESSIVE_CHECK allocator runs consistency checks over
56a2821e34SAihua Zhang  * structures. these checks slow things down a lot
57a2821e34SAihua Zhang  */
58a2821e34SAihua Zhang #define AGGRESSIVE_CHECK__
59a2821e34SAihua Zhang 
60a2821e34SAihua Zhang /*
61a2821e34SAihua Zhang  * with DOUBLE_CHECK defined mballoc creates persistent in-core
62a2821e34SAihua Zhang  * bitmaps, maintains and uses them to check for double allocations
63a2821e34SAihua Zhang  */
64a2821e34SAihua Zhang #define DOUBLE_CHECK__
65a2821e34SAihua Zhang 
66a2821e34SAihua Zhang /*
673dcf5451SChristoph Hellwig  * Define EXT4FS_DEBUG to produce debug messages
683dcf5451SChristoph Hellwig  */
693dcf5451SChristoph Hellwig #undef EXT4FS_DEBUG
703dcf5451SChristoph Hellwig 
713dcf5451SChristoph Hellwig /*
723dcf5451SChristoph Hellwig  * Debug code
733dcf5451SChristoph Hellwig  */
743dcf5451SChristoph Hellwig #ifdef EXT4FS_DEBUG
753dcf5451SChristoph Hellwig #define ext4_debug(f, a...)						\
763dcf5451SChristoph Hellwig 	do {								\
773dcf5451SChristoph Hellwig 		printk(KERN_DEBUG "EXT4-fs DEBUG (%s, %d): %s:",	\
784db9c54aSStoyan Gaydarov 			__FILE__, __LINE__, __func__);			\
793dcf5451SChristoph Hellwig 		printk(KERN_DEBUG f, ## a);				\
803dcf5451SChristoph Hellwig 	} while (0)
813dcf5451SChristoph Hellwig #else
82ace36ad4SJoe Perches #define ext4_debug(fmt, ...)	no_printk(fmt, ##__VA_ARGS__)
833dcf5451SChristoph Hellwig #endif
843dcf5451SChristoph Hellwig 
854a092d73STheodore Ts'o  /*
8670aa1554SRitesh Harjani   * Turn on EXT_DEBUG to enable ext4_ext_show_path/leaf/move in extents.c
874a092d73STheodore Ts'o   */
884a092d73STheodore Ts'o #define EXT_DEBUG__
8970aa1554SRitesh Harjani 
9070aa1554SRitesh Harjani /*
9170aa1554SRitesh Harjani  * Dynamic printk for controlled extents debugging.
9270aa1554SRitesh Harjani  */
9370aa1554SRitesh Harjani #ifdef CONFIG_EXT4_DEBUG
9470aa1554SRitesh Harjani #define ext_debug(ino, fmt, ...)					\
9570aa1554SRitesh Harjani 	pr_debug("[%s/%d] EXT4-fs (%s): ino %lu: (%s, %d): %s:" fmt,	\
9670aa1554SRitesh Harjani 		 current->comm, task_pid_nr(current),			\
9770aa1554SRitesh Harjani 		 ino->i_sb->s_id, ino->i_ino, __FILE__, __LINE__,	\
9870aa1554SRitesh Harjani 		 __func__, ##__VA_ARGS__)
994a092d73STheodore Ts'o #else
10070aa1554SRitesh Harjani #define ext_debug(ino, fmt, ...)	no_printk(fmt, ##__VA_ARGS__)
1014a092d73STheodore Ts'o #endif
1024a092d73STheodore Ts'o 
103837c23fbSChunguang Xu #define ASSERT(assert)						\
104837c23fbSChunguang Xu do {									\
105837c23fbSChunguang Xu 	if (unlikely(!(assert))) {					\
106837c23fbSChunguang Xu 		printk(KERN_EMERG					\
107837c23fbSChunguang Xu 		       "Assertion failure in %s() at %s:%d: '%s'\n",	\
108837c23fbSChunguang Xu 		       __func__, __FILE__, __LINE__, #assert);		\
109837c23fbSChunguang Xu 		BUG();							\
110837c23fbSChunguang Xu 	}								\
111837c23fbSChunguang Xu } while (0)
112837c23fbSChunguang Xu 
113d444c3c3STheodore Ts'o /* data type for block offset of block group */
114d444c3c3STheodore Ts'o typedef int ext4_grpblk_t;
115d444c3c3STheodore Ts'o 
116d444c3c3STheodore Ts'o /* data type for filesystem-wide blocks number */
117d444c3c3STheodore Ts'o typedef unsigned long long ext4_fsblk_t;
118d444c3c3STheodore Ts'o 
119d444c3c3STheodore Ts'o /* data type for file logical block number */
120d444c3c3STheodore Ts'o typedef __u32 ext4_lblk_t;
121d444c3c3STheodore Ts'o 
122d444c3c3STheodore Ts'o /* data type for block group number */
123d444c3c3STheodore Ts'o typedef unsigned int ext4_group_t;
124d444c3c3STheodore Ts'o 
125331573feSNamjae Jeon enum SHIFT_DIRECTION {
126331573feSNamjae Jeon 	SHIFT_LEFT = 0,
127331573feSNamjae Jeon 	SHIFT_RIGHT,
128331573feSNamjae Jeon };
129331573feSNamjae Jeon 
130296c355cSTheodore Ts'o /*
1314c0cfebdSTheodore Ts'o  * For each criteria, mballoc has slightly different way of finding
1324c0cfebdSTheodore Ts'o  * the required blocks nad usually, higher the criteria the slower the
1334c0cfebdSTheodore Ts'o  * allocation.  We start at lower criterias and keep falling back to
1344c0cfebdSTheodore Ts'o  * higher ones if we are not able to find any blocks.  Lower (earlier)
1354c0cfebdSTheodore Ts'o  * criteria are faster.
1364eb7a4a1SOjaswin Mujoo  */
1374eb7a4a1SOjaswin Mujoo enum criteria {
138f52f3d2bSOjaswin Mujoo 	/*
1394c0cfebdSTheodore Ts'o 	 * Used when number of blocks needed is a power of 2. This
1404c0cfebdSTheodore Ts'o 	 * doesn't trigger any disk IO except prefetch and is the
1414c0cfebdSTheodore Ts'o 	 * fastest criteria.
142f52f3d2bSOjaswin Mujoo 	 */
143f52f3d2bSOjaswin Mujoo 	CR_POWER2_ALIGNED,
144f52f3d2bSOjaswin Mujoo 
145f52f3d2bSOjaswin Mujoo 	/*
1464c0cfebdSTheodore Ts'o 	 * Tries to lookup in-memory data structures to find the most
1474c0cfebdSTheodore Ts'o 	 * suitable group that satisfies goal request. No disk IO
1484c0cfebdSTheodore Ts'o 	 * except block prefetch.
149f52f3d2bSOjaswin Mujoo 	 */
150f52f3d2bSOjaswin Mujoo 	CR_GOAL_LEN_FAST,
151f52f3d2bSOjaswin Mujoo 
152f52f3d2bSOjaswin Mujoo         /*
1534c0cfebdSTheodore Ts'o 	 * Same as CR_GOAL_LEN_FAST but is allowed to reduce the goal
1544c0cfebdSTheodore Ts'o          * length to the best available length for faster allocation.
155f52f3d2bSOjaswin Mujoo 	 */
156f52f3d2bSOjaswin Mujoo 	CR_BEST_AVAIL_LEN,
157f52f3d2bSOjaswin Mujoo 
158f52f3d2bSOjaswin Mujoo 	/*
1594c0cfebdSTheodore Ts'o 	 * Reads each block group sequentially, performing disk IO if
1604c0cfebdSTheodore Ts'o 	 * necessary, to find find_suitable block group. Tries to
1614c0cfebdSTheodore Ts'o 	 * allocate goal length but might trim the request if nothing
1624c0cfebdSTheodore Ts'o 	 * is found after enough tries.
163f52f3d2bSOjaswin Mujoo 	 */
164f52f3d2bSOjaswin Mujoo 	CR_GOAL_LEN_SLOW,
165f52f3d2bSOjaswin Mujoo 
166f52f3d2bSOjaswin Mujoo 	/*
1674c0cfebdSTheodore Ts'o 	 * Finds the first free set of blocks and allocates
1684c0cfebdSTheodore Ts'o 	 * those. This is only used in rare cases when
1694c0cfebdSTheodore Ts'o 	 * CR_GOAL_LEN_SLOW also fails to allocate anything.
170f52f3d2bSOjaswin Mujoo 	 */
171f52f3d2bSOjaswin Mujoo 	CR_ANY_FREE,
1724c0cfebdSTheodore Ts'o 
1734c0cfebdSTheodore Ts'o 	/*
1744c0cfebdSTheodore Ts'o 	 * Number of criterias defined.
1754c0cfebdSTheodore Ts'o 	 */
1764c0cfebdSTheodore Ts'o 	EXT4_MB_NUM_CRS
1774eb7a4a1SOjaswin Mujoo };
1784eb7a4a1SOjaswin Mujoo 
1794eb7a4a1SOjaswin Mujoo /*
180296c355cSTheodore Ts'o  * Flags used in mballoc's allocation_context flags field.
181296c355cSTheodore Ts'o  *
182296c355cSTheodore Ts'o  * Also used to show what's going on for debugging purposes when the
183296c355cSTheodore Ts'o  * flag field is exported via the traceport interface
184296c355cSTheodore Ts'o  */
185d444c3c3STheodore Ts'o 
1863dcf5451SChristoph Hellwig /* prefer goal again. length */
1870ef90db9STheodore Ts'o #define EXT4_MB_HINT_MERGE		0x0001
1883dcf5451SChristoph Hellwig /* blocks already reserved */
1890ef90db9STheodore Ts'o #define EXT4_MB_HINT_RESERVED		0x0002
1903dcf5451SChristoph Hellwig /* metadata is being allocated */
1910ef90db9STheodore Ts'o #define EXT4_MB_HINT_METADATA		0x0004
1923dcf5451SChristoph Hellwig /* first blocks in the file */
1930ef90db9STheodore Ts'o #define EXT4_MB_HINT_FIRST		0x0008
1943dcf5451SChristoph Hellwig /* search for the best chunk */
1950ef90db9STheodore Ts'o #define EXT4_MB_HINT_BEST		0x0010
1963dcf5451SChristoph Hellwig /* data is being allocated */
1970ef90db9STheodore Ts'o #define EXT4_MB_HINT_DATA		0x0020
1983dcf5451SChristoph Hellwig /* don't preallocate (for tails) */
1990ef90db9STheodore Ts'o #define EXT4_MB_HINT_NOPREALLOC		0x0040
2003dcf5451SChristoph Hellwig /* allocate for locality group */
2010ef90db9STheodore Ts'o #define EXT4_MB_HINT_GROUP_ALLOC	0x0080
2023dcf5451SChristoph Hellwig /* allocate goal blocks or none */
2030ef90db9STheodore Ts'o #define EXT4_MB_HINT_GOAL_ONLY		0x0100
2043dcf5451SChristoph Hellwig /* goal is meaningful */
2050ef90db9STheodore Ts'o #define EXT4_MB_HINT_TRY_GOAL		0x0200
206d2a17637SMingming Cao /* blocks already pre-reserved by delayed allocation */
2070ef90db9STheodore Ts'o #define EXT4_MB_DELALLOC_RESERVED	0x0400
2084ba74d00STheodore Ts'o /* We are doing stream allocation */
2094ba74d00STheodore Ts'o #define EXT4_MB_STREAM_ALLOC		0x0800
21055f020dbSAllison Henderson /* Use reserved root blocks if needed */
21155f020dbSAllison Henderson #define EXT4_MB_USE_ROOT_BLOCKS		0x1000
21227dd4385SLukas Czerner /* Use blocks from reserved pool */
21327dd4385SLukas Czerner #define EXT4_MB_USE_RESERVED		0x2000
21499377830SRitesh Harjani /* Do strict check for free blocks while retrying block allocation */
21599377830SRitesh Harjani #define EXT4_MB_STRICT_CHECK		0x4000
216196e402aSHarshad Shirwadkar /* Large fragment size list lookup succeeded at least once for cr = 0 */
217f52f3d2bSOjaswin Mujoo #define EXT4_MB_CR_POWER2_ALIGNED_OPTIMIZED		0x8000
218196e402aSHarshad Shirwadkar /* Avg fragment size rb tree lookup succeeded at least once for cr = 1 */
219f52f3d2bSOjaswin Mujoo #define EXT4_MB_CR_GOAL_LEN_FAST_OPTIMIZED		0x00010000
2207e170922SOjaswin Mujoo /* Avg fragment size rb tree lookup succeeded at least once for cr = 1.5 */
221f52f3d2bSOjaswin Mujoo #define EXT4_MB_CR_BEST_AVAIL_LEN_OPTIMIZED		0x00020000
2227e170922SOjaswin Mujoo 
2233dcf5451SChristoph Hellwig struct ext4_allocation_request {
2243dcf5451SChristoph Hellwig 	/* target inode for block we're allocating */
2253dcf5451SChristoph Hellwig 	struct inode *inode;
2263dcf5451SChristoph Hellwig 	/* how many blocks we want to allocate */
227498e5f24STheodore Ts'o 	unsigned int len;
228726447d8SEric Sandeen 	/* logical block in target inode */
229726447d8SEric Sandeen 	ext4_lblk_t logical;
230726447d8SEric Sandeen 	/* the closest logical allocated block to the left */
231726447d8SEric Sandeen 	ext4_lblk_t lleft;
232726447d8SEric Sandeen 	/* the closest logical allocated block to the right */
233726447d8SEric Sandeen 	ext4_lblk_t lright;
234726447d8SEric Sandeen 	/* phys. target (a hint) */
235726447d8SEric Sandeen 	ext4_fsblk_t goal;
236726447d8SEric Sandeen 	/* phys. block for the closest logical allocated block to the left */
237726447d8SEric Sandeen 	ext4_fsblk_t pleft;
238726447d8SEric Sandeen 	/* phys. block for the closest logical allocated block to the right */
239726447d8SEric Sandeen 	ext4_fsblk_t pright;
2403dcf5451SChristoph Hellwig 	/* flags. see above EXT4_MB_HINT_* */
241498e5f24STheodore Ts'o 	unsigned int flags;
2423dcf5451SChristoph Hellwig };
2433dcf5451SChristoph Hellwig 
2443dcf5451SChristoph Hellwig /*
245e35fd660STheodore Ts'o  * Logical to physical block mapping, used by ext4_map_blocks()
246e35fd660STheodore Ts'o  *
247e35fd660STheodore Ts'o  * This structure is used to pass requests into ext4_map_blocks() as
248e35fd660STheodore Ts'o  * well as to store the information returned by ext4_map_blocks().  It
249e35fd660STheodore Ts'o  * takes less room on the stack than a struct buffer_head.
250e35fd660STheodore Ts'o  */
2516db07461SRitesh Harjani #define EXT4_MAP_NEW		BIT(BH_New)
2526db07461SRitesh Harjani #define EXT4_MAP_MAPPED		BIT(BH_Mapped)
2536db07461SRitesh Harjani #define EXT4_MAP_UNWRITTEN	BIT(BH_Unwritten)
2546db07461SRitesh Harjani #define EXT4_MAP_BOUNDARY	BIT(BH_Boundary)
255e35fd660STheodore Ts'o #define EXT4_MAP_FLAGS		(EXT4_MAP_NEW | EXT4_MAP_MAPPED |\
256cbd7584eSJan Kara 				 EXT4_MAP_UNWRITTEN | EXT4_MAP_BOUNDARY)
257e35fd660STheodore Ts'o 
258e35fd660STheodore Ts'o struct ext4_map_blocks {
259e35fd660STheodore Ts'o 	ext4_fsblk_t m_pblk;
260e35fd660STheodore Ts'o 	ext4_lblk_t m_lblk;
261e35fd660STheodore Ts'o 	unsigned int m_len;
262e35fd660STheodore Ts'o 	unsigned int m_flags;
263e35fd660STheodore Ts'o };
264e35fd660STheodore Ts'o 
265e35fd660STheodore Ts'o /*
2667727ae52Szhangyi (F)  * Block validity checking, system zone rbtree.
2677727ae52Szhangyi (F)  */
2687727ae52Szhangyi (F) struct ext4_system_blocks {
2697727ae52Szhangyi (F) 	struct rb_root root;
2707727ae52Szhangyi (F) 	struct rcu_head rcu;
2717727ae52Szhangyi (F) };
2727727ae52Szhangyi (F) 
2737727ae52Szhangyi (F) /*
274bd2d0210STheodore Ts'o  * Flags for ext4_io_end->flags
275bd2d0210STheodore Ts'o  */
276bd2d0210STheodore Ts'o #define	EXT4_IO_END_UNWRITTEN	0x0001
277bd2d0210STheodore Ts'o 
278c8cc8816SRitesh Harjani struct ext4_io_end_vec {
279c8cc8816SRitesh Harjani 	struct list_head list;		/* list of io_end_vec */
280c8cc8816SRitesh Harjani 	loff_t offset;			/* offset in the file */
281c8cc8816SRitesh Harjani 	ssize_t size;			/* size of the extent */
282c8cc8816SRitesh Harjani };
283c8cc8816SRitesh Harjani 
2844188188bSCurt Wohlgemuth /*
285556615dcSLukas Czerner  * For converting unwritten extents on a work queue. 'handle' is used for
2866b523df4SJan Kara  * buffered writeback.
2874188188bSCurt Wohlgemuth  */
2880031462bSMingming Cao typedef struct ext4_io_end {
2895b3ff237Sjiayingz@google.com (Jiaying Zhang) 	struct list_head	list;		/* per-file finished IO list */
2906b523df4SJan Kara 	handle_t		*handle;	/* handle reserved for extent
2916b523df4SJan Kara 						 * conversion */
2920031462bSMingming Cao 	struct inode		*inode;		/* file being written to */
293b0857d30SJan Kara 	struct bio		*bio;		/* Linked list of completed
294b0857d30SJan Kara 						 * bios covering the extent */
2958d5d02e6SMingming Cao 	unsigned int		flag;		/* unwritten or not */
29631d21d21SXiyu Yang 	refcount_t		count;		/* reference counter */
297c8cc8816SRitesh Harjani 	struct list_head	list_vec;	/* list of ext4_io_end_vec */
2980031462bSMingming Cao } ext4_io_end_t;
2990031462bSMingming Cao 
300bd2d0210STheodore Ts'o struct ext4_io_submit {
3015a33911fSTejun Heo 	struct writeback_control *io_wbc;
302bd2d0210STheodore Ts'o 	struct bio		*io_bio;
303bd2d0210STheodore Ts'o 	ext4_io_end_t		*io_end;
304bd2d0210STheodore Ts'o 	sector_t		io_next_block;
305bd2d0210STheodore Ts'o };
306bd2d0210STheodore Ts'o 
307b3a3ca8cSTheodore Ts'o /*
3083dcf5451SChristoph Hellwig  * Special inodes numbers
3093dcf5451SChristoph Hellwig  */
3103dcf5451SChristoph Hellwig #define	EXT4_BAD_INO		 1	/* Bad blocks inode */
3113dcf5451SChristoph Hellwig #define EXT4_ROOT_INO		 2	/* Root inode */
312ae812306SAditya Kali #define EXT4_USR_QUOTA_INO	 3	/* User quota inode */
313ae812306SAditya Kali #define EXT4_GRP_QUOTA_INO	 4	/* Group quota inode */
3143dcf5451SChristoph Hellwig #define EXT4_BOOT_LOADER_INO	 5	/* Boot loader inode */
3153dcf5451SChristoph Hellwig #define EXT4_UNDEL_DIR_INO	 6	/* Undelete directory inode */
3163dcf5451SChristoph Hellwig #define EXT4_RESIZE_INO		 7	/* Reserved group descriptors inode */
3173dcf5451SChristoph Hellwig #define EXT4_JOURNAL_INO	 8	/* Journal inode */
3183dcf5451SChristoph Hellwig 
3193dcf5451SChristoph Hellwig /* First non-reserved inode for old ext4 filesystems */
3203dcf5451SChristoph Hellwig #define EXT4_GOOD_OLD_FIRST_INO	11
3213dcf5451SChristoph Hellwig 
3223dcf5451SChristoph Hellwig /*
3233dcf5451SChristoph Hellwig  * Maximal count of links to a file
3243dcf5451SChristoph Hellwig  */
3253dcf5451SChristoph Hellwig #define EXT4_LINK_MAX		65000
3263dcf5451SChristoph Hellwig 
3273dcf5451SChristoph Hellwig /*
3283dcf5451SChristoph Hellwig  * Macro-instructions used to manage several block sizes
3293dcf5451SChristoph Hellwig  */
3303dcf5451SChristoph Hellwig #define EXT4_MIN_BLOCK_SIZE		1024
3313dcf5451SChristoph Hellwig #define	EXT4_MAX_BLOCK_SIZE		65536
3323dcf5451SChristoph Hellwig #define EXT4_MIN_BLOCK_LOG_SIZE		10
333fb1813f4SCurt Wohlgemuth #define EXT4_MAX_BLOCK_LOG_SIZE		16
3349e47a4c9STheodore Ts'o #define EXT4_MAX_CLUSTER_LOG_SIZE	30
3353dcf5451SChristoph Hellwig #ifdef __KERNEL__
3363dcf5451SChristoph Hellwig # define EXT4_BLOCK_SIZE(s)		((s)->s_blocksize)
3373dcf5451SChristoph Hellwig #else
3383dcf5451SChristoph Hellwig # define EXT4_BLOCK_SIZE(s)		(EXT4_MIN_BLOCK_SIZE << (s)->s_log_block_size)
3393dcf5451SChristoph Hellwig #endif
3403dcf5451SChristoph Hellwig #define	EXT4_ADDR_PER_BLOCK(s)		(EXT4_BLOCK_SIZE(s) / sizeof(__u32))
341281b5995STheodore Ts'o #define EXT4_CLUSTER_SIZE(s)		(EXT4_BLOCK_SIZE(s) << \
342281b5995STheodore Ts'o 					 EXT4_SB(s)->s_cluster_bits)
3433dcf5451SChristoph Hellwig #ifdef __KERNEL__
3443dcf5451SChristoph Hellwig # define EXT4_BLOCK_SIZE_BITS(s)	((s)->s_blocksize_bits)
345281b5995STheodore Ts'o # define EXT4_CLUSTER_BITS(s)		(EXT4_SB(s)->s_cluster_bits)
3463dcf5451SChristoph Hellwig #else
3473dcf5451SChristoph Hellwig # define EXT4_BLOCK_SIZE_BITS(s)	((s)->s_log_block_size + 10)
3483dcf5451SChristoph Hellwig #endif
3493dcf5451SChristoph Hellwig #ifdef __KERNEL__
3503dcf5451SChristoph Hellwig #define	EXT4_ADDR_PER_BLOCK_BITS(s)	(EXT4_SB(s)->s_addr_per_block_bits)
3513dcf5451SChristoph Hellwig #define EXT4_INODE_SIZE(s)		(EXT4_SB(s)->s_inode_size)
3523dcf5451SChristoph Hellwig #define EXT4_FIRST_INO(s)		(EXT4_SB(s)->s_first_ino)
3533dcf5451SChristoph Hellwig #else
3543dcf5451SChristoph Hellwig #define EXT4_INODE_SIZE(s)	(((s)->s_rev_level == EXT4_GOOD_OLD_REV) ? \
3553dcf5451SChristoph Hellwig 				 EXT4_GOOD_OLD_INODE_SIZE : \
3563dcf5451SChristoph Hellwig 				 (s)->s_inode_size)
3573dcf5451SChristoph Hellwig #define EXT4_FIRST_INO(s)	(((s)->s_rev_level == EXT4_GOOD_OLD_REV) ? \
3583dcf5451SChristoph Hellwig 				 EXT4_GOOD_OLD_FIRST_INO : \
3593dcf5451SChristoph Hellwig 				 (s)->s_first_ino)
3603dcf5451SChristoph Hellwig #endif
3613dcf5451SChristoph Hellwig #define EXT4_BLOCK_ALIGN(size, blkbits)		ALIGN((size), (1 << (blkbits)))
362518eaa63SFabian Frederick #define EXT4_MAX_BLOCKS(size, offset, blkbits) \
363518eaa63SFabian Frederick 	((EXT4_BLOCK_ALIGN(size + offset, blkbits) >> blkbits) - (offset >> \
364518eaa63SFabian Frederick 								  blkbits))
3653dcf5451SChristoph Hellwig 
366d5b8f310STheodore Ts'o /* Translate a block number to a cluster number */
367d5b8f310STheodore Ts'o #define EXT4_B2C(sbi, blk)	((blk) >> (sbi)->s_cluster_bits)
368d5b8f310STheodore Ts'o /* Translate a cluster number to a block number */
369d5b8f310STheodore Ts'o #define EXT4_C2B(sbi, cluster)	((cluster) << (sbi)->s_cluster_bits)
370d5b8f310STheodore Ts'o /* Translate # of blks to # of clusters */
371d5b8f310STheodore Ts'o #define EXT4_NUM_B2C(sbi, blks)	(((blks) + (sbi)->s_cluster_ratio - 1) >> \
372d5b8f310STheodore Ts'o 				 (sbi)->s_cluster_bits)
373f5a44db5STheodore Ts'o /* Mask out the low bits to get the starting block of the cluster */
374f5a44db5STheodore Ts'o #define EXT4_PBLK_CMASK(s, pblk) ((pblk) &				\
375f5a44db5STheodore Ts'o 				  ~((ext4_fsblk_t) (s)->s_cluster_ratio - 1))
376f5a44db5STheodore Ts'o #define EXT4_LBLK_CMASK(s, lblk) ((lblk) &				\
377f5a44db5STheodore Ts'o 				  ~((ext4_lblk_t) (s)->s_cluster_ratio - 1))
3788fcc3a58SEric Whitney /* Fill in the low bits to get the last block of the cluster */
3798fcc3a58SEric Whitney #define EXT4_LBLK_CFILL(sbi, lblk) ((lblk) |				\
3808fcc3a58SEric Whitney 				    ((ext4_lblk_t) (sbi)->s_cluster_ratio - 1))
381f5a44db5STheodore Ts'o /* Get the cluster offset */
382f5a44db5STheodore Ts'o #define EXT4_PBLK_COFF(s, pblk) ((pblk) &				\
383f5a44db5STheodore Ts'o 				 ((ext4_fsblk_t) (s)->s_cluster_ratio - 1))
384f5a44db5STheodore Ts'o #define EXT4_LBLK_COFF(s, lblk) ((lblk) &				\
385f5a44db5STheodore Ts'o 				 ((ext4_lblk_t) (s)->s_cluster_ratio - 1))
386d5b8f310STheodore Ts'o 
3873dcf5451SChristoph Hellwig /*
3883dcf5451SChristoph Hellwig  * Structure of a blocks group descriptor
3893dcf5451SChristoph Hellwig  */
3903dcf5451SChristoph Hellwig struct ext4_group_desc
3913dcf5451SChristoph Hellwig {
3923dcf5451SChristoph Hellwig 	__le32	bg_block_bitmap_lo;	/* Blocks bitmap block */
3933dcf5451SChristoph Hellwig 	__le32	bg_inode_bitmap_lo;	/* Inodes bitmap block */
3943dcf5451SChristoph Hellwig 	__le32	bg_inode_table_lo;	/* Inodes table block */
395560671a0SAneesh Kumar K.V 	__le16	bg_free_blocks_count_lo;/* Free blocks count */
396560671a0SAneesh Kumar K.V 	__le16	bg_free_inodes_count_lo;/* Free inodes count */
397560671a0SAneesh Kumar K.V 	__le16	bg_used_dirs_count_lo;	/* Directories count */
3983dcf5451SChristoph Hellwig 	__le16	bg_flags;		/* EXT4_BG_flags (INODE_UNINIT, etc) */
399e6153918SDarrick J. Wong 	__le32  bg_exclude_bitmap_lo;   /* Exclude bitmap for snapshots */
400e6153918SDarrick J. Wong 	__le16  bg_block_bitmap_csum_lo;/* crc32c(s_uuid+grp_num+bbitmap) LE */
401e6153918SDarrick J. Wong 	__le16  bg_inode_bitmap_csum_lo;/* crc32c(s_uuid+grp_num+ibitmap) LE */
402560671a0SAneesh Kumar K.V 	__le16  bg_itable_unused_lo;	/* Unused inodes count */
4033dcf5451SChristoph Hellwig 	__le16  bg_checksum;		/* crc16(sb_uuid+group+desc) */
4043dcf5451SChristoph Hellwig 	__le32	bg_block_bitmap_hi;	/* Blocks bitmap block MSB */
4053dcf5451SChristoph Hellwig 	__le32	bg_inode_bitmap_hi;	/* Inodes bitmap block MSB */
4063dcf5451SChristoph Hellwig 	__le32	bg_inode_table_hi;	/* Inodes table block MSB */
4073dcf5451SChristoph Hellwig 	__le16	bg_free_blocks_count_hi;/* Free blocks count MSB */
4083dcf5451SChristoph Hellwig 	__le16	bg_free_inodes_count_hi;/* Free inodes count MSB */
4093dcf5451SChristoph Hellwig 	__le16	bg_used_dirs_count_hi;	/* Directories count MSB */
4103dcf5451SChristoph Hellwig 	__le16  bg_itable_unused_hi;    /* Unused inodes count MSB */
411e6153918SDarrick J. Wong 	__le32  bg_exclude_bitmap_hi;   /* Exclude bitmap block MSB */
412e6153918SDarrick J. Wong 	__le16  bg_block_bitmap_csum_hi;/* crc32c(s_uuid+grp_num+bbitmap) BE */
413e6153918SDarrick J. Wong 	__le16  bg_inode_bitmap_csum_hi;/* crc32c(s_uuid+grp_num+ibitmap) BE */
414e6153918SDarrick J. Wong 	__u32   bg_reserved;
4153dcf5451SChristoph Hellwig };
4163dcf5451SChristoph Hellwig 
41741a246d1SDarrick J. Wong #define EXT4_BG_INODE_BITMAP_CSUM_HI_END	\
41841a246d1SDarrick J. Wong 	(offsetof(struct ext4_group_desc, bg_inode_bitmap_csum_hi) + \
41941a246d1SDarrick J. Wong 	 sizeof(__le16))
42041a246d1SDarrick J. Wong #define EXT4_BG_BLOCK_BITMAP_CSUM_HI_END	\
42141a246d1SDarrick J. Wong 	(offsetof(struct ext4_group_desc, bg_block_bitmap_csum_hi) + \
42241a246d1SDarrick J. Wong 	 sizeof(__le16))
42341a246d1SDarrick J. Wong 
424772cb7c8SJose R. Santos /*
425772cb7c8SJose R. Santos  * Structure of a flex block group info
426772cb7c8SJose R. Santos  */
427772cb7c8SJose R. Santos 
428772cb7c8SJose R. Santos struct flex_groups {
42990ba983fSTheodore Ts'o 	atomic64_t	free_clusters;
4309f24e420STheodore Ts'o 	atomic_t	free_inodes;
4317d39db14STheodore Ts'o 	atomic_t	used_dirs;
432772cb7c8SJose R. Santos };
433772cb7c8SJose R. Santos 
4343dcf5451SChristoph Hellwig #define EXT4_BG_INODE_UNINIT	0x0001 /* Inode table/bitmap not in use */
4353dcf5451SChristoph Hellwig #define EXT4_BG_BLOCK_UNINIT	0x0002 /* Block bitmap not in use */
4363dcf5451SChristoph Hellwig #define EXT4_BG_INODE_ZEROED	0x0004 /* On-disk itable initialized to zero */
4373dcf5451SChristoph Hellwig 
4383dcf5451SChristoph Hellwig /*
4393dcf5451SChristoph Hellwig  * Macro-instructions used to manage group descriptors
4403dcf5451SChristoph Hellwig  */
4413dcf5451SChristoph Hellwig #define EXT4_MIN_DESC_SIZE		32
4423dcf5451SChristoph Hellwig #define EXT4_MIN_DESC_SIZE_64BIT	64
4433dcf5451SChristoph Hellwig #define	EXT4_MAX_DESC_SIZE		EXT4_MIN_BLOCK_SIZE
4443dcf5451SChristoph Hellwig #define EXT4_DESC_SIZE(s)		(EXT4_SB(s)->s_desc_size)
4453dcf5451SChristoph Hellwig #ifdef __KERNEL__
4463dcf5451SChristoph Hellwig # define EXT4_BLOCKS_PER_GROUP(s)	(EXT4_SB(s)->s_blocks_per_group)
447281b5995STheodore Ts'o # define EXT4_CLUSTERS_PER_GROUP(s)	(EXT4_SB(s)->s_clusters_per_group)
4483dcf5451SChristoph Hellwig # define EXT4_DESC_PER_BLOCK(s)		(EXT4_SB(s)->s_desc_per_block)
4493dcf5451SChristoph Hellwig # define EXT4_INODES_PER_GROUP(s)	(EXT4_SB(s)->s_inodes_per_group)
4503dcf5451SChristoph Hellwig # define EXT4_DESC_PER_BLOCK_BITS(s)	(EXT4_SB(s)->s_desc_per_block_bits)
4513dcf5451SChristoph Hellwig #else
4523dcf5451SChristoph Hellwig # define EXT4_BLOCKS_PER_GROUP(s)	((s)->s_blocks_per_group)
4533dcf5451SChristoph Hellwig # define EXT4_DESC_PER_BLOCK(s)		(EXT4_BLOCK_SIZE(s) / EXT4_DESC_SIZE(s))
4543dcf5451SChristoph Hellwig # define EXT4_INODES_PER_GROUP(s)	((s)->s_inodes_per_group)
4553dcf5451SChristoph Hellwig #endif
4563dcf5451SChristoph Hellwig 
4573dcf5451SChristoph Hellwig /*
4583dcf5451SChristoph Hellwig  * Constants relative to the data blocks
4593dcf5451SChristoph Hellwig  */
4603dcf5451SChristoph Hellwig #define	EXT4_NDIR_BLOCKS		12
4613dcf5451SChristoph Hellwig #define	EXT4_IND_BLOCK			EXT4_NDIR_BLOCKS
4623dcf5451SChristoph Hellwig #define	EXT4_DIND_BLOCK			(EXT4_IND_BLOCK + 1)
4633dcf5451SChristoph Hellwig #define	EXT4_TIND_BLOCK			(EXT4_DIND_BLOCK + 1)
4643dcf5451SChristoph Hellwig #define	EXT4_N_BLOCKS			(EXT4_TIND_BLOCK + 1)
4653dcf5451SChristoph Hellwig 
4663dcf5451SChristoph Hellwig /*
4673dcf5451SChristoph Hellwig  * Inode flags
4683dcf5451SChristoph Hellwig  */
4693dcf5451SChristoph Hellwig #define	EXT4_SECRM_FL			0x00000001 /* Secure deletion */
4703dcf5451SChristoph Hellwig #define	EXT4_UNRM_FL			0x00000002 /* Undelete */
4713dcf5451SChristoph Hellwig #define	EXT4_COMPR_FL			0x00000004 /* Compress file */
4723dcf5451SChristoph Hellwig #define EXT4_SYNC_FL			0x00000008 /* Synchronous updates */
4733dcf5451SChristoph Hellwig #define EXT4_IMMUTABLE_FL		0x00000010 /* Immutable file */
4743dcf5451SChristoph Hellwig #define EXT4_APPEND_FL			0x00000020 /* writes to file may only append */
4753dcf5451SChristoph Hellwig #define EXT4_NODUMP_FL			0x00000040 /* do not dump file */
4763dcf5451SChristoph Hellwig #define EXT4_NOATIME_FL			0x00000080 /* do not update atime */
4773dcf5451SChristoph Hellwig /* Reserved for compression usage... */
4783dcf5451SChristoph Hellwig #define EXT4_DIRTY_FL			0x00000100
4793dcf5451SChristoph Hellwig #define EXT4_COMPRBLK_FL		0x00000200 /* One or more compressed clusters */
4803dcf5451SChristoph Hellwig #define EXT4_NOCOMPR_FL			0x00000400 /* Don't compress */
4813edc18d8STheodore Ts'o 	/* nb: was previously EXT2_ECOMPR_FL */
4823edc18d8STheodore Ts'o #define EXT4_ENCRYPT_FL			0x00000800 /* encrypted file */
4833dcf5451SChristoph Hellwig /* End compression flags --- maybe not all used */
4843dcf5451SChristoph Hellwig #define EXT4_INDEX_FL			0x00001000 /* hash-indexed directory */
4853dcf5451SChristoph Hellwig #define EXT4_IMAGIC_FL			0x00002000 /* AFS directory */
4863dcf5451SChristoph Hellwig #define EXT4_JOURNAL_DATA_FL		0x00004000 /* file data should be journaled */
4873dcf5451SChristoph Hellwig #define EXT4_NOTAIL_FL			0x00008000 /* file tail should not be merged */
4883dcf5451SChristoph Hellwig #define EXT4_DIRSYNC_FL			0x00010000 /* dirsync behaviour (directories only) */
4893dcf5451SChristoph Hellwig #define EXT4_TOPDIR_FL			0x00020000 /* Top of directory hierarchies*/
4903dcf5451SChristoph Hellwig #define EXT4_HUGE_FILE_FL               0x00040000 /* Set to each huge file */
4913dcf5451SChristoph Hellwig #define EXT4_EXTENTS_FL			0x00080000 /* Inode uses extents */
492c93d8f88SEric Biggers #define EXT4_VERITY_FL			0x00100000 /* Verity protected inode */
493f710b4b9STheodore Ts'o #define EXT4_EA_INODE_FL	        0x00200000 /* Inode used for large EA */
4944337ecd1SEric Whitney /* 0x00400000 was formerly EXT4_EOFBLOCKS_FL */
495b383a73fSIra Weiny 
496b383a73fSIra Weiny #define EXT4_DAX_FL			0x02000000 /* Inode is DAX */
497b383a73fSIra Weiny 
49867cf5b09STao Ma #define EXT4_INLINE_DATA_FL		0x10000000 /* Inode has inline data. */
4998b4953e1STheodore Ts'o #define EXT4_PROJINHERIT_FL		0x20000000 /* Create with parents projid */
500de8ff14cSEric Biggers #define EXT4_CASEFOLD_FL		0x40000000 /* Casefolded directory */
5013dcf5451SChristoph Hellwig #define EXT4_RESERVED_FL		0x80000000 /* reserved for ext4 lib */
5023dcf5451SChristoph Hellwig 
5032a12e147SEric Biggers /* User modifiable flags */
5042a12e147SEric Biggers #define EXT4_FL_USER_MODIFIABLE		(EXT4_SECRM_FL | \
5052a12e147SEric Biggers 					 EXT4_UNRM_FL | \
5062a12e147SEric Biggers 					 EXT4_COMPR_FL | \
5072a12e147SEric Biggers 					 EXT4_SYNC_FL | \
5082a12e147SEric Biggers 					 EXT4_IMMUTABLE_FL | \
5092a12e147SEric Biggers 					 EXT4_APPEND_FL | \
5102a12e147SEric Biggers 					 EXT4_NODUMP_FL | \
5112a12e147SEric Biggers 					 EXT4_NOATIME_FL | \
5122a12e147SEric Biggers 					 EXT4_JOURNAL_DATA_FL | \
5132a12e147SEric Biggers 					 EXT4_NOTAIL_FL | \
5142a12e147SEric Biggers 					 EXT4_DIRSYNC_FL | \
5152a12e147SEric Biggers 					 EXT4_TOPDIR_FL | \
5162a12e147SEric Biggers 					 EXT4_EXTENTS_FL | \
5172a12e147SEric Biggers 					 0x00400000 /* EXT4_EOFBLOCKS_FL */ | \
5182a12e147SEric Biggers 					 EXT4_DAX_FL | \
5192a12e147SEric Biggers 					 EXT4_PROJINHERIT_FL | \
5202a12e147SEric Biggers 					 EXT4_CASEFOLD_FL)
5212a12e147SEric Biggers 
5222a12e147SEric Biggers /* User visible flags */
5232a12e147SEric Biggers #define EXT4_FL_USER_VISIBLE		(EXT4_FL_USER_MODIFIABLE | \
5242a12e147SEric Biggers 					 EXT4_DIRTY_FL | \
5252a12e147SEric Biggers 					 EXT4_COMPRBLK_FL | \
5262a12e147SEric Biggers 					 EXT4_NOCOMPR_FL | \
5272a12e147SEric Biggers 					 EXT4_ENCRYPT_FL | \
5282a12e147SEric Biggers 					 EXT4_INDEX_FL | \
5292a12e147SEric Biggers 					 EXT4_VERITY_FL | \
5302a12e147SEric Biggers 					 EXT4_INLINE_DATA_FL)
5313dcf5451SChristoph Hellwig 
5328fa43a81SDuane Griffin /* Flags that should be inherited by new inodes from their parent. */
5338fa43a81SDuane Griffin #define EXT4_FL_INHERITED (EXT4_SECRM_FL | EXT4_UNRM_FL | EXT4_COMPR_FL |\
5341cd9f097STheodore Ts'o 			   EXT4_SYNC_FL | EXT4_NODUMP_FL | EXT4_NOATIME_FL |\
5358fa43a81SDuane Griffin 			   EXT4_NOCOMPR_FL | EXT4_JOURNAL_DATA_FL |\
536040cb378SLi Xi 			   EXT4_NOTAIL_FL | EXT4_DIRSYNC_FL |\
537b383a73fSIra Weiny 			   EXT4_PROJINHERIT_FL | EXT4_CASEFOLD_FL |\
538b383a73fSIra Weiny 			   EXT4_DAX_FL)
5398fa43a81SDuane Griffin 
5402dc6b0d4SDuane Griffin /* Flags that are appropriate for regular files (all but dir-specific ones). */
5417ddf79a1SWang Shilong #define EXT4_REG_FLMASK (~(EXT4_DIRSYNC_FL | EXT4_TOPDIR_FL | EXT4_CASEFOLD_FL |\
5427ddf79a1SWang Shilong 			   EXT4_PROJINHERIT_FL))
5432dc6b0d4SDuane Griffin 
5442dc6b0d4SDuane Griffin /* Flags that are appropriate for non-directories/regular files. */
5452dc6b0d4SDuane Griffin #define EXT4_OTHER_FLMASK (EXT4_NODUMP_FL | EXT4_NOATIME_FL)
5462dc6b0d4SDuane Griffin 
547abdc644eSyangerkun /* The only flags that should be swapped */
548abdc644eSyangerkun #define EXT4_FL_SHOULD_SWAP (EXT4_HUGE_FILE_FL | EXT4_EXTENTS_FL)
549abdc644eSyangerkun 
550b383a73fSIra Weiny /* Flags which are mutually exclusive to DAX */
551b383a73fSIra Weiny #define EXT4_DAX_MUT_EXCL (EXT4_VERITY_FL | EXT4_ENCRYPT_FL |\
552aa2f7792SXiao Yang 			   EXT4_JOURNAL_DATA_FL | EXT4_INLINE_DATA_FL)
553b383a73fSIra Weiny 
5542dc6b0d4SDuane Griffin /* Mask out flags that are inappropriate for the given type of inode. */
ext4_mask_flags(umode_t mode,__u32 flags)5552dc6b0d4SDuane Griffin static inline __u32 ext4_mask_flags(umode_t mode, __u32 flags)
5562dc6b0d4SDuane Griffin {
5572dc6b0d4SDuane Griffin 	if (S_ISDIR(mode))
5582dc6b0d4SDuane Griffin 		return flags;
5592dc6b0d4SDuane Griffin 	else if (S_ISREG(mode))
5602dc6b0d4SDuane Griffin 		return flags & EXT4_REG_FLMASK;
5612dc6b0d4SDuane Griffin 	else
5622dc6b0d4SDuane Griffin 		return flags & EXT4_OTHER_FLMASK;
5632dc6b0d4SDuane Griffin }
5642dc6b0d4SDuane Griffin 
56512e9b892SDmitry Monakhov /*
56612e9b892SDmitry Monakhov  * Inode flags used for atomic set/get
56712e9b892SDmitry Monakhov  */
56812e9b892SDmitry Monakhov enum {
56912e9b892SDmitry Monakhov 	EXT4_INODE_SECRM	= 0,	/* Secure deletion */
57012e9b892SDmitry Monakhov 	EXT4_INODE_UNRM		= 1,	/* Undelete */
57112e9b892SDmitry Monakhov 	EXT4_INODE_COMPR	= 2,	/* Compress file */
57212e9b892SDmitry Monakhov 	EXT4_INODE_SYNC		= 3,	/* Synchronous updates */
57312e9b892SDmitry Monakhov 	EXT4_INODE_IMMUTABLE	= 4,	/* Immutable file */
57412e9b892SDmitry Monakhov 	EXT4_INODE_APPEND	= 5,	/* writes to file may only append */
57512e9b892SDmitry Monakhov 	EXT4_INODE_NODUMP	= 6,	/* do not dump file */
57612e9b892SDmitry Monakhov 	EXT4_INODE_NOATIME	= 7,	/* do not update atime */
57712e9b892SDmitry Monakhov /* Reserved for compression usage... */
57812e9b892SDmitry Monakhov 	EXT4_INODE_DIRTY	= 8,
57912e9b892SDmitry Monakhov 	EXT4_INODE_COMPRBLK	= 9,	/* One or more compressed clusters */
58012e9b892SDmitry Monakhov 	EXT4_INODE_NOCOMPR	= 10,	/* Don't compress */
581f542fbe8STheodore Ts'o 	EXT4_INODE_ENCRYPT	= 11,	/* Encrypted file */
58212e9b892SDmitry Monakhov /* End compression flags --- maybe not all used */
58312e9b892SDmitry Monakhov 	EXT4_INODE_INDEX	= 12,	/* hash-indexed directory */
58412e9b892SDmitry Monakhov 	EXT4_INODE_IMAGIC	= 13,	/* AFS directory */
58512e9b892SDmitry Monakhov 	EXT4_INODE_JOURNAL_DATA	= 14,	/* file data should be journaled */
58612e9b892SDmitry Monakhov 	EXT4_INODE_NOTAIL	= 15,	/* file tail should not be merged */
58712e9b892SDmitry Monakhov 	EXT4_INODE_DIRSYNC	= 16,	/* dirsync behaviour (directories only) */
58812e9b892SDmitry Monakhov 	EXT4_INODE_TOPDIR	= 17,	/* Top of directory hierarchies*/
58912e9b892SDmitry Monakhov 	EXT4_INODE_HUGE_FILE	= 18,	/* Set to each huge file */
59012e9b892SDmitry Monakhov 	EXT4_INODE_EXTENTS	= 19,	/* Inode uses extents */
591c93d8f88SEric Biggers 	EXT4_INODE_VERITY	= 20,	/* Verity protected inode */
59212e9b892SDmitry Monakhov 	EXT4_INODE_EA_INODE	= 21,	/* Inode used for large EA */
5934337ecd1SEric Whitney /* 22 was formerly EXT4_INODE_EOFBLOCKS */
594b383a73fSIra Weiny 	EXT4_INODE_DAX		= 25,	/* Inode is DAX */
59567cf5b09STao Ma 	EXT4_INODE_INLINE_DATA	= 28,	/* Data in inode. */
5968b4953e1STheodore Ts'o 	EXT4_INODE_PROJINHERIT	= 29,	/* Create with parents projid */
597de8ff14cSEric Biggers 	EXT4_INODE_CASEFOLD	= 30,	/* Casefolded directory */
59812e9b892SDmitry Monakhov 	EXT4_INODE_RESERVED	= 31,	/* reserved for ext4 lib */
59912e9b892SDmitry Monakhov };
60012e9b892SDmitry Monakhov 
60112e9b892SDmitry Monakhov /*
6029a4c8019SCarlos Maiolino  * Since it's pretty easy to mix up bit numbers and hex values, we use a
6039a4c8019SCarlos Maiolino  * build-time check to make sure that EXT4_XXX_FL is consistent with respect to
6049a4c8019SCarlos Maiolino  * EXT4_INODE_XXX. If all is well, the macros will be dropped, so, it won't cost
6059a4c8019SCarlos Maiolino  * any extra space in the compiled kernel image, otherwise, the build will fail.
6069a4c8019SCarlos Maiolino  * It's important that these values are the same, since we are using
6079a4c8019SCarlos Maiolino  * EXT4_INODE_XXX to test for flag values, but EXT4_XXX_FL must be consistent
6089a4c8019SCarlos Maiolino  * with the values of FS_XXX_FL defined in include/linux/fs.h and the on-disk
6099a4c8019SCarlos Maiolino  * values found in ext2, ext3 and ext4 filesystems, and of course the values
6109a4c8019SCarlos Maiolino  * defined in e2fsprogs.
61112e9b892SDmitry Monakhov  *
61212e9b892SDmitry Monakhov  * It's not paranoia if the Murphy's Law really *is* out to get you.  :-)
61312e9b892SDmitry Monakhov  */
6143bf678a0SGaosheng Cui #define TEST_FLAG_VALUE(FLAG) (EXT4_##FLAG##_FL == (1U << EXT4_INODE_##FLAG))
6159a4c8019SCarlos Maiolino #define CHECK_FLAG_VALUE(FLAG) BUILD_BUG_ON(!TEST_FLAG_VALUE(FLAG))
6169a4c8019SCarlos Maiolino 
ext4_check_flag_values(void)61712e9b892SDmitry Monakhov static inline void ext4_check_flag_values(void)
61812e9b892SDmitry Monakhov {
61912e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(SECRM);
62012e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(UNRM);
62112e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(COMPR);
62212e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(SYNC);
62312e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(IMMUTABLE);
62412e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(APPEND);
62512e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(NODUMP);
62612e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(NOATIME);
62712e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(DIRTY);
62812e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(COMPRBLK);
62912e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(NOCOMPR);
6303edc18d8STheodore Ts'o 	CHECK_FLAG_VALUE(ENCRYPT);
63112e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(INDEX);
63212e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(IMAGIC);
63312e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(JOURNAL_DATA);
63412e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(NOTAIL);
63512e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(DIRSYNC);
63612e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(TOPDIR);
63712e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(HUGE_FILE);
63812e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(EXTENTS);
639c93d8f88SEric Biggers 	CHECK_FLAG_VALUE(VERITY);
64012e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(EA_INODE);
64167cf5b09STao Ma 	CHECK_FLAG_VALUE(INLINE_DATA);
6428b4953e1STheodore Ts'o 	CHECK_FLAG_VALUE(PROJINHERIT);
643de8ff14cSEric Biggers 	CHECK_FLAG_VALUE(CASEFOLD);
64412e9b892SDmitry Monakhov 	CHECK_FLAG_VALUE(RESERVED);
64512e9b892SDmitry Monakhov }
64612e9b892SDmitry Monakhov 
6474d92dc0fSBen Hutchings #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
6484d92dc0fSBen Hutchings struct compat_ext4_new_group_input {
6494d92dc0fSBen Hutchings 	u32 group;
6504d92dc0fSBen Hutchings 	compat_u64 block_bitmap;
6514d92dc0fSBen Hutchings 	compat_u64 inode_bitmap;
6524d92dc0fSBen Hutchings 	compat_u64 inode_table;
6534d92dc0fSBen Hutchings 	u32 blocks_count;
6544d92dc0fSBen Hutchings 	u16 reserved_blocks;
6554d92dc0fSBen Hutchings 	u16 unused;
6564d92dc0fSBen Hutchings };
6574d92dc0fSBen Hutchings #endif
6584d92dc0fSBen Hutchings 
6593dcf5451SChristoph Hellwig /* The struct ext4_new_group_input in kernel space, with free_blocks_count */
6603dcf5451SChristoph Hellwig struct ext4_new_group_data {
6613dcf5451SChristoph Hellwig 	__u32 group;
6623dcf5451SChristoph Hellwig 	__u64 block_bitmap;
6633dcf5451SChristoph Hellwig 	__u64 inode_bitmap;
6643dcf5451SChristoph Hellwig 	__u64 inode_table;
6653dcf5451SChristoph Hellwig 	__u32 blocks_count;
6663dcf5451SChristoph Hellwig 	__u16 reserved_blocks;
667d77147ffSharshads 	__u16 mdata_blocks;
668d77147ffSharshads 	__u32 free_clusters_count;
6693dcf5451SChristoph Hellwig };
6703dcf5451SChristoph Hellwig 
67133afdcc5SYongqiang Yang /* Indexes used to index group tables in ext4_new_group_data */
67233afdcc5SYongqiang Yang enum {
67333afdcc5SYongqiang Yang 	BLOCK_BITMAP = 0,	/* block bitmap */
67433afdcc5SYongqiang Yang 	INODE_BITMAP,		/* inode bitmap */
67533afdcc5SYongqiang Yang 	INODE_TABLE,		/* inode tables */
67633afdcc5SYongqiang Yang 	GROUP_TABLE_COUNT,
67733afdcc5SYongqiang Yang };
67833afdcc5SYongqiang Yang 
6793dcf5451SChristoph Hellwig /*
68079e83036SEric Sandeen  * Flags used by ext4_map_blocks()
6813dcf5451SChristoph Hellwig  */
682556615dcSLukas Czerner 	/* Allocate any needed blocks and/or convert an unwritten
683c2177057STheodore Ts'o 	   extent to be an initialized ext4 */
6842ac3b6e0STheodore Ts'o #define EXT4_GET_BLOCKS_CREATE			0x0001
685556615dcSLukas Czerner 	/* Request the creation of an unwritten extent */
686556615dcSLukas Czerner #define EXT4_GET_BLOCKS_UNWRIT_EXT		0x0002
687556615dcSLukas Czerner #define EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT	(EXT4_GET_BLOCKS_UNWRIT_EXT|\
688c2177057STheodore Ts'o 						 EXT4_GET_BLOCKS_CREATE)
68927dd4385SLukas Czerner 	/* Caller is from the delayed allocation writeout path
69027dd4385SLukas Czerner 	 * finally doing the actual allocation of delayed blocks */
69103f5d8bcSJan Kara #define EXT4_GET_BLOCKS_DELALLOC_RESERVE	0x0004
6920031462bSMingming Cao 	/* caller is from the direct IO path, request to creation of an
693556615dcSLukas Czerner 	unwritten extents if not allocated, split the unwritten
6940031462bSMingming Cao 	extent if blocks has been preallocated already*/
695c7064ef1SJiaying Zhang #define EXT4_GET_BLOCKS_PRE_IO			0x0008
6961296cc85SAneesh Kumar K.V #define EXT4_GET_BLOCKS_CONVERT			0x0010
697c7064ef1SJiaying Zhang #define EXT4_GET_BLOCKS_IO_CREATE_EXT		(EXT4_GET_BLOCKS_PRE_IO|\
698556615dcSLukas Czerner 					 EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT)
699c7064ef1SJiaying Zhang 	/* Convert extent to initialized after IO complete */
700c7064ef1SJiaying Zhang #define EXT4_GET_BLOCKS_IO_CONVERT_EXT		(EXT4_GET_BLOCKS_CONVERT|\
701556615dcSLukas Czerner 					 EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT)
70227dd4385SLukas Czerner 	/* Eventual metadata allocation (due to growing extent tree)
70327dd4385SLukas Czerner 	 * should not fail, so try to use reserved blocks for that.*/
70427dd4385SLukas Czerner #define EXT4_GET_BLOCKS_METADATA_NOFAIL		0x0020
705556b27abSVivek Haldar 	/* Don't normalize allocation size (used for fallocate) */
706556b27abSVivek Haldar #define EXT4_GET_BLOCKS_NO_NORMALIZE		0x0040
707b8a86845SLukas Czerner 	/* Convert written extents to unwritten */
7082dcba478SJan Kara #define EXT4_GET_BLOCKS_CONVERT_UNWRITTEN	0x0100
709c86d8db3SJan Kara 	/* Write zeros to newly created written extents */
710c86d8db3SJan Kara #define EXT4_GET_BLOCKS_ZERO			0x0200
711c86d8db3SJan Kara #define EXT4_GET_BLOCKS_CREATE_ZERO		(EXT4_GET_BLOCKS_CREATE |\
712c86d8db3SJan Kara 					EXT4_GET_BLOCKS_ZERO)
713ee0876bcSJan Kara 	/* Caller will submit data before dropping transaction handle. This
714ee0876bcSJan Kara 	 * allows jbd2 to avoid submitting data before commit. */
715ee0876bcSJan Kara #define EXT4_GET_BLOCKS_IO_SUBMIT		0x0400
7169558cf14SZhang Yi 	/* Caller is in the atomic contex, find extent if it has been cached */
7179558cf14SZhang Yi #define EXT4_GET_BLOCKS_CACHED_NOWAIT		0x0800
7183dcf5451SChristoph Hellwig 
7193dcf5451SChristoph Hellwig /*
7207869a4a6STheodore Ts'o  * The bit position of these flags must not overlap with any of the
721ed8a1a76STheodore Ts'o  * EXT4_GET_BLOCKS_*.  They are used by ext4_find_extent(),
722107a7bd3STheodore Ts'o  * read_extent_tree_block(), ext4_split_extent_at(),
7237869a4a6STheodore Ts'o  * ext4_ext_insert_extent(), and ext4_ext_create_new_leaf().
7247869a4a6STheodore Ts'o  * EXT4_EX_NOCACHE is used to indicate that the we shouldn't be
7257869a4a6STheodore Ts'o  * caching the extents when reading from the extent tree while a
7267869a4a6STheodore Ts'o  * truncate or punch hole operation is in progress.
727107a7bd3STheodore Ts'o  */
728d26e2c4dSTheodore Ts'o #define EXT4_EX_NOCACHE				0x40000000
729d26e2c4dSTheodore Ts'o #define EXT4_EX_FORCE_CACHE			0x20000000
73073c384c0STheodore Ts'o #define EXT4_EX_NOFAIL				0x10000000
731107a7bd3STheodore Ts'o 
732107a7bd3STheodore Ts'o /*
733e6362609STheodore Ts'o  * Flags used by ext4_free_blocks
734e6362609STheodore Ts'o  */
735e6362609STheodore Ts'o #define EXT4_FREE_BLOCKS_METADATA		0x0001
736e6362609STheodore Ts'o #define EXT4_FREE_BLOCKS_FORGET			0x0002
7371f2acb60STheodore Ts'o #define EXT4_FREE_BLOCKS_VALIDATED		0x0004
7387132de74SMaxim Patlasov #define EXT4_FREE_BLOCKS_NO_QUOT_UPDATE		0x0008
73984130193STheodore Ts'o #define EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER	0x0010
74084130193STheodore Ts'o #define EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER	0x0020
7419fe67149SEric Whitney #define EXT4_FREE_BLOCKS_RERESERVE_CLUSTER      0x0040
742e6362609STheodore Ts'o 
743899ad0ceSBen Hutchings #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
7443dcf5451SChristoph Hellwig /*
7453dcf5451SChristoph Hellwig  * ioctl commands in 32 bit emulation
7463dcf5451SChristoph Hellwig  */
7473dcf5451SChristoph Hellwig #define EXT4_IOC32_GETVERSION		_IOR('f', 3, int)
7483dcf5451SChristoph Hellwig #define EXT4_IOC32_SETVERSION		_IOW('f', 4, int)
7493dcf5451SChristoph Hellwig #define EXT4_IOC32_GETRSVSZ		_IOR('f', 5, int)
7503dcf5451SChristoph Hellwig #define EXT4_IOC32_SETRSVSZ		_IOW('f', 6, int)
7513dcf5451SChristoph Hellwig #define EXT4_IOC32_GROUP_EXTEND		_IOW('f', 7, unsigned int)
7524d92dc0fSBen Hutchings #define EXT4_IOC32_GROUP_ADD		_IOW('f', 8, struct compat_ext4_new_group_input)
7533dcf5451SChristoph Hellwig #define EXT4_IOC32_GETVERSION_OLD	FS_IOC32_GETVERSION
7543dcf5451SChristoph Hellwig #define EXT4_IOC32_SETVERSION_OLD	FS_IOC32_SETVERSION
755899ad0ceSBen Hutchings #endif
7563dcf5451SChristoph Hellwig 
757b595076aSUwe Kleine-König /* Max physical block we can address w/o extents */
758fb0a387dSEric Sandeen #define EXT4_MAX_BLOCK_FILE_PHYS	0xFFFFFFFF
759fb0a387dSEric Sandeen 
760bcd8e91fSTheodore Ts'o /* Max logical block we can support */
7619f44eda1SRitesh Harjani #define EXT4_MAX_LOGICAL_BLOCK		0xFFFFFFFE
762bcd8e91fSTheodore Ts'o 
7633dcf5451SChristoph Hellwig /*
7643dcf5451SChristoph Hellwig  * Structure of an inode on the disk
7653dcf5451SChristoph Hellwig  */
7663dcf5451SChristoph Hellwig struct ext4_inode {
7673dcf5451SChristoph Hellwig 	__le16	i_mode;		/* File mode */
7683dcf5451SChristoph Hellwig 	__le16	i_uid;		/* Low 16 bits of Owner Uid */
7693dcf5451SChristoph Hellwig 	__le32	i_size_lo;	/* Size in bytes */
7703dcf5451SChristoph Hellwig 	__le32	i_atime;	/* Access time */
7713dcf5451SChristoph Hellwig 	__le32	i_ctime;	/* Inode Change time */
7723dcf5451SChristoph Hellwig 	__le32	i_mtime;	/* Modification time */
7733dcf5451SChristoph Hellwig 	__le32	i_dtime;	/* Deletion Time */
7743dcf5451SChristoph Hellwig 	__le16	i_gid;		/* Low 16 bits of Group Id */
7753dcf5451SChristoph Hellwig 	__le16	i_links_count;	/* Links count */
7763dcf5451SChristoph Hellwig 	__le32	i_blocks_lo;	/* Blocks count */
7773dcf5451SChristoph Hellwig 	__le32	i_flags;	/* File flags */
7783dcf5451SChristoph Hellwig 	union {
7793dcf5451SChristoph Hellwig 		struct {
7803dcf5451SChristoph Hellwig 			__le32  l_i_version;
7813dcf5451SChristoph Hellwig 		} linux1;
7823dcf5451SChristoph Hellwig 		struct {
7833dcf5451SChristoph Hellwig 			__u32  h_i_translator;
7843dcf5451SChristoph Hellwig 		} hurd1;
7853dcf5451SChristoph Hellwig 		struct {
7863dcf5451SChristoph Hellwig 			__u32  m_i_reserved1;
7873dcf5451SChristoph Hellwig 		} masix1;
7883dcf5451SChristoph Hellwig 	} osd1;				/* OS dependent 1 */
7893dcf5451SChristoph Hellwig 	__le32	i_block[EXT4_N_BLOCKS];/* Pointers to blocks */
7903dcf5451SChristoph Hellwig 	__le32	i_generation;	/* File version (for NFS) */
7913dcf5451SChristoph Hellwig 	__le32	i_file_acl_lo;	/* File ACL */
7923dcf5451SChristoph Hellwig 	__le32	i_size_high;
7933dcf5451SChristoph Hellwig 	__le32	i_obso_faddr;	/* Obsoleted fragment address */
7943dcf5451SChristoph Hellwig 	union {
7953dcf5451SChristoph Hellwig 		struct {
7963dcf5451SChristoph Hellwig 			__le16	l_i_blocks_high; /* were l_i_reserved1 */
7973dcf5451SChristoph Hellwig 			__le16	l_i_file_acl_high;
7983dcf5451SChristoph Hellwig 			__le16	l_i_uid_high;	/* these 2 fields */
7993dcf5451SChristoph Hellwig 			__le16	l_i_gid_high;	/* were reserved2[0] */
800e6153918SDarrick J. Wong 			__le16	l_i_checksum_lo;/* crc32c(uuid+inum+inode) LE */
801e6153918SDarrick J. Wong 			__le16	l_i_reserved;
8023dcf5451SChristoph Hellwig 		} linux2;
8033dcf5451SChristoph Hellwig 		struct {
8043dcf5451SChristoph Hellwig 			__le16	h_i_reserved1;	/* Obsoleted fragment number/size which are removed in ext4 */
8053dcf5451SChristoph Hellwig 			__u16	h_i_mode_high;
8063dcf5451SChristoph Hellwig 			__u16	h_i_uid_high;
8073dcf5451SChristoph Hellwig 			__u16	h_i_gid_high;
8083dcf5451SChristoph Hellwig 			__u32	h_i_author;
8093dcf5451SChristoph Hellwig 		} hurd2;
8103dcf5451SChristoph Hellwig 		struct {
8113dcf5451SChristoph Hellwig 			__le16	h_i_reserved1;	/* Obsoleted fragment number/size which are removed in ext4 */
8123dcf5451SChristoph Hellwig 			__le16	m_i_file_acl_high;
8133dcf5451SChristoph Hellwig 			__u32	m_i_reserved2[2];
8143dcf5451SChristoph Hellwig 		} masix2;
8153dcf5451SChristoph Hellwig 	} osd2;				/* OS dependent 2 */
8163dcf5451SChristoph Hellwig 	__le16	i_extra_isize;
817e6153918SDarrick J. Wong 	__le16	i_checksum_hi;	/* crc32c(uuid+inum+inode) BE */
8183dcf5451SChristoph Hellwig 	__le32  i_ctime_extra;  /* extra Change time      (nsec << 2 | epoch) */
8193dcf5451SChristoph Hellwig 	__le32  i_mtime_extra;  /* extra Modification time(nsec << 2 | epoch) */
8203dcf5451SChristoph Hellwig 	__le32  i_atime_extra;  /* extra Access time      (nsec << 2 | epoch) */
8213dcf5451SChristoph Hellwig 	__le32  i_crtime;       /* File Creation time */
8223dcf5451SChristoph Hellwig 	__le32  i_crtime_extra; /* extra FileCreationtime (nsec << 2 | epoch) */
8233dcf5451SChristoph Hellwig 	__le32  i_version_hi;	/* high 32 bits for 64-bit version */
8248b4953e1STheodore Ts'o 	__le32	i_projid;	/* Project ID */
8253dcf5451SChristoph Hellwig };
8263dcf5451SChristoph Hellwig 
8273dcf5451SChristoph Hellwig #define EXT4_EPOCH_BITS 2
8283dcf5451SChristoph Hellwig #define EXT4_EPOCH_MASK ((1 << EXT4_EPOCH_BITS) - 1)
8293dcf5451SChristoph Hellwig #define EXT4_NSEC_MASK  (~0UL << EXT4_EPOCH_BITS)
8303dcf5451SChristoph Hellwig 
8313dcf5451SChristoph Hellwig /*
8323dcf5451SChristoph Hellwig  * Extended fields will fit into an inode if the filesystem was formatted
8333dcf5451SChristoph Hellwig  * with large inodes (-I 256 or larger) and there are not currently any EAs
8343dcf5451SChristoph Hellwig  * consuming all of the available space. For new inodes we always reserve
8353dcf5451SChristoph Hellwig  * enough space for the kernel's known extended fields, but for inodes
8363dcf5451SChristoph Hellwig  * created with an old kernel this might not have been the case. None of
8373dcf5451SChristoph Hellwig  * the extended inode fields is critical for correct filesystem operation.
8383dcf5451SChristoph Hellwig  * This macro checks if a certain field fits in the inode. Note that
8393dcf5451SChristoph Hellwig  * inode-size = GOOD_OLD_INODE_SIZE + i_extra_isize
8403dcf5451SChristoph Hellwig  */
8413dcf5451SChristoph Hellwig #define EXT4_FITS_IN_INODE(ext4_inode, einode, field)	\
8423dcf5451SChristoph Hellwig 	((offsetof(typeof(*ext4_inode), field) +	\
8433dcf5451SChristoph Hellwig 	  sizeof((ext4_inode)->field))			\
8443dcf5451SChristoph Hellwig 	<= (EXT4_GOOD_OLD_INODE_SIZE +			\
8453dcf5451SChristoph Hellwig 	    (einode)->i_extra_isize))			\
8463dcf5451SChristoph Hellwig 
847a4dad1aeSDavid Turner /*
848a4dad1aeSDavid Turner  * We use an encoding that preserves the times for extra epoch "00":
849a4dad1aeSDavid Turner  *
850a4dad1aeSDavid Turner  * extra  msb of                         adjust for signed
851a4dad1aeSDavid Turner  * epoch  32-bit                         32-bit tv_sec to
852a4dad1aeSDavid Turner  * bits   time    decoded 64-bit tv_sec  64-bit tv_sec      valid time range
853a4dad1aeSDavid Turner  * 0 0    1    -0x80000000..-0x00000001  0x000000000 1901-12-13..1969-12-31
854a4dad1aeSDavid Turner  * 0 0    0    0x000000000..0x07fffffff  0x000000000 1970-01-01..2038-01-19
855a4dad1aeSDavid Turner  * 0 1    1    0x080000000..0x0ffffffff  0x100000000 2038-01-19..2106-02-07
856a4dad1aeSDavid Turner  * 0 1    0    0x100000000..0x17fffffff  0x100000000 2106-02-07..2174-02-25
857a4dad1aeSDavid Turner  * 1 0    1    0x180000000..0x1ffffffff  0x200000000 2174-02-25..2242-03-16
858a4dad1aeSDavid Turner  * 1 0    0    0x200000000..0x27fffffff  0x200000000 2242-03-16..2310-04-04
859a4dad1aeSDavid Turner  * 1 1    1    0x280000000..0x2ffffffff  0x300000000 2310-04-04..2378-04-22
860a4dad1aeSDavid Turner  * 1 1    0    0x300000000..0x37fffffff  0x300000000 2378-04-22..2446-05-10
861a4dad1aeSDavid Turner  *
862a4dad1aeSDavid Turner  * Note that previous versions of the kernel on 64-bit systems would
863a4dad1aeSDavid Turner  * incorrectly use extra epoch bits 1,1 for dates between 1901 and
864a4dad1aeSDavid Turner  * 1970.  e2fsck will correct this, assuming that it is run on the
865a4dad1aeSDavid Turner  * affected filesystem before 2242.
866a4dad1aeSDavid Turner  */
867a4dad1aeSDavid Turner 
ext4_encode_extra_time(struct timespec64 ts)8681bc33893SJeff Layton static inline __le32 ext4_encode_extra_time(struct timespec64 ts)
8693dcf5451SChristoph Hellwig {
8701bc33893SJeff Layton 	u32 extra = ((ts.tv_sec - (s32)ts.tv_sec) >> 32) & EXT4_EPOCH_MASK;
8711bc33893SJeff Layton 	return cpu_to_le32(extra | (ts.tv_nsec << EXT4_EPOCH_BITS));
8723dcf5451SChristoph Hellwig }
8733dcf5451SChristoph Hellwig 
ext4_decode_extra_time(__le32 base,__le32 extra)8741bc33893SJeff Layton static inline struct timespec64 ext4_decode_extra_time(__le32 base,
8757b62b293SArnd Bergmann 						       __le32 extra)
8763dcf5451SChristoph Hellwig {
8771bc33893SJeff Layton 	struct timespec64 ts = { .tv_sec = (signed)le32_to_cpu(base) };
8781bc33893SJeff Layton 
879cd2d9922STheodore Ts'o 	if (unlikely(extra & cpu_to_le32(EXT4_EPOCH_MASK)))
8801bc33893SJeff Layton 		ts.tv_sec += (u64)(le32_to_cpu(extra) & EXT4_EPOCH_MASK) << 32;
8811bc33893SJeff Layton 	ts.tv_nsec = (le32_to_cpu(extra) & EXT4_NSEC_MASK) >> EXT4_EPOCH_BITS;
8821bc33893SJeff Layton 	return ts;
8833dcf5451SChristoph Hellwig }
8843dcf5451SChristoph Hellwig 
8851bc33893SJeff Layton #define EXT4_INODE_SET_XTIME_VAL(xtime, inode, raw_inode, ts)			\
8863dcf5451SChristoph Hellwig do {										\
88795582b00SDeepa Dinamani 	if (EXT4_FITS_IN_INODE(raw_inode, EXT4_I(inode), xtime ## _extra)) {	\
8881bc33893SJeff Layton 		(raw_inode)->xtime = cpu_to_le32((ts).tv_sec);			\
8891bc33893SJeff Layton 		(raw_inode)->xtime ## _extra = ext4_encode_extra_time(ts);	\
8901bc33893SJeff Layton 	} else									\
8911bc33893SJeff Layton 		(raw_inode)->xtime = cpu_to_le32(clamp_t(int32_t, (ts).tv_sec, S32_MIN, S32_MAX));	\
8923dcf5451SChristoph Hellwig } while (0)
8933dcf5451SChristoph Hellwig 
8941bc33893SJeff Layton #define EXT4_INODE_SET_XTIME(xtime, inode, raw_inode)				\
8951bc33893SJeff Layton 	EXT4_INODE_SET_XTIME_VAL(xtime, inode, raw_inode, (inode)->xtime)
8961bc33893SJeff Layton 
8971bc33893SJeff Layton #define EXT4_INODE_SET_CTIME(inode, raw_inode)					\
8981bc33893SJeff Layton 	EXT4_INODE_SET_XTIME_VAL(i_ctime, inode, raw_inode, inode_get_ctime(inode))
8991bc33893SJeff Layton 
9003dcf5451SChristoph Hellwig #define EXT4_EINODE_SET_XTIME(xtime, einode, raw_inode)				\
9013dcf5451SChristoph Hellwig 	if (EXT4_FITS_IN_INODE(raw_inode, einode, xtime))			\
9021bc33893SJeff Layton 		EXT4_INODE_SET_XTIME_VAL(xtime, &((einode)->vfs_inode),		\
9031bc33893SJeff Layton 					 raw_inode, (einode)->xtime)
9041bc33893SJeff Layton 
9051bc33893SJeff Layton #define EXT4_INODE_GET_XTIME_VAL(xtime, inode, raw_inode)			\
9061bc33893SJeff Layton 	(EXT4_FITS_IN_INODE(raw_inode, EXT4_I(inode), xtime ## _extra) ?	\
9071bc33893SJeff Layton 		ext4_decode_extra_time((raw_inode)->xtime,				\
9081bc33893SJeff Layton 				       (raw_inode)->xtime ## _extra) :		\
9091bc33893SJeff Layton 		(struct timespec64) {						\
9101bc33893SJeff Layton 			.tv_sec = (signed)le32_to_cpu((raw_inode)->xtime)	\
9111bc33893SJeff Layton 		})
9123dcf5451SChristoph Hellwig 
9133dcf5451SChristoph Hellwig #define EXT4_INODE_GET_XTIME(xtime, inode, raw_inode)				\
9143dcf5451SChristoph Hellwig do {										\
9151bc33893SJeff Layton 	(inode)->xtime = EXT4_INODE_GET_XTIME_VAL(xtime, inode, raw_inode);	\
9163dcf5451SChristoph Hellwig } while (0)
9173dcf5451SChristoph Hellwig 
9181bc33893SJeff Layton #define EXT4_INODE_GET_CTIME(inode, raw_inode)					\
9191bc33893SJeff Layton do {										\
9201bc33893SJeff Layton 	inode_set_ctime_to_ts(inode,						\
9211bc33893SJeff Layton 		EXT4_INODE_GET_XTIME_VAL(i_ctime, inode, raw_inode));		\
9221bc33893SJeff Layton } while (0)
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)) 			\
9271bc33893SJeff Layton 		(einode)->xtime =						\
9281bc33893SJeff Layton 			EXT4_INODE_GET_XTIME_VAL(xtime, &(einode->vfs_inode),	\
9291bc33893SJeff Layton 						 raw_inode);			\
93019ea8060STheodore Ts'o 	else									\
9311bc33893SJeff Layton 		(einode)->xtime = (struct timespec64){0, 0};			\
9323dcf5451SChristoph Hellwig } while (0)
9333dcf5451SChristoph Hellwig 
9343dcf5451SChristoph Hellwig #define i_disk_version osd1.linux1.l_i_version
9353dcf5451SChristoph Hellwig 
9363dcf5451SChristoph Hellwig #if defined(__KERNEL__) || defined(__linux__)
9373dcf5451SChristoph Hellwig #define i_reserved1	osd1.linux1.l_i_reserved1
9383dcf5451SChristoph Hellwig #define i_file_acl_high	osd2.linux2.l_i_file_acl_high
9393dcf5451SChristoph Hellwig #define i_blocks_high	osd2.linux2.l_i_blocks_high
9403dcf5451SChristoph Hellwig #define i_uid_low	i_uid
9413dcf5451SChristoph Hellwig #define i_gid_low	i_gid
9423dcf5451SChristoph Hellwig #define i_uid_high	osd2.linux2.l_i_uid_high
9433dcf5451SChristoph Hellwig #define i_gid_high	osd2.linux2.l_i_gid_high
944e6153918SDarrick J. Wong #define i_checksum_lo	osd2.linux2.l_i_checksum_lo
9453dcf5451SChristoph Hellwig 
9463dcf5451SChristoph Hellwig #elif defined(__GNU__)
9473dcf5451SChristoph Hellwig 
9483dcf5451SChristoph Hellwig #define i_translator	osd1.hurd1.h_i_translator
9493dcf5451SChristoph Hellwig #define i_uid_high	osd2.hurd2.h_i_uid_high
9503dcf5451SChristoph Hellwig #define i_gid_high	osd2.hurd2.h_i_gid_high
9513dcf5451SChristoph Hellwig #define i_author	osd2.hurd2.h_i_author
9523dcf5451SChristoph Hellwig 
9533dcf5451SChristoph Hellwig #elif defined(__masix__)
9543dcf5451SChristoph Hellwig 
9553dcf5451SChristoph Hellwig #define i_reserved1	osd1.masix1.m_i_reserved1
9563dcf5451SChristoph Hellwig #define i_file_acl_high	osd2.masix2.m_i_file_acl_high
9573dcf5451SChristoph Hellwig #define i_reserved2	osd2.masix2.m_i_reserved2
9583dcf5451SChristoph Hellwig 
9593dcf5451SChristoph Hellwig #endif /* defined(__KERNEL__) || defined(__linux__) */
9603dcf5451SChristoph Hellwig 
961c0677e6dSZheng Liu #include "extents_status.h"
9626866d7b3SHarshad Shirwadkar #include "fast_commit.h"
963c0677e6dSZheng Liu 
964d444c3c3STheodore Ts'o /*
965daf647d2STheodore Ts'o  * Lock subclasses for i_data_sem in the ext4_inode_info structure.
966daf647d2STheodore Ts'o  *
967daf647d2STheodore Ts'o  * These are needed to avoid lockdep false positives when we need to
968daf647d2STheodore Ts'o  * allocate blocks to the quota inode during ext4_map_blocks(), while
969daf647d2STheodore Ts'o  * holding i_data_sem for a normal (non-quota) inode.  Since we don't
970daf647d2STheodore Ts'o  * do quota tracking for the quota inode, this avoids deadlock (as
971daf647d2STheodore Ts'o  * well as infinite recursion, since it isn't turtles all the way
972daf647d2STheodore Ts'o  * down...)
973daf647d2STheodore Ts'o  *
974daf647d2STheodore Ts'o  *  I_DATA_SEM_NORMAL - Used for most inodes
975daf647d2STheodore Ts'o  *  I_DATA_SEM_OTHER  - Used by move_inode.c for the second normal inode
976daf647d2STheodore Ts'o  *			  where the second inode has larger inode number
977daf647d2STheodore Ts'o  *			  than the first
978daf647d2STheodore Ts'o  *  I_DATA_SEM_QUOTA  - Used for quota inodes only
979aff3bea9STheodore Ts'o  *  I_DATA_SEM_EA     - Used for ea_inodes only
980daf647d2STheodore Ts'o  */
981daf647d2STheodore Ts'o enum {
982daf647d2STheodore Ts'o 	I_DATA_SEM_NORMAL = 0,
983daf647d2STheodore Ts'o 	I_DATA_SEM_OTHER,
984daf647d2STheodore Ts'o 	I_DATA_SEM_QUOTA,
985aff3bea9STheodore Ts'o 	I_DATA_SEM_EA
986daf647d2STheodore Ts'o };
987daf647d2STheodore Ts'o 
988daf647d2STheodore Ts'o 
989daf647d2STheodore Ts'o /*
990d444c3c3STheodore Ts'o  * fourth extended file system inode data in memory
991d444c3c3STheodore Ts'o  */
992d444c3c3STheodore Ts'o struct ext4_inode_info {
993d444c3c3STheodore Ts'o 	__le32	i_data[15];	/* unconverted */
994d444c3c3STheodore Ts'o 	__u32	i_dtime;
99512e9b892SDmitry Monakhov 	ext4_fsblk_t	i_file_acl;
996d444c3c3STheodore Ts'o 
997d444c3c3STheodore Ts'o 	/*
998d444c3c3STheodore Ts'o 	 * i_block_group is the number of the block group which contains
999d444c3c3STheodore Ts'o 	 * this file's inode.  Constant across the lifetime of the inode,
1000a627b0a7SEric Whitney 	 * it is used for making block allocation decisions - we try to
1001d444c3c3STheodore Ts'o 	 * place a file's data blocks near its inode block, and new inodes
1002d444c3c3STheodore Ts'o 	 * near to their parent directory's inode.
1003d444c3c3STheodore Ts'o 	 */
1004d444c3c3STheodore Ts'o 	ext4_group_t	i_block_group;
10058a2005d3STheodore Ts'o 	ext4_lblk_t	i_dir_start_lookup;
1006353eb83cSTheodore Ts'o #if (BITS_PER_LONG < 64)
100719f5fb7aSTheodore Ts'o 	unsigned long	i_state_flags;		/* Dynamic state flags */
1008353eb83cSTheodore Ts'o #endif
100912e9b892SDmitry Monakhov 	unsigned long	i_flags;
1010d444c3c3STheodore Ts'o 
1011d444c3c3STheodore Ts'o 	/*
1012d444c3c3STheodore Ts'o 	 * Extended attributes can be read independently of the main file
1013f340b3d9Shongnanli 	 * data. Taking i_rwsem even when reading would cause contention
1014d444c3c3STheodore Ts'o 	 * between readers of EAs and writers of regular file data, so
1015d444c3c3STheodore Ts'o 	 * instead we synchronize on xattr_sem when reading or changing
1016d444c3c3STheodore Ts'o 	 * EAs.
1017d444c3c3STheodore Ts'o 	 */
1018d444c3c3STheodore Ts'o 	struct rw_semaphore xattr_sem;
1019d444c3c3STheodore Ts'o 
102002f310fcSJan Kara 	/*
102102f310fcSJan Kara 	 * Inodes with EXT4_STATE_ORPHAN_FILE use i_orphan_idx. Otherwise
102202f310fcSJan Kara 	 * i_orphan is used.
102302f310fcSJan Kara 	 */
102402f310fcSJan Kara 	union {
1025d444c3c3STheodore Ts'o 		struct list_head i_orphan;	/* unlinked but open inodes */
102602f310fcSJan Kara 		unsigned int i_orphan_idx;	/* Index in orphan file */
102702f310fcSJan Kara 	};
1028d444c3c3STheodore Ts'o 
1029aa75f4d3SHarshad Shirwadkar 	/* Fast commit related info */
1030aa75f4d3SHarshad Shirwadkar 
1031b3998b3bSRitesh Harjani 	/* For tracking dentry create updates */
1032b3998b3bSRitesh Harjani 	struct list_head i_fc_dilist;
1033aa75f4d3SHarshad Shirwadkar 	struct list_head i_fc_list;	/*
1034aa75f4d3SHarshad Shirwadkar 					 * inodes that need fast commit
1035aa75f4d3SHarshad Shirwadkar 					 * protected by sbi->s_fc_lock.
1036aa75f4d3SHarshad Shirwadkar 					 */
1037aa75f4d3SHarshad Shirwadkar 
1038aa75f4d3SHarshad Shirwadkar 	/* Start of lblk range that needs to be committed in this fast commit */
1039aa75f4d3SHarshad Shirwadkar 	ext4_lblk_t i_fc_lblk_start;
1040aa75f4d3SHarshad Shirwadkar 
1041aa75f4d3SHarshad Shirwadkar 	/* End of lblk range that needs to be committed in this fast commit */
1042aa75f4d3SHarshad Shirwadkar 	ext4_lblk_t i_fc_lblk_len;
1043aa75f4d3SHarshad Shirwadkar 
1044aa75f4d3SHarshad Shirwadkar 	/* Number of ongoing updates on this inode */
1045aa75f4d3SHarshad Shirwadkar 	atomic_t  i_fc_updates;
1046aa75f4d3SHarshad Shirwadkar 
1047aa75f4d3SHarshad Shirwadkar 	/* Fast commit wait queue for this inode */
1048aa75f4d3SHarshad Shirwadkar 	wait_queue_head_t i_fc_wait;
1049aa75f4d3SHarshad Shirwadkar 
1050aa75f4d3SHarshad Shirwadkar 	/* Protect concurrent accesses on i_fc_lblk_start, i_fc_lblk_len */
1051aa75f4d3SHarshad Shirwadkar 	struct mutex i_fc_lock;
1052aa75f4d3SHarshad Shirwadkar 
1053d444c3c3STheodore Ts'o 	/*
1054d444c3c3STheodore Ts'o 	 * i_disksize keeps track of what the inode size is ON DISK, not
1055d444c3c3STheodore Ts'o 	 * in memory.  During truncate, i_size is set to the new size by
1056d444c3c3STheodore Ts'o 	 * the VFS prior to calling ext4_truncate(), but the filesystem won't
1057d444c3c3STheodore Ts'o 	 * set i_disksize to 0 until the truncate is actually under way.
1058d444c3c3STheodore Ts'o 	 *
1059d444c3c3STheodore Ts'o 	 * The intent is that i_disksize always represents the blocks which
1060d444c3c3STheodore Ts'o 	 * are used by this file.  This allows recovery to restart truncate
1061d444c3c3STheodore Ts'o 	 * on orphans if we crash during truncate.  We actually write i_disksize
1062d444c3c3STheodore Ts'o 	 * into the on-disk inode when writing inodes out, instead of i_size.
1063d444c3c3STheodore Ts'o 	 *
1064d444c3c3STheodore Ts'o 	 * The only time when i_disksize and i_size may be different is when
1065d444c3c3STheodore Ts'o 	 * a truncate is in progress.  The only things which change i_disksize
1066d444c3c3STheodore Ts'o 	 * are ext4_get_block (growth) and ext4_truncate (shrinkth).
1067d444c3c3STheodore Ts'o 	 */
1068d444c3c3STheodore Ts'o 	loff_t	i_disksize;
1069d444c3c3STheodore Ts'o 
1070d444c3c3STheodore Ts'o 	/*
1071d444c3c3STheodore Ts'o 	 * i_data_sem is for serialising ext4_truncate() against
1072d444c3c3STheodore Ts'o 	 * ext4_getblock().  In the 2.4 ext2 design, great chunks of inode's
1073d444c3c3STheodore Ts'o 	 * data tree are chopped off during truncate. We can't do that in
1074d444c3c3STheodore Ts'o 	 * ext4 because whenever we perform intermediate commits during
1075d444c3c3STheodore Ts'o 	 * truncate, the inode and all the metadata blocks *must* be in a
1076d444c3c3STheodore Ts'o 	 * consistent state which allows truncation of the orphans to restart
1077d444c3c3STheodore Ts'o 	 * during recovery.  Hence we must fix the get_block-vs-truncate race
1078d444c3c3STheodore Ts'o 	 * by other means, so we have i_data_sem.
1079d444c3c3STheodore Ts'o 	 */
1080d444c3c3STheodore Ts'o 	struct rw_semaphore i_data_sem;
1081d444c3c3STheodore Ts'o 	struct inode vfs_inode;
10828aefcd55STheodore Ts'o 	struct jbd2_inode *jinode;
1083d444c3c3STheodore Ts'o 
1084202ee5dfSTheodore Ts'o 	spinlock_t i_raw_lock;	/* protects updates to the raw inode */
1085202ee5dfSTheodore Ts'o 
1086d444c3c3STheodore Ts'o 	/*
1087d444c3c3STheodore Ts'o 	 * File creation time. Its function is same as that of
10887b62b293SArnd Bergmann 	 * struct timespec64 i_{a,c,m}time in the generic inode.
1089d444c3c3STheodore Ts'o 	 */
10907b62b293SArnd Bergmann 	struct timespec64 i_crtime;
1091d444c3c3STheodore Ts'o 
1092d444c3c3STheodore Ts'o 	/* mballoc */
109327bc446eSbrookxu 	atomic_t i_prealloc_active;
109438727786SOjaswin Mujoo 	struct rb_root i_prealloc_node;
109538727786SOjaswin Mujoo 	rwlock_t i_prealloc_lock;
1096d444c3c3STheodore Ts'o 
1097c0677e6dSZheng Liu 	/* extents status tree */
1098c0677e6dSZheng Liu 	struct ext4_es_tree i_es_tree;
1099c0677e6dSZheng Liu 	rwlock_t i_es_lock;
1100edaa53caSZheng Liu 	struct list_head i_es_list;
1101eb68d0e2SZheng Liu 	unsigned int i_es_all_nr;	/* protected by i_es_lock */
1102edaa53caSZheng Liu 	unsigned int i_es_shk_nr;	/* protected by i_es_lock */
1103dd475925SJan Kara 	ext4_lblk_t i_es_shrink_lblk;	/* Offset where we start searching for
1104dd475925SJan Kara 					   extents to shrink. Protected by
1105dd475925SJan Kara 					   i_es_lock  */
1106c0677e6dSZheng Liu 
1107d444c3c3STheodore Ts'o 	/* ialloc */
1108d444c3c3STheodore Ts'o 	ext4_group_t	i_last_alloc_group;
1109d444c3c3STheodore Ts'o 
1110d444c3c3STheodore Ts'o 	/* allocation reservation info for delalloc */
1111a627b0a7SEric Whitney 	/* In case of bigalloc, this refer to clusters rather than blocks */
1112d444c3c3STheodore Ts'o 	unsigned int i_reserved_data_blocks;
1113d444c3c3STheodore Ts'o 
11141dc0aa46SEric Whitney 	/* pending cluster reservations for bigalloc file systems */
11151dc0aa46SEric Whitney 	struct ext4_pending_tree i_pending_tree;
11161dc0aa46SEric Whitney 
1117d444c3c3STheodore Ts'o 	/* on-disk additional length */
1118d444c3c3STheodore Ts'o 	__u16 i_extra_isize;
1119d444c3c3STheodore Ts'o 
112067cf5b09STao Ma 	/* Indicate the inline data space. */
112167cf5b09STao Ma 	u16 i_inline_off;
112267cf5b09STao Ma 	u16 i_inline_size;
112367cf5b09STao Ma 
1124a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA
1125a9e7f447SDmitry Monakhov 	/* quota space reservation, managed internally by quota code */
1126a9e7f447SDmitry Monakhov 	qsize_t i_reserved_quota;
1127a9e7f447SDmitry Monakhov #endif
11288d5d02e6SMingming Cao 
11292e8fa54eSJan Kara 	/* Lock protecting lists below */
1130744692dcSJiaying Zhang 	spinlock_t i_completed_io_lock;
11312e8fa54eSJan Kara 	/*
11322e8fa54eSJan Kara 	 * Completed IOs that need unwritten extents handling and have
11332e8fa54eSJan Kara 	 * transaction reserved
11342e8fa54eSJan Kara 	 */
11352e8fa54eSJan Kara 	struct list_head i_rsv_conversion_list;
11362e8fa54eSJan Kara 	struct work_struct i_rsv_conversion_work;
1137600be30aSJan Kara 	atomic_t i_unwritten; /* Nr. of inflight conversions pending */
11388a2005d3STheodore Ts'o 
11398a2005d3STheodore Ts'o 	spinlock_t i_block_reservation_lock;
1140b436b9beSJan Kara 
1141b436b9beSJan Kara 	/*
1142b436b9beSJan Kara 	 * Transactions that contain inode's metadata needed to complete
1143b436b9beSJan Kara 	 * fsync and fdatasync, respectively.
1144b436b9beSJan Kara 	 */
1145b436b9beSJan Kara 	tid_t i_sync_tid;
1146b436b9beSJan Kara 	tid_t i_datasync_tid;
1147814525f4SDarrick J. Wong 
114896c7e0d9SJan Kara #ifdef CONFIG_QUOTA
1149*42954c37SJan Kara 	struct dquot __rcu *i_dquot[MAXQUOTAS];
115096c7e0d9SJan Kara #endif
115196c7e0d9SJan Kara 
1152814525f4SDarrick J. Wong 	/* Precomputed uuid+inum+igen checksum for seeding inode checksums */
1153814525f4SDarrick J. Wong 	__u32 i_csum_seed;
1154b30ab0e0SMichael Halcrow 
1155040cb378SLi Xi 	kprojid_t i_projid;
1156d444c3c3STheodore Ts'o };
1157d444c3c3STheodore Ts'o 
1158d444c3c3STheodore Ts'o /*
11593dcf5451SChristoph Hellwig  * File system states
11603dcf5451SChristoph Hellwig  */
11613dcf5451SChristoph Hellwig #define	EXT4_VALID_FS			0x0001	/* Unmounted cleanly */
11623dcf5451SChristoph Hellwig #define	EXT4_ERROR_FS			0x0002	/* Errors detected */
11633dcf5451SChristoph Hellwig #define	EXT4_ORPHAN_FS			0x0004	/* Orphans being recovered */
11648016e29fSHarshad Shirwadkar #define EXT4_FC_REPLAY			0x0020	/* Fast commit replay ongoing */
11653dcf5451SChristoph Hellwig 
11663dcf5451SChristoph Hellwig /*
11673dcf5451SChristoph Hellwig  * Misc. filesystem flags
11683dcf5451SChristoph Hellwig  */
11693dcf5451SChristoph Hellwig #define EXT2_FLAGS_SIGNED_HASH		0x0001  /* Signed dirhash in use */
11703dcf5451SChristoph Hellwig #define EXT2_FLAGS_UNSIGNED_HASH	0x0002  /* Unsigned dirhash in use */
11713dcf5451SChristoph Hellwig #define EXT2_FLAGS_TEST_FILESYS		0x0004	/* to test development code */
11723dcf5451SChristoph Hellwig 
11733dcf5451SChristoph Hellwig /*
117468911009SLukas Czerner  * Mount flags set via mount options or defaults
11753dcf5451SChristoph Hellwig  */
1176cdb7ee4cSTahsin Erdogan #define EXT4_MOUNT_NO_MBCACHE		0x00001 /* Do not use mbcache */
11773dcf5451SChristoph Hellwig #define EXT4_MOUNT_GRPID		0x00004	/* Create files with directory's group */
11783dcf5451SChristoph Hellwig #define EXT4_MOUNT_DEBUG		0x00008	/* Some debugging messages */
11793dcf5451SChristoph Hellwig #define EXT4_MOUNT_ERRORS_CONT		0x00010	/* Continue on errors */
11803dcf5451SChristoph Hellwig #define EXT4_MOUNT_ERRORS_RO		0x00020	/* Remount fs ro on errors */
11813dcf5451SChristoph Hellwig #define EXT4_MOUNT_ERRORS_PANIC		0x00040	/* Panic on errors */
118239ef17f1STheodore Ts'o #define EXT4_MOUNT_ERRORS_MASK		0x00070
11833dcf5451SChristoph Hellwig #define EXT4_MOUNT_MINIX_DF		0x00080	/* Mimics the Minix statfs */
11843dcf5451SChristoph Hellwig #define EXT4_MOUNT_NOLOAD		0x00100	/* Don't use existing journal*/
1185923ae0ffSRoss Zwisler #ifdef CONFIG_FS_DAX
1186fc626fe3SIra Weiny #define EXT4_MOUNT_DAX_ALWAYS		0x00200	/* Direct Access */
1187923ae0ffSRoss Zwisler #else
1188fc626fe3SIra Weiny #define EXT4_MOUNT_DAX_ALWAYS		0
1189923ae0ffSRoss Zwisler #endif
11903dcf5451SChristoph Hellwig #define EXT4_MOUNT_DATA_FLAGS		0x00C00	/* Mode for data writes: */
11913dcf5451SChristoph Hellwig #define EXT4_MOUNT_JOURNAL_DATA		0x00400	/* Write data to journal */
11923dcf5451SChristoph Hellwig #define EXT4_MOUNT_ORDERED_DATA		0x00800	/* Flush data before commit */
11933dcf5451SChristoph Hellwig #define EXT4_MOUNT_WRITEBACK_DATA	0x00C00	/* No data ordering */
11943dcf5451SChristoph Hellwig #define EXT4_MOUNT_UPDATE_JOURNAL	0x01000	/* Update the journal format */
11953dcf5451SChristoph Hellwig #define EXT4_MOUNT_NO_UID32		0x02000  /* Disable 32-bit UIDs */
11963dcf5451SChristoph Hellwig #define EXT4_MOUNT_XATTR_USER		0x04000	/* Extended user attributes */
11973dcf5451SChristoph Hellwig #define EXT4_MOUNT_POSIX_ACL		0x08000	/* POSIX Access Control Lists */
1198afd4672dSTheodore Ts'o #define EXT4_MOUNT_NO_AUTO_DA_ALLOC	0x10000	/* No auto delalloc mapping */
11993dcf5451SChristoph Hellwig #define EXT4_MOUNT_BARRIER		0x20000 /* Use block barriers */
120049da9392SJan Kara #define EXT4_MOUNT_QUOTA		0x40000 /* Some quota option set */
120149da9392SJan Kara #define EXT4_MOUNT_USRQUOTA		0x80000 /* "old" user quota,
120249da9392SJan Kara 						 * enable enforcement for hidden
120349da9392SJan Kara 						 * quota files */
120449da9392SJan Kara #define EXT4_MOUNT_GRPQUOTA		0x100000 /* "old" group quota, enable
120549da9392SJan Kara 						  * enforcement for hidden quota
120649da9392SJan Kara 						  * files */
120749da9392SJan Kara #define EXT4_MOUNT_PRJQUOTA		0x200000 /* Enable project quota
120849da9392SJan Kara 						  * enforcement */
1209744692dcSJiaying Zhang #define EXT4_MOUNT_DIOREAD_NOLOCK	0x400000 /* Enable support for dio read nolocking */
1210d4da6c9cSLinus Torvalds #define EXT4_MOUNT_JOURNAL_CHECKSUM	0x800000 /* Journal checksums */
12113dcf5451SChristoph Hellwig #define EXT4_MOUNT_JOURNAL_ASYNC_COMMIT	0x1000000 /* Journal Async Commit */
1212327eaf73STheodore Ts'o #define EXT4_MOUNT_WARN_ON_ERROR	0x2000000 /* Trigger WARN_ON on error */
121321175ca4SHarshad Shirwadkar #define EXT4_MOUNT_NO_PREFETCH_BLOCK_BITMAPS 0x4000000
121464769240SAlex Tomas #define EXT4_MOUNT_DELALLOC		0x8000000 /* Delalloc support */
12155bf5683aSHidehiro Kawai #define EXT4_MOUNT_DATA_ERR_ABORT	0x10000000 /* Abort on file data write */
12166fd058f7STheodore Ts'o #define EXT4_MOUNT_BLOCK_VALIDITY	0x20000000 /* Block validity checking */
12175328e635SEric Sandeen #define EXT4_MOUNT_DISCARD		0x40000000 /* Issue DISCARD requests */
1218bfff6873SLukas Czerner #define EXT4_MOUNT_INIT_INODE_TABLE	0x80000000 /* Initialize uninitialized itables */
12195bf5683aSHidehiro Kawai 
122068911009SLukas Czerner /*
122168911009SLukas Czerner  * Mount flags set either automatically (could not be set by mount option)
122268911009SLukas Czerner  * based on per file system feature or property or in special cases such as
122368911009SLukas Czerner  * distinguishing between explicit mount option definition and default.
122468911009SLukas Czerner  */
122556889787STheodore Ts'o #define EXT4_MOUNT2_EXPLICIT_DELALLOC	0x00000001 /* User explicitly
122656889787STheodore Ts'o 						      specified delalloc */
122768911009SLukas Czerner #define EXT4_MOUNT2_STD_GROUP_SIZE	0x00000002 /* We have standard group
122868911009SLukas Czerner 						      size of blocksize * 8
122968911009SLukas Czerner 						      blocks */
1230ed3654ebSTheodore Ts'o #define EXT4_MOUNT2_HURD_COMPAT		0x00000004 /* Support HURD-castrated
1231ed3654ebSTheodore Ts'o 						      file systems */
12321e381f60SDmitry Monakhov #define EXT4_MOUNT2_EXPLICIT_JOURNAL_CHECKSUM	0x00000008 /* User explicitly
12331e381f60SDmitry Monakhov 						specified journal checksum */
12341e381f60SDmitry Monakhov 
1235995a3ed6SHarshad Shirwadkar #define EXT4_MOUNT2_JOURNAL_FAST_COMMIT	0x00000010 /* Journal fast commit */
1236a72b38eeSHarshad Shirwadkar #define EXT4_MOUNT2_DAX_NEVER		0x00000020 /* Do not allow Direct Access */
1237a72b38eeSHarshad Shirwadkar #define EXT4_MOUNT2_DAX_INODE		0x00000040 /* For printing options only */
1238196e402aSHarshad Shirwadkar #define EXT4_MOUNT2_MB_OPTIMIZE_SCAN	0x00000080 /* Optimize group
1239196e402aSHarshad Shirwadkar 						    * scanning in mballoc
1240196e402aSHarshad Shirwadkar 						    */
124122b8d707SJan Kara #define EXT4_MOUNT2_ABORT		0x00000100 /* Abort filesystem */
1242995a3ed6SHarshad Shirwadkar 
1243fd8c37ecSTheodore Ts'o #define clear_opt(sb, opt)		EXT4_SB(sb)->s_mount_opt &= \
1244fd8c37ecSTheodore Ts'o 						~EXT4_MOUNT_##opt
1245fd8c37ecSTheodore Ts'o #define set_opt(sb, opt)		EXT4_SB(sb)->s_mount_opt |= \
1246fd8c37ecSTheodore Ts'o 						EXT4_MOUNT_##opt
12473dcf5451SChristoph Hellwig #define test_opt(sb, opt)		(EXT4_SB(sb)->s_mount_opt & \
12483dcf5451SChristoph Hellwig 					 EXT4_MOUNT_##opt)
12493dcf5451SChristoph Hellwig 
1250a2595b8aSTheodore Ts'o #define clear_opt2(sb, opt)		EXT4_SB(sb)->s_mount_opt2 &= \
1251a2595b8aSTheodore Ts'o 						~EXT4_MOUNT2_##opt
1252a2595b8aSTheodore Ts'o #define set_opt2(sb, opt)		EXT4_SB(sb)->s_mount_opt2 |= \
1253a2595b8aSTheodore Ts'o 						EXT4_MOUNT2_##opt
1254a2595b8aSTheodore Ts'o #define test_opt2(sb, opt)		(EXT4_SB(sb)->s_mount_opt2 & \
1255a2595b8aSTheodore Ts'o 					 EXT4_MOUNT2_##opt)
1256a2595b8aSTheodore Ts'o 
1257597d508cSAkinobu Mita #define ext4_test_and_set_bit		__test_and_set_bit_le
1258597d508cSAkinobu Mita #define ext4_set_bit			__set_bit_le
1259597d508cSAkinobu Mita #define ext4_test_and_clear_bit		__test_and_clear_bit_le
1260597d508cSAkinobu Mita #define ext4_clear_bit			__clear_bit_le
126150e0168cSAkinobu Mita #define ext4_test_bit			test_bit_le
126250e0168cSAkinobu Mita #define ext4_find_next_zero_bit		find_next_zero_bit_le
126350e0168cSAkinobu Mita #define ext4_find_next_bit		find_next_bit_le
12643dcf5451SChristoph Hellwig 
1265123e3016SRitesh Harjani extern void mb_set_bits(void *bm, int cur, int len);
1266c3e94d1dSYongqiang Yang 
12673dcf5451SChristoph Hellwig /*
12683dcf5451SChristoph Hellwig  * Maximal mount counts between two filesystem checks
12693dcf5451SChristoph Hellwig  */
12703dcf5451SChristoph Hellwig #define EXT4_DFL_MAX_MNT_COUNT		20	/* Allow 20 mounts */
12713dcf5451SChristoph Hellwig #define EXT4_DFL_CHECKINTERVAL		0	/* Don't use interval check */
12723dcf5451SChristoph Hellwig 
12733dcf5451SChristoph Hellwig /*
12743dcf5451SChristoph Hellwig  * Behaviour when detecting errors
12753dcf5451SChristoph Hellwig  */
12763dcf5451SChristoph Hellwig #define EXT4_ERRORS_CONTINUE		1	/* Continue execution */
12773dcf5451SChristoph Hellwig #define EXT4_ERRORS_RO			2	/* Remount fs read-only */
12783dcf5451SChristoph Hellwig #define EXT4_ERRORS_PANIC		3	/* Panic */
12793dcf5451SChristoph Hellwig #define EXT4_ERRORS_DEFAULT		EXT4_ERRORS_CONTINUE
12803dcf5451SChristoph Hellwig 
1281e6153918SDarrick J. Wong /* Metadata checksum algorithm codes */
1282e6153918SDarrick J. Wong #define EXT4_CRC32C_CHKSUM		1
1283e6153918SDarrick J. Wong 
1284bbc605cdSLukas Czerner #define EXT4_LABEL_MAX			16
1285bbc605cdSLukas Czerner 
12863dcf5451SChristoph Hellwig /*
12873dcf5451SChristoph Hellwig  * Structure of the super block
12883dcf5451SChristoph Hellwig  */
12893dcf5451SChristoph Hellwig struct ext4_super_block {
12903dcf5451SChristoph Hellwig /*00*/	__le32	s_inodes_count;		/* Inodes count */
12913dcf5451SChristoph Hellwig 	__le32	s_blocks_count_lo;	/* Blocks count */
12923dcf5451SChristoph Hellwig 	__le32	s_r_blocks_count_lo;	/* Reserved blocks count */
12933dcf5451SChristoph Hellwig 	__le32	s_free_blocks_count_lo;	/* Free blocks count */
12943dcf5451SChristoph Hellwig /*10*/	__le32	s_free_inodes_count;	/* Free inodes count */
12953dcf5451SChristoph Hellwig 	__le32	s_first_data_block;	/* First Data Block */
12963dcf5451SChristoph Hellwig 	__le32	s_log_block_size;	/* Block size */
1297281b5995STheodore Ts'o 	__le32	s_log_cluster_size;	/* Allocation cluster size */
12983dcf5451SChristoph Hellwig /*20*/	__le32	s_blocks_per_group;	/* # Blocks per group */
1299281b5995STheodore Ts'o 	__le32	s_clusters_per_group;	/* # Clusters per group */
13003dcf5451SChristoph Hellwig 	__le32	s_inodes_per_group;	/* # Inodes per group */
13013dcf5451SChristoph Hellwig 	__le32	s_mtime;		/* Mount time */
13023dcf5451SChristoph Hellwig /*30*/	__le32	s_wtime;		/* Write time */
13033dcf5451SChristoph Hellwig 	__le16	s_mnt_count;		/* Mount count */
13043dcf5451SChristoph Hellwig 	__le16	s_max_mnt_count;	/* Maximal mount count */
13053dcf5451SChristoph Hellwig 	__le16	s_magic;		/* Magic signature */
13063dcf5451SChristoph Hellwig 	__le16	s_state;		/* File system state */
13073dcf5451SChristoph Hellwig 	__le16	s_errors;		/* Behaviour when detecting errors */
13083dcf5451SChristoph Hellwig 	__le16	s_minor_rev_level;	/* minor revision level */
13093dcf5451SChristoph Hellwig /*40*/	__le32	s_lastcheck;		/* time of last check */
13103dcf5451SChristoph Hellwig 	__le32	s_checkinterval;	/* max. time between checks */
13113dcf5451SChristoph Hellwig 	__le32	s_creator_os;		/* OS */
13123dcf5451SChristoph Hellwig 	__le32	s_rev_level;		/* Revision level */
13133dcf5451SChristoph Hellwig /*50*/	__le16	s_def_resuid;		/* Default uid for reserved blocks */
13143dcf5451SChristoph Hellwig 	__le16	s_def_resgid;		/* Default gid for reserved blocks */
13153dcf5451SChristoph Hellwig 	/*
13163dcf5451SChristoph Hellwig 	 * These fields are for EXT4_DYNAMIC_REV superblocks only.
13173dcf5451SChristoph Hellwig 	 *
13183dcf5451SChristoph Hellwig 	 * Note: the difference between the compatible feature set and
13193dcf5451SChristoph Hellwig 	 * the incompatible feature set is that if there is a bit set
13203dcf5451SChristoph Hellwig 	 * in the incompatible feature set that the kernel doesn't
13213dcf5451SChristoph Hellwig 	 * know about, it should refuse to mount the filesystem.
13223dcf5451SChristoph Hellwig 	 *
13233dcf5451SChristoph Hellwig 	 * e2fsck's requirements are more strict; if it doesn't know
13243dcf5451SChristoph Hellwig 	 * about a feature in either the compatible or incompatible
13253dcf5451SChristoph Hellwig 	 * feature set, it must abort and not try to meddle with
13263dcf5451SChristoph Hellwig 	 * things it doesn't understand...
13273dcf5451SChristoph Hellwig 	 */
13283dcf5451SChristoph Hellwig 	__le32	s_first_ino;		/* First non-reserved inode */
13293dcf5451SChristoph Hellwig 	__le16  s_inode_size;		/* size of inode structure */
13303dcf5451SChristoph Hellwig 	__le16	s_block_group_nr;	/* block group # of this superblock */
13313dcf5451SChristoph Hellwig 	__le32	s_feature_compat;	/* compatible feature set */
13323dcf5451SChristoph Hellwig /*60*/	__le32	s_feature_incompat;	/* incompatible feature set */
13333dcf5451SChristoph Hellwig 	__le32	s_feature_ro_compat;	/* readonly-compatible feature set */
13343dcf5451SChristoph Hellwig /*68*/	__u8	s_uuid[16];		/* 128-bit uuid for volume */
1335bbc605cdSLukas Czerner /*78*/	char	s_volume_name[EXT4_LABEL_MAX];	/* volume name */
1336072ebb3bSTheodore Ts'o /*88*/	char	s_last_mounted[64] __nonstring;	/* directory where last mounted */
13373dcf5451SChristoph Hellwig /*C8*/	__le32	s_algorithm_usage_bitmap; /* For compression */
13383dcf5451SChristoph Hellwig 	/*
13393dcf5451SChristoph Hellwig 	 * Performance hints.  Directory preallocation should only
13403dcf5451SChristoph Hellwig 	 * happen if the EXT4_FEATURE_COMPAT_DIR_PREALLOC flag is on.
13413dcf5451SChristoph Hellwig 	 */
13423dcf5451SChristoph Hellwig 	__u8	s_prealloc_blocks;	/* Nr of blocks to try to preallocate*/
13433dcf5451SChristoph Hellwig 	__u8	s_prealloc_dir_blocks;	/* Nr to preallocate for dirs */
13443dcf5451SChristoph Hellwig 	__le16	s_reserved_gdt_blocks;	/* Per group desc for online growth */
13453dcf5451SChristoph Hellwig 	/*
13463dcf5451SChristoph Hellwig 	 * Journaling support valid if EXT4_FEATURE_COMPAT_HAS_JOURNAL set.
13473dcf5451SChristoph Hellwig 	 */
13483dcf5451SChristoph Hellwig /*D0*/	__u8	s_journal_uuid[16];	/* uuid of journal superblock */
13493dcf5451SChristoph Hellwig /*E0*/	__le32	s_journal_inum;		/* inode number of journal file */
13503dcf5451SChristoph Hellwig 	__le32	s_journal_dev;		/* device number of journal file */
13513dcf5451SChristoph Hellwig 	__le32	s_last_orphan;		/* start of list of inodes to delete */
13523dcf5451SChristoph Hellwig 	__le32	s_hash_seed[4];		/* HTREE hash seed */
13533dcf5451SChristoph Hellwig 	__u8	s_def_hash_version;	/* Default hash version to use */
135489eeddf0STheodore Ts'o 	__u8	s_jnl_backup_type;
13553dcf5451SChristoph Hellwig 	__le16  s_desc_size;		/* size of group descriptor */
13563dcf5451SChristoph Hellwig /*100*/	__le32	s_default_mount_opts;
13573dcf5451SChristoph Hellwig 	__le32	s_first_meta_bg;	/* First metablock block group */
13583dcf5451SChristoph Hellwig 	__le32	s_mkfs_time;		/* When the filesystem was created */
13593dcf5451SChristoph Hellwig 	__le32	s_jnl_blocks[17];	/* Backup of the journal inode */
13607fc1f5c2STudor Ambarus 	/* 64bit support valid if EXT4_FEATURE_INCOMPAT_64BIT */
13613dcf5451SChristoph Hellwig /*150*/	__le32	s_blocks_count_hi;	/* Blocks count */
13623dcf5451SChristoph Hellwig 	__le32	s_r_blocks_count_hi;	/* Reserved blocks count */
13633dcf5451SChristoph Hellwig 	__le32	s_free_blocks_count_hi;	/* Free blocks count */
13643dcf5451SChristoph Hellwig 	__le16	s_min_extra_isize;	/* All inodes have at least # bytes */
13653dcf5451SChristoph Hellwig 	__le16	s_want_extra_isize; 	/* New inodes should reserve # bytes */
13663dcf5451SChristoph Hellwig 	__le32	s_flags;		/* Miscellaneous flags */
13673dcf5451SChristoph Hellwig 	__le16  s_raid_stride;		/* RAID stride */
1368c5e06d10SJohann Lombardi 	__le16  s_mmp_update_interval;  /* # seconds to wait in MMP checking */
13693dcf5451SChristoph Hellwig 	__le64  s_mmp_block;            /* Block for multi-mount protection */
13703dcf5451SChristoph Hellwig 	__le32  s_raid_stripe_width;    /* blocks on all data disks (N*stride)*/
1371772cb7c8SJose R. Santos 	__u8	s_log_groups_per_flex;  /* FLEX_BG group size */
1372e6153918SDarrick J. Wong 	__u8	s_checksum_type;	/* metadata checksum algorithm used */
1373f542fbe8STheodore Ts'o 	__u8	s_encryption_level;	/* versioning level for encryption */
1374f542fbe8STheodore Ts'o 	__u8	s_reserved_pad;		/* Padding to next 32bits */
1375afc32f7eSTheodore Ts'o 	__le64	s_kbytes_written;	/* nr of lifetime kilobytes written */
13764a9cdec7STheodore Ts'o 	__le32	s_snapshot_inum;	/* Inode number of active snapshot */
13774a9cdec7STheodore Ts'o 	__le32	s_snapshot_id;		/* sequential ID of active snapshot */
13784a9cdec7STheodore Ts'o 	__le64	s_snapshot_r_blocks_count; /* reserved blocks for active
13794a9cdec7STheodore Ts'o 					      snapshot's future use */
13804a9cdec7STheodore Ts'o 	__le32	s_snapshot_list;	/* inode number of the head of the
13814a9cdec7STheodore Ts'o 					   on-disk snapshot list */
13821c13d5c0STheodore Ts'o #define EXT4_S_ERR_START offsetof(struct ext4_super_block, s_error_count)
13831c13d5c0STheodore Ts'o 	__le32	s_error_count;		/* number of fs errors */
13841c13d5c0STheodore Ts'o 	__le32	s_first_error_time;	/* first time an error happened */
13851c13d5c0STheodore Ts'o 	__le32	s_first_error_ino;	/* inode involved in first error */
13861c13d5c0STheodore Ts'o 	__le64	s_first_error_block;	/* block involved of first error */
1387072ebb3bSTheodore Ts'o 	__u8	s_first_error_func[32] __nonstring;	/* function where the error happened */
13881c13d5c0STheodore Ts'o 	__le32	s_first_error_line;	/* line number where error happened */
13891c13d5c0STheodore Ts'o 	__le32	s_last_error_time;	/* most recent time of an error */
13901c13d5c0STheodore Ts'o 	__le32	s_last_error_ino;	/* inode involved in last error */
13911c13d5c0STheodore Ts'o 	__le32	s_last_error_line;	/* line number where error happened */
13921c13d5c0STheodore Ts'o 	__le64	s_last_error_block;	/* block involved of last error */
1393072ebb3bSTheodore Ts'o 	__u8	s_last_error_func[32] __nonstring;	/* function where the error happened */
13948b67f04aSTheodore Ts'o #define EXT4_S_ERR_END offsetof(struct ext4_super_block, s_mount_opts)
13958b67f04aSTheodore Ts'o 	__u8	s_mount_opts[64];
1396281b5995STheodore Ts'o 	__le32	s_usr_quota_inum;	/* inode for tracking user quota */
1397281b5995STheodore Ts'o 	__le32	s_grp_quota_inum;	/* inode for tracking group quota */
1398281b5995STheodore Ts'o 	__le32	s_overhead_clusters;	/* overhead blocks/clusters in fs */
13991beeef1bSDarrick J. Wong 	__le32	s_backup_bgs[2];	/* groups with sparse_super2 SBs */
14003edc18d8STheodore Ts'o 	__u8	s_encrypt_algos[4];	/* Encryption algorithms in use  */
1401f542fbe8STheodore Ts'o 	__u8	s_encrypt_pw_salt[16];	/* Salt used for string2key algorithm */
1402f542fbe8STheodore Ts'o 	__le32	s_lpf_ino;		/* Location of the lost+found inode */
14038b4953e1STheodore Ts'o 	__le32	s_prj_quota_inum;	/* inode for tracking project quota */
14048c81bd8fSDarrick J. Wong 	__le32	s_checksum_seed;	/* crc32c(uuid) if csum_seed set */
14056a0678a7SArnd Bergmann 	__u8	s_wtime_hi;
14066a0678a7SArnd Bergmann 	__u8	s_mtime_hi;
14076a0678a7SArnd Bergmann 	__u8	s_mkfs_time_hi;
14086a0678a7SArnd Bergmann 	__u8	s_lastcheck_hi;
14096a0678a7SArnd Bergmann 	__u8	s_first_error_time_hi;
14106a0678a7SArnd Bergmann 	__u8	s_last_error_time_hi;
1411878520acSTheodore Ts'o 	__u8	s_first_error_errcode;
1412878520acSTheodore Ts'o 	__u8    s_last_error_errcode;
1413c83ad55eSGabriel Krisman Bertazi 	__le16  s_encoding;		/* Filename charset encoding */
1414c83ad55eSGabriel Krisman Bertazi 	__le16  s_encoding_flags;	/* Filename charset encoding flags */
141502f310fcSJan Kara 	__le32  s_orphan_file_inum;	/* Inode for tracking orphan inodes */
141602f310fcSJan Kara 	__le32	s_reserved[94];		/* Padding to the end of the block */
1417e6153918SDarrick J. Wong 	__le32	s_checksum;		/* crc32c(superblock) */
14183dcf5451SChristoph Hellwig };
14193dcf5451SChristoph Hellwig 
14201c13d5c0STheodore Ts'o #define EXT4_S_ERR_LEN (EXT4_S_ERR_END - EXT4_S_ERR_START)
14211c13d5c0STheodore Ts'o 
14223dcf5451SChristoph Hellwig #ifdef __KERNEL__
1423bc0b0d6dSTheodore Ts'o 
1424a2d4a646SJan Kara /* Number of quota types we support */
1425689c958cSLi Xi #define EXT4_MAXQUOTAS 3
1426a2d4a646SJan Kara 
1427c83ad55eSGabriel Krisman Bertazi #define EXT4_ENC_UTF8_12_1	1
1428c83ad55eSGabriel Krisman Bertazi 
1429188c299eSJan Kara /* Types of ext4 journal triggers */
1430188c299eSJan Kara enum ext4_journal_trigger_type {
143102f310fcSJan Kara 	EXT4_JTR_ORPHAN_FILE,
1432188c299eSJan Kara 	EXT4_JTR_NONE	/* This must be the last entry for indexing to work! */
1433188c299eSJan Kara };
1434188c299eSJan Kara 
1435188c299eSJan Kara #define EXT4_JOURNAL_TRIGGER_COUNT EXT4_JTR_NONE
1436188c299eSJan Kara 
1437188c299eSJan Kara struct ext4_journal_trigger {
1438188c299eSJan Kara 	struct jbd2_buffer_trigger_type tr_triggers;
1439188c299eSJan Kara 	struct super_block *sb;
1440188c299eSJan Kara };
1441188c299eSJan Kara 
EXT4_TRIGGER(struct jbd2_buffer_trigger_type * trigger)1442188c299eSJan Kara static inline struct ext4_journal_trigger *EXT4_TRIGGER(
1443188c299eSJan Kara 				struct jbd2_buffer_trigger_type *trigger)
1444188c299eSJan Kara {
1445188c299eSJan Kara 	return container_of(trigger, struct ext4_journal_trigger, tr_triggers);
1446188c299eSJan Kara }
1447188c299eSJan Kara 
144802f310fcSJan Kara #define EXT4_ORPHAN_BLOCK_MAGIC 0x0b10ca04
144902f310fcSJan Kara 
145002f310fcSJan Kara /* Structure at the tail of orphan block */
145102f310fcSJan Kara struct ext4_orphan_block_tail {
145202f310fcSJan Kara 	__le32 ob_magic;
145302f310fcSJan Kara 	__le32 ob_checksum;
145402f310fcSJan Kara };
145502f310fcSJan Kara 
ext4_inodes_per_orphan_block(struct super_block * sb)145602f310fcSJan Kara static inline int ext4_inodes_per_orphan_block(struct super_block *sb)
145702f310fcSJan Kara {
145802f310fcSJan Kara 	return (sb->s_blocksize - sizeof(struct ext4_orphan_block_tail)) /
145902f310fcSJan Kara 			sizeof(u32);
146002f310fcSJan Kara }
146102f310fcSJan Kara 
146202f310fcSJan Kara struct ext4_orphan_block {
14634a79a98cSJan Kara 	atomic_t ob_free_entries;	/* Number of free orphan entries in block */
146402f310fcSJan Kara 	struct buffer_head *ob_bh;	/* Buffer for orphan block */
146502f310fcSJan Kara };
146602f310fcSJan Kara 
146702f310fcSJan Kara /*
146802f310fcSJan Kara  * Info about orphan file.
146902f310fcSJan Kara  */
147002f310fcSJan Kara struct ext4_orphan_info {
147102f310fcSJan Kara 	int of_blocks;			/* Number of orphan blocks in a file */
147202f310fcSJan Kara 	__u32 of_csum_seed;		/* Checksum seed for orphan file */
147302f310fcSJan Kara 	struct ext4_orphan_block *of_binfo;	/* Array with info about orphan
147402f310fcSJan Kara 						 * file blocks */
147502f310fcSJan Kara };
147602f310fcSJan Kara 
1477c83ad55eSGabriel Krisman Bertazi /*
1478ca0faba0STheodore Ts'o  * fourth extended-fs super-block data in memory
1479ca0faba0STheodore Ts'o  */
1480ca0faba0STheodore Ts'o struct ext4_sb_info {
1481ca0faba0STheodore Ts'o 	unsigned long s_desc_size;	/* Size of a group descriptor in bytes */
1482ca0faba0STheodore Ts'o 	unsigned long s_inodes_per_block;/* Number of inodes per block */
1483ca0faba0STheodore Ts'o 	unsigned long s_blocks_per_group;/* Number of blocks in a group */
1484281b5995STheodore Ts'o 	unsigned long s_clusters_per_group; /* Number of clusters in a group */
1485ca0faba0STheodore Ts'o 	unsigned long s_inodes_per_group;/* Number of inodes in a group */
1486ca0faba0STheodore Ts'o 	unsigned long s_itb_per_group;	/* Number of inode table blocks per group */
1487ca0faba0STheodore Ts'o 	unsigned long s_gdb_count;	/* Number of group descriptor blocks */
1488ca0faba0STheodore Ts'o 	unsigned long s_desc_per_block;	/* Number of group descriptors per block */
1489ca0faba0STheodore Ts'o 	ext4_group_t s_groups_count;	/* Number of groups in the fs */
1490fb0a387dSEric Sandeen 	ext4_group_t s_blockfile_groups;/* Groups acceptable for non-extent files */
1491952fc18eSTheodore Ts'o 	unsigned long s_overhead;  /* # of fs overhead clusters */
1492281b5995STheodore Ts'o 	unsigned int s_cluster_ratio;	/* Number of blocks per cluster */
1493281b5995STheodore Ts'o 	unsigned int s_cluster_bits;	/* log2 of s_cluster_ratio */
1494ca0faba0STheodore Ts'o 	loff_t s_bitmap_maxbytes;	/* max bytes for bitmap files */
1495ca0faba0STheodore Ts'o 	struct buffer_head * s_sbh;	/* Buffer containing the super block */
1496ca0faba0STheodore Ts'o 	struct ext4_super_block *s_es;	/* Pointer to the super block in the buffer */
14971d0c3924STheodore Ts'o 	struct buffer_head * __rcu *s_group_desc;
14987f4520ccSTheodore Ts'o 	unsigned int s_mount_opt;
1499a2595b8aSTheodore Ts'o 	unsigned int s_mount_opt2;
15009b5f6c9bSHarshad Shirwadkar 	unsigned long s_mount_flags;
15015a916be1STheodore Ts'o 	unsigned int s_def_mount_opt;
1502e3645d72SZhang Yi 	unsigned int s_def_mount_opt2;
1503ca0faba0STheodore Ts'o 	ext4_fsblk_t s_sb_block;
150427dd4385SLukas Czerner 	atomic64_t s_resv_clusters;
150508cefc7aSEric W. Biederman 	kuid_t s_resuid;
150608cefc7aSEric W. Biederman 	kgid_t s_resgid;
1507ca0faba0STheodore Ts'o 	unsigned short s_mount_state;
1508ca0faba0STheodore Ts'o 	unsigned short s_pad;
1509ca0faba0STheodore Ts'o 	int s_addr_per_block_bits;
1510ca0faba0STheodore Ts'o 	int s_desc_per_block_bits;
1511ca0faba0STheodore Ts'o 	int s_inode_size;
1512ca0faba0STheodore Ts'o 	int s_first_ino;
1513ca0faba0STheodore Ts'o 	unsigned int s_inode_readahead_blks;
151411013911SAndreas Dilger 	unsigned int s_inode_goal;
1515ca0faba0STheodore Ts'o 	u32 s_hash_seed[4];
1516ca0faba0STheodore Ts'o 	int s_def_hash_version;
15176d2424a8SEric Biggers 	int s_hash_unsigned;	/* 3 if hash should be unsigned, 0 if not */
151857042651STheodore Ts'o 	struct percpu_counter s_freeclusters_counter;
1519ca0faba0STheodore Ts'o 	struct percpu_counter s_freeinodes_counter;
1520ca0faba0STheodore Ts'o 	struct percpu_counter s_dirs_counter;
152157042651STheodore Ts'o 	struct percpu_counter s_dirtyclusters_counter;
1522efc61345SEric Whitney 	struct percpu_counter s_sra_exceeded_retry_limit;
1523ca0faba0STheodore Ts'o 	struct blockgroup_lock *s_blockgroup_lock;
1524ca0faba0STheodore Ts'o 	struct proc_dir_entry *s_proc;
1525ca0faba0STheodore Ts'o 	struct kobject s_kobj;
1526ca0faba0STheodore Ts'o 	struct completion s_kobj_unregister;
15272c0544b2STheodore Ts'o 	struct super_block *s_sb;
1528618f0031SPavel Skripkin 	struct buffer_head *s_mmp_bh;
1529ca0faba0STheodore Ts'o 
1530ca0faba0STheodore Ts'o 	/* Journaling */
1531ca0faba0STheodore Ts'o 	struct journal_s *s_journal;
15329549a168STheodore Ts'o 	unsigned long s_ext4_flags;		/* Ext4 superblock flags */
153302f310fcSJan Kara 	struct mutex s_orphan_lock;	/* Protects on disk list changes */
153402f310fcSJan Kara 	struct list_head s_orphan;	/* List of orphaned inodes in on disk
153502f310fcSJan Kara 					   list */
153602f310fcSJan Kara 	struct ext4_orphan_info s_orphan_info;
1537ca0faba0STheodore Ts'o 	unsigned long s_commit_interval;
1538ca0faba0STheodore Ts'o 	u32 s_max_batch_time;
1539ca0faba0STheodore Ts'o 	u32 s_min_batch_time;
1540ee7ed3aaSChunguang Xu 	struct block_device *s_journal_bdev;
1541ca0faba0STheodore Ts'o #ifdef CONFIG_QUOTA
154233458eabSTheodore Ts'o 	/* Names of quota files with journalled quota */
154333458eabSTheodore Ts'o 	char __rcu *s_qf_names[EXT4_MAXQUOTAS];
1544ca0faba0STheodore Ts'o 	int s_jquota_fmt;			/* Format of quota to use */
1545ca0faba0STheodore Ts'o #endif
1546ca0faba0STheodore Ts'o 	unsigned int s_want_extra_isize; /* New inodes should reserve # bytes */
1547dd0db94fSChunguang Xu 	struct ext4_system_blocks __rcu *s_system_blks;
1548ca0faba0STheodore Ts'o 
1549ca0faba0STheodore Ts'o #ifdef EXTENTS_STATS
1550ca0faba0STheodore Ts'o 	/* ext4 extents stats */
1551ca0faba0STheodore Ts'o 	unsigned long s_ext_min;
1552ca0faba0STheodore Ts'o 	unsigned long s_ext_max;
1553ca0faba0STheodore Ts'o 	unsigned long s_depth_max;
1554ca0faba0STheodore Ts'o 	spinlock_t s_ext_stats_lock;
1555ca0faba0STheodore Ts'o 	unsigned long s_ext_blocks;
1556ca0faba0STheodore Ts'o 	unsigned long s_ext_extents;
1557ca0faba0STheodore Ts'o #endif
1558ca0faba0STheodore Ts'o 
1559ca0faba0STheodore Ts'o 	/* for buddy allocator */
1560df3da4eaSSuraj Jitindar Singh 	struct ext4_group_info ** __rcu *s_group_info;
1561ca0faba0STheodore Ts'o 	struct inode *s_buddy_cache;
1562ca0faba0STheodore Ts'o 	spinlock_t s_md_lock;
1563ca0faba0STheodore Ts'o 	unsigned short *s_mb_offsets;
1564ca0faba0STheodore Ts'o 	unsigned int *s_mb_maxs;
156528623c2fSTheodore Ts'o 	unsigned int s_group_info_size;
1566d08854f5STheodore Ts'o 	unsigned int s_mb_free_pending;
1567a0154344SDaeho Jeong 	struct list_head s_freed_data_list;	/* List of blocks to be freed
1568a0154344SDaeho Jeong 						   after commit completed */
156955cdd0afSWang Jianchao 	struct list_head s_discard_list;
157055cdd0afSWang Jianchao 	struct work_struct s_discard_work;
15715036ab8dSWang Jianchao 	atomic_t s_retry_alloc_pending;
157283e80a6eSJan Kara 	struct list_head *s_mb_avg_fragment_size;
157383e80a6eSJan Kara 	rwlock_t *s_mb_avg_fragment_size_locks;
1574196e402aSHarshad Shirwadkar 	struct list_head *s_mb_largest_free_orders;
1575196e402aSHarshad Shirwadkar 	rwlock_t *s_mb_largest_free_orders_locks;
1576ca0faba0STheodore Ts'o 
1577ca0faba0STheodore Ts'o 	/* tunables */
1578ca0faba0STheodore Ts'o 	unsigned long s_stripe;
1579196e402aSHarshad Shirwadkar 	unsigned int s_mb_max_linear_groups;
1580ca0faba0STheodore Ts'o 	unsigned int s_mb_stream_request;
1581ca0faba0STheodore Ts'o 	unsigned int s_mb_max_to_scan;
1582ca0faba0STheodore Ts'o 	unsigned int s_mb_min_to_scan;
1583ca0faba0STheodore Ts'o 	unsigned int s_mb_stats;
1584ca0faba0STheodore Ts'o 	unsigned int s_mb_order2_reqs;
1585ca0faba0STheodore Ts'o 	unsigned int s_mb_group_prealloc;
1586df981d03STheodore Ts'o 	unsigned int s_max_dir_size_kb;
1587ca0faba0STheodore Ts'o 	/* where last allocation was done - for stream allocation */
1588ca0faba0STheodore Ts'o 	unsigned long s_mb_last_group;
1589ca0faba0STheodore Ts'o 	unsigned long s_mb_last_start;
1590cfd73237SAlex Zhuravlev 	unsigned int s_mb_prefetch;
1591cfd73237SAlex Zhuravlev 	unsigned int s_mb_prefetch_limit;
1592f52f3d2bSOjaswin Mujoo 	unsigned int s_mb_best_avail_max_trim_order;
1593ca0faba0STheodore Ts'o 
1594ca0faba0STheodore Ts'o 	/* stats for buddy allocator */
1595ca0faba0STheodore Ts'o 	atomic_t s_bal_reqs;	/* number of reqs with len > 1 */
1596ca0faba0STheodore Ts'o 	atomic_t s_bal_success;	/* we found long enough chunks */
1597ca0faba0STheodore Ts'o 	atomic_t s_bal_allocated;	/* in blocks */
1598ca0faba0STheodore Ts'o 	atomic_t s_bal_ex_scanned;	/* total extents scanned */
1599fdd9a009SOjaswin Mujoo 	atomic_t s_bal_cX_ex_scanned[EXT4_MB_NUM_CRS];	/* total extents scanned */
1600a6c75eafSHarshad Shirwadkar 	atomic_t s_bal_groups_scanned;	/* number of groups scanned */
1601ca0faba0STheodore Ts'o 	atomic_t s_bal_goals;	/* goal hits */
16023ef5d263SOjaswin Mujoo 	atomic_t s_bal_len_goals;	/* len goal hits */
1603ca0faba0STheodore Ts'o 	atomic_t s_bal_breaks;	/* too long searches */
1604ca0faba0STheodore Ts'o 	atomic_t s_bal_2orders;	/* 2^order hits */
1605f52f3d2bSOjaswin Mujoo 	atomic_t s_bal_p2_aligned_bad_suggestions;
1606f52f3d2bSOjaswin Mujoo 	atomic_t s_bal_goal_fast_bad_suggestions;
1607f52f3d2bSOjaswin Mujoo 	atomic_t s_bal_best_avail_bad_suggestions;
16084eb7a4a1SOjaswin Mujoo 	atomic64_t s_bal_cX_groups_considered[EXT4_MB_NUM_CRS];
16094eb7a4a1SOjaswin Mujoo 	atomic64_t s_bal_cX_hits[EXT4_MB_NUM_CRS];
16104eb7a4a1SOjaswin Mujoo 	atomic64_t s_bal_cX_failed[EXT4_MB_NUM_CRS];		/* cX loop didn't find blocks */
161167d25186SHarshad Shirwadkar 	atomic_t s_mb_buddies_generated;	/* number of buddies generated */
161267d25186SHarshad Shirwadkar 	atomic64_t s_mb_generation_time;
1613ca0faba0STheodore Ts'o 	atomic_t s_mb_lost_chunks;
1614ca0faba0STheodore Ts'o 	atomic_t s_mb_preallocated;
1615ca0faba0STheodore Ts'o 	atomic_t s_mb_discarded;
161650797481STheodore Ts'o 	atomic_t s_lock_busy;
1617ca0faba0STheodore Ts'o 
1618ca0faba0STheodore Ts'o 	/* locality groups */
1619003cb608STejun Heo 	struct ext4_locality_group __percpu *s_locality_groups;
1620ca0faba0STheodore Ts'o 
1621ca0faba0STheodore Ts'o 	/* for write statistics */
1622ca0faba0STheodore Ts'o 	unsigned long s_sectors_written_start;
1623ca0faba0STheodore Ts'o 	u64 s_kbytes_written;
1624ca0faba0STheodore Ts'o 
162567a5da56SZheng Liu 	/* the size of zero-out chunk */
162667a5da56SZheng Liu 	unsigned int s_extent_max_zeroout_kb;
162767a5da56SZheng Liu 
1628ca0faba0STheodore Ts'o 	unsigned int s_log_groups_per_flex;
16297c990728SSuraj Jitindar Singh 	struct flex_groups * __rcu *s_flex_groups;
1630117fff10STheodore Ts'o 	ext4_group_t s_flex_groups_allocated;
16314c0425ffSMingming Cao 
16322e8fa54eSJan Kara 	/* workqueue for reserved extent conversions (buffered io) */
16332e8fa54eSJan Kara 	struct workqueue_struct *rsv_conversion_wq;
163466e61a9eSTheodore Ts'o 
163566e61a9eSTheodore Ts'o 	/* timer for periodic error stats printing */
163666e61a9eSTheodore Ts'o 	struct timer_list s_err_report;
1637bfff6873SLukas Czerner 
1638bfff6873SLukas Czerner 	/* Lazy inode table initialization info */
1639bfff6873SLukas Czerner 	struct ext4_li_request *s_li_request;
1640bfff6873SLukas Czerner 	/* Wait multiplier for lazy initialization thread */
1641bfff6873SLukas Czerner 	unsigned int s_li_wait_mult;
1642c5e06d10SJohann Lombardi 
1643c5e06d10SJohann Lombardi 	/* Kernel thread for multiple mount protection */
1644c5e06d10SJohann Lombardi 	struct task_struct *s_mmp_tsk;
16453d56b8d2STao Ma 
16463d56b8d2STao Ma 	/* record the last minlen when FITRIM is called. */
16472327fb2eSLukas Czerner 	unsigned long s_last_trim_minblks;
16480441984aSDarrick J. Wong 
16490441984aSDarrick J. Wong 	/* Reference to checksum algorithm driver via cryptoapi */
16500441984aSDarrick J. Wong 	struct crypto_shash *s_chksum_driver;
1651a9c47317SDarrick J. Wong 
1652a9c47317SDarrick J. Wong 	/* Precomputed FS UUID checksum for seeding other checksums */
1653a9c47317SDarrick J. Wong 	__u32 s_csum_seed;
165474cd15cdSZheng Liu 
165574cd15cdSZheng Liu 	/* Reclaim extents from extent status tree */
165674cd15cdSZheng Liu 	struct shrinker s_es_shrinker;
1657dd475925SJan Kara 	struct list_head s_es_list;	/* List of inodes with reclaimable extents */
1658edaa53caSZheng Liu 	long s_es_nr_inode;
1659eb68d0e2SZheng Liu 	struct ext4_es_stats s_es_stats;
166047387409STahsin Erdogan 	struct mb_cache *s_ea_block_cache;
1661dec214d0STahsin Erdogan 	struct mb_cache *s_ea_inode_cache;
1662edaa53caSZheng Liu 	spinlock_t s_es_lock ____cacheline_aligned_in_smp;
1663efbed4dcSTheodore Ts'o 
1664188c299eSJan Kara 	/* Journal triggers for checksum computation */
1665188c299eSJan Kara 	struct ext4_journal_trigger s_journal_triggers[EXT4_JOURNAL_TRIGGER_COUNT];
1666188c299eSJan Kara 
1667efbed4dcSTheodore Ts'o 	/* Ratelimit ext4 messages. */
1668efbed4dcSTheodore Ts'o 	struct ratelimit_state s_err_ratelimit_state;
1669efbed4dcSTheodore Ts'o 	struct ratelimit_state s_warning_ratelimit_state;
1670efbed4dcSTheodore Ts'o 	struct ratelimit_state s_msg_ratelimit_state;
16711cf006edSDmitry Monakhov 	atomic_t s_warning_count;
16721cf006edSDmitry Monakhov 	atomic_t s_msg_count;
1673c8585c6fSDaeho Jeong 
1674ac4acb1fSEric Biggers 	/* Encryption policy for '-o test_dummy_encryption' */
1675ac4acb1fSEric Biggers 	struct fscrypt_dummy_policy s_dummy_enc_policy;
1676ed318a6cSEric Biggers 
1677cb85f4d2SEric Biggers 	/*
1678cb85f4d2SEric Biggers 	 * Barrier between writepages ops and changing any inode's JOURNAL_DATA
1679904fa65cSBaokun Li 	 * or EXTENTS flag or between writepages ops and changing DELALLOC or
1680904fa65cSBaokun Li 	 * DIOREAD_NOLOCK mount options on remount.
1681cb85f4d2SEric Biggers 	 */
1682bbd55937SEric Biggers 	struct percpu_rw_semaphore s_writepages_rwsem;
16835e405595SDan Williams 	struct dax_device *s_daxdev;
1684cd913c76SChristoph Hellwig 	u64 s_dax_part_off;
168546f870d6STheodore Ts'o #ifdef CONFIG_EXT4_DEBUG
168646f870d6STheodore Ts'o 	unsigned long s_simulate_fail;
168746f870d6STheodore Ts'o #endif
1688bc71726cSzhangyi (F) 	/* Record the errseq of the backing block device */
1689bc71726cSzhangyi (F) 	errseq_t s_bdev_wb_err;
1690bc71726cSzhangyi (F) 	spinlock_t s_bdev_wb_lock;
1691aa75f4d3SHarshad Shirwadkar 
1692c92dc856SJan Kara 	/* Information about errors that happened during this mount */
1693c92dc856SJan Kara 	spinlock_t s_error_lock;
1694c92dc856SJan Kara 	int s_add_error_count;
1695c92dc856SJan Kara 	int s_first_error_code;
1696c92dc856SJan Kara 	__u32 s_first_error_line;
1697c92dc856SJan Kara 	__u32 s_first_error_ino;
1698c92dc856SJan Kara 	__u64 s_first_error_block;
1699c92dc856SJan Kara 	const char *s_first_error_func;
1700c92dc856SJan Kara 	time64_t s_first_error_time;
1701c92dc856SJan Kara 	int s_last_error_code;
1702c92dc856SJan Kara 	__u32 s_last_error_line;
1703c92dc856SJan Kara 	__u32 s_last_error_ino;
1704c92dc856SJan Kara 	__u64 s_last_error_block;
1705c92dc856SJan Kara 	const char *s_last_error_func;
1706c92dc856SJan Kara 	time64_t s_last_error_time;
1707c92dc856SJan Kara 	/*
1708bb15cea2STheodore Ts'o 	 * If we are in a context where we cannot update the on-disk
1709bb15cea2STheodore Ts'o 	 * superblock, we queue the work here.  This is used to update
1710bb15cea2STheodore Ts'o 	 * the error information in the superblock, and for periodic
1711bb15cea2STheodore Ts'o 	 * updates of the superblock called from the commit callback
1712bb15cea2STheodore Ts'o 	 * function.
1713c92dc856SJan Kara 	 */
1714bb15cea2STheodore Ts'o 	struct work_struct s_sb_upd_work;
1715c92dc856SJan Kara 
17167bbbe241SHarshad Shirwadkar 	/* Ext4 fast commit sub transaction ID */
1717aa75f4d3SHarshad Shirwadkar 	atomic_t s_fc_subtid;
17187bbbe241SHarshad Shirwadkar 
1719aa75f4d3SHarshad Shirwadkar 	/*
1720aa75f4d3SHarshad Shirwadkar 	 * After commit starts, the main queue gets locked, and the further
1721aa75f4d3SHarshad Shirwadkar 	 * updates get added in the staging queue.
1722aa75f4d3SHarshad Shirwadkar 	 */
1723aa75f4d3SHarshad Shirwadkar #define FC_Q_MAIN	0
1724aa75f4d3SHarshad Shirwadkar #define FC_Q_STAGING	1
1725aa75f4d3SHarshad Shirwadkar 	struct list_head s_fc_q[2];	/* Inodes staged for fast commit
1726aa75f4d3SHarshad Shirwadkar 					 * that have data changes in them.
1727aa75f4d3SHarshad Shirwadkar 					 */
1728aa75f4d3SHarshad Shirwadkar 	struct list_head s_fc_dentry_q[2];	/* directory entry updates */
1729aa75f4d3SHarshad Shirwadkar 	unsigned int s_fc_bytes;
1730aa75f4d3SHarshad Shirwadkar 	/*
1731aa75f4d3SHarshad Shirwadkar 	 * Main fast commit lock. This lock protects accesses to the
1732aa75f4d3SHarshad Shirwadkar 	 * following fields:
1733aa75f4d3SHarshad Shirwadkar 	 * ei->i_fc_list, s_fc_dentry_q, s_fc_q, s_fc_bytes, s_fc_bh.
1734aa75f4d3SHarshad Shirwadkar 	 */
1735aa75f4d3SHarshad Shirwadkar 	spinlock_t s_fc_lock;
1736aa75f4d3SHarshad Shirwadkar 	struct buffer_head *s_fc_bh;
1737aa75f4d3SHarshad Shirwadkar 	struct ext4_fc_stats s_fc_stats;
1738e85c81baSXin Yin 	tid_t s_fc_ineligible_tid;
17398016e29fSHarshad Shirwadkar #ifdef CONFIG_EXT4_DEBUG
17408016e29fSHarshad Shirwadkar 	int s_fc_debug_max_replay;
17418016e29fSHarshad Shirwadkar #endif
17428016e29fSHarshad Shirwadkar 	struct ext4_fc_replay_state s_fc_replay_state;
1743ca0faba0STheodore Ts'o };
1744ca0faba0STheodore Ts'o 
EXT4_SB(struct super_block * sb)17453dcf5451SChristoph Hellwig static inline struct ext4_sb_info *EXT4_SB(struct super_block *sb)
17463dcf5451SChristoph Hellwig {
17473dcf5451SChristoph Hellwig 	return sb->s_fs_info;
17483dcf5451SChristoph Hellwig }
EXT4_I(struct inode * inode)17493dcf5451SChristoph Hellwig static inline struct ext4_inode_info *EXT4_I(struct inode *inode)
17503dcf5451SChristoph Hellwig {
17513dcf5451SChristoph Hellwig 	return container_of(inode, struct ext4_inode_info, vfs_inode);
17523dcf5451SChristoph Hellwig }
17533dcf5451SChristoph Hellwig 
ext4_writepages_down_read(struct super_block * sb)175400d873c1SJan Kara static inline int ext4_writepages_down_read(struct super_block *sb)
175500d873c1SJan Kara {
175600d873c1SJan Kara 	percpu_down_read(&EXT4_SB(sb)->s_writepages_rwsem);
175700d873c1SJan Kara 	return memalloc_nofs_save();
175800d873c1SJan Kara }
175900d873c1SJan Kara 
ext4_writepages_up_read(struct super_block * sb,int ctx)176000d873c1SJan Kara static inline void ext4_writepages_up_read(struct super_block *sb, int ctx)
176100d873c1SJan Kara {
176200d873c1SJan Kara 	memalloc_nofs_restore(ctx);
176300d873c1SJan Kara 	percpu_up_read(&EXT4_SB(sb)->s_writepages_rwsem);
176400d873c1SJan Kara }
176500d873c1SJan Kara 
ext4_writepages_down_write(struct super_block * sb)176600d873c1SJan Kara static inline int ext4_writepages_down_write(struct super_block *sb)
176700d873c1SJan Kara {
176800d873c1SJan Kara 	percpu_down_write(&EXT4_SB(sb)->s_writepages_rwsem);
176900d873c1SJan Kara 	return memalloc_nofs_save();
177000d873c1SJan Kara }
177100d873c1SJan Kara 
ext4_writepages_up_write(struct super_block * sb,int ctx)177200d873c1SJan Kara static inline void ext4_writepages_up_write(struct super_block *sb, int ctx)
177300d873c1SJan Kara {
177400d873c1SJan Kara 	memalloc_nofs_restore(ctx);
177500d873c1SJan Kara 	percpu_up_write(&EXT4_SB(sb)->s_writepages_rwsem);
177600d873c1SJan Kara }
177700d873c1SJan Kara 
ext4_valid_inum(struct super_block * sb,unsigned long ino)17783dcf5451SChristoph Hellwig static inline int ext4_valid_inum(struct super_block *sb, unsigned long ino)
17793dcf5451SChristoph Hellwig {
17803dcf5451SChristoph Hellwig 	return ino == EXT4_ROOT_INO ||
17813dcf5451SChristoph Hellwig 		(ino >= EXT4_FIRST_INO(sb) &&
17823dcf5451SChristoph Hellwig 		 ino <= le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count));
17833dcf5451SChristoph Hellwig }
178419f5fb7aSTheodore Ts'o 
178519f5fb7aSTheodore Ts'o /*
17861d0c3924STheodore Ts'o  * Returns: sbi->field[index]
17871d0c3924STheodore Ts'o  * Used to access an array element from the following sbi fields which require
17881d0c3924STheodore Ts'o  * rcu protection to avoid dereferencing an invalid pointer due to reassignment
17891d0c3924STheodore Ts'o  * - s_group_desc
17901d0c3924STheodore Ts'o  * - s_group_info
17911d0c3924STheodore Ts'o  * - s_flex_group
17921d0c3924STheodore Ts'o  */
17931d0c3924STheodore Ts'o #define sbi_array_rcu_deref(sbi, field, index)				   \
17941d0c3924STheodore Ts'o ({									   \
17951d0c3924STheodore Ts'o 	typeof(*((sbi)->field)) _v;					   \
17961d0c3924STheodore Ts'o 	rcu_read_lock();						   \
17971d0c3924STheodore Ts'o 	_v = ((typeof(_v)*)rcu_dereference((sbi)->field))[index];	   \
17981d0c3924STheodore Ts'o 	rcu_read_unlock();						   \
17991d0c3924STheodore Ts'o 	_v;								   \
18001d0c3924STheodore Ts'o })
18011d0c3924STheodore Ts'o 
18021d0c3924STheodore Ts'o /*
18039b5f6c9bSHarshad Shirwadkar  * run-time mount flags
18049b5f6c9bSHarshad Shirwadkar  */
18059b5f6c9bSHarshad Shirwadkar enum {
18069b5f6c9bSHarshad Shirwadkar 	EXT4_MF_MNTDIR_SAMPLED,
1807bdc8a53aSXin Yin 	EXT4_MF_FC_INELIGIBLE	/* Fast commit ineligible */
18089b5f6c9bSHarshad Shirwadkar };
18099b5f6c9bSHarshad Shirwadkar 
ext4_set_mount_flag(struct super_block * sb,int bit)18109b5f6c9bSHarshad Shirwadkar static inline void ext4_set_mount_flag(struct super_block *sb, int bit)
18119b5f6c9bSHarshad Shirwadkar {
18129b5f6c9bSHarshad Shirwadkar 	set_bit(bit, &EXT4_SB(sb)->s_mount_flags);
18139b5f6c9bSHarshad Shirwadkar }
18149b5f6c9bSHarshad Shirwadkar 
ext4_clear_mount_flag(struct super_block * sb,int bit)18159b5f6c9bSHarshad Shirwadkar static inline void ext4_clear_mount_flag(struct super_block *sb, int bit)
18169b5f6c9bSHarshad Shirwadkar {
18179b5f6c9bSHarshad Shirwadkar 	clear_bit(bit, &EXT4_SB(sb)->s_mount_flags);
18189b5f6c9bSHarshad Shirwadkar }
18199b5f6c9bSHarshad Shirwadkar 
ext4_test_mount_flag(struct super_block * sb,int bit)18209b5f6c9bSHarshad Shirwadkar static inline int ext4_test_mount_flag(struct super_block *sb, int bit)
18219b5f6c9bSHarshad Shirwadkar {
18229b5f6c9bSHarshad Shirwadkar 	return test_bit(bit, &EXT4_SB(sb)->s_mount_flags);
18239b5f6c9bSHarshad Shirwadkar }
18249b5f6c9bSHarshad Shirwadkar 
18259b5f6c9bSHarshad Shirwadkar 
18269b5f6c9bSHarshad Shirwadkar /*
182746f870d6STheodore Ts'o  * Simulate_fail codes
182846f870d6STheodore Ts'o  */
182946f870d6STheodore Ts'o #define EXT4_SIM_BBITMAP_EIO	1
183046f870d6STheodore Ts'o #define EXT4_SIM_BBITMAP_CRC	2
183146f870d6STheodore Ts'o #define EXT4_SIM_IBITMAP_EIO	3
183246f870d6STheodore Ts'o #define EXT4_SIM_IBITMAP_CRC	4
183346f870d6STheodore Ts'o #define EXT4_SIM_INODE_EIO	5
183446f870d6STheodore Ts'o #define EXT4_SIM_INODE_CRC	6
183546f870d6STheodore Ts'o #define EXT4_SIM_DIRBLOCK_EIO	7
183646f870d6STheodore Ts'o #define EXT4_SIM_DIRBLOCK_CRC	8
183746f870d6STheodore Ts'o 
ext4_simulate_fail(struct super_block * sb,unsigned long code)183846f870d6STheodore Ts'o static inline bool ext4_simulate_fail(struct super_block *sb,
183946f870d6STheodore Ts'o 				     unsigned long code)
184046f870d6STheodore Ts'o {
184146f870d6STheodore Ts'o #ifdef CONFIG_EXT4_DEBUG
184246f870d6STheodore Ts'o 	struct ext4_sb_info *sbi = EXT4_SB(sb);
184346f870d6STheodore Ts'o 
184446f870d6STheodore Ts'o 	if (unlikely(sbi->s_simulate_fail == code)) {
184546f870d6STheodore Ts'o 		sbi->s_simulate_fail = 0;
184646f870d6STheodore Ts'o 		return true;
184746f870d6STheodore Ts'o 	}
184846f870d6STheodore Ts'o #endif
184946f870d6STheodore Ts'o 	return false;
185046f870d6STheodore Ts'o }
185146f870d6STheodore Ts'o 
ext4_simulate_fail_bh(struct super_block * sb,struct buffer_head * bh,unsigned long code)185246f870d6STheodore Ts'o static inline void ext4_simulate_fail_bh(struct super_block *sb,
185346f870d6STheodore Ts'o 					 struct buffer_head *bh,
185446f870d6STheodore Ts'o 					 unsigned long code)
185546f870d6STheodore Ts'o {
185646f870d6STheodore Ts'o 	if (!IS_ERR(bh) && ext4_simulate_fail(sb, code))
185746f870d6STheodore Ts'o 		clear_buffer_uptodate(bh);
185846f870d6STheodore Ts'o }
185946f870d6STheodore Ts'o 
186046f870d6STheodore Ts'o /*
1861878520acSTheodore Ts'o  * Error number codes for s_{first,last}_error_errno
1862878520acSTheodore Ts'o  *
1863878520acSTheodore Ts'o  * Linux errno numbers are architecture specific, so we need to translate
1864878520acSTheodore Ts'o  * them into something which is architecture independent.   We don't define
1865878520acSTheodore Ts'o  * codes for all errno's; just the ones which are most likely to be the cause
1866878520acSTheodore Ts'o  * of an ext4_error() call.
1867878520acSTheodore Ts'o  */
1868878520acSTheodore Ts'o #define EXT4_ERR_UNKNOWN	 1
1869878520acSTheodore Ts'o #define EXT4_ERR_EIO		 2
1870878520acSTheodore Ts'o #define EXT4_ERR_ENOMEM		 3
1871878520acSTheodore Ts'o #define EXT4_ERR_EFSBADCRC	 4
1872878520acSTheodore Ts'o #define EXT4_ERR_EFSCORRUPTED	 5
1873878520acSTheodore Ts'o #define EXT4_ERR_ENOSPC		 6
1874878520acSTheodore Ts'o #define EXT4_ERR_ENOKEY		 7
1875878520acSTheodore Ts'o #define EXT4_ERR_EROFS		 8
1876878520acSTheodore Ts'o #define EXT4_ERR_EFBIG		 9
1877878520acSTheodore Ts'o #define EXT4_ERR_EEXIST		10
1878878520acSTheodore Ts'o #define EXT4_ERR_ERANGE		11
1879878520acSTheodore Ts'o #define EXT4_ERR_EOVERFLOW	12
1880878520acSTheodore Ts'o #define EXT4_ERR_EBUSY		13
1881878520acSTheodore Ts'o #define EXT4_ERR_ENOTDIR	14
1882878520acSTheodore Ts'o #define EXT4_ERR_ENOTEMPTY	15
1883878520acSTheodore Ts'o #define EXT4_ERR_ESHUTDOWN	16
1884878520acSTheodore Ts'o #define EXT4_ERR_EFAULT		17
1885878520acSTheodore Ts'o 
1886878520acSTheodore Ts'o /*
188719f5fb7aSTheodore Ts'o  * Inode dynamic state flags
188819f5fb7aSTheodore Ts'o  */
188919f5fb7aSTheodore Ts'o enum {
189019f5fb7aSTheodore Ts'o 	EXT4_STATE_NEW,			/* inode is newly created */
189119f5fb7aSTheodore Ts'o 	EXT4_STATE_XATTR,		/* has in-inode xattrs */
189219f5fb7aSTheodore Ts'o 	EXT4_STATE_NO_EXPAND,		/* No space for expansion */
189319f5fb7aSTheodore Ts'o 	EXT4_STATE_DA_ALLOC_CLOSE,	/* Alloc DA blks on close */
189419f5fb7aSTheodore Ts'o 	EXT4_STATE_EXT_MIGRATE,		/* Inode is migrating */
189514ece102SFrank Mayhar 	EXT4_STATE_NEWENTRY,		/* File just added to dir */
189667cf5b09STao Ma 	EXT4_STATE_MAY_INLINE_DATA,	/* may have in-inode data */
18977869a4a6STheodore Ts'o 	EXT4_STATE_EXT_PRECACHED,	/* extents have been precached */
1898a6d05676STahsin Erdogan 	EXT4_STATE_LUSTRE_EA_INODE,	/* Lustre-style ea_inode */
1899c93d8f88SEric Biggers 	EXT4_STATE_VERITY_IN_PROGRESS,	/* building fs-verity Merkle tree */
1900aa75f4d3SHarshad Shirwadkar 	EXT4_STATE_FC_COMMITTING,	/* Fast commit ongoing */
190102f310fcSJan Kara 	EXT4_STATE_ORPHAN_FILE,		/* Inode orphaned in orphan file */
190219f5fb7aSTheodore Ts'o };
190319f5fb7aSTheodore Ts'o 
1904353eb83cSTheodore Ts'o #define EXT4_INODE_BIT_FNS(name, field, offset)				\
190512e9b892SDmitry Monakhov static inline int ext4_test_inode_##name(struct inode *inode, int bit)	\
190612e9b892SDmitry Monakhov {									\
1907353eb83cSTheodore Ts'o 	return test_bit(bit + (offset), &EXT4_I(inode)->i_##field);	\
190812e9b892SDmitry Monakhov }									\
190912e9b892SDmitry Monakhov static inline void ext4_set_inode_##name(struct inode *inode, int bit)	\
191012e9b892SDmitry Monakhov {									\
1911353eb83cSTheodore Ts'o 	set_bit(bit + (offset), &EXT4_I(inode)->i_##field);		\
191212e9b892SDmitry Monakhov }									\
191312e9b892SDmitry Monakhov static inline void ext4_clear_inode_##name(struct inode *inode, int bit) \
191412e9b892SDmitry Monakhov {									\
1915353eb83cSTheodore Ts'o 	clear_bit(bit + (offset), &EXT4_I(inode)->i_##field);		\
191619f5fb7aSTheodore Ts'o }
191719f5fb7aSTheodore Ts'o 
19183f61c0ccSAndreas Dilger /* Add these declarations here only so that these functions can be
19193f61c0ccSAndreas Dilger  * found by name.  Otherwise, they are very hard to locate. */
19203f61c0ccSAndreas Dilger static inline int ext4_test_inode_flag(struct inode *inode, int bit);
19213f61c0ccSAndreas Dilger static inline void ext4_set_inode_flag(struct inode *inode, int bit);
19223f61c0ccSAndreas Dilger static inline void ext4_clear_inode_flag(struct inode *inode, int bit);
1923353eb83cSTheodore Ts'o EXT4_INODE_BIT_FNS(flag, flags, 0)
19243f61c0ccSAndreas Dilger 
19253f61c0ccSAndreas Dilger /* Add these declarations here only so that these functions can be
19263f61c0ccSAndreas Dilger  * found by name.  Otherwise, they are very hard to locate. */
19273f61c0ccSAndreas Dilger static inline int ext4_test_inode_state(struct inode *inode, int bit);
19283f61c0ccSAndreas Dilger static inline void ext4_set_inode_state(struct inode *inode, int bit);
19293f61c0ccSAndreas Dilger static inline void ext4_clear_inode_state(struct inode *inode, int bit);
1930353eb83cSTheodore Ts'o #if (BITS_PER_LONG < 64)
1931353eb83cSTheodore Ts'o EXT4_INODE_BIT_FNS(state, state_flags, 0)
1932353eb83cSTheodore Ts'o 
ext4_clear_state_flags(struct ext4_inode_info * ei)1933353eb83cSTheodore Ts'o static inline void ext4_clear_state_flags(struct ext4_inode_info *ei)
1934353eb83cSTheodore Ts'o {
1935353eb83cSTheodore Ts'o 	(ei)->i_state_flags = 0;
1936353eb83cSTheodore Ts'o }
1937353eb83cSTheodore Ts'o #else
1938353eb83cSTheodore Ts'o EXT4_INODE_BIT_FNS(state, flags, 32)
1939353eb83cSTheodore Ts'o 
ext4_clear_state_flags(struct ext4_inode_info * ei)1940353eb83cSTheodore Ts'o static inline void ext4_clear_state_flags(struct ext4_inode_info *ei)
1941353eb83cSTheodore Ts'o {
1942353eb83cSTheodore Ts'o 	/* We depend on the fact that callers will set i_flags */
1943353eb83cSTheodore Ts'o }
1944353eb83cSTheodore Ts'o #endif
19453dcf5451SChristoph Hellwig #else
19463dcf5451SChristoph Hellwig /* Assume that user mode programs are passing in an ext4fs superblock, not
19473dcf5451SChristoph Hellwig  * a kernel struct super_block.  This will allow us to call the feature-test
19483dcf5451SChristoph Hellwig  * macros from user land. */
19493dcf5451SChristoph Hellwig #define EXT4_SB(sb)	(sb)
19503dcf5451SChristoph Hellwig #endif
19513dcf5451SChristoph Hellwig 
ext4_verity_in_progress(struct inode * inode)1952c93d8f88SEric Biggers static inline bool ext4_verity_in_progress(struct inode *inode)
1953c93d8f88SEric Biggers {
1954c93d8f88SEric Biggers 	return IS_ENABLED(CONFIG_FS_VERITY) &&
1955c93d8f88SEric Biggers 	       ext4_test_inode_state(inode, EXT4_STATE_VERITY_IN_PROGRESS);
1956c93d8f88SEric Biggers }
1957c93d8f88SEric Biggers 
19583dcf5451SChristoph Hellwig #define NEXT_ORPHAN(inode) EXT4_I(inode)->i_dtime
19593dcf5451SChristoph Hellwig 
19603dcf5451SChristoph Hellwig /*
19613dcf5451SChristoph Hellwig  * Codes for operating systems
19623dcf5451SChristoph Hellwig  */
19633dcf5451SChristoph Hellwig #define EXT4_OS_LINUX		0
19643dcf5451SChristoph Hellwig #define EXT4_OS_HURD		1
19653dcf5451SChristoph Hellwig #define EXT4_OS_MASIX		2
19663dcf5451SChristoph Hellwig #define EXT4_OS_FREEBSD		3
19673dcf5451SChristoph Hellwig #define EXT4_OS_LITES		4
19683dcf5451SChristoph Hellwig 
19693dcf5451SChristoph Hellwig /*
19703dcf5451SChristoph Hellwig  * Revision levels
19713dcf5451SChristoph Hellwig  */
19723dcf5451SChristoph Hellwig #define EXT4_GOOD_OLD_REV	0	/* The good old (original) format */
19733dcf5451SChristoph Hellwig #define EXT4_DYNAMIC_REV	1	/* V2 format w/ dynamic inode sizes */
19743dcf5451SChristoph Hellwig 
19753dcf5451SChristoph Hellwig #define EXT4_MAX_SUPP_REV	EXT4_DYNAMIC_REV
19763dcf5451SChristoph Hellwig 
19773dcf5451SChristoph Hellwig #define EXT4_GOOD_OLD_INODE_SIZE 128
19783dcf5451SChristoph Hellwig 
19794881c497SDeepa Dinamani #define EXT4_EXTRA_TIMESTAMP_MAX	(((s64)1 << 34) - 1  + S32_MIN)
19804881c497SDeepa Dinamani #define EXT4_NON_EXTRA_TIMESTAMP_MAX	S32_MAX
19814881c497SDeepa Dinamani #define EXT4_TIMESTAMP_MIN		S32_MIN
19824881c497SDeepa Dinamani 
19833dcf5451SChristoph Hellwig /*
19843dcf5451SChristoph Hellwig  * Feature set definitions
19853dcf5451SChristoph Hellwig  */
19863dcf5451SChristoph Hellwig 
19873dcf5451SChristoph Hellwig #define EXT4_FEATURE_COMPAT_DIR_PREALLOC	0x0001
19883dcf5451SChristoph Hellwig #define EXT4_FEATURE_COMPAT_IMAGIC_INODES	0x0002
19893dcf5451SChristoph Hellwig #define EXT4_FEATURE_COMPAT_HAS_JOURNAL		0x0004
19903dcf5451SChristoph Hellwig #define EXT4_FEATURE_COMPAT_EXT_ATTR		0x0008
19913dcf5451SChristoph Hellwig #define EXT4_FEATURE_COMPAT_RESIZE_INODE	0x0010
19923dcf5451SChristoph Hellwig #define EXT4_FEATURE_COMPAT_DIR_INDEX		0x0020
19931beeef1bSDarrick J. Wong #define EXT4_FEATURE_COMPAT_SPARSE_SUPER2	0x0200
1994a44ad683SHarshad Shirwadkar /*
1995a44ad683SHarshad Shirwadkar  * The reason why "FAST_COMMIT" is a compat feature is that, FS becomes
1996a44ad683SHarshad Shirwadkar  * incompatible only if fast commit blocks are present in the FS. Since we
1997a44ad683SHarshad Shirwadkar  * clear the journal (and thus the fast commit blocks), we don't mark FS as
1998a44ad683SHarshad Shirwadkar  * incompatible. We also have a JBD2 incompat feature, which gets set when
1999a44ad683SHarshad Shirwadkar  * there are fast commit blocks present in the journal.
2000a44ad683SHarshad Shirwadkar  */
2001995a3ed6SHarshad Shirwadkar #define EXT4_FEATURE_COMPAT_FAST_COMMIT		0x0400
2002b925acb8SEric Biggers #define EXT4_FEATURE_COMPAT_STABLE_INODES	0x0800
200302f310fcSJan Kara #define EXT4_FEATURE_COMPAT_ORPHAN_FILE		0x1000	/* Orphan file exists */
20043dcf5451SChristoph Hellwig 
20053dcf5451SChristoph Hellwig #define EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER	0x0001
20063dcf5451SChristoph Hellwig #define EXT4_FEATURE_RO_COMPAT_LARGE_FILE	0x0002
20073dcf5451SChristoph Hellwig #define EXT4_FEATURE_RO_COMPAT_BTREE_DIR	0x0004
20083dcf5451SChristoph Hellwig #define EXT4_FEATURE_RO_COMPAT_HUGE_FILE        0x0008
20093dcf5451SChristoph Hellwig #define EXT4_FEATURE_RO_COMPAT_GDT_CSUM		0x0010
20103dcf5451SChristoph Hellwig #define EXT4_FEATURE_RO_COMPAT_DIR_NLINK	0x0020
20113dcf5451SChristoph Hellwig #define EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE	0x0040
2012ae812306SAditya Kali #define EXT4_FEATURE_RO_COMPAT_QUOTA		0x0100
2013281b5995STheodore Ts'o #define EXT4_FEATURE_RO_COMPAT_BIGALLOC		0x0200
2014e6153918SDarrick J. Wong /*
2015e6153918SDarrick J. Wong  * METADATA_CSUM also enables group descriptor checksums (GDT_CSUM).  When
2016e6153918SDarrick J. Wong  * METADATA_CSUM is set, group descriptor checksums use the same algorithm as
2017e6153918SDarrick J. Wong  * all other data structures' checksums.  However, the METADATA_CSUM and
2018e6153918SDarrick J. Wong  * GDT_CSUM bits are mutually exclusive.
2019e6153918SDarrick J. Wong  */
20209b90e5e0STheodore Ts'o #define EXT4_FEATURE_RO_COMPAT_METADATA_CSUM	0x0400
20212cb5cc8bSDarrick J. Wong #define EXT4_FEATURE_RO_COMPAT_READONLY		0x1000
20228b4953e1STheodore Ts'o #define EXT4_FEATURE_RO_COMPAT_PROJECT		0x2000
2023c93d8f88SEric Biggers #define EXT4_FEATURE_RO_COMPAT_VERITY		0x8000
202402f310fcSJan Kara #define EXT4_FEATURE_RO_COMPAT_ORPHAN_PRESENT	0x10000 /* Orphan file may be
202502f310fcSJan Kara 							   non-empty */
20263dcf5451SChristoph Hellwig 
20273dcf5451SChristoph Hellwig #define EXT4_FEATURE_INCOMPAT_COMPRESSION	0x0001
20283dcf5451SChristoph Hellwig #define EXT4_FEATURE_INCOMPAT_FILETYPE		0x0002
20293dcf5451SChristoph Hellwig #define EXT4_FEATURE_INCOMPAT_RECOVER		0x0004 /* Needs recovery */
20303dcf5451SChristoph Hellwig #define EXT4_FEATURE_INCOMPAT_JOURNAL_DEV	0x0008 /* Journal device */
20313dcf5451SChristoph Hellwig #define EXT4_FEATURE_INCOMPAT_META_BG		0x0010
20323dcf5451SChristoph Hellwig #define EXT4_FEATURE_INCOMPAT_EXTENTS		0x0040 /* extents support */
20333dcf5451SChristoph Hellwig #define EXT4_FEATURE_INCOMPAT_64BIT		0x0080
20343dcf5451SChristoph Hellwig #define EXT4_FEATURE_INCOMPAT_MMP               0x0100
20353dcf5451SChristoph Hellwig #define EXT4_FEATURE_INCOMPAT_FLEX_BG		0x0200
2036f710b4b9STheodore Ts'o #define EXT4_FEATURE_INCOMPAT_EA_INODE		0x0400 /* EA in inode */
2037f710b4b9STheodore Ts'o #define EXT4_FEATURE_INCOMPAT_DIRDATA		0x1000 /* data in dirent */
20388c81bd8fSDarrick J. Wong #define EXT4_FEATURE_INCOMPAT_CSUM_SEED		0x2000
20399b90e5e0STheodore Ts'o #define EXT4_FEATURE_INCOMPAT_LARGEDIR		0x4000 /* >2GB or 3-lvl htree */
204067cf5b09STao Ma #define EXT4_FEATURE_INCOMPAT_INLINE_DATA	0x8000 /* data in inode */
20413edc18d8STheodore Ts'o #define EXT4_FEATURE_INCOMPAT_ENCRYPT		0x10000
2042c83ad55eSGabriel Krisman Bertazi #define EXT4_FEATURE_INCOMPAT_CASEFOLD		0x20000
20433dcf5451SChristoph Hellwig 
2044c9e716ebSAndreas Dilger extern void ext4_update_dynamic_rev(struct super_block *sb);
2045c9e716ebSAndreas Dilger 
2046e2b911c5SDarrick J. Wong #define EXT4_FEATURE_COMPAT_FUNCS(name, flagname) \
2047e2b911c5SDarrick J. Wong static inline bool ext4_has_feature_##name(struct super_block *sb) \
2048e2b911c5SDarrick J. Wong { \
2049e2b911c5SDarrick J. Wong 	return ((EXT4_SB(sb)->s_es->s_feature_compat & \
2050e2b911c5SDarrick J. Wong 		cpu_to_le32(EXT4_FEATURE_COMPAT_##flagname)) != 0); \
2051e2b911c5SDarrick J. Wong } \
2052e2b911c5SDarrick J. Wong static inline void ext4_set_feature_##name(struct super_block *sb) \
2053e2b911c5SDarrick J. Wong { \
2054c9e716ebSAndreas Dilger 	ext4_update_dynamic_rev(sb); \
2055e2b911c5SDarrick J. Wong 	EXT4_SB(sb)->s_es->s_feature_compat |= \
2056e2b911c5SDarrick J. Wong 		cpu_to_le32(EXT4_FEATURE_COMPAT_##flagname); \
2057e2b911c5SDarrick J. Wong } \
2058e2b911c5SDarrick J. Wong static inline void ext4_clear_feature_##name(struct super_block *sb) \
2059e2b911c5SDarrick J. Wong { \
2060e2b911c5SDarrick J. Wong 	EXT4_SB(sb)->s_es->s_feature_compat &= \
2061e2b911c5SDarrick J. Wong 		~cpu_to_le32(EXT4_FEATURE_COMPAT_##flagname); \
2062e2b911c5SDarrick J. Wong }
2063e2b911c5SDarrick J. Wong 
2064e2b911c5SDarrick J. Wong #define EXT4_FEATURE_RO_COMPAT_FUNCS(name, flagname) \
2065e2b911c5SDarrick J. Wong static inline bool ext4_has_feature_##name(struct super_block *sb) \
2066e2b911c5SDarrick J. Wong { \
2067e2b911c5SDarrick J. Wong 	return ((EXT4_SB(sb)->s_es->s_feature_ro_compat & \
2068e2b911c5SDarrick J. Wong 		cpu_to_le32(EXT4_FEATURE_RO_COMPAT_##flagname)) != 0); \
2069e2b911c5SDarrick J. Wong } \
2070e2b911c5SDarrick J. Wong static inline void ext4_set_feature_##name(struct super_block *sb) \
2071e2b911c5SDarrick J. Wong { \
2072c9e716ebSAndreas Dilger 	ext4_update_dynamic_rev(sb); \
2073e2b911c5SDarrick J. Wong 	EXT4_SB(sb)->s_es->s_feature_ro_compat |= \
2074e2b911c5SDarrick J. Wong 		cpu_to_le32(EXT4_FEATURE_RO_COMPAT_##flagname); \
2075e2b911c5SDarrick J. Wong } \
2076e2b911c5SDarrick J. Wong static inline void ext4_clear_feature_##name(struct super_block *sb) \
2077e2b911c5SDarrick J. Wong { \
2078e2b911c5SDarrick J. Wong 	EXT4_SB(sb)->s_es->s_feature_ro_compat &= \
2079e2b911c5SDarrick J. Wong 		~cpu_to_le32(EXT4_FEATURE_RO_COMPAT_##flagname); \
2080e2b911c5SDarrick J. Wong }
2081e2b911c5SDarrick J. Wong 
2082e2b911c5SDarrick J. Wong #define EXT4_FEATURE_INCOMPAT_FUNCS(name, flagname) \
2083e2b911c5SDarrick J. Wong static inline bool ext4_has_feature_##name(struct super_block *sb) \
2084e2b911c5SDarrick J. Wong { \
2085e2b911c5SDarrick J. Wong 	return ((EXT4_SB(sb)->s_es->s_feature_incompat & \
2086e2b911c5SDarrick J. Wong 		cpu_to_le32(EXT4_FEATURE_INCOMPAT_##flagname)) != 0); \
2087e2b911c5SDarrick J. Wong } \
2088e2b911c5SDarrick J. Wong static inline void ext4_set_feature_##name(struct super_block *sb) \
2089e2b911c5SDarrick J. Wong { \
2090c9e716ebSAndreas Dilger 	ext4_update_dynamic_rev(sb); \
2091e2b911c5SDarrick J. Wong 	EXT4_SB(sb)->s_es->s_feature_incompat |= \
2092e2b911c5SDarrick J. Wong 		cpu_to_le32(EXT4_FEATURE_INCOMPAT_##flagname); \
2093e2b911c5SDarrick J. Wong } \
2094e2b911c5SDarrick J. Wong static inline void ext4_clear_feature_##name(struct super_block *sb) \
2095e2b911c5SDarrick J. Wong { \
2096e2b911c5SDarrick J. Wong 	EXT4_SB(sb)->s_es->s_feature_incompat &= \
2097e2b911c5SDarrick J. Wong 		~cpu_to_le32(EXT4_FEATURE_INCOMPAT_##flagname); \
2098e2b911c5SDarrick J. Wong }
2099e2b911c5SDarrick J. Wong 
EXT4_FEATURE_COMPAT_FUNCS(dir_prealloc,DIR_PREALLOC)2100e2b911c5SDarrick J. Wong EXT4_FEATURE_COMPAT_FUNCS(dir_prealloc,		DIR_PREALLOC)
2101e2b911c5SDarrick J. Wong EXT4_FEATURE_COMPAT_FUNCS(imagic_inodes,	IMAGIC_INODES)
2102e2b911c5SDarrick J. Wong EXT4_FEATURE_COMPAT_FUNCS(journal,		HAS_JOURNAL)
2103e2b911c5SDarrick J. Wong EXT4_FEATURE_COMPAT_FUNCS(xattr,		EXT_ATTR)
2104e2b911c5SDarrick J. Wong EXT4_FEATURE_COMPAT_FUNCS(resize_inode,		RESIZE_INODE)
2105e2b911c5SDarrick J. Wong EXT4_FEATURE_COMPAT_FUNCS(dir_index,		DIR_INDEX)
2106e2b911c5SDarrick J. Wong EXT4_FEATURE_COMPAT_FUNCS(sparse_super2,	SPARSE_SUPER2)
2107995a3ed6SHarshad Shirwadkar EXT4_FEATURE_COMPAT_FUNCS(fast_commit,		FAST_COMMIT)
2108b925acb8SEric Biggers EXT4_FEATURE_COMPAT_FUNCS(stable_inodes,	STABLE_INODES)
210902f310fcSJan Kara EXT4_FEATURE_COMPAT_FUNCS(orphan_file,		ORPHAN_FILE)
2110e2b911c5SDarrick J. Wong 
2111e2b911c5SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_FUNCS(sparse_super,	SPARSE_SUPER)
2112e2b911c5SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_FUNCS(large_file,	LARGE_FILE)
2113e2b911c5SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_FUNCS(btree_dir,		BTREE_DIR)
2114e2b911c5SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_FUNCS(huge_file,		HUGE_FILE)
2115e2b911c5SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_FUNCS(gdt_csum,		GDT_CSUM)
2116e2b911c5SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_FUNCS(dir_nlink,		DIR_NLINK)
2117e2b911c5SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_FUNCS(extra_isize,	EXTRA_ISIZE)
2118e2b911c5SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_FUNCS(quota,		QUOTA)
2119e2b911c5SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_FUNCS(bigalloc,		BIGALLOC)
2120e2b911c5SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_FUNCS(metadata_csum,	METADATA_CSUM)
2121e2b911c5SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_FUNCS(readonly,		READONLY)
2122e2b911c5SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_FUNCS(project,		PROJECT)
2123c93d8f88SEric Biggers EXT4_FEATURE_RO_COMPAT_FUNCS(verity,		VERITY)
212402f310fcSJan Kara EXT4_FEATURE_RO_COMPAT_FUNCS(orphan_present,	ORPHAN_PRESENT)
2125e2b911c5SDarrick J. Wong 
2126e2b911c5SDarrick J. Wong EXT4_FEATURE_INCOMPAT_FUNCS(compression,	COMPRESSION)
2127e2b911c5SDarrick J. Wong EXT4_FEATURE_INCOMPAT_FUNCS(filetype,		FILETYPE)
2128e2b911c5SDarrick J. Wong EXT4_FEATURE_INCOMPAT_FUNCS(journal_needs_recovery,	RECOVER)
2129e2b911c5SDarrick J. Wong EXT4_FEATURE_INCOMPAT_FUNCS(journal_dev,	JOURNAL_DEV)
2130e2b911c5SDarrick J. Wong EXT4_FEATURE_INCOMPAT_FUNCS(meta_bg,		META_BG)
2131e2b911c5SDarrick J. Wong EXT4_FEATURE_INCOMPAT_FUNCS(extents,		EXTENTS)
2132e2b911c5SDarrick J. Wong EXT4_FEATURE_INCOMPAT_FUNCS(64bit,		64BIT)
2133e2b911c5SDarrick J. Wong EXT4_FEATURE_INCOMPAT_FUNCS(mmp,		MMP)
2134e2b911c5SDarrick J. Wong EXT4_FEATURE_INCOMPAT_FUNCS(flex_bg,		FLEX_BG)
2135e2b911c5SDarrick J. Wong EXT4_FEATURE_INCOMPAT_FUNCS(ea_inode,		EA_INODE)
2136e2b911c5SDarrick J. Wong EXT4_FEATURE_INCOMPAT_FUNCS(dirdata,		DIRDATA)
2137e2b911c5SDarrick J. Wong EXT4_FEATURE_INCOMPAT_FUNCS(csum_seed,		CSUM_SEED)
2138e2b911c5SDarrick J. Wong EXT4_FEATURE_INCOMPAT_FUNCS(largedir,		LARGEDIR)
2139e2b911c5SDarrick J. Wong EXT4_FEATURE_INCOMPAT_FUNCS(inline_data,	INLINE_DATA)
2140e2b911c5SDarrick J. Wong EXT4_FEATURE_INCOMPAT_FUNCS(encrypt,		ENCRYPT)
2141c83ad55eSGabriel Krisman Bertazi EXT4_FEATURE_INCOMPAT_FUNCS(casefold,		CASEFOLD)
2142e2b911c5SDarrick J. Wong 
21432035e776STheodore Ts'o #define EXT2_FEATURE_COMPAT_SUPP	EXT4_FEATURE_COMPAT_EXT_ATTR
21442035e776STheodore Ts'o #define EXT2_FEATURE_INCOMPAT_SUPP	(EXT4_FEATURE_INCOMPAT_FILETYPE| \
21452035e776STheodore Ts'o 					 EXT4_FEATURE_INCOMPAT_META_BG)
21462035e776STheodore Ts'o #define EXT2_FEATURE_RO_COMPAT_SUPP	(EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER| \
21472035e776STheodore Ts'o 					 EXT4_FEATURE_RO_COMPAT_LARGE_FILE| \
21482035e776STheodore Ts'o 					 EXT4_FEATURE_RO_COMPAT_BTREE_DIR)
21492035e776STheodore Ts'o 
21502035e776STheodore Ts'o #define EXT3_FEATURE_COMPAT_SUPP	EXT4_FEATURE_COMPAT_EXT_ATTR
21512035e776STheodore Ts'o #define EXT3_FEATURE_INCOMPAT_SUPP	(EXT4_FEATURE_INCOMPAT_FILETYPE| \
21522035e776STheodore Ts'o 					 EXT4_FEATURE_INCOMPAT_RECOVER| \
21532035e776STheodore Ts'o 					 EXT4_FEATURE_INCOMPAT_META_BG)
21542035e776STheodore Ts'o #define EXT3_FEATURE_RO_COMPAT_SUPP	(EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER| \
21552035e776STheodore Ts'o 					 EXT4_FEATURE_RO_COMPAT_LARGE_FILE| \
21562035e776STheodore Ts'o 					 EXT4_FEATURE_RO_COMPAT_BTREE_DIR)
21572035e776STheodore Ts'o 
215802f310fcSJan Kara #define EXT4_FEATURE_COMPAT_SUPP	(EXT4_FEATURE_COMPAT_EXT_ATTR| \
215902f310fcSJan Kara 					 EXT4_FEATURE_COMPAT_ORPHAN_FILE)
21603dcf5451SChristoph Hellwig #define EXT4_FEATURE_INCOMPAT_SUPP	(EXT4_FEATURE_INCOMPAT_FILETYPE| \
21613dcf5451SChristoph Hellwig 					 EXT4_FEATURE_INCOMPAT_RECOVER| \
21623dcf5451SChristoph Hellwig 					 EXT4_FEATURE_INCOMPAT_META_BG| \
21633dcf5451SChristoph Hellwig 					 EXT4_FEATURE_INCOMPAT_EXTENTS| \
21643dcf5451SChristoph Hellwig 					 EXT4_FEATURE_INCOMPAT_64BIT| \
2165c5e06d10SJohann Lombardi 					 EXT4_FEATURE_INCOMPAT_FLEX_BG| \
2166e50e5129SAndreas Dilger 					 EXT4_FEATURE_INCOMPAT_EA_INODE| \
2167f08225d1STao Ma 					 EXT4_FEATURE_INCOMPAT_MMP | \
21686ddb2447STheodore Ts'o 					 EXT4_FEATURE_INCOMPAT_INLINE_DATA | \
21698c81bd8fSDarrick J. Wong 					 EXT4_FEATURE_INCOMPAT_ENCRYPT | \
2170c83ad55eSGabriel Krisman Bertazi 					 EXT4_FEATURE_INCOMPAT_CASEFOLD | \
2171e08ac99fSArtem Blagodarenko 					 EXT4_FEATURE_INCOMPAT_CSUM_SEED | \
2172e08ac99fSArtem Blagodarenko 					 EXT4_FEATURE_INCOMPAT_LARGEDIR)
21733dcf5451SChristoph Hellwig #define EXT4_FEATURE_RO_COMPAT_SUPP	(EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER| \
21743dcf5451SChristoph Hellwig 					 EXT4_FEATURE_RO_COMPAT_LARGE_FILE| \
21753dcf5451SChristoph Hellwig 					 EXT4_FEATURE_RO_COMPAT_GDT_CSUM| \
21763dcf5451SChristoph Hellwig 					 EXT4_FEATURE_RO_COMPAT_DIR_NLINK | \
21773dcf5451SChristoph Hellwig 					 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE | \
21783dcf5451SChristoph Hellwig 					 EXT4_FEATURE_RO_COMPAT_BTREE_DIR |\
21796f16b606STheodore Ts'o 					 EXT4_FEATURE_RO_COMPAT_HUGE_FILE |\
2180e93376c2SDarrick J. Wong 					 EXT4_FEATURE_RO_COMPAT_BIGALLOC |\
21817c319d32SAditya Kali 					 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM|\
2182040cb378SLi Xi 					 EXT4_FEATURE_RO_COMPAT_QUOTA |\
2183c93d8f88SEric Biggers 					 EXT4_FEATURE_RO_COMPAT_PROJECT |\
218402f310fcSJan Kara 					 EXT4_FEATURE_RO_COMPAT_VERITY |\
218502f310fcSJan Kara 					 EXT4_FEATURE_RO_COMPAT_ORPHAN_PRESENT)
21863dcf5451SChristoph Hellwig 
2187e2b911c5SDarrick J. Wong #define EXTN_FEATURE_FUNCS(ver) \
2188e2b911c5SDarrick J. Wong static inline bool ext4_has_unknown_ext##ver##_compat_features(struct super_block *sb) \
2189e2b911c5SDarrick J. Wong { \
2190e2b911c5SDarrick J. Wong 	return ((EXT4_SB(sb)->s_es->s_feature_compat & \
2191e2b911c5SDarrick J. Wong 		cpu_to_le32(~EXT##ver##_FEATURE_COMPAT_SUPP)) != 0); \
2192e2b911c5SDarrick J. Wong } \
2193e2b911c5SDarrick J. Wong static inline bool ext4_has_unknown_ext##ver##_ro_compat_features(struct super_block *sb) \
2194e2b911c5SDarrick J. Wong { \
2195e2b911c5SDarrick J. Wong 	return ((EXT4_SB(sb)->s_es->s_feature_ro_compat & \
2196e2b911c5SDarrick J. Wong 		cpu_to_le32(~EXT##ver##_FEATURE_RO_COMPAT_SUPP)) != 0); \
2197e2b911c5SDarrick J. Wong } \
2198e2b911c5SDarrick J. Wong static inline bool ext4_has_unknown_ext##ver##_incompat_features(struct super_block *sb) \
2199e2b911c5SDarrick J. Wong { \
2200e2b911c5SDarrick J. Wong 	return ((EXT4_SB(sb)->s_es->s_feature_incompat & \
2201e2b911c5SDarrick J. Wong 		cpu_to_le32(~EXT##ver##_FEATURE_INCOMPAT_SUPP)) != 0); \
2202e2b911c5SDarrick J. Wong }
2203e2b911c5SDarrick J. Wong 
2204e2b911c5SDarrick J. Wong EXTN_FEATURE_FUNCS(2)
2205e2b911c5SDarrick J. Wong EXTN_FEATURE_FUNCS(3)
2206e2b911c5SDarrick J. Wong EXTN_FEATURE_FUNCS(4)
2207e2b911c5SDarrick J. Wong 
2208e2b911c5SDarrick J. Wong static inline bool ext4_has_compat_features(struct super_block *sb)
2209e2b911c5SDarrick J. Wong {
2210e2b911c5SDarrick J. Wong 	return (EXT4_SB(sb)->s_es->s_feature_compat != 0);
2211e2b911c5SDarrick J. Wong }
ext4_has_ro_compat_features(struct super_block * sb)2212e2b911c5SDarrick J. Wong static inline bool ext4_has_ro_compat_features(struct super_block *sb)
2213e2b911c5SDarrick J. Wong {
2214e2b911c5SDarrick J. Wong 	return (EXT4_SB(sb)->s_es->s_feature_ro_compat != 0);
2215e2b911c5SDarrick J. Wong }
ext4_has_incompat_features(struct super_block * sb)2216e2b911c5SDarrick J. Wong static inline bool ext4_has_incompat_features(struct super_block *sb)
2217e2b911c5SDarrick J. Wong {
2218e2b911c5SDarrick J. Wong 	return (EXT4_SB(sb)->s_es->s_feature_incompat != 0);
2219e2b911c5SDarrick J. Wong }
2220e2b911c5SDarrick J. Wong 
222125c6d98fSJan Kara extern int ext4_feature_set_ok(struct super_block *sb, int readonly);
222225c6d98fSJan Kara 
22233dcf5451SChristoph Hellwig /*
22249549a168STheodore Ts'o  * Superblock flags
22259549a168STheodore Ts'o  */
22269549a168STheodore Ts'o #define EXT4_FLAGS_RESIZING	0
22270db1ff22STheodore Ts'o #define EXT4_FLAGS_SHUTDOWN	1
2228a8ab6d38SIra Weiny #define EXT4_FLAGS_BDEV_IS_DAX	2
22290db1ff22STheodore Ts'o 
ext4_forced_shutdown(struct super_block * sb)2230eb8ab444SJan Kara static inline int ext4_forced_shutdown(struct super_block *sb)
22310db1ff22STheodore Ts'o {
2232eb8ab444SJan Kara 	return test_bit(EXT4_FLAGS_SHUTDOWN, &EXT4_SB(sb)->s_ext4_flags);
22330db1ff22STheodore Ts'o }
22349549a168STheodore Ts'o 
22359549a168STheodore Ts'o /*
22363dcf5451SChristoph Hellwig  * Default values for user and/or group using reserved blocks
22373dcf5451SChristoph Hellwig  */
22383dcf5451SChristoph Hellwig #define	EXT4_DEF_RESUID		0
22393dcf5451SChristoph Hellwig #define	EXT4_DEF_RESGID		0
22403dcf5451SChristoph Hellwig 
2241040cb378SLi Xi /*
2242040cb378SLi Xi  * Default project ID
2243040cb378SLi Xi  */
2244040cb378SLi Xi #define	EXT4_DEF_PROJID		0
2245040cb378SLi Xi 
2246240799cdSTheodore Ts'o #define EXT4_DEF_INODE_READAHEAD_BLKS	32
2247240799cdSTheodore Ts'o 
22483dcf5451SChristoph Hellwig /*
22493dcf5451SChristoph Hellwig  * Default mount options
22503dcf5451SChristoph Hellwig  */
22513dcf5451SChristoph Hellwig #define EXT4_DEFM_DEBUG		0x0001
22523dcf5451SChristoph Hellwig #define EXT4_DEFM_BSDGROUPS	0x0002
22533dcf5451SChristoph Hellwig #define EXT4_DEFM_XATTR_USER	0x0004
22543dcf5451SChristoph Hellwig #define EXT4_DEFM_ACL		0x0008
22553dcf5451SChristoph Hellwig #define EXT4_DEFM_UID16		0x0010
22563dcf5451SChristoph Hellwig #define EXT4_DEFM_JMODE		0x0060
22573dcf5451SChristoph Hellwig #define EXT4_DEFM_JMODE_DATA	0x0020
22583dcf5451SChristoph Hellwig #define EXT4_DEFM_JMODE_ORDERED	0x0040
22593dcf5451SChristoph Hellwig #define EXT4_DEFM_JMODE_WBACK	0x0060
22608b67f04aSTheodore Ts'o #define EXT4_DEFM_NOBARRIER	0x0100
22618b67f04aSTheodore Ts'o #define EXT4_DEFM_BLOCK_VALIDITY 0x0200
22628b67f04aSTheodore Ts'o #define EXT4_DEFM_DISCARD	0x0400
22638b67f04aSTheodore Ts'o #define EXT4_DEFM_NODELALLOC	0x0800
22643dcf5451SChristoph Hellwig 
22653dcf5451SChristoph Hellwig /*
226630773840STheodore Ts'o  * Default journal batch times
226730773840STheodore Ts'o  */
226830773840STheodore Ts'o #define EXT4_DEF_MIN_BATCH_TIME	0
226930773840STheodore Ts'o #define EXT4_DEF_MAX_BATCH_TIME	15000 /* 15ms */
227030773840STheodore Ts'o 
227130773840STheodore Ts'o /*
2272a4912123STheodore Ts'o  * Minimum number of groups in a flexgroup before we separate out
2273a4912123STheodore Ts'o  * directories into the first block group of a flexgroup
2274a4912123STheodore Ts'o  */
2275a4912123STheodore Ts'o #define EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME	4
2276a4912123STheodore Ts'o 
2277a4912123STheodore Ts'o /*
22783dcf5451SChristoph Hellwig  * Structure of a directory entry
22793dcf5451SChristoph Hellwig  */
22803dcf5451SChristoph Hellwig #define EXT4_NAME_LEN 255
2281c186f088SYe Bin /*
2282c186f088SYe Bin  * Base length of the ext4 directory entry excluding the name length
2283c186f088SYe Bin  */
2284c186f088SYe Bin #define EXT4_BASE_DIR_LEN (sizeof(struct ext4_dir_entry_2) - EXT4_NAME_LEN)
22853dcf5451SChristoph Hellwig 
22863dcf5451SChristoph Hellwig struct ext4_dir_entry {
22873dcf5451SChristoph Hellwig 	__le32	inode;			/* Inode number */
22883dcf5451SChristoph Hellwig 	__le16	rec_len;		/* Directory entry length */
22893dcf5451SChristoph Hellwig 	__le16	name_len;		/* Name length */
22903dcf5451SChristoph Hellwig 	char	name[EXT4_NAME_LEN];	/* File name */
22913dcf5451SChristoph Hellwig };
22923dcf5451SChristoph Hellwig 
2293471fbbeaSDaniel Rosenberg 
2294471fbbeaSDaniel Rosenberg /*
2295471fbbeaSDaniel Rosenberg  * Encrypted Casefolded entries require saving the hash on disk. This structure
2296471fbbeaSDaniel Rosenberg  * followed ext4_dir_entry_2's name[name_len] at the next 4 byte aligned
2297471fbbeaSDaniel Rosenberg  * boundary.
2298471fbbeaSDaniel Rosenberg  */
2299471fbbeaSDaniel Rosenberg struct ext4_dir_entry_hash {
2300471fbbeaSDaniel Rosenberg 	__le32 hash;
2301471fbbeaSDaniel Rosenberg 	__le32 minor_hash;
2302471fbbeaSDaniel Rosenberg };
2303471fbbeaSDaniel Rosenberg 
23043dcf5451SChristoph Hellwig /*
23053dcf5451SChristoph Hellwig  * The new version of the directory entry.  Since EXT4 structures are
23063dcf5451SChristoph Hellwig  * stored in intel byte order, and the name_len field could never be
23073dcf5451SChristoph Hellwig  * bigger than 255 chars, it's safe to reclaim the extra byte for the
23083dcf5451SChristoph Hellwig  * file_type field.
23093dcf5451SChristoph Hellwig  */
23103dcf5451SChristoph Hellwig struct ext4_dir_entry_2 {
23113dcf5451SChristoph Hellwig 	__le32	inode;			/* Inode number */
23123dcf5451SChristoph Hellwig 	__le16	rec_len;		/* Directory entry length */
23133dcf5451SChristoph Hellwig 	__u8	name_len;		/* Name length */
23149f364e1dSJonathan Grant 	__u8	file_type;		/* See file type macros EXT4_FT_* below */
23153dcf5451SChristoph Hellwig 	char	name[EXT4_NAME_LEN];	/* File name */
23163dcf5451SChristoph Hellwig };
23173dcf5451SChristoph Hellwig 
23183dcf5451SChristoph Hellwig /*
2319471fbbeaSDaniel Rosenberg  * Access the hashes at the end of ext4_dir_entry_2
2320471fbbeaSDaniel Rosenberg  */
2321471fbbeaSDaniel Rosenberg #define EXT4_DIRENT_HASHES(entry) \
2322471fbbeaSDaniel Rosenberg 	((struct ext4_dir_entry_hash *) \
2323471fbbeaSDaniel Rosenberg 		(((void *)(entry)) + \
2324471fbbeaSDaniel Rosenberg 		((8 + (entry)->name_len + EXT4_DIR_ROUND) & ~EXT4_DIR_ROUND)))
2325471fbbeaSDaniel Rosenberg #define EXT4_DIRENT_HASH(entry) le32_to_cpu(EXT4_DIRENT_HASHES(de)->hash)
2326471fbbeaSDaniel Rosenberg #define EXT4_DIRENT_MINOR_HASH(entry) \
2327471fbbeaSDaniel Rosenberg 		le32_to_cpu(EXT4_DIRENT_HASHES(de)->minor_hash)
2328471fbbeaSDaniel Rosenberg 
ext4_hash_in_dirent(const struct inode * inode)2329471fbbeaSDaniel Rosenberg static inline bool ext4_hash_in_dirent(const struct inode *inode)
2330471fbbeaSDaniel Rosenberg {
2331471fbbeaSDaniel Rosenberg 	return IS_CASEFOLDED(inode) && IS_ENCRYPTED(inode);
2332471fbbeaSDaniel Rosenberg }
2333471fbbeaSDaniel Rosenberg 
2334471fbbeaSDaniel Rosenberg /*
2335e6153918SDarrick J. Wong  * This is a bogus directory entry at the end of each leaf block that
2336e6153918SDarrick J. Wong  * records checksums.
2337e6153918SDarrick J. Wong  */
2338e6153918SDarrick J. Wong struct ext4_dir_entry_tail {
2339e6153918SDarrick J. Wong 	__le32	det_reserved_zero1;	/* Pretend to be unused */
2340e6153918SDarrick J. Wong 	__le16	det_rec_len;		/* 12 */
2341e6153918SDarrick J. Wong 	__u8	det_reserved_zero2;	/* Zero name length */
2342e6153918SDarrick J. Wong 	__u8	det_reserved_ft;	/* 0xDE, fake file type */
2343e6153918SDarrick J. Wong 	__le32	det_checksum;		/* crc32c(uuid+inum+dirblock) */
2344e6153918SDarrick J. Wong };
2345e6153918SDarrick J. Wong 
23463c47d541STao Ma #define EXT4_DIRENT_TAIL(block, blocksize) \
23473c47d541STao Ma 	((struct ext4_dir_entry_tail *)(((void *)(block)) + \
23483c47d541STao Ma 					((blocksize) - \
23493c47d541STao Ma 					 sizeof(struct ext4_dir_entry_tail))))
23503c47d541STao Ma 
2351e6153918SDarrick J. Wong /*
23523dcf5451SChristoph Hellwig  * Ext4 directory file types.  Only the low 3 bits are used.  The
23533dcf5451SChristoph Hellwig  * other bits are reserved for now.
23543dcf5451SChristoph Hellwig  */
23553dcf5451SChristoph Hellwig #define EXT4_FT_UNKNOWN		0
23563dcf5451SChristoph Hellwig #define EXT4_FT_REG_FILE	1
23573dcf5451SChristoph Hellwig #define EXT4_FT_DIR		2
23583dcf5451SChristoph Hellwig #define EXT4_FT_CHRDEV		3
23593dcf5451SChristoph Hellwig #define EXT4_FT_BLKDEV		4
23603dcf5451SChristoph Hellwig #define EXT4_FT_FIFO		5
23613dcf5451SChristoph Hellwig #define EXT4_FT_SOCK		6
23623dcf5451SChristoph Hellwig #define EXT4_FT_SYMLINK		7
23633dcf5451SChristoph Hellwig 
23643dcf5451SChristoph Hellwig #define EXT4_FT_MAX		8
23653dcf5451SChristoph Hellwig 
2366e6153918SDarrick J. Wong #define EXT4_FT_DIR_CSUM	0xDE
2367e6153918SDarrick J. Wong 
23683dcf5451SChristoph Hellwig /*
23693dcf5451SChristoph Hellwig  * EXT4_DIR_PAD defines the directory entries boundaries
23703dcf5451SChristoph Hellwig  *
23713dcf5451SChristoph Hellwig  * NOTE: It must be a multiple of 4
23723dcf5451SChristoph Hellwig  */
23733dcf5451SChristoph Hellwig #define EXT4_DIR_PAD			4
23743dcf5451SChristoph Hellwig #define EXT4_DIR_ROUND			(EXT4_DIR_PAD - 1)
23753dcf5451SChristoph Hellwig #define EXT4_MAX_REC_LEN		((1<<16)-1)
23763dcf5451SChristoph Hellwig 
23773dcf5451SChristoph Hellwig /*
2378471fbbeaSDaniel Rosenberg  * The rec_len is dependent on the type of directory. Directories that are
2379471fbbeaSDaniel Rosenberg  * casefolded and encrypted need to store the hash as well, so we add room for
2380471fbbeaSDaniel Rosenberg  * ext4_extended_dir_entry_2. For all entries related to '.' or '..' you should
2381471fbbeaSDaniel Rosenberg  * pass NULL for dir, as those entries do not use the extra fields.
2382471fbbeaSDaniel Rosenberg  */
ext4_dir_rec_len(__u8 name_len,const struct inode * dir)2383471fbbeaSDaniel Rosenberg static inline unsigned int ext4_dir_rec_len(__u8 name_len,
2384471fbbeaSDaniel Rosenberg 						const struct inode *dir)
2385471fbbeaSDaniel Rosenberg {
2386471fbbeaSDaniel Rosenberg 	int rec_len = (name_len + 8 + EXT4_DIR_ROUND);
2387471fbbeaSDaniel Rosenberg 
2388471fbbeaSDaniel Rosenberg 	if (dir && ext4_hash_in_dirent(dir))
2389471fbbeaSDaniel Rosenberg 		rec_len += sizeof(struct ext4_dir_entry_hash);
2390471fbbeaSDaniel Rosenberg 	return (rec_len & ~EXT4_DIR_ROUND);
2391471fbbeaSDaniel Rosenberg }
2392471fbbeaSDaniel Rosenberg 
2393471fbbeaSDaniel Rosenberg /*
23940cfc9255SEric Sandeen  * If we ever get support for fs block sizes > page_size, we'll need
23950cfc9255SEric Sandeen  * to remove the #if statements in the next two functions...
23960cfc9255SEric Sandeen  */
23970cfc9255SEric Sandeen static inline unsigned int
ext4_rec_len_from_disk(__le16 dlen,unsigned blocksize)23980cfc9255SEric Sandeen ext4_rec_len_from_disk(__le16 dlen, unsigned blocksize)
23990cfc9255SEric Sandeen {
24000cfc9255SEric Sandeen 	unsigned len = le16_to_cpu(dlen);
24010cfc9255SEric Sandeen 
2402ea1754a0SKirill A. Shutemov #if (PAGE_SIZE >= 65536)
24030cfc9255SEric Sandeen 	if (len == EXT4_MAX_REC_LEN || len == 0)
24040cfc9255SEric Sandeen 		return blocksize;
24050cfc9255SEric Sandeen 	return (len & 65532) | ((len & 3) << 16);
24060cfc9255SEric Sandeen #else
24070cfc9255SEric Sandeen 	return len;
24080cfc9255SEric Sandeen #endif
24090cfc9255SEric Sandeen }
24100cfc9255SEric Sandeen 
ext4_rec_len_to_disk(unsigned len,unsigned blocksize)24110cfc9255SEric Sandeen static inline __le16 ext4_rec_len_to_disk(unsigned len, unsigned blocksize)
24120cfc9255SEric Sandeen {
2413a2e3965dSxu xin 	BUG_ON((len > blocksize) || (blocksize > (1 << 18)) || (len & 3));
2414ea1754a0SKirill A. Shutemov #if (PAGE_SIZE >= 65536)
24150cfc9255SEric Sandeen 	if (len < 65536)
24160cfc9255SEric Sandeen 		return cpu_to_le16(len);
24170cfc9255SEric Sandeen 	if (len == blocksize) {
24180cfc9255SEric Sandeen 		if (blocksize == 65536)
24190cfc9255SEric Sandeen 			return cpu_to_le16(EXT4_MAX_REC_LEN);
24200cfc9255SEric Sandeen 		else
24210cfc9255SEric Sandeen 			return cpu_to_le16(0);
24220cfc9255SEric Sandeen 	}
24230cfc9255SEric Sandeen 	return cpu_to_le16((len & 65532) | ((len >> 16) & 3));
24240cfc9255SEric Sandeen #else
24250cfc9255SEric Sandeen 	return cpu_to_le16(len);
24260cfc9255SEric Sandeen #endif
24270cfc9255SEric Sandeen }
24280cfc9255SEric Sandeen 
24290cfc9255SEric Sandeen /*
24303dcf5451SChristoph Hellwig  * Hash Tree Directory indexing
24313dcf5451SChristoph Hellwig  * (c) Daniel Phillips, 2001
24323dcf5451SChristoph Hellwig  */
24333dcf5451SChristoph Hellwig 
2434e2b911c5SDarrick J. Wong #define is_dx(dir) (ext4_has_feature_dir_index((dir)->i_sb) && \
243512e9b892SDmitry Monakhov 		    ext4_test_inode_flag((dir), EXT4_INODE_INDEX))
2436c7414892SAndreas Dilger #define EXT4_DIR_LINK_MAX(dir) unlikely((dir)->i_nlink >= EXT4_LINK_MAX && \
2437c7414892SAndreas Dilger 		    !(ext4_has_feature_dir_nlink((dir)->i_sb) && is_dx(dir)))
24383dcf5451SChristoph Hellwig #define EXT4_DIR_LINK_EMPTY(dir) ((dir)->i_nlink == 2 || (dir)->i_nlink == 1)
24393dcf5451SChristoph Hellwig 
24403dcf5451SChristoph Hellwig /* Legal values for the dx_root hash_version field: */
24413dcf5451SChristoph Hellwig 
24423dcf5451SChristoph Hellwig #define DX_HASH_LEGACY			0
24433dcf5451SChristoph Hellwig #define DX_HASH_HALF_MD4		1
24443dcf5451SChristoph Hellwig #define DX_HASH_TEA			2
2445f99b2589STheodore Ts'o #define DX_HASH_LEGACY_UNSIGNED		3
2446f99b2589STheodore Ts'o #define DX_HASH_HALF_MD4_UNSIGNED	4
2447f99b2589STheodore Ts'o #define DX_HASH_TEA_UNSIGNED		5
2448471fbbeaSDaniel Rosenberg #define DX_HASH_SIPHASH			6
24493dcf5451SChristoph Hellwig 
ext4_chksum(struct ext4_sb_info * sbi,u32 crc,const void * address,unsigned int length)24500441984aSDarrick J. Wong static inline u32 ext4_chksum(struct ext4_sb_info *sbi, u32 crc,
24510441984aSDarrick J. Wong 			      const void *address, unsigned int length)
24520441984aSDarrick J. Wong {
24530441984aSDarrick J. Wong 	struct {
24540441984aSDarrick J. Wong 		struct shash_desc shash;
24553108b54bSTheodore Ts'o 		char ctx[4];
24560441984aSDarrick J. Wong 	} desc;
24570441984aSDarrick J. Wong 
24583108b54bSTheodore Ts'o 	BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver)!=sizeof(desc.ctx));
24593108b54bSTheodore Ts'o 
24600441984aSDarrick J. Wong 	desc.shash.tfm = sbi->s_chksum_driver;
24610441984aSDarrick J. Wong 	*(u32 *)desc.ctx = crc;
24620441984aSDarrick J. Wong 
2463e7093f0dSPetros Koutoupis 	BUG_ON(crypto_shash_update(&desc.shash, address, length));
24640441984aSDarrick J. Wong 
24650441984aSDarrick J. Wong 	return *(u32 *)desc.ctx;
24660441984aSDarrick J. Wong }
24670441984aSDarrick J. Wong 
24683dcf5451SChristoph Hellwig #ifdef __KERNEL__
24693dcf5451SChristoph Hellwig 
24703dcf5451SChristoph Hellwig /* hash info structure used by the directory hash */
24713dcf5451SChristoph Hellwig struct dx_hash_info
24723dcf5451SChristoph Hellwig {
24733dcf5451SChristoph Hellwig 	u32		hash;
24743dcf5451SChristoph Hellwig 	u32		minor_hash;
24753dcf5451SChristoph Hellwig 	int		hash_version;
24763dcf5451SChristoph Hellwig 	u32		*seed;
24773dcf5451SChristoph Hellwig };
24783dcf5451SChristoph Hellwig 
2479d1f5273eSFan Yong 
2480d1f5273eSFan Yong /* 32 and 64 bit signed EOF for dx directories */
2481d1f5273eSFan Yong #define EXT4_HTREE_EOF_32BIT   ((1UL  << (32 - 1)) - 1)
2482d1f5273eSFan Yong #define EXT4_HTREE_EOF_64BIT   ((1ULL << (64 - 1)) - 1)
2483d1f5273eSFan Yong 
24843dcf5451SChristoph Hellwig 
24853dcf5451SChristoph Hellwig /*
24863dcf5451SChristoph Hellwig  * Control parameters used by ext4_htree_next_block
24873dcf5451SChristoph Hellwig  */
24883dcf5451SChristoph Hellwig #define HASH_NB_ALWAYS		1
24893dcf5451SChristoph Hellwig 
24905b643f9cSTheodore Ts'o struct ext4_filename {
24915b643f9cSTheodore Ts'o 	const struct qstr *usr_fname;
2492a7550b30SJaegeuk Kim 	struct fscrypt_str disk_name;
24935b643f9cSTheodore Ts'o 	struct dx_hash_info hinfo;
2494643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
2495a7550b30SJaegeuk Kim 	struct fscrypt_str crypto_buf;
24965b643f9cSTheodore Ts'o #endif
24975298d4bfSChristoph Hellwig #if IS_ENABLED(CONFIG_UNICODE)
24983ae72562SGabriel Krisman Bertazi 	struct fscrypt_str cf_name;
24993ae72562SGabriel Krisman Bertazi #endif
25005b643f9cSTheodore Ts'o };
25015b643f9cSTheodore Ts'o 
25025b643f9cSTheodore Ts'o #define fname_name(p) ((p)->disk_name.name)
2503471fbbeaSDaniel Rosenberg #define fname_usr_name(p) ((p)->usr_fname->name)
25045b643f9cSTheodore Ts'o #define fname_len(p)  ((p)->disk_name.len)
25053dcf5451SChristoph Hellwig 
25063dcf5451SChristoph Hellwig /*
25073dcf5451SChristoph Hellwig  * Describe an inode's exact location on disk and in memory
25083dcf5451SChristoph Hellwig  */
25093dcf5451SChristoph Hellwig struct ext4_iloc
25103dcf5451SChristoph Hellwig {
25113dcf5451SChristoph Hellwig 	struct buffer_head *bh;
25123dcf5451SChristoph Hellwig 	unsigned long offset;
25133dcf5451SChristoph Hellwig 	ext4_group_t block_group;
25143dcf5451SChristoph Hellwig };
25153dcf5451SChristoph Hellwig 
ext4_raw_inode(struct ext4_iloc * iloc)25163dcf5451SChristoph Hellwig static inline struct ext4_inode *ext4_raw_inode(struct ext4_iloc *iloc)
25173dcf5451SChristoph Hellwig {
25183dcf5451SChristoph Hellwig 	return (struct ext4_inode *) (iloc->bh->b_data + iloc->offset);
25193dcf5451SChristoph Hellwig }
25203dcf5451SChristoph Hellwig 
ext4_is_quota_file(struct inode * inode)2521dec214d0STahsin Erdogan static inline bool ext4_is_quota_file(struct inode *inode)
2522dec214d0STahsin Erdogan {
2523dec214d0STahsin Erdogan 	return IS_NOQUOTA(inode) &&
2524dec214d0STahsin Erdogan 	       !(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL);
2525dec214d0STahsin Erdogan }
252602749a4cSTahsin Erdogan 
25273dcf5451SChristoph Hellwig /*
25283dcf5451SChristoph Hellwig  * This structure is stuffed into the struct file's private_data field
25293dcf5451SChristoph Hellwig  * for directories.  It is where we put information so that we can do
25303dcf5451SChristoph Hellwig  * readdir operations in hash tree order.
25313dcf5451SChristoph Hellwig  */
25323dcf5451SChristoph Hellwig struct dir_private_info {
25333dcf5451SChristoph Hellwig 	struct rb_root	root;
25343dcf5451SChristoph Hellwig 	struct rb_node	*curr_node;
25353dcf5451SChristoph Hellwig 	struct fname	*extra_fname;
25363dcf5451SChristoph Hellwig 	loff_t		last_pos;
25373dcf5451SChristoph Hellwig 	__u32		curr_hash;
25383dcf5451SChristoph Hellwig 	__u32		curr_minor_hash;
25393dcf5451SChristoph Hellwig 	__u32		next_hash;
25403dcf5451SChristoph Hellwig };
25413dcf5451SChristoph Hellwig 
25423dcf5451SChristoph Hellwig /* calculate the first block number of the group */
25433dcf5451SChristoph Hellwig static inline ext4_fsblk_t
ext4_group_first_block_no(struct super_block * sb,ext4_group_t group_no)25443dcf5451SChristoph Hellwig ext4_group_first_block_no(struct super_block *sb, ext4_group_t group_no)
25453dcf5451SChristoph Hellwig {
25463dcf5451SChristoph Hellwig 	return group_no * (ext4_fsblk_t)EXT4_BLOCKS_PER_GROUP(sb) +
25473dcf5451SChristoph Hellwig 		le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
25483dcf5451SChristoph Hellwig }
25493dcf5451SChristoph Hellwig 
25503dcf5451SChristoph Hellwig /*
25513dcf5451SChristoph Hellwig  * Special error return code only used by dx_probe() and its callers.
25523dcf5451SChristoph Hellwig  */
255336de9286STheodore Ts'o #define ERR_BAD_DX_DIR	(-(MAX_ERRNO - 1))
25543dcf5451SChristoph Hellwig 
2555e08ac99fSArtem Blagodarenko /* htree levels for ext4 */
2556e08ac99fSArtem Blagodarenko #define	EXT4_HTREE_LEVEL_COMPAT	2
2557e08ac99fSArtem Blagodarenko #define	EXT4_HTREE_LEVEL	3
2558e08ac99fSArtem Blagodarenko 
ext4_dir_htree_level(struct super_block * sb)2559e08ac99fSArtem Blagodarenko static inline int ext4_dir_htree_level(struct super_block *sb)
2560e08ac99fSArtem Blagodarenko {
2561e08ac99fSArtem Blagodarenko 	return ext4_has_feature_largedir(sb) ?
2562e08ac99fSArtem Blagodarenko 		EXT4_HTREE_LEVEL : EXT4_HTREE_LEVEL_COMPAT;
2563e08ac99fSArtem Blagodarenko }
2564e08ac99fSArtem Blagodarenko 
25653dcf5451SChristoph Hellwig /*
2566bfff6873SLukas Czerner  * Timeout and state flag for lazy initialization inode thread.
2567bfff6873SLukas Czerner  */
2568bfff6873SLukas Czerner #define EXT4_DEF_LI_WAIT_MULT			10
2569bfff6873SLukas Czerner #define EXT4_DEF_LI_MAX_START_DELAY		5
2570bfff6873SLukas Czerner #define EXT4_LAZYINIT_QUIT			0x0001
2571bfff6873SLukas Czerner #define EXT4_LAZYINIT_RUNNING			0x0002
2572bfff6873SLukas Czerner 
2573bfff6873SLukas Czerner /*
2574bfff6873SLukas Czerner  * Lazy inode table initialization info
2575bfff6873SLukas Czerner  */
2576bfff6873SLukas Czerner struct ext4_lazy_init {
2577bfff6873SLukas Czerner 	unsigned long		li_state;
2578bfff6873SLukas Czerner 	struct list_head	li_request_list;
2579bfff6873SLukas Czerner 	struct mutex		li_list_mtx;
2580bfff6873SLukas Czerner };
2581bfff6873SLukas Czerner 
25823d392b26STheodore Ts'o enum ext4_li_mode {
25833d392b26STheodore Ts'o 	EXT4_LI_MODE_PREFETCH_BBITMAP,
25843d392b26STheodore Ts'o 	EXT4_LI_MODE_ITABLE,
25853d392b26STheodore Ts'o };
25863d392b26STheodore Ts'o 
2587bfff6873SLukas Czerner struct ext4_li_request {
2588bfff6873SLukas Czerner 	struct super_block	*lr_super;
25893d392b26STheodore Ts'o 	enum ext4_li_mode	lr_mode;
25903d392b26STheodore Ts'o 	ext4_group_t		lr_first_not_zeroed;
2591bfff6873SLukas Czerner 	ext4_group_t		lr_next_group;
2592bfff6873SLukas Czerner 	struct list_head	lr_request;
2593bfff6873SLukas Czerner 	unsigned long		lr_next_sched;
2594bfff6873SLukas Czerner 	unsigned long		lr_timeout;
2595bfff6873SLukas Czerner };
2596bfff6873SLukas Czerner 
2597857ac889SLukas Czerner struct ext4_features {
2598857ac889SLukas Czerner 	struct kobject f_kobj;
2599857ac889SLukas Czerner 	struct completion f_kobj_unregister;
2600857ac889SLukas Czerner };
2601857ac889SLukas Czerner 
2602bfff6873SLukas Czerner /*
2603c5e06d10SJohann Lombardi  * This structure will be used for multiple mount protection. It will be
2604c5e06d10SJohann Lombardi  * written into the block number saved in the s_mmp_block field in the
2605c5e06d10SJohann Lombardi  * superblock. Programs that check MMP should assume that if
2606c5e06d10SJohann Lombardi  * SEQ_FSCK (or any unknown code above SEQ_MAX) is present then it is NOT safe
2607c5e06d10SJohann Lombardi  * to use the filesystem, regardless of how old the timestamp is.
2608c5e06d10SJohann Lombardi  */
2609c5e06d10SJohann Lombardi #define EXT4_MMP_MAGIC     0x004D4D50U /* ASCII for MMP */
2610c5e06d10SJohann Lombardi #define EXT4_MMP_SEQ_CLEAN 0xFF4D4D50U /* mmp_seq value for clean unmount */
2611c5e06d10SJohann Lombardi #define EXT4_MMP_SEQ_FSCK  0xE24D4D50U /* mmp_seq value when being fscked */
2612c5e06d10SJohann Lombardi #define EXT4_MMP_SEQ_MAX   0xE24D4D4FU /* maximum valid mmp_seq value */
2613c5e06d10SJohann Lombardi 
2614c5e06d10SJohann Lombardi struct mmp_struct {
2615c5e06d10SJohann Lombardi 	__le32	mmp_magic;		/* Magic number for MMP */
2616c5e06d10SJohann Lombardi 	__le32	mmp_seq;		/* Sequence no. updated periodically */
2617c5e06d10SJohann Lombardi 
2618c5e06d10SJohann Lombardi 	/*
2619c5e06d10SJohann Lombardi 	 * mmp_time, mmp_nodename & mmp_bdevname are only used for information
2620c5e06d10SJohann Lombardi 	 * purposes and do not affect the correctness of the algorithm
2621c5e06d10SJohann Lombardi 	 */
2622c5e06d10SJohann Lombardi 	__le64	mmp_time;		/* Time last updated */
2623c5e06d10SJohann Lombardi 	char	mmp_nodename[64];	/* Node which last updated MMP block */
2624c5e06d10SJohann Lombardi 	char	mmp_bdevname[32];	/* Bdev which last updated MMP block */
2625c5e06d10SJohann Lombardi 
2626c5e06d10SJohann Lombardi 	/*
2627c5e06d10SJohann Lombardi 	 * mmp_check_interval is used to verify if the MMP block has been
2628c5e06d10SJohann Lombardi 	 * updated on the block device. The value is updated based on the
2629c5e06d10SJohann Lombardi 	 * maximum time to write the MMP block during an update cycle.
2630c5e06d10SJohann Lombardi 	 */
2631c5e06d10SJohann Lombardi 	__le16	mmp_check_interval;
2632c5e06d10SJohann Lombardi 
2633c5e06d10SJohann Lombardi 	__le16	mmp_pad1;
2634e6153918SDarrick J. Wong 	__le32	mmp_pad2[226];
2635e6153918SDarrick J. Wong 	__le32	mmp_checksum;		/* crc32c(uuid+mmp_block) */
2636c5e06d10SJohann Lombardi };
2637c5e06d10SJohann Lombardi 
2638c5e06d10SJohann Lombardi /* arguments passed to the mmp thread */
2639c5e06d10SJohann Lombardi struct mmpd_data {
2640c5e06d10SJohann Lombardi 	struct buffer_head *bh; /* bh from initial read_mmp_block() */
2641c5e06d10SJohann Lombardi 	struct super_block *sb;  /* super block of the fs */
2642c5e06d10SJohann Lombardi };
2643c5e06d10SJohann Lombardi 
2644c5e06d10SJohann Lombardi /*
2645c5e06d10SJohann Lombardi  * Check interval multiplier
2646c5e06d10SJohann Lombardi  * The MMP block is written every update interval and initially checked every
2647c5e06d10SJohann Lombardi  * update interval x the multiplier (the value is then adapted based on the
2648c5e06d10SJohann Lombardi  * write latency). The reason is that writes can be delayed under load and we
2649c5e06d10SJohann Lombardi  * don't want readers to incorrectly assume that the filesystem is no longer
2650c5e06d10SJohann Lombardi  * in use.
2651c5e06d10SJohann Lombardi  */
2652c5e06d10SJohann Lombardi #define EXT4_MMP_CHECK_MULT		2UL
2653c5e06d10SJohann Lombardi 
2654c5e06d10SJohann Lombardi /*
2655c5e06d10SJohann Lombardi  * Minimum interval for MMP checking in seconds.
2656c5e06d10SJohann Lombardi  */
2657c5e06d10SJohann Lombardi #define EXT4_MMP_MIN_CHECK_INTERVAL	5UL
2658c5e06d10SJohann Lombardi 
2659c5e06d10SJohann Lombardi /*
2660c5e06d10SJohann Lombardi  * Maximum interval for MMP checking in seconds.
2661c5e06d10SJohann Lombardi  */
2662c5e06d10SJohann Lombardi #define EXT4_MMP_MAX_CHECK_INTERVAL	300UL
2663c5e06d10SJohann Lombardi 
2664c5e06d10SJohann Lombardi /*
26653dcf5451SChristoph Hellwig  * Function prototypes
26663dcf5451SChristoph Hellwig  */
26673dcf5451SChristoph Hellwig 
26683dcf5451SChristoph Hellwig /*
26693dcf5451SChristoph Hellwig  * Ok, these declarations are also in <linux/kernel.h> but none of the
26703dcf5451SChristoph Hellwig  * ext4 source programs needs to include it so they are duplicated here.
26713dcf5451SChristoph Hellwig  */
26723dcf5451SChristoph Hellwig # define NORET_TYPE	/**/
26733dcf5451SChristoph Hellwig # define ATTRIB_NORET	__attribute__((noreturn))
26743dcf5451SChristoph Hellwig # define NORET_AND	noreturn,
26753dcf5451SChristoph Hellwig 
2676498e5f24STheodore Ts'o /* bitmap.c */
2677f6fb99caSTheodore Ts'o extern unsigned int ext4_count_free(char *bitmap, unsigned numchars);
26784fd873c8SKemeng Shi void ext4_inode_bitmap_csum_set(struct super_block *sb,
267941a246d1SDarrick J. Wong 				struct ext4_group_desc *gdp,
268041a246d1SDarrick J. Wong 				struct buffer_head *bh, int sz);
2681b83acc77SKemeng Shi int ext4_inode_bitmap_csum_verify(struct super_block *sb,
268241a246d1SDarrick J. Wong 				  struct ext4_group_desc *gdp,
268341a246d1SDarrick J. Wong 				  struct buffer_head *bh, int sz);
26841df9bde4SKemeng Shi void ext4_block_bitmap_csum_set(struct super_block *sb,
2685fa77dcfaSDarrick J. Wong 				struct ext4_group_desc *gdp,
268679f1ba49STao Ma 				struct buffer_head *bh);
268782483dfeSKemeng Shi int ext4_block_bitmap_csum_verify(struct super_block *sb,
2688fa77dcfaSDarrick J. Wong 				  struct ext4_group_desc *gdp,
268979f1ba49STao Ma 				  struct buffer_head *bh);
2690498e5f24STheodore Ts'o 
26913dcf5451SChristoph Hellwig /* balloc.c */
2692bd86298eSLukas Czerner extern void ext4_get_group_no_and_offset(struct super_block *sb,
2693bd86298eSLukas Czerner 					 ext4_fsblk_t blocknr,
2694bd86298eSLukas Czerner 					 ext4_group_t *blockgrpp,
2695bd86298eSLukas Czerner 					 ext4_grpblk_t *offsetp);
2696bd86298eSLukas Czerner extern ext4_group_t ext4_get_group_number(struct super_block *sb,
2697bd86298eSLukas Czerner 					  ext4_fsblk_t block);
2698bd86298eSLukas Czerner 
26993dcf5451SChristoph Hellwig extern int ext4_bg_has_super(struct super_block *sb, ext4_group_t group);
27003dcf5451SChristoph Hellwig extern unsigned long ext4_bg_num_gdb(struct super_block *sb,
27013dcf5451SChristoph Hellwig 			ext4_group_t group);
27027061eba7SAneesh Kumar K.V extern ext4_fsblk_t ext4_new_meta_blocks(handle_t *handle, struct inode *inode,
270355f020dbSAllison Henderson 					 ext4_fsblk_t goal,
270455f020dbSAllison Henderson 					 unsigned int flags,
270555f020dbSAllison Henderson 					 unsigned long *count,
270655f020dbSAllison Henderson 					 int *errp);
2707e7d5f315STheodore Ts'o extern int ext4_claim_free_clusters(struct ext4_sb_info *sbi,
2708e7d5f315STheodore Ts'o 				    s64 nclusters, unsigned int flags);
27095dee5437STheodore Ts'o extern ext4_fsblk_t ext4_count_free_clusters(struct super_block *);
27103dcf5451SChristoph Hellwig extern struct ext4_group_desc * ext4_get_group_desc(struct super_block * sb,
27113dcf5451SChristoph Hellwig 						    ext4_group_t block_group,
27123dcf5451SChristoph Hellwig 						    struct buffer_head ** bh);
27135354b2afSTheodore Ts'o extern struct ext4_group_info *ext4_get_group_info(struct super_block *sb,
27145354b2afSTheodore Ts'o 						   ext4_group_t group);
27153dcf5451SChristoph Hellwig extern int ext4_should_retry_alloc(struct super_block *sb, int *retries);
2716813e5727STheodore Ts'o 
2717813e5727STheodore Ts'o extern struct buffer_head *ext4_read_block_bitmap_nowait(struct super_block *sb,
2718cfd73237SAlex Zhuravlev 						ext4_group_t block_group,
2719cfd73237SAlex Zhuravlev 						bool ignore_locked);
2720813e5727STheodore Ts'o extern int ext4_wait_block_bitmap(struct super_block *sb,
2721813e5727STheodore Ts'o 				  ext4_group_t block_group,
2722813e5727STheodore Ts'o 				  struct buffer_head *bh);
2723813e5727STheodore Ts'o extern struct buffer_head *ext4_read_block_bitmap(struct super_block *sb,
2724bb23c20aSTheodore Ts'o 						  ext4_group_t block_group);
2725cff1dfd7STheodore Ts'o extern unsigned ext4_free_clusters_after_init(struct super_block *sb,
2726fd034a84STheodore Ts'o 					      ext4_group_t block_group,
2727fd034a84STheodore Ts'o 					      struct ext4_group_desc *gdp);
2728f86186b4SEric Sandeen ext4_fsblk_t ext4_inode_to_goal_block(struct inode *);
27293dcf5451SChristoph Hellwig 
27305298d4bfSChristoph Hellwig #if IS_ENABLED(CONFIG_UNICODE)
27311ae98e29SDaniel Rosenberg extern int ext4_fname_setup_ci_filename(struct inode *dir,
27323ae72562SGabriel Krisman Bertazi 					 const struct qstr *iname,
27331ae98e29SDaniel Rosenberg 					 struct ext4_filename *fname);
27343ae72562SGabriel Krisman Bertazi #endif
27353ae72562SGabriel Krisman Bertazi 
2736b1241c8eSRitesh Harjani /* ext4 encryption related stuff goes here crypto.c */
2737643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
2738b1241c8eSRitesh Harjani extern const struct fscrypt_operations ext4_cryptops;
2739b1241c8eSRitesh Harjani 
27403030b59cSRitesh Harjani int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname,
27413030b59cSRitesh Harjani 			      int lookup, struct ext4_filename *fname);
2742b01531dbSEric Biggers 
27433030b59cSRitesh Harjani int ext4_fname_prepare_lookup(struct inode *dir, struct dentry *dentry,
27443030b59cSRitesh Harjani 			      struct ext4_filename *fname);
2745b01531dbSEric Biggers 
27463030b59cSRitesh Harjani void ext4_fname_free_filename(struct ext4_filename *fname);
2747a7550b30SJaegeuk Kim 
274872f63f4aSRitesh Harjani int ext4_ioctl_get_encryption_pwsalt(struct file *filp, void __user *arg);
274972f63f4aSRitesh Harjani 
2750b01531dbSEric Biggers #else /* !CONFIG_FS_ENCRYPTION */
ext4_fname_setup_filename(struct inode * dir,const struct qstr * iname,int lookup,struct ext4_filename * fname)27515b643f9cSTheodore Ts'o static inline int ext4_fname_setup_filename(struct inode *dir,
27525b643f9cSTheodore Ts'o 					    const struct qstr *iname,
2753b01531dbSEric Biggers 					    int lookup,
2754b01531dbSEric Biggers 					    struct ext4_filename *fname)
27555b643f9cSTheodore Ts'o {
27561ae98e29SDaniel Rosenberg 	int err = 0;
27575b643f9cSTheodore Ts'o 	fname->usr_fname = iname;
27585b643f9cSTheodore Ts'o 	fname->disk_name.name = (unsigned char *) iname->name;
27595b643f9cSTheodore Ts'o 	fname->disk_name.len = iname->len;
27603ae72562SGabriel Krisman Bertazi 
27615298d4bfSChristoph Hellwig #if IS_ENABLED(CONFIG_UNICODE)
27621ae98e29SDaniel Rosenberg 	err = ext4_fname_setup_ci_filename(dir, iname, fname);
27633ae72562SGabriel Krisman Bertazi #endif
27643ae72562SGabriel Krisman Bertazi 
27651ae98e29SDaniel Rosenberg 	return err;
27665b643f9cSTheodore Ts'o }
2767a7550b30SJaegeuk Kim 
ext4_fname_prepare_lookup(struct inode * dir,struct dentry * dentry,struct ext4_filename * fname)2768b01531dbSEric Biggers static inline int ext4_fname_prepare_lookup(struct inode *dir,
2769b01531dbSEric Biggers 					    struct dentry *dentry,
2770b01531dbSEric Biggers 					    struct ext4_filename *fname)
2771b01531dbSEric Biggers {
2772b01531dbSEric Biggers 	return ext4_fname_setup_filename(dir, &dentry->d_name, 1, fname);
2773b01531dbSEric Biggers }
2774b01531dbSEric Biggers 
ext4_fname_free_filename(struct ext4_filename * fname)27753ae72562SGabriel Krisman Bertazi static inline void ext4_fname_free_filename(struct ext4_filename *fname)
27763ae72562SGabriel Krisman Bertazi {
27775298d4bfSChristoph Hellwig #if IS_ENABLED(CONFIG_UNICODE)
27783ae72562SGabriel Krisman Bertazi 	kfree(fname->cf_name.name);
27793ae72562SGabriel Krisman Bertazi 	fname->cf_name.name = NULL;
27803ae72562SGabriel Krisman Bertazi #endif
27813ae72562SGabriel Krisman Bertazi }
278272f63f4aSRitesh Harjani 
ext4_ioctl_get_encryption_pwsalt(struct file * filp,void __user * arg)278372f63f4aSRitesh Harjani static inline int ext4_ioctl_get_encryption_pwsalt(struct file *filp,
278472f63f4aSRitesh Harjani 						   void __user *arg)
278572f63f4aSRitesh Harjani {
278672f63f4aSRitesh Harjani 	return -EOPNOTSUPP;
278772f63f4aSRitesh Harjani }
2788b01531dbSEric Biggers #endif /* !CONFIG_FS_ENCRYPTION */
2789d5d0e8c7SMichael Halcrow 
27903dcf5451SChristoph Hellwig /* dir.c */
279160fd4da3STheodore Ts'o extern int __ext4_check_dir_entry(const char *, unsigned int, struct inode *,
2792f7c21177STheodore Ts'o 				  struct file *,
27933dcf5451SChristoph Hellwig 				  struct ext4_dir_entry_2 *,
2794226ba972STao Ma 				  struct buffer_head *, char *, int,
2795226ba972STao Ma 				  unsigned int);
2796226ba972STao Ma #define ext4_check_dir_entry(dir, filp, de, bh, buf, size, offset) \
2797f7c21177STheodore Ts'o 	unlikely(__ext4_check_dir_entry(__func__, __LINE__, (dir), (filp), \
2798226ba972STao Ma 				(de), (bh), (buf), (size), (offset)))
27993dcf5451SChristoph Hellwig extern int ext4_htree_store_dirent(struct file *dir_file, __u32 hash,
28003dcf5451SChristoph Hellwig 				__u32 minor_hash,
28012f61830aSTheodore Ts'o 				struct ext4_dir_entry_2 *dirent,
2802a7550b30SJaegeuk Kim 				struct fscrypt_str *ent_name);
28033dcf5451SChristoph Hellwig extern void ext4_htree_free_dir_info(struct dir_private_info *p);
2804978fef91STao Ma extern int ext4_find_dest_de(struct inode *dir, struct inode *inode,
2805978fef91STao Ma 			     struct buffer_head *bh,
2806978fef91STao Ma 			     void *buf, int buf_size,
28075b643f9cSTheodore Ts'o 			     struct ext4_filename *fname,
2808978fef91STao Ma 			     struct ext4_dir_entry_2 **dest_de);
2809471fbbeaSDaniel Rosenberg void ext4_insert_dentry(struct inode *dir, struct inode *inode,
2810978fef91STao Ma 			struct ext4_dir_entry_2 *de,
2811978fef91STao Ma 			int buf_size,
28125b643f9cSTheodore Ts'o 			struct ext4_filename *fname);
ext4_update_dx_flag(struct inode * inode)2813978fef91STao Ma static inline void ext4_update_dx_flag(struct inode *inode)
2814978fef91STao Ma {
2815f902b216SJan Kara 	if (!ext4_has_feature_dir_index(inode->i_sb) &&
2816f902b216SJan Kara 	    ext4_test_inode_flag(inode, EXT4_INODE_INDEX)) {
281748a34311SJan Kara 		/* ext4_iget() should have caught this... */
281848a34311SJan Kara 		WARN_ON_ONCE(ext4_has_feature_metadata_csum(inode->i_sb));
2819978fef91STao Ma 		ext4_clear_inode_flag(inode, EXT4_INODE_INDEX);
2820978fef91STao Ma 	}
282148a34311SJan Kara }
2822d6006186SEric Biggers static const unsigned char ext4_filetype_table[] = {
282365d165d9STao Ma 	DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
282465d165d9STao Ma };
282565d165d9STao Ma 
get_dtype(struct super_block * sb,int filetype)282665d165d9STao Ma static inline  unsigned char get_dtype(struct super_block *sb, int filetype)
282765d165d9STao Ma {
2828e2b911c5SDarrick J. Wong 	if (!ext4_has_feature_filetype(sb) || filetype >= EXT4_FT_MAX)
282965d165d9STao Ma 		return DT_UNKNOWN;
283065d165d9STao Ma 
283165d165d9STao Ma 	return ext4_filetype_table[filetype];
283265d165d9STao Ma }
283340b163f1SDarrick J. Wong extern int ext4_check_all_de(struct inode *dir, struct buffer_head *bh,
283440b163f1SDarrick J. Wong 			     void *buf, int buf_size);
28353dcf5451SChristoph Hellwig 
28363dcf5451SChristoph Hellwig /* fsync.c */
283702c24a82SJosef Bacik extern int ext4_sync_file(struct file *, loff_t, loff_t, int);
28383dcf5451SChristoph Hellwig 
28393dcf5451SChristoph Hellwig /* hash.c */
2840b886ee3eSGabriel Krisman Bertazi extern int ext4fs_dirhash(const struct inode *dir, const char *name, int len,
2841b886ee3eSGabriel Krisman Bertazi 			  struct dx_hash_info *hinfo);
28423dcf5451SChristoph Hellwig 
28433dcf5451SChristoph Hellwig /* ialloc.c */
28448016e29fSHarshad Shirwadkar extern int ext4_mark_inode_used(struct super_block *sb, int ino);
2845f2d40141SChristian Brauner extern struct inode *__ext4_new_inode(struct mnt_idmap *, handle_t *,
284614f3db55SChristian Brauner 				      struct inode *, umode_t,
28475cb81dabSDmitry Monakhov 				      const struct qstr *qstr, __u32 goal,
28481b917ed8STahsin Erdogan 				      uid_t *owner, __u32 i_flags,
28491b917ed8STahsin Erdogan 				      int handle_type, unsigned int line_no,
28501b917ed8STahsin Erdogan 				      int nblocks);
28511139575aSTheodore Ts'o 
28521b917ed8STahsin Erdogan #define ext4_new_inode(handle, dir, mode, qstr, goal, owner, i_flags)          \
2853f2d40141SChristian Brauner 	__ext4_new_inode(&nop_mnt_idmap, (handle), (dir), (mode), (qstr),      \
285414f3db55SChristian Brauner 			 (goal), (owner), i_flags, 0, 0, 0)
2855f2d40141SChristian Brauner #define ext4_new_inode_start_handle(idmap, dir, mode, qstr, goal, owner, \
28561139575aSTheodore Ts'o 				    type, nblocks)		    \
2857f2d40141SChristian Brauner 	__ext4_new_inode((idmap), NULL, (dir), (mode), (qstr), (goal), (owner), \
28581b917ed8STahsin Erdogan 			 0, (type), __LINE__, (nblocks))
28591139575aSTheodore Ts'o 
28601139575aSTheodore Ts'o 
28613dcf5451SChristoph Hellwig extern void ext4_free_inode(handle_t *, struct inode *);
28623dcf5451SChristoph Hellwig extern struct inode * ext4_orphan_get(struct super_block *, unsigned long);
28633dcf5451SChristoph Hellwig extern unsigned long ext4_count_free_inodes(struct super_block *);
28643dcf5451SChristoph Hellwig extern unsigned long ext4_count_dirs(struct super_block *);
286561d08673STheodore Ts'o extern void ext4_mark_bitmap_end(int start_bit, int end_bit, char *bitmap);
2866bfff6873SLukas Czerner extern int ext4_init_inode_table(struct super_block *sb,
2867bfff6873SLukas Czerner 				 ext4_group_t group, int barrier);
2868813e5727STheodore Ts'o extern void ext4_end_bitmap_read(struct buffer_head *bh, int uptodate);
28693dcf5451SChristoph Hellwig 
28706866d7b3SHarshad Shirwadkar /* fast_commit.c */
2871ce8c59d1SHarshad Shirwadkar int ext4_fc_info_show(struct seq_file *seq, void *v);
28726866d7b3SHarshad Shirwadkar void ext4_fc_init(struct super_block *sb, journal_t *journal);
2873aa75f4d3SHarshad Shirwadkar void ext4_fc_init_inode(struct inode *inode);
2874a80f7fcfSHarshad Shirwadkar void ext4_fc_track_range(handle_t *handle, struct inode *inode, ext4_lblk_t start,
2875aa75f4d3SHarshad Shirwadkar 			 ext4_lblk_t end);
2876a80f7fcfSHarshad Shirwadkar void __ext4_fc_track_unlink(handle_t *handle, struct inode *inode,
2877a80f7fcfSHarshad Shirwadkar 	struct dentry *dentry);
2878a80f7fcfSHarshad Shirwadkar void __ext4_fc_track_link(handle_t *handle, struct inode *inode,
2879a80f7fcfSHarshad Shirwadkar 	struct dentry *dentry);
2880a80f7fcfSHarshad Shirwadkar void ext4_fc_track_unlink(handle_t *handle, struct dentry *dentry);
2881a80f7fcfSHarshad Shirwadkar void ext4_fc_track_link(handle_t *handle, struct dentry *dentry);
28828210bb29SHarshad Shirwadkar void __ext4_fc_track_create(handle_t *handle, struct inode *inode,
28838210bb29SHarshad Shirwadkar 			    struct dentry *dentry);
2884a80f7fcfSHarshad Shirwadkar void ext4_fc_track_create(handle_t *handle, struct dentry *dentry);
2885a80f7fcfSHarshad Shirwadkar void ext4_fc_track_inode(handle_t *handle, struct inode *inode);
2886e85c81baSXin Yin void ext4_fc_mark_ineligible(struct super_block *sb, int reason, handle_t *handle);
2887aa75f4d3SHarshad Shirwadkar void ext4_fc_start_update(struct inode *inode);
2888aa75f4d3SHarshad Shirwadkar void ext4_fc_stop_update(struct inode *inode);
2889aa75f4d3SHarshad Shirwadkar void ext4_fc_del(struct inode *inode);
28908016e29fSHarshad Shirwadkar bool ext4_fc_replay_check_excluded(struct super_block *sb, ext4_fsblk_t block);
28918016e29fSHarshad Shirwadkar void ext4_fc_replay_cleanup(struct super_block *sb);
2892aa75f4d3SHarshad Shirwadkar int ext4_fc_commit(journal_t *journal, tid_t commit_tid);
2893aa75f4d3SHarshad Shirwadkar int __init ext4_fc_init_dentry_cache(void);
2894ab047d51SSebastian Andrzej Siewior void ext4_fc_destroy_dentry_cache(void);
2895599ea31dSXin Yin int ext4_fc_record_regions(struct super_block *sb, int ino,
2896599ea31dSXin Yin 			   ext4_lblk_t lblk, ext4_fsblk_t pblk,
2897599ea31dSXin Yin 			   int len, int replay);
2898aa75f4d3SHarshad Shirwadkar 
28993dcf5451SChristoph Hellwig /* mballoc.c */
2900247dbed8SChristoph Hellwig extern const struct seq_operations ext4_mb_seq_groups_ops;
2901f68f4063SHarshad Shirwadkar extern const struct seq_operations ext4_mb_seq_structs_summary_ops;
2902a6c75eafSHarshad Shirwadkar extern int ext4_seq_mb_stats_show(struct seq_file *seq, void *offset);
29039d99012fSAkira Fujita extern int ext4_mb_init(struct super_block *);
29043dcf5451SChristoph Hellwig extern int ext4_mb_release(struct super_block *);
29053dcf5451SChristoph Hellwig extern ext4_fsblk_t ext4_mb_new_blocks(handle_t *,
29063dcf5451SChristoph Hellwig 				struct ext4_allocation_request *, int *);
290727bc446eSbrookxu extern void ext4_discard_preallocations(struct inode *, unsigned int);
29085dabfc78STheodore Ts'o extern int __init ext4_init_mballoc(void);
29095dabfc78STheodore Ts'o extern void ext4_exit_mballoc(void);
29103d392b26STheodore Ts'o extern ext4_group_t ext4_mb_prefetch(struct super_block *sb,
29113d392b26STheodore Ts'o 				     ext4_group_t group,
29123d392b26STheodore Ts'o 				     unsigned int nr, int *cnt);
29133d392b26STheodore Ts'o extern void ext4_mb_prefetch_fini(struct super_block *sb, ext4_group_t group,
29143d392b26STheodore Ts'o 				  unsigned int nr);
29153d392b26STheodore Ts'o 
291644338711STheodore Ts'o extern void ext4_free_blocks(handle_t *handle, struct inode *inode,
2917e6362609STheodore Ts'o 			     struct buffer_head *bh, ext4_fsblk_t block,
2918e6362609STheodore Ts'o 			     unsigned long count, int flags);
291928623c2fSTheodore Ts'o extern int ext4_mb_alloc_groupinfo(struct super_block *sb,
292028623c2fSTheodore Ts'o 				   ext4_group_t ngroups);
2921920313a7SAneesh Kumar K.V extern int ext4_mb_add_groupinfo(struct super_block *sb,
29225f21b0e6SFrederic Bohe 		ext4_group_t i, struct ext4_group_desc *desc);
2923cc7365dfSYongqiang Yang extern int ext4_group_add_blocks(handle_t *handle, struct super_block *sb,
29242846e820SAmir Goldstein 				ext4_fsblk_t block, unsigned long count);
29257360d173SLukas Czerner extern int ext4_trim_fs(struct super_block *, struct fstrim_range *);
2926a0154344SDaeho Jeong extern void ext4_process_freed_data(struct super_block *sb, tid_t commit_tid);
29278016e29fSHarshad Shirwadkar extern void ext4_mb_mark_bb(struct super_block *sb, ext4_fsblk_t block,
29288016e29fSHarshad Shirwadkar 		       int len, int state);
ext4_mb_cr_expensive(enum criteria cr)2929304749c0SOjaswin Mujoo static inline bool ext4_mb_cr_expensive(enum criteria cr)
2930304749c0SOjaswin Mujoo {
2931304749c0SOjaswin Mujoo 	return cr >= CR_GOAL_LEN_SLOW;
2932304749c0SOjaswin Mujoo }
29337360d173SLukas Czerner 
29343dcf5451SChristoph Hellwig /* inode.c */
29358016e29fSHarshad Shirwadkar void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw,
29368016e29fSHarshad Shirwadkar 			 struct ext4_inode_info *ei);
2937f348c252STheodore Ts'o int ext4_inode_is_fast_symlink(struct inode *inode);
293810560082STheodore Ts'o struct buffer_head *ext4_getblk(handle_t *, struct inode *, ext4_lblk_t, int);
29391c215028STheodore Ts'o struct buffer_head *ext4_bread(handle_t *, struct inode *, ext4_lblk_t, int);
29409699d4f9STahsin Erdogan int ext4_bread_batch(struct inode *inode, ext4_lblk_t block, int bh_count,
29419699d4f9STahsin Erdogan 		     bool wait, struct buffer_head **bhs);
2942705965bdSJan Kara int ext4_get_block_unwritten(struct inode *inode, sector_t iblock,
2943f19d5870STao Ma 			     struct buffer_head *bh_result, int create);
29446873fa0dSEric Sandeen int ext4_get_block(struct inode *inode, sector_t iblock,
29456873fa0dSEric Sandeen 		   struct buffer_head *bh_result, int create);
29469c3569b5STao Ma int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
29479c3569b5STao Ma 			   struct buffer_head *bh, int create);
2948f19d5870STao Ma int ext4_walk_page_buffers(handle_t *handle,
2949188c299eSJan Kara 			   struct inode *inode,
2950f19d5870STao Ma 			   struct buffer_head *head,
2951f19d5870STao Ma 			   unsigned from,
2952f19d5870STao Ma 			   unsigned to,
2953f19d5870STao Ma 			   int *partial,
2954188c299eSJan Kara 			   int (*fn)(handle_t *handle, struct inode *inode,
2955f19d5870STao Ma 				     struct buffer_head *bh));
2956188c299eSJan Kara int do_journal_get_write_access(handle_t *handle, struct inode *inode,
2957f19d5870STao Ma 				struct buffer_head *bh);
29589c3569b5STao Ma #define FALL_BACK_TO_NONDELALLOC 1
29599c3569b5STao Ma #define CONVERT_INLINE_DATA	 2
29603dcf5451SChristoph Hellwig 
29618a363970STheodore Ts'o typedef enum {
29628a363970STheodore Ts'o 	EXT4_IGET_NORMAL =	0,
29638a363970STheodore Ts'o 	EXT4_IGET_SPECIAL =	0x0001, /* OK to iget a system inode */
296463b1e9bcSBaokun Li 	EXT4_IGET_HANDLE = 	0x0002,	/* Inode # is from a handle */
2965b3e6bcb9STheodore Ts'o 	EXT4_IGET_BAD =		0x0004, /* Allow to iget a bad inode */
2966b3e6bcb9STheodore Ts'o 	EXT4_IGET_EA_INODE =	0x0008	/* Inode should contain an EA value */
29678a363970STheodore Ts'o } ext4_iget_flags;
29688a363970STheodore Ts'o 
29698a363970STheodore Ts'o extern struct inode *__ext4_iget(struct super_block *sb, unsigned long ino,
29708a363970STheodore Ts'o 				 ext4_iget_flags flags, const char *function,
29718a363970STheodore Ts'o 				 unsigned int line);
29728a363970STheodore Ts'o 
29738a363970STheodore Ts'o #define ext4_iget(sb, ino, flags) \
29748a363970STheodore Ts'o 	__ext4_iget((sb), (ino), (flags), __func__, __LINE__)
29758a363970STheodore Ts'o 
2976a9185b41SChristoph Hellwig extern int  ext4_write_inode(struct inode *, struct writeback_control *);
2977c1632a0fSChristian Brauner extern int  ext4_setattr(struct mnt_idmap *, struct dentry *,
2978549c7297SChristian Brauner 			 struct iattr *);
29798434ef1dSEric Biggers extern u32  ext4_dio_alignment(struct inode *inode);
2980b74d24f7SChristian Brauner extern int  ext4_getattr(struct mnt_idmap *, const struct path *,
2981549c7297SChristian Brauner 			 struct kstat *, u32, unsigned int);
29820930fcc1SAl Viro extern void ext4_evict_inode(struct inode *);
29830930fcc1SAl Viro extern void ext4_clear_inode(struct inode *);
2984b74d24f7SChristian Brauner extern int  ext4_file_getattr(struct mnt_idmap *, const struct path *,
2985549c7297SChristian Brauner 			      struct kstat *, u32, unsigned int);
2986aa385729SChristoph Hellwig extern void ext4_dirty_inode(struct inode *, int);
29873dcf5451SChristoph Hellwig extern int ext4_change_inode_journal_flag(struct inode *, int);
29883dcf5451SChristoph Hellwig extern int ext4_get_inode_loc(struct inode *, struct ext4_iloc *);
29898016e29fSHarshad Shirwadkar extern int ext4_get_fc_inode_loc(struct super_block *sb, unsigned long ino,
29908016e29fSHarshad Shirwadkar 			  struct ext4_iloc *iloc);
2991a361293fSJan Kara extern int ext4_inode_attach_jinode(struct inode *inode);
299291ef4cafSDuane Griffin extern int ext4_can_truncate(struct inode *inode);
29932c98eb5eSTheodore Ts'o extern int ext4_truncate(struct inode *);
2994430657b6SRoss Zwisler extern int ext4_break_layouts(struct inode *);
2995ad5cd4f4SDarrick J. Wong extern int ext4_punch_hole(struct file *file, loff_t offset, loff_t length);
2996043546e4SIra Weiny extern void ext4_set_inode_flags(struct inode *, bool init);
2997ccd2506bSTheodore Ts'o extern int ext4_alloc_da_blocks(struct inode *inode);
29983dcf5451SChristoph Hellwig extern void ext4_set_aops(struct inode *inode);
29993dcf5451SChristoph Hellwig extern int ext4_writepage_trans_blocks(struct inode *);
300059205c8dSJan Kara extern int ext4_normal_submit_inode_data_buffers(struct jbd2_inode *jinode);
3001f3bd1f3fSMingming Cao extern int ext4_chunk_trans_blocks(struct inode *, int nrblocks);
3002a87dd18cSLukas Czerner extern int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode,
3003a87dd18cSLukas Czerner 			     loff_t lstart, loff_t lend);
3004401b25aaSSouptick Joarder extern vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf);
3005a9e7f447SDmitry Monakhov extern qsize_t *ext4_get_reserved_space(struct inode *inode);
3006040cb378SLi Xi extern int ext4_get_projid(struct inode *inode, kprojid_t *projid);
3007f456767dSEric Whitney extern void ext4_da_release_space(struct inode *inode, int to_free);
30085f634d06SAneesh Kumar K.V extern void ext4_da_update_reserve_space(struct inode *inode,
30095f634d06SAneesh Kumar K.V 					int used, int quota_claim);
301053085facSJan Kara extern int ext4_issue_zeroout(struct inode *inode, ext4_lblk_t lblk,
301153085facSJan Kara 			      ext4_fsblk_t pblk, ext4_lblk_t len);
3012dae1e52cSAmir Goldstein 
3013dae1e52cSAmir Goldstein /* indirect.c */
3014dae1e52cSAmir Goldstein extern int ext4_ind_map_blocks(handle_t *handle, struct inode *inode,
3015dae1e52cSAmir Goldstein 				struct ext4_map_blocks *map, int flags);
3016fa55a0edSJan Kara extern int ext4_ind_trans_blocks(struct inode *inode, int nrblocks);
3017819c4920STheodore Ts'o extern void ext4_ind_truncate(handle_t *, struct inode *inode);
30184f579ae7SLukas Czerner extern int ext4_ind_remove_space(handle_t *handle, struct inode *inode,
30194f579ae7SLukas Czerner 				 ext4_lblk_t start, ext4_lblk_t end);
3020ff9893dcSAmir Goldstein 
30213dcf5451SChristoph Hellwig /* ioctl.c */
30223dcf5451SChristoph Hellwig extern long ext4_ioctl(struct file *, unsigned int, unsigned long);
30233dcf5451SChristoph Hellwig extern long ext4_compat_ioctl(struct file *, unsigned int, unsigned long);
30248782a9aeSChristian Brauner int ext4_fileattr_set(struct mnt_idmap *idmap,
30254db5c2e6SMiklos Szeredi 		      struct dentry *dentry, struct fileattr *fa);
30264db5c2e6SMiklos Szeredi int ext4_fileattr_get(struct dentry *dentry, struct fileattr *fa);
30278016e29fSHarshad Shirwadkar extern void ext4_reset_inode_seed(struct inode *inode);
3028827891a3STheodore Ts'o int ext4_update_overhead(struct super_block *sb, bool force);
302997524b45SChristoph Hellwig int ext4_force_shutdown(struct super_block *sb, u32 flags);
30303dcf5451SChristoph Hellwig 
30313dcf5451SChristoph Hellwig /* migrate.c */
30322a43a878SAneesh Kumar K.V extern int ext4_ext_migrate(struct inode *);
30330d14b098SLukas Czerner extern int ext4_ind_migrate(struct inode *inode);
30343d0518f4SWei Yongjun 
30353dcf5451SChristoph Hellwig /* namei.c */
30368016e29fSHarshad Shirwadkar extern int ext4_init_new_dir(handle_t *handle, struct inode *dir,
30378016e29fSHarshad Shirwadkar 			     struct inode *inode);
3038f036adb3STheodore Ts'o extern int ext4_dirblock_csum_verify(struct inode *inode,
3039f036adb3STheodore Ts'o 				     struct buffer_head *bh);
30403dcf5451SChristoph Hellwig extern int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
30413dcf5451SChristoph Hellwig 				__u32 start_minor_hash, __u32 *next_hash);
30425b643f9cSTheodore Ts'o extern int ext4_search_dir(struct buffer_head *bh,
30437335cd3bSTao Ma 			   char *search_buf,
30447335cd3bSTao Ma 			   int buf_size,
30457335cd3bSTao Ma 			   struct inode *dir,
30465b643f9cSTheodore Ts'o 			   struct ext4_filename *fname,
30477335cd3bSTao Ma 			   unsigned int offset,
30487335cd3bSTao Ma 			   struct ext4_dir_entry_2 **res_dir);
30492fe34d29SKyoungho Koo extern int ext4_generic_delete_entry(struct inode *dir,
305005019a9eSTao Ma 				     struct ext4_dir_entry_2 *de_del,
305105019a9eSTao Ma 				     struct buffer_head *bh,
305205019a9eSTao Ma 				     void *entry_buf,
305305019a9eSTao Ma 				     int buf_size,
305405019a9eSTao Ma 				     int csum_size);
3055a7550b30SJaegeuk Kim extern bool ext4_empty_dir(struct inode *inode);
30563dcf5451SChristoph Hellwig 
30573dcf5451SChristoph Hellwig /* resize.c */
30581d0c3924STheodore Ts'o extern void ext4_kvfree_array_rcu(void *to_free);
30593dcf5451SChristoph Hellwig extern int ext4_group_add(struct super_block *sb,
30603dcf5451SChristoph Hellwig 				struct ext4_new_group_data *input);
30613dcf5451SChristoph Hellwig extern int ext4_group_extend(struct super_block *sb,
30623dcf5451SChristoph Hellwig 				struct ext4_super_block *es,
30633dcf5451SChristoph Hellwig 				ext4_fsblk_t n_blocks_count);
306419c5246dSYongqiang Yang extern int ext4_resize_fs(struct super_block *sb, ext4_fsblk_t n_blocks_count);
3065bbc605cdSLukas Czerner extern unsigned int ext4_list_backups(struct super_block *sb,
3066bbc605cdSLukas Czerner 				      unsigned int *three, unsigned int *five,
3067bbc605cdSLukas Czerner 				      unsigned int *seven);
30683dcf5451SChristoph Hellwig 
30693dcf5451SChristoph Hellwig /* super.c */
3070fb265c9cSTheodore Ts'o extern struct buffer_head *ext4_sb_bread(struct super_block *sb,
307167c0f556SBart Van Assche 					 sector_t block, blk_opf_t op_flags);
30728394a6abSzhangyi (F) extern struct buffer_head *ext4_sb_bread_unmovable(struct super_block *sb,
30738394a6abSzhangyi (F) 						   sector_t block);
307467c0f556SBart Van Assche extern void ext4_read_bh_nowait(struct buffer_head *bh, blk_opf_t op_flags,
3075fa491b14Szhangyi (F) 				bh_end_io_t *end_io);
307667c0f556SBart Van Assche extern int ext4_read_bh(struct buffer_head *bh, blk_opf_t op_flags,
3077fa491b14Szhangyi (F) 			bh_end_io_t *end_io);
307867c0f556SBart Van Assche extern int ext4_read_bh_lock(struct buffer_head *bh, blk_opf_t op_flags, bool wait);
30795df1d412Szhangyi (F) extern void ext4_sb_breadahead_unmovable(struct super_block *sb, sector_t block);
3080ebd173beSTheodore Ts'o extern int ext4_seq_options_show(struct seq_file *seq, void *offset);
3081952fc18eSTheodore Ts'o extern int ext4_calculate_overhead(struct super_block *sb);
3082bbc605cdSLukas Czerner extern __le32 ext4_superblock_csum(struct super_block *sb,
3083bbc605cdSLukas Czerner 				   struct ext4_super_block *es);
308406db49e6STheodore Ts'o extern void ext4_superblock_csum_set(struct super_block *sb);
3085117fff10STheodore Ts'o extern int ext4_alloc_flex_bg_array(struct super_block *sb,
3086117fff10STheodore Ts'o 				    ext4_group_t ngroup);
3087722887ddSTheodore Ts'o extern const char *ext4_decode_error(struct super_block *sb, int errno,
3088722887ddSTheodore Ts'o 				     char nbuf[16]);
3089db79e6d1SWang Shilong extern void ext4_mark_group_bitmap_corrupted(struct super_block *sb,
3090db79e6d1SWang Shilong 					     ext4_group_t block_group,
3091db79e6d1SWang Shilong 					     unsigned int flags);
309268228da5SWang Jianjian extern unsigned int ext4_num_base_meta_blocks(struct super_block *sb,
309368228da5SWang Jianjian 					      ext4_group_t block_group);
3094e7c96e8eSJoe Perches 
3095014c9caaSJan Kara extern __printf(7, 8)
3096014c9caaSJan Kara void __ext4_error(struct super_block *, const char *, unsigned int, bool,
3097014c9caaSJan Kara 		  int, __u64, const char *, ...);
309854d3adbcSTheodore Ts'o extern __printf(6, 7)
309954d3adbcSTheodore Ts'o void __ext4_error_inode(struct inode *, const char *, unsigned int,
310054d3adbcSTheodore Ts'o 			ext4_fsblk_t, int, const char *, ...);
3101b9075fa9SJoe Perches extern __printf(5, 6)
3102e7c96e8eSJoe Perches void __ext4_error_file(struct file *, const char *, unsigned int, ext4_fsblk_t,
3103b9075fa9SJoe Perches 		     const char *, ...);
3104c398eda0STheodore Ts'o extern void __ext4_std_error(struct super_block *, const char *,
3105c398eda0STheodore Ts'o 			     unsigned int, int);
3106b9075fa9SJoe Perches extern __printf(4, 5)
3107b9075fa9SJoe Perches void __ext4_warning(struct super_block *, const char *, unsigned int,
3108b9075fa9SJoe Perches 		    const char *, ...);
3109b03a2f7eSAndreas Dilger extern __printf(4, 5)
3110b03a2f7eSAndreas Dilger void __ext4_warning_inode(const struct inode *inode, const char *function,
3111b03a2f7eSAndreas Dilger 			  unsigned int line, const char *fmt, ...);
3112b9075fa9SJoe Perches extern __printf(3, 4)
3113e7c96e8eSJoe Perches void __ext4_msg(struct super_block *, const char *, const char *, ...);
3114c5e06d10SJohann Lombardi extern void __dump_mmp_msg(struct super_block *, struct mmp_struct *mmp,
3115c5e06d10SJohann Lombardi 			   const char *, unsigned int, const char *);
3116b9075fa9SJoe Perches extern __printf(7, 8)
3117b9075fa9SJoe Perches void __ext4_grp_locked_error(const char *, unsigned int,
3118b9075fa9SJoe Perches 			     struct super_block *, ext4_group_t,
3119b9075fa9SJoe Perches 			     unsigned long, ext4_fsblk_t,
3120b9075fa9SJoe Perches 			     const char *, ...);
3121e7c96e8eSJoe Perches 
3122b03a2f7eSAndreas Dilger #define EXT4_ERROR_INODE(inode, fmt, a...) \
3123b03a2f7eSAndreas Dilger 	ext4_error_inode((inode), __func__, __LINE__, 0, (fmt), ## a)
3124b03a2f7eSAndreas Dilger 
312554d3adbcSTheodore Ts'o #define EXT4_ERROR_INODE_ERR(inode, err, fmt, a...)			\
312654d3adbcSTheodore Ts'o 	__ext4_error_inode((inode), __func__, __LINE__, 0, (err), (fmt), ## a)
312754d3adbcSTheodore Ts'o 
312854d3adbcSTheodore Ts'o #define ext4_error_inode_block(inode, block, err, fmt, a...)		\
312954d3adbcSTheodore Ts'o 	__ext4_error_inode((inode), __func__, __LINE__, (block), (err),	\
313054d3adbcSTheodore Ts'o 			   (fmt), ## a)
3131b03a2f7eSAndreas Dilger 
3132b03a2f7eSAndreas Dilger #define EXT4_ERROR_FILE(file, block, fmt, a...)				\
3133b03a2f7eSAndreas Dilger 	ext4_error_file((file), __func__, __LINE__, (block), (fmt), ## a)
3134b03a2f7eSAndreas Dilger 
3135014c9caaSJan Kara #define ext4_abort(sb, err, fmt, a...)					\
3136014c9caaSJan Kara 	__ext4_error((sb), __func__, __LINE__, true, (err), 0, (fmt), ## a)
3137014c9caaSJan Kara 
3138e7c96e8eSJoe Perches #ifdef CONFIG_PRINTK
3139e7c96e8eSJoe Perches 
3140e7c96e8eSJoe Perches #define ext4_error_inode(inode, func, line, block, fmt, ...)		\
314154d3adbcSTheodore Ts'o 	__ext4_error_inode(inode, func, line, block, 0, fmt, ##__VA_ARGS__)
314254d3adbcSTheodore Ts'o #define ext4_error_inode_err(inode, func, line, block, err, fmt, ...)	\
314354d3adbcSTheodore Ts'o 	__ext4_error_inode((inode), (func), (line), (block), 		\
314454d3adbcSTheodore Ts'o 			   (err), (fmt), ##__VA_ARGS__)
3145e7c96e8eSJoe Perches #define ext4_error_file(file, func, line, block, fmt, ...)		\
3146e7c96e8eSJoe Perches 	__ext4_error_file(file, func, line, block, fmt, ##__VA_ARGS__)
3147e7c96e8eSJoe Perches #define ext4_error(sb, fmt, ...)					\
3148014c9caaSJan Kara 	__ext4_error((sb), __func__, __LINE__, false, 0, 0, (fmt),	\
3149014c9caaSJan Kara 		##__VA_ARGS__)
315054d3adbcSTheodore Ts'o #define ext4_error_err(sb, err, fmt, ...)				\
3151014c9caaSJan Kara 	__ext4_error((sb), __func__, __LINE__, false, (err), 0, (fmt),	\
3152014c9caaSJan Kara 		##__VA_ARGS__)
3153e7c96e8eSJoe Perches #define ext4_warning(sb, fmt, ...)					\
3154e7c96e8eSJoe Perches 	__ext4_warning(sb, __func__, __LINE__, fmt, ##__VA_ARGS__)
3155b03a2f7eSAndreas Dilger #define ext4_warning_inode(inode, fmt, ...)				\
3156b03a2f7eSAndreas Dilger 	__ext4_warning_inode(inode, __func__, __LINE__, fmt, ##__VA_ARGS__)
3157e7c96e8eSJoe Perches #define ext4_msg(sb, level, fmt, ...)				\
3158e7c96e8eSJoe Perches 	__ext4_msg(sb, level, fmt, ##__VA_ARGS__)
3159e7c96e8eSJoe Perches #define dump_mmp_msg(sb, mmp, msg)					\
3160e7c96e8eSJoe Perches 	__dump_mmp_msg(sb, mmp, __func__, __LINE__, msg)
3161e7c96e8eSJoe Perches #define ext4_grp_locked_error(sb, grp, ino, block, fmt, ...)		\
3162e7c96e8eSJoe Perches 	__ext4_grp_locked_error(__func__, __LINE__, sb, grp, ino, block, \
3163e7c96e8eSJoe Perches 				fmt, ##__VA_ARGS__)
3164e7c96e8eSJoe Perches 
3165e7c96e8eSJoe Perches #else
3166e7c96e8eSJoe Perches 
3167e7c96e8eSJoe Perches #define ext4_error_inode(inode, func, line, block, fmt, ...)		\
3168e7c96e8eSJoe Perches do {									\
3169e7c96e8eSJoe Perches 	no_printk(fmt, ##__VA_ARGS__);					\
317054d3adbcSTheodore Ts'o 	__ext4_error_inode(inode, "", 0, block, 0, " ");		\
317154d3adbcSTheodore Ts'o } while (0)
317254d3adbcSTheodore Ts'o #define ext4_error_inode_err(inode, func, line, block, err, fmt, ...)	\
317354d3adbcSTheodore Ts'o do {									\
317454d3adbcSTheodore Ts'o 	no_printk(fmt, ##__VA_ARGS__);					\
317554d3adbcSTheodore Ts'o 	__ext4_error_inode(inode, "", 0, block, err, " ");		\
3176e7c96e8eSJoe Perches } while (0)
3177e7c96e8eSJoe Perches #define ext4_error_file(file, func, line, block, fmt, ...)		\
3178e7c96e8eSJoe Perches do {									\
3179e7c96e8eSJoe Perches 	no_printk(fmt, ##__VA_ARGS__);					\
3180e7c96e8eSJoe Perches 	__ext4_error_file(file, "", 0, block, " ");			\
3181e7c96e8eSJoe Perches } while (0)
3182e7c96e8eSJoe Perches #define ext4_error(sb, fmt, ...)					\
3183e7c96e8eSJoe Perches do {									\
3184e7c96e8eSJoe Perches 	no_printk(fmt, ##__VA_ARGS__);					\
3185014c9caaSJan Kara 	__ext4_error(sb, "", 0, false, 0, 0, " ");			\
3186e7c96e8eSJoe Perches } while (0)
318754d3adbcSTheodore Ts'o #define ext4_error_err(sb, err, fmt, ...)				\
3188e7c96e8eSJoe Perches do {									\
3189e7c96e8eSJoe Perches 	no_printk(fmt, ##__VA_ARGS__);					\
3190014c9caaSJan Kara 	__ext4_error(sb, "", 0, false, err, 0, " ");			\
3191e7c96e8eSJoe Perches } while (0)
3192e7c96e8eSJoe Perches #define ext4_warning(sb, fmt, ...)					\
3193e7c96e8eSJoe Perches do {									\
3194e7c96e8eSJoe Perches 	no_printk(fmt, ##__VA_ARGS__);					\
3195e7c96e8eSJoe Perches 	__ext4_warning(sb, "", 0, " ");					\
3196e7c96e8eSJoe Perches } while (0)
3197b03a2f7eSAndreas Dilger #define ext4_warning_inode(inode, fmt, ...)				\
3198b03a2f7eSAndreas Dilger do {									\
3199b03a2f7eSAndreas Dilger 	no_printk(fmt, ##__VA_ARGS__);					\
3200b03a2f7eSAndreas Dilger 	__ext4_warning_inode(inode, "", 0, " ");			\
3201b03a2f7eSAndreas Dilger } while (0)
3202e7c96e8eSJoe Perches #define ext4_msg(sb, level, fmt, ...)					\
3203e7c96e8eSJoe Perches do {									\
3204e7c96e8eSJoe Perches 	no_printk(fmt, ##__VA_ARGS__);					\
3205e7c96e8eSJoe Perches 	__ext4_msg(sb, "", " ");					\
3206e7c96e8eSJoe Perches } while (0)
3207e7c96e8eSJoe Perches #define dump_mmp_msg(sb, mmp, msg)					\
3208e7c96e8eSJoe Perches 	__dump_mmp_msg(sb, mmp, "", 0, "")
3209e7c96e8eSJoe Perches #define ext4_grp_locked_error(sb, grp, ino, block, fmt, ...)		\
3210e7c96e8eSJoe Perches do {									\
3211e7c96e8eSJoe Perches 	no_printk(fmt, ##__VA_ARGS__);				\
3212e7c96e8eSJoe Perches 	__ext4_grp_locked_error("", 0, sb, grp, ino, block, " ");	\
3213e7c96e8eSJoe Perches } while (0)
3214e7c96e8eSJoe Perches 
3215e7c96e8eSJoe Perches #endif
3216e7c96e8eSJoe Perches 
32173dcf5451SChristoph Hellwig extern ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
32183dcf5451SChristoph Hellwig 				      struct ext4_group_desc *bg);
32193dcf5451SChristoph Hellwig extern ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
32203dcf5451SChristoph Hellwig 				      struct ext4_group_desc *bg);
32213dcf5451SChristoph Hellwig extern ext4_fsblk_t ext4_inode_table(struct super_block *sb,
32223dcf5451SChristoph Hellwig 				     struct ext4_group_desc *bg);
3223021b65bbSTheodore Ts'o extern __u32 ext4_free_group_clusters(struct super_block *sb,
3224560671a0SAneesh Kumar K.V 				      struct ext4_group_desc *bg);
3225560671a0SAneesh Kumar K.V extern __u32 ext4_free_inodes_count(struct super_block *sb,
3226560671a0SAneesh Kumar K.V 				 struct ext4_group_desc *bg);
3227560671a0SAneesh Kumar K.V extern __u32 ext4_used_dirs_count(struct super_block *sb,
3228560671a0SAneesh Kumar K.V 				struct ext4_group_desc *bg);
3229560671a0SAneesh Kumar K.V extern __u32 ext4_itable_unused_count(struct super_block *sb,
3230560671a0SAneesh Kumar K.V 				   struct ext4_group_desc *bg);
32313dcf5451SChristoph Hellwig extern void ext4_block_bitmap_set(struct super_block *sb,
32323dcf5451SChristoph Hellwig 				  struct ext4_group_desc *bg, ext4_fsblk_t blk);
32333dcf5451SChristoph Hellwig extern void ext4_inode_bitmap_set(struct super_block *sb,
32343dcf5451SChristoph Hellwig 				  struct ext4_group_desc *bg, ext4_fsblk_t blk);
32353dcf5451SChristoph Hellwig extern void ext4_inode_table_set(struct super_block *sb,
32363dcf5451SChristoph Hellwig 				 struct ext4_group_desc *bg, ext4_fsblk_t blk);
3237021b65bbSTheodore Ts'o extern void ext4_free_group_clusters_set(struct super_block *sb,
3238021b65bbSTheodore Ts'o 					 struct ext4_group_desc *bg,
3239021b65bbSTheodore Ts'o 					 __u32 count);
3240560671a0SAneesh Kumar K.V extern void ext4_free_inodes_set(struct super_block *sb,
3241560671a0SAneesh Kumar K.V 				struct ext4_group_desc *bg, __u32 count);
3242560671a0SAneesh Kumar K.V extern void ext4_used_dirs_set(struct super_block *sb,
3243560671a0SAneesh Kumar K.V 				struct ext4_group_desc *bg, __u32 count);
3244560671a0SAneesh Kumar K.V extern void ext4_itable_unused_set(struct super_block *sb,
3245560671a0SAneesh Kumar K.V 				   struct ext4_group_desc *bg, __u32 count);
3246feb0ab32SDarrick J. Wong extern int ext4_group_desc_csum_verify(struct super_block *sb, __u32 group,
3247bb23c20aSTheodore Ts'o 				       struct ext4_group_desc *gdp);
3248feb0ab32SDarrick J. Wong extern void ext4_group_desc_csum_set(struct super_block *sb, __u32 group,
3249bb23c20aSTheodore Ts'o 				     struct ext4_group_desc *gdp);
32507f511862STheodore Ts'o extern int ext4_register_li_request(struct super_block *sb,
32517f511862STheodore Ts'o 				    ext4_group_t first_not_zeroed);
32523dcf5451SChristoph Hellwig 
ext4_has_metadata_csum(struct super_block * sb)32539aa5d32bSDmitry Monakhov static inline int ext4_has_metadata_csum(struct super_block *sb)
32549aa5d32bSDmitry Monakhov {
3255e2b911c5SDarrick J. Wong 	WARN_ON_ONCE(ext4_has_feature_metadata_csum(sb) &&
32569aa5d32bSDmitry Monakhov 		     !EXT4_SB(sb)->s_chksum_driver);
32579aa5d32bSDmitry Monakhov 
3258dec214d0STahsin Erdogan 	return ext4_has_feature_metadata_csum(sb) &&
3259dec214d0STahsin Erdogan 	       (EXT4_SB(sb)->s_chksum_driver != NULL);
32609aa5d32bSDmitry Monakhov }
3261dec214d0STahsin Erdogan 
ext4_has_group_desc_csum(struct super_block * sb)3262dec214d0STahsin Erdogan static inline int ext4_has_group_desc_csum(struct super_block *sb)
3263dec214d0STahsin Erdogan {
3264dec214d0STahsin Erdogan 	return ext4_has_feature_gdt_csum(sb) || ext4_has_metadata_csum(sb);
3265dec214d0STahsin Erdogan }
3266dec214d0STahsin Erdogan 
326781e8c3c5SPetr Malat #define ext4_read_incompat_64bit_val(es, name) \
326881e8c3c5SPetr Malat 	(((es)->s_feature_incompat & cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT) \
326981e8c3c5SPetr Malat 		? (ext4_fsblk_t)le32_to_cpu(es->name##_hi) << 32 : 0) | \
327081e8c3c5SPetr Malat 		le32_to_cpu(es->name##_lo))
327181e8c3c5SPetr Malat 
ext4_blocks_count(struct ext4_super_block * es)32723dcf5451SChristoph Hellwig static inline ext4_fsblk_t ext4_blocks_count(struct ext4_super_block *es)
32733dcf5451SChristoph Hellwig {
327481e8c3c5SPetr Malat 	return ext4_read_incompat_64bit_val(es, s_blocks_count);
32753dcf5451SChristoph Hellwig }
32763dcf5451SChristoph Hellwig 
ext4_r_blocks_count(struct ext4_super_block * es)32773dcf5451SChristoph Hellwig static inline ext4_fsblk_t ext4_r_blocks_count(struct ext4_super_block *es)
32783dcf5451SChristoph Hellwig {
327981e8c3c5SPetr Malat 	return ext4_read_incompat_64bit_val(es, s_r_blocks_count);
32803dcf5451SChristoph Hellwig }
32813dcf5451SChristoph Hellwig 
ext4_free_blocks_count(struct ext4_super_block * es)32823dcf5451SChristoph Hellwig static inline ext4_fsblk_t ext4_free_blocks_count(struct ext4_super_block *es)
32833dcf5451SChristoph Hellwig {
328481e8c3c5SPetr Malat 	return ext4_read_incompat_64bit_val(es, s_free_blocks_count);
32853dcf5451SChristoph Hellwig }
32863dcf5451SChristoph Hellwig 
ext4_blocks_count_set(struct ext4_super_block * es,ext4_fsblk_t blk)32873dcf5451SChristoph Hellwig static inline void ext4_blocks_count_set(struct ext4_super_block *es,
32883dcf5451SChristoph Hellwig 					 ext4_fsblk_t blk)
32893dcf5451SChristoph Hellwig {
32903dcf5451SChristoph Hellwig 	es->s_blocks_count_lo = cpu_to_le32((u32)blk);
32913dcf5451SChristoph Hellwig 	es->s_blocks_count_hi = cpu_to_le32(blk >> 32);
32923dcf5451SChristoph Hellwig }
32933dcf5451SChristoph Hellwig 
ext4_free_blocks_count_set(struct ext4_super_block * es,ext4_fsblk_t blk)32943dcf5451SChristoph Hellwig static inline void ext4_free_blocks_count_set(struct ext4_super_block *es,
32953dcf5451SChristoph Hellwig 					      ext4_fsblk_t blk)
32963dcf5451SChristoph Hellwig {
32973dcf5451SChristoph Hellwig 	es->s_free_blocks_count_lo = cpu_to_le32((u32)blk);
32983dcf5451SChristoph Hellwig 	es->s_free_blocks_count_hi = cpu_to_le32(blk >> 32);
32993dcf5451SChristoph Hellwig }
33003dcf5451SChristoph Hellwig 
ext4_r_blocks_count_set(struct ext4_super_block * es,ext4_fsblk_t blk)33013dcf5451SChristoph Hellwig static inline void ext4_r_blocks_count_set(struct ext4_super_block *es,
33023dcf5451SChristoph Hellwig 					   ext4_fsblk_t blk)
33033dcf5451SChristoph Hellwig {
33043dcf5451SChristoph Hellwig 	es->s_r_blocks_count_lo = cpu_to_le32((u32)blk);
33053dcf5451SChristoph Hellwig 	es->s_r_blocks_count_hi = cpu_to_le32(blk >> 32);
33063dcf5451SChristoph Hellwig }
33073dcf5451SChristoph Hellwig 
ext4_isize(struct super_block * sb,struct ext4_inode * raw_inode)3308e08ac99fSArtem Blagodarenko static inline loff_t ext4_isize(struct super_block *sb,
3309e08ac99fSArtem Blagodarenko 				struct ext4_inode *raw_inode)
33103dcf5451SChristoph Hellwig {
3311e08ac99fSArtem Blagodarenko 	if (ext4_has_feature_largedir(sb) ||
3312e08ac99fSArtem Blagodarenko 	    S_ISREG(le16_to_cpu(raw_inode->i_mode)))
33133dcf5451SChristoph Hellwig 		return ((loff_t)le32_to_cpu(raw_inode->i_size_high) << 32) |
33143dcf5451SChristoph Hellwig 			le32_to_cpu(raw_inode->i_size_lo);
3315e08ac99fSArtem Blagodarenko 
331606a279d6STheodore Ts'o 	return (loff_t) le32_to_cpu(raw_inode->i_size_lo);
33173dcf5451SChristoph Hellwig }
33183dcf5451SChristoph Hellwig 
ext4_isize_set(struct ext4_inode * raw_inode,loff_t i_size)33193dcf5451SChristoph Hellwig static inline void ext4_isize_set(struct ext4_inode *raw_inode, loff_t i_size)
33203dcf5451SChristoph Hellwig {
33213dcf5451SChristoph Hellwig 	raw_inode->i_size_lo = cpu_to_le32(i_size);
33223dcf5451SChristoph Hellwig 	raw_inode->i_size_high = cpu_to_le32(i_size >> 32);
33233dcf5451SChristoph Hellwig }
33243dcf5451SChristoph Hellwig 
33258df9675fSTheodore Ts'o /*
33268df9675fSTheodore Ts'o  * Reading s_groups_count requires using smp_rmb() afterwards.  See
33278df9675fSTheodore Ts'o  * the locking protocol documented in the comments of ext4_group_add()
33288df9675fSTheodore Ts'o  * in resize.c
33298df9675fSTheodore Ts'o  */
ext4_get_groups_count(struct super_block * sb)33308df9675fSTheodore Ts'o static inline ext4_group_t ext4_get_groups_count(struct super_block *sb)
33318df9675fSTheodore Ts'o {
33328df9675fSTheodore Ts'o 	ext4_group_t	ngroups = EXT4_SB(sb)->s_groups_count;
33338df9675fSTheodore Ts'o 
33348df9675fSTheodore Ts'o 	smp_rmb();
33358df9675fSTheodore Ts'o 	return ngroups;
33368df9675fSTheodore Ts'o }
33373dcf5451SChristoph Hellwig 
ext4_flex_group(struct ext4_sb_info * sbi,ext4_group_t block_group)3338772cb7c8SJose R. Santos static inline ext4_group_t ext4_flex_group(struct ext4_sb_info *sbi,
3339772cb7c8SJose R. Santos 					     ext4_group_t block_group)
3340772cb7c8SJose R. Santos {
3341772cb7c8SJose R. Santos 	return block_group >> sbi->s_log_groups_per_flex;
3342772cb7c8SJose R. Santos }
3343772cb7c8SJose R. Santos 
ext4_flex_bg_size(struct ext4_sb_info * sbi)3344772cb7c8SJose R. Santos static inline unsigned int ext4_flex_bg_size(struct ext4_sb_info *sbi)
3345772cb7c8SJose R. Santos {
3346772cb7c8SJose R. Santos 	return 1 << sbi->s_log_groups_per_flex;
3347772cb7c8SJose R. Santos }
3348772cb7c8SJose R. Santos 
33493dcf5451SChristoph Hellwig #define ext4_std_error(sb, errno)				\
33503dcf5451SChristoph Hellwig do {								\
33513dcf5451SChristoph Hellwig 	if ((errno))						\
3352c398eda0STheodore Ts'o 		__ext4_std_error((sb), __func__, __LINE__, (errno));	\
33533dcf5451SChristoph Hellwig } while (0)
33543dcf5451SChristoph Hellwig 
3355a30d542aSAneesh Kumar K.V #ifdef CONFIG_SMP
3356df55c99dSTheodore Ts'o /* Each CPU can accumulate percpu_counter_batch clusters in their local
3357df55c99dSTheodore Ts'o  * counters. So we need to make sure we have free clusters more
3358179f7ebfSEric Dumazet  * than percpu_counter_batch  * nr_cpu_ids. Also add a window of 4 times.
3359a30d542aSAneesh Kumar K.V  */
3360df55c99dSTheodore Ts'o #define EXT4_FREECLUSTERS_WATERMARK (4 * (percpu_counter_batch * nr_cpu_ids))
3361a30d542aSAneesh Kumar K.V #else
3362df55c99dSTheodore Ts'o #define EXT4_FREECLUSTERS_WATERMARK 0
3363a30d542aSAneesh Kumar K.V #endif
3364a30d542aSAneesh Kumar K.V 
3365f340b3d9Shongnanli /* Update i_disksize. Requires i_rwsem to avoid races with truncate */
ext4_update_i_disksize(struct inode * inode,loff_t newsize)3366cf17fea6SAneesh Kumar K.V static inline void ext4_update_i_disksize(struct inode *inode, loff_t newsize)
3367cf17fea6SAneesh Kumar K.V {
336890e775b7SJan Kara 	WARN_ON_ONCE(S_ISREG(inode->i_mode) &&
33695955102cSAl Viro 		     !inode_is_locked(inode));
3370cf17fea6SAneesh Kumar K.V 	down_write(&EXT4_I(inode)->i_data_sem);
3371cf17fea6SAneesh Kumar K.V 	if (newsize > EXT4_I(inode)->i_disksize)
337235df4299SQian Cai 		WRITE_ONCE(EXT4_I(inode)->i_disksize, newsize);
3373cf17fea6SAneesh Kumar K.V 	up_write(&EXT4_I(inode)->i_data_sem);
337490e775b7SJan Kara }
337590e775b7SJan Kara 
3376f340b3d9Shongnanli /* Update i_size, i_disksize. Requires i_rwsem to avoid races with truncate */
ext4_update_inode_size(struct inode * inode,loff_t newsize)33774631dbf6SDmitry Monakhov static inline int ext4_update_inode_size(struct inode *inode, loff_t newsize)
33784631dbf6SDmitry Monakhov {
33794631dbf6SDmitry Monakhov 	int changed = 0;
33804631dbf6SDmitry Monakhov 
33814631dbf6SDmitry Monakhov 	if (newsize > inode->i_size) {
33824631dbf6SDmitry Monakhov 		i_size_write(inode, newsize);
33834631dbf6SDmitry Monakhov 		changed = 1;
33844631dbf6SDmitry Monakhov 	}
33854631dbf6SDmitry Monakhov 	if (newsize > EXT4_I(inode)->i_disksize) {
33864631dbf6SDmitry Monakhov 		ext4_update_i_disksize(inode, newsize);
33874631dbf6SDmitry Monakhov 		changed |= 2;
33884631dbf6SDmitry Monakhov 	}
33894631dbf6SDmitry Monakhov 	return changed;
33904631dbf6SDmitry Monakhov }
33914631dbf6SDmitry Monakhov 
339201127848SJan Kara int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset,
339301127848SJan Kara 				      loff_t len);
339401127848SJan Kara 
33955d1b1b3fSAneesh Kumar K.V struct ext4_group_info {
33965d1b1b3fSAneesh Kumar K.V 	unsigned long   bb_state;
3397addd752cSChunguang Xu #ifdef AGGRESSIVE_CHECK
3398addd752cSChunguang Xu 	unsigned long	bb_check_counter;
3399addd752cSChunguang Xu #endif
34005d1b1b3fSAneesh Kumar K.V 	struct rb_root  bb_free_root;
3401a36b4498SEric Sandeen 	ext4_grpblk_t	bb_first_free;	/* first free block */
3402a36b4498SEric Sandeen 	ext4_grpblk_t	bb_free;	/* total free blocks */
3403a36b4498SEric Sandeen 	ext4_grpblk_t	bb_fragments;	/* nr of freespace fragments */
340483e80a6eSJan Kara 	int		bb_avg_fragment_size_order;	/* order of average
340583e80a6eSJan Kara 							   fragment in BG */
34068a57d9d6SCurt Wohlgemuth 	ext4_grpblk_t	bb_largest_free_order;/* order of largest frag in BG */
3407196e402aSHarshad Shirwadkar 	ext4_group_t	bb_group;	/* Group number */
34085d1b1b3fSAneesh Kumar K.V 	struct          list_head bb_prealloc_list;
34095d1b1b3fSAneesh Kumar K.V #ifdef DOUBLE_CHECK
34105d1b1b3fSAneesh Kumar K.V 	void            *bb_bitmap;
34115d1b1b3fSAneesh Kumar K.V #endif
34125d1b1b3fSAneesh Kumar K.V 	struct rw_semaphore alloc_sem;
341383e80a6eSJan Kara 	struct list_head bb_avg_fragment_size_node;
3414196e402aSHarshad Shirwadkar 	struct list_head bb_largest_free_order_node;
3415a36b4498SEric Sandeen 	ext4_grpblk_t	bb_counters[];	/* Nr of free power-of-two-block
3416a36b4498SEric Sandeen 					 * regions, index is order.
3417a36b4498SEric Sandeen 					 * bb_counters[3] = 5 means
3418a36b4498SEric Sandeen 					 * 5 free 8-block regions. */
34195d1b1b3fSAneesh Kumar K.V };
34205d1b1b3fSAneesh Kumar K.V 
34215d1b1b3fSAneesh Kumar K.V #define EXT4_GROUP_INFO_NEED_INIT_BIT		0
34223d56b8d2STao Ma #define EXT4_GROUP_INFO_WAS_TRIMMED_BIT		1
3423163a203dSDarrick J. Wong #define EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT	2
342487a39389SDarrick J. Wong #define EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT	3
3425db79e6d1SWang Shilong #define EXT4_GROUP_INFO_BBITMAP_CORRUPT		\
3426db79e6d1SWang Shilong 	(1 << EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT)
3427db79e6d1SWang Shilong #define EXT4_GROUP_INFO_IBITMAP_CORRUPT		\
3428db79e6d1SWang Shilong 	(1 << EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT)
3429cfd73237SAlex Zhuravlev #define EXT4_GROUP_INFO_BBITMAP_READ_BIT	4
34305d1b1b3fSAneesh Kumar K.V 
34315d1b1b3fSAneesh Kumar K.V #define EXT4_MB_GRP_NEED_INIT(grp)	\
34325d1b1b3fSAneesh Kumar K.V 	(test_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &((grp)->bb_state)))
3433163a203dSDarrick J. Wong #define EXT4_MB_GRP_BBITMAP_CORRUPT(grp)	\
3434163a203dSDarrick J. Wong 	(test_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT, &((grp)->bb_state)))
343587a39389SDarrick J. Wong #define EXT4_MB_GRP_IBITMAP_CORRUPT(grp)	\
343687a39389SDarrick J. Wong 	(test_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &((grp)->bb_state)))
34375d1b1b3fSAneesh Kumar K.V 
34383d56b8d2STao Ma #define EXT4_MB_GRP_WAS_TRIMMED(grp)	\
34393d56b8d2STao Ma 	(test_bit(EXT4_GROUP_INFO_WAS_TRIMMED_BIT, &((grp)->bb_state)))
34403d56b8d2STao Ma #define EXT4_MB_GRP_SET_TRIMMED(grp)	\
34413d56b8d2STao Ma 	(set_bit(EXT4_GROUP_INFO_WAS_TRIMMED_BIT, &((grp)->bb_state)))
34423d56b8d2STao Ma #define EXT4_MB_GRP_CLEAR_TRIMMED(grp)	\
34433d56b8d2STao Ma 	(clear_bit(EXT4_GROUP_INFO_WAS_TRIMMED_BIT, &((grp)->bb_state)))
3444cfd73237SAlex Zhuravlev #define EXT4_MB_GRP_TEST_AND_SET_READ(grp)	\
3445cfd73237SAlex Zhuravlev 	(test_and_set_bit(EXT4_GROUP_INFO_BBITMAP_READ_BIT, &((grp)->bb_state)))
34463d56b8d2STao Ma 
344750797481STheodore Ts'o #define EXT4_MAX_CONTENTION		8
344850797481STheodore Ts'o #define EXT4_CONTENTION_THRESHOLD	2
344950797481STheodore Ts'o 
ext4_group_lock_ptr(struct super_block * sb,ext4_group_t group)3450955ce5f5SAneesh Kumar K.V static inline spinlock_t *ext4_group_lock_ptr(struct super_block *sb,
3451955ce5f5SAneesh Kumar K.V 					      ext4_group_t group)
3452955ce5f5SAneesh Kumar K.V {
3453955ce5f5SAneesh Kumar K.V 	return bgl_lock_ptr(EXT4_SB(sb)->s_blockgroup_lock, group);
3454955ce5f5SAneesh Kumar K.V }
3455955ce5f5SAneesh Kumar K.V 
345650797481STheodore Ts'o /*
345750797481STheodore Ts'o  * Returns true if the filesystem is busy enough that attempts to
345850797481STheodore Ts'o  * access the block group locks has run into contention.
345950797481STheodore Ts'o  */
ext4_fs_is_busy(struct ext4_sb_info * sbi)346050797481STheodore Ts'o static inline int ext4_fs_is_busy(struct ext4_sb_info *sbi)
346150797481STheodore Ts'o {
346250797481STheodore Ts'o 	return (atomic_read(&sbi->s_lock_busy) > EXT4_CONTENTION_THRESHOLD);
346350797481STheodore Ts'o }
346450797481STheodore Ts'o 
ext4_lock_group(struct super_block * sb,ext4_group_t group)34655d1b1b3fSAneesh Kumar K.V static inline void ext4_lock_group(struct super_block *sb, ext4_group_t group)
34665d1b1b3fSAneesh Kumar K.V {
346750797481STheodore Ts'o 	spinlock_t *lock = ext4_group_lock_ptr(sb, group);
346850797481STheodore Ts'o 	if (spin_trylock(lock))
346950797481STheodore Ts'o 		/*
347050797481STheodore Ts'o 		 * We're able to grab the lock right away, so drop the
347150797481STheodore Ts'o 		 * lock contention counter.
347250797481STheodore Ts'o 		 */
347350797481STheodore Ts'o 		atomic_add_unless(&EXT4_SB(sb)->s_lock_busy, -1, 0);
347450797481STheodore Ts'o 	else {
347550797481STheodore Ts'o 		/*
347650797481STheodore Ts'o 		 * The lock is busy, so bump the contention counter,
347750797481STheodore Ts'o 		 * and then wait on the spin lock.
347850797481STheodore Ts'o 		 */
347950797481STheodore Ts'o 		atomic_add_unless(&EXT4_SB(sb)->s_lock_busy, 1,
348050797481STheodore Ts'o 				  EXT4_MAX_CONTENTION);
348150797481STheodore Ts'o 		spin_lock(lock);
348250797481STheodore Ts'o 	}
34835d1b1b3fSAneesh Kumar K.V }
34845d1b1b3fSAneesh Kumar K.V 
ext4_unlock_group(struct super_block * sb,ext4_group_t group)34855d1b1b3fSAneesh Kumar K.V static inline void ext4_unlock_group(struct super_block *sb,
34865d1b1b3fSAneesh Kumar K.V 					ext4_group_t group)
34875d1b1b3fSAneesh Kumar K.V {
3488955ce5f5SAneesh Kumar K.V 	spin_unlock(ext4_group_lock_ptr(sb, group));
34895d1b1b3fSAneesh Kumar K.V }
34905d1b1b3fSAneesh Kumar K.V 
3491f177ee08SRoman Anufriev #ifdef CONFIG_QUOTA
ext4_quota_capable(struct super_block * sb)3492f177ee08SRoman Anufriev static inline bool ext4_quota_capable(struct super_block *sb)
3493f177ee08SRoman Anufriev {
3494f177ee08SRoman Anufriev 	return (test_opt(sb, QUOTA) || ext4_has_feature_quota(sb));
3495f177ee08SRoman Anufriev }
3496f177ee08SRoman Anufriev 
ext4_is_quota_journalled(struct super_block * sb)3497f177ee08SRoman Anufriev static inline bool ext4_is_quota_journalled(struct super_block *sb)
3498f177ee08SRoman Anufriev {
3499f177ee08SRoman Anufriev 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3500f177ee08SRoman Anufriev 
3501f177ee08SRoman Anufriev 	return (ext4_has_feature_quota(sb) ||
3502f177ee08SRoman Anufriev 		sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]);
3503f177ee08SRoman Anufriev }
350425c6d98fSJan Kara int ext4_enable_quotas(struct super_block *sb);
3505f177ee08SRoman Anufriev #endif
3506f177ee08SRoman Anufriev 
35073dcf5451SChristoph Hellwig /*
35081f7d1e77STheodore Ts'o  * Block validity checking
35091f7d1e77STheodore Ts'o  */
35101f7d1e77STheodore Ts'o #define ext4_check_indirect_blockref(inode, bh)				\
35111f7d1e77STheodore Ts'o 	ext4_check_blockref(__func__, __LINE__, inode,			\
35121f7d1e77STheodore Ts'o 			    (__le32 *)(bh)->b_data,			\
35131f7d1e77STheodore Ts'o 			    EXT4_ADDR_PER_BLOCK((inode)->i_sb))
35141f7d1e77STheodore Ts'o 
35151f7d1e77STheodore Ts'o #define ext4_ind_check_inode(inode)					\
35161f7d1e77STheodore Ts'o 	ext4_check_blockref(__func__, __LINE__, inode,			\
35171f7d1e77STheodore Ts'o 			    EXT4_I(inode)->i_data,			\
35181f7d1e77STheodore Ts'o 			    EXT4_NDIR_BLOCKS)
35191f7d1e77STheodore Ts'o 
35201f7d1e77STheodore Ts'o /*
35213dcf5451SChristoph Hellwig  * Inodes and files operations
35223dcf5451SChristoph Hellwig  */
35233dcf5451SChristoph Hellwig 
35243dcf5451SChristoph Hellwig /* dir.c */
35253dcf5451SChristoph Hellwig extern const struct file_operations ext4_dir_operations;
35263dcf5451SChristoph Hellwig 
35273dcf5451SChristoph Hellwig /* file.c */
35283dcf5451SChristoph Hellwig extern const struct inode_operations ext4_file_inode_operations;
35293dcf5451SChristoph Hellwig extern const struct file_operations ext4_file_operations;
3530e0d10bfaSToshiyuki Okajima extern loff_t ext4_llseek(struct file *file, loff_t offset, int origin);
35313dcf5451SChristoph Hellwig 
353295eaefbdSTheodore Ts'o /* inline.c */
353395eaefbdSTheodore Ts'o extern int ext4_get_max_inline_size(struct inode *inode);
353495eaefbdSTheodore Ts'o extern int ext4_find_inline_data_nolock(struct inode *inode);
353595eaefbdSTheodore Ts'o extern int ext4_destroy_inline_data(handle_t *handle, struct inode *inode);
353695eaefbdSTheodore Ts'o 
35373edde93eSMatthew Wilcox int ext4_readpage_inline(struct inode *inode, struct folio *folio);
353895eaefbdSTheodore Ts'o extern int ext4_try_to_write_inline_data(struct address_space *mapping,
353995eaefbdSTheodore Ts'o 					 struct inode *inode,
354095eaefbdSTheodore Ts'o 					 loff_t pos, unsigned len,
354195eaefbdSTheodore Ts'o 					 struct page **pagep);
3542d19500daSRitesh Harjani int ext4_write_inline_data_end(struct inode *inode, loff_t pos, unsigned len,
3543d19500daSRitesh Harjani 			       unsigned copied, struct folio *folio);
354495eaefbdSTheodore Ts'o extern int ext4_da_write_inline_data_begin(struct address_space *mapping,
354595eaefbdSTheodore Ts'o 					   struct inode *inode,
354695eaefbdSTheodore Ts'o 					   loff_t pos, unsigned len,
354795eaefbdSTheodore Ts'o 					   struct page **pagep,
354895eaefbdSTheodore Ts'o 					   void **fsdata);
35495b643f9cSTheodore Ts'o extern int ext4_try_add_inline_entry(handle_t *handle,
35505b643f9cSTheodore Ts'o 				     struct ext4_filename *fname,
355156a04915STheodore Ts'o 				     struct inode *dir, struct inode *inode);
355295eaefbdSTheodore Ts'o extern int ext4_try_create_inline_dir(handle_t *handle,
355395eaefbdSTheodore Ts'o 				      struct inode *parent,
355495eaefbdSTheodore Ts'o 				      struct inode *inode);
355595eaefbdSTheodore Ts'o extern int ext4_read_inline_dir(struct file *filp,
3556725bebb2SAl Viro 				struct dir_context *ctx,
355795eaefbdSTheodore Ts'o 				int *has_inline_data);
35587633b08bSTheodore Ts'o extern int ext4_inlinedir_to_tree(struct file *dir_file,
35598af0f082STao Ma 				  struct inode *dir, ext4_lblk_t block,
35608af0f082STao Ma 				  struct dx_hash_info *hinfo,
35618af0f082STao Ma 				  __u32 start_hash, __u32 start_minor_hash,
35628af0f082STao Ma 				  int *has_inline_data);
356395eaefbdSTheodore Ts'o extern struct buffer_head *ext4_find_inline_entry(struct inode *dir,
35645b643f9cSTheodore Ts'o 					struct ext4_filename *fname,
356595eaefbdSTheodore Ts'o 					struct ext4_dir_entry_2 **res_dir,
356695eaefbdSTheodore Ts'o 					int *has_inline_data);
356795eaefbdSTheodore Ts'o extern int ext4_delete_inline_entry(handle_t *handle,
356895eaefbdSTheodore Ts'o 				    struct inode *dir,
356995eaefbdSTheodore Ts'o 				    struct ext4_dir_entry_2 *de_del,
357095eaefbdSTheodore Ts'o 				    struct buffer_head *bh,
357195eaefbdSTheodore Ts'o 				    int *has_inline_data);
3572a7550b30SJaegeuk Kim extern bool empty_inline_dir(struct inode *dir, int *has_inline_data);
357395eaefbdSTheodore Ts'o extern struct buffer_head *ext4_get_first_inline_block(struct inode *inode,
357495eaefbdSTheodore Ts'o 					struct ext4_dir_entry_2 **parent_de,
357595eaefbdSTheodore Ts'o 					int *retval);
35765a57bca9SZhang Yi extern void *ext4_read_inline_link(struct inode *inode);
35777046ae35SAndreas Gruenbacher 
35787046ae35SAndreas Gruenbacher struct iomap;
35797046ae35SAndreas Gruenbacher extern int ext4_inline_data_iomap(struct inode *inode, struct iomap *iomap);
35807046ae35SAndreas Gruenbacher 
358101daf945STheodore Ts'o extern int ext4_inline_data_truncate(struct inode *inode, int *has_inline);
358295eaefbdSTheodore Ts'o 
358395eaefbdSTheodore Ts'o extern int ext4_convert_inline_data(struct inode *inode);
358495eaefbdSTheodore Ts'o 
ext4_has_inline_data(struct inode * inode)358583447ccbSZheng Liu static inline int ext4_has_inline_data(struct inode *inode)
358683447ccbSZheng Liu {
358783447ccbSZheng Liu 	return ext4_test_inode_flag(inode, EXT4_INODE_INLINE_DATA) &&
358883447ccbSZheng Liu 	       EXT4_I(inode)->i_inline_off;
358983447ccbSZheng Liu }
359083447ccbSZheng Liu 
35913dcf5451SChristoph Hellwig /* namei.c */
35923dcf5451SChristoph Hellwig extern const struct inode_operations ext4_dir_inode_operations;
35933dcf5451SChristoph Hellwig extern const struct inode_operations ext4_special_inode_operations;
3594596397b7STheodore Ts'o extern struct dentry *ext4_get_parent(struct dentry *child);
3595a774f9c2STao Ma extern struct ext4_dir_entry_2 *ext4_init_dot_dotdot(struct inode *inode,
3596a774f9c2STao Ma 				 struct ext4_dir_entry_2 *de,
3597a774f9c2STao Ma 				 int blocksize, int csum_size,
3598a774f9c2STao Ma 				 unsigned int parent_ino, int dotdot_real_len);
3599ddce3b94STheodore Ts'o extern void ext4_initialize_dirent_tail(struct buffer_head *bh,
36003c47d541STao Ma 					unsigned int blocksize);
3601f036adb3STheodore Ts'o extern int ext4_handle_dirty_dirblock(handle_t *handle, struct inode *inode,
36023c47d541STao Ma 				      struct buffer_head *bh);
36034c0d5778SEric Biggers extern int __ext4_unlink(struct inode *dir, const struct qstr *d_name,
36044c0d5778SEric Biggers 			 struct inode *inode, struct dentry *dentry);
36058016e29fSHarshad Shirwadkar extern int __ext4_link(struct inode *dir, struct inode *inode,
36068016e29fSHarshad Shirwadkar 		       struct dentry *dentry);
3607b886ee3eSGabriel Krisman Bertazi 
36088af0f082STao Ma #define S_SHIFT 12
3609381cebfeSDan Carpenter static const unsigned char ext4_type_by_mode[(S_IFMT >> S_SHIFT) + 1] = {
36108af0f082STao Ma 	[S_IFREG >> S_SHIFT]	= EXT4_FT_REG_FILE,
36118af0f082STao Ma 	[S_IFDIR >> S_SHIFT]	= EXT4_FT_DIR,
36128af0f082STao Ma 	[S_IFCHR >> S_SHIFT]	= EXT4_FT_CHRDEV,
36138af0f082STao Ma 	[S_IFBLK >> S_SHIFT]	= EXT4_FT_BLKDEV,
36148af0f082STao Ma 	[S_IFIFO >> S_SHIFT]	= EXT4_FT_FIFO,
36158af0f082STao Ma 	[S_IFSOCK >> S_SHIFT]	= EXT4_FT_SOCK,
36168af0f082STao Ma 	[S_IFLNK >> S_SHIFT]	= EXT4_FT_SYMLINK,
36178af0f082STao Ma };
36188af0f082STao Ma 
ext4_set_de_type(struct super_block * sb,struct ext4_dir_entry_2 * de,umode_t mode)36198af0f082STao Ma static inline void ext4_set_de_type(struct super_block *sb,
36208af0f082STao Ma 				struct ext4_dir_entry_2 *de,
36218af0f082STao Ma 				umode_t mode) {
3622e2b911c5SDarrick J. Wong 	if (ext4_has_feature_filetype(sb))
36238af0f082STao Ma 		de->file_type = ext4_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
36248af0f082STao Ma }
36258af0f082STao Ma 
3626f64e02feSTheodore Ts'o /* readpages.c */
3627a07f624bSMatthew Wilcox (Oracle) extern int ext4_mpage_readpages(struct inode *inode,
3628c0be8e6fSMatthew Wilcox 		struct readahead_control *rac, struct folio *folio);
362922cfe4b4SEric Biggers extern int __init ext4_init_post_read_processing(void);
363022cfe4b4SEric Biggers extern void ext4_exit_post_read_processing(void);
36313dcf5451SChristoph Hellwig 
36323dcf5451SChristoph Hellwig /* symlink.c */
3633a7a67e8aSAl Viro extern const struct inode_operations ext4_encrypted_symlink_inode_operations;
36343dcf5451SChristoph Hellwig extern const struct inode_operations ext4_symlink_inode_operations;
36353dcf5451SChristoph Hellwig extern const struct inode_operations ext4_fast_symlink_inode_operations;
36363dcf5451SChristoph Hellwig 
3637b5799018STheodore Ts'o /* sysfs.c */
3638d578b994SJonathan Davies extern void ext4_notify_error_sysfs(struct ext4_sb_info *sbi);
3639b5799018STheodore Ts'o extern int ext4_register_sysfs(struct super_block *sb);
3640ebd173beSTheodore Ts'o extern void ext4_unregister_sysfs(struct super_block *sb);
3641b5799018STheodore Ts'o extern int __init ext4_init_sysfs(void);
3642b5799018STheodore Ts'o extern void ext4_exit_sysfs(void);
3643b5799018STheodore Ts'o 
36446fd058f7STheodore Ts'o /* block_validity */
36456fd058f7STheodore Ts'o extern void ext4_release_system_zone(struct super_block *sb);
36466fd058f7STheodore Ts'o extern int ext4_setup_system_zone(struct super_block *sb);
36475dabfc78STheodore Ts'o extern int __init ext4_init_system_zone(void);
36485dabfc78STheodore Ts'o extern void ext4_exit_system_zone(void);
3649ce9f24ccSJan Kara extern int ext4_inode_block_valid(struct inode *inode,
36506fd058f7STheodore Ts'o 				  ext4_fsblk_t start_blk,
36516fd058f7STheodore Ts'o 				  unsigned int count);
36521f7d1e77STheodore Ts'o extern int ext4_check_blockref(const char *, unsigned int,
36531f7d1e77STheodore Ts'o 			       struct inode *, __le32 *, unsigned int);
36546bc6c2bdSRitesh Harjani extern int ext4_sb_block_valid(struct super_block *sb, struct inode *inode,
36556bc6c2bdSRitesh Harjani 				ext4_fsblk_t start_blk, unsigned int count);
36566bc6c2bdSRitesh Harjani 
36576fd058f7STheodore Ts'o 
36583dcf5451SChristoph Hellwig /* extents.c */
36594a092d73STheodore Ts'o struct ext4_ext_path;
36604a092d73STheodore Ts'o struct ext4_extent;
36614a092d73STheodore Ts'o 
3662d7b2a00cSZheng Liu /*
3663d7b2a00cSZheng Liu  * Maximum number of logical blocks in a file; ext4_extent's ee_block is
3664d7b2a00cSZheng Liu  * __le32.
3665d7b2a00cSZheng Liu  */
3666d7b2a00cSZheng Liu #define EXT_MAX_BLOCKS	0xffffffff
3667d7b2a00cSZheng Liu 
36684209ae12SHarshad Shirwadkar extern void ext4_ext_tree_init(handle_t *handle, struct inode *inode);
3669fffb2739SJan Kara extern int ext4_ext_index_trans_blocks(struct inode *inode, int extents);
3670e35fd660STheodore Ts'o extern int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
3671e35fd660STheodore Ts'o 			       struct ext4_map_blocks *map, int flags);
3672d0abb36dSTheodore Ts'o extern int ext4_ext_truncate(handle_t *, struct inode *);
367326a4c0c6STheodore Ts'o extern int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
367426a4c0c6STheodore Ts'o 				 ext4_lblk_t end);
36753dcf5451SChristoph Hellwig extern void ext4_ext_init(struct super_block *);
36763dcf5451SChristoph Hellwig extern void ext4_ext_release(struct super_block *);
36772fe17c10SChristoph Hellwig extern long ext4_fallocate(struct file *file, int mode, loff_t offset,
36783dcf5451SChristoph Hellwig 			  loff_t len);
36796b523df4SJan Kara extern int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode,
36806b523df4SJan Kara 					  loff_t offset, ssize_t len);
3681a00713eaSRitesh Harjani extern int ext4_convert_unwritten_io_end_vec(handle_t *handle,
3682a00713eaSRitesh Harjani 					     ext4_io_end_t *io_end);
3683e35fd660STheodore Ts'o extern int ext4_map_blocks(handle_t *handle, struct inode *inode,
3684e35fd660STheodore Ts'o 			   struct ext4_map_blocks *map, int flags);
36854a092d73STheodore Ts'o extern int ext4_ext_calc_credits_for_single_extent(struct inode *inode,
36864a092d73STheodore Ts'o 						   int num,
36874a092d73STheodore Ts'o 						   struct ext4_ext_path *path);
36884a092d73STheodore Ts'o extern int ext4_ext_insert_extent(handle_t *, struct inode *,
3689dfe50809STheodore Ts'o 				  struct ext4_ext_path **,
36904a092d73STheodore Ts'o 				  struct ext4_extent *, int);
3691ed8a1a76STheodore Ts'o extern struct ext4_ext_path *ext4_find_extent(struct inode *, ext4_lblk_t,
3692705912caSTheodore Ts'o 					      struct ext4_ext_path **,
3693107a7bd3STheodore Ts'o 					      int flags);
36947ff5fddaSYe Bin extern void ext4_free_ext_path(struct ext4_ext_path *);
36954a092d73STheodore Ts'o extern int ext4_ext_check_inode(struct inode *inode);
3696fcf6b1b7SDmitry Monakhov extern ext4_lblk_t ext4_ext_next_allocated_block(struct ext4_ext_path *path);
36973a06d778SAneesh Kumar K.V extern int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
36983a06d778SAneesh Kumar K.V 			__u64 start, __u64 len);
3699bb5835edSTheodore Ts'o extern int ext4_get_es_cache(struct inode *inode,
3700bb5835edSTheodore Ts'o 			     struct fiemap_extent_info *fieinfo,
3701bb5835edSTheodore Ts'o 			     __u64 start, __u64 len);
37027869a4a6STheodore Ts'o extern int ext4_ext_precache(struct inode *inode);
3703fcf6b1b7SDmitry Monakhov extern int ext4_swap_extents(handle_t *handle, struct inode *inode1,
3704fcf6b1b7SDmitry Monakhov 				struct inode *inode2, ext4_lblk_t lblk1,
3705fcf6b1b7SDmitry Monakhov 			     ext4_lblk_t lblk2,  ext4_lblk_t count,
3706fcf6b1b7SDmitry Monakhov 			     int mark_unwritten,int *err);
37070b02f4c0SEric Whitney extern int ext4_clu_mapped(struct inode *inode, ext4_lblk_t lclu);
3708a4130367SJan Kara extern int ext4_datasem_ensure_credits(handle_t *handle, struct inode *inode,
370983448bdfSJan Kara 				       int check_cred, int restart_cred,
371083448bdfSJan Kara 				       int revoke_cred);
37118016e29fSHarshad Shirwadkar extern void ext4_ext_replay_shrink_inode(struct inode *inode, ext4_lblk_t end);
37128016e29fSHarshad Shirwadkar extern int ext4_ext_replay_set_iblocks(struct inode *inode);
37138016e29fSHarshad Shirwadkar extern int ext4_ext_replay_update_ex(struct inode *inode, ext4_lblk_t start,
37148016e29fSHarshad Shirwadkar 		int len, int unwritten, ext4_fsblk_t pblk);
37158016e29fSHarshad Shirwadkar extern int ext4_ext_clear_bb(struct inode *inode);
3716a4130367SJan Kara 
37174a092d73STheodore Ts'o 
3718748de673SAkira Fujita /* move_extent.c */
3719393d1d1dSDr. Tilmann Bubeck extern void ext4_double_down_write_data_sem(struct inode *first,
3720393d1d1dSDr. Tilmann Bubeck 					    struct inode *second);
3721393d1d1dSDr. Tilmann Bubeck extern void ext4_double_up_write_data_sem(struct inode *orig_inode,
3722393d1d1dSDr. Tilmann Bubeck 					  struct inode *donor_inode);
3723748de673SAkira Fujita extern int ext4_move_extents(struct file *o_filp, struct file *d_filp,
3724748de673SAkira Fujita 			     __u64 start_orig, __u64 start_donor,
3725748de673SAkira Fujita 			     __u64 len, __u64 *moved_len);
3726748de673SAkira Fujita 
3727bd2d0210STheodore Ts'o /* page-io.c */
37285dabfc78STheodore Ts'o extern int __init ext4_init_pageio(void);
37295dabfc78STheodore Ts'o extern void ext4_exit_pageio(void);
3730bd2d0210STheodore Ts'o extern ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags);
373197a851edSJan Kara extern ext4_io_end_t *ext4_get_io_end(ext4_io_end_t *io_end);
373297a851edSJan Kara extern int ext4_put_io_end(ext4_io_end_t *io_end);
373397a851edSJan Kara extern void ext4_put_io_end_defer(ext4_io_end_t *io_end);
373497a851edSJan Kara extern void ext4_io_submit_init(struct ext4_io_submit *io,
373597a851edSJan Kara 				struct writeback_control *wbc);
37362e8fa54eSJan Kara extern void ext4_end_io_rsv_work(struct work_struct *work);
3737bd2d0210STheodore Ts'o extern void ext4_io_submit(struct ext4_io_submit *io);
3738e8d6062cSMatthew Wilcox int ext4_bio_write_folio(struct ext4_io_submit *io, struct folio *page,
3739e8d6062cSMatthew Wilcox 		size_t len);
3740c8cc8816SRitesh Harjani extern struct ext4_io_end_vec *ext4_alloc_io_end_vec(ext4_io_end_t *io_end);
3741c8cc8816SRitesh Harjani extern struct ext4_io_end_vec *ext4_last_io_end_vec(ext4_io_end_t *io_end);
37423a06d778SAneesh Kumar K.V 
3743c5e06d10SJohann Lombardi /* mmp.c */
3744c5e06d10SJohann Lombardi extern int ext4_multi_mount_protect(struct super_block *, ext4_fsblk_t);
3745c5e06d10SJohann Lombardi 
3746618f0031SPavel Skripkin /* mmp.c */
3747618f0031SPavel Skripkin extern void ext4_stop_mmpd(struct ext4_sb_info *sbi);
3748618f0031SPavel Skripkin 
3749c93d8f88SEric Biggers /* verity.c */
3750c93d8f88SEric Biggers extern const struct fsverity_operations ext4_verityops;
3751c93d8f88SEric Biggers 
375225c6d98fSJan Kara /* orphan.c */
375325c6d98fSJan Kara extern int ext4_orphan_add(handle_t *, struct inode *);
375425c6d98fSJan Kara extern int ext4_orphan_del(handle_t *, struct inode *);
375525c6d98fSJan Kara extern void ext4_orphan_cleanup(struct super_block *sb,
375625c6d98fSJan Kara 				struct ext4_super_block *es);
375702f310fcSJan Kara extern void ext4_release_orphan_info(struct super_block *sb);
375802f310fcSJan Kara extern int ext4_init_orphan_info(struct super_block *sb);
375902f310fcSJan Kara extern int ext4_orphan_file_empty(struct super_block *sb);
376002f310fcSJan Kara extern void ext4_orphan_file_block_trigger(
376102f310fcSJan Kara 				struct jbd2_buffer_trigger_type *triggers,
376202f310fcSJan Kara 				struct buffer_head *bh,
376302f310fcSJan Kara 				void *data, size_t size);
376425c6d98fSJan Kara 
37653613d228SJan Kara /*
376648fc7f7eSAdam Buchbinder  * Add new method to test whether block and inode bitmaps are properly
37672ccb5fb9SAneesh Kumar K.V  * initialized. With uninit_bg reading the block from disk is not enough
37682ccb5fb9SAneesh Kumar K.V  * to mark the bitmap uptodate. We need to also zero-out the bitmap
37692ccb5fb9SAneesh Kumar K.V  */
37702ccb5fb9SAneesh Kumar K.V #define BH_BITMAP_UPTODATE BH_JBDPrivateStart
37712ccb5fb9SAneesh Kumar K.V 
bitmap_uptodate(struct buffer_head * bh)37722ccb5fb9SAneesh Kumar K.V static inline int bitmap_uptodate(struct buffer_head *bh)
37732ccb5fb9SAneesh Kumar K.V {
37742ccb5fb9SAneesh Kumar K.V 	return (buffer_uptodate(bh) &&
37752ccb5fb9SAneesh Kumar K.V 			test_bit(BH_BITMAP_UPTODATE, &(bh)->b_state));
37762ccb5fb9SAneesh Kumar K.V }
set_bitmap_uptodate(struct buffer_head * bh)37772ccb5fb9SAneesh Kumar K.V static inline void set_bitmap_uptodate(struct buffer_head *bh)
37782ccb5fb9SAneesh Kumar K.V {
37792ccb5fb9SAneesh Kumar K.V 	set_bit(BH_BITMAP_UPTODATE, &(bh)->b_state);
37802ccb5fb9SAneesh Kumar K.V }
37812ccb5fb9SAneesh Kumar K.V 
3782e9e3bcecSEric Sandeen /* For ioend & aio unwritten conversion wait queues */
3783e9e3bcecSEric Sandeen #define EXT4_WQ_HASH_SZ		37
3784e9e3bcecSEric Sandeen #define ext4_ioend_wq(v)   (&ext4__ioend_wq[((unsigned long)(v)) %\
3785e9e3bcecSEric Sandeen 					    EXT4_WQ_HASH_SZ])
3786e9e3bcecSEric Sandeen extern wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
3787e9e3bcecSEric Sandeen 
37888f82f840SYongqiang Yang extern int ext4_resize_begin(struct super_block *sb);
3789827891a3STheodore Ts'o extern int ext4_resize_end(struct super_block *sb, bool update_backups);
37908f82f840SYongqiang Yang 
ext4_set_io_unwritten_flag(struct inode * inode,struct ext4_io_end * io_end)379174c66bcbSJan Kara static inline void ext4_set_io_unwritten_flag(struct inode *inode,
379274c66bcbSJan Kara 					      struct ext4_io_end *io_end)
379374c66bcbSJan Kara {
379474c66bcbSJan Kara 	if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) {
379574c66bcbSJan Kara 		io_end->flag |= EXT4_IO_END_UNWRITTEN;
379674c66bcbSJan Kara 		atomic_inc(&EXT4_I(inode)->i_unwritten);
379774c66bcbSJan Kara 	}
379874c66bcbSJan Kara }
379974c66bcbSJan Kara 
ext4_clear_io_unwritten_flag(ext4_io_end_t * io_end)380074c66bcbSJan Kara static inline void ext4_clear_io_unwritten_flag(ext4_io_end_t *io_end)
380174c66bcbSJan Kara {
380274c66bcbSJan Kara 	struct inode *inode = io_end->inode;
380374c66bcbSJan Kara 
380474c66bcbSJan Kara 	if (io_end->flag & EXT4_IO_END_UNWRITTEN) {
380574c66bcbSJan Kara 		io_end->flag &= ~EXT4_IO_END_UNWRITTEN;
380674c66bcbSJan Kara 		/* Wake up anyone waiting on unwritten extent conversion */
380774c66bcbSJan Kara 		if (atomic_dec_and_test(&EXT4_I(inode)->i_unwritten))
380874c66bcbSJan Kara 			wake_up_all(ext4_ioend_wq(inode));
380974c66bcbSJan Kara 	}
381074c66bcbSJan Kara }
381174c66bcbSJan Kara 
38128ff6daa1SChristoph Hellwig extern const struct iomap_ops ext4_iomap_ops;
38138cd115bdSJan Kara extern const struct iomap_ops ext4_iomap_overwrite_ops;
381409edf4d3SMatthew Bobrowski extern const struct iomap_ops ext4_iomap_report_ops;
3815364443cbSJan Kara 
ext4_buffer_uptodate(struct buffer_head * bh)38167963e5acSZhangXiaoxu static inline int ext4_buffer_uptodate(struct buffer_head *bh)
38177963e5acSZhangXiaoxu {
38187963e5acSZhangXiaoxu 	/*
38197963e5acSZhangXiaoxu 	 * If the buffer has the write error flag, we have failed
38207963e5acSZhangXiaoxu 	 * to write out data in the block.  In this  case, we don't
38217963e5acSZhangXiaoxu 	 * have to read the block because we may read the old data
38227963e5acSZhangXiaoxu 	 * successfully.
38237963e5acSZhangXiaoxu 	 */
38245c680150SJoseph Qi 	if (buffer_write_io_error(bh))
38257963e5acSZhangXiaoxu 		set_buffer_uptodate(bh);
38267963e5acSZhangXiaoxu 	return buffer_uptodate(bh);
38277963e5acSZhangXiaoxu }
38287963e5acSZhangXiaoxu 
38293dcf5451SChristoph Hellwig #endif	/* __KERNEL__ */
38303dcf5451SChristoph Hellwig 
38316a797d27SDarrick J. Wong #define EFSBADCRC	EBADMSG		/* Bad CRC detected */
38326a797d27SDarrick J. Wong #define EFSCORRUPTED	EUCLEAN		/* Filesystem is corrupted */
38336a797d27SDarrick J. Wong 
38343dcf5451SChristoph Hellwig #endif	/* _EXT4_H */
3835