xref: /openbmc/linux/fs/f2fs/f2fs.h (revision ecc23d0a422a3118fcf6e4f0a46e17a6c2047b02)
1d29fbcdbSNishad Kamdar /* SPDX-License-Identifier: GPL-2.0 */
20a8165d7SJaegeuk Kim /*
339a53e0cSJaegeuk Kim  * fs/f2fs/f2fs.h
439a53e0cSJaegeuk Kim  *
539a53e0cSJaegeuk Kim  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
639a53e0cSJaegeuk Kim  *             http://www.samsung.com/
739a53e0cSJaegeuk Kim  */
839a53e0cSJaegeuk Kim #ifndef _LINUX_F2FS_H
939a53e0cSJaegeuk Kim #define _LINUX_F2FS_H
1039a53e0cSJaegeuk Kim 
11f847c699SChao Yu #include <linux/uio.h>
1239a53e0cSJaegeuk Kim #include <linux/types.h>
1339a53e0cSJaegeuk Kim #include <linux/page-flags.h>
1439a53e0cSJaegeuk Kim #include <linux/buffer_head.h>
1539a53e0cSJaegeuk Kim #include <linux/slab.h>
1639a53e0cSJaegeuk Kim #include <linux/crc32.h>
1739a53e0cSJaegeuk Kim #include <linux/magic.h>
18c2d715d1SJaegeuk Kim #include <linux/kobject.h>
197bd59381SGu Zheng #include <linux/sched.h>
207c2e5963SJaegeuk Kim #include <linux/cred.h>
21df808180SMatthew Wilcox (Oracle) #include <linux/sched/mm.h>
2239307a8eSJaegeuk Kim #include <linux/vmalloc.h>
23740432f8SJaegeuk Kim #include <linux/bio.h>
24d0239e1bSJaegeuk Kim #include <linux/blkdev.h>
250abd675eSChao Yu #include <linux/quotaops.h>
26c6a564ffSChristoph Hellwig #include <linux/part_stat.h>
2743b6573bSKeith Mok #include <crypto/hash.h>
2839a53e0cSJaegeuk Kim 
29734f0d24SDave Chinner #include <linux/fscrypt.h>
3095ae251fSEric Biggers #include <linux/fsverity.h>
31734f0d24SDave Chinner 
3251dcbdacSMatthew Wilcox (Oracle) struct pagevec;
3351dcbdacSMatthew Wilcox (Oracle) 
345d56b671SJaegeuk Kim #ifdef CONFIG_F2FS_CHECK_FS
359850cf4aSJaegeuk Kim #define f2fs_bug_on(sbi, condition)	BUG_ON(condition)
365d56b671SJaegeuk Kim #else
379850cf4aSJaegeuk Kim #define f2fs_bug_on(sbi, condition)					\
389850cf4aSJaegeuk Kim 	do {								\
39db489652SYangtao Li 		if (WARN_ON(condition))					\
40caf0047eSChao Yu 			set_sbi_flag(sbi, SBI_NEED_FSCK);		\
419850cf4aSJaegeuk Kim 	} while (0)
425d56b671SJaegeuk Kim #endif
435d56b671SJaegeuk Kim 
442c63feadSJaegeuk Kim enum {
452c63feadSJaegeuk Kim 	FAULT_KMALLOC,
46628b3d14SChao Yu 	FAULT_KVMALLOC,
47c41f3cc3SJaegeuk Kim 	FAULT_PAGE_ALLOC,
4801eccef7SChao Yu 	FAULT_PAGE_GET,
49b96d9b3bSChao Yu 	FAULT_ALLOC_BIO,	/* it's obsolete due to bio_alloc() will never fail */
50cb78942bSJaegeuk Kim 	FAULT_ALLOC_NID,
51cb78942bSJaegeuk Kim 	FAULT_ORPHAN,
52cb78942bSJaegeuk Kim 	FAULT_BLOCK,
53cb78942bSJaegeuk Kim 	FAULT_DIR_DEPTH,
5453aa6bbfSJaegeuk Kim 	FAULT_EVICT_INODE,
5514b44d23SJaegeuk Kim 	FAULT_TRUNCATE,
566f5c2ed0SChao Yu 	FAULT_READ_IO,
570f348028SChao Yu 	FAULT_CHECKPOINT,
58b83dcfe6SChao Yu 	FAULT_DISCARD,
596f5c2ed0SChao Yu 	FAULT_WRITE_IO,
6032410577SChao Yu 	FAULT_SLAB_ALLOC,
6110a26878SChao Yu 	FAULT_DQUOT_INIT,
623e020389SChao Yu 	FAULT_LOCK_OP,
6318792e64SChao Yu 	FAULT_BLKADDR,
642c63feadSJaegeuk Kim 	FAULT_MAX,
652c63feadSJaegeuk Kim };
662c63feadSJaegeuk Kim 
677fa750a1SArnd Bergmann #ifdef CONFIG_F2FS_FAULT_INJECTION
68447286ebSYangtao Li #define F2FS_ALL_FAULT_TYPE		(GENMASK(FAULT_MAX - 1, 0))
69d494500aSChao Yu 
7008796897SSheng Yong struct f2fs_fault_info {
7108796897SSheng Yong 	atomic_t inject_ops;
7244958ca9SChao Yu 	int inject_rate;
7308796897SSheng Yong 	unsigned int inject_type;
7408796897SSheng Yong };
7508796897SSheng Yong 
7619880e6eSAlexey Dobriyan extern const char *f2fs_fault_name[FAULT_MAX];
77447286ebSYangtao Li #define IS_FAULT_SET(fi, type) ((fi)->inject_type & BIT(type))
788844b2f8SChao Yu 
798844b2f8SChao Yu /* maximum retry count for injected failure */
808844b2f8SChao Yu #define DEFAULT_FAILURE_RETRY_COUNT		8
818844b2f8SChao Yu #else
828844b2f8SChao Yu #define DEFAULT_FAILURE_RETRY_COUNT		1
832c63feadSJaegeuk Kim #endif
842c63feadSJaegeuk Kim 
8539a53e0cSJaegeuk Kim /*
8639a53e0cSJaegeuk Kim  * For mount options
8739a53e0cSJaegeuk Kim  */
88478d7100SChao Yu #define F2FS_MOUNT_DISABLE_ROLL_FORWARD	0x00000001
89478d7100SChao Yu #define F2FS_MOUNT_DISCARD		0x00000002
90478d7100SChao Yu #define F2FS_MOUNT_NOHEAP		0x00000004
91478d7100SChao Yu #define F2FS_MOUNT_XATTR_USER		0x00000008
92478d7100SChao Yu #define F2FS_MOUNT_POSIX_ACL		0x00000010
93478d7100SChao Yu #define F2FS_MOUNT_DISABLE_EXT_IDENTIFY	0x00000020
94478d7100SChao Yu #define F2FS_MOUNT_INLINE_XATTR		0x00000040
95478d7100SChao Yu #define F2FS_MOUNT_INLINE_DATA		0x00000080
96478d7100SChao Yu #define F2FS_MOUNT_INLINE_DENTRY	0x00000100
97478d7100SChao Yu #define F2FS_MOUNT_FLUSH_MERGE		0x00000200
98478d7100SChao Yu #define F2FS_MOUNT_NOBARRIER		0x00000400
99478d7100SChao Yu #define F2FS_MOUNT_FASTBOOT		0x00000800
100478d7100SChao Yu #define F2FS_MOUNT_READ_EXTENT_CACHE	0x00001000
101478d7100SChao Yu #define F2FS_MOUNT_DATA_FLUSH		0x00002000
102478d7100SChao Yu #define F2FS_MOUNT_FAULT_INJECTION	0x00004000
103478d7100SChao Yu #define F2FS_MOUNT_USRQUOTA		0x00008000
104478d7100SChao Yu #define F2FS_MOUNT_GRPQUOTA		0x00010000
105478d7100SChao Yu #define F2FS_MOUNT_PRJQUOTA		0x00020000
106478d7100SChao Yu #define F2FS_MOUNT_QUOTA		0x00040000
107478d7100SChao Yu #define F2FS_MOUNT_INLINE_XATTR_SIZE	0x00080000
108478d7100SChao Yu #define F2FS_MOUNT_RESERVE_ROOT		0x00100000
109478d7100SChao Yu #define F2FS_MOUNT_DISABLE_CHECKPOINT	0x00200000
110478d7100SChao Yu #define F2FS_MOUNT_NORECOVERY		0x00400000
111478d7100SChao Yu #define F2FS_MOUNT_ATGC			0x00800000
112478d7100SChao Yu #define F2FS_MOUNT_MERGE_CHECKPOINT	0x01000000
113478d7100SChao Yu #define	F2FS_MOUNT_GC_MERGE		0x02000000
114478d7100SChao Yu #define F2FS_MOUNT_COMPRESS_CACHE	0x04000000
115478d7100SChao Yu #define F2FS_MOUNT_AGE_EXTENT_CACHE	0x08000000
11639a53e0cSJaegeuk Kim 
11763189b78SChao Yu #define F2FS_OPTION(sbi)	((sbi)->mount_opt)
11863189b78SChao Yu #define clear_opt(sbi, option)	(F2FS_OPTION(sbi).opt &= ~F2FS_MOUNT_##option)
11963189b78SChao Yu #define set_opt(sbi, option)	(F2FS_OPTION(sbi).opt |= F2FS_MOUNT_##option)
12063189b78SChao Yu #define test_opt(sbi, option)	(F2FS_OPTION(sbi).opt & F2FS_MOUNT_##option)
12139a53e0cSJaegeuk Kim 
12239a53e0cSJaegeuk Kim #define ver_after(a, b)	(typecheck(unsigned long long, a) &&		\
12339a53e0cSJaegeuk Kim 		typecheck(unsigned long long, b) &&			\
12439a53e0cSJaegeuk Kim 		((long long)((a) - (b)) > 0))
12539a53e0cSJaegeuk Kim 
126a9841c4dSJaegeuk Kim typedef u32 block_t;	/*
127a9841c4dSJaegeuk Kim 			 * should not change u32, since it is the on-disk block
128a9841c4dSJaegeuk Kim 			 * address format, __le32.
129a9841c4dSJaegeuk Kim 			 */
13039a53e0cSJaegeuk Kim typedef u32 nid_t;
13139a53e0cSJaegeuk Kim 
1324c8ff709SChao Yu #define COMPRESS_EXT_NUM		16
1334c8ff709SChao Yu 
134e4544b63STim Murray /*
135e4544b63STim Murray  * An implementation of an rwsem that is explicitly unfair to readers. This
136e4544b63STim Murray  * prevents priority inversion when a low-priority reader acquires the read lock
137e4544b63STim Murray  * while sleeping on the write lock but the write lock is needed by
138e4544b63STim Murray  * higher-priority clients.
139e4544b63STim Murray  */
140e4544b63STim Murray 
141e4544b63STim Murray struct f2fs_rwsem {
142e4544b63STim Murray         struct rw_semaphore internal_rwsem;
1437f8e249dSJaegeuk Kim #ifdef CONFIG_F2FS_UNFAIR_RWSEM
144e4544b63STim Murray         wait_queue_head_t read_waiters;
1457f8e249dSJaegeuk Kim #endif
146e4544b63STim Murray };
147e4544b63STim Murray 
14839a53e0cSJaegeuk Kim struct f2fs_mount_info {
14939a53e0cSJaegeuk Kim 	unsigned int opt;
15063189b78SChao Yu 	block_t root_reserved_blocks;	/* root reserved blocks */
15163189b78SChao Yu 	kuid_t s_resuid;		/* reserved blocks for uid */
15263189b78SChao Yu 	kgid_t s_resgid;		/* reserved blocks for gid */
15363189b78SChao Yu 	int active_logs;		/* # of active logs */
15463189b78SChao Yu 	int inline_xattr_size;		/* inline xattr size */
15563189b78SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
15663189b78SChao Yu 	struct f2fs_fault_info fault_info;	/* For fault injection */
15763189b78SChao Yu #endif
15863189b78SChao Yu #ifdef CONFIG_QUOTA
15963189b78SChao Yu 	/* Names of quota files with journalled quota */
16063189b78SChao Yu 	char *s_qf_names[MAXQUOTAS];
16163189b78SChao Yu 	int s_jquota_fmt;			/* Format of quota to use */
16263189b78SChao Yu #endif
16363189b78SChao Yu 	/* For which write hints are passed down to block layer */
16463189b78SChao Yu 	int alloc_mode;			/* segment allocation policy */
16563189b78SChao Yu 	int fsync_mode;			/* fsync policy */
166b0332a0fSChao Yu 	int fs_mode;			/* fs mode: LFS or ADAPTIVE */
167bbbc34fdSChao Yu 	int bggc_mode;			/* bggc mode: off, on or sync */
1687a8fc586SDaeho Jeong 	int memory_mode;		/* memory mode */
169b62e71beSChao Yu 	int errors;			/* errors parameter */
1704f993264SChao Yu 	int discard_unit;		/*
1714f993264SChao Yu 					 * discard command's offset/size should
1724f993264SChao Yu 					 * be aligned to this unit: block,
1734f993264SChao Yu 					 * segment or section
1744f993264SChao Yu 					 */
175ac4acb1fSEric Biggers 	struct fscrypt_dummy_policy dummy_enc_policy; /* test dummy encryption */
1761ae18f71SJaegeuk Kim 	block_t unusable_cap_perc;	/* percentage for cap */
1774d3aed70SDaniel Rosenberg 	block_t unusable_cap;		/* Amount of space allowed to be
1784d3aed70SDaniel Rosenberg 					 * unusable when disabling checkpoint
1794d3aed70SDaniel Rosenberg 					 */
1804c8ff709SChao Yu 
1814c8ff709SChao Yu 	/* For compression */
1824c8ff709SChao Yu 	unsigned char compress_algorithm;	/* algorithm type */
183b28f047bSChao Yu 	unsigned char compress_log_size;	/* cluster log size */
1843fde13f8SChao Yu 	unsigned char compress_level;		/* compress level */
185b28f047bSChao Yu 	bool compress_chksum;			/* compressed data chksum */
1864c8ff709SChao Yu 	unsigned char compress_ext_cnt;		/* extension count */
187151b1982SFengnan Chang 	unsigned char nocompress_ext_cnt;		/* nocompress extension count */
188602a16d5SDaeho Jeong 	int compress_mode;			/* compression mode */
1894c8ff709SChao Yu 	unsigned char extensions[COMPRESS_EXT_NUM][F2FS_EXTENSION_LEN];	/* extensions */
190151b1982SFengnan Chang 	unsigned char noextensions[COMPRESS_EXT_NUM][F2FS_EXTENSION_LEN]; /* extensions */
19139a53e0cSJaegeuk Kim };
19239a53e0cSJaegeuk Kim 
19377e820eaSChao Yu #define F2FS_FEATURE_ENCRYPT			0x00000001
19477e820eaSChao Yu #define F2FS_FEATURE_BLKZONED			0x00000002
19577e820eaSChao Yu #define F2FS_FEATURE_ATOMIC_WRITE		0x00000004
19677e820eaSChao Yu #define F2FS_FEATURE_EXTRA_ATTR			0x00000008
19777e820eaSChao Yu #define F2FS_FEATURE_PRJQUOTA			0x00000010
19877e820eaSChao Yu #define F2FS_FEATURE_INODE_CHKSUM		0x00000020
19977e820eaSChao Yu #define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR	0x00000040
20077e820eaSChao Yu #define F2FS_FEATURE_QUOTA_INO			0x00000080
20177e820eaSChao Yu #define F2FS_FEATURE_INODE_CRTIME		0x00000100
20277e820eaSChao Yu #define F2FS_FEATURE_LOST_FOUND			0x00000200
20377e820eaSChao Yu #define F2FS_FEATURE_VERITY			0x00000400
20477e820eaSChao Yu #define F2FS_FEATURE_SB_CHKSUM			0x00000800
20577e820eaSChao Yu #define F2FS_FEATURE_CASEFOLD			0x00001000
20677e820eaSChao Yu #define F2FS_FEATURE_COMPRESSION		0x00002000
20777e820eaSChao Yu #define F2FS_FEATURE_RO				0x00004000
208cde4de12SJaegeuk Kim 
2097beb01f7SChao Yu #define __F2FS_HAS_FEATURE(raw_super, mask)				\
2107beb01f7SChao Yu 	((raw_super->feature & cpu_to_le32(mask)) != 0)
2117beb01f7SChao Yu #define F2FS_HAS_FEATURE(sbi, mask)	__F2FS_HAS_FEATURE(sbi->raw_super, mask)
21276f105a2SJaegeuk Kim 
21339a53e0cSJaegeuk Kim /*
2147c2e5963SJaegeuk Kim  * Default values for user and/or group using reserved blocks
2157c2e5963SJaegeuk Kim  */
2167c2e5963SJaegeuk Kim #define	F2FS_DEF_RESUID		0
2177c2e5963SJaegeuk Kim #define	F2FS_DEF_RESGID		0
2187c2e5963SJaegeuk Kim 
2197c2e5963SJaegeuk Kim /*
22039a53e0cSJaegeuk Kim  * For checkpoint manager
22139a53e0cSJaegeuk Kim  */
22239a53e0cSJaegeuk Kim enum {
22339a53e0cSJaegeuk Kim 	NAT_BITMAP,
22439a53e0cSJaegeuk Kim 	SIT_BITMAP
22539a53e0cSJaegeuk Kim };
22639a53e0cSJaegeuk Kim 
227c473f1a9SChao Yu #define	CP_UMOUNT	0x00000001
228c473f1a9SChao Yu #define	CP_FASTBOOT	0x00000002
229c473f1a9SChao Yu #define	CP_SYNC		0x00000004
230c473f1a9SChao Yu #define	CP_RECOVERY	0x00000008
231c473f1a9SChao Yu #define	CP_DISCARD	0x00000010
2321f43e2adSChao Yu #define CP_TRIMMED	0x00000020
2334354994fSDaniel Rosenberg #define CP_PAUSE	0x00000040
234b4b10061SJaegeuk Kim #define CP_RESIZE 	0x00000080
23575ab4cb8SJaegeuk Kim 
236ecc9aa00SChao Yu #define DEF_MAX_DISCARD_REQUEST		8	/* issue 8 discards per round */
237969d1b18SChao Yu #define DEF_MIN_DISCARD_ISSUE_TIME	50	/* 50 ms, if exists */
238f9d1dcedSYunlei He #define DEF_MID_DISCARD_ISSUE_TIME	500	/* 500 ms, if device busy */
239969d1b18SChao Yu #define DEF_MAX_DISCARD_ISSUE_TIME	60000	/* 60 s, if no candidates */
2408bb4f253SJaegeuk Kim #define DEF_DISCARD_URGENT_UTIL		80	/* do more discard over 80% */
24160b99b48SJaegeuk Kim #define DEF_CP_INTERVAL			60	/* 60 secs */
242dcf25fe8SChao Yu #define DEF_IDLE_INTERVAL		5	/* 5 secs */
2434354994fSDaniel Rosenberg #define DEF_DISABLE_INTERVAL		5	/* 5 secs */
244db610a64SJaegeuk Kim #define DEF_DISABLE_QUICK_INTERVAL	1	/* 1 secs */
24503f2c02dSJaegeuk Kim #define DEF_UMOUNT_DISCARD_TIMEOUT	5	/* 5 secs */
246bba681cbSJaegeuk Kim 
24775ab4cb8SJaegeuk Kim struct cp_control {
24875ab4cb8SJaegeuk Kim 	int reason;
2494b2fecc8SJaegeuk Kim 	__u64 trim_start;
2504b2fecc8SJaegeuk Kim 	__u64 trim_end;
2514b2fecc8SJaegeuk Kim 	__u64 trim_minlen;
25275ab4cb8SJaegeuk Kim };
25375ab4cb8SJaegeuk Kim 
254662befdaSChao Yu /*
255e1da7872SChao Yu  * indicate meta/data type
256662befdaSChao Yu  */
257662befdaSChao Yu enum {
258662befdaSChao Yu 	META_CP,
259662befdaSChao Yu 	META_NAT,
26081c1a0f1SChao Yu 	META_SIT,
2614c521f49SJaegeuk Kim 	META_SSA,
262b63e7be5SChao Yu 	META_MAX,
2634c521f49SJaegeuk Kim 	META_POR,
26493770ab7SChao Yu 	DATA_GENERIC,		/* check range only */
26593770ab7SChao Yu 	DATA_GENERIC_ENHANCE,	/* strong check on range and segment bitmap */
26693770ab7SChao Yu 	DATA_GENERIC_ENHANCE_READ,	/*
26793770ab7SChao Yu 					 * strong check on range and segment
26893770ab7SChao Yu 					 * bitmap but no warning due to race
26993770ab7SChao Yu 					 * condition of read on truncated area
27093770ab7SChao Yu 					 * by extent_cache
27193770ab7SChao Yu 					 */
2720ef4ca04SChao Yu 	DATA_GENERIC_ENHANCE_UPDATE,	/*
2730ef4ca04SChao Yu 					 * strong check on range and segment
2740ef4ca04SChao Yu 					 * bitmap for update case
2750ef4ca04SChao Yu 					 */
276e1da7872SChao Yu 	META_GENERIC,
277662befdaSChao Yu };
278662befdaSChao Yu 
2796451e041SJaegeuk Kim /* for the list of ino */
2806451e041SJaegeuk Kim enum {
2816451e041SJaegeuk Kim 	ORPHAN_INO,		/* for orphan ino list */
282fff04f90SJaegeuk Kim 	APPEND_INO,		/* for append ino list */
283fff04f90SJaegeuk Kim 	UPDATE_INO,		/* for update ino list */
284d382e369SYonggil Song 	TRANS_DIR_INO,		/* for transactions dir ino list */
2856c59f87eSChao Yu 	XATTR_DIR_INO,		/* for xattr updated dir ino list */
28639d787beSChao Yu 	FLUSH_INO,		/* for multiple device flushing */
2876451e041SJaegeuk Kim 	MAX_INO_ENTRY,		/* max. list */
2886451e041SJaegeuk Kim };
2896451e041SJaegeuk Kim 
2906451e041SJaegeuk Kim struct ino_entry {
29139a53e0cSJaegeuk Kim 	struct list_head list;		/* list head */
29239a53e0cSJaegeuk Kim 	nid_t ino;			/* inode number */
29339d787beSChao Yu 	unsigned int dirty_device;	/* dirty device bitmap */
29439a53e0cSJaegeuk Kim };
29539a53e0cSJaegeuk Kim 
2962710fd7eSChao Yu /* for the list of inodes to be GCed */
29706292073SChao Yu struct inode_entry {
29839a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
29939a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
30039a53e0cSJaegeuk Kim };
30139a53e0cSJaegeuk Kim 
30250fa53ecSChao Yu struct fsync_node_entry {
30350fa53ecSChao Yu 	struct list_head list;	/* list head */
30450fa53ecSChao Yu 	struct page *page;	/* warm node page pointer */
30550fa53ecSChao Yu 	unsigned int seq_id;	/* sequence id */
30650fa53ecSChao Yu };
30750fa53ecSChao Yu 
308261eeb9cSDaeho Jeong struct ckpt_req {
309261eeb9cSDaeho Jeong 	struct completion wait;		/* completion for checkpoint done */
310261eeb9cSDaeho Jeong 	struct llist_node llnode;	/* llist_node to be linked in wait queue */
311261eeb9cSDaeho Jeong 	int ret;			/* return code of checkpoint */
312261eeb9cSDaeho Jeong 	ktime_t queue_time;		/* request queued time */
313261eeb9cSDaeho Jeong };
314261eeb9cSDaeho Jeong 
315261eeb9cSDaeho Jeong struct ckpt_req_control {
316261eeb9cSDaeho Jeong 	struct task_struct *f2fs_issue_ckpt;	/* checkpoint task */
317e6592066SDaeho Jeong 	int ckpt_thread_ioprio;			/* checkpoint merge thread ioprio */
318261eeb9cSDaeho Jeong 	wait_queue_head_t ckpt_wait_queue;	/* waiting queue for wake-up */
319261eeb9cSDaeho Jeong 	atomic_t issued_ckpt;		/* # of actually issued ckpts */
320261eeb9cSDaeho Jeong 	atomic_t total_ckpt;		/* # of total ckpts */
321261eeb9cSDaeho Jeong 	atomic_t queued_ckpt;		/* # of queued ckpts */
322261eeb9cSDaeho Jeong 	struct llist_head issue_list;	/* list for command issue */
323261eeb9cSDaeho Jeong 	spinlock_t stat_lock;		/* lock for below checkpoint time stats */
324261eeb9cSDaeho Jeong 	unsigned int cur_time;		/* cur wait time in msec for currently issued checkpoint */
325261eeb9cSDaeho Jeong 	unsigned int peak_time;		/* peak wait time in msec until now */
326261eeb9cSDaeho Jeong };
327261eeb9cSDaeho Jeong 
328a7eeb823SChao Yu /* for the bitmap indicate blocks to be discarded */
3297fd9e544SJaegeuk Kim struct discard_entry {
3307fd9e544SJaegeuk Kim 	struct list_head list;	/* list head */
331a7eeb823SChao Yu 	block_t start_blkaddr;	/* start blockaddr of current segment */
332a7eeb823SChao Yu 	unsigned char discard_map[SIT_VBLOCK_MAP_SIZE];	/* segment discard bitmap */
3337fd9e544SJaegeuk Kim };
3347fd9e544SJaegeuk Kim 
3351cd2e6d5SYangtao Li /* minimum discard granularity, unit: block count */
3361cd2e6d5SYangtao Li #define MIN_DISCARD_GRANULARITY		1
337969d1b18SChao Yu /* default discard granularity of inner discard thread, unit: block count */
338969d1b18SChao Yu #define DEFAULT_DISCARD_GRANULARITY		16
339c46867e9SYangtao Li /* default maximum discard granularity of ordered discard, unit: block count */
340c46867e9SYangtao Li #define DEFAULT_MAX_ORDERED_DISCARD_GRANULARITY	16
341969d1b18SChao Yu 
342ba48a33eSChao Yu /* max discard pend list number */
343ba48a33eSChao Yu #define MAX_PLIST_NUM		512
344ba48a33eSChao Yu #define plist_idx(blk_num)	((blk_num) >= MAX_PLIST_NUM ?		\
3452f84babfSSheng Yong 					(MAX_PLIST_NUM - 1) : ((blk_num) - 1))
346ba48a33eSChao Yu 
34715469963SJaegeuk Kim enum {
34835ec7d57SChao Yu 	D_PREP,			/* initial */
34935ec7d57SChao Yu 	D_PARTIAL,		/* partially submitted */
35035ec7d57SChao Yu 	D_SUBMIT,		/* all submitted */
35135ec7d57SChao Yu 	D_DONE,			/* finished */
35215469963SJaegeuk Kim };
35315469963SJaegeuk Kim 
354004b6862SChao Yu struct discard_info {
355b01a9201SJaegeuk Kim 	block_t lstart;			/* logical start address */
356b01a9201SJaegeuk Kim 	block_t len;			/* length */
357004b6862SChao Yu 	block_t start;			/* actual start address in dev */
358004b6862SChao Yu };
359004b6862SChao Yu 
360b01a9201SJaegeuk Kim struct discard_cmd {
361004b6862SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
362004b6862SChao Yu 	struct discard_info di;		/* discard info */
363b01a9201SJaegeuk Kim 	struct list_head list;		/* command list */
364b01a9201SJaegeuk Kim 	struct completion wait;		/* compleation */
365c81abe34SJaegeuk Kim 	struct block_device *bdev;	/* bdev */
366ec9895adSChao Yu 	unsigned short ref;		/* reference count */
3679a744b92SChao Yu 	unsigned char state;		/* state */
36872691af6SJaegeuk Kim 	unsigned char queued;		/* queued discard */
369c81abe34SJaegeuk Kim 	int error;			/* bio error */
37035ec7d57SChao Yu 	spinlock_t lock;		/* for state/bio_ref updating */
37135ec7d57SChao Yu 	unsigned short bio_ref;		/* bio reference count */
372275b66b0SChao Yu };
373275b66b0SChao Yu 
37478997b56SChao Yu enum {
37578997b56SChao Yu 	DPOLICY_BG,
37678997b56SChao Yu 	DPOLICY_FORCE,
37778997b56SChao Yu 	DPOLICY_FSTRIM,
37878997b56SChao Yu 	DPOLICY_UMOUNT,
37978997b56SChao Yu 	MAX_DPOLICY,
38078997b56SChao Yu };
38178997b56SChao Yu 
382ecc9aa00SChao Yu struct discard_policy {
38378997b56SChao Yu 	int type;			/* type of discard */
384ecc9aa00SChao Yu 	unsigned int min_interval;	/* used for candidates exist */
385f9d1dcedSYunlei He 	unsigned int mid_interval;	/* used for device busy */
386ecc9aa00SChao Yu 	unsigned int max_interval;	/* used for candidates not exist */
387ecc9aa00SChao Yu 	unsigned int max_requests;	/* # of discards issued per round */
388ecc9aa00SChao Yu 	unsigned int io_aware_gran;	/* minimum granularity discard not be aware of I/O */
389ecc9aa00SChao Yu 	bool io_aware;			/* issue discard in idle time */
390ecc9aa00SChao Yu 	bool sync;			/* submit discard with REQ_SYNC flag */
39120ee4382SChao Yu 	bool ordered;			/* issue discard by lba order */
3926ce48b0cSChao Yu 	bool timeout;			/* discard timeout for put_super */
39378997b56SChao Yu 	unsigned int granularity;	/* discard granularity */
394ecc9aa00SChao Yu };
395ecc9aa00SChao Yu 
3960b54fb84SJaegeuk Kim struct discard_cmd_control {
39715469963SJaegeuk Kim 	struct task_struct *f2fs_issue_discard;	/* discard thread */
39846f84c2cSChao Yu 	struct list_head entry_list;		/* 4KB discard entry list */
399ba48a33eSChao Yu 	struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
40046f84c2cSChao Yu 	struct list_head wait_list;		/* store on-flushing entries */
4018412663dSChao Yu 	struct list_head fstrim_list;		/* in-flight discard from fstrim */
40215469963SJaegeuk Kim 	wait_queue_head_t discard_wait_queue;	/* waiting queue for wake-up */
40315469963SJaegeuk Kim 	struct mutex cmd_lock;
404d618ebafSChao Yu 	unsigned int nr_discards;		/* # of discards in the list */
405d618ebafSChao Yu 	unsigned int max_discards;		/* max. discards to be issued */
406d2d8e896SKonstantin Vyshetsky 	unsigned int max_discard_request;	/* max. discard request per round */
407d2d8e896SKonstantin Vyshetsky 	unsigned int min_discard_issue_time;	/* min. interval between discard issue */
408d2d8e896SKonstantin Vyshetsky 	unsigned int mid_discard_issue_time;	/* mid. interval between discard issue */
409d2d8e896SKonstantin Vyshetsky 	unsigned int max_discard_issue_time;	/* max. interval between discard issue */
410120e0ea1SYangtao Li 	unsigned int discard_io_aware_gran; /* minimum discard granularity not be aware of I/O */
4118a47d228SYangtao Li 	unsigned int discard_urgent_util;	/* utilization which issue discard proactively */
412969d1b18SChao Yu 	unsigned int discard_granularity;	/* discard granularity */
413c46867e9SYangtao Li 	unsigned int max_ordered_discard;	/* maximum discard granularity issued by lba order */
414d84d1cbdSChao Yu 	unsigned int undiscard_blks;		/* # of undiscard blocks */
41520ee4382SChao Yu 	unsigned int next_pos;			/* next discard position */
4168b8dd65fSChao Yu 	atomic_t issued_discard;		/* # of issued discard */
41772691af6SJaegeuk Kim 	atomic_t queued_discard;		/* # of queued discard */
4185f32366aSChao Yu 	atomic_t discard_cmd_cnt;		/* # of cached cmd count */
4194dada3fdSChao Yu 	struct rb_root_cached root;		/* root of discard rb-tree */
42067fce70bSChao Yu 	bool rbtree_check;			/* config for consistence check */
42145c98f5aSYangtao Li 	bool discard_wake;			/* to wake up discard thread */
42239a53e0cSJaegeuk Kim };
42339a53e0cSJaegeuk Kim 
42439a53e0cSJaegeuk Kim /* for the list of fsync inodes, used only during recovery */
42539a53e0cSJaegeuk Kim struct fsync_inode_entry {
42639a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
42739a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
428c52e1b10SJaegeuk Kim 	block_t blkaddr;	/* block address locating the last fsync */
429c52e1b10SJaegeuk Kim 	block_t last_dentry;	/* block address locating the last dentry */
43039a53e0cSJaegeuk Kim };
43139a53e0cSJaegeuk Kim 
43268afcf2dSTomohiro Kusumi #define nats_in_cursum(jnl)		(le16_to_cpu((jnl)->n_nats))
43368afcf2dSTomohiro Kusumi #define sits_in_cursum(jnl)		(le16_to_cpu((jnl)->n_sits))
43439a53e0cSJaegeuk Kim 
43568afcf2dSTomohiro Kusumi #define nat_in_journal(jnl, i)		((jnl)->nat_j.entries[i].ne)
43668afcf2dSTomohiro Kusumi #define nid_in_journal(jnl, i)		((jnl)->nat_j.entries[i].nid)
43768afcf2dSTomohiro Kusumi #define sit_in_journal(jnl, i)		((jnl)->sit_j.entries[i].se)
43868afcf2dSTomohiro Kusumi #define segno_in_journal(jnl, i)	((jnl)->sit_j.entries[i].segno)
43939a53e0cSJaegeuk Kim 
440dfc08a12SChao Yu #define MAX_NAT_JENTRIES(jnl)	(NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
441dfc08a12SChao Yu #define MAX_SIT_JENTRIES(jnl)	(SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
442309cc2b6SJaegeuk Kim 
update_nats_in_cursum(struct f2fs_journal * journal,int i)443dfc08a12SChao Yu static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
44439a53e0cSJaegeuk Kim {
445dfc08a12SChao Yu 	int before = nats_in_cursum(journal);
446cac5a3d8SDongOh Shin 
447dfc08a12SChao Yu 	journal->n_nats = cpu_to_le16(before + i);
44839a53e0cSJaegeuk Kim 	return before;
44939a53e0cSJaegeuk Kim }
45039a53e0cSJaegeuk Kim 
update_sits_in_cursum(struct f2fs_journal * journal,int i)451dfc08a12SChao Yu static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
45239a53e0cSJaegeuk Kim {
453dfc08a12SChao Yu 	int before = sits_in_cursum(journal);
454cac5a3d8SDongOh Shin 
455dfc08a12SChao Yu 	journal->n_sits = cpu_to_le16(before + i);
45639a53e0cSJaegeuk Kim 	return before;
45739a53e0cSJaegeuk Kim }
45839a53e0cSJaegeuk Kim 
__has_cursum_space(struct f2fs_journal * journal,int size,int type)459dfc08a12SChao Yu static inline bool __has_cursum_space(struct f2fs_journal *journal,
460dfc08a12SChao Yu 							int size, int type)
461184a5cd2SChao Yu {
462184a5cd2SChao Yu 	if (type == NAT_JOURNAL)
463dfc08a12SChao Yu 		return size <= MAX_NAT_JENTRIES(journal);
464dfc08a12SChao Yu 	return size <= MAX_SIT_JENTRIES(journal);
465184a5cd2SChao Yu }
466184a5cd2SChao Yu 
467f2470371SChao Yu /* for inline stuff */
468f2470371SChao Yu #define DEF_INLINE_RESERVED_SIZE	1
4697a2af766SChao Yu static inline int get_extra_isize(struct inode *inode);
4706afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode);
4717a2af766SChao Yu #define MAX_INLINE_DATA(inode)	(sizeof(__le32) *			\
4727a2af766SChao Yu 				(CUR_ADDRS_PER_INODE(inode) -		\
473b323fd28SChao Yu 				get_inline_xattr_addrs(inode) -	\
4746afc662eSChao Yu 				DEF_INLINE_RESERVED_SIZE))
475f2470371SChao Yu 
476f2470371SChao Yu /* for inline dir */
477f2470371SChao Yu #define NR_INLINE_DENTRY(inode)	(MAX_INLINE_DATA(inode) * BITS_PER_BYTE / \
478f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
479f2470371SChao Yu 				BITS_PER_BYTE + 1))
480f91108b8SGeert Uytterhoeven #define INLINE_DENTRY_BITMAP_SIZE(inode) \
481f91108b8SGeert Uytterhoeven 	DIV_ROUND_UP(NR_INLINE_DENTRY(inode), BITS_PER_BYTE)
482f2470371SChao Yu #define INLINE_RESERVED_SIZE(inode)	(MAX_INLINE_DATA(inode) - \
483f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
484f2470371SChao Yu 				NR_INLINE_DENTRY(inode) + \
485f2470371SChao Yu 				INLINE_DENTRY_BITMAP_SIZE(inode)))
486f2470371SChao Yu 
487e9750824SNamjae Jeon /*
48839a53e0cSJaegeuk Kim  * For INODE and NODE manager
48939a53e0cSJaegeuk Kim  */
4907b3cd7d6SJaegeuk Kim /* for directory operations */
49143c780baSEric Biggers 
49243c780baSEric Biggers struct f2fs_filename {
49343c780baSEric Biggers 	/*
49443c780baSEric Biggers 	 * The filename the user specified.  This is NULL for some
49543c780baSEric Biggers 	 * filesystem-internal operations, e.g. converting an inline directory
49643c780baSEric Biggers 	 * to a non-inline one, or roll-forward recovering an encrypted dentry.
49743c780baSEric Biggers 	 */
49843c780baSEric Biggers 	const struct qstr *usr_fname;
49943c780baSEric Biggers 
50043c780baSEric Biggers 	/*
50143c780baSEric Biggers 	 * The on-disk filename.  For encrypted directories, this is encrypted.
50243c780baSEric Biggers 	 * This may be NULL for lookups in an encrypted dir without the key.
50343c780baSEric Biggers 	 */
50443c780baSEric Biggers 	struct fscrypt_str disk_name;
50543c780baSEric Biggers 
50643c780baSEric Biggers 	/* The dirhash of this filename */
50743c780baSEric Biggers 	f2fs_hash_t hash;
50843c780baSEric Biggers 
50943c780baSEric Biggers #ifdef CONFIG_FS_ENCRYPTION
51043c780baSEric Biggers 	/*
51143c780baSEric Biggers 	 * For lookups in encrypted directories: either the buffer backing
51243c780baSEric Biggers 	 * disk_name, or a buffer that holds the decoded no-key name.
51343c780baSEric Biggers 	 */
51443c780baSEric Biggers 	struct fscrypt_str crypto_buf;
51543c780baSEric Biggers #endif
5165298d4bfSChristoph Hellwig #if IS_ENABLED(CONFIG_UNICODE)
51743c780baSEric Biggers 	/*
51843c780baSEric Biggers 	 * For casefolded directories: the casefolded name, but it's left NULL
519b5639bb4SEric Biggers 	 * if the original name is not valid Unicode, if the original name is
520b5639bb4SEric Biggers 	 * "." or "..", if the directory is both casefolded and encrypted and
521b5639bb4SEric Biggers 	 * its encryption key is unavailable, or if the filesystem is doing an
522b5639bb4SEric Biggers 	 * internal operation where usr_fname is also NULL.  In all these cases
523b5639bb4SEric Biggers 	 * we fall back to treating the name as an opaque byte sequence.
52443c780baSEric Biggers 	 */
52543c780baSEric Biggers 	struct fscrypt_str cf_name;
52643c780baSEric Biggers #endif
52743c780baSEric Biggers };
52843c780baSEric Biggers 
5297b3cd7d6SJaegeuk Kim struct f2fs_dentry_ptr {
530d8c6822aSJaegeuk Kim 	struct inode *inode;
53176a9dd85SChao Yu 	void *bitmap;
5327b3cd7d6SJaegeuk Kim 	struct f2fs_dir_entry *dentry;
5337b3cd7d6SJaegeuk Kim 	__u8 (*filename)[F2FS_SLOT_LEN];
5347b3cd7d6SJaegeuk Kim 	int max;
53576a9dd85SChao Yu 	int nr_bitmap;
5367b3cd7d6SJaegeuk Kim };
5377b3cd7d6SJaegeuk Kim 
make_dentry_ptr_block(struct inode * inode,struct f2fs_dentry_ptr * d,struct f2fs_dentry_block * t)53864c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_block(struct inode *inode,
53964c24ecbSTomohiro Kusumi 		struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
5407b3cd7d6SJaegeuk Kim {
541d8c6822aSJaegeuk Kim 	d->inode = inode;
5427b3cd7d6SJaegeuk Kim 	d->max = NR_DENTRY_IN_BLOCK;
54376a9dd85SChao Yu 	d->nr_bitmap = SIZE_OF_DENTRY_BITMAP;
544c79d1520SYunlong Song 	d->bitmap = t->dentry_bitmap;
5457b3cd7d6SJaegeuk Kim 	d->dentry = t->dentry;
5467b3cd7d6SJaegeuk Kim 	d->filename = t->filename;
54764c24ecbSTomohiro Kusumi }
548cac5a3d8SDongOh Shin 
make_dentry_ptr_inline(struct inode * inode,struct f2fs_dentry_ptr * d,void * t)54964c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_inline(struct inode *inode,
550f2470371SChao Yu 					struct f2fs_dentry_ptr *d, void *t)
55164c24ecbSTomohiro Kusumi {
552f2470371SChao Yu 	int entry_cnt = NR_INLINE_DENTRY(inode);
553f2470371SChao Yu 	int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(inode);
554f2470371SChao Yu 	int reserved_size = INLINE_RESERVED_SIZE(inode);
555f2470371SChao Yu 
55664c24ecbSTomohiro Kusumi 	d->inode = inode;
557f2470371SChao Yu 	d->max = entry_cnt;
558f2470371SChao Yu 	d->nr_bitmap = bitmap_size;
559f2470371SChao Yu 	d->bitmap = t;
560f2470371SChao Yu 	d->dentry = t + bitmap_size + reserved_size;
561f2470371SChao Yu 	d->filename = t + bitmap_size + reserved_size +
562f2470371SChao Yu 					SIZE_OF_DIR_ENTRY * entry_cnt;
5637b3cd7d6SJaegeuk Kim }
5647b3cd7d6SJaegeuk Kim 
565dbe6a5ffSJaegeuk Kim /*
566dbe6a5ffSJaegeuk Kim  * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
567dbe6a5ffSJaegeuk Kim  * as its node offset to distinguish from index node blocks.
568dbe6a5ffSJaegeuk Kim  * But some bits are used to mark the node block.
56939a53e0cSJaegeuk Kim  */
570dbe6a5ffSJaegeuk Kim #define XATTR_NODE_OFFSET	((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
571dbe6a5ffSJaegeuk Kim 				>> OFFSET_BIT_SHIFT)
572266e97a8SJaegeuk Kim enum {
573266e97a8SJaegeuk Kim 	ALLOC_NODE,			/* allocate a new node page if needed */
574266e97a8SJaegeuk Kim 	LOOKUP_NODE,			/* look up a node without readahead */
575266e97a8SJaegeuk Kim 	LOOKUP_NODE_RA,			/*
576266e97a8SJaegeuk Kim 					 * look up a node with readahead called
5774f4124d0SChao Yu 					 * by get_data_block.
57839a53e0cSJaegeuk Kim 					 */
579266e97a8SJaegeuk Kim };
580266e97a8SJaegeuk Kim 
58191803392SChao Yu #define DEFAULT_RETRY_IO_COUNT	8	/* maximum retry read IO or flush count */
5827735730dSChao Yu 
5835df7731fSChao Yu /* congestion wait timeout value, default: 20ms */
5845df7731fSChao Yu #define	DEFAULT_IO_TIMEOUT	(msecs_to_jiffies(20))
5855df7731fSChao Yu 
586af033b2aSChao Yu /* maximum retry quota flush count */
587af033b2aSChao Yu #define DEFAULT_RETRY_QUOTA_FLUSH_COUNT		8
588af033b2aSChao Yu 
589a7b8618aSJaegeuk Kim /* maximum retry of EIO'ed page */
590a7b8618aSJaegeuk Kim #define MAX_RETRY_PAGE_EIO			100
59150c63009SJaegeuk Kim 
592a6db67f0SJaegeuk Kim #define F2FS_LINK_MAX	0xffffffff	/* maximum link count per file */
59339a53e0cSJaegeuk Kim 
594817202d9SChao Yu #define MAX_DIR_RA_PAGES	4	/* maximum ra pages of dir */
595817202d9SChao Yu 
596287b1406SChao Yu /* dirty segments threshold for triggering CP */
597287b1406SChao Yu #define DEFAULT_DIRTY_THRESHOLD		4
598287b1406SChao Yu 
599e7547dacSJaegeuk Kim #define RECOVERY_MAX_RA_BLOCKS		BIO_MAX_VECS
600e7547dacSJaegeuk Kim #define RECOVERY_MIN_RA_BLOCKS		1
601e7547dacSJaegeuk Kim 
602e7547dacSJaegeuk Kim #define F2FS_ONSTACK_PAGES	16	/* nr of onstack pages */
603e7547dacSJaegeuk Kim 
60439a53e0cSJaegeuk Kim /* for in-memory extent cache entry */
60513054c54SChao Yu #define F2FS_MIN_EXTENT_LEN	64	/* minimum extent length */
60613054c54SChao Yu 
60713054c54SChao Yu /* number of extent info in extent cache we try to shrink */
60812607c1bSJaegeuk Kim #define READ_EXTENT_CACHE_SHRINK_NUMBER	128
609c11abd1aSJaegeuk Kim 
61071644dffSJaegeuk Kim /* number of age extent info in extent cache we try to shrink */
61171644dffSJaegeuk Kim #define AGE_EXTENT_CACHE_SHRINK_NUMBER	128
61271644dffSJaegeuk Kim #define LAST_AGE_WEIGHT			30
61371644dffSJaegeuk Kim #define SAME_AGE_REGION			1024
61471644dffSJaegeuk Kim 
61571644dffSJaegeuk Kim /*
61671644dffSJaegeuk Kim  * Define data block with age less than 1GB as hot data
61771644dffSJaegeuk Kim  * define data block with age less than 10GB but more than 1GB as warm data
61871644dffSJaegeuk Kim  */
61971644dffSJaegeuk Kim #define DEF_HOT_DATA_AGE_THRESHOLD	262144
62071644dffSJaegeuk Kim #define DEF_WARM_DATA_AGE_THRESHOLD	2621440
62171644dffSJaegeuk Kim 
622e7547dacSJaegeuk Kim /* extent cache type */
623e7547dacSJaegeuk Kim enum extent_type {
624e7547dacSJaegeuk Kim 	EX_READ,
62571644dffSJaegeuk Kim 	EX_BLOCK_AGE,
626e7547dacSJaegeuk Kim 	NR_EXTENT_CACHES,
627e7547dacSJaegeuk Kim };
62801fc4b9aSFengnan Chang 
62939a53e0cSJaegeuk Kim struct extent_info {
63039a53e0cSJaegeuk Kim 	unsigned int fofs;		/* start offset in a file */
631111d2495SMasanari Iida 	unsigned int len;		/* length of the extent */
632e7547dacSJaegeuk Kim 	union {
633e7547dacSJaegeuk Kim 		/* read extent_cache */
634e7547dacSJaegeuk Kim 		struct {
635e7547dacSJaegeuk Kim 			/* start block address of the extent */
636e7547dacSJaegeuk Kim 			block_t blk;
63794afd6d6SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
638e7547dacSJaegeuk Kim 			/* physical extent length of compressed blocks */
639e7547dacSJaegeuk Kim 			unsigned int c_len;
64094afd6d6SChao Yu #endif
64139a53e0cSJaegeuk Kim 		};
64271644dffSJaegeuk Kim 		/* block age extent_cache */
64371644dffSJaegeuk Kim 		struct {
64471644dffSJaegeuk Kim 			/* block age of the extent */
64571644dffSJaegeuk Kim 			unsigned long long age;
64671644dffSJaegeuk Kim 			/* last total blocks allocated */
64771644dffSJaegeuk Kim 			unsigned long long last_blocks;
64871644dffSJaegeuk Kim 		};
649e7547dacSJaegeuk Kim 	};
650e7547dacSJaegeuk Kim };
65139a53e0cSJaegeuk Kim 
65213054c54SChao Yu struct extent_node {
653c0362117SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
65413054c54SChao Yu 	struct extent_info ei;		/* extent info */
65554c2258cSChao Yu 	struct list_head list;		/* node in global extent list of sbi */
656201ef5e0SHou Pengyang 	struct extent_tree *et;		/* extent tree pointer */
65713054c54SChao Yu };
65813054c54SChao Yu 
65913054c54SChao Yu struct extent_tree {
66013054c54SChao Yu 	nid_t ino;			/* inode number */
661e7547dacSJaegeuk Kim 	enum extent_type type;		/* keep the extent tree type */
6624dada3fdSChao Yu 	struct rb_root_cached root;	/* root of extent info rb-tree */
66362c8af65SChao Yu 	struct extent_node *cached_en;	/* recently accessed extent node */
664137d09f0SJaegeuk Kim 	struct list_head list;		/* to be used by sbi->zombie_list */
66513054c54SChao Yu 	rwlock_t lock;			/* protect extent info rb-tree */
66668e35385SChao Yu 	atomic_t node_cnt;		/* # of extent node in rb-tree*/
667b430f726SZhikang Zhang 	bool largest_updated;		/* largest extent updated */
668e7547dacSJaegeuk Kim 	struct extent_info largest;	/* largest cached extent for EX_READ */
669e7547dacSJaegeuk Kim };
670e7547dacSJaegeuk Kim 
671e7547dacSJaegeuk Kim struct extent_tree_info {
672e7547dacSJaegeuk Kim 	struct radix_tree_root extent_tree_root;/* cache extent cache entries */
673e7547dacSJaegeuk Kim 	struct mutex extent_tree_lock;	/* locking extent radix tree */
674e7547dacSJaegeuk Kim 	struct list_head extent_list;		/* lru list for shrinker */
675e7547dacSJaegeuk Kim 	spinlock_t extent_lock;			/* locking extent lru list */
676e7547dacSJaegeuk Kim 	atomic_t total_ext_tree;		/* extent tree count */
677e7547dacSJaegeuk Kim 	struct list_head zombie_list;		/* extent zombie tree list */
678e7547dacSJaegeuk Kim 	atomic_t total_zombie_tree;		/* extent zombie tree count */
679e7547dacSJaegeuk Kim 	atomic_t total_ext_node;		/* extent info count */
68013054c54SChao Yu };
68113054c54SChao Yu 
68239a53e0cSJaegeuk Kim /*
68362a134bdSChristoph Hellwig  * State of block returned by f2fs_map_blocks.
684003a3e1dSJaegeuk Kim  */
68562a134bdSChristoph Hellwig #define F2FS_MAP_NEW		(1U << 0)
68662a134bdSChristoph Hellwig #define F2FS_MAP_MAPPED		(1U << 1)
687da8c7fecSChristoph Hellwig #define F2FS_MAP_DELALLOC	(1U << 2)
6887f63eb77SJaegeuk Kim #define F2FS_MAP_FLAGS		(F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
689da8c7fecSChristoph Hellwig 				F2FS_MAP_DELALLOC)
690003a3e1dSJaegeuk Kim 
691003a3e1dSJaegeuk Kim struct f2fs_map_blocks {
69271f2c820SChao Yu 	struct block_device *m_bdev;	/* for multi-device dio */
693003a3e1dSJaegeuk Kim 	block_t m_pblk;
694003a3e1dSJaegeuk Kim 	block_t m_lblk;
695003a3e1dSJaegeuk Kim 	unsigned int m_len;
696003a3e1dSJaegeuk Kim 	unsigned int m_flags;
697da85985cSChao Yu 	pgoff_t *m_next_pgofs;		/* point next possible non-hole pgofs */
698c4020b2dSChao Yu 	pgoff_t *m_next_extent;		/* point to next possible extent */
699d5097be5SHyunchul Lee 	int m_seg_type;
700f9d6d059SChao Yu 	bool m_may_create;		/* indicate it is from write path */
70171f2c820SChao Yu 	bool m_multidev_dio;		/* indicate it allows multi-device dio */
702003a3e1dSJaegeuk Kim };
703003a3e1dSJaegeuk Kim 
704e2b4e2bcSChao Yu /* for flag in get_data_block */
705f2220c7fSQiuyang Sun enum {
706f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_DEFAULT,
707f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_FIEMAP,
708f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_BMAP,
7090a4daae5SJaegeuk Kim 	F2FS_GET_BLOCK_DIO,
710f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_DIO,
711f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_AIO,
712c4020b2dSChao Yu 	F2FS_GET_BLOCK_PRECACHE,
713f2220c7fSQiuyang Sun };
714e2b4e2bcSChao Yu 
715003a3e1dSJaegeuk Kim /*
71639a53e0cSJaegeuk Kim  * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
71739a53e0cSJaegeuk Kim  */
71839a53e0cSJaegeuk Kim #define FADVISE_COLD_BIT	0x01
719354a3399SJaegeuk Kim #define FADVISE_LOST_PINO_BIT	0x02
720cde4de12SJaegeuk Kim #define FADVISE_ENCRYPT_BIT	0x04
721e7d55452SJaegeuk Kim #define FADVISE_ENC_NAME_BIT	0x08
72226787236SJaegeuk Kim #define FADVISE_KEEP_SIZE_BIT	0x10
723b6a06cbbSChao Yu #define FADVISE_HOT_BIT		0x20
72495ae251fSEric Biggers #define FADVISE_VERITY_BIT	0x40
725d4dd19ecSJaegeuk Kim #define FADVISE_TRUNC_BIT	0x80
72639a53e0cSJaegeuk Kim 
727797c1cb5SChao Yu #define FADVISE_MODIFIABLE_BITS	(FADVISE_COLD_BIT | FADVISE_HOT_BIT)
728797c1cb5SChao Yu 
729b5492af7SJaegeuk Kim #define file_is_cold(inode)	is_file(inode, FADVISE_COLD_BIT)
730b5492af7SJaegeuk Kim #define file_set_cold(inode)	set_file(inode, FADVISE_COLD_BIT)
731b5492af7SJaegeuk Kim #define file_clear_cold(inode)	clear_file(inode, FADVISE_COLD_BIT)
7321153db09SChao Yu 
7331153db09SChao Yu #define file_wrong_pino(inode)	is_file(inode, FADVISE_LOST_PINO_BIT)
7341153db09SChao Yu #define file_lost_pino(inode)	set_file(inode, FADVISE_LOST_PINO_BIT)
735b5492af7SJaegeuk Kim #define file_got_pino(inode)	clear_file(inode, FADVISE_LOST_PINO_BIT)
7361153db09SChao Yu 
737cde4de12SJaegeuk Kim #define file_is_encrypt(inode)	is_file(inode, FADVISE_ENCRYPT_BIT)
738cde4de12SJaegeuk Kim #define file_set_encrypt(inode)	set_file(inode, FADVISE_ENCRYPT_BIT)
7391153db09SChao Yu 
740e7d55452SJaegeuk Kim #define file_enc_name(inode)	is_file(inode, FADVISE_ENC_NAME_BIT)
741e7d55452SJaegeuk Kim #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
7421153db09SChao Yu 
74326787236SJaegeuk Kim #define file_keep_isize(inode)	is_file(inode, FADVISE_KEEP_SIZE_BIT)
74426787236SJaegeuk Kim #define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
7451153db09SChao Yu 
746b6a06cbbSChao Yu #define file_is_hot(inode)	is_file(inode, FADVISE_HOT_BIT)
747b6a06cbbSChao Yu #define file_set_hot(inode)	set_file(inode, FADVISE_HOT_BIT)
748b6a06cbbSChao Yu #define file_clear_hot(inode)	clear_file(inode, FADVISE_HOT_BIT)
7491153db09SChao Yu 
75095ae251fSEric Biggers #define file_is_verity(inode)	is_file(inode, FADVISE_VERITY_BIT)
75195ae251fSEric Biggers #define file_set_verity(inode)	set_file(inode, FADVISE_VERITY_BIT)
752cde4de12SJaegeuk Kim 
753d4dd19ecSJaegeuk Kim #define file_should_truncate(inode)	is_file(inode, FADVISE_TRUNC_BIT)
754d4dd19ecSJaegeuk Kim #define file_need_truncate(inode)	set_file(inode, FADVISE_TRUNC_BIT)
755d4dd19ecSJaegeuk Kim #define file_dont_truncate(inode)	clear_file(inode, FADVISE_TRUNC_BIT)
756d4dd19ecSJaegeuk Kim 
757ab9fa662SJaegeuk Kim #define DEF_DIR_LEVEL		0
758ab9fa662SJaegeuk Kim 
7592ef79ecbSChao Yu enum {
7602ef79ecbSChao Yu 	GC_FAILURE_PIN,
7612ef79ecbSChao Yu 	MAX_GC_FAILURE
7622ef79ecbSChao Yu };
7632ef79ecbSChao Yu 
7647653b9d8SChao Yu /* used for f2fs_inode_info->flags */
7657653b9d8SChao Yu enum {
7667653b9d8SChao Yu 	FI_NEW_INODE,		/* indicate newly allocated inode */
7677653b9d8SChao Yu 	FI_DIRTY_INODE,		/* indicate inode is dirty or not */
7687653b9d8SChao Yu 	FI_AUTO_RECOVER,	/* indicate inode is recoverable */
7697653b9d8SChao Yu 	FI_DIRTY_DIR,		/* indicate directory has dirty pages */
7707653b9d8SChao Yu 	FI_INC_LINK,		/* need to increment i_nlink */
7717653b9d8SChao Yu 	FI_ACL_MODE,		/* indicate acl mode */
7727653b9d8SChao Yu 	FI_NO_ALLOC,		/* should not allocate any blocks */
7737653b9d8SChao Yu 	FI_FREE_NID,		/* free allocated nide */
7747653b9d8SChao Yu 	FI_NO_EXTENT,		/* not to use the extent cache */
7757653b9d8SChao Yu 	FI_INLINE_XATTR,	/* used for inline xattr */
7767653b9d8SChao Yu 	FI_INLINE_DATA,		/* used for inline data*/
7777653b9d8SChao Yu 	FI_INLINE_DENTRY,	/* used for inline dentry */
7787653b9d8SChao Yu 	FI_APPEND_WRITE,	/* inode has appended data */
7797653b9d8SChao Yu 	FI_UPDATE_WRITE,	/* inode has in-place-update data */
7807653b9d8SChao Yu 	FI_NEED_IPU,		/* used for ipu per file */
7817653b9d8SChao Yu 	FI_ATOMIC_FILE,		/* indicate atomic file */
7827653b9d8SChao Yu 	FI_DATA_EXIST,		/* indicate data exists */
7831018a546SChao Yu 	FI_SKIP_WRITES,		/* should skip data page writeback */
7841018a546SChao Yu 	FI_OPU_WRITE,		/* used for opu per file */
7857653b9d8SChao Yu 	FI_DIRTY_FILE,		/* indicate regular/symlink has dirty pages */
7863d697a4aSEric Biggers 	FI_PREALLOCATED_ALL,	/* all blocks for write were preallocated */
7877653b9d8SChao Yu 	FI_HOT_DATA,		/* indicate file is hot */
7887653b9d8SChao Yu 	FI_EXTRA_ATTR,		/* indicate file has extra attribute */
7897653b9d8SChao Yu 	FI_PROJ_INHERIT,	/* indicate file inherits projectid */
7907653b9d8SChao Yu 	FI_PIN_FILE,		/* indicate file should not be gced */
7917653b9d8SChao Yu 	FI_VERITY_IN_PROGRESS,	/* building fs-verity Merkle tree */
7927653b9d8SChao Yu 	FI_COMPRESSED_FILE,	/* indicate file's data can be compressed */
793b28f047bSChao Yu 	FI_COMPRESS_CORRUPT,	/* indicate compressed cluster is corrupted */
7947653b9d8SChao Yu 	FI_MMAP_FILE,		/* indicate file was mmapped */
795602a16d5SDaeho Jeong 	FI_ENABLE_COMPRESS,	/* enable compression in "user" compression mode */
796c6140415SJaegeuk Kim 	FI_COMPRESS_RELEASED,	/* compressed blocks were released */
797859fca6bSChao Yu 	FI_ALIGNED_WRITE,	/* enable aligned write */
7984a2c5b79SYe Bin 	FI_COW_FILE,		/* indicate COW file */
7994d8d45dfSDaeho Jeong 	FI_ATOMIC_COMMITTED,	/* indicate atomic commit completed except disk sync */
800364afd8aSDaeho Jeong 	FI_ATOMIC_DIRTIED,	/* indicate atomic file is dirtied */
80141e8f85aSDaeho Jeong 	FI_ATOMIC_REPLACE,	/* indicate atomic replace */
802f44a25a8SChao Yu 	FI_OPENED_FILE,		/* indicate file has been opened */
8037653b9d8SChao Yu 	FI_MAX,			/* max flag, never be used */
8047653b9d8SChao Yu };
8057653b9d8SChao Yu 
80639a53e0cSJaegeuk Kim struct f2fs_inode_info {
80739a53e0cSJaegeuk Kim 	struct inode vfs_inode;		/* serve a vfs inode */
80839a53e0cSJaegeuk Kim 	unsigned long i_flags;		/* keep an inode flags for ioctl */
80939a53e0cSJaegeuk Kim 	unsigned char i_advise;		/* use to give file attribute hints */
81038431545SJaegeuk Kim 	unsigned char i_dir_level;	/* use for dentry level for large dir */
8111ad71a27SJaegeuk Kim 	unsigned int i_current_depth;	/* only for directory depth */
8122ef79ecbSChao Yu 	/* for gc failure statistic */
8132ef79ecbSChao Yu 	unsigned int i_gc_failures[MAX_GC_FAILURE];
8146666e6aaSJaegeuk Kim 	unsigned int i_pino;		/* parent inode number */
81539a53e0cSJaegeuk Kim 	umode_t i_acl_mode;		/* keep file acl mode temporarily */
81639a53e0cSJaegeuk Kim 
81739a53e0cSJaegeuk Kim 	/* Use below internally in f2fs*/
8187653b9d8SChao Yu 	unsigned long flags[BITS_TO_LONGS(FI_MAX)];	/* use to pass per-file flags */
819e4544b63STim Murray 	struct f2fs_rwsem i_sem;	/* protect fi info */
820204706c7SJaegeuk Kim 	atomic_t dirty_pages;		/* # of dirty pages */
82139a53e0cSJaegeuk Kim 	f2fs_hash_t chash;		/* hash value of given file name */
82239a53e0cSJaegeuk Kim 	unsigned int clevel;		/* maximum level of given file name */
82388c5c13aSJaegeuk Kim 	struct task_struct *task;	/* lookup and create consistency */
824b0af6d49SChao Yu 	struct task_struct *cp_task;	/* separate cp/wb IO stats*/
825d80afefbSChao Yu 	struct task_struct *wb_task;	/* indicate inode is in context of writeback */
82639a53e0cSJaegeuk Kim 	nid_t i_xattr_nid;		/* node id that contains xattrs */
82726de9b11SJaegeuk Kim 	loff_t	last_disk_size;		/* lastly written file size */
828c10c9820SChao Yu 	spinlock_t i_size_lock;		/* protect last_disk_size */
82988b88a66SJaegeuk Kim 
8300abd675eSChao Yu #ifdef CONFIG_QUOTA
83142954c37SJan Kara 	struct dquot __rcu *i_dquot[MAXQUOTAS];
8320abd675eSChao Yu 
8330abd675eSChao Yu 	/* quota space reservation, managed internally by quota code */
8340abd675eSChao Yu 	qsize_t i_reserved_quota;
8350abd675eSChao Yu #endif
8360f18b462SJaegeuk Kim 	struct list_head dirty_list;	/* dirty list for dirs and files */
8370f18b462SJaegeuk Kim 	struct list_head gdirty_list;	/* linked in global dirty list */
8383db1de0eSDaeho Jeong 	struct task_struct *atomic_write_task;	/* store atomic write task */
839e7547dacSJaegeuk Kim 	struct extent_tree *extent_tree[NR_EXTENT_CACHES];
840e7547dacSJaegeuk Kim 					/* cached extent_tree entry */
841b82d4300SSunmin Jeong 	union {
8423db1de0eSDaeho Jeong 		struct inode *cow_inode;	/* copy-on-write inode for atomic write */
843b82d4300SSunmin Jeong 		struct inode *atomic_inode;
844b82d4300SSunmin Jeong 					/* point to atomic_inode, available only for cow_inode */
845b82d4300SSunmin Jeong 	};
846b2532c69SChao Yu 
847b2532c69SChao Yu 	/* avoid racing between foreground op and gc */
848e4544b63STim Murray 	struct f2fs_rwsem i_gc_rwsem[2];
849e4544b63STim Murray 	struct f2fs_rwsem i_xattr_sem; /* avoid racing between reading and changing EAs */
850f2470371SChao Yu 
8517a2af766SChao Yu 	int i_extra_isize;		/* size of extra space located in i_addr */
8525c57132eSChao Yu 	kprojid_t i_projid;		/* id for project quota */
8536afc662eSChao Yu 	int i_inline_xattr_size;	/* inline xattr size */
85424b81dfcSArnd Bergmann 	struct timespec64 i_crtime;	/* inode creation time */
855c277991dSChao Yu 	struct timespec64 i_disk_time[3];/* inode disk times */
8564c8ff709SChao Yu 
8574c8ff709SChao Yu 	/* for file compress */
858c2759ebaSDaeho Jeong 	atomic_t i_compr_blocks;		/* # of compressed blocks */
8594c8ff709SChao Yu 	unsigned char i_compress_algorithm;	/* algorithm type */
8604c8ff709SChao Yu 	unsigned char i_log_cluster_size;	/* log of cluster size */
8613fde13f8SChao Yu 	unsigned char i_compress_level;		/* compress level (lz4hc,zstd) */
862b90e5086SChao Yu 	unsigned char i_compress_flag;		/* compress flag */
8634c8ff709SChao Yu 	unsigned int i_cluster_size;		/* cluster size */
864f8e2f32bSDaeho Jeong 
865f8e2f32bSDaeho Jeong 	unsigned int atomic_write_cnt;
8664d8d45dfSDaeho Jeong 	loff_t original_i_size;		/* original i_size before atomic write */
86739a53e0cSJaegeuk Kim };
86839a53e0cSJaegeuk Kim 
get_read_extent_info(struct extent_info * ext,struct f2fs_extent * i_ext)86912607c1bSJaegeuk Kim static inline void get_read_extent_info(struct extent_info *ext,
870bd933d4fSChao Yu 					struct f2fs_extent *i_ext)
87139a53e0cSJaegeuk Kim {
872bd933d4fSChao Yu 	ext->fofs = le32_to_cpu(i_ext->fofs);
873bd933d4fSChao Yu 	ext->blk = le32_to_cpu(i_ext->blk);
874bd933d4fSChao Yu 	ext->len = le32_to_cpu(i_ext->len);
87539a53e0cSJaegeuk Kim }
87639a53e0cSJaegeuk Kim 
set_raw_read_extent(struct extent_info * ext,struct f2fs_extent * i_ext)87712607c1bSJaegeuk Kim static inline void set_raw_read_extent(struct extent_info *ext,
87839a53e0cSJaegeuk Kim 					struct f2fs_extent *i_ext)
87939a53e0cSJaegeuk Kim {
88039a53e0cSJaegeuk Kim 	i_ext->fofs = cpu_to_le32(ext->fofs);
8814d0b0bd4SChao Yu 	i_ext->blk = cpu_to_le32(ext->blk);
88239a53e0cSJaegeuk Kim 	i_ext->len = cpu_to_le32(ext->len);
88339a53e0cSJaegeuk Kim }
88439a53e0cSJaegeuk Kim 
__is_discard_mergeable(struct discard_info * back,struct discard_info * front,unsigned int max_len)885004b6862SChao Yu static inline bool __is_discard_mergeable(struct discard_info *back,
88635ec7d57SChao Yu 			struct discard_info *front, unsigned int max_len)
887004b6862SChao Yu {
8889a997188SJaegeuk Kim 	return (back->lstart + back->len == front->lstart) &&
88935ec7d57SChao Yu 		(back->len + front->len <= max_len);
890004b6862SChao Yu }
891004b6862SChao Yu 
__is_discard_back_mergeable(struct discard_info * cur,struct discard_info * back,unsigned int max_len)892004b6862SChao Yu static inline bool __is_discard_back_mergeable(struct discard_info *cur,
89335ec7d57SChao Yu 			struct discard_info *back, unsigned int max_len)
894004b6862SChao Yu {
89535ec7d57SChao Yu 	return __is_discard_mergeable(back, cur, max_len);
896004b6862SChao Yu }
897004b6862SChao Yu 
__is_discard_front_mergeable(struct discard_info * cur,struct discard_info * front,unsigned int max_len)898004b6862SChao Yu static inline bool __is_discard_front_mergeable(struct discard_info *cur,
89935ec7d57SChao Yu 			struct discard_info *front, unsigned int max_len)
900004b6862SChao Yu {
90135ec7d57SChao Yu 	return __is_discard_mergeable(cur, front, max_len);
902004b6862SChao Yu }
903004b6862SChao Yu 
9049a4ffdf5SChao Yu /*
9059a4ffdf5SChao Yu  * For free nid management
9069a4ffdf5SChao Yu  */
9079a4ffdf5SChao Yu enum nid_state {
9089a4ffdf5SChao Yu 	FREE_NID,		/* newly added to free nid list */
9099a4ffdf5SChao Yu 	PREALLOC_NID,		/* it is preallocated */
9109a4ffdf5SChao Yu 	MAX_NID_STATE,
911b8559dc2SChao Yu };
912b8559dc2SChao Yu 
913a95ba66aSJaegeuk Kim enum nat_state {
914a95ba66aSJaegeuk Kim 	TOTAL_NAT,
915a95ba66aSJaegeuk Kim 	DIRTY_NAT,
916a95ba66aSJaegeuk Kim 	RECLAIMABLE_NAT,
917a95ba66aSJaegeuk Kim 	MAX_NAT_STATE,
918a95ba66aSJaegeuk Kim };
919a95ba66aSJaegeuk Kim 
92039a53e0cSJaegeuk Kim struct f2fs_nm_info {
92139a53e0cSJaegeuk Kim 	block_t nat_blkaddr;		/* base disk address of NAT */
92239a53e0cSJaegeuk Kim 	nid_t max_nid;			/* maximum possible node ids */
92304d47e67SChao Yu 	nid_t available_nids;		/* # of available node ids */
92439a53e0cSJaegeuk Kim 	nid_t next_scan_nid;		/* the next nid to be scanned */
92547c8ebccSJaegeuk Kim 	nid_t max_rf_node_blocks;	/* max # of nodes for recovery */
926cdfc41c1SJaegeuk Kim 	unsigned int ram_thresh;	/* control the memory footprint */
927ea1a29a0SChao Yu 	unsigned int ra_nid_pages;	/* # of nid pages to be readaheaded */
9282304cb0cSChao Yu 	unsigned int dirty_nats_ratio;	/* control dirty nats ratio threshold */
92939a53e0cSJaegeuk Kim 
93039a53e0cSJaegeuk Kim 	/* NAT cache management */
93139a53e0cSJaegeuk Kim 	struct radix_tree_root nat_root;/* root of the nat entry cache */
932309cc2b6SJaegeuk Kim 	struct radix_tree_root nat_set_root;/* root of the nat set cache */
933e4544b63STim Murray 	struct f2fs_rwsem nat_tree_lock;	/* protect nat entry tree */
93439a53e0cSJaegeuk Kim 	struct list_head nat_entries;	/* cached nat entry list (clean) */
93522969158SChao Yu 	spinlock_t nat_list_lock;	/* protect clean nat entry list */
936a95ba66aSJaegeuk Kim 	unsigned int nat_cnt[MAX_NAT_STATE]; /* the # of cached nat entries */
93722ad0b6aSJaegeuk Kim 	unsigned int nat_blocks;	/* # of nat blocks */
93839a53e0cSJaegeuk Kim 
93939a53e0cSJaegeuk Kim 	/* free node ids management */
9408a7ed66aSJaegeuk Kim 	struct radix_tree_root free_nid_root;/* root of the free_nid cache */
9419a4ffdf5SChao Yu 	struct list_head free_nid_list;		/* list for free nids excluding preallocated nids */
9429a4ffdf5SChao Yu 	unsigned int nid_cnt[MAX_NID_STATE];	/* the number of free node id */
943b8559dc2SChao Yu 	spinlock_t nid_list_lock;	/* protect nid lists ops */
94439a53e0cSJaegeuk Kim 	struct mutex build_lock;	/* lock for build free nids */
945bb1105e4SJaegeuk Kim 	unsigned char **free_nid_bitmap;
9464ac91242SChao Yu 	unsigned char *nat_block_bitmap;
947586d1492SChao Yu 	unsigned short *free_nid_count;	/* free nid count of NAT block */
94839a53e0cSJaegeuk Kim 
94939a53e0cSJaegeuk Kim 	/* for checkpoint */
95039a53e0cSJaegeuk Kim 	char *nat_bitmap;		/* NAT bitmap pointer */
95122ad0b6aSJaegeuk Kim 
95222ad0b6aSJaegeuk Kim 	unsigned int nat_bits_blocks;	/* # of nat bits blocks */
95322ad0b6aSJaegeuk Kim 	unsigned char *nat_bits;	/* NAT bits blocks */
95422ad0b6aSJaegeuk Kim 	unsigned char *full_nat_bits;	/* full NAT pages */
95522ad0b6aSJaegeuk Kim 	unsigned char *empty_nat_bits;	/* empty NAT pages */
956599a09b2SChao Yu #ifdef CONFIG_F2FS_CHECK_FS
957599a09b2SChao Yu 	char *nat_bitmap_mir;		/* NAT bitmap mirror */
958599a09b2SChao Yu #endif
95939a53e0cSJaegeuk Kim 	int bitmap_size;		/* bitmap size */
96039a53e0cSJaegeuk Kim };
96139a53e0cSJaegeuk Kim 
96239a53e0cSJaegeuk Kim /*
96339a53e0cSJaegeuk Kim  * this structure is used as one of function parameters.
96439a53e0cSJaegeuk Kim  * all the information are dedicated to a given direct node block determined
96539a53e0cSJaegeuk Kim  * by the data offset in a file.
96639a53e0cSJaegeuk Kim  */
96739a53e0cSJaegeuk Kim struct dnode_of_data {
96839a53e0cSJaegeuk Kim 	struct inode *inode;		/* vfs inode pointer */
96939a53e0cSJaegeuk Kim 	struct page *inode_page;	/* its inode page, NULL is possible */
97039a53e0cSJaegeuk Kim 	struct page *node_page;		/* cached direct node page */
97139a53e0cSJaegeuk Kim 	nid_t nid;			/* node id of the direct node block */
97239a53e0cSJaegeuk Kim 	unsigned int ofs_in_node;	/* data offset in the node page */
97339a53e0cSJaegeuk Kim 	bool inode_page_locked;		/* inode page is locked or not */
97493bae099SJaegeuk Kim 	bool node_changed;		/* is node block changed */
9753cf45747SChao Yu 	char cur_level;			/* level of hole node page */
9763cf45747SChao Yu 	char max_level;			/* level of current page located */
97739a53e0cSJaegeuk Kim 	block_t	data_blkaddr;		/* block address of the node block */
97839a53e0cSJaegeuk Kim };
97939a53e0cSJaegeuk Kim 
set_new_dnode(struct dnode_of_data * dn,struct inode * inode,struct page * ipage,struct page * npage,nid_t nid)98039a53e0cSJaegeuk Kim static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
98139a53e0cSJaegeuk Kim 		struct page *ipage, struct page *npage, nid_t nid)
98239a53e0cSJaegeuk Kim {
983d66d1f76SJaegeuk Kim 	memset(dn, 0, sizeof(*dn));
98439a53e0cSJaegeuk Kim 	dn->inode = inode;
98539a53e0cSJaegeuk Kim 	dn->inode_page = ipage;
98639a53e0cSJaegeuk Kim 	dn->node_page = npage;
98739a53e0cSJaegeuk Kim 	dn->nid = nid;
98839a53e0cSJaegeuk Kim }
98939a53e0cSJaegeuk Kim 
99039a53e0cSJaegeuk Kim /*
99139a53e0cSJaegeuk Kim  * For SIT manager
99239a53e0cSJaegeuk Kim  *
99339a53e0cSJaegeuk Kim  * By default, there are 6 active log areas across the whole main area.
99439a53e0cSJaegeuk Kim  * When considering hot and cold data separation to reduce cleaning overhead,
99539a53e0cSJaegeuk Kim  * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
99639a53e0cSJaegeuk Kim  * respectively.
99739a53e0cSJaegeuk Kim  * In the current design, you should not change the numbers intentionally.
99839a53e0cSJaegeuk Kim  * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
99939a53e0cSJaegeuk Kim  * logs individually according to the underlying devices. (default: 6)
100039a53e0cSJaegeuk Kim  * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
100139a53e0cSJaegeuk Kim  * data and 8 for node logs.
100239a53e0cSJaegeuk Kim  */
100339a53e0cSJaegeuk Kim #define	NR_CURSEG_DATA_TYPE	(3)
100439a53e0cSJaegeuk Kim #define NR_CURSEG_NODE_TYPE	(3)
1005093749e2SChao Yu #define NR_CURSEG_INMEM_TYPE	(2)
1006a7d9fe3cSJaegeuk Kim #define NR_CURSEG_RO_TYPE	(2)
1007d0b9e42aSChao Yu #define NR_CURSEG_PERSIST_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
1008d0b9e42aSChao Yu #define NR_CURSEG_TYPE		(NR_CURSEG_INMEM_TYPE + NR_CURSEG_PERSIST_TYPE)
100939a53e0cSJaegeuk Kim 
101039a53e0cSJaegeuk Kim enum {
101139a53e0cSJaegeuk Kim 	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
101239a53e0cSJaegeuk Kim 	CURSEG_WARM_DATA,	/* data blocks */
101339a53e0cSJaegeuk Kim 	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
101439a53e0cSJaegeuk Kim 	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
101539a53e0cSJaegeuk Kim 	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
101639a53e0cSJaegeuk Kim 	CURSEG_COLD_NODE,	/* indirect node blocks */
1017d0b9e42aSChao Yu 	NR_PERSISTENT_LOG,	/* number of persistent log */
1018d0b9e42aSChao Yu 	CURSEG_COLD_DATA_PINNED = NR_PERSISTENT_LOG,
1019d0b9e42aSChao Yu 				/* pinned file that needs consecutive block address */
1020093749e2SChao Yu 	CURSEG_ALL_DATA_ATGC,	/* SSR alloctor in hot/warm/cold data area */
1021d0b9e42aSChao Yu 	NO_CHECK_TYPE,		/* number of persistent & inmem log */
102239a53e0cSJaegeuk Kim };
102339a53e0cSJaegeuk Kim 
10246b4afdd7SJaegeuk Kim struct flush_cmd {
10256b4afdd7SJaegeuk Kim 	struct completion wait;
1026721bd4d5SGu Zheng 	struct llist_node llnode;
102739d787beSChao Yu 	nid_t ino;
10286b4afdd7SJaegeuk Kim 	int ret;
10296b4afdd7SJaegeuk Kim };
10306b4afdd7SJaegeuk Kim 
1031a688b9d9SGu Zheng struct flush_cmd_control {
1032a688b9d9SGu Zheng 	struct task_struct *f2fs_issue_flush;	/* flush thread */
1033a688b9d9SGu Zheng 	wait_queue_head_t flush_wait_queue;	/* waiting queue for wake-up */
10348b8dd65fSChao Yu 	atomic_t issued_flush;			/* # of issued flushes */
103572691af6SJaegeuk Kim 	atomic_t queued_flush;			/* # of queued flushes */
1036721bd4d5SGu Zheng 	struct llist_head issue_list;		/* list for command issue */
1037721bd4d5SGu Zheng 	struct llist_node *dispatch_list;	/* list for command dispatch */
1038a688b9d9SGu Zheng };
1039a688b9d9SGu Zheng 
104039a53e0cSJaegeuk Kim struct f2fs_sm_info {
104139a53e0cSJaegeuk Kim 	struct sit_info *sit_info;		/* whole segment information */
104239a53e0cSJaegeuk Kim 	struct free_segmap_info *free_info;	/* free segment information */
104339a53e0cSJaegeuk Kim 	struct dirty_seglist_info *dirty_info;	/* dirty segment information */
104439a53e0cSJaegeuk Kim 	struct curseg_info *curseg_array;	/* active segment information */
104539a53e0cSJaegeuk Kim 
1046e4544b63STim Murray 	struct f2fs_rwsem curseg_lock;	/* for preventing curseg change */
10472b60311dSChao Yu 
104839a53e0cSJaegeuk Kim 	block_t seg0_blkaddr;		/* block address of 0'th segment */
104939a53e0cSJaegeuk Kim 	block_t main_blkaddr;		/* start block address of main area */
105039a53e0cSJaegeuk Kim 	block_t ssa_blkaddr;		/* start block address of SSA area */
105139a53e0cSJaegeuk Kim 
105239a53e0cSJaegeuk Kim 	unsigned int segment_count;	/* total # of segments */
105339a53e0cSJaegeuk Kim 	unsigned int main_segments;	/* # of segments in main area */
105439a53e0cSJaegeuk Kim 	unsigned int reserved_segments;	/* # of reserved segments */
1055300a8429SChao Yu 	unsigned int additional_reserved_segments;/* reserved segs for IO align feature */
105639a53e0cSJaegeuk Kim 	unsigned int ovp_segments;	/* # of overprovision segments */
105781eb8d6eSJaegeuk Kim 
105881eb8d6eSJaegeuk Kim 	/* a threshold to reclaim prefree segments */
105981eb8d6eSJaegeuk Kim 	unsigned int rec_prefree_segments;
10607fd9e544SJaegeuk Kim 
1061184a5cd2SChao Yu 	struct list_head sit_entry_set;	/* sit entry set list */
1062184a5cd2SChao Yu 
1063216fbd64SJaegeuk Kim 	unsigned int ipu_policy;	/* in-place-update policy */
1064216fbd64SJaegeuk Kim 	unsigned int min_ipu_util;	/* in-place-update threshold */
1065c1ce1b02SJaegeuk Kim 	unsigned int min_fsync_blocks;	/* threshold for fsync */
1066853137ceSJaegeuk Kim 	unsigned int min_seq_blocks;	/* threshold for sequential blocks */
1067ef095d19SJaegeuk Kim 	unsigned int min_hot_blocks;	/* threshold for hot block allocation */
1068a2a12b67SChao Yu 	unsigned int min_ssr_sections;	/* threshold to trigger SSR allocation */
10696b4afdd7SJaegeuk Kim 
10706b4afdd7SJaegeuk Kim 	/* for flush command control */
1071b01a9201SJaegeuk Kim 	struct flush_cmd_control *fcc_info;
1072a688b9d9SGu Zheng 
10730b54fb84SJaegeuk Kim 	/* for discard command control */
10740b54fb84SJaegeuk Kim 	struct discard_cmd_control *dcc_info;
107539a53e0cSJaegeuk Kim };
107639a53e0cSJaegeuk Kim 
107739a53e0cSJaegeuk Kim /*
107839a53e0cSJaegeuk Kim  * For superblock
107939a53e0cSJaegeuk Kim  */
108039a53e0cSJaegeuk Kim /*
108139a53e0cSJaegeuk Kim  * COUNT_TYPE for monitoring
108239a53e0cSJaegeuk Kim  *
108339a53e0cSJaegeuk Kim  * f2fs monitors the number of several block types such as on-writeback,
108439a53e0cSJaegeuk Kim  * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
108539a53e0cSJaegeuk Kim  */
108682704e59SChao Yu #define WB_DATA_TYPE(p, f)			\
108782704e59SChao Yu 	(f || f2fs_is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
108839a53e0cSJaegeuk Kim enum count_type {
108939a53e0cSJaegeuk Kim 	F2FS_DIRTY_DENTS,
1090c227f912SChao Yu 	F2FS_DIRTY_DATA,
10912c8a4a28SJaegeuk Kim 	F2FS_DIRTY_QDATA,
109239a53e0cSJaegeuk Kim 	F2FS_DIRTY_NODES,
109339a53e0cSJaegeuk Kim 	F2FS_DIRTY_META,
10940f18b462SJaegeuk Kim 	F2FS_DIRTY_IMETA,
109536951b38SChao Yu 	F2FS_WB_CP_DATA,
109636951b38SChao Yu 	F2FS_WB_DATA,
10975f9abab4SJaegeuk Kim 	F2FS_RD_DATA,
10985f9abab4SJaegeuk Kim 	F2FS_RD_NODE,
10995f9abab4SJaegeuk Kim 	F2FS_RD_META,
110002b16d0aSChao Yu 	F2FS_DIO_WRITE,
110102b16d0aSChao Yu 	F2FS_DIO_READ,
110239a53e0cSJaegeuk Kim 	NR_COUNT_TYPE,
110339a53e0cSJaegeuk Kim };
110439a53e0cSJaegeuk Kim 
110539a53e0cSJaegeuk Kim /*
1106e1c42045Sarter97  * The below are the page types of bios used in submit_bio().
110739a53e0cSJaegeuk Kim  * The available types are:
110839a53e0cSJaegeuk Kim  * DATA			User data pages. It operates as async mode.
110939a53e0cSJaegeuk Kim  * NODE			Node pages. It operates as async mode.
111039a53e0cSJaegeuk Kim  * META			FS metadata pages such as SIT, NAT, CP.
111139a53e0cSJaegeuk Kim  * NR_PAGE_TYPE		The number of page types.
111239a53e0cSJaegeuk Kim  * META_FLUSH		Make sure the previous pages are written
111339a53e0cSJaegeuk Kim  *			with waiting the bio's completion
111439a53e0cSJaegeuk Kim  * ...			Only can be used with META.
111539a53e0cSJaegeuk Kim  */
11167d5e5109SJaegeuk Kim #define PAGE_TYPE_OF_BIO(type)	((type) > META ? META : (type))
11175f8e5a09SJaegeuk Kim #define PAGE_TYPE_ON_MAIN(type)	((type) == DATA || (type) == NODE)
111839a53e0cSJaegeuk Kim enum page_type {
11196b8beca0SChao Yu 	DATA = 0,
11206b8beca0SChao Yu 	NODE = 1,	/* should not change this */
112139a53e0cSJaegeuk Kim 	META,
112239a53e0cSJaegeuk Kim 	NR_PAGE_TYPE,
112339a53e0cSJaegeuk Kim 	META_FLUSH,
11243db1de0eSDaeho Jeong 	IPU,		/* the below types are used by tracepoints only. */
11258ce67cb0SJaegeuk Kim 	OPU,
112639a53e0cSJaegeuk Kim };
112739a53e0cSJaegeuk Kim 
1128a912b54dSJaegeuk Kim enum temp_type {
1129a912b54dSJaegeuk Kim 	HOT = 0,	/* must be zero for meta bio */
1130a912b54dSJaegeuk Kim 	WARM,
1131a912b54dSJaegeuk Kim 	COLD,
1132a912b54dSJaegeuk Kim 	NR_TEMP_TYPE,
1133a912b54dSJaegeuk Kim };
1134a912b54dSJaegeuk Kim 
1135cc15620bSJaegeuk Kim enum need_lock_type {
1136cc15620bSJaegeuk Kim 	LOCK_REQ = 0,
1137cc15620bSJaegeuk Kim 	LOCK_DONE,
1138cc15620bSJaegeuk Kim 	LOCK_RETRY,
1139cc15620bSJaegeuk Kim };
1140cc15620bSJaegeuk Kim 
1141a5fd5050SChao Yu enum cp_reason_type {
1142a5fd5050SChao Yu 	CP_NO_NEEDED,
1143a5fd5050SChao Yu 	CP_NON_REGULAR,
11444c8ff709SChao Yu 	CP_COMPRESSED,
1145a5fd5050SChao Yu 	CP_HARDLINK,
1146a5fd5050SChao Yu 	CP_SB_NEED_CP,
1147a5fd5050SChao Yu 	CP_WRONG_PINO,
1148a5fd5050SChao Yu 	CP_NO_SPC_ROLL,
1149a5fd5050SChao Yu 	CP_NODE_NEED_CP,
1150a5fd5050SChao Yu 	CP_FASTBOOT_MODE,
1151a5fd5050SChao Yu 	CP_SPEC_LOG_NUM,
11520a007b97SJaegeuk Kim 	CP_RECOVER_DIR,
11536c59f87eSChao Yu 	CP_XATTR_DIR,
1154a5fd5050SChao Yu };
1155a5fd5050SChao Yu 
1156b0af6d49SChao Yu enum iostat_type {
11578b83ac81SChao Yu 	/* WRITE IO */
11588b83ac81SChao Yu 	APP_DIRECT_IO,			/* app direct write IOs */
11598b83ac81SChao Yu 	APP_BUFFERED_IO,		/* app buffered write IOs */
1160b0af6d49SChao Yu 	APP_WRITE_IO,			/* app write IOs */
1161b0af6d49SChao Yu 	APP_MAPPED_IO,			/* app mapped IOs */
116234a23525SChao Yu 	APP_BUFFERED_CDATA_IO,		/* app buffered write IOs on compressed file */
116334a23525SChao Yu 	APP_MAPPED_CDATA_IO,		/* app mapped write IOs on compressed file */
1164b0af6d49SChao Yu 	FS_DATA_IO,			/* data IOs from kworker/fsync/reclaimer */
116534a23525SChao Yu 	FS_CDATA_IO,			/* data IOs from kworker/fsync/reclaimer on compressed file */
1166b0af6d49SChao Yu 	FS_NODE_IO,			/* node IOs from kworker/fsync/reclaimer */
1167b0af6d49SChao Yu 	FS_META_IO,			/* meta IOs from kworker/reclaimer */
1168b0af6d49SChao Yu 	FS_GC_DATA_IO,			/* data IOs from forground gc */
1169b0af6d49SChao Yu 	FS_GC_NODE_IO,			/* node IOs from forground gc */
1170b0af6d49SChao Yu 	FS_CP_DATA_IO,			/* data IOs from checkpoint */
1171b0af6d49SChao Yu 	FS_CP_NODE_IO,			/* node IOs from checkpoint */
1172b0af6d49SChao Yu 	FS_CP_META_IO,			/* meta IOs from checkpoint */
11738b83ac81SChao Yu 
11748b83ac81SChao Yu 	/* READ IO */
11758b83ac81SChao Yu 	APP_DIRECT_READ_IO,		/* app direct read IOs */
11768b83ac81SChao Yu 	APP_BUFFERED_READ_IO,		/* app buffered read IOs */
11778b83ac81SChao Yu 	APP_READ_IO,			/* app read IOs */
11788b83ac81SChao Yu 	APP_MAPPED_READ_IO,		/* app mapped read IOs */
117934a23525SChao Yu 	APP_BUFFERED_CDATA_READ_IO,	/* app buffered read IOs on compressed file  */
118034a23525SChao Yu 	APP_MAPPED_CDATA_READ_IO,	/* app mapped read IOs on compressed file  */
11818b83ac81SChao Yu 	FS_DATA_READ_IO,		/* data read IOs */
11829c122384SChao Yu 	FS_GDATA_READ_IO,		/* data read IOs from background gc */
11839c122384SChao Yu 	FS_CDATA_READ_IO,		/* compressed data read IOs */
11848b83ac81SChao Yu 	FS_NODE_READ_IO,		/* node read IOs */
11858b83ac81SChao Yu 	FS_META_READ_IO,		/* meta read IOs */
11868b83ac81SChao Yu 
11878b83ac81SChao Yu 	/* other */
11887a2b15cfSYangtao Li 	FS_DISCARD_IO,			/* discard */
1189193a639fSYangtao Li 	FS_FLUSH_IO,			/* flush */
119025f90805SDaejun Park 	FS_ZONE_RESET_IO,		/* zone reset */
1191b0af6d49SChao Yu 	NR_IO_TYPE,
1192b0af6d49SChao Yu };
1193b0af6d49SChao Yu 
1194458e6197SJaegeuk Kim struct f2fs_io_info {
119505ca3632SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs_sb_info pointer */
119639d787beSChao Yu 	nid_t ino;		/* inode number */
1197458e6197SJaegeuk Kim 	enum page_type type;	/* contains DATA/NODE/META/META_FLUSH */
1198a912b54dSJaegeuk Kim 	enum temp_type temp;	/* contains HOT/WARM/COLD */
11997649c873SBart Van Assche 	enum req_op op;		/* contains REQ_OP_ */
12007649c873SBart Van Assche 	blk_opf_t op_flags;	/* req_flag_bits */
12017a9d7548SChao Yu 	block_t new_blkaddr;	/* new block address to be written */
120228bc106bSChao Yu 	block_t old_blkaddr;	/* old block address before Cow */
120305ca3632SJaegeuk Kim 	struct page *page;	/* page to be written */
12044375a336SJaegeuk Kim 	struct page *encrypted_page;	/* encrypted page */
12054c8ff709SChao Yu 	struct page *compressed_page;	/* compressed page */
1206fb830fc5SChao Yu 	struct list_head list;		/* serialize IOs */
12072eae077eSChao Yu 	unsigned int compr_blocks;	/* # of compressed block addresses */
12082eae077eSChao Yu 	unsigned int need_lock:8;	/* indicate we need to lock cp_rwsem */
12092eae077eSChao Yu 	unsigned int version:8;		/* version of the node */
12102eae077eSChao Yu 	unsigned int submitted:1;	/* indicate IO submission */
12112eae077eSChao Yu 	unsigned int in_list:1;		/* indicate fio is in io_list */
12122eae077eSChao Yu 	unsigned int is_por:1;		/* indicate IO is from recovery or not */
12132eae077eSChao Yu 	unsigned int encrypted:1;	/* indicate file is encrypted */
1214271fda62SSunmin Jeong 	unsigned int meta_gc:1;		/* require meta inode GC */
1215b0af6d49SChao Yu 	enum iostat_type io_type;	/* io type */
1216578c6478SYufen Yu 	struct writeback_control *io_wbc; /* writeback control */
12178648de2cSChao Yu 	struct bio **bio;		/* bio for ipu */
12188648de2cSChao Yu 	sector_t *last_block;		/* last block number in bio */
1219458e6197SJaegeuk Kim };
1220458e6197SJaegeuk Kim 
12210b20fcecSChao Yu struct bio_entry {
12220b20fcecSChao Yu 	struct bio *bio;
12230b20fcecSChao Yu 	struct list_head list;
12240b20fcecSChao Yu };
12250b20fcecSChao Yu 
122668afcf2dSTomohiro Kusumi #define is_read_io(rw) ((rw) == READ)
12271ff7bd3bSJaegeuk Kim struct f2fs_bio_info {
1228458e6197SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs superblock */
12291ff7bd3bSJaegeuk Kim 	struct bio *bio;		/* bios to merge */
12301ff7bd3bSJaegeuk Kim 	sector_t last_block_in_bio;	/* last block number */
1231458e6197SJaegeuk Kim 	struct f2fs_io_info fio;	/* store buffered io info. */
1232e067dc3cSDaeho Jeong #ifdef CONFIG_BLK_DEV_ZONED
1233e067dc3cSDaeho Jeong 	struct completion zone_wait;	/* condition value for the previous open zone to close */
1234e067dc3cSDaeho Jeong 	struct bio *zone_pending_bio;	/* pending bio for the previous zone */
1235e067dc3cSDaeho Jeong 	void *bi_private;		/* previous bi_private for pending bio */
1236e067dc3cSDaeho Jeong #endif
1237e4544b63STim Murray 	struct f2fs_rwsem io_rwsem;	/* blocking op for bio */
1238fb830fc5SChao Yu 	spinlock_t io_lock;		/* serialize DATA/NODE IOs */
1239fb830fc5SChao Yu 	struct list_head io_list;	/* track fios */
12400b20fcecSChao Yu 	struct list_head bio_list;	/* bio entry list head */
1241e4544b63STim Murray 	struct f2fs_rwsem bio_list_lock;	/* lock to protect bio entry list */
12421ff7bd3bSJaegeuk Kim };
12431ff7bd3bSJaegeuk Kim 
12443c62be17SJaegeuk Kim #define FDEV(i)				(sbi->devs[i])
12453c62be17SJaegeuk Kim #define RDEV(i)				(raw_super->devs[i])
12463c62be17SJaegeuk Kim struct f2fs_dev_info {
12473c62be17SJaegeuk Kim 	struct block_device *bdev;
12483c62be17SJaegeuk Kim 	char path[MAX_PATH_LEN];
12493c62be17SJaegeuk Kim 	unsigned int total_segments;
12503c62be17SJaegeuk Kim 	block_t start_blk;
12513c62be17SJaegeuk Kim 	block_t end_blk;
12523c62be17SJaegeuk Kim #ifdef CONFIG_BLK_DEV_ZONED
12533c62be17SJaegeuk Kim 	unsigned int nr_blkz;		/* Total number of zones */
125495175dafSDamien Le Moal 	unsigned long *blkz_seq;	/* Bitmap indicating sequential zones */
12553c62be17SJaegeuk Kim #endif
12563c62be17SJaegeuk Kim };
12573c62be17SJaegeuk Kim 
1258c227f912SChao Yu enum inode_type {
1259c227f912SChao Yu 	DIR_INODE,			/* for dirty dir inode */
1260c227f912SChao Yu 	FILE_INODE,			/* for dirty regular/symlink inode */
12610f18b462SJaegeuk Kim 	DIRTY_META,			/* for all dirtied inode metadata */
1262c227f912SChao Yu 	NR_INODE_TYPE,
1263c227f912SChao Yu };
1264c227f912SChao Yu 
126567298804SChao Yu /* for inner inode cache management */
126667298804SChao Yu struct inode_management {
126767298804SChao Yu 	struct radix_tree_root ino_root;	/* ino entry array */
126867298804SChao Yu 	spinlock_t ino_lock;			/* for ino entry lock */
126967298804SChao Yu 	struct list_head ino_list;		/* inode list head */
127067298804SChao Yu 	unsigned long ino_num;			/* number of entries */
127167298804SChao Yu };
127267298804SChao Yu 
1273093749e2SChao Yu /* for GC_AT */
1274093749e2SChao Yu struct atgc_management {
1275093749e2SChao Yu 	bool atgc_enabled;			/* ATGC is enabled or not */
1276093749e2SChao Yu 	struct rb_root_cached root;		/* root of victim rb-tree */
1277093749e2SChao Yu 	struct list_head victim_list;		/* linked with all victim entries */
1278093749e2SChao Yu 	unsigned int victim_count;		/* victim count in rb-tree */
1279093749e2SChao Yu 	unsigned int candidate_ratio;		/* candidate ratio */
1280093749e2SChao Yu 	unsigned int max_candidate_count;	/* max candidate count */
1281093749e2SChao Yu 	unsigned int age_weight;		/* age weight, vblock_weight = 100 - age_weight */
1282093749e2SChao Yu 	unsigned long long age_threshold;	/* age threshold */
1283093749e2SChao Yu };
1284093749e2SChao Yu 
1285d147ea4aSJaegeuk Kim struct f2fs_gc_control {
1286d147ea4aSJaegeuk Kim 	unsigned int victim_segno;	/* target victim segment number */
1287d147ea4aSJaegeuk Kim 	int init_gc_type;		/* FG_GC or BG_GC */
1288d147ea4aSJaegeuk Kim 	bool no_bg_gc;			/* check the space and stop bg_gc */
1289d147ea4aSJaegeuk Kim 	bool should_migrate_blocks;	/* should migrate blocks */
1290d147ea4aSJaegeuk Kim 	bool err_gc_skipped;		/* return EAGAIN if GC skipped */
1291c81d5baeSJaegeuk Kim 	unsigned int nr_free_secs;	/* # of free sections to do GC */
1292d147ea4aSJaegeuk Kim };
1293d147ea4aSJaegeuk Kim 
12945bb9c111SYangtao Li /*
12955bb9c111SYangtao Li  * For s_flag in struct f2fs_sb_info
12965bb9c111SYangtao Li  * Modification on enum should be synchronized with s_flag array
12975bb9c111SYangtao Li  */
1298caf0047eSChao Yu enum {
1299caf0047eSChao Yu 	SBI_IS_DIRTY,				/* dirty flag for checkpoint */
1300caf0047eSChao Yu 	SBI_IS_CLOSE,				/* specify unmounting */
1301caf0047eSChao Yu 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
1302caf0047eSChao Yu 	SBI_POR_DOING,				/* recovery is doing or not */
1303df728b0fSJaegeuk Kim 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
1304bbf156f7SJaegeuk Kim 	SBI_NEED_CP,				/* need to checkpoint */
130583a3bfdbSJaegeuk Kim 	SBI_IS_SHUTDOWN,			/* shutdown by ioctl */
13061378752bSChao Yu 	SBI_IS_RECOVERED,			/* recovered orphan/data */
13074354994fSDaniel Rosenberg 	SBI_CP_DISABLED,			/* CP was disabled last mount */
1308db610a64SJaegeuk Kim 	SBI_CP_DISABLED_QUICK,			/* CP was disabled quickly */
1309af033b2aSChao Yu 	SBI_QUOTA_NEED_FLUSH,			/* need to flush quota info in CP */
1310af033b2aSChao Yu 	SBI_QUOTA_SKIP_FLUSH,			/* skip flushing quota in current CP */
1311af033b2aSChao Yu 	SBI_QUOTA_NEED_REPAIR,			/* quota file may be corrupted */
131204f0b2eaSQiuyang Sun 	SBI_IS_RESIZEFS,			/* resizefs is in process */
1313ba900534SJaegeuk Kim 	SBI_IS_FREEZING,			/* freezefs is in process */
1314e1bb7d3dSChao Yu 	SBI_IS_WRITABLE,			/* remove ro mountoption transiently */
13155bb9c111SYangtao Li 	MAX_SBI_FLAG,
1316caf0047eSChao Yu };
1317caf0047eSChao Yu 
13186beceb54SJaegeuk Kim enum {
13196beceb54SJaegeuk Kim 	CP_TIME,
1320d0239e1bSJaegeuk Kim 	REQ_TIME,
1321a7d10cf3SSahitya Tummala 	DISCARD_TIME,
1322a7d10cf3SSahitya Tummala 	GC_TIME,
13234354994fSDaniel Rosenberg 	DISABLE_TIME,
132403f2c02dSJaegeuk Kim 	UMOUNT_DISCARD_TIMEOUT,
13256beceb54SJaegeuk Kim 	MAX_TIME,
13266beceb54SJaegeuk Kim };
13276beceb54SJaegeuk Kim 
1328a3951cd1SYangtao Li /* Note that you need to keep synchronization with this gc_mode_names array */
13290cdd3195SHyunchul Lee enum {
13305b0e9539SJaegeuk Kim 	GC_NORMAL,
13315b0e9539SJaegeuk Kim 	GC_IDLE_CB,
13325b0e9539SJaegeuk Kim 	GC_IDLE_GREEDY,
1333093749e2SChao Yu 	GC_IDLE_AT,
13340e5e8111SDaeho Jeong 	GC_URGENT_HIGH,
13350e5e8111SDaeho Jeong 	GC_URGENT_LOW,
1336d98af5f4SDaeho Jeong 	GC_URGENT_MID,
133707c6b593SDaeho Jeong 	MAX_GC_MODE,
13385b0e9539SJaegeuk Kim };
13395b0e9539SJaegeuk Kim 
13405b0e9539SJaegeuk Kim enum {
1341bbbc34fdSChao Yu 	BGGC_MODE_ON,		/* background gc is on */
1342bbbc34fdSChao Yu 	BGGC_MODE_OFF,		/* background gc is off */
1343bbbc34fdSChao Yu 	BGGC_MODE_SYNC,		/*
1344bbbc34fdSChao Yu 				 * background gc is on, migrating blocks
1345bbbc34fdSChao Yu 				 * like foreground gc
1346bbbc34fdSChao Yu 				 */
1347bbbc34fdSChao Yu };
1348bbbc34fdSChao Yu 
1349bbbc34fdSChao Yu enum {
1350b0332a0fSChao Yu 	FS_MODE_ADAPTIVE,		/* use both lfs/ssr allocation */
1351b0332a0fSChao Yu 	FS_MODE_LFS,			/* use lfs allocation only */
13526691d940SDaeho Jeong 	FS_MODE_FRAGMENT_SEG,		/* segment fragmentation mode */
13536691d940SDaeho Jeong 	FS_MODE_FRAGMENT_BLK,		/* block fragmentation mode */
1354b0332a0fSChao Yu };
1355b0332a0fSChao Yu 
1356b0332a0fSChao Yu enum {
135707939627SJaegeuk Kim 	ALLOC_MODE_DEFAULT,	/* stay default */
135807939627SJaegeuk Kim 	ALLOC_MODE_REUSE,	/* reuse segments as much as possible */
135907939627SJaegeuk Kim };
136007939627SJaegeuk Kim 
136193cf93f1SJunling Zheng enum fsync_mode {
136293cf93f1SJunling Zheng 	FSYNC_MODE_POSIX,	/* fsync follows posix semantics */
136393cf93f1SJunling Zheng 	FSYNC_MODE_STRICT,	/* fsync behaves in line with ext4 */
1364d6290814SJaegeuk Kim 	FSYNC_MODE_NOBARRIER,	/* fsync behaves nobarrier based on posix */
136593cf93f1SJunling Zheng };
136693cf93f1SJunling Zheng 
1367602a16d5SDaeho Jeong enum {
1368602a16d5SDaeho Jeong 	COMPR_MODE_FS,		/*
1369602a16d5SDaeho Jeong 				 * automatically compress compression
1370602a16d5SDaeho Jeong 				 * enabled files
1371602a16d5SDaeho Jeong 				 */
1372602a16d5SDaeho Jeong 	COMPR_MODE_USER,	/*
1373602a16d5SDaeho Jeong 				 * automatical compression is disabled.
1374602a16d5SDaeho Jeong 				 * user can control the file compression
1375602a16d5SDaeho Jeong 				 * using ioctls
1376602a16d5SDaeho Jeong 				 */
1377602a16d5SDaeho Jeong };
1378602a16d5SDaeho Jeong 
13794f993264SChao Yu enum {
13804f993264SChao Yu 	DISCARD_UNIT_BLOCK,	/* basic discard unit is block */
13814f993264SChao Yu 	DISCARD_UNIT_SEGMENT,	/* basic discard unit is segment */
13824f993264SChao Yu 	DISCARD_UNIT_SECTION,	/* basic discard unit is section */
13834f993264SChao Yu };
13844f993264SChao Yu 
13857a8fc586SDaeho Jeong enum {
13867a8fc586SDaeho Jeong 	MEMORY_MODE_NORMAL,	/* memory mode for normal devices */
13877a8fc586SDaeho Jeong 	MEMORY_MODE_LOW,	/* memory mode for low memry devices */
13887a8fc586SDaeho Jeong };
13897a8fc586SDaeho Jeong 
1390b62e71beSChao Yu enum errors_option {
1391b62e71beSChao Yu 	MOUNT_ERRORS_READONLY,	/* remount fs ro on errors */
1392b62e71beSChao Yu 	MOUNT_ERRORS_CONTINUE,	/* continue on errors */
1393b62e71beSChao Yu 	MOUNT_ERRORS_PANIC,	/* panic on errors */
1394b62e71beSChao Yu };
1395b62e71beSChao Yu 
1396eb61c2ccSChao Yu enum {
1397eb61c2ccSChao Yu 	BACKGROUND,
1398eb61c2ccSChao Yu 	FOREGROUND,
1399eb61c2ccSChao Yu 	MAX_CALL_TYPE,
1400eb61c2ccSChao Yu 	TOTAL_CALL = FOREGROUND,
1401eb61c2ccSChao Yu };
1402eb61c2ccSChao Yu 
1403b763f3beSChao Yu static inline int f2fs_test_bit(unsigned int nr, char *addr);
1404b763f3beSChao Yu static inline void f2fs_set_bit(unsigned int nr, char *addr);
1405b763f3beSChao Yu static inline void f2fs_clear_bit(unsigned int nr, char *addr);
14064c8ff709SChao Yu 
1407b763f3beSChao Yu /*
1408b763f3beSChao Yu  * Layout of f2fs page.private:
1409b763f3beSChao Yu  *
1410b763f3beSChao Yu  * Layout A: lowest bit should be 1
1411b763f3beSChao Yu  * | bit0 = 1 | bit1 | bit2 | ... | bit MAX | private data .... |
1412b763f3beSChao Yu  * bit 0	PAGE_PRIVATE_NOT_POINTER
14135f8e5a09SJaegeuk Kim  * bit 1	PAGE_PRIVATE_ONGOING_MIGRATION
14145f8e5a09SJaegeuk Kim  * bit 2	PAGE_PRIVATE_INLINE_INODE
14155f8e5a09SJaegeuk Kim  * bit 3	PAGE_PRIVATE_REF_RESOURCE
1416d889928bSChao Yu  * bit 4	PAGE_PRIVATE_ATOMIC_WRITE
1417d889928bSChao Yu  * bit 5-	f2fs private data
1418b763f3beSChao Yu  *
1419b763f3beSChao Yu  * Layout B: lowest bit should be 0
1420b763f3beSChao Yu  * page.private is a wrapped pointer.
1421b763f3beSChao Yu  */
1422b763f3beSChao Yu enum {
1423b763f3beSChao Yu 	PAGE_PRIVATE_NOT_POINTER,		/* private contains non-pointer data */
1424b763f3beSChao Yu 	PAGE_PRIVATE_ONGOING_MIGRATION,		/* data page which is on-going migrating */
1425b763f3beSChao Yu 	PAGE_PRIVATE_INLINE_INODE,		/* inode page contains inline data */
1426b763f3beSChao Yu 	PAGE_PRIVATE_REF_RESOURCE,		/* dirty page has referenced resources */
1427d889928bSChao Yu 	PAGE_PRIVATE_ATOMIC_WRITE,		/* data page from atomic write path */
1428b763f3beSChao Yu 	PAGE_PRIVATE_MAX
1429b763f3beSChao Yu };
1430b763f3beSChao Yu 
14314c8ff709SChao Yu /* For compression */
14324c8ff709SChao Yu enum compress_algorithm_type {
14334c8ff709SChao Yu 	COMPRESS_LZO,
14344c8ff709SChao Yu 	COMPRESS_LZ4,
143550cfa66fSChao Yu 	COMPRESS_ZSTD,
14366d92b201SChao Yu 	COMPRESS_LZORLE,
14374c8ff709SChao Yu 	COMPRESS_MAX,
14384c8ff709SChao Yu };
14394c8ff709SChao Yu 
1440b28f047bSChao Yu enum compress_flag {
1441b28f047bSChao Yu 	COMPRESS_CHKSUM,
1442b28f047bSChao Yu 	COMPRESS_MAX_FLAG,
1443b28f047bSChao Yu };
1444b28f047bSChao Yu 
14456ce19affSChao Yu #define	COMPRESS_WATERMARK			20
14466ce19affSChao Yu #define	COMPRESS_PERCENT			20
14476ce19affSChao Yu 
1448b28f047bSChao Yu #define COMPRESS_DATA_RESERVED_SIZE		4
14494c8ff709SChao Yu struct compress_data {
14504c8ff709SChao Yu 	__le32 clen;			/* compressed data size */
1451b28f047bSChao Yu 	__le32 chksum;			/* compressed data chksum */
14524c8ff709SChao Yu 	__le32 reserved[COMPRESS_DATA_RESERVED_SIZE];	/* reserved */
14534c8ff709SChao Yu 	u8 cdata[];			/* compressed data */
14544c8ff709SChao Yu };
14554c8ff709SChao Yu 
14564c8ff709SChao Yu #define COMPRESS_HEADER_SIZE	(sizeof(struct compress_data))
14574c8ff709SChao Yu 
14584c8ff709SChao Yu #define F2FS_COMPRESSED_PAGE_MAGIC	0xF5F2C000
14594c8ff709SChao Yu 
146000e120b5SJaegeuk Kim #define F2FS_ZSTD_DEFAULT_CLEVEL	1
146100e120b5SJaegeuk Kim 
14623fde13f8SChao Yu #define	COMPRESS_LEVEL_OFFSET	8
14633fde13f8SChao Yu 
14644c8ff709SChao Yu /* compress context */
14654c8ff709SChao Yu struct compress_ctx {
14664c8ff709SChao Yu 	struct inode *inode;		/* inode the context belong to */
14674c8ff709SChao Yu 	pgoff_t cluster_idx;		/* cluster index number */
14684c8ff709SChao Yu 	unsigned int cluster_size;	/* page count in cluster */
14694c8ff709SChao Yu 	unsigned int log_cluster_size;	/* log of cluster size */
14704c8ff709SChao Yu 	struct page **rpages;		/* pages store raw data in cluster */
14714c8ff709SChao Yu 	unsigned int nr_rpages;		/* total page number in rpages */
14724c8ff709SChao Yu 	struct page **cpages;		/* pages store compressed data in cluster */
14734c8ff709SChao Yu 	unsigned int nr_cpages;		/* total page number in cpages */
14743271d7ebSFengnan Chang 	unsigned int valid_nr_cpages;	/* valid page number in cpages */
14754c8ff709SChao Yu 	void *rbuf;			/* virtual mapped address on rpages */
14764c8ff709SChao Yu 	struct compress_data *cbuf;	/* virtual mapped address on cpages */
14774c8ff709SChao Yu 	size_t rlen;			/* valid data length in rbuf */
14784c8ff709SChao Yu 	size_t clen;			/* valid data length in cbuf */
14794c8ff709SChao Yu 	void *private;			/* payload buffer for specified compression algorithm */
148050cfa66fSChao Yu 	void *private2;			/* extra payload buffer */
14814c8ff709SChao Yu };
14824c8ff709SChao Yu 
14834c8ff709SChao Yu /* compress context for write IO path */
14844c8ff709SChao Yu struct compress_io_ctx {
14854c8ff709SChao Yu 	u32 magic;			/* magic number to indicate page is compressed */
14864c8ff709SChao Yu 	struct inode *inode;		/* inode the context belong to */
14874c8ff709SChao Yu 	struct page **rpages;		/* pages store raw data in cluster */
14884c8ff709SChao Yu 	unsigned int nr_rpages;		/* total page number in rpages */
1489e6c3948dSChao Yu 	atomic_t pending_pages;		/* in-flight compressed page count */
14904c8ff709SChao Yu };
14914c8ff709SChao Yu 
14927f59b277SEric Biggers /* Context for decompressing one cluster on the read IO path */
14934c8ff709SChao Yu struct decompress_io_ctx {
14944c8ff709SChao Yu 	u32 magic;			/* magic number to indicate page is compressed */
14954c8ff709SChao Yu 	struct inode *inode;		/* inode the context belong to */
14964c8ff709SChao Yu 	pgoff_t cluster_idx;		/* cluster index number */
14974c8ff709SChao Yu 	unsigned int cluster_size;	/* page count in cluster */
14984c8ff709SChao Yu 	unsigned int log_cluster_size;	/* log of cluster size */
14994c8ff709SChao Yu 	struct page **rpages;		/* pages store raw data in cluster */
15004c8ff709SChao Yu 	unsigned int nr_rpages;		/* total page number in rpages */
15014c8ff709SChao Yu 	struct page **cpages;		/* pages store compressed data in cluster */
15024c8ff709SChao Yu 	unsigned int nr_cpages;		/* total page number in cpages */
15034c8ff709SChao Yu 	struct page **tpages;		/* temp pages to pad holes in cluster */
15044c8ff709SChao Yu 	void *rbuf;			/* virtual mapped address on rpages */
15054c8ff709SChao Yu 	struct compress_data *cbuf;	/* virtual mapped address on cpages */
15064c8ff709SChao Yu 	size_t rlen;			/* valid data length in rbuf */
15074c8ff709SChao Yu 	size_t clen;			/* valid data length in cbuf */
15087f59b277SEric Biggers 
15097f59b277SEric Biggers 	/*
15107f59b277SEric Biggers 	 * The number of compressed pages remaining to be read in this cluster.
15117f59b277SEric Biggers 	 * This is initially nr_cpages.  It is decremented by 1 each time a page
15127f59b277SEric Biggers 	 * has been read (or failed to be read).  When it reaches 0, the cluster
15137f59b277SEric Biggers 	 * is decompressed (or an error is reported).
15147f59b277SEric Biggers 	 *
15157f59b277SEric Biggers 	 * If an error occurs before all the pages have been submitted for I/O,
15167f59b277SEric Biggers 	 * then this will never reach 0.  In this case the I/O submitter is
15177f59b277SEric Biggers 	 * responsible for calling f2fs_decompress_end_io() instead.
15187f59b277SEric Biggers 	 */
15197f59b277SEric Biggers 	atomic_t remaining_pages;
15207f59b277SEric Biggers 
15217f59b277SEric Biggers 	/*
15227f59b277SEric Biggers 	 * Number of references to this decompress_io_ctx.
15237f59b277SEric Biggers 	 *
15247f59b277SEric Biggers 	 * One reference is held for I/O completion.  This reference is dropped
15257f59b277SEric Biggers 	 * after the pagecache pages are updated and unlocked -- either after
15267f59b277SEric Biggers 	 * decompression (and verity if enabled), or after an error.
15277f59b277SEric Biggers 	 *
15287f59b277SEric Biggers 	 * In addition, each compressed page holds a reference while it is in a
15297f59b277SEric Biggers 	 * bio.  These references are necessary prevent compressed pages from
15307f59b277SEric Biggers 	 * being freed while they are still in a bio.
15317f59b277SEric Biggers 	 */
15327f59b277SEric Biggers 	refcount_t refcnt;
15337f59b277SEric Biggers 
15347f59b277SEric Biggers 	bool failed;			/* IO error occurred before decompression? */
15357f59b277SEric Biggers 	bool need_verity;		/* need fs-verity verification after decompression? */
153650cfa66fSChao Yu 	void *private;			/* payload buffer for specified decompression algorithm */
153750cfa66fSChao Yu 	void *private2;			/* extra payload buffer */
15387f59b277SEric Biggers 	struct work_struct verity_work;	/* work to verify the decompressed pages */
1539bff139b4SDaeho Jeong 	struct work_struct free_work;	/* work for late free this structure itself */
15404c8ff709SChao Yu };
15414c8ff709SChao Yu 
15424c8ff709SChao Yu #define NULL_CLUSTER			((unsigned int)(~0))
15434c8ff709SChao Yu #define MIN_COMPRESS_LOG_SIZE		2
15444c8ff709SChao Yu #define MAX_COMPRESS_LOG_SIZE		8
15450e2b7385SChao Yu #define MAX_COMPRESS_WINDOW_SIZE(log_size)	((PAGE_SIZE) << (log_size))
15464c8ff709SChao Yu 
154739a53e0cSJaegeuk Kim struct f2fs_sb_info {
154839a53e0cSJaegeuk Kim 	struct super_block *sb;			/* pointer to VFS super block */
15495e176d54SJaegeuk Kim 	struct proc_dir_entry *s_proc;		/* proc entry */
155039a53e0cSJaegeuk Kim 	struct f2fs_super_block *raw_super;	/* raw super block pointer */
1551e4544b63STim Murray 	struct f2fs_rwsem sb_lock;		/* lock for raw super block */
1552e8240f65SChao Yu 	int valid_super_block;			/* valid super block no */
1553fadb2fb8SChao Yu 	unsigned long s_flag;				/* flags for sbi */
1554853137ceSJaegeuk Kim 	struct mutex writepages;		/* mutex for writepages() */
155539a53e0cSJaegeuk Kim 
1556178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
1557178053e2SDamien Le Moal 	unsigned int blocks_per_blkz;		/* F2FS blocks per zone */
1558178053e2SDamien Le Moal #endif
1559178053e2SDamien Le Moal 
156039a53e0cSJaegeuk Kim 	/* for node-related operations */
156139a53e0cSJaegeuk Kim 	struct f2fs_nm_info *nm_info;		/* node manager */
156239a53e0cSJaegeuk Kim 	struct inode *node_inode;		/* cache node blocks */
156339a53e0cSJaegeuk Kim 
156439a53e0cSJaegeuk Kim 	/* for segment-related operations */
156539a53e0cSJaegeuk Kim 	struct f2fs_sm_info *sm_info;		/* segment manager */
15661ff7bd3bSJaegeuk Kim 
15671ff7bd3bSJaegeuk Kim 	/* for bio operations */
1568a912b54dSJaegeuk Kim 	struct f2fs_bio_info *write_io[NR_PAGE_TYPE];	/* for write bios */
1569107a805dSChao Yu 	/* keep migration IO order for LFS mode */
1570e4544b63STim Murray 	struct f2fs_rwsem io_order_lock;
1571a7b8618aSJaegeuk Kim 	pgoff_t page_eio_ofs[NR_PAGE_TYPE];	/* EIO page offset */
1572a7b8618aSJaegeuk Kim 	int page_eio_cnt[NR_PAGE_TYPE];		/* EIO count */
157339a53e0cSJaegeuk Kim 
157439a53e0cSJaegeuk Kim 	/* for checkpoint */
157539a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt;		/* raw checkpoint pointer */
15768508e44aSJaegeuk Kim 	int cur_cp_pack;			/* remain current cp pack */
1577aaec2b1dSChao Yu 	spinlock_t cp_lock;			/* for flag in ckpt */
157839a53e0cSJaegeuk Kim 	struct inode *meta_inode;		/* cache meta blocks */
1579e4544b63STim Murray 	struct f2fs_rwsem cp_global_sem;	/* checkpoint procedure lock */
1580e4544b63STim Murray 	struct f2fs_rwsem cp_rwsem;		/* blocking FS operations */
1581e4544b63STim Murray 	struct f2fs_rwsem node_write;		/* locking node writes */
1582e4544b63STim Murray 	struct f2fs_rwsem node_change;	/* locking node change */
1583fb51b5efSChangman Lee 	wait_queue_head_t cp_wait;
15846beceb54SJaegeuk Kim 	unsigned long last_time[MAX_TIME];	/* to store time in jiffies */
15856beceb54SJaegeuk Kim 	long interval_time[MAX_TIME];		/* to store thresholds */
1586261eeb9cSDaeho Jeong 	struct ckpt_req_control cprc_info;	/* for checkpoint request control */
158739a53e0cSJaegeuk Kim 
158867298804SChao Yu 	struct inode_management im[MAX_INO_ENTRY];	/* manage inode cache */
15896451e041SJaegeuk Kim 
159050fa53ecSChao Yu 	spinlock_t fsync_node_lock;		/* for node entry lock */
159150fa53ecSChao Yu 	struct list_head fsync_node_list;	/* node list head */
159250fa53ecSChao Yu 	unsigned int fsync_seg_id;		/* sequence id */
159350fa53ecSChao Yu 	unsigned int fsync_node_num;		/* number of node entries */
159450fa53ecSChao Yu 
15956451e041SJaegeuk Kim 	/* for orphan inode, use 0'th array */
15960d47c1adSGu Zheng 	unsigned int max_orphans;		/* max orphan inodes */
159739a53e0cSJaegeuk Kim 
1598c227f912SChao Yu 	/* for inode management */
1599c227f912SChao Yu 	struct list_head inode_list[NR_INODE_TYPE];	/* dirty inode list */
1600c227f912SChao Yu 	spinlock_t inode_lock[NR_INODE_TYPE];	/* for dirty inode list lock */
1601040d2bb3SChao Yu 	struct mutex flush_lock;		/* for flush exclusion */
160239a53e0cSJaegeuk Kim 
160313054c54SChao Yu 	/* for extent tree cache */
1604e7547dacSJaegeuk Kim 	struct extent_tree_info extent_tree[NR_EXTENT_CACHES];
160571644dffSJaegeuk Kim 	atomic64_t allocated_data_blocks;	/* for block age extent_cache */
160671644dffSJaegeuk Kim 
160771644dffSJaegeuk Kim 	/* The threshold used for hot and warm data seperation*/
160871644dffSJaegeuk Kim 	unsigned int hot_data_age_threshold;
160971644dffSJaegeuk Kim 	unsigned int warm_data_age_threshold;
1610d23be468Sqixiaoyu1 	unsigned int last_age_weight;
161113054c54SChao Yu 
161239a53e0cSJaegeuk Kim 	/* basic filesystem units */
161339a53e0cSJaegeuk Kim 	unsigned int log_sectors_per_block;	/* log2 sectors per block */
161439a53e0cSJaegeuk Kim 	unsigned int log_blocksize;		/* log2 block size */
161539a53e0cSJaegeuk Kim 	unsigned int blocksize;			/* block size */
161639a53e0cSJaegeuk Kim 	unsigned int root_ino_num;		/* root inode number*/
161739a53e0cSJaegeuk Kim 	unsigned int node_ino_num;		/* node inode number*/
161839a53e0cSJaegeuk Kim 	unsigned int meta_ino_num;		/* meta inode number*/
161939a53e0cSJaegeuk Kim 	unsigned int log_blocks_per_seg;	/* log2 blocks per segment */
162039a53e0cSJaegeuk Kim 	unsigned int blocks_per_seg;		/* blocks per segment */
1621b771aadcSJaegeuk Kim 	unsigned int unusable_blocks_per_sec;	/* unusable blocks per section */
162239a53e0cSJaegeuk Kim 	unsigned int segs_per_sec;		/* segments per section */
162339a53e0cSJaegeuk Kim 	unsigned int secs_per_zone;		/* sections per zone */
162439a53e0cSJaegeuk Kim 	unsigned int total_sections;		/* total section count */
162539a53e0cSJaegeuk Kim 	unsigned int total_node_count;		/* total node block count */
162639a53e0cSJaegeuk Kim 	unsigned int total_valid_node_count;	/* valid node block count */
1627ab9fa662SJaegeuk Kim 	int dir_level;				/* directory level */
162866aee5aaSYuwei Guan 	bool readdir_ra;			/* readahead inode in readdir */
162910208567SJaegeuk Kim 	u64 max_io_bytes;			/* max io bytes to merge IOs */
163039a53e0cSJaegeuk Kim 
163139a53e0cSJaegeuk Kim 	block_t user_block_count;		/* # of user blocks */
163239a53e0cSJaegeuk Kim 	block_t total_valid_block_count;	/* # of valid blocks */
1633a66cdd98SJaegeuk Kim 	block_t discard_blks;			/* discard command candidats */
163439a53e0cSJaegeuk Kim 	block_t last_valid_block_count;		/* for recovery */
1635daeb433eSChao Yu 	block_t reserved_blocks;		/* configurable reserved blocks */
163680d42145SYunlong Song 	block_t current_reserved_blocks;	/* current reserved blocks */
1637daeb433eSChao Yu 
16384354994fSDaniel Rosenberg 	/* Additional tracking for no checkpoint mode */
16394354994fSDaniel Rosenberg 	block_t unusable_block_count;		/* # of blocks saved by last cp */
16404354994fSDaniel Rosenberg 
1641292c196aSChao Yu 	unsigned int nquota_files;		/* # of quota sysfile */
1642e4544b63STim Murray 	struct f2fs_rwsem quota_sem;		/* blocking cp for flags */
1643292c196aSChao Yu 
1644523be8a6SJaegeuk Kim 	/* # of pages, see count_type */
164535782b23SJaegeuk Kim 	atomic_t nr_pages[NR_COUNT_TYPE];
164641382ec4SJaegeuk Kim 	/* # of allocated blocks */
164741382ec4SJaegeuk Kim 	struct percpu_counter alloc_valid_block_count;
164847c8ebccSJaegeuk Kim 	/* # of node block writes as roll forward recovery */
164947c8ebccSJaegeuk Kim 	struct percpu_counter rf_node_block_count;
165039a53e0cSJaegeuk Kim 
1651687de7f1SJaegeuk Kim 	/* writeback control */
1652c29fd0c0SChao Yu 	atomic_t wb_sync_req[META];	/* count # of WB_SYNC threads */
1653687de7f1SJaegeuk Kim 
1654513c5f37SJaegeuk Kim 	/* valid inode count */
1655513c5f37SJaegeuk Kim 	struct percpu_counter total_valid_inode_count;
1656513c5f37SJaegeuk Kim 
165739a53e0cSJaegeuk Kim 	struct f2fs_mount_info mount_opt;	/* mount options */
165839a53e0cSJaegeuk Kim 
165939a53e0cSJaegeuk Kim 	/* for cleaning operations */
1660e4544b63STim Murray 	struct f2fs_rwsem gc_lock;		/*
1661fb24fea7SChao Yu 						 * semaphore for GC, avoid
1662fb24fea7SChao Yu 						 * race between GC and GC or CP
1663fb24fea7SChao Yu 						 */
166439a53e0cSJaegeuk Kim 	struct f2fs_gc_kthread	*gc_thread;	/* GC thread */
1665093749e2SChao Yu 	struct atgc_management am;		/* atgc management */
16665ec4e49fSJaegeuk Kim 	unsigned int cur_victim_sec;		/* current victim section num */
16675b0e9539SJaegeuk Kim 	unsigned int gc_mode;			/* current GC state */
1668e3080b01SChao Yu 	unsigned int next_victim_seg[2];	/* next segment in victim section */
1669e5a0db6aSYangtao Li 	spinlock_t gc_remaining_trials_lock;
1670e5a0db6aSYangtao Li 	/* remaining trial count for GC_URGENT_* and GC_IDLE_* */
1671e5a0db6aSYangtao Li 	unsigned int gc_remaining_trials;
16720e5e8111SDaeho Jeong 
16732ef79ecbSChao Yu 	/* for skip statistic */
16746f8d4455SJaegeuk Kim 	unsigned long long skipped_gc_rwsem;		/* FG_GC only */
167539a53e0cSJaegeuk Kim 
16761ad71a27SJaegeuk Kim 	/* threshold for gc trials on pinned files */
16771ad71a27SJaegeuk Kim 	u64 gc_pin_file_threshold;
1678e4544b63STim Murray 	struct f2fs_rwsem pin_sem;
16791ad71a27SJaegeuk Kim 
1680b1c57c1cSJaegeuk Kim 	/* maximum # of trials to find a victim segment for SSR and GC */
1681b1c57c1cSJaegeuk Kim 	unsigned int max_victim_search;
1682e3080b01SChao Yu 	/* migration granularity of garbage collection, unit: segment */
1683e3080b01SChao Yu 	unsigned int migration_granularity;
1684b1c57c1cSJaegeuk Kim 
168539a53e0cSJaegeuk Kim 	/*
168639a53e0cSJaegeuk Kim 	 * for stat information.
168739a53e0cSJaegeuk Kim 	 * one is for the LFS mode, and the other is for the SSR mode.
168839a53e0cSJaegeuk Kim 	 */
168935b09d82SNamjae Jeon #ifdef CONFIG_F2FS_STAT_FS
169039a53e0cSJaegeuk Kim 	struct f2fs_stat_info *stat_info;	/* FS status information */
1691b63e7be5SChao Yu 	atomic_t meta_count[META_MAX];		/* # of meta blocks */
169239a53e0cSJaegeuk Kim 	unsigned int segment_count[2];		/* # of allocated segments */
169339a53e0cSJaegeuk Kim 	unsigned int block_count[2];		/* # of allocated blocks */
1694b9a2c252SChangman Lee 	atomic_t inplace_count;		/* # of inplace update */
1695e7547dacSJaegeuk Kim 	/* # of lookup extent cache */
1696e7547dacSJaegeuk Kim 	atomic64_t total_hit_ext[NR_EXTENT_CACHES];
1697e7547dacSJaegeuk Kim 	/* # of hit rbtree extent node */
1698e7547dacSJaegeuk Kim 	atomic64_t read_hit_rbtree[NR_EXTENT_CACHES];
1699e7547dacSJaegeuk Kim 	/* # of hit cached extent node */
1700e7547dacSJaegeuk Kim 	atomic64_t read_hit_cached[NR_EXTENT_CACHES];
1701e7547dacSJaegeuk Kim 	/* # of hit largest extent node in read extent cache */
1702e7547dacSJaegeuk Kim 	atomic64_t read_hit_largest;
1703d5e8f6c9SChao Yu 	atomic_t inline_xattr;			/* # of inline_xattr inodes */
170403e14d52SChao Yu 	atomic_t inline_inode;			/* # of inline_data inodes */
170503e14d52SChao Yu 	atomic_t inline_dir;			/* # of inline_dentry inodes */
17064c8ff709SChao Yu 	atomic_t compr_inode;			/* # of compressed inodes */
1707ae999bb9SDaeho Jeong 	atomic64_t compr_blocks;		/* # of compressed blocks */
17088ec071c3SChao Yu 	atomic_t swapfile_inode;		/* # of swapfile inodes */
1709b4dac120SChao Yu 	atomic_t atomic_files;			/* # of opened atomic file */
171026a28a0cSJaegeuk Kim 	atomic_t max_aw_cnt;			/* max # of atomic writes */
1711274bd9baSChao Yu 	unsigned int io_skip_bggc;		/* skip background gc for in-flight IO */
1712274bd9baSChao Yu 	unsigned int other_skip_bggc;		/* skip background gc for other reasons */
171333fbd510SChao Yu 	unsigned int ndirty_inode[NR_INODE_TYPE];	/* # of dirty inodes */
1714eb61c2ccSChao Yu 	atomic_t cp_call_count[MAX_CALL_TYPE];	/* # of cp call */
171535b09d82SNamjae Jeon #endif
171639a53e0cSJaegeuk Kim 	spinlock_t stat_lock;			/* lock for stat operations */
1717b59d0baeSNamjae Jeon 
1718da9953b7SJaegeuk Kim 	/* to attach REQ_META|REQ_FUA flags */
1719da9953b7SJaegeuk Kim 	unsigned int data_io_flag;
172032b6aba8SJaegeuk Kim 	unsigned int node_io_flag;
1721b0af6d49SChao Yu 
1722759820c9SJulia Lawall 	/* For sysfs support */
17235d4daa57SChao Yu 	struct kobject s_kobj;			/* /sys/fs/f2fs/<devname> */
1724b59d0baeSNamjae Jeon 	struct completion s_kobj_unregister;
17252658e50dSJaegeuk Kim 
17265d4daa57SChao Yu 	struct kobject s_stat_kobj;		/* /sys/fs/f2fs/<devname>/stat */
17275d4daa57SChao Yu 	struct completion s_stat_kobj_unregister;
17285d4daa57SChao Yu 
17294c89b53dSJaegeuk Kim 	struct kobject s_feature_list_kobj;		/* /sys/fs/f2fs/<devname>/feature_list */
17304c89b53dSJaegeuk Kim 	struct completion s_feature_list_kobj_unregister;
17314c89b53dSJaegeuk Kim 
17322658e50dSJaegeuk Kim 	/* For shrinker support */
17332658e50dSJaegeuk Kim 	struct list_head s_list;
173471f2c820SChao Yu 	struct mutex umount_mutex;
173571f2c820SChao Yu 	unsigned int shrinker_run_no;
173671f2c820SChao Yu 
173771f2c820SChao Yu 	/* For multi devices */
17383c62be17SJaegeuk Kim 	int s_ndevs;				/* number of devices */
17393c62be17SJaegeuk Kim 	struct f2fs_dev_info *devs;		/* for device list */
17401228b482SChao Yu 	unsigned int dirty_device;		/* for checkpoint data flush */
17411228b482SChao Yu 	spinlock_t dev_lock;			/* protect dirty_device */
174271f2c820SChao Yu 	bool aligned_blksize;			/* all devices has the same logical blksize */
17438f1dbbbbSShuoran Liu 
17448f1dbbbbSShuoran Liu 	/* For write statistics */
17458f1dbbbbSShuoran Liu 	u64 sectors_written_start;
17468f1dbbbbSShuoran Liu 	u64 kbytes_written;
174743b6573bSKeith Mok 
174843b6573bSKeith Mok 	/* Reference to checksum algorithm driver via cryptoapi */
174943b6573bSKeith Mok 	struct crypto_shash *s_chksum_driver;
17501ecc0c5cSChao Yu 
1751704956ecSChao Yu 	/* Precomputed FS UUID checksum for seeding other checksums */
1752704956ecSChao Yu 	__u32 s_chksum_seed;
17534c8ff709SChao Yu 
17544c8ff709SChao Yu 	struct workqueue_struct *post_read_wq;	/* post read workqueue */
1755a999150fSChao Yu 
1756b62e71beSChao Yu 	/*
1757b62e71beSChao Yu 	 * If we are in irq context, let's update error information into
1758b62e71beSChao Yu 	 * on-disk superblock in the work.
1759b62e71beSChao Yu 	 */
1760b62e71beSChao Yu 	struct work_struct s_error_work;
176195fa90c9SChao Yu 	unsigned char errors[MAX_F2FS_ERRORS];		/* error flags */
1762b62e71beSChao Yu 	unsigned char stop_reason[MAX_STOP_REASON];	/* stop reason */
1763b62e71beSChao Yu 	spinlock_t error_lock;			/* protect errors/stop_reason array */
176495fa90c9SChao Yu 	bool error_dirty;			/* errors of sb is dirty */
176595fa90c9SChao Yu 
1766a999150fSChao Yu 	struct kmem_cache *inline_xattr_slab;	/* inline xattr entry */
1767a999150fSChao Yu 	unsigned int inline_xattr_slab_size;	/* default inline xattr slab size */
176831083031SChao Yu 
176907c6b593SDaeho Jeong 	/* For reclaimed segs statistics per each GC mode */
177007c6b593SDaeho Jeong 	unsigned int gc_segment_mode;		/* GC state for reclaimed segments */
177107c6b593SDaeho Jeong 	unsigned int gc_reclaimed_segs[MAX_GC_MODE];	/* Reclaimed segs for each mode */
177207c6b593SDaeho Jeong 
17730f6b56ecSDaeho Jeong 	unsigned long seq_file_ra_mul;		/* multiplier for ra_pages of seq. files in fadvise */
17740f6b56ecSDaeho Jeong 
17756691d940SDaeho Jeong 	int max_fragment_chunk;			/* max chunk size for block fragmentation mode */
17766691d940SDaeho Jeong 	int max_fragment_hole;			/* max hole size for block fragmentation mode */
17776691d940SDaeho Jeong 
1778f8e2f32bSDaeho Jeong 	/* For atomic write statistics */
1779f8e2f32bSDaeho Jeong 	atomic64_t current_atomic_write;
1780f8e2f32bSDaeho Jeong 	s64 peak_atomic_write;
1781f8e2f32bSDaeho Jeong 	u64 committed_atomic_block;
1782f8e2f32bSDaeho Jeong 	u64 revoked_atomic_block;
1783f8e2f32bSDaeho Jeong 
178431083031SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
178531083031SChao Yu 	struct kmem_cache *page_array_slab;	/* page array entry */
178631083031SChao Yu 	unsigned int page_array_slab_size;	/* default page array slab size */
17875ac443e2SDaeho Jeong 
17885ac443e2SDaeho Jeong 	/* For runtime compression statistics */
17895ac443e2SDaeho Jeong 	u64 compr_written_block;
17905ac443e2SDaeho Jeong 	u64 compr_saved_block;
17915ac443e2SDaeho Jeong 	u32 compr_new_inode;
17926ce19affSChao Yu 
17936ce19affSChao Yu 	/* For compressed block cache */
17946ce19affSChao Yu 	struct inode *compress_inode;		/* cache compressed blocks */
17956ce19affSChao Yu 	unsigned int compress_percent;		/* cache page percentage */
17966ce19affSChao Yu 	unsigned int compress_watermark;	/* cache page watermark */
17976ce19affSChao Yu 	atomic_t compress_page_hit;		/* cache hit count */
179831083031SChao Yu #endif
179952118743SDaeho Jeong 
180052118743SDaeho Jeong #ifdef CONFIG_F2FS_IOSTAT
180152118743SDaeho Jeong 	/* For app/fs IO statistics */
180252118743SDaeho Jeong 	spinlock_t iostat_lock;
18037a2b15cfSYangtao Li 	unsigned long long iostat_count[NR_IO_TYPE];
18047a2b15cfSYangtao Li 	unsigned long long iostat_bytes[NR_IO_TYPE];
18057a2b15cfSYangtao Li 	unsigned long long prev_iostat_bytes[NR_IO_TYPE];
180652118743SDaeho Jeong 	bool iostat_enable;
180752118743SDaeho Jeong 	unsigned long iostat_next_period;
180852118743SDaeho Jeong 	unsigned int iostat_period_ms;
1809a4b68176SDaeho Jeong 
1810a4b68176SDaeho Jeong 	/* For io latency related statistics info in one iostat period */
1811a4b68176SDaeho Jeong 	spinlock_t iostat_lat_lock;
1812a4b68176SDaeho Jeong 	struct iostat_lat_info *iostat_io_lat;
181352118743SDaeho Jeong #endif
181439a53e0cSJaegeuk Kim };
181539a53e0cSJaegeuk Kim 
1816f0248ba6SJaegeuk Kim /* Definitions to access f2fs_sb_info */
1817f0248ba6SJaegeuk Kim #define BLKS_PER_SEG(sbi)					\
1818f0248ba6SJaegeuk Kim 	((sbi)->blocks_per_seg)
1819f0248ba6SJaegeuk Kim #define BLKS_PER_SEC(sbi)					\
1820f0248ba6SJaegeuk Kim 	((sbi)->segs_per_sec << (sbi)->log_blocks_per_seg)
1821f0248ba6SJaegeuk Kim #define SEGS_PER_SEC(sbi)					\
1822f0248ba6SJaegeuk Kim 	((sbi)->segs_per_sec)
1823f0248ba6SJaegeuk Kim 
182442d48304SChao Yu __printf(3, 4)
182542d48304SChao Yu void f2fs_printk(struct f2fs_sb_info *sbi, bool limit_rate, const char *fmt, ...);
182642d48304SChao Yu 
182742d48304SChao Yu #define f2fs_err(sbi, fmt, ...)						\
182842d48304SChao Yu 	f2fs_printk(sbi, false, KERN_ERR fmt, ##__VA_ARGS__)
182942d48304SChao Yu #define f2fs_warn(sbi, fmt, ...)					\
183042d48304SChao Yu 	f2fs_printk(sbi, false, KERN_WARNING fmt, ##__VA_ARGS__)
183142d48304SChao Yu #define f2fs_notice(sbi, fmt, ...)					\
183242d48304SChao Yu 	f2fs_printk(sbi, false, KERN_NOTICE fmt, ##__VA_ARGS__)
183342d48304SChao Yu #define f2fs_info(sbi, fmt, ...)					\
183442d48304SChao Yu 	f2fs_printk(sbi, false, KERN_INFO fmt, ##__VA_ARGS__)
183542d48304SChao Yu #define f2fs_debug(sbi, fmt, ...)					\
183642d48304SChao Yu 	f2fs_printk(sbi, false, KERN_DEBUG fmt, ##__VA_ARGS__)
183742d48304SChao Yu 
183842d48304SChao Yu #define f2fs_err_ratelimited(sbi, fmt, ...)				\
183942d48304SChao Yu 	f2fs_printk(sbi, true, KERN_ERR fmt, ##__VA_ARGS__)
184042d48304SChao Yu #define f2fs_warn_ratelimited(sbi, fmt, ...)				\
184142d48304SChao Yu 	f2fs_printk(sbi, true, KERN_WARNING fmt, ##__VA_ARGS__)
184242d48304SChao Yu #define f2fs_info_ratelimited(sbi, fmt, ...)				\
184342d48304SChao Yu 	f2fs_printk(sbi, true, KERN_INFO fmt, ##__VA_ARGS__)
184442d48304SChao Yu 
18451ecc0c5cSChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
1846c40e15a9SYangtao Li #define time_to_inject(sbi, type) __time_to_inject(sbi, type, __func__,	\
1847c40e15a9SYangtao Li 									__builtin_return_address(0))
__time_to_inject(struct f2fs_sb_info * sbi,int type,const char * func,const char * parent_func)1848c40e15a9SYangtao Li static inline bool __time_to_inject(struct f2fs_sb_info *sbi, int type,
1849c40e15a9SYangtao Li 				const char *func, const char *parent_func)
18501ecc0c5cSChao Yu {
185163189b78SChao Yu 	struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
18521ecc0c5cSChao Yu 
18531ecc0c5cSChao Yu 	if (!ffi->inject_rate)
18541ecc0c5cSChao Yu 		return false;
18551ecc0c5cSChao Yu 
18561ecc0c5cSChao Yu 	if (!IS_FAULT_SET(ffi, type))
18571ecc0c5cSChao Yu 		return false;
18581ecc0c5cSChao Yu 
18591ecc0c5cSChao Yu 	atomic_inc(&ffi->inject_ops);
18601ecc0c5cSChao Yu 	if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
18611ecc0c5cSChao Yu 		atomic_set(&ffi->inject_ops, 0);
186242d48304SChao Yu 		f2fs_info_ratelimited(sbi, "inject %s in %s of %pS",
186342d48304SChao Yu 				f2fs_fault_name[type], func, parent_func);
18641ecc0c5cSChao Yu 		return true;
18651ecc0c5cSChao Yu 	}
18661ecc0c5cSChao Yu 	return false;
18671ecc0c5cSChao Yu }
18687fa750a1SArnd Bergmann #else
time_to_inject(struct f2fs_sb_info * sbi,int type)18697fa750a1SArnd Bergmann static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
18707fa750a1SArnd Bergmann {
18717fa750a1SArnd Bergmann 	return false;
18727fa750a1SArnd Bergmann }
18731ecc0c5cSChao Yu #endif
18741ecc0c5cSChao Yu 
18750916878dSDamien Le Moal /*
18760916878dSDamien Le Moal  * Test if the mounted volume is a multi-device volume.
18770916878dSDamien Le Moal  *   - For a single regular disk volume, sbi->s_ndevs is 0.
18780916878dSDamien Le Moal  *   - For a single zoned disk volume, sbi->s_ndevs is 1.
18790916878dSDamien Le Moal  *   - For a multi-device volume, sbi->s_ndevs is always 2 or more.
18800916878dSDamien Le Moal  */
f2fs_is_multi_device(struct f2fs_sb_info * sbi)18810916878dSDamien Le Moal static inline bool f2fs_is_multi_device(struct f2fs_sb_info *sbi)
18820916878dSDamien Le Moal {
18830916878dSDamien Le Moal 	return sbi->s_ndevs > 1;
18840916878dSDamien Le Moal }
18850916878dSDamien Le Moal 
f2fs_update_time(struct f2fs_sb_info * sbi,int type)18866beceb54SJaegeuk Kim static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
18876beceb54SJaegeuk Kim {
1888a7d10cf3SSahitya Tummala 	unsigned long now = jiffies;
1889a7d10cf3SSahitya Tummala 
1890a7d10cf3SSahitya Tummala 	sbi->last_time[type] = now;
1891a7d10cf3SSahitya Tummala 
1892a7d10cf3SSahitya Tummala 	/* DISCARD_TIME and GC_TIME are based on REQ_TIME */
1893a7d10cf3SSahitya Tummala 	if (type == REQ_TIME) {
1894a7d10cf3SSahitya Tummala 		sbi->last_time[DISCARD_TIME] = now;
1895a7d10cf3SSahitya Tummala 		sbi->last_time[GC_TIME] = now;
1896a7d10cf3SSahitya Tummala 	}
18976beceb54SJaegeuk Kim }
18986beceb54SJaegeuk Kim 
f2fs_time_over(struct f2fs_sb_info * sbi,int type)18996beceb54SJaegeuk Kim static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
19006beceb54SJaegeuk Kim {
19016bccfa19SArnd Bergmann 	unsigned long interval = sbi->interval_time[type] * HZ;
19026beceb54SJaegeuk Kim 
19036beceb54SJaegeuk Kim 	return time_after(jiffies, sbi->last_time[type] + interval);
19046beceb54SJaegeuk Kim }
19056beceb54SJaegeuk Kim 
f2fs_time_to_wait(struct f2fs_sb_info * sbi,int type)1906a7d10cf3SSahitya Tummala static inline unsigned int f2fs_time_to_wait(struct f2fs_sb_info *sbi,
1907a7d10cf3SSahitya Tummala 						int type)
1908a7d10cf3SSahitya Tummala {
1909a7d10cf3SSahitya Tummala 	unsigned long interval = sbi->interval_time[type] * HZ;
1910a7d10cf3SSahitya Tummala 	unsigned int wait_ms = 0;
1911a7d10cf3SSahitya Tummala 	long delta;
1912a7d10cf3SSahitya Tummala 
1913a7d10cf3SSahitya Tummala 	delta = (sbi->last_time[type] + interval) - jiffies;
1914a7d10cf3SSahitya Tummala 	if (delta > 0)
1915a7d10cf3SSahitya Tummala 		wait_ms = jiffies_to_msecs(delta);
1916a7d10cf3SSahitya Tummala 
1917a7d10cf3SSahitya Tummala 	return wait_ms;
1918a7d10cf3SSahitya Tummala }
1919a7d10cf3SSahitya Tummala 
192039a53e0cSJaegeuk Kim /*
192139a53e0cSJaegeuk Kim  * Inline functions
192239a53e0cSJaegeuk Kim  */
__f2fs_crc32(struct f2fs_sb_info * sbi,u32 crc,const void * address,unsigned int length)1923416d2dbbSChao Yu static inline u32 __f2fs_crc32(struct f2fs_sb_info *sbi, u32 crc,
1924704956ecSChao Yu 			      const void *address, unsigned int length)
1925704956ecSChao Yu {
1926704956ecSChao Yu 	struct {
1927704956ecSChao Yu 		struct shash_desc shash;
1928704956ecSChao Yu 		char ctx[4];
1929704956ecSChao Yu 	} desc;
1930704956ecSChao Yu 	int err;
1931704956ecSChao Yu 
1932704956ecSChao Yu 	BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver) != sizeof(desc.ctx));
1933704956ecSChao Yu 
1934704956ecSChao Yu 	desc.shash.tfm = sbi->s_chksum_driver;
1935704956ecSChao Yu 	*(u32 *)desc.ctx = crc;
1936704956ecSChao Yu 
1937704956ecSChao Yu 	err = crypto_shash_update(&desc.shash, address, length);
1938704956ecSChao Yu 	BUG_ON(err);
1939704956ecSChao Yu 
1940704956ecSChao Yu 	return *(u32 *)desc.ctx;
1941704956ecSChao Yu }
1942704956ecSChao Yu 
f2fs_crc32(struct f2fs_sb_info * sbi,const void * address,unsigned int length)1943416d2dbbSChao Yu static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
1944416d2dbbSChao Yu 			   unsigned int length)
1945416d2dbbSChao Yu {
1946416d2dbbSChao Yu 	return __f2fs_crc32(sbi, F2FS_SUPER_MAGIC, address, length);
1947416d2dbbSChao Yu }
1948416d2dbbSChao Yu 
f2fs_crc_valid(struct f2fs_sb_info * sbi,__u32 blk_crc,void * buf,size_t buf_size)1949416d2dbbSChao Yu static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
1950416d2dbbSChao Yu 				  void *buf, size_t buf_size)
1951416d2dbbSChao Yu {
1952416d2dbbSChao Yu 	return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
1953416d2dbbSChao Yu }
1954416d2dbbSChao Yu 
f2fs_chksum(struct f2fs_sb_info * sbi,u32 crc,const void * address,unsigned int length)1955416d2dbbSChao Yu static inline u32 f2fs_chksum(struct f2fs_sb_info *sbi, u32 crc,
1956416d2dbbSChao Yu 			      const void *address, unsigned int length)
1957416d2dbbSChao Yu {
1958416d2dbbSChao Yu 	return __f2fs_crc32(sbi, crc, address, length);
1959416d2dbbSChao Yu }
1960416d2dbbSChao Yu 
F2FS_I(struct inode * inode)196139a53e0cSJaegeuk Kim static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
196239a53e0cSJaegeuk Kim {
196339a53e0cSJaegeuk Kim 	return container_of(inode, struct f2fs_inode_info, vfs_inode);
196439a53e0cSJaegeuk Kim }
196539a53e0cSJaegeuk Kim 
F2FS_SB(struct super_block * sb)196639a53e0cSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
196739a53e0cSJaegeuk Kim {
196839a53e0cSJaegeuk Kim 	return sb->s_fs_info;
196939a53e0cSJaegeuk Kim }
197039a53e0cSJaegeuk Kim 
F2FS_I_SB(struct inode * inode)19714081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
19724081363fSJaegeuk Kim {
19734081363fSJaegeuk Kim 	return F2FS_SB(inode->i_sb);
19744081363fSJaegeuk Kim }
19754081363fSJaegeuk Kim 
F2FS_M_SB(struct address_space * mapping)19764081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
19774081363fSJaegeuk Kim {
19784081363fSJaegeuk Kim 	return F2FS_I_SB(mapping->host);
19794081363fSJaegeuk Kim }
19804081363fSJaegeuk Kim 
F2FS_P_SB(struct page * page)19814081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
19824081363fSJaegeuk Kim {
19834969c06aSJaegeuk Kim 	return F2FS_M_SB(page_file_mapping(page));
19844081363fSJaegeuk Kim }
19854081363fSJaegeuk Kim 
F2FS_RAW_SUPER(struct f2fs_sb_info * sbi)198639a53e0cSJaegeuk Kim static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
198739a53e0cSJaegeuk Kim {
198839a53e0cSJaegeuk Kim 	return (struct f2fs_super_block *)(sbi->raw_super);
198939a53e0cSJaegeuk Kim }
199039a53e0cSJaegeuk Kim 
F2FS_CKPT(struct f2fs_sb_info * sbi)199139a53e0cSJaegeuk Kim static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
199239a53e0cSJaegeuk Kim {
199339a53e0cSJaegeuk Kim 	return (struct f2fs_checkpoint *)(sbi->ckpt);
199439a53e0cSJaegeuk Kim }
199539a53e0cSJaegeuk Kim 
F2FS_NODE(struct page * page)199645590710SGu Zheng static inline struct f2fs_node *F2FS_NODE(struct page *page)
199745590710SGu Zheng {
199845590710SGu Zheng 	return (struct f2fs_node *)page_address(page);
199945590710SGu Zheng }
200045590710SGu Zheng 
F2FS_INODE(struct page * page)200158bfaf44SJaegeuk Kim static inline struct f2fs_inode *F2FS_INODE(struct page *page)
200258bfaf44SJaegeuk Kim {
200358bfaf44SJaegeuk Kim 	return &((struct f2fs_node *)page_address(page))->i;
200458bfaf44SJaegeuk Kim }
200558bfaf44SJaegeuk Kim 
NM_I(struct f2fs_sb_info * sbi)200639a53e0cSJaegeuk Kim static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
200739a53e0cSJaegeuk Kim {
200839a53e0cSJaegeuk Kim 	return (struct f2fs_nm_info *)(sbi->nm_info);
200939a53e0cSJaegeuk Kim }
201039a53e0cSJaegeuk Kim 
SM_I(struct f2fs_sb_info * sbi)201139a53e0cSJaegeuk Kim static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
201239a53e0cSJaegeuk Kim {
201339a53e0cSJaegeuk Kim 	return (struct f2fs_sm_info *)(sbi->sm_info);
201439a53e0cSJaegeuk Kim }
201539a53e0cSJaegeuk Kim 
SIT_I(struct f2fs_sb_info * sbi)201639a53e0cSJaegeuk Kim static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
201739a53e0cSJaegeuk Kim {
201839a53e0cSJaegeuk Kim 	return (struct sit_info *)(SM_I(sbi)->sit_info);
201939a53e0cSJaegeuk Kim }
202039a53e0cSJaegeuk Kim 
FREE_I(struct f2fs_sb_info * sbi)202139a53e0cSJaegeuk Kim static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
202239a53e0cSJaegeuk Kim {
202339a53e0cSJaegeuk Kim 	return (struct free_segmap_info *)(SM_I(sbi)->free_info);
202439a53e0cSJaegeuk Kim }
202539a53e0cSJaegeuk Kim 
DIRTY_I(struct f2fs_sb_info * sbi)202639a53e0cSJaegeuk Kim static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
202739a53e0cSJaegeuk Kim {
202839a53e0cSJaegeuk Kim 	return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
202939a53e0cSJaegeuk Kim }
203039a53e0cSJaegeuk Kim 
META_MAPPING(struct f2fs_sb_info * sbi)20319df27d98SGu Zheng static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
20329df27d98SGu Zheng {
20339df27d98SGu Zheng 	return sbi->meta_inode->i_mapping;
20349df27d98SGu Zheng }
20359df27d98SGu Zheng 
NODE_MAPPING(struct f2fs_sb_info * sbi)20364ef51a8fSJaegeuk Kim static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
20374ef51a8fSJaegeuk Kim {
20384ef51a8fSJaegeuk Kim 	return sbi->node_inode->i_mapping;
20394ef51a8fSJaegeuk Kim }
20404ef51a8fSJaegeuk Kim 
is_sbi_flag_set(struct f2fs_sb_info * sbi,unsigned int type)2041caf0047eSChao Yu static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
204239a53e0cSJaegeuk Kim {
2043fadb2fb8SChao Yu 	return test_bit(type, &sbi->s_flag);
204439a53e0cSJaegeuk Kim }
204539a53e0cSJaegeuk Kim 
set_sbi_flag(struct f2fs_sb_info * sbi,unsigned int type)2046caf0047eSChao Yu static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
204739a53e0cSJaegeuk Kim {
2048fadb2fb8SChao Yu 	set_bit(type, &sbi->s_flag);
2049caf0047eSChao Yu }
2050caf0047eSChao Yu 
clear_sbi_flag(struct f2fs_sb_info * sbi,unsigned int type)2051caf0047eSChao Yu static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
2052caf0047eSChao Yu {
2053fadb2fb8SChao Yu 	clear_bit(type, &sbi->s_flag);
205439a53e0cSJaegeuk Kim }
205539a53e0cSJaegeuk Kim 
cur_cp_version(struct f2fs_checkpoint * cp)2056d71b5564SJaegeuk Kim static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
2057d71b5564SJaegeuk Kim {
2058d71b5564SJaegeuk Kim 	return le64_to_cpu(cp->checkpoint_ver);
2059d71b5564SJaegeuk Kim }
2060d71b5564SJaegeuk Kim 
f2fs_qf_ino(struct super_block * sb,int type)2061ea676733SJaegeuk Kim static inline unsigned long f2fs_qf_ino(struct super_block *sb, int type)
2062ea676733SJaegeuk Kim {
2063ea676733SJaegeuk Kim 	if (type < F2FS_MAX_QUOTAS)
2064ea676733SJaegeuk Kim 		return le32_to_cpu(F2FS_SB(sb)->raw_super->qf_ino[type]);
2065ea676733SJaegeuk Kim 	return 0;
2066ea676733SJaegeuk Kim }
2067ea676733SJaegeuk Kim 
cur_cp_crc(struct f2fs_checkpoint * cp)2068ced2c7eaSKinglong Mee static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
2069ced2c7eaSKinglong Mee {
2070ced2c7eaSKinglong Mee 	size_t crc_offset = le32_to_cpu(cp->checksum_offset);
2071ced2c7eaSKinglong Mee 	return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
2072ced2c7eaSKinglong Mee }
2073ced2c7eaSKinglong Mee 
__is_set_ckpt_flags(struct f2fs_checkpoint * cp,unsigned int f)2074aaec2b1dSChao Yu static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
207525ca923bSJaegeuk Kim {
207625ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
2077aaec2b1dSChao Yu 
207825ca923bSJaegeuk Kim 	return ckpt_flags & f;
207925ca923bSJaegeuk Kim }
208025ca923bSJaegeuk Kim 
is_set_ckpt_flags(struct f2fs_sb_info * sbi,unsigned int f)2081aaec2b1dSChao Yu static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
208225ca923bSJaegeuk Kim {
2083aaec2b1dSChao Yu 	return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
2084aaec2b1dSChao Yu }
2085aaec2b1dSChao Yu 
__set_ckpt_flags(struct f2fs_checkpoint * cp,unsigned int f)2086aaec2b1dSChao Yu static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
2087aaec2b1dSChao Yu {
2088aaec2b1dSChao Yu 	unsigned int ckpt_flags;
2089aaec2b1dSChao Yu 
2090aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
209125ca923bSJaegeuk Kim 	ckpt_flags |= f;
209225ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
209325ca923bSJaegeuk Kim }
209425ca923bSJaegeuk Kim 
set_ckpt_flags(struct f2fs_sb_info * sbi,unsigned int f)2095aaec2b1dSChao Yu static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
209625ca923bSJaegeuk Kim {
2097d1aa2453SChao Yu 	unsigned long flags;
2098d1aa2453SChao Yu 
2099d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
2100aaec2b1dSChao Yu 	__set_ckpt_flags(F2FS_CKPT(sbi), f);
2101d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
2102aaec2b1dSChao Yu }
2103aaec2b1dSChao Yu 
__clear_ckpt_flags(struct f2fs_checkpoint * cp,unsigned int f)2104aaec2b1dSChao Yu static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
2105aaec2b1dSChao Yu {
2106aaec2b1dSChao Yu 	unsigned int ckpt_flags;
2107aaec2b1dSChao Yu 
2108aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
210925ca923bSJaegeuk Kim 	ckpt_flags &= (~f);
211025ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
211125ca923bSJaegeuk Kim }
211225ca923bSJaegeuk Kim 
clear_ckpt_flags(struct f2fs_sb_info * sbi,unsigned int f)2113aaec2b1dSChao Yu static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
2114aaec2b1dSChao Yu {
2115d1aa2453SChao Yu 	unsigned long flags;
2116d1aa2453SChao Yu 
2117d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
2118aaec2b1dSChao Yu 	__clear_ckpt_flags(F2FS_CKPT(sbi), f);
2119d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
2120aaec2b1dSChao Yu }
2121aaec2b1dSChao Yu 
2122c7f91bd4SBart Van Assche #define init_f2fs_rwsem(sem)					\
2123c7f91bd4SBart Van Assche do {								\
2124c7f91bd4SBart Van Assche 	static struct lock_class_key __key;			\
2125c7f91bd4SBart Van Assche 								\
2126c7f91bd4SBart Van Assche 	__init_f2fs_rwsem((sem), #sem, &__key);			\
2127c7f91bd4SBart Van Assche } while (0)
2128c7f91bd4SBart Van Assche 
__init_f2fs_rwsem(struct f2fs_rwsem * sem,const char * sem_name,struct lock_class_key * key)2129c7f91bd4SBart Van Assche static inline void __init_f2fs_rwsem(struct f2fs_rwsem *sem,
2130c7f91bd4SBart Van Assche 		const char *sem_name, struct lock_class_key *key)
2131e4544b63STim Murray {
2132c7f91bd4SBart Van Assche 	__init_rwsem(&sem->internal_rwsem, sem_name, key);
21337f8e249dSJaegeuk Kim #ifdef CONFIG_F2FS_UNFAIR_RWSEM
2134e4544b63STim Murray 	init_waitqueue_head(&sem->read_waiters);
21357f8e249dSJaegeuk Kim #endif
2136e4544b63STim Murray }
2137e4544b63STim Murray 
f2fs_rwsem_is_locked(struct f2fs_rwsem * sem)2138e4544b63STim Murray static inline int f2fs_rwsem_is_locked(struct f2fs_rwsem *sem)
2139e4544b63STim Murray {
2140e4544b63STim Murray 	return rwsem_is_locked(&sem->internal_rwsem);
2141e4544b63STim Murray }
2142e4544b63STim Murray 
f2fs_rwsem_is_contended(struct f2fs_rwsem * sem)2143e4544b63STim Murray static inline int f2fs_rwsem_is_contended(struct f2fs_rwsem *sem)
2144e4544b63STim Murray {
2145e4544b63STim Murray 	return rwsem_is_contended(&sem->internal_rwsem);
2146e4544b63STim Murray }
2147e4544b63STim Murray 
f2fs_down_read(struct f2fs_rwsem * sem)2148e4544b63STim Murray static inline void f2fs_down_read(struct f2fs_rwsem *sem)
2149e4544b63STim Murray {
21507f8e249dSJaegeuk Kim #ifdef CONFIG_F2FS_UNFAIR_RWSEM
2151e4544b63STim Murray 	wait_event(sem->read_waiters, down_read_trylock(&sem->internal_rwsem));
21527f8e249dSJaegeuk Kim #else
21537f8e249dSJaegeuk Kim 	down_read(&sem->internal_rwsem);
21547f8e249dSJaegeuk Kim #endif
2155e4544b63STim Murray }
2156e4544b63STim Murray 
f2fs_down_read_trylock(struct f2fs_rwsem * sem)2157e4544b63STim Murray static inline int f2fs_down_read_trylock(struct f2fs_rwsem *sem)
2158e4544b63STim Murray {
2159e4544b63STim Murray 	return down_read_trylock(&sem->internal_rwsem);
2160e4544b63STim Murray }
2161e4544b63STim Murray 
f2fs_up_read(struct f2fs_rwsem * sem)2162e4544b63STim Murray static inline void f2fs_up_read(struct f2fs_rwsem *sem)
2163e4544b63STim Murray {
2164e4544b63STim Murray 	up_read(&sem->internal_rwsem);
2165e4544b63STim Murray }
2166e4544b63STim Murray 
f2fs_down_write(struct f2fs_rwsem * sem)2167e4544b63STim Murray static inline void f2fs_down_write(struct f2fs_rwsem *sem)
2168e4544b63STim Murray {
2169e4544b63STim Murray 	down_write(&sem->internal_rwsem);
2170e4544b63STim Murray }
2171e4544b63STim Murray 
21725c13e238SJaegeuk Kim #ifdef CONFIG_DEBUG_LOCK_ALLOC
f2fs_down_read_nested(struct f2fs_rwsem * sem,int subclass)21735c13e238SJaegeuk Kim static inline void f2fs_down_read_nested(struct f2fs_rwsem *sem, int subclass)
21745c13e238SJaegeuk Kim {
21755c13e238SJaegeuk Kim 	down_read_nested(&sem->internal_rwsem, subclass);
21765c13e238SJaegeuk Kim }
21775c13e238SJaegeuk Kim 
f2fs_down_write_nested(struct f2fs_rwsem * sem,int subclass)21785c13e238SJaegeuk Kim static inline void f2fs_down_write_nested(struct f2fs_rwsem *sem, int subclass)
21795c13e238SJaegeuk Kim {
21805c13e238SJaegeuk Kim 	down_write_nested(&sem->internal_rwsem, subclass);
21815c13e238SJaegeuk Kim }
21825c13e238SJaegeuk Kim #else
21835c13e238SJaegeuk Kim #define f2fs_down_read_nested(sem, subclass) f2fs_down_read(sem)
21845c13e238SJaegeuk Kim #define f2fs_down_write_nested(sem, subclass) f2fs_down_write(sem)
21855c13e238SJaegeuk Kim #endif
21865c13e238SJaegeuk Kim 
f2fs_down_write_trylock(struct f2fs_rwsem * sem)2187e4544b63STim Murray static inline int f2fs_down_write_trylock(struct f2fs_rwsem *sem)
2188e4544b63STim Murray {
2189e4544b63STim Murray 	return down_write_trylock(&sem->internal_rwsem);
2190e4544b63STim Murray }
2191e4544b63STim Murray 
f2fs_up_write(struct f2fs_rwsem * sem)2192e4544b63STim Murray static inline void f2fs_up_write(struct f2fs_rwsem *sem)
2193e4544b63STim Murray {
2194e4544b63STim Murray 	up_write(&sem->internal_rwsem);
21957f8e249dSJaegeuk Kim #ifdef CONFIG_F2FS_UNFAIR_RWSEM
2196e4544b63STim Murray 	wake_up_all(&sem->read_waiters);
21977f8e249dSJaegeuk Kim #endif
2198e4544b63STim Murray }
2199e4544b63STim Murray 
f2fs_lock_op(struct f2fs_sb_info * sbi)2200e479556bSGu Zheng static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
220139a53e0cSJaegeuk Kim {
2202e4544b63STim Murray 	f2fs_down_read(&sbi->cp_rwsem);
220339a53e0cSJaegeuk Kim }
220439a53e0cSJaegeuk Kim 
f2fs_trylock_op(struct f2fs_sb_info * sbi)2205cc15620bSJaegeuk Kim static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi)
2206cc15620bSJaegeuk Kim {
2207c40e15a9SYangtao Li 	if (time_to_inject(sbi, FAULT_LOCK_OP))
22083e020389SChao Yu 		return 0;
2209e4544b63STim Murray 	return f2fs_down_read_trylock(&sbi->cp_rwsem);
2210cc15620bSJaegeuk Kim }
2211cc15620bSJaegeuk Kim 
f2fs_unlock_op(struct f2fs_sb_info * sbi)2212e479556bSGu Zheng static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
221339a53e0cSJaegeuk Kim {
2214e4544b63STim Murray 	f2fs_up_read(&sbi->cp_rwsem);
221539936837SJaegeuk Kim }
221639936837SJaegeuk Kim 
f2fs_lock_all(struct f2fs_sb_info * sbi)2217e479556bSGu Zheng static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
221839936837SJaegeuk Kim {
2219e4544b63STim Murray 	f2fs_down_write(&sbi->cp_rwsem);
222039936837SJaegeuk Kim }
222139936837SJaegeuk Kim 
f2fs_unlock_all(struct f2fs_sb_info * sbi)2222e479556bSGu Zheng static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
222339936837SJaegeuk Kim {
2224e4544b63STim Murray 	f2fs_up_write(&sbi->cp_rwsem);
222539a53e0cSJaegeuk Kim }
222639a53e0cSJaegeuk Kim 
__get_cp_reason(struct f2fs_sb_info * sbi)2227119ee914SJaegeuk Kim static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
2228119ee914SJaegeuk Kim {
2229119ee914SJaegeuk Kim 	int reason = CP_SYNC;
2230119ee914SJaegeuk Kim 
2231119ee914SJaegeuk Kim 	if (test_opt(sbi, FASTBOOT))
2232119ee914SJaegeuk Kim 		reason = CP_FASTBOOT;
2233119ee914SJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
2234119ee914SJaegeuk Kim 		reason = CP_UMOUNT;
2235119ee914SJaegeuk Kim 	return reason;
2236119ee914SJaegeuk Kim }
2237119ee914SJaegeuk Kim 
__remain_node_summaries(int reason)2238119ee914SJaegeuk Kim static inline bool __remain_node_summaries(int reason)
2239119ee914SJaegeuk Kim {
2240c473f1a9SChao Yu 	return (reason & (CP_UMOUNT | CP_FASTBOOT));
2241119ee914SJaegeuk Kim }
2242119ee914SJaegeuk Kim 
__exist_node_summaries(struct f2fs_sb_info * sbi)2243119ee914SJaegeuk Kim static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
2244119ee914SJaegeuk Kim {
2245aaec2b1dSChao Yu 	return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
2246aaec2b1dSChao Yu 			is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
2247119ee914SJaegeuk Kim }
2248119ee914SJaegeuk Kim 
224939a53e0cSJaegeuk Kim /*
225039a53e0cSJaegeuk Kim  * Check whether the inode has blocks or not
225139a53e0cSJaegeuk Kim  */
F2FS_HAS_BLOCKS(struct inode * inode)225239a53e0cSJaegeuk Kim static inline int F2FS_HAS_BLOCKS(struct inode *inode)
225339a53e0cSJaegeuk Kim {
22540eb0adadSChao Yu 	block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
22550eb0adadSChao Yu 
2256000519f2SChao Yu 	return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block;
225739a53e0cSJaegeuk Kim }
225839a53e0cSJaegeuk Kim 
f2fs_has_xattr_block(unsigned int ofs)22594bc8e9bcSChao Yu static inline bool f2fs_has_xattr_block(unsigned int ofs)
22604bc8e9bcSChao Yu {
22614bc8e9bcSChao Yu 	return ofs == XATTR_NODE_OFFSET;
22624bc8e9bcSChao Yu }
22634bc8e9bcSChao Yu 
__allow_reserved_blocks(struct f2fs_sb_info * sbi,struct inode * inode,bool cap)2264d8a9a229SJaegeuk Kim static inline bool __allow_reserved_blocks(struct f2fs_sb_info *sbi,
2265a90a0884SJaegeuk Kim 					struct inode *inode, bool cap)
22667c2e5963SJaegeuk Kim {
2267d8a9a229SJaegeuk Kim 	if (!inode)
2268d8a9a229SJaegeuk Kim 		return true;
22697c2e5963SJaegeuk Kim 	if (!test_opt(sbi, RESERVE_ROOT))
22707c2e5963SJaegeuk Kim 		return false;
2271d8a9a229SJaegeuk Kim 	if (IS_NOQUOTA(inode))
2272d8a9a229SJaegeuk Kim 		return true;
227363189b78SChao Yu 	if (uid_eq(F2FS_OPTION(sbi).s_resuid, current_fsuid()))
22747c2e5963SJaegeuk Kim 		return true;
227563189b78SChao Yu 	if (!gid_eq(F2FS_OPTION(sbi).s_resgid, GLOBAL_ROOT_GID) &&
227663189b78SChao Yu 					in_group_p(F2FS_OPTION(sbi).s_resgid))
22777c2e5963SJaegeuk Kim 		return true;
2278a90a0884SJaegeuk Kim 	if (cap && capable(CAP_SYS_RESOURCE))
2279162b27aeSJaegeuk Kim 		return true;
22807c2e5963SJaegeuk Kim 	return false;
22817c2e5963SJaegeuk Kim }
22827c2e5963SJaegeuk Kim 
get_available_block_count(struct f2fs_sb_info * sbi,struct inode * inode,bool cap)2283b8a742a8SChao Yu static inline unsigned int get_available_block_count(struct f2fs_sb_info *sbi,
2284b8a742a8SChao Yu 						struct inode *inode, bool cap)
2285b8a742a8SChao Yu {
2286b8a742a8SChao Yu 	block_t avail_user_block_count;
2287b8a742a8SChao Yu 
2288b8a742a8SChao Yu 	avail_user_block_count = sbi->user_block_count -
2289b8a742a8SChao Yu 					sbi->current_reserved_blocks;
2290b8a742a8SChao Yu 
2291b8a742a8SChao Yu 	if (!__allow_reserved_blocks(sbi, inode, cap))
2292b8a742a8SChao Yu 		avail_user_block_count -= F2FS_OPTION(sbi).root_reserved_blocks;
2293b8a742a8SChao Yu 
2294b8a742a8SChao Yu 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2295b8a742a8SChao Yu 		if (avail_user_block_count > sbi->unusable_block_count)
2296b8a742a8SChao Yu 			avail_user_block_count -= sbi->unusable_block_count;
2297b8a742a8SChao Yu 		else
2298b8a742a8SChao Yu 			avail_user_block_count = 0;
2299b8a742a8SChao Yu 	}
2300b8a742a8SChao Yu 
2301b8a742a8SChao Yu 	return avail_user_block_count;
2302b8a742a8SChao Yu }
2303b8a742a8SChao Yu 
23040abd675eSChao Yu static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool);
inc_valid_block_count(struct f2fs_sb_info * sbi,struct inode * inode,blkcnt_t * count,bool partial)23050abd675eSChao Yu static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
2306bc1e3992SChao Yu 				 struct inode *inode, blkcnt_t *count, bool partial)
230739a53e0cSJaegeuk Kim {
230881c97590SChao Yu 	long long diff = 0, release = 0;
2309daeb433eSChao Yu 	block_t avail_user_block_count;
23100abd675eSChao Yu 	int ret;
23110abd675eSChao Yu 
23120abd675eSChao Yu 	ret = dquot_reserve_block(inode, *count);
23130abd675eSChao Yu 	if (ret)
23140abd675eSChao Yu 		return ret;
2315dd11a5dfSJaegeuk Kim 
231655523519SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
23170abd675eSChao Yu 		release = *count;
23189ea2f0beSChao Yu 		goto release_quota;
231955523519SChao Yu 	}
23207fa750a1SArnd Bergmann 
2321dd11a5dfSJaegeuk Kim 	/*
2322dd11a5dfSJaegeuk Kim 	 * let's increase this in prior to actual block count change in order
2323dd11a5dfSJaegeuk Kim 	 * for f2fs_sync_file to avoid data races when deciding checkpoint.
2324dd11a5dfSJaegeuk Kim 	 */
2325dd11a5dfSJaegeuk Kim 	percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
232639a53e0cSJaegeuk Kim 
232739a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
23287e65be49SJaegeuk Kim 
232981c97590SChao Yu 	avail_user_block_count = get_available_block_count(sbi, inode, true);
233081c97590SChao Yu 	diff = (long long)sbi->total_valid_block_count + *count -
233181c97590SChao Yu 						avail_user_block_count;
233281c97590SChao Yu 	if (unlikely(diff > 0)) {
2333bc1e3992SChao Yu 		if (!partial) {
2334bc1e3992SChao Yu 			spin_unlock(&sbi->stat_lock);
233581c97590SChao Yu 			release = *count;
2336bc1e3992SChao Yu 			goto enospc;
2337bc1e3992SChao Yu 		}
23387e65be49SJaegeuk Kim 		if (diff > *count)
23397e65be49SJaegeuk Kim 			diff = *count;
2340dd11a5dfSJaegeuk Kim 		*count -= diff;
23410abd675eSChao Yu 		release = diff;
234246008c6dSChao Yu 		if (!*count) {
234339a53e0cSJaegeuk Kim 			spin_unlock(&sbi->stat_lock);
23440abd675eSChao Yu 			goto enospc;
234539a53e0cSJaegeuk Kim 		}
234646008c6dSChao Yu 	}
234781c97590SChao Yu 	sbi->total_valid_block_count += (block_t)(*count);
234881c97590SChao Yu 
234939a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
235041382ec4SJaegeuk Kim 
235136b877afSDaniel Rosenberg 	if (unlikely(release)) {
235236b877afSDaniel Rosenberg 		percpu_counter_sub(&sbi->alloc_valid_block_count, release);
23530abd675eSChao Yu 		dquot_release_reservation_block(inode, release);
235436b877afSDaniel Rosenberg 	}
23550abd675eSChao Yu 	f2fs_i_blocks_write(inode, *count, true, true);
23560abd675eSChao Yu 	return 0;
23570abd675eSChao Yu 
23580abd675eSChao Yu enospc:
235936b877afSDaniel Rosenberg 	percpu_counter_sub(&sbi->alloc_valid_block_count, release);
23609ea2f0beSChao Yu release_quota:
23610abd675eSChao Yu 	dquot_release_reservation_block(inode, release);
23620abd675eSChao Yu 	return -ENOSPC;
236339a53e0cSJaegeuk Kim }
236439a53e0cSJaegeuk Kim 
2365635a52daSChao Yu #define PAGE_PRIVATE_GET_FUNC(name, flagname) \
2366635a52daSChao Yu static inline bool page_private_##name(struct page *page) \
2367635a52daSChao Yu { \
2368635a52daSChao Yu 	return PagePrivate(page) && \
2369635a52daSChao Yu 		test_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page)) && \
2370635a52daSChao Yu 		test_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
2371635a52daSChao Yu }
2372635a52daSChao Yu 
2373635a52daSChao Yu #define PAGE_PRIVATE_SET_FUNC(name, flagname) \
2374635a52daSChao Yu static inline void set_page_private_##name(struct page *page) \
2375635a52daSChao Yu { \
2376635a52daSChao Yu 	if (!PagePrivate(page)) \
2377635a52daSChao Yu 		attach_page_private(page, (void *)0); \
2378635a52daSChao Yu 	set_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page)); \
2379635a52daSChao Yu 	set_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
2380635a52daSChao Yu }
2381635a52daSChao Yu 
2382635a52daSChao Yu #define PAGE_PRIVATE_CLEAR_FUNC(name, flagname) \
2383635a52daSChao Yu static inline void clear_page_private_##name(struct page *page) \
2384635a52daSChao Yu { \
2385635a52daSChao Yu 	clear_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
2386635a52daSChao Yu 	if (page_private(page) == BIT(PAGE_PRIVATE_NOT_POINTER)) \
2387635a52daSChao Yu 		detach_page_private(page); \
2388635a52daSChao Yu }
2389635a52daSChao Yu 
2390635a52daSChao Yu PAGE_PRIVATE_GET_FUNC(nonpointer, NOT_POINTER);
2391635a52daSChao Yu PAGE_PRIVATE_GET_FUNC(inline, INLINE_INODE);
2392635a52daSChao Yu PAGE_PRIVATE_GET_FUNC(gcing, ONGOING_MIGRATION);
2393d889928bSChao Yu PAGE_PRIVATE_GET_FUNC(atomic, ATOMIC_WRITE);
2394635a52daSChao Yu 
2395635a52daSChao Yu PAGE_PRIVATE_SET_FUNC(reference, REF_RESOURCE);
2396635a52daSChao Yu PAGE_PRIVATE_SET_FUNC(inline, INLINE_INODE);
2397635a52daSChao Yu PAGE_PRIVATE_SET_FUNC(gcing, ONGOING_MIGRATION);
2398d889928bSChao Yu PAGE_PRIVATE_SET_FUNC(atomic, ATOMIC_WRITE);
2399635a52daSChao Yu 
2400635a52daSChao Yu PAGE_PRIVATE_CLEAR_FUNC(reference, REF_RESOURCE);
2401635a52daSChao Yu PAGE_PRIVATE_CLEAR_FUNC(inline, INLINE_INODE);
2402635a52daSChao Yu PAGE_PRIVATE_CLEAR_FUNC(gcing, ONGOING_MIGRATION);
2403d889928bSChao Yu PAGE_PRIVATE_CLEAR_FUNC(atomic, ATOMIC_WRITE);
2404635a52daSChao Yu 
get_page_private_data(struct page * page)2405635a52daSChao Yu static inline unsigned long get_page_private_data(struct page *page)
2406635a52daSChao Yu {
2407635a52daSChao Yu 	unsigned long data = page_private(page);
2408635a52daSChao Yu 
2409635a52daSChao Yu 	if (!test_bit(PAGE_PRIVATE_NOT_POINTER, &data))
2410635a52daSChao Yu 		return 0;
2411635a52daSChao Yu 	return data >> PAGE_PRIVATE_MAX;
2412635a52daSChao Yu }
2413635a52daSChao Yu 
set_page_private_data(struct page * page,unsigned long data)2414635a52daSChao Yu static inline void set_page_private_data(struct page *page, unsigned long data)
2415635a52daSChao Yu {
2416635a52daSChao Yu 	if (!PagePrivate(page))
2417635a52daSChao Yu 		attach_page_private(page, (void *)0);
2418635a52daSChao Yu 	set_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page));
2419635a52daSChao Yu 	page_private(page) |= data << PAGE_PRIVATE_MAX;
2420635a52daSChao Yu }
2421635a52daSChao Yu 
clear_page_private_data(struct page * page)2422635a52daSChao Yu static inline void clear_page_private_data(struct page *page)
2423635a52daSChao Yu {
2424635a52daSChao Yu 	page_private(page) &= GENMASK(PAGE_PRIVATE_MAX - 1, 0);
2425635a52daSChao Yu 	if (page_private(page) == BIT(PAGE_PRIVATE_NOT_POINTER))
2426635a52daSChao Yu 		detach_page_private(page);
2427635a52daSChao Yu }
2428635a52daSChao Yu 
clear_page_private_all(struct page * page)2429635a52daSChao Yu static inline void clear_page_private_all(struct page *page)
2430635a52daSChao Yu {
2431635a52daSChao Yu 	clear_page_private_data(page);
2432635a52daSChao Yu 	clear_page_private_reference(page);
2433635a52daSChao Yu 	clear_page_private_gcing(page);
2434635a52daSChao Yu 	clear_page_private_inline(page);
2435d889928bSChao Yu 	clear_page_private_atomic(page);
2436635a52daSChao Yu 
2437635a52daSChao Yu 	f2fs_bug_on(F2FS_P_SB(page), page_private(page));
2438635a52daSChao Yu }
2439635a52daSChao Yu 
dec_valid_block_count(struct f2fs_sb_info * sbi,struct inode * inode,block_t count)2440da19b0dcSGu Zheng static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
244139a53e0cSJaegeuk Kim 						struct inode *inode,
24420eb0adadSChao Yu 						block_t count)
244339a53e0cSJaegeuk Kim {
24440eb0adadSChao Yu 	blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK;
24450eb0adadSChao Yu 
244639a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
24479850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
244839a53e0cSJaegeuk Kim 	sbi->total_valid_block_count -= (block_t)count;
244980d42145SYunlong Song 	if (sbi->reserved_blocks &&
245080d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
245180d42145SYunlong Song 		sbi->current_reserved_blocks = min(sbi->reserved_blocks,
245280d42145SYunlong Song 					sbi->current_reserved_blocks + count);
245339a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
24545e159cd3SChao Yu 	if (unlikely(inode->i_blocks < sectors)) {
2455dcbb4c10SJoe Perches 		f2fs_warn(sbi, "Inconsistent i_blocks, ino:%lu, iblocks:%llu, sectors:%llu",
24565e159cd3SChao Yu 			  inode->i_ino,
24575e159cd3SChao Yu 			  (unsigned long long)inode->i_blocks,
24585e159cd3SChao Yu 			  (unsigned long long)sectors);
24595e159cd3SChao Yu 		set_sbi_flag(sbi, SBI_NEED_FSCK);
24605e159cd3SChao Yu 		return;
24615e159cd3SChao Yu 	}
24620abd675eSChao Yu 	f2fs_i_blocks_write(inode, count, false, true);
246339a53e0cSJaegeuk Kim }
246439a53e0cSJaegeuk Kim 
inc_page_count(struct f2fs_sb_info * sbi,int count_type)246539a53e0cSJaegeuk Kim static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
246639a53e0cSJaegeuk Kim {
246735782b23SJaegeuk Kim 	atomic_inc(&sbi->nr_pages[count_type]);
24687c4abcbeSChao Yu 
246920109873SChao Yu 	if (count_type == F2FS_DIRTY_DENTS ||
247020109873SChao Yu 			count_type == F2FS_DIRTY_NODES ||
247120109873SChao Yu 			count_type == F2FS_DIRTY_META ||
247220109873SChao Yu 			count_type == F2FS_DIRTY_QDATA ||
247320109873SChao Yu 			count_type == F2FS_DIRTY_IMETA)
2474caf0047eSChao Yu 		set_sbi_flag(sbi, SBI_IS_DIRTY);
247539a53e0cSJaegeuk Kim }
247639a53e0cSJaegeuk Kim 
inode_inc_dirty_pages(struct inode * inode)2477a7ffdbe2SJaegeuk Kim static inline void inode_inc_dirty_pages(struct inode *inode)
247839a53e0cSJaegeuk Kim {
2479204706c7SJaegeuk Kim 	atomic_inc(&F2FS_I(inode)->dirty_pages);
2480c227f912SChao Yu 	inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
2481c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
24822c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
24832c8a4a28SJaegeuk Kim 		inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
248439a53e0cSJaegeuk Kim }
248539a53e0cSJaegeuk Kim 
dec_page_count(struct f2fs_sb_info * sbi,int count_type)248639a53e0cSJaegeuk Kim static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
248739a53e0cSJaegeuk Kim {
248835782b23SJaegeuk Kim 	atomic_dec(&sbi->nr_pages[count_type]);
248939a53e0cSJaegeuk Kim }
249039a53e0cSJaegeuk Kim 
inode_dec_dirty_pages(struct inode * inode)2491a7ffdbe2SJaegeuk Kim static inline void inode_dec_dirty_pages(struct inode *inode)
249239a53e0cSJaegeuk Kim {
24935ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
24945ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
24951fe54f9dSJaegeuk Kim 		return;
24961fe54f9dSJaegeuk Kim 
2497204706c7SJaegeuk Kim 	atomic_dec(&F2FS_I(inode)->dirty_pages);
2498c227f912SChao Yu 	dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
2499c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
25002c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
25012c8a4a28SJaegeuk Kim 		dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
250239a53e0cSJaegeuk Kim }
250339a53e0cSJaegeuk Kim 
inc_atomic_write_cnt(struct inode * inode)2504f8e2f32bSDaeho Jeong static inline void inc_atomic_write_cnt(struct inode *inode)
2505f8e2f32bSDaeho Jeong {
2506f8e2f32bSDaeho Jeong 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2507f8e2f32bSDaeho Jeong 	struct f2fs_inode_info *fi = F2FS_I(inode);
2508f8e2f32bSDaeho Jeong 	u64 current_write;
2509f8e2f32bSDaeho Jeong 
2510f8e2f32bSDaeho Jeong 	fi->atomic_write_cnt++;
2511f8e2f32bSDaeho Jeong 	atomic64_inc(&sbi->current_atomic_write);
2512f8e2f32bSDaeho Jeong 	current_write = atomic64_read(&sbi->current_atomic_write);
2513f8e2f32bSDaeho Jeong 	if (current_write > sbi->peak_atomic_write)
2514f8e2f32bSDaeho Jeong 		sbi->peak_atomic_write = current_write;
2515f8e2f32bSDaeho Jeong }
2516f8e2f32bSDaeho Jeong 
release_atomic_write_cnt(struct inode * inode)2517f8e2f32bSDaeho Jeong static inline void release_atomic_write_cnt(struct inode *inode)
2518f8e2f32bSDaeho Jeong {
2519f8e2f32bSDaeho Jeong 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2520f8e2f32bSDaeho Jeong 	struct f2fs_inode_info *fi = F2FS_I(inode);
2521f8e2f32bSDaeho Jeong 
2522f8e2f32bSDaeho Jeong 	atomic64_sub(fi->atomic_write_cnt, &sbi->current_atomic_write);
2523f8e2f32bSDaeho Jeong 	fi->atomic_write_cnt = 0;
2524f8e2f32bSDaeho Jeong }
2525f8e2f32bSDaeho Jeong 
get_pages(struct f2fs_sb_info * sbi,int count_type)2526523be8a6SJaegeuk Kim static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
252739a53e0cSJaegeuk Kim {
252835782b23SJaegeuk Kim 	return atomic_read(&sbi->nr_pages[count_type]);
252939a53e0cSJaegeuk Kim }
253039a53e0cSJaegeuk Kim 
get_dirty_pages(struct inode * inode)2531204706c7SJaegeuk Kim static inline int get_dirty_pages(struct inode *inode)
2532f8b2c1f9SJaegeuk Kim {
2533204706c7SJaegeuk Kim 	return atomic_read(&F2FS_I(inode)->dirty_pages);
2534f8b2c1f9SJaegeuk Kim }
2535f8b2c1f9SJaegeuk Kim 
get_blocktype_secs(struct f2fs_sb_info * sbi,int block_type)25365ac206cfSNamjae Jeon static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
25375ac206cfSNamjae Jeon {
2538f0248ba6SJaegeuk Kim 	return div_u64(get_pages(sbi, block_type) + BLKS_PER_SEC(sbi) - 1,
2539f0248ba6SJaegeuk Kim 							BLKS_PER_SEC(sbi));
25405ac206cfSNamjae Jeon }
25415ac206cfSNamjae Jeon 
valid_user_blocks(struct f2fs_sb_info * sbi)254239a53e0cSJaegeuk Kim static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
254339a53e0cSJaegeuk Kim {
25448b8343faSJaegeuk Kim 	return sbi->total_valid_block_count;
254539a53e0cSJaegeuk Kim }
254639a53e0cSJaegeuk Kim 
discard_blocks(struct f2fs_sb_info * sbi)2547f83a2584SYunlei He static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
2548f83a2584SYunlei He {
2549f83a2584SYunlei He 	return sbi->discard_blks;
2550f83a2584SYunlei He }
2551f83a2584SYunlei He 
__bitmap_size(struct f2fs_sb_info * sbi,int flag)255239a53e0cSJaegeuk Kim static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
255339a53e0cSJaegeuk Kim {
255439a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
255539a53e0cSJaegeuk Kim 
255639a53e0cSJaegeuk Kim 	/* return NAT or SIT bitmap */
255739a53e0cSJaegeuk Kim 	if (flag == NAT_BITMAP)
255839a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
255939a53e0cSJaegeuk Kim 	else if (flag == SIT_BITMAP)
256039a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
256139a53e0cSJaegeuk Kim 
256239a53e0cSJaegeuk Kim 	return 0;
256339a53e0cSJaegeuk Kim }
256439a53e0cSJaegeuk Kim 
__cp_payload(struct f2fs_sb_info * sbi)256555141486SWanpeng Li static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
256655141486SWanpeng Li {
256755141486SWanpeng Li 	return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
256855141486SWanpeng Li }
256955141486SWanpeng Li 
__bitmap_ptr(struct f2fs_sb_info * sbi,int flag)257039a53e0cSJaegeuk Kim static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
257139a53e0cSJaegeuk Kim {
257239a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
25734260c406SGustavo A. R. Silva 	void *tmp_ptr = &ckpt->sit_nat_version_bitmap;
25741dbe4152SChangman Lee 	int offset;
25751dbe4152SChangman Lee 
2576199bc3feSChao Yu 	if (is_set_ckpt_flags(sbi, CP_LARGE_NAT_BITMAP_FLAG)) {
2577199bc3feSChao Yu 		offset = (flag == SIT_BITMAP) ?
2578199bc3feSChao Yu 			le32_to_cpu(ckpt->nat_ver_bitmap_bytesize) : 0;
2579b471eb99SChao Yu 		/*
2580b471eb99SChao Yu 		 * if large_nat_bitmap feature is enabled, leave checksum
2581b471eb99SChao Yu 		 * protection for all nat/sit bitmaps.
2582b471eb99SChao Yu 		 */
25834260c406SGustavo A. R. Silva 		return tmp_ptr + offset + sizeof(__le32);
2584199bc3feSChao Yu 	}
2585199bc3feSChao Yu 
258655141486SWanpeng Li 	if (__cp_payload(sbi) > 0) {
25871dbe4152SChangman Lee 		if (flag == NAT_BITMAP)
2588280dfeaeSZhang Qilong 			return tmp_ptr;
25891dbe4152SChangman Lee 		else
259065b85cccSJaegeuk Kim 			return (unsigned char *)ckpt + F2FS_BLKSIZE;
25911dbe4152SChangman Lee 	} else {
25921dbe4152SChangman Lee 		offset = (flag == NAT_BITMAP) ?
259325ca923bSJaegeuk Kim 			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
25944260c406SGustavo A. R. Silva 		return tmp_ptr + offset;
259539a53e0cSJaegeuk Kim 	}
25961dbe4152SChangman Lee }
259739a53e0cSJaegeuk Kim 
__start_cp_addr(struct f2fs_sb_info * sbi)259839a53e0cSJaegeuk Kim static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
259939a53e0cSJaegeuk Kim {
26008508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
260139a53e0cSJaegeuk Kim 
26028508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 2)
2603f0248ba6SJaegeuk Kim 		start_addr += BLKS_PER_SEG(sbi);
260439a53e0cSJaegeuk Kim 	return start_addr;
260539a53e0cSJaegeuk Kim }
260639a53e0cSJaegeuk Kim 
__start_cp_next_addr(struct f2fs_sb_info * sbi)26078508e44aSJaegeuk Kim static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
26088508e44aSJaegeuk Kim {
26098508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
26108508e44aSJaegeuk Kim 
26118508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 1)
2612f0248ba6SJaegeuk Kim 		start_addr += BLKS_PER_SEG(sbi);
26138508e44aSJaegeuk Kim 	return start_addr;
26148508e44aSJaegeuk Kim }
26158508e44aSJaegeuk Kim 
__set_cp_next_pack(struct f2fs_sb_info * sbi)26168508e44aSJaegeuk Kim static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
26178508e44aSJaegeuk Kim {
26188508e44aSJaegeuk Kim 	sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
26198508e44aSJaegeuk Kim }
26208508e44aSJaegeuk Kim 
__start_sum_addr(struct f2fs_sb_info * sbi)262139a53e0cSJaegeuk Kim static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
262239a53e0cSJaegeuk Kim {
262339a53e0cSJaegeuk Kim 	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
262439a53e0cSJaegeuk Kim }
262539a53e0cSJaegeuk Kim 
26263bac20a8SJaegeuk Kim extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
inc_valid_node_count(struct f2fs_sb_info * sbi,struct inode * inode,bool is_inode)26270abd675eSChao Yu static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
2628000519f2SChao Yu 					struct inode *inode, bool is_inode)
262939a53e0cSJaegeuk Kim {
263039a53e0cSJaegeuk Kim 	block_t	valid_block_count;
2631b8a742a8SChao Yu 	unsigned int valid_node_count;
2632b8a742a8SChao Yu 	unsigned int avail_user_block_count;
2633af033b2aSChao Yu 	int err;
26340abd675eSChao Yu 
2635af033b2aSChao Yu 	if (is_inode) {
2636af033b2aSChao Yu 		if (inode) {
2637af033b2aSChao Yu 			err = dquot_alloc_inode(inode);
2638af033b2aSChao Yu 			if (err)
2639af033b2aSChao Yu 				return err;
2640af033b2aSChao Yu 		}
2641af033b2aSChao Yu 	} else {
2642af033b2aSChao Yu 		err = dquot_reserve_block(inode, 1);
2643af033b2aSChao Yu 		if (err)
2644af033b2aSChao Yu 			return err;
26450abd675eSChao Yu 	}
264639a53e0cSJaegeuk Kim 
2647c40e15a9SYangtao Li 	if (time_to_inject(sbi, FAULT_BLOCK))
2648812c6056SChao Yu 		goto enospc;
2649812c6056SChao Yu 
265039a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
265139a53e0cSJaegeuk Kim 
2652b8a742a8SChao Yu 	valid_block_count = sbi->total_valid_block_count + 1;
2653b8a742a8SChao Yu 	avail_user_block_count = get_available_block_count(sbi, inode, false);
26547e65be49SJaegeuk Kim 
2655b8a742a8SChao Yu 	if (unlikely(valid_block_count > avail_user_block_count)) {
265639a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
26570abd675eSChao Yu 		goto enospc;
265839a53e0cSJaegeuk Kim 	}
265939a53e0cSJaegeuk Kim 
2660ef86d709SGu Zheng 	valid_node_count = sbi->total_valid_node_count + 1;
2661cfb271d4SChao Yu 	if (unlikely(valid_node_count > sbi->total_node_count)) {
266239a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
26630abd675eSChao Yu 		goto enospc;
266439a53e0cSJaegeuk Kim 	}
266539a53e0cSJaegeuk Kim 
2666ef86d709SGu Zheng 	sbi->total_valid_node_count++;
2667ef86d709SGu Zheng 	sbi->total_valid_block_count++;
266839a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
266939a53e0cSJaegeuk Kim 
26700abd675eSChao Yu 	if (inode) {
26710abd675eSChao Yu 		if (is_inode)
26720abd675eSChao Yu 			f2fs_mark_inode_dirty_sync(inode, true);
26730abd675eSChao Yu 		else
26740abd675eSChao Yu 			f2fs_i_blocks_write(inode, 1, true, true);
26750abd675eSChao Yu 	}
26760abd675eSChao Yu 
267741382ec4SJaegeuk Kim 	percpu_counter_inc(&sbi->alloc_valid_block_count);
26780abd675eSChao Yu 	return 0;
26790abd675eSChao Yu 
26800abd675eSChao Yu enospc:
2681af033b2aSChao Yu 	if (is_inode) {
2682af033b2aSChao Yu 		if (inode)
2683af033b2aSChao Yu 			dquot_free_inode(inode);
2684af033b2aSChao Yu 	} else {
26850abd675eSChao Yu 		dquot_release_reservation_block(inode, 1);
2686af033b2aSChao Yu 	}
26870abd675eSChao Yu 	return -ENOSPC;
268839a53e0cSJaegeuk Kim }
268939a53e0cSJaegeuk Kim 
dec_valid_node_count(struct f2fs_sb_info * sbi,struct inode * inode,bool is_inode)269039a53e0cSJaegeuk Kim static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
2691000519f2SChao Yu 					struct inode *inode, bool is_inode)
269239a53e0cSJaegeuk Kim {
269339a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
269439a53e0cSJaegeuk Kim 
26954d17e6feSChao Yu 	if (unlikely(!sbi->total_valid_block_count ||
26964d17e6feSChao Yu 			!sbi->total_valid_node_count)) {
26974d17e6feSChao Yu 		f2fs_warn(sbi, "dec_valid_node_count: inconsistent block counts, total_valid_block:%u, total_valid_node:%u",
26984d17e6feSChao Yu 			  sbi->total_valid_block_count,
26994d17e6feSChao Yu 			  sbi->total_valid_node_count);
27004d17e6feSChao Yu 		set_sbi_flag(sbi, SBI_NEED_FSCK);
27014d17e6feSChao Yu 	} else {
2702ef86d709SGu Zheng 		sbi->total_valid_block_count--;
27034d17e6feSChao Yu 		sbi->total_valid_node_count--;
27044d17e6feSChao Yu 	}
27054d17e6feSChao Yu 
270680d42145SYunlong Song 	if (sbi->reserved_blocks &&
270780d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
270880d42145SYunlong Song 		sbi->current_reserved_blocks++;
270939a53e0cSJaegeuk Kim 
271039a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
27110abd675eSChao Yu 
2712ea6d7e72SChao Yu 	if (is_inode) {
2713af033b2aSChao Yu 		dquot_free_inode(inode);
2714ea6d7e72SChao Yu 	} else {
2715ea6d7e72SChao Yu 		if (unlikely(inode->i_blocks == 0)) {
2716097a7686SChao Yu 			f2fs_warn(sbi, "dec_valid_node_count: inconsistent i_blocks, ino:%lu, iblocks:%llu",
2717ea6d7e72SChao Yu 				  inode->i_ino,
2718ea6d7e72SChao Yu 				  (unsigned long long)inode->i_blocks);
2719ea6d7e72SChao Yu 			set_sbi_flag(sbi, SBI_NEED_FSCK);
2720ea6d7e72SChao Yu 			return;
2721ea6d7e72SChao Yu 		}
27220abd675eSChao Yu 		f2fs_i_blocks_write(inode, 1, false, true);
272339a53e0cSJaegeuk Kim 	}
2724ea6d7e72SChao Yu }
272539a53e0cSJaegeuk Kim 
valid_node_count(struct f2fs_sb_info * sbi)272639a53e0cSJaegeuk Kim static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
272739a53e0cSJaegeuk Kim {
27288b8343faSJaegeuk Kim 	return sbi->total_valid_node_count;
272939a53e0cSJaegeuk Kim }
273039a53e0cSJaegeuk Kim 
inc_valid_inode_count(struct f2fs_sb_info * sbi)273139a53e0cSJaegeuk Kim static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
273239a53e0cSJaegeuk Kim {
2733513c5f37SJaegeuk Kim 	percpu_counter_inc(&sbi->total_valid_inode_count);
273439a53e0cSJaegeuk Kim }
273539a53e0cSJaegeuk Kim 
dec_valid_inode_count(struct f2fs_sb_info * sbi)27360e80220aSJaegeuk Kim static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
273739a53e0cSJaegeuk Kim {
2738513c5f37SJaegeuk Kim 	percpu_counter_dec(&sbi->total_valid_inode_count);
273939a53e0cSJaegeuk Kim }
274039a53e0cSJaegeuk Kim 
valid_inode_count(struct f2fs_sb_info * sbi)2741513c5f37SJaegeuk Kim static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
274239a53e0cSJaegeuk Kim {
2743513c5f37SJaegeuk Kim 	return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
274439a53e0cSJaegeuk Kim }
274539a53e0cSJaegeuk Kim 
f2fs_grab_cache_page(struct address_space * mapping,pgoff_t index,bool for_write)2746a56c7c6fSJaegeuk Kim static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
2747a56c7c6fSJaegeuk Kim 						pgoff_t index, bool for_write)
2748a56c7c6fSJaegeuk Kim {
274982cf4f13SChao Yu 	struct page *page;
2750df808180SMatthew Wilcox (Oracle) 	unsigned int flags;
2751cac5a3d8SDongOh Shin 
27527fa750a1SArnd Bergmann 	if (IS_ENABLED(CONFIG_F2FS_FAULT_INJECTION)) {
275382cf4f13SChao Yu 		if (!for_write)
275482cf4f13SChao Yu 			page = find_get_page_flags(mapping, index,
275582cf4f13SChao Yu 							FGP_LOCK | FGP_ACCESSED);
275682cf4f13SChao Yu 		else
275782cf4f13SChao Yu 			page = find_lock_page(mapping, index);
2758c41f3cc3SJaegeuk Kim 		if (page)
2759c41f3cc3SJaegeuk Kim 			return page;
2760c41f3cc3SJaegeuk Kim 
2761c40e15a9SYangtao Li 		if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC))
2762c41f3cc3SJaegeuk Kim 			return NULL;
276355523519SChao Yu 	}
27647fa750a1SArnd Bergmann 
2765a56c7c6fSJaegeuk Kim 	if (!for_write)
2766a56c7c6fSJaegeuk Kim 		return grab_cache_page(mapping, index);
2767df808180SMatthew Wilcox (Oracle) 
2768df808180SMatthew Wilcox (Oracle) 	flags = memalloc_nofs_save();
2769b7446e7cSMatthew Wilcox (Oracle) 	page = grab_cache_page_write_begin(mapping, index);
2770df808180SMatthew Wilcox (Oracle) 	memalloc_nofs_restore(flags);
2771df808180SMatthew Wilcox (Oracle) 
2772df808180SMatthew Wilcox (Oracle) 	return page;
2773a56c7c6fSJaegeuk Kim }
2774a56c7c6fSJaegeuk Kim 
f2fs_pagecache_get_page(struct address_space * mapping,pgoff_t index,fgf_t fgp_flags,gfp_t gfp_mask)277501eccef7SChao Yu static inline struct page *f2fs_pagecache_get_page(
277601eccef7SChao Yu 				struct address_space *mapping, pgoff_t index,
2777ffc143dbSMatthew Wilcox (Oracle) 				fgf_t fgp_flags, gfp_t gfp_mask)
277801eccef7SChao Yu {
2779c40e15a9SYangtao Li 	if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_GET))
278001eccef7SChao Yu 		return NULL;
27817fa750a1SArnd Bergmann 
278201eccef7SChao Yu 	return pagecache_get_page(mapping, index, fgp_flags, gfp_mask);
278301eccef7SChao Yu }
278401eccef7SChao Yu 
f2fs_put_page(struct page * page,int unlock)278539a53e0cSJaegeuk Kim static inline void f2fs_put_page(struct page *page, int unlock)
278639a53e0cSJaegeuk Kim {
2787031fa8ccSJaegeuk Kim 	if (!page)
278839a53e0cSJaegeuk Kim 		return;
278939a53e0cSJaegeuk Kim 
279039a53e0cSJaegeuk Kim 	if (unlock) {
27919850cf4aSJaegeuk Kim 		f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
279239a53e0cSJaegeuk Kim 		unlock_page(page);
279339a53e0cSJaegeuk Kim 	}
279409cbfeafSKirill A. Shutemov 	put_page(page);
279539a53e0cSJaegeuk Kim }
279639a53e0cSJaegeuk Kim 
f2fs_put_dnode(struct dnode_of_data * dn)279739a53e0cSJaegeuk Kim static inline void f2fs_put_dnode(struct dnode_of_data *dn)
279839a53e0cSJaegeuk Kim {
279939a53e0cSJaegeuk Kim 	if (dn->node_page)
280039a53e0cSJaegeuk Kim 		f2fs_put_page(dn->node_page, 1);
280139a53e0cSJaegeuk Kim 	if (dn->inode_page && dn->node_page != dn->inode_page)
280239a53e0cSJaegeuk Kim 		f2fs_put_page(dn->inode_page, 0);
280339a53e0cSJaegeuk Kim 	dn->node_page = NULL;
280439a53e0cSJaegeuk Kim 	dn->inode_page = NULL;
280539a53e0cSJaegeuk Kim }
280639a53e0cSJaegeuk Kim 
f2fs_kmem_cache_create(const char * name,size_t size)280739a53e0cSJaegeuk Kim static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
2808e8512d2eSGu Zheng 					size_t size)
280939a53e0cSJaegeuk Kim {
2810e8512d2eSGu Zheng 	return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
281139a53e0cSJaegeuk Kim }
281239a53e0cSJaegeuk Kim 
f2fs_kmem_cache_alloc_nofail(struct kmem_cache * cachep,gfp_t flags)281332410577SChao Yu static inline void *f2fs_kmem_cache_alloc_nofail(struct kmem_cache *cachep,
28147bd59381SGu Zheng 						gfp_t flags)
28157bd59381SGu Zheng {
28167bd59381SGu Zheng 	void *entry;
28177bd59381SGu Zheng 
281880c54505SJaegeuk Kim 	entry = kmem_cache_alloc(cachep, flags);
281980c54505SJaegeuk Kim 	if (!entry)
282080c54505SJaegeuk Kim 		entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
28217bd59381SGu Zheng 	return entry;
28227bd59381SGu Zheng }
28237bd59381SGu Zheng 
f2fs_kmem_cache_alloc(struct kmem_cache * cachep,gfp_t flags,bool nofail,struct f2fs_sb_info * sbi)282432410577SChao Yu static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
282532410577SChao Yu 			gfp_t flags, bool nofail, struct f2fs_sb_info *sbi)
282632410577SChao Yu {
282732410577SChao Yu 	if (nofail)
282832410577SChao Yu 		return f2fs_kmem_cache_alloc_nofail(cachep, flags);
282932410577SChao Yu 
2830c40e15a9SYangtao Li 	if (time_to_inject(sbi, FAULT_SLAB_ALLOC))
283132410577SChao Yu 		return NULL;
283232410577SChao Yu 
283332410577SChao Yu 	return kmem_cache_alloc(cachep, flags);
283432410577SChao Yu }
283532410577SChao Yu 
is_inflight_io(struct f2fs_sb_info * sbi,int type)2836493720a4SChao Yu static inline bool is_inflight_io(struct f2fs_sb_info *sbi, int type)
28375f9abab4SJaegeuk Kim {
28385f9abab4SJaegeuk Kim 	if (get_pages(sbi, F2FS_RD_DATA) || get_pages(sbi, F2FS_RD_NODE) ||
28395f9abab4SJaegeuk Kim 		get_pages(sbi, F2FS_RD_META) || get_pages(sbi, F2FS_WB_DATA) ||
2840fef4129eSChao Yu 		get_pages(sbi, F2FS_WB_CP_DATA) ||
2841fef4129eSChao Yu 		get_pages(sbi, F2FS_DIO_READ) ||
284211ac8ef8SJaegeuk Kim 		get_pages(sbi, F2FS_DIO_WRITE))
2843493720a4SChao Yu 		return true;
284411ac8ef8SJaegeuk Kim 
284556659ce8SSahitya Tummala 	if (type != DISCARD_TIME && SM_I(sbi) && SM_I(sbi)->dcc_info &&
284611ac8ef8SJaegeuk Kim 			atomic_read(&SM_I(sbi)->dcc_info->queued_discard))
2847493720a4SChao Yu 		return true;
284811ac8ef8SJaegeuk Kim 
284911ac8ef8SJaegeuk Kim 	if (SM_I(sbi) && SM_I(sbi)->fcc_info &&
285072691af6SJaegeuk Kim 			atomic_read(&SM_I(sbi)->fcc_info->queued_flush))
2851493720a4SChao Yu 		return true;
2852493720a4SChao Yu 	return false;
2853493720a4SChao Yu }
2854493720a4SChao Yu 
is_idle(struct f2fs_sb_info * sbi,int type)2855493720a4SChao Yu static inline bool is_idle(struct f2fs_sb_info *sbi, int type)
2856493720a4SChao Yu {
2857493720a4SChao Yu 	if (sbi->gc_mode == GC_URGENT_HIGH)
2858493720a4SChao Yu 		return true;
2859493720a4SChao Yu 
2860493720a4SChao Yu 	if (is_inflight_io(sbi, type))
28615f9abab4SJaegeuk Kim 		return false;
286211ac8ef8SJaegeuk Kim 
2863d98af5f4SDaeho Jeong 	if (sbi->gc_mode == GC_URGENT_MID)
2864d98af5f4SDaeho Jeong 		return true;
2865d98af5f4SDaeho Jeong 
28660e5e8111SDaeho Jeong 	if (sbi->gc_mode == GC_URGENT_LOW &&
28670e5e8111SDaeho Jeong 			(type == DISCARD_TIME || type == GC_TIME))
28680e5e8111SDaeho Jeong 		return true;
28690e5e8111SDaeho Jeong 
28705f9abab4SJaegeuk Kim 	return f2fs_time_over(sbi, type);
28715f9abab4SJaegeuk Kim }
28725f9abab4SJaegeuk Kim 
f2fs_radix_tree_insert(struct radix_tree_root * root,unsigned long index,void * item)28739be32d72SJaegeuk Kim static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
28749be32d72SJaegeuk Kim 				unsigned long index, void *item)
28759be32d72SJaegeuk Kim {
28769be32d72SJaegeuk Kim 	while (radix_tree_insert(root, index, item))
28779be32d72SJaegeuk Kim 		cond_resched();
28789be32d72SJaegeuk Kim }
28799be32d72SJaegeuk Kim 
288039a53e0cSJaegeuk Kim #define RAW_IS_INODE(p)	((p)->footer.nid == (p)->footer.ino)
288139a53e0cSJaegeuk Kim 
IS_INODE(struct page * page)288239a53e0cSJaegeuk Kim static inline bool IS_INODE(struct page *page)
288339a53e0cSJaegeuk Kim {
288445590710SGu Zheng 	struct f2fs_node *p = F2FS_NODE(page);
2885cac5a3d8SDongOh Shin 
288639a53e0cSJaegeuk Kim 	return RAW_IS_INODE(p);
288739a53e0cSJaegeuk Kim }
288839a53e0cSJaegeuk Kim 
offset_in_addr(struct f2fs_inode * i)28897a2af766SChao Yu static inline int offset_in_addr(struct f2fs_inode *i)
28907a2af766SChao Yu {
28917a2af766SChao Yu 	return (i->i_inline & F2FS_EXTRA_ATTR) ?
28927a2af766SChao Yu 			(le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0;
28937a2af766SChao Yu }
28947a2af766SChao Yu 
blkaddr_in_node(struct f2fs_node * node)289539a53e0cSJaegeuk Kim static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
289639a53e0cSJaegeuk Kim {
289739a53e0cSJaegeuk Kim 	return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
289839a53e0cSJaegeuk Kim }
289939a53e0cSJaegeuk Kim 
29007a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode);
data_blkaddr(struct inode * inode,struct page * node_page,unsigned int offset)2901a2ced1ceSChao Yu static inline block_t data_blkaddr(struct inode *inode,
29027a2af766SChao Yu 			struct page *node_page, unsigned int offset)
290339a53e0cSJaegeuk Kim {
290439a53e0cSJaegeuk Kim 	struct f2fs_node *raw_node;
290539a53e0cSJaegeuk Kim 	__le32 *addr_array;
29067a2af766SChao Yu 	int base = 0;
29077a2af766SChao Yu 	bool is_inode = IS_INODE(node_page);
2908cac5a3d8SDongOh Shin 
290945590710SGu Zheng 	raw_node = F2FS_NODE(node_page);
29107a2af766SChao Yu 
2911979f492fSLiFan 	if (is_inode) {
2912979f492fSLiFan 		if (!inode)
29137a88ddb5SChao Yu 			/* from GC path only */
29147a2af766SChao Yu 			base = offset_in_addr(&raw_node->i);
2915979f492fSLiFan 		else if (f2fs_has_extra_attr(inode))
29167a2af766SChao Yu 			base = get_extra_isize(inode);
29177a2af766SChao Yu 	}
29187a2af766SChao Yu 
291939a53e0cSJaegeuk Kim 	addr_array = blkaddr_in_node(raw_node);
29207a2af766SChao Yu 	return le32_to_cpu(addr_array[base + offset]);
292139a53e0cSJaegeuk Kim }
292239a53e0cSJaegeuk Kim 
f2fs_data_blkaddr(struct dnode_of_data * dn)2923a2ced1ceSChao Yu static inline block_t f2fs_data_blkaddr(struct dnode_of_data *dn)
2924a2ced1ceSChao Yu {
2925a2ced1ceSChao Yu 	return data_blkaddr(dn->inode, dn->node_page, dn->ofs_in_node);
2926a2ced1ceSChao Yu }
2927a2ced1ceSChao Yu 
f2fs_test_bit(unsigned int nr,char * addr)292839a53e0cSJaegeuk Kim static inline int f2fs_test_bit(unsigned int nr, char *addr)
292939a53e0cSJaegeuk Kim {
293039a53e0cSJaegeuk Kim 	int mask;
293139a53e0cSJaegeuk Kim 
293239a53e0cSJaegeuk Kim 	addr += (nr >> 3);
2933447286ebSYangtao Li 	mask = BIT(7 - (nr & 0x07));
293439a53e0cSJaegeuk Kim 	return mask & *addr;
293539a53e0cSJaegeuk Kim }
293639a53e0cSJaegeuk Kim 
f2fs_set_bit(unsigned int nr,char * addr)2937a66cdd98SJaegeuk Kim static inline void f2fs_set_bit(unsigned int nr, char *addr)
2938a66cdd98SJaegeuk Kim {
2939a66cdd98SJaegeuk Kim 	int mask;
2940a66cdd98SJaegeuk Kim 
2941a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2942447286ebSYangtao Li 	mask = BIT(7 - (nr & 0x07));
2943a66cdd98SJaegeuk Kim 	*addr |= mask;
2944a66cdd98SJaegeuk Kim }
2945a66cdd98SJaegeuk Kim 
f2fs_clear_bit(unsigned int nr,char * addr)2946a66cdd98SJaegeuk Kim static inline void f2fs_clear_bit(unsigned int nr, char *addr)
2947a66cdd98SJaegeuk Kim {
2948a66cdd98SJaegeuk Kim 	int mask;
2949a66cdd98SJaegeuk Kim 
2950a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2951447286ebSYangtao Li 	mask = BIT(7 - (nr & 0x07));
2952a66cdd98SJaegeuk Kim 	*addr &= ~mask;
2953a66cdd98SJaegeuk Kim }
2954a66cdd98SJaegeuk Kim 
f2fs_test_and_set_bit(unsigned int nr,char * addr)295552aca074SGu Zheng static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
295639a53e0cSJaegeuk Kim {
295739a53e0cSJaegeuk Kim 	int mask;
295839a53e0cSJaegeuk Kim 	int ret;
295939a53e0cSJaegeuk Kim 
296039a53e0cSJaegeuk Kim 	addr += (nr >> 3);
2961447286ebSYangtao Li 	mask = BIT(7 - (nr & 0x07));
296239a53e0cSJaegeuk Kim 	ret = mask & *addr;
296339a53e0cSJaegeuk Kim 	*addr |= mask;
296439a53e0cSJaegeuk Kim 	return ret;
296539a53e0cSJaegeuk Kim }
296639a53e0cSJaegeuk Kim 
f2fs_test_and_clear_bit(unsigned int nr,char * addr)296752aca074SGu Zheng static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
296839a53e0cSJaegeuk Kim {
296939a53e0cSJaegeuk Kim 	int mask;
297039a53e0cSJaegeuk Kim 	int ret;
297139a53e0cSJaegeuk Kim 
297239a53e0cSJaegeuk Kim 	addr += (nr >> 3);
2973447286ebSYangtao Li 	mask = BIT(7 - (nr & 0x07));
297439a53e0cSJaegeuk Kim 	ret = mask & *addr;
297539a53e0cSJaegeuk Kim 	*addr &= ~mask;
297639a53e0cSJaegeuk Kim 	return ret;
297739a53e0cSJaegeuk Kim }
297839a53e0cSJaegeuk Kim 
f2fs_change_bit(unsigned int nr,char * addr)2979c6ac4c0eSGu Zheng static inline void f2fs_change_bit(unsigned int nr, char *addr)
2980c6ac4c0eSGu Zheng {
2981c6ac4c0eSGu Zheng 	int mask;
2982c6ac4c0eSGu Zheng 
2983c6ac4c0eSGu Zheng 	addr += (nr >> 3);
2984447286ebSYangtao Li 	mask = BIT(7 - (nr & 0x07));
2985c6ac4c0eSGu Zheng 	*addr ^= mask;
2986c6ac4c0eSGu Zheng }
2987c6ac4c0eSGu Zheng 
298859c84408SChao Yu /*
298936098557SEric Biggers  * On-disk inode flags (f2fs_inode::i_flags)
299059c84408SChao Yu  */
29914c8ff709SChao Yu #define F2FS_COMPR_FL			0x00000004 /* Compress file */
299259c84408SChao Yu #define F2FS_SYNC_FL			0x00000008 /* Synchronous updates */
299359c84408SChao Yu #define F2FS_IMMUTABLE_FL		0x00000010 /* Immutable file */
299459c84408SChao Yu #define F2FS_APPEND_FL			0x00000020 /* writes to file may only append */
299559c84408SChao Yu #define F2FS_NODUMP_FL			0x00000040 /* do not dump file */
299659c84408SChao Yu #define F2FS_NOATIME_FL			0x00000080 /* do not update atime */
29974c8ff709SChao Yu #define F2FS_NOCOMP_FL			0x00000400 /* Don't compress */
299859c84408SChao Yu #define F2FS_INDEX_FL			0x00001000 /* hash-indexed directory */
299959c84408SChao Yu #define F2FS_DIRSYNC_FL			0x00010000 /* dirsync behaviour (directories only) */
300059c84408SChao Yu #define F2FS_PROJINHERIT_FL		0x20000000 /* Create with parents projid */
30012c2eb7a3SDaniel Rosenberg #define F2FS_CASEFOLD_FL		0x40000000 /* Casefolded file */
300259c84408SChao Yu 
3003ccf3ff2bSChao Yu #define F2FS_QUOTA_DEFAULT_FL		(F2FS_NOATIME_FL | F2FS_IMMUTABLE_FL)
3004ccf3ff2bSChao Yu 
300559c84408SChao Yu /* Flags that should be inherited by new inodes from their parent. */
300636098557SEric Biggers #define F2FS_FL_INHERITED (F2FS_SYNC_FL | F2FS_NODUMP_FL | F2FS_NOATIME_FL | \
30072c2eb7a3SDaniel Rosenberg 			   F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL | \
3008787caf1bSSheng Yong 			   F2FS_CASEFOLD_FL)
300959c84408SChao Yu 
301059c84408SChao Yu /* Flags that are appropriate for regular files (all but dir-specific ones). */
30112c2eb7a3SDaniel Rosenberg #define F2FS_REG_FLMASK		(~(F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL | \
30122c2eb7a3SDaniel Rosenberg 				F2FS_CASEFOLD_FL))
301359c84408SChao Yu 
301459c84408SChao Yu /* Flags that are appropriate for non-directories/regular files. */
301559c84408SChao Yu #define F2FS_OTHER_FLMASK	(F2FS_NODUMP_FL | F2FS_NOATIME_FL)
30165c57132eSChao Yu 
f2fs_mask_flags(umode_t mode,__u32 flags)30175c57132eSChao Yu static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
30185c57132eSChao Yu {
30195c57132eSChao Yu 	if (S_ISDIR(mode))
30205c57132eSChao Yu 		return flags;
30215c57132eSChao Yu 	else if (S_ISREG(mode))
30225c57132eSChao Yu 		return flags & F2FS_REG_FLMASK;
30235c57132eSChao Yu 	else
30245c57132eSChao Yu 		return flags & F2FS_OTHER_FLMASK;
30255c57132eSChao Yu }
30265c57132eSChao Yu 
__mark_inode_dirty_flag(struct inode * inode,int flag,bool set)3027205b9822SJaegeuk Kim static inline void __mark_inode_dirty_flag(struct inode *inode,
3028205b9822SJaegeuk Kim 						int flag, bool set)
302939a53e0cSJaegeuk Kim {
3030205b9822SJaegeuk Kim 	switch (flag) {
3031205b9822SJaegeuk Kim 	case FI_INLINE_XATTR:
3032205b9822SJaegeuk Kim 	case FI_INLINE_DATA:
3033205b9822SJaegeuk Kim 	case FI_INLINE_DENTRY:
30349ac1e2d8SDaeho Jeong 	case FI_NEW_INODE:
3035205b9822SJaegeuk Kim 		if (set)
3036205b9822SJaegeuk Kim 			return;
3037df561f66SGustavo A. R. Silva 		fallthrough;
3038205b9822SJaegeuk Kim 	case FI_DATA_EXIST:
30391ad71a27SJaegeuk Kim 	case FI_PIN_FILE:
3040c6140415SJaegeuk Kim 	case FI_COMPRESS_RELEASED:
30417c45729aSJaegeuk Kim 		f2fs_mark_inode_dirty_sync(inode, true);
3042205b9822SJaegeuk Kim 	}
304339a53e0cSJaegeuk Kim }
304439a53e0cSJaegeuk Kim 
set_inode_flag(struct inode * inode,int flag)304591942321SJaegeuk Kim static inline void set_inode_flag(struct inode *inode, int flag)
304639a53e0cSJaegeuk Kim {
304758f7e00fSYufen Yu 	set_bit(flag, F2FS_I(inode)->flags);
3048205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, true);
304939a53e0cSJaegeuk Kim }
305039a53e0cSJaegeuk Kim 
is_inode_flag_set(struct inode * inode,int flag)305191942321SJaegeuk Kim static inline int is_inode_flag_set(struct inode *inode, int flag)
305239a53e0cSJaegeuk Kim {
30537653b9d8SChao Yu 	return test_bit(flag, F2FS_I(inode)->flags);
305439a53e0cSJaegeuk Kim }
305539a53e0cSJaegeuk Kim 
clear_inode_flag(struct inode * inode,int flag)305691942321SJaegeuk Kim static inline void clear_inode_flag(struct inode *inode, int flag)
305739a53e0cSJaegeuk Kim {
305858f7e00fSYufen Yu 	clear_bit(flag, F2FS_I(inode)->flags);
3059205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, false);
306039a53e0cSJaegeuk Kim }
306139a53e0cSJaegeuk Kim 
f2fs_verity_in_progress(struct inode * inode)306295ae251fSEric Biggers static inline bool f2fs_verity_in_progress(struct inode *inode)
306395ae251fSEric Biggers {
306495ae251fSEric Biggers 	return IS_ENABLED(CONFIG_FS_VERITY) &&
306595ae251fSEric Biggers 	       is_inode_flag_set(inode, FI_VERITY_IN_PROGRESS);
306695ae251fSEric Biggers }
306795ae251fSEric Biggers 
set_acl_inode(struct inode * inode,umode_t mode)306891942321SJaegeuk Kim static inline void set_acl_inode(struct inode *inode, umode_t mode)
3069444c580fSJaegeuk Kim {
307091942321SJaegeuk Kim 	F2FS_I(inode)->i_acl_mode = mode;
307191942321SJaegeuk Kim 	set_inode_flag(inode, FI_ACL_MODE);
30727c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, false);
307339a53e0cSJaegeuk Kim }
307439a53e0cSJaegeuk Kim 
f2fs_i_links_write(struct inode * inode,bool inc)3075a1961246SJaegeuk Kim static inline void f2fs_i_links_write(struct inode *inode, bool inc)
3076a1961246SJaegeuk Kim {
3077a1961246SJaegeuk Kim 	if (inc)
3078a1961246SJaegeuk Kim 		inc_nlink(inode);
3079a1961246SJaegeuk Kim 	else
3080a1961246SJaegeuk Kim 		drop_nlink(inode);
30817c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3082a1961246SJaegeuk Kim }
3083a1961246SJaegeuk Kim 
f2fs_i_blocks_write(struct inode * inode,block_t diff,bool add,bool claim)30848edd03c8SJaegeuk Kim static inline void f2fs_i_blocks_write(struct inode *inode,
30850abd675eSChao Yu 					block_t diff, bool add, bool claim)
30868edd03c8SJaegeuk Kim {
308726de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
308826de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
308926de9b11SJaegeuk Kim 
30900abd675eSChao Yu 	/* add = 1, claim = 1 should be dquot_reserve_block in pair */
30910abd675eSChao Yu 	if (add) {
30920abd675eSChao Yu 		if (claim)
30930abd675eSChao Yu 			dquot_claim_block(inode, diff);
30940abd675eSChao Yu 		else
30950abd675eSChao Yu 			dquot_alloc_block_nofail(inode, diff);
30960abd675eSChao Yu 	} else {
30970abd675eSChao Yu 		dquot_free_block(inode, diff);
30980abd675eSChao Yu 	}
30990abd675eSChao Yu 
31007c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
310126de9b11SJaegeuk Kim 	if (clean || recover)
310226de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
31038edd03c8SJaegeuk Kim }
31048edd03c8SJaegeuk Kim 
31054d8d45dfSDaeho Jeong static inline bool f2fs_is_atomic_file(struct inode *inode);
31064d8d45dfSDaeho Jeong 
f2fs_i_size_write(struct inode * inode,loff_t i_size)3107fc9581c8SJaegeuk Kim static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
3108fc9581c8SJaegeuk Kim {
310926de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
311026de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
311126de9b11SJaegeuk Kim 
3112fc9581c8SJaegeuk Kim 	if (i_size_read(inode) == i_size)
3113fc9581c8SJaegeuk Kim 		return;
3114fc9581c8SJaegeuk Kim 
3115fc9581c8SJaegeuk Kim 	i_size_write(inode, i_size);
31164d8d45dfSDaeho Jeong 
31174d8d45dfSDaeho Jeong 	if (f2fs_is_atomic_file(inode))
31184d8d45dfSDaeho Jeong 		return;
31194d8d45dfSDaeho Jeong 
31207c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
312126de9b11SJaegeuk Kim 	if (clean || recover)
312226de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
312326de9b11SJaegeuk Kim }
312426de9b11SJaegeuk Kim 
f2fs_i_depth_write(struct inode * inode,unsigned int depth)3125205b9822SJaegeuk Kim static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
3126205b9822SJaegeuk Kim {
3127205b9822SJaegeuk Kim 	F2FS_I(inode)->i_current_depth = depth;
31287c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3129205b9822SJaegeuk Kim }
3130205b9822SJaegeuk Kim 
f2fs_i_gc_failures_write(struct inode * inode,unsigned int count)31311ad71a27SJaegeuk Kim static inline void f2fs_i_gc_failures_write(struct inode *inode,
31321ad71a27SJaegeuk Kim 					unsigned int count)
31331ad71a27SJaegeuk Kim {
31342ef79ecbSChao Yu 	F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN] = count;
31351ad71a27SJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
31361ad71a27SJaegeuk Kim }
31371ad71a27SJaegeuk Kim 
f2fs_i_xnid_write(struct inode * inode,nid_t xnid)3138205b9822SJaegeuk Kim static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
3139205b9822SJaegeuk Kim {
3140205b9822SJaegeuk Kim 	F2FS_I(inode)->i_xattr_nid = xnid;
31417c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3142205b9822SJaegeuk Kim }
3143205b9822SJaegeuk Kim 
f2fs_i_pino_write(struct inode * inode,nid_t pino)3144205b9822SJaegeuk Kim static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
3145205b9822SJaegeuk Kim {
3146205b9822SJaegeuk Kim 	F2FS_I(inode)->i_pino = pino;
31477c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3148205b9822SJaegeuk Kim }
3149205b9822SJaegeuk Kim 
get_inline_info(struct inode * inode,struct f2fs_inode * ri)315091942321SJaegeuk Kim static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
3151444c580fSJaegeuk Kim {
3152205b9822SJaegeuk Kim 	struct f2fs_inode_info *fi = F2FS_I(inode);
3153205b9822SJaegeuk Kim 
3154444c580fSJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_XATTR)
31557653b9d8SChao Yu 		set_bit(FI_INLINE_XATTR, fi->flags);
31561001b347SHuajun Li 	if (ri->i_inline & F2FS_INLINE_DATA)
31577653b9d8SChao Yu 		set_bit(FI_INLINE_DATA, fi->flags);
315834d67debSChao Yu 	if (ri->i_inline & F2FS_INLINE_DENTRY)
31597653b9d8SChao Yu 		set_bit(FI_INLINE_DENTRY, fi->flags);
3160b3d208f9SJaegeuk Kim 	if (ri->i_inline & F2FS_DATA_EXIST)
31617653b9d8SChao Yu 		set_bit(FI_DATA_EXIST, fi->flags);
31627a2af766SChao Yu 	if (ri->i_inline & F2FS_EXTRA_ATTR)
31637653b9d8SChao Yu 		set_bit(FI_EXTRA_ATTR, fi->flags);
31641ad71a27SJaegeuk Kim 	if (ri->i_inline & F2FS_PIN_FILE)
31657653b9d8SChao Yu 		set_bit(FI_PIN_FILE, fi->flags);
3166c6140415SJaegeuk Kim 	if (ri->i_inline & F2FS_COMPRESS_RELEASED)
3167c6140415SJaegeuk Kim 		set_bit(FI_COMPRESS_RELEASED, fi->flags);
3168444c580fSJaegeuk Kim }
3169444c580fSJaegeuk Kim 
set_raw_inline(struct inode * inode,struct f2fs_inode * ri)317091942321SJaegeuk Kim static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
3171444c580fSJaegeuk Kim {
3172444c580fSJaegeuk Kim 	ri->i_inline = 0;
3173444c580fSJaegeuk Kim 
317491942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_XATTR))
3175444c580fSJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_XATTR;
317691942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DATA))
31771001b347SHuajun Li 		ri->i_inline |= F2FS_INLINE_DATA;
317891942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
317934d67debSChao Yu 		ri->i_inline |= F2FS_INLINE_DENTRY;
318091942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_DATA_EXIST))
3181b3d208f9SJaegeuk Kim 		ri->i_inline |= F2FS_DATA_EXIST;
31827a2af766SChao Yu 	if (is_inode_flag_set(inode, FI_EXTRA_ATTR))
31837a2af766SChao Yu 		ri->i_inline |= F2FS_EXTRA_ATTR;
31841ad71a27SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_PIN_FILE))
31851ad71a27SJaegeuk Kim 		ri->i_inline |= F2FS_PIN_FILE;
3186c6140415SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
3187c6140415SJaegeuk Kim 		ri->i_inline |= F2FS_COMPRESS_RELEASED;
31887a2af766SChao Yu }
31897a2af766SChao Yu 
f2fs_has_extra_attr(struct inode * inode)31907a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode)
31917a2af766SChao Yu {
31927a2af766SChao Yu 	return is_inode_flag_set(inode, FI_EXTRA_ATTR);
3193444c580fSJaegeuk Kim }
3194444c580fSJaegeuk Kim 
f2fs_has_inline_xattr(struct inode * inode)3195987c7c31SChao Yu static inline int f2fs_has_inline_xattr(struct inode *inode)
3196987c7c31SChao Yu {
319791942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_XATTR);
3198987c7c31SChao Yu }
3199987c7c31SChao Yu 
f2fs_compressed_file(struct inode * inode)32004c8ff709SChao Yu static inline int f2fs_compressed_file(struct inode *inode)
32014c8ff709SChao Yu {
32024c8ff709SChao Yu 	return S_ISREG(inode->i_mode) &&
32034c8ff709SChao Yu 		is_inode_flag_set(inode, FI_COMPRESSED_FILE);
32044c8ff709SChao Yu }
32054c8ff709SChao Yu 
f2fs_need_compress_data(struct inode * inode)3206602a16d5SDaeho Jeong static inline bool f2fs_need_compress_data(struct inode *inode)
3207602a16d5SDaeho Jeong {
3208602a16d5SDaeho Jeong 	int compress_mode = F2FS_OPTION(F2FS_I_SB(inode)).compress_mode;
3209602a16d5SDaeho Jeong 
3210602a16d5SDaeho Jeong 	if (!f2fs_compressed_file(inode))
3211602a16d5SDaeho Jeong 		return false;
3212602a16d5SDaeho Jeong 
3213602a16d5SDaeho Jeong 	if (compress_mode == COMPR_MODE_FS)
3214602a16d5SDaeho Jeong 		return true;
3215602a16d5SDaeho Jeong 	else if (compress_mode == COMPR_MODE_USER &&
3216602a16d5SDaeho Jeong 			is_inode_flag_set(inode, FI_ENABLE_COMPRESS))
3217602a16d5SDaeho Jeong 		return true;
3218602a16d5SDaeho Jeong 
3219602a16d5SDaeho Jeong 	return false;
3220602a16d5SDaeho Jeong }
3221602a16d5SDaeho Jeong 
addrs_per_inode(struct inode * inode)322281ca7350SChao Yu static inline unsigned int addrs_per_inode(struct inode *inode)
3223de93653fSJaegeuk Kim {
3224d02a6e61SChao Yu 	unsigned int addrs = CUR_ADDRS_PER_INODE(inode) -
3225d02a6e61SChao Yu 				get_inline_xattr_addrs(inode);
32264c8ff709SChao Yu 
32274c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
32284c8ff709SChao Yu 		return addrs;
32294c8ff709SChao Yu 	return ALIGN_DOWN(addrs, F2FS_I(inode)->i_cluster_size);
3230d02a6e61SChao Yu }
3231d02a6e61SChao Yu 
addrs_per_block(struct inode * inode)3232d02a6e61SChao Yu static inline unsigned int addrs_per_block(struct inode *inode)
3233d02a6e61SChao Yu {
32344c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
32354c8ff709SChao Yu 		return DEF_ADDRS_PER_BLOCK;
32364c8ff709SChao Yu 	return ALIGN_DOWN(DEF_ADDRS_PER_BLOCK, F2FS_I(inode)->i_cluster_size);
3237de93653fSJaegeuk Kim }
3238de93653fSJaegeuk Kim 
inline_xattr_addr(struct inode * inode,struct page * page)32396afc662eSChao Yu static inline void *inline_xattr_addr(struct inode *inode, struct page *page)
324065985d93SJaegeuk Kim {
3241695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
3242cac5a3d8SDongOh Shin 
324365985d93SJaegeuk Kim 	return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
3244b323fd28SChao Yu 					get_inline_xattr_addrs(inode)]);
324565985d93SJaegeuk Kim }
324665985d93SJaegeuk Kim 
inline_xattr_size(struct inode * inode)324765985d93SJaegeuk Kim static inline int inline_xattr_size(struct inode *inode)
324865985d93SJaegeuk Kim {
3249622927f3SChao Yu 	if (f2fs_has_inline_xattr(inode))
32506afc662eSChao Yu 		return get_inline_xattr_addrs(inode) * sizeof(__le32);
3251622927f3SChao Yu 	return 0;
325265985d93SJaegeuk Kim }
325365985d93SJaegeuk Kim 
3254d9c454abSChao Liu /*
3255d9c454abSChao Liu  * Notice: check inline_data flag without inode page lock is unsafe.
3256d9c454abSChao Liu  * It could change at any time by f2fs_convert_inline_page().
3257d9c454abSChao Liu  */
f2fs_has_inline_data(struct inode * inode)32580dbdc2aeSJaegeuk Kim static inline int f2fs_has_inline_data(struct inode *inode)
32590dbdc2aeSJaegeuk Kim {
326091942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DATA);
32610dbdc2aeSJaegeuk Kim }
32620dbdc2aeSJaegeuk Kim 
f2fs_exist_data(struct inode * inode)3263b3d208f9SJaegeuk Kim static inline int f2fs_exist_data(struct inode *inode)
3264b3d208f9SJaegeuk Kim {
326591942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DATA_EXIST);
3266b3d208f9SJaegeuk Kim }
3267b3d208f9SJaegeuk Kim 
f2fs_is_mmap_file(struct inode * inode)32684c8ff709SChao Yu static inline int f2fs_is_mmap_file(struct inode *inode)
32694c8ff709SChao Yu {
32704c8ff709SChao Yu 	return is_inode_flag_set(inode, FI_MMAP_FILE);
32714c8ff709SChao Yu }
32724c8ff709SChao Yu 
f2fs_is_pinned_file(struct inode * inode)32731ad71a27SJaegeuk Kim static inline bool f2fs_is_pinned_file(struct inode *inode)
32741ad71a27SJaegeuk Kim {
32751ad71a27SJaegeuk Kim 	return is_inode_flag_set(inode, FI_PIN_FILE);
32761ad71a27SJaegeuk Kim }
32771ad71a27SJaegeuk Kim 
f2fs_is_atomic_file(struct inode * inode)327888b88a66SJaegeuk Kim static inline bool f2fs_is_atomic_file(struct inode *inode)
327988b88a66SJaegeuk Kim {
328091942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_ATOMIC_FILE);
328188b88a66SJaegeuk Kim }
328288b88a66SJaegeuk Kim 
f2fs_is_cow_file(struct inode * inode)32834a2c5b79SYe Bin static inline bool f2fs_is_cow_file(struct inode *inode)
32844a2c5b79SYe Bin {
32854a2c5b79SYe Bin 	return is_inode_flag_set(inode, FI_COW_FILE);
32864a2c5b79SYe Bin }
32874a2c5b79SYe Bin 
3288dcd6b38bSChao Yu static inline __le32 *get_dnode_addr(struct inode *inode,
3289dcd6b38bSChao Yu 					struct page *node_page);
inline_data_addr(struct inode * inode,struct page * page)3290f2470371SChao Yu static inline void *inline_data_addr(struct inode *inode, struct page *page)
32911001b347SHuajun Li {
3292dcd6b38bSChao Yu 	__le32 *addr = get_dnode_addr(inode, page);
3293cac5a3d8SDongOh Shin 
3294dcd6b38bSChao Yu 	return (void *)(addr + DEF_INLINE_RESERVED_SIZE);
32951001b347SHuajun Li }
32961001b347SHuajun Li 
f2fs_has_inline_dentry(struct inode * inode)329734d67debSChao Yu static inline int f2fs_has_inline_dentry(struct inode *inode)
329834d67debSChao Yu {
329991942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DENTRY);
330034d67debSChao Yu }
330134d67debSChao Yu 
is_file(struct inode * inode,int type)3302b5492af7SJaegeuk Kim static inline int is_file(struct inode *inode, int type)
3303b5492af7SJaegeuk Kim {
3304b5492af7SJaegeuk Kim 	return F2FS_I(inode)->i_advise & type;
3305b5492af7SJaegeuk Kim }
3306b5492af7SJaegeuk Kim 
set_file(struct inode * inode,int type)3307b5492af7SJaegeuk Kim static inline void set_file(struct inode *inode, int type)
3308b5492af7SJaegeuk Kim {
3309766c6639SJaegeuk Kim 	if (is_file(inode, type))
3310766c6639SJaegeuk Kim 		return;
3311b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise |= type;
33127c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3313b5492af7SJaegeuk Kim }
3314b5492af7SJaegeuk Kim 
clear_file(struct inode * inode,int type)3315b5492af7SJaegeuk Kim static inline void clear_file(struct inode *inode, int type)
3316b5492af7SJaegeuk Kim {
3317766c6639SJaegeuk Kim 	if (!is_file(inode, type))
3318766c6639SJaegeuk Kim 		return;
3319b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise &= ~type;
33207c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3321b5492af7SJaegeuk Kim }
3322b5492af7SJaegeuk Kim 
f2fs_is_time_consistent(struct inode * inode)3323fe1897eaSChao Yu static inline bool f2fs_is_time_consistent(struct inode *inode)
3324fe1897eaSChao Yu {
3325c62ebd35SJeff Layton 	struct timespec64 ctime = inode_get_ctime(inode);
3326c62ebd35SJeff Layton 
3327fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time, &inode->i_atime))
3328fe1897eaSChao Yu 		return false;
3329c62ebd35SJeff Layton 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 1, &ctime))
3330fe1897eaSChao Yu 		return false;
3331fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 2, &inode->i_mtime))
3332fe1897eaSChao Yu 		return false;
3333fe1897eaSChao Yu 	return true;
3334fe1897eaSChao Yu }
3335fe1897eaSChao Yu 
f2fs_skip_inode_update(struct inode * inode,int dsync)333626787236SJaegeuk Kim static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
333726787236SJaegeuk Kim {
3338a0d00fadSChao Yu 	bool ret;
3339a0d00fadSChao Yu 
334026787236SJaegeuk Kim 	if (dsync) {
334126787236SJaegeuk Kim 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
334226787236SJaegeuk Kim 
334326787236SJaegeuk Kim 		spin_lock(&sbi->inode_lock[DIRTY_META]);
334426787236SJaegeuk Kim 		ret = list_empty(&F2FS_I(inode)->gdirty_list);
334526787236SJaegeuk Kim 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
334626787236SJaegeuk Kim 		return ret;
334726787236SJaegeuk Kim 	}
334826787236SJaegeuk Kim 	if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
334926787236SJaegeuk Kim 			file_keep_isize(inode) ||
3350235831d7SJunling Zheng 			i_size_read(inode) & ~PAGE_MASK)
335126787236SJaegeuk Kim 		return false;
3352a0d00fadSChao Yu 
3353fe1897eaSChao Yu 	if (!f2fs_is_time_consistent(inode))
3354214c2461SJaegeuk Kim 		return false;
3355214c2461SJaegeuk Kim 
3356c10c9820SChao Yu 	spin_lock(&F2FS_I(inode)->i_size_lock);
3357a0d00fadSChao Yu 	ret = F2FS_I(inode)->last_disk_size == i_size_read(inode);
3358c10c9820SChao Yu 	spin_unlock(&F2FS_I(inode)->i_size_lock);
3359a0d00fadSChao Yu 
3360a0d00fadSChao Yu 	return ret;
3361267378d4SChao Yu }
3362267378d4SChao Yu 
f2fs_readonly(struct super_block * sb)3363deeedd71SChao Yu static inline bool f2fs_readonly(struct super_block *sb)
336477888c1eSJaegeuk Kim {
3365deeedd71SChao Yu 	return sb_rdonly(sb);
336677888c1eSJaegeuk Kim }
336777888c1eSJaegeuk Kim 
f2fs_cp_error(struct f2fs_sb_info * sbi)3368744602cfSJaegeuk Kim static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
3369744602cfSJaegeuk Kim {
3370aaec2b1dSChao Yu 	return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
3371744602cfSJaegeuk Kim }
3372744602cfSJaegeuk Kim 
f2fs_kmalloc(struct f2fs_sb_info * sbi,size_t size,gfp_t flags)33731ecc0c5cSChao Yu static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
33741ecc0c5cSChao Yu 					size_t size, gfp_t flags)
33750414b004SJaegeuk Kim {
3376c40e15a9SYangtao Li 	if (time_to_inject(sbi, FAULT_KMALLOC))
33772c63feadSJaegeuk Kim 		return NULL;
33787fa750a1SArnd Bergmann 
33790b6d4ca0SEric Biggers 	return kmalloc(size, flags);
33800414b004SJaegeuk Kim }
33810414b004SJaegeuk Kim 
f2fs_getname(struct f2fs_sb_info * sbi)338255847850SWu Bo static inline void *f2fs_getname(struct f2fs_sb_info *sbi)
338355847850SWu Bo {
338455847850SWu Bo 	if (time_to_inject(sbi, FAULT_KMALLOC))
338555847850SWu Bo 		return NULL;
338655847850SWu Bo 
338755847850SWu Bo 	return __getname();
338855847850SWu Bo }
338955847850SWu Bo 
f2fs_putname(char * buf)339055847850SWu Bo static inline void f2fs_putname(char *buf)
339155847850SWu Bo {
339255847850SWu Bo 	__putname(buf);
339355847850SWu Bo }
339455847850SWu Bo 
f2fs_kzalloc(struct f2fs_sb_info * sbi,size_t size,gfp_t flags)3395acbf054dSChao Yu static inline void *f2fs_kzalloc(struct f2fs_sb_info *sbi,
3396acbf054dSChao Yu 					size_t size, gfp_t flags)
3397acbf054dSChao Yu {
3398acbf054dSChao Yu 	return f2fs_kmalloc(sbi, size, flags | __GFP_ZERO);
3399acbf054dSChao Yu }
3400acbf054dSChao Yu 
f2fs_kvmalloc(struct f2fs_sb_info * sbi,size_t size,gfp_t flags)3401628b3d14SChao Yu static inline void *f2fs_kvmalloc(struct f2fs_sb_info *sbi,
3402628b3d14SChao Yu 					size_t size, gfp_t flags)
3403628b3d14SChao Yu {
3404c40e15a9SYangtao Li 	if (time_to_inject(sbi, FAULT_KVMALLOC))
3405628b3d14SChao Yu 		return NULL;
34067fa750a1SArnd Bergmann 
3407628b3d14SChao Yu 	return kvmalloc(size, flags);
3408628b3d14SChao Yu }
3409628b3d14SChao Yu 
f2fs_kvzalloc(struct f2fs_sb_info * sbi,size_t size,gfp_t flags)3410628b3d14SChao Yu static inline void *f2fs_kvzalloc(struct f2fs_sb_info *sbi,
3411628b3d14SChao Yu 					size_t size, gfp_t flags)
3412628b3d14SChao Yu {
3413628b3d14SChao Yu 	return f2fs_kvmalloc(sbi, size, flags | __GFP_ZERO);
3414628b3d14SChao Yu }
3415628b3d14SChao Yu 
get_extra_isize(struct inode * inode)34167a2af766SChao Yu static inline int get_extra_isize(struct inode *inode)
3417f2470371SChao Yu {
34187a2af766SChao Yu 	return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
3419f2470371SChao Yu }
3420f2470371SChao Yu 
get_inline_xattr_addrs(struct inode * inode)34216afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode)
34226afc662eSChao Yu {
34236afc662eSChao Yu 	return F2FS_I(inode)->i_inline_xattr_size;
34246afc662eSChao Yu }
34256afc662eSChao Yu 
get_dnode_addr(struct inode * inode,struct page * node_page)3426dcd6b38bSChao Yu static inline __le32 *get_dnode_addr(struct inode *inode,
3427dcd6b38bSChao Yu 					struct page *node_page)
3428dcd6b38bSChao Yu {
3429dcd6b38bSChao Yu 	int base = 0;
3430dcd6b38bSChao Yu 
3431dcd6b38bSChao Yu 	if (IS_INODE(node_page) && f2fs_has_extra_attr(inode))
3432dcd6b38bSChao Yu 		base = get_extra_isize(inode);
3433dcd6b38bSChao Yu 
3434dcd6b38bSChao Yu 	return blkaddr_in_node(F2FS_NODE(node_page)) + base;
3435dcd6b38bSChao Yu }
3436dcd6b38bSChao Yu 
34374d57b86dSChao Yu #define f2fs_get_inode_mode(i) \
343891942321SJaegeuk Kim 	((is_inode_flag_set(i, FI_ACL_MODE)) ? \
3439a6dda0e6SChristoph Hellwig 	 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
3440a6dda0e6SChristoph Hellwig 
3441f240d3aaSChao Yu #define F2FS_MIN_EXTRA_ATTR_SIZE		(sizeof(__le32))
3442f240d3aaSChao Yu 
34437a2af766SChao Yu #define F2FS_TOTAL_EXTRA_ATTR_SIZE			\
34447a2af766SChao Yu 	(offsetof(struct f2fs_inode, i_extra_end) -	\
34457a2af766SChao Yu 	offsetof(struct f2fs_inode, i_extra_isize))	\
34467a2af766SChao Yu 
34475c57132eSChao Yu #define F2FS_OLD_ATTRIBUTE_SIZE	(offsetof(struct f2fs_inode, i_addr))
34485c57132eSChao Yu #define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field)		\
34492f84babfSSheng Yong 		((offsetof(typeof(*(f2fs_inode)), field) +	\
34505c57132eSChao Yu 		sizeof((f2fs_inode)->field))			\
34512f84babfSSheng Yong 		<= (F2FS_OLD_ATTRIBUTE_SIZE + (extra_isize)))	\
34525c57132eSChao Yu 
3453f0248ba6SJaegeuk Kim #define __is_large_section(sbi)		(SEGS_PER_SEC(sbi) > 1)
34542c70c5e3SChao Yu 
34556dc3a126SChao Yu #define __is_meta_io(fio) (PAGE_TYPE_OF_BIO((fio)->type) == META)
3456c9b60788SChao Yu 
3457e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
3458e1da7872SChao Yu 					block_t blkaddr, int type);
verify_blkaddr(struct f2fs_sb_info * sbi,block_t blkaddr,int type)3459e1da7872SChao Yu static inline void verify_blkaddr(struct f2fs_sb_info *sbi,
3460e1da7872SChao Yu 					block_t blkaddr, int type)
3461e1da7872SChao Yu {
346203864080SChao Yu 	if (!f2fs_is_valid_blkaddr(sbi, blkaddr, type))
3463dcbb4c10SJoe Perches 		f2fs_err(sbi, "invalid blkaddr: %u, type: %d, run fsck to fix.",
3464e1da7872SChao Yu 			 blkaddr, type);
3465e1da7872SChao Yu }
3466e1da7872SChao Yu 
__is_valid_data_blkaddr(block_t blkaddr)3467e1da7872SChao Yu static inline bool __is_valid_data_blkaddr(block_t blkaddr)
34687b525dd0SChao Yu {
34694c8ff709SChao Yu 	if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR ||
34704c8ff709SChao Yu 			blkaddr == COMPRESS_ADDR)
34717b525dd0SChao Yu 		return false;
34727b525dd0SChao Yu 	return true;
34737b525dd0SChao Yu }
34747b525dd0SChao Yu 
347539a53e0cSJaegeuk Kim /*
347639a53e0cSJaegeuk Kim  * file.c
347739a53e0cSJaegeuk Kim  */
3478cac5a3d8SDongOh Shin int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
34793265d3dbSChao Yu int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock);
3480c42d28ceSChao Yu int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock);
3481cac5a3d8SDongOh Shin int f2fs_truncate(struct inode *inode);
3482b74d24f7SChristian Brauner int f2fs_getattr(struct mnt_idmap *idmap, const struct path *path,
3483549c7297SChristian Brauner 		 struct kstat *stat, u32 request_mask, unsigned int flags);
3484c1632a0fSChristian Brauner int f2fs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
3485549c7297SChristian Brauner 		 struct iattr *attr);
34864d57b86dSChao Yu int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
34874d57b86dSChao Yu void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count);
3488f2971778SChao Yu int f2fs_do_shutdown(struct f2fs_sb_info *sbi, unsigned int flag,
3489fc18e655SChao Yu 						bool readonly, bool need_lock);
3490c4020b2dSChao Yu int f2fs_precache_extents(struct inode *inode);
34919b1bb01cSMiklos Szeredi int f2fs_fileattr_get(struct dentry *dentry, struct fileattr *fa);
34928782a9aeSChristian Brauner int f2fs_fileattr_set(struct mnt_idmap *idmap,
34939b1bb01cSMiklos Szeredi 		      struct dentry *dentry, struct fileattr *fa);
3494cac5a3d8SDongOh Shin long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
3495cac5a3d8SDongOh Shin long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
349678130819SChao Yu int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid);
34971ad71a27SJaegeuk Kim int f2fs_pin_file_control(struct inode *inode, bool inc);
349839a53e0cSJaegeuk Kim 
349939a53e0cSJaegeuk Kim /*
350039a53e0cSJaegeuk Kim  * inode.c
350139a53e0cSJaegeuk Kim  */
3502cac5a3d8SDongOh Shin void f2fs_set_inode_flags(struct inode *inode);
3503704956ecSChao Yu bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page);
3504704956ecSChao Yu void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page);
3505cac5a3d8SDongOh Shin struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
3506cac5a3d8SDongOh Shin struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
35074d57b86dSChao Yu int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
35084d57b86dSChao Yu void f2fs_update_inode(struct inode *inode, struct page *node_page);
35094d57b86dSChao Yu void f2fs_update_inode_page(struct inode *inode);
3510cac5a3d8SDongOh Shin int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
3511cac5a3d8SDongOh Shin void f2fs_evict_inode(struct inode *inode);
35124d57b86dSChao Yu void f2fs_handle_failed_inode(struct inode *inode);
351339a53e0cSJaegeuk Kim 
351439a53e0cSJaegeuk Kim /*
351539a53e0cSJaegeuk Kim  * namei.c
351639a53e0cSJaegeuk Kim  */
35174d57b86dSChao Yu int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
3518b6a06cbbSChao Yu 							bool hot, bool set);
351939a53e0cSJaegeuk Kim struct dentry *f2fs_get_parent(struct dentry *child);
3520f2d40141SChristian Brauner int f2fs_get_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
35213db1de0eSDaeho Jeong 		     struct inode **new_inode);
352239a53e0cSJaegeuk Kim 
352339a53e0cSJaegeuk Kim /*
352439a53e0cSJaegeuk Kim  * dir.c
352539a53e0cSJaegeuk Kim  */
352643c780baSEric Biggers int f2fs_init_casefolded_name(const struct inode *dir,
352743c780baSEric Biggers 			      struct f2fs_filename *fname);
352843c780baSEric Biggers int f2fs_setup_filename(struct inode *dir, const struct qstr *iname,
352943c780baSEric Biggers 			int lookup, struct f2fs_filename *fname);
353043c780baSEric Biggers int f2fs_prepare_lookup(struct inode *dir, struct dentry *dentry,
353143c780baSEric Biggers 			struct f2fs_filename *fname);
353243c780baSEric Biggers void f2fs_free_filename(struct f2fs_filename *fname);
353343c780baSEric Biggers struct f2fs_dir_entry *f2fs_find_target_dentry(const struct f2fs_dentry_ptr *d,
353443c780baSEric Biggers 			const struct f2fs_filename *fname, int *max_slots);
3535cac5a3d8SDongOh Shin int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
3536cac5a3d8SDongOh Shin 			unsigned int start_pos, struct fscrypt_str *fstr);
35374d57b86dSChao Yu void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
3538cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
35394d57b86dSChao Yu struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
354043c780baSEric Biggers 			const struct f2fs_filename *fname, struct page *dpage);
35414d57b86dSChao Yu void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
3542cac5a3d8SDongOh Shin 			unsigned int current_depth);
35434d57b86dSChao Yu int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots);
3544cac5a3d8SDongOh Shin void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
3545cac5a3d8SDongOh Shin struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
354643c780baSEric Biggers 					 const struct f2fs_filename *fname,
354743c780baSEric Biggers 					 struct page **res_page);
3548cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
3549cac5a3d8SDongOh Shin 			const struct qstr *child, struct page **res_page);
3550cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
3551cac5a3d8SDongOh Shin ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
3552cac5a3d8SDongOh Shin 			struct page **page);
3553cac5a3d8SDongOh Shin void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
3554cac5a3d8SDongOh Shin 			struct page *page, struct inode *inode);
3555b06af2afSJaegeuk Kim bool f2fs_has_enough_room(struct inode *dir, struct page *ipage,
355643c780baSEric Biggers 			  const struct f2fs_filename *fname);
3557cac5a3d8SDongOh Shin void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
355843c780baSEric Biggers 			const struct fscrypt_str *name, f2fs_hash_t name_hash,
3559cac5a3d8SDongOh Shin 			unsigned int bit_pos);
356043c780baSEric Biggers int f2fs_add_regular_entry(struct inode *dir, const struct f2fs_filename *fname,
3561cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
356243c780baSEric Biggers int f2fs_add_dentry(struct inode *dir, const struct f2fs_filename *fname,
3563cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
35644d57b86dSChao Yu int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
3565cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
3566cac5a3d8SDongOh Shin void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
3567cac5a3d8SDongOh Shin 			struct inode *dir, struct inode *inode);
35687525dec4SChao Yu int f2fs_do_tmpfile(struct inode *inode, struct inode *dir,
35697525dec4SChao Yu 					struct f2fs_filename *fname);
3570cac5a3d8SDongOh Shin bool f2fs_empty_dir(struct inode *dir);
357139a53e0cSJaegeuk Kim 
f2fs_add_link(struct dentry * dentry,struct inode * inode)3572b7f7a5e0SAl Viro static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
3573b7f7a5e0SAl Viro {
3574bfc2b7e8SEric Biggers 	if (fscrypt_is_nokey_name(dentry))
3575bfc2b7e8SEric Biggers 		return -ENOKEY;
35764d57b86dSChao Yu 	return f2fs_do_add_link(d_inode(dentry->d_parent), &dentry->d_name,
3577510022a8SJaegeuk Kim 				inode, inode->i_ino, inode->i_mode);
3578b7f7a5e0SAl Viro }
3579b7f7a5e0SAl Viro 
358039a53e0cSJaegeuk Kim /*
358139a53e0cSJaegeuk Kim  * super.c
358239a53e0cSJaegeuk Kim  */
3583cac5a3d8SDongOh Shin int f2fs_inode_dirtied(struct inode *inode, bool sync);
3584cac5a3d8SDongOh Shin void f2fs_inode_synced(struct inode *inode);
358510a26878SChao Yu int f2fs_dquot_initialize(struct inode *inode);
3586ea676733SJaegeuk Kim int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly);
3587af033b2aSChao Yu int f2fs_quota_sync(struct super_block *sb, int type);
35886d1451bfSChengguang Xu loff_t max_file_blocks(struct inode *inode);
35894b2414d0SChao Yu void f2fs_quota_off_umount(struct super_block *sb);
35901aa161e4SJaegeuk Kim void f2fs_save_errors(struct f2fs_sb_info *sbi, unsigned char flag);
3591*ecf4e678SChao Yu void f2fs_handle_critical_error(struct f2fs_sb_info *sbi, unsigned char reason);
359295fa90c9SChao Yu void f2fs_handle_error(struct f2fs_sb_info *sbi, unsigned char error);
3593901c12d1SChao Yu void f2fs_handle_error_async(struct f2fs_sb_info *sbi, unsigned char error);
3594cac5a3d8SDongOh Shin int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
3595cac5a3d8SDongOh Shin int f2fs_sync_fs(struct super_block *sb, int sync);
35964d57b86dSChao Yu int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi);
359739a53e0cSJaegeuk Kim 
359839a53e0cSJaegeuk Kim /*
359939a53e0cSJaegeuk Kim  * hash.c
360039a53e0cSJaegeuk Kim  */
360143c780baSEric Biggers void f2fs_hash_filename(const struct inode *dir, struct f2fs_filename *fname);
360239a53e0cSJaegeuk Kim 
360339a53e0cSJaegeuk Kim /*
360439a53e0cSJaegeuk Kim  * node.c
360539a53e0cSJaegeuk Kim  */
360639a53e0cSJaegeuk Kim struct node_info;
360739a53e0cSJaegeuk Kim 
36084d57b86dSChao Yu int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid);
36094d57b86dSChao Yu bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type);
361050fa53ecSChao Yu bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct page *page);
361150fa53ecSChao Yu void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi);
361250fa53ecSChao Yu void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct page *page);
361350fa53ecSChao Yu void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi);
36144d57b86dSChao Yu int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
36154d57b86dSChao Yu bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
36164d57b86dSChao Yu bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
36177735730dSChao Yu int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
3618a9419b63SJaegeuk Kim 				struct node_info *ni, bool checkpoint_context);
36194d57b86dSChao Yu pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
36204d57b86dSChao Yu int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
36214d57b86dSChao Yu int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from);
36224d57b86dSChao Yu int f2fs_truncate_xattr_node(struct inode *inode);
362350fa53ecSChao Yu int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
362450fa53ecSChao Yu 					unsigned int seq_id);
362594c821fbSChao Yu bool f2fs_nat_bitmap_enabled(struct f2fs_sb_info *sbi);
36264d57b86dSChao Yu int f2fs_remove_inode_page(struct inode *inode);
36274d57b86dSChao Yu struct page *f2fs_new_inode_page(struct inode *inode);
36284d57b86dSChao Yu struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs);
36294d57b86dSChao Yu void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
36304d57b86dSChao Yu struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
36314d57b86dSChao Yu struct page *f2fs_get_node_page_ra(struct page *parent, int start);
363248018b4cSChao Yu int f2fs_move_node_page(struct page *node_page, int gc_type);
363368e79bafSJia Yang void f2fs_flush_inline_data(struct f2fs_sb_info *sbi);
36344d57b86dSChao Yu int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
363550fa53ecSChao Yu 			struct writeback_control *wbc, bool atomic,
363650fa53ecSChao Yu 			unsigned int *seq_id);
36374d57b86dSChao Yu int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
36384d57b86dSChao Yu 			struct writeback_control *wbc,
3639b0af6d49SChao Yu 			bool do_balance, enum iostat_type io_type);
3640e2374015SChao Yu int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
36414d57b86dSChao Yu bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
36424d57b86dSChao Yu void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
36434d57b86dSChao Yu void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
36444d57b86dSChao Yu int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
36459627a7b3SChao Yu int f2fs_recover_inline_xattr(struct inode *inode, struct page *page);
36464d57b86dSChao Yu int f2fs_recover_xattr_data(struct inode *inode, struct page *page);
36474d57b86dSChao Yu int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
36487735730dSChao Yu int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
3649cac5a3d8SDongOh Shin 			unsigned int segno, struct f2fs_summary_block *sum);
365094c821fbSChao Yu void f2fs_enable_nat_bits(struct f2fs_sb_info *sbi);
3651edc55aafSJaegeuk Kim int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
36524d57b86dSChao Yu int f2fs_build_node_manager(struct f2fs_sb_info *sbi);
36534d57b86dSChao Yu void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi);
36544d57b86dSChao Yu int __init f2fs_create_node_manager_caches(void);
36554d57b86dSChao Yu void f2fs_destroy_node_manager_caches(void);
365639a53e0cSJaegeuk Kim 
365739a53e0cSJaegeuk Kim /*
365839a53e0cSJaegeuk Kim  * segment.c
365939a53e0cSJaegeuk Kim  */
36604d57b86dSChao Yu bool f2fs_need_SSR(struct f2fs_sb_info *sbi);
36613db1de0eSDaeho Jeong int f2fs_commit_atomic_write(struct inode *inode);
36623db1de0eSDaeho Jeong void f2fs_abort_atomic_write(struct inode *inode, bool clean);
3663cac5a3d8SDongOh Shin void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
36647bcd0cfaSChao Yu void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi, bool from_bg);
366539d787beSChao Yu int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
36664d57b86dSChao Yu int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
36671228b482SChao Yu int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
36684d57b86dSChao Yu void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
36694d57b86dSChao Yu void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
36704d57b86dSChao Yu bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
36714d674904SFengnan Chang int f2fs_start_discard_thread(struct f2fs_sb_info *sbi);
36724d57b86dSChao Yu void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
36734d57b86dSChao Yu void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi);
367403f2c02dSJaegeuk Kim bool f2fs_issue_discard_timeout(struct f2fs_sb_info *sbi);
36754d57b86dSChao Yu void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
36764d57b86dSChao Yu 					struct cp_control *cpc);
36774354994fSDaniel Rosenberg void f2fs_dirty_to_prefree(struct f2fs_sb_info *sbi);
36784d3aed70SDaniel Rosenberg block_t f2fs_get_unusable_blocks(struct f2fs_sb_info *sbi);
36794d3aed70SDaniel Rosenberg int f2fs_disable_cp_again(struct f2fs_sb_info *sbi, block_t unusable);
36804d57b86dSChao Yu void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi);
36814d57b86dSChao Yu int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
368261461fc9SChao Yu bool f2fs_segment_has_free_slot(struct f2fs_sb_info *sbi, int segno);
3683093749e2SChao Yu void f2fs_init_inmem_curseg(struct f2fs_sb_info *sbi);
3684093749e2SChao Yu void f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi);
3685093749e2SChao Yu void f2fs_restore_inmem_curseg(struct f2fs_sb_info *sbi);
3686093749e2SChao Yu void f2fs_get_new_segment(struct f2fs_sb_info *sbi,
3687093749e2SChao Yu 			unsigned int *newseg, bool new_sec, int dir);
36880ef81833SChao Yu void f2fs_allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type,
368904f0b2eaSQiuyang Sun 					unsigned int start, unsigned int end);
369040d76c39SDaeho Jeong int f2fs_allocate_new_section(struct f2fs_sb_info *sbi, int type, bool force);
369140d76c39SDaeho Jeong int f2fs_allocate_pinning_section(struct f2fs_sb_info *sbi);
3692901d745fSChao Yu void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi);
3693cac5a3d8SDongOh Shin int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
36944d57b86dSChao Yu bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
36954d57b86dSChao Yu 					struct cp_control *cpc);
36964d57b86dSChao Yu struct page *f2fs_get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
36974d57b86dSChao Yu void f2fs_update_meta_page(struct f2fs_sb_info *sbi, void *src,
36984d57b86dSChao Yu 					block_t blk_addr);
36994d57b86dSChao Yu void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
3700b0af6d49SChao Yu 						enum iostat_type io_type);
37014d57b86dSChao Yu void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio);
37024d57b86dSChao Yu void f2fs_outplace_write_data(struct dnode_of_data *dn,
37034d57b86dSChao Yu 			struct f2fs_io_info *fio);
37044d57b86dSChao Yu int f2fs_inplace_write_data(struct f2fs_io_info *fio);
37054d57b86dSChao Yu void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
3706cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t new_blkaddr,
3707c5d02785SChao Yu 			bool recover_curseg, bool recover_newaddr,
3708c5d02785SChao Yu 			bool from_gc);
3709cac5a3d8SDongOh Shin void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
3710cac5a3d8SDongOh Shin 			block_t old_addr, block_t new_addr,
3711cac5a3d8SDongOh Shin 			unsigned char version, bool recover_curseg,
3712cac5a3d8SDongOh Shin 			bool recover_newaddr);
37134d57b86dSChao Yu void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
3714cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t *new_blkaddr,
3715fb830fc5SChao Yu 			struct f2fs_summary *sum, int type,
3716f608c38cSChao Yu 			struct f2fs_io_info *fio);
371771f2c820SChao Yu void f2fs_update_device_state(struct f2fs_sb_info *sbi, nid_t ino,
371871f2c820SChao Yu 					block_t blkaddr, unsigned int blkcnt);
3719cac5a3d8SDongOh Shin void f2fs_wait_on_page_writeback(struct page *page,
3720bae0ee7aSChao Yu 			enum page_type type, bool ordered, bool locked);
37210ded69f6SJaegeuk Kim void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr);
37221e78e8bdSSahitya Tummala void f2fs_wait_on_block_writeback_range(struct inode *inode, block_t blkaddr,
37231e78e8bdSSahitya Tummala 								block_t len);
37244d57b86dSChao Yu void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
37254d57b86dSChao Yu void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
37264d57b86dSChao Yu int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
3727cac5a3d8SDongOh Shin 			unsigned int val, int alloc);
37284d57b86dSChao Yu void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
3729c426d991SShin'ichiro Kawasaki int f2fs_fix_curseg_write_pointer(struct f2fs_sb_info *sbi);
3730d508c94eSShin'ichiro Kawasaki int f2fs_check_write_pointer(struct f2fs_sb_info *sbi);
37314d57b86dSChao Yu int f2fs_build_segment_manager(struct f2fs_sb_info *sbi);
37324d57b86dSChao Yu void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi);
37334d57b86dSChao Yu int __init f2fs_create_segment_manager_caches(void);
37344d57b86dSChao Yu void f2fs_destroy_segment_manager_caches(void);
37354d57b86dSChao Yu int f2fs_rw_hint_to_seg_type(enum rw_hint hint);
3736de881df9SAravind Ramesh unsigned int f2fs_usable_segs_in_sec(struct f2fs_sb_info *sbi,
3737de881df9SAravind Ramesh 			unsigned int segno);
3738de881df9SAravind Ramesh unsigned int f2fs_usable_blks_in_seg(struct f2fs_sb_info *sbi,
3739de881df9SAravind Ramesh 			unsigned int segno);
374039a53e0cSJaegeuk Kim 
37416691d940SDaeho Jeong #define DEF_FRAGMENT_SIZE	4
37426691d940SDaeho Jeong #define MIN_FRAGMENT_SIZE	1
37436691d940SDaeho Jeong #define MAX_FRAGMENT_SIZE	512
37446691d940SDaeho Jeong 
f2fs_need_rand_seg(struct f2fs_sb_info * sbi)37456691d940SDaeho Jeong static inline bool f2fs_need_rand_seg(struct f2fs_sb_info *sbi)
37466691d940SDaeho Jeong {
37476691d940SDaeho Jeong 	return F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_SEG ||
37486691d940SDaeho Jeong 		F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK;
37496691d940SDaeho Jeong }
37506691d940SDaeho Jeong 
375139a53e0cSJaegeuk Kim /*
375239a53e0cSJaegeuk Kim  * checkpoint.c
375339a53e0cSJaegeuk Kim  */
3754a9cfee0eSChao Yu void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io,
3755a9cfee0eSChao Yu 							unsigned char reason);
3756c7b58576SJaegeuk Kim void f2fs_flush_ckpt_thread(struct f2fs_sb_info *sbi);
37574d57b86dSChao Yu struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
37584d57b86dSChao Yu struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
375986f33603SJaegeuk Kim struct page *f2fs_get_meta_page_retry(struct f2fs_sb_info *sbi, pgoff_t index);
37604d57b86dSChao Yu struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
3761e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
37624d57b86dSChao Yu 					block_t blkaddr, int type);
37634d57b86dSChao Yu int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
3764cac5a3d8SDongOh Shin 			int type, bool sync);
3765430f163bSChao Yu void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index,
3766430f163bSChao Yu 							unsigned int ra_blocks);
37674d57b86dSChao Yu long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
3768b0af6d49SChao Yu 			long nr_to_write, enum iostat_type io_type);
37694d57b86dSChao Yu void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
37704d57b86dSChao Yu void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
37714d57b86dSChao Yu void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all);
37724d57b86dSChao Yu bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
37734d57b86dSChao Yu void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
377439d787beSChao Yu 					unsigned int devidx, int type);
37754d57b86dSChao Yu bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
377639d787beSChao Yu 					unsigned int devidx, int type);
37774d57b86dSChao Yu int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi);
37784d57b86dSChao Yu void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi);
37794d57b86dSChao Yu void f2fs_add_orphan_inode(struct inode *inode);
37804d57b86dSChao Yu void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
37814d57b86dSChao Yu int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi);
37824d57b86dSChao Yu int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi);
37834f5e34f7SMatthew Wilcox (Oracle) void f2fs_update_dirty_folio(struct inode *inode, struct folio *folio);
37844d57b86dSChao Yu void f2fs_remove_dirty_inode(struct inode *inode);
3785d80afefbSChao Yu int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type,
3786d80afefbSChao Yu 								bool from_cp);
3787bf22c3ccSSahitya Tummala void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type);
37883a0a9cbcSChao Yu u64 f2fs_get_sectors_written(struct f2fs_sb_info *sbi);
37894d57b86dSChao Yu int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
37904d57b86dSChao Yu void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi);
37914d57b86dSChao Yu int __init f2fs_create_checkpoint_caches(void);
37924d57b86dSChao Yu void f2fs_destroy_checkpoint_caches(void);
3793261eeb9cSDaeho Jeong int f2fs_issue_checkpoint(struct f2fs_sb_info *sbi);
3794261eeb9cSDaeho Jeong int f2fs_start_ckpt_thread(struct f2fs_sb_info *sbi);
3795261eeb9cSDaeho Jeong void f2fs_stop_ckpt_thread(struct f2fs_sb_info *sbi);
3796261eeb9cSDaeho Jeong void f2fs_init_ckpt_req_control(struct f2fs_sb_info *sbi);
379739a53e0cSJaegeuk Kim 
379839a53e0cSJaegeuk Kim /*
379939a53e0cSJaegeuk Kim  * data.c
380039a53e0cSJaegeuk Kim  */
3801f543805fSChao Yu int __init f2fs_init_bioset(void);
3802f543805fSChao Yu void f2fs_destroy_bioset(void);
380382704e59SChao Yu bool f2fs_is_cp_guaranteed(struct page *page);
38040b20fcecSChao Yu int f2fs_init_bio_entry_cache(void);
38050b20fcecSChao Yu void f2fs_destroy_bio_entry_cache(void);
3806bc29835aSChristoph Hellwig void f2fs_submit_read_bio(struct f2fs_sb_info *sbi, struct bio *bio,
3807bc29835aSChristoph Hellwig 			  enum page_type type);
3808908ea654SYufen Yu int f2fs_init_write_merge_io(struct f2fs_sb_info *sbi);
3809b9109b0eSJaegeuk Kim void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
3810b9109b0eSJaegeuk Kim void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
3811bab475c5SChao Yu 				struct inode *inode, struct page *page,
3812bab475c5SChao Yu 				nid_t ino, enum page_type type);
38130b20fcecSChao Yu void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
38140b20fcecSChao Yu 					struct bio **bio, struct page *page);
3815b9109b0eSJaegeuk Kim void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
3816cac5a3d8SDongOh Shin int f2fs_submit_page_bio(struct f2fs_io_info *fio);
38178648de2cSChao Yu int f2fs_merge_page_bio(struct f2fs_io_info *fio);
3818fe16efe6SChao Yu void f2fs_submit_page_write(struct f2fs_io_info *fio);
3819cac5a3d8SDongOh Shin struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
38205189810aSChristoph Hellwig 		block_t blk_addr, sector_t *sector);
3821cac5a3d8SDongOh Shin int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
3822eb6d30bcSChao Yu void f2fs_set_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
3823cac5a3d8SDongOh Shin void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
38244d57b86dSChao Yu int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
38254d57b86dSChao Yu int f2fs_reserve_new_block(struct dnode_of_data *dn);
3826cf342d3bSChristoph Hellwig int f2fs_get_block_locked(struct dnode_of_data *dn, pgoff_t index);
3827cac5a3d8SDongOh Shin int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
38284d57b86dSChao Yu struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
382959237a21SChao Yu 			blk_opf_t op_flags, bool for_write, pgoff_t *next_pgofs);
383059237a21SChao Yu struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index,
383159237a21SChao Yu 							pgoff_t *next_pgofs);
38324d57b86dSChao Yu struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
3833cac5a3d8SDongOh Shin 			bool for_write);
38344d57b86dSChao Yu struct page *f2fs_get_new_data_page(struct inode *inode,
3835cac5a3d8SDongOh Shin 			struct page *ipage, pgoff_t index, bool new_i_size);
38364d57b86dSChao Yu int f2fs_do_write_data_page(struct f2fs_io_info *fio);
3837cd8fc522SChristoph Hellwig int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, int flag);
3838cac5a3d8SDongOh Shin int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3839cac5a3d8SDongOh Shin 			u64 start, u64 len);
38404c8ff709SChao Yu int f2fs_encrypt_one_page(struct f2fs_io_info *fio);
38414d57b86dSChao Yu bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio);
38424d57b86dSChao Yu bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio);
38434c8ff709SChao Yu int f2fs_write_single_data_page(struct page *page, int *submitted,
38444c8ff709SChao Yu 				struct bio **bio, sector_t *last_block,
38454c8ff709SChao Yu 				struct writeback_control *wbc,
38464c8ff709SChao Yu 				enum iostat_type io_type,
38473afae09fSChao Yu 				int compr_blocks, bool allow_balance);
3848a1e09b03SEric Biggers void f2fs_write_failed(struct inode *inode, loff_t to);
384991503996SMatthew Wilcox (Oracle) void f2fs_invalidate_folio(struct folio *folio, size_t offset, size_t length);
3850c26cd045SMatthew Wilcox (Oracle) bool f2fs_release_folio(struct folio *folio, gfp_t wait);
3851b91050a8SHyunchul Lee bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len);
38525ec2d99dSMatthew Wilcox void f2fs_clear_page_cache_dirty_tag(struct page *page);
38534c8ff709SChao Yu int f2fs_init_post_read_processing(void);
38544c8ff709SChao Yu void f2fs_destroy_post_read_processing(void);
38554c8ff709SChao Yu int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi);
38564c8ff709SChao Yu void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi);
38571517c1a7SEric Biggers extern const struct iomap_ops f2fs_iomap_ops;
385839a53e0cSJaegeuk Kim 
385939a53e0cSJaegeuk Kim /*
386039a53e0cSJaegeuk Kim  * gc.c
386139a53e0cSJaegeuk Kim  */
38624d57b86dSChao Yu int f2fs_start_gc_thread(struct f2fs_sb_info *sbi);
38634d57b86dSChao Yu void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi);
38644d57b86dSChao Yu block_t f2fs_start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
3865d147ea4aSJaegeuk Kim int f2fs_gc(struct f2fs_sb_info *sbi, struct f2fs_gc_control *gc_control);
38664d57b86dSChao Yu void f2fs_build_gc_manager(struct f2fs_sb_info *sbi);
386740d76c39SDaeho Jeong int f2fs_gc_range(struct f2fs_sb_info *sbi,
386840d76c39SDaeho Jeong 		unsigned int start_seg, unsigned int end_seg,
386940d76c39SDaeho Jeong 		bool dry_run, unsigned int dry_run_sections);
3870d8189834SChao Yu int f2fs_resize_fs(struct file *filp, __u64 block_count);
3871093749e2SChao Yu int __init f2fs_create_garbage_collection_cache(void);
3872093749e2SChao Yu void f2fs_destroy_garbage_collection_cache(void);
387319e0e21aSYangtao Li /* victim selection function for cleaning and SSR */
387419e0e21aSYangtao Li int f2fs_get_victim(struct f2fs_sb_info *sbi, unsigned int *result,
387519e0e21aSYangtao Li 			int gc_type, int type, char alloc_mode,
387619e0e21aSYangtao Li 			unsigned long long age);
387739a53e0cSJaegeuk Kim 
387839a53e0cSJaegeuk Kim /*
387939a53e0cSJaegeuk Kim  * recovery.c
388039a53e0cSJaegeuk Kim  */
38814d57b86dSChao Yu int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
38824d57b86dSChao Yu bool f2fs_space_for_roll_forward(struct f2fs_sb_info *sbi);
3883cad83c96SChao Yu int __init f2fs_create_recovery_cache(void);
3884cad83c96SChao Yu void f2fs_destroy_recovery_cache(void);
388539a53e0cSJaegeuk Kim 
388639a53e0cSJaegeuk Kim /*
388739a53e0cSJaegeuk Kim  * debug.c
388839a53e0cSJaegeuk Kim  */
388939a53e0cSJaegeuk Kim #ifdef CONFIG_F2FS_STAT_FS
389039a53e0cSJaegeuk Kim struct f2fs_stat_info {
389139a53e0cSJaegeuk Kim 	struct list_head stat_list;
389239a53e0cSJaegeuk Kim 	struct f2fs_sb_info *sbi;
389339a53e0cSJaegeuk Kim 	int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
389439a53e0cSJaegeuk Kim 	int main_area_segs, main_area_sections, main_area_zones;
3895e7547dacSJaegeuk Kim 	unsigned long long hit_cached[NR_EXTENT_CACHES];
3896e7547dacSJaegeuk Kim 	unsigned long long hit_rbtree[NR_EXTENT_CACHES];
3897e7547dacSJaegeuk Kim 	unsigned long long total_ext[NR_EXTENT_CACHES];
3898e7547dacSJaegeuk Kim 	unsigned long long hit_total[NR_EXTENT_CACHES];
3899e7547dacSJaegeuk Kim 	int ext_tree[NR_EXTENT_CACHES];
3900e7547dacSJaegeuk Kim 	int zombie_tree[NR_EXTENT_CACHES];
3901e7547dacSJaegeuk Kim 	int ext_node[NR_EXTENT_CACHES];
3902e7547dacSJaegeuk Kim 	/* to count memory footprint */
3903e7547dacSJaegeuk Kim 	unsigned long long ext_mem[NR_EXTENT_CACHES];
3904e7547dacSJaegeuk Kim 	/* for read extent cache */
3905e7547dacSJaegeuk Kim 	unsigned long long hit_largest;
390671644dffSJaegeuk Kim 	/* for block age extent cache */
390771644dffSJaegeuk Kim 	unsigned long long allocated_data_blocks;
39082c8a4a28SJaegeuk Kim 	int ndirty_node, ndirty_dent, ndirty_meta, ndirty_imeta;
39092c8a4a28SJaegeuk Kim 	int ndirty_data, ndirty_qdata;
39102c8a4a28SJaegeuk Kim 	unsigned int ndirty_dirs, ndirty_files, nquota_files, ndirty_all;
39115b0ef73cSJaegeuk Kim 	int nats, dirty_nats, sits, dirty_sits;
39125b0ef73cSJaegeuk Kim 	int free_nids, avail_nids, alloc_nids;
391339a53e0cSJaegeuk Kim 	int total_count, utilization;
39149bf1dcbdSChao Yu 	int nr_wb_cp_data, nr_wb_data;
39155f9abab4SJaegeuk Kim 	int nr_rd_data, nr_rd_node, nr_rd_meta;
391602b16d0aSChao Yu 	int nr_dio_read, nr_dio_write;
3917274bd9baSChao Yu 	unsigned int io_skip_bggc, other_skip_bggc;
391814d8d5f7SChao Yu 	int nr_flushing, nr_flushed, flush_list_empty;
391914d8d5f7SChao Yu 	int nr_discarding, nr_discarded;
39205f32366aSChao Yu 	int nr_discard_cmd;
3921d84d1cbdSChao Yu 	unsigned int undiscard_blks;
3922261eeb9cSDaeho Jeong 	int nr_issued_ckpt, nr_total_ckpt, nr_queued_ckpt;
3923261eeb9cSDaeho Jeong 	unsigned int cur_ckpt_time, peak_ckpt_time;
3924a00861dbSJaegeuk Kim 	int inline_xattr, inline_inode, inline_dir, append, update, orphans;
39258ec071c3SChao Yu 	int compr_inode, swapfile_inode;
3926ae999bb9SDaeho Jeong 	unsigned long long compr_blocks;
39277bc155feSJaegeuk Kim 	int aw_cnt, max_aw_cnt;
3928f83a2584SYunlei He 	unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
392939a53e0cSJaegeuk Kim 	unsigned int bimodal, avg_vblocks;
393039a53e0cSJaegeuk Kim 	int util_free, util_valid, util_invalid;
393139a53e0cSJaegeuk Kim 	int rsvd_segs, overp_segs;
39326ce19affSChao Yu 	int dirty_count, node_pages, meta_pages, compress_pages;
39336ce19affSChao Yu 	int compress_page_hit;
39349bf1dcbdSChao Yu 	int prefree_count, free_segs, free_secs;
3935eb61c2ccSChao Yu 	int cp_call_count[MAX_CALL_TYPE], cp_count;
39369bf1dcbdSChao Yu 	int gc_call_count[MAX_CALL_TYPE];
39379bf1dcbdSChao Yu 	int gc_segs[2][2];
39389bf1dcbdSChao Yu 	int gc_secs[2][2];
393939a53e0cSJaegeuk Kim 	int tot_blks, data_blks, node_blks;
3940e1235983SChangman Lee 	int bg_data_blks, bg_node_blks;
394139a53e0cSJaegeuk Kim 	int curseg[NR_CURSEG_TYPE];
394239a53e0cSJaegeuk Kim 	int cursec[NR_CURSEG_TYPE];
394339a53e0cSJaegeuk Kim 	int curzone[NR_CURSEG_TYPE];
39440759e2c1SChao Yu 	unsigned int dirty_seg[NR_CURSEG_TYPE];
39450759e2c1SChao Yu 	unsigned int full_seg[NR_CURSEG_TYPE];
39460759e2c1SChao Yu 	unsigned int valid_blks[NR_CURSEG_TYPE];
394739a53e0cSJaegeuk Kim 
3948b63e7be5SChao Yu 	unsigned int meta_count[META_MAX];
394939a53e0cSJaegeuk Kim 	unsigned int segment_count[2];
395039a53e0cSJaegeuk Kim 	unsigned int block_count[2];
3951b9a2c252SChangman Lee 	unsigned int inplace_count;
39529edcdabfSChao Yu 	unsigned long long base_mem, cache_mem, page_mem;
395339a53e0cSJaegeuk Kim };
395439a53e0cSJaegeuk Kim 
F2FS_STAT(struct f2fs_sb_info * sbi)3955963d4f7dSGu Zheng static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
3956963d4f7dSGu Zheng {
3957963d4f7dSGu Zheng 	return (struct f2fs_stat_info *)sbi->stat_info;
3958963d4f7dSGu Zheng }
3959963d4f7dSGu Zheng 
3960eb61c2ccSChao Yu #define stat_inc_cp_call_count(sbi, foreground)				\
3961eb61c2ccSChao Yu 		atomic_inc(&sbi->cp_call_count[(foreground)])
3962eb61c2ccSChao Yu #define stat_inc_cp_count(si)		(F2FS_STAT(sbi)->cp_count++)
3963274bd9baSChao Yu #define stat_io_skip_bggc_count(sbi)	((sbi)->io_skip_bggc++)
3964274bd9baSChao Yu #define stat_other_skip_bggc_count(sbi)	((sbi)->other_skip_bggc++)
396533fbd510SChao Yu #define stat_inc_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]++)
396633fbd510SChao Yu #define stat_dec_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]--)
3967e7547dacSJaegeuk Kim #define stat_inc_total_hit(sbi, type)		(atomic64_inc(&(sbi)->total_hit_ext[type]))
3968e7547dacSJaegeuk Kim #define stat_inc_rbtree_node_hit(sbi, type)	(atomic64_inc(&(sbi)->read_hit_rbtree[type]))
39695b7ee374SChao Yu #define stat_inc_largest_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_largest))
3970e7547dacSJaegeuk Kim #define stat_inc_cached_node_hit(sbi, type)	(atomic64_inc(&(sbi)->read_hit_cached[type]))
3971d5e8f6c9SChao Yu #define stat_inc_inline_xattr(inode)					\
3972d5e8f6c9SChao Yu 	do {								\
3973d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3974d5e8f6c9SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_xattr));	\
3975d5e8f6c9SChao Yu 	} while (0)
3976d5e8f6c9SChao Yu #define stat_dec_inline_xattr(inode)					\
3977d5e8f6c9SChao Yu 	do {								\
3978d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3979d5e8f6c9SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_xattr));	\
3980d5e8f6c9SChao Yu 	} while (0)
39810dbdc2aeSJaegeuk Kim #define stat_inc_inline_inode(inode)					\
39820dbdc2aeSJaegeuk Kim 	do {								\
39830dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
398403e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_inode));	\
39850dbdc2aeSJaegeuk Kim 	} while (0)
39860dbdc2aeSJaegeuk Kim #define stat_dec_inline_inode(inode)					\
39870dbdc2aeSJaegeuk Kim 	do {								\
39880dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
398903e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_inode));	\
39900dbdc2aeSJaegeuk Kim 	} while (0)
39913289c061SJaegeuk Kim #define stat_inc_inline_dir(inode)					\
39923289c061SJaegeuk Kim 	do {								\
39933289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
399403e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_dir));	\
39953289c061SJaegeuk Kim 	} while (0)
39963289c061SJaegeuk Kim #define stat_dec_inline_dir(inode)					\
39973289c061SJaegeuk Kim 	do {								\
39983289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
399903e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_dir));	\
40003289c061SJaegeuk Kim 	} while (0)
40014c8ff709SChao Yu #define stat_inc_compr_inode(inode)					\
40024c8ff709SChao Yu 	do {								\
40034c8ff709SChao Yu 		if (f2fs_compressed_file(inode))			\
40044c8ff709SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->compr_inode));	\
40054c8ff709SChao Yu 	} while (0)
40064c8ff709SChao Yu #define stat_dec_compr_inode(inode)					\
40074c8ff709SChao Yu 	do {								\
40084c8ff709SChao Yu 		if (f2fs_compressed_file(inode))			\
40094c8ff709SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->compr_inode));	\
40104c8ff709SChao Yu 	} while (0)
40114c8ff709SChao Yu #define stat_add_compr_blocks(inode, blocks)				\
4012ae999bb9SDaeho Jeong 		(atomic64_add(blocks, &F2FS_I_SB(inode)->compr_blocks))
40134c8ff709SChao Yu #define stat_sub_compr_blocks(inode, blocks)				\
4014ae999bb9SDaeho Jeong 		(atomic64_sub(blocks, &F2FS_I_SB(inode)->compr_blocks))
40158ec071c3SChao Yu #define stat_inc_swapfile_inode(inode)					\
40168ec071c3SChao Yu 		(atomic_inc(&F2FS_I_SB(inode)->swapfile_inode))
40178ec071c3SChao Yu #define stat_dec_swapfile_inode(inode)					\
40188ec071c3SChao Yu 		(atomic_dec(&F2FS_I_SB(inode)->swapfile_inode))
4019b4dac120SChao Yu #define stat_inc_atomic_inode(inode)					\
4020b4dac120SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->atomic_files))
4021b4dac120SChao Yu #define stat_dec_atomic_inode(inode)					\
4022b4dac120SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->atomic_files))
4023b63e7be5SChao Yu #define stat_inc_meta_count(sbi, blkaddr)				\
4024b63e7be5SChao Yu 	do {								\
4025b63e7be5SChao Yu 		if (blkaddr < SIT_I(sbi)->sit_base_addr)		\
4026b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_CP]);	\
4027b63e7be5SChao Yu 		else if (blkaddr < NM_I(sbi)->nat_blkaddr)		\
4028b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_SIT]);	\
4029b63e7be5SChao Yu 		else if (blkaddr < SM_I(sbi)->ssa_blkaddr)		\
4030b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_NAT]);	\
4031b63e7be5SChao Yu 		else if (blkaddr < SM_I(sbi)->main_blkaddr)		\
4032b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_SSA]);	\
4033b63e7be5SChao Yu 	} while (0)
4034dcdfff65SJaegeuk Kim #define stat_inc_seg_type(sbi, curseg)					\
4035dcdfff65SJaegeuk Kim 		((sbi)->segment_count[(curseg)->alloc_type]++)
4036dcdfff65SJaegeuk Kim #define stat_inc_block_count(sbi, curseg)				\
4037dcdfff65SJaegeuk Kim 		((sbi)->block_count[(curseg)->alloc_type]++)
4038b9a2c252SChangman Lee #define stat_inc_inplace_blocks(sbi)					\
4039b9a2c252SChangman Lee 		(atomic_inc(&(sbi)->inplace_count))
404026a28a0cSJaegeuk Kim #define stat_update_max_atomic_write(inode)				\
404126a28a0cSJaegeuk Kim 	do {								\
4042b4dac120SChao Yu 		int cur = atomic_read(&F2FS_I_SB(inode)->atomic_files);	\
404326a28a0cSJaegeuk Kim 		int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt);	\
404426a28a0cSJaegeuk Kim 		if (cur > max)						\
404526a28a0cSJaegeuk Kim 			atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur);	\
404626a28a0cSJaegeuk Kim 	} while (0)
40479bf1dcbdSChao Yu #define stat_inc_gc_call_count(sbi, foreground)				\
40489bf1dcbdSChao Yu 		(F2FS_STAT(sbi)->gc_call_count[(foreground)]++)
40499bf1dcbdSChao Yu #define stat_inc_gc_sec_count(sbi, type, gc_type)			\
40509bf1dcbdSChao Yu 		(F2FS_STAT(sbi)->gc_secs[(type)][(gc_type)]++)
40519bf1dcbdSChao Yu #define stat_inc_gc_seg_count(sbi, type, gc_type)			\
40529bf1dcbdSChao Yu 		(F2FS_STAT(sbi)->gc_segs[(type)][(gc_type)]++)
405339a53e0cSJaegeuk Kim 
405439a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks)				\
405568afcf2dSTomohiro Kusumi 	((si)->tot_blks += (blks))
405639a53e0cSJaegeuk Kim 
4057e1235983SChangman Lee #define stat_inc_data_blk_count(sbi, blks, gc_type)			\
405839a53e0cSJaegeuk Kim 	do {								\
4059963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
406039a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
406139a53e0cSJaegeuk Kim 		si->data_blks += (blks);				\
406268afcf2dSTomohiro Kusumi 		si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
406339a53e0cSJaegeuk Kim 	} while (0)
406439a53e0cSJaegeuk Kim 
4065e1235983SChangman Lee #define stat_inc_node_blk_count(sbi, blks, gc_type)			\
406639a53e0cSJaegeuk Kim 	do {								\
4067963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
406839a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
406939a53e0cSJaegeuk Kim 		si->node_blks += (blks);				\
407068afcf2dSTomohiro Kusumi 		si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
407139a53e0cSJaegeuk Kim 	} while (0)
407239a53e0cSJaegeuk Kim 
4073cac5a3d8SDongOh Shin int f2fs_build_stats(struct f2fs_sb_info *sbi);
4074cac5a3d8SDongOh Shin void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
407521acc07dSGreg Kroah-Hartman void __init f2fs_create_root_stats(void);
40764589d25dSNamjae Jeon void f2fs_destroy_root_stats(void);
4077fc7100eaSHridya Valsaraju void f2fs_update_sit_info(struct f2fs_sb_info *sbi);
407839a53e0cSJaegeuk Kim #else
4079eb61c2ccSChao Yu #define stat_inc_cp_call_count(sbi, foreground)		do { } while (0)
4080eb61c2ccSChao Yu #define stat_inc_cp_count(sbi)				do { } while (0)
4081274bd9baSChao Yu #define stat_io_skip_bggc_count(sbi)			do { } while (0)
4082274bd9baSChao Yu #define stat_other_skip_bggc_count(sbi)			do { } while (0)
4083d66450e7SArnd Bergmann #define stat_inc_dirty_inode(sbi, type)			do { } while (0)
4084d66450e7SArnd Bergmann #define stat_dec_dirty_inode(sbi, type)			do { } while (0)
4085e7547dacSJaegeuk Kim #define stat_inc_total_hit(sbi, type)			do { } while (0)
4086e7547dacSJaegeuk Kim #define stat_inc_rbtree_node_hit(sbi, type)		do { } while (0)
4087d66450e7SArnd Bergmann #define stat_inc_largest_node_hit(sbi)			do { } while (0)
4088e7547dacSJaegeuk Kim #define stat_inc_cached_node_hit(sbi, type)		do { } while (0)
4089d66450e7SArnd Bergmann #define stat_inc_inline_xattr(inode)			do { } while (0)
4090d66450e7SArnd Bergmann #define stat_dec_inline_xattr(inode)			do { } while (0)
4091d66450e7SArnd Bergmann #define stat_inc_inline_inode(inode)			do { } while (0)
4092d66450e7SArnd Bergmann #define stat_dec_inline_inode(inode)			do { } while (0)
4093d66450e7SArnd Bergmann #define stat_inc_inline_dir(inode)			do { } while (0)
4094d66450e7SArnd Bergmann #define stat_dec_inline_dir(inode)			do { } while (0)
40954c8ff709SChao Yu #define stat_inc_compr_inode(inode)			do { } while (0)
40964c8ff709SChao Yu #define stat_dec_compr_inode(inode)			do { } while (0)
40974c8ff709SChao Yu #define stat_add_compr_blocks(inode, blocks)		do { } while (0)
40984c8ff709SChao Yu #define stat_sub_compr_blocks(inode, blocks)		do { } while (0)
40998ec071c3SChao Yu #define stat_inc_swapfile_inode(inode)			do { } while (0)
41008ec071c3SChao Yu #define stat_dec_swapfile_inode(inode)			do { } while (0)
4101b4dac120SChao Yu #define stat_inc_atomic_inode(inode)			do { } while (0)
4102b4dac120SChao Yu #define stat_dec_atomic_inode(inode)			do { } while (0)
4103d66450e7SArnd Bergmann #define stat_update_max_atomic_write(inode)		do { } while (0)
4104b63e7be5SChao Yu #define stat_inc_meta_count(sbi, blkaddr)		do { } while (0)
4105d66450e7SArnd Bergmann #define stat_inc_seg_type(sbi, curseg)			do { } while (0)
4106d66450e7SArnd Bergmann #define stat_inc_block_count(sbi, curseg)		do { } while (0)
4107d66450e7SArnd Bergmann #define stat_inc_inplace_blocks(sbi)			do { } while (0)
41089bf1dcbdSChao Yu #define stat_inc_gc_call_count(sbi, foreground)		do { } while (0)
41099bf1dcbdSChao Yu #define stat_inc_gc_sec_count(sbi, type, gc_type)	do { } while (0)
41109bf1dcbdSChao Yu #define stat_inc_gc_seg_count(sbi, type, gc_type)	do { } while (0)
4111d66450e7SArnd Bergmann #define stat_inc_tot_blk_count(si, blks)		do { } while (0)
4112d66450e7SArnd Bergmann #define stat_inc_data_blk_count(sbi, blks, gc_type)	do { } while (0)
4113d66450e7SArnd Bergmann #define stat_inc_node_blk_count(sbi, blks, gc_type)	do { } while (0)
411439a53e0cSJaegeuk Kim 
f2fs_build_stats(struct f2fs_sb_info * sbi)411539a53e0cSJaegeuk Kim static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
f2fs_destroy_stats(struct f2fs_sb_info * sbi)411639a53e0cSJaegeuk Kim static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
f2fs_create_root_stats(void)411721acc07dSGreg Kroah-Hartman static inline void __init f2fs_create_root_stats(void) { }
f2fs_destroy_root_stats(void)41184589d25dSNamjae Jeon static inline void f2fs_destroy_root_stats(void) { }
f2fs_update_sit_info(struct f2fs_sb_info * sbi)411948abe91aSYueHaibing static inline void f2fs_update_sit_info(struct f2fs_sb_info *sbi) {}
412039a53e0cSJaegeuk Kim #endif
412139a53e0cSJaegeuk Kim 
412239a53e0cSJaegeuk Kim extern const struct file_operations f2fs_dir_operations;
412339a53e0cSJaegeuk Kim extern const struct file_operations f2fs_file_operations;
412439a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_file_inode_operations;
412539a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_dblock_aops;
412639a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_node_aops;
412739a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_meta_aops;
412839a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_dir_inode_operations;
412939a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_symlink_inode_operations;
4130cbaf042aSJaegeuk Kim extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
413139a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_special_inode_operations;
41324d57b86dSChao Yu extern struct kmem_cache *f2fs_inode_entry_slab;
41331001b347SHuajun Li 
4134e18c65b2SHuajun Li /*
4135e18c65b2SHuajun Li  * inline.c
4136e18c65b2SHuajun Li  */
4137cac5a3d8SDongOh Shin bool f2fs_may_inline_data(struct inode *inode);
41386290d3f5SChao Yu bool f2fs_sanity_check_inline_data(struct inode *inode, struct page *ipage);
4139cac5a3d8SDongOh Shin bool f2fs_may_inline_dentry(struct inode *inode);
41404d57b86dSChao Yu void f2fs_do_read_inline_data(struct page *page, struct page *ipage);
41414d57b86dSChao Yu void f2fs_truncate_inline_inode(struct inode *inode,
41424d57b86dSChao Yu 						struct page *ipage, u64 from);
4143cac5a3d8SDongOh Shin int f2fs_read_inline_data(struct inode *inode, struct page *page);
4144cac5a3d8SDongOh Shin int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
4145cac5a3d8SDongOh Shin int f2fs_convert_inline_inode(struct inode *inode);
4146b06af2afSJaegeuk Kim int f2fs_try_convert_inline_dir(struct inode *dir, struct dentry *dentry);
4147cac5a3d8SDongOh Shin int f2fs_write_inline_data(struct inode *inode, struct page *page);
41489627a7b3SChao Yu int f2fs_recover_inline_data(struct inode *inode, struct page *npage);
41494d57b86dSChao Yu struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
415043c780baSEric Biggers 					const struct f2fs_filename *fname,
415143c780baSEric Biggers 					struct page **res_page);
41524d57b86dSChao Yu int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent,
4153cac5a3d8SDongOh Shin 			struct page *ipage);
415443c780baSEric Biggers int f2fs_add_inline_entry(struct inode *dir, const struct f2fs_filename *fname,
4155cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
41564d57b86dSChao Yu void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry,
41574d57b86dSChao Yu 				struct page *page, struct inode *dir,
41584d57b86dSChao Yu 				struct inode *inode);
4159cac5a3d8SDongOh Shin bool f2fs_empty_inline_dir(struct inode *dir);
4160cac5a3d8SDongOh Shin int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
4161cac5a3d8SDongOh Shin 			struct fscrypt_str *fstr);
4162cac5a3d8SDongOh Shin int f2fs_inline_data_fiemap(struct inode *inode,
4163cac5a3d8SDongOh Shin 			struct fiemap_extent_info *fieinfo,
4164cac5a3d8SDongOh Shin 			__u64 start, __u64 len);
4165cde4de12SJaegeuk Kim 
4166cde4de12SJaegeuk Kim /*
41672658e50dSJaegeuk Kim  * shrinker.c
41682658e50dSJaegeuk Kim  */
4169cac5a3d8SDongOh Shin unsigned long f2fs_shrink_count(struct shrinker *shrink,
4170cac5a3d8SDongOh Shin 			struct shrink_control *sc);
4171cac5a3d8SDongOh Shin unsigned long f2fs_shrink_scan(struct shrinker *shrink,
4172cac5a3d8SDongOh Shin 			struct shrink_control *sc);
4173cac5a3d8SDongOh Shin void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
4174cac5a3d8SDongOh Shin void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
41752658e50dSJaegeuk Kim 
41762658e50dSJaegeuk Kim /*
4177a28ef1f5SChao Yu  * extent_cache.c
4178a28ef1f5SChao Yu  */
4179263df781SChao Yu bool sanity_check_extent_cache(struct inode *inode, struct page *ipage);
418072840cccSJaegeuk Kim void f2fs_init_extent_tree(struct inode *inode);
4181cac5a3d8SDongOh Shin void f2fs_drop_extent_tree(struct inode *inode);
4182e7547dacSJaegeuk Kim void f2fs_destroy_extent_node(struct inode *inode);
4183cac5a3d8SDongOh Shin void f2fs_destroy_extent_tree(struct inode *inode);
41844d57b86dSChao Yu void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi);
41854d57b86dSChao Yu int __init f2fs_create_extent_cache(void);
41864d57b86dSChao Yu void f2fs_destroy_extent_cache(void);
4187a28ef1f5SChao Yu 
4188e7547dacSJaegeuk Kim /* read extent cache ops */
418972840cccSJaegeuk Kim void f2fs_init_read_extent_tree(struct inode *inode, struct page *ipage);
4190e7547dacSJaegeuk Kim bool f2fs_lookup_read_extent_cache(struct inode *inode, pgoff_t pgofs,
4191e7547dacSJaegeuk Kim 			struct extent_info *ei);
419204a91ab0SChristoph Hellwig bool f2fs_lookup_read_extent_cache_block(struct inode *inode, pgoff_t index,
419304a91ab0SChristoph Hellwig 			block_t *blkaddr);
4194e7547dacSJaegeuk Kim void f2fs_update_read_extent_cache(struct dnode_of_data *dn);
4195e7547dacSJaegeuk Kim void f2fs_update_read_extent_cache_range(struct dnode_of_data *dn,
4196e7547dacSJaegeuk Kim 			pgoff_t fofs, block_t blkaddr, unsigned int len);
4197e7547dacSJaegeuk Kim unsigned int f2fs_shrink_read_extent_tree(struct f2fs_sb_info *sbi,
4198e7547dacSJaegeuk Kim 			int nr_shrink);
4199e7547dacSJaegeuk Kim 
420071644dffSJaegeuk Kim /* block age extent cache ops */
420171644dffSJaegeuk Kim void f2fs_init_age_extent_tree(struct inode *inode);
420271644dffSJaegeuk Kim bool f2fs_lookup_age_extent_cache(struct inode *inode, pgoff_t pgofs,
420371644dffSJaegeuk Kim 			struct extent_info *ei);
420471644dffSJaegeuk Kim void f2fs_update_age_extent_cache(struct dnode_of_data *dn);
420571644dffSJaegeuk Kim void f2fs_update_age_extent_cache_range(struct dnode_of_data *dn,
420671644dffSJaegeuk Kim 			pgoff_t fofs, unsigned int len);
420771644dffSJaegeuk Kim unsigned int f2fs_shrink_age_extent_tree(struct f2fs_sb_info *sbi,
420871644dffSJaegeuk Kim 			int nr_shrink);
420971644dffSJaegeuk Kim 
4210a28ef1f5SChao Yu /*
42118ceffcb2SChao Yu  * sysfs.c
42128ceffcb2SChao Yu  */
42130f6b56ecSDaeho Jeong #define MIN_RA_MUL	2
42140f6b56ecSDaeho Jeong #define MAX_RA_MUL	256
42150f6b56ecSDaeho Jeong 
4216dc6b2055SJaegeuk Kim int __init f2fs_init_sysfs(void);
4217dc6b2055SJaegeuk Kim void f2fs_exit_sysfs(void);
4218dc6b2055SJaegeuk Kim int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
4219dc6b2055SJaegeuk Kim void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
42208ceffcb2SChao Yu 
422195ae251fSEric Biggers /* verity.c */
422295ae251fSEric Biggers extern const struct fsverity_operations f2fs_verityops;
422395ae251fSEric Biggers 
42248ceffcb2SChao Yu /*
4225cde4de12SJaegeuk Kim  * crypto support
4226cde4de12SJaegeuk Kim  */
f2fs_encrypted_file(struct inode * inode)42271958593eSJaegeuk Kim static inline bool f2fs_encrypted_file(struct inode *inode)
42281958593eSJaegeuk Kim {
422962230e0dSChandan Rajendra 	return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
42301958593eSJaegeuk Kim }
42311958593eSJaegeuk Kim 
f2fs_set_encrypted_inode(struct inode * inode)4232cde4de12SJaegeuk Kim static inline void f2fs_set_encrypted_inode(struct inode *inode)
4233cde4de12SJaegeuk Kim {
4234643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
4235cde4de12SJaegeuk Kim 	file_set_encrypt(inode);
42369149a5ebSChao Yu 	f2fs_set_inode_flags(inode);
4237cde4de12SJaegeuk Kim #endif
4238cde4de12SJaegeuk Kim }
4239cde4de12SJaegeuk Kim 
42406dbb1796SEric Biggers /*
42416dbb1796SEric Biggers  * Returns true if the reads of the inode's data need to undergo some
42426dbb1796SEric Biggers  * postprocessing step, like decryption or authenticity verification.
42436dbb1796SEric Biggers  */
f2fs_post_read_required(struct inode * inode)42446dbb1796SEric Biggers static inline bool f2fs_post_read_required(struct inode *inode)
4245cde4de12SJaegeuk Kim {
42464c8ff709SChao Yu 	return f2fs_encrypted_file(inode) || fsverity_active(inode) ||
42474c8ff709SChao Yu 		f2fs_compressed_file(inode);
42484c8ff709SChao Yu }
42494c8ff709SChao Yu 
f2fs_used_in_atomic_write(struct inode * inode)4250b82d4300SSunmin Jeong static inline bool f2fs_used_in_atomic_write(struct inode *inode)
4251b82d4300SSunmin Jeong {
4252b82d4300SSunmin Jeong 	return f2fs_is_atomic_file(inode) || f2fs_is_cow_file(inode);
4253b82d4300SSunmin Jeong }
4254b82d4300SSunmin Jeong 
f2fs_meta_inode_gc_required(struct inode * inode)4255271fda62SSunmin Jeong static inline bool f2fs_meta_inode_gc_required(struct inode *inode)
4256271fda62SSunmin Jeong {
4257b82d4300SSunmin Jeong 	return f2fs_post_read_required(inode) || f2fs_used_in_atomic_write(inode);
4258271fda62SSunmin Jeong }
4259271fda62SSunmin Jeong 
42604c8ff709SChao Yu /*
42614c8ff709SChao Yu  * compress.c
42624c8ff709SChao Yu  */
42634c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
4264b6f186bdSYeongjin Gil enum cluster_check_type {
4265b6f186bdSYeongjin Gil 	CLUSTER_IS_COMPR,   /* check only if compressed cluster */
4266b6f186bdSYeongjin Gil 	CLUSTER_COMPR_BLKS, /* return # of compressed blocks in a cluster */
4267b6f186bdSYeongjin Gil 	CLUSTER_RAW_BLKS    /* return # of raw blocks in a cluster */
4268b6f186bdSYeongjin Gil };
42694c8ff709SChao Yu bool f2fs_is_compressed_page(struct page *page);
42704c8ff709SChao Yu struct page *f2fs_compress_control_page(struct page *page);
42714c8ff709SChao Yu int f2fs_prepare_compress_overwrite(struct inode *inode,
42724c8ff709SChao Yu 			struct page **pagep, pgoff_t index, void **fsdata);
42734c8ff709SChao Yu bool f2fs_compress_write_end(struct inode *inode, void *fsdata,
42744c8ff709SChao Yu 					pgoff_t index, unsigned copied);
42753265d3dbSChao Yu int f2fs_truncate_partial_cluster(struct inode *inode, u64 from, bool lock);
42764c8ff709SChao Yu void f2fs_compress_write_end_io(struct bio *bio, struct page *page);
42774c8ff709SChao Yu bool f2fs_is_compress_backend_ready(struct inode *inode);
4278c571fbb5SSheng Yong bool f2fs_is_compress_level_valid(int alg, int lvl);
4279a1357a91SYangtao Li int __init f2fs_init_compress_mempool(void);
42805e6bbde9SChao Yu void f2fs_destroy_compress_mempool(void);
4281bff139b4SDaeho Jeong void f2fs_decompress_cluster(struct decompress_io_ctx *dic, bool in_task);
42826ce19affSChao Yu void f2fs_end_read_compressed_page(struct page *page, bool failed,
4283bff139b4SDaeho Jeong 				block_t blkaddr, bool in_task);
42844c8ff709SChao Yu bool f2fs_cluster_is_empty(struct compress_ctx *cc);
42854c8ff709SChao Yu bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index);
428601fc4b9aSFengnan Chang bool f2fs_all_cluster_page_ready(struct compress_ctx *cc, struct page **pages,
42874f8219f8SFengnan Chang 				int index, int nr_pages, bool uptodate);
4288bbe1da7eSChao Yu bool f2fs_sanity_check_cluster(struct dnode_of_data *dn);
42894c8ff709SChao Yu void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct page *page);
42904c8ff709SChao Yu int f2fs_write_multi_pages(struct compress_ctx *cc,
42914c8ff709SChao Yu 						int *submitted,
42924c8ff709SChao Yu 						struct writeback_control *wbc,
42934c8ff709SChao Yu 						enum iostat_type io_type);
42944c8ff709SChao Yu int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index);
4295b6f186bdSYeongjin Gil bool f2fs_is_sparse_cluster(struct inode *inode, pgoff_t index);
4296e7547dacSJaegeuk Kim void f2fs_update_read_extent_tree_range_compressed(struct inode *inode,
4297e7547dacSJaegeuk Kim 				pgoff_t fofs, block_t blkaddr,
4298e7547dacSJaegeuk Kim 				unsigned int llen, unsigned int c_len);
42994c8ff709SChao Yu int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
43004c8ff709SChao Yu 				unsigned nr_pages, sector_t *last_block_in_bio,
43010683728aSChao Yu 				bool is_readahead, bool for_write);
43024c8ff709SChao Yu struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc);
4303bff139b4SDaeho Jeong void f2fs_decompress_end_io(struct decompress_io_ctx *dic, bool failed,
4304bff139b4SDaeho Jeong 				bool in_task);
4305bff139b4SDaeho Jeong void f2fs_put_page_dic(struct page *page, bool in_task);
4306fa36f5ffSChao Yu unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn,
4307fa36f5ffSChao Yu 						unsigned int ofs_in_node);
43084c8ff709SChao Yu int f2fs_init_compress_ctx(struct compress_ctx *cc);
43098bfbfb0dSChao Yu void f2fs_destroy_compress_ctx(struct compress_ctx *cc, bool reuse);
43104c8ff709SChao Yu void f2fs_init_compress_info(struct f2fs_sb_info *sbi);
43116ce19affSChao Yu int f2fs_init_compress_inode(struct f2fs_sb_info *sbi);
43126ce19affSChao Yu void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi);
431331083031SChao Yu int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi);
431431083031SChao Yu void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi);
4315c68d6c88SChao Yu int __init f2fs_init_compress_cache(void);
4316c68d6c88SChao Yu void f2fs_destroy_compress_cache(void);
43176ce19affSChao Yu struct address_space *COMPRESS_MAPPING(struct f2fs_sb_info *sbi);
43186ce19affSChao Yu void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi, block_t blkaddr);
43196ce19affSChao Yu void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
43206ce19affSChao Yu 						nid_t ino, block_t blkaddr);
43216ce19affSChao Yu bool f2fs_load_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
43226ce19affSChao Yu 								block_t blkaddr);
43236ce19affSChao Yu void f2fs_invalidate_compress_pages(struct f2fs_sb_info *sbi, nid_t ino);
43245ac443e2SDaeho Jeong #define inc_compr_inode_stat(inode)					\
43255ac443e2SDaeho Jeong 	do {								\
43265ac443e2SDaeho Jeong 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);		\
43275ac443e2SDaeho Jeong 		sbi->compr_new_inode++;					\
43285ac443e2SDaeho Jeong 	} while (0)
43295ac443e2SDaeho Jeong #define add_compr_block_stat(inode, blocks)				\
43305ac443e2SDaeho Jeong 	do {								\
43315ac443e2SDaeho Jeong 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);		\
43325ac443e2SDaeho Jeong 		int diff = F2FS_I(inode)->i_cluster_size - blocks;	\
43335ac443e2SDaeho Jeong 		sbi->compr_written_block += blocks;			\
43345ac443e2SDaeho Jeong 		sbi->compr_saved_block += diff;				\
43355ac443e2SDaeho Jeong 	} while (0)
43364c8ff709SChao Yu #else
f2fs_is_compressed_page(struct page * page)43374c8ff709SChao Yu static inline bool f2fs_is_compressed_page(struct page *page) { return false; }
f2fs_is_compress_backend_ready(struct inode * inode)43384c8ff709SChao Yu static inline bool f2fs_is_compress_backend_ready(struct inode *inode)
43394c8ff709SChao Yu {
43404c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
43414c8ff709SChao Yu 		return true;
43424c8ff709SChao Yu 	/* not support compression */
43434c8ff709SChao Yu 	return false;
43444c8ff709SChao Yu }
f2fs_is_compress_level_valid(int alg,int lvl)4345c571fbb5SSheng Yong static inline bool f2fs_is_compress_level_valid(int alg, int lvl) { return false; }
f2fs_compress_control_page(struct page * page)43464c8ff709SChao Yu static inline struct page *f2fs_compress_control_page(struct page *page)
43474c8ff709SChao Yu {
43484c8ff709SChao Yu 	WARN_ON_ONCE(1);
43494c8ff709SChao Yu 	return ERR_PTR(-EINVAL);
43504c8ff709SChao Yu }
f2fs_init_compress_mempool(void)4351a1357a91SYangtao Li static inline int __init f2fs_init_compress_mempool(void) { return 0; }
f2fs_destroy_compress_mempool(void)43525e6bbde9SChao Yu static inline void f2fs_destroy_compress_mempool(void) { }
f2fs_decompress_cluster(struct decompress_io_ctx * dic,bool in_task)4353bff139b4SDaeho Jeong static inline void f2fs_decompress_cluster(struct decompress_io_ctx *dic,
4354bff139b4SDaeho Jeong 				bool in_task) { }
f2fs_end_read_compressed_page(struct page * page,bool failed,block_t blkaddr,bool in_task)43556ce19affSChao Yu static inline void f2fs_end_read_compressed_page(struct page *page,
4356bff139b4SDaeho Jeong 				bool failed, block_t blkaddr, bool in_task)
43577f59b277SEric Biggers {
43587f59b277SEric Biggers 	WARN_ON_ONCE(1);
43597f59b277SEric Biggers }
f2fs_put_page_dic(struct page * page,bool in_task)4360bff139b4SDaeho Jeong static inline void f2fs_put_page_dic(struct page *page, bool in_task)
43617f59b277SEric Biggers {
43627f59b277SEric Biggers 	WARN_ON_ONCE(1);
43637f59b277SEric Biggers }
f2fs_cluster_blocks_are_contiguous(struct dnode_of_data * dn,unsigned int ofs_in_node)4364fa36f5ffSChao Yu static inline unsigned int f2fs_cluster_blocks_are_contiguous(
4365fa36f5ffSChao Yu 			struct dnode_of_data *dn, unsigned int ofs_in_node) { return 0; }
f2fs_sanity_check_cluster(struct dnode_of_data * dn)4366bbe1da7eSChao Yu static inline bool f2fs_sanity_check_cluster(struct dnode_of_data *dn) { return false; }
f2fs_init_compress_inode(struct f2fs_sb_info * sbi)43676ce19affSChao Yu static inline int f2fs_init_compress_inode(struct f2fs_sb_info *sbi) { return 0; }
f2fs_destroy_compress_inode(struct f2fs_sb_info * sbi)43686ce19affSChao Yu static inline void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi) { }
f2fs_init_page_array_cache(struct f2fs_sb_info * sbi)436931083031SChao Yu static inline int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi) { return 0; }
f2fs_destroy_page_array_cache(struct f2fs_sb_info * sbi)437031083031SChao Yu static inline void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi) { }
f2fs_init_compress_cache(void)4371c68d6c88SChao Yu static inline int __init f2fs_init_compress_cache(void) { return 0; }
f2fs_destroy_compress_cache(void)4372c68d6c88SChao Yu static inline void f2fs_destroy_compress_cache(void) { }
f2fs_invalidate_compress_page(struct f2fs_sb_info * sbi,block_t blkaddr)43736ce19affSChao Yu static inline void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi,
43746ce19affSChao Yu 				block_t blkaddr) { }
f2fs_cache_compressed_page(struct f2fs_sb_info * sbi,struct page * page,nid_t ino,block_t blkaddr)43756ce19affSChao Yu static inline void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi,
43766ce19affSChao Yu 				struct page *page, nid_t ino, block_t blkaddr) { }
f2fs_load_compressed_page(struct f2fs_sb_info * sbi,struct page * page,block_t blkaddr)43776ce19affSChao Yu static inline bool f2fs_load_compressed_page(struct f2fs_sb_info *sbi,
43786ce19affSChao Yu 				struct page *page, block_t blkaddr) { return false; }
f2fs_invalidate_compress_pages(struct f2fs_sb_info * sbi,nid_t ino)43796ce19affSChao Yu static inline void f2fs_invalidate_compress_pages(struct f2fs_sb_info *sbi,
43806ce19affSChao Yu 							nid_t ino) { }
43815ac443e2SDaeho Jeong #define inc_compr_inode_stat(inode)		do { } while (0)
f2fs_is_compressed_cluster(struct inode * inode,pgoff_t index)4382b6f186bdSYeongjin Gil static inline int f2fs_is_compressed_cluster(
4383b6f186bdSYeongjin Gil 				struct inode *inode,
4384b6f186bdSYeongjin Gil 				pgoff_t index) { return 0; }
f2fs_is_sparse_cluster(struct inode * inode,pgoff_t index)4385b6f186bdSYeongjin Gil static inline bool f2fs_is_sparse_cluster(
4386b6f186bdSYeongjin Gil 				struct inode *inode,
4387b6f186bdSYeongjin Gil 				pgoff_t index) { return true; }
f2fs_update_read_extent_tree_range_compressed(struct inode * inode,pgoff_t fofs,block_t blkaddr,unsigned int llen,unsigned int c_len)4388e7547dacSJaegeuk Kim static inline void f2fs_update_read_extent_tree_range_compressed(
4389e7547dacSJaegeuk Kim 				struct inode *inode,
4390e7547dacSJaegeuk Kim 				pgoff_t fofs, block_t blkaddr,
4391e7547dacSJaegeuk Kim 				unsigned int llen, unsigned int c_len) { }
43924c8ff709SChao Yu #endif
43934c8ff709SChao Yu 
set_compress_context(struct inode * inode)4394912f0d65SJaegeuk Kim static inline int set_compress_context(struct inode *inode)
43954c8ff709SChao Yu {
4396912f0d65SJaegeuk Kim #ifdef CONFIG_F2FS_FS_COMPRESSION
43974c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
43984c8ff709SChao Yu 
43994c8ff709SChao Yu 	F2FS_I(inode)->i_compress_algorithm =
44004c8ff709SChao Yu 			F2FS_OPTION(sbi).compress_algorithm;
44014c8ff709SChao Yu 	F2FS_I(inode)->i_log_cluster_size =
44024c8ff709SChao Yu 			F2FS_OPTION(sbi).compress_log_size;
4403b28f047bSChao Yu 	F2FS_I(inode)->i_compress_flag =
4404b28f047bSChao Yu 			F2FS_OPTION(sbi).compress_chksum ?
4405447286ebSYangtao Li 				BIT(COMPRESS_CHKSUM) : 0;
44064c8ff709SChao Yu 	F2FS_I(inode)->i_cluster_size =
4407447286ebSYangtao Li 			BIT(F2FS_I(inode)->i_log_cluster_size);
440801f6afd0SChao Yu 	if ((F2FS_I(inode)->i_compress_algorithm == COMPRESS_LZ4 ||
440901f6afd0SChao Yu 		F2FS_I(inode)->i_compress_algorithm == COMPRESS_ZSTD) &&
44103fde13f8SChao Yu 			F2FS_OPTION(sbi).compress_level)
4411b90e5086SChao Yu 		F2FS_I(inode)->i_compress_level =
4412b90e5086SChao Yu 				F2FS_OPTION(sbi).compress_level;
44134c8ff709SChao Yu 	F2FS_I(inode)->i_flags |= F2FS_COMPR_FL;
44144c8ff709SChao Yu 	set_inode_flag(inode, FI_COMPRESSED_FILE);
44154c8ff709SChao Yu 	stat_inc_compr_inode(inode);
44165ac443e2SDaeho Jeong 	inc_compr_inode_stat(inode);
4417530e0704SChao Yu 	f2fs_mark_inode_dirty_sync(inode, true);
4418912f0d65SJaegeuk Kim 	return 0;
4419912f0d65SJaegeuk Kim #else
4420912f0d65SJaegeuk Kim 	return -EOPNOTSUPP;
4421912f0d65SJaegeuk Kim #endif
44224c8ff709SChao Yu }
44234c8ff709SChao Yu 
f2fs_disable_compressed_file(struct inode * inode)442478134d03SDaeho Jeong static inline bool f2fs_disable_compressed_file(struct inode *inode)
44254c8ff709SChao Yu {
44264c8ff709SChao Yu 	struct f2fs_inode_info *fi = F2FS_I(inode);
44274c8ff709SChao Yu 
44285e59e199SChao Yu 	f2fs_down_write(&F2FS_I(inode)->i_sem);
44295e59e199SChao Yu 
44305e59e199SChao Yu 	if (!f2fs_compressed_file(inode)) {
44315e59e199SChao Yu 		f2fs_up_write(&F2FS_I(inode)->i_sem);
443278134d03SDaeho Jeong 		return true;
44335e59e199SChao Yu 	}
44345e59e199SChao Yu 	if (f2fs_is_mmap_file(inode) ||
44355e59e199SChao Yu 		(S_ISREG(inode->i_mode) && F2FS_HAS_BLOCKS(inode))) {
44365e59e199SChao Yu 		f2fs_up_write(&F2FS_I(inode)->i_sem);
443778134d03SDaeho Jeong 		return false;
44385e59e199SChao Yu 	}
44394c8ff709SChao Yu 
44404c8ff709SChao Yu 	fi->i_flags &= ~F2FS_COMPR_FL;
44414c8ff709SChao Yu 	stat_dec_compr_inode(inode);
444296f5b4faSChao Yu 	clear_inode_flag(inode, FI_COMPRESSED_FILE);
4443530e0704SChao Yu 	f2fs_mark_inode_dirty_sync(inode, true);
44445e59e199SChao Yu 
44455e59e199SChao Yu 	f2fs_up_write(&F2FS_I(inode)->i_sem);
444678134d03SDaeho Jeong 	return true;
4447cde4de12SJaegeuk Kim }
4448cde4de12SJaegeuk Kim 
4449ccd31cb2SSheng Yong #define F2FS_FEATURE_FUNCS(name, flagname) \
4450e4807519SYangtao Li static inline bool f2fs_sb_has_##name(struct f2fs_sb_info *sbi) \
4451ccd31cb2SSheng Yong { \
44527beb01f7SChao Yu 	return F2FS_HAS_FEATURE(sbi, F2FS_FEATURE_##flagname); \
4453cde4de12SJaegeuk Kim }
4454f424f664SJaegeuk Kim 
4455ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(encrypt, ENCRYPT);
4456ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(blkzoned, BLKZONED);
4457ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(extra_attr, EXTRA_ATTR);
4458ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(project_quota, PRJQUOTA);
4459ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_chksum, INODE_CHKSUM);
4460ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
4461ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
4462ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
4463b7c409deSSheng Yong F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
446495ae251fSEric Biggers F2FS_FEATURE_FUNCS(verity, VERITY);
4465d440c52dSJunling Zheng F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
44665aba5430SDaniel Rosenberg F2FS_FEATURE_FUNCS(casefold, CASEFOLD);
44674c8ff709SChao Yu F2FS_FEATURE_FUNCS(compression, COMPRESSION);
4468a7d9fe3cSJaegeuk Kim F2FS_FEATURE_FUNCS(readonly, RO);
44691c1d35dfSChao Yu 
4470178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
f2fs_blkz_is_seq(struct f2fs_sb_info * sbi,int devi,block_t blkaddr)447195175dafSDamien Le Moal static inline bool f2fs_blkz_is_seq(struct f2fs_sb_info *sbi, int devi,
447295175dafSDamien Le Moal 				    block_t blkaddr)
4473178053e2SDamien Le Moal {
44742e2c6e9bSJaegeuk Kim 	unsigned int zno = blkaddr / sbi->blocks_per_blkz;
4475178053e2SDamien Le Moal 
447695175dafSDamien Le Moal 	return test_bit(zno, FDEV(devi).blkz_seq);
4477178053e2SDamien Le Moal }
4478178053e2SDamien Le Moal #endif
4479178053e2SDamien Le Moal 
f2fs_bdev_index(struct f2fs_sb_info * sbi,struct block_device * bdev)44803cb88bc1SShin'ichiro Kawasaki static inline int f2fs_bdev_index(struct f2fs_sb_info *sbi,
44813cb88bc1SShin'ichiro Kawasaki 				  struct block_device *bdev)
44823cb88bc1SShin'ichiro Kawasaki {
44833cb88bc1SShin'ichiro Kawasaki 	int i;
44843cb88bc1SShin'ichiro Kawasaki 
44853cb88bc1SShin'ichiro Kawasaki 	if (!f2fs_is_multi_device(sbi))
44863cb88bc1SShin'ichiro Kawasaki 		return 0;
44873cb88bc1SShin'ichiro Kawasaki 
44883cb88bc1SShin'ichiro Kawasaki 	for (i = 0; i < sbi->s_ndevs; i++)
44893cb88bc1SShin'ichiro Kawasaki 		if (FDEV(i).bdev == bdev)
44903cb88bc1SShin'ichiro Kawasaki 			return i;
44913cb88bc1SShin'ichiro Kawasaki 
44923cb88bc1SShin'ichiro Kawasaki 	WARN_ON(1);
44933cb88bc1SShin'ichiro Kawasaki 	return -1;
44943cb88bc1SShin'ichiro Kawasaki }
44953cb88bc1SShin'ichiro Kawasaki 
f2fs_hw_should_discard(struct f2fs_sb_info * sbi)44967d20c8abSChao Yu static inline bool f2fs_hw_should_discard(struct f2fs_sb_info *sbi)
449796ba2decSDamien Le Moal {
44987beb01f7SChao Yu 	return f2fs_sb_has_blkzoned(sbi);
44997d20c8abSChao Yu }
450096ba2decSDamien Le Moal 
f2fs_bdev_support_discard(struct block_device * bdev)45017f3d7719SDamien Le Moal static inline bool f2fs_bdev_support_discard(struct block_device *bdev)
45027f3d7719SDamien Le Moal {
450370200574SChristoph Hellwig 	return bdev_max_discard_sectors(bdev) || bdev_is_zoned(bdev);
45047f3d7719SDamien Le Moal }
45057f3d7719SDamien Le Moal 
f2fs_hw_support_discard(struct f2fs_sb_info * sbi)45067d20c8abSChao Yu static inline bool f2fs_hw_support_discard(struct f2fs_sb_info *sbi)
45077d20c8abSChao Yu {
45087f3d7719SDamien Le Moal 	int i;
45097f3d7719SDamien Le Moal 
45107f3d7719SDamien Le Moal 	if (!f2fs_is_multi_device(sbi))
45117f3d7719SDamien Le Moal 		return f2fs_bdev_support_discard(sbi->sb->s_bdev);
45127f3d7719SDamien Le Moal 
45137f3d7719SDamien Le Moal 	for (i = 0; i < sbi->s_ndevs; i++)
45147f3d7719SDamien Le Moal 		if (f2fs_bdev_support_discard(FDEV(i).bdev))
45157f3d7719SDamien Le Moal 			return true;
45167f3d7719SDamien Le Moal 	return false;
45177d20c8abSChao Yu }
45187d20c8abSChao Yu 
f2fs_realtime_discard_enable(struct f2fs_sb_info * sbi)45197d20c8abSChao Yu static inline bool f2fs_realtime_discard_enable(struct f2fs_sb_info *sbi)
45207d20c8abSChao Yu {
45217d20c8abSChao Yu 	return (test_opt(sbi, DISCARD) && f2fs_hw_support_discard(sbi)) ||
45227d20c8abSChao Yu 					f2fs_hw_should_discard(sbi);
452352763a4bSJaegeuk Kim }
452452763a4bSJaegeuk Kim 
f2fs_hw_is_readonly(struct f2fs_sb_info * sbi)4525f824deb5SChao Yu static inline bool f2fs_hw_is_readonly(struct f2fs_sb_info *sbi)
4526f824deb5SChao Yu {
4527f824deb5SChao Yu 	int i;
4528f824deb5SChao Yu 
4529f824deb5SChao Yu 	if (!f2fs_is_multi_device(sbi))
4530f824deb5SChao Yu 		return bdev_read_only(sbi->sb->s_bdev);
4531f824deb5SChao Yu 
4532f824deb5SChao Yu 	for (i = 0; i < sbi->s_ndevs; i++)
4533f824deb5SChao Yu 		if (bdev_read_only(FDEV(i).bdev))
4534f824deb5SChao Yu 			return true;
4535f824deb5SChao Yu 	return false;
4536f824deb5SChao Yu }
4537f824deb5SChao Yu 
f2fs_dev_is_readonly(struct f2fs_sb_info * sbi)4538d78dfefcSChao Yu static inline bool f2fs_dev_is_readonly(struct f2fs_sb_info *sbi)
4539d78dfefcSChao Yu {
4540d78dfefcSChao Yu 	return f2fs_sb_has_readonly(sbi) || f2fs_hw_is_readonly(sbi);
4541d78dfefcSChao Yu }
4542d78dfefcSChao Yu 
f2fs_lfs_mode(struct f2fs_sb_info * sbi)4543b0332a0fSChao Yu static inline bool f2fs_lfs_mode(struct f2fs_sb_info *sbi)
454452763a4bSJaegeuk Kim {
4545b0332a0fSChao Yu 	return F2FS_OPTION(sbi).fs_mode == FS_MODE_LFS;
454652763a4bSJaegeuk Kim }
454752763a4bSJaegeuk Kim 
f2fs_valid_pinned_area(struct f2fs_sb_info * sbi,block_t blkaddr)454840d76c39SDaeho Jeong static inline bool f2fs_valid_pinned_area(struct f2fs_sb_info *sbi,
454940d76c39SDaeho Jeong 					  block_t blkaddr)
455040d76c39SDaeho Jeong {
455140d76c39SDaeho Jeong 	if (f2fs_sb_has_blkzoned(sbi)) {
455240d76c39SDaeho Jeong 		int devi = f2fs_target_device_index(sbi, blkaddr);
455340d76c39SDaeho Jeong 
455440d76c39SDaeho Jeong 		return !bdev_is_zoned(FDEV(devi).bdev);
455540d76c39SDaeho Jeong 	}
455640d76c39SDaeho Jeong 	return true;
455740d76c39SDaeho Jeong }
455840d76c39SDaeho Jeong 
f2fs_low_mem_mode(struct f2fs_sb_info * sbi)45597a8fc586SDaeho Jeong static inline bool f2fs_low_mem_mode(struct f2fs_sb_info *sbi)
45607a8fc586SDaeho Jeong {
45617a8fc586SDaeho Jeong 	return F2FS_OPTION(sbi).memory_mode == MEMORY_MODE_LOW;
45627a8fc586SDaeho Jeong }
45637a8fc586SDaeho Jeong 
f2fs_may_compress(struct inode * inode)45644c8ff709SChao Yu static inline bool f2fs_may_compress(struct inode *inode)
45654c8ff709SChao Yu {
45664c8ff709SChao Yu 	if (IS_SWAPFILE(inode) || f2fs_is_pinned_file(inode) ||
4567b5ab3276SChao Yu 		f2fs_is_atomic_file(inode) || f2fs_has_inline_data(inode) ||
4568b5ab3276SChao Yu 		f2fs_is_mmap_file(inode))
45694c8ff709SChao Yu 		return false;
45704c8ff709SChao Yu 	return S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode);
45714c8ff709SChao Yu }
45724c8ff709SChao Yu 
f2fs_i_compr_blocks_update(struct inode * inode,u64 blocks,bool add)45734c8ff709SChao Yu static inline void f2fs_i_compr_blocks_update(struct inode *inode,
45744c8ff709SChao Yu 						u64 blocks, bool add)
45754c8ff709SChao Yu {
4576c2759ebaSDaeho Jeong 	struct f2fs_inode_info *fi = F2FS_I(inode);
4577054cb289SYufen Yu 	int diff = fi->i_cluster_size - blocks;
45784c8ff709SChao Yu 
4579ef8d563fSChao Yu 	/* don't update i_compr_blocks if saved blocks were released */
4580c2759ebaSDaeho Jeong 	if (!add && !atomic_read(&fi->i_compr_blocks))
4581ef8d563fSChao Yu 		return;
4582ef8d563fSChao Yu 
45834c8ff709SChao Yu 	if (add) {
4584c2759ebaSDaeho Jeong 		atomic_add(diff, &fi->i_compr_blocks);
45854c8ff709SChao Yu 		stat_add_compr_blocks(inode, diff);
45864c8ff709SChao Yu 	} else {
4587c2759ebaSDaeho Jeong 		atomic_sub(diff, &fi->i_compr_blocks);
45884c8ff709SChao Yu 		stat_sub_compr_blocks(inode, diff);
45894c8ff709SChao Yu 	}
45904c8ff709SChao Yu 	f2fs_mark_inode_dirty_sync(inode, true);
45914c8ff709SChao Yu }
45924c8ff709SChao Yu 
f2fs_allow_multi_device_dio(struct f2fs_sb_info * sbi,int flag)459371f2c820SChao Yu static inline bool f2fs_allow_multi_device_dio(struct f2fs_sb_info *sbi,
459471f2c820SChao Yu 								int flag)
459571f2c820SChao Yu {
459671f2c820SChao Yu 	if (!f2fs_is_multi_device(sbi))
459771f2c820SChao Yu 		return false;
459871f2c820SChao Yu 	if (flag != F2FS_GET_BLOCK_DIO)
459971f2c820SChao Yu 		return false;
460071f2c820SChao Yu 	return sbi->aligned_blksize;
460171f2c820SChao Yu }
460271f2c820SChao Yu 
f2fs_need_verity(const struct inode * inode,pgoff_t idx)46037f59b277SEric Biggers static inline bool f2fs_need_verity(const struct inode *inode, pgoff_t idx)
46047f59b277SEric Biggers {
46057f59b277SEric Biggers 	return fsverity_active(inode) &&
46067f59b277SEric Biggers 	       idx < DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
46077f59b277SEric Biggers }
46087f59b277SEric Biggers 
460983a3bfdbSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
461044958ca9SChao Yu extern int f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned long rate,
461144958ca9SChao Yu 							unsigned long type);
461283a3bfdbSJaegeuk Kim #else
f2fs_build_fault_attr(struct f2fs_sb_info * sbi,unsigned long rate,unsigned long type)461372aa369eSNathan Chancellor static inline int f2fs_build_fault_attr(struct f2fs_sb_info *sbi,
461472aa369eSNathan Chancellor 					unsigned long rate, unsigned long type)
461544958ca9SChao Yu {
461644958ca9SChao Yu 	return 0;
461744958ca9SChao Yu }
461883a3bfdbSJaegeuk Kim #endif
461983a3bfdbSJaegeuk Kim 
is_journalled_quota(struct f2fs_sb_info * sbi)4620af033b2aSChao Yu static inline bool is_journalled_quota(struct f2fs_sb_info *sbi)
4621af033b2aSChao Yu {
4622af033b2aSChao Yu #ifdef CONFIG_QUOTA
46237beb01f7SChao Yu 	if (f2fs_sb_has_quota_ino(sbi))
4624af033b2aSChao Yu 		return true;
4625af033b2aSChao Yu 	if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
4626af033b2aSChao Yu 		F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
4627af033b2aSChao Yu 		F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
4628af033b2aSChao Yu 		return true;
4629af033b2aSChao Yu #endif
4630af033b2aSChao Yu 	return false;
4631af033b2aSChao Yu }
46320af725fcSJaegeuk Kim 
f2fs_block_unit_discard(struct f2fs_sb_info * sbi)46334f993264SChao Yu static inline bool f2fs_block_unit_discard(struct f2fs_sb_info *sbi)
46344f993264SChao Yu {
46354f993264SChao Yu 	return F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_BLOCK;
46364f993264SChao Yu }
46374f993264SChao Yu 
f2fs_io_schedule_timeout(long timeout)4638a64239d0SNeilBrown static inline void f2fs_io_schedule_timeout(long timeout)
4639a64239d0SNeilBrown {
4640a64239d0SNeilBrown 	set_current_state(TASK_UNINTERRUPTIBLE);
4641a64239d0SNeilBrown 	io_schedule_timeout(timeout);
4642a64239d0SNeilBrown }
4643a64239d0SNeilBrown 
f2fs_handle_page_eio(struct f2fs_sb_info * sbi,pgoff_t ofs,enum page_type type)4644a7b8618aSJaegeuk Kim static inline void f2fs_handle_page_eio(struct f2fs_sb_info *sbi, pgoff_t ofs,
4645a7b8618aSJaegeuk Kim 					enum page_type type)
4646a7b8618aSJaegeuk Kim {
4647a7b8618aSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
4648a7b8618aSJaegeuk Kim 		return;
4649a7b8618aSJaegeuk Kim 
4650a7b8618aSJaegeuk Kim 	if (ofs == sbi->page_eio_ofs[type]) {
4651a7b8618aSJaegeuk Kim 		if (sbi->page_eio_cnt[type]++ == MAX_RETRY_PAGE_EIO)
4652a7b8618aSJaegeuk Kim 			set_ckpt_flags(sbi, CP_ERROR_FLAG);
4653a7b8618aSJaegeuk Kim 	} else {
4654a7b8618aSJaegeuk Kim 		sbi->page_eio_ofs[type] = ofs;
4655a7b8618aSJaegeuk Kim 		sbi->page_eio_cnt[type] = 0;
4656a7b8618aSJaegeuk Kim 	}
4657a7b8618aSJaegeuk Kim }
4658a7b8618aSJaegeuk Kim 
f2fs_is_readonly(struct f2fs_sb_info * sbi)4659ed8ac22bSYangtao Li static inline bool f2fs_is_readonly(struct f2fs_sb_info *sbi)
4660ed8ac22bSYangtao Li {
4661ed8ac22bSYangtao Li 	return f2fs_sb_has_readonly(sbi) || f2fs_readonly(sbi->sb);
4662ed8ac22bSYangtao Li }
4663ed8ac22bSYangtao Li 
f2fs_truncate_meta_inode_pages(struct f2fs_sb_info * sbi,block_t blkaddr,unsigned int cnt)4664c92f2927SChao Yu static inline void f2fs_truncate_meta_inode_pages(struct f2fs_sb_info *sbi,
4665c92f2927SChao Yu 					block_t blkaddr, unsigned int cnt)
4666c92f2927SChao Yu {
4667c92f2927SChao Yu 	bool need_submit = false;
4668c92f2927SChao Yu 	int i = 0;
4669c92f2927SChao Yu 
4670c92f2927SChao Yu 	do {
4671c92f2927SChao Yu 		struct page *page;
4672c92f2927SChao Yu 
4673c92f2927SChao Yu 		page = find_get_page(META_MAPPING(sbi), blkaddr + i);
4674c92f2927SChao Yu 		if (page) {
4675c92f2927SChao Yu 			if (PageWriteback(page))
4676c92f2927SChao Yu 				need_submit = true;
4677c92f2927SChao Yu 			f2fs_put_page(page, 0);
4678c92f2927SChao Yu 		}
4679c92f2927SChao Yu 	} while (++i < cnt && !need_submit);
4680c92f2927SChao Yu 
4681c92f2927SChao Yu 	if (need_submit)
4682c92f2927SChao Yu 		f2fs_submit_merged_write_cond(sbi, sbi->meta_inode,
4683c92f2927SChao Yu 							NULL, 0, DATA);
4684c92f2927SChao Yu 
4685c92f2927SChao Yu 	truncate_inode_pages_range(META_MAPPING(sbi),
4686c92f2927SChao Yu 			F2FS_BLK_TO_BYTES((loff_t)blkaddr),
4687c92f2927SChao Yu 			F2FS_BLK_END_BYTES((loff_t)(blkaddr + cnt - 1)));
4688c92f2927SChao Yu }
4689c92f2927SChao Yu 
f2fs_invalidate_internal_cache(struct f2fs_sb_info * sbi,block_t blkaddr)4690cfd217f6SChao Yu static inline void f2fs_invalidate_internal_cache(struct f2fs_sb_info *sbi,
4691cfd217f6SChao Yu 								block_t blkaddr)
4692cfd217f6SChao Yu {
4693c92f2927SChao Yu 	f2fs_truncate_meta_inode_pages(sbi, blkaddr, 1);
4694cfd217f6SChao Yu 	f2fs_invalidate_compress_page(sbi, blkaddr);
4695cfd217f6SChao Yu }
4696cfd217f6SChao Yu 
469710f966bbSChao Yu #define EFSBADCRC	EBADMSG		/* Bad CRC detected */
469810f966bbSChao Yu #define EFSCORRUPTED	EUCLEAN		/* Filesystem is corrupted */
469910f966bbSChao Yu 
4700e1074d4bSChao Yu #endif /* _LINUX_F2FS_H */
4701