xref: /openbmc/linux/fs/f2fs/f2fs.h (revision 94afd6d6e5253179c9b891d02081cc8355a11768)
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>
2139307a8eSJaegeuk Kim #include <linux/vmalloc.h>
22740432f8SJaegeuk Kim #include <linux/bio.h>
23d0239e1bSJaegeuk Kim #include <linux/blkdev.h>
240abd675eSChao Yu #include <linux/quotaops.h>
25c6a564ffSChristoph Hellwig #include <linux/part_stat.h>
2643b6573bSKeith Mok #include <crypto/hash.h>
2739a53e0cSJaegeuk Kim 
28734f0d24SDave Chinner #include <linux/fscrypt.h>
2995ae251fSEric Biggers #include <linux/fsverity.h>
30734f0d24SDave Chinner 
315d56b671SJaegeuk Kim #ifdef CONFIG_F2FS_CHECK_FS
329850cf4aSJaegeuk Kim #define f2fs_bug_on(sbi, condition)	BUG_ON(condition)
335d56b671SJaegeuk Kim #else
349850cf4aSJaegeuk Kim #define f2fs_bug_on(sbi, condition)					\
359850cf4aSJaegeuk Kim 	do {								\
36db489652SYangtao Li 		if (WARN_ON(condition))					\
37caf0047eSChao Yu 			set_sbi_flag(sbi, SBI_NEED_FSCK);		\
389850cf4aSJaegeuk Kim 	} while (0)
395d56b671SJaegeuk Kim #endif
405d56b671SJaegeuk Kim 
412c63feadSJaegeuk Kim enum {
422c63feadSJaegeuk Kim 	FAULT_KMALLOC,
43628b3d14SChao Yu 	FAULT_KVMALLOC,
44c41f3cc3SJaegeuk Kim 	FAULT_PAGE_ALLOC,
4501eccef7SChao Yu 	FAULT_PAGE_GET,
46cb78942bSJaegeuk Kim 	FAULT_ALLOC_NID,
47cb78942bSJaegeuk Kim 	FAULT_ORPHAN,
48cb78942bSJaegeuk Kim 	FAULT_BLOCK,
49cb78942bSJaegeuk Kim 	FAULT_DIR_DEPTH,
5053aa6bbfSJaegeuk Kim 	FAULT_EVICT_INODE,
5114b44d23SJaegeuk Kim 	FAULT_TRUNCATE,
526f5c2ed0SChao Yu 	FAULT_READ_IO,
530f348028SChao Yu 	FAULT_CHECKPOINT,
54b83dcfe6SChao Yu 	FAULT_DISCARD,
556f5c2ed0SChao Yu 	FAULT_WRITE_IO,
562c63feadSJaegeuk Kim 	FAULT_MAX,
572c63feadSJaegeuk Kim };
582c63feadSJaegeuk Kim 
597fa750a1SArnd Bergmann #ifdef CONFIG_F2FS_FAULT_INJECTION
60d494500aSChao Yu #define F2FS_ALL_FAULT_TYPE		((1 << FAULT_MAX) - 1)
61d494500aSChao Yu 
6208796897SSheng Yong struct f2fs_fault_info {
6308796897SSheng Yong 	atomic_t inject_ops;
6408796897SSheng Yong 	unsigned int inject_rate;
6508796897SSheng Yong 	unsigned int inject_type;
6608796897SSheng Yong };
6708796897SSheng Yong 
6819880e6eSAlexey Dobriyan extern const char *f2fs_fault_name[FAULT_MAX];
6968afcf2dSTomohiro Kusumi #define IS_FAULT_SET(fi, type) ((fi)->inject_type & (1 << (type)))
702c63feadSJaegeuk Kim #endif
712c63feadSJaegeuk Kim 
7239a53e0cSJaegeuk Kim /*
7339a53e0cSJaegeuk Kim  * For mount options
7439a53e0cSJaegeuk Kim  */
7539a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_ROLL_FORWARD	0x00000002
7639a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISCARD		0x00000004
7739a53e0cSJaegeuk Kim #define F2FS_MOUNT_NOHEAP		0x00000008
7839a53e0cSJaegeuk Kim #define F2FS_MOUNT_XATTR_USER		0x00000010
7939a53e0cSJaegeuk Kim #define F2FS_MOUNT_POSIX_ACL		0x00000020
8039a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_EXT_IDENTIFY	0x00000040
81444c580fSJaegeuk Kim #define F2FS_MOUNT_INLINE_XATTR		0x00000080
821001b347SHuajun Li #define F2FS_MOUNT_INLINE_DATA		0x00000100
8334d67debSChao Yu #define F2FS_MOUNT_INLINE_DENTRY	0x00000200
8434d67debSChao Yu #define F2FS_MOUNT_FLUSH_MERGE		0x00000400
8534d67debSChao Yu #define F2FS_MOUNT_NOBARRIER		0x00000800
86d5053a34SJaegeuk Kim #define F2FS_MOUNT_FASTBOOT		0x00001000
8789672159SChao Yu #define F2FS_MOUNT_EXTENT_CACHE		0x00002000
88343f40f0SChao Yu #define F2FS_MOUNT_DATA_FLUSH		0x00008000
8973faec4dSJaegeuk Kim #define F2FS_MOUNT_FAULT_INJECTION	0x00010000
900abd675eSChao Yu #define F2FS_MOUNT_USRQUOTA		0x00080000
910abd675eSChao Yu #define F2FS_MOUNT_GRPQUOTA		0x00100000
925c57132eSChao Yu #define F2FS_MOUNT_PRJQUOTA		0x00200000
934b2414d0SChao Yu #define F2FS_MOUNT_QUOTA		0x00400000
946afc662eSChao Yu #define F2FS_MOUNT_INLINE_XATTR_SIZE	0x00800000
957e65be49SJaegeuk Kim #define F2FS_MOUNT_RESERVE_ROOT		0x01000000
964354994fSDaniel Rosenberg #define F2FS_MOUNT_DISABLE_CHECKPOINT	0x02000000
97a9117ecaSChao Yu #define F2FS_MOUNT_NORECOVERY		0x04000000
98093749e2SChao Yu #define F2FS_MOUNT_ATGC			0x08000000
99261eeb9cSDaeho Jeong #define F2FS_MOUNT_MERGE_CHECKPOINT	0x10000000
1005911d2d1SChao Yu #define	F2FS_MOUNT_GC_MERGE		0x20000000
1016ce19affSChao Yu #define F2FS_MOUNT_COMPRESS_CACHE	0x40000000
10239a53e0cSJaegeuk Kim 
10363189b78SChao Yu #define F2FS_OPTION(sbi)	((sbi)->mount_opt)
10463189b78SChao Yu #define clear_opt(sbi, option)	(F2FS_OPTION(sbi).opt &= ~F2FS_MOUNT_##option)
10563189b78SChao Yu #define set_opt(sbi, option)	(F2FS_OPTION(sbi).opt |= F2FS_MOUNT_##option)
10663189b78SChao Yu #define test_opt(sbi, option)	(F2FS_OPTION(sbi).opt & F2FS_MOUNT_##option)
10739a53e0cSJaegeuk Kim 
10839a53e0cSJaegeuk Kim #define ver_after(a, b)	(typecheck(unsigned long long, a) &&		\
10939a53e0cSJaegeuk Kim 		typecheck(unsigned long long, b) &&			\
11039a53e0cSJaegeuk Kim 		((long long)((a) - (b)) > 0))
11139a53e0cSJaegeuk Kim 
112a9841c4dSJaegeuk Kim typedef u32 block_t;	/*
113a9841c4dSJaegeuk Kim 			 * should not change u32, since it is the on-disk block
114a9841c4dSJaegeuk Kim 			 * address format, __le32.
115a9841c4dSJaegeuk Kim 			 */
11639a53e0cSJaegeuk Kim typedef u32 nid_t;
11739a53e0cSJaegeuk Kim 
1184c8ff709SChao Yu #define COMPRESS_EXT_NUM		16
1194c8ff709SChao Yu 
12039a53e0cSJaegeuk Kim struct f2fs_mount_info {
12139a53e0cSJaegeuk Kim 	unsigned int opt;
12263189b78SChao Yu 	int write_io_size_bits;		/* Write IO size bits */
12363189b78SChao Yu 	block_t root_reserved_blocks;	/* root reserved blocks */
12463189b78SChao Yu 	kuid_t s_resuid;		/* reserved blocks for uid */
12563189b78SChao Yu 	kgid_t s_resgid;		/* reserved blocks for gid */
12663189b78SChao Yu 	int active_logs;		/* # of active logs */
12763189b78SChao Yu 	int inline_xattr_size;		/* inline xattr size */
12863189b78SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
12963189b78SChao Yu 	struct f2fs_fault_info fault_info;	/* For fault injection */
13063189b78SChao Yu #endif
13163189b78SChao Yu #ifdef CONFIG_QUOTA
13263189b78SChao Yu 	/* Names of quota files with journalled quota */
13363189b78SChao Yu 	char *s_qf_names[MAXQUOTAS];
13463189b78SChao Yu 	int s_jquota_fmt;			/* Format of quota to use */
13563189b78SChao Yu #endif
13663189b78SChao Yu 	/* For which write hints are passed down to block layer */
13763189b78SChao Yu 	int whint_mode;
13863189b78SChao Yu 	int alloc_mode;			/* segment allocation policy */
13963189b78SChao Yu 	int fsync_mode;			/* fsync policy */
140b0332a0fSChao Yu 	int fs_mode;			/* fs mode: LFS or ADAPTIVE */
141bbbc34fdSChao Yu 	int bggc_mode;			/* bggc mode: off, on or sync */
1424f993264SChao Yu 	int discard_unit;		/*
1434f993264SChao Yu 					 * discard command's offset/size should
1444f993264SChao Yu 					 * be aligned to this unit: block,
1454f993264SChao Yu 					 * segment or section
1464f993264SChao Yu 					 */
147ac4acb1fSEric Biggers 	struct fscrypt_dummy_policy dummy_enc_policy; /* test dummy encryption */
1481ae18f71SJaegeuk Kim 	block_t unusable_cap_perc;	/* percentage for cap */
1494d3aed70SDaniel Rosenberg 	block_t unusable_cap;		/* Amount of space allowed to be
1504d3aed70SDaniel Rosenberg 					 * unusable when disabling checkpoint
1514d3aed70SDaniel Rosenberg 					 */
1524c8ff709SChao Yu 
1534c8ff709SChao Yu 	/* For compression */
1544c8ff709SChao Yu 	unsigned char compress_algorithm;	/* algorithm type */
155b28f047bSChao Yu 	unsigned char compress_log_size;	/* cluster log size */
1563fde13f8SChao Yu 	unsigned char compress_level;		/* compress level */
157b28f047bSChao Yu 	bool compress_chksum;			/* compressed data chksum */
1584c8ff709SChao Yu 	unsigned char compress_ext_cnt;		/* extension count */
159151b1982SFengnan Chang 	unsigned char nocompress_ext_cnt;		/* nocompress extension count */
160602a16d5SDaeho Jeong 	int compress_mode;			/* compression mode */
1614c8ff709SChao Yu 	unsigned char extensions[COMPRESS_EXT_NUM][F2FS_EXTENSION_LEN];	/* extensions */
162151b1982SFengnan Chang 	unsigned char noextensions[COMPRESS_EXT_NUM][F2FS_EXTENSION_LEN]; /* extensions */
16339a53e0cSJaegeuk Kim };
16439a53e0cSJaegeuk Kim 
165cde4de12SJaegeuk Kim #define F2FS_FEATURE_ENCRYPT		0x0001
1660bfd7a09SDamien Le Moal #define F2FS_FEATURE_BLKZONED		0x0002
167e65ef207SJaegeuk Kim #define F2FS_FEATURE_ATOMIC_WRITE	0x0004
1687a2af766SChao Yu #define F2FS_FEATURE_EXTRA_ATTR		0x0008
1695c57132eSChao Yu #define F2FS_FEATURE_PRJQUOTA		0x0010
170704956ecSChao Yu #define F2FS_FEATURE_INODE_CHKSUM	0x0020
1716afc662eSChao Yu #define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR	0x0040
172234a9689SJaegeuk Kim #define F2FS_FEATURE_QUOTA_INO		0x0080
1731c1d35dfSChao Yu #define F2FS_FEATURE_INODE_CRTIME	0x0100
174b7c409deSSheng Yong #define F2FS_FEATURE_LOST_FOUND		0x0200
17595ae251fSEric Biggers #define F2FS_FEATURE_VERITY		0x0400
176d440c52dSJunling Zheng #define F2FS_FEATURE_SB_CHKSUM		0x0800
1775aba5430SDaniel Rosenberg #define F2FS_FEATURE_CASEFOLD		0x1000
1784c8ff709SChao Yu #define F2FS_FEATURE_COMPRESSION	0x2000
179a7d9fe3cSJaegeuk Kim #define F2FS_FEATURE_RO			0x4000
180cde4de12SJaegeuk Kim 
1817beb01f7SChao Yu #define __F2FS_HAS_FEATURE(raw_super, mask)				\
1827beb01f7SChao Yu 	((raw_super->feature & cpu_to_le32(mask)) != 0)
1837beb01f7SChao Yu #define F2FS_HAS_FEATURE(sbi, mask)	__F2FS_HAS_FEATURE(sbi->raw_super, mask)
1847beb01f7SChao Yu #define F2FS_SET_FEATURE(sbi, mask)					\
1857beb01f7SChao Yu 	(sbi->raw_super->feature |= cpu_to_le32(mask))
1867beb01f7SChao Yu #define F2FS_CLEAR_FEATURE(sbi, mask)					\
1877beb01f7SChao Yu 	(sbi->raw_super->feature &= ~cpu_to_le32(mask))
18876f105a2SJaegeuk Kim 
18939a53e0cSJaegeuk Kim /*
1907c2e5963SJaegeuk Kim  * Default values for user and/or group using reserved blocks
1917c2e5963SJaegeuk Kim  */
1927c2e5963SJaegeuk Kim #define	F2FS_DEF_RESUID		0
1937c2e5963SJaegeuk Kim #define	F2FS_DEF_RESGID		0
1947c2e5963SJaegeuk Kim 
1957c2e5963SJaegeuk Kim /*
19639a53e0cSJaegeuk Kim  * For checkpoint manager
19739a53e0cSJaegeuk Kim  */
19839a53e0cSJaegeuk Kim enum {
19939a53e0cSJaegeuk Kim 	NAT_BITMAP,
20039a53e0cSJaegeuk Kim 	SIT_BITMAP
20139a53e0cSJaegeuk Kim };
20239a53e0cSJaegeuk Kim 
203c473f1a9SChao Yu #define	CP_UMOUNT	0x00000001
204c473f1a9SChao Yu #define	CP_FASTBOOT	0x00000002
205c473f1a9SChao Yu #define	CP_SYNC		0x00000004
206c473f1a9SChao Yu #define	CP_RECOVERY	0x00000008
207c473f1a9SChao Yu #define	CP_DISCARD	0x00000010
2081f43e2adSChao Yu #define CP_TRIMMED	0x00000020
2094354994fSDaniel Rosenberg #define CP_PAUSE	0x00000040
210b4b10061SJaegeuk Kim #define CP_RESIZE 	0x00000080
21175ab4cb8SJaegeuk Kim 
2124ddb1a4dSJaegeuk Kim #define MAX_DISCARD_BLOCKS(sbi)		BLKS_PER_SEC(sbi)
213ecc9aa00SChao Yu #define DEF_MAX_DISCARD_REQUEST		8	/* issue 8 discards per round */
214969d1b18SChao Yu #define DEF_MIN_DISCARD_ISSUE_TIME	50	/* 50 ms, if exists */
215f9d1dcedSYunlei He #define DEF_MID_DISCARD_ISSUE_TIME	500	/* 500 ms, if device busy */
216969d1b18SChao Yu #define DEF_MAX_DISCARD_ISSUE_TIME	60000	/* 60 s, if no candidates */
2178bb4f253SJaegeuk Kim #define DEF_DISCARD_URGENT_UTIL		80	/* do more discard over 80% */
21860b99b48SJaegeuk Kim #define DEF_CP_INTERVAL			60	/* 60 secs */
219dcf25fe8SChao Yu #define DEF_IDLE_INTERVAL		5	/* 5 secs */
2204354994fSDaniel Rosenberg #define DEF_DISABLE_INTERVAL		5	/* 5 secs */
221db610a64SJaegeuk Kim #define DEF_DISABLE_QUICK_INTERVAL	1	/* 1 secs */
22203f2c02dSJaegeuk Kim #define DEF_UMOUNT_DISCARD_TIMEOUT	5	/* 5 secs */
223bba681cbSJaegeuk Kim 
22475ab4cb8SJaegeuk Kim struct cp_control {
22575ab4cb8SJaegeuk Kim 	int reason;
2264b2fecc8SJaegeuk Kim 	__u64 trim_start;
2274b2fecc8SJaegeuk Kim 	__u64 trim_end;
2284b2fecc8SJaegeuk Kim 	__u64 trim_minlen;
22975ab4cb8SJaegeuk Kim };
23075ab4cb8SJaegeuk Kim 
231662befdaSChao Yu /*
232e1da7872SChao Yu  * indicate meta/data type
233662befdaSChao Yu  */
234662befdaSChao Yu enum {
235662befdaSChao Yu 	META_CP,
236662befdaSChao Yu 	META_NAT,
23781c1a0f1SChao Yu 	META_SIT,
2384c521f49SJaegeuk Kim 	META_SSA,
239b63e7be5SChao Yu 	META_MAX,
2404c521f49SJaegeuk Kim 	META_POR,
24193770ab7SChao Yu 	DATA_GENERIC,		/* check range only */
24293770ab7SChao Yu 	DATA_GENERIC_ENHANCE,	/* strong check on range and segment bitmap */
24393770ab7SChao Yu 	DATA_GENERIC_ENHANCE_READ,	/*
24493770ab7SChao Yu 					 * strong check on range and segment
24593770ab7SChao Yu 					 * bitmap but no warning due to race
24693770ab7SChao Yu 					 * condition of read on truncated area
24793770ab7SChao Yu 					 * by extent_cache
24893770ab7SChao Yu 					 */
249e1da7872SChao Yu 	META_GENERIC,
250662befdaSChao Yu };
251662befdaSChao Yu 
2526451e041SJaegeuk Kim /* for the list of ino */
2536451e041SJaegeuk Kim enum {
2546451e041SJaegeuk Kim 	ORPHAN_INO,		/* for orphan ino list */
255fff04f90SJaegeuk Kim 	APPEND_INO,		/* for append ino list */
256fff04f90SJaegeuk Kim 	UPDATE_INO,		/* for update ino list */
2570a007b97SJaegeuk Kim 	TRANS_DIR_INO,		/* for trasactions dir ino list */
25839d787beSChao Yu 	FLUSH_INO,		/* for multiple device flushing */
2596451e041SJaegeuk Kim 	MAX_INO_ENTRY,		/* max. list */
2606451e041SJaegeuk Kim };
2616451e041SJaegeuk Kim 
2626451e041SJaegeuk Kim struct ino_entry {
26339a53e0cSJaegeuk Kim 	struct list_head list;		/* list head */
26439a53e0cSJaegeuk Kim 	nid_t ino;			/* inode number */
26539d787beSChao Yu 	unsigned int dirty_device;	/* dirty device bitmap */
26639a53e0cSJaegeuk Kim };
26739a53e0cSJaegeuk Kim 
2682710fd7eSChao Yu /* for the list of inodes to be GCed */
26906292073SChao Yu struct inode_entry {
27039a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
27139a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
27239a53e0cSJaegeuk Kim };
27339a53e0cSJaegeuk Kim 
27450fa53ecSChao Yu struct fsync_node_entry {
27550fa53ecSChao Yu 	struct list_head list;	/* list head */
27650fa53ecSChao Yu 	struct page *page;	/* warm node page pointer */
27750fa53ecSChao Yu 	unsigned int seq_id;	/* sequence id */
27850fa53ecSChao Yu };
27950fa53ecSChao Yu 
280261eeb9cSDaeho Jeong struct ckpt_req {
281261eeb9cSDaeho Jeong 	struct completion wait;		/* completion for checkpoint done */
282261eeb9cSDaeho Jeong 	struct llist_node llnode;	/* llist_node to be linked in wait queue */
283261eeb9cSDaeho Jeong 	int ret;			/* return code of checkpoint */
284261eeb9cSDaeho Jeong 	ktime_t queue_time;		/* request queued time */
285261eeb9cSDaeho Jeong };
286261eeb9cSDaeho Jeong 
287261eeb9cSDaeho Jeong struct ckpt_req_control {
288261eeb9cSDaeho Jeong 	struct task_struct *f2fs_issue_ckpt;	/* checkpoint task */
289e6592066SDaeho Jeong 	int ckpt_thread_ioprio;			/* checkpoint merge thread ioprio */
290261eeb9cSDaeho Jeong 	wait_queue_head_t ckpt_wait_queue;	/* waiting queue for wake-up */
291261eeb9cSDaeho Jeong 	atomic_t issued_ckpt;		/* # of actually issued ckpts */
292261eeb9cSDaeho Jeong 	atomic_t total_ckpt;		/* # of total ckpts */
293261eeb9cSDaeho Jeong 	atomic_t queued_ckpt;		/* # of queued ckpts */
294261eeb9cSDaeho Jeong 	struct llist_head issue_list;	/* list for command issue */
295261eeb9cSDaeho Jeong 	spinlock_t stat_lock;		/* lock for below checkpoint time stats */
296261eeb9cSDaeho Jeong 	unsigned int cur_time;		/* cur wait time in msec for currently issued checkpoint */
297261eeb9cSDaeho Jeong 	unsigned int peak_time;		/* peak wait time in msec until now */
298261eeb9cSDaeho Jeong };
299261eeb9cSDaeho Jeong 
300a7eeb823SChao Yu /* for the bitmap indicate blocks to be discarded */
3017fd9e544SJaegeuk Kim struct discard_entry {
3027fd9e544SJaegeuk Kim 	struct list_head list;	/* list head */
303a7eeb823SChao Yu 	block_t start_blkaddr;	/* start blockaddr of current segment */
304a7eeb823SChao Yu 	unsigned char discard_map[SIT_VBLOCK_MAP_SIZE];	/* segment discard bitmap */
3057fd9e544SJaegeuk Kim };
3067fd9e544SJaegeuk Kim 
307969d1b18SChao Yu /* default discard granularity of inner discard thread, unit: block count */
308969d1b18SChao Yu #define DEFAULT_DISCARD_GRANULARITY		16
309969d1b18SChao Yu 
310ba48a33eSChao Yu /* max discard pend list number */
311ba48a33eSChao Yu #define MAX_PLIST_NUM		512
312ba48a33eSChao Yu #define plist_idx(blk_num)	((blk_num) >= MAX_PLIST_NUM ?		\
3132f84babfSSheng Yong 					(MAX_PLIST_NUM - 1) : ((blk_num) - 1))
314ba48a33eSChao Yu 
31515469963SJaegeuk Kim enum {
31635ec7d57SChao Yu 	D_PREP,			/* initial */
31735ec7d57SChao Yu 	D_PARTIAL,		/* partially submitted */
31835ec7d57SChao Yu 	D_SUBMIT,		/* all submitted */
31935ec7d57SChao Yu 	D_DONE,			/* finished */
32015469963SJaegeuk Kim };
32115469963SJaegeuk Kim 
322004b6862SChao Yu struct discard_info {
323b01a9201SJaegeuk Kim 	block_t lstart;			/* logical start address */
324b01a9201SJaegeuk Kim 	block_t len;			/* length */
325004b6862SChao Yu 	block_t start;			/* actual start address in dev */
326004b6862SChao Yu };
327004b6862SChao Yu 
328b01a9201SJaegeuk Kim struct discard_cmd {
329004b6862SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
330004b6862SChao Yu 	union {
331004b6862SChao Yu 		struct {
332004b6862SChao Yu 			block_t lstart;	/* logical start address */
333004b6862SChao Yu 			block_t len;	/* length */
334004b6862SChao Yu 			block_t start;	/* actual start address in dev */
335004b6862SChao Yu 		};
336004b6862SChao Yu 		struct discard_info di;	/* discard info */
337004b6862SChao Yu 
338004b6862SChao Yu 	};
339b01a9201SJaegeuk Kim 	struct list_head list;		/* command list */
340b01a9201SJaegeuk Kim 	struct completion wait;		/* compleation */
341c81abe34SJaegeuk Kim 	struct block_device *bdev;	/* bdev */
342ec9895adSChao Yu 	unsigned short ref;		/* reference count */
3439a744b92SChao Yu 	unsigned char state;		/* state */
34472691af6SJaegeuk Kim 	unsigned char queued;		/* queued discard */
345c81abe34SJaegeuk Kim 	int error;			/* bio error */
34635ec7d57SChao Yu 	spinlock_t lock;		/* for state/bio_ref updating */
34735ec7d57SChao Yu 	unsigned short bio_ref;		/* bio reference count */
348275b66b0SChao Yu };
349275b66b0SChao Yu 
35078997b56SChao Yu enum {
35178997b56SChao Yu 	DPOLICY_BG,
35278997b56SChao Yu 	DPOLICY_FORCE,
35378997b56SChao Yu 	DPOLICY_FSTRIM,
35478997b56SChao Yu 	DPOLICY_UMOUNT,
35578997b56SChao Yu 	MAX_DPOLICY,
35678997b56SChao Yu };
35778997b56SChao Yu 
358ecc9aa00SChao Yu struct discard_policy {
35978997b56SChao Yu 	int type;			/* type of discard */
360ecc9aa00SChao Yu 	unsigned int min_interval;	/* used for candidates exist */
361f9d1dcedSYunlei He 	unsigned int mid_interval;	/* used for device busy */
362ecc9aa00SChao Yu 	unsigned int max_interval;	/* used for candidates not exist */
363ecc9aa00SChao Yu 	unsigned int max_requests;	/* # of discards issued per round */
364ecc9aa00SChao Yu 	unsigned int io_aware_gran;	/* minimum granularity discard not be aware of I/O */
365ecc9aa00SChao Yu 	bool io_aware;			/* issue discard in idle time */
366ecc9aa00SChao Yu 	bool sync;			/* submit discard with REQ_SYNC flag */
36720ee4382SChao Yu 	bool ordered;			/* issue discard by lba order */
3686ce48b0cSChao Yu 	bool timeout;			/* discard timeout for put_super */
36978997b56SChao Yu 	unsigned int granularity;	/* discard granularity */
370ecc9aa00SChao Yu };
371ecc9aa00SChao Yu 
3720b54fb84SJaegeuk Kim struct discard_cmd_control {
37315469963SJaegeuk Kim 	struct task_struct *f2fs_issue_discard;	/* discard thread */
37446f84c2cSChao Yu 	struct list_head entry_list;		/* 4KB discard entry list */
375ba48a33eSChao Yu 	struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
37646f84c2cSChao Yu 	struct list_head wait_list;		/* store on-flushing entries */
3778412663dSChao Yu 	struct list_head fstrim_list;		/* in-flight discard from fstrim */
37815469963SJaegeuk Kim 	wait_queue_head_t discard_wait_queue;	/* waiting queue for wake-up */
379969d1b18SChao Yu 	unsigned int discard_wake;		/* to wake up discard thread */
38015469963SJaegeuk Kim 	struct mutex cmd_lock;
381d618ebafSChao Yu 	unsigned int nr_discards;		/* # of discards in the list */
382d618ebafSChao Yu 	unsigned int max_discards;		/* max. discards to be issued */
383969d1b18SChao Yu 	unsigned int discard_granularity;	/* discard granularity */
384d84d1cbdSChao Yu 	unsigned int undiscard_blks;		/* # of undiscard blocks */
38520ee4382SChao Yu 	unsigned int next_pos;			/* next discard position */
3868b8dd65fSChao Yu 	atomic_t issued_discard;		/* # of issued discard */
38772691af6SJaegeuk Kim 	atomic_t queued_discard;		/* # of queued discard */
3885f32366aSChao Yu 	atomic_t discard_cmd_cnt;		/* # of cached cmd count */
3894dada3fdSChao Yu 	struct rb_root_cached root;		/* root of discard rb-tree */
39067fce70bSChao Yu 	bool rbtree_check;			/* config for consistence check */
39139a53e0cSJaegeuk Kim };
39239a53e0cSJaegeuk Kim 
39339a53e0cSJaegeuk Kim /* for the list of fsync inodes, used only during recovery */
39439a53e0cSJaegeuk Kim struct fsync_inode_entry {
39539a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
39639a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
397c52e1b10SJaegeuk Kim 	block_t blkaddr;	/* block address locating the last fsync */
398c52e1b10SJaegeuk Kim 	block_t last_dentry;	/* block address locating the last dentry */
39939a53e0cSJaegeuk Kim };
40039a53e0cSJaegeuk Kim 
40168afcf2dSTomohiro Kusumi #define nats_in_cursum(jnl)		(le16_to_cpu((jnl)->n_nats))
40268afcf2dSTomohiro Kusumi #define sits_in_cursum(jnl)		(le16_to_cpu((jnl)->n_sits))
40339a53e0cSJaegeuk Kim 
40468afcf2dSTomohiro Kusumi #define nat_in_journal(jnl, i)		((jnl)->nat_j.entries[i].ne)
40568afcf2dSTomohiro Kusumi #define nid_in_journal(jnl, i)		((jnl)->nat_j.entries[i].nid)
40668afcf2dSTomohiro Kusumi #define sit_in_journal(jnl, i)		((jnl)->sit_j.entries[i].se)
40768afcf2dSTomohiro Kusumi #define segno_in_journal(jnl, i)	((jnl)->sit_j.entries[i].segno)
40839a53e0cSJaegeuk Kim 
409dfc08a12SChao Yu #define MAX_NAT_JENTRIES(jnl)	(NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
410dfc08a12SChao Yu #define MAX_SIT_JENTRIES(jnl)	(SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
411309cc2b6SJaegeuk Kim 
412dfc08a12SChao Yu static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
41339a53e0cSJaegeuk Kim {
414dfc08a12SChao Yu 	int before = nats_in_cursum(journal);
415cac5a3d8SDongOh Shin 
416dfc08a12SChao Yu 	journal->n_nats = cpu_to_le16(before + i);
41739a53e0cSJaegeuk Kim 	return before;
41839a53e0cSJaegeuk Kim }
41939a53e0cSJaegeuk Kim 
420dfc08a12SChao Yu static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
42139a53e0cSJaegeuk Kim {
422dfc08a12SChao Yu 	int before = sits_in_cursum(journal);
423cac5a3d8SDongOh Shin 
424dfc08a12SChao Yu 	journal->n_sits = cpu_to_le16(before + i);
42539a53e0cSJaegeuk Kim 	return before;
42639a53e0cSJaegeuk Kim }
42739a53e0cSJaegeuk Kim 
428dfc08a12SChao Yu static inline bool __has_cursum_space(struct f2fs_journal *journal,
429dfc08a12SChao Yu 							int size, int type)
430184a5cd2SChao Yu {
431184a5cd2SChao Yu 	if (type == NAT_JOURNAL)
432dfc08a12SChao Yu 		return size <= MAX_NAT_JENTRIES(journal);
433dfc08a12SChao Yu 	return size <= MAX_SIT_JENTRIES(journal);
434184a5cd2SChao Yu }
435184a5cd2SChao Yu 
436f2470371SChao Yu /* for inline stuff */
437f2470371SChao Yu #define DEF_INLINE_RESERVED_SIZE	1
4387a2af766SChao Yu static inline int get_extra_isize(struct inode *inode);
4396afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode);
4407a2af766SChao Yu #define MAX_INLINE_DATA(inode)	(sizeof(__le32) *			\
4417a2af766SChao Yu 				(CUR_ADDRS_PER_INODE(inode) -		\
442b323fd28SChao Yu 				get_inline_xattr_addrs(inode) -	\
4436afc662eSChao Yu 				DEF_INLINE_RESERVED_SIZE))
444f2470371SChao Yu 
445f2470371SChao Yu /* for inline dir */
446f2470371SChao Yu #define NR_INLINE_DENTRY(inode)	(MAX_INLINE_DATA(inode) * BITS_PER_BYTE / \
447f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
448f2470371SChao Yu 				BITS_PER_BYTE + 1))
449f91108b8SGeert Uytterhoeven #define INLINE_DENTRY_BITMAP_SIZE(inode) \
450f91108b8SGeert Uytterhoeven 	DIV_ROUND_UP(NR_INLINE_DENTRY(inode), BITS_PER_BYTE)
451f2470371SChao Yu #define INLINE_RESERVED_SIZE(inode)	(MAX_INLINE_DATA(inode) - \
452f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
453f2470371SChao Yu 				NR_INLINE_DENTRY(inode) + \
454f2470371SChao Yu 				INLINE_DENTRY_BITMAP_SIZE(inode)))
455f2470371SChao Yu 
456e9750824SNamjae Jeon /*
45739a53e0cSJaegeuk Kim  * For INODE and NODE manager
45839a53e0cSJaegeuk Kim  */
4597b3cd7d6SJaegeuk Kim /* for directory operations */
46043c780baSEric Biggers 
46143c780baSEric Biggers struct f2fs_filename {
46243c780baSEric Biggers 	/*
46343c780baSEric Biggers 	 * The filename the user specified.  This is NULL for some
46443c780baSEric Biggers 	 * filesystem-internal operations, e.g. converting an inline directory
46543c780baSEric Biggers 	 * to a non-inline one, or roll-forward recovering an encrypted dentry.
46643c780baSEric Biggers 	 */
46743c780baSEric Biggers 	const struct qstr *usr_fname;
46843c780baSEric Biggers 
46943c780baSEric Biggers 	/*
47043c780baSEric Biggers 	 * The on-disk filename.  For encrypted directories, this is encrypted.
47143c780baSEric Biggers 	 * This may be NULL for lookups in an encrypted dir without the key.
47243c780baSEric Biggers 	 */
47343c780baSEric Biggers 	struct fscrypt_str disk_name;
47443c780baSEric Biggers 
47543c780baSEric Biggers 	/* The dirhash of this filename */
47643c780baSEric Biggers 	f2fs_hash_t hash;
47743c780baSEric Biggers 
47843c780baSEric Biggers #ifdef CONFIG_FS_ENCRYPTION
47943c780baSEric Biggers 	/*
48043c780baSEric Biggers 	 * For lookups in encrypted directories: either the buffer backing
48143c780baSEric Biggers 	 * disk_name, or a buffer that holds the decoded no-key name.
48243c780baSEric Biggers 	 */
48343c780baSEric Biggers 	struct fscrypt_str crypto_buf;
48443c780baSEric Biggers #endif
48543c780baSEric Biggers #ifdef CONFIG_UNICODE
48643c780baSEric Biggers 	/*
48743c780baSEric Biggers 	 * For casefolded directories: the casefolded name, but it's left NULL
4887ad08a58SDaniel Rosenberg 	 * if the original name is not valid Unicode, if the directory is both
4897ad08a58SDaniel Rosenberg 	 * casefolded and encrypted and its encryption key is unavailable, or if
4907ad08a58SDaniel Rosenberg 	 * the filesystem is doing an internal operation where usr_fname is also
4917ad08a58SDaniel Rosenberg 	 * NULL.  In all these cases we fall back to treating the name as an
4927ad08a58SDaniel Rosenberg 	 * opaque byte sequence.
49343c780baSEric Biggers 	 */
49443c780baSEric Biggers 	struct fscrypt_str cf_name;
49543c780baSEric Biggers #endif
49643c780baSEric Biggers };
49743c780baSEric Biggers 
4987b3cd7d6SJaegeuk Kim struct f2fs_dentry_ptr {
499d8c6822aSJaegeuk Kim 	struct inode *inode;
50076a9dd85SChao Yu 	void *bitmap;
5017b3cd7d6SJaegeuk Kim 	struct f2fs_dir_entry *dentry;
5027b3cd7d6SJaegeuk Kim 	__u8 (*filename)[F2FS_SLOT_LEN];
5037b3cd7d6SJaegeuk Kim 	int max;
50476a9dd85SChao Yu 	int nr_bitmap;
5057b3cd7d6SJaegeuk Kim };
5067b3cd7d6SJaegeuk Kim 
50764c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_block(struct inode *inode,
50864c24ecbSTomohiro Kusumi 		struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
5097b3cd7d6SJaegeuk Kim {
510d8c6822aSJaegeuk Kim 	d->inode = inode;
5117b3cd7d6SJaegeuk Kim 	d->max = NR_DENTRY_IN_BLOCK;
51276a9dd85SChao Yu 	d->nr_bitmap = SIZE_OF_DENTRY_BITMAP;
513c79d1520SYunlong Song 	d->bitmap = t->dentry_bitmap;
5147b3cd7d6SJaegeuk Kim 	d->dentry = t->dentry;
5157b3cd7d6SJaegeuk Kim 	d->filename = t->filename;
51664c24ecbSTomohiro Kusumi }
517cac5a3d8SDongOh Shin 
51864c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_inline(struct inode *inode,
519f2470371SChao Yu 					struct f2fs_dentry_ptr *d, void *t)
52064c24ecbSTomohiro Kusumi {
521f2470371SChao Yu 	int entry_cnt = NR_INLINE_DENTRY(inode);
522f2470371SChao Yu 	int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(inode);
523f2470371SChao Yu 	int reserved_size = INLINE_RESERVED_SIZE(inode);
524f2470371SChao Yu 
52564c24ecbSTomohiro Kusumi 	d->inode = inode;
526f2470371SChao Yu 	d->max = entry_cnt;
527f2470371SChao Yu 	d->nr_bitmap = bitmap_size;
528f2470371SChao Yu 	d->bitmap = t;
529f2470371SChao Yu 	d->dentry = t + bitmap_size + reserved_size;
530f2470371SChao Yu 	d->filename = t + bitmap_size + reserved_size +
531f2470371SChao Yu 					SIZE_OF_DIR_ENTRY * entry_cnt;
5327b3cd7d6SJaegeuk Kim }
5337b3cd7d6SJaegeuk Kim 
534dbe6a5ffSJaegeuk Kim /*
535dbe6a5ffSJaegeuk Kim  * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
536dbe6a5ffSJaegeuk Kim  * as its node offset to distinguish from index node blocks.
537dbe6a5ffSJaegeuk Kim  * But some bits are used to mark the node block.
53839a53e0cSJaegeuk Kim  */
539dbe6a5ffSJaegeuk Kim #define XATTR_NODE_OFFSET	((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
540dbe6a5ffSJaegeuk Kim 				>> OFFSET_BIT_SHIFT)
541266e97a8SJaegeuk Kim enum {
542266e97a8SJaegeuk Kim 	ALLOC_NODE,			/* allocate a new node page if needed */
543266e97a8SJaegeuk Kim 	LOOKUP_NODE,			/* look up a node without readahead */
544266e97a8SJaegeuk Kim 	LOOKUP_NODE_RA,			/*
545266e97a8SJaegeuk Kim 					 * look up a node with readahead called
5464f4124d0SChao Yu 					 * by get_data_block.
54739a53e0cSJaegeuk Kim 					 */
548266e97a8SJaegeuk Kim };
549266e97a8SJaegeuk Kim 
55091803392SChao Yu #define DEFAULT_RETRY_IO_COUNT	8	/* maximum retry read IO or flush count */
5517735730dSChao Yu 
5525df7731fSChao Yu /* congestion wait timeout value, default: 20ms */
5535df7731fSChao Yu #define	DEFAULT_IO_TIMEOUT	(msecs_to_jiffies(20))
5545df7731fSChao Yu 
555af033b2aSChao Yu /* maximum retry quota flush count */
556af033b2aSChao Yu #define DEFAULT_RETRY_QUOTA_FLUSH_COUNT		8
557af033b2aSChao Yu 
558a6db67f0SJaegeuk Kim #define F2FS_LINK_MAX	0xffffffff	/* maximum link count per file */
55939a53e0cSJaegeuk Kim 
560817202d9SChao Yu #define MAX_DIR_RA_PAGES	4	/* maximum ra pages of dir */
561817202d9SChao Yu 
56239a53e0cSJaegeuk Kim /* for in-memory extent cache entry */
56313054c54SChao Yu #define F2FS_MIN_EXTENT_LEN	64	/* minimum extent length */
56413054c54SChao Yu 
56513054c54SChao Yu /* number of extent info in extent cache we try to shrink */
56613054c54SChao Yu #define EXTENT_CACHE_SHRINK_NUMBER	128
567c11abd1aSJaegeuk Kim 
56854c2258cSChao Yu struct rb_entry {
56954c2258cSChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
5702e9b2bb2SChao Yu 	union {
5712e9b2bb2SChao Yu 		struct {
57254c2258cSChao Yu 			unsigned int ofs;	/* start offset of the entry */
57354c2258cSChao Yu 			unsigned int len;	/* length of the entry */
57454c2258cSChao Yu 		};
5752e9b2bb2SChao Yu 		unsigned long long key;		/* 64-bits key */
57648046cb5SJaegeuk Kim 	} __packed;
5772e9b2bb2SChao Yu };
57854c2258cSChao Yu 
57939a53e0cSJaegeuk Kim struct extent_info {
58039a53e0cSJaegeuk Kim 	unsigned int fofs;		/* start offset in a file */
581111d2495SMasanari Iida 	unsigned int len;		/* length of the extent */
58254c2258cSChao Yu 	u32 blk;			/* start block address of the extent */
583*94afd6d6SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
584*94afd6d6SChao Yu 	unsigned int c_len;		/* physical extent length of compressed blocks */
585*94afd6d6SChao Yu #endif
58639a53e0cSJaegeuk Kim };
58739a53e0cSJaegeuk Kim 
58813054c54SChao Yu struct extent_node {
589c0362117SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
59013054c54SChao Yu 	struct extent_info ei;		/* extent info */
59154c2258cSChao Yu 	struct list_head list;		/* node in global extent list of sbi */
592201ef5e0SHou Pengyang 	struct extent_tree *et;		/* extent tree pointer */
59313054c54SChao Yu };
59413054c54SChao Yu 
59513054c54SChao Yu struct extent_tree {
59613054c54SChao Yu 	nid_t ino;			/* inode number */
5974dada3fdSChao Yu 	struct rb_root_cached root;	/* root of extent info rb-tree */
59862c8af65SChao Yu 	struct extent_node *cached_en;	/* recently accessed extent node */
5993e72f721SJaegeuk Kim 	struct extent_info largest;	/* largested extent info */
600137d09f0SJaegeuk Kim 	struct list_head list;		/* to be used by sbi->zombie_list */
60113054c54SChao Yu 	rwlock_t lock;			/* protect extent info rb-tree */
60268e35385SChao Yu 	atomic_t node_cnt;		/* # of extent node in rb-tree*/
603b430f726SZhikang Zhang 	bool largest_updated;		/* largest extent updated */
60413054c54SChao Yu };
60513054c54SChao Yu 
60639a53e0cSJaegeuk Kim /*
607003a3e1dSJaegeuk Kim  * This structure is taken from ext4_map_blocks.
608003a3e1dSJaegeuk Kim  *
609003a3e1dSJaegeuk Kim  * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
610003a3e1dSJaegeuk Kim  */
611003a3e1dSJaegeuk Kim #define F2FS_MAP_NEW		(1 << BH_New)
612003a3e1dSJaegeuk Kim #define F2FS_MAP_MAPPED		(1 << BH_Mapped)
6137f63eb77SJaegeuk Kim #define F2FS_MAP_UNWRITTEN	(1 << BH_Unwritten)
6147f63eb77SJaegeuk Kim #define F2FS_MAP_FLAGS		(F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
6157f63eb77SJaegeuk Kim 				F2FS_MAP_UNWRITTEN)
616003a3e1dSJaegeuk Kim 
617003a3e1dSJaegeuk Kim struct f2fs_map_blocks {
618003a3e1dSJaegeuk Kim 	block_t m_pblk;
619003a3e1dSJaegeuk Kim 	block_t m_lblk;
620003a3e1dSJaegeuk Kim 	unsigned int m_len;
621003a3e1dSJaegeuk Kim 	unsigned int m_flags;
622da85985cSChao Yu 	pgoff_t *m_next_pgofs;		/* point next possible non-hole pgofs */
623c4020b2dSChao Yu 	pgoff_t *m_next_extent;		/* point to next possible extent */
624d5097be5SHyunchul Lee 	int m_seg_type;
625f9d6d059SChao Yu 	bool m_may_create;		/* indicate it is from write path */
626003a3e1dSJaegeuk Kim };
627003a3e1dSJaegeuk Kim 
628e2b4e2bcSChao Yu /* for flag in get_data_block */
629f2220c7fSQiuyang Sun enum {
630f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_DEFAULT,
631f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_FIEMAP,
632f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_BMAP,
6330a4daae5SJaegeuk Kim 	F2FS_GET_BLOCK_DIO,
634f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_DIO,
635f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_AIO,
636c4020b2dSChao Yu 	F2FS_GET_BLOCK_PRECACHE,
637f2220c7fSQiuyang Sun };
638e2b4e2bcSChao Yu 
639003a3e1dSJaegeuk Kim /*
64039a53e0cSJaegeuk Kim  * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
64139a53e0cSJaegeuk Kim  */
64239a53e0cSJaegeuk Kim #define FADVISE_COLD_BIT	0x01
643354a3399SJaegeuk Kim #define FADVISE_LOST_PINO_BIT	0x02
644cde4de12SJaegeuk Kim #define FADVISE_ENCRYPT_BIT	0x04
645e7d55452SJaegeuk Kim #define FADVISE_ENC_NAME_BIT	0x08
64626787236SJaegeuk Kim #define FADVISE_KEEP_SIZE_BIT	0x10
647b6a06cbbSChao Yu #define FADVISE_HOT_BIT		0x20
64895ae251fSEric Biggers #define FADVISE_VERITY_BIT	0x40
64939a53e0cSJaegeuk Kim 
650797c1cb5SChao Yu #define FADVISE_MODIFIABLE_BITS	(FADVISE_COLD_BIT | FADVISE_HOT_BIT)
651797c1cb5SChao Yu 
652b5492af7SJaegeuk Kim #define file_is_cold(inode)	is_file(inode, FADVISE_COLD_BIT)
653b5492af7SJaegeuk Kim #define file_set_cold(inode)	set_file(inode, FADVISE_COLD_BIT)
654b5492af7SJaegeuk Kim #define file_clear_cold(inode)	clear_file(inode, FADVISE_COLD_BIT)
6551153db09SChao Yu 
6561153db09SChao Yu #define file_wrong_pino(inode)	is_file(inode, FADVISE_LOST_PINO_BIT)
6571153db09SChao Yu #define file_lost_pino(inode)	set_file(inode, FADVISE_LOST_PINO_BIT)
658b5492af7SJaegeuk Kim #define file_got_pino(inode)	clear_file(inode, FADVISE_LOST_PINO_BIT)
6591153db09SChao Yu 
660cde4de12SJaegeuk Kim #define file_is_encrypt(inode)	is_file(inode, FADVISE_ENCRYPT_BIT)
661cde4de12SJaegeuk Kim #define file_set_encrypt(inode)	set_file(inode, FADVISE_ENCRYPT_BIT)
6621153db09SChao Yu 
663e7d55452SJaegeuk Kim #define file_enc_name(inode)	is_file(inode, FADVISE_ENC_NAME_BIT)
664e7d55452SJaegeuk Kim #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
6651153db09SChao Yu 
66626787236SJaegeuk Kim #define file_keep_isize(inode)	is_file(inode, FADVISE_KEEP_SIZE_BIT)
66726787236SJaegeuk Kim #define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
6681153db09SChao Yu 
669b6a06cbbSChao Yu #define file_is_hot(inode)	is_file(inode, FADVISE_HOT_BIT)
670b6a06cbbSChao Yu #define file_set_hot(inode)	set_file(inode, FADVISE_HOT_BIT)
671b6a06cbbSChao Yu #define file_clear_hot(inode)	clear_file(inode, FADVISE_HOT_BIT)
6721153db09SChao Yu 
67395ae251fSEric Biggers #define file_is_verity(inode)	is_file(inode, FADVISE_VERITY_BIT)
67495ae251fSEric Biggers #define file_set_verity(inode)	set_file(inode, FADVISE_VERITY_BIT)
675cde4de12SJaegeuk Kim 
676ab9fa662SJaegeuk Kim #define DEF_DIR_LEVEL		0
677ab9fa662SJaegeuk Kim 
6782ef79ecbSChao Yu enum {
6792ef79ecbSChao Yu 	GC_FAILURE_PIN,
6802ef79ecbSChao Yu 	GC_FAILURE_ATOMIC,
6812ef79ecbSChao Yu 	MAX_GC_FAILURE
6822ef79ecbSChao Yu };
6832ef79ecbSChao Yu 
6847653b9d8SChao Yu /* used for f2fs_inode_info->flags */
6857653b9d8SChao Yu enum {
6867653b9d8SChao Yu 	FI_NEW_INODE,		/* indicate newly allocated inode */
6877653b9d8SChao Yu 	FI_DIRTY_INODE,		/* indicate inode is dirty or not */
6887653b9d8SChao Yu 	FI_AUTO_RECOVER,	/* indicate inode is recoverable */
6897653b9d8SChao Yu 	FI_DIRTY_DIR,		/* indicate directory has dirty pages */
6907653b9d8SChao Yu 	FI_INC_LINK,		/* need to increment i_nlink */
6917653b9d8SChao Yu 	FI_ACL_MODE,		/* indicate acl mode */
6927653b9d8SChao Yu 	FI_NO_ALLOC,		/* should not allocate any blocks */
6937653b9d8SChao Yu 	FI_FREE_NID,		/* free allocated nide */
6947653b9d8SChao Yu 	FI_NO_EXTENT,		/* not to use the extent cache */
6957653b9d8SChao Yu 	FI_INLINE_XATTR,	/* used for inline xattr */
6967653b9d8SChao Yu 	FI_INLINE_DATA,		/* used for inline data*/
6977653b9d8SChao Yu 	FI_INLINE_DENTRY,	/* used for inline dentry */
6987653b9d8SChao Yu 	FI_APPEND_WRITE,	/* inode has appended data */
6997653b9d8SChao Yu 	FI_UPDATE_WRITE,	/* inode has in-place-update data */
7007653b9d8SChao Yu 	FI_NEED_IPU,		/* used for ipu per file */
7017653b9d8SChao Yu 	FI_ATOMIC_FILE,		/* indicate atomic file */
7027653b9d8SChao Yu 	FI_ATOMIC_COMMIT,	/* indicate the state of atomical committing */
7037653b9d8SChao Yu 	FI_VOLATILE_FILE,	/* indicate volatile file */
7047653b9d8SChao Yu 	FI_FIRST_BLOCK_WRITTEN,	/* indicate #0 data block was written */
7057653b9d8SChao Yu 	FI_DROP_CACHE,		/* drop dirty page cache */
7067653b9d8SChao Yu 	FI_DATA_EXIST,		/* indicate data exists */
7077653b9d8SChao Yu 	FI_INLINE_DOTS,		/* indicate inline dot dentries */
7087653b9d8SChao Yu 	FI_DO_DEFRAG,		/* indicate defragment is running */
7097653b9d8SChao Yu 	FI_DIRTY_FILE,		/* indicate regular/symlink has dirty pages */
7107653b9d8SChao Yu 	FI_NO_PREALLOC,		/* indicate skipped preallocated blocks */
7117653b9d8SChao Yu 	FI_HOT_DATA,		/* indicate file is hot */
7127653b9d8SChao Yu 	FI_EXTRA_ATTR,		/* indicate file has extra attribute */
7137653b9d8SChao Yu 	FI_PROJ_INHERIT,	/* indicate file inherits projectid */
7147653b9d8SChao Yu 	FI_PIN_FILE,		/* indicate file should not be gced */
7157653b9d8SChao Yu 	FI_ATOMIC_REVOKE_REQUEST, /* request to drop atomic data */
7167653b9d8SChao Yu 	FI_VERITY_IN_PROGRESS,	/* building fs-verity Merkle tree */
7177653b9d8SChao Yu 	FI_COMPRESSED_FILE,	/* indicate file's data can be compressed */
718b28f047bSChao Yu 	FI_COMPRESS_CORRUPT,	/* indicate compressed cluster is corrupted */
7197653b9d8SChao Yu 	FI_MMAP_FILE,		/* indicate file was mmapped */
720602a16d5SDaeho Jeong 	FI_ENABLE_COMPRESS,	/* enable compression in "user" compression mode */
721c6140415SJaegeuk Kim 	FI_COMPRESS_RELEASED,	/* compressed blocks were released */
722859fca6bSChao Yu 	FI_ALIGNED_WRITE,	/* enable aligned write */
7237653b9d8SChao Yu 	FI_MAX,			/* max flag, never be used */
7247653b9d8SChao Yu };
7257653b9d8SChao Yu 
72639a53e0cSJaegeuk Kim struct f2fs_inode_info {
72739a53e0cSJaegeuk Kim 	struct inode vfs_inode;		/* serve a vfs inode */
72839a53e0cSJaegeuk Kim 	unsigned long i_flags;		/* keep an inode flags for ioctl */
72939a53e0cSJaegeuk Kim 	unsigned char i_advise;		/* use to give file attribute hints */
73038431545SJaegeuk Kim 	unsigned char i_dir_level;	/* use for dentry level for large dir */
7311ad71a27SJaegeuk Kim 	unsigned int i_current_depth;	/* only for directory depth */
7322ef79ecbSChao Yu 	/* for gc failure statistic */
7332ef79ecbSChao Yu 	unsigned int i_gc_failures[MAX_GC_FAILURE];
7346666e6aaSJaegeuk Kim 	unsigned int i_pino;		/* parent inode number */
73539a53e0cSJaegeuk Kim 	umode_t i_acl_mode;		/* keep file acl mode temporarily */
73639a53e0cSJaegeuk Kim 
73739a53e0cSJaegeuk Kim 	/* Use below internally in f2fs*/
7387653b9d8SChao Yu 	unsigned long flags[BITS_TO_LONGS(FI_MAX)];	/* use to pass per-file flags */
739d928bfbfSJaegeuk Kim 	struct rw_semaphore i_sem;	/* protect fi info */
740204706c7SJaegeuk Kim 	atomic_t dirty_pages;		/* # of dirty pages */
74139a53e0cSJaegeuk Kim 	f2fs_hash_t chash;		/* hash value of given file name */
74239a53e0cSJaegeuk Kim 	unsigned int clevel;		/* maximum level of given file name */
74388c5c13aSJaegeuk Kim 	struct task_struct *task;	/* lookup and create consistency */
744b0af6d49SChao Yu 	struct task_struct *cp_task;	/* separate cp/wb IO stats*/
74539a53e0cSJaegeuk Kim 	nid_t i_xattr_nid;		/* node id that contains xattrs */
74626de9b11SJaegeuk Kim 	loff_t	last_disk_size;		/* lastly written file size */
747c10c9820SChao Yu 	spinlock_t i_size_lock;		/* protect last_disk_size */
74888b88a66SJaegeuk Kim 
7490abd675eSChao Yu #ifdef CONFIG_QUOTA
7500abd675eSChao Yu 	struct dquot *i_dquot[MAXQUOTAS];
7510abd675eSChao Yu 
7520abd675eSChao Yu 	/* quota space reservation, managed internally by quota code */
7530abd675eSChao Yu 	qsize_t i_reserved_quota;
7540abd675eSChao Yu #endif
7550f18b462SJaegeuk Kim 	struct list_head dirty_list;	/* dirty list for dirs and files */
7560f18b462SJaegeuk Kim 	struct list_head gdirty_list;	/* linked in global dirty list */
75757864ae5SJaegeuk Kim 	struct list_head inmem_ilist;	/* list for inmem inodes */
75888b88a66SJaegeuk Kim 	struct list_head inmem_pages;	/* inmemory pages managed by f2fs */
7597a10f017SJaegeuk Kim 	struct task_struct *inmem_task;	/* store inmemory task */
76088b88a66SJaegeuk Kim 	struct mutex inmem_lock;	/* lock for inmemory pages */
7613e72f721SJaegeuk Kim 	struct extent_tree *extent_tree;	/* cached extent_tree entry */
762b2532c69SChao Yu 
763b2532c69SChao Yu 	/* avoid racing between foreground op and gc */
764b2532c69SChao Yu 	struct rw_semaphore i_gc_rwsem[2];
7655a3a2d83SQiuyang Sun 	struct rw_semaphore i_mmap_sem;
76627161f13SYunlei He 	struct rw_semaphore i_xattr_sem; /* avoid racing between reading and changing EAs */
767f2470371SChao Yu 
7687a2af766SChao Yu 	int i_extra_isize;		/* size of extra space located in i_addr */
7695c57132eSChao Yu 	kprojid_t i_projid;		/* id for project quota */
7706afc662eSChao Yu 	int i_inline_xattr_size;	/* inline xattr size */
77124b81dfcSArnd Bergmann 	struct timespec64 i_crtime;	/* inode creation time */
77224b81dfcSArnd Bergmann 	struct timespec64 i_disk_time[4];/* inode disk times */
7734c8ff709SChao Yu 
7744c8ff709SChao Yu 	/* for file compress */
775c2759ebaSDaeho Jeong 	atomic_t i_compr_blocks;		/* # of compressed blocks */
7764c8ff709SChao Yu 	unsigned char i_compress_algorithm;	/* algorithm type */
7774c8ff709SChao Yu 	unsigned char i_log_cluster_size;	/* log of cluster size */
7783fde13f8SChao Yu 	unsigned char i_compress_level;		/* compress level (lz4hc,zstd) */
779b28f047bSChao Yu 	unsigned short i_compress_flag;		/* compress flag */
7804c8ff709SChao Yu 	unsigned int i_cluster_size;		/* cluster size */
78139a53e0cSJaegeuk Kim };
78239a53e0cSJaegeuk Kim 
78339a53e0cSJaegeuk Kim static inline void get_extent_info(struct extent_info *ext,
784bd933d4fSChao Yu 					struct f2fs_extent *i_ext)
78539a53e0cSJaegeuk Kim {
786bd933d4fSChao Yu 	ext->fofs = le32_to_cpu(i_ext->fofs);
787bd933d4fSChao Yu 	ext->blk = le32_to_cpu(i_ext->blk);
788bd933d4fSChao Yu 	ext->len = le32_to_cpu(i_ext->len);
78939a53e0cSJaegeuk Kim }
79039a53e0cSJaegeuk Kim 
79139a53e0cSJaegeuk Kim static inline void set_raw_extent(struct extent_info *ext,
79239a53e0cSJaegeuk Kim 					struct f2fs_extent *i_ext)
79339a53e0cSJaegeuk Kim {
79439a53e0cSJaegeuk Kim 	i_ext->fofs = cpu_to_le32(ext->fofs);
7954d0b0bd4SChao Yu 	i_ext->blk = cpu_to_le32(ext->blk);
79639a53e0cSJaegeuk Kim 	i_ext->len = cpu_to_le32(ext->len);
79739a53e0cSJaegeuk Kim }
79839a53e0cSJaegeuk Kim 
799429511cdSChao Yu static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
800429511cdSChao Yu 						u32 blk, unsigned int len)
801429511cdSChao Yu {
802429511cdSChao Yu 	ei->fofs = fofs;
803429511cdSChao Yu 	ei->blk = blk;
804429511cdSChao Yu 	ei->len = len;
805*94afd6d6SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
806*94afd6d6SChao Yu 	ei->c_len = 0;
807*94afd6d6SChao Yu #endif
808429511cdSChao Yu }
809429511cdSChao Yu 
810004b6862SChao Yu static inline bool __is_discard_mergeable(struct discard_info *back,
81135ec7d57SChao Yu 			struct discard_info *front, unsigned int max_len)
812004b6862SChao Yu {
8139a997188SJaegeuk Kim 	return (back->lstart + back->len == front->lstart) &&
81435ec7d57SChao Yu 		(back->len + front->len <= max_len);
815004b6862SChao Yu }
816004b6862SChao Yu 
817004b6862SChao Yu static inline bool __is_discard_back_mergeable(struct discard_info *cur,
81835ec7d57SChao Yu 			struct discard_info *back, unsigned int max_len)
819004b6862SChao Yu {
82035ec7d57SChao Yu 	return __is_discard_mergeable(back, cur, max_len);
821004b6862SChao Yu }
822004b6862SChao Yu 
823004b6862SChao Yu static inline bool __is_discard_front_mergeable(struct discard_info *cur,
82435ec7d57SChao Yu 			struct discard_info *front, unsigned int max_len)
825004b6862SChao Yu {
82635ec7d57SChao Yu 	return __is_discard_mergeable(cur, front, max_len);
827004b6862SChao Yu }
828004b6862SChao Yu 
829429511cdSChao Yu static inline bool __is_extent_mergeable(struct extent_info *back,
830429511cdSChao Yu 						struct extent_info *front)
831429511cdSChao Yu {
832*94afd6d6SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
833*94afd6d6SChao Yu 	if (back->c_len && back->len != back->c_len)
834*94afd6d6SChao Yu 		return false;
835*94afd6d6SChao Yu 	if (front->c_len && front->len != front->c_len)
836*94afd6d6SChao Yu 		return false;
837*94afd6d6SChao Yu #endif
838429511cdSChao Yu 	return (back->fofs + back->len == front->fofs &&
839429511cdSChao Yu 			back->blk + back->len == front->blk);
840429511cdSChao Yu }
841429511cdSChao Yu 
842429511cdSChao Yu static inline bool __is_back_mergeable(struct extent_info *cur,
843429511cdSChao Yu 						struct extent_info *back)
844429511cdSChao Yu {
845429511cdSChao Yu 	return __is_extent_mergeable(back, cur);
846429511cdSChao Yu }
847429511cdSChao Yu 
848429511cdSChao Yu static inline bool __is_front_mergeable(struct extent_info *cur,
849429511cdSChao Yu 						struct extent_info *front)
850429511cdSChao Yu {
851429511cdSChao Yu 	return __is_extent_mergeable(cur, front);
852429511cdSChao Yu }
853429511cdSChao Yu 
854cac5a3d8SDongOh Shin extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
855b430f726SZhikang Zhang static inline void __try_update_largest_extent(struct extent_tree *et,
856b430f726SZhikang Zhang 						struct extent_node *en)
8574abd3f5aSChao Yu {
858205b9822SJaegeuk Kim 	if (en->ei.len > et->largest.len) {
8594abd3f5aSChao Yu 		et->largest = en->ei;
860b430f726SZhikang Zhang 		et->largest_updated = true;
861205b9822SJaegeuk Kim 	}
8624abd3f5aSChao Yu }
8634abd3f5aSChao Yu 
8649a4ffdf5SChao Yu /*
8659a4ffdf5SChao Yu  * For free nid management
8669a4ffdf5SChao Yu  */
8679a4ffdf5SChao Yu enum nid_state {
8689a4ffdf5SChao Yu 	FREE_NID,		/* newly added to free nid list */
8699a4ffdf5SChao Yu 	PREALLOC_NID,		/* it is preallocated */
8709a4ffdf5SChao Yu 	MAX_NID_STATE,
871b8559dc2SChao Yu };
872b8559dc2SChao Yu 
873a95ba66aSJaegeuk Kim enum nat_state {
874a95ba66aSJaegeuk Kim 	TOTAL_NAT,
875a95ba66aSJaegeuk Kim 	DIRTY_NAT,
876a95ba66aSJaegeuk Kim 	RECLAIMABLE_NAT,
877a95ba66aSJaegeuk Kim 	MAX_NAT_STATE,
878a95ba66aSJaegeuk Kim };
879a95ba66aSJaegeuk Kim 
88039a53e0cSJaegeuk Kim struct f2fs_nm_info {
88139a53e0cSJaegeuk Kim 	block_t nat_blkaddr;		/* base disk address of NAT */
88239a53e0cSJaegeuk Kim 	nid_t max_nid;			/* maximum possible node ids */
88304d47e67SChao Yu 	nid_t available_nids;		/* # of available node ids */
88439a53e0cSJaegeuk Kim 	nid_t next_scan_nid;		/* the next nid to be scanned */
885cdfc41c1SJaegeuk Kim 	unsigned int ram_thresh;	/* control the memory footprint */
886ea1a29a0SChao Yu 	unsigned int ra_nid_pages;	/* # of nid pages to be readaheaded */
8872304cb0cSChao Yu 	unsigned int dirty_nats_ratio;	/* control dirty nats ratio threshold */
88839a53e0cSJaegeuk Kim 
88939a53e0cSJaegeuk Kim 	/* NAT cache management */
89039a53e0cSJaegeuk Kim 	struct radix_tree_root nat_root;/* root of the nat entry cache */
891309cc2b6SJaegeuk Kim 	struct radix_tree_root nat_set_root;/* root of the nat set cache */
892f3e367d4Sqiulaibin 	struct rw_semaphore nat_tree_lock;	/* protect nat entry tree */
89339a53e0cSJaegeuk Kim 	struct list_head nat_entries;	/* cached nat entry list (clean) */
89422969158SChao Yu 	spinlock_t nat_list_lock;	/* protect clean nat entry list */
895a95ba66aSJaegeuk Kim 	unsigned int nat_cnt[MAX_NAT_STATE]; /* the # of cached nat entries */
89622ad0b6aSJaegeuk Kim 	unsigned int nat_blocks;	/* # of nat blocks */
89739a53e0cSJaegeuk Kim 
89839a53e0cSJaegeuk Kim 	/* free node ids management */
8998a7ed66aSJaegeuk Kim 	struct radix_tree_root free_nid_root;/* root of the free_nid cache */
9009a4ffdf5SChao Yu 	struct list_head free_nid_list;		/* list for free nids excluding preallocated nids */
9019a4ffdf5SChao Yu 	unsigned int nid_cnt[MAX_NID_STATE];	/* the number of free node id */
902b8559dc2SChao Yu 	spinlock_t nid_list_lock;	/* protect nid lists ops */
90339a53e0cSJaegeuk Kim 	struct mutex build_lock;	/* lock for build free nids */
904bb1105e4SJaegeuk Kim 	unsigned char **free_nid_bitmap;
9054ac91242SChao Yu 	unsigned char *nat_block_bitmap;
906586d1492SChao Yu 	unsigned short *free_nid_count;	/* free nid count of NAT block */
90739a53e0cSJaegeuk Kim 
90839a53e0cSJaegeuk Kim 	/* for checkpoint */
90939a53e0cSJaegeuk Kim 	char *nat_bitmap;		/* NAT bitmap pointer */
91022ad0b6aSJaegeuk Kim 
91122ad0b6aSJaegeuk Kim 	unsigned int nat_bits_blocks;	/* # of nat bits blocks */
91222ad0b6aSJaegeuk Kim 	unsigned char *nat_bits;	/* NAT bits blocks */
91322ad0b6aSJaegeuk Kim 	unsigned char *full_nat_bits;	/* full NAT pages */
91422ad0b6aSJaegeuk Kim 	unsigned char *empty_nat_bits;	/* empty NAT pages */
915599a09b2SChao Yu #ifdef CONFIG_F2FS_CHECK_FS
916599a09b2SChao Yu 	char *nat_bitmap_mir;		/* NAT bitmap mirror */
917599a09b2SChao Yu #endif
91839a53e0cSJaegeuk Kim 	int bitmap_size;		/* bitmap size */
91939a53e0cSJaegeuk Kim };
92039a53e0cSJaegeuk Kim 
92139a53e0cSJaegeuk Kim /*
92239a53e0cSJaegeuk Kim  * this structure is used as one of function parameters.
92339a53e0cSJaegeuk Kim  * all the information are dedicated to a given direct node block determined
92439a53e0cSJaegeuk Kim  * by the data offset in a file.
92539a53e0cSJaegeuk Kim  */
92639a53e0cSJaegeuk Kim struct dnode_of_data {
92739a53e0cSJaegeuk Kim 	struct inode *inode;		/* vfs inode pointer */
92839a53e0cSJaegeuk Kim 	struct page *inode_page;	/* its inode page, NULL is possible */
92939a53e0cSJaegeuk Kim 	struct page *node_page;		/* cached direct node page */
93039a53e0cSJaegeuk Kim 	nid_t nid;			/* node id of the direct node block */
93139a53e0cSJaegeuk Kim 	unsigned int ofs_in_node;	/* data offset in the node page */
93239a53e0cSJaegeuk Kim 	bool inode_page_locked;		/* inode page is locked or not */
93393bae099SJaegeuk Kim 	bool node_changed;		/* is node block changed */
9343cf45747SChao Yu 	char cur_level;			/* level of hole node page */
9353cf45747SChao Yu 	char max_level;			/* level of current page located */
93639a53e0cSJaegeuk Kim 	block_t	data_blkaddr;		/* block address of the node block */
93739a53e0cSJaegeuk Kim };
93839a53e0cSJaegeuk Kim 
93939a53e0cSJaegeuk Kim static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
94039a53e0cSJaegeuk Kim 		struct page *ipage, struct page *npage, nid_t nid)
94139a53e0cSJaegeuk Kim {
942d66d1f76SJaegeuk Kim 	memset(dn, 0, sizeof(*dn));
94339a53e0cSJaegeuk Kim 	dn->inode = inode;
94439a53e0cSJaegeuk Kim 	dn->inode_page = ipage;
94539a53e0cSJaegeuk Kim 	dn->node_page = npage;
94639a53e0cSJaegeuk Kim 	dn->nid = nid;
94739a53e0cSJaegeuk Kim }
94839a53e0cSJaegeuk Kim 
94939a53e0cSJaegeuk Kim /*
95039a53e0cSJaegeuk Kim  * For SIT manager
95139a53e0cSJaegeuk Kim  *
95239a53e0cSJaegeuk Kim  * By default, there are 6 active log areas across the whole main area.
95339a53e0cSJaegeuk Kim  * When considering hot and cold data separation to reduce cleaning overhead,
95439a53e0cSJaegeuk Kim  * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
95539a53e0cSJaegeuk Kim  * respectively.
95639a53e0cSJaegeuk Kim  * In the current design, you should not change the numbers intentionally.
95739a53e0cSJaegeuk Kim  * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
95839a53e0cSJaegeuk Kim  * logs individually according to the underlying devices. (default: 6)
95939a53e0cSJaegeuk Kim  * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
96039a53e0cSJaegeuk Kim  * data and 8 for node logs.
96139a53e0cSJaegeuk Kim  */
96239a53e0cSJaegeuk Kim #define	NR_CURSEG_DATA_TYPE	(3)
96339a53e0cSJaegeuk Kim #define NR_CURSEG_NODE_TYPE	(3)
964093749e2SChao Yu #define NR_CURSEG_INMEM_TYPE	(2)
965a7d9fe3cSJaegeuk Kim #define NR_CURSEG_RO_TYPE	(2)
966d0b9e42aSChao Yu #define NR_CURSEG_PERSIST_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
967d0b9e42aSChao Yu #define NR_CURSEG_TYPE		(NR_CURSEG_INMEM_TYPE + NR_CURSEG_PERSIST_TYPE)
96839a53e0cSJaegeuk Kim 
96939a53e0cSJaegeuk Kim enum {
97039a53e0cSJaegeuk Kim 	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
97139a53e0cSJaegeuk Kim 	CURSEG_WARM_DATA,	/* data blocks */
97239a53e0cSJaegeuk Kim 	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
97339a53e0cSJaegeuk Kim 	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
97439a53e0cSJaegeuk Kim 	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
97539a53e0cSJaegeuk Kim 	CURSEG_COLD_NODE,	/* indirect node blocks */
976d0b9e42aSChao Yu 	NR_PERSISTENT_LOG,	/* number of persistent log */
977d0b9e42aSChao Yu 	CURSEG_COLD_DATA_PINNED = NR_PERSISTENT_LOG,
978d0b9e42aSChao Yu 				/* pinned file that needs consecutive block address */
979093749e2SChao Yu 	CURSEG_ALL_DATA_ATGC,	/* SSR alloctor in hot/warm/cold data area */
980d0b9e42aSChao Yu 	NO_CHECK_TYPE,		/* number of persistent & inmem log */
98139a53e0cSJaegeuk Kim };
98239a53e0cSJaegeuk Kim 
9836b4afdd7SJaegeuk Kim struct flush_cmd {
9846b4afdd7SJaegeuk Kim 	struct completion wait;
985721bd4d5SGu Zheng 	struct llist_node llnode;
98639d787beSChao Yu 	nid_t ino;
9876b4afdd7SJaegeuk Kim 	int ret;
9886b4afdd7SJaegeuk Kim };
9896b4afdd7SJaegeuk Kim 
990a688b9d9SGu Zheng struct flush_cmd_control {
991a688b9d9SGu Zheng 	struct task_struct *f2fs_issue_flush;	/* flush thread */
992a688b9d9SGu Zheng 	wait_queue_head_t flush_wait_queue;	/* waiting queue for wake-up */
9938b8dd65fSChao Yu 	atomic_t issued_flush;			/* # of issued flushes */
99472691af6SJaegeuk Kim 	atomic_t queued_flush;			/* # of queued flushes */
995721bd4d5SGu Zheng 	struct llist_head issue_list;		/* list for command issue */
996721bd4d5SGu Zheng 	struct llist_node *dispatch_list;	/* list for command dispatch */
997a688b9d9SGu Zheng };
998a688b9d9SGu Zheng 
99939a53e0cSJaegeuk Kim struct f2fs_sm_info {
100039a53e0cSJaegeuk Kim 	struct sit_info *sit_info;		/* whole segment information */
100139a53e0cSJaegeuk Kim 	struct free_segmap_info *free_info;	/* free segment information */
100239a53e0cSJaegeuk Kim 	struct dirty_seglist_info *dirty_info;	/* dirty segment information */
100339a53e0cSJaegeuk Kim 	struct curseg_info *curseg_array;	/* active segment information */
100439a53e0cSJaegeuk Kim 
10052b60311dSChao Yu 	struct rw_semaphore curseg_lock;	/* for preventing curseg change */
10062b60311dSChao Yu 
100739a53e0cSJaegeuk Kim 	block_t seg0_blkaddr;		/* block address of 0'th segment */
100839a53e0cSJaegeuk Kim 	block_t main_blkaddr;		/* start block address of main area */
100939a53e0cSJaegeuk Kim 	block_t ssa_blkaddr;		/* start block address of SSA area */
101039a53e0cSJaegeuk Kim 
101139a53e0cSJaegeuk Kim 	unsigned int segment_count;	/* total # of segments */
101239a53e0cSJaegeuk Kim 	unsigned int main_segments;	/* # of segments in main area */
101339a53e0cSJaegeuk Kim 	unsigned int reserved_segments;	/* # of reserved segments */
101439a53e0cSJaegeuk Kim 	unsigned int ovp_segments;	/* # of overprovision segments */
101581eb8d6eSJaegeuk Kim 
101681eb8d6eSJaegeuk Kim 	/* a threshold to reclaim prefree segments */
101781eb8d6eSJaegeuk Kim 	unsigned int rec_prefree_segments;
10187fd9e544SJaegeuk Kim 
1019bba681cbSJaegeuk Kim 	/* for batched trimming */
1020bba681cbSJaegeuk Kim 	unsigned int trim_sections;		/* # of sections to trim */
1021bba681cbSJaegeuk Kim 
1022184a5cd2SChao Yu 	struct list_head sit_entry_set;	/* sit entry set list */
1023184a5cd2SChao Yu 
1024216fbd64SJaegeuk Kim 	unsigned int ipu_policy;	/* in-place-update policy */
1025216fbd64SJaegeuk Kim 	unsigned int min_ipu_util;	/* in-place-update threshold */
1026c1ce1b02SJaegeuk Kim 	unsigned int min_fsync_blocks;	/* threshold for fsync */
1027853137ceSJaegeuk Kim 	unsigned int min_seq_blocks;	/* threshold for sequential blocks */
1028ef095d19SJaegeuk Kim 	unsigned int min_hot_blocks;	/* threshold for hot block allocation */
1029a2a12b67SChao Yu 	unsigned int min_ssr_sections;	/* threshold to trigger SSR allocation */
10306b4afdd7SJaegeuk Kim 
10316b4afdd7SJaegeuk Kim 	/* for flush command control */
1032b01a9201SJaegeuk Kim 	struct flush_cmd_control *fcc_info;
1033a688b9d9SGu Zheng 
10340b54fb84SJaegeuk Kim 	/* for discard command control */
10350b54fb84SJaegeuk Kim 	struct discard_cmd_control *dcc_info;
103639a53e0cSJaegeuk Kim };
103739a53e0cSJaegeuk Kim 
103839a53e0cSJaegeuk Kim /*
103939a53e0cSJaegeuk Kim  * For superblock
104039a53e0cSJaegeuk Kim  */
104139a53e0cSJaegeuk Kim /*
104239a53e0cSJaegeuk Kim  * COUNT_TYPE for monitoring
104339a53e0cSJaegeuk Kim  *
104439a53e0cSJaegeuk Kim  * f2fs monitors the number of several block types such as on-writeback,
104539a53e0cSJaegeuk Kim  * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
104639a53e0cSJaegeuk Kim  */
104736951b38SChao Yu #define WB_DATA_TYPE(p)	(__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
104839a53e0cSJaegeuk Kim enum count_type {
104939a53e0cSJaegeuk Kim 	F2FS_DIRTY_DENTS,
1050c227f912SChao Yu 	F2FS_DIRTY_DATA,
10512c8a4a28SJaegeuk Kim 	F2FS_DIRTY_QDATA,
105239a53e0cSJaegeuk Kim 	F2FS_DIRTY_NODES,
105339a53e0cSJaegeuk Kim 	F2FS_DIRTY_META,
10548dcf2ff7SJaegeuk Kim 	F2FS_INMEM_PAGES,
10550f18b462SJaegeuk Kim 	F2FS_DIRTY_IMETA,
105636951b38SChao Yu 	F2FS_WB_CP_DATA,
105736951b38SChao Yu 	F2FS_WB_DATA,
10585f9abab4SJaegeuk Kim 	F2FS_RD_DATA,
10595f9abab4SJaegeuk Kim 	F2FS_RD_NODE,
10605f9abab4SJaegeuk Kim 	F2FS_RD_META,
106102b16d0aSChao Yu 	F2FS_DIO_WRITE,
106202b16d0aSChao Yu 	F2FS_DIO_READ,
106339a53e0cSJaegeuk Kim 	NR_COUNT_TYPE,
106439a53e0cSJaegeuk Kim };
106539a53e0cSJaegeuk Kim 
106639a53e0cSJaegeuk Kim /*
1067e1c42045Sarter97  * The below are the page types of bios used in submit_bio().
106839a53e0cSJaegeuk Kim  * The available types are:
106939a53e0cSJaegeuk Kim  * DATA			User data pages. It operates as async mode.
107039a53e0cSJaegeuk Kim  * NODE			Node pages. It operates as async mode.
107139a53e0cSJaegeuk Kim  * META			FS metadata pages such as SIT, NAT, CP.
107239a53e0cSJaegeuk Kim  * NR_PAGE_TYPE		The number of page types.
107339a53e0cSJaegeuk Kim  * META_FLUSH		Make sure the previous pages are written
107439a53e0cSJaegeuk Kim  *			with waiting the bio's completion
107539a53e0cSJaegeuk Kim  * ...			Only can be used with META.
107639a53e0cSJaegeuk Kim  */
10777d5e5109SJaegeuk Kim #define PAGE_TYPE_OF_BIO(type)	((type) > META ? META : (type))
107839a53e0cSJaegeuk Kim enum page_type {
107939a53e0cSJaegeuk Kim 	DATA,
108039a53e0cSJaegeuk Kim 	NODE,
108139a53e0cSJaegeuk Kim 	META,
108239a53e0cSJaegeuk Kim 	NR_PAGE_TYPE,
108339a53e0cSJaegeuk Kim 	META_FLUSH,
10848ce67cb0SJaegeuk Kim 	INMEM,		/* the below types are used by tracepoints only. */
10858ce67cb0SJaegeuk Kim 	INMEM_DROP,
10868c242db9SJaegeuk Kim 	INMEM_INVALIDATE,
108728bc106bSChao Yu 	INMEM_REVOKE,
10888ce67cb0SJaegeuk Kim 	IPU,
10898ce67cb0SJaegeuk Kim 	OPU,
109039a53e0cSJaegeuk Kim };
109139a53e0cSJaegeuk Kim 
1092a912b54dSJaegeuk Kim enum temp_type {
1093a912b54dSJaegeuk Kim 	HOT = 0,	/* must be zero for meta bio */
1094a912b54dSJaegeuk Kim 	WARM,
1095a912b54dSJaegeuk Kim 	COLD,
1096a912b54dSJaegeuk Kim 	NR_TEMP_TYPE,
1097a912b54dSJaegeuk Kim };
1098a912b54dSJaegeuk Kim 
1099cc15620bSJaegeuk Kim enum need_lock_type {
1100cc15620bSJaegeuk Kim 	LOCK_REQ = 0,
1101cc15620bSJaegeuk Kim 	LOCK_DONE,
1102cc15620bSJaegeuk Kim 	LOCK_RETRY,
1103cc15620bSJaegeuk Kim };
1104cc15620bSJaegeuk Kim 
1105a5fd5050SChao Yu enum cp_reason_type {
1106a5fd5050SChao Yu 	CP_NO_NEEDED,
1107a5fd5050SChao Yu 	CP_NON_REGULAR,
11084c8ff709SChao Yu 	CP_COMPRESSED,
1109a5fd5050SChao Yu 	CP_HARDLINK,
1110a5fd5050SChao Yu 	CP_SB_NEED_CP,
1111a5fd5050SChao Yu 	CP_WRONG_PINO,
1112a5fd5050SChao Yu 	CP_NO_SPC_ROLL,
1113a5fd5050SChao Yu 	CP_NODE_NEED_CP,
1114a5fd5050SChao Yu 	CP_FASTBOOT_MODE,
1115a5fd5050SChao Yu 	CP_SPEC_LOG_NUM,
11160a007b97SJaegeuk Kim 	CP_RECOVER_DIR,
1117a5fd5050SChao Yu };
1118a5fd5050SChao Yu 
1119b0af6d49SChao Yu enum iostat_type {
11208b83ac81SChao Yu 	/* WRITE IO */
11218b83ac81SChao Yu 	APP_DIRECT_IO,			/* app direct write IOs */
11228b83ac81SChao Yu 	APP_BUFFERED_IO,		/* app buffered write IOs */
1123b0af6d49SChao Yu 	APP_WRITE_IO,			/* app write IOs */
1124b0af6d49SChao Yu 	APP_MAPPED_IO,			/* app mapped IOs */
1125b0af6d49SChao Yu 	FS_DATA_IO,			/* data IOs from kworker/fsync/reclaimer */
1126b0af6d49SChao Yu 	FS_NODE_IO,			/* node IOs from kworker/fsync/reclaimer */
1127b0af6d49SChao Yu 	FS_META_IO,			/* meta IOs from kworker/reclaimer */
1128b0af6d49SChao Yu 	FS_GC_DATA_IO,			/* data IOs from forground gc */
1129b0af6d49SChao Yu 	FS_GC_NODE_IO,			/* node IOs from forground gc */
1130b0af6d49SChao Yu 	FS_CP_DATA_IO,			/* data IOs from checkpoint */
1131b0af6d49SChao Yu 	FS_CP_NODE_IO,			/* node IOs from checkpoint */
1132b0af6d49SChao Yu 	FS_CP_META_IO,			/* meta IOs from checkpoint */
11338b83ac81SChao Yu 
11348b83ac81SChao Yu 	/* READ IO */
11358b83ac81SChao Yu 	APP_DIRECT_READ_IO,		/* app direct read IOs */
11368b83ac81SChao Yu 	APP_BUFFERED_READ_IO,		/* app buffered read IOs */
11378b83ac81SChao Yu 	APP_READ_IO,			/* app read IOs */
11388b83ac81SChao Yu 	APP_MAPPED_READ_IO,		/* app mapped read IOs */
11398b83ac81SChao Yu 	FS_DATA_READ_IO,		/* data read IOs */
11409c122384SChao Yu 	FS_GDATA_READ_IO,		/* data read IOs from background gc */
11419c122384SChao Yu 	FS_CDATA_READ_IO,		/* compressed data read IOs */
11428b83ac81SChao Yu 	FS_NODE_READ_IO,		/* node read IOs */
11438b83ac81SChao Yu 	FS_META_READ_IO,		/* meta read IOs */
11448b83ac81SChao Yu 
11458b83ac81SChao Yu 	/* other */
1146b0af6d49SChao Yu 	FS_DISCARD,			/* discard */
1147b0af6d49SChao Yu 	NR_IO_TYPE,
1148b0af6d49SChao Yu };
1149b0af6d49SChao Yu 
1150458e6197SJaegeuk Kim struct f2fs_io_info {
115105ca3632SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs_sb_info pointer */
115239d787beSChao Yu 	nid_t ino;		/* inode number */
1153458e6197SJaegeuk Kim 	enum page_type type;	/* contains DATA/NODE/META/META_FLUSH */
1154a912b54dSJaegeuk Kim 	enum temp_type temp;	/* contains HOT/WARM/COLD */
115504d328deSMike Christie 	int op;			/* contains REQ_OP_ */
1156ef295ecfSChristoph Hellwig 	int op_flags;		/* req_flag_bits */
11577a9d7548SChao Yu 	block_t new_blkaddr;	/* new block address to be written */
115828bc106bSChao Yu 	block_t old_blkaddr;	/* old block address before Cow */
115905ca3632SJaegeuk Kim 	struct page *page;	/* page to be written */
11604375a336SJaegeuk Kim 	struct page *encrypted_page;	/* encrypted page */
11614c8ff709SChao Yu 	struct page *compressed_page;	/* compressed page */
1162fb830fc5SChao Yu 	struct list_head list;		/* serialize IOs */
1163d68f735bSJaegeuk Kim 	bool submitted;		/* indicate IO submission */
1164cc15620bSJaegeuk Kim 	int need_lock;		/* indicate we need to lock cp_rwsem */
1165fb830fc5SChao Yu 	bool in_list;		/* indicate fio is in io_list */
11666dc3a126SChao Yu 	bool is_por;		/* indicate IO is from recovery or not */
1167fe16efe6SChao Yu 	bool retry;		/* need to reallocate block address */
11684c8ff709SChao Yu 	int compr_blocks;	/* # of compressed block addresses */
11694c8ff709SChao Yu 	bool encrypted;		/* indicate file is encrypted */
1170b0af6d49SChao Yu 	enum iostat_type io_type;	/* io type */
1171578c6478SYufen Yu 	struct writeback_control *io_wbc; /* writeback control */
11728648de2cSChao Yu 	struct bio **bio;		/* bio for ipu */
11738648de2cSChao Yu 	sector_t *last_block;		/* last block number in bio */
11747735730dSChao Yu 	unsigned char version;		/* version of the node */
1175458e6197SJaegeuk Kim };
1176458e6197SJaegeuk Kim 
11770b20fcecSChao Yu struct bio_entry {
11780b20fcecSChao Yu 	struct bio *bio;
11790b20fcecSChao Yu 	struct list_head list;
11800b20fcecSChao Yu };
11810b20fcecSChao Yu 
118268afcf2dSTomohiro Kusumi #define is_read_io(rw) ((rw) == READ)
11831ff7bd3bSJaegeuk Kim struct f2fs_bio_info {
1184458e6197SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs superblock */
11851ff7bd3bSJaegeuk Kim 	struct bio *bio;		/* bios to merge */
11861ff7bd3bSJaegeuk Kim 	sector_t last_block_in_bio;	/* last block number */
1187458e6197SJaegeuk Kim 	struct f2fs_io_info fio;	/* store buffered io info. */
1188df0f8dc0SChao Yu 	struct rw_semaphore io_rwsem;	/* blocking op for bio */
1189fb830fc5SChao Yu 	spinlock_t io_lock;		/* serialize DATA/NODE IOs */
1190fb830fc5SChao Yu 	struct list_head io_list;	/* track fios */
11910b20fcecSChao Yu 	struct list_head bio_list;	/* bio entry list head */
11920b20fcecSChao Yu 	struct rw_semaphore bio_list_lock;	/* lock to protect bio entry list */
11931ff7bd3bSJaegeuk Kim };
11941ff7bd3bSJaegeuk Kim 
11953c62be17SJaegeuk Kim #define FDEV(i)				(sbi->devs[i])
11963c62be17SJaegeuk Kim #define RDEV(i)				(raw_super->devs[i])
11973c62be17SJaegeuk Kim struct f2fs_dev_info {
11983c62be17SJaegeuk Kim 	struct block_device *bdev;
11993c62be17SJaegeuk Kim 	char path[MAX_PATH_LEN];
12003c62be17SJaegeuk Kim 	unsigned int total_segments;
12013c62be17SJaegeuk Kim 	block_t start_blk;
12023c62be17SJaegeuk Kim 	block_t end_blk;
12033c62be17SJaegeuk Kim #ifdef CONFIG_BLK_DEV_ZONED
12043c62be17SJaegeuk Kim 	unsigned int nr_blkz;		/* Total number of zones */
120595175dafSDamien Le Moal 	unsigned long *blkz_seq;	/* Bitmap indicating sequential zones */
1206de881df9SAravind Ramesh 	block_t *zone_capacity_blocks;  /* Array of zone capacity in blks */
12073c62be17SJaegeuk Kim #endif
12083c62be17SJaegeuk Kim };
12093c62be17SJaegeuk Kim 
1210c227f912SChao Yu enum inode_type {
1211c227f912SChao Yu 	DIR_INODE,			/* for dirty dir inode */
1212c227f912SChao Yu 	FILE_INODE,			/* for dirty regular/symlink inode */
12130f18b462SJaegeuk Kim 	DIRTY_META,			/* for all dirtied inode metadata */
121457864ae5SJaegeuk Kim 	ATOMIC_FILE,			/* for all atomic files */
1215c227f912SChao Yu 	NR_INODE_TYPE,
1216c227f912SChao Yu };
1217c227f912SChao Yu 
121867298804SChao Yu /* for inner inode cache management */
121967298804SChao Yu struct inode_management {
122067298804SChao Yu 	struct radix_tree_root ino_root;	/* ino entry array */
122167298804SChao Yu 	spinlock_t ino_lock;			/* for ino entry lock */
122267298804SChao Yu 	struct list_head ino_list;		/* inode list head */
122367298804SChao Yu 	unsigned long ino_num;			/* number of entries */
122467298804SChao Yu };
122567298804SChao Yu 
1226093749e2SChao Yu /* for GC_AT */
1227093749e2SChao Yu struct atgc_management {
1228093749e2SChao Yu 	bool atgc_enabled;			/* ATGC is enabled or not */
1229093749e2SChao Yu 	struct rb_root_cached root;		/* root of victim rb-tree */
1230093749e2SChao Yu 	struct list_head victim_list;		/* linked with all victim entries */
1231093749e2SChao Yu 	unsigned int victim_count;		/* victim count in rb-tree */
1232093749e2SChao Yu 	unsigned int candidate_ratio;		/* candidate ratio */
1233093749e2SChao Yu 	unsigned int max_candidate_count;	/* max candidate count */
1234093749e2SChao Yu 	unsigned int age_weight;		/* age weight, vblock_weight = 100 - age_weight */
1235093749e2SChao Yu 	unsigned long long age_threshold;	/* age threshold */
1236093749e2SChao Yu };
1237093749e2SChao Yu 
1238caf0047eSChao Yu /* For s_flag in struct f2fs_sb_info */
1239caf0047eSChao Yu enum {
1240caf0047eSChao Yu 	SBI_IS_DIRTY,				/* dirty flag for checkpoint */
1241caf0047eSChao Yu 	SBI_IS_CLOSE,				/* specify unmounting */
1242caf0047eSChao Yu 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
1243caf0047eSChao Yu 	SBI_POR_DOING,				/* recovery is doing or not */
1244df728b0fSJaegeuk Kim 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
1245bbf156f7SJaegeuk Kim 	SBI_NEED_CP,				/* need to checkpoint */
124683a3bfdbSJaegeuk Kim 	SBI_IS_SHUTDOWN,			/* shutdown by ioctl */
12471378752bSChao Yu 	SBI_IS_RECOVERED,			/* recovered orphan/data */
12484354994fSDaniel Rosenberg 	SBI_CP_DISABLED,			/* CP was disabled last mount */
1249db610a64SJaegeuk Kim 	SBI_CP_DISABLED_QUICK,			/* CP was disabled quickly */
1250af033b2aSChao Yu 	SBI_QUOTA_NEED_FLUSH,			/* need to flush quota info in CP */
1251af033b2aSChao Yu 	SBI_QUOTA_SKIP_FLUSH,			/* skip flushing quota in current CP */
1252af033b2aSChao Yu 	SBI_QUOTA_NEED_REPAIR,			/* quota file may be corrupted */
125304f0b2eaSQiuyang Sun 	SBI_IS_RESIZEFS,			/* resizefs is in process */
1254caf0047eSChao Yu };
1255caf0047eSChao Yu 
12566beceb54SJaegeuk Kim enum {
12576beceb54SJaegeuk Kim 	CP_TIME,
1258d0239e1bSJaegeuk Kim 	REQ_TIME,
1259a7d10cf3SSahitya Tummala 	DISCARD_TIME,
1260a7d10cf3SSahitya Tummala 	GC_TIME,
12614354994fSDaniel Rosenberg 	DISABLE_TIME,
126203f2c02dSJaegeuk Kim 	UMOUNT_DISCARD_TIMEOUT,
12636beceb54SJaegeuk Kim 	MAX_TIME,
12646beceb54SJaegeuk Kim };
12656beceb54SJaegeuk Kim 
12660cdd3195SHyunchul Lee enum {
12675b0e9539SJaegeuk Kim 	GC_NORMAL,
12685b0e9539SJaegeuk Kim 	GC_IDLE_CB,
12695b0e9539SJaegeuk Kim 	GC_IDLE_GREEDY,
1270093749e2SChao Yu 	GC_IDLE_AT,
12710e5e8111SDaeho Jeong 	GC_URGENT_HIGH,
12720e5e8111SDaeho Jeong 	GC_URGENT_LOW,
127307c6b593SDaeho Jeong 	MAX_GC_MODE,
12745b0e9539SJaegeuk Kim };
12755b0e9539SJaegeuk Kim 
12765b0e9539SJaegeuk Kim enum {
1277bbbc34fdSChao Yu 	BGGC_MODE_ON,		/* background gc is on */
1278bbbc34fdSChao Yu 	BGGC_MODE_OFF,		/* background gc is off */
1279bbbc34fdSChao Yu 	BGGC_MODE_SYNC,		/*
1280bbbc34fdSChao Yu 				 * background gc is on, migrating blocks
1281bbbc34fdSChao Yu 				 * like foreground gc
1282bbbc34fdSChao Yu 				 */
1283bbbc34fdSChao Yu };
1284bbbc34fdSChao Yu 
1285bbbc34fdSChao Yu enum {
1286b0332a0fSChao Yu 	FS_MODE_ADAPTIVE,	/* use both lfs/ssr allocation */
1287b0332a0fSChao Yu 	FS_MODE_LFS,		/* use lfs allocation only */
1288b0332a0fSChao Yu };
1289b0332a0fSChao Yu 
1290b0332a0fSChao Yu enum {
12910cdd3195SHyunchul Lee 	WHINT_MODE_OFF,		/* not pass down write hints */
12920cdd3195SHyunchul Lee 	WHINT_MODE_USER,	/* try to pass down hints given by users */
1293f2e703f9SHyunchul Lee 	WHINT_MODE_FS,		/* pass down hints with F2FS policy */
12940cdd3195SHyunchul Lee };
12950cdd3195SHyunchul Lee 
129607939627SJaegeuk Kim enum {
129707939627SJaegeuk Kim 	ALLOC_MODE_DEFAULT,	/* stay default */
129807939627SJaegeuk Kim 	ALLOC_MODE_REUSE,	/* reuse segments as much as possible */
129907939627SJaegeuk Kim };
130007939627SJaegeuk Kim 
130193cf93f1SJunling Zheng enum fsync_mode {
130293cf93f1SJunling Zheng 	FSYNC_MODE_POSIX,	/* fsync follows posix semantics */
130393cf93f1SJunling Zheng 	FSYNC_MODE_STRICT,	/* fsync behaves in line with ext4 */
1304d6290814SJaegeuk Kim 	FSYNC_MODE_NOBARRIER,	/* fsync behaves nobarrier based on posix */
130593cf93f1SJunling Zheng };
130693cf93f1SJunling Zheng 
1307602a16d5SDaeho Jeong enum {
1308602a16d5SDaeho Jeong 	COMPR_MODE_FS,		/*
1309602a16d5SDaeho Jeong 				 * automatically compress compression
1310602a16d5SDaeho Jeong 				 * enabled files
1311602a16d5SDaeho Jeong 				 */
1312602a16d5SDaeho Jeong 	COMPR_MODE_USER,	/*
1313602a16d5SDaeho Jeong 				 * automatical compression is disabled.
1314602a16d5SDaeho Jeong 				 * user can control the file compression
1315602a16d5SDaeho Jeong 				 * using ioctls
1316602a16d5SDaeho Jeong 				 */
1317602a16d5SDaeho Jeong };
1318602a16d5SDaeho Jeong 
13194f993264SChao Yu enum {
13204f993264SChao Yu 	DISCARD_UNIT_BLOCK,	/* basic discard unit is block */
13214f993264SChao Yu 	DISCARD_UNIT_SEGMENT,	/* basic discard unit is segment */
13224f993264SChao Yu 	DISCARD_UNIT_SECTION,	/* basic discard unit is section */
13234f993264SChao Yu };
13244f993264SChao Yu 
1325b763f3beSChao Yu static inline int f2fs_test_bit(unsigned int nr, char *addr);
1326b763f3beSChao Yu static inline void f2fs_set_bit(unsigned int nr, char *addr);
1327b763f3beSChao Yu static inline void f2fs_clear_bit(unsigned int nr, char *addr);
13284c8ff709SChao Yu 
1329b763f3beSChao Yu /*
1330b763f3beSChao Yu  * Layout of f2fs page.private:
1331b763f3beSChao Yu  *
1332b763f3beSChao Yu  * Layout A: lowest bit should be 1
1333b763f3beSChao Yu  * | bit0 = 1 | bit1 | bit2 | ... | bit MAX | private data .... |
1334b763f3beSChao Yu  * bit 0	PAGE_PRIVATE_NOT_POINTER
1335b763f3beSChao Yu  * bit 1	PAGE_PRIVATE_ATOMIC_WRITE
1336b763f3beSChao Yu  * bit 2	PAGE_PRIVATE_DUMMY_WRITE
1337b763f3beSChao Yu  * bit 3	PAGE_PRIVATE_ONGOING_MIGRATION
1338b763f3beSChao Yu  * bit 4	PAGE_PRIVATE_INLINE_INODE
1339b763f3beSChao Yu  * bit 5	PAGE_PRIVATE_REF_RESOURCE
1340b763f3beSChao Yu  * bit 6-	f2fs private data
1341b763f3beSChao Yu  *
1342b763f3beSChao Yu  * Layout B: lowest bit should be 0
1343b763f3beSChao Yu  * page.private is a wrapped pointer.
1344b763f3beSChao Yu  */
1345b763f3beSChao Yu enum {
1346b763f3beSChao Yu 	PAGE_PRIVATE_NOT_POINTER,		/* private contains non-pointer data */
1347b763f3beSChao Yu 	PAGE_PRIVATE_ATOMIC_WRITE,		/* data page from atomic write path */
1348b763f3beSChao Yu 	PAGE_PRIVATE_DUMMY_WRITE,		/* data page for padding aligned IO */
1349b763f3beSChao Yu 	PAGE_PRIVATE_ONGOING_MIGRATION,		/* data page which is on-going migrating */
1350b763f3beSChao Yu 	PAGE_PRIVATE_INLINE_INODE,		/* inode page contains inline data */
1351b763f3beSChao Yu 	PAGE_PRIVATE_REF_RESOURCE,		/* dirty page has referenced resources */
1352b763f3beSChao Yu 	PAGE_PRIVATE_MAX
1353b763f3beSChao Yu };
1354b763f3beSChao Yu 
1355b763f3beSChao Yu #define PAGE_PRIVATE_GET_FUNC(name, flagname) \
1356b763f3beSChao Yu static inline bool page_private_##name(struct page *page) \
1357b763f3beSChao Yu { \
1358c9ebd3dfSJaegeuk Kim 	return PagePrivate(page) && \
1359c9ebd3dfSJaegeuk Kim 		test_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page)) && \
1360b763f3beSChao Yu 		test_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
1361b763f3beSChao Yu }
1362b763f3beSChao Yu 
1363b763f3beSChao Yu #define PAGE_PRIVATE_SET_FUNC(name, flagname) \
1364b763f3beSChao Yu static inline void set_page_private_##name(struct page *page) \
1365b763f3beSChao Yu { \
1366b763f3beSChao Yu 	if (!PagePrivate(page)) { \
1367b763f3beSChao Yu 		get_page(page); \
1368b763f3beSChao Yu 		SetPagePrivate(page); \
1369c9ebd3dfSJaegeuk Kim 		set_page_private(page, 0); \
1370b763f3beSChao Yu 	} \
1371b763f3beSChao Yu 	set_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page)); \
1372b763f3beSChao Yu 	set_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
1373b763f3beSChao Yu }
1374b763f3beSChao Yu 
1375b763f3beSChao Yu #define PAGE_PRIVATE_CLEAR_FUNC(name, flagname) \
1376b763f3beSChao Yu static inline void clear_page_private_##name(struct page *page) \
1377b763f3beSChao Yu { \
1378b763f3beSChao Yu 	clear_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
1379b763f3beSChao Yu 	if (page_private(page) == 1 << PAGE_PRIVATE_NOT_POINTER) { \
1380b763f3beSChao Yu 		set_page_private(page, 0); \
1381b763f3beSChao Yu 		if (PagePrivate(page)) { \
1382b763f3beSChao Yu 			ClearPagePrivate(page); \
1383b763f3beSChao Yu 			put_page(page); \
1384b763f3beSChao Yu 		}\
1385b763f3beSChao Yu 	} \
1386b763f3beSChao Yu }
1387b763f3beSChao Yu 
1388b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(nonpointer, NOT_POINTER);
1389b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(reference, REF_RESOURCE);
1390b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(inline, INLINE_INODE);
1391b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(gcing, ONGOING_MIGRATION);
1392b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(atomic, ATOMIC_WRITE);
1393b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(dummy, DUMMY_WRITE);
1394b763f3beSChao Yu 
1395b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(reference, REF_RESOURCE);
1396b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(inline, INLINE_INODE);
1397b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(gcing, ONGOING_MIGRATION);
1398b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(atomic, ATOMIC_WRITE);
1399b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(dummy, DUMMY_WRITE);
1400b763f3beSChao Yu 
1401b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(reference, REF_RESOURCE);
1402b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(inline, INLINE_INODE);
1403b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(gcing, ONGOING_MIGRATION);
1404b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(atomic, ATOMIC_WRITE);
1405b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(dummy, DUMMY_WRITE);
14064c8ff709SChao Yu 
14076ce19affSChao Yu static inline unsigned long get_page_private_data(struct page *page)
14086ce19affSChao Yu {
14096ce19affSChao Yu 	unsigned long data = page_private(page);
14106ce19affSChao Yu 
14116ce19affSChao Yu 	if (!test_bit(PAGE_PRIVATE_NOT_POINTER, &data))
14126ce19affSChao Yu 		return 0;
14136ce19affSChao Yu 	return data >> PAGE_PRIVATE_MAX;
14146ce19affSChao Yu }
14156ce19affSChao Yu 
14166ce19affSChao Yu static inline void set_page_private_data(struct page *page, unsigned long data)
14176ce19affSChao Yu {
14186ce19affSChao Yu 	if (!PagePrivate(page)) {
14196ce19affSChao Yu 		get_page(page);
14206ce19affSChao Yu 		SetPagePrivate(page);
1421c9ebd3dfSJaegeuk Kim 		set_page_private(page, 0);
14226ce19affSChao Yu 	}
14236ce19affSChao Yu 	set_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page));
14246ce19affSChao Yu 	page_private(page) |= data << PAGE_PRIVATE_MAX;
14256ce19affSChao Yu }
14266ce19affSChao Yu 
14276ce19affSChao Yu static inline void clear_page_private_data(struct page *page)
14286ce19affSChao Yu {
14296ce19affSChao Yu 	page_private(page) &= (1 << PAGE_PRIVATE_MAX) - 1;
14306ce19affSChao Yu 	if (page_private(page) == 1 << PAGE_PRIVATE_NOT_POINTER) {
14316ce19affSChao Yu 		set_page_private(page, 0);
14326ce19affSChao Yu 		if (PagePrivate(page)) {
14336ce19affSChao Yu 			ClearPagePrivate(page);
14346ce19affSChao Yu 			put_page(page);
14356ce19affSChao Yu 		}
14366ce19affSChao Yu 	}
14376ce19affSChao Yu }
14386ce19affSChao Yu 
14394c8ff709SChao Yu /* For compression */
14404c8ff709SChao Yu enum compress_algorithm_type {
14414c8ff709SChao Yu 	COMPRESS_LZO,
14424c8ff709SChao Yu 	COMPRESS_LZ4,
144350cfa66fSChao Yu 	COMPRESS_ZSTD,
14446d92b201SChao Yu 	COMPRESS_LZORLE,
14454c8ff709SChao Yu 	COMPRESS_MAX,
14464c8ff709SChao Yu };
14474c8ff709SChao Yu 
1448b28f047bSChao Yu enum compress_flag {
1449b28f047bSChao Yu 	COMPRESS_CHKSUM,
1450b28f047bSChao Yu 	COMPRESS_MAX_FLAG,
1451b28f047bSChao Yu };
1452b28f047bSChao Yu 
14536ce19affSChao Yu #define	COMPRESS_WATERMARK			20
14546ce19affSChao Yu #define	COMPRESS_PERCENT			20
14556ce19affSChao Yu 
1456b28f047bSChao Yu #define COMPRESS_DATA_RESERVED_SIZE		4
14574c8ff709SChao Yu struct compress_data {
14584c8ff709SChao Yu 	__le32 clen;			/* compressed data size */
1459b28f047bSChao Yu 	__le32 chksum;			/* compressed data chksum */
14604c8ff709SChao Yu 	__le32 reserved[COMPRESS_DATA_RESERVED_SIZE];	/* reserved */
14614c8ff709SChao Yu 	u8 cdata[];			/* compressed data */
14624c8ff709SChao Yu };
14634c8ff709SChao Yu 
14644c8ff709SChao Yu #define COMPRESS_HEADER_SIZE	(sizeof(struct compress_data))
14654c8ff709SChao Yu 
14664c8ff709SChao Yu #define F2FS_COMPRESSED_PAGE_MAGIC	0xF5F2C000
14674c8ff709SChao Yu 
14683fde13f8SChao Yu #define	COMPRESS_LEVEL_OFFSET	8
14693fde13f8SChao Yu 
14704c8ff709SChao Yu /* compress context */
14714c8ff709SChao Yu struct compress_ctx {
14724c8ff709SChao Yu 	struct inode *inode;		/* inode the context belong to */
14734c8ff709SChao Yu 	pgoff_t cluster_idx;		/* cluster index number */
14744c8ff709SChao Yu 	unsigned int cluster_size;	/* page count in cluster */
14754c8ff709SChao Yu 	unsigned int log_cluster_size;	/* log of cluster size */
14764c8ff709SChao Yu 	struct page **rpages;		/* pages store raw data in cluster */
14774c8ff709SChao Yu 	unsigned int nr_rpages;		/* total page number in rpages */
14784c8ff709SChao Yu 	struct page **cpages;		/* pages store compressed data in cluster */
14794c8ff709SChao Yu 	unsigned int nr_cpages;		/* total page number in cpages */
14804c8ff709SChao Yu 	void *rbuf;			/* virtual mapped address on rpages */
14814c8ff709SChao Yu 	struct compress_data *cbuf;	/* virtual mapped address on cpages */
14824c8ff709SChao Yu 	size_t rlen;			/* valid data length in rbuf */
14834c8ff709SChao Yu 	size_t clen;			/* valid data length in cbuf */
14844c8ff709SChao Yu 	void *private;			/* payload buffer for specified compression algorithm */
148550cfa66fSChao Yu 	void *private2;			/* extra payload buffer */
14864c8ff709SChao Yu };
14874c8ff709SChao Yu 
14884c8ff709SChao Yu /* compress context for write IO path */
14894c8ff709SChao Yu struct compress_io_ctx {
14904c8ff709SChao Yu 	u32 magic;			/* magic number to indicate page is compressed */
14914c8ff709SChao Yu 	struct inode *inode;		/* inode the context belong to */
14924c8ff709SChao Yu 	struct page **rpages;		/* pages store raw data in cluster */
14934c8ff709SChao Yu 	unsigned int nr_rpages;		/* total page number in rpages */
1494e6c3948dSChao Yu 	atomic_t pending_pages;		/* in-flight compressed page count */
14954c8ff709SChao Yu };
14964c8ff709SChao Yu 
14977f59b277SEric Biggers /* Context for decompressing one cluster on the read IO path */
14984c8ff709SChao Yu struct decompress_io_ctx {
14994c8ff709SChao Yu 	u32 magic;			/* magic number to indicate page is compressed */
15004c8ff709SChao Yu 	struct inode *inode;		/* inode the context belong to */
15014c8ff709SChao Yu 	pgoff_t cluster_idx;		/* cluster index number */
15024c8ff709SChao Yu 	unsigned int cluster_size;	/* page count in cluster */
15034c8ff709SChao Yu 	unsigned int log_cluster_size;	/* log of cluster size */
15044c8ff709SChao Yu 	struct page **rpages;		/* pages store raw data in cluster */
15054c8ff709SChao Yu 	unsigned int nr_rpages;		/* total page number in rpages */
15064c8ff709SChao Yu 	struct page **cpages;		/* pages store compressed data in cluster */
15074c8ff709SChao Yu 	unsigned int nr_cpages;		/* total page number in cpages */
15084c8ff709SChao Yu 	struct page **tpages;		/* temp pages to pad holes in cluster */
15094c8ff709SChao Yu 	void *rbuf;			/* virtual mapped address on rpages */
15104c8ff709SChao Yu 	struct compress_data *cbuf;	/* virtual mapped address on cpages */
15114c8ff709SChao Yu 	size_t rlen;			/* valid data length in rbuf */
15124c8ff709SChao Yu 	size_t clen;			/* valid data length in cbuf */
15137f59b277SEric Biggers 
15147f59b277SEric Biggers 	/*
15157f59b277SEric Biggers 	 * The number of compressed pages remaining to be read in this cluster.
15167f59b277SEric Biggers 	 * This is initially nr_cpages.  It is decremented by 1 each time a page
15177f59b277SEric Biggers 	 * has been read (or failed to be read).  When it reaches 0, the cluster
15187f59b277SEric Biggers 	 * is decompressed (or an error is reported).
15197f59b277SEric Biggers 	 *
15207f59b277SEric Biggers 	 * If an error occurs before all the pages have been submitted for I/O,
15217f59b277SEric Biggers 	 * then this will never reach 0.  In this case the I/O submitter is
15227f59b277SEric Biggers 	 * responsible for calling f2fs_decompress_end_io() instead.
15237f59b277SEric Biggers 	 */
15247f59b277SEric Biggers 	atomic_t remaining_pages;
15257f59b277SEric Biggers 
15267f59b277SEric Biggers 	/*
15277f59b277SEric Biggers 	 * Number of references to this decompress_io_ctx.
15287f59b277SEric Biggers 	 *
15297f59b277SEric Biggers 	 * One reference is held for I/O completion.  This reference is dropped
15307f59b277SEric Biggers 	 * after the pagecache pages are updated and unlocked -- either after
15317f59b277SEric Biggers 	 * decompression (and verity if enabled), or after an error.
15327f59b277SEric Biggers 	 *
15337f59b277SEric Biggers 	 * In addition, each compressed page holds a reference while it is in a
15347f59b277SEric Biggers 	 * bio.  These references are necessary prevent compressed pages from
15357f59b277SEric Biggers 	 * being freed while they are still in a bio.
15367f59b277SEric Biggers 	 */
15377f59b277SEric Biggers 	refcount_t refcnt;
15387f59b277SEric Biggers 
15397f59b277SEric Biggers 	bool failed;			/* IO error occurred before decompression? */
15407f59b277SEric Biggers 	bool need_verity;		/* need fs-verity verification after decompression? */
154150cfa66fSChao Yu 	void *private;			/* payload buffer for specified decompression algorithm */
154250cfa66fSChao Yu 	void *private2;			/* extra payload buffer */
15437f59b277SEric Biggers 	struct work_struct verity_work;	/* work to verify the decompressed pages */
15444c8ff709SChao Yu };
15454c8ff709SChao Yu 
15464c8ff709SChao Yu #define NULL_CLUSTER			((unsigned int)(~0))
15474c8ff709SChao Yu #define MIN_COMPRESS_LOG_SIZE		2
15484c8ff709SChao Yu #define MAX_COMPRESS_LOG_SIZE		8
15490e2b7385SChao Yu #define MAX_COMPRESS_WINDOW_SIZE(log_size)	((PAGE_SIZE) << (log_size))
15504c8ff709SChao Yu 
155139a53e0cSJaegeuk Kim struct f2fs_sb_info {
155239a53e0cSJaegeuk Kim 	struct super_block *sb;			/* pointer to VFS super block */
15535e176d54SJaegeuk Kim 	struct proc_dir_entry *s_proc;		/* proc entry */
155439a53e0cSJaegeuk Kim 	struct f2fs_super_block *raw_super;	/* raw super block pointer */
1555846ae671SChao Yu 	struct rw_semaphore sb_lock;		/* lock for raw super block */
1556e8240f65SChao Yu 	int valid_super_block;			/* valid super block no */
1557fadb2fb8SChao Yu 	unsigned long s_flag;				/* flags for sbi */
1558853137ceSJaegeuk Kim 	struct mutex writepages;		/* mutex for writepages() */
155939a53e0cSJaegeuk Kim 
1560178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
1561178053e2SDamien Le Moal 	unsigned int blocks_per_blkz;		/* F2FS blocks per zone */
1562178053e2SDamien Le Moal 	unsigned int log_blocks_per_blkz;	/* log2 F2FS blocks per zone */
1563178053e2SDamien Le Moal #endif
1564178053e2SDamien Le Moal 
156539a53e0cSJaegeuk Kim 	/* for node-related operations */
156639a53e0cSJaegeuk Kim 	struct f2fs_nm_info *nm_info;		/* node manager */
156739a53e0cSJaegeuk Kim 	struct inode *node_inode;		/* cache node blocks */
156839a53e0cSJaegeuk Kim 
156939a53e0cSJaegeuk Kim 	/* for segment-related operations */
157039a53e0cSJaegeuk Kim 	struct f2fs_sm_info *sm_info;		/* segment manager */
15711ff7bd3bSJaegeuk Kim 
15721ff7bd3bSJaegeuk Kim 	/* for bio operations */
1573a912b54dSJaegeuk Kim 	struct f2fs_bio_info *write_io[NR_PAGE_TYPE];	/* for write bios */
1574107a805dSChao Yu 	/* keep migration IO order for LFS mode */
1575107a805dSChao Yu 	struct rw_semaphore io_order_lock;
15760a595ebaSJaegeuk Kim 	mempool_t *write_io_dummy;		/* Dummy pages */
157739a53e0cSJaegeuk Kim 
157839a53e0cSJaegeuk Kim 	/* for checkpoint */
157939a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt;		/* raw checkpoint pointer */
15808508e44aSJaegeuk Kim 	int cur_cp_pack;			/* remain current cp pack */
1581aaec2b1dSChao Yu 	spinlock_t cp_lock;			/* for flag in ckpt */
158239a53e0cSJaegeuk Kim 	struct inode *meta_inode;		/* cache meta blocks */
15838769918bSSahitya Tummala 	struct rw_semaphore cp_global_sem;	/* checkpoint procedure lock */
1584b873b798SJaegeuk Kim 	struct rw_semaphore cp_rwsem;		/* blocking FS operations */
1585b3582c68SChao Yu 	struct rw_semaphore node_write;		/* locking node writes */
158659c9081bSYunlei He 	struct rw_semaphore node_change;	/* locking node change */
1587fb51b5efSChangman Lee 	wait_queue_head_t cp_wait;
15886beceb54SJaegeuk Kim 	unsigned long last_time[MAX_TIME];	/* to store time in jiffies */
15896beceb54SJaegeuk Kim 	long interval_time[MAX_TIME];		/* to store thresholds */
1590261eeb9cSDaeho Jeong 	struct ckpt_req_control cprc_info;	/* for checkpoint request control */
159139a53e0cSJaegeuk Kim 
159267298804SChao Yu 	struct inode_management im[MAX_INO_ENTRY];	/* manage inode cache */
15936451e041SJaegeuk Kim 
159450fa53ecSChao Yu 	spinlock_t fsync_node_lock;		/* for node entry lock */
159550fa53ecSChao Yu 	struct list_head fsync_node_list;	/* node list head */
159650fa53ecSChao Yu 	unsigned int fsync_seg_id;		/* sequence id */
159750fa53ecSChao Yu 	unsigned int fsync_node_num;		/* number of node entries */
159850fa53ecSChao Yu 
15996451e041SJaegeuk Kim 	/* for orphan inode, use 0'th array */
16000d47c1adSGu Zheng 	unsigned int max_orphans;		/* max orphan inodes */
160139a53e0cSJaegeuk Kim 
1602c227f912SChao Yu 	/* for inode management */
1603c227f912SChao Yu 	struct list_head inode_list[NR_INODE_TYPE];	/* dirty inode list */
1604c227f912SChao Yu 	spinlock_t inode_lock[NR_INODE_TYPE];	/* for dirty inode list lock */
1605040d2bb3SChao Yu 	struct mutex flush_lock;		/* for flush exclusion */
160639a53e0cSJaegeuk Kim 
160713054c54SChao Yu 	/* for extent tree cache */
160813054c54SChao Yu 	struct radix_tree_root extent_tree_root;/* cache extent cache entries */
16095e8256acSYunlei He 	struct mutex extent_tree_lock;	/* locking extent radix tree */
161013054c54SChao Yu 	struct list_head extent_list;		/* lru list for shrinker */
161113054c54SChao Yu 	spinlock_t extent_lock;			/* locking extent lru list */
16127441ccefSJaegeuk Kim 	atomic_t total_ext_tree;		/* extent tree count */
1613137d09f0SJaegeuk Kim 	struct list_head zombie_list;		/* extent zombie tree list */
161474fd8d99SJaegeuk Kim 	atomic_t total_zombie_tree;		/* extent zombie tree count */
161513054c54SChao Yu 	atomic_t total_ext_node;		/* extent info count */
161613054c54SChao Yu 
161739a53e0cSJaegeuk Kim 	/* basic filesystem units */
161839a53e0cSJaegeuk Kim 	unsigned int log_sectors_per_block;	/* log2 sectors per block */
161939a53e0cSJaegeuk Kim 	unsigned int log_blocksize;		/* log2 block size */
162039a53e0cSJaegeuk Kim 	unsigned int blocksize;			/* block size */
162139a53e0cSJaegeuk Kim 	unsigned int root_ino_num;		/* root inode number*/
162239a53e0cSJaegeuk Kim 	unsigned int node_ino_num;		/* node inode number*/
162339a53e0cSJaegeuk Kim 	unsigned int meta_ino_num;		/* meta inode number*/
162439a53e0cSJaegeuk Kim 	unsigned int log_blocks_per_seg;	/* log2 blocks per segment */
162539a53e0cSJaegeuk Kim 	unsigned int blocks_per_seg;		/* blocks per segment */
162639a53e0cSJaegeuk Kim 	unsigned int segs_per_sec;		/* segments per section */
162739a53e0cSJaegeuk Kim 	unsigned int secs_per_zone;		/* sections per zone */
162839a53e0cSJaegeuk Kim 	unsigned int total_sections;		/* total section count */
162939a53e0cSJaegeuk Kim 	unsigned int total_node_count;		/* total node block count */
163039a53e0cSJaegeuk Kim 	unsigned int total_valid_node_count;	/* valid node block count */
1631ab9fa662SJaegeuk Kim 	int dir_level;				/* directory level */
1632f6df8f23SSheng Yong 	int readdir_ra;				/* readahead inode in readdir */
163310208567SJaegeuk Kim 	u64 max_io_bytes;			/* max io bytes to merge IOs */
163439a53e0cSJaegeuk Kim 
163539a53e0cSJaegeuk Kim 	block_t user_block_count;		/* # of user blocks */
163639a53e0cSJaegeuk Kim 	block_t total_valid_block_count;	/* # of valid blocks */
1637a66cdd98SJaegeuk Kim 	block_t discard_blks;			/* discard command candidats */
163839a53e0cSJaegeuk Kim 	block_t last_valid_block_count;		/* for recovery */
1639daeb433eSChao Yu 	block_t reserved_blocks;		/* configurable reserved blocks */
164080d42145SYunlong Song 	block_t current_reserved_blocks;	/* current reserved blocks */
1641daeb433eSChao Yu 
16424354994fSDaniel Rosenberg 	/* Additional tracking for no checkpoint mode */
16434354994fSDaniel Rosenberg 	block_t unusable_block_count;		/* # of blocks saved by last cp */
16444354994fSDaniel Rosenberg 
1645292c196aSChao Yu 	unsigned int nquota_files;		/* # of quota sysfile */
1646db6ec53bSJaegeuk Kim 	struct rw_semaphore quota_sem;		/* blocking cp for flags */
1647292c196aSChao Yu 
1648523be8a6SJaegeuk Kim 	/* # of pages, see count_type */
164935782b23SJaegeuk Kim 	atomic_t nr_pages[NR_COUNT_TYPE];
165041382ec4SJaegeuk Kim 	/* # of allocated blocks */
165141382ec4SJaegeuk Kim 	struct percpu_counter alloc_valid_block_count;
165239a53e0cSJaegeuk Kim 
1653687de7f1SJaegeuk Kim 	/* writeback control */
1654c29fd0c0SChao Yu 	atomic_t wb_sync_req[META];	/* count # of WB_SYNC threads */
1655687de7f1SJaegeuk Kim 
1656513c5f37SJaegeuk Kim 	/* valid inode count */
1657513c5f37SJaegeuk Kim 	struct percpu_counter total_valid_inode_count;
1658513c5f37SJaegeuk Kim 
165939a53e0cSJaegeuk Kim 	struct f2fs_mount_info mount_opt;	/* mount options */
166039a53e0cSJaegeuk Kim 
166139a53e0cSJaegeuk Kim 	/* for cleaning operations */
1662fb24fea7SChao Yu 	struct rw_semaphore gc_lock;		/*
1663fb24fea7SChao Yu 						 * semaphore for GC, avoid
1664fb24fea7SChao Yu 						 * race between GC and GC or CP
1665fb24fea7SChao Yu 						 */
166639a53e0cSJaegeuk Kim 	struct f2fs_gc_kthread	*gc_thread;	/* GC thread */
1667093749e2SChao Yu 	struct atgc_management am;		/* atgc management */
16685ec4e49fSJaegeuk Kim 	unsigned int cur_victim_sec;		/* current victim section num */
16695b0e9539SJaegeuk Kim 	unsigned int gc_mode;			/* current GC state */
1670e3080b01SChao Yu 	unsigned int next_victim_seg[2];	/* next segment in victim section */
16710e5e8111SDaeho Jeong 
16722ef79ecbSChao Yu 	/* for skip statistic */
1673677017d1SSahitya Tummala 	unsigned int atomic_files;		/* # of opened atomic file */
16742ef79ecbSChao Yu 	unsigned long long skipped_atomic_files[2];	/* FG_GC and BG_GC */
16756f8d4455SJaegeuk Kim 	unsigned long long skipped_gc_rwsem;		/* FG_GC only */
167639a53e0cSJaegeuk Kim 
16771ad71a27SJaegeuk Kim 	/* threshold for gc trials on pinned files */
16781ad71a27SJaegeuk Kim 	u64 gc_pin_file_threshold;
1679f5a53edcSJaegeuk Kim 	struct rw_semaphore pin_sem;
16801ad71a27SJaegeuk Kim 
1681b1c57c1cSJaegeuk Kim 	/* maximum # of trials to find a victim segment for SSR and GC */
1682b1c57c1cSJaegeuk Kim 	unsigned int max_victim_search;
1683e3080b01SChao Yu 	/* migration granularity of garbage collection, unit: segment */
1684e3080b01SChao Yu 	unsigned int migration_granularity;
1685b1c57c1cSJaegeuk Kim 
168639a53e0cSJaegeuk Kim 	/*
168739a53e0cSJaegeuk Kim 	 * for stat information.
168839a53e0cSJaegeuk Kim 	 * one is for the LFS mode, and the other is for the SSR mode.
168939a53e0cSJaegeuk Kim 	 */
169035b09d82SNamjae Jeon #ifdef CONFIG_F2FS_STAT_FS
169139a53e0cSJaegeuk Kim 	struct f2fs_stat_info *stat_info;	/* FS status information */
1692b63e7be5SChao Yu 	atomic_t meta_count[META_MAX];		/* # of meta blocks */
169339a53e0cSJaegeuk Kim 	unsigned int segment_count[2];		/* # of allocated segments */
169439a53e0cSJaegeuk Kim 	unsigned int block_count[2];		/* # of allocated blocks */
1695b9a2c252SChangman Lee 	atomic_t inplace_count;		/* # of inplace update */
16965b7ee374SChao Yu 	atomic64_t total_hit_ext;		/* # of lookup extent cache */
16975b7ee374SChao Yu 	atomic64_t read_hit_rbtree;		/* # of hit rbtree extent node */
16985b7ee374SChao Yu 	atomic64_t read_hit_largest;		/* # of hit largest extent node */
16995b7ee374SChao Yu 	atomic64_t read_hit_cached;		/* # of hit cached extent node */
1700d5e8f6c9SChao Yu 	atomic_t inline_xattr;			/* # of inline_xattr inodes */
170103e14d52SChao Yu 	atomic_t inline_inode;			/* # of inline_data inodes */
170203e14d52SChao Yu 	atomic_t inline_dir;			/* # of inline_dentry inodes */
17034c8ff709SChao Yu 	atomic_t compr_inode;			/* # of compressed inodes */
1704ae999bb9SDaeho Jeong 	atomic64_t compr_blocks;		/* # of compressed blocks */
1705648d50baSChao Yu 	atomic_t vw_cnt;			/* # of volatile writes */
170626a28a0cSJaegeuk Kim 	atomic_t max_aw_cnt;			/* max # of atomic writes */
1707648d50baSChao Yu 	atomic_t max_vw_cnt;			/* max # of volatile writes */
1708274bd9baSChao Yu 	unsigned int io_skip_bggc;		/* skip background gc for in-flight IO */
1709274bd9baSChao Yu 	unsigned int other_skip_bggc;		/* skip background gc for other reasons */
171033fbd510SChao Yu 	unsigned int ndirty_inode[NR_INODE_TYPE];	/* # of dirty inodes */
171135b09d82SNamjae Jeon #endif
171239a53e0cSJaegeuk Kim 	spinlock_t stat_lock;			/* lock for stat operations */
1713b59d0baeSNamjae Jeon 
1714b0af6d49SChao Yu 	/* For app/fs IO statistics */
1715b0af6d49SChao Yu 	spinlock_t iostat_lock;
17168b83ac81SChao Yu 	unsigned long long rw_iostat[NR_IO_TYPE];
17178b83ac81SChao Yu 	unsigned long long prev_rw_iostat[NR_IO_TYPE];
1718b0af6d49SChao Yu 	bool iostat_enable;
17192bc4bea3SDaeho Jeong 	unsigned long iostat_next_period;
17202bc4bea3SDaeho Jeong 	unsigned int iostat_period_ms;
1721b0af6d49SChao Yu 
1722da9953b7SJaegeuk Kim 	/* to attach REQ_META|REQ_FUA flags */
1723da9953b7SJaegeuk Kim 	unsigned int data_io_flag;
172432b6aba8SJaegeuk Kim 	unsigned int node_io_flag;
1725b59d0baeSNamjae Jeon 
1726b59d0baeSNamjae Jeon 	/* For sysfs suppport */
17275d4daa57SChao Yu 	struct kobject s_kobj;			/* /sys/fs/f2fs/<devname> */
1728b59d0baeSNamjae Jeon 	struct completion s_kobj_unregister;
17292658e50dSJaegeuk Kim 
17305d4daa57SChao Yu 	struct kobject s_stat_kobj;		/* /sys/fs/f2fs/<devname>/stat */
17315d4daa57SChao Yu 	struct completion s_stat_kobj_unregister;
17325d4daa57SChao Yu 
17334c89b53dSJaegeuk Kim 	struct kobject s_feature_list_kobj;		/* /sys/fs/f2fs/<devname>/feature_list */
17344c89b53dSJaegeuk Kim 	struct completion s_feature_list_kobj_unregister;
17354c89b53dSJaegeuk Kim 
17362658e50dSJaegeuk Kim 	/* For shrinker support */
17372658e50dSJaegeuk Kim 	struct list_head s_list;
17383c62be17SJaegeuk Kim 	int s_ndevs;				/* number of devices */
17393c62be17SJaegeuk Kim 	struct f2fs_dev_info *devs;		/* for device list */
17401228b482SChao Yu 	unsigned int dirty_device;		/* for checkpoint data flush */
17411228b482SChao Yu 	spinlock_t dev_lock;			/* protect dirty_device */
17422658e50dSJaegeuk Kim 	struct mutex umount_mutex;
17432658e50dSJaegeuk Kim 	unsigned int shrinker_run_no;
17448f1dbbbbSShuoran Liu 
17458f1dbbbbSShuoran Liu 	/* For write statistics */
17468f1dbbbbSShuoran Liu 	u64 sectors_written_start;
17478f1dbbbbSShuoran Liu 	u64 kbytes_written;
174843b6573bSKeith Mok 
174943b6573bSKeith Mok 	/* Reference to checksum algorithm driver via cryptoapi */
175043b6573bSKeith Mok 	struct crypto_shash *s_chksum_driver;
17511ecc0c5cSChao Yu 
1752704956ecSChao Yu 	/* Precomputed FS UUID checksum for seeding other checksums */
1753704956ecSChao Yu 	__u32 s_chksum_seed;
17544c8ff709SChao Yu 
17554c8ff709SChao Yu 	struct workqueue_struct *post_read_wq;	/* post read workqueue */
1756a999150fSChao Yu 
1757a999150fSChao Yu 	struct kmem_cache *inline_xattr_slab;	/* inline xattr entry */
1758a999150fSChao Yu 	unsigned int inline_xattr_slab_size;	/* default inline xattr slab size */
175931083031SChao Yu 
176007c6b593SDaeho Jeong 	/* For reclaimed segs statistics per each GC mode */
176107c6b593SDaeho Jeong 	unsigned int gc_segment_mode;		/* GC state for reclaimed segments */
176207c6b593SDaeho Jeong 	unsigned int gc_reclaimed_segs[MAX_GC_MODE];	/* Reclaimed segs for each mode */
176307c6b593SDaeho Jeong 
17640f6b56ecSDaeho Jeong 	unsigned long seq_file_ra_mul;		/* multiplier for ra_pages of seq. files in fadvise */
17650f6b56ecSDaeho Jeong 
176631083031SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
176731083031SChao Yu 	struct kmem_cache *page_array_slab;	/* page array entry */
176831083031SChao Yu 	unsigned int page_array_slab_size;	/* default page array slab size */
17695ac443e2SDaeho Jeong 
17705ac443e2SDaeho Jeong 	/* For runtime compression statistics */
17715ac443e2SDaeho Jeong 	u64 compr_written_block;
17725ac443e2SDaeho Jeong 	u64 compr_saved_block;
17735ac443e2SDaeho Jeong 	u32 compr_new_inode;
17746ce19affSChao Yu 
17756ce19affSChao Yu 	/* For compressed block cache */
17766ce19affSChao Yu 	struct inode *compress_inode;		/* cache compressed blocks */
17776ce19affSChao Yu 	unsigned int compress_percent;		/* cache page percentage */
17786ce19affSChao Yu 	unsigned int compress_watermark;	/* cache page watermark */
17796ce19affSChao Yu 	atomic_t compress_page_hit;		/* cache hit count */
178031083031SChao Yu #endif
178139a53e0cSJaegeuk Kim };
178239a53e0cSJaegeuk Kim 
178302b16d0aSChao Yu struct f2fs_private_dio {
178402b16d0aSChao Yu 	struct inode *inode;
178502b16d0aSChao Yu 	void *orig_private;
178602b16d0aSChao Yu 	bio_end_io_t *orig_end_io;
178702b16d0aSChao Yu 	bool write;
178802b16d0aSChao Yu };
178902b16d0aSChao Yu 
17901ecc0c5cSChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
1791c45d6002SChao Yu #define f2fs_show_injection_info(sbi, type)					\
1792c45d6002SChao Yu 	printk_ratelimited("%sF2FS-fs (%s) : inject %s in %s of %pS\n",	\
1793c45d6002SChao Yu 		KERN_INFO, sbi->sb->s_id,				\
1794c45d6002SChao Yu 		f2fs_fault_name[type],					\
179555523519SChao Yu 		__func__, __builtin_return_address(0))
17961ecc0c5cSChao Yu static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
17971ecc0c5cSChao Yu {
179863189b78SChao Yu 	struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
17991ecc0c5cSChao Yu 
18001ecc0c5cSChao Yu 	if (!ffi->inject_rate)
18011ecc0c5cSChao Yu 		return false;
18021ecc0c5cSChao Yu 
18031ecc0c5cSChao Yu 	if (!IS_FAULT_SET(ffi, type))
18041ecc0c5cSChao Yu 		return false;
18051ecc0c5cSChao Yu 
18061ecc0c5cSChao Yu 	atomic_inc(&ffi->inject_ops);
18071ecc0c5cSChao Yu 	if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
18081ecc0c5cSChao Yu 		atomic_set(&ffi->inject_ops, 0);
18091ecc0c5cSChao Yu 		return true;
18101ecc0c5cSChao Yu 	}
18111ecc0c5cSChao Yu 	return false;
18121ecc0c5cSChao Yu }
18137fa750a1SArnd Bergmann #else
1814c45d6002SChao Yu #define f2fs_show_injection_info(sbi, type) do { } while (0)
18157fa750a1SArnd Bergmann static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
18167fa750a1SArnd Bergmann {
18177fa750a1SArnd Bergmann 	return false;
18187fa750a1SArnd Bergmann }
18191ecc0c5cSChao Yu #endif
18201ecc0c5cSChao Yu 
18210916878dSDamien Le Moal /*
18220916878dSDamien Le Moal  * Test if the mounted volume is a multi-device volume.
18230916878dSDamien Le Moal  *   - For a single regular disk volume, sbi->s_ndevs is 0.
18240916878dSDamien Le Moal  *   - For a single zoned disk volume, sbi->s_ndevs is 1.
18250916878dSDamien Le Moal  *   - For a multi-device volume, sbi->s_ndevs is always 2 or more.
18260916878dSDamien Le Moal  */
18270916878dSDamien Le Moal static inline bool f2fs_is_multi_device(struct f2fs_sb_info *sbi)
18280916878dSDamien Le Moal {
18290916878dSDamien Le Moal 	return sbi->s_ndevs > 1;
18300916878dSDamien Le Moal }
18310916878dSDamien Le Moal 
18326beceb54SJaegeuk Kim static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
18336beceb54SJaegeuk Kim {
1834a7d10cf3SSahitya Tummala 	unsigned long now = jiffies;
1835a7d10cf3SSahitya Tummala 
1836a7d10cf3SSahitya Tummala 	sbi->last_time[type] = now;
1837a7d10cf3SSahitya Tummala 
1838a7d10cf3SSahitya Tummala 	/* DISCARD_TIME and GC_TIME are based on REQ_TIME */
1839a7d10cf3SSahitya Tummala 	if (type == REQ_TIME) {
1840a7d10cf3SSahitya Tummala 		sbi->last_time[DISCARD_TIME] = now;
1841a7d10cf3SSahitya Tummala 		sbi->last_time[GC_TIME] = now;
1842a7d10cf3SSahitya Tummala 	}
18436beceb54SJaegeuk Kim }
18446beceb54SJaegeuk Kim 
18456beceb54SJaegeuk Kim static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
18466beceb54SJaegeuk Kim {
18476bccfa19SArnd Bergmann 	unsigned long interval = sbi->interval_time[type] * HZ;
18486beceb54SJaegeuk Kim 
18496beceb54SJaegeuk Kim 	return time_after(jiffies, sbi->last_time[type] + interval);
18506beceb54SJaegeuk Kim }
18516beceb54SJaegeuk Kim 
1852a7d10cf3SSahitya Tummala static inline unsigned int f2fs_time_to_wait(struct f2fs_sb_info *sbi,
1853a7d10cf3SSahitya Tummala 						int type)
1854a7d10cf3SSahitya Tummala {
1855a7d10cf3SSahitya Tummala 	unsigned long interval = sbi->interval_time[type] * HZ;
1856a7d10cf3SSahitya Tummala 	unsigned int wait_ms = 0;
1857a7d10cf3SSahitya Tummala 	long delta;
1858a7d10cf3SSahitya Tummala 
1859a7d10cf3SSahitya Tummala 	delta = (sbi->last_time[type] + interval) - jiffies;
1860a7d10cf3SSahitya Tummala 	if (delta > 0)
1861a7d10cf3SSahitya Tummala 		wait_ms = jiffies_to_msecs(delta);
1862a7d10cf3SSahitya Tummala 
1863a7d10cf3SSahitya Tummala 	return wait_ms;
1864a7d10cf3SSahitya Tummala }
1865a7d10cf3SSahitya Tummala 
186639a53e0cSJaegeuk Kim /*
186739a53e0cSJaegeuk Kim  * Inline functions
186839a53e0cSJaegeuk Kim  */
1869416d2dbbSChao Yu static inline u32 __f2fs_crc32(struct f2fs_sb_info *sbi, u32 crc,
1870704956ecSChao Yu 			      const void *address, unsigned int length)
1871704956ecSChao Yu {
1872704956ecSChao Yu 	struct {
1873704956ecSChao Yu 		struct shash_desc shash;
1874704956ecSChao Yu 		char ctx[4];
1875704956ecSChao Yu 	} desc;
1876704956ecSChao Yu 	int err;
1877704956ecSChao Yu 
1878704956ecSChao Yu 	BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver) != sizeof(desc.ctx));
1879704956ecSChao Yu 
1880704956ecSChao Yu 	desc.shash.tfm = sbi->s_chksum_driver;
1881704956ecSChao Yu 	*(u32 *)desc.ctx = crc;
1882704956ecSChao Yu 
1883704956ecSChao Yu 	err = crypto_shash_update(&desc.shash, address, length);
1884704956ecSChao Yu 	BUG_ON(err);
1885704956ecSChao Yu 
1886704956ecSChao Yu 	return *(u32 *)desc.ctx;
1887704956ecSChao Yu }
1888704956ecSChao Yu 
1889416d2dbbSChao Yu static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
1890416d2dbbSChao Yu 			   unsigned int length)
1891416d2dbbSChao Yu {
1892416d2dbbSChao Yu 	return __f2fs_crc32(sbi, F2FS_SUPER_MAGIC, address, length);
1893416d2dbbSChao Yu }
1894416d2dbbSChao Yu 
1895416d2dbbSChao Yu static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
1896416d2dbbSChao Yu 				  void *buf, size_t buf_size)
1897416d2dbbSChao Yu {
1898416d2dbbSChao Yu 	return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
1899416d2dbbSChao Yu }
1900416d2dbbSChao Yu 
1901416d2dbbSChao Yu static inline u32 f2fs_chksum(struct f2fs_sb_info *sbi, u32 crc,
1902416d2dbbSChao Yu 			      const void *address, unsigned int length)
1903416d2dbbSChao Yu {
1904416d2dbbSChao Yu 	return __f2fs_crc32(sbi, crc, address, length);
1905416d2dbbSChao Yu }
1906416d2dbbSChao Yu 
190739a53e0cSJaegeuk Kim static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
190839a53e0cSJaegeuk Kim {
190939a53e0cSJaegeuk Kim 	return container_of(inode, struct f2fs_inode_info, vfs_inode);
191039a53e0cSJaegeuk Kim }
191139a53e0cSJaegeuk Kim 
191239a53e0cSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
191339a53e0cSJaegeuk Kim {
191439a53e0cSJaegeuk Kim 	return sb->s_fs_info;
191539a53e0cSJaegeuk Kim }
191639a53e0cSJaegeuk Kim 
19174081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
19184081363fSJaegeuk Kim {
19194081363fSJaegeuk Kim 	return F2FS_SB(inode->i_sb);
19204081363fSJaegeuk Kim }
19214081363fSJaegeuk Kim 
19224081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
19234081363fSJaegeuk Kim {
19244081363fSJaegeuk Kim 	return F2FS_I_SB(mapping->host);
19254081363fSJaegeuk Kim }
19264081363fSJaegeuk Kim 
19274081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
19284081363fSJaegeuk Kim {
19294969c06aSJaegeuk Kim 	return F2FS_M_SB(page_file_mapping(page));
19304081363fSJaegeuk Kim }
19314081363fSJaegeuk Kim 
193239a53e0cSJaegeuk Kim static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
193339a53e0cSJaegeuk Kim {
193439a53e0cSJaegeuk Kim 	return (struct f2fs_super_block *)(sbi->raw_super);
193539a53e0cSJaegeuk Kim }
193639a53e0cSJaegeuk Kim 
193739a53e0cSJaegeuk Kim static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
193839a53e0cSJaegeuk Kim {
193939a53e0cSJaegeuk Kim 	return (struct f2fs_checkpoint *)(sbi->ckpt);
194039a53e0cSJaegeuk Kim }
194139a53e0cSJaegeuk Kim 
194245590710SGu Zheng static inline struct f2fs_node *F2FS_NODE(struct page *page)
194345590710SGu Zheng {
194445590710SGu Zheng 	return (struct f2fs_node *)page_address(page);
194545590710SGu Zheng }
194645590710SGu Zheng 
194758bfaf44SJaegeuk Kim static inline struct f2fs_inode *F2FS_INODE(struct page *page)
194858bfaf44SJaegeuk Kim {
194958bfaf44SJaegeuk Kim 	return &((struct f2fs_node *)page_address(page))->i;
195058bfaf44SJaegeuk Kim }
195158bfaf44SJaegeuk Kim 
195239a53e0cSJaegeuk Kim static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
195339a53e0cSJaegeuk Kim {
195439a53e0cSJaegeuk Kim 	return (struct f2fs_nm_info *)(sbi->nm_info);
195539a53e0cSJaegeuk Kim }
195639a53e0cSJaegeuk Kim 
195739a53e0cSJaegeuk Kim static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
195839a53e0cSJaegeuk Kim {
195939a53e0cSJaegeuk Kim 	return (struct f2fs_sm_info *)(sbi->sm_info);
196039a53e0cSJaegeuk Kim }
196139a53e0cSJaegeuk Kim 
196239a53e0cSJaegeuk Kim static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
196339a53e0cSJaegeuk Kim {
196439a53e0cSJaegeuk Kim 	return (struct sit_info *)(SM_I(sbi)->sit_info);
196539a53e0cSJaegeuk Kim }
196639a53e0cSJaegeuk Kim 
196739a53e0cSJaegeuk Kim static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
196839a53e0cSJaegeuk Kim {
196939a53e0cSJaegeuk Kim 	return (struct free_segmap_info *)(SM_I(sbi)->free_info);
197039a53e0cSJaegeuk Kim }
197139a53e0cSJaegeuk Kim 
197239a53e0cSJaegeuk Kim static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
197339a53e0cSJaegeuk Kim {
197439a53e0cSJaegeuk Kim 	return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
197539a53e0cSJaegeuk Kim }
197639a53e0cSJaegeuk Kim 
19779df27d98SGu Zheng static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
19789df27d98SGu Zheng {
19799df27d98SGu Zheng 	return sbi->meta_inode->i_mapping;
19809df27d98SGu Zheng }
19819df27d98SGu Zheng 
19824ef51a8fSJaegeuk Kim static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
19834ef51a8fSJaegeuk Kim {
19844ef51a8fSJaegeuk Kim 	return sbi->node_inode->i_mapping;
19854ef51a8fSJaegeuk Kim }
19864ef51a8fSJaegeuk Kim 
1987caf0047eSChao Yu static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
198839a53e0cSJaegeuk Kim {
1989fadb2fb8SChao Yu 	return test_bit(type, &sbi->s_flag);
199039a53e0cSJaegeuk Kim }
199139a53e0cSJaegeuk Kim 
1992caf0047eSChao Yu static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
199339a53e0cSJaegeuk Kim {
1994fadb2fb8SChao Yu 	set_bit(type, &sbi->s_flag);
1995caf0047eSChao Yu }
1996caf0047eSChao Yu 
1997caf0047eSChao Yu static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
1998caf0047eSChao Yu {
1999fadb2fb8SChao Yu 	clear_bit(type, &sbi->s_flag);
200039a53e0cSJaegeuk Kim }
200139a53e0cSJaegeuk Kim 
2002d71b5564SJaegeuk Kim static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
2003d71b5564SJaegeuk Kim {
2004d71b5564SJaegeuk Kim 	return le64_to_cpu(cp->checkpoint_ver);
2005d71b5564SJaegeuk Kim }
2006d71b5564SJaegeuk Kim 
2007ea676733SJaegeuk Kim static inline unsigned long f2fs_qf_ino(struct super_block *sb, int type)
2008ea676733SJaegeuk Kim {
2009ea676733SJaegeuk Kim 	if (type < F2FS_MAX_QUOTAS)
2010ea676733SJaegeuk Kim 		return le32_to_cpu(F2FS_SB(sb)->raw_super->qf_ino[type]);
2011ea676733SJaegeuk Kim 	return 0;
2012ea676733SJaegeuk Kim }
2013ea676733SJaegeuk Kim 
2014ced2c7eaSKinglong Mee static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
2015ced2c7eaSKinglong Mee {
2016ced2c7eaSKinglong Mee 	size_t crc_offset = le32_to_cpu(cp->checksum_offset);
2017ced2c7eaSKinglong Mee 	return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
2018ced2c7eaSKinglong Mee }
2019ced2c7eaSKinglong Mee 
2020aaec2b1dSChao Yu static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
202125ca923bSJaegeuk Kim {
202225ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
2023aaec2b1dSChao Yu 
202425ca923bSJaegeuk Kim 	return ckpt_flags & f;
202525ca923bSJaegeuk Kim }
202625ca923bSJaegeuk Kim 
2027aaec2b1dSChao Yu static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
202825ca923bSJaegeuk Kim {
2029aaec2b1dSChao Yu 	return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
2030aaec2b1dSChao Yu }
2031aaec2b1dSChao Yu 
2032aaec2b1dSChao Yu static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
2033aaec2b1dSChao Yu {
2034aaec2b1dSChao Yu 	unsigned int ckpt_flags;
2035aaec2b1dSChao Yu 
2036aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
203725ca923bSJaegeuk Kim 	ckpt_flags |= f;
203825ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
203925ca923bSJaegeuk Kim }
204025ca923bSJaegeuk Kim 
2041aaec2b1dSChao Yu static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
204225ca923bSJaegeuk Kim {
2043d1aa2453SChao Yu 	unsigned long flags;
2044d1aa2453SChao Yu 
2045d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
2046aaec2b1dSChao Yu 	__set_ckpt_flags(F2FS_CKPT(sbi), f);
2047d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
2048aaec2b1dSChao Yu }
2049aaec2b1dSChao Yu 
2050aaec2b1dSChao Yu static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
2051aaec2b1dSChao Yu {
2052aaec2b1dSChao Yu 	unsigned int ckpt_flags;
2053aaec2b1dSChao Yu 
2054aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
205525ca923bSJaegeuk Kim 	ckpt_flags &= (~f);
205625ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
205725ca923bSJaegeuk Kim }
205825ca923bSJaegeuk Kim 
2059aaec2b1dSChao Yu static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
2060aaec2b1dSChao Yu {
2061d1aa2453SChao Yu 	unsigned long flags;
2062d1aa2453SChao Yu 
2063d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
2064aaec2b1dSChao Yu 	__clear_ckpt_flags(F2FS_CKPT(sbi), f);
2065d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
2066aaec2b1dSChao Yu }
2067aaec2b1dSChao Yu 
206822ad0b6aSJaegeuk Kim static inline void disable_nat_bits(struct f2fs_sb_info *sbi, bool lock)
206922ad0b6aSJaegeuk Kim {
2070d1aa2453SChao Yu 	unsigned long flags;
20710921835cSChao Yu 	unsigned char *nat_bits;
2072d1aa2453SChao Yu 
2073a742fd41SJaegeuk Kim 	/*
2074a742fd41SJaegeuk Kim 	 * In order to re-enable nat_bits we need to call fsck.f2fs by
2075a742fd41SJaegeuk Kim 	 * set_sbi_flag(sbi, SBI_NEED_FSCK). But it may give huge cost,
2076a742fd41SJaegeuk Kim 	 * so let's rely on regular fsck or unclean shutdown.
2077a742fd41SJaegeuk Kim 	 */
207822ad0b6aSJaegeuk Kim 
207922ad0b6aSJaegeuk Kim 	if (lock)
2080d1aa2453SChao Yu 		spin_lock_irqsave(&sbi->cp_lock, flags);
208122ad0b6aSJaegeuk Kim 	__clear_ckpt_flags(F2FS_CKPT(sbi), CP_NAT_BITS_FLAG);
20820921835cSChao Yu 	nat_bits = NM_I(sbi)->nat_bits;
208322ad0b6aSJaegeuk Kim 	NM_I(sbi)->nat_bits = NULL;
208422ad0b6aSJaegeuk Kim 	if (lock)
2085d1aa2453SChao Yu 		spin_unlock_irqrestore(&sbi->cp_lock, flags);
20860921835cSChao Yu 
20870921835cSChao Yu 	kvfree(nat_bits);
208822ad0b6aSJaegeuk Kim }
208922ad0b6aSJaegeuk Kim 
209022ad0b6aSJaegeuk Kim static inline bool enabled_nat_bits(struct f2fs_sb_info *sbi,
209122ad0b6aSJaegeuk Kim 					struct cp_control *cpc)
209222ad0b6aSJaegeuk Kim {
209322ad0b6aSJaegeuk Kim 	bool set = is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
209422ad0b6aSJaegeuk Kim 
2095c473f1a9SChao Yu 	return (cpc) ? (cpc->reason & CP_UMOUNT) && set : set;
209622ad0b6aSJaegeuk Kim }
209722ad0b6aSJaegeuk Kim 
2098e479556bSGu Zheng static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
209939a53e0cSJaegeuk Kim {
2100b873b798SJaegeuk Kim 	down_read(&sbi->cp_rwsem);
210139a53e0cSJaegeuk Kim }
210239a53e0cSJaegeuk Kim 
2103cc15620bSJaegeuk Kim static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi)
2104cc15620bSJaegeuk Kim {
2105cc15620bSJaegeuk Kim 	return down_read_trylock(&sbi->cp_rwsem);
2106cc15620bSJaegeuk Kim }
2107cc15620bSJaegeuk Kim 
2108e479556bSGu Zheng static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
210939a53e0cSJaegeuk Kim {
2110b873b798SJaegeuk Kim 	up_read(&sbi->cp_rwsem);
211139936837SJaegeuk Kim }
211239936837SJaegeuk Kim 
2113e479556bSGu Zheng static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
211439936837SJaegeuk Kim {
2115b873b798SJaegeuk Kim 	down_write(&sbi->cp_rwsem);
211639936837SJaegeuk Kim }
211739936837SJaegeuk Kim 
2118e479556bSGu Zheng static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
211939936837SJaegeuk Kim {
2120b873b798SJaegeuk Kim 	up_write(&sbi->cp_rwsem);
212139a53e0cSJaegeuk Kim }
212239a53e0cSJaegeuk Kim 
2123119ee914SJaegeuk Kim static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
2124119ee914SJaegeuk Kim {
2125119ee914SJaegeuk Kim 	int reason = CP_SYNC;
2126119ee914SJaegeuk Kim 
2127119ee914SJaegeuk Kim 	if (test_opt(sbi, FASTBOOT))
2128119ee914SJaegeuk Kim 		reason = CP_FASTBOOT;
2129119ee914SJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
2130119ee914SJaegeuk Kim 		reason = CP_UMOUNT;
2131119ee914SJaegeuk Kim 	return reason;
2132119ee914SJaegeuk Kim }
2133119ee914SJaegeuk Kim 
2134119ee914SJaegeuk Kim static inline bool __remain_node_summaries(int reason)
2135119ee914SJaegeuk Kim {
2136c473f1a9SChao Yu 	return (reason & (CP_UMOUNT | CP_FASTBOOT));
2137119ee914SJaegeuk Kim }
2138119ee914SJaegeuk Kim 
2139119ee914SJaegeuk Kim static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
2140119ee914SJaegeuk Kim {
2141aaec2b1dSChao Yu 	return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
2142aaec2b1dSChao Yu 			is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
2143119ee914SJaegeuk Kim }
2144119ee914SJaegeuk Kim 
214539a53e0cSJaegeuk Kim /*
214639a53e0cSJaegeuk Kim  * Check whether the inode has blocks or not
214739a53e0cSJaegeuk Kim  */
214839a53e0cSJaegeuk Kim static inline int F2FS_HAS_BLOCKS(struct inode *inode)
214939a53e0cSJaegeuk Kim {
21500eb0adadSChao Yu 	block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
21510eb0adadSChao Yu 
2152000519f2SChao Yu 	return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block;
215339a53e0cSJaegeuk Kim }
215439a53e0cSJaegeuk Kim 
21554bc8e9bcSChao Yu static inline bool f2fs_has_xattr_block(unsigned int ofs)
21564bc8e9bcSChao Yu {
21574bc8e9bcSChao Yu 	return ofs == XATTR_NODE_OFFSET;
21584bc8e9bcSChao Yu }
21594bc8e9bcSChao Yu 
2160d8a9a229SJaegeuk Kim static inline bool __allow_reserved_blocks(struct f2fs_sb_info *sbi,
2161a90a0884SJaegeuk Kim 					struct inode *inode, bool cap)
21627c2e5963SJaegeuk Kim {
2163d8a9a229SJaegeuk Kim 	if (!inode)
2164d8a9a229SJaegeuk Kim 		return true;
21657c2e5963SJaegeuk Kim 	if (!test_opt(sbi, RESERVE_ROOT))
21667c2e5963SJaegeuk Kim 		return false;
2167d8a9a229SJaegeuk Kim 	if (IS_NOQUOTA(inode))
2168d8a9a229SJaegeuk Kim 		return true;
216963189b78SChao Yu 	if (uid_eq(F2FS_OPTION(sbi).s_resuid, current_fsuid()))
21707c2e5963SJaegeuk Kim 		return true;
217163189b78SChao Yu 	if (!gid_eq(F2FS_OPTION(sbi).s_resgid, GLOBAL_ROOT_GID) &&
217263189b78SChao Yu 					in_group_p(F2FS_OPTION(sbi).s_resgid))
21737c2e5963SJaegeuk Kim 		return true;
2174a90a0884SJaegeuk Kim 	if (cap && capable(CAP_SYS_RESOURCE))
2175162b27aeSJaegeuk Kim 		return true;
21767c2e5963SJaegeuk Kim 	return false;
21777c2e5963SJaegeuk Kim }
21787c2e5963SJaegeuk Kim 
21790abd675eSChao Yu static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool);
21800abd675eSChao Yu static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
218146008c6dSChao Yu 				 struct inode *inode, blkcnt_t *count)
218239a53e0cSJaegeuk Kim {
21830abd675eSChao Yu 	blkcnt_t diff = 0, release = 0;
2184daeb433eSChao Yu 	block_t avail_user_block_count;
21850abd675eSChao Yu 	int ret;
21860abd675eSChao Yu 
21870abd675eSChao Yu 	ret = dquot_reserve_block(inode, *count);
21880abd675eSChao Yu 	if (ret)
21890abd675eSChao Yu 		return ret;
2190dd11a5dfSJaegeuk Kim 
219155523519SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
2192c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_BLOCK);
21930abd675eSChao Yu 		release = *count;
21949ea2f0beSChao Yu 		goto release_quota;
219555523519SChao Yu 	}
21967fa750a1SArnd Bergmann 
2197dd11a5dfSJaegeuk Kim 	/*
2198dd11a5dfSJaegeuk Kim 	 * let's increase this in prior to actual block count change in order
2199dd11a5dfSJaegeuk Kim 	 * for f2fs_sync_file to avoid data races when deciding checkpoint.
2200dd11a5dfSJaegeuk Kim 	 */
2201dd11a5dfSJaegeuk Kim 	percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
220239a53e0cSJaegeuk Kim 
220339a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
22042555a2d5SJaegeuk Kim 	sbi->total_valid_block_count += (block_t)(*count);
220580d42145SYunlong Song 	avail_user_block_count = sbi->user_block_count -
220680d42145SYunlong Song 					sbi->current_reserved_blocks;
22077e65be49SJaegeuk Kim 
2208a90a0884SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode, true))
220963189b78SChao Yu 		avail_user_block_count -= F2FS_OPTION(sbi).root_reserved_blocks;
2210a4c3ecaaSDaniel Rosenberg 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2211a4c3ecaaSDaniel Rosenberg 		if (avail_user_block_count > sbi->unusable_block_count)
22124354994fSDaniel Rosenberg 			avail_user_block_count -= sbi->unusable_block_count;
2213a4c3ecaaSDaniel Rosenberg 		else
2214a4c3ecaaSDaniel Rosenberg 			avail_user_block_count = 0;
2215a4c3ecaaSDaniel Rosenberg 	}
2216daeb433eSChao Yu 	if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
2217daeb433eSChao Yu 		diff = sbi->total_valid_block_count - avail_user_block_count;
22187e65be49SJaegeuk Kim 		if (diff > *count)
22197e65be49SJaegeuk Kim 			diff = *count;
2220dd11a5dfSJaegeuk Kim 		*count -= diff;
22210abd675eSChao Yu 		release = diff;
22227e65be49SJaegeuk Kim 		sbi->total_valid_block_count -= diff;
222346008c6dSChao Yu 		if (!*count) {
222439a53e0cSJaegeuk Kim 			spin_unlock(&sbi->stat_lock);
22250abd675eSChao Yu 			goto enospc;
222639a53e0cSJaegeuk Kim 		}
222746008c6dSChao Yu 	}
222839a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
222941382ec4SJaegeuk Kim 
223036b877afSDaniel Rosenberg 	if (unlikely(release)) {
223136b877afSDaniel Rosenberg 		percpu_counter_sub(&sbi->alloc_valid_block_count, release);
22320abd675eSChao Yu 		dquot_release_reservation_block(inode, release);
223336b877afSDaniel Rosenberg 	}
22340abd675eSChao Yu 	f2fs_i_blocks_write(inode, *count, true, true);
22350abd675eSChao Yu 	return 0;
22360abd675eSChao Yu 
22370abd675eSChao Yu enospc:
223836b877afSDaniel Rosenberg 	percpu_counter_sub(&sbi->alloc_valid_block_count, release);
22399ea2f0beSChao Yu release_quota:
22400abd675eSChao Yu 	dquot_release_reservation_block(inode, release);
22410abd675eSChao Yu 	return -ENOSPC;
224239a53e0cSJaegeuk Kim }
224339a53e0cSJaegeuk Kim 
2244dcbb4c10SJoe Perches __printf(2, 3)
2245dcbb4c10SJoe Perches void f2fs_printk(struct f2fs_sb_info *sbi, const char *fmt, ...);
2246dcbb4c10SJoe Perches 
2247dcbb4c10SJoe Perches #define f2fs_err(sbi, fmt, ...)						\
2248dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_ERR fmt, ##__VA_ARGS__)
2249dcbb4c10SJoe Perches #define f2fs_warn(sbi, fmt, ...)					\
2250dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_WARNING fmt, ##__VA_ARGS__)
2251dcbb4c10SJoe Perches #define f2fs_notice(sbi, fmt, ...)					\
2252dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_NOTICE fmt, ##__VA_ARGS__)
2253dcbb4c10SJoe Perches #define f2fs_info(sbi, fmt, ...)					\
2254dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_INFO fmt, ##__VA_ARGS__)
2255dcbb4c10SJoe Perches #define f2fs_debug(sbi, fmt, ...)					\
2256dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_DEBUG fmt, ##__VA_ARGS__)
2257dcbb4c10SJoe Perches 
2258da19b0dcSGu Zheng static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
225939a53e0cSJaegeuk Kim 						struct inode *inode,
22600eb0adadSChao Yu 						block_t count)
226139a53e0cSJaegeuk Kim {
22620eb0adadSChao Yu 	blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK;
22630eb0adadSChao Yu 
226439a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
22659850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
226639a53e0cSJaegeuk Kim 	sbi->total_valid_block_count -= (block_t)count;
226780d42145SYunlong Song 	if (sbi->reserved_blocks &&
226880d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
226980d42145SYunlong Song 		sbi->current_reserved_blocks = min(sbi->reserved_blocks,
227080d42145SYunlong Song 					sbi->current_reserved_blocks + count);
227139a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
22725e159cd3SChao Yu 	if (unlikely(inode->i_blocks < sectors)) {
2273dcbb4c10SJoe Perches 		f2fs_warn(sbi, "Inconsistent i_blocks, ino:%lu, iblocks:%llu, sectors:%llu",
22745e159cd3SChao Yu 			  inode->i_ino,
22755e159cd3SChao Yu 			  (unsigned long long)inode->i_blocks,
22765e159cd3SChao Yu 			  (unsigned long long)sectors);
22775e159cd3SChao Yu 		set_sbi_flag(sbi, SBI_NEED_FSCK);
22785e159cd3SChao Yu 		return;
22795e159cd3SChao Yu 	}
22800abd675eSChao Yu 	f2fs_i_blocks_write(inode, count, false, true);
228139a53e0cSJaegeuk Kim }
228239a53e0cSJaegeuk Kim 
228339a53e0cSJaegeuk Kim static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
228439a53e0cSJaegeuk Kim {
228535782b23SJaegeuk Kim 	atomic_inc(&sbi->nr_pages[count_type]);
22867c4abcbeSChao Yu 
228720109873SChao Yu 	if (count_type == F2FS_DIRTY_DENTS ||
228820109873SChao Yu 			count_type == F2FS_DIRTY_NODES ||
228920109873SChao Yu 			count_type == F2FS_DIRTY_META ||
229020109873SChao Yu 			count_type == F2FS_DIRTY_QDATA ||
229120109873SChao Yu 			count_type == F2FS_DIRTY_IMETA)
2292caf0047eSChao Yu 		set_sbi_flag(sbi, SBI_IS_DIRTY);
229339a53e0cSJaegeuk Kim }
229439a53e0cSJaegeuk Kim 
2295a7ffdbe2SJaegeuk Kim static inline void inode_inc_dirty_pages(struct inode *inode)
229639a53e0cSJaegeuk Kim {
2297204706c7SJaegeuk Kim 	atomic_inc(&F2FS_I(inode)->dirty_pages);
2298c227f912SChao Yu 	inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
2299c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
23002c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
23012c8a4a28SJaegeuk Kim 		inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
230239a53e0cSJaegeuk Kim }
230339a53e0cSJaegeuk Kim 
230439a53e0cSJaegeuk Kim static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
230539a53e0cSJaegeuk Kim {
230635782b23SJaegeuk Kim 	atomic_dec(&sbi->nr_pages[count_type]);
230739a53e0cSJaegeuk Kim }
230839a53e0cSJaegeuk Kim 
2309a7ffdbe2SJaegeuk Kim static inline void inode_dec_dirty_pages(struct inode *inode)
231039a53e0cSJaegeuk Kim {
23115ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
23125ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
23131fe54f9dSJaegeuk Kim 		return;
23141fe54f9dSJaegeuk Kim 
2315204706c7SJaegeuk Kim 	atomic_dec(&F2FS_I(inode)->dirty_pages);
2316c227f912SChao Yu 	dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
2317c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
23182c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
23192c8a4a28SJaegeuk Kim 		dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
232039a53e0cSJaegeuk Kim }
232139a53e0cSJaegeuk Kim 
2322523be8a6SJaegeuk Kim static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
232339a53e0cSJaegeuk Kim {
232435782b23SJaegeuk Kim 	return atomic_read(&sbi->nr_pages[count_type]);
232539a53e0cSJaegeuk Kim }
232639a53e0cSJaegeuk Kim 
2327204706c7SJaegeuk Kim static inline int get_dirty_pages(struct inode *inode)
2328f8b2c1f9SJaegeuk Kim {
2329204706c7SJaegeuk Kim 	return atomic_read(&F2FS_I(inode)->dirty_pages);
2330f8b2c1f9SJaegeuk Kim }
2331f8b2c1f9SJaegeuk Kim 
23325ac206cfSNamjae Jeon static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
23335ac206cfSNamjae Jeon {
23343519e3f9SChao Yu 	unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
2335523be8a6SJaegeuk Kim 	unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
2336523be8a6SJaegeuk Kim 						sbi->log_blocks_per_seg;
2337523be8a6SJaegeuk Kim 
2338523be8a6SJaegeuk Kim 	return segs / sbi->segs_per_sec;
23395ac206cfSNamjae Jeon }
23405ac206cfSNamjae Jeon 
234139a53e0cSJaegeuk Kim static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
234239a53e0cSJaegeuk Kim {
23438b8343faSJaegeuk Kim 	return sbi->total_valid_block_count;
234439a53e0cSJaegeuk Kim }
234539a53e0cSJaegeuk Kim 
2346f83a2584SYunlei He static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
2347f83a2584SYunlei He {
2348f83a2584SYunlei He 	return sbi->discard_blks;
2349f83a2584SYunlei He }
2350f83a2584SYunlei He 
235139a53e0cSJaegeuk Kim static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
235239a53e0cSJaegeuk Kim {
235339a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
235439a53e0cSJaegeuk Kim 
235539a53e0cSJaegeuk Kim 	/* return NAT or SIT bitmap */
235639a53e0cSJaegeuk Kim 	if (flag == NAT_BITMAP)
235739a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
235839a53e0cSJaegeuk Kim 	else if (flag == SIT_BITMAP)
235939a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
236039a53e0cSJaegeuk Kim 
236139a53e0cSJaegeuk Kim 	return 0;
236239a53e0cSJaegeuk Kim }
236339a53e0cSJaegeuk Kim 
236455141486SWanpeng Li static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
236555141486SWanpeng Li {
236655141486SWanpeng Li 	return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
236755141486SWanpeng Li }
236855141486SWanpeng Li 
236939a53e0cSJaegeuk Kim static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
237039a53e0cSJaegeuk Kim {
237139a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
23724260c406SGustavo A. R. Silva 	void *tmp_ptr = &ckpt->sit_nat_version_bitmap;
23731dbe4152SChangman Lee 	int offset;
23741dbe4152SChangman Lee 
2375199bc3feSChao Yu 	if (is_set_ckpt_flags(sbi, CP_LARGE_NAT_BITMAP_FLAG)) {
2376199bc3feSChao Yu 		offset = (flag == SIT_BITMAP) ?
2377199bc3feSChao Yu 			le32_to_cpu(ckpt->nat_ver_bitmap_bytesize) : 0;
2378b471eb99SChao Yu 		/*
2379b471eb99SChao Yu 		 * if large_nat_bitmap feature is enabled, leave checksum
2380b471eb99SChao Yu 		 * protection for all nat/sit bitmaps.
2381b471eb99SChao Yu 		 */
23824260c406SGustavo A. R. Silva 		return tmp_ptr + offset + sizeof(__le32);
2383199bc3feSChao Yu 	}
2384199bc3feSChao Yu 
238555141486SWanpeng Li 	if (__cp_payload(sbi) > 0) {
23861dbe4152SChangman Lee 		if (flag == NAT_BITMAP)
23871dbe4152SChangman Lee 			return &ckpt->sit_nat_version_bitmap;
23881dbe4152SChangman Lee 		else
238965b85cccSJaegeuk Kim 			return (unsigned char *)ckpt + F2FS_BLKSIZE;
23901dbe4152SChangman Lee 	} else {
23911dbe4152SChangman Lee 		offset = (flag == NAT_BITMAP) ?
239225ca923bSJaegeuk Kim 			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
23934260c406SGustavo A. R. Silva 		return tmp_ptr + offset;
239439a53e0cSJaegeuk Kim 	}
23951dbe4152SChangman Lee }
239639a53e0cSJaegeuk Kim 
239739a53e0cSJaegeuk Kim static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
239839a53e0cSJaegeuk Kim {
23998508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
240039a53e0cSJaegeuk Kim 
24018508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 2)
240239a53e0cSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
240339a53e0cSJaegeuk Kim 	return start_addr;
240439a53e0cSJaegeuk Kim }
240539a53e0cSJaegeuk Kim 
24068508e44aSJaegeuk Kim static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
24078508e44aSJaegeuk Kim {
24088508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
24098508e44aSJaegeuk Kim 
24108508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 1)
24118508e44aSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
24128508e44aSJaegeuk Kim 	return start_addr;
24138508e44aSJaegeuk Kim }
24148508e44aSJaegeuk Kim 
24158508e44aSJaegeuk Kim static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
24168508e44aSJaegeuk Kim {
24178508e44aSJaegeuk Kim 	sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
24188508e44aSJaegeuk Kim }
24198508e44aSJaegeuk Kim 
242039a53e0cSJaegeuk Kim static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
242139a53e0cSJaegeuk Kim {
242239a53e0cSJaegeuk Kim 	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
242339a53e0cSJaegeuk Kim }
242439a53e0cSJaegeuk Kim 
24250abd675eSChao Yu static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
2426000519f2SChao Yu 					struct inode *inode, bool is_inode)
242739a53e0cSJaegeuk Kim {
242839a53e0cSJaegeuk Kim 	block_t	valid_block_count;
2429a4c3ecaaSDaniel Rosenberg 	unsigned int valid_node_count, user_block_count;
2430af033b2aSChao Yu 	int err;
24310abd675eSChao Yu 
2432af033b2aSChao Yu 	if (is_inode) {
2433af033b2aSChao Yu 		if (inode) {
2434af033b2aSChao Yu 			err = dquot_alloc_inode(inode);
2435af033b2aSChao Yu 			if (err)
2436af033b2aSChao Yu 				return err;
2437af033b2aSChao Yu 		}
2438af033b2aSChao Yu 	} else {
2439af033b2aSChao Yu 		err = dquot_reserve_block(inode, 1);
2440af033b2aSChao Yu 		if (err)
2441af033b2aSChao Yu 			return err;
24420abd675eSChao Yu 	}
244339a53e0cSJaegeuk Kim 
2444812c6056SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
2445c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_BLOCK);
2446812c6056SChao Yu 		goto enospc;
2447812c6056SChao Yu 	}
2448812c6056SChao Yu 
244939a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
245039a53e0cSJaegeuk Kim 
24517e65be49SJaegeuk Kim 	valid_block_count = sbi->total_valid_block_count +
24527e65be49SJaegeuk Kim 					sbi->current_reserved_blocks + 1;
24537e65be49SJaegeuk Kim 
2454a90a0884SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode, false))
245563189b78SChao Yu 		valid_block_count += F2FS_OPTION(sbi).root_reserved_blocks;
2456a4c3ecaaSDaniel Rosenberg 	user_block_count = sbi->user_block_count;
24574354994fSDaniel Rosenberg 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2458a4c3ecaaSDaniel Rosenberg 		user_block_count -= sbi->unusable_block_count;
24597e65be49SJaegeuk Kim 
2460a4c3ecaaSDaniel Rosenberg 	if (unlikely(valid_block_count > user_block_count)) {
246139a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
24620abd675eSChao Yu 		goto enospc;
246339a53e0cSJaegeuk Kim 	}
246439a53e0cSJaegeuk Kim 
2465ef86d709SGu Zheng 	valid_node_count = sbi->total_valid_node_count + 1;
2466cfb271d4SChao Yu 	if (unlikely(valid_node_count > sbi->total_node_count)) {
246739a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
24680abd675eSChao Yu 		goto enospc;
246939a53e0cSJaegeuk Kim 	}
247039a53e0cSJaegeuk Kim 
2471ef86d709SGu Zheng 	sbi->total_valid_node_count++;
2472ef86d709SGu Zheng 	sbi->total_valid_block_count++;
247339a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
247439a53e0cSJaegeuk Kim 
24750abd675eSChao Yu 	if (inode) {
24760abd675eSChao Yu 		if (is_inode)
24770abd675eSChao Yu 			f2fs_mark_inode_dirty_sync(inode, true);
24780abd675eSChao Yu 		else
24790abd675eSChao Yu 			f2fs_i_blocks_write(inode, 1, true, true);
24800abd675eSChao Yu 	}
24810abd675eSChao Yu 
248241382ec4SJaegeuk Kim 	percpu_counter_inc(&sbi->alloc_valid_block_count);
24830abd675eSChao Yu 	return 0;
24840abd675eSChao Yu 
24850abd675eSChao Yu enospc:
2486af033b2aSChao Yu 	if (is_inode) {
2487af033b2aSChao Yu 		if (inode)
2488af033b2aSChao Yu 			dquot_free_inode(inode);
2489af033b2aSChao Yu 	} else {
24900abd675eSChao Yu 		dquot_release_reservation_block(inode, 1);
2491af033b2aSChao Yu 	}
24920abd675eSChao Yu 	return -ENOSPC;
249339a53e0cSJaegeuk Kim }
249439a53e0cSJaegeuk Kim 
249539a53e0cSJaegeuk Kim static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
2496000519f2SChao Yu 					struct inode *inode, bool is_inode)
249739a53e0cSJaegeuk Kim {
249839a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
249939a53e0cSJaegeuk Kim 
25009850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_block_count);
25019850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_node_count);
250239a53e0cSJaegeuk Kim 
2503ef86d709SGu Zheng 	sbi->total_valid_node_count--;
2504ef86d709SGu Zheng 	sbi->total_valid_block_count--;
250580d42145SYunlong Song 	if (sbi->reserved_blocks &&
250680d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
250780d42145SYunlong Song 		sbi->current_reserved_blocks++;
250839a53e0cSJaegeuk Kim 
250939a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
25100abd675eSChao Yu 
2511ea6d7e72SChao Yu 	if (is_inode) {
2512af033b2aSChao Yu 		dquot_free_inode(inode);
2513ea6d7e72SChao Yu 	} else {
2514ea6d7e72SChao Yu 		if (unlikely(inode->i_blocks == 0)) {
2515097a7686SChao Yu 			f2fs_warn(sbi, "dec_valid_node_count: inconsistent i_blocks, ino:%lu, iblocks:%llu",
2516ea6d7e72SChao Yu 				  inode->i_ino,
2517ea6d7e72SChao Yu 				  (unsigned long long)inode->i_blocks);
2518ea6d7e72SChao Yu 			set_sbi_flag(sbi, SBI_NEED_FSCK);
2519ea6d7e72SChao Yu 			return;
2520ea6d7e72SChao Yu 		}
25210abd675eSChao Yu 		f2fs_i_blocks_write(inode, 1, false, true);
252239a53e0cSJaegeuk Kim 	}
2523ea6d7e72SChao Yu }
252439a53e0cSJaegeuk Kim 
252539a53e0cSJaegeuk Kim static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
252639a53e0cSJaegeuk Kim {
25278b8343faSJaegeuk Kim 	return sbi->total_valid_node_count;
252839a53e0cSJaegeuk Kim }
252939a53e0cSJaegeuk Kim 
253039a53e0cSJaegeuk Kim static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
253139a53e0cSJaegeuk Kim {
2532513c5f37SJaegeuk Kim 	percpu_counter_inc(&sbi->total_valid_inode_count);
253339a53e0cSJaegeuk Kim }
253439a53e0cSJaegeuk Kim 
25350e80220aSJaegeuk Kim static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
253639a53e0cSJaegeuk Kim {
2537513c5f37SJaegeuk Kim 	percpu_counter_dec(&sbi->total_valid_inode_count);
253839a53e0cSJaegeuk Kim }
253939a53e0cSJaegeuk Kim 
2540513c5f37SJaegeuk Kim static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
254139a53e0cSJaegeuk Kim {
2542513c5f37SJaegeuk Kim 	return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
254339a53e0cSJaegeuk Kim }
254439a53e0cSJaegeuk Kim 
2545a56c7c6fSJaegeuk Kim static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
2546a56c7c6fSJaegeuk Kim 						pgoff_t index, bool for_write)
2547a56c7c6fSJaegeuk Kim {
254882cf4f13SChao Yu 	struct page *page;
2549cac5a3d8SDongOh Shin 
25507fa750a1SArnd Bergmann 	if (IS_ENABLED(CONFIG_F2FS_FAULT_INJECTION)) {
255182cf4f13SChao Yu 		if (!for_write)
255282cf4f13SChao Yu 			page = find_get_page_flags(mapping, index,
255382cf4f13SChao Yu 							FGP_LOCK | FGP_ACCESSED);
255482cf4f13SChao Yu 		else
255582cf4f13SChao Yu 			page = find_lock_page(mapping, index);
2556c41f3cc3SJaegeuk Kim 		if (page)
2557c41f3cc3SJaegeuk Kim 			return page;
2558c41f3cc3SJaegeuk Kim 
255955523519SChao Yu 		if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) {
2560c45d6002SChao Yu 			f2fs_show_injection_info(F2FS_M_SB(mapping),
2561c45d6002SChao Yu 							FAULT_PAGE_ALLOC);
2562c41f3cc3SJaegeuk Kim 			return NULL;
256355523519SChao Yu 		}
25647fa750a1SArnd Bergmann 	}
25657fa750a1SArnd Bergmann 
2566a56c7c6fSJaegeuk Kim 	if (!for_write)
2567a56c7c6fSJaegeuk Kim 		return grab_cache_page(mapping, index);
2568a56c7c6fSJaegeuk Kim 	return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
2569a56c7c6fSJaegeuk Kim }
2570a56c7c6fSJaegeuk Kim 
257101eccef7SChao Yu static inline struct page *f2fs_pagecache_get_page(
257201eccef7SChao Yu 				struct address_space *mapping, pgoff_t index,
257301eccef7SChao Yu 				int fgp_flags, gfp_t gfp_mask)
257401eccef7SChao Yu {
257501eccef7SChao Yu 	if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_GET)) {
2576c45d6002SChao Yu 		f2fs_show_injection_info(F2FS_M_SB(mapping), FAULT_PAGE_GET);
257701eccef7SChao Yu 		return NULL;
257801eccef7SChao Yu 	}
25797fa750a1SArnd Bergmann 
258001eccef7SChao Yu 	return pagecache_get_page(mapping, index, fgp_flags, gfp_mask);
258101eccef7SChao Yu }
258201eccef7SChao Yu 
25836e2c64adSJaegeuk Kim static inline void f2fs_copy_page(struct page *src, struct page *dst)
25846e2c64adSJaegeuk Kim {
25856e2c64adSJaegeuk Kim 	char *src_kaddr = kmap(src);
25866e2c64adSJaegeuk Kim 	char *dst_kaddr = kmap(dst);
25876e2c64adSJaegeuk Kim 
25886e2c64adSJaegeuk Kim 	memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
25896e2c64adSJaegeuk Kim 	kunmap(dst);
25906e2c64adSJaegeuk Kim 	kunmap(src);
25916e2c64adSJaegeuk Kim }
25926e2c64adSJaegeuk Kim 
259339a53e0cSJaegeuk Kim static inline void f2fs_put_page(struct page *page, int unlock)
259439a53e0cSJaegeuk Kim {
2595031fa8ccSJaegeuk Kim 	if (!page)
259639a53e0cSJaegeuk Kim 		return;
259739a53e0cSJaegeuk Kim 
259839a53e0cSJaegeuk Kim 	if (unlock) {
25999850cf4aSJaegeuk Kim 		f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
260039a53e0cSJaegeuk Kim 		unlock_page(page);
260139a53e0cSJaegeuk Kim 	}
260209cbfeafSKirill A. Shutemov 	put_page(page);
260339a53e0cSJaegeuk Kim }
260439a53e0cSJaegeuk Kim 
260539a53e0cSJaegeuk Kim static inline void f2fs_put_dnode(struct dnode_of_data *dn)
260639a53e0cSJaegeuk Kim {
260739a53e0cSJaegeuk Kim 	if (dn->node_page)
260839a53e0cSJaegeuk Kim 		f2fs_put_page(dn->node_page, 1);
260939a53e0cSJaegeuk Kim 	if (dn->inode_page && dn->node_page != dn->inode_page)
261039a53e0cSJaegeuk Kim 		f2fs_put_page(dn->inode_page, 0);
261139a53e0cSJaegeuk Kim 	dn->node_page = NULL;
261239a53e0cSJaegeuk Kim 	dn->inode_page = NULL;
261339a53e0cSJaegeuk Kim }
261439a53e0cSJaegeuk Kim 
261539a53e0cSJaegeuk Kim static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
2616e8512d2eSGu Zheng 					size_t size)
261739a53e0cSJaegeuk Kim {
2618e8512d2eSGu Zheng 	return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
261939a53e0cSJaegeuk Kim }
262039a53e0cSJaegeuk Kim 
26217bd59381SGu Zheng static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
26227bd59381SGu Zheng 						gfp_t flags)
26237bd59381SGu Zheng {
26247bd59381SGu Zheng 	void *entry;
26257bd59381SGu Zheng 
262680c54505SJaegeuk Kim 	entry = kmem_cache_alloc(cachep, flags);
262780c54505SJaegeuk Kim 	if (!entry)
262880c54505SJaegeuk Kim 		entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
26297bd59381SGu Zheng 	return entry;
26307bd59381SGu Zheng }
26317bd59381SGu Zheng 
2632493720a4SChao Yu static inline bool is_inflight_io(struct f2fs_sb_info *sbi, int type)
26335f9abab4SJaegeuk Kim {
26345f9abab4SJaegeuk Kim 	if (get_pages(sbi, F2FS_RD_DATA) || get_pages(sbi, F2FS_RD_NODE) ||
26355f9abab4SJaegeuk Kim 		get_pages(sbi, F2FS_RD_META) || get_pages(sbi, F2FS_WB_DATA) ||
2636fef4129eSChao Yu 		get_pages(sbi, F2FS_WB_CP_DATA) ||
2637fef4129eSChao Yu 		get_pages(sbi, F2FS_DIO_READ) ||
263811ac8ef8SJaegeuk Kim 		get_pages(sbi, F2FS_DIO_WRITE))
2639493720a4SChao Yu 		return true;
264011ac8ef8SJaegeuk Kim 
264156659ce8SSahitya Tummala 	if (type != DISCARD_TIME && SM_I(sbi) && SM_I(sbi)->dcc_info &&
264211ac8ef8SJaegeuk Kim 			atomic_read(&SM_I(sbi)->dcc_info->queued_discard))
2643493720a4SChao Yu 		return true;
264411ac8ef8SJaegeuk Kim 
264511ac8ef8SJaegeuk Kim 	if (SM_I(sbi) && SM_I(sbi)->fcc_info &&
264672691af6SJaegeuk Kim 			atomic_read(&SM_I(sbi)->fcc_info->queued_flush))
2647493720a4SChao Yu 		return true;
2648493720a4SChao Yu 	return false;
2649493720a4SChao Yu }
2650493720a4SChao Yu 
2651493720a4SChao Yu static inline bool is_idle(struct f2fs_sb_info *sbi, int type)
2652493720a4SChao Yu {
2653493720a4SChao Yu 	if (sbi->gc_mode == GC_URGENT_HIGH)
2654493720a4SChao Yu 		return true;
2655493720a4SChao Yu 
2656493720a4SChao Yu 	if (is_inflight_io(sbi, type))
26575f9abab4SJaegeuk Kim 		return false;
265811ac8ef8SJaegeuk Kim 
26590e5e8111SDaeho Jeong 	if (sbi->gc_mode == GC_URGENT_LOW &&
26600e5e8111SDaeho Jeong 			(type == DISCARD_TIME || type == GC_TIME))
26610e5e8111SDaeho Jeong 		return true;
26620e5e8111SDaeho Jeong 
26635f9abab4SJaegeuk Kim 	return f2fs_time_over(sbi, type);
26645f9abab4SJaegeuk Kim }
26655f9abab4SJaegeuk Kim 
26669be32d72SJaegeuk Kim static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
26679be32d72SJaegeuk Kim 				unsigned long index, void *item)
26689be32d72SJaegeuk Kim {
26699be32d72SJaegeuk Kim 	while (radix_tree_insert(root, index, item))
26709be32d72SJaegeuk Kim 		cond_resched();
26719be32d72SJaegeuk Kim }
26729be32d72SJaegeuk Kim 
267339a53e0cSJaegeuk Kim #define RAW_IS_INODE(p)	((p)->footer.nid == (p)->footer.ino)
267439a53e0cSJaegeuk Kim 
267539a53e0cSJaegeuk Kim static inline bool IS_INODE(struct page *page)
267639a53e0cSJaegeuk Kim {
267745590710SGu Zheng 	struct f2fs_node *p = F2FS_NODE(page);
2678cac5a3d8SDongOh Shin 
267939a53e0cSJaegeuk Kim 	return RAW_IS_INODE(p);
268039a53e0cSJaegeuk Kim }
268139a53e0cSJaegeuk Kim 
26827a2af766SChao Yu static inline int offset_in_addr(struct f2fs_inode *i)
26837a2af766SChao Yu {
26847a2af766SChao Yu 	return (i->i_inline & F2FS_EXTRA_ATTR) ?
26857a2af766SChao Yu 			(le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0;
26867a2af766SChao Yu }
26877a2af766SChao Yu 
268839a53e0cSJaegeuk Kim static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
268939a53e0cSJaegeuk Kim {
269039a53e0cSJaegeuk Kim 	return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
269139a53e0cSJaegeuk Kim }
269239a53e0cSJaegeuk Kim 
26937a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode);
2694a2ced1ceSChao Yu static inline block_t data_blkaddr(struct inode *inode,
26957a2af766SChao Yu 			struct page *node_page, unsigned int offset)
269639a53e0cSJaegeuk Kim {
269739a53e0cSJaegeuk Kim 	struct f2fs_node *raw_node;
269839a53e0cSJaegeuk Kim 	__le32 *addr_array;
26997a2af766SChao Yu 	int base = 0;
27007a2af766SChao Yu 	bool is_inode = IS_INODE(node_page);
2701cac5a3d8SDongOh Shin 
270245590710SGu Zheng 	raw_node = F2FS_NODE(node_page);
27037a2af766SChao Yu 
2704979f492fSLiFan 	if (is_inode) {
2705979f492fSLiFan 		if (!inode)
27067a88ddb5SChao Yu 			/* from GC path only */
27077a2af766SChao Yu 			base = offset_in_addr(&raw_node->i);
2708979f492fSLiFan 		else if (f2fs_has_extra_attr(inode))
27097a2af766SChao Yu 			base = get_extra_isize(inode);
27107a2af766SChao Yu 	}
27117a2af766SChao Yu 
271239a53e0cSJaegeuk Kim 	addr_array = blkaddr_in_node(raw_node);
27137a2af766SChao Yu 	return le32_to_cpu(addr_array[base + offset]);
271439a53e0cSJaegeuk Kim }
271539a53e0cSJaegeuk Kim 
2716a2ced1ceSChao Yu static inline block_t f2fs_data_blkaddr(struct dnode_of_data *dn)
2717a2ced1ceSChao Yu {
2718a2ced1ceSChao Yu 	return data_blkaddr(dn->inode, dn->node_page, dn->ofs_in_node);
2719a2ced1ceSChao Yu }
2720a2ced1ceSChao Yu 
272139a53e0cSJaegeuk Kim static inline int f2fs_test_bit(unsigned int nr, char *addr)
272239a53e0cSJaegeuk Kim {
272339a53e0cSJaegeuk Kim 	int mask;
272439a53e0cSJaegeuk Kim 
272539a53e0cSJaegeuk Kim 	addr += (nr >> 3);
272639a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
272739a53e0cSJaegeuk Kim 	return mask & *addr;
272839a53e0cSJaegeuk Kim }
272939a53e0cSJaegeuk Kim 
2730a66cdd98SJaegeuk Kim static inline void f2fs_set_bit(unsigned int nr, char *addr)
2731a66cdd98SJaegeuk Kim {
2732a66cdd98SJaegeuk Kim 	int mask;
2733a66cdd98SJaegeuk Kim 
2734a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2735a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2736a66cdd98SJaegeuk Kim 	*addr |= mask;
2737a66cdd98SJaegeuk Kim }
2738a66cdd98SJaegeuk Kim 
2739a66cdd98SJaegeuk Kim static inline void f2fs_clear_bit(unsigned int nr, char *addr)
2740a66cdd98SJaegeuk Kim {
2741a66cdd98SJaegeuk Kim 	int mask;
2742a66cdd98SJaegeuk Kim 
2743a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2744a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2745a66cdd98SJaegeuk Kim 	*addr &= ~mask;
2746a66cdd98SJaegeuk Kim }
2747a66cdd98SJaegeuk Kim 
274852aca074SGu Zheng static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
274939a53e0cSJaegeuk Kim {
275039a53e0cSJaegeuk Kim 	int mask;
275139a53e0cSJaegeuk Kim 	int ret;
275239a53e0cSJaegeuk Kim 
275339a53e0cSJaegeuk Kim 	addr += (nr >> 3);
275439a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
275539a53e0cSJaegeuk Kim 	ret = mask & *addr;
275639a53e0cSJaegeuk Kim 	*addr |= mask;
275739a53e0cSJaegeuk Kim 	return ret;
275839a53e0cSJaegeuk Kim }
275939a53e0cSJaegeuk Kim 
276052aca074SGu Zheng static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
276139a53e0cSJaegeuk Kim {
276239a53e0cSJaegeuk Kim 	int mask;
276339a53e0cSJaegeuk Kim 	int ret;
276439a53e0cSJaegeuk Kim 
276539a53e0cSJaegeuk Kim 	addr += (nr >> 3);
276639a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
276739a53e0cSJaegeuk Kim 	ret = mask & *addr;
276839a53e0cSJaegeuk Kim 	*addr &= ~mask;
276939a53e0cSJaegeuk Kim 	return ret;
277039a53e0cSJaegeuk Kim }
277139a53e0cSJaegeuk Kim 
2772c6ac4c0eSGu Zheng static inline void f2fs_change_bit(unsigned int nr, char *addr)
2773c6ac4c0eSGu Zheng {
2774c6ac4c0eSGu Zheng 	int mask;
2775c6ac4c0eSGu Zheng 
2776c6ac4c0eSGu Zheng 	addr += (nr >> 3);
2777c6ac4c0eSGu Zheng 	mask = 1 << (7 - (nr & 0x07));
2778c6ac4c0eSGu Zheng 	*addr ^= mask;
2779c6ac4c0eSGu Zheng }
2780c6ac4c0eSGu Zheng 
278159c84408SChao Yu /*
278236098557SEric Biggers  * On-disk inode flags (f2fs_inode::i_flags)
278359c84408SChao Yu  */
27844c8ff709SChao Yu #define F2FS_COMPR_FL			0x00000004 /* Compress file */
278559c84408SChao Yu #define F2FS_SYNC_FL			0x00000008 /* Synchronous updates */
278659c84408SChao Yu #define F2FS_IMMUTABLE_FL		0x00000010 /* Immutable file */
278759c84408SChao Yu #define F2FS_APPEND_FL			0x00000020 /* writes to file may only append */
278859c84408SChao Yu #define F2FS_NODUMP_FL			0x00000040 /* do not dump file */
278959c84408SChao Yu #define F2FS_NOATIME_FL			0x00000080 /* do not update atime */
27904c8ff709SChao Yu #define F2FS_NOCOMP_FL			0x00000400 /* Don't compress */
279159c84408SChao Yu #define F2FS_INDEX_FL			0x00001000 /* hash-indexed directory */
279259c84408SChao Yu #define F2FS_DIRSYNC_FL			0x00010000 /* dirsync behaviour (directories only) */
279359c84408SChao Yu #define F2FS_PROJINHERIT_FL		0x20000000 /* Create with parents projid */
27942c2eb7a3SDaniel Rosenberg #define F2FS_CASEFOLD_FL		0x40000000 /* Casefolded file */
279559c84408SChao Yu 
279659c84408SChao Yu /* Flags that should be inherited by new inodes from their parent. */
279736098557SEric Biggers #define F2FS_FL_INHERITED (F2FS_SYNC_FL | F2FS_NODUMP_FL | F2FS_NOATIME_FL | \
27982c2eb7a3SDaniel Rosenberg 			   F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL | \
27994c8ff709SChao Yu 			   F2FS_CASEFOLD_FL | F2FS_COMPR_FL | F2FS_NOCOMP_FL)
280059c84408SChao Yu 
280159c84408SChao Yu /* Flags that are appropriate for regular files (all but dir-specific ones). */
28022c2eb7a3SDaniel Rosenberg #define F2FS_REG_FLMASK		(~(F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL | \
28032c2eb7a3SDaniel Rosenberg 				F2FS_CASEFOLD_FL))
280459c84408SChao Yu 
280559c84408SChao Yu /* Flags that are appropriate for non-directories/regular files. */
280659c84408SChao Yu #define F2FS_OTHER_FLMASK	(F2FS_NODUMP_FL | F2FS_NOATIME_FL)
28075c57132eSChao Yu 
28085c57132eSChao Yu static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
28095c57132eSChao Yu {
28105c57132eSChao Yu 	if (S_ISDIR(mode))
28115c57132eSChao Yu 		return flags;
28125c57132eSChao Yu 	else if (S_ISREG(mode))
28135c57132eSChao Yu 		return flags & F2FS_REG_FLMASK;
28145c57132eSChao Yu 	else
28155c57132eSChao Yu 		return flags & F2FS_OTHER_FLMASK;
28165c57132eSChao Yu }
28175c57132eSChao Yu 
2818205b9822SJaegeuk Kim static inline void __mark_inode_dirty_flag(struct inode *inode,
2819205b9822SJaegeuk Kim 						int flag, bool set)
282039a53e0cSJaegeuk Kim {
2821205b9822SJaegeuk Kim 	switch (flag) {
2822205b9822SJaegeuk Kim 	case FI_INLINE_XATTR:
2823205b9822SJaegeuk Kim 	case FI_INLINE_DATA:
2824205b9822SJaegeuk Kim 	case FI_INLINE_DENTRY:
28259ac1e2d8SDaeho Jeong 	case FI_NEW_INODE:
2826205b9822SJaegeuk Kim 		if (set)
2827205b9822SJaegeuk Kim 			return;
2828df561f66SGustavo A. R. Silva 		fallthrough;
2829205b9822SJaegeuk Kim 	case FI_DATA_EXIST:
2830205b9822SJaegeuk Kim 	case FI_INLINE_DOTS:
28311ad71a27SJaegeuk Kim 	case FI_PIN_FILE:
2832c6140415SJaegeuk Kim 	case FI_COMPRESS_RELEASED:
28337c45729aSJaegeuk Kim 		f2fs_mark_inode_dirty_sync(inode, true);
2834205b9822SJaegeuk Kim 	}
283539a53e0cSJaegeuk Kim }
283639a53e0cSJaegeuk Kim 
283791942321SJaegeuk Kim static inline void set_inode_flag(struct inode *inode, int flag)
283839a53e0cSJaegeuk Kim {
283958f7e00fSYufen Yu 	set_bit(flag, F2FS_I(inode)->flags);
2840205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, true);
284139a53e0cSJaegeuk Kim }
284239a53e0cSJaegeuk Kim 
284391942321SJaegeuk Kim static inline int is_inode_flag_set(struct inode *inode, int flag)
284439a53e0cSJaegeuk Kim {
28457653b9d8SChao Yu 	return test_bit(flag, F2FS_I(inode)->flags);
284639a53e0cSJaegeuk Kim }
284739a53e0cSJaegeuk Kim 
284891942321SJaegeuk Kim static inline void clear_inode_flag(struct inode *inode, int flag)
284939a53e0cSJaegeuk Kim {
285058f7e00fSYufen Yu 	clear_bit(flag, F2FS_I(inode)->flags);
2851205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, false);
285239a53e0cSJaegeuk Kim }
285339a53e0cSJaegeuk Kim 
285495ae251fSEric Biggers static inline bool f2fs_verity_in_progress(struct inode *inode)
285595ae251fSEric Biggers {
285695ae251fSEric Biggers 	return IS_ENABLED(CONFIG_FS_VERITY) &&
285795ae251fSEric Biggers 	       is_inode_flag_set(inode, FI_VERITY_IN_PROGRESS);
285895ae251fSEric Biggers }
285995ae251fSEric Biggers 
286091942321SJaegeuk Kim static inline void set_acl_inode(struct inode *inode, umode_t mode)
2861444c580fSJaegeuk Kim {
286291942321SJaegeuk Kim 	F2FS_I(inode)->i_acl_mode = mode;
286391942321SJaegeuk Kim 	set_inode_flag(inode, FI_ACL_MODE);
28647c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, false);
286539a53e0cSJaegeuk Kim }
286639a53e0cSJaegeuk Kim 
2867a1961246SJaegeuk Kim static inline void f2fs_i_links_write(struct inode *inode, bool inc)
2868a1961246SJaegeuk Kim {
2869a1961246SJaegeuk Kim 	if (inc)
2870a1961246SJaegeuk Kim 		inc_nlink(inode);
2871a1961246SJaegeuk Kim 	else
2872a1961246SJaegeuk Kim 		drop_nlink(inode);
28737c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2874a1961246SJaegeuk Kim }
2875a1961246SJaegeuk Kim 
28768edd03c8SJaegeuk Kim static inline void f2fs_i_blocks_write(struct inode *inode,
28770abd675eSChao Yu 					block_t diff, bool add, bool claim)
28788edd03c8SJaegeuk Kim {
287926de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
288026de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
288126de9b11SJaegeuk Kim 
28820abd675eSChao Yu 	/* add = 1, claim = 1 should be dquot_reserve_block in pair */
28830abd675eSChao Yu 	if (add) {
28840abd675eSChao Yu 		if (claim)
28850abd675eSChao Yu 			dquot_claim_block(inode, diff);
28860abd675eSChao Yu 		else
28870abd675eSChao Yu 			dquot_alloc_block_nofail(inode, diff);
28880abd675eSChao Yu 	} else {
28890abd675eSChao Yu 		dquot_free_block(inode, diff);
28900abd675eSChao Yu 	}
28910abd675eSChao Yu 
28927c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
289326de9b11SJaegeuk Kim 	if (clean || recover)
289426de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
28958edd03c8SJaegeuk Kim }
28968edd03c8SJaegeuk Kim 
2897fc9581c8SJaegeuk Kim static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
2898fc9581c8SJaegeuk Kim {
289926de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
290026de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
290126de9b11SJaegeuk Kim 
2902fc9581c8SJaegeuk Kim 	if (i_size_read(inode) == i_size)
2903fc9581c8SJaegeuk Kim 		return;
2904fc9581c8SJaegeuk Kim 
2905fc9581c8SJaegeuk Kim 	i_size_write(inode, i_size);
29067c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
290726de9b11SJaegeuk Kim 	if (clean || recover)
290826de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
290926de9b11SJaegeuk Kim }
291026de9b11SJaegeuk Kim 
2911205b9822SJaegeuk Kim static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
2912205b9822SJaegeuk Kim {
2913205b9822SJaegeuk Kim 	F2FS_I(inode)->i_current_depth = depth;
29147c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2915205b9822SJaegeuk Kim }
2916205b9822SJaegeuk Kim 
29171ad71a27SJaegeuk Kim static inline void f2fs_i_gc_failures_write(struct inode *inode,
29181ad71a27SJaegeuk Kim 					unsigned int count)
29191ad71a27SJaegeuk Kim {
29202ef79ecbSChao Yu 	F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN] = count;
29211ad71a27SJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
29221ad71a27SJaegeuk Kim }
29231ad71a27SJaegeuk Kim 
2924205b9822SJaegeuk Kim static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
2925205b9822SJaegeuk Kim {
2926205b9822SJaegeuk Kim 	F2FS_I(inode)->i_xattr_nid = xnid;
29277c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2928205b9822SJaegeuk Kim }
2929205b9822SJaegeuk Kim 
2930205b9822SJaegeuk Kim static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
2931205b9822SJaegeuk Kim {
2932205b9822SJaegeuk Kim 	F2FS_I(inode)->i_pino = pino;
29337c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2934205b9822SJaegeuk Kim }
2935205b9822SJaegeuk Kim 
293691942321SJaegeuk Kim static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
2937444c580fSJaegeuk Kim {
2938205b9822SJaegeuk Kim 	struct f2fs_inode_info *fi = F2FS_I(inode);
2939205b9822SJaegeuk Kim 
2940444c580fSJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_XATTR)
29417653b9d8SChao Yu 		set_bit(FI_INLINE_XATTR, fi->flags);
29421001b347SHuajun Li 	if (ri->i_inline & F2FS_INLINE_DATA)
29437653b9d8SChao Yu 		set_bit(FI_INLINE_DATA, fi->flags);
294434d67debSChao Yu 	if (ri->i_inline & F2FS_INLINE_DENTRY)
29457653b9d8SChao Yu 		set_bit(FI_INLINE_DENTRY, fi->flags);
2946b3d208f9SJaegeuk Kim 	if (ri->i_inline & F2FS_DATA_EXIST)
29477653b9d8SChao Yu 		set_bit(FI_DATA_EXIST, fi->flags);
2948510022a8SJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_DOTS)
29497653b9d8SChao Yu 		set_bit(FI_INLINE_DOTS, fi->flags);
29507a2af766SChao Yu 	if (ri->i_inline & F2FS_EXTRA_ATTR)
29517653b9d8SChao Yu 		set_bit(FI_EXTRA_ATTR, fi->flags);
29521ad71a27SJaegeuk Kim 	if (ri->i_inline & F2FS_PIN_FILE)
29537653b9d8SChao Yu 		set_bit(FI_PIN_FILE, fi->flags);
2954c6140415SJaegeuk Kim 	if (ri->i_inline & F2FS_COMPRESS_RELEASED)
2955c6140415SJaegeuk Kim 		set_bit(FI_COMPRESS_RELEASED, fi->flags);
2956444c580fSJaegeuk Kim }
2957444c580fSJaegeuk Kim 
295891942321SJaegeuk Kim static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
2959444c580fSJaegeuk Kim {
2960444c580fSJaegeuk Kim 	ri->i_inline = 0;
2961444c580fSJaegeuk Kim 
296291942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_XATTR))
2963444c580fSJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_XATTR;
296491942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DATA))
29651001b347SHuajun Li 		ri->i_inline |= F2FS_INLINE_DATA;
296691942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
296734d67debSChao Yu 		ri->i_inline |= F2FS_INLINE_DENTRY;
296891942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_DATA_EXIST))
2969b3d208f9SJaegeuk Kim 		ri->i_inline |= F2FS_DATA_EXIST;
297091942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DOTS))
2971510022a8SJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_DOTS;
29727a2af766SChao Yu 	if (is_inode_flag_set(inode, FI_EXTRA_ATTR))
29737a2af766SChao Yu 		ri->i_inline |= F2FS_EXTRA_ATTR;
29741ad71a27SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_PIN_FILE))
29751ad71a27SJaegeuk Kim 		ri->i_inline |= F2FS_PIN_FILE;
2976c6140415SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
2977c6140415SJaegeuk Kim 		ri->i_inline |= F2FS_COMPRESS_RELEASED;
29787a2af766SChao Yu }
29797a2af766SChao Yu 
29807a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode)
29817a2af766SChao Yu {
29827a2af766SChao Yu 	return is_inode_flag_set(inode, FI_EXTRA_ATTR);
2983444c580fSJaegeuk Kim }
2984444c580fSJaegeuk Kim 
2985987c7c31SChao Yu static inline int f2fs_has_inline_xattr(struct inode *inode)
2986987c7c31SChao Yu {
298791942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_XATTR);
2988987c7c31SChao Yu }
2989987c7c31SChao Yu 
29904c8ff709SChao Yu static inline int f2fs_compressed_file(struct inode *inode)
29914c8ff709SChao Yu {
29924c8ff709SChao Yu 	return S_ISREG(inode->i_mode) &&
29934c8ff709SChao Yu 		is_inode_flag_set(inode, FI_COMPRESSED_FILE);
29944c8ff709SChao Yu }
29954c8ff709SChao Yu 
2996602a16d5SDaeho Jeong static inline bool f2fs_need_compress_data(struct inode *inode)
2997602a16d5SDaeho Jeong {
2998602a16d5SDaeho Jeong 	int compress_mode = F2FS_OPTION(F2FS_I_SB(inode)).compress_mode;
2999602a16d5SDaeho Jeong 
3000602a16d5SDaeho Jeong 	if (!f2fs_compressed_file(inode))
3001602a16d5SDaeho Jeong 		return false;
3002602a16d5SDaeho Jeong 
3003602a16d5SDaeho Jeong 	if (compress_mode == COMPR_MODE_FS)
3004602a16d5SDaeho Jeong 		return true;
3005602a16d5SDaeho Jeong 	else if (compress_mode == COMPR_MODE_USER &&
3006602a16d5SDaeho Jeong 			is_inode_flag_set(inode, FI_ENABLE_COMPRESS))
3007602a16d5SDaeho Jeong 		return true;
3008602a16d5SDaeho Jeong 
3009602a16d5SDaeho Jeong 	return false;
3010602a16d5SDaeho Jeong }
3011602a16d5SDaeho Jeong 
301281ca7350SChao Yu static inline unsigned int addrs_per_inode(struct inode *inode)
3013de93653fSJaegeuk Kim {
3014d02a6e61SChao Yu 	unsigned int addrs = CUR_ADDRS_PER_INODE(inode) -
3015d02a6e61SChao Yu 				get_inline_xattr_addrs(inode);
30164c8ff709SChao Yu 
30174c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
30184c8ff709SChao Yu 		return addrs;
30194c8ff709SChao Yu 	return ALIGN_DOWN(addrs, F2FS_I(inode)->i_cluster_size);
3020d02a6e61SChao Yu }
3021d02a6e61SChao Yu 
3022d02a6e61SChao Yu static inline unsigned int addrs_per_block(struct inode *inode)
3023d02a6e61SChao Yu {
30244c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
30254c8ff709SChao Yu 		return DEF_ADDRS_PER_BLOCK;
30264c8ff709SChao Yu 	return ALIGN_DOWN(DEF_ADDRS_PER_BLOCK, F2FS_I(inode)->i_cluster_size);
3027de93653fSJaegeuk Kim }
3028de93653fSJaegeuk Kim 
30296afc662eSChao Yu static inline void *inline_xattr_addr(struct inode *inode, struct page *page)
303065985d93SJaegeuk Kim {
3031695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
3032cac5a3d8SDongOh Shin 
303365985d93SJaegeuk Kim 	return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
3034b323fd28SChao Yu 					get_inline_xattr_addrs(inode)]);
303565985d93SJaegeuk Kim }
303665985d93SJaegeuk Kim 
303765985d93SJaegeuk Kim static inline int inline_xattr_size(struct inode *inode)
303865985d93SJaegeuk Kim {
3039622927f3SChao Yu 	if (f2fs_has_inline_xattr(inode))
30406afc662eSChao Yu 		return get_inline_xattr_addrs(inode) * sizeof(__le32);
3041622927f3SChao Yu 	return 0;
304265985d93SJaegeuk Kim }
304365985d93SJaegeuk Kim 
30440dbdc2aeSJaegeuk Kim static inline int f2fs_has_inline_data(struct inode *inode)
30450dbdc2aeSJaegeuk Kim {
304691942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DATA);
30470dbdc2aeSJaegeuk Kim }
30480dbdc2aeSJaegeuk Kim 
3049b3d208f9SJaegeuk Kim static inline int f2fs_exist_data(struct inode *inode)
3050b3d208f9SJaegeuk Kim {
305191942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DATA_EXIST);
3052b3d208f9SJaegeuk Kim }
3053b3d208f9SJaegeuk Kim 
3054510022a8SJaegeuk Kim static inline int f2fs_has_inline_dots(struct inode *inode)
3055510022a8SJaegeuk Kim {
305691942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DOTS);
3057510022a8SJaegeuk Kim }
3058510022a8SJaegeuk Kim 
30594c8ff709SChao Yu static inline int f2fs_is_mmap_file(struct inode *inode)
30604c8ff709SChao Yu {
30614c8ff709SChao Yu 	return is_inode_flag_set(inode, FI_MMAP_FILE);
30624c8ff709SChao Yu }
30634c8ff709SChao Yu 
30641ad71a27SJaegeuk Kim static inline bool f2fs_is_pinned_file(struct inode *inode)
30651ad71a27SJaegeuk Kim {
30661ad71a27SJaegeuk Kim 	return is_inode_flag_set(inode, FI_PIN_FILE);
30671ad71a27SJaegeuk Kim }
30681ad71a27SJaegeuk Kim 
306988b88a66SJaegeuk Kim static inline bool f2fs_is_atomic_file(struct inode *inode)
307088b88a66SJaegeuk Kim {
307191942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_ATOMIC_FILE);
307288b88a66SJaegeuk Kim }
307388b88a66SJaegeuk Kim 
30745fe45743SChao Yu static inline bool f2fs_is_commit_atomic_write(struct inode *inode)
30755fe45743SChao Yu {
30765fe45743SChao Yu 	return is_inode_flag_set(inode, FI_ATOMIC_COMMIT);
30775fe45743SChao Yu }
30785fe45743SChao Yu 
307902a1335fSJaegeuk Kim static inline bool f2fs_is_volatile_file(struct inode *inode)
308002a1335fSJaegeuk Kim {
308191942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_VOLATILE_FILE);
308202a1335fSJaegeuk Kim }
308302a1335fSJaegeuk Kim 
30843c6c2bebSJaegeuk Kim static inline bool f2fs_is_first_block_written(struct inode *inode)
30853c6c2bebSJaegeuk Kim {
308691942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
30873c6c2bebSJaegeuk Kim }
30883c6c2bebSJaegeuk Kim 
30891e84371fSJaegeuk Kim static inline bool f2fs_is_drop_cache(struct inode *inode)
30901e84371fSJaegeuk Kim {
309191942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DROP_CACHE);
30921e84371fSJaegeuk Kim }
30931e84371fSJaegeuk Kim 
3094f2470371SChao Yu static inline void *inline_data_addr(struct inode *inode, struct page *page)
30951001b347SHuajun Li {
3096695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
30977a2af766SChao Yu 	int extra_size = get_extra_isize(inode);
3098cac5a3d8SDongOh Shin 
30997a2af766SChao Yu 	return (void *)&(ri->i_addr[extra_size + DEF_INLINE_RESERVED_SIZE]);
31001001b347SHuajun Li }
31011001b347SHuajun Li 
310234d67debSChao Yu static inline int f2fs_has_inline_dentry(struct inode *inode)
310334d67debSChao Yu {
310491942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DENTRY);
310534d67debSChao Yu }
310634d67debSChao Yu 
3107b5492af7SJaegeuk Kim static inline int is_file(struct inode *inode, int type)
3108b5492af7SJaegeuk Kim {
3109b5492af7SJaegeuk Kim 	return F2FS_I(inode)->i_advise & type;
3110b5492af7SJaegeuk Kim }
3111b5492af7SJaegeuk Kim 
3112b5492af7SJaegeuk Kim static inline void set_file(struct inode *inode, int type)
3113b5492af7SJaegeuk Kim {
3114b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise |= type;
31157c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3116b5492af7SJaegeuk Kim }
3117b5492af7SJaegeuk Kim 
3118b5492af7SJaegeuk Kim static inline void clear_file(struct inode *inode, int type)
3119b5492af7SJaegeuk Kim {
3120b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise &= ~type;
31217c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3122b5492af7SJaegeuk Kim }
3123b5492af7SJaegeuk Kim 
3124fe1897eaSChao Yu static inline bool f2fs_is_time_consistent(struct inode *inode)
3125fe1897eaSChao Yu {
3126fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time, &inode->i_atime))
3127fe1897eaSChao Yu 		return false;
3128fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 1, &inode->i_ctime))
3129fe1897eaSChao Yu 		return false;
3130fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 2, &inode->i_mtime))
3131fe1897eaSChao Yu 		return false;
3132fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 3,
3133fe1897eaSChao Yu 						&F2FS_I(inode)->i_crtime))
3134fe1897eaSChao Yu 		return false;
3135fe1897eaSChao Yu 	return true;
3136fe1897eaSChao Yu }
3137fe1897eaSChao Yu 
313826787236SJaegeuk Kim static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
313926787236SJaegeuk Kim {
3140a0d00fadSChao Yu 	bool ret;
3141a0d00fadSChao Yu 
314226787236SJaegeuk Kim 	if (dsync) {
314326787236SJaegeuk Kim 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
314426787236SJaegeuk Kim 
314526787236SJaegeuk Kim 		spin_lock(&sbi->inode_lock[DIRTY_META]);
314626787236SJaegeuk Kim 		ret = list_empty(&F2FS_I(inode)->gdirty_list);
314726787236SJaegeuk Kim 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
314826787236SJaegeuk Kim 		return ret;
314926787236SJaegeuk Kim 	}
315026787236SJaegeuk Kim 	if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
315126787236SJaegeuk Kim 			file_keep_isize(inode) ||
3152235831d7SJunling Zheng 			i_size_read(inode) & ~PAGE_MASK)
315326787236SJaegeuk Kim 		return false;
3154a0d00fadSChao Yu 
3155fe1897eaSChao Yu 	if (!f2fs_is_time_consistent(inode))
3156214c2461SJaegeuk Kim 		return false;
3157214c2461SJaegeuk Kim 
3158c10c9820SChao Yu 	spin_lock(&F2FS_I(inode)->i_size_lock);
3159a0d00fadSChao Yu 	ret = F2FS_I(inode)->last_disk_size == i_size_read(inode);
3160c10c9820SChao Yu 	spin_unlock(&F2FS_I(inode)->i_size_lock);
3161a0d00fadSChao Yu 
3162a0d00fadSChao Yu 	return ret;
3163267378d4SChao Yu }
3164267378d4SChao Yu 
3165deeedd71SChao Yu static inline bool f2fs_readonly(struct super_block *sb)
316677888c1eSJaegeuk Kim {
3167deeedd71SChao Yu 	return sb_rdonly(sb);
316877888c1eSJaegeuk Kim }
316977888c1eSJaegeuk Kim 
3170744602cfSJaegeuk Kim static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
3171744602cfSJaegeuk Kim {
3172aaec2b1dSChao Yu 	return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
3173744602cfSJaegeuk Kim }
3174744602cfSJaegeuk Kim 
317543c780baSEric Biggers static inline bool is_dot_dotdot(const u8 *name, size_t len)
3176eaa693f4SJaegeuk Kim {
317743c780baSEric Biggers 	if (len == 1 && name[0] == '.')
3178eaa693f4SJaegeuk Kim 		return true;
3179eaa693f4SJaegeuk Kim 
318043c780baSEric Biggers 	if (len == 2 && name[0] == '.' && name[1] == '.')
3181eaa693f4SJaegeuk Kim 		return true;
3182eaa693f4SJaegeuk Kim 
3183eaa693f4SJaegeuk Kim 	return false;
3184eaa693f4SJaegeuk Kim }
3185eaa693f4SJaegeuk Kim 
31861ecc0c5cSChao Yu static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
31871ecc0c5cSChao Yu 					size_t size, gfp_t flags)
31880414b004SJaegeuk Kim {
318955523519SChao Yu 	if (time_to_inject(sbi, FAULT_KMALLOC)) {
3190c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_KMALLOC);
31912c63feadSJaegeuk Kim 		return NULL;
319255523519SChao Yu 	}
31937fa750a1SArnd Bergmann 
31940b6d4ca0SEric Biggers 	return kmalloc(size, flags);
31950414b004SJaegeuk Kim }
31960414b004SJaegeuk Kim 
3197acbf054dSChao Yu static inline void *f2fs_kzalloc(struct f2fs_sb_info *sbi,
3198acbf054dSChao Yu 					size_t size, gfp_t flags)
3199acbf054dSChao Yu {
3200acbf054dSChao Yu 	return f2fs_kmalloc(sbi, size, flags | __GFP_ZERO);
3201acbf054dSChao Yu }
3202acbf054dSChao Yu 
3203628b3d14SChao Yu static inline void *f2fs_kvmalloc(struct f2fs_sb_info *sbi,
3204628b3d14SChao Yu 					size_t size, gfp_t flags)
3205628b3d14SChao Yu {
3206628b3d14SChao Yu 	if (time_to_inject(sbi, FAULT_KVMALLOC)) {
3207c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_KVMALLOC);
3208628b3d14SChao Yu 		return NULL;
3209628b3d14SChao Yu 	}
32107fa750a1SArnd Bergmann 
3211628b3d14SChao Yu 	return kvmalloc(size, flags);
3212628b3d14SChao Yu }
3213628b3d14SChao Yu 
3214628b3d14SChao Yu static inline void *f2fs_kvzalloc(struct f2fs_sb_info *sbi,
3215628b3d14SChao Yu 					size_t size, gfp_t flags)
3216628b3d14SChao Yu {
3217628b3d14SChao Yu 	return f2fs_kvmalloc(sbi, size, flags | __GFP_ZERO);
3218628b3d14SChao Yu }
3219628b3d14SChao Yu 
32207a2af766SChao Yu static inline int get_extra_isize(struct inode *inode)
3221f2470371SChao Yu {
32227a2af766SChao Yu 	return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
3223f2470371SChao Yu }
3224f2470371SChao Yu 
32256afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode)
32266afc662eSChao Yu {
32276afc662eSChao Yu 	return F2FS_I(inode)->i_inline_xattr_size;
32286afc662eSChao Yu }
32296afc662eSChao Yu 
32304d57b86dSChao Yu #define f2fs_get_inode_mode(i) \
323191942321SJaegeuk Kim 	((is_inode_flag_set(i, FI_ACL_MODE)) ? \
3232a6dda0e6SChristoph Hellwig 	 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
3233a6dda0e6SChristoph Hellwig 
32347a2af766SChao Yu #define F2FS_TOTAL_EXTRA_ATTR_SIZE			\
32357a2af766SChao Yu 	(offsetof(struct f2fs_inode, i_extra_end) -	\
32367a2af766SChao Yu 	offsetof(struct f2fs_inode, i_extra_isize))	\
32377a2af766SChao Yu 
32385c57132eSChao Yu #define F2FS_OLD_ATTRIBUTE_SIZE	(offsetof(struct f2fs_inode, i_addr))
32395c57132eSChao Yu #define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field)		\
32402f84babfSSheng Yong 		((offsetof(typeof(*(f2fs_inode)), field) +	\
32415c57132eSChao Yu 		sizeof((f2fs_inode)->field))			\
32422f84babfSSheng Yong 		<= (F2FS_OLD_ATTRIBUTE_SIZE + (extra_isize)))	\
32435c57132eSChao Yu 
32442bc4bea3SDaeho Jeong #define DEFAULT_IOSTAT_PERIOD_MS	3000
32452bc4bea3SDaeho Jeong #define MIN_IOSTAT_PERIOD_MS		100
32462bc4bea3SDaeho Jeong /* maximum period of iostat tracing is 1 day */
32472bc4bea3SDaeho Jeong #define MAX_IOSTAT_PERIOD_MS		8640000
32482bc4bea3SDaeho Jeong 
3249b0af6d49SChao Yu static inline void f2fs_reset_iostat(struct f2fs_sb_info *sbi)
3250b0af6d49SChao Yu {
3251b0af6d49SChao Yu 	int i;
3252b0af6d49SChao Yu 
3253b0af6d49SChao Yu 	spin_lock(&sbi->iostat_lock);
32542bc4bea3SDaeho Jeong 	for (i = 0; i < NR_IO_TYPE; i++) {
32558b83ac81SChao Yu 		sbi->rw_iostat[i] = 0;
32568b83ac81SChao Yu 		sbi->prev_rw_iostat[i] = 0;
32572bc4bea3SDaeho Jeong 	}
3258b0af6d49SChao Yu 	spin_unlock(&sbi->iostat_lock);
3259b0af6d49SChao Yu }
3260b0af6d49SChao Yu 
32612bc4bea3SDaeho Jeong extern void f2fs_record_iostat(struct f2fs_sb_info *sbi);
32622bc4bea3SDaeho Jeong 
3263b0af6d49SChao Yu static inline void f2fs_update_iostat(struct f2fs_sb_info *sbi,
3264b0af6d49SChao Yu 			enum iostat_type type, unsigned long long io_bytes)
3265b0af6d49SChao Yu {
3266b0af6d49SChao Yu 	if (!sbi->iostat_enable)
3267b0af6d49SChao Yu 		return;
3268b0af6d49SChao Yu 	spin_lock(&sbi->iostat_lock);
32698b83ac81SChao Yu 	sbi->rw_iostat[type] += io_bytes;
3270b0af6d49SChao Yu 
3271b0af6d49SChao Yu 	if (type == APP_WRITE_IO || type == APP_DIRECT_IO)
32728b83ac81SChao Yu 		sbi->rw_iostat[APP_BUFFERED_IO] =
32738b83ac81SChao Yu 			sbi->rw_iostat[APP_WRITE_IO] -
32748b83ac81SChao Yu 			sbi->rw_iostat[APP_DIRECT_IO];
32758b83ac81SChao Yu 
32768b83ac81SChao Yu 	if (type == APP_READ_IO || type == APP_DIRECT_READ_IO)
32778b83ac81SChao Yu 		sbi->rw_iostat[APP_BUFFERED_READ_IO] =
32788b83ac81SChao Yu 			sbi->rw_iostat[APP_READ_IO] -
32798b83ac81SChao Yu 			sbi->rw_iostat[APP_DIRECT_READ_IO];
3280b0af6d49SChao Yu 	spin_unlock(&sbi->iostat_lock);
32812bc4bea3SDaeho Jeong 
32822bc4bea3SDaeho Jeong 	f2fs_record_iostat(sbi);
3283b0af6d49SChao Yu }
3284b0af6d49SChao Yu 
32852c70c5e3SChao Yu #define __is_large_section(sbi)		((sbi)->segs_per_sec > 1)
32862c70c5e3SChao Yu 
32876dc3a126SChao Yu #define __is_meta_io(fio) (PAGE_TYPE_OF_BIO((fio)->type) == META)
3288c9b60788SChao Yu 
3289e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
3290e1da7872SChao Yu 					block_t blkaddr, int type);
3291e1da7872SChao Yu static inline void verify_blkaddr(struct f2fs_sb_info *sbi,
3292e1da7872SChao Yu 					block_t blkaddr, int type)
3293e1da7872SChao Yu {
3294e1da7872SChao Yu 	if (!f2fs_is_valid_blkaddr(sbi, blkaddr, type)) {
3295dcbb4c10SJoe Perches 		f2fs_err(sbi, "invalid blkaddr: %u, type: %d, run fsck to fix.",
3296e1da7872SChao Yu 			 blkaddr, type);
3297e1da7872SChao Yu 		f2fs_bug_on(sbi, 1);
3298e1da7872SChao Yu 	}
3299e1da7872SChao Yu }
3300e1da7872SChao Yu 
3301e1da7872SChao Yu static inline bool __is_valid_data_blkaddr(block_t blkaddr)
33027b525dd0SChao Yu {
33034c8ff709SChao Yu 	if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR ||
33044c8ff709SChao Yu 			blkaddr == COMPRESS_ADDR)
33057b525dd0SChao Yu 		return false;
33067b525dd0SChao Yu 	return true;
33077b525dd0SChao Yu }
33087b525dd0SChao Yu 
330939a53e0cSJaegeuk Kim /*
331039a53e0cSJaegeuk Kim  * file.c
331139a53e0cSJaegeuk Kim  */
3312cac5a3d8SDongOh Shin int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
33134d57b86dSChao Yu void f2fs_truncate_data_blocks(struct dnode_of_data *dn);
33143265d3dbSChao Yu int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock);
3315c42d28ceSChao Yu int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock);
3316cac5a3d8SDongOh Shin int f2fs_truncate(struct inode *inode);
3317549c7297SChristian Brauner int f2fs_getattr(struct user_namespace *mnt_userns, const struct path *path,
3318549c7297SChristian Brauner 		 struct kstat *stat, u32 request_mask, unsigned int flags);
3319549c7297SChristian Brauner int f2fs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
3320549c7297SChristian Brauner 		 struct iattr *attr);
33214d57b86dSChao Yu int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
33224d57b86dSChao Yu void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count);
3323c4020b2dSChao Yu int f2fs_precache_extents(struct inode *inode);
33249b1bb01cSMiklos Szeredi int f2fs_fileattr_get(struct dentry *dentry, struct fileattr *fa);
33259b1bb01cSMiklos Szeredi int f2fs_fileattr_set(struct user_namespace *mnt_userns,
33269b1bb01cSMiklos Szeredi 		      struct dentry *dentry, struct fileattr *fa);
3327cac5a3d8SDongOh Shin long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
3328cac5a3d8SDongOh Shin long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
332978130819SChao Yu int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid);
33301ad71a27SJaegeuk Kim int f2fs_pin_file_control(struct inode *inode, bool inc);
333139a53e0cSJaegeuk Kim 
333239a53e0cSJaegeuk Kim /*
333339a53e0cSJaegeuk Kim  * inode.c
333439a53e0cSJaegeuk Kim  */
3335cac5a3d8SDongOh Shin void f2fs_set_inode_flags(struct inode *inode);
3336704956ecSChao Yu bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page);
3337704956ecSChao Yu void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page);
3338cac5a3d8SDongOh Shin struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
3339cac5a3d8SDongOh Shin struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
33404d57b86dSChao Yu int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
33414d57b86dSChao Yu void f2fs_update_inode(struct inode *inode, struct page *node_page);
33424d57b86dSChao Yu void f2fs_update_inode_page(struct inode *inode);
3343cac5a3d8SDongOh Shin int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
3344cac5a3d8SDongOh Shin void f2fs_evict_inode(struct inode *inode);
33454d57b86dSChao Yu void f2fs_handle_failed_inode(struct inode *inode);
334639a53e0cSJaegeuk Kim 
334739a53e0cSJaegeuk Kim /*
334839a53e0cSJaegeuk Kim  * namei.c
334939a53e0cSJaegeuk Kim  */
33504d57b86dSChao Yu int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
3351b6a06cbbSChao Yu 							bool hot, bool set);
335239a53e0cSJaegeuk Kim struct dentry *f2fs_get_parent(struct dentry *child);
335339a53e0cSJaegeuk Kim 
335439a53e0cSJaegeuk Kim /*
335539a53e0cSJaegeuk Kim  * dir.c
335639a53e0cSJaegeuk Kim  */
33574d57b86dSChao Yu unsigned char f2fs_get_de_type(struct f2fs_dir_entry *de);
335843c780baSEric Biggers int f2fs_init_casefolded_name(const struct inode *dir,
335943c780baSEric Biggers 			      struct f2fs_filename *fname);
336043c780baSEric Biggers int f2fs_setup_filename(struct inode *dir, const struct qstr *iname,
336143c780baSEric Biggers 			int lookup, struct f2fs_filename *fname);
336243c780baSEric Biggers int f2fs_prepare_lookup(struct inode *dir, struct dentry *dentry,
336343c780baSEric Biggers 			struct f2fs_filename *fname);
336443c780baSEric Biggers void f2fs_free_filename(struct f2fs_filename *fname);
336543c780baSEric Biggers struct f2fs_dir_entry *f2fs_find_target_dentry(const struct f2fs_dentry_ptr *d,
336643c780baSEric Biggers 			const struct f2fs_filename *fname, int *max_slots);
3367cac5a3d8SDongOh Shin int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
3368cac5a3d8SDongOh Shin 			unsigned int start_pos, struct fscrypt_str *fstr);
33694d57b86dSChao Yu void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
3370cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
33714d57b86dSChao Yu struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
337243c780baSEric Biggers 			const struct f2fs_filename *fname, struct page *dpage);
33734d57b86dSChao Yu void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
3374cac5a3d8SDongOh Shin 			unsigned int current_depth);
33754d57b86dSChao Yu int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots);
3376cac5a3d8SDongOh Shin void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
3377cac5a3d8SDongOh Shin struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
337843c780baSEric Biggers 					 const struct f2fs_filename *fname,
337943c780baSEric Biggers 					 struct page **res_page);
3380cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
3381cac5a3d8SDongOh Shin 			const struct qstr *child, struct page **res_page);
3382cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
3383cac5a3d8SDongOh Shin ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
3384cac5a3d8SDongOh Shin 			struct page **page);
3385cac5a3d8SDongOh Shin void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
3386cac5a3d8SDongOh Shin 			struct page *page, struct inode *inode);
3387b06af2afSJaegeuk Kim bool f2fs_has_enough_room(struct inode *dir, struct page *ipage,
338843c780baSEric Biggers 			  const struct f2fs_filename *fname);
3389cac5a3d8SDongOh Shin void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
339043c780baSEric Biggers 			const struct fscrypt_str *name, f2fs_hash_t name_hash,
3391cac5a3d8SDongOh Shin 			unsigned int bit_pos);
339243c780baSEric Biggers int f2fs_add_regular_entry(struct inode *dir, const struct f2fs_filename *fname,
3393cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
339443c780baSEric Biggers int f2fs_add_dentry(struct inode *dir, const struct f2fs_filename *fname,
3395cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
33964d57b86dSChao Yu int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
3397cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
3398cac5a3d8SDongOh Shin void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
3399cac5a3d8SDongOh Shin 			struct inode *dir, struct inode *inode);
3400cac5a3d8SDongOh Shin int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
3401cac5a3d8SDongOh Shin bool f2fs_empty_dir(struct inode *dir);
340239a53e0cSJaegeuk Kim 
3403b7f7a5e0SAl Viro static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
3404b7f7a5e0SAl Viro {
3405bfc2b7e8SEric Biggers 	if (fscrypt_is_nokey_name(dentry))
3406bfc2b7e8SEric Biggers 		return -ENOKEY;
34074d57b86dSChao Yu 	return f2fs_do_add_link(d_inode(dentry->d_parent), &dentry->d_name,
3408510022a8SJaegeuk Kim 				inode, inode->i_ino, inode->i_mode);
3409b7f7a5e0SAl Viro }
3410b7f7a5e0SAl Viro 
341139a53e0cSJaegeuk Kim /*
341239a53e0cSJaegeuk Kim  * super.c
341339a53e0cSJaegeuk Kim  */
3414cac5a3d8SDongOh Shin int f2fs_inode_dirtied(struct inode *inode, bool sync);
3415cac5a3d8SDongOh Shin void f2fs_inode_synced(struct inode *inode);
3416ea676733SJaegeuk Kim int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly);
3417af033b2aSChao Yu int f2fs_quota_sync(struct super_block *sb, int type);
34186d1451bfSChengguang Xu loff_t max_file_blocks(struct inode *inode);
34194b2414d0SChao Yu void f2fs_quota_off_umount(struct super_block *sb);
3420cac5a3d8SDongOh Shin int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
3421cac5a3d8SDongOh Shin int f2fs_sync_fs(struct super_block *sb, int sync);
34224d57b86dSChao Yu int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi);
342339a53e0cSJaegeuk Kim 
342439a53e0cSJaegeuk Kim /*
342539a53e0cSJaegeuk Kim  * hash.c
342639a53e0cSJaegeuk Kim  */
342743c780baSEric Biggers void f2fs_hash_filename(const struct inode *dir, struct f2fs_filename *fname);
342839a53e0cSJaegeuk Kim 
342939a53e0cSJaegeuk Kim /*
343039a53e0cSJaegeuk Kim  * node.c
343139a53e0cSJaegeuk Kim  */
343239a53e0cSJaegeuk Kim struct node_info;
343339a53e0cSJaegeuk Kim 
34344d57b86dSChao Yu int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid);
34354d57b86dSChao Yu bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type);
343650fa53ecSChao Yu bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct page *page);
343750fa53ecSChao Yu void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi);
343850fa53ecSChao Yu void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct page *page);
343950fa53ecSChao Yu void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi);
34404d57b86dSChao Yu int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
34414d57b86dSChao Yu bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
34424d57b86dSChao Yu bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
34437735730dSChao Yu int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
34444d57b86dSChao Yu 						struct node_info *ni);
34454d57b86dSChao Yu pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
34464d57b86dSChao Yu int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
34474d57b86dSChao Yu int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from);
34484d57b86dSChao Yu int f2fs_truncate_xattr_node(struct inode *inode);
344950fa53ecSChao Yu int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
345050fa53ecSChao Yu 					unsigned int seq_id);
34514d57b86dSChao Yu int f2fs_remove_inode_page(struct inode *inode);
34524d57b86dSChao Yu struct page *f2fs_new_inode_page(struct inode *inode);
34534d57b86dSChao Yu struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs);
34544d57b86dSChao Yu void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
34554d57b86dSChao Yu struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
34564d57b86dSChao Yu struct page *f2fs_get_node_page_ra(struct page *parent, int start);
345748018b4cSChao Yu int f2fs_move_node_page(struct page *node_page, int gc_type);
345868e79bafSJia Yang void f2fs_flush_inline_data(struct f2fs_sb_info *sbi);
34594d57b86dSChao Yu int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
346050fa53ecSChao Yu 			struct writeback_control *wbc, bool atomic,
346150fa53ecSChao Yu 			unsigned int *seq_id);
34624d57b86dSChao Yu int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
34634d57b86dSChao Yu 			struct writeback_control *wbc,
3464b0af6d49SChao Yu 			bool do_balance, enum iostat_type io_type);
3465e2374015SChao Yu int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
34664d57b86dSChao Yu bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
34674d57b86dSChao Yu void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
34684d57b86dSChao Yu void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
34694d57b86dSChao Yu int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
34709627a7b3SChao Yu int f2fs_recover_inline_xattr(struct inode *inode, struct page *page);
34714d57b86dSChao Yu int f2fs_recover_xattr_data(struct inode *inode, struct page *page);
34724d57b86dSChao Yu int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
34737735730dSChao Yu int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
3474cac5a3d8SDongOh Shin 			unsigned int segno, struct f2fs_summary_block *sum);
3475edc55aafSJaegeuk Kim int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
34764d57b86dSChao Yu int f2fs_build_node_manager(struct f2fs_sb_info *sbi);
34774d57b86dSChao Yu void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi);
34784d57b86dSChao Yu int __init f2fs_create_node_manager_caches(void);
34794d57b86dSChao Yu void f2fs_destroy_node_manager_caches(void);
348039a53e0cSJaegeuk Kim 
348139a53e0cSJaegeuk Kim /*
348239a53e0cSJaegeuk Kim  * segment.c
348339a53e0cSJaegeuk Kim  */
34844d57b86dSChao Yu bool f2fs_need_SSR(struct f2fs_sb_info *sbi);
34854d57b86dSChao Yu void f2fs_register_inmem_page(struct inode *inode, struct page *page);
34864d57b86dSChao Yu void f2fs_drop_inmem_pages_all(struct f2fs_sb_info *sbi, bool gc_failure);
34874d57b86dSChao Yu void f2fs_drop_inmem_pages(struct inode *inode);
34884d57b86dSChao Yu void f2fs_drop_inmem_page(struct inode *inode, struct page *page);
34894d57b86dSChao Yu int f2fs_commit_inmem_pages(struct inode *inode);
3490cac5a3d8SDongOh Shin void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
34917bcd0cfaSChao Yu void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi, bool from_bg);
349239d787beSChao Yu int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
34934d57b86dSChao Yu int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
34941228b482SChao Yu int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
34954d57b86dSChao Yu void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
34964d57b86dSChao Yu void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
34974d57b86dSChao Yu bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
34984d57b86dSChao Yu void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
34994d57b86dSChao Yu void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi);
350003f2c02dSJaegeuk Kim bool f2fs_issue_discard_timeout(struct f2fs_sb_info *sbi);
35014d57b86dSChao Yu void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
35024d57b86dSChao Yu 					struct cp_control *cpc);
35034354994fSDaniel Rosenberg void f2fs_dirty_to_prefree(struct f2fs_sb_info *sbi);
35044d3aed70SDaniel Rosenberg block_t f2fs_get_unusable_blocks(struct f2fs_sb_info *sbi);
35054d3aed70SDaniel Rosenberg int f2fs_disable_cp_again(struct f2fs_sb_info *sbi, block_t unusable);
35064d57b86dSChao Yu void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi);
35074d57b86dSChao Yu int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
350861461fc9SChao Yu bool f2fs_segment_has_free_slot(struct f2fs_sb_info *sbi, int segno);
3509093749e2SChao Yu void f2fs_init_inmem_curseg(struct f2fs_sb_info *sbi);
3510093749e2SChao Yu void f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi);
3511093749e2SChao Yu void f2fs_restore_inmem_curseg(struct f2fs_sb_info *sbi);
3512093749e2SChao Yu void f2fs_get_new_segment(struct f2fs_sb_info *sbi,
3513093749e2SChao Yu 			unsigned int *newseg, bool new_sec, int dir);
35140ef81833SChao Yu void f2fs_allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type,
351504f0b2eaSQiuyang Sun 					unsigned int start, unsigned int end);
3516509f1010SChao Yu void f2fs_allocate_new_section(struct f2fs_sb_info *sbi, int type, bool force);
3517901d745fSChao Yu void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi);
3518cac5a3d8SDongOh Shin int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
35194d57b86dSChao Yu bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
35204d57b86dSChao Yu 					struct cp_control *cpc);
35214d57b86dSChao Yu struct page *f2fs_get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
35224d57b86dSChao Yu void f2fs_update_meta_page(struct f2fs_sb_info *sbi, void *src,
35234d57b86dSChao Yu 					block_t blk_addr);
35244d57b86dSChao Yu void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
3525b0af6d49SChao Yu 						enum iostat_type io_type);
35264d57b86dSChao Yu void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio);
35274d57b86dSChao Yu void f2fs_outplace_write_data(struct dnode_of_data *dn,
35284d57b86dSChao Yu 			struct f2fs_io_info *fio);
35294d57b86dSChao Yu int f2fs_inplace_write_data(struct f2fs_io_info *fio);
35304d57b86dSChao Yu void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
3531cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t new_blkaddr,
3532c5d02785SChao Yu 			bool recover_curseg, bool recover_newaddr,
3533c5d02785SChao Yu 			bool from_gc);
3534cac5a3d8SDongOh Shin void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
3535cac5a3d8SDongOh Shin 			block_t old_addr, block_t new_addr,
3536cac5a3d8SDongOh Shin 			unsigned char version, bool recover_curseg,
3537cac5a3d8SDongOh Shin 			bool recover_newaddr);
35384d57b86dSChao Yu void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
3539cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t *new_blkaddr,
3540fb830fc5SChao Yu 			struct f2fs_summary *sum, int type,
3541f608c38cSChao Yu 			struct f2fs_io_info *fio);
3542cac5a3d8SDongOh Shin void f2fs_wait_on_page_writeback(struct page *page,
3543bae0ee7aSChao Yu 			enum page_type type, bool ordered, bool locked);
35440ded69f6SJaegeuk Kim void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr);
35451e78e8bdSSahitya Tummala void f2fs_wait_on_block_writeback_range(struct inode *inode, block_t blkaddr,
35461e78e8bdSSahitya Tummala 								block_t len);
35474d57b86dSChao Yu void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
35484d57b86dSChao Yu void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
35494d57b86dSChao Yu int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
3550cac5a3d8SDongOh Shin 			unsigned int val, int alloc);
35514d57b86dSChao Yu void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
3552c426d991SShin'ichiro Kawasaki int f2fs_fix_curseg_write_pointer(struct f2fs_sb_info *sbi);
3553d508c94eSShin'ichiro Kawasaki int f2fs_check_write_pointer(struct f2fs_sb_info *sbi);
35544d57b86dSChao Yu int f2fs_build_segment_manager(struct f2fs_sb_info *sbi);
35554d57b86dSChao Yu void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi);
35564d57b86dSChao Yu int __init f2fs_create_segment_manager_caches(void);
35574d57b86dSChao Yu void f2fs_destroy_segment_manager_caches(void);
35584d57b86dSChao Yu int f2fs_rw_hint_to_seg_type(enum rw_hint hint);
35594d57b86dSChao Yu enum rw_hint f2fs_io_type_to_rw_hint(struct f2fs_sb_info *sbi,
35604d57b86dSChao Yu 			enum page_type type, enum temp_type temp);
3561de881df9SAravind Ramesh unsigned int f2fs_usable_segs_in_sec(struct f2fs_sb_info *sbi,
3562de881df9SAravind Ramesh 			unsigned int segno);
3563de881df9SAravind Ramesh unsigned int f2fs_usable_blks_in_seg(struct f2fs_sb_info *sbi,
3564de881df9SAravind Ramesh 			unsigned int segno);
356539a53e0cSJaegeuk Kim 
356639a53e0cSJaegeuk Kim /*
356739a53e0cSJaegeuk Kim  * checkpoint.c
356839a53e0cSJaegeuk Kim  */
3569cac5a3d8SDongOh Shin void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io);
35704d57b86dSChao Yu struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
35714d57b86dSChao Yu struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
357286f33603SJaegeuk Kim struct page *f2fs_get_meta_page_retry(struct f2fs_sb_info *sbi, pgoff_t index);
35734d57b86dSChao Yu struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
3574e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
35754d57b86dSChao Yu 					block_t blkaddr, int type);
35764d57b86dSChao Yu int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
3577cac5a3d8SDongOh Shin 			int type, bool sync);
35784d57b86dSChao Yu void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index);
35794d57b86dSChao Yu long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
3580b0af6d49SChao Yu 			long nr_to_write, enum iostat_type io_type);
35814d57b86dSChao Yu void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
35824d57b86dSChao Yu void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
35834d57b86dSChao Yu void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all);
35844d57b86dSChao Yu bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
35854d57b86dSChao Yu void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
358639d787beSChao Yu 					unsigned int devidx, int type);
35874d57b86dSChao Yu bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
358839d787beSChao Yu 					unsigned int devidx, int type);
3589cac5a3d8SDongOh Shin int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
35904d57b86dSChao Yu int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi);
35914d57b86dSChao Yu void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi);
35924d57b86dSChao Yu void f2fs_add_orphan_inode(struct inode *inode);
35934d57b86dSChao Yu void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
35944d57b86dSChao Yu int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi);
35954d57b86dSChao Yu int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi);
35964d57b86dSChao Yu void f2fs_update_dirty_page(struct inode *inode, struct page *page);
35974d57b86dSChao Yu void f2fs_remove_dirty_inode(struct inode *inode);
35984d57b86dSChao Yu int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
3599bf22c3ccSSahitya Tummala void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type);
36003a0a9cbcSChao Yu u64 f2fs_get_sectors_written(struct f2fs_sb_info *sbi);
36014d57b86dSChao Yu int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
36024d57b86dSChao Yu void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi);
36034d57b86dSChao Yu int __init f2fs_create_checkpoint_caches(void);
36044d57b86dSChao Yu void f2fs_destroy_checkpoint_caches(void);
3605261eeb9cSDaeho Jeong int f2fs_issue_checkpoint(struct f2fs_sb_info *sbi);
3606261eeb9cSDaeho Jeong int f2fs_start_ckpt_thread(struct f2fs_sb_info *sbi);
3607261eeb9cSDaeho Jeong void f2fs_stop_ckpt_thread(struct f2fs_sb_info *sbi);
3608261eeb9cSDaeho Jeong void f2fs_init_ckpt_req_control(struct f2fs_sb_info *sbi);
360939a53e0cSJaegeuk Kim 
361039a53e0cSJaegeuk Kim /*
361139a53e0cSJaegeuk Kim  * data.c
361239a53e0cSJaegeuk Kim  */
3613f543805fSChao Yu int __init f2fs_init_bioset(void);
3614f543805fSChao Yu void f2fs_destroy_bioset(void);
36150b20fcecSChao Yu int f2fs_init_bio_entry_cache(void);
36160b20fcecSChao Yu void f2fs_destroy_bio_entry_cache(void);
36174c8ff709SChao Yu void f2fs_submit_bio(struct f2fs_sb_info *sbi,
36184c8ff709SChao Yu 				struct bio *bio, enum page_type type);
3619b9109b0eSJaegeuk Kim void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
3620b9109b0eSJaegeuk Kim void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
3621bab475c5SChao Yu 				struct inode *inode, struct page *page,
3622bab475c5SChao Yu 				nid_t ino, enum page_type type);
36230b20fcecSChao Yu void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
36240b20fcecSChao Yu 					struct bio **bio, struct page *page);
3625b9109b0eSJaegeuk Kim void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
3626cac5a3d8SDongOh Shin int f2fs_submit_page_bio(struct f2fs_io_info *fio);
36278648de2cSChao Yu int f2fs_merge_page_bio(struct f2fs_io_info *fio);
3628fe16efe6SChao Yu void f2fs_submit_page_write(struct f2fs_io_info *fio);
3629cac5a3d8SDongOh Shin struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
3630cac5a3d8SDongOh Shin 			block_t blk_addr, struct bio *bio);
3631cac5a3d8SDongOh Shin int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
36324d57b86dSChao Yu void f2fs_set_data_blkaddr(struct dnode_of_data *dn);
3633cac5a3d8SDongOh Shin void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
36344d57b86dSChao Yu int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
36354d57b86dSChao Yu int f2fs_reserve_new_block(struct dnode_of_data *dn);
3636cac5a3d8SDongOh Shin int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
3637cac5a3d8SDongOh Shin int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from);
3638cac5a3d8SDongOh Shin int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
36394d57b86dSChao Yu struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
3640cac5a3d8SDongOh Shin 			int op_flags, bool for_write);
36414d57b86dSChao Yu struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index);
36424d57b86dSChao Yu struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
3643cac5a3d8SDongOh Shin 			bool for_write);
36444d57b86dSChao Yu struct page *f2fs_get_new_data_page(struct inode *inode,
3645cac5a3d8SDongOh Shin 			struct page *ipage, pgoff_t index, bool new_i_size);
36464d57b86dSChao Yu int f2fs_do_write_data_page(struct f2fs_io_info *fio);
36470ef81833SChao Yu void f2fs_do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock);
3648cac5a3d8SDongOh Shin int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
3649cac5a3d8SDongOh Shin 			int create, int flag);
3650cac5a3d8SDongOh Shin int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3651cac5a3d8SDongOh Shin 			u64 start, u64 len);
36524c8ff709SChao Yu int f2fs_encrypt_one_page(struct f2fs_io_info *fio);
36534d57b86dSChao Yu bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio);
36544d57b86dSChao Yu bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio);
36554c8ff709SChao Yu int f2fs_write_single_data_page(struct page *page, int *submitted,
36564c8ff709SChao Yu 				struct bio **bio, sector_t *last_block,
36574c8ff709SChao Yu 				struct writeback_control *wbc,
36584c8ff709SChao Yu 				enum iostat_type io_type,
36593afae09fSChao Yu 				int compr_blocks, bool allow_balance);
3660cac5a3d8SDongOh Shin void f2fs_invalidate_page(struct page *page, unsigned int offset,
3661cac5a3d8SDongOh Shin 			unsigned int length);
3662cac5a3d8SDongOh Shin int f2fs_release_page(struct page *page, gfp_t wait);
36635b7a487cSWeichao Guo #ifdef CONFIG_MIGRATION
3664cac5a3d8SDongOh Shin int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,
3665cac5a3d8SDongOh Shin 			struct page *page, enum migrate_mode mode);
36665b7a487cSWeichao Guo #endif
3667b91050a8SHyunchul Lee bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len);
36685ec2d99dSMatthew Wilcox void f2fs_clear_page_cache_dirty_tag(struct page *page);
36694c8ff709SChao Yu int f2fs_init_post_read_processing(void);
36704c8ff709SChao Yu void f2fs_destroy_post_read_processing(void);
36714c8ff709SChao Yu int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi);
36724c8ff709SChao Yu void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi);
367339a53e0cSJaegeuk Kim 
367439a53e0cSJaegeuk Kim /*
367539a53e0cSJaegeuk Kim  * gc.c
367639a53e0cSJaegeuk Kim  */
36774d57b86dSChao Yu int f2fs_start_gc_thread(struct f2fs_sb_info *sbi);
36784d57b86dSChao Yu void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi);
36794d57b86dSChao Yu block_t f2fs_start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
36807dede886SChao Yu int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background, bool force,
3681e066b83cSJaegeuk Kim 			unsigned int segno);
36824d57b86dSChao Yu void f2fs_build_gc_manager(struct f2fs_sb_info *sbi);
368304f0b2eaSQiuyang Sun int f2fs_resize_fs(struct f2fs_sb_info *sbi, __u64 block_count);
3684093749e2SChao Yu int __init f2fs_create_garbage_collection_cache(void);
3685093749e2SChao Yu void f2fs_destroy_garbage_collection_cache(void);
368639a53e0cSJaegeuk Kim 
368739a53e0cSJaegeuk Kim /*
368839a53e0cSJaegeuk Kim  * recovery.c
368939a53e0cSJaegeuk Kim  */
36904d57b86dSChao Yu int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
36914d57b86dSChao Yu bool f2fs_space_for_roll_forward(struct f2fs_sb_info *sbi);
3692cad83c96SChao Yu int __init f2fs_create_recovery_cache(void);
3693cad83c96SChao Yu void f2fs_destroy_recovery_cache(void);
369439a53e0cSJaegeuk Kim 
369539a53e0cSJaegeuk Kim /*
369639a53e0cSJaegeuk Kim  * debug.c
369739a53e0cSJaegeuk Kim  */
369839a53e0cSJaegeuk Kim #ifdef CONFIG_F2FS_STAT_FS
369939a53e0cSJaegeuk Kim struct f2fs_stat_info {
370039a53e0cSJaegeuk Kim 	struct list_head stat_list;
370139a53e0cSJaegeuk Kim 	struct f2fs_sb_info *sbi;
370239a53e0cSJaegeuk Kim 	int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
370339a53e0cSJaegeuk Kim 	int main_area_segs, main_area_sections, main_area_zones;
37045b7ee374SChao Yu 	unsigned long long hit_largest, hit_cached, hit_rbtree;
37055b7ee374SChao Yu 	unsigned long long hit_total, total_ext;
3706c00ba554SJaegeuk Kim 	int ext_tree, zombie_tree, ext_node;
37072c8a4a28SJaegeuk Kim 	int ndirty_node, ndirty_dent, ndirty_meta, ndirty_imeta;
37082c8a4a28SJaegeuk Kim 	int ndirty_data, ndirty_qdata;
370935782b23SJaegeuk Kim 	int inmem_pages;
37102c8a4a28SJaegeuk Kim 	unsigned int ndirty_dirs, ndirty_files, nquota_files, ndirty_all;
37115b0ef73cSJaegeuk Kim 	int nats, dirty_nats, sits, dirty_sits;
37125b0ef73cSJaegeuk Kim 	int free_nids, avail_nids, alloc_nids;
371339a53e0cSJaegeuk Kim 	int total_count, utilization;
37148b8dd65fSChao Yu 	int bg_gc, nr_wb_cp_data, nr_wb_data;
37155f9abab4SJaegeuk Kim 	int nr_rd_data, nr_rd_node, nr_rd_meta;
371602b16d0aSChao Yu 	int nr_dio_read, nr_dio_write;
3717274bd9baSChao Yu 	unsigned int io_skip_bggc, other_skip_bggc;
371814d8d5f7SChao Yu 	int nr_flushing, nr_flushed, flush_list_empty;
371914d8d5f7SChao Yu 	int nr_discarding, nr_discarded;
37205f32366aSChao Yu 	int nr_discard_cmd;
3721d84d1cbdSChao Yu 	unsigned int undiscard_blks;
3722261eeb9cSDaeho Jeong 	int nr_issued_ckpt, nr_total_ckpt, nr_queued_ckpt;
3723261eeb9cSDaeho Jeong 	unsigned int cur_ckpt_time, peak_ckpt_time;
3724a00861dbSJaegeuk Kim 	int inline_xattr, inline_inode, inline_dir, append, update, orphans;
3725ae999bb9SDaeho Jeong 	int compr_inode;
3726ae999bb9SDaeho Jeong 	unsigned long long compr_blocks;
3727648d50baSChao Yu 	int aw_cnt, max_aw_cnt, vw_cnt, max_vw_cnt;
3728f83a2584SYunlei He 	unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
372939a53e0cSJaegeuk Kim 	unsigned int bimodal, avg_vblocks;
373039a53e0cSJaegeuk Kim 	int util_free, util_valid, util_invalid;
373139a53e0cSJaegeuk Kim 	int rsvd_segs, overp_segs;
37326ce19affSChao Yu 	int dirty_count, node_pages, meta_pages, compress_pages;
37336ce19affSChao Yu 	int compress_page_hit;
373442190d2aSJaegeuk Kim 	int prefree_count, call_count, cp_count, bg_cp_count;
373539a53e0cSJaegeuk Kim 	int tot_segs, node_segs, data_segs, free_segs, free_secs;
3736e1235983SChangman Lee 	int bg_node_segs, bg_data_segs;
373739a53e0cSJaegeuk Kim 	int tot_blks, data_blks, node_blks;
3738e1235983SChangman Lee 	int bg_data_blks, bg_node_blks;
37392ef79ecbSChao Yu 	unsigned long long skipped_atomic_files[2];
374039a53e0cSJaegeuk Kim 	int curseg[NR_CURSEG_TYPE];
374139a53e0cSJaegeuk Kim 	int cursec[NR_CURSEG_TYPE];
374239a53e0cSJaegeuk Kim 	int curzone[NR_CURSEG_TYPE];
37430759e2c1SChao Yu 	unsigned int dirty_seg[NR_CURSEG_TYPE];
37440759e2c1SChao Yu 	unsigned int full_seg[NR_CURSEG_TYPE];
37450759e2c1SChao Yu 	unsigned int valid_blks[NR_CURSEG_TYPE];
374639a53e0cSJaegeuk Kim 
3747b63e7be5SChao Yu 	unsigned int meta_count[META_MAX];
374839a53e0cSJaegeuk Kim 	unsigned int segment_count[2];
374939a53e0cSJaegeuk Kim 	unsigned int block_count[2];
3750b9a2c252SChangman Lee 	unsigned int inplace_count;
37519edcdabfSChao Yu 	unsigned long long base_mem, cache_mem, page_mem;
375239a53e0cSJaegeuk Kim };
375339a53e0cSJaegeuk Kim 
3754963d4f7dSGu Zheng static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
3755963d4f7dSGu Zheng {
3756963d4f7dSGu Zheng 	return (struct f2fs_stat_info *)sbi->stat_info;
3757963d4f7dSGu Zheng }
3758963d4f7dSGu Zheng 
3759942e0be6SChangman Lee #define stat_inc_cp_count(si)		((si)->cp_count++)
376042190d2aSJaegeuk Kim #define stat_inc_bg_cp_count(si)	((si)->bg_cp_count++)
376139a53e0cSJaegeuk Kim #define stat_inc_call_count(si)		((si)->call_count++)
3762fc7100eaSHridya Valsaraju #define stat_inc_bggc_count(si)		((si)->bg_gc++)
3763274bd9baSChao Yu #define stat_io_skip_bggc_count(sbi)	((sbi)->io_skip_bggc++)
3764274bd9baSChao Yu #define stat_other_skip_bggc_count(sbi)	((sbi)->other_skip_bggc++)
376533fbd510SChao Yu #define stat_inc_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]++)
376633fbd510SChao Yu #define stat_dec_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]--)
37675b7ee374SChao Yu #define stat_inc_total_hit(sbi)		(atomic64_inc(&(sbi)->total_hit_ext))
37685b7ee374SChao Yu #define stat_inc_rbtree_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_rbtree))
37695b7ee374SChao Yu #define stat_inc_largest_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_largest))
37705b7ee374SChao Yu #define stat_inc_cached_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_cached))
3771d5e8f6c9SChao Yu #define stat_inc_inline_xattr(inode)					\
3772d5e8f6c9SChao Yu 	do {								\
3773d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3774d5e8f6c9SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_xattr));	\
3775d5e8f6c9SChao Yu 	} while (0)
3776d5e8f6c9SChao Yu #define stat_dec_inline_xattr(inode)					\
3777d5e8f6c9SChao Yu 	do {								\
3778d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3779d5e8f6c9SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_xattr));	\
3780d5e8f6c9SChao Yu 	} while (0)
37810dbdc2aeSJaegeuk Kim #define stat_inc_inline_inode(inode)					\
37820dbdc2aeSJaegeuk Kim 	do {								\
37830dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
378403e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_inode));	\
37850dbdc2aeSJaegeuk Kim 	} while (0)
37860dbdc2aeSJaegeuk Kim #define stat_dec_inline_inode(inode)					\
37870dbdc2aeSJaegeuk Kim 	do {								\
37880dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
378903e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_inode));	\
37900dbdc2aeSJaegeuk Kim 	} while (0)
37913289c061SJaegeuk Kim #define stat_inc_inline_dir(inode)					\
37923289c061SJaegeuk Kim 	do {								\
37933289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
379403e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_dir));	\
37953289c061SJaegeuk Kim 	} while (0)
37963289c061SJaegeuk Kim #define stat_dec_inline_dir(inode)					\
37973289c061SJaegeuk Kim 	do {								\
37983289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
379903e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_dir));	\
38003289c061SJaegeuk Kim 	} while (0)
38014c8ff709SChao Yu #define stat_inc_compr_inode(inode)					\
38024c8ff709SChao Yu 	do {								\
38034c8ff709SChao Yu 		if (f2fs_compressed_file(inode))			\
38044c8ff709SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->compr_inode));	\
38054c8ff709SChao Yu 	} while (0)
38064c8ff709SChao Yu #define stat_dec_compr_inode(inode)					\
38074c8ff709SChao Yu 	do {								\
38084c8ff709SChao Yu 		if (f2fs_compressed_file(inode))			\
38094c8ff709SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->compr_inode));	\
38104c8ff709SChao Yu 	} while (0)
38114c8ff709SChao Yu #define stat_add_compr_blocks(inode, blocks)				\
3812ae999bb9SDaeho Jeong 		(atomic64_add(blocks, &F2FS_I_SB(inode)->compr_blocks))
38134c8ff709SChao Yu #define stat_sub_compr_blocks(inode, blocks)				\
3814ae999bb9SDaeho Jeong 		(atomic64_sub(blocks, &F2FS_I_SB(inode)->compr_blocks))
3815b63e7be5SChao Yu #define stat_inc_meta_count(sbi, blkaddr)				\
3816b63e7be5SChao Yu 	do {								\
3817b63e7be5SChao Yu 		if (blkaddr < SIT_I(sbi)->sit_base_addr)		\
3818b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_CP]);	\
3819b63e7be5SChao Yu 		else if (blkaddr < NM_I(sbi)->nat_blkaddr)		\
3820b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_SIT]);	\
3821b63e7be5SChao Yu 		else if (blkaddr < SM_I(sbi)->ssa_blkaddr)		\
3822b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_NAT]);	\
3823b63e7be5SChao Yu 		else if (blkaddr < SM_I(sbi)->main_blkaddr)		\
3824b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_SSA]);	\
3825b63e7be5SChao Yu 	} while (0)
3826dcdfff65SJaegeuk Kim #define stat_inc_seg_type(sbi, curseg)					\
3827dcdfff65SJaegeuk Kim 		((sbi)->segment_count[(curseg)->alloc_type]++)
3828dcdfff65SJaegeuk Kim #define stat_inc_block_count(sbi, curseg)				\
3829dcdfff65SJaegeuk Kim 		((sbi)->block_count[(curseg)->alloc_type]++)
3830b9a2c252SChangman Lee #define stat_inc_inplace_blocks(sbi)					\
3831b9a2c252SChangman Lee 		(atomic_inc(&(sbi)->inplace_count))
383226a28a0cSJaegeuk Kim #define stat_update_max_atomic_write(inode)				\
383326a28a0cSJaegeuk Kim 	do {								\
38340e6d0164SSahitya Tummala 		int cur = F2FS_I_SB(inode)->atomic_files;	\
383526a28a0cSJaegeuk Kim 		int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt);	\
383626a28a0cSJaegeuk Kim 		if (cur > max)						\
383726a28a0cSJaegeuk Kim 			atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur);	\
383826a28a0cSJaegeuk Kim 	} while (0)
3839648d50baSChao Yu #define stat_inc_volatile_write(inode)					\
3840648d50baSChao Yu 		(atomic_inc(&F2FS_I_SB(inode)->vw_cnt))
3841648d50baSChao Yu #define stat_dec_volatile_write(inode)					\
3842648d50baSChao Yu 		(atomic_dec(&F2FS_I_SB(inode)->vw_cnt))
3843648d50baSChao Yu #define stat_update_max_volatile_write(inode)				\
3844648d50baSChao Yu 	do {								\
3845648d50baSChao Yu 		int cur = atomic_read(&F2FS_I_SB(inode)->vw_cnt);	\
3846648d50baSChao Yu 		int max = atomic_read(&F2FS_I_SB(inode)->max_vw_cnt);	\
3847648d50baSChao Yu 		if (cur > max)						\
3848648d50baSChao Yu 			atomic_set(&F2FS_I_SB(inode)->max_vw_cnt, cur);	\
3849648d50baSChao Yu 	} while (0)
3850e1235983SChangman Lee #define stat_inc_seg_count(sbi, type, gc_type)				\
385139a53e0cSJaegeuk Kim 	do {								\
3852963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
385368afcf2dSTomohiro Kusumi 		si->tot_segs++;						\
385468afcf2dSTomohiro Kusumi 		if ((type) == SUM_TYPE_DATA) {				\
385539a53e0cSJaegeuk Kim 			si->data_segs++;				\
3856e1235983SChangman Lee 			si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0;	\
3857e1235983SChangman Lee 		} else {						\
385839a53e0cSJaegeuk Kim 			si->node_segs++;				\
3859e1235983SChangman Lee 			si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0;	\
3860e1235983SChangman Lee 		}							\
386139a53e0cSJaegeuk Kim 	} while (0)
386239a53e0cSJaegeuk Kim 
386339a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks)				\
386468afcf2dSTomohiro Kusumi 	((si)->tot_blks += (blks))
386539a53e0cSJaegeuk Kim 
3866e1235983SChangman Lee #define stat_inc_data_blk_count(sbi, blks, gc_type)			\
386739a53e0cSJaegeuk Kim 	do {								\
3868963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
386939a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
387039a53e0cSJaegeuk Kim 		si->data_blks += (blks);				\
387168afcf2dSTomohiro Kusumi 		si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
387239a53e0cSJaegeuk Kim 	} while (0)
387339a53e0cSJaegeuk Kim 
3874e1235983SChangman Lee #define stat_inc_node_blk_count(sbi, blks, gc_type)			\
387539a53e0cSJaegeuk Kim 	do {								\
3876963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
387739a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
387839a53e0cSJaegeuk Kim 		si->node_blks += (blks);				\
387968afcf2dSTomohiro Kusumi 		si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
388039a53e0cSJaegeuk Kim 	} while (0)
388139a53e0cSJaegeuk Kim 
3882cac5a3d8SDongOh Shin int f2fs_build_stats(struct f2fs_sb_info *sbi);
3883cac5a3d8SDongOh Shin void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
388421acc07dSGreg Kroah-Hartman void __init f2fs_create_root_stats(void);
38854589d25dSNamjae Jeon void f2fs_destroy_root_stats(void);
3886fc7100eaSHridya Valsaraju void f2fs_update_sit_info(struct f2fs_sb_info *sbi);
388739a53e0cSJaegeuk Kim #else
3888d66450e7SArnd Bergmann #define stat_inc_cp_count(si)				do { } while (0)
3889d66450e7SArnd Bergmann #define stat_inc_bg_cp_count(si)			do { } while (0)
3890d66450e7SArnd Bergmann #define stat_inc_call_count(si)				do { } while (0)
3891d66450e7SArnd Bergmann #define stat_inc_bggc_count(si)				do { } while (0)
3892274bd9baSChao Yu #define stat_io_skip_bggc_count(sbi)			do { } while (0)
3893274bd9baSChao Yu #define stat_other_skip_bggc_count(sbi)			do { } while (0)
3894d66450e7SArnd Bergmann #define stat_inc_dirty_inode(sbi, type)			do { } while (0)
3895d66450e7SArnd Bergmann #define stat_dec_dirty_inode(sbi, type)			do { } while (0)
3896fc7100eaSHridya Valsaraju #define stat_inc_total_hit(sbi)				do { } while (0)
3897fc7100eaSHridya Valsaraju #define stat_inc_rbtree_node_hit(sbi)			do { } while (0)
3898d66450e7SArnd Bergmann #define stat_inc_largest_node_hit(sbi)			do { } while (0)
3899d66450e7SArnd Bergmann #define stat_inc_cached_node_hit(sbi)			do { } while (0)
3900d66450e7SArnd Bergmann #define stat_inc_inline_xattr(inode)			do { } while (0)
3901d66450e7SArnd Bergmann #define stat_dec_inline_xattr(inode)			do { } while (0)
3902d66450e7SArnd Bergmann #define stat_inc_inline_inode(inode)			do { } while (0)
3903d66450e7SArnd Bergmann #define stat_dec_inline_inode(inode)			do { } while (0)
3904d66450e7SArnd Bergmann #define stat_inc_inline_dir(inode)			do { } while (0)
3905d66450e7SArnd Bergmann #define stat_dec_inline_dir(inode)			do { } while (0)
39064c8ff709SChao Yu #define stat_inc_compr_inode(inode)			do { } while (0)
39074c8ff709SChao Yu #define stat_dec_compr_inode(inode)			do { } while (0)
39084c8ff709SChao Yu #define stat_add_compr_blocks(inode, blocks)		do { } while (0)
39094c8ff709SChao Yu #define stat_sub_compr_blocks(inode, blocks)		do { } while (0)
3910d66450e7SArnd Bergmann #define stat_update_max_atomic_write(inode)		do { } while (0)
3911d66450e7SArnd Bergmann #define stat_inc_volatile_write(inode)			do { } while (0)
3912d66450e7SArnd Bergmann #define stat_dec_volatile_write(inode)			do { } while (0)
3913d66450e7SArnd Bergmann #define stat_update_max_volatile_write(inode)		do { } while (0)
3914b63e7be5SChao Yu #define stat_inc_meta_count(sbi, blkaddr)		do { } while (0)
3915d66450e7SArnd Bergmann #define stat_inc_seg_type(sbi, curseg)			do { } while (0)
3916d66450e7SArnd Bergmann #define stat_inc_block_count(sbi, curseg)		do { } while (0)
3917d66450e7SArnd Bergmann #define stat_inc_inplace_blocks(sbi)			do { } while (0)
3918d66450e7SArnd Bergmann #define stat_inc_seg_count(sbi, type, gc_type)		do { } while (0)
3919d66450e7SArnd Bergmann #define stat_inc_tot_blk_count(si, blks)		do { } while (0)
3920d66450e7SArnd Bergmann #define stat_inc_data_blk_count(sbi, blks, gc_type)	do { } while (0)
3921d66450e7SArnd Bergmann #define stat_inc_node_blk_count(sbi, blks, gc_type)	do { } while (0)
392239a53e0cSJaegeuk Kim 
392339a53e0cSJaegeuk Kim static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
392439a53e0cSJaegeuk Kim static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
392521acc07dSGreg Kroah-Hartman static inline void __init f2fs_create_root_stats(void) { }
39264589d25dSNamjae Jeon static inline void f2fs_destroy_root_stats(void) { }
392748abe91aSYueHaibing static inline void f2fs_update_sit_info(struct f2fs_sb_info *sbi) {}
392839a53e0cSJaegeuk Kim #endif
392939a53e0cSJaegeuk Kim 
393039a53e0cSJaegeuk Kim extern const struct file_operations f2fs_dir_operations;
393139a53e0cSJaegeuk Kim extern const struct file_operations f2fs_file_operations;
393239a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_file_inode_operations;
393339a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_dblock_aops;
393439a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_node_aops;
393539a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_meta_aops;
393639a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_dir_inode_operations;
393739a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_symlink_inode_operations;
3938cbaf042aSJaegeuk Kim extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
393939a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_special_inode_operations;
39404d57b86dSChao Yu extern struct kmem_cache *f2fs_inode_entry_slab;
39411001b347SHuajun Li 
3942e18c65b2SHuajun Li /*
3943e18c65b2SHuajun Li  * inline.c
3944e18c65b2SHuajun Li  */
3945cac5a3d8SDongOh Shin bool f2fs_may_inline_data(struct inode *inode);
3946cac5a3d8SDongOh Shin bool f2fs_may_inline_dentry(struct inode *inode);
39474d57b86dSChao Yu void f2fs_do_read_inline_data(struct page *page, struct page *ipage);
39484d57b86dSChao Yu void f2fs_truncate_inline_inode(struct inode *inode,
39494d57b86dSChao Yu 						struct page *ipage, u64 from);
3950cac5a3d8SDongOh Shin int f2fs_read_inline_data(struct inode *inode, struct page *page);
3951cac5a3d8SDongOh Shin int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
3952cac5a3d8SDongOh Shin int f2fs_convert_inline_inode(struct inode *inode);
3953b06af2afSJaegeuk Kim int f2fs_try_convert_inline_dir(struct inode *dir, struct dentry *dentry);
3954cac5a3d8SDongOh Shin int f2fs_write_inline_data(struct inode *inode, struct page *page);
39559627a7b3SChao Yu int f2fs_recover_inline_data(struct inode *inode, struct page *npage);
39564d57b86dSChao Yu struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
395743c780baSEric Biggers 					const struct f2fs_filename *fname,
395843c780baSEric Biggers 					struct page **res_page);
39594d57b86dSChao Yu int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent,
3960cac5a3d8SDongOh Shin 			struct page *ipage);
396143c780baSEric Biggers int f2fs_add_inline_entry(struct inode *dir, const struct f2fs_filename *fname,
3962cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
39634d57b86dSChao Yu void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry,
39644d57b86dSChao Yu 				struct page *page, struct inode *dir,
39654d57b86dSChao Yu 				struct inode *inode);
3966cac5a3d8SDongOh Shin bool f2fs_empty_inline_dir(struct inode *dir);
3967cac5a3d8SDongOh Shin int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
3968cac5a3d8SDongOh Shin 			struct fscrypt_str *fstr);
3969cac5a3d8SDongOh Shin int f2fs_inline_data_fiemap(struct inode *inode,
3970cac5a3d8SDongOh Shin 			struct fiemap_extent_info *fieinfo,
3971cac5a3d8SDongOh Shin 			__u64 start, __u64 len);
3972cde4de12SJaegeuk Kim 
3973cde4de12SJaegeuk Kim /*
39742658e50dSJaegeuk Kim  * shrinker.c
39752658e50dSJaegeuk Kim  */
3976cac5a3d8SDongOh Shin unsigned long f2fs_shrink_count(struct shrinker *shrink,
3977cac5a3d8SDongOh Shin 			struct shrink_control *sc);
3978cac5a3d8SDongOh Shin unsigned long f2fs_shrink_scan(struct shrinker *shrink,
3979cac5a3d8SDongOh Shin 			struct shrink_control *sc);
3980cac5a3d8SDongOh Shin void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
3981cac5a3d8SDongOh Shin void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
39822658e50dSJaegeuk Kim 
39832658e50dSJaegeuk Kim /*
3984a28ef1f5SChao Yu  * extent_cache.c
3985a28ef1f5SChao Yu  */
39864dada3fdSChao Yu struct rb_entry *f2fs_lookup_rb_tree(struct rb_root_cached *root,
3987004b6862SChao Yu 				struct rb_entry *cached_re, unsigned int ofs);
39882e9b2bb2SChao Yu struct rb_node **f2fs_lookup_rb_tree_ext(struct f2fs_sb_info *sbi,
39892e9b2bb2SChao Yu 				struct rb_root_cached *root,
39902e9b2bb2SChao Yu 				struct rb_node **parent,
39912e9b2bb2SChao Yu 				unsigned long long key, bool *left_most);
39924d57b86dSChao Yu struct rb_node **f2fs_lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
39934dada3fdSChao Yu 				struct rb_root_cached *root,
39944dada3fdSChao Yu 				struct rb_node **parent,
39954dada3fdSChao Yu 				unsigned int ofs, bool *leftmost);
39964dada3fdSChao Yu struct rb_entry *f2fs_lookup_rb_tree_ret(struct rb_root_cached *root,
3997004b6862SChao Yu 		struct rb_entry *cached_re, unsigned int ofs,
3998004b6862SChao Yu 		struct rb_entry **prev_entry, struct rb_entry **next_entry,
3999004b6862SChao Yu 		struct rb_node ***insert_p, struct rb_node **insert_parent,
40004dada3fdSChao Yu 		bool force, bool *leftmost);
40014d57b86dSChao Yu bool f2fs_check_rb_tree_consistence(struct f2fs_sb_info *sbi,
40022e9b2bb2SChao Yu 				struct rb_root_cached *root, bool check_key);
4003cac5a3d8SDongOh Shin unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
4004a6d601f3SChao Yu void f2fs_init_extent_tree(struct inode *inode, struct page *ipage);
4005cac5a3d8SDongOh Shin void f2fs_drop_extent_tree(struct inode *inode);
4006cac5a3d8SDongOh Shin unsigned int f2fs_destroy_extent_node(struct inode *inode);
4007cac5a3d8SDongOh Shin void f2fs_destroy_extent_tree(struct inode *inode);
4008cac5a3d8SDongOh Shin bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
4009cac5a3d8SDongOh Shin 			struct extent_info *ei);
4010cac5a3d8SDongOh Shin void f2fs_update_extent_cache(struct dnode_of_data *dn);
401119b2c30dSChao Yu void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
4012cac5a3d8SDongOh Shin 			pgoff_t fofs, block_t blkaddr, unsigned int len);
40134d57b86dSChao Yu void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi);
40144d57b86dSChao Yu int __init f2fs_create_extent_cache(void);
40154d57b86dSChao Yu void f2fs_destroy_extent_cache(void);
4016a28ef1f5SChao Yu 
4017a28ef1f5SChao Yu /*
40188ceffcb2SChao Yu  * sysfs.c
40198ceffcb2SChao Yu  */
40200f6b56ecSDaeho Jeong #define MIN_RA_MUL	2
40210f6b56ecSDaeho Jeong #define MAX_RA_MUL	256
40220f6b56ecSDaeho Jeong 
4023dc6b2055SJaegeuk Kim int __init f2fs_init_sysfs(void);
4024dc6b2055SJaegeuk Kim void f2fs_exit_sysfs(void);
4025dc6b2055SJaegeuk Kim int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
4026dc6b2055SJaegeuk Kim void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
40278ceffcb2SChao Yu 
402895ae251fSEric Biggers /* verity.c */
402995ae251fSEric Biggers extern const struct fsverity_operations f2fs_verityops;
403095ae251fSEric Biggers 
40318ceffcb2SChao Yu /*
4032cde4de12SJaegeuk Kim  * crypto support
4033cde4de12SJaegeuk Kim  */
40341958593eSJaegeuk Kim static inline bool f2fs_encrypted_file(struct inode *inode)
40351958593eSJaegeuk Kim {
403662230e0dSChandan Rajendra 	return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
40371958593eSJaegeuk Kim }
40381958593eSJaegeuk Kim 
4039cde4de12SJaegeuk Kim static inline void f2fs_set_encrypted_inode(struct inode *inode)
4040cde4de12SJaegeuk Kim {
4041643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
4042cde4de12SJaegeuk Kim 	file_set_encrypt(inode);
40439149a5ebSChao Yu 	f2fs_set_inode_flags(inode);
4044cde4de12SJaegeuk Kim #endif
4045cde4de12SJaegeuk Kim }
4046cde4de12SJaegeuk Kim 
40476dbb1796SEric Biggers /*
40486dbb1796SEric Biggers  * Returns true if the reads of the inode's data need to undergo some
40496dbb1796SEric Biggers  * postprocessing step, like decryption or authenticity verification.
40506dbb1796SEric Biggers  */
40516dbb1796SEric Biggers static inline bool f2fs_post_read_required(struct inode *inode)
4052cde4de12SJaegeuk Kim {
40534c8ff709SChao Yu 	return f2fs_encrypted_file(inode) || fsverity_active(inode) ||
40544c8ff709SChao Yu 		f2fs_compressed_file(inode);
40554c8ff709SChao Yu }
40564c8ff709SChao Yu 
40574c8ff709SChao Yu /*
40584c8ff709SChao Yu  * compress.c
40594c8ff709SChao Yu  */
40604c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
40614c8ff709SChao Yu bool f2fs_is_compressed_page(struct page *page);
40624c8ff709SChao Yu struct page *f2fs_compress_control_page(struct page *page);
40634c8ff709SChao Yu int f2fs_prepare_compress_overwrite(struct inode *inode,
40644c8ff709SChao Yu 			struct page **pagep, pgoff_t index, void **fsdata);
40654c8ff709SChao Yu bool f2fs_compress_write_end(struct inode *inode, void *fsdata,
40664c8ff709SChao Yu 					pgoff_t index, unsigned copied);
40673265d3dbSChao Yu int f2fs_truncate_partial_cluster(struct inode *inode, u64 from, bool lock);
40684c8ff709SChao Yu void f2fs_compress_write_end_io(struct bio *bio, struct page *page);
40694c8ff709SChao Yu bool f2fs_is_compress_backend_ready(struct inode *inode);
40705e6bbde9SChao Yu int f2fs_init_compress_mempool(void);
40715e6bbde9SChao Yu void f2fs_destroy_compress_mempool(void);
40726ce19affSChao Yu void f2fs_decompress_cluster(struct decompress_io_ctx *dic);
40736ce19affSChao Yu void f2fs_end_read_compressed_page(struct page *page, bool failed,
40746ce19affSChao Yu 							block_t blkaddr);
40754c8ff709SChao Yu bool f2fs_cluster_is_empty(struct compress_ctx *cc);
40764c8ff709SChao Yu bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index);
40774c8ff709SChao Yu void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct page *page);
40784c8ff709SChao Yu int f2fs_write_multi_pages(struct compress_ctx *cc,
40794c8ff709SChao Yu 						int *submitted,
40804c8ff709SChao Yu 						struct writeback_control *wbc,
40814c8ff709SChao Yu 						enum iostat_type io_type);
40824c8ff709SChao Yu int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index);
4083*94afd6d6SChao Yu void f2fs_update_extent_tree_range_compressed(struct inode *inode,
4084*94afd6d6SChao Yu 				pgoff_t fofs, block_t blkaddr, unsigned int llen,
4085*94afd6d6SChao Yu 				unsigned int c_len);
40864c8ff709SChao Yu int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
40874c8ff709SChao Yu 				unsigned nr_pages, sector_t *last_block_in_bio,
40880683728aSChao Yu 				bool is_readahead, bool for_write);
40894c8ff709SChao Yu struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc);
40907f59b277SEric Biggers void f2fs_decompress_end_io(struct decompress_io_ctx *dic, bool failed);
40917f59b277SEric Biggers void f2fs_put_page_dic(struct page *page);
4092*94afd6d6SChao Yu unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn);
40934c8ff709SChao Yu int f2fs_init_compress_ctx(struct compress_ctx *cc);
40948bfbfb0dSChao Yu void f2fs_destroy_compress_ctx(struct compress_ctx *cc, bool reuse);
40954c8ff709SChao Yu void f2fs_init_compress_info(struct f2fs_sb_info *sbi);
40966ce19affSChao Yu int f2fs_init_compress_inode(struct f2fs_sb_info *sbi);
40976ce19affSChao Yu void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi);
409831083031SChao Yu int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi);
409931083031SChao Yu void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi);
4100c68d6c88SChao Yu int __init f2fs_init_compress_cache(void);
4101c68d6c88SChao Yu void f2fs_destroy_compress_cache(void);
41026ce19affSChao Yu struct address_space *COMPRESS_MAPPING(struct f2fs_sb_info *sbi);
41036ce19affSChao Yu void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi, block_t blkaddr);
41046ce19affSChao Yu void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
41056ce19affSChao Yu 						nid_t ino, block_t blkaddr);
41066ce19affSChao Yu bool f2fs_load_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
41076ce19affSChao Yu 								block_t blkaddr);
41086ce19affSChao Yu void f2fs_invalidate_compress_pages(struct f2fs_sb_info *sbi, nid_t ino);
41095ac443e2SDaeho Jeong #define inc_compr_inode_stat(inode)					\
41105ac443e2SDaeho Jeong 	do {								\
41115ac443e2SDaeho Jeong 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);		\
41125ac443e2SDaeho Jeong 		sbi->compr_new_inode++;					\
41135ac443e2SDaeho Jeong 	} while (0)
41145ac443e2SDaeho Jeong #define add_compr_block_stat(inode, blocks)				\
41155ac443e2SDaeho Jeong 	do {								\
41165ac443e2SDaeho Jeong 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);		\
41175ac443e2SDaeho Jeong 		int diff = F2FS_I(inode)->i_cluster_size - blocks;	\
41185ac443e2SDaeho Jeong 		sbi->compr_written_block += blocks;			\
41195ac443e2SDaeho Jeong 		sbi->compr_saved_block += diff;				\
41205ac443e2SDaeho Jeong 	} while (0)
41214c8ff709SChao Yu #else
41224c8ff709SChao Yu static inline bool f2fs_is_compressed_page(struct page *page) { return false; }
41234c8ff709SChao Yu static inline bool f2fs_is_compress_backend_ready(struct inode *inode)
41244c8ff709SChao Yu {
41254c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
41264c8ff709SChao Yu 		return true;
41274c8ff709SChao Yu 	/* not support compression */
41284c8ff709SChao Yu 	return false;
41294c8ff709SChao Yu }
41304c8ff709SChao Yu static inline struct page *f2fs_compress_control_page(struct page *page)
41314c8ff709SChao Yu {
41324c8ff709SChao Yu 	WARN_ON_ONCE(1);
41334c8ff709SChao Yu 	return ERR_PTR(-EINVAL);
41344c8ff709SChao Yu }
41355e6bbde9SChao Yu static inline int f2fs_init_compress_mempool(void) { return 0; }
41365e6bbde9SChao Yu static inline void f2fs_destroy_compress_mempool(void) { }
41376ce19affSChao Yu static inline void f2fs_decompress_cluster(struct decompress_io_ctx *dic) { }
41386ce19affSChao Yu static inline void f2fs_end_read_compressed_page(struct page *page,
41396ce19affSChao Yu 						bool failed, block_t blkaddr)
41407f59b277SEric Biggers {
41417f59b277SEric Biggers 	WARN_ON_ONCE(1);
41427f59b277SEric Biggers }
41437f59b277SEric Biggers static inline void f2fs_put_page_dic(struct page *page)
41447f59b277SEric Biggers {
41457f59b277SEric Biggers 	WARN_ON_ONCE(1);
41467f59b277SEric Biggers }
4147*94afd6d6SChao Yu static inline unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn) { return 0; }
41486ce19affSChao Yu static inline int f2fs_init_compress_inode(struct f2fs_sb_info *sbi) { return 0; }
41496ce19affSChao Yu static inline void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi) { }
415031083031SChao Yu static inline int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi) { return 0; }
415131083031SChao Yu static inline void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi) { }
4152c68d6c88SChao Yu static inline int __init f2fs_init_compress_cache(void) { return 0; }
4153c68d6c88SChao Yu static inline void f2fs_destroy_compress_cache(void) { }
41546ce19affSChao Yu static inline void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi,
41556ce19affSChao Yu 				block_t blkaddr) { }
41566ce19affSChao Yu static inline void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi,
41576ce19affSChao Yu 				struct page *page, nid_t ino, block_t blkaddr) { }
41586ce19affSChao Yu static inline bool f2fs_load_compressed_page(struct f2fs_sb_info *sbi,
41596ce19affSChao Yu 				struct page *page, block_t blkaddr) { return false; }
41606ce19affSChao Yu static inline void f2fs_invalidate_compress_pages(struct f2fs_sb_info *sbi,
41616ce19affSChao Yu 							nid_t ino) { }
41625ac443e2SDaeho Jeong #define inc_compr_inode_stat(inode)		do { } while (0)
4163*94afd6d6SChao Yu static inline void f2fs_update_extent_tree_range_compressed(struct inode *inode,
4164*94afd6d6SChao Yu 				pgoff_t fofs, block_t blkaddr, unsigned int llen,
4165*94afd6d6SChao Yu 				unsigned int c_len) { }
41664c8ff709SChao Yu #endif
41674c8ff709SChao Yu 
41684c8ff709SChao Yu static inline void set_compress_context(struct inode *inode)
41694c8ff709SChao Yu {
41704c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
41714c8ff709SChao Yu 
41724c8ff709SChao Yu 	F2FS_I(inode)->i_compress_algorithm =
41734c8ff709SChao Yu 			F2FS_OPTION(sbi).compress_algorithm;
41744c8ff709SChao Yu 	F2FS_I(inode)->i_log_cluster_size =
41754c8ff709SChao Yu 			F2FS_OPTION(sbi).compress_log_size;
4176b28f047bSChao Yu 	F2FS_I(inode)->i_compress_flag =
4177b28f047bSChao Yu 			F2FS_OPTION(sbi).compress_chksum ?
4178b28f047bSChao Yu 				1 << COMPRESS_CHKSUM : 0;
41794c8ff709SChao Yu 	F2FS_I(inode)->i_cluster_size =
41804c8ff709SChao Yu 			1 << F2FS_I(inode)->i_log_cluster_size;
418101f6afd0SChao Yu 	if ((F2FS_I(inode)->i_compress_algorithm == COMPRESS_LZ4 ||
418201f6afd0SChao Yu 		F2FS_I(inode)->i_compress_algorithm == COMPRESS_ZSTD) &&
41833fde13f8SChao Yu 			F2FS_OPTION(sbi).compress_level)
41843fde13f8SChao Yu 		F2FS_I(inode)->i_compress_flag |=
41853fde13f8SChao Yu 				F2FS_OPTION(sbi).compress_level <<
41863fde13f8SChao Yu 				COMPRESS_LEVEL_OFFSET;
41874c8ff709SChao Yu 	F2FS_I(inode)->i_flags |= F2FS_COMPR_FL;
41884c8ff709SChao Yu 	set_inode_flag(inode, FI_COMPRESSED_FILE);
41894c8ff709SChao Yu 	stat_inc_compr_inode(inode);
41905ac443e2SDaeho Jeong 	inc_compr_inode_stat(inode);
4191530e0704SChao Yu 	f2fs_mark_inode_dirty_sync(inode, true);
41924c8ff709SChao Yu }
41934c8ff709SChao Yu 
419478134d03SDaeho Jeong static inline bool f2fs_disable_compressed_file(struct inode *inode)
41954c8ff709SChao Yu {
41964c8ff709SChao Yu 	struct f2fs_inode_info *fi = F2FS_I(inode);
41974c8ff709SChao Yu 
41984c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
419978134d03SDaeho Jeong 		return true;
420078134d03SDaeho Jeong 	if (S_ISREG(inode->i_mode) &&
420178134d03SDaeho Jeong 		(get_dirty_pages(inode) || atomic_read(&fi->i_compr_blocks)))
420278134d03SDaeho Jeong 		return false;
42034c8ff709SChao Yu 
42044c8ff709SChao Yu 	fi->i_flags &= ~F2FS_COMPR_FL;
42054c8ff709SChao Yu 	stat_dec_compr_inode(inode);
420696f5b4faSChao Yu 	clear_inode_flag(inode, FI_COMPRESSED_FILE);
4207530e0704SChao Yu 	f2fs_mark_inode_dirty_sync(inode, true);
420878134d03SDaeho Jeong 	return true;
4209cde4de12SJaegeuk Kim }
4210cde4de12SJaegeuk Kim 
4211ccd31cb2SSheng Yong #define F2FS_FEATURE_FUNCS(name, flagname) \
42127beb01f7SChao Yu static inline int f2fs_sb_has_##name(struct f2fs_sb_info *sbi) \
4213ccd31cb2SSheng Yong { \
42147beb01f7SChao Yu 	return F2FS_HAS_FEATURE(sbi, F2FS_FEATURE_##flagname); \
4215cde4de12SJaegeuk Kim }
4216f424f664SJaegeuk Kim 
4217ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(encrypt, ENCRYPT);
4218ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(blkzoned, BLKZONED);
4219ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(extra_attr, EXTRA_ATTR);
4220ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(project_quota, PRJQUOTA);
4221ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_chksum, INODE_CHKSUM);
4222ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
4223ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
4224ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
4225b7c409deSSheng Yong F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
422695ae251fSEric Biggers F2FS_FEATURE_FUNCS(verity, VERITY);
4227d440c52dSJunling Zheng F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
42285aba5430SDaniel Rosenberg F2FS_FEATURE_FUNCS(casefold, CASEFOLD);
42294c8ff709SChao Yu F2FS_FEATURE_FUNCS(compression, COMPRESSION);
4230a7d9fe3cSJaegeuk Kim F2FS_FEATURE_FUNCS(readonly, RO);
42311c1d35dfSChao Yu 
42324215d054SDaeho Jeong static inline bool f2fs_may_extent_tree(struct inode *inode)
42334215d054SDaeho Jeong {
42344215d054SDaeho Jeong 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
42354215d054SDaeho Jeong 
42364215d054SDaeho Jeong 	if (!test_opt(sbi, EXTENT_CACHE) ||
42374215d054SDaeho Jeong 			is_inode_flag_set(inode, FI_NO_EXTENT) ||
42384215d054SDaeho Jeong 			(is_inode_flag_set(inode, FI_COMPRESSED_FILE) &&
42394215d054SDaeho Jeong 			 !f2fs_sb_has_readonly(sbi)))
42404215d054SDaeho Jeong 		return false;
42414215d054SDaeho Jeong 
42424215d054SDaeho Jeong 	/*
42434215d054SDaeho Jeong 	 * for recovered files during mount do not create extents
42444215d054SDaeho Jeong 	 * if shrinker is not registered.
42454215d054SDaeho Jeong 	 */
42464215d054SDaeho Jeong 	if (list_empty(&sbi->s_list))
42474215d054SDaeho Jeong 		return false;
42484215d054SDaeho Jeong 
42494215d054SDaeho Jeong 	return S_ISREG(inode->i_mode);
42504215d054SDaeho Jeong }
42514215d054SDaeho Jeong 
4252178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
425395175dafSDamien Le Moal static inline bool f2fs_blkz_is_seq(struct f2fs_sb_info *sbi, int devi,
425495175dafSDamien Le Moal 				    block_t blkaddr)
4255178053e2SDamien Le Moal {
4256178053e2SDamien Le Moal 	unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
4257178053e2SDamien Le Moal 
425895175dafSDamien Le Moal 	return test_bit(zno, FDEV(devi).blkz_seq);
4259178053e2SDamien Le Moal }
4260178053e2SDamien Le Moal #endif
4261178053e2SDamien Le Moal 
42627d20c8abSChao Yu static inline bool f2fs_hw_should_discard(struct f2fs_sb_info *sbi)
426396ba2decSDamien Le Moal {
42647beb01f7SChao Yu 	return f2fs_sb_has_blkzoned(sbi);
42657d20c8abSChao Yu }
426696ba2decSDamien Le Moal 
42677f3d7719SDamien Le Moal static inline bool f2fs_bdev_support_discard(struct block_device *bdev)
42687f3d7719SDamien Le Moal {
42697f3d7719SDamien Le Moal 	return blk_queue_discard(bdev_get_queue(bdev)) ||
42707f3d7719SDamien Le Moal 	       bdev_is_zoned(bdev);
42717f3d7719SDamien Le Moal }
42727f3d7719SDamien Le Moal 
42737d20c8abSChao Yu static inline bool f2fs_hw_support_discard(struct f2fs_sb_info *sbi)
42747d20c8abSChao Yu {
42757f3d7719SDamien Le Moal 	int i;
42767f3d7719SDamien Le Moal 
42777f3d7719SDamien Le Moal 	if (!f2fs_is_multi_device(sbi))
42787f3d7719SDamien Le Moal 		return f2fs_bdev_support_discard(sbi->sb->s_bdev);
42797f3d7719SDamien Le Moal 
42807f3d7719SDamien Le Moal 	for (i = 0; i < sbi->s_ndevs; i++)
42817f3d7719SDamien Le Moal 		if (f2fs_bdev_support_discard(FDEV(i).bdev))
42827f3d7719SDamien Le Moal 			return true;
42837f3d7719SDamien Le Moal 	return false;
42847d20c8abSChao Yu }
42857d20c8abSChao Yu 
42867d20c8abSChao Yu static inline bool f2fs_realtime_discard_enable(struct f2fs_sb_info *sbi)
42877d20c8abSChao Yu {
42887d20c8abSChao Yu 	return (test_opt(sbi, DISCARD) && f2fs_hw_support_discard(sbi)) ||
42897d20c8abSChao Yu 					f2fs_hw_should_discard(sbi);
429052763a4bSJaegeuk Kim }
429152763a4bSJaegeuk Kim 
4292f824deb5SChao Yu static inline bool f2fs_hw_is_readonly(struct f2fs_sb_info *sbi)
4293f824deb5SChao Yu {
4294f824deb5SChao Yu 	int i;
4295f824deb5SChao Yu 
4296f824deb5SChao Yu 	if (!f2fs_is_multi_device(sbi))
4297f824deb5SChao Yu 		return bdev_read_only(sbi->sb->s_bdev);
4298f824deb5SChao Yu 
4299f824deb5SChao Yu 	for (i = 0; i < sbi->s_ndevs; i++)
4300f824deb5SChao Yu 		if (bdev_read_only(FDEV(i).bdev))
4301f824deb5SChao Yu 			return true;
4302f824deb5SChao Yu 	return false;
4303f824deb5SChao Yu }
4304f824deb5SChao Yu 
4305b0332a0fSChao Yu static inline bool f2fs_lfs_mode(struct f2fs_sb_info *sbi)
430652763a4bSJaegeuk Kim {
4307b0332a0fSChao Yu 	return F2FS_OPTION(sbi).fs_mode == FS_MODE_LFS;
430852763a4bSJaegeuk Kim }
430952763a4bSJaegeuk Kim 
43104c8ff709SChao Yu static inline bool f2fs_may_compress(struct inode *inode)
43114c8ff709SChao Yu {
43124c8ff709SChao Yu 	if (IS_SWAPFILE(inode) || f2fs_is_pinned_file(inode) ||
43134c8ff709SChao Yu 				f2fs_is_atomic_file(inode) ||
43144c8ff709SChao Yu 				f2fs_is_volatile_file(inode))
43154c8ff709SChao Yu 		return false;
43164c8ff709SChao Yu 	return S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode);
43174c8ff709SChao Yu }
43184c8ff709SChao Yu 
43194c8ff709SChao Yu static inline void f2fs_i_compr_blocks_update(struct inode *inode,
43204c8ff709SChao Yu 						u64 blocks, bool add)
43214c8ff709SChao Yu {
43224c8ff709SChao Yu 	int diff = F2FS_I(inode)->i_cluster_size - blocks;
4323c2759ebaSDaeho Jeong 	struct f2fs_inode_info *fi = F2FS_I(inode);
43244c8ff709SChao Yu 
4325ef8d563fSChao Yu 	/* don't update i_compr_blocks if saved blocks were released */
4326c2759ebaSDaeho Jeong 	if (!add && !atomic_read(&fi->i_compr_blocks))
4327ef8d563fSChao Yu 		return;
4328ef8d563fSChao Yu 
43294c8ff709SChao Yu 	if (add) {
4330c2759ebaSDaeho Jeong 		atomic_add(diff, &fi->i_compr_blocks);
43314c8ff709SChao Yu 		stat_add_compr_blocks(inode, diff);
43324c8ff709SChao Yu 	} else {
4333c2759ebaSDaeho Jeong 		atomic_sub(diff, &fi->i_compr_blocks);
43344c8ff709SChao Yu 		stat_sub_compr_blocks(inode, diff);
43354c8ff709SChao Yu 	}
43364c8ff709SChao Yu 	f2fs_mark_inode_dirty_sync(inode, true);
43374c8ff709SChao Yu }
43384c8ff709SChao Yu 
4339f847c699SChao Yu static inline int block_unaligned_IO(struct inode *inode,
4340f847c699SChao Yu 				struct kiocb *iocb, struct iov_iter *iter)
4341b91050a8SHyunchul Lee {
4342f847c699SChao Yu 	unsigned int i_blkbits = READ_ONCE(inode->i_blkbits);
4343f847c699SChao Yu 	unsigned int blocksize_mask = (1 << i_blkbits) - 1;
4344f847c699SChao Yu 	loff_t offset = iocb->ki_pos;
4345f847c699SChao Yu 	unsigned long align = offset | iov_iter_alignment(iter);
4346f847c699SChao Yu 
4347f847c699SChao Yu 	return align & blocksize_mask;
4348f847c699SChao Yu }
4349f847c699SChao Yu 
4350f847c699SChao Yu static inline bool f2fs_force_buffered_io(struct inode *inode,
4351f847c699SChao Yu 				struct kiocb *iocb, struct iov_iter *iter)
4352f847c699SChao Yu {
4353f847c699SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4354f847c699SChao Yu 	int rw = iov_iter_rw(iter);
4355f847c699SChao Yu 
4356f847c699SChao Yu 	if (f2fs_post_read_required(inode))
4357f847c699SChao Yu 		return true;
43580916878dSDamien Le Moal 	if (f2fs_is_multi_device(sbi))
4359f847c699SChao Yu 		return true;
4360f847c699SChao Yu 	/*
4361f847c699SChao Yu 	 * for blkzoned device, fallback direct IO to buffered IO, so
4362f847c699SChao Yu 	 * all IOs can be serialized by log-structured write.
4363f847c699SChao Yu 	 */
43647beb01f7SChao Yu 	if (f2fs_sb_has_blkzoned(sbi))
4365f847c699SChao Yu 		return true;
4366b0332a0fSChao Yu 	if (f2fs_lfs_mode(sbi) && (rw == WRITE)) {
43679720ee80SChao Yu 		if (block_unaligned_IO(inode, iocb, iter))
4368f847c699SChao Yu 			return true;
43699720ee80SChao Yu 		if (F2FS_IO_ALIGNED(sbi))
43709720ee80SChao Yu 			return true;
43719720ee80SChao Yu 	}
4372ebc29b62Shuangjianan@oppo.com 	if (is_sbi_flag_set(F2FS_I_SB(inode), SBI_CP_DISABLED))
43734354994fSDaniel Rosenberg 		return true;
43744354994fSDaniel Rosenberg 
4375f847c699SChao Yu 	return false;
4376b91050a8SHyunchul Lee }
4377b91050a8SHyunchul Lee 
43787f59b277SEric Biggers static inline bool f2fs_need_verity(const struct inode *inode, pgoff_t idx)
43797f59b277SEric Biggers {
43807f59b277SEric Biggers 	return fsverity_active(inode) &&
43817f59b277SEric Biggers 	       idx < DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
43827f59b277SEric Biggers }
43837f59b277SEric Biggers 
438483a3bfdbSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
4385d494500aSChao Yu extern void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate,
4386d494500aSChao Yu 							unsigned int type);
438783a3bfdbSJaegeuk Kim #else
4388d494500aSChao Yu #define f2fs_build_fault_attr(sbi, rate, type)		do { } while (0)
438983a3bfdbSJaegeuk Kim #endif
439083a3bfdbSJaegeuk Kim 
4391af033b2aSChao Yu static inline bool is_journalled_quota(struct f2fs_sb_info *sbi)
4392af033b2aSChao Yu {
4393af033b2aSChao Yu #ifdef CONFIG_QUOTA
43947beb01f7SChao Yu 	if (f2fs_sb_has_quota_ino(sbi))
4395af033b2aSChao Yu 		return true;
4396af033b2aSChao Yu 	if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
4397af033b2aSChao Yu 		F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
4398af033b2aSChao Yu 		F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
4399af033b2aSChao Yu 		return true;
4400af033b2aSChao Yu #endif
4401af033b2aSChao Yu 	return false;
4402af033b2aSChao Yu }
44030af725fcSJaegeuk Kim 
44044f993264SChao Yu static inline bool f2fs_block_unit_discard(struct f2fs_sb_info *sbi)
44054f993264SChao Yu {
44064f993264SChao Yu 	return F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_BLOCK;
44074f993264SChao Yu }
44084f993264SChao Yu 
440910f966bbSChao Yu #define EFSBADCRC	EBADMSG		/* Bad CRC detected */
441010f966bbSChao Yu #define EFSCORRUPTED	EUCLEAN		/* Filesystem is corrupted */
441110f966bbSChao Yu 
4412e1074d4bSChao Yu #endif /* _LINUX_F2FS_H */
4413