xref: /openbmc/linux/fs/f2fs/f2fs.h (revision 95fa90c9e5a7f14c2497d5b032544478c9377c3a)
1d29fbcdbSNishad Kamdar /* SPDX-License-Identifier: GPL-2.0 */
20a8165d7SJaegeuk Kim /*
339a53e0cSJaegeuk Kim  * fs/f2fs/f2fs.h
439a53e0cSJaegeuk Kim  *
539a53e0cSJaegeuk Kim  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
639a53e0cSJaegeuk Kim  *             http://www.samsung.com/
739a53e0cSJaegeuk Kim  */
839a53e0cSJaegeuk Kim #ifndef _LINUX_F2FS_H
939a53e0cSJaegeuk Kim #define _LINUX_F2FS_H
1039a53e0cSJaegeuk Kim 
11f847c699SChao Yu #include <linux/uio.h>
1239a53e0cSJaegeuk Kim #include <linux/types.h>
1339a53e0cSJaegeuk Kim #include <linux/page-flags.h>
1439a53e0cSJaegeuk Kim #include <linux/buffer_head.h>
1539a53e0cSJaegeuk Kim #include <linux/slab.h>
1639a53e0cSJaegeuk Kim #include <linux/crc32.h>
1739a53e0cSJaegeuk Kim #include <linux/magic.h>
18c2d715d1SJaegeuk Kim #include <linux/kobject.h>
197bd59381SGu Zheng #include <linux/sched.h>
207c2e5963SJaegeuk Kim #include <linux/cred.h>
21df808180SMatthew Wilcox (Oracle) #include <linux/sched/mm.h>
2239307a8eSJaegeuk Kim #include <linux/vmalloc.h>
23740432f8SJaegeuk Kim #include <linux/bio.h>
24d0239e1bSJaegeuk Kim #include <linux/blkdev.h>
250abd675eSChao Yu #include <linux/quotaops.h>
26c6a564ffSChristoph Hellwig #include <linux/part_stat.h>
2743b6573bSKeith Mok #include <crypto/hash.h>
2839a53e0cSJaegeuk Kim 
29734f0d24SDave Chinner #include <linux/fscrypt.h>
3095ae251fSEric Biggers #include <linux/fsverity.h>
31734f0d24SDave Chinner 
3251dcbdacSMatthew Wilcox (Oracle) struct pagevec;
3351dcbdacSMatthew Wilcox (Oracle) 
345d56b671SJaegeuk Kim #ifdef CONFIG_F2FS_CHECK_FS
359850cf4aSJaegeuk Kim #define f2fs_bug_on(sbi, condition)	BUG_ON(condition)
365d56b671SJaegeuk Kim #else
379850cf4aSJaegeuk Kim #define f2fs_bug_on(sbi, condition)					\
389850cf4aSJaegeuk Kim 	do {								\
39db489652SYangtao Li 		if (WARN_ON(condition))					\
40caf0047eSChao Yu 			set_sbi_flag(sbi, SBI_NEED_FSCK);		\
419850cf4aSJaegeuk Kim 	} while (0)
425d56b671SJaegeuk Kim #endif
435d56b671SJaegeuk Kim 
442c63feadSJaegeuk Kim enum {
452c63feadSJaegeuk Kim 	FAULT_KMALLOC,
46628b3d14SChao Yu 	FAULT_KVMALLOC,
47c41f3cc3SJaegeuk Kim 	FAULT_PAGE_ALLOC,
4801eccef7SChao Yu 	FAULT_PAGE_GET,
49b96d9b3bSChao Yu 	FAULT_ALLOC_BIO,	/* it's obsolete due to bio_alloc() will never fail */
50cb78942bSJaegeuk Kim 	FAULT_ALLOC_NID,
51cb78942bSJaegeuk Kim 	FAULT_ORPHAN,
52cb78942bSJaegeuk Kim 	FAULT_BLOCK,
53cb78942bSJaegeuk Kim 	FAULT_DIR_DEPTH,
5453aa6bbfSJaegeuk Kim 	FAULT_EVICT_INODE,
5514b44d23SJaegeuk Kim 	FAULT_TRUNCATE,
566f5c2ed0SChao Yu 	FAULT_READ_IO,
570f348028SChao Yu 	FAULT_CHECKPOINT,
58b83dcfe6SChao Yu 	FAULT_DISCARD,
596f5c2ed0SChao Yu 	FAULT_WRITE_IO,
6032410577SChao Yu 	FAULT_SLAB_ALLOC,
6110a26878SChao Yu 	FAULT_DQUOT_INIT,
623e020389SChao Yu 	FAULT_LOCK_OP,
632c63feadSJaegeuk Kim 	FAULT_MAX,
642c63feadSJaegeuk Kim };
652c63feadSJaegeuk Kim 
667fa750a1SArnd Bergmann #ifdef CONFIG_F2FS_FAULT_INJECTION
67d494500aSChao Yu #define F2FS_ALL_FAULT_TYPE		((1 << FAULT_MAX) - 1)
68d494500aSChao Yu 
6908796897SSheng Yong struct f2fs_fault_info {
7008796897SSheng Yong 	atomic_t inject_ops;
7108796897SSheng Yong 	unsigned int inject_rate;
7208796897SSheng Yong 	unsigned int inject_type;
7308796897SSheng Yong };
7408796897SSheng Yong 
7519880e6eSAlexey Dobriyan extern const char *f2fs_fault_name[FAULT_MAX];
7668afcf2dSTomohiro Kusumi #define IS_FAULT_SET(fi, type) ((fi)->inject_type & (1 << (type)))
772c63feadSJaegeuk Kim #endif
782c63feadSJaegeuk Kim 
7939a53e0cSJaegeuk Kim /*
8039a53e0cSJaegeuk Kim  * For mount options
8139a53e0cSJaegeuk Kim  */
8239a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_ROLL_FORWARD	0x00000002
8339a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISCARD		0x00000004
8439a53e0cSJaegeuk Kim #define F2FS_MOUNT_NOHEAP		0x00000008
8539a53e0cSJaegeuk Kim #define F2FS_MOUNT_XATTR_USER		0x00000010
8639a53e0cSJaegeuk Kim #define F2FS_MOUNT_POSIX_ACL		0x00000020
8739a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_EXT_IDENTIFY	0x00000040
88444c580fSJaegeuk Kim #define F2FS_MOUNT_INLINE_XATTR		0x00000080
891001b347SHuajun Li #define F2FS_MOUNT_INLINE_DATA		0x00000100
9034d67debSChao Yu #define F2FS_MOUNT_INLINE_DENTRY	0x00000200
9134d67debSChao Yu #define F2FS_MOUNT_FLUSH_MERGE		0x00000400
9234d67debSChao Yu #define F2FS_MOUNT_NOBARRIER		0x00000800
93d5053a34SJaegeuk Kim #define F2FS_MOUNT_FASTBOOT		0x00001000
9489672159SChao Yu #define F2FS_MOUNT_EXTENT_CACHE		0x00002000
95343f40f0SChao Yu #define F2FS_MOUNT_DATA_FLUSH		0x00008000
9673faec4dSJaegeuk Kim #define F2FS_MOUNT_FAULT_INJECTION	0x00010000
970abd675eSChao Yu #define F2FS_MOUNT_USRQUOTA		0x00080000
980abd675eSChao Yu #define F2FS_MOUNT_GRPQUOTA		0x00100000
995c57132eSChao Yu #define F2FS_MOUNT_PRJQUOTA		0x00200000
1004b2414d0SChao Yu #define F2FS_MOUNT_QUOTA		0x00400000
1016afc662eSChao Yu #define F2FS_MOUNT_INLINE_XATTR_SIZE	0x00800000
1027e65be49SJaegeuk Kim #define F2FS_MOUNT_RESERVE_ROOT		0x01000000
1034354994fSDaniel Rosenberg #define F2FS_MOUNT_DISABLE_CHECKPOINT	0x02000000
104a9117ecaSChao Yu #define F2FS_MOUNT_NORECOVERY		0x04000000
105093749e2SChao Yu #define F2FS_MOUNT_ATGC			0x08000000
106261eeb9cSDaeho Jeong #define F2FS_MOUNT_MERGE_CHECKPOINT	0x10000000
1075911d2d1SChao Yu #define	F2FS_MOUNT_GC_MERGE		0x20000000
1086ce19affSChao Yu #define F2FS_MOUNT_COMPRESS_CACHE	0x40000000
10939a53e0cSJaegeuk Kim 
11063189b78SChao Yu #define F2FS_OPTION(sbi)	((sbi)->mount_opt)
11163189b78SChao Yu #define clear_opt(sbi, option)	(F2FS_OPTION(sbi).opt &= ~F2FS_MOUNT_##option)
11263189b78SChao Yu #define set_opt(sbi, option)	(F2FS_OPTION(sbi).opt |= F2FS_MOUNT_##option)
11363189b78SChao Yu #define test_opt(sbi, option)	(F2FS_OPTION(sbi).opt & F2FS_MOUNT_##option)
11439a53e0cSJaegeuk Kim 
11539a53e0cSJaegeuk Kim #define ver_after(a, b)	(typecheck(unsigned long long, a) &&		\
11639a53e0cSJaegeuk Kim 		typecheck(unsigned long long, b) &&			\
11739a53e0cSJaegeuk Kim 		((long long)((a) - (b)) > 0))
11839a53e0cSJaegeuk Kim 
119a9841c4dSJaegeuk Kim typedef u32 block_t;	/*
120a9841c4dSJaegeuk Kim 			 * should not change u32, since it is the on-disk block
121a9841c4dSJaegeuk Kim 			 * address format, __le32.
122a9841c4dSJaegeuk Kim 			 */
12339a53e0cSJaegeuk Kim typedef u32 nid_t;
12439a53e0cSJaegeuk Kim 
1254c8ff709SChao Yu #define COMPRESS_EXT_NUM		16
1264c8ff709SChao Yu 
127e4544b63STim Murray /*
128e4544b63STim Murray  * An implementation of an rwsem that is explicitly unfair to readers. This
129e4544b63STim Murray  * prevents priority inversion when a low-priority reader acquires the read lock
130e4544b63STim Murray  * while sleeping on the write lock but the write lock is needed by
131e4544b63STim Murray  * higher-priority clients.
132e4544b63STim Murray  */
133e4544b63STim Murray 
134e4544b63STim Murray struct f2fs_rwsem {
135e4544b63STim Murray         struct rw_semaphore internal_rwsem;
1367f8e249dSJaegeuk Kim #ifdef CONFIG_F2FS_UNFAIR_RWSEM
137e4544b63STim Murray         wait_queue_head_t read_waiters;
1387f8e249dSJaegeuk Kim #endif
139e4544b63STim Murray };
140e4544b63STim Murray 
14139a53e0cSJaegeuk Kim struct f2fs_mount_info {
14239a53e0cSJaegeuk Kim 	unsigned int opt;
14363189b78SChao Yu 	int write_io_size_bits;		/* Write IO size bits */
14463189b78SChao Yu 	block_t root_reserved_blocks;	/* root reserved blocks */
14563189b78SChao Yu 	kuid_t s_resuid;		/* reserved blocks for uid */
14663189b78SChao Yu 	kgid_t s_resgid;		/* reserved blocks for gid */
14763189b78SChao Yu 	int active_logs;		/* # of active logs */
14863189b78SChao Yu 	int inline_xattr_size;		/* inline xattr size */
14963189b78SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
15063189b78SChao Yu 	struct f2fs_fault_info fault_info;	/* For fault injection */
15163189b78SChao Yu #endif
15263189b78SChao Yu #ifdef CONFIG_QUOTA
15363189b78SChao Yu 	/* Names of quota files with journalled quota */
15463189b78SChao Yu 	char *s_qf_names[MAXQUOTAS];
15563189b78SChao Yu 	int s_jquota_fmt;			/* Format of quota to use */
15663189b78SChao Yu #endif
15763189b78SChao Yu 	/* For which write hints are passed down to block layer */
15863189b78SChao Yu 	int alloc_mode;			/* segment allocation policy */
15963189b78SChao Yu 	int fsync_mode;			/* fsync policy */
160b0332a0fSChao Yu 	int fs_mode;			/* fs mode: LFS or ADAPTIVE */
161bbbc34fdSChao Yu 	int bggc_mode;			/* bggc mode: off, on or sync */
1627a8fc586SDaeho Jeong 	int memory_mode;		/* memory mode */
1634f993264SChao Yu 	int discard_unit;		/*
1644f993264SChao Yu 					 * discard command's offset/size should
1654f993264SChao Yu 					 * be aligned to this unit: block,
1664f993264SChao Yu 					 * segment or section
1674f993264SChao Yu 					 */
168ac4acb1fSEric Biggers 	struct fscrypt_dummy_policy dummy_enc_policy; /* test dummy encryption */
1691ae18f71SJaegeuk Kim 	block_t unusable_cap_perc;	/* percentage for cap */
1704d3aed70SDaniel Rosenberg 	block_t unusable_cap;		/* Amount of space allowed to be
1714d3aed70SDaniel Rosenberg 					 * unusable when disabling checkpoint
1724d3aed70SDaniel Rosenberg 					 */
1734c8ff709SChao Yu 
1744c8ff709SChao Yu 	/* For compression */
1754c8ff709SChao Yu 	unsigned char compress_algorithm;	/* algorithm type */
176b28f047bSChao Yu 	unsigned char compress_log_size;	/* cluster log size */
1773fde13f8SChao Yu 	unsigned char compress_level;		/* compress level */
178b28f047bSChao Yu 	bool compress_chksum;			/* compressed data chksum */
1794c8ff709SChao Yu 	unsigned char compress_ext_cnt;		/* extension count */
180151b1982SFengnan Chang 	unsigned char nocompress_ext_cnt;		/* nocompress extension count */
181602a16d5SDaeho Jeong 	int compress_mode;			/* compression mode */
1824c8ff709SChao Yu 	unsigned char extensions[COMPRESS_EXT_NUM][F2FS_EXTENSION_LEN];	/* extensions */
183151b1982SFengnan Chang 	unsigned char noextensions[COMPRESS_EXT_NUM][F2FS_EXTENSION_LEN]; /* extensions */
18439a53e0cSJaegeuk Kim };
18539a53e0cSJaegeuk Kim 
186cde4de12SJaegeuk Kim #define F2FS_FEATURE_ENCRYPT		0x0001
1870bfd7a09SDamien Le Moal #define F2FS_FEATURE_BLKZONED		0x0002
188e65ef207SJaegeuk Kim #define F2FS_FEATURE_ATOMIC_WRITE	0x0004
1897a2af766SChao Yu #define F2FS_FEATURE_EXTRA_ATTR		0x0008
1905c57132eSChao Yu #define F2FS_FEATURE_PRJQUOTA		0x0010
191704956ecSChao Yu #define F2FS_FEATURE_INODE_CHKSUM	0x0020
1926afc662eSChao Yu #define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR	0x0040
193234a9689SJaegeuk Kim #define F2FS_FEATURE_QUOTA_INO		0x0080
1941c1d35dfSChao Yu #define F2FS_FEATURE_INODE_CRTIME	0x0100
195b7c409deSSheng Yong #define F2FS_FEATURE_LOST_FOUND		0x0200
19695ae251fSEric Biggers #define F2FS_FEATURE_VERITY		0x0400
197d440c52dSJunling Zheng #define F2FS_FEATURE_SB_CHKSUM		0x0800
1985aba5430SDaniel Rosenberg #define F2FS_FEATURE_CASEFOLD		0x1000
1994c8ff709SChao Yu #define F2FS_FEATURE_COMPRESSION	0x2000
200a7d9fe3cSJaegeuk Kim #define F2FS_FEATURE_RO			0x4000
201cde4de12SJaegeuk Kim 
2027beb01f7SChao Yu #define __F2FS_HAS_FEATURE(raw_super, mask)				\
2037beb01f7SChao Yu 	((raw_super->feature & cpu_to_le32(mask)) != 0)
2047beb01f7SChao Yu #define F2FS_HAS_FEATURE(sbi, mask)	__F2FS_HAS_FEATURE(sbi->raw_super, mask)
2057beb01f7SChao Yu #define F2FS_SET_FEATURE(sbi, mask)					\
2067beb01f7SChao Yu 	(sbi->raw_super->feature |= cpu_to_le32(mask))
2077beb01f7SChao Yu #define F2FS_CLEAR_FEATURE(sbi, mask)					\
2087beb01f7SChao Yu 	(sbi->raw_super->feature &= ~cpu_to_le32(mask))
20976f105a2SJaegeuk Kim 
21039a53e0cSJaegeuk Kim /*
2117c2e5963SJaegeuk Kim  * Default values for user and/or group using reserved blocks
2127c2e5963SJaegeuk Kim  */
2137c2e5963SJaegeuk Kim #define	F2FS_DEF_RESUID		0
2147c2e5963SJaegeuk Kim #define	F2FS_DEF_RESGID		0
2157c2e5963SJaegeuk Kim 
2167c2e5963SJaegeuk Kim /*
21739a53e0cSJaegeuk Kim  * For checkpoint manager
21839a53e0cSJaegeuk Kim  */
21939a53e0cSJaegeuk Kim enum {
22039a53e0cSJaegeuk Kim 	NAT_BITMAP,
22139a53e0cSJaegeuk Kim 	SIT_BITMAP
22239a53e0cSJaegeuk Kim };
22339a53e0cSJaegeuk Kim 
224c473f1a9SChao Yu #define	CP_UMOUNT	0x00000001
225c473f1a9SChao Yu #define	CP_FASTBOOT	0x00000002
226c473f1a9SChao Yu #define	CP_SYNC		0x00000004
227c473f1a9SChao Yu #define	CP_RECOVERY	0x00000008
228c473f1a9SChao Yu #define	CP_DISCARD	0x00000010
2291f43e2adSChao Yu #define CP_TRIMMED	0x00000020
2304354994fSDaniel Rosenberg #define CP_PAUSE	0x00000040
231b4b10061SJaegeuk Kim #define CP_RESIZE 	0x00000080
23275ab4cb8SJaegeuk Kim 
233ecc9aa00SChao Yu #define DEF_MAX_DISCARD_REQUEST		8	/* issue 8 discards per round */
234969d1b18SChao Yu #define DEF_MIN_DISCARD_ISSUE_TIME	50	/* 50 ms, if exists */
235f9d1dcedSYunlei He #define DEF_MID_DISCARD_ISSUE_TIME	500	/* 500 ms, if device busy */
236969d1b18SChao Yu #define DEF_MAX_DISCARD_ISSUE_TIME	60000	/* 60 s, if no candidates */
2378bb4f253SJaegeuk Kim #define DEF_DISCARD_URGENT_UTIL		80	/* do more discard over 80% */
23860b99b48SJaegeuk Kim #define DEF_CP_INTERVAL			60	/* 60 secs */
239dcf25fe8SChao Yu #define DEF_IDLE_INTERVAL		5	/* 5 secs */
2404354994fSDaniel Rosenberg #define DEF_DISABLE_INTERVAL		5	/* 5 secs */
241db610a64SJaegeuk Kim #define DEF_DISABLE_QUICK_INTERVAL	1	/* 1 secs */
24203f2c02dSJaegeuk Kim #define DEF_UMOUNT_DISCARD_TIMEOUT	5	/* 5 secs */
243bba681cbSJaegeuk Kim 
24475ab4cb8SJaegeuk Kim struct cp_control {
24575ab4cb8SJaegeuk Kim 	int reason;
2464b2fecc8SJaegeuk Kim 	__u64 trim_start;
2474b2fecc8SJaegeuk Kim 	__u64 trim_end;
2484b2fecc8SJaegeuk Kim 	__u64 trim_minlen;
24975ab4cb8SJaegeuk Kim };
25075ab4cb8SJaegeuk Kim 
251662befdaSChao Yu /*
252e1da7872SChao Yu  * indicate meta/data type
253662befdaSChao Yu  */
254662befdaSChao Yu enum {
255662befdaSChao Yu 	META_CP,
256662befdaSChao Yu 	META_NAT,
25781c1a0f1SChao Yu 	META_SIT,
2584c521f49SJaegeuk Kim 	META_SSA,
259b63e7be5SChao Yu 	META_MAX,
2604c521f49SJaegeuk Kim 	META_POR,
26193770ab7SChao Yu 	DATA_GENERIC,		/* check range only */
26293770ab7SChao Yu 	DATA_GENERIC_ENHANCE,	/* strong check on range and segment bitmap */
26393770ab7SChao Yu 	DATA_GENERIC_ENHANCE_READ,	/*
26493770ab7SChao Yu 					 * strong check on range and segment
26593770ab7SChao Yu 					 * bitmap but no warning due to race
26693770ab7SChao Yu 					 * condition of read on truncated area
26793770ab7SChao Yu 					 * by extent_cache
26893770ab7SChao Yu 					 */
2690ef4ca04SChao Yu 	DATA_GENERIC_ENHANCE_UPDATE,	/*
2700ef4ca04SChao Yu 					 * strong check on range and segment
2710ef4ca04SChao Yu 					 * bitmap for update case
2720ef4ca04SChao Yu 					 */
273e1da7872SChao Yu 	META_GENERIC,
274662befdaSChao Yu };
275662befdaSChao Yu 
2766451e041SJaegeuk Kim /* for the list of ino */
2776451e041SJaegeuk Kim enum {
2786451e041SJaegeuk Kim 	ORPHAN_INO,		/* for orphan ino list */
279fff04f90SJaegeuk Kim 	APPEND_INO,		/* for append ino list */
280fff04f90SJaegeuk Kim 	UPDATE_INO,		/* for update ino list */
281d382e369SYonggil Song 	TRANS_DIR_INO,		/* for transactions dir ino list */
28239d787beSChao Yu 	FLUSH_INO,		/* for multiple device flushing */
2836451e041SJaegeuk Kim 	MAX_INO_ENTRY,		/* max. list */
2846451e041SJaegeuk Kim };
2856451e041SJaegeuk Kim 
2866451e041SJaegeuk Kim struct ino_entry {
28739a53e0cSJaegeuk Kim 	struct list_head list;		/* list head */
28839a53e0cSJaegeuk Kim 	nid_t ino;			/* inode number */
28939d787beSChao Yu 	unsigned int dirty_device;	/* dirty device bitmap */
29039a53e0cSJaegeuk Kim };
29139a53e0cSJaegeuk Kim 
2922710fd7eSChao Yu /* for the list of inodes to be GCed */
29306292073SChao Yu struct inode_entry {
29439a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
29539a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
29639a53e0cSJaegeuk Kim };
29739a53e0cSJaegeuk Kim 
29850fa53ecSChao Yu struct fsync_node_entry {
29950fa53ecSChao Yu 	struct list_head list;	/* list head */
30050fa53ecSChao Yu 	struct page *page;	/* warm node page pointer */
30150fa53ecSChao Yu 	unsigned int seq_id;	/* sequence id */
30250fa53ecSChao Yu };
30350fa53ecSChao Yu 
304261eeb9cSDaeho Jeong struct ckpt_req {
305261eeb9cSDaeho Jeong 	struct completion wait;		/* completion for checkpoint done */
306261eeb9cSDaeho Jeong 	struct llist_node llnode;	/* llist_node to be linked in wait queue */
307261eeb9cSDaeho Jeong 	int ret;			/* return code of checkpoint */
308261eeb9cSDaeho Jeong 	ktime_t queue_time;		/* request queued time */
309261eeb9cSDaeho Jeong };
310261eeb9cSDaeho Jeong 
311261eeb9cSDaeho Jeong struct ckpt_req_control {
312261eeb9cSDaeho Jeong 	struct task_struct *f2fs_issue_ckpt;	/* checkpoint task */
313e6592066SDaeho Jeong 	int ckpt_thread_ioprio;			/* checkpoint merge thread ioprio */
314261eeb9cSDaeho Jeong 	wait_queue_head_t ckpt_wait_queue;	/* waiting queue for wake-up */
315261eeb9cSDaeho Jeong 	atomic_t issued_ckpt;		/* # of actually issued ckpts */
316261eeb9cSDaeho Jeong 	atomic_t total_ckpt;		/* # of total ckpts */
317261eeb9cSDaeho Jeong 	atomic_t queued_ckpt;		/* # of queued ckpts */
318261eeb9cSDaeho Jeong 	struct llist_head issue_list;	/* list for command issue */
319261eeb9cSDaeho Jeong 	spinlock_t stat_lock;		/* lock for below checkpoint time stats */
320261eeb9cSDaeho Jeong 	unsigned int cur_time;		/* cur wait time in msec for currently issued checkpoint */
321261eeb9cSDaeho Jeong 	unsigned int peak_time;		/* peak wait time in msec until now */
322261eeb9cSDaeho Jeong };
323261eeb9cSDaeho Jeong 
324a7eeb823SChao Yu /* for the bitmap indicate blocks to be discarded */
3257fd9e544SJaegeuk Kim struct discard_entry {
3267fd9e544SJaegeuk Kim 	struct list_head list;	/* list head */
327a7eeb823SChao Yu 	block_t start_blkaddr;	/* start blockaddr of current segment */
328a7eeb823SChao Yu 	unsigned char discard_map[SIT_VBLOCK_MAP_SIZE];	/* segment discard bitmap */
3297fd9e544SJaegeuk Kim };
3307fd9e544SJaegeuk Kim 
331969d1b18SChao Yu /* default discard granularity of inner discard thread, unit: block count */
332969d1b18SChao Yu #define DEFAULT_DISCARD_GRANULARITY		16
333969d1b18SChao Yu 
334ba48a33eSChao Yu /* max discard pend list number */
335ba48a33eSChao Yu #define MAX_PLIST_NUM		512
336ba48a33eSChao Yu #define plist_idx(blk_num)	((blk_num) >= MAX_PLIST_NUM ?		\
3372f84babfSSheng Yong 					(MAX_PLIST_NUM - 1) : ((blk_num) - 1))
338ba48a33eSChao Yu 
33915469963SJaegeuk Kim enum {
34035ec7d57SChao Yu 	D_PREP,			/* initial */
34135ec7d57SChao Yu 	D_PARTIAL,		/* partially submitted */
34235ec7d57SChao Yu 	D_SUBMIT,		/* all submitted */
34335ec7d57SChao Yu 	D_DONE,			/* finished */
34415469963SJaegeuk Kim };
34515469963SJaegeuk Kim 
346004b6862SChao Yu struct discard_info {
347b01a9201SJaegeuk Kim 	block_t lstart;			/* logical start address */
348b01a9201SJaegeuk Kim 	block_t len;			/* length */
349004b6862SChao Yu 	block_t start;			/* actual start address in dev */
350004b6862SChao Yu };
351004b6862SChao Yu 
352b01a9201SJaegeuk Kim struct discard_cmd {
353004b6862SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
354004b6862SChao Yu 	union {
355004b6862SChao Yu 		struct {
356004b6862SChao Yu 			block_t lstart;	/* logical start address */
357004b6862SChao Yu 			block_t len;	/* length */
358004b6862SChao Yu 			block_t start;	/* actual start address in dev */
359004b6862SChao Yu 		};
360004b6862SChao Yu 		struct discard_info di;	/* discard info */
361004b6862SChao Yu 
362004b6862SChao Yu 	};
363b01a9201SJaegeuk Kim 	struct list_head list;		/* command list */
364b01a9201SJaegeuk Kim 	struct completion wait;		/* compleation */
365c81abe34SJaegeuk Kim 	struct block_device *bdev;	/* bdev */
366ec9895adSChao Yu 	unsigned short ref;		/* reference count */
3679a744b92SChao Yu 	unsigned char state;		/* state */
36872691af6SJaegeuk Kim 	unsigned char queued;		/* queued discard */
369c81abe34SJaegeuk Kim 	int error;			/* bio error */
37035ec7d57SChao Yu 	spinlock_t lock;		/* for state/bio_ref updating */
37135ec7d57SChao Yu 	unsigned short bio_ref;		/* bio reference count */
372275b66b0SChao Yu };
373275b66b0SChao Yu 
37478997b56SChao Yu enum {
37578997b56SChao Yu 	DPOLICY_BG,
37678997b56SChao Yu 	DPOLICY_FORCE,
37778997b56SChao Yu 	DPOLICY_FSTRIM,
37878997b56SChao Yu 	DPOLICY_UMOUNT,
37978997b56SChao Yu 	MAX_DPOLICY,
38078997b56SChao Yu };
38178997b56SChao Yu 
382ecc9aa00SChao Yu struct discard_policy {
38378997b56SChao Yu 	int type;			/* type of discard */
384ecc9aa00SChao Yu 	unsigned int min_interval;	/* used for candidates exist */
385f9d1dcedSYunlei He 	unsigned int mid_interval;	/* used for device busy */
386ecc9aa00SChao Yu 	unsigned int max_interval;	/* used for candidates not exist */
387ecc9aa00SChao Yu 	unsigned int max_requests;	/* # of discards issued per round */
388ecc9aa00SChao Yu 	unsigned int io_aware_gran;	/* minimum granularity discard not be aware of I/O */
389ecc9aa00SChao Yu 	bool io_aware;			/* issue discard in idle time */
390ecc9aa00SChao Yu 	bool sync;			/* submit discard with REQ_SYNC flag */
39120ee4382SChao Yu 	bool ordered;			/* issue discard by lba order */
3926ce48b0cSChao Yu 	bool timeout;			/* discard timeout for put_super */
39378997b56SChao Yu 	unsigned int granularity;	/* discard granularity */
394ecc9aa00SChao Yu };
395ecc9aa00SChao Yu 
3960b54fb84SJaegeuk Kim struct discard_cmd_control {
39715469963SJaegeuk Kim 	struct task_struct *f2fs_issue_discard;	/* discard thread */
39846f84c2cSChao Yu 	struct list_head entry_list;		/* 4KB discard entry list */
399ba48a33eSChao Yu 	struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
40046f84c2cSChao Yu 	struct list_head wait_list;		/* store on-flushing entries */
4018412663dSChao Yu 	struct list_head fstrim_list;		/* in-flight discard from fstrim */
40215469963SJaegeuk Kim 	wait_queue_head_t discard_wait_queue;	/* waiting queue for wake-up */
403969d1b18SChao Yu 	unsigned int discard_wake;		/* to wake up discard thread */
40415469963SJaegeuk Kim 	struct mutex cmd_lock;
405d618ebafSChao Yu 	unsigned int nr_discards;		/* # of discards in the list */
406d618ebafSChao Yu 	unsigned int max_discards;		/* max. discards to be issued */
407d2d8e896SKonstantin Vyshetsky 	unsigned int max_discard_request;	/* max. discard request per round */
408d2d8e896SKonstantin Vyshetsky 	unsigned int min_discard_issue_time;	/* min. interval between discard issue */
409d2d8e896SKonstantin Vyshetsky 	unsigned int mid_discard_issue_time;	/* mid. interval between discard issue */
410d2d8e896SKonstantin Vyshetsky 	unsigned int max_discard_issue_time;	/* max. interval between discard issue */
411969d1b18SChao Yu 	unsigned int discard_granularity;	/* discard granularity */
412d84d1cbdSChao Yu 	unsigned int undiscard_blks;		/* # of undiscard blocks */
41320ee4382SChao Yu 	unsigned int next_pos;			/* next discard position */
4148b8dd65fSChao Yu 	atomic_t issued_discard;		/* # of issued discard */
41572691af6SJaegeuk Kim 	atomic_t queued_discard;		/* # of queued discard */
4165f32366aSChao Yu 	atomic_t discard_cmd_cnt;		/* # of cached cmd count */
4174dada3fdSChao Yu 	struct rb_root_cached root;		/* root of discard rb-tree */
41867fce70bSChao Yu 	bool rbtree_check;			/* config for consistence check */
41939a53e0cSJaegeuk Kim };
42039a53e0cSJaegeuk Kim 
42139a53e0cSJaegeuk Kim /* for the list of fsync inodes, used only during recovery */
42239a53e0cSJaegeuk Kim struct fsync_inode_entry {
42339a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
42439a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
425c52e1b10SJaegeuk Kim 	block_t blkaddr;	/* block address locating the last fsync */
426c52e1b10SJaegeuk Kim 	block_t last_dentry;	/* block address locating the last dentry */
42739a53e0cSJaegeuk Kim };
42839a53e0cSJaegeuk Kim 
42968afcf2dSTomohiro Kusumi #define nats_in_cursum(jnl)		(le16_to_cpu((jnl)->n_nats))
43068afcf2dSTomohiro Kusumi #define sits_in_cursum(jnl)		(le16_to_cpu((jnl)->n_sits))
43139a53e0cSJaegeuk Kim 
43268afcf2dSTomohiro Kusumi #define nat_in_journal(jnl, i)		((jnl)->nat_j.entries[i].ne)
43368afcf2dSTomohiro Kusumi #define nid_in_journal(jnl, i)		((jnl)->nat_j.entries[i].nid)
43468afcf2dSTomohiro Kusumi #define sit_in_journal(jnl, i)		((jnl)->sit_j.entries[i].se)
43568afcf2dSTomohiro Kusumi #define segno_in_journal(jnl, i)	((jnl)->sit_j.entries[i].segno)
43639a53e0cSJaegeuk Kim 
437dfc08a12SChao Yu #define MAX_NAT_JENTRIES(jnl)	(NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
438dfc08a12SChao Yu #define MAX_SIT_JENTRIES(jnl)	(SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
439309cc2b6SJaegeuk Kim 
440dfc08a12SChao Yu static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
44139a53e0cSJaegeuk Kim {
442dfc08a12SChao Yu 	int before = nats_in_cursum(journal);
443cac5a3d8SDongOh Shin 
444dfc08a12SChao Yu 	journal->n_nats = cpu_to_le16(before + i);
44539a53e0cSJaegeuk Kim 	return before;
44639a53e0cSJaegeuk Kim }
44739a53e0cSJaegeuk Kim 
448dfc08a12SChao Yu static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
44939a53e0cSJaegeuk Kim {
450dfc08a12SChao Yu 	int before = sits_in_cursum(journal);
451cac5a3d8SDongOh Shin 
452dfc08a12SChao Yu 	journal->n_sits = cpu_to_le16(before + i);
45339a53e0cSJaegeuk Kim 	return before;
45439a53e0cSJaegeuk Kim }
45539a53e0cSJaegeuk Kim 
456dfc08a12SChao Yu static inline bool __has_cursum_space(struct f2fs_journal *journal,
457dfc08a12SChao Yu 							int size, int type)
458184a5cd2SChao Yu {
459184a5cd2SChao Yu 	if (type == NAT_JOURNAL)
460dfc08a12SChao Yu 		return size <= MAX_NAT_JENTRIES(journal);
461dfc08a12SChao Yu 	return size <= MAX_SIT_JENTRIES(journal);
462184a5cd2SChao Yu }
463184a5cd2SChao Yu 
464f2470371SChao Yu /* for inline stuff */
465f2470371SChao Yu #define DEF_INLINE_RESERVED_SIZE	1
4667a2af766SChao Yu static inline int get_extra_isize(struct inode *inode);
4676afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode);
4687a2af766SChao Yu #define MAX_INLINE_DATA(inode)	(sizeof(__le32) *			\
4697a2af766SChao Yu 				(CUR_ADDRS_PER_INODE(inode) -		\
470b323fd28SChao Yu 				get_inline_xattr_addrs(inode) -	\
4716afc662eSChao Yu 				DEF_INLINE_RESERVED_SIZE))
472f2470371SChao Yu 
473f2470371SChao Yu /* for inline dir */
474f2470371SChao Yu #define NR_INLINE_DENTRY(inode)	(MAX_INLINE_DATA(inode) * BITS_PER_BYTE / \
475f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
476f2470371SChao Yu 				BITS_PER_BYTE + 1))
477f91108b8SGeert Uytterhoeven #define INLINE_DENTRY_BITMAP_SIZE(inode) \
478f91108b8SGeert Uytterhoeven 	DIV_ROUND_UP(NR_INLINE_DENTRY(inode), BITS_PER_BYTE)
479f2470371SChao Yu #define INLINE_RESERVED_SIZE(inode)	(MAX_INLINE_DATA(inode) - \
480f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
481f2470371SChao Yu 				NR_INLINE_DENTRY(inode) + \
482f2470371SChao Yu 				INLINE_DENTRY_BITMAP_SIZE(inode)))
483f2470371SChao Yu 
484e9750824SNamjae Jeon /*
48539a53e0cSJaegeuk Kim  * For INODE and NODE manager
48639a53e0cSJaegeuk Kim  */
4877b3cd7d6SJaegeuk Kim /* for directory operations */
48843c780baSEric Biggers 
48943c780baSEric Biggers struct f2fs_filename {
49043c780baSEric Biggers 	/*
49143c780baSEric Biggers 	 * The filename the user specified.  This is NULL for some
49243c780baSEric Biggers 	 * filesystem-internal operations, e.g. converting an inline directory
49343c780baSEric Biggers 	 * to a non-inline one, or roll-forward recovering an encrypted dentry.
49443c780baSEric Biggers 	 */
49543c780baSEric Biggers 	const struct qstr *usr_fname;
49643c780baSEric Biggers 
49743c780baSEric Biggers 	/*
49843c780baSEric Biggers 	 * The on-disk filename.  For encrypted directories, this is encrypted.
49943c780baSEric Biggers 	 * This may be NULL for lookups in an encrypted dir without the key.
50043c780baSEric Biggers 	 */
50143c780baSEric Biggers 	struct fscrypt_str disk_name;
50243c780baSEric Biggers 
50343c780baSEric Biggers 	/* The dirhash of this filename */
50443c780baSEric Biggers 	f2fs_hash_t hash;
50543c780baSEric Biggers 
50643c780baSEric Biggers #ifdef CONFIG_FS_ENCRYPTION
50743c780baSEric Biggers 	/*
50843c780baSEric Biggers 	 * For lookups in encrypted directories: either the buffer backing
50943c780baSEric Biggers 	 * disk_name, or a buffer that holds the decoded no-key name.
51043c780baSEric Biggers 	 */
51143c780baSEric Biggers 	struct fscrypt_str crypto_buf;
51243c780baSEric Biggers #endif
5135298d4bfSChristoph Hellwig #if IS_ENABLED(CONFIG_UNICODE)
51443c780baSEric Biggers 	/*
51543c780baSEric Biggers 	 * For casefolded directories: the casefolded name, but it's left NULL
516b5639bb4SEric Biggers 	 * if the original name is not valid Unicode, if the original name is
517b5639bb4SEric Biggers 	 * "." or "..", if the directory is both casefolded and encrypted and
518b5639bb4SEric Biggers 	 * its encryption key is unavailable, or if the filesystem is doing an
519b5639bb4SEric Biggers 	 * internal operation where usr_fname is also NULL.  In all these cases
520b5639bb4SEric Biggers 	 * we fall back to treating the name as an opaque byte sequence.
52143c780baSEric Biggers 	 */
52243c780baSEric Biggers 	struct fscrypt_str cf_name;
52343c780baSEric Biggers #endif
52443c780baSEric Biggers };
52543c780baSEric Biggers 
5267b3cd7d6SJaegeuk Kim struct f2fs_dentry_ptr {
527d8c6822aSJaegeuk Kim 	struct inode *inode;
52876a9dd85SChao Yu 	void *bitmap;
5297b3cd7d6SJaegeuk Kim 	struct f2fs_dir_entry *dentry;
5307b3cd7d6SJaegeuk Kim 	__u8 (*filename)[F2FS_SLOT_LEN];
5317b3cd7d6SJaegeuk Kim 	int max;
53276a9dd85SChao Yu 	int nr_bitmap;
5337b3cd7d6SJaegeuk Kim };
5347b3cd7d6SJaegeuk Kim 
53564c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_block(struct inode *inode,
53664c24ecbSTomohiro Kusumi 		struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
5377b3cd7d6SJaegeuk Kim {
538d8c6822aSJaegeuk Kim 	d->inode = inode;
5397b3cd7d6SJaegeuk Kim 	d->max = NR_DENTRY_IN_BLOCK;
54076a9dd85SChao Yu 	d->nr_bitmap = SIZE_OF_DENTRY_BITMAP;
541c79d1520SYunlong Song 	d->bitmap = t->dentry_bitmap;
5427b3cd7d6SJaegeuk Kim 	d->dentry = t->dentry;
5437b3cd7d6SJaegeuk Kim 	d->filename = t->filename;
54464c24ecbSTomohiro Kusumi }
545cac5a3d8SDongOh Shin 
54664c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_inline(struct inode *inode,
547f2470371SChao Yu 					struct f2fs_dentry_ptr *d, void *t)
54864c24ecbSTomohiro Kusumi {
549f2470371SChao Yu 	int entry_cnt = NR_INLINE_DENTRY(inode);
550f2470371SChao Yu 	int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(inode);
551f2470371SChao Yu 	int reserved_size = INLINE_RESERVED_SIZE(inode);
552f2470371SChao Yu 
55364c24ecbSTomohiro Kusumi 	d->inode = inode;
554f2470371SChao Yu 	d->max = entry_cnt;
555f2470371SChao Yu 	d->nr_bitmap = bitmap_size;
556f2470371SChao Yu 	d->bitmap = t;
557f2470371SChao Yu 	d->dentry = t + bitmap_size + reserved_size;
558f2470371SChao Yu 	d->filename = t + bitmap_size + reserved_size +
559f2470371SChao Yu 					SIZE_OF_DIR_ENTRY * entry_cnt;
5607b3cd7d6SJaegeuk Kim }
5617b3cd7d6SJaegeuk Kim 
562dbe6a5ffSJaegeuk Kim /*
563dbe6a5ffSJaegeuk Kim  * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
564dbe6a5ffSJaegeuk Kim  * as its node offset to distinguish from index node blocks.
565dbe6a5ffSJaegeuk Kim  * But some bits are used to mark the node block.
56639a53e0cSJaegeuk Kim  */
567dbe6a5ffSJaegeuk Kim #define XATTR_NODE_OFFSET	((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
568dbe6a5ffSJaegeuk Kim 				>> OFFSET_BIT_SHIFT)
569266e97a8SJaegeuk Kim enum {
570266e97a8SJaegeuk Kim 	ALLOC_NODE,			/* allocate a new node page if needed */
571266e97a8SJaegeuk Kim 	LOOKUP_NODE,			/* look up a node without readahead */
572266e97a8SJaegeuk Kim 	LOOKUP_NODE_RA,			/*
573266e97a8SJaegeuk Kim 					 * look up a node with readahead called
5744f4124d0SChao Yu 					 * by get_data_block.
57539a53e0cSJaegeuk Kim 					 */
576266e97a8SJaegeuk Kim };
577266e97a8SJaegeuk Kim 
57891803392SChao Yu #define DEFAULT_RETRY_IO_COUNT	8	/* maximum retry read IO or flush count */
5797735730dSChao Yu 
5805df7731fSChao Yu /* congestion wait timeout value, default: 20ms */
5815df7731fSChao Yu #define	DEFAULT_IO_TIMEOUT	(msecs_to_jiffies(20))
5825df7731fSChao Yu 
583af033b2aSChao Yu /* maximum retry quota flush count */
584af033b2aSChao Yu #define DEFAULT_RETRY_QUOTA_FLUSH_COUNT		8
585af033b2aSChao Yu 
586a7b8618aSJaegeuk Kim /* maximum retry of EIO'ed page */
587a7b8618aSJaegeuk Kim #define MAX_RETRY_PAGE_EIO			100
58850c63009SJaegeuk Kim 
589a6db67f0SJaegeuk Kim #define F2FS_LINK_MAX	0xffffffff	/* maximum link count per file */
59039a53e0cSJaegeuk Kim 
591817202d9SChao Yu #define MAX_DIR_RA_PAGES	4	/* maximum ra pages of dir */
592817202d9SChao Yu 
593287b1406SChao Yu /* dirty segments threshold for triggering CP */
594287b1406SChao Yu #define DEFAULT_DIRTY_THRESHOLD		4
595287b1406SChao Yu 
59639a53e0cSJaegeuk Kim /* for in-memory extent cache entry */
59713054c54SChao Yu #define F2FS_MIN_EXTENT_LEN	64	/* minimum extent length */
59813054c54SChao Yu 
59913054c54SChao Yu /* number of extent info in extent cache we try to shrink */
60013054c54SChao Yu #define EXTENT_CACHE_SHRINK_NUMBER	128
601c11abd1aSJaegeuk Kim 
602430f163bSChao Yu #define RECOVERY_MAX_RA_BLOCKS		BIO_MAX_VECS
603430f163bSChao Yu #define RECOVERY_MIN_RA_BLOCKS		1
604430f163bSChao Yu 
60501fc4b9aSFengnan Chang #define F2FS_ONSTACK_PAGES	16	/* nr of onstack pages */
60601fc4b9aSFengnan Chang 
60754c2258cSChao Yu struct rb_entry {
60854c2258cSChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
6092e9b2bb2SChao Yu 	union {
6102e9b2bb2SChao Yu 		struct {
61154c2258cSChao Yu 			unsigned int ofs;	/* start offset of the entry */
61254c2258cSChao Yu 			unsigned int len;	/* length of the entry */
61354c2258cSChao Yu 		};
6142e9b2bb2SChao Yu 		unsigned long long key;		/* 64-bits key */
61548046cb5SJaegeuk Kim 	} __packed;
6162e9b2bb2SChao Yu };
61754c2258cSChao Yu 
61839a53e0cSJaegeuk Kim struct extent_info {
61939a53e0cSJaegeuk Kim 	unsigned int fofs;		/* start offset in a file */
620111d2495SMasanari Iida 	unsigned int len;		/* length of the extent */
62154c2258cSChao Yu 	u32 blk;			/* start block address of the extent */
62294afd6d6SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
62394afd6d6SChao Yu 	unsigned int c_len;		/* physical extent length of compressed blocks */
62494afd6d6SChao Yu #endif
62539a53e0cSJaegeuk Kim };
62639a53e0cSJaegeuk Kim 
62713054c54SChao Yu struct extent_node {
628c0362117SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
62913054c54SChao Yu 	struct extent_info ei;		/* extent info */
63054c2258cSChao Yu 	struct list_head list;		/* node in global extent list of sbi */
631201ef5e0SHou Pengyang 	struct extent_tree *et;		/* extent tree pointer */
63213054c54SChao Yu };
63313054c54SChao Yu 
63413054c54SChao Yu struct extent_tree {
63513054c54SChao Yu 	nid_t ino;			/* inode number */
6364dada3fdSChao Yu 	struct rb_root_cached root;	/* root of extent info rb-tree */
63762c8af65SChao Yu 	struct extent_node *cached_en;	/* recently accessed extent node */
6383e72f721SJaegeuk Kim 	struct extent_info largest;	/* largested extent info */
639137d09f0SJaegeuk Kim 	struct list_head list;		/* to be used by sbi->zombie_list */
64013054c54SChao Yu 	rwlock_t lock;			/* protect extent info rb-tree */
64168e35385SChao Yu 	atomic_t node_cnt;		/* # of extent node in rb-tree*/
642b430f726SZhikang Zhang 	bool largest_updated;		/* largest extent updated */
64313054c54SChao Yu };
64413054c54SChao Yu 
64539a53e0cSJaegeuk Kim /*
646003a3e1dSJaegeuk Kim  * This structure is taken from ext4_map_blocks.
647003a3e1dSJaegeuk Kim  *
648003a3e1dSJaegeuk Kim  * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
649003a3e1dSJaegeuk Kim  */
650003a3e1dSJaegeuk Kim #define F2FS_MAP_NEW		(1 << BH_New)
651003a3e1dSJaegeuk Kim #define F2FS_MAP_MAPPED		(1 << BH_Mapped)
6527f63eb77SJaegeuk Kim #define F2FS_MAP_UNWRITTEN	(1 << BH_Unwritten)
6537f63eb77SJaegeuk Kim #define F2FS_MAP_FLAGS		(F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
6547f63eb77SJaegeuk Kim 				F2FS_MAP_UNWRITTEN)
655003a3e1dSJaegeuk Kim 
656003a3e1dSJaegeuk Kim struct f2fs_map_blocks {
65771f2c820SChao Yu 	struct block_device *m_bdev;	/* for multi-device dio */
658003a3e1dSJaegeuk Kim 	block_t m_pblk;
659003a3e1dSJaegeuk Kim 	block_t m_lblk;
660003a3e1dSJaegeuk Kim 	unsigned int m_len;
661003a3e1dSJaegeuk Kim 	unsigned int m_flags;
662da85985cSChao Yu 	pgoff_t *m_next_pgofs;		/* point next possible non-hole pgofs */
663c4020b2dSChao Yu 	pgoff_t *m_next_extent;		/* point to next possible extent */
664d5097be5SHyunchul Lee 	int m_seg_type;
665f9d6d059SChao Yu 	bool m_may_create;		/* indicate it is from write path */
66671f2c820SChao Yu 	bool m_multidev_dio;		/* indicate it allows multi-device dio */
667003a3e1dSJaegeuk Kim };
668003a3e1dSJaegeuk Kim 
669e2b4e2bcSChao Yu /* for flag in get_data_block */
670f2220c7fSQiuyang Sun enum {
671f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_DEFAULT,
672f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_FIEMAP,
673f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_BMAP,
6740a4daae5SJaegeuk Kim 	F2FS_GET_BLOCK_DIO,
675f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_DIO,
676f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_AIO,
677c4020b2dSChao Yu 	F2FS_GET_BLOCK_PRECACHE,
678f2220c7fSQiuyang Sun };
679e2b4e2bcSChao Yu 
680003a3e1dSJaegeuk Kim /*
68139a53e0cSJaegeuk Kim  * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
68239a53e0cSJaegeuk Kim  */
68339a53e0cSJaegeuk Kim #define FADVISE_COLD_BIT	0x01
684354a3399SJaegeuk Kim #define FADVISE_LOST_PINO_BIT	0x02
685cde4de12SJaegeuk Kim #define FADVISE_ENCRYPT_BIT	0x04
686e7d55452SJaegeuk Kim #define FADVISE_ENC_NAME_BIT	0x08
68726787236SJaegeuk Kim #define FADVISE_KEEP_SIZE_BIT	0x10
688b6a06cbbSChao Yu #define FADVISE_HOT_BIT		0x20
68995ae251fSEric Biggers #define FADVISE_VERITY_BIT	0x40
690d4dd19ecSJaegeuk Kim #define FADVISE_TRUNC_BIT	0x80
69139a53e0cSJaegeuk Kim 
692797c1cb5SChao Yu #define FADVISE_MODIFIABLE_BITS	(FADVISE_COLD_BIT | FADVISE_HOT_BIT)
693797c1cb5SChao Yu 
694b5492af7SJaegeuk Kim #define file_is_cold(inode)	is_file(inode, FADVISE_COLD_BIT)
695b5492af7SJaegeuk Kim #define file_set_cold(inode)	set_file(inode, FADVISE_COLD_BIT)
696b5492af7SJaegeuk Kim #define file_clear_cold(inode)	clear_file(inode, FADVISE_COLD_BIT)
6971153db09SChao Yu 
6981153db09SChao Yu #define file_wrong_pino(inode)	is_file(inode, FADVISE_LOST_PINO_BIT)
6991153db09SChao Yu #define file_lost_pino(inode)	set_file(inode, FADVISE_LOST_PINO_BIT)
700b5492af7SJaegeuk Kim #define file_got_pino(inode)	clear_file(inode, FADVISE_LOST_PINO_BIT)
7011153db09SChao Yu 
702cde4de12SJaegeuk Kim #define file_is_encrypt(inode)	is_file(inode, FADVISE_ENCRYPT_BIT)
703cde4de12SJaegeuk Kim #define file_set_encrypt(inode)	set_file(inode, FADVISE_ENCRYPT_BIT)
7041153db09SChao Yu 
705e7d55452SJaegeuk Kim #define file_enc_name(inode)	is_file(inode, FADVISE_ENC_NAME_BIT)
706e7d55452SJaegeuk Kim #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
7071153db09SChao Yu 
70826787236SJaegeuk Kim #define file_keep_isize(inode)	is_file(inode, FADVISE_KEEP_SIZE_BIT)
70926787236SJaegeuk Kim #define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
7101153db09SChao Yu 
711b6a06cbbSChao Yu #define file_is_hot(inode)	is_file(inode, FADVISE_HOT_BIT)
712b6a06cbbSChao Yu #define file_set_hot(inode)	set_file(inode, FADVISE_HOT_BIT)
713b6a06cbbSChao Yu #define file_clear_hot(inode)	clear_file(inode, FADVISE_HOT_BIT)
7141153db09SChao Yu 
71595ae251fSEric Biggers #define file_is_verity(inode)	is_file(inode, FADVISE_VERITY_BIT)
71695ae251fSEric Biggers #define file_set_verity(inode)	set_file(inode, FADVISE_VERITY_BIT)
717cde4de12SJaegeuk Kim 
718d4dd19ecSJaegeuk Kim #define file_should_truncate(inode)	is_file(inode, FADVISE_TRUNC_BIT)
719d4dd19ecSJaegeuk Kim #define file_need_truncate(inode)	set_file(inode, FADVISE_TRUNC_BIT)
720d4dd19ecSJaegeuk Kim #define file_dont_truncate(inode)	clear_file(inode, FADVISE_TRUNC_BIT)
721d4dd19ecSJaegeuk Kim 
722ab9fa662SJaegeuk Kim #define DEF_DIR_LEVEL		0
723ab9fa662SJaegeuk Kim 
7242ef79ecbSChao Yu enum {
7252ef79ecbSChao Yu 	GC_FAILURE_PIN,
7262ef79ecbSChao Yu 	MAX_GC_FAILURE
7272ef79ecbSChao Yu };
7282ef79ecbSChao Yu 
7297653b9d8SChao Yu /* used for f2fs_inode_info->flags */
7307653b9d8SChao Yu enum {
7317653b9d8SChao Yu 	FI_NEW_INODE,		/* indicate newly allocated inode */
7327653b9d8SChao Yu 	FI_DIRTY_INODE,		/* indicate inode is dirty or not */
7337653b9d8SChao Yu 	FI_AUTO_RECOVER,	/* indicate inode is recoverable */
7347653b9d8SChao Yu 	FI_DIRTY_DIR,		/* indicate directory has dirty pages */
7357653b9d8SChao Yu 	FI_INC_LINK,		/* need to increment i_nlink */
7367653b9d8SChao Yu 	FI_ACL_MODE,		/* indicate acl mode */
7377653b9d8SChao Yu 	FI_NO_ALLOC,		/* should not allocate any blocks */
7387653b9d8SChao Yu 	FI_FREE_NID,		/* free allocated nide */
7397653b9d8SChao Yu 	FI_NO_EXTENT,		/* not to use the extent cache */
7407653b9d8SChao Yu 	FI_INLINE_XATTR,	/* used for inline xattr */
7417653b9d8SChao Yu 	FI_INLINE_DATA,		/* used for inline data*/
7427653b9d8SChao Yu 	FI_INLINE_DENTRY,	/* used for inline dentry */
7437653b9d8SChao Yu 	FI_APPEND_WRITE,	/* inode has appended data */
7447653b9d8SChao Yu 	FI_UPDATE_WRITE,	/* inode has in-place-update data */
7457653b9d8SChao Yu 	FI_NEED_IPU,		/* used for ipu per file */
7467653b9d8SChao Yu 	FI_ATOMIC_FILE,		/* indicate atomic file */
7477653b9d8SChao Yu 	FI_FIRST_BLOCK_WRITTEN,	/* indicate #0 data block was written */
7487653b9d8SChao Yu 	FI_DROP_CACHE,		/* drop dirty page cache */
7497653b9d8SChao Yu 	FI_DATA_EXIST,		/* indicate data exists */
7507653b9d8SChao Yu 	FI_INLINE_DOTS,		/* indicate inline dot dentries */
7511018a546SChao Yu 	FI_SKIP_WRITES,		/* should skip data page writeback */
7521018a546SChao Yu 	FI_OPU_WRITE,		/* used for opu per file */
7537653b9d8SChao Yu 	FI_DIRTY_FILE,		/* indicate regular/symlink has dirty pages */
7543d697a4aSEric Biggers 	FI_PREALLOCATED_ALL,	/* all blocks for write were preallocated */
7557653b9d8SChao Yu 	FI_HOT_DATA,		/* indicate file is hot */
7567653b9d8SChao Yu 	FI_EXTRA_ATTR,		/* indicate file has extra attribute */
7577653b9d8SChao Yu 	FI_PROJ_INHERIT,	/* indicate file inherits projectid */
7587653b9d8SChao Yu 	FI_PIN_FILE,		/* indicate file should not be gced */
7597653b9d8SChao Yu 	FI_VERITY_IN_PROGRESS,	/* building fs-verity Merkle tree */
7607653b9d8SChao Yu 	FI_COMPRESSED_FILE,	/* indicate file's data can be compressed */
761b28f047bSChao Yu 	FI_COMPRESS_CORRUPT,	/* indicate compressed cluster is corrupted */
7627653b9d8SChao Yu 	FI_MMAP_FILE,		/* indicate file was mmapped */
763602a16d5SDaeho Jeong 	FI_ENABLE_COMPRESS,	/* enable compression in "user" compression mode */
764c6140415SJaegeuk Kim 	FI_COMPRESS_RELEASED,	/* compressed blocks were released */
765859fca6bSChao Yu 	FI_ALIGNED_WRITE,	/* enable aligned write */
7664a2c5b79SYe Bin 	FI_COW_FILE,		/* indicate COW file */
7677653b9d8SChao Yu 	FI_MAX,			/* max flag, never be used */
7687653b9d8SChao Yu };
7697653b9d8SChao Yu 
77039a53e0cSJaegeuk Kim struct f2fs_inode_info {
77139a53e0cSJaegeuk Kim 	struct inode vfs_inode;		/* serve a vfs inode */
77239a53e0cSJaegeuk Kim 	unsigned long i_flags;		/* keep an inode flags for ioctl */
77339a53e0cSJaegeuk Kim 	unsigned char i_advise;		/* use to give file attribute hints */
77438431545SJaegeuk Kim 	unsigned char i_dir_level;	/* use for dentry level for large dir */
7751ad71a27SJaegeuk Kim 	unsigned int i_current_depth;	/* only for directory depth */
7762ef79ecbSChao Yu 	/* for gc failure statistic */
7772ef79ecbSChao Yu 	unsigned int i_gc_failures[MAX_GC_FAILURE];
7786666e6aaSJaegeuk Kim 	unsigned int i_pino;		/* parent inode number */
77939a53e0cSJaegeuk Kim 	umode_t i_acl_mode;		/* keep file acl mode temporarily */
78039a53e0cSJaegeuk Kim 
78139a53e0cSJaegeuk Kim 	/* Use below internally in f2fs*/
7827653b9d8SChao Yu 	unsigned long flags[BITS_TO_LONGS(FI_MAX)];	/* use to pass per-file flags */
783e4544b63STim Murray 	struct f2fs_rwsem i_sem;	/* protect fi info */
784204706c7SJaegeuk Kim 	atomic_t dirty_pages;		/* # of dirty pages */
78539a53e0cSJaegeuk Kim 	f2fs_hash_t chash;		/* hash value of given file name */
78639a53e0cSJaegeuk Kim 	unsigned int clevel;		/* maximum level of given file name */
78788c5c13aSJaegeuk Kim 	struct task_struct *task;	/* lookup and create consistency */
788b0af6d49SChao Yu 	struct task_struct *cp_task;	/* separate cp/wb IO stats*/
789d80afefbSChao Yu 	struct task_struct *wb_task;	/* indicate inode is in context of writeback */
79039a53e0cSJaegeuk Kim 	nid_t i_xattr_nid;		/* node id that contains xattrs */
79126de9b11SJaegeuk Kim 	loff_t	last_disk_size;		/* lastly written file size */
792c10c9820SChao Yu 	spinlock_t i_size_lock;		/* protect last_disk_size */
79388b88a66SJaegeuk Kim 
7940abd675eSChao Yu #ifdef CONFIG_QUOTA
7950abd675eSChao Yu 	struct dquot *i_dquot[MAXQUOTAS];
7960abd675eSChao Yu 
7970abd675eSChao Yu 	/* quota space reservation, managed internally by quota code */
7980abd675eSChao Yu 	qsize_t i_reserved_quota;
7990abd675eSChao Yu #endif
8000f18b462SJaegeuk Kim 	struct list_head dirty_list;	/* dirty list for dirs and files */
8010f18b462SJaegeuk Kim 	struct list_head gdirty_list;	/* linked in global dirty list */
8023db1de0eSDaeho Jeong 	struct task_struct *atomic_write_task;	/* store atomic write task */
8033e72f721SJaegeuk Kim 	struct extent_tree *extent_tree;	/* cached extent_tree entry */
8043db1de0eSDaeho Jeong 	struct inode *cow_inode;	/* copy-on-write inode for atomic write */
805b2532c69SChao Yu 
806b2532c69SChao Yu 	/* avoid racing between foreground op and gc */
807e4544b63STim Murray 	struct f2fs_rwsem i_gc_rwsem[2];
808e4544b63STim Murray 	struct f2fs_rwsem i_xattr_sem; /* avoid racing between reading and changing EAs */
809f2470371SChao Yu 
8107a2af766SChao Yu 	int i_extra_isize;		/* size of extra space located in i_addr */
8115c57132eSChao Yu 	kprojid_t i_projid;		/* id for project quota */
8126afc662eSChao Yu 	int i_inline_xattr_size;	/* inline xattr size */
81324b81dfcSArnd Bergmann 	struct timespec64 i_crtime;	/* inode creation time */
81424b81dfcSArnd Bergmann 	struct timespec64 i_disk_time[4];/* inode disk times */
8154c8ff709SChao Yu 
8164c8ff709SChao Yu 	/* for file compress */
817c2759ebaSDaeho Jeong 	atomic_t i_compr_blocks;		/* # of compressed blocks */
8184c8ff709SChao Yu 	unsigned char i_compress_algorithm;	/* algorithm type */
8194c8ff709SChao Yu 	unsigned char i_log_cluster_size;	/* log of cluster size */
8203fde13f8SChao Yu 	unsigned char i_compress_level;		/* compress level (lz4hc,zstd) */
821b28f047bSChao Yu 	unsigned short i_compress_flag;		/* compress flag */
8224c8ff709SChao Yu 	unsigned int i_cluster_size;		/* cluster size */
823f8e2f32bSDaeho Jeong 
824f8e2f32bSDaeho Jeong 	unsigned int atomic_write_cnt;
82539a53e0cSJaegeuk Kim };
82639a53e0cSJaegeuk Kim 
82739a53e0cSJaegeuk Kim static inline void get_extent_info(struct extent_info *ext,
828bd933d4fSChao Yu 					struct f2fs_extent *i_ext)
82939a53e0cSJaegeuk Kim {
830bd933d4fSChao Yu 	ext->fofs = le32_to_cpu(i_ext->fofs);
831bd933d4fSChao Yu 	ext->blk = le32_to_cpu(i_ext->blk);
832bd933d4fSChao Yu 	ext->len = le32_to_cpu(i_ext->len);
83339a53e0cSJaegeuk Kim }
83439a53e0cSJaegeuk Kim 
83539a53e0cSJaegeuk Kim static inline void set_raw_extent(struct extent_info *ext,
83639a53e0cSJaegeuk Kim 					struct f2fs_extent *i_ext)
83739a53e0cSJaegeuk Kim {
83839a53e0cSJaegeuk Kim 	i_ext->fofs = cpu_to_le32(ext->fofs);
8394d0b0bd4SChao Yu 	i_ext->blk = cpu_to_le32(ext->blk);
84039a53e0cSJaegeuk Kim 	i_ext->len = cpu_to_le32(ext->len);
84139a53e0cSJaegeuk Kim }
84239a53e0cSJaegeuk Kim 
843429511cdSChao Yu static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
844429511cdSChao Yu 						u32 blk, unsigned int len)
845429511cdSChao Yu {
846429511cdSChao Yu 	ei->fofs = fofs;
847429511cdSChao Yu 	ei->blk = blk;
848429511cdSChao Yu 	ei->len = len;
84994afd6d6SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
85094afd6d6SChao Yu 	ei->c_len = 0;
85194afd6d6SChao Yu #endif
852429511cdSChao Yu }
853429511cdSChao Yu 
854004b6862SChao Yu static inline bool __is_discard_mergeable(struct discard_info *back,
85535ec7d57SChao Yu 			struct discard_info *front, unsigned int max_len)
856004b6862SChao Yu {
8579a997188SJaegeuk Kim 	return (back->lstart + back->len == front->lstart) &&
85835ec7d57SChao Yu 		(back->len + front->len <= max_len);
859004b6862SChao Yu }
860004b6862SChao Yu 
861004b6862SChao Yu static inline bool __is_discard_back_mergeable(struct discard_info *cur,
86235ec7d57SChao Yu 			struct discard_info *back, unsigned int max_len)
863004b6862SChao Yu {
86435ec7d57SChao Yu 	return __is_discard_mergeable(back, cur, max_len);
865004b6862SChao Yu }
866004b6862SChao Yu 
867004b6862SChao Yu static inline bool __is_discard_front_mergeable(struct discard_info *cur,
86835ec7d57SChao Yu 			struct discard_info *front, unsigned int max_len)
869004b6862SChao Yu {
87035ec7d57SChao Yu 	return __is_discard_mergeable(cur, front, max_len);
871004b6862SChao Yu }
872004b6862SChao Yu 
873429511cdSChao Yu static inline bool __is_extent_mergeable(struct extent_info *back,
874429511cdSChao Yu 						struct extent_info *front)
875429511cdSChao Yu {
87694afd6d6SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
87794afd6d6SChao Yu 	if (back->c_len && back->len != back->c_len)
87894afd6d6SChao Yu 		return false;
87994afd6d6SChao Yu 	if (front->c_len && front->len != front->c_len)
88094afd6d6SChao Yu 		return false;
88194afd6d6SChao Yu #endif
882429511cdSChao Yu 	return (back->fofs + back->len == front->fofs &&
883429511cdSChao Yu 			back->blk + back->len == front->blk);
884429511cdSChao Yu }
885429511cdSChao Yu 
886429511cdSChao Yu static inline bool __is_back_mergeable(struct extent_info *cur,
887429511cdSChao Yu 						struct extent_info *back)
888429511cdSChao Yu {
889429511cdSChao Yu 	return __is_extent_mergeable(back, cur);
890429511cdSChao Yu }
891429511cdSChao Yu 
892429511cdSChao Yu static inline bool __is_front_mergeable(struct extent_info *cur,
893429511cdSChao Yu 						struct extent_info *front)
894429511cdSChao Yu {
895429511cdSChao Yu 	return __is_extent_mergeable(cur, front);
896429511cdSChao Yu }
897429511cdSChao Yu 
898cac5a3d8SDongOh Shin extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
899b430f726SZhikang Zhang static inline void __try_update_largest_extent(struct extent_tree *et,
900b430f726SZhikang Zhang 						struct extent_node *en)
9014abd3f5aSChao Yu {
902205b9822SJaegeuk Kim 	if (en->ei.len > et->largest.len) {
9034abd3f5aSChao Yu 		et->largest = en->ei;
904b430f726SZhikang Zhang 		et->largest_updated = true;
905205b9822SJaegeuk Kim 	}
9064abd3f5aSChao Yu }
9074abd3f5aSChao Yu 
9089a4ffdf5SChao Yu /*
9099a4ffdf5SChao Yu  * For free nid management
9109a4ffdf5SChao Yu  */
9119a4ffdf5SChao Yu enum nid_state {
9129a4ffdf5SChao Yu 	FREE_NID,		/* newly added to free nid list */
9139a4ffdf5SChao Yu 	PREALLOC_NID,		/* it is preallocated */
9149a4ffdf5SChao Yu 	MAX_NID_STATE,
915b8559dc2SChao Yu };
916b8559dc2SChao Yu 
917a95ba66aSJaegeuk Kim enum nat_state {
918a95ba66aSJaegeuk Kim 	TOTAL_NAT,
919a95ba66aSJaegeuk Kim 	DIRTY_NAT,
920a95ba66aSJaegeuk Kim 	RECLAIMABLE_NAT,
921a95ba66aSJaegeuk Kim 	MAX_NAT_STATE,
922a95ba66aSJaegeuk Kim };
923a95ba66aSJaegeuk Kim 
92439a53e0cSJaegeuk Kim struct f2fs_nm_info {
92539a53e0cSJaegeuk Kim 	block_t nat_blkaddr;		/* base disk address of NAT */
92639a53e0cSJaegeuk Kim 	nid_t max_nid;			/* maximum possible node ids */
92704d47e67SChao Yu 	nid_t available_nids;		/* # of available node ids */
92839a53e0cSJaegeuk Kim 	nid_t next_scan_nid;		/* the next nid to be scanned */
92947c8ebccSJaegeuk Kim 	nid_t max_rf_node_blocks;	/* max # of nodes for recovery */
930cdfc41c1SJaegeuk Kim 	unsigned int ram_thresh;	/* control the memory footprint */
931ea1a29a0SChao Yu 	unsigned int ra_nid_pages;	/* # of nid pages to be readaheaded */
9322304cb0cSChao Yu 	unsigned int dirty_nats_ratio;	/* control dirty nats ratio threshold */
93339a53e0cSJaegeuk Kim 
93439a53e0cSJaegeuk Kim 	/* NAT cache management */
93539a53e0cSJaegeuk Kim 	struct radix_tree_root nat_root;/* root of the nat entry cache */
936309cc2b6SJaegeuk Kim 	struct radix_tree_root nat_set_root;/* root of the nat set cache */
937e4544b63STim Murray 	struct f2fs_rwsem nat_tree_lock;	/* protect nat entry tree */
93839a53e0cSJaegeuk Kim 	struct list_head nat_entries;	/* cached nat entry list (clean) */
93922969158SChao Yu 	spinlock_t nat_list_lock;	/* protect clean nat entry list */
940a95ba66aSJaegeuk Kim 	unsigned int nat_cnt[MAX_NAT_STATE]; /* the # of cached nat entries */
94122ad0b6aSJaegeuk Kim 	unsigned int nat_blocks;	/* # of nat blocks */
94239a53e0cSJaegeuk Kim 
94339a53e0cSJaegeuk Kim 	/* free node ids management */
9448a7ed66aSJaegeuk Kim 	struct radix_tree_root free_nid_root;/* root of the free_nid cache */
9459a4ffdf5SChao Yu 	struct list_head free_nid_list;		/* list for free nids excluding preallocated nids */
9469a4ffdf5SChao Yu 	unsigned int nid_cnt[MAX_NID_STATE];	/* the number of free node id */
947b8559dc2SChao Yu 	spinlock_t nid_list_lock;	/* protect nid lists ops */
94839a53e0cSJaegeuk Kim 	struct mutex build_lock;	/* lock for build free nids */
949bb1105e4SJaegeuk Kim 	unsigned char **free_nid_bitmap;
9504ac91242SChao Yu 	unsigned char *nat_block_bitmap;
951586d1492SChao Yu 	unsigned short *free_nid_count;	/* free nid count of NAT block */
95239a53e0cSJaegeuk Kim 
95339a53e0cSJaegeuk Kim 	/* for checkpoint */
95439a53e0cSJaegeuk Kim 	char *nat_bitmap;		/* NAT bitmap pointer */
95522ad0b6aSJaegeuk Kim 
95622ad0b6aSJaegeuk Kim 	unsigned int nat_bits_blocks;	/* # of nat bits blocks */
95722ad0b6aSJaegeuk Kim 	unsigned char *nat_bits;	/* NAT bits blocks */
95822ad0b6aSJaegeuk Kim 	unsigned char *full_nat_bits;	/* full NAT pages */
95922ad0b6aSJaegeuk Kim 	unsigned char *empty_nat_bits;	/* empty NAT pages */
960599a09b2SChao Yu #ifdef CONFIG_F2FS_CHECK_FS
961599a09b2SChao Yu 	char *nat_bitmap_mir;		/* NAT bitmap mirror */
962599a09b2SChao Yu #endif
96339a53e0cSJaegeuk Kim 	int bitmap_size;		/* bitmap size */
96439a53e0cSJaegeuk Kim };
96539a53e0cSJaegeuk Kim 
96639a53e0cSJaegeuk Kim /*
96739a53e0cSJaegeuk Kim  * this structure is used as one of function parameters.
96839a53e0cSJaegeuk Kim  * all the information are dedicated to a given direct node block determined
96939a53e0cSJaegeuk Kim  * by the data offset in a file.
97039a53e0cSJaegeuk Kim  */
97139a53e0cSJaegeuk Kim struct dnode_of_data {
97239a53e0cSJaegeuk Kim 	struct inode *inode;		/* vfs inode pointer */
97339a53e0cSJaegeuk Kim 	struct page *inode_page;	/* its inode page, NULL is possible */
97439a53e0cSJaegeuk Kim 	struct page *node_page;		/* cached direct node page */
97539a53e0cSJaegeuk Kim 	nid_t nid;			/* node id of the direct node block */
97639a53e0cSJaegeuk Kim 	unsigned int ofs_in_node;	/* data offset in the node page */
97739a53e0cSJaegeuk Kim 	bool inode_page_locked;		/* inode page is locked or not */
97893bae099SJaegeuk Kim 	bool node_changed;		/* is node block changed */
9793cf45747SChao Yu 	char cur_level;			/* level of hole node page */
9803cf45747SChao Yu 	char max_level;			/* level of current page located */
98139a53e0cSJaegeuk Kim 	block_t	data_blkaddr;		/* block address of the node block */
98239a53e0cSJaegeuk Kim };
98339a53e0cSJaegeuk Kim 
98439a53e0cSJaegeuk Kim static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
98539a53e0cSJaegeuk Kim 		struct page *ipage, struct page *npage, nid_t nid)
98639a53e0cSJaegeuk Kim {
987d66d1f76SJaegeuk Kim 	memset(dn, 0, sizeof(*dn));
98839a53e0cSJaegeuk Kim 	dn->inode = inode;
98939a53e0cSJaegeuk Kim 	dn->inode_page = ipage;
99039a53e0cSJaegeuk Kim 	dn->node_page = npage;
99139a53e0cSJaegeuk Kim 	dn->nid = nid;
99239a53e0cSJaegeuk Kim }
99339a53e0cSJaegeuk Kim 
99439a53e0cSJaegeuk Kim /*
99539a53e0cSJaegeuk Kim  * For SIT manager
99639a53e0cSJaegeuk Kim  *
99739a53e0cSJaegeuk Kim  * By default, there are 6 active log areas across the whole main area.
99839a53e0cSJaegeuk Kim  * When considering hot and cold data separation to reduce cleaning overhead,
99939a53e0cSJaegeuk Kim  * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
100039a53e0cSJaegeuk Kim  * respectively.
100139a53e0cSJaegeuk Kim  * In the current design, you should not change the numbers intentionally.
100239a53e0cSJaegeuk Kim  * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
100339a53e0cSJaegeuk Kim  * logs individually according to the underlying devices. (default: 6)
100439a53e0cSJaegeuk Kim  * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
100539a53e0cSJaegeuk Kim  * data and 8 for node logs.
100639a53e0cSJaegeuk Kim  */
100739a53e0cSJaegeuk Kim #define	NR_CURSEG_DATA_TYPE	(3)
100839a53e0cSJaegeuk Kim #define NR_CURSEG_NODE_TYPE	(3)
1009093749e2SChao Yu #define NR_CURSEG_INMEM_TYPE	(2)
1010a7d9fe3cSJaegeuk Kim #define NR_CURSEG_RO_TYPE	(2)
1011d0b9e42aSChao Yu #define NR_CURSEG_PERSIST_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
1012d0b9e42aSChao Yu #define NR_CURSEG_TYPE		(NR_CURSEG_INMEM_TYPE + NR_CURSEG_PERSIST_TYPE)
101339a53e0cSJaegeuk Kim 
101439a53e0cSJaegeuk Kim enum {
101539a53e0cSJaegeuk Kim 	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
101639a53e0cSJaegeuk Kim 	CURSEG_WARM_DATA,	/* data blocks */
101739a53e0cSJaegeuk Kim 	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
101839a53e0cSJaegeuk Kim 	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
101939a53e0cSJaegeuk Kim 	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
102039a53e0cSJaegeuk Kim 	CURSEG_COLD_NODE,	/* indirect node blocks */
1021d0b9e42aSChao Yu 	NR_PERSISTENT_LOG,	/* number of persistent log */
1022d0b9e42aSChao Yu 	CURSEG_COLD_DATA_PINNED = NR_PERSISTENT_LOG,
1023d0b9e42aSChao Yu 				/* pinned file that needs consecutive block address */
1024093749e2SChao Yu 	CURSEG_ALL_DATA_ATGC,	/* SSR alloctor in hot/warm/cold data area */
1025d0b9e42aSChao Yu 	NO_CHECK_TYPE,		/* number of persistent & inmem log */
102639a53e0cSJaegeuk Kim };
102739a53e0cSJaegeuk Kim 
10286b4afdd7SJaegeuk Kim struct flush_cmd {
10296b4afdd7SJaegeuk Kim 	struct completion wait;
1030721bd4d5SGu Zheng 	struct llist_node llnode;
103139d787beSChao Yu 	nid_t ino;
10326b4afdd7SJaegeuk Kim 	int ret;
10336b4afdd7SJaegeuk Kim };
10346b4afdd7SJaegeuk Kim 
1035a688b9d9SGu Zheng struct flush_cmd_control {
1036a688b9d9SGu Zheng 	struct task_struct *f2fs_issue_flush;	/* flush thread */
1037a688b9d9SGu Zheng 	wait_queue_head_t flush_wait_queue;	/* waiting queue for wake-up */
10388b8dd65fSChao Yu 	atomic_t issued_flush;			/* # of issued flushes */
103972691af6SJaegeuk Kim 	atomic_t queued_flush;			/* # of queued flushes */
1040721bd4d5SGu Zheng 	struct llist_head issue_list;		/* list for command issue */
1041721bd4d5SGu Zheng 	struct llist_node *dispatch_list;	/* list for command dispatch */
1042a688b9d9SGu Zheng };
1043a688b9d9SGu Zheng 
104439a53e0cSJaegeuk Kim struct f2fs_sm_info {
104539a53e0cSJaegeuk Kim 	struct sit_info *sit_info;		/* whole segment information */
104639a53e0cSJaegeuk Kim 	struct free_segmap_info *free_info;	/* free segment information */
104739a53e0cSJaegeuk Kim 	struct dirty_seglist_info *dirty_info;	/* dirty segment information */
104839a53e0cSJaegeuk Kim 	struct curseg_info *curseg_array;	/* active segment information */
104939a53e0cSJaegeuk Kim 
1050e4544b63STim Murray 	struct f2fs_rwsem curseg_lock;	/* for preventing curseg change */
10512b60311dSChao Yu 
105239a53e0cSJaegeuk Kim 	block_t seg0_blkaddr;		/* block address of 0'th segment */
105339a53e0cSJaegeuk Kim 	block_t main_blkaddr;		/* start block address of main area */
105439a53e0cSJaegeuk Kim 	block_t ssa_blkaddr;		/* start block address of SSA area */
105539a53e0cSJaegeuk Kim 
105639a53e0cSJaegeuk Kim 	unsigned int segment_count;	/* total # of segments */
105739a53e0cSJaegeuk Kim 	unsigned int main_segments;	/* # of segments in main area */
105839a53e0cSJaegeuk Kim 	unsigned int reserved_segments;	/* # of reserved segments */
1059300a8429SChao Yu 	unsigned int additional_reserved_segments;/* reserved segs for IO align feature */
106039a53e0cSJaegeuk Kim 	unsigned int ovp_segments;	/* # of overprovision segments */
106181eb8d6eSJaegeuk Kim 
106281eb8d6eSJaegeuk Kim 	/* a threshold to reclaim prefree segments */
106381eb8d6eSJaegeuk Kim 	unsigned int rec_prefree_segments;
10647fd9e544SJaegeuk Kim 
1065bba681cbSJaegeuk Kim 	/* for batched trimming */
1066bba681cbSJaegeuk Kim 	unsigned int trim_sections;		/* # of sections to trim */
1067bba681cbSJaegeuk Kim 
1068184a5cd2SChao Yu 	struct list_head sit_entry_set;	/* sit entry set list */
1069184a5cd2SChao Yu 
1070216fbd64SJaegeuk Kim 	unsigned int ipu_policy;	/* in-place-update policy */
1071216fbd64SJaegeuk Kim 	unsigned int min_ipu_util;	/* in-place-update threshold */
1072c1ce1b02SJaegeuk Kim 	unsigned int min_fsync_blocks;	/* threshold for fsync */
1073853137ceSJaegeuk Kim 	unsigned int min_seq_blocks;	/* threshold for sequential blocks */
1074ef095d19SJaegeuk Kim 	unsigned int min_hot_blocks;	/* threshold for hot block allocation */
1075a2a12b67SChao Yu 	unsigned int min_ssr_sections;	/* threshold to trigger SSR allocation */
10766b4afdd7SJaegeuk Kim 
10776b4afdd7SJaegeuk Kim 	/* for flush command control */
1078b01a9201SJaegeuk Kim 	struct flush_cmd_control *fcc_info;
1079a688b9d9SGu Zheng 
10800b54fb84SJaegeuk Kim 	/* for discard command control */
10810b54fb84SJaegeuk Kim 	struct discard_cmd_control *dcc_info;
108239a53e0cSJaegeuk Kim };
108339a53e0cSJaegeuk Kim 
108439a53e0cSJaegeuk Kim /*
108539a53e0cSJaegeuk Kim  * For superblock
108639a53e0cSJaegeuk Kim  */
108739a53e0cSJaegeuk Kim /*
108839a53e0cSJaegeuk Kim  * COUNT_TYPE for monitoring
108939a53e0cSJaegeuk Kim  *
109039a53e0cSJaegeuk Kim  * f2fs monitors the number of several block types such as on-writeback,
109139a53e0cSJaegeuk Kim  * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
109239a53e0cSJaegeuk Kim  */
109336951b38SChao Yu #define WB_DATA_TYPE(p)	(__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
109439a53e0cSJaegeuk Kim enum count_type {
109539a53e0cSJaegeuk Kim 	F2FS_DIRTY_DENTS,
1096c227f912SChao Yu 	F2FS_DIRTY_DATA,
10972c8a4a28SJaegeuk Kim 	F2FS_DIRTY_QDATA,
109839a53e0cSJaegeuk Kim 	F2FS_DIRTY_NODES,
109939a53e0cSJaegeuk Kim 	F2FS_DIRTY_META,
11000f18b462SJaegeuk Kim 	F2FS_DIRTY_IMETA,
110136951b38SChao Yu 	F2FS_WB_CP_DATA,
110236951b38SChao Yu 	F2FS_WB_DATA,
11035f9abab4SJaegeuk Kim 	F2FS_RD_DATA,
11045f9abab4SJaegeuk Kim 	F2FS_RD_NODE,
11055f9abab4SJaegeuk Kim 	F2FS_RD_META,
110602b16d0aSChao Yu 	F2FS_DIO_WRITE,
110702b16d0aSChao Yu 	F2FS_DIO_READ,
110839a53e0cSJaegeuk Kim 	NR_COUNT_TYPE,
110939a53e0cSJaegeuk Kim };
111039a53e0cSJaegeuk Kim 
111139a53e0cSJaegeuk Kim /*
1112e1c42045Sarter97  * The below are the page types of bios used in submit_bio().
111339a53e0cSJaegeuk Kim  * The available types are:
111439a53e0cSJaegeuk Kim  * DATA			User data pages. It operates as async mode.
111539a53e0cSJaegeuk Kim  * NODE			Node pages. It operates as async mode.
111639a53e0cSJaegeuk Kim  * META			FS metadata pages such as SIT, NAT, CP.
111739a53e0cSJaegeuk Kim  * NR_PAGE_TYPE		The number of page types.
111839a53e0cSJaegeuk Kim  * META_FLUSH		Make sure the previous pages are written
111939a53e0cSJaegeuk Kim  *			with waiting the bio's completion
112039a53e0cSJaegeuk Kim  * ...			Only can be used with META.
112139a53e0cSJaegeuk Kim  */
11227d5e5109SJaegeuk Kim #define PAGE_TYPE_OF_BIO(type)	((type) > META ? META : (type))
112339a53e0cSJaegeuk Kim enum page_type {
11246b8beca0SChao Yu 	DATA = 0,
11256b8beca0SChao Yu 	NODE = 1,	/* should not change this */
112639a53e0cSJaegeuk Kim 	META,
112739a53e0cSJaegeuk Kim 	NR_PAGE_TYPE,
112839a53e0cSJaegeuk Kim 	META_FLUSH,
11293db1de0eSDaeho Jeong 	IPU,		/* the below types are used by tracepoints only. */
11308ce67cb0SJaegeuk Kim 	OPU,
113139a53e0cSJaegeuk Kim };
113239a53e0cSJaegeuk Kim 
1133a912b54dSJaegeuk Kim enum temp_type {
1134a912b54dSJaegeuk Kim 	HOT = 0,	/* must be zero for meta bio */
1135a912b54dSJaegeuk Kim 	WARM,
1136a912b54dSJaegeuk Kim 	COLD,
1137a912b54dSJaegeuk Kim 	NR_TEMP_TYPE,
1138a912b54dSJaegeuk Kim };
1139a912b54dSJaegeuk Kim 
1140cc15620bSJaegeuk Kim enum need_lock_type {
1141cc15620bSJaegeuk Kim 	LOCK_REQ = 0,
1142cc15620bSJaegeuk Kim 	LOCK_DONE,
1143cc15620bSJaegeuk Kim 	LOCK_RETRY,
1144cc15620bSJaegeuk Kim };
1145cc15620bSJaegeuk Kim 
1146a5fd5050SChao Yu enum cp_reason_type {
1147a5fd5050SChao Yu 	CP_NO_NEEDED,
1148a5fd5050SChao Yu 	CP_NON_REGULAR,
11494c8ff709SChao Yu 	CP_COMPRESSED,
1150a5fd5050SChao Yu 	CP_HARDLINK,
1151a5fd5050SChao Yu 	CP_SB_NEED_CP,
1152a5fd5050SChao Yu 	CP_WRONG_PINO,
1153a5fd5050SChao Yu 	CP_NO_SPC_ROLL,
1154a5fd5050SChao Yu 	CP_NODE_NEED_CP,
1155a5fd5050SChao Yu 	CP_FASTBOOT_MODE,
1156a5fd5050SChao Yu 	CP_SPEC_LOG_NUM,
11570a007b97SJaegeuk Kim 	CP_RECOVER_DIR,
1158a5fd5050SChao Yu };
1159a5fd5050SChao Yu 
1160b0af6d49SChao Yu enum iostat_type {
11618b83ac81SChao Yu 	/* WRITE IO */
11628b83ac81SChao Yu 	APP_DIRECT_IO,			/* app direct write IOs */
11638b83ac81SChao Yu 	APP_BUFFERED_IO,		/* app buffered write IOs */
1164b0af6d49SChao Yu 	APP_WRITE_IO,			/* app write IOs */
1165b0af6d49SChao Yu 	APP_MAPPED_IO,			/* app mapped IOs */
116634a23525SChao Yu 	APP_BUFFERED_CDATA_IO,		/* app buffered write IOs on compressed file */
116734a23525SChao Yu 	APP_MAPPED_CDATA_IO,		/* app mapped write IOs on compressed file */
1168b0af6d49SChao Yu 	FS_DATA_IO,			/* data IOs from kworker/fsync/reclaimer */
116934a23525SChao Yu 	FS_CDATA_IO,			/* data IOs from kworker/fsync/reclaimer on compressed file */
1170b0af6d49SChao Yu 	FS_NODE_IO,			/* node IOs from kworker/fsync/reclaimer */
1171b0af6d49SChao Yu 	FS_META_IO,			/* meta IOs from kworker/reclaimer */
1172b0af6d49SChao Yu 	FS_GC_DATA_IO,			/* data IOs from forground gc */
1173b0af6d49SChao Yu 	FS_GC_NODE_IO,			/* node IOs from forground gc */
1174b0af6d49SChao Yu 	FS_CP_DATA_IO,			/* data IOs from checkpoint */
1175b0af6d49SChao Yu 	FS_CP_NODE_IO,			/* node IOs from checkpoint */
1176b0af6d49SChao Yu 	FS_CP_META_IO,			/* meta IOs from checkpoint */
11778b83ac81SChao Yu 
11788b83ac81SChao Yu 	/* READ IO */
11798b83ac81SChao Yu 	APP_DIRECT_READ_IO,		/* app direct read IOs */
11808b83ac81SChao Yu 	APP_BUFFERED_READ_IO,		/* app buffered read IOs */
11818b83ac81SChao Yu 	APP_READ_IO,			/* app read IOs */
11828b83ac81SChao Yu 	APP_MAPPED_READ_IO,		/* app mapped read IOs */
118334a23525SChao Yu 	APP_BUFFERED_CDATA_READ_IO,	/* app buffered read IOs on compressed file  */
118434a23525SChao Yu 	APP_MAPPED_CDATA_READ_IO,	/* app mapped read IOs on compressed file  */
11858b83ac81SChao Yu 	FS_DATA_READ_IO,		/* data read IOs */
11869c122384SChao Yu 	FS_GDATA_READ_IO,		/* data read IOs from background gc */
11879c122384SChao Yu 	FS_CDATA_READ_IO,		/* compressed data read IOs */
11888b83ac81SChao Yu 	FS_NODE_READ_IO,		/* node read IOs */
11898b83ac81SChao Yu 	FS_META_READ_IO,		/* meta read IOs */
11908b83ac81SChao Yu 
11918b83ac81SChao Yu 	/* other */
1192b0af6d49SChao Yu 	FS_DISCARD,			/* discard */
1193b0af6d49SChao Yu 	NR_IO_TYPE,
1194b0af6d49SChao Yu };
1195b0af6d49SChao Yu 
1196458e6197SJaegeuk Kim struct f2fs_io_info {
119705ca3632SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs_sb_info pointer */
119839d787beSChao Yu 	nid_t ino;		/* inode number */
1199458e6197SJaegeuk Kim 	enum page_type type;	/* contains DATA/NODE/META/META_FLUSH */
1200a912b54dSJaegeuk Kim 	enum temp_type temp;	/* contains HOT/WARM/COLD */
12017649c873SBart Van Assche 	enum req_op op;		/* contains REQ_OP_ */
12027649c873SBart Van Assche 	blk_opf_t op_flags;	/* req_flag_bits */
12037a9d7548SChao Yu 	block_t new_blkaddr;	/* new block address to be written */
120428bc106bSChao Yu 	block_t old_blkaddr;	/* old block address before Cow */
120505ca3632SJaegeuk Kim 	struct page *page;	/* page to be written */
12064375a336SJaegeuk Kim 	struct page *encrypted_page;	/* encrypted page */
12074c8ff709SChao Yu 	struct page *compressed_page;	/* compressed page */
1208fb830fc5SChao Yu 	struct list_head list;		/* serialize IOs */
1209d68f735bSJaegeuk Kim 	bool submitted;		/* indicate IO submission */
1210cc15620bSJaegeuk Kim 	int need_lock;		/* indicate we need to lock cp_rwsem */
1211fb830fc5SChao Yu 	bool in_list;		/* indicate fio is in io_list */
12126dc3a126SChao Yu 	bool is_por;		/* indicate IO is from recovery or not */
1213fe16efe6SChao Yu 	bool retry;		/* need to reallocate block address */
12144c8ff709SChao Yu 	int compr_blocks;	/* # of compressed block addresses */
12154c8ff709SChao Yu 	bool encrypted;		/* indicate file is encrypted */
12160d5b9d81SChao Yu 	bool post_read;		/* require post read */
1217b0af6d49SChao Yu 	enum iostat_type io_type;	/* io type */
1218578c6478SYufen Yu 	struct writeback_control *io_wbc; /* writeback control */
12198648de2cSChao Yu 	struct bio **bio;		/* bio for ipu */
12208648de2cSChao Yu 	sector_t *last_block;		/* last block number in bio */
12217735730dSChao Yu 	unsigned char version;		/* version of the node */
1222458e6197SJaegeuk Kim };
1223458e6197SJaegeuk Kim 
12240b20fcecSChao Yu struct bio_entry {
12250b20fcecSChao Yu 	struct bio *bio;
12260b20fcecSChao Yu 	struct list_head list;
12270b20fcecSChao Yu };
12280b20fcecSChao Yu 
122968afcf2dSTomohiro Kusumi #define is_read_io(rw) ((rw) == READ)
12301ff7bd3bSJaegeuk Kim struct f2fs_bio_info {
1231458e6197SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs superblock */
12321ff7bd3bSJaegeuk Kim 	struct bio *bio;		/* bios to merge */
12331ff7bd3bSJaegeuk Kim 	sector_t last_block_in_bio;	/* last block number */
1234458e6197SJaegeuk Kim 	struct f2fs_io_info fio;	/* store buffered io info. */
1235e4544b63STim Murray 	struct f2fs_rwsem io_rwsem;	/* blocking op for bio */
1236fb830fc5SChao Yu 	spinlock_t io_lock;		/* serialize DATA/NODE IOs */
1237fb830fc5SChao Yu 	struct list_head io_list;	/* track fios */
12380b20fcecSChao Yu 	struct list_head bio_list;	/* bio entry list head */
1239e4544b63STim Murray 	struct f2fs_rwsem bio_list_lock;	/* lock to protect bio entry list */
12401ff7bd3bSJaegeuk Kim };
12411ff7bd3bSJaegeuk Kim 
12423c62be17SJaegeuk Kim #define FDEV(i)				(sbi->devs[i])
12433c62be17SJaegeuk Kim #define RDEV(i)				(raw_super->devs[i])
12443c62be17SJaegeuk Kim struct f2fs_dev_info {
12453c62be17SJaegeuk Kim 	struct block_device *bdev;
12463c62be17SJaegeuk Kim 	char path[MAX_PATH_LEN];
12473c62be17SJaegeuk Kim 	unsigned int total_segments;
12483c62be17SJaegeuk Kim 	block_t start_blk;
12493c62be17SJaegeuk Kim 	block_t end_blk;
12503c62be17SJaegeuk Kim #ifdef CONFIG_BLK_DEV_ZONED
12513c62be17SJaegeuk Kim 	unsigned int nr_blkz;		/* Total number of zones */
125295175dafSDamien Le Moal 	unsigned long *blkz_seq;	/* Bitmap indicating sequential zones */
12533c62be17SJaegeuk Kim #endif
12543c62be17SJaegeuk Kim };
12553c62be17SJaegeuk Kim 
1256c227f912SChao Yu enum inode_type {
1257c227f912SChao Yu 	DIR_INODE,			/* for dirty dir inode */
1258c227f912SChao Yu 	FILE_INODE,			/* for dirty regular/symlink inode */
12590f18b462SJaegeuk Kim 	DIRTY_META,			/* for all dirtied inode metadata */
126057864ae5SJaegeuk Kim 	ATOMIC_FILE,			/* for all atomic files */
1261c227f912SChao Yu 	NR_INODE_TYPE,
1262c227f912SChao Yu };
1263c227f912SChao Yu 
126467298804SChao Yu /* for inner inode cache management */
126567298804SChao Yu struct inode_management {
126667298804SChao Yu 	struct radix_tree_root ino_root;	/* ino entry array */
126767298804SChao Yu 	spinlock_t ino_lock;			/* for ino entry lock */
126867298804SChao Yu 	struct list_head ino_list;		/* inode list head */
126967298804SChao Yu 	unsigned long ino_num;			/* number of entries */
127067298804SChao Yu };
127167298804SChao Yu 
1272093749e2SChao Yu /* for GC_AT */
1273093749e2SChao Yu struct atgc_management {
1274093749e2SChao Yu 	bool atgc_enabled;			/* ATGC is enabled or not */
1275093749e2SChao Yu 	struct rb_root_cached root;		/* root of victim rb-tree */
1276093749e2SChao Yu 	struct list_head victim_list;		/* linked with all victim entries */
1277093749e2SChao Yu 	unsigned int victim_count;		/* victim count in rb-tree */
1278093749e2SChao Yu 	unsigned int candidate_ratio;		/* candidate ratio */
1279093749e2SChao Yu 	unsigned int max_candidate_count;	/* max candidate count */
1280093749e2SChao Yu 	unsigned int age_weight;		/* age weight, vblock_weight = 100 - age_weight */
1281093749e2SChao Yu 	unsigned long long age_threshold;	/* age threshold */
1282093749e2SChao Yu };
1283093749e2SChao Yu 
1284d147ea4aSJaegeuk Kim struct f2fs_gc_control {
1285d147ea4aSJaegeuk Kim 	unsigned int victim_segno;	/* target victim segment number */
1286d147ea4aSJaegeuk Kim 	int init_gc_type;		/* FG_GC or BG_GC */
1287d147ea4aSJaegeuk Kim 	bool no_bg_gc;			/* check the space and stop bg_gc */
1288d147ea4aSJaegeuk Kim 	bool should_migrate_blocks;	/* should migrate blocks */
1289d147ea4aSJaegeuk Kim 	bool err_gc_skipped;		/* return EAGAIN if GC skipped */
1290c81d5baeSJaegeuk Kim 	unsigned int nr_free_secs;	/* # of free sections to do GC */
1291d147ea4aSJaegeuk Kim };
1292d147ea4aSJaegeuk Kim 
1293caf0047eSChao Yu /* For s_flag in struct f2fs_sb_info */
1294caf0047eSChao Yu enum {
1295caf0047eSChao Yu 	SBI_IS_DIRTY,				/* dirty flag for checkpoint */
1296caf0047eSChao Yu 	SBI_IS_CLOSE,				/* specify unmounting */
1297caf0047eSChao Yu 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
1298caf0047eSChao Yu 	SBI_POR_DOING,				/* recovery is doing or not */
1299df728b0fSJaegeuk Kim 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
1300bbf156f7SJaegeuk Kim 	SBI_NEED_CP,				/* need to checkpoint */
130183a3bfdbSJaegeuk Kim 	SBI_IS_SHUTDOWN,			/* shutdown by ioctl */
13021378752bSChao Yu 	SBI_IS_RECOVERED,			/* recovered orphan/data */
13034354994fSDaniel Rosenberg 	SBI_CP_DISABLED,			/* CP was disabled last mount */
1304db610a64SJaegeuk Kim 	SBI_CP_DISABLED_QUICK,			/* CP was disabled quickly */
1305af033b2aSChao Yu 	SBI_QUOTA_NEED_FLUSH,			/* need to flush quota info in CP */
1306af033b2aSChao Yu 	SBI_QUOTA_SKIP_FLUSH,			/* skip flushing quota in current CP */
1307af033b2aSChao Yu 	SBI_QUOTA_NEED_REPAIR,			/* quota file may be corrupted */
130804f0b2eaSQiuyang Sun 	SBI_IS_RESIZEFS,			/* resizefs is in process */
1309ba900534SJaegeuk Kim 	SBI_IS_FREEZING,			/* freezefs is in process */
1310caf0047eSChao Yu };
1311caf0047eSChao Yu 
13126beceb54SJaegeuk Kim enum {
13136beceb54SJaegeuk Kim 	CP_TIME,
1314d0239e1bSJaegeuk Kim 	REQ_TIME,
1315a7d10cf3SSahitya Tummala 	DISCARD_TIME,
1316a7d10cf3SSahitya Tummala 	GC_TIME,
13174354994fSDaniel Rosenberg 	DISABLE_TIME,
131803f2c02dSJaegeuk Kim 	UMOUNT_DISCARD_TIMEOUT,
13196beceb54SJaegeuk Kim 	MAX_TIME,
13206beceb54SJaegeuk Kim };
13216beceb54SJaegeuk Kim 
13220cdd3195SHyunchul Lee enum {
13235b0e9539SJaegeuk Kim 	GC_NORMAL,
13245b0e9539SJaegeuk Kim 	GC_IDLE_CB,
13255b0e9539SJaegeuk Kim 	GC_IDLE_GREEDY,
1326093749e2SChao Yu 	GC_IDLE_AT,
13270e5e8111SDaeho Jeong 	GC_URGENT_HIGH,
13280e5e8111SDaeho Jeong 	GC_URGENT_LOW,
1329d98af5f4SDaeho Jeong 	GC_URGENT_MID,
133007c6b593SDaeho Jeong 	MAX_GC_MODE,
13315b0e9539SJaegeuk Kim };
13325b0e9539SJaegeuk Kim 
13335b0e9539SJaegeuk Kim enum {
1334bbbc34fdSChao Yu 	BGGC_MODE_ON,		/* background gc is on */
1335bbbc34fdSChao Yu 	BGGC_MODE_OFF,		/* background gc is off */
1336bbbc34fdSChao Yu 	BGGC_MODE_SYNC,		/*
1337bbbc34fdSChao Yu 				 * background gc is on, migrating blocks
1338bbbc34fdSChao Yu 				 * like foreground gc
1339bbbc34fdSChao Yu 				 */
1340bbbc34fdSChao Yu };
1341bbbc34fdSChao Yu 
1342bbbc34fdSChao Yu enum {
1343b0332a0fSChao Yu 	FS_MODE_ADAPTIVE,		/* use both lfs/ssr allocation */
1344b0332a0fSChao Yu 	FS_MODE_LFS,			/* use lfs allocation only */
13456691d940SDaeho Jeong 	FS_MODE_FRAGMENT_SEG,		/* segment fragmentation mode */
13466691d940SDaeho Jeong 	FS_MODE_FRAGMENT_BLK,		/* block fragmentation mode */
1347b0332a0fSChao Yu };
1348b0332a0fSChao Yu 
1349b0332a0fSChao Yu enum {
135007939627SJaegeuk Kim 	ALLOC_MODE_DEFAULT,	/* stay default */
135107939627SJaegeuk Kim 	ALLOC_MODE_REUSE,	/* reuse segments as much as possible */
135207939627SJaegeuk Kim };
135307939627SJaegeuk Kim 
135493cf93f1SJunling Zheng enum fsync_mode {
135593cf93f1SJunling Zheng 	FSYNC_MODE_POSIX,	/* fsync follows posix semantics */
135693cf93f1SJunling Zheng 	FSYNC_MODE_STRICT,	/* fsync behaves in line with ext4 */
1357d6290814SJaegeuk Kim 	FSYNC_MODE_NOBARRIER,	/* fsync behaves nobarrier based on posix */
135893cf93f1SJunling Zheng };
135993cf93f1SJunling Zheng 
1360602a16d5SDaeho Jeong enum {
1361602a16d5SDaeho Jeong 	COMPR_MODE_FS,		/*
1362602a16d5SDaeho Jeong 				 * automatically compress compression
1363602a16d5SDaeho Jeong 				 * enabled files
1364602a16d5SDaeho Jeong 				 */
1365602a16d5SDaeho Jeong 	COMPR_MODE_USER,	/*
1366602a16d5SDaeho Jeong 				 * automatical compression is disabled.
1367602a16d5SDaeho Jeong 				 * user can control the file compression
1368602a16d5SDaeho Jeong 				 * using ioctls
1369602a16d5SDaeho Jeong 				 */
1370602a16d5SDaeho Jeong };
1371602a16d5SDaeho Jeong 
13724f993264SChao Yu enum {
13734f993264SChao Yu 	DISCARD_UNIT_BLOCK,	/* basic discard unit is block */
13744f993264SChao Yu 	DISCARD_UNIT_SEGMENT,	/* basic discard unit is segment */
13754f993264SChao Yu 	DISCARD_UNIT_SECTION,	/* basic discard unit is section */
13764f993264SChao Yu };
13774f993264SChao Yu 
13787a8fc586SDaeho Jeong enum {
13797a8fc586SDaeho Jeong 	MEMORY_MODE_NORMAL,	/* memory mode for normal devices */
13807a8fc586SDaeho Jeong 	MEMORY_MODE_LOW,	/* memory mode for low memry devices */
13817a8fc586SDaeho Jeong };
13827a8fc586SDaeho Jeong 
13837a8fc586SDaeho Jeong 
13847a8fc586SDaeho Jeong 
1385b763f3beSChao Yu static inline int f2fs_test_bit(unsigned int nr, char *addr);
1386b763f3beSChao Yu static inline void f2fs_set_bit(unsigned int nr, char *addr);
1387b763f3beSChao Yu static inline void f2fs_clear_bit(unsigned int nr, char *addr);
13884c8ff709SChao Yu 
1389b763f3beSChao Yu /*
1390b763f3beSChao Yu  * Layout of f2fs page.private:
1391b763f3beSChao Yu  *
1392b763f3beSChao Yu  * Layout A: lowest bit should be 1
1393b763f3beSChao Yu  * | bit0 = 1 | bit1 | bit2 | ... | bit MAX | private data .... |
1394b763f3beSChao Yu  * bit 0	PAGE_PRIVATE_NOT_POINTER
1395b763f3beSChao Yu  * bit 1	PAGE_PRIVATE_ATOMIC_WRITE
1396b763f3beSChao Yu  * bit 2	PAGE_PRIVATE_DUMMY_WRITE
1397b763f3beSChao Yu  * bit 3	PAGE_PRIVATE_ONGOING_MIGRATION
1398b763f3beSChao Yu  * bit 4	PAGE_PRIVATE_INLINE_INODE
1399b763f3beSChao Yu  * bit 5	PAGE_PRIVATE_REF_RESOURCE
1400b763f3beSChao Yu  * bit 6-	f2fs private data
1401b763f3beSChao Yu  *
1402b763f3beSChao Yu  * Layout B: lowest bit should be 0
1403b763f3beSChao Yu  * page.private is a wrapped pointer.
1404b763f3beSChao Yu  */
1405b763f3beSChao Yu enum {
1406b763f3beSChao Yu 	PAGE_PRIVATE_NOT_POINTER,		/* private contains non-pointer data */
1407b763f3beSChao Yu 	PAGE_PRIVATE_ATOMIC_WRITE,		/* data page from atomic write path */
1408b763f3beSChao Yu 	PAGE_PRIVATE_DUMMY_WRITE,		/* data page for padding aligned IO */
1409b763f3beSChao Yu 	PAGE_PRIVATE_ONGOING_MIGRATION,		/* data page which is on-going migrating */
1410b763f3beSChao Yu 	PAGE_PRIVATE_INLINE_INODE,		/* inode page contains inline data */
1411b763f3beSChao Yu 	PAGE_PRIVATE_REF_RESOURCE,		/* dirty page has referenced resources */
1412b763f3beSChao Yu 	PAGE_PRIVATE_MAX
1413b763f3beSChao Yu };
1414b763f3beSChao Yu 
1415b763f3beSChao Yu #define PAGE_PRIVATE_GET_FUNC(name, flagname) \
1416b763f3beSChao Yu static inline bool page_private_##name(struct page *page) \
1417b763f3beSChao Yu { \
1418c9ebd3dfSJaegeuk Kim 	return PagePrivate(page) && \
1419c9ebd3dfSJaegeuk Kim 		test_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page)) && \
1420b763f3beSChao Yu 		test_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
1421b763f3beSChao Yu }
1422b763f3beSChao Yu 
1423b763f3beSChao Yu #define PAGE_PRIVATE_SET_FUNC(name, flagname) \
1424b763f3beSChao Yu static inline void set_page_private_##name(struct page *page) \
1425b763f3beSChao Yu { \
1426b763f3beSChao Yu 	if (!PagePrivate(page)) { \
1427b763f3beSChao Yu 		get_page(page); \
1428b763f3beSChao Yu 		SetPagePrivate(page); \
1429c9ebd3dfSJaegeuk Kim 		set_page_private(page, 0); \
1430b763f3beSChao Yu 	} \
1431b763f3beSChao Yu 	set_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page)); \
1432b763f3beSChao Yu 	set_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
1433b763f3beSChao Yu }
1434b763f3beSChao Yu 
1435b763f3beSChao Yu #define PAGE_PRIVATE_CLEAR_FUNC(name, flagname) \
1436b763f3beSChao Yu static inline void clear_page_private_##name(struct page *page) \
1437b763f3beSChao Yu { \
1438b763f3beSChao Yu 	clear_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
1439b763f3beSChao Yu 	if (page_private(page) == 1 << PAGE_PRIVATE_NOT_POINTER) { \
1440b763f3beSChao Yu 		set_page_private(page, 0); \
1441b763f3beSChao Yu 		if (PagePrivate(page)) { \
1442b763f3beSChao Yu 			ClearPagePrivate(page); \
1443b763f3beSChao Yu 			put_page(page); \
1444b763f3beSChao Yu 		}\
1445b763f3beSChao Yu 	} \
1446b763f3beSChao Yu }
1447b763f3beSChao Yu 
1448b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(nonpointer, NOT_POINTER);
1449b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(reference, REF_RESOURCE);
1450b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(inline, INLINE_INODE);
1451b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(gcing, ONGOING_MIGRATION);
1452b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(atomic, ATOMIC_WRITE);
1453b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(dummy, DUMMY_WRITE);
1454b763f3beSChao Yu 
1455b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(reference, REF_RESOURCE);
1456b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(inline, INLINE_INODE);
1457b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(gcing, ONGOING_MIGRATION);
1458b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(atomic, ATOMIC_WRITE);
1459b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(dummy, DUMMY_WRITE);
1460b763f3beSChao Yu 
1461b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(reference, REF_RESOURCE);
1462b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(inline, INLINE_INODE);
1463b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(gcing, ONGOING_MIGRATION);
1464b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(atomic, ATOMIC_WRITE);
1465b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(dummy, DUMMY_WRITE);
14664c8ff709SChao Yu 
14676ce19affSChao Yu static inline unsigned long get_page_private_data(struct page *page)
14686ce19affSChao Yu {
14696ce19affSChao Yu 	unsigned long data = page_private(page);
14706ce19affSChao Yu 
14716ce19affSChao Yu 	if (!test_bit(PAGE_PRIVATE_NOT_POINTER, &data))
14726ce19affSChao Yu 		return 0;
14736ce19affSChao Yu 	return data >> PAGE_PRIVATE_MAX;
14746ce19affSChao Yu }
14756ce19affSChao Yu 
14766ce19affSChao Yu static inline void set_page_private_data(struct page *page, unsigned long data)
14776ce19affSChao Yu {
14786ce19affSChao Yu 	if (!PagePrivate(page)) {
14796ce19affSChao Yu 		get_page(page);
14806ce19affSChao Yu 		SetPagePrivate(page);
1481c9ebd3dfSJaegeuk Kim 		set_page_private(page, 0);
14826ce19affSChao Yu 	}
14836ce19affSChao Yu 	set_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page));
14846ce19affSChao Yu 	page_private(page) |= data << PAGE_PRIVATE_MAX;
14856ce19affSChao Yu }
14866ce19affSChao Yu 
14876ce19affSChao Yu static inline void clear_page_private_data(struct page *page)
14886ce19affSChao Yu {
14896ce19affSChao Yu 	page_private(page) &= (1 << PAGE_PRIVATE_MAX) - 1;
14906ce19affSChao Yu 	if (page_private(page) == 1 << PAGE_PRIVATE_NOT_POINTER) {
14916ce19affSChao Yu 		set_page_private(page, 0);
14926ce19affSChao Yu 		if (PagePrivate(page)) {
14936ce19affSChao Yu 			ClearPagePrivate(page);
14946ce19affSChao Yu 			put_page(page);
14956ce19affSChao Yu 		}
14966ce19affSChao Yu 	}
14976ce19affSChao Yu }
14986ce19affSChao Yu 
14994c8ff709SChao Yu /* For compression */
15004c8ff709SChao Yu enum compress_algorithm_type {
15014c8ff709SChao Yu 	COMPRESS_LZO,
15024c8ff709SChao Yu 	COMPRESS_LZ4,
150350cfa66fSChao Yu 	COMPRESS_ZSTD,
15046d92b201SChao Yu 	COMPRESS_LZORLE,
15054c8ff709SChao Yu 	COMPRESS_MAX,
15064c8ff709SChao Yu };
15074c8ff709SChao Yu 
1508b28f047bSChao Yu enum compress_flag {
1509b28f047bSChao Yu 	COMPRESS_CHKSUM,
1510b28f047bSChao Yu 	COMPRESS_MAX_FLAG,
1511b28f047bSChao Yu };
1512b28f047bSChao Yu 
15136ce19affSChao Yu #define	COMPRESS_WATERMARK			20
15146ce19affSChao Yu #define	COMPRESS_PERCENT			20
15156ce19affSChao Yu 
1516b28f047bSChao Yu #define COMPRESS_DATA_RESERVED_SIZE		4
15174c8ff709SChao Yu struct compress_data {
15184c8ff709SChao Yu 	__le32 clen;			/* compressed data size */
1519b28f047bSChao Yu 	__le32 chksum;			/* compressed data chksum */
15204c8ff709SChao Yu 	__le32 reserved[COMPRESS_DATA_RESERVED_SIZE];	/* reserved */
15214c8ff709SChao Yu 	u8 cdata[];			/* compressed data */
15224c8ff709SChao Yu };
15234c8ff709SChao Yu 
15244c8ff709SChao Yu #define COMPRESS_HEADER_SIZE	(sizeof(struct compress_data))
15254c8ff709SChao Yu 
15264c8ff709SChao Yu #define F2FS_COMPRESSED_PAGE_MAGIC	0xF5F2C000
15274c8ff709SChao Yu 
15283fde13f8SChao Yu #define	COMPRESS_LEVEL_OFFSET	8
15293fde13f8SChao Yu 
15304c8ff709SChao Yu /* compress context */
15314c8ff709SChao Yu struct compress_ctx {
15324c8ff709SChao Yu 	struct inode *inode;		/* inode the context belong to */
15334c8ff709SChao Yu 	pgoff_t cluster_idx;		/* cluster index number */
15344c8ff709SChao Yu 	unsigned int cluster_size;	/* page count in cluster */
15354c8ff709SChao Yu 	unsigned int log_cluster_size;	/* log of cluster size */
15364c8ff709SChao Yu 	struct page **rpages;		/* pages store raw data in cluster */
15374c8ff709SChao Yu 	unsigned int nr_rpages;		/* total page number in rpages */
15384c8ff709SChao Yu 	struct page **cpages;		/* pages store compressed data in cluster */
15394c8ff709SChao Yu 	unsigned int nr_cpages;		/* total page number in cpages */
15403271d7ebSFengnan Chang 	unsigned int valid_nr_cpages;	/* valid page number in cpages */
15414c8ff709SChao Yu 	void *rbuf;			/* virtual mapped address on rpages */
15424c8ff709SChao Yu 	struct compress_data *cbuf;	/* virtual mapped address on cpages */
15434c8ff709SChao Yu 	size_t rlen;			/* valid data length in rbuf */
15444c8ff709SChao Yu 	size_t clen;			/* valid data length in cbuf */
15454c8ff709SChao Yu 	void *private;			/* payload buffer for specified compression algorithm */
154650cfa66fSChao Yu 	void *private2;			/* extra payload buffer */
15474c8ff709SChao Yu };
15484c8ff709SChao Yu 
15494c8ff709SChao Yu /* compress context for write IO path */
15504c8ff709SChao Yu struct compress_io_ctx {
15514c8ff709SChao Yu 	u32 magic;			/* magic number to indicate page is compressed */
15524c8ff709SChao Yu 	struct inode *inode;		/* inode the context belong to */
15534c8ff709SChao Yu 	struct page **rpages;		/* pages store raw data in cluster */
15544c8ff709SChao Yu 	unsigned int nr_rpages;		/* total page number in rpages */
1555e6c3948dSChao Yu 	atomic_t pending_pages;		/* in-flight compressed page count */
15564c8ff709SChao Yu };
15574c8ff709SChao Yu 
15587f59b277SEric Biggers /* Context for decompressing one cluster on the read IO path */
15594c8ff709SChao Yu struct decompress_io_ctx {
15604c8ff709SChao Yu 	u32 magic;			/* magic number to indicate page is compressed */
15614c8ff709SChao Yu 	struct inode *inode;		/* inode the context belong to */
15624c8ff709SChao Yu 	pgoff_t cluster_idx;		/* cluster index number */
15634c8ff709SChao Yu 	unsigned int cluster_size;	/* page count in cluster */
15644c8ff709SChao Yu 	unsigned int log_cluster_size;	/* log of cluster size */
15654c8ff709SChao Yu 	struct page **rpages;		/* pages store raw data in cluster */
15664c8ff709SChao Yu 	unsigned int nr_rpages;		/* total page number in rpages */
15674c8ff709SChao Yu 	struct page **cpages;		/* pages store compressed data in cluster */
15684c8ff709SChao Yu 	unsigned int nr_cpages;		/* total page number in cpages */
15694c8ff709SChao Yu 	struct page **tpages;		/* temp pages to pad holes in cluster */
15704c8ff709SChao Yu 	void *rbuf;			/* virtual mapped address on rpages */
15714c8ff709SChao Yu 	struct compress_data *cbuf;	/* virtual mapped address on cpages */
15724c8ff709SChao Yu 	size_t rlen;			/* valid data length in rbuf */
15734c8ff709SChao Yu 	size_t clen;			/* valid data length in cbuf */
15747f59b277SEric Biggers 
15757f59b277SEric Biggers 	/*
15767f59b277SEric Biggers 	 * The number of compressed pages remaining to be read in this cluster.
15777f59b277SEric Biggers 	 * This is initially nr_cpages.  It is decremented by 1 each time a page
15787f59b277SEric Biggers 	 * has been read (or failed to be read).  When it reaches 0, the cluster
15797f59b277SEric Biggers 	 * is decompressed (or an error is reported).
15807f59b277SEric Biggers 	 *
15817f59b277SEric Biggers 	 * If an error occurs before all the pages have been submitted for I/O,
15827f59b277SEric Biggers 	 * then this will never reach 0.  In this case the I/O submitter is
15837f59b277SEric Biggers 	 * responsible for calling f2fs_decompress_end_io() instead.
15847f59b277SEric Biggers 	 */
15857f59b277SEric Biggers 	atomic_t remaining_pages;
15867f59b277SEric Biggers 
15877f59b277SEric Biggers 	/*
15887f59b277SEric Biggers 	 * Number of references to this decompress_io_ctx.
15897f59b277SEric Biggers 	 *
15907f59b277SEric Biggers 	 * One reference is held for I/O completion.  This reference is dropped
15917f59b277SEric Biggers 	 * after the pagecache pages are updated and unlocked -- either after
15927f59b277SEric Biggers 	 * decompression (and verity if enabled), or after an error.
15937f59b277SEric Biggers 	 *
15947f59b277SEric Biggers 	 * In addition, each compressed page holds a reference while it is in a
15957f59b277SEric Biggers 	 * bio.  These references are necessary prevent compressed pages from
15967f59b277SEric Biggers 	 * being freed while they are still in a bio.
15977f59b277SEric Biggers 	 */
15987f59b277SEric Biggers 	refcount_t refcnt;
15997f59b277SEric Biggers 
16007f59b277SEric Biggers 	bool failed;			/* IO error occurred before decompression? */
16017f59b277SEric Biggers 	bool need_verity;		/* need fs-verity verification after decompression? */
160250cfa66fSChao Yu 	void *private;			/* payload buffer for specified decompression algorithm */
160350cfa66fSChao Yu 	void *private2;			/* extra payload buffer */
16047f59b277SEric Biggers 	struct work_struct verity_work;	/* work to verify the decompressed pages */
1605bff139b4SDaeho Jeong 	struct work_struct free_work;	/* work for late free this structure itself */
16064c8ff709SChao Yu };
16074c8ff709SChao Yu 
16084c8ff709SChao Yu #define NULL_CLUSTER			((unsigned int)(~0))
16094c8ff709SChao Yu #define MIN_COMPRESS_LOG_SIZE		2
16104c8ff709SChao Yu #define MAX_COMPRESS_LOG_SIZE		8
16110e2b7385SChao Yu #define MAX_COMPRESS_WINDOW_SIZE(log_size)	((PAGE_SIZE) << (log_size))
16124c8ff709SChao Yu 
161339a53e0cSJaegeuk Kim struct f2fs_sb_info {
161439a53e0cSJaegeuk Kim 	struct super_block *sb;			/* pointer to VFS super block */
16155e176d54SJaegeuk Kim 	struct proc_dir_entry *s_proc;		/* proc entry */
161639a53e0cSJaegeuk Kim 	struct f2fs_super_block *raw_super;	/* raw super block pointer */
1617e4544b63STim Murray 	struct f2fs_rwsem sb_lock;		/* lock for raw super block */
1618e8240f65SChao Yu 	int valid_super_block;			/* valid super block no */
1619fadb2fb8SChao Yu 	unsigned long s_flag;				/* flags for sbi */
1620853137ceSJaegeuk Kim 	struct mutex writepages;		/* mutex for writepages() */
162139a53e0cSJaegeuk Kim 
1622178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
1623178053e2SDamien Le Moal 	unsigned int blocks_per_blkz;		/* F2FS blocks per zone */
1624178053e2SDamien Le Moal 	unsigned int log_blocks_per_blkz;	/* log2 F2FS blocks per zone */
1625178053e2SDamien Le Moal #endif
1626178053e2SDamien Le Moal 
162739a53e0cSJaegeuk Kim 	/* for node-related operations */
162839a53e0cSJaegeuk Kim 	struct f2fs_nm_info *nm_info;		/* node manager */
162939a53e0cSJaegeuk Kim 	struct inode *node_inode;		/* cache node blocks */
163039a53e0cSJaegeuk Kim 
163139a53e0cSJaegeuk Kim 	/* for segment-related operations */
163239a53e0cSJaegeuk Kim 	struct f2fs_sm_info *sm_info;		/* segment manager */
16331ff7bd3bSJaegeuk Kim 
16341ff7bd3bSJaegeuk Kim 	/* for bio operations */
1635a912b54dSJaegeuk Kim 	struct f2fs_bio_info *write_io[NR_PAGE_TYPE];	/* for write bios */
1636107a805dSChao Yu 	/* keep migration IO order for LFS mode */
1637e4544b63STim Murray 	struct f2fs_rwsem io_order_lock;
16380a595ebaSJaegeuk Kim 	mempool_t *write_io_dummy;		/* Dummy pages */
1639a7b8618aSJaegeuk Kim 	pgoff_t page_eio_ofs[NR_PAGE_TYPE];	/* EIO page offset */
1640a7b8618aSJaegeuk Kim 	int page_eio_cnt[NR_PAGE_TYPE];		/* EIO count */
164139a53e0cSJaegeuk Kim 
164239a53e0cSJaegeuk Kim 	/* for checkpoint */
164339a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt;		/* raw checkpoint pointer */
16448508e44aSJaegeuk Kim 	int cur_cp_pack;			/* remain current cp pack */
1645aaec2b1dSChao Yu 	spinlock_t cp_lock;			/* for flag in ckpt */
164639a53e0cSJaegeuk Kim 	struct inode *meta_inode;		/* cache meta blocks */
1647e4544b63STim Murray 	struct f2fs_rwsem cp_global_sem;	/* checkpoint procedure lock */
1648e4544b63STim Murray 	struct f2fs_rwsem cp_rwsem;		/* blocking FS operations */
1649e4544b63STim Murray 	struct f2fs_rwsem node_write;		/* locking node writes */
1650e4544b63STim Murray 	struct f2fs_rwsem node_change;	/* locking node change */
1651fb51b5efSChangman Lee 	wait_queue_head_t cp_wait;
16526beceb54SJaegeuk Kim 	unsigned long last_time[MAX_TIME];	/* to store time in jiffies */
16536beceb54SJaegeuk Kim 	long interval_time[MAX_TIME];		/* to store thresholds */
1654261eeb9cSDaeho Jeong 	struct ckpt_req_control cprc_info;	/* for checkpoint request control */
165539a53e0cSJaegeuk Kim 
165667298804SChao Yu 	struct inode_management im[MAX_INO_ENTRY];	/* manage inode cache */
16576451e041SJaegeuk Kim 
165850fa53ecSChao Yu 	spinlock_t fsync_node_lock;		/* for node entry lock */
165950fa53ecSChao Yu 	struct list_head fsync_node_list;	/* node list head */
166050fa53ecSChao Yu 	unsigned int fsync_seg_id;		/* sequence id */
166150fa53ecSChao Yu 	unsigned int fsync_node_num;		/* number of node entries */
166250fa53ecSChao Yu 
16636451e041SJaegeuk Kim 	/* for orphan inode, use 0'th array */
16640d47c1adSGu Zheng 	unsigned int max_orphans;		/* max orphan inodes */
166539a53e0cSJaegeuk Kim 
1666c227f912SChao Yu 	/* for inode management */
1667c227f912SChao Yu 	struct list_head inode_list[NR_INODE_TYPE];	/* dirty inode list */
1668c227f912SChao Yu 	spinlock_t inode_lock[NR_INODE_TYPE];	/* for dirty inode list lock */
1669040d2bb3SChao Yu 	struct mutex flush_lock;		/* for flush exclusion */
167039a53e0cSJaegeuk Kim 
167113054c54SChao Yu 	/* for extent tree cache */
167213054c54SChao Yu 	struct radix_tree_root extent_tree_root;/* cache extent cache entries */
16735e8256acSYunlei He 	struct mutex extent_tree_lock;	/* locking extent radix tree */
167413054c54SChao Yu 	struct list_head extent_list;		/* lru list for shrinker */
167513054c54SChao Yu 	spinlock_t extent_lock;			/* locking extent lru list */
16767441ccefSJaegeuk Kim 	atomic_t total_ext_tree;		/* extent tree count */
1677137d09f0SJaegeuk Kim 	struct list_head zombie_list;		/* extent zombie tree list */
167874fd8d99SJaegeuk Kim 	atomic_t total_zombie_tree;		/* extent zombie tree count */
167913054c54SChao Yu 	atomic_t total_ext_node;		/* extent info count */
168013054c54SChao Yu 
168139a53e0cSJaegeuk Kim 	/* basic filesystem units */
168239a53e0cSJaegeuk Kim 	unsigned int log_sectors_per_block;	/* log2 sectors per block */
168339a53e0cSJaegeuk Kim 	unsigned int log_blocksize;		/* log2 block size */
168439a53e0cSJaegeuk Kim 	unsigned int blocksize;			/* block size */
168539a53e0cSJaegeuk Kim 	unsigned int root_ino_num;		/* root inode number*/
168639a53e0cSJaegeuk Kim 	unsigned int node_ino_num;		/* node inode number*/
168739a53e0cSJaegeuk Kim 	unsigned int meta_ino_num;		/* meta inode number*/
168839a53e0cSJaegeuk Kim 	unsigned int log_blocks_per_seg;	/* log2 blocks per segment */
168939a53e0cSJaegeuk Kim 	unsigned int blocks_per_seg;		/* blocks per segment */
1690b771aadcSJaegeuk Kim 	unsigned int unusable_blocks_per_sec;	/* unusable blocks per section */
169139a53e0cSJaegeuk Kim 	unsigned int segs_per_sec;		/* segments per section */
169239a53e0cSJaegeuk Kim 	unsigned int secs_per_zone;		/* sections per zone */
169339a53e0cSJaegeuk Kim 	unsigned int total_sections;		/* total section count */
169439a53e0cSJaegeuk Kim 	unsigned int total_node_count;		/* total node block count */
169539a53e0cSJaegeuk Kim 	unsigned int total_valid_node_count;	/* valid node block count */
1696ab9fa662SJaegeuk Kim 	int dir_level;				/* directory level */
1697f6df8f23SSheng Yong 	int readdir_ra;				/* readahead inode in readdir */
169810208567SJaegeuk Kim 	u64 max_io_bytes;			/* max io bytes to merge IOs */
169939a53e0cSJaegeuk Kim 
170039a53e0cSJaegeuk Kim 	block_t user_block_count;		/* # of user blocks */
170139a53e0cSJaegeuk Kim 	block_t total_valid_block_count;	/* # of valid blocks */
1702a66cdd98SJaegeuk Kim 	block_t discard_blks;			/* discard command candidats */
170339a53e0cSJaegeuk Kim 	block_t last_valid_block_count;		/* for recovery */
1704daeb433eSChao Yu 	block_t reserved_blocks;		/* configurable reserved blocks */
170580d42145SYunlong Song 	block_t current_reserved_blocks;	/* current reserved blocks */
1706daeb433eSChao Yu 
17074354994fSDaniel Rosenberg 	/* Additional tracking for no checkpoint mode */
17084354994fSDaniel Rosenberg 	block_t unusable_block_count;		/* # of blocks saved by last cp */
17094354994fSDaniel Rosenberg 
1710292c196aSChao Yu 	unsigned int nquota_files;		/* # of quota sysfile */
1711e4544b63STim Murray 	struct f2fs_rwsem quota_sem;		/* blocking cp for flags */
1712292c196aSChao Yu 
1713523be8a6SJaegeuk Kim 	/* # of pages, see count_type */
171435782b23SJaegeuk Kim 	atomic_t nr_pages[NR_COUNT_TYPE];
171541382ec4SJaegeuk Kim 	/* # of allocated blocks */
171641382ec4SJaegeuk Kim 	struct percpu_counter alloc_valid_block_count;
171747c8ebccSJaegeuk Kim 	/* # of node block writes as roll forward recovery */
171847c8ebccSJaegeuk Kim 	struct percpu_counter rf_node_block_count;
171939a53e0cSJaegeuk Kim 
1720687de7f1SJaegeuk Kim 	/* writeback control */
1721c29fd0c0SChao Yu 	atomic_t wb_sync_req[META];	/* count # of WB_SYNC threads */
1722687de7f1SJaegeuk Kim 
1723513c5f37SJaegeuk Kim 	/* valid inode count */
1724513c5f37SJaegeuk Kim 	struct percpu_counter total_valid_inode_count;
1725513c5f37SJaegeuk Kim 
172639a53e0cSJaegeuk Kim 	struct f2fs_mount_info mount_opt;	/* mount options */
172739a53e0cSJaegeuk Kim 
172839a53e0cSJaegeuk Kim 	/* for cleaning operations */
1729e4544b63STim Murray 	struct f2fs_rwsem gc_lock;		/*
1730fb24fea7SChao Yu 						 * semaphore for GC, avoid
1731fb24fea7SChao Yu 						 * race between GC and GC or CP
1732fb24fea7SChao Yu 						 */
173339a53e0cSJaegeuk Kim 	struct f2fs_gc_kthread	*gc_thread;	/* GC thread */
1734093749e2SChao Yu 	struct atgc_management am;		/* atgc management */
17355ec4e49fSJaegeuk Kim 	unsigned int cur_victim_sec;		/* current victim section num */
17365b0e9539SJaegeuk Kim 	unsigned int gc_mode;			/* current GC state */
1737e3080b01SChao Yu 	unsigned int next_victim_seg[2];	/* next segment in victim section */
1738325163e9SDaeho Jeong 	spinlock_t gc_urgent_high_lock;
1739325163e9SDaeho Jeong 	unsigned int gc_urgent_high_remaining;	/* remaining trial count for GC_URGENT_HIGH */
17400e5e8111SDaeho Jeong 
17412ef79ecbSChao Yu 	/* for skip statistic */
1742677017d1SSahitya Tummala 	unsigned int atomic_files;		/* # of opened atomic file */
17436f8d4455SJaegeuk Kim 	unsigned long long skipped_gc_rwsem;		/* FG_GC only */
174439a53e0cSJaegeuk Kim 
17451ad71a27SJaegeuk Kim 	/* threshold for gc trials on pinned files */
17461ad71a27SJaegeuk Kim 	u64 gc_pin_file_threshold;
1747e4544b63STim Murray 	struct f2fs_rwsem pin_sem;
17481ad71a27SJaegeuk Kim 
1749b1c57c1cSJaegeuk Kim 	/* maximum # of trials to find a victim segment for SSR and GC */
1750b1c57c1cSJaegeuk Kim 	unsigned int max_victim_search;
1751e3080b01SChao Yu 	/* migration granularity of garbage collection, unit: segment */
1752e3080b01SChao Yu 	unsigned int migration_granularity;
1753b1c57c1cSJaegeuk Kim 
175439a53e0cSJaegeuk Kim 	/*
175539a53e0cSJaegeuk Kim 	 * for stat information.
175639a53e0cSJaegeuk Kim 	 * one is for the LFS mode, and the other is for the SSR mode.
175739a53e0cSJaegeuk Kim 	 */
175835b09d82SNamjae Jeon #ifdef CONFIG_F2FS_STAT_FS
175939a53e0cSJaegeuk Kim 	struct f2fs_stat_info *stat_info;	/* FS status information */
1760b63e7be5SChao Yu 	atomic_t meta_count[META_MAX];		/* # of meta blocks */
176139a53e0cSJaegeuk Kim 	unsigned int segment_count[2];		/* # of allocated segments */
176239a53e0cSJaegeuk Kim 	unsigned int block_count[2];		/* # of allocated blocks */
1763b9a2c252SChangman Lee 	atomic_t inplace_count;		/* # of inplace update */
17645b7ee374SChao Yu 	atomic64_t total_hit_ext;		/* # of lookup extent cache */
17655b7ee374SChao Yu 	atomic64_t read_hit_rbtree;		/* # of hit rbtree extent node */
17665b7ee374SChao Yu 	atomic64_t read_hit_largest;		/* # of hit largest extent node */
17675b7ee374SChao Yu 	atomic64_t read_hit_cached;		/* # of hit cached extent node */
1768d5e8f6c9SChao Yu 	atomic_t inline_xattr;			/* # of inline_xattr inodes */
176903e14d52SChao Yu 	atomic_t inline_inode;			/* # of inline_data inodes */
177003e14d52SChao Yu 	atomic_t inline_dir;			/* # of inline_dentry inodes */
17714c8ff709SChao Yu 	atomic_t compr_inode;			/* # of compressed inodes */
1772ae999bb9SDaeho Jeong 	atomic64_t compr_blocks;		/* # of compressed blocks */
177326a28a0cSJaegeuk Kim 	atomic_t max_aw_cnt;			/* max # of atomic writes */
1774274bd9baSChao Yu 	unsigned int io_skip_bggc;		/* skip background gc for in-flight IO */
1775274bd9baSChao Yu 	unsigned int other_skip_bggc;		/* skip background gc for other reasons */
177633fbd510SChao Yu 	unsigned int ndirty_inode[NR_INODE_TYPE];	/* # of dirty inodes */
177735b09d82SNamjae Jeon #endif
177839a53e0cSJaegeuk Kim 	spinlock_t stat_lock;			/* lock for stat operations */
1779b59d0baeSNamjae Jeon 
1780da9953b7SJaegeuk Kim 	/* to attach REQ_META|REQ_FUA flags */
1781da9953b7SJaegeuk Kim 	unsigned int data_io_flag;
178232b6aba8SJaegeuk Kim 	unsigned int node_io_flag;
1783b0af6d49SChao Yu 
1784759820c9SJulia Lawall 	/* For sysfs support */
17855d4daa57SChao Yu 	struct kobject s_kobj;			/* /sys/fs/f2fs/<devname> */
1786b59d0baeSNamjae Jeon 	struct completion s_kobj_unregister;
17872658e50dSJaegeuk Kim 
17885d4daa57SChao Yu 	struct kobject s_stat_kobj;		/* /sys/fs/f2fs/<devname>/stat */
17895d4daa57SChao Yu 	struct completion s_stat_kobj_unregister;
17905d4daa57SChao Yu 
17914c89b53dSJaegeuk Kim 	struct kobject s_feature_list_kobj;		/* /sys/fs/f2fs/<devname>/feature_list */
17924c89b53dSJaegeuk Kim 	struct completion s_feature_list_kobj_unregister;
17934c89b53dSJaegeuk Kim 
17942658e50dSJaegeuk Kim 	/* For shrinker support */
17952658e50dSJaegeuk Kim 	struct list_head s_list;
179671f2c820SChao Yu 	struct mutex umount_mutex;
179771f2c820SChao Yu 	unsigned int shrinker_run_no;
179871f2c820SChao Yu 
179971f2c820SChao Yu 	/* For multi devices */
18003c62be17SJaegeuk Kim 	int s_ndevs;				/* number of devices */
18013c62be17SJaegeuk Kim 	struct f2fs_dev_info *devs;		/* for device list */
18021228b482SChao Yu 	unsigned int dirty_device;		/* for checkpoint data flush */
18031228b482SChao Yu 	spinlock_t dev_lock;			/* protect dirty_device */
180471f2c820SChao Yu 	bool aligned_blksize;			/* all devices has the same logical blksize */
18058f1dbbbbSShuoran Liu 
18068f1dbbbbSShuoran Liu 	/* For write statistics */
18078f1dbbbbSShuoran Liu 	u64 sectors_written_start;
18088f1dbbbbSShuoran Liu 	u64 kbytes_written;
180943b6573bSKeith Mok 
181043b6573bSKeith Mok 	/* Reference to checksum algorithm driver via cryptoapi */
181143b6573bSKeith Mok 	struct crypto_shash *s_chksum_driver;
18121ecc0c5cSChao Yu 
1813704956ecSChao Yu 	/* Precomputed FS UUID checksum for seeding other checksums */
1814704956ecSChao Yu 	__u32 s_chksum_seed;
18154c8ff709SChao Yu 
18164c8ff709SChao Yu 	struct workqueue_struct *post_read_wq;	/* post read workqueue */
1817a999150fSChao Yu 
1818*95fa90c9SChao Yu 	unsigned char errors[MAX_F2FS_ERRORS];	/* error flags */
1819*95fa90c9SChao Yu 	spinlock_t error_lock;			/* protect errors array */
1820*95fa90c9SChao Yu 	bool error_dirty;			/* errors of sb is dirty */
1821*95fa90c9SChao Yu 
1822a999150fSChao Yu 	struct kmem_cache *inline_xattr_slab;	/* inline xattr entry */
1823a999150fSChao Yu 	unsigned int inline_xattr_slab_size;	/* default inline xattr slab size */
182431083031SChao Yu 
182507c6b593SDaeho Jeong 	/* For reclaimed segs statistics per each GC mode */
182607c6b593SDaeho Jeong 	unsigned int gc_segment_mode;		/* GC state for reclaimed segments */
182707c6b593SDaeho Jeong 	unsigned int gc_reclaimed_segs[MAX_GC_MODE];	/* Reclaimed segs for each mode */
182807c6b593SDaeho Jeong 
18290f6b56ecSDaeho Jeong 	unsigned long seq_file_ra_mul;		/* multiplier for ra_pages of seq. files in fadvise */
18300f6b56ecSDaeho Jeong 
18316691d940SDaeho Jeong 	int max_fragment_chunk;			/* max chunk size for block fragmentation mode */
18326691d940SDaeho Jeong 	int max_fragment_hole;			/* max hole size for block fragmentation mode */
18336691d940SDaeho Jeong 
1834f8e2f32bSDaeho Jeong 	/* For atomic write statistics */
1835f8e2f32bSDaeho Jeong 	atomic64_t current_atomic_write;
1836f8e2f32bSDaeho Jeong 	s64 peak_atomic_write;
1837f8e2f32bSDaeho Jeong 	u64 committed_atomic_block;
1838f8e2f32bSDaeho Jeong 	u64 revoked_atomic_block;
1839f8e2f32bSDaeho Jeong 
184031083031SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
184131083031SChao Yu 	struct kmem_cache *page_array_slab;	/* page array entry */
184231083031SChao Yu 	unsigned int page_array_slab_size;	/* default page array slab size */
18435ac443e2SDaeho Jeong 
18445ac443e2SDaeho Jeong 	/* For runtime compression statistics */
18455ac443e2SDaeho Jeong 	u64 compr_written_block;
18465ac443e2SDaeho Jeong 	u64 compr_saved_block;
18475ac443e2SDaeho Jeong 	u32 compr_new_inode;
18486ce19affSChao Yu 
18496ce19affSChao Yu 	/* For compressed block cache */
18506ce19affSChao Yu 	struct inode *compress_inode;		/* cache compressed blocks */
18516ce19affSChao Yu 	unsigned int compress_percent;		/* cache page percentage */
18526ce19affSChao Yu 	unsigned int compress_watermark;	/* cache page watermark */
18536ce19affSChao Yu 	atomic_t compress_page_hit;		/* cache hit count */
185431083031SChao Yu #endif
185552118743SDaeho Jeong 
185652118743SDaeho Jeong #ifdef CONFIG_F2FS_IOSTAT
185752118743SDaeho Jeong 	/* For app/fs IO statistics */
185852118743SDaeho Jeong 	spinlock_t iostat_lock;
185952118743SDaeho Jeong 	unsigned long long rw_iostat[NR_IO_TYPE];
186052118743SDaeho Jeong 	unsigned long long prev_rw_iostat[NR_IO_TYPE];
186152118743SDaeho Jeong 	bool iostat_enable;
186252118743SDaeho Jeong 	unsigned long iostat_next_period;
186352118743SDaeho Jeong 	unsigned int iostat_period_ms;
1864a4b68176SDaeho Jeong 
1865a4b68176SDaeho Jeong 	/* For io latency related statistics info in one iostat period */
1866a4b68176SDaeho Jeong 	spinlock_t iostat_lat_lock;
1867a4b68176SDaeho Jeong 	struct iostat_lat_info *iostat_io_lat;
186852118743SDaeho Jeong #endif
186939a53e0cSJaegeuk Kim };
187039a53e0cSJaegeuk Kim 
18711ecc0c5cSChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
1872c45d6002SChao Yu #define f2fs_show_injection_info(sbi, type)					\
1873c45d6002SChao Yu 	printk_ratelimited("%sF2FS-fs (%s) : inject %s in %s of %pS\n",	\
1874c45d6002SChao Yu 		KERN_INFO, sbi->sb->s_id,				\
1875c45d6002SChao Yu 		f2fs_fault_name[type],					\
187655523519SChao Yu 		__func__, __builtin_return_address(0))
18771ecc0c5cSChao Yu static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
18781ecc0c5cSChao Yu {
187963189b78SChao Yu 	struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
18801ecc0c5cSChao Yu 
18811ecc0c5cSChao Yu 	if (!ffi->inject_rate)
18821ecc0c5cSChao Yu 		return false;
18831ecc0c5cSChao Yu 
18841ecc0c5cSChao Yu 	if (!IS_FAULT_SET(ffi, type))
18851ecc0c5cSChao Yu 		return false;
18861ecc0c5cSChao Yu 
18871ecc0c5cSChao Yu 	atomic_inc(&ffi->inject_ops);
18881ecc0c5cSChao Yu 	if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
18891ecc0c5cSChao Yu 		atomic_set(&ffi->inject_ops, 0);
18901ecc0c5cSChao Yu 		return true;
18911ecc0c5cSChao Yu 	}
18921ecc0c5cSChao Yu 	return false;
18931ecc0c5cSChao Yu }
18947fa750a1SArnd Bergmann #else
1895c45d6002SChao Yu #define f2fs_show_injection_info(sbi, type) do { } while (0)
18967fa750a1SArnd Bergmann static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
18977fa750a1SArnd Bergmann {
18987fa750a1SArnd Bergmann 	return false;
18997fa750a1SArnd Bergmann }
19001ecc0c5cSChao Yu #endif
19011ecc0c5cSChao Yu 
19020916878dSDamien Le Moal /*
19030916878dSDamien Le Moal  * Test if the mounted volume is a multi-device volume.
19040916878dSDamien Le Moal  *   - For a single regular disk volume, sbi->s_ndevs is 0.
19050916878dSDamien Le Moal  *   - For a single zoned disk volume, sbi->s_ndevs is 1.
19060916878dSDamien Le Moal  *   - For a multi-device volume, sbi->s_ndevs is always 2 or more.
19070916878dSDamien Le Moal  */
19080916878dSDamien Le Moal static inline bool f2fs_is_multi_device(struct f2fs_sb_info *sbi)
19090916878dSDamien Le Moal {
19100916878dSDamien Le Moal 	return sbi->s_ndevs > 1;
19110916878dSDamien Le Moal }
19120916878dSDamien Le Moal 
19136beceb54SJaegeuk Kim static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
19146beceb54SJaegeuk Kim {
1915a7d10cf3SSahitya Tummala 	unsigned long now = jiffies;
1916a7d10cf3SSahitya Tummala 
1917a7d10cf3SSahitya Tummala 	sbi->last_time[type] = now;
1918a7d10cf3SSahitya Tummala 
1919a7d10cf3SSahitya Tummala 	/* DISCARD_TIME and GC_TIME are based on REQ_TIME */
1920a7d10cf3SSahitya Tummala 	if (type == REQ_TIME) {
1921a7d10cf3SSahitya Tummala 		sbi->last_time[DISCARD_TIME] = now;
1922a7d10cf3SSahitya Tummala 		sbi->last_time[GC_TIME] = now;
1923a7d10cf3SSahitya Tummala 	}
19246beceb54SJaegeuk Kim }
19256beceb54SJaegeuk Kim 
19266beceb54SJaegeuk Kim static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
19276beceb54SJaegeuk Kim {
19286bccfa19SArnd Bergmann 	unsigned long interval = sbi->interval_time[type] * HZ;
19296beceb54SJaegeuk Kim 
19306beceb54SJaegeuk Kim 	return time_after(jiffies, sbi->last_time[type] + interval);
19316beceb54SJaegeuk Kim }
19326beceb54SJaegeuk Kim 
1933a7d10cf3SSahitya Tummala static inline unsigned int f2fs_time_to_wait(struct f2fs_sb_info *sbi,
1934a7d10cf3SSahitya Tummala 						int type)
1935a7d10cf3SSahitya Tummala {
1936a7d10cf3SSahitya Tummala 	unsigned long interval = sbi->interval_time[type] * HZ;
1937a7d10cf3SSahitya Tummala 	unsigned int wait_ms = 0;
1938a7d10cf3SSahitya Tummala 	long delta;
1939a7d10cf3SSahitya Tummala 
1940a7d10cf3SSahitya Tummala 	delta = (sbi->last_time[type] + interval) - jiffies;
1941a7d10cf3SSahitya Tummala 	if (delta > 0)
1942a7d10cf3SSahitya Tummala 		wait_ms = jiffies_to_msecs(delta);
1943a7d10cf3SSahitya Tummala 
1944a7d10cf3SSahitya Tummala 	return wait_ms;
1945a7d10cf3SSahitya Tummala }
1946a7d10cf3SSahitya Tummala 
194739a53e0cSJaegeuk Kim /*
194839a53e0cSJaegeuk Kim  * Inline functions
194939a53e0cSJaegeuk Kim  */
1950416d2dbbSChao Yu static inline u32 __f2fs_crc32(struct f2fs_sb_info *sbi, u32 crc,
1951704956ecSChao Yu 			      const void *address, unsigned int length)
1952704956ecSChao Yu {
1953704956ecSChao Yu 	struct {
1954704956ecSChao Yu 		struct shash_desc shash;
1955704956ecSChao Yu 		char ctx[4];
1956704956ecSChao Yu 	} desc;
1957704956ecSChao Yu 	int err;
1958704956ecSChao Yu 
1959704956ecSChao Yu 	BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver) != sizeof(desc.ctx));
1960704956ecSChao Yu 
1961704956ecSChao Yu 	desc.shash.tfm = sbi->s_chksum_driver;
1962704956ecSChao Yu 	*(u32 *)desc.ctx = crc;
1963704956ecSChao Yu 
1964704956ecSChao Yu 	err = crypto_shash_update(&desc.shash, address, length);
1965704956ecSChao Yu 	BUG_ON(err);
1966704956ecSChao Yu 
1967704956ecSChao Yu 	return *(u32 *)desc.ctx;
1968704956ecSChao Yu }
1969704956ecSChao Yu 
1970416d2dbbSChao Yu static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
1971416d2dbbSChao Yu 			   unsigned int length)
1972416d2dbbSChao Yu {
1973416d2dbbSChao Yu 	return __f2fs_crc32(sbi, F2FS_SUPER_MAGIC, address, length);
1974416d2dbbSChao Yu }
1975416d2dbbSChao Yu 
1976416d2dbbSChao Yu static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
1977416d2dbbSChao Yu 				  void *buf, size_t buf_size)
1978416d2dbbSChao Yu {
1979416d2dbbSChao Yu 	return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
1980416d2dbbSChao Yu }
1981416d2dbbSChao Yu 
1982416d2dbbSChao Yu static inline u32 f2fs_chksum(struct f2fs_sb_info *sbi, u32 crc,
1983416d2dbbSChao Yu 			      const void *address, unsigned int length)
1984416d2dbbSChao Yu {
1985416d2dbbSChao Yu 	return __f2fs_crc32(sbi, crc, address, length);
1986416d2dbbSChao Yu }
1987416d2dbbSChao Yu 
198839a53e0cSJaegeuk Kim static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
198939a53e0cSJaegeuk Kim {
199039a53e0cSJaegeuk Kim 	return container_of(inode, struct f2fs_inode_info, vfs_inode);
199139a53e0cSJaegeuk Kim }
199239a53e0cSJaegeuk Kim 
199339a53e0cSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
199439a53e0cSJaegeuk Kim {
199539a53e0cSJaegeuk Kim 	return sb->s_fs_info;
199639a53e0cSJaegeuk Kim }
199739a53e0cSJaegeuk Kim 
19984081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
19994081363fSJaegeuk Kim {
20004081363fSJaegeuk Kim 	return F2FS_SB(inode->i_sb);
20014081363fSJaegeuk Kim }
20024081363fSJaegeuk Kim 
20034081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
20044081363fSJaegeuk Kim {
20054081363fSJaegeuk Kim 	return F2FS_I_SB(mapping->host);
20064081363fSJaegeuk Kim }
20074081363fSJaegeuk Kim 
20084081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
20094081363fSJaegeuk Kim {
20104969c06aSJaegeuk Kim 	return F2FS_M_SB(page_file_mapping(page));
20114081363fSJaegeuk Kim }
20124081363fSJaegeuk Kim 
201339a53e0cSJaegeuk Kim static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
201439a53e0cSJaegeuk Kim {
201539a53e0cSJaegeuk Kim 	return (struct f2fs_super_block *)(sbi->raw_super);
201639a53e0cSJaegeuk Kim }
201739a53e0cSJaegeuk Kim 
201839a53e0cSJaegeuk Kim static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
201939a53e0cSJaegeuk Kim {
202039a53e0cSJaegeuk Kim 	return (struct f2fs_checkpoint *)(sbi->ckpt);
202139a53e0cSJaegeuk Kim }
202239a53e0cSJaegeuk Kim 
202345590710SGu Zheng static inline struct f2fs_node *F2FS_NODE(struct page *page)
202445590710SGu Zheng {
202545590710SGu Zheng 	return (struct f2fs_node *)page_address(page);
202645590710SGu Zheng }
202745590710SGu Zheng 
202858bfaf44SJaegeuk Kim static inline struct f2fs_inode *F2FS_INODE(struct page *page)
202958bfaf44SJaegeuk Kim {
203058bfaf44SJaegeuk Kim 	return &((struct f2fs_node *)page_address(page))->i;
203158bfaf44SJaegeuk Kim }
203258bfaf44SJaegeuk Kim 
203339a53e0cSJaegeuk Kim static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
203439a53e0cSJaegeuk Kim {
203539a53e0cSJaegeuk Kim 	return (struct f2fs_nm_info *)(sbi->nm_info);
203639a53e0cSJaegeuk Kim }
203739a53e0cSJaegeuk Kim 
203839a53e0cSJaegeuk Kim static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
203939a53e0cSJaegeuk Kim {
204039a53e0cSJaegeuk Kim 	return (struct f2fs_sm_info *)(sbi->sm_info);
204139a53e0cSJaegeuk Kim }
204239a53e0cSJaegeuk Kim 
204339a53e0cSJaegeuk Kim static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
204439a53e0cSJaegeuk Kim {
204539a53e0cSJaegeuk Kim 	return (struct sit_info *)(SM_I(sbi)->sit_info);
204639a53e0cSJaegeuk Kim }
204739a53e0cSJaegeuk Kim 
204839a53e0cSJaegeuk Kim static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
204939a53e0cSJaegeuk Kim {
205039a53e0cSJaegeuk Kim 	return (struct free_segmap_info *)(SM_I(sbi)->free_info);
205139a53e0cSJaegeuk Kim }
205239a53e0cSJaegeuk Kim 
205339a53e0cSJaegeuk Kim static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
205439a53e0cSJaegeuk Kim {
205539a53e0cSJaegeuk Kim 	return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
205639a53e0cSJaegeuk Kim }
205739a53e0cSJaegeuk Kim 
20589df27d98SGu Zheng static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
20599df27d98SGu Zheng {
20609df27d98SGu Zheng 	return sbi->meta_inode->i_mapping;
20619df27d98SGu Zheng }
20629df27d98SGu Zheng 
20634ef51a8fSJaegeuk Kim static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
20644ef51a8fSJaegeuk Kim {
20654ef51a8fSJaegeuk Kim 	return sbi->node_inode->i_mapping;
20664ef51a8fSJaegeuk Kim }
20674ef51a8fSJaegeuk Kim 
2068caf0047eSChao Yu static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
206939a53e0cSJaegeuk Kim {
2070fadb2fb8SChao Yu 	return test_bit(type, &sbi->s_flag);
207139a53e0cSJaegeuk Kim }
207239a53e0cSJaegeuk Kim 
2073caf0047eSChao Yu static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
207439a53e0cSJaegeuk Kim {
2075fadb2fb8SChao Yu 	set_bit(type, &sbi->s_flag);
2076caf0047eSChao Yu }
2077caf0047eSChao Yu 
2078caf0047eSChao Yu static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
2079caf0047eSChao Yu {
2080fadb2fb8SChao Yu 	clear_bit(type, &sbi->s_flag);
208139a53e0cSJaegeuk Kim }
208239a53e0cSJaegeuk Kim 
2083d71b5564SJaegeuk Kim static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
2084d71b5564SJaegeuk Kim {
2085d71b5564SJaegeuk Kim 	return le64_to_cpu(cp->checkpoint_ver);
2086d71b5564SJaegeuk Kim }
2087d71b5564SJaegeuk Kim 
2088ea676733SJaegeuk Kim static inline unsigned long f2fs_qf_ino(struct super_block *sb, int type)
2089ea676733SJaegeuk Kim {
2090ea676733SJaegeuk Kim 	if (type < F2FS_MAX_QUOTAS)
2091ea676733SJaegeuk Kim 		return le32_to_cpu(F2FS_SB(sb)->raw_super->qf_ino[type]);
2092ea676733SJaegeuk Kim 	return 0;
2093ea676733SJaegeuk Kim }
2094ea676733SJaegeuk Kim 
2095ced2c7eaSKinglong Mee static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
2096ced2c7eaSKinglong Mee {
2097ced2c7eaSKinglong Mee 	size_t crc_offset = le32_to_cpu(cp->checksum_offset);
2098ced2c7eaSKinglong Mee 	return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
2099ced2c7eaSKinglong Mee }
2100ced2c7eaSKinglong Mee 
2101aaec2b1dSChao Yu static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
210225ca923bSJaegeuk Kim {
210325ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
2104aaec2b1dSChao Yu 
210525ca923bSJaegeuk Kim 	return ckpt_flags & f;
210625ca923bSJaegeuk Kim }
210725ca923bSJaegeuk Kim 
2108aaec2b1dSChao Yu static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
210925ca923bSJaegeuk Kim {
2110aaec2b1dSChao Yu 	return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
2111aaec2b1dSChao Yu }
2112aaec2b1dSChao Yu 
2113aaec2b1dSChao Yu static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
2114aaec2b1dSChao Yu {
2115aaec2b1dSChao Yu 	unsigned int ckpt_flags;
2116aaec2b1dSChao Yu 
2117aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
211825ca923bSJaegeuk Kim 	ckpt_flags |= f;
211925ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
212025ca923bSJaegeuk Kim }
212125ca923bSJaegeuk Kim 
2122aaec2b1dSChao Yu static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
212325ca923bSJaegeuk Kim {
2124d1aa2453SChao Yu 	unsigned long flags;
2125d1aa2453SChao Yu 
2126d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
2127aaec2b1dSChao Yu 	__set_ckpt_flags(F2FS_CKPT(sbi), f);
2128d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
2129aaec2b1dSChao Yu }
2130aaec2b1dSChao Yu 
2131aaec2b1dSChao Yu static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
2132aaec2b1dSChao Yu {
2133aaec2b1dSChao Yu 	unsigned int ckpt_flags;
2134aaec2b1dSChao Yu 
2135aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
213625ca923bSJaegeuk Kim 	ckpt_flags &= (~f);
213725ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
213825ca923bSJaegeuk Kim }
213925ca923bSJaegeuk Kim 
2140aaec2b1dSChao Yu static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
2141aaec2b1dSChao Yu {
2142d1aa2453SChao Yu 	unsigned long flags;
2143d1aa2453SChao Yu 
2144d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
2145aaec2b1dSChao Yu 	__clear_ckpt_flags(F2FS_CKPT(sbi), f);
2146d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
2147aaec2b1dSChao Yu }
2148aaec2b1dSChao Yu 
2149c7f91bd4SBart Van Assche #define init_f2fs_rwsem(sem)					\
2150c7f91bd4SBart Van Assche do {								\
2151c7f91bd4SBart Van Assche 	static struct lock_class_key __key;			\
2152c7f91bd4SBart Van Assche 								\
2153c7f91bd4SBart Van Assche 	__init_f2fs_rwsem((sem), #sem, &__key);			\
2154c7f91bd4SBart Van Assche } while (0)
2155c7f91bd4SBart Van Assche 
2156c7f91bd4SBart Van Assche static inline void __init_f2fs_rwsem(struct f2fs_rwsem *sem,
2157c7f91bd4SBart Van Assche 		const char *sem_name, struct lock_class_key *key)
2158e4544b63STim Murray {
2159c7f91bd4SBart Van Assche 	__init_rwsem(&sem->internal_rwsem, sem_name, key);
21607f8e249dSJaegeuk Kim #ifdef CONFIG_F2FS_UNFAIR_RWSEM
2161e4544b63STim Murray 	init_waitqueue_head(&sem->read_waiters);
21627f8e249dSJaegeuk Kim #endif
2163e4544b63STim Murray }
2164e4544b63STim Murray 
2165e4544b63STim Murray static inline int f2fs_rwsem_is_locked(struct f2fs_rwsem *sem)
2166e4544b63STim Murray {
2167e4544b63STim Murray 	return rwsem_is_locked(&sem->internal_rwsem);
2168e4544b63STim Murray }
2169e4544b63STim Murray 
2170e4544b63STim Murray static inline int f2fs_rwsem_is_contended(struct f2fs_rwsem *sem)
2171e4544b63STim Murray {
2172e4544b63STim Murray 	return rwsem_is_contended(&sem->internal_rwsem);
2173e4544b63STim Murray }
2174e4544b63STim Murray 
2175e4544b63STim Murray static inline void f2fs_down_read(struct f2fs_rwsem *sem)
2176e4544b63STim Murray {
21777f8e249dSJaegeuk Kim #ifdef CONFIG_F2FS_UNFAIR_RWSEM
2178e4544b63STim Murray 	wait_event(sem->read_waiters, down_read_trylock(&sem->internal_rwsem));
21797f8e249dSJaegeuk Kim #else
21807f8e249dSJaegeuk Kim 	down_read(&sem->internal_rwsem);
21817f8e249dSJaegeuk Kim #endif
2182e4544b63STim Murray }
2183e4544b63STim Murray 
2184e4544b63STim Murray static inline int f2fs_down_read_trylock(struct f2fs_rwsem *sem)
2185e4544b63STim Murray {
2186e4544b63STim Murray 	return down_read_trylock(&sem->internal_rwsem);
2187e4544b63STim Murray }
2188e4544b63STim Murray 
2189e4544b63STim Murray #ifdef CONFIG_DEBUG_LOCK_ALLOC
2190e4544b63STim Murray static inline void f2fs_down_read_nested(struct f2fs_rwsem *sem, int subclass)
2191e4544b63STim Murray {
2192e4544b63STim Murray 	down_read_nested(&sem->internal_rwsem, subclass);
2193e4544b63STim Murray }
2194e4544b63STim Murray #else
2195e4544b63STim Murray #define f2fs_down_read_nested(sem, subclass) f2fs_down_read(sem)
2196e4544b63STim Murray #endif
2197e4544b63STim Murray 
2198e4544b63STim Murray static inline void f2fs_up_read(struct f2fs_rwsem *sem)
2199e4544b63STim Murray {
2200e4544b63STim Murray 	up_read(&sem->internal_rwsem);
2201e4544b63STim Murray }
2202e4544b63STim Murray 
2203e4544b63STim Murray static inline void f2fs_down_write(struct f2fs_rwsem *sem)
2204e4544b63STim Murray {
2205e4544b63STim Murray 	down_write(&sem->internal_rwsem);
2206e4544b63STim Murray }
2207e4544b63STim Murray 
2208e4544b63STim Murray static inline int f2fs_down_write_trylock(struct f2fs_rwsem *sem)
2209e4544b63STim Murray {
2210e4544b63STim Murray 	return down_write_trylock(&sem->internal_rwsem);
2211e4544b63STim Murray }
2212e4544b63STim Murray 
2213e4544b63STim Murray static inline void f2fs_up_write(struct f2fs_rwsem *sem)
2214e4544b63STim Murray {
2215e4544b63STim Murray 	up_write(&sem->internal_rwsem);
22167f8e249dSJaegeuk Kim #ifdef CONFIG_F2FS_UNFAIR_RWSEM
2217e4544b63STim Murray 	wake_up_all(&sem->read_waiters);
22187f8e249dSJaegeuk Kim #endif
2219e4544b63STim Murray }
2220e4544b63STim Murray 
2221e479556bSGu Zheng static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
222239a53e0cSJaegeuk Kim {
2223e4544b63STim Murray 	f2fs_down_read(&sbi->cp_rwsem);
222439a53e0cSJaegeuk Kim }
222539a53e0cSJaegeuk Kim 
2226cc15620bSJaegeuk Kim static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi)
2227cc15620bSJaegeuk Kim {
22283e020389SChao Yu 	if (time_to_inject(sbi, FAULT_LOCK_OP)) {
22293e020389SChao Yu 		f2fs_show_injection_info(sbi, FAULT_LOCK_OP);
22303e020389SChao Yu 		return 0;
22313e020389SChao Yu 	}
2232e4544b63STim Murray 	return f2fs_down_read_trylock(&sbi->cp_rwsem);
2233cc15620bSJaegeuk Kim }
2234cc15620bSJaegeuk Kim 
2235e479556bSGu Zheng static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
223639a53e0cSJaegeuk Kim {
2237e4544b63STim Murray 	f2fs_up_read(&sbi->cp_rwsem);
223839936837SJaegeuk Kim }
223939936837SJaegeuk Kim 
2240e479556bSGu Zheng static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
224139936837SJaegeuk Kim {
2242e4544b63STim Murray 	f2fs_down_write(&sbi->cp_rwsem);
224339936837SJaegeuk Kim }
224439936837SJaegeuk Kim 
2245e479556bSGu Zheng static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
224639936837SJaegeuk Kim {
2247e4544b63STim Murray 	f2fs_up_write(&sbi->cp_rwsem);
224839a53e0cSJaegeuk Kim }
224939a53e0cSJaegeuk Kim 
2250119ee914SJaegeuk Kim static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
2251119ee914SJaegeuk Kim {
2252119ee914SJaegeuk Kim 	int reason = CP_SYNC;
2253119ee914SJaegeuk Kim 
2254119ee914SJaegeuk Kim 	if (test_opt(sbi, FASTBOOT))
2255119ee914SJaegeuk Kim 		reason = CP_FASTBOOT;
2256119ee914SJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
2257119ee914SJaegeuk Kim 		reason = CP_UMOUNT;
2258119ee914SJaegeuk Kim 	return reason;
2259119ee914SJaegeuk Kim }
2260119ee914SJaegeuk Kim 
2261119ee914SJaegeuk Kim static inline bool __remain_node_summaries(int reason)
2262119ee914SJaegeuk Kim {
2263c473f1a9SChao Yu 	return (reason & (CP_UMOUNT | CP_FASTBOOT));
2264119ee914SJaegeuk Kim }
2265119ee914SJaegeuk Kim 
2266119ee914SJaegeuk Kim static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
2267119ee914SJaegeuk Kim {
2268aaec2b1dSChao Yu 	return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
2269aaec2b1dSChao Yu 			is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
2270119ee914SJaegeuk Kim }
2271119ee914SJaegeuk Kim 
227239a53e0cSJaegeuk Kim /*
227339a53e0cSJaegeuk Kim  * Check whether the inode has blocks or not
227439a53e0cSJaegeuk Kim  */
227539a53e0cSJaegeuk Kim static inline int F2FS_HAS_BLOCKS(struct inode *inode)
227639a53e0cSJaegeuk Kim {
22770eb0adadSChao Yu 	block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
22780eb0adadSChao Yu 
2279000519f2SChao Yu 	return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block;
228039a53e0cSJaegeuk Kim }
228139a53e0cSJaegeuk Kim 
22824bc8e9bcSChao Yu static inline bool f2fs_has_xattr_block(unsigned int ofs)
22834bc8e9bcSChao Yu {
22844bc8e9bcSChao Yu 	return ofs == XATTR_NODE_OFFSET;
22854bc8e9bcSChao Yu }
22864bc8e9bcSChao Yu 
2287d8a9a229SJaegeuk Kim static inline bool __allow_reserved_blocks(struct f2fs_sb_info *sbi,
2288a90a0884SJaegeuk Kim 					struct inode *inode, bool cap)
22897c2e5963SJaegeuk Kim {
2290d8a9a229SJaegeuk Kim 	if (!inode)
2291d8a9a229SJaegeuk Kim 		return true;
22927c2e5963SJaegeuk Kim 	if (!test_opt(sbi, RESERVE_ROOT))
22937c2e5963SJaegeuk Kim 		return false;
2294d8a9a229SJaegeuk Kim 	if (IS_NOQUOTA(inode))
2295d8a9a229SJaegeuk Kim 		return true;
229663189b78SChao Yu 	if (uid_eq(F2FS_OPTION(sbi).s_resuid, current_fsuid()))
22977c2e5963SJaegeuk Kim 		return true;
229863189b78SChao Yu 	if (!gid_eq(F2FS_OPTION(sbi).s_resgid, GLOBAL_ROOT_GID) &&
229963189b78SChao Yu 					in_group_p(F2FS_OPTION(sbi).s_resgid))
23007c2e5963SJaegeuk Kim 		return true;
2301a90a0884SJaegeuk Kim 	if (cap && capable(CAP_SYS_RESOURCE))
2302162b27aeSJaegeuk Kim 		return true;
23037c2e5963SJaegeuk Kim 	return false;
23047c2e5963SJaegeuk Kim }
23057c2e5963SJaegeuk Kim 
23060abd675eSChao Yu static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool);
23070abd675eSChao Yu static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
230846008c6dSChao Yu 				 struct inode *inode, blkcnt_t *count)
230939a53e0cSJaegeuk Kim {
23100abd675eSChao Yu 	blkcnt_t diff = 0, release = 0;
2311daeb433eSChao Yu 	block_t avail_user_block_count;
23120abd675eSChao Yu 	int ret;
23130abd675eSChao Yu 
23140abd675eSChao Yu 	ret = dquot_reserve_block(inode, *count);
23150abd675eSChao Yu 	if (ret)
23160abd675eSChao Yu 		return ret;
2317dd11a5dfSJaegeuk Kim 
231855523519SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
2319c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_BLOCK);
23200abd675eSChao Yu 		release = *count;
23219ea2f0beSChao Yu 		goto release_quota;
232255523519SChao Yu 	}
23237fa750a1SArnd Bergmann 
2324dd11a5dfSJaegeuk Kim 	/*
2325dd11a5dfSJaegeuk Kim 	 * let's increase this in prior to actual block count change in order
2326dd11a5dfSJaegeuk Kim 	 * for f2fs_sync_file to avoid data races when deciding checkpoint.
2327dd11a5dfSJaegeuk Kim 	 */
2328dd11a5dfSJaegeuk Kim 	percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
232939a53e0cSJaegeuk Kim 
233039a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
23312555a2d5SJaegeuk Kim 	sbi->total_valid_block_count += (block_t)(*count);
233280d42145SYunlong Song 	avail_user_block_count = sbi->user_block_count -
233380d42145SYunlong Song 					sbi->current_reserved_blocks;
23347e65be49SJaegeuk Kim 
2335a90a0884SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode, true))
233663189b78SChao Yu 		avail_user_block_count -= F2FS_OPTION(sbi).root_reserved_blocks;
2337300a8429SChao Yu 
2338300a8429SChao Yu 	if (F2FS_IO_ALIGNED(sbi))
2339300a8429SChao Yu 		avail_user_block_count -= sbi->blocks_per_seg *
2340300a8429SChao Yu 				SM_I(sbi)->additional_reserved_segments;
2341300a8429SChao Yu 
2342a4c3ecaaSDaniel Rosenberg 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2343a4c3ecaaSDaniel Rosenberg 		if (avail_user_block_count > sbi->unusable_block_count)
23444354994fSDaniel Rosenberg 			avail_user_block_count -= sbi->unusable_block_count;
2345a4c3ecaaSDaniel Rosenberg 		else
2346a4c3ecaaSDaniel Rosenberg 			avail_user_block_count = 0;
2347a4c3ecaaSDaniel Rosenberg 	}
2348daeb433eSChao Yu 	if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
2349daeb433eSChao Yu 		diff = sbi->total_valid_block_count - avail_user_block_count;
23507e65be49SJaegeuk Kim 		if (diff > *count)
23517e65be49SJaegeuk Kim 			diff = *count;
2352dd11a5dfSJaegeuk Kim 		*count -= diff;
23530abd675eSChao Yu 		release = diff;
23547e65be49SJaegeuk Kim 		sbi->total_valid_block_count -= diff;
235546008c6dSChao Yu 		if (!*count) {
235639a53e0cSJaegeuk Kim 			spin_unlock(&sbi->stat_lock);
23570abd675eSChao Yu 			goto enospc;
235839a53e0cSJaegeuk Kim 		}
235946008c6dSChao Yu 	}
236039a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
236141382ec4SJaegeuk Kim 
236236b877afSDaniel Rosenberg 	if (unlikely(release)) {
236336b877afSDaniel Rosenberg 		percpu_counter_sub(&sbi->alloc_valid_block_count, release);
23640abd675eSChao Yu 		dquot_release_reservation_block(inode, release);
236536b877afSDaniel Rosenberg 	}
23660abd675eSChao Yu 	f2fs_i_blocks_write(inode, *count, true, true);
23670abd675eSChao Yu 	return 0;
23680abd675eSChao Yu 
23690abd675eSChao Yu enospc:
237036b877afSDaniel Rosenberg 	percpu_counter_sub(&sbi->alloc_valid_block_count, release);
23719ea2f0beSChao Yu release_quota:
23720abd675eSChao Yu 	dquot_release_reservation_block(inode, release);
23730abd675eSChao Yu 	return -ENOSPC;
237439a53e0cSJaegeuk Kim }
237539a53e0cSJaegeuk Kim 
2376dcbb4c10SJoe Perches __printf(2, 3)
2377dcbb4c10SJoe Perches void f2fs_printk(struct f2fs_sb_info *sbi, const char *fmt, ...);
2378dcbb4c10SJoe Perches 
2379dcbb4c10SJoe Perches #define f2fs_err(sbi, fmt, ...)						\
2380dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_ERR fmt, ##__VA_ARGS__)
2381dcbb4c10SJoe Perches #define f2fs_warn(sbi, fmt, ...)					\
2382dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_WARNING fmt, ##__VA_ARGS__)
2383dcbb4c10SJoe Perches #define f2fs_notice(sbi, fmt, ...)					\
2384dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_NOTICE fmt, ##__VA_ARGS__)
2385dcbb4c10SJoe Perches #define f2fs_info(sbi, fmt, ...)					\
2386dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_INFO fmt, ##__VA_ARGS__)
2387dcbb4c10SJoe Perches #define f2fs_debug(sbi, fmt, ...)					\
2388dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_DEBUG fmt, ##__VA_ARGS__)
2389dcbb4c10SJoe Perches 
2390da19b0dcSGu Zheng static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
239139a53e0cSJaegeuk Kim 						struct inode *inode,
23920eb0adadSChao Yu 						block_t count)
239339a53e0cSJaegeuk Kim {
23940eb0adadSChao Yu 	blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK;
23950eb0adadSChao Yu 
239639a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
23979850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
239839a53e0cSJaegeuk Kim 	sbi->total_valid_block_count -= (block_t)count;
239980d42145SYunlong Song 	if (sbi->reserved_blocks &&
240080d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
240180d42145SYunlong Song 		sbi->current_reserved_blocks = min(sbi->reserved_blocks,
240280d42145SYunlong Song 					sbi->current_reserved_blocks + count);
240339a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
24045e159cd3SChao Yu 	if (unlikely(inode->i_blocks < sectors)) {
2405dcbb4c10SJoe Perches 		f2fs_warn(sbi, "Inconsistent i_blocks, ino:%lu, iblocks:%llu, sectors:%llu",
24065e159cd3SChao Yu 			  inode->i_ino,
24075e159cd3SChao Yu 			  (unsigned long long)inode->i_blocks,
24085e159cd3SChao Yu 			  (unsigned long long)sectors);
24095e159cd3SChao Yu 		set_sbi_flag(sbi, SBI_NEED_FSCK);
24105e159cd3SChao Yu 		return;
24115e159cd3SChao Yu 	}
24120abd675eSChao Yu 	f2fs_i_blocks_write(inode, count, false, true);
241339a53e0cSJaegeuk Kim }
241439a53e0cSJaegeuk Kim 
241539a53e0cSJaegeuk Kim static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
241639a53e0cSJaegeuk Kim {
241735782b23SJaegeuk Kim 	atomic_inc(&sbi->nr_pages[count_type]);
24187c4abcbeSChao Yu 
241920109873SChao Yu 	if (count_type == F2FS_DIRTY_DENTS ||
242020109873SChao Yu 			count_type == F2FS_DIRTY_NODES ||
242120109873SChao Yu 			count_type == F2FS_DIRTY_META ||
242220109873SChao Yu 			count_type == F2FS_DIRTY_QDATA ||
242320109873SChao Yu 			count_type == F2FS_DIRTY_IMETA)
2424caf0047eSChao Yu 		set_sbi_flag(sbi, SBI_IS_DIRTY);
242539a53e0cSJaegeuk Kim }
242639a53e0cSJaegeuk Kim 
2427a7ffdbe2SJaegeuk Kim static inline void inode_inc_dirty_pages(struct inode *inode)
242839a53e0cSJaegeuk Kim {
2429204706c7SJaegeuk Kim 	atomic_inc(&F2FS_I(inode)->dirty_pages);
2430c227f912SChao Yu 	inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
2431c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
24322c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
24332c8a4a28SJaegeuk Kim 		inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
243439a53e0cSJaegeuk Kim }
243539a53e0cSJaegeuk Kim 
243639a53e0cSJaegeuk Kim static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
243739a53e0cSJaegeuk Kim {
243835782b23SJaegeuk Kim 	atomic_dec(&sbi->nr_pages[count_type]);
243939a53e0cSJaegeuk Kim }
244039a53e0cSJaegeuk Kim 
2441a7ffdbe2SJaegeuk Kim static inline void inode_dec_dirty_pages(struct inode *inode)
244239a53e0cSJaegeuk Kim {
24435ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
24445ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
24451fe54f9dSJaegeuk Kim 		return;
24461fe54f9dSJaegeuk Kim 
2447204706c7SJaegeuk Kim 	atomic_dec(&F2FS_I(inode)->dirty_pages);
2448c227f912SChao Yu 	dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
2449c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
24502c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
24512c8a4a28SJaegeuk Kim 		dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
245239a53e0cSJaegeuk Kim }
245339a53e0cSJaegeuk Kim 
2454f8e2f32bSDaeho Jeong static inline void inc_atomic_write_cnt(struct inode *inode)
2455f8e2f32bSDaeho Jeong {
2456f8e2f32bSDaeho Jeong 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2457f8e2f32bSDaeho Jeong 	struct f2fs_inode_info *fi = F2FS_I(inode);
2458f8e2f32bSDaeho Jeong 	u64 current_write;
2459f8e2f32bSDaeho Jeong 
2460f8e2f32bSDaeho Jeong 	fi->atomic_write_cnt++;
2461f8e2f32bSDaeho Jeong 	atomic64_inc(&sbi->current_atomic_write);
2462f8e2f32bSDaeho Jeong 	current_write = atomic64_read(&sbi->current_atomic_write);
2463f8e2f32bSDaeho Jeong 	if (current_write > sbi->peak_atomic_write)
2464f8e2f32bSDaeho Jeong 		sbi->peak_atomic_write = current_write;
2465f8e2f32bSDaeho Jeong }
2466f8e2f32bSDaeho Jeong 
2467f8e2f32bSDaeho Jeong static inline void release_atomic_write_cnt(struct inode *inode)
2468f8e2f32bSDaeho Jeong {
2469f8e2f32bSDaeho Jeong 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2470f8e2f32bSDaeho Jeong 	struct f2fs_inode_info *fi = F2FS_I(inode);
2471f8e2f32bSDaeho Jeong 
2472f8e2f32bSDaeho Jeong 	atomic64_sub(fi->atomic_write_cnt, &sbi->current_atomic_write);
2473f8e2f32bSDaeho Jeong 	fi->atomic_write_cnt = 0;
2474f8e2f32bSDaeho Jeong }
2475f8e2f32bSDaeho Jeong 
2476523be8a6SJaegeuk Kim static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
247739a53e0cSJaegeuk Kim {
247835782b23SJaegeuk Kim 	return atomic_read(&sbi->nr_pages[count_type]);
247939a53e0cSJaegeuk Kim }
248039a53e0cSJaegeuk Kim 
2481204706c7SJaegeuk Kim static inline int get_dirty_pages(struct inode *inode)
2482f8b2c1f9SJaegeuk Kim {
2483204706c7SJaegeuk Kim 	return atomic_read(&F2FS_I(inode)->dirty_pages);
2484f8b2c1f9SJaegeuk Kim }
2485f8b2c1f9SJaegeuk Kim 
24865ac206cfSNamjae Jeon static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
24875ac206cfSNamjae Jeon {
24883519e3f9SChao Yu 	unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
2489523be8a6SJaegeuk Kim 	unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
2490523be8a6SJaegeuk Kim 						sbi->log_blocks_per_seg;
2491523be8a6SJaegeuk Kim 
2492523be8a6SJaegeuk Kim 	return segs / sbi->segs_per_sec;
24935ac206cfSNamjae Jeon }
24945ac206cfSNamjae Jeon 
249539a53e0cSJaegeuk Kim static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
249639a53e0cSJaegeuk Kim {
24978b8343faSJaegeuk Kim 	return sbi->total_valid_block_count;
249839a53e0cSJaegeuk Kim }
249939a53e0cSJaegeuk Kim 
2500f83a2584SYunlei He static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
2501f83a2584SYunlei He {
2502f83a2584SYunlei He 	return sbi->discard_blks;
2503f83a2584SYunlei He }
2504f83a2584SYunlei He 
250539a53e0cSJaegeuk Kim static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
250639a53e0cSJaegeuk Kim {
250739a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
250839a53e0cSJaegeuk Kim 
250939a53e0cSJaegeuk Kim 	/* return NAT or SIT bitmap */
251039a53e0cSJaegeuk Kim 	if (flag == NAT_BITMAP)
251139a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
251239a53e0cSJaegeuk Kim 	else if (flag == SIT_BITMAP)
251339a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
251439a53e0cSJaegeuk Kim 
251539a53e0cSJaegeuk Kim 	return 0;
251639a53e0cSJaegeuk Kim }
251739a53e0cSJaegeuk Kim 
251855141486SWanpeng Li static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
251955141486SWanpeng Li {
252055141486SWanpeng Li 	return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
252155141486SWanpeng Li }
252255141486SWanpeng Li 
252339a53e0cSJaegeuk Kim static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
252439a53e0cSJaegeuk Kim {
252539a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
25264260c406SGustavo A. R. Silva 	void *tmp_ptr = &ckpt->sit_nat_version_bitmap;
25271dbe4152SChangman Lee 	int offset;
25281dbe4152SChangman Lee 
2529199bc3feSChao Yu 	if (is_set_ckpt_flags(sbi, CP_LARGE_NAT_BITMAP_FLAG)) {
2530199bc3feSChao Yu 		offset = (flag == SIT_BITMAP) ?
2531199bc3feSChao Yu 			le32_to_cpu(ckpt->nat_ver_bitmap_bytesize) : 0;
2532b471eb99SChao Yu 		/*
2533b471eb99SChao Yu 		 * if large_nat_bitmap feature is enabled, leave checksum
2534b471eb99SChao Yu 		 * protection for all nat/sit bitmaps.
2535b471eb99SChao Yu 		 */
25364260c406SGustavo A. R. Silva 		return tmp_ptr + offset + sizeof(__le32);
2537199bc3feSChao Yu 	}
2538199bc3feSChao Yu 
253955141486SWanpeng Li 	if (__cp_payload(sbi) > 0) {
25401dbe4152SChangman Lee 		if (flag == NAT_BITMAP)
2541280dfeaeSZhang Qilong 			return tmp_ptr;
25421dbe4152SChangman Lee 		else
254365b85cccSJaegeuk Kim 			return (unsigned char *)ckpt + F2FS_BLKSIZE;
25441dbe4152SChangman Lee 	} else {
25451dbe4152SChangman Lee 		offset = (flag == NAT_BITMAP) ?
254625ca923bSJaegeuk Kim 			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
25474260c406SGustavo A. R. Silva 		return tmp_ptr + offset;
254839a53e0cSJaegeuk Kim 	}
25491dbe4152SChangman Lee }
255039a53e0cSJaegeuk Kim 
255139a53e0cSJaegeuk Kim static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
255239a53e0cSJaegeuk Kim {
25538508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
255439a53e0cSJaegeuk Kim 
25558508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 2)
255639a53e0cSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
255739a53e0cSJaegeuk Kim 	return start_addr;
255839a53e0cSJaegeuk Kim }
255939a53e0cSJaegeuk Kim 
25608508e44aSJaegeuk Kim static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
25618508e44aSJaegeuk Kim {
25628508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
25638508e44aSJaegeuk Kim 
25648508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 1)
25658508e44aSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
25668508e44aSJaegeuk Kim 	return start_addr;
25678508e44aSJaegeuk Kim }
25688508e44aSJaegeuk Kim 
25698508e44aSJaegeuk Kim static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
25708508e44aSJaegeuk Kim {
25718508e44aSJaegeuk Kim 	sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
25728508e44aSJaegeuk Kim }
25738508e44aSJaegeuk Kim 
257439a53e0cSJaegeuk Kim static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
257539a53e0cSJaegeuk Kim {
257639a53e0cSJaegeuk Kim 	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
257739a53e0cSJaegeuk Kim }
257839a53e0cSJaegeuk Kim 
25790abd675eSChao Yu static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
2580000519f2SChao Yu 					struct inode *inode, bool is_inode)
258139a53e0cSJaegeuk Kim {
258239a53e0cSJaegeuk Kim 	block_t	valid_block_count;
2583a4c3ecaaSDaniel Rosenberg 	unsigned int valid_node_count, user_block_count;
2584af033b2aSChao Yu 	int err;
25850abd675eSChao Yu 
2586af033b2aSChao Yu 	if (is_inode) {
2587af033b2aSChao Yu 		if (inode) {
2588af033b2aSChao Yu 			err = dquot_alloc_inode(inode);
2589af033b2aSChao Yu 			if (err)
2590af033b2aSChao Yu 				return err;
2591af033b2aSChao Yu 		}
2592af033b2aSChao Yu 	} else {
2593af033b2aSChao Yu 		err = dquot_reserve_block(inode, 1);
2594af033b2aSChao Yu 		if (err)
2595af033b2aSChao Yu 			return err;
25960abd675eSChao Yu 	}
259739a53e0cSJaegeuk Kim 
2598812c6056SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
2599c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_BLOCK);
2600812c6056SChao Yu 		goto enospc;
2601812c6056SChao Yu 	}
2602812c6056SChao Yu 
260339a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
260439a53e0cSJaegeuk Kim 
26057e65be49SJaegeuk Kim 	valid_block_count = sbi->total_valid_block_count +
26067e65be49SJaegeuk Kim 					sbi->current_reserved_blocks + 1;
26077e65be49SJaegeuk Kim 
2608a90a0884SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode, false))
260963189b78SChao Yu 		valid_block_count += F2FS_OPTION(sbi).root_reserved_blocks;
2610300a8429SChao Yu 
2611300a8429SChao Yu 	if (F2FS_IO_ALIGNED(sbi))
2612300a8429SChao Yu 		valid_block_count += sbi->blocks_per_seg *
2613300a8429SChao Yu 				SM_I(sbi)->additional_reserved_segments;
2614300a8429SChao Yu 
2615a4c3ecaaSDaniel Rosenberg 	user_block_count = sbi->user_block_count;
26164354994fSDaniel Rosenberg 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2617a4c3ecaaSDaniel Rosenberg 		user_block_count -= sbi->unusable_block_count;
26187e65be49SJaegeuk Kim 
2619a4c3ecaaSDaniel Rosenberg 	if (unlikely(valid_block_count > user_block_count)) {
262039a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
26210abd675eSChao Yu 		goto enospc;
262239a53e0cSJaegeuk Kim 	}
262339a53e0cSJaegeuk Kim 
2624ef86d709SGu Zheng 	valid_node_count = sbi->total_valid_node_count + 1;
2625cfb271d4SChao Yu 	if (unlikely(valid_node_count > sbi->total_node_count)) {
262639a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
26270abd675eSChao Yu 		goto enospc;
262839a53e0cSJaegeuk Kim 	}
262939a53e0cSJaegeuk Kim 
2630ef86d709SGu Zheng 	sbi->total_valid_node_count++;
2631ef86d709SGu Zheng 	sbi->total_valid_block_count++;
263239a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
263339a53e0cSJaegeuk Kim 
26340abd675eSChao Yu 	if (inode) {
26350abd675eSChao Yu 		if (is_inode)
26360abd675eSChao Yu 			f2fs_mark_inode_dirty_sync(inode, true);
26370abd675eSChao Yu 		else
26380abd675eSChao Yu 			f2fs_i_blocks_write(inode, 1, true, true);
26390abd675eSChao Yu 	}
26400abd675eSChao Yu 
264141382ec4SJaegeuk Kim 	percpu_counter_inc(&sbi->alloc_valid_block_count);
26420abd675eSChao Yu 	return 0;
26430abd675eSChao Yu 
26440abd675eSChao Yu enospc:
2645af033b2aSChao Yu 	if (is_inode) {
2646af033b2aSChao Yu 		if (inode)
2647af033b2aSChao Yu 			dquot_free_inode(inode);
2648af033b2aSChao Yu 	} else {
26490abd675eSChao Yu 		dquot_release_reservation_block(inode, 1);
2650af033b2aSChao Yu 	}
26510abd675eSChao Yu 	return -ENOSPC;
265239a53e0cSJaegeuk Kim }
265339a53e0cSJaegeuk Kim 
265439a53e0cSJaegeuk Kim static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
2655000519f2SChao Yu 					struct inode *inode, bool is_inode)
265639a53e0cSJaegeuk Kim {
265739a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
265839a53e0cSJaegeuk Kim 
26594d17e6feSChao Yu 	if (unlikely(!sbi->total_valid_block_count ||
26604d17e6feSChao Yu 			!sbi->total_valid_node_count)) {
26614d17e6feSChao Yu 		f2fs_warn(sbi, "dec_valid_node_count: inconsistent block counts, total_valid_block:%u, total_valid_node:%u",
26624d17e6feSChao Yu 			  sbi->total_valid_block_count,
26634d17e6feSChao Yu 			  sbi->total_valid_node_count);
26644d17e6feSChao Yu 		set_sbi_flag(sbi, SBI_NEED_FSCK);
26654d17e6feSChao Yu 	} else {
2666ef86d709SGu Zheng 		sbi->total_valid_block_count--;
26674d17e6feSChao Yu 		sbi->total_valid_node_count--;
26684d17e6feSChao Yu 	}
26694d17e6feSChao Yu 
267080d42145SYunlong Song 	if (sbi->reserved_blocks &&
267180d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
267280d42145SYunlong Song 		sbi->current_reserved_blocks++;
267339a53e0cSJaegeuk Kim 
267439a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
26750abd675eSChao Yu 
2676ea6d7e72SChao Yu 	if (is_inode) {
2677af033b2aSChao Yu 		dquot_free_inode(inode);
2678ea6d7e72SChao Yu 	} else {
2679ea6d7e72SChao Yu 		if (unlikely(inode->i_blocks == 0)) {
2680097a7686SChao Yu 			f2fs_warn(sbi, "dec_valid_node_count: inconsistent i_blocks, ino:%lu, iblocks:%llu",
2681ea6d7e72SChao Yu 				  inode->i_ino,
2682ea6d7e72SChao Yu 				  (unsigned long long)inode->i_blocks);
2683ea6d7e72SChao Yu 			set_sbi_flag(sbi, SBI_NEED_FSCK);
2684ea6d7e72SChao Yu 			return;
2685ea6d7e72SChao Yu 		}
26860abd675eSChao Yu 		f2fs_i_blocks_write(inode, 1, false, true);
268739a53e0cSJaegeuk Kim 	}
2688ea6d7e72SChao Yu }
268939a53e0cSJaegeuk Kim 
269039a53e0cSJaegeuk Kim static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
269139a53e0cSJaegeuk Kim {
26928b8343faSJaegeuk Kim 	return sbi->total_valid_node_count;
269339a53e0cSJaegeuk Kim }
269439a53e0cSJaegeuk Kim 
269539a53e0cSJaegeuk Kim static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
269639a53e0cSJaegeuk Kim {
2697513c5f37SJaegeuk Kim 	percpu_counter_inc(&sbi->total_valid_inode_count);
269839a53e0cSJaegeuk Kim }
269939a53e0cSJaegeuk Kim 
27000e80220aSJaegeuk Kim static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
270139a53e0cSJaegeuk Kim {
2702513c5f37SJaegeuk Kim 	percpu_counter_dec(&sbi->total_valid_inode_count);
270339a53e0cSJaegeuk Kim }
270439a53e0cSJaegeuk Kim 
2705513c5f37SJaegeuk Kim static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
270639a53e0cSJaegeuk Kim {
2707513c5f37SJaegeuk Kim 	return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
270839a53e0cSJaegeuk Kim }
270939a53e0cSJaegeuk Kim 
2710a56c7c6fSJaegeuk Kim static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
2711a56c7c6fSJaegeuk Kim 						pgoff_t index, bool for_write)
2712a56c7c6fSJaegeuk Kim {
271382cf4f13SChao Yu 	struct page *page;
2714df808180SMatthew Wilcox (Oracle) 	unsigned int flags;
2715cac5a3d8SDongOh Shin 
27167fa750a1SArnd Bergmann 	if (IS_ENABLED(CONFIG_F2FS_FAULT_INJECTION)) {
271782cf4f13SChao Yu 		if (!for_write)
271882cf4f13SChao Yu 			page = find_get_page_flags(mapping, index,
271982cf4f13SChao Yu 							FGP_LOCK | FGP_ACCESSED);
272082cf4f13SChao Yu 		else
272182cf4f13SChao Yu 			page = find_lock_page(mapping, index);
2722c41f3cc3SJaegeuk Kim 		if (page)
2723c41f3cc3SJaegeuk Kim 			return page;
2724c41f3cc3SJaegeuk Kim 
272555523519SChao Yu 		if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) {
2726c45d6002SChao Yu 			f2fs_show_injection_info(F2FS_M_SB(mapping),
2727c45d6002SChao Yu 							FAULT_PAGE_ALLOC);
2728c41f3cc3SJaegeuk Kim 			return NULL;
272955523519SChao Yu 		}
27307fa750a1SArnd Bergmann 	}
27317fa750a1SArnd Bergmann 
2732a56c7c6fSJaegeuk Kim 	if (!for_write)
2733a56c7c6fSJaegeuk Kim 		return grab_cache_page(mapping, index);
2734df808180SMatthew Wilcox (Oracle) 
2735df808180SMatthew Wilcox (Oracle) 	flags = memalloc_nofs_save();
2736b7446e7cSMatthew Wilcox (Oracle) 	page = grab_cache_page_write_begin(mapping, index);
2737df808180SMatthew Wilcox (Oracle) 	memalloc_nofs_restore(flags);
2738df808180SMatthew Wilcox (Oracle) 
2739df808180SMatthew Wilcox (Oracle) 	return page;
2740a56c7c6fSJaegeuk Kim }
2741a56c7c6fSJaegeuk Kim 
274201eccef7SChao Yu static inline struct page *f2fs_pagecache_get_page(
274301eccef7SChao Yu 				struct address_space *mapping, pgoff_t index,
274401eccef7SChao Yu 				int fgp_flags, gfp_t gfp_mask)
274501eccef7SChao Yu {
274601eccef7SChao Yu 	if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_GET)) {
2747c45d6002SChao Yu 		f2fs_show_injection_info(F2FS_M_SB(mapping), FAULT_PAGE_GET);
274801eccef7SChao Yu 		return NULL;
274901eccef7SChao Yu 	}
27507fa750a1SArnd Bergmann 
275101eccef7SChao Yu 	return pagecache_get_page(mapping, index, fgp_flags, gfp_mask);
275201eccef7SChao Yu }
275301eccef7SChao Yu 
275439a53e0cSJaegeuk Kim static inline void f2fs_put_page(struct page *page, int unlock)
275539a53e0cSJaegeuk Kim {
2756031fa8ccSJaegeuk Kim 	if (!page)
275739a53e0cSJaegeuk Kim 		return;
275839a53e0cSJaegeuk Kim 
275939a53e0cSJaegeuk Kim 	if (unlock) {
27609850cf4aSJaegeuk Kim 		f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
276139a53e0cSJaegeuk Kim 		unlock_page(page);
276239a53e0cSJaegeuk Kim 	}
276309cbfeafSKirill A. Shutemov 	put_page(page);
276439a53e0cSJaegeuk Kim }
276539a53e0cSJaegeuk Kim 
276639a53e0cSJaegeuk Kim static inline void f2fs_put_dnode(struct dnode_of_data *dn)
276739a53e0cSJaegeuk Kim {
276839a53e0cSJaegeuk Kim 	if (dn->node_page)
276939a53e0cSJaegeuk Kim 		f2fs_put_page(dn->node_page, 1);
277039a53e0cSJaegeuk Kim 	if (dn->inode_page && dn->node_page != dn->inode_page)
277139a53e0cSJaegeuk Kim 		f2fs_put_page(dn->inode_page, 0);
277239a53e0cSJaegeuk Kim 	dn->node_page = NULL;
277339a53e0cSJaegeuk Kim 	dn->inode_page = NULL;
277439a53e0cSJaegeuk Kim }
277539a53e0cSJaegeuk Kim 
277639a53e0cSJaegeuk Kim static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
2777e8512d2eSGu Zheng 					size_t size)
277839a53e0cSJaegeuk Kim {
2779e8512d2eSGu Zheng 	return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
278039a53e0cSJaegeuk Kim }
278139a53e0cSJaegeuk Kim 
278232410577SChao Yu static inline void *f2fs_kmem_cache_alloc_nofail(struct kmem_cache *cachep,
27837bd59381SGu Zheng 						gfp_t flags)
27847bd59381SGu Zheng {
27857bd59381SGu Zheng 	void *entry;
27867bd59381SGu Zheng 
278780c54505SJaegeuk Kim 	entry = kmem_cache_alloc(cachep, flags);
278880c54505SJaegeuk Kim 	if (!entry)
278980c54505SJaegeuk Kim 		entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
27907bd59381SGu Zheng 	return entry;
27917bd59381SGu Zheng }
27927bd59381SGu Zheng 
279332410577SChao Yu static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
279432410577SChao Yu 			gfp_t flags, bool nofail, struct f2fs_sb_info *sbi)
279532410577SChao Yu {
279632410577SChao Yu 	if (nofail)
279732410577SChao Yu 		return f2fs_kmem_cache_alloc_nofail(cachep, flags);
279832410577SChao Yu 
279932410577SChao Yu 	if (time_to_inject(sbi, FAULT_SLAB_ALLOC)) {
280032410577SChao Yu 		f2fs_show_injection_info(sbi, FAULT_SLAB_ALLOC);
280132410577SChao Yu 		return NULL;
280232410577SChao Yu 	}
280332410577SChao Yu 
280432410577SChao Yu 	return kmem_cache_alloc(cachep, flags);
280532410577SChao Yu }
280632410577SChao Yu 
2807493720a4SChao Yu static inline bool is_inflight_io(struct f2fs_sb_info *sbi, int type)
28085f9abab4SJaegeuk Kim {
28095f9abab4SJaegeuk Kim 	if (get_pages(sbi, F2FS_RD_DATA) || get_pages(sbi, F2FS_RD_NODE) ||
28105f9abab4SJaegeuk Kim 		get_pages(sbi, F2FS_RD_META) || get_pages(sbi, F2FS_WB_DATA) ||
2811fef4129eSChao Yu 		get_pages(sbi, F2FS_WB_CP_DATA) ||
2812fef4129eSChao Yu 		get_pages(sbi, F2FS_DIO_READ) ||
281311ac8ef8SJaegeuk Kim 		get_pages(sbi, F2FS_DIO_WRITE))
2814493720a4SChao Yu 		return true;
281511ac8ef8SJaegeuk Kim 
281656659ce8SSahitya Tummala 	if (type != DISCARD_TIME && SM_I(sbi) && SM_I(sbi)->dcc_info &&
281711ac8ef8SJaegeuk Kim 			atomic_read(&SM_I(sbi)->dcc_info->queued_discard))
2818493720a4SChao Yu 		return true;
281911ac8ef8SJaegeuk Kim 
282011ac8ef8SJaegeuk Kim 	if (SM_I(sbi) && SM_I(sbi)->fcc_info &&
282172691af6SJaegeuk Kim 			atomic_read(&SM_I(sbi)->fcc_info->queued_flush))
2822493720a4SChao Yu 		return true;
2823493720a4SChao Yu 	return false;
2824493720a4SChao Yu }
2825493720a4SChao Yu 
2826493720a4SChao Yu static inline bool is_idle(struct f2fs_sb_info *sbi, int type)
2827493720a4SChao Yu {
2828493720a4SChao Yu 	if (sbi->gc_mode == GC_URGENT_HIGH)
2829493720a4SChao Yu 		return true;
2830493720a4SChao Yu 
2831493720a4SChao Yu 	if (is_inflight_io(sbi, type))
28325f9abab4SJaegeuk Kim 		return false;
283311ac8ef8SJaegeuk Kim 
2834d98af5f4SDaeho Jeong 	if (sbi->gc_mode == GC_URGENT_MID)
2835d98af5f4SDaeho Jeong 		return true;
2836d98af5f4SDaeho Jeong 
28370e5e8111SDaeho Jeong 	if (sbi->gc_mode == GC_URGENT_LOW &&
28380e5e8111SDaeho Jeong 			(type == DISCARD_TIME || type == GC_TIME))
28390e5e8111SDaeho Jeong 		return true;
28400e5e8111SDaeho Jeong 
28415f9abab4SJaegeuk Kim 	return f2fs_time_over(sbi, type);
28425f9abab4SJaegeuk Kim }
28435f9abab4SJaegeuk Kim 
28449be32d72SJaegeuk Kim static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
28459be32d72SJaegeuk Kim 				unsigned long index, void *item)
28469be32d72SJaegeuk Kim {
28479be32d72SJaegeuk Kim 	while (radix_tree_insert(root, index, item))
28489be32d72SJaegeuk Kim 		cond_resched();
28499be32d72SJaegeuk Kim }
28509be32d72SJaegeuk Kim 
285139a53e0cSJaegeuk Kim #define RAW_IS_INODE(p)	((p)->footer.nid == (p)->footer.ino)
285239a53e0cSJaegeuk Kim 
285339a53e0cSJaegeuk Kim static inline bool IS_INODE(struct page *page)
285439a53e0cSJaegeuk Kim {
285545590710SGu Zheng 	struct f2fs_node *p = F2FS_NODE(page);
2856cac5a3d8SDongOh Shin 
285739a53e0cSJaegeuk Kim 	return RAW_IS_INODE(p);
285839a53e0cSJaegeuk Kim }
285939a53e0cSJaegeuk Kim 
28607a2af766SChao Yu static inline int offset_in_addr(struct f2fs_inode *i)
28617a2af766SChao Yu {
28627a2af766SChao Yu 	return (i->i_inline & F2FS_EXTRA_ATTR) ?
28637a2af766SChao Yu 			(le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0;
28647a2af766SChao Yu }
28657a2af766SChao Yu 
286639a53e0cSJaegeuk Kim static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
286739a53e0cSJaegeuk Kim {
286839a53e0cSJaegeuk Kim 	return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
286939a53e0cSJaegeuk Kim }
287039a53e0cSJaegeuk Kim 
28717a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode);
2872a2ced1ceSChao Yu static inline block_t data_blkaddr(struct inode *inode,
28737a2af766SChao Yu 			struct page *node_page, unsigned int offset)
287439a53e0cSJaegeuk Kim {
287539a53e0cSJaegeuk Kim 	struct f2fs_node *raw_node;
287639a53e0cSJaegeuk Kim 	__le32 *addr_array;
28777a2af766SChao Yu 	int base = 0;
28787a2af766SChao Yu 	bool is_inode = IS_INODE(node_page);
2879cac5a3d8SDongOh Shin 
288045590710SGu Zheng 	raw_node = F2FS_NODE(node_page);
28817a2af766SChao Yu 
2882979f492fSLiFan 	if (is_inode) {
2883979f492fSLiFan 		if (!inode)
28847a88ddb5SChao Yu 			/* from GC path only */
28857a2af766SChao Yu 			base = offset_in_addr(&raw_node->i);
2886979f492fSLiFan 		else if (f2fs_has_extra_attr(inode))
28877a2af766SChao Yu 			base = get_extra_isize(inode);
28887a2af766SChao Yu 	}
28897a2af766SChao Yu 
289039a53e0cSJaegeuk Kim 	addr_array = blkaddr_in_node(raw_node);
28917a2af766SChao Yu 	return le32_to_cpu(addr_array[base + offset]);
289239a53e0cSJaegeuk Kim }
289339a53e0cSJaegeuk Kim 
2894a2ced1ceSChao Yu static inline block_t f2fs_data_blkaddr(struct dnode_of_data *dn)
2895a2ced1ceSChao Yu {
2896a2ced1ceSChao Yu 	return data_blkaddr(dn->inode, dn->node_page, dn->ofs_in_node);
2897a2ced1ceSChao Yu }
2898a2ced1ceSChao Yu 
289939a53e0cSJaegeuk Kim static inline int f2fs_test_bit(unsigned int nr, char *addr)
290039a53e0cSJaegeuk Kim {
290139a53e0cSJaegeuk Kim 	int mask;
290239a53e0cSJaegeuk Kim 
290339a53e0cSJaegeuk Kim 	addr += (nr >> 3);
290439a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
290539a53e0cSJaegeuk Kim 	return mask & *addr;
290639a53e0cSJaegeuk Kim }
290739a53e0cSJaegeuk Kim 
2908a66cdd98SJaegeuk Kim static inline void f2fs_set_bit(unsigned int nr, char *addr)
2909a66cdd98SJaegeuk Kim {
2910a66cdd98SJaegeuk Kim 	int mask;
2911a66cdd98SJaegeuk Kim 
2912a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2913a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2914a66cdd98SJaegeuk Kim 	*addr |= mask;
2915a66cdd98SJaegeuk Kim }
2916a66cdd98SJaegeuk Kim 
2917a66cdd98SJaegeuk Kim static inline void f2fs_clear_bit(unsigned int nr, char *addr)
2918a66cdd98SJaegeuk Kim {
2919a66cdd98SJaegeuk Kim 	int mask;
2920a66cdd98SJaegeuk Kim 
2921a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2922a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2923a66cdd98SJaegeuk Kim 	*addr &= ~mask;
2924a66cdd98SJaegeuk Kim }
2925a66cdd98SJaegeuk Kim 
292652aca074SGu Zheng static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
292739a53e0cSJaegeuk Kim {
292839a53e0cSJaegeuk Kim 	int mask;
292939a53e0cSJaegeuk Kim 	int ret;
293039a53e0cSJaegeuk Kim 
293139a53e0cSJaegeuk Kim 	addr += (nr >> 3);
293239a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
293339a53e0cSJaegeuk Kim 	ret = mask & *addr;
293439a53e0cSJaegeuk Kim 	*addr |= mask;
293539a53e0cSJaegeuk Kim 	return ret;
293639a53e0cSJaegeuk Kim }
293739a53e0cSJaegeuk Kim 
293852aca074SGu Zheng static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
293939a53e0cSJaegeuk Kim {
294039a53e0cSJaegeuk Kim 	int mask;
294139a53e0cSJaegeuk Kim 	int ret;
294239a53e0cSJaegeuk Kim 
294339a53e0cSJaegeuk Kim 	addr += (nr >> 3);
294439a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
294539a53e0cSJaegeuk Kim 	ret = mask & *addr;
294639a53e0cSJaegeuk Kim 	*addr &= ~mask;
294739a53e0cSJaegeuk Kim 	return ret;
294839a53e0cSJaegeuk Kim }
294939a53e0cSJaegeuk Kim 
2950c6ac4c0eSGu Zheng static inline void f2fs_change_bit(unsigned int nr, char *addr)
2951c6ac4c0eSGu Zheng {
2952c6ac4c0eSGu Zheng 	int mask;
2953c6ac4c0eSGu Zheng 
2954c6ac4c0eSGu Zheng 	addr += (nr >> 3);
2955c6ac4c0eSGu Zheng 	mask = 1 << (7 - (nr & 0x07));
2956c6ac4c0eSGu Zheng 	*addr ^= mask;
2957c6ac4c0eSGu Zheng }
2958c6ac4c0eSGu Zheng 
295959c84408SChao Yu /*
296036098557SEric Biggers  * On-disk inode flags (f2fs_inode::i_flags)
296159c84408SChao Yu  */
29624c8ff709SChao Yu #define F2FS_COMPR_FL			0x00000004 /* Compress file */
296359c84408SChao Yu #define F2FS_SYNC_FL			0x00000008 /* Synchronous updates */
296459c84408SChao Yu #define F2FS_IMMUTABLE_FL		0x00000010 /* Immutable file */
296559c84408SChao Yu #define F2FS_APPEND_FL			0x00000020 /* writes to file may only append */
296659c84408SChao Yu #define F2FS_NODUMP_FL			0x00000040 /* do not dump file */
296759c84408SChao Yu #define F2FS_NOATIME_FL			0x00000080 /* do not update atime */
29684c8ff709SChao Yu #define F2FS_NOCOMP_FL			0x00000400 /* Don't compress */
296959c84408SChao Yu #define F2FS_INDEX_FL			0x00001000 /* hash-indexed directory */
297059c84408SChao Yu #define F2FS_DIRSYNC_FL			0x00010000 /* dirsync behaviour (directories only) */
297159c84408SChao Yu #define F2FS_PROJINHERIT_FL		0x20000000 /* Create with parents projid */
29722c2eb7a3SDaniel Rosenberg #define F2FS_CASEFOLD_FL		0x40000000 /* Casefolded file */
297359c84408SChao Yu 
297459c84408SChao Yu /* Flags that should be inherited by new inodes from their parent. */
297536098557SEric Biggers #define F2FS_FL_INHERITED (F2FS_SYNC_FL | F2FS_NODUMP_FL | F2FS_NOATIME_FL | \
29762c2eb7a3SDaniel Rosenberg 			   F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL | \
29774c8ff709SChao Yu 			   F2FS_CASEFOLD_FL | F2FS_COMPR_FL | F2FS_NOCOMP_FL)
297859c84408SChao Yu 
297959c84408SChao Yu /* Flags that are appropriate for regular files (all but dir-specific ones). */
29802c2eb7a3SDaniel Rosenberg #define F2FS_REG_FLMASK		(~(F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL | \
29812c2eb7a3SDaniel Rosenberg 				F2FS_CASEFOLD_FL))
298259c84408SChao Yu 
298359c84408SChao Yu /* Flags that are appropriate for non-directories/regular files. */
298459c84408SChao Yu #define F2FS_OTHER_FLMASK	(F2FS_NODUMP_FL | F2FS_NOATIME_FL)
29855c57132eSChao Yu 
29865c57132eSChao Yu static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
29875c57132eSChao Yu {
29885c57132eSChao Yu 	if (S_ISDIR(mode))
29895c57132eSChao Yu 		return flags;
29905c57132eSChao Yu 	else if (S_ISREG(mode))
29915c57132eSChao Yu 		return flags & F2FS_REG_FLMASK;
29925c57132eSChao Yu 	else
29935c57132eSChao Yu 		return flags & F2FS_OTHER_FLMASK;
29945c57132eSChao Yu }
29955c57132eSChao Yu 
2996205b9822SJaegeuk Kim static inline void __mark_inode_dirty_flag(struct inode *inode,
2997205b9822SJaegeuk Kim 						int flag, bool set)
299839a53e0cSJaegeuk Kim {
2999205b9822SJaegeuk Kim 	switch (flag) {
3000205b9822SJaegeuk Kim 	case FI_INLINE_XATTR:
3001205b9822SJaegeuk Kim 	case FI_INLINE_DATA:
3002205b9822SJaegeuk Kim 	case FI_INLINE_DENTRY:
30039ac1e2d8SDaeho Jeong 	case FI_NEW_INODE:
3004205b9822SJaegeuk Kim 		if (set)
3005205b9822SJaegeuk Kim 			return;
3006df561f66SGustavo A. R. Silva 		fallthrough;
3007205b9822SJaegeuk Kim 	case FI_DATA_EXIST:
3008205b9822SJaegeuk Kim 	case FI_INLINE_DOTS:
30091ad71a27SJaegeuk Kim 	case FI_PIN_FILE:
3010c6140415SJaegeuk Kim 	case FI_COMPRESS_RELEASED:
30117c45729aSJaegeuk Kim 		f2fs_mark_inode_dirty_sync(inode, true);
3012205b9822SJaegeuk Kim 	}
301339a53e0cSJaegeuk Kim }
301439a53e0cSJaegeuk Kim 
301591942321SJaegeuk Kim static inline void set_inode_flag(struct inode *inode, int flag)
301639a53e0cSJaegeuk Kim {
301758f7e00fSYufen Yu 	set_bit(flag, F2FS_I(inode)->flags);
3018205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, true);
301939a53e0cSJaegeuk Kim }
302039a53e0cSJaegeuk Kim 
302191942321SJaegeuk Kim static inline int is_inode_flag_set(struct inode *inode, int flag)
302239a53e0cSJaegeuk Kim {
30237653b9d8SChao Yu 	return test_bit(flag, F2FS_I(inode)->flags);
302439a53e0cSJaegeuk Kim }
302539a53e0cSJaegeuk Kim 
302691942321SJaegeuk Kim static inline void clear_inode_flag(struct inode *inode, int flag)
302739a53e0cSJaegeuk Kim {
302858f7e00fSYufen Yu 	clear_bit(flag, F2FS_I(inode)->flags);
3029205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, false);
303039a53e0cSJaegeuk Kim }
303139a53e0cSJaegeuk Kim 
303295ae251fSEric Biggers static inline bool f2fs_verity_in_progress(struct inode *inode)
303395ae251fSEric Biggers {
303495ae251fSEric Biggers 	return IS_ENABLED(CONFIG_FS_VERITY) &&
303595ae251fSEric Biggers 	       is_inode_flag_set(inode, FI_VERITY_IN_PROGRESS);
303695ae251fSEric Biggers }
303795ae251fSEric Biggers 
303891942321SJaegeuk Kim static inline void set_acl_inode(struct inode *inode, umode_t mode)
3039444c580fSJaegeuk Kim {
304091942321SJaegeuk Kim 	F2FS_I(inode)->i_acl_mode = mode;
304191942321SJaegeuk Kim 	set_inode_flag(inode, FI_ACL_MODE);
30427c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, false);
304339a53e0cSJaegeuk Kim }
304439a53e0cSJaegeuk Kim 
3045a1961246SJaegeuk Kim static inline void f2fs_i_links_write(struct inode *inode, bool inc)
3046a1961246SJaegeuk Kim {
3047a1961246SJaegeuk Kim 	if (inc)
3048a1961246SJaegeuk Kim 		inc_nlink(inode);
3049a1961246SJaegeuk Kim 	else
3050a1961246SJaegeuk Kim 		drop_nlink(inode);
30517c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3052a1961246SJaegeuk Kim }
3053a1961246SJaegeuk Kim 
30548edd03c8SJaegeuk Kim static inline void f2fs_i_blocks_write(struct inode *inode,
30550abd675eSChao Yu 					block_t diff, bool add, bool claim)
30568edd03c8SJaegeuk Kim {
305726de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
305826de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
305926de9b11SJaegeuk Kim 
30600abd675eSChao Yu 	/* add = 1, claim = 1 should be dquot_reserve_block in pair */
30610abd675eSChao Yu 	if (add) {
30620abd675eSChao Yu 		if (claim)
30630abd675eSChao Yu 			dquot_claim_block(inode, diff);
30640abd675eSChao Yu 		else
30650abd675eSChao Yu 			dquot_alloc_block_nofail(inode, diff);
30660abd675eSChao Yu 	} else {
30670abd675eSChao Yu 		dquot_free_block(inode, diff);
30680abd675eSChao Yu 	}
30690abd675eSChao Yu 
30707c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
307126de9b11SJaegeuk Kim 	if (clean || recover)
307226de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
30738edd03c8SJaegeuk Kim }
30748edd03c8SJaegeuk Kim 
3075fc9581c8SJaegeuk Kim static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
3076fc9581c8SJaegeuk Kim {
307726de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
307826de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
307926de9b11SJaegeuk Kim 
3080fc9581c8SJaegeuk Kim 	if (i_size_read(inode) == i_size)
3081fc9581c8SJaegeuk Kim 		return;
3082fc9581c8SJaegeuk Kim 
3083fc9581c8SJaegeuk Kim 	i_size_write(inode, i_size);
30847c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
308526de9b11SJaegeuk Kim 	if (clean || recover)
308626de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
308726de9b11SJaegeuk Kim }
308826de9b11SJaegeuk Kim 
3089205b9822SJaegeuk Kim static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
3090205b9822SJaegeuk Kim {
3091205b9822SJaegeuk Kim 	F2FS_I(inode)->i_current_depth = depth;
30927c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3093205b9822SJaegeuk Kim }
3094205b9822SJaegeuk Kim 
30951ad71a27SJaegeuk Kim static inline void f2fs_i_gc_failures_write(struct inode *inode,
30961ad71a27SJaegeuk Kim 					unsigned int count)
30971ad71a27SJaegeuk Kim {
30982ef79ecbSChao Yu 	F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN] = count;
30991ad71a27SJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
31001ad71a27SJaegeuk Kim }
31011ad71a27SJaegeuk Kim 
3102205b9822SJaegeuk Kim static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
3103205b9822SJaegeuk Kim {
3104205b9822SJaegeuk Kim 	F2FS_I(inode)->i_xattr_nid = xnid;
31057c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3106205b9822SJaegeuk Kim }
3107205b9822SJaegeuk Kim 
3108205b9822SJaegeuk Kim static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
3109205b9822SJaegeuk Kim {
3110205b9822SJaegeuk Kim 	F2FS_I(inode)->i_pino = pino;
31117c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3112205b9822SJaegeuk Kim }
3113205b9822SJaegeuk Kim 
311491942321SJaegeuk Kim static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
3115444c580fSJaegeuk Kim {
3116205b9822SJaegeuk Kim 	struct f2fs_inode_info *fi = F2FS_I(inode);
3117205b9822SJaegeuk Kim 
3118444c580fSJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_XATTR)
31197653b9d8SChao Yu 		set_bit(FI_INLINE_XATTR, fi->flags);
31201001b347SHuajun Li 	if (ri->i_inline & F2FS_INLINE_DATA)
31217653b9d8SChao Yu 		set_bit(FI_INLINE_DATA, fi->flags);
312234d67debSChao Yu 	if (ri->i_inline & F2FS_INLINE_DENTRY)
31237653b9d8SChao Yu 		set_bit(FI_INLINE_DENTRY, fi->flags);
3124b3d208f9SJaegeuk Kim 	if (ri->i_inline & F2FS_DATA_EXIST)
31257653b9d8SChao Yu 		set_bit(FI_DATA_EXIST, fi->flags);
3126510022a8SJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_DOTS)
31277653b9d8SChao Yu 		set_bit(FI_INLINE_DOTS, fi->flags);
31287a2af766SChao Yu 	if (ri->i_inline & F2FS_EXTRA_ATTR)
31297653b9d8SChao Yu 		set_bit(FI_EXTRA_ATTR, fi->flags);
31301ad71a27SJaegeuk Kim 	if (ri->i_inline & F2FS_PIN_FILE)
31317653b9d8SChao Yu 		set_bit(FI_PIN_FILE, fi->flags);
3132c6140415SJaegeuk Kim 	if (ri->i_inline & F2FS_COMPRESS_RELEASED)
3133c6140415SJaegeuk Kim 		set_bit(FI_COMPRESS_RELEASED, fi->flags);
3134444c580fSJaegeuk Kim }
3135444c580fSJaegeuk Kim 
313691942321SJaegeuk Kim static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
3137444c580fSJaegeuk Kim {
3138444c580fSJaegeuk Kim 	ri->i_inline = 0;
3139444c580fSJaegeuk Kim 
314091942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_XATTR))
3141444c580fSJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_XATTR;
314291942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DATA))
31431001b347SHuajun Li 		ri->i_inline |= F2FS_INLINE_DATA;
314491942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
314534d67debSChao Yu 		ri->i_inline |= F2FS_INLINE_DENTRY;
314691942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_DATA_EXIST))
3147b3d208f9SJaegeuk Kim 		ri->i_inline |= F2FS_DATA_EXIST;
314891942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DOTS))
3149510022a8SJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_DOTS;
31507a2af766SChao Yu 	if (is_inode_flag_set(inode, FI_EXTRA_ATTR))
31517a2af766SChao Yu 		ri->i_inline |= F2FS_EXTRA_ATTR;
31521ad71a27SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_PIN_FILE))
31531ad71a27SJaegeuk Kim 		ri->i_inline |= F2FS_PIN_FILE;
3154c6140415SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
3155c6140415SJaegeuk Kim 		ri->i_inline |= F2FS_COMPRESS_RELEASED;
31567a2af766SChao Yu }
31577a2af766SChao Yu 
31587a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode)
31597a2af766SChao Yu {
31607a2af766SChao Yu 	return is_inode_flag_set(inode, FI_EXTRA_ATTR);
3161444c580fSJaegeuk Kim }
3162444c580fSJaegeuk Kim 
3163987c7c31SChao Yu static inline int f2fs_has_inline_xattr(struct inode *inode)
3164987c7c31SChao Yu {
316591942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_XATTR);
3166987c7c31SChao Yu }
3167987c7c31SChao Yu 
31684c8ff709SChao Yu static inline int f2fs_compressed_file(struct inode *inode)
31694c8ff709SChao Yu {
31704c8ff709SChao Yu 	return S_ISREG(inode->i_mode) &&
31714c8ff709SChao Yu 		is_inode_flag_set(inode, FI_COMPRESSED_FILE);
31724c8ff709SChao Yu }
31734c8ff709SChao Yu 
3174602a16d5SDaeho Jeong static inline bool f2fs_need_compress_data(struct inode *inode)
3175602a16d5SDaeho Jeong {
3176602a16d5SDaeho Jeong 	int compress_mode = F2FS_OPTION(F2FS_I_SB(inode)).compress_mode;
3177602a16d5SDaeho Jeong 
3178602a16d5SDaeho Jeong 	if (!f2fs_compressed_file(inode))
3179602a16d5SDaeho Jeong 		return false;
3180602a16d5SDaeho Jeong 
3181602a16d5SDaeho Jeong 	if (compress_mode == COMPR_MODE_FS)
3182602a16d5SDaeho Jeong 		return true;
3183602a16d5SDaeho Jeong 	else if (compress_mode == COMPR_MODE_USER &&
3184602a16d5SDaeho Jeong 			is_inode_flag_set(inode, FI_ENABLE_COMPRESS))
3185602a16d5SDaeho Jeong 		return true;
3186602a16d5SDaeho Jeong 
3187602a16d5SDaeho Jeong 	return false;
3188602a16d5SDaeho Jeong }
3189602a16d5SDaeho Jeong 
319081ca7350SChao Yu static inline unsigned int addrs_per_inode(struct inode *inode)
3191de93653fSJaegeuk Kim {
3192d02a6e61SChao Yu 	unsigned int addrs = CUR_ADDRS_PER_INODE(inode) -
3193d02a6e61SChao Yu 				get_inline_xattr_addrs(inode);
31944c8ff709SChao Yu 
31954c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
31964c8ff709SChao Yu 		return addrs;
31974c8ff709SChao Yu 	return ALIGN_DOWN(addrs, F2FS_I(inode)->i_cluster_size);
3198d02a6e61SChao Yu }
3199d02a6e61SChao Yu 
3200d02a6e61SChao Yu static inline unsigned int addrs_per_block(struct inode *inode)
3201d02a6e61SChao Yu {
32024c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
32034c8ff709SChao Yu 		return DEF_ADDRS_PER_BLOCK;
32044c8ff709SChao Yu 	return ALIGN_DOWN(DEF_ADDRS_PER_BLOCK, F2FS_I(inode)->i_cluster_size);
3205de93653fSJaegeuk Kim }
3206de93653fSJaegeuk Kim 
32076afc662eSChao Yu static inline void *inline_xattr_addr(struct inode *inode, struct page *page)
320865985d93SJaegeuk Kim {
3209695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
3210cac5a3d8SDongOh Shin 
321165985d93SJaegeuk Kim 	return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
3212b323fd28SChao Yu 					get_inline_xattr_addrs(inode)]);
321365985d93SJaegeuk Kim }
321465985d93SJaegeuk Kim 
321565985d93SJaegeuk Kim static inline int inline_xattr_size(struct inode *inode)
321665985d93SJaegeuk Kim {
3217622927f3SChao Yu 	if (f2fs_has_inline_xattr(inode))
32186afc662eSChao Yu 		return get_inline_xattr_addrs(inode) * sizeof(__le32);
3219622927f3SChao Yu 	return 0;
322065985d93SJaegeuk Kim }
322165985d93SJaegeuk Kim 
3222d9c454abSChao Liu /*
3223d9c454abSChao Liu  * Notice: check inline_data flag without inode page lock is unsafe.
3224d9c454abSChao Liu  * It could change at any time by f2fs_convert_inline_page().
3225d9c454abSChao Liu  */
32260dbdc2aeSJaegeuk Kim static inline int f2fs_has_inline_data(struct inode *inode)
32270dbdc2aeSJaegeuk Kim {
322891942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DATA);
32290dbdc2aeSJaegeuk Kim }
32300dbdc2aeSJaegeuk Kim 
3231b3d208f9SJaegeuk Kim static inline int f2fs_exist_data(struct inode *inode)
3232b3d208f9SJaegeuk Kim {
323391942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DATA_EXIST);
3234b3d208f9SJaegeuk Kim }
3235b3d208f9SJaegeuk Kim 
3236510022a8SJaegeuk Kim static inline int f2fs_has_inline_dots(struct inode *inode)
3237510022a8SJaegeuk Kim {
323891942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DOTS);
3239510022a8SJaegeuk Kim }
3240510022a8SJaegeuk Kim 
32414c8ff709SChao Yu static inline int f2fs_is_mmap_file(struct inode *inode)
32424c8ff709SChao Yu {
32434c8ff709SChao Yu 	return is_inode_flag_set(inode, FI_MMAP_FILE);
32444c8ff709SChao Yu }
32454c8ff709SChao Yu 
32461ad71a27SJaegeuk Kim static inline bool f2fs_is_pinned_file(struct inode *inode)
32471ad71a27SJaegeuk Kim {
32481ad71a27SJaegeuk Kim 	return is_inode_flag_set(inode, FI_PIN_FILE);
32491ad71a27SJaegeuk Kim }
32501ad71a27SJaegeuk Kim 
325188b88a66SJaegeuk Kim static inline bool f2fs_is_atomic_file(struct inode *inode)
325288b88a66SJaegeuk Kim {
325391942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_ATOMIC_FILE);
325488b88a66SJaegeuk Kim }
325588b88a66SJaegeuk Kim 
32564a2c5b79SYe Bin static inline bool f2fs_is_cow_file(struct inode *inode)
32574a2c5b79SYe Bin {
32584a2c5b79SYe Bin 	return is_inode_flag_set(inode, FI_COW_FILE);
32594a2c5b79SYe Bin }
32604a2c5b79SYe Bin 
32613c6c2bebSJaegeuk Kim static inline bool f2fs_is_first_block_written(struct inode *inode)
32623c6c2bebSJaegeuk Kim {
326391942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
32643c6c2bebSJaegeuk Kim }
32653c6c2bebSJaegeuk Kim 
32661e84371fSJaegeuk Kim static inline bool f2fs_is_drop_cache(struct inode *inode)
32671e84371fSJaegeuk Kim {
326891942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DROP_CACHE);
32691e84371fSJaegeuk Kim }
32701e84371fSJaegeuk Kim 
3271f2470371SChao Yu static inline void *inline_data_addr(struct inode *inode, struct page *page)
32721001b347SHuajun Li {
3273695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
32747a2af766SChao Yu 	int extra_size = get_extra_isize(inode);
3275cac5a3d8SDongOh Shin 
32767a2af766SChao Yu 	return (void *)&(ri->i_addr[extra_size + DEF_INLINE_RESERVED_SIZE]);
32771001b347SHuajun Li }
32781001b347SHuajun Li 
327934d67debSChao Yu static inline int f2fs_has_inline_dentry(struct inode *inode)
328034d67debSChao Yu {
328191942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DENTRY);
328234d67debSChao Yu }
328334d67debSChao Yu 
3284b5492af7SJaegeuk Kim static inline int is_file(struct inode *inode, int type)
3285b5492af7SJaegeuk Kim {
3286b5492af7SJaegeuk Kim 	return F2FS_I(inode)->i_advise & type;
3287b5492af7SJaegeuk Kim }
3288b5492af7SJaegeuk Kim 
3289b5492af7SJaegeuk Kim static inline void set_file(struct inode *inode, int type)
3290b5492af7SJaegeuk Kim {
3291766c6639SJaegeuk Kim 	if (is_file(inode, type))
3292766c6639SJaegeuk Kim 		return;
3293b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise |= type;
32947c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3295b5492af7SJaegeuk Kim }
3296b5492af7SJaegeuk Kim 
3297b5492af7SJaegeuk Kim static inline void clear_file(struct inode *inode, int type)
3298b5492af7SJaegeuk Kim {
3299766c6639SJaegeuk Kim 	if (!is_file(inode, type))
3300766c6639SJaegeuk Kim 		return;
3301b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise &= ~type;
33027c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3303b5492af7SJaegeuk Kim }
3304b5492af7SJaegeuk Kim 
3305fe1897eaSChao Yu static inline bool f2fs_is_time_consistent(struct inode *inode)
3306fe1897eaSChao Yu {
3307fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time, &inode->i_atime))
3308fe1897eaSChao Yu 		return false;
3309fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 1, &inode->i_ctime))
3310fe1897eaSChao Yu 		return false;
3311fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 2, &inode->i_mtime))
3312fe1897eaSChao Yu 		return false;
3313fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 3,
3314fe1897eaSChao Yu 						&F2FS_I(inode)->i_crtime))
3315fe1897eaSChao Yu 		return false;
3316fe1897eaSChao Yu 	return true;
3317fe1897eaSChao Yu }
3318fe1897eaSChao Yu 
331926787236SJaegeuk Kim static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
332026787236SJaegeuk Kim {
3321a0d00fadSChao Yu 	bool ret;
3322a0d00fadSChao Yu 
332326787236SJaegeuk Kim 	if (dsync) {
332426787236SJaegeuk Kim 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
332526787236SJaegeuk Kim 
332626787236SJaegeuk Kim 		spin_lock(&sbi->inode_lock[DIRTY_META]);
332726787236SJaegeuk Kim 		ret = list_empty(&F2FS_I(inode)->gdirty_list);
332826787236SJaegeuk Kim 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
332926787236SJaegeuk Kim 		return ret;
333026787236SJaegeuk Kim 	}
333126787236SJaegeuk Kim 	if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
333226787236SJaegeuk Kim 			file_keep_isize(inode) ||
3333235831d7SJunling Zheng 			i_size_read(inode) & ~PAGE_MASK)
333426787236SJaegeuk Kim 		return false;
3335a0d00fadSChao Yu 
3336fe1897eaSChao Yu 	if (!f2fs_is_time_consistent(inode))
3337214c2461SJaegeuk Kim 		return false;
3338214c2461SJaegeuk Kim 
3339c10c9820SChao Yu 	spin_lock(&F2FS_I(inode)->i_size_lock);
3340a0d00fadSChao Yu 	ret = F2FS_I(inode)->last_disk_size == i_size_read(inode);
3341c10c9820SChao Yu 	spin_unlock(&F2FS_I(inode)->i_size_lock);
3342a0d00fadSChao Yu 
3343a0d00fadSChao Yu 	return ret;
3344267378d4SChao Yu }
3345267378d4SChao Yu 
3346deeedd71SChao Yu static inline bool f2fs_readonly(struct super_block *sb)
334777888c1eSJaegeuk Kim {
3348deeedd71SChao Yu 	return sb_rdonly(sb);
334977888c1eSJaegeuk Kim }
335077888c1eSJaegeuk Kim 
3351744602cfSJaegeuk Kim static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
3352744602cfSJaegeuk Kim {
3353aaec2b1dSChao Yu 	return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
3354744602cfSJaegeuk Kim }
3355744602cfSJaegeuk Kim 
335643c780baSEric Biggers static inline bool is_dot_dotdot(const u8 *name, size_t len)
3357eaa693f4SJaegeuk Kim {
335843c780baSEric Biggers 	if (len == 1 && name[0] == '.')
3359eaa693f4SJaegeuk Kim 		return true;
3360eaa693f4SJaegeuk Kim 
336143c780baSEric Biggers 	if (len == 2 && name[0] == '.' && name[1] == '.')
3362eaa693f4SJaegeuk Kim 		return true;
3363eaa693f4SJaegeuk Kim 
3364eaa693f4SJaegeuk Kim 	return false;
3365eaa693f4SJaegeuk Kim }
3366eaa693f4SJaegeuk Kim 
33671ecc0c5cSChao Yu static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
33681ecc0c5cSChao Yu 					size_t size, gfp_t flags)
33690414b004SJaegeuk Kim {
337055523519SChao Yu 	if (time_to_inject(sbi, FAULT_KMALLOC)) {
3371c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_KMALLOC);
33722c63feadSJaegeuk Kim 		return NULL;
337355523519SChao Yu 	}
33747fa750a1SArnd Bergmann 
33750b6d4ca0SEric Biggers 	return kmalloc(size, flags);
33760414b004SJaegeuk Kim }
33770414b004SJaegeuk Kim 
3378acbf054dSChao Yu static inline void *f2fs_kzalloc(struct f2fs_sb_info *sbi,
3379acbf054dSChao Yu 					size_t size, gfp_t flags)
3380acbf054dSChao Yu {
3381acbf054dSChao Yu 	return f2fs_kmalloc(sbi, size, flags | __GFP_ZERO);
3382acbf054dSChao Yu }
3383acbf054dSChao Yu 
3384628b3d14SChao Yu static inline void *f2fs_kvmalloc(struct f2fs_sb_info *sbi,
3385628b3d14SChao Yu 					size_t size, gfp_t flags)
3386628b3d14SChao Yu {
3387628b3d14SChao Yu 	if (time_to_inject(sbi, FAULT_KVMALLOC)) {
3388c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_KVMALLOC);
3389628b3d14SChao Yu 		return NULL;
3390628b3d14SChao Yu 	}
33917fa750a1SArnd Bergmann 
3392628b3d14SChao Yu 	return kvmalloc(size, flags);
3393628b3d14SChao Yu }
3394628b3d14SChao Yu 
3395628b3d14SChao Yu static inline void *f2fs_kvzalloc(struct f2fs_sb_info *sbi,
3396628b3d14SChao Yu 					size_t size, gfp_t flags)
3397628b3d14SChao Yu {
3398628b3d14SChao Yu 	return f2fs_kvmalloc(sbi, size, flags | __GFP_ZERO);
3399628b3d14SChao Yu }
3400628b3d14SChao Yu 
34017a2af766SChao Yu static inline int get_extra_isize(struct inode *inode)
3402f2470371SChao Yu {
34037a2af766SChao Yu 	return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
3404f2470371SChao Yu }
3405f2470371SChao Yu 
34066afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode)
34076afc662eSChao Yu {
34086afc662eSChao Yu 	return F2FS_I(inode)->i_inline_xattr_size;
34096afc662eSChao Yu }
34106afc662eSChao Yu 
34114d57b86dSChao Yu #define f2fs_get_inode_mode(i) \
341291942321SJaegeuk Kim 	((is_inode_flag_set(i, FI_ACL_MODE)) ? \
3413a6dda0e6SChristoph Hellwig 	 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
3414a6dda0e6SChristoph Hellwig 
34157a2af766SChao Yu #define F2FS_TOTAL_EXTRA_ATTR_SIZE			\
34167a2af766SChao Yu 	(offsetof(struct f2fs_inode, i_extra_end) -	\
34177a2af766SChao Yu 	offsetof(struct f2fs_inode, i_extra_isize))	\
34187a2af766SChao Yu 
34195c57132eSChao Yu #define F2FS_OLD_ATTRIBUTE_SIZE	(offsetof(struct f2fs_inode, i_addr))
34205c57132eSChao Yu #define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field)		\
34212f84babfSSheng Yong 		((offsetof(typeof(*(f2fs_inode)), field) +	\
34225c57132eSChao Yu 		sizeof((f2fs_inode)->field))			\
34232f84babfSSheng Yong 		<= (F2FS_OLD_ATTRIBUTE_SIZE + (extra_isize)))	\
34245c57132eSChao Yu 
34252c70c5e3SChao Yu #define __is_large_section(sbi)		((sbi)->segs_per_sec > 1)
34262c70c5e3SChao Yu 
34276dc3a126SChao Yu #define __is_meta_io(fio) (PAGE_TYPE_OF_BIO((fio)->type) == META)
3428c9b60788SChao Yu 
3429e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
3430e1da7872SChao Yu 					block_t blkaddr, int type);
3431e1da7872SChao Yu static inline void verify_blkaddr(struct f2fs_sb_info *sbi,
3432e1da7872SChao Yu 					block_t blkaddr, int type)
3433e1da7872SChao Yu {
3434e1da7872SChao Yu 	if (!f2fs_is_valid_blkaddr(sbi, blkaddr, type)) {
3435dcbb4c10SJoe Perches 		f2fs_err(sbi, "invalid blkaddr: %u, type: %d, run fsck to fix.",
3436e1da7872SChao Yu 			 blkaddr, type);
3437e1da7872SChao Yu 		f2fs_bug_on(sbi, 1);
3438e1da7872SChao Yu 	}
3439e1da7872SChao Yu }
3440e1da7872SChao Yu 
3441e1da7872SChao Yu static inline bool __is_valid_data_blkaddr(block_t blkaddr)
34427b525dd0SChao Yu {
34434c8ff709SChao Yu 	if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR ||
34444c8ff709SChao Yu 			blkaddr == COMPRESS_ADDR)
34457b525dd0SChao Yu 		return false;
34467b525dd0SChao Yu 	return true;
34477b525dd0SChao Yu }
34487b525dd0SChao Yu 
344939a53e0cSJaegeuk Kim /*
345039a53e0cSJaegeuk Kim  * file.c
345139a53e0cSJaegeuk Kim  */
3452cac5a3d8SDongOh Shin int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
34534d57b86dSChao Yu void f2fs_truncate_data_blocks(struct dnode_of_data *dn);
34543265d3dbSChao Yu int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock);
3455c42d28ceSChao Yu int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock);
3456cac5a3d8SDongOh Shin int f2fs_truncate(struct inode *inode);
3457549c7297SChristian Brauner int f2fs_getattr(struct user_namespace *mnt_userns, const struct path *path,
3458549c7297SChristian Brauner 		 struct kstat *stat, u32 request_mask, unsigned int flags);
3459549c7297SChristian Brauner int f2fs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
3460549c7297SChristian Brauner 		 struct iattr *attr);
34614d57b86dSChao Yu int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
34624d57b86dSChao Yu void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count);
3463c4020b2dSChao Yu int f2fs_precache_extents(struct inode *inode);
34649b1bb01cSMiklos Szeredi int f2fs_fileattr_get(struct dentry *dentry, struct fileattr *fa);
34659b1bb01cSMiklos Szeredi int f2fs_fileattr_set(struct user_namespace *mnt_userns,
34669b1bb01cSMiklos Szeredi 		      struct dentry *dentry, struct fileattr *fa);
3467cac5a3d8SDongOh Shin long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
3468cac5a3d8SDongOh Shin long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
346978130819SChao Yu int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid);
34701ad71a27SJaegeuk Kim int f2fs_pin_file_control(struct inode *inode, bool inc);
347139a53e0cSJaegeuk Kim 
347239a53e0cSJaegeuk Kim /*
347339a53e0cSJaegeuk Kim  * inode.c
347439a53e0cSJaegeuk Kim  */
3475cac5a3d8SDongOh Shin void f2fs_set_inode_flags(struct inode *inode);
3476704956ecSChao Yu bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page);
3477704956ecSChao Yu void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page);
3478cac5a3d8SDongOh Shin struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
3479cac5a3d8SDongOh Shin struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
34804d57b86dSChao Yu int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
34814d57b86dSChao Yu void f2fs_update_inode(struct inode *inode, struct page *node_page);
34824d57b86dSChao Yu void f2fs_update_inode_page(struct inode *inode);
3483cac5a3d8SDongOh Shin int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
3484cac5a3d8SDongOh Shin void f2fs_evict_inode(struct inode *inode);
34854d57b86dSChao Yu void f2fs_handle_failed_inode(struct inode *inode);
348639a53e0cSJaegeuk Kim 
348739a53e0cSJaegeuk Kim /*
348839a53e0cSJaegeuk Kim  * namei.c
348939a53e0cSJaegeuk Kim  */
34904d57b86dSChao Yu int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
3491b6a06cbbSChao Yu 							bool hot, bool set);
349239a53e0cSJaegeuk Kim struct dentry *f2fs_get_parent(struct dentry *child);
34933db1de0eSDaeho Jeong int f2fs_get_tmpfile(struct user_namespace *mnt_userns, struct inode *dir,
34943db1de0eSDaeho Jeong 		     struct inode **new_inode);
349539a53e0cSJaegeuk Kim 
349639a53e0cSJaegeuk Kim /*
349739a53e0cSJaegeuk Kim  * dir.c
349839a53e0cSJaegeuk Kim  */
34994d57b86dSChao Yu unsigned char f2fs_get_de_type(struct f2fs_dir_entry *de);
350043c780baSEric Biggers int f2fs_init_casefolded_name(const struct inode *dir,
350143c780baSEric Biggers 			      struct f2fs_filename *fname);
350243c780baSEric Biggers int f2fs_setup_filename(struct inode *dir, const struct qstr *iname,
350343c780baSEric Biggers 			int lookup, struct f2fs_filename *fname);
350443c780baSEric Biggers int f2fs_prepare_lookup(struct inode *dir, struct dentry *dentry,
350543c780baSEric Biggers 			struct f2fs_filename *fname);
350643c780baSEric Biggers void f2fs_free_filename(struct f2fs_filename *fname);
350743c780baSEric Biggers struct f2fs_dir_entry *f2fs_find_target_dentry(const struct f2fs_dentry_ptr *d,
350843c780baSEric Biggers 			const struct f2fs_filename *fname, int *max_slots);
3509cac5a3d8SDongOh Shin int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
3510cac5a3d8SDongOh Shin 			unsigned int start_pos, struct fscrypt_str *fstr);
35114d57b86dSChao Yu void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
3512cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
35134d57b86dSChao Yu struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
351443c780baSEric Biggers 			const struct f2fs_filename *fname, struct page *dpage);
35154d57b86dSChao Yu void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
3516cac5a3d8SDongOh Shin 			unsigned int current_depth);
35174d57b86dSChao Yu int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots);
3518cac5a3d8SDongOh Shin void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
3519cac5a3d8SDongOh Shin struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
352043c780baSEric Biggers 					 const struct f2fs_filename *fname,
352143c780baSEric Biggers 					 struct page **res_page);
3522cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
3523cac5a3d8SDongOh Shin 			const struct qstr *child, struct page **res_page);
3524cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
3525cac5a3d8SDongOh Shin ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
3526cac5a3d8SDongOh Shin 			struct page **page);
3527cac5a3d8SDongOh Shin void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
3528cac5a3d8SDongOh Shin 			struct page *page, struct inode *inode);
3529b06af2afSJaegeuk Kim bool f2fs_has_enough_room(struct inode *dir, struct page *ipage,
353043c780baSEric Biggers 			  const struct f2fs_filename *fname);
3531cac5a3d8SDongOh Shin void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
353243c780baSEric Biggers 			const struct fscrypt_str *name, f2fs_hash_t name_hash,
3533cac5a3d8SDongOh Shin 			unsigned int bit_pos);
353443c780baSEric Biggers int f2fs_add_regular_entry(struct inode *dir, const struct f2fs_filename *fname,
3535cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
353643c780baSEric Biggers int f2fs_add_dentry(struct inode *dir, const struct f2fs_filename *fname,
3537cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
35384d57b86dSChao Yu int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
3539cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
3540cac5a3d8SDongOh Shin void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
3541cac5a3d8SDongOh Shin 			struct inode *dir, struct inode *inode);
3542cac5a3d8SDongOh Shin int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
3543cac5a3d8SDongOh Shin bool f2fs_empty_dir(struct inode *dir);
354439a53e0cSJaegeuk Kim 
3545b7f7a5e0SAl Viro static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
3546b7f7a5e0SAl Viro {
3547bfc2b7e8SEric Biggers 	if (fscrypt_is_nokey_name(dentry))
3548bfc2b7e8SEric Biggers 		return -ENOKEY;
35494d57b86dSChao Yu 	return f2fs_do_add_link(d_inode(dentry->d_parent), &dentry->d_name,
3550510022a8SJaegeuk Kim 				inode, inode->i_ino, inode->i_mode);
3551b7f7a5e0SAl Viro }
3552b7f7a5e0SAl Viro 
355339a53e0cSJaegeuk Kim /*
355439a53e0cSJaegeuk Kim  * super.c
355539a53e0cSJaegeuk Kim  */
3556cac5a3d8SDongOh Shin int f2fs_inode_dirtied(struct inode *inode, bool sync);
3557cac5a3d8SDongOh Shin void f2fs_inode_synced(struct inode *inode);
355810a26878SChao Yu int f2fs_dquot_initialize(struct inode *inode);
3559ea676733SJaegeuk Kim int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly);
3560af033b2aSChao Yu int f2fs_quota_sync(struct super_block *sb, int type);
35616d1451bfSChengguang Xu loff_t max_file_blocks(struct inode *inode);
35624b2414d0SChao Yu void f2fs_quota_off_umount(struct super_block *sb);
3563a9cfee0eSChao Yu void f2fs_handle_stop(struct f2fs_sb_info *sbi, unsigned char reason);
3564*95fa90c9SChao Yu void f2fs_handle_error(struct f2fs_sb_info *sbi, unsigned char error);
3565cac5a3d8SDongOh Shin int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
3566cac5a3d8SDongOh Shin int f2fs_sync_fs(struct super_block *sb, int sync);
35674d57b86dSChao Yu int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi);
356839a53e0cSJaegeuk Kim 
356939a53e0cSJaegeuk Kim /*
357039a53e0cSJaegeuk Kim  * hash.c
357139a53e0cSJaegeuk Kim  */
357243c780baSEric Biggers void f2fs_hash_filename(const struct inode *dir, struct f2fs_filename *fname);
357339a53e0cSJaegeuk Kim 
357439a53e0cSJaegeuk Kim /*
357539a53e0cSJaegeuk Kim  * node.c
357639a53e0cSJaegeuk Kim  */
357739a53e0cSJaegeuk Kim struct node_info;
357839a53e0cSJaegeuk Kim 
35794d57b86dSChao Yu int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid);
35804d57b86dSChao Yu bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type);
358150fa53ecSChao Yu bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct page *page);
358250fa53ecSChao Yu void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi);
358350fa53ecSChao Yu void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct page *page);
358450fa53ecSChao Yu void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi);
35854d57b86dSChao Yu int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
35864d57b86dSChao Yu bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
35874d57b86dSChao Yu bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
35887735730dSChao Yu int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
3589a9419b63SJaegeuk Kim 				struct node_info *ni, bool checkpoint_context);
35904d57b86dSChao Yu pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
35914d57b86dSChao Yu int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
35924d57b86dSChao Yu int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from);
35934d57b86dSChao Yu int f2fs_truncate_xattr_node(struct inode *inode);
359450fa53ecSChao Yu int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
359550fa53ecSChao Yu 					unsigned int seq_id);
359694c821fbSChao Yu bool f2fs_nat_bitmap_enabled(struct f2fs_sb_info *sbi);
35974d57b86dSChao Yu int f2fs_remove_inode_page(struct inode *inode);
35984d57b86dSChao Yu struct page *f2fs_new_inode_page(struct inode *inode);
35994d57b86dSChao Yu struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs);
36004d57b86dSChao Yu void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
36014d57b86dSChao Yu struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
36024d57b86dSChao Yu struct page *f2fs_get_node_page_ra(struct page *parent, int start);
360348018b4cSChao Yu int f2fs_move_node_page(struct page *node_page, int gc_type);
360468e79bafSJia Yang void f2fs_flush_inline_data(struct f2fs_sb_info *sbi);
36054d57b86dSChao Yu int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
360650fa53ecSChao Yu 			struct writeback_control *wbc, bool atomic,
360750fa53ecSChao Yu 			unsigned int *seq_id);
36084d57b86dSChao Yu int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
36094d57b86dSChao Yu 			struct writeback_control *wbc,
3610b0af6d49SChao Yu 			bool do_balance, enum iostat_type io_type);
3611e2374015SChao Yu int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
36124d57b86dSChao Yu bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
36134d57b86dSChao Yu void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
36144d57b86dSChao Yu void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
36154d57b86dSChao Yu int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
36169627a7b3SChao Yu int f2fs_recover_inline_xattr(struct inode *inode, struct page *page);
36174d57b86dSChao Yu int f2fs_recover_xattr_data(struct inode *inode, struct page *page);
36184d57b86dSChao Yu int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
36197735730dSChao Yu int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
3620cac5a3d8SDongOh Shin 			unsigned int segno, struct f2fs_summary_block *sum);
362194c821fbSChao Yu void f2fs_enable_nat_bits(struct f2fs_sb_info *sbi);
3622edc55aafSJaegeuk Kim int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
36234d57b86dSChao Yu int f2fs_build_node_manager(struct f2fs_sb_info *sbi);
36244d57b86dSChao Yu void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi);
36254d57b86dSChao Yu int __init f2fs_create_node_manager_caches(void);
36264d57b86dSChao Yu void f2fs_destroy_node_manager_caches(void);
362739a53e0cSJaegeuk Kim 
362839a53e0cSJaegeuk Kim /*
362939a53e0cSJaegeuk Kim  * segment.c
363039a53e0cSJaegeuk Kim  */
36314d57b86dSChao Yu bool f2fs_need_SSR(struct f2fs_sb_info *sbi);
36323db1de0eSDaeho Jeong int f2fs_commit_atomic_write(struct inode *inode);
36333db1de0eSDaeho Jeong void f2fs_abort_atomic_write(struct inode *inode, bool clean);
3634cac5a3d8SDongOh Shin void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
36357bcd0cfaSChao Yu void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi, bool from_bg);
363639d787beSChao Yu int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
36374d57b86dSChao Yu int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
36381228b482SChao Yu int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
36394d57b86dSChao Yu void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
36404d57b86dSChao Yu void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
36414d57b86dSChao Yu bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
36424d674904SFengnan Chang int f2fs_start_discard_thread(struct f2fs_sb_info *sbi);
36434d57b86dSChao Yu void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
36444d57b86dSChao Yu void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi);
364503f2c02dSJaegeuk Kim bool f2fs_issue_discard_timeout(struct f2fs_sb_info *sbi);
36464d57b86dSChao Yu void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
36474d57b86dSChao Yu 					struct cp_control *cpc);
36484354994fSDaniel Rosenberg void f2fs_dirty_to_prefree(struct f2fs_sb_info *sbi);
36494d3aed70SDaniel Rosenberg block_t f2fs_get_unusable_blocks(struct f2fs_sb_info *sbi);
36504d3aed70SDaniel Rosenberg int f2fs_disable_cp_again(struct f2fs_sb_info *sbi, block_t unusable);
36514d57b86dSChao Yu void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi);
36524d57b86dSChao Yu int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
365361461fc9SChao Yu bool f2fs_segment_has_free_slot(struct f2fs_sb_info *sbi, int segno);
3654093749e2SChao Yu void f2fs_init_inmem_curseg(struct f2fs_sb_info *sbi);
3655093749e2SChao Yu void f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi);
3656093749e2SChao Yu void f2fs_restore_inmem_curseg(struct f2fs_sb_info *sbi);
3657093749e2SChao Yu void f2fs_get_new_segment(struct f2fs_sb_info *sbi,
3658093749e2SChao Yu 			unsigned int *newseg, bool new_sec, int dir);
36590ef81833SChao Yu void f2fs_allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type,
366004f0b2eaSQiuyang Sun 					unsigned int start, unsigned int end);
3661509f1010SChao Yu void f2fs_allocate_new_section(struct f2fs_sb_info *sbi, int type, bool force);
3662901d745fSChao Yu void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi);
3663cac5a3d8SDongOh Shin int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
36644d57b86dSChao Yu bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
36654d57b86dSChao Yu 					struct cp_control *cpc);
36664d57b86dSChao Yu struct page *f2fs_get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
36674d57b86dSChao Yu void f2fs_update_meta_page(struct f2fs_sb_info *sbi, void *src,
36684d57b86dSChao Yu 					block_t blk_addr);
36694d57b86dSChao Yu void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
3670b0af6d49SChao Yu 						enum iostat_type io_type);
36714d57b86dSChao Yu void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio);
36724d57b86dSChao Yu void f2fs_outplace_write_data(struct dnode_of_data *dn,
36734d57b86dSChao Yu 			struct f2fs_io_info *fio);
36744d57b86dSChao Yu int f2fs_inplace_write_data(struct f2fs_io_info *fio);
36754d57b86dSChao Yu void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
3676cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t new_blkaddr,
3677c5d02785SChao Yu 			bool recover_curseg, bool recover_newaddr,
3678c5d02785SChao Yu 			bool from_gc);
3679cac5a3d8SDongOh Shin void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
3680cac5a3d8SDongOh Shin 			block_t old_addr, block_t new_addr,
3681cac5a3d8SDongOh Shin 			unsigned char version, bool recover_curseg,
3682cac5a3d8SDongOh Shin 			bool recover_newaddr);
36834d57b86dSChao Yu void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
3684cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t *new_blkaddr,
3685fb830fc5SChao Yu 			struct f2fs_summary *sum, int type,
3686f608c38cSChao Yu 			struct f2fs_io_info *fio);
368771f2c820SChao Yu void f2fs_update_device_state(struct f2fs_sb_info *sbi, nid_t ino,
368871f2c820SChao Yu 					block_t blkaddr, unsigned int blkcnt);
3689cac5a3d8SDongOh Shin void f2fs_wait_on_page_writeback(struct page *page,
3690bae0ee7aSChao Yu 			enum page_type type, bool ordered, bool locked);
36910ded69f6SJaegeuk Kim void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr);
36921e78e8bdSSahitya Tummala void f2fs_wait_on_block_writeback_range(struct inode *inode, block_t blkaddr,
36931e78e8bdSSahitya Tummala 								block_t len);
36944d57b86dSChao Yu void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
36954d57b86dSChao Yu void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
36964d57b86dSChao Yu int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
3697cac5a3d8SDongOh Shin 			unsigned int val, int alloc);
36984d57b86dSChao Yu void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
3699c426d991SShin'ichiro Kawasaki int f2fs_fix_curseg_write_pointer(struct f2fs_sb_info *sbi);
3700d508c94eSShin'ichiro Kawasaki int f2fs_check_write_pointer(struct f2fs_sb_info *sbi);
37014d57b86dSChao Yu int f2fs_build_segment_manager(struct f2fs_sb_info *sbi);
37024d57b86dSChao Yu void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi);
37034d57b86dSChao Yu int __init f2fs_create_segment_manager_caches(void);
37044d57b86dSChao Yu void f2fs_destroy_segment_manager_caches(void);
37054d57b86dSChao Yu int f2fs_rw_hint_to_seg_type(enum rw_hint hint);
3706de881df9SAravind Ramesh unsigned int f2fs_usable_segs_in_sec(struct f2fs_sb_info *sbi,
3707de881df9SAravind Ramesh 			unsigned int segno);
3708de881df9SAravind Ramesh unsigned int f2fs_usable_blks_in_seg(struct f2fs_sb_info *sbi,
3709de881df9SAravind Ramesh 			unsigned int segno);
371039a53e0cSJaegeuk Kim 
37116691d940SDaeho Jeong #define DEF_FRAGMENT_SIZE	4
37126691d940SDaeho Jeong #define MIN_FRAGMENT_SIZE	1
37136691d940SDaeho Jeong #define MAX_FRAGMENT_SIZE	512
37146691d940SDaeho Jeong 
37156691d940SDaeho Jeong static inline bool f2fs_need_rand_seg(struct f2fs_sb_info *sbi)
37166691d940SDaeho Jeong {
37176691d940SDaeho Jeong 	return F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_SEG ||
37186691d940SDaeho Jeong 		F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK;
37196691d940SDaeho Jeong }
37206691d940SDaeho Jeong 
372139a53e0cSJaegeuk Kim /*
372239a53e0cSJaegeuk Kim  * checkpoint.c
372339a53e0cSJaegeuk Kim  */
3724a9cfee0eSChao Yu void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io,
3725a9cfee0eSChao Yu 							unsigned char reason);
3726c7b58576SJaegeuk Kim void f2fs_flush_ckpt_thread(struct f2fs_sb_info *sbi);
37274d57b86dSChao Yu struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
37284d57b86dSChao Yu struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
372986f33603SJaegeuk Kim struct page *f2fs_get_meta_page_retry(struct f2fs_sb_info *sbi, pgoff_t index);
37304d57b86dSChao Yu struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
3731e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
37324d57b86dSChao Yu 					block_t blkaddr, int type);
37334d57b86dSChao Yu int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
3734cac5a3d8SDongOh Shin 			int type, bool sync);
3735430f163bSChao Yu void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index,
3736430f163bSChao Yu 							unsigned int ra_blocks);
37374d57b86dSChao Yu long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
3738b0af6d49SChao Yu 			long nr_to_write, enum iostat_type io_type);
37394d57b86dSChao Yu void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
37404d57b86dSChao Yu void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
37414d57b86dSChao Yu void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all);
37424d57b86dSChao Yu bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
37434d57b86dSChao Yu void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
374439d787beSChao Yu 					unsigned int devidx, int type);
37454d57b86dSChao Yu bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
374639d787beSChao Yu 					unsigned int devidx, int type);
3747cac5a3d8SDongOh Shin int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
37484d57b86dSChao Yu int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi);
37494d57b86dSChao Yu void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi);
37504d57b86dSChao Yu void f2fs_add_orphan_inode(struct inode *inode);
37514d57b86dSChao Yu void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
37524d57b86dSChao Yu int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi);
37534d57b86dSChao Yu int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi);
37544f5e34f7SMatthew Wilcox (Oracle) void f2fs_update_dirty_folio(struct inode *inode, struct folio *folio);
37554d57b86dSChao Yu void f2fs_remove_dirty_inode(struct inode *inode);
3756d80afefbSChao Yu int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type,
3757d80afefbSChao Yu 								bool from_cp);
3758bf22c3ccSSahitya Tummala void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type);
37593a0a9cbcSChao Yu u64 f2fs_get_sectors_written(struct f2fs_sb_info *sbi);
37604d57b86dSChao Yu int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
37614d57b86dSChao Yu void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi);
37624d57b86dSChao Yu int __init f2fs_create_checkpoint_caches(void);
37634d57b86dSChao Yu void f2fs_destroy_checkpoint_caches(void);
3764261eeb9cSDaeho Jeong int f2fs_issue_checkpoint(struct f2fs_sb_info *sbi);
3765261eeb9cSDaeho Jeong int f2fs_start_ckpt_thread(struct f2fs_sb_info *sbi);
3766261eeb9cSDaeho Jeong void f2fs_stop_ckpt_thread(struct f2fs_sb_info *sbi);
3767261eeb9cSDaeho Jeong void f2fs_init_ckpt_req_control(struct f2fs_sb_info *sbi);
376839a53e0cSJaegeuk Kim 
376939a53e0cSJaegeuk Kim /*
377039a53e0cSJaegeuk Kim  * data.c
377139a53e0cSJaegeuk Kim  */
3772f543805fSChao Yu int __init f2fs_init_bioset(void);
3773f543805fSChao Yu void f2fs_destroy_bioset(void);
37740b20fcecSChao Yu int f2fs_init_bio_entry_cache(void);
37750b20fcecSChao Yu void f2fs_destroy_bio_entry_cache(void);
37764c8ff709SChao Yu void f2fs_submit_bio(struct f2fs_sb_info *sbi,
37774c8ff709SChao Yu 				struct bio *bio, enum page_type type);
3778908ea654SYufen Yu int f2fs_init_write_merge_io(struct f2fs_sb_info *sbi);
3779b9109b0eSJaegeuk Kim void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
3780b9109b0eSJaegeuk Kim void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
3781bab475c5SChao Yu 				struct inode *inode, struct page *page,
3782bab475c5SChao Yu 				nid_t ino, enum page_type type);
37830b20fcecSChao Yu void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
37840b20fcecSChao Yu 					struct bio **bio, struct page *page);
3785b9109b0eSJaegeuk Kim void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
3786cac5a3d8SDongOh Shin int f2fs_submit_page_bio(struct f2fs_io_info *fio);
37878648de2cSChao Yu int f2fs_merge_page_bio(struct f2fs_io_info *fio);
3788fe16efe6SChao Yu void f2fs_submit_page_write(struct f2fs_io_info *fio);
3789cac5a3d8SDongOh Shin struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
37905189810aSChristoph Hellwig 		block_t blk_addr, sector_t *sector);
3791cac5a3d8SDongOh Shin int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
37924d57b86dSChao Yu void f2fs_set_data_blkaddr(struct dnode_of_data *dn);
3793cac5a3d8SDongOh Shin void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
37944d57b86dSChao Yu int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
37954d57b86dSChao Yu int f2fs_reserve_new_block(struct dnode_of_data *dn);
3796cac5a3d8SDongOh Shin int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
3797cac5a3d8SDongOh Shin int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
37984d57b86dSChao Yu struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
37997649c873SBart Van Assche 			blk_opf_t op_flags, bool for_write);
38004d57b86dSChao Yu struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index);
38014d57b86dSChao Yu struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
3802cac5a3d8SDongOh Shin 			bool for_write);
38034d57b86dSChao Yu struct page *f2fs_get_new_data_page(struct inode *inode,
3804cac5a3d8SDongOh Shin 			struct page *ipage, pgoff_t index, bool new_i_size);
38054d57b86dSChao Yu int f2fs_do_write_data_page(struct f2fs_io_info *fio);
38060ef81833SChao Yu void f2fs_do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock);
3807cac5a3d8SDongOh Shin int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
3808cac5a3d8SDongOh Shin 			int create, int flag);
3809cac5a3d8SDongOh Shin int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3810cac5a3d8SDongOh Shin 			u64 start, u64 len);
38114c8ff709SChao Yu int f2fs_encrypt_one_page(struct f2fs_io_info *fio);
38124d57b86dSChao Yu bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio);
38134d57b86dSChao Yu bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio);
38144c8ff709SChao Yu int f2fs_write_single_data_page(struct page *page, int *submitted,
38154c8ff709SChao Yu 				struct bio **bio, sector_t *last_block,
38164c8ff709SChao Yu 				struct writeback_control *wbc,
38174c8ff709SChao Yu 				enum iostat_type io_type,
38183afae09fSChao Yu 				int compr_blocks, bool allow_balance);
3819a1e09b03SEric Biggers void f2fs_write_failed(struct inode *inode, loff_t to);
382091503996SMatthew Wilcox (Oracle) void f2fs_invalidate_folio(struct folio *folio, size_t offset, size_t length);
3821c26cd045SMatthew Wilcox (Oracle) bool f2fs_release_folio(struct folio *folio, gfp_t wait);
3822b91050a8SHyunchul Lee bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len);
38235ec2d99dSMatthew Wilcox void f2fs_clear_page_cache_dirty_tag(struct page *page);
38244c8ff709SChao Yu int f2fs_init_post_read_processing(void);
38254c8ff709SChao Yu void f2fs_destroy_post_read_processing(void);
38264c8ff709SChao Yu int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi);
38274c8ff709SChao Yu void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi);
38281517c1a7SEric Biggers extern const struct iomap_ops f2fs_iomap_ops;
382939a53e0cSJaegeuk Kim 
383039a53e0cSJaegeuk Kim /*
383139a53e0cSJaegeuk Kim  * gc.c
383239a53e0cSJaegeuk Kim  */
38334d57b86dSChao Yu int f2fs_start_gc_thread(struct f2fs_sb_info *sbi);
38344d57b86dSChao Yu void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi);
38354d57b86dSChao Yu block_t f2fs_start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
3836d147ea4aSJaegeuk Kim int f2fs_gc(struct f2fs_sb_info *sbi, struct f2fs_gc_control *gc_control);
38374d57b86dSChao Yu void f2fs_build_gc_manager(struct f2fs_sb_info *sbi);
383804f0b2eaSQiuyang Sun int f2fs_resize_fs(struct f2fs_sb_info *sbi, __u64 block_count);
3839093749e2SChao Yu int __init f2fs_create_garbage_collection_cache(void);
3840093749e2SChao Yu void f2fs_destroy_garbage_collection_cache(void);
384139a53e0cSJaegeuk Kim 
384239a53e0cSJaegeuk Kim /*
384339a53e0cSJaegeuk Kim  * recovery.c
384439a53e0cSJaegeuk Kim  */
38454d57b86dSChao Yu int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
38464d57b86dSChao Yu bool f2fs_space_for_roll_forward(struct f2fs_sb_info *sbi);
3847cad83c96SChao Yu int __init f2fs_create_recovery_cache(void);
3848cad83c96SChao Yu void f2fs_destroy_recovery_cache(void);
384939a53e0cSJaegeuk Kim 
385039a53e0cSJaegeuk Kim /*
385139a53e0cSJaegeuk Kim  * debug.c
385239a53e0cSJaegeuk Kim  */
385339a53e0cSJaegeuk Kim #ifdef CONFIG_F2FS_STAT_FS
385439a53e0cSJaegeuk Kim struct f2fs_stat_info {
385539a53e0cSJaegeuk Kim 	struct list_head stat_list;
385639a53e0cSJaegeuk Kim 	struct f2fs_sb_info *sbi;
385739a53e0cSJaegeuk Kim 	int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
385839a53e0cSJaegeuk Kim 	int main_area_segs, main_area_sections, main_area_zones;
38595b7ee374SChao Yu 	unsigned long long hit_largest, hit_cached, hit_rbtree;
38605b7ee374SChao Yu 	unsigned long long hit_total, total_ext;
3861c00ba554SJaegeuk Kim 	int ext_tree, zombie_tree, ext_node;
38622c8a4a28SJaegeuk Kim 	int ndirty_node, ndirty_dent, ndirty_meta, ndirty_imeta;
38632c8a4a28SJaegeuk Kim 	int ndirty_data, ndirty_qdata;
38642c8a4a28SJaegeuk Kim 	unsigned int ndirty_dirs, ndirty_files, nquota_files, ndirty_all;
38655b0ef73cSJaegeuk Kim 	int nats, dirty_nats, sits, dirty_sits;
38665b0ef73cSJaegeuk Kim 	int free_nids, avail_nids, alloc_nids;
386739a53e0cSJaegeuk Kim 	int total_count, utilization;
38688b8dd65fSChao Yu 	int bg_gc, nr_wb_cp_data, nr_wb_data;
38695f9abab4SJaegeuk Kim 	int nr_rd_data, nr_rd_node, nr_rd_meta;
387002b16d0aSChao Yu 	int nr_dio_read, nr_dio_write;
3871274bd9baSChao Yu 	unsigned int io_skip_bggc, other_skip_bggc;
387214d8d5f7SChao Yu 	int nr_flushing, nr_flushed, flush_list_empty;
387314d8d5f7SChao Yu 	int nr_discarding, nr_discarded;
38745f32366aSChao Yu 	int nr_discard_cmd;
3875d84d1cbdSChao Yu 	unsigned int undiscard_blks;
3876261eeb9cSDaeho Jeong 	int nr_issued_ckpt, nr_total_ckpt, nr_queued_ckpt;
3877261eeb9cSDaeho Jeong 	unsigned int cur_ckpt_time, peak_ckpt_time;
3878a00861dbSJaegeuk Kim 	int inline_xattr, inline_inode, inline_dir, append, update, orphans;
3879ae999bb9SDaeho Jeong 	int compr_inode;
3880ae999bb9SDaeho Jeong 	unsigned long long compr_blocks;
38817bc155feSJaegeuk Kim 	int aw_cnt, max_aw_cnt;
3882f83a2584SYunlei He 	unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
388339a53e0cSJaegeuk Kim 	unsigned int bimodal, avg_vblocks;
388439a53e0cSJaegeuk Kim 	int util_free, util_valid, util_invalid;
388539a53e0cSJaegeuk Kim 	int rsvd_segs, overp_segs;
38866ce19affSChao Yu 	int dirty_count, node_pages, meta_pages, compress_pages;
38876ce19affSChao Yu 	int compress_page_hit;
388842190d2aSJaegeuk Kim 	int prefree_count, call_count, cp_count, bg_cp_count;
388939a53e0cSJaegeuk Kim 	int tot_segs, node_segs, data_segs, free_segs, free_secs;
3890e1235983SChangman Lee 	int bg_node_segs, bg_data_segs;
389139a53e0cSJaegeuk Kim 	int tot_blks, data_blks, node_blks;
3892e1235983SChangman Lee 	int bg_data_blks, bg_node_blks;
389339a53e0cSJaegeuk Kim 	int curseg[NR_CURSEG_TYPE];
389439a53e0cSJaegeuk Kim 	int cursec[NR_CURSEG_TYPE];
389539a53e0cSJaegeuk Kim 	int curzone[NR_CURSEG_TYPE];
38960759e2c1SChao Yu 	unsigned int dirty_seg[NR_CURSEG_TYPE];
38970759e2c1SChao Yu 	unsigned int full_seg[NR_CURSEG_TYPE];
38980759e2c1SChao Yu 	unsigned int valid_blks[NR_CURSEG_TYPE];
389939a53e0cSJaegeuk Kim 
3900b63e7be5SChao Yu 	unsigned int meta_count[META_MAX];
390139a53e0cSJaegeuk Kim 	unsigned int segment_count[2];
390239a53e0cSJaegeuk Kim 	unsigned int block_count[2];
3903b9a2c252SChangman Lee 	unsigned int inplace_count;
39049edcdabfSChao Yu 	unsigned long long base_mem, cache_mem, page_mem;
390539a53e0cSJaegeuk Kim };
390639a53e0cSJaegeuk Kim 
3907963d4f7dSGu Zheng static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
3908963d4f7dSGu Zheng {
3909963d4f7dSGu Zheng 	return (struct f2fs_stat_info *)sbi->stat_info;
3910963d4f7dSGu Zheng }
3911963d4f7dSGu Zheng 
3912942e0be6SChangman Lee #define stat_inc_cp_count(si)		((si)->cp_count++)
391342190d2aSJaegeuk Kim #define stat_inc_bg_cp_count(si)	((si)->bg_cp_count++)
391439a53e0cSJaegeuk Kim #define stat_inc_call_count(si)		((si)->call_count++)
3915fc7100eaSHridya Valsaraju #define stat_inc_bggc_count(si)		((si)->bg_gc++)
3916274bd9baSChao Yu #define stat_io_skip_bggc_count(sbi)	((sbi)->io_skip_bggc++)
3917274bd9baSChao Yu #define stat_other_skip_bggc_count(sbi)	((sbi)->other_skip_bggc++)
391833fbd510SChao Yu #define stat_inc_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]++)
391933fbd510SChao Yu #define stat_dec_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]--)
39205b7ee374SChao Yu #define stat_inc_total_hit(sbi)		(atomic64_inc(&(sbi)->total_hit_ext))
39215b7ee374SChao Yu #define stat_inc_rbtree_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_rbtree))
39225b7ee374SChao Yu #define stat_inc_largest_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_largest))
39235b7ee374SChao Yu #define stat_inc_cached_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_cached))
3924d5e8f6c9SChao Yu #define stat_inc_inline_xattr(inode)					\
3925d5e8f6c9SChao Yu 	do {								\
3926d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3927d5e8f6c9SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_xattr));	\
3928d5e8f6c9SChao Yu 	} while (0)
3929d5e8f6c9SChao Yu #define stat_dec_inline_xattr(inode)					\
3930d5e8f6c9SChao Yu 	do {								\
3931d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3932d5e8f6c9SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_xattr));	\
3933d5e8f6c9SChao Yu 	} while (0)
39340dbdc2aeSJaegeuk Kim #define stat_inc_inline_inode(inode)					\
39350dbdc2aeSJaegeuk Kim 	do {								\
39360dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
393703e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_inode));	\
39380dbdc2aeSJaegeuk Kim 	} while (0)
39390dbdc2aeSJaegeuk Kim #define stat_dec_inline_inode(inode)					\
39400dbdc2aeSJaegeuk Kim 	do {								\
39410dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
394203e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_inode));	\
39430dbdc2aeSJaegeuk Kim 	} while (0)
39443289c061SJaegeuk Kim #define stat_inc_inline_dir(inode)					\
39453289c061SJaegeuk Kim 	do {								\
39463289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
394703e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_dir));	\
39483289c061SJaegeuk Kim 	} while (0)
39493289c061SJaegeuk Kim #define stat_dec_inline_dir(inode)					\
39503289c061SJaegeuk Kim 	do {								\
39513289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
395203e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_dir));	\
39533289c061SJaegeuk Kim 	} while (0)
39544c8ff709SChao Yu #define stat_inc_compr_inode(inode)					\
39554c8ff709SChao Yu 	do {								\
39564c8ff709SChao Yu 		if (f2fs_compressed_file(inode))			\
39574c8ff709SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->compr_inode));	\
39584c8ff709SChao Yu 	} while (0)
39594c8ff709SChao Yu #define stat_dec_compr_inode(inode)					\
39604c8ff709SChao Yu 	do {								\
39614c8ff709SChao Yu 		if (f2fs_compressed_file(inode))			\
39624c8ff709SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->compr_inode));	\
39634c8ff709SChao Yu 	} while (0)
39644c8ff709SChao Yu #define stat_add_compr_blocks(inode, blocks)				\
3965ae999bb9SDaeho Jeong 		(atomic64_add(blocks, &F2FS_I_SB(inode)->compr_blocks))
39664c8ff709SChao Yu #define stat_sub_compr_blocks(inode, blocks)				\
3967ae999bb9SDaeho Jeong 		(atomic64_sub(blocks, &F2FS_I_SB(inode)->compr_blocks))
3968b63e7be5SChao Yu #define stat_inc_meta_count(sbi, blkaddr)				\
3969b63e7be5SChao Yu 	do {								\
3970b63e7be5SChao Yu 		if (blkaddr < SIT_I(sbi)->sit_base_addr)		\
3971b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_CP]);	\
3972b63e7be5SChao Yu 		else if (blkaddr < NM_I(sbi)->nat_blkaddr)		\
3973b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_SIT]);	\
3974b63e7be5SChao Yu 		else if (blkaddr < SM_I(sbi)->ssa_blkaddr)		\
3975b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_NAT]);	\
3976b63e7be5SChao Yu 		else if (blkaddr < SM_I(sbi)->main_blkaddr)		\
3977b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_SSA]);	\
3978b63e7be5SChao Yu 	} while (0)
3979dcdfff65SJaegeuk Kim #define stat_inc_seg_type(sbi, curseg)					\
3980dcdfff65SJaegeuk Kim 		((sbi)->segment_count[(curseg)->alloc_type]++)
3981dcdfff65SJaegeuk Kim #define stat_inc_block_count(sbi, curseg)				\
3982dcdfff65SJaegeuk Kim 		((sbi)->block_count[(curseg)->alloc_type]++)
3983b9a2c252SChangman Lee #define stat_inc_inplace_blocks(sbi)					\
3984b9a2c252SChangman Lee 		(atomic_inc(&(sbi)->inplace_count))
398526a28a0cSJaegeuk Kim #define stat_update_max_atomic_write(inode)				\
398626a28a0cSJaegeuk Kim 	do {								\
39870e6d0164SSahitya Tummala 		int cur = F2FS_I_SB(inode)->atomic_files;	\
398826a28a0cSJaegeuk Kim 		int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt);	\
398926a28a0cSJaegeuk Kim 		if (cur > max)						\
399026a28a0cSJaegeuk Kim 			atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur);	\
399126a28a0cSJaegeuk Kim 	} while (0)
3992e1235983SChangman Lee #define stat_inc_seg_count(sbi, type, gc_type)				\
399339a53e0cSJaegeuk Kim 	do {								\
3994963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
399568afcf2dSTomohiro Kusumi 		si->tot_segs++;						\
399668afcf2dSTomohiro Kusumi 		if ((type) == SUM_TYPE_DATA) {				\
399739a53e0cSJaegeuk Kim 			si->data_segs++;				\
3998e1235983SChangman Lee 			si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0;	\
3999e1235983SChangman Lee 		} else {						\
400039a53e0cSJaegeuk Kim 			si->node_segs++;				\
4001e1235983SChangman Lee 			si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0;	\
4002e1235983SChangman Lee 		}							\
400339a53e0cSJaegeuk Kim 	} while (0)
400439a53e0cSJaegeuk Kim 
400539a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks)				\
400668afcf2dSTomohiro Kusumi 	((si)->tot_blks += (blks))
400739a53e0cSJaegeuk Kim 
4008e1235983SChangman Lee #define stat_inc_data_blk_count(sbi, blks, gc_type)			\
400939a53e0cSJaegeuk Kim 	do {								\
4010963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
401139a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
401239a53e0cSJaegeuk Kim 		si->data_blks += (blks);				\
401368afcf2dSTomohiro Kusumi 		si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
401439a53e0cSJaegeuk Kim 	} while (0)
401539a53e0cSJaegeuk Kim 
4016e1235983SChangman Lee #define stat_inc_node_blk_count(sbi, blks, gc_type)			\
401739a53e0cSJaegeuk Kim 	do {								\
4018963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
401939a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
402039a53e0cSJaegeuk Kim 		si->node_blks += (blks);				\
402168afcf2dSTomohiro Kusumi 		si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
402239a53e0cSJaegeuk Kim 	} while (0)
402339a53e0cSJaegeuk Kim 
4024cac5a3d8SDongOh Shin int f2fs_build_stats(struct f2fs_sb_info *sbi);
4025cac5a3d8SDongOh Shin void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
402621acc07dSGreg Kroah-Hartman void __init f2fs_create_root_stats(void);
40274589d25dSNamjae Jeon void f2fs_destroy_root_stats(void);
4028fc7100eaSHridya Valsaraju void f2fs_update_sit_info(struct f2fs_sb_info *sbi);
402939a53e0cSJaegeuk Kim #else
4030d66450e7SArnd Bergmann #define stat_inc_cp_count(si)				do { } while (0)
4031d66450e7SArnd Bergmann #define stat_inc_bg_cp_count(si)			do { } while (0)
4032d66450e7SArnd Bergmann #define stat_inc_call_count(si)				do { } while (0)
4033d66450e7SArnd Bergmann #define stat_inc_bggc_count(si)				do { } while (0)
4034274bd9baSChao Yu #define stat_io_skip_bggc_count(sbi)			do { } while (0)
4035274bd9baSChao Yu #define stat_other_skip_bggc_count(sbi)			do { } while (0)
4036d66450e7SArnd Bergmann #define stat_inc_dirty_inode(sbi, type)			do { } while (0)
4037d66450e7SArnd Bergmann #define stat_dec_dirty_inode(sbi, type)			do { } while (0)
4038fc7100eaSHridya Valsaraju #define stat_inc_total_hit(sbi)				do { } while (0)
4039fc7100eaSHridya Valsaraju #define stat_inc_rbtree_node_hit(sbi)			do { } while (0)
4040d66450e7SArnd Bergmann #define stat_inc_largest_node_hit(sbi)			do { } while (0)
4041d66450e7SArnd Bergmann #define stat_inc_cached_node_hit(sbi)			do { } while (0)
4042d66450e7SArnd Bergmann #define stat_inc_inline_xattr(inode)			do { } while (0)
4043d66450e7SArnd Bergmann #define stat_dec_inline_xattr(inode)			do { } while (0)
4044d66450e7SArnd Bergmann #define stat_inc_inline_inode(inode)			do { } while (0)
4045d66450e7SArnd Bergmann #define stat_dec_inline_inode(inode)			do { } while (0)
4046d66450e7SArnd Bergmann #define stat_inc_inline_dir(inode)			do { } while (0)
4047d66450e7SArnd Bergmann #define stat_dec_inline_dir(inode)			do { } while (0)
40484c8ff709SChao Yu #define stat_inc_compr_inode(inode)			do { } while (0)
40494c8ff709SChao Yu #define stat_dec_compr_inode(inode)			do { } while (0)
40504c8ff709SChao Yu #define stat_add_compr_blocks(inode, blocks)		do { } while (0)
40514c8ff709SChao Yu #define stat_sub_compr_blocks(inode, blocks)		do { } while (0)
4052d66450e7SArnd Bergmann #define stat_update_max_atomic_write(inode)		do { } while (0)
4053b63e7be5SChao Yu #define stat_inc_meta_count(sbi, blkaddr)		do { } while (0)
4054d66450e7SArnd Bergmann #define stat_inc_seg_type(sbi, curseg)			do { } while (0)
4055d66450e7SArnd Bergmann #define stat_inc_block_count(sbi, curseg)		do { } while (0)
4056d66450e7SArnd Bergmann #define stat_inc_inplace_blocks(sbi)			do { } while (0)
4057d66450e7SArnd Bergmann #define stat_inc_seg_count(sbi, type, gc_type)		do { } while (0)
4058d66450e7SArnd Bergmann #define stat_inc_tot_blk_count(si, blks)		do { } while (0)
4059d66450e7SArnd Bergmann #define stat_inc_data_blk_count(sbi, blks, gc_type)	do { } while (0)
4060d66450e7SArnd Bergmann #define stat_inc_node_blk_count(sbi, blks, gc_type)	do { } while (0)
406139a53e0cSJaegeuk Kim 
406239a53e0cSJaegeuk Kim static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
406339a53e0cSJaegeuk Kim static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
406421acc07dSGreg Kroah-Hartman static inline void __init f2fs_create_root_stats(void) { }
40654589d25dSNamjae Jeon static inline void f2fs_destroy_root_stats(void) { }
406648abe91aSYueHaibing static inline void f2fs_update_sit_info(struct f2fs_sb_info *sbi) {}
406739a53e0cSJaegeuk Kim #endif
406839a53e0cSJaegeuk Kim 
406939a53e0cSJaegeuk Kim extern const struct file_operations f2fs_dir_operations;
407039a53e0cSJaegeuk Kim extern const struct file_operations f2fs_file_operations;
407139a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_file_inode_operations;
407239a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_dblock_aops;
407339a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_node_aops;
407439a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_meta_aops;
407539a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_dir_inode_operations;
407639a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_symlink_inode_operations;
4077cbaf042aSJaegeuk Kim extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
407839a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_special_inode_operations;
40794d57b86dSChao Yu extern struct kmem_cache *f2fs_inode_entry_slab;
40801001b347SHuajun Li 
4081e18c65b2SHuajun Li /*
4082e18c65b2SHuajun Li  * inline.c
4083e18c65b2SHuajun Li  */
4084cac5a3d8SDongOh Shin bool f2fs_may_inline_data(struct inode *inode);
4085677a82b4SChao Yu bool f2fs_sanity_check_inline_data(struct inode *inode);
4086cac5a3d8SDongOh Shin bool f2fs_may_inline_dentry(struct inode *inode);
40874d57b86dSChao Yu void f2fs_do_read_inline_data(struct page *page, struct page *ipage);
40884d57b86dSChao Yu void f2fs_truncate_inline_inode(struct inode *inode,
40894d57b86dSChao Yu 						struct page *ipage, u64 from);
4090cac5a3d8SDongOh Shin int f2fs_read_inline_data(struct inode *inode, struct page *page);
4091cac5a3d8SDongOh Shin int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
4092cac5a3d8SDongOh Shin int f2fs_convert_inline_inode(struct inode *inode);
4093b06af2afSJaegeuk Kim int f2fs_try_convert_inline_dir(struct inode *dir, struct dentry *dentry);
4094cac5a3d8SDongOh Shin int f2fs_write_inline_data(struct inode *inode, struct page *page);
40959627a7b3SChao Yu int f2fs_recover_inline_data(struct inode *inode, struct page *npage);
40964d57b86dSChao Yu struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
409743c780baSEric Biggers 					const struct f2fs_filename *fname,
409843c780baSEric Biggers 					struct page **res_page);
40994d57b86dSChao Yu int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent,
4100cac5a3d8SDongOh Shin 			struct page *ipage);
410143c780baSEric Biggers int f2fs_add_inline_entry(struct inode *dir, const struct f2fs_filename *fname,
4102cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
41034d57b86dSChao Yu void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry,
41044d57b86dSChao Yu 				struct page *page, struct inode *dir,
41054d57b86dSChao Yu 				struct inode *inode);
4106cac5a3d8SDongOh Shin bool f2fs_empty_inline_dir(struct inode *dir);
4107cac5a3d8SDongOh Shin int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
4108cac5a3d8SDongOh Shin 			struct fscrypt_str *fstr);
4109cac5a3d8SDongOh Shin int f2fs_inline_data_fiemap(struct inode *inode,
4110cac5a3d8SDongOh Shin 			struct fiemap_extent_info *fieinfo,
4111cac5a3d8SDongOh Shin 			__u64 start, __u64 len);
4112cde4de12SJaegeuk Kim 
4113cde4de12SJaegeuk Kim /*
41142658e50dSJaegeuk Kim  * shrinker.c
41152658e50dSJaegeuk Kim  */
4116cac5a3d8SDongOh Shin unsigned long f2fs_shrink_count(struct shrinker *shrink,
4117cac5a3d8SDongOh Shin 			struct shrink_control *sc);
4118cac5a3d8SDongOh Shin unsigned long f2fs_shrink_scan(struct shrinker *shrink,
4119cac5a3d8SDongOh Shin 			struct shrink_control *sc);
4120cac5a3d8SDongOh Shin void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
4121cac5a3d8SDongOh Shin void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
41222658e50dSJaegeuk Kim 
41232658e50dSJaegeuk Kim /*
4124a28ef1f5SChao Yu  * extent_cache.c
4125a28ef1f5SChao Yu  */
41264dada3fdSChao Yu struct rb_entry *f2fs_lookup_rb_tree(struct rb_root_cached *root,
4127004b6862SChao Yu 				struct rb_entry *cached_re, unsigned int ofs);
41282e9b2bb2SChao Yu struct rb_node **f2fs_lookup_rb_tree_ext(struct f2fs_sb_info *sbi,
41292e9b2bb2SChao Yu 				struct rb_root_cached *root,
41302e9b2bb2SChao Yu 				struct rb_node **parent,
41312e9b2bb2SChao Yu 				unsigned long long key, bool *left_most);
41324d57b86dSChao Yu struct rb_node **f2fs_lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
41334dada3fdSChao Yu 				struct rb_root_cached *root,
41344dada3fdSChao Yu 				struct rb_node **parent,
41354dada3fdSChao Yu 				unsigned int ofs, bool *leftmost);
41364dada3fdSChao Yu struct rb_entry *f2fs_lookup_rb_tree_ret(struct rb_root_cached *root,
4137004b6862SChao Yu 		struct rb_entry *cached_re, unsigned int ofs,
4138004b6862SChao Yu 		struct rb_entry **prev_entry, struct rb_entry **next_entry,
4139004b6862SChao Yu 		struct rb_node ***insert_p, struct rb_node **insert_parent,
41404dada3fdSChao Yu 		bool force, bool *leftmost);
41414d57b86dSChao Yu bool f2fs_check_rb_tree_consistence(struct f2fs_sb_info *sbi,
41422e9b2bb2SChao Yu 				struct rb_root_cached *root, bool check_key);
4143cac5a3d8SDongOh Shin unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
4144a6d601f3SChao Yu void f2fs_init_extent_tree(struct inode *inode, struct page *ipage);
4145cac5a3d8SDongOh Shin void f2fs_drop_extent_tree(struct inode *inode);
4146cac5a3d8SDongOh Shin unsigned int f2fs_destroy_extent_node(struct inode *inode);
4147cac5a3d8SDongOh Shin void f2fs_destroy_extent_tree(struct inode *inode);
4148cac5a3d8SDongOh Shin bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
4149cac5a3d8SDongOh Shin 			struct extent_info *ei);
4150cac5a3d8SDongOh Shin void f2fs_update_extent_cache(struct dnode_of_data *dn);
415119b2c30dSChao Yu void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
4152cac5a3d8SDongOh Shin 			pgoff_t fofs, block_t blkaddr, unsigned int len);
41534d57b86dSChao Yu void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi);
41544d57b86dSChao Yu int __init f2fs_create_extent_cache(void);
41554d57b86dSChao Yu void f2fs_destroy_extent_cache(void);
4156a28ef1f5SChao Yu 
4157a28ef1f5SChao Yu /*
41588ceffcb2SChao Yu  * sysfs.c
41598ceffcb2SChao Yu  */
41600f6b56ecSDaeho Jeong #define MIN_RA_MUL	2
41610f6b56ecSDaeho Jeong #define MAX_RA_MUL	256
41620f6b56ecSDaeho Jeong 
4163dc6b2055SJaegeuk Kim int __init f2fs_init_sysfs(void);
4164dc6b2055SJaegeuk Kim void f2fs_exit_sysfs(void);
4165dc6b2055SJaegeuk Kim int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
4166dc6b2055SJaegeuk Kim void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
41678ceffcb2SChao Yu 
416895ae251fSEric Biggers /* verity.c */
416995ae251fSEric Biggers extern const struct fsverity_operations f2fs_verityops;
417095ae251fSEric Biggers 
41718ceffcb2SChao Yu /*
4172cde4de12SJaegeuk Kim  * crypto support
4173cde4de12SJaegeuk Kim  */
41741958593eSJaegeuk Kim static inline bool f2fs_encrypted_file(struct inode *inode)
41751958593eSJaegeuk Kim {
417662230e0dSChandan Rajendra 	return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
41771958593eSJaegeuk Kim }
41781958593eSJaegeuk Kim 
4179cde4de12SJaegeuk Kim static inline void f2fs_set_encrypted_inode(struct inode *inode)
4180cde4de12SJaegeuk Kim {
4181643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
4182cde4de12SJaegeuk Kim 	file_set_encrypt(inode);
41839149a5ebSChao Yu 	f2fs_set_inode_flags(inode);
4184cde4de12SJaegeuk Kim #endif
4185cde4de12SJaegeuk Kim }
4186cde4de12SJaegeuk Kim 
41876dbb1796SEric Biggers /*
41886dbb1796SEric Biggers  * Returns true if the reads of the inode's data need to undergo some
41896dbb1796SEric Biggers  * postprocessing step, like decryption or authenticity verification.
41906dbb1796SEric Biggers  */
41916dbb1796SEric Biggers static inline bool f2fs_post_read_required(struct inode *inode)
4192cde4de12SJaegeuk Kim {
41934c8ff709SChao Yu 	return f2fs_encrypted_file(inode) || fsverity_active(inode) ||
41944c8ff709SChao Yu 		f2fs_compressed_file(inode);
41954c8ff709SChao Yu }
41964c8ff709SChao Yu 
41974c8ff709SChao Yu /*
41984c8ff709SChao Yu  * compress.c
41994c8ff709SChao Yu  */
42004c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
42014c8ff709SChao Yu bool f2fs_is_compressed_page(struct page *page);
42024c8ff709SChao Yu struct page *f2fs_compress_control_page(struct page *page);
42034c8ff709SChao Yu int f2fs_prepare_compress_overwrite(struct inode *inode,
42044c8ff709SChao Yu 			struct page **pagep, pgoff_t index, void **fsdata);
42054c8ff709SChao Yu bool f2fs_compress_write_end(struct inode *inode, void *fsdata,
42064c8ff709SChao Yu 					pgoff_t index, unsigned copied);
42073265d3dbSChao Yu int f2fs_truncate_partial_cluster(struct inode *inode, u64 from, bool lock);
42084c8ff709SChao Yu void f2fs_compress_write_end_io(struct bio *bio, struct page *page);
42094c8ff709SChao Yu bool f2fs_is_compress_backend_ready(struct inode *inode);
42105e6bbde9SChao Yu int f2fs_init_compress_mempool(void);
42115e6bbde9SChao Yu void f2fs_destroy_compress_mempool(void);
4212bff139b4SDaeho Jeong void f2fs_decompress_cluster(struct decompress_io_ctx *dic, bool in_task);
42136ce19affSChao Yu void f2fs_end_read_compressed_page(struct page *page, bool failed,
4214bff139b4SDaeho Jeong 				block_t blkaddr, bool in_task);
42154c8ff709SChao Yu bool f2fs_cluster_is_empty(struct compress_ctx *cc);
42164c8ff709SChao Yu bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index);
421701fc4b9aSFengnan Chang bool f2fs_all_cluster_page_ready(struct compress_ctx *cc, struct page **pages,
42184f8219f8SFengnan Chang 				int index, int nr_pages, bool uptodate);
4219bbe1da7eSChao Yu bool f2fs_sanity_check_cluster(struct dnode_of_data *dn);
42204c8ff709SChao Yu void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct page *page);
42214c8ff709SChao Yu int f2fs_write_multi_pages(struct compress_ctx *cc,
42224c8ff709SChao Yu 						int *submitted,
42234c8ff709SChao Yu 						struct writeback_control *wbc,
42244c8ff709SChao Yu 						enum iostat_type io_type);
42254c8ff709SChao Yu int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index);
422694afd6d6SChao Yu void f2fs_update_extent_tree_range_compressed(struct inode *inode,
422794afd6d6SChao Yu 				pgoff_t fofs, block_t blkaddr, unsigned int llen,
422894afd6d6SChao Yu 				unsigned int c_len);
42294c8ff709SChao Yu int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
42304c8ff709SChao Yu 				unsigned nr_pages, sector_t *last_block_in_bio,
42310683728aSChao Yu 				bool is_readahead, bool for_write);
42324c8ff709SChao Yu struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc);
4233bff139b4SDaeho Jeong void f2fs_decompress_end_io(struct decompress_io_ctx *dic, bool failed,
4234bff139b4SDaeho Jeong 				bool in_task);
4235bff139b4SDaeho Jeong void f2fs_put_page_dic(struct page *page, bool in_task);
423694afd6d6SChao Yu unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn);
42374c8ff709SChao Yu int f2fs_init_compress_ctx(struct compress_ctx *cc);
42388bfbfb0dSChao Yu void f2fs_destroy_compress_ctx(struct compress_ctx *cc, bool reuse);
42394c8ff709SChao Yu void f2fs_init_compress_info(struct f2fs_sb_info *sbi);
42406ce19affSChao Yu int f2fs_init_compress_inode(struct f2fs_sb_info *sbi);
42416ce19affSChao Yu void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi);
424231083031SChao Yu int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi);
424331083031SChao Yu void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi);
4244c68d6c88SChao Yu int __init f2fs_init_compress_cache(void);
4245c68d6c88SChao Yu void f2fs_destroy_compress_cache(void);
42466ce19affSChao Yu struct address_space *COMPRESS_MAPPING(struct f2fs_sb_info *sbi);
42476ce19affSChao Yu void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi, block_t blkaddr);
42486ce19affSChao Yu void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
42496ce19affSChao Yu 						nid_t ino, block_t blkaddr);
42506ce19affSChao Yu bool f2fs_load_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
42516ce19affSChao Yu 								block_t blkaddr);
42526ce19affSChao Yu void f2fs_invalidate_compress_pages(struct f2fs_sb_info *sbi, nid_t ino);
42535ac443e2SDaeho Jeong #define inc_compr_inode_stat(inode)					\
42545ac443e2SDaeho Jeong 	do {								\
42555ac443e2SDaeho Jeong 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);		\
42565ac443e2SDaeho Jeong 		sbi->compr_new_inode++;					\
42575ac443e2SDaeho Jeong 	} while (0)
42585ac443e2SDaeho Jeong #define add_compr_block_stat(inode, blocks)				\
42595ac443e2SDaeho Jeong 	do {								\
42605ac443e2SDaeho Jeong 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);		\
42615ac443e2SDaeho Jeong 		int diff = F2FS_I(inode)->i_cluster_size - blocks;	\
42625ac443e2SDaeho Jeong 		sbi->compr_written_block += blocks;			\
42635ac443e2SDaeho Jeong 		sbi->compr_saved_block += diff;				\
42645ac443e2SDaeho Jeong 	} while (0)
42654c8ff709SChao Yu #else
42664c8ff709SChao Yu static inline bool f2fs_is_compressed_page(struct page *page) { return false; }
42674c8ff709SChao Yu static inline bool f2fs_is_compress_backend_ready(struct inode *inode)
42684c8ff709SChao Yu {
42694c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
42704c8ff709SChao Yu 		return true;
42714c8ff709SChao Yu 	/* not support compression */
42724c8ff709SChao Yu 	return false;
42734c8ff709SChao Yu }
42744c8ff709SChao Yu static inline struct page *f2fs_compress_control_page(struct page *page)
42754c8ff709SChao Yu {
42764c8ff709SChao Yu 	WARN_ON_ONCE(1);
42774c8ff709SChao Yu 	return ERR_PTR(-EINVAL);
42784c8ff709SChao Yu }
42795e6bbde9SChao Yu static inline int f2fs_init_compress_mempool(void) { return 0; }
42805e6bbde9SChao Yu static inline void f2fs_destroy_compress_mempool(void) { }
4281bff139b4SDaeho Jeong static inline void f2fs_decompress_cluster(struct decompress_io_ctx *dic,
4282bff139b4SDaeho Jeong 				bool in_task) { }
42836ce19affSChao Yu static inline void f2fs_end_read_compressed_page(struct page *page,
4284bff139b4SDaeho Jeong 				bool failed, block_t blkaddr, bool in_task)
42857f59b277SEric Biggers {
42867f59b277SEric Biggers 	WARN_ON_ONCE(1);
42877f59b277SEric Biggers }
4288bff139b4SDaeho Jeong static inline void f2fs_put_page_dic(struct page *page, bool in_task)
42897f59b277SEric Biggers {
42907f59b277SEric Biggers 	WARN_ON_ONCE(1);
42917f59b277SEric Biggers }
429294afd6d6SChao Yu static inline unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn) { return 0; }
4293bbe1da7eSChao Yu static inline bool f2fs_sanity_check_cluster(struct dnode_of_data *dn) { return false; }
42946ce19affSChao Yu static inline int f2fs_init_compress_inode(struct f2fs_sb_info *sbi) { return 0; }
42956ce19affSChao Yu static inline void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi) { }
429631083031SChao Yu static inline int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi) { return 0; }
429731083031SChao Yu static inline void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi) { }
4298c68d6c88SChao Yu static inline int __init f2fs_init_compress_cache(void) { return 0; }
4299c68d6c88SChao Yu static inline void f2fs_destroy_compress_cache(void) { }
43006ce19affSChao Yu static inline void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi,
43016ce19affSChao Yu 				block_t blkaddr) { }
43026ce19affSChao Yu static inline void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi,
43036ce19affSChao Yu 				struct page *page, nid_t ino, block_t blkaddr) { }
43046ce19affSChao Yu static inline bool f2fs_load_compressed_page(struct f2fs_sb_info *sbi,
43056ce19affSChao Yu 				struct page *page, block_t blkaddr) { return false; }
43066ce19affSChao Yu static inline void f2fs_invalidate_compress_pages(struct f2fs_sb_info *sbi,
43076ce19affSChao Yu 							nid_t ino) { }
43085ac443e2SDaeho Jeong #define inc_compr_inode_stat(inode)		do { } while (0)
430994afd6d6SChao Yu static inline void f2fs_update_extent_tree_range_compressed(struct inode *inode,
431094afd6d6SChao Yu 				pgoff_t fofs, block_t blkaddr, unsigned int llen,
431194afd6d6SChao Yu 				unsigned int c_len) { }
43124c8ff709SChao Yu #endif
43134c8ff709SChao Yu 
4314912f0d65SJaegeuk Kim static inline int set_compress_context(struct inode *inode)
43154c8ff709SChao Yu {
4316912f0d65SJaegeuk Kim #ifdef CONFIG_F2FS_FS_COMPRESSION
43174c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
43184c8ff709SChao Yu 
43194c8ff709SChao Yu 	F2FS_I(inode)->i_compress_algorithm =
43204c8ff709SChao Yu 			F2FS_OPTION(sbi).compress_algorithm;
43214c8ff709SChao Yu 	F2FS_I(inode)->i_log_cluster_size =
43224c8ff709SChao Yu 			F2FS_OPTION(sbi).compress_log_size;
4323b28f047bSChao Yu 	F2FS_I(inode)->i_compress_flag =
4324b28f047bSChao Yu 			F2FS_OPTION(sbi).compress_chksum ?
4325b28f047bSChao Yu 				1 << COMPRESS_CHKSUM : 0;
43264c8ff709SChao Yu 	F2FS_I(inode)->i_cluster_size =
43274c8ff709SChao Yu 			1 << F2FS_I(inode)->i_log_cluster_size;
432801f6afd0SChao Yu 	if ((F2FS_I(inode)->i_compress_algorithm == COMPRESS_LZ4 ||
432901f6afd0SChao Yu 		F2FS_I(inode)->i_compress_algorithm == COMPRESS_ZSTD) &&
43303fde13f8SChao Yu 			F2FS_OPTION(sbi).compress_level)
43313fde13f8SChao Yu 		F2FS_I(inode)->i_compress_flag |=
43323fde13f8SChao Yu 				F2FS_OPTION(sbi).compress_level <<
43333fde13f8SChao Yu 				COMPRESS_LEVEL_OFFSET;
43344c8ff709SChao Yu 	F2FS_I(inode)->i_flags |= F2FS_COMPR_FL;
43354c8ff709SChao Yu 	set_inode_flag(inode, FI_COMPRESSED_FILE);
43364c8ff709SChao Yu 	stat_inc_compr_inode(inode);
43375ac443e2SDaeho Jeong 	inc_compr_inode_stat(inode);
4338530e0704SChao Yu 	f2fs_mark_inode_dirty_sync(inode, true);
4339912f0d65SJaegeuk Kim 	return 0;
4340912f0d65SJaegeuk Kim #else
4341912f0d65SJaegeuk Kim 	return -EOPNOTSUPP;
4342912f0d65SJaegeuk Kim #endif
43434c8ff709SChao Yu }
43444c8ff709SChao Yu 
434578134d03SDaeho Jeong static inline bool f2fs_disable_compressed_file(struct inode *inode)
43464c8ff709SChao Yu {
43474c8ff709SChao Yu 	struct f2fs_inode_info *fi = F2FS_I(inode);
43484c8ff709SChao Yu 
43494c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
435078134d03SDaeho Jeong 		return true;
435102d58cd2SHyeong-Jun Kim 	if (S_ISREG(inode->i_mode) && F2FS_HAS_BLOCKS(inode))
435278134d03SDaeho Jeong 		return false;
43534c8ff709SChao Yu 
43544c8ff709SChao Yu 	fi->i_flags &= ~F2FS_COMPR_FL;
43554c8ff709SChao Yu 	stat_dec_compr_inode(inode);
435696f5b4faSChao Yu 	clear_inode_flag(inode, FI_COMPRESSED_FILE);
4357530e0704SChao Yu 	f2fs_mark_inode_dirty_sync(inode, true);
435878134d03SDaeho Jeong 	return true;
4359cde4de12SJaegeuk Kim }
4360cde4de12SJaegeuk Kim 
4361ccd31cb2SSheng Yong #define F2FS_FEATURE_FUNCS(name, flagname) \
43627beb01f7SChao Yu static inline int f2fs_sb_has_##name(struct f2fs_sb_info *sbi) \
4363ccd31cb2SSheng Yong { \
43647beb01f7SChao Yu 	return F2FS_HAS_FEATURE(sbi, F2FS_FEATURE_##flagname); \
4365cde4de12SJaegeuk Kim }
4366f424f664SJaegeuk Kim 
4367ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(encrypt, ENCRYPT);
4368ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(blkzoned, BLKZONED);
4369ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(extra_attr, EXTRA_ATTR);
4370ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(project_quota, PRJQUOTA);
4371ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_chksum, INODE_CHKSUM);
4372ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
4373ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
4374ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
4375b7c409deSSheng Yong F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
437695ae251fSEric Biggers F2FS_FEATURE_FUNCS(verity, VERITY);
4377d440c52dSJunling Zheng F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
43785aba5430SDaniel Rosenberg F2FS_FEATURE_FUNCS(casefold, CASEFOLD);
43794c8ff709SChao Yu F2FS_FEATURE_FUNCS(compression, COMPRESSION);
4380a7d9fe3cSJaegeuk Kim F2FS_FEATURE_FUNCS(readonly, RO);
43811c1d35dfSChao Yu 
43824215d054SDaeho Jeong static inline bool f2fs_may_extent_tree(struct inode *inode)
43834215d054SDaeho Jeong {
43844215d054SDaeho Jeong 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
43854215d054SDaeho Jeong 
43864215d054SDaeho Jeong 	if (!test_opt(sbi, EXTENT_CACHE) ||
43874215d054SDaeho Jeong 			is_inode_flag_set(inode, FI_NO_EXTENT) ||
43884215d054SDaeho Jeong 			(is_inode_flag_set(inode, FI_COMPRESSED_FILE) &&
43894215d054SDaeho Jeong 			 !f2fs_sb_has_readonly(sbi)))
43904215d054SDaeho Jeong 		return false;
43914215d054SDaeho Jeong 
43924215d054SDaeho Jeong 	/*
43934215d054SDaeho Jeong 	 * for recovered files during mount do not create extents
43944215d054SDaeho Jeong 	 * if shrinker is not registered.
43954215d054SDaeho Jeong 	 */
43964215d054SDaeho Jeong 	if (list_empty(&sbi->s_list))
43974215d054SDaeho Jeong 		return false;
43984215d054SDaeho Jeong 
43994215d054SDaeho Jeong 	return S_ISREG(inode->i_mode);
44004215d054SDaeho Jeong }
44014215d054SDaeho Jeong 
4402178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
440395175dafSDamien Le Moal static inline bool f2fs_blkz_is_seq(struct f2fs_sb_info *sbi, int devi,
440495175dafSDamien Le Moal 				    block_t blkaddr)
4405178053e2SDamien Le Moal {
4406178053e2SDamien Le Moal 	unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
4407178053e2SDamien Le Moal 
440895175dafSDamien Le Moal 	return test_bit(zno, FDEV(devi).blkz_seq);
4409178053e2SDamien Le Moal }
4410178053e2SDamien Le Moal #endif
4411178053e2SDamien Le Moal 
44127d20c8abSChao Yu static inline bool f2fs_hw_should_discard(struct f2fs_sb_info *sbi)
441396ba2decSDamien Le Moal {
44147beb01f7SChao Yu 	return f2fs_sb_has_blkzoned(sbi);
44157d20c8abSChao Yu }
441696ba2decSDamien Le Moal 
44177f3d7719SDamien Le Moal static inline bool f2fs_bdev_support_discard(struct block_device *bdev)
44187f3d7719SDamien Le Moal {
441970200574SChristoph Hellwig 	return bdev_max_discard_sectors(bdev) || bdev_is_zoned(bdev);
44207f3d7719SDamien Le Moal }
44217f3d7719SDamien Le Moal 
44227d20c8abSChao Yu static inline bool f2fs_hw_support_discard(struct f2fs_sb_info *sbi)
44237d20c8abSChao Yu {
44247f3d7719SDamien Le Moal 	int i;
44257f3d7719SDamien Le Moal 
44267f3d7719SDamien Le Moal 	if (!f2fs_is_multi_device(sbi))
44277f3d7719SDamien Le Moal 		return f2fs_bdev_support_discard(sbi->sb->s_bdev);
44287f3d7719SDamien Le Moal 
44297f3d7719SDamien Le Moal 	for (i = 0; i < sbi->s_ndevs; i++)
44307f3d7719SDamien Le Moal 		if (f2fs_bdev_support_discard(FDEV(i).bdev))
44317f3d7719SDamien Le Moal 			return true;
44327f3d7719SDamien Le Moal 	return false;
44337d20c8abSChao Yu }
44347d20c8abSChao Yu 
44357d20c8abSChao Yu static inline bool f2fs_realtime_discard_enable(struct f2fs_sb_info *sbi)
44367d20c8abSChao Yu {
44377d20c8abSChao Yu 	return (test_opt(sbi, DISCARD) && f2fs_hw_support_discard(sbi)) ||
44387d20c8abSChao Yu 					f2fs_hw_should_discard(sbi);
443952763a4bSJaegeuk Kim }
444052763a4bSJaegeuk Kim 
4441f824deb5SChao Yu static inline bool f2fs_hw_is_readonly(struct f2fs_sb_info *sbi)
4442f824deb5SChao Yu {
4443f824deb5SChao Yu 	int i;
4444f824deb5SChao Yu 
4445f824deb5SChao Yu 	if (!f2fs_is_multi_device(sbi))
4446f824deb5SChao Yu 		return bdev_read_only(sbi->sb->s_bdev);
4447f824deb5SChao Yu 
4448f824deb5SChao Yu 	for (i = 0; i < sbi->s_ndevs; i++)
4449f824deb5SChao Yu 		if (bdev_read_only(FDEV(i).bdev))
4450f824deb5SChao Yu 			return true;
4451f824deb5SChao Yu 	return false;
4452f824deb5SChao Yu }
4453f824deb5SChao Yu 
4454b0332a0fSChao Yu static inline bool f2fs_lfs_mode(struct f2fs_sb_info *sbi)
445552763a4bSJaegeuk Kim {
4456b0332a0fSChao Yu 	return F2FS_OPTION(sbi).fs_mode == FS_MODE_LFS;
445752763a4bSJaegeuk Kim }
445852763a4bSJaegeuk Kim 
44597a8fc586SDaeho Jeong static inline bool f2fs_low_mem_mode(struct f2fs_sb_info *sbi)
44607a8fc586SDaeho Jeong {
44617a8fc586SDaeho Jeong 	return F2FS_OPTION(sbi).memory_mode == MEMORY_MODE_LOW;
44627a8fc586SDaeho Jeong }
44637a8fc586SDaeho Jeong 
44644c8ff709SChao Yu static inline bool f2fs_may_compress(struct inode *inode)
44654c8ff709SChao Yu {
44664c8ff709SChao Yu 	if (IS_SWAPFILE(inode) || f2fs_is_pinned_file(inode) ||
44677165841dSChao Yu 		f2fs_is_atomic_file(inode) || f2fs_has_inline_data(inode))
44684c8ff709SChao Yu 		return false;
44694c8ff709SChao Yu 	return S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode);
44704c8ff709SChao Yu }
44714c8ff709SChao Yu 
44724c8ff709SChao Yu static inline void f2fs_i_compr_blocks_update(struct inode *inode,
44734c8ff709SChao Yu 						u64 blocks, bool add)
44744c8ff709SChao Yu {
4475c2759ebaSDaeho Jeong 	struct f2fs_inode_info *fi = F2FS_I(inode);
4476054cb289SYufen Yu 	int diff = fi->i_cluster_size - blocks;
44774c8ff709SChao Yu 
4478ef8d563fSChao Yu 	/* don't update i_compr_blocks if saved blocks were released */
4479c2759ebaSDaeho Jeong 	if (!add && !atomic_read(&fi->i_compr_blocks))
4480ef8d563fSChao Yu 		return;
4481ef8d563fSChao Yu 
44824c8ff709SChao Yu 	if (add) {
4483c2759ebaSDaeho Jeong 		atomic_add(diff, &fi->i_compr_blocks);
44844c8ff709SChao Yu 		stat_add_compr_blocks(inode, diff);
44854c8ff709SChao Yu 	} else {
4486c2759ebaSDaeho Jeong 		atomic_sub(diff, &fi->i_compr_blocks);
44874c8ff709SChao Yu 		stat_sub_compr_blocks(inode, diff);
44884c8ff709SChao Yu 	}
44894c8ff709SChao Yu 	f2fs_mark_inode_dirty_sync(inode, true);
44904c8ff709SChao Yu }
44914c8ff709SChao Yu 
4492f847c699SChao Yu static inline int block_unaligned_IO(struct inode *inode,
4493f847c699SChao Yu 				struct kiocb *iocb, struct iov_iter *iter)
4494b91050a8SHyunchul Lee {
4495f847c699SChao Yu 	unsigned int i_blkbits = READ_ONCE(inode->i_blkbits);
4496f847c699SChao Yu 	unsigned int blocksize_mask = (1 << i_blkbits) - 1;
4497f847c699SChao Yu 	loff_t offset = iocb->ki_pos;
4498f847c699SChao Yu 	unsigned long align = offset | iov_iter_alignment(iter);
4499f847c699SChao Yu 
4500f847c699SChao Yu 	return align & blocksize_mask;
4501f847c699SChao Yu }
4502f847c699SChao Yu 
450371f2c820SChao Yu static inline bool f2fs_allow_multi_device_dio(struct f2fs_sb_info *sbi,
450471f2c820SChao Yu 								int flag)
450571f2c820SChao Yu {
450671f2c820SChao Yu 	if (!f2fs_is_multi_device(sbi))
450771f2c820SChao Yu 		return false;
450871f2c820SChao Yu 	if (flag != F2FS_GET_BLOCK_DIO)
450971f2c820SChao Yu 		return false;
451071f2c820SChao Yu 	return sbi->aligned_blksize;
451171f2c820SChao Yu }
451271f2c820SChao Yu 
4513f847c699SChao Yu static inline bool f2fs_force_buffered_io(struct inode *inode,
4514f847c699SChao Yu 				struct kiocb *iocb, struct iov_iter *iter)
4515f847c699SChao Yu {
4516f847c699SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4517f847c699SChao Yu 	int rw = iov_iter_rw(iter);
4518f847c699SChao Yu 
45198a2c77bcSEric Biggers 	if (!fscrypt_dio_supported(iocb, iter))
45208a2c77bcSEric Biggers 		return true;
45218a2c77bcSEric Biggers 	if (fsverity_active(inode))
45228a2c77bcSEric Biggers 		return true;
45238a2c77bcSEric Biggers 	if (f2fs_compressed_file(inode))
4524f847c699SChao Yu 		return true;
452571f2c820SChao Yu 
452671f2c820SChao Yu 	/* disallow direct IO if any of devices has unaligned blksize */
452771f2c820SChao Yu 	if (f2fs_is_multi_device(sbi) && !sbi->aligned_blksize)
4528f847c699SChao Yu 		return true;
4529dbf8e63fSEunhee Rho 
4530b0332a0fSChao Yu 	if (f2fs_lfs_mode(sbi) && (rw == WRITE)) {
45319720ee80SChao Yu 		if (block_unaligned_IO(inode, iocb, iter))
4532f847c699SChao Yu 			return true;
45339720ee80SChao Yu 		if (F2FS_IO_ALIGNED(sbi))
45349720ee80SChao Yu 			return true;
45359720ee80SChao Yu 	}
4536ebc29b62Shuangjianan@oppo.com 	if (is_sbi_flag_set(F2FS_I_SB(inode), SBI_CP_DISABLED))
45374354994fSDaniel Rosenberg 		return true;
45384354994fSDaniel Rosenberg 
4539f847c699SChao Yu 	return false;
4540b91050a8SHyunchul Lee }
4541b91050a8SHyunchul Lee 
45427f59b277SEric Biggers static inline bool f2fs_need_verity(const struct inode *inode, pgoff_t idx)
45437f59b277SEric Biggers {
45447f59b277SEric Biggers 	return fsverity_active(inode) &&
45457f59b277SEric Biggers 	       idx < DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
45467f59b277SEric Biggers }
45477f59b277SEric Biggers 
454883a3bfdbSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
4549d494500aSChao Yu extern void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate,
4550d494500aSChao Yu 							unsigned int type);
455183a3bfdbSJaegeuk Kim #else
4552d494500aSChao Yu #define f2fs_build_fault_attr(sbi, rate, type)		do { } while (0)
455383a3bfdbSJaegeuk Kim #endif
455483a3bfdbSJaegeuk Kim 
4555af033b2aSChao Yu static inline bool is_journalled_quota(struct f2fs_sb_info *sbi)
4556af033b2aSChao Yu {
4557af033b2aSChao Yu #ifdef CONFIG_QUOTA
45587beb01f7SChao Yu 	if (f2fs_sb_has_quota_ino(sbi))
4559af033b2aSChao Yu 		return true;
4560af033b2aSChao Yu 	if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
4561af033b2aSChao Yu 		F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
4562af033b2aSChao Yu 		F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
4563af033b2aSChao Yu 		return true;
4564af033b2aSChao Yu #endif
4565af033b2aSChao Yu 	return false;
4566af033b2aSChao Yu }
45670af725fcSJaegeuk Kim 
45684f993264SChao Yu static inline bool f2fs_block_unit_discard(struct f2fs_sb_info *sbi)
45694f993264SChao Yu {
45704f993264SChao Yu 	return F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_BLOCK;
45714f993264SChao Yu }
45724f993264SChao Yu 
4573a64239d0SNeilBrown static inline void f2fs_io_schedule_timeout(long timeout)
4574a64239d0SNeilBrown {
4575a64239d0SNeilBrown 	set_current_state(TASK_UNINTERRUPTIBLE);
4576a64239d0SNeilBrown 	io_schedule_timeout(timeout);
4577a64239d0SNeilBrown }
4578a64239d0SNeilBrown 
4579a7b8618aSJaegeuk Kim static inline void f2fs_handle_page_eio(struct f2fs_sb_info *sbi, pgoff_t ofs,
4580a7b8618aSJaegeuk Kim 					enum page_type type)
4581a7b8618aSJaegeuk Kim {
4582a7b8618aSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
4583a7b8618aSJaegeuk Kim 		return;
4584a7b8618aSJaegeuk Kim 
4585a7b8618aSJaegeuk Kim 	if (ofs == sbi->page_eio_ofs[type]) {
4586a7b8618aSJaegeuk Kim 		if (sbi->page_eio_cnt[type]++ == MAX_RETRY_PAGE_EIO)
4587a7b8618aSJaegeuk Kim 			set_ckpt_flags(sbi, CP_ERROR_FLAG);
4588a7b8618aSJaegeuk Kim 	} else {
4589a7b8618aSJaegeuk Kim 		sbi->page_eio_ofs[type] = ofs;
4590a7b8618aSJaegeuk Kim 		sbi->page_eio_cnt[type] = 0;
4591a7b8618aSJaegeuk Kim 	}
4592a7b8618aSJaegeuk Kim }
4593a7b8618aSJaegeuk Kim 
459410f966bbSChao Yu #define EFSBADCRC	EBADMSG		/* Bad CRC detected */
459510f966bbSChao Yu #define EFSCORRUPTED	EUCLEAN		/* Filesystem is corrupted */
459610f966bbSChao Yu 
4597e1074d4bSChao Yu #endif /* _LINUX_F2FS_H */
4598