xref: /openbmc/linux/fs/f2fs/f2fs.h (revision b5639bb4313b9d455fc9fc4768d23a5e4ca8cb9d)
1d29fbcdbSNishad Kamdar /* SPDX-License-Identifier: GPL-2.0 */
20a8165d7SJaegeuk Kim /*
339a53e0cSJaegeuk Kim  * fs/f2fs/f2fs.h
439a53e0cSJaegeuk Kim  *
539a53e0cSJaegeuk Kim  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
639a53e0cSJaegeuk Kim  *             http://www.samsung.com/
739a53e0cSJaegeuk Kim  */
839a53e0cSJaegeuk Kim #ifndef _LINUX_F2FS_H
939a53e0cSJaegeuk Kim #define _LINUX_F2FS_H
1039a53e0cSJaegeuk Kim 
11f847c699SChao Yu #include <linux/uio.h>
1239a53e0cSJaegeuk Kim #include <linux/types.h>
1339a53e0cSJaegeuk Kim #include <linux/page-flags.h>
1439a53e0cSJaegeuk Kim #include <linux/buffer_head.h>
1539a53e0cSJaegeuk Kim #include <linux/slab.h>
1639a53e0cSJaegeuk Kim #include <linux/crc32.h>
1739a53e0cSJaegeuk Kim #include <linux/magic.h>
18c2d715d1SJaegeuk Kim #include <linux/kobject.h>
197bd59381SGu Zheng #include <linux/sched.h>
207c2e5963SJaegeuk Kim #include <linux/cred.h>
2139307a8eSJaegeuk Kim #include <linux/vmalloc.h>
22740432f8SJaegeuk Kim #include <linux/bio.h>
23d0239e1bSJaegeuk Kim #include <linux/blkdev.h>
240abd675eSChao Yu #include <linux/quotaops.h>
25c6a564ffSChristoph Hellwig #include <linux/part_stat.h>
2643b6573bSKeith Mok #include <crypto/hash.h>
2739a53e0cSJaegeuk Kim 
28734f0d24SDave Chinner #include <linux/fscrypt.h>
2995ae251fSEric Biggers #include <linux/fsverity.h>
30734f0d24SDave Chinner 
3151dcbdacSMatthew Wilcox (Oracle) struct pagevec;
3251dcbdacSMatthew Wilcox (Oracle) 
335d56b671SJaegeuk Kim #ifdef CONFIG_F2FS_CHECK_FS
349850cf4aSJaegeuk Kim #define f2fs_bug_on(sbi, condition)	BUG_ON(condition)
355d56b671SJaegeuk Kim #else
369850cf4aSJaegeuk Kim #define f2fs_bug_on(sbi, condition)					\
379850cf4aSJaegeuk Kim 	do {								\
38db489652SYangtao Li 		if (WARN_ON(condition))					\
39caf0047eSChao Yu 			set_sbi_flag(sbi, SBI_NEED_FSCK);		\
409850cf4aSJaegeuk Kim 	} while (0)
415d56b671SJaegeuk Kim #endif
425d56b671SJaegeuk Kim 
432c63feadSJaegeuk Kim enum {
442c63feadSJaegeuk Kim 	FAULT_KMALLOC,
45628b3d14SChao Yu 	FAULT_KVMALLOC,
46c41f3cc3SJaegeuk Kim 	FAULT_PAGE_ALLOC,
4701eccef7SChao Yu 	FAULT_PAGE_GET,
48b96d9b3bSChao Yu 	FAULT_ALLOC_BIO,	/* it's obsolete due to bio_alloc() will never fail */
49cb78942bSJaegeuk Kim 	FAULT_ALLOC_NID,
50cb78942bSJaegeuk Kim 	FAULT_ORPHAN,
51cb78942bSJaegeuk Kim 	FAULT_BLOCK,
52cb78942bSJaegeuk Kim 	FAULT_DIR_DEPTH,
5353aa6bbfSJaegeuk Kim 	FAULT_EVICT_INODE,
5414b44d23SJaegeuk Kim 	FAULT_TRUNCATE,
556f5c2ed0SChao Yu 	FAULT_READ_IO,
560f348028SChao Yu 	FAULT_CHECKPOINT,
57b83dcfe6SChao Yu 	FAULT_DISCARD,
586f5c2ed0SChao Yu 	FAULT_WRITE_IO,
5932410577SChao Yu 	FAULT_SLAB_ALLOC,
6010a26878SChao Yu 	FAULT_DQUOT_INIT,
613e020389SChao Yu 	FAULT_LOCK_OP,
622c63feadSJaegeuk Kim 	FAULT_MAX,
632c63feadSJaegeuk Kim };
642c63feadSJaegeuk Kim 
657fa750a1SArnd Bergmann #ifdef CONFIG_F2FS_FAULT_INJECTION
66d494500aSChao Yu #define F2FS_ALL_FAULT_TYPE		((1 << FAULT_MAX) - 1)
67d494500aSChao Yu 
6808796897SSheng Yong struct f2fs_fault_info {
6908796897SSheng Yong 	atomic_t inject_ops;
7008796897SSheng Yong 	unsigned int inject_rate;
7108796897SSheng Yong 	unsigned int inject_type;
7208796897SSheng Yong };
7308796897SSheng Yong 
7419880e6eSAlexey Dobriyan extern const char *f2fs_fault_name[FAULT_MAX];
7568afcf2dSTomohiro Kusumi #define IS_FAULT_SET(fi, type) ((fi)->inject_type & (1 << (type)))
762c63feadSJaegeuk Kim #endif
772c63feadSJaegeuk Kim 
7839a53e0cSJaegeuk Kim /*
7939a53e0cSJaegeuk Kim  * For mount options
8039a53e0cSJaegeuk Kim  */
8139a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_ROLL_FORWARD	0x00000002
8239a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISCARD		0x00000004
8339a53e0cSJaegeuk Kim #define F2FS_MOUNT_NOHEAP		0x00000008
8439a53e0cSJaegeuk Kim #define F2FS_MOUNT_XATTR_USER		0x00000010
8539a53e0cSJaegeuk Kim #define F2FS_MOUNT_POSIX_ACL		0x00000020
8639a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_EXT_IDENTIFY	0x00000040
87444c580fSJaegeuk Kim #define F2FS_MOUNT_INLINE_XATTR		0x00000080
881001b347SHuajun Li #define F2FS_MOUNT_INLINE_DATA		0x00000100
8934d67debSChao Yu #define F2FS_MOUNT_INLINE_DENTRY	0x00000200
9034d67debSChao Yu #define F2FS_MOUNT_FLUSH_MERGE		0x00000400
9134d67debSChao Yu #define F2FS_MOUNT_NOBARRIER		0x00000800
92d5053a34SJaegeuk Kim #define F2FS_MOUNT_FASTBOOT		0x00001000
9389672159SChao Yu #define F2FS_MOUNT_EXTENT_CACHE		0x00002000
94343f40f0SChao Yu #define F2FS_MOUNT_DATA_FLUSH		0x00008000
9573faec4dSJaegeuk Kim #define F2FS_MOUNT_FAULT_INJECTION	0x00010000
960abd675eSChao Yu #define F2FS_MOUNT_USRQUOTA		0x00080000
970abd675eSChao Yu #define F2FS_MOUNT_GRPQUOTA		0x00100000
985c57132eSChao Yu #define F2FS_MOUNT_PRJQUOTA		0x00200000
994b2414d0SChao Yu #define F2FS_MOUNT_QUOTA		0x00400000
1006afc662eSChao Yu #define F2FS_MOUNT_INLINE_XATTR_SIZE	0x00800000
1017e65be49SJaegeuk Kim #define F2FS_MOUNT_RESERVE_ROOT		0x01000000
1024354994fSDaniel Rosenberg #define F2FS_MOUNT_DISABLE_CHECKPOINT	0x02000000
103a9117ecaSChao Yu #define F2FS_MOUNT_NORECOVERY		0x04000000
104093749e2SChao Yu #define F2FS_MOUNT_ATGC			0x08000000
105261eeb9cSDaeho Jeong #define F2FS_MOUNT_MERGE_CHECKPOINT	0x10000000
1065911d2d1SChao Yu #define	F2FS_MOUNT_GC_MERGE		0x20000000
1076ce19affSChao Yu #define F2FS_MOUNT_COMPRESS_CACHE	0x40000000
10839a53e0cSJaegeuk Kim 
10963189b78SChao Yu #define F2FS_OPTION(sbi)	((sbi)->mount_opt)
11063189b78SChao Yu #define clear_opt(sbi, option)	(F2FS_OPTION(sbi).opt &= ~F2FS_MOUNT_##option)
11163189b78SChao Yu #define set_opt(sbi, option)	(F2FS_OPTION(sbi).opt |= F2FS_MOUNT_##option)
11263189b78SChao Yu #define test_opt(sbi, option)	(F2FS_OPTION(sbi).opt & F2FS_MOUNT_##option)
11339a53e0cSJaegeuk Kim 
11439a53e0cSJaegeuk Kim #define ver_after(a, b)	(typecheck(unsigned long long, a) &&		\
11539a53e0cSJaegeuk Kim 		typecheck(unsigned long long, b) &&			\
11639a53e0cSJaegeuk Kim 		((long long)((a) - (b)) > 0))
11739a53e0cSJaegeuk Kim 
118a9841c4dSJaegeuk Kim typedef u32 block_t;	/*
119a9841c4dSJaegeuk Kim 			 * should not change u32, since it is the on-disk block
120a9841c4dSJaegeuk Kim 			 * address format, __le32.
121a9841c4dSJaegeuk Kim 			 */
12239a53e0cSJaegeuk Kim typedef u32 nid_t;
12339a53e0cSJaegeuk Kim 
1244c8ff709SChao Yu #define COMPRESS_EXT_NUM		16
1254c8ff709SChao Yu 
126e4544b63STim Murray /*
127e4544b63STim Murray  * An implementation of an rwsem that is explicitly unfair to readers. This
128e4544b63STim Murray  * prevents priority inversion when a low-priority reader acquires the read lock
129e4544b63STim Murray  * while sleeping on the write lock but the write lock is needed by
130e4544b63STim Murray  * higher-priority clients.
131e4544b63STim Murray  */
132e4544b63STim Murray 
133e4544b63STim Murray struct f2fs_rwsem {
134e4544b63STim Murray         struct rw_semaphore internal_rwsem;
1357f8e249dSJaegeuk Kim #ifdef CONFIG_F2FS_UNFAIR_RWSEM
136e4544b63STim Murray         wait_queue_head_t read_waiters;
1377f8e249dSJaegeuk Kim #endif
138e4544b63STim Murray };
139e4544b63STim Murray 
14039a53e0cSJaegeuk Kim struct f2fs_mount_info {
14139a53e0cSJaegeuk Kim 	unsigned int opt;
14263189b78SChao Yu 	int write_io_size_bits;		/* Write IO size bits */
14363189b78SChao Yu 	block_t root_reserved_blocks;	/* root reserved blocks */
14463189b78SChao Yu 	kuid_t s_resuid;		/* reserved blocks for uid */
14563189b78SChao Yu 	kgid_t s_resgid;		/* reserved blocks for gid */
14663189b78SChao Yu 	int active_logs;		/* # of active logs */
14763189b78SChao Yu 	int inline_xattr_size;		/* inline xattr size */
14863189b78SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
14963189b78SChao Yu 	struct f2fs_fault_info fault_info;	/* For fault injection */
15063189b78SChao Yu #endif
15163189b78SChao Yu #ifdef CONFIG_QUOTA
15263189b78SChao Yu 	/* Names of quota files with journalled quota */
15363189b78SChao Yu 	char *s_qf_names[MAXQUOTAS];
15463189b78SChao Yu 	int s_jquota_fmt;			/* Format of quota to use */
15563189b78SChao Yu #endif
15663189b78SChao Yu 	/* For which write hints are passed down to block layer */
15763189b78SChao Yu 	int alloc_mode;			/* segment allocation policy */
15863189b78SChao Yu 	int fsync_mode;			/* fsync policy */
159b0332a0fSChao Yu 	int fs_mode;			/* fs mode: LFS or ADAPTIVE */
160bbbc34fdSChao Yu 	int bggc_mode;			/* bggc mode: off, on or sync */
1614f993264SChao Yu 	int discard_unit;		/*
1624f993264SChao Yu 					 * discard command's offset/size should
1634f993264SChao Yu 					 * be aligned to this unit: block,
1644f993264SChao Yu 					 * segment or section
1654f993264SChao Yu 					 */
166ac4acb1fSEric Biggers 	struct fscrypt_dummy_policy dummy_enc_policy; /* test dummy encryption */
1671ae18f71SJaegeuk Kim 	block_t unusable_cap_perc;	/* percentage for cap */
1684d3aed70SDaniel Rosenberg 	block_t unusable_cap;		/* Amount of space allowed to be
1694d3aed70SDaniel Rosenberg 					 * unusable when disabling checkpoint
1704d3aed70SDaniel Rosenberg 					 */
1714c8ff709SChao Yu 
1724c8ff709SChao Yu 	/* For compression */
1734c8ff709SChao Yu 	unsigned char compress_algorithm;	/* algorithm type */
174b28f047bSChao Yu 	unsigned char compress_log_size;	/* cluster log size */
1753fde13f8SChao Yu 	unsigned char compress_level;		/* compress level */
176b28f047bSChao Yu 	bool compress_chksum;			/* compressed data chksum */
1774c8ff709SChao Yu 	unsigned char compress_ext_cnt;		/* extension count */
178151b1982SFengnan Chang 	unsigned char nocompress_ext_cnt;		/* nocompress extension count */
179602a16d5SDaeho Jeong 	int compress_mode;			/* compression mode */
1804c8ff709SChao Yu 	unsigned char extensions[COMPRESS_EXT_NUM][F2FS_EXTENSION_LEN];	/* extensions */
181151b1982SFengnan Chang 	unsigned char noextensions[COMPRESS_EXT_NUM][F2FS_EXTENSION_LEN]; /* extensions */
18239a53e0cSJaegeuk Kim };
18339a53e0cSJaegeuk Kim 
184cde4de12SJaegeuk Kim #define F2FS_FEATURE_ENCRYPT		0x0001
1850bfd7a09SDamien Le Moal #define F2FS_FEATURE_BLKZONED		0x0002
186e65ef207SJaegeuk Kim #define F2FS_FEATURE_ATOMIC_WRITE	0x0004
1877a2af766SChao Yu #define F2FS_FEATURE_EXTRA_ATTR		0x0008
1885c57132eSChao Yu #define F2FS_FEATURE_PRJQUOTA		0x0010
189704956ecSChao Yu #define F2FS_FEATURE_INODE_CHKSUM	0x0020
1906afc662eSChao Yu #define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR	0x0040
191234a9689SJaegeuk Kim #define F2FS_FEATURE_QUOTA_INO		0x0080
1921c1d35dfSChao Yu #define F2FS_FEATURE_INODE_CRTIME	0x0100
193b7c409deSSheng Yong #define F2FS_FEATURE_LOST_FOUND		0x0200
19495ae251fSEric Biggers #define F2FS_FEATURE_VERITY		0x0400
195d440c52dSJunling Zheng #define F2FS_FEATURE_SB_CHKSUM		0x0800
1965aba5430SDaniel Rosenberg #define F2FS_FEATURE_CASEFOLD		0x1000
1974c8ff709SChao Yu #define F2FS_FEATURE_COMPRESSION	0x2000
198a7d9fe3cSJaegeuk Kim #define F2FS_FEATURE_RO			0x4000
199cde4de12SJaegeuk Kim 
2007beb01f7SChao Yu #define __F2FS_HAS_FEATURE(raw_super, mask)				\
2017beb01f7SChao Yu 	((raw_super->feature & cpu_to_le32(mask)) != 0)
2027beb01f7SChao Yu #define F2FS_HAS_FEATURE(sbi, mask)	__F2FS_HAS_FEATURE(sbi->raw_super, mask)
2037beb01f7SChao Yu #define F2FS_SET_FEATURE(sbi, mask)					\
2047beb01f7SChao Yu 	(sbi->raw_super->feature |= cpu_to_le32(mask))
2057beb01f7SChao Yu #define F2FS_CLEAR_FEATURE(sbi, mask)					\
2067beb01f7SChao Yu 	(sbi->raw_super->feature &= ~cpu_to_le32(mask))
20776f105a2SJaegeuk Kim 
20839a53e0cSJaegeuk Kim /*
2097c2e5963SJaegeuk Kim  * Default values for user and/or group using reserved blocks
2107c2e5963SJaegeuk Kim  */
2117c2e5963SJaegeuk Kim #define	F2FS_DEF_RESUID		0
2127c2e5963SJaegeuk Kim #define	F2FS_DEF_RESGID		0
2137c2e5963SJaegeuk Kim 
2147c2e5963SJaegeuk Kim /*
21539a53e0cSJaegeuk Kim  * For checkpoint manager
21639a53e0cSJaegeuk Kim  */
21739a53e0cSJaegeuk Kim enum {
21839a53e0cSJaegeuk Kim 	NAT_BITMAP,
21939a53e0cSJaegeuk Kim 	SIT_BITMAP
22039a53e0cSJaegeuk Kim };
22139a53e0cSJaegeuk Kim 
222c473f1a9SChao Yu #define	CP_UMOUNT	0x00000001
223c473f1a9SChao Yu #define	CP_FASTBOOT	0x00000002
224c473f1a9SChao Yu #define	CP_SYNC		0x00000004
225c473f1a9SChao Yu #define	CP_RECOVERY	0x00000008
226c473f1a9SChao Yu #define	CP_DISCARD	0x00000010
2271f43e2adSChao Yu #define CP_TRIMMED	0x00000020
2284354994fSDaniel Rosenberg #define CP_PAUSE	0x00000040
229b4b10061SJaegeuk Kim #define CP_RESIZE 	0x00000080
23075ab4cb8SJaegeuk Kim 
2314ddb1a4dSJaegeuk Kim #define MAX_DISCARD_BLOCKS(sbi)		BLKS_PER_SEC(sbi)
232ecc9aa00SChao Yu #define DEF_MAX_DISCARD_REQUEST		8	/* issue 8 discards per round */
233969d1b18SChao Yu #define DEF_MIN_DISCARD_ISSUE_TIME	50	/* 50 ms, if exists */
234f9d1dcedSYunlei He #define DEF_MID_DISCARD_ISSUE_TIME	500	/* 500 ms, if device busy */
235969d1b18SChao Yu #define DEF_MAX_DISCARD_ISSUE_TIME	60000	/* 60 s, if no candidates */
2368bb4f253SJaegeuk Kim #define DEF_DISCARD_URGENT_UTIL		80	/* do more discard over 80% */
23760b99b48SJaegeuk Kim #define DEF_CP_INTERVAL			60	/* 60 secs */
238dcf25fe8SChao Yu #define DEF_IDLE_INTERVAL		5	/* 5 secs */
2394354994fSDaniel Rosenberg #define DEF_DISABLE_INTERVAL		5	/* 5 secs */
240db610a64SJaegeuk Kim #define DEF_DISABLE_QUICK_INTERVAL	1	/* 1 secs */
24103f2c02dSJaegeuk Kim #define DEF_UMOUNT_DISCARD_TIMEOUT	5	/* 5 secs */
242bba681cbSJaegeuk Kim 
24375ab4cb8SJaegeuk Kim struct cp_control {
24475ab4cb8SJaegeuk Kim 	int reason;
2454b2fecc8SJaegeuk Kim 	__u64 trim_start;
2464b2fecc8SJaegeuk Kim 	__u64 trim_end;
2474b2fecc8SJaegeuk Kim 	__u64 trim_minlen;
24875ab4cb8SJaegeuk Kim };
24975ab4cb8SJaegeuk Kim 
250662befdaSChao Yu /*
251e1da7872SChao Yu  * indicate meta/data type
252662befdaSChao Yu  */
253662befdaSChao Yu enum {
254662befdaSChao Yu 	META_CP,
255662befdaSChao Yu 	META_NAT,
25681c1a0f1SChao Yu 	META_SIT,
2574c521f49SJaegeuk Kim 	META_SSA,
258b63e7be5SChao Yu 	META_MAX,
2594c521f49SJaegeuk Kim 	META_POR,
26093770ab7SChao Yu 	DATA_GENERIC,		/* check range only */
26193770ab7SChao Yu 	DATA_GENERIC_ENHANCE,	/* strong check on range and segment bitmap */
26293770ab7SChao Yu 	DATA_GENERIC_ENHANCE_READ,	/*
26393770ab7SChao Yu 					 * strong check on range and segment
26493770ab7SChao Yu 					 * bitmap but no warning due to race
26593770ab7SChao Yu 					 * condition of read on truncated area
26693770ab7SChao Yu 					 * by extent_cache
26793770ab7SChao Yu 					 */
268e1da7872SChao Yu 	META_GENERIC,
269662befdaSChao Yu };
270662befdaSChao Yu 
2716451e041SJaegeuk Kim /* for the list of ino */
2726451e041SJaegeuk Kim enum {
2736451e041SJaegeuk Kim 	ORPHAN_INO,		/* for orphan ino list */
274fff04f90SJaegeuk Kim 	APPEND_INO,		/* for append ino list */
275fff04f90SJaegeuk Kim 	UPDATE_INO,		/* for update ino list */
2760a007b97SJaegeuk Kim 	TRANS_DIR_INO,		/* for trasactions dir ino list */
27739d787beSChao Yu 	FLUSH_INO,		/* for multiple device flushing */
2786451e041SJaegeuk Kim 	MAX_INO_ENTRY,		/* max. list */
2796451e041SJaegeuk Kim };
2806451e041SJaegeuk Kim 
2816451e041SJaegeuk Kim struct ino_entry {
28239a53e0cSJaegeuk Kim 	struct list_head list;		/* list head */
28339a53e0cSJaegeuk Kim 	nid_t ino;			/* inode number */
28439d787beSChao Yu 	unsigned int dirty_device;	/* dirty device bitmap */
28539a53e0cSJaegeuk Kim };
28639a53e0cSJaegeuk Kim 
2872710fd7eSChao Yu /* for the list of inodes to be GCed */
28806292073SChao Yu struct inode_entry {
28939a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
29039a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
29139a53e0cSJaegeuk Kim };
29239a53e0cSJaegeuk Kim 
29350fa53ecSChao Yu struct fsync_node_entry {
29450fa53ecSChao Yu 	struct list_head list;	/* list head */
29550fa53ecSChao Yu 	struct page *page;	/* warm node page pointer */
29650fa53ecSChao Yu 	unsigned int seq_id;	/* sequence id */
29750fa53ecSChao Yu };
29850fa53ecSChao Yu 
299261eeb9cSDaeho Jeong struct ckpt_req {
300261eeb9cSDaeho Jeong 	struct completion wait;		/* completion for checkpoint done */
301261eeb9cSDaeho Jeong 	struct llist_node llnode;	/* llist_node to be linked in wait queue */
302261eeb9cSDaeho Jeong 	int ret;			/* return code of checkpoint */
303261eeb9cSDaeho Jeong 	ktime_t queue_time;		/* request queued time */
304261eeb9cSDaeho Jeong };
305261eeb9cSDaeho Jeong 
306261eeb9cSDaeho Jeong struct ckpt_req_control {
307261eeb9cSDaeho Jeong 	struct task_struct *f2fs_issue_ckpt;	/* checkpoint task */
308e6592066SDaeho Jeong 	int ckpt_thread_ioprio;			/* checkpoint merge thread ioprio */
309261eeb9cSDaeho Jeong 	wait_queue_head_t ckpt_wait_queue;	/* waiting queue for wake-up */
310261eeb9cSDaeho Jeong 	atomic_t issued_ckpt;		/* # of actually issued ckpts */
311261eeb9cSDaeho Jeong 	atomic_t total_ckpt;		/* # of total ckpts */
312261eeb9cSDaeho Jeong 	atomic_t queued_ckpt;		/* # of queued ckpts */
313261eeb9cSDaeho Jeong 	struct llist_head issue_list;	/* list for command issue */
314261eeb9cSDaeho Jeong 	spinlock_t stat_lock;		/* lock for below checkpoint time stats */
315261eeb9cSDaeho Jeong 	unsigned int cur_time;		/* cur wait time in msec for currently issued checkpoint */
316261eeb9cSDaeho Jeong 	unsigned int peak_time;		/* peak wait time in msec until now */
317261eeb9cSDaeho Jeong };
318261eeb9cSDaeho Jeong 
319a7eeb823SChao Yu /* for the bitmap indicate blocks to be discarded */
3207fd9e544SJaegeuk Kim struct discard_entry {
3217fd9e544SJaegeuk Kim 	struct list_head list;	/* list head */
322a7eeb823SChao Yu 	block_t start_blkaddr;	/* start blockaddr of current segment */
323a7eeb823SChao Yu 	unsigned char discard_map[SIT_VBLOCK_MAP_SIZE];	/* segment discard bitmap */
3247fd9e544SJaegeuk Kim };
3257fd9e544SJaegeuk Kim 
326969d1b18SChao Yu /* default discard granularity of inner discard thread, unit: block count */
327969d1b18SChao Yu #define DEFAULT_DISCARD_GRANULARITY		16
328969d1b18SChao Yu 
329ba48a33eSChao Yu /* max discard pend list number */
330ba48a33eSChao Yu #define MAX_PLIST_NUM		512
331ba48a33eSChao Yu #define plist_idx(blk_num)	((blk_num) >= MAX_PLIST_NUM ?		\
3322f84babfSSheng Yong 					(MAX_PLIST_NUM - 1) : ((blk_num) - 1))
333ba48a33eSChao Yu 
33415469963SJaegeuk Kim enum {
33535ec7d57SChao Yu 	D_PREP,			/* initial */
33635ec7d57SChao Yu 	D_PARTIAL,		/* partially submitted */
33735ec7d57SChao Yu 	D_SUBMIT,		/* all submitted */
33835ec7d57SChao Yu 	D_DONE,			/* finished */
33915469963SJaegeuk Kim };
34015469963SJaegeuk Kim 
341004b6862SChao Yu struct discard_info {
342b01a9201SJaegeuk Kim 	block_t lstart;			/* logical start address */
343b01a9201SJaegeuk Kim 	block_t len;			/* length */
344004b6862SChao Yu 	block_t start;			/* actual start address in dev */
345004b6862SChao Yu };
346004b6862SChao Yu 
347b01a9201SJaegeuk Kim struct discard_cmd {
348004b6862SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
349004b6862SChao Yu 	union {
350004b6862SChao Yu 		struct {
351004b6862SChao Yu 			block_t lstart;	/* logical start address */
352004b6862SChao Yu 			block_t len;	/* length */
353004b6862SChao Yu 			block_t start;	/* actual start address in dev */
354004b6862SChao Yu 		};
355004b6862SChao Yu 		struct discard_info di;	/* discard info */
356004b6862SChao Yu 
357004b6862SChao Yu 	};
358b01a9201SJaegeuk Kim 	struct list_head list;		/* command list */
359b01a9201SJaegeuk Kim 	struct completion wait;		/* compleation */
360c81abe34SJaegeuk Kim 	struct block_device *bdev;	/* bdev */
361ec9895adSChao Yu 	unsigned short ref;		/* reference count */
3629a744b92SChao Yu 	unsigned char state;		/* state */
36372691af6SJaegeuk Kim 	unsigned char queued;		/* queued discard */
364c81abe34SJaegeuk Kim 	int error;			/* bio error */
36535ec7d57SChao Yu 	spinlock_t lock;		/* for state/bio_ref updating */
36635ec7d57SChao Yu 	unsigned short bio_ref;		/* bio reference count */
367275b66b0SChao Yu };
368275b66b0SChao Yu 
36978997b56SChao Yu enum {
37078997b56SChao Yu 	DPOLICY_BG,
37178997b56SChao Yu 	DPOLICY_FORCE,
37278997b56SChao Yu 	DPOLICY_FSTRIM,
37378997b56SChao Yu 	DPOLICY_UMOUNT,
37478997b56SChao Yu 	MAX_DPOLICY,
37578997b56SChao Yu };
37678997b56SChao Yu 
377ecc9aa00SChao Yu struct discard_policy {
37878997b56SChao Yu 	int type;			/* type of discard */
379ecc9aa00SChao Yu 	unsigned int min_interval;	/* used for candidates exist */
380f9d1dcedSYunlei He 	unsigned int mid_interval;	/* used for device busy */
381ecc9aa00SChao Yu 	unsigned int max_interval;	/* used for candidates not exist */
382ecc9aa00SChao Yu 	unsigned int max_requests;	/* # of discards issued per round */
383ecc9aa00SChao Yu 	unsigned int io_aware_gran;	/* minimum granularity discard not be aware of I/O */
384ecc9aa00SChao Yu 	bool io_aware;			/* issue discard in idle time */
385ecc9aa00SChao Yu 	bool sync;			/* submit discard with REQ_SYNC flag */
38620ee4382SChao Yu 	bool ordered;			/* issue discard by lba order */
3876ce48b0cSChao Yu 	bool timeout;			/* discard timeout for put_super */
38878997b56SChao Yu 	unsigned int granularity;	/* discard granularity */
389ecc9aa00SChao Yu };
390ecc9aa00SChao Yu 
3910b54fb84SJaegeuk Kim struct discard_cmd_control {
39215469963SJaegeuk Kim 	struct task_struct *f2fs_issue_discard;	/* discard thread */
39346f84c2cSChao Yu 	struct list_head entry_list;		/* 4KB discard entry list */
394ba48a33eSChao Yu 	struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
39546f84c2cSChao Yu 	struct list_head wait_list;		/* store on-flushing entries */
3968412663dSChao Yu 	struct list_head fstrim_list;		/* in-flight discard from fstrim */
39715469963SJaegeuk Kim 	wait_queue_head_t discard_wait_queue;	/* waiting queue for wake-up */
398969d1b18SChao Yu 	unsigned int discard_wake;		/* to wake up discard thread */
39915469963SJaegeuk Kim 	struct mutex cmd_lock;
400d618ebafSChao Yu 	unsigned int nr_discards;		/* # of discards in the list */
401d618ebafSChao Yu 	unsigned int max_discards;		/* max. discards to be issued */
402d2d8e896SKonstantin Vyshetsky 	unsigned int max_discard_request;	/* max. discard request per round */
403d2d8e896SKonstantin Vyshetsky 	unsigned int min_discard_issue_time;	/* min. interval between discard issue */
404d2d8e896SKonstantin Vyshetsky 	unsigned int mid_discard_issue_time;	/* mid. interval between discard issue */
405d2d8e896SKonstantin Vyshetsky 	unsigned int max_discard_issue_time;	/* max. interval between discard issue */
406969d1b18SChao Yu 	unsigned int discard_granularity;	/* discard granularity */
407d84d1cbdSChao Yu 	unsigned int undiscard_blks;		/* # of undiscard blocks */
40820ee4382SChao Yu 	unsigned int next_pos;			/* next discard position */
4098b8dd65fSChao Yu 	atomic_t issued_discard;		/* # of issued discard */
41072691af6SJaegeuk Kim 	atomic_t queued_discard;		/* # of queued discard */
4115f32366aSChao Yu 	atomic_t discard_cmd_cnt;		/* # of cached cmd count */
4124dada3fdSChao Yu 	struct rb_root_cached root;		/* root of discard rb-tree */
41367fce70bSChao Yu 	bool rbtree_check;			/* config for consistence check */
41439a53e0cSJaegeuk Kim };
41539a53e0cSJaegeuk Kim 
41639a53e0cSJaegeuk Kim /* for the list of fsync inodes, used only during recovery */
41739a53e0cSJaegeuk Kim struct fsync_inode_entry {
41839a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
41939a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
420c52e1b10SJaegeuk Kim 	block_t blkaddr;	/* block address locating the last fsync */
421c52e1b10SJaegeuk Kim 	block_t last_dentry;	/* block address locating the last dentry */
42239a53e0cSJaegeuk Kim };
42339a53e0cSJaegeuk Kim 
42468afcf2dSTomohiro Kusumi #define nats_in_cursum(jnl)		(le16_to_cpu((jnl)->n_nats))
42568afcf2dSTomohiro Kusumi #define sits_in_cursum(jnl)		(le16_to_cpu((jnl)->n_sits))
42639a53e0cSJaegeuk Kim 
42768afcf2dSTomohiro Kusumi #define nat_in_journal(jnl, i)		((jnl)->nat_j.entries[i].ne)
42868afcf2dSTomohiro Kusumi #define nid_in_journal(jnl, i)		((jnl)->nat_j.entries[i].nid)
42968afcf2dSTomohiro Kusumi #define sit_in_journal(jnl, i)		((jnl)->sit_j.entries[i].se)
43068afcf2dSTomohiro Kusumi #define segno_in_journal(jnl, i)	((jnl)->sit_j.entries[i].segno)
43139a53e0cSJaegeuk Kim 
432dfc08a12SChao Yu #define MAX_NAT_JENTRIES(jnl)	(NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
433dfc08a12SChao Yu #define MAX_SIT_JENTRIES(jnl)	(SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
434309cc2b6SJaegeuk Kim 
435dfc08a12SChao Yu static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
43639a53e0cSJaegeuk Kim {
437dfc08a12SChao Yu 	int before = nats_in_cursum(journal);
438cac5a3d8SDongOh Shin 
439dfc08a12SChao Yu 	journal->n_nats = cpu_to_le16(before + i);
44039a53e0cSJaegeuk Kim 	return before;
44139a53e0cSJaegeuk Kim }
44239a53e0cSJaegeuk Kim 
443dfc08a12SChao Yu static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
44439a53e0cSJaegeuk Kim {
445dfc08a12SChao Yu 	int before = sits_in_cursum(journal);
446cac5a3d8SDongOh Shin 
447dfc08a12SChao Yu 	journal->n_sits = cpu_to_le16(before + i);
44839a53e0cSJaegeuk Kim 	return before;
44939a53e0cSJaegeuk Kim }
45039a53e0cSJaegeuk Kim 
451dfc08a12SChao Yu static inline bool __has_cursum_space(struct f2fs_journal *journal,
452dfc08a12SChao Yu 							int size, int type)
453184a5cd2SChao Yu {
454184a5cd2SChao Yu 	if (type == NAT_JOURNAL)
455dfc08a12SChao Yu 		return size <= MAX_NAT_JENTRIES(journal);
456dfc08a12SChao Yu 	return size <= MAX_SIT_JENTRIES(journal);
457184a5cd2SChao Yu }
458184a5cd2SChao Yu 
459f2470371SChao Yu /* for inline stuff */
460f2470371SChao Yu #define DEF_INLINE_RESERVED_SIZE	1
4617a2af766SChao Yu static inline int get_extra_isize(struct inode *inode);
4626afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode);
4637a2af766SChao Yu #define MAX_INLINE_DATA(inode)	(sizeof(__le32) *			\
4647a2af766SChao Yu 				(CUR_ADDRS_PER_INODE(inode) -		\
465b323fd28SChao Yu 				get_inline_xattr_addrs(inode) -	\
4666afc662eSChao Yu 				DEF_INLINE_RESERVED_SIZE))
467f2470371SChao Yu 
468f2470371SChao Yu /* for inline dir */
469f2470371SChao Yu #define NR_INLINE_DENTRY(inode)	(MAX_INLINE_DATA(inode) * BITS_PER_BYTE / \
470f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
471f2470371SChao Yu 				BITS_PER_BYTE + 1))
472f91108b8SGeert Uytterhoeven #define INLINE_DENTRY_BITMAP_SIZE(inode) \
473f91108b8SGeert Uytterhoeven 	DIV_ROUND_UP(NR_INLINE_DENTRY(inode), BITS_PER_BYTE)
474f2470371SChao Yu #define INLINE_RESERVED_SIZE(inode)	(MAX_INLINE_DATA(inode) - \
475f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
476f2470371SChao Yu 				NR_INLINE_DENTRY(inode) + \
477f2470371SChao Yu 				INLINE_DENTRY_BITMAP_SIZE(inode)))
478f2470371SChao Yu 
479e9750824SNamjae Jeon /*
48039a53e0cSJaegeuk Kim  * For INODE and NODE manager
48139a53e0cSJaegeuk Kim  */
4827b3cd7d6SJaegeuk Kim /* for directory operations */
48343c780baSEric Biggers 
48443c780baSEric Biggers struct f2fs_filename {
48543c780baSEric Biggers 	/*
48643c780baSEric Biggers 	 * The filename the user specified.  This is NULL for some
48743c780baSEric Biggers 	 * filesystem-internal operations, e.g. converting an inline directory
48843c780baSEric Biggers 	 * to a non-inline one, or roll-forward recovering an encrypted dentry.
48943c780baSEric Biggers 	 */
49043c780baSEric Biggers 	const struct qstr *usr_fname;
49143c780baSEric Biggers 
49243c780baSEric Biggers 	/*
49343c780baSEric Biggers 	 * The on-disk filename.  For encrypted directories, this is encrypted.
49443c780baSEric Biggers 	 * This may be NULL for lookups in an encrypted dir without the key.
49543c780baSEric Biggers 	 */
49643c780baSEric Biggers 	struct fscrypt_str disk_name;
49743c780baSEric Biggers 
49843c780baSEric Biggers 	/* The dirhash of this filename */
49943c780baSEric Biggers 	f2fs_hash_t hash;
50043c780baSEric Biggers 
50143c780baSEric Biggers #ifdef CONFIG_FS_ENCRYPTION
50243c780baSEric Biggers 	/*
50343c780baSEric Biggers 	 * For lookups in encrypted directories: either the buffer backing
50443c780baSEric Biggers 	 * disk_name, or a buffer that holds the decoded no-key name.
50543c780baSEric Biggers 	 */
50643c780baSEric Biggers 	struct fscrypt_str crypto_buf;
50743c780baSEric Biggers #endif
5085298d4bfSChristoph Hellwig #if IS_ENABLED(CONFIG_UNICODE)
50943c780baSEric Biggers 	/*
51043c780baSEric Biggers 	 * For casefolded directories: the casefolded name, but it's left NULL
511*b5639bb4SEric Biggers 	 * if the original name is not valid Unicode, if the original name is
512*b5639bb4SEric Biggers 	 * "." or "..", if the directory is both casefolded and encrypted and
513*b5639bb4SEric Biggers 	 * its encryption key is unavailable, or if the filesystem is doing an
514*b5639bb4SEric Biggers 	 * internal operation where usr_fname is also NULL.  In all these cases
515*b5639bb4SEric Biggers 	 * we fall back to treating the name as an opaque byte sequence.
51643c780baSEric Biggers 	 */
51743c780baSEric Biggers 	struct fscrypt_str cf_name;
51843c780baSEric Biggers #endif
51943c780baSEric Biggers };
52043c780baSEric Biggers 
5217b3cd7d6SJaegeuk Kim struct f2fs_dentry_ptr {
522d8c6822aSJaegeuk Kim 	struct inode *inode;
52376a9dd85SChao Yu 	void *bitmap;
5247b3cd7d6SJaegeuk Kim 	struct f2fs_dir_entry *dentry;
5257b3cd7d6SJaegeuk Kim 	__u8 (*filename)[F2FS_SLOT_LEN];
5267b3cd7d6SJaegeuk Kim 	int max;
52776a9dd85SChao Yu 	int nr_bitmap;
5287b3cd7d6SJaegeuk Kim };
5297b3cd7d6SJaegeuk Kim 
53064c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_block(struct inode *inode,
53164c24ecbSTomohiro Kusumi 		struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
5327b3cd7d6SJaegeuk Kim {
533d8c6822aSJaegeuk Kim 	d->inode = inode;
5347b3cd7d6SJaegeuk Kim 	d->max = NR_DENTRY_IN_BLOCK;
53576a9dd85SChao Yu 	d->nr_bitmap = SIZE_OF_DENTRY_BITMAP;
536c79d1520SYunlong Song 	d->bitmap = t->dentry_bitmap;
5377b3cd7d6SJaegeuk Kim 	d->dentry = t->dentry;
5387b3cd7d6SJaegeuk Kim 	d->filename = t->filename;
53964c24ecbSTomohiro Kusumi }
540cac5a3d8SDongOh Shin 
54164c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_inline(struct inode *inode,
542f2470371SChao Yu 					struct f2fs_dentry_ptr *d, void *t)
54364c24ecbSTomohiro Kusumi {
544f2470371SChao Yu 	int entry_cnt = NR_INLINE_DENTRY(inode);
545f2470371SChao Yu 	int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(inode);
546f2470371SChao Yu 	int reserved_size = INLINE_RESERVED_SIZE(inode);
547f2470371SChao Yu 
54864c24ecbSTomohiro Kusumi 	d->inode = inode;
549f2470371SChao Yu 	d->max = entry_cnt;
550f2470371SChao Yu 	d->nr_bitmap = bitmap_size;
551f2470371SChao Yu 	d->bitmap = t;
552f2470371SChao Yu 	d->dentry = t + bitmap_size + reserved_size;
553f2470371SChao Yu 	d->filename = t + bitmap_size + reserved_size +
554f2470371SChao Yu 					SIZE_OF_DIR_ENTRY * entry_cnt;
5557b3cd7d6SJaegeuk Kim }
5567b3cd7d6SJaegeuk Kim 
557dbe6a5ffSJaegeuk Kim /*
558dbe6a5ffSJaegeuk Kim  * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
559dbe6a5ffSJaegeuk Kim  * as its node offset to distinguish from index node blocks.
560dbe6a5ffSJaegeuk Kim  * But some bits are used to mark the node block.
56139a53e0cSJaegeuk Kim  */
562dbe6a5ffSJaegeuk Kim #define XATTR_NODE_OFFSET	((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
563dbe6a5ffSJaegeuk Kim 				>> OFFSET_BIT_SHIFT)
564266e97a8SJaegeuk Kim enum {
565266e97a8SJaegeuk Kim 	ALLOC_NODE,			/* allocate a new node page if needed */
566266e97a8SJaegeuk Kim 	LOOKUP_NODE,			/* look up a node without readahead */
567266e97a8SJaegeuk Kim 	LOOKUP_NODE_RA,			/*
568266e97a8SJaegeuk Kim 					 * look up a node with readahead called
5694f4124d0SChao Yu 					 * by get_data_block.
57039a53e0cSJaegeuk Kim 					 */
571266e97a8SJaegeuk Kim };
572266e97a8SJaegeuk Kim 
57391803392SChao Yu #define DEFAULT_RETRY_IO_COUNT	8	/* maximum retry read IO or flush count */
5747735730dSChao Yu 
5755df7731fSChao Yu /* congestion wait timeout value, default: 20ms */
5765df7731fSChao Yu #define	DEFAULT_IO_TIMEOUT	(msecs_to_jiffies(20))
5775df7731fSChao Yu 
578af033b2aSChao Yu /* maximum retry quota flush count */
579af033b2aSChao Yu #define DEFAULT_RETRY_QUOTA_FLUSH_COUNT		8
580af033b2aSChao Yu 
581a7b8618aSJaegeuk Kim /* maximum retry of EIO'ed page */
582a7b8618aSJaegeuk Kim #define MAX_RETRY_PAGE_EIO			100
58350c63009SJaegeuk Kim 
584a6db67f0SJaegeuk Kim #define F2FS_LINK_MAX	0xffffffff	/* maximum link count per file */
58539a53e0cSJaegeuk Kim 
586817202d9SChao Yu #define MAX_DIR_RA_PAGES	4	/* maximum ra pages of dir */
587817202d9SChao Yu 
588287b1406SChao Yu /* dirty segments threshold for triggering CP */
589287b1406SChao Yu #define DEFAULT_DIRTY_THRESHOLD		4
590287b1406SChao Yu 
59139a53e0cSJaegeuk Kim /* for in-memory extent cache entry */
59213054c54SChao Yu #define F2FS_MIN_EXTENT_LEN	64	/* minimum extent length */
59313054c54SChao Yu 
59413054c54SChao Yu /* number of extent info in extent cache we try to shrink */
59513054c54SChao Yu #define EXTENT_CACHE_SHRINK_NUMBER	128
596c11abd1aSJaegeuk Kim 
597430f163bSChao Yu #define RECOVERY_MAX_RA_BLOCKS		BIO_MAX_VECS
598430f163bSChao Yu #define RECOVERY_MIN_RA_BLOCKS		1
599430f163bSChao Yu 
60054c2258cSChao Yu struct rb_entry {
60154c2258cSChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
6022e9b2bb2SChao Yu 	union {
6032e9b2bb2SChao Yu 		struct {
60454c2258cSChao Yu 			unsigned int ofs;	/* start offset of the entry */
60554c2258cSChao Yu 			unsigned int len;	/* length of the entry */
60654c2258cSChao Yu 		};
6072e9b2bb2SChao Yu 		unsigned long long key;		/* 64-bits key */
60848046cb5SJaegeuk Kim 	} __packed;
6092e9b2bb2SChao Yu };
61054c2258cSChao Yu 
61139a53e0cSJaegeuk Kim struct extent_info {
61239a53e0cSJaegeuk Kim 	unsigned int fofs;		/* start offset in a file */
613111d2495SMasanari Iida 	unsigned int len;		/* length of the extent */
61454c2258cSChao Yu 	u32 blk;			/* start block address of the extent */
61594afd6d6SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
61694afd6d6SChao Yu 	unsigned int c_len;		/* physical extent length of compressed blocks */
61794afd6d6SChao Yu #endif
61839a53e0cSJaegeuk Kim };
61939a53e0cSJaegeuk Kim 
62013054c54SChao Yu struct extent_node {
621c0362117SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
62213054c54SChao Yu 	struct extent_info ei;		/* extent info */
62354c2258cSChao Yu 	struct list_head list;		/* node in global extent list of sbi */
624201ef5e0SHou Pengyang 	struct extent_tree *et;		/* extent tree pointer */
62513054c54SChao Yu };
62613054c54SChao Yu 
62713054c54SChao Yu struct extent_tree {
62813054c54SChao Yu 	nid_t ino;			/* inode number */
6294dada3fdSChao Yu 	struct rb_root_cached root;	/* root of extent info rb-tree */
63062c8af65SChao Yu 	struct extent_node *cached_en;	/* recently accessed extent node */
6313e72f721SJaegeuk Kim 	struct extent_info largest;	/* largested extent info */
632137d09f0SJaegeuk Kim 	struct list_head list;		/* to be used by sbi->zombie_list */
63313054c54SChao Yu 	rwlock_t lock;			/* protect extent info rb-tree */
63468e35385SChao Yu 	atomic_t node_cnt;		/* # of extent node in rb-tree*/
635b430f726SZhikang Zhang 	bool largest_updated;		/* largest extent updated */
63613054c54SChao Yu };
63713054c54SChao Yu 
63839a53e0cSJaegeuk Kim /*
639003a3e1dSJaegeuk Kim  * This structure is taken from ext4_map_blocks.
640003a3e1dSJaegeuk Kim  *
641003a3e1dSJaegeuk Kim  * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
642003a3e1dSJaegeuk Kim  */
643003a3e1dSJaegeuk Kim #define F2FS_MAP_NEW		(1 << BH_New)
644003a3e1dSJaegeuk Kim #define F2FS_MAP_MAPPED		(1 << BH_Mapped)
6457f63eb77SJaegeuk Kim #define F2FS_MAP_UNWRITTEN	(1 << BH_Unwritten)
6467f63eb77SJaegeuk Kim #define F2FS_MAP_FLAGS		(F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
6477f63eb77SJaegeuk Kim 				F2FS_MAP_UNWRITTEN)
648003a3e1dSJaegeuk Kim 
649003a3e1dSJaegeuk Kim struct f2fs_map_blocks {
65071f2c820SChao Yu 	struct block_device *m_bdev;	/* for multi-device dio */
651003a3e1dSJaegeuk Kim 	block_t m_pblk;
652003a3e1dSJaegeuk Kim 	block_t m_lblk;
653003a3e1dSJaegeuk Kim 	unsigned int m_len;
654003a3e1dSJaegeuk Kim 	unsigned int m_flags;
655da85985cSChao Yu 	pgoff_t *m_next_pgofs;		/* point next possible non-hole pgofs */
656c4020b2dSChao Yu 	pgoff_t *m_next_extent;		/* point to next possible extent */
657d5097be5SHyunchul Lee 	int m_seg_type;
658f9d6d059SChao Yu 	bool m_may_create;		/* indicate it is from write path */
65971f2c820SChao Yu 	bool m_multidev_dio;		/* indicate it allows multi-device dio */
660003a3e1dSJaegeuk Kim };
661003a3e1dSJaegeuk Kim 
662e2b4e2bcSChao Yu /* for flag in get_data_block */
663f2220c7fSQiuyang Sun enum {
664f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_DEFAULT,
665f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_FIEMAP,
666f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_BMAP,
6670a4daae5SJaegeuk Kim 	F2FS_GET_BLOCK_DIO,
668f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_DIO,
669f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_AIO,
670c4020b2dSChao Yu 	F2FS_GET_BLOCK_PRECACHE,
671f2220c7fSQiuyang Sun };
672e2b4e2bcSChao Yu 
673003a3e1dSJaegeuk Kim /*
67439a53e0cSJaegeuk Kim  * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
67539a53e0cSJaegeuk Kim  */
67639a53e0cSJaegeuk Kim #define FADVISE_COLD_BIT	0x01
677354a3399SJaegeuk Kim #define FADVISE_LOST_PINO_BIT	0x02
678cde4de12SJaegeuk Kim #define FADVISE_ENCRYPT_BIT	0x04
679e7d55452SJaegeuk Kim #define FADVISE_ENC_NAME_BIT	0x08
68026787236SJaegeuk Kim #define FADVISE_KEEP_SIZE_BIT	0x10
681b6a06cbbSChao Yu #define FADVISE_HOT_BIT		0x20
68295ae251fSEric Biggers #define FADVISE_VERITY_BIT	0x40
683d4dd19ecSJaegeuk Kim #define FADVISE_TRUNC_BIT	0x80
68439a53e0cSJaegeuk Kim 
685797c1cb5SChao Yu #define FADVISE_MODIFIABLE_BITS	(FADVISE_COLD_BIT | FADVISE_HOT_BIT)
686797c1cb5SChao Yu 
687b5492af7SJaegeuk Kim #define file_is_cold(inode)	is_file(inode, FADVISE_COLD_BIT)
688b5492af7SJaegeuk Kim #define file_set_cold(inode)	set_file(inode, FADVISE_COLD_BIT)
689b5492af7SJaegeuk Kim #define file_clear_cold(inode)	clear_file(inode, FADVISE_COLD_BIT)
6901153db09SChao Yu 
6911153db09SChao Yu #define file_wrong_pino(inode)	is_file(inode, FADVISE_LOST_PINO_BIT)
6921153db09SChao Yu #define file_lost_pino(inode)	set_file(inode, FADVISE_LOST_PINO_BIT)
693b5492af7SJaegeuk Kim #define file_got_pino(inode)	clear_file(inode, FADVISE_LOST_PINO_BIT)
6941153db09SChao Yu 
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)
6971153db09SChao Yu 
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)
7001153db09SChao Yu 
70126787236SJaegeuk Kim #define file_keep_isize(inode)	is_file(inode, FADVISE_KEEP_SIZE_BIT)
70226787236SJaegeuk Kim #define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
7031153db09SChao Yu 
704b6a06cbbSChao Yu #define file_is_hot(inode)	is_file(inode, FADVISE_HOT_BIT)
705b6a06cbbSChao Yu #define file_set_hot(inode)	set_file(inode, FADVISE_HOT_BIT)
706b6a06cbbSChao Yu #define file_clear_hot(inode)	clear_file(inode, FADVISE_HOT_BIT)
7071153db09SChao Yu 
70895ae251fSEric Biggers #define file_is_verity(inode)	is_file(inode, FADVISE_VERITY_BIT)
70995ae251fSEric Biggers #define file_set_verity(inode)	set_file(inode, FADVISE_VERITY_BIT)
710cde4de12SJaegeuk Kim 
711d4dd19ecSJaegeuk Kim #define file_should_truncate(inode)	is_file(inode, FADVISE_TRUNC_BIT)
712d4dd19ecSJaegeuk Kim #define file_need_truncate(inode)	set_file(inode, FADVISE_TRUNC_BIT)
713d4dd19ecSJaegeuk Kim #define file_dont_truncate(inode)	clear_file(inode, FADVISE_TRUNC_BIT)
714d4dd19ecSJaegeuk Kim 
715ab9fa662SJaegeuk Kim #define DEF_DIR_LEVEL		0
716ab9fa662SJaegeuk Kim 
7172ef79ecbSChao Yu enum {
7182ef79ecbSChao Yu 	GC_FAILURE_PIN,
7192ef79ecbSChao Yu 	MAX_GC_FAILURE
7202ef79ecbSChao Yu };
7212ef79ecbSChao Yu 
7227653b9d8SChao Yu /* used for f2fs_inode_info->flags */
7237653b9d8SChao Yu enum {
7247653b9d8SChao Yu 	FI_NEW_INODE,		/* indicate newly allocated inode */
7257653b9d8SChao Yu 	FI_DIRTY_INODE,		/* indicate inode is dirty or not */
7267653b9d8SChao Yu 	FI_AUTO_RECOVER,	/* indicate inode is recoverable */
7277653b9d8SChao Yu 	FI_DIRTY_DIR,		/* indicate directory has dirty pages */
7287653b9d8SChao Yu 	FI_INC_LINK,		/* need to increment i_nlink */
7297653b9d8SChao Yu 	FI_ACL_MODE,		/* indicate acl mode */
7307653b9d8SChao Yu 	FI_NO_ALLOC,		/* should not allocate any blocks */
7317653b9d8SChao Yu 	FI_FREE_NID,		/* free allocated nide */
7327653b9d8SChao Yu 	FI_NO_EXTENT,		/* not to use the extent cache */
7337653b9d8SChao Yu 	FI_INLINE_XATTR,	/* used for inline xattr */
7347653b9d8SChao Yu 	FI_INLINE_DATA,		/* used for inline data*/
7357653b9d8SChao Yu 	FI_INLINE_DENTRY,	/* used for inline dentry */
7367653b9d8SChao Yu 	FI_APPEND_WRITE,	/* inode has appended data */
7377653b9d8SChao Yu 	FI_UPDATE_WRITE,	/* inode has in-place-update data */
7387653b9d8SChao Yu 	FI_NEED_IPU,		/* used for ipu per file */
7397653b9d8SChao Yu 	FI_ATOMIC_FILE,		/* indicate atomic file */
7407653b9d8SChao Yu 	FI_FIRST_BLOCK_WRITTEN,	/* indicate #0 data block was written */
7417653b9d8SChao Yu 	FI_DROP_CACHE,		/* drop dirty page cache */
7427653b9d8SChao Yu 	FI_DATA_EXIST,		/* indicate data exists */
7437653b9d8SChao Yu 	FI_INLINE_DOTS,		/* indicate inline dot dentries */
7441018a546SChao Yu 	FI_SKIP_WRITES,		/* should skip data page writeback */
7451018a546SChao Yu 	FI_OPU_WRITE,		/* used for opu per file */
7467653b9d8SChao Yu 	FI_DIRTY_FILE,		/* indicate regular/symlink has dirty pages */
7473d697a4aSEric Biggers 	FI_PREALLOCATED_ALL,	/* all blocks for write were preallocated */
7487653b9d8SChao Yu 	FI_HOT_DATA,		/* indicate file is hot */
7497653b9d8SChao Yu 	FI_EXTRA_ATTR,		/* indicate file has extra attribute */
7507653b9d8SChao Yu 	FI_PROJ_INHERIT,	/* indicate file inherits projectid */
7517653b9d8SChao Yu 	FI_PIN_FILE,		/* indicate file should not be gced */
7527653b9d8SChao Yu 	FI_VERITY_IN_PROGRESS,	/* building fs-verity Merkle tree */
7537653b9d8SChao Yu 	FI_COMPRESSED_FILE,	/* indicate file's data can be compressed */
754b28f047bSChao Yu 	FI_COMPRESS_CORRUPT,	/* indicate compressed cluster is corrupted */
7557653b9d8SChao Yu 	FI_MMAP_FILE,		/* indicate file was mmapped */
756602a16d5SDaeho Jeong 	FI_ENABLE_COMPRESS,	/* enable compression in "user" compression mode */
757c6140415SJaegeuk Kim 	FI_COMPRESS_RELEASED,	/* compressed blocks were released */
758859fca6bSChao Yu 	FI_ALIGNED_WRITE,	/* enable aligned write */
7597653b9d8SChao Yu 	FI_MAX,			/* max flag, never be used */
7607653b9d8SChao Yu };
7617653b9d8SChao Yu 
76239a53e0cSJaegeuk Kim struct f2fs_inode_info {
76339a53e0cSJaegeuk Kim 	struct inode vfs_inode;		/* serve a vfs inode */
76439a53e0cSJaegeuk Kim 	unsigned long i_flags;		/* keep an inode flags for ioctl */
76539a53e0cSJaegeuk Kim 	unsigned char i_advise;		/* use to give file attribute hints */
76638431545SJaegeuk Kim 	unsigned char i_dir_level;	/* use for dentry level for large dir */
7671ad71a27SJaegeuk Kim 	unsigned int i_current_depth;	/* only for directory depth */
7682ef79ecbSChao Yu 	/* for gc failure statistic */
7692ef79ecbSChao Yu 	unsigned int i_gc_failures[MAX_GC_FAILURE];
7706666e6aaSJaegeuk Kim 	unsigned int i_pino;		/* parent inode number */
77139a53e0cSJaegeuk Kim 	umode_t i_acl_mode;		/* keep file acl mode temporarily */
77239a53e0cSJaegeuk Kim 
77339a53e0cSJaegeuk Kim 	/* Use below internally in f2fs*/
7747653b9d8SChao Yu 	unsigned long flags[BITS_TO_LONGS(FI_MAX)];	/* use to pass per-file flags */
775e4544b63STim Murray 	struct f2fs_rwsem i_sem;	/* protect fi info */
776204706c7SJaegeuk Kim 	atomic_t dirty_pages;		/* # of dirty pages */
77739a53e0cSJaegeuk Kim 	f2fs_hash_t chash;		/* hash value of given file name */
77839a53e0cSJaegeuk Kim 	unsigned int clevel;		/* maximum level of given file name */
77988c5c13aSJaegeuk Kim 	struct task_struct *task;	/* lookup and create consistency */
780b0af6d49SChao Yu 	struct task_struct *cp_task;	/* separate cp/wb IO stats*/
78139a53e0cSJaegeuk Kim 	nid_t i_xattr_nid;		/* node id that contains xattrs */
78226de9b11SJaegeuk Kim 	loff_t	last_disk_size;		/* lastly written file size */
783c10c9820SChao Yu 	spinlock_t i_size_lock;		/* protect last_disk_size */
78488b88a66SJaegeuk Kim 
7850abd675eSChao Yu #ifdef CONFIG_QUOTA
7860abd675eSChao Yu 	struct dquot *i_dquot[MAXQUOTAS];
7870abd675eSChao Yu 
7880abd675eSChao Yu 	/* quota space reservation, managed internally by quota code */
7890abd675eSChao Yu 	qsize_t i_reserved_quota;
7900abd675eSChao Yu #endif
7910f18b462SJaegeuk Kim 	struct list_head dirty_list;	/* dirty list for dirs and files */
7920f18b462SJaegeuk Kim 	struct list_head gdirty_list;	/* linked in global dirty list */
7933db1de0eSDaeho Jeong 	struct task_struct *atomic_write_task;	/* store atomic write task */
7943e72f721SJaegeuk Kim 	struct extent_tree *extent_tree;	/* cached extent_tree entry */
7953db1de0eSDaeho Jeong 	struct inode *cow_inode;	/* copy-on-write inode for atomic write */
796b2532c69SChao Yu 
797b2532c69SChao Yu 	/* avoid racing between foreground op and gc */
798e4544b63STim Murray 	struct f2fs_rwsem i_gc_rwsem[2];
799e4544b63STim Murray 	struct f2fs_rwsem i_xattr_sem; /* avoid racing between reading and changing EAs */
800f2470371SChao Yu 
8017a2af766SChao Yu 	int i_extra_isize;		/* size of extra space located in i_addr */
8025c57132eSChao Yu 	kprojid_t i_projid;		/* id for project quota */
8036afc662eSChao Yu 	int i_inline_xattr_size;	/* inline xattr size */
80424b81dfcSArnd Bergmann 	struct timespec64 i_crtime;	/* inode creation time */
80524b81dfcSArnd Bergmann 	struct timespec64 i_disk_time[4];/* inode disk times */
8064c8ff709SChao Yu 
8074c8ff709SChao Yu 	/* for file compress */
808c2759ebaSDaeho Jeong 	atomic_t i_compr_blocks;		/* # of compressed blocks */
8094c8ff709SChao Yu 	unsigned char i_compress_algorithm;	/* algorithm type */
8104c8ff709SChao Yu 	unsigned char i_log_cluster_size;	/* log of cluster size */
8113fde13f8SChao Yu 	unsigned char i_compress_level;		/* compress level (lz4hc,zstd) */
812b28f047bSChao Yu 	unsigned short i_compress_flag;		/* compress flag */
8134c8ff709SChao Yu 	unsigned int i_cluster_size;		/* cluster size */
81439a53e0cSJaegeuk Kim };
81539a53e0cSJaegeuk Kim 
81639a53e0cSJaegeuk Kim static inline void get_extent_info(struct extent_info *ext,
817bd933d4fSChao Yu 					struct f2fs_extent *i_ext)
81839a53e0cSJaegeuk Kim {
819bd933d4fSChao Yu 	ext->fofs = le32_to_cpu(i_ext->fofs);
820bd933d4fSChao Yu 	ext->blk = le32_to_cpu(i_ext->blk);
821bd933d4fSChao Yu 	ext->len = le32_to_cpu(i_ext->len);
82239a53e0cSJaegeuk Kim }
82339a53e0cSJaegeuk Kim 
82439a53e0cSJaegeuk Kim static inline void set_raw_extent(struct extent_info *ext,
82539a53e0cSJaegeuk Kim 					struct f2fs_extent *i_ext)
82639a53e0cSJaegeuk Kim {
82739a53e0cSJaegeuk Kim 	i_ext->fofs = cpu_to_le32(ext->fofs);
8284d0b0bd4SChao Yu 	i_ext->blk = cpu_to_le32(ext->blk);
82939a53e0cSJaegeuk Kim 	i_ext->len = cpu_to_le32(ext->len);
83039a53e0cSJaegeuk Kim }
83139a53e0cSJaegeuk Kim 
832429511cdSChao Yu static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
833429511cdSChao Yu 						u32 blk, unsigned int len)
834429511cdSChao Yu {
835429511cdSChao Yu 	ei->fofs = fofs;
836429511cdSChao Yu 	ei->blk = blk;
837429511cdSChao Yu 	ei->len = len;
83894afd6d6SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
83994afd6d6SChao Yu 	ei->c_len = 0;
84094afd6d6SChao Yu #endif
841429511cdSChao Yu }
842429511cdSChao Yu 
843004b6862SChao Yu static inline bool __is_discard_mergeable(struct discard_info *back,
84435ec7d57SChao Yu 			struct discard_info *front, unsigned int max_len)
845004b6862SChao Yu {
8469a997188SJaegeuk Kim 	return (back->lstart + back->len == front->lstart) &&
84735ec7d57SChao Yu 		(back->len + front->len <= max_len);
848004b6862SChao Yu }
849004b6862SChao Yu 
850004b6862SChao Yu static inline bool __is_discard_back_mergeable(struct discard_info *cur,
85135ec7d57SChao Yu 			struct discard_info *back, unsigned int max_len)
852004b6862SChao Yu {
85335ec7d57SChao Yu 	return __is_discard_mergeable(back, cur, max_len);
854004b6862SChao Yu }
855004b6862SChao Yu 
856004b6862SChao Yu static inline bool __is_discard_front_mergeable(struct discard_info *cur,
85735ec7d57SChao Yu 			struct discard_info *front, unsigned int max_len)
858004b6862SChao Yu {
85935ec7d57SChao Yu 	return __is_discard_mergeable(cur, front, max_len);
860004b6862SChao Yu }
861004b6862SChao Yu 
862429511cdSChao Yu static inline bool __is_extent_mergeable(struct extent_info *back,
863429511cdSChao Yu 						struct extent_info *front)
864429511cdSChao Yu {
86594afd6d6SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
86694afd6d6SChao Yu 	if (back->c_len && back->len != back->c_len)
86794afd6d6SChao Yu 		return false;
86894afd6d6SChao Yu 	if (front->c_len && front->len != front->c_len)
86994afd6d6SChao Yu 		return false;
87094afd6d6SChao Yu #endif
871429511cdSChao Yu 	return (back->fofs + back->len == front->fofs &&
872429511cdSChao Yu 			back->blk + back->len == front->blk);
873429511cdSChao Yu }
874429511cdSChao Yu 
875429511cdSChao Yu static inline bool __is_back_mergeable(struct extent_info *cur,
876429511cdSChao Yu 						struct extent_info *back)
877429511cdSChao Yu {
878429511cdSChao Yu 	return __is_extent_mergeable(back, cur);
879429511cdSChao Yu }
880429511cdSChao Yu 
881429511cdSChao Yu static inline bool __is_front_mergeable(struct extent_info *cur,
882429511cdSChao Yu 						struct extent_info *front)
883429511cdSChao Yu {
884429511cdSChao Yu 	return __is_extent_mergeable(cur, front);
885429511cdSChao Yu }
886429511cdSChao Yu 
887cac5a3d8SDongOh Shin extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
888b430f726SZhikang Zhang static inline void __try_update_largest_extent(struct extent_tree *et,
889b430f726SZhikang Zhang 						struct extent_node *en)
8904abd3f5aSChao Yu {
891205b9822SJaegeuk Kim 	if (en->ei.len > et->largest.len) {
8924abd3f5aSChao Yu 		et->largest = en->ei;
893b430f726SZhikang Zhang 		et->largest_updated = true;
894205b9822SJaegeuk Kim 	}
8954abd3f5aSChao Yu }
8964abd3f5aSChao Yu 
8979a4ffdf5SChao Yu /*
8989a4ffdf5SChao Yu  * For free nid management
8999a4ffdf5SChao Yu  */
9009a4ffdf5SChao Yu enum nid_state {
9019a4ffdf5SChao Yu 	FREE_NID,		/* newly added to free nid list */
9029a4ffdf5SChao Yu 	PREALLOC_NID,		/* it is preallocated */
9039a4ffdf5SChao Yu 	MAX_NID_STATE,
904b8559dc2SChao Yu };
905b8559dc2SChao Yu 
906a95ba66aSJaegeuk Kim enum nat_state {
907a95ba66aSJaegeuk Kim 	TOTAL_NAT,
908a95ba66aSJaegeuk Kim 	DIRTY_NAT,
909a95ba66aSJaegeuk Kim 	RECLAIMABLE_NAT,
910a95ba66aSJaegeuk Kim 	MAX_NAT_STATE,
911a95ba66aSJaegeuk Kim };
912a95ba66aSJaegeuk Kim 
91339a53e0cSJaegeuk Kim struct f2fs_nm_info {
91439a53e0cSJaegeuk Kim 	block_t nat_blkaddr;		/* base disk address of NAT */
91539a53e0cSJaegeuk Kim 	nid_t max_nid;			/* maximum possible node ids */
91604d47e67SChao Yu 	nid_t available_nids;		/* # of available node ids */
91739a53e0cSJaegeuk Kim 	nid_t next_scan_nid;		/* the next nid to be scanned */
91847c8ebccSJaegeuk Kim 	nid_t max_rf_node_blocks;	/* max # of nodes for recovery */
919cdfc41c1SJaegeuk Kim 	unsigned int ram_thresh;	/* control the memory footprint */
920ea1a29a0SChao Yu 	unsigned int ra_nid_pages;	/* # of nid pages to be readaheaded */
9212304cb0cSChao Yu 	unsigned int dirty_nats_ratio;	/* control dirty nats ratio threshold */
92239a53e0cSJaegeuk Kim 
92339a53e0cSJaegeuk Kim 	/* NAT cache management */
92439a53e0cSJaegeuk Kim 	struct radix_tree_root nat_root;/* root of the nat entry cache */
925309cc2b6SJaegeuk Kim 	struct radix_tree_root nat_set_root;/* root of the nat set cache */
926e4544b63STim Murray 	struct f2fs_rwsem nat_tree_lock;	/* protect nat entry tree */
92739a53e0cSJaegeuk Kim 	struct list_head nat_entries;	/* cached nat entry list (clean) */
92822969158SChao Yu 	spinlock_t nat_list_lock;	/* protect clean nat entry list */
929a95ba66aSJaegeuk Kim 	unsigned int nat_cnt[MAX_NAT_STATE]; /* the # of cached nat entries */
93022ad0b6aSJaegeuk Kim 	unsigned int nat_blocks;	/* # of nat blocks */
93139a53e0cSJaegeuk Kim 
93239a53e0cSJaegeuk Kim 	/* free node ids management */
9338a7ed66aSJaegeuk Kim 	struct radix_tree_root free_nid_root;/* root of the free_nid cache */
9349a4ffdf5SChao Yu 	struct list_head free_nid_list;		/* list for free nids excluding preallocated nids */
9359a4ffdf5SChao Yu 	unsigned int nid_cnt[MAX_NID_STATE];	/* the number of free node id */
936b8559dc2SChao Yu 	spinlock_t nid_list_lock;	/* protect nid lists ops */
93739a53e0cSJaegeuk Kim 	struct mutex build_lock;	/* lock for build free nids */
938bb1105e4SJaegeuk Kim 	unsigned char **free_nid_bitmap;
9394ac91242SChao Yu 	unsigned char *nat_block_bitmap;
940586d1492SChao Yu 	unsigned short *free_nid_count;	/* free nid count of NAT block */
94139a53e0cSJaegeuk Kim 
94239a53e0cSJaegeuk Kim 	/* for checkpoint */
94339a53e0cSJaegeuk Kim 	char *nat_bitmap;		/* NAT bitmap pointer */
94422ad0b6aSJaegeuk Kim 
94522ad0b6aSJaegeuk Kim 	unsigned int nat_bits_blocks;	/* # of nat bits blocks */
94622ad0b6aSJaegeuk Kim 	unsigned char *nat_bits;	/* NAT bits blocks */
94722ad0b6aSJaegeuk Kim 	unsigned char *full_nat_bits;	/* full NAT pages */
94822ad0b6aSJaegeuk Kim 	unsigned char *empty_nat_bits;	/* empty NAT pages */
949599a09b2SChao Yu #ifdef CONFIG_F2FS_CHECK_FS
950599a09b2SChao Yu 	char *nat_bitmap_mir;		/* NAT bitmap mirror */
951599a09b2SChao Yu #endif
95239a53e0cSJaegeuk Kim 	int bitmap_size;		/* bitmap size */
95339a53e0cSJaegeuk Kim };
95439a53e0cSJaegeuk Kim 
95539a53e0cSJaegeuk Kim /*
95639a53e0cSJaegeuk Kim  * this structure is used as one of function parameters.
95739a53e0cSJaegeuk Kim  * all the information are dedicated to a given direct node block determined
95839a53e0cSJaegeuk Kim  * by the data offset in a file.
95939a53e0cSJaegeuk Kim  */
96039a53e0cSJaegeuk Kim struct dnode_of_data {
96139a53e0cSJaegeuk Kim 	struct inode *inode;		/* vfs inode pointer */
96239a53e0cSJaegeuk Kim 	struct page *inode_page;	/* its inode page, NULL is possible */
96339a53e0cSJaegeuk Kim 	struct page *node_page;		/* cached direct node page */
96439a53e0cSJaegeuk Kim 	nid_t nid;			/* node id of the direct node block */
96539a53e0cSJaegeuk Kim 	unsigned int ofs_in_node;	/* data offset in the node page */
96639a53e0cSJaegeuk Kim 	bool inode_page_locked;		/* inode page is locked or not */
96793bae099SJaegeuk Kim 	bool node_changed;		/* is node block changed */
9683cf45747SChao Yu 	char cur_level;			/* level of hole node page */
9693cf45747SChao Yu 	char max_level;			/* level of current page located */
97039a53e0cSJaegeuk Kim 	block_t	data_blkaddr;		/* block address of the node block */
97139a53e0cSJaegeuk Kim };
97239a53e0cSJaegeuk Kim 
97339a53e0cSJaegeuk Kim static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
97439a53e0cSJaegeuk Kim 		struct page *ipage, struct page *npage, nid_t nid)
97539a53e0cSJaegeuk Kim {
976d66d1f76SJaegeuk Kim 	memset(dn, 0, sizeof(*dn));
97739a53e0cSJaegeuk Kim 	dn->inode = inode;
97839a53e0cSJaegeuk Kim 	dn->inode_page = ipage;
97939a53e0cSJaegeuk Kim 	dn->node_page = npage;
98039a53e0cSJaegeuk Kim 	dn->nid = nid;
98139a53e0cSJaegeuk Kim }
98239a53e0cSJaegeuk Kim 
98339a53e0cSJaegeuk Kim /*
98439a53e0cSJaegeuk Kim  * For SIT manager
98539a53e0cSJaegeuk Kim  *
98639a53e0cSJaegeuk Kim  * By default, there are 6 active log areas across the whole main area.
98739a53e0cSJaegeuk Kim  * When considering hot and cold data separation to reduce cleaning overhead,
98839a53e0cSJaegeuk Kim  * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
98939a53e0cSJaegeuk Kim  * respectively.
99039a53e0cSJaegeuk Kim  * In the current design, you should not change the numbers intentionally.
99139a53e0cSJaegeuk Kim  * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
99239a53e0cSJaegeuk Kim  * logs individually according to the underlying devices. (default: 6)
99339a53e0cSJaegeuk Kim  * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
99439a53e0cSJaegeuk Kim  * data and 8 for node logs.
99539a53e0cSJaegeuk Kim  */
99639a53e0cSJaegeuk Kim #define	NR_CURSEG_DATA_TYPE	(3)
99739a53e0cSJaegeuk Kim #define NR_CURSEG_NODE_TYPE	(3)
998093749e2SChao Yu #define NR_CURSEG_INMEM_TYPE	(2)
999a7d9fe3cSJaegeuk Kim #define NR_CURSEG_RO_TYPE	(2)
1000d0b9e42aSChao Yu #define NR_CURSEG_PERSIST_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
1001d0b9e42aSChao Yu #define NR_CURSEG_TYPE		(NR_CURSEG_INMEM_TYPE + NR_CURSEG_PERSIST_TYPE)
100239a53e0cSJaegeuk Kim 
100339a53e0cSJaegeuk Kim enum {
100439a53e0cSJaegeuk Kim 	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
100539a53e0cSJaegeuk Kim 	CURSEG_WARM_DATA,	/* data blocks */
100639a53e0cSJaegeuk Kim 	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
100739a53e0cSJaegeuk Kim 	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
100839a53e0cSJaegeuk Kim 	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
100939a53e0cSJaegeuk Kim 	CURSEG_COLD_NODE,	/* indirect node blocks */
1010d0b9e42aSChao Yu 	NR_PERSISTENT_LOG,	/* number of persistent log */
1011d0b9e42aSChao Yu 	CURSEG_COLD_DATA_PINNED = NR_PERSISTENT_LOG,
1012d0b9e42aSChao Yu 				/* pinned file that needs consecutive block address */
1013093749e2SChao Yu 	CURSEG_ALL_DATA_ATGC,	/* SSR alloctor in hot/warm/cold data area */
1014d0b9e42aSChao Yu 	NO_CHECK_TYPE,		/* number of persistent & inmem log */
101539a53e0cSJaegeuk Kim };
101639a53e0cSJaegeuk Kim 
10176b4afdd7SJaegeuk Kim struct flush_cmd {
10186b4afdd7SJaegeuk Kim 	struct completion wait;
1019721bd4d5SGu Zheng 	struct llist_node llnode;
102039d787beSChao Yu 	nid_t ino;
10216b4afdd7SJaegeuk Kim 	int ret;
10226b4afdd7SJaegeuk Kim };
10236b4afdd7SJaegeuk Kim 
1024a688b9d9SGu Zheng struct flush_cmd_control {
1025a688b9d9SGu Zheng 	struct task_struct *f2fs_issue_flush;	/* flush thread */
1026a688b9d9SGu Zheng 	wait_queue_head_t flush_wait_queue;	/* waiting queue for wake-up */
10278b8dd65fSChao Yu 	atomic_t issued_flush;			/* # of issued flushes */
102872691af6SJaegeuk Kim 	atomic_t queued_flush;			/* # of queued flushes */
1029721bd4d5SGu Zheng 	struct llist_head issue_list;		/* list for command issue */
1030721bd4d5SGu Zheng 	struct llist_node *dispatch_list;	/* list for command dispatch */
1031a688b9d9SGu Zheng };
1032a688b9d9SGu Zheng 
103339a53e0cSJaegeuk Kim struct f2fs_sm_info {
103439a53e0cSJaegeuk Kim 	struct sit_info *sit_info;		/* whole segment information */
103539a53e0cSJaegeuk Kim 	struct free_segmap_info *free_info;	/* free segment information */
103639a53e0cSJaegeuk Kim 	struct dirty_seglist_info *dirty_info;	/* dirty segment information */
103739a53e0cSJaegeuk Kim 	struct curseg_info *curseg_array;	/* active segment information */
103839a53e0cSJaegeuk Kim 
1039e4544b63STim Murray 	struct f2fs_rwsem curseg_lock;	/* for preventing curseg change */
10402b60311dSChao Yu 
104139a53e0cSJaegeuk Kim 	block_t seg0_blkaddr;		/* block address of 0'th segment */
104239a53e0cSJaegeuk Kim 	block_t main_blkaddr;		/* start block address of main area */
104339a53e0cSJaegeuk Kim 	block_t ssa_blkaddr;		/* start block address of SSA area */
104439a53e0cSJaegeuk Kim 
104539a53e0cSJaegeuk Kim 	unsigned int segment_count;	/* total # of segments */
104639a53e0cSJaegeuk Kim 	unsigned int main_segments;	/* # of segments in main area */
104739a53e0cSJaegeuk Kim 	unsigned int reserved_segments;	/* # of reserved segments */
1048300a8429SChao Yu 	unsigned int additional_reserved_segments;/* reserved segs for IO align feature */
104939a53e0cSJaegeuk Kim 	unsigned int ovp_segments;	/* # of overprovision segments */
105081eb8d6eSJaegeuk Kim 
105181eb8d6eSJaegeuk Kim 	/* a threshold to reclaim prefree segments */
105281eb8d6eSJaegeuk Kim 	unsigned int rec_prefree_segments;
10537fd9e544SJaegeuk Kim 
1054bba681cbSJaegeuk Kim 	/* for batched trimming */
1055bba681cbSJaegeuk Kim 	unsigned int trim_sections;		/* # of sections to trim */
1056bba681cbSJaegeuk Kim 
1057184a5cd2SChao Yu 	struct list_head sit_entry_set;	/* sit entry set list */
1058184a5cd2SChao Yu 
1059216fbd64SJaegeuk Kim 	unsigned int ipu_policy;	/* in-place-update policy */
1060216fbd64SJaegeuk Kim 	unsigned int min_ipu_util;	/* in-place-update threshold */
1061c1ce1b02SJaegeuk Kim 	unsigned int min_fsync_blocks;	/* threshold for fsync */
1062853137ceSJaegeuk Kim 	unsigned int min_seq_blocks;	/* threshold for sequential blocks */
1063ef095d19SJaegeuk Kim 	unsigned int min_hot_blocks;	/* threshold for hot block allocation */
1064a2a12b67SChao Yu 	unsigned int min_ssr_sections;	/* threshold to trigger SSR allocation */
10656b4afdd7SJaegeuk Kim 
10666b4afdd7SJaegeuk Kim 	/* for flush command control */
1067b01a9201SJaegeuk Kim 	struct flush_cmd_control *fcc_info;
1068a688b9d9SGu Zheng 
10690b54fb84SJaegeuk Kim 	/* for discard command control */
10700b54fb84SJaegeuk Kim 	struct discard_cmd_control *dcc_info;
107139a53e0cSJaegeuk Kim };
107239a53e0cSJaegeuk Kim 
107339a53e0cSJaegeuk Kim /*
107439a53e0cSJaegeuk Kim  * For superblock
107539a53e0cSJaegeuk Kim  */
107639a53e0cSJaegeuk Kim /*
107739a53e0cSJaegeuk Kim  * COUNT_TYPE for monitoring
107839a53e0cSJaegeuk Kim  *
107939a53e0cSJaegeuk Kim  * f2fs monitors the number of several block types such as on-writeback,
108039a53e0cSJaegeuk Kim  * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
108139a53e0cSJaegeuk Kim  */
108236951b38SChao Yu #define WB_DATA_TYPE(p)	(__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
108339a53e0cSJaegeuk Kim enum count_type {
108439a53e0cSJaegeuk Kim 	F2FS_DIRTY_DENTS,
1085c227f912SChao Yu 	F2FS_DIRTY_DATA,
10862c8a4a28SJaegeuk Kim 	F2FS_DIRTY_QDATA,
108739a53e0cSJaegeuk Kim 	F2FS_DIRTY_NODES,
108839a53e0cSJaegeuk Kim 	F2FS_DIRTY_META,
10890f18b462SJaegeuk Kim 	F2FS_DIRTY_IMETA,
109036951b38SChao Yu 	F2FS_WB_CP_DATA,
109136951b38SChao Yu 	F2FS_WB_DATA,
10925f9abab4SJaegeuk Kim 	F2FS_RD_DATA,
10935f9abab4SJaegeuk Kim 	F2FS_RD_NODE,
10945f9abab4SJaegeuk Kim 	F2FS_RD_META,
109502b16d0aSChao Yu 	F2FS_DIO_WRITE,
109602b16d0aSChao Yu 	F2FS_DIO_READ,
109739a53e0cSJaegeuk Kim 	NR_COUNT_TYPE,
109839a53e0cSJaegeuk Kim };
109939a53e0cSJaegeuk Kim 
110039a53e0cSJaegeuk Kim /*
1101e1c42045Sarter97  * The below are the page types of bios used in submit_bio().
110239a53e0cSJaegeuk Kim  * The available types are:
110339a53e0cSJaegeuk Kim  * DATA			User data pages. It operates as async mode.
110439a53e0cSJaegeuk Kim  * NODE			Node pages. It operates as async mode.
110539a53e0cSJaegeuk Kim  * META			FS metadata pages such as SIT, NAT, CP.
110639a53e0cSJaegeuk Kim  * NR_PAGE_TYPE		The number of page types.
110739a53e0cSJaegeuk Kim  * META_FLUSH		Make sure the previous pages are written
110839a53e0cSJaegeuk Kim  *			with waiting the bio's completion
110939a53e0cSJaegeuk Kim  * ...			Only can be used with META.
111039a53e0cSJaegeuk Kim  */
11117d5e5109SJaegeuk Kim #define PAGE_TYPE_OF_BIO(type)	((type) > META ? META : (type))
111239a53e0cSJaegeuk Kim enum page_type {
11136b8beca0SChao Yu 	DATA = 0,
11146b8beca0SChao Yu 	NODE = 1,	/* should not change this */
111539a53e0cSJaegeuk Kim 	META,
111639a53e0cSJaegeuk Kim 	NR_PAGE_TYPE,
111739a53e0cSJaegeuk Kim 	META_FLUSH,
11183db1de0eSDaeho Jeong 	IPU,		/* the below types are used by tracepoints only. */
11198ce67cb0SJaegeuk Kim 	OPU,
112039a53e0cSJaegeuk Kim };
112139a53e0cSJaegeuk Kim 
1122a912b54dSJaegeuk Kim enum temp_type {
1123a912b54dSJaegeuk Kim 	HOT = 0,	/* must be zero for meta bio */
1124a912b54dSJaegeuk Kim 	WARM,
1125a912b54dSJaegeuk Kim 	COLD,
1126a912b54dSJaegeuk Kim 	NR_TEMP_TYPE,
1127a912b54dSJaegeuk Kim };
1128a912b54dSJaegeuk Kim 
1129cc15620bSJaegeuk Kim enum need_lock_type {
1130cc15620bSJaegeuk Kim 	LOCK_REQ = 0,
1131cc15620bSJaegeuk Kim 	LOCK_DONE,
1132cc15620bSJaegeuk Kim 	LOCK_RETRY,
1133cc15620bSJaegeuk Kim };
1134cc15620bSJaegeuk Kim 
1135a5fd5050SChao Yu enum cp_reason_type {
1136a5fd5050SChao Yu 	CP_NO_NEEDED,
1137a5fd5050SChao Yu 	CP_NON_REGULAR,
11384c8ff709SChao Yu 	CP_COMPRESSED,
1139a5fd5050SChao Yu 	CP_HARDLINK,
1140a5fd5050SChao Yu 	CP_SB_NEED_CP,
1141a5fd5050SChao Yu 	CP_WRONG_PINO,
1142a5fd5050SChao Yu 	CP_NO_SPC_ROLL,
1143a5fd5050SChao Yu 	CP_NODE_NEED_CP,
1144a5fd5050SChao Yu 	CP_FASTBOOT_MODE,
1145a5fd5050SChao Yu 	CP_SPEC_LOG_NUM,
11460a007b97SJaegeuk Kim 	CP_RECOVER_DIR,
1147a5fd5050SChao Yu };
1148a5fd5050SChao Yu 
1149b0af6d49SChao Yu enum iostat_type {
11508b83ac81SChao Yu 	/* WRITE IO */
11518b83ac81SChao Yu 	APP_DIRECT_IO,			/* app direct write IOs */
11528b83ac81SChao Yu 	APP_BUFFERED_IO,		/* app buffered write IOs */
1153b0af6d49SChao Yu 	APP_WRITE_IO,			/* app write IOs */
1154b0af6d49SChao Yu 	APP_MAPPED_IO,			/* app mapped IOs */
1155b0af6d49SChao Yu 	FS_DATA_IO,			/* data IOs from kworker/fsync/reclaimer */
1156b0af6d49SChao Yu 	FS_NODE_IO,			/* node IOs from kworker/fsync/reclaimer */
1157b0af6d49SChao Yu 	FS_META_IO,			/* meta IOs from kworker/reclaimer */
1158b0af6d49SChao Yu 	FS_GC_DATA_IO,			/* data IOs from forground gc */
1159b0af6d49SChao Yu 	FS_GC_NODE_IO,			/* node IOs from forground gc */
1160b0af6d49SChao Yu 	FS_CP_DATA_IO,			/* data IOs from checkpoint */
1161b0af6d49SChao Yu 	FS_CP_NODE_IO,			/* node IOs from checkpoint */
1162b0af6d49SChao Yu 	FS_CP_META_IO,			/* meta IOs from checkpoint */
11638b83ac81SChao Yu 
11648b83ac81SChao Yu 	/* READ IO */
11658b83ac81SChao Yu 	APP_DIRECT_READ_IO,		/* app direct read IOs */
11668b83ac81SChao Yu 	APP_BUFFERED_READ_IO,		/* app buffered read IOs */
11678b83ac81SChao Yu 	APP_READ_IO,			/* app read IOs */
11688b83ac81SChao Yu 	APP_MAPPED_READ_IO,		/* app mapped read IOs */
11698b83ac81SChao Yu 	FS_DATA_READ_IO,		/* data read IOs */
11709c122384SChao Yu 	FS_GDATA_READ_IO,		/* data read IOs from background gc */
11719c122384SChao Yu 	FS_CDATA_READ_IO,		/* compressed data read IOs */
11728b83ac81SChao Yu 	FS_NODE_READ_IO,		/* node read IOs */
11738b83ac81SChao Yu 	FS_META_READ_IO,		/* meta read IOs */
11748b83ac81SChao Yu 
11758b83ac81SChao Yu 	/* other */
1176b0af6d49SChao Yu 	FS_DISCARD,			/* discard */
1177b0af6d49SChao Yu 	NR_IO_TYPE,
1178b0af6d49SChao Yu };
1179b0af6d49SChao Yu 
1180458e6197SJaegeuk Kim struct f2fs_io_info {
118105ca3632SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs_sb_info pointer */
118239d787beSChao Yu 	nid_t ino;		/* inode number */
1183458e6197SJaegeuk Kim 	enum page_type type;	/* contains DATA/NODE/META/META_FLUSH */
1184a912b54dSJaegeuk Kim 	enum temp_type temp;	/* contains HOT/WARM/COLD */
118504d328deSMike Christie 	int op;			/* contains REQ_OP_ */
1186ef295ecfSChristoph Hellwig 	int op_flags;		/* req_flag_bits */
11877a9d7548SChao Yu 	block_t new_blkaddr;	/* new block address to be written */
118828bc106bSChao Yu 	block_t old_blkaddr;	/* old block address before Cow */
118905ca3632SJaegeuk Kim 	struct page *page;	/* page to be written */
11904375a336SJaegeuk Kim 	struct page *encrypted_page;	/* encrypted page */
11914c8ff709SChao Yu 	struct page *compressed_page;	/* compressed page */
1192fb830fc5SChao Yu 	struct list_head list;		/* serialize IOs */
1193d68f735bSJaegeuk Kim 	bool submitted;		/* indicate IO submission */
1194cc15620bSJaegeuk Kim 	int need_lock;		/* indicate we need to lock cp_rwsem */
1195fb830fc5SChao Yu 	bool in_list;		/* indicate fio is in io_list */
11966dc3a126SChao Yu 	bool is_por;		/* indicate IO is from recovery or not */
1197fe16efe6SChao Yu 	bool retry;		/* need to reallocate block address */
11984c8ff709SChao Yu 	int compr_blocks;	/* # of compressed block addresses */
11994c8ff709SChao Yu 	bool encrypted;		/* indicate file is encrypted */
1200b0af6d49SChao Yu 	enum iostat_type io_type;	/* io type */
1201578c6478SYufen Yu 	struct writeback_control *io_wbc; /* writeback control */
12028648de2cSChao Yu 	struct bio **bio;		/* bio for ipu */
12038648de2cSChao Yu 	sector_t *last_block;		/* last block number in bio */
12047735730dSChao Yu 	unsigned char version;		/* version of the node */
1205458e6197SJaegeuk Kim };
1206458e6197SJaegeuk Kim 
12070b20fcecSChao Yu struct bio_entry {
12080b20fcecSChao Yu 	struct bio *bio;
12090b20fcecSChao Yu 	struct list_head list;
12100b20fcecSChao Yu };
12110b20fcecSChao Yu 
121268afcf2dSTomohiro Kusumi #define is_read_io(rw) ((rw) == READ)
12131ff7bd3bSJaegeuk Kim struct f2fs_bio_info {
1214458e6197SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs superblock */
12151ff7bd3bSJaegeuk Kim 	struct bio *bio;		/* bios to merge */
12161ff7bd3bSJaegeuk Kim 	sector_t last_block_in_bio;	/* last block number */
1217458e6197SJaegeuk Kim 	struct f2fs_io_info fio;	/* store buffered io info. */
1218e4544b63STim Murray 	struct f2fs_rwsem io_rwsem;	/* blocking op for bio */
1219fb830fc5SChao Yu 	spinlock_t io_lock;		/* serialize DATA/NODE IOs */
1220fb830fc5SChao Yu 	struct list_head io_list;	/* track fios */
12210b20fcecSChao Yu 	struct list_head bio_list;	/* bio entry list head */
1222e4544b63STim Murray 	struct f2fs_rwsem bio_list_lock;	/* lock to protect bio entry list */
12231ff7bd3bSJaegeuk Kim };
12241ff7bd3bSJaegeuk Kim 
12253c62be17SJaegeuk Kim #define FDEV(i)				(sbi->devs[i])
12263c62be17SJaegeuk Kim #define RDEV(i)				(raw_super->devs[i])
12273c62be17SJaegeuk Kim struct f2fs_dev_info {
12283c62be17SJaegeuk Kim 	struct block_device *bdev;
12293c62be17SJaegeuk Kim 	char path[MAX_PATH_LEN];
12303c62be17SJaegeuk Kim 	unsigned int total_segments;
12313c62be17SJaegeuk Kim 	block_t start_blk;
12323c62be17SJaegeuk Kim 	block_t end_blk;
12333c62be17SJaegeuk Kim #ifdef CONFIG_BLK_DEV_ZONED
12343c62be17SJaegeuk Kim 	unsigned int nr_blkz;		/* Total number of zones */
123595175dafSDamien Le Moal 	unsigned long *blkz_seq;	/* Bitmap indicating sequential zones */
1236de881df9SAravind Ramesh 	block_t *zone_capacity_blocks;  /* Array of zone capacity in blks */
12373c62be17SJaegeuk Kim #endif
12383c62be17SJaegeuk Kim };
12393c62be17SJaegeuk Kim 
1240c227f912SChao Yu enum inode_type {
1241c227f912SChao Yu 	DIR_INODE,			/* for dirty dir inode */
1242c227f912SChao Yu 	FILE_INODE,			/* for dirty regular/symlink inode */
12430f18b462SJaegeuk Kim 	DIRTY_META,			/* for all dirtied inode metadata */
124457864ae5SJaegeuk Kim 	ATOMIC_FILE,			/* for all atomic files */
1245c227f912SChao Yu 	NR_INODE_TYPE,
1246c227f912SChao Yu };
1247c227f912SChao Yu 
124867298804SChao Yu /* for inner inode cache management */
124967298804SChao Yu struct inode_management {
125067298804SChao Yu 	struct radix_tree_root ino_root;	/* ino entry array */
125167298804SChao Yu 	spinlock_t ino_lock;			/* for ino entry lock */
125267298804SChao Yu 	struct list_head ino_list;		/* inode list head */
125367298804SChao Yu 	unsigned long ino_num;			/* number of entries */
125467298804SChao Yu };
125567298804SChao Yu 
1256093749e2SChao Yu /* for GC_AT */
1257093749e2SChao Yu struct atgc_management {
1258093749e2SChao Yu 	bool atgc_enabled;			/* ATGC is enabled or not */
1259093749e2SChao Yu 	struct rb_root_cached root;		/* root of victim rb-tree */
1260093749e2SChao Yu 	struct list_head victim_list;		/* linked with all victim entries */
1261093749e2SChao Yu 	unsigned int victim_count;		/* victim count in rb-tree */
1262093749e2SChao Yu 	unsigned int candidate_ratio;		/* candidate ratio */
1263093749e2SChao Yu 	unsigned int max_candidate_count;	/* max candidate count */
1264093749e2SChao Yu 	unsigned int age_weight;		/* age weight, vblock_weight = 100 - age_weight */
1265093749e2SChao Yu 	unsigned long long age_threshold;	/* age threshold */
1266093749e2SChao Yu };
1267093749e2SChao Yu 
1268d147ea4aSJaegeuk Kim struct f2fs_gc_control {
1269d147ea4aSJaegeuk Kim 	unsigned int victim_segno;	/* target victim segment number */
1270d147ea4aSJaegeuk Kim 	int init_gc_type;		/* FG_GC or BG_GC */
1271d147ea4aSJaegeuk Kim 	bool no_bg_gc;			/* check the space and stop bg_gc */
1272d147ea4aSJaegeuk Kim 	bool should_migrate_blocks;	/* should migrate blocks */
1273d147ea4aSJaegeuk Kim 	bool err_gc_skipped;		/* return EAGAIN if GC skipped */
1274c81d5baeSJaegeuk Kim 	unsigned int nr_free_secs;	/* # of free sections to do GC */
1275d147ea4aSJaegeuk Kim };
1276d147ea4aSJaegeuk Kim 
1277caf0047eSChao Yu /* For s_flag in struct f2fs_sb_info */
1278caf0047eSChao Yu enum {
1279caf0047eSChao Yu 	SBI_IS_DIRTY,				/* dirty flag for checkpoint */
1280caf0047eSChao Yu 	SBI_IS_CLOSE,				/* specify unmounting */
1281caf0047eSChao Yu 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
1282caf0047eSChao Yu 	SBI_POR_DOING,				/* recovery is doing or not */
1283df728b0fSJaegeuk Kim 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
1284bbf156f7SJaegeuk Kim 	SBI_NEED_CP,				/* need to checkpoint */
128583a3bfdbSJaegeuk Kim 	SBI_IS_SHUTDOWN,			/* shutdown by ioctl */
12861378752bSChao Yu 	SBI_IS_RECOVERED,			/* recovered orphan/data */
12874354994fSDaniel Rosenberg 	SBI_CP_DISABLED,			/* CP was disabled last mount */
1288db610a64SJaegeuk Kim 	SBI_CP_DISABLED_QUICK,			/* CP was disabled quickly */
1289af033b2aSChao Yu 	SBI_QUOTA_NEED_FLUSH,			/* need to flush quota info in CP */
1290af033b2aSChao Yu 	SBI_QUOTA_SKIP_FLUSH,			/* skip flushing quota in current CP */
1291af033b2aSChao Yu 	SBI_QUOTA_NEED_REPAIR,			/* quota file may be corrupted */
129204f0b2eaSQiuyang Sun 	SBI_IS_RESIZEFS,			/* resizefs is in process */
1293ba900534SJaegeuk Kim 	SBI_IS_FREEZING,			/* freezefs is in process */
1294caf0047eSChao Yu };
1295caf0047eSChao Yu 
12966beceb54SJaegeuk Kim enum {
12976beceb54SJaegeuk Kim 	CP_TIME,
1298d0239e1bSJaegeuk Kim 	REQ_TIME,
1299a7d10cf3SSahitya Tummala 	DISCARD_TIME,
1300a7d10cf3SSahitya Tummala 	GC_TIME,
13014354994fSDaniel Rosenberg 	DISABLE_TIME,
130203f2c02dSJaegeuk Kim 	UMOUNT_DISCARD_TIMEOUT,
13036beceb54SJaegeuk Kim 	MAX_TIME,
13046beceb54SJaegeuk Kim };
13056beceb54SJaegeuk Kim 
13060cdd3195SHyunchul Lee enum {
13075b0e9539SJaegeuk Kim 	GC_NORMAL,
13085b0e9539SJaegeuk Kim 	GC_IDLE_CB,
13095b0e9539SJaegeuk Kim 	GC_IDLE_GREEDY,
1310093749e2SChao Yu 	GC_IDLE_AT,
13110e5e8111SDaeho Jeong 	GC_URGENT_HIGH,
13120e5e8111SDaeho Jeong 	GC_URGENT_LOW,
1313d98af5f4SDaeho Jeong 	GC_URGENT_MID,
131407c6b593SDaeho Jeong 	MAX_GC_MODE,
13155b0e9539SJaegeuk Kim };
13165b0e9539SJaegeuk Kim 
13175b0e9539SJaegeuk Kim enum {
1318bbbc34fdSChao Yu 	BGGC_MODE_ON,		/* background gc is on */
1319bbbc34fdSChao Yu 	BGGC_MODE_OFF,		/* background gc is off */
1320bbbc34fdSChao Yu 	BGGC_MODE_SYNC,		/*
1321bbbc34fdSChao Yu 				 * background gc is on, migrating blocks
1322bbbc34fdSChao Yu 				 * like foreground gc
1323bbbc34fdSChao Yu 				 */
1324bbbc34fdSChao Yu };
1325bbbc34fdSChao Yu 
1326bbbc34fdSChao Yu enum {
1327b0332a0fSChao Yu 	FS_MODE_ADAPTIVE,		/* use both lfs/ssr allocation */
1328b0332a0fSChao Yu 	FS_MODE_LFS,			/* use lfs allocation only */
13296691d940SDaeho Jeong 	FS_MODE_FRAGMENT_SEG,		/* segment fragmentation mode */
13306691d940SDaeho Jeong 	FS_MODE_FRAGMENT_BLK,		/* block fragmentation mode */
1331b0332a0fSChao Yu };
1332b0332a0fSChao Yu 
1333b0332a0fSChao Yu enum {
133407939627SJaegeuk Kim 	ALLOC_MODE_DEFAULT,	/* stay default */
133507939627SJaegeuk Kim 	ALLOC_MODE_REUSE,	/* reuse segments as much as possible */
133607939627SJaegeuk Kim };
133707939627SJaegeuk Kim 
133893cf93f1SJunling Zheng enum fsync_mode {
133993cf93f1SJunling Zheng 	FSYNC_MODE_POSIX,	/* fsync follows posix semantics */
134093cf93f1SJunling Zheng 	FSYNC_MODE_STRICT,	/* fsync behaves in line with ext4 */
1341d6290814SJaegeuk Kim 	FSYNC_MODE_NOBARRIER,	/* fsync behaves nobarrier based on posix */
134293cf93f1SJunling Zheng };
134393cf93f1SJunling Zheng 
1344602a16d5SDaeho Jeong enum {
1345602a16d5SDaeho Jeong 	COMPR_MODE_FS,		/*
1346602a16d5SDaeho Jeong 				 * automatically compress compression
1347602a16d5SDaeho Jeong 				 * enabled files
1348602a16d5SDaeho Jeong 				 */
1349602a16d5SDaeho Jeong 	COMPR_MODE_USER,	/*
1350602a16d5SDaeho Jeong 				 * automatical compression is disabled.
1351602a16d5SDaeho Jeong 				 * user can control the file compression
1352602a16d5SDaeho Jeong 				 * using ioctls
1353602a16d5SDaeho Jeong 				 */
1354602a16d5SDaeho Jeong };
1355602a16d5SDaeho Jeong 
13564f993264SChao Yu enum {
13574f993264SChao Yu 	DISCARD_UNIT_BLOCK,	/* basic discard unit is block */
13584f993264SChao Yu 	DISCARD_UNIT_SEGMENT,	/* basic discard unit is segment */
13594f993264SChao Yu 	DISCARD_UNIT_SECTION,	/* basic discard unit is section */
13604f993264SChao Yu };
13614f993264SChao Yu 
1362b763f3beSChao Yu static inline int f2fs_test_bit(unsigned int nr, char *addr);
1363b763f3beSChao Yu static inline void f2fs_set_bit(unsigned int nr, char *addr);
1364b763f3beSChao Yu static inline void f2fs_clear_bit(unsigned int nr, char *addr);
13654c8ff709SChao Yu 
1366b763f3beSChao Yu /*
1367b763f3beSChao Yu  * Layout of f2fs page.private:
1368b763f3beSChao Yu  *
1369b763f3beSChao Yu  * Layout A: lowest bit should be 1
1370b763f3beSChao Yu  * | bit0 = 1 | bit1 | bit2 | ... | bit MAX | private data .... |
1371b763f3beSChao Yu  * bit 0	PAGE_PRIVATE_NOT_POINTER
1372b763f3beSChao Yu  * bit 1	PAGE_PRIVATE_ATOMIC_WRITE
1373b763f3beSChao Yu  * bit 2	PAGE_PRIVATE_DUMMY_WRITE
1374b763f3beSChao Yu  * bit 3	PAGE_PRIVATE_ONGOING_MIGRATION
1375b763f3beSChao Yu  * bit 4	PAGE_PRIVATE_INLINE_INODE
1376b763f3beSChao Yu  * bit 5	PAGE_PRIVATE_REF_RESOURCE
1377b763f3beSChao Yu  * bit 6-	f2fs private data
1378b763f3beSChao Yu  *
1379b763f3beSChao Yu  * Layout B: lowest bit should be 0
1380b763f3beSChao Yu  * page.private is a wrapped pointer.
1381b763f3beSChao Yu  */
1382b763f3beSChao Yu enum {
1383b763f3beSChao Yu 	PAGE_PRIVATE_NOT_POINTER,		/* private contains non-pointer data */
1384b763f3beSChao Yu 	PAGE_PRIVATE_ATOMIC_WRITE,		/* data page from atomic write path */
1385b763f3beSChao Yu 	PAGE_PRIVATE_DUMMY_WRITE,		/* data page for padding aligned IO */
1386b763f3beSChao Yu 	PAGE_PRIVATE_ONGOING_MIGRATION,		/* data page which is on-going migrating */
1387b763f3beSChao Yu 	PAGE_PRIVATE_INLINE_INODE,		/* inode page contains inline data */
1388b763f3beSChao Yu 	PAGE_PRIVATE_REF_RESOURCE,		/* dirty page has referenced resources */
1389b763f3beSChao Yu 	PAGE_PRIVATE_MAX
1390b763f3beSChao Yu };
1391b763f3beSChao Yu 
1392b763f3beSChao Yu #define PAGE_PRIVATE_GET_FUNC(name, flagname) \
1393b763f3beSChao Yu static inline bool page_private_##name(struct page *page) \
1394b763f3beSChao Yu { \
1395c9ebd3dfSJaegeuk Kim 	return PagePrivate(page) && \
1396c9ebd3dfSJaegeuk Kim 		test_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page)) && \
1397b763f3beSChao Yu 		test_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
1398b763f3beSChao Yu }
1399b763f3beSChao Yu 
1400b763f3beSChao Yu #define PAGE_PRIVATE_SET_FUNC(name, flagname) \
1401b763f3beSChao Yu static inline void set_page_private_##name(struct page *page) \
1402b763f3beSChao Yu { \
1403b763f3beSChao Yu 	if (!PagePrivate(page)) { \
1404b763f3beSChao Yu 		get_page(page); \
1405b763f3beSChao Yu 		SetPagePrivate(page); \
1406c9ebd3dfSJaegeuk Kim 		set_page_private(page, 0); \
1407b763f3beSChao Yu 	} \
1408b763f3beSChao Yu 	set_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page)); \
1409b763f3beSChao Yu 	set_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
1410b763f3beSChao Yu }
1411b763f3beSChao Yu 
1412b763f3beSChao Yu #define PAGE_PRIVATE_CLEAR_FUNC(name, flagname) \
1413b763f3beSChao Yu static inline void clear_page_private_##name(struct page *page) \
1414b763f3beSChao Yu { \
1415b763f3beSChao Yu 	clear_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
1416b763f3beSChao Yu 	if (page_private(page) == 1 << PAGE_PRIVATE_NOT_POINTER) { \
1417b763f3beSChao Yu 		set_page_private(page, 0); \
1418b763f3beSChao Yu 		if (PagePrivate(page)) { \
1419b763f3beSChao Yu 			ClearPagePrivate(page); \
1420b763f3beSChao Yu 			put_page(page); \
1421b763f3beSChao Yu 		}\
1422b763f3beSChao Yu 	} \
1423b763f3beSChao Yu }
1424b763f3beSChao Yu 
1425b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(nonpointer, NOT_POINTER);
1426b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(reference, REF_RESOURCE);
1427b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(inline, INLINE_INODE);
1428b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(gcing, ONGOING_MIGRATION);
1429b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(atomic, ATOMIC_WRITE);
1430b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(dummy, DUMMY_WRITE);
1431b763f3beSChao Yu 
1432b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(reference, REF_RESOURCE);
1433b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(inline, INLINE_INODE);
1434b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(gcing, ONGOING_MIGRATION);
1435b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(atomic, ATOMIC_WRITE);
1436b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(dummy, DUMMY_WRITE);
1437b763f3beSChao Yu 
1438b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(reference, REF_RESOURCE);
1439b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(inline, INLINE_INODE);
1440b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(gcing, ONGOING_MIGRATION);
1441b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(atomic, ATOMIC_WRITE);
1442b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(dummy, DUMMY_WRITE);
14434c8ff709SChao Yu 
14446ce19affSChao Yu static inline unsigned long get_page_private_data(struct page *page)
14456ce19affSChao Yu {
14466ce19affSChao Yu 	unsigned long data = page_private(page);
14476ce19affSChao Yu 
14486ce19affSChao Yu 	if (!test_bit(PAGE_PRIVATE_NOT_POINTER, &data))
14496ce19affSChao Yu 		return 0;
14506ce19affSChao Yu 	return data >> PAGE_PRIVATE_MAX;
14516ce19affSChao Yu }
14526ce19affSChao Yu 
14536ce19affSChao Yu static inline void set_page_private_data(struct page *page, unsigned long data)
14546ce19affSChao Yu {
14556ce19affSChao Yu 	if (!PagePrivate(page)) {
14566ce19affSChao Yu 		get_page(page);
14576ce19affSChao Yu 		SetPagePrivate(page);
1458c9ebd3dfSJaegeuk Kim 		set_page_private(page, 0);
14596ce19affSChao Yu 	}
14606ce19affSChao Yu 	set_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page));
14616ce19affSChao Yu 	page_private(page) |= data << PAGE_PRIVATE_MAX;
14626ce19affSChao Yu }
14636ce19affSChao Yu 
14646ce19affSChao Yu static inline void clear_page_private_data(struct page *page)
14656ce19affSChao Yu {
14666ce19affSChao Yu 	page_private(page) &= (1 << PAGE_PRIVATE_MAX) - 1;
14676ce19affSChao Yu 	if (page_private(page) == 1 << PAGE_PRIVATE_NOT_POINTER) {
14686ce19affSChao Yu 		set_page_private(page, 0);
14696ce19affSChao Yu 		if (PagePrivate(page)) {
14706ce19affSChao Yu 			ClearPagePrivate(page);
14716ce19affSChao Yu 			put_page(page);
14726ce19affSChao Yu 		}
14736ce19affSChao Yu 	}
14746ce19affSChao Yu }
14756ce19affSChao Yu 
14764c8ff709SChao Yu /* For compression */
14774c8ff709SChao Yu enum compress_algorithm_type {
14784c8ff709SChao Yu 	COMPRESS_LZO,
14794c8ff709SChao Yu 	COMPRESS_LZ4,
148050cfa66fSChao Yu 	COMPRESS_ZSTD,
14816d92b201SChao Yu 	COMPRESS_LZORLE,
14824c8ff709SChao Yu 	COMPRESS_MAX,
14834c8ff709SChao Yu };
14844c8ff709SChao Yu 
1485b28f047bSChao Yu enum compress_flag {
1486b28f047bSChao Yu 	COMPRESS_CHKSUM,
1487b28f047bSChao Yu 	COMPRESS_MAX_FLAG,
1488b28f047bSChao Yu };
1489b28f047bSChao Yu 
14906ce19affSChao Yu #define	COMPRESS_WATERMARK			20
14916ce19affSChao Yu #define	COMPRESS_PERCENT			20
14926ce19affSChao Yu 
1493b28f047bSChao Yu #define COMPRESS_DATA_RESERVED_SIZE		4
14944c8ff709SChao Yu struct compress_data {
14954c8ff709SChao Yu 	__le32 clen;			/* compressed data size */
1496b28f047bSChao Yu 	__le32 chksum;			/* compressed data chksum */
14974c8ff709SChao Yu 	__le32 reserved[COMPRESS_DATA_RESERVED_SIZE];	/* reserved */
14984c8ff709SChao Yu 	u8 cdata[];			/* compressed data */
14994c8ff709SChao Yu };
15004c8ff709SChao Yu 
15014c8ff709SChao Yu #define COMPRESS_HEADER_SIZE	(sizeof(struct compress_data))
15024c8ff709SChao Yu 
15034c8ff709SChao Yu #define F2FS_COMPRESSED_PAGE_MAGIC	0xF5F2C000
15044c8ff709SChao Yu 
15053fde13f8SChao Yu #define	COMPRESS_LEVEL_OFFSET	8
15063fde13f8SChao Yu 
15074c8ff709SChao Yu /* compress context */
15084c8ff709SChao Yu struct compress_ctx {
15094c8ff709SChao Yu 	struct inode *inode;		/* inode the context belong to */
15104c8ff709SChao Yu 	pgoff_t cluster_idx;		/* cluster index number */
15114c8ff709SChao Yu 	unsigned int cluster_size;	/* page count in cluster */
15124c8ff709SChao Yu 	unsigned int log_cluster_size;	/* log of cluster size */
15134c8ff709SChao Yu 	struct page **rpages;		/* pages store raw data in cluster */
15144c8ff709SChao Yu 	unsigned int nr_rpages;		/* total page number in rpages */
15154c8ff709SChao Yu 	struct page **cpages;		/* pages store compressed data in cluster */
15164c8ff709SChao Yu 	unsigned int nr_cpages;		/* total page number in cpages */
15173271d7ebSFengnan Chang 	unsigned int valid_nr_cpages;	/* valid page number in cpages */
15184c8ff709SChao Yu 	void *rbuf;			/* virtual mapped address on rpages */
15194c8ff709SChao Yu 	struct compress_data *cbuf;	/* virtual mapped address on cpages */
15204c8ff709SChao Yu 	size_t rlen;			/* valid data length in rbuf */
15214c8ff709SChao Yu 	size_t clen;			/* valid data length in cbuf */
15224c8ff709SChao Yu 	void *private;			/* payload buffer for specified compression algorithm */
152350cfa66fSChao Yu 	void *private2;			/* extra payload buffer */
15244c8ff709SChao Yu };
15254c8ff709SChao Yu 
15264c8ff709SChao Yu /* compress context for write IO path */
15274c8ff709SChao Yu struct compress_io_ctx {
15284c8ff709SChao Yu 	u32 magic;			/* magic number to indicate page is compressed */
15294c8ff709SChao Yu 	struct inode *inode;		/* inode the context belong to */
15304c8ff709SChao Yu 	struct page **rpages;		/* pages store raw data in cluster */
15314c8ff709SChao Yu 	unsigned int nr_rpages;		/* total page number in rpages */
1532e6c3948dSChao Yu 	atomic_t pending_pages;		/* in-flight compressed page count */
15334c8ff709SChao Yu };
15344c8ff709SChao Yu 
15357f59b277SEric Biggers /* Context for decompressing one cluster on the read IO path */
15364c8ff709SChao Yu struct decompress_io_ctx {
15374c8ff709SChao Yu 	u32 magic;			/* magic number to indicate page is compressed */
15384c8ff709SChao Yu 	struct inode *inode;		/* inode the context belong to */
15394c8ff709SChao Yu 	pgoff_t cluster_idx;		/* cluster index number */
15404c8ff709SChao Yu 	unsigned int cluster_size;	/* page count in cluster */
15414c8ff709SChao Yu 	unsigned int log_cluster_size;	/* log of cluster size */
15424c8ff709SChao Yu 	struct page **rpages;		/* pages store raw data in cluster */
15434c8ff709SChao Yu 	unsigned int nr_rpages;		/* total page number in rpages */
15444c8ff709SChao Yu 	struct page **cpages;		/* pages store compressed data in cluster */
15454c8ff709SChao Yu 	unsigned int nr_cpages;		/* total page number in cpages */
15464c8ff709SChao Yu 	struct page **tpages;		/* temp pages to pad holes in cluster */
15474c8ff709SChao Yu 	void *rbuf;			/* virtual mapped address on rpages */
15484c8ff709SChao Yu 	struct compress_data *cbuf;	/* virtual mapped address on cpages */
15494c8ff709SChao Yu 	size_t rlen;			/* valid data length in rbuf */
15504c8ff709SChao Yu 	size_t clen;			/* valid data length in cbuf */
15517f59b277SEric Biggers 
15527f59b277SEric Biggers 	/*
15537f59b277SEric Biggers 	 * The number of compressed pages remaining to be read in this cluster.
15547f59b277SEric Biggers 	 * This is initially nr_cpages.  It is decremented by 1 each time a page
15557f59b277SEric Biggers 	 * has been read (or failed to be read).  When it reaches 0, the cluster
15567f59b277SEric Biggers 	 * is decompressed (or an error is reported).
15577f59b277SEric Biggers 	 *
15587f59b277SEric Biggers 	 * If an error occurs before all the pages have been submitted for I/O,
15597f59b277SEric Biggers 	 * then this will never reach 0.  In this case the I/O submitter is
15607f59b277SEric Biggers 	 * responsible for calling f2fs_decompress_end_io() instead.
15617f59b277SEric Biggers 	 */
15627f59b277SEric Biggers 	atomic_t remaining_pages;
15637f59b277SEric Biggers 
15647f59b277SEric Biggers 	/*
15657f59b277SEric Biggers 	 * Number of references to this decompress_io_ctx.
15667f59b277SEric Biggers 	 *
15677f59b277SEric Biggers 	 * One reference is held for I/O completion.  This reference is dropped
15687f59b277SEric Biggers 	 * after the pagecache pages are updated and unlocked -- either after
15697f59b277SEric Biggers 	 * decompression (and verity if enabled), or after an error.
15707f59b277SEric Biggers 	 *
15717f59b277SEric Biggers 	 * In addition, each compressed page holds a reference while it is in a
15727f59b277SEric Biggers 	 * bio.  These references are necessary prevent compressed pages from
15737f59b277SEric Biggers 	 * being freed while they are still in a bio.
15747f59b277SEric Biggers 	 */
15757f59b277SEric Biggers 	refcount_t refcnt;
15767f59b277SEric Biggers 
15777f59b277SEric Biggers 	bool failed;			/* IO error occurred before decompression? */
15787f59b277SEric Biggers 	bool need_verity;		/* need fs-verity verification after decompression? */
157950cfa66fSChao Yu 	void *private;			/* payload buffer for specified decompression algorithm */
158050cfa66fSChao Yu 	void *private2;			/* extra payload buffer */
15817f59b277SEric Biggers 	struct work_struct verity_work;	/* work to verify the decompressed pages */
15824c8ff709SChao Yu };
15834c8ff709SChao Yu 
15844c8ff709SChao Yu #define NULL_CLUSTER			((unsigned int)(~0))
15854c8ff709SChao Yu #define MIN_COMPRESS_LOG_SIZE		2
15864c8ff709SChao Yu #define MAX_COMPRESS_LOG_SIZE		8
15870e2b7385SChao Yu #define MAX_COMPRESS_WINDOW_SIZE(log_size)	((PAGE_SIZE) << (log_size))
15884c8ff709SChao Yu 
158939a53e0cSJaegeuk Kim struct f2fs_sb_info {
159039a53e0cSJaegeuk Kim 	struct super_block *sb;			/* pointer to VFS super block */
15915e176d54SJaegeuk Kim 	struct proc_dir_entry *s_proc;		/* proc entry */
159239a53e0cSJaegeuk Kim 	struct f2fs_super_block *raw_super;	/* raw super block pointer */
1593e4544b63STim Murray 	struct f2fs_rwsem sb_lock;		/* lock for raw super block */
1594e8240f65SChao Yu 	int valid_super_block;			/* valid super block no */
1595fadb2fb8SChao Yu 	unsigned long s_flag;				/* flags for sbi */
1596853137ceSJaegeuk Kim 	struct mutex writepages;		/* mutex for writepages() */
159739a53e0cSJaegeuk Kim 
1598178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
1599178053e2SDamien Le Moal 	unsigned int blocks_per_blkz;		/* F2FS blocks per zone */
1600178053e2SDamien Le Moal 	unsigned int log_blocks_per_blkz;	/* log2 F2FS blocks per zone */
1601178053e2SDamien Le Moal #endif
1602178053e2SDamien Le Moal 
160339a53e0cSJaegeuk Kim 	/* for node-related operations */
160439a53e0cSJaegeuk Kim 	struct f2fs_nm_info *nm_info;		/* node manager */
160539a53e0cSJaegeuk Kim 	struct inode *node_inode;		/* cache node blocks */
160639a53e0cSJaegeuk Kim 
160739a53e0cSJaegeuk Kim 	/* for segment-related operations */
160839a53e0cSJaegeuk Kim 	struct f2fs_sm_info *sm_info;		/* segment manager */
16091ff7bd3bSJaegeuk Kim 
16101ff7bd3bSJaegeuk Kim 	/* for bio operations */
1611a912b54dSJaegeuk Kim 	struct f2fs_bio_info *write_io[NR_PAGE_TYPE];	/* for write bios */
1612107a805dSChao Yu 	/* keep migration IO order for LFS mode */
1613e4544b63STim Murray 	struct f2fs_rwsem io_order_lock;
16140a595ebaSJaegeuk Kim 	mempool_t *write_io_dummy;		/* Dummy pages */
1615a7b8618aSJaegeuk Kim 	pgoff_t page_eio_ofs[NR_PAGE_TYPE];	/* EIO page offset */
1616a7b8618aSJaegeuk Kim 	int page_eio_cnt[NR_PAGE_TYPE];		/* EIO count */
161739a53e0cSJaegeuk Kim 
161839a53e0cSJaegeuk Kim 	/* for checkpoint */
161939a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt;		/* raw checkpoint pointer */
16208508e44aSJaegeuk Kim 	int cur_cp_pack;			/* remain current cp pack */
1621aaec2b1dSChao Yu 	spinlock_t cp_lock;			/* for flag in ckpt */
162239a53e0cSJaegeuk Kim 	struct inode *meta_inode;		/* cache meta blocks */
1623e4544b63STim Murray 	struct f2fs_rwsem cp_global_sem;	/* checkpoint procedure lock */
1624e4544b63STim Murray 	struct f2fs_rwsem cp_rwsem;		/* blocking FS operations */
1625e4544b63STim Murray 	struct f2fs_rwsem node_write;		/* locking node writes */
1626e4544b63STim Murray 	struct f2fs_rwsem node_change;	/* locking node change */
1627fb51b5efSChangman Lee 	wait_queue_head_t cp_wait;
16286beceb54SJaegeuk Kim 	unsigned long last_time[MAX_TIME];	/* to store time in jiffies */
16296beceb54SJaegeuk Kim 	long interval_time[MAX_TIME];		/* to store thresholds */
1630261eeb9cSDaeho Jeong 	struct ckpt_req_control cprc_info;	/* for checkpoint request control */
163139a53e0cSJaegeuk Kim 
163267298804SChao Yu 	struct inode_management im[MAX_INO_ENTRY];	/* manage inode cache */
16336451e041SJaegeuk Kim 
163450fa53ecSChao Yu 	spinlock_t fsync_node_lock;		/* for node entry lock */
163550fa53ecSChao Yu 	struct list_head fsync_node_list;	/* node list head */
163650fa53ecSChao Yu 	unsigned int fsync_seg_id;		/* sequence id */
163750fa53ecSChao Yu 	unsigned int fsync_node_num;		/* number of node entries */
163850fa53ecSChao Yu 
16396451e041SJaegeuk Kim 	/* for orphan inode, use 0'th array */
16400d47c1adSGu Zheng 	unsigned int max_orphans;		/* max orphan inodes */
164139a53e0cSJaegeuk Kim 
1642c227f912SChao Yu 	/* for inode management */
1643c227f912SChao Yu 	struct list_head inode_list[NR_INODE_TYPE];	/* dirty inode list */
1644c227f912SChao Yu 	spinlock_t inode_lock[NR_INODE_TYPE];	/* for dirty inode list lock */
1645040d2bb3SChao Yu 	struct mutex flush_lock;		/* for flush exclusion */
164639a53e0cSJaegeuk Kim 
164713054c54SChao Yu 	/* for extent tree cache */
164813054c54SChao Yu 	struct radix_tree_root extent_tree_root;/* cache extent cache entries */
16495e8256acSYunlei He 	struct mutex extent_tree_lock;	/* locking extent radix tree */
165013054c54SChao Yu 	struct list_head extent_list;		/* lru list for shrinker */
165113054c54SChao Yu 	spinlock_t extent_lock;			/* locking extent lru list */
16527441ccefSJaegeuk Kim 	atomic_t total_ext_tree;		/* extent tree count */
1653137d09f0SJaegeuk Kim 	struct list_head zombie_list;		/* extent zombie tree list */
165474fd8d99SJaegeuk Kim 	atomic_t total_zombie_tree;		/* extent zombie tree count */
165513054c54SChao Yu 	atomic_t total_ext_node;		/* extent info count */
165613054c54SChao Yu 
165739a53e0cSJaegeuk Kim 	/* basic filesystem units */
165839a53e0cSJaegeuk Kim 	unsigned int log_sectors_per_block;	/* log2 sectors per block */
165939a53e0cSJaegeuk Kim 	unsigned int log_blocksize;		/* log2 block size */
166039a53e0cSJaegeuk Kim 	unsigned int blocksize;			/* block size */
166139a53e0cSJaegeuk Kim 	unsigned int root_ino_num;		/* root inode number*/
166239a53e0cSJaegeuk Kim 	unsigned int node_ino_num;		/* node inode number*/
166339a53e0cSJaegeuk Kim 	unsigned int meta_ino_num;		/* meta inode number*/
166439a53e0cSJaegeuk Kim 	unsigned int log_blocks_per_seg;	/* log2 blocks per segment */
166539a53e0cSJaegeuk Kim 	unsigned int blocks_per_seg;		/* blocks per segment */
166639a53e0cSJaegeuk Kim 	unsigned int segs_per_sec;		/* segments per section */
166739a53e0cSJaegeuk Kim 	unsigned int secs_per_zone;		/* sections per zone */
166839a53e0cSJaegeuk Kim 	unsigned int total_sections;		/* total section count */
166939a53e0cSJaegeuk Kim 	unsigned int total_node_count;		/* total node block count */
167039a53e0cSJaegeuk Kim 	unsigned int total_valid_node_count;	/* valid node block count */
1671ab9fa662SJaegeuk Kim 	int dir_level;				/* directory level */
1672f6df8f23SSheng Yong 	int readdir_ra;				/* readahead inode in readdir */
167310208567SJaegeuk Kim 	u64 max_io_bytes;			/* max io bytes to merge IOs */
167439a53e0cSJaegeuk Kim 
167539a53e0cSJaegeuk Kim 	block_t user_block_count;		/* # of user blocks */
167639a53e0cSJaegeuk Kim 	block_t total_valid_block_count;	/* # of valid blocks */
1677a66cdd98SJaegeuk Kim 	block_t discard_blks;			/* discard command candidats */
167839a53e0cSJaegeuk Kim 	block_t last_valid_block_count;		/* for recovery */
1679daeb433eSChao Yu 	block_t reserved_blocks;		/* configurable reserved blocks */
168080d42145SYunlong Song 	block_t current_reserved_blocks;	/* current reserved blocks */
1681daeb433eSChao Yu 
16824354994fSDaniel Rosenberg 	/* Additional tracking for no checkpoint mode */
16834354994fSDaniel Rosenberg 	block_t unusable_block_count;		/* # of blocks saved by last cp */
16844354994fSDaniel Rosenberg 
1685292c196aSChao Yu 	unsigned int nquota_files;		/* # of quota sysfile */
1686e4544b63STim Murray 	struct f2fs_rwsem quota_sem;		/* blocking cp for flags */
1687292c196aSChao Yu 
1688523be8a6SJaegeuk Kim 	/* # of pages, see count_type */
168935782b23SJaegeuk Kim 	atomic_t nr_pages[NR_COUNT_TYPE];
169041382ec4SJaegeuk Kim 	/* # of allocated blocks */
169141382ec4SJaegeuk Kim 	struct percpu_counter alloc_valid_block_count;
169247c8ebccSJaegeuk Kim 	/* # of node block writes as roll forward recovery */
169347c8ebccSJaegeuk Kim 	struct percpu_counter rf_node_block_count;
169439a53e0cSJaegeuk Kim 
1695687de7f1SJaegeuk Kim 	/* writeback control */
1696c29fd0c0SChao Yu 	atomic_t wb_sync_req[META];	/* count # of WB_SYNC threads */
1697687de7f1SJaegeuk Kim 
1698513c5f37SJaegeuk Kim 	/* valid inode count */
1699513c5f37SJaegeuk Kim 	struct percpu_counter total_valid_inode_count;
1700513c5f37SJaegeuk Kim 
170139a53e0cSJaegeuk Kim 	struct f2fs_mount_info mount_opt;	/* mount options */
170239a53e0cSJaegeuk Kim 
170339a53e0cSJaegeuk Kim 	/* for cleaning operations */
1704e4544b63STim Murray 	struct f2fs_rwsem gc_lock;		/*
1705fb24fea7SChao Yu 						 * semaphore for GC, avoid
1706fb24fea7SChao Yu 						 * race between GC and GC or CP
1707fb24fea7SChao Yu 						 */
170839a53e0cSJaegeuk Kim 	struct f2fs_gc_kthread	*gc_thread;	/* GC thread */
1709093749e2SChao Yu 	struct atgc_management am;		/* atgc management */
17105ec4e49fSJaegeuk Kim 	unsigned int cur_victim_sec;		/* current victim section num */
17115b0e9539SJaegeuk Kim 	unsigned int gc_mode;			/* current GC state */
1712e3080b01SChao Yu 	unsigned int next_victim_seg[2];	/* next segment in victim section */
1713325163e9SDaeho Jeong 	spinlock_t gc_urgent_high_lock;
1714325163e9SDaeho Jeong 	bool gc_urgent_high_limited;		/* indicates having limited trial count */
1715325163e9SDaeho Jeong 	unsigned int gc_urgent_high_remaining;	/* remaining trial count for GC_URGENT_HIGH */
17160e5e8111SDaeho Jeong 
17172ef79ecbSChao Yu 	/* for skip statistic */
1718677017d1SSahitya Tummala 	unsigned int atomic_files;		/* # of opened atomic file */
17196f8d4455SJaegeuk Kim 	unsigned long long skipped_gc_rwsem;		/* FG_GC only */
172039a53e0cSJaegeuk Kim 
17211ad71a27SJaegeuk Kim 	/* threshold for gc trials on pinned files */
17221ad71a27SJaegeuk Kim 	u64 gc_pin_file_threshold;
1723e4544b63STim Murray 	struct f2fs_rwsem pin_sem;
17241ad71a27SJaegeuk Kim 
1725b1c57c1cSJaegeuk Kim 	/* maximum # of trials to find a victim segment for SSR and GC */
1726b1c57c1cSJaegeuk Kim 	unsigned int max_victim_search;
1727e3080b01SChao Yu 	/* migration granularity of garbage collection, unit: segment */
1728e3080b01SChao Yu 	unsigned int migration_granularity;
1729b1c57c1cSJaegeuk Kim 
173039a53e0cSJaegeuk Kim 	/*
173139a53e0cSJaegeuk Kim 	 * for stat information.
173239a53e0cSJaegeuk Kim 	 * one is for the LFS mode, and the other is for the SSR mode.
173339a53e0cSJaegeuk Kim 	 */
173435b09d82SNamjae Jeon #ifdef CONFIG_F2FS_STAT_FS
173539a53e0cSJaegeuk Kim 	struct f2fs_stat_info *stat_info;	/* FS status information */
1736b63e7be5SChao Yu 	atomic_t meta_count[META_MAX];		/* # of meta blocks */
173739a53e0cSJaegeuk Kim 	unsigned int segment_count[2];		/* # of allocated segments */
173839a53e0cSJaegeuk Kim 	unsigned int block_count[2];		/* # of allocated blocks */
1739b9a2c252SChangman Lee 	atomic_t inplace_count;		/* # of inplace update */
17405b7ee374SChao Yu 	atomic64_t total_hit_ext;		/* # of lookup extent cache */
17415b7ee374SChao Yu 	atomic64_t read_hit_rbtree;		/* # of hit rbtree extent node */
17425b7ee374SChao Yu 	atomic64_t read_hit_largest;		/* # of hit largest extent node */
17435b7ee374SChao Yu 	atomic64_t read_hit_cached;		/* # of hit cached extent node */
1744d5e8f6c9SChao Yu 	atomic_t inline_xattr;			/* # of inline_xattr inodes */
174503e14d52SChao Yu 	atomic_t inline_inode;			/* # of inline_data inodes */
174603e14d52SChao Yu 	atomic_t inline_dir;			/* # of inline_dentry inodes */
17474c8ff709SChao Yu 	atomic_t compr_inode;			/* # of compressed inodes */
1748ae999bb9SDaeho Jeong 	atomic64_t compr_blocks;		/* # of compressed blocks */
174926a28a0cSJaegeuk Kim 	atomic_t max_aw_cnt;			/* max # of atomic writes */
1750274bd9baSChao Yu 	unsigned int io_skip_bggc;		/* skip background gc for in-flight IO */
1751274bd9baSChao Yu 	unsigned int other_skip_bggc;		/* skip background gc for other reasons */
175233fbd510SChao Yu 	unsigned int ndirty_inode[NR_INODE_TYPE];	/* # of dirty inodes */
175335b09d82SNamjae Jeon #endif
175439a53e0cSJaegeuk Kim 	spinlock_t stat_lock;			/* lock for stat operations */
1755b59d0baeSNamjae Jeon 
1756da9953b7SJaegeuk Kim 	/* to attach REQ_META|REQ_FUA flags */
1757da9953b7SJaegeuk Kim 	unsigned int data_io_flag;
175832b6aba8SJaegeuk Kim 	unsigned int node_io_flag;
1759b0af6d49SChao Yu 
1760b59d0baeSNamjae Jeon 	/* For sysfs suppport */
17615d4daa57SChao Yu 	struct kobject s_kobj;			/* /sys/fs/f2fs/<devname> */
1762b59d0baeSNamjae Jeon 	struct completion s_kobj_unregister;
17632658e50dSJaegeuk Kim 
17645d4daa57SChao Yu 	struct kobject s_stat_kobj;		/* /sys/fs/f2fs/<devname>/stat */
17655d4daa57SChao Yu 	struct completion s_stat_kobj_unregister;
17665d4daa57SChao Yu 
17674c89b53dSJaegeuk Kim 	struct kobject s_feature_list_kobj;		/* /sys/fs/f2fs/<devname>/feature_list */
17684c89b53dSJaegeuk Kim 	struct completion s_feature_list_kobj_unregister;
17694c89b53dSJaegeuk Kim 
17702658e50dSJaegeuk Kim 	/* For shrinker support */
17712658e50dSJaegeuk Kim 	struct list_head s_list;
177271f2c820SChao Yu 	struct mutex umount_mutex;
177371f2c820SChao Yu 	unsigned int shrinker_run_no;
177471f2c820SChao Yu 
177571f2c820SChao Yu 	/* For multi devices */
17763c62be17SJaegeuk Kim 	int s_ndevs;				/* number of devices */
17773c62be17SJaegeuk Kim 	struct f2fs_dev_info *devs;		/* for device list */
17781228b482SChao Yu 	unsigned int dirty_device;		/* for checkpoint data flush */
17791228b482SChao Yu 	spinlock_t dev_lock;			/* protect dirty_device */
178071f2c820SChao Yu 	bool aligned_blksize;			/* all devices has the same logical blksize */
17818f1dbbbbSShuoran Liu 
17828f1dbbbbSShuoran Liu 	/* For write statistics */
17838f1dbbbbSShuoran Liu 	u64 sectors_written_start;
17848f1dbbbbSShuoran Liu 	u64 kbytes_written;
178543b6573bSKeith Mok 
178643b6573bSKeith Mok 	/* Reference to checksum algorithm driver via cryptoapi */
178743b6573bSKeith Mok 	struct crypto_shash *s_chksum_driver;
17881ecc0c5cSChao Yu 
1789704956ecSChao Yu 	/* Precomputed FS UUID checksum for seeding other checksums */
1790704956ecSChao Yu 	__u32 s_chksum_seed;
17914c8ff709SChao Yu 
17924c8ff709SChao Yu 	struct workqueue_struct *post_read_wq;	/* post read workqueue */
1793a999150fSChao Yu 
1794a999150fSChao Yu 	struct kmem_cache *inline_xattr_slab;	/* inline xattr entry */
1795a999150fSChao Yu 	unsigned int inline_xattr_slab_size;	/* default inline xattr slab size */
179631083031SChao Yu 
179707c6b593SDaeho Jeong 	/* For reclaimed segs statistics per each GC mode */
179807c6b593SDaeho Jeong 	unsigned int gc_segment_mode;		/* GC state for reclaimed segments */
179907c6b593SDaeho Jeong 	unsigned int gc_reclaimed_segs[MAX_GC_MODE];	/* Reclaimed segs for each mode */
180007c6b593SDaeho Jeong 
18010f6b56ecSDaeho Jeong 	unsigned long seq_file_ra_mul;		/* multiplier for ra_pages of seq. files in fadvise */
18020f6b56ecSDaeho Jeong 
18036691d940SDaeho Jeong 	int max_fragment_chunk;			/* max chunk size for block fragmentation mode */
18046691d940SDaeho Jeong 	int max_fragment_hole;			/* max hole size for block fragmentation mode */
18056691d940SDaeho Jeong 
180631083031SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
180731083031SChao Yu 	struct kmem_cache *page_array_slab;	/* page array entry */
180831083031SChao Yu 	unsigned int page_array_slab_size;	/* default page array slab size */
18095ac443e2SDaeho Jeong 
18105ac443e2SDaeho Jeong 	/* For runtime compression statistics */
18115ac443e2SDaeho Jeong 	u64 compr_written_block;
18125ac443e2SDaeho Jeong 	u64 compr_saved_block;
18135ac443e2SDaeho Jeong 	u32 compr_new_inode;
18146ce19affSChao Yu 
18156ce19affSChao Yu 	/* For compressed block cache */
18166ce19affSChao Yu 	struct inode *compress_inode;		/* cache compressed blocks */
18176ce19affSChao Yu 	unsigned int compress_percent;		/* cache page percentage */
18186ce19affSChao Yu 	unsigned int compress_watermark;	/* cache page watermark */
18196ce19affSChao Yu 	atomic_t compress_page_hit;		/* cache hit count */
182031083031SChao Yu #endif
182152118743SDaeho Jeong 
182252118743SDaeho Jeong #ifdef CONFIG_F2FS_IOSTAT
182352118743SDaeho Jeong 	/* For app/fs IO statistics */
182452118743SDaeho Jeong 	spinlock_t iostat_lock;
182552118743SDaeho Jeong 	unsigned long long rw_iostat[NR_IO_TYPE];
182652118743SDaeho Jeong 	unsigned long long prev_rw_iostat[NR_IO_TYPE];
182752118743SDaeho Jeong 	bool iostat_enable;
182852118743SDaeho Jeong 	unsigned long iostat_next_period;
182952118743SDaeho Jeong 	unsigned int iostat_period_ms;
1830a4b68176SDaeho Jeong 
1831a4b68176SDaeho Jeong 	/* For io latency related statistics info in one iostat period */
1832a4b68176SDaeho Jeong 	spinlock_t iostat_lat_lock;
1833a4b68176SDaeho Jeong 	struct iostat_lat_info *iostat_io_lat;
183452118743SDaeho Jeong #endif
183539a53e0cSJaegeuk Kim };
183639a53e0cSJaegeuk Kim 
18371ecc0c5cSChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
1838c45d6002SChao Yu #define f2fs_show_injection_info(sbi, type)					\
1839c45d6002SChao Yu 	printk_ratelimited("%sF2FS-fs (%s) : inject %s in %s of %pS\n",	\
1840c45d6002SChao Yu 		KERN_INFO, sbi->sb->s_id,				\
1841c45d6002SChao Yu 		f2fs_fault_name[type],					\
184255523519SChao Yu 		__func__, __builtin_return_address(0))
18431ecc0c5cSChao Yu static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
18441ecc0c5cSChao Yu {
184563189b78SChao Yu 	struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
18461ecc0c5cSChao Yu 
18471ecc0c5cSChao Yu 	if (!ffi->inject_rate)
18481ecc0c5cSChao Yu 		return false;
18491ecc0c5cSChao Yu 
18501ecc0c5cSChao Yu 	if (!IS_FAULT_SET(ffi, type))
18511ecc0c5cSChao Yu 		return false;
18521ecc0c5cSChao Yu 
18531ecc0c5cSChao Yu 	atomic_inc(&ffi->inject_ops);
18541ecc0c5cSChao Yu 	if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
18551ecc0c5cSChao Yu 		atomic_set(&ffi->inject_ops, 0);
18561ecc0c5cSChao Yu 		return true;
18571ecc0c5cSChao Yu 	}
18581ecc0c5cSChao Yu 	return false;
18591ecc0c5cSChao Yu }
18607fa750a1SArnd Bergmann #else
1861c45d6002SChao Yu #define f2fs_show_injection_info(sbi, type) do { } while (0)
18627fa750a1SArnd Bergmann static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
18637fa750a1SArnd Bergmann {
18647fa750a1SArnd Bergmann 	return false;
18657fa750a1SArnd Bergmann }
18661ecc0c5cSChao Yu #endif
18671ecc0c5cSChao Yu 
18680916878dSDamien Le Moal /*
18690916878dSDamien Le Moal  * Test if the mounted volume is a multi-device volume.
18700916878dSDamien Le Moal  *   - For a single regular disk volume, sbi->s_ndevs is 0.
18710916878dSDamien Le Moal  *   - For a single zoned disk volume, sbi->s_ndevs is 1.
18720916878dSDamien Le Moal  *   - For a multi-device volume, sbi->s_ndevs is always 2 or more.
18730916878dSDamien Le Moal  */
18740916878dSDamien Le Moal static inline bool f2fs_is_multi_device(struct f2fs_sb_info *sbi)
18750916878dSDamien Le Moal {
18760916878dSDamien Le Moal 	return sbi->s_ndevs > 1;
18770916878dSDamien Le Moal }
18780916878dSDamien Le Moal 
18796beceb54SJaegeuk Kim static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
18806beceb54SJaegeuk Kim {
1881a7d10cf3SSahitya Tummala 	unsigned long now = jiffies;
1882a7d10cf3SSahitya Tummala 
1883a7d10cf3SSahitya Tummala 	sbi->last_time[type] = now;
1884a7d10cf3SSahitya Tummala 
1885a7d10cf3SSahitya Tummala 	/* DISCARD_TIME and GC_TIME are based on REQ_TIME */
1886a7d10cf3SSahitya Tummala 	if (type == REQ_TIME) {
1887a7d10cf3SSahitya Tummala 		sbi->last_time[DISCARD_TIME] = now;
1888a7d10cf3SSahitya Tummala 		sbi->last_time[GC_TIME] = now;
1889a7d10cf3SSahitya Tummala 	}
18906beceb54SJaegeuk Kim }
18916beceb54SJaegeuk Kim 
18926beceb54SJaegeuk Kim static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
18936beceb54SJaegeuk Kim {
18946bccfa19SArnd Bergmann 	unsigned long interval = sbi->interval_time[type] * HZ;
18956beceb54SJaegeuk Kim 
18966beceb54SJaegeuk Kim 	return time_after(jiffies, sbi->last_time[type] + interval);
18976beceb54SJaegeuk Kim }
18986beceb54SJaegeuk Kim 
1899a7d10cf3SSahitya Tummala static inline unsigned int f2fs_time_to_wait(struct f2fs_sb_info *sbi,
1900a7d10cf3SSahitya Tummala 						int type)
1901a7d10cf3SSahitya Tummala {
1902a7d10cf3SSahitya Tummala 	unsigned long interval = sbi->interval_time[type] * HZ;
1903a7d10cf3SSahitya Tummala 	unsigned int wait_ms = 0;
1904a7d10cf3SSahitya Tummala 	long delta;
1905a7d10cf3SSahitya Tummala 
1906a7d10cf3SSahitya Tummala 	delta = (sbi->last_time[type] + interval) - jiffies;
1907a7d10cf3SSahitya Tummala 	if (delta > 0)
1908a7d10cf3SSahitya Tummala 		wait_ms = jiffies_to_msecs(delta);
1909a7d10cf3SSahitya Tummala 
1910a7d10cf3SSahitya Tummala 	return wait_ms;
1911a7d10cf3SSahitya Tummala }
1912a7d10cf3SSahitya Tummala 
191339a53e0cSJaegeuk Kim /*
191439a53e0cSJaegeuk Kim  * Inline functions
191539a53e0cSJaegeuk Kim  */
1916416d2dbbSChao Yu static inline u32 __f2fs_crc32(struct f2fs_sb_info *sbi, u32 crc,
1917704956ecSChao Yu 			      const void *address, unsigned int length)
1918704956ecSChao Yu {
1919704956ecSChao Yu 	struct {
1920704956ecSChao Yu 		struct shash_desc shash;
1921704956ecSChao Yu 		char ctx[4];
1922704956ecSChao Yu 	} desc;
1923704956ecSChao Yu 	int err;
1924704956ecSChao Yu 
1925704956ecSChao Yu 	BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver) != sizeof(desc.ctx));
1926704956ecSChao Yu 
1927704956ecSChao Yu 	desc.shash.tfm = sbi->s_chksum_driver;
1928704956ecSChao Yu 	*(u32 *)desc.ctx = crc;
1929704956ecSChao Yu 
1930704956ecSChao Yu 	err = crypto_shash_update(&desc.shash, address, length);
1931704956ecSChao Yu 	BUG_ON(err);
1932704956ecSChao Yu 
1933704956ecSChao Yu 	return *(u32 *)desc.ctx;
1934704956ecSChao Yu }
1935704956ecSChao Yu 
1936416d2dbbSChao Yu static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
1937416d2dbbSChao Yu 			   unsigned int length)
1938416d2dbbSChao Yu {
1939416d2dbbSChao Yu 	return __f2fs_crc32(sbi, F2FS_SUPER_MAGIC, address, length);
1940416d2dbbSChao Yu }
1941416d2dbbSChao Yu 
1942416d2dbbSChao Yu static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
1943416d2dbbSChao Yu 				  void *buf, size_t buf_size)
1944416d2dbbSChao Yu {
1945416d2dbbSChao Yu 	return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
1946416d2dbbSChao Yu }
1947416d2dbbSChao Yu 
1948416d2dbbSChao Yu static inline u32 f2fs_chksum(struct f2fs_sb_info *sbi, u32 crc,
1949416d2dbbSChao Yu 			      const void *address, unsigned int length)
1950416d2dbbSChao Yu {
1951416d2dbbSChao Yu 	return __f2fs_crc32(sbi, crc, address, length);
1952416d2dbbSChao Yu }
1953416d2dbbSChao Yu 
195439a53e0cSJaegeuk Kim static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
195539a53e0cSJaegeuk Kim {
195639a53e0cSJaegeuk Kim 	return container_of(inode, struct f2fs_inode_info, vfs_inode);
195739a53e0cSJaegeuk Kim }
195839a53e0cSJaegeuk Kim 
195939a53e0cSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
196039a53e0cSJaegeuk Kim {
196139a53e0cSJaegeuk Kim 	return sb->s_fs_info;
196239a53e0cSJaegeuk Kim }
196339a53e0cSJaegeuk Kim 
19644081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
19654081363fSJaegeuk Kim {
19664081363fSJaegeuk Kim 	return F2FS_SB(inode->i_sb);
19674081363fSJaegeuk Kim }
19684081363fSJaegeuk Kim 
19694081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
19704081363fSJaegeuk Kim {
19714081363fSJaegeuk Kim 	return F2FS_I_SB(mapping->host);
19724081363fSJaegeuk Kim }
19734081363fSJaegeuk Kim 
19744081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
19754081363fSJaegeuk Kim {
19764969c06aSJaegeuk Kim 	return F2FS_M_SB(page_file_mapping(page));
19774081363fSJaegeuk Kim }
19784081363fSJaegeuk Kim 
197939a53e0cSJaegeuk Kim static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
198039a53e0cSJaegeuk Kim {
198139a53e0cSJaegeuk Kim 	return (struct f2fs_super_block *)(sbi->raw_super);
198239a53e0cSJaegeuk Kim }
198339a53e0cSJaegeuk Kim 
198439a53e0cSJaegeuk Kim static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
198539a53e0cSJaegeuk Kim {
198639a53e0cSJaegeuk Kim 	return (struct f2fs_checkpoint *)(sbi->ckpt);
198739a53e0cSJaegeuk Kim }
198839a53e0cSJaegeuk Kim 
198945590710SGu Zheng static inline struct f2fs_node *F2FS_NODE(struct page *page)
199045590710SGu Zheng {
199145590710SGu Zheng 	return (struct f2fs_node *)page_address(page);
199245590710SGu Zheng }
199345590710SGu Zheng 
199458bfaf44SJaegeuk Kim static inline struct f2fs_inode *F2FS_INODE(struct page *page)
199558bfaf44SJaegeuk Kim {
199658bfaf44SJaegeuk Kim 	return &((struct f2fs_node *)page_address(page))->i;
199758bfaf44SJaegeuk Kim }
199858bfaf44SJaegeuk Kim 
199939a53e0cSJaegeuk Kim static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
200039a53e0cSJaegeuk Kim {
200139a53e0cSJaegeuk Kim 	return (struct f2fs_nm_info *)(sbi->nm_info);
200239a53e0cSJaegeuk Kim }
200339a53e0cSJaegeuk Kim 
200439a53e0cSJaegeuk Kim static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
200539a53e0cSJaegeuk Kim {
200639a53e0cSJaegeuk Kim 	return (struct f2fs_sm_info *)(sbi->sm_info);
200739a53e0cSJaegeuk Kim }
200839a53e0cSJaegeuk Kim 
200939a53e0cSJaegeuk Kim static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
201039a53e0cSJaegeuk Kim {
201139a53e0cSJaegeuk Kim 	return (struct sit_info *)(SM_I(sbi)->sit_info);
201239a53e0cSJaegeuk Kim }
201339a53e0cSJaegeuk Kim 
201439a53e0cSJaegeuk Kim static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
201539a53e0cSJaegeuk Kim {
201639a53e0cSJaegeuk Kim 	return (struct free_segmap_info *)(SM_I(sbi)->free_info);
201739a53e0cSJaegeuk Kim }
201839a53e0cSJaegeuk Kim 
201939a53e0cSJaegeuk Kim static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
202039a53e0cSJaegeuk Kim {
202139a53e0cSJaegeuk Kim 	return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
202239a53e0cSJaegeuk Kim }
202339a53e0cSJaegeuk Kim 
20249df27d98SGu Zheng static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
20259df27d98SGu Zheng {
20269df27d98SGu Zheng 	return sbi->meta_inode->i_mapping;
20279df27d98SGu Zheng }
20289df27d98SGu Zheng 
20294ef51a8fSJaegeuk Kim static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
20304ef51a8fSJaegeuk Kim {
20314ef51a8fSJaegeuk Kim 	return sbi->node_inode->i_mapping;
20324ef51a8fSJaegeuk Kim }
20334ef51a8fSJaegeuk Kim 
2034caf0047eSChao Yu static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
203539a53e0cSJaegeuk Kim {
2036fadb2fb8SChao Yu 	return test_bit(type, &sbi->s_flag);
203739a53e0cSJaegeuk Kim }
203839a53e0cSJaegeuk Kim 
2039caf0047eSChao Yu static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
204039a53e0cSJaegeuk Kim {
2041fadb2fb8SChao Yu 	set_bit(type, &sbi->s_flag);
2042caf0047eSChao Yu }
2043caf0047eSChao Yu 
2044caf0047eSChao Yu static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
2045caf0047eSChao Yu {
2046fadb2fb8SChao Yu 	clear_bit(type, &sbi->s_flag);
204739a53e0cSJaegeuk Kim }
204839a53e0cSJaegeuk Kim 
2049d71b5564SJaegeuk Kim static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
2050d71b5564SJaegeuk Kim {
2051d71b5564SJaegeuk Kim 	return le64_to_cpu(cp->checkpoint_ver);
2052d71b5564SJaegeuk Kim }
2053d71b5564SJaegeuk Kim 
2054ea676733SJaegeuk Kim static inline unsigned long f2fs_qf_ino(struct super_block *sb, int type)
2055ea676733SJaegeuk Kim {
2056ea676733SJaegeuk Kim 	if (type < F2FS_MAX_QUOTAS)
2057ea676733SJaegeuk Kim 		return le32_to_cpu(F2FS_SB(sb)->raw_super->qf_ino[type]);
2058ea676733SJaegeuk Kim 	return 0;
2059ea676733SJaegeuk Kim }
2060ea676733SJaegeuk Kim 
2061ced2c7eaSKinglong Mee static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
2062ced2c7eaSKinglong Mee {
2063ced2c7eaSKinglong Mee 	size_t crc_offset = le32_to_cpu(cp->checksum_offset);
2064ced2c7eaSKinglong Mee 	return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
2065ced2c7eaSKinglong Mee }
2066ced2c7eaSKinglong Mee 
2067aaec2b1dSChao Yu static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
206825ca923bSJaegeuk Kim {
206925ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
2070aaec2b1dSChao Yu 
207125ca923bSJaegeuk Kim 	return ckpt_flags & f;
207225ca923bSJaegeuk Kim }
207325ca923bSJaegeuk Kim 
2074aaec2b1dSChao Yu static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
207525ca923bSJaegeuk Kim {
2076aaec2b1dSChao Yu 	return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
2077aaec2b1dSChao Yu }
2078aaec2b1dSChao Yu 
2079aaec2b1dSChao Yu static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
2080aaec2b1dSChao Yu {
2081aaec2b1dSChao Yu 	unsigned int ckpt_flags;
2082aaec2b1dSChao Yu 
2083aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
208425ca923bSJaegeuk Kim 	ckpt_flags |= f;
208525ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
208625ca923bSJaegeuk Kim }
208725ca923bSJaegeuk Kim 
2088aaec2b1dSChao Yu static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
208925ca923bSJaegeuk Kim {
2090d1aa2453SChao Yu 	unsigned long flags;
2091d1aa2453SChao Yu 
2092d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
2093aaec2b1dSChao Yu 	__set_ckpt_flags(F2FS_CKPT(sbi), f);
2094d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
2095aaec2b1dSChao Yu }
2096aaec2b1dSChao Yu 
2097aaec2b1dSChao Yu static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
2098aaec2b1dSChao Yu {
2099aaec2b1dSChao Yu 	unsigned int ckpt_flags;
2100aaec2b1dSChao Yu 
2101aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
210225ca923bSJaegeuk Kim 	ckpt_flags &= (~f);
210325ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
210425ca923bSJaegeuk Kim }
210525ca923bSJaegeuk Kim 
2106aaec2b1dSChao Yu static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
2107aaec2b1dSChao Yu {
2108d1aa2453SChao Yu 	unsigned long flags;
2109d1aa2453SChao Yu 
2110d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
2111aaec2b1dSChao Yu 	__clear_ckpt_flags(F2FS_CKPT(sbi), f);
2112d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
2113aaec2b1dSChao Yu }
2114aaec2b1dSChao Yu 
2115c7f91bd4SBart Van Assche #define init_f2fs_rwsem(sem)					\
2116c7f91bd4SBart Van Assche do {								\
2117c7f91bd4SBart Van Assche 	static struct lock_class_key __key;			\
2118c7f91bd4SBart Van Assche 								\
2119c7f91bd4SBart Van Assche 	__init_f2fs_rwsem((sem), #sem, &__key);			\
2120c7f91bd4SBart Van Assche } while (0)
2121c7f91bd4SBart Van Assche 
2122c7f91bd4SBart Van Assche static inline void __init_f2fs_rwsem(struct f2fs_rwsem *sem,
2123c7f91bd4SBart Van Assche 		const char *sem_name, struct lock_class_key *key)
2124e4544b63STim Murray {
2125c7f91bd4SBart Van Assche 	__init_rwsem(&sem->internal_rwsem, sem_name, key);
21267f8e249dSJaegeuk Kim #ifdef CONFIG_F2FS_UNFAIR_RWSEM
2127e4544b63STim Murray 	init_waitqueue_head(&sem->read_waiters);
21287f8e249dSJaegeuk Kim #endif
2129e4544b63STim Murray }
2130e4544b63STim Murray 
2131e4544b63STim Murray static inline int f2fs_rwsem_is_locked(struct f2fs_rwsem *sem)
2132e4544b63STim Murray {
2133e4544b63STim Murray 	return rwsem_is_locked(&sem->internal_rwsem);
2134e4544b63STim Murray }
2135e4544b63STim Murray 
2136e4544b63STim Murray static inline int f2fs_rwsem_is_contended(struct f2fs_rwsem *sem)
2137e4544b63STim Murray {
2138e4544b63STim Murray 	return rwsem_is_contended(&sem->internal_rwsem);
2139e4544b63STim Murray }
2140e4544b63STim Murray 
2141e4544b63STim Murray static inline void f2fs_down_read(struct f2fs_rwsem *sem)
2142e4544b63STim Murray {
21437f8e249dSJaegeuk Kim #ifdef CONFIG_F2FS_UNFAIR_RWSEM
2144e4544b63STim Murray 	wait_event(sem->read_waiters, down_read_trylock(&sem->internal_rwsem));
21457f8e249dSJaegeuk Kim #else
21467f8e249dSJaegeuk Kim 	down_read(&sem->internal_rwsem);
21477f8e249dSJaegeuk Kim #endif
2148e4544b63STim Murray }
2149e4544b63STim Murray 
2150e4544b63STim Murray static inline int f2fs_down_read_trylock(struct f2fs_rwsem *sem)
2151e4544b63STim Murray {
2152e4544b63STim Murray 	return down_read_trylock(&sem->internal_rwsem);
2153e4544b63STim Murray }
2154e4544b63STim Murray 
2155e4544b63STim Murray #ifdef CONFIG_DEBUG_LOCK_ALLOC
2156e4544b63STim Murray static inline void f2fs_down_read_nested(struct f2fs_rwsem *sem, int subclass)
2157e4544b63STim Murray {
2158e4544b63STim Murray 	down_read_nested(&sem->internal_rwsem, subclass);
2159e4544b63STim Murray }
2160e4544b63STim Murray #else
2161e4544b63STim Murray #define f2fs_down_read_nested(sem, subclass) f2fs_down_read(sem)
2162e4544b63STim Murray #endif
2163e4544b63STim Murray 
2164e4544b63STim Murray static inline void f2fs_up_read(struct f2fs_rwsem *sem)
2165e4544b63STim Murray {
2166e4544b63STim Murray 	up_read(&sem->internal_rwsem);
2167e4544b63STim Murray }
2168e4544b63STim Murray 
2169e4544b63STim Murray static inline void f2fs_down_write(struct f2fs_rwsem *sem)
2170e4544b63STim Murray {
2171e4544b63STim Murray 	down_write(&sem->internal_rwsem);
2172e4544b63STim Murray }
2173e4544b63STim Murray 
2174e4544b63STim Murray static inline int f2fs_down_write_trylock(struct f2fs_rwsem *sem)
2175e4544b63STim Murray {
2176e4544b63STim Murray 	return down_write_trylock(&sem->internal_rwsem);
2177e4544b63STim Murray }
2178e4544b63STim Murray 
2179e4544b63STim Murray static inline void f2fs_up_write(struct f2fs_rwsem *sem)
2180e4544b63STim Murray {
2181e4544b63STim Murray 	up_write(&sem->internal_rwsem);
21827f8e249dSJaegeuk Kim #ifdef CONFIG_F2FS_UNFAIR_RWSEM
2183e4544b63STim Murray 	wake_up_all(&sem->read_waiters);
21847f8e249dSJaegeuk Kim #endif
2185e4544b63STim Murray }
2186e4544b63STim Murray 
2187e479556bSGu Zheng static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
218839a53e0cSJaegeuk Kim {
2189e4544b63STim Murray 	f2fs_down_read(&sbi->cp_rwsem);
219039a53e0cSJaegeuk Kim }
219139a53e0cSJaegeuk Kim 
2192cc15620bSJaegeuk Kim static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi)
2193cc15620bSJaegeuk Kim {
21943e020389SChao Yu 	if (time_to_inject(sbi, FAULT_LOCK_OP)) {
21953e020389SChao Yu 		f2fs_show_injection_info(sbi, FAULT_LOCK_OP);
21963e020389SChao Yu 		return 0;
21973e020389SChao Yu 	}
2198e4544b63STim Murray 	return f2fs_down_read_trylock(&sbi->cp_rwsem);
2199cc15620bSJaegeuk Kim }
2200cc15620bSJaegeuk Kim 
2201e479556bSGu Zheng static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
220239a53e0cSJaegeuk Kim {
2203e4544b63STim Murray 	f2fs_up_read(&sbi->cp_rwsem);
220439936837SJaegeuk Kim }
220539936837SJaegeuk Kim 
2206e479556bSGu Zheng static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
220739936837SJaegeuk Kim {
2208e4544b63STim Murray 	f2fs_down_write(&sbi->cp_rwsem);
220939936837SJaegeuk Kim }
221039936837SJaegeuk Kim 
2211e479556bSGu Zheng static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
221239936837SJaegeuk Kim {
2213e4544b63STim Murray 	f2fs_up_write(&sbi->cp_rwsem);
221439a53e0cSJaegeuk Kim }
221539a53e0cSJaegeuk Kim 
2216119ee914SJaegeuk Kim static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
2217119ee914SJaegeuk Kim {
2218119ee914SJaegeuk Kim 	int reason = CP_SYNC;
2219119ee914SJaegeuk Kim 
2220119ee914SJaegeuk Kim 	if (test_opt(sbi, FASTBOOT))
2221119ee914SJaegeuk Kim 		reason = CP_FASTBOOT;
2222119ee914SJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
2223119ee914SJaegeuk Kim 		reason = CP_UMOUNT;
2224119ee914SJaegeuk Kim 	return reason;
2225119ee914SJaegeuk Kim }
2226119ee914SJaegeuk Kim 
2227119ee914SJaegeuk Kim static inline bool __remain_node_summaries(int reason)
2228119ee914SJaegeuk Kim {
2229c473f1a9SChao Yu 	return (reason & (CP_UMOUNT | CP_FASTBOOT));
2230119ee914SJaegeuk Kim }
2231119ee914SJaegeuk Kim 
2232119ee914SJaegeuk Kim static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
2233119ee914SJaegeuk Kim {
2234aaec2b1dSChao Yu 	return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
2235aaec2b1dSChao Yu 			is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
2236119ee914SJaegeuk Kim }
2237119ee914SJaegeuk Kim 
223839a53e0cSJaegeuk Kim /*
223939a53e0cSJaegeuk Kim  * Check whether the inode has blocks or not
224039a53e0cSJaegeuk Kim  */
224139a53e0cSJaegeuk Kim static inline int F2FS_HAS_BLOCKS(struct inode *inode)
224239a53e0cSJaegeuk Kim {
22430eb0adadSChao Yu 	block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
22440eb0adadSChao Yu 
2245000519f2SChao Yu 	return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block;
224639a53e0cSJaegeuk Kim }
224739a53e0cSJaegeuk Kim 
22484bc8e9bcSChao Yu static inline bool f2fs_has_xattr_block(unsigned int ofs)
22494bc8e9bcSChao Yu {
22504bc8e9bcSChao Yu 	return ofs == XATTR_NODE_OFFSET;
22514bc8e9bcSChao Yu }
22524bc8e9bcSChao Yu 
2253d8a9a229SJaegeuk Kim static inline bool __allow_reserved_blocks(struct f2fs_sb_info *sbi,
2254a90a0884SJaegeuk Kim 					struct inode *inode, bool cap)
22557c2e5963SJaegeuk Kim {
2256d8a9a229SJaegeuk Kim 	if (!inode)
2257d8a9a229SJaegeuk Kim 		return true;
22587c2e5963SJaegeuk Kim 	if (!test_opt(sbi, RESERVE_ROOT))
22597c2e5963SJaegeuk Kim 		return false;
2260d8a9a229SJaegeuk Kim 	if (IS_NOQUOTA(inode))
2261d8a9a229SJaegeuk Kim 		return true;
226263189b78SChao Yu 	if (uid_eq(F2FS_OPTION(sbi).s_resuid, current_fsuid()))
22637c2e5963SJaegeuk Kim 		return true;
226463189b78SChao Yu 	if (!gid_eq(F2FS_OPTION(sbi).s_resgid, GLOBAL_ROOT_GID) &&
226563189b78SChao Yu 					in_group_p(F2FS_OPTION(sbi).s_resgid))
22667c2e5963SJaegeuk Kim 		return true;
2267a90a0884SJaegeuk Kim 	if (cap && capable(CAP_SYS_RESOURCE))
2268162b27aeSJaegeuk Kim 		return true;
22697c2e5963SJaegeuk Kim 	return false;
22707c2e5963SJaegeuk Kim }
22717c2e5963SJaegeuk Kim 
22720abd675eSChao Yu static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool);
22730abd675eSChao Yu static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
227446008c6dSChao Yu 				 struct inode *inode, blkcnt_t *count)
227539a53e0cSJaegeuk Kim {
22760abd675eSChao Yu 	blkcnt_t diff = 0, release = 0;
2277daeb433eSChao Yu 	block_t avail_user_block_count;
22780abd675eSChao Yu 	int ret;
22790abd675eSChao Yu 
22800abd675eSChao Yu 	ret = dquot_reserve_block(inode, *count);
22810abd675eSChao Yu 	if (ret)
22820abd675eSChao Yu 		return ret;
2283dd11a5dfSJaegeuk Kim 
228455523519SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
2285c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_BLOCK);
22860abd675eSChao Yu 		release = *count;
22879ea2f0beSChao Yu 		goto release_quota;
228855523519SChao Yu 	}
22897fa750a1SArnd Bergmann 
2290dd11a5dfSJaegeuk Kim 	/*
2291dd11a5dfSJaegeuk Kim 	 * let's increase this in prior to actual block count change in order
2292dd11a5dfSJaegeuk Kim 	 * for f2fs_sync_file to avoid data races when deciding checkpoint.
2293dd11a5dfSJaegeuk Kim 	 */
2294dd11a5dfSJaegeuk Kim 	percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
229539a53e0cSJaegeuk Kim 
229639a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
22972555a2d5SJaegeuk Kim 	sbi->total_valid_block_count += (block_t)(*count);
229880d42145SYunlong Song 	avail_user_block_count = sbi->user_block_count -
229980d42145SYunlong Song 					sbi->current_reserved_blocks;
23007e65be49SJaegeuk Kim 
2301a90a0884SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode, true))
230263189b78SChao Yu 		avail_user_block_count -= F2FS_OPTION(sbi).root_reserved_blocks;
2303300a8429SChao Yu 
2304300a8429SChao Yu 	if (F2FS_IO_ALIGNED(sbi))
2305300a8429SChao Yu 		avail_user_block_count -= sbi->blocks_per_seg *
2306300a8429SChao Yu 				SM_I(sbi)->additional_reserved_segments;
2307300a8429SChao Yu 
2308a4c3ecaaSDaniel Rosenberg 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2309a4c3ecaaSDaniel Rosenberg 		if (avail_user_block_count > sbi->unusable_block_count)
23104354994fSDaniel Rosenberg 			avail_user_block_count -= sbi->unusable_block_count;
2311a4c3ecaaSDaniel Rosenberg 		else
2312a4c3ecaaSDaniel Rosenberg 			avail_user_block_count = 0;
2313a4c3ecaaSDaniel Rosenberg 	}
2314daeb433eSChao Yu 	if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
2315daeb433eSChao Yu 		diff = sbi->total_valid_block_count - avail_user_block_count;
23167e65be49SJaegeuk Kim 		if (diff > *count)
23177e65be49SJaegeuk Kim 			diff = *count;
2318dd11a5dfSJaegeuk Kim 		*count -= diff;
23190abd675eSChao Yu 		release = diff;
23207e65be49SJaegeuk Kim 		sbi->total_valid_block_count -= diff;
232146008c6dSChao Yu 		if (!*count) {
232239a53e0cSJaegeuk Kim 			spin_unlock(&sbi->stat_lock);
23230abd675eSChao Yu 			goto enospc;
232439a53e0cSJaegeuk Kim 		}
232546008c6dSChao Yu 	}
232639a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
232741382ec4SJaegeuk Kim 
232836b877afSDaniel Rosenberg 	if (unlikely(release)) {
232936b877afSDaniel Rosenberg 		percpu_counter_sub(&sbi->alloc_valid_block_count, release);
23300abd675eSChao Yu 		dquot_release_reservation_block(inode, release);
233136b877afSDaniel Rosenberg 	}
23320abd675eSChao Yu 	f2fs_i_blocks_write(inode, *count, true, true);
23330abd675eSChao Yu 	return 0;
23340abd675eSChao Yu 
23350abd675eSChao Yu enospc:
233636b877afSDaniel Rosenberg 	percpu_counter_sub(&sbi->alloc_valid_block_count, release);
23379ea2f0beSChao Yu release_quota:
23380abd675eSChao Yu 	dquot_release_reservation_block(inode, release);
23390abd675eSChao Yu 	return -ENOSPC;
234039a53e0cSJaegeuk Kim }
234139a53e0cSJaegeuk Kim 
2342dcbb4c10SJoe Perches __printf(2, 3)
2343dcbb4c10SJoe Perches void f2fs_printk(struct f2fs_sb_info *sbi, const char *fmt, ...);
2344dcbb4c10SJoe Perches 
2345dcbb4c10SJoe Perches #define f2fs_err(sbi, fmt, ...)						\
2346dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_ERR fmt, ##__VA_ARGS__)
2347dcbb4c10SJoe Perches #define f2fs_warn(sbi, fmt, ...)					\
2348dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_WARNING fmt, ##__VA_ARGS__)
2349dcbb4c10SJoe Perches #define f2fs_notice(sbi, fmt, ...)					\
2350dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_NOTICE fmt, ##__VA_ARGS__)
2351dcbb4c10SJoe Perches #define f2fs_info(sbi, fmt, ...)					\
2352dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_INFO fmt, ##__VA_ARGS__)
2353dcbb4c10SJoe Perches #define f2fs_debug(sbi, fmt, ...)					\
2354dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_DEBUG fmt, ##__VA_ARGS__)
2355dcbb4c10SJoe Perches 
2356da19b0dcSGu Zheng static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
235739a53e0cSJaegeuk Kim 						struct inode *inode,
23580eb0adadSChao Yu 						block_t count)
235939a53e0cSJaegeuk Kim {
23600eb0adadSChao Yu 	blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK;
23610eb0adadSChao Yu 
236239a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
23639850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
236439a53e0cSJaegeuk Kim 	sbi->total_valid_block_count -= (block_t)count;
236580d42145SYunlong Song 	if (sbi->reserved_blocks &&
236680d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
236780d42145SYunlong Song 		sbi->current_reserved_blocks = min(sbi->reserved_blocks,
236880d42145SYunlong Song 					sbi->current_reserved_blocks + count);
236939a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
23705e159cd3SChao Yu 	if (unlikely(inode->i_blocks < sectors)) {
2371dcbb4c10SJoe Perches 		f2fs_warn(sbi, "Inconsistent i_blocks, ino:%lu, iblocks:%llu, sectors:%llu",
23725e159cd3SChao Yu 			  inode->i_ino,
23735e159cd3SChao Yu 			  (unsigned long long)inode->i_blocks,
23745e159cd3SChao Yu 			  (unsigned long long)sectors);
23755e159cd3SChao Yu 		set_sbi_flag(sbi, SBI_NEED_FSCK);
23765e159cd3SChao Yu 		return;
23775e159cd3SChao Yu 	}
23780abd675eSChao Yu 	f2fs_i_blocks_write(inode, count, false, true);
237939a53e0cSJaegeuk Kim }
238039a53e0cSJaegeuk Kim 
238139a53e0cSJaegeuk Kim static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
238239a53e0cSJaegeuk Kim {
238335782b23SJaegeuk Kim 	atomic_inc(&sbi->nr_pages[count_type]);
23847c4abcbeSChao Yu 
238520109873SChao Yu 	if (count_type == F2FS_DIRTY_DENTS ||
238620109873SChao Yu 			count_type == F2FS_DIRTY_NODES ||
238720109873SChao Yu 			count_type == F2FS_DIRTY_META ||
238820109873SChao Yu 			count_type == F2FS_DIRTY_QDATA ||
238920109873SChao Yu 			count_type == F2FS_DIRTY_IMETA)
2390caf0047eSChao Yu 		set_sbi_flag(sbi, SBI_IS_DIRTY);
239139a53e0cSJaegeuk Kim }
239239a53e0cSJaegeuk Kim 
2393a7ffdbe2SJaegeuk Kim static inline void inode_inc_dirty_pages(struct inode *inode)
239439a53e0cSJaegeuk Kim {
2395204706c7SJaegeuk Kim 	atomic_inc(&F2FS_I(inode)->dirty_pages);
2396c227f912SChao Yu 	inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
2397c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
23982c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
23992c8a4a28SJaegeuk Kim 		inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
240039a53e0cSJaegeuk Kim }
240139a53e0cSJaegeuk Kim 
240239a53e0cSJaegeuk Kim static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
240339a53e0cSJaegeuk Kim {
240435782b23SJaegeuk Kim 	atomic_dec(&sbi->nr_pages[count_type]);
240539a53e0cSJaegeuk Kim }
240639a53e0cSJaegeuk Kim 
2407a7ffdbe2SJaegeuk Kim static inline void inode_dec_dirty_pages(struct inode *inode)
240839a53e0cSJaegeuk Kim {
24095ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
24105ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
24111fe54f9dSJaegeuk Kim 		return;
24121fe54f9dSJaegeuk Kim 
2413204706c7SJaegeuk Kim 	atomic_dec(&F2FS_I(inode)->dirty_pages);
2414c227f912SChao Yu 	dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
2415c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
24162c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
24172c8a4a28SJaegeuk Kim 		dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
241839a53e0cSJaegeuk Kim }
241939a53e0cSJaegeuk Kim 
2420523be8a6SJaegeuk Kim static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
242139a53e0cSJaegeuk Kim {
242235782b23SJaegeuk Kim 	return atomic_read(&sbi->nr_pages[count_type]);
242339a53e0cSJaegeuk Kim }
242439a53e0cSJaegeuk Kim 
2425204706c7SJaegeuk Kim static inline int get_dirty_pages(struct inode *inode)
2426f8b2c1f9SJaegeuk Kim {
2427204706c7SJaegeuk Kim 	return atomic_read(&F2FS_I(inode)->dirty_pages);
2428f8b2c1f9SJaegeuk Kim }
2429f8b2c1f9SJaegeuk Kim 
24305ac206cfSNamjae Jeon static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
24315ac206cfSNamjae Jeon {
24323519e3f9SChao Yu 	unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
2433523be8a6SJaegeuk Kim 	unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
2434523be8a6SJaegeuk Kim 						sbi->log_blocks_per_seg;
2435523be8a6SJaegeuk Kim 
2436523be8a6SJaegeuk Kim 	return segs / sbi->segs_per_sec;
24375ac206cfSNamjae Jeon }
24385ac206cfSNamjae Jeon 
243939a53e0cSJaegeuk Kim static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
244039a53e0cSJaegeuk Kim {
24418b8343faSJaegeuk Kim 	return sbi->total_valid_block_count;
244239a53e0cSJaegeuk Kim }
244339a53e0cSJaegeuk Kim 
2444f83a2584SYunlei He static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
2445f83a2584SYunlei He {
2446f83a2584SYunlei He 	return sbi->discard_blks;
2447f83a2584SYunlei He }
2448f83a2584SYunlei He 
244939a53e0cSJaegeuk Kim static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
245039a53e0cSJaegeuk Kim {
245139a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
245239a53e0cSJaegeuk Kim 
245339a53e0cSJaegeuk Kim 	/* return NAT or SIT bitmap */
245439a53e0cSJaegeuk Kim 	if (flag == NAT_BITMAP)
245539a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
245639a53e0cSJaegeuk Kim 	else if (flag == SIT_BITMAP)
245739a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
245839a53e0cSJaegeuk Kim 
245939a53e0cSJaegeuk Kim 	return 0;
246039a53e0cSJaegeuk Kim }
246139a53e0cSJaegeuk Kim 
246255141486SWanpeng Li static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
246355141486SWanpeng Li {
246455141486SWanpeng Li 	return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
246555141486SWanpeng Li }
246655141486SWanpeng Li 
246739a53e0cSJaegeuk Kim static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
246839a53e0cSJaegeuk Kim {
246939a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
24704260c406SGustavo A. R. Silva 	void *tmp_ptr = &ckpt->sit_nat_version_bitmap;
24711dbe4152SChangman Lee 	int offset;
24721dbe4152SChangman Lee 
2473199bc3feSChao Yu 	if (is_set_ckpt_flags(sbi, CP_LARGE_NAT_BITMAP_FLAG)) {
2474199bc3feSChao Yu 		offset = (flag == SIT_BITMAP) ?
2475199bc3feSChao Yu 			le32_to_cpu(ckpt->nat_ver_bitmap_bytesize) : 0;
2476b471eb99SChao Yu 		/*
2477b471eb99SChao Yu 		 * if large_nat_bitmap feature is enabled, leave checksum
2478b471eb99SChao Yu 		 * protection for all nat/sit bitmaps.
2479b471eb99SChao Yu 		 */
24804260c406SGustavo A. R. Silva 		return tmp_ptr + offset + sizeof(__le32);
2481199bc3feSChao Yu 	}
2482199bc3feSChao Yu 
248355141486SWanpeng Li 	if (__cp_payload(sbi) > 0) {
24841dbe4152SChangman Lee 		if (flag == NAT_BITMAP)
24851dbe4152SChangman Lee 			return &ckpt->sit_nat_version_bitmap;
24861dbe4152SChangman Lee 		else
248765b85cccSJaegeuk Kim 			return (unsigned char *)ckpt + F2FS_BLKSIZE;
24881dbe4152SChangman Lee 	} else {
24891dbe4152SChangman Lee 		offset = (flag == NAT_BITMAP) ?
249025ca923bSJaegeuk Kim 			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
24914260c406SGustavo A. R. Silva 		return tmp_ptr + offset;
249239a53e0cSJaegeuk Kim 	}
24931dbe4152SChangman Lee }
249439a53e0cSJaegeuk Kim 
249539a53e0cSJaegeuk Kim static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
249639a53e0cSJaegeuk Kim {
24978508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
249839a53e0cSJaegeuk Kim 
24998508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 2)
250039a53e0cSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
250139a53e0cSJaegeuk Kim 	return start_addr;
250239a53e0cSJaegeuk Kim }
250339a53e0cSJaegeuk Kim 
25048508e44aSJaegeuk Kim static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
25058508e44aSJaegeuk Kim {
25068508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
25078508e44aSJaegeuk Kim 
25088508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 1)
25098508e44aSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
25108508e44aSJaegeuk Kim 	return start_addr;
25118508e44aSJaegeuk Kim }
25128508e44aSJaegeuk Kim 
25138508e44aSJaegeuk Kim static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
25148508e44aSJaegeuk Kim {
25158508e44aSJaegeuk Kim 	sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
25168508e44aSJaegeuk Kim }
25178508e44aSJaegeuk Kim 
251839a53e0cSJaegeuk Kim static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
251939a53e0cSJaegeuk Kim {
252039a53e0cSJaegeuk Kim 	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
252139a53e0cSJaegeuk Kim }
252239a53e0cSJaegeuk Kim 
25230abd675eSChao Yu static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
2524000519f2SChao Yu 					struct inode *inode, bool is_inode)
252539a53e0cSJaegeuk Kim {
252639a53e0cSJaegeuk Kim 	block_t	valid_block_count;
2527a4c3ecaaSDaniel Rosenberg 	unsigned int valid_node_count, user_block_count;
2528af033b2aSChao Yu 	int err;
25290abd675eSChao Yu 
2530af033b2aSChao Yu 	if (is_inode) {
2531af033b2aSChao Yu 		if (inode) {
2532af033b2aSChao Yu 			err = dquot_alloc_inode(inode);
2533af033b2aSChao Yu 			if (err)
2534af033b2aSChao Yu 				return err;
2535af033b2aSChao Yu 		}
2536af033b2aSChao Yu 	} else {
2537af033b2aSChao Yu 		err = dquot_reserve_block(inode, 1);
2538af033b2aSChao Yu 		if (err)
2539af033b2aSChao Yu 			return err;
25400abd675eSChao Yu 	}
254139a53e0cSJaegeuk Kim 
2542812c6056SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
2543c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_BLOCK);
2544812c6056SChao Yu 		goto enospc;
2545812c6056SChao Yu 	}
2546812c6056SChao Yu 
254739a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
254839a53e0cSJaegeuk Kim 
25497e65be49SJaegeuk Kim 	valid_block_count = sbi->total_valid_block_count +
25507e65be49SJaegeuk Kim 					sbi->current_reserved_blocks + 1;
25517e65be49SJaegeuk Kim 
2552a90a0884SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode, false))
255363189b78SChao Yu 		valid_block_count += F2FS_OPTION(sbi).root_reserved_blocks;
2554300a8429SChao Yu 
2555300a8429SChao Yu 	if (F2FS_IO_ALIGNED(sbi))
2556300a8429SChao Yu 		valid_block_count += sbi->blocks_per_seg *
2557300a8429SChao Yu 				SM_I(sbi)->additional_reserved_segments;
2558300a8429SChao Yu 
2559a4c3ecaaSDaniel Rosenberg 	user_block_count = sbi->user_block_count;
25604354994fSDaniel Rosenberg 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2561a4c3ecaaSDaniel Rosenberg 		user_block_count -= sbi->unusable_block_count;
25627e65be49SJaegeuk Kim 
2563a4c3ecaaSDaniel Rosenberg 	if (unlikely(valid_block_count > user_block_count)) {
256439a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
25650abd675eSChao Yu 		goto enospc;
256639a53e0cSJaegeuk Kim 	}
256739a53e0cSJaegeuk Kim 
2568ef86d709SGu Zheng 	valid_node_count = sbi->total_valid_node_count + 1;
2569cfb271d4SChao Yu 	if (unlikely(valid_node_count > sbi->total_node_count)) {
257039a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
25710abd675eSChao Yu 		goto enospc;
257239a53e0cSJaegeuk Kim 	}
257339a53e0cSJaegeuk Kim 
2574ef86d709SGu Zheng 	sbi->total_valid_node_count++;
2575ef86d709SGu Zheng 	sbi->total_valid_block_count++;
257639a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
257739a53e0cSJaegeuk Kim 
25780abd675eSChao Yu 	if (inode) {
25790abd675eSChao Yu 		if (is_inode)
25800abd675eSChao Yu 			f2fs_mark_inode_dirty_sync(inode, true);
25810abd675eSChao Yu 		else
25820abd675eSChao Yu 			f2fs_i_blocks_write(inode, 1, true, true);
25830abd675eSChao Yu 	}
25840abd675eSChao Yu 
258541382ec4SJaegeuk Kim 	percpu_counter_inc(&sbi->alloc_valid_block_count);
25860abd675eSChao Yu 	return 0;
25870abd675eSChao Yu 
25880abd675eSChao Yu enospc:
2589af033b2aSChao Yu 	if (is_inode) {
2590af033b2aSChao Yu 		if (inode)
2591af033b2aSChao Yu 			dquot_free_inode(inode);
2592af033b2aSChao Yu 	} else {
25930abd675eSChao Yu 		dquot_release_reservation_block(inode, 1);
2594af033b2aSChao Yu 	}
25950abd675eSChao Yu 	return -ENOSPC;
259639a53e0cSJaegeuk Kim }
259739a53e0cSJaegeuk Kim 
259839a53e0cSJaegeuk Kim static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
2599000519f2SChao Yu 					struct inode *inode, bool is_inode)
260039a53e0cSJaegeuk Kim {
260139a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
260239a53e0cSJaegeuk Kim 
26034d17e6feSChao Yu 	if (unlikely(!sbi->total_valid_block_count ||
26044d17e6feSChao Yu 			!sbi->total_valid_node_count)) {
26054d17e6feSChao Yu 		f2fs_warn(sbi, "dec_valid_node_count: inconsistent block counts, total_valid_block:%u, total_valid_node:%u",
26064d17e6feSChao Yu 			  sbi->total_valid_block_count,
26074d17e6feSChao Yu 			  sbi->total_valid_node_count);
26084d17e6feSChao Yu 		set_sbi_flag(sbi, SBI_NEED_FSCK);
26094d17e6feSChao Yu 	} else {
2610ef86d709SGu Zheng 		sbi->total_valid_block_count--;
26114d17e6feSChao Yu 		sbi->total_valid_node_count--;
26124d17e6feSChao Yu 	}
26134d17e6feSChao Yu 
261480d42145SYunlong Song 	if (sbi->reserved_blocks &&
261580d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
261680d42145SYunlong Song 		sbi->current_reserved_blocks++;
261739a53e0cSJaegeuk Kim 
261839a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
26190abd675eSChao Yu 
2620ea6d7e72SChao Yu 	if (is_inode) {
2621af033b2aSChao Yu 		dquot_free_inode(inode);
2622ea6d7e72SChao Yu 	} else {
2623ea6d7e72SChao Yu 		if (unlikely(inode->i_blocks == 0)) {
2624097a7686SChao Yu 			f2fs_warn(sbi, "dec_valid_node_count: inconsistent i_blocks, ino:%lu, iblocks:%llu",
2625ea6d7e72SChao Yu 				  inode->i_ino,
2626ea6d7e72SChao Yu 				  (unsigned long long)inode->i_blocks);
2627ea6d7e72SChao Yu 			set_sbi_flag(sbi, SBI_NEED_FSCK);
2628ea6d7e72SChao Yu 			return;
2629ea6d7e72SChao Yu 		}
26300abd675eSChao Yu 		f2fs_i_blocks_write(inode, 1, false, true);
263139a53e0cSJaegeuk Kim 	}
2632ea6d7e72SChao Yu }
263339a53e0cSJaegeuk Kim 
263439a53e0cSJaegeuk Kim static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
263539a53e0cSJaegeuk Kim {
26368b8343faSJaegeuk Kim 	return sbi->total_valid_node_count;
263739a53e0cSJaegeuk Kim }
263839a53e0cSJaegeuk Kim 
263939a53e0cSJaegeuk Kim static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
264039a53e0cSJaegeuk Kim {
2641513c5f37SJaegeuk Kim 	percpu_counter_inc(&sbi->total_valid_inode_count);
264239a53e0cSJaegeuk Kim }
264339a53e0cSJaegeuk Kim 
26440e80220aSJaegeuk Kim static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
264539a53e0cSJaegeuk Kim {
2646513c5f37SJaegeuk Kim 	percpu_counter_dec(&sbi->total_valid_inode_count);
264739a53e0cSJaegeuk Kim }
264839a53e0cSJaegeuk Kim 
2649513c5f37SJaegeuk Kim static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
265039a53e0cSJaegeuk Kim {
2651513c5f37SJaegeuk Kim 	return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
265239a53e0cSJaegeuk Kim }
265339a53e0cSJaegeuk Kim 
2654a56c7c6fSJaegeuk Kim static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
2655a56c7c6fSJaegeuk Kim 						pgoff_t index, bool for_write)
2656a56c7c6fSJaegeuk Kim {
265782cf4f13SChao Yu 	struct page *page;
2658cac5a3d8SDongOh Shin 
26597fa750a1SArnd Bergmann 	if (IS_ENABLED(CONFIG_F2FS_FAULT_INJECTION)) {
266082cf4f13SChao Yu 		if (!for_write)
266182cf4f13SChao Yu 			page = find_get_page_flags(mapping, index,
266282cf4f13SChao Yu 							FGP_LOCK | FGP_ACCESSED);
266382cf4f13SChao Yu 		else
266482cf4f13SChao Yu 			page = find_lock_page(mapping, index);
2665c41f3cc3SJaegeuk Kim 		if (page)
2666c41f3cc3SJaegeuk Kim 			return page;
2667c41f3cc3SJaegeuk Kim 
266855523519SChao Yu 		if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) {
2669c45d6002SChao Yu 			f2fs_show_injection_info(F2FS_M_SB(mapping),
2670c45d6002SChao Yu 							FAULT_PAGE_ALLOC);
2671c41f3cc3SJaegeuk Kim 			return NULL;
267255523519SChao Yu 		}
26737fa750a1SArnd Bergmann 	}
26747fa750a1SArnd Bergmann 
2675a56c7c6fSJaegeuk Kim 	if (!for_write)
2676a56c7c6fSJaegeuk Kim 		return grab_cache_page(mapping, index);
2677a56c7c6fSJaegeuk Kim 	return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
2678a56c7c6fSJaegeuk Kim }
2679a56c7c6fSJaegeuk Kim 
268001eccef7SChao Yu static inline struct page *f2fs_pagecache_get_page(
268101eccef7SChao Yu 				struct address_space *mapping, pgoff_t index,
268201eccef7SChao Yu 				int fgp_flags, gfp_t gfp_mask)
268301eccef7SChao Yu {
268401eccef7SChao Yu 	if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_GET)) {
2685c45d6002SChao Yu 		f2fs_show_injection_info(F2FS_M_SB(mapping), FAULT_PAGE_GET);
268601eccef7SChao Yu 		return NULL;
268701eccef7SChao Yu 	}
26887fa750a1SArnd Bergmann 
268901eccef7SChao Yu 	return pagecache_get_page(mapping, index, fgp_flags, gfp_mask);
269001eccef7SChao Yu }
269101eccef7SChao Yu 
26926e2c64adSJaegeuk Kim static inline void f2fs_copy_page(struct page *src, struct page *dst)
26936e2c64adSJaegeuk Kim {
26946e2c64adSJaegeuk Kim 	char *src_kaddr = kmap(src);
26956e2c64adSJaegeuk Kim 	char *dst_kaddr = kmap(dst);
26966e2c64adSJaegeuk Kim 
26976e2c64adSJaegeuk Kim 	memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
26986e2c64adSJaegeuk Kim 	kunmap(dst);
26996e2c64adSJaegeuk Kim 	kunmap(src);
27006e2c64adSJaegeuk Kim }
27016e2c64adSJaegeuk Kim 
270239a53e0cSJaegeuk Kim static inline void f2fs_put_page(struct page *page, int unlock)
270339a53e0cSJaegeuk Kim {
2704031fa8ccSJaegeuk Kim 	if (!page)
270539a53e0cSJaegeuk Kim 		return;
270639a53e0cSJaegeuk Kim 
270739a53e0cSJaegeuk Kim 	if (unlock) {
27089850cf4aSJaegeuk Kim 		f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
270939a53e0cSJaegeuk Kim 		unlock_page(page);
271039a53e0cSJaegeuk Kim 	}
271109cbfeafSKirill A. Shutemov 	put_page(page);
271239a53e0cSJaegeuk Kim }
271339a53e0cSJaegeuk Kim 
271439a53e0cSJaegeuk Kim static inline void f2fs_put_dnode(struct dnode_of_data *dn)
271539a53e0cSJaegeuk Kim {
271639a53e0cSJaegeuk Kim 	if (dn->node_page)
271739a53e0cSJaegeuk Kim 		f2fs_put_page(dn->node_page, 1);
271839a53e0cSJaegeuk Kim 	if (dn->inode_page && dn->node_page != dn->inode_page)
271939a53e0cSJaegeuk Kim 		f2fs_put_page(dn->inode_page, 0);
272039a53e0cSJaegeuk Kim 	dn->node_page = NULL;
272139a53e0cSJaegeuk Kim 	dn->inode_page = NULL;
272239a53e0cSJaegeuk Kim }
272339a53e0cSJaegeuk Kim 
272439a53e0cSJaegeuk Kim static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
2725e8512d2eSGu Zheng 					size_t size)
272639a53e0cSJaegeuk Kim {
2727e8512d2eSGu Zheng 	return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
272839a53e0cSJaegeuk Kim }
272939a53e0cSJaegeuk Kim 
273032410577SChao Yu static inline void *f2fs_kmem_cache_alloc_nofail(struct kmem_cache *cachep,
27317bd59381SGu Zheng 						gfp_t flags)
27327bd59381SGu Zheng {
27337bd59381SGu Zheng 	void *entry;
27347bd59381SGu Zheng 
273580c54505SJaegeuk Kim 	entry = kmem_cache_alloc(cachep, flags);
273680c54505SJaegeuk Kim 	if (!entry)
273780c54505SJaegeuk Kim 		entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
27387bd59381SGu Zheng 	return entry;
27397bd59381SGu Zheng }
27407bd59381SGu Zheng 
274132410577SChao Yu static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
274232410577SChao Yu 			gfp_t flags, bool nofail, struct f2fs_sb_info *sbi)
274332410577SChao Yu {
274432410577SChao Yu 	if (nofail)
274532410577SChao Yu 		return f2fs_kmem_cache_alloc_nofail(cachep, flags);
274632410577SChao Yu 
274732410577SChao Yu 	if (time_to_inject(sbi, FAULT_SLAB_ALLOC)) {
274832410577SChao Yu 		f2fs_show_injection_info(sbi, FAULT_SLAB_ALLOC);
274932410577SChao Yu 		return NULL;
275032410577SChao Yu 	}
275132410577SChao Yu 
275232410577SChao Yu 	return kmem_cache_alloc(cachep, flags);
275332410577SChao Yu }
275432410577SChao Yu 
2755493720a4SChao Yu static inline bool is_inflight_io(struct f2fs_sb_info *sbi, int type)
27565f9abab4SJaegeuk Kim {
27575f9abab4SJaegeuk Kim 	if (get_pages(sbi, F2FS_RD_DATA) || get_pages(sbi, F2FS_RD_NODE) ||
27585f9abab4SJaegeuk Kim 		get_pages(sbi, F2FS_RD_META) || get_pages(sbi, F2FS_WB_DATA) ||
2759fef4129eSChao Yu 		get_pages(sbi, F2FS_WB_CP_DATA) ||
2760fef4129eSChao Yu 		get_pages(sbi, F2FS_DIO_READ) ||
276111ac8ef8SJaegeuk Kim 		get_pages(sbi, F2FS_DIO_WRITE))
2762493720a4SChao Yu 		return true;
276311ac8ef8SJaegeuk Kim 
276456659ce8SSahitya Tummala 	if (type != DISCARD_TIME && SM_I(sbi) && SM_I(sbi)->dcc_info &&
276511ac8ef8SJaegeuk Kim 			atomic_read(&SM_I(sbi)->dcc_info->queued_discard))
2766493720a4SChao Yu 		return true;
276711ac8ef8SJaegeuk Kim 
276811ac8ef8SJaegeuk Kim 	if (SM_I(sbi) && SM_I(sbi)->fcc_info &&
276972691af6SJaegeuk Kim 			atomic_read(&SM_I(sbi)->fcc_info->queued_flush))
2770493720a4SChao Yu 		return true;
2771493720a4SChao Yu 	return false;
2772493720a4SChao Yu }
2773493720a4SChao Yu 
2774493720a4SChao Yu static inline bool is_idle(struct f2fs_sb_info *sbi, int type)
2775493720a4SChao Yu {
2776493720a4SChao Yu 	if (sbi->gc_mode == GC_URGENT_HIGH)
2777493720a4SChao Yu 		return true;
2778493720a4SChao Yu 
2779493720a4SChao Yu 	if (is_inflight_io(sbi, type))
27805f9abab4SJaegeuk Kim 		return false;
278111ac8ef8SJaegeuk Kim 
2782d98af5f4SDaeho Jeong 	if (sbi->gc_mode == GC_URGENT_MID)
2783d98af5f4SDaeho Jeong 		return true;
2784d98af5f4SDaeho Jeong 
27850e5e8111SDaeho Jeong 	if (sbi->gc_mode == GC_URGENT_LOW &&
27860e5e8111SDaeho Jeong 			(type == DISCARD_TIME || type == GC_TIME))
27870e5e8111SDaeho Jeong 		return true;
27880e5e8111SDaeho Jeong 
27895f9abab4SJaegeuk Kim 	return f2fs_time_over(sbi, type);
27905f9abab4SJaegeuk Kim }
27915f9abab4SJaegeuk Kim 
27929be32d72SJaegeuk Kim static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
27939be32d72SJaegeuk Kim 				unsigned long index, void *item)
27949be32d72SJaegeuk Kim {
27959be32d72SJaegeuk Kim 	while (radix_tree_insert(root, index, item))
27969be32d72SJaegeuk Kim 		cond_resched();
27979be32d72SJaegeuk Kim }
27989be32d72SJaegeuk Kim 
279939a53e0cSJaegeuk Kim #define RAW_IS_INODE(p)	((p)->footer.nid == (p)->footer.ino)
280039a53e0cSJaegeuk Kim 
280139a53e0cSJaegeuk Kim static inline bool IS_INODE(struct page *page)
280239a53e0cSJaegeuk Kim {
280345590710SGu Zheng 	struct f2fs_node *p = F2FS_NODE(page);
2804cac5a3d8SDongOh Shin 
280539a53e0cSJaegeuk Kim 	return RAW_IS_INODE(p);
280639a53e0cSJaegeuk Kim }
280739a53e0cSJaegeuk Kim 
28087a2af766SChao Yu static inline int offset_in_addr(struct f2fs_inode *i)
28097a2af766SChao Yu {
28107a2af766SChao Yu 	return (i->i_inline & F2FS_EXTRA_ATTR) ?
28117a2af766SChao Yu 			(le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0;
28127a2af766SChao Yu }
28137a2af766SChao Yu 
281439a53e0cSJaegeuk Kim static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
281539a53e0cSJaegeuk Kim {
281639a53e0cSJaegeuk Kim 	return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
281739a53e0cSJaegeuk Kim }
281839a53e0cSJaegeuk Kim 
28197a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode);
2820a2ced1ceSChao Yu static inline block_t data_blkaddr(struct inode *inode,
28217a2af766SChao Yu 			struct page *node_page, unsigned int offset)
282239a53e0cSJaegeuk Kim {
282339a53e0cSJaegeuk Kim 	struct f2fs_node *raw_node;
282439a53e0cSJaegeuk Kim 	__le32 *addr_array;
28257a2af766SChao Yu 	int base = 0;
28267a2af766SChao Yu 	bool is_inode = IS_INODE(node_page);
2827cac5a3d8SDongOh Shin 
282845590710SGu Zheng 	raw_node = F2FS_NODE(node_page);
28297a2af766SChao Yu 
2830979f492fSLiFan 	if (is_inode) {
2831979f492fSLiFan 		if (!inode)
28327a88ddb5SChao Yu 			/* from GC path only */
28337a2af766SChao Yu 			base = offset_in_addr(&raw_node->i);
2834979f492fSLiFan 		else if (f2fs_has_extra_attr(inode))
28357a2af766SChao Yu 			base = get_extra_isize(inode);
28367a2af766SChao Yu 	}
28377a2af766SChao Yu 
283839a53e0cSJaegeuk Kim 	addr_array = blkaddr_in_node(raw_node);
28397a2af766SChao Yu 	return le32_to_cpu(addr_array[base + offset]);
284039a53e0cSJaegeuk Kim }
284139a53e0cSJaegeuk Kim 
2842a2ced1ceSChao Yu static inline block_t f2fs_data_blkaddr(struct dnode_of_data *dn)
2843a2ced1ceSChao Yu {
2844a2ced1ceSChao Yu 	return data_blkaddr(dn->inode, dn->node_page, dn->ofs_in_node);
2845a2ced1ceSChao Yu }
2846a2ced1ceSChao Yu 
284739a53e0cSJaegeuk Kim static inline int f2fs_test_bit(unsigned int nr, char *addr)
284839a53e0cSJaegeuk Kim {
284939a53e0cSJaegeuk Kim 	int mask;
285039a53e0cSJaegeuk Kim 
285139a53e0cSJaegeuk Kim 	addr += (nr >> 3);
285239a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
285339a53e0cSJaegeuk Kim 	return mask & *addr;
285439a53e0cSJaegeuk Kim }
285539a53e0cSJaegeuk Kim 
2856a66cdd98SJaegeuk Kim static inline void f2fs_set_bit(unsigned int nr, char *addr)
2857a66cdd98SJaegeuk Kim {
2858a66cdd98SJaegeuk Kim 	int mask;
2859a66cdd98SJaegeuk Kim 
2860a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2861a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2862a66cdd98SJaegeuk Kim 	*addr |= mask;
2863a66cdd98SJaegeuk Kim }
2864a66cdd98SJaegeuk Kim 
2865a66cdd98SJaegeuk Kim static inline void f2fs_clear_bit(unsigned int nr, char *addr)
2866a66cdd98SJaegeuk Kim {
2867a66cdd98SJaegeuk Kim 	int mask;
2868a66cdd98SJaegeuk Kim 
2869a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2870a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2871a66cdd98SJaegeuk Kim 	*addr &= ~mask;
2872a66cdd98SJaegeuk Kim }
2873a66cdd98SJaegeuk Kim 
287452aca074SGu Zheng static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
287539a53e0cSJaegeuk Kim {
287639a53e0cSJaegeuk Kim 	int mask;
287739a53e0cSJaegeuk Kim 	int ret;
287839a53e0cSJaegeuk Kim 
287939a53e0cSJaegeuk Kim 	addr += (nr >> 3);
288039a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
288139a53e0cSJaegeuk Kim 	ret = mask & *addr;
288239a53e0cSJaegeuk Kim 	*addr |= mask;
288339a53e0cSJaegeuk Kim 	return ret;
288439a53e0cSJaegeuk Kim }
288539a53e0cSJaegeuk Kim 
288652aca074SGu Zheng static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
288739a53e0cSJaegeuk Kim {
288839a53e0cSJaegeuk Kim 	int mask;
288939a53e0cSJaegeuk Kim 	int ret;
289039a53e0cSJaegeuk Kim 
289139a53e0cSJaegeuk Kim 	addr += (nr >> 3);
289239a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
289339a53e0cSJaegeuk Kim 	ret = mask & *addr;
289439a53e0cSJaegeuk Kim 	*addr &= ~mask;
289539a53e0cSJaegeuk Kim 	return ret;
289639a53e0cSJaegeuk Kim }
289739a53e0cSJaegeuk Kim 
2898c6ac4c0eSGu Zheng static inline void f2fs_change_bit(unsigned int nr, char *addr)
2899c6ac4c0eSGu Zheng {
2900c6ac4c0eSGu Zheng 	int mask;
2901c6ac4c0eSGu Zheng 
2902c6ac4c0eSGu Zheng 	addr += (nr >> 3);
2903c6ac4c0eSGu Zheng 	mask = 1 << (7 - (nr & 0x07));
2904c6ac4c0eSGu Zheng 	*addr ^= mask;
2905c6ac4c0eSGu Zheng }
2906c6ac4c0eSGu Zheng 
290759c84408SChao Yu /*
290836098557SEric Biggers  * On-disk inode flags (f2fs_inode::i_flags)
290959c84408SChao Yu  */
29104c8ff709SChao Yu #define F2FS_COMPR_FL			0x00000004 /* Compress file */
291159c84408SChao Yu #define F2FS_SYNC_FL			0x00000008 /* Synchronous updates */
291259c84408SChao Yu #define F2FS_IMMUTABLE_FL		0x00000010 /* Immutable file */
291359c84408SChao Yu #define F2FS_APPEND_FL			0x00000020 /* writes to file may only append */
291459c84408SChao Yu #define F2FS_NODUMP_FL			0x00000040 /* do not dump file */
291559c84408SChao Yu #define F2FS_NOATIME_FL			0x00000080 /* do not update atime */
29164c8ff709SChao Yu #define F2FS_NOCOMP_FL			0x00000400 /* Don't compress */
291759c84408SChao Yu #define F2FS_INDEX_FL			0x00001000 /* hash-indexed directory */
291859c84408SChao Yu #define F2FS_DIRSYNC_FL			0x00010000 /* dirsync behaviour (directories only) */
291959c84408SChao Yu #define F2FS_PROJINHERIT_FL		0x20000000 /* Create with parents projid */
29202c2eb7a3SDaniel Rosenberg #define F2FS_CASEFOLD_FL		0x40000000 /* Casefolded file */
292159c84408SChao Yu 
292259c84408SChao Yu /* Flags that should be inherited by new inodes from their parent. */
292336098557SEric Biggers #define F2FS_FL_INHERITED (F2FS_SYNC_FL | F2FS_NODUMP_FL | F2FS_NOATIME_FL | \
29242c2eb7a3SDaniel Rosenberg 			   F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL | \
29254c8ff709SChao Yu 			   F2FS_CASEFOLD_FL | F2FS_COMPR_FL | F2FS_NOCOMP_FL)
292659c84408SChao Yu 
292759c84408SChao Yu /* Flags that are appropriate for regular files (all but dir-specific ones). */
29282c2eb7a3SDaniel Rosenberg #define F2FS_REG_FLMASK		(~(F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL | \
29292c2eb7a3SDaniel Rosenberg 				F2FS_CASEFOLD_FL))
293059c84408SChao Yu 
293159c84408SChao Yu /* Flags that are appropriate for non-directories/regular files. */
293259c84408SChao Yu #define F2FS_OTHER_FLMASK	(F2FS_NODUMP_FL | F2FS_NOATIME_FL)
29335c57132eSChao Yu 
29345c57132eSChao Yu static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
29355c57132eSChao Yu {
29365c57132eSChao Yu 	if (S_ISDIR(mode))
29375c57132eSChao Yu 		return flags;
29385c57132eSChao Yu 	else if (S_ISREG(mode))
29395c57132eSChao Yu 		return flags & F2FS_REG_FLMASK;
29405c57132eSChao Yu 	else
29415c57132eSChao Yu 		return flags & F2FS_OTHER_FLMASK;
29425c57132eSChao Yu }
29435c57132eSChao Yu 
2944205b9822SJaegeuk Kim static inline void __mark_inode_dirty_flag(struct inode *inode,
2945205b9822SJaegeuk Kim 						int flag, bool set)
294639a53e0cSJaegeuk Kim {
2947205b9822SJaegeuk Kim 	switch (flag) {
2948205b9822SJaegeuk Kim 	case FI_INLINE_XATTR:
2949205b9822SJaegeuk Kim 	case FI_INLINE_DATA:
2950205b9822SJaegeuk Kim 	case FI_INLINE_DENTRY:
29519ac1e2d8SDaeho Jeong 	case FI_NEW_INODE:
2952205b9822SJaegeuk Kim 		if (set)
2953205b9822SJaegeuk Kim 			return;
2954df561f66SGustavo A. R. Silva 		fallthrough;
2955205b9822SJaegeuk Kim 	case FI_DATA_EXIST:
2956205b9822SJaegeuk Kim 	case FI_INLINE_DOTS:
29571ad71a27SJaegeuk Kim 	case FI_PIN_FILE:
2958c6140415SJaegeuk Kim 	case FI_COMPRESS_RELEASED:
29597c45729aSJaegeuk Kim 		f2fs_mark_inode_dirty_sync(inode, true);
2960205b9822SJaegeuk Kim 	}
296139a53e0cSJaegeuk Kim }
296239a53e0cSJaegeuk Kim 
296391942321SJaegeuk Kim static inline void set_inode_flag(struct inode *inode, int flag)
296439a53e0cSJaegeuk Kim {
296558f7e00fSYufen Yu 	set_bit(flag, F2FS_I(inode)->flags);
2966205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, true);
296739a53e0cSJaegeuk Kim }
296839a53e0cSJaegeuk Kim 
296991942321SJaegeuk Kim static inline int is_inode_flag_set(struct inode *inode, int flag)
297039a53e0cSJaegeuk Kim {
29717653b9d8SChao Yu 	return test_bit(flag, F2FS_I(inode)->flags);
297239a53e0cSJaegeuk Kim }
297339a53e0cSJaegeuk Kim 
297491942321SJaegeuk Kim static inline void clear_inode_flag(struct inode *inode, int flag)
297539a53e0cSJaegeuk Kim {
297658f7e00fSYufen Yu 	clear_bit(flag, F2FS_I(inode)->flags);
2977205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, false);
297839a53e0cSJaegeuk Kim }
297939a53e0cSJaegeuk Kim 
298095ae251fSEric Biggers static inline bool f2fs_verity_in_progress(struct inode *inode)
298195ae251fSEric Biggers {
298295ae251fSEric Biggers 	return IS_ENABLED(CONFIG_FS_VERITY) &&
298395ae251fSEric Biggers 	       is_inode_flag_set(inode, FI_VERITY_IN_PROGRESS);
298495ae251fSEric Biggers }
298595ae251fSEric Biggers 
298691942321SJaegeuk Kim static inline void set_acl_inode(struct inode *inode, umode_t mode)
2987444c580fSJaegeuk Kim {
298891942321SJaegeuk Kim 	F2FS_I(inode)->i_acl_mode = mode;
298991942321SJaegeuk Kim 	set_inode_flag(inode, FI_ACL_MODE);
29907c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, false);
299139a53e0cSJaegeuk Kim }
299239a53e0cSJaegeuk Kim 
2993a1961246SJaegeuk Kim static inline void f2fs_i_links_write(struct inode *inode, bool inc)
2994a1961246SJaegeuk Kim {
2995a1961246SJaegeuk Kim 	if (inc)
2996a1961246SJaegeuk Kim 		inc_nlink(inode);
2997a1961246SJaegeuk Kim 	else
2998a1961246SJaegeuk Kim 		drop_nlink(inode);
29997c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3000a1961246SJaegeuk Kim }
3001a1961246SJaegeuk Kim 
30028edd03c8SJaegeuk Kim static inline void f2fs_i_blocks_write(struct inode *inode,
30030abd675eSChao Yu 					block_t diff, bool add, bool claim)
30048edd03c8SJaegeuk Kim {
300526de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
300626de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
300726de9b11SJaegeuk Kim 
30080abd675eSChao Yu 	/* add = 1, claim = 1 should be dquot_reserve_block in pair */
30090abd675eSChao Yu 	if (add) {
30100abd675eSChao Yu 		if (claim)
30110abd675eSChao Yu 			dquot_claim_block(inode, diff);
30120abd675eSChao Yu 		else
30130abd675eSChao Yu 			dquot_alloc_block_nofail(inode, diff);
30140abd675eSChao Yu 	} else {
30150abd675eSChao Yu 		dquot_free_block(inode, diff);
30160abd675eSChao Yu 	}
30170abd675eSChao Yu 
30187c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
301926de9b11SJaegeuk Kim 	if (clean || recover)
302026de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
30218edd03c8SJaegeuk Kim }
30228edd03c8SJaegeuk Kim 
3023fc9581c8SJaegeuk Kim static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
3024fc9581c8SJaegeuk Kim {
302526de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
302626de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
302726de9b11SJaegeuk Kim 
3028fc9581c8SJaegeuk Kim 	if (i_size_read(inode) == i_size)
3029fc9581c8SJaegeuk Kim 		return;
3030fc9581c8SJaegeuk Kim 
3031fc9581c8SJaegeuk Kim 	i_size_write(inode, i_size);
30327c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
303326de9b11SJaegeuk Kim 	if (clean || recover)
303426de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
303526de9b11SJaegeuk Kim }
303626de9b11SJaegeuk Kim 
3037205b9822SJaegeuk Kim static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
3038205b9822SJaegeuk Kim {
3039205b9822SJaegeuk Kim 	F2FS_I(inode)->i_current_depth = depth;
30407c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3041205b9822SJaegeuk Kim }
3042205b9822SJaegeuk Kim 
30431ad71a27SJaegeuk Kim static inline void f2fs_i_gc_failures_write(struct inode *inode,
30441ad71a27SJaegeuk Kim 					unsigned int count)
30451ad71a27SJaegeuk Kim {
30462ef79ecbSChao Yu 	F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN] = count;
30471ad71a27SJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
30481ad71a27SJaegeuk Kim }
30491ad71a27SJaegeuk Kim 
3050205b9822SJaegeuk Kim static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
3051205b9822SJaegeuk Kim {
3052205b9822SJaegeuk Kim 	F2FS_I(inode)->i_xattr_nid = xnid;
30537c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3054205b9822SJaegeuk Kim }
3055205b9822SJaegeuk Kim 
3056205b9822SJaegeuk Kim static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
3057205b9822SJaegeuk Kim {
3058205b9822SJaegeuk Kim 	F2FS_I(inode)->i_pino = pino;
30597c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3060205b9822SJaegeuk Kim }
3061205b9822SJaegeuk Kim 
306291942321SJaegeuk Kim static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
3063444c580fSJaegeuk Kim {
3064205b9822SJaegeuk Kim 	struct f2fs_inode_info *fi = F2FS_I(inode);
3065205b9822SJaegeuk Kim 
3066444c580fSJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_XATTR)
30677653b9d8SChao Yu 		set_bit(FI_INLINE_XATTR, fi->flags);
30681001b347SHuajun Li 	if (ri->i_inline & F2FS_INLINE_DATA)
30697653b9d8SChao Yu 		set_bit(FI_INLINE_DATA, fi->flags);
307034d67debSChao Yu 	if (ri->i_inline & F2FS_INLINE_DENTRY)
30717653b9d8SChao Yu 		set_bit(FI_INLINE_DENTRY, fi->flags);
3072b3d208f9SJaegeuk Kim 	if (ri->i_inline & F2FS_DATA_EXIST)
30737653b9d8SChao Yu 		set_bit(FI_DATA_EXIST, fi->flags);
3074510022a8SJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_DOTS)
30757653b9d8SChao Yu 		set_bit(FI_INLINE_DOTS, fi->flags);
30767a2af766SChao Yu 	if (ri->i_inline & F2FS_EXTRA_ATTR)
30777653b9d8SChao Yu 		set_bit(FI_EXTRA_ATTR, fi->flags);
30781ad71a27SJaegeuk Kim 	if (ri->i_inline & F2FS_PIN_FILE)
30797653b9d8SChao Yu 		set_bit(FI_PIN_FILE, fi->flags);
3080c6140415SJaegeuk Kim 	if (ri->i_inline & F2FS_COMPRESS_RELEASED)
3081c6140415SJaegeuk Kim 		set_bit(FI_COMPRESS_RELEASED, fi->flags);
3082444c580fSJaegeuk Kim }
3083444c580fSJaegeuk Kim 
308491942321SJaegeuk Kim static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
3085444c580fSJaegeuk Kim {
3086444c580fSJaegeuk Kim 	ri->i_inline = 0;
3087444c580fSJaegeuk Kim 
308891942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_XATTR))
3089444c580fSJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_XATTR;
309091942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DATA))
30911001b347SHuajun Li 		ri->i_inline |= F2FS_INLINE_DATA;
309291942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
309334d67debSChao Yu 		ri->i_inline |= F2FS_INLINE_DENTRY;
309491942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_DATA_EXIST))
3095b3d208f9SJaegeuk Kim 		ri->i_inline |= F2FS_DATA_EXIST;
309691942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DOTS))
3097510022a8SJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_DOTS;
30987a2af766SChao Yu 	if (is_inode_flag_set(inode, FI_EXTRA_ATTR))
30997a2af766SChao Yu 		ri->i_inline |= F2FS_EXTRA_ATTR;
31001ad71a27SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_PIN_FILE))
31011ad71a27SJaegeuk Kim 		ri->i_inline |= F2FS_PIN_FILE;
3102c6140415SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
3103c6140415SJaegeuk Kim 		ri->i_inline |= F2FS_COMPRESS_RELEASED;
31047a2af766SChao Yu }
31057a2af766SChao Yu 
31067a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode)
31077a2af766SChao Yu {
31087a2af766SChao Yu 	return is_inode_flag_set(inode, FI_EXTRA_ATTR);
3109444c580fSJaegeuk Kim }
3110444c580fSJaegeuk Kim 
3111987c7c31SChao Yu static inline int f2fs_has_inline_xattr(struct inode *inode)
3112987c7c31SChao Yu {
311391942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_XATTR);
3114987c7c31SChao Yu }
3115987c7c31SChao Yu 
31164c8ff709SChao Yu static inline int f2fs_compressed_file(struct inode *inode)
31174c8ff709SChao Yu {
31184c8ff709SChao Yu 	return S_ISREG(inode->i_mode) &&
31194c8ff709SChao Yu 		is_inode_flag_set(inode, FI_COMPRESSED_FILE);
31204c8ff709SChao Yu }
31214c8ff709SChao Yu 
3122602a16d5SDaeho Jeong static inline bool f2fs_need_compress_data(struct inode *inode)
3123602a16d5SDaeho Jeong {
3124602a16d5SDaeho Jeong 	int compress_mode = F2FS_OPTION(F2FS_I_SB(inode)).compress_mode;
3125602a16d5SDaeho Jeong 
3126602a16d5SDaeho Jeong 	if (!f2fs_compressed_file(inode))
3127602a16d5SDaeho Jeong 		return false;
3128602a16d5SDaeho Jeong 
3129602a16d5SDaeho Jeong 	if (compress_mode == COMPR_MODE_FS)
3130602a16d5SDaeho Jeong 		return true;
3131602a16d5SDaeho Jeong 	else if (compress_mode == COMPR_MODE_USER &&
3132602a16d5SDaeho Jeong 			is_inode_flag_set(inode, FI_ENABLE_COMPRESS))
3133602a16d5SDaeho Jeong 		return true;
3134602a16d5SDaeho Jeong 
3135602a16d5SDaeho Jeong 	return false;
3136602a16d5SDaeho Jeong }
3137602a16d5SDaeho Jeong 
313881ca7350SChao Yu static inline unsigned int addrs_per_inode(struct inode *inode)
3139de93653fSJaegeuk Kim {
3140d02a6e61SChao Yu 	unsigned int addrs = CUR_ADDRS_PER_INODE(inode) -
3141d02a6e61SChao Yu 				get_inline_xattr_addrs(inode);
31424c8ff709SChao Yu 
31434c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
31444c8ff709SChao Yu 		return addrs;
31454c8ff709SChao Yu 	return ALIGN_DOWN(addrs, F2FS_I(inode)->i_cluster_size);
3146d02a6e61SChao Yu }
3147d02a6e61SChao Yu 
3148d02a6e61SChao Yu static inline unsigned int addrs_per_block(struct inode *inode)
3149d02a6e61SChao Yu {
31504c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
31514c8ff709SChao Yu 		return DEF_ADDRS_PER_BLOCK;
31524c8ff709SChao Yu 	return ALIGN_DOWN(DEF_ADDRS_PER_BLOCK, F2FS_I(inode)->i_cluster_size);
3153de93653fSJaegeuk Kim }
3154de93653fSJaegeuk Kim 
31556afc662eSChao Yu static inline void *inline_xattr_addr(struct inode *inode, struct page *page)
315665985d93SJaegeuk Kim {
3157695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
3158cac5a3d8SDongOh Shin 
315965985d93SJaegeuk Kim 	return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
3160b323fd28SChao Yu 					get_inline_xattr_addrs(inode)]);
316165985d93SJaegeuk Kim }
316265985d93SJaegeuk Kim 
316365985d93SJaegeuk Kim static inline int inline_xattr_size(struct inode *inode)
316465985d93SJaegeuk Kim {
3165622927f3SChao Yu 	if (f2fs_has_inline_xattr(inode))
31666afc662eSChao Yu 		return get_inline_xattr_addrs(inode) * sizeof(__le32);
3167622927f3SChao Yu 	return 0;
316865985d93SJaegeuk Kim }
316965985d93SJaegeuk Kim 
31700dbdc2aeSJaegeuk Kim static inline int f2fs_has_inline_data(struct inode *inode)
31710dbdc2aeSJaegeuk Kim {
317291942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DATA);
31730dbdc2aeSJaegeuk Kim }
31740dbdc2aeSJaegeuk Kim 
3175b3d208f9SJaegeuk Kim static inline int f2fs_exist_data(struct inode *inode)
3176b3d208f9SJaegeuk Kim {
317791942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DATA_EXIST);
3178b3d208f9SJaegeuk Kim }
3179b3d208f9SJaegeuk Kim 
3180510022a8SJaegeuk Kim static inline int f2fs_has_inline_dots(struct inode *inode)
3181510022a8SJaegeuk Kim {
318291942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DOTS);
3183510022a8SJaegeuk Kim }
3184510022a8SJaegeuk Kim 
31854c8ff709SChao Yu static inline int f2fs_is_mmap_file(struct inode *inode)
31864c8ff709SChao Yu {
31874c8ff709SChao Yu 	return is_inode_flag_set(inode, FI_MMAP_FILE);
31884c8ff709SChao Yu }
31894c8ff709SChao Yu 
31901ad71a27SJaegeuk Kim static inline bool f2fs_is_pinned_file(struct inode *inode)
31911ad71a27SJaegeuk Kim {
31921ad71a27SJaegeuk Kim 	return is_inode_flag_set(inode, FI_PIN_FILE);
31931ad71a27SJaegeuk Kim }
31941ad71a27SJaegeuk Kim 
319588b88a66SJaegeuk Kim static inline bool f2fs_is_atomic_file(struct inode *inode)
319688b88a66SJaegeuk Kim {
319791942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_ATOMIC_FILE);
319888b88a66SJaegeuk Kim }
319988b88a66SJaegeuk Kim 
32003c6c2bebSJaegeuk Kim static inline bool f2fs_is_first_block_written(struct inode *inode)
32013c6c2bebSJaegeuk Kim {
320291942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
32033c6c2bebSJaegeuk Kim }
32043c6c2bebSJaegeuk Kim 
32051e84371fSJaegeuk Kim static inline bool f2fs_is_drop_cache(struct inode *inode)
32061e84371fSJaegeuk Kim {
320791942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DROP_CACHE);
32081e84371fSJaegeuk Kim }
32091e84371fSJaegeuk Kim 
3210f2470371SChao Yu static inline void *inline_data_addr(struct inode *inode, struct page *page)
32111001b347SHuajun Li {
3212695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
32137a2af766SChao Yu 	int extra_size = get_extra_isize(inode);
3214cac5a3d8SDongOh Shin 
32157a2af766SChao Yu 	return (void *)&(ri->i_addr[extra_size + DEF_INLINE_RESERVED_SIZE]);
32161001b347SHuajun Li }
32171001b347SHuajun Li 
321834d67debSChao Yu static inline int f2fs_has_inline_dentry(struct inode *inode)
321934d67debSChao Yu {
322091942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DENTRY);
322134d67debSChao Yu }
322234d67debSChao Yu 
3223b5492af7SJaegeuk Kim static inline int is_file(struct inode *inode, int type)
3224b5492af7SJaegeuk Kim {
3225b5492af7SJaegeuk Kim 	return F2FS_I(inode)->i_advise & type;
3226b5492af7SJaegeuk Kim }
3227b5492af7SJaegeuk Kim 
3228b5492af7SJaegeuk Kim static inline void set_file(struct inode *inode, int type)
3229b5492af7SJaegeuk Kim {
3230766c6639SJaegeuk Kim 	if (is_file(inode, type))
3231766c6639SJaegeuk Kim 		return;
3232b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise |= type;
32337c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3234b5492af7SJaegeuk Kim }
3235b5492af7SJaegeuk Kim 
3236b5492af7SJaegeuk Kim static inline void clear_file(struct inode *inode, int type)
3237b5492af7SJaegeuk Kim {
3238766c6639SJaegeuk Kim 	if (!is_file(inode, type))
3239766c6639SJaegeuk Kim 		return;
3240b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise &= ~type;
32417c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3242b5492af7SJaegeuk Kim }
3243b5492af7SJaegeuk Kim 
3244fe1897eaSChao Yu static inline bool f2fs_is_time_consistent(struct inode *inode)
3245fe1897eaSChao Yu {
3246fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time, &inode->i_atime))
3247fe1897eaSChao Yu 		return false;
3248fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 1, &inode->i_ctime))
3249fe1897eaSChao Yu 		return false;
3250fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 2, &inode->i_mtime))
3251fe1897eaSChao Yu 		return false;
3252fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 3,
3253fe1897eaSChao Yu 						&F2FS_I(inode)->i_crtime))
3254fe1897eaSChao Yu 		return false;
3255fe1897eaSChao Yu 	return true;
3256fe1897eaSChao Yu }
3257fe1897eaSChao Yu 
325826787236SJaegeuk Kim static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
325926787236SJaegeuk Kim {
3260a0d00fadSChao Yu 	bool ret;
3261a0d00fadSChao Yu 
326226787236SJaegeuk Kim 	if (dsync) {
326326787236SJaegeuk Kim 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
326426787236SJaegeuk Kim 
326526787236SJaegeuk Kim 		spin_lock(&sbi->inode_lock[DIRTY_META]);
326626787236SJaegeuk Kim 		ret = list_empty(&F2FS_I(inode)->gdirty_list);
326726787236SJaegeuk Kim 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
326826787236SJaegeuk Kim 		return ret;
326926787236SJaegeuk Kim 	}
327026787236SJaegeuk Kim 	if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
327126787236SJaegeuk Kim 			file_keep_isize(inode) ||
3272235831d7SJunling Zheng 			i_size_read(inode) & ~PAGE_MASK)
327326787236SJaegeuk Kim 		return false;
3274a0d00fadSChao Yu 
3275fe1897eaSChao Yu 	if (!f2fs_is_time_consistent(inode))
3276214c2461SJaegeuk Kim 		return false;
3277214c2461SJaegeuk Kim 
3278c10c9820SChao Yu 	spin_lock(&F2FS_I(inode)->i_size_lock);
3279a0d00fadSChao Yu 	ret = F2FS_I(inode)->last_disk_size == i_size_read(inode);
3280c10c9820SChao Yu 	spin_unlock(&F2FS_I(inode)->i_size_lock);
3281a0d00fadSChao Yu 
3282a0d00fadSChao Yu 	return ret;
3283267378d4SChao Yu }
3284267378d4SChao Yu 
3285deeedd71SChao Yu static inline bool f2fs_readonly(struct super_block *sb)
328677888c1eSJaegeuk Kim {
3287deeedd71SChao Yu 	return sb_rdonly(sb);
328877888c1eSJaegeuk Kim }
328977888c1eSJaegeuk Kim 
3290744602cfSJaegeuk Kim static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
3291744602cfSJaegeuk Kim {
3292aaec2b1dSChao Yu 	return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
3293744602cfSJaegeuk Kim }
3294744602cfSJaegeuk Kim 
329543c780baSEric Biggers static inline bool is_dot_dotdot(const u8 *name, size_t len)
3296eaa693f4SJaegeuk Kim {
329743c780baSEric Biggers 	if (len == 1 && name[0] == '.')
3298eaa693f4SJaegeuk Kim 		return true;
3299eaa693f4SJaegeuk Kim 
330043c780baSEric Biggers 	if (len == 2 && name[0] == '.' && name[1] == '.')
3301eaa693f4SJaegeuk Kim 		return true;
3302eaa693f4SJaegeuk Kim 
3303eaa693f4SJaegeuk Kim 	return false;
3304eaa693f4SJaegeuk Kim }
3305eaa693f4SJaegeuk Kim 
33061ecc0c5cSChao Yu static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
33071ecc0c5cSChao Yu 					size_t size, gfp_t flags)
33080414b004SJaegeuk Kim {
330955523519SChao Yu 	if (time_to_inject(sbi, FAULT_KMALLOC)) {
3310c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_KMALLOC);
33112c63feadSJaegeuk Kim 		return NULL;
331255523519SChao Yu 	}
33137fa750a1SArnd Bergmann 
33140b6d4ca0SEric Biggers 	return kmalloc(size, flags);
33150414b004SJaegeuk Kim }
33160414b004SJaegeuk Kim 
3317acbf054dSChao Yu static inline void *f2fs_kzalloc(struct f2fs_sb_info *sbi,
3318acbf054dSChao Yu 					size_t size, gfp_t flags)
3319acbf054dSChao Yu {
3320acbf054dSChao Yu 	return f2fs_kmalloc(sbi, size, flags | __GFP_ZERO);
3321acbf054dSChao Yu }
3322acbf054dSChao Yu 
3323628b3d14SChao Yu static inline void *f2fs_kvmalloc(struct f2fs_sb_info *sbi,
3324628b3d14SChao Yu 					size_t size, gfp_t flags)
3325628b3d14SChao Yu {
3326628b3d14SChao Yu 	if (time_to_inject(sbi, FAULT_KVMALLOC)) {
3327c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_KVMALLOC);
3328628b3d14SChao Yu 		return NULL;
3329628b3d14SChao Yu 	}
33307fa750a1SArnd Bergmann 
3331628b3d14SChao Yu 	return kvmalloc(size, flags);
3332628b3d14SChao Yu }
3333628b3d14SChao Yu 
3334628b3d14SChao Yu static inline void *f2fs_kvzalloc(struct f2fs_sb_info *sbi,
3335628b3d14SChao Yu 					size_t size, gfp_t flags)
3336628b3d14SChao Yu {
3337628b3d14SChao Yu 	return f2fs_kvmalloc(sbi, size, flags | __GFP_ZERO);
3338628b3d14SChao Yu }
3339628b3d14SChao Yu 
33407a2af766SChao Yu static inline int get_extra_isize(struct inode *inode)
3341f2470371SChao Yu {
33427a2af766SChao Yu 	return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
3343f2470371SChao Yu }
3344f2470371SChao Yu 
33456afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode)
33466afc662eSChao Yu {
33476afc662eSChao Yu 	return F2FS_I(inode)->i_inline_xattr_size;
33486afc662eSChao Yu }
33496afc662eSChao Yu 
33504d57b86dSChao Yu #define f2fs_get_inode_mode(i) \
335191942321SJaegeuk Kim 	((is_inode_flag_set(i, FI_ACL_MODE)) ? \
3352a6dda0e6SChristoph Hellwig 	 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
3353a6dda0e6SChristoph Hellwig 
33547a2af766SChao Yu #define F2FS_TOTAL_EXTRA_ATTR_SIZE			\
33557a2af766SChao Yu 	(offsetof(struct f2fs_inode, i_extra_end) -	\
33567a2af766SChao Yu 	offsetof(struct f2fs_inode, i_extra_isize))	\
33577a2af766SChao Yu 
33585c57132eSChao Yu #define F2FS_OLD_ATTRIBUTE_SIZE	(offsetof(struct f2fs_inode, i_addr))
33595c57132eSChao Yu #define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field)		\
33602f84babfSSheng Yong 		((offsetof(typeof(*(f2fs_inode)), field) +	\
33615c57132eSChao Yu 		sizeof((f2fs_inode)->field))			\
33622f84babfSSheng Yong 		<= (F2FS_OLD_ATTRIBUTE_SIZE + (extra_isize)))	\
33635c57132eSChao Yu 
33642c70c5e3SChao Yu #define __is_large_section(sbi)		((sbi)->segs_per_sec > 1)
33652c70c5e3SChao Yu 
33666dc3a126SChao Yu #define __is_meta_io(fio) (PAGE_TYPE_OF_BIO((fio)->type) == META)
3367c9b60788SChao Yu 
3368e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
3369e1da7872SChao Yu 					block_t blkaddr, int type);
3370e1da7872SChao Yu static inline void verify_blkaddr(struct f2fs_sb_info *sbi,
3371e1da7872SChao Yu 					block_t blkaddr, int type)
3372e1da7872SChao Yu {
3373e1da7872SChao Yu 	if (!f2fs_is_valid_blkaddr(sbi, blkaddr, type)) {
3374dcbb4c10SJoe Perches 		f2fs_err(sbi, "invalid blkaddr: %u, type: %d, run fsck to fix.",
3375e1da7872SChao Yu 			 blkaddr, type);
3376e1da7872SChao Yu 		f2fs_bug_on(sbi, 1);
3377e1da7872SChao Yu 	}
3378e1da7872SChao Yu }
3379e1da7872SChao Yu 
3380e1da7872SChao Yu static inline bool __is_valid_data_blkaddr(block_t blkaddr)
33817b525dd0SChao Yu {
33824c8ff709SChao Yu 	if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR ||
33834c8ff709SChao Yu 			blkaddr == COMPRESS_ADDR)
33847b525dd0SChao Yu 		return false;
33857b525dd0SChao Yu 	return true;
33867b525dd0SChao Yu }
33877b525dd0SChao Yu 
338839a53e0cSJaegeuk Kim /*
338939a53e0cSJaegeuk Kim  * file.c
339039a53e0cSJaegeuk Kim  */
3391cac5a3d8SDongOh Shin int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
33924d57b86dSChao Yu void f2fs_truncate_data_blocks(struct dnode_of_data *dn);
33933265d3dbSChao Yu int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock);
3394c42d28ceSChao Yu int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock);
3395cac5a3d8SDongOh Shin int f2fs_truncate(struct inode *inode);
3396549c7297SChristian Brauner int f2fs_getattr(struct user_namespace *mnt_userns, const struct path *path,
3397549c7297SChristian Brauner 		 struct kstat *stat, u32 request_mask, unsigned int flags);
3398549c7297SChristian Brauner int f2fs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
3399549c7297SChristian Brauner 		 struct iattr *attr);
34004d57b86dSChao Yu int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
34014d57b86dSChao Yu void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count);
3402c4020b2dSChao Yu int f2fs_precache_extents(struct inode *inode);
34039b1bb01cSMiklos Szeredi int f2fs_fileattr_get(struct dentry *dentry, struct fileattr *fa);
34049b1bb01cSMiklos Szeredi int f2fs_fileattr_set(struct user_namespace *mnt_userns,
34059b1bb01cSMiklos Szeredi 		      struct dentry *dentry, struct fileattr *fa);
3406cac5a3d8SDongOh Shin long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
3407cac5a3d8SDongOh Shin long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
340878130819SChao Yu int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid);
34091ad71a27SJaegeuk Kim int f2fs_pin_file_control(struct inode *inode, bool inc);
341039a53e0cSJaegeuk Kim 
341139a53e0cSJaegeuk Kim /*
341239a53e0cSJaegeuk Kim  * inode.c
341339a53e0cSJaegeuk Kim  */
3414cac5a3d8SDongOh Shin void f2fs_set_inode_flags(struct inode *inode);
3415704956ecSChao Yu bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page);
3416704956ecSChao Yu void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page);
3417cac5a3d8SDongOh Shin struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
3418cac5a3d8SDongOh Shin struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
34194d57b86dSChao Yu int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
34204d57b86dSChao Yu void f2fs_update_inode(struct inode *inode, struct page *node_page);
34214d57b86dSChao Yu void f2fs_update_inode_page(struct inode *inode);
3422cac5a3d8SDongOh Shin int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
3423cac5a3d8SDongOh Shin void f2fs_evict_inode(struct inode *inode);
34244d57b86dSChao Yu void f2fs_handle_failed_inode(struct inode *inode);
342539a53e0cSJaegeuk Kim 
342639a53e0cSJaegeuk Kim /*
342739a53e0cSJaegeuk Kim  * namei.c
342839a53e0cSJaegeuk Kim  */
34294d57b86dSChao Yu int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
3430b6a06cbbSChao Yu 							bool hot, bool set);
343139a53e0cSJaegeuk Kim struct dentry *f2fs_get_parent(struct dentry *child);
34323db1de0eSDaeho Jeong int f2fs_get_tmpfile(struct user_namespace *mnt_userns, struct inode *dir,
34333db1de0eSDaeho Jeong 		     struct inode **new_inode);
343439a53e0cSJaegeuk Kim 
343539a53e0cSJaegeuk Kim /*
343639a53e0cSJaegeuk Kim  * dir.c
343739a53e0cSJaegeuk Kim  */
34384d57b86dSChao Yu unsigned char f2fs_get_de_type(struct f2fs_dir_entry *de);
343943c780baSEric Biggers int f2fs_init_casefolded_name(const struct inode *dir,
344043c780baSEric Biggers 			      struct f2fs_filename *fname);
344143c780baSEric Biggers int f2fs_setup_filename(struct inode *dir, const struct qstr *iname,
344243c780baSEric Biggers 			int lookup, struct f2fs_filename *fname);
344343c780baSEric Biggers int f2fs_prepare_lookup(struct inode *dir, struct dentry *dentry,
344443c780baSEric Biggers 			struct f2fs_filename *fname);
344543c780baSEric Biggers void f2fs_free_filename(struct f2fs_filename *fname);
344643c780baSEric Biggers struct f2fs_dir_entry *f2fs_find_target_dentry(const struct f2fs_dentry_ptr *d,
344743c780baSEric Biggers 			const struct f2fs_filename *fname, int *max_slots);
3448cac5a3d8SDongOh Shin int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
3449cac5a3d8SDongOh Shin 			unsigned int start_pos, struct fscrypt_str *fstr);
34504d57b86dSChao Yu void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
3451cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
34524d57b86dSChao Yu struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
345343c780baSEric Biggers 			const struct f2fs_filename *fname, struct page *dpage);
34544d57b86dSChao Yu void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
3455cac5a3d8SDongOh Shin 			unsigned int current_depth);
34564d57b86dSChao Yu int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots);
3457cac5a3d8SDongOh Shin void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
3458cac5a3d8SDongOh Shin struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
345943c780baSEric Biggers 					 const struct f2fs_filename *fname,
346043c780baSEric Biggers 					 struct page **res_page);
3461cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
3462cac5a3d8SDongOh Shin 			const struct qstr *child, struct page **res_page);
3463cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
3464cac5a3d8SDongOh Shin ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
3465cac5a3d8SDongOh Shin 			struct page **page);
3466cac5a3d8SDongOh Shin void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
3467cac5a3d8SDongOh Shin 			struct page *page, struct inode *inode);
3468b06af2afSJaegeuk Kim bool f2fs_has_enough_room(struct inode *dir, struct page *ipage,
346943c780baSEric Biggers 			  const struct f2fs_filename *fname);
3470cac5a3d8SDongOh Shin void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
347143c780baSEric Biggers 			const struct fscrypt_str *name, f2fs_hash_t name_hash,
3472cac5a3d8SDongOh Shin 			unsigned int bit_pos);
347343c780baSEric Biggers int f2fs_add_regular_entry(struct inode *dir, const struct f2fs_filename *fname,
3474cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
347543c780baSEric Biggers int f2fs_add_dentry(struct inode *dir, const struct f2fs_filename *fname,
3476cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
34774d57b86dSChao Yu int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
3478cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
3479cac5a3d8SDongOh Shin void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
3480cac5a3d8SDongOh Shin 			struct inode *dir, struct inode *inode);
3481cac5a3d8SDongOh Shin int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
3482cac5a3d8SDongOh Shin bool f2fs_empty_dir(struct inode *dir);
348339a53e0cSJaegeuk Kim 
3484b7f7a5e0SAl Viro static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
3485b7f7a5e0SAl Viro {
3486bfc2b7e8SEric Biggers 	if (fscrypt_is_nokey_name(dentry))
3487bfc2b7e8SEric Biggers 		return -ENOKEY;
34884d57b86dSChao Yu 	return f2fs_do_add_link(d_inode(dentry->d_parent), &dentry->d_name,
3489510022a8SJaegeuk Kim 				inode, inode->i_ino, inode->i_mode);
3490b7f7a5e0SAl Viro }
3491b7f7a5e0SAl Viro 
349239a53e0cSJaegeuk Kim /*
349339a53e0cSJaegeuk Kim  * super.c
349439a53e0cSJaegeuk Kim  */
3495cac5a3d8SDongOh Shin int f2fs_inode_dirtied(struct inode *inode, bool sync);
3496cac5a3d8SDongOh Shin void f2fs_inode_synced(struct inode *inode);
349710a26878SChao Yu int f2fs_dquot_initialize(struct inode *inode);
3498ea676733SJaegeuk Kim int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly);
3499af033b2aSChao Yu int f2fs_quota_sync(struct super_block *sb, int type);
35006d1451bfSChengguang Xu loff_t max_file_blocks(struct inode *inode);
35014b2414d0SChao Yu void f2fs_quota_off_umount(struct super_block *sb);
3502cac5a3d8SDongOh Shin int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
3503cac5a3d8SDongOh Shin int f2fs_sync_fs(struct super_block *sb, int sync);
35044d57b86dSChao Yu int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi);
350539a53e0cSJaegeuk Kim 
350639a53e0cSJaegeuk Kim /*
350739a53e0cSJaegeuk Kim  * hash.c
350839a53e0cSJaegeuk Kim  */
350943c780baSEric Biggers void f2fs_hash_filename(const struct inode *dir, struct f2fs_filename *fname);
351039a53e0cSJaegeuk Kim 
351139a53e0cSJaegeuk Kim /*
351239a53e0cSJaegeuk Kim  * node.c
351339a53e0cSJaegeuk Kim  */
351439a53e0cSJaegeuk Kim struct node_info;
351539a53e0cSJaegeuk Kim 
35164d57b86dSChao Yu int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid);
35174d57b86dSChao Yu bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type);
351850fa53ecSChao Yu bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct page *page);
351950fa53ecSChao Yu void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi);
352050fa53ecSChao Yu void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct page *page);
352150fa53ecSChao Yu void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi);
35224d57b86dSChao Yu int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
35234d57b86dSChao Yu bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
35244d57b86dSChao Yu bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
35257735730dSChao Yu int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
3526a9419b63SJaegeuk Kim 				struct node_info *ni, bool checkpoint_context);
35274d57b86dSChao Yu pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
35284d57b86dSChao Yu int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
35294d57b86dSChao Yu int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from);
35304d57b86dSChao Yu int f2fs_truncate_xattr_node(struct inode *inode);
353150fa53ecSChao Yu int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
353250fa53ecSChao Yu 					unsigned int seq_id);
353394c821fbSChao Yu bool f2fs_nat_bitmap_enabled(struct f2fs_sb_info *sbi);
35344d57b86dSChao Yu int f2fs_remove_inode_page(struct inode *inode);
35354d57b86dSChao Yu struct page *f2fs_new_inode_page(struct inode *inode);
35364d57b86dSChao Yu struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs);
35374d57b86dSChao Yu void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
35384d57b86dSChao Yu struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
35394d57b86dSChao Yu struct page *f2fs_get_node_page_ra(struct page *parent, int start);
354048018b4cSChao Yu int f2fs_move_node_page(struct page *node_page, int gc_type);
354168e79bafSJia Yang void f2fs_flush_inline_data(struct f2fs_sb_info *sbi);
35424d57b86dSChao Yu int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
354350fa53ecSChao Yu 			struct writeback_control *wbc, bool atomic,
354450fa53ecSChao Yu 			unsigned int *seq_id);
35454d57b86dSChao Yu int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
35464d57b86dSChao Yu 			struct writeback_control *wbc,
3547b0af6d49SChao Yu 			bool do_balance, enum iostat_type io_type);
3548e2374015SChao Yu int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
35494d57b86dSChao Yu bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
35504d57b86dSChao Yu void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
35514d57b86dSChao Yu void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
35524d57b86dSChao Yu int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
35539627a7b3SChao Yu int f2fs_recover_inline_xattr(struct inode *inode, struct page *page);
35544d57b86dSChao Yu int f2fs_recover_xattr_data(struct inode *inode, struct page *page);
35554d57b86dSChao Yu int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
35567735730dSChao Yu int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
3557cac5a3d8SDongOh Shin 			unsigned int segno, struct f2fs_summary_block *sum);
355894c821fbSChao Yu void f2fs_enable_nat_bits(struct f2fs_sb_info *sbi);
3559edc55aafSJaegeuk Kim int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
35604d57b86dSChao Yu int f2fs_build_node_manager(struct f2fs_sb_info *sbi);
35614d57b86dSChao Yu void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi);
35624d57b86dSChao Yu int __init f2fs_create_node_manager_caches(void);
35634d57b86dSChao Yu void f2fs_destroy_node_manager_caches(void);
356439a53e0cSJaegeuk Kim 
356539a53e0cSJaegeuk Kim /*
356639a53e0cSJaegeuk Kim  * segment.c
356739a53e0cSJaegeuk Kim  */
35684d57b86dSChao Yu bool f2fs_need_SSR(struct f2fs_sb_info *sbi);
35693db1de0eSDaeho Jeong int f2fs_commit_atomic_write(struct inode *inode);
35703db1de0eSDaeho Jeong void f2fs_abort_atomic_write(struct inode *inode, bool clean);
3571cac5a3d8SDongOh Shin void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
35727bcd0cfaSChao Yu void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi, bool from_bg);
357339d787beSChao Yu int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
35744d57b86dSChao Yu int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
35751228b482SChao Yu int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
35764d57b86dSChao Yu void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
35774d57b86dSChao Yu void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
35784d57b86dSChao Yu bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
35794d674904SFengnan Chang int f2fs_start_discard_thread(struct f2fs_sb_info *sbi);
35804d57b86dSChao Yu void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
35814d57b86dSChao Yu void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi);
358203f2c02dSJaegeuk Kim bool f2fs_issue_discard_timeout(struct f2fs_sb_info *sbi);
35834d57b86dSChao Yu void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
35844d57b86dSChao Yu 					struct cp_control *cpc);
35854354994fSDaniel Rosenberg void f2fs_dirty_to_prefree(struct f2fs_sb_info *sbi);
35864d3aed70SDaniel Rosenberg block_t f2fs_get_unusable_blocks(struct f2fs_sb_info *sbi);
35874d3aed70SDaniel Rosenberg int f2fs_disable_cp_again(struct f2fs_sb_info *sbi, block_t unusable);
35884d57b86dSChao Yu void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi);
35894d57b86dSChao Yu int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
359061461fc9SChao Yu bool f2fs_segment_has_free_slot(struct f2fs_sb_info *sbi, int segno);
3591093749e2SChao Yu void f2fs_init_inmem_curseg(struct f2fs_sb_info *sbi);
3592093749e2SChao Yu void f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi);
3593093749e2SChao Yu void f2fs_restore_inmem_curseg(struct f2fs_sb_info *sbi);
3594093749e2SChao Yu void f2fs_get_new_segment(struct f2fs_sb_info *sbi,
3595093749e2SChao Yu 			unsigned int *newseg, bool new_sec, int dir);
35960ef81833SChao Yu void f2fs_allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type,
359704f0b2eaSQiuyang Sun 					unsigned int start, unsigned int end);
3598509f1010SChao Yu void f2fs_allocate_new_section(struct f2fs_sb_info *sbi, int type, bool force);
3599901d745fSChao Yu void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi);
3600cac5a3d8SDongOh Shin int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
36014d57b86dSChao Yu bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
36024d57b86dSChao Yu 					struct cp_control *cpc);
36034d57b86dSChao Yu struct page *f2fs_get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
36044d57b86dSChao Yu void f2fs_update_meta_page(struct f2fs_sb_info *sbi, void *src,
36054d57b86dSChao Yu 					block_t blk_addr);
36064d57b86dSChao Yu void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
3607b0af6d49SChao Yu 						enum iostat_type io_type);
36084d57b86dSChao Yu void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio);
36094d57b86dSChao Yu void f2fs_outplace_write_data(struct dnode_of_data *dn,
36104d57b86dSChao Yu 			struct f2fs_io_info *fio);
36114d57b86dSChao Yu int f2fs_inplace_write_data(struct f2fs_io_info *fio);
36124d57b86dSChao Yu void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
3613cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t new_blkaddr,
3614c5d02785SChao Yu 			bool recover_curseg, bool recover_newaddr,
3615c5d02785SChao Yu 			bool from_gc);
3616cac5a3d8SDongOh Shin void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
3617cac5a3d8SDongOh Shin 			block_t old_addr, block_t new_addr,
3618cac5a3d8SDongOh Shin 			unsigned char version, bool recover_curseg,
3619cac5a3d8SDongOh Shin 			bool recover_newaddr);
36204d57b86dSChao Yu void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
3621cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t *new_blkaddr,
3622fb830fc5SChao Yu 			struct f2fs_summary *sum, int type,
3623f608c38cSChao Yu 			struct f2fs_io_info *fio);
362471f2c820SChao Yu void f2fs_update_device_state(struct f2fs_sb_info *sbi, nid_t ino,
362571f2c820SChao Yu 					block_t blkaddr, unsigned int blkcnt);
3626cac5a3d8SDongOh Shin void f2fs_wait_on_page_writeback(struct page *page,
3627bae0ee7aSChao Yu 			enum page_type type, bool ordered, bool locked);
36280ded69f6SJaegeuk Kim void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr);
36291e78e8bdSSahitya Tummala void f2fs_wait_on_block_writeback_range(struct inode *inode, block_t blkaddr,
36301e78e8bdSSahitya Tummala 								block_t len);
36314d57b86dSChao Yu void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
36324d57b86dSChao Yu void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
36334d57b86dSChao Yu int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
3634cac5a3d8SDongOh Shin 			unsigned int val, int alloc);
36354d57b86dSChao Yu void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
3636c426d991SShin'ichiro Kawasaki int f2fs_fix_curseg_write_pointer(struct f2fs_sb_info *sbi);
3637d508c94eSShin'ichiro Kawasaki int f2fs_check_write_pointer(struct f2fs_sb_info *sbi);
36384d57b86dSChao Yu int f2fs_build_segment_manager(struct f2fs_sb_info *sbi);
36394d57b86dSChao Yu void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi);
36404d57b86dSChao Yu int __init f2fs_create_segment_manager_caches(void);
36414d57b86dSChao Yu void f2fs_destroy_segment_manager_caches(void);
36424d57b86dSChao Yu int f2fs_rw_hint_to_seg_type(enum rw_hint hint);
3643de881df9SAravind Ramesh unsigned int f2fs_usable_segs_in_sec(struct f2fs_sb_info *sbi,
3644de881df9SAravind Ramesh 			unsigned int segno);
3645de881df9SAravind Ramesh unsigned int f2fs_usable_blks_in_seg(struct f2fs_sb_info *sbi,
3646de881df9SAravind Ramesh 			unsigned int segno);
364739a53e0cSJaegeuk Kim 
36486691d940SDaeho Jeong #define DEF_FRAGMENT_SIZE	4
36496691d940SDaeho Jeong #define MIN_FRAGMENT_SIZE	1
36506691d940SDaeho Jeong #define MAX_FRAGMENT_SIZE	512
36516691d940SDaeho Jeong 
36526691d940SDaeho Jeong static inline bool f2fs_need_rand_seg(struct f2fs_sb_info *sbi)
36536691d940SDaeho Jeong {
36546691d940SDaeho Jeong 	return F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_SEG ||
36556691d940SDaeho Jeong 		F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK;
36566691d940SDaeho Jeong }
36576691d940SDaeho Jeong 
365839a53e0cSJaegeuk Kim /*
365939a53e0cSJaegeuk Kim  * checkpoint.c
366039a53e0cSJaegeuk Kim  */
3661cac5a3d8SDongOh Shin void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io);
36624d57b86dSChao Yu struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
36634d57b86dSChao Yu struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
366486f33603SJaegeuk Kim struct page *f2fs_get_meta_page_retry(struct f2fs_sb_info *sbi, pgoff_t index);
36654d57b86dSChao Yu struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
3666e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
36674d57b86dSChao Yu 					block_t blkaddr, int type);
36684d57b86dSChao Yu int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
3669cac5a3d8SDongOh Shin 			int type, bool sync);
3670430f163bSChao Yu void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index,
3671430f163bSChao Yu 							unsigned int ra_blocks);
36724d57b86dSChao Yu long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
3673b0af6d49SChao Yu 			long nr_to_write, enum iostat_type io_type);
36744d57b86dSChao Yu void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
36754d57b86dSChao Yu void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
36764d57b86dSChao Yu void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all);
36774d57b86dSChao Yu bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
36784d57b86dSChao Yu void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
367939d787beSChao Yu 					unsigned int devidx, int type);
36804d57b86dSChao Yu bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
368139d787beSChao Yu 					unsigned int devidx, int type);
3682cac5a3d8SDongOh Shin int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
36834d57b86dSChao Yu int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi);
36844d57b86dSChao Yu void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi);
36854d57b86dSChao Yu void f2fs_add_orphan_inode(struct inode *inode);
36864d57b86dSChao Yu void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
36874d57b86dSChao Yu int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi);
36884d57b86dSChao Yu int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi);
36894f5e34f7SMatthew Wilcox (Oracle) void f2fs_update_dirty_folio(struct inode *inode, struct folio *folio);
36904d57b86dSChao Yu void f2fs_remove_dirty_inode(struct inode *inode);
36914d57b86dSChao Yu int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
3692bf22c3ccSSahitya Tummala void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type);
36933a0a9cbcSChao Yu u64 f2fs_get_sectors_written(struct f2fs_sb_info *sbi);
36944d57b86dSChao Yu int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
36954d57b86dSChao Yu void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi);
36964d57b86dSChao Yu int __init f2fs_create_checkpoint_caches(void);
36974d57b86dSChao Yu void f2fs_destroy_checkpoint_caches(void);
3698261eeb9cSDaeho Jeong int f2fs_issue_checkpoint(struct f2fs_sb_info *sbi);
3699261eeb9cSDaeho Jeong int f2fs_start_ckpt_thread(struct f2fs_sb_info *sbi);
3700261eeb9cSDaeho Jeong void f2fs_stop_ckpt_thread(struct f2fs_sb_info *sbi);
3701261eeb9cSDaeho Jeong void f2fs_init_ckpt_req_control(struct f2fs_sb_info *sbi);
370239a53e0cSJaegeuk Kim 
370339a53e0cSJaegeuk Kim /*
370439a53e0cSJaegeuk Kim  * data.c
370539a53e0cSJaegeuk Kim  */
3706f543805fSChao Yu int __init f2fs_init_bioset(void);
3707f543805fSChao Yu void f2fs_destroy_bioset(void);
37080b20fcecSChao Yu int f2fs_init_bio_entry_cache(void);
37090b20fcecSChao Yu void f2fs_destroy_bio_entry_cache(void);
37104c8ff709SChao Yu void f2fs_submit_bio(struct f2fs_sb_info *sbi,
37114c8ff709SChao Yu 				struct bio *bio, enum page_type type);
3712b9109b0eSJaegeuk Kim void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
3713b9109b0eSJaegeuk Kim void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
3714bab475c5SChao Yu 				struct inode *inode, struct page *page,
3715bab475c5SChao Yu 				nid_t ino, enum page_type type);
37160b20fcecSChao Yu void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
37170b20fcecSChao Yu 					struct bio **bio, struct page *page);
3718b9109b0eSJaegeuk Kim void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
3719cac5a3d8SDongOh Shin int f2fs_submit_page_bio(struct f2fs_io_info *fio);
37208648de2cSChao Yu int f2fs_merge_page_bio(struct f2fs_io_info *fio);
3721fe16efe6SChao Yu void f2fs_submit_page_write(struct f2fs_io_info *fio);
3722cac5a3d8SDongOh Shin struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
37235189810aSChristoph Hellwig 		block_t blk_addr, sector_t *sector);
3724cac5a3d8SDongOh Shin int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
37254d57b86dSChao Yu void f2fs_set_data_blkaddr(struct dnode_of_data *dn);
3726cac5a3d8SDongOh Shin void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
37274d57b86dSChao Yu int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
37284d57b86dSChao Yu int f2fs_reserve_new_block(struct dnode_of_data *dn);
3729cac5a3d8SDongOh Shin int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
3730cac5a3d8SDongOh Shin int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
37314d57b86dSChao Yu struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
3732cac5a3d8SDongOh Shin 			int op_flags, bool for_write);
37334d57b86dSChao Yu struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index);
37344d57b86dSChao Yu struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
3735cac5a3d8SDongOh Shin 			bool for_write);
37364d57b86dSChao Yu struct page *f2fs_get_new_data_page(struct inode *inode,
3737cac5a3d8SDongOh Shin 			struct page *ipage, pgoff_t index, bool new_i_size);
37384d57b86dSChao Yu int f2fs_do_write_data_page(struct f2fs_io_info *fio);
37390ef81833SChao Yu void f2fs_do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock);
3740cac5a3d8SDongOh Shin int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
3741cac5a3d8SDongOh Shin 			int create, int flag);
3742cac5a3d8SDongOh Shin int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3743cac5a3d8SDongOh Shin 			u64 start, u64 len);
37444c8ff709SChao Yu int f2fs_encrypt_one_page(struct f2fs_io_info *fio);
37454d57b86dSChao Yu bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio);
37464d57b86dSChao Yu bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio);
37474c8ff709SChao Yu int f2fs_write_single_data_page(struct page *page, int *submitted,
37484c8ff709SChao Yu 				struct bio **bio, sector_t *last_block,
37494c8ff709SChao Yu 				struct writeback_control *wbc,
37504c8ff709SChao Yu 				enum iostat_type io_type,
37513afae09fSChao Yu 				int compr_blocks, bool allow_balance);
3752a1e09b03SEric Biggers void f2fs_write_failed(struct inode *inode, loff_t to);
375391503996SMatthew Wilcox (Oracle) void f2fs_invalidate_folio(struct folio *folio, size_t offset, size_t length);
3754cac5a3d8SDongOh Shin int f2fs_release_page(struct page *page, gfp_t wait);
37555b7a487cSWeichao Guo #ifdef CONFIG_MIGRATION
3756cac5a3d8SDongOh Shin int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,
3757cac5a3d8SDongOh Shin 			struct page *page, enum migrate_mode mode);
37585b7a487cSWeichao Guo #endif
3759b91050a8SHyunchul Lee bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len);
37605ec2d99dSMatthew Wilcox void f2fs_clear_page_cache_dirty_tag(struct page *page);
37614c8ff709SChao Yu int f2fs_init_post_read_processing(void);
37624c8ff709SChao Yu void f2fs_destroy_post_read_processing(void);
37634c8ff709SChao Yu int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi);
37644c8ff709SChao Yu void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi);
37651517c1a7SEric Biggers extern const struct iomap_ops f2fs_iomap_ops;
376639a53e0cSJaegeuk Kim 
376739a53e0cSJaegeuk Kim /*
376839a53e0cSJaegeuk Kim  * gc.c
376939a53e0cSJaegeuk Kim  */
37704d57b86dSChao Yu int f2fs_start_gc_thread(struct f2fs_sb_info *sbi);
37714d57b86dSChao Yu void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi);
37724d57b86dSChao Yu block_t f2fs_start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
3773d147ea4aSJaegeuk Kim int f2fs_gc(struct f2fs_sb_info *sbi, struct f2fs_gc_control *gc_control);
37744d57b86dSChao Yu void f2fs_build_gc_manager(struct f2fs_sb_info *sbi);
377504f0b2eaSQiuyang Sun int f2fs_resize_fs(struct f2fs_sb_info *sbi, __u64 block_count);
3776093749e2SChao Yu int __init f2fs_create_garbage_collection_cache(void);
3777093749e2SChao Yu void f2fs_destroy_garbage_collection_cache(void);
377839a53e0cSJaegeuk Kim 
377939a53e0cSJaegeuk Kim /*
378039a53e0cSJaegeuk Kim  * recovery.c
378139a53e0cSJaegeuk Kim  */
37824d57b86dSChao Yu int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
37834d57b86dSChao Yu bool f2fs_space_for_roll_forward(struct f2fs_sb_info *sbi);
3784cad83c96SChao Yu int __init f2fs_create_recovery_cache(void);
3785cad83c96SChao Yu void f2fs_destroy_recovery_cache(void);
378639a53e0cSJaegeuk Kim 
378739a53e0cSJaegeuk Kim /*
378839a53e0cSJaegeuk Kim  * debug.c
378939a53e0cSJaegeuk Kim  */
379039a53e0cSJaegeuk Kim #ifdef CONFIG_F2FS_STAT_FS
379139a53e0cSJaegeuk Kim struct f2fs_stat_info {
379239a53e0cSJaegeuk Kim 	struct list_head stat_list;
379339a53e0cSJaegeuk Kim 	struct f2fs_sb_info *sbi;
379439a53e0cSJaegeuk Kim 	int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
379539a53e0cSJaegeuk Kim 	int main_area_segs, main_area_sections, main_area_zones;
37965b7ee374SChao Yu 	unsigned long long hit_largest, hit_cached, hit_rbtree;
37975b7ee374SChao Yu 	unsigned long long hit_total, total_ext;
3798c00ba554SJaegeuk Kim 	int ext_tree, zombie_tree, ext_node;
37992c8a4a28SJaegeuk Kim 	int ndirty_node, ndirty_dent, ndirty_meta, ndirty_imeta;
38002c8a4a28SJaegeuk Kim 	int ndirty_data, ndirty_qdata;
38012c8a4a28SJaegeuk Kim 	unsigned int ndirty_dirs, ndirty_files, nquota_files, ndirty_all;
38025b0ef73cSJaegeuk Kim 	int nats, dirty_nats, sits, dirty_sits;
38035b0ef73cSJaegeuk Kim 	int free_nids, avail_nids, alloc_nids;
380439a53e0cSJaegeuk Kim 	int total_count, utilization;
38058b8dd65fSChao Yu 	int bg_gc, nr_wb_cp_data, nr_wb_data;
38065f9abab4SJaegeuk Kim 	int nr_rd_data, nr_rd_node, nr_rd_meta;
380702b16d0aSChao Yu 	int nr_dio_read, nr_dio_write;
3808274bd9baSChao Yu 	unsigned int io_skip_bggc, other_skip_bggc;
380914d8d5f7SChao Yu 	int nr_flushing, nr_flushed, flush_list_empty;
381014d8d5f7SChao Yu 	int nr_discarding, nr_discarded;
38115f32366aSChao Yu 	int nr_discard_cmd;
3812d84d1cbdSChao Yu 	unsigned int undiscard_blks;
3813261eeb9cSDaeho Jeong 	int nr_issued_ckpt, nr_total_ckpt, nr_queued_ckpt;
3814261eeb9cSDaeho Jeong 	unsigned int cur_ckpt_time, peak_ckpt_time;
3815a00861dbSJaegeuk Kim 	int inline_xattr, inline_inode, inline_dir, append, update, orphans;
3816ae999bb9SDaeho Jeong 	int compr_inode;
3817ae999bb9SDaeho Jeong 	unsigned long long compr_blocks;
38187bc155feSJaegeuk Kim 	int aw_cnt, max_aw_cnt;
3819f83a2584SYunlei He 	unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
382039a53e0cSJaegeuk Kim 	unsigned int bimodal, avg_vblocks;
382139a53e0cSJaegeuk Kim 	int util_free, util_valid, util_invalid;
382239a53e0cSJaegeuk Kim 	int rsvd_segs, overp_segs;
38236ce19affSChao Yu 	int dirty_count, node_pages, meta_pages, compress_pages;
38246ce19affSChao Yu 	int compress_page_hit;
382542190d2aSJaegeuk Kim 	int prefree_count, call_count, cp_count, bg_cp_count;
382639a53e0cSJaegeuk Kim 	int tot_segs, node_segs, data_segs, free_segs, free_secs;
3827e1235983SChangman Lee 	int bg_node_segs, bg_data_segs;
382839a53e0cSJaegeuk Kim 	int tot_blks, data_blks, node_blks;
3829e1235983SChangman Lee 	int bg_data_blks, bg_node_blks;
383039a53e0cSJaegeuk Kim 	int curseg[NR_CURSEG_TYPE];
383139a53e0cSJaegeuk Kim 	int cursec[NR_CURSEG_TYPE];
383239a53e0cSJaegeuk Kim 	int curzone[NR_CURSEG_TYPE];
38330759e2c1SChao Yu 	unsigned int dirty_seg[NR_CURSEG_TYPE];
38340759e2c1SChao Yu 	unsigned int full_seg[NR_CURSEG_TYPE];
38350759e2c1SChao Yu 	unsigned int valid_blks[NR_CURSEG_TYPE];
383639a53e0cSJaegeuk Kim 
3837b63e7be5SChao Yu 	unsigned int meta_count[META_MAX];
383839a53e0cSJaegeuk Kim 	unsigned int segment_count[2];
383939a53e0cSJaegeuk Kim 	unsigned int block_count[2];
3840b9a2c252SChangman Lee 	unsigned int inplace_count;
38419edcdabfSChao Yu 	unsigned long long base_mem, cache_mem, page_mem;
384239a53e0cSJaegeuk Kim };
384339a53e0cSJaegeuk Kim 
3844963d4f7dSGu Zheng static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
3845963d4f7dSGu Zheng {
3846963d4f7dSGu Zheng 	return (struct f2fs_stat_info *)sbi->stat_info;
3847963d4f7dSGu Zheng }
3848963d4f7dSGu Zheng 
3849942e0be6SChangman Lee #define stat_inc_cp_count(si)		((si)->cp_count++)
385042190d2aSJaegeuk Kim #define stat_inc_bg_cp_count(si)	((si)->bg_cp_count++)
385139a53e0cSJaegeuk Kim #define stat_inc_call_count(si)		((si)->call_count++)
3852fc7100eaSHridya Valsaraju #define stat_inc_bggc_count(si)		((si)->bg_gc++)
3853274bd9baSChao Yu #define stat_io_skip_bggc_count(sbi)	((sbi)->io_skip_bggc++)
3854274bd9baSChao Yu #define stat_other_skip_bggc_count(sbi)	((sbi)->other_skip_bggc++)
385533fbd510SChao Yu #define stat_inc_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]++)
385633fbd510SChao Yu #define stat_dec_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]--)
38575b7ee374SChao Yu #define stat_inc_total_hit(sbi)		(atomic64_inc(&(sbi)->total_hit_ext))
38585b7ee374SChao Yu #define stat_inc_rbtree_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_rbtree))
38595b7ee374SChao Yu #define stat_inc_largest_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_largest))
38605b7ee374SChao Yu #define stat_inc_cached_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_cached))
3861d5e8f6c9SChao Yu #define stat_inc_inline_xattr(inode)					\
3862d5e8f6c9SChao Yu 	do {								\
3863d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3864d5e8f6c9SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_xattr));	\
3865d5e8f6c9SChao Yu 	} while (0)
3866d5e8f6c9SChao Yu #define stat_dec_inline_xattr(inode)					\
3867d5e8f6c9SChao Yu 	do {								\
3868d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3869d5e8f6c9SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_xattr));	\
3870d5e8f6c9SChao Yu 	} while (0)
38710dbdc2aeSJaegeuk Kim #define stat_inc_inline_inode(inode)					\
38720dbdc2aeSJaegeuk Kim 	do {								\
38730dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
387403e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_inode));	\
38750dbdc2aeSJaegeuk Kim 	} while (0)
38760dbdc2aeSJaegeuk Kim #define stat_dec_inline_inode(inode)					\
38770dbdc2aeSJaegeuk Kim 	do {								\
38780dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
387903e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_inode));	\
38800dbdc2aeSJaegeuk Kim 	} while (0)
38813289c061SJaegeuk Kim #define stat_inc_inline_dir(inode)					\
38823289c061SJaegeuk Kim 	do {								\
38833289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
388403e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_dir));	\
38853289c061SJaegeuk Kim 	} while (0)
38863289c061SJaegeuk Kim #define stat_dec_inline_dir(inode)					\
38873289c061SJaegeuk Kim 	do {								\
38883289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
388903e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_dir));	\
38903289c061SJaegeuk Kim 	} while (0)
38914c8ff709SChao Yu #define stat_inc_compr_inode(inode)					\
38924c8ff709SChao Yu 	do {								\
38934c8ff709SChao Yu 		if (f2fs_compressed_file(inode))			\
38944c8ff709SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->compr_inode));	\
38954c8ff709SChao Yu 	} while (0)
38964c8ff709SChao Yu #define stat_dec_compr_inode(inode)					\
38974c8ff709SChao Yu 	do {								\
38984c8ff709SChao Yu 		if (f2fs_compressed_file(inode))			\
38994c8ff709SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->compr_inode));	\
39004c8ff709SChao Yu 	} while (0)
39014c8ff709SChao Yu #define stat_add_compr_blocks(inode, blocks)				\
3902ae999bb9SDaeho Jeong 		(atomic64_add(blocks, &F2FS_I_SB(inode)->compr_blocks))
39034c8ff709SChao Yu #define stat_sub_compr_blocks(inode, blocks)				\
3904ae999bb9SDaeho Jeong 		(atomic64_sub(blocks, &F2FS_I_SB(inode)->compr_blocks))
3905b63e7be5SChao Yu #define stat_inc_meta_count(sbi, blkaddr)				\
3906b63e7be5SChao Yu 	do {								\
3907b63e7be5SChao Yu 		if (blkaddr < SIT_I(sbi)->sit_base_addr)		\
3908b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_CP]);	\
3909b63e7be5SChao Yu 		else if (blkaddr < NM_I(sbi)->nat_blkaddr)		\
3910b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_SIT]);	\
3911b63e7be5SChao Yu 		else if (blkaddr < SM_I(sbi)->ssa_blkaddr)		\
3912b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_NAT]);	\
3913b63e7be5SChao Yu 		else if (blkaddr < SM_I(sbi)->main_blkaddr)		\
3914b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_SSA]);	\
3915b63e7be5SChao Yu 	} while (0)
3916dcdfff65SJaegeuk Kim #define stat_inc_seg_type(sbi, curseg)					\
3917dcdfff65SJaegeuk Kim 		((sbi)->segment_count[(curseg)->alloc_type]++)
3918dcdfff65SJaegeuk Kim #define stat_inc_block_count(sbi, curseg)				\
3919dcdfff65SJaegeuk Kim 		((sbi)->block_count[(curseg)->alloc_type]++)
3920b9a2c252SChangman Lee #define stat_inc_inplace_blocks(sbi)					\
3921b9a2c252SChangman Lee 		(atomic_inc(&(sbi)->inplace_count))
392226a28a0cSJaegeuk Kim #define stat_update_max_atomic_write(inode)				\
392326a28a0cSJaegeuk Kim 	do {								\
39240e6d0164SSahitya Tummala 		int cur = F2FS_I_SB(inode)->atomic_files;	\
392526a28a0cSJaegeuk Kim 		int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt);	\
392626a28a0cSJaegeuk Kim 		if (cur > max)						\
392726a28a0cSJaegeuk Kim 			atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur);	\
392826a28a0cSJaegeuk Kim 	} while (0)
3929e1235983SChangman Lee #define stat_inc_seg_count(sbi, type, gc_type)				\
393039a53e0cSJaegeuk Kim 	do {								\
3931963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
393268afcf2dSTomohiro Kusumi 		si->tot_segs++;						\
393368afcf2dSTomohiro Kusumi 		if ((type) == SUM_TYPE_DATA) {				\
393439a53e0cSJaegeuk Kim 			si->data_segs++;				\
3935e1235983SChangman Lee 			si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0;	\
3936e1235983SChangman Lee 		} else {						\
393739a53e0cSJaegeuk Kim 			si->node_segs++;				\
3938e1235983SChangman Lee 			si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0;	\
3939e1235983SChangman Lee 		}							\
394039a53e0cSJaegeuk Kim 	} while (0)
394139a53e0cSJaegeuk Kim 
394239a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks)				\
394368afcf2dSTomohiro Kusumi 	((si)->tot_blks += (blks))
394439a53e0cSJaegeuk Kim 
3945e1235983SChangman Lee #define stat_inc_data_blk_count(sbi, blks, gc_type)			\
394639a53e0cSJaegeuk Kim 	do {								\
3947963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
394839a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
394939a53e0cSJaegeuk Kim 		si->data_blks += (blks);				\
395068afcf2dSTomohiro Kusumi 		si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
395139a53e0cSJaegeuk Kim 	} while (0)
395239a53e0cSJaegeuk Kim 
3953e1235983SChangman Lee #define stat_inc_node_blk_count(sbi, blks, gc_type)			\
395439a53e0cSJaegeuk Kim 	do {								\
3955963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
395639a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
395739a53e0cSJaegeuk Kim 		si->node_blks += (blks);				\
395868afcf2dSTomohiro Kusumi 		si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
395939a53e0cSJaegeuk Kim 	} while (0)
396039a53e0cSJaegeuk Kim 
3961cac5a3d8SDongOh Shin int f2fs_build_stats(struct f2fs_sb_info *sbi);
3962cac5a3d8SDongOh Shin void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
396321acc07dSGreg Kroah-Hartman void __init f2fs_create_root_stats(void);
39644589d25dSNamjae Jeon void f2fs_destroy_root_stats(void);
3965fc7100eaSHridya Valsaraju void f2fs_update_sit_info(struct f2fs_sb_info *sbi);
396639a53e0cSJaegeuk Kim #else
3967d66450e7SArnd Bergmann #define stat_inc_cp_count(si)				do { } while (0)
3968d66450e7SArnd Bergmann #define stat_inc_bg_cp_count(si)			do { } while (0)
3969d66450e7SArnd Bergmann #define stat_inc_call_count(si)				do { } while (0)
3970d66450e7SArnd Bergmann #define stat_inc_bggc_count(si)				do { } while (0)
3971274bd9baSChao Yu #define stat_io_skip_bggc_count(sbi)			do { } while (0)
3972274bd9baSChao Yu #define stat_other_skip_bggc_count(sbi)			do { } while (0)
3973d66450e7SArnd Bergmann #define stat_inc_dirty_inode(sbi, type)			do { } while (0)
3974d66450e7SArnd Bergmann #define stat_dec_dirty_inode(sbi, type)			do { } while (0)
3975fc7100eaSHridya Valsaraju #define stat_inc_total_hit(sbi)				do { } while (0)
3976fc7100eaSHridya Valsaraju #define stat_inc_rbtree_node_hit(sbi)			do { } while (0)
3977d66450e7SArnd Bergmann #define stat_inc_largest_node_hit(sbi)			do { } while (0)
3978d66450e7SArnd Bergmann #define stat_inc_cached_node_hit(sbi)			do { } while (0)
3979d66450e7SArnd Bergmann #define stat_inc_inline_xattr(inode)			do { } while (0)
3980d66450e7SArnd Bergmann #define stat_dec_inline_xattr(inode)			do { } while (0)
3981d66450e7SArnd Bergmann #define stat_inc_inline_inode(inode)			do { } while (0)
3982d66450e7SArnd Bergmann #define stat_dec_inline_inode(inode)			do { } while (0)
3983d66450e7SArnd Bergmann #define stat_inc_inline_dir(inode)			do { } while (0)
3984d66450e7SArnd Bergmann #define stat_dec_inline_dir(inode)			do { } while (0)
39854c8ff709SChao Yu #define stat_inc_compr_inode(inode)			do { } while (0)
39864c8ff709SChao Yu #define stat_dec_compr_inode(inode)			do { } while (0)
39874c8ff709SChao Yu #define stat_add_compr_blocks(inode, blocks)		do { } while (0)
39884c8ff709SChao Yu #define stat_sub_compr_blocks(inode, blocks)		do { } while (0)
3989d66450e7SArnd Bergmann #define stat_update_max_atomic_write(inode)		do { } while (0)
3990b63e7be5SChao Yu #define stat_inc_meta_count(sbi, blkaddr)		do { } while (0)
3991d66450e7SArnd Bergmann #define stat_inc_seg_type(sbi, curseg)			do { } while (0)
3992d66450e7SArnd Bergmann #define stat_inc_block_count(sbi, curseg)		do { } while (0)
3993d66450e7SArnd Bergmann #define stat_inc_inplace_blocks(sbi)			do { } while (0)
3994d66450e7SArnd Bergmann #define stat_inc_seg_count(sbi, type, gc_type)		do { } while (0)
3995d66450e7SArnd Bergmann #define stat_inc_tot_blk_count(si, blks)		do { } while (0)
3996d66450e7SArnd Bergmann #define stat_inc_data_blk_count(sbi, blks, gc_type)	do { } while (0)
3997d66450e7SArnd Bergmann #define stat_inc_node_blk_count(sbi, blks, gc_type)	do { } while (0)
399839a53e0cSJaegeuk Kim 
399939a53e0cSJaegeuk Kim static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
400039a53e0cSJaegeuk Kim static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
400121acc07dSGreg Kroah-Hartman static inline void __init f2fs_create_root_stats(void) { }
40024589d25dSNamjae Jeon static inline void f2fs_destroy_root_stats(void) { }
400348abe91aSYueHaibing static inline void f2fs_update_sit_info(struct f2fs_sb_info *sbi) {}
400439a53e0cSJaegeuk Kim #endif
400539a53e0cSJaegeuk Kim 
400639a53e0cSJaegeuk Kim extern const struct file_operations f2fs_dir_operations;
400739a53e0cSJaegeuk Kim extern const struct file_operations f2fs_file_operations;
400839a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_file_inode_operations;
400939a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_dblock_aops;
401039a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_node_aops;
401139a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_meta_aops;
401239a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_dir_inode_operations;
401339a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_symlink_inode_operations;
4014cbaf042aSJaegeuk Kim extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
401539a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_special_inode_operations;
40164d57b86dSChao Yu extern struct kmem_cache *f2fs_inode_entry_slab;
40171001b347SHuajun Li 
4018e18c65b2SHuajun Li /*
4019e18c65b2SHuajun Li  * inline.c
4020e18c65b2SHuajun Li  */
4021cac5a3d8SDongOh Shin bool f2fs_may_inline_data(struct inode *inode);
4022cac5a3d8SDongOh Shin bool f2fs_may_inline_dentry(struct inode *inode);
40234d57b86dSChao Yu void f2fs_do_read_inline_data(struct page *page, struct page *ipage);
40244d57b86dSChao Yu void f2fs_truncate_inline_inode(struct inode *inode,
40254d57b86dSChao Yu 						struct page *ipage, u64 from);
4026cac5a3d8SDongOh Shin int f2fs_read_inline_data(struct inode *inode, struct page *page);
4027cac5a3d8SDongOh Shin int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
4028cac5a3d8SDongOh Shin int f2fs_convert_inline_inode(struct inode *inode);
4029b06af2afSJaegeuk Kim int f2fs_try_convert_inline_dir(struct inode *dir, struct dentry *dentry);
4030cac5a3d8SDongOh Shin int f2fs_write_inline_data(struct inode *inode, struct page *page);
40319627a7b3SChao Yu int f2fs_recover_inline_data(struct inode *inode, struct page *npage);
40324d57b86dSChao Yu struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
403343c780baSEric Biggers 					const struct f2fs_filename *fname,
403443c780baSEric Biggers 					struct page **res_page);
40354d57b86dSChao Yu int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent,
4036cac5a3d8SDongOh Shin 			struct page *ipage);
403743c780baSEric Biggers int f2fs_add_inline_entry(struct inode *dir, const struct f2fs_filename *fname,
4038cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
40394d57b86dSChao Yu void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry,
40404d57b86dSChao Yu 				struct page *page, struct inode *dir,
40414d57b86dSChao Yu 				struct inode *inode);
4042cac5a3d8SDongOh Shin bool f2fs_empty_inline_dir(struct inode *dir);
4043cac5a3d8SDongOh Shin int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
4044cac5a3d8SDongOh Shin 			struct fscrypt_str *fstr);
4045cac5a3d8SDongOh Shin int f2fs_inline_data_fiemap(struct inode *inode,
4046cac5a3d8SDongOh Shin 			struct fiemap_extent_info *fieinfo,
4047cac5a3d8SDongOh Shin 			__u64 start, __u64 len);
4048cde4de12SJaegeuk Kim 
4049cde4de12SJaegeuk Kim /*
40502658e50dSJaegeuk Kim  * shrinker.c
40512658e50dSJaegeuk Kim  */
4052cac5a3d8SDongOh Shin unsigned long f2fs_shrink_count(struct shrinker *shrink,
4053cac5a3d8SDongOh Shin 			struct shrink_control *sc);
4054cac5a3d8SDongOh Shin unsigned long f2fs_shrink_scan(struct shrinker *shrink,
4055cac5a3d8SDongOh Shin 			struct shrink_control *sc);
4056cac5a3d8SDongOh Shin void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
4057cac5a3d8SDongOh Shin void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
40582658e50dSJaegeuk Kim 
40592658e50dSJaegeuk Kim /*
4060a28ef1f5SChao Yu  * extent_cache.c
4061a28ef1f5SChao Yu  */
40624dada3fdSChao Yu struct rb_entry *f2fs_lookup_rb_tree(struct rb_root_cached *root,
4063004b6862SChao Yu 				struct rb_entry *cached_re, unsigned int ofs);
40642e9b2bb2SChao Yu struct rb_node **f2fs_lookup_rb_tree_ext(struct f2fs_sb_info *sbi,
40652e9b2bb2SChao Yu 				struct rb_root_cached *root,
40662e9b2bb2SChao Yu 				struct rb_node **parent,
40672e9b2bb2SChao Yu 				unsigned long long key, bool *left_most);
40684d57b86dSChao Yu struct rb_node **f2fs_lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
40694dada3fdSChao Yu 				struct rb_root_cached *root,
40704dada3fdSChao Yu 				struct rb_node **parent,
40714dada3fdSChao Yu 				unsigned int ofs, bool *leftmost);
40724dada3fdSChao Yu struct rb_entry *f2fs_lookup_rb_tree_ret(struct rb_root_cached *root,
4073004b6862SChao Yu 		struct rb_entry *cached_re, unsigned int ofs,
4074004b6862SChao Yu 		struct rb_entry **prev_entry, struct rb_entry **next_entry,
4075004b6862SChao Yu 		struct rb_node ***insert_p, struct rb_node **insert_parent,
40764dada3fdSChao Yu 		bool force, bool *leftmost);
40774d57b86dSChao Yu bool f2fs_check_rb_tree_consistence(struct f2fs_sb_info *sbi,
40782e9b2bb2SChao Yu 				struct rb_root_cached *root, bool check_key);
4079cac5a3d8SDongOh Shin unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
4080a6d601f3SChao Yu void f2fs_init_extent_tree(struct inode *inode, struct page *ipage);
4081cac5a3d8SDongOh Shin void f2fs_drop_extent_tree(struct inode *inode);
4082cac5a3d8SDongOh Shin unsigned int f2fs_destroy_extent_node(struct inode *inode);
4083cac5a3d8SDongOh Shin void f2fs_destroy_extent_tree(struct inode *inode);
4084cac5a3d8SDongOh Shin bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
4085cac5a3d8SDongOh Shin 			struct extent_info *ei);
4086cac5a3d8SDongOh Shin void f2fs_update_extent_cache(struct dnode_of_data *dn);
408719b2c30dSChao Yu void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
4088cac5a3d8SDongOh Shin 			pgoff_t fofs, block_t blkaddr, unsigned int len);
40894d57b86dSChao Yu void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi);
40904d57b86dSChao Yu int __init f2fs_create_extent_cache(void);
40914d57b86dSChao Yu void f2fs_destroy_extent_cache(void);
4092a28ef1f5SChao Yu 
4093a28ef1f5SChao Yu /*
40948ceffcb2SChao Yu  * sysfs.c
40958ceffcb2SChao Yu  */
40960f6b56ecSDaeho Jeong #define MIN_RA_MUL	2
40970f6b56ecSDaeho Jeong #define MAX_RA_MUL	256
40980f6b56ecSDaeho Jeong 
4099dc6b2055SJaegeuk Kim int __init f2fs_init_sysfs(void);
4100dc6b2055SJaegeuk Kim void f2fs_exit_sysfs(void);
4101dc6b2055SJaegeuk Kim int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
4102dc6b2055SJaegeuk Kim void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
41038ceffcb2SChao Yu 
410495ae251fSEric Biggers /* verity.c */
410595ae251fSEric Biggers extern const struct fsverity_operations f2fs_verityops;
410695ae251fSEric Biggers 
41078ceffcb2SChao Yu /*
4108cde4de12SJaegeuk Kim  * crypto support
4109cde4de12SJaegeuk Kim  */
41101958593eSJaegeuk Kim static inline bool f2fs_encrypted_file(struct inode *inode)
41111958593eSJaegeuk Kim {
411262230e0dSChandan Rajendra 	return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
41131958593eSJaegeuk Kim }
41141958593eSJaegeuk Kim 
4115cde4de12SJaegeuk Kim static inline void f2fs_set_encrypted_inode(struct inode *inode)
4116cde4de12SJaegeuk Kim {
4117643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
4118cde4de12SJaegeuk Kim 	file_set_encrypt(inode);
41199149a5ebSChao Yu 	f2fs_set_inode_flags(inode);
4120cde4de12SJaegeuk Kim #endif
4121cde4de12SJaegeuk Kim }
4122cde4de12SJaegeuk Kim 
41236dbb1796SEric Biggers /*
41246dbb1796SEric Biggers  * Returns true if the reads of the inode's data need to undergo some
41256dbb1796SEric Biggers  * postprocessing step, like decryption or authenticity verification.
41266dbb1796SEric Biggers  */
41276dbb1796SEric Biggers static inline bool f2fs_post_read_required(struct inode *inode)
4128cde4de12SJaegeuk Kim {
41294c8ff709SChao Yu 	return f2fs_encrypted_file(inode) || fsverity_active(inode) ||
41304c8ff709SChao Yu 		f2fs_compressed_file(inode);
41314c8ff709SChao Yu }
41324c8ff709SChao Yu 
41334c8ff709SChao Yu /*
41344c8ff709SChao Yu  * compress.c
41354c8ff709SChao Yu  */
41364c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
41374c8ff709SChao Yu bool f2fs_is_compressed_page(struct page *page);
41384c8ff709SChao Yu struct page *f2fs_compress_control_page(struct page *page);
41394c8ff709SChao Yu int f2fs_prepare_compress_overwrite(struct inode *inode,
41404c8ff709SChao Yu 			struct page **pagep, pgoff_t index, void **fsdata);
41414c8ff709SChao Yu bool f2fs_compress_write_end(struct inode *inode, void *fsdata,
41424c8ff709SChao Yu 					pgoff_t index, unsigned copied);
41433265d3dbSChao Yu int f2fs_truncate_partial_cluster(struct inode *inode, u64 from, bool lock);
41444c8ff709SChao Yu void f2fs_compress_write_end_io(struct bio *bio, struct page *page);
41454c8ff709SChao Yu bool f2fs_is_compress_backend_ready(struct inode *inode);
41465e6bbde9SChao Yu int f2fs_init_compress_mempool(void);
41475e6bbde9SChao Yu void f2fs_destroy_compress_mempool(void);
41486ce19affSChao Yu void f2fs_decompress_cluster(struct decompress_io_ctx *dic);
41496ce19affSChao Yu void f2fs_end_read_compressed_page(struct page *page, bool failed,
41506ce19affSChao Yu 							block_t blkaddr);
41514c8ff709SChao Yu bool f2fs_cluster_is_empty(struct compress_ctx *cc);
41524c8ff709SChao Yu bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index);
4153b368cc5eSFengnan Chang bool f2fs_all_cluster_page_loaded(struct compress_ctx *cc, struct pagevec *pvec,
4154b368cc5eSFengnan Chang 				int index, int nr_pages);
4155bbe1da7eSChao Yu bool f2fs_sanity_check_cluster(struct dnode_of_data *dn);
41564c8ff709SChao Yu void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct page *page);
41574c8ff709SChao Yu int f2fs_write_multi_pages(struct compress_ctx *cc,
41584c8ff709SChao Yu 						int *submitted,
41594c8ff709SChao Yu 						struct writeback_control *wbc,
41604c8ff709SChao Yu 						enum iostat_type io_type);
41614c8ff709SChao Yu int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index);
416294afd6d6SChao Yu void f2fs_update_extent_tree_range_compressed(struct inode *inode,
416394afd6d6SChao Yu 				pgoff_t fofs, block_t blkaddr, unsigned int llen,
416494afd6d6SChao Yu 				unsigned int c_len);
41654c8ff709SChao Yu int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
41664c8ff709SChao Yu 				unsigned nr_pages, sector_t *last_block_in_bio,
41670683728aSChao Yu 				bool is_readahead, bool for_write);
41684c8ff709SChao Yu struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc);
41697f59b277SEric Biggers void f2fs_decompress_end_io(struct decompress_io_ctx *dic, bool failed);
41707f59b277SEric Biggers void f2fs_put_page_dic(struct page *page);
417194afd6d6SChao Yu unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn);
41724c8ff709SChao Yu int f2fs_init_compress_ctx(struct compress_ctx *cc);
41738bfbfb0dSChao Yu void f2fs_destroy_compress_ctx(struct compress_ctx *cc, bool reuse);
41744c8ff709SChao Yu void f2fs_init_compress_info(struct f2fs_sb_info *sbi);
41756ce19affSChao Yu int f2fs_init_compress_inode(struct f2fs_sb_info *sbi);
41766ce19affSChao Yu void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi);
417731083031SChao Yu int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi);
417831083031SChao Yu void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi);
4179c68d6c88SChao Yu int __init f2fs_init_compress_cache(void);
4180c68d6c88SChao Yu void f2fs_destroy_compress_cache(void);
41816ce19affSChao Yu struct address_space *COMPRESS_MAPPING(struct f2fs_sb_info *sbi);
41826ce19affSChao Yu void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi, block_t blkaddr);
41836ce19affSChao Yu void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
41846ce19affSChao Yu 						nid_t ino, block_t blkaddr);
41856ce19affSChao Yu bool f2fs_load_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
41866ce19affSChao Yu 								block_t blkaddr);
41876ce19affSChao Yu void f2fs_invalidate_compress_pages(struct f2fs_sb_info *sbi, nid_t ino);
41885ac443e2SDaeho Jeong #define inc_compr_inode_stat(inode)					\
41895ac443e2SDaeho Jeong 	do {								\
41905ac443e2SDaeho Jeong 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);		\
41915ac443e2SDaeho Jeong 		sbi->compr_new_inode++;					\
41925ac443e2SDaeho Jeong 	} while (0)
41935ac443e2SDaeho Jeong #define add_compr_block_stat(inode, blocks)				\
41945ac443e2SDaeho Jeong 	do {								\
41955ac443e2SDaeho Jeong 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);		\
41965ac443e2SDaeho Jeong 		int diff = F2FS_I(inode)->i_cluster_size - blocks;	\
41975ac443e2SDaeho Jeong 		sbi->compr_written_block += blocks;			\
41985ac443e2SDaeho Jeong 		sbi->compr_saved_block += diff;				\
41995ac443e2SDaeho Jeong 	} while (0)
42004c8ff709SChao Yu #else
42014c8ff709SChao Yu static inline bool f2fs_is_compressed_page(struct page *page) { return false; }
42024c8ff709SChao Yu static inline bool f2fs_is_compress_backend_ready(struct inode *inode)
42034c8ff709SChao Yu {
42044c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
42054c8ff709SChao Yu 		return true;
42064c8ff709SChao Yu 	/* not support compression */
42074c8ff709SChao Yu 	return false;
42084c8ff709SChao Yu }
42094c8ff709SChao Yu static inline struct page *f2fs_compress_control_page(struct page *page)
42104c8ff709SChao Yu {
42114c8ff709SChao Yu 	WARN_ON_ONCE(1);
42124c8ff709SChao Yu 	return ERR_PTR(-EINVAL);
42134c8ff709SChao Yu }
42145e6bbde9SChao Yu static inline int f2fs_init_compress_mempool(void) { return 0; }
42155e6bbde9SChao Yu static inline void f2fs_destroy_compress_mempool(void) { }
42166ce19affSChao Yu static inline void f2fs_decompress_cluster(struct decompress_io_ctx *dic) { }
42176ce19affSChao Yu static inline void f2fs_end_read_compressed_page(struct page *page,
42186ce19affSChao Yu 						bool failed, block_t blkaddr)
42197f59b277SEric Biggers {
42207f59b277SEric Biggers 	WARN_ON_ONCE(1);
42217f59b277SEric Biggers }
42227f59b277SEric Biggers static inline void f2fs_put_page_dic(struct page *page)
42237f59b277SEric Biggers {
42247f59b277SEric Biggers 	WARN_ON_ONCE(1);
42257f59b277SEric Biggers }
422694afd6d6SChao Yu static inline unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn) { return 0; }
4227bbe1da7eSChao Yu static inline bool f2fs_sanity_check_cluster(struct dnode_of_data *dn) { return false; }
42286ce19affSChao Yu static inline int f2fs_init_compress_inode(struct f2fs_sb_info *sbi) { return 0; }
42296ce19affSChao Yu static inline void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi) { }
423031083031SChao Yu static inline int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi) { return 0; }
423131083031SChao Yu static inline void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi) { }
4232c68d6c88SChao Yu static inline int __init f2fs_init_compress_cache(void) { return 0; }
4233c68d6c88SChao Yu static inline void f2fs_destroy_compress_cache(void) { }
42346ce19affSChao Yu static inline void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi,
42356ce19affSChao Yu 				block_t blkaddr) { }
42366ce19affSChao Yu static inline void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi,
42376ce19affSChao Yu 				struct page *page, nid_t ino, block_t blkaddr) { }
42386ce19affSChao Yu static inline bool f2fs_load_compressed_page(struct f2fs_sb_info *sbi,
42396ce19affSChao Yu 				struct page *page, block_t blkaddr) { return false; }
42406ce19affSChao Yu static inline void f2fs_invalidate_compress_pages(struct f2fs_sb_info *sbi,
42416ce19affSChao Yu 							nid_t ino) { }
42425ac443e2SDaeho Jeong #define inc_compr_inode_stat(inode)		do { } while (0)
424394afd6d6SChao Yu static inline void f2fs_update_extent_tree_range_compressed(struct inode *inode,
424494afd6d6SChao Yu 				pgoff_t fofs, block_t blkaddr, unsigned int llen,
424594afd6d6SChao Yu 				unsigned int c_len) { }
42464c8ff709SChao Yu #endif
42474c8ff709SChao Yu 
42484c8ff709SChao Yu static inline void set_compress_context(struct inode *inode)
42494c8ff709SChao Yu {
42504c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
42514c8ff709SChao Yu 
42524c8ff709SChao Yu 	F2FS_I(inode)->i_compress_algorithm =
42534c8ff709SChao Yu 			F2FS_OPTION(sbi).compress_algorithm;
42544c8ff709SChao Yu 	F2FS_I(inode)->i_log_cluster_size =
42554c8ff709SChao Yu 			F2FS_OPTION(sbi).compress_log_size;
4256b28f047bSChao Yu 	F2FS_I(inode)->i_compress_flag =
4257b28f047bSChao Yu 			F2FS_OPTION(sbi).compress_chksum ?
4258b28f047bSChao Yu 				1 << COMPRESS_CHKSUM : 0;
42594c8ff709SChao Yu 	F2FS_I(inode)->i_cluster_size =
42604c8ff709SChao Yu 			1 << F2FS_I(inode)->i_log_cluster_size;
426101f6afd0SChao Yu 	if ((F2FS_I(inode)->i_compress_algorithm == COMPRESS_LZ4 ||
426201f6afd0SChao Yu 		F2FS_I(inode)->i_compress_algorithm == COMPRESS_ZSTD) &&
42633fde13f8SChao Yu 			F2FS_OPTION(sbi).compress_level)
42643fde13f8SChao Yu 		F2FS_I(inode)->i_compress_flag |=
42653fde13f8SChao Yu 				F2FS_OPTION(sbi).compress_level <<
42663fde13f8SChao Yu 				COMPRESS_LEVEL_OFFSET;
42674c8ff709SChao Yu 	F2FS_I(inode)->i_flags |= F2FS_COMPR_FL;
42684c8ff709SChao Yu 	set_inode_flag(inode, FI_COMPRESSED_FILE);
42694c8ff709SChao Yu 	stat_inc_compr_inode(inode);
42705ac443e2SDaeho Jeong 	inc_compr_inode_stat(inode);
4271530e0704SChao Yu 	f2fs_mark_inode_dirty_sync(inode, true);
42724c8ff709SChao Yu }
42734c8ff709SChao Yu 
427478134d03SDaeho Jeong static inline bool f2fs_disable_compressed_file(struct inode *inode)
42754c8ff709SChao Yu {
42764c8ff709SChao Yu 	struct f2fs_inode_info *fi = F2FS_I(inode);
42774c8ff709SChao Yu 
42784c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
427978134d03SDaeho Jeong 		return true;
428002d58cd2SHyeong-Jun Kim 	if (S_ISREG(inode->i_mode) && F2FS_HAS_BLOCKS(inode))
428178134d03SDaeho Jeong 		return false;
42824c8ff709SChao Yu 
42834c8ff709SChao Yu 	fi->i_flags &= ~F2FS_COMPR_FL;
42844c8ff709SChao Yu 	stat_dec_compr_inode(inode);
428596f5b4faSChao Yu 	clear_inode_flag(inode, FI_COMPRESSED_FILE);
4286530e0704SChao Yu 	f2fs_mark_inode_dirty_sync(inode, true);
428778134d03SDaeho Jeong 	return true;
4288cde4de12SJaegeuk Kim }
4289cde4de12SJaegeuk Kim 
4290ccd31cb2SSheng Yong #define F2FS_FEATURE_FUNCS(name, flagname) \
42917beb01f7SChao Yu static inline int f2fs_sb_has_##name(struct f2fs_sb_info *sbi) \
4292ccd31cb2SSheng Yong { \
42937beb01f7SChao Yu 	return F2FS_HAS_FEATURE(sbi, F2FS_FEATURE_##flagname); \
4294cde4de12SJaegeuk Kim }
4295f424f664SJaegeuk Kim 
4296ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(encrypt, ENCRYPT);
4297ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(blkzoned, BLKZONED);
4298ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(extra_attr, EXTRA_ATTR);
4299ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(project_quota, PRJQUOTA);
4300ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_chksum, INODE_CHKSUM);
4301ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
4302ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
4303ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
4304b7c409deSSheng Yong F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
430595ae251fSEric Biggers F2FS_FEATURE_FUNCS(verity, VERITY);
4306d440c52dSJunling Zheng F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
43075aba5430SDaniel Rosenberg F2FS_FEATURE_FUNCS(casefold, CASEFOLD);
43084c8ff709SChao Yu F2FS_FEATURE_FUNCS(compression, COMPRESSION);
4309a7d9fe3cSJaegeuk Kim F2FS_FEATURE_FUNCS(readonly, RO);
43101c1d35dfSChao Yu 
43114215d054SDaeho Jeong static inline bool f2fs_may_extent_tree(struct inode *inode)
43124215d054SDaeho Jeong {
43134215d054SDaeho Jeong 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
43144215d054SDaeho Jeong 
43154215d054SDaeho Jeong 	if (!test_opt(sbi, EXTENT_CACHE) ||
43164215d054SDaeho Jeong 			is_inode_flag_set(inode, FI_NO_EXTENT) ||
43174215d054SDaeho Jeong 			(is_inode_flag_set(inode, FI_COMPRESSED_FILE) &&
43184215d054SDaeho Jeong 			 !f2fs_sb_has_readonly(sbi)))
43194215d054SDaeho Jeong 		return false;
43204215d054SDaeho Jeong 
43214215d054SDaeho Jeong 	/*
43224215d054SDaeho Jeong 	 * for recovered files during mount do not create extents
43234215d054SDaeho Jeong 	 * if shrinker is not registered.
43244215d054SDaeho Jeong 	 */
43254215d054SDaeho Jeong 	if (list_empty(&sbi->s_list))
43264215d054SDaeho Jeong 		return false;
43274215d054SDaeho Jeong 
43284215d054SDaeho Jeong 	return S_ISREG(inode->i_mode);
43294215d054SDaeho Jeong }
43304215d054SDaeho Jeong 
4331178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
433295175dafSDamien Le Moal static inline bool f2fs_blkz_is_seq(struct f2fs_sb_info *sbi, int devi,
433395175dafSDamien Le Moal 				    block_t blkaddr)
4334178053e2SDamien Le Moal {
4335178053e2SDamien Le Moal 	unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
4336178053e2SDamien Le Moal 
433795175dafSDamien Le Moal 	return test_bit(zno, FDEV(devi).blkz_seq);
4338178053e2SDamien Le Moal }
4339178053e2SDamien Le Moal #endif
4340178053e2SDamien Le Moal 
43417d20c8abSChao Yu static inline bool f2fs_hw_should_discard(struct f2fs_sb_info *sbi)
434296ba2decSDamien Le Moal {
43437beb01f7SChao Yu 	return f2fs_sb_has_blkzoned(sbi);
43447d20c8abSChao Yu }
434596ba2decSDamien Le Moal 
43467f3d7719SDamien Le Moal static inline bool f2fs_bdev_support_discard(struct block_device *bdev)
43477f3d7719SDamien Le Moal {
43487f3d7719SDamien Le Moal 	return blk_queue_discard(bdev_get_queue(bdev)) ||
43497f3d7719SDamien Le Moal 	       bdev_is_zoned(bdev);
43507f3d7719SDamien Le Moal }
43517f3d7719SDamien Le Moal 
43527d20c8abSChao Yu static inline bool f2fs_hw_support_discard(struct f2fs_sb_info *sbi)
43537d20c8abSChao Yu {
43547f3d7719SDamien Le Moal 	int i;
43557f3d7719SDamien Le Moal 
43567f3d7719SDamien Le Moal 	if (!f2fs_is_multi_device(sbi))
43577f3d7719SDamien Le Moal 		return f2fs_bdev_support_discard(sbi->sb->s_bdev);
43587f3d7719SDamien Le Moal 
43597f3d7719SDamien Le Moal 	for (i = 0; i < sbi->s_ndevs; i++)
43607f3d7719SDamien Le Moal 		if (f2fs_bdev_support_discard(FDEV(i).bdev))
43617f3d7719SDamien Le Moal 			return true;
43627f3d7719SDamien Le Moal 	return false;
43637d20c8abSChao Yu }
43647d20c8abSChao Yu 
43657d20c8abSChao Yu static inline bool f2fs_realtime_discard_enable(struct f2fs_sb_info *sbi)
43667d20c8abSChao Yu {
43677d20c8abSChao Yu 	return (test_opt(sbi, DISCARD) && f2fs_hw_support_discard(sbi)) ||
43687d20c8abSChao Yu 					f2fs_hw_should_discard(sbi);
436952763a4bSJaegeuk Kim }
437052763a4bSJaegeuk Kim 
4371f824deb5SChao Yu static inline bool f2fs_hw_is_readonly(struct f2fs_sb_info *sbi)
4372f824deb5SChao Yu {
4373f824deb5SChao Yu 	int i;
4374f824deb5SChao Yu 
4375f824deb5SChao Yu 	if (!f2fs_is_multi_device(sbi))
4376f824deb5SChao Yu 		return bdev_read_only(sbi->sb->s_bdev);
4377f824deb5SChao Yu 
4378f824deb5SChao Yu 	for (i = 0; i < sbi->s_ndevs; i++)
4379f824deb5SChao Yu 		if (bdev_read_only(FDEV(i).bdev))
4380f824deb5SChao Yu 			return true;
4381f824deb5SChao Yu 	return false;
4382f824deb5SChao Yu }
4383f824deb5SChao Yu 
4384b0332a0fSChao Yu static inline bool f2fs_lfs_mode(struct f2fs_sb_info *sbi)
438552763a4bSJaegeuk Kim {
4386b0332a0fSChao Yu 	return F2FS_OPTION(sbi).fs_mode == FS_MODE_LFS;
438752763a4bSJaegeuk Kim }
438852763a4bSJaegeuk Kim 
43894c8ff709SChao Yu static inline bool f2fs_may_compress(struct inode *inode)
43904c8ff709SChao Yu {
43914c8ff709SChao Yu 	if (IS_SWAPFILE(inode) || f2fs_is_pinned_file(inode) ||
43927bc155feSJaegeuk Kim 				f2fs_is_atomic_file(inode))
43934c8ff709SChao Yu 		return false;
43944c8ff709SChao Yu 	return S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode);
43954c8ff709SChao Yu }
43964c8ff709SChao Yu 
43974c8ff709SChao Yu static inline void f2fs_i_compr_blocks_update(struct inode *inode,
43984c8ff709SChao Yu 						u64 blocks, bool add)
43994c8ff709SChao Yu {
44004c8ff709SChao Yu 	int diff = F2FS_I(inode)->i_cluster_size - blocks;
4401c2759ebaSDaeho Jeong 	struct f2fs_inode_info *fi = F2FS_I(inode);
44024c8ff709SChao Yu 
4403ef8d563fSChao Yu 	/* don't update i_compr_blocks if saved blocks were released */
4404c2759ebaSDaeho Jeong 	if (!add && !atomic_read(&fi->i_compr_blocks))
4405ef8d563fSChao Yu 		return;
4406ef8d563fSChao Yu 
44074c8ff709SChao Yu 	if (add) {
4408c2759ebaSDaeho Jeong 		atomic_add(diff, &fi->i_compr_blocks);
44094c8ff709SChao Yu 		stat_add_compr_blocks(inode, diff);
44104c8ff709SChao Yu 	} else {
4411c2759ebaSDaeho Jeong 		atomic_sub(diff, &fi->i_compr_blocks);
44124c8ff709SChao Yu 		stat_sub_compr_blocks(inode, diff);
44134c8ff709SChao Yu 	}
44144c8ff709SChao Yu 	f2fs_mark_inode_dirty_sync(inode, true);
44154c8ff709SChao Yu }
44164c8ff709SChao Yu 
4417f847c699SChao Yu static inline int block_unaligned_IO(struct inode *inode,
4418f847c699SChao Yu 				struct kiocb *iocb, struct iov_iter *iter)
4419b91050a8SHyunchul Lee {
4420f847c699SChao Yu 	unsigned int i_blkbits = READ_ONCE(inode->i_blkbits);
4421f847c699SChao Yu 	unsigned int blocksize_mask = (1 << i_blkbits) - 1;
4422f847c699SChao Yu 	loff_t offset = iocb->ki_pos;
4423f847c699SChao Yu 	unsigned long align = offset | iov_iter_alignment(iter);
4424f847c699SChao Yu 
4425f847c699SChao Yu 	return align & blocksize_mask;
4426f847c699SChao Yu }
4427f847c699SChao Yu 
442871f2c820SChao Yu static inline bool f2fs_allow_multi_device_dio(struct f2fs_sb_info *sbi,
442971f2c820SChao Yu 								int flag)
443071f2c820SChao Yu {
443171f2c820SChao Yu 	if (!f2fs_is_multi_device(sbi))
443271f2c820SChao Yu 		return false;
443371f2c820SChao Yu 	if (flag != F2FS_GET_BLOCK_DIO)
443471f2c820SChao Yu 		return false;
443571f2c820SChao Yu 	return sbi->aligned_blksize;
443671f2c820SChao Yu }
443771f2c820SChao Yu 
4438f847c699SChao Yu static inline bool f2fs_force_buffered_io(struct inode *inode,
4439f847c699SChao Yu 				struct kiocb *iocb, struct iov_iter *iter)
4440f847c699SChao Yu {
4441f847c699SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4442f847c699SChao Yu 	int rw = iov_iter_rw(iter);
4443f847c699SChao Yu 
44448a2c77bcSEric Biggers 	if (!fscrypt_dio_supported(iocb, iter))
44458a2c77bcSEric Biggers 		return true;
44468a2c77bcSEric Biggers 	if (fsverity_active(inode))
44478a2c77bcSEric Biggers 		return true;
44488a2c77bcSEric Biggers 	if (f2fs_compressed_file(inode))
4449f847c699SChao Yu 		return true;
445071f2c820SChao Yu 
445171f2c820SChao Yu 	/* disallow direct IO if any of devices has unaligned blksize */
445271f2c820SChao Yu 	if (f2fs_is_multi_device(sbi) && !sbi->aligned_blksize)
4453f847c699SChao Yu 		return true;
4454f847c699SChao Yu 	/*
4455f847c699SChao Yu 	 * for blkzoned device, fallback direct IO to buffered IO, so
4456f847c699SChao Yu 	 * all IOs can be serialized by log-structured write.
4457f847c699SChao Yu 	 */
44587beb01f7SChao Yu 	if (f2fs_sb_has_blkzoned(sbi))
4459f847c699SChao Yu 		return true;
4460b0332a0fSChao Yu 	if (f2fs_lfs_mode(sbi) && (rw == WRITE)) {
44619720ee80SChao Yu 		if (block_unaligned_IO(inode, iocb, iter))
4462f847c699SChao Yu 			return true;
44639720ee80SChao Yu 		if (F2FS_IO_ALIGNED(sbi))
44649720ee80SChao Yu 			return true;
44659720ee80SChao Yu 	}
4466ebc29b62Shuangjianan@oppo.com 	if (is_sbi_flag_set(F2FS_I_SB(inode), SBI_CP_DISABLED))
44674354994fSDaniel Rosenberg 		return true;
44684354994fSDaniel Rosenberg 
4469f847c699SChao Yu 	return false;
4470b91050a8SHyunchul Lee }
4471b91050a8SHyunchul Lee 
44727f59b277SEric Biggers static inline bool f2fs_need_verity(const struct inode *inode, pgoff_t idx)
44737f59b277SEric Biggers {
44747f59b277SEric Biggers 	return fsverity_active(inode) &&
44757f59b277SEric Biggers 	       idx < DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
44767f59b277SEric Biggers }
44777f59b277SEric Biggers 
447883a3bfdbSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
4479d494500aSChao Yu extern void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate,
4480d494500aSChao Yu 							unsigned int type);
448183a3bfdbSJaegeuk Kim #else
4482d494500aSChao Yu #define f2fs_build_fault_attr(sbi, rate, type)		do { } while (0)
448383a3bfdbSJaegeuk Kim #endif
448483a3bfdbSJaegeuk Kim 
4485af033b2aSChao Yu static inline bool is_journalled_quota(struct f2fs_sb_info *sbi)
4486af033b2aSChao Yu {
4487af033b2aSChao Yu #ifdef CONFIG_QUOTA
44887beb01f7SChao Yu 	if (f2fs_sb_has_quota_ino(sbi))
4489af033b2aSChao Yu 		return true;
4490af033b2aSChao Yu 	if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
4491af033b2aSChao Yu 		F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
4492af033b2aSChao Yu 		F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
4493af033b2aSChao Yu 		return true;
4494af033b2aSChao Yu #endif
4495af033b2aSChao Yu 	return false;
4496af033b2aSChao Yu }
44970af725fcSJaegeuk Kim 
44984f993264SChao Yu static inline bool f2fs_block_unit_discard(struct f2fs_sb_info *sbi)
44994f993264SChao Yu {
45004f993264SChao Yu 	return F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_BLOCK;
45014f993264SChao Yu }
45024f993264SChao Yu 
4503a64239d0SNeilBrown static inline void f2fs_io_schedule_timeout(long timeout)
4504a64239d0SNeilBrown {
4505a64239d0SNeilBrown 	set_current_state(TASK_UNINTERRUPTIBLE);
4506a64239d0SNeilBrown 	io_schedule_timeout(timeout);
4507a64239d0SNeilBrown }
4508a64239d0SNeilBrown 
4509a7b8618aSJaegeuk Kim static inline void f2fs_handle_page_eio(struct f2fs_sb_info *sbi, pgoff_t ofs,
4510a7b8618aSJaegeuk Kim 					enum page_type type)
4511a7b8618aSJaegeuk Kim {
4512a7b8618aSJaegeuk Kim 	if (unlikely(f2fs_cp_error(sbi)))
4513a7b8618aSJaegeuk Kim 		return;
4514a7b8618aSJaegeuk Kim 
4515a7b8618aSJaegeuk Kim 	if (ofs == sbi->page_eio_ofs[type]) {
4516a7b8618aSJaegeuk Kim 		if (sbi->page_eio_cnt[type]++ == MAX_RETRY_PAGE_EIO)
4517a7b8618aSJaegeuk Kim 			set_ckpt_flags(sbi, CP_ERROR_FLAG);
4518a7b8618aSJaegeuk Kim 	} else {
4519a7b8618aSJaegeuk Kim 		sbi->page_eio_ofs[type] = ofs;
4520a7b8618aSJaegeuk Kim 		sbi->page_eio_cnt[type] = 0;
4521a7b8618aSJaegeuk Kim 	}
4522a7b8618aSJaegeuk Kim }
4523a7b8618aSJaegeuk Kim 
452410f966bbSChao Yu #define EFSBADCRC	EBADMSG		/* Bad CRC detected */
452510f966bbSChao Yu #define EFSCORRUPTED	EUCLEAN		/* Filesystem is corrupted */
452610f966bbSChao Yu 
4527e1074d4bSChao Yu #endif /* _LINUX_F2FS_H */
4528