xref: /openbmc/linux/fs/f2fs/f2fs.h (revision c5d02785c59dd8ee20031d742003daff8df73e2d)
1d29fbcdbSNishad Kamdar /* SPDX-License-Identifier: GPL-2.0 */
20a8165d7SJaegeuk Kim /*
339a53e0cSJaegeuk Kim  * fs/f2fs/f2fs.h
439a53e0cSJaegeuk Kim  *
539a53e0cSJaegeuk Kim  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
639a53e0cSJaegeuk Kim  *             http://www.samsung.com/
739a53e0cSJaegeuk Kim  */
839a53e0cSJaegeuk Kim #ifndef _LINUX_F2FS_H
939a53e0cSJaegeuk Kim #define _LINUX_F2FS_H
1039a53e0cSJaegeuk Kim 
11f847c699SChao Yu #include <linux/uio.h>
1239a53e0cSJaegeuk Kim #include <linux/types.h>
1339a53e0cSJaegeuk Kim #include <linux/page-flags.h>
1439a53e0cSJaegeuk Kim #include <linux/buffer_head.h>
1539a53e0cSJaegeuk Kim #include <linux/slab.h>
1639a53e0cSJaegeuk Kim #include <linux/crc32.h>
1739a53e0cSJaegeuk Kim #include <linux/magic.h>
18c2d715d1SJaegeuk Kim #include <linux/kobject.h>
197bd59381SGu Zheng #include <linux/sched.h>
207c2e5963SJaegeuk Kim #include <linux/cred.h>
2139307a8eSJaegeuk Kim #include <linux/vmalloc.h>
22740432f8SJaegeuk Kim #include <linux/bio.h>
23d0239e1bSJaegeuk Kim #include <linux/blkdev.h>
240abd675eSChao Yu #include <linux/quotaops.h>
25c6a564ffSChristoph Hellwig #include <linux/part_stat.h>
2643b6573bSKeith Mok #include <crypto/hash.h>
2739a53e0cSJaegeuk Kim 
28734f0d24SDave Chinner #include <linux/fscrypt.h>
2995ae251fSEric Biggers #include <linux/fsverity.h>
30734f0d24SDave Chinner 
315d56b671SJaegeuk Kim #ifdef CONFIG_F2FS_CHECK_FS
329850cf4aSJaegeuk Kim #define f2fs_bug_on(sbi, condition)	BUG_ON(condition)
335d56b671SJaegeuk Kim #else
349850cf4aSJaegeuk Kim #define f2fs_bug_on(sbi, condition)					\
359850cf4aSJaegeuk Kim 	do {								\
369850cf4aSJaegeuk Kim 		if (unlikely(condition)) {				\
379850cf4aSJaegeuk Kim 			WARN_ON(1);					\
38caf0047eSChao Yu 			set_sbi_flag(sbi, SBI_NEED_FSCK);		\
399850cf4aSJaegeuk Kim 		}							\
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,
48d62fe971SChao Yu 	FAULT_ALLOC_BIO,
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,
592c63feadSJaegeuk Kim 	FAULT_MAX,
602c63feadSJaegeuk Kim };
612c63feadSJaegeuk Kim 
627fa750a1SArnd Bergmann #ifdef CONFIG_F2FS_FAULT_INJECTION
63d494500aSChao Yu #define F2FS_ALL_FAULT_TYPE		((1 << FAULT_MAX) - 1)
64d494500aSChao Yu 
6508796897SSheng Yong struct f2fs_fault_info {
6608796897SSheng Yong 	atomic_t inject_ops;
6708796897SSheng Yong 	unsigned int inject_rate;
6808796897SSheng Yong 	unsigned int inject_type;
6908796897SSheng Yong };
7008796897SSheng Yong 
7119880e6eSAlexey Dobriyan extern const char *f2fs_fault_name[FAULT_MAX];
7268afcf2dSTomohiro Kusumi #define IS_FAULT_SET(fi, type) ((fi)->inject_type & (1 << (type)))
732c63feadSJaegeuk Kim #endif
742c63feadSJaegeuk Kim 
7539a53e0cSJaegeuk Kim /*
7639a53e0cSJaegeuk Kim  * For mount options
7739a53e0cSJaegeuk Kim  */
7839a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_ROLL_FORWARD	0x00000002
7939a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISCARD		0x00000004
8039a53e0cSJaegeuk Kim #define F2FS_MOUNT_NOHEAP		0x00000008
8139a53e0cSJaegeuk Kim #define F2FS_MOUNT_XATTR_USER		0x00000010
8239a53e0cSJaegeuk Kim #define F2FS_MOUNT_POSIX_ACL		0x00000020
8339a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_EXT_IDENTIFY	0x00000040
84444c580fSJaegeuk Kim #define F2FS_MOUNT_INLINE_XATTR		0x00000080
851001b347SHuajun Li #define F2FS_MOUNT_INLINE_DATA		0x00000100
8634d67debSChao Yu #define F2FS_MOUNT_INLINE_DENTRY	0x00000200
8734d67debSChao Yu #define F2FS_MOUNT_FLUSH_MERGE		0x00000400
8834d67debSChao Yu #define F2FS_MOUNT_NOBARRIER		0x00000800
89d5053a34SJaegeuk Kim #define F2FS_MOUNT_FASTBOOT		0x00001000
9089672159SChao Yu #define F2FS_MOUNT_EXTENT_CACHE		0x00002000
91343f40f0SChao Yu #define F2FS_MOUNT_DATA_FLUSH		0x00008000
9273faec4dSJaegeuk Kim #define F2FS_MOUNT_FAULT_INJECTION	0x00010000
930abd675eSChao Yu #define F2FS_MOUNT_USRQUOTA		0x00080000
940abd675eSChao Yu #define F2FS_MOUNT_GRPQUOTA		0x00100000
955c57132eSChao Yu #define F2FS_MOUNT_PRJQUOTA		0x00200000
964b2414d0SChao Yu #define F2FS_MOUNT_QUOTA		0x00400000
976afc662eSChao Yu #define F2FS_MOUNT_INLINE_XATTR_SIZE	0x00800000
987e65be49SJaegeuk Kim #define F2FS_MOUNT_RESERVE_ROOT		0x01000000
994354994fSDaniel Rosenberg #define F2FS_MOUNT_DISABLE_CHECKPOINT	0x02000000
100a9117ecaSChao Yu #define F2FS_MOUNT_NORECOVERY		0x04000000
10139a53e0cSJaegeuk Kim 
10263189b78SChao Yu #define F2FS_OPTION(sbi)	((sbi)->mount_opt)
10363189b78SChao Yu #define clear_opt(sbi, option)	(F2FS_OPTION(sbi).opt &= ~F2FS_MOUNT_##option)
10463189b78SChao Yu #define set_opt(sbi, option)	(F2FS_OPTION(sbi).opt |= F2FS_MOUNT_##option)
10563189b78SChao Yu #define test_opt(sbi, option)	(F2FS_OPTION(sbi).opt & F2FS_MOUNT_##option)
10639a53e0cSJaegeuk Kim 
10739a53e0cSJaegeuk Kim #define ver_after(a, b)	(typecheck(unsigned long long, a) &&		\
10839a53e0cSJaegeuk Kim 		typecheck(unsigned long long, b) &&			\
10939a53e0cSJaegeuk Kim 		((long long)((a) - (b)) > 0))
11039a53e0cSJaegeuk Kim 
111a9841c4dSJaegeuk Kim typedef u32 block_t;	/*
112a9841c4dSJaegeuk Kim 			 * should not change u32, since it is the on-disk block
113a9841c4dSJaegeuk Kim 			 * address format, __le32.
114a9841c4dSJaegeuk Kim 			 */
11539a53e0cSJaegeuk Kim typedef u32 nid_t;
11639a53e0cSJaegeuk Kim 
1174c8ff709SChao Yu #define COMPRESS_EXT_NUM		16
1184c8ff709SChao Yu 
11939a53e0cSJaegeuk Kim struct f2fs_mount_info {
12039a53e0cSJaegeuk Kim 	unsigned int opt;
12163189b78SChao Yu 	int write_io_size_bits;		/* Write IO size bits */
12263189b78SChao Yu 	block_t root_reserved_blocks;	/* root reserved blocks */
12363189b78SChao Yu 	kuid_t s_resuid;		/* reserved blocks for uid */
12463189b78SChao Yu 	kgid_t s_resgid;		/* reserved blocks for gid */
12563189b78SChao Yu 	int active_logs;		/* # of active logs */
12663189b78SChao Yu 	int inline_xattr_size;		/* inline xattr size */
12763189b78SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
12863189b78SChao Yu 	struct f2fs_fault_info fault_info;	/* For fault injection */
12963189b78SChao Yu #endif
13063189b78SChao Yu #ifdef CONFIG_QUOTA
13163189b78SChao Yu 	/* Names of quota files with journalled quota */
13263189b78SChao Yu 	char *s_qf_names[MAXQUOTAS];
13363189b78SChao Yu 	int s_jquota_fmt;			/* Format of quota to use */
13463189b78SChao Yu #endif
13563189b78SChao Yu 	/* For which write hints are passed down to block layer */
13663189b78SChao Yu 	int whint_mode;
13763189b78SChao Yu 	int alloc_mode;			/* segment allocation policy */
13863189b78SChao Yu 	int fsync_mode;			/* fsync policy */
139b0332a0fSChao Yu 	int fs_mode;			/* fs mode: LFS or ADAPTIVE */
140bbbc34fdSChao Yu 	int bggc_mode;			/* bggc mode: off, on or sync */
141ed318a6cSEric Biggers 	struct fscrypt_dummy_context dummy_enc_ctx; /* test dummy encryption */
1421ae18f71SJaegeuk Kim 	block_t unusable_cap_perc;	/* percentage for cap */
1434d3aed70SDaniel Rosenberg 	block_t unusable_cap;		/* Amount of space allowed to be
1444d3aed70SDaniel Rosenberg 					 * unusable when disabling checkpoint
1454d3aed70SDaniel Rosenberg 					 */
1464c8ff709SChao Yu 
1474c8ff709SChao Yu 	/* For compression */
1484c8ff709SChao Yu 	unsigned char compress_algorithm;	/* algorithm type */
1494c8ff709SChao Yu 	unsigned compress_log_size;		/* cluster log size */
1504c8ff709SChao Yu 	unsigned char compress_ext_cnt;		/* extension count */
1514c8ff709SChao Yu 	unsigned char extensions[COMPRESS_EXT_NUM][F2FS_EXTENSION_LEN];	/* extensions */
15239a53e0cSJaegeuk Kim };
15339a53e0cSJaegeuk Kim 
154cde4de12SJaegeuk Kim #define F2FS_FEATURE_ENCRYPT		0x0001
1550bfd7a09SDamien Le Moal #define F2FS_FEATURE_BLKZONED		0x0002
156e65ef207SJaegeuk Kim #define F2FS_FEATURE_ATOMIC_WRITE	0x0004
1577a2af766SChao Yu #define F2FS_FEATURE_EXTRA_ATTR		0x0008
1585c57132eSChao Yu #define F2FS_FEATURE_PRJQUOTA		0x0010
159704956ecSChao Yu #define F2FS_FEATURE_INODE_CHKSUM	0x0020
1606afc662eSChao Yu #define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR	0x0040
161234a9689SJaegeuk Kim #define F2FS_FEATURE_QUOTA_INO		0x0080
1621c1d35dfSChao Yu #define F2FS_FEATURE_INODE_CRTIME	0x0100
163b7c409deSSheng Yong #define F2FS_FEATURE_LOST_FOUND		0x0200
16495ae251fSEric Biggers #define F2FS_FEATURE_VERITY		0x0400
165d440c52dSJunling Zheng #define F2FS_FEATURE_SB_CHKSUM		0x0800
1665aba5430SDaniel Rosenberg #define F2FS_FEATURE_CASEFOLD		0x1000
1674c8ff709SChao Yu #define F2FS_FEATURE_COMPRESSION	0x2000
168cde4de12SJaegeuk Kim 
1697beb01f7SChao Yu #define __F2FS_HAS_FEATURE(raw_super, mask)				\
1707beb01f7SChao Yu 	((raw_super->feature & cpu_to_le32(mask)) != 0)
1717beb01f7SChao Yu #define F2FS_HAS_FEATURE(sbi, mask)	__F2FS_HAS_FEATURE(sbi->raw_super, mask)
1727beb01f7SChao Yu #define F2FS_SET_FEATURE(sbi, mask)					\
1737beb01f7SChao Yu 	(sbi->raw_super->feature |= cpu_to_le32(mask))
1747beb01f7SChao Yu #define F2FS_CLEAR_FEATURE(sbi, mask)					\
1757beb01f7SChao Yu 	(sbi->raw_super->feature &= ~cpu_to_le32(mask))
17676f105a2SJaegeuk Kim 
17739a53e0cSJaegeuk Kim /*
1787c2e5963SJaegeuk Kim  * Default values for user and/or group using reserved blocks
1797c2e5963SJaegeuk Kim  */
1807c2e5963SJaegeuk Kim #define	F2FS_DEF_RESUID		0
1817c2e5963SJaegeuk Kim #define	F2FS_DEF_RESGID		0
1827c2e5963SJaegeuk Kim 
1837c2e5963SJaegeuk Kim /*
18439a53e0cSJaegeuk Kim  * For checkpoint manager
18539a53e0cSJaegeuk Kim  */
18639a53e0cSJaegeuk Kim enum {
18739a53e0cSJaegeuk Kim 	NAT_BITMAP,
18839a53e0cSJaegeuk Kim 	SIT_BITMAP
18939a53e0cSJaegeuk Kim };
19039a53e0cSJaegeuk Kim 
191c473f1a9SChao Yu #define	CP_UMOUNT	0x00000001
192c473f1a9SChao Yu #define	CP_FASTBOOT	0x00000002
193c473f1a9SChao Yu #define	CP_SYNC		0x00000004
194c473f1a9SChao Yu #define	CP_RECOVERY	0x00000008
195c473f1a9SChao Yu #define	CP_DISCARD	0x00000010
1961f43e2adSChao Yu #define CP_TRIMMED	0x00000020
1974354994fSDaniel Rosenberg #define CP_PAUSE	0x00000040
198b4b10061SJaegeuk Kim #define CP_RESIZE 	0x00000080
19975ab4cb8SJaegeuk Kim 
2004ddb1a4dSJaegeuk Kim #define MAX_DISCARD_BLOCKS(sbi)		BLKS_PER_SEC(sbi)
201ecc9aa00SChao Yu #define DEF_MAX_DISCARD_REQUEST		8	/* issue 8 discards per round */
202969d1b18SChao Yu #define DEF_MIN_DISCARD_ISSUE_TIME	50	/* 50 ms, if exists */
203f9d1dcedSYunlei He #define DEF_MID_DISCARD_ISSUE_TIME	500	/* 500 ms, if device busy */
204969d1b18SChao Yu #define DEF_MAX_DISCARD_ISSUE_TIME	60000	/* 60 s, if no candidates */
2058bb4f253SJaegeuk Kim #define DEF_DISCARD_URGENT_UTIL		80	/* do more discard over 80% */
20660b99b48SJaegeuk Kim #define DEF_CP_INTERVAL			60	/* 60 secs */
207dcf25fe8SChao Yu #define DEF_IDLE_INTERVAL		5	/* 5 secs */
2084354994fSDaniel Rosenberg #define DEF_DISABLE_INTERVAL		5	/* 5 secs */
209db610a64SJaegeuk Kim #define DEF_DISABLE_QUICK_INTERVAL	1	/* 1 secs */
21003f2c02dSJaegeuk Kim #define DEF_UMOUNT_DISCARD_TIMEOUT	5	/* 5 secs */
211bba681cbSJaegeuk Kim 
21275ab4cb8SJaegeuk Kim struct cp_control {
21375ab4cb8SJaegeuk Kim 	int reason;
2144b2fecc8SJaegeuk Kim 	__u64 trim_start;
2154b2fecc8SJaegeuk Kim 	__u64 trim_end;
2164b2fecc8SJaegeuk Kim 	__u64 trim_minlen;
21775ab4cb8SJaegeuk Kim };
21875ab4cb8SJaegeuk Kim 
219662befdaSChao Yu /*
220e1da7872SChao Yu  * indicate meta/data type
221662befdaSChao Yu  */
222662befdaSChao Yu enum {
223662befdaSChao Yu 	META_CP,
224662befdaSChao Yu 	META_NAT,
22581c1a0f1SChao Yu 	META_SIT,
2264c521f49SJaegeuk Kim 	META_SSA,
227b63e7be5SChao Yu 	META_MAX,
2284c521f49SJaegeuk Kim 	META_POR,
22993770ab7SChao Yu 	DATA_GENERIC,		/* check range only */
23093770ab7SChao Yu 	DATA_GENERIC_ENHANCE,	/* strong check on range and segment bitmap */
23193770ab7SChao Yu 	DATA_GENERIC_ENHANCE_READ,	/*
23293770ab7SChao Yu 					 * strong check on range and segment
23393770ab7SChao Yu 					 * bitmap but no warning due to race
23493770ab7SChao Yu 					 * condition of read on truncated area
23593770ab7SChao Yu 					 * by extent_cache
23693770ab7SChao Yu 					 */
237e1da7872SChao Yu 	META_GENERIC,
238662befdaSChao Yu };
239662befdaSChao Yu 
2406451e041SJaegeuk Kim /* for the list of ino */
2416451e041SJaegeuk Kim enum {
2426451e041SJaegeuk Kim 	ORPHAN_INO,		/* for orphan ino list */
243fff04f90SJaegeuk Kim 	APPEND_INO,		/* for append ino list */
244fff04f90SJaegeuk Kim 	UPDATE_INO,		/* for update ino list */
2450a007b97SJaegeuk Kim 	TRANS_DIR_INO,		/* for trasactions dir ino list */
24639d787beSChao Yu 	FLUSH_INO,		/* for multiple device flushing */
2476451e041SJaegeuk Kim 	MAX_INO_ENTRY,		/* max. list */
2486451e041SJaegeuk Kim };
2496451e041SJaegeuk Kim 
2506451e041SJaegeuk Kim struct ino_entry {
25139a53e0cSJaegeuk Kim 	struct list_head list;		/* list head */
25239a53e0cSJaegeuk Kim 	nid_t ino;			/* inode number */
25339d787beSChao Yu 	unsigned int dirty_device;	/* dirty device bitmap */
25439a53e0cSJaegeuk Kim };
25539a53e0cSJaegeuk Kim 
2562710fd7eSChao Yu /* for the list of inodes to be GCed */
25706292073SChao Yu struct inode_entry {
25839a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
25939a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
26039a53e0cSJaegeuk Kim };
26139a53e0cSJaegeuk Kim 
26250fa53ecSChao Yu struct fsync_node_entry {
26350fa53ecSChao Yu 	struct list_head list;	/* list head */
26450fa53ecSChao Yu 	struct page *page;	/* warm node page pointer */
26550fa53ecSChao Yu 	unsigned int seq_id;	/* sequence id */
26650fa53ecSChao Yu };
26750fa53ecSChao Yu 
268a7eeb823SChao Yu /* for the bitmap indicate blocks to be discarded */
2697fd9e544SJaegeuk Kim struct discard_entry {
2707fd9e544SJaegeuk Kim 	struct list_head list;	/* list head */
271a7eeb823SChao Yu 	block_t start_blkaddr;	/* start blockaddr of current segment */
272a7eeb823SChao Yu 	unsigned char discard_map[SIT_VBLOCK_MAP_SIZE];	/* segment discard bitmap */
2737fd9e544SJaegeuk Kim };
2747fd9e544SJaegeuk Kim 
275969d1b18SChao Yu /* default discard granularity of inner discard thread, unit: block count */
276969d1b18SChao Yu #define DEFAULT_DISCARD_GRANULARITY		16
277969d1b18SChao Yu 
278ba48a33eSChao Yu /* max discard pend list number */
279ba48a33eSChao Yu #define MAX_PLIST_NUM		512
280ba48a33eSChao Yu #define plist_idx(blk_num)	((blk_num) >= MAX_PLIST_NUM ?		\
2812f84babfSSheng Yong 					(MAX_PLIST_NUM - 1) : ((blk_num) - 1))
282ba48a33eSChao Yu 
28315469963SJaegeuk Kim enum {
28435ec7d57SChao Yu 	D_PREP,			/* initial */
28535ec7d57SChao Yu 	D_PARTIAL,		/* partially submitted */
28635ec7d57SChao Yu 	D_SUBMIT,		/* all submitted */
28735ec7d57SChao Yu 	D_DONE,			/* finished */
28815469963SJaegeuk Kim };
28915469963SJaegeuk Kim 
290004b6862SChao Yu struct discard_info {
291b01a9201SJaegeuk Kim 	block_t lstart;			/* logical start address */
292b01a9201SJaegeuk Kim 	block_t len;			/* length */
293004b6862SChao Yu 	block_t start;			/* actual start address in dev */
294004b6862SChao Yu };
295004b6862SChao Yu 
296b01a9201SJaegeuk Kim struct discard_cmd {
297004b6862SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
298004b6862SChao Yu 	union {
299004b6862SChao Yu 		struct {
300004b6862SChao Yu 			block_t lstart;	/* logical start address */
301004b6862SChao Yu 			block_t len;	/* length */
302004b6862SChao Yu 			block_t start;	/* actual start address in dev */
303004b6862SChao Yu 		};
304004b6862SChao Yu 		struct discard_info di;	/* discard info */
305004b6862SChao Yu 
306004b6862SChao Yu 	};
307b01a9201SJaegeuk Kim 	struct list_head list;		/* command list */
308b01a9201SJaegeuk Kim 	struct completion wait;		/* compleation */
309c81abe34SJaegeuk Kim 	struct block_device *bdev;	/* bdev */
310ec9895adSChao Yu 	unsigned short ref;		/* reference count */
3119a744b92SChao Yu 	unsigned char state;		/* state */
31272691af6SJaegeuk Kim 	unsigned char queued;		/* queued discard */
313c81abe34SJaegeuk Kim 	int error;			/* bio error */
31435ec7d57SChao Yu 	spinlock_t lock;		/* for state/bio_ref updating */
31535ec7d57SChao Yu 	unsigned short bio_ref;		/* bio reference count */
316275b66b0SChao Yu };
317275b66b0SChao Yu 
31878997b56SChao Yu enum {
31978997b56SChao Yu 	DPOLICY_BG,
32078997b56SChao Yu 	DPOLICY_FORCE,
32178997b56SChao Yu 	DPOLICY_FSTRIM,
32278997b56SChao Yu 	DPOLICY_UMOUNT,
32378997b56SChao Yu 	MAX_DPOLICY,
32478997b56SChao Yu };
32578997b56SChao Yu 
326ecc9aa00SChao Yu struct discard_policy {
32778997b56SChao Yu 	int type;			/* type of discard */
328ecc9aa00SChao Yu 	unsigned int min_interval;	/* used for candidates exist */
329f9d1dcedSYunlei He 	unsigned int mid_interval;	/* used for device busy */
330ecc9aa00SChao Yu 	unsigned int max_interval;	/* used for candidates not exist */
331ecc9aa00SChao Yu 	unsigned int max_requests;	/* # of discards issued per round */
332ecc9aa00SChao Yu 	unsigned int io_aware_gran;	/* minimum granularity discard not be aware of I/O */
333ecc9aa00SChao Yu 	bool io_aware;			/* issue discard in idle time */
334ecc9aa00SChao Yu 	bool sync;			/* submit discard with REQ_SYNC flag */
33520ee4382SChao Yu 	bool ordered;			/* issue discard by lba order */
3366ce48b0cSChao Yu 	bool timeout;			/* discard timeout for put_super */
33778997b56SChao Yu 	unsigned int granularity;	/* discard granularity */
338ecc9aa00SChao Yu };
339ecc9aa00SChao Yu 
3400b54fb84SJaegeuk Kim struct discard_cmd_control {
34115469963SJaegeuk Kim 	struct task_struct *f2fs_issue_discard;	/* discard thread */
34246f84c2cSChao Yu 	struct list_head entry_list;		/* 4KB discard entry list */
343ba48a33eSChao Yu 	struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
34446f84c2cSChao Yu 	struct list_head wait_list;		/* store on-flushing entries */
3458412663dSChao Yu 	struct list_head fstrim_list;		/* in-flight discard from fstrim */
34615469963SJaegeuk Kim 	wait_queue_head_t discard_wait_queue;	/* waiting queue for wake-up */
347969d1b18SChao Yu 	unsigned int discard_wake;		/* to wake up discard thread */
34815469963SJaegeuk Kim 	struct mutex cmd_lock;
349d618ebafSChao Yu 	unsigned int nr_discards;		/* # of discards in the list */
350d618ebafSChao Yu 	unsigned int max_discards;		/* max. discards to be issued */
351969d1b18SChao Yu 	unsigned int discard_granularity;	/* discard granularity */
352d84d1cbdSChao Yu 	unsigned int undiscard_blks;		/* # of undiscard blocks */
35320ee4382SChao Yu 	unsigned int next_pos;			/* next discard position */
3548b8dd65fSChao Yu 	atomic_t issued_discard;		/* # of issued discard */
35572691af6SJaegeuk Kim 	atomic_t queued_discard;		/* # of queued discard */
3565f32366aSChao Yu 	atomic_t discard_cmd_cnt;		/* # of cached cmd count */
3574dada3fdSChao Yu 	struct rb_root_cached root;		/* root of discard rb-tree */
35867fce70bSChao Yu 	bool rbtree_check;			/* config for consistence check */
35939a53e0cSJaegeuk Kim };
36039a53e0cSJaegeuk Kim 
36139a53e0cSJaegeuk Kim /* for the list of fsync inodes, used only during recovery */
36239a53e0cSJaegeuk Kim struct fsync_inode_entry {
36339a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
36439a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
365c52e1b10SJaegeuk Kim 	block_t blkaddr;	/* block address locating the last fsync */
366c52e1b10SJaegeuk Kim 	block_t last_dentry;	/* block address locating the last dentry */
36739a53e0cSJaegeuk Kim };
36839a53e0cSJaegeuk Kim 
36968afcf2dSTomohiro Kusumi #define nats_in_cursum(jnl)		(le16_to_cpu((jnl)->n_nats))
37068afcf2dSTomohiro Kusumi #define sits_in_cursum(jnl)		(le16_to_cpu((jnl)->n_sits))
37139a53e0cSJaegeuk Kim 
37268afcf2dSTomohiro Kusumi #define nat_in_journal(jnl, i)		((jnl)->nat_j.entries[i].ne)
37368afcf2dSTomohiro Kusumi #define nid_in_journal(jnl, i)		((jnl)->nat_j.entries[i].nid)
37468afcf2dSTomohiro Kusumi #define sit_in_journal(jnl, i)		((jnl)->sit_j.entries[i].se)
37568afcf2dSTomohiro Kusumi #define segno_in_journal(jnl, i)	((jnl)->sit_j.entries[i].segno)
37639a53e0cSJaegeuk Kim 
377dfc08a12SChao Yu #define MAX_NAT_JENTRIES(jnl)	(NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
378dfc08a12SChao Yu #define MAX_SIT_JENTRIES(jnl)	(SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
379309cc2b6SJaegeuk Kim 
380dfc08a12SChao Yu static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
38139a53e0cSJaegeuk Kim {
382dfc08a12SChao Yu 	int before = nats_in_cursum(journal);
383cac5a3d8SDongOh Shin 
384dfc08a12SChao Yu 	journal->n_nats = cpu_to_le16(before + i);
38539a53e0cSJaegeuk Kim 	return before;
38639a53e0cSJaegeuk Kim }
38739a53e0cSJaegeuk Kim 
388dfc08a12SChao Yu static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
38939a53e0cSJaegeuk Kim {
390dfc08a12SChao Yu 	int before = sits_in_cursum(journal);
391cac5a3d8SDongOh Shin 
392dfc08a12SChao Yu 	journal->n_sits = cpu_to_le16(before + i);
39339a53e0cSJaegeuk Kim 	return before;
39439a53e0cSJaegeuk Kim }
39539a53e0cSJaegeuk Kim 
396dfc08a12SChao Yu static inline bool __has_cursum_space(struct f2fs_journal *journal,
397dfc08a12SChao Yu 							int size, int type)
398184a5cd2SChao Yu {
399184a5cd2SChao Yu 	if (type == NAT_JOURNAL)
400dfc08a12SChao Yu 		return size <= MAX_NAT_JENTRIES(journal);
401dfc08a12SChao Yu 	return size <= MAX_SIT_JENTRIES(journal);
402184a5cd2SChao Yu }
403184a5cd2SChao Yu 
40439a53e0cSJaegeuk Kim /*
4053357af8fSEric Biggers  * f2fs-specific ioctl commands
406e9750824SNamjae Jeon  */
40788b88a66SJaegeuk Kim #define F2FS_IOCTL_MAGIC		0xf5
40888b88a66SJaegeuk Kim #define F2FS_IOC_START_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 1)
40988b88a66SJaegeuk Kim #define F2FS_IOC_COMMIT_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 2)
41002a1335fSJaegeuk Kim #define F2FS_IOC_START_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 3)
4111e84371fSJaegeuk Kim #define F2FS_IOC_RELEASE_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 4)
4121e84371fSJaegeuk Kim #define F2FS_IOC_ABORT_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 5)
413d07efb50SJaegeuk Kim #define F2FS_IOC_GARBAGE_COLLECT	_IOW(F2FS_IOCTL_MAGIC, 6, __u32)
414456b88e4SChao Yu #define F2FS_IOC_WRITE_CHECKPOINT	_IO(F2FS_IOCTL_MAGIC, 7)
415d07efb50SJaegeuk Kim #define F2FS_IOC_DEFRAGMENT		_IOWR(F2FS_IOCTL_MAGIC, 8,	\
416d07efb50SJaegeuk Kim 						struct f2fs_defragment)
4174dd6f977SJaegeuk Kim #define F2FS_IOC_MOVE_RANGE		_IOWR(F2FS_IOCTL_MAGIC, 9,	\
4184dd6f977SJaegeuk Kim 						struct f2fs_move_range)
419e066b83cSJaegeuk Kim #define F2FS_IOC_FLUSH_DEVICE		_IOW(F2FS_IOCTL_MAGIC, 10,	\
420e066b83cSJaegeuk Kim 						struct f2fs_flush_device)
42134dc77adSJaegeuk Kim #define F2FS_IOC_GARBAGE_COLLECT_RANGE	_IOW(F2FS_IOCTL_MAGIC, 11,	\
42234dc77adSJaegeuk Kim 						struct f2fs_gc_range)
423e65ef207SJaegeuk Kim #define F2FS_IOC_GET_FEATURES		_IOR(F2FS_IOCTL_MAGIC, 12, __u32)
4241ad71a27SJaegeuk Kim #define F2FS_IOC_SET_PIN_FILE		_IOW(F2FS_IOCTL_MAGIC, 13, __u32)
4251ad71a27SJaegeuk Kim #define F2FS_IOC_GET_PIN_FILE		_IOR(F2FS_IOCTL_MAGIC, 14, __u32)
426c4020b2dSChao Yu #define F2FS_IOC_PRECACHE_EXTENTS	_IO(F2FS_IOCTL_MAGIC, 15)
42704f0b2eaSQiuyang Sun #define F2FS_IOC_RESIZE_FS		_IOW(F2FS_IOCTL_MAGIC, 16, __u64)
428439dfb10SChao Yu #define F2FS_IOC_GET_COMPRESS_BLOCKS	_IOR(F2FS_IOCTL_MAGIC, 17, __u64)
429ef8d563fSChao Yu #define F2FS_IOC_RELEASE_COMPRESS_BLOCKS				\
430ef8d563fSChao Yu 					_IOR(F2FS_IOCTL_MAGIC, 18, __u64)
431c75488fbSChao Yu #define F2FS_IOC_RESERVE_COMPRESS_BLOCKS				\
432c75488fbSChao Yu 					_IOR(F2FS_IOCTL_MAGIC, 19, __u64)
4339af84648SDaeho Jeong #define F2FS_IOC_SEC_TRIM_FILE		_IOW(F2FS_IOCTL_MAGIC, 20,	\
4349af84648SDaeho Jeong 						struct f2fs_sectrim_range)
43588b88a66SJaegeuk Kim 
4361abff93dSJaegeuk Kim /*
4371abff93dSJaegeuk Kim  * should be same as XFS_IOC_GOINGDOWN.
4381abff93dSJaegeuk Kim  * Flags for going down operation used by FS_IOC_GOINGDOWN
4391abff93dSJaegeuk Kim  */
4401abff93dSJaegeuk Kim #define F2FS_IOC_SHUTDOWN	_IOR('X', 125, __u32)	/* Shutdown */
4411abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_FULLSYNC	0x0	/* going down with full sync */
4421abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_METASYNC	0x1	/* going down with metadata */
4431abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_NOSYNC		0x2	/* going down */
444c912a829SJaegeuk Kim #define F2FS_GOING_DOWN_METAFLUSH	0x3	/* going down with meta flush */
4450cd6d9b0SJaegeuk Kim #define F2FS_GOING_DOWN_NEED_FSCK	0x4	/* going down to trigger fsck */
4461abff93dSJaegeuk Kim 
4479af84648SDaeho Jeong /*
4489af84648SDaeho Jeong  * Flags used by F2FS_IOC_SEC_TRIM_FILE
4499af84648SDaeho Jeong  */
4509af84648SDaeho Jeong #define F2FS_TRIM_FILE_DISCARD		0x1	/* send discard command */
4519af84648SDaeho Jeong #define F2FS_TRIM_FILE_ZEROOUT		0x2	/* zero out */
4529af84648SDaeho Jeong #define F2FS_TRIM_FILE_MASK		0x3
4539af84648SDaeho Jeong 
45434dc77adSJaegeuk Kim struct f2fs_gc_range {
45534dc77adSJaegeuk Kim 	u32 sync;
45634dc77adSJaegeuk Kim 	u64 start;
45734dc77adSJaegeuk Kim 	u64 len;
45834dc77adSJaegeuk Kim };
45934dc77adSJaegeuk Kim 
460d323d005SChao Yu struct f2fs_defragment {
461d323d005SChao Yu 	u64 start;
462d323d005SChao Yu 	u64 len;
463d323d005SChao Yu };
464d323d005SChao Yu 
4654dd6f977SJaegeuk Kim struct f2fs_move_range {
4664dd6f977SJaegeuk Kim 	u32 dst_fd;		/* destination fd */
4674dd6f977SJaegeuk Kim 	u64 pos_in;		/* start position in src_fd */
4684dd6f977SJaegeuk Kim 	u64 pos_out;		/* start position in dst_fd */
4694dd6f977SJaegeuk Kim 	u64 len;		/* size to move */
4704dd6f977SJaegeuk Kim };
4714dd6f977SJaegeuk Kim 
472e066b83cSJaegeuk Kim struct f2fs_flush_device {
473e066b83cSJaegeuk Kim 	u32 dev_num;		/* device number to flush */
474e066b83cSJaegeuk Kim 	u32 segments;		/* # of segments to flush */
475e066b83cSJaegeuk Kim };
476e066b83cSJaegeuk Kim 
4779af84648SDaeho Jeong struct f2fs_sectrim_range {
4789af84648SDaeho Jeong 	u64 start;
4799af84648SDaeho Jeong 	u64 len;
4809af84648SDaeho Jeong 	u64 flags;
4819af84648SDaeho Jeong };
4829af84648SDaeho Jeong 
483f2470371SChao Yu /* for inline stuff */
484f2470371SChao Yu #define DEF_INLINE_RESERVED_SIZE	1
4857a2af766SChao Yu static inline int get_extra_isize(struct inode *inode);
4866afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode);
4877a2af766SChao Yu #define MAX_INLINE_DATA(inode)	(sizeof(__le32) *			\
4887a2af766SChao Yu 				(CUR_ADDRS_PER_INODE(inode) -		\
489b323fd28SChao Yu 				get_inline_xattr_addrs(inode) -	\
4906afc662eSChao Yu 				DEF_INLINE_RESERVED_SIZE))
491f2470371SChao Yu 
492f2470371SChao Yu /* for inline dir */
493f2470371SChao Yu #define NR_INLINE_DENTRY(inode)	(MAX_INLINE_DATA(inode) * BITS_PER_BYTE / \
494f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
495f2470371SChao Yu 				BITS_PER_BYTE + 1))
496f91108b8SGeert Uytterhoeven #define INLINE_DENTRY_BITMAP_SIZE(inode) \
497f91108b8SGeert Uytterhoeven 	DIV_ROUND_UP(NR_INLINE_DENTRY(inode), BITS_PER_BYTE)
498f2470371SChao Yu #define INLINE_RESERVED_SIZE(inode)	(MAX_INLINE_DATA(inode) - \
499f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
500f2470371SChao Yu 				NR_INLINE_DENTRY(inode) + \
501f2470371SChao Yu 				INLINE_DENTRY_BITMAP_SIZE(inode)))
502f2470371SChao Yu 
503e9750824SNamjae Jeon /*
50439a53e0cSJaegeuk Kim  * For INODE and NODE manager
50539a53e0cSJaegeuk Kim  */
5067b3cd7d6SJaegeuk Kim /* for directory operations */
50743c780baSEric Biggers 
50843c780baSEric Biggers struct f2fs_filename {
50943c780baSEric Biggers 	/*
51043c780baSEric Biggers 	 * The filename the user specified.  This is NULL for some
51143c780baSEric Biggers 	 * filesystem-internal operations, e.g. converting an inline directory
51243c780baSEric Biggers 	 * to a non-inline one, or roll-forward recovering an encrypted dentry.
51343c780baSEric Biggers 	 */
51443c780baSEric Biggers 	const struct qstr *usr_fname;
51543c780baSEric Biggers 
51643c780baSEric Biggers 	/*
51743c780baSEric Biggers 	 * The on-disk filename.  For encrypted directories, this is encrypted.
51843c780baSEric Biggers 	 * This may be NULL for lookups in an encrypted dir without the key.
51943c780baSEric Biggers 	 */
52043c780baSEric Biggers 	struct fscrypt_str disk_name;
52143c780baSEric Biggers 
52243c780baSEric Biggers 	/* The dirhash of this filename */
52343c780baSEric Biggers 	f2fs_hash_t hash;
52443c780baSEric Biggers 
52543c780baSEric Biggers #ifdef CONFIG_FS_ENCRYPTION
52643c780baSEric Biggers 	/*
52743c780baSEric Biggers 	 * For lookups in encrypted directories: either the buffer backing
52843c780baSEric Biggers 	 * disk_name, or a buffer that holds the decoded no-key name.
52943c780baSEric Biggers 	 */
53043c780baSEric Biggers 	struct fscrypt_str crypto_buf;
53143c780baSEric Biggers #endif
53243c780baSEric Biggers #ifdef CONFIG_UNICODE
53343c780baSEric Biggers 	/*
53443c780baSEric Biggers 	 * For casefolded directories: the casefolded name, but it's left NULL
53543c780baSEric Biggers 	 * if the original name is not valid Unicode or if the filesystem is
53643c780baSEric Biggers 	 * doing an internal operation where usr_fname is also NULL.  In these
53743c780baSEric Biggers 	 * cases we fall back to treating the name as an opaque byte sequence.
53843c780baSEric Biggers 	 */
53943c780baSEric Biggers 	struct fscrypt_str cf_name;
54043c780baSEric Biggers #endif
54143c780baSEric Biggers };
54243c780baSEric Biggers 
5437b3cd7d6SJaegeuk Kim struct f2fs_dentry_ptr {
544d8c6822aSJaegeuk Kim 	struct inode *inode;
54576a9dd85SChao Yu 	void *bitmap;
5467b3cd7d6SJaegeuk Kim 	struct f2fs_dir_entry *dentry;
5477b3cd7d6SJaegeuk Kim 	__u8 (*filename)[F2FS_SLOT_LEN];
5487b3cd7d6SJaegeuk Kim 	int max;
54976a9dd85SChao Yu 	int nr_bitmap;
5507b3cd7d6SJaegeuk Kim };
5517b3cd7d6SJaegeuk Kim 
55264c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_block(struct inode *inode,
55364c24ecbSTomohiro Kusumi 		struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
5547b3cd7d6SJaegeuk Kim {
555d8c6822aSJaegeuk Kim 	d->inode = inode;
5567b3cd7d6SJaegeuk Kim 	d->max = NR_DENTRY_IN_BLOCK;
55776a9dd85SChao Yu 	d->nr_bitmap = SIZE_OF_DENTRY_BITMAP;
558c79d1520SYunlong Song 	d->bitmap = t->dentry_bitmap;
5597b3cd7d6SJaegeuk Kim 	d->dentry = t->dentry;
5607b3cd7d6SJaegeuk Kim 	d->filename = t->filename;
56164c24ecbSTomohiro Kusumi }
562cac5a3d8SDongOh Shin 
56364c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_inline(struct inode *inode,
564f2470371SChao Yu 					struct f2fs_dentry_ptr *d, void *t)
56564c24ecbSTomohiro Kusumi {
566f2470371SChao Yu 	int entry_cnt = NR_INLINE_DENTRY(inode);
567f2470371SChao Yu 	int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(inode);
568f2470371SChao Yu 	int reserved_size = INLINE_RESERVED_SIZE(inode);
569f2470371SChao Yu 
57064c24ecbSTomohiro Kusumi 	d->inode = inode;
571f2470371SChao Yu 	d->max = entry_cnt;
572f2470371SChao Yu 	d->nr_bitmap = bitmap_size;
573f2470371SChao Yu 	d->bitmap = t;
574f2470371SChao Yu 	d->dentry = t + bitmap_size + reserved_size;
575f2470371SChao Yu 	d->filename = t + bitmap_size + reserved_size +
576f2470371SChao Yu 					SIZE_OF_DIR_ENTRY * entry_cnt;
5777b3cd7d6SJaegeuk Kim }
5787b3cd7d6SJaegeuk Kim 
579dbe6a5ffSJaegeuk Kim /*
580dbe6a5ffSJaegeuk Kim  * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
581dbe6a5ffSJaegeuk Kim  * as its node offset to distinguish from index node blocks.
582dbe6a5ffSJaegeuk Kim  * But some bits are used to mark the node block.
58339a53e0cSJaegeuk Kim  */
584dbe6a5ffSJaegeuk Kim #define XATTR_NODE_OFFSET	((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
585dbe6a5ffSJaegeuk Kim 				>> OFFSET_BIT_SHIFT)
586266e97a8SJaegeuk Kim enum {
587266e97a8SJaegeuk Kim 	ALLOC_NODE,			/* allocate a new node page if needed */
588266e97a8SJaegeuk Kim 	LOOKUP_NODE,			/* look up a node without readahead */
589266e97a8SJaegeuk Kim 	LOOKUP_NODE_RA,			/*
590266e97a8SJaegeuk Kim 					 * look up a node with readahead called
5914f4124d0SChao Yu 					 * by get_data_block.
59239a53e0cSJaegeuk Kim 					 */
593266e97a8SJaegeuk Kim };
594266e97a8SJaegeuk Kim 
5957735730dSChao Yu #define DEFAULT_RETRY_IO_COUNT	8	/* maximum retry read IO count */
5967735730dSChao Yu 
5975df7731fSChao Yu /* congestion wait timeout value, default: 20ms */
5985df7731fSChao Yu #define	DEFAULT_IO_TIMEOUT	(msecs_to_jiffies(20))
5995df7731fSChao Yu 
600af033b2aSChao Yu /* maximum retry quota flush count */
601af033b2aSChao Yu #define DEFAULT_RETRY_QUOTA_FLUSH_COUNT		8
602af033b2aSChao Yu 
603a6db67f0SJaegeuk Kim #define F2FS_LINK_MAX	0xffffffff	/* maximum link count per file */
60439a53e0cSJaegeuk Kim 
605817202d9SChao Yu #define MAX_DIR_RA_PAGES	4	/* maximum ra pages of dir */
606817202d9SChao Yu 
60739a53e0cSJaegeuk Kim /* for in-memory extent cache entry */
60813054c54SChao Yu #define F2FS_MIN_EXTENT_LEN	64	/* minimum extent length */
60913054c54SChao Yu 
61013054c54SChao Yu /* number of extent info in extent cache we try to shrink */
61113054c54SChao Yu #define EXTENT_CACHE_SHRINK_NUMBER	128
612c11abd1aSJaegeuk Kim 
61354c2258cSChao Yu struct rb_entry {
61454c2258cSChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
61554c2258cSChao Yu 	unsigned int ofs;		/* start offset of the entry */
61654c2258cSChao Yu 	unsigned int len;		/* length of the entry */
61754c2258cSChao Yu };
61854c2258cSChao Yu 
61939a53e0cSJaegeuk Kim struct extent_info {
62039a53e0cSJaegeuk Kim 	unsigned int fofs;		/* start offset in a file */
621111d2495SMasanari Iida 	unsigned int len;		/* length of the extent */
62254c2258cSChao Yu 	u32 blk;			/* start block address of the extent */
62339a53e0cSJaegeuk Kim };
62439a53e0cSJaegeuk Kim 
62513054c54SChao Yu struct extent_node {
626c0362117SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
62713054c54SChao Yu 	struct extent_info ei;		/* extent info */
62854c2258cSChao Yu 	struct list_head list;		/* node in global extent list of sbi */
629201ef5e0SHou Pengyang 	struct extent_tree *et;		/* extent tree pointer */
63013054c54SChao Yu };
63113054c54SChao Yu 
63213054c54SChao Yu struct extent_tree {
63313054c54SChao Yu 	nid_t ino;			/* inode number */
6344dada3fdSChao Yu 	struct rb_root_cached root;	/* root of extent info rb-tree */
63562c8af65SChao Yu 	struct extent_node *cached_en;	/* recently accessed extent node */
6363e72f721SJaegeuk Kim 	struct extent_info largest;	/* largested extent info */
637137d09f0SJaegeuk Kim 	struct list_head list;		/* to be used by sbi->zombie_list */
63813054c54SChao Yu 	rwlock_t lock;			/* protect extent info rb-tree */
63968e35385SChao Yu 	atomic_t node_cnt;		/* # of extent node in rb-tree*/
640b430f726SZhikang Zhang 	bool largest_updated;		/* largest extent updated */
64113054c54SChao Yu };
64213054c54SChao Yu 
64339a53e0cSJaegeuk Kim /*
644003a3e1dSJaegeuk Kim  * This structure is taken from ext4_map_blocks.
645003a3e1dSJaegeuk Kim  *
646003a3e1dSJaegeuk Kim  * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
647003a3e1dSJaegeuk Kim  */
648003a3e1dSJaegeuk Kim #define F2FS_MAP_NEW		(1 << BH_New)
649003a3e1dSJaegeuk Kim #define F2FS_MAP_MAPPED		(1 << BH_Mapped)
6507f63eb77SJaegeuk Kim #define F2FS_MAP_UNWRITTEN	(1 << BH_Unwritten)
6517f63eb77SJaegeuk Kim #define F2FS_MAP_FLAGS		(F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
6527f63eb77SJaegeuk Kim 				F2FS_MAP_UNWRITTEN)
653003a3e1dSJaegeuk Kim 
654003a3e1dSJaegeuk Kim struct f2fs_map_blocks {
655003a3e1dSJaegeuk Kim 	block_t m_pblk;
656003a3e1dSJaegeuk Kim 	block_t m_lblk;
657003a3e1dSJaegeuk Kim 	unsigned int m_len;
658003a3e1dSJaegeuk Kim 	unsigned int m_flags;
659da85985cSChao Yu 	pgoff_t *m_next_pgofs;		/* point next possible non-hole pgofs */
660c4020b2dSChao Yu 	pgoff_t *m_next_extent;		/* point to next possible extent */
661d5097be5SHyunchul Lee 	int m_seg_type;
662f9d6d059SChao Yu 	bool m_may_create;		/* indicate it is from write path */
663003a3e1dSJaegeuk Kim };
664003a3e1dSJaegeuk Kim 
665e2b4e2bcSChao Yu /* for flag in get_data_block */
666f2220c7fSQiuyang Sun enum {
667f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_DEFAULT,
668f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_FIEMAP,
669f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_BMAP,
6700a4daae5SJaegeuk Kim 	F2FS_GET_BLOCK_DIO,
671f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_DIO,
672f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_AIO,
673c4020b2dSChao Yu 	F2FS_GET_BLOCK_PRECACHE,
674f2220c7fSQiuyang Sun };
675e2b4e2bcSChao Yu 
676003a3e1dSJaegeuk Kim /*
67739a53e0cSJaegeuk Kim  * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
67839a53e0cSJaegeuk Kim  */
67939a53e0cSJaegeuk Kim #define FADVISE_COLD_BIT	0x01
680354a3399SJaegeuk Kim #define FADVISE_LOST_PINO_BIT	0x02
681cde4de12SJaegeuk Kim #define FADVISE_ENCRYPT_BIT	0x04
682e7d55452SJaegeuk Kim #define FADVISE_ENC_NAME_BIT	0x08
68326787236SJaegeuk Kim #define FADVISE_KEEP_SIZE_BIT	0x10
684b6a06cbbSChao Yu #define FADVISE_HOT_BIT		0x20
68595ae251fSEric Biggers #define FADVISE_VERITY_BIT	0x40
68639a53e0cSJaegeuk Kim 
687797c1cb5SChao Yu #define FADVISE_MODIFIABLE_BITS	(FADVISE_COLD_BIT | FADVISE_HOT_BIT)
688797c1cb5SChao Yu 
689b5492af7SJaegeuk Kim #define file_is_cold(inode)	is_file(inode, FADVISE_COLD_BIT)
690b5492af7SJaegeuk Kim #define file_wrong_pino(inode)	is_file(inode, FADVISE_LOST_PINO_BIT)
691b5492af7SJaegeuk Kim #define file_set_cold(inode)	set_file(inode, FADVISE_COLD_BIT)
692b5492af7SJaegeuk Kim #define file_lost_pino(inode)	set_file(inode, FADVISE_LOST_PINO_BIT)
693b5492af7SJaegeuk Kim #define file_clear_cold(inode)	clear_file(inode, FADVISE_COLD_BIT)
694b5492af7SJaegeuk Kim #define file_got_pino(inode)	clear_file(inode, FADVISE_LOST_PINO_BIT)
695cde4de12SJaegeuk Kim #define file_is_encrypt(inode)	is_file(inode, FADVISE_ENCRYPT_BIT)
696cde4de12SJaegeuk Kim #define file_set_encrypt(inode)	set_file(inode, FADVISE_ENCRYPT_BIT)
697cde4de12SJaegeuk Kim #define file_clear_encrypt(inode) clear_file(inode, FADVISE_ENCRYPT_BIT)
698e7d55452SJaegeuk Kim #define file_enc_name(inode)	is_file(inode, FADVISE_ENC_NAME_BIT)
699e7d55452SJaegeuk Kim #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
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)
702b6a06cbbSChao Yu #define file_is_hot(inode)	is_file(inode, FADVISE_HOT_BIT)
703b6a06cbbSChao Yu #define file_set_hot(inode)	set_file(inode, FADVISE_HOT_BIT)
704b6a06cbbSChao Yu #define file_clear_hot(inode)	clear_file(inode, FADVISE_HOT_BIT)
70595ae251fSEric Biggers #define file_is_verity(inode)	is_file(inode, FADVISE_VERITY_BIT)
70695ae251fSEric Biggers #define file_set_verity(inode)	set_file(inode, FADVISE_VERITY_BIT)
707cde4de12SJaegeuk Kim 
708ab9fa662SJaegeuk Kim #define DEF_DIR_LEVEL		0
709ab9fa662SJaegeuk Kim 
7102ef79ecbSChao Yu enum {
7112ef79ecbSChao Yu 	GC_FAILURE_PIN,
7122ef79ecbSChao Yu 	GC_FAILURE_ATOMIC,
7132ef79ecbSChao Yu 	MAX_GC_FAILURE
7142ef79ecbSChao Yu };
7152ef79ecbSChao Yu 
7167653b9d8SChao Yu /* used for f2fs_inode_info->flags */
7177653b9d8SChao Yu enum {
7187653b9d8SChao Yu 	FI_NEW_INODE,		/* indicate newly allocated inode */
7197653b9d8SChao Yu 	FI_DIRTY_INODE,		/* indicate inode is dirty or not */
7207653b9d8SChao Yu 	FI_AUTO_RECOVER,	/* indicate inode is recoverable */
7217653b9d8SChao Yu 	FI_DIRTY_DIR,		/* indicate directory has dirty pages */
7227653b9d8SChao Yu 	FI_INC_LINK,		/* need to increment i_nlink */
7237653b9d8SChao Yu 	FI_ACL_MODE,		/* indicate acl mode */
7247653b9d8SChao Yu 	FI_NO_ALLOC,		/* should not allocate any blocks */
7257653b9d8SChao Yu 	FI_FREE_NID,		/* free allocated nide */
7267653b9d8SChao Yu 	FI_NO_EXTENT,		/* not to use the extent cache */
7277653b9d8SChao Yu 	FI_INLINE_XATTR,	/* used for inline xattr */
7287653b9d8SChao Yu 	FI_INLINE_DATA,		/* used for inline data*/
7297653b9d8SChao Yu 	FI_INLINE_DENTRY,	/* used for inline dentry */
7307653b9d8SChao Yu 	FI_APPEND_WRITE,	/* inode has appended data */
7317653b9d8SChao Yu 	FI_UPDATE_WRITE,	/* inode has in-place-update data */
7327653b9d8SChao Yu 	FI_NEED_IPU,		/* used for ipu per file */
7337653b9d8SChao Yu 	FI_ATOMIC_FILE,		/* indicate atomic file */
7347653b9d8SChao Yu 	FI_ATOMIC_COMMIT,	/* indicate the state of atomical committing */
7357653b9d8SChao Yu 	FI_VOLATILE_FILE,	/* indicate volatile file */
7367653b9d8SChao Yu 	FI_FIRST_BLOCK_WRITTEN,	/* indicate #0 data block was written */
7377653b9d8SChao Yu 	FI_DROP_CACHE,		/* drop dirty page cache */
7387653b9d8SChao Yu 	FI_DATA_EXIST,		/* indicate data exists */
7397653b9d8SChao Yu 	FI_INLINE_DOTS,		/* indicate inline dot dentries */
7407653b9d8SChao Yu 	FI_DO_DEFRAG,		/* indicate defragment is running */
7417653b9d8SChao Yu 	FI_DIRTY_FILE,		/* indicate regular/symlink has dirty pages */
7427653b9d8SChao Yu 	FI_NO_PREALLOC,		/* indicate skipped preallocated blocks */
7437653b9d8SChao Yu 	FI_HOT_DATA,		/* indicate file is hot */
7447653b9d8SChao Yu 	FI_EXTRA_ATTR,		/* indicate file has extra attribute */
7457653b9d8SChao Yu 	FI_PROJ_INHERIT,	/* indicate file inherits projectid */
7467653b9d8SChao Yu 	FI_PIN_FILE,		/* indicate file should not be gced */
7477653b9d8SChao Yu 	FI_ATOMIC_REVOKE_REQUEST, /* request to drop atomic data */
7487653b9d8SChao Yu 	FI_VERITY_IN_PROGRESS,	/* building fs-verity Merkle tree */
7497653b9d8SChao Yu 	FI_COMPRESSED_FILE,	/* indicate file's data can be compressed */
7507653b9d8SChao Yu 	FI_MMAP_FILE,		/* indicate file was mmapped */
7517653b9d8SChao Yu 	FI_MAX,			/* max flag, never be used */
7527653b9d8SChao Yu };
7537653b9d8SChao Yu 
75439a53e0cSJaegeuk Kim struct f2fs_inode_info {
75539a53e0cSJaegeuk Kim 	struct inode vfs_inode;		/* serve a vfs inode */
75639a53e0cSJaegeuk Kim 	unsigned long i_flags;		/* keep an inode flags for ioctl */
75739a53e0cSJaegeuk Kim 	unsigned char i_advise;		/* use to give file attribute hints */
75838431545SJaegeuk Kim 	unsigned char i_dir_level;	/* use for dentry level for large dir */
7591ad71a27SJaegeuk Kim 	unsigned int i_current_depth;	/* only for directory depth */
7602ef79ecbSChao Yu 	/* for gc failure statistic */
7612ef79ecbSChao Yu 	unsigned int i_gc_failures[MAX_GC_FAILURE];
7626666e6aaSJaegeuk Kim 	unsigned int i_pino;		/* parent inode number */
76339a53e0cSJaegeuk Kim 	umode_t i_acl_mode;		/* keep file acl mode temporarily */
76439a53e0cSJaegeuk Kim 
76539a53e0cSJaegeuk Kim 	/* Use below internally in f2fs*/
7667653b9d8SChao Yu 	unsigned long flags[BITS_TO_LONGS(FI_MAX)];	/* use to pass per-file flags */
767d928bfbfSJaegeuk Kim 	struct rw_semaphore i_sem;	/* protect fi info */
768204706c7SJaegeuk Kim 	atomic_t dirty_pages;		/* # of dirty pages */
76939a53e0cSJaegeuk Kim 	f2fs_hash_t chash;		/* hash value of given file name */
77039a53e0cSJaegeuk Kim 	unsigned int clevel;		/* maximum level of given file name */
77188c5c13aSJaegeuk Kim 	struct task_struct *task;	/* lookup and create consistency */
772b0af6d49SChao Yu 	struct task_struct *cp_task;	/* separate cp/wb IO stats*/
77339a53e0cSJaegeuk Kim 	nid_t i_xattr_nid;		/* node id that contains xattrs */
77426de9b11SJaegeuk Kim 	loff_t	last_disk_size;		/* lastly written file size */
775c10c9820SChao Yu 	spinlock_t i_size_lock;		/* protect last_disk_size */
77688b88a66SJaegeuk Kim 
7770abd675eSChao Yu #ifdef CONFIG_QUOTA
7780abd675eSChao Yu 	struct dquot *i_dquot[MAXQUOTAS];
7790abd675eSChao Yu 
7800abd675eSChao Yu 	/* quota space reservation, managed internally by quota code */
7810abd675eSChao Yu 	qsize_t i_reserved_quota;
7820abd675eSChao Yu #endif
7830f18b462SJaegeuk Kim 	struct list_head dirty_list;	/* dirty list for dirs and files */
7840f18b462SJaegeuk Kim 	struct list_head gdirty_list;	/* linked in global dirty list */
78557864ae5SJaegeuk Kim 	struct list_head inmem_ilist;	/* list for inmem inodes */
78688b88a66SJaegeuk Kim 	struct list_head inmem_pages;	/* inmemory pages managed by f2fs */
7877a10f017SJaegeuk Kim 	struct task_struct *inmem_task;	/* store inmemory task */
78888b88a66SJaegeuk Kim 	struct mutex inmem_lock;	/* lock for inmemory pages */
7896b12367dSJaegeuk Kim 	pgoff_t ra_offset;		/* ongoing readahead offset */
7903e72f721SJaegeuk Kim 	struct extent_tree *extent_tree;	/* cached extent_tree entry */
791b2532c69SChao Yu 
792b2532c69SChao Yu 	/* avoid racing between foreground op and gc */
793b2532c69SChao Yu 	struct rw_semaphore i_gc_rwsem[2];
7945a3a2d83SQiuyang Sun 	struct rw_semaphore i_mmap_sem;
79527161f13SYunlei He 	struct rw_semaphore i_xattr_sem; /* avoid racing between reading and changing EAs */
796f2470371SChao Yu 
7977a2af766SChao Yu 	int i_extra_isize;		/* size of extra space located in i_addr */
7985c57132eSChao Yu 	kprojid_t i_projid;		/* id for project quota */
7996afc662eSChao Yu 	int i_inline_xattr_size;	/* inline xattr size */
80024b81dfcSArnd Bergmann 	struct timespec64 i_crtime;	/* inode creation time */
80124b81dfcSArnd Bergmann 	struct timespec64 i_disk_time[4];/* inode disk times */
8024c8ff709SChao Yu 
8034c8ff709SChao Yu 	/* for file compress */
8044c8ff709SChao Yu 	u64 i_compr_blocks;			/* # of compressed blocks */
8054c8ff709SChao Yu 	unsigned char i_compress_algorithm;	/* algorithm type */
8064c8ff709SChao Yu 	unsigned char i_log_cluster_size;	/* log of cluster size */
8074c8ff709SChao Yu 	unsigned int i_cluster_size;		/* cluster size */
80839a53e0cSJaegeuk Kim };
80939a53e0cSJaegeuk Kim 
81039a53e0cSJaegeuk Kim static inline void get_extent_info(struct extent_info *ext,
811bd933d4fSChao Yu 					struct f2fs_extent *i_ext)
81239a53e0cSJaegeuk Kim {
813bd933d4fSChao Yu 	ext->fofs = le32_to_cpu(i_ext->fofs);
814bd933d4fSChao Yu 	ext->blk = le32_to_cpu(i_ext->blk);
815bd933d4fSChao Yu 	ext->len = le32_to_cpu(i_ext->len);
81639a53e0cSJaegeuk Kim }
81739a53e0cSJaegeuk Kim 
81839a53e0cSJaegeuk Kim static inline void set_raw_extent(struct extent_info *ext,
81939a53e0cSJaegeuk Kim 					struct f2fs_extent *i_ext)
82039a53e0cSJaegeuk Kim {
82139a53e0cSJaegeuk Kim 	i_ext->fofs = cpu_to_le32(ext->fofs);
8224d0b0bd4SChao Yu 	i_ext->blk = cpu_to_le32(ext->blk);
82339a53e0cSJaegeuk Kim 	i_ext->len = cpu_to_le32(ext->len);
82439a53e0cSJaegeuk Kim }
82539a53e0cSJaegeuk Kim 
826429511cdSChao Yu static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
827429511cdSChao Yu 						u32 blk, unsigned int len)
828429511cdSChao Yu {
829429511cdSChao Yu 	ei->fofs = fofs;
830429511cdSChao Yu 	ei->blk = blk;
831429511cdSChao Yu 	ei->len = len;
832429511cdSChao Yu }
833429511cdSChao Yu 
834004b6862SChao Yu static inline bool __is_discard_mergeable(struct discard_info *back,
83535ec7d57SChao Yu 			struct discard_info *front, unsigned int max_len)
836004b6862SChao Yu {
8379a997188SJaegeuk Kim 	return (back->lstart + back->len == front->lstart) &&
83835ec7d57SChao Yu 		(back->len + front->len <= max_len);
839004b6862SChao Yu }
840004b6862SChao Yu 
841004b6862SChao Yu static inline bool __is_discard_back_mergeable(struct discard_info *cur,
84235ec7d57SChao Yu 			struct discard_info *back, unsigned int max_len)
843004b6862SChao Yu {
84435ec7d57SChao Yu 	return __is_discard_mergeable(back, cur, max_len);
845004b6862SChao Yu }
846004b6862SChao Yu 
847004b6862SChao Yu static inline bool __is_discard_front_mergeable(struct discard_info *cur,
84835ec7d57SChao Yu 			struct discard_info *front, unsigned int max_len)
849004b6862SChao Yu {
85035ec7d57SChao Yu 	return __is_discard_mergeable(cur, front, max_len);
851004b6862SChao Yu }
852004b6862SChao Yu 
853429511cdSChao Yu static inline bool __is_extent_mergeable(struct extent_info *back,
854429511cdSChao Yu 						struct extent_info *front)
855429511cdSChao Yu {
856429511cdSChao Yu 	return (back->fofs + back->len == front->fofs &&
857429511cdSChao Yu 			back->blk + back->len == front->blk);
858429511cdSChao Yu }
859429511cdSChao Yu 
860429511cdSChao Yu static inline bool __is_back_mergeable(struct extent_info *cur,
861429511cdSChao Yu 						struct extent_info *back)
862429511cdSChao Yu {
863429511cdSChao Yu 	return __is_extent_mergeable(back, cur);
864429511cdSChao Yu }
865429511cdSChao Yu 
866429511cdSChao Yu static inline bool __is_front_mergeable(struct extent_info *cur,
867429511cdSChao Yu 						struct extent_info *front)
868429511cdSChao Yu {
869429511cdSChao Yu 	return __is_extent_mergeable(cur, front);
870429511cdSChao Yu }
871429511cdSChao Yu 
872cac5a3d8SDongOh Shin extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
873b430f726SZhikang Zhang static inline void __try_update_largest_extent(struct extent_tree *et,
874b430f726SZhikang Zhang 						struct extent_node *en)
8754abd3f5aSChao Yu {
876205b9822SJaegeuk Kim 	if (en->ei.len > et->largest.len) {
8774abd3f5aSChao Yu 		et->largest = en->ei;
878b430f726SZhikang Zhang 		et->largest_updated = true;
879205b9822SJaegeuk Kim 	}
8804abd3f5aSChao Yu }
8814abd3f5aSChao Yu 
8829a4ffdf5SChao Yu /*
8839a4ffdf5SChao Yu  * For free nid management
8849a4ffdf5SChao Yu  */
8859a4ffdf5SChao Yu enum nid_state {
8869a4ffdf5SChao Yu 	FREE_NID,		/* newly added to free nid list */
8879a4ffdf5SChao Yu 	PREALLOC_NID,		/* it is preallocated */
8889a4ffdf5SChao Yu 	MAX_NID_STATE,
889b8559dc2SChao Yu };
890b8559dc2SChao Yu 
89139a53e0cSJaegeuk Kim struct f2fs_nm_info {
89239a53e0cSJaegeuk Kim 	block_t nat_blkaddr;		/* base disk address of NAT */
89339a53e0cSJaegeuk Kim 	nid_t max_nid;			/* maximum possible node ids */
89404d47e67SChao Yu 	nid_t available_nids;		/* # of available node ids */
89539a53e0cSJaegeuk Kim 	nid_t next_scan_nid;		/* the next nid to be scanned */
896cdfc41c1SJaegeuk Kim 	unsigned int ram_thresh;	/* control the memory footprint */
897ea1a29a0SChao Yu 	unsigned int ra_nid_pages;	/* # of nid pages to be readaheaded */
8982304cb0cSChao Yu 	unsigned int dirty_nats_ratio;	/* control dirty nats ratio threshold */
89939a53e0cSJaegeuk Kim 
90039a53e0cSJaegeuk Kim 	/* NAT cache management */
90139a53e0cSJaegeuk Kim 	struct radix_tree_root nat_root;/* root of the nat entry cache */
902309cc2b6SJaegeuk Kim 	struct radix_tree_root nat_set_root;/* root of the nat set cache */
903b873b798SJaegeuk Kim 	struct rw_semaphore nat_tree_lock;	/* protect nat_tree_lock */
90439a53e0cSJaegeuk Kim 	struct list_head nat_entries;	/* cached nat entry list (clean) */
90522969158SChao Yu 	spinlock_t nat_list_lock;	/* protect clean nat entry list */
906309cc2b6SJaegeuk Kim 	unsigned int nat_cnt;		/* the # of cached nat entries */
907aec71382SChao Yu 	unsigned int dirty_nat_cnt;	/* total num of nat entries in set */
90822ad0b6aSJaegeuk Kim 	unsigned int nat_blocks;	/* # of nat blocks */
90939a53e0cSJaegeuk Kim 
91039a53e0cSJaegeuk Kim 	/* free node ids management */
9118a7ed66aSJaegeuk Kim 	struct radix_tree_root free_nid_root;/* root of the free_nid cache */
9129a4ffdf5SChao Yu 	struct list_head free_nid_list;		/* list for free nids excluding preallocated nids */
9139a4ffdf5SChao Yu 	unsigned int nid_cnt[MAX_NID_STATE];	/* the number of free node id */
914b8559dc2SChao Yu 	spinlock_t nid_list_lock;	/* protect nid lists ops */
91539a53e0cSJaegeuk Kim 	struct mutex build_lock;	/* lock for build free nids */
916bb1105e4SJaegeuk Kim 	unsigned char **free_nid_bitmap;
9174ac91242SChao Yu 	unsigned char *nat_block_bitmap;
918586d1492SChao Yu 	unsigned short *free_nid_count;	/* free nid count of NAT block */
91939a53e0cSJaegeuk Kim 
92039a53e0cSJaegeuk Kim 	/* for checkpoint */
92139a53e0cSJaegeuk Kim 	char *nat_bitmap;		/* NAT bitmap pointer */
92222ad0b6aSJaegeuk Kim 
92322ad0b6aSJaegeuk Kim 	unsigned int nat_bits_blocks;	/* # of nat bits blocks */
92422ad0b6aSJaegeuk Kim 	unsigned char *nat_bits;	/* NAT bits blocks */
92522ad0b6aSJaegeuk Kim 	unsigned char *full_nat_bits;	/* full NAT pages */
92622ad0b6aSJaegeuk Kim 	unsigned char *empty_nat_bits;	/* empty NAT pages */
927599a09b2SChao Yu #ifdef CONFIG_F2FS_CHECK_FS
928599a09b2SChao Yu 	char *nat_bitmap_mir;		/* NAT bitmap mirror */
929599a09b2SChao Yu #endif
93039a53e0cSJaegeuk Kim 	int bitmap_size;		/* bitmap size */
93139a53e0cSJaegeuk Kim };
93239a53e0cSJaegeuk Kim 
93339a53e0cSJaegeuk Kim /*
93439a53e0cSJaegeuk Kim  * this structure is used as one of function parameters.
93539a53e0cSJaegeuk Kim  * all the information are dedicated to a given direct node block determined
93639a53e0cSJaegeuk Kim  * by the data offset in a file.
93739a53e0cSJaegeuk Kim  */
93839a53e0cSJaegeuk Kim struct dnode_of_data {
93939a53e0cSJaegeuk Kim 	struct inode *inode;		/* vfs inode pointer */
94039a53e0cSJaegeuk Kim 	struct page *inode_page;	/* its inode page, NULL is possible */
94139a53e0cSJaegeuk Kim 	struct page *node_page;		/* cached direct node page */
94239a53e0cSJaegeuk Kim 	nid_t nid;			/* node id of the direct node block */
94339a53e0cSJaegeuk Kim 	unsigned int ofs_in_node;	/* data offset in the node page */
94439a53e0cSJaegeuk Kim 	bool inode_page_locked;		/* inode page is locked or not */
94593bae099SJaegeuk Kim 	bool node_changed;		/* is node block changed */
9463cf45747SChao Yu 	char cur_level;			/* level of hole node page */
9473cf45747SChao Yu 	char max_level;			/* level of current page located */
94839a53e0cSJaegeuk Kim 	block_t	data_blkaddr;		/* block address of the node block */
94939a53e0cSJaegeuk Kim };
95039a53e0cSJaegeuk Kim 
95139a53e0cSJaegeuk Kim static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
95239a53e0cSJaegeuk Kim 		struct page *ipage, struct page *npage, nid_t nid)
95339a53e0cSJaegeuk Kim {
954d66d1f76SJaegeuk Kim 	memset(dn, 0, sizeof(*dn));
95539a53e0cSJaegeuk Kim 	dn->inode = inode;
95639a53e0cSJaegeuk Kim 	dn->inode_page = ipage;
95739a53e0cSJaegeuk Kim 	dn->node_page = npage;
95839a53e0cSJaegeuk Kim 	dn->nid = nid;
95939a53e0cSJaegeuk Kim }
96039a53e0cSJaegeuk Kim 
96139a53e0cSJaegeuk Kim /*
96239a53e0cSJaegeuk Kim  * For SIT manager
96339a53e0cSJaegeuk Kim  *
96439a53e0cSJaegeuk Kim  * By default, there are 6 active log areas across the whole main area.
96539a53e0cSJaegeuk Kim  * When considering hot and cold data separation to reduce cleaning overhead,
96639a53e0cSJaegeuk Kim  * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
96739a53e0cSJaegeuk Kim  * respectively.
96839a53e0cSJaegeuk Kim  * In the current design, you should not change the numbers intentionally.
96939a53e0cSJaegeuk Kim  * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
97039a53e0cSJaegeuk Kim  * logs individually according to the underlying devices. (default: 6)
97139a53e0cSJaegeuk Kim  * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
97239a53e0cSJaegeuk Kim  * data and 8 for node logs.
97339a53e0cSJaegeuk Kim  */
97439a53e0cSJaegeuk Kim #define	NR_CURSEG_DATA_TYPE	(3)
97539a53e0cSJaegeuk Kim #define NR_CURSEG_NODE_TYPE	(3)
976d0b9e42aSChao Yu #define NR_CURSEG_INMEM_TYPE	(1)
977d0b9e42aSChao Yu #define NR_CURSEG_PERSIST_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
978d0b9e42aSChao Yu #define NR_CURSEG_TYPE		(NR_CURSEG_INMEM_TYPE + NR_CURSEG_PERSIST_TYPE)
97939a53e0cSJaegeuk Kim 
98039a53e0cSJaegeuk Kim enum {
98139a53e0cSJaegeuk Kim 	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
98239a53e0cSJaegeuk Kim 	CURSEG_WARM_DATA,	/* data blocks */
98339a53e0cSJaegeuk Kim 	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
98439a53e0cSJaegeuk Kim 	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
98539a53e0cSJaegeuk Kim 	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
98639a53e0cSJaegeuk Kim 	CURSEG_COLD_NODE,	/* indirect node blocks */
987d0b9e42aSChao Yu 	NR_PERSISTENT_LOG,	/* number of persistent log */
988d0b9e42aSChao Yu 	CURSEG_COLD_DATA_PINNED = NR_PERSISTENT_LOG,
989d0b9e42aSChao Yu 				/* pinned file that needs consecutive block address */
990d0b9e42aSChao Yu 	NO_CHECK_TYPE,		/* number of persistent & inmem log */
99139a53e0cSJaegeuk Kim };
99239a53e0cSJaegeuk Kim 
9936b4afdd7SJaegeuk Kim struct flush_cmd {
9946b4afdd7SJaegeuk Kim 	struct completion wait;
995721bd4d5SGu Zheng 	struct llist_node llnode;
99639d787beSChao Yu 	nid_t ino;
9976b4afdd7SJaegeuk Kim 	int ret;
9986b4afdd7SJaegeuk Kim };
9996b4afdd7SJaegeuk Kim 
1000a688b9d9SGu Zheng struct flush_cmd_control {
1001a688b9d9SGu Zheng 	struct task_struct *f2fs_issue_flush;	/* flush thread */
1002a688b9d9SGu Zheng 	wait_queue_head_t flush_wait_queue;	/* waiting queue for wake-up */
10038b8dd65fSChao Yu 	atomic_t issued_flush;			/* # of issued flushes */
100472691af6SJaegeuk Kim 	atomic_t queued_flush;			/* # of queued flushes */
1005721bd4d5SGu Zheng 	struct llist_head issue_list;		/* list for command issue */
1006721bd4d5SGu Zheng 	struct llist_node *dispatch_list;	/* list for command dispatch */
1007a688b9d9SGu Zheng };
1008a688b9d9SGu Zheng 
100939a53e0cSJaegeuk Kim struct f2fs_sm_info {
101039a53e0cSJaegeuk Kim 	struct sit_info *sit_info;		/* whole segment information */
101139a53e0cSJaegeuk Kim 	struct free_segmap_info *free_info;	/* free segment information */
101239a53e0cSJaegeuk Kim 	struct dirty_seglist_info *dirty_info;	/* dirty segment information */
101339a53e0cSJaegeuk Kim 	struct curseg_info *curseg_array;	/* active segment information */
101439a53e0cSJaegeuk Kim 
10152b60311dSChao Yu 	struct rw_semaphore curseg_lock;	/* for preventing curseg change */
10162b60311dSChao Yu 
101739a53e0cSJaegeuk Kim 	block_t seg0_blkaddr;		/* block address of 0'th segment */
101839a53e0cSJaegeuk Kim 	block_t main_blkaddr;		/* start block address of main area */
101939a53e0cSJaegeuk Kim 	block_t ssa_blkaddr;		/* start block address of SSA area */
102039a53e0cSJaegeuk Kim 
102139a53e0cSJaegeuk Kim 	unsigned int segment_count;	/* total # of segments */
102239a53e0cSJaegeuk Kim 	unsigned int main_segments;	/* # of segments in main area */
102339a53e0cSJaegeuk Kim 	unsigned int reserved_segments;	/* # of reserved segments */
102439a53e0cSJaegeuk Kim 	unsigned int ovp_segments;	/* # of overprovision segments */
102581eb8d6eSJaegeuk Kim 
102681eb8d6eSJaegeuk Kim 	/* a threshold to reclaim prefree segments */
102781eb8d6eSJaegeuk Kim 	unsigned int rec_prefree_segments;
10287fd9e544SJaegeuk Kim 
1029bba681cbSJaegeuk Kim 	/* for batched trimming */
1030bba681cbSJaegeuk Kim 	unsigned int trim_sections;		/* # of sections to trim */
1031bba681cbSJaegeuk Kim 
1032184a5cd2SChao Yu 	struct list_head sit_entry_set;	/* sit entry set list */
1033184a5cd2SChao Yu 
1034216fbd64SJaegeuk Kim 	unsigned int ipu_policy;	/* in-place-update policy */
1035216fbd64SJaegeuk Kim 	unsigned int min_ipu_util;	/* in-place-update threshold */
1036c1ce1b02SJaegeuk Kim 	unsigned int min_fsync_blocks;	/* threshold for fsync */
1037853137ceSJaegeuk Kim 	unsigned int min_seq_blocks;	/* threshold for sequential blocks */
1038ef095d19SJaegeuk Kim 	unsigned int min_hot_blocks;	/* threshold for hot block allocation */
1039a2a12b67SChao Yu 	unsigned int min_ssr_sections;	/* threshold to trigger SSR allocation */
10406b4afdd7SJaegeuk Kim 
10416b4afdd7SJaegeuk Kim 	/* for flush command control */
1042b01a9201SJaegeuk Kim 	struct flush_cmd_control *fcc_info;
1043a688b9d9SGu Zheng 
10440b54fb84SJaegeuk Kim 	/* for discard command control */
10450b54fb84SJaegeuk Kim 	struct discard_cmd_control *dcc_info;
104639a53e0cSJaegeuk Kim };
104739a53e0cSJaegeuk Kim 
104839a53e0cSJaegeuk Kim /*
104939a53e0cSJaegeuk Kim  * For superblock
105039a53e0cSJaegeuk Kim  */
105139a53e0cSJaegeuk Kim /*
105239a53e0cSJaegeuk Kim  * COUNT_TYPE for monitoring
105339a53e0cSJaegeuk Kim  *
105439a53e0cSJaegeuk Kim  * f2fs monitors the number of several block types such as on-writeback,
105539a53e0cSJaegeuk Kim  * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
105639a53e0cSJaegeuk Kim  */
105736951b38SChao Yu #define WB_DATA_TYPE(p)	(__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
105839a53e0cSJaegeuk Kim enum count_type {
105939a53e0cSJaegeuk Kim 	F2FS_DIRTY_DENTS,
1060c227f912SChao Yu 	F2FS_DIRTY_DATA,
10612c8a4a28SJaegeuk Kim 	F2FS_DIRTY_QDATA,
106239a53e0cSJaegeuk Kim 	F2FS_DIRTY_NODES,
106339a53e0cSJaegeuk Kim 	F2FS_DIRTY_META,
10648dcf2ff7SJaegeuk Kim 	F2FS_INMEM_PAGES,
10650f18b462SJaegeuk Kim 	F2FS_DIRTY_IMETA,
106636951b38SChao Yu 	F2FS_WB_CP_DATA,
106736951b38SChao Yu 	F2FS_WB_DATA,
10685f9abab4SJaegeuk Kim 	F2FS_RD_DATA,
10695f9abab4SJaegeuk Kim 	F2FS_RD_NODE,
10705f9abab4SJaegeuk Kim 	F2FS_RD_META,
107102b16d0aSChao Yu 	F2FS_DIO_WRITE,
107202b16d0aSChao Yu 	F2FS_DIO_READ,
107339a53e0cSJaegeuk Kim 	NR_COUNT_TYPE,
107439a53e0cSJaegeuk Kim };
107539a53e0cSJaegeuk Kim 
107639a53e0cSJaegeuk Kim /*
1077e1c42045Sarter97  * The below are the page types of bios used in submit_bio().
107839a53e0cSJaegeuk Kim  * The available types are:
107939a53e0cSJaegeuk Kim  * DATA			User data pages. It operates as async mode.
108039a53e0cSJaegeuk Kim  * NODE			Node pages. It operates as async mode.
108139a53e0cSJaegeuk Kim  * META			FS metadata pages such as SIT, NAT, CP.
108239a53e0cSJaegeuk Kim  * NR_PAGE_TYPE		The number of page types.
108339a53e0cSJaegeuk Kim  * META_FLUSH		Make sure the previous pages are written
108439a53e0cSJaegeuk Kim  *			with waiting the bio's completion
108539a53e0cSJaegeuk Kim  * ...			Only can be used with META.
108639a53e0cSJaegeuk Kim  */
10877d5e5109SJaegeuk Kim #define PAGE_TYPE_OF_BIO(type)	((type) > META ? META : (type))
108839a53e0cSJaegeuk Kim enum page_type {
108939a53e0cSJaegeuk Kim 	DATA,
109039a53e0cSJaegeuk Kim 	NODE,
109139a53e0cSJaegeuk Kim 	META,
109239a53e0cSJaegeuk Kim 	NR_PAGE_TYPE,
109339a53e0cSJaegeuk Kim 	META_FLUSH,
10948ce67cb0SJaegeuk Kim 	INMEM,		/* the below types are used by tracepoints only. */
10958ce67cb0SJaegeuk Kim 	INMEM_DROP,
10968c242db9SJaegeuk Kim 	INMEM_INVALIDATE,
109728bc106bSChao Yu 	INMEM_REVOKE,
10988ce67cb0SJaegeuk Kim 	IPU,
10998ce67cb0SJaegeuk Kim 	OPU,
110039a53e0cSJaegeuk Kim };
110139a53e0cSJaegeuk Kim 
1102a912b54dSJaegeuk Kim enum temp_type {
1103a912b54dSJaegeuk Kim 	HOT = 0,	/* must be zero for meta bio */
1104a912b54dSJaegeuk Kim 	WARM,
1105a912b54dSJaegeuk Kim 	COLD,
1106a912b54dSJaegeuk Kim 	NR_TEMP_TYPE,
1107a912b54dSJaegeuk Kim };
1108a912b54dSJaegeuk Kim 
1109cc15620bSJaegeuk Kim enum need_lock_type {
1110cc15620bSJaegeuk Kim 	LOCK_REQ = 0,
1111cc15620bSJaegeuk Kim 	LOCK_DONE,
1112cc15620bSJaegeuk Kim 	LOCK_RETRY,
1113cc15620bSJaegeuk Kim };
1114cc15620bSJaegeuk Kim 
1115a5fd5050SChao Yu enum cp_reason_type {
1116a5fd5050SChao Yu 	CP_NO_NEEDED,
1117a5fd5050SChao Yu 	CP_NON_REGULAR,
11184c8ff709SChao Yu 	CP_COMPRESSED,
1119a5fd5050SChao Yu 	CP_HARDLINK,
1120a5fd5050SChao Yu 	CP_SB_NEED_CP,
1121a5fd5050SChao Yu 	CP_WRONG_PINO,
1122a5fd5050SChao Yu 	CP_NO_SPC_ROLL,
1123a5fd5050SChao Yu 	CP_NODE_NEED_CP,
1124a5fd5050SChao Yu 	CP_FASTBOOT_MODE,
1125a5fd5050SChao Yu 	CP_SPEC_LOG_NUM,
11260a007b97SJaegeuk Kim 	CP_RECOVER_DIR,
1127a5fd5050SChao Yu };
1128a5fd5050SChao Yu 
1129b0af6d49SChao Yu enum iostat_type {
11308b83ac81SChao Yu 	/* WRITE IO */
11318b83ac81SChao Yu 	APP_DIRECT_IO,			/* app direct write IOs */
11328b83ac81SChao Yu 	APP_BUFFERED_IO,		/* app buffered write IOs */
1133b0af6d49SChao Yu 	APP_WRITE_IO,			/* app write IOs */
1134b0af6d49SChao Yu 	APP_MAPPED_IO,			/* app mapped IOs */
1135b0af6d49SChao Yu 	FS_DATA_IO,			/* data IOs from kworker/fsync/reclaimer */
1136b0af6d49SChao Yu 	FS_NODE_IO,			/* node IOs from kworker/fsync/reclaimer */
1137b0af6d49SChao Yu 	FS_META_IO,			/* meta IOs from kworker/reclaimer */
1138b0af6d49SChao Yu 	FS_GC_DATA_IO,			/* data IOs from forground gc */
1139b0af6d49SChao Yu 	FS_GC_NODE_IO,			/* node IOs from forground gc */
1140b0af6d49SChao Yu 	FS_CP_DATA_IO,			/* data IOs from checkpoint */
1141b0af6d49SChao Yu 	FS_CP_NODE_IO,			/* node IOs from checkpoint */
1142b0af6d49SChao Yu 	FS_CP_META_IO,			/* meta IOs from checkpoint */
11438b83ac81SChao Yu 
11448b83ac81SChao Yu 	/* READ IO */
11458b83ac81SChao Yu 	APP_DIRECT_READ_IO,		/* app direct read IOs */
11468b83ac81SChao Yu 	APP_BUFFERED_READ_IO,		/* app buffered read IOs */
11478b83ac81SChao Yu 	APP_READ_IO,			/* app read IOs */
11488b83ac81SChao Yu 	APP_MAPPED_READ_IO,		/* app mapped read IOs */
11498b83ac81SChao Yu 	FS_DATA_READ_IO,		/* data read IOs */
11509c122384SChao Yu 	FS_GDATA_READ_IO,		/* data read IOs from background gc */
11519c122384SChao Yu 	FS_CDATA_READ_IO,		/* compressed data read IOs */
11528b83ac81SChao Yu 	FS_NODE_READ_IO,		/* node read IOs */
11538b83ac81SChao Yu 	FS_META_READ_IO,		/* meta read IOs */
11548b83ac81SChao Yu 
11558b83ac81SChao Yu 	/* other */
1156b0af6d49SChao Yu 	FS_DISCARD,			/* discard */
1157b0af6d49SChao Yu 	NR_IO_TYPE,
1158b0af6d49SChao Yu };
1159b0af6d49SChao Yu 
1160458e6197SJaegeuk Kim struct f2fs_io_info {
116105ca3632SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs_sb_info pointer */
116239d787beSChao Yu 	nid_t ino;		/* inode number */
1163458e6197SJaegeuk Kim 	enum page_type type;	/* contains DATA/NODE/META/META_FLUSH */
1164a912b54dSJaegeuk Kim 	enum temp_type temp;	/* contains HOT/WARM/COLD */
116504d328deSMike Christie 	int op;			/* contains REQ_OP_ */
1166ef295ecfSChristoph Hellwig 	int op_flags;		/* req_flag_bits */
11677a9d7548SChao Yu 	block_t new_blkaddr;	/* new block address to be written */
116828bc106bSChao Yu 	block_t old_blkaddr;	/* old block address before Cow */
116905ca3632SJaegeuk Kim 	struct page *page;	/* page to be written */
11704375a336SJaegeuk Kim 	struct page *encrypted_page;	/* encrypted page */
11714c8ff709SChao Yu 	struct page *compressed_page;	/* compressed page */
1172fb830fc5SChao Yu 	struct list_head list;		/* serialize IOs */
1173d68f735bSJaegeuk Kim 	bool submitted;		/* indicate IO submission */
1174cc15620bSJaegeuk Kim 	int need_lock;		/* indicate we need to lock cp_rwsem */
1175fb830fc5SChao Yu 	bool in_list;		/* indicate fio is in io_list */
11766dc3a126SChao Yu 	bool is_por;		/* indicate IO is from recovery or not */
1177fe16efe6SChao Yu 	bool retry;		/* need to reallocate block address */
11784c8ff709SChao Yu 	int compr_blocks;	/* # of compressed block addresses */
11794c8ff709SChao Yu 	bool encrypted;		/* indicate file is encrypted */
1180b0af6d49SChao Yu 	enum iostat_type io_type;	/* io type */
1181578c6478SYufen Yu 	struct writeback_control *io_wbc; /* writeback control */
11828648de2cSChao Yu 	struct bio **bio;		/* bio for ipu */
11838648de2cSChao Yu 	sector_t *last_block;		/* last block number in bio */
11847735730dSChao Yu 	unsigned char version;		/* version of the node */
1185458e6197SJaegeuk Kim };
1186458e6197SJaegeuk Kim 
11870b20fcecSChao Yu struct bio_entry {
11880b20fcecSChao Yu 	struct bio *bio;
11890b20fcecSChao Yu 	struct list_head list;
11900b20fcecSChao Yu };
11910b20fcecSChao Yu 
119268afcf2dSTomohiro Kusumi #define is_read_io(rw) ((rw) == READ)
11931ff7bd3bSJaegeuk Kim struct f2fs_bio_info {
1194458e6197SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs superblock */
11951ff7bd3bSJaegeuk Kim 	struct bio *bio;		/* bios to merge */
11961ff7bd3bSJaegeuk Kim 	sector_t last_block_in_bio;	/* last block number */
1197458e6197SJaegeuk Kim 	struct f2fs_io_info fio;	/* store buffered io info. */
1198df0f8dc0SChao Yu 	struct rw_semaphore io_rwsem;	/* blocking op for bio */
1199fb830fc5SChao Yu 	spinlock_t io_lock;		/* serialize DATA/NODE IOs */
1200fb830fc5SChao Yu 	struct list_head io_list;	/* track fios */
12010b20fcecSChao Yu 	struct list_head bio_list;	/* bio entry list head */
12020b20fcecSChao Yu 	struct rw_semaphore bio_list_lock;	/* lock to protect bio entry list */
12031ff7bd3bSJaegeuk Kim };
12041ff7bd3bSJaegeuk Kim 
12053c62be17SJaegeuk Kim #define FDEV(i)				(sbi->devs[i])
12063c62be17SJaegeuk Kim #define RDEV(i)				(raw_super->devs[i])
12073c62be17SJaegeuk Kim struct f2fs_dev_info {
12083c62be17SJaegeuk Kim 	struct block_device *bdev;
12093c62be17SJaegeuk Kim 	char path[MAX_PATH_LEN];
12103c62be17SJaegeuk Kim 	unsigned int total_segments;
12113c62be17SJaegeuk Kim 	block_t start_blk;
12123c62be17SJaegeuk Kim 	block_t end_blk;
12133c62be17SJaegeuk Kim #ifdef CONFIG_BLK_DEV_ZONED
12143c62be17SJaegeuk Kim 	unsigned int nr_blkz;		/* Total number of zones */
121595175dafSDamien Le Moal 	unsigned long *blkz_seq;	/* Bitmap indicating sequential zones */
1216de881df9SAravind Ramesh 	block_t *zone_capacity_blocks;  /* Array of zone capacity in blks */
12173c62be17SJaegeuk Kim #endif
12183c62be17SJaegeuk Kim };
12193c62be17SJaegeuk Kim 
1220c227f912SChao Yu enum inode_type {
1221c227f912SChao Yu 	DIR_INODE,			/* for dirty dir inode */
1222c227f912SChao Yu 	FILE_INODE,			/* for dirty regular/symlink inode */
12230f18b462SJaegeuk Kim 	DIRTY_META,			/* for all dirtied inode metadata */
122457864ae5SJaegeuk Kim 	ATOMIC_FILE,			/* for all atomic files */
1225c227f912SChao Yu 	NR_INODE_TYPE,
1226c227f912SChao Yu };
1227c227f912SChao Yu 
122867298804SChao Yu /* for inner inode cache management */
122967298804SChao Yu struct inode_management {
123067298804SChao Yu 	struct radix_tree_root ino_root;	/* ino entry array */
123167298804SChao Yu 	spinlock_t ino_lock;			/* for ino entry lock */
123267298804SChao Yu 	struct list_head ino_list;		/* inode list head */
123367298804SChao Yu 	unsigned long ino_num;			/* number of entries */
123467298804SChao Yu };
123567298804SChao Yu 
1236caf0047eSChao Yu /* For s_flag in struct f2fs_sb_info */
1237caf0047eSChao Yu enum {
1238caf0047eSChao Yu 	SBI_IS_DIRTY,				/* dirty flag for checkpoint */
1239caf0047eSChao Yu 	SBI_IS_CLOSE,				/* specify unmounting */
1240caf0047eSChao Yu 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
1241caf0047eSChao Yu 	SBI_POR_DOING,				/* recovery is doing or not */
1242df728b0fSJaegeuk Kim 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
1243bbf156f7SJaegeuk Kim 	SBI_NEED_CP,				/* need to checkpoint */
124483a3bfdbSJaegeuk Kim 	SBI_IS_SHUTDOWN,			/* shutdown by ioctl */
12451378752bSChao Yu 	SBI_IS_RECOVERED,			/* recovered orphan/data */
12464354994fSDaniel Rosenberg 	SBI_CP_DISABLED,			/* CP was disabled last mount */
1247db610a64SJaegeuk Kim 	SBI_CP_DISABLED_QUICK,			/* CP was disabled quickly */
1248af033b2aSChao Yu 	SBI_QUOTA_NEED_FLUSH,			/* need to flush quota info in CP */
1249af033b2aSChao Yu 	SBI_QUOTA_SKIP_FLUSH,			/* skip flushing quota in current CP */
1250af033b2aSChao Yu 	SBI_QUOTA_NEED_REPAIR,			/* quota file may be corrupted */
125104f0b2eaSQiuyang Sun 	SBI_IS_RESIZEFS,			/* resizefs is in process */
1252caf0047eSChao Yu };
1253caf0047eSChao Yu 
12546beceb54SJaegeuk Kim enum {
12556beceb54SJaegeuk Kim 	CP_TIME,
1256d0239e1bSJaegeuk Kim 	REQ_TIME,
1257a7d10cf3SSahitya Tummala 	DISCARD_TIME,
1258a7d10cf3SSahitya Tummala 	GC_TIME,
12594354994fSDaniel Rosenberg 	DISABLE_TIME,
126003f2c02dSJaegeuk Kim 	UMOUNT_DISCARD_TIMEOUT,
12616beceb54SJaegeuk Kim 	MAX_TIME,
12626beceb54SJaegeuk Kim };
12636beceb54SJaegeuk Kim 
12640cdd3195SHyunchul Lee enum {
12655b0e9539SJaegeuk Kim 	GC_NORMAL,
12665b0e9539SJaegeuk Kim 	GC_IDLE_CB,
12675b0e9539SJaegeuk Kim 	GC_IDLE_GREEDY,
12680e5e8111SDaeho Jeong 	GC_URGENT_HIGH,
12690e5e8111SDaeho Jeong 	GC_URGENT_LOW,
12705b0e9539SJaegeuk Kim };
12715b0e9539SJaegeuk Kim 
12725b0e9539SJaegeuk Kim enum {
1273bbbc34fdSChao Yu 	BGGC_MODE_ON,		/* background gc is on */
1274bbbc34fdSChao Yu 	BGGC_MODE_OFF,		/* background gc is off */
1275bbbc34fdSChao Yu 	BGGC_MODE_SYNC,		/*
1276bbbc34fdSChao Yu 				 * background gc is on, migrating blocks
1277bbbc34fdSChao Yu 				 * like foreground gc
1278bbbc34fdSChao Yu 				 */
1279bbbc34fdSChao Yu };
1280bbbc34fdSChao Yu 
1281bbbc34fdSChao Yu enum {
1282b0332a0fSChao Yu 	FS_MODE_ADAPTIVE,	/* use both lfs/ssr allocation */
1283b0332a0fSChao Yu 	FS_MODE_LFS,		/* use lfs allocation only */
1284b0332a0fSChao Yu };
1285b0332a0fSChao Yu 
1286b0332a0fSChao Yu enum {
12870cdd3195SHyunchul Lee 	WHINT_MODE_OFF,		/* not pass down write hints */
12880cdd3195SHyunchul Lee 	WHINT_MODE_USER,	/* try to pass down hints given by users */
1289f2e703f9SHyunchul Lee 	WHINT_MODE_FS,		/* pass down hints with F2FS policy */
12900cdd3195SHyunchul Lee };
12910cdd3195SHyunchul Lee 
129207939627SJaegeuk Kim enum {
129307939627SJaegeuk Kim 	ALLOC_MODE_DEFAULT,	/* stay default */
129407939627SJaegeuk Kim 	ALLOC_MODE_REUSE,	/* reuse segments as much as possible */
129507939627SJaegeuk Kim };
129607939627SJaegeuk Kim 
129793cf93f1SJunling Zheng enum fsync_mode {
129893cf93f1SJunling Zheng 	FSYNC_MODE_POSIX,	/* fsync follows posix semantics */
129993cf93f1SJunling Zheng 	FSYNC_MODE_STRICT,	/* fsync behaves in line with ext4 */
1300d6290814SJaegeuk Kim 	FSYNC_MODE_NOBARRIER,	/* fsync behaves nobarrier based on posix */
130193cf93f1SJunling Zheng };
130293cf93f1SJunling Zheng 
13034c8ff709SChao Yu /*
13044c8ff709SChao Yu  * this value is set in page as a private data which indicate that
13054c8ff709SChao Yu  * the page is atomically written, and it is in inmem_pages list.
13064c8ff709SChao Yu  */
13074c8ff709SChao Yu #define ATOMIC_WRITTEN_PAGE		((unsigned long)-1)
13084c8ff709SChao Yu #define DUMMY_WRITTEN_PAGE		((unsigned long)-2)
13094c8ff709SChao Yu 
13104c8ff709SChao Yu #define IS_ATOMIC_WRITTEN_PAGE(page)			\
1311e90027d2SXiaojun Wang 		(page_private(page) == ATOMIC_WRITTEN_PAGE)
13124c8ff709SChao Yu #define IS_DUMMY_WRITTEN_PAGE(page)			\
1313e90027d2SXiaojun Wang 		(page_private(page) == DUMMY_WRITTEN_PAGE)
13144c8ff709SChao Yu 
131529b993c7SYu Changchun #ifdef CONFIG_F2FS_IO_TRACE
131629b993c7SYu Changchun #define IS_IO_TRACED_PAGE(page)			\
131729b993c7SYu Changchun 		(page_private(page) > 0 &&		\
131829b993c7SYu Changchun 		 page_private(page) < (unsigned long)PID_MAX_LIMIT)
131929b993c7SYu Changchun #else
132029b993c7SYu Changchun #define IS_IO_TRACED_PAGE(page) (0)
132129b993c7SYu Changchun #endif
132229b993c7SYu Changchun 
1323643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
1324ff62af20SSheng Yong #define DUMMY_ENCRYPTION_ENABLED(sbi) \
1325ed318a6cSEric Biggers 	(unlikely(F2FS_OPTION(sbi).dummy_enc_ctx.ctx != NULL))
1326ff62af20SSheng Yong #else
1327ff62af20SSheng Yong #define DUMMY_ENCRYPTION_ENABLED(sbi) (0)
1328ff62af20SSheng Yong #endif
1329ff62af20SSheng Yong 
13304c8ff709SChao Yu /* For compression */
13314c8ff709SChao Yu enum compress_algorithm_type {
13324c8ff709SChao Yu 	COMPRESS_LZO,
13334c8ff709SChao Yu 	COMPRESS_LZ4,
133450cfa66fSChao Yu 	COMPRESS_ZSTD,
13356d92b201SChao Yu 	COMPRESS_LZORLE,
13364c8ff709SChao Yu 	COMPRESS_MAX,
13374c8ff709SChao Yu };
13384c8ff709SChao Yu 
13390b32dc18SChao Yu #define COMPRESS_DATA_RESERVED_SIZE		5
13404c8ff709SChao Yu struct compress_data {
13414c8ff709SChao Yu 	__le32 clen;			/* compressed data size */
13424c8ff709SChao Yu 	__le32 reserved[COMPRESS_DATA_RESERVED_SIZE];	/* reserved */
13434c8ff709SChao Yu 	u8 cdata[];			/* compressed data */
13444c8ff709SChao Yu };
13454c8ff709SChao Yu 
13464c8ff709SChao Yu #define COMPRESS_HEADER_SIZE	(sizeof(struct compress_data))
13474c8ff709SChao Yu 
13484c8ff709SChao Yu #define F2FS_COMPRESSED_PAGE_MAGIC	0xF5F2C000
13494c8ff709SChao Yu 
13504c8ff709SChao Yu /* compress context */
13514c8ff709SChao Yu struct compress_ctx {
13524c8ff709SChao Yu 	struct inode *inode;		/* inode the context belong to */
13534c8ff709SChao Yu 	pgoff_t cluster_idx;		/* cluster index number */
13544c8ff709SChao Yu 	unsigned int cluster_size;	/* page count in cluster */
13554c8ff709SChao Yu 	unsigned int log_cluster_size;	/* log of cluster size */
13564c8ff709SChao Yu 	struct page **rpages;		/* pages store raw data in cluster */
13574c8ff709SChao Yu 	unsigned int nr_rpages;		/* total page number in rpages */
13584c8ff709SChao Yu 	struct page **cpages;		/* pages store compressed data in cluster */
13594c8ff709SChao Yu 	unsigned int nr_cpages;		/* total page number in cpages */
13604c8ff709SChao Yu 	void *rbuf;			/* virtual mapped address on rpages */
13614c8ff709SChao Yu 	struct compress_data *cbuf;	/* virtual mapped address on cpages */
13624c8ff709SChao Yu 	size_t rlen;			/* valid data length in rbuf */
13634c8ff709SChao Yu 	size_t clen;			/* valid data length in cbuf */
13644c8ff709SChao Yu 	void *private;			/* payload buffer for specified compression algorithm */
136550cfa66fSChao Yu 	void *private2;			/* extra payload buffer */
13664c8ff709SChao Yu };
13674c8ff709SChao Yu 
13684c8ff709SChao Yu /* compress context for write IO path */
13694c8ff709SChao Yu struct compress_io_ctx {
13704c8ff709SChao Yu 	u32 magic;			/* magic number to indicate page is compressed */
13714c8ff709SChao Yu 	struct inode *inode;		/* inode the context belong to */
13724c8ff709SChao Yu 	struct page **rpages;		/* pages store raw data in cluster */
13734c8ff709SChao Yu 	unsigned int nr_rpages;		/* total page number in rpages */
13744c8ff709SChao Yu 	refcount_t ref;			/* referrence count of raw page */
13754c8ff709SChao Yu };
13764c8ff709SChao Yu 
13774c8ff709SChao Yu /* decompress io context for read IO path */
13784c8ff709SChao Yu struct decompress_io_ctx {
13794c8ff709SChao Yu 	u32 magic;			/* magic number to indicate page is compressed */
13804c8ff709SChao Yu 	struct inode *inode;		/* inode the context belong to */
13814c8ff709SChao Yu 	pgoff_t cluster_idx;		/* cluster index number */
13824c8ff709SChao Yu 	unsigned int cluster_size;	/* page count in cluster */
13834c8ff709SChao Yu 	unsigned int log_cluster_size;	/* log of cluster size */
13844c8ff709SChao Yu 	struct page **rpages;		/* pages store raw data in cluster */
13854c8ff709SChao Yu 	unsigned int nr_rpages;		/* total page number in rpages */
13864c8ff709SChao Yu 	struct page **cpages;		/* pages store compressed data in cluster */
13874c8ff709SChao Yu 	unsigned int nr_cpages;		/* total page number in cpages */
13884c8ff709SChao Yu 	struct page **tpages;		/* temp pages to pad holes in cluster */
13894c8ff709SChao Yu 	void *rbuf;			/* virtual mapped address on rpages */
13904c8ff709SChao Yu 	struct compress_data *cbuf;	/* virtual mapped address on cpages */
13914c8ff709SChao Yu 	size_t rlen;			/* valid data length in rbuf */
13924c8ff709SChao Yu 	size_t clen;			/* valid data length in cbuf */
13934c8ff709SChao Yu 	refcount_t ref;			/* referrence count of compressed page */
13944c8ff709SChao Yu 	bool failed;			/* indicate IO error during decompression */
139550cfa66fSChao Yu 	void *private;			/* payload buffer for specified decompression algorithm */
139650cfa66fSChao Yu 	void *private2;			/* extra payload buffer */
13974c8ff709SChao Yu };
13984c8ff709SChao Yu 
13994c8ff709SChao Yu #define NULL_CLUSTER			((unsigned int)(~0))
14004c8ff709SChao Yu #define MIN_COMPRESS_LOG_SIZE		2
14014c8ff709SChao Yu #define MAX_COMPRESS_LOG_SIZE		8
140250cfa66fSChao Yu #define MAX_COMPRESS_WINDOW_SIZE	((PAGE_SIZE) << MAX_COMPRESS_LOG_SIZE)
14034c8ff709SChao Yu 
140439a53e0cSJaegeuk Kim struct f2fs_sb_info {
140539a53e0cSJaegeuk Kim 	struct super_block *sb;			/* pointer to VFS super block */
14065e176d54SJaegeuk Kim 	struct proc_dir_entry *s_proc;		/* proc entry */
140739a53e0cSJaegeuk Kim 	struct f2fs_super_block *raw_super;	/* raw super block pointer */
1408846ae671SChao Yu 	struct rw_semaphore sb_lock;		/* lock for raw super block */
1409e8240f65SChao Yu 	int valid_super_block;			/* valid super block no */
1410fadb2fb8SChao Yu 	unsigned long s_flag;				/* flags for sbi */
1411853137ceSJaegeuk Kim 	struct mutex writepages;		/* mutex for writepages() */
14125aba5430SDaniel Rosenberg #ifdef CONFIG_UNICODE
14135aba5430SDaniel Rosenberg 	struct unicode_map *s_encoding;
14145aba5430SDaniel Rosenberg 	__u16 s_encoding_flags;
14155aba5430SDaniel Rosenberg #endif
141639a53e0cSJaegeuk Kim 
1417178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
1418178053e2SDamien Le Moal 	unsigned int blocks_per_blkz;		/* F2FS blocks per zone */
1419178053e2SDamien Le Moal 	unsigned int log_blocks_per_blkz;	/* log2 F2FS blocks per zone */
1420178053e2SDamien Le Moal #endif
1421178053e2SDamien Le Moal 
142239a53e0cSJaegeuk Kim 	/* for node-related operations */
142339a53e0cSJaegeuk Kim 	struct f2fs_nm_info *nm_info;		/* node manager */
142439a53e0cSJaegeuk Kim 	struct inode *node_inode;		/* cache node blocks */
142539a53e0cSJaegeuk Kim 
142639a53e0cSJaegeuk Kim 	/* for segment-related operations */
142739a53e0cSJaegeuk Kim 	struct f2fs_sm_info *sm_info;		/* segment manager */
14281ff7bd3bSJaegeuk Kim 
14291ff7bd3bSJaegeuk Kim 	/* for bio operations */
1430a912b54dSJaegeuk Kim 	struct f2fs_bio_info *write_io[NR_PAGE_TYPE];	/* for write bios */
1431107a805dSChao Yu 	/* keep migration IO order for LFS mode */
1432107a805dSChao Yu 	struct rw_semaphore io_order_lock;
14330a595ebaSJaegeuk Kim 	mempool_t *write_io_dummy;		/* Dummy pages */
143439a53e0cSJaegeuk Kim 
143539a53e0cSJaegeuk Kim 	/* for checkpoint */
143639a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt;		/* raw checkpoint pointer */
14378508e44aSJaegeuk Kim 	int cur_cp_pack;			/* remain current cp pack */
1438aaec2b1dSChao Yu 	spinlock_t cp_lock;			/* for flag in ckpt */
143939a53e0cSJaegeuk Kim 	struct inode *meta_inode;		/* cache meta blocks */
144039936837SJaegeuk Kim 	struct mutex cp_mutex;			/* checkpoint procedure lock */
1441b873b798SJaegeuk Kim 	struct rw_semaphore cp_rwsem;		/* blocking FS operations */
1442b3582c68SChao Yu 	struct rw_semaphore node_write;		/* locking node writes */
144359c9081bSYunlei He 	struct rw_semaphore node_change;	/* locking node change */
1444fb51b5efSChangman Lee 	wait_queue_head_t cp_wait;
14456beceb54SJaegeuk Kim 	unsigned long last_time[MAX_TIME];	/* to store time in jiffies */
14466beceb54SJaegeuk Kim 	long interval_time[MAX_TIME];		/* to store thresholds */
144739a53e0cSJaegeuk Kim 
144867298804SChao Yu 	struct inode_management im[MAX_INO_ENTRY];	/* manage inode cache */
14496451e041SJaegeuk Kim 
145050fa53ecSChao Yu 	spinlock_t fsync_node_lock;		/* for node entry lock */
145150fa53ecSChao Yu 	struct list_head fsync_node_list;	/* node list head */
145250fa53ecSChao Yu 	unsigned int fsync_seg_id;		/* sequence id */
145350fa53ecSChao Yu 	unsigned int fsync_node_num;		/* number of node entries */
145450fa53ecSChao Yu 
14556451e041SJaegeuk Kim 	/* for orphan inode, use 0'th array */
14560d47c1adSGu Zheng 	unsigned int max_orphans;		/* max orphan inodes */
145739a53e0cSJaegeuk Kim 
1458c227f912SChao Yu 	/* for inode management */
1459c227f912SChao Yu 	struct list_head inode_list[NR_INODE_TYPE];	/* dirty inode list */
1460c227f912SChao Yu 	spinlock_t inode_lock[NR_INODE_TYPE];	/* for dirty inode list lock */
1461040d2bb3SChao Yu 	struct mutex flush_lock;		/* for flush exclusion */
146239a53e0cSJaegeuk Kim 
146313054c54SChao Yu 	/* for extent tree cache */
146413054c54SChao Yu 	struct radix_tree_root extent_tree_root;/* cache extent cache entries */
14655e8256acSYunlei He 	struct mutex extent_tree_lock;	/* locking extent radix tree */
146613054c54SChao Yu 	struct list_head extent_list;		/* lru list for shrinker */
146713054c54SChao Yu 	spinlock_t extent_lock;			/* locking extent lru list */
14687441ccefSJaegeuk Kim 	atomic_t total_ext_tree;		/* extent tree count */
1469137d09f0SJaegeuk Kim 	struct list_head zombie_list;		/* extent zombie tree list */
147074fd8d99SJaegeuk Kim 	atomic_t total_zombie_tree;		/* extent zombie tree count */
147113054c54SChao Yu 	atomic_t total_ext_node;		/* extent info count */
147213054c54SChao Yu 
147339a53e0cSJaegeuk Kim 	/* basic filesystem units */
147439a53e0cSJaegeuk Kim 	unsigned int log_sectors_per_block;	/* log2 sectors per block */
147539a53e0cSJaegeuk Kim 	unsigned int log_blocksize;		/* log2 block size */
147639a53e0cSJaegeuk Kim 	unsigned int blocksize;			/* block size */
147739a53e0cSJaegeuk Kim 	unsigned int root_ino_num;		/* root inode number*/
147839a53e0cSJaegeuk Kim 	unsigned int node_ino_num;		/* node inode number*/
147939a53e0cSJaegeuk Kim 	unsigned int meta_ino_num;		/* meta inode number*/
148039a53e0cSJaegeuk Kim 	unsigned int log_blocks_per_seg;	/* log2 blocks per segment */
148139a53e0cSJaegeuk Kim 	unsigned int blocks_per_seg;		/* blocks per segment */
148239a53e0cSJaegeuk Kim 	unsigned int segs_per_sec;		/* segments per section */
148339a53e0cSJaegeuk Kim 	unsigned int secs_per_zone;		/* sections per zone */
148439a53e0cSJaegeuk Kim 	unsigned int total_sections;		/* total section count */
148539a53e0cSJaegeuk Kim 	unsigned int total_node_count;		/* total node block count */
148639a53e0cSJaegeuk Kim 	unsigned int total_valid_node_count;	/* valid node block count */
1487e0afc4d6SChao Yu 	loff_t max_file_blocks;			/* max block index of file */
1488ab9fa662SJaegeuk Kim 	int dir_level;				/* directory level */
1489f6df8f23SSheng Yong 	int readdir_ra;				/* readahead inode in readdir */
149039a53e0cSJaegeuk Kim 
149139a53e0cSJaegeuk Kim 	block_t user_block_count;		/* # of user blocks */
149239a53e0cSJaegeuk Kim 	block_t total_valid_block_count;	/* # of valid blocks */
1493a66cdd98SJaegeuk Kim 	block_t discard_blks;			/* discard command candidats */
149439a53e0cSJaegeuk Kim 	block_t last_valid_block_count;		/* for recovery */
1495daeb433eSChao Yu 	block_t reserved_blocks;		/* configurable reserved blocks */
149680d42145SYunlong Song 	block_t current_reserved_blocks;	/* current reserved blocks */
1497daeb433eSChao Yu 
14984354994fSDaniel Rosenberg 	/* Additional tracking for no checkpoint mode */
14994354994fSDaniel Rosenberg 	block_t unusable_block_count;		/* # of blocks saved by last cp */
15004354994fSDaniel Rosenberg 
1501292c196aSChao Yu 	unsigned int nquota_files;		/* # of quota sysfile */
1502db6ec53bSJaegeuk Kim 	struct rw_semaphore quota_sem;		/* blocking cp for flags */
1503292c196aSChao Yu 
1504523be8a6SJaegeuk Kim 	/* # of pages, see count_type */
150535782b23SJaegeuk Kim 	atomic_t nr_pages[NR_COUNT_TYPE];
150641382ec4SJaegeuk Kim 	/* # of allocated blocks */
150741382ec4SJaegeuk Kim 	struct percpu_counter alloc_valid_block_count;
150839a53e0cSJaegeuk Kim 
1509687de7f1SJaegeuk Kim 	/* writeback control */
1510c29fd0c0SChao Yu 	atomic_t wb_sync_req[META];	/* count # of WB_SYNC threads */
1511687de7f1SJaegeuk Kim 
1512513c5f37SJaegeuk Kim 	/* valid inode count */
1513513c5f37SJaegeuk Kim 	struct percpu_counter total_valid_inode_count;
1514513c5f37SJaegeuk Kim 
151539a53e0cSJaegeuk Kim 	struct f2fs_mount_info mount_opt;	/* mount options */
151639a53e0cSJaegeuk Kim 
151739a53e0cSJaegeuk Kim 	/* for cleaning operations */
1518fb24fea7SChao Yu 	struct rw_semaphore gc_lock;		/*
1519fb24fea7SChao Yu 						 * semaphore for GC, avoid
1520fb24fea7SChao Yu 						 * race between GC and GC or CP
1521fb24fea7SChao Yu 						 */
152239a53e0cSJaegeuk Kim 	struct f2fs_gc_kthread	*gc_thread;	/* GC thread */
15235ec4e49fSJaegeuk Kim 	unsigned int cur_victim_sec;		/* current victim section num */
15245b0e9539SJaegeuk Kim 	unsigned int gc_mode;			/* current GC state */
1525e3080b01SChao Yu 	unsigned int next_victim_seg[2];	/* next segment in victim section */
15260e5e8111SDaeho Jeong 
15272ef79ecbSChao Yu 	/* for skip statistic */
1528677017d1SSahitya Tummala 	unsigned int atomic_files;		/* # of opened atomic file */
15292ef79ecbSChao Yu 	unsigned long long skipped_atomic_files[2];	/* FG_GC and BG_GC */
15306f8d4455SJaegeuk Kim 	unsigned long long skipped_gc_rwsem;		/* FG_GC only */
153139a53e0cSJaegeuk Kim 
15321ad71a27SJaegeuk Kim 	/* threshold for gc trials on pinned files */
15331ad71a27SJaegeuk Kim 	u64 gc_pin_file_threshold;
1534f5a53edcSJaegeuk Kim 	struct rw_semaphore pin_sem;
15351ad71a27SJaegeuk Kim 
1536b1c57c1cSJaegeuk Kim 	/* maximum # of trials to find a victim segment for SSR and GC */
1537b1c57c1cSJaegeuk Kim 	unsigned int max_victim_search;
1538e3080b01SChao Yu 	/* migration granularity of garbage collection, unit: segment */
1539e3080b01SChao Yu 	unsigned int migration_granularity;
1540b1c57c1cSJaegeuk Kim 
154139a53e0cSJaegeuk Kim 	/*
154239a53e0cSJaegeuk Kim 	 * for stat information.
154339a53e0cSJaegeuk Kim 	 * one is for the LFS mode, and the other is for the SSR mode.
154439a53e0cSJaegeuk Kim 	 */
154535b09d82SNamjae Jeon #ifdef CONFIG_F2FS_STAT_FS
154639a53e0cSJaegeuk Kim 	struct f2fs_stat_info *stat_info;	/* FS status information */
1547b63e7be5SChao Yu 	atomic_t meta_count[META_MAX];		/* # of meta blocks */
154839a53e0cSJaegeuk Kim 	unsigned int segment_count[2];		/* # of allocated segments */
154939a53e0cSJaegeuk Kim 	unsigned int block_count[2];		/* # of allocated blocks */
1550b9a2c252SChangman Lee 	atomic_t inplace_count;		/* # of inplace update */
15515b7ee374SChao Yu 	atomic64_t total_hit_ext;		/* # of lookup extent cache */
15525b7ee374SChao Yu 	atomic64_t read_hit_rbtree;		/* # of hit rbtree extent node */
15535b7ee374SChao Yu 	atomic64_t read_hit_largest;		/* # of hit largest extent node */
15545b7ee374SChao Yu 	atomic64_t read_hit_cached;		/* # of hit cached extent node */
1555d5e8f6c9SChao Yu 	atomic_t inline_xattr;			/* # of inline_xattr inodes */
155603e14d52SChao Yu 	atomic_t inline_inode;			/* # of inline_data inodes */
155703e14d52SChao Yu 	atomic_t inline_dir;			/* # of inline_dentry inodes */
15584c8ff709SChao Yu 	atomic_t compr_inode;			/* # of compressed inodes */
15594c8ff709SChao Yu 	atomic_t compr_blocks;			/* # of compressed blocks */
1560648d50baSChao Yu 	atomic_t vw_cnt;			/* # of volatile writes */
156126a28a0cSJaegeuk Kim 	atomic_t max_aw_cnt;			/* max # of atomic writes */
1562648d50baSChao Yu 	atomic_t max_vw_cnt;			/* max # of volatile writes */
1563274bd9baSChao Yu 	unsigned int io_skip_bggc;		/* skip background gc for in-flight IO */
1564274bd9baSChao Yu 	unsigned int other_skip_bggc;		/* skip background gc for other reasons */
156533fbd510SChao Yu 	unsigned int ndirty_inode[NR_INODE_TYPE];	/* # of dirty inodes */
156635b09d82SNamjae Jeon #endif
156739a53e0cSJaegeuk Kim 	spinlock_t stat_lock;			/* lock for stat operations */
1568b59d0baeSNamjae Jeon 
1569b0af6d49SChao Yu 	/* For app/fs IO statistics */
1570b0af6d49SChao Yu 	spinlock_t iostat_lock;
15718b83ac81SChao Yu 	unsigned long long rw_iostat[NR_IO_TYPE];
15728b83ac81SChao Yu 	unsigned long long prev_rw_iostat[NR_IO_TYPE];
1573b0af6d49SChao Yu 	bool iostat_enable;
15742bc4bea3SDaeho Jeong 	unsigned long iostat_next_period;
15752bc4bea3SDaeho Jeong 	unsigned int iostat_period_ms;
1576b0af6d49SChao Yu 
1577da9953b7SJaegeuk Kim 	/* to attach REQ_META|REQ_FUA flags */
1578da9953b7SJaegeuk Kim 	unsigned int data_io_flag;
157932b6aba8SJaegeuk Kim 	unsigned int node_io_flag;
1580b59d0baeSNamjae Jeon 
1581b59d0baeSNamjae Jeon 	/* For sysfs suppport */
1582b59d0baeSNamjae Jeon 	struct kobject s_kobj;
1583b59d0baeSNamjae Jeon 	struct completion s_kobj_unregister;
15842658e50dSJaegeuk Kim 
15852658e50dSJaegeuk Kim 	/* For shrinker support */
15862658e50dSJaegeuk Kim 	struct list_head s_list;
15873c62be17SJaegeuk Kim 	int s_ndevs;				/* number of devices */
15883c62be17SJaegeuk Kim 	struct f2fs_dev_info *devs;		/* for device list */
15891228b482SChao Yu 	unsigned int dirty_device;		/* for checkpoint data flush */
15901228b482SChao Yu 	spinlock_t dev_lock;			/* protect dirty_device */
15912658e50dSJaegeuk Kim 	struct mutex umount_mutex;
15922658e50dSJaegeuk Kim 	unsigned int shrinker_run_no;
15938f1dbbbbSShuoran Liu 
15948f1dbbbbSShuoran Liu 	/* For write statistics */
15958f1dbbbbSShuoran Liu 	u64 sectors_written_start;
15968f1dbbbbSShuoran Liu 	u64 kbytes_written;
159743b6573bSKeith Mok 
159843b6573bSKeith Mok 	/* Reference to checksum algorithm driver via cryptoapi */
159943b6573bSKeith Mok 	struct crypto_shash *s_chksum_driver;
16001ecc0c5cSChao Yu 
1601704956ecSChao Yu 	/* Precomputed FS UUID checksum for seeding other checksums */
1602704956ecSChao Yu 	__u32 s_chksum_seed;
16034c8ff709SChao Yu 
16044c8ff709SChao Yu 	struct workqueue_struct *post_read_wq;	/* post read workqueue */
1605a999150fSChao Yu 
1606a999150fSChao Yu 	struct kmem_cache *inline_xattr_slab;	/* inline xattr entry */
1607a999150fSChao Yu 	unsigned int inline_xattr_slab_size;	/* default inline xattr slab size */
160839a53e0cSJaegeuk Kim };
160939a53e0cSJaegeuk Kim 
161002b16d0aSChao Yu struct f2fs_private_dio {
161102b16d0aSChao Yu 	struct inode *inode;
161202b16d0aSChao Yu 	void *orig_private;
161302b16d0aSChao Yu 	bio_end_io_t *orig_end_io;
161402b16d0aSChao Yu 	bool write;
161502b16d0aSChao Yu };
161602b16d0aSChao Yu 
16171ecc0c5cSChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
1618c45d6002SChao Yu #define f2fs_show_injection_info(sbi, type)					\
1619c45d6002SChao Yu 	printk_ratelimited("%sF2FS-fs (%s) : inject %s in %s of %pS\n",	\
1620c45d6002SChao Yu 		KERN_INFO, sbi->sb->s_id,				\
1621c45d6002SChao Yu 		f2fs_fault_name[type],					\
162255523519SChao Yu 		__func__, __builtin_return_address(0))
16231ecc0c5cSChao Yu static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
16241ecc0c5cSChao Yu {
162563189b78SChao Yu 	struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
16261ecc0c5cSChao Yu 
16271ecc0c5cSChao Yu 	if (!ffi->inject_rate)
16281ecc0c5cSChao Yu 		return false;
16291ecc0c5cSChao Yu 
16301ecc0c5cSChao Yu 	if (!IS_FAULT_SET(ffi, type))
16311ecc0c5cSChao Yu 		return false;
16321ecc0c5cSChao Yu 
16331ecc0c5cSChao Yu 	atomic_inc(&ffi->inject_ops);
16341ecc0c5cSChao Yu 	if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
16351ecc0c5cSChao Yu 		atomic_set(&ffi->inject_ops, 0);
16361ecc0c5cSChao Yu 		return true;
16371ecc0c5cSChao Yu 	}
16381ecc0c5cSChao Yu 	return false;
16391ecc0c5cSChao Yu }
16407fa750a1SArnd Bergmann #else
1641c45d6002SChao Yu #define f2fs_show_injection_info(sbi, type) do { } while (0)
16427fa750a1SArnd Bergmann static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
16437fa750a1SArnd Bergmann {
16447fa750a1SArnd Bergmann 	return false;
16457fa750a1SArnd Bergmann }
16461ecc0c5cSChao Yu #endif
16471ecc0c5cSChao Yu 
16480916878dSDamien Le Moal /*
16490916878dSDamien Le Moal  * Test if the mounted volume is a multi-device volume.
16500916878dSDamien Le Moal  *   - For a single regular disk volume, sbi->s_ndevs is 0.
16510916878dSDamien Le Moal  *   - For a single zoned disk volume, sbi->s_ndevs is 1.
16520916878dSDamien Le Moal  *   - For a multi-device volume, sbi->s_ndevs is always 2 or more.
16530916878dSDamien Le Moal  */
16540916878dSDamien Le Moal static inline bool f2fs_is_multi_device(struct f2fs_sb_info *sbi)
16550916878dSDamien Le Moal {
16560916878dSDamien Le Moal 	return sbi->s_ndevs > 1;
16570916878dSDamien Le Moal }
16580916878dSDamien Le Moal 
16598f1dbbbbSShuoran Liu /* For write statistics. Suppose sector size is 512 bytes,
16608f1dbbbbSShuoran Liu  * and the return value is in kbytes. s is of struct f2fs_sb_info.
16618f1dbbbbSShuoran Liu  */
16628f1dbbbbSShuoran Liu #define BD_PART_WRITTEN(s)						 \
1663dbae2c55SMichael Callahan (((u64)part_stat_read((s)->sb->s_bdev->bd_part, sectors[STAT_WRITE]) -   \
166468afcf2dSTomohiro Kusumi 		(s)->sectors_written_start) >> 1)
16658f1dbbbbSShuoran Liu 
16666beceb54SJaegeuk Kim static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
16676beceb54SJaegeuk Kim {
1668a7d10cf3SSahitya Tummala 	unsigned long now = jiffies;
1669a7d10cf3SSahitya Tummala 
1670a7d10cf3SSahitya Tummala 	sbi->last_time[type] = now;
1671a7d10cf3SSahitya Tummala 
1672a7d10cf3SSahitya Tummala 	/* DISCARD_TIME and GC_TIME are based on REQ_TIME */
1673a7d10cf3SSahitya Tummala 	if (type == REQ_TIME) {
1674a7d10cf3SSahitya Tummala 		sbi->last_time[DISCARD_TIME] = now;
1675a7d10cf3SSahitya Tummala 		sbi->last_time[GC_TIME] = now;
1676a7d10cf3SSahitya Tummala 	}
16776beceb54SJaegeuk Kim }
16786beceb54SJaegeuk Kim 
16796beceb54SJaegeuk Kim static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
16806beceb54SJaegeuk Kim {
16816bccfa19SArnd Bergmann 	unsigned long interval = sbi->interval_time[type] * HZ;
16826beceb54SJaegeuk Kim 
16836beceb54SJaegeuk Kim 	return time_after(jiffies, sbi->last_time[type] + interval);
16846beceb54SJaegeuk Kim }
16856beceb54SJaegeuk Kim 
1686a7d10cf3SSahitya Tummala static inline unsigned int f2fs_time_to_wait(struct f2fs_sb_info *sbi,
1687a7d10cf3SSahitya Tummala 						int type)
1688a7d10cf3SSahitya Tummala {
1689a7d10cf3SSahitya Tummala 	unsigned long interval = sbi->interval_time[type] * HZ;
1690a7d10cf3SSahitya Tummala 	unsigned int wait_ms = 0;
1691a7d10cf3SSahitya Tummala 	long delta;
1692a7d10cf3SSahitya Tummala 
1693a7d10cf3SSahitya Tummala 	delta = (sbi->last_time[type] + interval) - jiffies;
1694a7d10cf3SSahitya Tummala 	if (delta > 0)
1695a7d10cf3SSahitya Tummala 		wait_ms = jiffies_to_msecs(delta);
1696a7d10cf3SSahitya Tummala 
1697a7d10cf3SSahitya Tummala 	return wait_ms;
1698a7d10cf3SSahitya Tummala }
1699a7d10cf3SSahitya Tummala 
170039a53e0cSJaegeuk Kim /*
170139a53e0cSJaegeuk Kim  * Inline functions
170239a53e0cSJaegeuk Kim  */
1703416d2dbbSChao Yu static inline u32 __f2fs_crc32(struct f2fs_sb_info *sbi, u32 crc,
1704704956ecSChao Yu 			      const void *address, unsigned int length)
1705704956ecSChao Yu {
1706704956ecSChao Yu 	struct {
1707704956ecSChao Yu 		struct shash_desc shash;
1708704956ecSChao Yu 		char ctx[4];
1709704956ecSChao Yu 	} desc;
1710704956ecSChao Yu 	int err;
1711704956ecSChao Yu 
1712704956ecSChao Yu 	BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver) != sizeof(desc.ctx));
1713704956ecSChao Yu 
1714704956ecSChao Yu 	desc.shash.tfm = sbi->s_chksum_driver;
1715704956ecSChao Yu 	*(u32 *)desc.ctx = crc;
1716704956ecSChao Yu 
1717704956ecSChao Yu 	err = crypto_shash_update(&desc.shash, address, length);
1718704956ecSChao Yu 	BUG_ON(err);
1719704956ecSChao Yu 
1720704956ecSChao Yu 	return *(u32 *)desc.ctx;
1721704956ecSChao Yu }
1722704956ecSChao Yu 
1723416d2dbbSChao Yu static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
1724416d2dbbSChao Yu 			   unsigned int length)
1725416d2dbbSChao Yu {
1726416d2dbbSChao Yu 	return __f2fs_crc32(sbi, F2FS_SUPER_MAGIC, address, length);
1727416d2dbbSChao Yu }
1728416d2dbbSChao Yu 
1729416d2dbbSChao Yu static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
1730416d2dbbSChao Yu 				  void *buf, size_t buf_size)
1731416d2dbbSChao Yu {
1732416d2dbbSChao Yu 	return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
1733416d2dbbSChao Yu }
1734416d2dbbSChao Yu 
1735416d2dbbSChao Yu static inline u32 f2fs_chksum(struct f2fs_sb_info *sbi, u32 crc,
1736416d2dbbSChao Yu 			      const void *address, unsigned int length)
1737416d2dbbSChao Yu {
1738416d2dbbSChao Yu 	return __f2fs_crc32(sbi, crc, address, length);
1739416d2dbbSChao Yu }
1740416d2dbbSChao Yu 
174139a53e0cSJaegeuk Kim static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
174239a53e0cSJaegeuk Kim {
174339a53e0cSJaegeuk Kim 	return container_of(inode, struct f2fs_inode_info, vfs_inode);
174439a53e0cSJaegeuk Kim }
174539a53e0cSJaegeuk Kim 
174639a53e0cSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
174739a53e0cSJaegeuk Kim {
174839a53e0cSJaegeuk Kim 	return sb->s_fs_info;
174939a53e0cSJaegeuk Kim }
175039a53e0cSJaegeuk Kim 
17514081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
17524081363fSJaegeuk Kim {
17534081363fSJaegeuk Kim 	return F2FS_SB(inode->i_sb);
17544081363fSJaegeuk Kim }
17554081363fSJaegeuk Kim 
17564081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
17574081363fSJaegeuk Kim {
17584081363fSJaegeuk Kim 	return F2FS_I_SB(mapping->host);
17594081363fSJaegeuk Kim }
17604081363fSJaegeuk Kim 
17614081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
17624081363fSJaegeuk Kim {
17634969c06aSJaegeuk Kim 	return F2FS_M_SB(page_file_mapping(page));
17644081363fSJaegeuk Kim }
17654081363fSJaegeuk Kim 
176639a53e0cSJaegeuk Kim static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
176739a53e0cSJaegeuk Kim {
176839a53e0cSJaegeuk Kim 	return (struct f2fs_super_block *)(sbi->raw_super);
176939a53e0cSJaegeuk Kim }
177039a53e0cSJaegeuk Kim 
177139a53e0cSJaegeuk Kim static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
177239a53e0cSJaegeuk Kim {
177339a53e0cSJaegeuk Kim 	return (struct f2fs_checkpoint *)(sbi->ckpt);
177439a53e0cSJaegeuk Kim }
177539a53e0cSJaegeuk Kim 
177645590710SGu Zheng static inline struct f2fs_node *F2FS_NODE(struct page *page)
177745590710SGu Zheng {
177845590710SGu Zheng 	return (struct f2fs_node *)page_address(page);
177945590710SGu Zheng }
178045590710SGu Zheng 
178158bfaf44SJaegeuk Kim static inline struct f2fs_inode *F2FS_INODE(struct page *page)
178258bfaf44SJaegeuk Kim {
178358bfaf44SJaegeuk Kim 	return &((struct f2fs_node *)page_address(page))->i;
178458bfaf44SJaegeuk Kim }
178558bfaf44SJaegeuk Kim 
178639a53e0cSJaegeuk Kim static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
178739a53e0cSJaegeuk Kim {
178839a53e0cSJaegeuk Kim 	return (struct f2fs_nm_info *)(sbi->nm_info);
178939a53e0cSJaegeuk Kim }
179039a53e0cSJaegeuk Kim 
179139a53e0cSJaegeuk Kim static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
179239a53e0cSJaegeuk Kim {
179339a53e0cSJaegeuk Kim 	return (struct f2fs_sm_info *)(sbi->sm_info);
179439a53e0cSJaegeuk Kim }
179539a53e0cSJaegeuk Kim 
179639a53e0cSJaegeuk Kim static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
179739a53e0cSJaegeuk Kim {
179839a53e0cSJaegeuk Kim 	return (struct sit_info *)(SM_I(sbi)->sit_info);
179939a53e0cSJaegeuk Kim }
180039a53e0cSJaegeuk Kim 
180139a53e0cSJaegeuk Kim static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
180239a53e0cSJaegeuk Kim {
180339a53e0cSJaegeuk Kim 	return (struct free_segmap_info *)(SM_I(sbi)->free_info);
180439a53e0cSJaegeuk Kim }
180539a53e0cSJaegeuk Kim 
180639a53e0cSJaegeuk Kim static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
180739a53e0cSJaegeuk Kim {
180839a53e0cSJaegeuk Kim 	return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
180939a53e0cSJaegeuk Kim }
181039a53e0cSJaegeuk Kim 
18119df27d98SGu Zheng static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
18129df27d98SGu Zheng {
18139df27d98SGu Zheng 	return sbi->meta_inode->i_mapping;
18149df27d98SGu Zheng }
18159df27d98SGu Zheng 
18164ef51a8fSJaegeuk Kim static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
18174ef51a8fSJaegeuk Kim {
18184ef51a8fSJaegeuk Kim 	return sbi->node_inode->i_mapping;
18194ef51a8fSJaegeuk Kim }
18204ef51a8fSJaegeuk Kim 
1821caf0047eSChao Yu static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
182239a53e0cSJaegeuk Kim {
1823fadb2fb8SChao Yu 	return test_bit(type, &sbi->s_flag);
182439a53e0cSJaegeuk Kim }
182539a53e0cSJaegeuk Kim 
1826caf0047eSChao Yu static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
182739a53e0cSJaegeuk Kim {
1828fadb2fb8SChao Yu 	set_bit(type, &sbi->s_flag);
1829caf0047eSChao Yu }
1830caf0047eSChao Yu 
1831caf0047eSChao Yu static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
1832caf0047eSChao Yu {
1833fadb2fb8SChao Yu 	clear_bit(type, &sbi->s_flag);
183439a53e0cSJaegeuk Kim }
183539a53e0cSJaegeuk Kim 
1836d71b5564SJaegeuk Kim static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
1837d71b5564SJaegeuk Kim {
1838d71b5564SJaegeuk Kim 	return le64_to_cpu(cp->checkpoint_ver);
1839d71b5564SJaegeuk Kim }
1840d71b5564SJaegeuk Kim 
1841ea676733SJaegeuk Kim static inline unsigned long f2fs_qf_ino(struct super_block *sb, int type)
1842ea676733SJaegeuk Kim {
1843ea676733SJaegeuk Kim 	if (type < F2FS_MAX_QUOTAS)
1844ea676733SJaegeuk Kim 		return le32_to_cpu(F2FS_SB(sb)->raw_super->qf_ino[type]);
1845ea676733SJaegeuk Kim 	return 0;
1846ea676733SJaegeuk Kim }
1847ea676733SJaegeuk Kim 
1848ced2c7eaSKinglong Mee static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
1849ced2c7eaSKinglong Mee {
1850ced2c7eaSKinglong Mee 	size_t crc_offset = le32_to_cpu(cp->checksum_offset);
1851ced2c7eaSKinglong Mee 	return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
1852ced2c7eaSKinglong Mee }
1853ced2c7eaSKinglong Mee 
1854aaec2b1dSChao Yu static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
185525ca923bSJaegeuk Kim {
185625ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
1857aaec2b1dSChao Yu 
185825ca923bSJaegeuk Kim 	return ckpt_flags & f;
185925ca923bSJaegeuk Kim }
186025ca923bSJaegeuk Kim 
1861aaec2b1dSChao Yu static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
186225ca923bSJaegeuk Kim {
1863aaec2b1dSChao Yu 	return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
1864aaec2b1dSChao Yu }
1865aaec2b1dSChao Yu 
1866aaec2b1dSChao Yu static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1867aaec2b1dSChao Yu {
1868aaec2b1dSChao Yu 	unsigned int ckpt_flags;
1869aaec2b1dSChao Yu 
1870aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
187125ca923bSJaegeuk Kim 	ckpt_flags |= f;
187225ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
187325ca923bSJaegeuk Kim }
187425ca923bSJaegeuk Kim 
1875aaec2b1dSChao Yu static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
187625ca923bSJaegeuk Kim {
1877d1aa2453SChao Yu 	unsigned long flags;
1878d1aa2453SChao Yu 
1879d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1880aaec2b1dSChao Yu 	__set_ckpt_flags(F2FS_CKPT(sbi), f);
1881d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1882aaec2b1dSChao Yu }
1883aaec2b1dSChao Yu 
1884aaec2b1dSChao Yu static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1885aaec2b1dSChao Yu {
1886aaec2b1dSChao Yu 	unsigned int ckpt_flags;
1887aaec2b1dSChao Yu 
1888aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
188925ca923bSJaegeuk Kim 	ckpt_flags &= (~f);
189025ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
189125ca923bSJaegeuk Kim }
189225ca923bSJaegeuk Kim 
1893aaec2b1dSChao Yu static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
1894aaec2b1dSChao Yu {
1895d1aa2453SChao Yu 	unsigned long flags;
1896d1aa2453SChao Yu 
1897d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1898aaec2b1dSChao Yu 	__clear_ckpt_flags(F2FS_CKPT(sbi), f);
1899d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1900aaec2b1dSChao Yu }
1901aaec2b1dSChao Yu 
190222ad0b6aSJaegeuk Kim static inline void disable_nat_bits(struct f2fs_sb_info *sbi, bool lock)
190322ad0b6aSJaegeuk Kim {
1904d1aa2453SChao Yu 	unsigned long flags;
19050921835cSChao Yu 	unsigned char *nat_bits;
1906d1aa2453SChao Yu 
1907a742fd41SJaegeuk Kim 	/*
1908a742fd41SJaegeuk Kim 	 * In order to re-enable nat_bits we need to call fsck.f2fs by
1909a742fd41SJaegeuk Kim 	 * set_sbi_flag(sbi, SBI_NEED_FSCK). But it may give huge cost,
1910a742fd41SJaegeuk Kim 	 * so let's rely on regular fsck or unclean shutdown.
1911a742fd41SJaegeuk Kim 	 */
191222ad0b6aSJaegeuk Kim 
191322ad0b6aSJaegeuk Kim 	if (lock)
1914d1aa2453SChao Yu 		spin_lock_irqsave(&sbi->cp_lock, flags);
191522ad0b6aSJaegeuk Kim 	__clear_ckpt_flags(F2FS_CKPT(sbi), CP_NAT_BITS_FLAG);
19160921835cSChao Yu 	nat_bits = NM_I(sbi)->nat_bits;
191722ad0b6aSJaegeuk Kim 	NM_I(sbi)->nat_bits = NULL;
191822ad0b6aSJaegeuk Kim 	if (lock)
1919d1aa2453SChao Yu 		spin_unlock_irqrestore(&sbi->cp_lock, flags);
19200921835cSChao Yu 
19210921835cSChao Yu 	kvfree(nat_bits);
192222ad0b6aSJaegeuk Kim }
192322ad0b6aSJaegeuk Kim 
192422ad0b6aSJaegeuk Kim static inline bool enabled_nat_bits(struct f2fs_sb_info *sbi,
192522ad0b6aSJaegeuk Kim 					struct cp_control *cpc)
192622ad0b6aSJaegeuk Kim {
192722ad0b6aSJaegeuk Kim 	bool set = is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
192822ad0b6aSJaegeuk Kim 
1929c473f1a9SChao Yu 	return (cpc) ? (cpc->reason & CP_UMOUNT) && set : set;
193022ad0b6aSJaegeuk Kim }
193122ad0b6aSJaegeuk Kim 
1932e479556bSGu Zheng static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
193339a53e0cSJaegeuk Kim {
1934b873b798SJaegeuk Kim 	down_read(&sbi->cp_rwsem);
193539a53e0cSJaegeuk Kim }
193639a53e0cSJaegeuk Kim 
1937cc15620bSJaegeuk Kim static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi)
1938cc15620bSJaegeuk Kim {
1939cc15620bSJaegeuk Kim 	return down_read_trylock(&sbi->cp_rwsem);
1940cc15620bSJaegeuk Kim }
1941cc15620bSJaegeuk Kim 
1942e479556bSGu Zheng static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
194339a53e0cSJaegeuk Kim {
1944b873b798SJaegeuk Kim 	up_read(&sbi->cp_rwsem);
194539936837SJaegeuk Kim }
194639936837SJaegeuk Kim 
1947e479556bSGu Zheng static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
194839936837SJaegeuk Kim {
1949b873b798SJaegeuk Kim 	down_write(&sbi->cp_rwsem);
195039936837SJaegeuk Kim }
195139936837SJaegeuk Kim 
1952e479556bSGu Zheng static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
195339936837SJaegeuk Kim {
1954b873b798SJaegeuk Kim 	up_write(&sbi->cp_rwsem);
195539a53e0cSJaegeuk Kim }
195639a53e0cSJaegeuk Kim 
1957119ee914SJaegeuk Kim static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
1958119ee914SJaegeuk Kim {
1959119ee914SJaegeuk Kim 	int reason = CP_SYNC;
1960119ee914SJaegeuk Kim 
1961119ee914SJaegeuk Kim 	if (test_opt(sbi, FASTBOOT))
1962119ee914SJaegeuk Kim 		reason = CP_FASTBOOT;
1963119ee914SJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
1964119ee914SJaegeuk Kim 		reason = CP_UMOUNT;
1965119ee914SJaegeuk Kim 	return reason;
1966119ee914SJaegeuk Kim }
1967119ee914SJaegeuk Kim 
1968119ee914SJaegeuk Kim static inline bool __remain_node_summaries(int reason)
1969119ee914SJaegeuk Kim {
1970c473f1a9SChao Yu 	return (reason & (CP_UMOUNT | CP_FASTBOOT));
1971119ee914SJaegeuk Kim }
1972119ee914SJaegeuk Kim 
1973119ee914SJaegeuk Kim static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
1974119ee914SJaegeuk Kim {
1975aaec2b1dSChao Yu 	return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
1976aaec2b1dSChao Yu 			is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
1977119ee914SJaegeuk Kim }
1978119ee914SJaegeuk Kim 
197939a53e0cSJaegeuk Kim /*
198039a53e0cSJaegeuk Kim  * Check whether the inode has blocks or not
198139a53e0cSJaegeuk Kim  */
198239a53e0cSJaegeuk Kim static inline int F2FS_HAS_BLOCKS(struct inode *inode)
198339a53e0cSJaegeuk Kim {
19840eb0adadSChao Yu 	block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
19850eb0adadSChao Yu 
1986000519f2SChao Yu 	return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block;
198739a53e0cSJaegeuk Kim }
198839a53e0cSJaegeuk Kim 
19894bc8e9bcSChao Yu static inline bool f2fs_has_xattr_block(unsigned int ofs)
19904bc8e9bcSChao Yu {
19914bc8e9bcSChao Yu 	return ofs == XATTR_NODE_OFFSET;
19924bc8e9bcSChao Yu }
19934bc8e9bcSChao Yu 
1994d8a9a229SJaegeuk Kim static inline bool __allow_reserved_blocks(struct f2fs_sb_info *sbi,
1995a90a0884SJaegeuk Kim 					struct inode *inode, bool cap)
19967c2e5963SJaegeuk Kim {
1997d8a9a229SJaegeuk Kim 	if (!inode)
1998d8a9a229SJaegeuk Kim 		return true;
19997c2e5963SJaegeuk Kim 	if (!test_opt(sbi, RESERVE_ROOT))
20007c2e5963SJaegeuk Kim 		return false;
2001d8a9a229SJaegeuk Kim 	if (IS_NOQUOTA(inode))
2002d8a9a229SJaegeuk Kim 		return true;
200363189b78SChao Yu 	if (uid_eq(F2FS_OPTION(sbi).s_resuid, current_fsuid()))
20047c2e5963SJaegeuk Kim 		return true;
200563189b78SChao Yu 	if (!gid_eq(F2FS_OPTION(sbi).s_resgid, GLOBAL_ROOT_GID) &&
200663189b78SChao Yu 					in_group_p(F2FS_OPTION(sbi).s_resgid))
20077c2e5963SJaegeuk Kim 		return true;
2008a90a0884SJaegeuk Kim 	if (cap && capable(CAP_SYS_RESOURCE))
2009162b27aeSJaegeuk Kim 		return true;
20107c2e5963SJaegeuk Kim 	return false;
20117c2e5963SJaegeuk Kim }
20127c2e5963SJaegeuk Kim 
20130abd675eSChao Yu static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool);
20140abd675eSChao Yu static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
201546008c6dSChao Yu 				 struct inode *inode, blkcnt_t *count)
201639a53e0cSJaegeuk Kim {
20170abd675eSChao Yu 	blkcnt_t diff = 0, release = 0;
2018daeb433eSChao Yu 	block_t avail_user_block_count;
20190abd675eSChao Yu 	int ret;
20200abd675eSChao Yu 
20210abd675eSChao Yu 	ret = dquot_reserve_block(inode, *count);
20220abd675eSChao Yu 	if (ret)
20230abd675eSChao Yu 		return ret;
2024dd11a5dfSJaegeuk Kim 
202555523519SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
2026c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_BLOCK);
20270abd675eSChao Yu 		release = *count;
20289ea2f0beSChao Yu 		goto release_quota;
202955523519SChao Yu 	}
20307fa750a1SArnd Bergmann 
2031dd11a5dfSJaegeuk Kim 	/*
2032dd11a5dfSJaegeuk Kim 	 * let's increase this in prior to actual block count change in order
2033dd11a5dfSJaegeuk Kim 	 * for f2fs_sync_file to avoid data races when deciding checkpoint.
2034dd11a5dfSJaegeuk Kim 	 */
2035dd11a5dfSJaegeuk Kim 	percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
203639a53e0cSJaegeuk Kim 
203739a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
20382555a2d5SJaegeuk Kim 	sbi->total_valid_block_count += (block_t)(*count);
203980d42145SYunlong Song 	avail_user_block_count = sbi->user_block_count -
204080d42145SYunlong Song 					sbi->current_reserved_blocks;
20417e65be49SJaegeuk Kim 
2042a90a0884SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode, true))
204363189b78SChao Yu 		avail_user_block_count -= F2FS_OPTION(sbi).root_reserved_blocks;
2044a4c3ecaaSDaniel Rosenberg 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2045a4c3ecaaSDaniel Rosenberg 		if (avail_user_block_count > sbi->unusable_block_count)
20464354994fSDaniel Rosenberg 			avail_user_block_count -= sbi->unusable_block_count;
2047a4c3ecaaSDaniel Rosenberg 		else
2048a4c3ecaaSDaniel Rosenberg 			avail_user_block_count = 0;
2049a4c3ecaaSDaniel Rosenberg 	}
2050daeb433eSChao Yu 	if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
2051daeb433eSChao Yu 		diff = sbi->total_valid_block_count - avail_user_block_count;
20527e65be49SJaegeuk Kim 		if (diff > *count)
20537e65be49SJaegeuk Kim 			diff = *count;
2054dd11a5dfSJaegeuk Kim 		*count -= diff;
20550abd675eSChao Yu 		release = diff;
20567e65be49SJaegeuk Kim 		sbi->total_valid_block_count -= diff;
205746008c6dSChao Yu 		if (!*count) {
205839a53e0cSJaegeuk Kim 			spin_unlock(&sbi->stat_lock);
20590abd675eSChao Yu 			goto enospc;
206039a53e0cSJaegeuk Kim 		}
206146008c6dSChao Yu 	}
206239a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
206341382ec4SJaegeuk Kim 
206436b877afSDaniel Rosenberg 	if (unlikely(release)) {
206536b877afSDaniel Rosenberg 		percpu_counter_sub(&sbi->alloc_valid_block_count, release);
20660abd675eSChao Yu 		dquot_release_reservation_block(inode, release);
206736b877afSDaniel Rosenberg 	}
20680abd675eSChao Yu 	f2fs_i_blocks_write(inode, *count, true, true);
20690abd675eSChao Yu 	return 0;
20700abd675eSChao Yu 
20710abd675eSChao Yu enospc:
207236b877afSDaniel Rosenberg 	percpu_counter_sub(&sbi->alloc_valid_block_count, release);
20739ea2f0beSChao Yu release_quota:
20740abd675eSChao Yu 	dquot_release_reservation_block(inode, release);
20750abd675eSChao Yu 	return -ENOSPC;
207639a53e0cSJaegeuk Kim }
207739a53e0cSJaegeuk Kim 
2078dcbb4c10SJoe Perches __printf(2, 3)
2079dcbb4c10SJoe Perches void f2fs_printk(struct f2fs_sb_info *sbi, const char *fmt, ...);
2080dcbb4c10SJoe Perches 
2081dcbb4c10SJoe Perches #define f2fs_err(sbi, fmt, ...)						\
2082dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_ERR fmt, ##__VA_ARGS__)
2083dcbb4c10SJoe Perches #define f2fs_warn(sbi, fmt, ...)					\
2084dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_WARNING fmt, ##__VA_ARGS__)
2085dcbb4c10SJoe Perches #define f2fs_notice(sbi, fmt, ...)					\
2086dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_NOTICE fmt, ##__VA_ARGS__)
2087dcbb4c10SJoe Perches #define f2fs_info(sbi, fmt, ...)					\
2088dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_INFO fmt, ##__VA_ARGS__)
2089dcbb4c10SJoe Perches #define f2fs_debug(sbi, fmt, ...)					\
2090dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_DEBUG fmt, ##__VA_ARGS__)
2091dcbb4c10SJoe Perches 
2092da19b0dcSGu Zheng static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
209339a53e0cSJaegeuk Kim 						struct inode *inode,
20940eb0adadSChao Yu 						block_t count)
209539a53e0cSJaegeuk Kim {
20960eb0adadSChao Yu 	blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK;
20970eb0adadSChao Yu 
209839a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
20999850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
210039a53e0cSJaegeuk Kim 	sbi->total_valid_block_count -= (block_t)count;
210180d42145SYunlong Song 	if (sbi->reserved_blocks &&
210280d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
210380d42145SYunlong Song 		sbi->current_reserved_blocks = min(sbi->reserved_blocks,
210480d42145SYunlong Song 					sbi->current_reserved_blocks + count);
210539a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
21065e159cd3SChao Yu 	if (unlikely(inode->i_blocks < sectors)) {
2107dcbb4c10SJoe Perches 		f2fs_warn(sbi, "Inconsistent i_blocks, ino:%lu, iblocks:%llu, sectors:%llu",
21085e159cd3SChao Yu 			  inode->i_ino,
21095e159cd3SChao Yu 			  (unsigned long long)inode->i_blocks,
21105e159cd3SChao Yu 			  (unsigned long long)sectors);
21115e159cd3SChao Yu 		set_sbi_flag(sbi, SBI_NEED_FSCK);
21125e159cd3SChao Yu 		return;
21135e159cd3SChao Yu 	}
21140abd675eSChao Yu 	f2fs_i_blocks_write(inode, count, false, true);
211539a53e0cSJaegeuk Kim }
211639a53e0cSJaegeuk Kim 
211739a53e0cSJaegeuk Kim static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
211839a53e0cSJaegeuk Kim {
211935782b23SJaegeuk Kim 	atomic_inc(&sbi->nr_pages[count_type]);
21207c4abcbeSChao Yu 
212120109873SChao Yu 	if (count_type == F2FS_DIRTY_DENTS ||
212220109873SChao Yu 			count_type == F2FS_DIRTY_NODES ||
212320109873SChao Yu 			count_type == F2FS_DIRTY_META ||
212420109873SChao Yu 			count_type == F2FS_DIRTY_QDATA ||
212520109873SChao Yu 			count_type == F2FS_DIRTY_IMETA)
2126caf0047eSChao Yu 		set_sbi_flag(sbi, SBI_IS_DIRTY);
212739a53e0cSJaegeuk Kim }
212839a53e0cSJaegeuk Kim 
2129a7ffdbe2SJaegeuk Kim static inline void inode_inc_dirty_pages(struct inode *inode)
213039a53e0cSJaegeuk Kim {
2131204706c7SJaegeuk Kim 	atomic_inc(&F2FS_I(inode)->dirty_pages);
2132c227f912SChao Yu 	inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
2133c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
21342c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
21352c8a4a28SJaegeuk Kim 		inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
213639a53e0cSJaegeuk Kim }
213739a53e0cSJaegeuk Kim 
213839a53e0cSJaegeuk Kim static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
213939a53e0cSJaegeuk Kim {
214035782b23SJaegeuk Kim 	atomic_dec(&sbi->nr_pages[count_type]);
214139a53e0cSJaegeuk Kim }
214239a53e0cSJaegeuk Kim 
2143a7ffdbe2SJaegeuk Kim static inline void inode_dec_dirty_pages(struct inode *inode)
214439a53e0cSJaegeuk Kim {
21455ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
21465ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
21471fe54f9dSJaegeuk Kim 		return;
21481fe54f9dSJaegeuk Kim 
2149204706c7SJaegeuk Kim 	atomic_dec(&F2FS_I(inode)->dirty_pages);
2150c227f912SChao Yu 	dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
2151c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
21522c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
21532c8a4a28SJaegeuk Kim 		dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
215439a53e0cSJaegeuk Kim }
215539a53e0cSJaegeuk Kim 
2156523be8a6SJaegeuk Kim static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
215739a53e0cSJaegeuk Kim {
215835782b23SJaegeuk Kim 	return atomic_read(&sbi->nr_pages[count_type]);
215939a53e0cSJaegeuk Kim }
216039a53e0cSJaegeuk Kim 
2161204706c7SJaegeuk Kim static inline int get_dirty_pages(struct inode *inode)
2162f8b2c1f9SJaegeuk Kim {
2163204706c7SJaegeuk Kim 	return atomic_read(&F2FS_I(inode)->dirty_pages);
2164f8b2c1f9SJaegeuk Kim }
2165f8b2c1f9SJaegeuk Kim 
21665ac206cfSNamjae Jeon static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
21675ac206cfSNamjae Jeon {
21683519e3f9SChao Yu 	unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
2169523be8a6SJaegeuk Kim 	unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
2170523be8a6SJaegeuk Kim 						sbi->log_blocks_per_seg;
2171523be8a6SJaegeuk Kim 
2172523be8a6SJaegeuk Kim 	return segs / sbi->segs_per_sec;
21735ac206cfSNamjae Jeon }
21745ac206cfSNamjae Jeon 
217539a53e0cSJaegeuk Kim static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
217639a53e0cSJaegeuk Kim {
21778b8343faSJaegeuk Kim 	return sbi->total_valid_block_count;
217839a53e0cSJaegeuk Kim }
217939a53e0cSJaegeuk Kim 
2180f83a2584SYunlei He static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
2181f83a2584SYunlei He {
2182f83a2584SYunlei He 	return sbi->discard_blks;
2183f83a2584SYunlei He }
2184f83a2584SYunlei He 
218539a53e0cSJaegeuk Kim static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
218639a53e0cSJaegeuk Kim {
218739a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
218839a53e0cSJaegeuk Kim 
218939a53e0cSJaegeuk Kim 	/* return NAT or SIT bitmap */
219039a53e0cSJaegeuk Kim 	if (flag == NAT_BITMAP)
219139a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
219239a53e0cSJaegeuk Kim 	else if (flag == SIT_BITMAP)
219339a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
219439a53e0cSJaegeuk Kim 
219539a53e0cSJaegeuk Kim 	return 0;
219639a53e0cSJaegeuk Kim }
219739a53e0cSJaegeuk Kim 
219855141486SWanpeng Li static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
219955141486SWanpeng Li {
220055141486SWanpeng Li 	return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
220155141486SWanpeng Li }
220255141486SWanpeng Li 
220339a53e0cSJaegeuk Kim static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
220439a53e0cSJaegeuk Kim {
220539a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
22061dbe4152SChangman Lee 	int offset;
22071dbe4152SChangman Lee 
2208199bc3feSChao Yu 	if (is_set_ckpt_flags(sbi, CP_LARGE_NAT_BITMAP_FLAG)) {
2209199bc3feSChao Yu 		offset = (flag == SIT_BITMAP) ?
2210199bc3feSChao Yu 			le32_to_cpu(ckpt->nat_ver_bitmap_bytesize) : 0;
2211b471eb99SChao Yu 		/*
2212b471eb99SChao Yu 		 * if large_nat_bitmap feature is enabled, leave checksum
2213b471eb99SChao Yu 		 * protection for all nat/sit bitmaps.
2214b471eb99SChao Yu 		 */
2215b471eb99SChao Yu 		return &ckpt->sit_nat_version_bitmap + offset + sizeof(__le32);
2216199bc3feSChao Yu 	}
2217199bc3feSChao Yu 
221855141486SWanpeng Li 	if (__cp_payload(sbi) > 0) {
22191dbe4152SChangman Lee 		if (flag == NAT_BITMAP)
22201dbe4152SChangman Lee 			return &ckpt->sit_nat_version_bitmap;
22211dbe4152SChangman Lee 		else
222265b85cccSJaegeuk Kim 			return (unsigned char *)ckpt + F2FS_BLKSIZE;
22231dbe4152SChangman Lee 	} else {
22241dbe4152SChangman Lee 		offset = (flag == NAT_BITMAP) ?
222525ca923bSJaegeuk Kim 			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
222639a53e0cSJaegeuk Kim 		return &ckpt->sit_nat_version_bitmap + offset;
222739a53e0cSJaegeuk Kim 	}
22281dbe4152SChangman Lee }
222939a53e0cSJaegeuk Kim 
223039a53e0cSJaegeuk Kim static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
223139a53e0cSJaegeuk Kim {
22328508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
223339a53e0cSJaegeuk Kim 
22348508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 2)
223539a53e0cSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
223639a53e0cSJaegeuk Kim 	return start_addr;
223739a53e0cSJaegeuk Kim }
223839a53e0cSJaegeuk Kim 
22398508e44aSJaegeuk Kim static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
22408508e44aSJaegeuk Kim {
22418508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
22428508e44aSJaegeuk Kim 
22438508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 1)
22448508e44aSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
22458508e44aSJaegeuk Kim 	return start_addr;
22468508e44aSJaegeuk Kim }
22478508e44aSJaegeuk Kim 
22488508e44aSJaegeuk Kim static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
22498508e44aSJaegeuk Kim {
22508508e44aSJaegeuk Kim 	sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
22518508e44aSJaegeuk Kim }
22528508e44aSJaegeuk Kim 
225339a53e0cSJaegeuk Kim static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
225439a53e0cSJaegeuk Kim {
225539a53e0cSJaegeuk Kim 	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
225639a53e0cSJaegeuk Kim }
225739a53e0cSJaegeuk Kim 
22580abd675eSChao Yu static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
2259000519f2SChao Yu 					struct inode *inode, bool is_inode)
226039a53e0cSJaegeuk Kim {
226139a53e0cSJaegeuk Kim 	block_t	valid_block_count;
2262a4c3ecaaSDaniel Rosenberg 	unsigned int valid_node_count, user_block_count;
2263af033b2aSChao Yu 	int err;
22640abd675eSChao Yu 
2265af033b2aSChao Yu 	if (is_inode) {
2266af033b2aSChao Yu 		if (inode) {
2267af033b2aSChao Yu 			err = dquot_alloc_inode(inode);
2268af033b2aSChao Yu 			if (err)
2269af033b2aSChao Yu 				return err;
2270af033b2aSChao Yu 		}
2271af033b2aSChao Yu 	} else {
2272af033b2aSChao Yu 		err = dquot_reserve_block(inode, 1);
2273af033b2aSChao Yu 		if (err)
2274af033b2aSChao Yu 			return err;
22750abd675eSChao Yu 	}
227639a53e0cSJaegeuk Kim 
2277812c6056SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
2278c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_BLOCK);
2279812c6056SChao Yu 		goto enospc;
2280812c6056SChao Yu 	}
2281812c6056SChao Yu 
228239a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
228339a53e0cSJaegeuk Kim 
22847e65be49SJaegeuk Kim 	valid_block_count = sbi->total_valid_block_count +
22857e65be49SJaegeuk Kim 					sbi->current_reserved_blocks + 1;
22867e65be49SJaegeuk Kim 
2287a90a0884SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode, false))
228863189b78SChao Yu 		valid_block_count += F2FS_OPTION(sbi).root_reserved_blocks;
2289a4c3ecaaSDaniel Rosenberg 	user_block_count = sbi->user_block_count;
22904354994fSDaniel Rosenberg 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2291a4c3ecaaSDaniel Rosenberg 		user_block_count -= sbi->unusable_block_count;
22927e65be49SJaegeuk Kim 
2293a4c3ecaaSDaniel Rosenberg 	if (unlikely(valid_block_count > user_block_count)) {
229439a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
22950abd675eSChao Yu 		goto enospc;
229639a53e0cSJaegeuk Kim 	}
229739a53e0cSJaegeuk Kim 
2298ef86d709SGu Zheng 	valid_node_count = sbi->total_valid_node_count + 1;
2299cfb271d4SChao Yu 	if (unlikely(valid_node_count > sbi->total_node_count)) {
230039a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
23010abd675eSChao Yu 		goto enospc;
230239a53e0cSJaegeuk Kim 	}
230339a53e0cSJaegeuk Kim 
2304ef86d709SGu Zheng 	sbi->total_valid_node_count++;
2305ef86d709SGu Zheng 	sbi->total_valid_block_count++;
230639a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
230739a53e0cSJaegeuk Kim 
23080abd675eSChao Yu 	if (inode) {
23090abd675eSChao Yu 		if (is_inode)
23100abd675eSChao Yu 			f2fs_mark_inode_dirty_sync(inode, true);
23110abd675eSChao Yu 		else
23120abd675eSChao Yu 			f2fs_i_blocks_write(inode, 1, true, true);
23130abd675eSChao Yu 	}
23140abd675eSChao Yu 
231541382ec4SJaegeuk Kim 	percpu_counter_inc(&sbi->alloc_valid_block_count);
23160abd675eSChao Yu 	return 0;
23170abd675eSChao Yu 
23180abd675eSChao Yu enospc:
2319af033b2aSChao Yu 	if (is_inode) {
2320af033b2aSChao Yu 		if (inode)
2321af033b2aSChao Yu 			dquot_free_inode(inode);
2322af033b2aSChao Yu 	} else {
23230abd675eSChao Yu 		dquot_release_reservation_block(inode, 1);
2324af033b2aSChao Yu 	}
23250abd675eSChao Yu 	return -ENOSPC;
232639a53e0cSJaegeuk Kim }
232739a53e0cSJaegeuk Kim 
232839a53e0cSJaegeuk Kim static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
2329000519f2SChao Yu 					struct inode *inode, bool is_inode)
233039a53e0cSJaegeuk Kim {
233139a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
233239a53e0cSJaegeuk Kim 
23339850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_block_count);
23349850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_node_count);
233539a53e0cSJaegeuk Kim 
2336ef86d709SGu Zheng 	sbi->total_valid_node_count--;
2337ef86d709SGu Zheng 	sbi->total_valid_block_count--;
233880d42145SYunlong Song 	if (sbi->reserved_blocks &&
233980d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
234080d42145SYunlong Song 		sbi->current_reserved_blocks++;
234139a53e0cSJaegeuk Kim 
234239a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
23430abd675eSChao Yu 
2344ea6d7e72SChao Yu 	if (is_inode) {
2345af033b2aSChao Yu 		dquot_free_inode(inode);
2346ea6d7e72SChao Yu 	} else {
2347ea6d7e72SChao Yu 		if (unlikely(inode->i_blocks == 0)) {
2348097a7686SChao Yu 			f2fs_warn(sbi, "dec_valid_node_count: inconsistent i_blocks, ino:%lu, iblocks:%llu",
2349ea6d7e72SChao Yu 				  inode->i_ino,
2350ea6d7e72SChao Yu 				  (unsigned long long)inode->i_blocks);
2351ea6d7e72SChao Yu 			set_sbi_flag(sbi, SBI_NEED_FSCK);
2352ea6d7e72SChao Yu 			return;
2353ea6d7e72SChao Yu 		}
23540abd675eSChao Yu 		f2fs_i_blocks_write(inode, 1, false, true);
235539a53e0cSJaegeuk Kim 	}
2356ea6d7e72SChao Yu }
235739a53e0cSJaegeuk Kim 
235839a53e0cSJaegeuk Kim static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
235939a53e0cSJaegeuk Kim {
23608b8343faSJaegeuk Kim 	return sbi->total_valid_node_count;
236139a53e0cSJaegeuk Kim }
236239a53e0cSJaegeuk Kim 
236339a53e0cSJaegeuk Kim static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
236439a53e0cSJaegeuk Kim {
2365513c5f37SJaegeuk Kim 	percpu_counter_inc(&sbi->total_valid_inode_count);
236639a53e0cSJaegeuk Kim }
236739a53e0cSJaegeuk Kim 
23680e80220aSJaegeuk Kim static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
236939a53e0cSJaegeuk Kim {
2370513c5f37SJaegeuk Kim 	percpu_counter_dec(&sbi->total_valid_inode_count);
237139a53e0cSJaegeuk Kim }
237239a53e0cSJaegeuk Kim 
2373513c5f37SJaegeuk Kim static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
237439a53e0cSJaegeuk Kim {
2375513c5f37SJaegeuk Kim 	return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
237639a53e0cSJaegeuk Kim }
237739a53e0cSJaegeuk Kim 
2378a56c7c6fSJaegeuk Kim static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
2379a56c7c6fSJaegeuk Kim 						pgoff_t index, bool for_write)
2380a56c7c6fSJaegeuk Kim {
238182cf4f13SChao Yu 	struct page *page;
2382cac5a3d8SDongOh Shin 
23837fa750a1SArnd Bergmann 	if (IS_ENABLED(CONFIG_F2FS_FAULT_INJECTION)) {
238482cf4f13SChao Yu 		if (!for_write)
238582cf4f13SChao Yu 			page = find_get_page_flags(mapping, index,
238682cf4f13SChao Yu 							FGP_LOCK | FGP_ACCESSED);
238782cf4f13SChao Yu 		else
238882cf4f13SChao Yu 			page = find_lock_page(mapping, index);
2389c41f3cc3SJaegeuk Kim 		if (page)
2390c41f3cc3SJaegeuk Kim 			return page;
2391c41f3cc3SJaegeuk Kim 
239255523519SChao Yu 		if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) {
2393c45d6002SChao Yu 			f2fs_show_injection_info(F2FS_M_SB(mapping),
2394c45d6002SChao Yu 							FAULT_PAGE_ALLOC);
2395c41f3cc3SJaegeuk Kim 			return NULL;
239655523519SChao Yu 		}
23977fa750a1SArnd Bergmann 	}
23987fa750a1SArnd Bergmann 
2399a56c7c6fSJaegeuk Kim 	if (!for_write)
2400a56c7c6fSJaegeuk Kim 		return grab_cache_page(mapping, index);
2401a56c7c6fSJaegeuk Kim 	return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
2402a56c7c6fSJaegeuk Kim }
2403a56c7c6fSJaegeuk Kim 
240401eccef7SChao Yu static inline struct page *f2fs_pagecache_get_page(
240501eccef7SChao Yu 				struct address_space *mapping, pgoff_t index,
240601eccef7SChao Yu 				int fgp_flags, gfp_t gfp_mask)
240701eccef7SChao Yu {
240801eccef7SChao Yu 	if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_GET)) {
2409c45d6002SChao Yu 		f2fs_show_injection_info(F2FS_M_SB(mapping), FAULT_PAGE_GET);
241001eccef7SChao Yu 		return NULL;
241101eccef7SChao Yu 	}
24127fa750a1SArnd Bergmann 
241301eccef7SChao Yu 	return pagecache_get_page(mapping, index, fgp_flags, gfp_mask);
241401eccef7SChao Yu }
241501eccef7SChao Yu 
24166e2c64adSJaegeuk Kim static inline void f2fs_copy_page(struct page *src, struct page *dst)
24176e2c64adSJaegeuk Kim {
24186e2c64adSJaegeuk Kim 	char *src_kaddr = kmap(src);
24196e2c64adSJaegeuk Kim 	char *dst_kaddr = kmap(dst);
24206e2c64adSJaegeuk Kim 
24216e2c64adSJaegeuk Kim 	memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
24226e2c64adSJaegeuk Kim 	kunmap(dst);
24236e2c64adSJaegeuk Kim 	kunmap(src);
24246e2c64adSJaegeuk Kim }
24256e2c64adSJaegeuk Kim 
242639a53e0cSJaegeuk Kim static inline void f2fs_put_page(struct page *page, int unlock)
242739a53e0cSJaegeuk Kim {
2428031fa8ccSJaegeuk Kim 	if (!page)
242939a53e0cSJaegeuk Kim 		return;
243039a53e0cSJaegeuk Kim 
243139a53e0cSJaegeuk Kim 	if (unlock) {
24329850cf4aSJaegeuk Kim 		f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
243339a53e0cSJaegeuk Kim 		unlock_page(page);
243439a53e0cSJaegeuk Kim 	}
243509cbfeafSKirill A. Shutemov 	put_page(page);
243639a53e0cSJaegeuk Kim }
243739a53e0cSJaegeuk Kim 
243839a53e0cSJaegeuk Kim static inline void f2fs_put_dnode(struct dnode_of_data *dn)
243939a53e0cSJaegeuk Kim {
244039a53e0cSJaegeuk Kim 	if (dn->node_page)
244139a53e0cSJaegeuk Kim 		f2fs_put_page(dn->node_page, 1);
244239a53e0cSJaegeuk Kim 	if (dn->inode_page && dn->node_page != dn->inode_page)
244339a53e0cSJaegeuk Kim 		f2fs_put_page(dn->inode_page, 0);
244439a53e0cSJaegeuk Kim 	dn->node_page = NULL;
244539a53e0cSJaegeuk Kim 	dn->inode_page = NULL;
244639a53e0cSJaegeuk Kim }
244739a53e0cSJaegeuk Kim 
244839a53e0cSJaegeuk Kim static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
2449e8512d2eSGu Zheng 					size_t size)
245039a53e0cSJaegeuk Kim {
2451e8512d2eSGu Zheng 	return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
245239a53e0cSJaegeuk Kim }
245339a53e0cSJaegeuk Kim 
24547bd59381SGu Zheng static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
24557bd59381SGu Zheng 						gfp_t flags)
24567bd59381SGu Zheng {
24577bd59381SGu Zheng 	void *entry;
24587bd59381SGu Zheng 
245980c54505SJaegeuk Kim 	entry = kmem_cache_alloc(cachep, flags);
246080c54505SJaegeuk Kim 	if (!entry)
246180c54505SJaegeuk Kim 		entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
24627bd59381SGu Zheng 	return entry;
24637bd59381SGu Zheng }
24647bd59381SGu Zheng 
24655f9abab4SJaegeuk Kim static inline bool is_idle(struct f2fs_sb_info *sbi, int type)
24665f9abab4SJaegeuk Kim {
24670e5e8111SDaeho Jeong 	if (sbi->gc_mode == GC_URGENT_HIGH)
2468f7dfd9f3SPark Ju Hyung 		return true;
2469f7dfd9f3SPark Ju Hyung 
24705f9abab4SJaegeuk Kim 	if (get_pages(sbi, F2FS_RD_DATA) || get_pages(sbi, F2FS_RD_NODE) ||
24715f9abab4SJaegeuk Kim 		get_pages(sbi, F2FS_RD_META) || get_pages(sbi, F2FS_WB_DATA) ||
2472fef4129eSChao Yu 		get_pages(sbi, F2FS_WB_CP_DATA) ||
2473fef4129eSChao Yu 		get_pages(sbi, F2FS_DIO_READ) ||
247411ac8ef8SJaegeuk Kim 		get_pages(sbi, F2FS_DIO_WRITE))
247511ac8ef8SJaegeuk Kim 		return false;
247611ac8ef8SJaegeuk Kim 
247756659ce8SSahitya Tummala 	if (type != DISCARD_TIME && SM_I(sbi) && SM_I(sbi)->dcc_info &&
247811ac8ef8SJaegeuk Kim 			atomic_read(&SM_I(sbi)->dcc_info->queued_discard))
247911ac8ef8SJaegeuk Kim 		return false;
248011ac8ef8SJaegeuk Kim 
248111ac8ef8SJaegeuk Kim 	if (SM_I(sbi) && SM_I(sbi)->fcc_info &&
248272691af6SJaegeuk Kim 			atomic_read(&SM_I(sbi)->fcc_info->queued_flush))
24835f9abab4SJaegeuk Kim 		return false;
248411ac8ef8SJaegeuk Kim 
24850e5e8111SDaeho Jeong 	if (sbi->gc_mode == GC_URGENT_LOW &&
24860e5e8111SDaeho Jeong 			(type == DISCARD_TIME || type == GC_TIME))
24870e5e8111SDaeho Jeong 		return true;
24880e5e8111SDaeho Jeong 
24895f9abab4SJaegeuk Kim 	return f2fs_time_over(sbi, type);
24905f9abab4SJaegeuk Kim }
24915f9abab4SJaegeuk Kim 
24929be32d72SJaegeuk Kim static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
24939be32d72SJaegeuk Kim 				unsigned long index, void *item)
24949be32d72SJaegeuk Kim {
24959be32d72SJaegeuk Kim 	while (radix_tree_insert(root, index, item))
24969be32d72SJaegeuk Kim 		cond_resched();
24979be32d72SJaegeuk Kim }
24989be32d72SJaegeuk Kim 
249939a53e0cSJaegeuk Kim #define RAW_IS_INODE(p)	((p)->footer.nid == (p)->footer.ino)
250039a53e0cSJaegeuk Kim 
250139a53e0cSJaegeuk Kim static inline bool IS_INODE(struct page *page)
250239a53e0cSJaegeuk Kim {
250345590710SGu Zheng 	struct f2fs_node *p = F2FS_NODE(page);
2504cac5a3d8SDongOh Shin 
250539a53e0cSJaegeuk Kim 	return RAW_IS_INODE(p);
250639a53e0cSJaegeuk Kim }
250739a53e0cSJaegeuk Kim 
25087a2af766SChao Yu static inline int offset_in_addr(struct f2fs_inode *i)
25097a2af766SChao Yu {
25107a2af766SChao Yu 	return (i->i_inline & F2FS_EXTRA_ATTR) ?
25117a2af766SChao Yu 			(le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0;
25127a2af766SChao Yu }
25137a2af766SChao Yu 
251439a53e0cSJaegeuk Kim static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
251539a53e0cSJaegeuk Kim {
251639a53e0cSJaegeuk Kim 	return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
251739a53e0cSJaegeuk Kim }
251839a53e0cSJaegeuk Kim 
25197a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode);
2520a2ced1ceSChao Yu static inline block_t data_blkaddr(struct inode *inode,
25217a2af766SChao Yu 			struct page *node_page, unsigned int offset)
252239a53e0cSJaegeuk Kim {
252339a53e0cSJaegeuk Kim 	struct f2fs_node *raw_node;
252439a53e0cSJaegeuk Kim 	__le32 *addr_array;
25257a2af766SChao Yu 	int base = 0;
25267a2af766SChao Yu 	bool is_inode = IS_INODE(node_page);
2527cac5a3d8SDongOh Shin 
252845590710SGu Zheng 	raw_node = F2FS_NODE(node_page);
25297a2af766SChao Yu 
2530979f492fSLiFan 	if (is_inode) {
2531979f492fSLiFan 		if (!inode)
25327a88ddb5SChao Yu 			/* from GC path only */
25337a2af766SChao Yu 			base = offset_in_addr(&raw_node->i);
2534979f492fSLiFan 		else if (f2fs_has_extra_attr(inode))
25357a2af766SChao Yu 			base = get_extra_isize(inode);
25367a2af766SChao Yu 	}
25377a2af766SChao Yu 
253839a53e0cSJaegeuk Kim 	addr_array = blkaddr_in_node(raw_node);
25397a2af766SChao Yu 	return le32_to_cpu(addr_array[base + offset]);
254039a53e0cSJaegeuk Kim }
254139a53e0cSJaegeuk Kim 
2542a2ced1ceSChao Yu static inline block_t f2fs_data_blkaddr(struct dnode_of_data *dn)
2543a2ced1ceSChao Yu {
2544a2ced1ceSChao Yu 	return data_blkaddr(dn->inode, dn->node_page, dn->ofs_in_node);
2545a2ced1ceSChao Yu }
2546a2ced1ceSChao Yu 
254739a53e0cSJaegeuk Kim static inline int f2fs_test_bit(unsigned int nr, char *addr)
254839a53e0cSJaegeuk Kim {
254939a53e0cSJaegeuk Kim 	int mask;
255039a53e0cSJaegeuk Kim 
255139a53e0cSJaegeuk Kim 	addr += (nr >> 3);
255239a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
255339a53e0cSJaegeuk Kim 	return mask & *addr;
255439a53e0cSJaegeuk Kim }
255539a53e0cSJaegeuk Kim 
2556a66cdd98SJaegeuk Kim static inline void f2fs_set_bit(unsigned int nr, char *addr)
2557a66cdd98SJaegeuk Kim {
2558a66cdd98SJaegeuk Kim 	int mask;
2559a66cdd98SJaegeuk Kim 
2560a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2561a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2562a66cdd98SJaegeuk Kim 	*addr |= mask;
2563a66cdd98SJaegeuk Kim }
2564a66cdd98SJaegeuk Kim 
2565a66cdd98SJaegeuk Kim static inline void f2fs_clear_bit(unsigned int nr, char *addr)
2566a66cdd98SJaegeuk Kim {
2567a66cdd98SJaegeuk Kim 	int mask;
2568a66cdd98SJaegeuk Kim 
2569a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2570a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2571a66cdd98SJaegeuk Kim 	*addr &= ~mask;
2572a66cdd98SJaegeuk Kim }
2573a66cdd98SJaegeuk Kim 
257452aca074SGu Zheng static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
257539a53e0cSJaegeuk Kim {
257639a53e0cSJaegeuk Kim 	int mask;
257739a53e0cSJaegeuk Kim 	int ret;
257839a53e0cSJaegeuk Kim 
257939a53e0cSJaegeuk Kim 	addr += (nr >> 3);
258039a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
258139a53e0cSJaegeuk Kim 	ret = mask & *addr;
258239a53e0cSJaegeuk Kim 	*addr |= mask;
258339a53e0cSJaegeuk Kim 	return ret;
258439a53e0cSJaegeuk Kim }
258539a53e0cSJaegeuk Kim 
258652aca074SGu Zheng static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
258739a53e0cSJaegeuk Kim {
258839a53e0cSJaegeuk Kim 	int mask;
258939a53e0cSJaegeuk Kim 	int ret;
259039a53e0cSJaegeuk Kim 
259139a53e0cSJaegeuk Kim 	addr += (nr >> 3);
259239a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
259339a53e0cSJaegeuk Kim 	ret = mask & *addr;
259439a53e0cSJaegeuk Kim 	*addr &= ~mask;
259539a53e0cSJaegeuk Kim 	return ret;
259639a53e0cSJaegeuk Kim }
259739a53e0cSJaegeuk Kim 
2598c6ac4c0eSGu Zheng static inline void f2fs_change_bit(unsigned int nr, char *addr)
2599c6ac4c0eSGu Zheng {
2600c6ac4c0eSGu Zheng 	int mask;
2601c6ac4c0eSGu Zheng 
2602c6ac4c0eSGu Zheng 	addr += (nr >> 3);
2603c6ac4c0eSGu Zheng 	mask = 1 << (7 - (nr & 0x07));
2604c6ac4c0eSGu Zheng 	*addr ^= mask;
2605c6ac4c0eSGu Zheng }
2606c6ac4c0eSGu Zheng 
260759c84408SChao Yu /*
260836098557SEric Biggers  * On-disk inode flags (f2fs_inode::i_flags)
260959c84408SChao Yu  */
26104c8ff709SChao Yu #define F2FS_COMPR_FL			0x00000004 /* Compress file */
261159c84408SChao Yu #define F2FS_SYNC_FL			0x00000008 /* Synchronous updates */
261259c84408SChao Yu #define F2FS_IMMUTABLE_FL		0x00000010 /* Immutable file */
261359c84408SChao Yu #define F2FS_APPEND_FL			0x00000020 /* writes to file may only append */
261459c84408SChao Yu #define F2FS_NODUMP_FL			0x00000040 /* do not dump file */
261559c84408SChao Yu #define F2FS_NOATIME_FL			0x00000080 /* do not update atime */
26164c8ff709SChao Yu #define F2FS_NOCOMP_FL			0x00000400 /* Don't compress */
261759c84408SChao Yu #define F2FS_INDEX_FL			0x00001000 /* hash-indexed directory */
261859c84408SChao Yu #define F2FS_DIRSYNC_FL			0x00010000 /* dirsync behaviour (directories only) */
261959c84408SChao Yu #define F2FS_PROJINHERIT_FL		0x20000000 /* Create with parents projid */
26202c2eb7a3SDaniel Rosenberg #define F2FS_CASEFOLD_FL		0x40000000 /* Casefolded file */
262159c84408SChao Yu 
262259c84408SChao Yu /* Flags that should be inherited by new inodes from their parent. */
262336098557SEric Biggers #define F2FS_FL_INHERITED (F2FS_SYNC_FL | F2FS_NODUMP_FL | F2FS_NOATIME_FL | \
26242c2eb7a3SDaniel Rosenberg 			   F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL | \
26254c8ff709SChao Yu 			   F2FS_CASEFOLD_FL | F2FS_COMPR_FL | F2FS_NOCOMP_FL)
262659c84408SChao Yu 
262759c84408SChao Yu /* Flags that are appropriate for regular files (all but dir-specific ones). */
26282c2eb7a3SDaniel Rosenberg #define F2FS_REG_FLMASK		(~(F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL | \
26292c2eb7a3SDaniel Rosenberg 				F2FS_CASEFOLD_FL))
263059c84408SChao Yu 
263159c84408SChao Yu /* Flags that are appropriate for non-directories/regular files. */
263259c84408SChao Yu #define F2FS_OTHER_FLMASK	(F2FS_NODUMP_FL | F2FS_NOATIME_FL)
26335c57132eSChao Yu 
26345c57132eSChao Yu static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
26355c57132eSChao Yu {
26365c57132eSChao Yu 	if (S_ISDIR(mode))
26375c57132eSChao Yu 		return flags;
26385c57132eSChao Yu 	else if (S_ISREG(mode))
26395c57132eSChao Yu 		return flags & F2FS_REG_FLMASK;
26405c57132eSChao Yu 	else
26415c57132eSChao Yu 		return flags & F2FS_OTHER_FLMASK;
26425c57132eSChao Yu }
26435c57132eSChao Yu 
2644205b9822SJaegeuk Kim static inline void __mark_inode_dirty_flag(struct inode *inode,
2645205b9822SJaegeuk Kim 						int flag, bool set)
264639a53e0cSJaegeuk Kim {
2647205b9822SJaegeuk Kim 	switch (flag) {
2648205b9822SJaegeuk Kim 	case FI_INLINE_XATTR:
2649205b9822SJaegeuk Kim 	case FI_INLINE_DATA:
2650205b9822SJaegeuk Kim 	case FI_INLINE_DENTRY:
26519ac1e2d8SDaeho Jeong 	case FI_NEW_INODE:
2652205b9822SJaegeuk Kim 		if (set)
2653205b9822SJaegeuk Kim 			return;
2654df561f66SGustavo A. R. Silva 		fallthrough;
2655205b9822SJaegeuk Kim 	case FI_DATA_EXIST:
2656205b9822SJaegeuk Kim 	case FI_INLINE_DOTS:
26571ad71a27SJaegeuk Kim 	case FI_PIN_FILE:
26587c45729aSJaegeuk Kim 		f2fs_mark_inode_dirty_sync(inode, true);
2659205b9822SJaegeuk Kim 	}
266039a53e0cSJaegeuk Kim }
266139a53e0cSJaegeuk Kim 
266291942321SJaegeuk Kim static inline void set_inode_flag(struct inode *inode, int flag)
266339a53e0cSJaegeuk Kim {
266458f7e00fSYufen Yu 	set_bit(flag, F2FS_I(inode)->flags);
2665205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, true);
266639a53e0cSJaegeuk Kim }
266739a53e0cSJaegeuk Kim 
266891942321SJaegeuk Kim static inline int is_inode_flag_set(struct inode *inode, int flag)
266939a53e0cSJaegeuk Kim {
26707653b9d8SChao Yu 	return test_bit(flag, F2FS_I(inode)->flags);
267139a53e0cSJaegeuk Kim }
267239a53e0cSJaegeuk Kim 
267391942321SJaegeuk Kim static inline void clear_inode_flag(struct inode *inode, int flag)
267439a53e0cSJaegeuk Kim {
267558f7e00fSYufen Yu 	clear_bit(flag, F2FS_I(inode)->flags);
2676205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, false);
267739a53e0cSJaegeuk Kim }
267839a53e0cSJaegeuk Kim 
267995ae251fSEric Biggers static inline bool f2fs_verity_in_progress(struct inode *inode)
268095ae251fSEric Biggers {
268195ae251fSEric Biggers 	return IS_ENABLED(CONFIG_FS_VERITY) &&
268295ae251fSEric Biggers 	       is_inode_flag_set(inode, FI_VERITY_IN_PROGRESS);
268395ae251fSEric Biggers }
268495ae251fSEric Biggers 
268591942321SJaegeuk Kim static inline void set_acl_inode(struct inode *inode, umode_t mode)
2686444c580fSJaegeuk Kim {
268791942321SJaegeuk Kim 	F2FS_I(inode)->i_acl_mode = mode;
268891942321SJaegeuk Kim 	set_inode_flag(inode, FI_ACL_MODE);
26897c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, false);
269039a53e0cSJaegeuk Kim }
269139a53e0cSJaegeuk Kim 
2692a1961246SJaegeuk Kim static inline void f2fs_i_links_write(struct inode *inode, bool inc)
2693a1961246SJaegeuk Kim {
2694a1961246SJaegeuk Kim 	if (inc)
2695a1961246SJaegeuk Kim 		inc_nlink(inode);
2696a1961246SJaegeuk Kim 	else
2697a1961246SJaegeuk Kim 		drop_nlink(inode);
26987c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2699a1961246SJaegeuk Kim }
2700a1961246SJaegeuk Kim 
27018edd03c8SJaegeuk Kim static inline void f2fs_i_blocks_write(struct inode *inode,
27020abd675eSChao Yu 					block_t diff, bool add, bool claim)
27038edd03c8SJaegeuk Kim {
270426de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
270526de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
270626de9b11SJaegeuk Kim 
27070abd675eSChao Yu 	/* add = 1, claim = 1 should be dquot_reserve_block in pair */
27080abd675eSChao Yu 	if (add) {
27090abd675eSChao Yu 		if (claim)
27100abd675eSChao Yu 			dquot_claim_block(inode, diff);
27110abd675eSChao Yu 		else
27120abd675eSChao Yu 			dquot_alloc_block_nofail(inode, diff);
27130abd675eSChao Yu 	} else {
27140abd675eSChao Yu 		dquot_free_block(inode, diff);
27150abd675eSChao Yu 	}
27160abd675eSChao Yu 
27177c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
271826de9b11SJaegeuk Kim 	if (clean || recover)
271926de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
27208edd03c8SJaegeuk Kim }
27218edd03c8SJaegeuk Kim 
2722fc9581c8SJaegeuk Kim static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
2723fc9581c8SJaegeuk Kim {
272426de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
272526de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
272626de9b11SJaegeuk Kim 
2727fc9581c8SJaegeuk Kim 	if (i_size_read(inode) == i_size)
2728fc9581c8SJaegeuk Kim 		return;
2729fc9581c8SJaegeuk Kim 
2730fc9581c8SJaegeuk Kim 	i_size_write(inode, i_size);
27317c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
273226de9b11SJaegeuk Kim 	if (clean || recover)
273326de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
273426de9b11SJaegeuk Kim }
273526de9b11SJaegeuk Kim 
2736205b9822SJaegeuk Kim static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
2737205b9822SJaegeuk Kim {
2738205b9822SJaegeuk Kim 	F2FS_I(inode)->i_current_depth = depth;
27397c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2740205b9822SJaegeuk Kim }
2741205b9822SJaegeuk Kim 
27421ad71a27SJaegeuk Kim static inline void f2fs_i_gc_failures_write(struct inode *inode,
27431ad71a27SJaegeuk Kim 					unsigned int count)
27441ad71a27SJaegeuk Kim {
27452ef79ecbSChao Yu 	F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN] = count;
27461ad71a27SJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
27471ad71a27SJaegeuk Kim }
27481ad71a27SJaegeuk Kim 
2749205b9822SJaegeuk Kim static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
2750205b9822SJaegeuk Kim {
2751205b9822SJaegeuk Kim 	F2FS_I(inode)->i_xattr_nid = xnid;
27527c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2753205b9822SJaegeuk Kim }
2754205b9822SJaegeuk Kim 
2755205b9822SJaegeuk Kim static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
2756205b9822SJaegeuk Kim {
2757205b9822SJaegeuk Kim 	F2FS_I(inode)->i_pino = pino;
27587c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2759205b9822SJaegeuk Kim }
2760205b9822SJaegeuk Kim 
276191942321SJaegeuk Kim static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
2762444c580fSJaegeuk Kim {
2763205b9822SJaegeuk Kim 	struct f2fs_inode_info *fi = F2FS_I(inode);
2764205b9822SJaegeuk Kim 
2765444c580fSJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_XATTR)
27667653b9d8SChao Yu 		set_bit(FI_INLINE_XATTR, fi->flags);
27671001b347SHuajun Li 	if (ri->i_inline & F2FS_INLINE_DATA)
27687653b9d8SChao Yu 		set_bit(FI_INLINE_DATA, fi->flags);
276934d67debSChao Yu 	if (ri->i_inline & F2FS_INLINE_DENTRY)
27707653b9d8SChao Yu 		set_bit(FI_INLINE_DENTRY, fi->flags);
2771b3d208f9SJaegeuk Kim 	if (ri->i_inline & F2FS_DATA_EXIST)
27727653b9d8SChao Yu 		set_bit(FI_DATA_EXIST, fi->flags);
2773510022a8SJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_DOTS)
27747653b9d8SChao Yu 		set_bit(FI_INLINE_DOTS, fi->flags);
27757a2af766SChao Yu 	if (ri->i_inline & F2FS_EXTRA_ATTR)
27767653b9d8SChao Yu 		set_bit(FI_EXTRA_ATTR, fi->flags);
27771ad71a27SJaegeuk Kim 	if (ri->i_inline & F2FS_PIN_FILE)
27787653b9d8SChao Yu 		set_bit(FI_PIN_FILE, fi->flags);
2779444c580fSJaegeuk Kim }
2780444c580fSJaegeuk Kim 
278191942321SJaegeuk Kim static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
2782444c580fSJaegeuk Kim {
2783444c580fSJaegeuk Kim 	ri->i_inline = 0;
2784444c580fSJaegeuk Kim 
278591942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_XATTR))
2786444c580fSJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_XATTR;
278791942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DATA))
27881001b347SHuajun Li 		ri->i_inline |= F2FS_INLINE_DATA;
278991942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
279034d67debSChao Yu 		ri->i_inline |= F2FS_INLINE_DENTRY;
279191942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_DATA_EXIST))
2792b3d208f9SJaegeuk Kim 		ri->i_inline |= F2FS_DATA_EXIST;
279391942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DOTS))
2794510022a8SJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_DOTS;
27957a2af766SChao Yu 	if (is_inode_flag_set(inode, FI_EXTRA_ATTR))
27967a2af766SChao Yu 		ri->i_inline |= F2FS_EXTRA_ATTR;
27971ad71a27SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_PIN_FILE))
27981ad71a27SJaegeuk Kim 		ri->i_inline |= F2FS_PIN_FILE;
27997a2af766SChao Yu }
28007a2af766SChao Yu 
28017a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode)
28027a2af766SChao Yu {
28037a2af766SChao Yu 	return is_inode_flag_set(inode, FI_EXTRA_ATTR);
2804444c580fSJaegeuk Kim }
2805444c580fSJaegeuk Kim 
2806987c7c31SChao Yu static inline int f2fs_has_inline_xattr(struct inode *inode)
2807987c7c31SChao Yu {
280891942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_XATTR);
2809987c7c31SChao Yu }
2810987c7c31SChao Yu 
28114c8ff709SChao Yu static inline int f2fs_compressed_file(struct inode *inode)
28124c8ff709SChao Yu {
28134c8ff709SChao Yu 	return S_ISREG(inode->i_mode) &&
28144c8ff709SChao Yu 		is_inode_flag_set(inode, FI_COMPRESSED_FILE);
28154c8ff709SChao Yu }
28164c8ff709SChao Yu 
281781ca7350SChao Yu static inline unsigned int addrs_per_inode(struct inode *inode)
2818de93653fSJaegeuk Kim {
2819d02a6e61SChao Yu 	unsigned int addrs = CUR_ADDRS_PER_INODE(inode) -
2820d02a6e61SChao Yu 				get_inline_xattr_addrs(inode);
28214c8ff709SChao Yu 
28224c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
28234c8ff709SChao Yu 		return addrs;
28244c8ff709SChao Yu 	return ALIGN_DOWN(addrs, F2FS_I(inode)->i_cluster_size);
2825d02a6e61SChao Yu }
2826d02a6e61SChao Yu 
2827d02a6e61SChao Yu static inline unsigned int addrs_per_block(struct inode *inode)
2828d02a6e61SChao Yu {
28294c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
28304c8ff709SChao Yu 		return DEF_ADDRS_PER_BLOCK;
28314c8ff709SChao Yu 	return ALIGN_DOWN(DEF_ADDRS_PER_BLOCK, F2FS_I(inode)->i_cluster_size);
2832de93653fSJaegeuk Kim }
2833de93653fSJaegeuk Kim 
28346afc662eSChao Yu static inline void *inline_xattr_addr(struct inode *inode, struct page *page)
283565985d93SJaegeuk Kim {
2836695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
2837cac5a3d8SDongOh Shin 
283865985d93SJaegeuk Kim 	return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
2839b323fd28SChao Yu 					get_inline_xattr_addrs(inode)]);
284065985d93SJaegeuk Kim }
284165985d93SJaegeuk Kim 
284265985d93SJaegeuk Kim static inline int inline_xattr_size(struct inode *inode)
284365985d93SJaegeuk Kim {
2844622927f3SChao Yu 	if (f2fs_has_inline_xattr(inode))
28456afc662eSChao Yu 		return get_inline_xattr_addrs(inode) * sizeof(__le32);
2846622927f3SChao Yu 	return 0;
284765985d93SJaegeuk Kim }
284865985d93SJaegeuk Kim 
28490dbdc2aeSJaegeuk Kim static inline int f2fs_has_inline_data(struct inode *inode)
28500dbdc2aeSJaegeuk Kim {
285191942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DATA);
28520dbdc2aeSJaegeuk Kim }
28530dbdc2aeSJaegeuk Kim 
2854b3d208f9SJaegeuk Kim static inline int f2fs_exist_data(struct inode *inode)
2855b3d208f9SJaegeuk Kim {
285691942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DATA_EXIST);
2857b3d208f9SJaegeuk Kim }
2858b3d208f9SJaegeuk Kim 
2859510022a8SJaegeuk Kim static inline int f2fs_has_inline_dots(struct inode *inode)
2860510022a8SJaegeuk Kim {
286191942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DOTS);
2862510022a8SJaegeuk Kim }
2863510022a8SJaegeuk Kim 
28644c8ff709SChao Yu static inline int f2fs_is_mmap_file(struct inode *inode)
28654c8ff709SChao Yu {
28664c8ff709SChao Yu 	return is_inode_flag_set(inode, FI_MMAP_FILE);
28674c8ff709SChao Yu }
28684c8ff709SChao Yu 
28691ad71a27SJaegeuk Kim static inline bool f2fs_is_pinned_file(struct inode *inode)
28701ad71a27SJaegeuk Kim {
28711ad71a27SJaegeuk Kim 	return is_inode_flag_set(inode, FI_PIN_FILE);
28721ad71a27SJaegeuk Kim }
28731ad71a27SJaegeuk Kim 
287488b88a66SJaegeuk Kim static inline bool f2fs_is_atomic_file(struct inode *inode)
287588b88a66SJaegeuk Kim {
287691942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_ATOMIC_FILE);
287788b88a66SJaegeuk Kim }
287888b88a66SJaegeuk Kim 
28795fe45743SChao Yu static inline bool f2fs_is_commit_atomic_write(struct inode *inode)
28805fe45743SChao Yu {
28815fe45743SChao Yu 	return is_inode_flag_set(inode, FI_ATOMIC_COMMIT);
28825fe45743SChao Yu }
28835fe45743SChao Yu 
288402a1335fSJaegeuk Kim static inline bool f2fs_is_volatile_file(struct inode *inode)
288502a1335fSJaegeuk Kim {
288691942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_VOLATILE_FILE);
288702a1335fSJaegeuk Kim }
288802a1335fSJaegeuk Kim 
28893c6c2bebSJaegeuk Kim static inline bool f2fs_is_first_block_written(struct inode *inode)
28903c6c2bebSJaegeuk Kim {
289191942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
28923c6c2bebSJaegeuk Kim }
28933c6c2bebSJaegeuk Kim 
28941e84371fSJaegeuk Kim static inline bool f2fs_is_drop_cache(struct inode *inode)
28951e84371fSJaegeuk Kim {
289691942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DROP_CACHE);
28971e84371fSJaegeuk Kim }
28981e84371fSJaegeuk Kim 
2899f2470371SChao Yu static inline void *inline_data_addr(struct inode *inode, struct page *page)
29001001b347SHuajun Li {
2901695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
29027a2af766SChao Yu 	int extra_size = get_extra_isize(inode);
2903cac5a3d8SDongOh Shin 
29047a2af766SChao Yu 	return (void *)&(ri->i_addr[extra_size + DEF_INLINE_RESERVED_SIZE]);
29051001b347SHuajun Li }
29061001b347SHuajun Li 
290734d67debSChao Yu static inline int f2fs_has_inline_dentry(struct inode *inode)
290834d67debSChao Yu {
290991942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DENTRY);
291034d67debSChao Yu }
291134d67debSChao Yu 
2912b5492af7SJaegeuk Kim static inline int is_file(struct inode *inode, int type)
2913b5492af7SJaegeuk Kim {
2914b5492af7SJaegeuk Kim 	return F2FS_I(inode)->i_advise & type;
2915b5492af7SJaegeuk Kim }
2916b5492af7SJaegeuk Kim 
2917b5492af7SJaegeuk Kim static inline void set_file(struct inode *inode, int type)
2918b5492af7SJaegeuk Kim {
2919b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise |= type;
29207c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2921b5492af7SJaegeuk Kim }
2922b5492af7SJaegeuk Kim 
2923b5492af7SJaegeuk Kim static inline void clear_file(struct inode *inode, int type)
2924b5492af7SJaegeuk Kim {
2925b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise &= ~type;
29267c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2927b5492af7SJaegeuk Kim }
2928b5492af7SJaegeuk Kim 
2929fe1897eaSChao Yu static inline bool f2fs_is_time_consistent(struct inode *inode)
2930fe1897eaSChao Yu {
2931fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time, &inode->i_atime))
2932fe1897eaSChao Yu 		return false;
2933fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 1, &inode->i_ctime))
2934fe1897eaSChao Yu 		return false;
2935fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 2, &inode->i_mtime))
2936fe1897eaSChao Yu 		return false;
2937fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 3,
2938fe1897eaSChao Yu 						&F2FS_I(inode)->i_crtime))
2939fe1897eaSChao Yu 		return false;
2940fe1897eaSChao Yu 	return true;
2941fe1897eaSChao Yu }
2942fe1897eaSChao Yu 
294326787236SJaegeuk Kim static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
294426787236SJaegeuk Kim {
2945a0d00fadSChao Yu 	bool ret;
2946a0d00fadSChao Yu 
294726787236SJaegeuk Kim 	if (dsync) {
294826787236SJaegeuk Kim 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
294926787236SJaegeuk Kim 
295026787236SJaegeuk Kim 		spin_lock(&sbi->inode_lock[DIRTY_META]);
295126787236SJaegeuk Kim 		ret = list_empty(&F2FS_I(inode)->gdirty_list);
295226787236SJaegeuk Kim 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
295326787236SJaegeuk Kim 		return ret;
295426787236SJaegeuk Kim 	}
295526787236SJaegeuk Kim 	if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
295626787236SJaegeuk Kim 			file_keep_isize(inode) ||
2957235831d7SJunling Zheng 			i_size_read(inode) & ~PAGE_MASK)
295826787236SJaegeuk Kim 		return false;
2959a0d00fadSChao Yu 
2960fe1897eaSChao Yu 	if (!f2fs_is_time_consistent(inode))
2961214c2461SJaegeuk Kim 		return false;
2962214c2461SJaegeuk Kim 
2963c10c9820SChao Yu 	spin_lock(&F2FS_I(inode)->i_size_lock);
2964a0d00fadSChao Yu 	ret = F2FS_I(inode)->last_disk_size == i_size_read(inode);
2965c10c9820SChao Yu 	spin_unlock(&F2FS_I(inode)->i_size_lock);
2966a0d00fadSChao Yu 
2967a0d00fadSChao Yu 	return ret;
2968267378d4SChao Yu }
2969267378d4SChao Yu 
2970deeedd71SChao Yu static inline bool f2fs_readonly(struct super_block *sb)
297177888c1eSJaegeuk Kim {
2972deeedd71SChao Yu 	return sb_rdonly(sb);
297377888c1eSJaegeuk Kim }
297477888c1eSJaegeuk Kim 
2975744602cfSJaegeuk Kim static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
2976744602cfSJaegeuk Kim {
2977aaec2b1dSChao Yu 	return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
2978744602cfSJaegeuk Kim }
2979744602cfSJaegeuk Kim 
298043c780baSEric Biggers static inline bool is_dot_dotdot(const u8 *name, size_t len)
2981eaa693f4SJaegeuk Kim {
298243c780baSEric Biggers 	if (len == 1 && name[0] == '.')
2983eaa693f4SJaegeuk Kim 		return true;
2984eaa693f4SJaegeuk Kim 
298543c780baSEric Biggers 	if (len == 2 && name[0] == '.' && name[1] == '.')
2986eaa693f4SJaegeuk Kim 		return true;
2987eaa693f4SJaegeuk Kim 
2988eaa693f4SJaegeuk Kim 	return false;
2989eaa693f4SJaegeuk Kim }
2990eaa693f4SJaegeuk Kim 
29913e72f721SJaegeuk Kim static inline bool f2fs_may_extent_tree(struct inode *inode)
29923e72f721SJaegeuk Kim {
2993e4589fa5SSahitya Tummala 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2994e4589fa5SSahitya Tummala 
2995e4589fa5SSahitya Tummala 	if (!test_opt(sbi, EXTENT_CACHE) ||
29964c8ff709SChao Yu 			is_inode_flag_set(inode, FI_NO_EXTENT) ||
29974c8ff709SChao Yu 			is_inode_flag_set(inode, FI_COMPRESSED_FILE))
29983e72f721SJaegeuk Kim 		return false;
29993e72f721SJaegeuk Kim 
3000e4589fa5SSahitya Tummala 	/*
3001e4589fa5SSahitya Tummala 	 * for recovered files during mount do not create extents
3002e4589fa5SSahitya Tummala 	 * if shrinker is not registered.
3003e4589fa5SSahitya Tummala 	 */
3004e4589fa5SSahitya Tummala 	if (list_empty(&sbi->s_list))
3005e4589fa5SSahitya Tummala 		return false;
3006e4589fa5SSahitya Tummala 
3007886f56f9SAl Viro 	return S_ISREG(inode->i_mode);
30083e72f721SJaegeuk Kim }
30093e72f721SJaegeuk Kim 
30101ecc0c5cSChao Yu static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
30111ecc0c5cSChao Yu 					size_t size, gfp_t flags)
30120414b004SJaegeuk Kim {
301355523519SChao Yu 	if (time_to_inject(sbi, FAULT_KMALLOC)) {
3014c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_KMALLOC);
30152c63feadSJaegeuk Kim 		return NULL;
301655523519SChao Yu 	}
30177fa750a1SArnd Bergmann 
30180b6d4ca0SEric Biggers 	return kmalloc(size, flags);
30190414b004SJaegeuk Kim }
30200414b004SJaegeuk Kim 
3021acbf054dSChao Yu static inline void *f2fs_kzalloc(struct f2fs_sb_info *sbi,
3022acbf054dSChao Yu 					size_t size, gfp_t flags)
3023acbf054dSChao Yu {
3024acbf054dSChao Yu 	return f2fs_kmalloc(sbi, size, flags | __GFP_ZERO);
3025acbf054dSChao Yu }
3026acbf054dSChao Yu 
3027628b3d14SChao Yu static inline void *f2fs_kvmalloc(struct f2fs_sb_info *sbi,
3028628b3d14SChao Yu 					size_t size, gfp_t flags)
3029628b3d14SChao Yu {
3030628b3d14SChao Yu 	if (time_to_inject(sbi, FAULT_KVMALLOC)) {
3031c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_KVMALLOC);
3032628b3d14SChao Yu 		return NULL;
3033628b3d14SChao Yu 	}
30347fa750a1SArnd Bergmann 
3035628b3d14SChao Yu 	return kvmalloc(size, flags);
3036628b3d14SChao Yu }
3037628b3d14SChao Yu 
3038628b3d14SChao Yu static inline void *f2fs_kvzalloc(struct f2fs_sb_info *sbi,
3039628b3d14SChao Yu 					size_t size, gfp_t flags)
3040628b3d14SChao Yu {
3041628b3d14SChao Yu 	return f2fs_kvmalloc(sbi, size, flags | __GFP_ZERO);
3042628b3d14SChao Yu }
3043628b3d14SChao Yu 
30447a2af766SChao Yu static inline int get_extra_isize(struct inode *inode)
3045f2470371SChao Yu {
30467a2af766SChao Yu 	return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
3047f2470371SChao Yu }
3048f2470371SChao Yu 
30496afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode)
30506afc662eSChao Yu {
30516afc662eSChao Yu 	return F2FS_I(inode)->i_inline_xattr_size;
30526afc662eSChao Yu }
30536afc662eSChao Yu 
30544d57b86dSChao Yu #define f2fs_get_inode_mode(i) \
305591942321SJaegeuk Kim 	((is_inode_flag_set(i, FI_ACL_MODE)) ? \
3056a6dda0e6SChristoph Hellwig 	 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
3057a6dda0e6SChristoph Hellwig 
30587a2af766SChao Yu #define F2FS_TOTAL_EXTRA_ATTR_SIZE			\
30597a2af766SChao Yu 	(offsetof(struct f2fs_inode, i_extra_end) -	\
30607a2af766SChao Yu 	offsetof(struct f2fs_inode, i_extra_isize))	\
30617a2af766SChao Yu 
30625c57132eSChao Yu #define F2FS_OLD_ATTRIBUTE_SIZE	(offsetof(struct f2fs_inode, i_addr))
30635c57132eSChao Yu #define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field)		\
30642f84babfSSheng Yong 		((offsetof(typeof(*(f2fs_inode)), field) +	\
30655c57132eSChao Yu 		sizeof((f2fs_inode)->field))			\
30662f84babfSSheng Yong 		<= (F2FS_OLD_ATTRIBUTE_SIZE + (extra_isize)))	\
30675c57132eSChao Yu 
30682bc4bea3SDaeho Jeong #define DEFAULT_IOSTAT_PERIOD_MS	3000
30692bc4bea3SDaeho Jeong #define MIN_IOSTAT_PERIOD_MS		100
30702bc4bea3SDaeho Jeong /* maximum period of iostat tracing is 1 day */
30712bc4bea3SDaeho Jeong #define MAX_IOSTAT_PERIOD_MS		8640000
30722bc4bea3SDaeho Jeong 
3073b0af6d49SChao Yu static inline void f2fs_reset_iostat(struct f2fs_sb_info *sbi)
3074b0af6d49SChao Yu {
3075b0af6d49SChao Yu 	int i;
3076b0af6d49SChao Yu 
3077b0af6d49SChao Yu 	spin_lock(&sbi->iostat_lock);
30782bc4bea3SDaeho Jeong 	for (i = 0; i < NR_IO_TYPE; i++) {
30798b83ac81SChao Yu 		sbi->rw_iostat[i] = 0;
30808b83ac81SChao Yu 		sbi->prev_rw_iostat[i] = 0;
30812bc4bea3SDaeho Jeong 	}
3082b0af6d49SChao Yu 	spin_unlock(&sbi->iostat_lock);
3083b0af6d49SChao Yu }
3084b0af6d49SChao Yu 
30852bc4bea3SDaeho Jeong extern void f2fs_record_iostat(struct f2fs_sb_info *sbi);
30862bc4bea3SDaeho Jeong 
3087b0af6d49SChao Yu static inline void f2fs_update_iostat(struct f2fs_sb_info *sbi,
3088b0af6d49SChao Yu 			enum iostat_type type, unsigned long long io_bytes)
3089b0af6d49SChao Yu {
3090b0af6d49SChao Yu 	if (!sbi->iostat_enable)
3091b0af6d49SChao Yu 		return;
3092b0af6d49SChao Yu 	spin_lock(&sbi->iostat_lock);
30938b83ac81SChao Yu 	sbi->rw_iostat[type] += io_bytes;
3094b0af6d49SChao Yu 
3095b0af6d49SChao Yu 	if (type == APP_WRITE_IO || type == APP_DIRECT_IO)
30968b83ac81SChao Yu 		sbi->rw_iostat[APP_BUFFERED_IO] =
30978b83ac81SChao Yu 			sbi->rw_iostat[APP_WRITE_IO] -
30988b83ac81SChao Yu 			sbi->rw_iostat[APP_DIRECT_IO];
30998b83ac81SChao Yu 
31008b83ac81SChao Yu 	if (type == APP_READ_IO || type == APP_DIRECT_READ_IO)
31018b83ac81SChao Yu 		sbi->rw_iostat[APP_BUFFERED_READ_IO] =
31028b83ac81SChao Yu 			sbi->rw_iostat[APP_READ_IO] -
31038b83ac81SChao Yu 			sbi->rw_iostat[APP_DIRECT_READ_IO];
3104b0af6d49SChao Yu 	spin_unlock(&sbi->iostat_lock);
31052bc4bea3SDaeho Jeong 
31062bc4bea3SDaeho Jeong 	f2fs_record_iostat(sbi);
3107b0af6d49SChao Yu }
3108b0af6d49SChao Yu 
31092c70c5e3SChao Yu #define __is_large_section(sbi)		((sbi)->segs_per_sec > 1)
31102c70c5e3SChao Yu 
31116dc3a126SChao Yu #define __is_meta_io(fio) (PAGE_TYPE_OF_BIO((fio)->type) == META)
3112c9b60788SChao Yu 
3113e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
3114e1da7872SChao Yu 					block_t blkaddr, int type);
3115e1da7872SChao Yu static inline void verify_blkaddr(struct f2fs_sb_info *sbi,
3116e1da7872SChao Yu 					block_t blkaddr, int type)
3117e1da7872SChao Yu {
3118e1da7872SChao Yu 	if (!f2fs_is_valid_blkaddr(sbi, blkaddr, type)) {
3119dcbb4c10SJoe Perches 		f2fs_err(sbi, "invalid blkaddr: %u, type: %d, run fsck to fix.",
3120e1da7872SChao Yu 			 blkaddr, type);
3121e1da7872SChao Yu 		f2fs_bug_on(sbi, 1);
3122e1da7872SChao Yu 	}
3123e1da7872SChao Yu }
3124e1da7872SChao Yu 
3125e1da7872SChao Yu static inline bool __is_valid_data_blkaddr(block_t blkaddr)
31267b525dd0SChao Yu {
31274c8ff709SChao Yu 	if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR ||
31284c8ff709SChao Yu 			blkaddr == COMPRESS_ADDR)
31297b525dd0SChao Yu 		return false;
31307b525dd0SChao Yu 	return true;
31317b525dd0SChao Yu }
31327b525dd0SChao Yu 
3133240a5915SChao Yu static inline void f2fs_set_page_private(struct page *page,
3134240a5915SChao Yu 						unsigned long data)
3135240a5915SChao Yu {
3136240a5915SChao Yu 	if (PagePrivate(page))
3137240a5915SChao Yu 		return;
3138240a5915SChao Yu 
31397128cf9aSGuoqing Jiang 	attach_page_private(page, (void *)data);
3140240a5915SChao Yu }
3141240a5915SChao Yu 
3142240a5915SChao Yu static inline void f2fs_clear_page_private(struct page *page)
3143240a5915SChao Yu {
31447128cf9aSGuoqing Jiang 	detach_page_private(page);
3145240a5915SChao Yu }
3146240a5915SChao Yu 
314739a53e0cSJaegeuk Kim /*
314839a53e0cSJaegeuk Kim  * file.c
314939a53e0cSJaegeuk Kim  */
3150cac5a3d8SDongOh Shin int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
31514d57b86dSChao Yu void f2fs_truncate_data_blocks(struct dnode_of_data *dn);
31523265d3dbSChao Yu int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock);
3153c42d28ceSChao Yu int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock);
3154cac5a3d8SDongOh Shin int f2fs_truncate(struct inode *inode);
3155a528d35eSDavid Howells int f2fs_getattr(const struct path *path, struct kstat *stat,
3156a528d35eSDavid Howells 			u32 request_mask, unsigned int flags);
3157cac5a3d8SDongOh Shin int f2fs_setattr(struct dentry *dentry, struct iattr *attr);
31584d57b86dSChao Yu int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
31594d57b86dSChao Yu void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count);
3160c4020b2dSChao Yu int f2fs_precache_extents(struct inode *inode);
3161cac5a3d8SDongOh Shin long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
3162cac5a3d8SDongOh Shin long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
316378130819SChao Yu int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid);
31641ad71a27SJaegeuk Kim int f2fs_pin_file_control(struct inode *inode, bool inc);
316539a53e0cSJaegeuk Kim 
316639a53e0cSJaegeuk Kim /*
316739a53e0cSJaegeuk Kim  * inode.c
316839a53e0cSJaegeuk Kim  */
3169cac5a3d8SDongOh Shin void f2fs_set_inode_flags(struct inode *inode);
3170704956ecSChao Yu bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page);
3171704956ecSChao Yu void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page);
3172cac5a3d8SDongOh Shin struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
3173cac5a3d8SDongOh Shin struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
31744d57b86dSChao Yu int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
31754d57b86dSChao Yu void f2fs_update_inode(struct inode *inode, struct page *node_page);
31764d57b86dSChao Yu void f2fs_update_inode_page(struct inode *inode);
3177cac5a3d8SDongOh Shin int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
3178cac5a3d8SDongOh Shin void f2fs_evict_inode(struct inode *inode);
31794d57b86dSChao Yu void f2fs_handle_failed_inode(struct inode *inode);
318039a53e0cSJaegeuk Kim 
318139a53e0cSJaegeuk Kim /*
318239a53e0cSJaegeuk Kim  * namei.c
318339a53e0cSJaegeuk Kim  */
31844d57b86dSChao Yu int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
3185b6a06cbbSChao Yu 							bool hot, bool set);
318639a53e0cSJaegeuk Kim struct dentry *f2fs_get_parent(struct dentry *child);
318739a53e0cSJaegeuk Kim 
318839a53e0cSJaegeuk Kim /*
318939a53e0cSJaegeuk Kim  * dir.c
319039a53e0cSJaegeuk Kim  */
31914d57b86dSChao Yu unsigned char f2fs_get_de_type(struct f2fs_dir_entry *de);
319243c780baSEric Biggers int f2fs_init_casefolded_name(const struct inode *dir,
319343c780baSEric Biggers 			      struct f2fs_filename *fname);
319443c780baSEric Biggers int f2fs_setup_filename(struct inode *dir, const struct qstr *iname,
319543c780baSEric Biggers 			int lookup, struct f2fs_filename *fname);
319643c780baSEric Biggers int f2fs_prepare_lookup(struct inode *dir, struct dentry *dentry,
319743c780baSEric Biggers 			struct f2fs_filename *fname);
319843c780baSEric Biggers void f2fs_free_filename(struct f2fs_filename *fname);
319943c780baSEric Biggers struct f2fs_dir_entry *f2fs_find_target_dentry(const struct f2fs_dentry_ptr *d,
320043c780baSEric Biggers 			const struct f2fs_filename *fname, int *max_slots);
3201cac5a3d8SDongOh Shin int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
3202cac5a3d8SDongOh Shin 			unsigned int start_pos, struct fscrypt_str *fstr);
32034d57b86dSChao Yu void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
3204cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
32054d57b86dSChao Yu struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
320643c780baSEric Biggers 			const struct f2fs_filename *fname, struct page *dpage);
32074d57b86dSChao Yu void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
3208cac5a3d8SDongOh Shin 			unsigned int current_depth);
32094d57b86dSChao Yu int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots);
3210cac5a3d8SDongOh Shin void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
3211cac5a3d8SDongOh Shin struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
321243c780baSEric Biggers 					 const struct f2fs_filename *fname,
321343c780baSEric Biggers 					 struct page **res_page);
3214cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
3215cac5a3d8SDongOh Shin 			const struct qstr *child, struct page **res_page);
3216cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
3217cac5a3d8SDongOh Shin ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
3218cac5a3d8SDongOh Shin 			struct page **page);
3219cac5a3d8SDongOh Shin void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
3220cac5a3d8SDongOh Shin 			struct page *page, struct inode *inode);
3221b06af2afSJaegeuk Kim bool f2fs_has_enough_room(struct inode *dir, struct page *ipage,
322243c780baSEric Biggers 			  const struct f2fs_filename *fname);
3223cac5a3d8SDongOh Shin void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
322443c780baSEric Biggers 			const struct fscrypt_str *name, f2fs_hash_t name_hash,
3225cac5a3d8SDongOh Shin 			unsigned int bit_pos);
322643c780baSEric Biggers int f2fs_add_regular_entry(struct inode *dir, const struct f2fs_filename *fname,
3227cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
322843c780baSEric Biggers int f2fs_add_dentry(struct inode *dir, const struct f2fs_filename *fname,
3229cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
32304d57b86dSChao Yu int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
3231cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
3232cac5a3d8SDongOh Shin void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
3233cac5a3d8SDongOh Shin 			struct inode *dir, struct inode *inode);
3234cac5a3d8SDongOh Shin int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
3235cac5a3d8SDongOh Shin bool f2fs_empty_dir(struct inode *dir);
323639a53e0cSJaegeuk Kim 
3237b7f7a5e0SAl Viro static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
3238b7f7a5e0SAl Viro {
32394d57b86dSChao Yu 	return f2fs_do_add_link(d_inode(dentry->d_parent), &dentry->d_name,
3240510022a8SJaegeuk Kim 				inode, inode->i_ino, inode->i_mode);
3241b7f7a5e0SAl Viro }
3242b7f7a5e0SAl Viro 
324339a53e0cSJaegeuk Kim /*
324439a53e0cSJaegeuk Kim  * super.c
324539a53e0cSJaegeuk Kim  */
3246cac5a3d8SDongOh Shin int f2fs_inode_dirtied(struct inode *inode, bool sync);
3247cac5a3d8SDongOh Shin void f2fs_inode_synced(struct inode *inode);
3248ea676733SJaegeuk Kim int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly);
3249af033b2aSChao Yu int f2fs_quota_sync(struct super_block *sb, int type);
32504b2414d0SChao Yu void f2fs_quota_off_umount(struct super_block *sb);
3251cac5a3d8SDongOh Shin int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
3252cac5a3d8SDongOh Shin int f2fs_sync_fs(struct super_block *sb, int sync);
32534d57b86dSChao Yu int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi);
325439a53e0cSJaegeuk Kim 
325539a53e0cSJaegeuk Kim /*
325639a53e0cSJaegeuk Kim  * hash.c
325739a53e0cSJaegeuk Kim  */
325843c780baSEric Biggers void f2fs_hash_filename(const struct inode *dir, struct f2fs_filename *fname);
325939a53e0cSJaegeuk Kim 
326039a53e0cSJaegeuk Kim /*
326139a53e0cSJaegeuk Kim  * node.c
326239a53e0cSJaegeuk Kim  */
326339a53e0cSJaegeuk Kim struct dnode_of_data;
326439a53e0cSJaegeuk Kim struct node_info;
326539a53e0cSJaegeuk Kim 
32664d57b86dSChao Yu int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid);
32674d57b86dSChao Yu bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type);
326850fa53ecSChao Yu bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct page *page);
326950fa53ecSChao Yu void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi);
327050fa53ecSChao Yu void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct page *page);
327150fa53ecSChao Yu void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi);
32724d57b86dSChao Yu int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
32734d57b86dSChao Yu bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
32744d57b86dSChao Yu bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
32757735730dSChao Yu int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
32764d57b86dSChao Yu 						struct node_info *ni);
32774d57b86dSChao Yu pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
32784d57b86dSChao Yu int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
32794d57b86dSChao Yu int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from);
32804d57b86dSChao Yu int f2fs_truncate_xattr_node(struct inode *inode);
328150fa53ecSChao Yu int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
328250fa53ecSChao Yu 					unsigned int seq_id);
32834d57b86dSChao Yu int f2fs_remove_inode_page(struct inode *inode);
32844d57b86dSChao Yu struct page *f2fs_new_inode_page(struct inode *inode);
32854d57b86dSChao Yu struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs);
32864d57b86dSChao Yu void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
32874d57b86dSChao Yu struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
32884d57b86dSChao Yu struct page *f2fs_get_node_page_ra(struct page *parent, int start);
328948018b4cSChao Yu int f2fs_move_node_page(struct page *node_page, int gc_type);
329068e79bafSJia Yang void f2fs_flush_inline_data(struct f2fs_sb_info *sbi);
32914d57b86dSChao Yu int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
329250fa53ecSChao Yu 			struct writeback_control *wbc, bool atomic,
329350fa53ecSChao Yu 			unsigned int *seq_id);
32944d57b86dSChao Yu int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
32954d57b86dSChao Yu 			struct writeback_control *wbc,
3296b0af6d49SChao Yu 			bool do_balance, enum iostat_type io_type);
3297e2374015SChao Yu int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
32984d57b86dSChao Yu bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
32994d57b86dSChao Yu void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
33004d57b86dSChao Yu void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
33014d57b86dSChao Yu int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
33029627a7b3SChao Yu int f2fs_recover_inline_xattr(struct inode *inode, struct page *page);
33034d57b86dSChao Yu int f2fs_recover_xattr_data(struct inode *inode, struct page *page);
33044d57b86dSChao Yu int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
33057735730dSChao Yu int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
3306cac5a3d8SDongOh Shin 			unsigned int segno, struct f2fs_summary_block *sum);
3307edc55aafSJaegeuk Kim int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
33084d57b86dSChao Yu int f2fs_build_node_manager(struct f2fs_sb_info *sbi);
33094d57b86dSChao Yu void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi);
33104d57b86dSChao Yu int __init f2fs_create_node_manager_caches(void);
33114d57b86dSChao Yu void f2fs_destroy_node_manager_caches(void);
331239a53e0cSJaegeuk Kim 
331339a53e0cSJaegeuk Kim /*
331439a53e0cSJaegeuk Kim  * segment.c
331539a53e0cSJaegeuk Kim  */
33164d57b86dSChao Yu bool f2fs_need_SSR(struct f2fs_sb_info *sbi);
33174d57b86dSChao Yu void f2fs_register_inmem_page(struct inode *inode, struct page *page);
33184d57b86dSChao Yu void f2fs_drop_inmem_pages_all(struct f2fs_sb_info *sbi, bool gc_failure);
33194d57b86dSChao Yu void f2fs_drop_inmem_pages(struct inode *inode);
33204d57b86dSChao Yu void f2fs_drop_inmem_page(struct inode *inode, struct page *page);
33214d57b86dSChao Yu int f2fs_commit_inmem_pages(struct inode *inode);
3322cac5a3d8SDongOh Shin void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
33237bcd0cfaSChao Yu void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi, bool from_bg);
332439d787beSChao Yu int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
33254d57b86dSChao Yu int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
33261228b482SChao Yu int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
33274d57b86dSChao Yu void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
33284d57b86dSChao Yu void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
33294d57b86dSChao Yu bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
33304d57b86dSChao Yu void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
33314d57b86dSChao Yu void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi);
333203f2c02dSJaegeuk Kim bool f2fs_issue_discard_timeout(struct f2fs_sb_info *sbi);
33334d57b86dSChao Yu void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
33344d57b86dSChao Yu 					struct cp_control *cpc);
33354354994fSDaniel Rosenberg void f2fs_dirty_to_prefree(struct f2fs_sb_info *sbi);
33364d3aed70SDaniel Rosenberg block_t f2fs_get_unusable_blocks(struct f2fs_sb_info *sbi);
33374d3aed70SDaniel Rosenberg int f2fs_disable_cp_again(struct f2fs_sb_info *sbi, block_t unusable);
33384d57b86dSChao Yu void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi);
33394d57b86dSChao Yu int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
3340d0b9e42aSChao Yu void f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi, int type);
3341d0b9e42aSChao Yu void f2fs_restore_inmem_curseg(struct f2fs_sb_info *sbi, int type);
33420ef81833SChao Yu void f2fs_allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type,
334304f0b2eaSQiuyang Sun 					unsigned int start, unsigned int end);
3344901d745fSChao Yu void f2fs_allocate_new_segment(struct f2fs_sb_info *sbi, int type);
3345901d745fSChao Yu void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi);
3346cac5a3d8SDongOh Shin int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
33474d57b86dSChao Yu bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
33484d57b86dSChao Yu 					struct cp_control *cpc);
33494d57b86dSChao Yu struct page *f2fs_get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
33504d57b86dSChao Yu void f2fs_update_meta_page(struct f2fs_sb_info *sbi, void *src,
33514d57b86dSChao Yu 					block_t blk_addr);
33524d57b86dSChao Yu void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
3353b0af6d49SChao Yu 						enum iostat_type io_type);
33544d57b86dSChao Yu void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio);
33554d57b86dSChao Yu void f2fs_outplace_write_data(struct dnode_of_data *dn,
33564d57b86dSChao Yu 			struct f2fs_io_info *fio);
33574d57b86dSChao Yu int f2fs_inplace_write_data(struct f2fs_io_info *fio);
33584d57b86dSChao Yu void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
3359cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t new_blkaddr,
3360*c5d02785SChao Yu 			bool recover_curseg, bool recover_newaddr,
3361*c5d02785SChao Yu 			bool from_gc);
3362cac5a3d8SDongOh Shin void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
3363cac5a3d8SDongOh Shin 			block_t old_addr, block_t new_addr,
3364cac5a3d8SDongOh Shin 			unsigned char version, bool recover_curseg,
3365cac5a3d8SDongOh Shin 			bool recover_newaddr);
33664d57b86dSChao Yu void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
3367cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t *new_blkaddr,
3368fb830fc5SChao Yu 			struct f2fs_summary *sum, int type,
3369*c5d02785SChao Yu 			struct f2fs_io_info *fio, bool from_gc);
3370cac5a3d8SDongOh Shin void f2fs_wait_on_page_writeback(struct page *page,
3371bae0ee7aSChao Yu 			enum page_type type, bool ordered, bool locked);
33720ded69f6SJaegeuk Kim void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr);
33731e78e8bdSSahitya Tummala void f2fs_wait_on_block_writeback_range(struct inode *inode, block_t blkaddr,
33741e78e8bdSSahitya Tummala 								block_t len);
33754d57b86dSChao Yu void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
33764d57b86dSChao Yu void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
33774d57b86dSChao Yu int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
3378cac5a3d8SDongOh Shin 			unsigned int val, int alloc);
33794d57b86dSChao Yu void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
3380c426d991SShin'ichiro Kawasaki int f2fs_fix_curseg_write_pointer(struct f2fs_sb_info *sbi);
3381d508c94eSShin'ichiro Kawasaki int f2fs_check_write_pointer(struct f2fs_sb_info *sbi);
33824d57b86dSChao Yu int f2fs_build_segment_manager(struct f2fs_sb_info *sbi);
33834d57b86dSChao Yu void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi);
33844d57b86dSChao Yu int __init f2fs_create_segment_manager_caches(void);
33854d57b86dSChao Yu void f2fs_destroy_segment_manager_caches(void);
33864d57b86dSChao Yu int f2fs_rw_hint_to_seg_type(enum rw_hint hint);
33874d57b86dSChao Yu enum rw_hint f2fs_io_type_to_rw_hint(struct f2fs_sb_info *sbi,
33884d57b86dSChao Yu 			enum page_type type, enum temp_type temp);
3389de881df9SAravind Ramesh unsigned int f2fs_usable_segs_in_sec(struct f2fs_sb_info *sbi,
3390de881df9SAravind Ramesh 			unsigned int segno);
3391de881df9SAravind Ramesh unsigned int f2fs_usable_blks_in_seg(struct f2fs_sb_info *sbi,
3392de881df9SAravind Ramesh 			unsigned int segno);
339339a53e0cSJaegeuk Kim 
339439a53e0cSJaegeuk Kim /*
339539a53e0cSJaegeuk Kim  * checkpoint.c
339639a53e0cSJaegeuk Kim  */
3397cac5a3d8SDongOh Shin void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io);
33984d57b86dSChao Yu struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
33994d57b86dSChao Yu struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
34007735730dSChao Yu struct page *f2fs_get_meta_page_nofail(struct f2fs_sb_info *sbi, pgoff_t index);
34014d57b86dSChao Yu struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
3402e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
34034d57b86dSChao Yu 					block_t blkaddr, int type);
34044d57b86dSChao Yu int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
3405cac5a3d8SDongOh Shin 			int type, bool sync);
34064d57b86dSChao Yu void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index);
34074d57b86dSChao Yu long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
3408b0af6d49SChao Yu 			long nr_to_write, enum iostat_type io_type);
34094d57b86dSChao Yu void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
34104d57b86dSChao Yu void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
34114d57b86dSChao Yu void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all);
34124d57b86dSChao Yu bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
34134d57b86dSChao Yu void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
341439d787beSChao Yu 					unsigned int devidx, int type);
34154d57b86dSChao Yu bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
341639d787beSChao Yu 					unsigned int devidx, int type);
3417cac5a3d8SDongOh Shin int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
34184d57b86dSChao Yu int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi);
34194d57b86dSChao Yu void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi);
34204d57b86dSChao Yu void f2fs_add_orphan_inode(struct inode *inode);
34214d57b86dSChao Yu void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
34224d57b86dSChao Yu int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi);
34234d57b86dSChao Yu int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi);
34244d57b86dSChao Yu void f2fs_update_dirty_page(struct inode *inode, struct page *page);
34254d57b86dSChao Yu void f2fs_remove_dirty_inode(struct inode *inode);
34264d57b86dSChao Yu int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
3427bf22c3ccSSahitya Tummala void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type);
34284d57b86dSChao Yu int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
34294d57b86dSChao Yu void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi);
34304d57b86dSChao Yu int __init f2fs_create_checkpoint_caches(void);
34314d57b86dSChao Yu void f2fs_destroy_checkpoint_caches(void);
343239a53e0cSJaegeuk Kim 
343339a53e0cSJaegeuk Kim /*
343439a53e0cSJaegeuk Kim  * data.c
343539a53e0cSJaegeuk Kim  */
3436f543805fSChao Yu int __init f2fs_init_bioset(void);
3437f543805fSChao Yu void f2fs_destroy_bioset(void);
3438ca9e968aSChao Yu struct bio *f2fs_bio_alloc(struct f2fs_sb_info *sbi, int npages, bool noio);
34390b20fcecSChao Yu int f2fs_init_bio_entry_cache(void);
34400b20fcecSChao Yu void f2fs_destroy_bio_entry_cache(void);
34414c8ff709SChao Yu void f2fs_submit_bio(struct f2fs_sb_info *sbi,
34424c8ff709SChao Yu 				struct bio *bio, enum page_type type);
3443b9109b0eSJaegeuk Kim void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
3444b9109b0eSJaegeuk Kim void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
3445bab475c5SChao Yu 				struct inode *inode, struct page *page,
3446bab475c5SChao Yu 				nid_t ino, enum page_type type);
34470b20fcecSChao Yu void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
34480b20fcecSChao Yu 					struct bio **bio, struct page *page);
3449b9109b0eSJaegeuk Kim void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
3450cac5a3d8SDongOh Shin int f2fs_submit_page_bio(struct f2fs_io_info *fio);
34518648de2cSChao Yu int f2fs_merge_page_bio(struct f2fs_io_info *fio);
3452fe16efe6SChao Yu void f2fs_submit_page_write(struct f2fs_io_info *fio);
3453cac5a3d8SDongOh Shin struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
3454cac5a3d8SDongOh Shin 			block_t blk_addr, struct bio *bio);
3455cac5a3d8SDongOh Shin int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
34564d57b86dSChao Yu void f2fs_set_data_blkaddr(struct dnode_of_data *dn);
3457cac5a3d8SDongOh Shin void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
34584d57b86dSChao Yu int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
34594d57b86dSChao Yu int f2fs_reserve_new_block(struct dnode_of_data *dn);
3460cac5a3d8SDongOh Shin int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
3461cac5a3d8SDongOh Shin int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from);
3462cac5a3d8SDongOh Shin int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
34634d57b86dSChao Yu struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
3464cac5a3d8SDongOh Shin 			int op_flags, bool for_write);
34654d57b86dSChao Yu struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index);
34664d57b86dSChao Yu struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
3467cac5a3d8SDongOh Shin 			bool for_write);
34684d57b86dSChao Yu struct page *f2fs_get_new_data_page(struct inode *inode,
3469cac5a3d8SDongOh Shin 			struct page *ipage, pgoff_t index, bool new_i_size);
34704d57b86dSChao Yu int f2fs_do_write_data_page(struct f2fs_io_info *fio);
34710ef81833SChao Yu void f2fs_do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock);
3472cac5a3d8SDongOh Shin int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
3473cac5a3d8SDongOh Shin 			int create, int flag);
3474cac5a3d8SDongOh Shin int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3475cac5a3d8SDongOh Shin 			u64 start, u64 len);
34764c8ff709SChao Yu int f2fs_encrypt_one_page(struct f2fs_io_info *fio);
34774d57b86dSChao Yu bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio);
34784d57b86dSChao Yu bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio);
34794c8ff709SChao Yu int f2fs_write_single_data_page(struct page *page, int *submitted,
34804c8ff709SChao Yu 				struct bio **bio, sector_t *last_block,
34814c8ff709SChao Yu 				struct writeback_control *wbc,
34824c8ff709SChao Yu 				enum iostat_type io_type,
34834c8ff709SChao Yu 				int compr_blocks);
3484cac5a3d8SDongOh Shin void f2fs_invalidate_page(struct page *page, unsigned int offset,
3485cac5a3d8SDongOh Shin 			unsigned int length);
3486cac5a3d8SDongOh Shin int f2fs_release_page(struct page *page, gfp_t wait);
34875b7a487cSWeichao Guo #ifdef CONFIG_MIGRATION
3488cac5a3d8SDongOh Shin int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,
3489cac5a3d8SDongOh Shin 			struct page *page, enum migrate_mode mode);
34905b7a487cSWeichao Guo #endif
3491b91050a8SHyunchul Lee bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len);
34925ec2d99dSMatthew Wilcox void f2fs_clear_page_cache_dirty_tag(struct page *page);
34934c8ff709SChao Yu int f2fs_init_post_read_processing(void);
34944c8ff709SChao Yu void f2fs_destroy_post_read_processing(void);
34954c8ff709SChao Yu int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi);
34964c8ff709SChao Yu void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi);
349739a53e0cSJaegeuk Kim 
349839a53e0cSJaegeuk Kim /*
349939a53e0cSJaegeuk Kim  * gc.c
350039a53e0cSJaegeuk Kim  */
35014d57b86dSChao Yu int f2fs_start_gc_thread(struct f2fs_sb_info *sbi);
35024d57b86dSChao Yu void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi);
35034d57b86dSChao Yu block_t f2fs_start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
3504e066b83cSJaegeuk Kim int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background,
3505e066b83cSJaegeuk Kim 			unsigned int segno);
35064d57b86dSChao Yu void f2fs_build_gc_manager(struct f2fs_sb_info *sbi);
350704f0b2eaSQiuyang Sun int f2fs_resize_fs(struct f2fs_sb_info *sbi, __u64 block_count);
350839a53e0cSJaegeuk Kim 
350939a53e0cSJaegeuk Kim /*
351039a53e0cSJaegeuk Kim  * recovery.c
351139a53e0cSJaegeuk Kim  */
35124d57b86dSChao Yu int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
35134d57b86dSChao Yu bool f2fs_space_for_roll_forward(struct f2fs_sb_info *sbi);
351439a53e0cSJaegeuk Kim 
351539a53e0cSJaegeuk Kim /*
351639a53e0cSJaegeuk Kim  * debug.c
351739a53e0cSJaegeuk Kim  */
351839a53e0cSJaegeuk Kim #ifdef CONFIG_F2FS_STAT_FS
351939a53e0cSJaegeuk Kim struct f2fs_stat_info {
352039a53e0cSJaegeuk Kim 	struct list_head stat_list;
352139a53e0cSJaegeuk Kim 	struct f2fs_sb_info *sbi;
352239a53e0cSJaegeuk Kim 	int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
352339a53e0cSJaegeuk Kim 	int main_area_segs, main_area_sections, main_area_zones;
35245b7ee374SChao Yu 	unsigned long long hit_largest, hit_cached, hit_rbtree;
35255b7ee374SChao Yu 	unsigned long long hit_total, total_ext;
3526c00ba554SJaegeuk Kim 	int ext_tree, zombie_tree, ext_node;
35272c8a4a28SJaegeuk Kim 	int ndirty_node, ndirty_dent, ndirty_meta, ndirty_imeta;
35282c8a4a28SJaegeuk Kim 	int ndirty_data, ndirty_qdata;
352935782b23SJaegeuk Kim 	int inmem_pages;
35302c8a4a28SJaegeuk Kim 	unsigned int ndirty_dirs, ndirty_files, nquota_files, ndirty_all;
35315b0ef73cSJaegeuk Kim 	int nats, dirty_nats, sits, dirty_sits;
35325b0ef73cSJaegeuk Kim 	int free_nids, avail_nids, alloc_nids;
353339a53e0cSJaegeuk Kim 	int total_count, utilization;
35348b8dd65fSChao Yu 	int bg_gc, nr_wb_cp_data, nr_wb_data;
35355f9abab4SJaegeuk Kim 	int nr_rd_data, nr_rd_node, nr_rd_meta;
353602b16d0aSChao Yu 	int nr_dio_read, nr_dio_write;
3537274bd9baSChao Yu 	unsigned int io_skip_bggc, other_skip_bggc;
353814d8d5f7SChao Yu 	int nr_flushing, nr_flushed, flush_list_empty;
353914d8d5f7SChao Yu 	int nr_discarding, nr_discarded;
35405f32366aSChao Yu 	int nr_discard_cmd;
3541d84d1cbdSChao Yu 	unsigned int undiscard_blks;
3542a00861dbSJaegeuk Kim 	int inline_xattr, inline_inode, inline_dir, append, update, orphans;
35434c8ff709SChao Yu 	int compr_inode, compr_blocks;
3544648d50baSChao Yu 	int aw_cnt, max_aw_cnt, vw_cnt, max_vw_cnt;
3545f83a2584SYunlei He 	unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
354639a53e0cSJaegeuk Kim 	unsigned int bimodal, avg_vblocks;
354739a53e0cSJaegeuk Kim 	int util_free, util_valid, util_invalid;
354839a53e0cSJaegeuk Kim 	int rsvd_segs, overp_segs;
354939a53e0cSJaegeuk Kim 	int dirty_count, node_pages, meta_pages;
355042190d2aSJaegeuk Kim 	int prefree_count, call_count, cp_count, bg_cp_count;
355139a53e0cSJaegeuk Kim 	int tot_segs, node_segs, data_segs, free_segs, free_secs;
3552e1235983SChangman Lee 	int bg_node_segs, bg_data_segs;
355339a53e0cSJaegeuk Kim 	int tot_blks, data_blks, node_blks;
3554e1235983SChangman Lee 	int bg_data_blks, bg_node_blks;
35552ef79ecbSChao Yu 	unsigned long long skipped_atomic_files[2];
355639a53e0cSJaegeuk Kim 	int curseg[NR_CURSEG_TYPE];
355739a53e0cSJaegeuk Kim 	int cursec[NR_CURSEG_TYPE];
355839a53e0cSJaegeuk Kim 	int curzone[NR_CURSEG_TYPE];
35590759e2c1SChao Yu 	unsigned int dirty_seg[NR_CURSEG_TYPE];
35600759e2c1SChao Yu 	unsigned int full_seg[NR_CURSEG_TYPE];
35610759e2c1SChao Yu 	unsigned int valid_blks[NR_CURSEG_TYPE];
356239a53e0cSJaegeuk Kim 
3563b63e7be5SChao Yu 	unsigned int meta_count[META_MAX];
356439a53e0cSJaegeuk Kim 	unsigned int segment_count[2];
356539a53e0cSJaegeuk Kim 	unsigned int block_count[2];
3566b9a2c252SChangman Lee 	unsigned int inplace_count;
35679edcdabfSChao Yu 	unsigned long long base_mem, cache_mem, page_mem;
356839a53e0cSJaegeuk Kim };
356939a53e0cSJaegeuk Kim 
3570963d4f7dSGu Zheng static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
3571963d4f7dSGu Zheng {
3572963d4f7dSGu Zheng 	return (struct f2fs_stat_info *)sbi->stat_info;
3573963d4f7dSGu Zheng }
3574963d4f7dSGu Zheng 
3575942e0be6SChangman Lee #define stat_inc_cp_count(si)		((si)->cp_count++)
357642190d2aSJaegeuk Kim #define stat_inc_bg_cp_count(si)	((si)->bg_cp_count++)
357739a53e0cSJaegeuk Kim #define stat_inc_call_count(si)		((si)->call_count++)
3578fc7100eaSHridya Valsaraju #define stat_inc_bggc_count(si)		((si)->bg_gc++)
3579274bd9baSChao Yu #define stat_io_skip_bggc_count(sbi)	((sbi)->io_skip_bggc++)
3580274bd9baSChao Yu #define stat_other_skip_bggc_count(sbi)	((sbi)->other_skip_bggc++)
358133fbd510SChao Yu #define stat_inc_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]++)
358233fbd510SChao Yu #define stat_dec_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]--)
35835b7ee374SChao Yu #define stat_inc_total_hit(sbi)		(atomic64_inc(&(sbi)->total_hit_ext))
35845b7ee374SChao Yu #define stat_inc_rbtree_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_rbtree))
35855b7ee374SChao Yu #define stat_inc_largest_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_largest))
35865b7ee374SChao Yu #define stat_inc_cached_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_cached))
3587d5e8f6c9SChao Yu #define stat_inc_inline_xattr(inode)					\
3588d5e8f6c9SChao Yu 	do {								\
3589d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3590d5e8f6c9SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_xattr));	\
3591d5e8f6c9SChao Yu 	} while (0)
3592d5e8f6c9SChao Yu #define stat_dec_inline_xattr(inode)					\
3593d5e8f6c9SChao Yu 	do {								\
3594d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3595d5e8f6c9SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_xattr));	\
3596d5e8f6c9SChao Yu 	} while (0)
35970dbdc2aeSJaegeuk Kim #define stat_inc_inline_inode(inode)					\
35980dbdc2aeSJaegeuk Kim 	do {								\
35990dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
360003e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_inode));	\
36010dbdc2aeSJaegeuk Kim 	} while (0)
36020dbdc2aeSJaegeuk Kim #define stat_dec_inline_inode(inode)					\
36030dbdc2aeSJaegeuk Kim 	do {								\
36040dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
360503e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_inode));	\
36060dbdc2aeSJaegeuk Kim 	} while (0)
36073289c061SJaegeuk Kim #define stat_inc_inline_dir(inode)					\
36083289c061SJaegeuk Kim 	do {								\
36093289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
361003e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_dir));	\
36113289c061SJaegeuk Kim 	} while (0)
36123289c061SJaegeuk Kim #define stat_dec_inline_dir(inode)					\
36133289c061SJaegeuk Kim 	do {								\
36143289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
361503e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_dir));	\
36163289c061SJaegeuk Kim 	} while (0)
36174c8ff709SChao Yu #define stat_inc_compr_inode(inode)					\
36184c8ff709SChao Yu 	do {								\
36194c8ff709SChao Yu 		if (f2fs_compressed_file(inode))			\
36204c8ff709SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->compr_inode));	\
36214c8ff709SChao Yu 	} while (0)
36224c8ff709SChao Yu #define stat_dec_compr_inode(inode)					\
36234c8ff709SChao Yu 	do {								\
36244c8ff709SChao Yu 		if (f2fs_compressed_file(inode))			\
36254c8ff709SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->compr_inode));	\
36264c8ff709SChao Yu 	} while (0)
36274c8ff709SChao Yu #define stat_add_compr_blocks(inode, blocks)				\
36284c8ff709SChao Yu 		(atomic_add(blocks, &F2FS_I_SB(inode)->compr_blocks))
36294c8ff709SChao Yu #define stat_sub_compr_blocks(inode, blocks)				\
36304c8ff709SChao Yu 		(atomic_sub(blocks, &F2FS_I_SB(inode)->compr_blocks))
3631b63e7be5SChao Yu #define stat_inc_meta_count(sbi, blkaddr)				\
3632b63e7be5SChao Yu 	do {								\
3633b63e7be5SChao Yu 		if (blkaddr < SIT_I(sbi)->sit_base_addr)		\
3634b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_CP]);	\
3635b63e7be5SChao Yu 		else if (blkaddr < NM_I(sbi)->nat_blkaddr)		\
3636b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_SIT]);	\
3637b63e7be5SChao Yu 		else if (blkaddr < SM_I(sbi)->ssa_blkaddr)		\
3638b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_NAT]);	\
3639b63e7be5SChao Yu 		else if (blkaddr < SM_I(sbi)->main_blkaddr)		\
3640b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_SSA]);	\
3641b63e7be5SChao Yu 	} while (0)
3642dcdfff65SJaegeuk Kim #define stat_inc_seg_type(sbi, curseg)					\
3643dcdfff65SJaegeuk Kim 		((sbi)->segment_count[(curseg)->alloc_type]++)
3644dcdfff65SJaegeuk Kim #define stat_inc_block_count(sbi, curseg)				\
3645dcdfff65SJaegeuk Kim 		((sbi)->block_count[(curseg)->alloc_type]++)
3646b9a2c252SChangman Lee #define stat_inc_inplace_blocks(sbi)					\
3647b9a2c252SChangman Lee 		(atomic_inc(&(sbi)->inplace_count))
364826a28a0cSJaegeuk Kim #define stat_update_max_atomic_write(inode)				\
364926a28a0cSJaegeuk Kim 	do {								\
36500e6d0164SSahitya Tummala 		int cur = F2FS_I_SB(inode)->atomic_files;	\
365126a28a0cSJaegeuk Kim 		int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt);	\
365226a28a0cSJaegeuk Kim 		if (cur > max)						\
365326a28a0cSJaegeuk Kim 			atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur);	\
365426a28a0cSJaegeuk Kim 	} while (0)
3655648d50baSChao Yu #define stat_inc_volatile_write(inode)					\
3656648d50baSChao Yu 		(atomic_inc(&F2FS_I_SB(inode)->vw_cnt))
3657648d50baSChao Yu #define stat_dec_volatile_write(inode)					\
3658648d50baSChao Yu 		(atomic_dec(&F2FS_I_SB(inode)->vw_cnt))
3659648d50baSChao Yu #define stat_update_max_volatile_write(inode)				\
3660648d50baSChao Yu 	do {								\
3661648d50baSChao Yu 		int cur = atomic_read(&F2FS_I_SB(inode)->vw_cnt);	\
3662648d50baSChao Yu 		int max = atomic_read(&F2FS_I_SB(inode)->max_vw_cnt);	\
3663648d50baSChao Yu 		if (cur > max)						\
3664648d50baSChao Yu 			atomic_set(&F2FS_I_SB(inode)->max_vw_cnt, cur);	\
3665648d50baSChao Yu 	} while (0)
3666e1235983SChangman Lee #define stat_inc_seg_count(sbi, type, gc_type)				\
366739a53e0cSJaegeuk Kim 	do {								\
3668963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
366968afcf2dSTomohiro Kusumi 		si->tot_segs++;						\
367068afcf2dSTomohiro Kusumi 		if ((type) == SUM_TYPE_DATA) {				\
367139a53e0cSJaegeuk Kim 			si->data_segs++;				\
3672e1235983SChangman Lee 			si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0;	\
3673e1235983SChangman Lee 		} else {						\
367439a53e0cSJaegeuk Kim 			si->node_segs++;				\
3675e1235983SChangman Lee 			si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0;	\
3676e1235983SChangman Lee 		}							\
367739a53e0cSJaegeuk Kim 	} while (0)
367839a53e0cSJaegeuk Kim 
367939a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks)				\
368068afcf2dSTomohiro Kusumi 	((si)->tot_blks += (blks))
368139a53e0cSJaegeuk Kim 
3682e1235983SChangman Lee #define stat_inc_data_blk_count(sbi, blks, gc_type)			\
368339a53e0cSJaegeuk Kim 	do {								\
3684963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
368539a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
368639a53e0cSJaegeuk Kim 		si->data_blks += (blks);				\
368768afcf2dSTomohiro Kusumi 		si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
368839a53e0cSJaegeuk Kim 	} while (0)
368939a53e0cSJaegeuk Kim 
3690e1235983SChangman Lee #define stat_inc_node_blk_count(sbi, blks, gc_type)			\
369139a53e0cSJaegeuk Kim 	do {								\
3692963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
369339a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
369439a53e0cSJaegeuk Kim 		si->node_blks += (blks);				\
369568afcf2dSTomohiro Kusumi 		si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
369639a53e0cSJaegeuk Kim 	} while (0)
369739a53e0cSJaegeuk Kim 
3698cac5a3d8SDongOh Shin int f2fs_build_stats(struct f2fs_sb_info *sbi);
3699cac5a3d8SDongOh Shin void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
370021acc07dSGreg Kroah-Hartman void __init f2fs_create_root_stats(void);
37014589d25dSNamjae Jeon void f2fs_destroy_root_stats(void);
3702fc7100eaSHridya Valsaraju void f2fs_update_sit_info(struct f2fs_sb_info *sbi);
370339a53e0cSJaegeuk Kim #else
3704d66450e7SArnd Bergmann #define stat_inc_cp_count(si)				do { } while (0)
3705d66450e7SArnd Bergmann #define stat_inc_bg_cp_count(si)			do { } while (0)
3706d66450e7SArnd Bergmann #define stat_inc_call_count(si)				do { } while (0)
3707d66450e7SArnd Bergmann #define stat_inc_bggc_count(si)				do { } while (0)
3708274bd9baSChao Yu #define stat_io_skip_bggc_count(sbi)			do { } while (0)
3709274bd9baSChao Yu #define stat_other_skip_bggc_count(sbi)			do { } while (0)
3710d66450e7SArnd Bergmann #define stat_inc_dirty_inode(sbi, type)			do { } while (0)
3711d66450e7SArnd Bergmann #define stat_dec_dirty_inode(sbi, type)			do { } while (0)
3712fc7100eaSHridya Valsaraju #define stat_inc_total_hit(sbi)				do { } while (0)
3713fc7100eaSHridya Valsaraju #define stat_inc_rbtree_node_hit(sbi)			do { } while (0)
3714d66450e7SArnd Bergmann #define stat_inc_largest_node_hit(sbi)			do { } while (0)
3715d66450e7SArnd Bergmann #define stat_inc_cached_node_hit(sbi)			do { } while (0)
3716d66450e7SArnd Bergmann #define stat_inc_inline_xattr(inode)			do { } while (0)
3717d66450e7SArnd Bergmann #define stat_dec_inline_xattr(inode)			do { } while (0)
3718d66450e7SArnd Bergmann #define stat_inc_inline_inode(inode)			do { } while (0)
3719d66450e7SArnd Bergmann #define stat_dec_inline_inode(inode)			do { } while (0)
3720d66450e7SArnd Bergmann #define stat_inc_inline_dir(inode)			do { } while (0)
3721d66450e7SArnd Bergmann #define stat_dec_inline_dir(inode)			do { } while (0)
37224c8ff709SChao Yu #define stat_inc_compr_inode(inode)			do { } while (0)
37234c8ff709SChao Yu #define stat_dec_compr_inode(inode)			do { } while (0)
37244c8ff709SChao Yu #define stat_add_compr_blocks(inode, blocks)		do { } while (0)
37254c8ff709SChao Yu #define stat_sub_compr_blocks(inode, blocks)		do { } while (0)
3726d66450e7SArnd Bergmann #define stat_inc_atomic_write(inode)			do { } while (0)
3727d66450e7SArnd Bergmann #define stat_dec_atomic_write(inode)			do { } while (0)
3728d66450e7SArnd Bergmann #define stat_update_max_atomic_write(inode)		do { } while (0)
3729d66450e7SArnd Bergmann #define stat_inc_volatile_write(inode)			do { } while (0)
3730d66450e7SArnd Bergmann #define stat_dec_volatile_write(inode)			do { } while (0)
3731d66450e7SArnd Bergmann #define stat_update_max_volatile_write(inode)		do { } while (0)
3732b63e7be5SChao Yu #define stat_inc_meta_count(sbi, blkaddr)		do { } while (0)
3733d66450e7SArnd Bergmann #define stat_inc_seg_type(sbi, curseg)			do { } while (0)
3734d66450e7SArnd Bergmann #define stat_inc_block_count(sbi, curseg)		do { } while (0)
3735d66450e7SArnd Bergmann #define stat_inc_inplace_blocks(sbi)			do { } while (0)
3736d66450e7SArnd Bergmann #define stat_inc_seg_count(sbi, type, gc_type)		do { } while (0)
3737d66450e7SArnd Bergmann #define stat_inc_tot_blk_count(si, blks)		do { } while (0)
3738d66450e7SArnd Bergmann #define stat_inc_data_blk_count(sbi, blks, gc_type)	do { } while (0)
3739d66450e7SArnd Bergmann #define stat_inc_node_blk_count(sbi, blks, gc_type)	do { } while (0)
374039a53e0cSJaegeuk Kim 
374139a53e0cSJaegeuk Kim static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
374239a53e0cSJaegeuk Kim static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
374321acc07dSGreg Kroah-Hartman static inline void __init f2fs_create_root_stats(void) { }
37444589d25dSNamjae Jeon static inline void f2fs_destroy_root_stats(void) { }
374548abe91aSYueHaibing static inline void f2fs_update_sit_info(struct f2fs_sb_info *sbi) {}
374639a53e0cSJaegeuk Kim #endif
374739a53e0cSJaegeuk Kim 
374839a53e0cSJaegeuk Kim extern const struct file_operations f2fs_dir_operations;
37492c2eb7a3SDaniel Rosenberg #ifdef CONFIG_UNICODE
37502c2eb7a3SDaniel Rosenberg extern const struct dentry_operations f2fs_dentry_ops;
37512c2eb7a3SDaniel Rosenberg #endif
375239a53e0cSJaegeuk Kim extern const struct file_operations f2fs_file_operations;
375339a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_file_inode_operations;
375439a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_dblock_aops;
375539a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_node_aops;
375639a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_meta_aops;
375739a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_dir_inode_operations;
375839a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_symlink_inode_operations;
3759cbaf042aSJaegeuk Kim extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
376039a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_special_inode_operations;
37614d57b86dSChao Yu extern struct kmem_cache *f2fs_inode_entry_slab;
37621001b347SHuajun Li 
3763e18c65b2SHuajun Li /*
3764e18c65b2SHuajun Li  * inline.c
3765e18c65b2SHuajun Li  */
3766cac5a3d8SDongOh Shin bool f2fs_may_inline_data(struct inode *inode);
3767cac5a3d8SDongOh Shin bool f2fs_may_inline_dentry(struct inode *inode);
37684d57b86dSChao Yu void f2fs_do_read_inline_data(struct page *page, struct page *ipage);
37694d57b86dSChao Yu void f2fs_truncate_inline_inode(struct inode *inode,
37704d57b86dSChao Yu 						struct page *ipage, u64 from);
3771cac5a3d8SDongOh Shin int f2fs_read_inline_data(struct inode *inode, struct page *page);
3772cac5a3d8SDongOh Shin int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
3773cac5a3d8SDongOh Shin int f2fs_convert_inline_inode(struct inode *inode);
3774b06af2afSJaegeuk Kim int f2fs_try_convert_inline_dir(struct inode *dir, struct dentry *dentry);
3775cac5a3d8SDongOh Shin int f2fs_write_inline_data(struct inode *inode, struct page *page);
37769627a7b3SChao Yu int f2fs_recover_inline_data(struct inode *inode, struct page *npage);
37774d57b86dSChao Yu struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
377843c780baSEric Biggers 					const struct f2fs_filename *fname,
377943c780baSEric Biggers 					struct page **res_page);
37804d57b86dSChao Yu int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent,
3781cac5a3d8SDongOh Shin 			struct page *ipage);
378243c780baSEric Biggers int f2fs_add_inline_entry(struct inode *dir, const struct f2fs_filename *fname,
3783cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
37844d57b86dSChao Yu void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry,
37854d57b86dSChao Yu 				struct page *page, struct inode *dir,
37864d57b86dSChao Yu 				struct inode *inode);
3787cac5a3d8SDongOh Shin bool f2fs_empty_inline_dir(struct inode *dir);
3788cac5a3d8SDongOh Shin int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
3789cac5a3d8SDongOh Shin 			struct fscrypt_str *fstr);
3790cac5a3d8SDongOh Shin int f2fs_inline_data_fiemap(struct inode *inode,
3791cac5a3d8SDongOh Shin 			struct fiemap_extent_info *fieinfo,
3792cac5a3d8SDongOh Shin 			__u64 start, __u64 len);
3793cde4de12SJaegeuk Kim 
3794cde4de12SJaegeuk Kim /*
37952658e50dSJaegeuk Kim  * shrinker.c
37962658e50dSJaegeuk Kim  */
3797cac5a3d8SDongOh Shin unsigned long f2fs_shrink_count(struct shrinker *shrink,
3798cac5a3d8SDongOh Shin 			struct shrink_control *sc);
3799cac5a3d8SDongOh Shin unsigned long f2fs_shrink_scan(struct shrinker *shrink,
3800cac5a3d8SDongOh Shin 			struct shrink_control *sc);
3801cac5a3d8SDongOh Shin void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
3802cac5a3d8SDongOh Shin void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
38032658e50dSJaegeuk Kim 
38042658e50dSJaegeuk Kim /*
3805a28ef1f5SChao Yu  * extent_cache.c
3806a28ef1f5SChao Yu  */
38074dada3fdSChao Yu struct rb_entry *f2fs_lookup_rb_tree(struct rb_root_cached *root,
3808004b6862SChao Yu 				struct rb_entry *cached_re, unsigned int ofs);
38094d57b86dSChao Yu struct rb_node **f2fs_lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
38104dada3fdSChao Yu 				struct rb_root_cached *root,
38114dada3fdSChao Yu 				struct rb_node **parent,
38124dada3fdSChao Yu 				unsigned int ofs, bool *leftmost);
38134dada3fdSChao Yu struct rb_entry *f2fs_lookup_rb_tree_ret(struct rb_root_cached *root,
3814004b6862SChao Yu 		struct rb_entry *cached_re, unsigned int ofs,
3815004b6862SChao Yu 		struct rb_entry **prev_entry, struct rb_entry **next_entry,
3816004b6862SChao Yu 		struct rb_node ***insert_p, struct rb_node **insert_parent,
38174dada3fdSChao Yu 		bool force, bool *leftmost);
38184d57b86dSChao Yu bool f2fs_check_rb_tree_consistence(struct f2fs_sb_info *sbi,
38194dada3fdSChao Yu 						struct rb_root_cached *root);
3820cac5a3d8SDongOh Shin unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
3821a6d601f3SChao Yu void f2fs_init_extent_tree(struct inode *inode, struct page *ipage);
3822cac5a3d8SDongOh Shin void f2fs_drop_extent_tree(struct inode *inode);
3823cac5a3d8SDongOh Shin unsigned int f2fs_destroy_extent_node(struct inode *inode);
3824cac5a3d8SDongOh Shin void f2fs_destroy_extent_tree(struct inode *inode);
3825cac5a3d8SDongOh Shin bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
3826cac5a3d8SDongOh Shin 			struct extent_info *ei);
3827cac5a3d8SDongOh Shin void f2fs_update_extent_cache(struct dnode_of_data *dn);
382819b2c30dSChao Yu void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
3829cac5a3d8SDongOh Shin 			pgoff_t fofs, block_t blkaddr, unsigned int len);
38304d57b86dSChao Yu void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi);
38314d57b86dSChao Yu int __init f2fs_create_extent_cache(void);
38324d57b86dSChao Yu void f2fs_destroy_extent_cache(void);
3833a28ef1f5SChao Yu 
3834a28ef1f5SChao Yu /*
38358ceffcb2SChao Yu  * sysfs.c
38368ceffcb2SChao Yu  */
3837dc6b2055SJaegeuk Kim int __init f2fs_init_sysfs(void);
3838dc6b2055SJaegeuk Kim void f2fs_exit_sysfs(void);
3839dc6b2055SJaegeuk Kim int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
3840dc6b2055SJaegeuk Kim void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
38418ceffcb2SChao Yu 
384295ae251fSEric Biggers /* verity.c */
384395ae251fSEric Biggers extern const struct fsverity_operations f2fs_verityops;
384495ae251fSEric Biggers 
38458ceffcb2SChao Yu /*
3846cde4de12SJaegeuk Kim  * crypto support
3847cde4de12SJaegeuk Kim  */
38481958593eSJaegeuk Kim static inline bool f2fs_encrypted_file(struct inode *inode)
38491958593eSJaegeuk Kim {
385062230e0dSChandan Rajendra 	return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
38511958593eSJaegeuk Kim }
38521958593eSJaegeuk Kim 
3853cde4de12SJaegeuk Kim static inline void f2fs_set_encrypted_inode(struct inode *inode)
3854cde4de12SJaegeuk Kim {
3855643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
3856cde4de12SJaegeuk Kim 	file_set_encrypt(inode);
38579149a5ebSChao Yu 	f2fs_set_inode_flags(inode);
3858cde4de12SJaegeuk Kim #endif
3859cde4de12SJaegeuk Kim }
3860cde4de12SJaegeuk Kim 
38616dbb1796SEric Biggers /*
38626dbb1796SEric Biggers  * Returns true if the reads of the inode's data need to undergo some
38636dbb1796SEric Biggers  * postprocessing step, like decryption or authenticity verification.
38646dbb1796SEric Biggers  */
38656dbb1796SEric Biggers static inline bool f2fs_post_read_required(struct inode *inode)
3866cde4de12SJaegeuk Kim {
38674c8ff709SChao Yu 	return f2fs_encrypted_file(inode) || fsverity_active(inode) ||
38684c8ff709SChao Yu 		f2fs_compressed_file(inode);
38694c8ff709SChao Yu }
38704c8ff709SChao Yu 
38714c8ff709SChao Yu /*
38724c8ff709SChao Yu  * compress.c
38734c8ff709SChao Yu  */
38744c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
38754c8ff709SChao Yu bool f2fs_is_compressed_page(struct page *page);
38764c8ff709SChao Yu struct page *f2fs_compress_control_page(struct page *page);
38774c8ff709SChao Yu int f2fs_prepare_compress_overwrite(struct inode *inode,
38784c8ff709SChao Yu 			struct page **pagep, pgoff_t index, void **fsdata);
38794c8ff709SChao Yu bool f2fs_compress_write_end(struct inode *inode, void *fsdata,
38804c8ff709SChao Yu 					pgoff_t index, unsigned copied);
38813265d3dbSChao Yu int f2fs_truncate_partial_cluster(struct inode *inode, u64 from, bool lock);
38824c8ff709SChao Yu void f2fs_compress_write_end_io(struct bio *bio, struct page *page);
38834c8ff709SChao Yu bool f2fs_is_compress_backend_ready(struct inode *inode);
38845e6bbde9SChao Yu int f2fs_init_compress_mempool(void);
38855e6bbde9SChao Yu void f2fs_destroy_compress_mempool(void);
38864c8ff709SChao Yu void f2fs_decompress_pages(struct bio *bio, struct page *page, bool verity);
38874c8ff709SChao Yu bool f2fs_cluster_is_empty(struct compress_ctx *cc);
38884c8ff709SChao Yu bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index);
38894c8ff709SChao Yu void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct page *page);
38904c8ff709SChao Yu int f2fs_write_multi_pages(struct compress_ctx *cc,
38914c8ff709SChao Yu 						int *submitted,
38924c8ff709SChao Yu 						struct writeback_control *wbc,
38934c8ff709SChao Yu 						enum iostat_type io_type);
38944c8ff709SChao Yu int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index);
38954c8ff709SChao Yu int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
38964c8ff709SChao Yu 				unsigned nr_pages, sector_t *last_block_in_bio,
38970683728aSChao Yu 				bool is_readahead, bool for_write);
38984c8ff709SChao Yu struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc);
38994c8ff709SChao Yu void f2fs_free_dic(struct decompress_io_ctx *dic);
39004c8ff709SChao Yu void f2fs_decompress_end_io(struct page **rpages,
39014c8ff709SChao Yu 			unsigned int cluster_size, bool err, bool verity);
39024c8ff709SChao Yu int f2fs_init_compress_ctx(struct compress_ctx *cc);
39034c8ff709SChao Yu void f2fs_destroy_compress_ctx(struct compress_ctx *cc);
39044c8ff709SChao Yu void f2fs_init_compress_info(struct f2fs_sb_info *sbi);
39054c8ff709SChao Yu #else
39064c8ff709SChao Yu static inline bool f2fs_is_compressed_page(struct page *page) { return false; }
39074c8ff709SChao Yu static inline bool f2fs_is_compress_backend_ready(struct inode *inode)
39084c8ff709SChao Yu {
39094c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
39104c8ff709SChao Yu 		return true;
39114c8ff709SChao Yu 	/* not support compression */
39124c8ff709SChao Yu 	return false;
39134c8ff709SChao Yu }
39144c8ff709SChao Yu static inline struct page *f2fs_compress_control_page(struct page *page)
39154c8ff709SChao Yu {
39164c8ff709SChao Yu 	WARN_ON_ONCE(1);
39174c8ff709SChao Yu 	return ERR_PTR(-EINVAL);
39184c8ff709SChao Yu }
39195e6bbde9SChao Yu static inline int f2fs_init_compress_mempool(void) { return 0; }
39205e6bbde9SChao Yu static inline void f2fs_destroy_compress_mempool(void) { }
39214c8ff709SChao Yu #endif
39224c8ff709SChao Yu 
39234c8ff709SChao Yu static inline void set_compress_context(struct inode *inode)
39244c8ff709SChao Yu {
39254c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
39264c8ff709SChao Yu 
39274c8ff709SChao Yu 	F2FS_I(inode)->i_compress_algorithm =
39284c8ff709SChao Yu 			F2FS_OPTION(sbi).compress_algorithm;
39294c8ff709SChao Yu 	F2FS_I(inode)->i_log_cluster_size =
39304c8ff709SChao Yu 			F2FS_OPTION(sbi).compress_log_size;
39314c8ff709SChao Yu 	F2FS_I(inode)->i_cluster_size =
39324c8ff709SChao Yu 			1 << F2FS_I(inode)->i_log_cluster_size;
39334c8ff709SChao Yu 	F2FS_I(inode)->i_flags |= F2FS_COMPR_FL;
39344c8ff709SChao Yu 	set_inode_flag(inode, FI_COMPRESSED_FILE);
39354c8ff709SChao Yu 	stat_inc_compr_inode(inode);
3936530e0704SChao Yu 	f2fs_mark_inode_dirty_sync(inode, true);
39374c8ff709SChao Yu }
39384c8ff709SChao Yu 
39394c8ff709SChao Yu static inline u64 f2fs_disable_compressed_file(struct inode *inode)
39404c8ff709SChao Yu {
39414c8ff709SChao Yu 	struct f2fs_inode_info *fi = F2FS_I(inode);
39424c8ff709SChao Yu 
39434c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
39444c8ff709SChao Yu 		return 0;
3945aa576970SChao Yu 	if (S_ISREG(inode->i_mode)) {
39466cfdf15fSChao Yu 		if (get_dirty_pages(inode))
39476cfdf15fSChao Yu 			return 1;
39484c8ff709SChao Yu 		if (fi->i_compr_blocks)
39494c8ff709SChao Yu 			return fi->i_compr_blocks;
3950aa576970SChao Yu 	}
39514c8ff709SChao Yu 
39524c8ff709SChao Yu 	fi->i_flags &= ~F2FS_COMPR_FL;
39534c8ff709SChao Yu 	stat_dec_compr_inode(inode);
395496f5b4faSChao Yu 	clear_inode_flag(inode, FI_COMPRESSED_FILE);
3955530e0704SChao Yu 	f2fs_mark_inode_dirty_sync(inode, true);
39564c8ff709SChao Yu 	return 0;
3957cde4de12SJaegeuk Kim }
3958cde4de12SJaegeuk Kim 
3959ccd31cb2SSheng Yong #define F2FS_FEATURE_FUNCS(name, flagname) \
39607beb01f7SChao Yu static inline int f2fs_sb_has_##name(struct f2fs_sb_info *sbi) \
3961ccd31cb2SSheng Yong { \
39627beb01f7SChao Yu 	return F2FS_HAS_FEATURE(sbi, F2FS_FEATURE_##flagname); \
3963cde4de12SJaegeuk Kim }
3964f424f664SJaegeuk Kim 
3965ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(encrypt, ENCRYPT);
3966ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(blkzoned, BLKZONED);
3967ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(extra_attr, EXTRA_ATTR);
3968ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(project_quota, PRJQUOTA);
3969ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_chksum, INODE_CHKSUM);
3970ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
3971ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
3972ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
3973b7c409deSSheng Yong F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
397495ae251fSEric Biggers F2FS_FEATURE_FUNCS(verity, VERITY);
3975d440c52dSJunling Zheng F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
39765aba5430SDaniel Rosenberg F2FS_FEATURE_FUNCS(casefold, CASEFOLD);
39774c8ff709SChao Yu F2FS_FEATURE_FUNCS(compression, COMPRESSION);
39781c1d35dfSChao Yu 
3979178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
398095175dafSDamien Le Moal static inline bool f2fs_blkz_is_seq(struct f2fs_sb_info *sbi, int devi,
398195175dafSDamien Le Moal 				    block_t blkaddr)
3982178053e2SDamien Le Moal {
3983178053e2SDamien Le Moal 	unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
3984178053e2SDamien Le Moal 
398595175dafSDamien Le Moal 	return test_bit(zno, FDEV(devi).blkz_seq);
3986178053e2SDamien Le Moal }
3987178053e2SDamien Le Moal #endif
3988178053e2SDamien Le Moal 
39897d20c8abSChao Yu static inline bool f2fs_hw_should_discard(struct f2fs_sb_info *sbi)
399096ba2decSDamien Le Moal {
39917beb01f7SChao Yu 	return f2fs_sb_has_blkzoned(sbi);
39927d20c8abSChao Yu }
399396ba2decSDamien Le Moal 
39947f3d7719SDamien Le Moal static inline bool f2fs_bdev_support_discard(struct block_device *bdev)
39957f3d7719SDamien Le Moal {
39967f3d7719SDamien Le Moal 	return blk_queue_discard(bdev_get_queue(bdev)) ||
39977f3d7719SDamien Le Moal 	       bdev_is_zoned(bdev);
39987f3d7719SDamien Le Moal }
39997f3d7719SDamien Le Moal 
40007d20c8abSChao Yu static inline bool f2fs_hw_support_discard(struct f2fs_sb_info *sbi)
40017d20c8abSChao Yu {
40027f3d7719SDamien Le Moal 	int i;
40037f3d7719SDamien Le Moal 
40047f3d7719SDamien Le Moal 	if (!f2fs_is_multi_device(sbi))
40057f3d7719SDamien Le Moal 		return f2fs_bdev_support_discard(sbi->sb->s_bdev);
40067f3d7719SDamien Le Moal 
40077f3d7719SDamien Le Moal 	for (i = 0; i < sbi->s_ndevs; i++)
40087f3d7719SDamien Le Moal 		if (f2fs_bdev_support_discard(FDEV(i).bdev))
40097f3d7719SDamien Le Moal 			return true;
40107f3d7719SDamien Le Moal 	return false;
40117d20c8abSChao Yu }
40127d20c8abSChao Yu 
40137d20c8abSChao Yu static inline bool f2fs_realtime_discard_enable(struct f2fs_sb_info *sbi)
40147d20c8abSChao Yu {
40157d20c8abSChao Yu 	return (test_opt(sbi, DISCARD) && f2fs_hw_support_discard(sbi)) ||
40167d20c8abSChao Yu 					f2fs_hw_should_discard(sbi);
401752763a4bSJaegeuk Kim }
401852763a4bSJaegeuk Kim 
4019f824deb5SChao Yu static inline bool f2fs_hw_is_readonly(struct f2fs_sb_info *sbi)
4020f824deb5SChao Yu {
4021f824deb5SChao Yu 	int i;
4022f824deb5SChao Yu 
4023f824deb5SChao Yu 	if (!f2fs_is_multi_device(sbi))
4024f824deb5SChao Yu 		return bdev_read_only(sbi->sb->s_bdev);
4025f824deb5SChao Yu 
4026f824deb5SChao Yu 	for (i = 0; i < sbi->s_ndevs; i++)
4027f824deb5SChao Yu 		if (bdev_read_only(FDEV(i).bdev))
4028f824deb5SChao Yu 			return true;
4029f824deb5SChao Yu 	return false;
4030f824deb5SChao Yu }
4031f824deb5SChao Yu 
4032b0332a0fSChao Yu static inline bool f2fs_lfs_mode(struct f2fs_sb_info *sbi)
403352763a4bSJaegeuk Kim {
4034b0332a0fSChao Yu 	return F2FS_OPTION(sbi).fs_mode == FS_MODE_LFS;
403552763a4bSJaegeuk Kim }
403652763a4bSJaegeuk Kim 
40378c7d4b57SChao Yu static inline bool f2fs_may_encrypt(struct inode *dir, struct inode *inode)
4038fcc85a4dSJaegeuk Kim {
4039643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
40408c7d4b57SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
4041886f56f9SAl Viro 	umode_t mode = inode->i_mode;
4042fcc85a4dSJaegeuk Kim 
40438c7d4b57SChao Yu 	/*
40448c7d4b57SChao Yu 	 * If the directory encrypted or dummy encryption enabled,
40458c7d4b57SChao Yu 	 * then we should encrypt the inode.
40468c7d4b57SChao Yu 	 */
40478c7d4b57SChao Yu 	if (IS_ENCRYPTED(dir) || DUMMY_ENCRYPTION_ENABLED(sbi))
4048fcc85a4dSJaegeuk Kim 		return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
4049fcc85a4dSJaegeuk Kim #endif
40508c7d4b57SChao Yu 	return false;
4051fcc85a4dSJaegeuk Kim }
4052fcc85a4dSJaegeuk Kim 
40534c8ff709SChao Yu static inline bool f2fs_may_compress(struct inode *inode)
40544c8ff709SChao Yu {
40554c8ff709SChao Yu 	if (IS_SWAPFILE(inode) || f2fs_is_pinned_file(inode) ||
40564c8ff709SChao Yu 				f2fs_is_atomic_file(inode) ||
40574c8ff709SChao Yu 				f2fs_is_volatile_file(inode))
40584c8ff709SChao Yu 		return false;
40594c8ff709SChao Yu 	return S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode);
40604c8ff709SChao Yu }
40614c8ff709SChao Yu 
40624c8ff709SChao Yu static inline void f2fs_i_compr_blocks_update(struct inode *inode,
40634c8ff709SChao Yu 						u64 blocks, bool add)
40644c8ff709SChao Yu {
40654c8ff709SChao Yu 	int diff = F2FS_I(inode)->i_cluster_size - blocks;
40664c8ff709SChao Yu 
4067ef8d563fSChao Yu 	/* don't update i_compr_blocks if saved blocks were released */
4068ef8d563fSChao Yu 	if (!add && !F2FS_I(inode)->i_compr_blocks)
4069ef8d563fSChao Yu 		return;
4070ef8d563fSChao Yu 
40714c8ff709SChao Yu 	if (add) {
40724c8ff709SChao Yu 		F2FS_I(inode)->i_compr_blocks += diff;
40734c8ff709SChao Yu 		stat_add_compr_blocks(inode, diff);
40744c8ff709SChao Yu 	} else {
40754c8ff709SChao Yu 		F2FS_I(inode)->i_compr_blocks -= diff;
40764c8ff709SChao Yu 		stat_sub_compr_blocks(inode, diff);
40774c8ff709SChao Yu 	}
40784c8ff709SChao Yu 	f2fs_mark_inode_dirty_sync(inode, true);
40794c8ff709SChao Yu }
40804c8ff709SChao Yu 
4081f847c699SChao Yu static inline int block_unaligned_IO(struct inode *inode,
4082f847c699SChao Yu 				struct kiocb *iocb, struct iov_iter *iter)
4083b91050a8SHyunchul Lee {
4084f847c699SChao Yu 	unsigned int i_blkbits = READ_ONCE(inode->i_blkbits);
4085f847c699SChao Yu 	unsigned int blocksize_mask = (1 << i_blkbits) - 1;
4086f847c699SChao Yu 	loff_t offset = iocb->ki_pos;
4087f847c699SChao Yu 	unsigned long align = offset | iov_iter_alignment(iter);
4088f847c699SChao Yu 
4089f847c699SChao Yu 	return align & blocksize_mask;
4090f847c699SChao Yu }
4091f847c699SChao Yu 
4092f847c699SChao Yu static inline int allow_outplace_dio(struct inode *inode,
4093f847c699SChao Yu 				struct kiocb *iocb, struct iov_iter *iter)
4094f847c699SChao Yu {
4095f847c699SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4096f847c699SChao Yu 	int rw = iov_iter_rw(iter);
4097f847c699SChao Yu 
4098b0332a0fSChao Yu 	return (f2fs_lfs_mode(sbi) && (rw == WRITE) &&
4099f847c699SChao Yu 				!block_unaligned_IO(inode, iocb, iter));
4100f847c699SChao Yu }
4101f847c699SChao Yu 
4102f847c699SChao Yu static inline bool f2fs_force_buffered_io(struct inode *inode,
4103f847c699SChao Yu 				struct kiocb *iocb, struct iov_iter *iter)
4104f847c699SChao Yu {
4105f847c699SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4106f847c699SChao Yu 	int rw = iov_iter_rw(iter);
4107f847c699SChao Yu 
4108f847c699SChao Yu 	if (f2fs_post_read_required(inode))
4109f847c699SChao Yu 		return true;
41100916878dSDamien Le Moal 	if (f2fs_is_multi_device(sbi))
4111f847c699SChao Yu 		return true;
4112f847c699SChao Yu 	/*
4113f847c699SChao Yu 	 * for blkzoned device, fallback direct IO to buffered IO, so
4114f847c699SChao Yu 	 * all IOs can be serialized by log-structured write.
4115f847c699SChao Yu 	 */
41167beb01f7SChao Yu 	if (f2fs_sb_has_blkzoned(sbi))
4117f847c699SChao Yu 		return true;
4118b0332a0fSChao Yu 	if (f2fs_lfs_mode(sbi) && (rw == WRITE)) {
41199720ee80SChao Yu 		if (block_unaligned_IO(inode, iocb, iter))
4120f847c699SChao Yu 			return true;
41219720ee80SChao Yu 		if (F2FS_IO_ALIGNED(sbi))
41229720ee80SChao Yu 			return true;
41239720ee80SChao Yu 	}
41244969c06aSJaegeuk Kim 	if (is_sbi_flag_set(F2FS_I_SB(inode), SBI_CP_DISABLED) &&
41253ee0c5d3SChao Yu 					!IS_SWAPFILE(inode))
41264354994fSDaniel Rosenberg 		return true;
41274354994fSDaniel Rosenberg 
4128f847c699SChao Yu 	return false;
4129b91050a8SHyunchul Lee }
4130b91050a8SHyunchul Lee 
413183a3bfdbSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
4132d494500aSChao Yu extern void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate,
4133d494500aSChao Yu 							unsigned int type);
413483a3bfdbSJaegeuk Kim #else
4135d494500aSChao Yu #define f2fs_build_fault_attr(sbi, rate, type)		do { } while (0)
413683a3bfdbSJaegeuk Kim #endif
413783a3bfdbSJaegeuk Kim 
4138af033b2aSChao Yu static inline bool is_journalled_quota(struct f2fs_sb_info *sbi)
4139af033b2aSChao Yu {
4140af033b2aSChao Yu #ifdef CONFIG_QUOTA
41417beb01f7SChao Yu 	if (f2fs_sb_has_quota_ino(sbi))
4142af033b2aSChao Yu 		return true;
4143af033b2aSChao Yu 	if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
4144af033b2aSChao Yu 		F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
4145af033b2aSChao Yu 		F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
4146af033b2aSChao Yu 		return true;
4147af033b2aSChao Yu #endif
4148af033b2aSChao Yu 	return false;
4149af033b2aSChao Yu }
41500af725fcSJaegeuk Kim 
415110f966bbSChao Yu #define EFSBADCRC	EBADMSG		/* Bad CRC detected */
415210f966bbSChao Yu #define EFSCORRUPTED	EUCLEAN		/* Filesystem is corrupted */
415310f966bbSChao Yu 
4154e1074d4bSChao Yu #endif /* _LINUX_F2FS_H */
4155