xref: /openbmc/linux/fs/f2fs/f2fs.h (revision 25f9080576b9549be9435123d7e45bfeebd2dc97)
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;
7208796897SSheng Yong 	unsigned 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))
782c63feadSJaegeuk Kim #endif
792c63feadSJaegeuk Kim 
8039a53e0cSJaegeuk Kim /*
8139a53e0cSJaegeuk Kim  * For mount options
8239a53e0cSJaegeuk Kim  */
83478d7100SChao Yu #define F2FS_MOUNT_DISABLE_ROLL_FORWARD	0x00000001
84478d7100SChao Yu #define F2FS_MOUNT_DISCARD		0x00000002
85478d7100SChao Yu #define F2FS_MOUNT_NOHEAP		0x00000004
86478d7100SChao Yu #define F2FS_MOUNT_XATTR_USER		0x00000008
87478d7100SChao Yu #define F2FS_MOUNT_POSIX_ACL		0x00000010
88478d7100SChao Yu #define F2FS_MOUNT_DISABLE_EXT_IDENTIFY	0x00000020
89478d7100SChao Yu #define F2FS_MOUNT_INLINE_XATTR		0x00000040
90478d7100SChao Yu #define F2FS_MOUNT_INLINE_DATA		0x00000080
91478d7100SChao Yu #define F2FS_MOUNT_INLINE_DENTRY	0x00000100
92478d7100SChao Yu #define F2FS_MOUNT_FLUSH_MERGE		0x00000200
93478d7100SChao Yu #define F2FS_MOUNT_NOBARRIER		0x00000400
94478d7100SChao Yu #define F2FS_MOUNT_FASTBOOT		0x00000800
95478d7100SChao Yu #define F2FS_MOUNT_READ_EXTENT_CACHE	0x00001000
96478d7100SChao Yu #define F2FS_MOUNT_DATA_FLUSH		0x00002000
97478d7100SChao Yu #define F2FS_MOUNT_FAULT_INJECTION	0x00004000
98478d7100SChao Yu #define F2FS_MOUNT_USRQUOTA		0x00008000
99478d7100SChao Yu #define F2FS_MOUNT_GRPQUOTA		0x00010000
100478d7100SChao Yu #define F2FS_MOUNT_PRJQUOTA		0x00020000
101478d7100SChao Yu #define F2FS_MOUNT_QUOTA		0x00040000
102478d7100SChao Yu #define F2FS_MOUNT_INLINE_XATTR_SIZE	0x00080000
103478d7100SChao Yu #define F2FS_MOUNT_RESERVE_ROOT		0x00100000
104478d7100SChao Yu #define F2FS_MOUNT_DISABLE_CHECKPOINT	0x00200000
105478d7100SChao Yu #define F2FS_MOUNT_NORECOVERY		0x00400000
106478d7100SChao Yu #define F2FS_MOUNT_ATGC			0x00800000
107478d7100SChao Yu #define F2FS_MOUNT_MERGE_CHECKPOINT	0x01000000
108478d7100SChao Yu #define	F2FS_MOUNT_GC_MERGE		0x02000000
109478d7100SChao Yu #define F2FS_MOUNT_COMPRESS_CACHE	0x04000000
110478d7100SChao Yu #define F2FS_MOUNT_AGE_EXTENT_CACHE	0x08000000
11139a53e0cSJaegeuk Kim 
11263189b78SChao Yu #define F2FS_OPTION(sbi)	((sbi)->mount_opt)
11363189b78SChao Yu #define clear_opt(sbi, option)	(F2FS_OPTION(sbi).opt &= ~F2FS_MOUNT_##option)
11463189b78SChao Yu #define set_opt(sbi, option)	(F2FS_OPTION(sbi).opt |= F2FS_MOUNT_##option)
11563189b78SChao Yu #define test_opt(sbi, option)	(F2FS_OPTION(sbi).opt & F2FS_MOUNT_##option)
11639a53e0cSJaegeuk Kim 
11739a53e0cSJaegeuk Kim #define ver_after(a, b)	(typecheck(unsigned long long, a) &&		\
11839a53e0cSJaegeuk Kim 		typecheck(unsigned long long, b) &&			\
11939a53e0cSJaegeuk Kim 		((long long)((a) - (b)) > 0))
12039a53e0cSJaegeuk Kim 
121a9841c4dSJaegeuk Kim typedef u32 block_t;	/*
122a9841c4dSJaegeuk Kim 			 * should not change u32, since it is the on-disk block
123a9841c4dSJaegeuk Kim 			 * address format, __le32.
124a9841c4dSJaegeuk Kim 			 */
12539a53e0cSJaegeuk Kim typedef u32 nid_t;
12639a53e0cSJaegeuk Kim 
1274c8ff709SChao Yu #define COMPRESS_EXT_NUM		16
1284c8ff709SChao Yu 
129e4544b63STim Murray /*
130e4544b63STim Murray  * An implementation of an rwsem that is explicitly unfair to readers. This
131e4544b63STim Murray  * prevents priority inversion when a low-priority reader acquires the read lock
132e4544b63STim Murray  * while sleeping on the write lock but the write lock is needed by
133e4544b63STim Murray  * higher-priority clients.
134e4544b63STim Murray  */
135e4544b63STim Murray 
136e4544b63STim Murray struct f2fs_rwsem {
137e4544b63STim Murray         struct rw_semaphore internal_rwsem;
1387f8e249dSJaegeuk Kim #ifdef CONFIG_F2FS_UNFAIR_RWSEM
139e4544b63STim Murray         wait_queue_head_t read_waiters;
1407f8e249dSJaegeuk Kim #endif
141e4544b63STim Murray };
142e4544b63STim Murray 
14339a53e0cSJaegeuk Kim struct f2fs_mount_info {
14439a53e0cSJaegeuk Kim 	unsigned int opt;
14563189b78SChao Yu 	int write_io_size_bits;		/* Write IO size bits */
14663189b78SChao Yu 	block_t root_reserved_blocks;	/* root reserved blocks */
14763189b78SChao Yu 	kuid_t s_resuid;		/* reserved blocks for uid */
14863189b78SChao Yu 	kgid_t s_resgid;		/* reserved blocks for gid */
14963189b78SChao Yu 	int active_logs;		/* # of active logs */
15063189b78SChao Yu 	int inline_xattr_size;		/* inline xattr size */
15163189b78SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
15263189b78SChao Yu 	struct f2fs_fault_info fault_info;	/* For fault injection */
15363189b78SChao Yu #endif
15463189b78SChao Yu #ifdef CONFIG_QUOTA
15563189b78SChao Yu 	/* Names of quota files with journalled quota */
15663189b78SChao Yu 	char *s_qf_names[MAXQUOTAS];
15763189b78SChao Yu 	int s_jquota_fmt;			/* Format of quota to use */
15863189b78SChao Yu #endif
15963189b78SChao Yu 	/* For which write hints are passed down to block layer */
16063189b78SChao Yu 	int alloc_mode;			/* segment allocation policy */
16163189b78SChao Yu 	int fsync_mode;			/* fsync policy */
162b0332a0fSChao Yu 	int fs_mode;			/* fs mode: LFS or ADAPTIVE */
163bbbc34fdSChao Yu 	int bggc_mode;			/* bggc mode: off, on or sync */
1647a8fc586SDaeho Jeong 	int memory_mode;		/* memory mode */
165b62e71beSChao Yu 	int errors;			/* errors parameter */
1664f993264SChao Yu 	int discard_unit;		/*
1674f993264SChao Yu 					 * discard command's offset/size should
1684f993264SChao Yu 					 * be aligned to this unit: block,
1694f993264SChao Yu 					 * segment or section
1704f993264SChao Yu 					 */
171ac4acb1fSEric Biggers 	struct fscrypt_dummy_policy dummy_enc_policy; /* test dummy encryption */
1721ae18f71SJaegeuk Kim 	block_t unusable_cap_perc;	/* percentage for cap */
1734d3aed70SDaniel Rosenberg 	block_t unusable_cap;		/* Amount of space allowed to be
1744d3aed70SDaniel Rosenberg 					 * unusable when disabling checkpoint
1754d3aed70SDaniel Rosenberg 					 */
1764c8ff709SChao Yu 
1774c8ff709SChao Yu 	/* For compression */
1784c8ff709SChao Yu 	unsigned char compress_algorithm;	/* algorithm type */
179b28f047bSChao Yu 	unsigned char compress_log_size;	/* cluster log size */
1803fde13f8SChao Yu 	unsigned char compress_level;		/* compress level */
181b28f047bSChao Yu 	bool compress_chksum;			/* compressed data chksum */
1824c8ff709SChao Yu 	unsigned char compress_ext_cnt;		/* extension count */
183151b1982SFengnan Chang 	unsigned char nocompress_ext_cnt;		/* nocompress extension count */
184602a16d5SDaeho Jeong 	int compress_mode;			/* compression mode */
1854c8ff709SChao Yu 	unsigned char extensions[COMPRESS_EXT_NUM][F2FS_EXTENSION_LEN];	/* extensions */
186151b1982SFengnan Chang 	unsigned char noextensions[COMPRESS_EXT_NUM][F2FS_EXTENSION_LEN]; /* extensions */
18739a53e0cSJaegeuk Kim };
18839a53e0cSJaegeuk Kim 
18977e820eaSChao Yu #define F2FS_FEATURE_ENCRYPT			0x00000001
19077e820eaSChao Yu #define F2FS_FEATURE_BLKZONED			0x00000002
19177e820eaSChao Yu #define F2FS_FEATURE_ATOMIC_WRITE		0x00000004
19277e820eaSChao Yu #define F2FS_FEATURE_EXTRA_ATTR			0x00000008
19377e820eaSChao Yu #define F2FS_FEATURE_PRJQUOTA			0x00000010
19477e820eaSChao Yu #define F2FS_FEATURE_INODE_CHKSUM		0x00000020
19577e820eaSChao Yu #define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR	0x00000040
19677e820eaSChao Yu #define F2FS_FEATURE_QUOTA_INO			0x00000080
19777e820eaSChao Yu #define F2FS_FEATURE_INODE_CRTIME		0x00000100
19877e820eaSChao Yu #define F2FS_FEATURE_LOST_FOUND			0x00000200
19977e820eaSChao Yu #define F2FS_FEATURE_VERITY			0x00000400
20077e820eaSChao Yu #define F2FS_FEATURE_SB_CHKSUM			0x00000800
20177e820eaSChao Yu #define F2FS_FEATURE_CASEFOLD			0x00001000
20277e820eaSChao Yu #define F2FS_FEATURE_COMPRESSION		0x00002000
20377e820eaSChao Yu #define F2FS_FEATURE_RO				0x00004000
204cde4de12SJaegeuk Kim 
2057beb01f7SChao Yu #define __F2FS_HAS_FEATURE(raw_super, mask)				\
2067beb01f7SChao Yu 	((raw_super->feature & cpu_to_le32(mask)) != 0)
2077beb01f7SChao Yu #define F2FS_HAS_FEATURE(sbi, mask)	__F2FS_HAS_FEATURE(sbi->raw_super, mask)
20876f105a2SJaegeuk Kim 
20939a53e0cSJaegeuk Kim /*
2107c2e5963SJaegeuk Kim  * Default values for user and/or group using reserved blocks
2117c2e5963SJaegeuk Kim  */
2127c2e5963SJaegeuk Kim #define	F2FS_DEF_RESUID		0
2137c2e5963SJaegeuk Kim #define	F2FS_DEF_RESGID		0
2147c2e5963SJaegeuk Kim 
2157c2e5963SJaegeuk Kim /*
21639a53e0cSJaegeuk Kim  * For checkpoint manager
21739a53e0cSJaegeuk Kim  */
21839a53e0cSJaegeuk Kim enum {
21939a53e0cSJaegeuk Kim 	NAT_BITMAP,
22039a53e0cSJaegeuk Kim 	SIT_BITMAP
22139a53e0cSJaegeuk Kim };
22239a53e0cSJaegeuk Kim 
223c473f1a9SChao Yu #define	CP_UMOUNT	0x00000001
224c473f1a9SChao Yu #define	CP_FASTBOOT	0x00000002
225c473f1a9SChao Yu #define	CP_SYNC		0x00000004
226c473f1a9SChao Yu #define	CP_RECOVERY	0x00000008
227c473f1a9SChao Yu #define	CP_DISCARD	0x00000010
2281f43e2adSChao Yu #define CP_TRIMMED	0x00000020
2294354994fSDaniel Rosenberg #define CP_PAUSE	0x00000040
230b4b10061SJaegeuk Kim #define CP_RESIZE 	0x00000080
23175ab4cb8SJaegeuk Kim 
232ecc9aa00SChao Yu #define DEF_MAX_DISCARD_REQUEST		8	/* issue 8 discards per round */
233969d1b18SChao Yu #define DEF_MIN_DISCARD_ISSUE_TIME	50	/* 50 ms, if exists */
234f9d1dcedSYunlei He #define DEF_MID_DISCARD_ISSUE_TIME	500	/* 500 ms, if device busy */
235969d1b18SChao Yu #define DEF_MAX_DISCARD_ISSUE_TIME	60000	/* 60 s, if no candidates */
2368bb4f253SJaegeuk Kim #define DEF_DISCARD_URGENT_UTIL		80	/* do more discard over 80% */
23760b99b48SJaegeuk Kim #define DEF_CP_INTERVAL			60	/* 60 secs */
238dcf25fe8SChao Yu #define DEF_IDLE_INTERVAL		5	/* 5 secs */
2394354994fSDaniel Rosenberg #define DEF_DISABLE_INTERVAL		5	/* 5 secs */
240db610a64SJaegeuk Kim #define DEF_DISABLE_QUICK_INTERVAL	1	/* 1 secs */
24103f2c02dSJaegeuk Kim #define DEF_UMOUNT_DISCARD_TIMEOUT	5	/* 5 secs */
242bba681cbSJaegeuk Kim 
24375ab4cb8SJaegeuk Kim struct cp_control {
24475ab4cb8SJaegeuk Kim 	int reason;
2454b2fecc8SJaegeuk Kim 	__u64 trim_start;
2464b2fecc8SJaegeuk Kim 	__u64 trim_end;
2474b2fecc8SJaegeuk Kim 	__u64 trim_minlen;
24875ab4cb8SJaegeuk Kim };
24975ab4cb8SJaegeuk Kim 
250662befdaSChao Yu /*
251e1da7872SChao Yu  * indicate meta/data type
252662befdaSChao Yu  */
253662befdaSChao Yu enum {
254662befdaSChao Yu 	META_CP,
255662befdaSChao Yu 	META_NAT,
25681c1a0f1SChao Yu 	META_SIT,
2574c521f49SJaegeuk Kim 	META_SSA,
258b63e7be5SChao Yu 	META_MAX,
2594c521f49SJaegeuk Kim 	META_POR,
26093770ab7SChao Yu 	DATA_GENERIC,		/* check range only */
26193770ab7SChao Yu 	DATA_GENERIC_ENHANCE,	/* strong check on range and segment bitmap */
26293770ab7SChao Yu 	DATA_GENERIC_ENHANCE_READ,	/*
26393770ab7SChao Yu 					 * strong check on range and segment
26493770ab7SChao Yu 					 * bitmap but no warning due to race
26593770ab7SChao Yu 					 * condition of read on truncated area
26693770ab7SChao Yu 					 * by extent_cache
26793770ab7SChao Yu 					 */
2680ef4ca04SChao Yu 	DATA_GENERIC_ENHANCE_UPDATE,	/*
2690ef4ca04SChao Yu 					 * strong check on range and segment
2700ef4ca04SChao Yu 					 * bitmap for update case
2710ef4ca04SChao Yu 					 */
272e1da7872SChao Yu 	META_GENERIC,
273662befdaSChao Yu };
274662befdaSChao Yu 
2756451e041SJaegeuk Kim /* for the list of ino */
2766451e041SJaegeuk Kim enum {
2776451e041SJaegeuk Kim 	ORPHAN_INO,		/* for orphan ino list */
278fff04f90SJaegeuk Kim 	APPEND_INO,		/* for append ino list */
279fff04f90SJaegeuk Kim 	UPDATE_INO,		/* for update ino list */
280d382e369SYonggil Song 	TRANS_DIR_INO,		/* for transactions dir ino list */
28139d787beSChao Yu 	FLUSH_INO,		/* for multiple device flushing */
2826451e041SJaegeuk Kim 	MAX_INO_ENTRY,		/* max. list */
2836451e041SJaegeuk Kim };
2846451e041SJaegeuk Kim 
2856451e041SJaegeuk Kim struct ino_entry {
28639a53e0cSJaegeuk Kim 	struct list_head list;		/* list head */
28739a53e0cSJaegeuk Kim 	nid_t ino;			/* inode number */
28839d787beSChao Yu 	unsigned int dirty_device;	/* dirty device bitmap */
28939a53e0cSJaegeuk Kim };
29039a53e0cSJaegeuk Kim 
2912710fd7eSChao Yu /* for the list of inodes to be GCed */
29206292073SChao Yu struct inode_entry {
29339a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
29439a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
29539a53e0cSJaegeuk Kim };
29639a53e0cSJaegeuk Kim 
29750fa53ecSChao Yu struct fsync_node_entry {
29850fa53ecSChao Yu 	struct list_head list;	/* list head */
29950fa53ecSChao Yu 	struct page *page;	/* warm node page pointer */
30050fa53ecSChao Yu 	unsigned int seq_id;	/* sequence id */
30150fa53ecSChao Yu };
30250fa53ecSChao Yu 
303261eeb9cSDaeho Jeong struct ckpt_req {
304261eeb9cSDaeho Jeong 	struct completion wait;		/* completion for checkpoint done */
305261eeb9cSDaeho Jeong 	struct llist_node llnode;	/* llist_node to be linked in wait queue */
306261eeb9cSDaeho Jeong 	int ret;			/* return code of checkpoint */
307261eeb9cSDaeho Jeong 	ktime_t queue_time;		/* request queued time */
308261eeb9cSDaeho Jeong };
309261eeb9cSDaeho Jeong 
310261eeb9cSDaeho Jeong struct ckpt_req_control {
311261eeb9cSDaeho Jeong 	struct task_struct *f2fs_issue_ckpt;	/* checkpoint task */
312e6592066SDaeho Jeong 	int ckpt_thread_ioprio;			/* checkpoint merge thread ioprio */
313261eeb9cSDaeho Jeong 	wait_queue_head_t ckpt_wait_queue;	/* waiting queue for wake-up */
314261eeb9cSDaeho Jeong 	atomic_t issued_ckpt;		/* # of actually issued ckpts */
315261eeb9cSDaeho Jeong 	atomic_t total_ckpt;		/* # of total ckpts */
316261eeb9cSDaeho Jeong 	atomic_t queued_ckpt;		/* # of queued ckpts */
317261eeb9cSDaeho Jeong 	struct llist_head issue_list;	/* list for command issue */
318261eeb9cSDaeho Jeong 	spinlock_t stat_lock;		/* lock for below checkpoint time stats */
319261eeb9cSDaeho Jeong 	unsigned int cur_time;		/* cur wait time in msec for currently issued checkpoint */
320261eeb9cSDaeho Jeong 	unsigned int peak_time;		/* peak wait time in msec until now */
321261eeb9cSDaeho Jeong };
322261eeb9cSDaeho Jeong 
323a7eeb823SChao Yu /* for the bitmap indicate blocks to be discarded */
3247fd9e544SJaegeuk Kim struct discard_entry {
3257fd9e544SJaegeuk Kim 	struct list_head list;	/* list head */
326a7eeb823SChao Yu 	block_t start_blkaddr;	/* start blockaddr of current segment */
327a7eeb823SChao Yu 	unsigned char discard_map[SIT_VBLOCK_MAP_SIZE];	/* segment discard bitmap */
3287fd9e544SJaegeuk Kim };
3297fd9e544SJaegeuk Kim 
3301cd2e6d5SYangtao Li /* minimum discard granularity, unit: block count */
3311cd2e6d5SYangtao Li #define MIN_DISCARD_GRANULARITY		1
332969d1b18SChao Yu /* default discard granularity of inner discard thread, unit: block count */
333969d1b18SChao Yu #define DEFAULT_DISCARD_GRANULARITY		16
334c46867e9SYangtao Li /* default maximum discard granularity of ordered discard, unit: block count */
335c46867e9SYangtao Li #define DEFAULT_MAX_ORDERED_DISCARD_GRANULARITY	16
336969d1b18SChao Yu 
337ba48a33eSChao Yu /* max discard pend list number */
338ba48a33eSChao Yu #define MAX_PLIST_NUM		512
339ba48a33eSChao Yu #define plist_idx(blk_num)	((blk_num) >= MAX_PLIST_NUM ?		\
3402f84babfSSheng Yong 					(MAX_PLIST_NUM - 1) : ((blk_num) - 1))
341ba48a33eSChao Yu 
34215469963SJaegeuk Kim enum {
34335ec7d57SChao Yu 	D_PREP,			/* initial */
34435ec7d57SChao Yu 	D_PARTIAL,		/* partially submitted */
34535ec7d57SChao Yu 	D_SUBMIT,		/* all submitted */
34635ec7d57SChao Yu 	D_DONE,			/* finished */
34715469963SJaegeuk Kim };
34815469963SJaegeuk Kim 
349004b6862SChao Yu struct discard_info {
350b01a9201SJaegeuk Kim 	block_t lstart;			/* logical start address */
351b01a9201SJaegeuk Kim 	block_t len;			/* length */
352004b6862SChao Yu 	block_t start;			/* actual start address in dev */
353004b6862SChao Yu };
354004b6862SChao Yu 
355b01a9201SJaegeuk Kim struct discard_cmd {
356004b6862SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
357004b6862SChao Yu 	struct discard_info di;		/* discard info */
358b01a9201SJaegeuk Kim 	struct list_head list;		/* command list */
359b01a9201SJaegeuk Kim 	struct completion wait;		/* compleation */
360c81abe34SJaegeuk Kim 	struct block_device *bdev;	/* bdev */
361ec9895adSChao Yu 	unsigned short ref;		/* reference count */
3629a744b92SChao Yu 	unsigned char state;		/* state */
36372691af6SJaegeuk Kim 	unsigned char queued;		/* queued discard */
364c81abe34SJaegeuk Kim 	int error;			/* bio error */
36535ec7d57SChao Yu 	spinlock_t lock;		/* for state/bio_ref updating */
36635ec7d57SChao Yu 	unsigned short bio_ref;		/* bio reference count */
367275b66b0SChao Yu };
368275b66b0SChao Yu 
36978997b56SChao Yu enum {
37078997b56SChao Yu 	DPOLICY_BG,
37178997b56SChao Yu 	DPOLICY_FORCE,
37278997b56SChao Yu 	DPOLICY_FSTRIM,
37378997b56SChao Yu 	DPOLICY_UMOUNT,
37478997b56SChao Yu 	MAX_DPOLICY,
37578997b56SChao Yu };
37678997b56SChao Yu 
377ecc9aa00SChao Yu struct discard_policy {
37878997b56SChao Yu 	int type;			/* type of discard */
379ecc9aa00SChao Yu 	unsigned int min_interval;	/* used for candidates exist */
380f9d1dcedSYunlei He 	unsigned int mid_interval;	/* used for device busy */
381ecc9aa00SChao Yu 	unsigned int max_interval;	/* used for candidates not exist */
382ecc9aa00SChao Yu 	unsigned int max_requests;	/* # of discards issued per round */
383ecc9aa00SChao Yu 	unsigned int io_aware_gran;	/* minimum granularity discard not be aware of I/O */
384ecc9aa00SChao Yu 	bool io_aware;			/* issue discard in idle time */
385ecc9aa00SChao Yu 	bool sync;			/* submit discard with REQ_SYNC flag */
38620ee4382SChao Yu 	bool ordered;			/* issue discard by lba order */
3876ce48b0cSChao Yu 	bool timeout;			/* discard timeout for put_super */
38878997b56SChao Yu 	unsigned int granularity;	/* discard granularity */
389ecc9aa00SChao Yu };
390ecc9aa00SChao Yu 
3910b54fb84SJaegeuk Kim struct discard_cmd_control {
39215469963SJaegeuk Kim 	struct task_struct *f2fs_issue_discard;	/* discard thread */
39346f84c2cSChao Yu 	struct list_head entry_list;		/* 4KB discard entry list */
394ba48a33eSChao Yu 	struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
39546f84c2cSChao Yu 	struct list_head wait_list;		/* store on-flushing entries */
3968412663dSChao Yu 	struct list_head fstrim_list;		/* in-flight discard from fstrim */
39715469963SJaegeuk Kim 	wait_queue_head_t discard_wait_queue;	/* waiting queue for wake-up */
39815469963SJaegeuk Kim 	struct mutex cmd_lock;
399d618ebafSChao Yu 	unsigned int nr_discards;		/* # of discards in the list */
400d618ebafSChao Yu 	unsigned int max_discards;		/* max. discards to be issued */
401d2d8e896SKonstantin Vyshetsky 	unsigned int max_discard_request;	/* max. discard request per round */
402d2d8e896SKonstantin Vyshetsky 	unsigned int min_discard_issue_time;	/* min. interval between discard issue */
403d2d8e896SKonstantin Vyshetsky 	unsigned int mid_discard_issue_time;	/* mid. interval between discard issue */
404d2d8e896SKonstantin Vyshetsky 	unsigned int max_discard_issue_time;	/* max. interval between discard issue */
405120e0ea1SYangtao Li 	unsigned int discard_io_aware_gran; /* minimum discard granularity not be aware of I/O */
4068a47d228SYangtao Li 	unsigned int discard_urgent_util;	/* utilization which issue discard proactively */
407969d1b18SChao Yu 	unsigned int discard_granularity;	/* discard granularity */
408c46867e9SYangtao Li 	unsigned int max_ordered_discard;	/* maximum discard granularity issued by lba order */
409d84d1cbdSChao Yu 	unsigned int undiscard_blks;		/* # of undiscard blocks */
41020ee4382SChao Yu 	unsigned int next_pos;			/* next discard position */
4118b8dd65fSChao Yu 	atomic_t issued_discard;		/* # of issued discard */
41272691af6SJaegeuk Kim 	atomic_t queued_discard;		/* # of queued discard */
4135f32366aSChao Yu 	atomic_t discard_cmd_cnt;		/* # of cached cmd count */
4144dada3fdSChao Yu 	struct rb_root_cached root;		/* root of discard rb-tree */
41567fce70bSChao Yu 	bool rbtree_check;			/* config for consistence check */
41645c98f5aSYangtao Li 	bool discard_wake;			/* to wake up discard thread */
41739a53e0cSJaegeuk Kim };
41839a53e0cSJaegeuk Kim 
41939a53e0cSJaegeuk Kim /* for the list of fsync inodes, used only during recovery */
42039a53e0cSJaegeuk Kim struct fsync_inode_entry {
42139a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
42239a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
423c52e1b10SJaegeuk Kim 	block_t blkaddr;	/* block address locating the last fsync */
424c52e1b10SJaegeuk Kim 	block_t last_dentry;	/* block address locating the last dentry */
42539a53e0cSJaegeuk Kim };
42639a53e0cSJaegeuk Kim 
42768afcf2dSTomohiro Kusumi #define nats_in_cursum(jnl)		(le16_to_cpu((jnl)->n_nats))
42868afcf2dSTomohiro Kusumi #define sits_in_cursum(jnl)		(le16_to_cpu((jnl)->n_sits))
42939a53e0cSJaegeuk Kim 
43068afcf2dSTomohiro Kusumi #define nat_in_journal(jnl, i)		((jnl)->nat_j.entries[i].ne)
43168afcf2dSTomohiro Kusumi #define nid_in_journal(jnl, i)		((jnl)->nat_j.entries[i].nid)
43268afcf2dSTomohiro Kusumi #define sit_in_journal(jnl, i)		((jnl)->sit_j.entries[i].se)
43368afcf2dSTomohiro Kusumi #define segno_in_journal(jnl, i)	((jnl)->sit_j.entries[i].segno)
43439a53e0cSJaegeuk Kim 
435dfc08a12SChao Yu #define MAX_NAT_JENTRIES(jnl)	(NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
436dfc08a12SChao Yu #define MAX_SIT_JENTRIES(jnl)	(SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
437309cc2b6SJaegeuk Kim 
438dfc08a12SChao Yu static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
43939a53e0cSJaegeuk Kim {
440dfc08a12SChao Yu 	int before = nats_in_cursum(journal);
441cac5a3d8SDongOh Shin 
442dfc08a12SChao Yu 	journal->n_nats = cpu_to_le16(before + i);
44339a53e0cSJaegeuk Kim 	return before;
44439a53e0cSJaegeuk Kim }
44539a53e0cSJaegeuk Kim 
446dfc08a12SChao Yu static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
44739a53e0cSJaegeuk Kim {
448dfc08a12SChao Yu 	int before = sits_in_cursum(journal);
449cac5a3d8SDongOh Shin 
450dfc08a12SChao Yu 	journal->n_sits = cpu_to_le16(before + i);
45139a53e0cSJaegeuk Kim 	return before;
45239a53e0cSJaegeuk Kim }
45339a53e0cSJaegeuk Kim 
454dfc08a12SChao Yu static inline bool __has_cursum_space(struct f2fs_journal *journal,
455dfc08a12SChao Yu 							int size, int type)
456184a5cd2SChao Yu {
457184a5cd2SChao Yu 	if (type == NAT_JOURNAL)
458dfc08a12SChao Yu 		return size <= MAX_NAT_JENTRIES(journal);
459dfc08a12SChao Yu 	return size <= MAX_SIT_JENTRIES(journal);
460184a5cd2SChao Yu }
461184a5cd2SChao Yu 
462f2470371SChao Yu /* for inline stuff */
463f2470371SChao Yu #define DEF_INLINE_RESERVED_SIZE	1
4647a2af766SChao Yu static inline int get_extra_isize(struct inode *inode);
4656afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode);
4667a2af766SChao Yu #define MAX_INLINE_DATA(inode)	(sizeof(__le32) *			\
4677a2af766SChao Yu 				(CUR_ADDRS_PER_INODE(inode) -		\
468b323fd28SChao Yu 				get_inline_xattr_addrs(inode) -	\
4696afc662eSChao Yu 				DEF_INLINE_RESERVED_SIZE))
470f2470371SChao Yu 
471f2470371SChao Yu /* for inline dir */
472f2470371SChao Yu #define NR_INLINE_DENTRY(inode)	(MAX_INLINE_DATA(inode) * BITS_PER_BYTE / \
473f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
474f2470371SChao Yu 				BITS_PER_BYTE + 1))
475f91108b8SGeert Uytterhoeven #define INLINE_DENTRY_BITMAP_SIZE(inode) \
476f91108b8SGeert Uytterhoeven 	DIV_ROUND_UP(NR_INLINE_DENTRY(inode), BITS_PER_BYTE)
477f2470371SChao Yu #define INLINE_RESERVED_SIZE(inode)	(MAX_INLINE_DATA(inode) - \
478f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
479f2470371SChao Yu 				NR_INLINE_DENTRY(inode) + \
480f2470371SChao Yu 				INLINE_DENTRY_BITMAP_SIZE(inode)))
481f2470371SChao Yu 
482e9750824SNamjae Jeon /*
48339a53e0cSJaegeuk Kim  * For INODE and NODE manager
48439a53e0cSJaegeuk Kim  */
4857b3cd7d6SJaegeuk Kim /* for directory operations */
48643c780baSEric Biggers 
48743c780baSEric Biggers struct f2fs_filename {
48843c780baSEric Biggers 	/*
48943c780baSEric Biggers 	 * The filename the user specified.  This is NULL for some
49043c780baSEric Biggers 	 * filesystem-internal operations, e.g. converting an inline directory
49143c780baSEric Biggers 	 * to a non-inline one, or roll-forward recovering an encrypted dentry.
49243c780baSEric Biggers 	 */
49343c780baSEric Biggers 	const struct qstr *usr_fname;
49443c780baSEric Biggers 
49543c780baSEric Biggers 	/*
49643c780baSEric Biggers 	 * The on-disk filename.  For encrypted directories, this is encrypted.
49743c780baSEric Biggers 	 * This may be NULL for lookups in an encrypted dir without the key.
49843c780baSEric Biggers 	 */
49943c780baSEric Biggers 	struct fscrypt_str disk_name;
50043c780baSEric Biggers 
50143c780baSEric Biggers 	/* The dirhash of this filename */
50243c780baSEric Biggers 	f2fs_hash_t hash;
50343c780baSEric Biggers 
50443c780baSEric Biggers #ifdef CONFIG_FS_ENCRYPTION
50543c780baSEric Biggers 	/*
50643c780baSEric Biggers 	 * For lookups in encrypted directories: either the buffer backing
50743c780baSEric Biggers 	 * disk_name, or a buffer that holds the decoded no-key name.
50843c780baSEric Biggers 	 */
50943c780baSEric Biggers 	struct fscrypt_str crypto_buf;
51043c780baSEric Biggers #endif
5115298d4bfSChristoph Hellwig #if IS_ENABLED(CONFIG_UNICODE)
51243c780baSEric Biggers 	/*
51343c780baSEric Biggers 	 * For casefolded directories: the casefolded name, but it's left NULL
514b5639bb4SEric Biggers 	 * if the original name is not valid Unicode, if the original name is
515b5639bb4SEric Biggers 	 * "." or "..", if the directory is both casefolded and encrypted and
516b5639bb4SEric Biggers 	 * its encryption key is unavailable, or if the filesystem is doing an
517b5639bb4SEric Biggers 	 * internal operation where usr_fname is also NULL.  In all these cases
518b5639bb4SEric Biggers 	 * we fall back to treating the name as an opaque byte sequence.
51943c780baSEric Biggers 	 */
52043c780baSEric Biggers 	struct fscrypt_str cf_name;
52143c780baSEric Biggers #endif
52243c780baSEric Biggers };
52343c780baSEric Biggers 
5247b3cd7d6SJaegeuk Kim struct f2fs_dentry_ptr {
525d8c6822aSJaegeuk Kim 	struct inode *inode;
52676a9dd85SChao Yu 	void *bitmap;
5277b3cd7d6SJaegeuk Kim 	struct f2fs_dir_entry *dentry;
5287b3cd7d6SJaegeuk Kim 	__u8 (*filename)[F2FS_SLOT_LEN];
5297b3cd7d6SJaegeuk Kim 	int max;
53076a9dd85SChao Yu 	int nr_bitmap;
5317b3cd7d6SJaegeuk Kim };
5327b3cd7d6SJaegeuk Kim 
53364c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_block(struct inode *inode,
53464c24ecbSTomohiro Kusumi 		struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
5357b3cd7d6SJaegeuk Kim {
536d8c6822aSJaegeuk Kim 	d->inode = inode;
5377b3cd7d6SJaegeuk Kim 	d->max = NR_DENTRY_IN_BLOCK;
53876a9dd85SChao Yu 	d->nr_bitmap = SIZE_OF_DENTRY_BITMAP;
539c79d1520SYunlong Song 	d->bitmap = t->dentry_bitmap;
5407b3cd7d6SJaegeuk Kim 	d->dentry = t->dentry;
5417b3cd7d6SJaegeuk Kim 	d->filename = t->filename;
54264c24ecbSTomohiro Kusumi }
543cac5a3d8SDongOh Shin 
54464c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_inline(struct inode *inode,
545f2470371SChao Yu 					struct f2fs_dentry_ptr *d, void *t)
54664c24ecbSTomohiro Kusumi {
547f2470371SChao Yu 	int entry_cnt = NR_INLINE_DENTRY(inode);
548f2470371SChao Yu 	int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(inode);
549f2470371SChao Yu 	int reserved_size = INLINE_RESERVED_SIZE(inode);
550f2470371SChao Yu 
55164c24ecbSTomohiro Kusumi 	d->inode = inode;
552f2470371SChao Yu 	d->max = entry_cnt;
553f2470371SChao Yu 	d->nr_bitmap = bitmap_size;
554f2470371SChao Yu 	d->bitmap = t;
555f2470371SChao Yu 	d->dentry = t + bitmap_size + reserved_size;
556f2470371SChao Yu 	d->filename = t + bitmap_size + reserved_size +
557f2470371SChao Yu 					SIZE_OF_DIR_ENTRY * entry_cnt;
5587b3cd7d6SJaegeuk Kim }
5597b3cd7d6SJaegeuk Kim 
560dbe6a5ffSJaegeuk Kim /*
561dbe6a5ffSJaegeuk Kim  * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
562dbe6a5ffSJaegeuk Kim  * as its node offset to distinguish from index node blocks.
563dbe6a5ffSJaegeuk Kim  * But some bits are used to mark the node block.
56439a53e0cSJaegeuk Kim  */
565dbe6a5ffSJaegeuk Kim #define XATTR_NODE_OFFSET	((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
566dbe6a5ffSJaegeuk Kim 				>> OFFSET_BIT_SHIFT)
567266e97a8SJaegeuk Kim enum {
568266e97a8SJaegeuk Kim 	ALLOC_NODE,			/* allocate a new node page if needed */
569266e97a8SJaegeuk Kim 	LOOKUP_NODE,			/* look up a node without readahead */
570266e97a8SJaegeuk Kim 	LOOKUP_NODE_RA,			/*
571266e97a8SJaegeuk Kim 					 * look up a node with readahead called
5724f4124d0SChao Yu 					 * by get_data_block.
57339a53e0cSJaegeuk Kim 					 */
574266e97a8SJaegeuk Kim };
575266e97a8SJaegeuk Kim 
57691803392SChao Yu #define DEFAULT_RETRY_IO_COUNT	8	/* maximum retry read IO or flush count */
5777735730dSChao Yu 
5785df7731fSChao Yu /* congestion wait timeout value, default: 20ms */
5795df7731fSChao Yu #define	DEFAULT_IO_TIMEOUT	(msecs_to_jiffies(20))
5805df7731fSChao Yu 
581af033b2aSChao Yu /* maximum retry quota flush count */
582af033b2aSChao Yu #define DEFAULT_RETRY_QUOTA_FLUSH_COUNT		8
583af033b2aSChao Yu 
584a7b8618aSJaegeuk Kim /* maximum retry of EIO'ed page */
585a7b8618aSJaegeuk Kim #define MAX_RETRY_PAGE_EIO			100
58650c63009SJaegeuk Kim 
587a6db67f0SJaegeuk Kim #define F2FS_LINK_MAX	0xffffffff	/* maximum link count per file */
58839a53e0cSJaegeuk Kim 
589817202d9SChao Yu #define MAX_DIR_RA_PAGES	4	/* maximum ra pages of dir */
590817202d9SChao Yu 
591287b1406SChao Yu /* dirty segments threshold for triggering CP */
592287b1406SChao Yu #define DEFAULT_DIRTY_THRESHOLD		4
593287b1406SChao Yu 
594e7547dacSJaegeuk Kim #define RECOVERY_MAX_RA_BLOCKS		BIO_MAX_VECS
595e7547dacSJaegeuk Kim #define RECOVERY_MIN_RA_BLOCKS		1
596e7547dacSJaegeuk Kim 
597e7547dacSJaegeuk Kim #define F2FS_ONSTACK_PAGES	16	/* nr of onstack pages */
598e7547dacSJaegeuk Kim 
59939a53e0cSJaegeuk Kim /* for in-memory extent cache entry */
60013054c54SChao Yu #define F2FS_MIN_EXTENT_LEN	64	/* minimum extent length */
60113054c54SChao Yu 
60213054c54SChao Yu /* number of extent info in extent cache we try to shrink */
60312607c1bSJaegeuk Kim #define READ_EXTENT_CACHE_SHRINK_NUMBER	128
604c11abd1aSJaegeuk Kim 
60571644dffSJaegeuk Kim /* number of age extent info in extent cache we try to shrink */
60671644dffSJaegeuk Kim #define AGE_EXTENT_CACHE_SHRINK_NUMBER	128
60771644dffSJaegeuk Kim #define LAST_AGE_WEIGHT			30
60871644dffSJaegeuk Kim #define SAME_AGE_REGION			1024
60971644dffSJaegeuk Kim 
61071644dffSJaegeuk Kim /*
61171644dffSJaegeuk Kim  * Define data block with age less than 1GB as hot data
61271644dffSJaegeuk Kim  * define data block with age less than 10GB but more than 1GB as warm data
61371644dffSJaegeuk Kim  */
61471644dffSJaegeuk Kim #define DEF_HOT_DATA_AGE_THRESHOLD	262144
61571644dffSJaegeuk Kim #define DEF_WARM_DATA_AGE_THRESHOLD	2621440
61671644dffSJaegeuk Kim 
617e7547dacSJaegeuk Kim /* extent cache type */
618e7547dacSJaegeuk Kim enum extent_type {
619e7547dacSJaegeuk Kim 	EX_READ,
62071644dffSJaegeuk Kim 	EX_BLOCK_AGE,
621e7547dacSJaegeuk Kim 	NR_EXTENT_CACHES,
622e7547dacSJaegeuk Kim };
62301fc4b9aSFengnan Chang 
62439a53e0cSJaegeuk Kim struct extent_info {
62539a53e0cSJaegeuk Kim 	unsigned int fofs;		/* start offset in a file */
626111d2495SMasanari Iida 	unsigned int len;		/* length of the extent */
627e7547dacSJaegeuk Kim 	union {
628e7547dacSJaegeuk Kim 		/* read extent_cache */
629e7547dacSJaegeuk Kim 		struct {
630e7547dacSJaegeuk Kim 			/* start block address of the extent */
631e7547dacSJaegeuk Kim 			block_t blk;
63294afd6d6SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
633e7547dacSJaegeuk Kim 			/* physical extent length of compressed blocks */
634e7547dacSJaegeuk Kim 			unsigned int c_len;
63594afd6d6SChao Yu #endif
63639a53e0cSJaegeuk Kim 		};
63771644dffSJaegeuk Kim 		/* block age extent_cache */
63871644dffSJaegeuk Kim 		struct {
63971644dffSJaegeuk Kim 			/* block age of the extent */
64071644dffSJaegeuk Kim 			unsigned long long age;
64171644dffSJaegeuk Kim 			/* last total blocks allocated */
64271644dffSJaegeuk Kim 			unsigned long long last_blocks;
64371644dffSJaegeuk Kim 		};
644e7547dacSJaegeuk Kim 	};
645e7547dacSJaegeuk Kim };
64639a53e0cSJaegeuk Kim 
64713054c54SChao Yu struct extent_node {
648c0362117SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
64913054c54SChao Yu 	struct extent_info ei;		/* extent info */
65054c2258cSChao Yu 	struct list_head list;		/* node in global extent list of sbi */
651201ef5e0SHou Pengyang 	struct extent_tree *et;		/* extent tree pointer */
65213054c54SChao Yu };
65313054c54SChao Yu 
65413054c54SChao Yu struct extent_tree {
65513054c54SChao Yu 	nid_t ino;			/* inode number */
656e7547dacSJaegeuk Kim 	enum extent_type type;		/* keep the extent tree type */
6574dada3fdSChao Yu 	struct rb_root_cached root;	/* root of extent info rb-tree */
65862c8af65SChao Yu 	struct extent_node *cached_en;	/* recently accessed extent node */
659137d09f0SJaegeuk Kim 	struct list_head list;		/* to be used by sbi->zombie_list */
66013054c54SChao Yu 	rwlock_t lock;			/* protect extent info rb-tree */
66168e35385SChao Yu 	atomic_t node_cnt;		/* # of extent node in rb-tree*/
662b430f726SZhikang Zhang 	bool largest_updated;		/* largest extent updated */
663e7547dacSJaegeuk Kim 	struct extent_info largest;	/* largest cached extent for EX_READ */
664e7547dacSJaegeuk Kim };
665e7547dacSJaegeuk Kim 
666e7547dacSJaegeuk Kim struct extent_tree_info {
667e7547dacSJaegeuk Kim 	struct radix_tree_root extent_tree_root;/* cache extent cache entries */
668e7547dacSJaegeuk Kim 	struct mutex extent_tree_lock;	/* locking extent radix tree */
669e7547dacSJaegeuk Kim 	struct list_head extent_list;		/* lru list for shrinker */
670e7547dacSJaegeuk Kim 	spinlock_t extent_lock;			/* locking extent lru list */
671e7547dacSJaegeuk Kim 	atomic_t total_ext_tree;		/* extent tree count */
672e7547dacSJaegeuk Kim 	struct list_head zombie_list;		/* extent zombie tree list */
673e7547dacSJaegeuk Kim 	atomic_t total_zombie_tree;		/* extent zombie tree count */
674e7547dacSJaegeuk Kim 	atomic_t total_ext_node;		/* extent info count */
67513054c54SChao Yu };
67613054c54SChao Yu 
67739a53e0cSJaegeuk Kim /*
67862a134bdSChristoph Hellwig  * State of block returned by f2fs_map_blocks.
679003a3e1dSJaegeuk Kim  */
68062a134bdSChristoph Hellwig #define F2FS_MAP_NEW		(1U << 0)
68162a134bdSChristoph Hellwig #define F2FS_MAP_MAPPED		(1U << 1)
682da8c7fecSChristoph Hellwig #define F2FS_MAP_DELALLOC	(1U << 2)
6837f63eb77SJaegeuk Kim #define F2FS_MAP_FLAGS		(F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
684da8c7fecSChristoph Hellwig 				F2FS_MAP_DELALLOC)
685003a3e1dSJaegeuk Kim 
686003a3e1dSJaegeuk Kim struct f2fs_map_blocks {
68771f2c820SChao Yu 	struct block_device *m_bdev;	/* for multi-device dio */
688003a3e1dSJaegeuk Kim 	block_t m_pblk;
689003a3e1dSJaegeuk Kim 	block_t m_lblk;
690003a3e1dSJaegeuk Kim 	unsigned int m_len;
691003a3e1dSJaegeuk Kim 	unsigned int m_flags;
692da85985cSChao Yu 	pgoff_t *m_next_pgofs;		/* point next possible non-hole pgofs */
693c4020b2dSChao Yu 	pgoff_t *m_next_extent;		/* point to next possible extent */
694d5097be5SHyunchul Lee 	int m_seg_type;
695f9d6d059SChao Yu 	bool m_may_create;		/* indicate it is from write path */
69671f2c820SChao Yu 	bool m_multidev_dio;		/* indicate it allows multi-device dio */
697003a3e1dSJaegeuk Kim };
698003a3e1dSJaegeuk Kim 
699e2b4e2bcSChao Yu /* for flag in get_data_block */
700f2220c7fSQiuyang Sun enum {
701f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_DEFAULT,
702f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_FIEMAP,
703f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_BMAP,
7040a4daae5SJaegeuk Kim 	F2FS_GET_BLOCK_DIO,
705f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_DIO,
706f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_AIO,
707c4020b2dSChao Yu 	F2FS_GET_BLOCK_PRECACHE,
708f2220c7fSQiuyang Sun };
709e2b4e2bcSChao Yu 
710003a3e1dSJaegeuk Kim /*
71139a53e0cSJaegeuk Kim  * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
71239a53e0cSJaegeuk Kim  */
71339a53e0cSJaegeuk Kim #define FADVISE_COLD_BIT	0x01
714354a3399SJaegeuk Kim #define FADVISE_LOST_PINO_BIT	0x02
715cde4de12SJaegeuk Kim #define FADVISE_ENCRYPT_BIT	0x04
716e7d55452SJaegeuk Kim #define FADVISE_ENC_NAME_BIT	0x08
71726787236SJaegeuk Kim #define FADVISE_KEEP_SIZE_BIT	0x10
718b6a06cbbSChao Yu #define FADVISE_HOT_BIT		0x20
71995ae251fSEric Biggers #define FADVISE_VERITY_BIT	0x40
720d4dd19ecSJaegeuk Kim #define FADVISE_TRUNC_BIT	0x80
72139a53e0cSJaegeuk Kim 
722797c1cb5SChao Yu #define FADVISE_MODIFIABLE_BITS	(FADVISE_COLD_BIT | FADVISE_HOT_BIT)
723797c1cb5SChao Yu 
724b5492af7SJaegeuk Kim #define file_is_cold(inode)	is_file(inode, FADVISE_COLD_BIT)
725b5492af7SJaegeuk Kim #define file_set_cold(inode)	set_file(inode, FADVISE_COLD_BIT)
726b5492af7SJaegeuk Kim #define file_clear_cold(inode)	clear_file(inode, FADVISE_COLD_BIT)
7271153db09SChao Yu 
7281153db09SChao Yu #define file_wrong_pino(inode)	is_file(inode, FADVISE_LOST_PINO_BIT)
7291153db09SChao Yu #define file_lost_pino(inode)	set_file(inode, FADVISE_LOST_PINO_BIT)
730b5492af7SJaegeuk Kim #define file_got_pino(inode)	clear_file(inode, FADVISE_LOST_PINO_BIT)
7311153db09SChao Yu 
732cde4de12SJaegeuk Kim #define file_is_encrypt(inode)	is_file(inode, FADVISE_ENCRYPT_BIT)
733cde4de12SJaegeuk Kim #define file_set_encrypt(inode)	set_file(inode, FADVISE_ENCRYPT_BIT)
7341153db09SChao Yu 
735e7d55452SJaegeuk Kim #define file_enc_name(inode)	is_file(inode, FADVISE_ENC_NAME_BIT)
736e7d55452SJaegeuk Kim #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
7371153db09SChao Yu 
73826787236SJaegeuk Kim #define file_keep_isize(inode)	is_file(inode, FADVISE_KEEP_SIZE_BIT)
73926787236SJaegeuk Kim #define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
7401153db09SChao Yu 
741b6a06cbbSChao Yu #define file_is_hot(inode)	is_file(inode, FADVISE_HOT_BIT)
742b6a06cbbSChao Yu #define file_set_hot(inode)	set_file(inode, FADVISE_HOT_BIT)
743b6a06cbbSChao Yu #define file_clear_hot(inode)	clear_file(inode, FADVISE_HOT_BIT)
7441153db09SChao Yu 
74595ae251fSEric Biggers #define file_is_verity(inode)	is_file(inode, FADVISE_VERITY_BIT)
74695ae251fSEric Biggers #define file_set_verity(inode)	set_file(inode, FADVISE_VERITY_BIT)
747cde4de12SJaegeuk Kim 
748d4dd19ecSJaegeuk Kim #define file_should_truncate(inode)	is_file(inode, FADVISE_TRUNC_BIT)
749d4dd19ecSJaegeuk Kim #define file_need_truncate(inode)	set_file(inode, FADVISE_TRUNC_BIT)
750d4dd19ecSJaegeuk Kim #define file_dont_truncate(inode)	clear_file(inode, FADVISE_TRUNC_BIT)
751d4dd19ecSJaegeuk Kim 
752ab9fa662SJaegeuk Kim #define DEF_DIR_LEVEL		0
753ab9fa662SJaegeuk Kim 
7542ef79ecbSChao Yu enum {
7552ef79ecbSChao Yu 	GC_FAILURE_PIN,
7562ef79ecbSChao Yu 	MAX_GC_FAILURE
7572ef79ecbSChao Yu };
7582ef79ecbSChao Yu 
7597653b9d8SChao Yu /* used for f2fs_inode_info->flags */
7607653b9d8SChao Yu enum {
7617653b9d8SChao Yu 	FI_NEW_INODE,		/* indicate newly allocated inode */
7627653b9d8SChao Yu 	FI_DIRTY_INODE,		/* indicate inode is dirty or not */
7637653b9d8SChao Yu 	FI_AUTO_RECOVER,	/* indicate inode is recoverable */
7647653b9d8SChao Yu 	FI_DIRTY_DIR,		/* indicate directory has dirty pages */
7657653b9d8SChao Yu 	FI_INC_LINK,		/* need to increment i_nlink */
7667653b9d8SChao Yu 	FI_ACL_MODE,		/* indicate acl mode */
7677653b9d8SChao Yu 	FI_NO_ALLOC,		/* should not allocate any blocks */
7687653b9d8SChao Yu 	FI_FREE_NID,		/* free allocated nide */
7697653b9d8SChao Yu 	FI_NO_EXTENT,		/* not to use the extent cache */
7707653b9d8SChao Yu 	FI_INLINE_XATTR,	/* used for inline xattr */
7717653b9d8SChao Yu 	FI_INLINE_DATA,		/* used for inline data*/
7727653b9d8SChao Yu 	FI_INLINE_DENTRY,	/* used for inline dentry */
7737653b9d8SChao Yu 	FI_APPEND_WRITE,	/* inode has appended data */
7747653b9d8SChao Yu 	FI_UPDATE_WRITE,	/* inode has in-place-update data */
7757653b9d8SChao Yu 	FI_NEED_IPU,		/* used for ipu per file */
7767653b9d8SChao Yu 	FI_ATOMIC_FILE,		/* indicate atomic file */
7777653b9d8SChao Yu 	FI_FIRST_BLOCK_WRITTEN,	/* indicate #0 data block was written */
7787653b9d8SChao Yu 	FI_DROP_CACHE,		/* drop dirty page cache */
7797653b9d8SChao Yu 	FI_DATA_EXIST,		/* indicate data exists */
7807653b9d8SChao Yu 	FI_INLINE_DOTS,		/* indicate inline dot dentries */
7811018a546SChao Yu 	FI_SKIP_WRITES,		/* should skip data page writeback */
7821018a546SChao Yu 	FI_OPU_WRITE,		/* used for opu per file */
7837653b9d8SChao Yu 	FI_DIRTY_FILE,		/* indicate regular/symlink has dirty pages */
7843d697a4aSEric Biggers 	FI_PREALLOCATED_ALL,	/* all blocks for write were preallocated */
7857653b9d8SChao Yu 	FI_HOT_DATA,		/* indicate file is hot */
7867653b9d8SChao Yu 	FI_EXTRA_ATTR,		/* indicate file has extra attribute */
7877653b9d8SChao Yu 	FI_PROJ_INHERIT,	/* indicate file inherits projectid */
7887653b9d8SChao Yu 	FI_PIN_FILE,		/* indicate file should not be gced */
7897653b9d8SChao Yu 	FI_VERITY_IN_PROGRESS,	/* building fs-verity Merkle tree */
7907653b9d8SChao Yu 	FI_COMPRESSED_FILE,	/* indicate file's data can be compressed */
791b28f047bSChao Yu 	FI_COMPRESS_CORRUPT,	/* indicate compressed cluster is corrupted */
7927653b9d8SChao Yu 	FI_MMAP_FILE,		/* indicate file was mmapped */
793602a16d5SDaeho Jeong 	FI_ENABLE_COMPRESS,	/* enable compression in "user" compression mode */
794c6140415SJaegeuk Kim 	FI_COMPRESS_RELEASED,	/* compressed blocks were released */
795859fca6bSChao Yu 	FI_ALIGNED_WRITE,	/* enable aligned write */
7964a2c5b79SYe Bin 	FI_COW_FILE,		/* indicate COW file */
7974d8d45dfSDaeho Jeong 	FI_ATOMIC_COMMITTED,	/* indicate atomic commit completed except disk sync */
79841e8f85aSDaeho Jeong 	FI_ATOMIC_REPLACE,	/* indicate atomic replace */
7997653b9d8SChao Yu 	FI_MAX,			/* max flag, never be used */
8007653b9d8SChao Yu };
8017653b9d8SChao Yu 
80239a53e0cSJaegeuk Kim struct f2fs_inode_info {
80339a53e0cSJaegeuk Kim 	struct inode vfs_inode;		/* serve a vfs inode */
80439a53e0cSJaegeuk Kim 	unsigned long i_flags;		/* keep an inode flags for ioctl */
80539a53e0cSJaegeuk Kim 	unsigned char i_advise;		/* use to give file attribute hints */
80638431545SJaegeuk Kim 	unsigned char i_dir_level;	/* use for dentry level for large dir */
8071ad71a27SJaegeuk Kim 	unsigned int i_current_depth;	/* only for directory depth */
8082ef79ecbSChao Yu 	/* for gc failure statistic */
8092ef79ecbSChao Yu 	unsigned int i_gc_failures[MAX_GC_FAILURE];
8106666e6aaSJaegeuk Kim 	unsigned int i_pino;		/* parent inode number */
81139a53e0cSJaegeuk Kim 	umode_t i_acl_mode;		/* keep file acl mode temporarily */
81239a53e0cSJaegeuk Kim 
81339a53e0cSJaegeuk Kim 	/* Use below internally in f2fs*/
8147653b9d8SChao Yu 	unsigned long flags[BITS_TO_LONGS(FI_MAX)];	/* use to pass per-file flags */
815e4544b63STim Murray 	struct f2fs_rwsem i_sem;	/* protect fi info */
816204706c7SJaegeuk Kim 	atomic_t dirty_pages;		/* # of dirty pages */
81739a53e0cSJaegeuk Kim 	f2fs_hash_t chash;		/* hash value of given file name */
81839a53e0cSJaegeuk Kim 	unsigned int clevel;		/* maximum level of given file name */
81988c5c13aSJaegeuk Kim 	struct task_struct *task;	/* lookup and create consistency */
820b0af6d49SChao Yu 	struct task_struct *cp_task;	/* separate cp/wb IO stats*/
821d80afefbSChao Yu 	struct task_struct *wb_task;	/* indicate inode is in context of writeback */
82239a53e0cSJaegeuk Kim 	nid_t i_xattr_nid;		/* node id that contains xattrs */
82326de9b11SJaegeuk Kim 	loff_t	last_disk_size;		/* lastly written file size */
824c10c9820SChao Yu 	spinlock_t i_size_lock;		/* protect last_disk_size */
82588b88a66SJaegeuk Kim 
8260abd675eSChao Yu #ifdef CONFIG_QUOTA
8270abd675eSChao Yu 	struct dquot *i_dquot[MAXQUOTAS];
8280abd675eSChao Yu 
8290abd675eSChao Yu 	/* quota space reservation, managed internally by quota code */
8300abd675eSChao Yu 	qsize_t i_reserved_quota;
8310abd675eSChao Yu #endif
8320f18b462SJaegeuk Kim 	struct list_head dirty_list;	/* dirty list for dirs and files */
8330f18b462SJaegeuk Kim 	struct list_head gdirty_list;	/* linked in global dirty list */
8343db1de0eSDaeho Jeong 	struct task_struct *atomic_write_task;	/* store atomic write task */
835e7547dacSJaegeuk Kim 	struct extent_tree *extent_tree[NR_EXTENT_CACHES];
836e7547dacSJaegeuk Kim 					/* cached extent_tree entry */
8373db1de0eSDaeho Jeong 	struct inode *cow_inode;	/* copy-on-write inode for atomic write */
838b2532c69SChao Yu 
839b2532c69SChao Yu 	/* avoid racing between foreground op and gc */
840e4544b63STim Murray 	struct f2fs_rwsem i_gc_rwsem[2];
841e4544b63STim Murray 	struct f2fs_rwsem i_xattr_sem; /* avoid racing between reading and changing EAs */
842f2470371SChao Yu 
8437a2af766SChao Yu 	int i_extra_isize;		/* size of extra space located in i_addr */
8445c57132eSChao Yu 	kprojid_t i_projid;		/* id for project quota */
8456afc662eSChao Yu 	int i_inline_xattr_size;	/* inline xattr size */
84624b81dfcSArnd Bergmann 	struct timespec64 i_crtime;	/* inode creation time */
847c277991dSChao Yu 	struct timespec64 i_disk_time[3];/* inode disk times */
8484c8ff709SChao Yu 
8494c8ff709SChao Yu 	/* for file compress */
850c2759ebaSDaeho Jeong 	atomic_t i_compr_blocks;		/* # of compressed blocks */
8514c8ff709SChao Yu 	unsigned char i_compress_algorithm;	/* algorithm type */
8524c8ff709SChao Yu 	unsigned char i_log_cluster_size;	/* log of cluster size */
8533fde13f8SChao Yu 	unsigned char i_compress_level;		/* compress level (lz4hc,zstd) */
854b90e5086SChao Yu 	unsigned char i_compress_flag;		/* compress flag */
8554c8ff709SChao Yu 	unsigned int i_cluster_size;		/* cluster size */
856f8e2f32bSDaeho Jeong 
857f8e2f32bSDaeho Jeong 	unsigned int atomic_write_cnt;
8584d8d45dfSDaeho Jeong 	loff_t original_i_size;		/* original i_size before atomic write */
85939a53e0cSJaegeuk Kim };
86039a53e0cSJaegeuk Kim 
86112607c1bSJaegeuk Kim static inline void get_read_extent_info(struct extent_info *ext,
862bd933d4fSChao Yu 					struct f2fs_extent *i_ext)
86339a53e0cSJaegeuk Kim {
864bd933d4fSChao Yu 	ext->fofs = le32_to_cpu(i_ext->fofs);
865bd933d4fSChao Yu 	ext->blk = le32_to_cpu(i_ext->blk);
866bd933d4fSChao Yu 	ext->len = le32_to_cpu(i_ext->len);
86739a53e0cSJaegeuk Kim }
86839a53e0cSJaegeuk Kim 
86912607c1bSJaegeuk Kim static inline void set_raw_read_extent(struct extent_info *ext,
87039a53e0cSJaegeuk Kim 					struct f2fs_extent *i_ext)
87139a53e0cSJaegeuk Kim {
87239a53e0cSJaegeuk Kim 	i_ext->fofs = cpu_to_le32(ext->fofs);
8734d0b0bd4SChao Yu 	i_ext->blk = cpu_to_le32(ext->blk);
87439a53e0cSJaegeuk Kim 	i_ext->len = cpu_to_le32(ext->len);
87539a53e0cSJaegeuk Kim }
87639a53e0cSJaegeuk Kim 
877004b6862SChao Yu static inline bool __is_discard_mergeable(struct discard_info *back,
87835ec7d57SChao Yu 			struct discard_info *front, unsigned int max_len)
879004b6862SChao Yu {
8809a997188SJaegeuk Kim 	return (back->lstart + back->len == front->lstart) &&
88135ec7d57SChao Yu 		(back->len + front->len <= max_len);
882004b6862SChao Yu }
883004b6862SChao Yu 
884004b6862SChao Yu static inline bool __is_discard_back_mergeable(struct discard_info *cur,
88535ec7d57SChao Yu 			struct discard_info *back, unsigned int max_len)
886004b6862SChao Yu {
88735ec7d57SChao Yu 	return __is_discard_mergeable(back, cur, max_len);
888004b6862SChao Yu }
889004b6862SChao Yu 
890004b6862SChao Yu static inline bool __is_discard_front_mergeable(struct discard_info *cur,
89135ec7d57SChao Yu 			struct discard_info *front, unsigned int max_len)
892004b6862SChao Yu {
89335ec7d57SChao Yu 	return __is_discard_mergeable(cur, front, max_len);
894004b6862SChao Yu }
895004b6862SChao Yu 
8969a4ffdf5SChao Yu /*
8979a4ffdf5SChao Yu  * For free nid management
8989a4ffdf5SChao Yu  */
8999a4ffdf5SChao Yu enum nid_state {
9009a4ffdf5SChao Yu 	FREE_NID,		/* newly added to free nid list */
9019a4ffdf5SChao Yu 	PREALLOC_NID,		/* it is preallocated */
9029a4ffdf5SChao Yu 	MAX_NID_STATE,
903b8559dc2SChao Yu };
904b8559dc2SChao Yu 
905a95ba66aSJaegeuk Kim enum nat_state {
906a95ba66aSJaegeuk Kim 	TOTAL_NAT,
907a95ba66aSJaegeuk Kim 	DIRTY_NAT,
908a95ba66aSJaegeuk Kim 	RECLAIMABLE_NAT,
909a95ba66aSJaegeuk Kim 	MAX_NAT_STATE,
910a95ba66aSJaegeuk Kim };
911a95ba66aSJaegeuk Kim 
91239a53e0cSJaegeuk Kim struct f2fs_nm_info {
91339a53e0cSJaegeuk Kim 	block_t nat_blkaddr;		/* base disk address of NAT */
91439a53e0cSJaegeuk Kim 	nid_t max_nid;			/* maximum possible node ids */
91504d47e67SChao Yu 	nid_t available_nids;		/* # of available node ids */
91639a53e0cSJaegeuk Kim 	nid_t next_scan_nid;		/* the next nid to be scanned */
91747c8ebccSJaegeuk Kim 	nid_t max_rf_node_blocks;	/* max # of nodes for recovery */
918cdfc41c1SJaegeuk Kim 	unsigned int ram_thresh;	/* control the memory footprint */
919ea1a29a0SChao Yu 	unsigned int ra_nid_pages;	/* # of nid pages to be readaheaded */
9202304cb0cSChao Yu 	unsigned int dirty_nats_ratio;	/* control dirty nats ratio threshold */
92139a53e0cSJaegeuk Kim 
92239a53e0cSJaegeuk Kim 	/* NAT cache management */
92339a53e0cSJaegeuk Kim 	struct radix_tree_root nat_root;/* root of the nat entry cache */
924309cc2b6SJaegeuk Kim 	struct radix_tree_root nat_set_root;/* root of the nat set cache */
925e4544b63STim Murray 	struct f2fs_rwsem nat_tree_lock;	/* protect nat entry tree */
92639a53e0cSJaegeuk Kim 	struct list_head nat_entries;	/* cached nat entry list (clean) */
92722969158SChao Yu 	spinlock_t nat_list_lock;	/* protect clean nat entry list */
928a95ba66aSJaegeuk Kim 	unsigned int nat_cnt[MAX_NAT_STATE]; /* the # of cached nat entries */
92922ad0b6aSJaegeuk Kim 	unsigned int nat_blocks;	/* # of nat blocks */
93039a53e0cSJaegeuk Kim 
93139a53e0cSJaegeuk Kim 	/* free node ids management */
9328a7ed66aSJaegeuk Kim 	struct radix_tree_root free_nid_root;/* root of the free_nid cache */
9339a4ffdf5SChao Yu 	struct list_head free_nid_list;		/* list for free nids excluding preallocated nids */
9349a4ffdf5SChao Yu 	unsigned int nid_cnt[MAX_NID_STATE];	/* the number of free node id */
935b8559dc2SChao Yu 	spinlock_t nid_list_lock;	/* protect nid lists ops */
93639a53e0cSJaegeuk Kim 	struct mutex build_lock;	/* lock for build free nids */
937bb1105e4SJaegeuk Kim 	unsigned char **free_nid_bitmap;
9384ac91242SChao Yu 	unsigned char *nat_block_bitmap;
939586d1492SChao Yu 	unsigned short *free_nid_count;	/* free nid count of NAT block */
94039a53e0cSJaegeuk Kim 
94139a53e0cSJaegeuk Kim 	/* for checkpoint */
94239a53e0cSJaegeuk Kim 	char *nat_bitmap;		/* NAT bitmap pointer */
94322ad0b6aSJaegeuk Kim 
94422ad0b6aSJaegeuk Kim 	unsigned int nat_bits_blocks;	/* # of nat bits blocks */
94522ad0b6aSJaegeuk Kim 	unsigned char *nat_bits;	/* NAT bits blocks */
94622ad0b6aSJaegeuk Kim 	unsigned char *full_nat_bits;	/* full NAT pages */
94722ad0b6aSJaegeuk Kim 	unsigned char *empty_nat_bits;	/* empty NAT pages */
948599a09b2SChao Yu #ifdef CONFIG_F2FS_CHECK_FS
949599a09b2SChao Yu 	char *nat_bitmap_mir;		/* NAT bitmap mirror */
950599a09b2SChao Yu #endif
95139a53e0cSJaegeuk Kim 	int bitmap_size;		/* bitmap size */
95239a53e0cSJaegeuk Kim };
95339a53e0cSJaegeuk Kim 
95439a53e0cSJaegeuk Kim /*
95539a53e0cSJaegeuk Kim  * this structure is used as one of function parameters.
95639a53e0cSJaegeuk Kim  * all the information are dedicated to a given direct node block determined
95739a53e0cSJaegeuk Kim  * by the data offset in a file.
95839a53e0cSJaegeuk Kim  */
95939a53e0cSJaegeuk Kim struct dnode_of_data {
96039a53e0cSJaegeuk Kim 	struct inode *inode;		/* vfs inode pointer */
96139a53e0cSJaegeuk Kim 	struct page *inode_page;	/* its inode page, NULL is possible */
96239a53e0cSJaegeuk Kim 	struct page *node_page;		/* cached direct node page */
96339a53e0cSJaegeuk Kim 	nid_t nid;			/* node id of the direct node block */
96439a53e0cSJaegeuk Kim 	unsigned int ofs_in_node;	/* data offset in the node page */
96539a53e0cSJaegeuk Kim 	bool inode_page_locked;		/* inode page is locked or not */
96693bae099SJaegeuk Kim 	bool node_changed;		/* is node block changed */
9673cf45747SChao Yu 	char cur_level;			/* level of hole node page */
9683cf45747SChao Yu 	char max_level;			/* level of current page located */
96939a53e0cSJaegeuk Kim 	block_t	data_blkaddr;		/* block address of the node block */
97039a53e0cSJaegeuk Kim };
97139a53e0cSJaegeuk Kim 
97239a53e0cSJaegeuk Kim static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
97339a53e0cSJaegeuk Kim 		struct page *ipage, struct page *npage, nid_t nid)
97439a53e0cSJaegeuk Kim {
975d66d1f76SJaegeuk Kim 	memset(dn, 0, sizeof(*dn));
97639a53e0cSJaegeuk Kim 	dn->inode = inode;
97739a53e0cSJaegeuk Kim 	dn->inode_page = ipage;
97839a53e0cSJaegeuk Kim 	dn->node_page = npage;
97939a53e0cSJaegeuk Kim 	dn->nid = nid;
98039a53e0cSJaegeuk Kim }
98139a53e0cSJaegeuk Kim 
98239a53e0cSJaegeuk Kim /*
98339a53e0cSJaegeuk Kim  * For SIT manager
98439a53e0cSJaegeuk Kim  *
98539a53e0cSJaegeuk Kim  * By default, there are 6 active log areas across the whole main area.
98639a53e0cSJaegeuk Kim  * When considering hot and cold data separation to reduce cleaning overhead,
98739a53e0cSJaegeuk Kim  * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
98839a53e0cSJaegeuk Kim  * respectively.
98939a53e0cSJaegeuk Kim  * In the current design, you should not change the numbers intentionally.
99039a53e0cSJaegeuk Kim  * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
99139a53e0cSJaegeuk Kim  * logs individually according to the underlying devices. (default: 6)
99239a53e0cSJaegeuk Kim  * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
99339a53e0cSJaegeuk Kim  * data and 8 for node logs.
99439a53e0cSJaegeuk Kim  */
99539a53e0cSJaegeuk Kim #define	NR_CURSEG_DATA_TYPE	(3)
99639a53e0cSJaegeuk Kim #define NR_CURSEG_NODE_TYPE	(3)
997093749e2SChao Yu #define NR_CURSEG_INMEM_TYPE	(2)
998a7d9fe3cSJaegeuk Kim #define NR_CURSEG_RO_TYPE	(2)
999d0b9e42aSChao Yu #define NR_CURSEG_PERSIST_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
1000d0b9e42aSChao Yu #define NR_CURSEG_TYPE		(NR_CURSEG_INMEM_TYPE + NR_CURSEG_PERSIST_TYPE)
100139a53e0cSJaegeuk Kim 
100239a53e0cSJaegeuk Kim enum {
100339a53e0cSJaegeuk Kim 	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
100439a53e0cSJaegeuk Kim 	CURSEG_WARM_DATA,	/* data blocks */
100539a53e0cSJaegeuk Kim 	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
100639a53e0cSJaegeuk Kim 	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
100739a53e0cSJaegeuk Kim 	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
100839a53e0cSJaegeuk Kim 	CURSEG_COLD_NODE,	/* indirect node blocks */
1009d0b9e42aSChao Yu 	NR_PERSISTENT_LOG,	/* number of persistent log */
1010d0b9e42aSChao Yu 	CURSEG_COLD_DATA_PINNED = NR_PERSISTENT_LOG,
1011d0b9e42aSChao Yu 				/* pinned file that needs consecutive block address */
1012093749e2SChao Yu 	CURSEG_ALL_DATA_ATGC,	/* SSR alloctor in hot/warm/cold data area */
1013d0b9e42aSChao Yu 	NO_CHECK_TYPE,		/* number of persistent & inmem log */
101439a53e0cSJaegeuk Kim };
101539a53e0cSJaegeuk Kim 
10166b4afdd7SJaegeuk Kim struct flush_cmd {
10176b4afdd7SJaegeuk Kim 	struct completion wait;
1018721bd4d5SGu Zheng 	struct llist_node llnode;
101939d787beSChao Yu 	nid_t ino;
10206b4afdd7SJaegeuk Kim 	int ret;
10216b4afdd7SJaegeuk Kim };
10226b4afdd7SJaegeuk Kim 
1023a688b9d9SGu Zheng struct flush_cmd_control {
1024a688b9d9SGu Zheng 	struct task_struct *f2fs_issue_flush;	/* flush thread */
1025a688b9d9SGu Zheng 	wait_queue_head_t flush_wait_queue;	/* waiting queue for wake-up */
10268b8dd65fSChao Yu 	atomic_t issued_flush;			/* # of issued flushes */
102772691af6SJaegeuk Kim 	atomic_t queued_flush;			/* # of queued flushes */
1028721bd4d5SGu Zheng 	struct llist_head issue_list;		/* list for command issue */
1029721bd4d5SGu Zheng 	struct llist_node *dispatch_list;	/* list for command dispatch */
1030a688b9d9SGu Zheng };
1031a688b9d9SGu Zheng 
103239a53e0cSJaegeuk Kim struct f2fs_sm_info {
103339a53e0cSJaegeuk Kim 	struct sit_info *sit_info;		/* whole segment information */
103439a53e0cSJaegeuk Kim 	struct free_segmap_info *free_info;	/* free segment information */
103539a53e0cSJaegeuk Kim 	struct dirty_seglist_info *dirty_info;	/* dirty segment information */
103639a53e0cSJaegeuk Kim 	struct curseg_info *curseg_array;	/* active segment information */
103739a53e0cSJaegeuk Kim 
1038e4544b63STim Murray 	struct f2fs_rwsem curseg_lock;	/* for preventing curseg change */
10392b60311dSChao Yu 
104039a53e0cSJaegeuk Kim 	block_t seg0_blkaddr;		/* block address of 0'th segment */
104139a53e0cSJaegeuk Kim 	block_t main_blkaddr;		/* start block address of main area */
104239a53e0cSJaegeuk Kim 	block_t ssa_blkaddr;		/* start block address of SSA area */
104339a53e0cSJaegeuk Kim 
104439a53e0cSJaegeuk Kim 	unsigned int segment_count;	/* total # of segments */
104539a53e0cSJaegeuk Kim 	unsigned int main_segments;	/* # of segments in main area */
104639a53e0cSJaegeuk Kim 	unsigned int reserved_segments;	/* # of reserved segments */
1047300a8429SChao Yu 	unsigned int additional_reserved_segments;/* reserved segs for IO align feature */
104839a53e0cSJaegeuk Kim 	unsigned int ovp_segments;	/* # of overprovision segments */
104981eb8d6eSJaegeuk Kim 
105081eb8d6eSJaegeuk Kim 	/* a threshold to reclaim prefree segments */
105181eb8d6eSJaegeuk Kim 	unsigned int rec_prefree_segments;
10527fd9e544SJaegeuk Kim 
1053184a5cd2SChao Yu 	struct list_head sit_entry_set;	/* sit entry set list */
1054184a5cd2SChao Yu 
1055216fbd64SJaegeuk Kim 	unsigned int ipu_policy;	/* in-place-update policy */
1056216fbd64SJaegeuk Kim 	unsigned int min_ipu_util;	/* in-place-update threshold */
1057c1ce1b02SJaegeuk Kim 	unsigned int min_fsync_blocks;	/* threshold for fsync */
1058853137ceSJaegeuk Kim 	unsigned int min_seq_blocks;	/* threshold for sequential blocks */
1059ef095d19SJaegeuk Kim 	unsigned int min_hot_blocks;	/* threshold for hot block allocation */
1060a2a12b67SChao Yu 	unsigned int min_ssr_sections;	/* threshold to trigger SSR allocation */
10616b4afdd7SJaegeuk Kim 
10626b4afdd7SJaegeuk Kim 	/* for flush command control */
1063b01a9201SJaegeuk Kim 	struct flush_cmd_control *fcc_info;
1064a688b9d9SGu Zheng 
10650b54fb84SJaegeuk Kim 	/* for discard command control */
10660b54fb84SJaegeuk Kim 	struct discard_cmd_control *dcc_info;
106739a53e0cSJaegeuk Kim };
106839a53e0cSJaegeuk Kim 
106939a53e0cSJaegeuk Kim /*
107039a53e0cSJaegeuk Kim  * For superblock
107139a53e0cSJaegeuk Kim  */
107239a53e0cSJaegeuk Kim /*
107339a53e0cSJaegeuk Kim  * COUNT_TYPE for monitoring
107439a53e0cSJaegeuk Kim  *
107539a53e0cSJaegeuk Kim  * f2fs monitors the number of several block types such as on-writeback,
107639a53e0cSJaegeuk Kim  * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
107739a53e0cSJaegeuk Kim  */
107836951b38SChao Yu #define WB_DATA_TYPE(p)	(__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
107939a53e0cSJaegeuk Kim enum count_type {
108039a53e0cSJaegeuk Kim 	F2FS_DIRTY_DENTS,
1081c227f912SChao Yu 	F2FS_DIRTY_DATA,
10822c8a4a28SJaegeuk Kim 	F2FS_DIRTY_QDATA,
108339a53e0cSJaegeuk Kim 	F2FS_DIRTY_NODES,
108439a53e0cSJaegeuk Kim 	F2FS_DIRTY_META,
10850f18b462SJaegeuk Kim 	F2FS_DIRTY_IMETA,
108636951b38SChao Yu 	F2FS_WB_CP_DATA,
108736951b38SChao Yu 	F2FS_WB_DATA,
10885f9abab4SJaegeuk Kim 	F2FS_RD_DATA,
10895f9abab4SJaegeuk Kim 	F2FS_RD_NODE,
10905f9abab4SJaegeuk Kim 	F2FS_RD_META,
109102b16d0aSChao Yu 	F2FS_DIO_WRITE,
109202b16d0aSChao Yu 	F2FS_DIO_READ,
109339a53e0cSJaegeuk Kim 	NR_COUNT_TYPE,
109439a53e0cSJaegeuk Kim };
109539a53e0cSJaegeuk Kim 
109639a53e0cSJaegeuk Kim /*
1097e1c42045Sarter97  * The below are the page types of bios used in submit_bio().
109839a53e0cSJaegeuk Kim  * The available types are:
109939a53e0cSJaegeuk Kim  * DATA			User data pages. It operates as async mode.
110039a53e0cSJaegeuk Kim  * NODE			Node pages. It operates as async mode.
110139a53e0cSJaegeuk Kim  * META			FS metadata pages such as SIT, NAT, CP.
110239a53e0cSJaegeuk Kim  * NR_PAGE_TYPE		The number of page types.
110339a53e0cSJaegeuk Kim  * META_FLUSH		Make sure the previous pages are written
110439a53e0cSJaegeuk Kim  *			with waiting the bio's completion
110539a53e0cSJaegeuk Kim  * ...			Only can be used with META.
110639a53e0cSJaegeuk Kim  */
11077d5e5109SJaegeuk Kim #define PAGE_TYPE_OF_BIO(type)	((type) > META ? META : (type))
110839a53e0cSJaegeuk Kim enum page_type {
11096b8beca0SChao Yu 	DATA = 0,
11106b8beca0SChao Yu 	NODE = 1,	/* should not change this */
111139a53e0cSJaegeuk Kim 	META,
111239a53e0cSJaegeuk Kim 	NR_PAGE_TYPE,
111339a53e0cSJaegeuk Kim 	META_FLUSH,
11143db1de0eSDaeho Jeong 	IPU,		/* the below types are used by tracepoints only. */
11158ce67cb0SJaegeuk Kim 	OPU,
111639a53e0cSJaegeuk Kim };
111739a53e0cSJaegeuk Kim 
1118a912b54dSJaegeuk Kim enum temp_type {
1119a912b54dSJaegeuk Kim 	HOT = 0,	/* must be zero for meta bio */
1120a912b54dSJaegeuk Kim 	WARM,
1121a912b54dSJaegeuk Kim 	COLD,
1122a912b54dSJaegeuk Kim 	NR_TEMP_TYPE,
1123a912b54dSJaegeuk Kim };
1124a912b54dSJaegeuk Kim 
1125cc15620bSJaegeuk Kim enum need_lock_type {
1126cc15620bSJaegeuk Kim 	LOCK_REQ = 0,
1127cc15620bSJaegeuk Kim 	LOCK_DONE,
1128cc15620bSJaegeuk Kim 	LOCK_RETRY,
1129cc15620bSJaegeuk Kim };
1130cc15620bSJaegeuk Kim 
1131a5fd5050SChao Yu enum cp_reason_type {
1132a5fd5050SChao Yu 	CP_NO_NEEDED,
1133a5fd5050SChao Yu 	CP_NON_REGULAR,
11344c8ff709SChao Yu 	CP_COMPRESSED,
1135a5fd5050SChao Yu 	CP_HARDLINK,
1136a5fd5050SChao Yu 	CP_SB_NEED_CP,
1137a5fd5050SChao Yu 	CP_WRONG_PINO,
1138a5fd5050SChao Yu 	CP_NO_SPC_ROLL,
1139a5fd5050SChao Yu 	CP_NODE_NEED_CP,
1140a5fd5050SChao Yu 	CP_FASTBOOT_MODE,
1141a5fd5050SChao Yu 	CP_SPEC_LOG_NUM,
11420a007b97SJaegeuk Kim 	CP_RECOVER_DIR,
1143a5fd5050SChao Yu };
1144a5fd5050SChao Yu 
1145b0af6d49SChao Yu enum iostat_type {
11468b83ac81SChao Yu 	/* WRITE IO */
11478b83ac81SChao Yu 	APP_DIRECT_IO,			/* app direct write IOs */
11488b83ac81SChao Yu 	APP_BUFFERED_IO,		/* app buffered write IOs */
1149b0af6d49SChao Yu 	APP_WRITE_IO,			/* app write IOs */
1150b0af6d49SChao Yu 	APP_MAPPED_IO,			/* app mapped IOs */
115134a23525SChao Yu 	APP_BUFFERED_CDATA_IO,		/* app buffered write IOs on compressed file */
115234a23525SChao Yu 	APP_MAPPED_CDATA_IO,		/* app mapped write IOs on compressed file */
1153b0af6d49SChao Yu 	FS_DATA_IO,			/* data IOs from kworker/fsync/reclaimer */
115434a23525SChao Yu 	FS_CDATA_IO,			/* data IOs from kworker/fsync/reclaimer on compressed file */
1155b0af6d49SChao Yu 	FS_NODE_IO,			/* node IOs from kworker/fsync/reclaimer */
1156b0af6d49SChao Yu 	FS_META_IO,			/* meta IOs from kworker/reclaimer */
1157b0af6d49SChao Yu 	FS_GC_DATA_IO,			/* data IOs from forground gc */
1158b0af6d49SChao Yu 	FS_GC_NODE_IO,			/* node IOs from forground gc */
1159b0af6d49SChao Yu 	FS_CP_DATA_IO,			/* data IOs from checkpoint */
1160b0af6d49SChao Yu 	FS_CP_NODE_IO,			/* node IOs from checkpoint */
1161b0af6d49SChao Yu 	FS_CP_META_IO,			/* meta IOs from checkpoint */
11628b83ac81SChao Yu 
11638b83ac81SChao Yu 	/* READ IO */
11648b83ac81SChao Yu 	APP_DIRECT_READ_IO,		/* app direct read IOs */
11658b83ac81SChao Yu 	APP_BUFFERED_READ_IO,		/* app buffered read IOs */
11668b83ac81SChao Yu 	APP_READ_IO,			/* app read IOs */
11678b83ac81SChao Yu 	APP_MAPPED_READ_IO,		/* app mapped read IOs */
116834a23525SChao Yu 	APP_BUFFERED_CDATA_READ_IO,	/* app buffered read IOs on compressed file  */
116934a23525SChao Yu 	APP_MAPPED_CDATA_READ_IO,	/* app mapped read IOs on compressed file  */
11708b83ac81SChao Yu 	FS_DATA_READ_IO,		/* data read IOs */
11719c122384SChao Yu 	FS_GDATA_READ_IO,		/* data read IOs from background gc */
11729c122384SChao Yu 	FS_CDATA_READ_IO,		/* compressed data read IOs */
11738b83ac81SChao Yu 	FS_NODE_READ_IO,		/* node read IOs */
11748b83ac81SChao Yu 	FS_META_READ_IO,		/* meta read IOs */
11758b83ac81SChao Yu 
11768b83ac81SChao Yu 	/* other */
11777a2b15cfSYangtao Li 	FS_DISCARD_IO,			/* discard */
1178193a639fSYangtao Li 	FS_FLUSH_IO,			/* flush */
1179*25f90805SDaejun Park 	FS_ZONE_RESET_IO,		/* zone reset */
1180b0af6d49SChao Yu 	NR_IO_TYPE,
1181b0af6d49SChao Yu };
1182b0af6d49SChao Yu 
1183458e6197SJaegeuk Kim struct f2fs_io_info {
118405ca3632SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs_sb_info pointer */
118539d787beSChao Yu 	nid_t ino;		/* inode number */
1186458e6197SJaegeuk Kim 	enum page_type type;	/* contains DATA/NODE/META/META_FLUSH */
1187a912b54dSJaegeuk Kim 	enum temp_type temp;	/* contains HOT/WARM/COLD */
11887649c873SBart Van Assche 	enum req_op op;		/* contains REQ_OP_ */
11897649c873SBart Van Assche 	blk_opf_t op_flags;	/* req_flag_bits */
11907a9d7548SChao Yu 	block_t new_blkaddr;	/* new block address to be written */
119128bc106bSChao Yu 	block_t old_blkaddr;	/* old block address before Cow */
119205ca3632SJaegeuk Kim 	struct page *page;	/* page to be written */
11934375a336SJaegeuk Kim 	struct page *encrypted_page;	/* encrypted page */
11944c8ff709SChao Yu 	struct page *compressed_page;	/* compressed page */
1195fb830fc5SChao Yu 	struct list_head list;		/* serialize IOs */
11962eae077eSChao Yu 	unsigned int compr_blocks;	/* # of compressed block addresses */
11972eae077eSChao Yu 	unsigned int need_lock:8;	/* indicate we need to lock cp_rwsem */
11982eae077eSChao Yu 	unsigned int version:8;		/* version of the node */
11992eae077eSChao Yu 	unsigned int submitted:1;	/* indicate IO submission */
12002eae077eSChao Yu 	unsigned int in_list:1;		/* indicate fio is in io_list */
12012eae077eSChao Yu 	unsigned int is_por:1;		/* indicate IO is from recovery or not */
12022eae077eSChao Yu 	unsigned int retry:1;		/* need to reallocate block address */
12032eae077eSChao Yu 	unsigned int encrypted:1;	/* indicate file is encrypted */
12042eae077eSChao Yu 	unsigned int post_read:1;	/* require post read */
1205b0af6d49SChao Yu 	enum iostat_type io_type;	/* io type */
1206578c6478SYufen Yu 	struct writeback_control *io_wbc; /* writeback control */
12078648de2cSChao Yu 	struct bio **bio;		/* bio for ipu */
12088648de2cSChao Yu 	sector_t *last_block;		/* last block number in bio */
1209458e6197SJaegeuk Kim };
1210458e6197SJaegeuk Kim 
12110b20fcecSChao Yu struct bio_entry {
12120b20fcecSChao Yu 	struct bio *bio;
12130b20fcecSChao Yu 	struct list_head list;
12140b20fcecSChao Yu };
12150b20fcecSChao Yu 
121668afcf2dSTomohiro Kusumi #define is_read_io(rw) ((rw) == READ)
12171ff7bd3bSJaegeuk Kim struct f2fs_bio_info {
1218458e6197SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs superblock */
12191ff7bd3bSJaegeuk Kim 	struct bio *bio;		/* bios to merge */
12201ff7bd3bSJaegeuk Kim 	sector_t last_block_in_bio;	/* last block number */
1221458e6197SJaegeuk Kim 	struct f2fs_io_info fio;	/* store buffered io info. */
1222e067dc3cSDaeho Jeong #ifdef CONFIG_BLK_DEV_ZONED
1223e067dc3cSDaeho Jeong 	struct completion zone_wait;	/* condition value for the previous open zone to close */
1224e067dc3cSDaeho Jeong 	struct bio *zone_pending_bio;	/* pending bio for the previous zone */
1225e067dc3cSDaeho Jeong 	void *bi_private;		/* previous bi_private for pending bio */
1226e067dc3cSDaeho Jeong #endif
1227e4544b63STim Murray 	struct f2fs_rwsem io_rwsem;	/* blocking op for bio */
1228fb830fc5SChao Yu 	spinlock_t io_lock;		/* serialize DATA/NODE IOs */
1229fb830fc5SChao Yu 	struct list_head io_list;	/* track fios */
12300b20fcecSChao Yu 	struct list_head bio_list;	/* bio entry list head */
1231e4544b63STim Murray 	struct f2fs_rwsem bio_list_lock;	/* lock to protect bio entry list */
12321ff7bd3bSJaegeuk Kim };
12331ff7bd3bSJaegeuk Kim 
12343c62be17SJaegeuk Kim #define FDEV(i)				(sbi->devs[i])
12353c62be17SJaegeuk Kim #define RDEV(i)				(raw_super->devs[i])
12363c62be17SJaegeuk Kim struct f2fs_dev_info {
12373c62be17SJaegeuk Kim 	struct block_device *bdev;
12383c62be17SJaegeuk Kim 	char path[MAX_PATH_LEN];
12393c62be17SJaegeuk Kim 	unsigned int total_segments;
12403c62be17SJaegeuk Kim 	block_t start_blk;
12413c62be17SJaegeuk Kim 	block_t end_blk;
12423c62be17SJaegeuk Kim #ifdef CONFIG_BLK_DEV_ZONED
12433c62be17SJaegeuk Kim 	unsigned int nr_blkz;		/* Total number of zones */
124495175dafSDamien Le Moal 	unsigned long *blkz_seq;	/* Bitmap indicating sequential zones */
12453c62be17SJaegeuk Kim #endif
12463c62be17SJaegeuk Kim };
12473c62be17SJaegeuk Kim 
1248c227f912SChao Yu enum inode_type {
1249c227f912SChao Yu 	DIR_INODE,			/* for dirty dir inode */
1250c227f912SChao Yu 	FILE_INODE,			/* for dirty regular/symlink inode */
12510f18b462SJaegeuk Kim 	DIRTY_META,			/* for all dirtied inode metadata */
1252c227f912SChao Yu 	NR_INODE_TYPE,
1253c227f912SChao Yu };
1254c227f912SChao Yu 
125567298804SChao Yu /* for inner inode cache management */
125667298804SChao Yu struct inode_management {
125767298804SChao Yu 	struct radix_tree_root ino_root;	/* ino entry array */
125867298804SChao Yu 	spinlock_t ino_lock;			/* for ino entry lock */
125967298804SChao Yu 	struct list_head ino_list;		/* inode list head */
126067298804SChao Yu 	unsigned long ino_num;			/* number of entries */
126167298804SChao Yu };
126267298804SChao Yu 
1263093749e2SChao Yu /* for GC_AT */
1264093749e2SChao Yu struct atgc_management {
1265093749e2SChao Yu 	bool atgc_enabled;			/* ATGC is enabled or not */
1266093749e2SChao Yu 	struct rb_root_cached root;		/* root of victim rb-tree */
1267093749e2SChao Yu 	struct list_head victim_list;		/* linked with all victim entries */
1268093749e2SChao Yu 	unsigned int victim_count;		/* victim count in rb-tree */
1269093749e2SChao Yu 	unsigned int candidate_ratio;		/* candidate ratio */
1270093749e2SChao Yu 	unsigned int max_candidate_count;	/* max candidate count */
1271093749e2SChao Yu 	unsigned int age_weight;		/* age weight, vblock_weight = 100 - age_weight */
1272093749e2SChao Yu 	unsigned long long age_threshold;	/* age threshold */
1273093749e2SChao Yu };
1274093749e2SChao Yu 
1275d147ea4aSJaegeuk Kim struct f2fs_gc_control {
1276d147ea4aSJaegeuk Kim 	unsigned int victim_segno;	/* target victim segment number */
1277d147ea4aSJaegeuk Kim 	int init_gc_type;		/* FG_GC or BG_GC */
1278d147ea4aSJaegeuk Kim 	bool no_bg_gc;			/* check the space and stop bg_gc */
1279d147ea4aSJaegeuk Kim 	bool should_migrate_blocks;	/* should migrate blocks */
1280d147ea4aSJaegeuk Kim 	bool err_gc_skipped;		/* return EAGAIN if GC skipped */
1281c81d5baeSJaegeuk Kim 	unsigned int nr_free_secs;	/* # of free sections to do GC */
1282d147ea4aSJaegeuk Kim };
1283d147ea4aSJaegeuk Kim 
12845bb9c111SYangtao Li /*
12855bb9c111SYangtao Li  * For s_flag in struct f2fs_sb_info
12865bb9c111SYangtao Li  * Modification on enum should be synchronized with s_flag array
12875bb9c111SYangtao Li  */
1288caf0047eSChao Yu enum {
1289caf0047eSChao Yu 	SBI_IS_DIRTY,				/* dirty flag for checkpoint */
1290caf0047eSChao Yu 	SBI_IS_CLOSE,				/* specify unmounting */
1291caf0047eSChao Yu 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
1292caf0047eSChao Yu 	SBI_POR_DOING,				/* recovery is doing or not */
1293df728b0fSJaegeuk Kim 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
1294bbf156f7SJaegeuk Kim 	SBI_NEED_CP,				/* need to checkpoint */
129583a3bfdbSJaegeuk Kim 	SBI_IS_SHUTDOWN,			/* shutdown by ioctl */
12961378752bSChao Yu 	SBI_IS_RECOVERED,			/* recovered orphan/data */
12974354994fSDaniel Rosenberg 	SBI_CP_DISABLED,			/* CP was disabled last mount */
1298db610a64SJaegeuk Kim 	SBI_CP_DISABLED_QUICK,			/* CP was disabled quickly */
1299af033b2aSChao Yu 	SBI_QUOTA_NEED_FLUSH,			/* need to flush quota info in CP */
1300af033b2aSChao Yu 	SBI_QUOTA_SKIP_FLUSH,			/* skip flushing quota in current CP */
1301af033b2aSChao Yu 	SBI_QUOTA_NEED_REPAIR,			/* quota file may be corrupted */
130204f0b2eaSQiuyang Sun 	SBI_IS_RESIZEFS,			/* resizefs is in process */
1303ba900534SJaegeuk Kim 	SBI_IS_FREEZING,			/* freezefs is in process */
1304e1bb7d3dSChao Yu 	SBI_IS_WRITABLE,			/* remove ro mountoption transiently */
13055bb9c111SYangtao Li 	MAX_SBI_FLAG,
1306caf0047eSChao Yu };
1307caf0047eSChao Yu 
13086beceb54SJaegeuk Kim enum {
13096beceb54SJaegeuk Kim 	CP_TIME,
1310d0239e1bSJaegeuk Kim 	REQ_TIME,
1311a7d10cf3SSahitya Tummala 	DISCARD_TIME,
1312a7d10cf3SSahitya Tummala 	GC_TIME,
13134354994fSDaniel Rosenberg 	DISABLE_TIME,
131403f2c02dSJaegeuk Kim 	UMOUNT_DISCARD_TIMEOUT,
13156beceb54SJaegeuk Kim 	MAX_TIME,
13166beceb54SJaegeuk Kim };
13176beceb54SJaegeuk Kim 
1318a3951cd1SYangtao Li /* Note that you need to keep synchronization with this gc_mode_names array */
13190cdd3195SHyunchul Lee enum {
13205b0e9539SJaegeuk Kim 	GC_NORMAL,
13215b0e9539SJaegeuk Kim 	GC_IDLE_CB,
13225b0e9539SJaegeuk Kim 	GC_IDLE_GREEDY,
1323093749e2SChao Yu 	GC_IDLE_AT,
13240e5e8111SDaeho Jeong 	GC_URGENT_HIGH,
13250e5e8111SDaeho Jeong 	GC_URGENT_LOW,
1326d98af5f4SDaeho Jeong 	GC_URGENT_MID,
132707c6b593SDaeho Jeong 	MAX_GC_MODE,
13285b0e9539SJaegeuk Kim };
13295b0e9539SJaegeuk Kim 
13305b0e9539SJaegeuk Kim enum {
1331bbbc34fdSChao Yu 	BGGC_MODE_ON,		/* background gc is on */
1332bbbc34fdSChao Yu 	BGGC_MODE_OFF,		/* background gc is off */
1333bbbc34fdSChao Yu 	BGGC_MODE_SYNC,		/*
1334bbbc34fdSChao Yu 				 * background gc is on, migrating blocks
1335bbbc34fdSChao Yu 				 * like foreground gc
1336bbbc34fdSChao Yu 				 */
1337bbbc34fdSChao Yu };
1338bbbc34fdSChao Yu 
1339bbbc34fdSChao Yu enum {
1340b0332a0fSChao Yu 	FS_MODE_ADAPTIVE,		/* use both lfs/ssr allocation */
1341b0332a0fSChao Yu 	FS_MODE_LFS,			/* use lfs allocation only */
13426691d940SDaeho Jeong 	FS_MODE_FRAGMENT_SEG,		/* segment fragmentation mode */
13436691d940SDaeho Jeong 	FS_MODE_FRAGMENT_BLK,		/* block fragmentation mode */
1344b0332a0fSChao Yu };
1345b0332a0fSChao Yu 
1346b0332a0fSChao Yu enum {
134707939627SJaegeuk Kim 	ALLOC_MODE_DEFAULT,	/* stay default */
134807939627SJaegeuk Kim 	ALLOC_MODE_REUSE,	/* reuse segments as much as possible */
134907939627SJaegeuk Kim };
135007939627SJaegeuk Kim 
135193cf93f1SJunling Zheng enum fsync_mode {
135293cf93f1SJunling Zheng 	FSYNC_MODE_POSIX,	/* fsync follows posix semantics */
135393cf93f1SJunling Zheng 	FSYNC_MODE_STRICT,	/* fsync behaves in line with ext4 */
1354d6290814SJaegeuk Kim 	FSYNC_MODE_NOBARRIER,	/* fsync behaves nobarrier based on posix */
135593cf93f1SJunling Zheng };
135693cf93f1SJunling Zheng 
1357602a16d5SDaeho Jeong enum {
1358602a16d5SDaeho Jeong 	COMPR_MODE_FS,		/*
1359602a16d5SDaeho Jeong 				 * automatically compress compression
1360602a16d5SDaeho Jeong 				 * enabled files
1361602a16d5SDaeho Jeong 				 */
1362602a16d5SDaeho Jeong 	COMPR_MODE_USER,	/*
1363602a16d5SDaeho Jeong 				 * automatical compression is disabled.
1364602a16d5SDaeho Jeong 				 * user can control the file compression
1365602a16d5SDaeho Jeong 				 * using ioctls
1366602a16d5SDaeho Jeong 				 */
1367602a16d5SDaeho Jeong };
1368602a16d5SDaeho Jeong 
13694f993264SChao Yu enum {
13704f993264SChao Yu 	DISCARD_UNIT_BLOCK,	/* basic discard unit is block */
13714f993264SChao Yu 	DISCARD_UNIT_SEGMENT,	/* basic discard unit is segment */
13724f993264SChao Yu 	DISCARD_UNIT_SECTION,	/* basic discard unit is section */
13734f993264SChao Yu };
13744f993264SChao Yu 
13757a8fc586SDaeho Jeong enum {
13767a8fc586SDaeho Jeong 	MEMORY_MODE_NORMAL,	/* memory mode for normal devices */
13777a8fc586SDaeho Jeong 	MEMORY_MODE_LOW,	/* memory mode for low memry devices */
13787a8fc586SDaeho Jeong };
13797a8fc586SDaeho Jeong 
1380b62e71beSChao Yu enum errors_option {
1381b62e71beSChao Yu 	MOUNT_ERRORS_READONLY,	/* remount fs ro on errors */
1382b62e71beSChao Yu 	MOUNT_ERRORS_CONTINUE,	/* continue on errors */
1383b62e71beSChao Yu 	MOUNT_ERRORS_PANIC,	/* panic on errors */
1384b62e71beSChao Yu };
1385b62e71beSChao Yu 
1386b763f3beSChao Yu static inline int f2fs_test_bit(unsigned int nr, char *addr);
1387b763f3beSChao Yu static inline void f2fs_set_bit(unsigned int nr, char *addr);
1388b763f3beSChao Yu static inline void f2fs_clear_bit(unsigned int nr, char *addr);
13894c8ff709SChao Yu 
1390b763f3beSChao Yu /*
1391b763f3beSChao Yu  * Layout of f2fs page.private:
1392b763f3beSChao Yu  *
1393b763f3beSChao Yu  * Layout A: lowest bit should be 1
1394b763f3beSChao Yu  * | bit0 = 1 | bit1 | bit2 | ... | bit MAX | private data .... |
1395b763f3beSChao Yu  * bit 0	PAGE_PRIVATE_NOT_POINTER
1396b3107b38SChao Yu  * bit 1	PAGE_PRIVATE_DUMMY_WRITE
1397b3107b38SChao Yu  * bit 2	PAGE_PRIVATE_ONGOING_MIGRATION
1398b3107b38SChao Yu  * bit 3	PAGE_PRIVATE_INLINE_INODE
1399b3107b38SChao Yu  * bit 4	PAGE_PRIVATE_REF_RESOURCE
1400b3107b38SChao Yu  * bit 5-	f2fs private data
1401b763f3beSChao Yu  *
1402b763f3beSChao Yu  * Layout B: lowest bit should be 0
1403b763f3beSChao Yu  * page.private is a wrapped pointer.
1404b763f3beSChao Yu  */
1405b763f3beSChao Yu enum {
1406b763f3beSChao Yu 	PAGE_PRIVATE_NOT_POINTER,		/* private contains non-pointer data */
1407b763f3beSChao Yu 	PAGE_PRIVATE_DUMMY_WRITE,		/* data page for padding aligned IO */
1408b763f3beSChao Yu 	PAGE_PRIVATE_ONGOING_MIGRATION,		/* data page which is on-going migrating */
1409b763f3beSChao Yu 	PAGE_PRIVATE_INLINE_INODE,		/* inode page contains inline data */
1410b763f3beSChao Yu 	PAGE_PRIVATE_REF_RESOURCE,		/* dirty page has referenced resources */
1411b763f3beSChao Yu 	PAGE_PRIVATE_MAX
1412b763f3beSChao Yu };
1413b763f3beSChao Yu 
14144c8ff709SChao Yu /* For compression */
14154c8ff709SChao Yu enum compress_algorithm_type {
14164c8ff709SChao Yu 	COMPRESS_LZO,
14174c8ff709SChao Yu 	COMPRESS_LZ4,
141850cfa66fSChao Yu 	COMPRESS_ZSTD,
14196d92b201SChao Yu 	COMPRESS_LZORLE,
14204c8ff709SChao Yu 	COMPRESS_MAX,
14214c8ff709SChao Yu };
14224c8ff709SChao Yu 
1423b28f047bSChao Yu enum compress_flag {
1424b28f047bSChao Yu 	COMPRESS_CHKSUM,
1425b28f047bSChao Yu 	COMPRESS_MAX_FLAG,
1426b28f047bSChao Yu };
1427b28f047bSChao Yu 
14286ce19affSChao Yu #define	COMPRESS_WATERMARK			20
14296ce19affSChao Yu #define	COMPRESS_PERCENT			20
14306ce19affSChao Yu 
1431b28f047bSChao Yu #define COMPRESS_DATA_RESERVED_SIZE		4
14324c8ff709SChao Yu struct compress_data {
14334c8ff709SChao Yu 	__le32 clen;			/* compressed data size */
1434b28f047bSChao Yu 	__le32 chksum;			/* compressed data chksum */
14354c8ff709SChao Yu 	__le32 reserved[COMPRESS_DATA_RESERVED_SIZE];	/* reserved */
14364c8ff709SChao Yu 	u8 cdata[];			/* compressed data */
14374c8ff709SChao Yu };
14384c8ff709SChao Yu 
14394c8ff709SChao Yu #define COMPRESS_HEADER_SIZE	(sizeof(struct compress_data))
14404c8ff709SChao Yu 
14414c8ff709SChao Yu #define F2FS_COMPRESSED_PAGE_MAGIC	0xF5F2C000
14424c8ff709SChao Yu 
14433fde13f8SChao Yu #define	COMPRESS_LEVEL_OFFSET	8
14443fde13f8SChao Yu 
14454c8ff709SChao Yu /* compress context */
14464c8ff709SChao Yu struct compress_ctx {
14474c8ff709SChao Yu 	struct inode *inode;		/* inode the context belong to */
14484c8ff709SChao Yu 	pgoff_t cluster_idx;		/* cluster index number */
14494c8ff709SChao Yu 	unsigned int cluster_size;	/* page count in cluster */
14504c8ff709SChao Yu 	unsigned int log_cluster_size;	/* log of cluster size */
14514c8ff709SChao Yu 	struct page **rpages;		/* pages store raw data in cluster */
14524c8ff709SChao Yu 	unsigned int nr_rpages;		/* total page number in rpages */
14534c8ff709SChao Yu 	struct page **cpages;		/* pages store compressed data in cluster */
14544c8ff709SChao Yu 	unsigned int nr_cpages;		/* total page number in cpages */
14553271d7ebSFengnan Chang 	unsigned int valid_nr_cpages;	/* valid page number in cpages */
14564c8ff709SChao Yu 	void *rbuf;			/* virtual mapped address on rpages */
14574c8ff709SChao Yu 	struct compress_data *cbuf;	/* virtual mapped address on cpages */
14584c8ff709SChao Yu 	size_t rlen;			/* valid data length in rbuf */
14594c8ff709SChao Yu 	size_t clen;			/* valid data length in cbuf */
14604c8ff709SChao Yu 	void *private;			/* payload buffer for specified compression algorithm */
146150cfa66fSChao Yu 	void *private2;			/* extra payload buffer */
14624c8ff709SChao Yu };
14634c8ff709SChao Yu 
14644c8ff709SChao Yu /* compress context for write IO path */
14654c8ff709SChao Yu struct compress_io_ctx {
14664c8ff709SChao Yu 	u32 magic;			/* magic number to indicate page is compressed */
14674c8ff709SChao Yu 	struct inode *inode;		/* inode the context belong to */
14684c8ff709SChao Yu 	struct page **rpages;		/* pages store raw data in cluster */
14694c8ff709SChao Yu 	unsigned int nr_rpages;		/* total page number in rpages */
1470e6c3948dSChao Yu 	atomic_t pending_pages;		/* in-flight compressed page count */
14714c8ff709SChao Yu };
14724c8ff709SChao Yu 
14737f59b277SEric Biggers /* Context for decompressing one cluster on the read IO path */
14744c8ff709SChao Yu struct decompress_io_ctx {
14754c8ff709SChao Yu 	u32 magic;			/* magic number to indicate page is compressed */
14764c8ff709SChao Yu 	struct inode *inode;		/* inode the context belong to */
14774c8ff709SChao Yu 	pgoff_t cluster_idx;		/* cluster index number */
14784c8ff709SChao Yu 	unsigned int cluster_size;	/* page count in cluster */
14794c8ff709SChao Yu 	unsigned int log_cluster_size;	/* log of cluster size */
14804c8ff709SChao Yu 	struct page **rpages;		/* pages store raw data in cluster */
14814c8ff709SChao Yu 	unsigned int nr_rpages;		/* total page number in rpages */
14824c8ff709SChao Yu 	struct page **cpages;		/* pages store compressed data in cluster */
14834c8ff709SChao Yu 	unsigned int nr_cpages;		/* total page number in cpages */
14844c8ff709SChao Yu 	struct page **tpages;		/* temp pages to pad holes in cluster */
14854c8ff709SChao Yu 	void *rbuf;			/* virtual mapped address on rpages */
14864c8ff709SChao Yu 	struct compress_data *cbuf;	/* virtual mapped address on cpages */
14874c8ff709SChao Yu 	size_t rlen;			/* valid data length in rbuf */
14884c8ff709SChao Yu 	size_t clen;			/* valid data length in cbuf */
14897f59b277SEric Biggers 
14907f59b277SEric Biggers 	/*
14917f59b277SEric Biggers 	 * The number of compressed pages remaining to be read in this cluster.
14927f59b277SEric Biggers 	 * This is initially nr_cpages.  It is decremented by 1 each time a page
14937f59b277SEric Biggers 	 * has been read (or failed to be read).  When it reaches 0, the cluster
14947f59b277SEric Biggers 	 * is decompressed (or an error is reported).
14957f59b277SEric Biggers 	 *
14967f59b277SEric Biggers 	 * If an error occurs before all the pages have been submitted for I/O,
14977f59b277SEric Biggers 	 * then this will never reach 0.  In this case the I/O submitter is
14987f59b277SEric Biggers 	 * responsible for calling f2fs_decompress_end_io() instead.
14997f59b277SEric Biggers 	 */
15007f59b277SEric Biggers 	atomic_t remaining_pages;
15017f59b277SEric Biggers 
15027f59b277SEric Biggers 	/*
15037f59b277SEric Biggers 	 * Number of references to this decompress_io_ctx.
15047f59b277SEric Biggers 	 *
15057f59b277SEric Biggers 	 * One reference is held for I/O completion.  This reference is dropped
15067f59b277SEric Biggers 	 * after the pagecache pages are updated and unlocked -- either after
15077f59b277SEric Biggers 	 * decompression (and verity if enabled), or after an error.
15087f59b277SEric Biggers 	 *
15097f59b277SEric Biggers 	 * In addition, each compressed page holds a reference while it is in a
15107f59b277SEric Biggers 	 * bio.  These references are necessary prevent compressed pages from
15117f59b277SEric Biggers 	 * being freed while they are still in a bio.
15127f59b277SEric Biggers 	 */
15137f59b277SEric Biggers 	refcount_t refcnt;
15147f59b277SEric Biggers 
15157f59b277SEric Biggers 	bool failed;			/* IO error occurred before decompression? */
15167f59b277SEric Biggers 	bool need_verity;		/* need fs-verity verification after decompression? */
151750cfa66fSChao Yu 	void *private;			/* payload buffer for specified decompression algorithm */
151850cfa66fSChao Yu 	void *private2;			/* extra payload buffer */
15197f59b277SEric Biggers 	struct work_struct verity_work;	/* work to verify the decompressed pages */
1520bff139b4SDaeho Jeong 	struct work_struct free_work;	/* work for late free this structure itself */
15214c8ff709SChao Yu };
15224c8ff709SChao Yu 
15234c8ff709SChao Yu #define NULL_CLUSTER			((unsigned int)(~0))
15244c8ff709SChao Yu #define MIN_COMPRESS_LOG_SIZE		2
15254c8ff709SChao Yu #define MAX_COMPRESS_LOG_SIZE		8
15260e2b7385SChao Yu #define MAX_COMPRESS_WINDOW_SIZE(log_size)	((PAGE_SIZE) << (log_size))
15274c8ff709SChao Yu 
152839a53e0cSJaegeuk Kim struct f2fs_sb_info {
152939a53e0cSJaegeuk Kim 	struct super_block *sb;			/* pointer to VFS super block */
15305e176d54SJaegeuk Kim 	struct proc_dir_entry *s_proc;		/* proc entry */
153139a53e0cSJaegeuk Kim 	struct f2fs_super_block *raw_super;	/* raw super block pointer */
1532e4544b63STim Murray 	struct f2fs_rwsem sb_lock;		/* lock for raw super block */
1533e8240f65SChao Yu 	int valid_super_block;			/* valid super block no */
1534fadb2fb8SChao Yu 	unsigned long s_flag;				/* flags for sbi */
1535853137ceSJaegeuk Kim 	struct mutex writepages;		/* mutex for writepages() */
153639a53e0cSJaegeuk Kim 
1537178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
1538178053e2SDamien Le Moal 	unsigned int blocks_per_blkz;		/* F2FS blocks per zone */
1539178053e2SDamien Le Moal #endif
1540178053e2SDamien Le Moal 
154139a53e0cSJaegeuk Kim 	/* for node-related operations */
154239a53e0cSJaegeuk Kim 	struct f2fs_nm_info *nm_info;		/* node manager */
154339a53e0cSJaegeuk Kim 	struct inode *node_inode;		/* cache node blocks */
154439a53e0cSJaegeuk Kim 
154539a53e0cSJaegeuk Kim 	/* for segment-related operations */
154639a53e0cSJaegeuk Kim 	struct f2fs_sm_info *sm_info;		/* segment manager */
15471ff7bd3bSJaegeuk Kim 
15481ff7bd3bSJaegeuk Kim 	/* for bio operations */
1549a912b54dSJaegeuk Kim 	struct f2fs_bio_info *write_io[NR_PAGE_TYPE];	/* for write bios */
1550107a805dSChao Yu 	/* keep migration IO order for LFS mode */
1551e4544b63STim Murray 	struct f2fs_rwsem io_order_lock;
15520a595ebaSJaegeuk Kim 	mempool_t *write_io_dummy;		/* Dummy pages */
1553a7b8618aSJaegeuk Kim 	pgoff_t page_eio_ofs[NR_PAGE_TYPE];	/* EIO page offset */
1554a7b8618aSJaegeuk Kim 	int page_eio_cnt[NR_PAGE_TYPE];		/* EIO count */
155539a53e0cSJaegeuk Kim 
155639a53e0cSJaegeuk Kim 	/* for checkpoint */
155739a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt;		/* raw checkpoint pointer */
15588508e44aSJaegeuk Kim 	int cur_cp_pack;			/* remain current cp pack */
1559aaec2b1dSChao Yu 	spinlock_t cp_lock;			/* for flag in ckpt */
156039a53e0cSJaegeuk Kim 	struct inode *meta_inode;		/* cache meta blocks */
1561e4544b63STim Murray 	struct f2fs_rwsem cp_global_sem;	/* checkpoint procedure lock */
1562e4544b63STim Murray 	struct f2fs_rwsem cp_rwsem;		/* blocking FS operations */
1563e4544b63STim Murray 	struct f2fs_rwsem node_write;		/* locking node writes */
1564e4544b63STim Murray 	struct f2fs_rwsem node_change;	/* locking node change */
1565fb51b5efSChangman Lee 	wait_queue_head_t cp_wait;
15666beceb54SJaegeuk Kim 	unsigned long last_time[MAX_TIME];	/* to store time in jiffies */
15676beceb54SJaegeuk Kim 	long interval_time[MAX_TIME];		/* to store thresholds */
1568261eeb9cSDaeho Jeong 	struct ckpt_req_control cprc_info;	/* for checkpoint request control */
156939a53e0cSJaegeuk Kim 
157067298804SChao Yu 	struct inode_management im[MAX_INO_ENTRY];	/* manage inode cache */
15716451e041SJaegeuk Kim 
157250fa53ecSChao Yu 	spinlock_t fsync_node_lock;		/* for node entry lock */
157350fa53ecSChao Yu 	struct list_head fsync_node_list;	/* node list head */
157450fa53ecSChao Yu 	unsigned int fsync_seg_id;		/* sequence id */
157550fa53ecSChao Yu 	unsigned int fsync_node_num;		/* number of node entries */
157650fa53ecSChao Yu 
15776451e041SJaegeuk Kim 	/* for orphan inode, use 0'th array */
15780d47c1adSGu Zheng 	unsigned int max_orphans;		/* max orphan inodes */
157939a53e0cSJaegeuk Kim 
1580c227f912SChao Yu 	/* for inode management */
1581c227f912SChao Yu 	struct list_head inode_list[NR_INODE_TYPE];	/* dirty inode list */
1582c227f912SChao Yu 	spinlock_t inode_lock[NR_INODE_TYPE];	/* for dirty inode list lock */
1583040d2bb3SChao Yu 	struct mutex flush_lock;		/* for flush exclusion */
158439a53e0cSJaegeuk Kim 
158513054c54SChao Yu 	/* for extent tree cache */
1586e7547dacSJaegeuk Kim 	struct extent_tree_info extent_tree[NR_EXTENT_CACHES];
158771644dffSJaegeuk Kim 	atomic64_t allocated_data_blocks;	/* for block age extent_cache */
158871644dffSJaegeuk Kim 
158971644dffSJaegeuk Kim 	/* The threshold used for hot and warm data seperation*/
159071644dffSJaegeuk Kim 	unsigned int hot_data_age_threshold;
159171644dffSJaegeuk Kim 	unsigned int warm_data_age_threshold;
1592d23be468Sqixiaoyu1 	unsigned int last_age_weight;
159313054c54SChao Yu 
159439a53e0cSJaegeuk Kim 	/* basic filesystem units */
159539a53e0cSJaegeuk Kim 	unsigned int log_sectors_per_block;	/* log2 sectors per block */
159639a53e0cSJaegeuk Kim 	unsigned int log_blocksize;		/* log2 block size */
159739a53e0cSJaegeuk Kim 	unsigned int blocksize;			/* block size */
159839a53e0cSJaegeuk Kim 	unsigned int root_ino_num;		/* root inode number*/
159939a53e0cSJaegeuk Kim 	unsigned int node_ino_num;		/* node inode number*/
160039a53e0cSJaegeuk Kim 	unsigned int meta_ino_num;		/* meta inode number*/
160139a53e0cSJaegeuk Kim 	unsigned int log_blocks_per_seg;	/* log2 blocks per segment */
160239a53e0cSJaegeuk Kim 	unsigned int blocks_per_seg;		/* blocks per segment */
1603b771aadcSJaegeuk Kim 	unsigned int unusable_blocks_per_sec;	/* unusable blocks per section */
160439a53e0cSJaegeuk Kim 	unsigned int segs_per_sec;		/* segments per section */
160539a53e0cSJaegeuk Kim 	unsigned int secs_per_zone;		/* sections per zone */
160639a53e0cSJaegeuk Kim 	unsigned int total_sections;		/* total section count */
160739a53e0cSJaegeuk Kim 	unsigned int total_node_count;		/* total node block count */
160839a53e0cSJaegeuk Kim 	unsigned int total_valid_node_count;	/* valid node block count */
1609ab9fa662SJaegeuk Kim 	int dir_level;				/* directory level */
161066aee5aaSYuwei Guan 	bool readdir_ra;			/* readahead inode in readdir */
161110208567SJaegeuk Kim 	u64 max_io_bytes;			/* max io bytes to merge IOs */
161239a53e0cSJaegeuk Kim 
161339a53e0cSJaegeuk Kim 	block_t user_block_count;		/* # of user blocks */
161439a53e0cSJaegeuk Kim 	block_t total_valid_block_count;	/* # of valid blocks */
1615a66cdd98SJaegeuk Kim 	block_t discard_blks;			/* discard command candidats */
161639a53e0cSJaegeuk Kim 	block_t last_valid_block_count;		/* for recovery */
1617daeb433eSChao Yu 	block_t reserved_blocks;		/* configurable reserved blocks */
161880d42145SYunlong Song 	block_t current_reserved_blocks;	/* current reserved blocks */
1619daeb433eSChao Yu 
16204354994fSDaniel Rosenberg 	/* Additional tracking for no checkpoint mode */
16214354994fSDaniel Rosenberg 	block_t unusable_block_count;		/* # of blocks saved by last cp */
16224354994fSDaniel Rosenberg 
1623292c196aSChao Yu 	unsigned int nquota_files;		/* # of quota sysfile */
1624e4544b63STim Murray 	struct f2fs_rwsem quota_sem;		/* blocking cp for flags */
1625292c196aSChao Yu 
1626523be8a6SJaegeuk Kim 	/* # of pages, see count_type */
162735782b23SJaegeuk Kim 	atomic_t nr_pages[NR_COUNT_TYPE];
162841382ec4SJaegeuk Kim 	/* # of allocated blocks */
162941382ec4SJaegeuk Kim 	struct percpu_counter alloc_valid_block_count;
163047c8ebccSJaegeuk Kim 	/* # of node block writes as roll forward recovery */
163147c8ebccSJaegeuk Kim 	struct percpu_counter rf_node_block_count;
163239a53e0cSJaegeuk Kim 
1633687de7f1SJaegeuk Kim 	/* writeback control */
1634c29fd0c0SChao Yu 	atomic_t wb_sync_req[META];	/* count # of WB_SYNC threads */
1635687de7f1SJaegeuk Kim 
1636513c5f37SJaegeuk Kim 	/* valid inode count */
1637513c5f37SJaegeuk Kim 	struct percpu_counter total_valid_inode_count;
1638513c5f37SJaegeuk Kim 
163939a53e0cSJaegeuk Kim 	struct f2fs_mount_info mount_opt;	/* mount options */
164039a53e0cSJaegeuk Kim 
164139a53e0cSJaegeuk Kim 	/* for cleaning operations */
1642e4544b63STim Murray 	struct f2fs_rwsem gc_lock;		/*
1643fb24fea7SChao Yu 						 * semaphore for GC, avoid
1644fb24fea7SChao Yu 						 * race between GC and GC or CP
1645fb24fea7SChao Yu 						 */
164639a53e0cSJaegeuk Kim 	struct f2fs_gc_kthread	*gc_thread;	/* GC thread */
1647093749e2SChao Yu 	struct atgc_management am;		/* atgc management */
16485ec4e49fSJaegeuk Kim 	unsigned int cur_victim_sec;		/* current victim section num */
16495b0e9539SJaegeuk Kim 	unsigned int gc_mode;			/* current GC state */
1650e3080b01SChao Yu 	unsigned int next_victim_seg[2];	/* next segment in victim section */
1651e5a0db6aSYangtao Li 	spinlock_t gc_remaining_trials_lock;
1652e5a0db6aSYangtao Li 	/* remaining trial count for GC_URGENT_* and GC_IDLE_* */
1653e5a0db6aSYangtao Li 	unsigned int gc_remaining_trials;
16540e5e8111SDaeho Jeong 
16552ef79ecbSChao Yu 	/* for skip statistic */
16566f8d4455SJaegeuk Kim 	unsigned long long skipped_gc_rwsem;		/* FG_GC only */
165739a53e0cSJaegeuk Kim 
16581ad71a27SJaegeuk Kim 	/* threshold for gc trials on pinned files */
16591ad71a27SJaegeuk Kim 	u64 gc_pin_file_threshold;
1660e4544b63STim Murray 	struct f2fs_rwsem pin_sem;
16611ad71a27SJaegeuk Kim 
1662b1c57c1cSJaegeuk Kim 	/* maximum # of trials to find a victim segment for SSR and GC */
1663b1c57c1cSJaegeuk Kim 	unsigned int max_victim_search;
1664e3080b01SChao Yu 	/* migration granularity of garbage collection, unit: segment */
1665e3080b01SChao Yu 	unsigned int migration_granularity;
1666b1c57c1cSJaegeuk Kim 
166739a53e0cSJaegeuk Kim 	/*
166839a53e0cSJaegeuk Kim 	 * for stat information.
166939a53e0cSJaegeuk Kim 	 * one is for the LFS mode, and the other is for the SSR mode.
167039a53e0cSJaegeuk Kim 	 */
167135b09d82SNamjae Jeon #ifdef CONFIG_F2FS_STAT_FS
167239a53e0cSJaegeuk Kim 	struct f2fs_stat_info *stat_info;	/* FS status information */
1673b63e7be5SChao Yu 	atomic_t meta_count[META_MAX];		/* # of meta blocks */
167439a53e0cSJaegeuk Kim 	unsigned int segment_count[2];		/* # of allocated segments */
167539a53e0cSJaegeuk Kim 	unsigned int block_count[2];		/* # of allocated blocks */
1676b9a2c252SChangman Lee 	atomic_t inplace_count;		/* # of inplace update */
1677e7547dacSJaegeuk Kim 	/* # of lookup extent cache */
1678e7547dacSJaegeuk Kim 	atomic64_t total_hit_ext[NR_EXTENT_CACHES];
1679e7547dacSJaegeuk Kim 	/* # of hit rbtree extent node */
1680e7547dacSJaegeuk Kim 	atomic64_t read_hit_rbtree[NR_EXTENT_CACHES];
1681e7547dacSJaegeuk Kim 	/* # of hit cached extent node */
1682e7547dacSJaegeuk Kim 	atomic64_t read_hit_cached[NR_EXTENT_CACHES];
1683e7547dacSJaegeuk Kim 	/* # of hit largest extent node in read extent cache */
1684e7547dacSJaegeuk Kim 	atomic64_t read_hit_largest;
1685d5e8f6c9SChao Yu 	atomic_t inline_xattr;			/* # of inline_xattr inodes */
168603e14d52SChao Yu 	atomic_t inline_inode;			/* # of inline_data inodes */
168703e14d52SChao Yu 	atomic_t inline_dir;			/* # of inline_dentry inodes */
16884c8ff709SChao Yu 	atomic_t compr_inode;			/* # of compressed inodes */
1689ae999bb9SDaeho Jeong 	atomic64_t compr_blocks;		/* # of compressed blocks */
16908ec071c3SChao Yu 	atomic_t swapfile_inode;		/* # of swapfile inodes */
1691b4dac120SChao Yu 	atomic_t atomic_files;			/* # of opened atomic file */
169226a28a0cSJaegeuk Kim 	atomic_t max_aw_cnt;			/* max # of atomic writes */
1693274bd9baSChao Yu 	unsigned int io_skip_bggc;		/* skip background gc for in-flight IO */
1694274bd9baSChao Yu 	unsigned int other_skip_bggc;		/* skip background gc for other reasons */
169533fbd510SChao Yu 	unsigned int ndirty_inode[NR_INODE_TYPE];	/* # of dirty inodes */
169635b09d82SNamjae Jeon #endif
169739a53e0cSJaegeuk Kim 	spinlock_t stat_lock;			/* lock for stat operations */
1698b59d0baeSNamjae Jeon 
1699da9953b7SJaegeuk Kim 	/* to attach REQ_META|REQ_FUA flags */
1700da9953b7SJaegeuk Kim 	unsigned int data_io_flag;
170132b6aba8SJaegeuk Kim 	unsigned int node_io_flag;
1702b0af6d49SChao Yu 
1703759820c9SJulia Lawall 	/* For sysfs support */
17045d4daa57SChao Yu 	struct kobject s_kobj;			/* /sys/fs/f2fs/<devname> */
1705b59d0baeSNamjae Jeon 	struct completion s_kobj_unregister;
17062658e50dSJaegeuk Kim 
17075d4daa57SChao Yu 	struct kobject s_stat_kobj;		/* /sys/fs/f2fs/<devname>/stat */
17085d4daa57SChao Yu 	struct completion s_stat_kobj_unregister;
17095d4daa57SChao Yu 
17104c89b53dSJaegeuk Kim 	struct kobject s_feature_list_kobj;		/* /sys/fs/f2fs/<devname>/feature_list */
17114c89b53dSJaegeuk Kim 	struct completion s_feature_list_kobj_unregister;
17124c89b53dSJaegeuk Kim 
17132658e50dSJaegeuk Kim 	/* For shrinker support */
17142658e50dSJaegeuk Kim 	struct list_head s_list;
171571f2c820SChao Yu 	struct mutex umount_mutex;
171671f2c820SChao Yu 	unsigned int shrinker_run_no;
171771f2c820SChao Yu 
171871f2c820SChao Yu 	/* For multi devices */
17193c62be17SJaegeuk Kim 	int s_ndevs;				/* number of devices */
17203c62be17SJaegeuk Kim 	struct f2fs_dev_info *devs;		/* for device list */
17211228b482SChao Yu 	unsigned int dirty_device;		/* for checkpoint data flush */
17221228b482SChao Yu 	spinlock_t dev_lock;			/* protect dirty_device */
172371f2c820SChao Yu 	bool aligned_blksize;			/* all devices has the same logical blksize */
17248f1dbbbbSShuoran Liu 
17258f1dbbbbSShuoran Liu 	/* For write statistics */
17268f1dbbbbSShuoran Liu 	u64 sectors_written_start;
17278f1dbbbbSShuoran Liu 	u64 kbytes_written;
172843b6573bSKeith Mok 
172943b6573bSKeith Mok 	/* Reference to checksum algorithm driver via cryptoapi */
173043b6573bSKeith Mok 	struct crypto_shash *s_chksum_driver;
17311ecc0c5cSChao Yu 
1732704956ecSChao Yu 	/* Precomputed FS UUID checksum for seeding other checksums */
1733704956ecSChao Yu 	__u32 s_chksum_seed;
17344c8ff709SChao Yu 
17354c8ff709SChao Yu 	struct workqueue_struct *post_read_wq;	/* post read workqueue */
1736a999150fSChao Yu 
1737b62e71beSChao Yu 	/*
1738b62e71beSChao Yu 	 * If we are in irq context, let's update error information into
1739b62e71beSChao Yu 	 * on-disk superblock in the work.
1740b62e71beSChao Yu 	 */
1741b62e71beSChao Yu 	struct work_struct s_error_work;
174295fa90c9SChao Yu 	unsigned char errors[MAX_F2FS_ERRORS];		/* error flags */
1743b62e71beSChao Yu 	unsigned char stop_reason[MAX_STOP_REASON];	/* stop reason */
1744b62e71beSChao Yu 	spinlock_t error_lock;			/* protect errors/stop_reason array */
174595fa90c9SChao Yu 	bool error_dirty;			/* errors of sb is dirty */
174695fa90c9SChao Yu 
1747a999150fSChao Yu 	struct kmem_cache *inline_xattr_slab;	/* inline xattr entry */
1748a999150fSChao Yu 	unsigned int inline_xattr_slab_size;	/* default inline xattr slab size */
174931083031SChao Yu 
175007c6b593SDaeho Jeong 	/* For reclaimed segs statistics per each GC mode */
175107c6b593SDaeho Jeong 	unsigned int gc_segment_mode;		/* GC state for reclaimed segments */
175207c6b593SDaeho Jeong 	unsigned int gc_reclaimed_segs[MAX_GC_MODE];	/* Reclaimed segs for each mode */
175307c6b593SDaeho Jeong 
17540f6b56ecSDaeho Jeong 	unsigned long seq_file_ra_mul;		/* multiplier for ra_pages of seq. files in fadvise */
17550f6b56ecSDaeho Jeong 
17566691d940SDaeho Jeong 	int max_fragment_chunk;			/* max chunk size for block fragmentation mode */
17576691d940SDaeho Jeong 	int max_fragment_hole;			/* max hole size for block fragmentation mode */
17586691d940SDaeho Jeong 
1759f8e2f32bSDaeho Jeong 	/* For atomic write statistics */
1760f8e2f32bSDaeho Jeong 	atomic64_t current_atomic_write;
1761f8e2f32bSDaeho Jeong 	s64 peak_atomic_write;
1762f8e2f32bSDaeho Jeong 	u64 committed_atomic_block;
1763f8e2f32bSDaeho Jeong 	u64 revoked_atomic_block;
1764f8e2f32bSDaeho Jeong 
176531083031SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
176631083031SChao Yu 	struct kmem_cache *page_array_slab;	/* page array entry */
176731083031SChao Yu 	unsigned int page_array_slab_size;	/* default page array slab size */
17685ac443e2SDaeho Jeong 
17695ac443e2SDaeho Jeong 	/* For runtime compression statistics */
17705ac443e2SDaeho Jeong 	u64 compr_written_block;
17715ac443e2SDaeho Jeong 	u64 compr_saved_block;
17725ac443e2SDaeho Jeong 	u32 compr_new_inode;
17736ce19affSChao Yu 
17746ce19affSChao Yu 	/* For compressed block cache */
17756ce19affSChao Yu 	struct inode *compress_inode;		/* cache compressed blocks */
17766ce19affSChao Yu 	unsigned int compress_percent;		/* cache page percentage */
17776ce19affSChao Yu 	unsigned int compress_watermark;	/* cache page watermark */
17786ce19affSChao Yu 	atomic_t compress_page_hit;		/* cache hit count */
177931083031SChao Yu #endif
178052118743SDaeho Jeong 
178152118743SDaeho Jeong #ifdef CONFIG_F2FS_IOSTAT
178252118743SDaeho Jeong 	/* For app/fs IO statistics */
178352118743SDaeho Jeong 	spinlock_t iostat_lock;
17847a2b15cfSYangtao Li 	unsigned long long iostat_count[NR_IO_TYPE];
17857a2b15cfSYangtao Li 	unsigned long long iostat_bytes[NR_IO_TYPE];
17867a2b15cfSYangtao Li 	unsigned long long prev_iostat_bytes[NR_IO_TYPE];
178752118743SDaeho Jeong 	bool iostat_enable;
178852118743SDaeho Jeong 	unsigned long iostat_next_period;
178952118743SDaeho Jeong 	unsigned int iostat_period_ms;
1790a4b68176SDaeho Jeong 
1791a4b68176SDaeho Jeong 	/* For io latency related statistics info in one iostat period */
1792a4b68176SDaeho Jeong 	spinlock_t iostat_lat_lock;
1793a4b68176SDaeho Jeong 	struct iostat_lat_info *iostat_io_lat;
179452118743SDaeho Jeong #endif
179539a53e0cSJaegeuk Kim };
179639a53e0cSJaegeuk Kim 
17971ecc0c5cSChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
1798c40e15a9SYangtao Li #define time_to_inject(sbi, type) __time_to_inject(sbi, type, __func__,	\
1799c40e15a9SYangtao Li 									__builtin_return_address(0))
1800c40e15a9SYangtao Li static inline bool __time_to_inject(struct f2fs_sb_info *sbi, int type,
1801c40e15a9SYangtao Li 				const char *func, const char *parent_func)
18021ecc0c5cSChao Yu {
180363189b78SChao Yu 	struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
18041ecc0c5cSChao Yu 
18051ecc0c5cSChao Yu 	if (!ffi->inject_rate)
18061ecc0c5cSChao Yu 		return false;
18071ecc0c5cSChao Yu 
18081ecc0c5cSChao Yu 	if (!IS_FAULT_SET(ffi, type))
18091ecc0c5cSChao Yu 		return false;
18101ecc0c5cSChao Yu 
18111ecc0c5cSChao Yu 	atomic_inc(&ffi->inject_ops);
18121ecc0c5cSChao Yu 	if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
18131ecc0c5cSChao Yu 		atomic_set(&ffi->inject_ops, 0);
1814c40e15a9SYangtao Li 		printk_ratelimited("%sF2FS-fs (%s) : inject %s in %s of %pS\n",
1815c40e15a9SYangtao Li 			KERN_INFO, sbi->sb->s_id, f2fs_fault_name[type],
1816c40e15a9SYangtao Li 			func, parent_func);
18171ecc0c5cSChao Yu 		return true;
18181ecc0c5cSChao Yu 	}
18191ecc0c5cSChao Yu 	return false;
18201ecc0c5cSChao Yu }
18217fa750a1SArnd Bergmann #else
18227fa750a1SArnd Bergmann static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
18237fa750a1SArnd Bergmann {
18247fa750a1SArnd Bergmann 	return false;
18257fa750a1SArnd Bergmann }
18261ecc0c5cSChao Yu #endif
18271ecc0c5cSChao Yu 
18280916878dSDamien Le Moal /*
18290916878dSDamien Le Moal  * Test if the mounted volume is a multi-device volume.
18300916878dSDamien Le Moal  *   - For a single regular disk volume, sbi->s_ndevs is 0.
18310916878dSDamien Le Moal  *   - For a single zoned disk volume, sbi->s_ndevs is 1.
18320916878dSDamien Le Moal  *   - For a multi-device volume, sbi->s_ndevs is always 2 or more.
18330916878dSDamien Le Moal  */
18340916878dSDamien Le Moal static inline bool f2fs_is_multi_device(struct f2fs_sb_info *sbi)
18350916878dSDamien Le Moal {
18360916878dSDamien Le Moal 	return sbi->s_ndevs > 1;
18370916878dSDamien Le Moal }
18380916878dSDamien Le Moal 
18396beceb54SJaegeuk Kim static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
18406beceb54SJaegeuk Kim {
1841a7d10cf3SSahitya Tummala 	unsigned long now = jiffies;
1842a7d10cf3SSahitya Tummala 
1843a7d10cf3SSahitya Tummala 	sbi->last_time[type] = now;
1844a7d10cf3SSahitya Tummala 
1845a7d10cf3SSahitya Tummala 	/* DISCARD_TIME and GC_TIME are based on REQ_TIME */
1846a7d10cf3SSahitya Tummala 	if (type == REQ_TIME) {
1847a7d10cf3SSahitya Tummala 		sbi->last_time[DISCARD_TIME] = now;
1848a7d10cf3SSahitya Tummala 		sbi->last_time[GC_TIME] = now;
1849a7d10cf3SSahitya Tummala 	}
18506beceb54SJaegeuk Kim }
18516beceb54SJaegeuk Kim 
18526beceb54SJaegeuk Kim static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
18536beceb54SJaegeuk Kim {
18546bccfa19SArnd Bergmann 	unsigned long interval = sbi->interval_time[type] * HZ;
18556beceb54SJaegeuk Kim 
18566beceb54SJaegeuk Kim 	return time_after(jiffies, sbi->last_time[type] + interval);
18576beceb54SJaegeuk Kim }
18586beceb54SJaegeuk Kim 
1859a7d10cf3SSahitya Tummala static inline unsigned int f2fs_time_to_wait(struct f2fs_sb_info *sbi,
1860a7d10cf3SSahitya Tummala 						int type)
1861a7d10cf3SSahitya Tummala {
1862a7d10cf3SSahitya Tummala 	unsigned long interval = sbi->interval_time[type] * HZ;
1863a7d10cf3SSahitya Tummala 	unsigned int wait_ms = 0;
1864a7d10cf3SSahitya Tummala 	long delta;
1865a7d10cf3SSahitya Tummala 
1866a7d10cf3SSahitya Tummala 	delta = (sbi->last_time[type] + interval) - jiffies;
1867a7d10cf3SSahitya Tummala 	if (delta > 0)
1868a7d10cf3SSahitya Tummala 		wait_ms = jiffies_to_msecs(delta);
1869a7d10cf3SSahitya Tummala 
1870a7d10cf3SSahitya Tummala 	return wait_ms;
1871a7d10cf3SSahitya Tummala }
1872a7d10cf3SSahitya Tummala 
187339a53e0cSJaegeuk Kim /*
187439a53e0cSJaegeuk Kim  * Inline functions
187539a53e0cSJaegeuk Kim  */
1876416d2dbbSChao Yu static inline u32 __f2fs_crc32(struct f2fs_sb_info *sbi, u32 crc,
1877704956ecSChao Yu 			      const void *address, unsigned int length)
1878704956ecSChao Yu {
1879704956ecSChao Yu 	struct {
1880704956ecSChao Yu 		struct shash_desc shash;
1881704956ecSChao Yu 		char ctx[4];
1882704956ecSChao Yu 	} desc;
1883704956ecSChao Yu 	int err;
1884704956ecSChao Yu 
1885704956ecSChao Yu 	BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver) != sizeof(desc.ctx));
1886704956ecSChao Yu 
1887704956ecSChao Yu 	desc.shash.tfm = sbi->s_chksum_driver;
1888704956ecSChao Yu 	*(u32 *)desc.ctx = crc;
1889704956ecSChao Yu 
1890704956ecSChao Yu 	err = crypto_shash_update(&desc.shash, address, length);
1891704956ecSChao Yu 	BUG_ON(err);
1892704956ecSChao Yu 
1893704956ecSChao Yu 	return *(u32 *)desc.ctx;
1894704956ecSChao Yu }
1895704956ecSChao Yu 
1896416d2dbbSChao Yu static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
1897416d2dbbSChao Yu 			   unsigned int length)
1898416d2dbbSChao Yu {
1899416d2dbbSChao Yu 	return __f2fs_crc32(sbi, F2FS_SUPER_MAGIC, address, length);
1900416d2dbbSChao Yu }
1901416d2dbbSChao Yu 
1902416d2dbbSChao Yu static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
1903416d2dbbSChao Yu 				  void *buf, size_t buf_size)
1904416d2dbbSChao Yu {
1905416d2dbbSChao Yu 	return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
1906416d2dbbSChao Yu }
1907416d2dbbSChao Yu 
1908416d2dbbSChao Yu static inline u32 f2fs_chksum(struct f2fs_sb_info *sbi, u32 crc,
1909416d2dbbSChao Yu 			      const void *address, unsigned int length)
1910416d2dbbSChao Yu {
1911416d2dbbSChao Yu 	return __f2fs_crc32(sbi, crc, address, length);
1912416d2dbbSChao Yu }
1913416d2dbbSChao Yu 
191439a53e0cSJaegeuk Kim static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
191539a53e0cSJaegeuk Kim {
191639a53e0cSJaegeuk Kim 	return container_of(inode, struct f2fs_inode_info, vfs_inode);
191739a53e0cSJaegeuk Kim }
191839a53e0cSJaegeuk Kim 
191939a53e0cSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
192039a53e0cSJaegeuk Kim {
192139a53e0cSJaegeuk Kim 	return sb->s_fs_info;
192239a53e0cSJaegeuk Kim }
192339a53e0cSJaegeuk Kim 
19244081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
19254081363fSJaegeuk Kim {
19264081363fSJaegeuk Kim 	return F2FS_SB(inode->i_sb);
19274081363fSJaegeuk Kim }
19284081363fSJaegeuk Kim 
19294081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
19304081363fSJaegeuk Kim {
19314081363fSJaegeuk Kim 	return F2FS_I_SB(mapping->host);
19324081363fSJaegeuk Kim }
19334081363fSJaegeuk Kim 
19344081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
19354081363fSJaegeuk Kim {
19364969c06aSJaegeuk Kim 	return F2FS_M_SB(page_file_mapping(page));
19374081363fSJaegeuk Kim }
19384081363fSJaegeuk Kim 
193939a53e0cSJaegeuk Kim static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
194039a53e0cSJaegeuk Kim {
194139a53e0cSJaegeuk Kim 	return (struct f2fs_super_block *)(sbi->raw_super);
194239a53e0cSJaegeuk Kim }
194339a53e0cSJaegeuk Kim 
194439a53e0cSJaegeuk Kim static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
194539a53e0cSJaegeuk Kim {
194639a53e0cSJaegeuk Kim 	return (struct f2fs_checkpoint *)(sbi->ckpt);
194739a53e0cSJaegeuk Kim }
194839a53e0cSJaegeuk Kim 
194945590710SGu Zheng static inline struct f2fs_node *F2FS_NODE(struct page *page)
195045590710SGu Zheng {
195145590710SGu Zheng 	return (struct f2fs_node *)page_address(page);
195245590710SGu Zheng }
195345590710SGu Zheng 
195458bfaf44SJaegeuk Kim static inline struct f2fs_inode *F2FS_INODE(struct page *page)
195558bfaf44SJaegeuk Kim {
195658bfaf44SJaegeuk Kim 	return &((struct f2fs_node *)page_address(page))->i;
195758bfaf44SJaegeuk Kim }
195858bfaf44SJaegeuk Kim 
195939a53e0cSJaegeuk Kim static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
196039a53e0cSJaegeuk Kim {
196139a53e0cSJaegeuk Kim 	return (struct f2fs_nm_info *)(sbi->nm_info);
196239a53e0cSJaegeuk Kim }
196339a53e0cSJaegeuk Kim 
196439a53e0cSJaegeuk Kim static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
196539a53e0cSJaegeuk Kim {
196639a53e0cSJaegeuk Kim 	return (struct f2fs_sm_info *)(sbi->sm_info);
196739a53e0cSJaegeuk Kim }
196839a53e0cSJaegeuk Kim 
196939a53e0cSJaegeuk Kim static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
197039a53e0cSJaegeuk Kim {
197139a53e0cSJaegeuk Kim 	return (struct sit_info *)(SM_I(sbi)->sit_info);
197239a53e0cSJaegeuk Kim }
197339a53e0cSJaegeuk Kim 
197439a53e0cSJaegeuk Kim static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
197539a53e0cSJaegeuk Kim {
197639a53e0cSJaegeuk Kim 	return (struct free_segmap_info *)(SM_I(sbi)->free_info);
197739a53e0cSJaegeuk Kim }
197839a53e0cSJaegeuk Kim 
197939a53e0cSJaegeuk Kim static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
198039a53e0cSJaegeuk Kim {
198139a53e0cSJaegeuk Kim 	return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
198239a53e0cSJaegeuk Kim }
198339a53e0cSJaegeuk Kim 
19849df27d98SGu Zheng static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
19859df27d98SGu Zheng {
19869df27d98SGu Zheng 	return sbi->meta_inode->i_mapping;
19879df27d98SGu Zheng }
19889df27d98SGu Zheng 
19894ef51a8fSJaegeuk Kim static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
19904ef51a8fSJaegeuk Kim {
19914ef51a8fSJaegeuk Kim 	return sbi->node_inode->i_mapping;
19924ef51a8fSJaegeuk Kim }
19934ef51a8fSJaegeuk Kim 
1994caf0047eSChao Yu static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
199539a53e0cSJaegeuk Kim {
1996fadb2fb8SChao Yu 	return test_bit(type, &sbi->s_flag);
199739a53e0cSJaegeuk Kim }
199839a53e0cSJaegeuk Kim 
1999caf0047eSChao Yu static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
200039a53e0cSJaegeuk Kim {
2001fadb2fb8SChao Yu 	set_bit(type, &sbi->s_flag);
2002caf0047eSChao Yu }
2003caf0047eSChao Yu 
2004caf0047eSChao Yu static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
2005caf0047eSChao Yu {
2006fadb2fb8SChao Yu 	clear_bit(type, &sbi->s_flag);
200739a53e0cSJaegeuk Kim }
200839a53e0cSJaegeuk Kim 
2009d71b5564SJaegeuk Kim static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
2010d71b5564SJaegeuk Kim {
2011d71b5564SJaegeuk Kim 	return le64_to_cpu(cp->checkpoint_ver);
2012d71b5564SJaegeuk Kim }
2013d71b5564SJaegeuk Kim 
2014ea676733SJaegeuk Kim static inline unsigned long f2fs_qf_ino(struct super_block *sb, int type)
2015ea676733SJaegeuk Kim {
2016ea676733SJaegeuk Kim 	if (type < F2FS_MAX_QUOTAS)
2017ea676733SJaegeuk Kim 		return le32_to_cpu(F2FS_SB(sb)->raw_super->qf_ino[type]);
2018ea676733SJaegeuk Kim 	return 0;
2019ea676733SJaegeuk Kim }
2020ea676733SJaegeuk Kim 
2021ced2c7eaSKinglong Mee static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
2022ced2c7eaSKinglong Mee {
2023ced2c7eaSKinglong Mee 	size_t crc_offset = le32_to_cpu(cp->checksum_offset);
2024ced2c7eaSKinglong Mee 	return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
2025ced2c7eaSKinglong Mee }
2026ced2c7eaSKinglong Mee 
2027aaec2b1dSChao Yu static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
202825ca923bSJaegeuk Kim {
202925ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
2030aaec2b1dSChao Yu 
203125ca923bSJaegeuk Kim 	return ckpt_flags & f;
203225ca923bSJaegeuk Kim }
203325ca923bSJaegeuk Kim 
2034aaec2b1dSChao Yu static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
203525ca923bSJaegeuk Kim {
2036aaec2b1dSChao Yu 	return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
2037aaec2b1dSChao Yu }
2038aaec2b1dSChao Yu 
2039aaec2b1dSChao Yu static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
2040aaec2b1dSChao Yu {
2041aaec2b1dSChao Yu 	unsigned int ckpt_flags;
2042aaec2b1dSChao Yu 
2043aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
204425ca923bSJaegeuk Kim 	ckpt_flags |= f;
204525ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
204625ca923bSJaegeuk Kim }
204725ca923bSJaegeuk Kim 
2048aaec2b1dSChao Yu static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
204925ca923bSJaegeuk Kim {
2050d1aa2453SChao Yu 	unsigned long flags;
2051d1aa2453SChao Yu 
2052d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
2053aaec2b1dSChao Yu 	__set_ckpt_flags(F2FS_CKPT(sbi), f);
2054d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
2055aaec2b1dSChao Yu }
2056aaec2b1dSChao Yu 
2057aaec2b1dSChao Yu static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
2058aaec2b1dSChao Yu {
2059aaec2b1dSChao Yu 	unsigned int ckpt_flags;
2060aaec2b1dSChao Yu 
2061aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
206225ca923bSJaegeuk Kim 	ckpt_flags &= (~f);
206325ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
206425ca923bSJaegeuk Kim }
206525ca923bSJaegeuk Kim 
2066aaec2b1dSChao Yu static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
2067aaec2b1dSChao Yu {
2068d1aa2453SChao Yu 	unsigned long flags;
2069d1aa2453SChao Yu 
2070d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
2071aaec2b1dSChao Yu 	__clear_ckpt_flags(F2FS_CKPT(sbi), f);
2072d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
2073aaec2b1dSChao Yu }
2074aaec2b1dSChao Yu 
2075c7f91bd4SBart Van Assche #define init_f2fs_rwsem(sem)					\
2076c7f91bd4SBart Van Assche do {								\
2077c7f91bd4SBart Van Assche 	static struct lock_class_key __key;			\
2078c7f91bd4SBart Van Assche 								\
2079c7f91bd4SBart Van Assche 	__init_f2fs_rwsem((sem), #sem, &__key);			\
2080c7f91bd4SBart Van Assche } while (0)
2081c7f91bd4SBart Van Assche 
2082c7f91bd4SBart Van Assche static inline void __init_f2fs_rwsem(struct f2fs_rwsem *sem,
2083c7f91bd4SBart Van Assche 		const char *sem_name, struct lock_class_key *key)
2084e4544b63STim Murray {
2085c7f91bd4SBart Van Assche 	__init_rwsem(&sem->internal_rwsem, sem_name, key);
20867f8e249dSJaegeuk Kim #ifdef CONFIG_F2FS_UNFAIR_RWSEM
2087e4544b63STim Murray 	init_waitqueue_head(&sem->read_waiters);
20887f8e249dSJaegeuk Kim #endif
2089e4544b63STim Murray }
2090e4544b63STim Murray 
2091e4544b63STim Murray static inline int f2fs_rwsem_is_locked(struct f2fs_rwsem *sem)
2092e4544b63STim Murray {
2093e4544b63STim Murray 	return rwsem_is_locked(&sem->internal_rwsem);
2094e4544b63STim Murray }
2095e4544b63STim Murray 
2096e4544b63STim Murray static inline int f2fs_rwsem_is_contended(struct f2fs_rwsem *sem)
2097e4544b63STim Murray {
2098e4544b63STim Murray 	return rwsem_is_contended(&sem->internal_rwsem);
2099e4544b63STim Murray }
2100e4544b63STim Murray 
2101e4544b63STim Murray static inline void f2fs_down_read(struct f2fs_rwsem *sem)
2102e4544b63STim Murray {
21037f8e249dSJaegeuk Kim #ifdef CONFIG_F2FS_UNFAIR_RWSEM
2104e4544b63STim Murray 	wait_event(sem->read_waiters, down_read_trylock(&sem->internal_rwsem));
21057f8e249dSJaegeuk Kim #else
21067f8e249dSJaegeuk Kim 	down_read(&sem->internal_rwsem);
21077f8e249dSJaegeuk Kim #endif
2108e4544b63STim Murray }
2109e4544b63STim Murray 
2110e4544b63STim Murray static inline int f2fs_down_read_trylock(struct f2fs_rwsem *sem)
2111e4544b63STim Murray {
2112e4544b63STim Murray 	return down_read_trylock(&sem->internal_rwsem);
2113e4544b63STim Murray }
2114e4544b63STim Murray 
2115e4544b63STim Murray #ifdef CONFIG_DEBUG_LOCK_ALLOC
2116e4544b63STim Murray static inline void f2fs_down_read_nested(struct f2fs_rwsem *sem, int subclass)
2117e4544b63STim Murray {
2118e4544b63STim Murray 	down_read_nested(&sem->internal_rwsem, subclass);
2119e4544b63STim Murray }
2120e4544b63STim Murray #else
2121e4544b63STim Murray #define f2fs_down_read_nested(sem, subclass) f2fs_down_read(sem)
2122e4544b63STim Murray #endif
2123e4544b63STim Murray 
2124e4544b63STim Murray static inline void f2fs_up_read(struct f2fs_rwsem *sem)
2125e4544b63STim Murray {
2126e4544b63STim Murray 	up_read(&sem->internal_rwsem);
2127e4544b63STim Murray }
2128e4544b63STim Murray 
2129e4544b63STim Murray static inline void f2fs_down_write(struct f2fs_rwsem *sem)
2130e4544b63STim Murray {
2131e4544b63STim Murray 	down_write(&sem->internal_rwsem);
2132e4544b63STim Murray }
2133e4544b63STim Murray 
2134e4544b63STim Murray static inline int f2fs_down_write_trylock(struct f2fs_rwsem *sem)
2135e4544b63STim Murray {
2136e4544b63STim Murray 	return down_write_trylock(&sem->internal_rwsem);
2137e4544b63STim Murray }
2138e4544b63STim Murray 
2139e4544b63STim Murray static inline void f2fs_up_write(struct f2fs_rwsem *sem)
2140e4544b63STim Murray {
2141e4544b63STim Murray 	up_write(&sem->internal_rwsem);
21427f8e249dSJaegeuk Kim #ifdef CONFIG_F2FS_UNFAIR_RWSEM
2143e4544b63STim Murray 	wake_up_all(&sem->read_waiters);
21447f8e249dSJaegeuk Kim #endif
2145e4544b63STim Murray }
2146e4544b63STim Murray 
2147e479556bSGu Zheng static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
214839a53e0cSJaegeuk Kim {
2149e4544b63STim Murray 	f2fs_down_read(&sbi->cp_rwsem);
215039a53e0cSJaegeuk Kim }
215139a53e0cSJaegeuk Kim 
2152cc15620bSJaegeuk Kim static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi)
2153cc15620bSJaegeuk Kim {
2154c40e15a9SYangtao Li 	if (time_to_inject(sbi, FAULT_LOCK_OP))
21553e020389SChao Yu 		return 0;
2156e4544b63STim Murray 	return f2fs_down_read_trylock(&sbi->cp_rwsem);
2157cc15620bSJaegeuk Kim }
2158cc15620bSJaegeuk Kim 
2159e479556bSGu Zheng static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
216039a53e0cSJaegeuk Kim {
2161e4544b63STim Murray 	f2fs_up_read(&sbi->cp_rwsem);
216239936837SJaegeuk Kim }
216339936837SJaegeuk Kim 
2164e479556bSGu Zheng static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
216539936837SJaegeuk Kim {
2166e4544b63STim Murray 	f2fs_down_write(&sbi->cp_rwsem);
216739936837SJaegeuk Kim }
216839936837SJaegeuk Kim 
2169e479556bSGu Zheng static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
217039936837SJaegeuk Kim {
2171e4544b63STim Murray 	f2fs_up_write(&sbi->cp_rwsem);
217239a53e0cSJaegeuk Kim }
217339a53e0cSJaegeuk Kim 
2174119ee914SJaegeuk Kim static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
2175119ee914SJaegeuk Kim {
2176119ee914SJaegeuk Kim 	int reason = CP_SYNC;
2177119ee914SJaegeuk Kim 
2178119ee914SJaegeuk Kim 	if (test_opt(sbi, FASTBOOT))
2179119ee914SJaegeuk Kim 		reason = CP_FASTBOOT;
2180119ee914SJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
2181119ee914SJaegeuk Kim 		reason = CP_UMOUNT;
2182119ee914SJaegeuk Kim 	return reason;
2183119ee914SJaegeuk Kim }
2184119ee914SJaegeuk Kim 
2185119ee914SJaegeuk Kim static inline bool __remain_node_summaries(int reason)
2186119ee914SJaegeuk Kim {
2187c473f1a9SChao Yu 	return (reason & (CP_UMOUNT | CP_FASTBOOT));
2188119ee914SJaegeuk Kim }
2189119ee914SJaegeuk Kim 
2190119ee914SJaegeuk Kim static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
2191119ee914SJaegeuk Kim {
2192aaec2b1dSChao Yu 	return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
2193aaec2b1dSChao Yu 			is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
2194119ee914SJaegeuk Kim }
2195119ee914SJaegeuk Kim 
219639a53e0cSJaegeuk Kim /*
219739a53e0cSJaegeuk Kim  * Check whether the inode has blocks or not
219839a53e0cSJaegeuk Kim  */
219939a53e0cSJaegeuk Kim static inline int F2FS_HAS_BLOCKS(struct inode *inode)
220039a53e0cSJaegeuk Kim {
22010eb0adadSChao Yu 	block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
22020eb0adadSChao Yu 
2203000519f2SChao Yu 	return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block;
220439a53e0cSJaegeuk Kim }
220539a53e0cSJaegeuk Kim 
22064bc8e9bcSChao Yu static inline bool f2fs_has_xattr_block(unsigned int ofs)
22074bc8e9bcSChao Yu {
22084bc8e9bcSChao Yu 	return ofs == XATTR_NODE_OFFSET;
22094bc8e9bcSChao Yu }
22104bc8e9bcSChao Yu 
2211d8a9a229SJaegeuk Kim static inline bool __allow_reserved_blocks(struct f2fs_sb_info *sbi,
2212a90a0884SJaegeuk Kim 					struct inode *inode, bool cap)
22137c2e5963SJaegeuk Kim {
2214d8a9a229SJaegeuk Kim 	if (!inode)
2215d8a9a229SJaegeuk Kim 		return true;
22167c2e5963SJaegeuk Kim 	if (!test_opt(sbi, RESERVE_ROOT))
22177c2e5963SJaegeuk Kim 		return false;
2218d8a9a229SJaegeuk Kim 	if (IS_NOQUOTA(inode))
2219d8a9a229SJaegeuk Kim 		return true;
222063189b78SChao Yu 	if (uid_eq(F2FS_OPTION(sbi).s_resuid, current_fsuid()))
22217c2e5963SJaegeuk Kim 		return true;
222263189b78SChao Yu 	if (!gid_eq(F2FS_OPTION(sbi).s_resgid, GLOBAL_ROOT_GID) &&
222363189b78SChao Yu 					in_group_p(F2FS_OPTION(sbi).s_resgid))
22247c2e5963SJaegeuk Kim 		return true;
2225a90a0884SJaegeuk Kim 	if (cap && capable(CAP_SYS_RESOURCE))
2226162b27aeSJaegeuk Kim 		return true;
22277c2e5963SJaegeuk Kim 	return false;
22287c2e5963SJaegeuk Kim }
22297c2e5963SJaegeuk Kim 
22300abd675eSChao Yu static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool);
22310abd675eSChao Yu static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
223246008c6dSChao Yu 				 struct inode *inode, blkcnt_t *count)
223339a53e0cSJaegeuk Kim {
22340abd675eSChao Yu 	blkcnt_t diff = 0, release = 0;
2235daeb433eSChao Yu 	block_t avail_user_block_count;
22360abd675eSChao Yu 	int ret;
22370abd675eSChao Yu 
22380abd675eSChao Yu 	ret = dquot_reserve_block(inode, *count);
22390abd675eSChao Yu 	if (ret)
22400abd675eSChao Yu 		return ret;
2241dd11a5dfSJaegeuk Kim 
224255523519SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
22430abd675eSChao Yu 		release = *count;
22449ea2f0beSChao Yu 		goto release_quota;
224555523519SChao Yu 	}
22467fa750a1SArnd Bergmann 
2247dd11a5dfSJaegeuk Kim 	/*
2248dd11a5dfSJaegeuk Kim 	 * let's increase this in prior to actual block count change in order
2249dd11a5dfSJaegeuk Kim 	 * for f2fs_sync_file to avoid data races when deciding checkpoint.
2250dd11a5dfSJaegeuk Kim 	 */
2251dd11a5dfSJaegeuk Kim 	percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
225239a53e0cSJaegeuk Kim 
225339a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
22542555a2d5SJaegeuk Kim 	sbi->total_valid_block_count += (block_t)(*count);
225580d42145SYunlong Song 	avail_user_block_count = sbi->user_block_count -
225680d42145SYunlong Song 					sbi->current_reserved_blocks;
22577e65be49SJaegeuk Kim 
2258a90a0884SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode, true))
225963189b78SChao Yu 		avail_user_block_count -= F2FS_OPTION(sbi).root_reserved_blocks;
2260300a8429SChao Yu 
2261300a8429SChao Yu 	if (F2FS_IO_ALIGNED(sbi))
2262300a8429SChao Yu 		avail_user_block_count -= sbi->blocks_per_seg *
2263300a8429SChao Yu 				SM_I(sbi)->additional_reserved_segments;
2264300a8429SChao Yu 
2265a4c3ecaaSDaniel Rosenberg 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2266a4c3ecaaSDaniel Rosenberg 		if (avail_user_block_count > sbi->unusable_block_count)
22674354994fSDaniel Rosenberg 			avail_user_block_count -= sbi->unusable_block_count;
2268a4c3ecaaSDaniel Rosenberg 		else
2269a4c3ecaaSDaniel Rosenberg 			avail_user_block_count = 0;
2270a4c3ecaaSDaniel Rosenberg 	}
2271daeb433eSChao Yu 	if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
2272daeb433eSChao Yu 		diff = sbi->total_valid_block_count - avail_user_block_count;
22737e65be49SJaegeuk Kim 		if (diff > *count)
22747e65be49SJaegeuk Kim 			diff = *count;
2275dd11a5dfSJaegeuk Kim 		*count -= diff;
22760abd675eSChao Yu 		release = diff;
22777e65be49SJaegeuk Kim 		sbi->total_valid_block_count -= diff;
227846008c6dSChao Yu 		if (!*count) {
227939a53e0cSJaegeuk Kim 			spin_unlock(&sbi->stat_lock);
22800abd675eSChao Yu 			goto enospc;
228139a53e0cSJaegeuk Kim 		}
228246008c6dSChao Yu 	}
228339a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
228441382ec4SJaegeuk Kim 
228536b877afSDaniel Rosenberg 	if (unlikely(release)) {
228636b877afSDaniel Rosenberg 		percpu_counter_sub(&sbi->alloc_valid_block_count, release);
22870abd675eSChao Yu 		dquot_release_reservation_block(inode, release);
228836b877afSDaniel Rosenberg 	}
22890abd675eSChao Yu 	f2fs_i_blocks_write(inode, *count, true, true);
22900abd675eSChao Yu 	return 0;
22910abd675eSChao Yu 
22920abd675eSChao Yu enospc:
229336b877afSDaniel Rosenberg 	percpu_counter_sub(&sbi->alloc_valid_block_count, release);
22949ea2f0beSChao Yu release_quota:
22950abd675eSChao Yu 	dquot_release_reservation_block(inode, release);
22960abd675eSChao Yu 	return -ENOSPC;
229739a53e0cSJaegeuk Kim }
229839a53e0cSJaegeuk Kim 
2299dcbb4c10SJoe Perches __printf(2, 3)
2300dcbb4c10SJoe Perches void f2fs_printk(struct f2fs_sb_info *sbi, const char *fmt, ...);
2301dcbb4c10SJoe Perches 
2302dcbb4c10SJoe Perches #define f2fs_err(sbi, fmt, ...)						\
2303dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_ERR fmt, ##__VA_ARGS__)
2304dcbb4c10SJoe Perches #define f2fs_warn(sbi, fmt, ...)					\
2305dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_WARNING fmt, ##__VA_ARGS__)
2306dcbb4c10SJoe Perches #define f2fs_notice(sbi, fmt, ...)					\
2307dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_NOTICE fmt, ##__VA_ARGS__)
2308dcbb4c10SJoe Perches #define f2fs_info(sbi, fmt, ...)					\
2309dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_INFO fmt, ##__VA_ARGS__)
2310dcbb4c10SJoe Perches #define f2fs_debug(sbi, fmt, ...)					\
2311dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_DEBUG fmt, ##__VA_ARGS__)
2312dcbb4c10SJoe Perches 
2313635a52daSChao Yu #define PAGE_PRIVATE_GET_FUNC(name, flagname) \
2314635a52daSChao Yu static inline bool page_private_##name(struct page *page) \
2315635a52daSChao Yu { \
2316635a52daSChao Yu 	return PagePrivate(page) && \
2317635a52daSChao Yu 		test_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page)) && \
2318635a52daSChao Yu 		test_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
2319635a52daSChao Yu }
2320635a52daSChao Yu 
2321635a52daSChao Yu #define PAGE_PRIVATE_SET_FUNC(name, flagname) \
2322635a52daSChao Yu static inline void set_page_private_##name(struct page *page) \
2323635a52daSChao Yu { \
2324635a52daSChao Yu 	if (!PagePrivate(page)) \
2325635a52daSChao Yu 		attach_page_private(page, (void *)0); \
2326635a52daSChao Yu 	set_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page)); \
2327635a52daSChao Yu 	set_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
2328635a52daSChao Yu }
2329635a52daSChao Yu 
2330635a52daSChao Yu #define PAGE_PRIVATE_CLEAR_FUNC(name, flagname) \
2331635a52daSChao Yu static inline void clear_page_private_##name(struct page *page) \
2332635a52daSChao Yu { \
2333635a52daSChao Yu 	clear_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
2334635a52daSChao Yu 	if (page_private(page) == BIT(PAGE_PRIVATE_NOT_POINTER)) \
2335635a52daSChao Yu 		detach_page_private(page); \
2336635a52daSChao Yu }
2337635a52daSChao Yu 
2338635a52daSChao Yu PAGE_PRIVATE_GET_FUNC(nonpointer, NOT_POINTER);
2339635a52daSChao Yu PAGE_PRIVATE_GET_FUNC(inline, INLINE_INODE);
2340635a52daSChao Yu PAGE_PRIVATE_GET_FUNC(gcing, ONGOING_MIGRATION);
2341635a52daSChao Yu PAGE_PRIVATE_GET_FUNC(dummy, DUMMY_WRITE);
2342635a52daSChao Yu 
2343635a52daSChao Yu PAGE_PRIVATE_SET_FUNC(reference, REF_RESOURCE);
2344635a52daSChao Yu PAGE_PRIVATE_SET_FUNC(inline, INLINE_INODE);
2345635a52daSChao Yu PAGE_PRIVATE_SET_FUNC(gcing, ONGOING_MIGRATION);
2346635a52daSChao Yu PAGE_PRIVATE_SET_FUNC(dummy, DUMMY_WRITE);
2347635a52daSChao Yu 
2348635a52daSChao Yu PAGE_PRIVATE_CLEAR_FUNC(reference, REF_RESOURCE);
2349635a52daSChao Yu PAGE_PRIVATE_CLEAR_FUNC(inline, INLINE_INODE);
2350635a52daSChao Yu PAGE_PRIVATE_CLEAR_FUNC(gcing, ONGOING_MIGRATION);
2351635a52daSChao Yu PAGE_PRIVATE_CLEAR_FUNC(dummy, DUMMY_WRITE);
2352635a52daSChao Yu 
2353635a52daSChao Yu static inline unsigned long get_page_private_data(struct page *page)
2354635a52daSChao Yu {
2355635a52daSChao Yu 	unsigned long data = page_private(page);
2356635a52daSChao Yu 
2357635a52daSChao Yu 	if (!test_bit(PAGE_PRIVATE_NOT_POINTER, &data))
2358635a52daSChao Yu 		return 0;
2359635a52daSChao Yu 	return data >> PAGE_PRIVATE_MAX;
2360635a52daSChao Yu }
2361635a52daSChao Yu 
2362635a52daSChao Yu static inline void set_page_private_data(struct page *page, unsigned long data)
2363635a52daSChao Yu {
2364635a52daSChao Yu 	if (!PagePrivate(page))
2365635a52daSChao Yu 		attach_page_private(page, (void *)0);
2366635a52daSChao Yu 	set_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page));
2367635a52daSChao Yu 	page_private(page) |= data << PAGE_PRIVATE_MAX;
2368635a52daSChao Yu }
2369635a52daSChao Yu 
2370635a52daSChao Yu static inline void clear_page_private_data(struct page *page)
2371635a52daSChao Yu {
2372635a52daSChao Yu 	page_private(page) &= GENMASK(PAGE_PRIVATE_MAX - 1, 0);
2373635a52daSChao Yu 	if (page_private(page) == BIT(PAGE_PRIVATE_NOT_POINTER))
2374635a52daSChao Yu 		detach_page_private(page);
2375635a52daSChao Yu }
2376635a52daSChao Yu 
2377635a52daSChao Yu static inline void clear_page_private_all(struct page *page)
2378635a52daSChao Yu {
2379635a52daSChao Yu 	clear_page_private_data(page);
2380635a52daSChao Yu 	clear_page_private_reference(page);
2381635a52daSChao Yu 	clear_page_private_gcing(page);
2382635a52daSChao Yu 	clear_page_private_inline(page);
2383635a52daSChao Yu 
2384635a52daSChao Yu 	f2fs_bug_on(F2FS_P_SB(page), page_private(page));
2385635a52daSChao Yu }
2386635a52daSChao Yu 
2387da19b0dcSGu Zheng static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
238839a53e0cSJaegeuk Kim 						struct inode *inode,
23890eb0adadSChao Yu 						block_t count)
239039a53e0cSJaegeuk Kim {
23910eb0adadSChao Yu 	blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK;
23920eb0adadSChao Yu 
239339a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
23949850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
239539a53e0cSJaegeuk Kim 	sbi->total_valid_block_count -= (block_t)count;
239680d42145SYunlong Song 	if (sbi->reserved_blocks &&
239780d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
239880d42145SYunlong Song 		sbi->current_reserved_blocks = min(sbi->reserved_blocks,
239980d42145SYunlong Song 					sbi->current_reserved_blocks + count);
240039a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
24015e159cd3SChao Yu 	if (unlikely(inode->i_blocks < sectors)) {
2402dcbb4c10SJoe Perches 		f2fs_warn(sbi, "Inconsistent i_blocks, ino:%lu, iblocks:%llu, sectors:%llu",
24035e159cd3SChao Yu 			  inode->i_ino,
24045e159cd3SChao Yu 			  (unsigned long long)inode->i_blocks,
24055e159cd3SChao Yu 			  (unsigned long long)sectors);
24065e159cd3SChao Yu 		set_sbi_flag(sbi, SBI_NEED_FSCK);
24075e159cd3SChao Yu 		return;
24085e159cd3SChao Yu 	}
24090abd675eSChao Yu 	f2fs_i_blocks_write(inode, count, false, true);
241039a53e0cSJaegeuk Kim }
241139a53e0cSJaegeuk Kim 
241239a53e0cSJaegeuk Kim static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
241339a53e0cSJaegeuk Kim {
241435782b23SJaegeuk Kim 	atomic_inc(&sbi->nr_pages[count_type]);
24157c4abcbeSChao Yu 
241620109873SChao Yu 	if (count_type == F2FS_DIRTY_DENTS ||
241720109873SChao Yu 			count_type == F2FS_DIRTY_NODES ||
241820109873SChao Yu 			count_type == F2FS_DIRTY_META ||
241920109873SChao Yu 			count_type == F2FS_DIRTY_QDATA ||
242020109873SChao Yu 			count_type == F2FS_DIRTY_IMETA)
2421caf0047eSChao Yu 		set_sbi_flag(sbi, SBI_IS_DIRTY);
242239a53e0cSJaegeuk Kim }
242339a53e0cSJaegeuk Kim 
2424a7ffdbe2SJaegeuk Kim static inline void inode_inc_dirty_pages(struct inode *inode)
242539a53e0cSJaegeuk Kim {
2426204706c7SJaegeuk Kim 	atomic_inc(&F2FS_I(inode)->dirty_pages);
2427c227f912SChao Yu 	inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
2428c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
24292c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
24302c8a4a28SJaegeuk Kim 		inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
243139a53e0cSJaegeuk Kim }
243239a53e0cSJaegeuk Kim 
243339a53e0cSJaegeuk Kim static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
243439a53e0cSJaegeuk Kim {
243535782b23SJaegeuk Kim 	atomic_dec(&sbi->nr_pages[count_type]);
243639a53e0cSJaegeuk Kim }
243739a53e0cSJaegeuk Kim 
2438a7ffdbe2SJaegeuk Kim static inline void inode_dec_dirty_pages(struct inode *inode)
243939a53e0cSJaegeuk Kim {
24405ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
24415ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
24421fe54f9dSJaegeuk Kim 		return;
24431fe54f9dSJaegeuk Kim 
2444204706c7SJaegeuk Kim 	atomic_dec(&F2FS_I(inode)->dirty_pages);
2445c227f912SChao Yu 	dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
2446c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
24472c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
24482c8a4a28SJaegeuk Kim 		dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
244939a53e0cSJaegeuk Kim }
245039a53e0cSJaegeuk Kim 
2451f8e2f32bSDaeho Jeong static inline void inc_atomic_write_cnt(struct inode *inode)
2452f8e2f32bSDaeho Jeong {
2453f8e2f32bSDaeho Jeong 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2454f8e2f32bSDaeho Jeong 	struct f2fs_inode_info *fi = F2FS_I(inode);
2455f8e2f32bSDaeho Jeong 	u64 current_write;
2456f8e2f32bSDaeho Jeong 
2457f8e2f32bSDaeho Jeong 	fi->atomic_write_cnt++;
2458f8e2f32bSDaeho Jeong 	atomic64_inc(&sbi->current_atomic_write);
2459f8e2f32bSDaeho Jeong 	current_write = atomic64_read(&sbi->current_atomic_write);
2460f8e2f32bSDaeho Jeong 	if (current_write > sbi->peak_atomic_write)
2461f8e2f32bSDaeho Jeong 		sbi->peak_atomic_write = current_write;
2462f8e2f32bSDaeho Jeong }
2463f8e2f32bSDaeho Jeong 
2464f8e2f32bSDaeho Jeong static inline void release_atomic_write_cnt(struct inode *inode)
2465f8e2f32bSDaeho Jeong {
2466f8e2f32bSDaeho Jeong 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2467f8e2f32bSDaeho Jeong 	struct f2fs_inode_info *fi = F2FS_I(inode);
2468f8e2f32bSDaeho Jeong 
2469f8e2f32bSDaeho Jeong 	atomic64_sub(fi->atomic_write_cnt, &sbi->current_atomic_write);
2470f8e2f32bSDaeho Jeong 	fi->atomic_write_cnt = 0;
2471f8e2f32bSDaeho Jeong }
2472f8e2f32bSDaeho Jeong 
2473523be8a6SJaegeuk Kim static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
247439a53e0cSJaegeuk Kim {
247535782b23SJaegeuk Kim 	return atomic_read(&sbi->nr_pages[count_type]);
247639a53e0cSJaegeuk Kim }
247739a53e0cSJaegeuk Kim 
2478204706c7SJaegeuk Kim static inline int get_dirty_pages(struct inode *inode)
2479f8b2c1f9SJaegeuk Kim {
2480204706c7SJaegeuk Kim 	return atomic_read(&F2FS_I(inode)->dirty_pages);
2481f8b2c1f9SJaegeuk Kim }
2482f8b2c1f9SJaegeuk Kim 
24835ac206cfSNamjae Jeon static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
24845ac206cfSNamjae Jeon {
24853519e3f9SChao Yu 	unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
2486523be8a6SJaegeuk Kim 	unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
2487523be8a6SJaegeuk Kim 						sbi->log_blocks_per_seg;
2488523be8a6SJaegeuk Kim 
2489523be8a6SJaegeuk Kim 	return segs / sbi->segs_per_sec;
24905ac206cfSNamjae Jeon }
24915ac206cfSNamjae Jeon 
249239a53e0cSJaegeuk Kim static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
249339a53e0cSJaegeuk Kim {
24948b8343faSJaegeuk Kim 	return sbi->total_valid_block_count;
249539a53e0cSJaegeuk Kim }
249639a53e0cSJaegeuk Kim 
2497f83a2584SYunlei He static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
2498f83a2584SYunlei He {
2499f83a2584SYunlei He 	return sbi->discard_blks;
2500f83a2584SYunlei He }
2501f83a2584SYunlei He 
250239a53e0cSJaegeuk Kim static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
250339a53e0cSJaegeuk Kim {
250439a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
250539a53e0cSJaegeuk Kim 
250639a53e0cSJaegeuk Kim 	/* return NAT or SIT bitmap */
250739a53e0cSJaegeuk Kim 	if (flag == NAT_BITMAP)
250839a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
250939a53e0cSJaegeuk Kim 	else if (flag == SIT_BITMAP)
251039a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
251139a53e0cSJaegeuk Kim 
251239a53e0cSJaegeuk Kim 	return 0;
251339a53e0cSJaegeuk Kim }
251439a53e0cSJaegeuk Kim 
251555141486SWanpeng Li static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
251655141486SWanpeng Li {
251755141486SWanpeng Li 	return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
251855141486SWanpeng Li }
251955141486SWanpeng Li 
252039a53e0cSJaegeuk Kim static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
252139a53e0cSJaegeuk Kim {
252239a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
25234260c406SGustavo A. R. Silva 	void *tmp_ptr = &ckpt->sit_nat_version_bitmap;
25241dbe4152SChangman Lee 	int offset;
25251dbe4152SChangman Lee 
2526199bc3feSChao Yu 	if (is_set_ckpt_flags(sbi, CP_LARGE_NAT_BITMAP_FLAG)) {
2527199bc3feSChao Yu 		offset = (flag == SIT_BITMAP) ?
2528199bc3feSChao Yu 			le32_to_cpu(ckpt->nat_ver_bitmap_bytesize) : 0;
2529b471eb99SChao Yu 		/*
2530b471eb99SChao Yu 		 * if large_nat_bitmap feature is enabled, leave checksum
2531b471eb99SChao Yu 		 * protection for all nat/sit bitmaps.
2532b471eb99SChao Yu 		 */
25334260c406SGustavo A. R. Silva 		return tmp_ptr + offset + sizeof(__le32);
2534199bc3feSChao Yu 	}
2535199bc3feSChao Yu 
253655141486SWanpeng Li 	if (__cp_payload(sbi) > 0) {
25371dbe4152SChangman Lee 		if (flag == NAT_BITMAP)
2538280dfeaeSZhang Qilong 			return tmp_ptr;
25391dbe4152SChangman Lee 		else
254065b85cccSJaegeuk Kim 			return (unsigned char *)ckpt + F2FS_BLKSIZE;
25411dbe4152SChangman Lee 	} else {
25421dbe4152SChangman Lee 		offset = (flag == NAT_BITMAP) ?
254325ca923bSJaegeuk Kim 			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
25444260c406SGustavo A. R. Silva 		return tmp_ptr + offset;
254539a53e0cSJaegeuk Kim 	}
25461dbe4152SChangman Lee }
254739a53e0cSJaegeuk Kim 
254839a53e0cSJaegeuk Kim static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
254939a53e0cSJaegeuk Kim {
25508508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
255139a53e0cSJaegeuk Kim 
25528508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 2)
255339a53e0cSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
255439a53e0cSJaegeuk Kim 	return start_addr;
255539a53e0cSJaegeuk Kim }
255639a53e0cSJaegeuk Kim 
25578508e44aSJaegeuk Kim static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
25588508e44aSJaegeuk Kim {
25598508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
25608508e44aSJaegeuk Kim 
25618508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 1)
25628508e44aSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
25638508e44aSJaegeuk Kim 	return start_addr;
25648508e44aSJaegeuk Kim }
25658508e44aSJaegeuk Kim 
25668508e44aSJaegeuk Kim static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
25678508e44aSJaegeuk Kim {
25688508e44aSJaegeuk Kim 	sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
25698508e44aSJaegeuk Kim }
25708508e44aSJaegeuk Kim 
257139a53e0cSJaegeuk Kim static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
257239a53e0cSJaegeuk Kim {
257339a53e0cSJaegeuk Kim 	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
257439a53e0cSJaegeuk Kim }
257539a53e0cSJaegeuk Kim 
25763bac20a8SJaegeuk Kim extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
25770abd675eSChao Yu static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
2578000519f2SChao Yu 					struct inode *inode, bool is_inode)
257939a53e0cSJaegeuk Kim {
258039a53e0cSJaegeuk Kim 	block_t	valid_block_count;
2581a4c3ecaaSDaniel Rosenberg 	unsigned int valid_node_count, user_block_count;
2582af033b2aSChao Yu 	int err;
25830abd675eSChao Yu 
2584af033b2aSChao Yu 	if (is_inode) {
2585af033b2aSChao Yu 		if (inode) {
2586af033b2aSChao Yu 			err = dquot_alloc_inode(inode);
2587af033b2aSChao Yu 			if (err)
2588af033b2aSChao Yu 				return err;
2589af033b2aSChao Yu 		}
2590af033b2aSChao Yu 	} else {
2591af033b2aSChao Yu 		err = dquot_reserve_block(inode, 1);
2592af033b2aSChao Yu 		if (err)
2593af033b2aSChao Yu 			return err;
25940abd675eSChao Yu 	}
259539a53e0cSJaegeuk Kim 
2596c40e15a9SYangtao Li 	if (time_to_inject(sbi, FAULT_BLOCK))
2597812c6056SChao Yu 		goto enospc;
2598812c6056SChao Yu 
259939a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
260039a53e0cSJaegeuk Kim 
26017e65be49SJaegeuk Kim 	valid_block_count = sbi->total_valid_block_count +
26027e65be49SJaegeuk Kim 					sbi->current_reserved_blocks + 1;
26037e65be49SJaegeuk Kim 
2604a90a0884SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode, false))
260563189b78SChao Yu 		valid_block_count += F2FS_OPTION(sbi).root_reserved_blocks;
2606300a8429SChao Yu 
2607300a8429SChao Yu 	if (F2FS_IO_ALIGNED(sbi))
2608300a8429SChao Yu 		valid_block_count += sbi->blocks_per_seg *
2609300a8429SChao Yu 				SM_I(sbi)->additional_reserved_segments;
2610300a8429SChao Yu 
2611a4c3ecaaSDaniel Rosenberg 	user_block_count = sbi->user_block_count;
26124354994fSDaniel Rosenberg 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2613a4c3ecaaSDaniel Rosenberg 		user_block_count -= sbi->unusable_block_count;
26147e65be49SJaegeuk Kim 
2615a4c3ecaaSDaniel Rosenberg 	if (unlikely(valid_block_count > user_block_count)) {
261639a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
26170abd675eSChao Yu 		goto enospc;
261839a53e0cSJaegeuk Kim 	}
261939a53e0cSJaegeuk Kim 
2620ef86d709SGu Zheng 	valid_node_count = sbi->total_valid_node_count + 1;
2621cfb271d4SChao Yu 	if (unlikely(valid_node_count > sbi->total_node_count)) {
262239a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
26230abd675eSChao Yu 		goto enospc;
262439a53e0cSJaegeuk Kim 	}
262539a53e0cSJaegeuk Kim 
2626ef86d709SGu Zheng 	sbi->total_valid_node_count++;
2627ef86d709SGu Zheng 	sbi->total_valid_block_count++;
262839a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
262939a53e0cSJaegeuk Kim 
26300abd675eSChao Yu 	if (inode) {
26310abd675eSChao Yu 		if (is_inode)
26320abd675eSChao Yu 			f2fs_mark_inode_dirty_sync(inode, true);
26330abd675eSChao Yu 		else
26340abd675eSChao Yu 			f2fs_i_blocks_write(inode, 1, true, true);
26350abd675eSChao Yu 	}
26360abd675eSChao Yu 
263741382ec4SJaegeuk Kim 	percpu_counter_inc(&sbi->alloc_valid_block_count);
26380abd675eSChao Yu 	return 0;
26390abd675eSChao Yu 
26400abd675eSChao Yu enospc:
2641af033b2aSChao Yu 	if (is_inode) {
2642af033b2aSChao Yu 		if (inode)
2643af033b2aSChao Yu 			dquot_free_inode(inode);
2644af033b2aSChao Yu 	} else {
26450abd675eSChao Yu 		dquot_release_reservation_block(inode, 1);
2646af033b2aSChao Yu 	}
26470abd675eSChao Yu 	return -ENOSPC;
264839a53e0cSJaegeuk Kim }
264939a53e0cSJaegeuk Kim 
265039a53e0cSJaegeuk Kim static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
2651000519f2SChao Yu 					struct inode *inode, bool is_inode)
265239a53e0cSJaegeuk Kim {
265339a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
265439a53e0cSJaegeuk Kim 
26554d17e6feSChao Yu 	if (unlikely(!sbi->total_valid_block_count ||
26564d17e6feSChao Yu 			!sbi->total_valid_node_count)) {
26574d17e6feSChao Yu 		f2fs_warn(sbi, "dec_valid_node_count: inconsistent block counts, total_valid_block:%u, total_valid_node:%u",
26584d17e6feSChao Yu 			  sbi->total_valid_block_count,
26594d17e6feSChao Yu 			  sbi->total_valid_node_count);
26604d17e6feSChao Yu 		set_sbi_flag(sbi, SBI_NEED_FSCK);
26614d17e6feSChao Yu 	} else {
2662ef86d709SGu Zheng 		sbi->total_valid_block_count--;
26634d17e6feSChao Yu 		sbi->total_valid_node_count--;
26644d17e6feSChao Yu 	}
26654d17e6feSChao Yu 
266680d42145SYunlong Song 	if (sbi->reserved_blocks &&
266780d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
266880d42145SYunlong Song 		sbi->current_reserved_blocks++;
266939a53e0cSJaegeuk Kim 
267039a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
26710abd675eSChao Yu 
2672ea6d7e72SChao Yu 	if (is_inode) {
2673af033b2aSChao Yu 		dquot_free_inode(inode);
2674ea6d7e72SChao Yu 	} else {
2675ea6d7e72SChao Yu 		if (unlikely(inode->i_blocks == 0)) {
2676097a7686SChao Yu 			f2fs_warn(sbi, "dec_valid_node_count: inconsistent i_blocks, ino:%lu, iblocks:%llu",
2677ea6d7e72SChao Yu 				  inode->i_ino,
2678ea6d7e72SChao Yu 				  (unsigned long long)inode->i_blocks);
2679ea6d7e72SChao Yu 			set_sbi_flag(sbi, SBI_NEED_FSCK);
2680ea6d7e72SChao Yu 			return;
2681ea6d7e72SChao Yu 		}
26820abd675eSChao Yu 		f2fs_i_blocks_write(inode, 1, false, true);
268339a53e0cSJaegeuk Kim 	}
2684ea6d7e72SChao Yu }
268539a53e0cSJaegeuk Kim 
268639a53e0cSJaegeuk Kim static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
268739a53e0cSJaegeuk Kim {
26888b8343faSJaegeuk Kim 	return sbi->total_valid_node_count;
268939a53e0cSJaegeuk Kim }
269039a53e0cSJaegeuk Kim 
269139a53e0cSJaegeuk Kim static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
269239a53e0cSJaegeuk Kim {
2693513c5f37SJaegeuk Kim 	percpu_counter_inc(&sbi->total_valid_inode_count);
269439a53e0cSJaegeuk Kim }
269539a53e0cSJaegeuk Kim 
26960e80220aSJaegeuk Kim static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
269739a53e0cSJaegeuk Kim {
2698513c5f37SJaegeuk Kim 	percpu_counter_dec(&sbi->total_valid_inode_count);
269939a53e0cSJaegeuk Kim }
270039a53e0cSJaegeuk Kim 
2701513c5f37SJaegeuk Kim static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
270239a53e0cSJaegeuk Kim {
2703513c5f37SJaegeuk Kim 	return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
270439a53e0cSJaegeuk Kim }
270539a53e0cSJaegeuk Kim 
2706a56c7c6fSJaegeuk Kim static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
2707a56c7c6fSJaegeuk Kim 						pgoff_t index, bool for_write)
2708a56c7c6fSJaegeuk Kim {
270982cf4f13SChao Yu 	struct page *page;
2710df808180SMatthew Wilcox (Oracle) 	unsigned int flags;
2711cac5a3d8SDongOh Shin 
27127fa750a1SArnd Bergmann 	if (IS_ENABLED(CONFIG_F2FS_FAULT_INJECTION)) {
271382cf4f13SChao Yu 		if (!for_write)
271482cf4f13SChao Yu 			page = find_get_page_flags(mapping, index,
271582cf4f13SChao Yu 							FGP_LOCK | FGP_ACCESSED);
271682cf4f13SChao Yu 		else
271782cf4f13SChao Yu 			page = find_lock_page(mapping, index);
2718c41f3cc3SJaegeuk Kim 		if (page)
2719c41f3cc3SJaegeuk Kim 			return page;
2720c41f3cc3SJaegeuk Kim 
2721c40e15a9SYangtao Li 		if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC))
2722c41f3cc3SJaegeuk Kim 			return NULL;
272355523519SChao Yu 	}
27247fa750a1SArnd Bergmann 
2725a56c7c6fSJaegeuk Kim 	if (!for_write)
2726a56c7c6fSJaegeuk Kim 		return grab_cache_page(mapping, index);
2727df808180SMatthew Wilcox (Oracle) 
2728df808180SMatthew Wilcox (Oracle) 	flags = memalloc_nofs_save();
2729b7446e7cSMatthew Wilcox (Oracle) 	page = grab_cache_page_write_begin(mapping, index);
2730df808180SMatthew Wilcox (Oracle) 	memalloc_nofs_restore(flags);
2731df808180SMatthew Wilcox (Oracle) 
2732df808180SMatthew Wilcox (Oracle) 	return page;
2733a56c7c6fSJaegeuk Kim }
2734a56c7c6fSJaegeuk Kim 
273501eccef7SChao Yu static inline struct page *f2fs_pagecache_get_page(
273601eccef7SChao Yu 				struct address_space *mapping, pgoff_t index,
273701eccef7SChao Yu 				int fgp_flags, gfp_t gfp_mask)
273801eccef7SChao Yu {
2739c40e15a9SYangtao Li 	if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_GET))
274001eccef7SChao Yu 		return NULL;
27417fa750a1SArnd Bergmann 
274201eccef7SChao Yu 	return pagecache_get_page(mapping, index, fgp_flags, gfp_mask);
274301eccef7SChao Yu }
274401eccef7SChao Yu 
274539a53e0cSJaegeuk Kim static inline void f2fs_put_page(struct page *page, int unlock)
274639a53e0cSJaegeuk Kim {
2747031fa8ccSJaegeuk Kim 	if (!page)
274839a53e0cSJaegeuk Kim 		return;
274939a53e0cSJaegeuk Kim 
275039a53e0cSJaegeuk Kim 	if (unlock) {
27519850cf4aSJaegeuk Kim 		f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
275239a53e0cSJaegeuk Kim 		unlock_page(page);
275339a53e0cSJaegeuk Kim 	}
275409cbfeafSKirill A. Shutemov 	put_page(page);
275539a53e0cSJaegeuk Kim }
275639a53e0cSJaegeuk Kim 
275739a53e0cSJaegeuk Kim static inline void f2fs_put_dnode(struct dnode_of_data *dn)
275839a53e0cSJaegeuk Kim {
275939a53e0cSJaegeuk Kim 	if (dn->node_page)
276039a53e0cSJaegeuk Kim 		f2fs_put_page(dn->node_page, 1);
276139a53e0cSJaegeuk Kim 	if (dn->inode_page && dn->node_page != dn->inode_page)
276239a53e0cSJaegeuk Kim 		f2fs_put_page(dn->inode_page, 0);
276339a53e0cSJaegeuk Kim 	dn->node_page = NULL;
276439a53e0cSJaegeuk Kim 	dn->inode_page = NULL;
276539a53e0cSJaegeuk Kim }
276639a53e0cSJaegeuk Kim 
276739a53e0cSJaegeuk Kim static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
2768e8512d2eSGu Zheng 					size_t size)
276939a53e0cSJaegeuk Kim {
2770e8512d2eSGu Zheng 	return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
277139a53e0cSJaegeuk Kim }
277239a53e0cSJaegeuk Kim 
277332410577SChao Yu static inline void *f2fs_kmem_cache_alloc_nofail(struct kmem_cache *cachep,
27747bd59381SGu Zheng 						gfp_t flags)
27757bd59381SGu Zheng {
27767bd59381SGu Zheng 	void *entry;
27777bd59381SGu Zheng 
277880c54505SJaegeuk Kim 	entry = kmem_cache_alloc(cachep, flags);
277980c54505SJaegeuk Kim 	if (!entry)
278080c54505SJaegeuk Kim 		entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
27817bd59381SGu Zheng 	return entry;
27827bd59381SGu Zheng }
27837bd59381SGu Zheng 
278432410577SChao Yu static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
278532410577SChao Yu 			gfp_t flags, bool nofail, struct f2fs_sb_info *sbi)
278632410577SChao Yu {
278732410577SChao Yu 	if (nofail)
278832410577SChao Yu 		return f2fs_kmem_cache_alloc_nofail(cachep, flags);
278932410577SChao Yu 
2790c40e15a9SYangtao Li 	if (time_to_inject(sbi, FAULT_SLAB_ALLOC))
279132410577SChao Yu 		return NULL;
279232410577SChao Yu 
279332410577SChao Yu 	return kmem_cache_alloc(cachep, flags);
279432410577SChao Yu }
279532410577SChao Yu 
2796493720a4SChao Yu static inline bool is_inflight_io(struct f2fs_sb_info *sbi, int type)
27975f9abab4SJaegeuk Kim {
27985f9abab4SJaegeuk Kim 	if (get_pages(sbi, F2FS_RD_DATA) || get_pages(sbi, F2FS_RD_NODE) ||
27995f9abab4SJaegeuk Kim 		get_pages(sbi, F2FS_RD_META) || get_pages(sbi, F2FS_WB_DATA) ||
2800fef4129eSChao Yu 		get_pages(sbi, F2FS_WB_CP_DATA) ||
2801fef4129eSChao Yu 		get_pages(sbi, F2FS_DIO_READ) ||
280211ac8ef8SJaegeuk Kim 		get_pages(sbi, F2FS_DIO_WRITE))
2803493720a4SChao Yu 		return true;
280411ac8ef8SJaegeuk Kim 
280556659ce8SSahitya Tummala 	if (type != DISCARD_TIME && SM_I(sbi) && SM_I(sbi)->dcc_info &&
280611ac8ef8SJaegeuk Kim 			atomic_read(&SM_I(sbi)->dcc_info->queued_discard))
2807493720a4SChao Yu 		return true;
280811ac8ef8SJaegeuk Kim 
280911ac8ef8SJaegeuk Kim 	if (SM_I(sbi) && SM_I(sbi)->fcc_info &&
281072691af6SJaegeuk Kim 			atomic_read(&SM_I(sbi)->fcc_info->queued_flush))
2811493720a4SChao Yu 		return true;
2812493720a4SChao Yu 	return false;
2813493720a4SChao Yu }
2814493720a4SChao Yu 
2815493720a4SChao Yu static inline bool is_idle(struct f2fs_sb_info *sbi, int type)
2816493720a4SChao Yu {
2817493720a4SChao Yu 	if (sbi->gc_mode == GC_URGENT_HIGH)
2818493720a4SChao Yu 		return true;
2819493720a4SChao Yu 
2820493720a4SChao Yu 	if (is_inflight_io(sbi, type))
28215f9abab4SJaegeuk Kim 		return false;
282211ac8ef8SJaegeuk Kim 
2823d98af5f4SDaeho Jeong 	if (sbi->gc_mode == GC_URGENT_MID)
2824d98af5f4SDaeho Jeong 		return true;
2825d98af5f4SDaeho Jeong 
28260e5e8111SDaeho Jeong 	if (sbi->gc_mode == GC_URGENT_LOW &&
28270e5e8111SDaeho Jeong 			(type == DISCARD_TIME || type == GC_TIME))
28280e5e8111SDaeho Jeong 		return true;
28290e5e8111SDaeho Jeong 
28305f9abab4SJaegeuk Kim 	return f2fs_time_over(sbi, type);
28315f9abab4SJaegeuk Kim }
28325f9abab4SJaegeuk Kim 
28339be32d72SJaegeuk Kim static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
28349be32d72SJaegeuk Kim 				unsigned long index, void *item)
28359be32d72SJaegeuk Kim {
28369be32d72SJaegeuk Kim 	while (radix_tree_insert(root, index, item))
28379be32d72SJaegeuk Kim 		cond_resched();
28389be32d72SJaegeuk Kim }
28399be32d72SJaegeuk Kim 
284039a53e0cSJaegeuk Kim #define RAW_IS_INODE(p)	((p)->footer.nid == (p)->footer.ino)
284139a53e0cSJaegeuk Kim 
284239a53e0cSJaegeuk Kim static inline bool IS_INODE(struct page *page)
284339a53e0cSJaegeuk Kim {
284445590710SGu Zheng 	struct f2fs_node *p = F2FS_NODE(page);
2845cac5a3d8SDongOh Shin 
284639a53e0cSJaegeuk Kim 	return RAW_IS_INODE(p);
284739a53e0cSJaegeuk Kim }
284839a53e0cSJaegeuk Kim 
28497a2af766SChao Yu static inline int offset_in_addr(struct f2fs_inode *i)
28507a2af766SChao Yu {
28517a2af766SChao Yu 	return (i->i_inline & F2FS_EXTRA_ATTR) ?
28527a2af766SChao Yu 			(le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0;
28537a2af766SChao Yu }
28547a2af766SChao Yu 
285539a53e0cSJaegeuk Kim static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
285639a53e0cSJaegeuk Kim {
285739a53e0cSJaegeuk Kim 	return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
285839a53e0cSJaegeuk Kim }
285939a53e0cSJaegeuk Kim 
28607a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode);
2861a2ced1ceSChao Yu static inline block_t data_blkaddr(struct inode *inode,
28627a2af766SChao Yu 			struct page *node_page, unsigned int offset)
286339a53e0cSJaegeuk Kim {
286439a53e0cSJaegeuk Kim 	struct f2fs_node *raw_node;
286539a53e0cSJaegeuk Kim 	__le32 *addr_array;
28667a2af766SChao Yu 	int base = 0;
28677a2af766SChao Yu 	bool is_inode = IS_INODE(node_page);
2868cac5a3d8SDongOh Shin 
286945590710SGu Zheng 	raw_node = F2FS_NODE(node_page);
28707a2af766SChao Yu 
2871979f492fSLiFan 	if (is_inode) {
2872979f492fSLiFan 		if (!inode)
28737a88ddb5SChao Yu 			/* from GC path only */
28747a2af766SChao Yu 			base = offset_in_addr(&raw_node->i);
2875979f492fSLiFan 		else if (f2fs_has_extra_attr(inode))
28767a2af766SChao Yu 			base = get_extra_isize(inode);
28777a2af766SChao Yu 	}
28787a2af766SChao Yu 
287939a53e0cSJaegeuk Kim 	addr_array = blkaddr_in_node(raw_node);
28807a2af766SChao Yu 	return le32_to_cpu(addr_array[base + offset]);
288139a53e0cSJaegeuk Kim }
288239a53e0cSJaegeuk Kim 
2883a2ced1ceSChao Yu static inline block_t f2fs_data_blkaddr(struct dnode_of_data *dn)
2884a2ced1ceSChao Yu {
2885a2ced1ceSChao Yu 	return data_blkaddr(dn->inode, dn->node_page, dn->ofs_in_node);
2886a2ced1ceSChao Yu }
2887a2ced1ceSChao Yu 
288839a53e0cSJaegeuk Kim static inline int f2fs_test_bit(unsigned int nr, char *addr)
288939a53e0cSJaegeuk Kim {
289039a53e0cSJaegeuk Kim 	int mask;
289139a53e0cSJaegeuk Kim 
289239a53e0cSJaegeuk Kim 	addr += (nr >> 3);
2893447286ebSYangtao Li 	mask = BIT(7 - (nr & 0x07));
289439a53e0cSJaegeuk Kim 	return mask & *addr;
289539a53e0cSJaegeuk Kim }
289639a53e0cSJaegeuk Kim 
2897a66cdd98SJaegeuk Kim static inline void f2fs_set_bit(unsigned int nr, char *addr)
2898a66cdd98SJaegeuk Kim {
2899a66cdd98SJaegeuk Kim 	int mask;
2900a66cdd98SJaegeuk Kim 
2901a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2902447286ebSYangtao Li 	mask = BIT(7 - (nr & 0x07));
2903a66cdd98SJaegeuk Kim 	*addr |= mask;
2904a66cdd98SJaegeuk Kim }
2905a66cdd98SJaegeuk Kim 
2906a66cdd98SJaegeuk Kim static inline void f2fs_clear_bit(unsigned int nr, char *addr)
2907a66cdd98SJaegeuk Kim {
2908a66cdd98SJaegeuk Kim 	int mask;
2909a66cdd98SJaegeuk Kim 
2910a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2911447286ebSYangtao Li 	mask = BIT(7 - (nr & 0x07));
2912a66cdd98SJaegeuk Kim 	*addr &= ~mask;
2913a66cdd98SJaegeuk Kim }
2914a66cdd98SJaegeuk Kim 
291552aca074SGu Zheng static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
291639a53e0cSJaegeuk Kim {
291739a53e0cSJaegeuk Kim 	int mask;
291839a53e0cSJaegeuk Kim 	int ret;
291939a53e0cSJaegeuk Kim 
292039a53e0cSJaegeuk Kim 	addr += (nr >> 3);
2921447286ebSYangtao Li 	mask = BIT(7 - (nr & 0x07));
292239a53e0cSJaegeuk Kim 	ret = mask & *addr;
292339a53e0cSJaegeuk Kim 	*addr |= mask;
292439a53e0cSJaegeuk Kim 	return ret;
292539a53e0cSJaegeuk Kim }
292639a53e0cSJaegeuk Kim 
292752aca074SGu Zheng static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
292839a53e0cSJaegeuk Kim {
292939a53e0cSJaegeuk Kim 	int mask;
293039a53e0cSJaegeuk Kim 	int ret;
293139a53e0cSJaegeuk Kim 
293239a53e0cSJaegeuk Kim 	addr += (nr >> 3);
2933447286ebSYangtao Li 	mask = BIT(7 - (nr & 0x07));
293439a53e0cSJaegeuk Kim 	ret = mask & *addr;
293539a53e0cSJaegeuk Kim 	*addr &= ~mask;
293639a53e0cSJaegeuk Kim 	return ret;
293739a53e0cSJaegeuk Kim }
293839a53e0cSJaegeuk Kim 
2939c6ac4c0eSGu Zheng static inline void f2fs_change_bit(unsigned int nr, char *addr)
2940c6ac4c0eSGu Zheng {
2941c6ac4c0eSGu Zheng 	int mask;
2942c6ac4c0eSGu Zheng 
2943c6ac4c0eSGu Zheng 	addr += (nr >> 3);
2944447286ebSYangtao Li 	mask = BIT(7 - (nr & 0x07));
2945c6ac4c0eSGu Zheng 	*addr ^= mask;
2946c6ac4c0eSGu Zheng }
2947c6ac4c0eSGu Zheng 
294859c84408SChao Yu /*
294936098557SEric Biggers  * On-disk inode flags (f2fs_inode::i_flags)
295059c84408SChao Yu  */
29514c8ff709SChao Yu #define F2FS_COMPR_FL			0x00000004 /* Compress file */
295259c84408SChao Yu #define F2FS_SYNC_FL			0x00000008 /* Synchronous updates */
295359c84408SChao Yu #define F2FS_IMMUTABLE_FL		0x00000010 /* Immutable file */
295459c84408SChao Yu #define F2FS_APPEND_FL			0x00000020 /* writes to file may only append */
295559c84408SChao Yu #define F2FS_NODUMP_FL			0x00000040 /* do not dump file */
295659c84408SChao Yu #define F2FS_NOATIME_FL			0x00000080 /* do not update atime */
29574c8ff709SChao Yu #define F2FS_NOCOMP_FL			0x00000400 /* Don't compress */
295859c84408SChao Yu #define F2FS_INDEX_FL			0x00001000 /* hash-indexed directory */
295959c84408SChao Yu #define F2FS_DIRSYNC_FL			0x00010000 /* dirsync behaviour (directories only) */
296059c84408SChao Yu #define F2FS_PROJINHERIT_FL		0x20000000 /* Create with parents projid */
29612c2eb7a3SDaniel Rosenberg #define F2FS_CASEFOLD_FL		0x40000000 /* Casefolded file */
296259c84408SChao Yu 
296359c84408SChao Yu /* Flags that should be inherited by new inodes from their parent. */
296436098557SEric Biggers #define F2FS_FL_INHERITED (F2FS_SYNC_FL | F2FS_NODUMP_FL | F2FS_NOATIME_FL | \
29652c2eb7a3SDaniel Rosenberg 			   F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL | \
2966787caf1bSSheng Yong 			   F2FS_CASEFOLD_FL)
296759c84408SChao Yu 
296859c84408SChao Yu /* Flags that are appropriate for regular files (all but dir-specific ones). */
29692c2eb7a3SDaniel Rosenberg #define F2FS_REG_FLMASK		(~(F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL | \
29702c2eb7a3SDaniel Rosenberg 				F2FS_CASEFOLD_FL))
297159c84408SChao Yu 
297259c84408SChao Yu /* Flags that are appropriate for non-directories/regular files. */
297359c84408SChao Yu #define F2FS_OTHER_FLMASK	(F2FS_NODUMP_FL | F2FS_NOATIME_FL)
29745c57132eSChao Yu 
29755c57132eSChao Yu static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
29765c57132eSChao Yu {
29775c57132eSChao Yu 	if (S_ISDIR(mode))
29785c57132eSChao Yu 		return flags;
29795c57132eSChao Yu 	else if (S_ISREG(mode))
29805c57132eSChao Yu 		return flags & F2FS_REG_FLMASK;
29815c57132eSChao Yu 	else
29825c57132eSChao Yu 		return flags & F2FS_OTHER_FLMASK;
29835c57132eSChao Yu }
29845c57132eSChao Yu 
2985205b9822SJaegeuk Kim static inline void __mark_inode_dirty_flag(struct inode *inode,
2986205b9822SJaegeuk Kim 						int flag, bool set)
298739a53e0cSJaegeuk Kim {
2988205b9822SJaegeuk Kim 	switch (flag) {
2989205b9822SJaegeuk Kim 	case FI_INLINE_XATTR:
2990205b9822SJaegeuk Kim 	case FI_INLINE_DATA:
2991205b9822SJaegeuk Kim 	case FI_INLINE_DENTRY:
29929ac1e2d8SDaeho Jeong 	case FI_NEW_INODE:
2993205b9822SJaegeuk Kim 		if (set)
2994205b9822SJaegeuk Kim 			return;
2995df561f66SGustavo A. R. Silva 		fallthrough;
2996205b9822SJaegeuk Kim 	case FI_DATA_EXIST:
2997205b9822SJaegeuk Kim 	case FI_INLINE_DOTS:
29981ad71a27SJaegeuk Kim 	case FI_PIN_FILE:
2999c6140415SJaegeuk Kim 	case FI_COMPRESS_RELEASED:
30007c45729aSJaegeuk Kim 		f2fs_mark_inode_dirty_sync(inode, true);
3001205b9822SJaegeuk Kim 	}
300239a53e0cSJaegeuk Kim }
300339a53e0cSJaegeuk Kim 
300491942321SJaegeuk Kim static inline void set_inode_flag(struct inode *inode, int flag)
300539a53e0cSJaegeuk Kim {
300658f7e00fSYufen Yu 	set_bit(flag, F2FS_I(inode)->flags);
3007205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, true);
300839a53e0cSJaegeuk Kim }
300939a53e0cSJaegeuk Kim 
301091942321SJaegeuk Kim static inline int is_inode_flag_set(struct inode *inode, int flag)
301139a53e0cSJaegeuk Kim {
30127653b9d8SChao Yu 	return test_bit(flag, F2FS_I(inode)->flags);
301339a53e0cSJaegeuk Kim }
301439a53e0cSJaegeuk Kim 
301591942321SJaegeuk Kim static inline void clear_inode_flag(struct inode *inode, int flag)
301639a53e0cSJaegeuk Kim {
301758f7e00fSYufen Yu 	clear_bit(flag, F2FS_I(inode)->flags);
3018205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, false);
301939a53e0cSJaegeuk Kim }
302039a53e0cSJaegeuk Kim 
302195ae251fSEric Biggers static inline bool f2fs_verity_in_progress(struct inode *inode)
302295ae251fSEric Biggers {
302395ae251fSEric Biggers 	return IS_ENABLED(CONFIG_FS_VERITY) &&
302495ae251fSEric Biggers 	       is_inode_flag_set(inode, FI_VERITY_IN_PROGRESS);
302595ae251fSEric Biggers }
302695ae251fSEric Biggers 
302791942321SJaegeuk Kim static inline void set_acl_inode(struct inode *inode, umode_t mode)
3028444c580fSJaegeuk Kim {
302991942321SJaegeuk Kim 	F2FS_I(inode)->i_acl_mode = mode;
303091942321SJaegeuk Kim 	set_inode_flag(inode, FI_ACL_MODE);
30317c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, false);
303239a53e0cSJaegeuk Kim }
303339a53e0cSJaegeuk Kim 
3034a1961246SJaegeuk Kim static inline void f2fs_i_links_write(struct inode *inode, bool inc)
3035a1961246SJaegeuk Kim {
3036a1961246SJaegeuk Kim 	if (inc)
3037a1961246SJaegeuk Kim 		inc_nlink(inode);
3038a1961246SJaegeuk Kim 	else
3039a1961246SJaegeuk Kim 		drop_nlink(inode);
30407c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3041a1961246SJaegeuk Kim }
3042a1961246SJaegeuk Kim 
30438edd03c8SJaegeuk Kim static inline void f2fs_i_blocks_write(struct inode *inode,
30440abd675eSChao Yu 					block_t diff, bool add, bool claim)
30458edd03c8SJaegeuk Kim {
304626de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
304726de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
304826de9b11SJaegeuk Kim 
30490abd675eSChao Yu 	/* add = 1, claim = 1 should be dquot_reserve_block in pair */
30500abd675eSChao Yu 	if (add) {
30510abd675eSChao Yu 		if (claim)
30520abd675eSChao Yu 			dquot_claim_block(inode, diff);
30530abd675eSChao Yu 		else
30540abd675eSChao Yu 			dquot_alloc_block_nofail(inode, diff);
30550abd675eSChao Yu 	} else {
30560abd675eSChao Yu 		dquot_free_block(inode, diff);
30570abd675eSChao Yu 	}
30580abd675eSChao Yu 
30597c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
306026de9b11SJaegeuk Kim 	if (clean || recover)
306126de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
30628edd03c8SJaegeuk Kim }
30638edd03c8SJaegeuk Kim 
30644d8d45dfSDaeho Jeong static inline bool f2fs_is_atomic_file(struct inode *inode);
30654d8d45dfSDaeho Jeong 
3066fc9581c8SJaegeuk Kim static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
3067fc9581c8SJaegeuk Kim {
306826de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
306926de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
307026de9b11SJaegeuk Kim 
3071fc9581c8SJaegeuk Kim 	if (i_size_read(inode) == i_size)
3072fc9581c8SJaegeuk Kim 		return;
3073fc9581c8SJaegeuk Kim 
3074fc9581c8SJaegeuk Kim 	i_size_write(inode, i_size);
30754d8d45dfSDaeho Jeong 
30764d8d45dfSDaeho Jeong 	if (f2fs_is_atomic_file(inode))
30774d8d45dfSDaeho Jeong 		return;
30784d8d45dfSDaeho Jeong 
30797c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
308026de9b11SJaegeuk Kim 	if (clean || recover)
308126de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
308226de9b11SJaegeuk Kim }
308326de9b11SJaegeuk Kim 
3084205b9822SJaegeuk Kim static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
3085205b9822SJaegeuk Kim {
3086205b9822SJaegeuk Kim 	F2FS_I(inode)->i_current_depth = depth;
30877c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3088205b9822SJaegeuk Kim }
3089205b9822SJaegeuk Kim 
30901ad71a27SJaegeuk Kim static inline void f2fs_i_gc_failures_write(struct inode *inode,
30911ad71a27SJaegeuk Kim 					unsigned int count)
30921ad71a27SJaegeuk Kim {
30932ef79ecbSChao Yu 	F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN] = count;
30941ad71a27SJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
30951ad71a27SJaegeuk Kim }
30961ad71a27SJaegeuk Kim 
3097205b9822SJaegeuk Kim static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
3098205b9822SJaegeuk Kim {
3099205b9822SJaegeuk Kim 	F2FS_I(inode)->i_xattr_nid = xnid;
31007c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3101205b9822SJaegeuk Kim }
3102205b9822SJaegeuk Kim 
3103205b9822SJaegeuk Kim static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
3104205b9822SJaegeuk Kim {
3105205b9822SJaegeuk Kim 	F2FS_I(inode)->i_pino = pino;
31067c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3107205b9822SJaegeuk Kim }
3108205b9822SJaegeuk Kim 
310991942321SJaegeuk Kim static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
3110444c580fSJaegeuk Kim {
3111205b9822SJaegeuk Kim 	struct f2fs_inode_info *fi = F2FS_I(inode);
3112205b9822SJaegeuk Kim 
3113444c580fSJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_XATTR)
31147653b9d8SChao Yu 		set_bit(FI_INLINE_XATTR, fi->flags);
31151001b347SHuajun Li 	if (ri->i_inline & F2FS_INLINE_DATA)
31167653b9d8SChao Yu 		set_bit(FI_INLINE_DATA, fi->flags);
311734d67debSChao Yu 	if (ri->i_inline & F2FS_INLINE_DENTRY)
31187653b9d8SChao Yu 		set_bit(FI_INLINE_DENTRY, fi->flags);
3119b3d208f9SJaegeuk Kim 	if (ri->i_inline & F2FS_DATA_EXIST)
31207653b9d8SChao Yu 		set_bit(FI_DATA_EXIST, fi->flags);
3121510022a8SJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_DOTS)
31227653b9d8SChao Yu 		set_bit(FI_INLINE_DOTS, fi->flags);
31237a2af766SChao Yu 	if (ri->i_inline & F2FS_EXTRA_ATTR)
31247653b9d8SChao Yu 		set_bit(FI_EXTRA_ATTR, fi->flags);
31251ad71a27SJaegeuk Kim 	if (ri->i_inline & F2FS_PIN_FILE)
31267653b9d8SChao Yu 		set_bit(FI_PIN_FILE, fi->flags);
3127c6140415SJaegeuk Kim 	if (ri->i_inline & F2FS_COMPRESS_RELEASED)
3128c6140415SJaegeuk Kim 		set_bit(FI_COMPRESS_RELEASED, fi->flags);
3129444c580fSJaegeuk Kim }
3130444c580fSJaegeuk Kim 
313191942321SJaegeuk Kim static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
3132444c580fSJaegeuk Kim {
3133444c580fSJaegeuk Kim 	ri->i_inline = 0;
3134444c580fSJaegeuk Kim 
313591942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_XATTR))
3136444c580fSJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_XATTR;
313791942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DATA))
31381001b347SHuajun Li 		ri->i_inline |= F2FS_INLINE_DATA;
313991942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
314034d67debSChao Yu 		ri->i_inline |= F2FS_INLINE_DENTRY;
314191942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_DATA_EXIST))
3142b3d208f9SJaegeuk Kim 		ri->i_inline |= F2FS_DATA_EXIST;
314391942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DOTS))
3144510022a8SJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_DOTS;
31457a2af766SChao Yu 	if (is_inode_flag_set(inode, FI_EXTRA_ATTR))
31467a2af766SChao Yu 		ri->i_inline |= F2FS_EXTRA_ATTR;
31471ad71a27SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_PIN_FILE))
31481ad71a27SJaegeuk Kim 		ri->i_inline |= F2FS_PIN_FILE;
3149c6140415SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
3150c6140415SJaegeuk Kim 		ri->i_inline |= F2FS_COMPRESS_RELEASED;
31517a2af766SChao Yu }
31527a2af766SChao Yu 
31537a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode)
31547a2af766SChao Yu {
31557a2af766SChao Yu 	return is_inode_flag_set(inode, FI_EXTRA_ATTR);
3156444c580fSJaegeuk Kim }
3157444c580fSJaegeuk Kim 
3158987c7c31SChao Yu static inline int f2fs_has_inline_xattr(struct inode *inode)
3159987c7c31SChao Yu {
316091942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_XATTR);
3161987c7c31SChao Yu }
3162987c7c31SChao Yu 
31634c8ff709SChao Yu static inline int f2fs_compressed_file(struct inode *inode)
31644c8ff709SChao Yu {
31654c8ff709SChao Yu 	return S_ISREG(inode->i_mode) &&
31664c8ff709SChao Yu 		is_inode_flag_set(inode, FI_COMPRESSED_FILE);
31674c8ff709SChao Yu }
31684c8ff709SChao Yu 
3169602a16d5SDaeho Jeong static inline bool f2fs_need_compress_data(struct inode *inode)
3170602a16d5SDaeho Jeong {
3171602a16d5SDaeho Jeong 	int compress_mode = F2FS_OPTION(F2FS_I_SB(inode)).compress_mode;
3172602a16d5SDaeho Jeong 
3173602a16d5SDaeho Jeong 	if (!f2fs_compressed_file(inode))
3174602a16d5SDaeho Jeong 		return false;
3175602a16d5SDaeho Jeong 
3176602a16d5SDaeho Jeong 	if (compress_mode == COMPR_MODE_FS)
3177602a16d5SDaeho Jeong 		return true;
3178602a16d5SDaeho Jeong 	else if (compress_mode == COMPR_MODE_USER &&
3179602a16d5SDaeho Jeong 			is_inode_flag_set(inode, FI_ENABLE_COMPRESS))
3180602a16d5SDaeho Jeong 		return true;
3181602a16d5SDaeho Jeong 
3182602a16d5SDaeho Jeong 	return false;
3183602a16d5SDaeho Jeong }
3184602a16d5SDaeho Jeong 
318581ca7350SChao Yu static inline unsigned int addrs_per_inode(struct inode *inode)
3186de93653fSJaegeuk Kim {
3187d02a6e61SChao Yu 	unsigned int addrs = CUR_ADDRS_PER_INODE(inode) -
3188d02a6e61SChao Yu 				get_inline_xattr_addrs(inode);
31894c8ff709SChao Yu 
31904c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
31914c8ff709SChao Yu 		return addrs;
31924c8ff709SChao Yu 	return ALIGN_DOWN(addrs, F2FS_I(inode)->i_cluster_size);
3193d02a6e61SChao Yu }
3194d02a6e61SChao Yu 
3195d02a6e61SChao Yu static inline unsigned int addrs_per_block(struct inode *inode)
3196d02a6e61SChao Yu {
31974c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
31984c8ff709SChao Yu 		return DEF_ADDRS_PER_BLOCK;
31994c8ff709SChao Yu 	return ALIGN_DOWN(DEF_ADDRS_PER_BLOCK, F2FS_I(inode)->i_cluster_size);
3200de93653fSJaegeuk Kim }
3201de93653fSJaegeuk Kim 
32026afc662eSChao Yu static inline void *inline_xattr_addr(struct inode *inode, struct page *page)
320365985d93SJaegeuk Kim {
3204695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
3205cac5a3d8SDongOh Shin 
320665985d93SJaegeuk Kim 	return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
3207b323fd28SChao Yu 					get_inline_xattr_addrs(inode)]);
320865985d93SJaegeuk Kim }
320965985d93SJaegeuk Kim 
321065985d93SJaegeuk Kim static inline int inline_xattr_size(struct inode *inode)
321165985d93SJaegeuk Kim {
3212622927f3SChao Yu 	if (f2fs_has_inline_xattr(inode))
32136afc662eSChao Yu 		return get_inline_xattr_addrs(inode) * sizeof(__le32);
3214622927f3SChao Yu 	return 0;
321565985d93SJaegeuk Kim }
321665985d93SJaegeuk Kim 
3217d9c454abSChao Liu /*
3218d9c454abSChao Liu  * Notice: check inline_data flag without inode page lock is unsafe.
3219d9c454abSChao Liu  * It could change at any time by f2fs_convert_inline_page().
3220d9c454abSChao Liu  */
32210dbdc2aeSJaegeuk Kim static inline int f2fs_has_inline_data(struct inode *inode)
32220dbdc2aeSJaegeuk Kim {
322391942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DATA);
32240dbdc2aeSJaegeuk Kim }
32250dbdc2aeSJaegeuk Kim 
3226b3d208f9SJaegeuk Kim static inline int f2fs_exist_data(struct inode *inode)
3227b3d208f9SJaegeuk Kim {
322891942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DATA_EXIST);
3229b3d208f9SJaegeuk Kim }
3230b3d208f9SJaegeuk Kim 
3231510022a8SJaegeuk Kim static inline int f2fs_has_inline_dots(struct inode *inode)
3232510022a8SJaegeuk Kim {
323391942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DOTS);
3234510022a8SJaegeuk Kim }
3235510022a8SJaegeuk Kim 
32364c8ff709SChao Yu static inline int f2fs_is_mmap_file(struct inode *inode)
32374c8ff709SChao Yu {
32384c8ff709SChao Yu 	return is_inode_flag_set(inode, FI_MMAP_FILE);
32394c8ff709SChao Yu }
32404c8ff709SChao Yu 
32411ad71a27SJaegeuk Kim static inline bool f2fs_is_pinned_file(struct inode *inode)
32421ad71a27SJaegeuk Kim {
32431ad71a27SJaegeuk Kim 	return is_inode_flag_set(inode, FI_PIN_FILE);
32441ad71a27SJaegeuk Kim }
32451ad71a27SJaegeuk Kim 
324688b88a66SJaegeuk Kim static inline bool f2fs_is_atomic_file(struct inode *inode)
324788b88a66SJaegeuk Kim {
324891942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_ATOMIC_FILE);
324988b88a66SJaegeuk Kim }
325088b88a66SJaegeuk Kim 
32514a2c5b79SYe Bin static inline bool f2fs_is_cow_file(struct inode *inode)
32524a2c5b79SYe Bin {
32534a2c5b79SYe Bin 	return is_inode_flag_set(inode, FI_COW_FILE);
32544a2c5b79SYe Bin }
32554a2c5b79SYe Bin 
32563c6c2bebSJaegeuk Kim static inline bool f2fs_is_first_block_written(struct inode *inode)
32573c6c2bebSJaegeuk Kim {
325891942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
32593c6c2bebSJaegeuk Kim }
32603c6c2bebSJaegeuk Kim 
32611e84371fSJaegeuk Kim static inline bool f2fs_is_drop_cache(struct inode *inode)
32621e84371fSJaegeuk Kim {
326391942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DROP_CACHE);
32641e84371fSJaegeuk Kim }
32651e84371fSJaegeuk Kim 
3266f2470371SChao Yu static inline void *inline_data_addr(struct inode *inode, struct page *page)
32671001b347SHuajun Li {
3268695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
32697a2af766SChao Yu 	int extra_size = get_extra_isize(inode);
3270cac5a3d8SDongOh Shin 
32717a2af766SChao Yu 	return (void *)&(ri->i_addr[extra_size + DEF_INLINE_RESERVED_SIZE]);
32721001b347SHuajun Li }
32731001b347SHuajun Li 
327434d67debSChao Yu static inline int f2fs_has_inline_dentry(struct inode *inode)
327534d67debSChao Yu {
327691942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DENTRY);
327734d67debSChao Yu }
327834d67debSChao Yu 
3279b5492af7SJaegeuk Kim static inline int is_file(struct inode *inode, int type)
3280b5492af7SJaegeuk Kim {
3281b5492af7SJaegeuk Kim 	return F2FS_I(inode)->i_advise & type;
3282b5492af7SJaegeuk Kim }
3283b5492af7SJaegeuk Kim 
3284b5492af7SJaegeuk Kim static inline void set_file(struct inode *inode, int type)
3285b5492af7SJaegeuk Kim {
3286766c6639SJaegeuk Kim 	if (is_file(inode, type))
3287766c6639SJaegeuk Kim 		return;
3288b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise |= type;
32897c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3290b5492af7SJaegeuk Kim }
3291b5492af7SJaegeuk Kim 
3292b5492af7SJaegeuk Kim static inline void clear_file(struct inode *inode, int type)
3293b5492af7SJaegeuk Kim {
3294766c6639SJaegeuk Kim 	if (!is_file(inode, type))
3295766c6639SJaegeuk Kim 		return;
3296b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise &= ~type;
32977c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3298b5492af7SJaegeuk Kim }
3299b5492af7SJaegeuk Kim 
3300fe1897eaSChao Yu static inline bool f2fs_is_time_consistent(struct inode *inode)
3301fe1897eaSChao Yu {
3302fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time, &inode->i_atime))
3303fe1897eaSChao Yu 		return false;
3304fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 1, &inode->i_ctime))
3305fe1897eaSChao Yu 		return false;
3306fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 2, &inode->i_mtime))
3307fe1897eaSChao Yu 		return false;
3308fe1897eaSChao Yu 	return true;
3309fe1897eaSChao Yu }
3310fe1897eaSChao Yu 
331126787236SJaegeuk Kim static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
331226787236SJaegeuk Kim {
3313a0d00fadSChao Yu 	bool ret;
3314a0d00fadSChao Yu 
331526787236SJaegeuk Kim 	if (dsync) {
331626787236SJaegeuk Kim 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
331726787236SJaegeuk Kim 
331826787236SJaegeuk Kim 		spin_lock(&sbi->inode_lock[DIRTY_META]);
331926787236SJaegeuk Kim 		ret = list_empty(&F2FS_I(inode)->gdirty_list);
332026787236SJaegeuk Kim 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
332126787236SJaegeuk Kim 		return ret;
332226787236SJaegeuk Kim 	}
332326787236SJaegeuk Kim 	if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
332426787236SJaegeuk Kim 			file_keep_isize(inode) ||
3325235831d7SJunling Zheng 			i_size_read(inode) & ~PAGE_MASK)
332626787236SJaegeuk Kim 		return false;
3327a0d00fadSChao Yu 
3328fe1897eaSChao Yu 	if (!f2fs_is_time_consistent(inode))
3329214c2461SJaegeuk Kim 		return false;
3330214c2461SJaegeuk Kim 
3331c10c9820SChao Yu 	spin_lock(&F2FS_I(inode)->i_size_lock);
3332a0d00fadSChao Yu 	ret = F2FS_I(inode)->last_disk_size == i_size_read(inode);
3333c10c9820SChao Yu 	spin_unlock(&F2FS_I(inode)->i_size_lock);
3334a0d00fadSChao Yu 
3335a0d00fadSChao Yu 	return ret;
3336267378d4SChao Yu }
3337267378d4SChao Yu 
3338deeedd71SChao Yu static inline bool f2fs_readonly(struct super_block *sb)
333977888c1eSJaegeuk Kim {
3340deeedd71SChao Yu 	return sb_rdonly(sb);
334177888c1eSJaegeuk Kim }
334277888c1eSJaegeuk Kim 
3343744602cfSJaegeuk Kim static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
3344744602cfSJaegeuk Kim {
3345aaec2b1dSChao Yu 	return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
3346744602cfSJaegeuk Kim }
3347744602cfSJaegeuk Kim 
334843c780baSEric Biggers static inline bool is_dot_dotdot(const u8 *name, size_t len)
3349eaa693f4SJaegeuk Kim {
335043c780baSEric Biggers 	if (len == 1 && name[0] == '.')
3351eaa693f4SJaegeuk Kim 		return true;
3352eaa693f4SJaegeuk Kim 
335343c780baSEric Biggers 	if (len == 2 && name[0] == '.' && name[1] == '.')
3354eaa693f4SJaegeuk Kim 		return true;
3355eaa693f4SJaegeuk Kim 
3356eaa693f4SJaegeuk Kim 	return false;
3357eaa693f4SJaegeuk Kim }
3358eaa693f4SJaegeuk Kim 
33591ecc0c5cSChao Yu static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
33601ecc0c5cSChao Yu 					size_t size, gfp_t flags)
33610414b004SJaegeuk Kim {
3362c40e15a9SYangtao Li 	if (time_to_inject(sbi, FAULT_KMALLOC))
33632c63feadSJaegeuk Kim 		return NULL;
33647fa750a1SArnd Bergmann 
33650b6d4ca0SEric Biggers 	return kmalloc(size, flags);
33660414b004SJaegeuk Kim }
33670414b004SJaegeuk Kim 
336855847850SWu Bo static inline void *f2fs_getname(struct f2fs_sb_info *sbi)
336955847850SWu Bo {
337055847850SWu Bo 	if (time_to_inject(sbi, FAULT_KMALLOC))
337155847850SWu Bo 		return NULL;
337255847850SWu Bo 
337355847850SWu Bo 	return __getname();
337455847850SWu Bo }
337555847850SWu Bo 
337655847850SWu Bo static inline void f2fs_putname(char *buf)
337755847850SWu Bo {
337855847850SWu Bo 	__putname(buf);
337955847850SWu Bo }
338055847850SWu Bo 
3381acbf054dSChao Yu static inline void *f2fs_kzalloc(struct f2fs_sb_info *sbi,
3382acbf054dSChao Yu 					size_t size, gfp_t flags)
3383acbf054dSChao Yu {
3384acbf054dSChao Yu 	return f2fs_kmalloc(sbi, size, flags | __GFP_ZERO);
3385acbf054dSChao Yu }
3386acbf054dSChao Yu 
3387628b3d14SChao Yu static inline void *f2fs_kvmalloc(struct f2fs_sb_info *sbi,
3388628b3d14SChao Yu 					size_t size, gfp_t flags)
3389628b3d14SChao Yu {
3390c40e15a9SYangtao Li 	if (time_to_inject(sbi, FAULT_KVMALLOC))
3391628b3d14SChao Yu 		return NULL;
33927fa750a1SArnd Bergmann 
3393628b3d14SChao Yu 	return kvmalloc(size, flags);
3394628b3d14SChao Yu }
3395628b3d14SChao Yu 
3396628b3d14SChao Yu static inline void *f2fs_kvzalloc(struct f2fs_sb_info *sbi,
3397628b3d14SChao Yu 					size_t size, gfp_t flags)
3398628b3d14SChao Yu {
3399628b3d14SChao Yu 	return f2fs_kvmalloc(sbi, size, flags | __GFP_ZERO);
3400628b3d14SChao Yu }
3401628b3d14SChao Yu 
34027a2af766SChao Yu static inline int get_extra_isize(struct inode *inode)
3403f2470371SChao Yu {
34047a2af766SChao Yu 	return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
3405f2470371SChao Yu }
3406f2470371SChao Yu 
34076afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode)
34086afc662eSChao Yu {
34096afc662eSChao Yu 	return F2FS_I(inode)->i_inline_xattr_size;
34106afc662eSChao Yu }
34116afc662eSChao Yu 
34124d57b86dSChao Yu #define f2fs_get_inode_mode(i) \
341391942321SJaegeuk Kim 	((is_inode_flag_set(i, FI_ACL_MODE)) ? \
3414a6dda0e6SChristoph Hellwig 	 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
3415a6dda0e6SChristoph Hellwig 
34167a2af766SChao Yu #define F2FS_TOTAL_EXTRA_ATTR_SIZE			\
34177a2af766SChao Yu 	(offsetof(struct f2fs_inode, i_extra_end) -	\
34187a2af766SChao Yu 	offsetof(struct f2fs_inode, i_extra_isize))	\
34197a2af766SChao Yu 
34205c57132eSChao Yu #define F2FS_OLD_ATTRIBUTE_SIZE	(offsetof(struct f2fs_inode, i_addr))
34215c57132eSChao Yu #define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field)		\
34222f84babfSSheng Yong 		((offsetof(typeof(*(f2fs_inode)), field) +	\
34235c57132eSChao Yu 		sizeof((f2fs_inode)->field))			\
34242f84babfSSheng Yong 		<= (F2FS_OLD_ATTRIBUTE_SIZE + (extra_isize)))	\
34255c57132eSChao Yu 
34262c70c5e3SChao Yu #define __is_large_section(sbi)		((sbi)->segs_per_sec > 1)
34272c70c5e3SChao Yu 
34286dc3a126SChao Yu #define __is_meta_io(fio) (PAGE_TYPE_OF_BIO((fio)->type) == META)
3429c9b60788SChao Yu 
3430e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
3431e1da7872SChao Yu 					block_t blkaddr, int type);
3432e1da7872SChao Yu static inline void verify_blkaddr(struct f2fs_sb_info *sbi,
3433e1da7872SChao Yu 					block_t blkaddr, int type)
3434e1da7872SChao Yu {
3435e1da7872SChao Yu 	if (!f2fs_is_valid_blkaddr(sbi, blkaddr, type)) {
3436dcbb4c10SJoe Perches 		f2fs_err(sbi, "invalid blkaddr: %u, type: %d, run fsck to fix.",
3437e1da7872SChao Yu 			 blkaddr, type);
3438e1da7872SChao Yu 		f2fs_bug_on(sbi, 1);
3439e1da7872SChao Yu 	}
3440e1da7872SChao Yu }
3441e1da7872SChao Yu 
3442e1da7872SChao Yu static inline bool __is_valid_data_blkaddr(block_t blkaddr)
34437b525dd0SChao Yu {
34444c8ff709SChao Yu 	if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR ||
34454c8ff709SChao Yu 			blkaddr == COMPRESS_ADDR)
34467b525dd0SChao Yu 		return false;
34477b525dd0SChao Yu 	return true;
34487b525dd0SChao Yu }
34497b525dd0SChao Yu 
345039a53e0cSJaegeuk Kim /*
345139a53e0cSJaegeuk Kim  * file.c
345239a53e0cSJaegeuk Kim  */
3453cac5a3d8SDongOh Shin int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
34544d57b86dSChao Yu void f2fs_truncate_data_blocks(struct dnode_of_data *dn);
34553265d3dbSChao Yu int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock);
3456c42d28ceSChao Yu int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock);
3457cac5a3d8SDongOh Shin int f2fs_truncate(struct inode *inode);
3458b74d24f7SChristian Brauner int f2fs_getattr(struct mnt_idmap *idmap, const struct path *path,
3459549c7297SChristian Brauner 		 struct kstat *stat, u32 request_mask, unsigned int flags);
3460c1632a0fSChristian Brauner int f2fs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
3461549c7297SChristian Brauner 		 struct iattr *attr);
34624d57b86dSChao Yu int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
34634d57b86dSChao Yu void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count);
3464c4020b2dSChao Yu int f2fs_precache_extents(struct inode *inode);
34659b1bb01cSMiklos Szeredi int f2fs_fileattr_get(struct dentry *dentry, struct fileattr *fa);
34668782a9aeSChristian Brauner int f2fs_fileattr_set(struct mnt_idmap *idmap,
34679b1bb01cSMiklos Szeredi 		      struct dentry *dentry, struct fileattr *fa);
3468cac5a3d8SDongOh Shin long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
3469cac5a3d8SDongOh Shin long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
347078130819SChao Yu int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid);
34711ad71a27SJaegeuk Kim int f2fs_pin_file_control(struct inode *inode, bool inc);
347239a53e0cSJaegeuk Kim 
347339a53e0cSJaegeuk Kim /*
347439a53e0cSJaegeuk Kim  * inode.c
347539a53e0cSJaegeuk Kim  */
3476cac5a3d8SDongOh Shin void f2fs_set_inode_flags(struct inode *inode);
3477704956ecSChao Yu bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page);
3478704956ecSChao Yu void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page);
3479cac5a3d8SDongOh Shin struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
3480cac5a3d8SDongOh Shin struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
34814d57b86dSChao Yu int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
34824d57b86dSChao Yu void f2fs_update_inode(struct inode *inode, struct page *node_page);
34834d57b86dSChao Yu void f2fs_update_inode_page(struct inode *inode);
3484cac5a3d8SDongOh Shin int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
3485cac5a3d8SDongOh Shin void f2fs_evict_inode(struct inode *inode);
34864d57b86dSChao Yu void f2fs_handle_failed_inode(struct inode *inode);
348739a53e0cSJaegeuk Kim 
348839a53e0cSJaegeuk Kim /*
348939a53e0cSJaegeuk Kim  * namei.c
349039a53e0cSJaegeuk Kim  */
34914d57b86dSChao Yu int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
3492b6a06cbbSChao Yu 							bool hot, bool set);
349339a53e0cSJaegeuk Kim struct dentry *f2fs_get_parent(struct dentry *child);
3494f2d40141SChristian Brauner int f2fs_get_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
34953db1de0eSDaeho Jeong 		     struct inode **new_inode);
349639a53e0cSJaegeuk Kim 
349739a53e0cSJaegeuk Kim /*
349839a53e0cSJaegeuk Kim  * dir.c
349939a53e0cSJaegeuk Kim  */
350043c780baSEric Biggers int f2fs_init_casefolded_name(const struct inode *dir,
350143c780baSEric Biggers 			      struct f2fs_filename *fname);
350243c780baSEric Biggers int f2fs_setup_filename(struct inode *dir, const struct qstr *iname,
350343c780baSEric Biggers 			int lookup, struct f2fs_filename *fname);
350443c780baSEric Biggers int f2fs_prepare_lookup(struct inode *dir, struct dentry *dentry,
350543c780baSEric Biggers 			struct f2fs_filename *fname);
350643c780baSEric Biggers void f2fs_free_filename(struct f2fs_filename *fname);
350743c780baSEric Biggers struct f2fs_dir_entry *f2fs_find_target_dentry(const struct f2fs_dentry_ptr *d,
350843c780baSEric Biggers 			const struct f2fs_filename *fname, int *max_slots);
3509cac5a3d8SDongOh Shin int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
3510cac5a3d8SDongOh Shin 			unsigned int start_pos, struct fscrypt_str *fstr);
35114d57b86dSChao Yu void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
3512cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
35134d57b86dSChao Yu struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
351443c780baSEric Biggers 			const struct f2fs_filename *fname, struct page *dpage);
35154d57b86dSChao Yu void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
3516cac5a3d8SDongOh Shin 			unsigned int current_depth);
35174d57b86dSChao Yu int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots);
3518cac5a3d8SDongOh Shin void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
3519cac5a3d8SDongOh Shin struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
352043c780baSEric Biggers 					 const struct f2fs_filename *fname,
352143c780baSEric Biggers 					 struct page **res_page);
3522cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
3523cac5a3d8SDongOh Shin 			const struct qstr *child, struct page **res_page);
3524cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
3525cac5a3d8SDongOh Shin ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
3526cac5a3d8SDongOh Shin 			struct page **page);
3527cac5a3d8SDongOh Shin void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
3528cac5a3d8SDongOh Shin 			struct page *page, struct inode *inode);
3529b06af2afSJaegeuk Kim bool f2fs_has_enough_room(struct inode *dir, struct page *ipage,
353043c780baSEric Biggers 			  const struct f2fs_filename *fname);
3531cac5a3d8SDongOh Shin void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
353243c780baSEric Biggers 			const struct fscrypt_str *name, f2fs_hash_t name_hash,
3533cac5a3d8SDongOh Shin 			unsigned int bit_pos);
353443c780baSEric Biggers int f2fs_add_regular_entry(struct inode *dir, const struct f2fs_filename *fname,
3535cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
353643c780baSEric Biggers int f2fs_add_dentry(struct inode *dir, const struct f2fs_filename *fname,
3537cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
35384d57b86dSChao Yu int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
3539cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
3540cac5a3d8SDongOh Shin void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
3541cac5a3d8SDongOh Shin 			struct inode *dir, struct inode *inode);
3542cac5a3d8SDongOh Shin int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
3543cac5a3d8SDongOh Shin bool f2fs_empty_dir(struct inode *dir);
354439a53e0cSJaegeuk Kim 
3545b7f7a5e0SAl Viro static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
3546b7f7a5e0SAl Viro {
3547bfc2b7e8SEric Biggers 	if (fscrypt_is_nokey_name(dentry))
3548bfc2b7e8SEric Biggers 		return -ENOKEY;
35494d57b86dSChao Yu 	return f2fs_do_add_link(d_inode(dentry->d_parent), &dentry->d_name,
3550510022a8SJaegeuk Kim 				inode, inode->i_ino, inode->i_mode);
3551b7f7a5e0SAl Viro }
3552b7f7a5e0SAl Viro 
355339a53e0cSJaegeuk Kim /*
355439a53e0cSJaegeuk Kim  * super.c
355539a53e0cSJaegeuk Kim  */
3556cac5a3d8SDongOh Shin int f2fs_inode_dirtied(struct inode *inode, bool sync);
3557cac5a3d8SDongOh Shin void f2fs_inode_synced(struct inode *inode);
355810a26878SChao Yu int f2fs_dquot_initialize(struct inode *inode);
3559ea676733SJaegeuk Kim int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly);
3560af033b2aSChao Yu int f2fs_quota_sync(struct super_block *sb, int type);
35616d1451bfSChengguang Xu loff_t max_file_blocks(struct inode *inode);
35624b2414d0SChao Yu void f2fs_quota_off_umount(struct super_block *sb);
35631aa161e4SJaegeuk Kim void f2fs_save_errors(struct f2fs_sb_info *sbi, unsigned char flag);
3564b62e71beSChao Yu void f2fs_handle_critical_error(struct f2fs_sb_info *sbi, unsigned char reason,
3565b62e71beSChao Yu 							bool irq_context);
356695fa90c9SChao Yu void f2fs_handle_error(struct f2fs_sb_info *sbi, unsigned char error);
3567901c12d1SChao Yu void f2fs_handle_error_async(struct f2fs_sb_info *sbi, unsigned char error);
3568cac5a3d8SDongOh Shin int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
3569cac5a3d8SDongOh Shin int f2fs_sync_fs(struct super_block *sb, int sync);
35704d57b86dSChao Yu int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi);
357139a53e0cSJaegeuk Kim 
357239a53e0cSJaegeuk Kim /*
357339a53e0cSJaegeuk Kim  * hash.c
357439a53e0cSJaegeuk Kim  */
357543c780baSEric Biggers void f2fs_hash_filename(const struct inode *dir, struct f2fs_filename *fname);
357639a53e0cSJaegeuk Kim 
357739a53e0cSJaegeuk Kim /*
357839a53e0cSJaegeuk Kim  * node.c
357939a53e0cSJaegeuk Kim  */
358039a53e0cSJaegeuk Kim struct node_info;
358139a53e0cSJaegeuk Kim 
35824d57b86dSChao Yu int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid);
35834d57b86dSChao Yu bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type);
358450fa53ecSChao Yu bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct page *page);
358550fa53ecSChao Yu void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi);
358650fa53ecSChao Yu void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct page *page);
358750fa53ecSChao Yu void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi);
35884d57b86dSChao Yu int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
35894d57b86dSChao Yu bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
35904d57b86dSChao Yu bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
35917735730dSChao Yu int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
3592a9419b63SJaegeuk Kim 				struct node_info *ni, bool checkpoint_context);
35934d57b86dSChao Yu pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
35944d57b86dSChao Yu int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
35954d57b86dSChao Yu int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from);
35964d57b86dSChao Yu int f2fs_truncate_xattr_node(struct inode *inode);
359750fa53ecSChao Yu int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
359850fa53ecSChao Yu 					unsigned int seq_id);
359994c821fbSChao Yu bool f2fs_nat_bitmap_enabled(struct f2fs_sb_info *sbi);
36004d57b86dSChao Yu int f2fs_remove_inode_page(struct inode *inode);
36014d57b86dSChao Yu struct page *f2fs_new_inode_page(struct inode *inode);
36024d57b86dSChao Yu struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs);
36034d57b86dSChao Yu void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
36044d57b86dSChao Yu struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
36054d57b86dSChao Yu struct page *f2fs_get_node_page_ra(struct page *parent, int start);
360648018b4cSChao Yu int f2fs_move_node_page(struct page *node_page, int gc_type);
360768e79bafSJia Yang void f2fs_flush_inline_data(struct f2fs_sb_info *sbi);
36084d57b86dSChao Yu int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
360950fa53ecSChao Yu 			struct writeback_control *wbc, bool atomic,
361050fa53ecSChao Yu 			unsigned int *seq_id);
36114d57b86dSChao Yu int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
36124d57b86dSChao Yu 			struct writeback_control *wbc,
3613b0af6d49SChao Yu 			bool do_balance, enum iostat_type io_type);
3614e2374015SChao Yu int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
36154d57b86dSChao Yu bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
36164d57b86dSChao Yu void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
36174d57b86dSChao Yu void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
36184d57b86dSChao Yu int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
36199627a7b3SChao Yu int f2fs_recover_inline_xattr(struct inode *inode, struct page *page);
36204d57b86dSChao Yu int f2fs_recover_xattr_data(struct inode *inode, struct page *page);
36214d57b86dSChao Yu int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
36227735730dSChao Yu int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
3623cac5a3d8SDongOh Shin 			unsigned int segno, struct f2fs_summary_block *sum);
362494c821fbSChao Yu void f2fs_enable_nat_bits(struct f2fs_sb_info *sbi);
3625edc55aafSJaegeuk Kim int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
36264d57b86dSChao Yu int f2fs_build_node_manager(struct f2fs_sb_info *sbi);
36274d57b86dSChao Yu void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi);
36284d57b86dSChao Yu int __init f2fs_create_node_manager_caches(void);
36294d57b86dSChao Yu void f2fs_destroy_node_manager_caches(void);
363039a53e0cSJaegeuk Kim 
363139a53e0cSJaegeuk Kim /*
363239a53e0cSJaegeuk Kim  * segment.c
363339a53e0cSJaegeuk Kim  */
36344d57b86dSChao Yu bool f2fs_need_SSR(struct f2fs_sb_info *sbi);
36353db1de0eSDaeho Jeong int f2fs_commit_atomic_write(struct inode *inode);
36363db1de0eSDaeho Jeong void f2fs_abort_atomic_write(struct inode *inode, bool clean);
3637cac5a3d8SDongOh Shin void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
36387bcd0cfaSChao Yu void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi, bool from_bg);
363939d787beSChao Yu int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
36404d57b86dSChao Yu int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
36411228b482SChao Yu int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
36424d57b86dSChao Yu void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
36434d57b86dSChao Yu void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
36444d57b86dSChao Yu bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
36454d674904SFengnan Chang int f2fs_start_discard_thread(struct f2fs_sb_info *sbi);
36464d57b86dSChao Yu void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
36474d57b86dSChao Yu void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi);
364803f2c02dSJaegeuk Kim bool f2fs_issue_discard_timeout(struct f2fs_sb_info *sbi);
36494d57b86dSChao Yu void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
36504d57b86dSChao Yu 					struct cp_control *cpc);
36514354994fSDaniel Rosenberg void f2fs_dirty_to_prefree(struct f2fs_sb_info *sbi);
36524d3aed70SDaniel Rosenberg block_t f2fs_get_unusable_blocks(struct f2fs_sb_info *sbi);
36534d3aed70SDaniel Rosenberg int f2fs_disable_cp_again(struct f2fs_sb_info *sbi, block_t unusable);
36544d57b86dSChao Yu void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi);
36554d57b86dSChao Yu int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
365661461fc9SChao Yu bool f2fs_segment_has_free_slot(struct f2fs_sb_info *sbi, int segno);
3657093749e2SChao Yu void f2fs_init_inmem_curseg(struct f2fs_sb_info *sbi);
3658093749e2SChao Yu void f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi);
3659093749e2SChao Yu void f2fs_restore_inmem_curseg(struct f2fs_sb_info *sbi);
3660093749e2SChao Yu void f2fs_get_new_segment(struct f2fs_sb_info *sbi,
3661093749e2SChao Yu 			unsigned int *newseg, bool new_sec, int dir);
36620ef81833SChao Yu void f2fs_allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type,
366304f0b2eaSQiuyang Sun 					unsigned int start, unsigned int end);
3664509f1010SChao Yu void f2fs_allocate_new_section(struct f2fs_sb_info *sbi, int type, bool force);
3665901d745fSChao Yu void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi);
3666cac5a3d8SDongOh Shin int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
36674d57b86dSChao Yu bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
36684d57b86dSChao Yu 					struct cp_control *cpc);
36694d57b86dSChao Yu struct page *f2fs_get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
36704d57b86dSChao Yu void f2fs_update_meta_page(struct f2fs_sb_info *sbi, void *src,
36714d57b86dSChao Yu 					block_t blk_addr);
36724d57b86dSChao Yu void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
3673b0af6d49SChao Yu 						enum iostat_type io_type);
36744d57b86dSChao Yu void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio);
36754d57b86dSChao Yu void f2fs_outplace_write_data(struct dnode_of_data *dn,
36764d57b86dSChao Yu 			struct f2fs_io_info *fio);
36774d57b86dSChao Yu int f2fs_inplace_write_data(struct f2fs_io_info *fio);
36784d57b86dSChao Yu void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
3679cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t new_blkaddr,
3680c5d02785SChao Yu 			bool recover_curseg, bool recover_newaddr,
3681c5d02785SChao Yu 			bool from_gc);
3682cac5a3d8SDongOh Shin void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
3683cac5a3d8SDongOh Shin 			block_t old_addr, block_t new_addr,
3684cac5a3d8SDongOh Shin 			unsigned char version, bool recover_curseg,
3685cac5a3d8SDongOh Shin 			bool recover_newaddr);
36864d57b86dSChao Yu void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
3687cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t *new_blkaddr,
3688fb830fc5SChao Yu 			struct f2fs_summary *sum, int type,
3689f608c38cSChao Yu 			struct f2fs_io_info *fio);
369071f2c820SChao Yu void f2fs_update_device_state(struct f2fs_sb_info *sbi, nid_t ino,
369171f2c820SChao Yu 					block_t blkaddr, unsigned int blkcnt);
3692cac5a3d8SDongOh Shin void f2fs_wait_on_page_writeback(struct page *page,
3693bae0ee7aSChao Yu 			enum page_type type, bool ordered, bool locked);
36940ded69f6SJaegeuk Kim void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr);
36951e78e8bdSSahitya Tummala void f2fs_wait_on_block_writeback_range(struct inode *inode, block_t blkaddr,
36961e78e8bdSSahitya Tummala 								block_t len);
36974d57b86dSChao Yu void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
36984d57b86dSChao Yu void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
36994d57b86dSChao Yu int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
3700cac5a3d8SDongOh Shin 			unsigned int val, int alloc);
37014d57b86dSChao Yu void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
3702c426d991SShin'ichiro Kawasaki int f2fs_fix_curseg_write_pointer(struct f2fs_sb_info *sbi);
3703d508c94eSShin'ichiro Kawasaki int f2fs_check_write_pointer(struct f2fs_sb_info *sbi);
37044d57b86dSChao Yu int f2fs_build_segment_manager(struct f2fs_sb_info *sbi);
37054d57b86dSChao Yu void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi);
37064d57b86dSChao Yu int __init f2fs_create_segment_manager_caches(void);
37074d57b86dSChao Yu void f2fs_destroy_segment_manager_caches(void);
37084d57b86dSChao Yu int f2fs_rw_hint_to_seg_type(enum rw_hint hint);
3709de881df9SAravind Ramesh unsigned int f2fs_usable_segs_in_sec(struct f2fs_sb_info *sbi,
3710de881df9SAravind Ramesh 			unsigned int segno);
3711de881df9SAravind Ramesh unsigned int f2fs_usable_blks_in_seg(struct f2fs_sb_info *sbi,
3712de881df9SAravind Ramesh 			unsigned int segno);
371339a53e0cSJaegeuk Kim 
37146691d940SDaeho Jeong #define DEF_FRAGMENT_SIZE	4
37156691d940SDaeho Jeong #define MIN_FRAGMENT_SIZE	1
37166691d940SDaeho Jeong #define MAX_FRAGMENT_SIZE	512
37176691d940SDaeho Jeong 
37186691d940SDaeho Jeong static inline bool f2fs_need_rand_seg(struct f2fs_sb_info *sbi)
37196691d940SDaeho Jeong {
37206691d940SDaeho Jeong 	return F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_SEG ||
37216691d940SDaeho Jeong 		F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK;
37226691d940SDaeho Jeong }
37236691d940SDaeho Jeong 
372439a53e0cSJaegeuk Kim /*
372539a53e0cSJaegeuk Kim  * checkpoint.c
372639a53e0cSJaegeuk Kim  */
3727a9cfee0eSChao Yu void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io,
3728a9cfee0eSChao Yu 							unsigned char reason);
3729c7b58576SJaegeuk Kim void f2fs_flush_ckpt_thread(struct f2fs_sb_info *sbi);
37304d57b86dSChao Yu struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
37314d57b86dSChao Yu struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
373286f33603SJaegeuk Kim struct page *f2fs_get_meta_page_retry(struct f2fs_sb_info *sbi, pgoff_t index);
37334d57b86dSChao Yu struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
3734e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
37354d57b86dSChao Yu 					block_t blkaddr, int type);
37364d57b86dSChao Yu int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
3737cac5a3d8SDongOh Shin 			int type, bool sync);
3738430f163bSChao Yu void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index,
3739430f163bSChao Yu 							unsigned int ra_blocks);
37404d57b86dSChao Yu long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
3741b0af6d49SChao Yu 			long nr_to_write, enum iostat_type io_type);
37424d57b86dSChao Yu void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
37434d57b86dSChao Yu void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
37444d57b86dSChao Yu void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all);
37454d57b86dSChao Yu bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
37464d57b86dSChao Yu void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
374739d787beSChao Yu 					unsigned int devidx, int type);
37484d57b86dSChao Yu bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
374939d787beSChao Yu 					unsigned int devidx, int type);
37504d57b86dSChao Yu int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi);
37514d57b86dSChao Yu void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi);
37524d57b86dSChao Yu void f2fs_add_orphan_inode(struct inode *inode);
37534d57b86dSChao Yu void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
37544d57b86dSChao Yu int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi);
37554d57b86dSChao Yu int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi);
37564f5e34f7SMatthew Wilcox (Oracle) void f2fs_update_dirty_folio(struct inode *inode, struct folio *folio);
37574d57b86dSChao Yu void f2fs_remove_dirty_inode(struct inode *inode);
3758d80afefbSChao Yu int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type,
3759d80afefbSChao Yu 								bool from_cp);
3760bf22c3ccSSahitya Tummala void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type);
37613a0a9cbcSChao Yu u64 f2fs_get_sectors_written(struct f2fs_sb_info *sbi);
37624d57b86dSChao Yu int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
37634d57b86dSChao Yu void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi);
37644d57b86dSChao Yu int __init f2fs_create_checkpoint_caches(void);
37654d57b86dSChao Yu void f2fs_destroy_checkpoint_caches(void);
3766261eeb9cSDaeho Jeong int f2fs_issue_checkpoint(struct f2fs_sb_info *sbi);
3767261eeb9cSDaeho Jeong int f2fs_start_ckpt_thread(struct f2fs_sb_info *sbi);
3768261eeb9cSDaeho Jeong void f2fs_stop_ckpt_thread(struct f2fs_sb_info *sbi);
3769261eeb9cSDaeho Jeong void f2fs_init_ckpt_req_control(struct f2fs_sb_info *sbi);
377039a53e0cSJaegeuk Kim 
377139a53e0cSJaegeuk Kim /*
377239a53e0cSJaegeuk Kim  * data.c
377339a53e0cSJaegeuk Kim  */
3774f543805fSChao Yu int __init f2fs_init_bioset(void);
3775f543805fSChao Yu void f2fs_destroy_bioset(void);
37760b20fcecSChao Yu int f2fs_init_bio_entry_cache(void);
37770b20fcecSChao Yu void f2fs_destroy_bio_entry_cache(void);
3778bc29835aSChristoph Hellwig void f2fs_submit_read_bio(struct f2fs_sb_info *sbi, struct bio *bio,
3779bc29835aSChristoph Hellwig 			  enum page_type type);
3780908ea654SYufen Yu int f2fs_init_write_merge_io(struct f2fs_sb_info *sbi);
3781b9109b0eSJaegeuk Kim void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
3782b9109b0eSJaegeuk Kim void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
3783bab475c5SChao Yu 				struct inode *inode, struct page *page,
3784bab475c5SChao Yu 				nid_t ino, enum page_type type);
37850b20fcecSChao Yu void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
37860b20fcecSChao Yu 					struct bio **bio, struct page *page);
3787b9109b0eSJaegeuk Kim void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
3788cac5a3d8SDongOh Shin int f2fs_submit_page_bio(struct f2fs_io_info *fio);
37898648de2cSChao Yu int f2fs_merge_page_bio(struct f2fs_io_info *fio);
3790fe16efe6SChao Yu void f2fs_submit_page_write(struct f2fs_io_info *fio);
3791cac5a3d8SDongOh Shin struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
37925189810aSChristoph Hellwig 		block_t blk_addr, sector_t *sector);
3793cac5a3d8SDongOh Shin int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
37944d57b86dSChao Yu void f2fs_set_data_blkaddr(struct dnode_of_data *dn);
3795cac5a3d8SDongOh Shin void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
37964d57b86dSChao Yu int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
37974d57b86dSChao Yu int f2fs_reserve_new_block(struct dnode_of_data *dn);
3798cf342d3bSChristoph Hellwig int f2fs_get_block_locked(struct dnode_of_data *dn, pgoff_t index);
3799cac5a3d8SDongOh Shin int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
38004d57b86dSChao Yu struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
380159237a21SChao Yu 			blk_opf_t op_flags, bool for_write, pgoff_t *next_pgofs);
380259237a21SChao Yu struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index,
380359237a21SChao Yu 							pgoff_t *next_pgofs);
38044d57b86dSChao Yu struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
3805cac5a3d8SDongOh Shin 			bool for_write);
38064d57b86dSChao Yu struct page *f2fs_get_new_data_page(struct inode *inode,
3807cac5a3d8SDongOh Shin 			struct page *ipage, pgoff_t index, bool new_i_size);
38084d57b86dSChao Yu int f2fs_do_write_data_page(struct f2fs_io_info *fio);
3809cd8fc522SChristoph Hellwig int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, int flag);
3810cac5a3d8SDongOh Shin int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3811cac5a3d8SDongOh Shin 			u64 start, u64 len);
38124c8ff709SChao Yu int f2fs_encrypt_one_page(struct f2fs_io_info *fio);
38134d57b86dSChao Yu bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio);
38144d57b86dSChao Yu bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio);
38154c8ff709SChao Yu int f2fs_write_single_data_page(struct page *page, int *submitted,
38164c8ff709SChao Yu 				struct bio **bio, sector_t *last_block,
38174c8ff709SChao Yu 				struct writeback_control *wbc,
38184c8ff709SChao Yu 				enum iostat_type io_type,
38193afae09fSChao Yu 				int compr_blocks, bool allow_balance);
3820a1e09b03SEric Biggers void f2fs_write_failed(struct inode *inode, loff_t to);
382191503996SMatthew Wilcox (Oracle) void f2fs_invalidate_folio(struct folio *folio, size_t offset, size_t length);
3822c26cd045SMatthew Wilcox (Oracle) bool f2fs_release_folio(struct folio *folio, gfp_t wait);
3823b91050a8SHyunchul Lee bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len);
38245ec2d99dSMatthew Wilcox void f2fs_clear_page_cache_dirty_tag(struct page *page);
38254c8ff709SChao Yu int f2fs_init_post_read_processing(void);
38264c8ff709SChao Yu void f2fs_destroy_post_read_processing(void);
38274c8ff709SChao Yu int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi);
38284c8ff709SChao Yu void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi);
38291517c1a7SEric Biggers extern const struct iomap_ops f2fs_iomap_ops;
383039a53e0cSJaegeuk Kim 
383139a53e0cSJaegeuk Kim /*
383239a53e0cSJaegeuk Kim  * gc.c
383339a53e0cSJaegeuk Kim  */
38344d57b86dSChao Yu int f2fs_start_gc_thread(struct f2fs_sb_info *sbi);
38354d57b86dSChao Yu void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi);
38364d57b86dSChao Yu block_t f2fs_start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
3837d147ea4aSJaegeuk Kim int f2fs_gc(struct f2fs_sb_info *sbi, struct f2fs_gc_control *gc_control);
38384d57b86dSChao Yu void f2fs_build_gc_manager(struct f2fs_sb_info *sbi);
3839d8189834SChao Yu int f2fs_resize_fs(struct file *filp, __u64 block_count);
3840093749e2SChao Yu int __init f2fs_create_garbage_collection_cache(void);
3841093749e2SChao Yu void f2fs_destroy_garbage_collection_cache(void);
384219e0e21aSYangtao Li /* victim selection function for cleaning and SSR */
384319e0e21aSYangtao Li int f2fs_get_victim(struct f2fs_sb_info *sbi, unsigned int *result,
384419e0e21aSYangtao Li 			int gc_type, int type, char alloc_mode,
384519e0e21aSYangtao Li 			unsigned long long age);
384639a53e0cSJaegeuk Kim 
384739a53e0cSJaegeuk Kim /*
384839a53e0cSJaegeuk Kim  * recovery.c
384939a53e0cSJaegeuk Kim  */
38504d57b86dSChao Yu int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
38514d57b86dSChao Yu bool f2fs_space_for_roll_forward(struct f2fs_sb_info *sbi);
3852cad83c96SChao Yu int __init f2fs_create_recovery_cache(void);
3853cad83c96SChao Yu void f2fs_destroy_recovery_cache(void);
385439a53e0cSJaegeuk Kim 
385539a53e0cSJaegeuk Kim /*
385639a53e0cSJaegeuk Kim  * debug.c
385739a53e0cSJaegeuk Kim  */
385839a53e0cSJaegeuk Kim #ifdef CONFIG_F2FS_STAT_FS
385939a53e0cSJaegeuk Kim struct f2fs_stat_info {
386039a53e0cSJaegeuk Kim 	struct list_head stat_list;
386139a53e0cSJaegeuk Kim 	struct f2fs_sb_info *sbi;
386239a53e0cSJaegeuk Kim 	int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
386339a53e0cSJaegeuk Kim 	int main_area_segs, main_area_sections, main_area_zones;
3864e7547dacSJaegeuk Kim 	unsigned long long hit_cached[NR_EXTENT_CACHES];
3865e7547dacSJaegeuk Kim 	unsigned long long hit_rbtree[NR_EXTENT_CACHES];
3866e7547dacSJaegeuk Kim 	unsigned long long total_ext[NR_EXTENT_CACHES];
3867e7547dacSJaegeuk Kim 	unsigned long long hit_total[NR_EXTENT_CACHES];
3868e7547dacSJaegeuk Kim 	int ext_tree[NR_EXTENT_CACHES];
3869e7547dacSJaegeuk Kim 	int zombie_tree[NR_EXTENT_CACHES];
3870e7547dacSJaegeuk Kim 	int ext_node[NR_EXTENT_CACHES];
3871e7547dacSJaegeuk Kim 	/* to count memory footprint */
3872e7547dacSJaegeuk Kim 	unsigned long long ext_mem[NR_EXTENT_CACHES];
3873e7547dacSJaegeuk Kim 	/* for read extent cache */
3874e7547dacSJaegeuk Kim 	unsigned long long hit_largest;
387571644dffSJaegeuk Kim 	/* for block age extent cache */
387671644dffSJaegeuk Kim 	unsigned long long allocated_data_blocks;
38772c8a4a28SJaegeuk Kim 	int ndirty_node, ndirty_dent, ndirty_meta, ndirty_imeta;
38782c8a4a28SJaegeuk Kim 	int ndirty_data, ndirty_qdata;
38792c8a4a28SJaegeuk Kim 	unsigned int ndirty_dirs, ndirty_files, nquota_files, ndirty_all;
38805b0ef73cSJaegeuk Kim 	int nats, dirty_nats, sits, dirty_sits;
38815b0ef73cSJaegeuk Kim 	int free_nids, avail_nids, alloc_nids;
388239a53e0cSJaegeuk Kim 	int total_count, utilization;
38838b8dd65fSChao Yu 	int bg_gc, nr_wb_cp_data, nr_wb_data;
38845f9abab4SJaegeuk Kim 	int nr_rd_data, nr_rd_node, nr_rd_meta;
388502b16d0aSChao Yu 	int nr_dio_read, nr_dio_write;
3886274bd9baSChao Yu 	unsigned int io_skip_bggc, other_skip_bggc;
388714d8d5f7SChao Yu 	int nr_flushing, nr_flushed, flush_list_empty;
388814d8d5f7SChao Yu 	int nr_discarding, nr_discarded;
38895f32366aSChao Yu 	int nr_discard_cmd;
3890d84d1cbdSChao Yu 	unsigned int undiscard_blks;
3891261eeb9cSDaeho Jeong 	int nr_issued_ckpt, nr_total_ckpt, nr_queued_ckpt;
3892261eeb9cSDaeho Jeong 	unsigned int cur_ckpt_time, peak_ckpt_time;
3893a00861dbSJaegeuk Kim 	int inline_xattr, inline_inode, inline_dir, append, update, orphans;
38948ec071c3SChao Yu 	int compr_inode, swapfile_inode;
3895ae999bb9SDaeho Jeong 	unsigned long long compr_blocks;
38967bc155feSJaegeuk Kim 	int aw_cnt, max_aw_cnt;
3897f83a2584SYunlei He 	unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
389839a53e0cSJaegeuk Kim 	unsigned int bimodal, avg_vblocks;
389939a53e0cSJaegeuk Kim 	int util_free, util_valid, util_invalid;
390039a53e0cSJaegeuk Kim 	int rsvd_segs, overp_segs;
39016ce19affSChao Yu 	int dirty_count, node_pages, meta_pages, compress_pages;
39026ce19affSChao Yu 	int compress_page_hit;
390342190d2aSJaegeuk Kim 	int prefree_count, call_count, cp_count, bg_cp_count;
390439a53e0cSJaegeuk Kim 	int tot_segs, node_segs, data_segs, free_segs, free_secs;
3905e1235983SChangman Lee 	int bg_node_segs, bg_data_segs;
390639a53e0cSJaegeuk Kim 	int tot_blks, data_blks, node_blks;
3907e1235983SChangman Lee 	int bg_data_blks, bg_node_blks;
390839a53e0cSJaegeuk Kim 	int curseg[NR_CURSEG_TYPE];
390939a53e0cSJaegeuk Kim 	int cursec[NR_CURSEG_TYPE];
391039a53e0cSJaegeuk Kim 	int curzone[NR_CURSEG_TYPE];
39110759e2c1SChao Yu 	unsigned int dirty_seg[NR_CURSEG_TYPE];
39120759e2c1SChao Yu 	unsigned int full_seg[NR_CURSEG_TYPE];
39130759e2c1SChao Yu 	unsigned int valid_blks[NR_CURSEG_TYPE];
391439a53e0cSJaegeuk Kim 
3915b63e7be5SChao Yu 	unsigned int meta_count[META_MAX];
391639a53e0cSJaegeuk Kim 	unsigned int segment_count[2];
391739a53e0cSJaegeuk Kim 	unsigned int block_count[2];
3918b9a2c252SChangman Lee 	unsigned int inplace_count;
39199edcdabfSChao Yu 	unsigned long long base_mem, cache_mem, page_mem;
392039a53e0cSJaegeuk Kim };
392139a53e0cSJaegeuk Kim 
3922963d4f7dSGu Zheng static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
3923963d4f7dSGu Zheng {
3924963d4f7dSGu Zheng 	return (struct f2fs_stat_info *)sbi->stat_info;
3925963d4f7dSGu Zheng }
3926963d4f7dSGu Zheng 
3927942e0be6SChangman Lee #define stat_inc_cp_count(si)		((si)->cp_count++)
392842190d2aSJaegeuk Kim #define stat_inc_bg_cp_count(si)	((si)->bg_cp_count++)
392939a53e0cSJaegeuk Kim #define stat_inc_call_count(si)		((si)->call_count++)
3930fc7100eaSHridya Valsaraju #define stat_inc_bggc_count(si)		((si)->bg_gc++)
3931274bd9baSChao Yu #define stat_io_skip_bggc_count(sbi)	((sbi)->io_skip_bggc++)
3932274bd9baSChao Yu #define stat_other_skip_bggc_count(sbi)	((sbi)->other_skip_bggc++)
393333fbd510SChao Yu #define stat_inc_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]++)
393433fbd510SChao Yu #define stat_dec_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]--)
3935e7547dacSJaegeuk Kim #define stat_inc_total_hit(sbi, type)		(atomic64_inc(&(sbi)->total_hit_ext[type]))
3936e7547dacSJaegeuk Kim #define stat_inc_rbtree_node_hit(sbi, type)	(atomic64_inc(&(sbi)->read_hit_rbtree[type]))
39375b7ee374SChao Yu #define stat_inc_largest_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_largest))
3938e7547dacSJaegeuk Kim #define stat_inc_cached_node_hit(sbi, type)	(atomic64_inc(&(sbi)->read_hit_cached[type]))
3939d5e8f6c9SChao Yu #define stat_inc_inline_xattr(inode)					\
3940d5e8f6c9SChao Yu 	do {								\
3941d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3942d5e8f6c9SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_xattr));	\
3943d5e8f6c9SChao Yu 	} while (0)
3944d5e8f6c9SChao Yu #define stat_dec_inline_xattr(inode)					\
3945d5e8f6c9SChao Yu 	do {								\
3946d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3947d5e8f6c9SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_xattr));	\
3948d5e8f6c9SChao Yu 	} while (0)
39490dbdc2aeSJaegeuk Kim #define stat_inc_inline_inode(inode)					\
39500dbdc2aeSJaegeuk Kim 	do {								\
39510dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
395203e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_inode));	\
39530dbdc2aeSJaegeuk Kim 	} while (0)
39540dbdc2aeSJaegeuk Kim #define stat_dec_inline_inode(inode)					\
39550dbdc2aeSJaegeuk Kim 	do {								\
39560dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
395703e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_inode));	\
39580dbdc2aeSJaegeuk Kim 	} while (0)
39593289c061SJaegeuk Kim #define stat_inc_inline_dir(inode)					\
39603289c061SJaegeuk Kim 	do {								\
39613289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
396203e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_dir));	\
39633289c061SJaegeuk Kim 	} while (0)
39643289c061SJaegeuk Kim #define stat_dec_inline_dir(inode)					\
39653289c061SJaegeuk Kim 	do {								\
39663289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
396703e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_dir));	\
39683289c061SJaegeuk Kim 	} while (0)
39694c8ff709SChao Yu #define stat_inc_compr_inode(inode)					\
39704c8ff709SChao Yu 	do {								\
39714c8ff709SChao Yu 		if (f2fs_compressed_file(inode))			\
39724c8ff709SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->compr_inode));	\
39734c8ff709SChao Yu 	} while (0)
39744c8ff709SChao Yu #define stat_dec_compr_inode(inode)					\
39754c8ff709SChao Yu 	do {								\
39764c8ff709SChao Yu 		if (f2fs_compressed_file(inode))			\
39774c8ff709SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->compr_inode));	\
39784c8ff709SChao Yu 	} while (0)
39794c8ff709SChao Yu #define stat_add_compr_blocks(inode, blocks)				\
3980ae999bb9SDaeho Jeong 		(atomic64_add(blocks, &F2FS_I_SB(inode)->compr_blocks))
39814c8ff709SChao Yu #define stat_sub_compr_blocks(inode, blocks)				\
3982ae999bb9SDaeho Jeong 		(atomic64_sub(blocks, &F2FS_I_SB(inode)->compr_blocks))
39838ec071c3SChao Yu #define stat_inc_swapfile_inode(inode)					\
39848ec071c3SChao Yu 		(atomic_inc(&F2FS_I_SB(inode)->swapfile_inode))
39858ec071c3SChao Yu #define stat_dec_swapfile_inode(inode)					\
39868ec071c3SChao Yu 		(atomic_dec(&F2FS_I_SB(inode)->swapfile_inode))
3987b4dac120SChao Yu #define stat_inc_atomic_inode(inode)					\
3988b4dac120SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->atomic_files))
3989b4dac120SChao Yu #define stat_dec_atomic_inode(inode)					\
3990b4dac120SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->atomic_files))
3991b63e7be5SChao Yu #define stat_inc_meta_count(sbi, blkaddr)				\
3992b63e7be5SChao Yu 	do {								\
3993b63e7be5SChao Yu 		if (blkaddr < SIT_I(sbi)->sit_base_addr)		\
3994b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_CP]);	\
3995b63e7be5SChao Yu 		else if (blkaddr < NM_I(sbi)->nat_blkaddr)		\
3996b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_SIT]);	\
3997b63e7be5SChao Yu 		else if (blkaddr < SM_I(sbi)->ssa_blkaddr)		\
3998b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_NAT]);	\
3999b63e7be5SChao Yu 		else if (blkaddr < SM_I(sbi)->main_blkaddr)		\
4000b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_SSA]);	\
4001b63e7be5SChao Yu 	} while (0)
4002dcdfff65SJaegeuk Kim #define stat_inc_seg_type(sbi, curseg)					\
4003dcdfff65SJaegeuk Kim 		((sbi)->segment_count[(curseg)->alloc_type]++)
4004dcdfff65SJaegeuk Kim #define stat_inc_block_count(sbi, curseg)				\
4005dcdfff65SJaegeuk Kim 		((sbi)->block_count[(curseg)->alloc_type]++)
4006b9a2c252SChangman Lee #define stat_inc_inplace_blocks(sbi)					\
4007b9a2c252SChangman Lee 		(atomic_inc(&(sbi)->inplace_count))
400826a28a0cSJaegeuk Kim #define stat_update_max_atomic_write(inode)				\
400926a28a0cSJaegeuk Kim 	do {								\
4010b4dac120SChao Yu 		int cur = atomic_read(&F2FS_I_SB(inode)->atomic_files);	\
401126a28a0cSJaegeuk Kim 		int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt);	\
401226a28a0cSJaegeuk Kim 		if (cur > max)						\
401326a28a0cSJaegeuk Kim 			atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur);	\
401426a28a0cSJaegeuk Kim 	} while (0)
4015e1235983SChangman Lee #define stat_inc_seg_count(sbi, type, gc_type)				\
401639a53e0cSJaegeuk Kim 	do {								\
4017963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
401868afcf2dSTomohiro Kusumi 		si->tot_segs++;						\
401968afcf2dSTomohiro Kusumi 		if ((type) == SUM_TYPE_DATA) {				\
402039a53e0cSJaegeuk Kim 			si->data_segs++;				\
4021e1235983SChangman Lee 			si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0;	\
4022e1235983SChangman Lee 		} else {						\
402339a53e0cSJaegeuk Kim 			si->node_segs++;				\
4024e1235983SChangman Lee 			si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0;	\
4025e1235983SChangman Lee 		}							\
402639a53e0cSJaegeuk Kim 	} while (0)
402739a53e0cSJaegeuk Kim 
402839a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks)				\
402968afcf2dSTomohiro Kusumi 	((si)->tot_blks += (blks))
403039a53e0cSJaegeuk Kim 
4031e1235983SChangman Lee #define stat_inc_data_blk_count(sbi, blks, gc_type)			\
403239a53e0cSJaegeuk Kim 	do {								\
4033963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
403439a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
403539a53e0cSJaegeuk Kim 		si->data_blks += (blks);				\
403668afcf2dSTomohiro Kusumi 		si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
403739a53e0cSJaegeuk Kim 	} while (0)
403839a53e0cSJaegeuk Kim 
4039e1235983SChangman Lee #define stat_inc_node_blk_count(sbi, blks, gc_type)			\
404039a53e0cSJaegeuk Kim 	do {								\
4041963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
404239a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
404339a53e0cSJaegeuk Kim 		si->node_blks += (blks);				\
404468afcf2dSTomohiro Kusumi 		si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
404539a53e0cSJaegeuk Kim 	} while (0)
404639a53e0cSJaegeuk Kim 
4047cac5a3d8SDongOh Shin int f2fs_build_stats(struct f2fs_sb_info *sbi);
4048cac5a3d8SDongOh Shin void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
404921acc07dSGreg Kroah-Hartman void __init f2fs_create_root_stats(void);
40504589d25dSNamjae Jeon void f2fs_destroy_root_stats(void);
4051fc7100eaSHridya Valsaraju void f2fs_update_sit_info(struct f2fs_sb_info *sbi);
405239a53e0cSJaegeuk Kim #else
4053d66450e7SArnd Bergmann #define stat_inc_cp_count(si)				do { } while (0)
4054d66450e7SArnd Bergmann #define stat_inc_bg_cp_count(si)			do { } while (0)
4055d66450e7SArnd Bergmann #define stat_inc_call_count(si)				do { } while (0)
4056d66450e7SArnd Bergmann #define stat_inc_bggc_count(si)				do { } while (0)
4057274bd9baSChao Yu #define stat_io_skip_bggc_count(sbi)			do { } while (0)
4058274bd9baSChao Yu #define stat_other_skip_bggc_count(sbi)			do { } while (0)
4059d66450e7SArnd Bergmann #define stat_inc_dirty_inode(sbi, type)			do { } while (0)
4060d66450e7SArnd Bergmann #define stat_dec_dirty_inode(sbi, type)			do { } while (0)
4061e7547dacSJaegeuk Kim #define stat_inc_total_hit(sbi, type)			do { } while (0)
4062e7547dacSJaegeuk Kim #define stat_inc_rbtree_node_hit(sbi, type)		do { } while (0)
4063d66450e7SArnd Bergmann #define stat_inc_largest_node_hit(sbi)			do { } while (0)
4064e7547dacSJaegeuk Kim #define stat_inc_cached_node_hit(sbi, type)		do { } while (0)
4065d66450e7SArnd Bergmann #define stat_inc_inline_xattr(inode)			do { } while (0)
4066d66450e7SArnd Bergmann #define stat_dec_inline_xattr(inode)			do { } while (0)
4067d66450e7SArnd Bergmann #define stat_inc_inline_inode(inode)			do { } while (0)
4068d66450e7SArnd Bergmann #define stat_dec_inline_inode(inode)			do { } while (0)
4069d66450e7SArnd Bergmann #define stat_inc_inline_dir(inode)			do { } while (0)
4070d66450e7SArnd Bergmann #define stat_dec_inline_dir(inode)			do { } while (0)
40714c8ff709SChao Yu #define stat_inc_compr_inode(inode)			do { } while (0)
40724c8ff709SChao Yu #define stat_dec_compr_inode(inode)			do { } while (0)
40734c8ff709SChao Yu #define stat_add_compr_blocks(inode, blocks)		do { } while (0)
40744c8ff709SChao Yu #define stat_sub_compr_blocks(inode, blocks)		do { } while (0)
40758ec071c3SChao Yu #define stat_inc_swapfile_inode(inode)			do { } while (0)
40768ec071c3SChao Yu #define stat_dec_swapfile_inode(inode)			do { } while (0)
4077b4dac120SChao Yu #define stat_inc_atomic_inode(inode)			do { } while (0)
4078b4dac120SChao Yu #define stat_dec_atomic_inode(inode)			do { } while (0)
4079d66450e7SArnd Bergmann #define stat_update_max_atomic_write(inode)		do { } while (0)
4080b63e7be5SChao Yu #define stat_inc_meta_count(sbi, blkaddr)		do { } while (0)
4081d66450e7SArnd Bergmann #define stat_inc_seg_type(sbi, curseg)			do { } while (0)
4082d66450e7SArnd Bergmann #define stat_inc_block_count(sbi, curseg)		do { } while (0)
4083d66450e7SArnd Bergmann #define stat_inc_inplace_blocks(sbi)			do { } while (0)
4084d66450e7SArnd Bergmann #define stat_inc_seg_count(sbi, type, gc_type)		do { } while (0)
4085d66450e7SArnd Bergmann #define stat_inc_tot_blk_count(si, blks)		do { } while (0)
4086d66450e7SArnd Bergmann #define stat_inc_data_blk_count(sbi, blks, gc_type)	do { } while (0)
4087d66450e7SArnd Bergmann #define stat_inc_node_blk_count(sbi, blks, gc_type)	do { } while (0)
408839a53e0cSJaegeuk Kim 
408939a53e0cSJaegeuk Kim static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
409039a53e0cSJaegeuk Kim static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
409121acc07dSGreg Kroah-Hartman static inline void __init f2fs_create_root_stats(void) { }
40924589d25dSNamjae Jeon static inline void f2fs_destroy_root_stats(void) { }
409348abe91aSYueHaibing static inline void f2fs_update_sit_info(struct f2fs_sb_info *sbi) {}
409439a53e0cSJaegeuk Kim #endif
409539a53e0cSJaegeuk Kim 
409639a53e0cSJaegeuk Kim extern const struct file_operations f2fs_dir_operations;
409739a53e0cSJaegeuk Kim extern const struct file_operations f2fs_file_operations;
409839a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_file_inode_operations;
409939a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_dblock_aops;
410039a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_node_aops;
410139a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_meta_aops;
410239a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_dir_inode_operations;
410339a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_symlink_inode_operations;
4104cbaf042aSJaegeuk Kim extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
410539a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_special_inode_operations;
41064d57b86dSChao Yu extern struct kmem_cache *f2fs_inode_entry_slab;
41071001b347SHuajun Li 
4108e18c65b2SHuajun Li /*
4109e18c65b2SHuajun Li  * inline.c
4110e18c65b2SHuajun Li  */
4111cac5a3d8SDongOh Shin bool f2fs_may_inline_data(struct inode *inode);
4112677a82b4SChao Yu bool f2fs_sanity_check_inline_data(struct inode *inode);
4113cac5a3d8SDongOh Shin bool f2fs_may_inline_dentry(struct inode *inode);
41144d57b86dSChao Yu void f2fs_do_read_inline_data(struct page *page, struct page *ipage);
41154d57b86dSChao Yu void f2fs_truncate_inline_inode(struct inode *inode,
41164d57b86dSChao Yu 						struct page *ipage, u64 from);
4117cac5a3d8SDongOh Shin int f2fs_read_inline_data(struct inode *inode, struct page *page);
4118cac5a3d8SDongOh Shin int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
4119cac5a3d8SDongOh Shin int f2fs_convert_inline_inode(struct inode *inode);
4120b06af2afSJaegeuk Kim int f2fs_try_convert_inline_dir(struct inode *dir, struct dentry *dentry);
4121cac5a3d8SDongOh Shin int f2fs_write_inline_data(struct inode *inode, struct page *page);
41229627a7b3SChao Yu int f2fs_recover_inline_data(struct inode *inode, struct page *npage);
41234d57b86dSChao Yu struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
412443c780baSEric Biggers 					const struct f2fs_filename *fname,
412543c780baSEric Biggers 					struct page **res_page);
41264d57b86dSChao Yu int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent,
4127cac5a3d8SDongOh Shin 			struct page *ipage);
412843c780baSEric Biggers int f2fs_add_inline_entry(struct inode *dir, const struct f2fs_filename *fname,
4129cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
41304d57b86dSChao Yu void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry,
41314d57b86dSChao Yu 				struct page *page, struct inode *dir,
41324d57b86dSChao Yu 				struct inode *inode);
4133cac5a3d8SDongOh Shin bool f2fs_empty_inline_dir(struct inode *dir);
4134cac5a3d8SDongOh Shin int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
4135cac5a3d8SDongOh Shin 			struct fscrypt_str *fstr);
4136cac5a3d8SDongOh Shin int f2fs_inline_data_fiemap(struct inode *inode,
4137cac5a3d8SDongOh Shin 			struct fiemap_extent_info *fieinfo,
4138cac5a3d8SDongOh Shin 			__u64 start, __u64 len);
4139cde4de12SJaegeuk Kim 
4140cde4de12SJaegeuk Kim /*
41412658e50dSJaegeuk Kim  * shrinker.c
41422658e50dSJaegeuk Kim  */
4143cac5a3d8SDongOh Shin unsigned long f2fs_shrink_count(struct shrinker *shrink,
4144cac5a3d8SDongOh Shin 			struct shrink_control *sc);
4145cac5a3d8SDongOh Shin unsigned long f2fs_shrink_scan(struct shrinker *shrink,
4146cac5a3d8SDongOh Shin 			struct shrink_control *sc);
4147cac5a3d8SDongOh Shin void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
4148cac5a3d8SDongOh Shin void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
41492658e50dSJaegeuk Kim 
41502658e50dSJaegeuk Kim /*
4151a28ef1f5SChao Yu  * extent_cache.c
4152a28ef1f5SChao Yu  */
4153269d1194SChao Yu bool sanity_check_extent_cache(struct inode *inode);
415472840cccSJaegeuk Kim void f2fs_init_extent_tree(struct inode *inode);
4155cac5a3d8SDongOh Shin void f2fs_drop_extent_tree(struct inode *inode);
4156e7547dacSJaegeuk Kim void f2fs_destroy_extent_node(struct inode *inode);
4157cac5a3d8SDongOh Shin void f2fs_destroy_extent_tree(struct inode *inode);
41584d57b86dSChao Yu void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi);
41594d57b86dSChao Yu int __init f2fs_create_extent_cache(void);
41604d57b86dSChao Yu void f2fs_destroy_extent_cache(void);
4161a28ef1f5SChao Yu 
4162e7547dacSJaegeuk Kim /* read extent cache ops */
416372840cccSJaegeuk Kim void f2fs_init_read_extent_tree(struct inode *inode, struct page *ipage);
4164e7547dacSJaegeuk Kim bool f2fs_lookup_read_extent_cache(struct inode *inode, pgoff_t pgofs,
4165e7547dacSJaegeuk Kim 			struct extent_info *ei);
416604a91ab0SChristoph Hellwig bool f2fs_lookup_read_extent_cache_block(struct inode *inode, pgoff_t index,
416704a91ab0SChristoph Hellwig 			block_t *blkaddr);
4168e7547dacSJaegeuk Kim void f2fs_update_read_extent_cache(struct dnode_of_data *dn);
4169e7547dacSJaegeuk Kim void f2fs_update_read_extent_cache_range(struct dnode_of_data *dn,
4170e7547dacSJaegeuk Kim 			pgoff_t fofs, block_t blkaddr, unsigned int len);
4171e7547dacSJaegeuk Kim unsigned int f2fs_shrink_read_extent_tree(struct f2fs_sb_info *sbi,
4172e7547dacSJaegeuk Kim 			int nr_shrink);
4173e7547dacSJaegeuk Kim 
417471644dffSJaegeuk Kim /* block age extent cache ops */
417571644dffSJaegeuk Kim void f2fs_init_age_extent_tree(struct inode *inode);
417671644dffSJaegeuk Kim bool f2fs_lookup_age_extent_cache(struct inode *inode, pgoff_t pgofs,
417771644dffSJaegeuk Kim 			struct extent_info *ei);
417871644dffSJaegeuk Kim void f2fs_update_age_extent_cache(struct dnode_of_data *dn);
417971644dffSJaegeuk Kim void f2fs_update_age_extent_cache_range(struct dnode_of_data *dn,
418071644dffSJaegeuk Kim 			pgoff_t fofs, unsigned int len);
418171644dffSJaegeuk Kim unsigned int f2fs_shrink_age_extent_tree(struct f2fs_sb_info *sbi,
418271644dffSJaegeuk Kim 			int nr_shrink);
418371644dffSJaegeuk Kim 
4184a28ef1f5SChao Yu /*
41858ceffcb2SChao Yu  * sysfs.c
41868ceffcb2SChao Yu  */
41870f6b56ecSDaeho Jeong #define MIN_RA_MUL	2
41880f6b56ecSDaeho Jeong #define MAX_RA_MUL	256
41890f6b56ecSDaeho Jeong 
4190dc6b2055SJaegeuk Kim int __init f2fs_init_sysfs(void);
4191dc6b2055SJaegeuk Kim void f2fs_exit_sysfs(void);
4192dc6b2055SJaegeuk Kim int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
4193dc6b2055SJaegeuk Kim void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
41948ceffcb2SChao Yu 
419595ae251fSEric Biggers /* verity.c */
419695ae251fSEric Biggers extern const struct fsverity_operations f2fs_verityops;
419795ae251fSEric Biggers 
41988ceffcb2SChao Yu /*
4199cde4de12SJaegeuk Kim  * crypto support
4200cde4de12SJaegeuk Kim  */
42011958593eSJaegeuk Kim static inline bool f2fs_encrypted_file(struct inode *inode)
42021958593eSJaegeuk Kim {
420362230e0dSChandan Rajendra 	return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
42041958593eSJaegeuk Kim }
42051958593eSJaegeuk Kim 
4206cde4de12SJaegeuk Kim static inline void f2fs_set_encrypted_inode(struct inode *inode)
4207cde4de12SJaegeuk Kim {
4208643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
4209cde4de12SJaegeuk Kim 	file_set_encrypt(inode);
42109149a5ebSChao Yu 	f2fs_set_inode_flags(inode);
4211cde4de12SJaegeuk Kim #endif
4212cde4de12SJaegeuk Kim }
4213cde4de12SJaegeuk Kim 
42146dbb1796SEric Biggers /*
42156dbb1796SEric Biggers  * Returns true if the reads of the inode's data need to undergo some
42166dbb1796SEric Biggers  * postprocessing step, like decryption or authenticity verification.
42176dbb1796SEric Biggers  */
42186dbb1796SEric Biggers static inline bool f2fs_post_read_required(struct inode *inode)
4219cde4de12SJaegeuk Kim {
42204c8ff709SChao Yu 	return f2fs_encrypted_file(inode) || fsverity_active(inode) ||
42214c8ff709SChao Yu 		f2fs_compressed_file(inode);
42224c8ff709SChao Yu }
42234c8ff709SChao Yu 
42244c8ff709SChao Yu /*
42254c8ff709SChao Yu  * compress.c
42264c8ff709SChao Yu  */
42274c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
42284c8ff709SChao Yu bool f2fs_is_compressed_page(struct page *page);
42294c8ff709SChao Yu struct page *f2fs_compress_control_page(struct page *page);
42304c8ff709SChao Yu int f2fs_prepare_compress_overwrite(struct inode *inode,
42314c8ff709SChao Yu 			struct page **pagep, pgoff_t index, void **fsdata);
42324c8ff709SChao Yu bool f2fs_compress_write_end(struct inode *inode, void *fsdata,
42334c8ff709SChao Yu 					pgoff_t index, unsigned copied);
42343265d3dbSChao Yu int f2fs_truncate_partial_cluster(struct inode *inode, u64 from, bool lock);
42354c8ff709SChao Yu void f2fs_compress_write_end_io(struct bio *bio, struct page *page);
42364c8ff709SChao Yu bool f2fs_is_compress_backend_ready(struct inode *inode);
4237a1357a91SYangtao Li int __init f2fs_init_compress_mempool(void);
42385e6bbde9SChao Yu void f2fs_destroy_compress_mempool(void);
4239bff139b4SDaeho Jeong void f2fs_decompress_cluster(struct decompress_io_ctx *dic, bool in_task);
42406ce19affSChao Yu void f2fs_end_read_compressed_page(struct page *page, bool failed,
4241bff139b4SDaeho Jeong 				block_t blkaddr, bool in_task);
42424c8ff709SChao Yu bool f2fs_cluster_is_empty(struct compress_ctx *cc);
42434c8ff709SChao Yu bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index);
424401fc4b9aSFengnan Chang bool f2fs_all_cluster_page_ready(struct compress_ctx *cc, struct page **pages,
42454f8219f8SFengnan Chang 				int index, int nr_pages, bool uptodate);
4246bbe1da7eSChao Yu bool f2fs_sanity_check_cluster(struct dnode_of_data *dn);
42474c8ff709SChao Yu void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct page *page);
42484c8ff709SChao Yu int f2fs_write_multi_pages(struct compress_ctx *cc,
42494c8ff709SChao Yu 						int *submitted,
42504c8ff709SChao Yu 						struct writeback_control *wbc,
42514c8ff709SChao Yu 						enum iostat_type io_type);
42524c8ff709SChao Yu int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index);
4253e7547dacSJaegeuk Kim void f2fs_update_read_extent_tree_range_compressed(struct inode *inode,
4254e7547dacSJaegeuk Kim 				pgoff_t fofs, block_t blkaddr,
4255e7547dacSJaegeuk Kim 				unsigned int llen, unsigned int c_len);
42564c8ff709SChao Yu int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
42574c8ff709SChao Yu 				unsigned nr_pages, sector_t *last_block_in_bio,
42580683728aSChao Yu 				bool is_readahead, bool for_write);
42594c8ff709SChao Yu struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc);
4260bff139b4SDaeho Jeong void f2fs_decompress_end_io(struct decompress_io_ctx *dic, bool failed,
4261bff139b4SDaeho Jeong 				bool in_task);
4262bff139b4SDaeho Jeong void f2fs_put_page_dic(struct page *page, bool in_task);
426394afd6d6SChao Yu unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn);
42644c8ff709SChao Yu int f2fs_init_compress_ctx(struct compress_ctx *cc);
42658bfbfb0dSChao Yu void f2fs_destroy_compress_ctx(struct compress_ctx *cc, bool reuse);
42664c8ff709SChao Yu void f2fs_init_compress_info(struct f2fs_sb_info *sbi);
42676ce19affSChao Yu int f2fs_init_compress_inode(struct f2fs_sb_info *sbi);
42686ce19affSChao Yu void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi);
426931083031SChao Yu int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi);
427031083031SChao Yu void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi);
4271c68d6c88SChao Yu int __init f2fs_init_compress_cache(void);
4272c68d6c88SChao Yu void f2fs_destroy_compress_cache(void);
42736ce19affSChao Yu struct address_space *COMPRESS_MAPPING(struct f2fs_sb_info *sbi);
42746ce19affSChao Yu void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi, block_t blkaddr);
42756ce19affSChao Yu void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
42766ce19affSChao Yu 						nid_t ino, block_t blkaddr);
42776ce19affSChao Yu bool f2fs_load_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
42786ce19affSChao Yu 								block_t blkaddr);
42796ce19affSChao Yu void f2fs_invalidate_compress_pages(struct f2fs_sb_info *sbi, nid_t ino);
42805ac443e2SDaeho Jeong #define inc_compr_inode_stat(inode)					\
42815ac443e2SDaeho Jeong 	do {								\
42825ac443e2SDaeho Jeong 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);		\
42835ac443e2SDaeho Jeong 		sbi->compr_new_inode++;					\
42845ac443e2SDaeho Jeong 	} while (0)
42855ac443e2SDaeho Jeong #define add_compr_block_stat(inode, blocks)				\
42865ac443e2SDaeho Jeong 	do {								\
42875ac443e2SDaeho Jeong 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);		\
42885ac443e2SDaeho Jeong 		int diff = F2FS_I(inode)->i_cluster_size - blocks;	\
42895ac443e2SDaeho Jeong 		sbi->compr_written_block += blocks;			\
42905ac443e2SDaeho Jeong 		sbi->compr_saved_block += diff;				\
42915ac443e2SDaeho Jeong 	} while (0)
42924c8ff709SChao Yu #else
42934c8ff709SChao Yu static inline bool f2fs_is_compressed_page(struct page *page) { return false; }
42944c8ff709SChao Yu static inline bool f2fs_is_compress_backend_ready(struct inode *inode)
42954c8ff709SChao Yu {
42964c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
42974c8ff709SChao Yu 		return true;
42984c8ff709SChao Yu 	/* not support compression */
42994c8ff709SChao Yu 	return false;
43004c8ff709SChao Yu }
43014c8ff709SChao Yu static inline struct page *f2fs_compress_control_page(struct page *page)
43024c8ff709SChao Yu {
43034c8ff709SChao Yu 	WARN_ON_ONCE(1);
43044c8ff709SChao Yu 	return ERR_PTR(-EINVAL);
43054c8ff709SChao Yu }
4306a1357a91SYangtao Li static inline int __init f2fs_init_compress_mempool(void) { return 0; }
43075e6bbde9SChao Yu static inline void f2fs_destroy_compress_mempool(void) { }
4308bff139b4SDaeho Jeong static inline void f2fs_decompress_cluster(struct decompress_io_ctx *dic,
4309bff139b4SDaeho Jeong 				bool in_task) { }
43106ce19affSChao Yu static inline void f2fs_end_read_compressed_page(struct page *page,
4311bff139b4SDaeho Jeong 				bool failed, block_t blkaddr, bool in_task)
43127f59b277SEric Biggers {
43137f59b277SEric Biggers 	WARN_ON_ONCE(1);
43147f59b277SEric Biggers }
4315bff139b4SDaeho Jeong static inline void f2fs_put_page_dic(struct page *page, bool in_task)
43167f59b277SEric Biggers {
43177f59b277SEric Biggers 	WARN_ON_ONCE(1);
43187f59b277SEric Biggers }
431994afd6d6SChao Yu static inline unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn) { return 0; }
4320bbe1da7eSChao Yu static inline bool f2fs_sanity_check_cluster(struct dnode_of_data *dn) { return false; }
43216ce19affSChao Yu static inline int f2fs_init_compress_inode(struct f2fs_sb_info *sbi) { return 0; }
43226ce19affSChao Yu static inline void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi) { }
432331083031SChao Yu static inline int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi) { return 0; }
432431083031SChao Yu static inline void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi) { }
4325c68d6c88SChao Yu static inline int __init f2fs_init_compress_cache(void) { return 0; }
4326c68d6c88SChao Yu static inline void f2fs_destroy_compress_cache(void) { }
43276ce19affSChao Yu static inline void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi,
43286ce19affSChao Yu 				block_t blkaddr) { }
43296ce19affSChao Yu static inline void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi,
43306ce19affSChao Yu 				struct page *page, nid_t ino, block_t blkaddr) { }
43316ce19affSChao Yu static inline bool f2fs_load_compressed_page(struct f2fs_sb_info *sbi,
43326ce19affSChao Yu 				struct page *page, block_t blkaddr) { return false; }
43336ce19affSChao Yu static inline void f2fs_invalidate_compress_pages(struct f2fs_sb_info *sbi,
43346ce19affSChao Yu 							nid_t ino) { }
43355ac443e2SDaeho Jeong #define inc_compr_inode_stat(inode)		do { } while (0)
4336e7547dacSJaegeuk Kim static inline void f2fs_update_read_extent_tree_range_compressed(
4337e7547dacSJaegeuk Kim 				struct inode *inode,
4338e7547dacSJaegeuk Kim 				pgoff_t fofs, block_t blkaddr,
4339e7547dacSJaegeuk Kim 				unsigned int llen, unsigned int c_len) { }
43404c8ff709SChao Yu #endif
43414c8ff709SChao Yu 
4342912f0d65SJaegeuk Kim static inline int set_compress_context(struct inode *inode)
43434c8ff709SChao Yu {
4344912f0d65SJaegeuk Kim #ifdef CONFIG_F2FS_FS_COMPRESSION
43454c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
43464c8ff709SChao Yu 
43474c8ff709SChao Yu 	F2FS_I(inode)->i_compress_algorithm =
43484c8ff709SChao Yu 			F2FS_OPTION(sbi).compress_algorithm;
43494c8ff709SChao Yu 	F2FS_I(inode)->i_log_cluster_size =
43504c8ff709SChao Yu 			F2FS_OPTION(sbi).compress_log_size;
4351b28f047bSChao Yu 	F2FS_I(inode)->i_compress_flag =
4352b28f047bSChao Yu 			F2FS_OPTION(sbi).compress_chksum ?
4353447286ebSYangtao Li 				BIT(COMPRESS_CHKSUM) : 0;
43544c8ff709SChao Yu 	F2FS_I(inode)->i_cluster_size =
4355447286ebSYangtao Li 			BIT(F2FS_I(inode)->i_log_cluster_size);
435601f6afd0SChao Yu 	if ((F2FS_I(inode)->i_compress_algorithm == COMPRESS_LZ4 ||
435701f6afd0SChao Yu 		F2FS_I(inode)->i_compress_algorithm == COMPRESS_ZSTD) &&
43583fde13f8SChao Yu 			F2FS_OPTION(sbi).compress_level)
4359b90e5086SChao Yu 		F2FS_I(inode)->i_compress_level =
4360b90e5086SChao Yu 				F2FS_OPTION(sbi).compress_level;
43614c8ff709SChao Yu 	F2FS_I(inode)->i_flags |= F2FS_COMPR_FL;
43624c8ff709SChao Yu 	set_inode_flag(inode, FI_COMPRESSED_FILE);
43634c8ff709SChao Yu 	stat_inc_compr_inode(inode);
43645ac443e2SDaeho Jeong 	inc_compr_inode_stat(inode);
4365530e0704SChao Yu 	f2fs_mark_inode_dirty_sync(inode, true);
4366912f0d65SJaegeuk Kim 	return 0;
4367912f0d65SJaegeuk Kim #else
4368912f0d65SJaegeuk Kim 	return -EOPNOTSUPP;
4369912f0d65SJaegeuk Kim #endif
43704c8ff709SChao Yu }
43714c8ff709SChao Yu 
437278134d03SDaeho Jeong static inline bool f2fs_disable_compressed_file(struct inode *inode)
43734c8ff709SChao Yu {
43744c8ff709SChao Yu 	struct f2fs_inode_info *fi = F2FS_I(inode);
43754c8ff709SChao Yu 
43764c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
437778134d03SDaeho Jeong 		return true;
437802d58cd2SHyeong-Jun Kim 	if (S_ISREG(inode->i_mode) && F2FS_HAS_BLOCKS(inode))
437978134d03SDaeho Jeong 		return false;
43804c8ff709SChao Yu 
43814c8ff709SChao Yu 	fi->i_flags &= ~F2FS_COMPR_FL;
43824c8ff709SChao Yu 	stat_dec_compr_inode(inode);
438396f5b4faSChao Yu 	clear_inode_flag(inode, FI_COMPRESSED_FILE);
4384530e0704SChao Yu 	f2fs_mark_inode_dirty_sync(inode, true);
438578134d03SDaeho Jeong 	return true;
4386cde4de12SJaegeuk Kim }
4387cde4de12SJaegeuk Kim 
4388ccd31cb2SSheng Yong #define F2FS_FEATURE_FUNCS(name, flagname) \
4389e4807519SYangtao Li static inline bool f2fs_sb_has_##name(struct f2fs_sb_info *sbi) \
4390ccd31cb2SSheng Yong { \
43917beb01f7SChao Yu 	return F2FS_HAS_FEATURE(sbi, F2FS_FEATURE_##flagname); \
4392cde4de12SJaegeuk Kim }
4393f424f664SJaegeuk Kim 
4394ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(encrypt, ENCRYPT);
4395ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(blkzoned, BLKZONED);
4396ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(extra_attr, EXTRA_ATTR);
4397ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(project_quota, PRJQUOTA);
4398ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_chksum, INODE_CHKSUM);
4399ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
4400ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
4401ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
4402b7c409deSSheng Yong F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
440395ae251fSEric Biggers F2FS_FEATURE_FUNCS(verity, VERITY);
4404d440c52dSJunling Zheng F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
44055aba5430SDaniel Rosenberg F2FS_FEATURE_FUNCS(casefold, CASEFOLD);
44064c8ff709SChao Yu F2FS_FEATURE_FUNCS(compression, COMPRESSION);
4407a7d9fe3cSJaegeuk Kim F2FS_FEATURE_FUNCS(readonly, RO);
44081c1d35dfSChao Yu 
4409178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
441095175dafSDamien Le Moal static inline bool f2fs_blkz_is_seq(struct f2fs_sb_info *sbi, int devi,
441195175dafSDamien Le Moal 				    block_t blkaddr)
4412178053e2SDamien Le Moal {
44132e2c6e9bSJaegeuk Kim 	unsigned int zno = blkaddr / sbi->blocks_per_blkz;
4414178053e2SDamien Le Moal 
441595175dafSDamien Le Moal 	return test_bit(zno, FDEV(devi).blkz_seq);
4416178053e2SDamien Le Moal }
4417178053e2SDamien Le Moal #endif
4418178053e2SDamien Le Moal 
44197d20c8abSChao Yu static inline bool f2fs_hw_should_discard(struct f2fs_sb_info *sbi)
442096ba2decSDamien Le Moal {
44217beb01f7SChao Yu 	return f2fs_sb_has_blkzoned(sbi);
44227d20c8abSChao Yu }
442396ba2decSDamien Le Moal 
44247f3d7719SDamien Le Moal static inline bool f2fs_bdev_support_discard(struct block_device *bdev)
44257f3d7719SDamien Le Moal {
442670200574SChristoph Hellwig 	return bdev_max_discard_sectors(bdev) || bdev_is_zoned(bdev);
44277f3d7719SDamien Le Moal }
44287f3d7719SDamien Le Moal 
44297d20c8abSChao Yu static inline bool f2fs_hw_support_discard(struct f2fs_sb_info *sbi)
44307d20c8abSChao Yu {
44317f3d7719SDamien Le Moal 	int i;
44327f3d7719SDamien Le Moal 
44337f3d7719SDamien Le Moal 	if (!f2fs_is_multi_device(sbi))
44347f3d7719SDamien Le Moal 		return f2fs_bdev_support_discard(sbi->sb->s_bdev);
44357f3d7719SDamien Le Moal 
44367f3d7719SDamien Le Moal 	for (i = 0; i < sbi->s_ndevs; i++)
44377f3d7719SDamien Le Moal 		if (f2fs_bdev_support_discard(FDEV(i).bdev))
44387f3d7719SDamien Le Moal 			return true;
44397f3d7719SDamien Le Moal 	return false;
44407d20c8abSChao Yu }
44417d20c8abSChao Yu 
44427d20c8abSChao Yu static inline bool f2fs_realtime_discard_enable(struct f2fs_sb_info *sbi)
44437d20c8abSChao Yu {
44447d20c8abSChao Yu 	return (test_opt(sbi, DISCARD) && f2fs_hw_support_discard(sbi)) ||
44457d20c8abSChao Yu 					f2fs_hw_should_discard(sbi);
444652763a4bSJaegeuk Kim }
444752763a4bSJaegeuk Kim 
4448f824deb5SChao Yu static inline bool f2fs_hw_is_readonly(struct f2fs_sb_info *sbi)
4449f824deb5SChao Yu {
4450f824deb5SChao Yu 	int i;
4451f824deb5SChao Yu 
4452f824deb5SChao Yu 	if (!f2fs_is_multi_device(sbi))
4453f824deb5SChao Yu 		return bdev_read_only(sbi->sb->s_bdev);
4454f824deb5SChao Yu 
4455f824deb5SChao Yu 	for (i = 0; i < sbi->s_ndevs; i++)
4456f824deb5SChao Yu 		if (bdev_read_only(FDEV(i).bdev))
4457f824deb5SChao Yu 			return true;
4458f824deb5SChao Yu 	return false;
4459f824deb5SChao Yu }
4460f824deb5SChao Yu 
4461d78dfefcSChao Yu static inline bool f2fs_dev_is_readonly(struct f2fs_sb_info *sbi)
4462d78dfefcSChao Yu {
4463d78dfefcSChao Yu 	return f2fs_sb_has_readonly(sbi) || f2fs_hw_is_readonly(sbi);
4464d78dfefcSChao Yu }
4465d78dfefcSChao Yu 
4466b0332a0fSChao Yu static inline bool f2fs_lfs_mode(struct f2fs_sb_info *sbi)
446752763a4bSJaegeuk Kim {
4468b0332a0fSChao Yu 	return F2FS_OPTION(sbi).fs_mode == FS_MODE_LFS;
446952763a4bSJaegeuk Kim }
447052763a4bSJaegeuk Kim 
44717a8fc586SDaeho Jeong static inline bool f2fs_low_mem_mode(struct f2fs_sb_info *sbi)
44727a8fc586SDaeho Jeong {
44737a8fc586SDaeho Jeong 	return F2FS_OPTION(sbi).memory_mode == MEMORY_MODE_LOW;
44747a8fc586SDaeho Jeong }
44757a8fc586SDaeho Jeong 
44764c8ff709SChao Yu static inline bool f2fs_may_compress(struct inode *inode)
44774c8ff709SChao Yu {
44784c8ff709SChao Yu 	if (IS_SWAPFILE(inode) || f2fs_is_pinned_file(inode) ||
44797165841dSChao Yu 		f2fs_is_atomic_file(inode) || f2fs_has_inline_data(inode))
44804c8ff709SChao Yu 		return false;
44814c8ff709SChao Yu 	return S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode);
44824c8ff709SChao Yu }
44834c8ff709SChao Yu 
44844c8ff709SChao Yu static inline void f2fs_i_compr_blocks_update(struct inode *inode,
44854c8ff709SChao Yu 						u64 blocks, bool add)
44864c8ff709SChao Yu {
4487c2759ebaSDaeho Jeong 	struct f2fs_inode_info *fi = F2FS_I(inode);
4488054cb289SYufen Yu 	int diff = fi->i_cluster_size - blocks;
44894c8ff709SChao Yu 
4490ef8d563fSChao Yu 	/* don't update i_compr_blocks if saved blocks were released */
4491c2759ebaSDaeho Jeong 	if (!add && !atomic_read(&fi->i_compr_blocks))
4492ef8d563fSChao Yu 		return;
4493ef8d563fSChao Yu 
44944c8ff709SChao Yu 	if (add) {
4495c2759ebaSDaeho Jeong 		atomic_add(diff, &fi->i_compr_blocks);
44964c8ff709SChao Yu 		stat_add_compr_blocks(inode, diff);
44974c8ff709SChao Yu 	} else {
4498c2759ebaSDaeho Jeong 		atomic_sub(diff, &fi->i_compr_blocks);
44994c8ff709SChao Yu 		stat_sub_compr_blocks(inode, diff);
45004c8ff709SChao Yu 	}
45014c8ff709SChao Yu 	f2fs_mark_inode_dirty_sync(inode, true);
45024c8ff709SChao Yu }
45034c8ff709SChao Yu 
450471f2c820SChao Yu static inline bool f2fs_allow_multi_device_dio(struct f2fs_sb_info *sbi,
450571f2c820SChao Yu 								int flag)
450671f2c820SChao Yu {
450771f2c820SChao Yu 	if (!f2fs_is_multi_device(sbi))
450871f2c820SChao Yu 		return false;
450971f2c820SChao Yu 	if (flag != F2FS_GET_BLOCK_DIO)
451071f2c820SChao Yu 		return false;
451171f2c820SChao Yu 	return sbi->aligned_blksize;
451271f2c820SChao Yu }
451371f2c820SChao Yu 
45147f59b277SEric Biggers static inline bool f2fs_need_verity(const struct inode *inode, pgoff_t idx)
45157f59b277SEric Biggers {
45167f59b277SEric Biggers 	return fsverity_active(inode) &&
45177f59b277SEric Biggers 	       idx < DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
45187f59b277SEric Biggers }
45197f59b277SEric Biggers 
452083a3bfdbSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
4521d494500aSChao Yu extern void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate,
4522d494500aSChao Yu 							unsigned int type);
452383a3bfdbSJaegeuk Kim #else
4524d494500aSChao Yu #define f2fs_build_fault_attr(sbi, rate, type)		do { } while (0)
452583a3bfdbSJaegeuk Kim #endif
452683a3bfdbSJaegeuk Kim 
4527af033b2aSChao Yu static inline bool is_journalled_quota(struct f2fs_sb_info *sbi)
4528af033b2aSChao Yu {
4529af033b2aSChao Yu #ifdef CONFIG_QUOTA
45307beb01f7SChao Yu 	if (f2fs_sb_has_quota_ino(sbi))
4531af033b2aSChao Yu 		return true;
4532af033b2aSChao Yu 	if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
4533af033b2aSChao Yu 		F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
4534af033b2aSChao Yu 		F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
4535af033b2aSChao Yu 		return true;
4536af033b2aSChao Yu #endif
4537af033b2aSChao Yu 	return false;
4538af033b2aSChao Yu }
45390af725fcSJaegeuk Kim 
45404f993264SChao Yu static inline bool f2fs_block_unit_discard(struct f2fs_sb_info *sbi)
45414f993264SChao Yu {
45424f993264SChao Yu 	return F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_BLOCK;
45434f993264SChao Yu }
45444f993264SChao Yu 
4545a64239d0SNeilBrown static inline void f2fs_io_schedule_timeout(long timeout)
4546a64239d0SNeilBrown {
4547a64239d0SNeilBrown 	set_current_state(TASK_UNINTERRUPTIBLE);
4548a64239d0SNeilBrown 	io_schedule_timeout(timeout);
4549a64239d0SNeilBrown }
4550a64239d0SNeilBrown 
4551a7b8618aSJaegeuk Kim static inline void f2fs_handle_page_eio(struct f2fs_sb_info *sbi, pgoff_t ofs,
4552a7b8618aSJaegeuk Kim 					enum page_type type)
4553a7b8618aSJaegeuk Kim {
4554a7b8618aSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
4555a7b8618aSJaegeuk Kim 		return;
4556a7b8618aSJaegeuk Kim 
4557a7b8618aSJaegeuk Kim 	if (ofs == sbi->page_eio_ofs[type]) {
4558a7b8618aSJaegeuk Kim 		if (sbi->page_eio_cnt[type]++ == MAX_RETRY_PAGE_EIO)
4559a7b8618aSJaegeuk Kim 			set_ckpt_flags(sbi, CP_ERROR_FLAG);
4560a7b8618aSJaegeuk Kim 	} else {
4561a7b8618aSJaegeuk Kim 		sbi->page_eio_ofs[type] = ofs;
4562a7b8618aSJaegeuk Kim 		sbi->page_eio_cnt[type] = 0;
4563a7b8618aSJaegeuk Kim 	}
4564a7b8618aSJaegeuk Kim }
4565a7b8618aSJaegeuk Kim 
4566ed8ac22bSYangtao Li static inline bool f2fs_is_readonly(struct f2fs_sb_info *sbi)
4567ed8ac22bSYangtao Li {
4568ed8ac22bSYangtao Li 	return f2fs_sb_has_readonly(sbi) || f2fs_readonly(sbi->sb);
4569ed8ac22bSYangtao Li }
4570ed8ac22bSYangtao Li 
457110f966bbSChao Yu #define EFSBADCRC	EBADMSG		/* Bad CRC detected */
457210f966bbSChao Yu #define EFSCORRUPTED	EUCLEAN		/* Filesystem is corrupted */
457310f966bbSChao Yu 
4574e1074d4bSChao Yu #endif /* _LINUX_F2FS_H */
4575