xref: /openbmc/linux/fs/f2fs/f2fs.h (revision e4544b63a7ee49e7fbebf35ece0a6acd3b9617ae)
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 
126*e4544b63STim Murray /*
127*e4544b63STim Murray  * An implementation of an rwsem that is explicitly unfair to readers. This
128*e4544b63STim Murray  * prevents priority inversion when a low-priority reader acquires the read lock
129*e4544b63STim Murray  * while sleeping on the write lock but the write lock is needed by
130*e4544b63STim Murray  * higher-priority clients.
131*e4544b63STim Murray  */
132*e4544b63STim Murray 
133*e4544b63STim Murray struct f2fs_rwsem {
134*e4544b63STim Murray         struct rw_semaphore internal_rwsem;
135*e4544b63STim Murray         wait_queue_head_t read_waiters;
136*e4544b63STim Murray };
137*e4544b63STim Murray 
13839a53e0cSJaegeuk Kim struct f2fs_mount_info {
13939a53e0cSJaegeuk Kim 	unsigned int opt;
14063189b78SChao Yu 	int write_io_size_bits;		/* Write IO size bits */
14163189b78SChao Yu 	block_t root_reserved_blocks;	/* root reserved blocks */
14263189b78SChao Yu 	kuid_t s_resuid;		/* reserved blocks for uid */
14363189b78SChao Yu 	kgid_t s_resgid;		/* reserved blocks for gid */
14463189b78SChao Yu 	int active_logs;		/* # of active logs */
14563189b78SChao Yu 	int inline_xattr_size;		/* inline xattr size */
14663189b78SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
14763189b78SChao Yu 	struct f2fs_fault_info fault_info;	/* For fault injection */
14863189b78SChao Yu #endif
14963189b78SChao Yu #ifdef CONFIG_QUOTA
15063189b78SChao Yu 	/* Names of quota files with journalled quota */
15163189b78SChao Yu 	char *s_qf_names[MAXQUOTAS];
15263189b78SChao Yu 	int s_jquota_fmt;			/* Format of quota to use */
15363189b78SChao Yu #endif
15463189b78SChao Yu 	/* For which write hints are passed down to block layer */
15563189b78SChao Yu 	int whint_mode;
15663189b78SChao Yu 	int alloc_mode;			/* segment allocation policy */
15763189b78SChao Yu 	int fsync_mode;			/* fsync policy */
158b0332a0fSChao Yu 	int fs_mode;			/* fs mode: LFS or ADAPTIVE */
159bbbc34fdSChao Yu 	int bggc_mode;			/* bggc mode: off, on or sync */
1604f993264SChao Yu 	int discard_unit;		/*
1614f993264SChao Yu 					 * discard command's offset/size should
1624f993264SChao Yu 					 * be aligned to this unit: block,
1634f993264SChao Yu 					 * segment or section
1644f993264SChao Yu 					 */
165ac4acb1fSEric Biggers 	struct fscrypt_dummy_policy dummy_enc_policy; /* test dummy encryption */
1661ae18f71SJaegeuk Kim 	block_t unusable_cap_perc;	/* percentage for cap */
1674d3aed70SDaniel Rosenberg 	block_t unusable_cap;		/* Amount of space allowed to be
1684d3aed70SDaniel Rosenberg 					 * unusable when disabling checkpoint
1694d3aed70SDaniel Rosenberg 					 */
1704c8ff709SChao Yu 
1714c8ff709SChao Yu 	/* For compression */
1724c8ff709SChao Yu 	unsigned char compress_algorithm;	/* algorithm type */
173b28f047bSChao Yu 	unsigned char compress_log_size;	/* cluster log size */
1743fde13f8SChao Yu 	unsigned char compress_level;		/* compress level */
175b28f047bSChao Yu 	bool compress_chksum;			/* compressed data chksum */
1764c8ff709SChao Yu 	unsigned char compress_ext_cnt;		/* extension count */
177151b1982SFengnan Chang 	unsigned char nocompress_ext_cnt;		/* nocompress extension count */
178602a16d5SDaeho Jeong 	int compress_mode;			/* compression mode */
1794c8ff709SChao Yu 	unsigned char extensions[COMPRESS_EXT_NUM][F2FS_EXTENSION_LEN];	/* extensions */
180151b1982SFengnan Chang 	unsigned char noextensions[COMPRESS_EXT_NUM][F2FS_EXTENSION_LEN]; /* extensions */
18139a53e0cSJaegeuk Kim };
18239a53e0cSJaegeuk Kim 
183cde4de12SJaegeuk Kim #define F2FS_FEATURE_ENCRYPT		0x0001
1840bfd7a09SDamien Le Moal #define F2FS_FEATURE_BLKZONED		0x0002
185e65ef207SJaegeuk Kim #define F2FS_FEATURE_ATOMIC_WRITE	0x0004
1867a2af766SChao Yu #define F2FS_FEATURE_EXTRA_ATTR		0x0008
1875c57132eSChao Yu #define F2FS_FEATURE_PRJQUOTA		0x0010
188704956ecSChao Yu #define F2FS_FEATURE_INODE_CHKSUM	0x0020
1896afc662eSChao Yu #define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR	0x0040
190234a9689SJaegeuk Kim #define F2FS_FEATURE_QUOTA_INO		0x0080
1911c1d35dfSChao Yu #define F2FS_FEATURE_INODE_CRTIME	0x0100
192b7c409deSSheng Yong #define F2FS_FEATURE_LOST_FOUND		0x0200
19395ae251fSEric Biggers #define F2FS_FEATURE_VERITY		0x0400
194d440c52dSJunling Zheng #define F2FS_FEATURE_SB_CHKSUM		0x0800
1955aba5430SDaniel Rosenberg #define F2FS_FEATURE_CASEFOLD		0x1000
1964c8ff709SChao Yu #define F2FS_FEATURE_COMPRESSION	0x2000
197a7d9fe3cSJaegeuk Kim #define F2FS_FEATURE_RO			0x4000
198cde4de12SJaegeuk Kim 
1997beb01f7SChao Yu #define __F2FS_HAS_FEATURE(raw_super, mask)				\
2007beb01f7SChao Yu 	((raw_super->feature & cpu_to_le32(mask)) != 0)
2017beb01f7SChao Yu #define F2FS_HAS_FEATURE(sbi, mask)	__F2FS_HAS_FEATURE(sbi->raw_super, mask)
2027beb01f7SChao Yu #define F2FS_SET_FEATURE(sbi, mask)					\
2037beb01f7SChao Yu 	(sbi->raw_super->feature |= cpu_to_le32(mask))
2047beb01f7SChao Yu #define F2FS_CLEAR_FEATURE(sbi, mask)					\
2057beb01f7SChao Yu 	(sbi->raw_super->feature &= ~cpu_to_le32(mask))
20676f105a2SJaegeuk Kim 
20739a53e0cSJaegeuk Kim /*
2087c2e5963SJaegeuk Kim  * Default values for user and/or group using reserved blocks
2097c2e5963SJaegeuk Kim  */
2107c2e5963SJaegeuk Kim #define	F2FS_DEF_RESUID		0
2117c2e5963SJaegeuk Kim #define	F2FS_DEF_RESGID		0
2127c2e5963SJaegeuk Kim 
2137c2e5963SJaegeuk Kim /*
21439a53e0cSJaegeuk Kim  * For checkpoint manager
21539a53e0cSJaegeuk Kim  */
21639a53e0cSJaegeuk Kim enum {
21739a53e0cSJaegeuk Kim 	NAT_BITMAP,
21839a53e0cSJaegeuk Kim 	SIT_BITMAP
21939a53e0cSJaegeuk Kim };
22039a53e0cSJaegeuk Kim 
221c473f1a9SChao Yu #define	CP_UMOUNT	0x00000001
222c473f1a9SChao Yu #define	CP_FASTBOOT	0x00000002
223c473f1a9SChao Yu #define	CP_SYNC		0x00000004
224c473f1a9SChao Yu #define	CP_RECOVERY	0x00000008
225c473f1a9SChao Yu #define	CP_DISCARD	0x00000010
2261f43e2adSChao Yu #define CP_TRIMMED	0x00000020
2274354994fSDaniel Rosenberg #define CP_PAUSE	0x00000040
228b4b10061SJaegeuk Kim #define CP_RESIZE 	0x00000080
22975ab4cb8SJaegeuk Kim 
2304ddb1a4dSJaegeuk Kim #define MAX_DISCARD_BLOCKS(sbi)		BLKS_PER_SEC(sbi)
231ecc9aa00SChao Yu #define DEF_MAX_DISCARD_REQUEST		8	/* issue 8 discards per round */
232969d1b18SChao Yu #define DEF_MIN_DISCARD_ISSUE_TIME	50	/* 50 ms, if exists */
233f9d1dcedSYunlei He #define DEF_MID_DISCARD_ISSUE_TIME	500	/* 500 ms, if device busy */
234969d1b18SChao Yu #define DEF_MAX_DISCARD_ISSUE_TIME	60000	/* 60 s, if no candidates */
2358bb4f253SJaegeuk Kim #define DEF_DISCARD_URGENT_UTIL		80	/* do more discard over 80% */
23660b99b48SJaegeuk Kim #define DEF_CP_INTERVAL			60	/* 60 secs */
237dcf25fe8SChao Yu #define DEF_IDLE_INTERVAL		5	/* 5 secs */
2384354994fSDaniel Rosenberg #define DEF_DISABLE_INTERVAL		5	/* 5 secs */
239db610a64SJaegeuk Kim #define DEF_DISABLE_QUICK_INTERVAL	1	/* 1 secs */
24003f2c02dSJaegeuk Kim #define DEF_UMOUNT_DISCARD_TIMEOUT	5	/* 5 secs */
241bba681cbSJaegeuk Kim 
24275ab4cb8SJaegeuk Kim struct cp_control {
24375ab4cb8SJaegeuk Kim 	int reason;
2444b2fecc8SJaegeuk Kim 	__u64 trim_start;
2454b2fecc8SJaegeuk Kim 	__u64 trim_end;
2464b2fecc8SJaegeuk Kim 	__u64 trim_minlen;
24775ab4cb8SJaegeuk Kim };
24875ab4cb8SJaegeuk Kim 
249662befdaSChao Yu /*
250e1da7872SChao Yu  * indicate meta/data type
251662befdaSChao Yu  */
252662befdaSChao Yu enum {
253662befdaSChao Yu 	META_CP,
254662befdaSChao Yu 	META_NAT,
25581c1a0f1SChao Yu 	META_SIT,
2564c521f49SJaegeuk Kim 	META_SSA,
257b63e7be5SChao Yu 	META_MAX,
2584c521f49SJaegeuk Kim 	META_POR,
25993770ab7SChao Yu 	DATA_GENERIC,		/* check range only */
26093770ab7SChao Yu 	DATA_GENERIC_ENHANCE,	/* strong check on range and segment bitmap */
26193770ab7SChao Yu 	DATA_GENERIC_ENHANCE_READ,	/*
26293770ab7SChao Yu 					 * strong check on range and segment
26393770ab7SChao Yu 					 * bitmap but no warning due to race
26493770ab7SChao Yu 					 * condition of read on truncated area
26593770ab7SChao Yu 					 * by extent_cache
26693770ab7SChao Yu 					 */
267e1da7872SChao Yu 	META_GENERIC,
268662befdaSChao Yu };
269662befdaSChao Yu 
2706451e041SJaegeuk Kim /* for the list of ino */
2716451e041SJaegeuk Kim enum {
2726451e041SJaegeuk Kim 	ORPHAN_INO,		/* for orphan ino list */
273fff04f90SJaegeuk Kim 	APPEND_INO,		/* for append ino list */
274fff04f90SJaegeuk Kim 	UPDATE_INO,		/* for update ino list */
2750a007b97SJaegeuk Kim 	TRANS_DIR_INO,		/* for trasactions dir ino list */
27639d787beSChao Yu 	FLUSH_INO,		/* for multiple device flushing */
2776451e041SJaegeuk Kim 	MAX_INO_ENTRY,		/* max. list */
2786451e041SJaegeuk Kim };
2796451e041SJaegeuk Kim 
2806451e041SJaegeuk Kim struct ino_entry {
28139a53e0cSJaegeuk Kim 	struct list_head list;		/* list head */
28239a53e0cSJaegeuk Kim 	nid_t ino;			/* inode number */
28339d787beSChao Yu 	unsigned int dirty_device;	/* dirty device bitmap */
28439a53e0cSJaegeuk Kim };
28539a53e0cSJaegeuk Kim 
2862710fd7eSChao Yu /* for the list of inodes to be GCed */
28706292073SChao Yu struct inode_entry {
28839a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
28939a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
29039a53e0cSJaegeuk Kim };
29139a53e0cSJaegeuk Kim 
29250fa53ecSChao Yu struct fsync_node_entry {
29350fa53ecSChao Yu 	struct list_head list;	/* list head */
29450fa53ecSChao Yu 	struct page *page;	/* warm node page pointer */
29550fa53ecSChao Yu 	unsigned int seq_id;	/* sequence id */
29650fa53ecSChao Yu };
29750fa53ecSChao Yu 
298261eeb9cSDaeho Jeong struct ckpt_req {
299261eeb9cSDaeho Jeong 	struct completion wait;		/* completion for checkpoint done */
300261eeb9cSDaeho Jeong 	struct llist_node llnode;	/* llist_node to be linked in wait queue */
301261eeb9cSDaeho Jeong 	int ret;			/* return code of checkpoint */
302261eeb9cSDaeho Jeong 	ktime_t queue_time;		/* request queued time */
303261eeb9cSDaeho Jeong };
304261eeb9cSDaeho Jeong 
305261eeb9cSDaeho Jeong struct ckpt_req_control {
306261eeb9cSDaeho Jeong 	struct task_struct *f2fs_issue_ckpt;	/* checkpoint task */
307e6592066SDaeho Jeong 	int ckpt_thread_ioprio;			/* checkpoint merge thread ioprio */
308261eeb9cSDaeho Jeong 	wait_queue_head_t ckpt_wait_queue;	/* waiting queue for wake-up */
309261eeb9cSDaeho Jeong 	atomic_t issued_ckpt;		/* # of actually issued ckpts */
310261eeb9cSDaeho Jeong 	atomic_t total_ckpt;		/* # of total ckpts */
311261eeb9cSDaeho Jeong 	atomic_t queued_ckpt;		/* # of queued ckpts */
312261eeb9cSDaeho Jeong 	struct llist_head issue_list;	/* list for command issue */
313261eeb9cSDaeho Jeong 	spinlock_t stat_lock;		/* lock for below checkpoint time stats */
314261eeb9cSDaeho Jeong 	unsigned int cur_time;		/* cur wait time in msec for currently issued checkpoint */
315261eeb9cSDaeho Jeong 	unsigned int peak_time;		/* peak wait time in msec until now */
316261eeb9cSDaeho Jeong };
317261eeb9cSDaeho Jeong 
318a7eeb823SChao Yu /* for the bitmap indicate blocks to be discarded */
3197fd9e544SJaegeuk Kim struct discard_entry {
3207fd9e544SJaegeuk Kim 	struct list_head list;	/* list head */
321a7eeb823SChao Yu 	block_t start_blkaddr;	/* start blockaddr of current segment */
322a7eeb823SChao Yu 	unsigned char discard_map[SIT_VBLOCK_MAP_SIZE];	/* segment discard bitmap */
3237fd9e544SJaegeuk Kim };
3247fd9e544SJaegeuk Kim 
325969d1b18SChao Yu /* default discard granularity of inner discard thread, unit: block count */
326969d1b18SChao Yu #define DEFAULT_DISCARD_GRANULARITY		16
327969d1b18SChao Yu 
328ba48a33eSChao Yu /* max discard pend list number */
329ba48a33eSChao Yu #define MAX_PLIST_NUM		512
330ba48a33eSChao Yu #define plist_idx(blk_num)	((blk_num) >= MAX_PLIST_NUM ?		\
3312f84babfSSheng Yong 					(MAX_PLIST_NUM - 1) : ((blk_num) - 1))
332ba48a33eSChao Yu 
33315469963SJaegeuk Kim enum {
33435ec7d57SChao Yu 	D_PREP,			/* initial */
33535ec7d57SChao Yu 	D_PARTIAL,		/* partially submitted */
33635ec7d57SChao Yu 	D_SUBMIT,		/* all submitted */
33735ec7d57SChao Yu 	D_DONE,			/* finished */
33815469963SJaegeuk Kim };
33915469963SJaegeuk Kim 
340004b6862SChao Yu struct discard_info {
341b01a9201SJaegeuk Kim 	block_t lstart;			/* logical start address */
342b01a9201SJaegeuk Kim 	block_t len;			/* length */
343004b6862SChao Yu 	block_t start;			/* actual start address in dev */
344004b6862SChao Yu };
345004b6862SChao Yu 
346b01a9201SJaegeuk Kim struct discard_cmd {
347004b6862SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
348004b6862SChao Yu 	union {
349004b6862SChao Yu 		struct {
350004b6862SChao Yu 			block_t lstart;	/* logical start address */
351004b6862SChao Yu 			block_t len;	/* length */
352004b6862SChao Yu 			block_t start;	/* actual start address in dev */
353004b6862SChao Yu 		};
354004b6862SChao Yu 		struct discard_info di;	/* discard info */
355004b6862SChao Yu 
356004b6862SChao Yu 	};
357b01a9201SJaegeuk Kim 	struct list_head list;		/* command list */
358b01a9201SJaegeuk Kim 	struct completion wait;		/* compleation */
359c81abe34SJaegeuk Kim 	struct block_device *bdev;	/* bdev */
360ec9895adSChao Yu 	unsigned short ref;		/* reference count */
3619a744b92SChao Yu 	unsigned char state;		/* state */
36272691af6SJaegeuk Kim 	unsigned char queued;		/* queued discard */
363c81abe34SJaegeuk Kim 	int error;			/* bio error */
36435ec7d57SChao Yu 	spinlock_t lock;		/* for state/bio_ref updating */
36535ec7d57SChao Yu 	unsigned short bio_ref;		/* bio reference count */
366275b66b0SChao Yu };
367275b66b0SChao Yu 
36878997b56SChao Yu enum {
36978997b56SChao Yu 	DPOLICY_BG,
37078997b56SChao Yu 	DPOLICY_FORCE,
37178997b56SChao Yu 	DPOLICY_FSTRIM,
37278997b56SChao Yu 	DPOLICY_UMOUNT,
37378997b56SChao Yu 	MAX_DPOLICY,
37478997b56SChao Yu };
37578997b56SChao Yu 
376ecc9aa00SChao Yu struct discard_policy {
37778997b56SChao Yu 	int type;			/* type of discard */
378ecc9aa00SChao Yu 	unsigned int min_interval;	/* used for candidates exist */
379f9d1dcedSYunlei He 	unsigned int mid_interval;	/* used for device busy */
380ecc9aa00SChao Yu 	unsigned int max_interval;	/* used for candidates not exist */
381ecc9aa00SChao Yu 	unsigned int max_requests;	/* # of discards issued per round */
382ecc9aa00SChao Yu 	unsigned int io_aware_gran;	/* minimum granularity discard not be aware of I/O */
383ecc9aa00SChao Yu 	bool io_aware;			/* issue discard in idle time */
384ecc9aa00SChao Yu 	bool sync;			/* submit discard with REQ_SYNC flag */
38520ee4382SChao Yu 	bool ordered;			/* issue discard by lba order */
3866ce48b0cSChao Yu 	bool timeout;			/* discard timeout for put_super */
38778997b56SChao Yu 	unsigned int granularity;	/* discard granularity */
388ecc9aa00SChao Yu };
389ecc9aa00SChao Yu 
3900b54fb84SJaegeuk Kim struct discard_cmd_control {
39115469963SJaegeuk Kim 	struct task_struct *f2fs_issue_discard;	/* discard thread */
39246f84c2cSChao Yu 	struct list_head entry_list;		/* 4KB discard entry list */
393ba48a33eSChao Yu 	struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
39446f84c2cSChao Yu 	struct list_head wait_list;		/* store on-flushing entries */
3958412663dSChao Yu 	struct list_head fstrim_list;		/* in-flight discard from fstrim */
39615469963SJaegeuk Kim 	wait_queue_head_t discard_wait_queue;	/* waiting queue for wake-up */
397969d1b18SChao Yu 	unsigned int discard_wake;		/* to wake up discard thread */
39815469963SJaegeuk Kim 	struct mutex cmd_lock;
399d618ebafSChao Yu 	unsigned int nr_discards;		/* # of discards in the list */
400d618ebafSChao Yu 	unsigned int max_discards;		/* max. discards to be issued */
401969d1b18SChao Yu 	unsigned int discard_granularity;	/* discard granularity */
402d84d1cbdSChao Yu 	unsigned int undiscard_blks;		/* # of undiscard blocks */
40320ee4382SChao Yu 	unsigned int next_pos;			/* next discard position */
4048b8dd65fSChao Yu 	atomic_t issued_discard;		/* # of issued discard */
40572691af6SJaegeuk Kim 	atomic_t queued_discard;		/* # of queued discard */
4065f32366aSChao Yu 	atomic_t discard_cmd_cnt;		/* # of cached cmd count */
4074dada3fdSChao Yu 	struct rb_root_cached root;		/* root of discard rb-tree */
40867fce70bSChao Yu 	bool rbtree_check;			/* config for consistence check */
40939a53e0cSJaegeuk Kim };
41039a53e0cSJaegeuk Kim 
41139a53e0cSJaegeuk Kim /* for the list of fsync inodes, used only during recovery */
41239a53e0cSJaegeuk Kim struct fsync_inode_entry {
41339a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
41439a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
415c52e1b10SJaegeuk Kim 	block_t blkaddr;	/* block address locating the last fsync */
416c52e1b10SJaegeuk Kim 	block_t last_dentry;	/* block address locating the last dentry */
41739a53e0cSJaegeuk Kim };
41839a53e0cSJaegeuk Kim 
41968afcf2dSTomohiro Kusumi #define nats_in_cursum(jnl)		(le16_to_cpu((jnl)->n_nats))
42068afcf2dSTomohiro Kusumi #define sits_in_cursum(jnl)		(le16_to_cpu((jnl)->n_sits))
42139a53e0cSJaegeuk Kim 
42268afcf2dSTomohiro Kusumi #define nat_in_journal(jnl, i)		((jnl)->nat_j.entries[i].ne)
42368afcf2dSTomohiro Kusumi #define nid_in_journal(jnl, i)		((jnl)->nat_j.entries[i].nid)
42468afcf2dSTomohiro Kusumi #define sit_in_journal(jnl, i)		((jnl)->sit_j.entries[i].se)
42568afcf2dSTomohiro Kusumi #define segno_in_journal(jnl, i)	((jnl)->sit_j.entries[i].segno)
42639a53e0cSJaegeuk Kim 
427dfc08a12SChao Yu #define MAX_NAT_JENTRIES(jnl)	(NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
428dfc08a12SChao Yu #define MAX_SIT_JENTRIES(jnl)	(SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
429309cc2b6SJaegeuk Kim 
430dfc08a12SChao Yu static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
43139a53e0cSJaegeuk Kim {
432dfc08a12SChao Yu 	int before = nats_in_cursum(journal);
433cac5a3d8SDongOh Shin 
434dfc08a12SChao Yu 	journal->n_nats = cpu_to_le16(before + i);
43539a53e0cSJaegeuk Kim 	return before;
43639a53e0cSJaegeuk Kim }
43739a53e0cSJaegeuk Kim 
438dfc08a12SChao Yu static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
43939a53e0cSJaegeuk Kim {
440dfc08a12SChao Yu 	int before = sits_in_cursum(journal);
441cac5a3d8SDongOh Shin 
442dfc08a12SChao Yu 	journal->n_sits = cpu_to_le16(before + i);
44339a53e0cSJaegeuk Kim 	return before;
44439a53e0cSJaegeuk Kim }
44539a53e0cSJaegeuk Kim 
446dfc08a12SChao Yu static inline bool __has_cursum_space(struct f2fs_journal *journal,
447dfc08a12SChao Yu 							int size, int type)
448184a5cd2SChao Yu {
449184a5cd2SChao Yu 	if (type == NAT_JOURNAL)
450dfc08a12SChao Yu 		return size <= MAX_NAT_JENTRIES(journal);
451dfc08a12SChao Yu 	return size <= MAX_SIT_JENTRIES(journal);
452184a5cd2SChao Yu }
453184a5cd2SChao Yu 
454f2470371SChao Yu /* for inline stuff */
455f2470371SChao Yu #define DEF_INLINE_RESERVED_SIZE	1
4567a2af766SChao Yu static inline int get_extra_isize(struct inode *inode);
4576afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode);
4587a2af766SChao Yu #define MAX_INLINE_DATA(inode)	(sizeof(__le32) *			\
4597a2af766SChao Yu 				(CUR_ADDRS_PER_INODE(inode) -		\
460b323fd28SChao Yu 				get_inline_xattr_addrs(inode) -	\
4616afc662eSChao Yu 				DEF_INLINE_RESERVED_SIZE))
462f2470371SChao Yu 
463f2470371SChao Yu /* for inline dir */
464f2470371SChao Yu #define NR_INLINE_DENTRY(inode)	(MAX_INLINE_DATA(inode) * BITS_PER_BYTE / \
465f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
466f2470371SChao Yu 				BITS_PER_BYTE + 1))
467f91108b8SGeert Uytterhoeven #define INLINE_DENTRY_BITMAP_SIZE(inode) \
468f91108b8SGeert Uytterhoeven 	DIV_ROUND_UP(NR_INLINE_DENTRY(inode), BITS_PER_BYTE)
469f2470371SChao Yu #define INLINE_RESERVED_SIZE(inode)	(MAX_INLINE_DATA(inode) - \
470f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
471f2470371SChao Yu 				NR_INLINE_DENTRY(inode) + \
472f2470371SChao Yu 				INLINE_DENTRY_BITMAP_SIZE(inode)))
473f2470371SChao Yu 
474e9750824SNamjae Jeon /*
47539a53e0cSJaegeuk Kim  * For INODE and NODE manager
47639a53e0cSJaegeuk Kim  */
4777b3cd7d6SJaegeuk Kim /* for directory operations */
47843c780baSEric Biggers 
47943c780baSEric Biggers struct f2fs_filename {
48043c780baSEric Biggers 	/*
48143c780baSEric Biggers 	 * The filename the user specified.  This is NULL for some
48243c780baSEric Biggers 	 * filesystem-internal operations, e.g. converting an inline directory
48343c780baSEric Biggers 	 * to a non-inline one, or roll-forward recovering an encrypted dentry.
48443c780baSEric Biggers 	 */
48543c780baSEric Biggers 	const struct qstr *usr_fname;
48643c780baSEric Biggers 
48743c780baSEric Biggers 	/*
48843c780baSEric Biggers 	 * The on-disk filename.  For encrypted directories, this is encrypted.
48943c780baSEric Biggers 	 * This may be NULL for lookups in an encrypted dir without the key.
49043c780baSEric Biggers 	 */
49143c780baSEric Biggers 	struct fscrypt_str disk_name;
49243c780baSEric Biggers 
49343c780baSEric Biggers 	/* The dirhash of this filename */
49443c780baSEric Biggers 	f2fs_hash_t hash;
49543c780baSEric Biggers 
49643c780baSEric Biggers #ifdef CONFIG_FS_ENCRYPTION
49743c780baSEric Biggers 	/*
49843c780baSEric Biggers 	 * For lookups in encrypted directories: either the buffer backing
49943c780baSEric Biggers 	 * disk_name, or a buffer that holds the decoded no-key name.
50043c780baSEric Biggers 	 */
50143c780baSEric Biggers 	struct fscrypt_str crypto_buf;
50243c780baSEric Biggers #endif
50343c780baSEric Biggers #ifdef CONFIG_UNICODE
50443c780baSEric Biggers 	/*
50543c780baSEric Biggers 	 * For casefolded directories: the casefolded name, but it's left NULL
5067ad08a58SDaniel Rosenberg 	 * if the original name is not valid Unicode, if the directory is both
5077ad08a58SDaniel Rosenberg 	 * casefolded and encrypted and its encryption key is unavailable, or if
5087ad08a58SDaniel Rosenberg 	 * the filesystem is doing an internal operation where usr_fname is also
5097ad08a58SDaniel Rosenberg 	 * NULL.  In all these cases we fall back to treating the name as an
5107ad08a58SDaniel Rosenberg 	 * opaque byte sequence.
51143c780baSEric Biggers 	 */
51243c780baSEric Biggers 	struct fscrypt_str cf_name;
51343c780baSEric Biggers #endif
51443c780baSEric Biggers };
51543c780baSEric Biggers 
5167b3cd7d6SJaegeuk Kim struct f2fs_dentry_ptr {
517d8c6822aSJaegeuk Kim 	struct inode *inode;
51876a9dd85SChao Yu 	void *bitmap;
5197b3cd7d6SJaegeuk Kim 	struct f2fs_dir_entry *dentry;
5207b3cd7d6SJaegeuk Kim 	__u8 (*filename)[F2FS_SLOT_LEN];
5217b3cd7d6SJaegeuk Kim 	int max;
52276a9dd85SChao Yu 	int nr_bitmap;
5237b3cd7d6SJaegeuk Kim };
5247b3cd7d6SJaegeuk Kim 
52564c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_block(struct inode *inode,
52664c24ecbSTomohiro Kusumi 		struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
5277b3cd7d6SJaegeuk Kim {
528d8c6822aSJaegeuk Kim 	d->inode = inode;
5297b3cd7d6SJaegeuk Kim 	d->max = NR_DENTRY_IN_BLOCK;
53076a9dd85SChao Yu 	d->nr_bitmap = SIZE_OF_DENTRY_BITMAP;
531c79d1520SYunlong Song 	d->bitmap = t->dentry_bitmap;
5327b3cd7d6SJaegeuk Kim 	d->dentry = t->dentry;
5337b3cd7d6SJaegeuk Kim 	d->filename = t->filename;
53464c24ecbSTomohiro Kusumi }
535cac5a3d8SDongOh Shin 
53664c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_inline(struct inode *inode,
537f2470371SChao Yu 					struct f2fs_dentry_ptr *d, void *t)
53864c24ecbSTomohiro Kusumi {
539f2470371SChao Yu 	int entry_cnt = NR_INLINE_DENTRY(inode);
540f2470371SChao Yu 	int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(inode);
541f2470371SChao Yu 	int reserved_size = INLINE_RESERVED_SIZE(inode);
542f2470371SChao Yu 
54364c24ecbSTomohiro Kusumi 	d->inode = inode;
544f2470371SChao Yu 	d->max = entry_cnt;
545f2470371SChao Yu 	d->nr_bitmap = bitmap_size;
546f2470371SChao Yu 	d->bitmap = t;
547f2470371SChao Yu 	d->dentry = t + bitmap_size + reserved_size;
548f2470371SChao Yu 	d->filename = t + bitmap_size + reserved_size +
549f2470371SChao Yu 					SIZE_OF_DIR_ENTRY * entry_cnt;
5507b3cd7d6SJaegeuk Kim }
5517b3cd7d6SJaegeuk Kim 
552dbe6a5ffSJaegeuk Kim /*
553dbe6a5ffSJaegeuk Kim  * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
554dbe6a5ffSJaegeuk Kim  * as its node offset to distinguish from index node blocks.
555dbe6a5ffSJaegeuk Kim  * But some bits are used to mark the node block.
55639a53e0cSJaegeuk Kim  */
557dbe6a5ffSJaegeuk Kim #define XATTR_NODE_OFFSET	((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
558dbe6a5ffSJaegeuk Kim 				>> OFFSET_BIT_SHIFT)
559266e97a8SJaegeuk Kim enum {
560266e97a8SJaegeuk Kim 	ALLOC_NODE,			/* allocate a new node page if needed */
561266e97a8SJaegeuk Kim 	LOOKUP_NODE,			/* look up a node without readahead */
562266e97a8SJaegeuk Kim 	LOOKUP_NODE_RA,			/*
563266e97a8SJaegeuk Kim 					 * look up a node with readahead called
5644f4124d0SChao Yu 					 * by get_data_block.
56539a53e0cSJaegeuk Kim 					 */
566266e97a8SJaegeuk Kim };
567266e97a8SJaegeuk Kim 
56891803392SChao Yu #define DEFAULT_RETRY_IO_COUNT	8	/* maximum retry read IO or flush count */
5697735730dSChao Yu 
5705df7731fSChao Yu /* congestion wait timeout value, default: 20ms */
5715df7731fSChao Yu #define	DEFAULT_IO_TIMEOUT	(msecs_to_jiffies(20))
5725df7731fSChao Yu 
573af033b2aSChao Yu /* maximum retry quota flush count */
574af033b2aSChao Yu #define DEFAULT_RETRY_QUOTA_FLUSH_COUNT		8
575af033b2aSChao Yu 
576a6db67f0SJaegeuk Kim #define F2FS_LINK_MAX	0xffffffff	/* maximum link count per file */
57739a53e0cSJaegeuk Kim 
578817202d9SChao Yu #define MAX_DIR_RA_PAGES	4	/* maximum ra pages of dir */
579817202d9SChao Yu 
580287b1406SChao Yu /* dirty segments threshold for triggering CP */
581287b1406SChao Yu #define DEFAULT_DIRTY_THRESHOLD		4
582287b1406SChao Yu 
58339a53e0cSJaegeuk Kim /* for in-memory extent cache entry */
58413054c54SChao Yu #define F2FS_MIN_EXTENT_LEN	64	/* minimum extent length */
58513054c54SChao Yu 
58613054c54SChao Yu /* number of extent info in extent cache we try to shrink */
58713054c54SChao Yu #define EXTENT_CACHE_SHRINK_NUMBER	128
588c11abd1aSJaegeuk Kim 
58954c2258cSChao Yu struct rb_entry {
59054c2258cSChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
5912e9b2bb2SChao Yu 	union {
5922e9b2bb2SChao Yu 		struct {
59354c2258cSChao Yu 			unsigned int ofs;	/* start offset of the entry */
59454c2258cSChao Yu 			unsigned int len;	/* length of the entry */
59554c2258cSChao Yu 		};
5962e9b2bb2SChao Yu 		unsigned long long key;		/* 64-bits key */
59748046cb5SJaegeuk Kim 	} __packed;
5982e9b2bb2SChao Yu };
59954c2258cSChao Yu 
60039a53e0cSJaegeuk Kim struct extent_info {
60139a53e0cSJaegeuk Kim 	unsigned int fofs;		/* start offset in a file */
602111d2495SMasanari Iida 	unsigned int len;		/* length of the extent */
60354c2258cSChao Yu 	u32 blk;			/* start block address of the extent */
60494afd6d6SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
60594afd6d6SChao Yu 	unsigned int c_len;		/* physical extent length of compressed blocks */
60694afd6d6SChao Yu #endif
60739a53e0cSJaegeuk Kim };
60839a53e0cSJaegeuk Kim 
60913054c54SChao Yu struct extent_node {
610c0362117SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
61113054c54SChao Yu 	struct extent_info ei;		/* extent info */
61254c2258cSChao Yu 	struct list_head list;		/* node in global extent list of sbi */
613201ef5e0SHou Pengyang 	struct extent_tree *et;		/* extent tree pointer */
61413054c54SChao Yu };
61513054c54SChao Yu 
61613054c54SChao Yu struct extent_tree {
61713054c54SChao Yu 	nid_t ino;			/* inode number */
6184dada3fdSChao Yu 	struct rb_root_cached root;	/* root of extent info rb-tree */
61962c8af65SChao Yu 	struct extent_node *cached_en;	/* recently accessed extent node */
6203e72f721SJaegeuk Kim 	struct extent_info largest;	/* largested extent info */
621137d09f0SJaegeuk Kim 	struct list_head list;		/* to be used by sbi->zombie_list */
62213054c54SChao Yu 	rwlock_t lock;			/* protect extent info rb-tree */
62368e35385SChao Yu 	atomic_t node_cnt;		/* # of extent node in rb-tree*/
624b430f726SZhikang Zhang 	bool largest_updated;		/* largest extent updated */
62513054c54SChao Yu };
62613054c54SChao Yu 
62739a53e0cSJaegeuk Kim /*
628003a3e1dSJaegeuk Kim  * This structure is taken from ext4_map_blocks.
629003a3e1dSJaegeuk Kim  *
630003a3e1dSJaegeuk Kim  * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
631003a3e1dSJaegeuk Kim  */
632003a3e1dSJaegeuk Kim #define F2FS_MAP_NEW		(1 << BH_New)
633003a3e1dSJaegeuk Kim #define F2FS_MAP_MAPPED		(1 << BH_Mapped)
6347f63eb77SJaegeuk Kim #define F2FS_MAP_UNWRITTEN	(1 << BH_Unwritten)
6357f63eb77SJaegeuk Kim #define F2FS_MAP_FLAGS		(F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
6367f63eb77SJaegeuk Kim 				F2FS_MAP_UNWRITTEN)
637003a3e1dSJaegeuk Kim 
638003a3e1dSJaegeuk Kim struct f2fs_map_blocks {
63971f2c820SChao Yu 	struct block_device *m_bdev;	/* for multi-device dio */
640003a3e1dSJaegeuk Kim 	block_t m_pblk;
641003a3e1dSJaegeuk Kim 	block_t m_lblk;
642003a3e1dSJaegeuk Kim 	unsigned int m_len;
643003a3e1dSJaegeuk Kim 	unsigned int m_flags;
644da85985cSChao Yu 	pgoff_t *m_next_pgofs;		/* point next possible non-hole pgofs */
645c4020b2dSChao Yu 	pgoff_t *m_next_extent;		/* point to next possible extent */
646d5097be5SHyunchul Lee 	int m_seg_type;
647f9d6d059SChao Yu 	bool m_may_create;		/* indicate it is from write path */
64871f2c820SChao Yu 	bool m_multidev_dio;		/* indicate it allows multi-device dio */
649003a3e1dSJaegeuk Kim };
650003a3e1dSJaegeuk Kim 
651e2b4e2bcSChao Yu /* for flag in get_data_block */
652f2220c7fSQiuyang Sun enum {
653f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_DEFAULT,
654f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_FIEMAP,
655f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_BMAP,
6560a4daae5SJaegeuk Kim 	F2FS_GET_BLOCK_DIO,
657f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_DIO,
658f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_AIO,
659c4020b2dSChao Yu 	F2FS_GET_BLOCK_PRECACHE,
660f2220c7fSQiuyang Sun };
661e2b4e2bcSChao Yu 
662003a3e1dSJaegeuk Kim /*
66339a53e0cSJaegeuk Kim  * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
66439a53e0cSJaegeuk Kim  */
66539a53e0cSJaegeuk Kim #define FADVISE_COLD_BIT	0x01
666354a3399SJaegeuk Kim #define FADVISE_LOST_PINO_BIT	0x02
667cde4de12SJaegeuk Kim #define FADVISE_ENCRYPT_BIT	0x04
668e7d55452SJaegeuk Kim #define FADVISE_ENC_NAME_BIT	0x08
66926787236SJaegeuk Kim #define FADVISE_KEEP_SIZE_BIT	0x10
670b6a06cbbSChao Yu #define FADVISE_HOT_BIT		0x20
67195ae251fSEric Biggers #define FADVISE_VERITY_BIT	0x40
672d4dd19ecSJaegeuk Kim #define FADVISE_TRUNC_BIT	0x80
67339a53e0cSJaegeuk Kim 
674797c1cb5SChao Yu #define FADVISE_MODIFIABLE_BITS	(FADVISE_COLD_BIT | FADVISE_HOT_BIT)
675797c1cb5SChao Yu 
676b5492af7SJaegeuk Kim #define file_is_cold(inode)	is_file(inode, FADVISE_COLD_BIT)
677b5492af7SJaegeuk Kim #define file_set_cold(inode)	set_file(inode, FADVISE_COLD_BIT)
678b5492af7SJaegeuk Kim #define file_clear_cold(inode)	clear_file(inode, FADVISE_COLD_BIT)
6791153db09SChao Yu 
6801153db09SChao Yu #define file_wrong_pino(inode)	is_file(inode, FADVISE_LOST_PINO_BIT)
6811153db09SChao Yu #define file_lost_pino(inode)	set_file(inode, FADVISE_LOST_PINO_BIT)
682b5492af7SJaegeuk Kim #define file_got_pino(inode)	clear_file(inode, FADVISE_LOST_PINO_BIT)
6831153db09SChao Yu 
684cde4de12SJaegeuk Kim #define file_is_encrypt(inode)	is_file(inode, FADVISE_ENCRYPT_BIT)
685cde4de12SJaegeuk Kim #define file_set_encrypt(inode)	set_file(inode, FADVISE_ENCRYPT_BIT)
6861153db09SChao Yu 
687e7d55452SJaegeuk Kim #define file_enc_name(inode)	is_file(inode, FADVISE_ENC_NAME_BIT)
688e7d55452SJaegeuk Kim #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
6891153db09SChao Yu 
69026787236SJaegeuk Kim #define file_keep_isize(inode)	is_file(inode, FADVISE_KEEP_SIZE_BIT)
69126787236SJaegeuk Kim #define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
6921153db09SChao Yu 
693b6a06cbbSChao Yu #define file_is_hot(inode)	is_file(inode, FADVISE_HOT_BIT)
694b6a06cbbSChao Yu #define file_set_hot(inode)	set_file(inode, FADVISE_HOT_BIT)
695b6a06cbbSChao Yu #define file_clear_hot(inode)	clear_file(inode, FADVISE_HOT_BIT)
6961153db09SChao Yu 
69795ae251fSEric Biggers #define file_is_verity(inode)	is_file(inode, FADVISE_VERITY_BIT)
69895ae251fSEric Biggers #define file_set_verity(inode)	set_file(inode, FADVISE_VERITY_BIT)
699cde4de12SJaegeuk Kim 
700d4dd19ecSJaegeuk Kim #define file_should_truncate(inode)	is_file(inode, FADVISE_TRUNC_BIT)
701d4dd19ecSJaegeuk Kim #define file_need_truncate(inode)	set_file(inode, FADVISE_TRUNC_BIT)
702d4dd19ecSJaegeuk Kim #define file_dont_truncate(inode)	clear_file(inode, FADVISE_TRUNC_BIT)
703d4dd19ecSJaegeuk Kim 
704ab9fa662SJaegeuk Kim #define DEF_DIR_LEVEL		0
705ab9fa662SJaegeuk Kim 
7062ef79ecbSChao Yu enum {
7072ef79ecbSChao Yu 	GC_FAILURE_PIN,
7082ef79ecbSChao Yu 	GC_FAILURE_ATOMIC,
7092ef79ecbSChao Yu 	MAX_GC_FAILURE
7102ef79ecbSChao Yu };
7112ef79ecbSChao Yu 
7127653b9d8SChao Yu /* used for f2fs_inode_info->flags */
7137653b9d8SChao Yu enum {
7147653b9d8SChao Yu 	FI_NEW_INODE,		/* indicate newly allocated inode */
7157653b9d8SChao Yu 	FI_DIRTY_INODE,		/* indicate inode is dirty or not */
7167653b9d8SChao Yu 	FI_AUTO_RECOVER,	/* indicate inode is recoverable */
7177653b9d8SChao Yu 	FI_DIRTY_DIR,		/* indicate directory has dirty pages */
7187653b9d8SChao Yu 	FI_INC_LINK,		/* need to increment i_nlink */
7197653b9d8SChao Yu 	FI_ACL_MODE,		/* indicate acl mode */
7207653b9d8SChao Yu 	FI_NO_ALLOC,		/* should not allocate any blocks */
7217653b9d8SChao Yu 	FI_FREE_NID,		/* free allocated nide */
7227653b9d8SChao Yu 	FI_NO_EXTENT,		/* not to use the extent cache */
7237653b9d8SChao Yu 	FI_INLINE_XATTR,	/* used for inline xattr */
7247653b9d8SChao Yu 	FI_INLINE_DATA,		/* used for inline data*/
7257653b9d8SChao Yu 	FI_INLINE_DENTRY,	/* used for inline dentry */
7267653b9d8SChao Yu 	FI_APPEND_WRITE,	/* inode has appended data */
7277653b9d8SChao Yu 	FI_UPDATE_WRITE,	/* inode has in-place-update data */
7287653b9d8SChao Yu 	FI_NEED_IPU,		/* used for ipu per file */
7297653b9d8SChao Yu 	FI_ATOMIC_FILE,		/* indicate atomic file */
7307653b9d8SChao Yu 	FI_ATOMIC_COMMIT,	/* indicate the state of atomical committing */
7317653b9d8SChao Yu 	FI_VOLATILE_FILE,	/* indicate volatile file */
7327653b9d8SChao Yu 	FI_FIRST_BLOCK_WRITTEN,	/* indicate #0 data block was written */
7337653b9d8SChao Yu 	FI_DROP_CACHE,		/* drop dirty page cache */
7347653b9d8SChao Yu 	FI_DATA_EXIST,		/* indicate data exists */
7357653b9d8SChao Yu 	FI_INLINE_DOTS,		/* indicate inline dot dentries */
7367653b9d8SChao Yu 	FI_DO_DEFRAG,		/* indicate defragment is running */
7377653b9d8SChao Yu 	FI_DIRTY_FILE,		/* indicate regular/symlink has dirty pages */
7383d697a4aSEric Biggers 	FI_PREALLOCATED_ALL,	/* all blocks for write were preallocated */
7397653b9d8SChao Yu 	FI_HOT_DATA,		/* indicate file is hot */
7407653b9d8SChao Yu 	FI_EXTRA_ATTR,		/* indicate file has extra attribute */
7417653b9d8SChao Yu 	FI_PROJ_INHERIT,	/* indicate file inherits projectid */
7427653b9d8SChao Yu 	FI_PIN_FILE,		/* indicate file should not be gced */
7437653b9d8SChao Yu 	FI_ATOMIC_REVOKE_REQUEST, /* request to drop atomic data */
7447653b9d8SChao Yu 	FI_VERITY_IN_PROGRESS,	/* building fs-verity Merkle tree */
7457653b9d8SChao Yu 	FI_COMPRESSED_FILE,	/* indicate file's data can be compressed */
746b28f047bSChao Yu 	FI_COMPRESS_CORRUPT,	/* indicate compressed cluster is corrupted */
7477653b9d8SChao Yu 	FI_MMAP_FILE,		/* indicate file was mmapped */
748602a16d5SDaeho Jeong 	FI_ENABLE_COMPRESS,	/* enable compression in "user" compression mode */
749c6140415SJaegeuk Kim 	FI_COMPRESS_RELEASED,	/* compressed blocks were released */
750859fca6bSChao Yu 	FI_ALIGNED_WRITE,	/* enable aligned write */
7517653b9d8SChao Yu 	FI_MAX,			/* max flag, never be used */
7527653b9d8SChao Yu };
7537653b9d8SChao Yu 
75439a53e0cSJaegeuk Kim struct f2fs_inode_info {
75539a53e0cSJaegeuk Kim 	struct inode vfs_inode;		/* serve a vfs inode */
75639a53e0cSJaegeuk Kim 	unsigned long i_flags;		/* keep an inode flags for ioctl */
75739a53e0cSJaegeuk Kim 	unsigned char i_advise;		/* use to give file attribute hints */
75838431545SJaegeuk Kim 	unsigned char i_dir_level;	/* use for dentry level for large dir */
7591ad71a27SJaegeuk Kim 	unsigned int i_current_depth;	/* only for directory depth */
7602ef79ecbSChao Yu 	/* for gc failure statistic */
7612ef79ecbSChao Yu 	unsigned int i_gc_failures[MAX_GC_FAILURE];
7626666e6aaSJaegeuk Kim 	unsigned int i_pino;		/* parent inode number */
76339a53e0cSJaegeuk Kim 	umode_t i_acl_mode;		/* keep file acl mode temporarily */
76439a53e0cSJaegeuk Kim 
76539a53e0cSJaegeuk Kim 	/* Use below internally in f2fs*/
7667653b9d8SChao Yu 	unsigned long flags[BITS_TO_LONGS(FI_MAX)];	/* use to pass per-file flags */
767*e4544b63STim Murray 	struct f2fs_rwsem i_sem;	/* protect fi info */
768204706c7SJaegeuk Kim 	atomic_t dirty_pages;		/* # of dirty pages */
76939a53e0cSJaegeuk Kim 	f2fs_hash_t chash;		/* hash value of given file name */
77039a53e0cSJaegeuk Kim 	unsigned int clevel;		/* maximum level of given file name */
77188c5c13aSJaegeuk Kim 	struct task_struct *task;	/* lookup and create consistency */
772b0af6d49SChao Yu 	struct task_struct *cp_task;	/* separate cp/wb IO stats*/
77339a53e0cSJaegeuk Kim 	nid_t i_xattr_nid;		/* node id that contains xattrs */
77426de9b11SJaegeuk Kim 	loff_t	last_disk_size;		/* lastly written file size */
775c10c9820SChao Yu 	spinlock_t i_size_lock;		/* protect last_disk_size */
77688b88a66SJaegeuk Kim 
7770abd675eSChao Yu #ifdef CONFIG_QUOTA
7780abd675eSChao Yu 	struct dquot *i_dquot[MAXQUOTAS];
7790abd675eSChao Yu 
7800abd675eSChao Yu 	/* quota space reservation, managed internally by quota code */
7810abd675eSChao Yu 	qsize_t i_reserved_quota;
7820abd675eSChao Yu #endif
7830f18b462SJaegeuk Kim 	struct list_head dirty_list;	/* dirty list for dirs and files */
7840f18b462SJaegeuk Kim 	struct list_head gdirty_list;	/* linked in global dirty list */
78557864ae5SJaegeuk Kim 	struct list_head inmem_ilist;	/* list for inmem inodes */
78688b88a66SJaegeuk Kim 	struct list_head inmem_pages;	/* inmemory pages managed by f2fs */
7877a10f017SJaegeuk Kim 	struct task_struct *inmem_task;	/* store inmemory task */
78888b88a66SJaegeuk Kim 	struct mutex inmem_lock;	/* lock for inmemory pages */
7893e72f721SJaegeuk Kim 	struct extent_tree *extent_tree;	/* cached extent_tree entry */
790b2532c69SChao Yu 
791b2532c69SChao Yu 	/* avoid racing between foreground op and gc */
792*e4544b63STim Murray 	struct f2fs_rwsem i_gc_rwsem[2];
793*e4544b63STim Murray 	struct f2fs_rwsem i_xattr_sem; /* avoid racing between reading and changing EAs */
794f2470371SChao Yu 
7957a2af766SChao Yu 	int i_extra_isize;		/* size of extra space located in i_addr */
7965c57132eSChao Yu 	kprojid_t i_projid;		/* id for project quota */
7976afc662eSChao Yu 	int i_inline_xattr_size;	/* inline xattr size */
79824b81dfcSArnd Bergmann 	struct timespec64 i_crtime;	/* inode creation time */
79924b81dfcSArnd Bergmann 	struct timespec64 i_disk_time[4];/* inode disk times */
8004c8ff709SChao Yu 
8014c8ff709SChao Yu 	/* for file compress */
802c2759ebaSDaeho Jeong 	atomic_t i_compr_blocks;		/* # of compressed blocks */
8034c8ff709SChao Yu 	unsigned char i_compress_algorithm;	/* algorithm type */
8044c8ff709SChao Yu 	unsigned char i_log_cluster_size;	/* log of cluster size */
8053fde13f8SChao Yu 	unsigned char i_compress_level;		/* compress level (lz4hc,zstd) */
806b28f047bSChao Yu 	unsigned short i_compress_flag;		/* compress flag */
8074c8ff709SChao Yu 	unsigned int i_cluster_size;		/* cluster size */
80839a53e0cSJaegeuk Kim };
80939a53e0cSJaegeuk Kim 
81039a53e0cSJaegeuk Kim static inline void get_extent_info(struct extent_info *ext,
811bd933d4fSChao Yu 					struct f2fs_extent *i_ext)
81239a53e0cSJaegeuk Kim {
813bd933d4fSChao Yu 	ext->fofs = le32_to_cpu(i_ext->fofs);
814bd933d4fSChao Yu 	ext->blk = le32_to_cpu(i_ext->blk);
815bd933d4fSChao Yu 	ext->len = le32_to_cpu(i_ext->len);
81639a53e0cSJaegeuk Kim }
81739a53e0cSJaegeuk Kim 
81839a53e0cSJaegeuk Kim static inline void set_raw_extent(struct extent_info *ext,
81939a53e0cSJaegeuk Kim 					struct f2fs_extent *i_ext)
82039a53e0cSJaegeuk Kim {
82139a53e0cSJaegeuk Kim 	i_ext->fofs = cpu_to_le32(ext->fofs);
8224d0b0bd4SChao Yu 	i_ext->blk = cpu_to_le32(ext->blk);
82339a53e0cSJaegeuk Kim 	i_ext->len = cpu_to_le32(ext->len);
82439a53e0cSJaegeuk Kim }
82539a53e0cSJaegeuk Kim 
826429511cdSChao Yu static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
827429511cdSChao Yu 						u32 blk, unsigned int len)
828429511cdSChao Yu {
829429511cdSChao Yu 	ei->fofs = fofs;
830429511cdSChao Yu 	ei->blk = blk;
831429511cdSChao Yu 	ei->len = len;
83294afd6d6SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
83394afd6d6SChao Yu 	ei->c_len = 0;
83494afd6d6SChao Yu #endif
835429511cdSChao Yu }
836429511cdSChao Yu 
837004b6862SChao Yu static inline bool __is_discard_mergeable(struct discard_info *back,
83835ec7d57SChao Yu 			struct discard_info *front, unsigned int max_len)
839004b6862SChao Yu {
8409a997188SJaegeuk Kim 	return (back->lstart + back->len == front->lstart) &&
84135ec7d57SChao Yu 		(back->len + front->len <= max_len);
842004b6862SChao Yu }
843004b6862SChao Yu 
844004b6862SChao Yu static inline bool __is_discard_back_mergeable(struct discard_info *cur,
84535ec7d57SChao Yu 			struct discard_info *back, unsigned int max_len)
846004b6862SChao Yu {
84735ec7d57SChao Yu 	return __is_discard_mergeable(back, cur, max_len);
848004b6862SChao Yu }
849004b6862SChao Yu 
850004b6862SChao Yu static inline bool __is_discard_front_mergeable(struct discard_info *cur,
85135ec7d57SChao Yu 			struct discard_info *front, unsigned int max_len)
852004b6862SChao Yu {
85335ec7d57SChao Yu 	return __is_discard_mergeable(cur, front, max_len);
854004b6862SChao Yu }
855004b6862SChao Yu 
856429511cdSChao Yu static inline bool __is_extent_mergeable(struct extent_info *back,
857429511cdSChao Yu 						struct extent_info *front)
858429511cdSChao Yu {
85994afd6d6SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
86094afd6d6SChao Yu 	if (back->c_len && back->len != back->c_len)
86194afd6d6SChao Yu 		return false;
86294afd6d6SChao Yu 	if (front->c_len && front->len != front->c_len)
86394afd6d6SChao Yu 		return false;
86494afd6d6SChao Yu #endif
865429511cdSChao Yu 	return (back->fofs + back->len == front->fofs &&
866429511cdSChao Yu 			back->blk + back->len == front->blk);
867429511cdSChao Yu }
868429511cdSChao Yu 
869429511cdSChao Yu static inline bool __is_back_mergeable(struct extent_info *cur,
870429511cdSChao Yu 						struct extent_info *back)
871429511cdSChao Yu {
872429511cdSChao Yu 	return __is_extent_mergeable(back, cur);
873429511cdSChao Yu }
874429511cdSChao Yu 
875429511cdSChao Yu static inline bool __is_front_mergeable(struct extent_info *cur,
876429511cdSChao Yu 						struct extent_info *front)
877429511cdSChao Yu {
878429511cdSChao Yu 	return __is_extent_mergeable(cur, front);
879429511cdSChao Yu }
880429511cdSChao Yu 
881cac5a3d8SDongOh Shin extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
882b430f726SZhikang Zhang static inline void __try_update_largest_extent(struct extent_tree *et,
883b430f726SZhikang Zhang 						struct extent_node *en)
8844abd3f5aSChao Yu {
885205b9822SJaegeuk Kim 	if (en->ei.len > et->largest.len) {
8864abd3f5aSChao Yu 		et->largest = en->ei;
887b430f726SZhikang Zhang 		et->largest_updated = true;
888205b9822SJaegeuk Kim 	}
8894abd3f5aSChao Yu }
8904abd3f5aSChao Yu 
8919a4ffdf5SChao Yu /*
8929a4ffdf5SChao Yu  * For free nid management
8939a4ffdf5SChao Yu  */
8949a4ffdf5SChao Yu enum nid_state {
8959a4ffdf5SChao Yu 	FREE_NID,		/* newly added to free nid list */
8969a4ffdf5SChao Yu 	PREALLOC_NID,		/* it is preallocated */
8979a4ffdf5SChao Yu 	MAX_NID_STATE,
898b8559dc2SChao Yu };
899b8559dc2SChao Yu 
900a95ba66aSJaegeuk Kim enum nat_state {
901a95ba66aSJaegeuk Kim 	TOTAL_NAT,
902a95ba66aSJaegeuk Kim 	DIRTY_NAT,
903a95ba66aSJaegeuk Kim 	RECLAIMABLE_NAT,
904a95ba66aSJaegeuk Kim 	MAX_NAT_STATE,
905a95ba66aSJaegeuk Kim };
906a95ba66aSJaegeuk Kim 
90739a53e0cSJaegeuk Kim struct f2fs_nm_info {
90839a53e0cSJaegeuk Kim 	block_t nat_blkaddr;		/* base disk address of NAT */
90939a53e0cSJaegeuk Kim 	nid_t max_nid;			/* maximum possible node ids */
91004d47e67SChao Yu 	nid_t available_nids;		/* # of available node ids */
91139a53e0cSJaegeuk Kim 	nid_t next_scan_nid;		/* the next nid to be scanned */
912cdfc41c1SJaegeuk Kim 	unsigned int ram_thresh;	/* control the memory footprint */
913ea1a29a0SChao Yu 	unsigned int ra_nid_pages;	/* # of nid pages to be readaheaded */
9142304cb0cSChao Yu 	unsigned int dirty_nats_ratio;	/* control dirty nats ratio threshold */
91539a53e0cSJaegeuk Kim 
91639a53e0cSJaegeuk Kim 	/* NAT cache management */
91739a53e0cSJaegeuk Kim 	struct radix_tree_root nat_root;/* root of the nat entry cache */
918309cc2b6SJaegeuk Kim 	struct radix_tree_root nat_set_root;/* root of the nat set cache */
919*e4544b63STim Murray 	struct f2fs_rwsem nat_tree_lock;	/* protect nat entry tree */
92039a53e0cSJaegeuk Kim 	struct list_head nat_entries;	/* cached nat entry list (clean) */
92122969158SChao Yu 	spinlock_t nat_list_lock;	/* protect clean nat entry list */
922a95ba66aSJaegeuk Kim 	unsigned int nat_cnt[MAX_NAT_STATE]; /* the # of cached nat entries */
92322ad0b6aSJaegeuk Kim 	unsigned int nat_blocks;	/* # of nat blocks */
92439a53e0cSJaegeuk Kim 
92539a53e0cSJaegeuk Kim 	/* free node ids management */
9268a7ed66aSJaegeuk Kim 	struct radix_tree_root free_nid_root;/* root of the free_nid cache */
9279a4ffdf5SChao Yu 	struct list_head free_nid_list;		/* list for free nids excluding preallocated nids */
9289a4ffdf5SChao Yu 	unsigned int nid_cnt[MAX_NID_STATE];	/* the number of free node id */
929b8559dc2SChao Yu 	spinlock_t nid_list_lock;	/* protect nid lists ops */
93039a53e0cSJaegeuk Kim 	struct mutex build_lock;	/* lock for build free nids */
931bb1105e4SJaegeuk Kim 	unsigned char **free_nid_bitmap;
9324ac91242SChao Yu 	unsigned char *nat_block_bitmap;
933586d1492SChao Yu 	unsigned short *free_nid_count;	/* free nid count of NAT block */
93439a53e0cSJaegeuk Kim 
93539a53e0cSJaegeuk Kim 	/* for checkpoint */
93639a53e0cSJaegeuk Kim 	char *nat_bitmap;		/* NAT bitmap pointer */
93722ad0b6aSJaegeuk Kim 
93822ad0b6aSJaegeuk Kim 	unsigned int nat_bits_blocks;	/* # of nat bits blocks */
93922ad0b6aSJaegeuk Kim 	unsigned char *nat_bits;	/* NAT bits blocks */
94022ad0b6aSJaegeuk Kim 	unsigned char *full_nat_bits;	/* full NAT pages */
94122ad0b6aSJaegeuk Kim 	unsigned char *empty_nat_bits;	/* empty NAT pages */
942599a09b2SChao Yu #ifdef CONFIG_F2FS_CHECK_FS
943599a09b2SChao Yu 	char *nat_bitmap_mir;		/* NAT bitmap mirror */
944599a09b2SChao Yu #endif
94539a53e0cSJaegeuk Kim 	int bitmap_size;		/* bitmap size */
94639a53e0cSJaegeuk Kim };
94739a53e0cSJaegeuk Kim 
94839a53e0cSJaegeuk Kim /*
94939a53e0cSJaegeuk Kim  * this structure is used as one of function parameters.
95039a53e0cSJaegeuk Kim  * all the information are dedicated to a given direct node block determined
95139a53e0cSJaegeuk Kim  * by the data offset in a file.
95239a53e0cSJaegeuk Kim  */
95339a53e0cSJaegeuk Kim struct dnode_of_data {
95439a53e0cSJaegeuk Kim 	struct inode *inode;		/* vfs inode pointer */
95539a53e0cSJaegeuk Kim 	struct page *inode_page;	/* its inode page, NULL is possible */
95639a53e0cSJaegeuk Kim 	struct page *node_page;		/* cached direct node page */
95739a53e0cSJaegeuk Kim 	nid_t nid;			/* node id of the direct node block */
95839a53e0cSJaegeuk Kim 	unsigned int ofs_in_node;	/* data offset in the node page */
95939a53e0cSJaegeuk Kim 	bool inode_page_locked;		/* inode page is locked or not */
96093bae099SJaegeuk Kim 	bool node_changed;		/* is node block changed */
9613cf45747SChao Yu 	char cur_level;			/* level of hole node page */
9623cf45747SChao Yu 	char max_level;			/* level of current page located */
96339a53e0cSJaegeuk Kim 	block_t	data_blkaddr;		/* block address of the node block */
96439a53e0cSJaegeuk Kim };
96539a53e0cSJaegeuk Kim 
96639a53e0cSJaegeuk Kim static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
96739a53e0cSJaegeuk Kim 		struct page *ipage, struct page *npage, nid_t nid)
96839a53e0cSJaegeuk Kim {
969d66d1f76SJaegeuk Kim 	memset(dn, 0, sizeof(*dn));
97039a53e0cSJaegeuk Kim 	dn->inode = inode;
97139a53e0cSJaegeuk Kim 	dn->inode_page = ipage;
97239a53e0cSJaegeuk Kim 	dn->node_page = npage;
97339a53e0cSJaegeuk Kim 	dn->nid = nid;
97439a53e0cSJaegeuk Kim }
97539a53e0cSJaegeuk Kim 
97639a53e0cSJaegeuk Kim /*
97739a53e0cSJaegeuk Kim  * For SIT manager
97839a53e0cSJaegeuk Kim  *
97939a53e0cSJaegeuk Kim  * By default, there are 6 active log areas across the whole main area.
98039a53e0cSJaegeuk Kim  * When considering hot and cold data separation to reduce cleaning overhead,
98139a53e0cSJaegeuk Kim  * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
98239a53e0cSJaegeuk Kim  * respectively.
98339a53e0cSJaegeuk Kim  * In the current design, you should not change the numbers intentionally.
98439a53e0cSJaegeuk Kim  * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
98539a53e0cSJaegeuk Kim  * logs individually according to the underlying devices. (default: 6)
98639a53e0cSJaegeuk Kim  * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
98739a53e0cSJaegeuk Kim  * data and 8 for node logs.
98839a53e0cSJaegeuk Kim  */
98939a53e0cSJaegeuk Kim #define	NR_CURSEG_DATA_TYPE	(3)
99039a53e0cSJaegeuk Kim #define NR_CURSEG_NODE_TYPE	(3)
991093749e2SChao Yu #define NR_CURSEG_INMEM_TYPE	(2)
992a7d9fe3cSJaegeuk Kim #define NR_CURSEG_RO_TYPE	(2)
993d0b9e42aSChao Yu #define NR_CURSEG_PERSIST_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
994d0b9e42aSChao Yu #define NR_CURSEG_TYPE		(NR_CURSEG_INMEM_TYPE + NR_CURSEG_PERSIST_TYPE)
99539a53e0cSJaegeuk Kim 
99639a53e0cSJaegeuk Kim enum {
99739a53e0cSJaegeuk Kim 	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
99839a53e0cSJaegeuk Kim 	CURSEG_WARM_DATA,	/* data blocks */
99939a53e0cSJaegeuk Kim 	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
100039a53e0cSJaegeuk Kim 	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
100139a53e0cSJaegeuk Kim 	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
100239a53e0cSJaegeuk Kim 	CURSEG_COLD_NODE,	/* indirect node blocks */
1003d0b9e42aSChao Yu 	NR_PERSISTENT_LOG,	/* number of persistent log */
1004d0b9e42aSChao Yu 	CURSEG_COLD_DATA_PINNED = NR_PERSISTENT_LOG,
1005d0b9e42aSChao Yu 				/* pinned file that needs consecutive block address */
1006093749e2SChao Yu 	CURSEG_ALL_DATA_ATGC,	/* SSR alloctor in hot/warm/cold data area */
1007d0b9e42aSChao Yu 	NO_CHECK_TYPE,		/* number of persistent & inmem log */
100839a53e0cSJaegeuk Kim };
100939a53e0cSJaegeuk Kim 
10106b4afdd7SJaegeuk Kim struct flush_cmd {
10116b4afdd7SJaegeuk Kim 	struct completion wait;
1012721bd4d5SGu Zheng 	struct llist_node llnode;
101339d787beSChao Yu 	nid_t ino;
10146b4afdd7SJaegeuk Kim 	int ret;
10156b4afdd7SJaegeuk Kim };
10166b4afdd7SJaegeuk Kim 
1017a688b9d9SGu Zheng struct flush_cmd_control {
1018a688b9d9SGu Zheng 	struct task_struct *f2fs_issue_flush;	/* flush thread */
1019a688b9d9SGu Zheng 	wait_queue_head_t flush_wait_queue;	/* waiting queue for wake-up */
10208b8dd65fSChao Yu 	atomic_t issued_flush;			/* # of issued flushes */
102172691af6SJaegeuk Kim 	atomic_t queued_flush;			/* # of queued flushes */
1022721bd4d5SGu Zheng 	struct llist_head issue_list;		/* list for command issue */
1023721bd4d5SGu Zheng 	struct llist_node *dispatch_list;	/* list for command dispatch */
1024a688b9d9SGu Zheng };
1025a688b9d9SGu Zheng 
102639a53e0cSJaegeuk Kim struct f2fs_sm_info {
102739a53e0cSJaegeuk Kim 	struct sit_info *sit_info;		/* whole segment information */
102839a53e0cSJaegeuk Kim 	struct free_segmap_info *free_info;	/* free segment information */
102939a53e0cSJaegeuk Kim 	struct dirty_seglist_info *dirty_info;	/* dirty segment information */
103039a53e0cSJaegeuk Kim 	struct curseg_info *curseg_array;	/* active segment information */
103139a53e0cSJaegeuk Kim 
1032*e4544b63STim Murray 	struct f2fs_rwsem curseg_lock;	/* for preventing curseg change */
10332b60311dSChao Yu 
103439a53e0cSJaegeuk Kim 	block_t seg0_blkaddr;		/* block address of 0'th segment */
103539a53e0cSJaegeuk Kim 	block_t main_blkaddr;		/* start block address of main area */
103639a53e0cSJaegeuk Kim 	block_t ssa_blkaddr;		/* start block address of SSA area */
103739a53e0cSJaegeuk Kim 
103839a53e0cSJaegeuk Kim 	unsigned int segment_count;	/* total # of segments */
103939a53e0cSJaegeuk Kim 	unsigned int main_segments;	/* # of segments in main area */
104039a53e0cSJaegeuk Kim 	unsigned int reserved_segments;	/* # of reserved segments */
1041300a8429SChao Yu 	unsigned int additional_reserved_segments;/* reserved segs for IO align feature */
104239a53e0cSJaegeuk Kim 	unsigned int ovp_segments;	/* # of overprovision segments */
104381eb8d6eSJaegeuk Kim 
104481eb8d6eSJaegeuk Kim 	/* a threshold to reclaim prefree segments */
104581eb8d6eSJaegeuk Kim 	unsigned int rec_prefree_segments;
10467fd9e544SJaegeuk Kim 
1047bba681cbSJaegeuk Kim 	/* for batched trimming */
1048bba681cbSJaegeuk Kim 	unsigned int trim_sections;		/* # of sections to trim */
1049bba681cbSJaegeuk Kim 
1050184a5cd2SChao Yu 	struct list_head sit_entry_set;	/* sit entry set list */
1051184a5cd2SChao Yu 
1052216fbd64SJaegeuk Kim 	unsigned int ipu_policy;	/* in-place-update policy */
1053216fbd64SJaegeuk Kim 	unsigned int min_ipu_util;	/* in-place-update threshold */
1054c1ce1b02SJaegeuk Kim 	unsigned int min_fsync_blocks;	/* threshold for fsync */
1055853137ceSJaegeuk Kim 	unsigned int min_seq_blocks;	/* threshold for sequential blocks */
1056ef095d19SJaegeuk Kim 	unsigned int min_hot_blocks;	/* threshold for hot block allocation */
1057a2a12b67SChao Yu 	unsigned int min_ssr_sections;	/* threshold to trigger SSR allocation */
10586b4afdd7SJaegeuk Kim 
10596b4afdd7SJaegeuk Kim 	/* for flush command control */
1060b01a9201SJaegeuk Kim 	struct flush_cmd_control *fcc_info;
1061a688b9d9SGu Zheng 
10620b54fb84SJaegeuk Kim 	/* for discard command control */
10630b54fb84SJaegeuk Kim 	struct discard_cmd_control *dcc_info;
106439a53e0cSJaegeuk Kim };
106539a53e0cSJaegeuk Kim 
106639a53e0cSJaegeuk Kim /*
106739a53e0cSJaegeuk Kim  * For superblock
106839a53e0cSJaegeuk Kim  */
106939a53e0cSJaegeuk Kim /*
107039a53e0cSJaegeuk Kim  * COUNT_TYPE for monitoring
107139a53e0cSJaegeuk Kim  *
107239a53e0cSJaegeuk Kim  * f2fs monitors the number of several block types such as on-writeback,
107339a53e0cSJaegeuk Kim  * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
107439a53e0cSJaegeuk Kim  */
107536951b38SChao Yu #define WB_DATA_TYPE(p)	(__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
107639a53e0cSJaegeuk Kim enum count_type {
107739a53e0cSJaegeuk Kim 	F2FS_DIRTY_DENTS,
1078c227f912SChao Yu 	F2FS_DIRTY_DATA,
10792c8a4a28SJaegeuk Kim 	F2FS_DIRTY_QDATA,
108039a53e0cSJaegeuk Kim 	F2FS_DIRTY_NODES,
108139a53e0cSJaegeuk Kim 	F2FS_DIRTY_META,
10828dcf2ff7SJaegeuk Kim 	F2FS_INMEM_PAGES,
10830f18b462SJaegeuk Kim 	F2FS_DIRTY_IMETA,
108436951b38SChao Yu 	F2FS_WB_CP_DATA,
108536951b38SChao Yu 	F2FS_WB_DATA,
10865f9abab4SJaegeuk Kim 	F2FS_RD_DATA,
10875f9abab4SJaegeuk Kim 	F2FS_RD_NODE,
10885f9abab4SJaegeuk Kim 	F2FS_RD_META,
108902b16d0aSChao Yu 	F2FS_DIO_WRITE,
109002b16d0aSChao Yu 	F2FS_DIO_READ,
109139a53e0cSJaegeuk Kim 	NR_COUNT_TYPE,
109239a53e0cSJaegeuk Kim };
109339a53e0cSJaegeuk Kim 
109439a53e0cSJaegeuk Kim /*
1095e1c42045Sarter97  * The below are the page types of bios used in submit_bio().
109639a53e0cSJaegeuk Kim  * The available types are:
109739a53e0cSJaegeuk Kim  * DATA			User data pages. It operates as async mode.
109839a53e0cSJaegeuk Kim  * NODE			Node pages. It operates as async mode.
109939a53e0cSJaegeuk Kim  * META			FS metadata pages such as SIT, NAT, CP.
110039a53e0cSJaegeuk Kim  * NR_PAGE_TYPE		The number of page types.
110139a53e0cSJaegeuk Kim  * META_FLUSH		Make sure the previous pages are written
110239a53e0cSJaegeuk Kim  *			with waiting the bio's completion
110339a53e0cSJaegeuk Kim  * ...			Only can be used with META.
110439a53e0cSJaegeuk Kim  */
11057d5e5109SJaegeuk Kim #define PAGE_TYPE_OF_BIO(type)	((type) > META ? META : (type))
110639a53e0cSJaegeuk Kim enum page_type {
110739a53e0cSJaegeuk Kim 	DATA,
110839a53e0cSJaegeuk Kim 	NODE,
110939a53e0cSJaegeuk Kim 	META,
111039a53e0cSJaegeuk Kim 	NR_PAGE_TYPE,
111139a53e0cSJaegeuk Kim 	META_FLUSH,
11128ce67cb0SJaegeuk Kim 	INMEM,		/* the below types are used by tracepoints only. */
11138ce67cb0SJaegeuk Kim 	INMEM_DROP,
11148c242db9SJaegeuk Kim 	INMEM_INVALIDATE,
111528bc106bSChao Yu 	INMEM_REVOKE,
11168ce67cb0SJaegeuk Kim 	IPU,
11178ce67cb0SJaegeuk Kim 	OPU,
111839a53e0cSJaegeuk Kim };
111939a53e0cSJaegeuk Kim 
1120a912b54dSJaegeuk Kim enum temp_type {
1121a912b54dSJaegeuk Kim 	HOT = 0,	/* must be zero for meta bio */
1122a912b54dSJaegeuk Kim 	WARM,
1123a912b54dSJaegeuk Kim 	COLD,
1124a912b54dSJaegeuk Kim 	NR_TEMP_TYPE,
1125a912b54dSJaegeuk Kim };
1126a912b54dSJaegeuk Kim 
1127cc15620bSJaegeuk Kim enum need_lock_type {
1128cc15620bSJaegeuk Kim 	LOCK_REQ = 0,
1129cc15620bSJaegeuk Kim 	LOCK_DONE,
1130cc15620bSJaegeuk Kim 	LOCK_RETRY,
1131cc15620bSJaegeuk Kim };
1132cc15620bSJaegeuk Kim 
1133a5fd5050SChao Yu enum cp_reason_type {
1134a5fd5050SChao Yu 	CP_NO_NEEDED,
1135a5fd5050SChao Yu 	CP_NON_REGULAR,
11364c8ff709SChao Yu 	CP_COMPRESSED,
1137a5fd5050SChao Yu 	CP_HARDLINK,
1138a5fd5050SChao Yu 	CP_SB_NEED_CP,
1139a5fd5050SChao Yu 	CP_WRONG_PINO,
1140a5fd5050SChao Yu 	CP_NO_SPC_ROLL,
1141a5fd5050SChao Yu 	CP_NODE_NEED_CP,
1142a5fd5050SChao Yu 	CP_FASTBOOT_MODE,
1143a5fd5050SChao Yu 	CP_SPEC_LOG_NUM,
11440a007b97SJaegeuk Kim 	CP_RECOVER_DIR,
1145a5fd5050SChao Yu };
1146a5fd5050SChao Yu 
1147b0af6d49SChao Yu enum iostat_type {
11488b83ac81SChao Yu 	/* WRITE IO */
11498b83ac81SChao Yu 	APP_DIRECT_IO,			/* app direct write IOs */
11508b83ac81SChao Yu 	APP_BUFFERED_IO,		/* app buffered write IOs */
1151b0af6d49SChao Yu 	APP_WRITE_IO,			/* app write IOs */
1152b0af6d49SChao Yu 	APP_MAPPED_IO,			/* app mapped IOs */
1153b0af6d49SChao Yu 	FS_DATA_IO,			/* data IOs from kworker/fsync/reclaimer */
1154b0af6d49SChao Yu 	FS_NODE_IO,			/* node IOs from kworker/fsync/reclaimer */
1155b0af6d49SChao Yu 	FS_META_IO,			/* meta IOs from kworker/reclaimer */
1156b0af6d49SChao Yu 	FS_GC_DATA_IO,			/* data IOs from forground gc */
1157b0af6d49SChao Yu 	FS_GC_NODE_IO,			/* node IOs from forground gc */
1158b0af6d49SChao Yu 	FS_CP_DATA_IO,			/* data IOs from checkpoint */
1159b0af6d49SChao Yu 	FS_CP_NODE_IO,			/* node IOs from checkpoint */
1160b0af6d49SChao Yu 	FS_CP_META_IO,			/* meta IOs from checkpoint */
11618b83ac81SChao Yu 
11628b83ac81SChao Yu 	/* READ IO */
11638b83ac81SChao Yu 	APP_DIRECT_READ_IO,		/* app direct read IOs */
11648b83ac81SChao Yu 	APP_BUFFERED_READ_IO,		/* app buffered read IOs */
11658b83ac81SChao Yu 	APP_READ_IO,			/* app read IOs */
11668b83ac81SChao Yu 	APP_MAPPED_READ_IO,		/* app mapped read IOs */
11678b83ac81SChao Yu 	FS_DATA_READ_IO,		/* data read IOs */
11689c122384SChao Yu 	FS_GDATA_READ_IO,		/* data read IOs from background gc */
11699c122384SChao Yu 	FS_CDATA_READ_IO,		/* compressed data read IOs */
11708b83ac81SChao Yu 	FS_NODE_READ_IO,		/* node read IOs */
11718b83ac81SChao Yu 	FS_META_READ_IO,		/* meta read IOs */
11728b83ac81SChao Yu 
11738b83ac81SChao Yu 	/* other */
1174b0af6d49SChao Yu 	FS_DISCARD,			/* discard */
1175b0af6d49SChao Yu 	NR_IO_TYPE,
1176b0af6d49SChao Yu };
1177b0af6d49SChao Yu 
1178458e6197SJaegeuk Kim struct f2fs_io_info {
117905ca3632SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs_sb_info pointer */
118039d787beSChao Yu 	nid_t ino;		/* inode number */
1181458e6197SJaegeuk Kim 	enum page_type type;	/* contains DATA/NODE/META/META_FLUSH */
1182a912b54dSJaegeuk Kim 	enum temp_type temp;	/* contains HOT/WARM/COLD */
118304d328deSMike Christie 	int op;			/* contains REQ_OP_ */
1184ef295ecfSChristoph Hellwig 	int op_flags;		/* req_flag_bits */
11857a9d7548SChao Yu 	block_t new_blkaddr;	/* new block address to be written */
118628bc106bSChao Yu 	block_t old_blkaddr;	/* old block address before Cow */
118705ca3632SJaegeuk Kim 	struct page *page;	/* page to be written */
11884375a336SJaegeuk Kim 	struct page *encrypted_page;	/* encrypted page */
11894c8ff709SChao Yu 	struct page *compressed_page;	/* compressed page */
1190fb830fc5SChao Yu 	struct list_head list;		/* serialize IOs */
1191d68f735bSJaegeuk Kim 	bool submitted;		/* indicate IO submission */
1192cc15620bSJaegeuk Kim 	int need_lock;		/* indicate we need to lock cp_rwsem */
1193fb830fc5SChao Yu 	bool in_list;		/* indicate fio is in io_list */
11946dc3a126SChao Yu 	bool is_por;		/* indicate IO is from recovery or not */
1195fe16efe6SChao Yu 	bool retry;		/* need to reallocate block address */
11964c8ff709SChao Yu 	int compr_blocks;	/* # of compressed block addresses */
11974c8ff709SChao Yu 	bool encrypted;		/* indicate file is encrypted */
1198b0af6d49SChao Yu 	enum iostat_type io_type;	/* io type */
1199578c6478SYufen Yu 	struct writeback_control *io_wbc; /* writeback control */
12008648de2cSChao Yu 	struct bio **bio;		/* bio for ipu */
12018648de2cSChao Yu 	sector_t *last_block;		/* last block number in bio */
12027735730dSChao Yu 	unsigned char version;		/* version of the node */
1203458e6197SJaegeuk Kim };
1204458e6197SJaegeuk Kim 
12050b20fcecSChao Yu struct bio_entry {
12060b20fcecSChao Yu 	struct bio *bio;
12070b20fcecSChao Yu 	struct list_head list;
12080b20fcecSChao Yu };
12090b20fcecSChao Yu 
121068afcf2dSTomohiro Kusumi #define is_read_io(rw) ((rw) == READ)
12111ff7bd3bSJaegeuk Kim struct f2fs_bio_info {
1212458e6197SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs superblock */
12131ff7bd3bSJaegeuk Kim 	struct bio *bio;		/* bios to merge */
12141ff7bd3bSJaegeuk Kim 	sector_t last_block_in_bio;	/* last block number */
1215458e6197SJaegeuk Kim 	struct f2fs_io_info fio;	/* store buffered io info. */
1216*e4544b63STim Murray 	struct f2fs_rwsem io_rwsem;	/* blocking op for bio */
1217fb830fc5SChao Yu 	spinlock_t io_lock;		/* serialize DATA/NODE IOs */
1218fb830fc5SChao Yu 	struct list_head io_list;	/* track fios */
12190b20fcecSChao Yu 	struct list_head bio_list;	/* bio entry list head */
1220*e4544b63STim Murray 	struct f2fs_rwsem bio_list_lock;	/* lock to protect bio entry list */
12211ff7bd3bSJaegeuk Kim };
12221ff7bd3bSJaegeuk Kim 
12233c62be17SJaegeuk Kim #define FDEV(i)				(sbi->devs[i])
12243c62be17SJaegeuk Kim #define RDEV(i)				(raw_super->devs[i])
12253c62be17SJaegeuk Kim struct f2fs_dev_info {
12263c62be17SJaegeuk Kim 	struct block_device *bdev;
12273c62be17SJaegeuk Kim 	char path[MAX_PATH_LEN];
12283c62be17SJaegeuk Kim 	unsigned int total_segments;
12293c62be17SJaegeuk Kim 	block_t start_blk;
12303c62be17SJaegeuk Kim 	block_t end_blk;
12313c62be17SJaegeuk Kim #ifdef CONFIG_BLK_DEV_ZONED
12323c62be17SJaegeuk Kim 	unsigned int nr_blkz;		/* Total number of zones */
123395175dafSDamien Le Moal 	unsigned long *blkz_seq;	/* Bitmap indicating sequential zones */
1234de881df9SAravind Ramesh 	block_t *zone_capacity_blocks;  /* Array of zone capacity in blks */
12353c62be17SJaegeuk Kim #endif
12363c62be17SJaegeuk Kim };
12373c62be17SJaegeuk Kim 
1238c227f912SChao Yu enum inode_type {
1239c227f912SChao Yu 	DIR_INODE,			/* for dirty dir inode */
1240c227f912SChao Yu 	FILE_INODE,			/* for dirty regular/symlink inode */
12410f18b462SJaegeuk Kim 	DIRTY_META,			/* for all dirtied inode metadata */
124257864ae5SJaegeuk Kim 	ATOMIC_FILE,			/* for all atomic files */
1243c227f912SChao Yu 	NR_INODE_TYPE,
1244c227f912SChao Yu };
1245c227f912SChao Yu 
124667298804SChao Yu /* for inner inode cache management */
124767298804SChao Yu struct inode_management {
124867298804SChao Yu 	struct radix_tree_root ino_root;	/* ino entry array */
124967298804SChao Yu 	spinlock_t ino_lock;			/* for ino entry lock */
125067298804SChao Yu 	struct list_head ino_list;		/* inode list head */
125167298804SChao Yu 	unsigned long ino_num;			/* number of entries */
125267298804SChao Yu };
125367298804SChao Yu 
1254093749e2SChao Yu /* for GC_AT */
1255093749e2SChao Yu struct atgc_management {
1256093749e2SChao Yu 	bool atgc_enabled;			/* ATGC is enabled or not */
1257093749e2SChao Yu 	struct rb_root_cached root;		/* root of victim rb-tree */
1258093749e2SChao Yu 	struct list_head victim_list;		/* linked with all victim entries */
1259093749e2SChao Yu 	unsigned int victim_count;		/* victim count in rb-tree */
1260093749e2SChao Yu 	unsigned int candidate_ratio;		/* candidate ratio */
1261093749e2SChao Yu 	unsigned int max_candidate_count;	/* max candidate count */
1262093749e2SChao Yu 	unsigned int age_weight;		/* age weight, vblock_weight = 100 - age_weight */
1263093749e2SChao Yu 	unsigned long long age_threshold;	/* age threshold */
1264093749e2SChao Yu };
1265093749e2SChao Yu 
1266caf0047eSChao Yu /* For s_flag in struct f2fs_sb_info */
1267caf0047eSChao Yu enum {
1268caf0047eSChao Yu 	SBI_IS_DIRTY,				/* dirty flag for checkpoint */
1269caf0047eSChao Yu 	SBI_IS_CLOSE,				/* specify unmounting */
1270caf0047eSChao Yu 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
1271caf0047eSChao Yu 	SBI_POR_DOING,				/* recovery is doing or not */
1272df728b0fSJaegeuk Kim 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
1273bbf156f7SJaegeuk Kim 	SBI_NEED_CP,				/* need to checkpoint */
127483a3bfdbSJaegeuk Kim 	SBI_IS_SHUTDOWN,			/* shutdown by ioctl */
12751378752bSChao Yu 	SBI_IS_RECOVERED,			/* recovered orphan/data */
12764354994fSDaniel Rosenberg 	SBI_CP_DISABLED,			/* CP was disabled last mount */
1277db610a64SJaegeuk Kim 	SBI_CP_DISABLED_QUICK,			/* CP was disabled quickly */
1278af033b2aSChao Yu 	SBI_QUOTA_NEED_FLUSH,			/* need to flush quota info in CP */
1279af033b2aSChao Yu 	SBI_QUOTA_SKIP_FLUSH,			/* skip flushing quota in current CP */
1280af033b2aSChao Yu 	SBI_QUOTA_NEED_REPAIR,			/* quota file may be corrupted */
128104f0b2eaSQiuyang Sun 	SBI_IS_RESIZEFS,			/* resizefs is in process */
1282caf0047eSChao Yu };
1283caf0047eSChao Yu 
12846beceb54SJaegeuk Kim enum {
12856beceb54SJaegeuk Kim 	CP_TIME,
1286d0239e1bSJaegeuk Kim 	REQ_TIME,
1287a7d10cf3SSahitya Tummala 	DISCARD_TIME,
1288a7d10cf3SSahitya Tummala 	GC_TIME,
12894354994fSDaniel Rosenberg 	DISABLE_TIME,
129003f2c02dSJaegeuk Kim 	UMOUNT_DISCARD_TIMEOUT,
12916beceb54SJaegeuk Kim 	MAX_TIME,
12926beceb54SJaegeuk Kim };
12936beceb54SJaegeuk Kim 
12940cdd3195SHyunchul Lee enum {
12955b0e9539SJaegeuk Kim 	GC_NORMAL,
12965b0e9539SJaegeuk Kim 	GC_IDLE_CB,
12975b0e9539SJaegeuk Kim 	GC_IDLE_GREEDY,
1298093749e2SChao Yu 	GC_IDLE_AT,
12990e5e8111SDaeho Jeong 	GC_URGENT_HIGH,
13000e5e8111SDaeho Jeong 	GC_URGENT_LOW,
130107c6b593SDaeho Jeong 	MAX_GC_MODE,
13025b0e9539SJaegeuk Kim };
13035b0e9539SJaegeuk Kim 
13045b0e9539SJaegeuk Kim enum {
1305bbbc34fdSChao Yu 	BGGC_MODE_ON,		/* background gc is on */
1306bbbc34fdSChao Yu 	BGGC_MODE_OFF,		/* background gc is off */
1307bbbc34fdSChao Yu 	BGGC_MODE_SYNC,		/*
1308bbbc34fdSChao Yu 				 * background gc is on, migrating blocks
1309bbbc34fdSChao Yu 				 * like foreground gc
1310bbbc34fdSChao Yu 				 */
1311bbbc34fdSChao Yu };
1312bbbc34fdSChao Yu 
1313bbbc34fdSChao Yu enum {
1314b0332a0fSChao Yu 	FS_MODE_ADAPTIVE,		/* use both lfs/ssr allocation */
1315b0332a0fSChao Yu 	FS_MODE_LFS,			/* use lfs allocation only */
13166691d940SDaeho Jeong 	FS_MODE_FRAGMENT_SEG,		/* segment fragmentation mode */
13176691d940SDaeho Jeong 	FS_MODE_FRAGMENT_BLK,		/* block fragmentation mode */
1318b0332a0fSChao Yu };
1319b0332a0fSChao Yu 
1320b0332a0fSChao Yu enum {
13210cdd3195SHyunchul Lee 	WHINT_MODE_OFF,		/* not pass down write hints */
13220cdd3195SHyunchul Lee 	WHINT_MODE_USER,	/* try to pass down hints given by users */
1323f2e703f9SHyunchul Lee 	WHINT_MODE_FS,		/* pass down hints with F2FS policy */
13240cdd3195SHyunchul Lee };
13250cdd3195SHyunchul Lee 
132607939627SJaegeuk Kim enum {
132707939627SJaegeuk Kim 	ALLOC_MODE_DEFAULT,	/* stay default */
132807939627SJaegeuk Kim 	ALLOC_MODE_REUSE,	/* reuse segments as much as possible */
132907939627SJaegeuk Kim };
133007939627SJaegeuk Kim 
133193cf93f1SJunling Zheng enum fsync_mode {
133293cf93f1SJunling Zheng 	FSYNC_MODE_POSIX,	/* fsync follows posix semantics */
133393cf93f1SJunling Zheng 	FSYNC_MODE_STRICT,	/* fsync behaves in line with ext4 */
1334d6290814SJaegeuk Kim 	FSYNC_MODE_NOBARRIER,	/* fsync behaves nobarrier based on posix */
133593cf93f1SJunling Zheng };
133693cf93f1SJunling Zheng 
1337602a16d5SDaeho Jeong enum {
1338602a16d5SDaeho Jeong 	COMPR_MODE_FS,		/*
1339602a16d5SDaeho Jeong 				 * automatically compress compression
1340602a16d5SDaeho Jeong 				 * enabled files
1341602a16d5SDaeho Jeong 				 */
1342602a16d5SDaeho Jeong 	COMPR_MODE_USER,	/*
1343602a16d5SDaeho Jeong 				 * automatical compression is disabled.
1344602a16d5SDaeho Jeong 				 * user can control the file compression
1345602a16d5SDaeho Jeong 				 * using ioctls
1346602a16d5SDaeho Jeong 				 */
1347602a16d5SDaeho Jeong };
1348602a16d5SDaeho Jeong 
13494f993264SChao Yu enum {
13504f993264SChao Yu 	DISCARD_UNIT_BLOCK,	/* basic discard unit is block */
13514f993264SChao Yu 	DISCARD_UNIT_SEGMENT,	/* basic discard unit is segment */
13524f993264SChao Yu 	DISCARD_UNIT_SECTION,	/* basic discard unit is section */
13534f993264SChao Yu };
13544f993264SChao Yu 
1355b763f3beSChao Yu static inline int f2fs_test_bit(unsigned int nr, char *addr);
1356b763f3beSChao Yu static inline void f2fs_set_bit(unsigned int nr, char *addr);
1357b763f3beSChao Yu static inline void f2fs_clear_bit(unsigned int nr, char *addr);
13584c8ff709SChao Yu 
1359b763f3beSChao Yu /*
1360b763f3beSChao Yu  * Layout of f2fs page.private:
1361b763f3beSChao Yu  *
1362b763f3beSChao Yu  * Layout A: lowest bit should be 1
1363b763f3beSChao Yu  * | bit0 = 1 | bit1 | bit2 | ... | bit MAX | private data .... |
1364b763f3beSChao Yu  * bit 0	PAGE_PRIVATE_NOT_POINTER
1365b763f3beSChao Yu  * bit 1	PAGE_PRIVATE_ATOMIC_WRITE
1366b763f3beSChao Yu  * bit 2	PAGE_PRIVATE_DUMMY_WRITE
1367b763f3beSChao Yu  * bit 3	PAGE_PRIVATE_ONGOING_MIGRATION
1368b763f3beSChao Yu  * bit 4	PAGE_PRIVATE_INLINE_INODE
1369b763f3beSChao Yu  * bit 5	PAGE_PRIVATE_REF_RESOURCE
1370b763f3beSChao Yu  * bit 6-	f2fs private data
1371b763f3beSChao Yu  *
1372b763f3beSChao Yu  * Layout B: lowest bit should be 0
1373b763f3beSChao Yu  * page.private is a wrapped pointer.
1374b763f3beSChao Yu  */
1375b763f3beSChao Yu enum {
1376b763f3beSChao Yu 	PAGE_PRIVATE_NOT_POINTER,		/* private contains non-pointer data */
1377b763f3beSChao Yu 	PAGE_PRIVATE_ATOMIC_WRITE,		/* data page from atomic write path */
1378b763f3beSChao Yu 	PAGE_PRIVATE_DUMMY_WRITE,		/* data page for padding aligned IO */
1379b763f3beSChao Yu 	PAGE_PRIVATE_ONGOING_MIGRATION,		/* data page which is on-going migrating */
1380b763f3beSChao Yu 	PAGE_PRIVATE_INLINE_INODE,		/* inode page contains inline data */
1381b763f3beSChao Yu 	PAGE_PRIVATE_REF_RESOURCE,		/* dirty page has referenced resources */
1382b763f3beSChao Yu 	PAGE_PRIVATE_MAX
1383b763f3beSChao Yu };
1384b763f3beSChao Yu 
1385b763f3beSChao Yu #define PAGE_PRIVATE_GET_FUNC(name, flagname) \
1386b763f3beSChao Yu static inline bool page_private_##name(struct page *page) \
1387b763f3beSChao Yu { \
1388c9ebd3dfSJaegeuk Kim 	return PagePrivate(page) && \
1389c9ebd3dfSJaegeuk Kim 		test_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page)) && \
1390b763f3beSChao Yu 		test_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
1391b763f3beSChao Yu }
1392b763f3beSChao Yu 
1393b763f3beSChao Yu #define PAGE_PRIVATE_SET_FUNC(name, flagname) \
1394b763f3beSChao Yu static inline void set_page_private_##name(struct page *page) \
1395b763f3beSChao Yu { \
1396b763f3beSChao Yu 	if (!PagePrivate(page)) { \
1397b763f3beSChao Yu 		get_page(page); \
1398b763f3beSChao Yu 		SetPagePrivate(page); \
1399c9ebd3dfSJaegeuk Kim 		set_page_private(page, 0); \
1400b763f3beSChao Yu 	} \
1401b763f3beSChao Yu 	set_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page)); \
1402b763f3beSChao Yu 	set_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
1403b763f3beSChao Yu }
1404b763f3beSChao Yu 
1405b763f3beSChao Yu #define PAGE_PRIVATE_CLEAR_FUNC(name, flagname) \
1406b763f3beSChao Yu static inline void clear_page_private_##name(struct page *page) \
1407b763f3beSChao Yu { \
1408b763f3beSChao Yu 	clear_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
1409b763f3beSChao Yu 	if (page_private(page) == 1 << PAGE_PRIVATE_NOT_POINTER) { \
1410b763f3beSChao Yu 		set_page_private(page, 0); \
1411b763f3beSChao Yu 		if (PagePrivate(page)) { \
1412b763f3beSChao Yu 			ClearPagePrivate(page); \
1413b763f3beSChao Yu 			put_page(page); \
1414b763f3beSChao Yu 		}\
1415b763f3beSChao Yu 	} \
1416b763f3beSChao Yu }
1417b763f3beSChao Yu 
1418b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(nonpointer, NOT_POINTER);
1419b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(reference, REF_RESOURCE);
1420b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(inline, INLINE_INODE);
1421b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(gcing, ONGOING_MIGRATION);
1422b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(atomic, ATOMIC_WRITE);
1423b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(dummy, DUMMY_WRITE);
1424b763f3beSChao Yu 
1425b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(reference, REF_RESOURCE);
1426b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(inline, INLINE_INODE);
1427b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(gcing, ONGOING_MIGRATION);
1428b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(atomic, ATOMIC_WRITE);
1429b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(dummy, DUMMY_WRITE);
1430b763f3beSChao Yu 
1431b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(reference, REF_RESOURCE);
1432b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(inline, INLINE_INODE);
1433b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(gcing, ONGOING_MIGRATION);
1434b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(atomic, ATOMIC_WRITE);
1435b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(dummy, DUMMY_WRITE);
14364c8ff709SChao Yu 
14376ce19affSChao Yu static inline unsigned long get_page_private_data(struct page *page)
14386ce19affSChao Yu {
14396ce19affSChao Yu 	unsigned long data = page_private(page);
14406ce19affSChao Yu 
14416ce19affSChao Yu 	if (!test_bit(PAGE_PRIVATE_NOT_POINTER, &data))
14426ce19affSChao Yu 		return 0;
14436ce19affSChao Yu 	return data >> PAGE_PRIVATE_MAX;
14446ce19affSChao Yu }
14456ce19affSChao Yu 
14466ce19affSChao Yu static inline void set_page_private_data(struct page *page, unsigned long data)
14476ce19affSChao Yu {
14486ce19affSChao Yu 	if (!PagePrivate(page)) {
14496ce19affSChao Yu 		get_page(page);
14506ce19affSChao Yu 		SetPagePrivate(page);
1451c9ebd3dfSJaegeuk Kim 		set_page_private(page, 0);
14526ce19affSChao Yu 	}
14536ce19affSChao Yu 	set_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page));
14546ce19affSChao Yu 	page_private(page) |= data << PAGE_PRIVATE_MAX;
14556ce19affSChao Yu }
14566ce19affSChao Yu 
14576ce19affSChao Yu static inline void clear_page_private_data(struct page *page)
14586ce19affSChao Yu {
14596ce19affSChao Yu 	page_private(page) &= (1 << PAGE_PRIVATE_MAX) - 1;
14606ce19affSChao Yu 	if (page_private(page) == 1 << PAGE_PRIVATE_NOT_POINTER) {
14616ce19affSChao Yu 		set_page_private(page, 0);
14626ce19affSChao Yu 		if (PagePrivate(page)) {
14636ce19affSChao Yu 			ClearPagePrivate(page);
14646ce19affSChao Yu 			put_page(page);
14656ce19affSChao Yu 		}
14666ce19affSChao Yu 	}
14676ce19affSChao Yu }
14686ce19affSChao Yu 
14694c8ff709SChao Yu /* For compression */
14704c8ff709SChao Yu enum compress_algorithm_type {
14714c8ff709SChao Yu 	COMPRESS_LZO,
14724c8ff709SChao Yu 	COMPRESS_LZ4,
147350cfa66fSChao Yu 	COMPRESS_ZSTD,
14746d92b201SChao Yu 	COMPRESS_LZORLE,
14754c8ff709SChao Yu 	COMPRESS_MAX,
14764c8ff709SChao Yu };
14774c8ff709SChao Yu 
1478b28f047bSChao Yu enum compress_flag {
1479b28f047bSChao Yu 	COMPRESS_CHKSUM,
1480b28f047bSChao Yu 	COMPRESS_MAX_FLAG,
1481b28f047bSChao Yu };
1482b28f047bSChao Yu 
14836ce19affSChao Yu #define	COMPRESS_WATERMARK			20
14846ce19affSChao Yu #define	COMPRESS_PERCENT			20
14856ce19affSChao Yu 
1486b28f047bSChao Yu #define COMPRESS_DATA_RESERVED_SIZE		4
14874c8ff709SChao Yu struct compress_data {
14884c8ff709SChao Yu 	__le32 clen;			/* compressed data size */
1489b28f047bSChao Yu 	__le32 chksum;			/* compressed data chksum */
14904c8ff709SChao Yu 	__le32 reserved[COMPRESS_DATA_RESERVED_SIZE];	/* reserved */
14914c8ff709SChao Yu 	u8 cdata[];			/* compressed data */
14924c8ff709SChao Yu };
14934c8ff709SChao Yu 
14944c8ff709SChao Yu #define COMPRESS_HEADER_SIZE	(sizeof(struct compress_data))
14954c8ff709SChao Yu 
14964c8ff709SChao Yu #define F2FS_COMPRESSED_PAGE_MAGIC	0xF5F2C000
14974c8ff709SChao Yu 
14983fde13f8SChao Yu #define	COMPRESS_LEVEL_OFFSET	8
14993fde13f8SChao Yu 
15004c8ff709SChao Yu /* compress context */
15014c8ff709SChao Yu struct compress_ctx {
15024c8ff709SChao Yu 	struct inode *inode;		/* inode the context belong to */
15034c8ff709SChao Yu 	pgoff_t cluster_idx;		/* cluster index number */
15044c8ff709SChao Yu 	unsigned int cluster_size;	/* page count in cluster */
15054c8ff709SChao Yu 	unsigned int log_cluster_size;	/* log of cluster size */
15064c8ff709SChao Yu 	struct page **rpages;		/* pages store raw data in cluster */
15074c8ff709SChao Yu 	unsigned int nr_rpages;		/* total page number in rpages */
15084c8ff709SChao Yu 	struct page **cpages;		/* pages store compressed data in cluster */
15094c8ff709SChao Yu 	unsigned int nr_cpages;		/* total page number in cpages */
15103271d7ebSFengnan Chang 	unsigned int valid_nr_cpages;	/* valid page number in cpages */
15114c8ff709SChao Yu 	void *rbuf;			/* virtual mapped address on rpages */
15124c8ff709SChao Yu 	struct compress_data *cbuf;	/* virtual mapped address on cpages */
15134c8ff709SChao Yu 	size_t rlen;			/* valid data length in rbuf */
15144c8ff709SChao Yu 	size_t clen;			/* valid data length in cbuf */
15154c8ff709SChao Yu 	void *private;			/* payload buffer for specified compression algorithm */
151650cfa66fSChao Yu 	void *private2;			/* extra payload buffer */
15174c8ff709SChao Yu };
15184c8ff709SChao Yu 
15194c8ff709SChao Yu /* compress context for write IO path */
15204c8ff709SChao Yu struct compress_io_ctx {
15214c8ff709SChao Yu 	u32 magic;			/* magic number to indicate page is compressed */
15224c8ff709SChao Yu 	struct inode *inode;		/* inode the context belong to */
15234c8ff709SChao Yu 	struct page **rpages;		/* pages store raw data in cluster */
15244c8ff709SChao Yu 	unsigned int nr_rpages;		/* total page number in rpages */
1525e6c3948dSChao Yu 	atomic_t pending_pages;		/* in-flight compressed page count */
15264c8ff709SChao Yu };
15274c8ff709SChao Yu 
15287f59b277SEric Biggers /* Context for decompressing one cluster on the read IO path */
15294c8ff709SChao Yu struct decompress_io_ctx {
15304c8ff709SChao Yu 	u32 magic;			/* magic number to indicate page is compressed */
15314c8ff709SChao Yu 	struct inode *inode;		/* inode the context belong to */
15324c8ff709SChao Yu 	pgoff_t cluster_idx;		/* cluster index number */
15334c8ff709SChao Yu 	unsigned int cluster_size;	/* page count in cluster */
15344c8ff709SChao Yu 	unsigned int log_cluster_size;	/* log of cluster size */
15354c8ff709SChao Yu 	struct page **rpages;		/* pages store raw data in cluster */
15364c8ff709SChao Yu 	unsigned int nr_rpages;		/* total page number in rpages */
15374c8ff709SChao Yu 	struct page **cpages;		/* pages store compressed data in cluster */
15384c8ff709SChao Yu 	unsigned int nr_cpages;		/* total page number in cpages */
15394c8ff709SChao Yu 	struct page **tpages;		/* temp pages to pad holes in cluster */
15404c8ff709SChao Yu 	void *rbuf;			/* virtual mapped address on rpages */
15414c8ff709SChao Yu 	struct compress_data *cbuf;	/* virtual mapped address on cpages */
15424c8ff709SChao Yu 	size_t rlen;			/* valid data length in rbuf */
15434c8ff709SChao Yu 	size_t clen;			/* valid data length in cbuf */
15447f59b277SEric Biggers 
15457f59b277SEric Biggers 	/*
15467f59b277SEric Biggers 	 * The number of compressed pages remaining to be read in this cluster.
15477f59b277SEric Biggers 	 * This is initially nr_cpages.  It is decremented by 1 each time a page
15487f59b277SEric Biggers 	 * has been read (or failed to be read).  When it reaches 0, the cluster
15497f59b277SEric Biggers 	 * is decompressed (or an error is reported).
15507f59b277SEric Biggers 	 *
15517f59b277SEric Biggers 	 * If an error occurs before all the pages have been submitted for I/O,
15527f59b277SEric Biggers 	 * then this will never reach 0.  In this case the I/O submitter is
15537f59b277SEric Biggers 	 * responsible for calling f2fs_decompress_end_io() instead.
15547f59b277SEric Biggers 	 */
15557f59b277SEric Biggers 	atomic_t remaining_pages;
15567f59b277SEric Biggers 
15577f59b277SEric Biggers 	/*
15587f59b277SEric Biggers 	 * Number of references to this decompress_io_ctx.
15597f59b277SEric Biggers 	 *
15607f59b277SEric Biggers 	 * One reference is held for I/O completion.  This reference is dropped
15617f59b277SEric Biggers 	 * after the pagecache pages are updated and unlocked -- either after
15627f59b277SEric Biggers 	 * decompression (and verity if enabled), or after an error.
15637f59b277SEric Biggers 	 *
15647f59b277SEric Biggers 	 * In addition, each compressed page holds a reference while it is in a
15657f59b277SEric Biggers 	 * bio.  These references are necessary prevent compressed pages from
15667f59b277SEric Biggers 	 * being freed while they are still in a bio.
15677f59b277SEric Biggers 	 */
15687f59b277SEric Biggers 	refcount_t refcnt;
15697f59b277SEric Biggers 
15707f59b277SEric Biggers 	bool failed;			/* IO error occurred before decompression? */
15717f59b277SEric Biggers 	bool need_verity;		/* need fs-verity verification after decompression? */
157250cfa66fSChao Yu 	void *private;			/* payload buffer for specified decompression algorithm */
157350cfa66fSChao Yu 	void *private2;			/* extra payload buffer */
15747f59b277SEric Biggers 	struct work_struct verity_work;	/* work to verify the decompressed pages */
15754c8ff709SChao Yu };
15764c8ff709SChao Yu 
15774c8ff709SChao Yu #define NULL_CLUSTER			((unsigned int)(~0))
15784c8ff709SChao Yu #define MIN_COMPRESS_LOG_SIZE		2
15794c8ff709SChao Yu #define MAX_COMPRESS_LOG_SIZE		8
15800e2b7385SChao Yu #define MAX_COMPRESS_WINDOW_SIZE(log_size)	((PAGE_SIZE) << (log_size))
15814c8ff709SChao Yu 
158239a53e0cSJaegeuk Kim struct f2fs_sb_info {
158339a53e0cSJaegeuk Kim 	struct super_block *sb;			/* pointer to VFS super block */
15845e176d54SJaegeuk Kim 	struct proc_dir_entry *s_proc;		/* proc entry */
158539a53e0cSJaegeuk Kim 	struct f2fs_super_block *raw_super;	/* raw super block pointer */
1586*e4544b63STim Murray 	struct f2fs_rwsem sb_lock;		/* lock for raw super block */
1587e8240f65SChao Yu 	int valid_super_block;			/* valid super block no */
1588fadb2fb8SChao Yu 	unsigned long s_flag;				/* flags for sbi */
1589853137ceSJaegeuk Kim 	struct mutex writepages;		/* mutex for writepages() */
159039a53e0cSJaegeuk Kim 
1591178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
1592178053e2SDamien Le Moal 	unsigned int blocks_per_blkz;		/* F2FS blocks per zone */
1593178053e2SDamien Le Moal 	unsigned int log_blocks_per_blkz;	/* log2 F2FS blocks per zone */
1594178053e2SDamien Le Moal #endif
1595178053e2SDamien Le Moal 
159639a53e0cSJaegeuk Kim 	/* for node-related operations */
159739a53e0cSJaegeuk Kim 	struct f2fs_nm_info *nm_info;		/* node manager */
159839a53e0cSJaegeuk Kim 	struct inode *node_inode;		/* cache node blocks */
159939a53e0cSJaegeuk Kim 
160039a53e0cSJaegeuk Kim 	/* for segment-related operations */
160139a53e0cSJaegeuk Kim 	struct f2fs_sm_info *sm_info;		/* segment manager */
16021ff7bd3bSJaegeuk Kim 
16031ff7bd3bSJaegeuk Kim 	/* for bio operations */
1604a912b54dSJaegeuk Kim 	struct f2fs_bio_info *write_io[NR_PAGE_TYPE];	/* for write bios */
1605107a805dSChao Yu 	/* keep migration IO order for LFS mode */
1606*e4544b63STim Murray 	struct f2fs_rwsem io_order_lock;
16070a595ebaSJaegeuk Kim 	mempool_t *write_io_dummy;		/* Dummy pages */
160839a53e0cSJaegeuk Kim 
160939a53e0cSJaegeuk Kim 	/* for checkpoint */
161039a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt;		/* raw checkpoint pointer */
16118508e44aSJaegeuk Kim 	int cur_cp_pack;			/* remain current cp pack */
1612aaec2b1dSChao Yu 	spinlock_t cp_lock;			/* for flag in ckpt */
161339a53e0cSJaegeuk Kim 	struct inode *meta_inode;		/* cache meta blocks */
1614*e4544b63STim Murray 	struct f2fs_rwsem cp_global_sem;	/* checkpoint procedure lock */
1615*e4544b63STim Murray 	struct f2fs_rwsem cp_rwsem;		/* blocking FS operations */
1616*e4544b63STim Murray 	struct f2fs_rwsem node_write;		/* locking node writes */
1617*e4544b63STim Murray 	struct f2fs_rwsem node_change;	/* locking node change */
1618fb51b5efSChangman Lee 	wait_queue_head_t cp_wait;
16196beceb54SJaegeuk Kim 	unsigned long last_time[MAX_TIME];	/* to store time in jiffies */
16206beceb54SJaegeuk Kim 	long interval_time[MAX_TIME];		/* to store thresholds */
1621261eeb9cSDaeho Jeong 	struct ckpt_req_control cprc_info;	/* for checkpoint request control */
162239a53e0cSJaegeuk Kim 
162367298804SChao Yu 	struct inode_management im[MAX_INO_ENTRY];	/* manage inode cache */
16246451e041SJaegeuk Kim 
162550fa53ecSChao Yu 	spinlock_t fsync_node_lock;		/* for node entry lock */
162650fa53ecSChao Yu 	struct list_head fsync_node_list;	/* node list head */
162750fa53ecSChao Yu 	unsigned int fsync_seg_id;		/* sequence id */
162850fa53ecSChao Yu 	unsigned int fsync_node_num;		/* number of node entries */
162950fa53ecSChao Yu 
16306451e041SJaegeuk Kim 	/* for orphan inode, use 0'th array */
16310d47c1adSGu Zheng 	unsigned int max_orphans;		/* max orphan inodes */
163239a53e0cSJaegeuk Kim 
1633c227f912SChao Yu 	/* for inode management */
1634c227f912SChao Yu 	struct list_head inode_list[NR_INODE_TYPE];	/* dirty inode list */
1635c227f912SChao Yu 	spinlock_t inode_lock[NR_INODE_TYPE];	/* for dirty inode list lock */
1636040d2bb3SChao Yu 	struct mutex flush_lock;		/* for flush exclusion */
163739a53e0cSJaegeuk Kim 
163813054c54SChao Yu 	/* for extent tree cache */
163913054c54SChao Yu 	struct radix_tree_root extent_tree_root;/* cache extent cache entries */
16405e8256acSYunlei He 	struct mutex extent_tree_lock;	/* locking extent radix tree */
164113054c54SChao Yu 	struct list_head extent_list;		/* lru list for shrinker */
164213054c54SChao Yu 	spinlock_t extent_lock;			/* locking extent lru list */
16437441ccefSJaegeuk Kim 	atomic_t total_ext_tree;		/* extent tree count */
1644137d09f0SJaegeuk Kim 	struct list_head zombie_list;		/* extent zombie tree list */
164574fd8d99SJaegeuk Kim 	atomic_t total_zombie_tree;		/* extent zombie tree count */
164613054c54SChao Yu 	atomic_t total_ext_node;		/* extent info count */
164713054c54SChao Yu 
164839a53e0cSJaegeuk Kim 	/* basic filesystem units */
164939a53e0cSJaegeuk Kim 	unsigned int log_sectors_per_block;	/* log2 sectors per block */
165039a53e0cSJaegeuk Kim 	unsigned int log_blocksize;		/* log2 block size */
165139a53e0cSJaegeuk Kim 	unsigned int blocksize;			/* block size */
165239a53e0cSJaegeuk Kim 	unsigned int root_ino_num;		/* root inode number*/
165339a53e0cSJaegeuk Kim 	unsigned int node_ino_num;		/* node inode number*/
165439a53e0cSJaegeuk Kim 	unsigned int meta_ino_num;		/* meta inode number*/
165539a53e0cSJaegeuk Kim 	unsigned int log_blocks_per_seg;	/* log2 blocks per segment */
165639a53e0cSJaegeuk Kim 	unsigned int blocks_per_seg;		/* blocks per segment */
165739a53e0cSJaegeuk Kim 	unsigned int segs_per_sec;		/* segments per section */
165839a53e0cSJaegeuk Kim 	unsigned int secs_per_zone;		/* sections per zone */
165939a53e0cSJaegeuk Kim 	unsigned int total_sections;		/* total section count */
166039a53e0cSJaegeuk Kim 	unsigned int total_node_count;		/* total node block count */
166139a53e0cSJaegeuk Kim 	unsigned int total_valid_node_count;	/* valid node block count */
1662ab9fa662SJaegeuk Kim 	int dir_level;				/* directory level */
1663f6df8f23SSheng Yong 	int readdir_ra;				/* readahead inode in readdir */
166410208567SJaegeuk Kim 	u64 max_io_bytes;			/* max io bytes to merge IOs */
166539a53e0cSJaegeuk Kim 
166639a53e0cSJaegeuk Kim 	block_t user_block_count;		/* # of user blocks */
166739a53e0cSJaegeuk Kim 	block_t total_valid_block_count;	/* # of valid blocks */
1668a66cdd98SJaegeuk Kim 	block_t discard_blks;			/* discard command candidats */
166939a53e0cSJaegeuk Kim 	block_t last_valid_block_count;		/* for recovery */
1670daeb433eSChao Yu 	block_t reserved_blocks;		/* configurable reserved blocks */
167180d42145SYunlong Song 	block_t current_reserved_blocks;	/* current reserved blocks */
1672daeb433eSChao Yu 
16734354994fSDaniel Rosenberg 	/* Additional tracking for no checkpoint mode */
16744354994fSDaniel Rosenberg 	block_t unusable_block_count;		/* # of blocks saved by last cp */
16754354994fSDaniel Rosenberg 
1676292c196aSChao Yu 	unsigned int nquota_files;		/* # of quota sysfile */
1677*e4544b63STim Murray 	struct f2fs_rwsem quota_sem;		/* blocking cp for flags */
1678292c196aSChao Yu 
1679523be8a6SJaegeuk Kim 	/* # of pages, see count_type */
168035782b23SJaegeuk Kim 	atomic_t nr_pages[NR_COUNT_TYPE];
168141382ec4SJaegeuk Kim 	/* # of allocated blocks */
168241382ec4SJaegeuk Kim 	struct percpu_counter alloc_valid_block_count;
168339a53e0cSJaegeuk Kim 
1684687de7f1SJaegeuk Kim 	/* writeback control */
1685c29fd0c0SChao Yu 	atomic_t wb_sync_req[META];	/* count # of WB_SYNC threads */
1686687de7f1SJaegeuk Kim 
1687513c5f37SJaegeuk Kim 	/* valid inode count */
1688513c5f37SJaegeuk Kim 	struct percpu_counter total_valid_inode_count;
1689513c5f37SJaegeuk Kim 
169039a53e0cSJaegeuk Kim 	struct f2fs_mount_info mount_opt;	/* mount options */
169139a53e0cSJaegeuk Kim 
169239a53e0cSJaegeuk Kim 	/* for cleaning operations */
1693*e4544b63STim Murray 	struct f2fs_rwsem gc_lock;		/*
1694fb24fea7SChao Yu 						 * semaphore for GC, avoid
1695fb24fea7SChao Yu 						 * race between GC and GC or CP
1696fb24fea7SChao Yu 						 */
169739a53e0cSJaegeuk Kim 	struct f2fs_gc_kthread	*gc_thread;	/* GC thread */
1698093749e2SChao Yu 	struct atgc_management am;		/* atgc management */
16995ec4e49fSJaegeuk Kim 	unsigned int cur_victim_sec;		/* current victim section num */
17005b0e9539SJaegeuk Kim 	unsigned int gc_mode;			/* current GC state */
1701e3080b01SChao Yu 	unsigned int next_victim_seg[2];	/* next segment in victim section */
1702325163e9SDaeho Jeong 	spinlock_t gc_urgent_high_lock;
1703325163e9SDaeho Jeong 	bool gc_urgent_high_limited;		/* indicates having limited trial count */
1704325163e9SDaeho Jeong 	unsigned int gc_urgent_high_remaining;	/* remaining trial count for GC_URGENT_HIGH */
17050e5e8111SDaeho Jeong 
17062ef79ecbSChao Yu 	/* for skip statistic */
1707677017d1SSahitya Tummala 	unsigned int atomic_files;		/* # of opened atomic file */
17082ef79ecbSChao Yu 	unsigned long long skipped_atomic_files[2];	/* FG_GC and BG_GC */
17096f8d4455SJaegeuk Kim 	unsigned long long skipped_gc_rwsem;		/* FG_GC only */
171039a53e0cSJaegeuk Kim 
17111ad71a27SJaegeuk Kim 	/* threshold for gc trials on pinned files */
17121ad71a27SJaegeuk Kim 	u64 gc_pin_file_threshold;
1713*e4544b63STim Murray 	struct f2fs_rwsem pin_sem;
17141ad71a27SJaegeuk Kim 
1715b1c57c1cSJaegeuk Kim 	/* maximum # of trials to find a victim segment for SSR and GC */
1716b1c57c1cSJaegeuk Kim 	unsigned int max_victim_search;
1717e3080b01SChao Yu 	/* migration granularity of garbage collection, unit: segment */
1718e3080b01SChao Yu 	unsigned int migration_granularity;
1719b1c57c1cSJaegeuk Kim 
172039a53e0cSJaegeuk Kim 	/*
172139a53e0cSJaegeuk Kim 	 * for stat information.
172239a53e0cSJaegeuk Kim 	 * one is for the LFS mode, and the other is for the SSR mode.
172339a53e0cSJaegeuk Kim 	 */
172435b09d82SNamjae Jeon #ifdef CONFIG_F2FS_STAT_FS
172539a53e0cSJaegeuk Kim 	struct f2fs_stat_info *stat_info;	/* FS status information */
1726b63e7be5SChao Yu 	atomic_t meta_count[META_MAX];		/* # of meta blocks */
172739a53e0cSJaegeuk Kim 	unsigned int segment_count[2];		/* # of allocated segments */
172839a53e0cSJaegeuk Kim 	unsigned int block_count[2];		/* # of allocated blocks */
1729b9a2c252SChangman Lee 	atomic_t inplace_count;		/* # of inplace update */
17305b7ee374SChao Yu 	atomic64_t total_hit_ext;		/* # of lookup extent cache */
17315b7ee374SChao Yu 	atomic64_t read_hit_rbtree;		/* # of hit rbtree extent node */
17325b7ee374SChao Yu 	atomic64_t read_hit_largest;		/* # of hit largest extent node */
17335b7ee374SChao Yu 	atomic64_t read_hit_cached;		/* # of hit cached extent node */
1734d5e8f6c9SChao Yu 	atomic_t inline_xattr;			/* # of inline_xattr inodes */
173503e14d52SChao Yu 	atomic_t inline_inode;			/* # of inline_data inodes */
173603e14d52SChao Yu 	atomic_t inline_dir;			/* # of inline_dentry inodes */
17374c8ff709SChao Yu 	atomic_t compr_inode;			/* # of compressed inodes */
1738ae999bb9SDaeho Jeong 	atomic64_t compr_blocks;		/* # of compressed blocks */
1739648d50baSChao Yu 	atomic_t vw_cnt;			/* # of volatile writes */
174026a28a0cSJaegeuk Kim 	atomic_t max_aw_cnt;			/* max # of atomic writes */
1741648d50baSChao Yu 	atomic_t max_vw_cnt;			/* max # of volatile writes */
1742274bd9baSChao Yu 	unsigned int io_skip_bggc;		/* skip background gc for in-flight IO */
1743274bd9baSChao Yu 	unsigned int other_skip_bggc;		/* skip background gc for other reasons */
174433fbd510SChao Yu 	unsigned int ndirty_inode[NR_INODE_TYPE];	/* # of dirty inodes */
174535b09d82SNamjae Jeon #endif
174639a53e0cSJaegeuk Kim 	spinlock_t stat_lock;			/* lock for stat operations */
1747b59d0baeSNamjae Jeon 
1748da9953b7SJaegeuk Kim 	/* to attach REQ_META|REQ_FUA flags */
1749da9953b7SJaegeuk Kim 	unsigned int data_io_flag;
175032b6aba8SJaegeuk Kim 	unsigned int node_io_flag;
1751b0af6d49SChao Yu 
1752b59d0baeSNamjae Jeon 	/* For sysfs suppport */
17535d4daa57SChao Yu 	struct kobject s_kobj;			/* /sys/fs/f2fs/<devname> */
1754b59d0baeSNamjae Jeon 	struct completion s_kobj_unregister;
17552658e50dSJaegeuk Kim 
17565d4daa57SChao Yu 	struct kobject s_stat_kobj;		/* /sys/fs/f2fs/<devname>/stat */
17575d4daa57SChao Yu 	struct completion s_stat_kobj_unregister;
17585d4daa57SChao Yu 
17594c89b53dSJaegeuk Kim 	struct kobject s_feature_list_kobj;		/* /sys/fs/f2fs/<devname>/feature_list */
17604c89b53dSJaegeuk Kim 	struct completion s_feature_list_kobj_unregister;
17614c89b53dSJaegeuk Kim 
17622658e50dSJaegeuk Kim 	/* For shrinker support */
17632658e50dSJaegeuk Kim 	struct list_head s_list;
176471f2c820SChao Yu 	struct mutex umount_mutex;
176571f2c820SChao Yu 	unsigned int shrinker_run_no;
176671f2c820SChao Yu 
176771f2c820SChao Yu 	/* For multi devices */
17683c62be17SJaegeuk Kim 	int s_ndevs;				/* number of devices */
17693c62be17SJaegeuk Kim 	struct f2fs_dev_info *devs;		/* for device list */
17701228b482SChao Yu 	unsigned int dirty_device;		/* for checkpoint data flush */
17711228b482SChao Yu 	spinlock_t dev_lock;			/* protect dirty_device */
177271f2c820SChao Yu 	bool aligned_blksize;			/* all devices has the same logical blksize */
17738f1dbbbbSShuoran Liu 
17748f1dbbbbSShuoran Liu 	/* For write statistics */
17758f1dbbbbSShuoran Liu 	u64 sectors_written_start;
17768f1dbbbbSShuoran Liu 	u64 kbytes_written;
177743b6573bSKeith Mok 
177843b6573bSKeith Mok 	/* Reference to checksum algorithm driver via cryptoapi */
177943b6573bSKeith Mok 	struct crypto_shash *s_chksum_driver;
17801ecc0c5cSChao Yu 
1781704956ecSChao Yu 	/* Precomputed FS UUID checksum for seeding other checksums */
1782704956ecSChao Yu 	__u32 s_chksum_seed;
17834c8ff709SChao Yu 
17844c8ff709SChao Yu 	struct workqueue_struct *post_read_wq;	/* post read workqueue */
1785a999150fSChao Yu 
1786a999150fSChao Yu 	struct kmem_cache *inline_xattr_slab;	/* inline xattr entry */
1787a999150fSChao Yu 	unsigned int inline_xattr_slab_size;	/* default inline xattr slab size */
178831083031SChao Yu 
178907c6b593SDaeho Jeong 	/* For reclaimed segs statistics per each GC mode */
179007c6b593SDaeho Jeong 	unsigned int gc_segment_mode;		/* GC state for reclaimed segments */
179107c6b593SDaeho Jeong 	unsigned int gc_reclaimed_segs[MAX_GC_MODE];	/* Reclaimed segs for each mode */
179207c6b593SDaeho Jeong 
17930f6b56ecSDaeho Jeong 	unsigned long seq_file_ra_mul;		/* multiplier for ra_pages of seq. files in fadvise */
17940f6b56ecSDaeho Jeong 
17956691d940SDaeho Jeong 	int max_fragment_chunk;			/* max chunk size for block fragmentation mode */
17966691d940SDaeho Jeong 	int max_fragment_hole;			/* max hole size for block fragmentation mode */
17976691d940SDaeho Jeong 
179831083031SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
179931083031SChao Yu 	struct kmem_cache *page_array_slab;	/* page array entry */
180031083031SChao Yu 	unsigned int page_array_slab_size;	/* default page array slab size */
18015ac443e2SDaeho Jeong 
18025ac443e2SDaeho Jeong 	/* For runtime compression statistics */
18035ac443e2SDaeho Jeong 	u64 compr_written_block;
18045ac443e2SDaeho Jeong 	u64 compr_saved_block;
18055ac443e2SDaeho Jeong 	u32 compr_new_inode;
18066ce19affSChao Yu 
18076ce19affSChao Yu 	/* For compressed block cache */
18086ce19affSChao Yu 	struct inode *compress_inode;		/* cache compressed blocks */
18096ce19affSChao Yu 	unsigned int compress_percent;		/* cache page percentage */
18106ce19affSChao Yu 	unsigned int compress_watermark;	/* cache page watermark */
18116ce19affSChao Yu 	atomic_t compress_page_hit;		/* cache hit count */
181231083031SChao Yu #endif
181352118743SDaeho Jeong 
181452118743SDaeho Jeong #ifdef CONFIG_F2FS_IOSTAT
181552118743SDaeho Jeong 	/* For app/fs IO statistics */
181652118743SDaeho Jeong 	spinlock_t iostat_lock;
181752118743SDaeho Jeong 	unsigned long long rw_iostat[NR_IO_TYPE];
181852118743SDaeho Jeong 	unsigned long long prev_rw_iostat[NR_IO_TYPE];
181952118743SDaeho Jeong 	bool iostat_enable;
182052118743SDaeho Jeong 	unsigned long iostat_next_period;
182152118743SDaeho Jeong 	unsigned int iostat_period_ms;
1822a4b68176SDaeho Jeong 
1823a4b68176SDaeho Jeong 	/* For io latency related statistics info in one iostat period */
1824a4b68176SDaeho Jeong 	spinlock_t iostat_lat_lock;
1825a4b68176SDaeho Jeong 	struct iostat_lat_info *iostat_io_lat;
182652118743SDaeho Jeong #endif
182739a53e0cSJaegeuk Kim };
182839a53e0cSJaegeuk Kim 
18291ecc0c5cSChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
1830c45d6002SChao Yu #define f2fs_show_injection_info(sbi, type)					\
1831c45d6002SChao Yu 	printk_ratelimited("%sF2FS-fs (%s) : inject %s in %s of %pS\n",	\
1832c45d6002SChao Yu 		KERN_INFO, sbi->sb->s_id,				\
1833c45d6002SChao Yu 		f2fs_fault_name[type],					\
183455523519SChao Yu 		__func__, __builtin_return_address(0))
18351ecc0c5cSChao Yu static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
18361ecc0c5cSChao Yu {
183763189b78SChao Yu 	struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
18381ecc0c5cSChao Yu 
18391ecc0c5cSChao Yu 	if (!ffi->inject_rate)
18401ecc0c5cSChao Yu 		return false;
18411ecc0c5cSChao Yu 
18421ecc0c5cSChao Yu 	if (!IS_FAULT_SET(ffi, type))
18431ecc0c5cSChao Yu 		return false;
18441ecc0c5cSChao Yu 
18451ecc0c5cSChao Yu 	atomic_inc(&ffi->inject_ops);
18461ecc0c5cSChao Yu 	if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
18471ecc0c5cSChao Yu 		atomic_set(&ffi->inject_ops, 0);
18481ecc0c5cSChao Yu 		return true;
18491ecc0c5cSChao Yu 	}
18501ecc0c5cSChao Yu 	return false;
18511ecc0c5cSChao Yu }
18527fa750a1SArnd Bergmann #else
1853c45d6002SChao Yu #define f2fs_show_injection_info(sbi, type) do { } while (0)
18547fa750a1SArnd Bergmann static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
18557fa750a1SArnd Bergmann {
18567fa750a1SArnd Bergmann 	return false;
18577fa750a1SArnd Bergmann }
18581ecc0c5cSChao Yu #endif
18591ecc0c5cSChao Yu 
18600916878dSDamien Le Moal /*
18610916878dSDamien Le Moal  * Test if the mounted volume is a multi-device volume.
18620916878dSDamien Le Moal  *   - For a single regular disk volume, sbi->s_ndevs is 0.
18630916878dSDamien Le Moal  *   - For a single zoned disk volume, sbi->s_ndevs is 1.
18640916878dSDamien Le Moal  *   - For a multi-device volume, sbi->s_ndevs is always 2 or more.
18650916878dSDamien Le Moal  */
18660916878dSDamien Le Moal static inline bool f2fs_is_multi_device(struct f2fs_sb_info *sbi)
18670916878dSDamien Le Moal {
18680916878dSDamien Le Moal 	return sbi->s_ndevs > 1;
18690916878dSDamien Le Moal }
18700916878dSDamien Le Moal 
18716beceb54SJaegeuk Kim static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
18726beceb54SJaegeuk Kim {
1873a7d10cf3SSahitya Tummala 	unsigned long now = jiffies;
1874a7d10cf3SSahitya Tummala 
1875a7d10cf3SSahitya Tummala 	sbi->last_time[type] = now;
1876a7d10cf3SSahitya Tummala 
1877a7d10cf3SSahitya Tummala 	/* DISCARD_TIME and GC_TIME are based on REQ_TIME */
1878a7d10cf3SSahitya Tummala 	if (type == REQ_TIME) {
1879a7d10cf3SSahitya Tummala 		sbi->last_time[DISCARD_TIME] = now;
1880a7d10cf3SSahitya Tummala 		sbi->last_time[GC_TIME] = now;
1881a7d10cf3SSahitya Tummala 	}
18826beceb54SJaegeuk Kim }
18836beceb54SJaegeuk Kim 
18846beceb54SJaegeuk Kim static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
18856beceb54SJaegeuk Kim {
18866bccfa19SArnd Bergmann 	unsigned long interval = sbi->interval_time[type] * HZ;
18876beceb54SJaegeuk Kim 
18886beceb54SJaegeuk Kim 	return time_after(jiffies, sbi->last_time[type] + interval);
18896beceb54SJaegeuk Kim }
18906beceb54SJaegeuk Kim 
1891a7d10cf3SSahitya Tummala static inline unsigned int f2fs_time_to_wait(struct f2fs_sb_info *sbi,
1892a7d10cf3SSahitya Tummala 						int type)
1893a7d10cf3SSahitya Tummala {
1894a7d10cf3SSahitya Tummala 	unsigned long interval = sbi->interval_time[type] * HZ;
1895a7d10cf3SSahitya Tummala 	unsigned int wait_ms = 0;
1896a7d10cf3SSahitya Tummala 	long delta;
1897a7d10cf3SSahitya Tummala 
1898a7d10cf3SSahitya Tummala 	delta = (sbi->last_time[type] + interval) - jiffies;
1899a7d10cf3SSahitya Tummala 	if (delta > 0)
1900a7d10cf3SSahitya Tummala 		wait_ms = jiffies_to_msecs(delta);
1901a7d10cf3SSahitya Tummala 
1902a7d10cf3SSahitya Tummala 	return wait_ms;
1903a7d10cf3SSahitya Tummala }
1904a7d10cf3SSahitya Tummala 
190539a53e0cSJaegeuk Kim /*
190639a53e0cSJaegeuk Kim  * Inline functions
190739a53e0cSJaegeuk Kim  */
1908416d2dbbSChao Yu static inline u32 __f2fs_crc32(struct f2fs_sb_info *sbi, u32 crc,
1909704956ecSChao Yu 			      const void *address, unsigned int length)
1910704956ecSChao Yu {
1911704956ecSChao Yu 	struct {
1912704956ecSChao Yu 		struct shash_desc shash;
1913704956ecSChao Yu 		char ctx[4];
1914704956ecSChao Yu 	} desc;
1915704956ecSChao Yu 	int err;
1916704956ecSChao Yu 
1917704956ecSChao Yu 	BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver) != sizeof(desc.ctx));
1918704956ecSChao Yu 
1919704956ecSChao Yu 	desc.shash.tfm = sbi->s_chksum_driver;
1920704956ecSChao Yu 	*(u32 *)desc.ctx = crc;
1921704956ecSChao Yu 
1922704956ecSChao Yu 	err = crypto_shash_update(&desc.shash, address, length);
1923704956ecSChao Yu 	BUG_ON(err);
1924704956ecSChao Yu 
1925704956ecSChao Yu 	return *(u32 *)desc.ctx;
1926704956ecSChao Yu }
1927704956ecSChao Yu 
1928416d2dbbSChao Yu static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
1929416d2dbbSChao Yu 			   unsigned int length)
1930416d2dbbSChao Yu {
1931416d2dbbSChao Yu 	return __f2fs_crc32(sbi, F2FS_SUPER_MAGIC, address, length);
1932416d2dbbSChao Yu }
1933416d2dbbSChao Yu 
1934416d2dbbSChao Yu static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
1935416d2dbbSChao Yu 				  void *buf, size_t buf_size)
1936416d2dbbSChao Yu {
1937416d2dbbSChao Yu 	return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
1938416d2dbbSChao Yu }
1939416d2dbbSChao Yu 
1940416d2dbbSChao Yu static inline u32 f2fs_chksum(struct f2fs_sb_info *sbi, u32 crc,
1941416d2dbbSChao Yu 			      const void *address, unsigned int length)
1942416d2dbbSChao Yu {
1943416d2dbbSChao Yu 	return __f2fs_crc32(sbi, crc, address, length);
1944416d2dbbSChao Yu }
1945416d2dbbSChao Yu 
194639a53e0cSJaegeuk Kim static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
194739a53e0cSJaegeuk Kim {
194839a53e0cSJaegeuk Kim 	return container_of(inode, struct f2fs_inode_info, vfs_inode);
194939a53e0cSJaegeuk Kim }
195039a53e0cSJaegeuk Kim 
195139a53e0cSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
195239a53e0cSJaegeuk Kim {
195339a53e0cSJaegeuk Kim 	return sb->s_fs_info;
195439a53e0cSJaegeuk Kim }
195539a53e0cSJaegeuk Kim 
19564081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
19574081363fSJaegeuk Kim {
19584081363fSJaegeuk Kim 	return F2FS_SB(inode->i_sb);
19594081363fSJaegeuk Kim }
19604081363fSJaegeuk Kim 
19614081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
19624081363fSJaegeuk Kim {
19634081363fSJaegeuk Kim 	return F2FS_I_SB(mapping->host);
19644081363fSJaegeuk Kim }
19654081363fSJaegeuk Kim 
19664081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
19674081363fSJaegeuk Kim {
19684969c06aSJaegeuk Kim 	return F2FS_M_SB(page_file_mapping(page));
19694081363fSJaegeuk Kim }
19704081363fSJaegeuk Kim 
197139a53e0cSJaegeuk Kim static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
197239a53e0cSJaegeuk Kim {
197339a53e0cSJaegeuk Kim 	return (struct f2fs_super_block *)(sbi->raw_super);
197439a53e0cSJaegeuk Kim }
197539a53e0cSJaegeuk Kim 
197639a53e0cSJaegeuk Kim static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
197739a53e0cSJaegeuk Kim {
197839a53e0cSJaegeuk Kim 	return (struct f2fs_checkpoint *)(sbi->ckpt);
197939a53e0cSJaegeuk Kim }
198039a53e0cSJaegeuk Kim 
198145590710SGu Zheng static inline struct f2fs_node *F2FS_NODE(struct page *page)
198245590710SGu Zheng {
198345590710SGu Zheng 	return (struct f2fs_node *)page_address(page);
198445590710SGu Zheng }
198545590710SGu Zheng 
198658bfaf44SJaegeuk Kim static inline struct f2fs_inode *F2FS_INODE(struct page *page)
198758bfaf44SJaegeuk Kim {
198858bfaf44SJaegeuk Kim 	return &((struct f2fs_node *)page_address(page))->i;
198958bfaf44SJaegeuk Kim }
199058bfaf44SJaegeuk Kim 
199139a53e0cSJaegeuk Kim static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
199239a53e0cSJaegeuk Kim {
199339a53e0cSJaegeuk Kim 	return (struct f2fs_nm_info *)(sbi->nm_info);
199439a53e0cSJaegeuk Kim }
199539a53e0cSJaegeuk Kim 
199639a53e0cSJaegeuk Kim static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
199739a53e0cSJaegeuk Kim {
199839a53e0cSJaegeuk Kim 	return (struct f2fs_sm_info *)(sbi->sm_info);
199939a53e0cSJaegeuk Kim }
200039a53e0cSJaegeuk Kim 
200139a53e0cSJaegeuk Kim static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
200239a53e0cSJaegeuk Kim {
200339a53e0cSJaegeuk Kim 	return (struct sit_info *)(SM_I(sbi)->sit_info);
200439a53e0cSJaegeuk Kim }
200539a53e0cSJaegeuk Kim 
200639a53e0cSJaegeuk Kim static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
200739a53e0cSJaegeuk Kim {
200839a53e0cSJaegeuk Kim 	return (struct free_segmap_info *)(SM_I(sbi)->free_info);
200939a53e0cSJaegeuk Kim }
201039a53e0cSJaegeuk Kim 
201139a53e0cSJaegeuk Kim static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
201239a53e0cSJaegeuk Kim {
201339a53e0cSJaegeuk Kim 	return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
201439a53e0cSJaegeuk Kim }
201539a53e0cSJaegeuk Kim 
20169df27d98SGu Zheng static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
20179df27d98SGu Zheng {
20189df27d98SGu Zheng 	return sbi->meta_inode->i_mapping;
20199df27d98SGu Zheng }
20209df27d98SGu Zheng 
20214ef51a8fSJaegeuk Kim static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
20224ef51a8fSJaegeuk Kim {
20234ef51a8fSJaegeuk Kim 	return sbi->node_inode->i_mapping;
20244ef51a8fSJaegeuk Kim }
20254ef51a8fSJaegeuk Kim 
2026caf0047eSChao Yu static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
202739a53e0cSJaegeuk Kim {
2028fadb2fb8SChao Yu 	return test_bit(type, &sbi->s_flag);
202939a53e0cSJaegeuk Kim }
203039a53e0cSJaegeuk Kim 
2031caf0047eSChao Yu static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
203239a53e0cSJaegeuk Kim {
2033fadb2fb8SChao Yu 	set_bit(type, &sbi->s_flag);
2034caf0047eSChao Yu }
2035caf0047eSChao Yu 
2036caf0047eSChao Yu static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
2037caf0047eSChao Yu {
2038fadb2fb8SChao Yu 	clear_bit(type, &sbi->s_flag);
203939a53e0cSJaegeuk Kim }
204039a53e0cSJaegeuk Kim 
2041d71b5564SJaegeuk Kim static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
2042d71b5564SJaegeuk Kim {
2043d71b5564SJaegeuk Kim 	return le64_to_cpu(cp->checkpoint_ver);
2044d71b5564SJaegeuk Kim }
2045d71b5564SJaegeuk Kim 
2046ea676733SJaegeuk Kim static inline unsigned long f2fs_qf_ino(struct super_block *sb, int type)
2047ea676733SJaegeuk Kim {
2048ea676733SJaegeuk Kim 	if (type < F2FS_MAX_QUOTAS)
2049ea676733SJaegeuk Kim 		return le32_to_cpu(F2FS_SB(sb)->raw_super->qf_ino[type]);
2050ea676733SJaegeuk Kim 	return 0;
2051ea676733SJaegeuk Kim }
2052ea676733SJaegeuk Kim 
2053ced2c7eaSKinglong Mee static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
2054ced2c7eaSKinglong Mee {
2055ced2c7eaSKinglong Mee 	size_t crc_offset = le32_to_cpu(cp->checksum_offset);
2056ced2c7eaSKinglong Mee 	return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
2057ced2c7eaSKinglong Mee }
2058ced2c7eaSKinglong Mee 
2059aaec2b1dSChao Yu static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
206025ca923bSJaegeuk Kim {
206125ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
2062aaec2b1dSChao Yu 
206325ca923bSJaegeuk Kim 	return ckpt_flags & f;
206425ca923bSJaegeuk Kim }
206525ca923bSJaegeuk Kim 
2066aaec2b1dSChao Yu static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
206725ca923bSJaegeuk Kim {
2068aaec2b1dSChao Yu 	return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
2069aaec2b1dSChao Yu }
2070aaec2b1dSChao Yu 
2071aaec2b1dSChao Yu static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
2072aaec2b1dSChao Yu {
2073aaec2b1dSChao Yu 	unsigned int ckpt_flags;
2074aaec2b1dSChao Yu 
2075aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
207625ca923bSJaegeuk Kim 	ckpt_flags |= f;
207725ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
207825ca923bSJaegeuk Kim }
207925ca923bSJaegeuk Kim 
2080aaec2b1dSChao Yu static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
208125ca923bSJaegeuk Kim {
2082d1aa2453SChao Yu 	unsigned long flags;
2083d1aa2453SChao Yu 
2084d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
2085aaec2b1dSChao Yu 	__set_ckpt_flags(F2FS_CKPT(sbi), f);
2086d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
2087aaec2b1dSChao Yu }
2088aaec2b1dSChao Yu 
2089aaec2b1dSChao Yu static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
2090aaec2b1dSChao Yu {
2091aaec2b1dSChao Yu 	unsigned int ckpt_flags;
2092aaec2b1dSChao Yu 
2093aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
209425ca923bSJaegeuk Kim 	ckpt_flags &= (~f);
209525ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
209625ca923bSJaegeuk Kim }
209725ca923bSJaegeuk Kim 
2098aaec2b1dSChao Yu static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
2099aaec2b1dSChao Yu {
2100d1aa2453SChao Yu 	unsigned long flags;
2101d1aa2453SChao Yu 
2102d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
2103aaec2b1dSChao Yu 	__clear_ckpt_flags(F2FS_CKPT(sbi), f);
2104d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
2105aaec2b1dSChao Yu }
2106aaec2b1dSChao Yu 
2107*e4544b63STim Murray static inline void init_f2fs_rwsem(struct f2fs_rwsem *sem)
2108*e4544b63STim Murray {
2109*e4544b63STim Murray 	init_rwsem(&sem->internal_rwsem);
2110*e4544b63STim Murray 	init_waitqueue_head(&sem->read_waiters);
2111*e4544b63STim Murray }
2112*e4544b63STim Murray 
2113*e4544b63STim Murray static inline int f2fs_rwsem_is_locked(struct f2fs_rwsem *sem)
2114*e4544b63STim Murray {
2115*e4544b63STim Murray 	return rwsem_is_locked(&sem->internal_rwsem);
2116*e4544b63STim Murray }
2117*e4544b63STim Murray 
2118*e4544b63STim Murray static inline int f2fs_rwsem_is_contended(struct f2fs_rwsem *sem)
2119*e4544b63STim Murray {
2120*e4544b63STim Murray 	return rwsem_is_contended(&sem->internal_rwsem);
2121*e4544b63STim Murray }
2122*e4544b63STim Murray 
2123*e4544b63STim Murray static inline void f2fs_down_read(struct f2fs_rwsem *sem)
2124*e4544b63STim Murray {
2125*e4544b63STim Murray 	wait_event(sem->read_waiters, down_read_trylock(&sem->internal_rwsem));
2126*e4544b63STim Murray }
2127*e4544b63STim Murray 
2128*e4544b63STim Murray static inline int f2fs_down_read_trylock(struct f2fs_rwsem *sem)
2129*e4544b63STim Murray {
2130*e4544b63STim Murray 	return down_read_trylock(&sem->internal_rwsem);
2131*e4544b63STim Murray }
2132*e4544b63STim Murray 
2133*e4544b63STim Murray #ifdef CONFIG_DEBUG_LOCK_ALLOC
2134*e4544b63STim Murray static inline void f2fs_down_read_nested(struct f2fs_rwsem *sem, int subclass)
2135*e4544b63STim Murray {
2136*e4544b63STim Murray 	down_read_nested(&sem->internal_rwsem, subclass);
2137*e4544b63STim Murray }
2138*e4544b63STim Murray #else
2139*e4544b63STim Murray #define f2fs_down_read_nested(sem, subclass) f2fs_down_read(sem)
2140*e4544b63STim Murray #endif
2141*e4544b63STim Murray 
2142*e4544b63STim Murray static inline void f2fs_up_read(struct f2fs_rwsem *sem)
2143*e4544b63STim Murray {
2144*e4544b63STim Murray 	up_read(&sem->internal_rwsem);
2145*e4544b63STim Murray }
2146*e4544b63STim Murray 
2147*e4544b63STim Murray static inline void f2fs_down_write(struct f2fs_rwsem *sem)
2148*e4544b63STim Murray {
2149*e4544b63STim Murray 	down_write(&sem->internal_rwsem);
2150*e4544b63STim Murray }
2151*e4544b63STim Murray 
2152*e4544b63STim Murray static inline int f2fs_down_write_trylock(struct f2fs_rwsem *sem)
2153*e4544b63STim Murray {
2154*e4544b63STim Murray 	return down_write_trylock(&sem->internal_rwsem);
2155*e4544b63STim Murray }
2156*e4544b63STim Murray 
2157*e4544b63STim Murray static inline void f2fs_up_write(struct f2fs_rwsem *sem)
2158*e4544b63STim Murray {
2159*e4544b63STim Murray 	up_write(&sem->internal_rwsem);
2160*e4544b63STim Murray 	wake_up_all(&sem->read_waiters);
2161*e4544b63STim Murray }
2162*e4544b63STim Murray 
2163e479556bSGu Zheng static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
216439a53e0cSJaegeuk Kim {
2165*e4544b63STim Murray 	f2fs_down_read(&sbi->cp_rwsem);
216639a53e0cSJaegeuk Kim }
216739a53e0cSJaegeuk Kim 
2168cc15620bSJaegeuk Kim static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi)
2169cc15620bSJaegeuk Kim {
21703e020389SChao Yu 	if (time_to_inject(sbi, FAULT_LOCK_OP)) {
21713e020389SChao Yu 		f2fs_show_injection_info(sbi, FAULT_LOCK_OP);
21723e020389SChao Yu 		return 0;
21733e020389SChao Yu 	}
2174*e4544b63STim Murray 	return f2fs_down_read_trylock(&sbi->cp_rwsem);
2175cc15620bSJaegeuk Kim }
2176cc15620bSJaegeuk Kim 
2177e479556bSGu Zheng static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
217839a53e0cSJaegeuk Kim {
2179*e4544b63STim Murray 	f2fs_up_read(&sbi->cp_rwsem);
218039936837SJaegeuk Kim }
218139936837SJaegeuk Kim 
2182e479556bSGu Zheng static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
218339936837SJaegeuk Kim {
2184*e4544b63STim Murray 	f2fs_down_write(&sbi->cp_rwsem);
218539936837SJaegeuk Kim }
218639936837SJaegeuk Kim 
2187e479556bSGu Zheng static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
218839936837SJaegeuk Kim {
2189*e4544b63STim Murray 	f2fs_up_write(&sbi->cp_rwsem);
219039a53e0cSJaegeuk Kim }
219139a53e0cSJaegeuk Kim 
2192119ee914SJaegeuk Kim static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
2193119ee914SJaegeuk Kim {
2194119ee914SJaegeuk Kim 	int reason = CP_SYNC;
2195119ee914SJaegeuk Kim 
2196119ee914SJaegeuk Kim 	if (test_opt(sbi, FASTBOOT))
2197119ee914SJaegeuk Kim 		reason = CP_FASTBOOT;
2198119ee914SJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
2199119ee914SJaegeuk Kim 		reason = CP_UMOUNT;
2200119ee914SJaegeuk Kim 	return reason;
2201119ee914SJaegeuk Kim }
2202119ee914SJaegeuk Kim 
2203119ee914SJaegeuk Kim static inline bool __remain_node_summaries(int reason)
2204119ee914SJaegeuk Kim {
2205c473f1a9SChao Yu 	return (reason & (CP_UMOUNT | CP_FASTBOOT));
2206119ee914SJaegeuk Kim }
2207119ee914SJaegeuk Kim 
2208119ee914SJaegeuk Kim static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
2209119ee914SJaegeuk Kim {
2210aaec2b1dSChao Yu 	return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
2211aaec2b1dSChao Yu 			is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
2212119ee914SJaegeuk Kim }
2213119ee914SJaegeuk Kim 
221439a53e0cSJaegeuk Kim /*
221539a53e0cSJaegeuk Kim  * Check whether the inode has blocks or not
221639a53e0cSJaegeuk Kim  */
221739a53e0cSJaegeuk Kim static inline int F2FS_HAS_BLOCKS(struct inode *inode)
221839a53e0cSJaegeuk Kim {
22190eb0adadSChao Yu 	block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
22200eb0adadSChao Yu 
2221000519f2SChao Yu 	return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block;
222239a53e0cSJaegeuk Kim }
222339a53e0cSJaegeuk Kim 
22244bc8e9bcSChao Yu static inline bool f2fs_has_xattr_block(unsigned int ofs)
22254bc8e9bcSChao Yu {
22264bc8e9bcSChao Yu 	return ofs == XATTR_NODE_OFFSET;
22274bc8e9bcSChao Yu }
22284bc8e9bcSChao Yu 
2229d8a9a229SJaegeuk Kim static inline bool __allow_reserved_blocks(struct f2fs_sb_info *sbi,
2230a90a0884SJaegeuk Kim 					struct inode *inode, bool cap)
22317c2e5963SJaegeuk Kim {
2232d8a9a229SJaegeuk Kim 	if (!inode)
2233d8a9a229SJaegeuk Kim 		return true;
22347c2e5963SJaegeuk Kim 	if (!test_opt(sbi, RESERVE_ROOT))
22357c2e5963SJaegeuk Kim 		return false;
2236d8a9a229SJaegeuk Kim 	if (IS_NOQUOTA(inode))
2237d8a9a229SJaegeuk Kim 		return true;
223863189b78SChao Yu 	if (uid_eq(F2FS_OPTION(sbi).s_resuid, current_fsuid()))
22397c2e5963SJaegeuk Kim 		return true;
224063189b78SChao Yu 	if (!gid_eq(F2FS_OPTION(sbi).s_resgid, GLOBAL_ROOT_GID) &&
224163189b78SChao Yu 					in_group_p(F2FS_OPTION(sbi).s_resgid))
22427c2e5963SJaegeuk Kim 		return true;
2243a90a0884SJaegeuk Kim 	if (cap && capable(CAP_SYS_RESOURCE))
2244162b27aeSJaegeuk Kim 		return true;
22457c2e5963SJaegeuk Kim 	return false;
22467c2e5963SJaegeuk Kim }
22477c2e5963SJaegeuk Kim 
22480abd675eSChao Yu static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool);
22490abd675eSChao Yu static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
225046008c6dSChao Yu 				 struct inode *inode, blkcnt_t *count)
225139a53e0cSJaegeuk Kim {
22520abd675eSChao Yu 	blkcnt_t diff = 0, release = 0;
2253daeb433eSChao Yu 	block_t avail_user_block_count;
22540abd675eSChao Yu 	int ret;
22550abd675eSChao Yu 
22560abd675eSChao Yu 	ret = dquot_reserve_block(inode, *count);
22570abd675eSChao Yu 	if (ret)
22580abd675eSChao Yu 		return ret;
2259dd11a5dfSJaegeuk Kim 
226055523519SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
2261c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_BLOCK);
22620abd675eSChao Yu 		release = *count;
22639ea2f0beSChao Yu 		goto release_quota;
226455523519SChao Yu 	}
22657fa750a1SArnd Bergmann 
2266dd11a5dfSJaegeuk Kim 	/*
2267dd11a5dfSJaegeuk Kim 	 * let's increase this in prior to actual block count change in order
2268dd11a5dfSJaegeuk Kim 	 * for f2fs_sync_file to avoid data races when deciding checkpoint.
2269dd11a5dfSJaegeuk Kim 	 */
2270dd11a5dfSJaegeuk Kim 	percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
227139a53e0cSJaegeuk Kim 
227239a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
22732555a2d5SJaegeuk Kim 	sbi->total_valid_block_count += (block_t)(*count);
227480d42145SYunlong Song 	avail_user_block_count = sbi->user_block_count -
227580d42145SYunlong Song 					sbi->current_reserved_blocks;
22767e65be49SJaegeuk Kim 
2277a90a0884SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode, true))
227863189b78SChao Yu 		avail_user_block_count -= F2FS_OPTION(sbi).root_reserved_blocks;
2279300a8429SChao Yu 
2280300a8429SChao Yu 	if (F2FS_IO_ALIGNED(sbi))
2281300a8429SChao Yu 		avail_user_block_count -= sbi->blocks_per_seg *
2282300a8429SChao Yu 				SM_I(sbi)->additional_reserved_segments;
2283300a8429SChao Yu 
2284a4c3ecaaSDaniel Rosenberg 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2285a4c3ecaaSDaniel Rosenberg 		if (avail_user_block_count > sbi->unusable_block_count)
22864354994fSDaniel Rosenberg 			avail_user_block_count -= sbi->unusable_block_count;
2287a4c3ecaaSDaniel Rosenberg 		else
2288a4c3ecaaSDaniel Rosenberg 			avail_user_block_count = 0;
2289a4c3ecaaSDaniel Rosenberg 	}
2290daeb433eSChao Yu 	if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
2291daeb433eSChao Yu 		diff = sbi->total_valid_block_count - avail_user_block_count;
22927e65be49SJaegeuk Kim 		if (diff > *count)
22937e65be49SJaegeuk Kim 			diff = *count;
2294dd11a5dfSJaegeuk Kim 		*count -= diff;
22950abd675eSChao Yu 		release = diff;
22967e65be49SJaegeuk Kim 		sbi->total_valid_block_count -= diff;
229746008c6dSChao Yu 		if (!*count) {
229839a53e0cSJaegeuk Kim 			spin_unlock(&sbi->stat_lock);
22990abd675eSChao Yu 			goto enospc;
230039a53e0cSJaegeuk Kim 		}
230146008c6dSChao Yu 	}
230239a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
230341382ec4SJaegeuk Kim 
230436b877afSDaniel Rosenberg 	if (unlikely(release)) {
230536b877afSDaniel Rosenberg 		percpu_counter_sub(&sbi->alloc_valid_block_count, release);
23060abd675eSChao Yu 		dquot_release_reservation_block(inode, release);
230736b877afSDaniel Rosenberg 	}
23080abd675eSChao Yu 	f2fs_i_blocks_write(inode, *count, true, true);
23090abd675eSChao Yu 	return 0;
23100abd675eSChao Yu 
23110abd675eSChao Yu enospc:
231236b877afSDaniel Rosenberg 	percpu_counter_sub(&sbi->alloc_valid_block_count, release);
23139ea2f0beSChao Yu release_quota:
23140abd675eSChao Yu 	dquot_release_reservation_block(inode, release);
23150abd675eSChao Yu 	return -ENOSPC;
231639a53e0cSJaegeuk Kim }
231739a53e0cSJaegeuk Kim 
2318dcbb4c10SJoe Perches __printf(2, 3)
2319dcbb4c10SJoe Perches void f2fs_printk(struct f2fs_sb_info *sbi, const char *fmt, ...);
2320dcbb4c10SJoe Perches 
2321dcbb4c10SJoe Perches #define f2fs_err(sbi, fmt, ...)						\
2322dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_ERR fmt, ##__VA_ARGS__)
2323dcbb4c10SJoe Perches #define f2fs_warn(sbi, fmt, ...)					\
2324dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_WARNING fmt, ##__VA_ARGS__)
2325dcbb4c10SJoe Perches #define f2fs_notice(sbi, fmt, ...)					\
2326dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_NOTICE fmt, ##__VA_ARGS__)
2327dcbb4c10SJoe Perches #define f2fs_info(sbi, fmt, ...)					\
2328dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_INFO fmt, ##__VA_ARGS__)
2329dcbb4c10SJoe Perches #define f2fs_debug(sbi, fmt, ...)					\
2330dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_DEBUG fmt, ##__VA_ARGS__)
2331dcbb4c10SJoe Perches 
2332da19b0dcSGu Zheng static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
233339a53e0cSJaegeuk Kim 						struct inode *inode,
23340eb0adadSChao Yu 						block_t count)
233539a53e0cSJaegeuk Kim {
23360eb0adadSChao Yu 	blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK;
23370eb0adadSChao Yu 
233839a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
23399850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
234039a53e0cSJaegeuk Kim 	sbi->total_valid_block_count -= (block_t)count;
234180d42145SYunlong Song 	if (sbi->reserved_blocks &&
234280d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
234380d42145SYunlong Song 		sbi->current_reserved_blocks = min(sbi->reserved_blocks,
234480d42145SYunlong Song 					sbi->current_reserved_blocks + count);
234539a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
23465e159cd3SChao Yu 	if (unlikely(inode->i_blocks < sectors)) {
2347dcbb4c10SJoe Perches 		f2fs_warn(sbi, "Inconsistent i_blocks, ino:%lu, iblocks:%llu, sectors:%llu",
23485e159cd3SChao Yu 			  inode->i_ino,
23495e159cd3SChao Yu 			  (unsigned long long)inode->i_blocks,
23505e159cd3SChao Yu 			  (unsigned long long)sectors);
23515e159cd3SChao Yu 		set_sbi_flag(sbi, SBI_NEED_FSCK);
23525e159cd3SChao Yu 		return;
23535e159cd3SChao Yu 	}
23540abd675eSChao Yu 	f2fs_i_blocks_write(inode, count, false, true);
235539a53e0cSJaegeuk Kim }
235639a53e0cSJaegeuk Kim 
235739a53e0cSJaegeuk Kim static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
235839a53e0cSJaegeuk Kim {
235935782b23SJaegeuk Kim 	atomic_inc(&sbi->nr_pages[count_type]);
23607c4abcbeSChao Yu 
236120109873SChao Yu 	if (count_type == F2FS_DIRTY_DENTS ||
236220109873SChao Yu 			count_type == F2FS_DIRTY_NODES ||
236320109873SChao Yu 			count_type == F2FS_DIRTY_META ||
236420109873SChao Yu 			count_type == F2FS_DIRTY_QDATA ||
236520109873SChao Yu 			count_type == F2FS_DIRTY_IMETA)
2366caf0047eSChao Yu 		set_sbi_flag(sbi, SBI_IS_DIRTY);
236739a53e0cSJaegeuk Kim }
236839a53e0cSJaegeuk Kim 
2369a7ffdbe2SJaegeuk Kim static inline void inode_inc_dirty_pages(struct inode *inode)
237039a53e0cSJaegeuk Kim {
2371204706c7SJaegeuk Kim 	atomic_inc(&F2FS_I(inode)->dirty_pages);
2372c227f912SChao Yu 	inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
2373c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
23742c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
23752c8a4a28SJaegeuk Kim 		inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
237639a53e0cSJaegeuk Kim }
237739a53e0cSJaegeuk Kim 
237839a53e0cSJaegeuk Kim static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
237939a53e0cSJaegeuk Kim {
238035782b23SJaegeuk Kim 	atomic_dec(&sbi->nr_pages[count_type]);
238139a53e0cSJaegeuk Kim }
238239a53e0cSJaegeuk Kim 
2383a7ffdbe2SJaegeuk Kim static inline void inode_dec_dirty_pages(struct inode *inode)
238439a53e0cSJaegeuk Kim {
23855ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
23865ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
23871fe54f9dSJaegeuk Kim 		return;
23881fe54f9dSJaegeuk Kim 
2389204706c7SJaegeuk Kim 	atomic_dec(&F2FS_I(inode)->dirty_pages);
2390c227f912SChao Yu 	dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
2391c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
23922c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
23932c8a4a28SJaegeuk Kim 		dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
239439a53e0cSJaegeuk Kim }
239539a53e0cSJaegeuk Kim 
2396523be8a6SJaegeuk Kim static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
239739a53e0cSJaegeuk Kim {
239835782b23SJaegeuk Kim 	return atomic_read(&sbi->nr_pages[count_type]);
239939a53e0cSJaegeuk Kim }
240039a53e0cSJaegeuk Kim 
2401204706c7SJaegeuk Kim static inline int get_dirty_pages(struct inode *inode)
2402f8b2c1f9SJaegeuk Kim {
2403204706c7SJaegeuk Kim 	return atomic_read(&F2FS_I(inode)->dirty_pages);
2404f8b2c1f9SJaegeuk Kim }
2405f8b2c1f9SJaegeuk Kim 
24065ac206cfSNamjae Jeon static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
24075ac206cfSNamjae Jeon {
24083519e3f9SChao Yu 	unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
2409523be8a6SJaegeuk Kim 	unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
2410523be8a6SJaegeuk Kim 						sbi->log_blocks_per_seg;
2411523be8a6SJaegeuk Kim 
2412523be8a6SJaegeuk Kim 	return segs / sbi->segs_per_sec;
24135ac206cfSNamjae Jeon }
24145ac206cfSNamjae Jeon 
241539a53e0cSJaegeuk Kim static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
241639a53e0cSJaegeuk Kim {
24178b8343faSJaegeuk Kim 	return sbi->total_valid_block_count;
241839a53e0cSJaegeuk Kim }
241939a53e0cSJaegeuk Kim 
2420f83a2584SYunlei He static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
2421f83a2584SYunlei He {
2422f83a2584SYunlei He 	return sbi->discard_blks;
2423f83a2584SYunlei He }
2424f83a2584SYunlei He 
242539a53e0cSJaegeuk Kim static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
242639a53e0cSJaegeuk Kim {
242739a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
242839a53e0cSJaegeuk Kim 
242939a53e0cSJaegeuk Kim 	/* return NAT or SIT bitmap */
243039a53e0cSJaegeuk Kim 	if (flag == NAT_BITMAP)
243139a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
243239a53e0cSJaegeuk Kim 	else if (flag == SIT_BITMAP)
243339a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
243439a53e0cSJaegeuk Kim 
243539a53e0cSJaegeuk Kim 	return 0;
243639a53e0cSJaegeuk Kim }
243739a53e0cSJaegeuk Kim 
243855141486SWanpeng Li static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
243955141486SWanpeng Li {
244055141486SWanpeng Li 	return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
244155141486SWanpeng Li }
244255141486SWanpeng Li 
244339a53e0cSJaegeuk Kim static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
244439a53e0cSJaegeuk Kim {
244539a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
24464260c406SGustavo A. R. Silva 	void *tmp_ptr = &ckpt->sit_nat_version_bitmap;
24471dbe4152SChangman Lee 	int offset;
24481dbe4152SChangman Lee 
2449199bc3feSChao Yu 	if (is_set_ckpt_flags(sbi, CP_LARGE_NAT_BITMAP_FLAG)) {
2450199bc3feSChao Yu 		offset = (flag == SIT_BITMAP) ?
2451199bc3feSChao Yu 			le32_to_cpu(ckpt->nat_ver_bitmap_bytesize) : 0;
2452b471eb99SChao Yu 		/*
2453b471eb99SChao Yu 		 * if large_nat_bitmap feature is enabled, leave checksum
2454b471eb99SChao Yu 		 * protection for all nat/sit bitmaps.
2455b471eb99SChao Yu 		 */
24564260c406SGustavo A. R. Silva 		return tmp_ptr + offset + sizeof(__le32);
2457199bc3feSChao Yu 	}
2458199bc3feSChao Yu 
245955141486SWanpeng Li 	if (__cp_payload(sbi) > 0) {
24601dbe4152SChangman Lee 		if (flag == NAT_BITMAP)
24611dbe4152SChangman Lee 			return &ckpt->sit_nat_version_bitmap;
24621dbe4152SChangman Lee 		else
246365b85cccSJaegeuk Kim 			return (unsigned char *)ckpt + F2FS_BLKSIZE;
24641dbe4152SChangman Lee 	} else {
24651dbe4152SChangman Lee 		offset = (flag == NAT_BITMAP) ?
246625ca923bSJaegeuk Kim 			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
24674260c406SGustavo A. R. Silva 		return tmp_ptr + offset;
246839a53e0cSJaegeuk Kim 	}
24691dbe4152SChangman Lee }
247039a53e0cSJaegeuk Kim 
247139a53e0cSJaegeuk Kim static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
247239a53e0cSJaegeuk Kim {
24738508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
247439a53e0cSJaegeuk Kim 
24758508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 2)
247639a53e0cSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
247739a53e0cSJaegeuk Kim 	return start_addr;
247839a53e0cSJaegeuk Kim }
247939a53e0cSJaegeuk Kim 
24808508e44aSJaegeuk Kim static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
24818508e44aSJaegeuk Kim {
24828508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
24838508e44aSJaegeuk Kim 
24848508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 1)
24858508e44aSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
24868508e44aSJaegeuk Kim 	return start_addr;
24878508e44aSJaegeuk Kim }
24888508e44aSJaegeuk Kim 
24898508e44aSJaegeuk Kim static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
24908508e44aSJaegeuk Kim {
24918508e44aSJaegeuk Kim 	sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
24928508e44aSJaegeuk Kim }
24938508e44aSJaegeuk Kim 
249439a53e0cSJaegeuk Kim static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
249539a53e0cSJaegeuk Kim {
249639a53e0cSJaegeuk Kim 	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
249739a53e0cSJaegeuk Kim }
249839a53e0cSJaegeuk Kim 
24990abd675eSChao Yu static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
2500000519f2SChao Yu 					struct inode *inode, bool is_inode)
250139a53e0cSJaegeuk Kim {
250239a53e0cSJaegeuk Kim 	block_t	valid_block_count;
2503a4c3ecaaSDaniel Rosenberg 	unsigned int valid_node_count, user_block_count;
2504af033b2aSChao Yu 	int err;
25050abd675eSChao Yu 
2506af033b2aSChao Yu 	if (is_inode) {
2507af033b2aSChao Yu 		if (inode) {
2508af033b2aSChao Yu 			err = dquot_alloc_inode(inode);
2509af033b2aSChao Yu 			if (err)
2510af033b2aSChao Yu 				return err;
2511af033b2aSChao Yu 		}
2512af033b2aSChao Yu 	} else {
2513af033b2aSChao Yu 		err = dquot_reserve_block(inode, 1);
2514af033b2aSChao Yu 		if (err)
2515af033b2aSChao Yu 			return err;
25160abd675eSChao Yu 	}
251739a53e0cSJaegeuk Kim 
2518812c6056SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
2519c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_BLOCK);
2520812c6056SChao Yu 		goto enospc;
2521812c6056SChao Yu 	}
2522812c6056SChao Yu 
252339a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
252439a53e0cSJaegeuk Kim 
25257e65be49SJaegeuk Kim 	valid_block_count = sbi->total_valid_block_count +
25267e65be49SJaegeuk Kim 					sbi->current_reserved_blocks + 1;
25277e65be49SJaegeuk Kim 
2528a90a0884SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode, false))
252963189b78SChao Yu 		valid_block_count += F2FS_OPTION(sbi).root_reserved_blocks;
2530300a8429SChao Yu 
2531300a8429SChao Yu 	if (F2FS_IO_ALIGNED(sbi))
2532300a8429SChao Yu 		valid_block_count += sbi->blocks_per_seg *
2533300a8429SChao Yu 				SM_I(sbi)->additional_reserved_segments;
2534300a8429SChao Yu 
2535a4c3ecaaSDaniel Rosenberg 	user_block_count = sbi->user_block_count;
25364354994fSDaniel Rosenberg 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2537a4c3ecaaSDaniel Rosenberg 		user_block_count -= sbi->unusable_block_count;
25387e65be49SJaegeuk Kim 
2539a4c3ecaaSDaniel Rosenberg 	if (unlikely(valid_block_count > user_block_count)) {
254039a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
25410abd675eSChao Yu 		goto enospc;
254239a53e0cSJaegeuk Kim 	}
254339a53e0cSJaegeuk Kim 
2544ef86d709SGu Zheng 	valid_node_count = sbi->total_valid_node_count + 1;
2545cfb271d4SChao Yu 	if (unlikely(valid_node_count > sbi->total_node_count)) {
254639a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
25470abd675eSChao Yu 		goto enospc;
254839a53e0cSJaegeuk Kim 	}
254939a53e0cSJaegeuk Kim 
2550ef86d709SGu Zheng 	sbi->total_valid_node_count++;
2551ef86d709SGu Zheng 	sbi->total_valid_block_count++;
255239a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
255339a53e0cSJaegeuk Kim 
25540abd675eSChao Yu 	if (inode) {
25550abd675eSChao Yu 		if (is_inode)
25560abd675eSChao Yu 			f2fs_mark_inode_dirty_sync(inode, true);
25570abd675eSChao Yu 		else
25580abd675eSChao Yu 			f2fs_i_blocks_write(inode, 1, true, true);
25590abd675eSChao Yu 	}
25600abd675eSChao Yu 
256141382ec4SJaegeuk Kim 	percpu_counter_inc(&sbi->alloc_valid_block_count);
25620abd675eSChao Yu 	return 0;
25630abd675eSChao Yu 
25640abd675eSChao Yu enospc:
2565af033b2aSChao Yu 	if (is_inode) {
2566af033b2aSChao Yu 		if (inode)
2567af033b2aSChao Yu 			dquot_free_inode(inode);
2568af033b2aSChao Yu 	} else {
25690abd675eSChao Yu 		dquot_release_reservation_block(inode, 1);
2570af033b2aSChao Yu 	}
25710abd675eSChao Yu 	return -ENOSPC;
257239a53e0cSJaegeuk Kim }
257339a53e0cSJaegeuk Kim 
257439a53e0cSJaegeuk Kim static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
2575000519f2SChao Yu 					struct inode *inode, bool is_inode)
257639a53e0cSJaegeuk Kim {
257739a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
257839a53e0cSJaegeuk Kim 
25799850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_block_count);
25809850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_node_count);
258139a53e0cSJaegeuk Kim 
2582ef86d709SGu Zheng 	sbi->total_valid_node_count--;
2583ef86d709SGu Zheng 	sbi->total_valid_block_count--;
258480d42145SYunlong Song 	if (sbi->reserved_blocks &&
258580d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
258680d42145SYunlong Song 		sbi->current_reserved_blocks++;
258739a53e0cSJaegeuk Kim 
258839a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
25890abd675eSChao Yu 
2590ea6d7e72SChao Yu 	if (is_inode) {
2591af033b2aSChao Yu 		dquot_free_inode(inode);
2592ea6d7e72SChao Yu 	} else {
2593ea6d7e72SChao Yu 		if (unlikely(inode->i_blocks == 0)) {
2594097a7686SChao Yu 			f2fs_warn(sbi, "dec_valid_node_count: inconsistent i_blocks, ino:%lu, iblocks:%llu",
2595ea6d7e72SChao Yu 				  inode->i_ino,
2596ea6d7e72SChao Yu 				  (unsigned long long)inode->i_blocks);
2597ea6d7e72SChao Yu 			set_sbi_flag(sbi, SBI_NEED_FSCK);
2598ea6d7e72SChao Yu 			return;
2599ea6d7e72SChao Yu 		}
26000abd675eSChao Yu 		f2fs_i_blocks_write(inode, 1, false, true);
260139a53e0cSJaegeuk Kim 	}
2602ea6d7e72SChao Yu }
260339a53e0cSJaegeuk Kim 
260439a53e0cSJaegeuk Kim static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
260539a53e0cSJaegeuk Kim {
26068b8343faSJaegeuk Kim 	return sbi->total_valid_node_count;
260739a53e0cSJaegeuk Kim }
260839a53e0cSJaegeuk Kim 
260939a53e0cSJaegeuk Kim static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
261039a53e0cSJaegeuk Kim {
2611513c5f37SJaegeuk Kim 	percpu_counter_inc(&sbi->total_valid_inode_count);
261239a53e0cSJaegeuk Kim }
261339a53e0cSJaegeuk Kim 
26140e80220aSJaegeuk Kim static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
261539a53e0cSJaegeuk Kim {
2616513c5f37SJaegeuk Kim 	percpu_counter_dec(&sbi->total_valid_inode_count);
261739a53e0cSJaegeuk Kim }
261839a53e0cSJaegeuk Kim 
2619513c5f37SJaegeuk Kim static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
262039a53e0cSJaegeuk Kim {
2621513c5f37SJaegeuk Kim 	return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
262239a53e0cSJaegeuk Kim }
262339a53e0cSJaegeuk Kim 
2624a56c7c6fSJaegeuk Kim static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
2625a56c7c6fSJaegeuk Kim 						pgoff_t index, bool for_write)
2626a56c7c6fSJaegeuk Kim {
262782cf4f13SChao Yu 	struct page *page;
2628cac5a3d8SDongOh Shin 
26297fa750a1SArnd Bergmann 	if (IS_ENABLED(CONFIG_F2FS_FAULT_INJECTION)) {
263082cf4f13SChao Yu 		if (!for_write)
263182cf4f13SChao Yu 			page = find_get_page_flags(mapping, index,
263282cf4f13SChao Yu 							FGP_LOCK | FGP_ACCESSED);
263382cf4f13SChao Yu 		else
263482cf4f13SChao Yu 			page = find_lock_page(mapping, index);
2635c41f3cc3SJaegeuk Kim 		if (page)
2636c41f3cc3SJaegeuk Kim 			return page;
2637c41f3cc3SJaegeuk Kim 
263855523519SChao Yu 		if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) {
2639c45d6002SChao Yu 			f2fs_show_injection_info(F2FS_M_SB(mapping),
2640c45d6002SChao Yu 							FAULT_PAGE_ALLOC);
2641c41f3cc3SJaegeuk Kim 			return NULL;
264255523519SChao Yu 		}
26437fa750a1SArnd Bergmann 	}
26447fa750a1SArnd Bergmann 
2645a56c7c6fSJaegeuk Kim 	if (!for_write)
2646a56c7c6fSJaegeuk Kim 		return grab_cache_page(mapping, index);
2647a56c7c6fSJaegeuk Kim 	return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
2648a56c7c6fSJaegeuk Kim }
2649a56c7c6fSJaegeuk Kim 
265001eccef7SChao Yu static inline struct page *f2fs_pagecache_get_page(
265101eccef7SChao Yu 				struct address_space *mapping, pgoff_t index,
265201eccef7SChao Yu 				int fgp_flags, gfp_t gfp_mask)
265301eccef7SChao Yu {
265401eccef7SChao Yu 	if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_GET)) {
2655c45d6002SChao Yu 		f2fs_show_injection_info(F2FS_M_SB(mapping), FAULT_PAGE_GET);
265601eccef7SChao Yu 		return NULL;
265701eccef7SChao Yu 	}
26587fa750a1SArnd Bergmann 
265901eccef7SChao Yu 	return pagecache_get_page(mapping, index, fgp_flags, gfp_mask);
266001eccef7SChao Yu }
266101eccef7SChao Yu 
26626e2c64adSJaegeuk Kim static inline void f2fs_copy_page(struct page *src, struct page *dst)
26636e2c64adSJaegeuk Kim {
26646e2c64adSJaegeuk Kim 	char *src_kaddr = kmap(src);
26656e2c64adSJaegeuk Kim 	char *dst_kaddr = kmap(dst);
26666e2c64adSJaegeuk Kim 
26676e2c64adSJaegeuk Kim 	memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
26686e2c64adSJaegeuk Kim 	kunmap(dst);
26696e2c64adSJaegeuk Kim 	kunmap(src);
26706e2c64adSJaegeuk Kim }
26716e2c64adSJaegeuk Kim 
267239a53e0cSJaegeuk Kim static inline void f2fs_put_page(struct page *page, int unlock)
267339a53e0cSJaegeuk Kim {
2674031fa8ccSJaegeuk Kim 	if (!page)
267539a53e0cSJaegeuk Kim 		return;
267639a53e0cSJaegeuk Kim 
267739a53e0cSJaegeuk Kim 	if (unlock) {
26789850cf4aSJaegeuk Kim 		f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
267939a53e0cSJaegeuk Kim 		unlock_page(page);
268039a53e0cSJaegeuk Kim 	}
268109cbfeafSKirill A. Shutemov 	put_page(page);
268239a53e0cSJaegeuk Kim }
268339a53e0cSJaegeuk Kim 
268439a53e0cSJaegeuk Kim static inline void f2fs_put_dnode(struct dnode_of_data *dn)
268539a53e0cSJaegeuk Kim {
268639a53e0cSJaegeuk Kim 	if (dn->node_page)
268739a53e0cSJaegeuk Kim 		f2fs_put_page(dn->node_page, 1);
268839a53e0cSJaegeuk Kim 	if (dn->inode_page && dn->node_page != dn->inode_page)
268939a53e0cSJaegeuk Kim 		f2fs_put_page(dn->inode_page, 0);
269039a53e0cSJaegeuk Kim 	dn->node_page = NULL;
269139a53e0cSJaegeuk Kim 	dn->inode_page = NULL;
269239a53e0cSJaegeuk Kim }
269339a53e0cSJaegeuk Kim 
269439a53e0cSJaegeuk Kim static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
2695e8512d2eSGu Zheng 					size_t size)
269639a53e0cSJaegeuk Kim {
2697e8512d2eSGu Zheng 	return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
269839a53e0cSJaegeuk Kim }
269939a53e0cSJaegeuk Kim 
270032410577SChao Yu static inline void *f2fs_kmem_cache_alloc_nofail(struct kmem_cache *cachep,
27017bd59381SGu Zheng 						gfp_t flags)
27027bd59381SGu Zheng {
27037bd59381SGu Zheng 	void *entry;
27047bd59381SGu Zheng 
270580c54505SJaegeuk Kim 	entry = kmem_cache_alloc(cachep, flags);
270680c54505SJaegeuk Kim 	if (!entry)
270780c54505SJaegeuk Kim 		entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
27087bd59381SGu Zheng 	return entry;
27097bd59381SGu Zheng }
27107bd59381SGu Zheng 
271132410577SChao Yu static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
271232410577SChao Yu 			gfp_t flags, bool nofail, struct f2fs_sb_info *sbi)
271332410577SChao Yu {
271432410577SChao Yu 	if (nofail)
271532410577SChao Yu 		return f2fs_kmem_cache_alloc_nofail(cachep, flags);
271632410577SChao Yu 
271732410577SChao Yu 	if (time_to_inject(sbi, FAULT_SLAB_ALLOC)) {
271832410577SChao Yu 		f2fs_show_injection_info(sbi, FAULT_SLAB_ALLOC);
271932410577SChao Yu 		return NULL;
272032410577SChao Yu 	}
272132410577SChao Yu 
272232410577SChao Yu 	return kmem_cache_alloc(cachep, flags);
272332410577SChao Yu }
272432410577SChao Yu 
2725493720a4SChao Yu static inline bool is_inflight_io(struct f2fs_sb_info *sbi, int type)
27265f9abab4SJaegeuk Kim {
27275f9abab4SJaegeuk Kim 	if (get_pages(sbi, F2FS_RD_DATA) || get_pages(sbi, F2FS_RD_NODE) ||
27285f9abab4SJaegeuk Kim 		get_pages(sbi, F2FS_RD_META) || get_pages(sbi, F2FS_WB_DATA) ||
2729fef4129eSChao Yu 		get_pages(sbi, F2FS_WB_CP_DATA) ||
2730fef4129eSChao Yu 		get_pages(sbi, F2FS_DIO_READ) ||
273111ac8ef8SJaegeuk Kim 		get_pages(sbi, F2FS_DIO_WRITE))
2732493720a4SChao Yu 		return true;
273311ac8ef8SJaegeuk Kim 
273456659ce8SSahitya Tummala 	if (type != DISCARD_TIME && SM_I(sbi) && SM_I(sbi)->dcc_info &&
273511ac8ef8SJaegeuk Kim 			atomic_read(&SM_I(sbi)->dcc_info->queued_discard))
2736493720a4SChao Yu 		return true;
273711ac8ef8SJaegeuk Kim 
273811ac8ef8SJaegeuk Kim 	if (SM_I(sbi) && SM_I(sbi)->fcc_info &&
273972691af6SJaegeuk Kim 			atomic_read(&SM_I(sbi)->fcc_info->queued_flush))
2740493720a4SChao Yu 		return true;
2741493720a4SChao Yu 	return false;
2742493720a4SChao Yu }
2743493720a4SChao Yu 
2744493720a4SChao Yu static inline bool is_idle(struct f2fs_sb_info *sbi, int type)
2745493720a4SChao Yu {
2746493720a4SChao Yu 	if (sbi->gc_mode == GC_URGENT_HIGH)
2747493720a4SChao Yu 		return true;
2748493720a4SChao Yu 
2749493720a4SChao Yu 	if (is_inflight_io(sbi, type))
27505f9abab4SJaegeuk Kim 		return false;
275111ac8ef8SJaegeuk Kim 
27520e5e8111SDaeho Jeong 	if (sbi->gc_mode == GC_URGENT_LOW &&
27530e5e8111SDaeho Jeong 			(type == DISCARD_TIME || type == GC_TIME))
27540e5e8111SDaeho Jeong 		return true;
27550e5e8111SDaeho Jeong 
27565f9abab4SJaegeuk Kim 	return f2fs_time_over(sbi, type);
27575f9abab4SJaegeuk Kim }
27585f9abab4SJaegeuk Kim 
27599be32d72SJaegeuk Kim static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
27609be32d72SJaegeuk Kim 				unsigned long index, void *item)
27619be32d72SJaegeuk Kim {
27629be32d72SJaegeuk Kim 	while (radix_tree_insert(root, index, item))
27639be32d72SJaegeuk Kim 		cond_resched();
27649be32d72SJaegeuk Kim }
27659be32d72SJaegeuk Kim 
276639a53e0cSJaegeuk Kim #define RAW_IS_INODE(p)	((p)->footer.nid == (p)->footer.ino)
276739a53e0cSJaegeuk Kim 
276839a53e0cSJaegeuk Kim static inline bool IS_INODE(struct page *page)
276939a53e0cSJaegeuk Kim {
277045590710SGu Zheng 	struct f2fs_node *p = F2FS_NODE(page);
2771cac5a3d8SDongOh Shin 
277239a53e0cSJaegeuk Kim 	return RAW_IS_INODE(p);
277339a53e0cSJaegeuk Kim }
277439a53e0cSJaegeuk Kim 
27757a2af766SChao Yu static inline int offset_in_addr(struct f2fs_inode *i)
27767a2af766SChao Yu {
27777a2af766SChao Yu 	return (i->i_inline & F2FS_EXTRA_ATTR) ?
27787a2af766SChao Yu 			(le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0;
27797a2af766SChao Yu }
27807a2af766SChao Yu 
278139a53e0cSJaegeuk Kim static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
278239a53e0cSJaegeuk Kim {
278339a53e0cSJaegeuk Kim 	return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
278439a53e0cSJaegeuk Kim }
278539a53e0cSJaegeuk Kim 
27867a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode);
2787a2ced1ceSChao Yu static inline block_t data_blkaddr(struct inode *inode,
27887a2af766SChao Yu 			struct page *node_page, unsigned int offset)
278939a53e0cSJaegeuk Kim {
279039a53e0cSJaegeuk Kim 	struct f2fs_node *raw_node;
279139a53e0cSJaegeuk Kim 	__le32 *addr_array;
27927a2af766SChao Yu 	int base = 0;
27937a2af766SChao Yu 	bool is_inode = IS_INODE(node_page);
2794cac5a3d8SDongOh Shin 
279545590710SGu Zheng 	raw_node = F2FS_NODE(node_page);
27967a2af766SChao Yu 
2797979f492fSLiFan 	if (is_inode) {
2798979f492fSLiFan 		if (!inode)
27997a88ddb5SChao Yu 			/* from GC path only */
28007a2af766SChao Yu 			base = offset_in_addr(&raw_node->i);
2801979f492fSLiFan 		else if (f2fs_has_extra_attr(inode))
28027a2af766SChao Yu 			base = get_extra_isize(inode);
28037a2af766SChao Yu 	}
28047a2af766SChao Yu 
280539a53e0cSJaegeuk Kim 	addr_array = blkaddr_in_node(raw_node);
28067a2af766SChao Yu 	return le32_to_cpu(addr_array[base + offset]);
280739a53e0cSJaegeuk Kim }
280839a53e0cSJaegeuk Kim 
2809a2ced1ceSChao Yu static inline block_t f2fs_data_blkaddr(struct dnode_of_data *dn)
2810a2ced1ceSChao Yu {
2811a2ced1ceSChao Yu 	return data_blkaddr(dn->inode, dn->node_page, dn->ofs_in_node);
2812a2ced1ceSChao Yu }
2813a2ced1ceSChao Yu 
281439a53e0cSJaegeuk Kim static inline int f2fs_test_bit(unsigned int nr, char *addr)
281539a53e0cSJaegeuk Kim {
281639a53e0cSJaegeuk Kim 	int mask;
281739a53e0cSJaegeuk Kim 
281839a53e0cSJaegeuk Kim 	addr += (nr >> 3);
281939a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
282039a53e0cSJaegeuk Kim 	return mask & *addr;
282139a53e0cSJaegeuk Kim }
282239a53e0cSJaegeuk Kim 
2823a66cdd98SJaegeuk Kim static inline void f2fs_set_bit(unsigned int nr, char *addr)
2824a66cdd98SJaegeuk Kim {
2825a66cdd98SJaegeuk Kim 	int mask;
2826a66cdd98SJaegeuk Kim 
2827a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2828a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2829a66cdd98SJaegeuk Kim 	*addr |= mask;
2830a66cdd98SJaegeuk Kim }
2831a66cdd98SJaegeuk Kim 
2832a66cdd98SJaegeuk Kim static inline void f2fs_clear_bit(unsigned int nr, char *addr)
2833a66cdd98SJaegeuk Kim {
2834a66cdd98SJaegeuk Kim 	int mask;
2835a66cdd98SJaegeuk Kim 
2836a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2837a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2838a66cdd98SJaegeuk Kim 	*addr &= ~mask;
2839a66cdd98SJaegeuk Kim }
2840a66cdd98SJaegeuk Kim 
284152aca074SGu Zheng static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
284239a53e0cSJaegeuk Kim {
284339a53e0cSJaegeuk Kim 	int mask;
284439a53e0cSJaegeuk Kim 	int ret;
284539a53e0cSJaegeuk Kim 
284639a53e0cSJaegeuk Kim 	addr += (nr >> 3);
284739a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
284839a53e0cSJaegeuk Kim 	ret = mask & *addr;
284939a53e0cSJaegeuk Kim 	*addr |= mask;
285039a53e0cSJaegeuk Kim 	return ret;
285139a53e0cSJaegeuk Kim }
285239a53e0cSJaegeuk Kim 
285352aca074SGu Zheng static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
285439a53e0cSJaegeuk Kim {
285539a53e0cSJaegeuk Kim 	int mask;
285639a53e0cSJaegeuk Kim 	int ret;
285739a53e0cSJaegeuk Kim 
285839a53e0cSJaegeuk Kim 	addr += (nr >> 3);
285939a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
286039a53e0cSJaegeuk Kim 	ret = mask & *addr;
286139a53e0cSJaegeuk Kim 	*addr &= ~mask;
286239a53e0cSJaegeuk Kim 	return ret;
286339a53e0cSJaegeuk Kim }
286439a53e0cSJaegeuk Kim 
2865c6ac4c0eSGu Zheng static inline void f2fs_change_bit(unsigned int nr, char *addr)
2866c6ac4c0eSGu Zheng {
2867c6ac4c0eSGu Zheng 	int mask;
2868c6ac4c0eSGu Zheng 
2869c6ac4c0eSGu Zheng 	addr += (nr >> 3);
2870c6ac4c0eSGu Zheng 	mask = 1 << (7 - (nr & 0x07));
2871c6ac4c0eSGu Zheng 	*addr ^= mask;
2872c6ac4c0eSGu Zheng }
2873c6ac4c0eSGu Zheng 
287459c84408SChao Yu /*
287536098557SEric Biggers  * On-disk inode flags (f2fs_inode::i_flags)
287659c84408SChao Yu  */
28774c8ff709SChao Yu #define F2FS_COMPR_FL			0x00000004 /* Compress file */
287859c84408SChao Yu #define F2FS_SYNC_FL			0x00000008 /* Synchronous updates */
287959c84408SChao Yu #define F2FS_IMMUTABLE_FL		0x00000010 /* Immutable file */
288059c84408SChao Yu #define F2FS_APPEND_FL			0x00000020 /* writes to file may only append */
288159c84408SChao Yu #define F2FS_NODUMP_FL			0x00000040 /* do not dump file */
288259c84408SChao Yu #define F2FS_NOATIME_FL			0x00000080 /* do not update atime */
28834c8ff709SChao Yu #define F2FS_NOCOMP_FL			0x00000400 /* Don't compress */
288459c84408SChao Yu #define F2FS_INDEX_FL			0x00001000 /* hash-indexed directory */
288559c84408SChao Yu #define F2FS_DIRSYNC_FL			0x00010000 /* dirsync behaviour (directories only) */
288659c84408SChao Yu #define F2FS_PROJINHERIT_FL		0x20000000 /* Create with parents projid */
28872c2eb7a3SDaniel Rosenberg #define F2FS_CASEFOLD_FL		0x40000000 /* Casefolded file */
288859c84408SChao Yu 
288959c84408SChao Yu /* Flags that should be inherited by new inodes from their parent. */
289036098557SEric Biggers #define F2FS_FL_INHERITED (F2FS_SYNC_FL | F2FS_NODUMP_FL | F2FS_NOATIME_FL | \
28912c2eb7a3SDaniel Rosenberg 			   F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL | \
28924c8ff709SChao Yu 			   F2FS_CASEFOLD_FL | F2FS_COMPR_FL | F2FS_NOCOMP_FL)
289359c84408SChao Yu 
289459c84408SChao Yu /* Flags that are appropriate for regular files (all but dir-specific ones). */
28952c2eb7a3SDaniel Rosenberg #define F2FS_REG_FLMASK		(~(F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL | \
28962c2eb7a3SDaniel Rosenberg 				F2FS_CASEFOLD_FL))
289759c84408SChao Yu 
289859c84408SChao Yu /* Flags that are appropriate for non-directories/regular files. */
289959c84408SChao Yu #define F2FS_OTHER_FLMASK	(F2FS_NODUMP_FL | F2FS_NOATIME_FL)
29005c57132eSChao Yu 
29015c57132eSChao Yu static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
29025c57132eSChao Yu {
29035c57132eSChao Yu 	if (S_ISDIR(mode))
29045c57132eSChao Yu 		return flags;
29055c57132eSChao Yu 	else if (S_ISREG(mode))
29065c57132eSChao Yu 		return flags & F2FS_REG_FLMASK;
29075c57132eSChao Yu 	else
29085c57132eSChao Yu 		return flags & F2FS_OTHER_FLMASK;
29095c57132eSChao Yu }
29105c57132eSChao Yu 
2911205b9822SJaegeuk Kim static inline void __mark_inode_dirty_flag(struct inode *inode,
2912205b9822SJaegeuk Kim 						int flag, bool set)
291339a53e0cSJaegeuk Kim {
2914205b9822SJaegeuk Kim 	switch (flag) {
2915205b9822SJaegeuk Kim 	case FI_INLINE_XATTR:
2916205b9822SJaegeuk Kim 	case FI_INLINE_DATA:
2917205b9822SJaegeuk Kim 	case FI_INLINE_DENTRY:
29189ac1e2d8SDaeho Jeong 	case FI_NEW_INODE:
2919205b9822SJaegeuk Kim 		if (set)
2920205b9822SJaegeuk Kim 			return;
2921df561f66SGustavo A. R. Silva 		fallthrough;
2922205b9822SJaegeuk Kim 	case FI_DATA_EXIST:
2923205b9822SJaegeuk Kim 	case FI_INLINE_DOTS:
29241ad71a27SJaegeuk Kim 	case FI_PIN_FILE:
2925c6140415SJaegeuk Kim 	case FI_COMPRESS_RELEASED:
29267c45729aSJaegeuk Kim 		f2fs_mark_inode_dirty_sync(inode, true);
2927205b9822SJaegeuk Kim 	}
292839a53e0cSJaegeuk Kim }
292939a53e0cSJaegeuk Kim 
293091942321SJaegeuk Kim static inline void set_inode_flag(struct inode *inode, int flag)
293139a53e0cSJaegeuk Kim {
293258f7e00fSYufen Yu 	set_bit(flag, F2FS_I(inode)->flags);
2933205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, true);
293439a53e0cSJaegeuk Kim }
293539a53e0cSJaegeuk Kim 
293691942321SJaegeuk Kim static inline int is_inode_flag_set(struct inode *inode, int flag)
293739a53e0cSJaegeuk Kim {
29387653b9d8SChao Yu 	return test_bit(flag, F2FS_I(inode)->flags);
293939a53e0cSJaegeuk Kim }
294039a53e0cSJaegeuk Kim 
294191942321SJaegeuk Kim static inline void clear_inode_flag(struct inode *inode, int flag)
294239a53e0cSJaegeuk Kim {
294358f7e00fSYufen Yu 	clear_bit(flag, F2FS_I(inode)->flags);
2944205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, false);
294539a53e0cSJaegeuk Kim }
294639a53e0cSJaegeuk Kim 
294795ae251fSEric Biggers static inline bool f2fs_verity_in_progress(struct inode *inode)
294895ae251fSEric Biggers {
294995ae251fSEric Biggers 	return IS_ENABLED(CONFIG_FS_VERITY) &&
295095ae251fSEric Biggers 	       is_inode_flag_set(inode, FI_VERITY_IN_PROGRESS);
295195ae251fSEric Biggers }
295295ae251fSEric Biggers 
295391942321SJaegeuk Kim static inline void set_acl_inode(struct inode *inode, umode_t mode)
2954444c580fSJaegeuk Kim {
295591942321SJaegeuk Kim 	F2FS_I(inode)->i_acl_mode = mode;
295691942321SJaegeuk Kim 	set_inode_flag(inode, FI_ACL_MODE);
29577c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, false);
295839a53e0cSJaegeuk Kim }
295939a53e0cSJaegeuk Kim 
2960a1961246SJaegeuk Kim static inline void f2fs_i_links_write(struct inode *inode, bool inc)
2961a1961246SJaegeuk Kim {
2962a1961246SJaegeuk Kim 	if (inc)
2963a1961246SJaegeuk Kim 		inc_nlink(inode);
2964a1961246SJaegeuk Kim 	else
2965a1961246SJaegeuk Kim 		drop_nlink(inode);
29667c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2967a1961246SJaegeuk Kim }
2968a1961246SJaegeuk Kim 
29698edd03c8SJaegeuk Kim static inline void f2fs_i_blocks_write(struct inode *inode,
29700abd675eSChao Yu 					block_t diff, bool add, bool claim)
29718edd03c8SJaegeuk Kim {
297226de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
297326de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
297426de9b11SJaegeuk Kim 
29750abd675eSChao Yu 	/* add = 1, claim = 1 should be dquot_reserve_block in pair */
29760abd675eSChao Yu 	if (add) {
29770abd675eSChao Yu 		if (claim)
29780abd675eSChao Yu 			dquot_claim_block(inode, diff);
29790abd675eSChao Yu 		else
29800abd675eSChao Yu 			dquot_alloc_block_nofail(inode, diff);
29810abd675eSChao Yu 	} else {
29820abd675eSChao Yu 		dquot_free_block(inode, diff);
29830abd675eSChao Yu 	}
29840abd675eSChao Yu 
29857c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
298626de9b11SJaegeuk Kim 	if (clean || recover)
298726de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
29888edd03c8SJaegeuk Kim }
29898edd03c8SJaegeuk Kim 
2990fc9581c8SJaegeuk Kim static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
2991fc9581c8SJaegeuk Kim {
299226de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
299326de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
299426de9b11SJaegeuk Kim 
2995fc9581c8SJaegeuk Kim 	if (i_size_read(inode) == i_size)
2996fc9581c8SJaegeuk Kim 		return;
2997fc9581c8SJaegeuk Kim 
2998fc9581c8SJaegeuk Kim 	i_size_write(inode, i_size);
29997c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
300026de9b11SJaegeuk Kim 	if (clean || recover)
300126de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
300226de9b11SJaegeuk Kim }
300326de9b11SJaegeuk Kim 
3004205b9822SJaegeuk Kim static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
3005205b9822SJaegeuk Kim {
3006205b9822SJaegeuk Kim 	F2FS_I(inode)->i_current_depth = depth;
30077c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3008205b9822SJaegeuk Kim }
3009205b9822SJaegeuk Kim 
30101ad71a27SJaegeuk Kim static inline void f2fs_i_gc_failures_write(struct inode *inode,
30111ad71a27SJaegeuk Kim 					unsigned int count)
30121ad71a27SJaegeuk Kim {
30132ef79ecbSChao Yu 	F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN] = count;
30141ad71a27SJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
30151ad71a27SJaegeuk Kim }
30161ad71a27SJaegeuk Kim 
3017205b9822SJaegeuk Kim static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
3018205b9822SJaegeuk Kim {
3019205b9822SJaegeuk Kim 	F2FS_I(inode)->i_xattr_nid = xnid;
30207c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3021205b9822SJaegeuk Kim }
3022205b9822SJaegeuk Kim 
3023205b9822SJaegeuk Kim static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
3024205b9822SJaegeuk Kim {
3025205b9822SJaegeuk Kim 	F2FS_I(inode)->i_pino = pino;
30267c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3027205b9822SJaegeuk Kim }
3028205b9822SJaegeuk Kim 
302991942321SJaegeuk Kim static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
3030444c580fSJaegeuk Kim {
3031205b9822SJaegeuk Kim 	struct f2fs_inode_info *fi = F2FS_I(inode);
3032205b9822SJaegeuk Kim 
3033444c580fSJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_XATTR)
30347653b9d8SChao Yu 		set_bit(FI_INLINE_XATTR, fi->flags);
30351001b347SHuajun Li 	if (ri->i_inline & F2FS_INLINE_DATA)
30367653b9d8SChao Yu 		set_bit(FI_INLINE_DATA, fi->flags);
303734d67debSChao Yu 	if (ri->i_inline & F2FS_INLINE_DENTRY)
30387653b9d8SChao Yu 		set_bit(FI_INLINE_DENTRY, fi->flags);
3039b3d208f9SJaegeuk Kim 	if (ri->i_inline & F2FS_DATA_EXIST)
30407653b9d8SChao Yu 		set_bit(FI_DATA_EXIST, fi->flags);
3041510022a8SJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_DOTS)
30427653b9d8SChao Yu 		set_bit(FI_INLINE_DOTS, fi->flags);
30437a2af766SChao Yu 	if (ri->i_inline & F2FS_EXTRA_ATTR)
30447653b9d8SChao Yu 		set_bit(FI_EXTRA_ATTR, fi->flags);
30451ad71a27SJaegeuk Kim 	if (ri->i_inline & F2FS_PIN_FILE)
30467653b9d8SChao Yu 		set_bit(FI_PIN_FILE, fi->flags);
3047c6140415SJaegeuk Kim 	if (ri->i_inline & F2FS_COMPRESS_RELEASED)
3048c6140415SJaegeuk Kim 		set_bit(FI_COMPRESS_RELEASED, fi->flags);
3049444c580fSJaegeuk Kim }
3050444c580fSJaegeuk Kim 
305191942321SJaegeuk Kim static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
3052444c580fSJaegeuk Kim {
3053444c580fSJaegeuk Kim 	ri->i_inline = 0;
3054444c580fSJaegeuk Kim 
305591942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_XATTR))
3056444c580fSJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_XATTR;
305791942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DATA))
30581001b347SHuajun Li 		ri->i_inline |= F2FS_INLINE_DATA;
305991942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
306034d67debSChao Yu 		ri->i_inline |= F2FS_INLINE_DENTRY;
306191942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_DATA_EXIST))
3062b3d208f9SJaegeuk Kim 		ri->i_inline |= F2FS_DATA_EXIST;
306391942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DOTS))
3064510022a8SJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_DOTS;
30657a2af766SChao Yu 	if (is_inode_flag_set(inode, FI_EXTRA_ATTR))
30667a2af766SChao Yu 		ri->i_inline |= F2FS_EXTRA_ATTR;
30671ad71a27SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_PIN_FILE))
30681ad71a27SJaegeuk Kim 		ri->i_inline |= F2FS_PIN_FILE;
3069c6140415SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
3070c6140415SJaegeuk Kim 		ri->i_inline |= F2FS_COMPRESS_RELEASED;
30717a2af766SChao Yu }
30727a2af766SChao Yu 
30737a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode)
30747a2af766SChao Yu {
30757a2af766SChao Yu 	return is_inode_flag_set(inode, FI_EXTRA_ATTR);
3076444c580fSJaegeuk Kim }
3077444c580fSJaegeuk Kim 
3078987c7c31SChao Yu static inline int f2fs_has_inline_xattr(struct inode *inode)
3079987c7c31SChao Yu {
308091942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_XATTR);
3081987c7c31SChao Yu }
3082987c7c31SChao Yu 
30834c8ff709SChao Yu static inline int f2fs_compressed_file(struct inode *inode)
30844c8ff709SChao Yu {
30854c8ff709SChao Yu 	return S_ISREG(inode->i_mode) &&
30864c8ff709SChao Yu 		is_inode_flag_set(inode, FI_COMPRESSED_FILE);
30874c8ff709SChao Yu }
30884c8ff709SChao Yu 
3089602a16d5SDaeho Jeong static inline bool f2fs_need_compress_data(struct inode *inode)
3090602a16d5SDaeho Jeong {
3091602a16d5SDaeho Jeong 	int compress_mode = F2FS_OPTION(F2FS_I_SB(inode)).compress_mode;
3092602a16d5SDaeho Jeong 
3093602a16d5SDaeho Jeong 	if (!f2fs_compressed_file(inode))
3094602a16d5SDaeho Jeong 		return false;
3095602a16d5SDaeho Jeong 
3096602a16d5SDaeho Jeong 	if (compress_mode == COMPR_MODE_FS)
3097602a16d5SDaeho Jeong 		return true;
3098602a16d5SDaeho Jeong 	else if (compress_mode == COMPR_MODE_USER &&
3099602a16d5SDaeho Jeong 			is_inode_flag_set(inode, FI_ENABLE_COMPRESS))
3100602a16d5SDaeho Jeong 		return true;
3101602a16d5SDaeho Jeong 
3102602a16d5SDaeho Jeong 	return false;
3103602a16d5SDaeho Jeong }
3104602a16d5SDaeho Jeong 
310581ca7350SChao Yu static inline unsigned int addrs_per_inode(struct inode *inode)
3106de93653fSJaegeuk Kim {
3107d02a6e61SChao Yu 	unsigned int addrs = CUR_ADDRS_PER_INODE(inode) -
3108d02a6e61SChao Yu 				get_inline_xattr_addrs(inode);
31094c8ff709SChao Yu 
31104c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
31114c8ff709SChao Yu 		return addrs;
31124c8ff709SChao Yu 	return ALIGN_DOWN(addrs, F2FS_I(inode)->i_cluster_size);
3113d02a6e61SChao Yu }
3114d02a6e61SChao Yu 
3115d02a6e61SChao Yu static inline unsigned int addrs_per_block(struct inode *inode)
3116d02a6e61SChao Yu {
31174c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
31184c8ff709SChao Yu 		return DEF_ADDRS_PER_BLOCK;
31194c8ff709SChao Yu 	return ALIGN_DOWN(DEF_ADDRS_PER_BLOCK, F2FS_I(inode)->i_cluster_size);
3120de93653fSJaegeuk Kim }
3121de93653fSJaegeuk Kim 
31226afc662eSChao Yu static inline void *inline_xattr_addr(struct inode *inode, struct page *page)
312365985d93SJaegeuk Kim {
3124695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
3125cac5a3d8SDongOh Shin 
312665985d93SJaegeuk Kim 	return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
3127b323fd28SChao Yu 					get_inline_xattr_addrs(inode)]);
312865985d93SJaegeuk Kim }
312965985d93SJaegeuk Kim 
313065985d93SJaegeuk Kim static inline int inline_xattr_size(struct inode *inode)
313165985d93SJaegeuk Kim {
3132622927f3SChao Yu 	if (f2fs_has_inline_xattr(inode))
31336afc662eSChao Yu 		return get_inline_xattr_addrs(inode) * sizeof(__le32);
3134622927f3SChao Yu 	return 0;
313565985d93SJaegeuk Kim }
313665985d93SJaegeuk Kim 
31370dbdc2aeSJaegeuk Kim static inline int f2fs_has_inline_data(struct inode *inode)
31380dbdc2aeSJaegeuk Kim {
313991942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DATA);
31400dbdc2aeSJaegeuk Kim }
31410dbdc2aeSJaegeuk Kim 
3142b3d208f9SJaegeuk Kim static inline int f2fs_exist_data(struct inode *inode)
3143b3d208f9SJaegeuk Kim {
314491942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DATA_EXIST);
3145b3d208f9SJaegeuk Kim }
3146b3d208f9SJaegeuk Kim 
3147510022a8SJaegeuk Kim static inline int f2fs_has_inline_dots(struct inode *inode)
3148510022a8SJaegeuk Kim {
314991942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DOTS);
3150510022a8SJaegeuk Kim }
3151510022a8SJaegeuk Kim 
31524c8ff709SChao Yu static inline int f2fs_is_mmap_file(struct inode *inode)
31534c8ff709SChao Yu {
31544c8ff709SChao Yu 	return is_inode_flag_set(inode, FI_MMAP_FILE);
31554c8ff709SChao Yu }
31564c8ff709SChao Yu 
31571ad71a27SJaegeuk Kim static inline bool f2fs_is_pinned_file(struct inode *inode)
31581ad71a27SJaegeuk Kim {
31591ad71a27SJaegeuk Kim 	return is_inode_flag_set(inode, FI_PIN_FILE);
31601ad71a27SJaegeuk Kim }
31611ad71a27SJaegeuk Kim 
316288b88a66SJaegeuk Kim static inline bool f2fs_is_atomic_file(struct inode *inode)
316388b88a66SJaegeuk Kim {
316491942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_ATOMIC_FILE);
316588b88a66SJaegeuk Kim }
316688b88a66SJaegeuk Kim 
31675fe45743SChao Yu static inline bool f2fs_is_commit_atomic_write(struct inode *inode)
31685fe45743SChao Yu {
31695fe45743SChao Yu 	return is_inode_flag_set(inode, FI_ATOMIC_COMMIT);
31705fe45743SChao Yu }
31715fe45743SChao Yu 
317202a1335fSJaegeuk Kim static inline bool f2fs_is_volatile_file(struct inode *inode)
317302a1335fSJaegeuk Kim {
317491942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_VOLATILE_FILE);
317502a1335fSJaegeuk Kim }
317602a1335fSJaegeuk Kim 
31773c6c2bebSJaegeuk Kim static inline bool f2fs_is_first_block_written(struct inode *inode)
31783c6c2bebSJaegeuk Kim {
317991942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
31803c6c2bebSJaegeuk Kim }
31813c6c2bebSJaegeuk Kim 
31821e84371fSJaegeuk Kim static inline bool f2fs_is_drop_cache(struct inode *inode)
31831e84371fSJaegeuk Kim {
318491942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DROP_CACHE);
31851e84371fSJaegeuk Kim }
31861e84371fSJaegeuk Kim 
3187f2470371SChao Yu static inline void *inline_data_addr(struct inode *inode, struct page *page)
31881001b347SHuajun Li {
3189695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
31907a2af766SChao Yu 	int extra_size = get_extra_isize(inode);
3191cac5a3d8SDongOh Shin 
31927a2af766SChao Yu 	return (void *)&(ri->i_addr[extra_size + DEF_INLINE_RESERVED_SIZE]);
31931001b347SHuajun Li }
31941001b347SHuajun Li 
319534d67debSChao Yu static inline int f2fs_has_inline_dentry(struct inode *inode)
319634d67debSChao Yu {
319791942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DENTRY);
319834d67debSChao Yu }
319934d67debSChao Yu 
3200b5492af7SJaegeuk Kim static inline int is_file(struct inode *inode, int type)
3201b5492af7SJaegeuk Kim {
3202b5492af7SJaegeuk Kim 	return F2FS_I(inode)->i_advise & type;
3203b5492af7SJaegeuk Kim }
3204b5492af7SJaegeuk Kim 
3205b5492af7SJaegeuk Kim static inline void set_file(struct inode *inode, int type)
3206b5492af7SJaegeuk Kim {
3207766c6639SJaegeuk Kim 	if (is_file(inode, type))
3208766c6639SJaegeuk Kim 		return;
3209b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise |= type;
32107c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3211b5492af7SJaegeuk Kim }
3212b5492af7SJaegeuk Kim 
3213b5492af7SJaegeuk Kim static inline void clear_file(struct inode *inode, int type)
3214b5492af7SJaegeuk Kim {
3215766c6639SJaegeuk Kim 	if (!is_file(inode, type))
3216766c6639SJaegeuk Kim 		return;
3217b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise &= ~type;
32187c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3219b5492af7SJaegeuk Kim }
3220b5492af7SJaegeuk Kim 
3221fe1897eaSChao Yu static inline bool f2fs_is_time_consistent(struct inode *inode)
3222fe1897eaSChao Yu {
3223fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time, &inode->i_atime))
3224fe1897eaSChao Yu 		return false;
3225fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 1, &inode->i_ctime))
3226fe1897eaSChao Yu 		return false;
3227fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 2, &inode->i_mtime))
3228fe1897eaSChao Yu 		return false;
3229fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 3,
3230fe1897eaSChao Yu 						&F2FS_I(inode)->i_crtime))
3231fe1897eaSChao Yu 		return false;
3232fe1897eaSChao Yu 	return true;
3233fe1897eaSChao Yu }
3234fe1897eaSChao Yu 
323526787236SJaegeuk Kim static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
323626787236SJaegeuk Kim {
3237a0d00fadSChao Yu 	bool ret;
3238a0d00fadSChao Yu 
323926787236SJaegeuk Kim 	if (dsync) {
324026787236SJaegeuk Kim 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
324126787236SJaegeuk Kim 
324226787236SJaegeuk Kim 		spin_lock(&sbi->inode_lock[DIRTY_META]);
324326787236SJaegeuk Kim 		ret = list_empty(&F2FS_I(inode)->gdirty_list);
324426787236SJaegeuk Kim 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
324526787236SJaegeuk Kim 		return ret;
324626787236SJaegeuk Kim 	}
324726787236SJaegeuk Kim 	if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
324826787236SJaegeuk Kim 			file_keep_isize(inode) ||
3249235831d7SJunling Zheng 			i_size_read(inode) & ~PAGE_MASK)
325026787236SJaegeuk Kim 		return false;
3251a0d00fadSChao Yu 
3252fe1897eaSChao Yu 	if (!f2fs_is_time_consistent(inode))
3253214c2461SJaegeuk Kim 		return false;
3254214c2461SJaegeuk Kim 
3255c10c9820SChao Yu 	spin_lock(&F2FS_I(inode)->i_size_lock);
3256a0d00fadSChao Yu 	ret = F2FS_I(inode)->last_disk_size == i_size_read(inode);
3257c10c9820SChao Yu 	spin_unlock(&F2FS_I(inode)->i_size_lock);
3258a0d00fadSChao Yu 
3259a0d00fadSChao Yu 	return ret;
3260267378d4SChao Yu }
3261267378d4SChao Yu 
3262deeedd71SChao Yu static inline bool f2fs_readonly(struct super_block *sb)
326377888c1eSJaegeuk Kim {
3264deeedd71SChao Yu 	return sb_rdonly(sb);
326577888c1eSJaegeuk Kim }
326677888c1eSJaegeuk Kim 
3267744602cfSJaegeuk Kim static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
3268744602cfSJaegeuk Kim {
3269aaec2b1dSChao Yu 	return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
3270744602cfSJaegeuk Kim }
3271744602cfSJaegeuk Kim 
327243c780baSEric Biggers static inline bool is_dot_dotdot(const u8 *name, size_t len)
3273eaa693f4SJaegeuk Kim {
327443c780baSEric Biggers 	if (len == 1 && name[0] == '.')
3275eaa693f4SJaegeuk Kim 		return true;
3276eaa693f4SJaegeuk Kim 
327743c780baSEric Biggers 	if (len == 2 && name[0] == '.' && name[1] == '.')
3278eaa693f4SJaegeuk Kim 		return true;
3279eaa693f4SJaegeuk Kim 
3280eaa693f4SJaegeuk Kim 	return false;
3281eaa693f4SJaegeuk Kim }
3282eaa693f4SJaegeuk Kim 
32831ecc0c5cSChao Yu static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
32841ecc0c5cSChao Yu 					size_t size, gfp_t flags)
32850414b004SJaegeuk Kim {
328655523519SChao Yu 	if (time_to_inject(sbi, FAULT_KMALLOC)) {
3287c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_KMALLOC);
32882c63feadSJaegeuk Kim 		return NULL;
328955523519SChao Yu 	}
32907fa750a1SArnd Bergmann 
32910b6d4ca0SEric Biggers 	return kmalloc(size, flags);
32920414b004SJaegeuk Kim }
32930414b004SJaegeuk Kim 
3294acbf054dSChao Yu static inline void *f2fs_kzalloc(struct f2fs_sb_info *sbi,
3295acbf054dSChao Yu 					size_t size, gfp_t flags)
3296acbf054dSChao Yu {
3297acbf054dSChao Yu 	return f2fs_kmalloc(sbi, size, flags | __GFP_ZERO);
3298acbf054dSChao Yu }
3299acbf054dSChao Yu 
3300628b3d14SChao Yu static inline void *f2fs_kvmalloc(struct f2fs_sb_info *sbi,
3301628b3d14SChao Yu 					size_t size, gfp_t flags)
3302628b3d14SChao Yu {
3303628b3d14SChao Yu 	if (time_to_inject(sbi, FAULT_KVMALLOC)) {
3304c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_KVMALLOC);
3305628b3d14SChao Yu 		return NULL;
3306628b3d14SChao Yu 	}
33077fa750a1SArnd Bergmann 
3308628b3d14SChao Yu 	return kvmalloc(size, flags);
3309628b3d14SChao Yu }
3310628b3d14SChao Yu 
3311628b3d14SChao Yu static inline void *f2fs_kvzalloc(struct f2fs_sb_info *sbi,
3312628b3d14SChao Yu 					size_t size, gfp_t flags)
3313628b3d14SChao Yu {
3314628b3d14SChao Yu 	return f2fs_kvmalloc(sbi, size, flags | __GFP_ZERO);
3315628b3d14SChao Yu }
3316628b3d14SChao Yu 
33177a2af766SChao Yu static inline int get_extra_isize(struct inode *inode)
3318f2470371SChao Yu {
33197a2af766SChao Yu 	return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
3320f2470371SChao Yu }
3321f2470371SChao Yu 
33226afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode)
33236afc662eSChao Yu {
33246afc662eSChao Yu 	return F2FS_I(inode)->i_inline_xattr_size;
33256afc662eSChao Yu }
33266afc662eSChao Yu 
33274d57b86dSChao Yu #define f2fs_get_inode_mode(i) \
332891942321SJaegeuk Kim 	((is_inode_flag_set(i, FI_ACL_MODE)) ? \
3329a6dda0e6SChristoph Hellwig 	 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
3330a6dda0e6SChristoph Hellwig 
33317a2af766SChao Yu #define F2FS_TOTAL_EXTRA_ATTR_SIZE			\
33327a2af766SChao Yu 	(offsetof(struct f2fs_inode, i_extra_end) -	\
33337a2af766SChao Yu 	offsetof(struct f2fs_inode, i_extra_isize))	\
33347a2af766SChao Yu 
33355c57132eSChao Yu #define F2FS_OLD_ATTRIBUTE_SIZE	(offsetof(struct f2fs_inode, i_addr))
33365c57132eSChao Yu #define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field)		\
33372f84babfSSheng Yong 		((offsetof(typeof(*(f2fs_inode)), field) +	\
33385c57132eSChao Yu 		sizeof((f2fs_inode)->field))			\
33392f84babfSSheng Yong 		<= (F2FS_OLD_ATTRIBUTE_SIZE + (extra_isize)))	\
33405c57132eSChao Yu 
33412c70c5e3SChao Yu #define __is_large_section(sbi)		((sbi)->segs_per_sec > 1)
33422c70c5e3SChao Yu 
33436dc3a126SChao Yu #define __is_meta_io(fio) (PAGE_TYPE_OF_BIO((fio)->type) == META)
3344c9b60788SChao Yu 
3345e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
3346e1da7872SChao Yu 					block_t blkaddr, int type);
3347e1da7872SChao Yu static inline void verify_blkaddr(struct f2fs_sb_info *sbi,
3348e1da7872SChao Yu 					block_t blkaddr, int type)
3349e1da7872SChao Yu {
3350e1da7872SChao Yu 	if (!f2fs_is_valid_blkaddr(sbi, blkaddr, type)) {
3351dcbb4c10SJoe Perches 		f2fs_err(sbi, "invalid blkaddr: %u, type: %d, run fsck to fix.",
3352e1da7872SChao Yu 			 blkaddr, type);
3353e1da7872SChao Yu 		f2fs_bug_on(sbi, 1);
3354e1da7872SChao Yu 	}
3355e1da7872SChao Yu }
3356e1da7872SChao Yu 
3357e1da7872SChao Yu static inline bool __is_valid_data_blkaddr(block_t blkaddr)
33587b525dd0SChao Yu {
33594c8ff709SChao Yu 	if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR ||
33604c8ff709SChao Yu 			blkaddr == COMPRESS_ADDR)
33617b525dd0SChao Yu 		return false;
33627b525dd0SChao Yu 	return true;
33637b525dd0SChao Yu }
33647b525dd0SChao Yu 
336539a53e0cSJaegeuk Kim /*
336639a53e0cSJaegeuk Kim  * file.c
336739a53e0cSJaegeuk Kim  */
3368cac5a3d8SDongOh Shin int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
33694d57b86dSChao Yu void f2fs_truncate_data_blocks(struct dnode_of_data *dn);
33703265d3dbSChao Yu int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock);
3371c42d28ceSChao Yu int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock);
3372cac5a3d8SDongOh Shin int f2fs_truncate(struct inode *inode);
3373549c7297SChristian Brauner int f2fs_getattr(struct user_namespace *mnt_userns, const struct path *path,
3374549c7297SChristian Brauner 		 struct kstat *stat, u32 request_mask, unsigned int flags);
3375549c7297SChristian Brauner int f2fs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
3376549c7297SChristian Brauner 		 struct iattr *attr);
33774d57b86dSChao Yu int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
33784d57b86dSChao Yu void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count);
3379c4020b2dSChao Yu int f2fs_precache_extents(struct inode *inode);
33809b1bb01cSMiklos Szeredi int f2fs_fileattr_get(struct dentry *dentry, struct fileattr *fa);
33819b1bb01cSMiklos Szeredi int f2fs_fileattr_set(struct user_namespace *mnt_userns,
33829b1bb01cSMiklos Szeredi 		      struct dentry *dentry, struct fileattr *fa);
3383cac5a3d8SDongOh Shin long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
3384cac5a3d8SDongOh Shin long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
338578130819SChao Yu int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid);
33861ad71a27SJaegeuk Kim int f2fs_pin_file_control(struct inode *inode, bool inc);
338739a53e0cSJaegeuk Kim 
338839a53e0cSJaegeuk Kim /*
338939a53e0cSJaegeuk Kim  * inode.c
339039a53e0cSJaegeuk Kim  */
3391cac5a3d8SDongOh Shin void f2fs_set_inode_flags(struct inode *inode);
3392704956ecSChao Yu bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page);
3393704956ecSChao Yu void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page);
3394cac5a3d8SDongOh Shin struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
3395cac5a3d8SDongOh Shin struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
33964d57b86dSChao Yu int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
33974d57b86dSChao Yu void f2fs_update_inode(struct inode *inode, struct page *node_page);
33984d57b86dSChao Yu void f2fs_update_inode_page(struct inode *inode);
3399cac5a3d8SDongOh Shin int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
3400cac5a3d8SDongOh Shin void f2fs_evict_inode(struct inode *inode);
34014d57b86dSChao Yu void f2fs_handle_failed_inode(struct inode *inode);
340239a53e0cSJaegeuk Kim 
340339a53e0cSJaegeuk Kim /*
340439a53e0cSJaegeuk Kim  * namei.c
340539a53e0cSJaegeuk Kim  */
34064d57b86dSChao Yu int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
3407b6a06cbbSChao Yu 							bool hot, bool set);
340839a53e0cSJaegeuk Kim struct dentry *f2fs_get_parent(struct dentry *child);
340939a53e0cSJaegeuk Kim 
341039a53e0cSJaegeuk Kim /*
341139a53e0cSJaegeuk Kim  * dir.c
341239a53e0cSJaegeuk Kim  */
34134d57b86dSChao Yu unsigned char f2fs_get_de_type(struct f2fs_dir_entry *de);
341443c780baSEric Biggers int f2fs_init_casefolded_name(const struct inode *dir,
341543c780baSEric Biggers 			      struct f2fs_filename *fname);
341643c780baSEric Biggers int f2fs_setup_filename(struct inode *dir, const struct qstr *iname,
341743c780baSEric Biggers 			int lookup, struct f2fs_filename *fname);
341843c780baSEric Biggers int f2fs_prepare_lookup(struct inode *dir, struct dentry *dentry,
341943c780baSEric Biggers 			struct f2fs_filename *fname);
342043c780baSEric Biggers void f2fs_free_filename(struct f2fs_filename *fname);
342143c780baSEric Biggers struct f2fs_dir_entry *f2fs_find_target_dentry(const struct f2fs_dentry_ptr *d,
342243c780baSEric Biggers 			const struct f2fs_filename *fname, int *max_slots);
3423cac5a3d8SDongOh Shin int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
3424cac5a3d8SDongOh Shin 			unsigned int start_pos, struct fscrypt_str *fstr);
34254d57b86dSChao Yu void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
3426cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
34274d57b86dSChao Yu struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
342843c780baSEric Biggers 			const struct f2fs_filename *fname, struct page *dpage);
34294d57b86dSChao Yu void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
3430cac5a3d8SDongOh Shin 			unsigned int current_depth);
34314d57b86dSChao Yu int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots);
3432cac5a3d8SDongOh Shin void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
3433cac5a3d8SDongOh Shin struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
343443c780baSEric Biggers 					 const struct f2fs_filename *fname,
343543c780baSEric Biggers 					 struct page **res_page);
3436cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
3437cac5a3d8SDongOh Shin 			const struct qstr *child, struct page **res_page);
3438cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
3439cac5a3d8SDongOh Shin ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
3440cac5a3d8SDongOh Shin 			struct page **page);
3441cac5a3d8SDongOh Shin void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
3442cac5a3d8SDongOh Shin 			struct page *page, struct inode *inode);
3443b06af2afSJaegeuk Kim bool f2fs_has_enough_room(struct inode *dir, struct page *ipage,
344443c780baSEric Biggers 			  const struct f2fs_filename *fname);
3445cac5a3d8SDongOh Shin void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
344643c780baSEric Biggers 			const struct fscrypt_str *name, f2fs_hash_t name_hash,
3447cac5a3d8SDongOh Shin 			unsigned int bit_pos);
344843c780baSEric Biggers int f2fs_add_regular_entry(struct inode *dir, const struct f2fs_filename *fname,
3449cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
345043c780baSEric Biggers int f2fs_add_dentry(struct inode *dir, const struct f2fs_filename *fname,
3451cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
34524d57b86dSChao Yu int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
3453cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
3454cac5a3d8SDongOh Shin void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
3455cac5a3d8SDongOh Shin 			struct inode *dir, struct inode *inode);
3456cac5a3d8SDongOh Shin int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
3457cac5a3d8SDongOh Shin bool f2fs_empty_dir(struct inode *dir);
345839a53e0cSJaegeuk Kim 
3459b7f7a5e0SAl Viro static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
3460b7f7a5e0SAl Viro {
3461bfc2b7e8SEric Biggers 	if (fscrypt_is_nokey_name(dentry))
3462bfc2b7e8SEric Biggers 		return -ENOKEY;
34634d57b86dSChao Yu 	return f2fs_do_add_link(d_inode(dentry->d_parent), &dentry->d_name,
3464510022a8SJaegeuk Kim 				inode, inode->i_ino, inode->i_mode);
3465b7f7a5e0SAl Viro }
3466b7f7a5e0SAl Viro 
346739a53e0cSJaegeuk Kim /*
346839a53e0cSJaegeuk Kim  * super.c
346939a53e0cSJaegeuk Kim  */
3470cac5a3d8SDongOh Shin int f2fs_inode_dirtied(struct inode *inode, bool sync);
3471cac5a3d8SDongOh Shin void f2fs_inode_synced(struct inode *inode);
347210a26878SChao Yu int f2fs_dquot_initialize(struct inode *inode);
3473ea676733SJaegeuk Kim int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly);
3474af033b2aSChao Yu int f2fs_quota_sync(struct super_block *sb, int type);
34756d1451bfSChengguang Xu loff_t max_file_blocks(struct inode *inode);
34764b2414d0SChao Yu void f2fs_quota_off_umount(struct super_block *sb);
3477cac5a3d8SDongOh Shin int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
3478cac5a3d8SDongOh Shin int f2fs_sync_fs(struct super_block *sb, int sync);
34794d57b86dSChao Yu int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi);
348039a53e0cSJaegeuk Kim 
348139a53e0cSJaegeuk Kim /*
348239a53e0cSJaegeuk Kim  * hash.c
348339a53e0cSJaegeuk Kim  */
348443c780baSEric Biggers void f2fs_hash_filename(const struct inode *dir, struct f2fs_filename *fname);
348539a53e0cSJaegeuk Kim 
348639a53e0cSJaegeuk Kim /*
348739a53e0cSJaegeuk Kim  * node.c
348839a53e0cSJaegeuk Kim  */
348939a53e0cSJaegeuk Kim struct node_info;
349039a53e0cSJaegeuk Kim 
34914d57b86dSChao Yu int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid);
34924d57b86dSChao Yu bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type);
349350fa53ecSChao Yu bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct page *page);
349450fa53ecSChao Yu void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi);
349550fa53ecSChao Yu void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct page *page);
349650fa53ecSChao Yu void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi);
34974d57b86dSChao Yu int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
34984d57b86dSChao Yu bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
34994d57b86dSChao Yu bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
35007735730dSChao Yu int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
3501a9419b63SJaegeuk Kim 				struct node_info *ni, bool checkpoint_context);
35024d57b86dSChao Yu pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
35034d57b86dSChao Yu int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
35044d57b86dSChao Yu int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from);
35054d57b86dSChao Yu int f2fs_truncate_xattr_node(struct inode *inode);
350650fa53ecSChao Yu int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
350750fa53ecSChao Yu 					unsigned int seq_id);
350894c821fbSChao Yu bool f2fs_nat_bitmap_enabled(struct f2fs_sb_info *sbi);
35094d57b86dSChao Yu int f2fs_remove_inode_page(struct inode *inode);
35104d57b86dSChao Yu struct page *f2fs_new_inode_page(struct inode *inode);
35114d57b86dSChao Yu struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs);
35124d57b86dSChao Yu void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
35134d57b86dSChao Yu struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
35144d57b86dSChao Yu struct page *f2fs_get_node_page_ra(struct page *parent, int start);
351548018b4cSChao Yu int f2fs_move_node_page(struct page *node_page, int gc_type);
351668e79bafSJia Yang void f2fs_flush_inline_data(struct f2fs_sb_info *sbi);
35174d57b86dSChao Yu int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
351850fa53ecSChao Yu 			struct writeback_control *wbc, bool atomic,
351950fa53ecSChao Yu 			unsigned int *seq_id);
35204d57b86dSChao Yu int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
35214d57b86dSChao Yu 			struct writeback_control *wbc,
3522b0af6d49SChao Yu 			bool do_balance, enum iostat_type io_type);
3523e2374015SChao Yu int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
35244d57b86dSChao Yu bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
35254d57b86dSChao Yu void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
35264d57b86dSChao Yu void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
35274d57b86dSChao Yu int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
35289627a7b3SChao Yu int f2fs_recover_inline_xattr(struct inode *inode, struct page *page);
35294d57b86dSChao Yu int f2fs_recover_xattr_data(struct inode *inode, struct page *page);
35304d57b86dSChao Yu int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
35317735730dSChao Yu int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
3532cac5a3d8SDongOh Shin 			unsigned int segno, struct f2fs_summary_block *sum);
353394c821fbSChao Yu void f2fs_enable_nat_bits(struct f2fs_sb_info *sbi);
3534edc55aafSJaegeuk Kim int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
35354d57b86dSChao Yu int f2fs_build_node_manager(struct f2fs_sb_info *sbi);
35364d57b86dSChao Yu void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi);
35374d57b86dSChao Yu int __init f2fs_create_node_manager_caches(void);
35384d57b86dSChao Yu void f2fs_destroy_node_manager_caches(void);
353939a53e0cSJaegeuk Kim 
354039a53e0cSJaegeuk Kim /*
354139a53e0cSJaegeuk Kim  * segment.c
354239a53e0cSJaegeuk Kim  */
35434d57b86dSChao Yu bool f2fs_need_SSR(struct f2fs_sb_info *sbi);
35444d57b86dSChao Yu void f2fs_register_inmem_page(struct inode *inode, struct page *page);
35454d57b86dSChao Yu void f2fs_drop_inmem_pages_all(struct f2fs_sb_info *sbi, bool gc_failure);
35464d57b86dSChao Yu void f2fs_drop_inmem_pages(struct inode *inode);
35474d57b86dSChao Yu void f2fs_drop_inmem_page(struct inode *inode, struct page *page);
35484d57b86dSChao Yu int f2fs_commit_inmem_pages(struct inode *inode);
3549cac5a3d8SDongOh Shin void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
35507bcd0cfaSChao Yu void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi, bool from_bg);
355139d787beSChao Yu int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
35524d57b86dSChao Yu int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
35531228b482SChao Yu int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
35544d57b86dSChao Yu void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
35554d57b86dSChao Yu void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
35564d57b86dSChao Yu bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
35574d674904SFengnan Chang int f2fs_start_discard_thread(struct f2fs_sb_info *sbi);
35584d57b86dSChao Yu void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
35594d57b86dSChao Yu void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi);
356003f2c02dSJaegeuk Kim bool f2fs_issue_discard_timeout(struct f2fs_sb_info *sbi);
35614d57b86dSChao Yu void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
35624d57b86dSChao Yu 					struct cp_control *cpc);
35634354994fSDaniel Rosenberg void f2fs_dirty_to_prefree(struct f2fs_sb_info *sbi);
35644d3aed70SDaniel Rosenberg block_t f2fs_get_unusable_blocks(struct f2fs_sb_info *sbi);
35654d3aed70SDaniel Rosenberg int f2fs_disable_cp_again(struct f2fs_sb_info *sbi, block_t unusable);
35664d57b86dSChao Yu void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi);
35674d57b86dSChao Yu int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
356861461fc9SChao Yu bool f2fs_segment_has_free_slot(struct f2fs_sb_info *sbi, int segno);
3569093749e2SChao Yu void f2fs_init_inmem_curseg(struct f2fs_sb_info *sbi);
3570093749e2SChao Yu void f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi);
3571093749e2SChao Yu void f2fs_restore_inmem_curseg(struct f2fs_sb_info *sbi);
3572093749e2SChao Yu void f2fs_get_new_segment(struct f2fs_sb_info *sbi,
3573093749e2SChao Yu 			unsigned int *newseg, bool new_sec, int dir);
35740ef81833SChao Yu void f2fs_allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type,
357504f0b2eaSQiuyang Sun 					unsigned int start, unsigned int end);
3576509f1010SChao Yu void f2fs_allocate_new_section(struct f2fs_sb_info *sbi, int type, bool force);
3577901d745fSChao Yu void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi);
3578cac5a3d8SDongOh Shin int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
35794d57b86dSChao Yu bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
35804d57b86dSChao Yu 					struct cp_control *cpc);
35814d57b86dSChao Yu struct page *f2fs_get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
35824d57b86dSChao Yu void f2fs_update_meta_page(struct f2fs_sb_info *sbi, void *src,
35834d57b86dSChao Yu 					block_t blk_addr);
35844d57b86dSChao Yu void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
3585b0af6d49SChao Yu 						enum iostat_type io_type);
35864d57b86dSChao Yu void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio);
35874d57b86dSChao Yu void f2fs_outplace_write_data(struct dnode_of_data *dn,
35884d57b86dSChao Yu 			struct f2fs_io_info *fio);
35894d57b86dSChao Yu int f2fs_inplace_write_data(struct f2fs_io_info *fio);
35904d57b86dSChao Yu void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
3591cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t new_blkaddr,
3592c5d02785SChao Yu 			bool recover_curseg, bool recover_newaddr,
3593c5d02785SChao Yu 			bool from_gc);
3594cac5a3d8SDongOh Shin void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
3595cac5a3d8SDongOh Shin 			block_t old_addr, block_t new_addr,
3596cac5a3d8SDongOh Shin 			unsigned char version, bool recover_curseg,
3597cac5a3d8SDongOh Shin 			bool recover_newaddr);
35984d57b86dSChao Yu void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
3599cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t *new_blkaddr,
3600fb830fc5SChao Yu 			struct f2fs_summary *sum, int type,
3601f608c38cSChao Yu 			struct f2fs_io_info *fio);
360271f2c820SChao Yu void f2fs_update_device_state(struct f2fs_sb_info *sbi, nid_t ino,
360371f2c820SChao Yu 					block_t blkaddr, unsigned int blkcnt);
3604cac5a3d8SDongOh Shin void f2fs_wait_on_page_writeback(struct page *page,
3605bae0ee7aSChao Yu 			enum page_type type, bool ordered, bool locked);
36060ded69f6SJaegeuk Kim void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr);
36071e78e8bdSSahitya Tummala void f2fs_wait_on_block_writeback_range(struct inode *inode, block_t blkaddr,
36081e78e8bdSSahitya Tummala 								block_t len);
36094d57b86dSChao Yu void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
36104d57b86dSChao Yu void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
36114d57b86dSChao Yu int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
3612cac5a3d8SDongOh Shin 			unsigned int val, int alloc);
36134d57b86dSChao Yu void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
3614c426d991SShin'ichiro Kawasaki int f2fs_fix_curseg_write_pointer(struct f2fs_sb_info *sbi);
3615d508c94eSShin'ichiro Kawasaki int f2fs_check_write_pointer(struct f2fs_sb_info *sbi);
36164d57b86dSChao Yu int f2fs_build_segment_manager(struct f2fs_sb_info *sbi);
36174d57b86dSChao Yu void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi);
36184d57b86dSChao Yu int __init f2fs_create_segment_manager_caches(void);
36194d57b86dSChao Yu void f2fs_destroy_segment_manager_caches(void);
36204d57b86dSChao Yu int f2fs_rw_hint_to_seg_type(enum rw_hint hint);
36214d57b86dSChao Yu enum rw_hint f2fs_io_type_to_rw_hint(struct f2fs_sb_info *sbi,
36224d57b86dSChao Yu 			enum page_type type, enum temp_type temp);
3623de881df9SAravind Ramesh unsigned int f2fs_usable_segs_in_sec(struct f2fs_sb_info *sbi,
3624de881df9SAravind Ramesh 			unsigned int segno);
3625de881df9SAravind Ramesh unsigned int f2fs_usable_blks_in_seg(struct f2fs_sb_info *sbi,
3626de881df9SAravind Ramesh 			unsigned int segno);
362739a53e0cSJaegeuk Kim 
36286691d940SDaeho Jeong #define DEF_FRAGMENT_SIZE	4
36296691d940SDaeho Jeong #define MIN_FRAGMENT_SIZE	1
36306691d940SDaeho Jeong #define MAX_FRAGMENT_SIZE	512
36316691d940SDaeho Jeong 
36326691d940SDaeho Jeong static inline bool f2fs_need_rand_seg(struct f2fs_sb_info *sbi)
36336691d940SDaeho Jeong {
36346691d940SDaeho Jeong 	return F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_SEG ||
36356691d940SDaeho Jeong 		F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK;
36366691d940SDaeho Jeong }
36376691d940SDaeho Jeong 
363839a53e0cSJaegeuk Kim /*
363939a53e0cSJaegeuk Kim  * checkpoint.c
364039a53e0cSJaegeuk Kim  */
3641cac5a3d8SDongOh Shin void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io);
36424d57b86dSChao Yu struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
36434d57b86dSChao Yu struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
364486f33603SJaegeuk Kim struct page *f2fs_get_meta_page_retry(struct f2fs_sb_info *sbi, pgoff_t index);
36454d57b86dSChao Yu struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
3646e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
36474d57b86dSChao Yu 					block_t blkaddr, int type);
36484d57b86dSChao Yu int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
3649cac5a3d8SDongOh Shin 			int type, bool sync);
36504d57b86dSChao Yu void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index);
36514d57b86dSChao Yu long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
3652b0af6d49SChao Yu 			long nr_to_write, enum iostat_type io_type);
36534d57b86dSChao Yu void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
36544d57b86dSChao Yu void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
36554d57b86dSChao Yu void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all);
36564d57b86dSChao Yu bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
36574d57b86dSChao Yu void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
365839d787beSChao Yu 					unsigned int devidx, int type);
36594d57b86dSChao Yu bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
366039d787beSChao Yu 					unsigned int devidx, int type);
3661cac5a3d8SDongOh Shin int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
36624d57b86dSChao Yu int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi);
36634d57b86dSChao Yu void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi);
36644d57b86dSChao Yu void f2fs_add_orphan_inode(struct inode *inode);
36654d57b86dSChao Yu void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
36664d57b86dSChao Yu int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi);
36674d57b86dSChao Yu int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi);
36684d57b86dSChao Yu void f2fs_update_dirty_page(struct inode *inode, struct page *page);
36694d57b86dSChao Yu void f2fs_remove_dirty_inode(struct inode *inode);
36704d57b86dSChao Yu int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
3671bf22c3ccSSahitya Tummala void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type);
36723a0a9cbcSChao Yu u64 f2fs_get_sectors_written(struct f2fs_sb_info *sbi);
36734d57b86dSChao Yu int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
36744d57b86dSChao Yu void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi);
36754d57b86dSChao Yu int __init f2fs_create_checkpoint_caches(void);
36764d57b86dSChao Yu void f2fs_destroy_checkpoint_caches(void);
3677261eeb9cSDaeho Jeong int f2fs_issue_checkpoint(struct f2fs_sb_info *sbi);
3678261eeb9cSDaeho Jeong int f2fs_start_ckpt_thread(struct f2fs_sb_info *sbi);
3679261eeb9cSDaeho Jeong void f2fs_stop_ckpt_thread(struct f2fs_sb_info *sbi);
3680261eeb9cSDaeho Jeong void f2fs_init_ckpt_req_control(struct f2fs_sb_info *sbi);
368139a53e0cSJaegeuk Kim 
368239a53e0cSJaegeuk Kim /*
368339a53e0cSJaegeuk Kim  * data.c
368439a53e0cSJaegeuk Kim  */
3685f543805fSChao Yu int __init f2fs_init_bioset(void);
3686f543805fSChao Yu void f2fs_destroy_bioset(void);
36870b20fcecSChao Yu int f2fs_init_bio_entry_cache(void);
36880b20fcecSChao Yu void f2fs_destroy_bio_entry_cache(void);
36894c8ff709SChao Yu void f2fs_submit_bio(struct f2fs_sb_info *sbi,
36904c8ff709SChao Yu 				struct bio *bio, enum page_type type);
3691b9109b0eSJaegeuk Kim void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
3692b9109b0eSJaegeuk Kim void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
3693bab475c5SChao Yu 				struct inode *inode, struct page *page,
3694bab475c5SChao Yu 				nid_t ino, enum page_type type);
36950b20fcecSChao Yu void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
36960b20fcecSChao Yu 					struct bio **bio, struct page *page);
3697b9109b0eSJaegeuk Kim void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
3698cac5a3d8SDongOh Shin int f2fs_submit_page_bio(struct f2fs_io_info *fio);
36998648de2cSChao Yu int f2fs_merge_page_bio(struct f2fs_io_info *fio);
3700fe16efe6SChao Yu void f2fs_submit_page_write(struct f2fs_io_info *fio);
3701cac5a3d8SDongOh Shin struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
3702cac5a3d8SDongOh Shin 			block_t blk_addr, struct bio *bio);
3703cac5a3d8SDongOh Shin int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
37044d57b86dSChao Yu void f2fs_set_data_blkaddr(struct dnode_of_data *dn);
3705cac5a3d8SDongOh Shin void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
37064d57b86dSChao Yu int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
37074d57b86dSChao Yu int f2fs_reserve_new_block(struct dnode_of_data *dn);
3708cac5a3d8SDongOh Shin int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
3709cac5a3d8SDongOh Shin int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
37104d57b86dSChao Yu struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
3711cac5a3d8SDongOh Shin 			int op_flags, bool for_write);
37124d57b86dSChao Yu struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index);
37134d57b86dSChao Yu struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
3714cac5a3d8SDongOh Shin 			bool for_write);
37154d57b86dSChao Yu struct page *f2fs_get_new_data_page(struct inode *inode,
3716cac5a3d8SDongOh Shin 			struct page *ipage, pgoff_t index, bool new_i_size);
37174d57b86dSChao Yu int f2fs_do_write_data_page(struct f2fs_io_info *fio);
37180ef81833SChao Yu void f2fs_do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock);
3719cac5a3d8SDongOh Shin int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
3720cac5a3d8SDongOh Shin 			int create, int flag);
3721cac5a3d8SDongOh Shin int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3722cac5a3d8SDongOh Shin 			u64 start, u64 len);
37234c8ff709SChao Yu int f2fs_encrypt_one_page(struct f2fs_io_info *fio);
37244d57b86dSChao Yu bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio);
37254d57b86dSChao Yu bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio);
37264c8ff709SChao Yu int f2fs_write_single_data_page(struct page *page, int *submitted,
37274c8ff709SChao Yu 				struct bio **bio, sector_t *last_block,
37284c8ff709SChao Yu 				struct writeback_control *wbc,
37294c8ff709SChao Yu 				enum iostat_type io_type,
37303afae09fSChao Yu 				int compr_blocks, bool allow_balance);
3731a1e09b03SEric Biggers void f2fs_write_failed(struct inode *inode, loff_t to);
3732cac5a3d8SDongOh Shin void f2fs_invalidate_page(struct page *page, unsigned int offset,
3733cac5a3d8SDongOh Shin 			unsigned int length);
3734cac5a3d8SDongOh Shin int f2fs_release_page(struct page *page, gfp_t wait);
37355b7a487cSWeichao Guo #ifdef CONFIG_MIGRATION
3736cac5a3d8SDongOh Shin int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,
3737cac5a3d8SDongOh Shin 			struct page *page, enum migrate_mode mode);
37385b7a487cSWeichao Guo #endif
3739b91050a8SHyunchul Lee bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len);
37405ec2d99dSMatthew Wilcox void f2fs_clear_page_cache_dirty_tag(struct page *page);
37414c8ff709SChao Yu int f2fs_init_post_read_processing(void);
37424c8ff709SChao Yu void f2fs_destroy_post_read_processing(void);
37434c8ff709SChao Yu int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi);
37444c8ff709SChao Yu void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi);
37451517c1a7SEric Biggers extern const struct iomap_ops f2fs_iomap_ops;
374639a53e0cSJaegeuk Kim 
374739a53e0cSJaegeuk Kim /*
374839a53e0cSJaegeuk Kim  * gc.c
374939a53e0cSJaegeuk Kim  */
37504d57b86dSChao Yu int f2fs_start_gc_thread(struct f2fs_sb_info *sbi);
37514d57b86dSChao Yu void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi);
37524d57b86dSChao Yu block_t f2fs_start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
37537dede886SChao Yu int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background, bool force,
3754e066b83cSJaegeuk Kim 			unsigned int segno);
37554d57b86dSChao Yu void f2fs_build_gc_manager(struct f2fs_sb_info *sbi);
375604f0b2eaSQiuyang Sun int f2fs_resize_fs(struct f2fs_sb_info *sbi, __u64 block_count);
3757093749e2SChao Yu int __init f2fs_create_garbage_collection_cache(void);
3758093749e2SChao Yu void f2fs_destroy_garbage_collection_cache(void);
375939a53e0cSJaegeuk Kim 
376039a53e0cSJaegeuk Kim /*
376139a53e0cSJaegeuk Kim  * recovery.c
376239a53e0cSJaegeuk Kim  */
37634d57b86dSChao Yu int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
37644d57b86dSChao Yu bool f2fs_space_for_roll_forward(struct f2fs_sb_info *sbi);
3765cad83c96SChao Yu int __init f2fs_create_recovery_cache(void);
3766cad83c96SChao Yu void f2fs_destroy_recovery_cache(void);
376739a53e0cSJaegeuk Kim 
376839a53e0cSJaegeuk Kim /*
376939a53e0cSJaegeuk Kim  * debug.c
377039a53e0cSJaegeuk Kim  */
377139a53e0cSJaegeuk Kim #ifdef CONFIG_F2FS_STAT_FS
377239a53e0cSJaegeuk Kim struct f2fs_stat_info {
377339a53e0cSJaegeuk Kim 	struct list_head stat_list;
377439a53e0cSJaegeuk Kim 	struct f2fs_sb_info *sbi;
377539a53e0cSJaegeuk Kim 	int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
377639a53e0cSJaegeuk Kim 	int main_area_segs, main_area_sections, main_area_zones;
37775b7ee374SChao Yu 	unsigned long long hit_largest, hit_cached, hit_rbtree;
37785b7ee374SChao Yu 	unsigned long long hit_total, total_ext;
3779c00ba554SJaegeuk Kim 	int ext_tree, zombie_tree, ext_node;
37802c8a4a28SJaegeuk Kim 	int ndirty_node, ndirty_dent, ndirty_meta, ndirty_imeta;
37812c8a4a28SJaegeuk Kim 	int ndirty_data, ndirty_qdata;
378235782b23SJaegeuk Kim 	int inmem_pages;
37832c8a4a28SJaegeuk Kim 	unsigned int ndirty_dirs, ndirty_files, nquota_files, ndirty_all;
37845b0ef73cSJaegeuk Kim 	int nats, dirty_nats, sits, dirty_sits;
37855b0ef73cSJaegeuk Kim 	int free_nids, avail_nids, alloc_nids;
378639a53e0cSJaegeuk Kim 	int total_count, utilization;
37878b8dd65fSChao Yu 	int bg_gc, nr_wb_cp_data, nr_wb_data;
37885f9abab4SJaegeuk Kim 	int nr_rd_data, nr_rd_node, nr_rd_meta;
378902b16d0aSChao Yu 	int nr_dio_read, nr_dio_write;
3790274bd9baSChao Yu 	unsigned int io_skip_bggc, other_skip_bggc;
379114d8d5f7SChao Yu 	int nr_flushing, nr_flushed, flush_list_empty;
379214d8d5f7SChao Yu 	int nr_discarding, nr_discarded;
37935f32366aSChao Yu 	int nr_discard_cmd;
3794d84d1cbdSChao Yu 	unsigned int undiscard_blks;
3795261eeb9cSDaeho Jeong 	int nr_issued_ckpt, nr_total_ckpt, nr_queued_ckpt;
3796261eeb9cSDaeho Jeong 	unsigned int cur_ckpt_time, peak_ckpt_time;
3797a00861dbSJaegeuk Kim 	int inline_xattr, inline_inode, inline_dir, append, update, orphans;
3798ae999bb9SDaeho Jeong 	int compr_inode;
3799ae999bb9SDaeho Jeong 	unsigned long long compr_blocks;
3800648d50baSChao Yu 	int aw_cnt, max_aw_cnt, vw_cnt, max_vw_cnt;
3801f83a2584SYunlei He 	unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
380239a53e0cSJaegeuk Kim 	unsigned int bimodal, avg_vblocks;
380339a53e0cSJaegeuk Kim 	int util_free, util_valid, util_invalid;
380439a53e0cSJaegeuk Kim 	int rsvd_segs, overp_segs;
38056ce19affSChao Yu 	int dirty_count, node_pages, meta_pages, compress_pages;
38066ce19affSChao Yu 	int compress_page_hit;
380742190d2aSJaegeuk Kim 	int prefree_count, call_count, cp_count, bg_cp_count;
380839a53e0cSJaegeuk Kim 	int tot_segs, node_segs, data_segs, free_segs, free_secs;
3809e1235983SChangman Lee 	int bg_node_segs, bg_data_segs;
381039a53e0cSJaegeuk Kim 	int tot_blks, data_blks, node_blks;
3811e1235983SChangman Lee 	int bg_data_blks, bg_node_blks;
38122ef79ecbSChao Yu 	unsigned long long skipped_atomic_files[2];
381339a53e0cSJaegeuk Kim 	int curseg[NR_CURSEG_TYPE];
381439a53e0cSJaegeuk Kim 	int cursec[NR_CURSEG_TYPE];
381539a53e0cSJaegeuk Kim 	int curzone[NR_CURSEG_TYPE];
38160759e2c1SChao Yu 	unsigned int dirty_seg[NR_CURSEG_TYPE];
38170759e2c1SChao Yu 	unsigned int full_seg[NR_CURSEG_TYPE];
38180759e2c1SChao Yu 	unsigned int valid_blks[NR_CURSEG_TYPE];
381939a53e0cSJaegeuk Kim 
3820b63e7be5SChao Yu 	unsigned int meta_count[META_MAX];
382139a53e0cSJaegeuk Kim 	unsigned int segment_count[2];
382239a53e0cSJaegeuk Kim 	unsigned int block_count[2];
3823b9a2c252SChangman Lee 	unsigned int inplace_count;
38249edcdabfSChao Yu 	unsigned long long base_mem, cache_mem, page_mem;
382539a53e0cSJaegeuk Kim };
382639a53e0cSJaegeuk Kim 
3827963d4f7dSGu Zheng static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
3828963d4f7dSGu Zheng {
3829963d4f7dSGu Zheng 	return (struct f2fs_stat_info *)sbi->stat_info;
3830963d4f7dSGu Zheng }
3831963d4f7dSGu Zheng 
3832942e0be6SChangman Lee #define stat_inc_cp_count(si)		((si)->cp_count++)
383342190d2aSJaegeuk Kim #define stat_inc_bg_cp_count(si)	((si)->bg_cp_count++)
383439a53e0cSJaegeuk Kim #define stat_inc_call_count(si)		((si)->call_count++)
3835fc7100eaSHridya Valsaraju #define stat_inc_bggc_count(si)		((si)->bg_gc++)
3836274bd9baSChao Yu #define stat_io_skip_bggc_count(sbi)	((sbi)->io_skip_bggc++)
3837274bd9baSChao Yu #define stat_other_skip_bggc_count(sbi)	((sbi)->other_skip_bggc++)
383833fbd510SChao Yu #define stat_inc_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]++)
383933fbd510SChao Yu #define stat_dec_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]--)
38405b7ee374SChao Yu #define stat_inc_total_hit(sbi)		(atomic64_inc(&(sbi)->total_hit_ext))
38415b7ee374SChao Yu #define stat_inc_rbtree_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_rbtree))
38425b7ee374SChao Yu #define stat_inc_largest_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_largest))
38435b7ee374SChao Yu #define stat_inc_cached_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_cached))
3844d5e8f6c9SChao Yu #define stat_inc_inline_xattr(inode)					\
3845d5e8f6c9SChao Yu 	do {								\
3846d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3847d5e8f6c9SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_xattr));	\
3848d5e8f6c9SChao Yu 	} while (0)
3849d5e8f6c9SChao Yu #define stat_dec_inline_xattr(inode)					\
3850d5e8f6c9SChao Yu 	do {								\
3851d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3852d5e8f6c9SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_xattr));	\
3853d5e8f6c9SChao Yu 	} while (0)
38540dbdc2aeSJaegeuk Kim #define stat_inc_inline_inode(inode)					\
38550dbdc2aeSJaegeuk Kim 	do {								\
38560dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
385703e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_inode));	\
38580dbdc2aeSJaegeuk Kim 	} while (0)
38590dbdc2aeSJaegeuk Kim #define stat_dec_inline_inode(inode)					\
38600dbdc2aeSJaegeuk Kim 	do {								\
38610dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
386203e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_inode));	\
38630dbdc2aeSJaegeuk Kim 	} while (0)
38643289c061SJaegeuk Kim #define stat_inc_inline_dir(inode)					\
38653289c061SJaegeuk Kim 	do {								\
38663289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
386703e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_dir));	\
38683289c061SJaegeuk Kim 	} while (0)
38693289c061SJaegeuk Kim #define stat_dec_inline_dir(inode)					\
38703289c061SJaegeuk Kim 	do {								\
38713289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
387203e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_dir));	\
38733289c061SJaegeuk Kim 	} while (0)
38744c8ff709SChao Yu #define stat_inc_compr_inode(inode)					\
38754c8ff709SChao Yu 	do {								\
38764c8ff709SChao Yu 		if (f2fs_compressed_file(inode))			\
38774c8ff709SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->compr_inode));	\
38784c8ff709SChao Yu 	} while (0)
38794c8ff709SChao Yu #define stat_dec_compr_inode(inode)					\
38804c8ff709SChao Yu 	do {								\
38814c8ff709SChao Yu 		if (f2fs_compressed_file(inode))			\
38824c8ff709SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->compr_inode));	\
38834c8ff709SChao Yu 	} while (0)
38844c8ff709SChao Yu #define stat_add_compr_blocks(inode, blocks)				\
3885ae999bb9SDaeho Jeong 		(atomic64_add(blocks, &F2FS_I_SB(inode)->compr_blocks))
38864c8ff709SChao Yu #define stat_sub_compr_blocks(inode, blocks)				\
3887ae999bb9SDaeho Jeong 		(atomic64_sub(blocks, &F2FS_I_SB(inode)->compr_blocks))
3888b63e7be5SChao Yu #define stat_inc_meta_count(sbi, blkaddr)				\
3889b63e7be5SChao Yu 	do {								\
3890b63e7be5SChao Yu 		if (blkaddr < SIT_I(sbi)->sit_base_addr)		\
3891b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_CP]);	\
3892b63e7be5SChao Yu 		else if (blkaddr < NM_I(sbi)->nat_blkaddr)		\
3893b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_SIT]);	\
3894b63e7be5SChao Yu 		else if (blkaddr < SM_I(sbi)->ssa_blkaddr)		\
3895b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_NAT]);	\
3896b63e7be5SChao Yu 		else if (blkaddr < SM_I(sbi)->main_blkaddr)		\
3897b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_SSA]);	\
3898b63e7be5SChao Yu 	} while (0)
3899dcdfff65SJaegeuk Kim #define stat_inc_seg_type(sbi, curseg)					\
3900dcdfff65SJaegeuk Kim 		((sbi)->segment_count[(curseg)->alloc_type]++)
3901dcdfff65SJaegeuk Kim #define stat_inc_block_count(sbi, curseg)				\
3902dcdfff65SJaegeuk Kim 		((sbi)->block_count[(curseg)->alloc_type]++)
3903b9a2c252SChangman Lee #define stat_inc_inplace_blocks(sbi)					\
3904b9a2c252SChangman Lee 		(atomic_inc(&(sbi)->inplace_count))
390526a28a0cSJaegeuk Kim #define stat_update_max_atomic_write(inode)				\
390626a28a0cSJaegeuk Kim 	do {								\
39070e6d0164SSahitya Tummala 		int cur = F2FS_I_SB(inode)->atomic_files;	\
390826a28a0cSJaegeuk Kim 		int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt);	\
390926a28a0cSJaegeuk Kim 		if (cur > max)						\
391026a28a0cSJaegeuk Kim 			atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur);	\
391126a28a0cSJaegeuk Kim 	} while (0)
3912648d50baSChao Yu #define stat_inc_volatile_write(inode)					\
3913648d50baSChao Yu 		(atomic_inc(&F2FS_I_SB(inode)->vw_cnt))
3914648d50baSChao Yu #define stat_dec_volatile_write(inode)					\
3915648d50baSChao Yu 		(atomic_dec(&F2FS_I_SB(inode)->vw_cnt))
3916648d50baSChao Yu #define stat_update_max_volatile_write(inode)				\
3917648d50baSChao Yu 	do {								\
3918648d50baSChao Yu 		int cur = atomic_read(&F2FS_I_SB(inode)->vw_cnt);	\
3919648d50baSChao Yu 		int max = atomic_read(&F2FS_I_SB(inode)->max_vw_cnt);	\
3920648d50baSChao Yu 		if (cur > max)						\
3921648d50baSChao Yu 			atomic_set(&F2FS_I_SB(inode)->max_vw_cnt, cur);	\
3922648d50baSChao Yu 	} while (0)
3923e1235983SChangman Lee #define stat_inc_seg_count(sbi, type, gc_type)				\
392439a53e0cSJaegeuk Kim 	do {								\
3925963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
392668afcf2dSTomohiro Kusumi 		si->tot_segs++;						\
392768afcf2dSTomohiro Kusumi 		if ((type) == SUM_TYPE_DATA) {				\
392839a53e0cSJaegeuk Kim 			si->data_segs++;				\
3929e1235983SChangman Lee 			si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0;	\
3930e1235983SChangman Lee 		} else {						\
393139a53e0cSJaegeuk Kim 			si->node_segs++;				\
3932e1235983SChangman Lee 			si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0;	\
3933e1235983SChangman Lee 		}							\
393439a53e0cSJaegeuk Kim 	} while (0)
393539a53e0cSJaegeuk Kim 
393639a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks)				\
393768afcf2dSTomohiro Kusumi 	((si)->tot_blks += (blks))
393839a53e0cSJaegeuk Kim 
3939e1235983SChangman Lee #define stat_inc_data_blk_count(sbi, blks, gc_type)			\
394039a53e0cSJaegeuk Kim 	do {								\
3941963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
394239a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
394339a53e0cSJaegeuk Kim 		si->data_blks += (blks);				\
394468afcf2dSTomohiro Kusumi 		si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
394539a53e0cSJaegeuk Kim 	} while (0)
394639a53e0cSJaegeuk Kim 
3947e1235983SChangman Lee #define stat_inc_node_blk_count(sbi, blks, gc_type)			\
394839a53e0cSJaegeuk Kim 	do {								\
3949963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
395039a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
395139a53e0cSJaegeuk Kim 		si->node_blks += (blks);				\
395268afcf2dSTomohiro Kusumi 		si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
395339a53e0cSJaegeuk Kim 	} while (0)
395439a53e0cSJaegeuk Kim 
3955cac5a3d8SDongOh Shin int f2fs_build_stats(struct f2fs_sb_info *sbi);
3956cac5a3d8SDongOh Shin void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
395721acc07dSGreg Kroah-Hartman void __init f2fs_create_root_stats(void);
39584589d25dSNamjae Jeon void f2fs_destroy_root_stats(void);
3959fc7100eaSHridya Valsaraju void f2fs_update_sit_info(struct f2fs_sb_info *sbi);
396039a53e0cSJaegeuk Kim #else
3961d66450e7SArnd Bergmann #define stat_inc_cp_count(si)				do { } while (0)
3962d66450e7SArnd Bergmann #define stat_inc_bg_cp_count(si)			do { } while (0)
3963d66450e7SArnd Bergmann #define stat_inc_call_count(si)				do { } while (0)
3964d66450e7SArnd Bergmann #define stat_inc_bggc_count(si)				do { } while (0)
3965274bd9baSChao Yu #define stat_io_skip_bggc_count(sbi)			do { } while (0)
3966274bd9baSChao Yu #define stat_other_skip_bggc_count(sbi)			do { } while (0)
3967d66450e7SArnd Bergmann #define stat_inc_dirty_inode(sbi, type)			do { } while (0)
3968d66450e7SArnd Bergmann #define stat_dec_dirty_inode(sbi, type)			do { } while (0)
3969fc7100eaSHridya Valsaraju #define stat_inc_total_hit(sbi)				do { } while (0)
3970fc7100eaSHridya Valsaraju #define stat_inc_rbtree_node_hit(sbi)			do { } while (0)
3971d66450e7SArnd Bergmann #define stat_inc_largest_node_hit(sbi)			do { } while (0)
3972d66450e7SArnd Bergmann #define stat_inc_cached_node_hit(sbi)			do { } while (0)
3973d66450e7SArnd Bergmann #define stat_inc_inline_xattr(inode)			do { } while (0)
3974d66450e7SArnd Bergmann #define stat_dec_inline_xattr(inode)			do { } while (0)
3975d66450e7SArnd Bergmann #define stat_inc_inline_inode(inode)			do { } while (0)
3976d66450e7SArnd Bergmann #define stat_dec_inline_inode(inode)			do { } while (0)
3977d66450e7SArnd Bergmann #define stat_inc_inline_dir(inode)			do { } while (0)
3978d66450e7SArnd Bergmann #define stat_dec_inline_dir(inode)			do { } while (0)
39794c8ff709SChao Yu #define stat_inc_compr_inode(inode)			do { } while (0)
39804c8ff709SChao Yu #define stat_dec_compr_inode(inode)			do { } while (0)
39814c8ff709SChao Yu #define stat_add_compr_blocks(inode, blocks)		do { } while (0)
39824c8ff709SChao Yu #define stat_sub_compr_blocks(inode, blocks)		do { } while (0)
3983d66450e7SArnd Bergmann #define stat_update_max_atomic_write(inode)		do { } while (0)
3984d66450e7SArnd Bergmann #define stat_inc_volatile_write(inode)			do { } while (0)
3985d66450e7SArnd Bergmann #define stat_dec_volatile_write(inode)			do { } while (0)
3986d66450e7SArnd Bergmann #define stat_update_max_volatile_write(inode)		do { } while (0)
3987b63e7be5SChao Yu #define stat_inc_meta_count(sbi, blkaddr)		do { } while (0)
3988d66450e7SArnd Bergmann #define stat_inc_seg_type(sbi, curseg)			do { } while (0)
3989d66450e7SArnd Bergmann #define stat_inc_block_count(sbi, curseg)		do { } while (0)
3990d66450e7SArnd Bergmann #define stat_inc_inplace_blocks(sbi)			do { } while (0)
3991d66450e7SArnd Bergmann #define stat_inc_seg_count(sbi, type, gc_type)		do { } while (0)
3992d66450e7SArnd Bergmann #define stat_inc_tot_blk_count(si, blks)		do { } while (0)
3993d66450e7SArnd Bergmann #define stat_inc_data_blk_count(sbi, blks, gc_type)	do { } while (0)
3994d66450e7SArnd Bergmann #define stat_inc_node_blk_count(sbi, blks, gc_type)	do { } while (0)
399539a53e0cSJaegeuk Kim 
399639a53e0cSJaegeuk Kim static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
399739a53e0cSJaegeuk Kim static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
399821acc07dSGreg Kroah-Hartman static inline void __init f2fs_create_root_stats(void) { }
39994589d25dSNamjae Jeon static inline void f2fs_destroy_root_stats(void) { }
400048abe91aSYueHaibing static inline void f2fs_update_sit_info(struct f2fs_sb_info *sbi) {}
400139a53e0cSJaegeuk Kim #endif
400239a53e0cSJaegeuk Kim 
400339a53e0cSJaegeuk Kim extern const struct file_operations f2fs_dir_operations;
400439a53e0cSJaegeuk Kim extern const struct file_operations f2fs_file_operations;
400539a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_file_inode_operations;
400639a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_dblock_aops;
400739a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_node_aops;
400839a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_meta_aops;
400939a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_dir_inode_operations;
401039a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_symlink_inode_operations;
4011cbaf042aSJaegeuk Kim extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
401239a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_special_inode_operations;
40134d57b86dSChao Yu extern struct kmem_cache *f2fs_inode_entry_slab;
40141001b347SHuajun Li 
4015e18c65b2SHuajun Li /*
4016e18c65b2SHuajun Li  * inline.c
4017e18c65b2SHuajun Li  */
4018cac5a3d8SDongOh Shin bool f2fs_may_inline_data(struct inode *inode);
4019cac5a3d8SDongOh Shin bool f2fs_may_inline_dentry(struct inode *inode);
40204d57b86dSChao Yu void f2fs_do_read_inline_data(struct page *page, struct page *ipage);
40214d57b86dSChao Yu void f2fs_truncate_inline_inode(struct inode *inode,
40224d57b86dSChao Yu 						struct page *ipage, u64 from);
4023cac5a3d8SDongOh Shin int f2fs_read_inline_data(struct inode *inode, struct page *page);
4024cac5a3d8SDongOh Shin int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
4025cac5a3d8SDongOh Shin int f2fs_convert_inline_inode(struct inode *inode);
4026b06af2afSJaegeuk Kim int f2fs_try_convert_inline_dir(struct inode *dir, struct dentry *dentry);
4027cac5a3d8SDongOh Shin int f2fs_write_inline_data(struct inode *inode, struct page *page);
40289627a7b3SChao Yu int f2fs_recover_inline_data(struct inode *inode, struct page *npage);
40294d57b86dSChao Yu struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
403043c780baSEric Biggers 					const struct f2fs_filename *fname,
403143c780baSEric Biggers 					struct page **res_page);
40324d57b86dSChao Yu int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent,
4033cac5a3d8SDongOh Shin 			struct page *ipage);
403443c780baSEric Biggers int f2fs_add_inline_entry(struct inode *dir, const struct f2fs_filename *fname,
4035cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
40364d57b86dSChao Yu void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry,
40374d57b86dSChao Yu 				struct page *page, struct inode *dir,
40384d57b86dSChao Yu 				struct inode *inode);
4039cac5a3d8SDongOh Shin bool f2fs_empty_inline_dir(struct inode *dir);
4040cac5a3d8SDongOh Shin int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
4041cac5a3d8SDongOh Shin 			struct fscrypt_str *fstr);
4042cac5a3d8SDongOh Shin int f2fs_inline_data_fiemap(struct inode *inode,
4043cac5a3d8SDongOh Shin 			struct fiemap_extent_info *fieinfo,
4044cac5a3d8SDongOh Shin 			__u64 start, __u64 len);
4045cde4de12SJaegeuk Kim 
4046cde4de12SJaegeuk Kim /*
40472658e50dSJaegeuk Kim  * shrinker.c
40482658e50dSJaegeuk Kim  */
4049cac5a3d8SDongOh Shin unsigned long f2fs_shrink_count(struct shrinker *shrink,
4050cac5a3d8SDongOh Shin 			struct shrink_control *sc);
4051cac5a3d8SDongOh Shin unsigned long f2fs_shrink_scan(struct shrinker *shrink,
4052cac5a3d8SDongOh Shin 			struct shrink_control *sc);
4053cac5a3d8SDongOh Shin void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
4054cac5a3d8SDongOh Shin void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
40552658e50dSJaegeuk Kim 
40562658e50dSJaegeuk Kim /*
4057a28ef1f5SChao Yu  * extent_cache.c
4058a28ef1f5SChao Yu  */
40594dada3fdSChao Yu struct rb_entry *f2fs_lookup_rb_tree(struct rb_root_cached *root,
4060004b6862SChao Yu 				struct rb_entry *cached_re, unsigned int ofs);
40612e9b2bb2SChao Yu struct rb_node **f2fs_lookup_rb_tree_ext(struct f2fs_sb_info *sbi,
40622e9b2bb2SChao Yu 				struct rb_root_cached *root,
40632e9b2bb2SChao Yu 				struct rb_node **parent,
40642e9b2bb2SChao Yu 				unsigned long long key, bool *left_most);
40654d57b86dSChao Yu struct rb_node **f2fs_lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
40664dada3fdSChao Yu 				struct rb_root_cached *root,
40674dada3fdSChao Yu 				struct rb_node **parent,
40684dada3fdSChao Yu 				unsigned int ofs, bool *leftmost);
40694dada3fdSChao Yu struct rb_entry *f2fs_lookup_rb_tree_ret(struct rb_root_cached *root,
4070004b6862SChao Yu 		struct rb_entry *cached_re, unsigned int ofs,
4071004b6862SChao Yu 		struct rb_entry **prev_entry, struct rb_entry **next_entry,
4072004b6862SChao Yu 		struct rb_node ***insert_p, struct rb_node **insert_parent,
40734dada3fdSChao Yu 		bool force, bool *leftmost);
40744d57b86dSChao Yu bool f2fs_check_rb_tree_consistence(struct f2fs_sb_info *sbi,
40752e9b2bb2SChao Yu 				struct rb_root_cached *root, bool check_key);
4076cac5a3d8SDongOh Shin unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
4077a6d601f3SChao Yu void f2fs_init_extent_tree(struct inode *inode, struct page *ipage);
4078cac5a3d8SDongOh Shin void f2fs_drop_extent_tree(struct inode *inode);
4079cac5a3d8SDongOh Shin unsigned int f2fs_destroy_extent_node(struct inode *inode);
4080cac5a3d8SDongOh Shin void f2fs_destroy_extent_tree(struct inode *inode);
4081cac5a3d8SDongOh Shin bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
4082cac5a3d8SDongOh Shin 			struct extent_info *ei);
4083cac5a3d8SDongOh Shin void f2fs_update_extent_cache(struct dnode_of_data *dn);
408419b2c30dSChao Yu void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
4085cac5a3d8SDongOh Shin 			pgoff_t fofs, block_t blkaddr, unsigned int len);
40864d57b86dSChao Yu void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi);
40874d57b86dSChao Yu int __init f2fs_create_extent_cache(void);
40884d57b86dSChao Yu void f2fs_destroy_extent_cache(void);
4089a28ef1f5SChao Yu 
4090a28ef1f5SChao Yu /*
40918ceffcb2SChao Yu  * sysfs.c
40928ceffcb2SChao Yu  */
40930f6b56ecSDaeho Jeong #define MIN_RA_MUL	2
40940f6b56ecSDaeho Jeong #define MAX_RA_MUL	256
40950f6b56ecSDaeho Jeong 
4096dc6b2055SJaegeuk Kim int __init f2fs_init_sysfs(void);
4097dc6b2055SJaegeuk Kim void f2fs_exit_sysfs(void);
4098dc6b2055SJaegeuk Kim int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
4099dc6b2055SJaegeuk Kim void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
41008ceffcb2SChao Yu 
410195ae251fSEric Biggers /* verity.c */
410295ae251fSEric Biggers extern const struct fsverity_operations f2fs_verityops;
410395ae251fSEric Biggers 
41048ceffcb2SChao Yu /*
4105cde4de12SJaegeuk Kim  * crypto support
4106cde4de12SJaegeuk Kim  */
41071958593eSJaegeuk Kim static inline bool f2fs_encrypted_file(struct inode *inode)
41081958593eSJaegeuk Kim {
410962230e0dSChandan Rajendra 	return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
41101958593eSJaegeuk Kim }
41111958593eSJaegeuk Kim 
4112cde4de12SJaegeuk Kim static inline void f2fs_set_encrypted_inode(struct inode *inode)
4113cde4de12SJaegeuk Kim {
4114643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
4115cde4de12SJaegeuk Kim 	file_set_encrypt(inode);
41169149a5ebSChao Yu 	f2fs_set_inode_flags(inode);
4117cde4de12SJaegeuk Kim #endif
4118cde4de12SJaegeuk Kim }
4119cde4de12SJaegeuk Kim 
41206dbb1796SEric Biggers /*
41216dbb1796SEric Biggers  * Returns true if the reads of the inode's data need to undergo some
41226dbb1796SEric Biggers  * postprocessing step, like decryption or authenticity verification.
41236dbb1796SEric Biggers  */
41246dbb1796SEric Biggers static inline bool f2fs_post_read_required(struct inode *inode)
4125cde4de12SJaegeuk Kim {
41264c8ff709SChao Yu 	return f2fs_encrypted_file(inode) || fsverity_active(inode) ||
41274c8ff709SChao Yu 		f2fs_compressed_file(inode);
41284c8ff709SChao Yu }
41294c8ff709SChao Yu 
41304c8ff709SChao Yu /*
41314c8ff709SChao Yu  * compress.c
41324c8ff709SChao Yu  */
41334c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
41344c8ff709SChao Yu bool f2fs_is_compressed_page(struct page *page);
41354c8ff709SChao Yu struct page *f2fs_compress_control_page(struct page *page);
41364c8ff709SChao Yu int f2fs_prepare_compress_overwrite(struct inode *inode,
41374c8ff709SChao Yu 			struct page **pagep, pgoff_t index, void **fsdata);
41384c8ff709SChao Yu bool f2fs_compress_write_end(struct inode *inode, void *fsdata,
41394c8ff709SChao Yu 					pgoff_t index, unsigned copied);
41403265d3dbSChao Yu int f2fs_truncate_partial_cluster(struct inode *inode, u64 from, bool lock);
41414c8ff709SChao Yu void f2fs_compress_write_end_io(struct bio *bio, struct page *page);
41424c8ff709SChao Yu bool f2fs_is_compress_backend_ready(struct inode *inode);
41435e6bbde9SChao Yu int f2fs_init_compress_mempool(void);
41445e6bbde9SChao Yu void f2fs_destroy_compress_mempool(void);
41456ce19affSChao Yu void f2fs_decompress_cluster(struct decompress_io_ctx *dic);
41466ce19affSChao Yu void f2fs_end_read_compressed_page(struct page *page, bool failed,
41476ce19affSChao Yu 							block_t blkaddr);
41484c8ff709SChao Yu bool f2fs_cluster_is_empty(struct compress_ctx *cc);
41494c8ff709SChao Yu bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index);
4150b368cc5eSFengnan Chang bool f2fs_all_cluster_page_loaded(struct compress_ctx *cc, struct pagevec *pvec,
4151b368cc5eSFengnan Chang 				int index, int nr_pages);
4152bbe1da7eSChao Yu bool f2fs_sanity_check_cluster(struct dnode_of_data *dn);
41534c8ff709SChao Yu void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct page *page);
41544c8ff709SChao Yu int f2fs_write_multi_pages(struct compress_ctx *cc,
41554c8ff709SChao Yu 						int *submitted,
41564c8ff709SChao Yu 						struct writeback_control *wbc,
41574c8ff709SChao Yu 						enum iostat_type io_type);
41584c8ff709SChao Yu int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index);
415994afd6d6SChao Yu void f2fs_update_extent_tree_range_compressed(struct inode *inode,
416094afd6d6SChao Yu 				pgoff_t fofs, block_t blkaddr, unsigned int llen,
416194afd6d6SChao Yu 				unsigned int c_len);
41624c8ff709SChao Yu int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
41634c8ff709SChao Yu 				unsigned nr_pages, sector_t *last_block_in_bio,
41640683728aSChao Yu 				bool is_readahead, bool for_write);
41654c8ff709SChao Yu struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc);
41667f59b277SEric Biggers void f2fs_decompress_end_io(struct decompress_io_ctx *dic, bool failed);
41677f59b277SEric Biggers void f2fs_put_page_dic(struct page *page);
416894afd6d6SChao Yu unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn);
41694c8ff709SChao Yu int f2fs_init_compress_ctx(struct compress_ctx *cc);
41708bfbfb0dSChao Yu void f2fs_destroy_compress_ctx(struct compress_ctx *cc, bool reuse);
41714c8ff709SChao Yu void f2fs_init_compress_info(struct f2fs_sb_info *sbi);
41726ce19affSChao Yu int f2fs_init_compress_inode(struct f2fs_sb_info *sbi);
41736ce19affSChao Yu void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi);
417431083031SChao Yu int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi);
417531083031SChao Yu void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi);
4176c68d6c88SChao Yu int __init f2fs_init_compress_cache(void);
4177c68d6c88SChao Yu void f2fs_destroy_compress_cache(void);
41786ce19affSChao Yu struct address_space *COMPRESS_MAPPING(struct f2fs_sb_info *sbi);
41796ce19affSChao Yu void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi, block_t blkaddr);
41806ce19affSChao Yu void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
41816ce19affSChao Yu 						nid_t ino, block_t blkaddr);
41826ce19affSChao Yu bool f2fs_load_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
41836ce19affSChao Yu 								block_t blkaddr);
41846ce19affSChao Yu void f2fs_invalidate_compress_pages(struct f2fs_sb_info *sbi, nid_t ino);
41855ac443e2SDaeho Jeong #define inc_compr_inode_stat(inode)					\
41865ac443e2SDaeho Jeong 	do {								\
41875ac443e2SDaeho Jeong 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);		\
41885ac443e2SDaeho Jeong 		sbi->compr_new_inode++;					\
41895ac443e2SDaeho Jeong 	} while (0)
41905ac443e2SDaeho Jeong #define add_compr_block_stat(inode, blocks)				\
41915ac443e2SDaeho Jeong 	do {								\
41925ac443e2SDaeho Jeong 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);		\
41935ac443e2SDaeho Jeong 		int diff = F2FS_I(inode)->i_cluster_size - blocks;	\
41945ac443e2SDaeho Jeong 		sbi->compr_written_block += blocks;			\
41955ac443e2SDaeho Jeong 		sbi->compr_saved_block += diff;				\
41965ac443e2SDaeho Jeong 	} while (0)
41974c8ff709SChao Yu #else
41984c8ff709SChao Yu static inline bool f2fs_is_compressed_page(struct page *page) { return false; }
41994c8ff709SChao Yu static inline bool f2fs_is_compress_backend_ready(struct inode *inode)
42004c8ff709SChao Yu {
42014c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
42024c8ff709SChao Yu 		return true;
42034c8ff709SChao Yu 	/* not support compression */
42044c8ff709SChao Yu 	return false;
42054c8ff709SChao Yu }
42064c8ff709SChao Yu static inline struct page *f2fs_compress_control_page(struct page *page)
42074c8ff709SChao Yu {
42084c8ff709SChao Yu 	WARN_ON_ONCE(1);
42094c8ff709SChao Yu 	return ERR_PTR(-EINVAL);
42104c8ff709SChao Yu }
42115e6bbde9SChao Yu static inline int f2fs_init_compress_mempool(void) { return 0; }
42125e6bbde9SChao Yu static inline void f2fs_destroy_compress_mempool(void) { }
42136ce19affSChao Yu static inline void f2fs_decompress_cluster(struct decompress_io_ctx *dic) { }
42146ce19affSChao Yu static inline void f2fs_end_read_compressed_page(struct page *page,
42156ce19affSChao Yu 						bool failed, block_t blkaddr)
42167f59b277SEric Biggers {
42177f59b277SEric Biggers 	WARN_ON_ONCE(1);
42187f59b277SEric Biggers }
42197f59b277SEric Biggers static inline void f2fs_put_page_dic(struct page *page)
42207f59b277SEric Biggers {
42217f59b277SEric Biggers 	WARN_ON_ONCE(1);
42227f59b277SEric Biggers }
422394afd6d6SChao Yu static inline unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn) { return 0; }
4224bbe1da7eSChao Yu static inline bool f2fs_sanity_check_cluster(struct dnode_of_data *dn) { return false; }
42256ce19affSChao Yu static inline int f2fs_init_compress_inode(struct f2fs_sb_info *sbi) { return 0; }
42266ce19affSChao Yu static inline void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi) { }
422731083031SChao Yu static inline int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi) { return 0; }
422831083031SChao Yu static inline void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi) { }
4229c68d6c88SChao Yu static inline int __init f2fs_init_compress_cache(void) { return 0; }
4230c68d6c88SChao Yu static inline void f2fs_destroy_compress_cache(void) { }
42316ce19affSChao Yu static inline void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi,
42326ce19affSChao Yu 				block_t blkaddr) { }
42336ce19affSChao Yu static inline void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi,
42346ce19affSChao Yu 				struct page *page, nid_t ino, block_t blkaddr) { }
42356ce19affSChao Yu static inline bool f2fs_load_compressed_page(struct f2fs_sb_info *sbi,
42366ce19affSChao Yu 				struct page *page, block_t blkaddr) { return false; }
42376ce19affSChao Yu static inline void f2fs_invalidate_compress_pages(struct f2fs_sb_info *sbi,
42386ce19affSChao Yu 							nid_t ino) { }
42395ac443e2SDaeho Jeong #define inc_compr_inode_stat(inode)		do { } while (0)
424094afd6d6SChao Yu static inline void f2fs_update_extent_tree_range_compressed(struct inode *inode,
424194afd6d6SChao Yu 				pgoff_t fofs, block_t blkaddr, unsigned int llen,
424294afd6d6SChao Yu 				unsigned int c_len) { }
42434c8ff709SChao Yu #endif
42444c8ff709SChao Yu 
42454c8ff709SChao Yu static inline void set_compress_context(struct inode *inode)
42464c8ff709SChao Yu {
42474c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
42484c8ff709SChao Yu 
42494c8ff709SChao Yu 	F2FS_I(inode)->i_compress_algorithm =
42504c8ff709SChao Yu 			F2FS_OPTION(sbi).compress_algorithm;
42514c8ff709SChao Yu 	F2FS_I(inode)->i_log_cluster_size =
42524c8ff709SChao Yu 			F2FS_OPTION(sbi).compress_log_size;
4253b28f047bSChao Yu 	F2FS_I(inode)->i_compress_flag =
4254b28f047bSChao Yu 			F2FS_OPTION(sbi).compress_chksum ?
4255b28f047bSChao Yu 				1 << COMPRESS_CHKSUM : 0;
42564c8ff709SChao Yu 	F2FS_I(inode)->i_cluster_size =
42574c8ff709SChao Yu 			1 << F2FS_I(inode)->i_log_cluster_size;
425801f6afd0SChao Yu 	if ((F2FS_I(inode)->i_compress_algorithm == COMPRESS_LZ4 ||
425901f6afd0SChao Yu 		F2FS_I(inode)->i_compress_algorithm == COMPRESS_ZSTD) &&
42603fde13f8SChao Yu 			F2FS_OPTION(sbi).compress_level)
42613fde13f8SChao Yu 		F2FS_I(inode)->i_compress_flag |=
42623fde13f8SChao Yu 				F2FS_OPTION(sbi).compress_level <<
42633fde13f8SChao Yu 				COMPRESS_LEVEL_OFFSET;
42644c8ff709SChao Yu 	F2FS_I(inode)->i_flags |= F2FS_COMPR_FL;
42654c8ff709SChao Yu 	set_inode_flag(inode, FI_COMPRESSED_FILE);
42664c8ff709SChao Yu 	stat_inc_compr_inode(inode);
42675ac443e2SDaeho Jeong 	inc_compr_inode_stat(inode);
4268530e0704SChao Yu 	f2fs_mark_inode_dirty_sync(inode, true);
42694c8ff709SChao Yu }
42704c8ff709SChao Yu 
427178134d03SDaeho Jeong static inline bool f2fs_disable_compressed_file(struct inode *inode)
42724c8ff709SChao Yu {
42734c8ff709SChao Yu 	struct f2fs_inode_info *fi = F2FS_I(inode);
42744c8ff709SChao Yu 
42754c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
427678134d03SDaeho Jeong 		return true;
427702d58cd2SHyeong-Jun Kim 	if (S_ISREG(inode->i_mode) && F2FS_HAS_BLOCKS(inode))
427878134d03SDaeho Jeong 		return false;
42794c8ff709SChao Yu 
42804c8ff709SChao Yu 	fi->i_flags &= ~F2FS_COMPR_FL;
42814c8ff709SChao Yu 	stat_dec_compr_inode(inode);
428296f5b4faSChao Yu 	clear_inode_flag(inode, FI_COMPRESSED_FILE);
4283530e0704SChao Yu 	f2fs_mark_inode_dirty_sync(inode, true);
428478134d03SDaeho Jeong 	return true;
4285cde4de12SJaegeuk Kim }
4286cde4de12SJaegeuk Kim 
4287ccd31cb2SSheng Yong #define F2FS_FEATURE_FUNCS(name, flagname) \
42887beb01f7SChao Yu static inline int f2fs_sb_has_##name(struct f2fs_sb_info *sbi) \
4289ccd31cb2SSheng Yong { \
42907beb01f7SChao Yu 	return F2FS_HAS_FEATURE(sbi, F2FS_FEATURE_##flagname); \
4291cde4de12SJaegeuk Kim }
4292f424f664SJaegeuk Kim 
4293ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(encrypt, ENCRYPT);
4294ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(blkzoned, BLKZONED);
4295ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(extra_attr, EXTRA_ATTR);
4296ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(project_quota, PRJQUOTA);
4297ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_chksum, INODE_CHKSUM);
4298ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
4299ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
4300ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
4301b7c409deSSheng Yong F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
430295ae251fSEric Biggers F2FS_FEATURE_FUNCS(verity, VERITY);
4303d440c52dSJunling Zheng F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
43045aba5430SDaniel Rosenberg F2FS_FEATURE_FUNCS(casefold, CASEFOLD);
43054c8ff709SChao Yu F2FS_FEATURE_FUNCS(compression, COMPRESSION);
4306a7d9fe3cSJaegeuk Kim F2FS_FEATURE_FUNCS(readonly, RO);
43071c1d35dfSChao Yu 
43084215d054SDaeho Jeong static inline bool f2fs_may_extent_tree(struct inode *inode)
43094215d054SDaeho Jeong {
43104215d054SDaeho Jeong 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
43114215d054SDaeho Jeong 
43124215d054SDaeho Jeong 	if (!test_opt(sbi, EXTENT_CACHE) ||
43134215d054SDaeho Jeong 			is_inode_flag_set(inode, FI_NO_EXTENT) ||
43144215d054SDaeho Jeong 			(is_inode_flag_set(inode, FI_COMPRESSED_FILE) &&
43154215d054SDaeho Jeong 			 !f2fs_sb_has_readonly(sbi)))
43164215d054SDaeho Jeong 		return false;
43174215d054SDaeho Jeong 
43184215d054SDaeho Jeong 	/*
43194215d054SDaeho Jeong 	 * for recovered files during mount do not create extents
43204215d054SDaeho Jeong 	 * if shrinker is not registered.
43214215d054SDaeho Jeong 	 */
43224215d054SDaeho Jeong 	if (list_empty(&sbi->s_list))
43234215d054SDaeho Jeong 		return false;
43244215d054SDaeho Jeong 
43254215d054SDaeho Jeong 	return S_ISREG(inode->i_mode);
43264215d054SDaeho Jeong }
43274215d054SDaeho Jeong 
4328178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
432995175dafSDamien Le Moal static inline bool f2fs_blkz_is_seq(struct f2fs_sb_info *sbi, int devi,
433095175dafSDamien Le Moal 				    block_t blkaddr)
4331178053e2SDamien Le Moal {
4332178053e2SDamien Le Moal 	unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
4333178053e2SDamien Le Moal 
433495175dafSDamien Le Moal 	return test_bit(zno, FDEV(devi).blkz_seq);
4335178053e2SDamien Le Moal }
4336178053e2SDamien Le Moal #endif
4337178053e2SDamien Le Moal 
43387d20c8abSChao Yu static inline bool f2fs_hw_should_discard(struct f2fs_sb_info *sbi)
433996ba2decSDamien Le Moal {
43407beb01f7SChao Yu 	return f2fs_sb_has_blkzoned(sbi);
43417d20c8abSChao Yu }
434296ba2decSDamien Le Moal 
43437f3d7719SDamien Le Moal static inline bool f2fs_bdev_support_discard(struct block_device *bdev)
43447f3d7719SDamien Le Moal {
43457f3d7719SDamien Le Moal 	return blk_queue_discard(bdev_get_queue(bdev)) ||
43467f3d7719SDamien Le Moal 	       bdev_is_zoned(bdev);
43477f3d7719SDamien Le Moal }
43487f3d7719SDamien Le Moal 
43497d20c8abSChao Yu static inline bool f2fs_hw_support_discard(struct f2fs_sb_info *sbi)
43507d20c8abSChao Yu {
43517f3d7719SDamien Le Moal 	int i;
43527f3d7719SDamien Le Moal 
43537f3d7719SDamien Le Moal 	if (!f2fs_is_multi_device(sbi))
43547f3d7719SDamien Le Moal 		return f2fs_bdev_support_discard(sbi->sb->s_bdev);
43557f3d7719SDamien Le Moal 
43567f3d7719SDamien Le Moal 	for (i = 0; i < sbi->s_ndevs; i++)
43577f3d7719SDamien Le Moal 		if (f2fs_bdev_support_discard(FDEV(i).bdev))
43587f3d7719SDamien Le Moal 			return true;
43597f3d7719SDamien Le Moal 	return false;
43607d20c8abSChao Yu }
43617d20c8abSChao Yu 
43627d20c8abSChao Yu static inline bool f2fs_realtime_discard_enable(struct f2fs_sb_info *sbi)
43637d20c8abSChao Yu {
43647d20c8abSChao Yu 	return (test_opt(sbi, DISCARD) && f2fs_hw_support_discard(sbi)) ||
43657d20c8abSChao Yu 					f2fs_hw_should_discard(sbi);
436652763a4bSJaegeuk Kim }
436752763a4bSJaegeuk Kim 
4368f824deb5SChao Yu static inline bool f2fs_hw_is_readonly(struct f2fs_sb_info *sbi)
4369f824deb5SChao Yu {
4370f824deb5SChao Yu 	int i;
4371f824deb5SChao Yu 
4372f824deb5SChao Yu 	if (!f2fs_is_multi_device(sbi))
4373f824deb5SChao Yu 		return bdev_read_only(sbi->sb->s_bdev);
4374f824deb5SChao Yu 
4375f824deb5SChao Yu 	for (i = 0; i < sbi->s_ndevs; i++)
4376f824deb5SChao Yu 		if (bdev_read_only(FDEV(i).bdev))
4377f824deb5SChao Yu 			return true;
4378f824deb5SChao Yu 	return false;
4379f824deb5SChao Yu }
4380f824deb5SChao Yu 
4381b0332a0fSChao Yu static inline bool f2fs_lfs_mode(struct f2fs_sb_info *sbi)
438252763a4bSJaegeuk Kim {
4383b0332a0fSChao Yu 	return F2FS_OPTION(sbi).fs_mode == FS_MODE_LFS;
438452763a4bSJaegeuk Kim }
438552763a4bSJaegeuk Kim 
43864c8ff709SChao Yu static inline bool f2fs_may_compress(struct inode *inode)
43874c8ff709SChao Yu {
43884c8ff709SChao Yu 	if (IS_SWAPFILE(inode) || f2fs_is_pinned_file(inode) ||
43894c8ff709SChao Yu 				f2fs_is_atomic_file(inode) ||
43904c8ff709SChao Yu 				f2fs_is_volatile_file(inode))
43914c8ff709SChao Yu 		return false;
43924c8ff709SChao Yu 	return S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode);
43934c8ff709SChao Yu }
43944c8ff709SChao Yu 
43954c8ff709SChao Yu static inline void f2fs_i_compr_blocks_update(struct inode *inode,
43964c8ff709SChao Yu 						u64 blocks, bool add)
43974c8ff709SChao Yu {
43984c8ff709SChao Yu 	int diff = F2FS_I(inode)->i_cluster_size - blocks;
4399c2759ebaSDaeho Jeong 	struct f2fs_inode_info *fi = F2FS_I(inode);
44004c8ff709SChao Yu 
4401ef8d563fSChao Yu 	/* don't update i_compr_blocks if saved blocks were released */
4402c2759ebaSDaeho Jeong 	if (!add && !atomic_read(&fi->i_compr_blocks))
4403ef8d563fSChao Yu 		return;
4404ef8d563fSChao Yu 
44054c8ff709SChao Yu 	if (add) {
4406c2759ebaSDaeho Jeong 		atomic_add(diff, &fi->i_compr_blocks);
44074c8ff709SChao Yu 		stat_add_compr_blocks(inode, diff);
44084c8ff709SChao Yu 	} else {
4409c2759ebaSDaeho Jeong 		atomic_sub(diff, &fi->i_compr_blocks);
44104c8ff709SChao Yu 		stat_sub_compr_blocks(inode, diff);
44114c8ff709SChao Yu 	}
44124c8ff709SChao Yu 	f2fs_mark_inode_dirty_sync(inode, true);
44134c8ff709SChao Yu }
44144c8ff709SChao Yu 
4415f847c699SChao Yu static inline int block_unaligned_IO(struct inode *inode,
4416f847c699SChao Yu 				struct kiocb *iocb, struct iov_iter *iter)
4417b91050a8SHyunchul Lee {
4418f847c699SChao Yu 	unsigned int i_blkbits = READ_ONCE(inode->i_blkbits);
4419f847c699SChao Yu 	unsigned int blocksize_mask = (1 << i_blkbits) - 1;
4420f847c699SChao Yu 	loff_t offset = iocb->ki_pos;
4421f847c699SChao Yu 	unsigned long align = offset | iov_iter_alignment(iter);
4422f847c699SChao Yu 
4423f847c699SChao Yu 	return align & blocksize_mask;
4424f847c699SChao Yu }
4425f847c699SChao Yu 
442671f2c820SChao Yu static inline bool f2fs_allow_multi_device_dio(struct f2fs_sb_info *sbi,
442771f2c820SChao Yu 								int flag)
442871f2c820SChao Yu {
442971f2c820SChao Yu 	if (!f2fs_is_multi_device(sbi))
443071f2c820SChao Yu 		return false;
443171f2c820SChao Yu 	if (flag != F2FS_GET_BLOCK_DIO)
443271f2c820SChao Yu 		return false;
443371f2c820SChao Yu 	return sbi->aligned_blksize;
443471f2c820SChao Yu }
443571f2c820SChao Yu 
4436f847c699SChao Yu static inline bool f2fs_force_buffered_io(struct inode *inode,
4437f847c699SChao Yu 				struct kiocb *iocb, struct iov_iter *iter)
4438f847c699SChao Yu {
4439f847c699SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4440f847c699SChao Yu 	int rw = iov_iter_rw(iter);
4441f847c699SChao Yu 
4442f847c699SChao Yu 	if (f2fs_post_read_required(inode))
4443f847c699SChao Yu 		return true;
444471f2c820SChao Yu 
444571f2c820SChao Yu 	/* disallow direct IO if any of devices has unaligned blksize */
444671f2c820SChao Yu 	if (f2fs_is_multi_device(sbi) && !sbi->aligned_blksize)
4447f847c699SChao Yu 		return true;
4448f847c699SChao Yu 	/*
4449f847c699SChao Yu 	 * for blkzoned device, fallback direct IO to buffered IO, so
4450f847c699SChao Yu 	 * all IOs can be serialized by log-structured write.
4451f847c699SChao Yu 	 */
44527beb01f7SChao Yu 	if (f2fs_sb_has_blkzoned(sbi))
4453f847c699SChao Yu 		return true;
4454b0332a0fSChao Yu 	if (f2fs_lfs_mode(sbi) && (rw == WRITE)) {
44559720ee80SChao Yu 		if (block_unaligned_IO(inode, iocb, iter))
4456f847c699SChao Yu 			return true;
44579720ee80SChao Yu 		if (F2FS_IO_ALIGNED(sbi))
44589720ee80SChao Yu 			return true;
44599720ee80SChao Yu 	}
4460ebc29b62Shuangjianan@oppo.com 	if (is_sbi_flag_set(F2FS_I_SB(inode), SBI_CP_DISABLED))
44614354994fSDaniel Rosenberg 		return true;
44624354994fSDaniel Rosenberg 
4463f847c699SChao Yu 	return false;
4464b91050a8SHyunchul Lee }
4465b91050a8SHyunchul Lee 
44667f59b277SEric Biggers static inline bool f2fs_need_verity(const struct inode *inode, pgoff_t idx)
44677f59b277SEric Biggers {
44687f59b277SEric Biggers 	return fsverity_active(inode) &&
44697f59b277SEric Biggers 	       idx < DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
44707f59b277SEric Biggers }
44717f59b277SEric Biggers 
447283a3bfdbSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
4473d494500aSChao Yu extern void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate,
4474d494500aSChao Yu 							unsigned int type);
447583a3bfdbSJaegeuk Kim #else
4476d494500aSChao Yu #define f2fs_build_fault_attr(sbi, rate, type)		do { } while (0)
447783a3bfdbSJaegeuk Kim #endif
447883a3bfdbSJaegeuk Kim 
4479af033b2aSChao Yu static inline bool is_journalled_quota(struct f2fs_sb_info *sbi)
4480af033b2aSChao Yu {
4481af033b2aSChao Yu #ifdef CONFIG_QUOTA
44827beb01f7SChao Yu 	if (f2fs_sb_has_quota_ino(sbi))
4483af033b2aSChao Yu 		return true;
4484af033b2aSChao Yu 	if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
4485af033b2aSChao Yu 		F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
4486af033b2aSChao Yu 		F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
4487af033b2aSChao Yu 		return true;
4488af033b2aSChao Yu #endif
4489af033b2aSChao Yu 	return false;
4490af033b2aSChao Yu }
44910af725fcSJaegeuk Kim 
44924f993264SChao Yu static inline bool f2fs_block_unit_discard(struct f2fs_sb_info *sbi)
44934f993264SChao Yu {
44944f993264SChao Yu 	return F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_BLOCK;
44954f993264SChao Yu }
44964f993264SChao Yu 
449710f966bbSChao Yu #define EFSBADCRC	EBADMSG		/* Bad CRC detected */
449810f966bbSChao Yu #define EFSCORRUPTED	EUCLEAN		/* Filesystem is corrupted */
449910f966bbSChao Yu 
4500e1074d4bSChao Yu #endif /* _LINUX_F2FS_H */
4501