xref: /openbmc/linux/fs/f2fs/f2fs.h (revision 50c63009f6ab37eb78d8b4f9bc606a12b2b46505)
1d29fbcdbSNishad Kamdar /* SPDX-License-Identifier: GPL-2.0 */
20a8165d7SJaegeuk Kim /*
339a53e0cSJaegeuk Kim  * fs/f2fs/f2fs.h
439a53e0cSJaegeuk Kim  *
539a53e0cSJaegeuk Kim  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
639a53e0cSJaegeuk Kim  *             http://www.samsung.com/
739a53e0cSJaegeuk Kim  */
839a53e0cSJaegeuk Kim #ifndef _LINUX_F2FS_H
939a53e0cSJaegeuk Kim #define _LINUX_F2FS_H
1039a53e0cSJaegeuk Kim 
11f847c699SChao Yu #include <linux/uio.h>
1239a53e0cSJaegeuk Kim #include <linux/types.h>
1339a53e0cSJaegeuk Kim #include <linux/page-flags.h>
1439a53e0cSJaegeuk Kim #include <linux/buffer_head.h>
1539a53e0cSJaegeuk Kim #include <linux/slab.h>
1639a53e0cSJaegeuk Kim #include <linux/crc32.h>
1739a53e0cSJaegeuk Kim #include <linux/magic.h>
18c2d715d1SJaegeuk Kim #include <linux/kobject.h>
197bd59381SGu Zheng #include <linux/sched.h>
207c2e5963SJaegeuk Kim #include <linux/cred.h>
2139307a8eSJaegeuk Kim #include <linux/vmalloc.h>
22740432f8SJaegeuk Kim #include <linux/bio.h>
23d0239e1bSJaegeuk Kim #include <linux/blkdev.h>
240abd675eSChao Yu #include <linux/quotaops.h>
25c6a564ffSChristoph Hellwig #include <linux/part_stat.h>
2643b6573bSKeith Mok #include <crypto/hash.h>
2739a53e0cSJaegeuk Kim 
28734f0d24SDave Chinner #include <linux/fscrypt.h>
2995ae251fSEric Biggers #include <linux/fsverity.h>
30734f0d24SDave Chinner 
3151dcbdacSMatthew Wilcox (Oracle) struct pagevec;
3251dcbdacSMatthew Wilcox (Oracle) 
335d56b671SJaegeuk Kim #ifdef CONFIG_F2FS_CHECK_FS
349850cf4aSJaegeuk Kim #define f2fs_bug_on(sbi, condition)	BUG_ON(condition)
355d56b671SJaegeuk Kim #else
369850cf4aSJaegeuk Kim #define f2fs_bug_on(sbi, condition)					\
379850cf4aSJaegeuk Kim 	do {								\
38db489652SYangtao Li 		if (WARN_ON(condition))					\
39caf0047eSChao Yu 			set_sbi_flag(sbi, SBI_NEED_FSCK);		\
409850cf4aSJaegeuk Kim 	} while (0)
415d56b671SJaegeuk Kim #endif
425d56b671SJaegeuk Kim 
432c63feadSJaegeuk Kim enum {
442c63feadSJaegeuk Kim 	FAULT_KMALLOC,
45628b3d14SChao Yu 	FAULT_KVMALLOC,
46c41f3cc3SJaegeuk Kim 	FAULT_PAGE_ALLOC,
4701eccef7SChao Yu 	FAULT_PAGE_GET,
48b96d9b3bSChao Yu 	FAULT_ALLOC_BIO,	/* it's obsolete due to bio_alloc() will never fail */
49cb78942bSJaegeuk Kim 	FAULT_ALLOC_NID,
50cb78942bSJaegeuk Kim 	FAULT_ORPHAN,
51cb78942bSJaegeuk Kim 	FAULT_BLOCK,
52cb78942bSJaegeuk Kim 	FAULT_DIR_DEPTH,
5353aa6bbfSJaegeuk Kim 	FAULT_EVICT_INODE,
5414b44d23SJaegeuk Kim 	FAULT_TRUNCATE,
556f5c2ed0SChao Yu 	FAULT_READ_IO,
560f348028SChao Yu 	FAULT_CHECKPOINT,
57b83dcfe6SChao Yu 	FAULT_DISCARD,
586f5c2ed0SChao Yu 	FAULT_WRITE_IO,
5932410577SChao Yu 	FAULT_SLAB_ALLOC,
6010a26878SChao Yu 	FAULT_DQUOT_INIT,
613e020389SChao Yu 	FAULT_LOCK_OP,
622c63feadSJaegeuk Kim 	FAULT_MAX,
632c63feadSJaegeuk Kim };
642c63feadSJaegeuk Kim 
657fa750a1SArnd Bergmann #ifdef CONFIG_F2FS_FAULT_INJECTION
66d494500aSChao Yu #define F2FS_ALL_FAULT_TYPE		((1 << FAULT_MAX) - 1)
67d494500aSChao Yu 
6808796897SSheng Yong struct f2fs_fault_info {
6908796897SSheng Yong 	atomic_t inject_ops;
7008796897SSheng Yong 	unsigned int inject_rate;
7108796897SSheng Yong 	unsigned int inject_type;
7208796897SSheng Yong };
7308796897SSheng Yong 
7419880e6eSAlexey Dobriyan extern const char *f2fs_fault_name[FAULT_MAX];
7568afcf2dSTomohiro Kusumi #define IS_FAULT_SET(fi, type) ((fi)->inject_type & (1 << (type)))
762c63feadSJaegeuk Kim #endif
772c63feadSJaegeuk Kim 
7839a53e0cSJaegeuk Kim /*
7939a53e0cSJaegeuk Kim  * For mount options
8039a53e0cSJaegeuk Kim  */
8139a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_ROLL_FORWARD	0x00000002
8239a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISCARD		0x00000004
8339a53e0cSJaegeuk Kim #define F2FS_MOUNT_NOHEAP		0x00000008
8439a53e0cSJaegeuk Kim #define F2FS_MOUNT_XATTR_USER		0x00000010
8539a53e0cSJaegeuk Kim #define F2FS_MOUNT_POSIX_ACL		0x00000020
8639a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_EXT_IDENTIFY	0x00000040
87444c580fSJaegeuk Kim #define F2FS_MOUNT_INLINE_XATTR		0x00000080
881001b347SHuajun Li #define F2FS_MOUNT_INLINE_DATA		0x00000100
8934d67debSChao Yu #define F2FS_MOUNT_INLINE_DENTRY	0x00000200
9034d67debSChao Yu #define F2FS_MOUNT_FLUSH_MERGE		0x00000400
9134d67debSChao Yu #define F2FS_MOUNT_NOBARRIER		0x00000800
92d5053a34SJaegeuk Kim #define F2FS_MOUNT_FASTBOOT		0x00001000
9389672159SChao Yu #define F2FS_MOUNT_EXTENT_CACHE		0x00002000
94343f40f0SChao Yu #define F2FS_MOUNT_DATA_FLUSH		0x00008000
9573faec4dSJaegeuk Kim #define F2FS_MOUNT_FAULT_INJECTION	0x00010000
960abd675eSChao Yu #define F2FS_MOUNT_USRQUOTA		0x00080000
970abd675eSChao Yu #define F2FS_MOUNT_GRPQUOTA		0x00100000
985c57132eSChao Yu #define F2FS_MOUNT_PRJQUOTA		0x00200000
994b2414d0SChao Yu #define F2FS_MOUNT_QUOTA		0x00400000
1006afc662eSChao Yu #define F2FS_MOUNT_INLINE_XATTR_SIZE	0x00800000
1017e65be49SJaegeuk Kim #define F2FS_MOUNT_RESERVE_ROOT		0x01000000
1024354994fSDaniel Rosenberg #define F2FS_MOUNT_DISABLE_CHECKPOINT	0x02000000
103a9117ecaSChao Yu #define F2FS_MOUNT_NORECOVERY		0x04000000
104093749e2SChao Yu #define F2FS_MOUNT_ATGC			0x08000000
105261eeb9cSDaeho Jeong #define F2FS_MOUNT_MERGE_CHECKPOINT	0x10000000
1065911d2d1SChao Yu #define	F2FS_MOUNT_GC_MERGE		0x20000000
1076ce19affSChao Yu #define F2FS_MOUNT_COMPRESS_CACHE	0x40000000
10839a53e0cSJaegeuk Kim 
10963189b78SChao Yu #define F2FS_OPTION(sbi)	((sbi)->mount_opt)
11063189b78SChao Yu #define clear_opt(sbi, option)	(F2FS_OPTION(sbi).opt &= ~F2FS_MOUNT_##option)
11163189b78SChao Yu #define set_opt(sbi, option)	(F2FS_OPTION(sbi).opt |= F2FS_MOUNT_##option)
11263189b78SChao Yu #define test_opt(sbi, option)	(F2FS_OPTION(sbi).opt & F2FS_MOUNT_##option)
11339a53e0cSJaegeuk Kim 
11439a53e0cSJaegeuk Kim #define ver_after(a, b)	(typecheck(unsigned long long, a) &&		\
11539a53e0cSJaegeuk Kim 		typecheck(unsigned long long, b) &&			\
11639a53e0cSJaegeuk Kim 		((long long)((a) - (b)) > 0))
11739a53e0cSJaegeuk Kim 
118a9841c4dSJaegeuk Kim typedef u32 block_t;	/*
119a9841c4dSJaegeuk Kim 			 * should not change u32, since it is the on-disk block
120a9841c4dSJaegeuk Kim 			 * address format, __le32.
121a9841c4dSJaegeuk Kim 			 */
12239a53e0cSJaegeuk Kim typedef u32 nid_t;
12339a53e0cSJaegeuk Kim 
1244c8ff709SChao Yu #define COMPRESS_EXT_NUM		16
1254c8ff709SChao Yu 
126e4544b63STim Murray /*
127e4544b63STim Murray  * An implementation of an rwsem that is explicitly unfair to readers. This
128e4544b63STim Murray  * prevents priority inversion when a low-priority reader acquires the read lock
129e4544b63STim Murray  * while sleeping on the write lock but the write lock is needed by
130e4544b63STim Murray  * higher-priority clients.
131e4544b63STim Murray  */
132e4544b63STim Murray 
133e4544b63STim Murray struct f2fs_rwsem {
134e4544b63STim Murray         struct rw_semaphore internal_rwsem;
135e4544b63STim Murray         wait_queue_head_t read_waiters;
136e4544b63STim Murray };
137e4544b63STim Murray 
13839a53e0cSJaegeuk Kim struct f2fs_mount_info {
13939a53e0cSJaegeuk Kim 	unsigned int opt;
14063189b78SChao Yu 	int write_io_size_bits;		/* Write IO size bits */
14163189b78SChao Yu 	block_t root_reserved_blocks;	/* root reserved blocks */
14263189b78SChao Yu 	kuid_t s_resuid;		/* reserved blocks for uid */
14363189b78SChao Yu 	kgid_t s_resgid;		/* reserved blocks for gid */
14463189b78SChao Yu 	int active_logs;		/* # of active logs */
14563189b78SChao Yu 	int inline_xattr_size;		/* inline xattr size */
14663189b78SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
14763189b78SChao Yu 	struct f2fs_fault_info fault_info;	/* For fault injection */
14863189b78SChao Yu #endif
14963189b78SChao Yu #ifdef CONFIG_QUOTA
15063189b78SChao Yu 	/* Names of quota files with journalled quota */
15163189b78SChao Yu 	char *s_qf_names[MAXQUOTAS];
15263189b78SChao Yu 	int s_jquota_fmt;			/* Format of quota to use */
15363189b78SChao Yu #endif
15463189b78SChao Yu 	/* For which write hints are passed down to block layer */
15563189b78SChao Yu 	int whint_mode;
15663189b78SChao Yu 	int alloc_mode;			/* segment allocation policy */
15763189b78SChao Yu 	int fsync_mode;			/* fsync policy */
158b0332a0fSChao Yu 	int fs_mode;			/* fs mode: LFS or ADAPTIVE */
159bbbc34fdSChao Yu 	int bggc_mode;			/* bggc mode: off, on or sync */
1604f993264SChao Yu 	int discard_unit;		/*
1614f993264SChao Yu 					 * discard command's offset/size should
1624f993264SChao Yu 					 * be aligned to this unit: block,
1634f993264SChao Yu 					 * segment or section
1644f993264SChao Yu 					 */
165ac4acb1fSEric Biggers 	struct fscrypt_dummy_policy dummy_enc_policy; /* test dummy encryption */
1661ae18f71SJaegeuk Kim 	block_t unusable_cap_perc;	/* percentage for cap */
1674d3aed70SDaniel Rosenberg 	block_t unusable_cap;		/* Amount of space allowed to be
1684d3aed70SDaniel Rosenberg 					 * unusable when disabling checkpoint
1694d3aed70SDaniel Rosenberg 					 */
1704c8ff709SChao Yu 
1714c8ff709SChao Yu 	/* For compression */
1724c8ff709SChao Yu 	unsigned char compress_algorithm;	/* algorithm type */
173b28f047bSChao Yu 	unsigned char compress_log_size;	/* cluster log size */
1743fde13f8SChao Yu 	unsigned char compress_level;		/* compress level */
175b28f047bSChao Yu 	bool compress_chksum;			/* compressed data chksum */
1764c8ff709SChao Yu 	unsigned char compress_ext_cnt;		/* extension count */
177151b1982SFengnan Chang 	unsigned char nocompress_ext_cnt;		/* nocompress extension count */
178602a16d5SDaeho Jeong 	int compress_mode;			/* compression mode */
1794c8ff709SChao Yu 	unsigned char extensions[COMPRESS_EXT_NUM][F2FS_EXTENSION_LEN];	/* extensions */
180151b1982SFengnan Chang 	unsigned char noextensions[COMPRESS_EXT_NUM][F2FS_EXTENSION_LEN]; /* extensions */
18139a53e0cSJaegeuk Kim };
18239a53e0cSJaegeuk Kim 
183cde4de12SJaegeuk Kim #define F2FS_FEATURE_ENCRYPT		0x0001
1840bfd7a09SDamien Le Moal #define F2FS_FEATURE_BLKZONED		0x0002
185e65ef207SJaegeuk Kim #define F2FS_FEATURE_ATOMIC_WRITE	0x0004
1867a2af766SChao Yu #define F2FS_FEATURE_EXTRA_ATTR		0x0008
1875c57132eSChao Yu #define F2FS_FEATURE_PRJQUOTA		0x0010
188704956ecSChao Yu #define F2FS_FEATURE_INODE_CHKSUM	0x0020
1896afc662eSChao Yu #define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR	0x0040
190234a9689SJaegeuk Kim #define F2FS_FEATURE_QUOTA_INO		0x0080
1911c1d35dfSChao Yu #define F2FS_FEATURE_INODE_CRTIME	0x0100
192b7c409deSSheng Yong #define F2FS_FEATURE_LOST_FOUND		0x0200
19395ae251fSEric Biggers #define F2FS_FEATURE_VERITY		0x0400
194d440c52dSJunling Zheng #define F2FS_FEATURE_SB_CHKSUM		0x0800
1955aba5430SDaniel Rosenberg #define F2FS_FEATURE_CASEFOLD		0x1000
1964c8ff709SChao Yu #define F2FS_FEATURE_COMPRESSION	0x2000
197a7d9fe3cSJaegeuk Kim #define F2FS_FEATURE_RO			0x4000
198cde4de12SJaegeuk Kim 
1997beb01f7SChao Yu #define __F2FS_HAS_FEATURE(raw_super, mask)				\
2007beb01f7SChao Yu 	((raw_super->feature & cpu_to_le32(mask)) != 0)
2017beb01f7SChao Yu #define F2FS_HAS_FEATURE(sbi, mask)	__F2FS_HAS_FEATURE(sbi->raw_super, mask)
2027beb01f7SChao Yu #define F2FS_SET_FEATURE(sbi, mask)					\
2037beb01f7SChao Yu 	(sbi->raw_super->feature |= cpu_to_le32(mask))
2047beb01f7SChao Yu #define F2FS_CLEAR_FEATURE(sbi, mask)					\
2057beb01f7SChao Yu 	(sbi->raw_super->feature &= ~cpu_to_le32(mask))
20676f105a2SJaegeuk Kim 
20739a53e0cSJaegeuk Kim /*
2087c2e5963SJaegeuk Kim  * Default values for user and/or group using reserved blocks
2097c2e5963SJaegeuk Kim  */
2107c2e5963SJaegeuk Kim #define	F2FS_DEF_RESUID		0
2117c2e5963SJaegeuk Kim #define	F2FS_DEF_RESGID		0
2127c2e5963SJaegeuk Kim 
2137c2e5963SJaegeuk Kim /*
21439a53e0cSJaegeuk Kim  * For checkpoint manager
21539a53e0cSJaegeuk Kim  */
21639a53e0cSJaegeuk Kim enum {
21739a53e0cSJaegeuk Kim 	NAT_BITMAP,
21839a53e0cSJaegeuk Kim 	SIT_BITMAP
21939a53e0cSJaegeuk Kim };
22039a53e0cSJaegeuk Kim 
221c473f1a9SChao Yu #define	CP_UMOUNT	0x00000001
222c473f1a9SChao Yu #define	CP_FASTBOOT	0x00000002
223c473f1a9SChao Yu #define	CP_SYNC		0x00000004
224c473f1a9SChao Yu #define	CP_RECOVERY	0x00000008
225c473f1a9SChao Yu #define	CP_DISCARD	0x00000010
2261f43e2adSChao Yu #define CP_TRIMMED	0x00000020
2274354994fSDaniel Rosenberg #define CP_PAUSE	0x00000040
228b4b10061SJaegeuk Kim #define CP_RESIZE 	0x00000080
22975ab4cb8SJaegeuk Kim 
2304ddb1a4dSJaegeuk Kim #define MAX_DISCARD_BLOCKS(sbi)		BLKS_PER_SEC(sbi)
231ecc9aa00SChao Yu #define DEF_MAX_DISCARD_REQUEST		8	/* issue 8 discards per round */
232969d1b18SChao Yu #define DEF_MIN_DISCARD_ISSUE_TIME	50	/* 50 ms, if exists */
233f9d1dcedSYunlei He #define DEF_MID_DISCARD_ISSUE_TIME	500	/* 500 ms, if device busy */
234969d1b18SChao Yu #define DEF_MAX_DISCARD_ISSUE_TIME	60000	/* 60 s, if no candidates */
2358bb4f253SJaegeuk Kim #define DEF_DISCARD_URGENT_UTIL		80	/* do more discard over 80% */
23660b99b48SJaegeuk Kim #define DEF_CP_INTERVAL			60	/* 60 secs */
237dcf25fe8SChao Yu #define DEF_IDLE_INTERVAL		5	/* 5 secs */
2384354994fSDaniel Rosenberg #define DEF_DISABLE_INTERVAL		5	/* 5 secs */
239db610a64SJaegeuk Kim #define DEF_DISABLE_QUICK_INTERVAL	1	/* 1 secs */
24003f2c02dSJaegeuk Kim #define DEF_UMOUNT_DISCARD_TIMEOUT	5	/* 5 secs */
241bba681cbSJaegeuk Kim 
24275ab4cb8SJaegeuk Kim struct cp_control {
24375ab4cb8SJaegeuk Kim 	int reason;
2444b2fecc8SJaegeuk Kim 	__u64 trim_start;
2454b2fecc8SJaegeuk Kim 	__u64 trim_end;
2464b2fecc8SJaegeuk Kim 	__u64 trim_minlen;
24775ab4cb8SJaegeuk Kim };
24875ab4cb8SJaegeuk Kim 
249662befdaSChao Yu /*
250e1da7872SChao Yu  * indicate meta/data type
251662befdaSChao Yu  */
252662befdaSChao Yu enum {
253662befdaSChao Yu 	META_CP,
254662befdaSChao Yu 	META_NAT,
25581c1a0f1SChao Yu 	META_SIT,
2564c521f49SJaegeuk Kim 	META_SSA,
257b63e7be5SChao Yu 	META_MAX,
2584c521f49SJaegeuk Kim 	META_POR,
25993770ab7SChao Yu 	DATA_GENERIC,		/* check range only */
26093770ab7SChao Yu 	DATA_GENERIC_ENHANCE,	/* strong check on range and segment bitmap */
26193770ab7SChao Yu 	DATA_GENERIC_ENHANCE_READ,	/*
26293770ab7SChao Yu 					 * strong check on range and segment
26393770ab7SChao Yu 					 * bitmap but no warning due to race
26493770ab7SChao Yu 					 * condition of read on truncated area
26593770ab7SChao Yu 					 * by extent_cache
26693770ab7SChao Yu 					 */
267e1da7872SChao Yu 	META_GENERIC,
268662befdaSChao Yu };
269662befdaSChao Yu 
2706451e041SJaegeuk Kim /* for the list of ino */
2716451e041SJaegeuk Kim enum {
2726451e041SJaegeuk Kim 	ORPHAN_INO,		/* for orphan ino list */
273fff04f90SJaegeuk Kim 	APPEND_INO,		/* for append ino list */
274fff04f90SJaegeuk Kim 	UPDATE_INO,		/* for update ino list */
2750a007b97SJaegeuk Kim 	TRANS_DIR_INO,		/* for trasactions dir ino list */
27639d787beSChao Yu 	FLUSH_INO,		/* for multiple device flushing */
2776451e041SJaegeuk Kim 	MAX_INO_ENTRY,		/* max. list */
2786451e041SJaegeuk Kim };
2796451e041SJaegeuk Kim 
2806451e041SJaegeuk Kim struct ino_entry {
28139a53e0cSJaegeuk Kim 	struct list_head list;		/* list head */
28239a53e0cSJaegeuk Kim 	nid_t ino;			/* inode number */
28339d787beSChao Yu 	unsigned int dirty_device;	/* dirty device bitmap */
28439a53e0cSJaegeuk Kim };
28539a53e0cSJaegeuk Kim 
2862710fd7eSChao Yu /* for the list of inodes to be GCed */
28706292073SChao Yu struct inode_entry {
28839a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
28939a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
29039a53e0cSJaegeuk Kim };
29139a53e0cSJaegeuk Kim 
29250fa53ecSChao Yu struct fsync_node_entry {
29350fa53ecSChao Yu 	struct list_head list;	/* list head */
29450fa53ecSChao Yu 	struct page *page;	/* warm node page pointer */
29550fa53ecSChao Yu 	unsigned int seq_id;	/* sequence id */
29650fa53ecSChao Yu };
29750fa53ecSChao Yu 
298261eeb9cSDaeho Jeong struct ckpt_req {
299261eeb9cSDaeho Jeong 	struct completion wait;		/* completion for checkpoint done */
300261eeb9cSDaeho Jeong 	struct llist_node llnode;	/* llist_node to be linked in wait queue */
301261eeb9cSDaeho Jeong 	int ret;			/* return code of checkpoint */
302261eeb9cSDaeho Jeong 	ktime_t queue_time;		/* request queued time */
303261eeb9cSDaeho Jeong };
304261eeb9cSDaeho Jeong 
305261eeb9cSDaeho Jeong struct ckpt_req_control {
306261eeb9cSDaeho Jeong 	struct task_struct *f2fs_issue_ckpt;	/* checkpoint task */
307e6592066SDaeho Jeong 	int ckpt_thread_ioprio;			/* checkpoint merge thread ioprio */
308261eeb9cSDaeho Jeong 	wait_queue_head_t ckpt_wait_queue;	/* waiting queue for wake-up */
309261eeb9cSDaeho Jeong 	atomic_t issued_ckpt;		/* # of actually issued ckpts */
310261eeb9cSDaeho Jeong 	atomic_t total_ckpt;		/* # of total ckpts */
311261eeb9cSDaeho Jeong 	atomic_t queued_ckpt;		/* # of queued ckpts */
312261eeb9cSDaeho Jeong 	struct llist_head issue_list;	/* list for command issue */
313261eeb9cSDaeho Jeong 	spinlock_t stat_lock;		/* lock for below checkpoint time stats */
314261eeb9cSDaeho Jeong 	unsigned int cur_time;		/* cur wait time in msec for currently issued checkpoint */
315261eeb9cSDaeho Jeong 	unsigned int peak_time;		/* peak wait time in msec until now */
316261eeb9cSDaeho Jeong };
317261eeb9cSDaeho Jeong 
318a7eeb823SChao Yu /* for the bitmap indicate blocks to be discarded */
3197fd9e544SJaegeuk Kim struct discard_entry {
3207fd9e544SJaegeuk Kim 	struct list_head list;	/* list head */
321a7eeb823SChao Yu 	block_t start_blkaddr;	/* start blockaddr of current segment */
322a7eeb823SChao Yu 	unsigned char discard_map[SIT_VBLOCK_MAP_SIZE];	/* segment discard bitmap */
3237fd9e544SJaegeuk Kim };
3247fd9e544SJaegeuk Kim 
325969d1b18SChao Yu /* default discard granularity of inner discard thread, unit: block count */
326969d1b18SChao Yu #define DEFAULT_DISCARD_GRANULARITY		16
327969d1b18SChao Yu 
328ba48a33eSChao Yu /* max discard pend list number */
329ba48a33eSChao Yu #define MAX_PLIST_NUM		512
330ba48a33eSChao Yu #define plist_idx(blk_num)	((blk_num) >= MAX_PLIST_NUM ?		\
3312f84babfSSheng Yong 					(MAX_PLIST_NUM - 1) : ((blk_num) - 1))
332ba48a33eSChao Yu 
33315469963SJaegeuk Kim enum {
33435ec7d57SChao Yu 	D_PREP,			/* initial */
33535ec7d57SChao Yu 	D_PARTIAL,		/* partially submitted */
33635ec7d57SChao Yu 	D_SUBMIT,		/* all submitted */
33735ec7d57SChao Yu 	D_DONE,			/* finished */
33815469963SJaegeuk Kim };
33915469963SJaegeuk Kim 
340004b6862SChao Yu struct discard_info {
341b01a9201SJaegeuk Kim 	block_t lstart;			/* logical start address */
342b01a9201SJaegeuk Kim 	block_t len;			/* length */
343004b6862SChao Yu 	block_t start;			/* actual start address in dev */
344004b6862SChao Yu };
345004b6862SChao Yu 
346b01a9201SJaegeuk Kim struct discard_cmd {
347004b6862SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
348004b6862SChao Yu 	union {
349004b6862SChao Yu 		struct {
350004b6862SChao Yu 			block_t lstart;	/* logical start address */
351004b6862SChao Yu 			block_t len;	/* length */
352004b6862SChao Yu 			block_t start;	/* actual start address in dev */
353004b6862SChao Yu 		};
354004b6862SChao Yu 		struct discard_info di;	/* discard info */
355004b6862SChao Yu 
356004b6862SChao Yu 	};
357b01a9201SJaegeuk Kim 	struct list_head list;		/* command list */
358b01a9201SJaegeuk Kim 	struct completion wait;		/* compleation */
359c81abe34SJaegeuk Kim 	struct block_device *bdev;	/* bdev */
360ec9895adSChao Yu 	unsigned short ref;		/* reference count */
3619a744b92SChao Yu 	unsigned char state;		/* state */
36272691af6SJaegeuk Kim 	unsigned char queued;		/* queued discard */
363c81abe34SJaegeuk Kim 	int error;			/* bio error */
36435ec7d57SChao Yu 	spinlock_t lock;		/* for state/bio_ref updating */
36535ec7d57SChao Yu 	unsigned short bio_ref;		/* bio reference count */
366275b66b0SChao Yu };
367275b66b0SChao Yu 
36878997b56SChao Yu enum {
36978997b56SChao Yu 	DPOLICY_BG,
37078997b56SChao Yu 	DPOLICY_FORCE,
37178997b56SChao Yu 	DPOLICY_FSTRIM,
37278997b56SChao Yu 	DPOLICY_UMOUNT,
37378997b56SChao Yu 	MAX_DPOLICY,
37478997b56SChao Yu };
37578997b56SChao Yu 
376ecc9aa00SChao Yu struct discard_policy {
37778997b56SChao Yu 	int type;			/* type of discard */
378ecc9aa00SChao Yu 	unsigned int min_interval;	/* used for candidates exist */
379f9d1dcedSYunlei He 	unsigned int mid_interval;	/* used for device busy */
380ecc9aa00SChao Yu 	unsigned int max_interval;	/* used for candidates not exist */
381ecc9aa00SChao Yu 	unsigned int max_requests;	/* # of discards issued per round */
382ecc9aa00SChao Yu 	unsigned int io_aware_gran;	/* minimum granularity discard not be aware of I/O */
383ecc9aa00SChao Yu 	bool io_aware;			/* issue discard in idle time */
384ecc9aa00SChao Yu 	bool sync;			/* submit discard with REQ_SYNC flag */
38520ee4382SChao Yu 	bool ordered;			/* issue discard by lba order */
3866ce48b0cSChao Yu 	bool timeout;			/* discard timeout for put_super */
38778997b56SChao Yu 	unsigned int granularity;	/* discard granularity */
388ecc9aa00SChao Yu };
389ecc9aa00SChao Yu 
3900b54fb84SJaegeuk Kim struct discard_cmd_control {
39115469963SJaegeuk Kim 	struct task_struct *f2fs_issue_discard;	/* discard thread */
39246f84c2cSChao Yu 	struct list_head entry_list;		/* 4KB discard entry list */
393ba48a33eSChao Yu 	struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
39446f84c2cSChao Yu 	struct list_head wait_list;		/* store on-flushing entries */
3958412663dSChao Yu 	struct list_head fstrim_list;		/* in-flight discard from fstrim */
39615469963SJaegeuk Kim 	wait_queue_head_t discard_wait_queue;	/* waiting queue for wake-up */
397969d1b18SChao Yu 	unsigned int discard_wake;		/* to wake up discard thread */
39815469963SJaegeuk Kim 	struct mutex cmd_lock;
399d618ebafSChao Yu 	unsigned int nr_discards;		/* # of discards in the list */
400d618ebafSChao Yu 	unsigned int max_discards;		/* max. discards to be issued */
401d2d8e896SKonstantin Vyshetsky 	unsigned int max_discard_request;	/* max. discard request per round */
402d2d8e896SKonstantin Vyshetsky 	unsigned int min_discard_issue_time;	/* min. interval between discard issue */
403d2d8e896SKonstantin Vyshetsky 	unsigned int mid_discard_issue_time;	/* mid. interval between discard issue */
404d2d8e896SKonstantin Vyshetsky 	unsigned int max_discard_issue_time;	/* max. interval between discard issue */
405969d1b18SChao Yu 	unsigned int discard_granularity;	/* discard granularity */
406d84d1cbdSChao Yu 	unsigned int undiscard_blks;		/* # of undiscard blocks */
40720ee4382SChao Yu 	unsigned int next_pos;			/* next discard position */
4088b8dd65fSChao Yu 	atomic_t issued_discard;		/* # of issued discard */
40972691af6SJaegeuk Kim 	atomic_t queued_discard;		/* # of queued discard */
4105f32366aSChao Yu 	atomic_t discard_cmd_cnt;		/* # of cached cmd count */
4114dada3fdSChao Yu 	struct rb_root_cached root;		/* root of discard rb-tree */
41267fce70bSChao Yu 	bool rbtree_check;			/* config for consistence check */
41339a53e0cSJaegeuk Kim };
41439a53e0cSJaegeuk Kim 
41539a53e0cSJaegeuk Kim /* for the list of fsync inodes, used only during recovery */
41639a53e0cSJaegeuk Kim struct fsync_inode_entry {
41739a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
41839a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
419c52e1b10SJaegeuk Kim 	block_t blkaddr;	/* block address locating the last fsync */
420c52e1b10SJaegeuk Kim 	block_t last_dentry;	/* block address locating the last dentry */
42139a53e0cSJaegeuk Kim };
42239a53e0cSJaegeuk Kim 
42368afcf2dSTomohiro Kusumi #define nats_in_cursum(jnl)		(le16_to_cpu((jnl)->n_nats))
42468afcf2dSTomohiro Kusumi #define sits_in_cursum(jnl)		(le16_to_cpu((jnl)->n_sits))
42539a53e0cSJaegeuk Kim 
42668afcf2dSTomohiro Kusumi #define nat_in_journal(jnl, i)		((jnl)->nat_j.entries[i].ne)
42768afcf2dSTomohiro Kusumi #define nid_in_journal(jnl, i)		((jnl)->nat_j.entries[i].nid)
42868afcf2dSTomohiro Kusumi #define sit_in_journal(jnl, i)		((jnl)->sit_j.entries[i].se)
42968afcf2dSTomohiro Kusumi #define segno_in_journal(jnl, i)	((jnl)->sit_j.entries[i].segno)
43039a53e0cSJaegeuk Kim 
431dfc08a12SChao Yu #define MAX_NAT_JENTRIES(jnl)	(NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
432dfc08a12SChao Yu #define MAX_SIT_JENTRIES(jnl)	(SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
433309cc2b6SJaegeuk Kim 
434dfc08a12SChao Yu static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
43539a53e0cSJaegeuk Kim {
436dfc08a12SChao Yu 	int before = nats_in_cursum(journal);
437cac5a3d8SDongOh Shin 
438dfc08a12SChao Yu 	journal->n_nats = cpu_to_le16(before + i);
43939a53e0cSJaegeuk Kim 	return before;
44039a53e0cSJaegeuk Kim }
44139a53e0cSJaegeuk Kim 
442dfc08a12SChao Yu static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
44339a53e0cSJaegeuk Kim {
444dfc08a12SChao Yu 	int before = sits_in_cursum(journal);
445cac5a3d8SDongOh Shin 
446dfc08a12SChao Yu 	journal->n_sits = cpu_to_le16(before + i);
44739a53e0cSJaegeuk Kim 	return before;
44839a53e0cSJaegeuk Kim }
44939a53e0cSJaegeuk Kim 
450dfc08a12SChao Yu static inline bool __has_cursum_space(struct f2fs_journal *journal,
451dfc08a12SChao Yu 							int size, int type)
452184a5cd2SChao Yu {
453184a5cd2SChao Yu 	if (type == NAT_JOURNAL)
454dfc08a12SChao Yu 		return size <= MAX_NAT_JENTRIES(journal);
455dfc08a12SChao Yu 	return size <= MAX_SIT_JENTRIES(journal);
456184a5cd2SChao Yu }
457184a5cd2SChao Yu 
458f2470371SChao Yu /* for inline stuff */
459f2470371SChao Yu #define DEF_INLINE_RESERVED_SIZE	1
4607a2af766SChao Yu static inline int get_extra_isize(struct inode *inode);
4616afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode);
4627a2af766SChao Yu #define MAX_INLINE_DATA(inode)	(sizeof(__le32) *			\
4637a2af766SChao Yu 				(CUR_ADDRS_PER_INODE(inode) -		\
464b323fd28SChao Yu 				get_inline_xattr_addrs(inode) -	\
4656afc662eSChao Yu 				DEF_INLINE_RESERVED_SIZE))
466f2470371SChao Yu 
467f2470371SChao Yu /* for inline dir */
468f2470371SChao Yu #define NR_INLINE_DENTRY(inode)	(MAX_INLINE_DATA(inode) * BITS_PER_BYTE / \
469f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
470f2470371SChao Yu 				BITS_PER_BYTE + 1))
471f91108b8SGeert Uytterhoeven #define INLINE_DENTRY_BITMAP_SIZE(inode) \
472f91108b8SGeert Uytterhoeven 	DIV_ROUND_UP(NR_INLINE_DENTRY(inode), BITS_PER_BYTE)
473f2470371SChao Yu #define INLINE_RESERVED_SIZE(inode)	(MAX_INLINE_DATA(inode) - \
474f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
475f2470371SChao Yu 				NR_INLINE_DENTRY(inode) + \
476f2470371SChao Yu 				INLINE_DENTRY_BITMAP_SIZE(inode)))
477f2470371SChao Yu 
478e9750824SNamjae Jeon /*
47939a53e0cSJaegeuk Kim  * For INODE and NODE manager
48039a53e0cSJaegeuk Kim  */
4817b3cd7d6SJaegeuk Kim /* for directory operations */
48243c780baSEric Biggers 
48343c780baSEric Biggers struct f2fs_filename {
48443c780baSEric Biggers 	/*
48543c780baSEric Biggers 	 * The filename the user specified.  This is NULL for some
48643c780baSEric Biggers 	 * filesystem-internal operations, e.g. converting an inline directory
48743c780baSEric Biggers 	 * to a non-inline one, or roll-forward recovering an encrypted dentry.
48843c780baSEric Biggers 	 */
48943c780baSEric Biggers 	const struct qstr *usr_fname;
49043c780baSEric Biggers 
49143c780baSEric Biggers 	/*
49243c780baSEric Biggers 	 * The on-disk filename.  For encrypted directories, this is encrypted.
49343c780baSEric Biggers 	 * This may be NULL for lookups in an encrypted dir without the key.
49443c780baSEric Biggers 	 */
49543c780baSEric Biggers 	struct fscrypt_str disk_name;
49643c780baSEric Biggers 
49743c780baSEric Biggers 	/* The dirhash of this filename */
49843c780baSEric Biggers 	f2fs_hash_t hash;
49943c780baSEric Biggers 
50043c780baSEric Biggers #ifdef CONFIG_FS_ENCRYPTION
50143c780baSEric Biggers 	/*
50243c780baSEric Biggers 	 * For lookups in encrypted directories: either the buffer backing
50343c780baSEric Biggers 	 * disk_name, or a buffer that holds the decoded no-key name.
50443c780baSEric Biggers 	 */
50543c780baSEric Biggers 	struct fscrypt_str crypto_buf;
50643c780baSEric Biggers #endif
50743c780baSEric Biggers #ifdef CONFIG_UNICODE
50843c780baSEric Biggers 	/*
50943c780baSEric Biggers 	 * For casefolded directories: the casefolded name, but it's left NULL
5107ad08a58SDaniel Rosenberg 	 * if the original name is not valid Unicode, if the directory is both
5117ad08a58SDaniel Rosenberg 	 * casefolded and encrypted and its encryption key is unavailable, or if
5127ad08a58SDaniel Rosenberg 	 * the filesystem is doing an internal operation where usr_fname is also
5137ad08a58SDaniel Rosenberg 	 * NULL.  In all these cases we fall back to treating the name as an
5147ad08a58SDaniel Rosenberg 	 * opaque byte sequence.
51543c780baSEric Biggers 	 */
51643c780baSEric Biggers 	struct fscrypt_str cf_name;
51743c780baSEric Biggers #endif
51843c780baSEric Biggers };
51943c780baSEric Biggers 
5207b3cd7d6SJaegeuk Kim struct f2fs_dentry_ptr {
521d8c6822aSJaegeuk Kim 	struct inode *inode;
52276a9dd85SChao Yu 	void *bitmap;
5237b3cd7d6SJaegeuk Kim 	struct f2fs_dir_entry *dentry;
5247b3cd7d6SJaegeuk Kim 	__u8 (*filename)[F2FS_SLOT_LEN];
5257b3cd7d6SJaegeuk Kim 	int max;
52676a9dd85SChao Yu 	int nr_bitmap;
5277b3cd7d6SJaegeuk Kim };
5287b3cd7d6SJaegeuk Kim 
52964c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_block(struct inode *inode,
53064c24ecbSTomohiro Kusumi 		struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
5317b3cd7d6SJaegeuk Kim {
532d8c6822aSJaegeuk Kim 	d->inode = inode;
5337b3cd7d6SJaegeuk Kim 	d->max = NR_DENTRY_IN_BLOCK;
53476a9dd85SChao Yu 	d->nr_bitmap = SIZE_OF_DENTRY_BITMAP;
535c79d1520SYunlong Song 	d->bitmap = t->dentry_bitmap;
5367b3cd7d6SJaegeuk Kim 	d->dentry = t->dentry;
5377b3cd7d6SJaegeuk Kim 	d->filename = t->filename;
53864c24ecbSTomohiro Kusumi }
539cac5a3d8SDongOh Shin 
54064c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_inline(struct inode *inode,
541f2470371SChao Yu 					struct f2fs_dentry_ptr *d, void *t)
54264c24ecbSTomohiro Kusumi {
543f2470371SChao Yu 	int entry_cnt = NR_INLINE_DENTRY(inode);
544f2470371SChao Yu 	int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(inode);
545f2470371SChao Yu 	int reserved_size = INLINE_RESERVED_SIZE(inode);
546f2470371SChao Yu 
54764c24ecbSTomohiro Kusumi 	d->inode = inode;
548f2470371SChao Yu 	d->max = entry_cnt;
549f2470371SChao Yu 	d->nr_bitmap = bitmap_size;
550f2470371SChao Yu 	d->bitmap = t;
551f2470371SChao Yu 	d->dentry = t + bitmap_size + reserved_size;
552f2470371SChao Yu 	d->filename = t + bitmap_size + reserved_size +
553f2470371SChao Yu 					SIZE_OF_DIR_ENTRY * entry_cnt;
5547b3cd7d6SJaegeuk Kim }
5557b3cd7d6SJaegeuk Kim 
556dbe6a5ffSJaegeuk Kim /*
557dbe6a5ffSJaegeuk Kim  * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
558dbe6a5ffSJaegeuk Kim  * as its node offset to distinguish from index node blocks.
559dbe6a5ffSJaegeuk Kim  * But some bits are used to mark the node block.
56039a53e0cSJaegeuk Kim  */
561dbe6a5ffSJaegeuk Kim #define XATTR_NODE_OFFSET	((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
562dbe6a5ffSJaegeuk Kim 				>> OFFSET_BIT_SHIFT)
563266e97a8SJaegeuk Kim enum {
564266e97a8SJaegeuk Kim 	ALLOC_NODE,			/* allocate a new node page if needed */
565266e97a8SJaegeuk Kim 	LOOKUP_NODE,			/* look up a node without readahead */
566266e97a8SJaegeuk Kim 	LOOKUP_NODE_RA,			/*
567266e97a8SJaegeuk Kim 					 * look up a node with readahead called
5684f4124d0SChao Yu 					 * by get_data_block.
56939a53e0cSJaegeuk Kim 					 */
570266e97a8SJaegeuk Kim };
571266e97a8SJaegeuk Kim 
57291803392SChao Yu #define DEFAULT_RETRY_IO_COUNT	8	/* maximum retry read IO or flush count */
5737735730dSChao Yu 
5745df7731fSChao Yu /* congestion wait timeout value, default: 20ms */
5755df7731fSChao Yu #define	DEFAULT_IO_TIMEOUT	(msecs_to_jiffies(20))
5765df7731fSChao Yu 
577af033b2aSChao Yu /* maximum retry quota flush count */
578af033b2aSChao Yu #define DEFAULT_RETRY_QUOTA_FLUSH_COUNT		8
579af033b2aSChao Yu 
580*50c63009SJaegeuk Kim /* maximum retry of EIO'ed meta page */
581*50c63009SJaegeuk Kim #define MAX_RETRY_META_PAGE_EIO			100
582*50c63009SJaegeuk Kim 
583a6db67f0SJaegeuk Kim #define F2FS_LINK_MAX	0xffffffff	/* maximum link count per file */
58439a53e0cSJaegeuk Kim 
585817202d9SChao Yu #define MAX_DIR_RA_PAGES	4	/* maximum ra pages of dir */
586817202d9SChao Yu 
587287b1406SChao Yu /* dirty segments threshold for triggering CP */
588287b1406SChao Yu #define DEFAULT_DIRTY_THRESHOLD		4
589287b1406SChao Yu 
59039a53e0cSJaegeuk Kim /* for in-memory extent cache entry */
59113054c54SChao Yu #define F2FS_MIN_EXTENT_LEN	64	/* minimum extent length */
59213054c54SChao Yu 
59313054c54SChao Yu /* number of extent info in extent cache we try to shrink */
59413054c54SChao Yu #define EXTENT_CACHE_SHRINK_NUMBER	128
595c11abd1aSJaegeuk Kim 
596430f163bSChao Yu #define RECOVERY_MAX_RA_BLOCKS		BIO_MAX_VECS
597430f163bSChao Yu #define RECOVERY_MIN_RA_BLOCKS		1
598430f163bSChao Yu 
59954c2258cSChao Yu struct rb_entry {
60054c2258cSChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
6012e9b2bb2SChao Yu 	union {
6022e9b2bb2SChao Yu 		struct {
60354c2258cSChao Yu 			unsigned int ofs;	/* start offset of the entry */
60454c2258cSChao Yu 			unsigned int len;	/* length of the entry */
60554c2258cSChao Yu 		};
6062e9b2bb2SChao Yu 		unsigned long long key;		/* 64-bits key */
60748046cb5SJaegeuk Kim 	} __packed;
6082e9b2bb2SChao Yu };
60954c2258cSChao Yu 
61039a53e0cSJaegeuk Kim struct extent_info {
61139a53e0cSJaegeuk Kim 	unsigned int fofs;		/* start offset in a file */
612111d2495SMasanari Iida 	unsigned int len;		/* length of the extent */
61354c2258cSChao Yu 	u32 blk;			/* start block address of the extent */
61494afd6d6SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
61594afd6d6SChao Yu 	unsigned int c_len;		/* physical extent length of compressed blocks */
61694afd6d6SChao Yu #endif
61739a53e0cSJaegeuk Kim };
61839a53e0cSJaegeuk Kim 
61913054c54SChao Yu struct extent_node {
620c0362117SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
62113054c54SChao Yu 	struct extent_info ei;		/* extent info */
62254c2258cSChao Yu 	struct list_head list;		/* node in global extent list of sbi */
623201ef5e0SHou Pengyang 	struct extent_tree *et;		/* extent tree pointer */
62413054c54SChao Yu };
62513054c54SChao Yu 
62613054c54SChao Yu struct extent_tree {
62713054c54SChao Yu 	nid_t ino;			/* inode number */
6284dada3fdSChao Yu 	struct rb_root_cached root;	/* root of extent info rb-tree */
62962c8af65SChao Yu 	struct extent_node *cached_en;	/* recently accessed extent node */
6303e72f721SJaegeuk Kim 	struct extent_info largest;	/* largested extent info */
631137d09f0SJaegeuk Kim 	struct list_head list;		/* to be used by sbi->zombie_list */
63213054c54SChao Yu 	rwlock_t lock;			/* protect extent info rb-tree */
63368e35385SChao Yu 	atomic_t node_cnt;		/* # of extent node in rb-tree*/
634b430f726SZhikang Zhang 	bool largest_updated;		/* largest extent updated */
63513054c54SChao Yu };
63613054c54SChao Yu 
63739a53e0cSJaegeuk Kim /*
638003a3e1dSJaegeuk Kim  * This structure is taken from ext4_map_blocks.
639003a3e1dSJaegeuk Kim  *
640003a3e1dSJaegeuk Kim  * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
641003a3e1dSJaegeuk Kim  */
642003a3e1dSJaegeuk Kim #define F2FS_MAP_NEW		(1 << BH_New)
643003a3e1dSJaegeuk Kim #define F2FS_MAP_MAPPED		(1 << BH_Mapped)
6447f63eb77SJaegeuk Kim #define F2FS_MAP_UNWRITTEN	(1 << BH_Unwritten)
6457f63eb77SJaegeuk Kim #define F2FS_MAP_FLAGS		(F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
6467f63eb77SJaegeuk Kim 				F2FS_MAP_UNWRITTEN)
647003a3e1dSJaegeuk Kim 
648003a3e1dSJaegeuk Kim struct f2fs_map_blocks {
64971f2c820SChao Yu 	struct block_device *m_bdev;	/* for multi-device dio */
650003a3e1dSJaegeuk Kim 	block_t m_pblk;
651003a3e1dSJaegeuk Kim 	block_t m_lblk;
652003a3e1dSJaegeuk Kim 	unsigned int m_len;
653003a3e1dSJaegeuk Kim 	unsigned int m_flags;
654da85985cSChao Yu 	pgoff_t *m_next_pgofs;		/* point next possible non-hole pgofs */
655c4020b2dSChao Yu 	pgoff_t *m_next_extent;		/* point to next possible extent */
656d5097be5SHyunchul Lee 	int m_seg_type;
657f9d6d059SChao Yu 	bool m_may_create;		/* indicate it is from write path */
65871f2c820SChao Yu 	bool m_multidev_dio;		/* indicate it allows multi-device dio */
659003a3e1dSJaegeuk Kim };
660003a3e1dSJaegeuk Kim 
661e2b4e2bcSChao Yu /* for flag in get_data_block */
662f2220c7fSQiuyang Sun enum {
663f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_DEFAULT,
664f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_FIEMAP,
665f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_BMAP,
6660a4daae5SJaegeuk Kim 	F2FS_GET_BLOCK_DIO,
667f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_DIO,
668f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_AIO,
669c4020b2dSChao Yu 	F2FS_GET_BLOCK_PRECACHE,
670f2220c7fSQiuyang Sun };
671e2b4e2bcSChao Yu 
672003a3e1dSJaegeuk Kim /*
67339a53e0cSJaegeuk Kim  * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
67439a53e0cSJaegeuk Kim  */
67539a53e0cSJaegeuk Kim #define FADVISE_COLD_BIT	0x01
676354a3399SJaegeuk Kim #define FADVISE_LOST_PINO_BIT	0x02
677cde4de12SJaegeuk Kim #define FADVISE_ENCRYPT_BIT	0x04
678e7d55452SJaegeuk Kim #define FADVISE_ENC_NAME_BIT	0x08
67926787236SJaegeuk Kim #define FADVISE_KEEP_SIZE_BIT	0x10
680b6a06cbbSChao Yu #define FADVISE_HOT_BIT		0x20
68195ae251fSEric Biggers #define FADVISE_VERITY_BIT	0x40
682d4dd19ecSJaegeuk Kim #define FADVISE_TRUNC_BIT	0x80
68339a53e0cSJaegeuk Kim 
684797c1cb5SChao Yu #define FADVISE_MODIFIABLE_BITS	(FADVISE_COLD_BIT | FADVISE_HOT_BIT)
685797c1cb5SChao Yu 
686b5492af7SJaegeuk Kim #define file_is_cold(inode)	is_file(inode, FADVISE_COLD_BIT)
687b5492af7SJaegeuk Kim #define file_set_cold(inode)	set_file(inode, FADVISE_COLD_BIT)
688b5492af7SJaegeuk Kim #define file_clear_cold(inode)	clear_file(inode, FADVISE_COLD_BIT)
6891153db09SChao Yu 
6901153db09SChao Yu #define file_wrong_pino(inode)	is_file(inode, FADVISE_LOST_PINO_BIT)
6911153db09SChao Yu #define file_lost_pino(inode)	set_file(inode, FADVISE_LOST_PINO_BIT)
692b5492af7SJaegeuk Kim #define file_got_pino(inode)	clear_file(inode, FADVISE_LOST_PINO_BIT)
6931153db09SChao Yu 
694cde4de12SJaegeuk Kim #define file_is_encrypt(inode)	is_file(inode, FADVISE_ENCRYPT_BIT)
695cde4de12SJaegeuk Kim #define file_set_encrypt(inode)	set_file(inode, FADVISE_ENCRYPT_BIT)
6961153db09SChao Yu 
697e7d55452SJaegeuk Kim #define file_enc_name(inode)	is_file(inode, FADVISE_ENC_NAME_BIT)
698e7d55452SJaegeuk Kim #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
6991153db09SChao Yu 
70026787236SJaegeuk Kim #define file_keep_isize(inode)	is_file(inode, FADVISE_KEEP_SIZE_BIT)
70126787236SJaegeuk Kim #define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
7021153db09SChao Yu 
703b6a06cbbSChao Yu #define file_is_hot(inode)	is_file(inode, FADVISE_HOT_BIT)
704b6a06cbbSChao Yu #define file_set_hot(inode)	set_file(inode, FADVISE_HOT_BIT)
705b6a06cbbSChao Yu #define file_clear_hot(inode)	clear_file(inode, FADVISE_HOT_BIT)
7061153db09SChao Yu 
70795ae251fSEric Biggers #define file_is_verity(inode)	is_file(inode, FADVISE_VERITY_BIT)
70895ae251fSEric Biggers #define file_set_verity(inode)	set_file(inode, FADVISE_VERITY_BIT)
709cde4de12SJaegeuk Kim 
710d4dd19ecSJaegeuk Kim #define file_should_truncate(inode)	is_file(inode, FADVISE_TRUNC_BIT)
711d4dd19ecSJaegeuk Kim #define file_need_truncate(inode)	set_file(inode, FADVISE_TRUNC_BIT)
712d4dd19ecSJaegeuk Kim #define file_dont_truncate(inode)	clear_file(inode, FADVISE_TRUNC_BIT)
713d4dd19ecSJaegeuk Kim 
714ab9fa662SJaegeuk Kim #define DEF_DIR_LEVEL		0
715ab9fa662SJaegeuk Kim 
7162ef79ecbSChao Yu enum {
7172ef79ecbSChao Yu 	GC_FAILURE_PIN,
7182ef79ecbSChao Yu 	GC_FAILURE_ATOMIC,
7192ef79ecbSChao Yu 	MAX_GC_FAILURE
7202ef79ecbSChao Yu };
7212ef79ecbSChao Yu 
7227653b9d8SChao Yu /* used for f2fs_inode_info->flags */
7237653b9d8SChao Yu enum {
7247653b9d8SChao Yu 	FI_NEW_INODE,		/* indicate newly allocated inode */
7257653b9d8SChao Yu 	FI_DIRTY_INODE,		/* indicate inode is dirty or not */
7267653b9d8SChao Yu 	FI_AUTO_RECOVER,	/* indicate inode is recoverable */
7277653b9d8SChao Yu 	FI_DIRTY_DIR,		/* indicate directory has dirty pages */
7287653b9d8SChao Yu 	FI_INC_LINK,		/* need to increment i_nlink */
7297653b9d8SChao Yu 	FI_ACL_MODE,		/* indicate acl mode */
7307653b9d8SChao Yu 	FI_NO_ALLOC,		/* should not allocate any blocks */
7317653b9d8SChao Yu 	FI_FREE_NID,		/* free allocated nide */
7327653b9d8SChao Yu 	FI_NO_EXTENT,		/* not to use the extent cache */
7337653b9d8SChao Yu 	FI_INLINE_XATTR,	/* used for inline xattr */
7347653b9d8SChao Yu 	FI_INLINE_DATA,		/* used for inline data*/
7357653b9d8SChao Yu 	FI_INLINE_DENTRY,	/* used for inline dentry */
7367653b9d8SChao Yu 	FI_APPEND_WRITE,	/* inode has appended data */
7377653b9d8SChao Yu 	FI_UPDATE_WRITE,	/* inode has in-place-update data */
7387653b9d8SChao Yu 	FI_NEED_IPU,		/* used for ipu per file */
7397653b9d8SChao Yu 	FI_ATOMIC_FILE,		/* indicate atomic file */
7407653b9d8SChao Yu 	FI_ATOMIC_COMMIT,	/* indicate the state of atomical committing */
7417653b9d8SChao Yu 	FI_VOLATILE_FILE,	/* indicate volatile file */
7427653b9d8SChao Yu 	FI_FIRST_BLOCK_WRITTEN,	/* indicate #0 data block was written */
7437653b9d8SChao Yu 	FI_DROP_CACHE,		/* drop dirty page cache */
7447653b9d8SChao Yu 	FI_DATA_EXIST,		/* indicate data exists */
7457653b9d8SChao Yu 	FI_INLINE_DOTS,		/* indicate inline dot dentries */
7461018a546SChao Yu 	FI_SKIP_WRITES,		/* should skip data page writeback */
7471018a546SChao Yu 	FI_OPU_WRITE,		/* used for opu per file */
7487653b9d8SChao Yu 	FI_DIRTY_FILE,		/* indicate regular/symlink has dirty pages */
7493d697a4aSEric Biggers 	FI_PREALLOCATED_ALL,	/* all blocks for write were preallocated */
7507653b9d8SChao Yu 	FI_HOT_DATA,		/* indicate file is hot */
7517653b9d8SChao Yu 	FI_EXTRA_ATTR,		/* indicate file has extra attribute */
7527653b9d8SChao Yu 	FI_PROJ_INHERIT,	/* indicate file inherits projectid */
7537653b9d8SChao Yu 	FI_PIN_FILE,		/* indicate file should not be gced */
7547653b9d8SChao Yu 	FI_ATOMIC_REVOKE_REQUEST, /* request to drop atomic data */
7557653b9d8SChao Yu 	FI_VERITY_IN_PROGRESS,	/* building fs-verity Merkle tree */
7567653b9d8SChao Yu 	FI_COMPRESSED_FILE,	/* indicate file's data can be compressed */
757b28f047bSChao Yu 	FI_COMPRESS_CORRUPT,	/* indicate compressed cluster is corrupted */
7587653b9d8SChao Yu 	FI_MMAP_FILE,		/* indicate file was mmapped */
759602a16d5SDaeho Jeong 	FI_ENABLE_COMPRESS,	/* enable compression in "user" compression mode */
760c6140415SJaegeuk Kim 	FI_COMPRESS_RELEASED,	/* compressed blocks were released */
761859fca6bSChao Yu 	FI_ALIGNED_WRITE,	/* enable aligned write */
7627653b9d8SChao Yu 	FI_MAX,			/* max flag, never be used */
7637653b9d8SChao Yu };
7647653b9d8SChao Yu 
76539a53e0cSJaegeuk Kim struct f2fs_inode_info {
76639a53e0cSJaegeuk Kim 	struct inode vfs_inode;		/* serve a vfs inode */
76739a53e0cSJaegeuk Kim 	unsigned long i_flags;		/* keep an inode flags for ioctl */
76839a53e0cSJaegeuk Kim 	unsigned char i_advise;		/* use to give file attribute hints */
76938431545SJaegeuk Kim 	unsigned char i_dir_level;	/* use for dentry level for large dir */
7701ad71a27SJaegeuk Kim 	unsigned int i_current_depth;	/* only for directory depth */
7712ef79ecbSChao Yu 	/* for gc failure statistic */
7722ef79ecbSChao Yu 	unsigned int i_gc_failures[MAX_GC_FAILURE];
7736666e6aaSJaegeuk Kim 	unsigned int i_pino;		/* parent inode number */
77439a53e0cSJaegeuk Kim 	umode_t i_acl_mode;		/* keep file acl mode temporarily */
77539a53e0cSJaegeuk Kim 
77639a53e0cSJaegeuk Kim 	/* Use below internally in f2fs*/
7777653b9d8SChao Yu 	unsigned long flags[BITS_TO_LONGS(FI_MAX)];	/* use to pass per-file flags */
778e4544b63STim Murray 	struct f2fs_rwsem i_sem;	/* protect fi info */
779204706c7SJaegeuk Kim 	atomic_t dirty_pages;		/* # of dirty pages */
78039a53e0cSJaegeuk Kim 	f2fs_hash_t chash;		/* hash value of given file name */
78139a53e0cSJaegeuk Kim 	unsigned int clevel;		/* maximum level of given file name */
78288c5c13aSJaegeuk Kim 	struct task_struct *task;	/* lookup and create consistency */
783b0af6d49SChao Yu 	struct task_struct *cp_task;	/* separate cp/wb IO stats*/
78439a53e0cSJaegeuk Kim 	nid_t i_xattr_nid;		/* node id that contains xattrs */
78526de9b11SJaegeuk Kim 	loff_t	last_disk_size;		/* lastly written file size */
786c10c9820SChao Yu 	spinlock_t i_size_lock;		/* protect last_disk_size */
78788b88a66SJaegeuk Kim 
7880abd675eSChao Yu #ifdef CONFIG_QUOTA
7890abd675eSChao Yu 	struct dquot *i_dquot[MAXQUOTAS];
7900abd675eSChao Yu 
7910abd675eSChao Yu 	/* quota space reservation, managed internally by quota code */
7920abd675eSChao Yu 	qsize_t i_reserved_quota;
7930abd675eSChao Yu #endif
7940f18b462SJaegeuk Kim 	struct list_head dirty_list;	/* dirty list for dirs and files */
7950f18b462SJaegeuk Kim 	struct list_head gdirty_list;	/* linked in global dirty list */
79657864ae5SJaegeuk Kim 	struct list_head inmem_ilist;	/* list for inmem inodes */
79788b88a66SJaegeuk Kim 	struct list_head inmem_pages;	/* inmemory pages managed by f2fs */
7987a10f017SJaegeuk Kim 	struct task_struct *inmem_task;	/* store inmemory task */
79988b88a66SJaegeuk Kim 	struct mutex inmem_lock;	/* lock for inmemory pages */
8003e72f721SJaegeuk Kim 	struct extent_tree *extent_tree;	/* cached extent_tree entry */
801b2532c69SChao Yu 
802b2532c69SChao Yu 	/* avoid racing between foreground op and gc */
803e4544b63STim Murray 	struct f2fs_rwsem i_gc_rwsem[2];
804e4544b63STim Murray 	struct f2fs_rwsem i_xattr_sem; /* avoid racing between reading and changing EAs */
805f2470371SChao Yu 
8067a2af766SChao Yu 	int i_extra_isize;		/* size of extra space located in i_addr */
8075c57132eSChao Yu 	kprojid_t i_projid;		/* id for project quota */
8086afc662eSChao Yu 	int i_inline_xattr_size;	/* inline xattr size */
80924b81dfcSArnd Bergmann 	struct timespec64 i_crtime;	/* inode creation time */
81024b81dfcSArnd Bergmann 	struct timespec64 i_disk_time[4];/* inode disk times */
8114c8ff709SChao Yu 
8124c8ff709SChao Yu 	/* for file compress */
813c2759ebaSDaeho Jeong 	atomic_t i_compr_blocks;		/* # of compressed blocks */
8144c8ff709SChao Yu 	unsigned char i_compress_algorithm;	/* algorithm type */
8154c8ff709SChao Yu 	unsigned char i_log_cluster_size;	/* log of cluster size */
8163fde13f8SChao Yu 	unsigned char i_compress_level;		/* compress level (lz4hc,zstd) */
817b28f047bSChao Yu 	unsigned short i_compress_flag;		/* compress flag */
8184c8ff709SChao Yu 	unsigned int i_cluster_size;		/* cluster size */
81939a53e0cSJaegeuk Kim };
82039a53e0cSJaegeuk Kim 
82139a53e0cSJaegeuk Kim static inline void get_extent_info(struct extent_info *ext,
822bd933d4fSChao Yu 					struct f2fs_extent *i_ext)
82339a53e0cSJaegeuk Kim {
824bd933d4fSChao Yu 	ext->fofs = le32_to_cpu(i_ext->fofs);
825bd933d4fSChao Yu 	ext->blk = le32_to_cpu(i_ext->blk);
826bd933d4fSChao Yu 	ext->len = le32_to_cpu(i_ext->len);
82739a53e0cSJaegeuk Kim }
82839a53e0cSJaegeuk Kim 
82939a53e0cSJaegeuk Kim static inline void set_raw_extent(struct extent_info *ext,
83039a53e0cSJaegeuk Kim 					struct f2fs_extent *i_ext)
83139a53e0cSJaegeuk Kim {
83239a53e0cSJaegeuk Kim 	i_ext->fofs = cpu_to_le32(ext->fofs);
8334d0b0bd4SChao Yu 	i_ext->blk = cpu_to_le32(ext->blk);
83439a53e0cSJaegeuk Kim 	i_ext->len = cpu_to_le32(ext->len);
83539a53e0cSJaegeuk Kim }
83639a53e0cSJaegeuk Kim 
837429511cdSChao Yu static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
838429511cdSChao Yu 						u32 blk, unsigned int len)
839429511cdSChao Yu {
840429511cdSChao Yu 	ei->fofs = fofs;
841429511cdSChao Yu 	ei->blk = blk;
842429511cdSChao Yu 	ei->len = len;
84394afd6d6SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
84494afd6d6SChao Yu 	ei->c_len = 0;
84594afd6d6SChao Yu #endif
846429511cdSChao Yu }
847429511cdSChao Yu 
848004b6862SChao Yu static inline bool __is_discard_mergeable(struct discard_info *back,
84935ec7d57SChao Yu 			struct discard_info *front, unsigned int max_len)
850004b6862SChao Yu {
8519a997188SJaegeuk Kim 	return (back->lstart + back->len == front->lstart) &&
85235ec7d57SChao Yu 		(back->len + front->len <= max_len);
853004b6862SChao Yu }
854004b6862SChao Yu 
855004b6862SChao Yu static inline bool __is_discard_back_mergeable(struct discard_info *cur,
85635ec7d57SChao Yu 			struct discard_info *back, unsigned int max_len)
857004b6862SChao Yu {
85835ec7d57SChao Yu 	return __is_discard_mergeable(back, cur, max_len);
859004b6862SChao Yu }
860004b6862SChao Yu 
861004b6862SChao Yu static inline bool __is_discard_front_mergeable(struct discard_info *cur,
86235ec7d57SChao Yu 			struct discard_info *front, unsigned int max_len)
863004b6862SChao Yu {
86435ec7d57SChao Yu 	return __is_discard_mergeable(cur, front, max_len);
865004b6862SChao Yu }
866004b6862SChao Yu 
867429511cdSChao Yu static inline bool __is_extent_mergeable(struct extent_info *back,
868429511cdSChao Yu 						struct extent_info *front)
869429511cdSChao Yu {
87094afd6d6SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
87194afd6d6SChao Yu 	if (back->c_len && back->len != back->c_len)
87294afd6d6SChao Yu 		return false;
87394afd6d6SChao Yu 	if (front->c_len && front->len != front->c_len)
87494afd6d6SChao Yu 		return false;
87594afd6d6SChao Yu #endif
876429511cdSChao Yu 	return (back->fofs + back->len == front->fofs &&
877429511cdSChao Yu 			back->blk + back->len == front->blk);
878429511cdSChao Yu }
879429511cdSChao Yu 
880429511cdSChao Yu static inline bool __is_back_mergeable(struct extent_info *cur,
881429511cdSChao Yu 						struct extent_info *back)
882429511cdSChao Yu {
883429511cdSChao Yu 	return __is_extent_mergeable(back, cur);
884429511cdSChao Yu }
885429511cdSChao Yu 
886429511cdSChao Yu static inline bool __is_front_mergeable(struct extent_info *cur,
887429511cdSChao Yu 						struct extent_info *front)
888429511cdSChao Yu {
889429511cdSChao Yu 	return __is_extent_mergeable(cur, front);
890429511cdSChao Yu }
891429511cdSChao Yu 
892cac5a3d8SDongOh Shin extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
893b430f726SZhikang Zhang static inline void __try_update_largest_extent(struct extent_tree *et,
894b430f726SZhikang Zhang 						struct extent_node *en)
8954abd3f5aSChao Yu {
896205b9822SJaegeuk Kim 	if (en->ei.len > et->largest.len) {
8974abd3f5aSChao Yu 		et->largest = en->ei;
898b430f726SZhikang Zhang 		et->largest_updated = true;
899205b9822SJaegeuk Kim 	}
9004abd3f5aSChao Yu }
9014abd3f5aSChao Yu 
9029a4ffdf5SChao Yu /*
9039a4ffdf5SChao Yu  * For free nid management
9049a4ffdf5SChao Yu  */
9059a4ffdf5SChao Yu enum nid_state {
9069a4ffdf5SChao Yu 	FREE_NID,		/* newly added to free nid list */
9079a4ffdf5SChao Yu 	PREALLOC_NID,		/* it is preallocated */
9089a4ffdf5SChao Yu 	MAX_NID_STATE,
909b8559dc2SChao Yu };
910b8559dc2SChao Yu 
911a95ba66aSJaegeuk Kim enum nat_state {
912a95ba66aSJaegeuk Kim 	TOTAL_NAT,
913a95ba66aSJaegeuk Kim 	DIRTY_NAT,
914a95ba66aSJaegeuk Kim 	RECLAIMABLE_NAT,
915a95ba66aSJaegeuk Kim 	MAX_NAT_STATE,
916a95ba66aSJaegeuk Kim };
917a95ba66aSJaegeuk Kim 
91839a53e0cSJaegeuk Kim struct f2fs_nm_info {
91939a53e0cSJaegeuk Kim 	block_t nat_blkaddr;		/* base disk address of NAT */
92039a53e0cSJaegeuk Kim 	nid_t max_nid;			/* maximum possible node ids */
92104d47e67SChao Yu 	nid_t available_nids;		/* # of available node ids */
92239a53e0cSJaegeuk Kim 	nid_t next_scan_nid;		/* the next nid to be scanned */
92347c8ebccSJaegeuk Kim 	nid_t max_rf_node_blocks;	/* max # of nodes for recovery */
924cdfc41c1SJaegeuk Kim 	unsigned int ram_thresh;	/* control the memory footprint */
925ea1a29a0SChao Yu 	unsigned int ra_nid_pages;	/* # of nid pages to be readaheaded */
9262304cb0cSChao Yu 	unsigned int dirty_nats_ratio;	/* control dirty nats ratio threshold */
92739a53e0cSJaegeuk Kim 
92839a53e0cSJaegeuk Kim 	/* NAT cache management */
92939a53e0cSJaegeuk Kim 	struct radix_tree_root nat_root;/* root of the nat entry cache */
930309cc2b6SJaegeuk Kim 	struct radix_tree_root nat_set_root;/* root of the nat set cache */
931e4544b63STim Murray 	struct f2fs_rwsem nat_tree_lock;	/* protect nat entry tree */
93239a53e0cSJaegeuk Kim 	struct list_head nat_entries;	/* cached nat entry list (clean) */
93322969158SChao Yu 	spinlock_t nat_list_lock;	/* protect clean nat entry list */
934a95ba66aSJaegeuk Kim 	unsigned int nat_cnt[MAX_NAT_STATE]; /* the # of cached nat entries */
93522ad0b6aSJaegeuk Kim 	unsigned int nat_blocks;	/* # of nat blocks */
93639a53e0cSJaegeuk Kim 
93739a53e0cSJaegeuk Kim 	/* free node ids management */
9388a7ed66aSJaegeuk Kim 	struct radix_tree_root free_nid_root;/* root of the free_nid cache */
9399a4ffdf5SChao Yu 	struct list_head free_nid_list;		/* list for free nids excluding preallocated nids */
9409a4ffdf5SChao Yu 	unsigned int nid_cnt[MAX_NID_STATE];	/* the number of free node id */
941b8559dc2SChao Yu 	spinlock_t nid_list_lock;	/* protect nid lists ops */
94239a53e0cSJaegeuk Kim 	struct mutex build_lock;	/* lock for build free nids */
943bb1105e4SJaegeuk Kim 	unsigned char **free_nid_bitmap;
9444ac91242SChao Yu 	unsigned char *nat_block_bitmap;
945586d1492SChao Yu 	unsigned short *free_nid_count;	/* free nid count of NAT block */
94639a53e0cSJaegeuk Kim 
94739a53e0cSJaegeuk Kim 	/* for checkpoint */
94839a53e0cSJaegeuk Kim 	char *nat_bitmap;		/* NAT bitmap pointer */
94922ad0b6aSJaegeuk Kim 
95022ad0b6aSJaegeuk Kim 	unsigned int nat_bits_blocks;	/* # of nat bits blocks */
95122ad0b6aSJaegeuk Kim 	unsigned char *nat_bits;	/* NAT bits blocks */
95222ad0b6aSJaegeuk Kim 	unsigned char *full_nat_bits;	/* full NAT pages */
95322ad0b6aSJaegeuk Kim 	unsigned char *empty_nat_bits;	/* empty NAT pages */
954599a09b2SChao Yu #ifdef CONFIG_F2FS_CHECK_FS
955599a09b2SChao Yu 	char *nat_bitmap_mir;		/* NAT bitmap mirror */
956599a09b2SChao Yu #endif
95739a53e0cSJaegeuk Kim 	int bitmap_size;		/* bitmap size */
95839a53e0cSJaegeuk Kim };
95939a53e0cSJaegeuk Kim 
96039a53e0cSJaegeuk Kim /*
96139a53e0cSJaegeuk Kim  * this structure is used as one of function parameters.
96239a53e0cSJaegeuk Kim  * all the information are dedicated to a given direct node block determined
96339a53e0cSJaegeuk Kim  * by the data offset in a file.
96439a53e0cSJaegeuk Kim  */
96539a53e0cSJaegeuk Kim struct dnode_of_data {
96639a53e0cSJaegeuk Kim 	struct inode *inode;		/* vfs inode pointer */
96739a53e0cSJaegeuk Kim 	struct page *inode_page;	/* its inode page, NULL is possible */
96839a53e0cSJaegeuk Kim 	struct page *node_page;		/* cached direct node page */
96939a53e0cSJaegeuk Kim 	nid_t nid;			/* node id of the direct node block */
97039a53e0cSJaegeuk Kim 	unsigned int ofs_in_node;	/* data offset in the node page */
97139a53e0cSJaegeuk Kim 	bool inode_page_locked;		/* inode page is locked or not */
97293bae099SJaegeuk Kim 	bool node_changed;		/* is node block changed */
9733cf45747SChao Yu 	char cur_level;			/* level of hole node page */
9743cf45747SChao Yu 	char max_level;			/* level of current page located */
97539a53e0cSJaegeuk Kim 	block_t	data_blkaddr;		/* block address of the node block */
97639a53e0cSJaegeuk Kim };
97739a53e0cSJaegeuk Kim 
97839a53e0cSJaegeuk Kim static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
97939a53e0cSJaegeuk Kim 		struct page *ipage, struct page *npage, nid_t nid)
98039a53e0cSJaegeuk Kim {
981d66d1f76SJaegeuk Kim 	memset(dn, 0, sizeof(*dn));
98239a53e0cSJaegeuk Kim 	dn->inode = inode;
98339a53e0cSJaegeuk Kim 	dn->inode_page = ipage;
98439a53e0cSJaegeuk Kim 	dn->node_page = npage;
98539a53e0cSJaegeuk Kim 	dn->nid = nid;
98639a53e0cSJaegeuk Kim }
98739a53e0cSJaegeuk Kim 
98839a53e0cSJaegeuk Kim /*
98939a53e0cSJaegeuk Kim  * For SIT manager
99039a53e0cSJaegeuk Kim  *
99139a53e0cSJaegeuk Kim  * By default, there are 6 active log areas across the whole main area.
99239a53e0cSJaegeuk Kim  * When considering hot and cold data separation to reduce cleaning overhead,
99339a53e0cSJaegeuk Kim  * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
99439a53e0cSJaegeuk Kim  * respectively.
99539a53e0cSJaegeuk Kim  * In the current design, you should not change the numbers intentionally.
99639a53e0cSJaegeuk Kim  * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
99739a53e0cSJaegeuk Kim  * logs individually according to the underlying devices. (default: 6)
99839a53e0cSJaegeuk Kim  * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
99939a53e0cSJaegeuk Kim  * data and 8 for node logs.
100039a53e0cSJaegeuk Kim  */
100139a53e0cSJaegeuk Kim #define	NR_CURSEG_DATA_TYPE	(3)
100239a53e0cSJaegeuk Kim #define NR_CURSEG_NODE_TYPE	(3)
1003093749e2SChao Yu #define NR_CURSEG_INMEM_TYPE	(2)
1004a7d9fe3cSJaegeuk Kim #define NR_CURSEG_RO_TYPE	(2)
1005d0b9e42aSChao Yu #define NR_CURSEG_PERSIST_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
1006d0b9e42aSChao Yu #define NR_CURSEG_TYPE		(NR_CURSEG_INMEM_TYPE + NR_CURSEG_PERSIST_TYPE)
100739a53e0cSJaegeuk Kim 
100839a53e0cSJaegeuk Kim enum {
100939a53e0cSJaegeuk Kim 	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
101039a53e0cSJaegeuk Kim 	CURSEG_WARM_DATA,	/* data blocks */
101139a53e0cSJaegeuk Kim 	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
101239a53e0cSJaegeuk Kim 	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
101339a53e0cSJaegeuk Kim 	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
101439a53e0cSJaegeuk Kim 	CURSEG_COLD_NODE,	/* indirect node blocks */
1015d0b9e42aSChao Yu 	NR_PERSISTENT_LOG,	/* number of persistent log */
1016d0b9e42aSChao Yu 	CURSEG_COLD_DATA_PINNED = NR_PERSISTENT_LOG,
1017d0b9e42aSChao Yu 				/* pinned file that needs consecutive block address */
1018093749e2SChao Yu 	CURSEG_ALL_DATA_ATGC,	/* SSR alloctor in hot/warm/cold data area */
1019d0b9e42aSChao Yu 	NO_CHECK_TYPE,		/* number of persistent & inmem log */
102039a53e0cSJaegeuk Kim };
102139a53e0cSJaegeuk Kim 
10226b4afdd7SJaegeuk Kim struct flush_cmd {
10236b4afdd7SJaegeuk Kim 	struct completion wait;
1024721bd4d5SGu Zheng 	struct llist_node llnode;
102539d787beSChao Yu 	nid_t ino;
10266b4afdd7SJaegeuk Kim 	int ret;
10276b4afdd7SJaegeuk Kim };
10286b4afdd7SJaegeuk Kim 
1029a688b9d9SGu Zheng struct flush_cmd_control {
1030a688b9d9SGu Zheng 	struct task_struct *f2fs_issue_flush;	/* flush thread */
1031a688b9d9SGu Zheng 	wait_queue_head_t flush_wait_queue;	/* waiting queue for wake-up */
10328b8dd65fSChao Yu 	atomic_t issued_flush;			/* # of issued flushes */
103372691af6SJaegeuk Kim 	atomic_t queued_flush;			/* # of queued flushes */
1034721bd4d5SGu Zheng 	struct llist_head issue_list;		/* list for command issue */
1035721bd4d5SGu Zheng 	struct llist_node *dispatch_list;	/* list for command dispatch */
1036a688b9d9SGu Zheng };
1037a688b9d9SGu Zheng 
103839a53e0cSJaegeuk Kim struct f2fs_sm_info {
103939a53e0cSJaegeuk Kim 	struct sit_info *sit_info;		/* whole segment information */
104039a53e0cSJaegeuk Kim 	struct free_segmap_info *free_info;	/* free segment information */
104139a53e0cSJaegeuk Kim 	struct dirty_seglist_info *dirty_info;	/* dirty segment information */
104239a53e0cSJaegeuk Kim 	struct curseg_info *curseg_array;	/* active segment information */
104339a53e0cSJaegeuk Kim 
1044e4544b63STim Murray 	struct f2fs_rwsem curseg_lock;	/* for preventing curseg change */
10452b60311dSChao Yu 
104639a53e0cSJaegeuk Kim 	block_t seg0_blkaddr;		/* block address of 0'th segment */
104739a53e0cSJaegeuk Kim 	block_t main_blkaddr;		/* start block address of main area */
104839a53e0cSJaegeuk Kim 	block_t ssa_blkaddr;		/* start block address of SSA area */
104939a53e0cSJaegeuk Kim 
105039a53e0cSJaegeuk Kim 	unsigned int segment_count;	/* total # of segments */
105139a53e0cSJaegeuk Kim 	unsigned int main_segments;	/* # of segments in main area */
105239a53e0cSJaegeuk Kim 	unsigned int reserved_segments;	/* # of reserved segments */
1053300a8429SChao Yu 	unsigned int additional_reserved_segments;/* reserved segs for IO align feature */
105439a53e0cSJaegeuk Kim 	unsigned int ovp_segments;	/* # of overprovision segments */
105581eb8d6eSJaegeuk Kim 
105681eb8d6eSJaegeuk Kim 	/* a threshold to reclaim prefree segments */
105781eb8d6eSJaegeuk Kim 	unsigned int rec_prefree_segments;
10587fd9e544SJaegeuk Kim 
1059bba681cbSJaegeuk Kim 	/* for batched trimming */
1060bba681cbSJaegeuk Kim 	unsigned int trim_sections;		/* # of sections to trim */
1061bba681cbSJaegeuk Kim 
1062184a5cd2SChao Yu 	struct list_head sit_entry_set;	/* sit entry set list */
1063184a5cd2SChao Yu 
1064216fbd64SJaegeuk Kim 	unsigned int ipu_policy;	/* in-place-update policy */
1065216fbd64SJaegeuk Kim 	unsigned int min_ipu_util;	/* in-place-update threshold */
1066c1ce1b02SJaegeuk Kim 	unsigned int min_fsync_blocks;	/* threshold for fsync */
1067853137ceSJaegeuk Kim 	unsigned int min_seq_blocks;	/* threshold for sequential blocks */
1068ef095d19SJaegeuk Kim 	unsigned int min_hot_blocks;	/* threshold for hot block allocation */
1069a2a12b67SChao Yu 	unsigned int min_ssr_sections;	/* threshold to trigger SSR allocation */
10706b4afdd7SJaegeuk Kim 
10716b4afdd7SJaegeuk Kim 	/* for flush command control */
1072b01a9201SJaegeuk Kim 	struct flush_cmd_control *fcc_info;
1073a688b9d9SGu Zheng 
10740b54fb84SJaegeuk Kim 	/* for discard command control */
10750b54fb84SJaegeuk Kim 	struct discard_cmd_control *dcc_info;
107639a53e0cSJaegeuk Kim };
107739a53e0cSJaegeuk Kim 
107839a53e0cSJaegeuk Kim /*
107939a53e0cSJaegeuk Kim  * For superblock
108039a53e0cSJaegeuk Kim  */
108139a53e0cSJaegeuk Kim /*
108239a53e0cSJaegeuk Kim  * COUNT_TYPE for monitoring
108339a53e0cSJaegeuk Kim  *
108439a53e0cSJaegeuk Kim  * f2fs monitors the number of several block types such as on-writeback,
108539a53e0cSJaegeuk Kim  * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
108639a53e0cSJaegeuk Kim  */
108736951b38SChao Yu #define WB_DATA_TYPE(p)	(__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
108839a53e0cSJaegeuk Kim enum count_type {
108939a53e0cSJaegeuk Kim 	F2FS_DIRTY_DENTS,
1090c227f912SChao Yu 	F2FS_DIRTY_DATA,
10912c8a4a28SJaegeuk Kim 	F2FS_DIRTY_QDATA,
109239a53e0cSJaegeuk Kim 	F2FS_DIRTY_NODES,
109339a53e0cSJaegeuk Kim 	F2FS_DIRTY_META,
10948dcf2ff7SJaegeuk Kim 	F2FS_INMEM_PAGES,
10950f18b462SJaegeuk Kim 	F2FS_DIRTY_IMETA,
109636951b38SChao Yu 	F2FS_WB_CP_DATA,
109736951b38SChao Yu 	F2FS_WB_DATA,
10985f9abab4SJaegeuk Kim 	F2FS_RD_DATA,
10995f9abab4SJaegeuk Kim 	F2FS_RD_NODE,
11005f9abab4SJaegeuk Kim 	F2FS_RD_META,
110102b16d0aSChao Yu 	F2FS_DIO_WRITE,
110202b16d0aSChao Yu 	F2FS_DIO_READ,
110339a53e0cSJaegeuk Kim 	NR_COUNT_TYPE,
110439a53e0cSJaegeuk Kim };
110539a53e0cSJaegeuk Kim 
110639a53e0cSJaegeuk Kim /*
1107e1c42045Sarter97  * The below are the page types of bios used in submit_bio().
110839a53e0cSJaegeuk Kim  * The available types are:
110939a53e0cSJaegeuk Kim  * DATA			User data pages. It operates as async mode.
111039a53e0cSJaegeuk Kim  * NODE			Node pages. It operates as async mode.
111139a53e0cSJaegeuk Kim  * META			FS metadata pages such as SIT, NAT, CP.
111239a53e0cSJaegeuk Kim  * NR_PAGE_TYPE		The number of page types.
111339a53e0cSJaegeuk Kim  * META_FLUSH		Make sure the previous pages are written
111439a53e0cSJaegeuk Kim  *			with waiting the bio's completion
111539a53e0cSJaegeuk Kim  * ...			Only can be used with META.
111639a53e0cSJaegeuk Kim  */
11177d5e5109SJaegeuk Kim #define PAGE_TYPE_OF_BIO(type)	((type) > META ? META : (type))
111839a53e0cSJaegeuk Kim enum page_type {
111939a53e0cSJaegeuk Kim 	DATA,
112039a53e0cSJaegeuk Kim 	NODE,
112139a53e0cSJaegeuk Kim 	META,
112239a53e0cSJaegeuk Kim 	NR_PAGE_TYPE,
112339a53e0cSJaegeuk Kim 	META_FLUSH,
11248ce67cb0SJaegeuk Kim 	INMEM,		/* the below types are used by tracepoints only. */
11258ce67cb0SJaegeuk Kim 	INMEM_DROP,
11268c242db9SJaegeuk Kim 	INMEM_INVALIDATE,
112728bc106bSChao Yu 	INMEM_REVOKE,
11288ce67cb0SJaegeuk Kim 	IPU,
11298ce67cb0SJaegeuk Kim 	OPU,
113039a53e0cSJaegeuk Kim };
113139a53e0cSJaegeuk Kim 
1132a912b54dSJaegeuk Kim enum temp_type {
1133a912b54dSJaegeuk Kim 	HOT = 0,	/* must be zero for meta bio */
1134a912b54dSJaegeuk Kim 	WARM,
1135a912b54dSJaegeuk Kim 	COLD,
1136a912b54dSJaegeuk Kim 	NR_TEMP_TYPE,
1137a912b54dSJaegeuk Kim };
1138a912b54dSJaegeuk Kim 
1139cc15620bSJaegeuk Kim enum need_lock_type {
1140cc15620bSJaegeuk Kim 	LOCK_REQ = 0,
1141cc15620bSJaegeuk Kim 	LOCK_DONE,
1142cc15620bSJaegeuk Kim 	LOCK_RETRY,
1143cc15620bSJaegeuk Kim };
1144cc15620bSJaegeuk Kim 
1145a5fd5050SChao Yu enum cp_reason_type {
1146a5fd5050SChao Yu 	CP_NO_NEEDED,
1147a5fd5050SChao Yu 	CP_NON_REGULAR,
11484c8ff709SChao Yu 	CP_COMPRESSED,
1149a5fd5050SChao Yu 	CP_HARDLINK,
1150a5fd5050SChao Yu 	CP_SB_NEED_CP,
1151a5fd5050SChao Yu 	CP_WRONG_PINO,
1152a5fd5050SChao Yu 	CP_NO_SPC_ROLL,
1153a5fd5050SChao Yu 	CP_NODE_NEED_CP,
1154a5fd5050SChao Yu 	CP_FASTBOOT_MODE,
1155a5fd5050SChao Yu 	CP_SPEC_LOG_NUM,
11560a007b97SJaegeuk Kim 	CP_RECOVER_DIR,
1157a5fd5050SChao Yu };
1158a5fd5050SChao Yu 
1159b0af6d49SChao Yu enum iostat_type {
11608b83ac81SChao Yu 	/* WRITE IO */
11618b83ac81SChao Yu 	APP_DIRECT_IO,			/* app direct write IOs */
11628b83ac81SChao Yu 	APP_BUFFERED_IO,		/* app buffered write IOs */
1163b0af6d49SChao Yu 	APP_WRITE_IO,			/* app write IOs */
1164b0af6d49SChao Yu 	APP_MAPPED_IO,			/* app mapped IOs */
1165b0af6d49SChao Yu 	FS_DATA_IO,			/* data IOs from kworker/fsync/reclaimer */
1166b0af6d49SChao Yu 	FS_NODE_IO,			/* node IOs from kworker/fsync/reclaimer */
1167b0af6d49SChao Yu 	FS_META_IO,			/* meta IOs from kworker/reclaimer */
1168b0af6d49SChao Yu 	FS_GC_DATA_IO,			/* data IOs from forground gc */
1169b0af6d49SChao Yu 	FS_GC_NODE_IO,			/* node IOs from forground gc */
1170b0af6d49SChao Yu 	FS_CP_DATA_IO,			/* data IOs from checkpoint */
1171b0af6d49SChao Yu 	FS_CP_NODE_IO,			/* node IOs from checkpoint */
1172b0af6d49SChao Yu 	FS_CP_META_IO,			/* meta IOs from checkpoint */
11738b83ac81SChao Yu 
11748b83ac81SChao Yu 	/* READ IO */
11758b83ac81SChao Yu 	APP_DIRECT_READ_IO,		/* app direct read IOs */
11768b83ac81SChao Yu 	APP_BUFFERED_READ_IO,		/* app buffered read IOs */
11778b83ac81SChao Yu 	APP_READ_IO,			/* app read IOs */
11788b83ac81SChao Yu 	APP_MAPPED_READ_IO,		/* app mapped read IOs */
11798b83ac81SChao Yu 	FS_DATA_READ_IO,		/* data read IOs */
11809c122384SChao Yu 	FS_GDATA_READ_IO,		/* data read IOs from background gc */
11819c122384SChao Yu 	FS_CDATA_READ_IO,		/* compressed data read IOs */
11828b83ac81SChao Yu 	FS_NODE_READ_IO,		/* node read IOs */
11838b83ac81SChao Yu 	FS_META_READ_IO,		/* meta read IOs */
11848b83ac81SChao Yu 
11858b83ac81SChao Yu 	/* other */
1186b0af6d49SChao Yu 	FS_DISCARD,			/* discard */
1187b0af6d49SChao Yu 	NR_IO_TYPE,
1188b0af6d49SChao Yu };
1189b0af6d49SChao Yu 
1190458e6197SJaegeuk Kim struct f2fs_io_info {
119105ca3632SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs_sb_info pointer */
119239d787beSChao Yu 	nid_t ino;		/* inode number */
1193458e6197SJaegeuk Kim 	enum page_type type;	/* contains DATA/NODE/META/META_FLUSH */
1194a912b54dSJaegeuk Kim 	enum temp_type temp;	/* contains HOT/WARM/COLD */
119504d328deSMike Christie 	int op;			/* contains REQ_OP_ */
1196ef295ecfSChristoph Hellwig 	int op_flags;		/* req_flag_bits */
11977a9d7548SChao Yu 	block_t new_blkaddr;	/* new block address to be written */
119828bc106bSChao Yu 	block_t old_blkaddr;	/* old block address before Cow */
119905ca3632SJaegeuk Kim 	struct page *page;	/* page to be written */
12004375a336SJaegeuk Kim 	struct page *encrypted_page;	/* encrypted page */
12014c8ff709SChao Yu 	struct page *compressed_page;	/* compressed page */
1202fb830fc5SChao Yu 	struct list_head list;		/* serialize IOs */
1203d68f735bSJaegeuk Kim 	bool submitted;		/* indicate IO submission */
1204cc15620bSJaegeuk Kim 	int need_lock;		/* indicate we need to lock cp_rwsem */
1205fb830fc5SChao Yu 	bool in_list;		/* indicate fio is in io_list */
12066dc3a126SChao Yu 	bool is_por;		/* indicate IO is from recovery or not */
1207fe16efe6SChao Yu 	bool retry;		/* need to reallocate block address */
12084c8ff709SChao Yu 	int compr_blocks;	/* # of compressed block addresses */
12094c8ff709SChao Yu 	bool encrypted;		/* indicate file is encrypted */
1210b0af6d49SChao Yu 	enum iostat_type io_type;	/* io type */
1211578c6478SYufen Yu 	struct writeback_control *io_wbc; /* writeback control */
12128648de2cSChao Yu 	struct bio **bio;		/* bio for ipu */
12138648de2cSChao Yu 	sector_t *last_block;		/* last block number in bio */
12147735730dSChao Yu 	unsigned char version;		/* version of the node */
1215458e6197SJaegeuk Kim };
1216458e6197SJaegeuk Kim 
12170b20fcecSChao Yu struct bio_entry {
12180b20fcecSChao Yu 	struct bio *bio;
12190b20fcecSChao Yu 	struct list_head list;
12200b20fcecSChao Yu };
12210b20fcecSChao Yu 
122268afcf2dSTomohiro Kusumi #define is_read_io(rw) ((rw) == READ)
12231ff7bd3bSJaegeuk Kim struct f2fs_bio_info {
1224458e6197SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs superblock */
12251ff7bd3bSJaegeuk Kim 	struct bio *bio;		/* bios to merge */
12261ff7bd3bSJaegeuk Kim 	sector_t last_block_in_bio;	/* last block number */
1227458e6197SJaegeuk Kim 	struct f2fs_io_info fio;	/* store buffered io info. */
1228e4544b63STim Murray 	struct f2fs_rwsem io_rwsem;	/* blocking op for bio */
1229fb830fc5SChao Yu 	spinlock_t io_lock;		/* serialize DATA/NODE IOs */
1230fb830fc5SChao Yu 	struct list_head io_list;	/* track fios */
12310b20fcecSChao Yu 	struct list_head bio_list;	/* bio entry list head */
1232e4544b63STim Murray 	struct f2fs_rwsem bio_list_lock;	/* lock to protect bio entry list */
12331ff7bd3bSJaegeuk Kim };
12341ff7bd3bSJaegeuk Kim 
12353c62be17SJaegeuk Kim #define FDEV(i)				(sbi->devs[i])
12363c62be17SJaegeuk Kim #define RDEV(i)				(raw_super->devs[i])
12373c62be17SJaegeuk Kim struct f2fs_dev_info {
12383c62be17SJaegeuk Kim 	struct block_device *bdev;
12393c62be17SJaegeuk Kim 	char path[MAX_PATH_LEN];
12403c62be17SJaegeuk Kim 	unsigned int total_segments;
12413c62be17SJaegeuk Kim 	block_t start_blk;
12423c62be17SJaegeuk Kim 	block_t end_blk;
12433c62be17SJaegeuk Kim #ifdef CONFIG_BLK_DEV_ZONED
12443c62be17SJaegeuk Kim 	unsigned int nr_blkz;		/* Total number of zones */
124595175dafSDamien Le Moal 	unsigned long *blkz_seq;	/* Bitmap indicating sequential zones */
1246de881df9SAravind Ramesh 	block_t *zone_capacity_blocks;  /* Array of zone capacity in blks */
12473c62be17SJaegeuk Kim #endif
12483c62be17SJaegeuk Kim };
12493c62be17SJaegeuk Kim 
1250c227f912SChao Yu enum inode_type {
1251c227f912SChao Yu 	DIR_INODE,			/* for dirty dir inode */
1252c227f912SChao Yu 	FILE_INODE,			/* for dirty regular/symlink inode */
12530f18b462SJaegeuk Kim 	DIRTY_META,			/* for all dirtied inode metadata */
125457864ae5SJaegeuk Kim 	ATOMIC_FILE,			/* for all atomic files */
1255c227f912SChao Yu 	NR_INODE_TYPE,
1256c227f912SChao Yu };
1257c227f912SChao Yu 
125867298804SChao Yu /* for inner inode cache management */
125967298804SChao Yu struct inode_management {
126067298804SChao Yu 	struct radix_tree_root ino_root;	/* ino entry array */
126167298804SChao Yu 	spinlock_t ino_lock;			/* for ino entry lock */
126267298804SChao Yu 	struct list_head ino_list;		/* inode list head */
126367298804SChao Yu 	unsigned long ino_num;			/* number of entries */
126467298804SChao Yu };
126567298804SChao Yu 
1266093749e2SChao Yu /* for GC_AT */
1267093749e2SChao Yu struct atgc_management {
1268093749e2SChao Yu 	bool atgc_enabled;			/* ATGC is enabled or not */
1269093749e2SChao Yu 	struct rb_root_cached root;		/* root of victim rb-tree */
1270093749e2SChao Yu 	struct list_head victim_list;		/* linked with all victim entries */
1271093749e2SChao Yu 	unsigned int victim_count;		/* victim count in rb-tree */
1272093749e2SChao Yu 	unsigned int candidate_ratio;		/* candidate ratio */
1273093749e2SChao Yu 	unsigned int max_candidate_count;	/* max candidate count */
1274093749e2SChao Yu 	unsigned int age_weight;		/* age weight, vblock_weight = 100 - age_weight */
1275093749e2SChao Yu 	unsigned long long age_threshold;	/* age threshold */
1276093749e2SChao Yu };
1277093749e2SChao Yu 
1278caf0047eSChao Yu /* For s_flag in struct f2fs_sb_info */
1279caf0047eSChao Yu enum {
1280caf0047eSChao Yu 	SBI_IS_DIRTY,				/* dirty flag for checkpoint */
1281caf0047eSChao Yu 	SBI_IS_CLOSE,				/* specify unmounting */
1282caf0047eSChao Yu 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
1283caf0047eSChao Yu 	SBI_POR_DOING,				/* recovery is doing or not */
1284df728b0fSJaegeuk Kim 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
1285bbf156f7SJaegeuk Kim 	SBI_NEED_CP,				/* need to checkpoint */
128683a3bfdbSJaegeuk Kim 	SBI_IS_SHUTDOWN,			/* shutdown by ioctl */
12871378752bSChao Yu 	SBI_IS_RECOVERED,			/* recovered orphan/data */
12884354994fSDaniel Rosenberg 	SBI_CP_DISABLED,			/* CP was disabled last mount */
1289db610a64SJaegeuk Kim 	SBI_CP_DISABLED_QUICK,			/* CP was disabled quickly */
1290af033b2aSChao Yu 	SBI_QUOTA_NEED_FLUSH,			/* need to flush quota info in CP */
1291af033b2aSChao Yu 	SBI_QUOTA_SKIP_FLUSH,			/* skip flushing quota in current CP */
1292af033b2aSChao Yu 	SBI_QUOTA_NEED_REPAIR,			/* quota file may be corrupted */
129304f0b2eaSQiuyang Sun 	SBI_IS_RESIZEFS,			/* resizefs is in process */
1294caf0047eSChao Yu };
1295caf0047eSChao Yu 
12966beceb54SJaegeuk Kim enum {
12976beceb54SJaegeuk Kim 	CP_TIME,
1298d0239e1bSJaegeuk Kim 	REQ_TIME,
1299a7d10cf3SSahitya Tummala 	DISCARD_TIME,
1300a7d10cf3SSahitya Tummala 	GC_TIME,
13014354994fSDaniel Rosenberg 	DISABLE_TIME,
130203f2c02dSJaegeuk Kim 	UMOUNT_DISCARD_TIMEOUT,
13036beceb54SJaegeuk Kim 	MAX_TIME,
13046beceb54SJaegeuk Kim };
13056beceb54SJaegeuk Kim 
13060cdd3195SHyunchul Lee enum {
13075b0e9539SJaegeuk Kim 	GC_NORMAL,
13085b0e9539SJaegeuk Kim 	GC_IDLE_CB,
13095b0e9539SJaegeuk Kim 	GC_IDLE_GREEDY,
1310093749e2SChao Yu 	GC_IDLE_AT,
13110e5e8111SDaeho Jeong 	GC_URGENT_HIGH,
13120e5e8111SDaeho Jeong 	GC_URGENT_LOW,
131307c6b593SDaeho Jeong 	MAX_GC_MODE,
13145b0e9539SJaegeuk Kim };
13155b0e9539SJaegeuk Kim 
13165b0e9539SJaegeuk Kim enum {
1317bbbc34fdSChao Yu 	BGGC_MODE_ON,		/* background gc is on */
1318bbbc34fdSChao Yu 	BGGC_MODE_OFF,		/* background gc is off */
1319bbbc34fdSChao Yu 	BGGC_MODE_SYNC,		/*
1320bbbc34fdSChao Yu 				 * background gc is on, migrating blocks
1321bbbc34fdSChao Yu 				 * like foreground gc
1322bbbc34fdSChao Yu 				 */
1323bbbc34fdSChao Yu };
1324bbbc34fdSChao Yu 
1325bbbc34fdSChao Yu enum {
1326b0332a0fSChao Yu 	FS_MODE_ADAPTIVE,		/* use both lfs/ssr allocation */
1327b0332a0fSChao Yu 	FS_MODE_LFS,			/* use lfs allocation only */
13286691d940SDaeho Jeong 	FS_MODE_FRAGMENT_SEG,		/* segment fragmentation mode */
13296691d940SDaeho Jeong 	FS_MODE_FRAGMENT_BLK,		/* block fragmentation mode */
1330b0332a0fSChao Yu };
1331b0332a0fSChao Yu 
1332b0332a0fSChao Yu enum {
13330cdd3195SHyunchul Lee 	WHINT_MODE_OFF,		/* not pass down write hints */
13340cdd3195SHyunchul Lee 	WHINT_MODE_USER,	/* try to pass down hints given by users */
1335f2e703f9SHyunchul Lee 	WHINT_MODE_FS,		/* pass down hints with F2FS policy */
13360cdd3195SHyunchul Lee };
13370cdd3195SHyunchul Lee 
133807939627SJaegeuk Kim enum {
133907939627SJaegeuk Kim 	ALLOC_MODE_DEFAULT,	/* stay default */
134007939627SJaegeuk Kim 	ALLOC_MODE_REUSE,	/* reuse segments as much as possible */
134107939627SJaegeuk Kim };
134207939627SJaegeuk Kim 
134393cf93f1SJunling Zheng enum fsync_mode {
134493cf93f1SJunling Zheng 	FSYNC_MODE_POSIX,	/* fsync follows posix semantics */
134593cf93f1SJunling Zheng 	FSYNC_MODE_STRICT,	/* fsync behaves in line with ext4 */
1346d6290814SJaegeuk Kim 	FSYNC_MODE_NOBARRIER,	/* fsync behaves nobarrier based on posix */
134793cf93f1SJunling Zheng };
134893cf93f1SJunling Zheng 
1349602a16d5SDaeho Jeong enum {
1350602a16d5SDaeho Jeong 	COMPR_MODE_FS,		/*
1351602a16d5SDaeho Jeong 				 * automatically compress compression
1352602a16d5SDaeho Jeong 				 * enabled files
1353602a16d5SDaeho Jeong 				 */
1354602a16d5SDaeho Jeong 	COMPR_MODE_USER,	/*
1355602a16d5SDaeho Jeong 				 * automatical compression is disabled.
1356602a16d5SDaeho Jeong 				 * user can control the file compression
1357602a16d5SDaeho Jeong 				 * using ioctls
1358602a16d5SDaeho Jeong 				 */
1359602a16d5SDaeho Jeong };
1360602a16d5SDaeho Jeong 
13614f993264SChao Yu enum {
13624f993264SChao Yu 	DISCARD_UNIT_BLOCK,	/* basic discard unit is block */
13634f993264SChao Yu 	DISCARD_UNIT_SEGMENT,	/* basic discard unit is segment */
13644f993264SChao Yu 	DISCARD_UNIT_SECTION,	/* basic discard unit is section */
13654f993264SChao Yu };
13664f993264SChao Yu 
1367b763f3beSChao Yu static inline int f2fs_test_bit(unsigned int nr, char *addr);
1368b763f3beSChao Yu static inline void f2fs_set_bit(unsigned int nr, char *addr);
1369b763f3beSChao Yu static inline void f2fs_clear_bit(unsigned int nr, char *addr);
13704c8ff709SChao Yu 
1371b763f3beSChao Yu /*
1372b763f3beSChao Yu  * Layout of f2fs page.private:
1373b763f3beSChao Yu  *
1374b763f3beSChao Yu  * Layout A: lowest bit should be 1
1375b763f3beSChao Yu  * | bit0 = 1 | bit1 | bit2 | ... | bit MAX | private data .... |
1376b763f3beSChao Yu  * bit 0	PAGE_PRIVATE_NOT_POINTER
1377b763f3beSChao Yu  * bit 1	PAGE_PRIVATE_ATOMIC_WRITE
1378b763f3beSChao Yu  * bit 2	PAGE_PRIVATE_DUMMY_WRITE
1379b763f3beSChao Yu  * bit 3	PAGE_PRIVATE_ONGOING_MIGRATION
1380b763f3beSChao Yu  * bit 4	PAGE_PRIVATE_INLINE_INODE
1381b763f3beSChao Yu  * bit 5	PAGE_PRIVATE_REF_RESOURCE
1382b763f3beSChao Yu  * bit 6-	f2fs private data
1383b763f3beSChao Yu  *
1384b763f3beSChao Yu  * Layout B: lowest bit should be 0
1385b763f3beSChao Yu  * page.private is a wrapped pointer.
1386b763f3beSChao Yu  */
1387b763f3beSChao Yu enum {
1388b763f3beSChao Yu 	PAGE_PRIVATE_NOT_POINTER,		/* private contains non-pointer data */
1389b763f3beSChao Yu 	PAGE_PRIVATE_ATOMIC_WRITE,		/* data page from atomic write path */
1390b763f3beSChao Yu 	PAGE_PRIVATE_DUMMY_WRITE,		/* data page for padding aligned IO */
1391b763f3beSChao Yu 	PAGE_PRIVATE_ONGOING_MIGRATION,		/* data page which is on-going migrating */
1392b763f3beSChao Yu 	PAGE_PRIVATE_INLINE_INODE,		/* inode page contains inline data */
1393b763f3beSChao Yu 	PAGE_PRIVATE_REF_RESOURCE,		/* dirty page has referenced resources */
1394b763f3beSChao Yu 	PAGE_PRIVATE_MAX
1395b763f3beSChao Yu };
1396b763f3beSChao Yu 
1397b763f3beSChao Yu #define PAGE_PRIVATE_GET_FUNC(name, flagname) \
1398b763f3beSChao Yu static inline bool page_private_##name(struct page *page) \
1399b763f3beSChao Yu { \
1400c9ebd3dfSJaegeuk Kim 	return PagePrivate(page) && \
1401c9ebd3dfSJaegeuk Kim 		test_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page)) && \
1402b763f3beSChao Yu 		test_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
1403b763f3beSChao Yu }
1404b763f3beSChao Yu 
1405b763f3beSChao Yu #define PAGE_PRIVATE_SET_FUNC(name, flagname) \
1406b763f3beSChao Yu static inline void set_page_private_##name(struct page *page) \
1407b763f3beSChao Yu { \
1408b763f3beSChao Yu 	if (!PagePrivate(page)) { \
1409b763f3beSChao Yu 		get_page(page); \
1410b763f3beSChao Yu 		SetPagePrivate(page); \
1411c9ebd3dfSJaegeuk Kim 		set_page_private(page, 0); \
1412b763f3beSChao Yu 	} \
1413b763f3beSChao Yu 	set_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page)); \
1414b763f3beSChao Yu 	set_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
1415b763f3beSChao Yu }
1416b763f3beSChao Yu 
1417b763f3beSChao Yu #define PAGE_PRIVATE_CLEAR_FUNC(name, flagname) \
1418b763f3beSChao Yu static inline void clear_page_private_##name(struct page *page) \
1419b763f3beSChao Yu { \
1420b763f3beSChao Yu 	clear_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
1421b763f3beSChao Yu 	if (page_private(page) == 1 << PAGE_PRIVATE_NOT_POINTER) { \
1422b763f3beSChao Yu 		set_page_private(page, 0); \
1423b763f3beSChao Yu 		if (PagePrivate(page)) { \
1424b763f3beSChao Yu 			ClearPagePrivate(page); \
1425b763f3beSChao Yu 			put_page(page); \
1426b763f3beSChao Yu 		}\
1427b763f3beSChao Yu 	} \
1428b763f3beSChao Yu }
1429b763f3beSChao Yu 
1430b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(nonpointer, NOT_POINTER);
1431b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(reference, REF_RESOURCE);
1432b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(inline, INLINE_INODE);
1433b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(gcing, ONGOING_MIGRATION);
1434b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(atomic, ATOMIC_WRITE);
1435b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(dummy, DUMMY_WRITE);
1436b763f3beSChao Yu 
1437b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(reference, REF_RESOURCE);
1438b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(inline, INLINE_INODE);
1439b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(gcing, ONGOING_MIGRATION);
1440b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(atomic, ATOMIC_WRITE);
1441b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(dummy, DUMMY_WRITE);
1442b763f3beSChao Yu 
1443b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(reference, REF_RESOURCE);
1444b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(inline, INLINE_INODE);
1445b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(gcing, ONGOING_MIGRATION);
1446b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(atomic, ATOMIC_WRITE);
1447b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(dummy, DUMMY_WRITE);
14484c8ff709SChao Yu 
14496ce19affSChao Yu static inline unsigned long get_page_private_data(struct page *page)
14506ce19affSChao Yu {
14516ce19affSChao Yu 	unsigned long data = page_private(page);
14526ce19affSChao Yu 
14536ce19affSChao Yu 	if (!test_bit(PAGE_PRIVATE_NOT_POINTER, &data))
14546ce19affSChao Yu 		return 0;
14556ce19affSChao Yu 	return data >> PAGE_PRIVATE_MAX;
14566ce19affSChao Yu }
14576ce19affSChao Yu 
14586ce19affSChao Yu static inline void set_page_private_data(struct page *page, unsigned long data)
14596ce19affSChao Yu {
14606ce19affSChao Yu 	if (!PagePrivate(page)) {
14616ce19affSChao Yu 		get_page(page);
14626ce19affSChao Yu 		SetPagePrivate(page);
1463c9ebd3dfSJaegeuk Kim 		set_page_private(page, 0);
14646ce19affSChao Yu 	}
14656ce19affSChao Yu 	set_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page));
14666ce19affSChao Yu 	page_private(page) |= data << PAGE_PRIVATE_MAX;
14676ce19affSChao Yu }
14686ce19affSChao Yu 
14696ce19affSChao Yu static inline void clear_page_private_data(struct page *page)
14706ce19affSChao Yu {
14716ce19affSChao Yu 	page_private(page) &= (1 << PAGE_PRIVATE_MAX) - 1;
14726ce19affSChao Yu 	if (page_private(page) == 1 << PAGE_PRIVATE_NOT_POINTER) {
14736ce19affSChao Yu 		set_page_private(page, 0);
14746ce19affSChao Yu 		if (PagePrivate(page)) {
14756ce19affSChao Yu 			ClearPagePrivate(page);
14766ce19affSChao Yu 			put_page(page);
14776ce19affSChao Yu 		}
14786ce19affSChao Yu 	}
14796ce19affSChao Yu }
14806ce19affSChao Yu 
14814c8ff709SChao Yu /* For compression */
14824c8ff709SChao Yu enum compress_algorithm_type {
14834c8ff709SChao Yu 	COMPRESS_LZO,
14844c8ff709SChao Yu 	COMPRESS_LZ4,
148550cfa66fSChao Yu 	COMPRESS_ZSTD,
14866d92b201SChao Yu 	COMPRESS_LZORLE,
14874c8ff709SChao Yu 	COMPRESS_MAX,
14884c8ff709SChao Yu };
14894c8ff709SChao Yu 
1490b28f047bSChao Yu enum compress_flag {
1491b28f047bSChao Yu 	COMPRESS_CHKSUM,
1492b28f047bSChao Yu 	COMPRESS_MAX_FLAG,
1493b28f047bSChao Yu };
1494b28f047bSChao Yu 
14956ce19affSChao Yu #define	COMPRESS_WATERMARK			20
14966ce19affSChao Yu #define	COMPRESS_PERCENT			20
14976ce19affSChao Yu 
1498b28f047bSChao Yu #define COMPRESS_DATA_RESERVED_SIZE		4
14994c8ff709SChao Yu struct compress_data {
15004c8ff709SChao Yu 	__le32 clen;			/* compressed data size */
1501b28f047bSChao Yu 	__le32 chksum;			/* compressed data chksum */
15024c8ff709SChao Yu 	__le32 reserved[COMPRESS_DATA_RESERVED_SIZE];	/* reserved */
15034c8ff709SChao Yu 	u8 cdata[];			/* compressed data */
15044c8ff709SChao Yu };
15054c8ff709SChao Yu 
15064c8ff709SChao Yu #define COMPRESS_HEADER_SIZE	(sizeof(struct compress_data))
15074c8ff709SChao Yu 
15084c8ff709SChao Yu #define F2FS_COMPRESSED_PAGE_MAGIC	0xF5F2C000
15094c8ff709SChao Yu 
15103fde13f8SChao Yu #define	COMPRESS_LEVEL_OFFSET	8
15113fde13f8SChao Yu 
15124c8ff709SChao Yu /* compress context */
15134c8ff709SChao Yu struct compress_ctx {
15144c8ff709SChao Yu 	struct inode *inode;		/* inode the context belong to */
15154c8ff709SChao Yu 	pgoff_t cluster_idx;		/* cluster index number */
15164c8ff709SChao Yu 	unsigned int cluster_size;	/* page count in cluster */
15174c8ff709SChao Yu 	unsigned int log_cluster_size;	/* log of cluster size */
15184c8ff709SChao Yu 	struct page **rpages;		/* pages store raw data in cluster */
15194c8ff709SChao Yu 	unsigned int nr_rpages;		/* total page number in rpages */
15204c8ff709SChao Yu 	struct page **cpages;		/* pages store compressed data in cluster */
15214c8ff709SChao Yu 	unsigned int nr_cpages;		/* total page number in cpages */
15223271d7ebSFengnan Chang 	unsigned int valid_nr_cpages;	/* valid page number in cpages */
15234c8ff709SChao Yu 	void *rbuf;			/* virtual mapped address on rpages */
15244c8ff709SChao Yu 	struct compress_data *cbuf;	/* virtual mapped address on cpages */
15254c8ff709SChao Yu 	size_t rlen;			/* valid data length in rbuf */
15264c8ff709SChao Yu 	size_t clen;			/* valid data length in cbuf */
15274c8ff709SChao Yu 	void *private;			/* payload buffer for specified compression algorithm */
152850cfa66fSChao Yu 	void *private2;			/* extra payload buffer */
15294c8ff709SChao Yu };
15304c8ff709SChao Yu 
15314c8ff709SChao Yu /* compress context for write IO path */
15324c8ff709SChao Yu struct compress_io_ctx {
15334c8ff709SChao Yu 	u32 magic;			/* magic number to indicate page is compressed */
15344c8ff709SChao Yu 	struct inode *inode;		/* inode the context belong to */
15354c8ff709SChao Yu 	struct page **rpages;		/* pages store raw data in cluster */
15364c8ff709SChao Yu 	unsigned int nr_rpages;		/* total page number in rpages */
1537e6c3948dSChao Yu 	atomic_t pending_pages;		/* in-flight compressed page count */
15384c8ff709SChao Yu };
15394c8ff709SChao Yu 
15407f59b277SEric Biggers /* Context for decompressing one cluster on the read IO path */
15414c8ff709SChao Yu struct decompress_io_ctx {
15424c8ff709SChao Yu 	u32 magic;			/* magic number to indicate page is compressed */
15434c8ff709SChao Yu 	struct inode *inode;		/* inode the context belong to */
15444c8ff709SChao Yu 	pgoff_t cluster_idx;		/* cluster index number */
15454c8ff709SChao Yu 	unsigned int cluster_size;	/* page count in cluster */
15464c8ff709SChao Yu 	unsigned int log_cluster_size;	/* log of cluster size */
15474c8ff709SChao Yu 	struct page **rpages;		/* pages store raw data in cluster */
15484c8ff709SChao Yu 	unsigned int nr_rpages;		/* total page number in rpages */
15494c8ff709SChao Yu 	struct page **cpages;		/* pages store compressed data in cluster */
15504c8ff709SChao Yu 	unsigned int nr_cpages;		/* total page number in cpages */
15514c8ff709SChao Yu 	struct page **tpages;		/* temp pages to pad holes in cluster */
15524c8ff709SChao Yu 	void *rbuf;			/* virtual mapped address on rpages */
15534c8ff709SChao Yu 	struct compress_data *cbuf;	/* virtual mapped address on cpages */
15544c8ff709SChao Yu 	size_t rlen;			/* valid data length in rbuf */
15554c8ff709SChao Yu 	size_t clen;			/* valid data length in cbuf */
15567f59b277SEric Biggers 
15577f59b277SEric Biggers 	/*
15587f59b277SEric Biggers 	 * The number of compressed pages remaining to be read in this cluster.
15597f59b277SEric Biggers 	 * This is initially nr_cpages.  It is decremented by 1 each time a page
15607f59b277SEric Biggers 	 * has been read (or failed to be read).  When it reaches 0, the cluster
15617f59b277SEric Biggers 	 * is decompressed (or an error is reported).
15627f59b277SEric Biggers 	 *
15637f59b277SEric Biggers 	 * If an error occurs before all the pages have been submitted for I/O,
15647f59b277SEric Biggers 	 * then this will never reach 0.  In this case the I/O submitter is
15657f59b277SEric Biggers 	 * responsible for calling f2fs_decompress_end_io() instead.
15667f59b277SEric Biggers 	 */
15677f59b277SEric Biggers 	atomic_t remaining_pages;
15687f59b277SEric Biggers 
15697f59b277SEric Biggers 	/*
15707f59b277SEric Biggers 	 * Number of references to this decompress_io_ctx.
15717f59b277SEric Biggers 	 *
15727f59b277SEric Biggers 	 * One reference is held for I/O completion.  This reference is dropped
15737f59b277SEric Biggers 	 * after the pagecache pages are updated and unlocked -- either after
15747f59b277SEric Biggers 	 * decompression (and verity if enabled), or after an error.
15757f59b277SEric Biggers 	 *
15767f59b277SEric Biggers 	 * In addition, each compressed page holds a reference while it is in a
15777f59b277SEric Biggers 	 * bio.  These references are necessary prevent compressed pages from
15787f59b277SEric Biggers 	 * being freed while they are still in a bio.
15797f59b277SEric Biggers 	 */
15807f59b277SEric Biggers 	refcount_t refcnt;
15817f59b277SEric Biggers 
15827f59b277SEric Biggers 	bool failed;			/* IO error occurred before decompression? */
15837f59b277SEric Biggers 	bool need_verity;		/* need fs-verity verification after decompression? */
158450cfa66fSChao Yu 	void *private;			/* payload buffer for specified decompression algorithm */
158550cfa66fSChao Yu 	void *private2;			/* extra payload buffer */
15867f59b277SEric Biggers 	struct work_struct verity_work;	/* work to verify the decompressed pages */
15874c8ff709SChao Yu };
15884c8ff709SChao Yu 
15894c8ff709SChao Yu #define NULL_CLUSTER			((unsigned int)(~0))
15904c8ff709SChao Yu #define MIN_COMPRESS_LOG_SIZE		2
15914c8ff709SChao Yu #define MAX_COMPRESS_LOG_SIZE		8
15920e2b7385SChao Yu #define MAX_COMPRESS_WINDOW_SIZE(log_size)	((PAGE_SIZE) << (log_size))
15934c8ff709SChao Yu 
159439a53e0cSJaegeuk Kim struct f2fs_sb_info {
159539a53e0cSJaegeuk Kim 	struct super_block *sb;			/* pointer to VFS super block */
15965e176d54SJaegeuk Kim 	struct proc_dir_entry *s_proc;		/* proc entry */
159739a53e0cSJaegeuk Kim 	struct f2fs_super_block *raw_super;	/* raw super block pointer */
1598e4544b63STim Murray 	struct f2fs_rwsem sb_lock;		/* lock for raw super block */
1599e8240f65SChao Yu 	int valid_super_block;			/* valid super block no */
1600fadb2fb8SChao Yu 	unsigned long s_flag;				/* flags for sbi */
1601853137ceSJaegeuk Kim 	struct mutex writepages;		/* mutex for writepages() */
160239a53e0cSJaegeuk Kim 
1603178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
1604178053e2SDamien Le Moal 	unsigned int blocks_per_blkz;		/* F2FS blocks per zone */
1605178053e2SDamien Le Moal 	unsigned int log_blocks_per_blkz;	/* log2 F2FS blocks per zone */
1606178053e2SDamien Le Moal #endif
1607178053e2SDamien Le Moal 
160839a53e0cSJaegeuk Kim 	/* for node-related operations */
160939a53e0cSJaegeuk Kim 	struct f2fs_nm_info *nm_info;		/* node manager */
161039a53e0cSJaegeuk Kim 	struct inode *node_inode;		/* cache node blocks */
161139a53e0cSJaegeuk Kim 
161239a53e0cSJaegeuk Kim 	/* for segment-related operations */
161339a53e0cSJaegeuk Kim 	struct f2fs_sm_info *sm_info;		/* segment manager */
16141ff7bd3bSJaegeuk Kim 
16151ff7bd3bSJaegeuk Kim 	/* for bio operations */
1616a912b54dSJaegeuk Kim 	struct f2fs_bio_info *write_io[NR_PAGE_TYPE];	/* for write bios */
1617107a805dSChao Yu 	/* keep migration IO order for LFS mode */
1618e4544b63STim Murray 	struct f2fs_rwsem io_order_lock;
16190a595ebaSJaegeuk Kim 	mempool_t *write_io_dummy;		/* Dummy pages */
1620*50c63009SJaegeuk Kim 	pgoff_t metapage_eio_ofs;		/* EIO page offset */
1621*50c63009SJaegeuk Kim 	int metapage_eio_cnt;			/* EIO count */
162239a53e0cSJaegeuk Kim 
162339a53e0cSJaegeuk Kim 	/* for checkpoint */
162439a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt;		/* raw checkpoint pointer */
16258508e44aSJaegeuk Kim 	int cur_cp_pack;			/* remain current cp pack */
1626aaec2b1dSChao Yu 	spinlock_t cp_lock;			/* for flag in ckpt */
162739a53e0cSJaegeuk Kim 	struct inode *meta_inode;		/* cache meta blocks */
1628e4544b63STim Murray 	struct f2fs_rwsem cp_global_sem;	/* checkpoint procedure lock */
1629e4544b63STim Murray 	struct f2fs_rwsem cp_rwsem;		/* blocking FS operations */
1630e4544b63STim Murray 	struct f2fs_rwsem node_write;		/* locking node writes */
1631e4544b63STim Murray 	struct f2fs_rwsem node_change;	/* locking node change */
1632fb51b5efSChangman Lee 	wait_queue_head_t cp_wait;
16336beceb54SJaegeuk Kim 	unsigned long last_time[MAX_TIME];	/* to store time in jiffies */
16346beceb54SJaegeuk Kim 	long interval_time[MAX_TIME];		/* to store thresholds */
1635261eeb9cSDaeho Jeong 	struct ckpt_req_control cprc_info;	/* for checkpoint request control */
163639a53e0cSJaegeuk Kim 
163767298804SChao Yu 	struct inode_management im[MAX_INO_ENTRY];	/* manage inode cache */
16386451e041SJaegeuk Kim 
163950fa53ecSChao Yu 	spinlock_t fsync_node_lock;		/* for node entry lock */
164050fa53ecSChao Yu 	struct list_head fsync_node_list;	/* node list head */
164150fa53ecSChao Yu 	unsigned int fsync_seg_id;		/* sequence id */
164250fa53ecSChao Yu 	unsigned int fsync_node_num;		/* number of node entries */
164350fa53ecSChao Yu 
16446451e041SJaegeuk Kim 	/* for orphan inode, use 0'th array */
16450d47c1adSGu Zheng 	unsigned int max_orphans;		/* max orphan inodes */
164639a53e0cSJaegeuk Kim 
1647c227f912SChao Yu 	/* for inode management */
1648c227f912SChao Yu 	struct list_head inode_list[NR_INODE_TYPE];	/* dirty inode list */
1649c227f912SChao Yu 	spinlock_t inode_lock[NR_INODE_TYPE];	/* for dirty inode list lock */
1650040d2bb3SChao Yu 	struct mutex flush_lock;		/* for flush exclusion */
165139a53e0cSJaegeuk Kim 
165213054c54SChao Yu 	/* for extent tree cache */
165313054c54SChao Yu 	struct radix_tree_root extent_tree_root;/* cache extent cache entries */
16545e8256acSYunlei He 	struct mutex extent_tree_lock;	/* locking extent radix tree */
165513054c54SChao Yu 	struct list_head extent_list;		/* lru list for shrinker */
165613054c54SChao Yu 	spinlock_t extent_lock;			/* locking extent lru list */
16577441ccefSJaegeuk Kim 	atomic_t total_ext_tree;		/* extent tree count */
1658137d09f0SJaegeuk Kim 	struct list_head zombie_list;		/* extent zombie tree list */
165974fd8d99SJaegeuk Kim 	atomic_t total_zombie_tree;		/* extent zombie tree count */
166013054c54SChao Yu 	atomic_t total_ext_node;		/* extent info count */
166113054c54SChao Yu 
166239a53e0cSJaegeuk Kim 	/* basic filesystem units */
166339a53e0cSJaegeuk Kim 	unsigned int log_sectors_per_block;	/* log2 sectors per block */
166439a53e0cSJaegeuk Kim 	unsigned int log_blocksize;		/* log2 block size */
166539a53e0cSJaegeuk Kim 	unsigned int blocksize;			/* block size */
166639a53e0cSJaegeuk Kim 	unsigned int root_ino_num;		/* root inode number*/
166739a53e0cSJaegeuk Kim 	unsigned int node_ino_num;		/* node inode number*/
166839a53e0cSJaegeuk Kim 	unsigned int meta_ino_num;		/* meta inode number*/
166939a53e0cSJaegeuk Kim 	unsigned int log_blocks_per_seg;	/* log2 blocks per segment */
167039a53e0cSJaegeuk Kim 	unsigned int blocks_per_seg;		/* blocks per segment */
167139a53e0cSJaegeuk Kim 	unsigned int segs_per_sec;		/* segments per section */
167239a53e0cSJaegeuk Kim 	unsigned int secs_per_zone;		/* sections per zone */
167339a53e0cSJaegeuk Kim 	unsigned int total_sections;		/* total section count */
167439a53e0cSJaegeuk Kim 	unsigned int total_node_count;		/* total node block count */
167539a53e0cSJaegeuk Kim 	unsigned int total_valid_node_count;	/* valid node block count */
1676ab9fa662SJaegeuk Kim 	int dir_level;				/* directory level */
1677f6df8f23SSheng Yong 	int readdir_ra;				/* readahead inode in readdir */
167810208567SJaegeuk Kim 	u64 max_io_bytes;			/* max io bytes to merge IOs */
167939a53e0cSJaegeuk Kim 
168039a53e0cSJaegeuk Kim 	block_t user_block_count;		/* # of user blocks */
168139a53e0cSJaegeuk Kim 	block_t total_valid_block_count;	/* # of valid blocks */
1682a66cdd98SJaegeuk Kim 	block_t discard_blks;			/* discard command candidats */
168339a53e0cSJaegeuk Kim 	block_t last_valid_block_count;		/* for recovery */
1684daeb433eSChao Yu 	block_t reserved_blocks;		/* configurable reserved blocks */
168580d42145SYunlong Song 	block_t current_reserved_blocks;	/* current reserved blocks */
1686daeb433eSChao Yu 
16874354994fSDaniel Rosenberg 	/* Additional tracking for no checkpoint mode */
16884354994fSDaniel Rosenberg 	block_t unusable_block_count;		/* # of blocks saved by last cp */
16894354994fSDaniel Rosenberg 
1690292c196aSChao Yu 	unsigned int nquota_files;		/* # of quota sysfile */
1691e4544b63STim Murray 	struct f2fs_rwsem quota_sem;		/* blocking cp for flags */
1692292c196aSChao Yu 
1693523be8a6SJaegeuk Kim 	/* # of pages, see count_type */
169435782b23SJaegeuk Kim 	atomic_t nr_pages[NR_COUNT_TYPE];
169541382ec4SJaegeuk Kim 	/* # of allocated blocks */
169641382ec4SJaegeuk Kim 	struct percpu_counter alloc_valid_block_count;
169747c8ebccSJaegeuk Kim 	/* # of node block writes as roll forward recovery */
169847c8ebccSJaegeuk Kim 	struct percpu_counter rf_node_block_count;
169939a53e0cSJaegeuk Kim 
1700687de7f1SJaegeuk Kim 	/* writeback control */
1701c29fd0c0SChao Yu 	atomic_t wb_sync_req[META];	/* count # of WB_SYNC threads */
1702687de7f1SJaegeuk Kim 
1703513c5f37SJaegeuk Kim 	/* valid inode count */
1704513c5f37SJaegeuk Kim 	struct percpu_counter total_valid_inode_count;
1705513c5f37SJaegeuk Kim 
170639a53e0cSJaegeuk Kim 	struct f2fs_mount_info mount_opt;	/* mount options */
170739a53e0cSJaegeuk Kim 
170839a53e0cSJaegeuk Kim 	/* for cleaning operations */
1709e4544b63STim Murray 	struct f2fs_rwsem gc_lock;		/*
1710fb24fea7SChao Yu 						 * semaphore for GC, avoid
1711fb24fea7SChao Yu 						 * race between GC and GC or CP
1712fb24fea7SChao Yu 						 */
171339a53e0cSJaegeuk Kim 	struct f2fs_gc_kthread	*gc_thread;	/* GC thread */
1714093749e2SChao Yu 	struct atgc_management am;		/* atgc management */
17155ec4e49fSJaegeuk Kim 	unsigned int cur_victim_sec;		/* current victim section num */
17165b0e9539SJaegeuk Kim 	unsigned int gc_mode;			/* current GC state */
1717e3080b01SChao Yu 	unsigned int next_victim_seg[2];	/* next segment in victim section */
1718325163e9SDaeho Jeong 	spinlock_t gc_urgent_high_lock;
1719325163e9SDaeho Jeong 	bool gc_urgent_high_limited;		/* indicates having limited trial count */
1720325163e9SDaeho Jeong 	unsigned int gc_urgent_high_remaining;	/* remaining trial count for GC_URGENT_HIGH */
17210e5e8111SDaeho Jeong 
17222ef79ecbSChao Yu 	/* for skip statistic */
1723677017d1SSahitya Tummala 	unsigned int atomic_files;		/* # of opened atomic file */
17242ef79ecbSChao Yu 	unsigned long long skipped_atomic_files[2];	/* FG_GC and BG_GC */
17256f8d4455SJaegeuk Kim 	unsigned long long skipped_gc_rwsem;		/* FG_GC only */
172639a53e0cSJaegeuk Kim 
17271ad71a27SJaegeuk Kim 	/* threshold for gc trials on pinned files */
17281ad71a27SJaegeuk Kim 	u64 gc_pin_file_threshold;
1729e4544b63STim Murray 	struct f2fs_rwsem pin_sem;
17301ad71a27SJaegeuk Kim 
1731b1c57c1cSJaegeuk Kim 	/* maximum # of trials to find a victim segment for SSR and GC */
1732b1c57c1cSJaegeuk Kim 	unsigned int max_victim_search;
1733e3080b01SChao Yu 	/* migration granularity of garbage collection, unit: segment */
1734e3080b01SChao Yu 	unsigned int migration_granularity;
1735b1c57c1cSJaegeuk Kim 
173639a53e0cSJaegeuk Kim 	/*
173739a53e0cSJaegeuk Kim 	 * for stat information.
173839a53e0cSJaegeuk Kim 	 * one is for the LFS mode, and the other is for the SSR mode.
173939a53e0cSJaegeuk Kim 	 */
174035b09d82SNamjae Jeon #ifdef CONFIG_F2FS_STAT_FS
174139a53e0cSJaegeuk Kim 	struct f2fs_stat_info *stat_info;	/* FS status information */
1742b63e7be5SChao Yu 	atomic_t meta_count[META_MAX];		/* # of meta blocks */
174339a53e0cSJaegeuk Kim 	unsigned int segment_count[2];		/* # of allocated segments */
174439a53e0cSJaegeuk Kim 	unsigned int block_count[2];		/* # of allocated blocks */
1745b9a2c252SChangman Lee 	atomic_t inplace_count;		/* # of inplace update */
17465b7ee374SChao Yu 	atomic64_t total_hit_ext;		/* # of lookup extent cache */
17475b7ee374SChao Yu 	atomic64_t read_hit_rbtree;		/* # of hit rbtree extent node */
17485b7ee374SChao Yu 	atomic64_t read_hit_largest;		/* # of hit largest extent node */
17495b7ee374SChao Yu 	atomic64_t read_hit_cached;		/* # of hit cached extent node */
1750d5e8f6c9SChao Yu 	atomic_t inline_xattr;			/* # of inline_xattr inodes */
175103e14d52SChao Yu 	atomic_t inline_inode;			/* # of inline_data inodes */
175203e14d52SChao Yu 	atomic_t inline_dir;			/* # of inline_dentry inodes */
17534c8ff709SChao Yu 	atomic_t compr_inode;			/* # of compressed inodes */
1754ae999bb9SDaeho Jeong 	atomic64_t compr_blocks;		/* # of compressed blocks */
1755648d50baSChao Yu 	atomic_t vw_cnt;			/* # of volatile writes */
175626a28a0cSJaegeuk Kim 	atomic_t max_aw_cnt;			/* max # of atomic writes */
1757648d50baSChao Yu 	atomic_t max_vw_cnt;			/* max # of volatile writes */
1758274bd9baSChao Yu 	unsigned int io_skip_bggc;		/* skip background gc for in-flight IO */
1759274bd9baSChao Yu 	unsigned int other_skip_bggc;		/* skip background gc for other reasons */
176033fbd510SChao Yu 	unsigned int ndirty_inode[NR_INODE_TYPE];	/* # of dirty inodes */
176135b09d82SNamjae Jeon #endif
176239a53e0cSJaegeuk Kim 	spinlock_t stat_lock;			/* lock for stat operations */
1763b59d0baeSNamjae Jeon 
1764da9953b7SJaegeuk Kim 	/* to attach REQ_META|REQ_FUA flags */
1765da9953b7SJaegeuk Kim 	unsigned int data_io_flag;
176632b6aba8SJaegeuk Kim 	unsigned int node_io_flag;
1767b0af6d49SChao Yu 
1768b59d0baeSNamjae Jeon 	/* For sysfs suppport */
17695d4daa57SChao Yu 	struct kobject s_kobj;			/* /sys/fs/f2fs/<devname> */
1770b59d0baeSNamjae Jeon 	struct completion s_kobj_unregister;
17712658e50dSJaegeuk Kim 
17725d4daa57SChao Yu 	struct kobject s_stat_kobj;		/* /sys/fs/f2fs/<devname>/stat */
17735d4daa57SChao Yu 	struct completion s_stat_kobj_unregister;
17745d4daa57SChao Yu 
17754c89b53dSJaegeuk Kim 	struct kobject s_feature_list_kobj;		/* /sys/fs/f2fs/<devname>/feature_list */
17764c89b53dSJaegeuk Kim 	struct completion s_feature_list_kobj_unregister;
17774c89b53dSJaegeuk Kim 
17782658e50dSJaegeuk Kim 	/* For shrinker support */
17792658e50dSJaegeuk Kim 	struct list_head s_list;
178071f2c820SChao Yu 	struct mutex umount_mutex;
178171f2c820SChao Yu 	unsigned int shrinker_run_no;
178271f2c820SChao Yu 
178371f2c820SChao Yu 	/* For multi devices */
17843c62be17SJaegeuk Kim 	int s_ndevs;				/* number of devices */
17853c62be17SJaegeuk Kim 	struct f2fs_dev_info *devs;		/* for device list */
17861228b482SChao Yu 	unsigned int dirty_device;		/* for checkpoint data flush */
17871228b482SChao Yu 	spinlock_t dev_lock;			/* protect dirty_device */
178871f2c820SChao Yu 	bool aligned_blksize;			/* all devices has the same logical blksize */
17898f1dbbbbSShuoran Liu 
17908f1dbbbbSShuoran Liu 	/* For write statistics */
17918f1dbbbbSShuoran Liu 	u64 sectors_written_start;
17928f1dbbbbSShuoran Liu 	u64 kbytes_written;
179343b6573bSKeith Mok 
179443b6573bSKeith Mok 	/* Reference to checksum algorithm driver via cryptoapi */
179543b6573bSKeith Mok 	struct crypto_shash *s_chksum_driver;
17961ecc0c5cSChao Yu 
1797704956ecSChao Yu 	/* Precomputed FS UUID checksum for seeding other checksums */
1798704956ecSChao Yu 	__u32 s_chksum_seed;
17994c8ff709SChao Yu 
18004c8ff709SChao Yu 	struct workqueue_struct *post_read_wq;	/* post read workqueue */
1801a999150fSChao Yu 
1802a999150fSChao Yu 	struct kmem_cache *inline_xattr_slab;	/* inline xattr entry */
1803a999150fSChao Yu 	unsigned int inline_xattr_slab_size;	/* default inline xattr slab size */
180431083031SChao Yu 
180507c6b593SDaeho Jeong 	/* For reclaimed segs statistics per each GC mode */
180607c6b593SDaeho Jeong 	unsigned int gc_segment_mode;		/* GC state for reclaimed segments */
180707c6b593SDaeho Jeong 	unsigned int gc_reclaimed_segs[MAX_GC_MODE];	/* Reclaimed segs for each mode */
180807c6b593SDaeho Jeong 
18090f6b56ecSDaeho Jeong 	unsigned long seq_file_ra_mul;		/* multiplier for ra_pages of seq. files in fadvise */
18100f6b56ecSDaeho Jeong 
18116691d940SDaeho Jeong 	int max_fragment_chunk;			/* max chunk size for block fragmentation mode */
18126691d940SDaeho Jeong 	int max_fragment_hole;			/* max hole size for block fragmentation mode */
18136691d940SDaeho Jeong 
181431083031SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
181531083031SChao Yu 	struct kmem_cache *page_array_slab;	/* page array entry */
181631083031SChao Yu 	unsigned int page_array_slab_size;	/* default page array slab size */
18175ac443e2SDaeho Jeong 
18185ac443e2SDaeho Jeong 	/* For runtime compression statistics */
18195ac443e2SDaeho Jeong 	u64 compr_written_block;
18205ac443e2SDaeho Jeong 	u64 compr_saved_block;
18215ac443e2SDaeho Jeong 	u32 compr_new_inode;
18226ce19affSChao Yu 
18236ce19affSChao Yu 	/* For compressed block cache */
18246ce19affSChao Yu 	struct inode *compress_inode;		/* cache compressed blocks */
18256ce19affSChao Yu 	unsigned int compress_percent;		/* cache page percentage */
18266ce19affSChao Yu 	unsigned int compress_watermark;	/* cache page watermark */
18276ce19affSChao Yu 	atomic_t compress_page_hit;		/* cache hit count */
182831083031SChao Yu #endif
182952118743SDaeho Jeong 
183052118743SDaeho Jeong #ifdef CONFIG_F2FS_IOSTAT
183152118743SDaeho Jeong 	/* For app/fs IO statistics */
183252118743SDaeho Jeong 	spinlock_t iostat_lock;
183352118743SDaeho Jeong 	unsigned long long rw_iostat[NR_IO_TYPE];
183452118743SDaeho Jeong 	unsigned long long prev_rw_iostat[NR_IO_TYPE];
183552118743SDaeho Jeong 	bool iostat_enable;
183652118743SDaeho Jeong 	unsigned long iostat_next_period;
183752118743SDaeho Jeong 	unsigned int iostat_period_ms;
1838a4b68176SDaeho Jeong 
1839a4b68176SDaeho Jeong 	/* For io latency related statistics info in one iostat period */
1840a4b68176SDaeho Jeong 	spinlock_t iostat_lat_lock;
1841a4b68176SDaeho Jeong 	struct iostat_lat_info *iostat_io_lat;
184252118743SDaeho Jeong #endif
184339a53e0cSJaegeuk Kim };
184439a53e0cSJaegeuk Kim 
18451ecc0c5cSChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
1846c45d6002SChao Yu #define f2fs_show_injection_info(sbi, type)					\
1847c45d6002SChao Yu 	printk_ratelimited("%sF2FS-fs (%s) : inject %s in %s of %pS\n",	\
1848c45d6002SChao Yu 		KERN_INFO, sbi->sb->s_id,				\
1849c45d6002SChao Yu 		f2fs_fault_name[type],					\
185055523519SChao Yu 		__func__, __builtin_return_address(0))
18511ecc0c5cSChao Yu static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
18521ecc0c5cSChao Yu {
185363189b78SChao Yu 	struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
18541ecc0c5cSChao Yu 
18551ecc0c5cSChao Yu 	if (!ffi->inject_rate)
18561ecc0c5cSChao Yu 		return false;
18571ecc0c5cSChao Yu 
18581ecc0c5cSChao Yu 	if (!IS_FAULT_SET(ffi, type))
18591ecc0c5cSChao Yu 		return false;
18601ecc0c5cSChao Yu 
18611ecc0c5cSChao Yu 	atomic_inc(&ffi->inject_ops);
18621ecc0c5cSChao Yu 	if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
18631ecc0c5cSChao Yu 		atomic_set(&ffi->inject_ops, 0);
18641ecc0c5cSChao Yu 		return true;
18651ecc0c5cSChao Yu 	}
18661ecc0c5cSChao Yu 	return false;
18671ecc0c5cSChao Yu }
18687fa750a1SArnd Bergmann #else
1869c45d6002SChao Yu #define f2fs_show_injection_info(sbi, type) do { } while (0)
18707fa750a1SArnd Bergmann static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
18717fa750a1SArnd Bergmann {
18727fa750a1SArnd Bergmann 	return false;
18737fa750a1SArnd Bergmann }
18741ecc0c5cSChao Yu #endif
18751ecc0c5cSChao Yu 
18760916878dSDamien Le Moal /*
18770916878dSDamien Le Moal  * Test if the mounted volume is a multi-device volume.
18780916878dSDamien Le Moal  *   - For a single regular disk volume, sbi->s_ndevs is 0.
18790916878dSDamien Le Moal  *   - For a single zoned disk volume, sbi->s_ndevs is 1.
18800916878dSDamien Le Moal  *   - For a multi-device volume, sbi->s_ndevs is always 2 or more.
18810916878dSDamien Le Moal  */
18820916878dSDamien Le Moal static inline bool f2fs_is_multi_device(struct f2fs_sb_info *sbi)
18830916878dSDamien Le Moal {
18840916878dSDamien Le Moal 	return sbi->s_ndevs > 1;
18850916878dSDamien Le Moal }
18860916878dSDamien Le Moal 
18876beceb54SJaegeuk Kim static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
18886beceb54SJaegeuk Kim {
1889a7d10cf3SSahitya Tummala 	unsigned long now = jiffies;
1890a7d10cf3SSahitya Tummala 
1891a7d10cf3SSahitya Tummala 	sbi->last_time[type] = now;
1892a7d10cf3SSahitya Tummala 
1893a7d10cf3SSahitya Tummala 	/* DISCARD_TIME and GC_TIME are based on REQ_TIME */
1894a7d10cf3SSahitya Tummala 	if (type == REQ_TIME) {
1895a7d10cf3SSahitya Tummala 		sbi->last_time[DISCARD_TIME] = now;
1896a7d10cf3SSahitya Tummala 		sbi->last_time[GC_TIME] = now;
1897a7d10cf3SSahitya Tummala 	}
18986beceb54SJaegeuk Kim }
18996beceb54SJaegeuk Kim 
19006beceb54SJaegeuk Kim static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
19016beceb54SJaegeuk Kim {
19026bccfa19SArnd Bergmann 	unsigned long interval = sbi->interval_time[type] * HZ;
19036beceb54SJaegeuk Kim 
19046beceb54SJaegeuk Kim 	return time_after(jiffies, sbi->last_time[type] + interval);
19056beceb54SJaegeuk Kim }
19066beceb54SJaegeuk Kim 
1907a7d10cf3SSahitya Tummala static inline unsigned int f2fs_time_to_wait(struct f2fs_sb_info *sbi,
1908a7d10cf3SSahitya Tummala 						int type)
1909a7d10cf3SSahitya Tummala {
1910a7d10cf3SSahitya Tummala 	unsigned long interval = sbi->interval_time[type] * HZ;
1911a7d10cf3SSahitya Tummala 	unsigned int wait_ms = 0;
1912a7d10cf3SSahitya Tummala 	long delta;
1913a7d10cf3SSahitya Tummala 
1914a7d10cf3SSahitya Tummala 	delta = (sbi->last_time[type] + interval) - jiffies;
1915a7d10cf3SSahitya Tummala 	if (delta > 0)
1916a7d10cf3SSahitya Tummala 		wait_ms = jiffies_to_msecs(delta);
1917a7d10cf3SSahitya Tummala 
1918a7d10cf3SSahitya Tummala 	return wait_ms;
1919a7d10cf3SSahitya Tummala }
1920a7d10cf3SSahitya Tummala 
192139a53e0cSJaegeuk Kim /*
192239a53e0cSJaegeuk Kim  * Inline functions
192339a53e0cSJaegeuk Kim  */
1924416d2dbbSChao Yu static inline u32 __f2fs_crc32(struct f2fs_sb_info *sbi, u32 crc,
1925704956ecSChao Yu 			      const void *address, unsigned int length)
1926704956ecSChao Yu {
1927704956ecSChao Yu 	struct {
1928704956ecSChao Yu 		struct shash_desc shash;
1929704956ecSChao Yu 		char ctx[4];
1930704956ecSChao Yu 	} desc;
1931704956ecSChao Yu 	int err;
1932704956ecSChao Yu 
1933704956ecSChao Yu 	BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver) != sizeof(desc.ctx));
1934704956ecSChao Yu 
1935704956ecSChao Yu 	desc.shash.tfm = sbi->s_chksum_driver;
1936704956ecSChao Yu 	*(u32 *)desc.ctx = crc;
1937704956ecSChao Yu 
1938704956ecSChao Yu 	err = crypto_shash_update(&desc.shash, address, length);
1939704956ecSChao Yu 	BUG_ON(err);
1940704956ecSChao Yu 
1941704956ecSChao Yu 	return *(u32 *)desc.ctx;
1942704956ecSChao Yu }
1943704956ecSChao Yu 
1944416d2dbbSChao Yu static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
1945416d2dbbSChao Yu 			   unsigned int length)
1946416d2dbbSChao Yu {
1947416d2dbbSChao Yu 	return __f2fs_crc32(sbi, F2FS_SUPER_MAGIC, address, length);
1948416d2dbbSChao Yu }
1949416d2dbbSChao Yu 
1950416d2dbbSChao Yu static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
1951416d2dbbSChao Yu 				  void *buf, size_t buf_size)
1952416d2dbbSChao Yu {
1953416d2dbbSChao Yu 	return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
1954416d2dbbSChao Yu }
1955416d2dbbSChao Yu 
1956416d2dbbSChao Yu static inline u32 f2fs_chksum(struct f2fs_sb_info *sbi, u32 crc,
1957416d2dbbSChao Yu 			      const void *address, unsigned int length)
1958416d2dbbSChao Yu {
1959416d2dbbSChao Yu 	return __f2fs_crc32(sbi, crc, address, length);
1960416d2dbbSChao Yu }
1961416d2dbbSChao Yu 
196239a53e0cSJaegeuk Kim static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
196339a53e0cSJaegeuk Kim {
196439a53e0cSJaegeuk Kim 	return container_of(inode, struct f2fs_inode_info, vfs_inode);
196539a53e0cSJaegeuk Kim }
196639a53e0cSJaegeuk Kim 
196739a53e0cSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
196839a53e0cSJaegeuk Kim {
196939a53e0cSJaegeuk Kim 	return sb->s_fs_info;
197039a53e0cSJaegeuk Kim }
197139a53e0cSJaegeuk Kim 
19724081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
19734081363fSJaegeuk Kim {
19744081363fSJaegeuk Kim 	return F2FS_SB(inode->i_sb);
19754081363fSJaegeuk Kim }
19764081363fSJaegeuk Kim 
19774081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
19784081363fSJaegeuk Kim {
19794081363fSJaegeuk Kim 	return F2FS_I_SB(mapping->host);
19804081363fSJaegeuk Kim }
19814081363fSJaegeuk Kim 
19824081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
19834081363fSJaegeuk Kim {
19844969c06aSJaegeuk Kim 	return F2FS_M_SB(page_file_mapping(page));
19854081363fSJaegeuk Kim }
19864081363fSJaegeuk Kim 
198739a53e0cSJaegeuk Kim static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
198839a53e0cSJaegeuk Kim {
198939a53e0cSJaegeuk Kim 	return (struct f2fs_super_block *)(sbi->raw_super);
199039a53e0cSJaegeuk Kim }
199139a53e0cSJaegeuk Kim 
199239a53e0cSJaegeuk Kim static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
199339a53e0cSJaegeuk Kim {
199439a53e0cSJaegeuk Kim 	return (struct f2fs_checkpoint *)(sbi->ckpt);
199539a53e0cSJaegeuk Kim }
199639a53e0cSJaegeuk Kim 
199745590710SGu Zheng static inline struct f2fs_node *F2FS_NODE(struct page *page)
199845590710SGu Zheng {
199945590710SGu Zheng 	return (struct f2fs_node *)page_address(page);
200045590710SGu Zheng }
200145590710SGu Zheng 
200258bfaf44SJaegeuk Kim static inline struct f2fs_inode *F2FS_INODE(struct page *page)
200358bfaf44SJaegeuk Kim {
200458bfaf44SJaegeuk Kim 	return &((struct f2fs_node *)page_address(page))->i;
200558bfaf44SJaegeuk Kim }
200658bfaf44SJaegeuk Kim 
200739a53e0cSJaegeuk Kim static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
200839a53e0cSJaegeuk Kim {
200939a53e0cSJaegeuk Kim 	return (struct f2fs_nm_info *)(sbi->nm_info);
201039a53e0cSJaegeuk Kim }
201139a53e0cSJaegeuk Kim 
201239a53e0cSJaegeuk Kim static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
201339a53e0cSJaegeuk Kim {
201439a53e0cSJaegeuk Kim 	return (struct f2fs_sm_info *)(sbi->sm_info);
201539a53e0cSJaegeuk Kim }
201639a53e0cSJaegeuk Kim 
201739a53e0cSJaegeuk Kim static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
201839a53e0cSJaegeuk Kim {
201939a53e0cSJaegeuk Kim 	return (struct sit_info *)(SM_I(sbi)->sit_info);
202039a53e0cSJaegeuk Kim }
202139a53e0cSJaegeuk Kim 
202239a53e0cSJaegeuk Kim static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
202339a53e0cSJaegeuk Kim {
202439a53e0cSJaegeuk Kim 	return (struct free_segmap_info *)(SM_I(sbi)->free_info);
202539a53e0cSJaegeuk Kim }
202639a53e0cSJaegeuk Kim 
202739a53e0cSJaegeuk Kim static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
202839a53e0cSJaegeuk Kim {
202939a53e0cSJaegeuk Kim 	return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
203039a53e0cSJaegeuk Kim }
203139a53e0cSJaegeuk Kim 
20329df27d98SGu Zheng static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
20339df27d98SGu Zheng {
20349df27d98SGu Zheng 	return sbi->meta_inode->i_mapping;
20359df27d98SGu Zheng }
20369df27d98SGu Zheng 
20374ef51a8fSJaegeuk Kim static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
20384ef51a8fSJaegeuk Kim {
20394ef51a8fSJaegeuk Kim 	return sbi->node_inode->i_mapping;
20404ef51a8fSJaegeuk Kim }
20414ef51a8fSJaegeuk Kim 
2042caf0047eSChao Yu static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
204339a53e0cSJaegeuk Kim {
2044fadb2fb8SChao Yu 	return test_bit(type, &sbi->s_flag);
204539a53e0cSJaegeuk Kim }
204639a53e0cSJaegeuk Kim 
2047caf0047eSChao Yu static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
204839a53e0cSJaegeuk Kim {
2049fadb2fb8SChao Yu 	set_bit(type, &sbi->s_flag);
2050caf0047eSChao Yu }
2051caf0047eSChao Yu 
2052caf0047eSChao Yu static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
2053caf0047eSChao Yu {
2054fadb2fb8SChao Yu 	clear_bit(type, &sbi->s_flag);
205539a53e0cSJaegeuk Kim }
205639a53e0cSJaegeuk Kim 
2057d71b5564SJaegeuk Kim static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
2058d71b5564SJaegeuk Kim {
2059d71b5564SJaegeuk Kim 	return le64_to_cpu(cp->checkpoint_ver);
2060d71b5564SJaegeuk Kim }
2061d71b5564SJaegeuk Kim 
2062ea676733SJaegeuk Kim static inline unsigned long f2fs_qf_ino(struct super_block *sb, int type)
2063ea676733SJaegeuk Kim {
2064ea676733SJaegeuk Kim 	if (type < F2FS_MAX_QUOTAS)
2065ea676733SJaegeuk Kim 		return le32_to_cpu(F2FS_SB(sb)->raw_super->qf_ino[type]);
2066ea676733SJaegeuk Kim 	return 0;
2067ea676733SJaegeuk Kim }
2068ea676733SJaegeuk Kim 
2069ced2c7eaSKinglong Mee static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
2070ced2c7eaSKinglong Mee {
2071ced2c7eaSKinglong Mee 	size_t crc_offset = le32_to_cpu(cp->checksum_offset);
2072ced2c7eaSKinglong Mee 	return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
2073ced2c7eaSKinglong Mee }
2074ced2c7eaSKinglong Mee 
2075aaec2b1dSChao Yu static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
207625ca923bSJaegeuk Kim {
207725ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
2078aaec2b1dSChao Yu 
207925ca923bSJaegeuk Kim 	return ckpt_flags & f;
208025ca923bSJaegeuk Kim }
208125ca923bSJaegeuk Kim 
2082aaec2b1dSChao Yu static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
208325ca923bSJaegeuk Kim {
2084aaec2b1dSChao Yu 	return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
2085aaec2b1dSChao Yu }
2086aaec2b1dSChao Yu 
2087aaec2b1dSChao Yu static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
2088aaec2b1dSChao Yu {
2089aaec2b1dSChao Yu 	unsigned int ckpt_flags;
2090aaec2b1dSChao Yu 
2091aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
209225ca923bSJaegeuk Kim 	ckpt_flags |= f;
209325ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
209425ca923bSJaegeuk Kim }
209525ca923bSJaegeuk Kim 
2096aaec2b1dSChao Yu static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
209725ca923bSJaegeuk Kim {
2098d1aa2453SChao Yu 	unsigned long flags;
2099d1aa2453SChao Yu 
2100d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
2101aaec2b1dSChao Yu 	__set_ckpt_flags(F2FS_CKPT(sbi), f);
2102d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
2103aaec2b1dSChao Yu }
2104aaec2b1dSChao Yu 
2105aaec2b1dSChao Yu static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
2106aaec2b1dSChao Yu {
2107aaec2b1dSChao Yu 	unsigned int ckpt_flags;
2108aaec2b1dSChao Yu 
2109aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
211025ca923bSJaegeuk Kim 	ckpt_flags &= (~f);
211125ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
211225ca923bSJaegeuk Kim }
211325ca923bSJaegeuk Kim 
2114aaec2b1dSChao Yu static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
2115aaec2b1dSChao Yu {
2116d1aa2453SChao Yu 	unsigned long flags;
2117d1aa2453SChao Yu 
2118d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
2119aaec2b1dSChao Yu 	__clear_ckpt_flags(F2FS_CKPT(sbi), f);
2120d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
2121aaec2b1dSChao Yu }
2122aaec2b1dSChao Yu 
2123c7f91bd4SBart Van Assche #define init_f2fs_rwsem(sem)					\
2124c7f91bd4SBart Van Assche do {								\
2125c7f91bd4SBart Van Assche 	static struct lock_class_key __key;			\
2126c7f91bd4SBart Van Assche 								\
2127c7f91bd4SBart Van Assche 	__init_f2fs_rwsem((sem), #sem, &__key);			\
2128c7f91bd4SBart Van Assche } while (0)
2129c7f91bd4SBart Van Assche 
2130c7f91bd4SBart Van Assche static inline void __init_f2fs_rwsem(struct f2fs_rwsem *sem,
2131c7f91bd4SBart Van Assche 		const char *sem_name, struct lock_class_key *key)
2132e4544b63STim Murray {
2133c7f91bd4SBart Van Assche 	__init_rwsem(&sem->internal_rwsem, sem_name, key);
2134e4544b63STim Murray 	init_waitqueue_head(&sem->read_waiters);
2135e4544b63STim Murray }
2136e4544b63STim Murray 
2137e4544b63STim Murray static inline int f2fs_rwsem_is_locked(struct f2fs_rwsem *sem)
2138e4544b63STim Murray {
2139e4544b63STim Murray 	return rwsem_is_locked(&sem->internal_rwsem);
2140e4544b63STim Murray }
2141e4544b63STim Murray 
2142e4544b63STim Murray static inline int f2fs_rwsem_is_contended(struct f2fs_rwsem *sem)
2143e4544b63STim Murray {
2144e4544b63STim Murray 	return rwsem_is_contended(&sem->internal_rwsem);
2145e4544b63STim Murray }
2146e4544b63STim Murray 
2147e4544b63STim Murray static inline void f2fs_down_read(struct f2fs_rwsem *sem)
2148e4544b63STim Murray {
2149e4544b63STim Murray 	wait_event(sem->read_waiters, down_read_trylock(&sem->internal_rwsem));
2150e4544b63STim Murray }
2151e4544b63STim Murray 
2152e4544b63STim Murray static inline int f2fs_down_read_trylock(struct f2fs_rwsem *sem)
2153e4544b63STim Murray {
2154e4544b63STim Murray 	return down_read_trylock(&sem->internal_rwsem);
2155e4544b63STim Murray }
2156e4544b63STim Murray 
2157e4544b63STim Murray #ifdef CONFIG_DEBUG_LOCK_ALLOC
2158e4544b63STim Murray static inline void f2fs_down_read_nested(struct f2fs_rwsem *sem, int subclass)
2159e4544b63STim Murray {
2160e4544b63STim Murray 	down_read_nested(&sem->internal_rwsem, subclass);
2161e4544b63STim Murray }
2162e4544b63STim Murray #else
2163e4544b63STim Murray #define f2fs_down_read_nested(sem, subclass) f2fs_down_read(sem)
2164e4544b63STim Murray #endif
2165e4544b63STim Murray 
2166e4544b63STim Murray static inline void f2fs_up_read(struct f2fs_rwsem *sem)
2167e4544b63STim Murray {
2168e4544b63STim Murray 	up_read(&sem->internal_rwsem);
2169e4544b63STim Murray }
2170e4544b63STim Murray 
2171e4544b63STim Murray static inline void f2fs_down_write(struct f2fs_rwsem *sem)
2172e4544b63STim Murray {
2173e4544b63STim Murray 	down_write(&sem->internal_rwsem);
2174e4544b63STim Murray }
2175e4544b63STim Murray 
2176e4544b63STim Murray static inline int f2fs_down_write_trylock(struct f2fs_rwsem *sem)
2177e4544b63STim Murray {
2178e4544b63STim Murray 	return down_write_trylock(&sem->internal_rwsem);
2179e4544b63STim Murray }
2180e4544b63STim Murray 
2181e4544b63STim Murray static inline void f2fs_up_write(struct f2fs_rwsem *sem)
2182e4544b63STim Murray {
2183e4544b63STim Murray 	up_write(&sem->internal_rwsem);
2184e4544b63STim Murray 	wake_up_all(&sem->read_waiters);
2185e4544b63STim Murray }
2186e4544b63STim Murray 
2187e479556bSGu Zheng static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
218839a53e0cSJaegeuk Kim {
2189e4544b63STim Murray 	f2fs_down_read(&sbi->cp_rwsem);
219039a53e0cSJaegeuk Kim }
219139a53e0cSJaegeuk Kim 
2192cc15620bSJaegeuk Kim static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi)
2193cc15620bSJaegeuk Kim {
21943e020389SChao Yu 	if (time_to_inject(sbi, FAULT_LOCK_OP)) {
21953e020389SChao Yu 		f2fs_show_injection_info(sbi, FAULT_LOCK_OP);
21963e020389SChao Yu 		return 0;
21973e020389SChao Yu 	}
2198e4544b63STim Murray 	return f2fs_down_read_trylock(&sbi->cp_rwsem);
2199cc15620bSJaegeuk Kim }
2200cc15620bSJaegeuk Kim 
2201e479556bSGu Zheng static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
220239a53e0cSJaegeuk Kim {
2203e4544b63STim Murray 	f2fs_up_read(&sbi->cp_rwsem);
220439936837SJaegeuk Kim }
220539936837SJaegeuk Kim 
2206e479556bSGu Zheng static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
220739936837SJaegeuk Kim {
2208e4544b63STim Murray 	f2fs_down_write(&sbi->cp_rwsem);
220939936837SJaegeuk Kim }
221039936837SJaegeuk Kim 
2211e479556bSGu Zheng static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
221239936837SJaegeuk Kim {
2213e4544b63STim Murray 	f2fs_up_write(&sbi->cp_rwsem);
221439a53e0cSJaegeuk Kim }
221539a53e0cSJaegeuk Kim 
2216119ee914SJaegeuk Kim static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
2217119ee914SJaegeuk Kim {
2218119ee914SJaegeuk Kim 	int reason = CP_SYNC;
2219119ee914SJaegeuk Kim 
2220119ee914SJaegeuk Kim 	if (test_opt(sbi, FASTBOOT))
2221119ee914SJaegeuk Kim 		reason = CP_FASTBOOT;
2222119ee914SJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
2223119ee914SJaegeuk Kim 		reason = CP_UMOUNT;
2224119ee914SJaegeuk Kim 	return reason;
2225119ee914SJaegeuk Kim }
2226119ee914SJaegeuk Kim 
2227119ee914SJaegeuk Kim static inline bool __remain_node_summaries(int reason)
2228119ee914SJaegeuk Kim {
2229c473f1a9SChao Yu 	return (reason & (CP_UMOUNT | CP_FASTBOOT));
2230119ee914SJaegeuk Kim }
2231119ee914SJaegeuk Kim 
2232119ee914SJaegeuk Kim static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
2233119ee914SJaegeuk Kim {
2234aaec2b1dSChao Yu 	return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
2235aaec2b1dSChao Yu 			is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
2236119ee914SJaegeuk Kim }
2237119ee914SJaegeuk Kim 
223839a53e0cSJaegeuk Kim /*
223939a53e0cSJaegeuk Kim  * Check whether the inode has blocks or not
224039a53e0cSJaegeuk Kim  */
224139a53e0cSJaegeuk Kim static inline int F2FS_HAS_BLOCKS(struct inode *inode)
224239a53e0cSJaegeuk Kim {
22430eb0adadSChao Yu 	block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
22440eb0adadSChao Yu 
2245000519f2SChao Yu 	return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block;
224639a53e0cSJaegeuk Kim }
224739a53e0cSJaegeuk Kim 
22484bc8e9bcSChao Yu static inline bool f2fs_has_xattr_block(unsigned int ofs)
22494bc8e9bcSChao Yu {
22504bc8e9bcSChao Yu 	return ofs == XATTR_NODE_OFFSET;
22514bc8e9bcSChao Yu }
22524bc8e9bcSChao Yu 
2253d8a9a229SJaegeuk Kim static inline bool __allow_reserved_blocks(struct f2fs_sb_info *sbi,
2254a90a0884SJaegeuk Kim 					struct inode *inode, bool cap)
22557c2e5963SJaegeuk Kim {
2256d8a9a229SJaegeuk Kim 	if (!inode)
2257d8a9a229SJaegeuk Kim 		return true;
22587c2e5963SJaegeuk Kim 	if (!test_opt(sbi, RESERVE_ROOT))
22597c2e5963SJaegeuk Kim 		return false;
2260d8a9a229SJaegeuk Kim 	if (IS_NOQUOTA(inode))
2261d8a9a229SJaegeuk Kim 		return true;
226263189b78SChao Yu 	if (uid_eq(F2FS_OPTION(sbi).s_resuid, current_fsuid()))
22637c2e5963SJaegeuk Kim 		return true;
226463189b78SChao Yu 	if (!gid_eq(F2FS_OPTION(sbi).s_resgid, GLOBAL_ROOT_GID) &&
226563189b78SChao Yu 					in_group_p(F2FS_OPTION(sbi).s_resgid))
22667c2e5963SJaegeuk Kim 		return true;
2267a90a0884SJaegeuk Kim 	if (cap && capable(CAP_SYS_RESOURCE))
2268162b27aeSJaegeuk Kim 		return true;
22697c2e5963SJaegeuk Kim 	return false;
22707c2e5963SJaegeuk Kim }
22717c2e5963SJaegeuk Kim 
22720abd675eSChao Yu static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool);
22730abd675eSChao Yu static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
227446008c6dSChao Yu 				 struct inode *inode, blkcnt_t *count)
227539a53e0cSJaegeuk Kim {
22760abd675eSChao Yu 	blkcnt_t diff = 0, release = 0;
2277daeb433eSChao Yu 	block_t avail_user_block_count;
22780abd675eSChao Yu 	int ret;
22790abd675eSChao Yu 
22800abd675eSChao Yu 	ret = dquot_reserve_block(inode, *count);
22810abd675eSChao Yu 	if (ret)
22820abd675eSChao Yu 		return ret;
2283dd11a5dfSJaegeuk Kim 
228455523519SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
2285c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_BLOCK);
22860abd675eSChao Yu 		release = *count;
22879ea2f0beSChao Yu 		goto release_quota;
228855523519SChao Yu 	}
22897fa750a1SArnd Bergmann 
2290dd11a5dfSJaegeuk Kim 	/*
2291dd11a5dfSJaegeuk Kim 	 * let's increase this in prior to actual block count change in order
2292dd11a5dfSJaegeuk Kim 	 * for f2fs_sync_file to avoid data races when deciding checkpoint.
2293dd11a5dfSJaegeuk Kim 	 */
2294dd11a5dfSJaegeuk Kim 	percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
229539a53e0cSJaegeuk Kim 
229639a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
22972555a2d5SJaegeuk Kim 	sbi->total_valid_block_count += (block_t)(*count);
229880d42145SYunlong Song 	avail_user_block_count = sbi->user_block_count -
229980d42145SYunlong Song 					sbi->current_reserved_blocks;
23007e65be49SJaegeuk Kim 
2301a90a0884SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode, true))
230263189b78SChao Yu 		avail_user_block_count -= F2FS_OPTION(sbi).root_reserved_blocks;
2303300a8429SChao Yu 
2304300a8429SChao Yu 	if (F2FS_IO_ALIGNED(sbi))
2305300a8429SChao Yu 		avail_user_block_count -= sbi->blocks_per_seg *
2306300a8429SChao Yu 				SM_I(sbi)->additional_reserved_segments;
2307300a8429SChao Yu 
2308a4c3ecaaSDaniel Rosenberg 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2309a4c3ecaaSDaniel Rosenberg 		if (avail_user_block_count > sbi->unusable_block_count)
23104354994fSDaniel Rosenberg 			avail_user_block_count -= sbi->unusable_block_count;
2311a4c3ecaaSDaniel Rosenberg 		else
2312a4c3ecaaSDaniel Rosenberg 			avail_user_block_count = 0;
2313a4c3ecaaSDaniel Rosenberg 	}
2314daeb433eSChao Yu 	if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
2315daeb433eSChao Yu 		diff = sbi->total_valid_block_count - avail_user_block_count;
23167e65be49SJaegeuk Kim 		if (diff > *count)
23177e65be49SJaegeuk Kim 			diff = *count;
2318dd11a5dfSJaegeuk Kim 		*count -= diff;
23190abd675eSChao Yu 		release = diff;
23207e65be49SJaegeuk Kim 		sbi->total_valid_block_count -= diff;
232146008c6dSChao Yu 		if (!*count) {
232239a53e0cSJaegeuk Kim 			spin_unlock(&sbi->stat_lock);
23230abd675eSChao Yu 			goto enospc;
232439a53e0cSJaegeuk Kim 		}
232546008c6dSChao Yu 	}
232639a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
232741382ec4SJaegeuk Kim 
232836b877afSDaniel Rosenberg 	if (unlikely(release)) {
232936b877afSDaniel Rosenberg 		percpu_counter_sub(&sbi->alloc_valid_block_count, release);
23300abd675eSChao Yu 		dquot_release_reservation_block(inode, release);
233136b877afSDaniel Rosenberg 	}
23320abd675eSChao Yu 	f2fs_i_blocks_write(inode, *count, true, true);
23330abd675eSChao Yu 	return 0;
23340abd675eSChao Yu 
23350abd675eSChao Yu enospc:
233636b877afSDaniel Rosenberg 	percpu_counter_sub(&sbi->alloc_valid_block_count, release);
23379ea2f0beSChao Yu release_quota:
23380abd675eSChao Yu 	dquot_release_reservation_block(inode, release);
23390abd675eSChao Yu 	return -ENOSPC;
234039a53e0cSJaegeuk Kim }
234139a53e0cSJaegeuk Kim 
2342dcbb4c10SJoe Perches __printf(2, 3)
2343dcbb4c10SJoe Perches void f2fs_printk(struct f2fs_sb_info *sbi, const char *fmt, ...);
2344dcbb4c10SJoe Perches 
2345dcbb4c10SJoe Perches #define f2fs_err(sbi, fmt, ...)						\
2346dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_ERR fmt, ##__VA_ARGS__)
2347dcbb4c10SJoe Perches #define f2fs_warn(sbi, fmt, ...)					\
2348dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_WARNING fmt, ##__VA_ARGS__)
2349dcbb4c10SJoe Perches #define f2fs_notice(sbi, fmt, ...)					\
2350dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_NOTICE fmt, ##__VA_ARGS__)
2351dcbb4c10SJoe Perches #define f2fs_info(sbi, fmt, ...)					\
2352dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_INFO fmt, ##__VA_ARGS__)
2353dcbb4c10SJoe Perches #define f2fs_debug(sbi, fmt, ...)					\
2354dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_DEBUG fmt, ##__VA_ARGS__)
2355dcbb4c10SJoe Perches 
2356da19b0dcSGu Zheng static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
235739a53e0cSJaegeuk Kim 						struct inode *inode,
23580eb0adadSChao Yu 						block_t count)
235939a53e0cSJaegeuk Kim {
23600eb0adadSChao Yu 	blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK;
23610eb0adadSChao Yu 
236239a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
23639850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
236439a53e0cSJaegeuk Kim 	sbi->total_valid_block_count -= (block_t)count;
236580d42145SYunlong Song 	if (sbi->reserved_blocks &&
236680d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
236780d42145SYunlong Song 		sbi->current_reserved_blocks = min(sbi->reserved_blocks,
236880d42145SYunlong Song 					sbi->current_reserved_blocks + count);
236939a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
23705e159cd3SChao Yu 	if (unlikely(inode->i_blocks < sectors)) {
2371dcbb4c10SJoe Perches 		f2fs_warn(sbi, "Inconsistent i_blocks, ino:%lu, iblocks:%llu, sectors:%llu",
23725e159cd3SChao Yu 			  inode->i_ino,
23735e159cd3SChao Yu 			  (unsigned long long)inode->i_blocks,
23745e159cd3SChao Yu 			  (unsigned long long)sectors);
23755e159cd3SChao Yu 		set_sbi_flag(sbi, SBI_NEED_FSCK);
23765e159cd3SChao Yu 		return;
23775e159cd3SChao Yu 	}
23780abd675eSChao Yu 	f2fs_i_blocks_write(inode, count, false, true);
237939a53e0cSJaegeuk Kim }
238039a53e0cSJaegeuk Kim 
238139a53e0cSJaegeuk Kim static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
238239a53e0cSJaegeuk Kim {
238335782b23SJaegeuk Kim 	atomic_inc(&sbi->nr_pages[count_type]);
23847c4abcbeSChao Yu 
238520109873SChao Yu 	if (count_type == F2FS_DIRTY_DENTS ||
238620109873SChao Yu 			count_type == F2FS_DIRTY_NODES ||
238720109873SChao Yu 			count_type == F2FS_DIRTY_META ||
238820109873SChao Yu 			count_type == F2FS_DIRTY_QDATA ||
238920109873SChao Yu 			count_type == F2FS_DIRTY_IMETA)
2390caf0047eSChao Yu 		set_sbi_flag(sbi, SBI_IS_DIRTY);
239139a53e0cSJaegeuk Kim }
239239a53e0cSJaegeuk Kim 
2393a7ffdbe2SJaegeuk Kim static inline void inode_inc_dirty_pages(struct inode *inode)
239439a53e0cSJaegeuk Kim {
2395204706c7SJaegeuk Kim 	atomic_inc(&F2FS_I(inode)->dirty_pages);
2396c227f912SChao Yu 	inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
2397c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
23982c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
23992c8a4a28SJaegeuk Kim 		inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
240039a53e0cSJaegeuk Kim }
240139a53e0cSJaegeuk Kim 
240239a53e0cSJaegeuk Kim static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
240339a53e0cSJaegeuk Kim {
240435782b23SJaegeuk Kim 	atomic_dec(&sbi->nr_pages[count_type]);
240539a53e0cSJaegeuk Kim }
240639a53e0cSJaegeuk Kim 
2407a7ffdbe2SJaegeuk Kim static inline void inode_dec_dirty_pages(struct inode *inode)
240839a53e0cSJaegeuk Kim {
24095ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
24105ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
24111fe54f9dSJaegeuk Kim 		return;
24121fe54f9dSJaegeuk Kim 
2413204706c7SJaegeuk Kim 	atomic_dec(&F2FS_I(inode)->dirty_pages);
2414c227f912SChao Yu 	dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
2415c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
24162c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
24172c8a4a28SJaegeuk Kim 		dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
241839a53e0cSJaegeuk Kim }
241939a53e0cSJaegeuk Kim 
2420523be8a6SJaegeuk Kim static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
242139a53e0cSJaegeuk Kim {
242235782b23SJaegeuk Kim 	return atomic_read(&sbi->nr_pages[count_type]);
242339a53e0cSJaegeuk Kim }
242439a53e0cSJaegeuk Kim 
2425204706c7SJaegeuk Kim static inline int get_dirty_pages(struct inode *inode)
2426f8b2c1f9SJaegeuk Kim {
2427204706c7SJaegeuk Kim 	return atomic_read(&F2FS_I(inode)->dirty_pages);
2428f8b2c1f9SJaegeuk Kim }
2429f8b2c1f9SJaegeuk Kim 
24305ac206cfSNamjae Jeon static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
24315ac206cfSNamjae Jeon {
24323519e3f9SChao Yu 	unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
2433523be8a6SJaegeuk Kim 	unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
2434523be8a6SJaegeuk Kim 						sbi->log_blocks_per_seg;
2435523be8a6SJaegeuk Kim 
2436523be8a6SJaegeuk Kim 	return segs / sbi->segs_per_sec;
24375ac206cfSNamjae Jeon }
24385ac206cfSNamjae Jeon 
243939a53e0cSJaegeuk Kim static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
244039a53e0cSJaegeuk Kim {
24418b8343faSJaegeuk Kim 	return sbi->total_valid_block_count;
244239a53e0cSJaegeuk Kim }
244339a53e0cSJaegeuk Kim 
2444f83a2584SYunlei He static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
2445f83a2584SYunlei He {
2446f83a2584SYunlei He 	return sbi->discard_blks;
2447f83a2584SYunlei He }
2448f83a2584SYunlei He 
244939a53e0cSJaegeuk Kim static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
245039a53e0cSJaegeuk Kim {
245139a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
245239a53e0cSJaegeuk Kim 
245339a53e0cSJaegeuk Kim 	/* return NAT or SIT bitmap */
245439a53e0cSJaegeuk Kim 	if (flag == NAT_BITMAP)
245539a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
245639a53e0cSJaegeuk Kim 	else if (flag == SIT_BITMAP)
245739a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
245839a53e0cSJaegeuk Kim 
245939a53e0cSJaegeuk Kim 	return 0;
246039a53e0cSJaegeuk Kim }
246139a53e0cSJaegeuk Kim 
246255141486SWanpeng Li static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
246355141486SWanpeng Li {
246455141486SWanpeng Li 	return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
246555141486SWanpeng Li }
246655141486SWanpeng Li 
246739a53e0cSJaegeuk Kim static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
246839a53e0cSJaegeuk Kim {
246939a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
24704260c406SGustavo A. R. Silva 	void *tmp_ptr = &ckpt->sit_nat_version_bitmap;
24711dbe4152SChangman Lee 	int offset;
24721dbe4152SChangman Lee 
2473199bc3feSChao Yu 	if (is_set_ckpt_flags(sbi, CP_LARGE_NAT_BITMAP_FLAG)) {
2474199bc3feSChao Yu 		offset = (flag == SIT_BITMAP) ?
2475199bc3feSChao Yu 			le32_to_cpu(ckpt->nat_ver_bitmap_bytesize) : 0;
2476b471eb99SChao Yu 		/*
2477b471eb99SChao Yu 		 * if large_nat_bitmap feature is enabled, leave checksum
2478b471eb99SChao Yu 		 * protection for all nat/sit bitmaps.
2479b471eb99SChao Yu 		 */
24804260c406SGustavo A. R. Silva 		return tmp_ptr + offset + sizeof(__le32);
2481199bc3feSChao Yu 	}
2482199bc3feSChao Yu 
248355141486SWanpeng Li 	if (__cp_payload(sbi) > 0) {
24841dbe4152SChangman Lee 		if (flag == NAT_BITMAP)
24851dbe4152SChangman Lee 			return &ckpt->sit_nat_version_bitmap;
24861dbe4152SChangman Lee 		else
248765b85cccSJaegeuk Kim 			return (unsigned char *)ckpt + F2FS_BLKSIZE;
24881dbe4152SChangman Lee 	} else {
24891dbe4152SChangman Lee 		offset = (flag == NAT_BITMAP) ?
249025ca923bSJaegeuk Kim 			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
24914260c406SGustavo A. R. Silva 		return tmp_ptr + offset;
249239a53e0cSJaegeuk Kim 	}
24931dbe4152SChangman Lee }
249439a53e0cSJaegeuk Kim 
249539a53e0cSJaegeuk Kim static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
249639a53e0cSJaegeuk Kim {
24978508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
249839a53e0cSJaegeuk Kim 
24998508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 2)
250039a53e0cSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
250139a53e0cSJaegeuk Kim 	return start_addr;
250239a53e0cSJaegeuk Kim }
250339a53e0cSJaegeuk Kim 
25048508e44aSJaegeuk Kim static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
25058508e44aSJaegeuk Kim {
25068508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
25078508e44aSJaegeuk Kim 
25088508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 1)
25098508e44aSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
25108508e44aSJaegeuk Kim 	return start_addr;
25118508e44aSJaegeuk Kim }
25128508e44aSJaegeuk Kim 
25138508e44aSJaegeuk Kim static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
25148508e44aSJaegeuk Kim {
25158508e44aSJaegeuk Kim 	sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
25168508e44aSJaegeuk Kim }
25178508e44aSJaegeuk Kim 
251839a53e0cSJaegeuk Kim static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
251939a53e0cSJaegeuk Kim {
252039a53e0cSJaegeuk Kim 	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
252139a53e0cSJaegeuk Kim }
252239a53e0cSJaegeuk Kim 
25230abd675eSChao Yu static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
2524000519f2SChao Yu 					struct inode *inode, bool is_inode)
252539a53e0cSJaegeuk Kim {
252639a53e0cSJaegeuk Kim 	block_t	valid_block_count;
2527a4c3ecaaSDaniel Rosenberg 	unsigned int valid_node_count, user_block_count;
2528af033b2aSChao Yu 	int err;
25290abd675eSChao Yu 
2530af033b2aSChao Yu 	if (is_inode) {
2531af033b2aSChao Yu 		if (inode) {
2532af033b2aSChao Yu 			err = dquot_alloc_inode(inode);
2533af033b2aSChao Yu 			if (err)
2534af033b2aSChao Yu 				return err;
2535af033b2aSChao Yu 		}
2536af033b2aSChao Yu 	} else {
2537af033b2aSChao Yu 		err = dquot_reserve_block(inode, 1);
2538af033b2aSChao Yu 		if (err)
2539af033b2aSChao Yu 			return err;
25400abd675eSChao Yu 	}
254139a53e0cSJaegeuk Kim 
2542812c6056SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
2543c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_BLOCK);
2544812c6056SChao Yu 		goto enospc;
2545812c6056SChao Yu 	}
2546812c6056SChao Yu 
254739a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
254839a53e0cSJaegeuk Kim 
25497e65be49SJaegeuk Kim 	valid_block_count = sbi->total_valid_block_count +
25507e65be49SJaegeuk Kim 					sbi->current_reserved_blocks + 1;
25517e65be49SJaegeuk Kim 
2552a90a0884SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode, false))
255363189b78SChao Yu 		valid_block_count += F2FS_OPTION(sbi).root_reserved_blocks;
2554300a8429SChao Yu 
2555300a8429SChao Yu 	if (F2FS_IO_ALIGNED(sbi))
2556300a8429SChao Yu 		valid_block_count += sbi->blocks_per_seg *
2557300a8429SChao Yu 				SM_I(sbi)->additional_reserved_segments;
2558300a8429SChao Yu 
2559a4c3ecaaSDaniel Rosenberg 	user_block_count = sbi->user_block_count;
25604354994fSDaniel Rosenberg 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2561a4c3ecaaSDaniel Rosenberg 		user_block_count -= sbi->unusable_block_count;
25627e65be49SJaegeuk Kim 
2563a4c3ecaaSDaniel Rosenberg 	if (unlikely(valid_block_count > user_block_count)) {
256439a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
25650abd675eSChao Yu 		goto enospc;
256639a53e0cSJaegeuk Kim 	}
256739a53e0cSJaegeuk Kim 
2568ef86d709SGu Zheng 	valid_node_count = sbi->total_valid_node_count + 1;
2569cfb271d4SChao Yu 	if (unlikely(valid_node_count > sbi->total_node_count)) {
257039a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
25710abd675eSChao Yu 		goto enospc;
257239a53e0cSJaegeuk Kim 	}
257339a53e0cSJaegeuk Kim 
2574ef86d709SGu Zheng 	sbi->total_valid_node_count++;
2575ef86d709SGu Zheng 	sbi->total_valid_block_count++;
257639a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
257739a53e0cSJaegeuk Kim 
25780abd675eSChao Yu 	if (inode) {
25790abd675eSChao Yu 		if (is_inode)
25800abd675eSChao Yu 			f2fs_mark_inode_dirty_sync(inode, true);
25810abd675eSChao Yu 		else
25820abd675eSChao Yu 			f2fs_i_blocks_write(inode, 1, true, true);
25830abd675eSChao Yu 	}
25840abd675eSChao Yu 
258541382ec4SJaegeuk Kim 	percpu_counter_inc(&sbi->alloc_valid_block_count);
25860abd675eSChao Yu 	return 0;
25870abd675eSChao Yu 
25880abd675eSChao Yu enospc:
2589af033b2aSChao Yu 	if (is_inode) {
2590af033b2aSChao Yu 		if (inode)
2591af033b2aSChao Yu 			dquot_free_inode(inode);
2592af033b2aSChao Yu 	} else {
25930abd675eSChao Yu 		dquot_release_reservation_block(inode, 1);
2594af033b2aSChao Yu 	}
25950abd675eSChao Yu 	return -ENOSPC;
259639a53e0cSJaegeuk Kim }
259739a53e0cSJaegeuk Kim 
259839a53e0cSJaegeuk Kim static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
2599000519f2SChao Yu 					struct inode *inode, bool is_inode)
260039a53e0cSJaegeuk Kim {
260139a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
260239a53e0cSJaegeuk Kim 
26039850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_block_count);
26049850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_node_count);
260539a53e0cSJaegeuk Kim 
2606ef86d709SGu Zheng 	sbi->total_valid_node_count--;
2607ef86d709SGu Zheng 	sbi->total_valid_block_count--;
260880d42145SYunlong Song 	if (sbi->reserved_blocks &&
260980d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
261080d42145SYunlong Song 		sbi->current_reserved_blocks++;
261139a53e0cSJaegeuk Kim 
261239a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
26130abd675eSChao Yu 
2614ea6d7e72SChao Yu 	if (is_inode) {
2615af033b2aSChao Yu 		dquot_free_inode(inode);
2616ea6d7e72SChao Yu 	} else {
2617ea6d7e72SChao Yu 		if (unlikely(inode->i_blocks == 0)) {
2618097a7686SChao Yu 			f2fs_warn(sbi, "dec_valid_node_count: inconsistent i_blocks, ino:%lu, iblocks:%llu",
2619ea6d7e72SChao Yu 				  inode->i_ino,
2620ea6d7e72SChao Yu 				  (unsigned long long)inode->i_blocks);
2621ea6d7e72SChao Yu 			set_sbi_flag(sbi, SBI_NEED_FSCK);
2622ea6d7e72SChao Yu 			return;
2623ea6d7e72SChao Yu 		}
26240abd675eSChao Yu 		f2fs_i_blocks_write(inode, 1, false, true);
262539a53e0cSJaegeuk Kim 	}
2626ea6d7e72SChao Yu }
262739a53e0cSJaegeuk Kim 
262839a53e0cSJaegeuk Kim static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
262939a53e0cSJaegeuk Kim {
26308b8343faSJaegeuk Kim 	return sbi->total_valid_node_count;
263139a53e0cSJaegeuk Kim }
263239a53e0cSJaegeuk Kim 
263339a53e0cSJaegeuk Kim static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
263439a53e0cSJaegeuk Kim {
2635513c5f37SJaegeuk Kim 	percpu_counter_inc(&sbi->total_valid_inode_count);
263639a53e0cSJaegeuk Kim }
263739a53e0cSJaegeuk Kim 
26380e80220aSJaegeuk Kim static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
263939a53e0cSJaegeuk Kim {
2640513c5f37SJaegeuk Kim 	percpu_counter_dec(&sbi->total_valid_inode_count);
264139a53e0cSJaegeuk Kim }
264239a53e0cSJaegeuk Kim 
2643513c5f37SJaegeuk Kim static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
264439a53e0cSJaegeuk Kim {
2645513c5f37SJaegeuk Kim 	return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
264639a53e0cSJaegeuk Kim }
264739a53e0cSJaegeuk Kim 
2648a56c7c6fSJaegeuk Kim static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
2649a56c7c6fSJaegeuk Kim 						pgoff_t index, bool for_write)
2650a56c7c6fSJaegeuk Kim {
265182cf4f13SChao Yu 	struct page *page;
2652cac5a3d8SDongOh Shin 
26537fa750a1SArnd Bergmann 	if (IS_ENABLED(CONFIG_F2FS_FAULT_INJECTION)) {
265482cf4f13SChao Yu 		if (!for_write)
265582cf4f13SChao Yu 			page = find_get_page_flags(mapping, index,
265682cf4f13SChao Yu 							FGP_LOCK | FGP_ACCESSED);
265782cf4f13SChao Yu 		else
265882cf4f13SChao Yu 			page = find_lock_page(mapping, index);
2659c41f3cc3SJaegeuk Kim 		if (page)
2660c41f3cc3SJaegeuk Kim 			return page;
2661c41f3cc3SJaegeuk Kim 
266255523519SChao Yu 		if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) {
2663c45d6002SChao Yu 			f2fs_show_injection_info(F2FS_M_SB(mapping),
2664c45d6002SChao Yu 							FAULT_PAGE_ALLOC);
2665c41f3cc3SJaegeuk Kim 			return NULL;
266655523519SChao Yu 		}
26677fa750a1SArnd Bergmann 	}
26687fa750a1SArnd Bergmann 
2669a56c7c6fSJaegeuk Kim 	if (!for_write)
2670a56c7c6fSJaegeuk Kim 		return grab_cache_page(mapping, index);
2671a56c7c6fSJaegeuk Kim 	return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
2672a56c7c6fSJaegeuk Kim }
2673a56c7c6fSJaegeuk Kim 
267401eccef7SChao Yu static inline struct page *f2fs_pagecache_get_page(
267501eccef7SChao Yu 				struct address_space *mapping, pgoff_t index,
267601eccef7SChao Yu 				int fgp_flags, gfp_t gfp_mask)
267701eccef7SChao Yu {
267801eccef7SChao Yu 	if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_GET)) {
2679c45d6002SChao Yu 		f2fs_show_injection_info(F2FS_M_SB(mapping), FAULT_PAGE_GET);
268001eccef7SChao Yu 		return NULL;
268101eccef7SChao Yu 	}
26827fa750a1SArnd Bergmann 
268301eccef7SChao Yu 	return pagecache_get_page(mapping, index, fgp_flags, gfp_mask);
268401eccef7SChao Yu }
268501eccef7SChao Yu 
26866e2c64adSJaegeuk Kim static inline void f2fs_copy_page(struct page *src, struct page *dst)
26876e2c64adSJaegeuk Kim {
26886e2c64adSJaegeuk Kim 	char *src_kaddr = kmap(src);
26896e2c64adSJaegeuk Kim 	char *dst_kaddr = kmap(dst);
26906e2c64adSJaegeuk Kim 
26916e2c64adSJaegeuk Kim 	memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
26926e2c64adSJaegeuk Kim 	kunmap(dst);
26936e2c64adSJaegeuk Kim 	kunmap(src);
26946e2c64adSJaegeuk Kim }
26956e2c64adSJaegeuk Kim 
269639a53e0cSJaegeuk Kim static inline void f2fs_put_page(struct page *page, int unlock)
269739a53e0cSJaegeuk Kim {
2698031fa8ccSJaegeuk Kim 	if (!page)
269939a53e0cSJaegeuk Kim 		return;
270039a53e0cSJaegeuk Kim 
270139a53e0cSJaegeuk Kim 	if (unlock) {
27029850cf4aSJaegeuk Kim 		f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
270339a53e0cSJaegeuk Kim 		unlock_page(page);
270439a53e0cSJaegeuk Kim 	}
270509cbfeafSKirill A. Shutemov 	put_page(page);
270639a53e0cSJaegeuk Kim }
270739a53e0cSJaegeuk Kim 
270839a53e0cSJaegeuk Kim static inline void f2fs_put_dnode(struct dnode_of_data *dn)
270939a53e0cSJaegeuk Kim {
271039a53e0cSJaegeuk Kim 	if (dn->node_page)
271139a53e0cSJaegeuk Kim 		f2fs_put_page(dn->node_page, 1);
271239a53e0cSJaegeuk Kim 	if (dn->inode_page && dn->node_page != dn->inode_page)
271339a53e0cSJaegeuk Kim 		f2fs_put_page(dn->inode_page, 0);
271439a53e0cSJaegeuk Kim 	dn->node_page = NULL;
271539a53e0cSJaegeuk Kim 	dn->inode_page = NULL;
271639a53e0cSJaegeuk Kim }
271739a53e0cSJaegeuk Kim 
271839a53e0cSJaegeuk Kim static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
2719e8512d2eSGu Zheng 					size_t size)
272039a53e0cSJaegeuk Kim {
2721e8512d2eSGu Zheng 	return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
272239a53e0cSJaegeuk Kim }
272339a53e0cSJaegeuk Kim 
272432410577SChao Yu static inline void *f2fs_kmem_cache_alloc_nofail(struct kmem_cache *cachep,
27257bd59381SGu Zheng 						gfp_t flags)
27267bd59381SGu Zheng {
27277bd59381SGu Zheng 	void *entry;
27287bd59381SGu Zheng 
272980c54505SJaegeuk Kim 	entry = kmem_cache_alloc(cachep, flags);
273080c54505SJaegeuk Kim 	if (!entry)
273180c54505SJaegeuk Kim 		entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
27327bd59381SGu Zheng 	return entry;
27337bd59381SGu Zheng }
27347bd59381SGu Zheng 
273532410577SChao Yu static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
273632410577SChao Yu 			gfp_t flags, bool nofail, struct f2fs_sb_info *sbi)
273732410577SChao Yu {
273832410577SChao Yu 	if (nofail)
273932410577SChao Yu 		return f2fs_kmem_cache_alloc_nofail(cachep, flags);
274032410577SChao Yu 
274132410577SChao Yu 	if (time_to_inject(sbi, FAULT_SLAB_ALLOC)) {
274232410577SChao Yu 		f2fs_show_injection_info(sbi, FAULT_SLAB_ALLOC);
274332410577SChao Yu 		return NULL;
274432410577SChao Yu 	}
274532410577SChao Yu 
274632410577SChao Yu 	return kmem_cache_alloc(cachep, flags);
274732410577SChao Yu }
274832410577SChao Yu 
2749493720a4SChao Yu static inline bool is_inflight_io(struct f2fs_sb_info *sbi, int type)
27505f9abab4SJaegeuk Kim {
27515f9abab4SJaegeuk Kim 	if (get_pages(sbi, F2FS_RD_DATA) || get_pages(sbi, F2FS_RD_NODE) ||
27525f9abab4SJaegeuk Kim 		get_pages(sbi, F2FS_RD_META) || get_pages(sbi, F2FS_WB_DATA) ||
2753fef4129eSChao Yu 		get_pages(sbi, F2FS_WB_CP_DATA) ||
2754fef4129eSChao Yu 		get_pages(sbi, F2FS_DIO_READ) ||
275511ac8ef8SJaegeuk Kim 		get_pages(sbi, F2FS_DIO_WRITE))
2756493720a4SChao Yu 		return true;
275711ac8ef8SJaegeuk Kim 
275856659ce8SSahitya Tummala 	if (type != DISCARD_TIME && SM_I(sbi) && SM_I(sbi)->dcc_info &&
275911ac8ef8SJaegeuk Kim 			atomic_read(&SM_I(sbi)->dcc_info->queued_discard))
2760493720a4SChao Yu 		return true;
276111ac8ef8SJaegeuk Kim 
276211ac8ef8SJaegeuk Kim 	if (SM_I(sbi) && SM_I(sbi)->fcc_info &&
276372691af6SJaegeuk Kim 			atomic_read(&SM_I(sbi)->fcc_info->queued_flush))
2764493720a4SChao Yu 		return true;
2765493720a4SChao Yu 	return false;
2766493720a4SChao Yu }
2767493720a4SChao Yu 
2768493720a4SChao Yu static inline bool is_idle(struct f2fs_sb_info *sbi, int type)
2769493720a4SChao Yu {
2770493720a4SChao Yu 	if (sbi->gc_mode == GC_URGENT_HIGH)
2771493720a4SChao Yu 		return true;
2772493720a4SChao Yu 
2773493720a4SChao Yu 	if (is_inflight_io(sbi, type))
27745f9abab4SJaegeuk Kim 		return false;
277511ac8ef8SJaegeuk Kim 
27760e5e8111SDaeho Jeong 	if (sbi->gc_mode == GC_URGENT_LOW &&
27770e5e8111SDaeho Jeong 			(type == DISCARD_TIME || type == GC_TIME))
27780e5e8111SDaeho Jeong 		return true;
27790e5e8111SDaeho Jeong 
27805f9abab4SJaegeuk Kim 	return f2fs_time_over(sbi, type);
27815f9abab4SJaegeuk Kim }
27825f9abab4SJaegeuk Kim 
27839be32d72SJaegeuk Kim static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
27849be32d72SJaegeuk Kim 				unsigned long index, void *item)
27859be32d72SJaegeuk Kim {
27869be32d72SJaegeuk Kim 	while (radix_tree_insert(root, index, item))
27879be32d72SJaegeuk Kim 		cond_resched();
27889be32d72SJaegeuk Kim }
27899be32d72SJaegeuk Kim 
279039a53e0cSJaegeuk Kim #define RAW_IS_INODE(p)	((p)->footer.nid == (p)->footer.ino)
279139a53e0cSJaegeuk Kim 
279239a53e0cSJaegeuk Kim static inline bool IS_INODE(struct page *page)
279339a53e0cSJaegeuk Kim {
279445590710SGu Zheng 	struct f2fs_node *p = F2FS_NODE(page);
2795cac5a3d8SDongOh Shin 
279639a53e0cSJaegeuk Kim 	return RAW_IS_INODE(p);
279739a53e0cSJaegeuk Kim }
279839a53e0cSJaegeuk Kim 
27997a2af766SChao Yu static inline int offset_in_addr(struct f2fs_inode *i)
28007a2af766SChao Yu {
28017a2af766SChao Yu 	return (i->i_inline & F2FS_EXTRA_ATTR) ?
28027a2af766SChao Yu 			(le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0;
28037a2af766SChao Yu }
28047a2af766SChao Yu 
280539a53e0cSJaegeuk Kim static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
280639a53e0cSJaegeuk Kim {
280739a53e0cSJaegeuk Kim 	return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
280839a53e0cSJaegeuk Kim }
280939a53e0cSJaegeuk Kim 
28107a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode);
2811a2ced1ceSChao Yu static inline block_t data_blkaddr(struct inode *inode,
28127a2af766SChao Yu 			struct page *node_page, unsigned int offset)
281339a53e0cSJaegeuk Kim {
281439a53e0cSJaegeuk Kim 	struct f2fs_node *raw_node;
281539a53e0cSJaegeuk Kim 	__le32 *addr_array;
28167a2af766SChao Yu 	int base = 0;
28177a2af766SChao Yu 	bool is_inode = IS_INODE(node_page);
2818cac5a3d8SDongOh Shin 
281945590710SGu Zheng 	raw_node = F2FS_NODE(node_page);
28207a2af766SChao Yu 
2821979f492fSLiFan 	if (is_inode) {
2822979f492fSLiFan 		if (!inode)
28237a88ddb5SChao Yu 			/* from GC path only */
28247a2af766SChao Yu 			base = offset_in_addr(&raw_node->i);
2825979f492fSLiFan 		else if (f2fs_has_extra_attr(inode))
28267a2af766SChao Yu 			base = get_extra_isize(inode);
28277a2af766SChao Yu 	}
28287a2af766SChao Yu 
282939a53e0cSJaegeuk Kim 	addr_array = blkaddr_in_node(raw_node);
28307a2af766SChao Yu 	return le32_to_cpu(addr_array[base + offset]);
283139a53e0cSJaegeuk Kim }
283239a53e0cSJaegeuk Kim 
2833a2ced1ceSChao Yu static inline block_t f2fs_data_blkaddr(struct dnode_of_data *dn)
2834a2ced1ceSChao Yu {
2835a2ced1ceSChao Yu 	return data_blkaddr(dn->inode, dn->node_page, dn->ofs_in_node);
2836a2ced1ceSChao Yu }
2837a2ced1ceSChao Yu 
283839a53e0cSJaegeuk Kim static inline int f2fs_test_bit(unsigned int nr, char *addr)
283939a53e0cSJaegeuk Kim {
284039a53e0cSJaegeuk Kim 	int mask;
284139a53e0cSJaegeuk Kim 
284239a53e0cSJaegeuk Kim 	addr += (nr >> 3);
284339a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
284439a53e0cSJaegeuk Kim 	return mask & *addr;
284539a53e0cSJaegeuk Kim }
284639a53e0cSJaegeuk Kim 
2847a66cdd98SJaegeuk Kim static inline void f2fs_set_bit(unsigned int nr, char *addr)
2848a66cdd98SJaegeuk Kim {
2849a66cdd98SJaegeuk Kim 	int mask;
2850a66cdd98SJaegeuk Kim 
2851a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2852a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2853a66cdd98SJaegeuk Kim 	*addr |= mask;
2854a66cdd98SJaegeuk Kim }
2855a66cdd98SJaegeuk Kim 
2856a66cdd98SJaegeuk Kim static inline void f2fs_clear_bit(unsigned int nr, char *addr)
2857a66cdd98SJaegeuk Kim {
2858a66cdd98SJaegeuk Kim 	int mask;
2859a66cdd98SJaegeuk Kim 
2860a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2861a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2862a66cdd98SJaegeuk Kim 	*addr &= ~mask;
2863a66cdd98SJaegeuk Kim }
2864a66cdd98SJaegeuk Kim 
286552aca074SGu Zheng static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
286639a53e0cSJaegeuk Kim {
286739a53e0cSJaegeuk Kim 	int mask;
286839a53e0cSJaegeuk Kim 	int ret;
286939a53e0cSJaegeuk Kim 
287039a53e0cSJaegeuk Kim 	addr += (nr >> 3);
287139a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
287239a53e0cSJaegeuk Kim 	ret = mask & *addr;
287339a53e0cSJaegeuk Kim 	*addr |= mask;
287439a53e0cSJaegeuk Kim 	return ret;
287539a53e0cSJaegeuk Kim }
287639a53e0cSJaegeuk Kim 
287752aca074SGu Zheng static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
287839a53e0cSJaegeuk Kim {
287939a53e0cSJaegeuk Kim 	int mask;
288039a53e0cSJaegeuk Kim 	int ret;
288139a53e0cSJaegeuk Kim 
288239a53e0cSJaegeuk Kim 	addr += (nr >> 3);
288339a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
288439a53e0cSJaegeuk Kim 	ret = mask & *addr;
288539a53e0cSJaegeuk Kim 	*addr &= ~mask;
288639a53e0cSJaegeuk Kim 	return ret;
288739a53e0cSJaegeuk Kim }
288839a53e0cSJaegeuk Kim 
2889c6ac4c0eSGu Zheng static inline void f2fs_change_bit(unsigned int nr, char *addr)
2890c6ac4c0eSGu Zheng {
2891c6ac4c0eSGu Zheng 	int mask;
2892c6ac4c0eSGu Zheng 
2893c6ac4c0eSGu Zheng 	addr += (nr >> 3);
2894c6ac4c0eSGu Zheng 	mask = 1 << (7 - (nr & 0x07));
2895c6ac4c0eSGu Zheng 	*addr ^= mask;
2896c6ac4c0eSGu Zheng }
2897c6ac4c0eSGu Zheng 
289859c84408SChao Yu /*
289936098557SEric Biggers  * On-disk inode flags (f2fs_inode::i_flags)
290059c84408SChao Yu  */
29014c8ff709SChao Yu #define F2FS_COMPR_FL			0x00000004 /* Compress file */
290259c84408SChao Yu #define F2FS_SYNC_FL			0x00000008 /* Synchronous updates */
290359c84408SChao Yu #define F2FS_IMMUTABLE_FL		0x00000010 /* Immutable file */
290459c84408SChao Yu #define F2FS_APPEND_FL			0x00000020 /* writes to file may only append */
290559c84408SChao Yu #define F2FS_NODUMP_FL			0x00000040 /* do not dump file */
290659c84408SChao Yu #define F2FS_NOATIME_FL			0x00000080 /* do not update atime */
29074c8ff709SChao Yu #define F2FS_NOCOMP_FL			0x00000400 /* Don't compress */
290859c84408SChao Yu #define F2FS_INDEX_FL			0x00001000 /* hash-indexed directory */
290959c84408SChao Yu #define F2FS_DIRSYNC_FL			0x00010000 /* dirsync behaviour (directories only) */
291059c84408SChao Yu #define F2FS_PROJINHERIT_FL		0x20000000 /* Create with parents projid */
29112c2eb7a3SDaniel Rosenberg #define F2FS_CASEFOLD_FL		0x40000000 /* Casefolded file */
291259c84408SChao Yu 
291359c84408SChao Yu /* Flags that should be inherited by new inodes from their parent. */
291436098557SEric Biggers #define F2FS_FL_INHERITED (F2FS_SYNC_FL | F2FS_NODUMP_FL | F2FS_NOATIME_FL | \
29152c2eb7a3SDaniel Rosenberg 			   F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL | \
29164c8ff709SChao Yu 			   F2FS_CASEFOLD_FL | F2FS_COMPR_FL | F2FS_NOCOMP_FL)
291759c84408SChao Yu 
291859c84408SChao Yu /* Flags that are appropriate for regular files (all but dir-specific ones). */
29192c2eb7a3SDaniel Rosenberg #define F2FS_REG_FLMASK		(~(F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL | \
29202c2eb7a3SDaniel Rosenberg 				F2FS_CASEFOLD_FL))
292159c84408SChao Yu 
292259c84408SChao Yu /* Flags that are appropriate for non-directories/regular files. */
292359c84408SChao Yu #define F2FS_OTHER_FLMASK	(F2FS_NODUMP_FL | F2FS_NOATIME_FL)
29245c57132eSChao Yu 
29255c57132eSChao Yu static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
29265c57132eSChao Yu {
29275c57132eSChao Yu 	if (S_ISDIR(mode))
29285c57132eSChao Yu 		return flags;
29295c57132eSChao Yu 	else if (S_ISREG(mode))
29305c57132eSChao Yu 		return flags & F2FS_REG_FLMASK;
29315c57132eSChao Yu 	else
29325c57132eSChao Yu 		return flags & F2FS_OTHER_FLMASK;
29335c57132eSChao Yu }
29345c57132eSChao Yu 
2935205b9822SJaegeuk Kim static inline void __mark_inode_dirty_flag(struct inode *inode,
2936205b9822SJaegeuk Kim 						int flag, bool set)
293739a53e0cSJaegeuk Kim {
2938205b9822SJaegeuk Kim 	switch (flag) {
2939205b9822SJaegeuk Kim 	case FI_INLINE_XATTR:
2940205b9822SJaegeuk Kim 	case FI_INLINE_DATA:
2941205b9822SJaegeuk Kim 	case FI_INLINE_DENTRY:
29429ac1e2d8SDaeho Jeong 	case FI_NEW_INODE:
2943205b9822SJaegeuk Kim 		if (set)
2944205b9822SJaegeuk Kim 			return;
2945df561f66SGustavo A. R. Silva 		fallthrough;
2946205b9822SJaegeuk Kim 	case FI_DATA_EXIST:
2947205b9822SJaegeuk Kim 	case FI_INLINE_DOTS:
29481ad71a27SJaegeuk Kim 	case FI_PIN_FILE:
2949c6140415SJaegeuk Kim 	case FI_COMPRESS_RELEASED:
29507c45729aSJaegeuk Kim 		f2fs_mark_inode_dirty_sync(inode, true);
2951205b9822SJaegeuk Kim 	}
295239a53e0cSJaegeuk Kim }
295339a53e0cSJaegeuk Kim 
295491942321SJaegeuk Kim static inline void set_inode_flag(struct inode *inode, int flag)
295539a53e0cSJaegeuk Kim {
295658f7e00fSYufen Yu 	set_bit(flag, F2FS_I(inode)->flags);
2957205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, true);
295839a53e0cSJaegeuk Kim }
295939a53e0cSJaegeuk Kim 
296091942321SJaegeuk Kim static inline int is_inode_flag_set(struct inode *inode, int flag)
296139a53e0cSJaegeuk Kim {
29627653b9d8SChao Yu 	return test_bit(flag, F2FS_I(inode)->flags);
296339a53e0cSJaegeuk Kim }
296439a53e0cSJaegeuk Kim 
296591942321SJaegeuk Kim static inline void clear_inode_flag(struct inode *inode, int flag)
296639a53e0cSJaegeuk Kim {
296758f7e00fSYufen Yu 	clear_bit(flag, F2FS_I(inode)->flags);
2968205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, false);
296939a53e0cSJaegeuk Kim }
297039a53e0cSJaegeuk Kim 
297195ae251fSEric Biggers static inline bool f2fs_verity_in_progress(struct inode *inode)
297295ae251fSEric Biggers {
297395ae251fSEric Biggers 	return IS_ENABLED(CONFIG_FS_VERITY) &&
297495ae251fSEric Biggers 	       is_inode_flag_set(inode, FI_VERITY_IN_PROGRESS);
297595ae251fSEric Biggers }
297695ae251fSEric Biggers 
297791942321SJaegeuk Kim static inline void set_acl_inode(struct inode *inode, umode_t mode)
2978444c580fSJaegeuk Kim {
297991942321SJaegeuk Kim 	F2FS_I(inode)->i_acl_mode = mode;
298091942321SJaegeuk Kim 	set_inode_flag(inode, FI_ACL_MODE);
29817c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, false);
298239a53e0cSJaegeuk Kim }
298339a53e0cSJaegeuk Kim 
2984a1961246SJaegeuk Kim static inline void f2fs_i_links_write(struct inode *inode, bool inc)
2985a1961246SJaegeuk Kim {
2986a1961246SJaegeuk Kim 	if (inc)
2987a1961246SJaegeuk Kim 		inc_nlink(inode);
2988a1961246SJaegeuk Kim 	else
2989a1961246SJaegeuk Kim 		drop_nlink(inode);
29907c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2991a1961246SJaegeuk Kim }
2992a1961246SJaegeuk Kim 
29938edd03c8SJaegeuk Kim static inline void f2fs_i_blocks_write(struct inode *inode,
29940abd675eSChao Yu 					block_t diff, bool add, bool claim)
29958edd03c8SJaegeuk Kim {
299626de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
299726de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
299826de9b11SJaegeuk Kim 
29990abd675eSChao Yu 	/* add = 1, claim = 1 should be dquot_reserve_block in pair */
30000abd675eSChao Yu 	if (add) {
30010abd675eSChao Yu 		if (claim)
30020abd675eSChao Yu 			dquot_claim_block(inode, diff);
30030abd675eSChao Yu 		else
30040abd675eSChao Yu 			dquot_alloc_block_nofail(inode, diff);
30050abd675eSChao Yu 	} else {
30060abd675eSChao Yu 		dquot_free_block(inode, diff);
30070abd675eSChao Yu 	}
30080abd675eSChao Yu 
30097c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
301026de9b11SJaegeuk Kim 	if (clean || recover)
301126de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
30128edd03c8SJaegeuk Kim }
30138edd03c8SJaegeuk Kim 
3014fc9581c8SJaegeuk Kim static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
3015fc9581c8SJaegeuk Kim {
301626de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
301726de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
301826de9b11SJaegeuk Kim 
3019fc9581c8SJaegeuk Kim 	if (i_size_read(inode) == i_size)
3020fc9581c8SJaegeuk Kim 		return;
3021fc9581c8SJaegeuk Kim 
3022fc9581c8SJaegeuk Kim 	i_size_write(inode, i_size);
30237c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
302426de9b11SJaegeuk Kim 	if (clean || recover)
302526de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
302626de9b11SJaegeuk Kim }
302726de9b11SJaegeuk Kim 
3028205b9822SJaegeuk Kim static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
3029205b9822SJaegeuk Kim {
3030205b9822SJaegeuk Kim 	F2FS_I(inode)->i_current_depth = depth;
30317c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3032205b9822SJaegeuk Kim }
3033205b9822SJaegeuk Kim 
30341ad71a27SJaegeuk Kim static inline void f2fs_i_gc_failures_write(struct inode *inode,
30351ad71a27SJaegeuk Kim 					unsigned int count)
30361ad71a27SJaegeuk Kim {
30372ef79ecbSChao Yu 	F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN] = count;
30381ad71a27SJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
30391ad71a27SJaegeuk Kim }
30401ad71a27SJaegeuk Kim 
3041205b9822SJaegeuk Kim static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
3042205b9822SJaegeuk Kim {
3043205b9822SJaegeuk Kim 	F2FS_I(inode)->i_xattr_nid = xnid;
30447c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3045205b9822SJaegeuk Kim }
3046205b9822SJaegeuk Kim 
3047205b9822SJaegeuk Kim static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
3048205b9822SJaegeuk Kim {
3049205b9822SJaegeuk Kim 	F2FS_I(inode)->i_pino = pino;
30507c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3051205b9822SJaegeuk Kim }
3052205b9822SJaegeuk Kim 
305391942321SJaegeuk Kim static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
3054444c580fSJaegeuk Kim {
3055205b9822SJaegeuk Kim 	struct f2fs_inode_info *fi = F2FS_I(inode);
3056205b9822SJaegeuk Kim 
3057444c580fSJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_XATTR)
30587653b9d8SChao Yu 		set_bit(FI_INLINE_XATTR, fi->flags);
30591001b347SHuajun Li 	if (ri->i_inline & F2FS_INLINE_DATA)
30607653b9d8SChao Yu 		set_bit(FI_INLINE_DATA, fi->flags);
306134d67debSChao Yu 	if (ri->i_inline & F2FS_INLINE_DENTRY)
30627653b9d8SChao Yu 		set_bit(FI_INLINE_DENTRY, fi->flags);
3063b3d208f9SJaegeuk Kim 	if (ri->i_inline & F2FS_DATA_EXIST)
30647653b9d8SChao Yu 		set_bit(FI_DATA_EXIST, fi->flags);
3065510022a8SJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_DOTS)
30667653b9d8SChao Yu 		set_bit(FI_INLINE_DOTS, fi->flags);
30677a2af766SChao Yu 	if (ri->i_inline & F2FS_EXTRA_ATTR)
30687653b9d8SChao Yu 		set_bit(FI_EXTRA_ATTR, fi->flags);
30691ad71a27SJaegeuk Kim 	if (ri->i_inline & F2FS_PIN_FILE)
30707653b9d8SChao Yu 		set_bit(FI_PIN_FILE, fi->flags);
3071c6140415SJaegeuk Kim 	if (ri->i_inline & F2FS_COMPRESS_RELEASED)
3072c6140415SJaegeuk Kim 		set_bit(FI_COMPRESS_RELEASED, fi->flags);
3073444c580fSJaegeuk Kim }
3074444c580fSJaegeuk Kim 
307591942321SJaegeuk Kim static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
3076444c580fSJaegeuk Kim {
3077444c580fSJaegeuk Kim 	ri->i_inline = 0;
3078444c580fSJaegeuk Kim 
307991942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_XATTR))
3080444c580fSJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_XATTR;
308191942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DATA))
30821001b347SHuajun Li 		ri->i_inline |= F2FS_INLINE_DATA;
308391942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
308434d67debSChao Yu 		ri->i_inline |= F2FS_INLINE_DENTRY;
308591942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_DATA_EXIST))
3086b3d208f9SJaegeuk Kim 		ri->i_inline |= F2FS_DATA_EXIST;
308791942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DOTS))
3088510022a8SJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_DOTS;
30897a2af766SChao Yu 	if (is_inode_flag_set(inode, FI_EXTRA_ATTR))
30907a2af766SChao Yu 		ri->i_inline |= F2FS_EXTRA_ATTR;
30911ad71a27SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_PIN_FILE))
30921ad71a27SJaegeuk Kim 		ri->i_inline |= F2FS_PIN_FILE;
3093c6140415SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
3094c6140415SJaegeuk Kim 		ri->i_inline |= F2FS_COMPRESS_RELEASED;
30957a2af766SChao Yu }
30967a2af766SChao Yu 
30977a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode)
30987a2af766SChao Yu {
30997a2af766SChao Yu 	return is_inode_flag_set(inode, FI_EXTRA_ATTR);
3100444c580fSJaegeuk Kim }
3101444c580fSJaegeuk Kim 
3102987c7c31SChao Yu static inline int f2fs_has_inline_xattr(struct inode *inode)
3103987c7c31SChao Yu {
310491942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_XATTR);
3105987c7c31SChao Yu }
3106987c7c31SChao Yu 
31074c8ff709SChao Yu static inline int f2fs_compressed_file(struct inode *inode)
31084c8ff709SChao Yu {
31094c8ff709SChao Yu 	return S_ISREG(inode->i_mode) &&
31104c8ff709SChao Yu 		is_inode_flag_set(inode, FI_COMPRESSED_FILE);
31114c8ff709SChao Yu }
31124c8ff709SChao Yu 
3113602a16d5SDaeho Jeong static inline bool f2fs_need_compress_data(struct inode *inode)
3114602a16d5SDaeho Jeong {
3115602a16d5SDaeho Jeong 	int compress_mode = F2FS_OPTION(F2FS_I_SB(inode)).compress_mode;
3116602a16d5SDaeho Jeong 
3117602a16d5SDaeho Jeong 	if (!f2fs_compressed_file(inode))
3118602a16d5SDaeho Jeong 		return false;
3119602a16d5SDaeho Jeong 
3120602a16d5SDaeho Jeong 	if (compress_mode == COMPR_MODE_FS)
3121602a16d5SDaeho Jeong 		return true;
3122602a16d5SDaeho Jeong 	else if (compress_mode == COMPR_MODE_USER &&
3123602a16d5SDaeho Jeong 			is_inode_flag_set(inode, FI_ENABLE_COMPRESS))
3124602a16d5SDaeho Jeong 		return true;
3125602a16d5SDaeho Jeong 
3126602a16d5SDaeho Jeong 	return false;
3127602a16d5SDaeho Jeong }
3128602a16d5SDaeho Jeong 
312981ca7350SChao Yu static inline unsigned int addrs_per_inode(struct inode *inode)
3130de93653fSJaegeuk Kim {
3131d02a6e61SChao Yu 	unsigned int addrs = CUR_ADDRS_PER_INODE(inode) -
3132d02a6e61SChao Yu 				get_inline_xattr_addrs(inode);
31334c8ff709SChao Yu 
31344c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
31354c8ff709SChao Yu 		return addrs;
31364c8ff709SChao Yu 	return ALIGN_DOWN(addrs, F2FS_I(inode)->i_cluster_size);
3137d02a6e61SChao Yu }
3138d02a6e61SChao Yu 
3139d02a6e61SChao Yu static inline unsigned int addrs_per_block(struct inode *inode)
3140d02a6e61SChao Yu {
31414c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
31424c8ff709SChao Yu 		return DEF_ADDRS_PER_BLOCK;
31434c8ff709SChao Yu 	return ALIGN_DOWN(DEF_ADDRS_PER_BLOCK, F2FS_I(inode)->i_cluster_size);
3144de93653fSJaegeuk Kim }
3145de93653fSJaegeuk Kim 
31466afc662eSChao Yu static inline void *inline_xattr_addr(struct inode *inode, struct page *page)
314765985d93SJaegeuk Kim {
3148695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
3149cac5a3d8SDongOh Shin 
315065985d93SJaegeuk Kim 	return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
3151b323fd28SChao Yu 					get_inline_xattr_addrs(inode)]);
315265985d93SJaegeuk Kim }
315365985d93SJaegeuk Kim 
315465985d93SJaegeuk Kim static inline int inline_xattr_size(struct inode *inode)
315565985d93SJaegeuk Kim {
3156622927f3SChao Yu 	if (f2fs_has_inline_xattr(inode))
31576afc662eSChao Yu 		return get_inline_xattr_addrs(inode) * sizeof(__le32);
3158622927f3SChao Yu 	return 0;
315965985d93SJaegeuk Kim }
316065985d93SJaegeuk Kim 
31610dbdc2aeSJaegeuk Kim static inline int f2fs_has_inline_data(struct inode *inode)
31620dbdc2aeSJaegeuk Kim {
316391942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DATA);
31640dbdc2aeSJaegeuk Kim }
31650dbdc2aeSJaegeuk Kim 
3166b3d208f9SJaegeuk Kim static inline int f2fs_exist_data(struct inode *inode)
3167b3d208f9SJaegeuk Kim {
316891942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DATA_EXIST);
3169b3d208f9SJaegeuk Kim }
3170b3d208f9SJaegeuk Kim 
3171510022a8SJaegeuk Kim static inline int f2fs_has_inline_dots(struct inode *inode)
3172510022a8SJaegeuk Kim {
317391942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DOTS);
3174510022a8SJaegeuk Kim }
3175510022a8SJaegeuk Kim 
31764c8ff709SChao Yu static inline int f2fs_is_mmap_file(struct inode *inode)
31774c8ff709SChao Yu {
31784c8ff709SChao Yu 	return is_inode_flag_set(inode, FI_MMAP_FILE);
31794c8ff709SChao Yu }
31804c8ff709SChao Yu 
31811ad71a27SJaegeuk Kim static inline bool f2fs_is_pinned_file(struct inode *inode)
31821ad71a27SJaegeuk Kim {
31831ad71a27SJaegeuk Kim 	return is_inode_flag_set(inode, FI_PIN_FILE);
31841ad71a27SJaegeuk Kim }
31851ad71a27SJaegeuk Kim 
318688b88a66SJaegeuk Kim static inline bool f2fs_is_atomic_file(struct inode *inode)
318788b88a66SJaegeuk Kim {
318891942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_ATOMIC_FILE);
318988b88a66SJaegeuk Kim }
319088b88a66SJaegeuk Kim 
31915fe45743SChao Yu static inline bool f2fs_is_commit_atomic_write(struct inode *inode)
31925fe45743SChao Yu {
31935fe45743SChao Yu 	return is_inode_flag_set(inode, FI_ATOMIC_COMMIT);
31945fe45743SChao Yu }
31955fe45743SChao Yu 
319602a1335fSJaegeuk Kim static inline bool f2fs_is_volatile_file(struct inode *inode)
319702a1335fSJaegeuk Kim {
319891942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_VOLATILE_FILE);
319902a1335fSJaegeuk Kim }
320002a1335fSJaegeuk Kim 
32013c6c2bebSJaegeuk Kim static inline bool f2fs_is_first_block_written(struct inode *inode)
32023c6c2bebSJaegeuk Kim {
320391942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
32043c6c2bebSJaegeuk Kim }
32053c6c2bebSJaegeuk Kim 
32061e84371fSJaegeuk Kim static inline bool f2fs_is_drop_cache(struct inode *inode)
32071e84371fSJaegeuk Kim {
320891942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DROP_CACHE);
32091e84371fSJaegeuk Kim }
32101e84371fSJaegeuk Kim 
3211f2470371SChao Yu static inline void *inline_data_addr(struct inode *inode, struct page *page)
32121001b347SHuajun Li {
3213695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
32147a2af766SChao Yu 	int extra_size = get_extra_isize(inode);
3215cac5a3d8SDongOh Shin 
32167a2af766SChao Yu 	return (void *)&(ri->i_addr[extra_size + DEF_INLINE_RESERVED_SIZE]);
32171001b347SHuajun Li }
32181001b347SHuajun Li 
321934d67debSChao Yu static inline int f2fs_has_inline_dentry(struct inode *inode)
322034d67debSChao Yu {
322191942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DENTRY);
322234d67debSChao Yu }
322334d67debSChao Yu 
3224b5492af7SJaegeuk Kim static inline int is_file(struct inode *inode, int type)
3225b5492af7SJaegeuk Kim {
3226b5492af7SJaegeuk Kim 	return F2FS_I(inode)->i_advise & type;
3227b5492af7SJaegeuk Kim }
3228b5492af7SJaegeuk Kim 
3229b5492af7SJaegeuk Kim static inline void set_file(struct inode *inode, int type)
3230b5492af7SJaegeuk Kim {
3231766c6639SJaegeuk Kim 	if (is_file(inode, type))
3232766c6639SJaegeuk Kim 		return;
3233b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise |= type;
32347c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3235b5492af7SJaegeuk Kim }
3236b5492af7SJaegeuk Kim 
3237b5492af7SJaegeuk Kim static inline void clear_file(struct inode *inode, int type)
3238b5492af7SJaegeuk Kim {
3239766c6639SJaegeuk Kim 	if (!is_file(inode, type))
3240766c6639SJaegeuk Kim 		return;
3241b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise &= ~type;
32427c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3243b5492af7SJaegeuk Kim }
3244b5492af7SJaegeuk Kim 
3245fe1897eaSChao Yu static inline bool f2fs_is_time_consistent(struct inode *inode)
3246fe1897eaSChao Yu {
3247fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time, &inode->i_atime))
3248fe1897eaSChao Yu 		return false;
3249fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 1, &inode->i_ctime))
3250fe1897eaSChao Yu 		return false;
3251fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 2, &inode->i_mtime))
3252fe1897eaSChao Yu 		return false;
3253fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 3,
3254fe1897eaSChao Yu 						&F2FS_I(inode)->i_crtime))
3255fe1897eaSChao Yu 		return false;
3256fe1897eaSChao Yu 	return true;
3257fe1897eaSChao Yu }
3258fe1897eaSChao Yu 
325926787236SJaegeuk Kim static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
326026787236SJaegeuk Kim {
3261a0d00fadSChao Yu 	bool ret;
3262a0d00fadSChao Yu 
326326787236SJaegeuk Kim 	if (dsync) {
326426787236SJaegeuk Kim 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
326526787236SJaegeuk Kim 
326626787236SJaegeuk Kim 		spin_lock(&sbi->inode_lock[DIRTY_META]);
326726787236SJaegeuk Kim 		ret = list_empty(&F2FS_I(inode)->gdirty_list);
326826787236SJaegeuk Kim 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
326926787236SJaegeuk Kim 		return ret;
327026787236SJaegeuk Kim 	}
327126787236SJaegeuk Kim 	if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
327226787236SJaegeuk Kim 			file_keep_isize(inode) ||
3273235831d7SJunling Zheng 			i_size_read(inode) & ~PAGE_MASK)
327426787236SJaegeuk Kim 		return false;
3275a0d00fadSChao Yu 
3276fe1897eaSChao Yu 	if (!f2fs_is_time_consistent(inode))
3277214c2461SJaegeuk Kim 		return false;
3278214c2461SJaegeuk Kim 
3279c10c9820SChao Yu 	spin_lock(&F2FS_I(inode)->i_size_lock);
3280a0d00fadSChao Yu 	ret = F2FS_I(inode)->last_disk_size == i_size_read(inode);
3281c10c9820SChao Yu 	spin_unlock(&F2FS_I(inode)->i_size_lock);
3282a0d00fadSChao Yu 
3283a0d00fadSChao Yu 	return ret;
3284267378d4SChao Yu }
3285267378d4SChao Yu 
3286deeedd71SChao Yu static inline bool f2fs_readonly(struct super_block *sb)
328777888c1eSJaegeuk Kim {
3288deeedd71SChao Yu 	return sb_rdonly(sb);
328977888c1eSJaegeuk Kim }
329077888c1eSJaegeuk Kim 
3291744602cfSJaegeuk Kim static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
3292744602cfSJaegeuk Kim {
3293aaec2b1dSChao Yu 	return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
3294744602cfSJaegeuk Kim }
3295744602cfSJaegeuk Kim 
329643c780baSEric Biggers static inline bool is_dot_dotdot(const u8 *name, size_t len)
3297eaa693f4SJaegeuk Kim {
329843c780baSEric Biggers 	if (len == 1 && name[0] == '.')
3299eaa693f4SJaegeuk Kim 		return true;
3300eaa693f4SJaegeuk Kim 
330143c780baSEric Biggers 	if (len == 2 && name[0] == '.' && name[1] == '.')
3302eaa693f4SJaegeuk Kim 		return true;
3303eaa693f4SJaegeuk Kim 
3304eaa693f4SJaegeuk Kim 	return false;
3305eaa693f4SJaegeuk Kim }
3306eaa693f4SJaegeuk Kim 
33071ecc0c5cSChao Yu static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
33081ecc0c5cSChao Yu 					size_t size, gfp_t flags)
33090414b004SJaegeuk Kim {
331055523519SChao Yu 	if (time_to_inject(sbi, FAULT_KMALLOC)) {
3311c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_KMALLOC);
33122c63feadSJaegeuk Kim 		return NULL;
331355523519SChao Yu 	}
33147fa750a1SArnd Bergmann 
33150b6d4ca0SEric Biggers 	return kmalloc(size, flags);
33160414b004SJaegeuk Kim }
33170414b004SJaegeuk Kim 
3318acbf054dSChao Yu static inline void *f2fs_kzalloc(struct f2fs_sb_info *sbi,
3319acbf054dSChao Yu 					size_t size, gfp_t flags)
3320acbf054dSChao Yu {
3321acbf054dSChao Yu 	return f2fs_kmalloc(sbi, size, flags | __GFP_ZERO);
3322acbf054dSChao Yu }
3323acbf054dSChao Yu 
3324628b3d14SChao Yu static inline void *f2fs_kvmalloc(struct f2fs_sb_info *sbi,
3325628b3d14SChao Yu 					size_t size, gfp_t flags)
3326628b3d14SChao Yu {
3327628b3d14SChao Yu 	if (time_to_inject(sbi, FAULT_KVMALLOC)) {
3328c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_KVMALLOC);
3329628b3d14SChao Yu 		return NULL;
3330628b3d14SChao Yu 	}
33317fa750a1SArnd Bergmann 
3332628b3d14SChao Yu 	return kvmalloc(size, flags);
3333628b3d14SChao Yu }
3334628b3d14SChao Yu 
3335628b3d14SChao Yu static inline void *f2fs_kvzalloc(struct f2fs_sb_info *sbi,
3336628b3d14SChao Yu 					size_t size, gfp_t flags)
3337628b3d14SChao Yu {
3338628b3d14SChao Yu 	return f2fs_kvmalloc(sbi, size, flags | __GFP_ZERO);
3339628b3d14SChao Yu }
3340628b3d14SChao Yu 
33417a2af766SChao Yu static inline int get_extra_isize(struct inode *inode)
3342f2470371SChao Yu {
33437a2af766SChao Yu 	return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
3344f2470371SChao Yu }
3345f2470371SChao Yu 
33466afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode)
33476afc662eSChao Yu {
33486afc662eSChao Yu 	return F2FS_I(inode)->i_inline_xattr_size;
33496afc662eSChao Yu }
33506afc662eSChao Yu 
33514d57b86dSChao Yu #define f2fs_get_inode_mode(i) \
335291942321SJaegeuk Kim 	((is_inode_flag_set(i, FI_ACL_MODE)) ? \
3353a6dda0e6SChristoph Hellwig 	 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
3354a6dda0e6SChristoph Hellwig 
33557a2af766SChao Yu #define F2FS_TOTAL_EXTRA_ATTR_SIZE			\
33567a2af766SChao Yu 	(offsetof(struct f2fs_inode, i_extra_end) -	\
33577a2af766SChao Yu 	offsetof(struct f2fs_inode, i_extra_isize))	\
33587a2af766SChao Yu 
33595c57132eSChao Yu #define F2FS_OLD_ATTRIBUTE_SIZE	(offsetof(struct f2fs_inode, i_addr))
33605c57132eSChao Yu #define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field)		\
33612f84babfSSheng Yong 		((offsetof(typeof(*(f2fs_inode)), field) +	\
33625c57132eSChao Yu 		sizeof((f2fs_inode)->field))			\
33632f84babfSSheng Yong 		<= (F2FS_OLD_ATTRIBUTE_SIZE + (extra_isize)))	\
33645c57132eSChao Yu 
33652c70c5e3SChao Yu #define __is_large_section(sbi)		((sbi)->segs_per_sec > 1)
33662c70c5e3SChao Yu 
33676dc3a126SChao Yu #define __is_meta_io(fio) (PAGE_TYPE_OF_BIO((fio)->type) == META)
3368c9b60788SChao Yu 
3369e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
3370e1da7872SChao Yu 					block_t blkaddr, int type);
3371e1da7872SChao Yu static inline void verify_blkaddr(struct f2fs_sb_info *sbi,
3372e1da7872SChao Yu 					block_t blkaddr, int type)
3373e1da7872SChao Yu {
3374e1da7872SChao Yu 	if (!f2fs_is_valid_blkaddr(sbi, blkaddr, type)) {
3375dcbb4c10SJoe Perches 		f2fs_err(sbi, "invalid blkaddr: %u, type: %d, run fsck to fix.",
3376e1da7872SChao Yu 			 blkaddr, type);
3377e1da7872SChao Yu 		f2fs_bug_on(sbi, 1);
3378e1da7872SChao Yu 	}
3379e1da7872SChao Yu }
3380e1da7872SChao Yu 
3381e1da7872SChao Yu static inline bool __is_valid_data_blkaddr(block_t blkaddr)
33827b525dd0SChao Yu {
33834c8ff709SChao Yu 	if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR ||
33844c8ff709SChao Yu 			blkaddr == COMPRESS_ADDR)
33857b525dd0SChao Yu 		return false;
33867b525dd0SChao Yu 	return true;
33877b525dd0SChao Yu }
33887b525dd0SChao Yu 
338939a53e0cSJaegeuk Kim /*
339039a53e0cSJaegeuk Kim  * file.c
339139a53e0cSJaegeuk Kim  */
3392cac5a3d8SDongOh Shin int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
33934d57b86dSChao Yu void f2fs_truncate_data_blocks(struct dnode_of_data *dn);
33943265d3dbSChao Yu int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock);
3395c42d28ceSChao Yu int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock);
3396cac5a3d8SDongOh Shin int f2fs_truncate(struct inode *inode);
3397549c7297SChristian Brauner int f2fs_getattr(struct user_namespace *mnt_userns, const struct path *path,
3398549c7297SChristian Brauner 		 struct kstat *stat, u32 request_mask, unsigned int flags);
3399549c7297SChristian Brauner int f2fs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
3400549c7297SChristian Brauner 		 struct iattr *attr);
34014d57b86dSChao Yu int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
34024d57b86dSChao Yu void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count);
3403c4020b2dSChao Yu int f2fs_precache_extents(struct inode *inode);
34049b1bb01cSMiklos Szeredi int f2fs_fileattr_get(struct dentry *dentry, struct fileattr *fa);
34059b1bb01cSMiklos Szeredi int f2fs_fileattr_set(struct user_namespace *mnt_userns,
34069b1bb01cSMiklos Szeredi 		      struct dentry *dentry, struct fileattr *fa);
3407cac5a3d8SDongOh Shin long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
3408cac5a3d8SDongOh Shin long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
340978130819SChao Yu int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid);
34101ad71a27SJaegeuk Kim int f2fs_pin_file_control(struct inode *inode, bool inc);
341139a53e0cSJaegeuk Kim 
341239a53e0cSJaegeuk Kim /*
341339a53e0cSJaegeuk Kim  * inode.c
341439a53e0cSJaegeuk Kim  */
3415cac5a3d8SDongOh Shin void f2fs_set_inode_flags(struct inode *inode);
3416704956ecSChao Yu bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page);
3417704956ecSChao Yu void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page);
3418cac5a3d8SDongOh Shin struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
3419cac5a3d8SDongOh Shin struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
34204d57b86dSChao Yu int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
34214d57b86dSChao Yu void f2fs_update_inode(struct inode *inode, struct page *node_page);
34224d57b86dSChao Yu void f2fs_update_inode_page(struct inode *inode);
3423cac5a3d8SDongOh Shin int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
3424cac5a3d8SDongOh Shin void f2fs_evict_inode(struct inode *inode);
34254d57b86dSChao Yu void f2fs_handle_failed_inode(struct inode *inode);
342639a53e0cSJaegeuk Kim 
342739a53e0cSJaegeuk Kim /*
342839a53e0cSJaegeuk Kim  * namei.c
342939a53e0cSJaegeuk Kim  */
34304d57b86dSChao Yu int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
3431b6a06cbbSChao Yu 							bool hot, bool set);
343239a53e0cSJaegeuk Kim struct dentry *f2fs_get_parent(struct dentry *child);
343339a53e0cSJaegeuk Kim 
343439a53e0cSJaegeuk Kim /*
343539a53e0cSJaegeuk Kim  * dir.c
343639a53e0cSJaegeuk Kim  */
34374d57b86dSChao Yu unsigned char f2fs_get_de_type(struct f2fs_dir_entry *de);
343843c780baSEric Biggers int f2fs_init_casefolded_name(const struct inode *dir,
343943c780baSEric Biggers 			      struct f2fs_filename *fname);
344043c780baSEric Biggers int f2fs_setup_filename(struct inode *dir, const struct qstr *iname,
344143c780baSEric Biggers 			int lookup, struct f2fs_filename *fname);
344243c780baSEric Biggers int f2fs_prepare_lookup(struct inode *dir, struct dentry *dentry,
344343c780baSEric Biggers 			struct f2fs_filename *fname);
344443c780baSEric Biggers void f2fs_free_filename(struct f2fs_filename *fname);
344543c780baSEric Biggers struct f2fs_dir_entry *f2fs_find_target_dentry(const struct f2fs_dentry_ptr *d,
344643c780baSEric Biggers 			const struct f2fs_filename *fname, int *max_slots);
3447cac5a3d8SDongOh Shin int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
3448cac5a3d8SDongOh Shin 			unsigned int start_pos, struct fscrypt_str *fstr);
34494d57b86dSChao Yu void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
3450cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
34514d57b86dSChao Yu struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
345243c780baSEric Biggers 			const struct f2fs_filename *fname, struct page *dpage);
34534d57b86dSChao Yu void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
3454cac5a3d8SDongOh Shin 			unsigned int current_depth);
34554d57b86dSChao Yu int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots);
3456cac5a3d8SDongOh Shin void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
3457cac5a3d8SDongOh Shin struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
345843c780baSEric Biggers 					 const struct f2fs_filename *fname,
345943c780baSEric Biggers 					 struct page **res_page);
3460cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
3461cac5a3d8SDongOh Shin 			const struct qstr *child, struct page **res_page);
3462cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
3463cac5a3d8SDongOh Shin ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
3464cac5a3d8SDongOh Shin 			struct page **page);
3465cac5a3d8SDongOh Shin void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
3466cac5a3d8SDongOh Shin 			struct page *page, struct inode *inode);
3467b06af2afSJaegeuk Kim bool f2fs_has_enough_room(struct inode *dir, struct page *ipage,
346843c780baSEric Biggers 			  const struct f2fs_filename *fname);
3469cac5a3d8SDongOh Shin void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
347043c780baSEric Biggers 			const struct fscrypt_str *name, f2fs_hash_t name_hash,
3471cac5a3d8SDongOh Shin 			unsigned int bit_pos);
347243c780baSEric Biggers int f2fs_add_regular_entry(struct inode *dir, const struct f2fs_filename *fname,
3473cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
347443c780baSEric Biggers int f2fs_add_dentry(struct inode *dir, const struct f2fs_filename *fname,
3475cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
34764d57b86dSChao Yu int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
3477cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
3478cac5a3d8SDongOh Shin void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
3479cac5a3d8SDongOh Shin 			struct inode *dir, struct inode *inode);
3480cac5a3d8SDongOh Shin int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
3481cac5a3d8SDongOh Shin bool f2fs_empty_dir(struct inode *dir);
348239a53e0cSJaegeuk Kim 
3483b7f7a5e0SAl Viro static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
3484b7f7a5e0SAl Viro {
3485bfc2b7e8SEric Biggers 	if (fscrypt_is_nokey_name(dentry))
3486bfc2b7e8SEric Biggers 		return -ENOKEY;
34874d57b86dSChao Yu 	return f2fs_do_add_link(d_inode(dentry->d_parent), &dentry->d_name,
3488510022a8SJaegeuk Kim 				inode, inode->i_ino, inode->i_mode);
3489b7f7a5e0SAl Viro }
3490b7f7a5e0SAl Viro 
349139a53e0cSJaegeuk Kim /*
349239a53e0cSJaegeuk Kim  * super.c
349339a53e0cSJaegeuk Kim  */
3494cac5a3d8SDongOh Shin int f2fs_inode_dirtied(struct inode *inode, bool sync);
3495cac5a3d8SDongOh Shin void f2fs_inode_synced(struct inode *inode);
349610a26878SChao Yu int f2fs_dquot_initialize(struct inode *inode);
3497ea676733SJaegeuk Kim int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly);
3498af033b2aSChao Yu int f2fs_quota_sync(struct super_block *sb, int type);
34996d1451bfSChengguang Xu loff_t max_file_blocks(struct inode *inode);
35004b2414d0SChao Yu void f2fs_quota_off_umount(struct super_block *sb);
3501cac5a3d8SDongOh Shin int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
3502cac5a3d8SDongOh Shin int f2fs_sync_fs(struct super_block *sb, int sync);
35034d57b86dSChao Yu int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi);
350439a53e0cSJaegeuk Kim 
350539a53e0cSJaegeuk Kim /*
350639a53e0cSJaegeuk Kim  * hash.c
350739a53e0cSJaegeuk Kim  */
350843c780baSEric Biggers void f2fs_hash_filename(const struct inode *dir, struct f2fs_filename *fname);
350939a53e0cSJaegeuk Kim 
351039a53e0cSJaegeuk Kim /*
351139a53e0cSJaegeuk Kim  * node.c
351239a53e0cSJaegeuk Kim  */
351339a53e0cSJaegeuk Kim struct node_info;
351439a53e0cSJaegeuk Kim 
35154d57b86dSChao Yu int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid);
35164d57b86dSChao Yu bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type);
351750fa53ecSChao Yu bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct page *page);
351850fa53ecSChao Yu void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi);
351950fa53ecSChao Yu void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct page *page);
352050fa53ecSChao Yu void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi);
35214d57b86dSChao Yu int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
35224d57b86dSChao Yu bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
35234d57b86dSChao Yu bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
35247735730dSChao Yu int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
3525a9419b63SJaegeuk Kim 				struct node_info *ni, bool checkpoint_context);
35264d57b86dSChao Yu pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
35274d57b86dSChao Yu int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
35284d57b86dSChao Yu int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from);
35294d57b86dSChao Yu int f2fs_truncate_xattr_node(struct inode *inode);
353050fa53ecSChao Yu int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
353150fa53ecSChao Yu 					unsigned int seq_id);
353294c821fbSChao Yu bool f2fs_nat_bitmap_enabled(struct f2fs_sb_info *sbi);
35334d57b86dSChao Yu int f2fs_remove_inode_page(struct inode *inode);
35344d57b86dSChao Yu struct page *f2fs_new_inode_page(struct inode *inode);
35354d57b86dSChao Yu struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs);
35364d57b86dSChao Yu void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
35374d57b86dSChao Yu struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
35384d57b86dSChao Yu struct page *f2fs_get_node_page_ra(struct page *parent, int start);
353948018b4cSChao Yu int f2fs_move_node_page(struct page *node_page, int gc_type);
354068e79bafSJia Yang void f2fs_flush_inline_data(struct f2fs_sb_info *sbi);
35414d57b86dSChao Yu int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
354250fa53ecSChao Yu 			struct writeback_control *wbc, bool atomic,
354350fa53ecSChao Yu 			unsigned int *seq_id);
35444d57b86dSChao Yu int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
35454d57b86dSChao Yu 			struct writeback_control *wbc,
3546b0af6d49SChao Yu 			bool do_balance, enum iostat_type io_type);
3547e2374015SChao Yu int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
35484d57b86dSChao Yu bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
35494d57b86dSChao Yu void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
35504d57b86dSChao Yu void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
35514d57b86dSChao Yu int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
35529627a7b3SChao Yu int f2fs_recover_inline_xattr(struct inode *inode, struct page *page);
35534d57b86dSChao Yu int f2fs_recover_xattr_data(struct inode *inode, struct page *page);
35544d57b86dSChao Yu int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
35557735730dSChao Yu int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
3556cac5a3d8SDongOh Shin 			unsigned int segno, struct f2fs_summary_block *sum);
355794c821fbSChao Yu void f2fs_enable_nat_bits(struct f2fs_sb_info *sbi);
3558edc55aafSJaegeuk Kim int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
35594d57b86dSChao Yu int f2fs_build_node_manager(struct f2fs_sb_info *sbi);
35604d57b86dSChao Yu void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi);
35614d57b86dSChao Yu int __init f2fs_create_node_manager_caches(void);
35624d57b86dSChao Yu void f2fs_destroy_node_manager_caches(void);
356339a53e0cSJaegeuk Kim 
356439a53e0cSJaegeuk Kim /*
356539a53e0cSJaegeuk Kim  * segment.c
356639a53e0cSJaegeuk Kim  */
35674d57b86dSChao Yu bool f2fs_need_SSR(struct f2fs_sb_info *sbi);
35684d57b86dSChao Yu void f2fs_register_inmem_page(struct inode *inode, struct page *page);
35694d57b86dSChao Yu void f2fs_drop_inmem_pages_all(struct f2fs_sb_info *sbi, bool gc_failure);
35704d57b86dSChao Yu void f2fs_drop_inmem_pages(struct inode *inode);
35714d57b86dSChao Yu void f2fs_drop_inmem_page(struct inode *inode, struct page *page);
35724d57b86dSChao Yu int f2fs_commit_inmem_pages(struct inode *inode);
3573cac5a3d8SDongOh Shin void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
35747bcd0cfaSChao Yu void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi, bool from_bg);
357539d787beSChao Yu int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
35764d57b86dSChao Yu int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
35771228b482SChao Yu int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
35784d57b86dSChao Yu void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
35794d57b86dSChao Yu void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
35804d57b86dSChao Yu bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
35814d674904SFengnan Chang int f2fs_start_discard_thread(struct f2fs_sb_info *sbi);
35824d57b86dSChao Yu void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
35834d57b86dSChao Yu void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi);
358403f2c02dSJaegeuk Kim bool f2fs_issue_discard_timeout(struct f2fs_sb_info *sbi);
35854d57b86dSChao Yu void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
35864d57b86dSChao Yu 					struct cp_control *cpc);
35874354994fSDaniel Rosenberg void f2fs_dirty_to_prefree(struct f2fs_sb_info *sbi);
35884d3aed70SDaniel Rosenberg block_t f2fs_get_unusable_blocks(struct f2fs_sb_info *sbi);
35894d3aed70SDaniel Rosenberg int f2fs_disable_cp_again(struct f2fs_sb_info *sbi, block_t unusable);
35904d57b86dSChao Yu void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi);
35914d57b86dSChao Yu int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
359261461fc9SChao Yu bool f2fs_segment_has_free_slot(struct f2fs_sb_info *sbi, int segno);
3593093749e2SChao Yu void f2fs_init_inmem_curseg(struct f2fs_sb_info *sbi);
3594093749e2SChao Yu void f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi);
3595093749e2SChao Yu void f2fs_restore_inmem_curseg(struct f2fs_sb_info *sbi);
3596093749e2SChao Yu void f2fs_get_new_segment(struct f2fs_sb_info *sbi,
3597093749e2SChao Yu 			unsigned int *newseg, bool new_sec, int dir);
35980ef81833SChao Yu void f2fs_allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type,
359904f0b2eaSQiuyang Sun 					unsigned int start, unsigned int end);
3600509f1010SChao Yu void f2fs_allocate_new_section(struct f2fs_sb_info *sbi, int type, bool force);
3601901d745fSChao Yu void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi);
3602cac5a3d8SDongOh Shin int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
36034d57b86dSChao Yu bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
36044d57b86dSChao Yu 					struct cp_control *cpc);
36054d57b86dSChao Yu struct page *f2fs_get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
36064d57b86dSChao Yu void f2fs_update_meta_page(struct f2fs_sb_info *sbi, void *src,
36074d57b86dSChao Yu 					block_t blk_addr);
36084d57b86dSChao Yu void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
3609b0af6d49SChao Yu 						enum iostat_type io_type);
36104d57b86dSChao Yu void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio);
36114d57b86dSChao Yu void f2fs_outplace_write_data(struct dnode_of_data *dn,
36124d57b86dSChao Yu 			struct f2fs_io_info *fio);
36134d57b86dSChao Yu int f2fs_inplace_write_data(struct f2fs_io_info *fio);
36144d57b86dSChao Yu void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
3615cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t new_blkaddr,
3616c5d02785SChao Yu 			bool recover_curseg, bool recover_newaddr,
3617c5d02785SChao Yu 			bool from_gc);
3618cac5a3d8SDongOh Shin void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
3619cac5a3d8SDongOh Shin 			block_t old_addr, block_t new_addr,
3620cac5a3d8SDongOh Shin 			unsigned char version, bool recover_curseg,
3621cac5a3d8SDongOh Shin 			bool recover_newaddr);
36224d57b86dSChao Yu void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
3623cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t *new_blkaddr,
3624fb830fc5SChao Yu 			struct f2fs_summary *sum, int type,
3625f608c38cSChao Yu 			struct f2fs_io_info *fio);
362671f2c820SChao Yu void f2fs_update_device_state(struct f2fs_sb_info *sbi, nid_t ino,
362771f2c820SChao Yu 					block_t blkaddr, unsigned int blkcnt);
3628cac5a3d8SDongOh Shin void f2fs_wait_on_page_writeback(struct page *page,
3629bae0ee7aSChao Yu 			enum page_type type, bool ordered, bool locked);
36300ded69f6SJaegeuk Kim void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr);
36311e78e8bdSSahitya Tummala void f2fs_wait_on_block_writeback_range(struct inode *inode, block_t blkaddr,
36321e78e8bdSSahitya Tummala 								block_t len);
36334d57b86dSChao Yu void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
36344d57b86dSChao Yu void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
36354d57b86dSChao Yu int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
3636cac5a3d8SDongOh Shin 			unsigned int val, int alloc);
36374d57b86dSChao Yu void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
3638c426d991SShin'ichiro Kawasaki int f2fs_fix_curseg_write_pointer(struct f2fs_sb_info *sbi);
3639d508c94eSShin'ichiro Kawasaki int f2fs_check_write_pointer(struct f2fs_sb_info *sbi);
36404d57b86dSChao Yu int f2fs_build_segment_manager(struct f2fs_sb_info *sbi);
36414d57b86dSChao Yu void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi);
36424d57b86dSChao Yu int __init f2fs_create_segment_manager_caches(void);
36434d57b86dSChao Yu void f2fs_destroy_segment_manager_caches(void);
36444d57b86dSChao Yu int f2fs_rw_hint_to_seg_type(enum rw_hint hint);
36454d57b86dSChao Yu enum rw_hint f2fs_io_type_to_rw_hint(struct f2fs_sb_info *sbi,
36464d57b86dSChao Yu 			enum page_type type, enum temp_type temp);
3647de881df9SAravind Ramesh unsigned int f2fs_usable_segs_in_sec(struct f2fs_sb_info *sbi,
3648de881df9SAravind Ramesh 			unsigned int segno);
3649de881df9SAravind Ramesh unsigned int f2fs_usable_blks_in_seg(struct f2fs_sb_info *sbi,
3650de881df9SAravind Ramesh 			unsigned int segno);
365139a53e0cSJaegeuk Kim 
36526691d940SDaeho Jeong #define DEF_FRAGMENT_SIZE	4
36536691d940SDaeho Jeong #define MIN_FRAGMENT_SIZE	1
36546691d940SDaeho Jeong #define MAX_FRAGMENT_SIZE	512
36556691d940SDaeho Jeong 
36566691d940SDaeho Jeong static inline bool f2fs_need_rand_seg(struct f2fs_sb_info *sbi)
36576691d940SDaeho Jeong {
36586691d940SDaeho Jeong 	return F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_SEG ||
36596691d940SDaeho Jeong 		F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK;
36606691d940SDaeho Jeong }
36616691d940SDaeho Jeong 
366239a53e0cSJaegeuk Kim /*
366339a53e0cSJaegeuk Kim  * checkpoint.c
366439a53e0cSJaegeuk Kim  */
3665cac5a3d8SDongOh Shin void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io);
36664d57b86dSChao Yu struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
36674d57b86dSChao Yu struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
366886f33603SJaegeuk Kim struct page *f2fs_get_meta_page_retry(struct f2fs_sb_info *sbi, pgoff_t index);
36694d57b86dSChao Yu struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
3670e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
36714d57b86dSChao Yu 					block_t blkaddr, int type);
36724d57b86dSChao Yu int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
3673cac5a3d8SDongOh Shin 			int type, bool sync);
3674430f163bSChao Yu void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index,
3675430f163bSChao Yu 							unsigned int ra_blocks);
36764d57b86dSChao Yu long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
3677b0af6d49SChao Yu 			long nr_to_write, enum iostat_type io_type);
36784d57b86dSChao Yu void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
36794d57b86dSChao Yu void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
36804d57b86dSChao Yu void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all);
36814d57b86dSChao Yu bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
36824d57b86dSChao Yu void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
368339d787beSChao Yu 					unsigned int devidx, int type);
36844d57b86dSChao Yu bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
368539d787beSChao Yu 					unsigned int devidx, int type);
3686cac5a3d8SDongOh Shin int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
36874d57b86dSChao Yu int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi);
36884d57b86dSChao Yu void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi);
36894d57b86dSChao Yu void f2fs_add_orphan_inode(struct inode *inode);
36904d57b86dSChao Yu void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
36914d57b86dSChao Yu int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi);
36924d57b86dSChao Yu int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi);
36934d57b86dSChao Yu void f2fs_update_dirty_page(struct inode *inode, struct page *page);
36944d57b86dSChao Yu void f2fs_remove_dirty_inode(struct inode *inode);
36954d57b86dSChao Yu int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
3696bf22c3ccSSahitya Tummala void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type);
36973a0a9cbcSChao Yu u64 f2fs_get_sectors_written(struct f2fs_sb_info *sbi);
36984d57b86dSChao Yu int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
36994d57b86dSChao Yu void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi);
37004d57b86dSChao Yu int __init f2fs_create_checkpoint_caches(void);
37014d57b86dSChao Yu void f2fs_destroy_checkpoint_caches(void);
3702261eeb9cSDaeho Jeong int f2fs_issue_checkpoint(struct f2fs_sb_info *sbi);
3703261eeb9cSDaeho Jeong int f2fs_start_ckpt_thread(struct f2fs_sb_info *sbi);
3704261eeb9cSDaeho Jeong void f2fs_stop_ckpt_thread(struct f2fs_sb_info *sbi);
3705261eeb9cSDaeho Jeong void f2fs_init_ckpt_req_control(struct f2fs_sb_info *sbi);
370639a53e0cSJaegeuk Kim 
370739a53e0cSJaegeuk Kim /*
370839a53e0cSJaegeuk Kim  * data.c
370939a53e0cSJaegeuk Kim  */
3710f543805fSChao Yu int __init f2fs_init_bioset(void);
3711f543805fSChao Yu void f2fs_destroy_bioset(void);
37120b20fcecSChao Yu int f2fs_init_bio_entry_cache(void);
37130b20fcecSChao Yu void f2fs_destroy_bio_entry_cache(void);
37144c8ff709SChao Yu void f2fs_submit_bio(struct f2fs_sb_info *sbi,
37154c8ff709SChao Yu 				struct bio *bio, enum page_type type);
3716b9109b0eSJaegeuk Kim void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
3717b9109b0eSJaegeuk Kim void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
3718bab475c5SChao Yu 				struct inode *inode, struct page *page,
3719bab475c5SChao Yu 				nid_t ino, enum page_type type);
37200b20fcecSChao Yu void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
37210b20fcecSChao Yu 					struct bio **bio, struct page *page);
3722b9109b0eSJaegeuk Kim void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
3723cac5a3d8SDongOh Shin int f2fs_submit_page_bio(struct f2fs_io_info *fio);
37248648de2cSChao Yu int f2fs_merge_page_bio(struct f2fs_io_info *fio);
3725fe16efe6SChao Yu void f2fs_submit_page_write(struct f2fs_io_info *fio);
3726cac5a3d8SDongOh Shin struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
3727cac5a3d8SDongOh Shin 			block_t blk_addr, struct bio *bio);
3728cac5a3d8SDongOh Shin int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
37294d57b86dSChao Yu void f2fs_set_data_blkaddr(struct dnode_of_data *dn);
3730cac5a3d8SDongOh Shin void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
37314d57b86dSChao Yu int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
37324d57b86dSChao Yu int f2fs_reserve_new_block(struct dnode_of_data *dn);
3733cac5a3d8SDongOh Shin int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
3734cac5a3d8SDongOh Shin int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
37354d57b86dSChao Yu struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
3736cac5a3d8SDongOh Shin 			int op_flags, bool for_write);
37374d57b86dSChao Yu struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index);
37384d57b86dSChao Yu struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
3739cac5a3d8SDongOh Shin 			bool for_write);
37404d57b86dSChao Yu struct page *f2fs_get_new_data_page(struct inode *inode,
3741cac5a3d8SDongOh Shin 			struct page *ipage, pgoff_t index, bool new_i_size);
37424d57b86dSChao Yu int f2fs_do_write_data_page(struct f2fs_io_info *fio);
37430ef81833SChao Yu void f2fs_do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock);
3744cac5a3d8SDongOh Shin int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
3745cac5a3d8SDongOh Shin 			int create, int flag);
3746cac5a3d8SDongOh Shin int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3747cac5a3d8SDongOh Shin 			u64 start, u64 len);
37484c8ff709SChao Yu int f2fs_encrypt_one_page(struct f2fs_io_info *fio);
37494d57b86dSChao Yu bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio);
37504d57b86dSChao Yu bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio);
37514c8ff709SChao Yu int f2fs_write_single_data_page(struct page *page, int *submitted,
37524c8ff709SChao Yu 				struct bio **bio, sector_t *last_block,
37534c8ff709SChao Yu 				struct writeback_control *wbc,
37544c8ff709SChao Yu 				enum iostat_type io_type,
37553afae09fSChao Yu 				int compr_blocks, bool allow_balance);
3756a1e09b03SEric Biggers void f2fs_write_failed(struct inode *inode, loff_t to);
3757cac5a3d8SDongOh Shin void f2fs_invalidate_page(struct page *page, unsigned int offset,
3758cac5a3d8SDongOh Shin 			unsigned int length);
3759cac5a3d8SDongOh Shin int f2fs_release_page(struct page *page, gfp_t wait);
37605b7a487cSWeichao Guo #ifdef CONFIG_MIGRATION
3761cac5a3d8SDongOh Shin int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,
3762cac5a3d8SDongOh Shin 			struct page *page, enum migrate_mode mode);
37635b7a487cSWeichao Guo #endif
3764b91050a8SHyunchul Lee bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len);
37655ec2d99dSMatthew Wilcox void f2fs_clear_page_cache_dirty_tag(struct page *page);
37664c8ff709SChao Yu int f2fs_init_post_read_processing(void);
37674c8ff709SChao Yu void f2fs_destroy_post_read_processing(void);
37684c8ff709SChao Yu int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi);
37694c8ff709SChao Yu void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi);
37701517c1a7SEric Biggers extern const struct iomap_ops f2fs_iomap_ops;
377139a53e0cSJaegeuk Kim 
377239a53e0cSJaegeuk Kim /*
377339a53e0cSJaegeuk Kim  * gc.c
377439a53e0cSJaegeuk Kim  */
37754d57b86dSChao Yu int f2fs_start_gc_thread(struct f2fs_sb_info *sbi);
37764d57b86dSChao Yu void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi);
37774d57b86dSChao Yu block_t f2fs_start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
37787dede886SChao Yu int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background, bool force,
3779e066b83cSJaegeuk Kim 			unsigned int segno);
37804d57b86dSChao Yu void f2fs_build_gc_manager(struct f2fs_sb_info *sbi);
378104f0b2eaSQiuyang Sun int f2fs_resize_fs(struct f2fs_sb_info *sbi, __u64 block_count);
3782093749e2SChao Yu int __init f2fs_create_garbage_collection_cache(void);
3783093749e2SChao Yu void f2fs_destroy_garbage_collection_cache(void);
378439a53e0cSJaegeuk Kim 
378539a53e0cSJaegeuk Kim /*
378639a53e0cSJaegeuk Kim  * recovery.c
378739a53e0cSJaegeuk Kim  */
37884d57b86dSChao Yu int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
37894d57b86dSChao Yu bool f2fs_space_for_roll_forward(struct f2fs_sb_info *sbi);
3790cad83c96SChao Yu int __init f2fs_create_recovery_cache(void);
3791cad83c96SChao Yu void f2fs_destroy_recovery_cache(void);
379239a53e0cSJaegeuk Kim 
379339a53e0cSJaegeuk Kim /*
379439a53e0cSJaegeuk Kim  * debug.c
379539a53e0cSJaegeuk Kim  */
379639a53e0cSJaegeuk Kim #ifdef CONFIG_F2FS_STAT_FS
379739a53e0cSJaegeuk Kim struct f2fs_stat_info {
379839a53e0cSJaegeuk Kim 	struct list_head stat_list;
379939a53e0cSJaegeuk Kim 	struct f2fs_sb_info *sbi;
380039a53e0cSJaegeuk Kim 	int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
380139a53e0cSJaegeuk Kim 	int main_area_segs, main_area_sections, main_area_zones;
38025b7ee374SChao Yu 	unsigned long long hit_largest, hit_cached, hit_rbtree;
38035b7ee374SChao Yu 	unsigned long long hit_total, total_ext;
3804c00ba554SJaegeuk Kim 	int ext_tree, zombie_tree, ext_node;
38052c8a4a28SJaegeuk Kim 	int ndirty_node, ndirty_dent, ndirty_meta, ndirty_imeta;
38062c8a4a28SJaegeuk Kim 	int ndirty_data, ndirty_qdata;
380735782b23SJaegeuk Kim 	int inmem_pages;
38082c8a4a28SJaegeuk Kim 	unsigned int ndirty_dirs, ndirty_files, nquota_files, ndirty_all;
38095b0ef73cSJaegeuk Kim 	int nats, dirty_nats, sits, dirty_sits;
38105b0ef73cSJaegeuk Kim 	int free_nids, avail_nids, alloc_nids;
381139a53e0cSJaegeuk Kim 	int total_count, utilization;
38128b8dd65fSChao Yu 	int bg_gc, nr_wb_cp_data, nr_wb_data;
38135f9abab4SJaegeuk Kim 	int nr_rd_data, nr_rd_node, nr_rd_meta;
381402b16d0aSChao Yu 	int nr_dio_read, nr_dio_write;
3815274bd9baSChao Yu 	unsigned int io_skip_bggc, other_skip_bggc;
381614d8d5f7SChao Yu 	int nr_flushing, nr_flushed, flush_list_empty;
381714d8d5f7SChao Yu 	int nr_discarding, nr_discarded;
38185f32366aSChao Yu 	int nr_discard_cmd;
3819d84d1cbdSChao Yu 	unsigned int undiscard_blks;
3820261eeb9cSDaeho Jeong 	int nr_issued_ckpt, nr_total_ckpt, nr_queued_ckpt;
3821261eeb9cSDaeho Jeong 	unsigned int cur_ckpt_time, peak_ckpt_time;
3822a00861dbSJaegeuk Kim 	int inline_xattr, inline_inode, inline_dir, append, update, orphans;
3823ae999bb9SDaeho Jeong 	int compr_inode;
3824ae999bb9SDaeho Jeong 	unsigned long long compr_blocks;
3825648d50baSChao Yu 	int aw_cnt, max_aw_cnt, vw_cnt, max_vw_cnt;
3826f83a2584SYunlei He 	unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
382739a53e0cSJaegeuk Kim 	unsigned int bimodal, avg_vblocks;
382839a53e0cSJaegeuk Kim 	int util_free, util_valid, util_invalid;
382939a53e0cSJaegeuk Kim 	int rsvd_segs, overp_segs;
38306ce19affSChao Yu 	int dirty_count, node_pages, meta_pages, compress_pages;
38316ce19affSChao Yu 	int compress_page_hit;
383242190d2aSJaegeuk Kim 	int prefree_count, call_count, cp_count, bg_cp_count;
383339a53e0cSJaegeuk Kim 	int tot_segs, node_segs, data_segs, free_segs, free_secs;
3834e1235983SChangman Lee 	int bg_node_segs, bg_data_segs;
383539a53e0cSJaegeuk Kim 	int tot_blks, data_blks, node_blks;
3836e1235983SChangman Lee 	int bg_data_blks, bg_node_blks;
38372ef79ecbSChao Yu 	unsigned long long skipped_atomic_files[2];
383839a53e0cSJaegeuk Kim 	int curseg[NR_CURSEG_TYPE];
383939a53e0cSJaegeuk Kim 	int cursec[NR_CURSEG_TYPE];
384039a53e0cSJaegeuk Kim 	int curzone[NR_CURSEG_TYPE];
38410759e2c1SChao Yu 	unsigned int dirty_seg[NR_CURSEG_TYPE];
38420759e2c1SChao Yu 	unsigned int full_seg[NR_CURSEG_TYPE];
38430759e2c1SChao Yu 	unsigned int valid_blks[NR_CURSEG_TYPE];
384439a53e0cSJaegeuk Kim 
3845b63e7be5SChao Yu 	unsigned int meta_count[META_MAX];
384639a53e0cSJaegeuk Kim 	unsigned int segment_count[2];
384739a53e0cSJaegeuk Kim 	unsigned int block_count[2];
3848b9a2c252SChangman Lee 	unsigned int inplace_count;
38499edcdabfSChao Yu 	unsigned long long base_mem, cache_mem, page_mem;
385039a53e0cSJaegeuk Kim };
385139a53e0cSJaegeuk Kim 
3852963d4f7dSGu Zheng static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
3853963d4f7dSGu Zheng {
3854963d4f7dSGu Zheng 	return (struct f2fs_stat_info *)sbi->stat_info;
3855963d4f7dSGu Zheng }
3856963d4f7dSGu Zheng 
3857942e0be6SChangman Lee #define stat_inc_cp_count(si)		((si)->cp_count++)
385842190d2aSJaegeuk Kim #define stat_inc_bg_cp_count(si)	((si)->bg_cp_count++)
385939a53e0cSJaegeuk Kim #define stat_inc_call_count(si)		((si)->call_count++)
3860fc7100eaSHridya Valsaraju #define stat_inc_bggc_count(si)		((si)->bg_gc++)
3861274bd9baSChao Yu #define stat_io_skip_bggc_count(sbi)	((sbi)->io_skip_bggc++)
3862274bd9baSChao Yu #define stat_other_skip_bggc_count(sbi)	((sbi)->other_skip_bggc++)
386333fbd510SChao Yu #define stat_inc_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]++)
386433fbd510SChao Yu #define stat_dec_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]--)
38655b7ee374SChao Yu #define stat_inc_total_hit(sbi)		(atomic64_inc(&(sbi)->total_hit_ext))
38665b7ee374SChao Yu #define stat_inc_rbtree_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_rbtree))
38675b7ee374SChao Yu #define stat_inc_largest_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_largest))
38685b7ee374SChao Yu #define stat_inc_cached_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_cached))
3869d5e8f6c9SChao Yu #define stat_inc_inline_xattr(inode)					\
3870d5e8f6c9SChao Yu 	do {								\
3871d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3872d5e8f6c9SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_xattr));	\
3873d5e8f6c9SChao Yu 	} while (0)
3874d5e8f6c9SChao Yu #define stat_dec_inline_xattr(inode)					\
3875d5e8f6c9SChao Yu 	do {								\
3876d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3877d5e8f6c9SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_xattr));	\
3878d5e8f6c9SChao Yu 	} while (0)
38790dbdc2aeSJaegeuk Kim #define stat_inc_inline_inode(inode)					\
38800dbdc2aeSJaegeuk Kim 	do {								\
38810dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
388203e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_inode));	\
38830dbdc2aeSJaegeuk Kim 	} while (0)
38840dbdc2aeSJaegeuk Kim #define stat_dec_inline_inode(inode)					\
38850dbdc2aeSJaegeuk Kim 	do {								\
38860dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
388703e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_inode));	\
38880dbdc2aeSJaegeuk Kim 	} while (0)
38893289c061SJaegeuk Kim #define stat_inc_inline_dir(inode)					\
38903289c061SJaegeuk Kim 	do {								\
38913289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
389203e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_dir));	\
38933289c061SJaegeuk Kim 	} while (0)
38943289c061SJaegeuk Kim #define stat_dec_inline_dir(inode)					\
38953289c061SJaegeuk Kim 	do {								\
38963289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
389703e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_dir));	\
38983289c061SJaegeuk Kim 	} while (0)
38994c8ff709SChao Yu #define stat_inc_compr_inode(inode)					\
39004c8ff709SChao Yu 	do {								\
39014c8ff709SChao Yu 		if (f2fs_compressed_file(inode))			\
39024c8ff709SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->compr_inode));	\
39034c8ff709SChao Yu 	} while (0)
39044c8ff709SChao Yu #define stat_dec_compr_inode(inode)					\
39054c8ff709SChao Yu 	do {								\
39064c8ff709SChao Yu 		if (f2fs_compressed_file(inode))			\
39074c8ff709SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->compr_inode));	\
39084c8ff709SChao Yu 	} while (0)
39094c8ff709SChao Yu #define stat_add_compr_blocks(inode, blocks)				\
3910ae999bb9SDaeho Jeong 		(atomic64_add(blocks, &F2FS_I_SB(inode)->compr_blocks))
39114c8ff709SChao Yu #define stat_sub_compr_blocks(inode, blocks)				\
3912ae999bb9SDaeho Jeong 		(atomic64_sub(blocks, &F2FS_I_SB(inode)->compr_blocks))
3913b63e7be5SChao Yu #define stat_inc_meta_count(sbi, blkaddr)				\
3914b63e7be5SChao Yu 	do {								\
3915b63e7be5SChao Yu 		if (blkaddr < SIT_I(sbi)->sit_base_addr)		\
3916b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_CP]);	\
3917b63e7be5SChao Yu 		else if (blkaddr < NM_I(sbi)->nat_blkaddr)		\
3918b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_SIT]);	\
3919b63e7be5SChao Yu 		else if (blkaddr < SM_I(sbi)->ssa_blkaddr)		\
3920b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_NAT]);	\
3921b63e7be5SChao Yu 		else if (blkaddr < SM_I(sbi)->main_blkaddr)		\
3922b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_SSA]);	\
3923b63e7be5SChao Yu 	} while (0)
3924dcdfff65SJaegeuk Kim #define stat_inc_seg_type(sbi, curseg)					\
3925dcdfff65SJaegeuk Kim 		((sbi)->segment_count[(curseg)->alloc_type]++)
3926dcdfff65SJaegeuk Kim #define stat_inc_block_count(sbi, curseg)				\
3927dcdfff65SJaegeuk Kim 		((sbi)->block_count[(curseg)->alloc_type]++)
3928b9a2c252SChangman Lee #define stat_inc_inplace_blocks(sbi)					\
3929b9a2c252SChangman Lee 		(atomic_inc(&(sbi)->inplace_count))
393026a28a0cSJaegeuk Kim #define stat_update_max_atomic_write(inode)				\
393126a28a0cSJaegeuk Kim 	do {								\
39320e6d0164SSahitya Tummala 		int cur = F2FS_I_SB(inode)->atomic_files;	\
393326a28a0cSJaegeuk Kim 		int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt);	\
393426a28a0cSJaegeuk Kim 		if (cur > max)						\
393526a28a0cSJaegeuk Kim 			atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur);	\
393626a28a0cSJaegeuk Kim 	} while (0)
3937648d50baSChao Yu #define stat_inc_volatile_write(inode)					\
3938648d50baSChao Yu 		(atomic_inc(&F2FS_I_SB(inode)->vw_cnt))
3939648d50baSChao Yu #define stat_dec_volatile_write(inode)					\
3940648d50baSChao Yu 		(atomic_dec(&F2FS_I_SB(inode)->vw_cnt))
3941648d50baSChao Yu #define stat_update_max_volatile_write(inode)				\
3942648d50baSChao Yu 	do {								\
3943648d50baSChao Yu 		int cur = atomic_read(&F2FS_I_SB(inode)->vw_cnt);	\
3944648d50baSChao Yu 		int max = atomic_read(&F2FS_I_SB(inode)->max_vw_cnt);	\
3945648d50baSChao Yu 		if (cur > max)						\
3946648d50baSChao Yu 			atomic_set(&F2FS_I_SB(inode)->max_vw_cnt, cur);	\
3947648d50baSChao Yu 	} while (0)
3948e1235983SChangman Lee #define stat_inc_seg_count(sbi, type, gc_type)				\
394939a53e0cSJaegeuk Kim 	do {								\
3950963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
395168afcf2dSTomohiro Kusumi 		si->tot_segs++;						\
395268afcf2dSTomohiro Kusumi 		if ((type) == SUM_TYPE_DATA) {				\
395339a53e0cSJaegeuk Kim 			si->data_segs++;				\
3954e1235983SChangman Lee 			si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0;	\
3955e1235983SChangman Lee 		} else {						\
395639a53e0cSJaegeuk Kim 			si->node_segs++;				\
3957e1235983SChangman Lee 			si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0;	\
3958e1235983SChangman Lee 		}							\
395939a53e0cSJaegeuk Kim 	} while (0)
396039a53e0cSJaegeuk Kim 
396139a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks)				\
396268afcf2dSTomohiro Kusumi 	((si)->tot_blks += (blks))
396339a53e0cSJaegeuk Kim 
3964e1235983SChangman Lee #define stat_inc_data_blk_count(sbi, blks, gc_type)			\
396539a53e0cSJaegeuk Kim 	do {								\
3966963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
396739a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
396839a53e0cSJaegeuk Kim 		si->data_blks += (blks);				\
396968afcf2dSTomohiro Kusumi 		si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
397039a53e0cSJaegeuk Kim 	} while (0)
397139a53e0cSJaegeuk Kim 
3972e1235983SChangman Lee #define stat_inc_node_blk_count(sbi, blks, gc_type)			\
397339a53e0cSJaegeuk Kim 	do {								\
3974963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
397539a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
397639a53e0cSJaegeuk Kim 		si->node_blks += (blks);				\
397768afcf2dSTomohiro Kusumi 		si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
397839a53e0cSJaegeuk Kim 	} while (0)
397939a53e0cSJaegeuk Kim 
3980cac5a3d8SDongOh Shin int f2fs_build_stats(struct f2fs_sb_info *sbi);
3981cac5a3d8SDongOh Shin void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
398221acc07dSGreg Kroah-Hartman void __init f2fs_create_root_stats(void);
39834589d25dSNamjae Jeon void f2fs_destroy_root_stats(void);
3984fc7100eaSHridya Valsaraju void f2fs_update_sit_info(struct f2fs_sb_info *sbi);
398539a53e0cSJaegeuk Kim #else
3986d66450e7SArnd Bergmann #define stat_inc_cp_count(si)				do { } while (0)
3987d66450e7SArnd Bergmann #define stat_inc_bg_cp_count(si)			do { } while (0)
3988d66450e7SArnd Bergmann #define stat_inc_call_count(si)				do { } while (0)
3989d66450e7SArnd Bergmann #define stat_inc_bggc_count(si)				do { } while (0)
3990274bd9baSChao Yu #define stat_io_skip_bggc_count(sbi)			do { } while (0)
3991274bd9baSChao Yu #define stat_other_skip_bggc_count(sbi)			do { } while (0)
3992d66450e7SArnd Bergmann #define stat_inc_dirty_inode(sbi, type)			do { } while (0)
3993d66450e7SArnd Bergmann #define stat_dec_dirty_inode(sbi, type)			do { } while (0)
3994fc7100eaSHridya Valsaraju #define stat_inc_total_hit(sbi)				do { } while (0)
3995fc7100eaSHridya Valsaraju #define stat_inc_rbtree_node_hit(sbi)			do { } while (0)
3996d66450e7SArnd Bergmann #define stat_inc_largest_node_hit(sbi)			do { } while (0)
3997d66450e7SArnd Bergmann #define stat_inc_cached_node_hit(sbi)			do { } while (0)
3998d66450e7SArnd Bergmann #define stat_inc_inline_xattr(inode)			do { } while (0)
3999d66450e7SArnd Bergmann #define stat_dec_inline_xattr(inode)			do { } while (0)
4000d66450e7SArnd Bergmann #define stat_inc_inline_inode(inode)			do { } while (0)
4001d66450e7SArnd Bergmann #define stat_dec_inline_inode(inode)			do { } while (0)
4002d66450e7SArnd Bergmann #define stat_inc_inline_dir(inode)			do { } while (0)
4003d66450e7SArnd Bergmann #define stat_dec_inline_dir(inode)			do { } while (0)
40044c8ff709SChao Yu #define stat_inc_compr_inode(inode)			do { } while (0)
40054c8ff709SChao Yu #define stat_dec_compr_inode(inode)			do { } while (0)
40064c8ff709SChao Yu #define stat_add_compr_blocks(inode, blocks)		do { } while (0)
40074c8ff709SChao Yu #define stat_sub_compr_blocks(inode, blocks)		do { } while (0)
4008d66450e7SArnd Bergmann #define stat_update_max_atomic_write(inode)		do { } while (0)
4009d66450e7SArnd Bergmann #define stat_inc_volatile_write(inode)			do { } while (0)
4010d66450e7SArnd Bergmann #define stat_dec_volatile_write(inode)			do { } while (0)
4011d66450e7SArnd Bergmann #define stat_update_max_volatile_write(inode)		do { } while (0)
4012b63e7be5SChao Yu #define stat_inc_meta_count(sbi, blkaddr)		do { } while (0)
4013d66450e7SArnd Bergmann #define stat_inc_seg_type(sbi, curseg)			do { } while (0)
4014d66450e7SArnd Bergmann #define stat_inc_block_count(sbi, curseg)		do { } while (0)
4015d66450e7SArnd Bergmann #define stat_inc_inplace_blocks(sbi)			do { } while (0)
4016d66450e7SArnd Bergmann #define stat_inc_seg_count(sbi, type, gc_type)		do { } while (0)
4017d66450e7SArnd Bergmann #define stat_inc_tot_blk_count(si, blks)		do { } while (0)
4018d66450e7SArnd Bergmann #define stat_inc_data_blk_count(sbi, blks, gc_type)	do { } while (0)
4019d66450e7SArnd Bergmann #define stat_inc_node_blk_count(sbi, blks, gc_type)	do { } while (0)
402039a53e0cSJaegeuk Kim 
402139a53e0cSJaegeuk Kim static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
402239a53e0cSJaegeuk Kim static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
402321acc07dSGreg Kroah-Hartman static inline void __init f2fs_create_root_stats(void) { }
40244589d25dSNamjae Jeon static inline void f2fs_destroy_root_stats(void) { }
402548abe91aSYueHaibing static inline void f2fs_update_sit_info(struct f2fs_sb_info *sbi) {}
402639a53e0cSJaegeuk Kim #endif
402739a53e0cSJaegeuk Kim 
402839a53e0cSJaegeuk Kim extern const struct file_operations f2fs_dir_operations;
402939a53e0cSJaegeuk Kim extern const struct file_operations f2fs_file_operations;
403039a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_file_inode_operations;
403139a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_dblock_aops;
403239a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_node_aops;
403339a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_meta_aops;
403439a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_dir_inode_operations;
403539a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_symlink_inode_operations;
4036cbaf042aSJaegeuk Kim extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
403739a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_special_inode_operations;
40384d57b86dSChao Yu extern struct kmem_cache *f2fs_inode_entry_slab;
40391001b347SHuajun Li 
4040e18c65b2SHuajun Li /*
4041e18c65b2SHuajun Li  * inline.c
4042e18c65b2SHuajun Li  */
4043cac5a3d8SDongOh Shin bool f2fs_may_inline_data(struct inode *inode);
4044cac5a3d8SDongOh Shin bool f2fs_may_inline_dentry(struct inode *inode);
40454d57b86dSChao Yu void f2fs_do_read_inline_data(struct page *page, struct page *ipage);
40464d57b86dSChao Yu void f2fs_truncate_inline_inode(struct inode *inode,
40474d57b86dSChao Yu 						struct page *ipage, u64 from);
4048cac5a3d8SDongOh Shin int f2fs_read_inline_data(struct inode *inode, struct page *page);
4049cac5a3d8SDongOh Shin int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
4050cac5a3d8SDongOh Shin int f2fs_convert_inline_inode(struct inode *inode);
4051b06af2afSJaegeuk Kim int f2fs_try_convert_inline_dir(struct inode *dir, struct dentry *dentry);
4052cac5a3d8SDongOh Shin int f2fs_write_inline_data(struct inode *inode, struct page *page);
40539627a7b3SChao Yu int f2fs_recover_inline_data(struct inode *inode, struct page *npage);
40544d57b86dSChao Yu struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
405543c780baSEric Biggers 					const struct f2fs_filename *fname,
405643c780baSEric Biggers 					struct page **res_page);
40574d57b86dSChao Yu int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent,
4058cac5a3d8SDongOh Shin 			struct page *ipage);
405943c780baSEric Biggers int f2fs_add_inline_entry(struct inode *dir, const struct f2fs_filename *fname,
4060cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
40614d57b86dSChao Yu void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry,
40624d57b86dSChao Yu 				struct page *page, struct inode *dir,
40634d57b86dSChao Yu 				struct inode *inode);
4064cac5a3d8SDongOh Shin bool f2fs_empty_inline_dir(struct inode *dir);
4065cac5a3d8SDongOh Shin int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
4066cac5a3d8SDongOh Shin 			struct fscrypt_str *fstr);
4067cac5a3d8SDongOh Shin int f2fs_inline_data_fiemap(struct inode *inode,
4068cac5a3d8SDongOh Shin 			struct fiemap_extent_info *fieinfo,
4069cac5a3d8SDongOh Shin 			__u64 start, __u64 len);
4070cde4de12SJaegeuk Kim 
4071cde4de12SJaegeuk Kim /*
40722658e50dSJaegeuk Kim  * shrinker.c
40732658e50dSJaegeuk Kim  */
4074cac5a3d8SDongOh Shin unsigned long f2fs_shrink_count(struct shrinker *shrink,
4075cac5a3d8SDongOh Shin 			struct shrink_control *sc);
4076cac5a3d8SDongOh Shin unsigned long f2fs_shrink_scan(struct shrinker *shrink,
4077cac5a3d8SDongOh Shin 			struct shrink_control *sc);
4078cac5a3d8SDongOh Shin void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
4079cac5a3d8SDongOh Shin void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
40802658e50dSJaegeuk Kim 
40812658e50dSJaegeuk Kim /*
4082a28ef1f5SChao Yu  * extent_cache.c
4083a28ef1f5SChao Yu  */
40844dada3fdSChao Yu struct rb_entry *f2fs_lookup_rb_tree(struct rb_root_cached *root,
4085004b6862SChao Yu 				struct rb_entry *cached_re, unsigned int ofs);
40862e9b2bb2SChao Yu struct rb_node **f2fs_lookup_rb_tree_ext(struct f2fs_sb_info *sbi,
40872e9b2bb2SChao Yu 				struct rb_root_cached *root,
40882e9b2bb2SChao Yu 				struct rb_node **parent,
40892e9b2bb2SChao Yu 				unsigned long long key, bool *left_most);
40904d57b86dSChao Yu struct rb_node **f2fs_lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
40914dada3fdSChao Yu 				struct rb_root_cached *root,
40924dada3fdSChao Yu 				struct rb_node **parent,
40934dada3fdSChao Yu 				unsigned int ofs, bool *leftmost);
40944dada3fdSChao Yu struct rb_entry *f2fs_lookup_rb_tree_ret(struct rb_root_cached *root,
4095004b6862SChao Yu 		struct rb_entry *cached_re, unsigned int ofs,
4096004b6862SChao Yu 		struct rb_entry **prev_entry, struct rb_entry **next_entry,
4097004b6862SChao Yu 		struct rb_node ***insert_p, struct rb_node **insert_parent,
40984dada3fdSChao Yu 		bool force, bool *leftmost);
40994d57b86dSChao Yu bool f2fs_check_rb_tree_consistence(struct f2fs_sb_info *sbi,
41002e9b2bb2SChao Yu 				struct rb_root_cached *root, bool check_key);
4101cac5a3d8SDongOh Shin unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
4102a6d601f3SChao Yu void f2fs_init_extent_tree(struct inode *inode, struct page *ipage);
4103cac5a3d8SDongOh Shin void f2fs_drop_extent_tree(struct inode *inode);
4104cac5a3d8SDongOh Shin unsigned int f2fs_destroy_extent_node(struct inode *inode);
4105cac5a3d8SDongOh Shin void f2fs_destroy_extent_tree(struct inode *inode);
4106cac5a3d8SDongOh Shin bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
4107cac5a3d8SDongOh Shin 			struct extent_info *ei);
4108cac5a3d8SDongOh Shin void f2fs_update_extent_cache(struct dnode_of_data *dn);
410919b2c30dSChao Yu void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
4110cac5a3d8SDongOh Shin 			pgoff_t fofs, block_t blkaddr, unsigned int len);
41114d57b86dSChao Yu void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi);
41124d57b86dSChao Yu int __init f2fs_create_extent_cache(void);
41134d57b86dSChao Yu void f2fs_destroy_extent_cache(void);
4114a28ef1f5SChao Yu 
4115a28ef1f5SChao Yu /*
41168ceffcb2SChao Yu  * sysfs.c
41178ceffcb2SChao Yu  */
41180f6b56ecSDaeho Jeong #define MIN_RA_MUL	2
41190f6b56ecSDaeho Jeong #define MAX_RA_MUL	256
41200f6b56ecSDaeho Jeong 
4121dc6b2055SJaegeuk Kim int __init f2fs_init_sysfs(void);
4122dc6b2055SJaegeuk Kim void f2fs_exit_sysfs(void);
4123dc6b2055SJaegeuk Kim int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
4124dc6b2055SJaegeuk Kim void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
41258ceffcb2SChao Yu 
412695ae251fSEric Biggers /* verity.c */
412795ae251fSEric Biggers extern const struct fsverity_operations f2fs_verityops;
412895ae251fSEric Biggers 
41298ceffcb2SChao Yu /*
4130cde4de12SJaegeuk Kim  * crypto support
4131cde4de12SJaegeuk Kim  */
41321958593eSJaegeuk Kim static inline bool f2fs_encrypted_file(struct inode *inode)
41331958593eSJaegeuk Kim {
413462230e0dSChandan Rajendra 	return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
41351958593eSJaegeuk Kim }
41361958593eSJaegeuk Kim 
4137cde4de12SJaegeuk Kim static inline void f2fs_set_encrypted_inode(struct inode *inode)
4138cde4de12SJaegeuk Kim {
4139643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
4140cde4de12SJaegeuk Kim 	file_set_encrypt(inode);
41419149a5ebSChao Yu 	f2fs_set_inode_flags(inode);
4142cde4de12SJaegeuk Kim #endif
4143cde4de12SJaegeuk Kim }
4144cde4de12SJaegeuk Kim 
41456dbb1796SEric Biggers /*
41466dbb1796SEric Biggers  * Returns true if the reads of the inode's data need to undergo some
41476dbb1796SEric Biggers  * postprocessing step, like decryption or authenticity verification.
41486dbb1796SEric Biggers  */
41496dbb1796SEric Biggers static inline bool f2fs_post_read_required(struct inode *inode)
4150cde4de12SJaegeuk Kim {
41514c8ff709SChao Yu 	return f2fs_encrypted_file(inode) || fsverity_active(inode) ||
41524c8ff709SChao Yu 		f2fs_compressed_file(inode);
41534c8ff709SChao Yu }
41544c8ff709SChao Yu 
41554c8ff709SChao Yu /*
41564c8ff709SChao Yu  * compress.c
41574c8ff709SChao Yu  */
41584c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
41594c8ff709SChao Yu bool f2fs_is_compressed_page(struct page *page);
41604c8ff709SChao Yu struct page *f2fs_compress_control_page(struct page *page);
41614c8ff709SChao Yu int f2fs_prepare_compress_overwrite(struct inode *inode,
41624c8ff709SChao Yu 			struct page **pagep, pgoff_t index, void **fsdata);
41634c8ff709SChao Yu bool f2fs_compress_write_end(struct inode *inode, void *fsdata,
41644c8ff709SChao Yu 					pgoff_t index, unsigned copied);
41653265d3dbSChao Yu int f2fs_truncate_partial_cluster(struct inode *inode, u64 from, bool lock);
41664c8ff709SChao Yu void f2fs_compress_write_end_io(struct bio *bio, struct page *page);
41674c8ff709SChao Yu bool f2fs_is_compress_backend_ready(struct inode *inode);
41685e6bbde9SChao Yu int f2fs_init_compress_mempool(void);
41695e6bbde9SChao Yu void f2fs_destroy_compress_mempool(void);
41706ce19affSChao Yu void f2fs_decompress_cluster(struct decompress_io_ctx *dic);
41716ce19affSChao Yu void f2fs_end_read_compressed_page(struct page *page, bool failed,
41726ce19affSChao Yu 							block_t blkaddr);
41734c8ff709SChao Yu bool f2fs_cluster_is_empty(struct compress_ctx *cc);
41744c8ff709SChao Yu bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index);
4175b368cc5eSFengnan Chang bool f2fs_all_cluster_page_loaded(struct compress_ctx *cc, struct pagevec *pvec,
4176b368cc5eSFengnan Chang 				int index, int nr_pages);
4177bbe1da7eSChao Yu bool f2fs_sanity_check_cluster(struct dnode_of_data *dn);
41784c8ff709SChao Yu void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct page *page);
41794c8ff709SChao Yu int f2fs_write_multi_pages(struct compress_ctx *cc,
41804c8ff709SChao Yu 						int *submitted,
41814c8ff709SChao Yu 						struct writeback_control *wbc,
41824c8ff709SChao Yu 						enum iostat_type io_type);
41834c8ff709SChao Yu int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index);
418494afd6d6SChao Yu void f2fs_update_extent_tree_range_compressed(struct inode *inode,
418594afd6d6SChao Yu 				pgoff_t fofs, block_t blkaddr, unsigned int llen,
418694afd6d6SChao Yu 				unsigned int c_len);
41874c8ff709SChao Yu int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
41884c8ff709SChao Yu 				unsigned nr_pages, sector_t *last_block_in_bio,
41890683728aSChao Yu 				bool is_readahead, bool for_write);
41904c8ff709SChao Yu struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc);
41917f59b277SEric Biggers void f2fs_decompress_end_io(struct decompress_io_ctx *dic, bool failed);
41927f59b277SEric Biggers void f2fs_put_page_dic(struct page *page);
419394afd6d6SChao Yu unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn);
41944c8ff709SChao Yu int f2fs_init_compress_ctx(struct compress_ctx *cc);
41958bfbfb0dSChao Yu void f2fs_destroy_compress_ctx(struct compress_ctx *cc, bool reuse);
41964c8ff709SChao Yu void f2fs_init_compress_info(struct f2fs_sb_info *sbi);
41976ce19affSChao Yu int f2fs_init_compress_inode(struct f2fs_sb_info *sbi);
41986ce19affSChao Yu void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi);
419931083031SChao Yu int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi);
420031083031SChao Yu void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi);
4201c68d6c88SChao Yu int __init f2fs_init_compress_cache(void);
4202c68d6c88SChao Yu void f2fs_destroy_compress_cache(void);
42036ce19affSChao Yu struct address_space *COMPRESS_MAPPING(struct f2fs_sb_info *sbi);
42046ce19affSChao Yu void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi, block_t blkaddr);
42056ce19affSChao Yu void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
42066ce19affSChao Yu 						nid_t ino, block_t blkaddr);
42076ce19affSChao Yu bool f2fs_load_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
42086ce19affSChao Yu 								block_t blkaddr);
42096ce19affSChao Yu void f2fs_invalidate_compress_pages(struct f2fs_sb_info *sbi, nid_t ino);
42105ac443e2SDaeho Jeong #define inc_compr_inode_stat(inode)					\
42115ac443e2SDaeho Jeong 	do {								\
42125ac443e2SDaeho Jeong 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);		\
42135ac443e2SDaeho Jeong 		sbi->compr_new_inode++;					\
42145ac443e2SDaeho Jeong 	} while (0)
42155ac443e2SDaeho Jeong #define add_compr_block_stat(inode, blocks)				\
42165ac443e2SDaeho Jeong 	do {								\
42175ac443e2SDaeho Jeong 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);		\
42185ac443e2SDaeho Jeong 		int diff = F2FS_I(inode)->i_cluster_size - blocks;	\
42195ac443e2SDaeho Jeong 		sbi->compr_written_block += blocks;			\
42205ac443e2SDaeho Jeong 		sbi->compr_saved_block += diff;				\
42215ac443e2SDaeho Jeong 	} while (0)
42224c8ff709SChao Yu #else
42234c8ff709SChao Yu static inline bool f2fs_is_compressed_page(struct page *page) { return false; }
42244c8ff709SChao Yu static inline bool f2fs_is_compress_backend_ready(struct inode *inode)
42254c8ff709SChao Yu {
42264c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
42274c8ff709SChao Yu 		return true;
42284c8ff709SChao Yu 	/* not support compression */
42294c8ff709SChao Yu 	return false;
42304c8ff709SChao Yu }
42314c8ff709SChao Yu static inline struct page *f2fs_compress_control_page(struct page *page)
42324c8ff709SChao Yu {
42334c8ff709SChao Yu 	WARN_ON_ONCE(1);
42344c8ff709SChao Yu 	return ERR_PTR(-EINVAL);
42354c8ff709SChao Yu }
42365e6bbde9SChao Yu static inline int f2fs_init_compress_mempool(void) { return 0; }
42375e6bbde9SChao Yu static inline void f2fs_destroy_compress_mempool(void) { }
42386ce19affSChao Yu static inline void f2fs_decompress_cluster(struct decompress_io_ctx *dic) { }
42396ce19affSChao Yu static inline void f2fs_end_read_compressed_page(struct page *page,
42406ce19affSChao Yu 						bool failed, block_t blkaddr)
42417f59b277SEric Biggers {
42427f59b277SEric Biggers 	WARN_ON_ONCE(1);
42437f59b277SEric Biggers }
42447f59b277SEric Biggers static inline void f2fs_put_page_dic(struct page *page)
42457f59b277SEric Biggers {
42467f59b277SEric Biggers 	WARN_ON_ONCE(1);
42477f59b277SEric Biggers }
424894afd6d6SChao Yu static inline unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn) { return 0; }
4249bbe1da7eSChao Yu static inline bool f2fs_sanity_check_cluster(struct dnode_of_data *dn) { return false; }
42506ce19affSChao Yu static inline int f2fs_init_compress_inode(struct f2fs_sb_info *sbi) { return 0; }
42516ce19affSChao Yu static inline void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi) { }
425231083031SChao Yu static inline int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi) { return 0; }
425331083031SChao Yu static inline void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi) { }
4254c68d6c88SChao Yu static inline int __init f2fs_init_compress_cache(void) { return 0; }
4255c68d6c88SChao Yu static inline void f2fs_destroy_compress_cache(void) { }
42566ce19affSChao Yu static inline void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi,
42576ce19affSChao Yu 				block_t blkaddr) { }
42586ce19affSChao Yu static inline void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi,
42596ce19affSChao Yu 				struct page *page, nid_t ino, block_t blkaddr) { }
42606ce19affSChao Yu static inline bool f2fs_load_compressed_page(struct f2fs_sb_info *sbi,
42616ce19affSChao Yu 				struct page *page, block_t blkaddr) { return false; }
42626ce19affSChao Yu static inline void f2fs_invalidate_compress_pages(struct f2fs_sb_info *sbi,
42636ce19affSChao Yu 							nid_t ino) { }
42645ac443e2SDaeho Jeong #define inc_compr_inode_stat(inode)		do { } while (0)
426594afd6d6SChao Yu static inline void f2fs_update_extent_tree_range_compressed(struct inode *inode,
426694afd6d6SChao Yu 				pgoff_t fofs, block_t blkaddr, unsigned int llen,
426794afd6d6SChao Yu 				unsigned int c_len) { }
42684c8ff709SChao Yu #endif
42694c8ff709SChao Yu 
42704c8ff709SChao Yu static inline void set_compress_context(struct inode *inode)
42714c8ff709SChao Yu {
42724c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
42734c8ff709SChao Yu 
42744c8ff709SChao Yu 	F2FS_I(inode)->i_compress_algorithm =
42754c8ff709SChao Yu 			F2FS_OPTION(sbi).compress_algorithm;
42764c8ff709SChao Yu 	F2FS_I(inode)->i_log_cluster_size =
42774c8ff709SChao Yu 			F2FS_OPTION(sbi).compress_log_size;
4278b28f047bSChao Yu 	F2FS_I(inode)->i_compress_flag =
4279b28f047bSChao Yu 			F2FS_OPTION(sbi).compress_chksum ?
4280b28f047bSChao Yu 				1 << COMPRESS_CHKSUM : 0;
42814c8ff709SChao Yu 	F2FS_I(inode)->i_cluster_size =
42824c8ff709SChao Yu 			1 << F2FS_I(inode)->i_log_cluster_size;
428301f6afd0SChao Yu 	if ((F2FS_I(inode)->i_compress_algorithm == COMPRESS_LZ4 ||
428401f6afd0SChao Yu 		F2FS_I(inode)->i_compress_algorithm == COMPRESS_ZSTD) &&
42853fde13f8SChao Yu 			F2FS_OPTION(sbi).compress_level)
42863fde13f8SChao Yu 		F2FS_I(inode)->i_compress_flag |=
42873fde13f8SChao Yu 				F2FS_OPTION(sbi).compress_level <<
42883fde13f8SChao Yu 				COMPRESS_LEVEL_OFFSET;
42894c8ff709SChao Yu 	F2FS_I(inode)->i_flags |= F2FS_COMPR_FL;
42904c8ff709SChao Yu 	set_inode_flag(inode, FI_COMPRESSED_FILE);
42914c8ff709SChao Yu 	stat_inc_compr_inode(inode);
42925ac443e2SDaeho Jeong 	inc_compr_inode_stat(inode);
4293530e0704SChao Yu 	f2fs_mark_inode_dirty_sync(inode, true);
42944c8ff709SChao Yu }
42954c8ff709SChao Yu 
429678134d03SDaeho Jeong static inline bool f2fs_disable_compressed_file(struct inode *inode)
42974c8ff709SChao Yu {
42984c8ff709SChao Yu 	struct f2fs_inode_info *fi = F2FS_I(inode);
42994c8ff709SChao Yu 
43004c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
430178134d03SDaeho Jeong 		return true;
430202d58cd2SHyeong-Jun Kim 	if (S_ISREG(inode->i_mode) && F2FS_HAS_BLOCKS(inode))
430378134d03SDaeho Jeong 		return false;
43044c8ff709SChao Yu 
43054c8ff709SChao Yu 	fi->i_flags &= ~F2FS_COMPR_FL;
43064c8ff709SChao Yu 	stat_dec_compr_inode(inode);
430796f5b4faSChao Yu 	clear_inode_flag(inode, FI_COMPRESSED_FILE);
4308530e0704SChao Yu 	f2fs_mark_inode_dirty_sync(inode, true);
430978134d03SDaeho Jeong 	return true;
4310cde4de12SJaegeuk Kim }
4311cde4de12SJaegeuk Kim 
4312ccd31cb2SSheng Yong #define F2FS_FEATURE_FUNCS(name, flagname) \
43137beb01f7SChao Yu static inline int f2fs_sb_has_##name(struct f2fs_sb_info *sbi) \
4314ccd31cb2SSheng Yong { \
43157beb01f7SChao Yu 	return F2FS_HAS_FEATURE(sbi, F2FS_FEATURE_##flagname); \
4316cde4de12SJaegeuk Kim }
4317f424f664SJaegeuk Kim 
4318ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(encrypt, ENCRYPT);
4319ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(blkzoned, BLKZONED);
4320ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(extra_attr, EXTRA_ATTR);
4321ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(project_quota, PRJQUOTA);
4322ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_chksum, INODE_CHKSUM);
4323ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
4324ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
4325ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
4326b7c409deSSheng Yong F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
432795ae251fSEric Biggers F2FS_FEATURE_FUNCS(verity, VERITY);
4328d440c52dSJunling Zheng F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
43295aba5430SDaniel Rosenberg F2FS_FEATURE_FUNCS(casefold, CASEFOLD);
43304c8ff709SChao Yu F2FS_FEATURE_FUNCS(compression, COMPRESSION);
4331a7d9fe3cSJaegeuk Kim F2FS_FEATURE_FUNCS(readonly, RO);
43321c1d35dfSChao Yu 
43334215d054SDaeho Jeong static inline bool f2fs_may_extent_tree(struct inode *inode)
43344215d054SDaeho Jeong {
43354215d054SDaeho Jeong 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
43364215d054SDaeho Jeong 
43374215d054SDaeho Jeong 	if (!test_opt(sbi, EXTENT_CACHE) ||
43384215d054SDaeho Jeong 			is_inode_flag_set(inode, FI_NO_EXTENT) ||
43394215d054SDaeho Jeong 			(is_inode_flag_set(inode, FI_COMPRESSED_FILE) &&
43404215d054SDaeho Jeong 			 !f2fs_sb_has_readonly(sbi)))
43414215d054SDaeho Jeong 		return false;
43424215d054SDaeho Jeong 
43434215d054SDaeho Jeong 	/*
43444215d054SDaeho Jeong 	 * for recovered files during mount do not create extents
43454215d054SDaeho Jeong 	 * if shrinker is not registered.
43464215d054SDaeho Jeong 	 */
43474215d054SDaeho Jeong 	if (list_empty(&sbi->s_list))
43484215d054SDaeho Jeong 		return false;
43494215d054SDaeho Jeong 
43504215d054SDaeho Jeong 	return S_ISREG(inode->i_mode);
43514215d054SDaeho Jeong }
43524215d054SDaeho Jeong 
4353178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
435495175dafSDamien Le Moal static inline bool f2fs_blkz_is_seq(struct f2fs_sb_info *sbi, int devi,
435595175dafSDamien Le Moal 				    block_t blkaddr)
4356178053e2SDamien Le Moal {
4357178053e2SDamien Le Moal 	unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
4358178053e2SDamien Le Moal 
435995175dafSDamien Le Moal 	return test_bit(zno, FDEV(devi).blkz_seq);
4360178053e2SDamien Le Moal }
4361178053e2SDamien Le Moal #endif
4362178053e2SDamien Le Moal 
43637d20c8abSChao Yu static inline bool f2fs_hw_should_discard(struct f2fs_sb_info *sbi)
436496ba2decSDamien Le Moal {
43657beb01f7SChao Yu 	return f2fs_sb_has_blkzoned(sbi);
43667d20c8abSChao Yu }
436796ba2decSDamien Le Moal 
43687f3d7719SDamien Le Moal static inline bool f2fs_bdev_support_discard(struct block_device *bdev)
43697f3d7719SDamien Le Moal {
43707f3d7719SDamien Le Moal 	return blk_queue_discard(bdev_get_queue(bdev)) ||
43717f3d7719SDamien Le Moal 	       bdev_is_zoned(bdev);
43727f3d7719SDamien Le Moal }
43737f3d7719SDamien Le Moal 
43747d20c8abSChao Yu static inline bool f2fs_hw_support_discard(struct f2fs_sb_info *sbi)
43757d20c8abSChao Yu {
43767f3d7719SDamien Le Moal 	int i;
43777f3d7719SDamien Le Moal 
43787f3d7719SDamien Le Moal 	if (!f2fs_is_multi_device(sbi))
43797f3d7719SDamien Le Moal 		return f2fs_bdev_support_discard(sbi->sb->s_bdev);
43807f3d7719SDamien Le Moal 
43817f3d7719SDamien Le Moal 	for (i = 0; i < sbi->s_ndevs; i++)
43827f3d7719SDamien Le Moal 		if (f2fs_bdev_support_discard(FDEV(i).bdev))
43837f3d7719SDamien Le Moal 			return true;
43847f3d7719SDamien Le Moal 	return false;
43857d20c8abSChao Yu }
43867d20c8abSChao Yu 
43877d20c8abSChao Yu static inline bool f2fs_realtime_discard_enable(struct f2fs_sb_info *sbi)
43887d20c8abSChao Yu {
43897d20c8abSChao Yu 	return (test_opt(sbi, DISCARD) && f2fs_hw_support_discard(sbi)) ||
43907d20c8abSChao Yu 					f2fs_hw_should_discard(sbi);
439152763a4bSJaegeuk Kim }
439252763a4bSJaegeuk Kim 
4393f824deb5SChao Yu static inline bool f2fs_hw_is_readonly(struct f2fs_sb_info *sbi)
4394f824deb5SChao Yu {
4395f824deb5SChao Yu 	int i;
4396f824deb5SChao Yu 
4397f824deb5SChao Yu 	if (!f2fs_is_multi_device(sbi))
4398f824deb5SChao Yu 		return bdev_read_only(sbi->sb->s_bdev);
4399f824deb5SChao Yu 
4400f824deb5SChao Yu 	for (i = 0; i < sbi->s_ndevs; i++)
4401f824deb5SChao Yu 		if (bdev_read_only(FDEV(i).bdev))
4402f824deb5SChao Yu 			return true;
4403f824deb5SChao Yu 	return false;
4404f824deb5SChao Yu }
4405f824deb5SChao Yu 
4406b0332a0fSChao Yu static inline bool f2fs_lfs_mode(struct f2fs_sb_info *sbi)
440752763a4bSJaegeuk Kim {
4408b0332a0fSChao Yu 	return F2FS_OPTION(sbi).fs_mode == FS_MODE_LFS;
440952763a4bSJaegeuk Kim }
441052763a4bSJaegeuk Kim 
44114c8ff709SChao Yu static inline bool f2fs_may_compress(struct inode *inode)
44124c8ff709SChao Yu {
44134c8ff709SChao Yu 	if (IS_SWAPFILE(inode) || f2fs_is_pinned_file(inode) ||
44144c8ff709SChao Yu 				f2fs_is_atomic_file(inode) ||
44154c8ff709SChao Yu 				f2fs_is_volatile_file(inode))
44164c8ff709SChao Yu 		return false;
44174c8ff709SChao Yu 	return S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode);
44184c8ff709SChao Yu }
44194c8ff709SChao Yu 
44204c8ff709SChao Yu static inline void f2fs_i_compr_blocks_update(struct inode *inode,
44214c8ff709SChao Yu 						u64 blocks, bool add)
44224c8ff709SChao Yu {
44234c8ff709SChao Yu 	int diff = F2FS_I(inode)->i_cluster_size - blocks;
4424c2759ebaSDaeho Jeong 	struct f2fs_inode_info *fi = F2FS_I(inode);
44254c8ff709SChao Yu 
4426ef8d563fSChao Yu 	/* don't update i_compr_blocks if saved blocks were released */
4427c2759ebaSDaeho Jeong 	if (!add && !atomic_read(&fi->i_compr_blocks))
4428ef8d563fSChao Yu 		return;
4429ef8d563fSChao Yu 
44304c8ff709SChao Yu 	if (add) {
4431c2759ebaSDaeho Jeong 		atomic_add(diff, &fi->i_compr_blocks);
44324c8ff709SChao Yu 		stat_add_compr_blocks(inode, diff);
44334c8ff709SChao Yu 	} else {
4434c2759ebaSDaeho Jeong 		atomic_sub(diff, &fi->i_compr_blocks);
44354c8ff709SChao Yu 		stat_sub_compr_blocks(inode, diff);
44364c8ff709SChao Yu 	}
44374c8ff709SChao Yu 	f2fs_mark_inode_dirty_sync(inode, true);
44384c8ff709SChao Yu }
44394c8ff709SChao Yu 
4440f847c699SChao Yu static inline int block_unaligned_IO(struct inode *inode,
4441f847c699SChao Yu 				struct kiocb *iocb, struct iov_iter *iter)
4442b91050a8SHyunchul Lee {
4443f847c699SChao Yu 	unsigned int i_blkbits = READ_ONCE(inode->i_blkbits);
4444f847c699SChao Yu 	unsigned int blocksize_mask = (1 << i_blkbits) - 1;
4445f847c699SChao Yu 	loff_t offset = iocb->ki_pos;
4446f847c699SChao Yu 	unsigned long align = offset | iov_iter_alignment(iter);
4447f847c699SChao Yu 
4448f847c699SChao Yu 	return align & blocksize_mask;
4449f847c699SChao Yu }
4450f847c699SChao Yu 
445171f2c820SChao Yu static inline bool f2fs_allow_multi_device_dio(struct f2fs_sb_info *sbi,
445271f2c820SChao Yu 								int flag)
445371f2c820SChao Yu {
445471f2c820SChao Yu 	if (!f2fs_is_multi_device(sbi))
445571f2c820SChao Yu 		return false;
445671f2c820SChao Yu 	if (flag != F2FS_GET_BLOCK_DIO)
445771f2c820SChao Yu 		return false;
445871f2c820SChao Yu 	return sbi->aligned_blksize;
445971f2c820SChao Yu }
446071f2c820SChao Yu 
4461f847c699SChao Yu static inline bool f2fs_force_buffered_io(struct inode *inode,
4462f847c699SChao Yu 				struct kiocb *iocb, struct iov_iter *iter)
4463f847c699SChao Yu {
4464f847c699SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4465f847c699SChao Yu 	int rw = iov_iter_rw(iter);
4466f847c699SChao Yu 
4467f847c699SChao Yu 	if (f2fs_post_read_required(inode))
4468f847c699SChao Yu 		return true;
446971f2c820SChao Yu 
447071f2c820SChao Yu 	/* disallow direct IO if any of devices has unaligned blksize */
447171f2c820SChao Yu 	if (f2fs_is_multi_device(sbi) && !sbi->aligned_blksize)
4472f847c699SChao Yu 		return true;
4473f847c699SChao Yu 	/*
4474f847c699SChao Yu 	 * for blkzoned device, fallback direct IO to buffered IO, so
4475f847c699SChao Yu 	 * all IOs can be serialized by log-structured write.
4476f847c699SChao Yu 	 */
44777beb01f7SChao Yu 	if (f2fs_sb_has_blkzoned(sbi))
4478f847c699SChao Yu 		return true;
4479b0332a0fSChao Yu 	if (f2fs_lfs_mode(sbi) && (rw == WRITE)) {
44809720ee80SChao Yu 		if (block_unaligned_IO(inode, iocb, iter))
4481f847c699SChao Yu 			return true;
44829720ee80SChao Yu 		if (F2FS_IO_ALIGNED(sbi))
44839720ee80SChao Yu 			return true;
44849720ee80SChao Yu 	}
4485ebc29b62Shuangjianan@oppo.com 	if (is_sbi_flag_set(F2FS_I_SB(inode), SBI_CP_DISABLED))
44864354994fSDaniel Rosenberg 		return true;
44874354994fSDaniel Rosenberg 
4488f847c699SChao Yu 	return false;
4489b91050a8SHyunchul Lee }
4490b91050a8SHyunchul Lee 
44917f59b277SEric Biggers static inline bool f2fs_need_verity(const struct inode *inode, pgoff_t idx)
44927f59b277SEric Biggers {
44937f59b277SEric Biggers 	return fsverity_active(inode) &&
44947f59b277SEric Biggers 	       idx < DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
44957f59b277SEric Biggers }
44967f59b277SEric Biggers 
449783a3bfdbSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
4498d494500aSChao Yu extern void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate,
4499d494500aSChao Yu 							unsigned int type);
450083a3bfdbSJaegeuk Kim #else
4501d494500aSChao Yu #define f2fs_build_fault_attr(sbi, rate, type)		do { } while (0)
450283a3bfdbSJaegeuk Kim #endif
450383a3bfdbSJaegeuk Kim 
4504af033b2aSChao Yu static inline bool is_journalled_quota(struct f2fs_sb_info *sbi)
4505af033b2aSChao Yu {
4506af033b2aSChao Yu #ifdef CONFIG_QUOTA
45077beb01f7SChao Yu 	if (f2fs_sb_has_quota_ino(sbi))
4508af033b2aSChao Yu 		return true;
4509af033b2aSChao Yu 	if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
4510af033b2aSChao Yu 		F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
4511af033b2aSChao Yu 		F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
4512af033b2aSChao Yu 		return true;
4513af033b2aSChao Yu #endif
4514af033b2aSChao Yu 	return false;
4515af033b2aSChao Yu }
45160af725fcSJaegeuk Kim 
45174f993264SChao Yu static inline bool f2fs_block_unit_discard(struct f2fs_sb_info *sbi)
45184f993264SChao Yu {
45194f993264SChao Yu 	return F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_BLOCK;
45204f993264SChao Yu }
45214f993264SChao Yu 
452210f966bbSChao Yu #define EFSBADCRC	EBADMSG		/* Bad CRC detected */
452310f966bbSChao Yu #define EFSCORRUPTED	EUCLEAN		/* Filesystem is corrupted */
452410f966bbSChao Yu 
4525e1074d4bSChao Yu #endif /* _LINUX_F2FS_H */
4526