xref: /openbmc/linux/fs/f2fs/f2fs.h (revision 859fca6b706e005f7cf19aa2ce7bb4005bcef427)
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 */
142ac4acb1fSEric Biggers 	struct fscrypt_dummy_policy dummy_enc_policy; /* test dummy encryption */
1431ae18f71SJaegeuk Kim 	block_t unusable_cap_perc;	/* percentage for cap */
1444d3aed70SDaniel Rosenberg 	block_t unusable_cap;		/* Amount of space allowed to be
1454d3aed70SDaniel Rosenberg 					 * unusable when disabling checkpoint
1464d3aed70SDaniel Rosenberg 					 */
1474c8ff709SChao Yu 
1484c8ff709SChao Yu 	/* For compression */
1494c8ff709SChao Yu 	unsigned char compress_algorithm;	/* algorithm type */
150b28f047bSChao Yu 	unsigned char compress_log_size;	/* cluster log size */
1513fde13f8SChao Yu 	unsigned char compress_level;		/* compress level */
152b28f047bSChao Yu 	bool compress_chksum;			/* compressed data chksum */
1534c8ff709SChao Yu 	unsigned char compress_ext_cnt;		/* extension count */
154602a16d5SDaeho Jeong 	int compress_mode;			/* compression mode */
1554c8ff709SChao Yu 	unsigned char extensions[COMPRESS_EXT_NUM][F2FS_EXTENSION_LEN];	/* extensions */
15639a53e0cSJaegeuk Kim };
15739a53e0cSJaegeuk Kim 
158cde4de12SJaegeuk Kim #define F2FS_FEATURE_ENCRYPT		0x0001
1590bfd7a09SDamien Le Moal #define F2FS_FEATURE_BLKZONED		0x0002
160e65ef207SJaegeuk Kim #define F2FS_FEATURE_ATOMIC_WRITE	0x0004
1617a2af766SChao Yu #define F2FS_FEATURE_EXTRA_ATTR		0x0008
1625c57132eSChao Yu #define F2FS_FEATURE_PRJQUOTA		0x0010
163704956ecSChao Yu #define F2FS_FEATURE_INODE_CHKSUM	0x0020
1646afc662eSChao Yu #define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR	0x0040
165234a9689SJaegeuk Kim #define F2FS_FEATURE_QUOTA_INO		0x0080
1661c1d35dfSChao Yu #define F2FS_FEATURE_INODE_CRTIME	0x0100
167b7c409deSSheng Yong #define F2FS_FEATURE_LOST_FOUND		0x0200
16895ae251fSEric Biggers #define F2FS_FEATURE_VERITY		0x0400
169d440c52dSJunling Zheng #define F2FS_FEATURE_SB_CHKSUM		0x0800
1705aba5430SDaniel Rosenberg #define F2FS_FEATURE_CASEFOLD		0x1000
1714c8ff709SChao Yu #define F2FS_FEATURE_COMPRESSION	0x2000
172a7d9fe3cSJaegeuk Kim #define F2FS_FEATURE_RO			0x4000
173cde4de12SJaegeuk Kim 
1747beb01f7SChao Yu #define __F2FS_HAS_FEATURE(raw_super, mask)				\
1757beb01f7SChao Yu 	((raw_super->feature & cpu_to_le32(mask)) != 0)
1767beb01f7SChao Yu #define F2FS_HAS_FEATURE(sbi, mask)	__F2FS_HAS_FEATURE(sbi->raw_super, mask)
1777beb01f7SChao Yu #define F2FS_SET_FEATURE(sbi, mask)					\
1787beb01f7SChao Yu 	(sbi->raw_super->feature |= cpu_to_le32(mask))
1797beb01f7SChao Yu #define F2FS_CLEAR_FEATURE(sbi, mask)					\
1807beb01f7SChao Yu 	(sbi->raw_super->feature &= ~cpu_to_le32(mask))
18176f105a2SJaegeuk Kim 
18239a53e0cSJaegeuk Kim /*
1837c2e5963SJaegeuk Kim  * Default values for user and/or group using reserved blocks
1847c2e5963SJaegeuk Kim  */
1857c2e5963SJaegeuk Kim #define	F2FS_DEF_RESUID		0
1867c2e5963SJaegeuk Kim #define	F2FS_DEF_RESGID		0
1877c2e5963SJaegeuk Kim 
1887c2e5963SJaegeuk Kim /*
18939a53e0cSJaegeuk Kim  * For checkpoint manager
19039a53e0cSJaegeuk Kim  */
19139a53e0cSJaegeuk Kim enum {
19239a53e0cSJaegeuk Kim 	NAT_BITMAP,
19339a53e0cSJaegeuk Kim 	SIT_BITMAP
19439a53e0cSJaegeuk Kim };
19539a53e0cSJaegeuk Kim 
196c473f1a9SChao Yu #define	CP_UMOUNT	0x00000001
197c473f1a9SChao Yu #define	CP_FASTBOOT	0x00000002
198c473f1a9SChao Yu #define	CP_SYNC		0x00000004
199c473f1a9SChao Yu #define	CP_RECOVERY	0x00000008
200c473f1a9SChao Yu #define	CP_DISCARD	0x00000010
2011f43e2adSChao Yu #define CP_TRIMMED	0x00000020
2024354994fSDaniel Rosenberg #define CP_PAUSE	0x00000040
203b4b10061SJaegeuk Kim #define CP_RESIZE 	0x00000080
20475ab4cb8SJaegeuk Kim 
2054ddb1a4dSJaegeuk Kim #define MAX_DISCARD_BLOCKS(sbi)		BLKS_PER_SEC(sbi)
206ecc9aa00SChao Yu #define DEF_MAX_DISCARD_REQUEST		8	/* issue 8 discards per round */
207969d1b18SChao Yu #define DEF_MIN_DISCARD_ISSUE_TIME	50	/* 50 ms, if exists */
208f9d1dcedSYunlei He #define DEF_MID_DISCARD_ISSUE_TIME	500	/* 500 ms, if device busy */
209969d1b18SChao Yu #define DEF_MAX_DISCARD_ISSUE_TIME	60000	/* 60 s, if no candidates */
2108bb4f253SJaegeuk Kim #define DEF_DISCARD_URGENT_UTIL		80	/* do more discard over 80% */
21160b99b48SJaegeuk Kim #define DEF_CP_INTERVAL			60	/* 60 secs */
212dcf25fe8SChao Yu #define DEF_IDLE_INTERVAL		5	/* 5 secs */
2134354994fSDaniel Rosenberg #define DEF_DISABLE_INTERVAL		5	/* 5 secs */
214db610a64SJaegeuk Kim #define DEF_DISABLE_QUICK_INTERVAL	1	/* 1 secs */
21503f2c02dSJaegeuk Kim #define DEF_UMOUNT_DISCARD_TIMEOUT	5	/* 5 secs */
216bba681cbSJaegeuk Kim 
21775ab4cb8SJaegeuk Kim struct cp_control {
21875ab4cb8SJaegeuk Kim 	int reason;
2194b2fecc8SJaegeuk Kim 	__u64 trim_start;
2204b2fecc8SJaegeuk Kim 	__u64 trim_end;
2214b2fecc8SJaegeuk Kim 	__u64 trim_minlen;
22275ab4cb8SJaegeuk Kim };
22375ab4cb8SJaegeuk Kim 
224662befdaSChao Yu /*
225e1da7872SChao Yu  * indicate meta/data type
226662befdaSChao Yu  */
227662befdaSChao Yu enum {
228662befdaSChao Yu 	META_CP,
229662befdaSChao Yu 	META_NAT,
23081c1a0f1SChao Yu 	META_SIT,
2314c521f49SJaegeuk Kim 	META_SSA,
232b63e7be5SChao Yu 	META_MAX,
2334c521f49SJaegeuk Kim 	META_POR,
23493770ab7SChao Yu 	DATA_GENERIC,		/* check range only */
23593770ab7SChao Yu 	DATA_GENERIC_ENHANCE,	/* strong check on range and segment bitmap */
23693770ab7SChao Yu 	DATA_GENERIC_ENHANCE_READ,	/*
23793770ab7SChao Yu 					 * strong check on range and segment
23893770ab7SChao Yu 					 * bitmap but no warning due to race
23993770ab7SChao Yu 					 * condition of read on truncated area
24093770ab7SChao Yu 					 * by extent_cache
24193770ab7SChao Yu 					 */
242e1da7872SChao Yu 	META_GENERIC,
243662befdaSChao Yu };
244662befdaSChao Yu 
2456451e041SJaegeuk Kim /* for the list of ino */
2466451e041SJaegeuk Kim enum {
2476451e041SJaegeuk Kim 	ORPHAN_INO,		/* for orphan ino list */
248fff04f90SJaegeuk Kim 	APPEND_INO,		/* for append ino list */
249fff04f90SJaegeuk Kim 	UPDATE_INO,		/* for update ino list */
2500a007b97SJaegeuk Kim 	TRANS_DIR_INO,		/* for trasactions dir ino list */
25139d787beSChao Yu 	FLUSH_INO,		/* for multiple device flushing */
2526451e041SJaegeuk Kim 	MAX_INO_ENTRY,		/* max. list */
2536451e041SJaegeuk Kim };
2546451e041SJaegeuk Kim 
2556451e041SJaegeuk Kim struct ino_entry {
25639a53e0cSJaegeuk Kim 	struct list_head list;		/* list head */
25739a53e0cSJaegeuk Kim 	nid_t ino;			/* inode number */
25839d787beSChao Yu 	unsigned int dirty_device;	/* dirty device bitmap */
25939a53e0cSJaegeuk Kim };
26039a53e0cSJaegeuk Kim 
2612710fd7eSChao Yu /* for the list of inodes to be GCed */
26206292073SChao Yu struct inode_entry {
26339a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
26439a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
26539a53e0cSJaegeuk Kim };
26639a53e0cSJaegeuk Kim 
26750fa53ecSChao Yu struct fsync_node_entry {
26850fa53ecSChao Yu 	struct list_head list;	/* list head */
26950fa53ecSChao Yu 	struct page *page;	/* warm node page pointer */
27050fa53ecSChao Yu 	unsigned int seq_id;	/* sequence id */
27150fa53ecSChao Yu };
27250fa53ecSChao Yu 
273261eeb9cSDaeho Jeong struct ckpt_req {
274261eeb9cSDaeho Jeong 	struct completion wait;		/* completion for checkpoint done */
275261eeb9cSDaeho Jeong 	struct llist_node llnode;	/* llist_node to be linked in wait queue */
276261eeb9cSDaeho Jeong 	int ret;			/* return code of checkpoint */
277261eeb9cSDaeho Jeong 	ktime_t queue_time;		/* request queued time */
278261eeb9cSDaeho Jeong };
279261eeb9cSDaeho Jeong 
280261eeb9cSDaeho Jeong struct ckpt_req_control {
281261eeb9cSDaeho Jeong 	struct task_struct *f2fs_issue_ckpt;	/* checkpoint task */
282e6592066SDaeho Jeong 	int ckpt_thread_ioprio;			/* checkpoint merge thread ioprio */
283261eeb9cSDaeho Jeong 	wait_queue_head_t ckpt_wait_queue;	/* waiting queue for wake-up */
284261eeb9cSDaeho Jeong 	atomic_t issued_ckpt;		/* # of actually issued ckpts */
285261eeb9cSDaeho Jeong 	atomic_t total_ckpt;		/* # of total ckpts */
286261eeb9cSDaeho Jeong 	atomic_t queued_ckpt;		/* # of queued ckpts */
287261eeb9cSDaeho Jeong 	struct llist_head issue_list;	/* list for command issue */
288261eeb9cSDaeho Jeong 	spinlock_t stat_lock;		/* lock for below checkpoint time stats */
289261eeb9cSDaeho Jeong 	unsigned int cur_time;		/* cur wait time in msec for currently issued checkpoint */
290261eeb9cSDaeho Jeong 	unsigned int peak_time;		/* peak wait time in msec until now */
291261eeb9cSDaeho Jeong };
292261eeb9cSDaeho Jeong 
293a7eeb823SChao Yu /* for the bitmap indicate blocks to be discarded */
2947fd9e544SJaegeuk Kim struct discard_entry {
2957fd9e544SJaegeuk Kim 	struct list_head list;	/* list head */
296a7eeb823SChao Yu 	block_t start_blkaddr;	/* start blockaddr of current segment */
297a7eeb823SChao Yu 	unsigned char discard_map[SIT_VBLOCK_MAP_SIZE];	/* segment discard bitmap */
2987fd9e544SJaegeuk Kim };
2997fd9e544SJaegeuk Kim 
300969d1b18SChao Yu /* default discard granularity of inner discard thread, unit: block count */
301969d1b18SChao Yu #define DEFAULT_DISCARD_GRANULARITY		16
302969d1b18SChao Yu 
303ba48a33eSChao Yu /* max discard pend list number */
304ba48a33eSChao Yu #define MAX_PLIST_NUM		512
305ba48a33eSChao Yu #define plist_idx(blk_num)	((blk_num) >= MAX_PLIST_NUM ?		\
3062f84babfSSheng Yong 					(MAX_PLIST_NUM - 1) : ((blk_num) - 1))
307ba48a33eSChao Yu 
30815469963SJaegeuk Kim enum {
30935ec7d57SChao Yu 	D_PREP,			/* initial */
31035ec7d57SChao Yu 	D_PARTIAL,		/* partially submitted */
31135ec7d57SChao Yu 	D_SUBMIT,		/* all submitted */
31235ec7d57SChao Yu 	D_DONE,			/* finished */
31315469963SJaegeuk Kim };
31415469963SJaegeuk Kim 
315004b6862SChao Yu struct discard_info {
316b01a9201SJaegeuk Kim 	block_t lstart;			/* logical start address */
317b01a9201SJaegeuk Kim 	block_t len;			/* length */
318004b6862SChao Yu 	block_t start;			/* actual start address in dev */
319004b6862SChao Yu };
320004b6862SChao Yu 
321b01a9201SJaegeuk Kim struct discard_cmd {
322004b6862SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
323004b6862SChao Yu 	union {
324004b6862SChao Yu 		struct {
325004b6862SChao Yu 			block_t lstart;	/* logical start address */
326004b6862SChao Yu 			block_t len;	/* length */
327004b6862SChao Yu 			block_t start;	/* actual start address in dev */
328004b6862SChao Yu 		};
329004b6862SChao Yu 		struct discard_info di;	/* discard info */
330004b6862SChao Yu 
331004b6862SChao Yu 	};
332b01a9201SJaegeuk Kim 	struct list_head list;		/* command list */
333b01a9201SJaegeuk Kim 	struct completion wait;		/* compleation */
334c81abe34SJaegeuk Kim 	struct block_device *bdev;	/* bdev */
335ec9895adSChao Yu 	unsigned short ref;		/* reference count */
3369a744b92SChao Yu 	unsigned char state;		/* state */
33772691af6SJaegeuk Kim 	unsigned char queued;		/* queued discard */
338c81abe34SJaegeuk Kim 	int error;			/* bio error */
33935ec7d57SChao Yu 	spinlock_t lock;		/* for state/bio_ref updating */
34035ec7d57SChao Yu 	unsigned short bio_ref;		/* bio reference count */
341275b66b0SChao Yu };
342275b66b0SChao Yu 
34378997b56SChao Yu enum {
34478997b56SChao Yu 	DPOLICY_BG,
34578997b56SChao Yu 	DPOLICY_FORCE,
34678997b56SChao Yu 	DPOLICY_FSTRIM,
34778997b56SChao Yu 	DPOLICY_UMOUNT,
34878997b56SChao Yu 	MAX_DPOLICY,
34978997b56SChao Yu };
35078997b56SChao Yu 
351ecc9aa00SChao Yu struct discard_policy {
35278997b56SChao Yu 	int type;			/* type of discard */
353ecc9aa00SChao Yu 	unsigned int min_interval;	/* used for candidates exist */
354f9d1dcedSYunlei He 	unsigned int mid_interval;	/* used for device busy */
355ecc9aa00SChao Yu 	unsigned int max_interval;	/* used for candidates not exist */
356ecc9aa00SChao Yu 	unsigned int max_requests;	/* # of discards issued per round */
357ecc9aa00SChao Yu 	unsigned int io_aware_gran;	/* minimum granularity discard not be aware of I/O */
358ecc9aa00SChao Yu 	bool io_aware;			/* issue discard in idle time */
359ecc9aa00SChao Yu 	bool sync;			/* submit discard with REQ_SYNC flag */
36020ee4382SChao Yu 	bool ordered;			/* issue discard by lba order */
3616ce48b0cSChao Yu 	bool timeout;			/* discard timeout for put_super */
36278997b56SChao Yu 	unsigned int granularity;	/* discard granularity */
363ecc9aa00SChao Yu };
364ecc9aa00SChao Yu 
3650b54fb84SJaegeuk Kim struct discard_cmd_control {
36615469963SJaegeuk Kim 	struct task_struct *f2fs_issue_discard;	/* discard thread */
36746f84c2cSChao Yu 	struct list_head entry_list;		/* 4KB discard entry list */
368ba48a33eSChao Yu 	struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
36946f84c2cSChao Yu 	struct list_head wait_list;		/* store on-flushing entries */
3708412663dSChao Yu 	struct list_head fstrim_list;		/* in-flight discard from fstrim */
37115469963SJaegeuk Kim 	wait_queue_head_t discard_wait_queue;	/* waiting queue for wake-up */
372969d1b18SChao Yu 	unsigned int discard_wake;		/* to wake up discard thread */
37315469963SJaegeuk Kim 	struct mutex cmd_lock;
374d618ebafSChao Yu 	unsigned int nr_discards;		/* # of discards in the list */
375d618ebafSChao Yu 	unsigned int max_discards;		/* max. discards to be issued */
376969d1b18SChao Yu 	unsigned int discard_granularity;	/* discard granularity */
377d84d1cbdSChao Yu 	unsigned int undiscard_blks;		/* # of undiscard blocks */
37820ee4382SChao Yu 	unsigned int next_pos;			/* next discard position */
3798b8dd65fSChao Yu 	atomic_t issued_discard;		/* # of issued discard */
38072691af6SJaegeuk Kim 	atomic_t queued_discard;		/* # of queued discard */
3815f32366aSChao Yu 	atomic_t discard_cmd_cnt;		/* # of cached cmd count */
3824dada3fdSChao Yu 	struct rb_root_cached root;		/* root of discard rb-tree */
38367fce70bSChao Yu 	bool rbtree_check;			/* config for consistence check */
38439a53e0cSJaegeuk Kim };
38539a53e0cSJaegeuk Kim 
38639a53e0cSJaegeuk Kim /* for the list of fsync inodes, used only during recovery */
38739a53e0cSJaegeuk Kim struct fsync_inode_entry {
38839a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
38939a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
390c52e1b10SJaegeuk Kim 	block_t blkaddr;	/* block address locating the last fsync */
391c52e1b10SJaegeuk Kim 	block_t last_dentry;	/* block address locating the last dentry */
39239a53e0cSJaegeuk Kim };
39339a53e0cSJaegeuk Kim 
39468afcf2dSTomohiro Kusumi #define nats_in_cursum(jnl)		(le16_to_cpu((jnl)->n_nats))
39568afcf2dSTomohiro Kusumi #define sits_in_cursum(jnl)		(le16_to_cpu((jnl)->n_sits))
39639a53e0cSJaegeuk Kim 
39768afcf2dSTomohiro Kusumi #define nat_in_journal(jnl, i)		((jnl)->nat_j.entries[i].ne)
39868afcf2dSTomohiro Kusumi #define nid_in_journal(jnl, i)		((jnl)->nat_j.entries[i].nid)
39968afcf2dSTomohiro Kusumi #define sit_in_journal(jnl, i)		((jnl)->sit_j.entries[i].se)
40068afcf2dSTomohiro Kusumi #define segno_in_journal(jnl, i)	((jnl)->sit_j.entries[i].segno)
40139a53e0cSJaegeuk Kim 
402dfc08a12SChao Yu #define MAX_NAT_JENTRIES(jnl)	(NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
403dfc08a12SChao Yu #define MAX_SIT_JENTRIES(jnl)	(SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
404309cc2b6SJaegeuk Kim 
405dfc08a12SChao Yu static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
40639a53e0cSJaegeuk Kim {
407dfc08a12SChao Yu 	int before = nats_in_cursum(journal);
408cac5a3d8SDongOh Shin 
409dfc08a12SChao Yu 	journal->n_nats = cpu_to_le16(before + i);
41039a53e0cSJaegeuk Kim 	return before;
41139a53e0cSJaegeuk Kim }
41239a53e0cSJaegeuk Kim 
413dfc08a12SChao Yu static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
41439a53e0cSJaegeuk Kim {
415dfc08a12SChao Yu 	int before = sits_in_cursum(journal);
416cac5a3d8SDongOh Shin 
417dfc08a12SChao Yu 	journal->n_sits = cpu_to_le16(before + i);
41839a53e0cSJaegeuk Kim 	return before;
41939a53e0cSJaegeuk Kim }
42039a53e0cSJaegeuk Kim 
421dfc08a12SChao Yu static inline bool __has_cursum_space(struct f2fs_journal *journal,
422dfc08a12SChao Yu 							int size, int type)
423184a5cd2SChao Yu {
424184a5cd2SChao Yu 	if (type == NAT_JOURNAL)
425dfc08a12SChao Yu 		return size <= MAX_NAT_JENTRIES(journal);
426dfc08a12SChao Yu 	return size <= MAX_SIT_JENTRIES(journal);
427184a5cd2SChao Yu }
428184a5cd2SChao Yu 
429f2470371SChao Yu /* for inline stuff */
430f2470371SChao Yu #define DEF_INLINE_RESERVED_SIZE	1
4317a2af766SChao Yu static inline int get_extra_isize(struct inode *inode);
4326afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode);
4337a2af766SChao Yu #define MAX_INLINE_DATA(inode)	(sizeof(__le32) *			\
4347a2af766SChao Yu 				(CUR_ADDRS_PER_INODE(inode) -		\
435b323fd28SChao Yu 				get_inline_xattr_addrs(inode) -	\
4366afc662eSChao Yu 				DEF_INLINE_RESERVED_SIZE))
437f2470371SChao Yu 
438f2470371SChao Yu /* for inline dir */
439f2470371SChao Yu #define NR_INLINE_DENTRY(inode)	(MAX_INLINE_DATA(inode) * BITS_PER_BYTE / \
440f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
441f2470371SChao Yu 				BITS_PER_BYTE + 1))
442f91108b8SGeert Uytterhoeven #define INLINE_DENTRY_BITMAP_SIZE(inode) \
443f91108b8SGeert Uytterhoeven 	DIV_ROUND_UP(NR_INLINE_DENTRY(inode), BITS_PER_BYTE)
444f2470371SChao Yu #define INLINE_RESERVED_SIZE(inode)	(MAX_INLINE_DATA(inode) - \
445f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
446f2470371SChao Yu 				NR_INLINE_DENTRY(inode) + \
447f2470371SChao Yu 				INLINE_DENTRY_BITMAP_SIZE(inode)))
448f2470371SChao Yu 
449e9750824SNamjae Jeon /*
45039a53e0cSJaegeuk Kim  * For INODE and NODE manager
45139a53e0cSJaegeuk Kim  */
4527b3cd7d6SJaegeuk Kim /* for directory operations */
45343c780baSEric Biggers 
45443c780baSEric Biggers struct f2fs_filename {
45543c780baSEric Biggers 	/*
45643c780baSEric Biggers 	 * The filename the user specified.  This is NULL for some
45743c780baSEric Biggers 	 * filesystem-internal operations, e.g. converting an inline directory
45843c780baSEric Biggers 	 * to a non-inline one, or roll-forward recovering an encrypted dentry.
45943c780baSEric Biggers 	 */
46043c780baSEric Biggers 	const struct qstr *usr_fname;
46143c780baSEric Biggers 
46243c780baSEric Biggers 	/*
46343c780baSEric Biggers 	 * The on-disk filename.  For encrypted directories, this is encrypted.
46443c780baSEric Biggers 	 * This may be NULL for lookups in an encrypted dir without the key.
46543c780baSEric Biggers 	 */
46643c780baSEric Biggers 	struct fscrypt_str disk_name;
46743c780baSEric Biggers 
46843c780baSEric Biggers 	/* The dirhash of this filename */
46943c780baSEric Biggers 	f2fs_hash_t hash;
47043c780baSEric Biggers 
47143c780baSEric Biggers #ifdef CONFIG_FS_ENCRYPTION
47243c780baSEric Biggers 	/*
47343c780baSEric Biggers 	 * For lookups in encrypted directories: either the buffer backing
47443c780baSEric Biggers 	 * disk_name, or a buffer that holds the decoded no-key name.
47543c780baSEric Biggers 	 */
47643c780baSEric Biggers 	struct fscrypt_str crypto_buf;
47743c780baSEric Biggers #endif
47843c780baSEric Biggers #ifdef CONFIG_UNICODE
47943c780baSEric Biggers 	/*
48043c780baSEric Biggers 	 * For casefolded directories: the casefolded name, but it's left NULL
4817ad08a58SDaniel Rosenberg 	 * if the original name is not valid Unicode, if the directory is both
4827ad08a58SDaniel Rosenberg 	 * casefolded and encrypted and its encryption key is unavailable, or if
4837ad08a58SDaniel Rosenberg 	 * the filesystem is doing an internal operation where usr_fname is also
4847ad08a58SDaniel Rosenberg 	 * NULL.  In all these cases we fall back to treating the name as an
4857ad08a58SDaniel Rosenberg 	 * opaque byte sequence.
48643c780baSEric Biggers 	 */
48743c780baSEric Biggers 	struct fscrypt_str cf_name;
48843c780baSEric Biggers #endif
48943c780baSEric Biggers };
49043c780baSEric Biggers 
4917b3cd7d6SJaegeuk Kim struct f2fs_dentry_ptr {
492d8c6822aSJaegeuk Kim 	struct inode *inode;
49376a9dd85SChao Yu 	void *bitmap;
4947b3cd7d6SJaegeuk Kim 	struct f2fs_dir_entry *dentry;
4957b3cd7d6SJaegeuk Kim 	__u8 (*filename)[F2FS_SLOT_LEN];
4967b3cd7d6SJaegeuk Kim 	int max;
49776a9dd85SChao Yu 	int nr_bitmap;
4987b3cd7d6SJaegeuk Kim };
4997b3cd7d6SJaegeuk Kim 
50064c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_block(struct inode *inode,
50164c24ecbSTomohiro Kusumi 		struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
5027b3cd7d6SJaegeuk Kim {
503d8c6822aSJaegeuk Kim 	d->inode = inode;
5047b3cd7d6SJaegeuk Kim 	d->max = NR_DENTRY_IN_BLOCK;
50576a9dd85SChao Yu 	d->nr_bitmap = SIZE_OF_DENTRY_BITMAP;
506c79d1520SYunlong Song 	d->bitmap = t->dentry_bitmap;
5077b3cd7d6SJaegeuk Kim 	d->dentry = t->dentry;
5087b3cd7d6SJaegeuk Kim 	d->filename = t->filename;
50964c24ecbSTomohiro Kusumi }
510cac5a3d8SDongOh Shin 
51164c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_inline(struct inode *inode,
512f2470371SChao Yu 					struct f2fs_dentry_ptr *d, void *t)
51364c24ecbSTomohiro Kusumi {
514f2470371SChao Yu 	int entry_cnt = NR_INLINE_DENTRY(inode);
515f2470371SChao Yu 	int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(inode);
516f2470371SChao Yu 	int reserved_size = INLINE_RESERVED_SIZE(inode);
517f2470371SChao Yu 
51864c24ecbSTomohiro Kusumi 	d->inode = inode;
519f2470371SChao Yu 	d->max = entry_cnt;
520f2470371SChao Yu 	d->nr_bitmap = bitmap_size;
521f2470371SChao Yu 	d->bitmap = t;
522f2470371SChao Yu 	d->dentry = t + bitmap_size + reserved_size;
523f2470371SChao Yu 	d->filename = t + bitmap_size + reserved_size +
524f2470371SChao Yu 					SIZE_OF_DIR_ENTRY * entry_cnt;
5257b3cd7d6SJaegeuk Kim }
5267b3cd7d6SJaegeuk Kim 
527dbe6a5ffSJaegeuk Kim /*
528dbe6a5ffSJaegeuk Kim  * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
529dbe6a5ffSJaegeuk Kim  * as its node offset to distinguish from index node blocks.
530dbe6a5ffSJaegeuk Kim  * But some bits are used to mark the node block.
53139a53e0cSJaegeuk Kim  */
532dbe6a5ffSJaegeuk Kim #define XATTR_NODE_OFFSET	((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
533dbe6a5ffSJaegeuk Kim 				>> OFFSET_BIT_SHIFT)
534266e97a8SJaegeuk Kim enum {
535266e97a8SJaegeuk Kim 	ALLOC_NODE,			/* allocate a new node page if needed */
536266e97a8SJaegeuk Kim 	LOOKUP_NODE,			/* look up a node without readahead */
537266e97a8SJaegeuk Kim 	LOOKUP_NODE_RA,			/*
538266e97a8SJaegeuk Kim 					 * look up a node with readahead called
5394f4124d0SChao Yu 					 * by get_data_block.
54039a53e0cSJaegeuk Kim 					 */
541266e97a8SJaegeuk Kim };
542266e97a8SJaegeuk Kim 
5437735730dSChao Yu #define DEFAULT_RETRY_IO_COUNT	8	/* maximum retry read IO count */
5447735730dSChao Yu 
5455df7731fSChao Yu /* congestion wait timeout value, default: 20ms */
5465df7731fSChao Yu #define	DEFAULT_IO_TIMEOUT	(msecs_to_jiffies(20))
5475df7731fSChao Yu 
548af033b2aSChao Yu /* maximum retry quota flush count */
549af033b2aSChao Yu #define DEFAULT_RETRY_QUOTA_FLUSH_COUNT		8
550af033b2aSChao Yu 
551a6db67f0SJaegeuk Kim #define F2FS_LINK_MAX	0xffffffff	/* maximum link count per file */
55239a53e0cSJaegeuk Kim 
553817202d9SChao Yu #define MAX_DIR_RA_PAGES	4	/* maximum ra pages of dir */
554817202d9SChao Yu 
55539a53e0cSJaegeuk Kim /* for in-memory extent cache entry */
55613054c54SChao Yu #define F2FS_MIN_EXTENT_LEN	64	/* minimum extent length */
55713054c54SChao Yu 
55813054c54SChao Yu /* number of extent info in extent cache we try to shrink */
55913054c54SChao Yu #define EXTENT_CACHE_SHRINK_NUMBER	128
560c11abd1aSJaegeuk Kim 
56154c2258cSChao Yu struct rb_entry {
56254c2258cSChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
5632e9b2bb2SChao Yu 	union {
5642e9b2bb2SChao Yu 		struct {
56554c2258cSChao Yu 			unsigned int ofs;	/* start offset of the entry */
56654c2258cSChao Yu 			unsigned int len;	/* length of the entry */
56754c2258cSChao Yu 		};
5682e9b2bb2SChao Yu 		unsigned long long key;		/* 64-bits key */
56948046cb5SJaegeuk Kim 	} __packed;
5702e9b2bb2SChao Yu };
57154c2258cSChao Yu 
57239a53e0cSJaegeuk Kim struct extent_info {
57339a53e0cSJaegeuk Kim 	unsigned int fofs;		/* start offset in a file */
574111d2495SMasanari Iida 	unsigned int len;		/* length of the extent */
57554c2258cSChao Yu 	u32 blk;			/* start block address of the extent */
57639a53e0cSJaegeuk Kim };
57739a53e0cSJaegeuk Kim 
57813054c54SChao Yu struct extent_node {
579c0362117SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
58013054c54SChao Yu 	struct extent_info ei;		/* extent info */
58154c2258cSChao Yu 	struct list_head list;		/* node in global extent list of sbi */
582201ef5e0SHou Pengyang 	struct extent_tree *et;		/* extent tree pointer */
58313054c54SChao Yu };
58413054c54SChao Yu 
58513054c54SChao Yu struct extent_tree {
58613054c54SChao Yu 	nid_t ino;			/* inode number */
5874dada3fdSChao Yu 	struct rb_root_cached root;	/* root of extent info rb-tree */
58862c8af65SChao Yu 	struct extent_node *cached_en;	/* recently accessed extent node */
5893e72f721SJaegeuk Kim 	struct extent_info largest;	/* largested extent info */
590137d09f0SJaegeuk Kim 	struct list_head list;		/* to be used by sbi->zombie_list */
59113054c54SChao Yu 	rwlock_t lock;			/* protect extent info rb-tree */
59268e35385SChao Yu 	atomic_t node_cnt;		/* # of extent node in rb-tree*/
593b430f726SZhikang Zhang 	bool largest_updated;		/* largest extent updated */
59413054c54SChao Yu };
59513054c54SChao Yu 
59639a53e0cSJaegeuk Kim /*
597003a3e1dSJaegeuk Kim  * This structure is taken from ext4_map_blocks.
598003a3e1dSJaegeuk Kim  *
599003a3e1dSJaegeuk Kim  * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
600003a3e1dSJaegeuk Kim  */
601003a3e1dSJaegeuk Kim #define F2FS_MAP_NEW		(1 << BH_New)
602003a3e1dSJaegeuk Kim #define F2FS_MAP_MAPPED		(1 << BH_Mapped)
6037f63eb77SJaegeuk Kim #define F2FS_MAP_UNWRITTEN	(1 << BH_Unwritten)
6047f63eb77SJaegeuk Kim #define F2FS_MAP_FLAGS		(F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
6057f63eb77SJaegeuk Kim 				F2FS_MAP_UNWRITTEN)
606003a3e1dSJaegeuk Kim 
607003a3e1dSJaegeuk Kim struct f2fs_map_blocks {
608003a3e1dSJaegeuk Kim 	block_t m_pblk;
609003a3e1dSJaegeuk Kim 	block_t m_lblk;
610003a3e1dSJaegeuk Kim 	unsigned int m_len;
611003a3e1dSJaegeuk Kim 	unsigned int m_flags;
612da85985cSChao Yu 	pgoff_t *m_next_pgofs;		/* point next possible non-hole pgofs */
613c4020b2dSChao Yu 	pgoff_t *m_next_extent;		/* point to next possible extent */
614d5097be5SHyunchul Lee 	int m_seg_type;
615f9d6d059SChao Yu 	bool m_may_create;		/* indicate it is from write path */
616003a3e1dSJaegeuk Kim };
617003a3e1dSJaegeuk Kim 
618e2b4e2bcSChao Yu /* for flag in get_data_block */
619f2220c7fSQiuyang Sun enum {
620f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_DEFAULT,
621f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_FIEMAP,
622f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_BMAP,
6230a4daae5SJaegeuk Kim 	F2FS_GET_BLOCK_DIO,
624f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_DIO,
625f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_AIO,
626c4020b2dSChao Yu 	F2FS_GET_BLOCK_PRECACHE,
627f2220c7fSQiuyang Sun };
628e2b4e2bcSChao Yu 
629003a3e1dSJaegeuk Kim /*
63039a53e0cSJaegeuk Kim  * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
63139a53e0cSJaegeuk Kim  */
63239a53e0cSJaegeuk Kim #define FADVISE_COLD_BIT	0x01
633354a3399SJaegeuk Kim #define FADVISE_LOST_PINO_BIT	0x02
634cde4de12SJaegeuk Kim #define FADVISE_ENCRYPT_BIT	0x04
635e7d55452SJaegeuk Kim #define FADVISE_ENC_NAME_BIT	0x08
63626787236SJaegeuk Kim #define FADVISE_KEEP_SIZE_BIT	0x10
637b6a06cbbSChao Yu #define FADVISE_HOT_BIT		0x20
63895ae251fSEric Biggers #define FADVISE_VERITY_BIT	0x40
63939a53e0cSJaegeuk Kim 
640797c1cb5SChao Yu #define FADVISE_MODIFIABLE_BITS	(FADVISE_COLD_BIT | FADVISE_HOT_BIT)
641797c1cb5SChao Yu 
642b5492af7SJaegeuk Kim #define file_is_cold(inode)	is_file(inode, FADVISE_COLD_BIT)
643b5492af7SJaegeuk Kim #define file_set_cold(inode)	set_file(inode, FADVISE_COLD_BIT)
644b5492af7SJaegeuk Kim #define file_clear_cold(inode)	clear_file(inode, FADVISE_COLD_BIT)
6451153db09SChao Yu 
6461153db09SChao Yu #define file_wrong_pino(inode)	is_file(inode, FADVISE_LOST_PINO_BIT)
6471153db09SChao Yu #define file_lost_pino(inode)	set_file(inode, FADVISE_LOST_PINO_BIT)
648b5492af7SJaegeuk Kim #define file_got_pino(inode)	clear_file(inode, FADVISE_LOST_PINO_BIT)
6491153db09SChao Yu 
650cde4de12SJaegeuk Kim #define file_is_encrypt(inode)	is_file(inode, FADVISE_ENCRYPT_BIT)
651cde4de12SJaegeuk Kim #define file_set_encrypt(inode)	set_file(inode, FADVISE_ENCRYPT_BIT)
6521153db09SChao Yu 
653e7d55452SJaegeuk Kim #define file_enc_name(inode)	is_file(inode, FADVISE_ENC_NAME_BIT)
654e7d55452SJaegeuk Kim #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
6551153db09SChao Yu 
65626787236SJaegeuk Kim #define file_keep_isize(inode)	is_file(inode, FADVISE_KEEP_SIZE_BIT)
65726787236SJaegeuk Kim #define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
6581153db09SChao Yu 
659b6a06cbbSChao Yu #define file_is_hot(inode)	is_file(inode, FADVISE_HOT_BIT)
660b6a06cbbSChao Yu #define file_set_hot(inode)	set_file(inode, FADVISE_HOT_BIT)
661b6a06cbbSChao Yu #define file_clear_hot(inode)	clear_file(inode, FADVISE_HOT_BIT)
6621153db09SChao Yu 
66395ae251fSEric Biggers #define file_is_verity(inode)	is_file(inode, FADVISE_VERITY_BIT)
66495ae251fSEric Biggers #define file_set_verity(inode)	set_file(inode, FADVISE_VERITY_BIT)
665cde4de12SJaegeuk Kim 
666ab9fa662SJaegeuk Kim #define DEF_DIR_LEVEL		0
667ab9fa662SJaegeuk Kim 
6682ef79ecbSChao Yu enum {
6692ef79ecbSChao Yu 	GC_FAILURE_PIN,
6702ef79ecbSChao Yu 	GC_FAILURE_ATOMIC,
6712ef79ecbSChao Yu 	MAX_GC_FAILURE
6722ef79ecbSChao Yu };
6732ef79ecbSChao Yu 
6747653b9d8SChao Yu /* used for f2fs_inode_info->flags */
6757653b9d8SChao Yu enum {
6767653b9d8SChao Yu 	FI_NEW_INODE,		/* indicate newly allocated inode */
6777653b9d8SChao Yu 	FI_DIRTY_INODE,		/* indicate inode is dirty or not */
6787653b9d8SChao Yu 	FI_AUTO_RECOVER,	/* indicate inode is recoverable */
6797653b9d8SChao Yu 	FI_DIRTY_DIR,		/* indicate directory has dirty pages */
6807653b9d8SChao Yu 	FI_INC_LINK,		/* need to increment i_nlink */
6817653b9d8SChao Yu 	FI_ACL_MODE,		/* indicate acl mode */
6827653b9d8SChao Yu 	FI_NO_ALLOC,		/* should not allocate any blocks */
6837653b9d8SChao Yu 	FI_FREE_NID,		/* free allocated nide */
6847653b9d8SChao Yu 	FI_NO_EXTENT,		/* not to use the extent cache */
6857653b9d8SChao Yu 	FI_INLINE_XATTR,	/* used for inline xattr */
6867653b9d8SChao Yu 	FI_INLINE_DATA,		/* used for inline data*/
6877653b9d8SChao Yu 	FI_INLINE_DENTRY,	/* used for inline dentry */
6887653b9d8SChao Yu 	FI_APPEND_WRITE,	/* inode has appended data */
6897653b9d8SChao Yu 	FI_UPDATE_WRITE,	/* inode has in-place-update data */
6907653b9d8SChao Yu 	FI_NEED_IPU,		/* used for ipu per file */
6917653b9d8SChao Yu 	FI_ATOMIC_FILE,		/* indicate atomic file */
6927653b9d8SChao Yu 	FI_ATOMIC_COMMIT,	/* indicate the state of atomical committing */
6937653b9d8SChao Yu 	FI_VOLATILE_FILE,	/* indicate volatile file */
6947653b9d8SChao Yu 	FI_FIRST_BLOCK_WRITTEN,	/* indicate #0 data block was written */
6957653b9d8SChao Yu 	FI_DROP_CACHE,		/* drop dirty page cache */
6967653b9d8SChao Yu 	FI_DATA_EXIST,		/* indicate data exists */
6977653b9d8SChao Yu 	FI_INLINE_DOTS,		/* indicate inline dot dentries */
6987653b9d8SChao Yu 	FI_DO_DEFRAG,		/* indicate defragment is running */
6997653b9d8SChao Yu 	FI_DIRTY_FILE,		/* indicate regular/symlink has dirty pages */
7007653b9d8SChao Yu 	FI_NO_PREALLOC,		/* indicate skipped preallocated blocks */
7017653b9d8SChao Yu 	FI_HOT_DATA,		/* indicate file is hot */
7027653b9d8SChao Yu 	FI_EXTRA_ATTR,		/* indicate file has extra attribute */
7037653b9d8SChao Yu 	FI_PROJ_INHERIT,	/* indicate file inherits projectid */
7047653b9d8SChao Yu 	FI_PIN_FILE,		/* indicate file should not be gced */
7057653b9d8SChao Yu 	FI_ATOMIC_REVOKE_REQUEST, /* request to drop atomic data */
7067653b9d8SChao Yu 	FI_VERITY_IN_PROGRESS,	/* building fs-verity Merkle tree */
7077653b9d8SChao Yu 	FI_COMPRESSED_FILE,	/* indicate file's data can be compressed */
708b28f047bSChao Yu 	FI_COMPRESS_CORRUPT,	/* indicate compressed cluster is corrupted */
7097653b9d8SChao Yu 	FI_MMAP_FILE,		/* indicate file was mmapped */
710602a16d5SDaeho Jeong 	FI_ENABLE_COMPRESS,	/* enable compression in "user" compression mode */
711c6140415SJaegeuk Kim 	FI_COMPRESS_RELEASED,	/* compressed blocks were released */
712*859fca6bSChao Yu 	FI_ALIGNED_WRITE,	/* enable aligned write */
7137653b9d8SChao Yu 	FI_MAX,			/* max flag, never be used */
7147653b9d8SChao Yu };
7157653b9d8SChao Yu 
71639a53e0cSJaegeuk Kim struct f2fs_inode_info {
71739a53e0cSJaegeuk Kim 	struct inode vfs_inode;		/* serve a vfs inode */
71839a53e0cSJaegeuk Kim 	unsigned long i_flags;		/* keep an inode flags for ioctl */
71939a53e0cSJaegeuk Kim 	unsigned char i_advise;		/* use to give file attribute hints */
72038431545SJaegeuk Kim 	unsigned char i_dir_level;	/* use for dentry level for large dir */
7211ad71a27SJaegeuk Kim 	unsigned int i_current_depth;	/* only for directory depth */
7222ef79ecbSChao Yu 	/* for gc failure statistic */
7232ef79ecbSChao Yu 	unsigned int i_gc_failures[MAX_GC_FAILURE];
7246666e6aaSJaegeuk Kim 	unsigned int i_pino;		/* parent inode number */
72539a53e0cSJaegeuk Kim 	umode_t i_acl_mode;		/* keep file acl mode temporarily */
72639a53e0cSJaegeuk Kim 
72739a53e0cSJaegeuk Kim 	/* Use below internally in f2fs*/
7287653b9d8SChao Yu 	unsigned long flags[BITS_TO_LONGS(FI_MAX)];	/* use to pass per-file flags */
729d928bfbfSJaegeuk Kim 	struct rw_semaphore i_sem;	/* protect fi info */
730204706c7SJaegeuk Kim 	atomic_t dirty_pages;		/* # of dirty pages */
73139a53e0cSJaegeuk Kim 	f2fs_hash_t chash;		/* hash value of given file name */
73239a53e0cSJaegeuk Kim 	unsigned int clevel;		/* maximum level of given file name */
73388c5c13aSJaegeuk Kim 	struct task_struct *task;	/* lookup and create consistency */
734b0af6d49SChao Yu 	struct task_struct *cp_task;	/* separate cp/wb IO stats*/
73539a53e0cSJaegeuk Kim 	nid_t i_xattr_nid;		/* node id that contains xattrs */
73626de9b11SJaegeuk Kim 	loff_t	last_disk_size;		/* lastly written file size */
737c10c9820SChao Yu 	spinlock_t i_size_lock;		/* protect last_disk_size */
73888b88a66SJaegeuk Kim 
7390abd675eSChao Yu #ifdef CONFIG_QUOTA
7400abd675eSChao Yu 	struct dquot *i_dquot[MAXQUOTAS];
7410abd675eSChao Yu 
7420abd675eSChao Yu 	/* quota space reservation, managed internally by quota code */
7430abd675eSChao Yu 	qsize_t i_reserved_quota;
7440abd675eSChao Yu #endif
7450f18b462SJaegeuk Kim 	struct list_head dirty_list;	/* dirty list for dirs and files */
7460f18b462SJaegeuk Kim 	struct list_head gdirty_list;	/* linked in global dirty list */
74757864ae5SJaegeuk Kim 	struct list_head inmem_ilist;	/* list for inmem inodes */
74888b88a66SJaegeuk Kim 	struct list_head inmem_pages;	/* inmemory pages managed by f2fs */
7497a10f017SJaegeuk Kim 	struct task_struct *inmem_task;	/* store inmemory task */
75088b88a66SJaegeuk Kim 	struct mutex inmem_lock;	/* lock for inmemory pages */
7513e72f721SJaegeuk Kim 	struct extent_tree *extent_tree;	/* cached extent_tree entry */
752b2532c69SChao Yu 
753b2532c69SChao Yu 	/* avoid racing between foreground op and gc */
754b2532c69SChao Yu 	struct rw_semaphore i_gc_rwsem[2];
7555a3a2d83SQiuyang Sun 	struct rw_semaphore i_mmap_sem;
75627161f13SYunlei He 	struct rw_semaphore i_xattr_sem; /* avoid racing between reading and changing EAs */
757f2470371SChao Yu 
7587a2af766SChao Yu 	int i_extra_isize;		/* size of extra space located in i_addr */
7595c57132eSChao Yu 	kprojid_t i_projid;		/* id for project quota */
7606afc662eSChao Yu 	int i_inline_xattr_size;	/* inline xattr size */
76124b81dfcSArnd Bergmann 	struct timespec64 i_crtime;	/* inode creation time */
76224b81dfcSArnd Bergmann 	struct timespec64 i_disk_time[4];/* inode disk times */
7634c8ff709SChao Yu 
7644c8ff709SChao Yu 	/* for file compress */
765c2759ebaSDaeho Jeong 	atomic_t i_compr_blocks;		/* # of compressed blocks */
7664c8ff709SChao Yu 	unsigned char i_compress_algorithm;	/* algorithm type */
7674c8ff709SChao Yu 	unsigned char i_log_cluster_size;	/* log of cluster size */
7683fde13f8SChao Yu 	unsigned char i_compress_level;		/* compress level (lz4hc,zstd) */
769b28f047bSChao Yu 	unsigned short i_compress_flag;		/* compress flag */
7704c8ff709SChao Yu 	unsigned int i_cluster_size;		/* cluster size */
77139a53e0cSJaegeuk Kim };
77239a53e0cSJaegeuk Kim 
77339a53e0cSJaegeuk Kim static inline void get_extent_info(struct extent_info *ext,
774bd933d4fSChao Yu 					struct f2fs_extent *i_ext)
77539a53e0cSJaegeuk Kim {
776bd933d4fSChao Yu 	ext->fofs = le32_to_cpu(i_ext->fofs);
777bd933d4fSChao Yu 	ext->blk = le32_to_cpu(i_ext->blk);
778bd933d4fSChao Yu 	ext->len = le32_to_cpu(i_ext->len);
77939a53e0cSJaegeuk Kim }
78039a53e0cSJaegeuk Kim 
78139a53e0cSJaegeuk Kim static inline void set_raw_extent(struct extent_info *ext,
78239a53e0cSJaegeuk Kim 					struct f2fs_extent *i_ext)
78339a53e0cSJaegeuk Kim {
78439a53e0cSJaegeuk Kim 	i_ext->fofs = cpu_to_le32(ext->fofs);
7854d0b0bd4SChao Yu 	i_ext->blk = cpu_to_le32(ext->blk);
78639a53e0cSJaegeuk Kim 	i_ext->len = cpu_to_le32(ext->len);
78739a53e0cSJaegeuk Kim }
78839a53e0cSJaegeuk Kim 
789429511cdSChao Yu static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
790429511cdSChao Yu 						u32 blk, unsigned int len)
791429511cdSChao Yu {
792429511cdSChao Yu 	ei->fofs = fofs;
793429511cdSChao Yu 	ei->blk = blk;
794429511cdSChao Yu 	ei->len = len;
795429511cdSChao Yu }
796429511cdSChao Yu 
797004b6862SChao Yu static inline bool __is_discard_mergeable(struct discard_info *back,
79835ec7d57SChao Yu 			struct discard_info *front, unsigned int max_len)
799004b6862SChao Yu {
8009a997188SJaegeuk Kim 	return (back->lstart + back->len == front->lstart) &&
80135ec7d57SChao Yu 		(back->len + front->len <= max_len);
802004b6862SChao Yu }
803004b6862SChao Yu 
804004b6862SChao Yu static inline bool __is_discard_back_mergeable(struct discard_info *cur,
80535ec7d57SChao Yu 			struct discard_info *back, unsigned int max_len)
806004b6862SChao Yu {
80735ec7d57SChao Yu 	return __is_discard_mergeable(back, cur, max_len);
808004b6862SChao Yu }
809004b6862SChao Yu 
810004b6862SChao Yu static inline bool __is_discard_front_mergeable(struct discard_info *cur,
81135ec7d57SChao Yu 			struct discard_info *front, unsigned int max_len)
812004b6862SChao Yu {
81335ec7d57SChao Yu 	return __is_discard_mergeable(cur, front, max_len);
814004b6862SChao Yu }
815004b6862SChao Yu 
816429511cdSChao Yu static inline bool __is_extent_mergeable(struct extent_info *back,
817429511cdSChao Yu 						struct extent_info *front)
818429511cdSChao Yu {
819429511cdSChao Yu 	return (back->fofs + back->len == front->fofs &&
820429511cdSChao Yu 			back->blk + back->len == front->blk);
821429511cdSChao Yu }
822429511cdSChao Yu 
823429511cdSChao Yu static inline bool __is_back_mergeable(struct extent_info *cur,
824429511cdSChao Yu 						struct extent_info *back)
825429511cdSChao Yu {
826429511cdSChao Yu 	return __is_extent_mergeable(back, cur);
827429511cdSChao Yu }
828429511cdSChao Yu 
829429511cdSChao Yu static inline bool __is_front_mergeable(struct extent_info *cur,
830429511cdSChao Yu 						struct extent_info *front)
831429511cdSChao Yu {
832429511cdSChao Yu 	return __is_extent_mergeable(cur, front);
833429511cdSChao Yu }
834429511cdSChao Yu 
835cac5a3d8SDongOh Shin extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
836b430f726SZhikang Zhang static inline void __try_update_largest_extent(struct extent_tree *et,
837b430f726SZhikang Zhang 						struct extent_node *en)
8384abd3f5aSChao Yu {
839205b9822SJaegeuk Kim 	if (en->ei.len > et->largest.len) {
8404abd3f5aSChao Yu 		et->largest = en->ei;
841b430f726SZhikang Zhang 		et->largest_updated = true;
842205b9822SJaegeuk Kim 	}
8434abd3f5aSChao Yu }
8444abd3f5aSChao Yu 
8459a4ffdf5SChao Yu /*
8469a4ffdf5SChao Yu  * For free nid management
8479a4ffdf5SChao Yu  */
8489a4ffdf5SChao Yu enum nid_state {
8499a4ffdf5SChao Yu 	FREE_NID,		/* newly added to free nid list */
8509a4ffdf5SChao Yu 	PREALLOC_NID,		/* it is preallocated */
8519a4ffdf5SChao Yu 	MAX_NID_STATE,
852b8559dc2SChao Yu };
853b8559dc2SChao Yu 
854a95ba66aSJaegeuk Kim enum nat_state {
855a95ba66aSJaegeuk Kim 	TOTAL_NAT,
856a95ba66aSJaegeuk Kim 	DIRTY_NAT,
857a95ba66aSJaegeuk Kim 	RECLAIMABLE_NAT,
858a95ba66aSJaegeuk Kim 	MAX_NAT_STATE,
859a95ba66aSJaegeuk Kim };
860a95ba66aSJaegeuk Kim 
86139a53e0cSJaegeuk Kim struct f2fs_nm_info {
86239a53e0cSJaegeuk Kim 	block_t nat_blkaddr;		/* base disk address of NAT */
86339a53e0cSJaegeuk Kim 	nid_t max_nid;			/* maximum possible node ids */
86404d47e67SChao Yu 	nid_t available_nids;		/* # of available node ids */
86539a53e0cSJaegeuk Kim 	nid_t next_scan_nid;		/* the next nid to be scanned */
866cdfc41c1SJaegeuk Kim 	unsigned int ram_thresh;	/* control the memory footprint */
867ea1a29a0SChao Yu 	unsigned int ra_nid_pages;	/* # of nid pages to be readaheaded */
8682304cb0cSChao Yu 	unsigned int dirty_nats_ratio;	/* control dirty nats ratio threshold */
86939a53e0cSJaegeuk Kim 
87039a53e0cSJaegeuk Kim 	/* NAT cache management */
87139a53e0cSJaegeuk Kim 	struct radix_tree_root nat_root;/* root of the nat entry cache */
872309cc2b6SJaegeuk Kim 	struct radix_tree_root nat_set_root;/* root of the nat set cache */
873f3e367d4Sqiulaibin 	struct rw_semaphore nat_tree_lock;	/* protect nat entry tree */
87439a53e0cSJaegeuk Kim 	struct list_head nat_entries;	/* cached nat entry list (clean) */
87522969158SChao Yu 	spinlock_t nat_list_lock;	/* protect clean nat entry list */
876a95ba66aSJaegeuk Kim 	unsigned int nat_cnt[MAX_NAT_STATE]; /* the # of cached nat entries */
87722ad0b6aSJaegeuk Kim 	unsigned int nat_blocks;	/* # of nat blocks */
87839a53e0cSJaegeuk Kim 
87939a53e0cSJaegeuk Kim 	/* free node ids management */
8808a7ed66aSJaegeuk Kim 	struct radix_tree_root free_nid_root;/* root of the free_nid cache */
8819a4ffdf5SChao Yu 	struct list_head free_nid_list;		/* list for free nids excluding preallocated nids */
8829a4ffdf5SChao Yu 	unsigned int nid_cnt[MAX_NID_STATE];	/* the number of free node id */
883b8559dc2SChao Yu 	spinlock_t nid_list_lock;	/* protect nid lists ops */
88439a53e0cSJaegeuk Kim 	struct mutex build_lock;	/* lock for build free nids */
885bb1105e4SJaegeuk Kim 	unsigned char **free_nid_bitmap;
8864ac91242SChao Yu 	unsigned char *nat_block_bitmap;
887586d1492SChao Yu 	unsigned short *free_nid_count;	/* free nid count of NAT block */
88839a53e0cSJaegeuk Kim 
88939a53e0cSJaegeuk Kim 	/* for checkpoint */
89039a53e0cSJaegeuk Kim 	char *nat_bitmap;		/* NAT bitmap pointer */
89122ad0b6aSJaegeuk Kim 
89222ad0b6aSJaegeuk Kim 	unsigned int nat_bits_blocks;	/* # of nat bits blocks */
89322ad0b6aSJaegeuk Kim 	unsigned char *nat_bits;	/* NAT bits blocks */
89422ad0b6aSJaegeuk Kim 	unsigned char *full_nat_bits;	/* full NAT pages */
89522ad0b6aSJaegeuk Kim 	unsigned char *empty_nat_bits;	/* empty NAT pages */
896599a09b2SChao Yu #ifdef CONFIG_F2FS_CHECK_FS
897599a09b2SChao Yu 	char *nat_bitmap_mir;		/* NAT bitmap mirror */
898599a09b2SChao Yu #endif
89939a53e0cSJaegeuk Kim 	int bitmap_size;		/* bitmap size */
90039a53e0cSJaegeuk Kim };
90139a53e0cSJaegeuk Kim 
90239a53e0cSJaegeuk Kim /*
90339a53e0cSJaegeuk Kim  * this structure is used as one of function parameters.
90439a53e0cSJaegeuk Kim  * all the information are dedicated to a given direct node block determined
90539a53e0cSJaegeuk Kim  * by the data offset in a file.
90639a53e0cSJaegeuk Kim  */
90739a53e0cSJaegeuk Kim struct dnode_of_data {
90839a53e0cSJaegeuk Kim 	struct inode *inode;		/* vfs inode pointer */
90939a53e0cSJaegeuk Kim 	struct page *inode_page;	/* its inode page, NULL is possible */
91039a53e0cSJaegeuk Kim 	struct page *node_page;		/* cached direct node page */
91139a53e0cSJaegeuk Kim 	nid_t nid;			/* node id of the direct node block */
91239a53e0cSJaegeuk Kim 	unsigned int ofs_in_node;	/* data offset in the node page */
91339a53e0cSJaegeuk Kim 	bool inode_page_locked;		/* inode page is locked or not */
91493bae099SJaegeuk Kim 	bool node_changed;		/* is node block changed */
9153cf45747SChao Yu 	char cur_level;			/* level of hole node page */
9163cf45747SChao Yu 	char max_level;			/* level of current page located */
91739a53e0cSJaegeuk Kim 	block_t	data_blkaddr;		/* block address of the node block */
91839a53e0cSJaegeuk Kim };
91939a53e0cSJaegeuk Kim 
92039a53e0cSJaegeuk Kim static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
92139a53e0cSJaegeuk Kim 		struct page *ipage, struct page *npage, nid_t nid)
92239a53e0cSJaegeuk Kim {
923d66d1f76SJaegeuk Kim 	memset(dn, 0, sizeof(*dn));
92439a53e0cSJaegeuk Kim 	dn->inode = inode;
92539a53e0cSJaegeuk Kim 	dn->inode_page = ipage;
92639a53e0cSJaegeuk Kim 	dn->node_page = npage;
92739a53e0cSJaegeuk Kim 	dn->nid = nid;
92839a53e0cSJaegeuk Kim }
92939a53e0cSJaegeuk Kim 
93039a53e0cSJaegeuk Kim /*
93139a53e0cSJaegeuk Kim  * For SIT manager
93239a53e0cSJaegeuk Kim  *
93339a53e0cSJaegeuk Kim  * By default, there are 6 active log areas across the whole main area.
93439a53e0cSJaegeuk Kim  * When considering hot and cold data separation to reduce cleaning overhead,
93539a53e0cSJaegeuk Kim  * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
93639a53e0cSJaegeuk Kim  * respectively.
93739a53e0cSJaegeuk Kim  * In the current design, you should not change the numbers intentionally.
93839a53e0cSJaegeuk Kim  * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
93939a53e0cSJaegeuk Kim  * logs individually according to the underlying devices. (default: 6)
94039a53e0cSJaegeuk Kim  * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
94139a53e0cSJaegeuk Kim  * data and 8 for node logs.
94239a53e0cSJaegeuk Kim  */
94339a53e0cSJaegeuk Kim #define	NR_CURSEG_DATA_TYPE	(3)
94439a53e0cSJaegeuk Kim #define NR_CURSEG_NODE_TYPE	(3)
945093749e2SChao Yu #define NR_CURSEG_INMEM_TYPE	(2)
946a7d9fe3cSJaegeuk Kim #define NR_CURSEG_RO_TYPE	(2)
947d0b9e42aSChao Yu #define NR_CURSEG_PERSIST_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
948d0b9e42aSChao Yu #define NR_CURSEG_TYPE		(NR_CURSEG_INMEM_TYPE + NR_CURSEG_PERSIST_TYPE)
94939a53e0cSJaegeuk Kim 
95039a53e0cSJaegeuk Kim enum {
95139a53e0cSJaegeuk Kim 	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
95239a53e0cSJaegeuk Kim 	CURSEG_WARM_DATA,	/* data blocks */
95339a53e0cSJaegeuk Kim 	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
95439a53e0cSJaegeuk Kim 	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
95539a53e0cSJaegeuk Kim 	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
95639a53e0cSJaegeuk Kim 	CURSEG_COLD_NODE,	/* indirect node blocks */
957d0b9e42aSChao Yu 	NR_PERSISTENT_LOG,	/* number of persistent log */
958d0b9e42aSChao Yu 	CURSEG_COLD_DATA_PINNED = NR_PERSISTENT_LOG,
959d0b9e42aSChao Yu 				/* pinned file that needs consecutive block address */
960093749e2SChao Yu 	CURSEG_ALL_DATA_ATGC,	/* SSR alloctor in hot/warm/cold data area */
961d0b9e42aSChao Yu 	NO_CHECK_TYPE,		/* number of persistent & inmem log */
96239a53e0cSJaegeuk Kim };
96339a53e0cSJaegeuk Kim 
9646b4afdd7SJaegeuk Kim struct flush_cmd {
9656b4afdd7SJaegeuk Kim 	struct completion wait;
966721bd4d5SGu Zheng 	struct llist_node llnode;
96739d787beSChao Yu 	nid_t ino;
9686b4afdd7SJaegeuk Kim 	int ret;
9696b4afdd7SJaegeuk Kim };
9706b4afdd7SJaegeuk Kim 
971a688b9d9SGu Zheng struct flush_cmd_control {
972a688b9d9SGu Zheng 	struct task_struct *f2fs_issue_flush;	/* flush thread */
973a688b9d9SGu Zheng 	wait_queue_head_t flush_wait_queue;	/* waiting queue for wake-up */
9748b8dd65fSChao Yu 	atomic_t issued_flush;			/* # of issued flushes */
97572691af6SJaegeuk Kim 	atomic_t queued_flush;			/* # of queued flushes */
976721bd4d5SGu Zheng 	struct llist_head issue_list;		/* list for command issue */
977721bd4d5SGu Zheng 	struct llist_node *dispatch_list;	/* list for command dispatch */
978a688b9d9SGu Zheng };
979a688b9d9SGu Zheng 
98039a53e0cSJaegeuk Kim struct f2fs_sm_info {
98139a53e0cSJaegeuk Kim 	struct sit_info *sit_info;		/* whole segment information */
98239a53e0cSJaegeuk Kim 	struct free_segmap_info *free_info;	/* free segment information */
98339a53e0cSJaegeuk Kim 	struct dirty_seglist_info *dirty_info;	/* dirty segment information */
98439a53e0cSJaegeuk Kim 	struct curseg_info *curseg_array;	/* active segment information */
98539a53e0cSJaegeuk Kim 
9862b60311dSChao Yu 	struct rw_semaphore curseg_lock;	/* for preventing curseg change */
9872b60311dSChao Yu 
98839a53e0cSJaegeuk Kim 	block_t seg0_blkaddr;		/* block address of 0'th segment */
98939a53e0cSJaegeuk Kim 	block_t main_blkaddr;		/* start block address of main area */
99039a53e0cSJaegeuk Kim 	block_t ssa_blkaddr;		/* start block address of SSA area */
99139a53e0cSJaegeuk Kim 
99239a53e0cSJaegeuk Kim 	unsigned int segment_count;	/* total # of segments */
99339a53e0cSJaegeuk Kim 	unsigned int main_segments;	/* # of segments in main area */
99439a53e0cSJaegeuk Kim 	unsigned int reserved_segments;	/* # of reserved segments */
99539a53e0cSJaegeuk Kim 	unsigned int ovp_segments;	/* # of overprovision segments */
99681eb8d6eSJaegeuk Kim 
99781eb8d6eSJaegeuk Kim 	/* a threshold to reclaim prefree segments */
99881eb8d6eSJaegeuk Kim 	unsigned int rec_prefree_segments;
9997fd9e544SJaegeuk Kim 
1000bba681cbSJaegeuk Kim 	/* for batched trimming */
1001bba681cbSJaegeuk Kim 	unsigned int trim_sections;		/* # of sections to trim */
1002bba681cbSJaegeuk Kim 
1003184a5cd2SChao Yu 	struct list_head sit_entry_set;	/* sit entry set list */
1004184a5cd2SChao Yu 
1005216fbd64SJaegeuk Kim 	unsigned int ipu_policy;	/* in-place-update policy */
1006216fbd64SJaegeuk Kim 	unsigned int min_ipu_util;	/* in-place-update threshold */
1007c1ce1b02SJaegeuk Kim 	unsigned int min_fsync_blocks;	/* threshold for fsync */
1008853137ceSJaegeuk Kim 	unsigned int min_seq_blocks;	/* threshold for sequential blocks */
1009ef095d19SJaegeuk Kim 	unsigned int min_hot_blocks;	/* threshold for hot block allocation */
1010a2a12b67SChao Yu 	unsigned int min_ssr_sections;	/* threshold to trigger SSR allocation */
10116b4afdd7SJaegeuk Kim 
10126b4afdd7SJaegeuk Kim 	/* for flush command control */
1013b01a9201SJaegeuk Kim 	struct flush_cmd_control *fcc_info;
1014a688b9d9SGu Zheng 
10150b54fb84SJaegeuk Kim 	/* for discard command control */
10160b54fb84SJaegeuk Kim 	struct discard_cmd_control *dcc_info;
101739a53e0cSJaegeuk Kim };
101839a53e0cSJaegeuk Kim 
101939a53e0cSJaegeuk Kim /*
102039a53e0cSJaegeuk Kim  * For superblock
102139a53e0cSJaegeuk Kim  */
102239a53e0cSJaegeuk Kim /*
102339a53e0cSJaegeuk Kim  * COUNT_TYPE for monitoring
102439a53e0cSJaegeuk Kim  *
102539a53e0cSJaegeuk Kim  * f2fs monitors the number of several block types such as on-writeback,
102639a53e0cSJaegeuk Kim  * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
102739a53e0cSJaegeuk Kim  */
102836951b38SChao Yu #define WB_DATA_TYPE(p)	(__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
102939a53e0cSJaegeuk Kim enum count_type {
103039a53e0cSJaegeuk Kim 	F2FS_DIRTY_DENTS,
1031c227f912SChao Yu 	F2FS_DIRTY_DATA,
10322c8a4a28SJaegeuk Kim 	F2FS_DIRTY_QDATA,
103339a53e0cSJaegeuk Kim 	F2FS_DIRTY_NODES,
103439a53e0cSJaegeuk Kim 	F2FS_DIRTY_META,
10358dcf2ff7SJaegeuk Kim 	F2FS_INMEM_PAGES,
10360f18b462SJaegeuk Kim 	F2FS_DIRTY_IMETA,
103736951b38SChao Yu 	F2FS_WB_CP_DATA,
103836951b38SChao Yu 	F2FS_WB_DATA,
10395f9abab4SJaegeuk Kim 	F2FS_RD_DATA,
10405f9abab4SJaegeuk Kim 	F2FS_RD_NODE,
10415f9abab4SJaegeuk Kim 	F2FS_RD_META,
104202b16d0aSChao Yu 	F2FS_DIO_WRITE,
104302b16d0aSChao Yu 	F2FS_DIO_READ,
104439a53e0cSJaegeuk Kim 	NR_COUNT_TYPE,
104539a53e0cSJaegeuk Kim };
104639a53e0cSJaegeuk Kim 
104739a53e0cSJaegeuk Kim /*
1048e1c42045Sarter97  * The below are the page types of bios used in submit_bio().
104939a53e0cSJaegeuk Kim  * The available types are:
105039a53e0cSJaegeuk Kim  * DATA			User data pages. It operates as async mode.
105139a53e0cSJaegeuk Kim  * NODE			Node pages. It operates as async mode.
105239a53e0cSJaegeuk Kim  * META			FS metadata pages such as SIT, NAT, CP.
105339a53e0cSJaegeuk Kim  * NR_PAGE_TYPE		The number of page types.
105439a53e0cSJaegeuk Kim  * META_FLUSH		Make sure the previous pages are written
105539a53e0cSJaegeuk Kim  *			with waiting the bio's completion
105639a53e0cSJaegeuk Kim  * ...			Only can be used with META.
105739a53e0cSJaegeuk Kim  */
10587d5e5109SJaegeuk Kim #define PAGE_TYPE_OF_BIO(type)	((type) > META ? META : (type))
105939a53e0cSJaegeuk Kim enum page_type {
106039a53e0cSJaegeuk Kim 	DATA,
106139a53e0cSJaegeuk Kim 	NODE,
106239a53e0cSJaegeuk Kim 	META,
106339a53e0cSJaegeuk Kim 	NR_PAGE_TYPE,
106439a53e0cSJaegeuk Kim 	META_FLUSH,
10658ce67cb0SJaegeuk Kim 	INMEM,		/* the below types are used by tracepoints only. */
10668ce67cb0SJaegeuk Kim 	INMEM_DROP,
10678c242db9SJaegeuk Kim 	INMEM_INVALIDATE,
106828bc106bSChao Yu 	INMEM_REVOKE,
10698ce67cb0SJaegeuk Kim 	IPU,
10708ce67cb0SJaegeuk Kim 	OPU,
107139a53e0cSJaegeuk Kim };
107239a53e0cSJaegeuk Kim 
1073a912b54dSJaegeuk Kim enum temp_type {
1074a912b54dSJaegeuk Kim 	HOT = 0,	/* must be zero for meta bio */
1075a912b54dSJaegeuk Kim 	WARM,
1076a912b54dSJaegeuk Kim 	COLD,
1077a912b54dSJaegeuk Kim 	NR_TEMP_TYPE,
1078a912b54dSJaegeuk Kim };
1079a912b54dSJaegeuk Kim 
1080cc15620bSJaegeuk Kim enum need_lock_type {
1081cc15620bSJaegeuk Kim 	LOCK_REQ = 0,
1082cc15620bSJaegeuk Kim 	LOCK_DONE,
1083cc15620bSJaegeuk Kim 	LOCK_RETRY,
1084cc15620bSJaegeuk Kim };
1085cc15620bSJaegeuk Kim 
1086a5fd5050SChao Yu enum cp_reason_type {
1087a5fd5050SChao Yu 	CP_NO_NEEDED,
1088a5fd5050SChao Yu 	CP_NON_REGULAR,
10894c8ff709SChao Yu 	CP_COMPRESSED,
1090a5fd5050SChao Yu 	CP_HARDLINK,
1091a5fd5050SChao Yu 	CP_SB_NEED_CP,
1092a5fd5050SChao Yu 	CP_WRONG_PINO,
1093a5fd5050SChao Yu 	CP_NO_SPC_ROLL,
1094a5fd5050SChao Yu 	CP_NODE_NEED_CP,
1095a5fd5050SChao Yu 	CP_FASTBOOT_MODE,
1096a5fd5050SChao Yu 	CP_SPEC_LOG_NUM,
10970a007b97SJaegeuk Kim 	CP_RECOVER_DIR,
1098a5fd5050SChao Yu };
1099a5fd5050SChao Yu 
1100b0af6d49SChao Yu enum iostat_type {
11018b83ac81SChao Yu 	/* WRITE IO */
11028b83ac81SChao Yu 	APP_DIRECT_IO,			/* app direct write IOs */
11038b83ac81SChao Yu 	APP_BUFFERED_IO,		/* app buffered write IOs */
1104b0af6d49SChao Yu 	APP_WRITE_IO,			/* app write IOs */
1105b0af6d49SChao Yu 	APP_MAPPED_IO,			/* app mapped IOs */
1106b0af6d49SChao Yu 	FS_DATA_IO,			/* data IOs from kworker/fsync/reclaimer */
1107b0af6d49SChao Yu 	FS_NODE_IO,			/* node IOs from kworker/fsync/reclaimer */
1108b0af6d49SChao Yu 	FS_META_IO,			/* meta IOs from kworker/reclaimer */
1109b0af6d49SChao Yu 	FS_GC_DATA_IO,			/* data IOs from forground gc */
1110b0af6d49SChao Yu 	FS_GC_NODE_IO,			/* node IOs from forground gc */
1111b0af6d49SChao Yu 	FS_CP_DATA_IO,			/* data IOs from checkpoint */
1112b0af6d49SChao Yu 	FS_CP_NODE_IO,			/* node IOs from checkpoint */
1113b0af6d49SChao Yu 	FS_CP_META_IO,			/* meta IOs from checkpoint */
11148b83ac81SChao Yu 
11158b83ac81SChao Yu 	/* READ IO */
11168b83ac81SChao Yu 	APP_DIRECT_READ_IO,		/* app direct read IOs */
11178b83ac81SChao Yu 	APP_BUFFERED_READ_IO,		/* app buffered read IOs */
11188b83ac81SChao Yu 	APP_READ_IO,			/* app read IOs */
11198b83ac81SChao Yu 	APP_MAPPED_READ_IO,		/* app mapped read IOs */
11208b83ac81SChao Yu 	FS_DATA_READ_IO,		/* data read IOs */
11219c122384SChao Yu 	FS_GDATA_READ_IO,		/* data read IOs from background gc */
11229c122384SChao Yu 	FS_CDATA_READ_IO,		/* compressed data read IOs */
11238b83ac81SChao Yu 	FS_NODE_READ_IO,		/* node read IOs */
11248b83ac81SChao Yu 	FS_META_READ_IO,		/* meta read IOs */
11258b83ac81SChao Yu 
11268b83ac81SChao Yu 	/* other */
1127b0af6d49SChao Yu 	FS_DISCARD,			/* discard */
1128b0af6d49SChao Yu 	NR_IO_TYPE,
1129b0af6d49SChao Yu };
1130b0af6d49SChao Yu 
1131458e6197SJaegeuk Kim struct f2fs_io_info {
113205ca3632SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs_sb_info pointer */
113339d787beSChao Yu 	nid_t ino;		/* inode number */
1134458e6197SJaegeuk Kim 	enum page_type type;	/* contains DATA/NODE/META/META_FLUSH */
1135a912b54dSJaegeuk Kim 	enum temp_type temp;	/* contains HOT/WARM/COLD */
113604d328deSMike Christie 	int op;			/* contains REQ_OP_ */
1137ef295ecfSChristoph Hellwig 	int op_flags;		/* req_flag_bits */
11387a9d7548SChao Yu 	block_t new_blkaddr;	/* new block address to be written */
113928bc106bSChao Yu 	block_t old_blkaddr;	/* old block address before Cow */
114005ca3632SJaegeuk Kim 	struct page *page;	/* page to be written */
11414375a336SJaegeuk Kim 	struct page *encrypted_page;	/* encrypted page */
11424c8ff709SChao Yu 	struct page *compressed_page;	/* compressed page */
1143fb830fc5SChao Yu 	struct list_head list;		/* serialize IOs */
1144d68f735bSJaegeuk Kim 	bool submitted;		/* indicate IO submission */
1145cc15620bSJaegeuk Kim 	int need_lock;		/* indicate we need to lock cp_rwsem */
1146fb830fc5SChao Yu 	bool in_list;		/* indicate fio is in io_list */
11476dc3a126SChao Yu 	bool is_por;		/* indicate IO is from recovery or not */
1148fe16efe6SChao Yu 	bool retry;		/* need to reallocate block address */
11494c8ff709SChao Yu 	int compr_blocks;	/* # of compressed block addresses */
11504c8ff709SChao Yu 	bool encrypted;		/* indicate file is encrypted */
1151b0af6d49SChao Yu 	enum iostat_type io_type;	/* io type */
1152578c6478SYufen Yu 	struct writeback_control *io_wbc; /* writeback control */
11538648de2cSChao Yu 	struct bio **bio;		/* bio for ipu */
11548648de2cSChao Yu 	sector_t *last_block;		/* last block number in bio */
11557735730dSChao Yu 	unsigned char version;		/* version of the node */
1156458e6197SJaegeuk Kim };
1157458e6197SJaegeuk Kim 
11580b20fcecSChao Yu struct bio_entry {
11590b20fcecSChao Yu 	struct bio *bio;
11600b20fcecSChao Yu 	struct list_head list;
11610b20fcecSChao Yu };
11620b20fcecSChao Yu 
116368afcf2dSTomohiro Kusumi #define is_read_io(rw) ((rw) == READ)
11641ff7bd3bSJaegeuk Kim struct f2fs_bio_info {
1165458e6197SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs superblock */
11661ff7bd3bSJaegeuk Kim 	struct bio *bio;		/* bios to merge */
11671ff7bd3bSJaegeuk Kim 	sector_t last_block_in_bio;	/* last block number */
1168458e6197SJaegeuk Kim 	struct f2fs_io_info fio;	/* store buffered io info. */
1169df0f8dc0SChao Yu 	struct rw_semaphore io_rwsem;	/* blocking op for bio */
1170fb830fc5SChao Yu 	spinlock_t io_lock;		/* serialize DATA/NODE IOs */
1171fb830fc5SChao Yu 	struct list_head io_list;	/* track fios */
11720b20fcecSChao Yu 	struct list_head bio_list;	/* bio entry list head */
11730b20fcecSChao Yu 	struct rw_semaphore bio_list_lock;	/* lock to protect bio entry list */
11741ff7bd3bSJaegeuk Kim };
11751ff7bd3bSJaegeuk Kim 
11763c62be17SJaegeuk Kim #define FDEV(i)				(sbi->devs[i])
11773c62be17SJaegeuk Kim #define RDEV(i)				(raw_super->devs[i])
11783c62be17SJaegeuk Kim struct f2fs_dev_info {
11793c62be17SJaegeuk Kim 	struct block_device *bdev;
11803c62be17SJaegeuk Kim 	char path[MAX_PATH_LEN];
11813c62be17SJaegeuk Kim 	unsigned int total_segments;
11823c62be17SJaegeuk Kim 	block_t start_blk;
11833c62be17SJaegeuk Kim 	block_t end_blk;
11843c62be17SJaegeuk Kim #ifdef CONFIG_BLK_DEV_ZONED
11853c62be17SJaegeuk Kim 	unsigned int nr_blkz;		/* Total number of zones */
118695175dafSDamien Le Moal 	unsigned long *blkz_seq;	/* Bitmap indicating sequential zones */
1187de881df9SAravind Ramesh 	block_t *zone_capacity_blocks;  /* Array of zone capacity in blks */
11883c62be17SJaegeuk Kim #endif
11893c62be17SJaegeuk Kim };
11903c62be17SJaegeuk Kim 
1191c227f912SChao Yu enum inode_type {
1192c227f912SChao Yu 	DIR_INODE,			/* for dirty dir inode */
1193c227f912SChao Yu 	FILE_INODE,			/* for dirty regular/symlink inode */
11940f18b462SJaegeuk Kim 	DIRTY_META,			/* for all dirtied inode metadata */
119557864ae5SJaegeuk Kim 	ATOMIC_FILE,			/* for all atomic files */
1196c227f912SChao Yu 	NR_INODE_TYPE,
1197c227f912SChao Yu };
1198c227f912SChao Yu 
119967298804SChao Yu /* for inner inode cache management */
120067298804SChao Yu struct inode_management {
120167298804SChao Yu 	struct radix_tree_root ino_root;	/* ino entry array */
120267298804SChao Yu 	spinlock_t ino_lock;			/* for ino entry lock */
120367298804SChao Yu 	struct list_head ino_list;		/* inode list head */
120467298804SChao Yu 	unsigned long ino_num;			/* number of entries */
120567298804SChao Yu };
120667298804SChao Yu 
1207093749e2SChao Yu /* for GC_AT */
1208093749e2SChao Yu struct atgc_management {
1209093749e2SChao Yu 	bool atgc_enabled;			/* ATGC is enabled or not */
1210093749e2SChao Yu 	struct rb_root_cached root;		/* root of victim rb-tree */
1211093749e2SChao Yu 	struct list_head victim_list;		/* linked with all victim entries */
1212093749e2SChao Yu 	unsigned int victim_count;		/* victim count in rb-tree */
1213093749e2SChao Yu 	unsigned int candidate_ratio;		/* candidate ratio */
1214093749e2SChao Yu 	unsigned int max_candidate_count;	/* max candidate count */
1215093749e2SChao Yu 	unsigned int age_weight;		/* age weight, vblock_weight = 100 - age_weight */
1216093749e2SChao Yu 	unsigned long long age_threshold;	/* age threshold */
1217093749e2SChao Yu };
1218093749e2SChao Yu 
1219caf0047eSChao Yu /* For s_flag in struct f2fs_sb_info */
1220caf0047eSChao Yu enum {
1221caf0047eSChao Yu 	SBI_IS_DIRTY,				/* dirty flag for checkpoint */
1222caf0047eSChao Yu 	SBI_IS_CLOSE,				/* specify unmounting */
1223caf0047eSChao Yu 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
1224caf0047eSChao Yu 	SBI_POR_DOING,				/* recovery is doing or not */
1225df728b0fSJaegeuk Kim 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
1226bbf156f7SJaegeuk Kim 	SBI_NEED_CP,				/* need to checkpoint */
122783a3bfdbSJaegeuk Kim 	SBI_IS_SHUTDOWN,			/* shutdown by ioctl */
12281378752bSChao Yu 	SBI_IS_RECOVERED,			/* recovered orphan/data */
12294354994fSDaniel Rosenberg 	SBI_CP_DISABLED,			/* CP was disabled last mount */
1230db610a64SJaegeuk Kim 	SBI_CP_DISABLED_QUICK,			/* CP was disabled quickly */
1231af033b2aSChao Yu 	SBI_QUOTA_NEED_FLUSH,			/* need to flush quota info in CP */
1232af033b2aSChao Yu 	SBI_QUOTA_SKIP_FLUSH,			/* skip flushing quota in current CP */
1233af033b2aSChao Yu 	SBI_QUOTA_NEED_REPAIR,			/* quota file may be corrupted */
123404f0b2eaSQiuyang Sun 	SBI_IS_RESIZEFS,			/* resizefs is in process */
1235caf0047eSChao Yu };
1236caf0047eSChao Yu 
12376beceb54SJaegeuk Kim enum {
12386beceb54SJaegeuk Kim 	CP_TIME,
1239d0239e1bSJaegeuk Kim 	REQ_TIME,
1240a7d10cf3SSahitya Tummala 	DISCARD_TIME,
1241a7d10cf3SSahitya Tummala 	GC_TIME,
12424354994fSDaniel Rosenberg 	DISABLE_TIME,
124303f2c02dSJaegeuk Kim 	UMOUNT_DISCARD_TIMEOUT,
12446beceb54SJaegeuk Kim 	MAX_TIME,
12456beceb54SJaegeuk Kim };
12466beceb54SJaegeuk Kim 
12470cdd3195SHyunchul Lee enum {
12485b0e9539SJaegeuk Kim 	GC_NORMAL,
12495b0e9539SJaegeuk Kim 	GC_IDLE_CB,
12505b0e9539SJaegeuk Kim 	GC_IDLE_GREEDY,
1251093749e2SChao Yu 	GC_IDLE_AT,
12520e5e8111SDaeho Jeong 	GC_URGENT_HIGH,
12530e5e8111SDaeho Jeong 	GC_URGENT_LOW,
12545b0e9539SJaegeuk Kim };
12555b0e9539SJaegeuk Kim 
12565b0e9539SJaegeuk Kim enum {
1257bbbc34fdSChao Yu 	BGGC_MODE_ON,		/* background gc is on */
1258bbbc34fdSChao Yu 	BGGC_MODE_OFF,		/* background gc is off */
1259bbbc34fdSChao Yu 	BGGC_MODE_SYNC,		/*
1260bbbc34fdSChao Yu 				 * background gc is on, migrating blocks
1261bbbc34fdSChao Yu 				 * like foreground gc
1262bbbc34fdSChao Yu 				 */
1263bbbc34fdSChao Yu };
1264bbbc34fdSChao Yu 
1265bbbc34fdSChao Yu enum {
1266b0332a0fSChao Yu 	FS_MODE_ADAPTIVE,	/* use both lfs/ssr allocation */
1267b0332a0fSChao Yu 	FS_MODE_LFS,		/* use lfs allocation only */
1268b0332a0fSChao Yu };
1269b0332a0fSChao Yu 
1270b0332a0fSChao Yu enum {
12710cdd3195SHyunchul Lee 	WHINT_MODE_OFF,		/* not pass down write hints */
12720cdd3195SHyunchul Lee 	WHINT_MODE_USER,	/* try to pass down hints given by users */
1273f2e703f9SHyunchul Lee 	WHINT_MODE_FS,		/* pass down hints with F2FS policy */
12740cdd3195SHyunchul Lee };
12750cdd3195SHyunchul Lee 
127607939627SJaegeuk Kim enum {
127707939627SJaegeuk Kim 	ALLOC_MODE_DEFAULT,	/* stay default */
127807939627SJaegeuk Kim 	ALLOC_MODE_REUSE,	/* reuse segments as much as possible */
127907939627SJaegeuk Kim };
128007939627SJaegeuk Kim 
128193cf93f1SJunling Zheng enum fsync_mode {
128293cf93f1SJunling Zheng 	FSYNC_MODE_POSIX,	/* fsync follows posix semantics */
128393cf93f1SJunling Zheng 	FSYNC_MODE_STRICT,	/* fsync behaves in line with ext4 */
1284d6290814SJaegeuk Kim 	FSYNC_MODE_NOBARRIER,	/* fsync behaves nobarrier based on posix */
128593cf93f1SJunling Zheng };
128693cf93f1SJunling Zheng 
1287602a16d5SDaeho Jeong enum {
1288602a16d5SDaeho Jeong 	COMPR_MODE_FS,		/*
1289602a16d5SDaeho Jeong 				 * automatically compress compression
1290602a16d5SDaeho Jeong 				 * enabled files
1291602a16d5SDaeho Jeong 				 */
1292602a16d5SDaeho Jeong 	COMPR_MODE_USER,	/*
1293602a16d5SDaeho Jeong 				 * automatical compression is disabled.
1294602a16d5SDaeho Jeong 				 * user can control the file compression
1295602a16d5SDaeho Jeong 				 * using ioctls
1296602a16d5SDaeho Jeong 				 */
1297602a16d5SDaeho Jeong };
1298602a16d5SDaeho Jeong 
1299b763f3beSChao Yu static inline int f2fs_test_bit(unsigned int nr, char *addr);
1300b763f3beSChao Yu static inline void f2fs_set_bit(unsigned int nr, char *addr);
1301b763f3beSChao Yu static inline void f2fs_clear_bit(unsigned int nr, char *addr);
13024c8ff709SChao Yu 
1303b763f3beSChao Yu /*
1304b763f3beSChao Yu  * Layout of f2fs page.private:
1305b763f3beSChao Yu  *
1306b763f3beSChao Yu  * Layout A: lowest bit should be 1
1307b763f3beSChao Yu  * | bit0 = 1 | bit1 | bit2 | ... | bit MAX | private data .... |
1308b763f3beSChao Yu  * bit 0	PAGE_PRIVATE_NOT_POINTER
1309b763f3beSChao Yu  * bit 1	PAGE_PRIVATE_ATOMIC_WRITE
1310b763f3beSChao Yu  * bit 2	PAGE_PRIVATE_DUMMY_WRITE
1311b763f3beSChao Yu  * bit 3	PAGE_PRIVATE_ONGOING_MIGRATION
1312b763f3beSChao Yu  * bit 4	PAGE_PRIVATE_INLINE_INODE
1313b763f3beSChao Yu  * bit 5	PAGE_PRIVATE_REF_RESOURCE
1314b763f3beSChao Yu  * bit 6-	f2fs private data
1315b763f3beSChao Yu  *
1316b763f3beSChao Yu  * Layout B: lowest bit should be 0
1317b763f3beSChao Yu  * page.private is a wrapped pointer.
1318b763f3beSChao Yu  */
1319b763f3beSChao Yu enum {
1320b763f3beSChao Yu 	PAGE_PRIVATE_NOT_POINTER,		/* private contains non-pointer data */
1321b763f3beSChao Yu 	PAGE_PRIVATE_ATOMIC_WRITE,		/* data page from atomic write path */
1322b763f3beSChao Yu 	PAGE_PRIVATE_DUMMY_WRITE,		/* data page for padding aligned IO */
1323b763f3beSChao Yu 	PAGE_PRIVATE_ONGOING_MIGRATION,		/* data page which is on-going migrating */
1324b763f3beSChao Yu 	PAGE_PRIVATE_INLINE_INODE,		/* inode page contains inline data */
1325b763f3beSChao Yu 	PAGE_PRIVATE_REF_RESOURCE,		/* dirty page has referenced resources */
1326b763f3beSChao Yu 	PAGE_PRIVATE_MAX
1327b763f3beSChao Yu };
1328b763f3beSChao Yu 
1329b763f3beSChao Yu #define PAGE_PRIVATE_GET_FUNC(name, flagname) \
1330b763f3beSChao Yu static inline bool page_private_##name(struct page *page) \
1331b763f3beSChao Yu { \
1332b763f3beSChao Yu 	return test_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page)) && \
1333b763f3beSChao Yu 		test_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
1334b763f3beSChao Yu }
1335b763f3beSChao Yu 
1336b763f3beSChao Yu #define PAGE_PRIVATE_SET_FUNC(name, flagname) \
1337b763f3beSChao Yu static inline void set_page_private_##name(struct page *page) \
1338b763f3beSChao Yu { \
1339b763f3beSChao Yu 	if (!PagePrivate(page)) { \
1340b763f3beSChao Yu 		get_page(page); \
1341b763f3beSChao Yu 		SetPagePrivate(page); \
1342b763f3beSChao Yu 	} \
1343b763f3beSChao Yu 	set_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page)); \
1344b763f3beSChao Yu 	set_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
1345b763f3beSChao Yu }
1346b763f3beSChao Yu 
1347b763f3beSChao Yu #define PAGE_PRIVATE_CLEAR_FUNC(name, flagname) \
1348b763f3beSChao Yu static inline void clear_page_private_##name(struct page *page) \
1349b763f3beSChao Yu { \
1350b763f3beSChao Yu 	clear_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
1351b763f3beSChao Yu 	if (page_private(page) == 1 << PAGE_PRIVATE_NOT_POINTER) { \
1352b763f3beSChao Yu 		set_page_private(page, 0); \
1353b763f3beSChao Yu 		if (PagePrivate(page)) { \
1354b763f3beSChao Yu 			ClearPagePrivate(page); \
1355b763f3beSChao Yu 			put_page(page); \
1356b763f3beSChao Yu 		}\
1357b763f3beSChao Yu 	} \
1358b763f3beSChao Yu }
1359b763f3beSChao Yu 
1360b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(nonpointer, NOT_POINTER);
1361b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(reference, REF_RESOURCE);
1362b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(inline, INLINE_INODE);
1363b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(gcing, ONGOING_MIGRATION);
1364b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(atomic, ATOMIC_WRITE);
1365b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(dummy, DUMMY_WRITE);
1366b763f3beSChao Yu 
1367b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(reference, REF_RESOURCE);
1368b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(inline, INLINE_INODE);
1369b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(gcing, ONGOING_MIGRATION);
1370b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(atomic, ATOMIC_WRITE);
1371b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(dummy, DUMMY_WRITE);
1372b763f3beSChao Yu 
1373b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(reference, REF_RESOURCE);
1374b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(inline, INLINE_INODE);
1375b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(gcing, ONGOING_MIGRATION);
1376b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(atomic, ATOMIC_WRITE);
1377b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(dummy, DUMMY_WRITE);
13784c8ff709SChao Yu 
13796ce19affSChao Yu static inline unsigned long get_page_private_data(struct page *page)
13806ce19affSChao Yu {
13816ce19affSChao Yu 	unsigned long data = page_private(page);
13826ce19affSChao Yu 
13836ce19affSChao Yu 	if (!test_bit(PAGE_PRIVATE_NOT_POINTER, &data))
13846ce19affSChao Yu 		return 0;
13856ce19affSChao Yu 	return data >> PAGE_PRIVATE_MAX;
13866ce19affSChao Yu }
13876ce19affSChao Yu 
13886ce19affSChao Yu static inline void set_page_private_data(struct page *page, unsigned long data)
13896ce19affSChao Yu {
13906ce19affSChao Yu 	if (!PagePrivate(page)) {
13916ce19affSChao Yu 		get_page(page);
13926ce19affSChao Yu 		SetPagePrivate(page);
13936ce19affSChao Yu 	}
13946ce19affSChao Yu 	set_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page));
13956ce19affSChao Yu 	page_private(page) |= data << PAGE_PRIVATE_MAX;
13966ce19affSChao Yu }
13976ce19affSChao Yu 
13986ce19affSChao Yu static inline void clear_page_private_data(struct page *page)
13996ce19affSChao Yu {
14006ce19affSChao Yu 	page_private(page) &= (1 << PAGE_PRIVATE_MAX) - 1;
14016ce19affSChao Yu 	if (page_private(page) == 1 << PAGE_PRIVATE_NOT_POINTER) {
14026ce19affSChao Yu 		set_page_private(page, 0);
14036ce19affSChao Yu 		if (PagePrivate(page)) {
14046ce19affSChao Yu 			ClearPagePrivate(page);
14056ce19affSChao Yu 			put_page(page);
14066ce19affSChao Yu 		}
14076ce19affSChao Yu 	}
14086ce19affSChao Yu }
14096ce19affSChao Yu 
14104c8ff709SChao Yu /* For compression */
14114c8ff709SChao Yu enum compress_algorithm_type {
14124c8ff709SChao Yu 	COMPRESS_LZO,
14134c8ff709SChao Yu 	COMPRESS_LZ4,
141450cfa66fSChao Yu 	COMPRESS_ZSTD,
14156d92b201SChao Yu 	COMPRESS_LZORLE,
14164c8ff709SChao Yu 	COMPRESS_MAX,
14174c8ff709SChao Yu };
14184c8ff709SChao Yu 
1419b28f047bSChao Yu enum compress_flag {
1420b28f047bSChao Yu 	COMPRESS_CHKSUM,
1421b28f047bSChao Yu 	COMPRESS_MAX_FLAG,
1422b28f047bSChao Yu };
1423b28f047bSChao Yu 
14246ce19affSChao Yu #define	COMPRESS_WATERMARK			20
14256ce19affSChao Yu #define	COMPRESS_PERCENT			20
14266ce19affSChao Yu 
1427b28f047bSChao Yu #define COMPRESS_DATA_RESERVED_SIZE		4
14284c8ff709SChao Yu struct compress_data {
14294c8ff709SChao Yu 	__le32 clen;			/* compressed data size */
1430b28f047bSChao Yu 	__le32 chksum;			/* compressed data chksum */
14314c8ff709SChao Yu 	__le32 reserved[COMPRESS_DATA_RESERVED_SIZE];	/* reserved */
14324c8ff709SChao Yu 	u8 cdata[];			/* compressed data */
14334c8ff709SChao Yu };
14344c8ff709SChao Yu 
14354c8ff709SChao Yu #define COMPRESS_HEADER_SIZE	(sizeof(struct compress_data))
14364c8ff709SChao Yu 
14374c8ff709SChao Yu #define F2FS_COMPRESSED_PAGE_MAGIC	0xF5F2C000
14384c8ff709SChao Yu 
14393fde13f8SChao Yu #define	COMPRESS_LEVEL_OFFSET	8
14403fde13f8SChao Yu 
14414c8ff709SChao Yu /* compress context */
14424c8ff709SChao Yu struct compress_ctx {
14434c8ff709SChao Yu 	struct inode *inode;		/* inode the context belong to */
14444c8ff709SChao Yu 	pgoff_t cluster_idx;		/* cluster index number */
14454c8ff709SChao Yu 	unsigned int cluster_size;	/* page count in cluster */
14464c8ff709SChao Yu 	unsigned int log_cluster_size;	/* log of cluster size */
14474c8ff709SChao Yu 	struct page **rpages;		/* pages store raw data in cluster */
14484c8ff709SChao Yu 	unsigned int nr_rpages;		/* total page number in rpages */
14494c8ff709SChao Yu 	struct page **cpages;		/* pages store compressed data in cluster */
14504c8ff709SChao Yu 	unsigned int nr_cpages;		/* total page number in cpages */
14514c8ff709SChao Yu 	void *rbuf;			/* virtual mapped address on rpages */
14524c8ff709SChao Yu 	struct compress_data *cbuf;	/* virtual mapped address on cpages */
14534c8ff709SChao Yu 	size_t rlen;			/* valid data length in rbuf */
14544c8ff709SChao Yu 	size_t clen;			/* valid data length in cbuf */
14554c8ff709SChao Yu 	void *private;			/* payload buffer for specified compression algorithm */
145650cfa66fSChao Yu 	void *private2;			/* extra payload buffer */
14574c8ff709SChao Yu };
14584c8ff709SChao Yu 
14594c8ff709SChao Yu /* compress context for write IO path */
14604c8ff709SChao Yu struct compress_io_ctx {
14614c8ff709SChao Yu 	u32 magic;			/* magic number to indicate page is compressed */
14624c8ff709SChao Yu 	struct inode *inode;		/* inode the context belong to */
14634c8ff709SChao Yu 	struct page **rpages;		/* pages store raw data in cluster */
14644c8ff709SChao Yu 	unsigned int nr_rpages;		/* total page number in rpages */
1465e6c3948dSChao Yu 	atomic_t pending_pages;		/* in-flight compressed page count */
14664c8ff709SChao Yu };
14674c8ff709SChao Yu 
14687f59b277SEric Biggers /* Context for decompressing one cluster on the read IO path */
14694c8ff709SChao Yu struct decompress_io_ctx {
14704c8ff709SChao Yu 	u32 magic;			/* magic number to indicate page is compressed */
14714c8ff709SChao Yu 	struct inode *inode;		/* inode the context belong to */
14724c8ff709SChao Yu 	pgoff_t cluster_idx;		/* cluster index number */
14734c8ff709SChao Yu 	unsigned int cluster_size;	/* page count in cluster */
14744c8ff709SChao Yu 	unsigned int log_cluster_size;	/* log of cluster size */
14754c8ff709SChao Yu 	struct page **rpages;		/* pages store raw data in cluster */
14764c8ff709SChao Yu 	unsigned int nr_rpages;		/* total page number in rpages */
14774c8ff709SChao Yu 	struct page **cpages;		/* pages store compressed data in cluster */
14784c8ff709SChao Yu 	unsigned int nr_cpages;		/* total page number in cpages */
14794c8ff709SChao Yu 	struct page **tpages;		/* temp pages to pad holes in cluster */
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 */
14847f59b277SEric Biggers 
14857f59b277SEric Biggers 	/*
14867f59b277SEric Biggers 	 * The number of compressed pages remaining to be read in this cluster.
14877f59b277SEric Biggers 	 * This is initially nr_cpages.  It is decremented by 1 each time a page
14887f59b277SEric Biggers 	 * has been read (or failed to be read).  When it reaches 0, the cluster
14897f59b277SEric Biggers 	 * is decompressed (or an error is reported).
14907f59b277SEric Biggers 	 *
14917f59b277SEric Biggers 	 * If an error occurs before all the pages have been submitted for I/O,
14927f59b277SEric Biggers 	 * then this will never reach 0.  In this case the I/O submitter is
14937f59b277SEric Biggers 	 * responsible for calling f2fs_decompress_end_io() instead.
14947f59b277SEric Biggers 	 */
14957f59b277SEric Biggers 	atomic_t remaining_pages;
14967f59b277SEric Biggers 
14977f59b277SEric Biggers 	/*
14987f59b277SEric Biggers 	 * Number of references to this decompress_io_ctx.
14997f59b277SEric Biggers 	 *
15007f59b277SEric Biggers 	 * One reference is held for I/O completion.  This reference is dropped
15017f59b277SEric Biggers 	 * after the pagecache pages are updated and unlocked -- either after
15027f59b277SEric Biggers 	 * decompression (and verity if enabled), or after an error.
15037f59b277SEric Biggers 	 *
15047f59b277SEric Biggers 	 * In addition, each compressed page holds a reference while it is in a
15057f59b277SEric Biggers 	 * bio.  These references are necessary prevent compressed pages from
15067f59b277SEric Biggers 	 * being freed while they are still in a bio.
15077f59b277SEric Biggers 	 */
15087f59b277SEric Biggers 	refcount_t refcnt;
15097f59b277SEric Biggers 
15107f59b277SEric Biggers 	bool failed;			/* IO error occurred before decompression? */
15117f59b277SEric Biggers 	bool need_verity;		/* need fs-verity verification after decompression? */
151250cfa66fSChao Yu 	void *private;			/* payload buffer for specified decompression algorithm */
151350cfa66fSChao Yu 	void *private2;			/* extra payload buffer */
15147f59b277SEric Biggers 	struct work_struct verity_work;	/* work to verify the decompressed pages */
15154c8ff709SChao Yu };
15164c8ff709SChao Yu 
15174c8ff709SChao Yu #define NULL_CLUSTER			((unsigned int)(~0))
15184c8ff709SChao Yu #define MIN_COMPRESS_LOG_SIZE		2
15194c8ff709SChao Yu #define MAX_COMPRESS_LOG_SIZE		8
15200e2b7385SChao Yu #define MAX_COMPRESS_WINDOW_SIZE(log_size)	((PAGE_SIZE) << (log_size))
15214c8ff709SChao Yu 
152239a53e0cSJaegeuk Kim struct f2fs_sb_info {
152339a53e0cSJaegeuk Kim 	struct super_block *sb;			/* pointer to VFS super block */
15245e176d54SJaegeuk Kim 	struct proc_dir_entry *s_proc;		/* proc entry */
152539a53e0cSJaegeuk Kim 	struct f2fs_super_block *raw_super;	/* raw super block pointer */
1526846ae671SChao Yu 	struct rw_semaphore sb_lock;		/* lock for raw super block */
1527e8240f65SChao Yu 	int valid_super_block;			/* valid super block no */
1528fadb2fb8SChao Yu 	unsigned long s_flag;				/* flags for sbi */
1529853137ceSJaegeuk Kim 	struct mutex writepages;		/* mutex for writepages() */
153039a53e0cSJaegeuk Kim 
1531178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
1532178053e2SDamien Le Moal 	unsigned int blocks_per_blkz;		/* F2FS blocks per zone */
1533178053e2SDamien Le Moal 	unsigned int log_blocks_per_blkz;	/* log2 F2FS blocks per zone */
1534178053e2SDamien Le Moal #endif
1535178053e2SDamien Le Moal 
153639a53e0cSJaegeuk Kim 	/* for node-related operations */
153739a53e0cSJaegeuk Kim 	struct f2fs_nm_info *nm_info;		/* node manager */
153839a53e0cSJaegeuk Kim 	struct inode *node_inode;		/* cache node blocks */
153939a53e0cSJaegeuk Kim 
154039a53e0cSJaegeuk Kim 	/* for segment-related operations */
154139a53e0cSJaegeuk Kim 	struct f2fs_sm_info *sm_info;		/* segment manager */
15421ff7bd3bSJaegeuk Kim 
15431ff7bd3bSJaegeuk Kim 	/* for bio operations */
1544a912b54dSJaegeuk Kim 	struct f2fs_bio_info *write_io[NR_PAGE_TYPE];	/* for write bios */
1545107a805dSChao Yu 	/* keep migration IO order for LFS mode */
1546107a805dSChao Yu 	struct rw_semaphore io_order_lock;
15470a595ebaSJaegeuk Kim 	mempool_t *write_io_dummy;		/* Dummy pages */
154839a53e0cSJaegeuk Kim 
154939a53e0cSJaegeuk Kim 	/* for checkpoint */
155039a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt;		/* raw checkpoint pointer */
15518508e44aSJaegeuk Kim 	int cur_cp_pack;			/* remain current cp pack */
1552aaec2b1dSChao Yu 	spinlock_t cp_lock;			/* for flag in ckpt */
155339a53e0cSJaegeuk Kim 	struct inode *meta_inode;		/* cache meta blocks */
15548769918bSSahitya Tummala 	struct rw_semaphore cp_global_sem;	/* checkpoint procedure lock */
1555b873b798SJaegeuk Kim 	struct rw_semaphore cp_rwsem;		/* blocking FS operations */
1556b3582c68SChao Yu 	struct rw_semaphore node_write;		/* locking node writes */
155759c9081bSYunlei He 	struct rw_semaphore node_change;	/* locking node change */
1558fb51b5efSChangman Lee 	wait_queue_head_t cp_wait;
15596beceb54SJaegeuk Kim 	unsigned long last_time[MAX_TIME];	/* to store time in jiffies */
15606beceb54SJaegeuk Kim 	long interval_time[MAX_TIME];		/* to store thresholds */
1561261eeb9cSDaeho Jeong 	struct ckpt_req_control cprc_info;	/* for checkpoint request control */
156239a53e0cSJaegeuk Kim 
156367298804SChao Yu 	struct inode_management im[MAX_INO_ENTRY];	/* manage inode cache */
15646451e041SJaegeuk Kim 
156550fa53ecSChao Yu 	spinlock_t fsync_node_lock;		/* for node entry lock */
156650fa53ecSChao Yu 	struct list_head fsync_node_list;	/* node list head */
156750fa53ecSChao Yu 	unsigned int fsync_seg_id;		/* sequence id */
156850fa53ecSChao Yu 	unsigned int fsync_node_num;		/* number of node entries */
156950fa53ecSChao Yu 
15706451e041SJaegeuk Kim 	/* for orphan inode, use 0'th array */
15710d47c1adSGu Zheng 	unsigned int max_orphans;		/* max orphan inodes */
157239a53e0cSJaegeuk Kim 
1573c227f912SChao Yu 	/* for inode management */
1574c227f912SChao Yu 	struct list_head inode_list[NR_INODE_TYPE];	/* dirty inode list */
1575c227f912SChao Yu 	spinlock_t inode_lock[NR_INODE_TYPE];	/* for dirty inode list lock */
1576040d2bb3SChao Yu 	struct mutex flush_lock;		/* for flush exclusion */
157739a53e0cSJaegeuk Kim 
157813054c54SChao Yu 	/* for extent tree cache */
157913054c54SChao Yu 	struct radix_tree_root extent_tree_root;/* cache extent cache entries */
15805e8256acSYunlei He 	struct mutex extent_tree_lock;	/* locking extent radix tree */
158113054c54SChao Yu 	struct list_head extent_list;		/* lru list for shrinker */
158213054c54SChao Yu 	spinlock_t extent_lock;			/* locking extent lru list */
15837441ccefSJaegeuk Kim 	atomic_t total_ext_tree;		/* extent tree count */
1584137d09f0SJaegeuk Kim 	struct list_head zombie_list;		/* extent zombie tree list */
158574fd8d99SJaegeuk Kim 	atomic_t total_zombie_tree;		/* extent zombie tree count */
158613054c54SChao Yu 	atomic_t total_ext_node;		/* extent info count */
158713054c54SChao Yu 
158839a53e0cSJaegeuk Kim 	/* basic filesystem units */
158939a53e0cSJaegeuk Kim 	unsigned int log_sectors_per_block;	/* log2 sectors per block */
159039a53e0cSJaegeuk Kim 	unsigned int log_blocksize;		/* log2 block size */
159139a53e0cSJaegeuk Kim 	unsigned int blocksize;			/* block size */
159239a53e0cSJaegeuk Kim 	unsigned int root_ino_num;		/* root inode number*/
159339a53e0cSJaegeuk Kim 	unsigned int node_ino_num;		/* node inode number*/
159439a53e0cSJaegeuk Kim 	unsigned int meta_ino_num;		/* meta inode number*/
159539a53e0cSJaegeuk Kim 	unsigned int log_blocks_per_seg;	/* log2 blocks per segment */
159639a53e0cSJaegeuk Kim 	unsigned int blocks_per_seg;		/* blocks per segment */
159739a53e0cSJaegeuk Kim 	unsigned int segs_per_sec;		/* segments per section */
159839a53e0cSJaegeuk Kim 	unsigned int secs_per_zone;		/* sections per zone */
159939a53e0cSJaegeuk Kim 	unsigned int total_sections;		/* total section count */
160039a53e0cSJaegeuk Kim 	unsigned int total_node_count;		/* total node block count */
160139a53e0cSJaegeuk Kim 	unsigned int total_valid_node_count;	/* valid node block count */
1602ab9fa662SJaegeuk Kim 	int dir_level;				/* directory level */
1603f6df8f23SSheng Yong 	int readdir_ra;				/* readahead inode in readdir */
160410208567SJaegeuk Kim 	u64 max_io_bytes;			/* max io bytes to merge IOs */
160539a53e0cSJaegeuk Kim 
160639a53e0cSJaegeuk Kim 	block_t user_block_count;		/* # of user blocks */
160739a53e0cSJaegeuk Kim 	block_t total_valid_block_count;	/* # of valid blocks */
1608a66cdd98SJaegeuk Kim 	block_t discard_blks;			/* discard command candidats */
160939a53e0cSJaegeuk Kim 	block_t last_valid_block_count;		/* for recovery */
1610daeb433eSChao Yu 	block_t reserved_blocks;		/* configurable reserved blocks */
161180d42145SYunlong Song 	block_t current_reserved_blocks;	/* current reserved blocks */
1612daeb433eSChao Yu 
16134354994fSDaniel Rosenberg 	/* Additional tracking for no checkpoint mode */
16144354994fSDaniel Rosenberg 	block_t unusable_block_count;		/* # of blocks saved by last cp */
16154354994fSDaniel Rosenberg 
1616292c196aSChao Yu 	unsigned int nquota_files;		/* # of quota sysfile */
1617db6ec53bSJaegeuk Kim 	struct rw_semaphore quota_sem;		/* blocking cp for flags */
1618292c196aSChao Yu 
1619523be8a6SJaegeuk Kim 	/* # of pages, see count_type */
162035782b23SJaegeuk Kim 	atomic_t nr_pages[NR_COUNT_TYPE];
162141382ec4SJaegeuk Kim 	/* # of allocated blocks */
162241382ec4SJaegeuk Kim 	struct percpu_counter alloc_valid_block_count;
162339a53e0cSJaegeuk Kim 
1624687de7f1SJaegeuk Kim 	/* writeback control */
1625c29fd0c0SChao Yu 	atomic_t wb_sync_req[META];	/* count # of WB_SYNC threads */
1626687de7f1SJaegeuk Kim 
1627513c5f37SJaegeuk Kim 	/* valid inode count */
1628513c5f37SJaegeuk Kim 	struct percpu_counter total_valid_inode_count;
1629513c5f37SJaegeuk Kim 
163039a53e0cSJaegeuk Kim 	struct f2fs_mount_info mount_opt;	/* mount options */
163139a53e0cSJaegeuk Kim 
163239a53e0cSJaegeuk Kim 	/* for cleaning operations */
1633fb24fea7SChao Yu 	struct rw_semaphore gc_lock;		/*
1634fb24fea7SChao Yu 						 * semaphore for GC, avoid
1635fb24fea7SChao Yu 						 * race between GC and GC or CP
1636fb24fea7SChao Yu 						 */
163739a53e0cSJaegeuk Kim 	struct f2fs_gc_kthread	*gc_thread;	/* GC thread */
1638093749e2SChao Yu 	struct atgc_management am;		/* atgc management */
16395ec4e49fSJaegeuk Kim 	unsigned int cur_victim_sec;		/* current victim section num */
16405b0e9539SJaegeuk Kim 	unsigned int gc_mode;			/* current GC state */
1641e3080b01SChao Yu 	unsigned int next_victim_seg[2];	/* next segment in victim section */
16420e5e8111SDaeho Jeong 
16432ef79ecbSChao Yu 	/* for skip statistic */
1644677017d1SSahitya Tummala 	unsigned int atomic_files;		/* # of opened atomic file */
16452ef79ecbSChao Yu 	unsigned long long skipped_atomic_files[2];	/* FG_GC and BG_GC */
16466f8d4455SJaegeuk Kim 	unsigned long long skipped_gc_rwsem;		/* FG_GC only */
164739a53e0cSJaegeuk Kim 
16481ad71a27SJaegeuk Kim 	/* threshold for gc trials on pinned files */
16491ad71a27SJaegeuk Kim 	u64 gc_pin_file_threshold;
1650f5a53edcSJaegeuk Kim 	struct rw_semaphore pin_sem;
16511ad71a27SJaegeuk Kim 
1652b1c57c1cSJaegeuk Kim 	/* maximum # of trials to find a victim segment for SSR and GC */
1653b1c57c1cSJaegeuk Kim 	unsigned int max_victim_search;
1654e3080b01SChao Yu 	/* migration granularity of garbage collection, unit: segment */
1655e3080b01SChao Yu 	unsigned int migration_granularity;
1656b1c57c1cSJaegeuk Kim 
165739a53e0cSJaegeuk Kim 	/*
165839a53e0cSJaegeuk Kim 	 * for stat information.
165939a53e0cSJaegeuk Kim 	 * one is for the LFS mode, and the other is for the SSR mode.
166039a53e0cSJaegeuk Kim 	 */
166135b09d82SNamjae Jeon #ifdef CONFIG_F2FS_STAT_FS
166239a53e0cSJaegeuk Kim 	struct f2fs_stat_info *stat_info;	/* FS status information */
1663b63e7be5SChao Yu 	atomic_t meta_count[META_MAX];		/* # of meta blocks */
166439a53e0cSJaegeuk Kim 	unsigned int segment_count[2];		/* # of allocated segments */
166539a53e0cSJaegeuk Kim 	unsigned int block_count[2];		/* # of allocated blocks */
1666b9a2c252SChangman Lee 	atomic_t inplace_count;		/* # of inplace update */
16675b7ee374SChao Yu 	atomic64_t total_hit_ext;		/* # of lookup extent cache */
16685b7ee374SChao Yu 	atomic64_t read_hit_rbtree;		/* # of hit rbtree extent node */
16695b7ee374SChao Yu 	atomic64_t read_hit_largest;		/* # of hit largest extent node */
16705b7ee374SChao Yu 	atomic64_t read_hit_cached;		/* # of hit cached extent node */
1671d5e8f6c9SChao Yu 	atomic_t inline_xattr;			/* # of inline_xattr inodes */
167203e14d52SChao Yu 	atomic_t inline_inode;			/* # of inline_data inodes */
167303e14d52SChao Yu 	atomic_t inline_dir;			/* # of inline_dentry inodes */
16744c8ff709SChao Yu 	atomic_t compr_inode;			/* # of compressed inodes */
1675ae999bb9SDaeho Jeong 	atomic64_t compr_blocks;		/* # of compressed blocks */
1676648d50baSChao Yu 	atomic_t vw_cnt;			/* # of volatile writes */
167726a28a0cSJaegeuk Kim 	atomic_t max_aw_cnt;			/* max # of atomic writes */
1678648d50baSChao Yu 	atomic_t max_vw_cnt;			/* max # of volatile writes */
1679274bd9baSChao Yu 	unsigned int io_skip_bggc;		/* skip background gc for in-flight IO */
1680274bd9baSChao Yu 	unsigned int other_skip_bggc;		/* skip background gc for other reasons */
168133fbd510SChao Yu 	unsigned int ndirty_inode[NR_INODE_TYPE];	/* # of dirty inodes */
168235b09d82SNamjae Jeon #endif
168339a53e0cSJaegeuk Kim 	spinlock_t stat_lock;			/* lock for stat operations */
1684b59d0baeSNamjae Jeon 
1685b0af6d49SChao Yu 	/* For app/fs IO statistics */
1686b0af6d49SChao Yu 	spinlock_t iostat_lock;
16878b83ac81SChao Yu 	unsigned long long rw_iostat[NR_IO_TYPE];
16888b83ac81SChao Yu 	unsigned long long prev_rw_iostat[NR_IO_TYPE];
1689b0af6d49SChao Yu 	bool iostat_enable;
16902bc4bea3SDaeho Jeong 	unsigned long iostat_next_period;
16912bc4bea3SDaeho Jeong 	unsigned int iostat_period_ms;
1692b0af6d49SChao Yu 
1693da9953b7SJaegeuk Kim 	/* to attach REQ_META|REQ_FUA flags */
1694da9953b7SJaegeuk Kim 	unsigned int data_io_flag;
169532b6aba8SJaegeuk Kim 	unsigned int node_io_flag;
1696b59d0baeSNamjae Jeon 
1697b59d0baeSNamjae Jeon 	/* For sysfs suppport */
16985d4daa57SChao Yu 	struct kobject s_kobj;			/* /sys/fs/f2fs/<devname> */
1699b59d0baeSNamjae Jeon 	struct completion s_kobj_unregister;
17002658e50dSJaegeuk Kim 
17015d4daa57SChao Yu 	struct kobject s_stat_kobj;		/* /sys/fs/f2fs/<devname>/stat */
17025d4daa57SChao Yu 	struct completion s_stat_kobj_unregister;
17035d4daa57SChao Yu 
17044c89b53dSJaegeuk Kim 	struct kobject s_feature_list_kobj;		/* /sys/fs/f2fs/<devname>/feature_list */
17054c89b53dSJaegeuk Kim 	struct completion s_feature_list_kobj_unregister;
17064c89b53dSJaegeuk Kim 
17072658e50dSJaegeuk Kim 	/* For shrinker support */
17082658e50dSJaegeuk Kim 	struct list_head s_list;
17093c62be17SJaegeuk Kim 	int s_ndevs;				/* number of devices */
17103c62be17SJaegeuk Kim 	struct f2fs_dev_info *devs;		/* for device list */
17111228b482SChao Yu 	unsigned int dirty_device;		/* for checkpoint data flush */
17121228b482SChao Yu 	spinlock_t dev_lock;			/* protect dirty_device */
17132658e50dSJaegeuk Kim 	struct mutex umount_mutex;
17142658e50dSJaegeuk Kim 	unsigned int shrinker_run_no;
17158f1dbbbbSShuoran Liu 
17168f1dbbbbSShuoran Liu 	/* For write statistics */
17178f1dbbbbSShuoran Liu 	u64 sectors_written_start;
17188f1dbbbbSShuoran Liu 	u64 kbytes_written;
171943b6573bSKeith Mok 
172043b6573bSKeith Mok 	/* Reference to checksum algorithm driver via cryptoapi */
172143b6573bSKeith Mok 	struct crypto_shash *s_chksum_driver;
17221ecc0c5cSChao Yu 
1723704956ecSChao Yu 	/* Precomputed FS UUID checksum for seeding other checksums */
1724704956ecSChao Yu 	__u32 s_chksum_seed;
17254c8ff709SChao Yu 
17264c8ff709SChao Yu 	struct workqueue_struct *post_read_wq;	/* post read workqueue */
1727a999150fSChao Yu 
1728a999150fSChao Yu 	struct kmem_cache *inline_xattr_slab;	/* inline xattr entry */
1729a999150fSChao Yu 	unsigned int inline_xattr_slab_size;	/* default inline xattr slab size */
173031083031SChao Yu 
173131083031SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
173231083031SChao Yu 	struct kmem_cache *page_array_slab;	/* page array entry */
173331083031SChao Yu 	unsigned int page_array_slab_size;	/* default page array slab size */
17345ac443e2SDaeho Jeong 
17355ac443e2SDaeho Jeong 	/* For runtime compression statistics */
17365ac443e2SDaeho Jeong 	u64 compr_written_block;
17375ac443e2SDaeho Jeong 	u64 compr_saved_block;
17385ac443e2SDaeho Jeong 	u32 compr_new_inode;
17396ce19affSChao Yu 
17406ce19affSChao Yu 	/* For compressed block cache */
17416ce19affSChao Yu 	struct inode *compress_inode;		/* cache compressed blocks */
17426ce19affSChao Yu 	unsigned int compress_percent;		/* cache page percentage */
17436ce19affSChao Yu 	unsigned int compress_watermark;	/* cache page watermark */
17446ce19affSChao Yu 	atomic_t compress_page_hit;		/* cache hit count */
174531083031SChao Yu #endif
174639a53e0cSJaegeuk Kim };
174739a53e0cSJaegeuk Kim 
174802b16d0aSChao Yu struct f2fs_private_dio {
174902b16d0aSChao Yu 	struct inode *inode;
175002b16d0aSChao Yu 	void *orig_private;
175102b16d0aSChao Yu 	bio_end_io_t *orig_end_io;
175202b16d0aSChao Yu 	bool write;
175302b16d0aSChao Yu };
175402b16d0aSChao Yu 
17551ecc0c5cSChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
1756c45d6002SChao Yu #define f2fs_show_injection_info(sbi, type)					\
1757c45d6002SChao Yu 	printk_ratelimited("%sF2FS-fs (%s) : inject %s in %s of %pS\n",	\
1758c45d6002SChao Yu 		KERN_INFO, sbi->sb->s_id,				\
1759c45d6002SChao Yu 		f2fs_fault_name[type],					\
176055523519SChao Yu 		__func__, __builtin_return_address(0))
17611ecc0c5cSChao Yu static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
17621ecc0c5cSChao Yu {
176363189b78SChao Yu 	struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
17641ecc0c5cSChao Yu 
17651ecc0c5cSChao Yu 	if (!ffi->inject_rate)
17661ecc0c5cSChao Yu 		return false;
17671ecc0c5cSChao Yu 
17681ecc0c5cSChao Yu 	if (!IS_FAULT_SET(ffi, type))
17691ecc0c5cSChao Yu 		return false;
17701ecc0c5cSChao Yu 
17711ecc0c5cSChao Yu 	atomic_inc(&ffi->inject_ops);
17721ecc0c5cSChao Yu 	if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
17731ecc0c5cSChao Yu 		atomic_set(&ffi->inject_ops, 0);
17741ecc0c5cSChao Yu 		return true;
17751ecc0c5cSChao Yu 	}
17761ecc0c5cSChao Yu 	return false;
17771ecc0c5cSChao Yu }
17787fa750a1SArnd Bergmann #else
1779c45d6002SChao Yu #define f2fs_show_injection_info(sbi, type) do { } while (0)
17807fa750a1SArnd Bergmann static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
17817fa750a1SArnd Bergmann {
17827fa750a1SArnd Bergmann 	return false;
17837fa750a1SArnd Bergmann }
17841ecc0c5cSChao Yu #endif
17851ecc0c5cSChao Yu 
17860916878dSDamien Le Moal /*
17870916878dSDamien Le Moal  * Test if the mounted volume is a multi-device volume.
17880916878dSDamien Le Moal  *   - For a single regular disk volume, sbi->s_ndevs is 0.
17890916878dSDamien Le Moal  *   - For a single zoned disk volume, sbi->s_ndevs is 1.
17900916878dSDamien Le Moal  *   - For a multi-device volume, sbi->s_ndevs is always 2 or more.
17910916878dSDamien Le Moal  */
17920916878dSDamien Le Moal static inline bool f2fs_is_multi_device(struct f2fs_sb_info *sbi)
17930916878dSDamien Le Moal {
17940916878dSDamien Le Moal 	return sbi->s_ndevs > 1;
17950916878dSDamien Le Moal }
17960916878dSDamien Le Moal 
17976beceb54SJaegeuk Kim static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
17986beceb54SJaegeuk Kim {
1799a7d10cf3SSahitya Tummala 	unsigned long now = jiffies;
1800a7d10cf3SSahitya Tummala 
1801a7d10cf3SSahitya Tummala 	sbi->last_time[type] = now;
1802a7d10cf3SSahitya Tummala 
1803a7d10cf3SSahitya Tummala 	/* DISCARD_TIME and GC_TIME are based on REQ_TIME */
1804a7d10cf3SSahitya Tummala 	if (type == REQ_TIME) {
1805a7d10cf3SSahitya Tummala 		sbi->last_time[DISCARD_TIME] = now;
1806a7d10cf3SSahitya Tummala 		sbi->last_time[GC_TIME] = now;
1807a7d10cf3SSahitya Tummala 	}
18086beceb54SJaegeuk Kim }
18096beceb54SJaegeuk Kim 
18106beceb54SJaegeuk Kim static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
18116beceb54SJaegeuk Kim {
18126bccfa19SArnd Bergmann 	unsigned long interval = sbi->interval_time[type] * HZ;
18136beceb54SJaegeuk Kim 
18146beceb54SJaegeuk Kim 	return time_after(jiffies, sbi->last_time[type] + interval);
18156beceb54SJaegeuk Kim }
18166beceb54SJaegeuk Kim 
1817a7d10cf3SSahitya Tummala static inline unsigned int f2fs_time_to_wait(struct f2fs_sb_info *sbi,
1818a7d10cf3SSahitya Tummala 						int type)
1819a7d10cf3SSahitya Tummala {
1820a7d10cf3SSahitya Tummala 	unsigned long interval = sbi->interval_time[type] * HZ;
1821a7d10cf3SSahitya Tummala 	unsigned int wait_ms = 0;
1822a7d10cf3SSahitya Tummala 	long delta;
1823a7d10cf3SSahitya Tummala 
1824a7d10cf3SSahitya Tummala 	delta = (sbi->last_time[type] + interval) - jiffies;
1825a7d10cf3SSahitya Tummala 	if (delta > 0)
1826a7d10cf3SSahitya Tummala 		wait_ms = jiffies_to_msecs(delta);
1827a7d10cf3SSahitya Tummala 
1828a7d10cf3SSahitya Tummala 	return wait_ms;
1829a7d10cf3SSahitya Tummala }
1830a7d10cf3SSahitya Tummala 
183139a53e0cSJaegeuk Kim /*
183239a53e0cSJaegeuk Kim  * Inline functions
183339a53e0cSJaegeuk Kim  */
1834416d2dbbSChao Yu static inline u32 __f2fs_crc32(struct f2fs_sb_info *sbi, u32 crc,
1835704956ecSChao Yu 			      const void *address, unsigned int length)
1836704956ecSChao Yu {
1837704956ecSChao Yu 	struct {
1838704956ecSChao Yu 		struct shash_desc shash;
1839704956ecSChao Yu 		char ctx[4];
1840704956ecSChao Yu 	} desc;
1841704956ecSChao Yu 	int err;
1842704956ecSChao Yu 
1843704956ecSChao Yu 	BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver) != sizeof(desc.ctx));
1844704956ecSChao Yu 
1845704956ecSChao Yu 	desc.shash.tfm = sbi->s_chksum_driver;
1846704956ecSChao Yu 	*(u32 *)desc.ctx = crc;
1847704956ecSChao Yu 
1848704956ecSChao Yu 	err = crypto_shash_update(&desc.shash, address, length);
1849704956ecSChao Yu 	BUG_ON(err);
1850704956ecSChao Yu 
1851704956ecSChao Yu 	return *(u32 *)desc.ctx;
1852704956ecSChao Yu }
1853704956ecSChao Yu 
1854416d2dbbSChao Yu static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
1855416d2dbbSChao Yu 			   unsigned int length)
1856416d2dbbSChao Yu {
1857416d2dbbSChao Yu 	return __f2fs_crc32(sbi, F2FS_SUPER_MAGIC, address, length);
1858416d2dbbSChao Yu }
1859416d2dbbSChao Yu 
1860416d2dbbSChao Yu static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
1861416d2dbbSChao Yu 				  void *buf, size_t buf_size)
1862416d2dbbSChao Yu {
1863416d2dbbSChao Yu 	return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
1864416d2dbbSChao Yu }
1865416d2dbbSChao Yu 
1866416d2dbbSChao Yu static inline u32 f2fs_chksum(struct f2fs_sb_info *sbi, u32 crc,
1867416d2dbbSChao Yu 			      const void *address, unsigned int length)
1868416d2dbbSChao Yu {
1869416d2dbbSChao Yu 	return __f2fs_crc32(sbi, crc, address, length);
1870416d2dbbSChao Yu }
1871416d2dbbSChao Yu 
187239a53e0cSJaegeuk Kim static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
187339a53e0cSJaegeuk Kim {
187439a53e0cSJaegeuk Kim 	return container_of(inode, struct f2fs_inode_info, vfs_inode);
187539a53e0cSJaegeuk Kim }
187639a53e0cSJaegeuk Kim 
187739a53e0cSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
187839a53e0cSJaegeuk Kim {
187939a53e0cSJaegeuk Kim 	return sb->s_fs_info;
188039a53e0cSJaegeuk Kim }
188139a53e0cSJaegeuk Kim 
18824081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
18834081363fSJaegeuk Kim {
18844081363fSJaegeuk Kim 	return F2FS_SB(inode->i_sb);
18854081363fSJaegeuk Kim }
18864081363fSJaegeuk Kim 
18874081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
18884081363fSJaegeuk Kim {
18894081363fSJaegeuk Kim 	return F2FS_I_SB(mapping->host);
18904081363fSJaegeuk Kim }
18914081363fSJaegeuk Kim 
18924081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
18934081363fSJaegeuk Kim {
18944969c06aSJaegeuk Kim 	return F2FS_M_SB(page_file_mapping(page));
18954081363fSJaegeuk Kim }
18964081363fSJaegeuk Kim 
189739a53e0cSJaegeuk Kim static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
189839a53e0cSJaegeuk Kim {
189939a53e0cSJaegeuk Kim 	return (struct f2fs_super_block *)(sbi->raw_super);
190039a53e0cSJaegeuk Kim }
190139a53e0cSJaegeuk Kim 
190239a53e0cSJaegeuk Kim static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
190339a53e0cSJaegeuk Kim {
190439a53e0cSJaegeuk Kim 	return (struct f2fs_checkpoint *)(sbi->ckpt);
190539a53e0cSJaegeuk Kim }
190639a53e0cSJaegeuk Kim 
190745590710SGu Zheng static inline struct f2fs_node *F2FS_NODE(struct page *page)
190845590710SGu Zheng {
190945590710SGu Zheng 	return (struct f2fs_node *)page_address(page);
191045590710SGu Zheng }
191145590710SGu Zheng 
191258bfaf44SJaegeuk Kim static inline struct f2fs_inode *F2FS_INODE(struct page *page)
191358bfaf44SJaegeuk Kim {
191458bfaf44SJaegeuk Kim 	return &((struct f2fs_node *)page_address(page))->i;
191558bfaf44SJaegeuk Kim }
191658bfaf44SJaegeuk Kim 
191739a53e0cSJaegeuk Kim static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
191839a53e0cSJaegeuk Kim {
191939a53e0cSJaegeuk Kim 	return (struct f2fs_nm_info *)(sbi->nm_info);
192039a53e0cSJaegeuk Kim }
192139a53e0cSJaegeuk Kim 
192239a53e0cSJaegeuk Kim static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
192339a53e0cSJaegeuk Kim {
192439a53e0cSJaegeuk Kim 	return (struct f2fs_sm_info *)(sbi->sm_info);
192539a53e0cSJaegeuk Kim }
192639a53e0cSJaegeuk Kim 
192739a53e0cSJaegeuk Kim static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
192839a53e0cSJaegeuk Kim {
192939a53e0cSJaegeuk Kim 	return (struct sit_info *)(SM_I(sbi)->sit_info);
193039a53e0cSJaegeuk Kim }
193139a53e0cSJaegeuk Kim 
193239a53e0cSJaegeuk Kim static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
193339a53e0cSJaegeuk Kim {
193439a53e0cSJaegeuk Kim 	return (struct free_segmap_info *)(SM_I(sbi)->free_info);
193539a53e0cSJaegeuk Kim }
193639a53e0cSJaegeuk Kim 
193739a53e0cSJaegeuk Kim static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
193839a53e0cSJaegeuk Kim {
193939a53e0cSJaegeuk Kim 	return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
194039a53e0cSJaegeuk Kim }
194139a53e0cSJaegeuk Kim 
19429df27d98SGu Zheng static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
19439df27d98SGu Zheng {
19449df27d98SGu Zheng 	return sbi->meta_inode->i_mapping;
19459df27d98SGu Zheng }
19469df27d98SGu Zheng 
19474ef51a8fSJaegeuk Kim static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
19484ef51a8fSJaegeuk Kim {
19494ef51a8fSJaegeuk Kim 	return sbi->node_inode->i_mapping;
19504ef51a8fSJaegeuk Kim }
19514ef51a8fSJaegeuk Kim 
1952caf0047eSChao Yu static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
195339a53e0cSJaegeuk Kim {
1954fadb2fb8SChao Yu 	return test_bit(type, &sbi->s_flag);
195539a53e0cSJaegeuk Kim }
195639a53e0cSJaegeuk Kim 
1957caf0047eSChao Yu static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
195839a53e0cSJaegeuk Kim {
1959fadb2fb8SChao Yu 	set_bit(type, &sbi->s_flag);
1960caf0047eSChao Yu }
1961caf0047eSChao Yu 
1962caf0047eSChao Yu static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
1963caf0047eSChao Yu {
1964fadb2fb8SChao Yu 	clear_bit(type, &sbi->s_flag);
196539a53e0cSJaegeuk Kim }
196639a53e0cSJaegeuk Kim 
1967d71b5564SJaegeuk Kim static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
1968d71b5564SJaegeuk Kim {
1969d71b5564SJaegeuk Kim 	return le64_to_cpu(cp->checkpoint_ver);
1970d71b5564SJaegeuk Kim }
1971d71b5564SJaegeuk Kim 
1972ea676733SJaegeuk Kim static inline unsigned long f2fs_qf_ino(struct super_block *sb, int type)
1973ea676733SJaegeuk Kim {
1974ea676733SJaegeuk Kim 	if (type < F2FS_MAX_QUOTAS)
1975ea676733SJaegeuk Kim 		return le32_to_cpu(F2FS_SB(sb)->raw_super->qf_ino[type]);
1976ea676733SJaegeuk Kim 	return 0;
1977ea676733SJaegeuk Kim }
1978ea676733SJaegeuk Kim 
1979ced2c7eaSKinglong Mee static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
1980ced2c7eaSKinglong Mee {
1981ced2c7eaSKinglong Mee 	size_t crc_offset = le32_to_cpu(cp->checksum_offset);
1982ced2c7eaSKinglong Mee 	return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
1983ced2c7eaSKinglong Mee }
1984ced2c7eaSKinglong Mee 
1985aaec2b1dSChao Yu static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
198625ca923bSJaegeuk Kim {
198725ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
1988aaec2b1dSChao Yu 
198925ca923bSJaegeuk Kim 	return ckpt_flags & f;
199025ca923bSJaegeuk Kim }
199125ca923bSJaegeuk Kim 
1992aaec2b1dSChao Yu static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
199325ca923bSJaegeuk Kim {
1994aaec2b1dSChao Yu 	return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
1995aaec2b1dSChao Yu }
1996aaec2b1dSChao Yu 
1997aaec2b1dSChao Yu static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1998aaec2b1dSChao Yu {
1999aaec2b1dSChao Yu 	unsigned int ckpt_flags;
2000aaec2b1dSChao Yu 
2001aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
200225ca923bSJaegeuk Kim 	ckpt_flags |= f;
200325ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
200425ca923bSJaegeuk Kim }
200525ca923bSJaegeuk Kim 
2006aaec2b1dSChao Yu static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
200725ca923bSJaegeuk Kim {
2008d1aa2453SChao Yu 	unsigned long flags;
2009d1aa2453SChao Yu 
2010d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
2011aaec2b1dSChao Yu 	__set_ckpt_flags(F2FS_CKPT(sbi), f);
2012d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
2013aaec2b1dSChao Yu }
2014aaec2b1dSChao Yu 
2015aaec2b1dSChao Yu static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
2016aaec2b1dSChao Yu {
2017aaec2b1dSChao Yu 	unsigned int ckpt_flags;
2018aaec2b1dSChao Yu 
2019aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
202025ca923bSJaegeuk Kim 	ckpt_flags &= (~f);
202125ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
202225ca923bSJaegeuk Kim }
202325ca923bSJaegeuk Kim 
2024aaec2b1dSChao Yu static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
2025aaec2b1dSChao Yu {
2026d1aa2453SChao Yu 	unsigned long flags;
2027d1aa2453SChao Yu 
2028d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
2029aaec2b1dSChao Yu 	__clear_ckpt_flags(F2FS_CKPT(sbi), f);
2030d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
2031aaec2b1dSChao Yu }
2032aaec2b1dSChao Yu 
203322ad0b6aSJaegeuk Kim static inline void disable_nat_bits(struct f2fs_sb_info *sbi, bool lock)
203422ad0b6aSJaegeuk Kim {
2035d1aa2453SChao Yu 	unsigned long flags;
20360921835cSChao Yu 	unsigned char *nat_bits;
2037d1aa2453SChao Yu 
2038a742fd41SJaegeuk Kim 	/*
2039a742fd41SJaegeuk Kim 	 * In order to re-enable nat_bits we need to call fsck.f2fs by
2040a742fd41SJaegeuk Kim 	 * set_sbi_flag(sbi, SBI_NEED_FSCK). But it may give huge cost,
2041a742fd41SJaegeuk Kim 	 * so let's rely on regular fsck or unclean shutdown.
2042a742fd41SJaegeuk Kim 	 */
204322ad0b6aSJaegeuk Kim 
204422ad0b6aSJaegeuk Kim 	if (lock)
2045d1aa2453SChao Yu 		spin_lock_irqsave(&sbi->cp_lock, flags);
204622ad0b6aSJaegeuk Kim 	__clear_ckpt_flags(F2FS_CKPT(sbi), CP_NAT_BITS_FLAG);
20470921835cSChao Yu 	nat_bits = NM_I(sbi)->nat_bits;
204822ad0b6aSJaegeuk Kim 	NM_I(sbi)->nat_bits = NULL;
204922ad0b6aSJaegeuk Kim 	if (lock)
2050d1aa2453SChao Yu 		spin_unlock_irqrestore(&sbi->cp_lock, flags);
20510921835cSChao Yu 
20520921835cSChao Yu 	kvfree(nat_bits);
205322ad0b6aSJaegeuk Kim }
205422ad0b6aSJaegeuk Kim 
205522ad0b6aSJaegeuk Kim static inline bool enabled_nat_bits(struct f2fs_sb_info *sbi,
205622ad0b6aSJaegeuk Kim 					struct cp_control *cpc)
205722ad0b6aSJaegeuk Kim {
205822ad0b6aSJaegeuk Kim 	bool set = is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
205922ad0b6aSJaegeuk Kim 
2060c473f1a9SChao Yu 	return (cpc) ? (cpc->reason & CP_UMOUNT) && set : set;
206122ad0b6aSJaegeuk Kim }
206222ad0b6aSJaegeuk Kim 
2063e479556bSGu Zheng static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
206439a53e0cSJaegeuk Kim {
2065b873b798SJaegeuk Kim 	down_read(&sbi->cp_rwsem);
206639a53e0cSJaegeuk Kim }
206739a53e0cSJaegeuk Kim 
2068cc15620bSJaegeuk Kim static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi)
2069cc15620bSJaegeuk Kim {
2070cc15620bSJaegeuk Kim 	return down_read_trylock(&sbi->cp_rwsem);
2071cc15620bSJaegeuk Kim }
2072cc15620bSJaegeuk Kim 
2073e479556bSGu Zheng static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
207439a53e0cSJaegeuk Kim {
2075b873b798SJaegeuk Kim 	up_read(&sbi->cp_rwsem);
207639936837SJaegeuk Kim }
207739936837SJaegeuk Kim 
2078e479556bSGu Zheng static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
207939936837SJaegeuk Kim {
2080b873b798SJaegeuk Kim 	down_write(&sbi->cp_rwsem);
208139936837SJaegeuk Kim }
208239936837SJaegeuk Kim 
2083e479556bSGu Zheng static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
208439936837SJaegeuk Kim {
2085b873b798SJaegeuk Kim 	up_write(&sbi->cp_rwsem);
208639a53e0cSJaegeuk Kim }
208739a53e0cSJaegeuk Kim 
2088119ee914SJaegeuk Kim static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
2089119ee914SJaegeuk Kim {
2090119ee914SJaegeuk Kim 	int reason = CP_SYNC;
2091119ee914SJaegeuk Kim 
2092119ee914SJaegeuk Kim 	if (test_opt(sbi, FASTBOOT))
2093119ee914SJaegeuk Kim 		reason = CP_FASTBOOT;
2094119ee914SJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
2095119ee914SJaegeuk Kim 		reason = CP_UMOUNT;
2096119ee914SJaegeuk Kim 	return reason;
2097119ee914SJaegeuk Kim }
2098119ee914SJaegeuk Kim 
2099119ee914SJaegeuk Kim static inline bool __remain_node_summaries(int reason)
2100119ee914SJaegeuk Kim {
2101c473f1a9SChao Yu 	return (reason & (CP_UMOUNT | CP_FASTBOOT));
2102119ee914SJaegeuk Kim }
2103119ee914SJaegeuk Kim 
2104119ee914SJaegeuk Kim static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
2105119ee914SJaegeuk Kim {
2106aaec2b1dSChao Yu 	return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
2107aaec2b1dSChao Yu 			is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
2108119ee914SJaegeuk Kim }
2109119ee914SJaegeuk Kim 
211039a53e0cSJaegeuk Kim /*
211139a53e0cSJaegeuk Kim  * Check whether the inode has blocks or not
211239a53e0cSJaegeuk Kim  */
211339a53e0cSJaegeuk Kim static inline int F2FS_HAS_BLOCKS(struct inode *inode)
211439a53e0cSJaegeuk Kim {
21150eb0adadSChao Yu 	block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
21160eb0adadSChao Yu 
2117000519f2SChao Yu 	return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block;
211839a53e0cSJaegeuk Kim }
211939a53e0cSJaegeuk Kim 
21204bc8e9bcSChao Yu static inline bool f2fs_has_xattr_block(unsigned int ofs)
21214bc8e9bcSChao Yu {
21224bc8e9bcSChao Yu 	return ofs == XATTR_NODE_OFFSET;
21234bc8e9bcSChao Yu }
21244bc8e9bcSChao Yu 
2125d8a9a229SJaegeuk Kim static inline bool __allow_reserved_blocks(struct f2fs_sb_info *sbi,
2126a90a0884SJaegeuk Kim 					struct inode *inode, bool cap)
21277c2e5963SJaegeuk Kim {
2128d8a9a229SJaegeuk Kim 	if (!inode)
2129d8a9a229SJaegeuk Kim 		return true;
21307c2e5963SJaegeuk Kim 	if (!test_opt(sbi, RESERVE_ROOT))
21317c2e5963SJaegeuk Kim 		return false;
2132d8a9a229SJaegeuk Kim 	if (IS_NOQUOTA(inode))
2133d8a9a229SJaegeuk Kim 		return true;
213463189b78SChao Yu 	if (uid_eq(F2FS_OPTION(sbi).s_resuid, current_fsuid()))
21357c2e5963SJaegeuk Kim 		return true;
213663189b78SChao Yu 	if (!gid_eq(F2FS_OPTION(sbi).s_resgid, GLOBAL_ROOT_GID) &&
213763189b78SChao Yu 					in_group_p(F2FS_OPTION(sbi).s_resgid))
21387c2e5963SJaegeuk Kim 		return true;
2139a90a0884SJaegeuk Kim 	if (cap && capable(CAP_SYS_RESOURCE))
2140162b27aeSJaegeuk Kim 		return true;
21417c2e5963SJaegeuk Kim 	return false;
21427c2e5963SJaegeuk Kim }
21437c2e5963SJaegeuk Kim 
21440abd675eSChao Yu static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool);
21450abd675eSChao Yu static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
214646008c6dSChao Yu 				 struct inode *inode, blkcnt_t *count)
214739a53e0cSJaegeuk Kim {
21480abd675eSChao Yu 	blkcnt_t diff = 0, release = 0;
2149daeb433eSChao Yu 	block_t avail_user_block_count;
21500abd675eSChao Yu 	int ret;
21510abd675eSChao Yu 
21520abd675eSChao Yu 	ret = dquot_reserve_block(inode, *count);
21530abd675eSChao Yu 	if (ret)
21540abd675eSChao Yu 		return ret;
2155dd11a5dfSJaegeuk Kim 
215655523519SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
2157c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_BLOCK);
21580abd675eSChao Yu 		release = *count;
21599ea2f0beSChao Yu 		goto release_quota;
216055523519SChao Yu 	}
21617fa750a1SArnd Bergmann 
2162dd11a5dfSJaegeuk Kim 	/*
2163dd11a5dfSJaegeuk Kim 	 * let's increase this in prior to actual block count change in order
2164dd11a5dfSJaegeuk Kim 	 * for f2fs_sync_file to avoid data races when deciding checkpoint.
2165dd11a5dfSJaegeuk Kim 	 */
2166dd11a5dfSJaegeuk Kim 	percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
216739a53e0cSJaegeuk Kim 
216839a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
21692555a2d5SJaegeuk Kim 	sbi->total_valid_block_count += (block_t)(*count);
217080d42145SYunlong Song 	avail_user_block_count = sbi->user_block_count -
217180d42145SYunlong Song 					sbi->current_reserved_blocks;
21727e65be49SJaegeuk Kim 
2173a90a0884SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode, true))
217463189b78SChao Yu 		avail_user_block_count -= F2FS_OPTION(sbi).root_reserved_blocks;
2175a4c3ecaaSDaniel Rosenberg 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2176a4c3ecaaSDaniel Rosenberg 		if (avail_user_block_count > sbi->unusable_block_count)
21774354994fSDaniel Rosenberg 			avail_user_block_count -= sbi->unusable_block_count;
2178a4c3ecaaSDaniel Rosenberg 		else
2179a4c3ecaaSDaniel Rosenberg 			avail_user_block_count = 0;
2180a4c3ecaaSDaniel Rosenberg 	}
2181daeb433eSChao Yu 	if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
2182daeb433eSChao Yu 		diff = sbi->total_valid_block_count - avail_user_block_count;
21837e65be49SJaegeuk Kim 		if (diff > *count)
21847e65be49SJaegeuk Kim 			diff = *count;
2185dd11a5dfSJaegeuk Kim 		*count -= diff;
21860abd675eSChao Yu 		release = diff;
21877e65be49SJaegeuk Kim 		sbi->total_valid_block_count -= diff;
218846008c6dSChao Yu 		if (!*count) {
218939a53e0cSJaegeuk Kim 			spin_unlock(&sbi->stat_lock);
21900abd675eSChao Yu 			goto enospc;
219139a53e0cSJaegeuk Kim 		}
219246008c6dSChao Yu 	}
219339a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
219441382ec4SJaegeuk Kim 
219536b877afSDaniel Rosenberg 	if (unlikely(release)) {
219636b877afSDaniel Rosenberg 		percpu_counter_sub(&sbi->alloc_valid_block_count, release);
21970abd675eSChao Yu 		dquot_release_reservation_block(inode, release);
219836b877afSDaniel Rosenberg 	}
21990abd675eSChao Yu 	f2fs_i_blocks_write(inode, *count, true, true);
22000abd675eSChao Yu 	return 0;
22010abd675eSChao Yu 
22020abd675eSChao Yu enospc:
220336b877afSDaniel Rosenberg 	percpu_counter_sub(&sbi->alloc_valid_block_count, release);
22049ea2f0beSChao Yu release_quota:
22050abd675eSChao Yu 	dquot_release_reservation_block(inode, release);
22060abd675eSChao Yu 	return -ENOSPC;
220739a53e0cSJaegeuk Kim }
220839a53e0cSJaegeuk Kim 
2209dcbb4c10SJoe Perches __printf(2, 3)
2210dcbb4c10SJoe Perches void f2fs_printk(struct f2fs_sb_info *sbi, const char *fmt, ...);
2211dcbb4c10SJoe Perches 
2212dcbb4c10SJoe Perches #define f2fs_err(sbi, fmt, ...)						\
2213dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_ERR fmt, ##__VA_ARGS__)
2214dcbb4c10SJoe Perches #define f2fs_warn(sbi, fmt, ...)					\
2215dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_WARNING fmt, ##__VA_ARGS__)
2216dcbb4c10SJoe Perches #define f2fs_notice(sbi, fmt, ...)					\
2217dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_NOTICE fmt, ##__VA_ARGS__)
2218dcbb4c10SJoe Perches #define f2fs_info(sbi, fmt, ...)					\
2219dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_INFO fmt, ##__VA_ARGS__)
2220dcbb4c10SJoe Perches #define f2fs_debug(sbi, fmt, ...)					\
2221dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_DEBUG fmt, ##__VA_ARGS__)
2222dcbb4c10SJoe Perches 
2223da19b0dcSGu Zheng static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
222439a53e0cSJaegeuk Kim 						struct inode *inode,
22250eb0adadSChao Yu 						block_t count)
222639a53e0cSJaegeuk Kim {
22270eb0adadSChao Yu 	blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK;
22280eb0adadSChao Yu 
222939a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
22309850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
223139a53e0cSJaegeuk Kim 	sbi->total_valid_block_count -= (block_t)count;
223280d42145SYunlong Song 	if (sbi->reserved_blocks &&
223380d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
223480d42145SYunlong Song 		sbi->current_reserved_blocks = min(sbi->reserved_blocks,
223580d42145SYunlong Song 					sbi->current_reserved_blocks + count);
223639a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
22375e159cd3SChao Yu 	if (unlikely(inode->i_blocks < sectors)) {
2238dcbb4c10SJoe Perches 		f2fs_warn(sbi, "Inconsistent i_blocks, ino:%lu, iblocks:%llu, sectors:%llu",
22395e159cd3SChao Yu 			  inode->i_ino,
22405e159cd3SChao Yu 			  (unsigned long long)inode->i_blocks,
22415e159cd3SChao Yu 			  (unsigned long long)sectors);
22425e159cd3SChao Yu 		set_sbi_flag(sbi, SBI_NEED_FSCK);
22435e159cd3SChao Yu 		return;
22445e159cd3SChao Yu 	}
22450abd675eSChao Yu 	f2fs_i_blocks_write(inode, count, false, true);
224639a53e0cSJaegeuk Kim }
224739a53e0cSJaegeuk Kim 
224839a53e0cSJaegeuk Kim static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
224939a53e0cSJaegeuk Kim {
225035782b23SJaegeuk Kim 	atomic_inc(&sbi->nr_pages[count_type]);
22517c4abcbeSChao Yu 
225220109873SChao Yu 	if (count_type == F2FS_DIRTY_DENTS ||
225320109873SChao Yu 			count_type == F2FS_DIRTY_NODES ||
225420109873SChao Yu 			count_type == F2FS_DIRTY_META ||
225520109873SChao Yu 			count_type == F2FS_DIRTY_QDATA ||
225620109873SChao Yu 			count_type == F2FS_DIRTY_IMETA)
2257caf0047eSChao Yu 		set_sbi_flag(sbi, SBI_IS_DIRTY);
225839a53e0cSJaegeuk Kim }
225939a53e0cSJaegeuk Kim 
2260a7ffdbe2SJaegeuk Kim static inline void inode_inc_dirty_pages(struct inode *inode)
226139a53e0cSJaegeuk Kim {
2262204706c7SJaegeuk Kim 	atomic_inc(&F2FS_I(inode)->dirty_pages);
2263c227f912SChao Yu 	inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
2264c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
22652c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
22662c8a4a28SJaegeuk Kim 		inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
226739a53e0cSJaegeuk Kim }
226839a53e0cSJaegeuk Kim 
226939a53e0cSJaegeuk Kim static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
227039a53e0cSJaegeuk Kim {
227135782b23SJaegeuk Kim 	atomic_dec(&sbi->nr_pages[count_type]);
227239a53e0cSJaegeuk Kim }
227339a53e0cSJaegeuk Kim 
2274a7ffdbe2SJaegeuk Kim static inline void inode_dec_dirty_pages(struct inode *inode)
227539a53e0cSJaegeuk Kim {
22765ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
22775ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
22781fe54f9dSJaegeuk Kim 		return;
22791fe54f9dSJaegeuk Kim 
2280204706c7SJaegeuk Kim 	atomic_dec(&F2FS_I(inode)->dirty_pages);
2281c227f912SChao Yu 	dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
2282c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
22832c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
22842c8a4a28SJaegeuk Kim 		dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
228539a53e0cSJaegeuk Kim }
228639a53e0cSJaegeuk Kim 
2287523be8a6SJaegeuk Kim static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
228839a53e0cSJaegeuk Kim {
228935782b23SJaegeuk Kim 	return atomic_read(&sbi->nr_pages[count_type]);
229039a53e0cSJaegeuk Kim }
229139a53e0cSJaegeuk Kim 
2292204706c7SJaegeuk Kim static inline int get_dirty_pages(struct inode *inode)
2293f8b2c1f9SJaegeuk Kim {
2294204706c7SJaegeuk Kim 	return atomic_read(&F2FS_I(inode)->dirty_pages);
2295f8b2c1f9SJaegeuk Kim }
2296f8b2c1f9SJaegeuk Kim 
22975ac206cfSNamjae Jeon static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
22985ac206cfSNamjae Jeon {
22993519e3f9SChao Yu 	unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
2300523be8a6SJaegeuk Kim 	unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
2301523be8a6SJaegeuk Kim 						sbi->log_blocks_per_seg;
2302523be8a6SJaegeuk Kim 
2303523be8a6SJaegeuk Kim 	return segs / sbi->segs_per_sec;
23045ac206cfSNamjae Jeon }
23055ac206cfSNamjae Jeon 
230639a53e0cSJaegeuk Kim static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
230739a53e0cSJaegeuk Kim {
23088b8343faSJaegeuk Kim 	return sbi->total_valid_block_count;
230939a53e0cSJaegeuk Kim }
231039a53e0cSJaegeuk Kim 
2311f83a2584SYunlei He static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
2312f83a2584SYunlei He {
2313f83a2584SYunlei He 	return sbi->discard_blks;
2314f83a2584SYunlei He }
2315f83a2584SYunlei He 
231639a53e0cSJaegeuk Kim static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
231739a53e0cSJaegeuk Kim {
231839a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
231939a53e0cSJaegeuk Kim 
232039a53e0cSJaegeuk Kim 	/* return NAT or SIT bitmap */
232139a53e0cSJaegeuk Kim 	if (flag == NAT_BITMAP)
232239a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
232339a53e0cSJaegeuk Kim 	else if (flag == SIT_BITMAP)
232439a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
232539a53e0cSJaegeuk Kim 
232639a53e0cSJaegeuk Kim 	return 0;
232739a53e0cSJaegeuk Kim }
232839a53e0cSJaegeuk Kim 
232955141486SWanpeng Li static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
233055141486SWanpeng Li {
233155141486SWanpeng Li 	return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
233255141486SWanpeng Li }
233355141486SWanpeng Li 
233439a53e0cSJaegeuk Kim static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
233539a53e0cSJaegeuk Kim {
233639a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
23374260c406SGustavo A. R. Silva 	void *tmp_ptr = &ckpt->sit_nat_version_bitmap;
23381dbe4152SChangman Lee 	int offset;
23391dbe4152SChangman Lee 
2340199bc3feSChao Yu 	if (is_set_ckpt_flags(sbi, CP_LARGE_NAT_BITMAP_FLAG)) {
2341199bc3feSChao Yu 		offset = (flag == SIT_BITMAP) ?
2342199bc3feSChao Yu 			le32_to_cpu(ckpt->nat_ver_bitmap_bytesize) : 0;
2343b471eb99SChao Yu 		/*
2344b471eb99SChao Yu 		 * if large_nat_bitmap feature is enabled, leave checksum
2345b471eb99SChao Yu 		 * protection for all nat/sit bitmaps.
2346b471eb99SChao Yu 		 */
23474260c406SGustavo A. R. Silva 		return tmp_ptr + offset + sizeof(__le32);
2348199bc3feSChao Yu 	}
2349199bc3feSChao Yu 
235055141486SWanpeng Li 	if (__cp_payload(sbi) > 0) {
23511dbe4152SChangman Lee 		if (flag == NAT_BITMAP)
23521dbe4152SChangman Lee 			return &ckpt->sit_nat_version_bitmap;
23531dbe4152SChangman Lee 		else
235465b85cccSJaegeuk Kim 			return (unsigned char *)ckpt + F2FS_BLKSIZE;
23551dbe4152SChangman Lee 	} else {
23561dbe4152SChangman Lee 		offset = (flag == NAT_BITMAP) ?
235725ca923bSJaegeuk Kim 			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
23584260c406SGustavo A. R. Silva 		return tmp_ptr + offset;
235939a53e0cSJaegeuk Kim 	}
23601dbe4152SChangman Lee }
236139a53e0cSJaegeuk Kim 
236239a53e0cSJaegeuk Kim static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
236339a53e0cSJaegeuk Kim {
23648508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
236539a53e0cSJaegeuk Kim 
23668508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 2)
236739a53e0cSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
236839a53e0cSJaegeuk Kim 	return start_addr;
236939a53e0cSJaegeuk Kim }
237039a53e0cSJaegeuk Kim 
23718508e44aSJaegeuk Kim static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
23728508e44aSJaegeuk Kim {
23738508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
23748508e44aSJaegeuk Kim 
23758508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 1)
23768508e44aSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
23778508e44aSJaegeuk Kim 	return start_addr;
23788508e44aSJaegeuk Kim }
23798508e44aSJaegeuk Kim 
23808508e44aSJaegeuk Kim static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
23818508e44aSJaegeuk Kim {
23828508e44aSJaegeuk Kim 	sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
23838508e44aSJaegeuk Kim }
23848508e44aSJaegeuk Kim 
238539a53e0cSJaegeuk Kim static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
238639a53e0cSJaegeuk Kim {
238739a53e0cSJaegeuk Kim 	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
238839a53e0cSJaegeuk Kim }
238939a53e0cSJaegeuk Kim 
23900abd675eSChao Yu static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
2391000519f2SChao Yu 					struct inode *inode, bool is_inode)
239239a53e0cSJaegeuk Kim {
239339a53e0cSJaegeuk Kim 	block_t	valid_block_count;
2394a4c3ecaaSDaniel Rosenberg 	unsigned int valid_node_count, user_block_count;
2395af033b2aSChao Yu 	int err;
23960abd675eSChao Yu 
2397af033b2aSChao Yu 	if (is_inode) {
2398af033b2aSChao Yu 		if (inode) {
2399af033b2aSChao Yu 			err = dquot_alloc_inode(inode);
2400af033b2aSChao Yu 			if (err)
2401af033b2aSChao Yu 				return err;
2402af033b2aSChao Yu 		}
2403af033b2aSChao Yu 	} else {
2404af033b2aSChao Yu 		err = dquot_reserve_block(inode, 1);
2405af033b2aSChao Yu 		if (err)
2406af033b2aSChao Yu 			return err;
24070abd675eSChao Yu 	}
240839a53e0cSJaegeuk Kim 
2409812c6056SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
2410c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_BLOCK);
2411812c6056SChao Yu 		goto enospc;
2412812c6056SChao Yu 	}
2413812c6056SChao Yu 
241439a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
241539a53e0cSJaegeuk Kim 
24167e65be49SJaegeuk Kim 	valid_block_count = sbi->total_valid_block_count +
24177e65be49SJaegeuk Kim 					sbi->current_reserved_blocks + 1;
24187e65be49SJaegeuk Kim 
2419a90a0884SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode, false))
242063189b78SChao Yu 		valid_block_count += F2FS_OPTION(sbi).root_reserved_blocks;
2421a4c3ecaaSDaniel Rosenberg 	user_block_count = sbi->user_block_count;
24224354994fSDaniel Rosenberg 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2423a4c3ecaaSDaniel Rosenberg 		user_block_count -= sbi->unusable_block_count;
24247e65be49SJaegeuk Kim 
2425a4c3ecaaSDaniel Rosenberg 	if (unlikely(valid_block_count > user_block_count)) {
242639a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
24270abd675eSChao Yu 		goto enospc;
242839a53e0cSJaegeuk Kim 	}
242939a53e0cSJaegeuk Kim 
2430ef86d709SGu Zheng 	valid_node_count = sbi->total_valid_node_count + 1;
2431cfb271d4SChao Yu 	if (unlikely(valid_node_count > sbi->total_node_count)) {
243239a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
24330abd675eSChao Yu 		goto enospc;
243439a53e0cSJaegeuk Kim 	}
243539a53e0cSJaegeuk Kim 
2436ef86d709SGu Zheng 	sbi->total_valid_node_count++;
2437ef86d709SGu Zheng 	sbi->total_valid_block_count++;
243839a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
243939a53e0cSJaegeuk Kim 
24400abd675eSChao Yu 	if (inode) {
24410abd675eSChao Yu 		if (is_inode)
24420abd675eSChao Yu 			f2fs_mark_inode_dirty_sync(inode, true);
24430abd675eSChao Yu 		else
24440abd675eSChao Yu 			f2fs_i_blocks_write(inode, 1, true, true);
24450abd675eSChao Yu 	}
24460abd675eSChao Yu 
244741382ec4SJaegeuk Kim 	percpu_counter_inc(&sbi->alloc_valid_block_count);
24480abd675eSChao Yu 	return 0;
24490abd675eSChao Yu 
24500abd675eSChao Yu enospc:
2451af033b2aSChao Yu 	if (is_inode) {
2452af033b2aSChao Yu 		if (inode)
2453af033b2aSChao Yu 			dquot_free_inode(inode);
2454af033b2aSChao Yu 	} else {
24550abd675eSChao Yu 		dquot_release_reservation_block(inode, 1);
2456af033b2aSChao Yu 	}
24570abd675eSChao Yu 	return -ENOSPC;
245839a53e0cSJaegeuk Kim }
245939a53e0cSJaegeuk Kim 
246039a53e0cSJaegeuk Kim static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
2461000519f2SChao Yu 					struct inode *inode, bool is_inode)
246239a53e0cSJaegeuk Kim {
246339a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
246439a53e0cSJaegeuk Kim 
24659850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_block_count);
24669850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_node_count);
246739a53e0cSJaegeuk Kim 
2468ef86d709SGu Zheng 	sbi->total_valid_node_count--;
2469ef86d709SGu Zheng 	sbi->total_valid_block_count--;
247080d42145SYunlong Song 	if (sbi->reserved_blocks &&
247180d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
247280d42145SYunlong Song 		sbi->current_reserved_blocks++;
247339a53e0cSJaegeuk Kim 
247439a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
24750abd675eSChao Yu 
2476ea6d7e72SChao Yu 	if (is_inode) {
2477af033b2aSChao Yu 		dquot_free_inode(inode);
2478ea6d7e72SChao Yu 	} else {
2479ea6d7e72SChao Yu 		if (unlikely(inode->i_blocks == 0)) {
2480097a7686SChao Yu 			f2fs_warn(sbi, "dec_valid_node_count: inconsistent i_blocks, ino:%lu, iblocks:%llu",
2481ea6d7e72SChao Yu 				  inode->i_ino,
2482ea6d7e72SChao Yu 				  (unsigned long long)inode->i_blocks);
2483ea6d7e72SChao Yu 			set_sbi_flag(sbi, SBI_NEED_FSCK);
2484ea6d7e72SChao Yu 			return;
2485ea6d7e72SChao Yu 		}
24860abd675eSChao Yu 		f2fs_i_blocks_write(inode, 1, false, true);
248739a53e0cSJaegeuk Kim 	}
2488ea6d7e72SChao Yu }
248939a53e0cSJaegeuk Kim 
249039a53e0cSJaegeuk Kim static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
249139a53e0cSJaegeuk Kim {
24928b8343faSJaegeuk Kim 	return sbi->total_valid_node_count;
249339a53e0cSJaegeuk Kim }
249439a53e0cSJaegeuk Kim 
249539a53e0cSJaegeuk Kim static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
249639a53e0cSJaegeuk Kim {
2497513c5f37SJaegeuk Kim 	percpu_counter_inc(&sbi->total_valid_inode_count);
249839a53e0cSJaegeuk Kim }
249939a53e0cSJaegeuk Kim 
25000e80220aSJaegeuk Kim static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
250139a53e0cSJaegeuk Kim {
2502513c5f37SJaegeuk Kim 	percpu_counter_dec(&sbi->total_valid_inode_count);
250339a53e0cSJaegeuk Kim }
250439a53e0cSJaegeuk Kim 
2505513c5f37SJaegeuk Kim static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
250639a53e0cSJaegeuk Kim {
2507513c5f37SJaegeuk Kim 	return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
250839a53e0cSJaegeuk Kim }
250939a53e0cSJaegeuk Kim 
2510a56c7c6fSJaegeuk Kim static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
2511a56c7c6fSJaegeuk Kim 						pgoff_t index, bool for_write)
2512a56c7c6fSJaegeuk Kim {
251382cf4f13SChao Yu 	struct page *page;
2514cac5a3d8SDongOh Shin 
25157fa750a1SArnd Bergmann 	if (IS_ENABLED(CONFIG_F2FS_FAULT_INJECTION)) {
251682cf4f13SChao Yu 		if (!for_write)
251782cf4f13SChao Yu 			page = find_get_page_flags(mapping, index,
251882cf4f13SChao Yu 							FGP_LOCK | FGP_ACCESSED);
251982cf4f13SChao Yu 		else
252082cf4f13SChao Yu 			page = find_lock_page(mapping, index);
2521c41f3cc3SJaegeuk Kim 		if (page)
2522c41f3cc3SJaegeuk Kim 			return page;
2523c41f3cc3SJaegeuk Kim 
252455523519SChao Yu 		if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) {
2525c45d6002SChao Yu 			f2fs_show_injection_info(F2FS_M_SB(mapping),
2526c45d6002SChao Yu 							FAULT_PAGE_ALLOC);
2527c41f3cc3SJaegeuk Kim 			return NULL;
252855523519SChao Yu 		}
25297fa750a1SArnd Bergmann 	}
25307fa750a1SArnd Bergmann 
2531a56c7c6fSJaegeuk Kim 	if (!for_write)
2532a56c7c6fSJaegeuk Kim 		return grab_cache_page(mapping, index);
2533a56c7c6fSJaegeuk Kim 	return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
2534a56c7c6fSJaegeuk Kim }
2535a56c7c6fSJaegeuk Kim 
253601eccef7SChao Yu static inline struct page *f2fs_pagecache_get_page(
253701eccef7SChao Yu 				struct address_space *mapping, pgoff_t index,
253801eccef7SChao Yu 				int fgp_flags, gfp_t gfp_mask)
253901eccef7SChao Yu {
254001eccef7SChao Yu 	if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_GET)) {
2541c45d6002SChao Yu 		f2fs_show_injection_info(F2FS_M_SB(mapping), FAULT_PAGE_GET);
254201eccef7SChao Yu 		return NULL;
254301eccef7SChao Yu 	}
25447fa750a1SArnd Bergmann 
254501eccef7SChao Yu 	return pagecache_get_page(mapping, index, fgp_flags, gfp_mask);
254601eccef7SChao Yu }
254701eccef7SChao Yu 
25486e2c64adSJaegeuk Kim static inline void f2fs_copy_page(struct page *src, struct page *dst)
25496e2c64adSJaegeuk Kim {
25506e2c64adSJaegeuk Kim 	char *src_kaddr = kmap(src);
25516e2c64adSJaegeuk Kim 	char *dst_kaddr = kmap(dst);
25526e2c64adSJaegeuk Kim 
25536e2c64adSJaegeuk Kim 	memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
25546e2c64adSJaegeuk Kim 	kunmap(dst);
25556e2c64adSJaegeuk Kim 	kunmap(src);
25566e2c64adSJaegeuk Kim }
25576e2c64adSJaegeuk Kim 
255839a53e0cSJaegeuk Kim static inline void f2fs_put_page(struct page *page, int unlock)
255939a53e0cSJaegeuk Kim {
2560031fa8ccSJaegeuk Kim 	if (!page)
256139a53e0cSJaegeuk Kim 		return;
256239a53e0cSJaegeuk Kim 
256339a53e0cSJaegeuk Kim 	if (unlock) {
25649850cf4aSJaegeuk Kim 		f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
256539a53e0cSJaegeuk Kim 		unlock_page(page);
256639a53e0cSJaegeuk Kim 	}
256709cbfeafSKirill A. Shutemov 	put_page(page);
256839a53e0cSJaegeuk Kim }
256939a53e0cSJaegeuk Kim 
257039a53e0cSJaegeuk Kim static inline void f2fs_put_dnode(struct dnode_of_data *dn)
257139a53e0cSJaegeuk Kim {
257239a53e0cSJaegeuk Kim 	if (dn->node_page)
257339a53e0cSJaegeuk Kim 		f2fs_put_page(dn->node_page, 1);
257439a53e0cSJaegeuk Kim 	if (dn->inode_page && dn->node_page != dn->inode_page)
257539a53e0cSJaegeuk Kim 		f2fs_put_page(dn->inode_page, 0);
257639a53e0cSJaegeuk Kim 	dn->node_page = NULL;
257739a53e0cSJaegeuk Kim 	dn->inode_page = NULL;
257839a53e0cSJaegeuk Kim }
257939a53e0cSJaegeuk Kim 
258039a53e0cSJaegeuk Kim static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
2581e8512d2eSGu Zheng 					size_t size)
258239a53e0cSJaegeuk Kim {
2583e8512d2eSGu Zheng 	return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
258439a53e0cSJaegeuk Kim }
258539a53e0cSJaegeuk Kim 
25867bd59381SGu Zheng static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
25877bd59381SGu Zheng 						gfp_t flags)
25887bd59381SGu Zheng {
25897bd59381SGu Zheng 	void *entry;
25907bd59381SGu Zheng 
259180c54505SJaegeuk Kim 	entry = kmem_cache_alloc(cachep, flags);
259280c54505SJaegeuk Kim 	if (!entry)
259380c54505SJaegeuk Kim 		entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
25947bd59381SGu Zheng 	return entry;
25957bd59381SGu Zheng }
25967bd59381SGu Zheng 
2597493720a4SChao Yu static inline bool is_inflight_io(struct f2fs_sb_info *sbi, int type)
25985f9abab4SJaegeuk Kim {
25995f9abab4SJaegeuk Kim 	if (get_pages(sbi, F2FS_RD_DATA) || get_pages(sbi, F2FS_RD_NODE) ||
26005f9abab4SJaegeuk Kim 		get_pages(sbi, F2FS_RD_META) || get_pages(sbi, F2FS_WB_DATA) ||
2601fef4129eSChao Yu 		get_pages(sbi, F2FS_WB_CP_DATA) ||
2602fef4129eSChao Yu 		get_pages(sbi, F2FS_DIO_READ) ||
260311ac8ef8SJaegeuk Kim 		get_pages(sbi, F2FS_DIO_WRITE))
2604493720a4SChao Yu 		return true;
260511ac8ef8SJaegeuk Kim 
260656659ce8SSahitya Tummala 	if (type != DISCARD_TIME && SM_I(sbi) && SM_I(sbi)->dcc_info &&
260711ac8ef8SJaegeuk Kim 			atomic_read(&SM_I(sbi)->dcc_info->queued_discard))
2608493720a4SChao Yu 		return true;
260911ac8ef8SJaegeuk Kim 
261011ac8ef8SJaegeuk Kim 	if (SM_I(sbi) && SM_I(sbi)->fcc_info &&
261172691af6SJaegeuk Kim 			atomic_read(&SM_I(sbi)->fcc_info->queued_flush))
2612493720a4SChao Yu 		return true;
2613493720a4SChao Yu 	return false;
2614493720a4SChao Yu }
2615493720a4SChao Yu 
2616493720a4SChao Yu static inline bool is_idle(struct f2fs_sb_info *sbi, int type)
2617493720a4SChao Yu {
2618493720a4SChao Yu 	if (sbi->gc_mode == GC_URGENT_HIGH)
2619493720a4SChao Yu 		return true;
2620493720a4SChao Yu 
2621493720a4SChao Yu 	if (is_inflight_io(sbi, type))
26225f9abab4SJaegeuk Kim 		return false;
262311ac8ef8SJaegeuk Kim 
26240e5e8111SDaeho Jeong 	if (sbi->gc_mode == GC_URGENT_LOW &&
26250e5e8111SDaeho Jeong 			(type == DISCARD_TIME || type == GC_TIME))
26260e5e8111SDaeho Jeong 		return true;
26270e5e8111SDaeho Jeong 
26285f9abab4SJaegeuk Kim 	return f2fs_time_over(sbi, type);
26295f9abab4SJaegeuk Kim }
26305f9abab4SJaegeuk Kim 
26319be32d72SJaegeuk Kim static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
26329be32d72SJaegeuk Kim 				unsigned long index, void *item)
26339be32d72SJaegeuk Kim {
26349be32d72SJaegeuk Kim 	while (radix_tree_insert(root, index, item))
26359be32d72SJaegeuk Kim 		cond_resched();
26369be32d72SJaegeuk Kim }
26379be32d72SJaegeuk Kim 
263839a53e0cSJaegeuk Kim #define RAW_IS_INODE(p)	((p)->footer.nid == (p)->footer.ino)
263939a53e0cSJaegeuk Kim 
264039a53e0cSJaegeuk Kim static inline bool IS_INODE(struct page *page)
264139a53e0cSJaegeuk Kim {
264245590710SGu Zheng 	struct f2fs_node *p = F2FS_NODE(page);
2643cac5a3d8SDongOh Shin 
264439a53e0cSJaegeuk Kim 	return RAW_IS_INODE(p);
264539a53e0cSJaegeuk Kim }
264639a53e0cSJaegeuk Kim 
26477a2af766SChao Yu static inline int offset_in_addr(struct f2fs_inode *i)
26487a2af766SChao Yu {
26497a2af766SChao Yu 	return (i->i_inline & F2FS_EXTRA_ATTR) ?
26507a2af766SChao Yu 			(le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0;
26517a2af766SChao Yu }
26527a2af766SChao Yu 
265339a53e0cSJaegeuk Kim static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
265439a53e0cSJaegeuk Kim {
265539a53e0cSJaegeuk Kim 	return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
265639a53e0cSJaegeuk Kim }
265739a53e0cSJaegeuk Kim 
26587a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode);
2659a2ced1ceSChao Yu static inline block_t data_blkaddr(struct inode *inode,
26607a2af766SChao Yu 			struct page *node_page, unsigned int offset)
266139a53e0cSJaegeuk Kim {
266239a53e0cSJaegeuk Kim 	struct f2fs_node *raw_node;
266339a53e0cSJaegeuk Kim 	__le32 *addr_array;
26647a2af766SChao Yu 	int base = 0;
26657a2af766SChao Yu 	bool is_inode = IS_INODE(node_page);
2666cac5a3d8SDongOh Shin 
266745590710SGu Zheng 	raw_node = F2FS_NODE(node_page);
26687a2af766SChao Yu 
2669979f492fSLiFan 	if (is_inode) {
2670979f492fSLiFan 		if (!inode)
26717a88ddb5SChao Yu 			/* from GC path only */
26727a2af766SChao Yu 			base = offset_in_addr(&raw_node->i);
2673979f492fSLiFan 		else if (f2fs_has_extra_attr(inode))
26747a2af766SChao Yu 			base = get_extra_isize(inode);
26757a2af766SChao Yu 	}
26767a2af766SChao Yu 
267739a53e0cSJaegeuk Kim 	addr_array = blkaddr_in_node(raw_node);
26787a2af766SChao Yu 	return le32_to_cpu(addr_array[base + offset]);
267939a53e0cSJaegeuk Kim }
268039a53e0cSJaegeuk Kim 
2681a2ced1ceSChao Yu static inline block_t f2fs_data_blkaddr(struct dnode_of_data *dn)
2682a2ced1ceSChao Yu {
2683a2ced1ceSChao Yu 	return data_blkaddr(dn->inode, dn->node_page, dn->ofs_in_node);
2684a2ced1ceSChao Yu }
2685a2ced1ceSChao Yu 
268639a53e0cSJaegeuk Kim static inline int f2fs_test_bit(unsigned int nr, char *addr)
268739a53e0cSJaegeuk Kim {
268839a53e0cSJaegeuk Kim 	int mask;
268939a53e0cSJaegeuk Kim 
269039a53e0cSJaegeuk Kim 	addr += (nr >> 3);
269139a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
269239a53e0cSJaegeuk Kim 	return mask & *addr;
269339a53e0cSJaegeuk Kim }
269439a53e0cSJaegeuk Kim 
2695a66cdd98SJaegeuk Kim static inline void f2fs_set_bit(unsigned int nr, char *addr)
2696a66cdd98SJaegeuk Kim {
2697a66cdd98SJaegeuk Kim 	int mask;
2698a66cdd98SJaegeuk Kim 
2699a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2700a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2701a66cdd98SJaegeuk Kim 	*addr |= mask;
2702a66cdd98SJaegeuk Kim }
2703a66cdd98SJaegeuk Kim 
2704a66cdd98SJaegeuk Kim static inline void f2fs_clear_bit(unsigned int nr, char *addr)
2705a66cdd98SJaegeuk Kim {
2706a66cdd98SJaegeuk Kim 	int mask;
2707a66cdd98SJaegeuk Kim 
2708a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2709a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2710a66cdd98SJaegeuk Kim 	*addr &= ~mask;
2711a66cdd98SJaegeuk Kim }
2712a66cdd98SJaegeuk Kim 
271352aca074SGu Zheng static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
271439a53e0cSJaegeuk Kim {
271539a53e0cSJaegeuk Kim 	int mask;
271639a53e0cSJaegeuk Kim 	int ret;
271739a53e0cSJaegeuk Kim 
271839a53e0cSJaegeuk Kim 	addr += (nr >> 3);
271939a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
272039a53e0cSJaegeuk Kim 	ret = mask & *addr;
272139a53e0cSJaegeuk Kim 	*addr |= mask;
272239a53e0cSJaegeuk Kim 	return ret;
272339a53e0cSJaegeuk Kim }
272439a53e0cSJaegeuk Kim 
272552aca074SGu Zheng static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
272639a53e0cSJaegeuk Kim {
272739a53e0cSJaegeuk Kim 	int mask;
272839a53e0cSJaegeuk Kim 	int ret;
272939a53e0cSJaegeuk Kim 
273039a53e0cSJaegeuk Kim 	addr += (nr >> 3);
273139a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
273239a53e0cSJaegeuk Kim 	ret = mask & *addr;
273339a53e0cSJaegeuk Kim 	*addr &= ~mask;
273439a53e0cSJaegeuk Kim 	return ret;
273539a53e0cSJaegeuk Kim }
273639a53e0cSJaegeuk Kim 
2737c6ac4c0eSGu Zheng static inline void f2fs_change_bit(unsigned int nr, char *addr)
2738c6ac4c0eSGu Zheng {
2739c6ac4c0eSGu Zheng 	int mask;
2740c6ac4c0eSGu Zheng 
2741c6ac4c0eSGu Zheng 	addr += (nr >> 3);
2742c6ac4c0eSGu Zheng 	mask = 1 << (7 - (nr & 0x07));
2743c6ac4c0eSGu Zheng 	*addr ^= mask;
2744c6ac4c0eSGu Zheng }
2745c6ac4c0eSGu Zheng 
274659c84408SChao Yu /*
274736098557SEric Biggers  * On-disk inode flags (f2fs_inode::i_flags)
274859c84408SChao Yu  */
27494c8ff709SChao Yu #define F2FS_COMPR_FL			0x00000004 /* Compress file */
275059c84408SChao Yu #define F2FS_SYNC_FL			0x00000008 /* Synchronous updates */
275159c84408SChao Yu #define F2FS_IMMUTABLE_FL		0x00000010 /* Immutable file */
275259c84408SChao Yu #define F2FS_APPEND_FL			0x00000020 /* writes to file may only append */
275359c84408SChao Yu #define F2FS_NODUMP_FL			0x00000040 /* do not dump file */
275459c84408SChao Yu #define F2FS_NOATIME_FL			0x00000080 /* do not update atime */
27554c8ff709SChao Yu #define F2FS_NOCOMP_FL			0x00000400 /* Don't compress */
275659c84408SChao Yu #define F2FS_INDEX_FL			0x00001000 /* hash-indexed directory */
275759c84408SChao Yu #define F2FS_DIRSYNC_FL			0x00010000 /* dirsync behaviour (directories only) */
275859c84408SChao Yu #define F2FS_PROJINHERIT_FL		0x20000000 /* Create with parents projid */
27592c2eb7a3SDaniel Rosenberg #define F2FS_CASEFOLD_FL		0x40000000 /* Casefolded file */
276059c84408SChao Yu 
276159c84408SChao Yu /* Flags that should be inherited by new inodes from their parent. */
276236098557SEric Biggers #define F2FS_FL_INHERITED (F2FS_SYNC_FL | F2FS_NODUMP_FL | F2FS_NOATIME_FL | \
27632c2eb7a3SDaniel Rosenberg 			   F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL | \
27644c8ff709SChao Yu 			   F2FS_CASEFOLD_FL | F2FS_COMPR_FL | F2FS_NOCOMP_FL)
276559c84408SChao Yu 
276659c84408SChao Yu /* Flags that are appropriate for regular files (all but dir-specific ones). */
27672c2eb7a3SDaniel Rosenberg #define F2FS_REG_FLMASK		(~(F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL | \
27682c2eb7a3SDaniel Rosenberg 				F2FS_CASEFOLD_FL))
276959c84408SChao Yu 
277059c84408SChao Yu /* Flags that are appropriate for non-directories/regular files. */
277159c84408SChao Yu #define F2FS_OTHER_FLMASK	(F2FS_NODUMP_FL | F2FS_NOATIME_FL)
27725c57132eSChao Yu 
27735c57132eSChao Yu static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
27745c57132eSChao Yu {
27755c57132eSChao Yu 	if (S_ISDIR(mode))
27765c57132eSChao Yu 		return flags;
27775c57132eSChao Yu 	else if (S_ISREG(mode))
27785c57132eSChao Yu 		return flags & F2FS_REG_FLMASK;
27795c57132eSChao Yu 	else
27805c57132eSChao Yu 		return flags & F2FS_OTHER_FLMASK;
27815c57132eSChao Yu }
27825c57132eSChao Yu 
2783205b9822SJaegeuk Kim static inline void __mark_inode_dirty_flag(struct inode *inode,
2784205b9822SJaegeuk Kim 						int flag, bool set)
278539a53e0cSJaegeuk Kim {
2786205b9822SJaegeuk Kim 	switch (flag) {
2787205b9822SJaegeuk Kim 	case FI_INLINE_XATTR:
2788205b9822SJaegeuk Kim 	case FI_INLINE_DATA:
2789205b9822SJaegeuk Kim 	case FI_INLINE_DENTRY:
27909ac1e2d8SDaeho Jeong 	case FI_NEW_INODE:
2791205b9822SJaegeuk Kim 		if (set)
2792205b9822SJaegeuk Kim 			return;
2793df561f66SGustavo A. R. Silva 		fallthrough;
2794205b9822SJaegeuk Kim 	case FI_DATA_EXIST:
2795205b9822SJaegeuk Kim 	case FI_INLINE_DOTS:
27961ad71a27SJaegeuk Kim 	case FI_PIN_FILE:
2797c6140415SJaegeuk Kim 	case FI_COMPRESS_RELEASED:
27987c45729aSJaegeuk Kim 		f2fs_mark_inode_dirty_sync(inode, true);
2799205b9822SJaegeuk Kim 	}
280039a53e0cSJaegeuk Kim }
280139a53e0cSJaegeuk Kim 
280291942321SJaegeuk Kim static inline void set_inode_flag(struct inode *inode, int flag)
280339a53e0cSJaegeuk Kim {
280458f7e00fSYufen Yu 	set_bit(flag, F2FS_I(inode)->flags);
2805205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, true);
280639a53e0cSJaegeuk Kim }
280739a53e0cSJaegeuk Kim 
280891942321SJaegeuk Kim static inline int is_inode_flag_set(struct inode *inode, int flag)
280939a53e0cSJaegeuk Kim {
28107653b9d8SChao Yu 	return test_bit(flag, F2FS_I(inode)->flags);
281139a53e0cSJaegeuk Kim }
281239a53e0cSJaegeuk Kim 
281391942321SJaegeuk Kim static inline void clear_inode_flag(struct inode *inode, int flag)
281439a53e0cSJaegeuk Kim {
281558f7e00fSYufen Yu 	clear_bit(flag, F2FS_I(inode)->flags);
2816205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, false);
281739a53e0cSJaegeuk Kim }
281839a53e0cSJaegeuk Kim 
281995ae251fSEric Biggers static inline bool f2fs_verity_in_progress(struct inode *inode)
282095ae251fSEric Biggers {
282195ae251fSEric Biggers 	return IS_ENABLED(CONFIG_FS_VERITY) &&
282295ae251fSEric Biggers 	       is_inode_flag_set(inode, FI_VERITY_IN_PROGRESS);
282395ae251fSEric Biggers }
282495ae251fSEric Biggers 
282591942321SJaegeuk Kim static inline void set_acl_inode(struct inode *inode, umode_t mode)
2826444c580fSJaegeuk Kim {
282791942321SJaegeuk Kim 	F2FS_I(inode)->i_acl_mode = mode;
282891942321SJaegeuk Kim 	set_inode_flag(inode, FI_ACL_MODE);
28297c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, false);
283039a53e0cSJaegeuk Kim }
283139a53e0cSJaegeuk Kim 
2832a1961246SJaegeuk Kim static inline void f2fs_i_links_write(struct inode *inode, bool inc)
2833a1961246SJaegeuk Kim {
2834a1961246SJaegeuk Kim 	if (inc)
2835a1961246SJaegeuk Kim 		inc_nlink(inode);
2836a1961246SJaegeuk Kim 	else
2837a1961246SJaegeuk Kim 		drop_nlink(inode);
28387c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2839a1961246SJaegeuk Kim }
2840a1961246SJaegeuk Kim 
28418edd03c8SJaegeuk Kim static inline void f2fs_i_blocks_write(struct inode *inode,
28420abd675eSChao Yu 					block_t diff, bool add, bool claim)
28438edd03c8SJaegeuk Kim {
284426de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
284526de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
284626de9b11SJaegeuk Kim 
28470abd675eSChao Yu 	/* add = 1, claim = 1 should be dquot_reserve_block in pair */
28480abd675eSChao Yu 	if (add) {
28490abd675eSChao Yu 		if (claim)
28500abd675eSChao Yu 			dquot_claim_block(inode, diff);
28510abd675eSChao Yu 		else
28520abd675eSChao Yu 			dquot_alloc_block_nofail(inode, diff);
28530abd675eSChao Yu 	} else {
28540abd675eSChao Yu 		dquot_free_block(inode, diff);
28550abd675eSChao Yu 	}
28560abd675eSChao Yu 
28577c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
285826de9b11SJaegeuk Kim 	if (clean || recover)
285926de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
28608edd03c8SJaegeuk Kim }
28618edd03c8SJaegeuk Kim 
2862fc9581c8SJaegeuk Kim static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
2863fc9581c8SJaegeuk Kim {
286426de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
286526de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
286626de9b11SJaegeuk Kim 
2867fc9581c8SJaegeuk Kim 	if (i_size_read(inode) == i_size)
2868fc9581c8SJaegeuk Kim 		return;
2869fc9581c8SJaegeuk Kim 
2870fc9581c8SJaegeuk Kim 	i_size_write(inode, i_size);
28717c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
287226de9b11SJaegeuk Kim 	if (clean || recover)
287326de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
287426de9b11SJaegeuk Kim }
287526de9b11SJaegeuk Kim 
2876205b9822SJaegeuk Kim static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
2877205b9822SJaegeuk Kim {
2878205b9822SJaegeuk Kim 	F2FS_I(inode)->i_current_depth = depth;
28797c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2880205b9822SJaegeuk Kim }
2881205b9822SJaegeuk Kim 
28821ad71a27SJaegeuk Kim static inline void f2fs_i_gc_failures_write(struct inode *inode,
28831ad71a27SJaegeuk Kim 					unsigned int count)
28841ad71a27SJaegeuk Kim {
28852ef79ecbSChao Yu 	F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN] = count;
28861ad71a27SJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
28871ad71a27SJaegeuk Kim }
28881ad71a27SJaegeuk Kim 
2889205b9822SJaegeuk Kim static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
2890205b9822SJaegeuk Kim {
2891205b9822SJaegeuk Kim 	F2FS_I(inode)->i_xattr_nid = xnid;
28927c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2893205b9822SJaegeuk Kim }
2894205b9822SJaegeuk Kim 
2895205b9822SJaegeuk Kim static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
2896205b9822SJaegeuk Kim {
2897205b9822SJaegeuk Kim 	F2FS_I(inode)->i_pino = pino;
28987c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2899205b9822SJaegeuk Kim }
2900205b9822SJaegeuk Kim 
290191942321SJaegeuk Kim static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
2902444c580fSJaegeuk Kim {
2903205b9822SJaegeuk Kim 	struct f2fs_inode_info *fi = F2FS_I(inode);
2904205b9822SJaegeuk Kim 
2905444c580fSJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_XATTR)
29067653b9d8SChao Yu 		set_bit(FI_INLINE_XATTR, fi->flags);
29071001b347SHuajun Li 	if (ri->i_inline & F2FS_INLINE_DATA)
29087653b9d8SChao Yu 		set_bit(FI_INLINE_DATA, fi->flags);
290934d67debSChao Yu 	if (ri->i_inline & F2FS_INLINE_DENTRY)
29107653b9d8SChao Yu 		set_bit(FI_INLINE_DENTRY, fi->flags);
2911b3d208f9SJaegeuk Kim 	if (ri->i_inline & F2FS_DATA_EXIST)
29127653b9d8SChao Yu 		set_bit(FI_DATA_EXIST, fi->flags);
2913510022a8SJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_DOTS)
29147653b9d8SChao Yu 		set_bit(FI_INLINE_DOTS, fi->flags);
29157a2af766SChao Yu 	if (ri->i_inline & F2FS_EXTRA_ATTR)
29167653b9d8SChao Yu 		set_bit(FI_EXTRA_ATTR, fi->flags);
29171ad71a27SJaegeuk Kim 	if (ri->i_inline & F2FS_PIN_FILE)
29187653b9d8SChao Yu 		set_bit(FI_PIN_FILE, fi->flags);
2919c6140415SJaegeuk Kim 	if (ri->i_inline & F2FS_COMPRESS_RELEASED)
2920c6140415SJaegeuk Kim 		set_bit(FI_COMPRESS_RELEASED, fi->flags);
2921444c580fSJaegeuk Kim }
2922444c580fSJaegeuk Kim 
292391942321SJaegeuk Kim static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
2924444c580fSJaegeuk Kim {
2925444c580fSJaegeuk Kim 	ri->i_inline = 0;
2926444c580fSJaegeuk Kim 
292791942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_XATTR))
2928444c580fSJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_XATTR;
292991942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DATA))
29301001b347SHuajun Li 		ri->i_inline |= F2FS_INLINE_DATA;
293191942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
293234d67debSChao Yu 		ri->i_inline |= F2FS_INLINE_DENTRY;
293391942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_DATA_EXIST))
2934b3d208f9SJaegeuk Kim 		ri->i_inline |= F2FS_DATA_EXIST;
293591942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DOTS))
2936510022a8SJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_DOTS;
29377a2af766SChao Yu 	if (is_inode_flag_set(inode, FI_EXTRA_ATTR))
29387a2af766SChao Yu 		ri->i_inline |= F2FS_EXTRA_ATTR;
29391ad71a27SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_PIN_FILE))
29401ad71a27SJaegeuk Kim 		ri->i_inline |= F2FS_PIN_FILE;
2941c6140415SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
2942c6140415SJaegeuk Kim 		ri->i_inline |= F2FS_COMPRESS_RELEASED;
29437a2af766SChao Yu }
29447a2af766SChao Yu 
29457a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode)
29467a2af766SChao Yu {
29477a2af766SChao Yu 	return is_inode_flag_set(inode, FI_EXTRA_ATTR);
2948444c580fSJaegeuk Kim }
2949444c580fSJaegeuk Kim 
2950987c7c31SChao Yu static inline int f2fs_has_inline_xattr(struct inode *inode)
2951987c7c31SChao Yu {
295291942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_XATTR);
2953987c7c31SChao Yu }
2954987c7c31SChao Yu 
29554c8ff709SChao Yu static inline int f2fs_compressed_file(struct inode *inode)
29564c8ff709SChao Yu {
29574c8ff709SChao Yu 	return S_ISREG(inode->i_mode) &&
29584c8ff709SChao Yu 		is_inode_flag_set(inode, FI_COMPRESSED_FILE);
29594c8ff709SChao Yu }
29604c8ff709SChao Yu 
2961602a16d5SDaeho Jeong static inline bool f2fs_need_compress_data(struct inode *inode)
2962602a16d5SDaeho Jeong {
2963602a16d5SDaeho Jeong 	int compress_mode = F2FS_OPTION(F2FS_I_SB(inode)).compress_mode;
2964602a16d5SDaeho Jeong 
2965602a16d5SDaeho Jeong 	if (!f2fs_compressed_file(inode))
2966602a16d5SDaeho Jeong 		return false;
2967602a16d5SDaeho Jeong 
2968602a16d5SDaeho Jeong 	if (compress_mode == COMPR_MODE_FS)
2969602a16d5SDaeho Jeong 		return true;
2970602a16d5SDaeho Jeong 	else if (compress_mode == COMPR_MODE_USER &&
2971602a16d5SDaeho Jeong 			is_inode_flag_set(inode, FI_ENABLE_COMPRESS))
2972602a16d5SDaeho Jeong 		return true;
2973602a16d5SDaeho Jeong 
2974602a16d5SDaeho Jeong 	return false;
2975602a16d5SDaeho Jeong }
2976602a16d5SDaeho Jeong 
297781ca7350SChao Yu static inline unsigned int addrs_per_inode(struct inode *inode)
2978de93653fSJaegeuk Kim {
2979d02a6e61SChao Yu 	unsigned int addrs = CUR_ADDRS_PER_INODE(inode) -
2980d02a6e61SChao Yu 				get_inline_xattr_addrs(inode);
29814c8ff709SChao Yu 
29824c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
29834c8ff709SChao Yu 		return addrs;
29844c8ff709SChao Yu 	return ALIGN_DOWN(addrs, F2FS_I(inode)->i_cluster_size);
2985d02a6e61SChao Yu }
2986d02a6e61SChao Yu 
2987d02a6e61SChao Yu static inline unsigned int addrs_per_block(struct inode *inode)
2988d02a6e61SChao Yu {
29894c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
29904c8ff709SChao Yu 		return DEF_ADDRS_PER_BLOCK;
29914c8ff709SChao Yu 	return ALIGN_DOWN(DEF_ADDRS_PER_BLOCK, F2FS_I(inode)->i_cluster_size);
2992de93653fSJaegeuk Kim }
2993de93653fSJaegeuk Kim 
29946afc662eSChao Yu static inline void *inline_xattr_addr(struct inode *inode, struct page *page)
299565985d93SJaegeuk Kim {
2996695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
2997cac5a3d8SDongOh Shin 
299865985d93SJaegeuk Kim 	return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
2999b323fd28SChao Yu 					get_inline_xattr_addrs(inode)]);
300065985d93SJaegeuk Kim }
300165985d93SJaegeuk Kim 
300265985d93SJaegeuk Kim static inline int inline_xattr_size(struct inode *inode)
300365985d93SJaegeuk Kim {
3004622927f3SChao Yu 	if (f2fs_has_inline_xattr(inode))
30056afc662eSChao Yu 		return get_inline_xattr_addrs(inode) * sizeof(__le32);
3006622927f3SChao Yu 	return 0;
300765985d93SJaegeuk Kim }
300865985d93SJaegeuk Kim 
30090dbdc2aeSJaegeuk Kim static inline int f2fs_has_inline_data(struct inode *inode)
30100dbdc2aeSJaegeuk Kim {
301191942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DATA);
30120dbdc2aeSJaegeuk Kim }
30130dbdc2aeSJaegeuk Kim 
3014b3d208f9SJaegeuk Kim static inline int f2fs_exist_data(struct inode *inode)
3015b3d208f9SJaegeuk Kim {
301691942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DATA_EXIST);
3017b3d208f9SJaegeuk Kim }
3018b3d208f9SJaegeuk Kim 
3019510022a8SJaegeuk Kim static inline int f2fs_has_inline_dots(struct inode *inode)
3020510022a8SJaegeuk Kim {
302191942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DOTS);
3022510022a8SJaegeuk Kim }
3023510022a8SJaegeuk Kim 
30244c8ff709SChao Yu static inline int f2fs_is_mmap_file(struct inode *inode)
30254c8ff709SChao Yu {
30264c8ff709SChao Yu 	return is_inode_flag_set(inode, FI_MMAP_FILE);
30274c8ff709SChao Yu }
30284c8ff709SChao Yu 
30291ad71a27SJaegeuk Kim static inline bool f2fs_is_pinned_file(struct inode *inode)
30301ad71a27SJaegeuk Kim {
30311ad71a27SJaegeuk Kim 	return is_inode_flag_set(inode, FI_PIN_FILE);
30321ad71a27SJaegeuk Kim }
30331ad71a27SJaegeuk Kim 
303488b88a66SJaegeuk Kim static inline bool f2fs_is_atomic_file(struct inode *inode)
303588b88a66SJaegeuk Kim {
303691942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_ATOMIC_FILE);
303788b88a66SJaegeuk Kim }
303888b88a66SJaegeuk Kim 
30395fe45743SChao Yu static inline bool f2fs_is_commit_atomic_write(struct inode *inode)
30405fe45743SChao Yu {
30415fe45743SChao Yu 	return is_inode_flag_set(inode, FI_ATOMIC_COMMIT);
30425fe45743SChao Yu }
30435fe45743SChao Yu 
304402a1335fSJaegeuk Kim static inline bool f2fs_is_volatile_file(struct inode *inode)
304502a1335fSJaegeuk Kim {
304691942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_VOLATILE_FILE);
304702a1335fSJaegeuk Kim }
304802a1335fSJaegeuk Kim 
30493c6c2bebSJaegeuk Kim static inline bool f2fs_is_first_block_written(struct inode *inode)
30503c6c2bebSJaegeuk Kim {
305191942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
30523c6c2bebSJaegeuk Kim }
30533c6c2bebSJaegeuk Kim 
30541e84371fSJaegeuk Kim static inline bool f2fs_is_drop_cache(struct inode *inode)
30551e84371fSJaegeuk Kim {
305691942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DROP_CACHE);
30571e84371fSJaegeuk Kim }
30581e84371fSJaegeuk Kim 
3059f2470371SChao Yu static inline void *inline_data_addr(struct inode *inode, struct page *page)
30601001b347SHuajun Li {
3061695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
30627a2af766SChao Yu 	int extra_size = get_extra_isize(inode);
3063cac5a3d8SDongOh Shin 
30647a2af766SChao Yu 	return (void *)&(ri->i_addr[extra_size + DEF_INLINE_RESERVED_SIZE]);
30651001b347SHuajun Li }
30661001b347SHuajun Li 
306734d67debSChao Yu static inline int f2fs_has_inline_dentry(struct inode *inode)
306834d67debSChao Yu {
306991942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DENTRY);
307034d67debSChao Yu }
307134d67debSChao Yu 
3072b5492af7SJaegeuk Kim static inline int is_file(struct inode *inode, int type)
3073b5492af7SJaegeuk Kim {
3074b5492af7SJaegeuk Kim 	return F2FS_I(inode)->i_advise & type;
3075b5492af7SJaegeuk Kim }
3076b5492af7SJaegeuk Kim 
3077b5492af7SJaegeuk Kim static inline void set_file(struct inode *inode, int type)
3078b5492af7SJaegeuk Kim {
3079b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise |= type;
30807c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3081b5492af7SJaegeuk Kim }
3082b5492af7SJaegeuk Kim 
3083b5492af7SJaegeuk Kim static inline void clear_file(struct inode *inode, int type)
3084b5492af7SJaegeuk Kim {
3085b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise &= ~type;
30867c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3087b5492af7SJaegeuk Kim }
3088b5492af7SJaegeuk Kim 
3089fe1897eaSChao Yu static inline bool f2fs_is_time_consistent(struct inode *inode)
3090fe1897eaSChao Yu {
3091fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time, &inode->i_atime))
3092fe1897eaSChao Yu 		return false;
3093fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 1, &inode->i_ctime))
3094fe1897eaSChao Yu 		return false;
3095fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 2, &inode->i_mtime))
3096fe1897eaSChao Yu 		return false;
3097fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 3,
3098fe1897eaSChao Yu 						&F2FS_I(inode)->i_crtime))
3099fe1897eaSChao Yu 		return false;
3100fe1897eaSChao Yu 	return true;
3101fe1897eaSChao Yu }
3102fe1897eaSChao Yu 
310326787236SJaegeuk Kim static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
310426787236SJaegeuk Kim {
3105a0d00fadSChao Yu 	bool ret;
3106a0d00fadSChao Yu 
310726787236SJaegeuk Kim 	if (dsync) {
310826787236SJaegeuk Kim 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
310926787236SJaegeuk Kim 
311026787236SJaegeuk Kim 		spin_lock(&sbi->inode_lock[DIRTY_META]);
311126787236SJaegeuk Kim 		ret = list_empty(&F2FS_I(inode)->gdirty_list);
311226787236SJaegeuk Kim 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
311326787236SJaegeuk Kim 		return ret;
311426787236SJaegeuk Kim 	}
311526787236SJaegeuk Kim 	if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
311626787236SJaegeuk Kim 			file_keep_isize(inode) ||
3117235831d7SJunling Zheng 			i_size_read(inode) & ~PAGE_MASK)
311826787236SJaegeuk Kim 		return false;
3119a0d00fadSChao Yu 
3120fe1897eaSChao Yu 	if (!f2fs_is_time_consistent(inode))
3121214c2461SJaegeuk Kim 		return false;
3122214c2461SJaegeuk Kim 
3123c10c9820SChao Yu 	spin_lock(&F2FS_I(inode)->i_size_lock);
3124a0d00fadSChao Yu 	ret = F2FS_I(inode)->last_disk_size == i_size_read(inode);
3125c10c9820SChao Yu 	spin_unlock(&F2FS_I(inode)->i_size_lock);
3126a0d00fadSChao Yu 
3127a0d00fadSChao Yu 	return ret;
3128267378d4SChao Yu }
3129267378d4SChao Yu 
3130deeedd71SChao Yu static inline bool f2fs_readonly(struct super_block *sb)
313177888c1eSJaegeuk Kim {
3132deeedd71SChao Yu 	return sb_rdonly(sb);
313377888c1eSJaegeuk Kim }
313477888c1eSJaegeuk Kim 
3135744602cfSJaegeuk Kim static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
3136744602cfSJaegeuk Kim {
3137aaec2b1dSChao Yu 	return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
3138744602cfSJaegeuk Kim }
3139744602cfSJaegeuk Kim 
314043c780baSEric Biggers static inline bool is_dot_dotdot(const u8 *name, size_t len)
3141eaa693f4SJaegeuk Kim {
314243c780baSEric Biggers 	if (len == 1 && name[0] == '.')
3143eaa693f4SJaegeuk Kim 		return true;
3144eaa693f4SJaegeuk Kim 
314543c780baSEric Biggers 	if (len == 2 && name[0] == '.' && name[1] == '.')
3146eaa693f4SJaegeuk Kim 		return true;
3147eaa693f4SJaegeuk Kim 
3148eaa693f4SJaegeuk Kim 	return false;
3149eaa693f4SJaegeuk Kim }
3150eaa693f4SJaegeuk Kim 
31513e72f721SJaegeuk Kim static inline bool f2fs_may_extent_tree(struct inode *inode)
31523e72f721SJaegeuk Kim {
3153e4589fa5SSahitya Tummala 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3154e4589fa5SSahitya Tummala 
3155e4589fa5SSahitya Tummala 	if (!test_opt(sbi, EXTENT_CACHE) ||
31564c8ff709SChao Yu 			is_inode_flag_set(inode, FI_NO_EXTENT) ||
31574c8ff709SChao Yu 			is_inode_flag_set(inode, FI_COMPRESSED_FILE))
31583e72f721SJaegeuk Kim 		return false;
31593e72f721SJaegeuk Kim 
3160e4589fa5SSahitya Tummala 	/*
3161e4589fa5SSahitya Tummala 	 * for recovered files during mount do not create extents
3162e4589fa5SSahitya Tummala 	 * if shrinker is not registered.
3163e4589fa5SSahitya Tummala 	 */
3164e4589fa5SSahitya Tummala 	if (list_empty(&sbi->s_list))
3165e4589fa5SSahitya Tummala 		return false;
3166e4589fa5SSahitya Tummala 
3167886f56f9SAl Viro 	return S_ISREG(inode->i_mode);
31683e72f721SJaegeuk Kim }
31693e72f721SJaegeuk Kim 
31701ecc0c5cSChao Yu static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
31711ecc0c5cSChao Yu 					size_t size, gfp_t flags)
31720414b004SJaegeuk Kim {
317355523519SChao Yu 	if (time_to_inject(sbi, FAULT_KMALLOC)) {
3174c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_KMALLOC);
31752c63feadSJaegeuk Kim 		return NULL;
317655523519SChao Yu 	}
31777fa750a1SArnd Bergmann 
31780b6d4ca0SEric Biggers 	return kmalloc(size, flags);
31790414b004SJaegeuk Kim }
31800414b004SJaegeuk Kim 
3181acbf054dSChao Yu static inline void *f2fs_kzalloc(struct f2fs_sb_info *sbi,
3182acbf054dSChao Yu 					size_t size, gfp_t flags)
3183acbf054dSChao Yu {
3184acbf054dSChao Yu 	return f2fs_kmalloc(sbi, size, flags | __GFP_ZERO);
3185acbf054dSChao Yu }
3186acbf054dSChao Yu 
3187628b3d14SChao Yu static inline void *f2fs_kvmalloc(struct f2fs_sb_info *sbi,
3188628b3d14SChao Yu 					size_t size, gfp_t flags)
3189628b3d14SChao Yu {
3190628b3d14SChao Yu 	if (time_to_inject(sbi, FAULT_KVMALLOC)) {
3191c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_KVMALLOC);
3192628b3d14SChao Yu 		return NULL;
3193628b3d14SChao Yu 	}
31947fa750a1SArnd Bergmann 
3195628b3d14SChao Yu 	return kvmalloc(size, flags);
3196628b3d14SChao Yu }
3197628b3d14SChao Yu 
3198628b3d14SChao Yu static inline void *f2fs_kvzalloc(struct f2fs_sb_info *sbi,
3199628b3d14SChao Yu 					size_t size, gfp_t flags)
3200628b3d14SChao Yu {
3201628b3d14SChao Yu 	return f2fs_kvmalloc(sbi, size, flags | __GFP_ZERO);
3202628b3d14SChao Yu }
3203628b3d14SChao Yu 
32047a2af766SChao Yu static inline int get_extra_isize(struct inode *inode)
3205f2470371SChao Yu {
32067a2af766SChao Yu 	return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
3207f2470371SChao Yu }
3208f2470371SChao Yu 
32096afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode)
32106afc662eSChao Yu {
32116afc662eSChao Yu 	return F2FS_I(inode)->i_inline_xattr_size;
32126afc662eSChao Yu }
32136afc662eSChao Yu 
32144d57b86dSChao Yu #define f2fs_get_inode_mode(i) \
321591942321SJaegeuk Kim 	((is_inode_flag_set(i, FI_ACL_MODE)) ? \
3216a6dda0e6SChristoph Hellwig 	 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
3217a6dda0e6SChristoph Hellwig 
32187a2af766SChao Yu #define F2FS_TOTAL_EXTRA_ATTR_SIZE			\
32197a2af766SChao Yu 	(offsetof(struct f2fs_inode, i_extra_end) -	\
32207a2af766SChao Yu 	offsetof(struct f2fs_inode, i_extra_isize))	\
32217a2af766SChao Yu 
32225c57132eSChao Yu #define F2FS_OLD_ATTRIBUTE_SIZE	(offsetof(struct f2fs_inode, i_addr))
32235c57132eSChao Yu #define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field)		\
32242f84babfSSheng Yong 		((offsetof(typeof(*(f2fs_inode)), field) +	\
32255c57132eSChao Yu 		sizeof((f2fs_inode)->field))			\
32262f84babfSSheng Yong 		<= (F2FS_OLD_ATTRIBUTE_SIZE + (extra_isize)))	\
32275c57132eSChao Yu 
32282bc4bea3SDaeho Jeong #define DEFAULT_IOSTAT_PERIOD_MS	3000
32292bc4bea3SDaeho Jeong #define MIN_IOSTAT_PERIOD_MS		100
32302bc4bea3SDaeho Jeong /* maximum period of iostat tracing is 1 day */
32312bc4bea3SDaeho Jeong #define MAX_IOSTAT_PERIOD_MS		8640000
32322bc4bea3SDaeho Jeong 
3233b0af6d49SChao Yu static inline void f2fs_reset_iostat(struct f2fs_sb_info *sbi)
3234b0af6d49SChao Yu {
3235b0af6d49SChao Yu 	int i;
3236b0af6d49SChao Yu 
3237b0af6d49SChao Yu 	spin_lock(&sbi->iostat_lock);
32382bc4bea3SDaeho Jeong 	for (i = 0; i < NR_IO_TYPE; i++) {
32398b83ac81SChao Yu 		sbi->rw_iostat[i] = 0;
32408b83ac81SChao Yu 		sbi->prev_rw_iostat[i] = 0;
32412bc4bea3SDaeho Jeong 	}
3242b0af6d49SChao Yu 	spin_unlock(&sbi->iostat_lock);
3243b0af6d49SChao Yu }
3244b0af6d49SChao Yu 
32452bc4bea3SDaeho Jeong extern void f2fs_record_iostat(struct f2fs_sb_info *sbi);
32462bc4bea3SDaeho Jeong 
3247b0af6d49SChao Yu static inline void f2fs_update_iostat(struct f2fs_sb_info *sbi,
3248b0af6d49SChao Yu 			enum iostat_type type, unsigned long long io_bytes)
3249b0af6d49SChao Yu {
3250b0af6d49SChao Yu 	if (!sbi->iostat_enable)
3251b0af6d49SChao Yu 		return;
3252b0af6d49SChao Yu 	spin_lock(&sbi->iostat_lock);
32538b83ac81SChao Yu 	sbi->rw_iostat[type] += io_bytes;
3254b0af6d49SChao Yu 
3255b0af6d49SChao Yu 	if (type == APP_WRITE_IO || type == APP_DIRECT_IO)
32568b83ac81SChao Yu 		sbi->rw_iostat[APP_BUFFERED_IO] =
32578b83ac81SChao Yu 			sbi->rw_iostat[APP_WRITE_IO] -
32588b83ac81SChao Yu 			sbi->rw_iostat[APP_DIRECT_IO];
32598b83ac81SChao Yu 
32608b83ac81SChao Yu 	if (type == APP_READ_IO || type == APP_DIRECT_READ_IO)
32618b83ac81SChao Yu 		sbi->rw_iostat[APP_BUFFERED_READ_IO] =
32628b83ac81SChao Yu 			sbi->rw_iostat[APP_READ_IO] -
32638b83ac81SChao Yu 			sbi->rw_iostat[APP_DIRECT_READ_IO];
3264b0af6d49SChao Yu 	spin_unlock(&sbi->iostat_lock);
32652bc4bea3SDaeho Jeong 
32662bc4bea3SDaeho Jeong 	f2fs_record_iostat(sbi);
3267b0af6d49SChao Yu }
3268b0af6d49SChao Yu 
32692c70c5e3SChao Yu #define __is_large_section(sbi)		((sbi)->segs_per_sec > 1)
32702c70c5e3SChao Yu 
32716dc3a126SChao Yu #define __is_meta_io(fio) (PAGE_TYPE_OF_BIO((fio)->type) == META)
3272c9b60788SChao Yu 
3273e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
3274e1da7872SChao Yu 					block_t blkaddr, int type);
3275e1da7872SChao Yu static inline void verify_blkaddr(struct f2fs_sb_info *sbi,
3276e1da7872SChao Yu 					block_t blkaddr, int type)
3277e1da7872SChao Yu {
3278e1da7872SChao Yu 	if (!f2fs_is_valid_blkaddr(sbi, blkaddr, type)) {
3279dcbb4c10SJoe Perches 		f2fs_err(sbi, "invalid blkaddr: %u, type: %d, run fsck to fix.",
3280e1da7872SChao Yu 			 blkaddr, type);
3281e1da7872SChao Yu 		f2fs_bug_on(sbi, 1);
3282e1da7872SChao Yu 	}
3283e1da7872SChao Yu }
3284e1da7872SChao Yu 
3285e1da7872SChao Yu static inline bool __is_valid_data_blkaddr(block_t blkaddr)
32867b525dd0SChao Yu {
32874c8ff709SChao Yu 	if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR ||
32884c8ff709SChao Yu 			blkaddr == COMPRESS_ADDR)
32897b525dd0SChao Yu 		return false;
32907b525dd0SChao Yu 	return true;
32917b525dd0SChao Yu }
32927b525dd0SChao Yu 
329339a53e0cSJaegeuk Kim /*
329439a53e0cSJaegeuk Kim  * file.c
329539a53e0cSJaegeuk Kim  */
3296cac5a3d8SDongOh Shin int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
32974d57b86dSChao Yu void f2fs_truncate_data_blocks(struct dnode_of_data *dn);
32983265d3dbSChao Yu int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock);
3299c42d28ceSChao Yu int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock);
3300cac5a3d8SDongOh Shin int f2fs_truncate(struct inode *inode);
3301549c7297SChristian Brauner int f2fs_getattr(struct user_namespace *mnt_userns, const struct path *path,
3302549c7297SChristian Brauner 		 struct kstat *stat, u32 request_mask, unsigned int flags);
3303549c7297SChristian Brauner int f2fs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
3304549c7297SChristian Brauner 		 struct iattr *attr);
33054d57b86dSChao Yu int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
33064d57b86dSChao Yu void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count);
3307c4020b2dSChao Yu int f2fs_precache_extents(struct inode *inode);
33089b1bb01cSMiklos Szeredi int f2fs_fileattr_get(struct dentry *dentry, struct fileattr *fa);
33099b1bb01cSMiklos Szeredi int f2fs_fileattr_set(struct user_namespace *mnt_userns,
33109b1bb01cSMiklos Szeredi 		      struct dentry *dentry, struct fileattr *fa);
3311cac5a3d8SDongOh Shin long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
3312cac5a3d8SDongOh Shin long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
331378130819SChao Yu int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid);
33141ad71a27SJaegeuk Kim int f2fs_pin_file_control(struct inode *inode, bool inc);
331539a53e0cSJaegeuk Kim 
331639a53e0cSJaegeuk Kim /*
331739a53e0cSJaegeuk Kim  * inode.c
331839a53e0cSJaegeuk Kim  */
3319cac5a3d8SDongOh Shin void f2fs_set_inode_flags(struct inode *inode);
3320704956ecSChao Yu bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page);
3321704956ecSChao Yu void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page);
3322cac5a3d8SDongOh Shin struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
3323cac5a3d8SDongOh Shin struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
33244d57b86dSChao Yu int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
33254d57b86dSChao Yu void f2fs_update_inode(struct inode *inode, struct page *node_page);
33264d57b86dSChao Yu void f2fs_update_inode_page(struct inode *inode);
3327cac5a3d8SDongOh Shin int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
3328cac5a3d8SDongOh Shin void f2fs_evict_inode(struct inode *inode);
33294d57b86dSChao Yu void f2fs_handle_failed_inode(struct inode *inode);
333039a53e0cSJaegeuk Kim 
333139a53e0cSJaegeuk Kim /*
333239a53e0cSJaegeuk Kim  * namei.c
333339a53e0cSJaegeuk Kim  */
33344d57b86dSChao Yu int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
3335b6a06cbbSChao Yu 							bool hot, bool set);
333639a53e0cSJaegeuk Kim struct dentry *f2fs_get_parent(struct dentry *child);
333739a53e0cSJaegeuk Kim 
333839a53e0cSJaegeuk Kim /*
333939a53e0cSJaegeuk Kim  * dir.c
334039a53e0cSJaegeuk Kim  */
33414d57b86dSChao Yu unsigned char f2fs_get_de_type(struct f2fs_dir_entry *de);
334243c780baSEric Biggers int f2fs_init_casefolded_name(const struct inode *dir,
334343c780baSEric Biggers 			      struct f2fs_filename *fname);
334443c780baSEric Biggers int f2fs_setup_filename(struct inode *dir, const struct qstr *iname,
334543c780baSEric Biggers 			int lookup, struct f2fs_filename *fname);
334643c780baSEric Biggers int f2fs_prepare_lookup(struct inode *dir, struct dentry *dentry,
334743c780baSEric Biggers 			struct f2fs_filename *fname);
334843c780baSEric Biggers void f2fs_free_filename(struct f2fs_filename *fname);
334943c780baSEric Biggers struct f2fs_dir_entry *f2fs_find_target_dentry(const struct f2fs_dentry_ptr *d,
335043c780baSEric Biggers 			const struct f2fs_filename *fname, int *max_slots);
3351cac5a3d8SDongOh Shin int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
3352cac5a3d8SDongOh Shin 			unsigned int start_pos, struct fscrypt_str *fstr);
33534d57b86dSChao Yu void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
3354cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
33554d57b86dSChao Yu struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
335643c780baSEric Biggers 			const struct f2fs_filename *fname, struct page *dpage);
33574d57b86dSChao Yu void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
3358cac5a3d8SDongOh Shin 			unsigned int current_depth);
33594d57b86dSChao Yu int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots);
3360cac5a3d8SDongOh Shin void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
3361cac5a3d8SDongOh Shin struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
336243c780baSEric Biggers 					 const struct f2fs_filename *fname,
336343c780baSEric Biggers 					 struct page **res_page);
3364cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
3365cac5a3d8SDongOh Shin 			const struct qstr *child, struct page **res_page);
3366cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
3367cac5a3d8SDongOh Shin ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
3368cac5a3d8SDongOh Shin 			struct page **page);
3369cac5a3d8SDongOh Shin void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
3370cac5a3d8SDongOh Shin 			struct page *page, struct inode *inode);
3371b06af2afSJaegeuk Kim bool f2fs_has_enough_room(struct inode *dir, struct page *ipage,
337243c780baSEric Biggers 			  const struct f2fs_filename *fname);
3373cac5a3d8SDongOh Shin void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
337443c780baSEric Biggers 			const struct fscrypt_str *name, f2fs_hash_t name_hash,
3375cac5a3d8SDongOh Shin 			unsigned int bit_pos);
337643c780baSEric Biggers int f2fs_add_regular_entry(struct inode *dir, const struct f2fs_filename *fname,
3377cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
337843c780baSEric Biggers int f2fs_add_dentry(struct inode *dir, const struct f2fs_filename *fname,
3379cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
33804d57b86dSChao Yu int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
3381cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
3382cac5a3d8SDongOh Shin void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
3383cac5a3d8SDongOh Shin 			struct inode *dir, struct inode *inode);
3384cac5a3d8SDongOh Shin int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
3385cac5a3d8SDongOh Shin bool f2fs_empty_dir(struct inode *dir);
338639a53e0cSJaegeuk Kim 
3387b7f7a5e0SAl Viro static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
3388b7f7a5e0SAl Viro {
3389bfc2b7e8SEric Biggers 	if (fscrypt_is_nokey_name(dentry))
3390bfc2b7e8SEric Biggers 		return -ENOKEY;
33914d57b86dSChao Yu 	return f2fs_do_add_link(d_inode(dentry->d_parent), &dentry->d_name,
3392510022a8SJaegeuk Kim 				inode, inode->i_ino, inode->i_mode);
3393b7f7a5e0SAl Viro }
3394b7f7a5e0SAl Viro 
339539a53e0cSJaegeuk Kim /*
339639a53e0cSJaegeuk Kim  * super.c
339739a53e0cSJaegeuk Kim  */
3398cac5a3d8SDongOh Shin int f2fs_inode_dirtied(struct inode *inode, bool sync);
3399cac5a3d8SDongOh Shin void f2fs_inode_synced(struct inode *inode);
3400ea676733SJaegeuk Kim int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly);
3401af033b2aSChao Yu int f2fs_quota_sync(struct super_block *sb, int type);
34026d1451bfSChengguang Xu loff_t max_file_blocks(struct inode *inode);
34034b2414d0SChao Yu void f2fs_quota_off_umount(struct super_block *sb);
3404cac5a3d8SDongOh Shin int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
3405cac5a3d8SDongOh Shin int f2fs_sync_fs(struct super_block *sb, int sync);
34064d57b86dSChao Yu int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi);
340739a53e0cSJaegeuk Kim 
340839a53e0cSJaegeuk Kim /*
340939a53e0cSJaegeuk Kim  * hash.c
341039a53e0cSJaegeuk Kim  */
341143c780baSEric Biggers void f2fs_hash_filename(const struct inode *dir, struct f2fs_filename *fname);
341239a53e0cSJaegeuk Kim 
341339a53e0cSJaegeuk Kim /*
341439a53e0cSJaegeuk Kim  * node.c
341539a53e0cSJaegeuk Kim  */
341639a53e0cSJaegeuk Kim struct node_info;
341739a53e0cSJaegeuk Kim 
34184d57b86dSChao Yu int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid);
34194d57b86dSChao Yu bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type);
342050fa53ecSChao Yu bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct page *page);
342150fa53ecSChao Yu void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi);
342250fa53ecSChao Yu void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct page *page);
342350fa53ecSChao Yu void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi);
34244d57b86dSChao Yu int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
34254d57b86dSChao Yu bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
34264d57b86dSChao Yu bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
34277735730dSChao Yu int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
34284d57b86dSChao Yu 						struct node_info *ni);
34294d57b86dSChao Yu pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
34304d57b86dSChao Yu int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
34314d57b86dSChao Yu int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from);
34324d57b86dSChao Yu int f2fs_truncate_xattr_node(struct inode *inode);
343350fa53ecSChao Yu int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
343450fa53ecSChao Yu 					unsigned int seq_id);
34354d57b86dSChao Yu int f2fs_remove_inode_page(struct inode *inode);
34364d57b86dSChao Yu struct page *f2fs_new_inode_page(struct inode *inode);
34374d57b86dSChao Yu struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs);
34384d57b86dSChao Yu void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
34394d57b86dSChao Yu struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
34404d57b86dSChao Yu struct page *f2fs_get_node_page_ra(struct page *parent, int start);
344148018b4cSChao Yu int f2fs_move_node_page(struct page *node_page, int gc_type);
344268e79bafSJia Yang void f2fs_flush_inline_data(struct f2fs_sb_info *sbi);
34434d57b86dSChao Yu int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
344450fa53ecSChao Yu 			struct writeback_control *wbc, bool atomic,
344550fa53ecSChao Yu 			unsigned int *seq_id);
34464d57b86dSChao Yu int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
34474d57b86dSChao Yu 			struct writeback_control *wbc,
3448b0af6d49SChao Yu 			bool do_balance, enum iostat_type io_type);
3449e2374015SChao Yu int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
34504d57b86dSChao Yu bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
34514d57b86dSChao Yu void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
34524d57b86dSChao Yu void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
34534d57b86dSChao Yu int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
34549627a7b3SChao Yu int f2fs_recover_inline_xattr(struct inode *inode, struct page *page);
34554d57b86dSChao Yu int f2fs_recover_xattr_data(struct inode *inode, struct page *page);
34564d57b86dSChao Yu int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
34577735730dSChao Yu int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
3458cac5a3d8SDongOh Shin 			unsigned int segno, struct f2fs_summary_block *sum);
3459edc55aafSJaegeuk Kim int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
34604d57b86dSChao Yu int f2fs_build_node_manager(struct f2fs_sb_info *sbi);
34614d57b86dSChao Yu void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi);
34624d57b86dSChao Yu int __init f2fs_create_node_manager_caches(void);
34634d57b86dSChao Yu void f2fs_destroy_node_manager_caches(void);
346439a53e0cSJaegeuk Kim 
346539a53e0cSJaegeuk Kim /*
346639a53e0cSJaegeuk Kim  * segment.c
346739a53e0cSJaegeuk Kim  */
34684d57b86dSChao Yu bool f2fs_need_SSR(struct f2fs_sb_info *sbi);
34694d57b86dSChao Yu void f2fs_register_inmem_page(struct inode *inode, struct page *page);
34704d57b86dSChao Yu void f2fs_drop_inmem_pages_all(struct f2fs_sb_info *sbi, bool gc_failure);
34714d57b86dSChao Yu void f2fs_drop_inmem_pages(struct inode *inode);
34724d57b86dSChao Yu void f2fs_drop_inmem_page(struct inode *inode, struct page *page);
34734d57b86dSChao Yu int f2fs_commit_inmem_pages(struct inode *inode);
3474cac5a3d8SDongOh Shin void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
34757bcd0cfaSChao Yu void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi, bool from_bg);
347639d787beSChao Yu int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
34774d57b86dSChao Yu int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
34781228b482SChao Yu int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
34794d57b86dSChao Yu void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
34804d57b86dSChao Yu void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
34814d57b86dSChao Yu bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
34824d57b86dSChao Yu void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
34834d57b86dSChao Yu void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi);
348403f2c02dSJaegeuk Kim bool f2fs_issue_discard_timeout(struct f2fs_sb_info *sbi);
34854d57b86dSChao Yu void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
34864d57b86dSChao Yu 					struct cp_control *cpc);
34874354994fSDaniel Rosenberg void f2fs_dirty_to_prefree(struct f2fs_sb_info *sbi);
34884d3aed70SDaniel Rosenberg block_t f2fs_get_unusable_blocks(struct f2fs_sb_info *sbi);
34894d3aed70SDaniel Rosenberg int f2fs_disable_cp_again(struct f2fs_sb_info *sbi, block_t unusable);
34904d57b86dSChao Yu void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi);
34914d57b86dSChao Yu int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
349261461fc9SChao Yu bool f2fs_segment_has_free_slot(struct f2fs_sb_info *sbi, int segno);
3493093749e2SChao Yu void f2fs_init_inmem_curseg(struct f2fs_sb_info *sbi);
3494093749e2SChao Yu void f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi);
3495093749e2SChao Yu void f2fs_restore_inmem_curseg(struct f2fs_sb_info *sbi);
3496093749e2SChao Yu void f2fs_get_new_segment(struct f2fs_sb_info *sbi,
3497093749e2SChao Yu 			unsigned int *newseg, bool new_sec, int dir);
34980ef81833SChao Yu void f2fs_allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type,
349904f0b2eaSQiuyang Sun 					unsigned int start, unsigned int end);
3500509f1010SChao Yu void f2fs_allocate_new_section(struct f2fs_sb_info *sbi, int type, bool force);
3501901d745fSChao Yu void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi);
3502cac5a3d8SDongOh Shin int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
35034d57b86dSChao Yu bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
35044d57b86dSChao Yu 					struct cp_control *cpc);
35054d57b86dSChao Yu struct page *f2fs_get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
35064d57b86dSChao Yu void f2fs_update_meta_page(struct f2fs_sb_info *sbi, void *src,
35074d57b86dSChao Yu 					block_t blk_addr);
35084d57b86dSChao Yu void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
3509b0af6d49SChao Yu 						enum iostat_type io_type);
35104d57b86dSChao Yu void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio);
35114d57b86dSChao Yu void f2fs_outplace_write_data(struct dnode_of_data *dn,
35124d57b86dSChao Yu 			struct f2fs_io_info *fio);
35134d57b86dSChao Yu int f2fs_inplace_write_data(struct f2fs_io_info *fio);
35144d57b86dSChao Yu void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
3515cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t new_blkaddr,
3516c5d02785SChao Yu 			bool recover_curseg, bool recover_newaddr,
3517c5d02785SChao Yu 			bool from_gc);
3518cac5a3d8SDongOh Shin void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
3519cac5a3d8SDongOh Shin 			block_t old_addr, block_t new_addr,
3520cac5a3d8SDongOh Shin 			unsigned char version, bool recover_curseg,
3521cac5a3d8SDongOh Shin 			bool recover_newaddr);
35224d57b86dSChao Yu void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
3523cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t *new_blkaddr,
3524fb830fc5SChao Yu 			struct f2fs_summary *sum, int type,
3525f608c38cSChao Yu 			struct f2fs_io_info *fio);
3526cac5a3d8SDongOh Shin void f2fs_wait_on_page_writeback(struct page *page,
3527bae0ee7aSChao Yu 			enum page_type type, bool ordered, bool locked);
35280ded69f6SJaegeuk Kim void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr);
35291e78e8bdSSahitya Tummala void f2fs_wait_on_block_writeback_range(struct inode *inode, block_t blkaddr,
35301e78e8bdSSahitya Tummala 								block_t len);
35314d57b86dSChao Yu void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
35324d57b86dSChao Yu void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
35334d57b86dSChao Yu int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
3534cac5a3d8SDongOh Shin 			unsigned int val, int alloc);
35354d57b86dSChao Yu void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
3536c426d991SShin'ichiro Kawasaki int f2fs_fix_curseg_write_pointer(struct f2fs_sb_info *sbi);
3537d508c94eSShin'ichiro Kawasaki int f2fs_check_write_pointer(struct f2fs_sb_info *sbi);
35384d57b86dSChao Yu int f2fs_build_segment_manager(struct f2fs_sb_info *sbi);
35394d57b86dSChao Yu void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi);
35404d57b86dSChao Yu int __init f2fs_create_segment_manager_caches(void);
35414d57b86dSChao Yu void f2fs_destroy_segment_manager_caches(void);
35424d57b86dSChao Yu int f2fs_rw_hint_to_seg_type(enum rw_hint hint);
35434d57b86dSChao Yu enum rw_hint f2fs_io_type_to_rw_hint(struct f2fs_sb_info *sbi,
35444d57b86dSChao Yu 			enum page_type type, enum temp_type temp);
3545de881df9SAravind Ramesh unsigned int f2fs_usable_segs_in_sec(struct f2fs_sb_info *sbi,
3546de881df9SAravind Ramesh 			unsigned int segno);
3547de881df9SAravind Ramesh unsigned int f2fs_usable_blks_in_seg(struct f2fs_sb_info *sbi,
3548de881df9SAravind Ramesh 			unsigned int segno);
354939a53e0cSJaegeuk Kim 
355039a53e0cSJaegeuk Kim /*
355139a53e0cSJaegeuk Kim  * checkpoint.c
355239a53e0cSJaegeuk Kim  */
3553cac5a3d8SDongOh Shin void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io);
35544d57b86dSChao Yu struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
35554d57b86dSChao Yu struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
355686f33603SJaegeuk Kim struct page *f2fs_get_meta_page_retry(struct f2fs_sb_info *sbi, pgoff_t index);
35574d57b86dSChao Yu struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
3558e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
35594d57b86dSChao Yu 					block_t blkaddr, int type);
35604d57b86dSChao Yu int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
3561cac5a3d8SDongOh Shin 			int type, bool sync);
35624d57b86dSChao Yu void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index);
35634d57b86dSChao Yu long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
3564b0af6d49SChao Yu 			long nr_to_write, enum iostat_type io_type);
35654d57b86dSChao Yu void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
35664d57b86dSChao Yu void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
35674d57b86dSChao Yu void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all);
35684d57b86dSChao Yu bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
35694d57b86dSChao Yu void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
357039d787beSChao Yu 					unsigned int devidx, int type);
35714d57b86dSChao Yu bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
357239d787beSChao Yu 					unsigned int devidx, int type);
3573cac5a3d8SDongOh Shin int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
35744d57b86dSChao Yu int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi);
35754d57b86dSChao Yu void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi);
35764d57b86dSChao Yu void f2fs_add_orphan_inode(struct inode *inode);
35774d57b86dSChao Yu void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
35784d57b86dSChao Yu int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi);
35794d57b86dSChao Yu int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi);
35804d57b86dSChao Yu void f2fs_update_dirty_page(struct inode *inode, struct page *page);
35814d57b86dSChao Yu void f2fs_remove_dirty_inode(struct inode *inode);
35824d57b86dSChao Yu int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
3583bf22c3ccSSahitya Tummala void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type);
35843a0a9cbcSChao Yu u64 f2fs_get_sectors_written(struct f2fs_sb_info *sbi);
35854d57b86dSChao Yu int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
35864d57b86dSChao Yu void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi);
35874d57b86dSChao Yu int __init f2fs_create_checkpoint_caches(void);
35884d57b86dSChao Yu void f2fs_destroy_checkpoint_caches(void);
3589261eeb9cSDaeho Jeong int f2fs_issue_checkpoint(struct f2fs_sb_info *sbi);
3590261eeb9cSDaeho Jeong int f2fs_start_ckpt_thread(struct f2fs_sb_info *sbi);
3591261eeb9cSDaeho Jeong void f2fs_stop_ckpt_thread(struct f2fs_sb_info *sbi);
3592261eeb9cSDaeho Jeong void f2fs_init_ckpt_req_control(struct f2fs_sb_info *sbi);
359339a53e0cSJaegeuk Kim 
359439a53e0cSJaegeuk Kim /*
359539a53e0cSJaegeuk Kim  * data.c
359639a53e0cSJaegeuk Kim  */
3597f543805fSChao Yu int __init f2fs_init_bioset(void);
3598f543805fSChao Yu void f2fs_destroy_bioset(void);
35990b20fcecSChao Yu int f2fs_init_bio_entry_cache(void);
36000b20fcecSChao Yu void f2fs_destroy_bio_entry_cache(void);
36014c8ff709SChao Yu void f2fs_submit_bio(struct f2fs_sb_info *sbi,
36024c8ff709SChao Yu 				struct bio *bio, enum page_type type);
3603b9109b0eSJaegeuk Kim void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
3604b9109b0eSJaegeuk Kim void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
3605bab475c5SChao Yu 				struct inode *inode, struct page *page,
3606bab475c5SChao Yu 				nid_t ino, enum page_type type);
36070b20fcecSChao Yu void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
36080b20fcecSChao Yu 					struct bio **bio, struct page *page);
3609b9109b0eSJaegeuk Kim void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
3610cac5a3d8SDongOh Shin int f2fs_submit_page_bio(struct f2fs_io_info *fio);
36118648de2cSChao Yu int f2fs_merge_page_bio(struct f2fs_io_info *fio);
3612fe16efe6SChao Yu void f2fs_submit_page_write(struct f2fs_io_info *fio);
3613cac5a3d8SDongOh Shin struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
3614cac5a3d8SDongOh Shin 			block_t blk_addr, struct bio *bio);
3615cac5a3d8SDongOh Shin int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
36164d57b86dSChao Yu void f2fs_set_data_blkaddr(struct dnode_of_data *dn);
3617cac5a3d8SDongOh Shin void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
36184d57b86dSChao Yu int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
36194d57b86dSChao Yu int f2fs_reserve_new_block(struct dnode_of_data *dn);
3620cac5a3d8SDongOh Shin int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
3621cac5a3d8SDongOh Shin int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from);
3622cac5a3d8SDongOh Shin int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
36234d57b86dSChao Yu struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
3624cac5a3d8SDongOh Shin 			int op_flags, bool for_write);
36254d57b86dSChao Yu struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index);
36264d57b86dSChao Yu struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
3627cac5a3d8SDongOh Shin 			bool for_write);
36284d57b86dSChao Yu struct page *f2fs_get_new_data_page(struct inode *inode,
3629cac5a3d8SDongOh Shin 			struct page *ipage, pgoff_t index, bool new_i_size);
36304d57b86dSChao Yu int f2fs_do_write_data_page(struct f2fs_io_info *fio);
36310ef81833SChao Yu void f2fs_do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock);
3632cac5a3d8SDongOh Shin int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
3633cac5a3d8SDongOh Shin 			int create, int flag);
3634cac5a3d8SDongOh Shin int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3635cac5a3d8SDongOh Shin 			u64 start, u64 len);
36364c8ff709SChao Yu int f2fs_encrypt_one_page(struct f2fs_io_info *fio);
36374d57b86dSChao Yu bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio);
36384d57b86dSChao Yu bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio);
36394c8ff709SChao Yu int f2fs_write_single_data_page(struct page *page, int *submitted,
36404c8ff709SChao Yu 				struct bio **bio, sector_t *last_block,
36414c8ff709SChao Yu 				struct writeback_control *wbc,
36424c8ff709SChao Yu 				enum iostat_type io_type,
36433afae09fSChao Yu 				int compr_blocks, bool allow_balance);
3644cac5a3d8SDongOh Shin void f2fs_invalidate_page(struct page *page, unsigned int offset,
3645cac5a3d8SDongOh Shin 			unsigned int length);
3646cac5a3d8SDongOh Shin int f2fs_release_page(struct page *page, gfp_t wait);
36475b7a487cSWeichao Guo #ifdef CONFIG_MIGRATION
3648cac5a3d8SDongOh Shin int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,
3649cac5a3d8SDongOh Shin 			struct page *page, enum migrate_mode mode);
36505b7a487cSWeichao Guo #endif
3651b91050a8SHyunchul Lee bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len);
36525ec2d99dSMatthew Wilcox void f2fs_clear_page_cache_dirty_tag(struct page *page);
36534c8ff709SChao Yu int f2fs_init_post_read_processing(void);
36544c8ff709SChao Yu void f2fs_destroy_post_read_processing(void);
36554c8ff709SChao Yu int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi);
36564c8ff709SChao Yu void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi);
365739a53e0cSJaegeuk Kim 
365839a53e0cSJaegeuk Kim /*
365939a53e0cSJaegeuk Kim  * gc.c
366039a53e0cSJaegeuk Kim  */
36614d57b86dSChao Yu int f2fs_start_gc_thread(struct f2fs_sb_info *sbi);
36624d57b86dSChao Yu void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi);
36634d57b86dSChao Yu block_t f2fs_start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
36647dede886SChao Yu int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background, bool force,
3665e066b83cSJaegeuk Kim 			unsigned int segno);
36664d57b86dSChao Yu void f2fs_build_gc_manager(struct f2fs_sb_info *sbi);
366704f0b2eaSQiuyang Sun int f2fs_resize_fs(struct f2fs_sb_info *sbi, __u64 block_count);
3668093749e2SChao Yu int __init f2fs_create_garbage_collection_cache(void);
3669093749e2SChao Yu void f2fs_destroy_garbage_collection_cache(void);
367039a53e0cSJaegeuk Kim 
367139a53e0cSJaegeuk Kim /*
367239a53e0cSJaegeuk Kim  * recovery.c
367339a53e0cSJaegeuk Kim  */
36744d57b86dSChao Yu int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
36754d57b86dSChao Yu bool f2fs_space_for_roll_forward(struct f2fs_sb_info *sbi);
3676cad83c96SChao Yu int __init f2fs_create_recovery_cache(void);
3677cad83c96SChao Yu void f2fs_destroy_recovery_cache(void);
367839a53e0cSJaegeuk Kim 
367939a53e0cSJaegeuk Kim /*
368039a53e0cSJaegeuk Kim  * debug.c
368139a53e0cSJaegeuk Kim  */
368239a53e0cSJaegeuk Kim #ifdef CONFIG_F2FS_STAT_FS
368339a53e0cSJaegeuk Kim struct f2fs_stat_info {
368439a53e0cSJaegeuk Kim 	struct list_head stat_list;
368539a53e0cSJaegeuk Kim 	struct f2fs_sb_info *sbi;
368639a53e0cSJaegeuk Kim 	int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
368739a53e0cSJaegeuk Kim 	int main_area_segs, main_area_sections, main_area_zones;
36885b7ee374SChao Yu 	unsigned long long hit_largest, hit_cached, hit_rbtree;
36895b7ee374SChao Yu 	unsigned long long hit_total, total_ext;
3690c00ba554SJaegeuk Kim 	int ext_tree, zombie_tree, ext_node;
36912c8a4a28SJaegeuk Kim 	int ndirty_node, ndirty_dent, ndirty_meta, ndirty_imeta;
36922c8a4a28SJaegeuk Kim 	int ndirty_data, ndirty_qdata;
369335782b23SJaegeuk Kim 	int inmem_pages;
36942c8a4a28SJaegeuk Kim 	unsigned int ndirty_dirs, ndirty_files, nquota_files, ndirty_all;
36955b0ef73cSJaegeuk Kim 	int nats, dirty_nats, sits, dirty_sits;
36965b0ef73cSJaegeuk Kim 	int free_nids, avail_nids, alloc_nids;
369739a53e0cSJaegeuk Kim 	int total_count, utilization;
36988b8dd65fSChao Yu 	int bg_gc, nr_wb_cp_data, nr_wb_data;
36995f9abab4SJaegeuk Kim 	int nr_rd_data, nr_rd_node, nr_rd_meta;
370002b16d0aSChao Yu 	int nr_dio_read, nr_dio_write;
3701274bd9baSChao Yu 	unsigned int io_skip_bggc, other_skip_bggc;
370214d8d5f7SChao Yu 	int nr_flushing, nr_flushed, flush_list_empty;
370314d8d5f7SChao Yu 	int nr_discarding, nr_discarded;
37045f32366aSChao Yu 	int nr_discard_cmd;
3705d84d1cbdSChao Yu 	unsigned int undiscard_blks;
3706261eeb9cSDaeho Jeong 	int nr_issued_ckpt, nr_total_ckpt, nr_queued_ckpt;
3707261eeb9cSDaeho Jeong 	unsigned int cur_ckpt_time, peak_ckpt_time;
3708a00861dbSJaegeuk Kim 	int inline_xattr, inline_inode, inline_dir, append, update, orphans;
3709ae999bb9SDaeho Jeong 	int compr_inode;
3710ae999bb9SDaeho Jeong 	unsigned long long compr_blocks;
3711648d50baSChao Yu 	int aw_cnt, max_aw_cnt, vw_cnt, max_vw_cnt;
3712f83a2584SYunlei He 	unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
371339a53e0cSJaegeuk Kim 	unsigned int bimodal, avg_vblocks;
371439a53e0cSJaegeuk Kim 	int util_free, util_valid, util_invalid;
371539a53e0cSJaegeuk Kim 	int rsvd_segs, overp_segs;
37166ce19affSChao Yu 	int dirty_count, node_pages, meta_pages, compress_pages;
37176ce19affSChao Yu 	int compress_page_hit;
371842190d2aSJaegeuk Kim 	int prefree_count, call_count, cp_count, bg_cp_count;
371939a53e0cSJaegeuk Kim 	int tot_segs, node_segs, data_segs, free_segs, free_secs;
3720e1235983SChangman Lee 	int bg_node_segs, bg_data_segs;
372139a53e0cSJaegeuk Kim 	int tot_blks, data_blks, node_blks;
3722e1235983SChangman Lee 	int bg_data_blks, bg_node_blks;
37232ef79ecbSChao Yu 	unsigned long long skipped_atomic_files[2];
372439a53e0cSJaegeuk Kim 	int curseg[NR_CURSEG_TYPE];
372539a53e0cSJaegeuk Kim 	int cursec[NR_CURSEG_TYPE];
372639a53e0cSJaegeuk Kim 	int curzone[NR_CURSEG_TYPE];
37270759e2c1SChao Yu 	unsigned int dirty_seg[NR_CURSEG_TYPE];
37280759e2c1SChao Yu 	unsigned int full_seg[NR_CURSEG_TYPE];
37290759e2c1SChao Yu 	unsigned int valid_blks[NR_CURSEG_TYPE];
373039a53e0cSJaegeuk Kim 
3731b63e7be5SChao Yu 	unsigned int meta_count[META_MAX];
373239a53e0cSJaegeuk Kim 	unsigned int segment_count[2];
373339a53e0cSJaegeuk Kim 	unsigned int block_count[2];
3734b9a2c252SChangman Lee 	unsigned int inplace_count;
37359edcdabfSChao Yu 	unsigned long long base_mem, cache_mem, page_mem;
373639a53e0cSJaegeuk Kim };
373739a53e0cSJaegeuk Kim 
3738963d4f7dSGu Zheng static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
3739963d4f7dSGu Zheng {
3740963d4f7dSGu Zheng 	return (struct f2fs_stat_info *)sbi->stat_info;
3741963d4f7dSGu Zheng }
3742963d4f7dSGu Zheng 
3743942e0be6SChangman Lee #define stat_inc_cp_count(si)		((si)->cp_count++)
374442190d2aSJaegeuk Kim #define stat_inc_bg_cp_count(si)	((si)->bg_cp_count++)
374539a53e0cSJaegeuk Kim #define stat_inc_call_count(si)		((si)->call_count++)
3746fc7100eaSHridya Valsaraju #define stat_inc_bggc_count(si)		((si)->bg_gc++)
3747274bd9baSChao Yu #define stat_io_skip_bggc_count(sbi)	((sbi)->io_skip_bggc++)
3748274bd9baSChao Yu #define stat_other_skip_bggc_count(sbi)	((sbi)->other_skip_bggc++)
374933fbd510SChao Yu #define stat_inc_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]++)
375033fbd510SChao Yu #define stat_dec_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]--)
37515b7ee374SChao Yu #define stat_inc_total_hit(sbi)		(atomic64_inc(&(sbi)->total_hit_ext))
37525b7ee374SChao Yu #define stat_inc_rbtree_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_rbtree))
37535b7ee374SChao Yu #define stat_inc_largest_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_largest))
37545b7ee374SChao Yu #define stat_inc_cached_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_cached))
3755d5e8f6c9SChao Yu #define stat_inc_inline_xattr(inode)					\
3756d5e8f6c9SChao Yu 	do {								\
3757d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3758d5e8f6c9SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_xattr));	\
3759d5e8f6c9SChao Yu 	} while (0)
3760d5e8f6c9SChao Yu #define stat_dec_inline_xattr(inode)					\
3761d5e8f6c9SChao Yu 	do {								\
3762d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3763d5e8f6c9SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_xattr));	\
3764d5e8f6c9SChao Yu 	} while (0)
37650dbdc2aeSJaegeuk Kim #define stat_inc_inline_inode(inode)					\
37660dbdc2aeSJaegeuk Kim 	do {								\
37670dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
376803e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_inode));	\
37690dbdc2aeSJaegeuk Kim 	} while (0)
37700dbdc2aeSJaegeuk Kim #define stat_dec_inline_inode(inode)					\
37710dbdc2aeSJaegeuk Kim 	do {								\
37720dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
377303e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_inode));	\
37740dbdc2aeSJaegeuk Kim 	} while (0)
37753289c061SJaegeuk Kim #define stat_inc_inline_dir(inode)					\
37763289c061SJaegeuk Kim 	do {								\
37773289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
377803e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_dir));	\
37793289c061SJaegeuk Kim 	} while (0)
37803289c061SJaegeuk Kim #define stat_dec_inline_dir(inode)					\
37813289c061SJaegeuk Kim 	do {								\
37823289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
378303e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_dir));	\
37843289c061SJaegeuk Kim 	} while (0)
37854c8ff709SChao Yu #define stat_inc_compr_inode(inode)					\
37864c8ff709SChao Yu 	do {								\
37874c8ff709SChao Yu 		if (f2fs_compressed_file(inode))			\
37884c8ff709SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->compr_inode));	\
37894c8ff709SChao Yu 	} while (0)
37904c8ff709SChao Yu #define stat_dec_compr_inode(inode)					\
37914c8ff709SChao Yu 	do {								\
37924c8ff709SChao Yu 		if (f2fs_compressed_file(inode))			\
37934c8ff709SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->compr_inode));	\
37944c8ff709SChao Yu 	} while (0)
37954c8ff709SChao Yu #define stat_add_compr_blocks(inode, blocks)				\
3796ae999bb9SDaeho Jeong 		(atomic64_add(blocks, &F2FS_I_SB(inode)->compr_blocks))
37974c8ff709SChao Yu #define stat_sub_compr_blocks(inode, blocks)				\
3798ae999bb9SDaeho Jeong 		(atomic64_sub(blocks, &F2FS_I_SB(inode)->compr_blocks))
3799b63e7be5SChao Yu #define stat_inc_meta_count(sbi, blkaddr)				\
3800b63e7be5SChao Yu 	do {								\
3801b63e7be5SChao Yu 		if (blkaddr < SIT_I(sbi)->sit_base_addr)		\
3802b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_CP]);	\
3803b63e7be5SChao Yu 		else if (blkaddr < NM_I(sbi)->nat_blkaddr)		\
3804b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_SIT]);	\
3805b63e7be5SChao Yu 		else if (blkaddr < SM_I(sbi)->ssa_blkaddr)		\
3806b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_NAT]);	\
3807b63e7be5SChao Yu 		else if (blkaddr < SM_I(sbi)->main_blkaddr)		\
3808b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_SSA]);	\
3809b63e7be5SChao Yu 	} while (0)
3810dcdfff65SJaegeuk Kim #define stat_inc_seg_type(sbi, curseg)					\
3811dcdfff65SJaegeuk Kim 		((sbi)->segment_count[(curseg)->alloc_type]++)
3812dcdfff65SJaegeuk Kim #define stat_inc_block_count(sbi, curseg)				\
3813dcdfff65SJaegeuk Kim 		((sbi)->block_count[(curseg)->alloc_type]++)
3814b9a2c252SChangman Lee #define stat_inc_inplace_blocks(sbi)					\
3815b9a2c252SChangman Lee 		(atomic_inc(&(sbi)->inplace_count))
381626a28a0cSJaegeuk Kim #define stat_update_max_atomic_write(inode)				\
381726a28a0cSJaegeuk Kim 	do {								\
38180e6d0164SSahitya Tummala 		int cur = F2FS_I_SB(inode)->atomic_files;	\
381926a28a0cSJaegeuk Kim 		int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt);	\
382026a28a0cSJaegeuk Kim 		if (cur > max)						\
382126a28a0cSJaegeuk Kim 			atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur);	\
382226a28a0cSJaegeuk Kim 	} while (0)
3823648d50baSChao Yu #define stat_inc_volatile_write(inode)					\
3824648d50baSChao Yu 		(atomic_inc(&F2FS_I_SB(inode)->vw_cnt))
3825648d50baSChao Yu #define stat_dec_volatile_write(inode)					\
3826648d50baSChao Yu 		(atomic_dec(&F2FS_I_SB(inode)->vw_cnt))
3827648d50baSChao Yu #define stat_update_max_volatile_write(inode)				\
3828648d50baSChao Yu 	do {								\
3829648d50baSChao Yu 		int cur = atomic_read(&F2FS_I_SB(inode)->vw_cnt);	\
3830648d50baSChao Yu 		int max = atomic_read(&F2FS_I_SB(inode)->max_vw_cnt);	\
3831648d50baSChao Yu 		if (cur > max)						\
3832648d50baSChao Yu 			atomic_set(&F2FS_I_SB(inode)->max_vw_cnt, cur);	\
3833648d50baSChao Yu 	} while (0)
3834e1235983SChangman Lee #define stat_inc_seg_count(sbi, type, gc_type)				\
383539a53e0cSJaegeuk Kim 	do {								\
3836963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
383768afcf2dSTomohiro Kusumi 		si->tot_segs++;						\
383868afcf2dSTomohiro Kusumi 		if ((type) == SUM_TYPE_DATA) {				\
383939a53e0cSJaegeuk Kim 			si->data_segs++;				\
3840e1235983SChangman Lee 			si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0;	\
3841e1235983SChangman Lee 		} else {						\
384239a53e0cSJaegeuk Kim 			si->node_segs++;				\
3843e1235983SChangman Lee 			si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0;	\
3844e1235983SChangman Lee 		}							\
384539a53e0cSJaegeuk Kim 	} while (0)
384639a53e0cSJaegeuk Kim 
384739a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks)				\
384868afcf2dSTomohiro Kusumi 	((si)->tot_blks += (blks))
384939a53e0cSJaegeuk Kim 
3850e1235983SChangman Lee #define stat_inc_data_blk_count(sbi, blks, gc_type)			\
385139a53e0cSJaegeuk Kim 	do {								\
3852963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
385339a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
385439a53e0cSJaegeuk Kim 		si->data_blks += (blks);				\
385568afcf2dSTomohiro Kusumi 		si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
385639a53e0cSJaegeuk Kim 	} while (0)
385739a53e0cSJaegeuk Kim 
3858e1235983SChangman Lee #define stat_inc_node_blk_count(sbi, blks, gc_type)			\
385939a53e0cSJaegeuk Kim 	do {								\
3860963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
386139a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
386239a53e0cSJaegeuk Kim 		si->node_blks += (blks);				\
386368afcf2dSTomohiro Kusumi 		si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
386439a53e0cSJaegeuk Kim 	} while (0)
386539a53e0cSJaegeuk Kim 
3866cac5a3d8SDongOh Shin int f2fs_build_stats(struct f2fs_sb_info *sbi);
3867cac5a3d8SDongOh Shin void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
386821acc07dSGreg Kroah-Hartman void __init f2fs_create_root_stats(void);
38694589d25dSNamjae Jeon void f2fs_destroy_root_stats(void);
3870fc7100eaSHridya Valsaraju void f2fs_update_sit_info(struct f2fs_sb_info *sbi);
387139a53e0cSJaegeuk Kim #else
3872d66450e7SArnd Bergmann #define stat_inc_cp_count(si)				do { } while (0)
3873d66450e7SArnd Bergmann #define stat_inc_bg_cp_count(si)			do { } while (0)
3874d66450e7SArnd Bergmann #define stat_inc_call_count(si)				do { } while (0)
3875d66450e7SArnd Bergmann #define stat_inc_bggc_count(si)				do { } while (0)
3876274bd9baSChao Yu #define stat_io_skip_bggc_count(sbi)			do { } while (0)
3877274bd9baSChao Yu #define stat_other_skip_bggc_count(sbi)			do { } while (0)
3878d66450e7SArnd Bergmann #define stat_inc_dirty_inode(sbi, type)			do { } while (0)
3879d66450e7SArnd Bergmann #define stat_dec_dirty_inode(sbi, type)			do { } while (0)
3880fc7100eaSHridya Valsaraju #define stat_inc_total_hit(sbi)				do { } while (0)
3881fc7100eaSHridya Valsaraju #define stat_inc_rbtree_node_hit(sbi)			do { } while (0)
3882d66450e7SArnd Bergmann #define stat_inc_largest_node_hit(sbi)			do { } while (0)
3883d66450e7SArnd Bergmann #define stat_inc_cached_node_hit(sbi)			do { } while (0)
3884d66450e7SArnd Bergmann #define stat_inc_inline_xattr(inode)			do { } while (0)
3885d66450e7SArnd Bergmann #define stat_dec_inline_xattr(inode)			do { } while (0)
3886d66450e7SArnd Bergmann #define stat_inc_inline_inode(inode)			do { } while (0)
3887d66450e7SArnd Bergmann #define stat_dec_inline_inode(inode)			do { } while (0)
3888d66450e7SArnd Bergmann #define stat_inc_inline_dir(inode)			do { } while (0)
3889d66450e7SArnd Bergmann #define stat_dec_inline_dir(inode)			do { } while (0)
38904c8ff709SChao Yu #define stat_inc_compr_inode(inode)			do { } while (0)
38914c8ff709SChao Yu #define stat_dec_compr_inode(inode)			do { } while (0)
38924c8ff709SChao Yu #define stat_add_compr_blocks(inode, blocks)		do { } while (0)
38934c8ff709SChao Yu #define stat_sub_compr_blocks(inode, blocks)		do { } while (0)
3894d66450e7SArnd Bergmann #define stat_update_max_atomic_write(inode)		do { } while (0)
3895d66450e7SArnd Bergmann #define stat_inc_volatile_write(inode)			do { } while (0)
3896d66450e7SArnd Bergmann #define stat_dec_volatile_write(inode)			do { } while (0)
3897d66450e7SArnd Bergmann #define stat_update_max_volatile_write(inode)		do { } while (0)
3898b63e7be5SChao Yu #define stat_inc_meta_count(sbi, blkaddr)		do { } while (0)
3899d66450e7SArnd Bergmann #define stat_inc_seg_type(sbi, curseg)			do { } while (0)
3900d66450e7SArnd Bergmann #define stat_inc_block_count(sbi, curseg)		do { } while (0)
3901d66450e7SArnd Bergmann #define stat_inc_inplace_blocks(sbi)			do { } while (0)
3902d66450e7SArnd Bergmann #define stat_inc_seg_count(sbi, type, gc_type)		do { } while (0)
3903d66450e7SArnd Bergmann #define stat_inc_tot_blk_count(si, blks)		do { } while (0)
3904d66450e7SArnd Bergmann #define stat_inc_data_blk_count(sbi, blks, gc_type)	do { } while (0)
3905d66450e7SArnd Bergmann #define stat_inc_node_blk_count(sbi, blks, gc_type)	do { } while (0)
390639a53e0cSJaegeuk Kim 
390739a53e0cSJaegeuk Kim static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
390839a53e0cSJaegeuk Kim static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
390921acc07dSGreg Kroah-Hartman static inline void __init f2fs_create_root_stats(void) { }
39104589d25dSNamjae Jeon static inline void f2fs_destroy_root_stats(void) { }
391148abe91aSYueHaibing static inline void f2fs_update_sit_info(struct f2fs_sb_info *sbi) {}
391239a53e0cSJaegeuk Kim #endif
391339a53e0cSJaegeuk Kim 
391439a53e0cSJaegeuk Kim extern const struct file_operations f2fs_dir_operations;
391539a53e0cSJaegeuk Kim extern const struct file_operations f2fs_file_operations;
391639a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_file_inode_operations;
391739a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_dblock_aops;
391839a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_node_aops;
391939a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_meta_aops;
392039a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_dir_inode_operations;
392139a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_symlink_inode_operations;
3922cbaf042aSJaegeuk Kim extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
392339a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_special_inode_operations;
39244d57b86dSChao Yu extern struct kmem_cache *f2fs_inode_entry_slab;
39251001b347SHuajun Li 
3926e18c65b2SHuajun Li /*
3927e18c65b2SHuajun Li  * inline.c
3928e18c65b2SHuajun Li  */
3929cac5a3d8SDongOh Shin bool f2fs_may_inline_data(struct inode *inode);
3930cac5a3d8SDongOh Shin bool f2fs_may_inline_dentry(struct inode *inode);
39314d57b86dSChao Yu void f2fs_do_read_inline_data(struct page *page, struct page *ipage);
39324d57b86dSChao Yu void f2fs_truncate_inline_inode(struct inode *inode,
39334d57b86dSChao Yu 						struct page *ipage, u64 from);
3934cac5a3d8SDongOh Shin int f2fs_read_inline_data(struct inode *inode, struct page *page);
3935cac5a3d8SDongOh Shin int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
3936cac5a3d8SDongOh Shin int f2fs_convert_inline_inode(struct inode *inode);
3937b06af2afSJaegeuk Kim int f2fs_try_convert_inline_dir(struct inode *dir, struct dentry *dentry);
3938cac5a3d8SDongOh Shin int f2fs_write_inline_data(struct inode *inode, struct page *page);
39399627a7b3SChao Yu int f2fs_recover_inline_data(struct inode *inode, struct page *npage);
39404d57b86dSChao Yu struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
394143c780baSEric Biggers 					const struct f2fs_filename *fname,
394243c780baSEric Biggers 					struct page **res_page);
39434d57b86dSChao Yu int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent,
3944cac5a3d8SDongOh Shin 			struct page *ipage);
394543c780baSEric Biggers int f2fs_add_inline_entry(struct inode *dir, const struct f2fs_filename *fname,
3946cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
39474d57b86dSChao Yu void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry,
39484d57b86dSChao Yu 				struct page *page, struct inode *dir,
39494d57b86dSChao Yu 				struct inode *inode);
3950cac5a3d8SDongOh Shin bool f2fs_empty_inline_dir(struct inode *dir);
3951cac5a3d8SDongOh Shin int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
3952cac5a3d8SDongOh Shin 			struct fscrypt_str *fstr);
3953cac5a3d8SDongOh Shin int f2fs_inline_data_fiemap(struct inode *inode,
3954cac5a3d8SDongOh Shin 			struct fiemap_extent_info *fieinfo,
3955cac5a3d8SDongOh Shin 			__u64 start, __u64 len);
3956cde4de12SJaegeuk Kim 
3957cde4de12SJaegeuk Kim /*
39582658e50dSJaegeuk Kim  * shrinker.c
39592658e50dSJaegeuk Kim  */
3960cac5a3d8SDongOh Shin unsigned long f2fs_shrink_count(struct shrinker *shrink,
3961cac5a3d8SDongOh Shin 			struct shrink_control *sc);
3962cac5a3d8SDongOh Shin unsigned long f2fs_shrink_scan(struct shrinker *shrink,
3963cac5a3d8SDongOh Shin 			struct shrink_control *sc);
3964cac5a3d8SDongOh Shin void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
3965cac5a3d8SDongOh Shin void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
39662658e50dSJaegeuk Kim 
39672658e50dSJaegeuk Kim /*
3968a28ef1f5SChao Yu  * extent_cache.c
3969a28ef1f5SChao Yu  */
39704dada3fdSChao Yu struct rb_entry *f2fs_lookup_rb_tree(struct rb_root_cached *root,
3971004b6862SChao Yu 				struct rb_entry *cached_re, unsigned int ofs);
39722e9b2bb2SChao Yu struct rb_node **f2fs_lookup_rb_tree_ext(struct f2fs_sb_info *sbi,
39732e9b2bb2SChao Yu 				struct rb_root_cached *root,
39742e9b2bb2SChao Yu 				struct rb_node **parent,
39752e9b2bb2SChao Yu 				unsigned long long key, bool *left_most);
39764d57b86dSChao Yu struct rb_node **f2fs_lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
39774dada3fdSChao Yu 				struct rb_root_cached *root,
39784dada3fdSChao Yu 				struct rb_node **parent,
39794dada3fdSChao Yu 				unsigned int ofs, bool *leftmost);
39804dada3fdSChao Yu struct rb_entry *f2fs_lookup_rb_tree_ret(struct rb_root_cached *root,
3981004b6862SChao Yu 		struct rb_entry *cached_re, unsigned int ofs,
3982004b6862SChao Yu 		struct rb_entry **prev_entry, struct rb_entry **next_entry,
3983004b6862SChao Yu 		struct rb_node ***insert_p, struct rb_node **insert_parent,
39844dada3fdSChao Yu 		bool force, bool *leftmost);
39854d57b86dSChao Yu bool f2fs_check_rb_tree_consistence(struct f2fs_sb_info *sbi,
39862e9b2bb2SChao Yu 				struct rb_root_cached *root, bool check_key);
3987cac5a3d8SDongOh Shin unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
3988a6d601f3SChao Yu void f2fs_init_extent_tree(struct inode *inode, struct page *ipage);
3989cac5a3d8SDongOh Shin void f2fs_drop_extent_tree(struct inode *inode);
3990cac5a3d8SDongOh Shin unsigned int f2fs_destroy_extent_node(struct inode *inode);
3991cac5a3d8SDongOh Shin void f2fs_destroy_extent_tree(struct inode *inode);
3992cac5a3d8SDongOh Shin bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
3993cac5a3d8SDongOh Shin 			struct extent_info *ei);
3994cac5a3d8SDongOh Shin void f2fs_update_extent_cache(struct dnode_of_data *dn);
399519b2c30dSChao Yu void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
3996cac5a3d8SDongOh Shin 			pgoff_t fofs, block_t blkaddr, unsigned int len);
39974d57b86dSChao Yu void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi);
39984d57b86dSChao Yu int __init f2fs_create_extent_cache(void);
39994d57b86dSChao Yu void f2fs_destroy_extent_cache(void);
4000a28ef1f5SChao Yu 
4001a28ef1f5SChao Yu /*
40028ceffcb2SChao Yu  * sysfs.c
40038ceffcb2SChao Yu  */
4004dc6b2055SJaegeuk Kim int __init f2fs_init_sysfs(void);
4005dc6b2055SJaegeuk Kim void f2fs_exit_sysfs(void);
4006dc6b2055SJaegeuk Kim int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
4007dc6b2055SJaegeuk Kim void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
40088ceffcb2SChao Yu 
400995ae251fSEric Biggers /* verity.c */
401095ae251fSEric Biggers extern const struct fsverity_operations f2fs_verityops;
401195ae251fSEric Biggers 
40128ceffcb2SChao Yu /*
4013cde4de12SJaegeuk Kim  * crypto support
4014cde4de12SJaegeuk Kim  */
40151958593eSJaegeuk Kim static inline bool f2fs_encrypted_file(struct inode *inode)
40161958593eSJaegeuk Kim {
401762230e0dSChandan Rajendra 	return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
40181958593eSJaegeuk Kim }
40191958593eSJaegeuk Kim 
4020cde4de12SJaegeuk Kim static inline void f2fs_set_encrypted_inode(struct inode *inode)
4021cde4de12SJaegeuk Kim {
4022643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
4023cde4de12SJaegeuk Kim 	file_set_encrypt(inode);
40249149a5ebSChao Yu 	f2fs_set_inode_flags(inode);
4025cde4de12SJaegeuk Kim #endif
4026cde4de12SJaegeuk Kim }
4027cde4de12SJaegeuk Kim 
40286dbb1796SEric Biggers /*
40296dbb1796SEric Biggers  * Returns true if the reads of the inode's data need to undergo some
40306dbb1796SEric Biggers  * postprocessing step, like decryption or authenticity verification.
40316dbb1796SEric Biggers  */
40326dbb1796SEric Biggers static inline bool f2fs_post_read_required(struct inode *inode)
4033cde4de12SJaegeuk Kim {
40344c8ff709SChao Yu 	return f2fs_encrypted_file(inode) || fsverity_active(inode) ||
40354c8ff709SChao Yu 		f2fs_compressed_file(inode);
40364c8ff709SChao Yu }
40374c8ff709SChao Yu 
40384c8ff709SChao Yu /*
40394c8ff709SChao Yu  * compress.c
40404c8ff709SChao Yu  */
40414c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
40424c8ff709SChao Yu bool f2fs_is_compressed_page(struct page *page);
40434c8ff709SChao Yu struct page *f2fs_compress_control_page(struct page *page);
40444c8ff709SChao Yu int f2fs_prepare_compress_overwrite(struct inode *inode,
40454c8ff709SChao Yu 			struct page **pagep, pgoff_t index, void **fsdata);
40464c8ff709SChao Yu bool f2fs_compress_write_end(struct inode *inode, void *fsdata,
40474c8ff709SChao Yu 					pgoff_t index, unsigned copied);
40483265d3dbSChao Yu int f2fs_truncate_partial_cluster(struct inode *inode, u64 from, bool lock);
40494c8ff709SChao Yu void f2fs_compress_write_end_io(struct bio *bio, struct page *page);
40504c8ff709SChao Yu bool f2fs_is_compress_backend_ready(struct inode *inode);
40515e6bbde9SChao Yu int f2fs_init_compress_mempool(void);
40525e6bbde9SChao Yu void f2fs_destroy_compress_mempool(void);
40536ce19affSChao Yu void f2fs_decompress_cluster(struct decompress_io_ctx *dic);
40546ce19affSChao Yu void f2fs_end_read_compressed_page(struct page *page, bool failed,
40556ce19affSChao Yu 							block_t blkaddr);
40564c8ff709SChao Yu bool f2fs_cluster_is_empty(struct compress_ctx *cc);
40574c8ff709SChao Yu bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index);
40584c8ff709SChao Yu void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct page *page);
40594c8ff709SChao Yu int f2fs_write_multi_pages(struct compress_ctx *cc,
40604c8ff709SChao Yu 						int *submitted,
40614c8ff709SChao Yu 						struct writeback_control *wbc,
40624c8ff709SChao Yu 						enum iostat_type io_type);
40634c8ff709SChao Yu int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index);
40644c8ff709SChao Yu int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
40654c8ff709SChao Yu 				unsigned nr_pages, sector_t *last_block_in_bio,
40660683728aSChao Yu 				bool is_readahead, bool for_write);
40674c8ff709SChao Yu struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc);
40687f59b277SEric Biggers void f2fs_decompress_end_io(struct decompress_io_ctx *dic, bool failed);
40697f59b277SEric Biggers void f2fs_put_page_dic(struct page *page);
40704c8ff709SChao Yu int f2fs_init_compress_ctx(struct compress_ctx *cc);
40718bfbfb0dSChao Yu void f2fs_destroy_compress_ctx(struct compress_ctx *cc, bool reuse);
40724c8ff709SChao Yu void f2fs_init_compress_info(struct f2fs_sb_info *sbi);
40736ce19affSChao Yu int f2fs_init_compress_inode(struct f2fs_sb_info *sbi);
40746ce19affSChao Yu void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi);
407531083031SChao Yu int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi);
407631083031SChao Yu void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi);
4077c68d6c88SChao Yu int __init f2fs_init_compress_cache(void);
4078c68d6c88SChao Yu void f2fs_destroy_compress_cache(void);
40796ce19affSChao Yu struct address_space *COMPRESS_MAPPING(struct f2fs_sb_info *sbi);
40806ce19affSChao Yu void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi, block_t blkaddr);
40816ce19affSChao Yu void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
40826ce19affSChao Yu 						nid_t ino, block_t blkaddr);
40836ce19affSChao Yu bool f2fs_load_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
40846ce19affSChao Yu 								block_t blkaddr);
40856ce19affSChao Yu void f2fs_invalidate_compress_pages(struct f2fs_sb_info *sbi, nid_t ino);
40865ac443e2SDaeho Jeong #define inc_compr_inode_stat(inode)					\
40875ac443e2SDaeho Jeong 	do {								\
40885ac443e2SDaeho Jeong 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);		\
40895ac443e2SDaeho Jeong 		sbi->compr_new_inode++;					\
40905ac443e2SDaeho Jeong 	} while (0)
40915ac443e2SDaeho Jeong #define add_compr_block_stat(inode, blocks)				\
40925ac443e2SDaeho Jeong 	do {								\
40935ac443e2SDaeho Jeong 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);		\
40945ac443e2SDaeho Jeong 		int diff = F2FS_I(inode)->i_cluster_size - blocks;	\
40955ac443e2SDaeho Jeong 		sbi->compr_written_block += blocks;			\
40965ac443e2SDaeho Jeong 		sbi->compr_saved_block += diff;				\
40975ac443e2SDaeho Jeong 	} while (0)
40984c8ff709SChao Yu #else
40994c8ff709SChao Yu static inline bool f2fs_is_compressed_page(struct page *page) { return false; }
41004c8ff709SChao Yu static inline bool f2fs_is_compress_backend_ready(struct inode *inode)
41014c8ff709SChao Yu {
41024c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
41034c8ff709SChao Yu 		return true;
41044c8ff709SChao Yu 	/* not support compression */
41054c8ff709SChao Yu 	return false;
41064c8ff709SChao Yu }
41074c8ff709SChao Yu static inline struct page *f2fs_compress_control_page(struct page *page)
41084c8ff709SChao Yu {
41094c8ff709SChao Yu 	WARN_ON_ONCE(1);
41104c8ff709SChao Yu 	return ERR_PTR(-EINVAL);
41114c8ff709SChao Yu }
41125e6bbde9SChao Yu static inline int f2fs_init_compress_mempool(void) { return 0; }
41135e6bbde9SChao Yu static inline void f2fs_destroy_compress_mempool(void) { }
41146ce19affSChao Yu static inline void f2fs_decompress_cluster(struct decompress_io_ctx *dic) { }
41156ce19affSChao Yu static inline void f2fs_end_read_compressed_page(struct page *page,
41166ce19affSChao Yu 						bool failed, block_t blkaddr)
41177f59b277SEric Biggers {
41187f59b277SEric Biggers 	WARN_ON_ONCE(1);
41197f59b277SEric Biggers }
41207f59b277SEric Biggers static inline void f2fs_put_page_dic(struct page *page)
41217f59b277SEric Biggers {
41227f59b277SEric Biggers 	WARN_ON_ONCE(1);
41237f59b277SEric Biggers }
41246ce19affSChao Yu static inline int f2fs_init_compress_inode(struct f2fs_sb_info *sbi) { return 0; }
41256ce19affSChao Yu static inline void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi) { }
412631083031SChao Yu static inline int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi) { return 0; }
412731083031SChao Yu static inline void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi) { }
4128c68d6c88SChao Yu static inline int __init f2fs_init_compress_cache(void) { return 0; }
4129c68d6c88SChao Yu static inline void f2fs_destroy_compress_cache(void) { }
41306ce19affSChao Yu static inline void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi,
41316ce19affSChao Yu 				block_t blkaddr) { }
41326ce19affSChao Yu static inline void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi,
41336ce19affSChao Yu 				struct page *page, nid_t ino, block_t blkaddr) { }
41346ce19affSChao Yu static inline bool f2fs_load_compressed_page(struct f2fs_sb_info *sbi,
41356ce19affSChao Yu 				struct page *page, block_t blkaddr) { return false; }
41366ce19affSChao Yu static inline void f2fs_invalidate_compress_pages(struct f2fs_sb_info *sbi,
41376ce19affSChao Yu 							nid_t ino) { }
41385ac443e2SDaeho Jeong #define inc_compr_inode_stat(inode)		do { } while (0)
41394c8ff709SChao Yu #endif
41404c8ff709SChao Yu 
41414c8ff709SChao Yu static inline void set_compress_context(struct inode *inode)
41424c8ff709SChao Yu {
41434c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
41444c8ff709SChao Yu 
41454c8ff709SChao Yu 	F2FS_I(inode)->i_compress_algorithm =
41464c8ff709SChao Yu 			F2FS_OPTION(sbi).compress_algorithm;
41474c8ff709SChao Yu 	F2FS_I(inode)->i_log_cluster_size =
41484c8ff709SChao Yu 			F2FS_OPTION(sbi).compress_log_size;
4149b28f047bSChao Yu 	F2FS_I(inode)->i_compress_flag =
4150b28f047bSChao Yu 			F2FS_OPTION(sbi).compress_chksum ?
4151b28f047bSChao Yu 				1 << COMPRESS_CHKSUM : 0;
41524c8ff709SChao Yu 	F2FS_I(inode)->i_cluster_size =
41534c8ff709SChao Yu 			1 << F2FS_I(inode)->i_log_cluster_size;
41543fde13f8SChao Yu 	if (F2FS_I(inode)->i_compress_algorithm == COMPRESS_LZ4 &&
41553fde13f8SChao Yu 			F2FS_OPTION(sbi).compress_level)
41563fde13f8SChao Yu 		F2FS_I(inode)->i_compress_flag |=
41573fde13f8SChao Yu 				F2FS_OPTION(sbi).compress_level <<
41583fde13f8SChao Yu 				COMPRESS_LEVEL_OFFSET;
41594c8ff709SChao Yu 	F2FS_I(inode)->i_flags |= F2FS_COMPR_FL;
41604c8ff709SChao Yu 	set_inode_flag(inode, FI_COMPRESSED_FILE);
41614c8ff709SChao Yu 	stat_inc_compr_inode(inode);
41625ac443e2SDaeho Jeong 	inc_compr_inode_stat(inode);
4163530e0704SChao Yu 	f2fs_mark_inode_dirty_sync(inode, true);
41644c8ff709SChao Yu }
41654c8ff709SChao Yu 
416678134d03SDaeho Jeong static inline bool f2fs_disable_compressed_file(struct inode *inode)
41674c8ff709SChao Yu {
41684c8ff709SChao Yu 	struct f2fs_inode_info *fi = F2FS_I(inode);
41694c8ff709SChao Yu 
41704c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
417178134d03SDaeho Jeong 		return true;
417278134d03SDaeho Jeong 	if (S_ISREG(inode->i_mode) &&
417378134d03SDaeho Jeong 		(get_dirty_pages(inode) || atomic_read(&fi->i_compr_blocks)))
417478134d03SDaeho Jeong 		return false;
41754c8ff709SChao Yu 
41764c8ff709SChao Yu 	fi->i_flags &= ~F2FS_COMPR_FL;
41774c8ff709SChao Yu 	stat_dec_compr_inode(inode);
417896f5b4faSChao Yu 	clear_inode_flag(inode, FI_COMPRESSED_FILE);
4179530e0704SChao Yu 	f2fs_mark_inode_dirty_sync(inode, true);
418078134d03SDaeho Jeong 	return true;
4181cde4de12SJaegeuk Kim }
4182cde4de12SJaegeuk Kim 
4183ccd31cb2SSheng Yong #define F2FS_FEATURE_FUNCS(name, flagname) \
41847beb01f7SChao Yu static inline int f2fs_sb_has_##name(struct f2fs_sb_info *sbi) \
4185ccd31cb2SSheng Yong { \
41867beb01f7SChao Yu 	return F2FS_HAS_FEATURE(sbi, F2FS_FEATURE_##flagname); \
4187cde4de12SJaegeuk Kim }
4188f424f664SJaegeuk Kim 
4189ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(encrypt, ENCRYPT);
4190ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(blkzoned, BLKZONED);
4191ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(extra_attr, EXTRA_ATTR);
4192ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(project_quota, PRJQUOTA);
4193ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_chksum, INODE_CHKSUM);
4194ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
4195ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
4196ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
4197b7c409deSSheng Yong F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
419895ae251fSEric Biggers F2FS_FEATURE_FUNCS(verity, VERITY);
4199d440c52dSJunling Zheng F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
42005aba5430SDaniel Rosenberg F2FS_FEATURE_FUNCS(casefold, CASEFOLD);
42014c8ff709SChao Yu F2FS_FEATURE_FUNCS(compression, COMPRESSION);
4202a7d9fe3cSJaegeuk Kim F2FS_FEATURE_FUNCS(readonly, RO);
42031c1d35dfSChao Yu 
4204178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
420595175dafSDamien Le Moal static inline bool f2fs_blkz_is_seq(struct f2fs_sb_info *sbi, int devi,
420695175dafSDamien Le Moal 				    block_t blkaddr)
4207178053e2SDamien Le Moal {
4208178053e2SDamien Le Moal 	unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
4209178053e2SDamien Le Moal 
421095175dafSDamien Le Moal 	return test_bit(zno, FDEV(devi).blkz_seq);
4211178053e2SDamien Le Moal }
4212178053e2SDamien Le Moal #endif
4213178053e2SDamien Le Moal 
42147d20c8abSChao Yu static inline bool f2fs_hw_should_discard(struct f2fs_sb_info *sbi)
421596ba2decSDamien Le Moal {
42167beb01f7SChao Yu 	return f2fs_sb_has_blkzoned(sbi);
42177d20c8abSChao Yu }
421896ba2decSDamien Le Moal 
42197f3d7719SDamien Le Moal static inline bool f2fs_bdev_support_discard(struct block_device *bdev)
42207f3d7719SDamien Le Moal {
42217f3d7719SDamien Le Moal 	return blk_queue_discard(bdev_get_queue(bdev)) ||
42227f3d7719SDamien Le Moal 	       bdev_is_zoned(bdev);
42237f3d7719SDamien Le Moal }
42247f3d7719SDamien Le Moal 
42257d20c8abSChao Yu static inline bool f2fs_hw_support_discard(struct f2fs_sb_info *sbi)
42267d20c8abSChao Yu {
42277f3d7719SDamien Le Moal 	int i;
42287f3d7719SDamien Le Moal 
42297f3d7719SDamien Le Moal 	if (!f2fs_is_multi_device(sbi))
42307f3d7719SDamien Le Moal 		return f2fs_bdev_support_discard(sbi->sb->s_bdev);
42317f3d7719SDamien Le Moal 
42327f3d7719SDamien Le Moal 	for (i = 0; i < sbi->s_ndevs; i++)
42337f3d7719SDamien Le Moal 		if (f2fs_bdev_support_discard(FDEV(i).bdev))
42347f3d7719SDamien Le Moal 			return true;
42357f3d7719SDamien Le Moal 	return false;
42367d20c8abSChao Yu }
42377d20c8abSChao Yu 
42387d20c8abSChao Yu static inline bool f2fs_realtime_discard_enable(struct f2fs_sb_info *sbi)
42397d20c8abSChao Yu {
42407d20c8abSChao Yu 	return (test_opt(sbi, DISCARD) && f2fs_hw_support_discard(sbi)) ||
42417d20c8abSChao Yu 					f2fs_hw_should_discard(sbi);
424252763a4bSJaegeuk Kim }
424352763a4bSJaegeuk Kim 
4244f824deb5SChao Yu static inline bool f2fs_hw_is_readonly(struct f2fs_sb_info *sbi)
4245f824deb5SChao Yu {
4246f824deb5SChao Yu 	int i;
4247f824deb5SChao Yu 
4248f824deb5SChao Yu 	if (!f2fs_is_multi_device(sbi))
4249f824deb5SChao Yu 		return bdev_read_only(sbi->sb->s_bdev);
4250f824deb5SChao Yu 
4251f824deb5SChao Yu 	for (i = 0; i < sbi->s_ndevs; i++)
4252f824deb5SChao Yu 		if (bdev_read_only(FDEV(i).bdev))
4253f824deb5SChao Yu 			return true;
4254f824deb5SChao Yu 	return false;
4255f824deb5SChao Yu }
4256f824deb5SChao Yu 
4257b0332a0fSChao Yu static inline bool f2fs_lfs_mode(struct f2fs_sb_info *sbi)
425852763a4bSJaegeuk Kim {
4259b0332a0fSChao Yu 	return F2FS_OPTION(sbi).fs_mode == FS_MODE_LFS;
426052763a4bSJaegeuk Kim }
426152763a4bSJaegeuk Kim 
42624c8ff709SChao Yu static inline bool f2fs_may_compress(struct inode *inode)
42634c8ff709SChao Yu {
42644c8ff709SChao Yu 	if (IS_SWAPFILE(inode) || f2fs_is_pinned_file(inode) ||
42654c8ff709SChao Yu 				f2fs_is_atomic_file(inode) ||
42664c8ff709SChao Yu 				f2fs_is_volatile_file(inode))
42674c8ff709SChao Yu 		return false;
42684c8ff709SChao Yu 	return S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode);
42694c8ff709SChao Yu }
42704c8ff709SChao Yu 
42714c8ff709SChao Yu static inline void f2fs_i_compr_blocks_update(struct inode *inode,
42724c8ff709SChao Yu 						u64 blocks, bool add)
42734c8ff709SChao Yu {
42744c8ff709SChao Yu 	int diff = F2FS_I(inode)->i_cluster_size - blocks;
4275c2759ebaSDaeho Jeong 	struct f2fs_inode_info *fi = F2FS_I(inode);
42764c8ff709SChao Yu 
4277ef8d563fSChao Yu 	/* don't update i_compr_blocks if saved blocks were released */
4278c2759ebaSDaeho Jeong 	if (!add && !atomic_read(&fi->i_compr_blocks))
4279ef8d563fSChao Yu 		return;
4280ef8d563fSChao Yu 
42814c8ff709SChao Yu 	if (add) {
4282c2759ebaSDaeho Jeong 		atomic_add(diff, &fi->i_compr_blocks);
42834c8ff709SChao Yu 		stat_add_compr_blocks(inode, diff);
42844c8ff709SChao Yu 	} else {
4285c2759ebaSDaeho Jeong 		atomic_sub(diff, &fi->i_compr_blocks);
42864c8ff709SChao Yu 		stat_sub_compr_blocks(inode, diff);
42874c8ff709SChao Yu 	}
42884c8ff709SChao Yu 	f2fs_mark_inode_dirty_sync(inode, true);
42894c8ff709SChao Yu }
42904c8ff709SChao Yu 
4291f847c699SChao Yu static inline int block_unaligned_IO(struct inode *inode,
4292f847c699SChao Yu 				struct kiocb *iocb, struct iov_iter *iter)
4293b91050a8SHyunchul Lee {
4294f847c699SChao Yu 	unsigned int i_blkbits = READ_ONCE(inode->i_blkbits);
4295f847c699SChao Yu 	unsigned int blocksize_mask = (1 << i_blkbits) - 1;
4296f847c699SChao Yu 	loff_t offset = iocb->ki_pos;
4297f847c699SChao Yu 	unsigned long align = offset | iov_iter_alignment(iter);
4298f847c699SChao Yu 
4299f847c699SChao Yu 	return align & blocksize_mask;
4300f847c699SChao Yu }
4301f847c699SChao Yu 
4302f847c699SChao Yu static inline int allow_outplace_dio(struct inode *inode,
4303f847c699SChao Yu 				struct kiocb *iocb, struct iov_iter *iter)
4304f847c699SChao Yu {
4305f847c699SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4306f847c699SChao Yu 	int rw = iov_iter_rw(iter);
4307f847c699SChao Yu 
4308b0332a0fSChao Yu 	return (f2fs_lfs_mode(sbi) && (rw == WRITE) &&
4309f847c699SChao Yu 				!block_unaligned_IO(inode, iocb, iter));
4310f847c699SChao Yu }
4311f847c699SChao Yu 
4312f847c699SChao Yu static inline bool f2fs_force_buffered_io(struct inode *inode,
4313f847c699SChao Yu 				struct kiocb *iocb, struct iov_iter *iter)
4314f847c699SChao Yu {
4315f847c699SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4316f847c699SChao Yu 	int rw = iov_iter_rw(iter);
4317f847c699SChao Yu 
4318f847c699SChao Yu 	if (f2fs_post_read_required(inode))
4319f847c699SChao Yu 		return true;
43200916878dSDamien Le Moal 	if (f2fs_is_multi_device(sbi))
4321f847c699SChao Yu 		return true;
4322f847c699SChao Yu 	/*
4323f847c699SChao Yu 	 * for blkzoned device, fallback direct IO to buffered IO, so
4324f847c699SChao Yu 	 * all IOs can be serialized by log-structured write.
4325f847c699SChao Yu 	 */
43267beb01f7SChao Yu 	if (f2fs_sb_has_blkzoned(sbi))
4327f847c699SChao Yu 		return true;
4328b0332a0fSChao Yu 	if (f2fs_lfs_mode(sbi) && (rw == WRITE)) {
43299720ee80SChao Yu 		if (block_unaligned_IO(inode, iocb, iter))
4330f847c699SChao Yu 			return true;
43319720ee80SChao Yu 		if (F2FS_IO_ALIGNED(sbi))
43329720ee80SChao Yu 			return true;
43339720ee80SChao Yu 	}
4334ebc29b62Shuangjianan@oppo.com 	if (is_sbi_flag_set(F2FS_I_SB(inode), SBI_CP_DISABLED))
43354354994fSDaniel Rosenberg 		return true;
43364354994fSDaniel Rosenberg 
4337f847c699SChao Yu 	return false;
4338b91050a8SHyunchul Lee }
4339b91050a8SHyunchul Lee 
43407f59b277SEric Biggers static inline bool f2fs_need_verity(const struct inode *inode, pgoff_t idx)
43417f59b277SEric Biggers {
43427f59b277SEric Biggers 	return fsverity_active(inode) &&
43437f59b277SEric Biggers 	       idx < DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
43447f59b277SEric Biggers }
43457f59b277SEric Biggers 
434683a3bfdbSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
4347d494500aSChao Yu extern void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate,
4348d494500aSChao Yu 							unsigned int type);
434983a3bfdbSJaegeuk Kim #else
4350d494500aSChao Yu #define f2fs_build_fault_attr(sbi, rate, type)		do { } while (0)
435183a3bfdbSJaegeuk Kim #endif
435283a3bfdbSJaegeuk Kim 
4353af033b2aSChao Yu static inline bool is_journalled_quota(struct f2fs_sb_info *sbi)
4354af033b2aSChao Yu {
4355af033b2aSChao Yu #ifdef CONFIG_QUOTA
43567beb01f7SChao Yu 	if (f2fs_sb_has_quota_ino(sbi))
4357af033b2aSChao Yu 		return true;
4358af033b2aSChao Yu 	if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
4359af033b2aSChao Yu 		F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
4360af033b2aSChao Yu 		F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
4361af033b2aSChao Yu 		return true;
4362af033b2aSChao Yu #endif
4363af033b2aSChao Yu 	return false;
4364af033b2aSChao Yu }
43650af725fcSJaegeuk Kim 
436610f966bbSChao Yu #define EFSBADCRC	EBADMSG		/* Bad CRC detected */
436710f966bbSChao Yu #define EFSCORRUPTED	EUCLEAN		/* Filesystem is corrupted */
436810f966bbSChao Yu 
4369e1074d4bSChao Yu #endif /* _LINUX_F2FS_H */
4370