xref: /openbmc/linux/fs/f2fs/f2fs.h (revision d98af5f4552058a5c22030641ef79cee92c61f54)
1d29fbcdbSNishad Kamdar /* SPDX-License-Identifier: GPL-2.0 */
20a8165d7SJaegeuk Kim /*
339a53e0cSJaegeuk Kim  * fs/f2fs/f2fs.h
439a53e0cSJaegeuk Kim  *
539a53e0cSJaegeuk Kim  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
639a53e0cSJaegeuk Kim  *             http://www.samsung.com/
739a53e0cSJaegeuk Kim  */
839a53e0cSJaegeuk Kim #ifndef _LINUX_F2FS_H
939a53e0cSJaegeuk Kim #define _LINUX_F2FS_H
1039a53e0cSJaegeuk Kim 
11f847c699SChao Yu #include <linux/uio.h>
1239a53e0cSJaegeuk Kim #include <linux/types.h>
1339a53e0cSJaegeuk Kim #include <linux/page-flags.h>
1439a53e0cSJaegeuk Kim #include <linux/buffer_head.h>
1539a53e0cSJaegeuk Kim #include <linux/slab.h>
1639a53e0cSJaegeuk Kim #include <linux/crc32.h>
1739a53e0cSJaegeuk Kim #include <linux/magic.h>
18c2d715d1SJaegeuk Kim #include <linux/kobject.h>
197bd59381SGu Zheng #include <linux/sched.h>
207c2e5963SJaegeuk Kim #include <linux/cred.h>
2139307a8eSJaegeuk Kim #include <linux/vmalloc.h>
22740432f8SJaegeuk Kim #include <linux/bio.h>
23d0239e1bSJaegeuk Kim #include <linux/blkdev.h>
240abd675eSChao Yu #include <linux/quotaops.h>
25c6a564ffSChristoph Hellwig #include <linux/part_stat.h>
2643b6573bSKeith Mok #include <crypto/hash.h>
2739a53e0cSJaegeuk Kim 
28734f0d24SDave Chinner #include <linux/fscrypt.h>
2995ae251fSEric Biggers #include <linux/fsverity.h>
30734f0d24SDave Chinner 
3151dcbdacSMatthew Wilcox (Oracle) struct pagevec;
3251dcbdacSMatthew Wilcox (Oracle) 
335d56b671SJaegeuk Kim #ifdef CONFIG_F2FS_CHECK_FS
349850cf4aSJaegeuk Kim #define f2fs_bug_on(sbi, condition)	BUG_ON(condition)
355d56b671SJaegeuk Kim #else
369850cf4aSJaegeuk Kim #define f2fs_bug_on(sbi, condition)					\
379850cf4aSJaegeuk Kim 	do {								\
38db489652SYangtao Li 		if (WARN_ON(condition))					\
39caf0047eSChao Yu 			set_sbi_flag(sbi, SBI_NEED_FSCK);		\
409850cf4aSJaegeuk Kim 	} while (0)
415d56b671SJaegeuk Kim #endif
425d56b671SJaegeuk Kim 
432c63feadSJaegeuk Kim enum {
442c63feadSJaegeuk Kim 	FAULT_KMALLOC,
45628b3d14SChao Yu 	FAULT_KVMALLOC,
46c41f3cc3SJaegeuk Kim 	FAULT_PAGE_ALLOC,
4701eccef7SChao Yu 	FAULT_PAGE_GET,
48b96d9b3bSChao Yu 	FAULT_ALLOC_BIO,	/* it's obsolete due to bio_alloc() will never fail */
49cb78942bSJaegeuk Kim 	FAULT_ALLOC_NID,
50cb78942bSJaegeuk Kim 	FAULT_ORPHAN,
51cb78942bSJaegeuk Kim 	FAULT_BLOCK,
52cb78942bSJaegeuk Kim 	FAULT_DIR_DEPTH,
5353aa6bbfSJaegeuk Kim 	FAULT_EVICT_INODE,
5414b44d23SJaegeuk Kim 	FAULT_TRUNCATE,
556f5c2ed0SChao Yu 	FAULT_READ_IO,
560f348028SChao Yu 	FAULT_CHECKPOINT,
57b83dcfe6SChao Yu 	FAULT_DISCARD,
586f5c2ed0SChao Yu 	FAULT_WRITE_IO,
5932410577SChao Yu 	FAULT_SLAB_ALLOC,
6010a26878SChao Yu 	FAULT_DQUOT_INIT,
613e020389SChao Yu 	FAULT_LOCK_OP,
622c63feadSJaegeuk Kim 	FAULT_MAX,
632c63feadSJaegeuk Kim };
642c63feadSJaegeuk Kim 
657fa750a1SArnd Bergmann #ifdef CONFIG_F2FS_FAULT_INJECTION
66d494500aSChao Yu #define F2FS_ALL_FAULT_TYPE		((1 << FAULT_MAX) - 1)
67d494500aSChao Yu 
6808796897SSheng Yong struct f2fs_fault_info {
6908796897SSheng Yong 	atomic_t inject_ops;
7008796897SSheng Yong 	unsigned int inject_rate;
7108796897SSheng Yong 	unsigned int inject_type;
7208796897SSheng Yong };
7308796897SSheng Yong 
7419880e6eSAlexey Dobriyan extern const char *f2fs_fault_name[FAULT_MAX];
7568afcf2dSTomohiro Kusumi #define IS_FAULT_SET(fi, type) ((fi)->inject_type & (1 << (type)))
762c63feadSJaegeuk Kim #endif
772c63feadSJaegeuk Kim 
7839a53e0cSJaegeuk Kim /*
7939a53e0cSJaegeuk Kim  * For mount options
8039a53e0cSJaegeuk Kim  */
8139a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_ROLL_FORWARD	0x00000002
8239a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISCARD		0x00000004
8339a53e0cSJaegeuk Kim #define F2FS_MOUNT_NOHEAP		0x00000008
8439a53e0cSJaegeuk Kim #define F2FS_MOUNT_XATTR_USER		0x00000010
8539a53e0cSJaegeuk Kim #define F2FS_MOUNT_POSIX_ACL		0x00000020
8639a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_EXT_IDENTIFY	0x00000040
87444c580fSJaegeuk Kim #define F2FS_MOUNT_INLINE_XATTR		0x00000080
881001b347SHuajun Li #define F2FS_MOUNT_INLINE_DATA		0x00000100
8934d67debSChao Yu #define F2FS_MOUNT_INLINE_DENTRY	0x00000200
9034d67debSChao Yu #define F2FS_MOUNT_FLUSH_MERGE		0x00000400
9134d67debSChao Yu #define F2FS_MOUNT_NOBARRIER		0x00000800
92d5053a34SJaegeuk Kim #define F2FS_MOUNT_FASTBOOT		0x00001000
9389672159SChao Yu #define F2FS_MOUNT_EXTENT_CACHE		0x00002000
94343f40f0SChao Yu #define F2FS_MOUNT_DATA_FLUSH		0x00008000
9573faec4dSJaegeuk Kim #define F2FS_MOUNT_FAULT_INJECTION	0x00010000
960abd675eSChao Yu #define F2FS_MOUNT_USRQUOTA		0x00080000
970abd675eSChao Yu #define F2FS_MOUNT_GRPQUOTA		0x00100000
985c57132eSChao Yu #define F2FS_MOUNT_PRJQUOTA		0x00200000
994b2414d0SChao Yu #define F2FS_MOUNT_QUOTA		0x00400000
1006afc662eSChao Yu #define F2FS_MOUNT_INLINE_XATTR_SIZE	0x00800000
1017e65be49SJaegeuk Kim #define F2FS_MOUNT_RESERVE_ROOT		0x01000000
1024354994fSDaniel Rosenberg #define F2FS_MOUNT_DISABLE_CHECKPOINT	0x02000000
103a9117ecaSChao Yu #define F2FS_MOUNT_NORECOVERY		0x04000000
104093749e2SChao Yu #define F2FS_MOUNT_ATGC			0x08000000
105261eeb9cSDaeho Jeong #define F2FS_MOUNT_MERGE_CHECKPOINT	0x10000000
1065911d2d1SChao Yu #define	F2FS_MOUNT_GC_MERGE		0x20000000
1076ce19affSChao Yu #define F2FS_MOUNT_COMPRESS_CACHE	0x40000000
10839a53e0cSJaegeuk Kim 
10963189b78SChao Yu #define F2FS_OPTION(sbi)	((sbi)->mount_opt)
11063189b78SChao Yu #define clear_opt(sbi, option)	(F2FS_OPTION(sbi).opt &= ~F2FS_MOUNT_##option)
11163189b78SChao Yu #define set_opt(sbi, option)	(F2FS_OPTION(sbi).opt |= F2FS_MOUNT_##option)
11263189b78SChao Yu #define test_opt(sbi, option)	(F2FS_OPTION(sbi).opt & F2FS_MOUNT_##option)
11339a53e0cSJaegeuk Kim 
11439a53e0cSJaegeuk Kim #define ver_after(a, b)	(typecheck(unsigned long long, a) &&		\
11539a53e0cSJaegeuk Kim 		typecheck(unsigned long long, b) &&			\
11639a53e0cSJaegeuk Kim 		((long long)((a) - (b)) > 0))
11739a53e0cSJaegeuk Kim 
118a9841c4dSJaegeuk Kim typedef u32 block_t;	/*
119a9841c4dSJaegeuk Kim 			 * should not change u32, since it is the on-disk block
120a9841c4dSJaegeuk Kim 			 * address format, __le32.
121a9841c4dSJaegeuk Kim 			 */
12239a53e0cSJaegeuk Kim typedef u32 nid_t;
12339a53e0cSJaegeuk Kim 
1244c8ff709SChao Yu #define COMPRESS_EXT_NUM		16
1254c8ff709SChao Yu 
126e4544b63STim Murray /*
127e4544b63STim Murray  * An implementation of an rwsem that is explicitly unfair to readers. This
128e4544b63STim Murray  * prevents priority inversion when a low-priority reader acquires the read lock
129e4544b63STim Murray  * while sleeping on the write lock but the write lock is needed by
130e4544b63STim Murray  * higher-priority clients.
131e4544b63STim Murray  */
132e4544b63STim Murray 
133e4544b63STim Murray struct f2fs_rwsem {
134e4544b63STim Murray         struct rw_semaphore internal_rwsem;
1357f8e249dSJaegeuk Kim #ifdef CONFIG_F2FS_UNFAIR_RWSEM
136e4544b63STim Murray         wait_queue_head_t read_waiters;
1377f8e249dSJaegeuk Kim #endif
138e4544b63STim Murray };
139e4544b63STim Murray 
14039a53e0cSJaegeuk Kim struct f2fs_mount_info {
14139a53e0cSJaegeuk Kim 	unsigned int opt;
14263189b78SChao Yu 	int write_io_size_bits;		/* Write IO size bits */
14363189b78SChao Yu 	block_t root_reserved_blocks;	/* root reserved blocks */
14463189b78SChao Yu 	kuid_t s_resuid;		/* reserved blocks for uid */
14563189b78SChao Yu 	kgid_t s_resgid;		/* reserved blocks for gid */
14663189b78SChao Yu 	int active_logs;		/* # of active logs */
14763189b78SChao Yu 	int inline_xattr_size;		/* inline xattr size */
14863189b78SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
14963189b78SChao Yu 	struct f2fs_fault_info fault_info;	/* For fault injection */
15063189b78SChao Yu #endif
15163189b78SChao Yu #ifdef CONFIG_QUOTA
15263189b78SChao Yu 	/* Names of quota files with journalled quota */
15363189b78SChao Yu 	char *s_qf_names[MAXQUOTAS];
15463189b78SChao Yu 	int s_jquota_fmt;			/* Format of quota to use */
15563189b78SChao Yu #endif
15663189b78SChao Yu 	/* For which write hints are passed down to block layer */
15763189b78SChao Yu 	int whint_mode;
15863189b78SChao Yu 	int alloc_mode;			/* segment allocation policy */
15963189b78SChao Yu 	int fsync_mode;			/* fsync policy */
160b0332a0fSChao Yu 	int fs_mode;			/* fs mode: LFS or ADAPTIVE */
161bbbc34fdSChao Yu 	int bggc_mode;			/* bggc mode: off, on or sync */
1624f993264SChao Yu 	int discard_unit;		/*
1634f993264SChao Yu 					 * discard command's offset/size should
1644f993264SChao Yu 					 * be aligned to this unit: block,
1654f993264SChao Yu 					 * segment or section
1664f993264SChao Yu 					 */
167ac4acb1fSEric Biggers 	struct fscrypt_dummy_policy dummy_enc_policy; /* test dummy encryption */
1681ae18f71SJaegeuk Kim 	block_t unusable_cap_perc;	/* percentage for cap */
1694d3aed70SDaniel Rosenberg 	block_t unusable_cap;		/* Amount of space allowed to be
1704d3aed70SDaniel Rosenberg 					 * unusable when disabling checkpoint
1714d3aed70SDaniel Rosenberg 					 */
1724c8ff709SChao Yu 
1734c8ff709SChao Yu 	/* For compression */
1744c8ff709SChao Yu 	unsigned char compress_algorithm;	/* algorithm type */
175b28f047bSChao Yu 	unsigned char compress_log_size;	/* cluster log size */
1763fde13f8SChao Yu 	unsigned char compress_level;		/* compress level */
177b28f047bSChao Yu 	bool compress_chksum;			/* compressed data chksum */
1784c8ff709SChao Yu 	unsigned char compress_ext_cnt;		/* extension count */
179151b1982SFengnan Chang 	unsigned char nocompress_ext_cnt;		/* nocompress extension count */
180602a16d5SDaeho Jeong 	int compress_mode;			/* compression mode */
1814c8ff709SChao Yu 	unsigned char extensions[COMPRESS_EXT_NUM][F2FS_EXTENSION_LEN];	/* extensions */
182151b1982SFengnan Chang 	unsigned char noextensions[COMPRESS_EXT_NUM][F2FS_EXTENSION_LEN]; /* extensions */
18339a53e0cSJaegeuk Kim };
18439a53e0cSJaegeuk Kim 
185cde4de12SJaegeuk Kim #define F2FS_FEATURE_ENCRYPT		0x0001
1860bfd7a09SDamien Le Moal #define F2FS_FEATURE_BLKZONED		0x0002
187e65ef207SJaegeuk Kim #define F2FS_FEATURE_ATOMIC_WRITE	0x0004
1887a2af766SChao Yu #define F2FS_FEATURE_EXTRA_ATTR		0x0008
1895c57132eSChao Yu #define F2FS_FEATURE_PRJQUOTA		0x0010
190704956ecSChao Yu #define F2FS_FEATURE_INODE_CHKSUM	0x0020
1916afc662eSChao Yu #define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR	0x0040
192234a9689SJaegeuk Kim #define F2FS_FEATURE_QUOTA_INO		0x0080
1931c1d35dfSChao Yu #define F2FS_FEATURE_INODE_CRTIME	0x0100
194b7c409deSSheng Yong #define F2FS_FEATURE_LOST_FOUND		0x0200
19595ae251fSEric Biggers #define F2FS_FEATURE_VERITY		0x0400
196d440c52dSJunling Zheng #define F2FS_FEATURE_SB_CHKSUM		0x0800
1975aba5430SDaniel Rosenberg #define F2FS_FEATURE_CASEFOLD		0x1000
1984c8ff709SChao Yu #define F2FS_FEATURE_COMPRESSION	0x2000
199a7d9fe3cSJaegeuk Kim #define F2FS_FEATURE_RO			0x4000
200cde4de12SJaegeuk Kim 
2017beb01f7SChao Yu #define __F2FS_HAS_FEATURE(raw_super, mask)				\
2027beb01f7SChao Yu 	((raw_super->feature & cpu_to_le32(mask)) != 0)
2037beb01f7SChao Yu #define F2FS_HAS_FEATURE(sbi, mask)	__F2FS_HAS_FEATURE(sbi->raw_super, mask)
2047beb01f7SChao Yu #define F2FS_SET_FEATURE(sbi, mask)					\
2057beb01f7SChao Yu 	(sbi->raw_super->feature |= cpu_to_le32(mask))
2067beb01f7SChao Yu #define F2FS_CLEAR_FEATURE(sbi, mask)					\
2077beb01f7SChao Yu 	(sbi->raw_super->feature &= ~cpu_to_le32(mask))
20876f105a2SJaegeuk Kim 
20939a53e0cSJaegeuk Kim /*
2107c2e5963SJaegeuk Kim  * Default values for user and/or group using reserved blocks
2117c2e5963SJaegeuk Kim  */
2127c2e5963SJaegeuk Kim #define	F2FS_DEF_RESUID		0
2137c2e5963SJaegeuk Kim #define	F2FS_DEF_RESGID		0
2147c2e5963SJaegeuk Kim 
2157c2e5963SJaegeuk Kim /*
21639a53e0cSJaegeuk Kim  * For checkpoint manager
21739a53e0cSJaegeuk Kim  */
21839a53e0cSJaegeuk Kim enum {
21939a53e0cSJaegeuk Kim 	NAT_BITMAP,
22039a53e0cSJaegeuk Kim 	SIT_BITMAP
22139a53e0cSJaegeuk Kim };
22239a53e0cSJaegeuk Kim 
223c473f1a9SChao Yu #define	CP_UMOUNT	0x00000001
224c473f1a9SChao Yu #define	CP_FASTBOOT	0x00000002
225c473f1a9SChao Yu #define	CP_SYNC		0x00000004
226c473f1a9SChao Yu #define	CP_RECOVERY	0x00000008
227c473f1a9SChao Yu #define	CP_DISCARD	0x00000010
2281f43e2adSChao Yu #define CP_TRIMMED	0x00000020
2294354994fSDaniel Rosenberg #define CP_PAUSE	0x00000040
230b4b10061SJaegeuk Kim #define CP_RESIZE 	0x00000080
23175ab4cb8SJaegeuk Kim 
2324ddb1a4dSJaegeuk Kim #define MAX_DISCARD_BLOCKS(sbi)		BLKS_PER_SEC(sbi)
233ecc9aa00SChao Yu #define DEF_MAX_DISCARD_REQUEST		8	/* issue 8 discards per round */
234969d1b18SChao Yu #define DEF_MIN_DISCARD_ISSUE_TIME	50	/* 50 ms, if exists */
235f9d1dcedSYunlei He #define DEF_MID_DISCARD_ISSUE_TIME	500	/* 500 ms, if device busy */
236969d1b18SChao Yu #define DEF_MAX_DISCARD_ISSUE_TIME	60000	/* 60 s, if no candidates */
2378bb4f253SJaegeuk Kim #define DEF_DISCARD_URGENT_UTIL		80	/* do more discard over 80% */
23860b99b48SJaegeuk Kim #define DEF_CP_INTERVAL			60	/* 60 secs */
239dcf25fe8SChao Yu #define DEF_IDLE_INTERVAL		5	/* 5 secs */
2404354994fSDaniel Rosenberg #define DEF_DISABLE_INTERVAL		5	/* 5 secs */
241db610a64SJaegeuk Kim #define DEF_DISABLE_QUICK_INTERVAL	1	/* 1 secs */
24203f2c02dSJaegeuk Kim #define DEF_UMOUNT_DISCARD_TIMEOUT	5	/* 5 secs */
243bba681cbSJaegeuk Kim 
24475ab4cb8SJaegeuk Kim struct cp_control {
24575ab4cb8SJaegeuk Kim 	int reason;
2464b2fecc8SJaegeuk Kim 	__u64 trim_start;
2474b2fecc8SJaegeuk Kim 	__u64 trim_end;
2484b2fecc8SJaegeuk Kim 	__u64 trim_minlen;
24975ab4cb8SJaegeuk Kim };
25075ab4cb8SJaegeuk Kim 
251662befdaSChao Yu /*
252e1da7872SChao Yu  * indicate meta/data type
253662befdaSChao Yu  */
254662befdaSChao Yu enum {
255662befdaSChao Yu 	META_CP,
256662befdaSChao Yu 	META_NAT,
25781c1a0f1SChao Yu 	META_SIT,
2584c521f49SJaegeuk Kim 	META_SSA,
259b63e7be5SChao Yu 	META_MAX,
2604c521f49SJaegeuk Kim 	META_POR,
26193770ab7SChao Yu 	DATA_GENERIC,		/* check range only */
26293770ab7SChao Yu 	DATA_GENERIC_ENHANCE,	/* strong check on range and segment bitmap */
26393770ab7SChao Yu 	DATA_GENERIC_ENHANCE_READ,	/*
26493770ab7SChao Yu 					 * strong check on range and segment
26593770ab7SChao Yu 					 * bitmap but no warning due to race
26693770ab7SChao Yu 					 * condition of read on truncated area
26793770ab7SChao Yu 					 * by extent_cache
26893770ab7SChao Yu 					 */
269e1da7872SChao Yu 	META_GENERIC,
270662befdaSChao Yu };
271662befdaSChao Yu 
2726451e041SJaegeuk Kim /* for the list of ino */
2736451e041SJaegeuk Kim enum {
2746451e041SJaegeuk Kim 	ORPHAN_INO,		/* for orphan ino list */
275fff04f90SJaegeuk Kim 	APPEND_INO,		/* for append ino list */
276fff04f90SJaegeuk Kim 	UPDATE_INO,		/* for update ino list */
2770a007b97SJaegeuk Kim 	TRANS_DIR_INO,		/* for trasactions dir ino list */
27839d787beSChao Yu 	FLUSH_INO,		/* for multiple device flushing */
2796451e041SJaegeuk Kim 	MAX_INO_ENTRY,		/* max. list */
2806451e041SJaegeuk Kim };
2816451e041SJaegeuk Kim 
2826451e041SJaegeuk Kim struct ino_entry {
28339a53e0cSJaegeuk Kim 	struct list_head list;		/* list head */
28439a53e0cSJaegeuk Kim 	nid_t ino;			/* inode number */
28539d787beSChao Yu 	unsigned int dirty_device;	/* dirty device bitmap */
28639a53e0cSJaegeuk Kim };
28739a53e0cSJaegeuk Kim 
2882710fd7eSChao Yu /* for the list of inodes to be GCed */
28906292073SChao Yu struct inode_entry {
29039a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
29139a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
29239a53e0cSJaegeuk Kim };
29339a53e0cSJaegeuk Kim 
29450fa53ecSChao Yu struct fsync_node_entry {
29550fa53ecSChao Yu 	struct list_head list;	/* list head */
29650fa53ecSChao Yu 	struct page *page;	/* warm node page pointer */
29750fa53ecSChao Yu 	unsigned int seq_id;	/* sequence id */
29850fa53ecSChao Yu };
29950fa53ecSChao Yu 
300261eeb9cSDaeho Jeong struct ckpt_req {
301261eeb9cSDaeho Jeong 	struct completion wait;		/* completion for checkpoint done */
302261eeb9cSDaeho Jeong 	struct llist_node llnode;	/* llist_node to be linked in wait queue */
303261eeb9cSDaeho Jeong 	int ret;			/* return code of checkpoint */
304261eeb9cSDaeho Jeong 	ktime_t queue_time;		/* request queued time */
305261eeb9cSDaeho Jeong };
306261eeb9cSDaeho Jeong 
307261eeb9cSDaeho Jeong struct ckpt_req_control {
308261eeb9cSDaeho Jeong 	struct task_struct *f2fs_issue_ckpt;	/* checkpoint task */
309e6592066SDaeho Jeong 	int ckpt_thread_ioprio;			/* checkpoint merge thread ioprio */
310261eeb9cSDaeho Jeong 	wait_queue_head_t ckpt_wait_queue;	/* waiting queue for wake-up */
311261eeb9cSDaeho Jeong 	atomic_t issued_ckpt;		/* # of actually issued ckpts */
312261eeb9cSDaeho Jeong 	atomic_t total_ckpt;		/* # of total ckpts */
313261eeb9cSDaeho Jeong 	atomic_t queued_ckpt;		/* # of queued ckpts */
314261eeb9cSDaeho Jeong 	struct llist_head issue_list;	/* list for command issue */
315261eeb9cSDaeho Jeong 	spinlock_t stat_lock;		/* lock for below checkpoint time stats */
316261eeb9cSDaeho Jeong 	unsigned int cur_time;		/* cur wait time in msec for currently issued checkpoint */
317261eeb9cSDaeho Jeong 	unsigned int peak_time;		/* peak wait time in msec until now */
318261eeb9cSDaeho Jeong };
319261eeb9cSDaeho Jeong 
320a7eeb823SChao Yu /* for the bitmap indicate blocks to be discarded */
3217fd9e544SJaegeuk Kim struct discard_entry {
3227fd9e544SJaegeuk Kim 	struct list_head list;	/* list head */
323a7eeb823SChao Yu 	block_t start_blkaddr;	/* start blockaddr of current segment */
324a7eeb823SChao Yu 	unsigned char discard_map[SIT_VBLOCK_MAP_SIZE];	/* segment discard bitmap */
3257fd9e544SJaegeuk Kim };
3267fd9e544SJaegeuk Kim 
327969d1b18SChao Yu /* default discard granularity of inner discard thread, unit: block count */
328969d1b18SChao Yu #define DEFAULT_DISCARD_GRANULARITY		16
329969d1b18SChao Yu 
330ba48a33eSChao Yu /* max discard pend list number */
331ba48a33eSChao Yu #define MAX_PLIST_NUM		512
332ba48a33eSChao Yu #define plist_idx(blk_num)	((blk_num) >= MAX_PLIST_NUM ?		\
3332f84babfSSheng Yong 					(MAX_PLIST_NUM - 1) : ((blk_num) - 1))
334ba48a33eSChao Yu 
33515469963SJaegeuk Kim enum {
33635ec7d57SChao Yu 	D_PREP,			/* initial */
33735ec7d57SChao Yu 	D_PARTIAL,		/* partially submitted */
33835ec7d57SChao Yu 	D_SUBMIT,		/* all submitted */
33935ec7d57SChao Yu 	D_DONE,			/* finished */
34015469963SJaegeuk Kim };
34115469963SJaegeuk Kim 
342004b6862SChao Yu struct discard_info {
343b01a9201SJaegeuk Kim 	block_t lstart;			/* logical start address */
344b01a9201SJaegeuk Kim 	block_t len;			/* length */
345004b6862SChao Yu 	block_t start;			/* actual start address in dev */
346004b6862SChao Yu };
347004b6862SChao Yu 
348b01a9201SJaegeuk Kim struct discard_cmd {
349004b6862SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
350004b6862SChao Yu 	union {
351004b6862SChao Yu 		struct {
352004b6862SChao Yu 			block_t lstart;	/* logical start address */
353004b6862SChao Yu 			block_t len;	/* length */
354004b6862SChao Yu 			block_t start;	/* actual start address in dev */
355004b6862SChao Yu 		};
356004b6862SChao Yu 		struct discard_info di;	/* discard info */
357004b6862SChao Yu 
358004b6862SChao Yu 	};
359b01a9201SJaegeuk Kim 	struct list_head list;		/* command list */
360b01a9201SJaegeuk Kim 	struct completion wait;		/* compleation */
361c81abe34SJaegeuk Kim 	struct block_device *bdev;	/* bdev */
362ec9895adSChao Yu 	unsigned short ref;		/* reference count */
3639a744b92SChao Yu 	unsigned char state;		/* state */
36472691af6SJaegeuk Kim 	unsigned char queued;		/* queued discard */
365c81abe34SJaegeuk Kim 	int error;			/* bio error */
36635ec7d57SChao Yu 	spinlock_t lock;		/* for state/bio_ref updating */
36735ec7d57SChao Yu 	unsigned short bio_ref;		/* bio reference count */
368275b66b0SChao Yu };
369275b66b0SChao Yu 
37078997b56SChao Yu enum {
37178997b56SChao Yu 	DPOLICY_BG,
37278997b56SChao Yu 	DPOLICY_FORCE,
37378997b56SChao Yu 	DPOLICY_FSTRIM,
37478997b56SChao Yu 	DPOLICY_UMOUNT,
37578997b56SChao Yu 	MAX_DPOLICY,
37678997b56SChao Yu };
37778997b56SChao Yu 
378ecc9aa00SChao Yu struct discard_policy {
37978997b56SChao Yu 	int type;			/* type of discard */
380ecc9aa00SChao Yu 	unsigned int min_interval;	/* used for candidates exist */
381f9d1dcedSYunlei He 	unsigned int mid_interval;	/* used for device busy */
382ecc9aa00SChao Yu 	unsigned int max_interval;	/* used for candidates not exist */
383ecc9aa00SChao Yu 	unsigned int max_requests;	/* # of discards issued per round */
384ecc9aa00SChao Yu 	unsigned int io_aware_gran;	/* minimum granularity discard not be aware of I/O */
385ecc9aa00SChao Yu 	bool io_aware;			/* issue discard in idle time */
386ecc9aa00SChao Yu 	bool sync;			/* submit discard with REQ_SYNC flag */
38720ee4382SChao Yu 	bool ordered;			/* issue discard by lba order */
3886ce48b0cSChao Yu 	bool timeout;			/* discard timeout for put_super */
38978997b56SChao Yu 	unsigned int granularity;	/* discard granularity */
390ecc9aa00SChao Yu };
391ecc9aa00SChao Yu 
3920b54fb84SJaegeuk Kim struct discard_cmd_control {
39315469963SJaegeuk Kim 	struct task_struct *f2fs_issue_discard;	/* discard thread */
39446f84c2cSChao Yu 	struct list_head entry_list;		/* 4KB discard entry list */
395ba48a33eSChao Yu 	struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
39646f84c2cSChao Yu 	struct list_head wait_list;		/* store on-flushing entries */
3978412663dSChao Yu 	struct list_head fstrim_list;		/* in-flight discard from fstrim */
39815469963SJaegeuk Kim 	wait_queue_head_t discard_wait_queue;	/* waiting queue for wake-up */
399969d1b18SChao Yu 	unsigned int discard_wake;		/* to wake up discard thread */
40015469963SJaegeuk Kim 	struct mutex cmd_lock;
401d618ebafSChao Yu 	unsigned int nr_discards;		/* # of discards in the list */
402d618ebafSChao Yu 	unsigned int max_discards;		/* max. discards to be issued */
403d2d8e896SKonstantin Vyshetsky 	unsigned int max_discard_request;	/* max. discard request per round */
404d2d8e896SKonstantin Vyshetsky 	unsigned int min_discard_issue_time;	/* min. interval between discard issue */
405d2d8e896SKonstantin Vyshetsky 	unsigned int mid_discard_issue_time;	/* mid. interval between discard issue */
406d2d8e896SKonstantin Vyshetsky 	unsigned int max_discard_issue_time;	/* max. interval between discard issue */
407969d1b18SChao Yu 	unsigned int discard_granularity;	/* discard granularity */
408d84d1cbdSChao Yu 	unsigned int undiscard_blks;		/* # of undiscard blocks */
40920ee4382SChao Yu 	unsigned int next_pos;			/* next discard position */
4108b8dd65fSChao Yu 	atomic_t issued_discard;		/* # of issued discard */
41172691af6SJaegeuk Kim 	atomic_t queued_discard;		/* # of queued discard */
4125f32366aSChao Yu 	atomic_t discard_cmd_cnt;		/* # of cached cmd count */
4134dada3fdSChao Yu 	struct rb_root_cached root;		/* root of discard rb-tree */
41467fce70bSChao Yu 	bool rbtree_check;			/* config for consistence check */
41539a53e0cSJaegeuk Kim };
41639a53e0cSJaegeuk Kim 
41739a53e0cSJaegeuk Kim /* for the list of fsync inodes, used only during recovery */
41839a53e0cSJaegeuk Kim struct fsync_inode_entry {
41939a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
42039a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
421c52e1b10SJaegeuk Kim 	block_t blkaddr;	/* block address locating the last fsync */
422c52e1b10SJaegeuk Kim 	block_t last_dentry;	/* block address locating the last dentry */
42339a53e0cSJaegeuk Kim };
42439a53e0cSJaegeuk Kim 
42568afcf2dSTomohiro Kusumi #define nats_in_cursum(jnl)		(le16_to_cpu((jnl)->n_nats))
42668afcf2dSTomohiro Kusumi #define sits_in_cursum(jnl)		(le16_to_cpu((jnl)->n_sits))
42739a53e0cSJaegeuk Kim 
42868afcf2dSTomohiro Kusumi #define nat_in_journal(jnl, i)		((jnl)->nat_j.entries[i].ne)
42968afcf2dSTomohiro Kusumi #define nid_in_journal(jnl, i)		((jnl)->nat_j.entries[i].nid)
43068afcf2dSTomohiro Kusumi #define sit_in_journal(jnl, i)		((jnl)->sit_j.entries[i].se)
43168afcf2dSTomohiro Kusumi #define segno_in_journal(jnl, i)	((jnl)->sit_j.entries[i].segno)
43239a53e0cSJaegeuk Kim 
433dfc08a12SChao Yu #define MAX_NAT_JENTRIES(jnl)	(NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
434dfc08a12SChao Yu #define MAX_SIT_JENTRIES(jnl)	(SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
435309cc2b6SJaegeuk Kim 
436dfc08a12SChao Yu static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
43739a53e0cSJaegeuk Kim {
438dfc08a12SChao Yu 	int before = nats_in_cursum(journal);
439cac5a3d8SDongOh Shin 
440dfc08a12SChao Yu 	journal->n_nats = cpu_to_le16(before + i);
44139a53e0cSJaegeuk Kim 	return before;
44239a53e0cSJaegeuk Kim }
44339a53e0cSJaegeuk Kim 
444dfc08a12SChao Yu static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
44539a53e0cSJaegeuk Kim {
446dfc08a12SChao Yu 	int before = sits_in_cursum(journal);
447cac5a3d8SDongOh Shin 
448dfc08a12SChao Yu 	journal->n_sits = cpu_to_le16(before + i);
44939a53e0cSJaegeuk Kim 	return before;
45039a53e0cSJaegeuk Kim }
45139a53e0cSJaegeuk Kim 
452dfc08a12SChao Yu static inline bool __has_cursum_space(struct f2fs_journal *journal,
453dfc08a12SChao Yu 							int size, int type)
454184a5cd2SChao Yu {
455184a5cd2SChao Yu 	if (type == NAT_JOURNAL)
456dfc08a12SChao Yu 		return size <= MAX_NAT_JENTRIES(journal);
457dfc08a12SChao Yu 	return size <= MAX_SIT_JENTRIES(journal);
458184a5cd2SChao Yu }
459184a5cd2SChao Yu 
460f2470371SChao Yu /* for inline stuff */
461f2470371SChao Yu #define DEF_INLINE_RESERVED_SIZE	1
4627a2af766SChao Yu static inline int get_extra_isize(struct inode *inode);
4636afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode);
4647a2af766SChao Yu #define MAX_INLINE_DATA(inode)	(sizeof(__le32) *			\
4657a2af766SChao Yu 				(CUR_ADDRS_PER_INODE(inode) -		\
466b323fd28SChao Yu 				get_inline_xattr_addrs(inode) -	\
4676afc662eSChao Yu 				DEF_INLINE_RESERVED_SIZE))
468f2470371SChao Yu 
469f2470371SChao Yu /* for inline dir */
470f2470371SChao Yu #define NR_INLINE_DENTRY(inode)	(MAX_INLINE_DATA(inode) * BITS_PER_BYTE / \
471f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
472f2470371SChao Yu 				BITS_PER_BYTE + 1))
473f91108b8SGeert Uytterhoeven #define INLINE_DENTRY_BITMAP_SIZE(inode) \
474f91108b8SGeert Uytterhoeven 	DIV_ROUND_UP(NR_INLINE_DENTRY(inode), BITS_PER_BYTE)
475f2470371SChao Yu #define INLINE_RESERVED_SIZE(inode)	(MAX_INLINE_DATA(inode) - \
476f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
477f2470371SChao Yu 				NR_INLINE_DENTRY(inode) + \
478f2470371SChao Yu 				INLINE_DENTRY_BITMAP_SIZE(inode)))
479f2470371SChao Yu 
480e9750824SNamjae Jeon /*
48139a53e0cSJaegeuk Kim  * For INODE and NODE manager
48239a53e0cSJaegeuk Kim  */
4837b3cd7d6SJaegeuk Kim /* for directory operations */
48443c780baSEric Biggers 
48543c780baSEric Biggers struct f2fs_filename {
48643c780baSEric Biggers 	/*
48743c780baSEric Biggers 	 * The filename the user specified.  This is NULL for some
48843c780baSEric Biggers 	 * filesystem-internal operations, e.g. converting an inline directory
48943c780baSEric Biggers 	 * to a non-inline one, or roll-forward recovering an encrypted dentry.
49043c780baSEric Biggers 	 */
49143c780baSEric Biggers 	const struct qstr *usr_fname;
49243c780baSEric Biggers 
49343c780baSEric Biggers 	/*
49443c780baSEric Biggers 	 * The on-disk filename.  For encrypted directories, this is encrypted.
49543c780baSEric Biggers 	 * This may be NULL for lookups in an encrypted dir without the key.
49643c780baSEric Biggers 	 */
49743c780baSEric Biggers 	struct fscrypt_str disk_name;
49843c780baSEric Biggers 
49943c780baSEric Biggers 	/* The dirhash of this filename */
50043c780baSEric Biggers 	f2fs_hash_t hash;
50143c780baSEric Biggers 
50243c780baSEric Biggers #ifdef CONFIG_FS_ENCRYPTION
50343c780baSEric Biggers 	/*
50443c780baSEric Biggers 	 * For lookups in encrypted directories: either the buffer backing
50543c780baSEric Biggers 	 * disk_name, or a buffer that holds the decoded no-key name.
50643c780baSEric Biggers 	 */
50743c780baSEric Biggers 	struct fscrypt_str crypto_buf;
50843c780baSEric Biggers #endif
50943c780baSEric Biggers #ifdef CONFIG_UNICODE
51043c780baSEric Biggers 	/*
51143c780baSEric Biggers 	 * For casefolded directories: the casefolded name, but it's left NULL
5127ad08a58SDaniel Rosenberg 	 * if the original name is not valid Unicode, if the directory is both
5137ad08a58SDaniel Rosenberg 	 * casefolded and encrypted and its encryption key is unavailable, or if
5147ad08a58SDaniel Rosenberg 	 * the filesystem is doing an internal operation where usr_fname is also
5157ad08a58SDaniel Rosenberg 	 * NULL.  In all these cases we fall back to treating the name as an
5167ad08a58SDaniel Rosenberg 	 * opaque byte sequence.
51743c780baSEric Biggers 	 */
51843c780baSEric Biggers 	struct fscrypt_str cf_name;
51943c780baSEric Biggers #endif
52043c780baSEric Biggers };
52143c780baSEric Biggers 
5227b3cd7d6SJaegeuk Kim struct f2fs_dentry_ptr {
523d8c6822aSJaegeuk Kim 	struct inode *inode;
52476a9dd85SChao Yu 	void *bitmap;
5257b3cd7d6SJaegeuk Kim 	struct f2fs_dir_entry *dentry;
5267b3cd7d6SJaegeuk Kim 	__u8 (*filename)[F2FS_SLOT_LEN];
5277b3cd7d6SJaegeuk Kim 	int max;
52876a9dd85SChao Yu 	int nr_bitmap;
5297b3cd7d6SJaegeuk Kim };
5307b3cd7d6SJaegeuk Kim 
53164c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_block(struct inode *inode,
53264c24ecbSTomohiro Kusumi 		struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
5337b3cd7d6SJaegeuk Kim {
534d8c6822aSJaegeuk Kim 	d->inode = inode;
5357b3cd7d6SJaegeuk Kim 	d->max = NR_DENTRY_IN_BLOCK;
53676a9dd85SChao Yu 	d->nr_bitmap = SIZE_OF_DENTRY_BITMAP;
537c79d1520SYunlong Song 	d->bitmap = t->dentry_bitmap;
5387b3cd7d6SJaegeuk Kim 	d->dentry = t->dentry;
5397b3cd7d6SJaegeuk Kim 	d->filename = t->filename;
54064c24ecbSTomohiro Kusumi }
541cac5a3d8SDongOh Shin 
54264c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_inline(struct inode *inode,
543f2470371SChao Yu 					struct f2fs_dentry_ptr *d, void *t)
54464c24ecbSTomohiro Kusumi {
545f2470371SChao Yu 	int entry_cnt = NR_INLINE_DENTRY(inode);
546f2470371SChao Yu 	int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(inode);
547f2470371SChao Yu 	int reserved_size = INLINE_RESERVED_SIZE(inode);
548f2470371SChao Yu 
54964c24ecbSTomohiro Kusumi 	d->inode = inode;
550f2470371SChao Yu 	d->max = entry_cnt;
551f2470371SChao Yu 	d->nr_bitmap = bitmap_size;
552f2470371SChao Yu 	d->bitmap = t;
553f2470371SChao Yu 	d->dentry = t + bitmap_size + reserved_size;
554f2470371SChao Yu 	d->filename = t + bitmap_size + reserved_size +
555f2470371SChao Yu 					SIZE_OF_DIR_ENTRY * entry_cnt;
5567b3cd7d6SJaegeuk Kim }
5577b3cd7d6SJaegeuk Kim 
558dbe6a5ffSJaegeuk Kim /*
559dbe6a5ffSJaegeuk Kim  * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
560dbe6a5ffSJaegeuk Kim  * as its node offset to distinguish from index node blocks.
561dbe6a5ffSJaegeuk Kim  * But some bits are used to mark the node block.
56239a53e0cSJaegeuk Kim  */
563dbe6a5ffSJaegeuk Kim #define XATTR_NODE_OFFSET	((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
564dbe6a5ffSJaegeuk Kim 				>> OFFSET_BIT_SHIFT)
565266e97a8SJaegeuk Kim enum {
566266e97a8SJaegeuk Kim 	ALLOC_NODE,			/* allocate a new node page if needed */
567266e97a8SJaegeuk Kim 	LOOKUP_NODE,			/* look up a node without readahead */
568266e97a8SJaegeuk Kim 	LOOKUP_NODE_RA,			/*
569266e97a8SJaegeuk Kim 					 * look up a node with readahead called
5704f4124d0SChao Yu 					 * by get_data_block.
57139a53e0cSJaegeuk Kim 					 */
572266e97a8SJaegeuk Kim };
573266e97a8SJaegeuk Kim 
57491803392SChao Yu #define DEFAULT_RETRY_IO_COUNT	8	/* maximum retry read IO or flush count */
5757735730dSChao Yu 
5765df7731fSChao Yu /* congestion wait timeout value, default: 20ms */
5775df7731fSChao Yu #define	DEFAULT_IO_TIMEOUT	(msecs_to_jiffies(20))
5785df7731fSChao Yu 
579af033b2aSChao Yu /* maximum retry quota flush count */
580af033b2aSChao Yu #define DEFAULT_RETRY_QUOTA_FLUSH_COUNT		8
581af033b2aSChao Yu 
58250c63009SJaegeuk Kim /* maximum retry of EIO'ed meta page */
58350c63009SJaegeuk Kim #define MAX_RETRY_META_PAGE_EIO			100
58450c63009SJaegeuk Kim 
585a6db67f0SJaegeuk Kim #define F2FS_LINK_MAX	0xffffffff	/* maximum link count per file */
58639a53e0cSJaegeuk Kim 
587817202d9SChao Yu #define MAX_DIR_RA_PAGES	4	/* maximum ra pages of dir */
588817202d9SChao Yu 
589287b1406SChao Yu /* dirty segments threshold for triggering CP */
590287b1406SChao Yu #define DEFAULT_DIRTY_THRESHOLD		4
591287b1406SChao Yu 
59239a53e0cSJaegeuk Kim /* for in-memory extent cache entry */
59313054c54SChao Yu #define F2FS_MIN_EXTENT_LEN	64	/* minimum extent length */
59413054c54SChao Yu 
59513054c54SChao Yu /* number of extent info in extent cache we try to shrink */
59613054c54SChao Yu #define EXTENT_CACHE_SHRINK_NUMBER	128
597c11abd1aSJaegeuk Kim 
598430f163bSChao Yu #define RECOVERY_MAX_RA_BLOCKS		BIO_MAX_VECS
599430f163bSChao Yu #define RECOVERY_MIN_RA_BLOCKS		1
600430f163bSChao Yu 
60154c2258cSChao Yu struct rb_entry {
60254c2258cSChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
6032e9b2bb2SChao Yu 	union {
6042e9b2bb2SChao Yu 		struct {
60554c2258cSChao Yu 			unsigned int ofs;	/* start offset of the entry */
60654c2258cSChao Yu 			unsigned int len;	/* length of the entry */
60754c2258cSChao Yu 		};
6082e9b2bb2SChao Yu 		unsigned long long key;		/* 64-bits key */
60948046cb5SJaegeuk Kim 	} __packed;
6102e9b2bb2SChao Yu };
61154c2258cSChao Yu 
61239a53e0cSJaegeuk Kim struct extent_info {
61339a53e0cSJaegeuk Kim 	unsigned int fofs;		/* start offset in a file */
614111d2495SMasanari Iida 	unsigned int len;		/* length of the extent */
61554c2258cSChao Yu 	u32 blk;			/* start block address of the extent */
61694afd6d6SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
61794afd6d6SChao Yu 	unsigned int c_len;		/* physical extent length of compressed blocks */
61894afd6d6SChao Yu #endif
61939a53e0cSJaegeuk Kim };
62039a53e0cSJaegeuk Kim 
62113054c54SChao Yu struct extent_node {
622c0362117SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
62313054c54SChao Yu 	struct extent_info ei;		/* extent info */
62454c2258cSChao Yu 	struct list_head list;		/* node in global extent list of sbi */
625201ef5e0SHou Pengyang 	struct extent_tree *et;		/* extent tree pointer */
62613054c54SChao Yu };
62713054c54SChao Yu 
62813054c54SChao Yu struct extent_tree {
62913054c54SChao Yu 	nid_t ino;			/* inode number */
6304dada3fdSChao Yu 	struct rb_root_cached root;	/* root of extent info rb-tree */
63162c8af65SChao Yu 	struct extent_node *cached_en;	/* recently accessed extent node */
6323e72f721SJaegeuk Kim 	struct extent_info largest;	/* largested extent info */
633137d09f0SJaegeuk Kim 	struct list_head list;		/* to be used by sbi->zombie_list */
63413054c54SChao Yu 	rwlock_t lock;			/* protect extent info rb-tree */
63568e35385SChao Yu 	atomic_t node_cnt;		/* # of extent node in rb-tree*/
636b430f726SZhikang Zhang 	bool largest_updated;		/* largest extent updated */
63713054c54SChao Yu };
63813054c54SChao Yu 
63939a53e0cSJaegeuk Kim /*
640003a3e1dSJaegeuk Kim  * This structure is taken from ext4_map_blocks.
641003a3e1dSJaegeuk Kim  *
642003a3e1dSJaegeuk Kim  * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
643003a3e1dSJaegeuk Kim  */
644003a3e1dSJaegeuk Kim #define F2FS_MAP_NEW		(1 << BH_New)
645003a3e1dSJaegeuk Kim #define F2FS_MAP_MAPPED		(1 << BH_Mapped)
6467f63eb77SJaegeuk Kim #define F2FS_MAP_UNWRITTEN	(1 << BH_Unwritten)
6477f63eb77SJaegeuk Kim #define F2FS_MAP_FLAGS		(F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
6487f63eb77SJaegeuk Kim 				F2FS_MAP_UNWRITTEN)
649003a3e1dSJaegeuk Kim 
650003a3e1dSJaegeuk Kim struct f2fs_map_blocks {
65171f2c820SChao Yu 	struct block_device *m_bdev;	/* for multi-device dio */
652003a3e1dSJaegeuk Kim 	block_t m_pblk;
653003a3e1dSJaegeuk Kim 	block_t m_lblk;
654003a3e1dSJaegeuk Kim 	unsigned int m_len;
655003a3e1dSJaegeuk Kim 	unsigned int m_flags;
656da85985cSChao Yu 	pgoff_t *m_next_pgofs;		/* point next possible non-hole pgofs */
657c4020b2dSChao Yu 	pgoff_t *m_next_extent;		/* point to next possible extent */
658d5097be5SHyunchul Lee 	int m_seg_type;
659f9d6d059SChao Yu 	bool m_may_create;		/* indicate it is from write path */
66071f2c820SChao Yu 	bool m_multidev_dio;		/* indicate it allows multi-device dio */
661003a3e1dSJaegeuk Kim };
662003a3e1dSJaegeuk Kim 
663e2b4e2bcSChao Yu /* for flag in get_data_block */
664f2220c7fSQiuyang Sun enum {
665f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_DEFAULT,
666f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_FIEMAP,
667f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_BMAP,
6680a4daae5SJaegeuk Kim 	F2FS_GET_BLOCK_DIO,
669f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_DIO,
670f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_AIO,
671c4020b2dSChao Yu 	F2FS_GET_BLOCK_PRECACHE,
672f2220c7fSQiuyang Sun };
673e2b4e2bcSChao Yu 
674003a3e1dSJaegeuk Kim /*
67539a53e0cSJaegeuk Kim  * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
67639a53e0cSJaegeuk Kim  */
67739a53e0cSJaegeuk Kim #define FADVISE_COLD_BIT	0x01
678354a3399SJaegeuk Kim #define FADVISE_LOST_PINO_BIT	0x02
679cde4de12SJaegeuk Kim #define FADVISE_ENCRYPT_BIT	0x04
680e7d55452SJaegeuk Kim #define FADVISE_ENC_NAME_BIT	0x08
68126787236SJaegeuk Kim #define FADVISE_KEEP_SIZE_BIT	0x10
682b6a06cbbSChao Yu #define FADVISE_HOT_BIT		0x20
68395ae251fSEric Biggers #define FADVISE_VERITY_BIT	0x40
684d4dd19ecSJaegeuk Kim #define FADVISE_TRUNC_BIT	0x80
68539a53e0cSJaegeuk Kim 
686797c1cb5SChao Yu #define FADVISE_MODIFIABLE_BITS	(FADVISE_COLD_BIT | FADVISE_HOT_BIT)
687797c1cb5SChao Yu 
688b5492af7SJaegeuk Kim #define file_is_cold(inode)	is_file(inode, FADVISE_COLD_BIT)
689b5492af7SJaegeuk Kim #define file_set_cold(inode)	set_file(inode, FADVISE_COLD_BIT)
690b5492af7SJaegeuk Kim #define file_clear_cold(inode)	clear_file(inode, FADVISE_COLD_BIT)
6911153db09SChao Yu 
6921153db09SChao Yu #define file_wrong_pino(inode)	is_file(inode, FADVISE_LOST_PINO_BIT)
6931153db09SChao Yu #define file_lost_pino(inode)	set_file(inode, FADVISE_LOST_PINO_BIT)
694b5492af7SJaegeuk Kim #define file_got_pino(inode)	clear_file(inode, FADVISE_LOST_PINO_BIT)
6951153db09SChao Yu 
696cde4de12SJaegeuk Kim #define file_is_encrypt(inode)	is_file(inode, FADVISE_ENCRYPT_BIT)
697cde4de12SJaegeuk Kim #define file_set_encrypt(inode)	set_file(inode, FADVISE_ENCRYPT_BIT)
6981153db09SChao Yu 
699e7d55452SJaegeuk Kim #define file_enc_name(inode)	is_file(inode, FADVISE_ENC_NAME_BIT)
700e7d55452SJaegeuk Kim #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
7011153db09SChao Yu 
70226787236SJaegeuk Kim #define file_keep_isize(inode)	is_file(inode, FADVISE_KEEP_SIZE_BIT)
70326787236SJaegeuk Kim #define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
7041153db09SChao Yu 
705b6a06cbbSChao Yu #define file_is_hot(inode)	is_file(inode, FADVISE_HOT_BIT)
706b6a06cbbSChao Yu #define file_set_hot(inode)	set_file(inode, FADVISE_HOT_BIT)
707b6a06cbbSChao Yu #define file_clear_hot(inode)	clear_file(inode, FADVISE_HOT_BIT)
7081153db09SChao Yu 
70995ae251fSEric Biggers #define file_is_verity(inode)	is_file(inode, FADVISE_VERITY_BIT)
71095ae251fSEric Biggers #define file_set_verity(inode)	set_file(inode, FADVISE_VERITY_BIT)
711cde4de12SJaegeuk Kim 
712d4dd19ecSJaegeuk Kim #define file_should_truncate(inode)	is_file(inode, FADVISE_TRUNC_BIT)
713d4dd19ecSJaegeuk Kim #define file_need_truncate(inode)	set_file(inode, FADVISE_TRUNC_BIT)
714d4dd19ecSJaegeuk Kim #define file_dont_truncate(inode)	clear_file(inode, FADVISE_TRUNC_BIT)
715d4dd19ecSJaegeuk Kim 
716ab9fa662SJaegeuk Kim #define DEF_DIR_LEVEL		0
717ab9fa662SJaegeuk Kim 
7182ef79ecbSChao Yu enum {
7192ef79ecbSChao Yu 	GC_FAILURE_PIN,
7202ef79ecbSChao Yu 	GC_FAILURE_ATOMIC,
7212ef79ecbSChao Yu 	MAX_GC_FAILURE
7222ef79ecbSChao Yu };
7232ef79ecbSChao Yu 
7247653b9d8SChao Yu /* used for f2fs_inode_info->flags */
7257653b9d8SChao Yu enum {
7267653b9d8SChao Yu 	FI_NEW_INODE,		/* indicate newly allocated inode */
7277653b9d8SChao Yu 	FI_DIRTY_INODE,		/* indicate inode is dirty or not */
7287653b9d8SChao Yu 	FI_AUTO_RECOVER,	/* indicate inode is recoverable */
7297653b9d8SChao Yu 	FI_DIRTY_DIR,		/* indicate directory has dirty pages */
7307653b9d8SChao Yu 	FI_INC_LINK,		/* need to increment i_nlink */
7317653b9d8SChao Yu 	FI_ACL_MODE,		/* indicate acl mode */
7327653b9d8SChao Yu 	FI_NO_ALLOC,		/* should not allocate any blocks */
7337653b9d8SChao Yu 	FI_FREE_NID,		/* free allocated nide */
7347653b9d8SChao Yu 	FI_NO_EXTENT,		/* not to use the extent cache */
7357653b9d8SChao Yu 	FI_INLINE_XATTR,	/* used for inline xattr */
7367653b9d8SChao Yu 	FI_INLINE_DATA,		/* used for inline data*/
7377653b9d8SChao Yu 	FI_INLINE_DENTRY,	/* used for inline dentry */
7387653b9d8SChao Yu 	FI_APPEND_WRITE,	/* inode has appended data */
7397653b9d8SChao Yu 	FI_UPDATE_WRITE,	/* inode has in-place-update data */
7407653b9d8SChao Yu 	FI_NEED_IPU,		/* used for ipu per file */
7417653b9d8SChao Yu 	FI_ATOMIC_FILE,		/* indicate atomic file */
7427653b9d8SChao Yu 	FI_ATOMIC_COMMIT,	/* indicate the state of atomical committing */
7437653b9d8SChao Yu 	FI_VOLATILE_FILE,	/* indicate volatile file */
7447653b9d8SChao Yu 	FI_FIRST_BLOCK_WRITTEN,	/* indicate #0 data block was written */
7457653b9d8SChao Yu 	FI_DROP_CACHE,		/* drop dirty page cache */
7467653b9d8SChao Yu 	FI_DATA_EXIST,		/* indicate data exists */
7477653b9d8SChao Yu 	FI_INLINE_DOTS,		/* indicate inline dot dentries */
7481018a546SChao Yu 	FI_SKIP_WRITES,		/* should skip data page writeback */
7491018a546SChao Yu 	FI_OPU_WRITE,		/* used for opu per file */
7507653b9d8SChao Yu 	FI_DIRTY_FILE,		/* indicate regular/symlink has dirty pages */
7513d697a4aSEric Biggers 	FI_PREALLOCATED_ALL,	/* all blocks for write were preallocated */
7527653b9d8SChao Yu 	FI_HOT_DATA,		/* indicate file is hot */
7537653b9d8SChao Yu 	FI_EXTRA_ATTR,		/* indicate file has extra attribute */
7547653b9d8SChao Yu 	FI_PROJ_INHERIT,	/* indicate file inherits projectid */
7557653b9d8SChao Yu 	FI_PIN_FILE,		/* indicate file should not be gced */
7567653b9d8SChao Yu 	FI_ATOMIC_REVOKE_REQUEST, /* request to drop atomic data */
7577653b9d8SChao Yu 	FI_VERITY_IN_PROGRESS,	/* building fs-verity Merkle tree */
7587653b9d8SChao Yu 	FI_COMPRESSED_FILE,	/* indicate file's data can be compressed */
759b28f047bSChao Yu 	FI_COMPRESS_CORRUPT,	/* indicate compressed cluster is corrupted */
7607653b9d8SChao Yu 	FI_MMAP_FILE,		/* indicate file was mmapped */
761602a16d5SDaeho Jeong 	FI_ENABLE_COMPRESS,	/* enable compression in "user" compression mode */
762c6140415SJaegeuk Kim 	FI_COMPRESS_RELEASED,	/* compressed blocks were released */
763859fca6bSChao Yu 	FI_ALIGNED_WRITE,	/* enable aligned write */
7647653b9d8SChao Yu 	FI_MAX,			/* max flag, never be used */
7657653b9d8SChao Yu };
7667653b9d8SChao Yu 
76739a53e0cSJaegeuk Kim struct f2fs_inode_info {
76839a53e0cSJaegeuk Kim 	struct inode vfs_inode;		/* serve a vfs inode */
76939a53e0cSJaegeuk Kim 	unsigned long i_flags;		/* keep an inode flags for ioctl */
77039a53e0cSJaegeuk Kim 	unsigned char i_advise;		/* use to give file attribute hints */
77138431545SJaegeuk Kim 	unsigned char i_dir_level;	/* use for dentry level for large dir */
7721ad71a27SJaegeuk Kim 	unsigned int i_current_depth;	/* only for directory depth */
7732ef79ecbSChao Yu 	/* for gc failure statistic */
7742ef79ecbSChao Yu 	unsigned int i_gc_failures[MAX_GC_FAILURE];
7756666e6aaSJaegeuk Kim 	unsigned int i_pino;		/* parent inode number */
77639a53e0cSJaegeuk Kim 	umode_t i_acl_mode;		/* keep file acl mode temporarily */
77739a53e0cSJaegeuk Kim 
77839a53e0cSJaegeuk Kim 	/* Use below internally in f2fs*/
7797653b9d8SChao Yu 	unsigned long flags[BITS_TO_LONGS(FI_MAX)];	/* use to pass per-file flags */
780e4544b63STim Murray 	struct f2fs_rwsem i_sem;	/* protect fi info */
781204706c7SJaegeuk Kim 	atomic_t dirty_pages;		/* # of dirty pages */
78239a53e0cSJaegeuk Kim 	f2fs_hash_t chash;		/* hash value of given file name */
78339a53e0cSJaegeuk Kim 	unsigned int clevel;		/* maximum level of given file name */
78488c5c13aSJaegeuk Kim 	struct task_struct *task;	/* lookup and create consistency */
785b0af6d49SChao Yu 	struct task_struct *cp_task;	/* separate cp/wb IO stats*/
78639a53e0cSJaegeuk Kim 	nid_t i_xattr_nid;		/* node id that contains xattrs */
78726de9b11SJaegeuk Kim 	loff_t	last_disk_size;		/* lastly written file size */
788c10c9820SChao Yu 	spinlock_t i_size_lock;		/* protect last_disk_size */
78988b88a66SJaegeuk Kim 
7900abd675eSChao Yu #ifdef CONFIG_QUOTA
7910abd675eSChao Yu 	struct dquot *i_dquot[MAXQUOTAS];
7920abd675eSChao Yu 
7930abd675eSChao Yu 	/* quota space reservation, managed internally by quota code */
7940abd675eSChao Yu 	qsize_t i_reserved_quota;
7950abd675eSChao Yu #endif
7960f18b462SJaegeuk Kim 	struct list_head dirty_list;	/* dirty list for dirs and files */
7970f18b462SJaegeuk Kim 	struct list_head gdirty_list;	/* linked in global dirty list */
79857864ae5SJaegeuk Kim 	struct list_head inmem_ilist;	/* list for inmem inodes */
79988b88a66SJaegeuk Kim 	struct list_head inmem_pages;	/* inmemory pages managed by f2fs */
8007a10f017SJaegeuk Kim 	struct task_struct *inmem_task;	/* store inmemory task */
80188b88a66SJaegeuk Kim 	struct mutex inmem_lock;	/* lock for inmemory pages */
8023e72f721SJaegeuk Kim 	struct extent_tree *extent_tree;	/* cached extent_tree entry */
803b2532c69SChao Yu 
804b2532c69SChao Yu 	/* avoid racing between foreground op and gc */
805e4544b63STim Murray 	struct f2fs_rwsem i_gc_rwsem[2];
806e4544b63STim Murray 	struct f2fs_rwsem i_xattr_sem; /* avoid racing between reading and changing EAs */
807f2470371SChao Yu 
8087a2af766SChao Yu 	int i_extra_isize;		/* size of extra space located in i_addr */
8095c57132eSChao Yu 	kprojid_t i_projid;		/* id for project quota */
8106afc662eSChao Yu 	int i_inline_xattr_size;	/* inline xattr size */
81124b81dfcSArnd Bergmann 	struct timespec64 i_crtime;	/* inode creation time */
81224b81dfcSArnd Bergmann 	struct timespec64 i_disk_time[4];/* inode disk times */
8134c8ff709SChao Yu 
8144c8ff709SChao Yu 	/* for file compress */
815c2759ebaSDaeho Jeong 	atomic_t i_compr_blocks;		/* # of compressed blocks */
8164c8ff709SChao Yu 	unsigned char i_compress_algorithm;	/* algorithm type */
8174c8ff709SChao Yu 	unsigned char i_log_cluster_size;	/* log of cluster size */
8183fde13f8SChao Yu 	unsigned char i_compress_level;		/* compress level (lz4hc,zstd) */
819b28f047bSChao Yu 	unsigned short i_compress_flag;		/* compress flag */
8204c8ff709SChao Yu 	unsigned int i_cluster_size;		/* cluster size */
82139a53e0cSJaegeuk Kim };
82239a53e0cSJaegeuk Kim 
82339a53e0cSJaegeuk Kim static inline void get_extent_info(struct extent_info *ext,
824bd933d4fSChao Yu 					struct f2fs_extent *i_ext)
82539a53e0cSJaegeuk Kim {
826bd933d4fSChao Yu 	ext->fofs = le32_to_cpu(i_ext->fofs);
827bd933d4fSChao Yu 	ext->blk = le32_to_cpu(i_ext->blk);
828bd933d4fSChao Yu 	ext->len = le32_to_cpu(i_ext->len);
82939a53e0cSJaegeuk Kim }
83039a53e0cSJaegeuk Kim 
83139a53e0cSJaegeuk Kim static inline void set_raw_extent(struct extent_info *ext,
83239a53e0cSJaegeuk Kim 					struct f2fs_extent *i_ext)
83339a53e0cSJaegeuk Kim {
83439a53e0cSJaegeuk Kim 	i_ext->fofs = cpu_to_le32(ext->fofs);
8354d0b0bd4SChao Yu 	i_ext->blk = cpu_to_le32(ext->blk);
83639a53e0cSJaegeuk Kim 	i_ext->len = cpu_to_le32(ext->len);
83739a53e0cSJaegeuk Kim }
83839a53e0cSJaegeuk Kim 
839429511cdSChao Yu static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
840429511cdSChao Yu 						u32 blk, unsigned int len)
841429511cdSChao Yu {
842429511cdSChao Yu 	ei->fofs = fofs;
843429511cdSChao Yu 	ei->blk = blk;
844429511cdSChao Yu 	ei->len = len;
84594afd6d6SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
84694afd6d6SChao Yu 	ei->c_len = 0;
84794afd6d6SChao Yu #endif
848429511cdSChao Yu }
849429511cdSChao Yu 
850004b6862SChao Yu static inline bool __is_discard_mergeable(struct discard_info *back,
85135ec7d57SChao Yu 			struct discard_info *front, unsigned int max_len)
852004b6862SChao Yu {
8539a997188SJaegeuk Kim 	return (back->lstart + back->len == front->lstart) &&
85435ec7d57SChao Yu 		(back->len + front->len <= max_len);
855004b6862SChao Yu }
856004b6862SChao Yu 
857004b6862SChao Yu static inline bool __is_discard_back_mergeable(struct discard_info *cur,
85835ec7d57SChao Yu 			struct discard_info *back, unsigned int max_len)
859004b6862SChao Yu {
86035ec7d57SChao Yu 	return __is_discard_mergeable(back, cur, max_len);
861004b6862SChao Yu }
862004b6862SChao Yu 
863004b6862SChao Yu static inline bool __is_discard_front_mergeable(struct discard_info *cur,
86435ec7d57SChao Yu 			struct discard_info *front, unsigned int max_len)
865004b6862SChao Yu {
86635ec7d57SChao Yu 	return __is_discard_mergeable(cur, front, max_len);
867004b6862SChao Yu }
868004b6862SChao Yu 
869429511cdSChao Yu static inline bool __is_extent_mergeable(struct extent_info *back,
870429511cdSChao Yu 						struct extent_info *front)
871429511cdSChao Yu {
87294afd6d6SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
87394afd6d6SChao Yu 	if (back->c_len && back->len != back->c_len)
87494afd6d6SChao Yu 		return false;
87594afd6d6SChao Yu 	if (front->c_len && front->len != front->c_len)
87694afd6d6SChao Yu 		return false;
87794afd6d6SChao Yu #endif
878429511cdSChao Yu 	return (back->fofs + back->len == front->fofs &&
879429511cdSChao Yu 			back->blk + back->len == front->blk);
880429511cdSChao Yu }
881429511cdSChao Yu 
882429511cdSChao Yu static inline bool __is_back_mergeable(struct extent_info *cur,
883429511cdSChao Yu 						struct extent_info *back)
884429511cdSChao Yu {
885429511cdSChao Yu 	return __is_extent_mergeable(back, cur);
886429511cdSChao Yu }
887429511cdSChao Yu 
888429511cdSChao Yu static inline bool __is_front_mergeable(struct extent_info *cur,
889429511cdSChao Yu 						struct extent_info *front)
890429511cdSChao Yu {
891429511cdSChao Yu 	return __is_extent_mergeable(cur, front);
892429511cdSChao Yu }
893429511cdSChao Yu 
894cac5a3d8SDongOh Shin extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
895b430f726SZhikang Zhang static inline void __try_update_largest_extent(struct extent_tree *et,
896b430f726SZhikang Zhang 						struct extent_node *en)
8974abd3f5aSChao Yu {
898205b9822SJaegeuk Kim 	if (en->ei.len > et->largest.len) {
8994abd3f5aSChao Yu 		et->largest = en->ei;
900b430f726SZhikang Zhang 		et->largest_updated = true;
901205b9822SJaegeuk Kim 	}
9024abd3f5aSChao Yu }
9034abd3f5aSChao Yu 
9049a4ffdf5SChao Yu /*
9059a4ffdf5SChao Yu  * For free nid management
9069a4ffdf5SChao Yu  */
9079a4ffdf5SChao Yu enum nid_state {
9089a4ffdf5SChao Yu 	FREE_NID,		/* newly added to free nid list */
9099a4ffdf5SChao Yu 	PREALLOC_NID,		/* it is preallocated */
9109a4ffdf5SChao Yu 	MAX_NID_STATE,
911b8559dc2SChao Yu };
912b8559dc2SChao Yu 
913a95ba66aSJaegeuk Kim enum nat_state {
914a95ba66aSJaegeuk Kim 	TOTAL_NAT,
915a95ba66aSJaegeuk Kim 	DIRTY_NAT,
916a95ba66aSJaegeuk Kim 	RECLAIMABLE_NAT,
917a95ba66aSJaegeuk Kim 	MAX_NAT_STATE,
918a95ba66aSJaegeuk Kim };
919a95ba66aSJaegeuk Kim 
92039a53e0cSJaegeuk Kim struct f2fs_nm_info {
92139a53e0cSJaegeuk Kim 	block_t nat_blkaddr;		/* base disk address of NAT */
92239a53e0cSJaegeuk Kim 	nid_t max_nid;			/* maximum possible node ids */
92304d47e67SChao Yu 	nid_t available_nids;		/* # of available node ids */
92439a53e0cSJaegeuk Kim 	nid_t next_scan_nid;		/* the next nid to be scanned */
92547c8ebccSJaegeuk Kim 	nid_t max_rf_node_blocks;	/* max # of nodes for recovery */
926cdfc41c1SJaegeuk Kim 	unsigned int ram_thresh;	/* control the memory footprint */
927ea1a29a0SChao Yu 	unsigned int ra_nid_pages;	/* # of nid pages to be readaheaded */
9282304cb0cSChao Yu 	unsigned int dirty_nats_ratio;	/* control dirty nats ratio threshold */
92939a53e0cSJaegeuk Kim 
93039a53e0cSJaegeuk Kim 	/* NAT cache management */
93139a53e0cSJaegeuk Kim 	struct radix_tree_root nat_root;/* root of the nat entry cache */
932309cc2b6SJaegeuk Kim 	struct radix_tree_root nat_set_root;/* root of the nat set cache */
933e4544b63STim Murray 	struct f2fs_rwsem nat_tree_lock;	/* protect nat entry tree */
93439a53e0cSJaegeuk Kim 	struct list_head nat_entries;	/* cached nat entry list (clean) */
93522969158SChao Yu 	spinlock_t nat_list_lock;	/* protect clean nat entry list */
936a95ba66aSJaegeuk Kim 	unsigned int nat_cnt[MAX_NAT_STATE]; /* the # of cached nat entries */
93722ad0b6aSJaegeuk Kim 	unsigned int nat_blocks;	/* # of nat blocks */
93839a53e0cSJaegeuk Kim 
93939a53e0cSJaegeuk Kim 	/* free node ids management */
9408a7ed66aSJaegeuk Kim 	struct radix_tree_root free_nid_root;/* root of the free_nid cache */
9419a4ffdf5SChao Yu 	struct list_head free_nid_list;		/* list for free nids excluding preallocated nids */
9429a4ffdf5SChao Yu 	unsigned int nid_cnt[MAX_NID_STATE];	/* the number of free node id */
943b8559dc2SChao Yu 	spinlock_t nid_list_lock;	/* protect nid lists ops */
94439a53e0cSJaegeuk Kim 	struct mutex build_lock;	/* lock for build free nids */
945bb1105e4SJaegeuk Kim 	unsigned char **free_nid_bitmap;
9464ac91242SChao Yu 	unsigned char *nat_block_bitmap;
947586d1492SChao Yu 	unsigned short *free_nid_count;	/* free nid count of NAT block */
94839a53e0cSJaegeuk Kim 
94939a53e0cSJaegeuk Kim 	/* for checkpoint */
95039a53e0cSJaegeuk Kim 	char *nat_bitmap;		/* NAT bitmap pointer */
95122ad0b6aSJaegeuk Kim 
95222ad0b6aSJaegeuk Kim 	unsigned int nat_bits_blocks;	/* # of nat bits blocks */
95322ad0b6aSJaegeuk Kim 	unsigned char *nat_bits;	/* NAT bits blocks */
95422ad0b6aSJaegeuk Kim 	unsigned char *full_nat_bits;	/* full NAT pages */
95522ad0b6aSJaegeuk Kim 	unsigned char *empty_nat_bits;	/* empty NAT pages */
956599a09b2SChao Yu #ifdef CONFIG_F2FS_CHECK_FS
957599a09b2SChao Yu 	char *nat_bitmap_mir;		/* NAT bitmap mirror */
958599a09b2SChao Yu #endif
95939a53e0cSJaegeuk Kim 	int bitmap_size;		/* bitmap size */
96039a53e0cSJaegeuk Kim };
96139a53e0cSJaegeuk Kim 
96239a53e0cSJaegeuk Kim /*
96339a53e0cSJaegeuk Kim  * this structure is used as one of function parameters.
96439a53e0cSJaegeuk Kim  * all the information are dedicated to a given direct node block determined
96539a53e0cSJaegeuk Kim  * by the data offset in a file.
96639a53e0cSJaegeuk Kim  */
96739a53e0cSJaegeuk Kim struct dnode_of_data {
96839a53e0cSJaegeuk Kim 	struct inode *inode;		/* vfs inode pointer */
96939a53e0cSJaegeuk Kim 	struct page *inode_page;	/* its inode page, NULL is possible */
97039a53e0cSJaegeuk Kim 	struct page *node_page;		/* cached direct node page */
97139a53e0cSJaegeuk Kim 	nid_t nid;			/* node id of the direct node block */
97239a53e0cSJaegeuk Kim 	unsigned int ofs_in_node;	/* data offset in the node page */
97339a53e0cSJaegeuk Kim 	bool inode_page_locked;		/* inode page is locked or not */
97493bae099SJaegeuk Kim 	bool node_changed;		/* is node block changed */
9753cf45747SChao Yu 	char cur_level;			/* level of hole node page */
9763cf45747SChao Yu 	char max_level;			/* level of current page located */
97739a53e0cSJaegeuk Kim 	block_t	data_blkaddr;		/* block address of the node block */
97839a53e0cSJaegeuk Kim };
97939a53e0cSJaegeuk Kim 
98039a53e0cSJaegeuk Kim static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
98139a53e0cSJaegeuk Kim 		struct page *ipage, struct page *npage, nid_t nid)
98239a53e0cSJaegeuk Kim {
983d66d1f76SJaegeuk Kim 	memset(dn, 0, sizeof(*dn));
98439a53e0cSJaegeuk Kim 	dn->inode = inode;
98539a53e0cSJaegeuk Kim 	dn->inode_page = ipage;
98639a53e0cSJaegeuk Kim 	dn->node_page = npage;
98739a53e0cSJaegeuk Kim 	dn->nid = nid;
98839a53e0cSJaegeuk Kim }
98939a53e0cSJaegeuk Kim 
99039a53e0cSJaegeuk Kim /*
99139a53e0cSJaegeuk Kim  * For SIT manager
99239a53e0cSJaegeuk Kim  *
99339a53e0cSJaegeuk Kim  * By default, there are 6 active log areas across the whole main area.
99439a53e0cSJaegeuk Kim  * When considering hot and cold data separation to reduce cleaning overhead,
99539a53e0cSJaegeuk Kim  * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
99639a53e0cSJaegeuk Kim  * respectively.
99739a53e0cSJaegeuk Kim  * In the current design, you should not change the numbers intentionally.
99839a53e0cSJaegeuk Kim  * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
99939a53e0cSJaegeuk Kim  * logs individually according to the underlying devices. (default: 6)
100039a53e0cSJaegeuk Kim  * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
100139a53e0cSJaegeuk Kim  * data and 8 for node logs.
100239a53e0cSJaegeuk Kim  */
100339a53e0cSJaegeuk Kim #define	NR_CURSEG_DATA_TYPE	(3)
100439a53e0cSJaegeuk Kim #define NR_CURSEG_NODE_TYPE	(3)
1005093749e2SChao Yu #define NR_CURSEG_INMEM_TYPE	(2)
1006a7d9fe3cSJaegeuk Kim #define NR_CURSEG_RO_TYPE	(2)
1007d0b9e42aSChao Yu #define NR_CURSEG_PERSIST_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
1008d0b9e42aSChao Yu #define NR_CURSEG_TYPE		(NR_CURSEG_INMEM_TYPE + NR_CURSEG_PERSIST_TYPE)
100939a53e0cSJaegeuk Kim 
101039a53e0cSJaegeuk Kim enum {
101139a53e0cSJaegeuk Kim 	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
101239a53e0cSJaegeuk Kim 	CURSEG_WARM_DATA,	/* data blocks */
101339a53e0cSJaegeuk Kim 	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
101439a53e0cSJaegeuk Kim 	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
101539a53e0cSJaegeuk Kim 	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
101639a53e0cSJaegeuk Kim 	CURSEG_COLD_NODE,	/* indirect node blocks */
1017d0b9e42aSChao Yu 	NR_PERSISTENT_LOG,	/* number of persistent log */
1018d0b9e42aSChao Yu 	CURSEG_COLD_DATA_PINNED = NR_PERSISTENT_LOG,
1019d0b9e42aSChao Yu 				/* pinned file that needs consecutive block address */
1020093749e2SChao Yu 	CURSEG_ALL_DATA_ATGC,	/* SSR alloctor in hot/warm/cold data area */
1021d0b9e42aSChao Yu 	NO_CHECK_TYPE,		/* number of persistent & inmem log */
102239a53e0cSJaegeuk Kim };
102339a53e0cSJaegeuk Kim 
10246b4afdd7SJaegeuk Kim struct flush_cmd {
10256b4afdd7SJaegeuk Kim 	struct completion wait;
1026721bd4d5SGu Zheng 	struct llist_node llnode;
102739d787beSChao Yu 	nid_t ino;
10286b4afdd7SJaegeuk Kim 	int ret;
10296b4afdd7SJaegeuk Kim };
10306b4afdd7SJaegeuk Kim 
1031a688b9d9SGu Zheng struct flush_cmd_control {
1032a688b9d9SGu Zheng 	struct task_struct *f2fs_issue_flush;	/* flush thread */
1033a688b9d9SGu Zheng 	wait_queue_head_t flush_wait_queue;	/* waiting queue for wake-up */
10348b8dd65fSChao Yu 	atomic_t issued_flush;			/* # of issued flushes */
103572691af6SJaegeuk Kim 	atomic_t queued_flush;			/* # of queued flushes */
1036721bd4d5SGu Zheng 	struct llist_head issue_list;		/* list for command issue */
1037721bd4d5SGu Zheng 	struct llist_node *dispatch_list;	/* list for command dispatch */
1038a688b9d9SGu Zheng };
1039a688b9d9SGu Zheng 
104039a53e0cSJaegeuk Kim struct f2fs_sm_info {
104139a53e0cSJaegeuk Kim 	struct sit_info *sit_info;		/* whole segment information */
104239a53e0cSJaegeuk Kim 	struct free_segmap_info *free_info;	/* free segment information */
104339a53e0cSJaegeuk Kim 	struct dirty_seglist_info *dirty_info;	/* dirty segment information */
104439a53e0cSJaegeuk Kim 	struct curseg_info *curseg_array;	/* active segment information */
104539a53e0cSJaegeuk Kim 
1046e4544b63STim Murray 	struct f2fs_rwsem curseg_lock;	/* for preventing curseg change */
10472b60311dSChao Yu 
104839a53e0cSJaegeuk Kim 	block_t seg0_blkaddr;		/* block address of 0'th segment */
104939a53e0cSJaegeuk Kim 	block_t main_blkaddr;		/* start block address of main area */
105039a53e0cSJaegeuk Kim 	block_t ssa_blkaddr;		/* start block address of SSA area */
105139a53e0cSJaegeuk Kim 
105239a53e0cSJaegeuk Kim 	unsigned int segment_count;	/* total # of segments */
105339a53e0cSJaegeuk Kim 	unsigned int main_segments;	/* # of segments in main area */
105439a53e0cSJaegeuk Kim 	unsigned int reserved_segments;	/* # of reserved segments */
1055300a8429SChao Yu 	unsigned int additional_reserved_segments;/* reserved segs for IO align feature */
105639a53e0cSJaegeuk Kim 	unsigned int ovp_segments;	/* # of overprovision segments */
105781eb8d6eSJaegeuk Kim 
105881eb8d6eSJaegeuk Kim 	/* a threshold to reclaim prefree segments */
105981eb8d6eSJaegeuk Kim 	unsigned int rec_prefree_segments;
10607fd9e544SJaegeuk Kim 
1061bba681cbSJaegeuk Kim 	/* for batched trimming */
1062bba681cbSJaegeuk Kim 	unsigned int trim_sections;		/* # of sections to trim */
1063bba681cbSJaegeuk Kim 
1064184a5cd2SChao Yu 	struct list_head sit_entry_set;	/* sit entry set list */
1065184a5cd2SChao Yu 
1066216fbd64SJaegeuk Kim 	unsigned int ipu_policy;	/* in-place-update policy */
1067216fbd64SJaegeuk Kim 	unsigned int min_ipu_util;	/* in-place-update threshold */
1068c1ce1b02SJaegeuk Kim 	unsigned int min_fsync_blocks;	/* threshold for fsync */
1069853137ceSJaegeuk Kim 	unsigned int min_seq_blocks;	/* threshold for sequential blocks */
1070ef095d19SJaegeuk Kim 	unsigned int min_hot_blocks;	/* threshold for hot block allocation */
1071a2a12b67SChao Yu 	unsigned int min_ssr_sections;	/* threshold to trigger SSR allocation */
10726b4afdd7SJaegeuk Kim 
10736b4afdd7SJaegeuk Kim 	/* for flush command control */
1074b01a9201SJaegeuk Kim 	struct flush_cmd_control *fcc_info;
1075a688b9d9SGu Zheng 
10760b54fb84SJaegeuk Kim 	/* for discard command control */
10770b54fb84SJaegeuk Kim 	struct discard_cmd_control *dcc_info;
107839a53e0cSJaegeuk Kim };
107939a53e0cSJaegeuk Kim 
108039a53e0cSJaegeuk Kim /*
108139a53e0cSJaegeuk Kim  * For superblock
108239a53e0cSJaegeuk Kim  */
108339a53e0cSJaegeuk Kim /*
108439a53e0cSJaegeuk Kim  * COUNT_TYPE for monitoring
108539a53e0cSJaegeuk Kim  *
108639a53e0cSJaegeuk Kim  * f2fs monitors the number of several block types such as on-writeback,
108739a53e0cSJaegeuk Kim  * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
108839a53e0cSJaegeuk Kim  */
108936951b38SChao Yu #define WB_DATA_TYPE(p)	(__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
109039a53e0cSJaegeuk Kim enum count_type {
109139a53e0cSJaegeuk Kim 	F2FS_DIRTY_DENTS,
1092c227f912SChao Yu 	F2FS_DIRTY_DATA,
10932c8a4a28SJaegeuk Kim 	F2FS_DIRTY_QDATA,
109439a53e0cSJaegeuk Kim 	F2FS_DIRTY_NODES,
109539a53e0cSJaegeuk Kim 	F2FS_DIRTY_META,
10968dcf2ff7SJaegeuk Kim 	F2FS_INMEM_PAGES,
10970f18b462SJaegeuk Kim 	F2FS_DIRTY_IMETA,
109836951b38SChao Yu 	F2FS_WB_CP_DATA,
109936951b38SChao Yu 	F2FS_WB_DATA,
11005f9abab4SJaegeuk Kim 	F2FS_RD_DATA,
11015f9abab4SJaegeuk Kim 	F2FS_RD_NODE,
11025f9abab4SJaegeuk Kim 	F2FS_RD_META,
110302b16d0aSChao Yu 	F2FS_DIO_WRITE,
110402b16d0aSChao Yu 	F2FS_DIO_READ,
110539a53e0cSJaegeuk Kim 	NR_COUNT_TYPE,
110639a53e0cSJaegeuk Kim };
110739a53e0cSJaegeuk Kim 
110839a53e0cSJaegeuk Kim /*
1109e1c42045Sarter97  * The below are the page types of bios used in submit_bio().
111039a53e0cSJaegeuk Kim  * The available types are:
111139a53e0cSJaegeuk Kim  * DATA			User data pages. It operates as async mode.
111239a53e0cSJaegeuk Kim  * NODE			Node pages. It operates as async mode.
111339a53e0cSJaegeuk Kim  * META			FS metadata pages such as SIT, NAT, CP.
111439a53e0cSJaegeuk Kim  * NR_PAGE_TYPE		The number of page types.
111539a53e0cSJaegeuk Kim  * META_FLUSH		Make sure the previous pages are written
111639a53e0cSJaegeuk Kim  *			with waiting the bio's completion
111739a53e0cSJaegeuk Kim  * ...			Only can be used with META.
111839a53e0cSJaegeuk Kim  */
11197d5e5109SJaegeuk Kim #define PAGE_TYPE_OF_BIO(type)	((type) > META ? META : (type))
112039a53e0cSJaegeuk Kim enum page_type {
112139a53e0cSJaegeuk Kim 	DATA,
112239a53e0cSJaegeuk Kim 	NODE,
112339a53e0cSJaegeuk Kim 	META,
112439a53e0cSJaegeuk Kim 	NR_PAGE_TYPE,
112539a53e0cSJaegeuk Kim 	META_FLUSH,
11268ce67cb0SJaegeuk Kim 	INMEM,		/* the below types are used by tracepoints only. */
11278ce67cb0SJaegeuk Kim 	INMEM_DROP,
11288c242db9SJaegeuk Kim 	INMEM_INVALIDATE,
112928bc106bSChao Yu 	INMEM_REVOKE,
11308ce67cb0SJaegeuk Kim 	IPU,
11318ce67cb0SJaegeuk Kim 	OPU,
113239a53e0cSJaegeuk Kim };
113339a53e0cSJaegeuk Kim 
1134a912b54dSJaegeuk Kim enum temp_type {
1135a912b54dSJaegeuk Kim 	HOT = 0,	/* must be zero for meta bio */
1136a912b54dSJaegeuk Kim 	WARM,
1137a912b54dSJaegeuk Kim 	COLD,
1138a912b54dSJaegeuk Kim 	NR_TEMP_TYPE,
1139a912b54dSJaegeuk Kim };
1140a912b54dSJaegeuk Kim 
1141cc15620bSJaegeuk Kim enum need_lock_type {
1142cc15620bSJaegeuk Kim 	LOCK_REQ = 0,
1143cc15620bSJaegeuk Kim 	LOCK_DONE,
1144cc15620bSJaegeuk Kim 	LOCK_RETRY,
1145cc15620bSJaegeuk Kim };
1146cc15620bSJaegeuk Kim 
1147a5fd5050SChao Yu enum cp_reason_type {
1148a5fd5050SChao Yu 	CP_NO_NEEDED,
1149a5fd5050SChao Yu 	CP_NON_REGULAR,
11504c8ff709SChao Yu 	CP_COMPRESSED,
1151a5fd5050SChao Yu 	CP_HARDLINK,
1152a5fd5050SChao Yu 	CP_SB_NEED_CP,
1153a5fd5050SChao Yu 	CP_WRONG_PINO,
1154a5fd5050SChao Yu 	CP_NO_SPC_ROLL,
1155a5fd5050SChao Yu 	CP_NODE_NEED_CP,
1156a5fd5050SChao Yu 	CP_FASTBOOT_MODE,
1157a5fd5050SChao Yu 	CP_SPEC_LOG_NUM,
11580a007b97SJaegeuk Kim 	CP_RECOVER_DIR,
1159a5fd5050SChao Yu };
1160a5fd5050SChao Yu 
1161b0af6d49SChao Yu enum iostat_type {
11628b83ac81SChao Yu 	/* WRITE IO */
11638b83ac81SChao Yu 	APP_DIRECT_IO,			/* app direct write IOs */
11648b83ac81SChao Yu 	APP_BUFFERED_IO,		/* app buffered write IOs */
1165b0af6d49SChao Yu 	APP_WRITE_IO,			/* app write IOs */
1166b0af6d49SChao Yu 	APP_MAPPED_IO,			/* app mapped IOs */
1167b0af6d49SChao Yu 	FS_DATA_IO,			/* data IOs from kworker/fsync/reclaimer */
1168b0af6d49SChao Yu 	FS_NODE_IO,			/* node IOs from kworker/fsync/reclaimer */
1169b0af6d49SChao Yu 	FS_META_IO,			/* meta IOs from kworker/reclaimer */
1170b0af6d49SChao Yu 	FS_GC_DATA_IO,			/* data IOs from forground gc */
1171b0af6d49SChao Yu 	FS_GC_NODE_IO,			/* node IOs from forground gc */
1172b0af6d49SChao Yu 	FS_CP_DATA_IO,			/* data IOs from checkpoint */
1173b0af6d49SChao Yu 	FS_CP_NODE_IO,			/* node IOs from checkpoint */
1174b0af6d49SChao Yu 	FS_CP_META_IO,			/* meta IOs from checkpoint */
11758b83ac81SChao Yu 
11768b83ac81SChao Yu 	/* READ IO */
11778b83ac81SChao Yu 	APP_DIRECT_READ_IO,		/* app direct read IOs */
11788b83ac81SChao Yu 	APP_BUFFERED_READ_IO,		/* app buffered read IOs */
11798b83ac81SChao Yu 	APP_READ_IO,			/* app read IOs */
11808b83ac81SChao Yu 	APP_MAPPED_READ_IO,		/* app mapped read IOs */
11818b83ac81SChao Yu 	FS_DATA_READ_IO,		/* data read IOs */
11829c122384SChao Yu 	FS_GDATA_READ_IO,		/* data read IOs from background gc */
11839c122384SChao Yu 	FS_CDATA_READ_IO,		/* compressed data read IOs */
11848b83ac81SChao Yu 	FS_NODE_READ_IO,		/* node read IOs */
11858b83ac81SChao Yu 	FS_META_READ_IO,		/* meta read IOs */
11868b83ac81SChao Yu 
11878b83ac81SChao Yu 	/* other */
1188b0af6d49SChao Yu 	FS_DISCARD,			/* discard */
1189b0af6d49SChao Yu 	NR_IO_TYPE,
1190b0af6d49SChao Yu };
1191b0af6d49SChao Yu 
1192458e6197SJaegeuk Kim struct f2fs_io_info {
119305ca3632SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs_sb_info pointer */
119439d787beSChao Yu 	nid_t ino;		/* inode number */
1195458e6197SJaegeuk Kim 	enum page_type type;	/* contains DATA/NODE/META/META_FLUSH */
1196a912b54dSJaegeuk Kim 	enum temp_type temp;	/* contains HOT/WARM/COLD */
119704d328deSMike Christie 	int op;			/* contains REQ_OP_ */
1198ef295ecfSChristoph Hellwig 	int op_flags;		/* req_flag_bits */
11997a9d7548SChao Yu 	block_t new_blkaddr;	/* new block address to be written */
120028bc106bSChao Yu 	block_t old_blkaddr;	/* old block address before Cow */
120105ca3632SJaegeuk Kim 	struct page *page;	/* page to be written */
12024375a336SJaegeuk Kim 	struct page *encrypted_page;	/* encrypted page */
12034c8ff709SChao Yu 	struct page *compressed_page;	/* compressed page */
1204fb830fc5SChao Yu 	struct list_head list;		/* serialize IOs */
1205d68f735bSJaegeuk Kim 	bool submitted;		/* indicate IO submission */
1206cc15620bSJaegeuk Kim 	int need_lock;		/* indicate we need to lock cp_rwsem */
1207fb830fc5SChao Yu 	bool in_list;		/* indicate fio is in io_list */
12086dc3a126SChao Yu 	bool is_por;		/* indicate IO is from recovery or not */
1209fe16efe6SChao Yu 	bool retry;		/* need to reallocate block address */
12104c8ff709SChao Yu 	int compr_blocks;	/* # of compressed block addresses */
12114c8ff709SChao Yu 	bool encrypted;		/* indicate file is encrypted */
1212b0af6d49SChao Yu 	enum iostat_type io_type;	/* io type */
1213578c6478SYufen Yu 	struct writeback_control *io_wbc; /* writeback control */
12148648de2cSChao Yu 	struct bio **bio;		/* bio for ipu */
12158648de2cSChao Yu 	sector_t *last_block;		/* last block number in bio */
12167735730dSChao Yu 	unsigned char version;		/* version of the node */
1217458e6197SJaegeuk Kim };
1218458e6197SJaegeuk Kim 
12190b20fcecSChao Yu struct bio_entry {
12200b20fcecSChao Yu 	struct bio *bio;
12210b20fcecSChao Yu 	struct list_head list;
12220b20fcecSChao Yu };
12230b20fcecSChao Yu 
122468afcf2dSTomohiro Kusumi #define is_read_io(rw) ((rw) == READ)
12251ff7bd3bSJaegeuk Kim struct f2fs_bio_info {
1226458e6197SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs superblock */
12271ff7bd3bSJaegeuk Kim 	struct bio *bio;		/* bios to merge */
12281ff7bd3bSJaegeuk Kim 	sector_t last_block_in_bio;	/* last block number */
1229458e6197SJaegeuk Kim 	struct f2fs_io_info fio;	/* store buffered io info. */
1230e4544b63STim Murray 	struct f2fs_rwsem io_rwsem;	/* blocking op for bio */
1231fb830fc5SChao Yu 	spinlock_t io_lock;		/* serialize DATA/NODE IOs */
1232fb830fc5SChao Yu 	struct list_head io_list;	/* track fios */
12330b20fcecSChao Yu 	struct list_head bio_list;	/* bio entry list head */
1234e4544b63STim Murray 	struct f2fs_rwsem bio_list_lock;	/* lock to protect bio entry list */
12351ff7bd3bSJaegeuk Kim };
12361ff7bd3bSJaegeuk Kim 
12373c62be17SJaegeuk Kim #define FDEV(i)				(sbi->devs[i])
12383c62be17SJaegeuk Kim #define RDEV(i)				(raw_super->devs[i])
12393c62be17SJaegeuk Kim struct f2fs_dev_info {
12403c62be17SJaegeuk Kim 	struct block_device *bdev;
12413c62be17SJaegeuk Kim 	char path[MAX_PATH_LEN];
12423c62be17SJaegeuk Kim 	unsigned int total_segments;
12433c62be17SJaegeuk Kim 	block_t start_blk;
12443c62be17SJaegeuk Kim 	block_t end_blk;
12453c62be17SJaegeuk Kim #ifdef CONFIG_BLK_DEV_ZONED
12463c62be17SJaegeuk Kim 	unsigned int nr_blkz;		/* Total number of zones */
124795175dafSDamien Le Moal 	unsigned long *blkz_seq;	/* Bitmap indicating sequential zones */
1248de881df9SAravind Ramesh 	block_t *zone_capacity_blocks;  /* Array of zone capacity in blks */
12493c62be17SJaegeuk Kim #endif
12503c62be17SJaegeuk Kim };
12513c62be17SJaegeuk Kim 
1252c227f912SChao Yu enum inode_type {
1253c227f912SChao Yu 	DIR_INODE,			/* for dirty dir inode */
1254c227f912SChao Yu 	FILE_INODE,			/* for dirty regular/symlink inode */
12550f18b462SJaegeuk Kim 	DIRTY_META,			/* for all dirtied inode metadata */
125657864ae5SJaegeuk Kim 	ATOMIC_FILE,			/* for all atomic files */
1257c227f912SChao Yu 	NR_INODE_TYPE,
1258c227f912SChao Yu };
1259c227f912SChao Yu 
126067298804SChao Yu /* for inner inode cache management */
126167298804SChao Yu struct inode_management {
126267298804SChao Yu 	struct radix_tree_root ino_root;	/* ino entry array */
126367298804SChao Yu 	spinlock_t ino_lock;			/* for ino entry lock */
126467298804SChao Yu 	struct list_head ino_list;		/* inode list head */
126567298804SChao Yu 	unsigned long ino_num;			/* number of entries */
126667298804SChao Yu };
126767298804SChao Yu 
1268093749e2SChao Yu /* for GC_AT */
1269093749e2SChao Yu struct atgc_management {
1270093749e2SChao Yu 	bool atgc_enabled;			/* ATGC is enabled or not */
1271093749e2SChao Yu 	struct rb_root_cached root;		/* root of victim rb-tree */
1272093749e2SChao Yu 	struct list_head victim_list;		/* linked with all victim entries */
1273093749e2SChao Yu 	unsigned int victim_count;		/* victim count in rb-tree */
1274093749e2SChao Yu 	unsigned int candidate_ratio;		/* candidate ratio */
1275093749e2SChao Yu 	unsigned int max_candidate_count;	/* max candidate count */
1276093749e2SChao Yu 	unsigned int age_weight;		/* age weight, vblock_weight = 100 - age_weight */
1277093749e2SChao Yu 	unsigned long long age_threshold;	/* age threshold */
1278093749e2SChao Yu };
1279093749e2SChao Yu 
1280caf0047eSChao Yu /* For s_flag in struct f2fs_sb_info */
1281caf0047eSChao Yu enum {
1282caf0047eSChao Yu 	SBI_IS_DIRTY,				/* dirty flag for checkpoint */
1283caf0047eSChao Yu 	SBI_IS_CLOSE,				/* specify unmounting */
1284caf0047eSChao Yu 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
1285caf0047eSChao Yu 	SBI_POR_DOING,				/* recovery is doing or not */
1286df728b0fSJaegeuk Kim 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
1287bbf156f7SJaegeuk Kim 	SBI_NEED_CP,				/* need to checkpoint */
128883a3bfdbSJaegeuk Kim 	SBI_IS_SHUTDOWN,			/* shutdown by ioctl */
12891378752bSChao Yu 	SBI_IS_RECOVERED,			/* recovered orphan/data */
12904354994fSDaniel Rosenberg 	SBI_CP_DISABLED,			/* CP was disabled last mount */
1291db610a64SJaegeuk Kim 	SBI_CP_DISABLED_QUICK,			/* CP was disabled quickly */
1292af033b2aSChao Yu 	SBI_QUOTA_NEED_FLUSH,			/* need to flush quota info in CP */
1293af033b2aSChao Yu 	SBI_QUOTA_SKIP_FLUSH,			/* skip flushing quota in current CP */
1294af033b2aSChao Yu 	SBI_QUOTA_NEED_REPAIR,			/* quota file may be corrupted */
129504f0b2eaSQiuyang Sun 	SBI_IS_RESIZEFS,			/* resizefs is in process */
1296ba900534SJaegeuk Kim 	SBI_IS_FREEZING,			/* freezefs is in process */
1297caf0047eSChao Yu };
1298caf0047eSChao Yu 
12996beceb54SJaegeuk Kim enum {
13006beceb54SJaegeuk Kim 	CP_TIME,
1301d0239e1bSJaegeuk Kim 	REQ_TIME,
1302a7d10cf3SSahitya Tummala 	DISCARD_TIME,
1303a7d10cf3SSahitya Tummala 	GC_TIME,
13044354994fSDaniel Rosenberg 	DISABLE_TIME,
130503f2c02dSJaegeuk Kim 	UMOUNT_DISCARD_TIMEOUT,
13066beceb54SJaegeuk Kim 	MAX_TIME,
13076beceb54SJaegeuk Kim };
13086beceb54SJaegeuk Kim 
13090cdd3195SHyunchul Lee enum {
13105b0e9539SJaegeuk Kim 	GC_NORMAL,
13115b0e9539SJaegeuk Kim 	GC_IDLE_CB,
13125b0e9539SJaegeuk Kim 	GC_IDLE_GREEDY,
1313093749e2SChao Yu 	GC_IDLE_AT,
13140e5e8111SDaeho Jeong 	GC_URGENT_HIGH,
13150e5e8111SDaeho Jeong 	GC_URGENT_LOW,
1316*d98af5f4SDaeho Jeong 	GC_URGENT_MID,
131707c6b593SDaeho Jeong 	MAX_GC_MODE,
13185b0e9539SJaegeuk Kim };
13195b0e9539SJaegeuk Kim 
13205b0e9539SJaegeuk Kim enum {
1321bbbc34fdSChao Yu 	BGGC_MODE_ON,		/* background gc is on */
1322bbbc34fdSChao Yu 	BGGC_MODE_OFF,		/* background gc is off */
1323bbbc34fdSChao Yu 	BGGC_MODE_SYNC,		/*
1324bbbc34fdSChao Yu 				 * background gc is on, migrating blocks
1325bbbc34fdSChao Yu 				 * like foreground gc
1326bbbc34fdSChao Yu 				 */
1327bbbc34fdSChao Yu };
1328bbbc34fdSChao Yu 
1329bbbc34fdSChao Yu enum {
1330b0332a0fSChao Yu 	FS_MODE_ADAPTIVE,		/* use both lfs/ssr allocation */
1331b0332a0fSChao Yu 	FS_MODE_LFS,			/* use lfs allocation only */
13326691d940SDaeho Jeong 	FS_MODE_FRAGMENT_SEG,		/* segment fragmentation mode */
13336691d940SDaeho Jeong 	FS_MODE_FRAGMENT_BLK,		/* block fragmentation mode */
1334b0332a0fSChao Yu };
1335b0332a0fSChao Yu 
1336b0332a0fSChao Yu enum {
13370cdd3195SHyunchul Lee 	WHINT_MODE_OFF,		/* not pass down write hints */
13380cdd3195SHyunchul Lee 	WHINT_MODE_USER,	/* try to pass down hints given by users */
1339f2e703f9SHyunchul Lee 	WHINT_MODE_FS,		/* pass down hints with F2FS policy */
13400cdd3195SHyunchul Lee };
13410cdd3195SHyunchul Lee 
134207939627SJaegeuk Kim enum {
134307939627SJaegeuk Kim 	ALLOC_MODE_DEFAULT,	/* stay default */
134407939627SJaegeuk Kim 	ALLOC_MODE_REUSE,	/* reuse segments as much as possible */
134507939627SJaegeuk Kim };
134607939627SJaegeuk Kim 
134793cf93f1SJunling Zheng enum fsync_mode {
134893cf93f1SJunling Zheng 	FSYNC_MODE_POSIX,	/* fsync follows posix semantics */
134993cf93f1SJunling Zheng 	FSYNC_MODE_STRICT,	/* fsync behaves in line with ext4 */
1350d6290814SJaegeuk Kim 	FSYNC_MODE_NOBARRIER,	/* fsync behaves nobarrier based on posix */
135193cf93f1SJunling Zheng };
135293cf93f1SJunling Zheng 
1353602a16d5SDaeho Jeong enum {
1354602a16d5SDaeho Jeong 	COMPR_MODE_FS,		/*
1355602a16d5SDaeho Jeong 				 * automatically compress compression
1356602a16d5SDaeho Jeong 				 * enabled files
1357602a16d5SDaeho Jeong 				 */
1358602a16d5SDaeho Jeong 	COMPR_MODE_USER,	/*
1359602a16d5SDaeho Jeong 				 * automatical compression is disabled.
1360602a16d5SDaeho Jeong 				 * user can control the file compression
1361602a16d5SDaeho Jeong 				 * using ioctls
1362602a16d5SDaeho Jeong 				 */
1363602a16d5SDaeho Jeong };
1364602a16d5SDaeho Jeong 
13654f993264SChao Yu enum {
13664f993264SChao Yu 	DISCARD_UNIT_BLOCK,	/* basic discard unit is block */
13674f993264SChao Yu 	DISCARD_UNIT_SEGMENT,	/* basic discard unit is segment */
13684f993264SChao Yu 	DISCARD_UNIT_SECTION,	/* basic discard unit is section */
13694f993264SChao Yu };
13704f993264SChao Yu 
1371b763f3beSChao Yu static inline int f2fs_test_bit(unsigned int nr, char *addr);
1372b763f3beSChao Yu static inline void f2fs_set_bit(unsigned int nr, char *addr);
1373b763f3beSChao Yu static inline void f2fs_clear_bit(unsigned int nr, char *addr);
13744c8ff709SChao Yu 
1375b763f3beSChao Yu /*
1376b763f3beSChao Yu  * Layout of f2fs page.private:
1377b763f3beSChao Yu  *
1378b763f3beSChao Yu  * Layout A: lowest bit should be 1
1379b763f3beSChao Yu  * | bit0 = 1 | bit1 | bit2 | ... | bit MAX | private data .... |
1380b763f3beSChao Yu  * bit 0	PAGE_PRIVATE_NOT_POINTER
1381b763f3beSChao Yu  * bit 1	PAGE_PRIVATE_ATOMIC_WRITE
1382b763f3beSChao Yu  * bit 2	PAGE_PRIVATE_DUMMY_WRITE
1383b763f3beSChao Yu  * bit 3	PAGE_PRIVATE_ONGOING_MIGRATION
1384b763f3beSChao Yu  * bit 4	PAGE_PRIVATE_INLINE_INODE
1385b763f3beSChao Yu  * bit 5	PAGE_PRIVATE_REF_RESOURCE
1386b763f3beSChao Yu  * bit 6-	f2fs private data
1387b763f3beSChao Yu  *
1388b763f3beSChao Yu  * Layout B: lowest bit should be 0
1389b763f3beSChao Yu  * page.private is a wrapped pointer.
1390b763f3beSChao Yu  */
1391b763f3beSChao Yu enum {
1392b763f3beSChao Yu 	PAGE_PRIVATE_NOT_POINTER,		/* private contains non-pointer data */
1393b763f3beSChao Yu 	PAGE_PRIVATE_ATOMIC_WRITE,		/* data page from atomic write path */
1394b763f3beSChao Yu 	PAGE_PRIVATE_DUMMY_WRITE,		/* data page for padding aligned IO */
1395b763f3beSChao Yu 	PAGE_PRIVATE_ONGOING_MIGRATION,		/* data page which is on-going migrating */
1396b763f3beSChao Yu 	PAGE_PRIVATE_INLINE_INODE,		/* inode page contains inline data */
1397b763f3beSChao Yu 	PAGE_PRIVATE_REF_RESOURCE,		/* dirty page has referenced resources */
1398b763f3beSChao Yu 	PAGE_PRIVATE_MAX
1399b763f3beSChao Yu };
1400b763f3beSChao Yu 
1401b763f3beSChao Yu #define PAGE_PRIVATE_GET_FUNC(name, flagname) \
1402b763f3beSChao Yu static inline bool page_private_##name(struct page *page) \
1403b763f3beSChao Yu { \
1404c9ebd3dfSJaegeuk Kim 	return PagePrivate(page) && \
1405c9ebd3dfSJaegeuk Kim 		test_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page)) && \
1406b763f3beSChao Yu 		test_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
1407b763f3beSChao Yu }
1408b763f3beSChao Yu 
1409b763f3beSChao Yu #define PAGE_PRIVATE_SET_FUNC(name, flagname) \
1410b763f3beSChao Yu static inline void set_page_private_##name(struct page *page) \
1411b763f3beSChao Yu { \
1412b763f3beSChao Yu 	if (!PagePrivate(page)) { \
1413b763f3beSChao Yu 		get_page(page); \
1414b763f3beSChao Yu 		SetPagePrivate(page); \
1415c9ebd3dfSJaegeuk Kim 		set_page_private(page, 0); \
1416b763f3beSChao Yu 	} \
1417b763f3beSChao Yu 	set_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page)); \
1418b763f3beSChao Yu 	set_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
1419b763f3beSChao Yu }
1420b763f3beSChao Yu 
1421b763f3beSChao Yu #define PAGE_PRIVATE_CLEAR_FUNC(name, flagname) \
1422b763f3beSChao Yu static inline void clear_page_private_##name(struct page *page) \
1423b763f3beSChao Yu { \
1424b763f3beSChao Yu 	clear_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
1425b763f3beSChao Yu 	if (page_private(page) == 1 << PAGE_PRIVATE_NOT_POINTER) { \
1426b763f3beSChao Yu 		set_page_private(page, 0); \
1427b763f3beSChao Yu 		if (PagePrivate(page)) { \
1428b763f3beSChao Yu 			ClearPagePrivate(page); \
1429b763f3beSChao Yu 			put_page(page); \
1430b763f3beSChao Yu 		}\
1431b763f3beSChao Yu 	} \
1432b763f3beSChao Yu }
1433b763f3beSChao Yu 
1434b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(nonpointer, NOT_POINTER);
1435b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(reference, REF_RESOURCE);
1436b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(inline, INLINE_INODE);
1437b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(gcing, ONGOING_MIGRATION);
1438b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(atomic, ATOMIC_WRITE);
1439b763f3beSChao Yu PAGE_PRIVATE_GET_FUNC(dummy, DUMMY_WRITE);
1440b763f3beSChao Yu 
1441b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(reference, REF_RESOURCE);
1442b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(inline, INLINE_INODE);
1443b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(gcing, ONGOING_MIGRATION);
1444b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(atomic, ATOMIC_WRITE);
1445b763f3beSChao Yu PAGE_PRIVATE_SET_FUNC(dummy, DUMMY_WRITE);
1446b763f3beSChao Yu 
1447b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(reference, REF_RESOURCE);
1448b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(inline, INLINE_INODE);
1449b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(gcing, ONGOING_MIGRATION);
1450b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(atomic, ATOMIC_WRITE);
1451b763f3beSChao Yu PAGE_PRIVATE_CLEAR_FUNC(dummy, DUMMY_WRITE);
14524c8ff709SChao Yu 
14536ce19affSChao Yu static inline unsigned long get_page_private_data(struct page *page)
14546ce19affSChao Yu {
14556ce19affSChao Yu 	unsigned long data = page_private(page);
14566ce19affSChao Yu 
14576ce19affSChao Yu 	if (!test_bit(PAGE_PRIVATE_NOT_POINTER, &data))
14586ce19affSChao Yu 		return 0;
14596ce19affSChao Yu 	return data >> PAGE_PRIVATE_MAX;
14606ce19affSChao Yu }
14616ce19affSChao Yu 
14626ce19affSChao Yu static inline void set_page_private_data(struct page *page, unsigned long data)
14636ce19affSChao Yu {
14646ce19affSChao Yu 	if (!PagePrivate(page)) {
14656ce19affSChao Yu 		get_page(page);
14666ce19affSChao Yu 		SetPagePrivate(page);
1467c9ebd3dfSJaegeuk Kim 		set_page_private(page, 0);
14686ce19affSChao Yu 	}
14696ce19affSChao Yu 	set_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page));
14706ce19affSChao Yu 	page_private(page) |= data << PAGE_PRIVATE_MAX;
14716ce19affSChao Yu }
14726ce19affSChao Yu 
14736ce19affSChao Yu static inline void clear_page_private_data(struct page *page)
14746ce19affSChao Yu {
14756ce19affSChao Yu 	page_private(page) &= (1 << PAGE_PRIVATE_MAX) - 1;
14766ce19affSChao Yu 	if (page_private(page) == 1 << PAGE_PRIVATE_NOT_POINTER) {
14776ce19affSChao Yu 		set_page_private(page, 0);
14786ce19affSChao Yu 		if (PagePrivate(page)) {
14796ce19affSChao Yu 			ClearPagePrivate(page);
14806ce19affSChao Yu 			put_page(page);
14816ce19affSChao Yu 		}
14826ce19affSChao Yu 	}
14836ce19affSChao Yu }
14846ce19affSChao Yu 
14854c8ff709SChao Yu /* For compression */
14864c8ff709SChao Yu enum compress_algorithm_type {
14874c8ff709SChao Yu 	COMPRESS_LZO,
14884c8ff709SChao Yu 	COMPRESS_LZ4,
148950cfa66fSChao Yu 	COMPRESS_ZSTD,
14906d92b201SChao Yu 	COMPRESS_LZORLE,
14914c8ff709SChao Yu 	COMPRESS_MAX,
14924c8ff709SChao Yu };
14934c8ff709SChao Yu 
1494b28f047bSChao Yu enum compress_flag {
1495b28f047bSChao Yu 	COMPRESS_CHKSUM,
1496b28f047bSChao Yu 	COMPRESS_MAX_FLAG,
1497b28f047bSChao Yu };
1498b28f047bSChao Yu 
14996ce19affSChao Yu #define	COMPRESS_WATERMARK			20
15006ce19affSChao Yu #define	COMPRESS_PERCENT			20
15016ce19affSChao Yu 
1502b28f047bSChao Yu #define COMPRESS_DATA_RESERVED_SIZE		4
15034c8ff709SChao Yu struct compress_data {
15044c8ff709SChao Yu 	__le32 clen;			/* compressed data size */
1505b28f047bSChao Yu 	__le32 chksum;			/* compressed data chksum */
15064c8ff709SChao Yu 	__le32 reserved[COMPRESS_DATA_RESERVED_SIZE];	/* reserved */
15074c8ff709SChao Yu 	u8 cdata[];			/* compressed data */
15084c8ff709SChao Yu };
15094c8ff709SChao Yu 
15104c8ff709SChao Yu #define COMPRESS_HEADER_SIZE	(sizeof(struct compress_data))
15114c8ff709SChao Yu 
15124c8ff709SChao Yu #define F2FS_COMPRESSED_PAGE_MAGIC	0xF5F2C000
15134c8ff709SChao Yu 
15143fde13f8SChao Yu #define	COMPRESS_LEVEL_OFFSET	8
15153fde13f8SChao Yu 
15164c8ff709SChao Yu /* compress context */
15174c8ff709SChao Yu struct compress_ctx {
15184c8ff709SChao Yu 	struct inode *inode;		/* inode the context belong to */
15194c8ff709SChao Yu 	pgoff_t cluster_idx;		/* cluster index number */
15204c8ff709SChao Yu 	unsigned int cluster_size;	/* page count in cluster */
15214c8ff709SChao Yu 	unsigned int log_cluster_size;	/* log of cluster size */
15224c8ff709SChao Yu 	struct page **rpages;		/* pages store raw data in cluster */
15234c8ff709SChao Yu 	unsigned int nr_rpages;		/* total page number in rpages */
15244c8ff709SChao Yu 	struct page **cpages;		/* pages store compressed data in cluster */
15254c8ff709SChao Yu 	unsigned int nr_cpages;		/* total page number in cpages */
15263271d7ebSFengnan Chang 	unsigned int valid_nr_cpages;	/* valid page number in cpages */
15274c8ff709SChao Yu 	void *rbuf;			/* virtual mapped address on rpages */
15284c8ff709SChao Yu 	struct compress_data *cbuf;	/* virtual mapped address on cpages */
15294c8ff709SChao Yu 	size_t rlen;			/* valid data length in rbuf */
15304c8ff709SChao Yu 	size_t clen;			/* valid data length in cbuf */
15314c8ff709SChao Yu 	void *private;			/* payload buffer for specified compression algorithm */
153250cfa66fSChao Yu 	void *private2;			/* extra payload buffer */
15334c8ff709SChao Yu };
15344c8ff709SChao Yu 
15354c8ff709SChao Yu /* compress context for write IO path */
15364c8ff709SChao Yu struct compress_io_ctx {
15374c8ff709SChao Yu 	u32 magic;			/* magic number to indicate page is compressed */
15384c8ff709SChao Yu 	struct inode *inode;		/* inode the context belong to */
15394c8ff709SChao Yu 	struct page **rpages;		/* pages store raw data in cluster */
15404c8ff709SChao Yu 	unsigned int nr_rpages;		/* total page number in rpages */
1541e6c3948dSChao Yu 	atomic_t pending_pages;		/* in-flight compressed page count */
15424c8ff709SChao Yu };
15434c8ff709SChao Yu 
15447f59b277SEric Biggers /* Context for decompressing one cluster on the read IO path */
15454c8ff709SChao Yu struct decompress_io_ctx {
15464c8ff709SChao Yu 	u32 magic;			/* magic number to indicate page is compressed */
15474c8ff709SChao Yu 	struct inode *inode;		/* inode the context belong to */
15484c8ff709SChao Yu 	pgoff_t cluster_idx;		/* cluster index number */
15494c8ff709SChao Yu 	unsigned int cluster_size;	/* page count in cluster */
15504c8ff709SChao Yu 	unsigned int log_cluster_size;	/* log of cluster size */
15514c8ff709SChao Yu 	struct page **rpages;		/* pages store raw data in cluster */
15524c8ff709SChao Yu 	unsigned int nr_rpages;		/* total page number in rpages */
15534c8ff709SChao Yu 	struct page **cpages;		/* pages store compressed data in cluster */
15544c8ff709SChao Yu 	unsigned int nr_cpages;		/* total page number in cpages */
15554c8ff709SChao Yu 	struct page **tpages;		/* temp pages to pad holes in cluster */
15564c8ff709SChao Yu 	void *rbuf;			/* virtual mapped address on rpages */
15574c8ff709SChao Yu 	struct compress_data *cbuf;	/* virtual mapped address on cpages */
15584c8ff709SChao Yu 	size_t rlen;			/* valid data length in rbuf */
15594c8ff709SChao Yu 	size_t clen;			/* valid data length in cbuf */
15607f59b277SEric Biggers 
15617f59b277SEric Biggers 	/*
15627f59b277SEric Biggers 	 * The number of compressed pages remaining to be read in this cluster.
15637f59b277SEric Biggers 	 * This is initially nr_cpages.  It is decremented by 1 each time a page
15647f59b277SEric Biggers 	 * has been read (or failed to be read).  When it reaches 0, the cluster
15657f59b277SEric Biggers 	 * is decompressed (or an error is reported).
15667f59b277SEric Biggers 	 *
15677f59b277SEric Biggers 	 * If an error occurs before all the pages have been submitted for I/O,
15687f59b277SEric Biggers 	 * then this will never reach 0.  In this case the I/O submitter is
15697f59b277SEric Biggers 	 * responsible for calling f2fs_decompress_end_io() instead.
15707f59b277SEric Biggers 	 */
15717f59b277SEric Biggers 	atomic_t remaining_pages;
15727f59b277SEric Biggers 
15737f59b277SEric Biggers 	/*
15747f59b277SEric Biggers 	 * Number of references to this decompress_io_ctx.
15757f59b277SEric Biggers 	 *
15767f59b277SEric Biggers 	 * One reference is held for I/O completion.  This reference is dropped
15777f59b277SEric Biggers 	 * after the pagecache pages are updated and unlocked -- either after
15787f59b277SEric Biggers 	 * decompression (and verity if enabled), or after an error.
15797f59b277SEric Biggers 	 *
15807f59b277SEric Biggers 	 * In addition, each compressed page holds a reference while it is in a
15817f59b277SEric Biggers 	 * bio.  These references are necessary prevent compressed pages from
15827f59b277SEric Biggers 	 * being freed while they are still in a bio.
15837f59b277SEric Biggers 	 */
15847f59b277SEric Biggers 	refcount_t refcnt;
15857f59b277SEric Biggers 
15867f59b277SEric Biggers 	bool failed;			/* IO error occurred before decompression? */
15877f59b277SEric Biggers 	bool need_verity;		/* need fs-verity verification after decompression? */
158850cfa66fSChao Yu 	void *private;			/* payload buffer for specified decompression algorithm */
158950cfa66fSChao Yu 	void *private2;			/* extra payload buffer */
15907f59b277SEric Biggers 	struct work_struct verity_work;	/* work to verify the decompressed pages */
15914c8ff709SChao Yu };
15924c8ff709SChao Yu 
15934c8ff709SChao Yu #define NULL_CLUSTER			((unsigned int)(~0))
15944c8ff709SChao Yu #define MIN_COMPRESS_LOG_SIZE		2
15954c8ff709SChao Yu #define MAX_COMPRESS_LOG_SIZE		8
15960e2b7385SChao Yu #define MAX_COMPRESS_WINDOW_SIZE(log_size)	((PAGE_SIZE) << (log_size))
15974c8ff709SChao Yu 
159839a53e0cSJaegeuk Kim struct f2fs_sb_info {
159939a53e0cSJaegeuk Kim 	struct super_block *sb;			/* pointer to VFS super block */
16005e176d54SJaegeuk Kim 	struct proc_dir_entry *s_proc;		/* proc entry */
160139a53e0cSJaegeuk Kim 	struct f2fs_super_block *raw_super;	/* raw super block pointer */
1602e4544b63STim Murray 	struct f2fs_rwsem sb_lock;		/* lock for raw super block */
1603e8240f65SChao Yu 	int valid_super_block;			/* valid super block no */
1604fadb2fb8SChao Yu 	unsigned long s_flag;				/* flags for sbi */
1605853137ceSJaegeuk Kim 	struct mutex writepages;		/* mutex for writepages() */
160639a53e0cSJaegeuk Kim 
1607178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
1608178053e2SDamien Le Moal 	unsigned int blocks_per_blkz;		/* F2FS blocks per zone */
1609178053e2SDamien Le Moal 	unsigned int log_blocks_per_blkz;	/* log2 F2FS blocks per zone */
1610178053e2SDamien Le Moal #endif
1611178053e2SDamien Le Moal 
161239a53e0cSJaegeuk Kim 	/* for node-related operations */
161339a53e0cSJaegeuk Kim 	struct f2fs_nm_info *nm_info;		/* node manager */
161439a53e0cSJaegeuk Kim 	struct inode *node_inode;		/* cache node blocks */
161539a53e0cSJaegeuk Kim 
161639a53e0cSJaegeuk Kim 	/* for segment-related operations */
161739a53e0cSJaegeuk Kim 	struct f2fs_sm_info *sm_info;		/* segment manager */
16181ff7bd3bSJaegeuk Kim 
16191ff7bd3bSJaegeuk Kim 	/* for bio operations */
1620a912b54dSJaegeuk Kim 	struct f2fs_bio_info *write_io[NR_PAGE_TYPE];	/* for write bios */
1621107a805dSChao Yu 	/* keep migration IO order for LFS mode */
1622e4544b63STim Murray 	struct f2fs_rwsem io_order_lock;
16230a595ebaSJaegeuk Kim 	mempool_t *write_io_dummy;		/* Dummy pages */
162450c63009SJaegeuk Kim 	pgoff_t metapage_eio_ofs;		/* EIO page offset */
162550c63009SJaegeuk Kim 	int metapage_eio_cnt;			/* EIO count */
162639a53e0cSJaegeuk Kim 
162739a53e0cSJaegeuk Kim 	/* for checkpoint */
162839a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt;		/* raw checkpoint pointer */
16298508e44aSJaegeuk Kim 	int cur_cp_pack;			/* remain current cp pack */
1630aaec2b1dSChao Yu 	spinlock_t cp_lock;			/* for flag in ckpt */
163139a53e0cSJaegeuk Kim 	struct inode *meta_inode;		/* cache meta blocks */
1632e4544b63STim Murray 	struct f2fs_rwsem cp_global_sem;	/* checkpoint procedure lock */
1633e4544b63STim Murray 	struct f2fs_rwsem cp_rwsem;		/* blocking FS operations */
1634e4544b63STim Murray 	struct f2fs_rwsem node_write;		/* locking node writes */
1635e4544b63STim Murray 	struct f2fs_rwsem node_change;	/* locking node change */
1636fb51b5efSChangman Lee 	wait_queue_head_t cp_wait;
16376beceb54SJaegeuk Kim 	unsigned long last_time[MAX_TIME];	/* to store time in jiffies */
16386beceb54SJaegeuk Kim 	long interval_time[MAX_TIME];		/* to store thresholds */
1639261eeb9cSDaeho Jeong 	struct ckpt_req_control cprc_info;	/* for checkpoint request control */
164039a53e0cSJaegeuk Kim 
164167298804SChao Yu 	struct inode_management im[MAX_INO_ENTRY];	/* manage inode cache */
16426451e041SJaegeuk Kim 
164350fa53ecSChao Yu 	spinlock_t fsync_node_lock;		/* for node entry lock */
164450fa53ecSChao Yu 	struct list_head fsync_node_list;	/* node list head */
164550fa53ecSChao Yu 	unsigned int fsync_seg_id;		/* sequence id */
164650fa53ecSChao Yu 	unsigned int fsync_node_num;		/* number of node entries */
164750fa53ecSChao Yu 
16486451e041SJaegeuk Kim 	/* for orphan inode, use 0'th array */
16490d47c1adSGu Zheng 	unsigned int max_orphans;		/* max orphan inodes */
165039a53e0cSJaegeuk Kim 
1651c227f912SChao Yu 	/* for inode management */
1652c227f912SChao Yu 	struct list_head inode_list[NR_INODE_TYPE];	/* dirty inode list */
1653c227f912SChao Yu 	spinlock_t inode_lock[NR_INODE_TYPE];	/* for dirty inode list lock */
1654040d2bb3SChao Yu 	struct mutex flush_lock;		/* for flush exclusion */
165539a53e0cSJaegeuk Kim 
165613054c54SChao Yu 	/* for extent tree cache */
165713054c54SChao Yu 	struct radix_tree_root extent_tree_root;/* cache extent cache entries */
16585e8256acSYunlei He 	struct mutex extent_tree_lock;	/* locking extent radix tree */
165913054c54SChao Yu 	struct list_head extent_list;		/* lru list for shrinker */
166013054c54SChao Yu 	spinlock_t extent_lock;			/* locking extent lru list */
16617441ccefSJaegeuk Kim 	atomic_t total_ext_tree;		/* extent tree count */
1662137d09f0SJaegeuk Kim 	struct list_head zombie_list;		/* extent zombie tree list */
166374fd8d99SJaegeuk Kim 	atomic_t total_zombie_tree;		/* extent zombie tree count */
166413054c54SChao Yu 	atomic_t total_ext_node;		/* extent info count */
166513054c54SChao Yu 
166639a53e0cSJaegeuk Kim 	/* basic filesystem units */
166739a53e0cSJaegeuk Kim 	unsigned int log_sectors_per_block;	/* log2 sectors per block */
166839a53e0cSJaegeuk Kim 	unsigned int log_blocksize;		/* log2 block size */
166939a53e0cSJaegeuk Kim 	unsigned int blocksize;			/* block size */
167039a53e0cSJaegeuk Kim 	unsigned int root_ino_num;		/* root inode number*/
167139a53e0cSJaegeuk Kim 	unsigned int node_ino_num;		/* node inode number*/
167239a53e0cSJaegeuk Kim 	unsigned int meta_ino_num;		/* meta inode number*/
167339a53e0cSJaegeuk Kim 	unsigned int log_blocks_per_seg;	/* log2 blocks per segment */
167439a53e0cSJaegeuk Kim 	unsigned int blocks_per_seg;		/* blocks per segment */
167539a53e0cSJaegeuk Kim 	unsigned int segs_per_sec;		/* segments per section */
167639a53e0cSJaegeuk Kim 	unsigned int secs_per_zone;		/* sections per zone */
167739a53e0cSJaegeuk Kim 	unsigned int total_sections;		/* total section count */
167839a53e0cSJaegeuk Kim 	unsigned int total_node_count;		/* total node block count */
167939a53e0cSJaegeuk Kim 	unsigned int total_valid_node_count;	/* valid node block count */
1680ab9fa662SJaegeuk Kim 	int dir_level;				/* directory level */
1681f6df8f23SSheng Yong 	int readdir_ra;				/* readahead inode in readdir */
168210208567SJaegeuk Kim 	u64 max_io_bytes;			/* max io bytes to merge IOs */
168339a53e0cSJaegeuk Kim 
168439a53e0cSJaegeuk Kim 	block_t user_block_count;		/* # of user blocks */
168539a53e0cSJaegeuk Kim 	block_t total_valid_block_count;	/* # of valid blocks */
1686a66cdd98SJaegeuk Kim 	block_t discard_blks;			/* discard command candidats */
168739a53e0cSJaegeuk Kim 	block_t last_valid_block_count;		/* for recovery */
1688daeb433eSChao Yu 	block_t reserved_blocks;		/* configurable reserved blocks */
168980d42145SYunlong Song 	block_t current_reserved_blocks;	/* current reserved blocks */
1690daeb433eSChao Yu 
16914354994fSDaniel Rosenberg 	/* Additional tracking for no checkpoint mode */
16924354994fSDaniel Rosenberg 	block_t unusable_block_count;		/* # of blocks saved by last cp */
16934354994fSDaniel Rosenberg 
1694292c196aSChao Yu 	unsigned int nquota_files;		/* # of quota sysfile */
1695e4544b63STim Murray 	struct f2fs_rwsem quota_sem;		/* blocking cp for flags */
1696292c196aSChao Yu 
1697523be8a6SJaegeuk Kim 	/* # of pages, see count_type */
169835782b23SJaegeuk Kim 	atomic_t nr_pages[NR_COUNT_TYPE];
169941382ec4SJaegeuk Kim 	/* # of allocated blocks */
170041382ec4SJaegeuk Kim 	struct percpu_counter alloc_valid_block_count;
170147c8ebccSJaegeuk Kim 	/* # of node block writes as roll forward recovery */
170247c8ebccSJaegeuk Kim 	struct percpu_counter rf_node_block_count;
170339a53e0cSJaegeuk Kim 
1704687de7f1SJaegeuk Kim 	/* writeback control */
1705c29fd0c0SChao Yu 	atomic_t wb_sync_req[META];	/* count # of WB_SYNC threads */
1706687de7f1SJaegeuk Kim 
1707513c5f37SJaegeuk Kim 	/* valid inode count */
1708513c5f37SJaegeuk Kim 	struct percpu_counter total_valid_inode_count;
1709513c5f37SJaegeuk Kim 
171039a53e0cSJaegeuk Kim 	struct f2fs_mount_info mount_opt;	/* mount options */
171139a53e0cSJaegeuk Kim 
171239a53e0cSJaegeuk Kim 	/* for cleaning operations */
1713e4544b63STim Murray 	struct f2fs_rwsem gc_lock;		/*
1714fb24fea7SChao Yu 						 * semaphore for GC, avoid
1715fb24fea7SChao Yu 						 * race between GC and GC or CP
1716fb24fea7SChao Yu 						 */
171739a53e0cSJaegeuk Kim 	struct f2fs_gc_kthread	*gc_thread;	/* GC thread */
1718093749e2SChao Yu 	struct atgc_management am;		/* atgc management */
17195ec4e49fSJaegeuk Kim 	unsigned int cur_victim_sec;		/* current victim section num */
17205b0e9539SJaegeuk Kim 	unsigned int gc_mode;			/* current GC state */
1721e3080b01SChao Yu 	unsigned int next_victim_seg[2];	/* next segment in victim section */
1722325163e9SDaeho Jeong 	spinlock_t gc_urgent_high_lock;
1723325163e9SDaeho Jeong 	bool gc_urgent_high_limited;		/* indicates having limited trial count */
1724325163e9SDaeho Jeong 	unsigned int gc_urgent_high_remaining;	/* remaining trial count for GC_URGENT_HIGH */
17250e5e8111SDaeho Jeong 
17262ef79ecbSChao Yu 	/* for skip statistic */
1727677017d1SSahitya Tummala 	unsigned int atomic_files;		/* # of opened atomic file */
17282ef79ecbSChao Yu 	unsigned long long skipped_atomic_files[2];	/* FG_GC and BG_GC */
17296f8d4455SJaegeuk Kim 	unsigned long long skipped_gc_rwsem;		/* FG_GC only */
173039a53e0cSJaegeuk Kim 
17311ad71a27SJaegeuk Kim 	/* threshold for gc trials on pinned files */
17321ad71a27SJaegeuk Kim 	u64 gc_pin_file_threshold;
1733e4544b63STim Murray 	struct f2fs_rwsem pin_sem;
17341ad71a27SJaegeuk Kim 
1735b1c57c1cSJaegeuk Kim 	/* maximum # of trials to find a victim segment for SSR and GC */
1736b1c57c1cSJaegeuk Kim 	unsigned int max_victim_search;
1737e3080b01SChao Yu 	/* migration granularity of garbage collection, unit: segment */
1738e3080b01SChao Yu 	unsigned int migration_granularity;
1739b1c57c1cSJaegeuk Kim 
174039a53e0cSJaegeuk Kim 	/*
174139a53e0cSJaegeuk Kim 	 * for stat information.
174239a53e0cSJaegeuk Kim 	 * one is for the LFS mode, and the other is for the SSR mode.
174339a53e0cSJaegeuk Kim 	 */
174435b09d82SNamjae Jeon #ifdef CONFIG_F2FS_STAT_FS
174539a53e0cSJaegeuk Kim 	struct f2fs_stat_info *stat_info;	/* FS status information */
1746b63e7be5SChao Yu 	atomic_t meta_count[META_MAX];		/* # of meta blocks */
174739a53e0cSJaegeuk Kim 	unsigned int segment_count[2];		/* # of allocated segments */
174839a53e0cSJaegeuk Kim 	unsigned int block_count[2];		/* # of allocated blocks */
1749b9a2c252SChangman Lee 	atomic_t inplace_count;		/* # of inplace update */
17505b7ee374SChao Yu 	atomic64_t total_hit_ext;		/* # of lookup extent cache */
17515b7ee374SChao Yu 	atomic64_t read_hit_rbtree;		/* # of hit rbtree extent node */
17525b7ee374SChao Yu 	atomic64_t read_hit_largest;		/* # of hit largest extent node */
17535b7ee374SChao Yu 	atomic64_t read_hit_cached;		/* # of hit cached extent node */
1754d5e8f6c9SChao Yu 	atomic_t inline_xattr;			/* # of inline_xattr inodes */
175503e14d52SChao Yu 	atomic_t inline_inode;			/* # of inline_data inodes */
175603e14d52SChao Yu 	atomic_t inline_dir;			/* # of inline_dentry inodes */
17574c8ff709SChao Yu 	atomic_t compr_inode;			/* # of compressed inodes */
1758ae999bb9SDaeho Jeong 	atomic64_t compr_blocks;		/* # of compressed blocks */
1759648d50baSChao Yu 	atomic_t vw_cnt;			/* # of volatile writes */
176026a28a0cSJaegeuk Kim 	atomic_t max_aw_cnt;			/* max # of atomic writes */
1761648d50baSChao Yu 	atomic_t max_vw_cnt;			/* max # of volatile writes */
1762274bd9baSChao Yu 	unsigned int io_skip_bggc;		/* skip background gc for in-flight IO */
1763274bd9baSChao Yu 	unsigned int other_skip_bggc;		/* skip background gc for other reasons */
176433fbd510SChao Yu 	unsigned int ndirty_inode[NR_INODE_TYPE];	/* # of dirty inodes */
176535b09d82SNamjae Jeon #endif
176639a53e0cSJaegeuk Kim 	spinlock_t stat_lock;			/* lock for stat operations */
1767b59d0baeSNamjae Jeon 
1768da9953b7SJaegeuk Kim 	/* to attach REQ_META|REQ_FUA flags */
1769da9953b7SJaegeuk Kim 	unsigned int data_io_flag;
177032b6aba8SJaegeuk Kim 	unsigned int node_io_flag;
1771b0af6d49SChao Yu 
1772b59d0baeSNamjae Jeon 	/* For sysfs suppport */
17735d4daa57SChao Yu 	struct kobject s_kobj;			/* /sys/fs/f2fs/<devname> */
1774b59d0baeSNamjae Jeon 	struct completion s_kobj_unregister;
17752658e50dSJaegeuk Kim 
17765d4daa57SChao Yu 	struct kobject s_stat_kobj;		/* /sys/fs/f2fs/<devname>/stat */
17775d4daa57SChao Yu 	struct completion s_stat_kobj_unregister;
17785d4daa57SChao Yu 
17794c89b53dSJaegeuk Kim 	struct kobject s_feature_list_kobj;		/* /sys/fs/f2fs/<devname>/feature_list */
17804c89b53dSJaegeuk Kim 	struct completion s_feature_list_kobj_unregister;
17814c89b53dSJaegeuk Kim 
17822658e50dSJaegeuk Kim 	/* For shrinker support */
17832658e50dSJaegeuk Kim 	struct list_head s_list;
178471f2c820SChao Yu 	struct mutex umount_mutex;
178571f2c820SChao Yu 	unsigned int shrinker_run_no;
178671f2c820SChao Yu 
178771f2c820SChao Yu 	/* For multi devices */
17883c62be17SJaegeuk Kim 	int s_ndevs;				/* number of devices */
17893c62be17SJaegeuk Kim 	struct f2fs_dev_info *devs;		/* for device list */
17901228b482SChao Yu 	unsigned int dirty_device;		/* for checkpoint data flush */
17911228b482SChao Yu 	spinlock_t dev_lock;			/* protect dirty_device */
179271f2c820SChao Yu 	bool aligned_blksize;			/* all devices has the same logical blksize */
17938f1dbbbbSShuoran Liu 
17948f1dbbbbSShuoran Liu 	/* For write statistics */
17958f1dbbbbSShuoran Liu 	u64 sectors_written_start;
17968f1dbbbbSShuoran Liu 	u64 kbytes_written;
179743b6573bSKeith Mok 
179843b6573bSKeith Mok 	/* Reference to checksum algorithm driver via cryptoapi */
179943b6573bSKeith Mok 	struct crypto_shash *s_chksum_driver;
18001ecc0c5cSChao Yu 
1801704956ecSChao Yu 	/* Precomputed FS UUID checksum for seeding other checksums */
1802704956ecSChao Yu 	__u32 s_chksum_seed;
18034c8ff709SChao Yu 
18044c8ff709SChao Yu 	struct workqueue_struct *post_read_wq;	/* post read workqueue */
1805a999150fSChao Yu 
1806a999150fSChao Yu 	struct kmem_cache *inline_xattr_slab;	/* inline xattr entry */
1807a999150fSChao Yu 	unsigned int inline_xattr_slab_size;	/* default inline xattr slab size */
180831083031SChao Yu 
180907c6b593SDaeho Jeong 	/* For reclaimed segs statistics per each GC mode */
181007c6b593SDaeho Jeong 	unsigned int gc_segment_mode;		/* GC state for reclaimed segments */
181107c6b593SDaeho Jeong 	unsigned int gc_reclaimed_segs[MAX_GC_MODE];	/* Reclaimed segs for each mode */
181207c6b593SDaeho Jeong 
18130f6b56ecSDaeho Jeong 	unsigned long seq_file_ra_mul;		/* multiplier for ra_pages of seq. files in fadvise */
18140f6b56ecSDaeho Jeong 
18156691d940SDaeho Jeong 	int max_fragment_chunk;			/* max chunk size for block fragmentation mode */
18166691d940SDaeho Jeong 	int max_fragment_hole;			/* max hole size for block fragmentation mode */
18176691d940SDaeho Jeong 
181831083031SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
181931083031SChao Yu 	struct kmem_cache *page_array_slab;	/* page array entry */
182031083031SChao Yu 	unsigned int page_array_slab_size;	/* default page array slab size */
18215ac443e2SDaeho Jeong 
18225ac443e2SDaeho Jeong 	/* For runtime compression statistics */
18235ac443e2SDaeho Jeong 	u64 compr_written_block;
18245ac443e2SDaeho Jeong 	u64 compr_saved_block;
18255ac443e2SDaeho Jeong 	u32 compr_new_inode;
18266ce19affSChao Yu 
18276ce19affSChao Yu 	/* For compressed block cache */
18286ce19affSChao Yu 	struct inode *compress_inode;		/* cache compressed blocks */
18296ce19affSChao Yu 	unsigned int compress_percent;		/* cache page percentage */
18306ce19affSChao Yu 	unsigned int compress_watermark;	/* cache page watermark */
18316ce19affSChao Yu 	atomic_t compress_page_hit;		/* cache hit count */
183231083031SChao Yu #endif
183352118743SDaeho Jeong 
183452118743SDaeho Jeong #ifdef CONFIG_F2FS_IOSTAT
183552118743SDaeho Jeong 	/* For app/fs IO statistics */
183652118743SDaeho Jeong 	spinlock_t iostat_lock;
183752118743SDaeho Jeong 	unsigned long long rw_iostat[NR_IO_TYPE];
183852118743SDaeho Jeong 	unsigned long long prev_rw_iostat[NR_IO_TYPE];
183952118743SDaeho Jeong 	bool iostat_enable;
184052118743SDaeho Jeong 	unsigned long iostat_next_period;
184152118743SDaeho Jeong 	unsigned int iostat_period_ms;
1842a4b68176SDaeho Jeong 
1843a4b68176SDaeho Jeong 	/* For io latency related statistics info in one iostat period */
1844a4b68176SDaeho Jeong 	spinlock_t iostat_lat_lock;
1845a4b68176SDaeho Jeong 	struct iostat_lat_info *iostat_io_lat;
184652118743SDaeho Jeong #endif
184739a53e0cSJaegeuk Kim };
184839a53e0cSJaegeuk Kim 
18491ecc0c5cSChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
1850c45d6002SChao Yu #define f2fs_show_injection_info(sbi, type)					\
1851c45d6002SChao Yu 	printk_ratelimited("%sF2FS-fs (%s) : inject %s in %s of %pS\n",	\
1852c45d6002SChao Yu 		KERN_INFO, sbi->sb->s_id,				\
1853c45d6002SChao Yu 		f2fs_fault_name[type],					\
185455523519SChao Yu 		__func__, __builtin_return_address(0))
18551ecc0c5cSChao Yu static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
18561ecc0c5cSChao Yu {
185763189b78SChao Yu 	struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
18581ecc0c5cSChao Yu 
18591ecc0c5cSChao Yu 	if (!ffi->inject_rate)
18601ecc0c5cSChao Yu 		return false;
18611ecc0c5cSChao Yu 
18621ecc0c5cSChao Yu 	if (!IS_FAULT_SET(ffi, type))
18631ecc0c5cSChao Yu 		return false;
18641ecc0c5cSChao Yu 
18651ecc0c5cSChao Yu 	atomic_inc(&ffi->inject_ops);
18661ecc0c5cSChao Yu 	if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
18671ecc0c5cSChao Yu 		atomic_set(&ffi->inject_ops, 0);
18681ecc0c5cSChao Yu 		return true;
18691ecc0c5cSChao Yu 	}
18701ecc0c5cSChao Yu 	return false;
18711ecc0c5cSChao Yu }
18727fa750a1SArnd Bergmann #else
1873c45d6002SChao Yu #define f2fs_show_injection_info(sbi, type) do { } while (0)
18747fa750a1SArnd Bergmann static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
18757fa750a1SArnd Bergmann {
18767fa750a1SArnd Bergmann 	return false;
18777fa750a1SArnd Bergmann }
18781ecc0c5cSChao Yu #endif
18791ecc0c5cSChao Yu 
18800916878dSDamien Le Moal /*
18810916878dSDamien Le Moal  * Test if the mounted volume is a multi-device volume.
18820916878dSDamien Le Moal  *   - For a single regular disk volume, sbi->s_ndevs is 0.
18830916878dSDamien Le Moal  *   - For a single zoned disk volume, sbi->s_ndevs is 1.
18840916878dSDamien Le Moal  *   - For a multi-device volume, sbi->s_ndevs is always 2 or more.
18850916878dSDamien Le Moal  */
18860916878dSDamien Le Moal static inline bool f2fs_is_multi_device(struct f2fs_sb_info *sbi)
18870916878dSDamien Le Moal {
18880916878dSDamien Le Moal 	return sbi->s_ndevs > 1;
18890916878dSDamien Le Moal }
18900916878dSDamien Le Moal 
18916beceb54SJaegeuk Kim static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
18926beceb54SJaegeuk Kim {
1893a7d10cf3SSahitya Tummala 	unsigned long now = jiffies;
1894a7d10cf3SSahitya Tummala 
1895a7d10cf3SSahitya Tummala 	sbi->last_time[type] = now;
1896a7d10cf3SSahitya Tummala 
1897a7d10cf3SSahitya Tummala 	/* DISCARD_TIME and GC_TIME are based on REQ_TIME */
1898a7d10cf3SSahitya Tummala 	if (type == REQ_TIME) {
1899a7d10cf3SSahitya Tummala 		sbi->last_time[DISCARD_TIME] = now;
1900a7d10cf3SSahitya Tummala 		sbi->last_time[GC_TIME] = now;
1901a7d10cf3SSahitya Tummala 	}
19026beceb54SJaegeuk Kim }
19036beceb54SJaegeuk Kim 
19046beceb54SJaegeuk Kim static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
19056beceb54SJaegeuk Kim {
19066bccfa19SArnd Bergmann 	unsigned long interval = sbi->interval_time[type] * HZ;
19076beceb54SJaegeuk Kim 
19086beceb54SJaegeuk Kim 	return time_after(jiffies, sbi->last_time[type] + interval);
19096beceb54SJaegeuk Kim }
19106beceb54SJaegeuk Kim 
1911a7d10cf3SSahitya Tummala static inline unsigned int f2fs_time_to_wait(struct f2fs_sb_info *sbi,
1912a7d10cf3SSahitya Tummala 						int type)
1913a7d10cf3SSahitya Tummala {
1914a7d10cf3SSahitya Tummala 	unsigned long interval = sbi->interval_time[type] * HZ;
1915a7d10cf3SSahitya Tummala 	unsigned int wait_ms = 0;
1916a7d10cf3SSahitya Tummala 	long delta;
1917a7d10cf3SSahitya Tummala 
1918a7d10cf3SSahitya Tummala 	delta = (sbi->last_time[type] + interval) - jiffies;
1919a7d10cf3SSahitya Tummala 	if (delta > 0)
1920a7d10cf3SSahitya Tummala 		wait_ms = jiffies_to_msecs(delta);
1921a7d10cf3SSahitya Tummala 
1922a7d10cf3SSahitya Tummala 	return wait_ms;
1923a7d10cf3SSahitya Tummala }
1924a7d10cf3SSahitya Tummala 
192539a53e0cSJaegeuk Kim /*
192639a53e0cSJaegeuk Kim  * Inline functions
192739a53e0cSJaegeuk Kim  */
1928416d2dbbSChao Yu static inline u32 __f2fs_crc32(struct f2fs_sb_info *sbi, u32 crc,
1929704956ecSChao Yu 			      const void *address, unsigned int length)
1930704956ecSChao Yu {
1931704956ecSChao Yu 	struct {
1932704956ecSChao Yu 		struct shash_desc shash;
1933704956ecSChao Yu 		char ctx[4];
1934704956ecSChao Yu 	} desc;
1935704956ecSChao Yu 	int err;
1936704956ecSChao Yu 
1937704956ecSChao Yu 	BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver) != sizeof(desc.ctx));
1938704956ecSChao Yu 
1939704956ecSChao Yu 	desc.shash.tfm = sbi->s_chksum_driver;
1940704956ecSChao Yu 	*(u32 *)desc.ctx = crc;
1941704956ecSChao Yu 
1942704956ecSChao Yu 	err = crypto_shash_update(&desc.shash, address, length);
1943704956ecSChao Yu 	BUG_ON(err);
1944704956ecSChao Yu 
1945704956ecSChao Yu 	return *(u32 *)desc.ctx;
1946704956ecSChao Yu }
1947704956ecSChao Yu 
1948416d2dbbSChao Yu static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
1949416d2dbbSChao Yu 			   unsigned int length)
1950416d2dbbSChao Yu {
1951416d2dbbSChao Yu 	return __f2fs_crc32(sbi, F2FS_SUPER_MAGIC, address, length);
1952416d2dbbSChao Yu }
1953416d2dbbSChao Yu 
1954416d2dbbSChao Yu static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
1955416d2dbbSChao Yu 				  void *buf, size_t buf_size)
1956416d2dbbSChao Yu {
1957416d2dbbSChao Yu 	return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
1958416d2dbbSChao Yu }
1959416d2dbbSChao Yu 
1960416d2dbbSChao Yu static inline u32 f2fs_chksum(struct f2fs_sb_info *sbi, u32 crc,
1961416d2dbbSChao Yu 			      const void *address, unsigned int length)
1962416d2dbbSChao Yu {
1963416d2dbbSChao Yu 	return __f2fs_crc32(sbi, crc, address, length);
1964416d2dbbSChao Yu }
1965416d2dbbSChao Yu 
196639a53e0cSJaegeuk Kim static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
196739a53e0cSJaegeuk Kim {
196839a53e0cSJaegeuk Kim 	return container_of(inode, struct f2fs_inode_info, vfs_inode);
196939a53e0cSJaegeuk Kim }
197039a53e0cSJaegeuk Kim 
197139a53e0cSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
197239a53e0cSJaegeuk Kim {
197339a53e0cSJaegeuk Kim 	return sb->s_fs_info;
197439a53e0cSJaegeuk Kim }
197539a53e0cSJaegeuk Kim 
19764081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
19774081363fSJaegeuk Kim {
19784081363fSJaegeuk Kim 	return F2FS_SB(inode->i_sb);
19794081363fSJaegeuk Kim }
19804081363fSJaegeuk Kim 
19814081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
19824081363fSJaegeuk Kim {
19834081363fSJaegeuk Kim 	return F2FS_I_SB(mapping->host);
19844081363fSJaegeuk Kim }
19854081363fSJaegeuk Kim 
19864081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
19874081363fSJaegeuk Kim {
19884969c06aSJaegeuk Kim 	return F2FS_M_SB(page_file_mapping(page));
19894081363fSJaegeuk Kim }
19904081363fSJaegeuk Kim 
199139a53e0cSJaegeuk Kim static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
199239a53e0cSJaegeuk Kim {
199339a53e0cSJaegeuk Kim 	return (struct f2fs_super_block *)(sbi->raw_super);
199439a53e0cSJaegeuk Kim }
199539a53e0cSJaegeuk Kim 
199639a53e0cSJaegeuk Kim static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
199739a53e0cSJaegeuk Kim {
199839a53e0cSJaegeuk Kim 	return (struct f2fs_checkpoint *)(sbi->ckpt);
199939a53e0cSJaegeuk Kim }
200039a53e0cSJaegeuk Kim 
200145590710SGu Zheng static inline struct f2fs_node *F2FS_NODE(struct page *page)
200245590710SGu Zheng {
200345590710SGu Zheng 	return (struct f2fs_node *)page_address(page);
200445590710SGu Zheng }
200545590710SGu Zheng 
200658bfaf44SJaegeuk Kim static inline struct f2fs_inode *F2FS_INODE(struct page *page)
200758bfaf44SJaegeuk Kim {
200858bfaf44SJaegeuk Kim 	return &((struct f2fs_node *)page_address(page))->i;
200958bfaf44SJaegeuk Kim }
201058bfaf44SJaegeuk Kim 
201139a53e0cSJaegeuk Kim static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
201239a53e0cSJaegeuk Kim {
201339a53e0cSJaegeuk Kim 	return (struct f2fs_nm_info *)(sbi->nm_info);
201439a53e0cSJaegeuk Kim }
201539a53e0cSJaegeuk Kim 
201639a53e0cSJaegeuk Kim static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
201739a53e0cSJaegeuk Kim {
201839a53e0cSJaegeuk Kim 	return (struct f2fs_sm_info *)(sbi->sm_info);
201939a53e0cSJaegeuk Kim }
202039a53e0cSJaegeuk Kim 
202139a53e0cSJaegeuk Kim static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
202239a53e0cSJaegeuk Kim {
202339a53e0cSJaegeuk Kim 	return (struct sit_info *)(SM_I(sbi)->sit_info);
202439a53e0cSJaegeuk Kim }
202539a53e0cSJaegeuk Kim 
202639a53e0cSJaegeuk Kim static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
202739a53e0cSJaegeuk Kim {
202839a53e0cSJaegeuk Kim 	return (struct free_segmap_info *)(SM_I(sbi)->free_info);
202939a53e0cSJaegeuk Kim }
203039a53e0cSJaegeuk Kim 
203139a53e0cSJaegeuk Kim static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
203239a53e0cSJaegeuk Kim {
203339a53e0cSJaegeuk Kim 	return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
203439a53e0cSJaegeuk Kim }
203539a53e0cSJaegeuk Kim 
20369df27d98SGu Zheng static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
20379df27d98SGu Zheng {
20389df27d98SGu Zheng 	return sbi->meta_inode->i_mapping;
20399df27d98SGu Zheng }
20409df27d98SGu Zheng 
20414ef51a8fSJaegeuk Kim static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
20424ef51a8fSJaegeuk Kim {
20434ef51a8fSJaegeuk Kim 	return sbi->node_inode->i_mapping;
20444ef51a8fSJaegeuk Kim }
20454ef51a8fSJaegeuk Kim 
2046caf0047eSChao Yu static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
204739a53e0cSJaegeuk Kim {
2048fadb2fb8SChao Yu 	return test_bit(type, &sbi->s_flag);
204939a53e0cSJaegeuk Kim }
205039a53e0cSJaegeuk Kim 
2051caf0047eSChao Yu static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
205239a53e0cSJaegeuk Kim {
2053fadb2fb8SChao Yu 	set_bit(type, &sbi->s_flag);
2054caf0047eSChao Yu }
2055caf0047eSChao Yu 
2056caf0047eSChao Yu static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
2057caf0047eSChao Yu {
2058fadb2fb8SChao Yu 	clear_bit(type, &sbi->s_flag);
205939a53e0cSJaegeuk Kim }
206039a53e0cSJaegeuk Kim 
2061d71b5564SJaegeuk Kim static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
2062d71b5564SJaegeuk Kim {
2063d71b5564SJaegeuk Kim 	return le64_to_cpu(cp->checkpoint_ver);
2064d71b5564SJaegeuk Kim }
2065d71b5564SJaegeuk Kim 
2066ea676733SJaegeuk Kim static inline unsigned long f2fs_qf_ino(struct super_block *sb, int type)
2067ea676733SJaegeuk Kim {
2068ea676733SJaegeuk Kim 	if (type < F2FS_MAX_QUOTAS)
2069ea676733SJaegeuk Kim 		return le32_to_cpu(F2FS_SB(sb)->raw_super->qf_ino[type]);
2070ea676733SJaegeuk Kim 	return 0;
2071ea676733SJaegeuk Kim }
2072ea676733SJaegeuk Kim 
2073ced2c7eaSKinglong Mee static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
2074ced2c7eaSKinglong Mee {
2075ced2c7eaSKinglong Mee 	size_t crc_offset = le32_to_cpu(cp->checksum_offset);
2076ced2c7eaSKinglong Mee 	return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
2077ced2c7eaSKinglong Mee }
2078ced2c7eaSKinglong Mee 
2079aaec2b1dSChao Yu static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
208025ca923bSJaegeuk Kim {
208125ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
2082aaec2b1dSChao Yu 
208325ca923bSJaegeuk Kim 	return ckpt_flags & f;
208425ca923bSJaegeuk Kim }
208525ca923bSJaegeuk Kim 
2086aaec2b1dSChao Yu static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
208725ca923bSJaegeuk Kim {
2088aaec2b1dSChao Yu 	return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
2089aaec2b1dSChao Yu }
2090aaec2b1dSChao Yu 
2091aaec2b1dSChao Yu static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
2092aaec2b1dSChao Yu {
2093aaec2b1dSChao Yu 	unsigned int ckpt_flags;
2094aaec2b1dSChao Yu 
2095aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
209625ca923bSJaegeuk Kim 	ckpt_flags |= f;
209725ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
209825ca923bSJaegeuk Kim }
209925ca923bSJaegeuk Kim 
2100aaec2b1dSChao Yu static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
210125ca923bSJaegeuk Kim {
2102d1aa2453SChao Yu 	unsigned long flags;
2103d1aa2453SChao Yu 
2104d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
2105aaec2b1dSChao Yu 	__set_ckpt_flags(F2FS_CKPT(sbi), f);
2106d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
2107aaec2b1dSChao Yu }
2108aaec2b1dSChao Yu 
2109aaec2b1dSChao Yu static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
2110aaec2b1dSChao Yu {
2111aaec2b1dSChao Yu 	unsigned int ckpt_flags;
2112aaec2b1dSChao Yu 
2113aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
211425ca923bSJaegeuk Kim 	ckpt_flags &= (~f);
211525ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
211625ca923bSJaegeuk Kim }
211725ca923bSJaegeuk Kim 
2118aaec2b1dSChao Yu static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
2119aaec2b1dSChao Yu {
2120d1aa2453SChao Yu 	unsigned long flags;
2121d1aa2453SChao Yu 
2122d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
2123aaec2b1dSChao Yu 	__clear_ckpt_flags(F2FS_CKPT(sbi), f);
2124d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
2125aaec2b1dSChao Yu }
2126aaec2b1dSChao Yu 
2127c7f91bd4SBart Van Assche #define init_f2fs_rwsem(sem)					\
2128c7f91bd4SBart Van Assche do {								\
2129c7f91bd4SBart Van Assche 	static struct lock_class_key __key;			\
2130c7f91bd4SBart Van Assche 								\
2131c7f91bd4SBart Van Assche 	__init_f2fs_rwsem((sem), #sem, &__key);			\
2132c7f91bd4SBart Van Assche } while (0)
2133c7f91bd4SBart Van Assche 
2134c7f91bd4SBart Van Assche static inline void __init_f2fs_rwsem(struct f2fs_rwsem *sem,
2135c7f91bd4SBart Van Assche 		const char *sem_name, struct lock_class_key *key)
2136e4544b63STim Murray {
2137c7f91bd4SBart Van Assche 	__init_rwsem(&sem->internal_rwsem, sem_name, key);
21387f8e249dSJaegeuk Kim #ifdef CONFIG_F2FS_UNFAIR_RWSEM
2139e4544b63STim Murray 	init_waitqueue_head(&sem->read_waiters);
21407f8e249dSJaegeuk Kim #endif
2141e4544b63STim Murray }
2142e4544b63STim Murray 
2143e4544b63STim Murray static inline int f2fs_rwsem_is_locked(struct f2fs_rwsem *sem)
2144e4544b63STim Murray {
2145e4544b63STim Murray 	return rwsem_is_locked(&sem->internal_rwsem);
2146e4544b63STim Murray }
2147e4544b63STim Murray 
2148e4544b63STim Murray static inline int f2fs_rwsem_is_contended(struct f2fs_rwsem *sem)
2149e4544b63STim Murray {
2150e4544b63STim Murray 	return rwsem_is_contended(&sem->internal_rwsem);
2151e4544b63STim Murray }
2152e4544b63STim Murray 
2153e4544b63STim Murray static inline void f2fs_down_read(struct f2fs_rwsem *sem)
2154e4544b63STim Murray {
21557f8e249dSJaegeuk Kim #ifdef CONFIG_F2FS_UNFAIR_RWSEM
2156e4544b63STim Murray 	wait_event(sem->read_waiters, down_read_trylock(&sem->internal_rwsem));
21577f8e249dSJaegeuk Kim #else
21587f8e249dSJaegeuk Kim 	down_read(&sem->internal_rwsem);
21597f8e249dSJaegeuk Kim #endif
2160e4544b63STim Murray }
2161e4544b63STim Murray 
2162e4544b63STim Murray static inline int f2fs_down_read_trylock(struct f2fs_rwsem *sem)
2163e4544b63STim Murray {
2164e4544b63STim Murray 	return down_read_trylock(&sem->internal_rwsem);
2165e4544b63STim Murray }
2166e4544b63STim Murray 
2167e4544b63STim Murray #ifdef CONFIG_DEBUG_LOCK_ALLOC
2168e4544b63STim Murray static inline void f2fs_down_read_nested(struct f2fs_rwsem *sem, int subclass)
2169e4544b63STim Murray {
2170e4544b63STim Murray 	down_read_nested(&sem->internal_rwsem, subclass);
2171e4544b63STim Murray }
2172e4544b63STim Murray #else
2173e4544b63STim Murray #define f2fs_down_read_nested(sem, subclass) f2fs_down_read(sem)
2174e4544b63STim Murray #endif
2175e4544b63STim Murray 
2176e4544b63STim Murray static inline void f2fs_up_read(struct f2fs_rwsem *sem)
2177e4544b63STim Murray {
2178e4544b63STim Murray 	up_read(&sem->internal_rwsem);
2179e4544b63STim Murray }
2180e4544b63STim Murray 
2181e4544b63STim Murray static inline void f2fs_down_write(struct f2fs_rwsem *sem)
2182e4544b63STim Murray {
2183e4544b63STim Murray 	down_write(&sem->internal_rwsem);
2184e4544b63STim Murray }
2185e4544b63STim Murray 
2186e4544b63STim Murray static inline int f2fs_down_write_trylock(struct f2fs_rwsem *sem)
2187e4544b63STim Murray {
2188e4544b63STim Murray 	return down_write_trylock(&sem->internal_rwsem);
2189e4544b63STim Murray }
2190e4544b63STim Murray 
2191e4544b63STim Murray static inline void f2fs_up_write(struct f2fs_rwsem *sem)
2192e4544b63STim Murray {
2193e4544b63STim Murray 	up_write(&sem->internal_rwsem);
21947f8e249dSJaegeuk Kim #ifdef CONFIG_F2FS_UNFAIR_RWSEM
2195e4544b63STim Murray 	wake_up_all(&sem->read_waiters);
21967f8e249dSJaegeuk Kim #endif
2197e4544b63STim Murray }
2198e4544b63STim Murray 
2199e479556bSGu Zheng static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
220039a53e0cSJaegeuk Kim {
2201e4544b63STim Murray 	f2fs_down_read(&sbi->cp_rwsem);
220239a53e0cSJaegeuk Kim }
220339a53e0cSJaegeuk Kim 
2204cc15620bSJaegeuk Kim static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi)
2205cc15620bSJaegeuk Kim {
22063e020389SChao Yu 	if (time_to_inject(sbi, FAULT_LOCK_OP)) {
22073e020389SChao Yu 		f2fs_show_injection_info(sbi, FAULT_LOCK_OP);
22083e020389SChao Yu 		return 0;
22093e020389SChao Yu 	}
2210e4544b63STim Murray 	return f2fs_down_read_trylock(&sbi->cp_rwsem);
2211cc15620bSJaegeuk Kim }
2212cc15620bSJaegeuk Kim 
2213e479556bSGu Zheng static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
221439a53e0cSJaegeuk Kim {
2215e4544b63STim Murray 	f2fs_up_read(&sbi->cp_rwsem);
221639936837SJaegeuk Kim }
221739936837SJaegeuk Kim 
2218e479556bSGu Zheng static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
221939936837SJaegeuk Kim {
2220e4544b63STim Murray 	f2fs_down_write(&sbi->cp_rwsem);
222139936837SJaegeuk Kim }
222239936837SJaegeuk Kim 
2223e479556bSGu Zheng static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
222439936837SJaegeuk Kim {
2225e4544b63STim Murray 	f2fs_up_write(&sbi->cp_rwsem);
222639a53e0cSJaegeuk Kim }
222739a53e0cSJaegeuk Kim 
2228119ee914SJaegeuk Kim static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
2229119ee914SJaegeuk Kim {
2230119ee914SJaegeuk Kim 	int reason = CP_SYNC;
2231119ee914SJaegeuk Kim 
2232119ee914SJaegeuk Kim 	if (test_opt(sbi, FASTBOOT))
2233119ee914SJaegeuk Kim 		reason = CP_FASTBOOT;
2234119ee914SJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
2235119ee914SJaegeuk Kim 		reason = CP_UMOUNT;
2236119ee914SJaegeuk Kim 	return reason;
2237119ee914SJaegeuk Kim }
2238119ee914SJaegeuk Kim 
2239119ee914SJaegeuk Kim static inline bool __remain_node_summaries(int reason)
2240119ee914SJaegeuk Kim {
2241c473f1a9SChao Yu 	return (reason & (CP_UMOUNT | CP_FASTBOOT));
2242119ee914SJaegeuk Kim }
2243119ee914SJaegeuk Kim 
2244119ee914SJaegeuk Kim static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
2245119ee914SJaegeuk Kim {
2246aaec2b1dSChao Yu 	return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
2247aaec2b1dSChao Yu 			is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
2248119ee914SJaegeuk Kim }
2249119ee914SJaegeuk Kim 
225039a53e0cSJaegeuk Kim /*
225139a53e0cSJaegeuk Kim  * Check whether the inode has blocks or not
225239a53e0cSJaegeuk Kim  */
225339a53e0cSJaegeuk Kim static inline int F2FS_HAS_BLOCKS(struct inode *inode)
225439a53e0cSJaegeuk Kim {
22550eb0adadSChao Yu 	block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
22560eb0adadSChao Yu 
2257000519f2SChao Yu 	return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block;
225839a53e0cSJaegeuk Kim }
225939a53e0cSJaegeuk Kim 
22604bc8e9bcSChao Yu static inline bool f2fs_has_xattr_block(unsigned int ofs)
22614bc8e9bcSChao Yu {
22624bc8e9bcSChao Yu 	return ofs == XATTR_NODE_OFFSET;
22634bc8e9bcSChao Yu }
22644bc8e9bcSChao Yu 
2265d8a9a229SJaegeuk Kim static inline bool __allow_reserved_blocks(struct f2fs_sb_info *sbi,
2266a90a0884SJaegeuk Kim 					struct inode *inode, bool cap)
22677c2e5963SJaegeuk Kim {
2268d8a9a229SJaegeuk Kim 	if (!inode)
2269d8a9a229SJaegeuk Kim 		return true;
22707c2e5963SJaegeuk Kim 	if (!test_opt(sbi, RESERVE_ROOT))
22717c2e5963SJaegeuk Kim 		return false;
2272d8a9a229SJaegeuk Kim 	if (IS_NOQUOTA(inode))
2273d8a9a229SJaegeuk Kim 		return true;
227463189b78SChao Yu 	if (uid_eq(F2FS_OPTION(sbi).s_resuid, current_fsuid()))
22757c2e5963SJaegeuk Kim 		return true;
227663189b78SChao Yu 	if (!gid_eq(F2FS_OPTION(sbi).s_resgid, GLOBAL_ROOT_GID) &&
227763189b78SChao Yu 					in_group_p(F2FS_OPTION(sbi).s_resgid))
22787c2e5963SJaegeuk Kim 		return true;
2279a90a0884SJaegeuk Kim 	if (cap && capable(CAP_SYS_RESOURCE))
2280162b27aeSJaegeuk Kim 		return true;
22817c2e5963SJaegeuk Kim 	return false;
22827c2e5963SJaegeuk Kim }
22837c2e5963SJaegeuk Kim 
22840abd675eSChao Yu static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool);
22850abd675eSChao Yu static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
228646008c6dSChao Yu 				 struct inode *inode, blkcnt_t *count)
228739a53e0cSJaegeuk Kim {
22880abd675eSChao Yu 	blkcnt_t diff = 0, release = 0;
2289daeb433eSChao Yu 	block_t avail_user_block_count;
22900abd675eSChao Yu 	int ret;
22910abd675eSChao Yu 
22920abd675eSChao Yu 	ret = dquot_reserve_block(inode, *count);
22930abd675eSChao Yu 	if (ret)
22940abd675eSChao Yu 		return ret;
2295dd11a5dfSJaegeuk Kim 
229655523519SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
2297c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_BLOCK);
22980abd675eSChao Yu 		release = *count;
22999ea2f0beSChao Yu 		goto release_quota;
230055523519SChao Yu 	}
23017fa750a1SArnd Bergmann 
2302dd11a5dfSJaegeuk Kim 	/*
2303dd11a5dfSJaegeuk Kim 	 * let's increase this in prior to actual block count change in order
2304dd11a5dfSJaegeuk Kim 	 * for f2fs_sync_file to avoid data races when deciding checkpoint.
2305dd11a5dfSJaegeuk Kim 	 */
2306dd11a5dfSJaegeuk Kim 	percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
230739a53e0cSJaegeuk Kim 
230839a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
23092555a2d5SJaegeuk Kim 	sbi->total_valid_block_count += (block_t)(*count);
231080d42145SYunlong Song 	avail_user_block_count = sbi->user_block_count -
231180d42145SYunlong Song 					sbi->current_reserved_blocks;
23127e65be49SJaegeuk Kim 
2313a90a0884SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode, true))
231463189b78SChao Yu 		avail_user_block_count -= F2FS_OPTION(sbi).root_reserved_blocks;
2315300a8429SChao Yu 
2316300a8429SChao Yu 	if (F2FS_IO_ALIGNED(sbi))
2317300a8429SChao Yu 		avail_user_block_count -= sbi->blocks_per_seg *
2318300a8429SChao Yu 				SM_I(sbi)->additional_reserved_segments;
2319300a8429SChao Yu 
2320a4c3ecaaSDaniel Rosenberg 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2321a4c3ecaaSDaniel Rosenberg 		if (avail_user_block_count > sbi->unusable_block_count)
23224354994fSDaniel Rosenberg 			avail_user_block_count -= sbi->unusable_block_count;
2323a4c3ecaaSDaniel Rosenberg 		else
2324a4c3ecaaSDaniel Rosenberg 			avail_user_block_count = 0;
2325a4c3ecaaSDaniel Rosenberg 	}
2326daeb433eSChao Yu 	if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
2327daeb433eSChao Yu 		diff = sbi->total_valid_block_count - avail_user_block_count;
23287e65be49SJaegeuk Kim 		if (diff > *count)
23297e65be49SJaegeuk Kim 			diff = *count;
2330dd11a5dfSJaegeuk Kim 		*count -= diff;
23310abd675eSChao Yu 		release = diff;
23327e65be49SJaegeuk Kim 		sbi->total_valid_block_count -= diff;
233346008c6dSChao Yu 		if (!*count) {
233439a53e0cSJaegeuk Kim 			spin_unlock(&sbi->stat_lock);
23350abd675eSChao Yu 			goto enospc;
233639a53e0cSJaegeuk Kim 		}
233746008c6dSChao Yu 	}
233839a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
233941382ec4SJaegeuk Kim 
234036b877afSDaniel Rosenberg 	if (unlikely(release)) {
234136b877afSDaniel Rosenberg 		percpu_counter_sub(&sbi->alloc_valid_block_count, release);
23420abd675eSChao Yu 		dquot_release_reservation_block(inode, release);
234336b877afSDaniel Rosenberg 	}
23440abd675eSChao Yu 	f2fs_i_blocks_write(inode, *count, true, true);
23450abd675eSChao Yu 	return 0;
23460abd675eSChao Yu 
23470abd675eSChao Yu enospc:
234836b877afSDaniel Rosenberg 	percpu_counter_sub(&sbi->alloc_valid_block_count, release);
23499ea2f0beSChao Yu release_quota:
23500abd675eSChao Yu 	dquot_release_reservation_block(inode, release);
23510abd675eSChao Yu 	return -ENOSPC;
235239a53e0cSJaegeuk Kim }
235339a53e0cSJaegeuk Kim 
2354dcbb4c10SJoe Perches __printf(2, 3)
2355dcbb4c10SJoe Perches void f2fs_printk(struct f2fs_sb_info *sbi, const char *fmt, ...);
2356dcbb4c10SJoe Perches 
2357dcbb4c10SJoe Perches #define f2fs_err(sbi, fmt, ...)						\
2358dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_ERR fmt, ##__VA_ARGS__)
2359dcbb4c10SJoe Perches #define f2fs_warn(sbi, fmt, ...)					\
2360dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_WARNING fmt, ##__VA_ARGS__)
2361dcbb4c10SJoe Perches #define f2fs_notice(sbi, fmt, ...)					\
2362dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_NOTICE fmt, ##__VA_ARGS__)
2363dcbb4c10SJoe Perches #define f2fs_info(sbi, fmt, ...)					\
2364dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_INFO fmt, ##__VA_ARGS__)
2365dcbb4c10SJoe Perches #define f2fs_debug(sbi, fmt, ...)					\
2366dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_DEBUG fmt, ##__VA_ARGS__)
2367dcbb4c10SJoe Perches 
2368da19b0dcSGu Zheng static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
236939a53e0cSJaegeuk Kim 						struct inode *inode,
23700eb0adadSChao Yu 						block_t count)
237139a53e0cSJaegeuk Kim {
23720eb0adadSChao Yu 	blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK;
23730eb0adadSChao Yu 
237439a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
23759850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
237639a53e0cSJaegeuk Kim 	sbi->total_valid_block_count -= (block_t)count;
237780d42145SYunlong Song 	if (sbi->reserved_blocks &&
237880d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
237980d42145SYunlong Song 		sbi->current_reserved_blocks = min(sbi->reserved_blocks,
238080d42145SYunlong Song 					sbi->current_reserved_blocks + count);
238139a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
23825e159cd3SChao Yu 	if (unlikely(inode->i_blocks < sectors)) {
2383dcbb4c10SJoe Perches 		f2fs_warn(sbi, "Inconsistent i_blocks, ino:%lu, iblocks:%llu, sectors:%llu",
23845e159cd3SChao Yu 			  inode->i_ino,
23855e159cd3SChao Yu 			  (unsigned long long)inode->i_blocks,
23865e159cd3SChao Yu 			  (unsigned long long)sectors);
23875e159cd3SChao Yu 		set_sbi_flag(sbi, SBI_NEED_FSCK);
23885e159cd3SChao Yu 		return;
23895e159cd3SChao Yu 	}
23900abd675eSChao Yu 	f2fs_i_blocks_write(inode, count, false, true);
239139a53e0cSJaegeuk Kim }
239239a53e0cSJaegeuk Kim 
239339a53e0cSJaegeuk Kim static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
239439a53e0cSJaegeuk Kim {
239535782b23SJaegeuk Kim 	atomic_inc(&sbi->nr_pages[count_type]);
23967c4abcbeSChao Yu 
239720109873SChao Yu 	if (count_type == F2FS_DIRTY_DENTS ||
239820109873SChao Yu 			count_type == F2FS_DIRTY_NODES ||
239920109873SChao Yu 			count_type == F2FS_DIRTY_META ||
240020109873SChao Yu 			count_type == F2FS_DIRTY_QDATA ||
240120109873SChao Yu 			count_type == F2FS_DIRTY_IMETA)
2402caf0047eSChao Yu 		set_sbi_flag(sbi, SBI_IS_DIRTY);
240339a53e0cSJaegeuk Kim }
240439a53e0cSJaegeuk Kim 
2405a7ffdbe2SJaegeuk Kim static inline void inode_inc_dirty_pages(struct inode *inode)
240639a53e0cSJaegeuk Kim {
2407204706c7SJaegeuk Kim 	atomic_inc(&F2FS_I(inode)->dirty_pages);
2408c227f912SChao Yu 	inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
2409c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
24102c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
24112c8a4a28SJaegeuk Kim 		inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
241239a53e0cSJaegeuk Kim }
241339a53e0cSJaegeuk Kim 
241439a53e0cSJaegeuk Kim static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
241539a53e0cSJaegeuk Kim {
241635782b23SJaegeuk Kim 	atomic_dec(&sbi->nr_pages[count_type]);
241739a53e0cSJaegeuk Kim }
241839a53e0cSJaegeuk Kim 
2419a7ffdbe2SJaegeuk Kim static inline void inode_dec_dirty_pages(struct inode *inode)
242039a53e0cSJaegeuk Kim {
24215ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
24225ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
24231fe54f9dSJaegeuk Kim 		return;
24241fe54f9dSJaegeuk Kim 
2425204706c7SJaegeuk Kim 	atomic_dec(&F2FS_I(inode)->dirty_pages);
2426c227f912SChao Yu 	dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
2427c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
24282c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
24292c8a4a28SJaegeuk Kim 		dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
243039a53e0cSJaegeuk Kim }
243139a53e0cSJaegeuk Kim 
2432523be8a6SJaegeuk Kim static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
243339a53e0cSJaegeuk Kim {
243435782b23SJaegeuk Kim 	return atomic_read(&sbi->nr_pages[count_type]);
243539a53e0cSJaegeuk Kim }
243639a53e0cSJaegeuk Kim 
2437204706c7SJaegeuk Kim static inline int get_dirty_pages(struct inode *inode)
2438f8b2c1f9SJaegeuk Kim {
2439204706c7SJaegeuk Kim 	return atomic_read(&F2FS_I(inode)->dirty_pages);
2440f8b2c1f9SJaegeuk Kim }
2441f8b2c1f9SJaegeuk Kim 
24425ac206cfSNamjae Jeon static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
24435ac206cfSNamjae Jeon {
24443519e3f9SChao Yu 	unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
2445523be8a6SJaegeuk Kim 	unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
2446523be8a6SJaegeuk Kim 						sbi->log_blocks_per_seg;
2447523be8a6SJaegeuk Kim 
2448523be8a6SJaegeuk Kim 	return segs / sbi->segs_per_sec;
24495ac206cfSNamjae Jeon }
24505ac206cfSNamjae Jeon 
245139a53e0cSJaegeuk Kim static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
245239a53e0cSJaegeuk Kim {
24538b8343faSJaegeuk Kim 	return sbi->total_valid_block_count;
245439a53e0cSJaegeuk Kim }
245539a53e0cSJaegeuk Kim 
2456f83a2584SYunlei He static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
2457f83a2584SYunlei He {
2458f83a2584SYunlei He 	return sbi->discard_blks;
2459f83a2584SYunlei He }
2460f83a2584SYunlei He 
246139a53e0cSJaegeuk Kim static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
246239a53e0cSJaegeuk Kim {
246339a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
246439a53e0cSJaegeuk Kim 
246539a53e0cSJaegeuk Kim 	/* return NAT or SIT bitmap */
246639a53e0cSJaegeuk Kim 	if (flag == NAT_BITMAP)
246739a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
246839a53e0cSJaegeuk Kim 	else if (flag == SIT_BITMAP)
246939a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
247039a53e0cSJaegeuk Kim 
247139a53e0cSJaegeuk Kim 	return 0;
247239a53e0cSJaegeuk Kim }
247339a53e0cSJaegeuk Kim 
247455141486SWanpeng Li static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
247555141486SWanpeng Li {
247655141486SWanpeng Li 	return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
247755141486SWanpeng Li }
247855141486SWanpeng Li 
247939a53e0cSJaegeuk Kim static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
248039a53e0cSJaegeuk Kim {
248139a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
24824260c406SGustavo A. R. Silva 	void *tmp_ptr = &ckpt->sit_nat_version_bitmap;
24831dbe4152SChangman Lee 	int offset;
24841dbe4152SChangman Lee 
2485199bc3feSChao Yu 	if (is_set_ckpt_flags(sbi, CP_LARGE_NAT_BITMAP_FLAG)) {
2486199bc3feSChao Yu 		offset = (flag == SIT_BITMAP) ?
2487199bc3feSChao Yu 			le32_to_cpu(ckpt->nat_ver_bitmap_bytesize) : 0;
2488b471eb99SChao Yu 		/*
2489b471eb99SChao Yu 		 * if large_nat_bitmap feature is enabled, leave checksum
2490b471eb99SChao Yu 		 * protection for all nat/sit bitmaps.
2491b471eb99SChao Yu 		 */
24924260c406SGustavo A. R. Silva 		return tmp_ptr + offset + sizeof(__le32);
2493199bc3feSChao Yu 	}
2494199bc3feSChao Yu 
249555141486SWanpeng Li 	if (__cp_payload(sbi) > 0) {
24961dbe4152SChangman Lee 		if (flag == NAT_BITMAP)
24971dbe4152SChangman Lee 			return &ckpt->sit_nat_version_bitmap;
24981dbe4152SChangman Lee 		else
249965b85cccSJaegeuk Kim 			return (unsigned char *)ckpt + F2FS_BLKSIZE;
25001dbe4152SChangman Lee 	} else {
25011dbe4152SChangman Lee 		offset = (flag == NAT_BITMAP) ?
250225ca923bSJaegeuk Kim 			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
25034260c406SGustavo A. R. Silva 		return tmp_ptr + offset;
250439a53e0cSJaegeuk Kim 	}
25051dbe4152SChangman Lee }
250639a53e0cSJaegeuk Kim 
250739a53e0cSJaegeuk Kim static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
250839a53e0cSJaegeuk Kim {
25098508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
251039a53e0cSJaegeuk Kim 
25118508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 2)
251239a53e0cSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
251339a53e0cSJaegeuk Kim 	return start_addr;
251439a53e0cSJaegeuk Kim }
251539a53e0cSJaegeuk Kim 
25168508e44aSJaegeuk Kim static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
25178508e44aSJaegeuk Kim {
25188508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
25198508e44aSJaegeuk Kim 
25208508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 1)
25218508e44aSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
25228508e44aSJaegeuk Kim 	return start_addr;
25238508e44aSJaegeuk Kim }
25248508e44aSJaegeuk Kim 
25258508e44aSJaegeuk Kim static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
25268508e44aSJaegeuk Kim {
25278508e44aSJaegeuk Kim 	sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
25288508e44aSJaegeuk Kim }
25298508e44aSJaegeuk Kim 
253039a53e0cSJaegeuk Kim static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
253139a53e0cSJaegeuk Kim {
253239a53e0cSJaegeuk Kim 	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
253339a53e0cSJaegeuk Kim }
253439a53e0cSJaegeuk Kim 
25350abd675eSChao Yu static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
2536000519f2SChao Yu 					struct inode *inode, bool is_inode)
253739a53e0cSJaegeuk Kim {
253839a53e0cSJaegeuk Kim 	block_t	valid_block_count;
2539a4c3ecaaSDaniel Rosenberg 	unsigned int valid_node_count, user_block_count;
2540af033b2aSChao Yu 	int err;
25410abd675eSChao Yu 
2542af033b2aSChao Yu 	if (is_inode) {
2543af033b2aSChao Yu 		if (inode) {
2544af033b2aSChao Yu 			err = dquot_alloc_inode(inode);
2545af033b2aSChao Yu 			if (err)
2546af033b2aSChao Yu 				return err;
2547af033b2aSChao Yu 		}
2548af033b2aSChao Yu 	} else {
2549af033b2aSChao Yu 		err = dquot_reserve_block(inode, 1);
2550af033b2aSChao Yu 		if (err)
2551af033b2aSChao Yu 			return err;
25520abd675eSChao Yu 	}
255339a53e0cSJaegeuk Kim 
2554812c6056SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
2555c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_BLOCK);
2556812c6056SChao Yu 		goto enospc;
2557812c6056SChao Yu 	}
2558812c6056SChao Yu 
255939a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
256039a53e0cSJaegeuk Kim 
25617e65be49SJaegeuk Kim 	valid_block_count = sbi->total_valid_block_count +
25627e65be49SJaegeuk Kim 					sbi->current_reserved_blocks + 1;
25637e65be49SJaegeuk Kim 
2564a90a0884SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode, false))
256563189b78SChao Yu 		valid_block_count += F2FS_OPTION(sbi).root_reserved_blocks;
2566300a8429SChao Yu 
2567300a8429SChao Yu 	if (F2FS_IO_ALIGNED(sbi))
2568300a8429SChao Yu 		valid_block_count += sbi->blocks_per_seg *
2569300a8429SChao Yu 				SM_I(sbi)->additional_reserved_segments;
2570300a8429SChao Yu 
2571a4c3ecaaSDaniel Rosenberg 	user_block_count = sbi->user_block_count;
25724354994fSDaniel Rosenberg 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2573a4c3ecaaSDaniel Rosenberg 		user_block_count -= sbi->unusable_block_count;
25747e65be49SJaegeuk Kim 
2575a4c3ecaaSDaniel Rosenberg 	if (unlikely(valid_block_count > user_block_count)) {
257639a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
25770abd675eSChao Yu 		goto enospc;
257839a53e0cSJaegeuk Kim 	}
257939a53e0cSJaegeuk Kim 
2580ef86d709SGu Zheng 	valid_node_count = sbi->total_valid_node_count + 1;
2581cfb271d4SChao Yu 	if (unlikely(valid_node_count > sbi->total_node_count)) {
258239a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
25830abd675eSChao Yu 		goto enospc;
258439a53e0cSJaegeuk Kim 	}
258539a53e0cSJaegeuk Kim 
2586ef86d709SGu Zheng 	sbi->total_valid_node_count++;
2587ef86d709SGu Zheng 	sbi->total_valid_block_count++;
258839a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
258939a53e0cSJaegeuk Kim 
25900abd675eSChao Yu 	if (inode) {
25910abd675eSChao Yu 		if (is_inode)
25920abd675eSChao Yu 			f2fs_mark_inode_dirty_sync(inode, true);
25930abd675eSChao Yu 		else
25940abd675eSChao Yu 			f2fs_i_blocks_write(inode, 1, true, true);
25950abd675eSChao Yu 	}
25960abd675eSChao Yu 
259741382ec4SJaegeuk Kim 	percpu_counter_inc(&sbi->alloc_valid_block_count);
25980abd675eSChao Yu 	return 0;
25990abd675eSChao Yu 
26000abd675eSChao Yu enospc:
2601af033b2aSChao Yu 	if (is_inode) {
2602af033b2aSChao Yu 		if (inode)
2603af033b2aSChao Yu 			dquot_free_inode(inode);
2604af033b2aSChao Yu 	} else {
26050abd675eSChao Yu 		dquot_release_reservation_block(inode, 1);
2606af033b2aSChao Yu 	}
26070abd675eSChao Yu 	return -ENOSPC;
260839a53e0cSJaegeuk Kim }
260939a53e0cSJaegeuk Kim 
261039a53e0cSJaegeuk Kim static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
2611000519f2SChao Yu 					struct inode *inode, bool is_inode)
261239a53e0cSJaegeuk Kim {
261339a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
261439a53e0cSJaegeuk Kim 
26159850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_block_count);
26169850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_node_count);
261739a53e0cSJaegeuk Kim 
2618ef86d709SGu Zheng 	sbi->total_valid_node_count--;
2619ef86d709SGu Zheng 	sbi->total_valid_block_count--;
262080d42145SYunlong Song 	if (sbi->reserved_blocks &&
262180d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
262280d42145SYunlong Song 		sbi->current_reserved_blocks++;
262339a53e0cSJaegeuk Kim 
262439a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
26250abd675eSChao Yu 
2626ea6d7e72SChao Yu 	if (is_inode) {
2627af033b2aSChao Yu 		dquot_free_inode(inode);
2628ea6d7e72SChao Yu 	} else {
2629ea6d7e72SChao Yu 		if (unlikely(inode->i_blocks == 0)) {
2630097a7686SChao Yu 			f2fs_warn(sbi, "dec_valid_node_count: inconsistent i_blocks, ino:%lu, iblocks:%llu",
2631ea6d7e72SChao Yu 				  inode->i_ino,
2632ea6d7e72SChao Yu 				  (unsigned long long)inode->i_blocks);
2633ea6d7e72SChao Yu 			set_sbi_flag(sbi, SBI_NEED_FSCK);
2634ea6d7e72SChao Yu 			return;
2635ea6d7e72SChao Yu 		}
26360abd675eSChao Yu 		f2fs_i_blocks_write(inode, 1, false, true);
263739a53e0cSJaegeuk Kim 	}
2638ea6d7e72SChao Yu }
263939a53e0cSJaegeuk Kim 
264039a53e0cSJaegeuk Kim static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
264139a53e0cSJaegeuk Kim {
26428b8343faSJaegeuk Kim 	return sbi->total_valid_node_count;
264339a53e0cSJaegeuk Kim }
264439a53e0cSJaegeuk Kim 
264539a53e0cSJaegeuk Kim static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
264639a53e0cSJaegeuk Kim {
2647513c5f37SJaegeuk Kim 	percpu_counter_inc(&sbi->total_valid_inode_count);
264839a53e0cSJaegeuk Kim }
264939a53e0cSJaegeuk Kim 
26500e80220aSJaegeuk Kim static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
265139a53e0cSJaegeuk Kim {
2652513c5f37SJaegeuk Kim 	percpu_counter_dec(&sbi->total_valid_inode_count);
265339a53e0cSJaegeuk Kim }
265439a53e0cSJaegeuk Kim 
2655513c5f37SJaegeuk Kim static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
265639a53e0cSJaegeuk Kim {
2657513c5f37SJaegeuk Kim 	return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
265839a53e0cSJaegeuk Kim }
265939a53e0cSJaegeuk Kim 
2660a56c7c6fSJaegeuk Kim static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
2661a56c7c6fSJaegeuk Kim 						pgoff_t index, bool for_write)
2662a56c7c6fSJaegeuk Kim {
266382cf4f13SChao Yu 	struct page *page;
2664cac5a3d8SDongOh Shin 
26657fa750a1SArnd Bergmann 	if (IS_ENABLED(CONFIG_F2FS_FAULT_INJECTION)) {
266682cf4f13SChao Yu 		if (!for_write)
266782cf4f13SChao Yu 			page = find_get_page_flags(mapping, index,
266882cf4f13SChao Yu 							FGP_LOCK | FGP_ACCESSED);
266982cf4f13SChao Yu 		else
267082cf4f13SChao Yu 			page = find_lock_page(mapping, index);
2671c41f3cc3SJaegeuk Kim 		if (page)
2672c41f3cc3SJaegeuk Kim 			return page;
2673c41f3cc3SJaegeuk Kim 
267455523519SChao Yu 		if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) {
2675c45d6002SChao Yu 			f2fs_show_injection_info(F2FS_M_SB(mapping),
2676c45d6002SChao Yu 							FAULT_PAGE_ALLOC);
2677c41f3cc3SJaegeuk Kim 			return NULL;
267855523519SChao Yu 		}
26797fa750a1SArnd Bergmann 	}
26807fa750a1SArnd Bergmann 
2681a56c7c6fSJaegeuk Kim 	if (!for_write)
2682a56c7c6fSJaegeuk Kim 		return grab_cache_page(mapping, index);
2683a56c7c6fSJaegeuk Kim 	return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
2684a56c7c6fSJaegeuk Kim }
2685a56c7c6fSJaegeuk Kim 
268601eccef7SChao Yu static inline struct page *f2fs_pagecache_get_page(
268701eccef7SChao Yu 				struct address_space *mapping, pgoff_t index,
268801eccef7SChao Yu 				int fgp_flags, gfp_t gfp_mask)
268901eccef7SChao Yu {
269001eccef7SChao Yu 	if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_GET)) {
2691c45d6002SChao Yu 		f2fs_show_injection_info(F2FS_M_SB(mapping), FAULT_PAGE_GET);
269201eccef7SChao Yu 		return NULL;
269301eccef7SChao Yu 	}
26947fa750a1SArnd Bergmann 
269501eccef7SChao Yu 	return pagecache_get_page(mapping, index, fgp_flags, gfp_mask);
269601eccef7SChao Yu }
269701eccef7SChao Yu 
26986e2c64adSJaegeuk Kim static inline void f2fs_copy_page(struct page *src, struct page *dst)
26996e2c64adSJaegeuk Kim {
27006e2c64adSJaegeuk Kim 	char *src_kaddr = kmap(src);
27016e2c64adSJaegeuk Kim 	char *dst_kaddr = kmap(dst);
27026e2c64adSJaegeuk Kim 
27036e2c64adSJaegeuk Kim 	memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
27046e2c64adSJaegeuk Kim 	kunmap(dst);
27056e2c64adSJaegeuk Kim 	kunmap(src);
27066e2c64adSJaegeuk Kim }
27076e2c64adSJaegeuk Kim 
270839a53e0cSJaegeuk Kim static inline void f2fs_put_page(struct page *page, int unlock)
270939a53e0cSJaegeuk Kim {
2710031fa8ccSJaegeuk Kim 	if (!page)
271139a53e0cSJaegeuk Kim 		return;
271239a53e0cSJaegeuk Kim 
271339a53e0cSJaegeuk Kim 	if (unlock) {
27149850cf4aSJaegeuk Kim 		f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
271539a53e0cSJaegeuk Kim 		unlock_page(page);
271639a53e0cSJaegeuk Kim 	}
271709cbfeafSKirill A. Shutemov 	put_page(page);
271839a53e0cSJaegeuk Kim }
271939a53e0cSJaegeuk Kim 
272039a53e0cSJaegeuk Kim static inline void f2fs_put_dnode(struct dnode_of_data *dn)
272139a53e0cSJaegeuk Kim {
272239a53e0cSJaegeuk Kim 	if (dn->node_page)
272339a53e0cSJaegeuk Kim 		f2fs_put_page(dn->node_page, 1);
272439a53e0cSJaegeuk Kim 	if (dn->inode_page && dn->node_page != dn->inode_page)
272539a53e0cSJaegeuk Kim 		f2fs_put_page(dn->inode_page, 0);
272639a53e0cSJaegeuk Kim 	dn->node_page = NULL;
272739a53e0cSJaegeuk Kim 	dn->inode_page = NULL;
272839a53e0cSJaegeuk Kim }
272939a53e0cSJaegeuk Kim 
273039a53e0cSJaegeuk Kim static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
2731e8512d2eSGu Zheng 					size_t size)
273239a53e0cSJaegeuk Kim {
2733e8512d2eSGu Zheng 	return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
273439a53e0cSJaegeuk Kim }
273539a53e0cSJaegeuk Kim 
273632410577SChao Yu static inline void *f2fs_kmem_cache_alloc_nofail(struct kmem_cache *cachep,
27377bd59381SGu Zheng 						gfp_t flags)
27387bd59381SGu Zheng {
27397bd59381SGu Zheng 	void *entry;
27407bd59381SGu Zheng 
274180c54505SJaegeuk Kim 	entry = kmem_cache_alloc(cachep, flags);
274280c54505SJaegeuk Kim 	if (!entry)
274380c54505SJaegeuk Kim 		entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
27447bd59381SGu Zheng 	return entry;
27457bd59381SGu Zheng }
27467bd59381SGu Zheng 
274732410577SChao Yu static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
274832410577SChao Yu 			gfp_t flags, bool nofail, struct f2fs_sb_info *sbi)
274932410577SChao Yu {
275032410577SChao Yu 	if (nofail)
275132410577SChao Yu 		return f2fs_kmem_cache_alloc_nofail(cachep, flags);
275232410577SChao Yu 
275332410577SChao Yu 	if (time_to_inject(sbi, FAULT_SLAB_ALLOC)) {
275432410577SChao Yu 		f2fs_show_injection_info(sbi, FAULT_SLAB_ALLOC);
275532410577SChao Yu 		return NULL;
275632410577SChao Yu 	}
275732410577SChao Yu 
275832410577SChao Yu 	return kmem_cache_alloc(cachep, flags);
275932410577SChao Yu }
276032410577SChao Yu 
2761493720a4SChao Yu static inline bool is_inflight_io(struct f2fs_sb_info *sbi, int type)
27625f9abab4SJaegeuk Kim {
27635f9abab4SJaegeuk Kim 	if (get_pages(sbi, F2FS_RD_DATA) || get_pages(sbi, F2FS_RD_NODE) ||
27645f9abab4SJaegeuk Kim 		get_pages(sbi, F2FS_RD_META) || get_pages(sbi, F2FS_WB_DATA) ||
2765fef4129eSChao Yu 		get_pages(sbi, F2FS_WB_CP_DATA) ||
2766fef4129eSChao Yu 		get_pages(sbi, F2FS_DIO_READ) ||
276711ac8ef8SJaegeuk Kim 		get_pages(sbi, F2FS_DIO_WRITE))
2768493720a4SChao Yu 		return true;
276911ac8ef8SJaegeuk Kim 
277056659ce8SSahitya Tummala 	if (type != DISCARD_TIME && SM_I(sbi) && SM_I(sbi)->dcc_info &&
277111ac8ef8SJaegeuk Kim 			atomic_read(&SM_I(sbi)->dcc_info->queued_discard))
2772493720a4SChao Yu 		return true;
277311ac8ef8SJaegeuk Kim 
277411ac8ef8SJaegeuk Kim 	if (SM_I(sbi) && SM_I(sbi)->fcc_info &&
277572691af6SJaegeuk Kim 			atomic_read(&SM_I(sbi)->fcc_info->queued_flush))
2776493720a4SChao Yu 		return true;
2777493720a4SChao Yu 	return false;
2778493720a4SChao Yu }
2779493720a4SChao Yu 
2780493720a4SChao Yu static inline bool is_idle(struct f2fs_sb_info *sbi, int type)
2781493720a4SChao Yu {
2782493720a4SChao Yu 	if (sbi->gc_mode == GC_URGENT_HIGH)
2783493720a4SChao Yu 		return true;
2784493720a4SChao Yu 
2785493720a4SChao Yu 	if (is_inflight_io(sbi, type))
27865f9abab4SJaegeuk Kim 		return false;
278711ac8ef8SJaegeuk Kim 
2788*d98af5f4SDaeho Jeong 	if (sbi->gc_mode == GC_URGENT_MID)
2789*d98af5f4SDaeho Jeong 		return true;
2790*d98af5f4SDaeho Jeong 
27910e5e8111SDaeho Jeong 	if (sbi->gc_mode == GC_URGENT_LOW &&
27920e5e8111SDaeho Jeong 			(type == DISCARD_TIME || type == GC_TIME))
27930e5e8111SDaeho Jeong 		return true;
27940e5e8111SDaeho Jeong 
27955f9abab4SJaegeuk Kim 	return f2fs_time_over(sbi, type);
27965f9abab4SJaegeuk Kim }
27975f9abab4SJaegeuk Kim 
27989be32d72SJaegeuk Kim static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
27999be32d72SJaegeuk Kim 				unsigned long index, void *item)
28009be32d72SJaegeuk Kim {
28019be32d72SJaegeuk Kim 	while (radix_tree_insert(root, index, item))
28029be32d72SJaegeuk Kim 		cond_resched();
28039be32d72SJaegeuk Kim }
28049be32d72SJaegeuk Kim 
280539a53e0cSJaegeuk Kim #define RAW_IS_INODE(p)	((p)->footer.nid == (p)->footer.ino)
280639a53e0cSJaegeuk Kim 
280739a53e0cSJaegeuk Kim static inline bool IS_INODE(struct page *page)
280839a53e0cSJaegeuk Kim {
280945590710SGu Zheng 	struct f2fs_node *p = F2FS_NODE(page);
2810cac5a3d8SDongOh Shin 
281139a53e0cSJaegeuk Kim 	return RAW_IS_INODE(p);
281239a53e0cSJaegeuk Kim }
281339a53e0cSJaegeuk Kim 
28147a2af766SChao Yu static inline int offset_in_addr(struct f2fs_inode *i)
28157a2af766SChao Yu {
28167a2af766SChao Yu 	return (i->i_inline & F2FS_EXTRA_ATTR) ?
28177a2af766SChao Yu 			(le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0;
28187a2af766SChao Yu }
28197a2af766SChao Yu 
282039a53e0cSJaegeuk Kim static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
282139a53e0cSJaegeuk Kim {
282239a53e0cSJaegeuk Kim 	return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
282339a53e0cSJaegeuk Kim }
282439a53e0cSJaegeuk Kim 
28257a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode);
2826a2ced1ceSChao Yu static inline block_t data_blkaddr(struct inode *inode,
28277a2af766SChao Yu 			struct page *node_page, unsigned int offset)
282839a53e0cSJaegeuk Kim {
282939a53e0cSJaegeuk Kim 	struct f2fs_node *raw_node;
283039a53e0cSJaegeuk Kim 	__le32 *addr_array;
28317a2af766SChao Yu 	int base = 0;
28327a2af766SChao Yu 	bool is_inode = IS_INODE(node_page);
2833cac5a3d8SDongOh Shin 
283445590710SGu Zheng 	raw_node = F2FS_NODE(node_page);
28357a2af766SChao Yu 
2836979f492fSLiFan 	if (is_inode) {
2837979f492fSLiFan 		if (!inode)
28387a88ddb5SChao Yu 			/* from GC path only */
28397a2af766SChao Yu 			base = offset_in_addr(&raw_node->i);
2840979f492fSLiFan 		else if (f2fs_has_extra_attr(inode))
28417a2af766SChao Yu 			base = get_extra_isize(inode);
28427a2af766SChao Yu 	}
28437a2af766SChao Yu 
284439a53e0cSJaegeuk Kim 	addr_array = blkaddr_in_node(raw_node);
28457a2af766SChao Yu 	return le32_to_cpu(addr_array[base + offset]);
284639a53e0cSJaegeuk Kim }
284739a53e0cSJaegeuk Kim 
2848a2ced1ceSChao Yu static inline block_t f2fs_data_blkaddr(struct dnode_of_data *dn)
2849a2ced1ceSChao Yu {
2850a2ced1ceSChao Yu 	return data_blkaddr(dn->inode, dn->node_page, dn->ofs_in_node);
2851a2ced1ceSChao Yu }
2852a2ced1ceSChao Yu 
285339a53e0cSJaegeuk Kim static inline int f2fs_test_bit(unsigned int nr, char *addr)
285439a53e0cSJaegeuk Kim {
285539a53e0cSJaegeuk Kim 	int mask;
285639a53e0cSJaegeuk Kim 
285739a53e0cSJaegeuk Kim 	addr += (nr >> 3);
285839a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
285939a53e0cSJaegeuk Kim 	return mask & *addr;
286039a53e0cSJaegeuk Kim }
286139a53e0cSJaegeuk Kim 
2862a66cdd98SJaegeuk Kim static inline void f2fs_set_bit(unsigned int nr, char *addr)
2863a66cdd98SJaegeuk Kim {
2864a66cdd98SJaegeuk Kim 	int mask;
2865a66cdd98SJaegeuk Kim 
2866a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2867a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2868a66cdd98SJaegeuk Kim 	*addr |= mask;
2869a66cdd98SJaegeuk Kim }
2870a66cdd98SJaegeuk Kim 
2871a66cdd98SJaegeuk Kim static inline void f2fs_clear_bit(unsigned int nr, char *addr)
2872a66cdd98SJaegeuk Kim {
2873a66cdd98SJaegeuk Kim 	int mask;
2874a66cdd98SJaegeuk Kim 
2875a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2876a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2877a66cdd98SJaegeuk Kim 	*addr &= ~mask;
2878a66cdd98SJaegeuk Kim }
2879a66cdd98SJaegeuk Kim 
288052aca074SGu Zheng static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
288139a53e0cSJaegeuk Kim {
288239a53e0cSJaegeuk Kim 	int mask;
288339a53e0cSJaegeuk Kim 	int ret;
288439a53e0cSJaegeuk Kim 
288539a53e0cSJaegeuk Kim 	addr += (nr >> 3);
288639a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
288739a53e0cSJaegeuk Kim 	ret = mask & *addr;
288839a53e0cSJaegeuk Kim 	*addr |= mask;
288939a53e0cSJaegeuk Kim 	return ret;
289039a53e0cSJaegeuk Kim }
289139a53e0cSJaegeuk Kim 
289252aca074SGu Zheng static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
289339a53e0cSJaegeuk Kim {
289439a53e0cSJaegeuk Kim 	int mask;
289539a53e0cSJaegeuk Kim 	int ret;
289639a53e0cSJaegeuk Kim 
289739a53e0cSJaegeuk Kim 	addr += (nr >> 3);
289839a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
289939a53e0cSJaegeuk Kim 	ret = mask & *addr;
290039a53e0cSJaegeuk Kim 	*addr &= ~mask;
290139a53e0cSJaegeuk Kim 	return ret;
290239a53e0cSJaegeuk Kim }
290339a53e0cSJaegeuk Kim 
2904c6ac4c0eSGu Zheng static inline void f2fs_change_bit(unsigned int nr, char *addr)
2905c6ac4c0eSGu Zheng {
2906c6ac4c0eSGu Zheng 	int mask;
2907c6ac4c0eSGu Zheng 
2908c6ac4c0eSGu Zheng 	addr += (nr >> 3);
2909c6ac4c0eSGu Zheng 	mask = 1 << (7 - (nr & 0x07));
2910c6ac4c0eSGu Zheng 	*addr ^= mask;
2911c6ac4c0eSGu Zheng }
2912c6ac4c0eSGu Zheng 
291359c84408SChao Yu /*
291436098557SEric Biggers  * On-disk inode flags (f2fs_inode::i_flags)
291559c84408SChao Yu  */
29164c8ff709SChao Yu #define F2FS_COMPR_FL			0x00000004 /* Compress file */
291759c84408SChao Yu #define F2FS_SYNC_FL			0x00000008 /* Synchronous updates */
291859c84408SChao Yu #define F2FS_IMMUTABLE_FL		0x00000010 /* Immutable file */
291959c84408SChao Yu #define F2FS_APPEND_FL			0x00000020 /* writes to file may only append */
292059c84408SChao Yu #define F2FS_NODUMP_FL			0x00000040 /* do not dump file */
292159c84408SChao Yu #define F2FS_NOATIME_FL			0x00000080 /* do not update atime */
29224c8ff709SChao Yu #define F2FS_NOCOMP_FL			0x00000400 /* Don't compress */
292359c84408SChao Yu #define F2FS_INDEX_FL			0x00001000 /* hash-indexed directory */
292459c84408SChao Yu #define F2FS_DIRSYNC_FL			0x00010000 /* dirsync behaviour (directories only) */
292559c84408SChao Yu #define F2FS_PROJINHERIT_FL		0x20000000 /* Create with parents projid */
29262c2eb7a3SDaniel Rosenberg #define F2FS_CASEFOLD_FL		0x40000000 /* Casefolded file */
292759c84408SChao Yu 
292859c84408SChao Yu /* Flags that should be inherited by new inodes from their parent. */
292936098557SEric Biggers #define F2FS_FL_INHERITED (F2FS_SYNC_FL | F2FS_NODUMP_FL | F2FS_NOATIME_FL | \
29302c2eb7a3SDaniel Rosenberg 			   F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL | \
29314c8ff709SChao Yu 			   F2FS_CASEFOLD_FL | F2FS_COMPR_FL | F2FS_NOCOMP_FL)
293259c84408SChao Yu 
293359c84408SChao Yu /* Flags that are appropriate for regular files (all but dir-specific ones). */
29342c2eb7a3SDaniel Rosenberg #define F2FS_REG_FLMASK		(~(F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL | \
29352c2eb7a3SDaniel Rosenberg 				F2FS_CASEFOLD_FL))
293659c84408SChao Yu 
293759c84408SChao Yu /* Flags that are appropriate for non-directories/regular files. */
293859c84408SChao Yu #define F2FS_OTHER_FLMASK	(F2FS_NODUMP_FL | F2FS_NOATIME_FL)
29395c57132eSChao Yu 
29405c57132eSChao Yu static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
29415c57132eSChao Yu {
29425c57132eSChao Yu 	if (S_ISDIR(mode))
29435c57132eSChao Yu 		return flags;
29445c57132eSChao Yu 	else if (S_ISREG(mode))
29455c57132eSChao Yu 		return flags & F2FS_REG_FLMASK;
29465c57132eSChao Yu 	else
29475c57132eSChao Yu 		return flags & F2FS_OTHER_FLMASK;
29485c57132eSChao Yu }
29495c57132eSChao Yu 
2950205b9822SJaegeuk Kim static inline void __mark_inode_dirty_flag(struct inode *inode,
2951205b9822SJaegeuk Kim 						int flag, bool set)
295239a53e0cSJaegeuk Kim {
2953205b9822SJaegeuk Kim 	switch (flag) {
2954205b9822SJaegeuk Kim 	case FI_INLINE_XATTR:
2955205b9822SJaegeuk Kim 	case FI_INLINE_DATA:
2956205b9822SJaegeuk Kim 	case FI_INLINE_DENTRY:
29579ac1e2d8SDaeho Jeong 	case FI_NEW_INODE:
2958205b9822SJaegeuk Kim 		if (set)
2959205b9822SJaegeuk Kim 			return;
2960df561f66SGustavo A. R. Silva 		fallthrough;
2961205b9822SJaegeuk Kim 	case FI_DATA_EXIST:
2962205b9822SJaegeuk Kim 	case FI_INLINE_DOTS:
29631ad71a27SJaegeuk Kim 	case FI_PIN_FILE:
2964c6140415SJaegeuk Kim 	case FI_COMPRESS_RELEASED:
29657c45729aSJaegeuk Kim 		f2fs_mark_inode_dirty_sync(inode, true);
2966205b9822SJaegeuk Kim 	}
296739a53e0cSJaegeuk Kim }
296839a53e0cSJaegeuk Kim 
296991942321SJaegeuk Kim static inline void set_inode_flag(struct inode *inode, int flag)
297039a53e0cSJaegeuk Kim {
297158f7e00fSYufen Yu 	set_bit(flag, F2FS_I(inode)->flags);
2972205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, true);
297339a53e0cSJaegeuk Kim }
297439a53e0cSJaegeuk Kim 
297591942321SJaegeuk Kim static inline int is_inode_flag_set(struct inode *inode, int flag)
297639a53e0cSJaegeuk Kim {
29777653b9d8SChao Yu 	return test_bit(flag, F2FS_I(inode)->flags);
297839a53e0cSJaegeuk Kim }
297939a53e0cSJaegeuk Kim 
298091942321SJaegeuk Kim static inline void clear_inode_flag(struct inode *inode, int flag)
298139a53e0cSJaegeuk Kim {
298258f7e00fSYufen Yu 	clear_bit(flag, F2FS_I(inode)->flags);
2983205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, false);
298439a53e0cSJaegeuk Kim }
298539a53e0cSJaegeuk Kim 
298695ae251fSEric Biggers static inline bool f2fs_verity_in_progress(struct inode *inode)
298795ae251fSEric Biggers {
298895ae251fSEric Biggers 	return IS_ENABLED(CONFIG_FS_VERITY) &&
298995ae251fSEric Biggers 	       is_inode_flag_set(inode, FI_VERITY_IN_PROGRESS);
299095ae251fSEric Biggers }
299195ae251fSEric Biggers 
299291942321SJaegeuk Kim static inline void set_acl_inode(struct inode *inode, umode_t mode)
2993444c580fSJaegeuk Kim {
299491942321SJaegeuk Kim 	F2FS_I(inode)->i_acl_mode = mode;
299591942321SJaegeuk Kim 	set_inode_flag(inode, FI_ACL_MODE);
29967c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, false);
299739a53e0cSJaegeuk Kim }
299839a53e0cSJaegeuk Kim 
2999a1961246SJaegeuk Kim static inline void f2fs_i_links_write(struct inode *inode, bool inc)
3000a1961246SJaegeuk Kim {
3001a1961246SJaegeuk Kim 	if (inc)
3002a1961246SJaegeuk Kim 		inc_nlink(inode);
3003a1961246SJaegeuk Kim 	else
3004a1961246SJaegeuk Kim 		drop_nlink(inode);
30057c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3006a1961246SJaegeuk Kim }
3007a1961246SJaegeuk Kim 
30088edd03c8SJaegeuk Kim static inline void f2fs_i_blocks_write(struct inode *inode,
30090abd675eSChao Yu 					block_t diff, bool add, bool claim)
30108edd03c8SJaegeuk Kim {
301126de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
301226de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
301326de9b11SJaegeuk Kim 
30140abd675eSChao Yu 	/* add = 1, claim = 1 should be dquot_reserve_block in pair */
30150abd675eSChao Yu 	if (add) {
30160abd675eSChao Yu 		if (claim)
30170abd675eSChao Yu 			dquot_claim_block(inode, diff);
30180abd675eSChao Yu 		else
30190abd675eSChao Yu 			dquot_alloc_block_nofail(inode, diff);
30200abd675eSChao Yu 	} else {
30210abd675eSChao Yu 		dquot_free_block(inode, diff);
30220abd675eSChao Yu 	}
30230abd675eSChao Yu 
30247c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
302526de9b11SJaegeuk Kim 	if (clean || recover)
302626de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
30278edd03c8SJaegeuk Kim }
30288edd03c8SJaegeuk Kim 
3029fc9581c8SJaegeuk Kim static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
3030fc9581c8SJaegeuk Kim {
303126de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
303226de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
303326de9b11SJaegeuk Kim 
3034fc9581c8SJaegeuk Kim 	if (i_size_read(inode) == i_size)
3035fc9581c8SJaegeuk Kim 		return;
3036fc9581c8SJaegeuk Kim 
3037fc9581c8SJaegeuk Kim 	i_size_write(inode, i_size);
30387c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
303926de9b11SJaegeuk Kim 	if (clean || recover)
304026de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
304126de9b11SJaegeuk Kim }
304226de9b11SJaegeuk Kim 
3043205b9822SJaegeuk Kim static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
3044205b9822SJaegeuk Kim {
3045205b9822SJaegeuk Kim 	F2FS_I(inode)->i_current_depth = depth;
30467c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3047205b9822SJaegeuk Kim }
3048205b9822SJaegeuk Kim 
30491ad71a27SJaegeuk Kim static inline void f2fs_i_gc_failures_write(struct inode *inode,
30501ad71a27SJaegeuk Kim 					unsigned int count)
30511ad71a27SJaegeuk Kim {
30522ef79ecbSChao Yu 	F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN] = count;
30531ad71a27SJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
30541ad71a27SJaegeuk Kim }
30551ad71a27SJaegeuk Kim 
3056205b9822SJaegeuk Kim static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
3057205b9822SJaegeuk Kim {
3058205b9822SJaegeuk Kim 	F2FS_I(inode)->i_xattr_nid = xnid;
30597c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3060205b9822SJaegeuk Kim }
3061205b9822SJaegeuk Kim 
3062205b9822SJaegeuk Kim static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
3063205b9822SJaegeuk Kim {
3064205b9822SJaegeuk Kim 	F2FS_I(inode)->i_pino = pino;
30657c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3066205b9822SJaegeuk Kim }
3067205b9822SJaegeuk Kim 
306891942321SJaegeuk Kim static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
3069444c580fSJaegeuk Kim {
3070205b9822SJaegeuk Kim 	struct f2fs_inode_info *fi = F2FS_I(inode);
3071205b9822SJaegeuk Kim 
3072444c580fSJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_XATTR)
30737653b9d8SChao Yu 		set_bit(FI_INLINE_XATTR, fi->flags);
30741001b347SHuajun Li 	if (ri->i_inline & F2FS_INLINE_DATA)
30757653b9d8SChao Yu 		set_bit(FI_INLINE_DATA, fi->flags);
307634d67debSChao Yu 	if (ri->i_inline & F2FS_INLINE_DENTRY)
30777653b9d8SChao Yu 		set_bit(FI_INLINE_DENTRY, fi->flags);
3078b3d208f9SJaegeuk Kim 	if (ri->i_inline & F2FS_DATA_EXIST)
30797653b9d8SChao Yu 		set_bit(FI_DATA_EXIST, fi->flags);
3080510022a8SJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_DOTS)
30817653b9d8SChao Yu 		set_bit(FI_INLINE_DOTS, fi->flags);
30827a2af766SChao Yu 	if (ri->i_inline & F2FS_EXTRA_ATTR)
30837653b9d8SChao Yu 		set_bit(FI_EXTRA_ATTR, fi->flags);
30841ad71a27SJaegeuk Kim 	if (ri->i_inline & F2FS_PIN_FILE)
30857653b9d8SChao Yu 		set_bit(FI_PIN_FILE, fi->flags);
3086c6140415SJaegeuk Kim 	if (ri->i_inline & F2FS_COMPRESS_RELEASED)
3087c6140415SJaegeuk Kim 		set_bit(FI_COMPRESS_RELEASED, fi->flags);
3088444c580fSJaegeuk Kim }
3089444c580fSJaegeuk Kim 
309091942321SJaegeuk Kim static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
3091444c580fSJaegeuk Kim {
3092444c580fSJaegeuk Kim 	ri->i_inline = 0;
3093444c580fSJaegeuk Kim 
309491942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_XATTR))
3095444c580fSJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_XATTR;
309691942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DATA))
30971001b347SHuajun Li 		ri->i_inline |= F2FS_INLINE_DATA;
309891942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
309934d67debSChao Yu 		ri->i_inline |= F2FS_INLINE_DENTRY;
310091942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_DATA_EXIST))
3101b3d208f9SJaegeuk Kim 		ri->i_inline |= F2FS_DATA_EXIST;
310291942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DOTS))
3103510022a8SJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_DOTS;
31047a2af766SChao Yu 	if (is_inode_flag_set(inode, FI_EXTRA_ATTR))
31057a2af766SChao Yu 		ri->i_inline |= F2FS_EXTRA_ATTR;
31061ad71a27SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_PIN_FILE))
31071ad71a27SJaegeuk Kim 		ri->i_inline |= F2FS_PIN_FILE;
3108c6140415SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
3109c6140415SJaegeuk Kim 		ri->i_inline |= F2FS_COMPRESS_RELEASED;
31107a2af766SChao Yu }
31117a2af766SChao Yu 
31127a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode)
31137a2af766SChao Yu {
31147a2af766SChao Yu 	return is_inode_flag_set(inode, FI_EXTRA_ATTR);
3115444c580fSJaegeuk Kim }
3116444c580fSJaegeuk Kim 
3117987c7c31SChao Yu static inline int f2fs_has_inline_xattr(struct inode *inode)
3118987c7c31SChao Yu {
311991942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_XATTR);
3120987c7c31SChao Yu }
3121987c7c31SChao Yu 
31224c8ff709SChao Yu static inline int f2fs_compressed_file(struct inode *inode)
31234c8ff709SChao Yu {
31244c8ff709SChao Yu 	return S_ISREG(inode->i_mode) &&
31254c8ff709SChao Yu 		is_inode_flag_set(inode, FI_COMPRESSED_FILE);
31264c8ff709SChao Yu }
31274c8ff709SChao Yu 
3128602a16d5SDaeho Jeong static inline bool f2fs_need_compress_data(struct inode *inode)
3129602a16d5SDaeho Jeong {
3130602a16d5SDaeho Jeong 	int compress_mode = F2FS_OPTION(F2FS_I_SB(inode)).compress_mode;
3131602a16d5SDaeho Jeong 
3132602a16d5SDaeho Jeong 	if (!f2fs_compressed_file(inode))
3133602a16d5SDaeho Jeong 		return false;
3134602a16d5SDaeho Jeong 
3135602a16d5SDaeho Jeong 	if (compress_mode == COMPR_MODE_FS)
3136602a16d5SDaeho Jeong 		return true;
3137602a16d5SDaeho Jeong 	else if (compress_mode == COMPR_MODE_USER &&
3138602a16d5SDaeho Jeong 			is_inode_flag_set(inode, FI_ENABLE_COMPRESS))
3139602a16d5SDaeho Jeong 		return true;
3140602a16d5SDaeho Jeong 
3141602a16d5SDaeho Jeong 	return false;
3142602a16d5SDaeho Jeong }
3143602a16d5SDaeho Jeong 
314481ca7350SChao Yu static inline unsigned int addrs_per_inode(struct inode *inode)
3145de93653fSJaegeuk Kim {
3146d02a6e61SChao Yu 	unsigned int addrs = CUR_ADDRS_PER_INODE(inode) -
3147d02a6e61SChao Yu 				get_inline_xattr_addrs(inode);
31484c8ff709SChao Yu 
31494c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
31504c8ff709SChao Yu 		return addrs;
31514c8ff709SChao Yu 	return ALIGN_DOWN(addrs, F2FS_I(inode)->i_cluster_size);
3152d02a6e61SChao Yu }
3153d02a6e61SChao Yu 
3154d02a6e61SChao Yu static inline unsigned int addrs_per_block(struct inode *inode)
3155d02a6e61SChao Yu {
31564c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
31574c8ff709SChao Yu 		return DEF_ADDRS_PER_BLOCK;
31584c8ff709SChao Yu 	return ALIGN_DOWN(DEF_ADDRS_PER_BLOCK, F2FS_I(inode)->i_cluster_size);
3159de93653fSJaegeuk Kim }
3160de93653fSJaegeuk Kim 
31616afc662eSChao Yu static inline void *inline_xattr_addr(struct inode *inode, struct page *page)
316265985d93SJaegeuk Kim {
3163695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
3164cac5a3d8SDongOh Shin 
316565985d93SJaegeuk Kim 	return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
3166b323fd28SChao Yu 					get_inline_xattr_addrs(inode)]);
316765985d93SJaegeuk Kim }
316865985d93SJaegeuk Kim 
316965985d93SJaegeuk Kim static inline int inline_xattr_size(struct inode *inode)
317065985d93SJaegeuk Kim {
3171622927f3SChao Yu 	if (f2fs_has_inline_xattr(inode))
31726afc662eSChao Yu 		return get_inline_xattr_addrs(inode) * sizeof(__le32);
3173622927f3SChao Yu 	return 0;
317465985d93SJaegeuk Kim }
317565985d93SJaegeuk Kim 
31760dbdc2aeSJaegeuk Kim static inline int f2fs_has_inline_data(struct inode *inode)
31770dbdc2aeSJaegeuk Kim {
317891942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DATA);
31790dbdc2aeSJaegeuk Kim }
31800dbdc2aeSJaegeuk Kim 
3181b3d208f9SJaegeuk Kim static inline int f2fs_exist_data(struct inode *inode)
3182b3d208f9SJaegeuk Kim {
318391942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DATA_EXIST);
3184b3d208f9SJaegeuk Kim }
3185b3d208f9SJaegeuk Kim 
3186510022a8SJaegeuk Kim static inline int f2fs_has_inline_dots(struct inode *inode)
3187510022a8SJaegeuk Kim {
318891942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DOTS);
3189510022a8SJaegeuk Kim }
3190510022a8SJaegeuk Kim 
31914c8ff709SChao Yu static inline int f2fs_is_mmap_file(struct inode *inode)
31924c8ff709SChao Yu {
31934c8ff709SChao Yu 	return is_inode_flag_set(inode, FI_MMAP_FILE);
31944c8ff709SChao Yu }
31954c8ff709SChao Yu 
31961ad71a27SJaegeuk Kim static inline bool f2fs_is_pinned_file(struct inode *inode)
31971ad71a27SJaegeuk Kim {
31981ad71a27SJaegeuk Kim 	return is_inode_flag_set(inode, FI_PIN_FILE);
31991ad71a27SJaegeuk Kim }
32001ad71a27SJaegeuk Kim 
320188b88a66SJaegeuk Kim static inline bool f2fs_is_atomic_file(struct inode *inode)
320288b88a66SJaegeuk Kim {
320391942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_ATOMIC_FILE);
320488b88a66SJaegeuk Kim }
320588b88a66SJaegeuk Kim 
32065fe45743SChao Yu static inline bool f2fs_is_commit_atomic_write(struct inode *inode)
32075fe45743SChao Yu {
32085fe45743SChao Yu 	return is_inode_flag_set(inode, FI_ATOMIC_COMMIT);
32095fe45743SChao Yu }
32105fe45743SChao Yu 
321102a1335fSJaegeuk Kim static inline bool f2fs_is_volatile_file(struct inode *inode)
321202a1335fSJaegeuk Kim {
321391942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_VOLATILE_FILE);
321402a1335fSJaegeuk Kim }
321502a1335fSJaegeuk Kim 
32163c6c2bebSJaegeuk Kim static inline bool f2fs_is_first_block_written(struct inode *inode)
32173c6c2bebSJaegeuk Kim {
321891942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
32193c6c2bebSJaegeuk Kim }
32203c6c2bebSJaegeuk Kim 
32211e84371fSJaegeuk Kim static inline bool f2fs_is_drop_cache(struct inode *inode)
32221e84371fSJaegeuk Kim {
322391942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DROP_CACHE);
32241e84371fSJaegeuk Kim }
32251e84371fSJaegeuk Kim 
3226f2470371SChao Yu static inline void *inline_data_addr(struct inode *inode, struct page *page)
32271001b347SHuajun Li {
3228695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
32297a2af766SChao Yu 	int extra_size = get_extra_isize(inode);
3230cac5a3d8SDongOh Shin 
32317a2af766SChao Yu 	return (void *)&(ri->i_addr[extra_size + DEF_INLINE_RESERVED_SIZE]);
32321001b347SHuajun Li }
32331001b347SHuajun Li 
323434d67debSChao Yu static inline int f2fs_has_inline_dentry(struct inode *inode)
323534d67debSChao Yu {
323691942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DENTRY);
323734d67debSChao Yu }
323834d67debSChao Yu 
3239b5492af7SJaegeuk Kim static inline int is_file(struct inode *inode, int type)
3240b5492af7SJaegeuk Kim {
3241b5492af7SJaegeuk Kim 	return F2FS_I(inode)->i_advise & type;
3242b5492af7SJaegeuk Kim }
3243b5492af7SJaegeuk Kim 
3244b5492af7SJaegeuk Kim static inline void set_file(struct inode *inode, int type)
3245b5492af7SJaegeuk Kim {
3246766c6639SJaegeuk Kim 	if (is_file(inode, type))
3247766c6639SJaegeuk Kim 		return;
3248b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise |= type;
32497c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3250b5492af7SJaegeuk Kim }
3251b5492af7SJaegeuk Kim 
3252b5492af7SJaegeuk Kim static inline void clear_file(struct inode *inode, int type)
3253b5492af7SJaegeuk Kim {
3254766c6639SJaegeuk Kim 	if (!is_file(inode, type))
3255766c6639SJaegeuk Kim 		return;
3256b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise &= ~type;
32577c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
3258b5492af7SJaegeuk Kim }
3259b5492af7SJaegeuk Kim 
3260fe1897eaSChao Yu static inline bool f2fs_is_time_consistent(struct inode *inode)
3261fe1897eaSChao Yu {
3262fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time, &inode->i_atime))
3263fe1897eaSChao Yu 		return false;
3264fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 1, &inode->i_ctime))
3265fe1897eaSChao Yu 		return false;
3266fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 2, &inode->i_mtime))
3267fe1897eaSChao Yu 		return false;
3268fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 3,
3269fe1897eaSChao Yu 						&F2FS_I(inode)->i_crtime))
3270fe1897eaSChao Yu 		return false;
3271fe1897eaSChao Yu 	return true;
3272fe1897eaSChao Yu }
3273fe1897eaSChao Yu 
327426787236SJaegeuk Kim static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
327526787236SJaegeuk Kim {
3276a0d00fadSChao Yu 	bool ret;
3277a0d00fadSChao Yu 
327826787236SJaegeuk Kim 	if (dsync) {
327926787236SJaegeuk Kim 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
328026787236SJaegeuk Kim 
328126787236SJaegeuk Kim 		spin_lock(&sbi->inode_lock[DIRTY_META]);
328226787236SJaegeuk Kim 		ret = list_empty(&F2FS_I(inode)->gdirty_list);
328326787236SJaegeuk Kim 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
328426787236SJaegeuk Kim 		return ret;
328526787236SJaegeuk Kim 	}
328626787236SJaegeuk Kim 	if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
328726787236SJaegeuk Kim 			file_keep_isize(inode) ||
3288235831d7SJunling Zheng 			i_size_read(inode) & ~PAGE_MASK)
328926787236SJaegeuk Kim 		return false;
3290a0d00fadSChao Yu 
3291fe1897eaSChao Yu 	if (!f2fs_is_time_consistent(inode))
3292214c2461SJaegeuk Kim 		return false;
3293214c2461SJaegeuk Kim 
3294c10c9820SChao Yu 	spin_lock(&F2FS_I(inode)->i_size_lock);
3295a0d00fadSChao Yu 	ret = F2FS_I(inode)->last_disk_size == i_size_read(inode);
3296c10c9820SChao Yu 	spin_unlock(&F2FS_I(inode)->i_size_lock);
3297a0d00fadSChao Yu 
3298a0d00fadSChao Yu 	return ret;
3299267378d4SChao Yu }
3300267378d4SChao Yu 
3301deeedd71SChao Yu static inline bool f2fs_readonly(struct super_block *sb)
330277888c1eSJaegeuk Kim {
3303deeedd71SChao Yu 	return sb_rdonly(sb);
330477888c1eSJaegeuk Kim }
330577888c1eSJaegeuk Kim 
3306744602cfSJaegeuk Kim static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
3307744602cfSJaegeuk Kim {
3308aaec2b1dSChao Yu 	return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
3309744602cfSJaegeuk Kim }
3310744602cfSJaegeuk Kim 
331143c780baSEric Biggers static inline bool is_dot_dotdot(const u8 *name, size_t len)
3312eaa693f4SJaegeuk Kim {
331343c780baSEric Biggers 	if (len == 1 && name[0] == '.')
3314eaa693f4SJaegeuk Kim 		return true;
3315eaa693f4SJaegeuk Kim 
331643c780baSEric Biggers 	if (len == 2 && name[0] == '.' && name[1] == '.')
3317eaa693f4SJaegeuk Kim 		return true;
3318eaa693f4SJaegeuk Kim 
3319eaa693f4SJaegeuk Kim 	return false;
3320eaa693f4SJaegeuk Kim }
3321eaa693f4SJaegeuk Kim 
33221ecc0c5cSChao Yu static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
33231ecc0c5cSChao Yu 					size_t size, gfp_t flags)
33240414b004SJaegeuk Kim {
332555523519SChao Yu 	if (time_to_inject(sbi, FAULT_KMALLOC)) {
3326c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_KMALLOC);
33272c63feadSJaegeuk Kim 		return NULL;
332855523519SChao Yu 	}
33297fa750a1SArnd Bergmann 
33300b6d4ca0SEric Biggers 	return kmalloc(size, flags);
33310414b004SJaegeuk Kim }
33320414b004SJaegeuk Kim 
3333acbf054dSChao Yu static inline void *f2fs_kzalloc(struct f2fs_sb_info *sbi,
3334acbf054dSChao Yu 					size_t size, gfp_t flags)
3335acbf054dSChao Yu {
3336acbf054dSChao Yu 	return f2fs_kmalloc(sbi, size, flags | __GFP_ZERO);
3337acbf054dSChao Yu }
3338acbf054dSChao Yu 
3339628b3d14SChao Yu static inline void *f2fs_kvmalloc(struct f2fs_sb_info *sbi,
3340628b3d14SChao Yu 					size_t size, gfp_t flags)
3341628b3d14SChao Yu {
3342628b3d14SChao Yu 	if (time_to_inject(sbi, FAULT_KVMALLOC)) {
3343c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_KVMALLOC);
3344628b3d14SChao Yu 		return NULL;
3345628b3d14SChao Yu 	}
33467fa750a1SArnd Bergmann 
3347628b3d14SChao Yu 	return kvmalloc(size, flags);
3348628b3d14SChao Yu }
3349628b3d14SChao Yu 
3350628b3d14SChao Yu static inline void *f2fs_kvzalloc(struct f2fs_sb_info *sbi,
3351628b3d14SChao Yu 					size_t size, gfp_t flags)
3352628b3d14SChao Yu {
3353628b3d14SChao Yu 	return f2fs_kvmalloc(sbi, size, flags | __GFP_ZERO);
3354628b3d14SChao Yu }
3355628b3d14SChao Yu 
33567a2af766SChao Yu static inline int get_extra_isize(struct inode *inode)
3357f2470371SChao Yu {
33587a2af766SChao Yu 	return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
3359f2470371SChao Yu }
3360f2470371SChao Yu 
33616afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode)
33626afc662eSChao Yu {
33636afc662eSChao Yu 	return F2FS_I(inode)->i_inline_xattr_size;
33646afc662eSChao Yu }
33656afc662eSChao Yu 
33664d57b86dSChao Yu #define f2fs_get_inode_mode(i) \
336791942321SJaegeuk Kim 	((is_inode_flag_set(i, FI_ACL_MODE)) ? \
3368a6dda0e6SChristoph Hellwig 	 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
3369a6dda0e6SChristoph Hellwig 
33707a2af766SChao Yu #define F2FS_TOTAL_EXTRA_ATTR_SIZE			\
33717a2af766SChao Yu 	(offsetof(struct f2fs_inode, i_extra_end) -	\
33727a2af766SChao Yu 	offsetof(struct f2fs_inode, i_extra_isize))	\
33737a2af766SChao Yu 
33745c57132eSChao Yu #define F2FS_OLD_ATTRIBUTE_SIZE	(offsetof(struct f2fs_inode, i_addr))
33755c57132eSChao Yu #define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field)		\
33762f84babfSSheng Yong 		((offsetof(typeof(*(f2fs_inode)), field) +	\
33775c57132eSChao Yu 		sizeof((f2fs_inode)->field))			\
33782f84babfSSheng Yong 		<= (F2FS_OLD_ATTRIBUTE_SIZE + (extra_isize)))	\
33795c57132eSChao Yu 
33802c70c5e3SChao Yu #define __is_large_section(sbi)		((sbi)->segs_per_sec > 1)
33812c70c5e3SChao Yu 
33826dc3a126SChao Yu #define __is_meta_io(fio) (PAGE_TYPE_OF_BIO((fio)->type) == META)
3383c9b60788SChao Yu 
3384e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
3385e1da7872SChao Yu 					block_t blkaddr, int type);
3386e1da7872SChao Yu static inline void verify_blkaddr(struct f2fs_sb_info *sbi,
3387e1da7872SChao Yu 					block_t blkaddr, int type)
3388e1da7872SChao Yu {
3389e1da7872SChao Yu 	if (!f2fs_is_valid_blkaddr(sbi, blkaddr, type)) {
3390dcbb4c10SJoe Perches 		f2fs_err(sbi, "invalid blkaddr: %u, type: %d, run fsck to fix.",
3391e1da7872SChao Yu 			 blkaddr, type);
3392e1da7872SChao Yu 		f2fs_bug_on(sbi, 1);
3393e1da7872SChao Yu 	}
3394e1da7872SChao Yu }
3395e1da7872SChao Yu 
3396e1da7872SChao Yu static inline bool __is_valid_data_blkaddr(block_t blkaddr)
33977b525dd0SChao Yu {
33984c8ff709SChao Yu 	if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR ||
33994c8ff709SChao Yu 			blkaddr == COMPRESS_ADDR)
34007b525dd0SChao Yu 		return false;
34017b525dd0SChao Yu 	return true;
34027b525dd0SChao Yu }
34037b525dd0SChao Yu 
340439a53e0cSJaegeuk Kim /*
340539a53e0cSJaegeuk Kim  * file.c
340639a53e0cSJaegeuk Kim  */
3407cac5a3d8SDongOh Shin int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
34084d57b86dSChao Yu void f2fs_truncate_data_blocks(struct dnode_of_data *dn);
34093265d3dbSChao Yu int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock);
3410c42d28ceSChao Yu int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock);
3411cac5a3d8SDongOh Shin int f2fs_truncate(struct inode *inode);
3412549c7297SChristian Brauner int f2fs_getattr(struct user_namespace *mnt_userns, const struct path *path,
3413549c7297SChristian Brauner 		 struct kstat *stat, u32 request_mask, unsigned int flags);
3414549c7297SChristian Brauner int f2fs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
3415549c7297SChristian Brauner 		 struct iattr *attr);
34164d57b86dSChao Yu int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
34174d57b86dSChao Yu void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count);
3418c4020b2dSChao Yu int f2fs_precache_extents(struct inode *inode);
34199b1bb01cSMiklos Szeredi int f2fs_fileattr_get(struct dentry *dentry, struct fileattr *fa);
34209b1bb01cSMiklos Szeredi int f2fs_fileattr_set(struct user_namespace *mnt_userns,
34219b1bb01cSMiklos Szeredi 		      struct dentry *dentry, struct fileattr *fa);
3422cac5a3d8SDongOh Shin long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
3423cac5a3d8SDongOh Shin long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
342478130819SChao Yu int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid);
34251ad71a27SJaegeuk Kim int f2fs_pin_file_control(struct inode *inode, bool inc);
342639a53e0cSJaegeuk Kim 
342739a53e0cSJaegeuk Kim /*
342839a53e0cSJaegeuk Kim  * inode.c
342939a53e0cSJaegeuk Kim  */
3430cac5a3d8SDongOh Shin void f2fs_set_inode_flags(struct inode *inode);
3431704956ecSChao Yu bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page);
3432704956ecSChao Yu void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page);
3433cac5a3d8SDongOh Shin struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
3434cac5a3d8SDongOh Shin struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
34354d57b86dSChao Yu int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
34364d57b86dSChao Yu void f2fs_update_inode(struct inode *inode, struct page *node_page);
34374d57b86dSChao Yu void f2fs_update_inode_page(struct inode *inode);
3438cac5a3d8SDongOh Shin int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
3439cac5a3d8SDongOh Shin void f2fs_evict_inode(struct inode *inode);
34404d57b86dSChao Yu void f2fs_handle_failed_inode(struct inode *inode);
344139a53e0cSJaegeuk Kim 
344239a53e0cSJaegeuk Kim /*
344339a53e0cSJaegeuk Kim  * namei.c
344439a53e0cSJaegeuk Kim  */
34454d57b86dSChao Yu int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
3446b6a06cbbSChao Yu 							bool hot, bool set);
344739a53e0cSJaegeuk Kim struct dentry *f2fs_get_parent(struct dentry *child);
344839a53e0cSJaegeuk Kim 
344939a53e0cSJaegeuk Kim /*
345039a53e0cSJaegeuk Kim  * dir.c
345139a53e0cSJaegeuk Kim  */
34524d57b86dSChao Yu unsigned char f2fs_get_de_type(struct f2fs_dir_entry *de);
345343c780baSEric Biggers int f2fs_init_casefolded_name(const struct inode *dir,
345443c780baSEric Biggers 			      struct f2fs_filename *fname);
345543c780baSEric Biggers int f2fs_setup_filename(struct inode *dir, const struct qstr *iname,
345643c780baSEric Biggers 			int lookup, struct f2fs_filename *fname);
345743c780baSEric Biggers int f2fs_prepare_lookup(struct inode *dir, struct dentry *dentry,
345843c780baSEric Biggers 			struct f2fs_filename *fname);
345943c780baSEric Biggers void f2fs_free_filename(struct f2fs_filename *fname);
346043c780baSEric Biggers struct f2fs_dir_entry *f2fs_find_target_dentry(const struct f2fs_dentry_ptr *d,
346143c780baSEric Biggers 			const struct f2fs_filename *fname, int *max_slots);
3462cac5a3d8SDongOh Shin int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
3463cac5a3d8SDongOh Shin 			unsigned int start_pos, struct fscrypt_str *fstr);
34644d57b86dSChao Yu void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
3465cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
34664d57b86dSChao Yu struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
346743c780baSEric Biggers 			const struct f2fs_filename *fname, struct page *dpage);
34684d57b86dSChao Yu void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
3469cac5a3d8SDongOh Shin 			unsigned int current_depth);
34704d57b86dSChao Yu int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots);
3471cac5a3d8SDongOh Shin void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
3472cac5a3d8SDongOh Shin struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
347343c780baSEric Biggers 					 const struct f2fs_filename *fname,
347443c780baSEric Biggers 					 struct page **res_page);
3475cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
3476cac5a3d8SDongOh Shin 			const struct qstr *child, struct page **res_page);
3477cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
3478cac5a3d8SDongOh Shin ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
3479cac5a3d8SDongOh Shin 			struct page **page);
3480cac5a3d8SDongOh Shin void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
3481cac5a3d8SDongOh Shin 			struct page *page, struct inode *inode);
3482b06af2afSJaegeuk Kim bool f2fs_has_enough_room(struct inode *dir, struct page *ipage,
348343c780baSEric Biggers 			  const struct f2fs_filename *fname);
3484cac5a3d8SDongOh Shin void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
348543c780baSEric Biggers 			const struct fscrypt_str *name, f2fs_hash_t name_hash,
3486cac5a3d8SDongOh Shin 			unsigned int bit_pos);
348743c780baSEric Biggers int f2fs_add_regular_entry(struct inode *dir, const struct f2fs_filename *fname,
3488cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
348943c780baSEric Biggers int f2fs_add_dentry(struct inode *dir, const struct f2fs_filename *fname,
3490cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
34914d57b86dSChao Yu int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
3492cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
3493cac5a3d8SDongOh Shin void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
3494cac5a3d8SDongOh Shin 			struct inode *dir, struct inode *inode);
3495cac5a3d8SDongOh Shin int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
3496cac5a3d8SDongOh Shin bool f2fs_empty_dir(struct inode *dir);
349739a53e0cSJaegeuk Kim 
3498b7f7a5e0SAl Viro static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
3499b7f7a5e0SAl Viro {
3500bfc2b7e8SEric Biggers 	if (fscrypt_is_nokey_name(dentry))
3501bfc2b7e8SEric Biggers 		return -ENOKEY;
35024d57b86dSChao Yu 	return f2fs_do_add_link(d_inode(dentry->d_parent), &dentry->d_name,
3503510022a8SJaegeuk Kim 				inode, inode->i_ino, inode->i_mode);
3504b7f7a5e0SAl Viro }
3505b7f7a5e0SAl Viro 
350639a53e0cSJaegeuk Kim /*
350739a53e0cSJaegeuk Kim  * super.c
350839a53e0cSJaegeuk Kim  */
3509cac5a3d8SDongOh Shin int f2fs_inode_dirtied(struct inode *inode, bool sync);
3510cac5a3d8SDongOh Shin void f2fs_inode_synced(struct inode *inode);
351110a26878SChao Yu int f2fs_dquot_initialize(struct inode *inode);
3512ea676733SJaegeuk Kim int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly);
3513af033b2aSChao Yu int f2fs_quota_sync(struct super_block *sb, int type);
35146d1451bfSChengguang Xu loff_t max_file_blocks(struct inode *inode);
35154b2414d0SChao Yu void f2fs_quota_off_umount(struct super_block *sb);
3516cac5a3d8SDongOh Shin int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
3517cac5a3d8SDongOh Shin int f2fs_sync_fs(struct super_block *sb, int sync);
35184d57b86dSChao Yu int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi);
351939a53e0cSJaegeuk Kim 
352039a53e0cSJaegeuk Kim /*
352139a53e0cSJaegeuk Kim  * hash.c
352239a53e0cSJaegeuk Kim  */
352343c780baSEric Biggers void f2fs_hash_filename(const struct inode *dir, struct f2fs_filename *fname);
352439a53e0cSJaegeuk Kim 
352539a53e0cSJaegeuk Kim /*
352639a53e0cSJaegeuk Kim  * node.c
352739a53e0cSJaegeuk Kim  */
352839a53e0cSJaegeuk Kim struct node_info;
352939a53e0cSJaegeuk Kim 
35304d57b86dSChao Yu int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid);
35314d57b86dSChao Yu bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type);
353250fa53ecSChao Yu bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct page *page);
353350fa53ecSChao Yu void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi);
353450fa53ecSChao Yu void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct page *page);
353550fa53ecSChao Yu void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi);
35364d57b86dSChao Yu int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
35374d57b86dSChao Yu bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
35384d57b86dSChao Yu bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
35397735730dSChao Yu int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
3540a9419b63SJaegeuk Kim 				struct node_info *ni, bool checkpoint_context);
35414d57b86dSChao Yu pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
35424d57b86dSChao Yu int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
35434d57b86dSChao Yu int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from);
35444d57b86dSChao Yu int f2fs_truncate_xattr_node(struct inode *inode);
354550fa53ecSChao Yu int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
354650fa53ecSChao Yu 					unsigned int seq_id);
354794c821fbSChao Yu bool f2fs_nat_bitmap_enabled(struct f2fs_sb_info *sbi);
35484d57b86dSChao Yu int f2fs_remove_inode_page(struct inode *inode);
35494d57b86dSChao Yu struct page *f2fs_new_inode_page(struct inode *inode);
35504d57b86dSChao Yu struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs);
35514d57b86dSChao Yu void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
35524d57b86dSChao Yu struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
35534d57b86dSChao Yu struct page *f2fs_get_node_page_ra(struct page *parent, int start);
355448018b4cSChao Yu int f2fs_move_node_page(struct page *node_page, int gc_type);
355568e79bafSJia Yang void f2fs_flush_inline_data(struct f2fs_sb_info *sbi);
35564d57b86dSChao Yu int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
355750fa53ecSChao Yu 			struct writeback_control *wbc, bool atomic,
355850fa53ecSChao Yu 			unsigned int *seq_id);
35594d57b86dSChao Yu int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
35604d57b86dSChao Yu 			struct writeback_control *wbc,
3561b0af6d49SChao Yu 			bool do_balance, enum iostat_type io_type);
3562e2374015SChao Yu int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
35634d57b86dSChao Yu bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
35644d57b86dSChao Yu void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
35654d57b86dSChao Yu void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
35664d57b86dSChao Yu int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
35679627a7b3SChao Yu int f2fs_recover_inline_xattr(struct inode *inode, struct page *page);
35684d57b86dSChao Yu int f2fs_recover_xattr_data(struct inode *inode, struct page *page);
35694d57b86dSChao Yu int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
35707735730dSChao Yu int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
3571cac5a3d8SDongOh Shin 			unsigned int segno, struct f2fs_summary_block *sum);
357294c821fbSChao Yu void f2fs_enable_nat_bits(struct f2fs_sb_info *sbi);
3573edc55aafSJaegeuk Kim int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
35744d57b86dSChao Yu int f2fs_build_node_manager(struct f2fs_sb_info *sbi);
35754d57b86dSChao Yu void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi);
35764d57b86dSChao Yu int __init f2fs_create_node_manager_caches(void);
35774d57b86dSChao Yu void f2fs_destroy_node_manager_caches(void);
357839a53e0cSJaegeuk Kim 
357939a53e0cSJaegeuk Kim /*
358039a53e0cSJaegeuk Kim  * segment.c
358139a53e0cSJaegeuk Kim  */
35824d57b86dSChao Yu bool f2fs_need_SSR(struct f2fs_sb_info *sbi);
35834d57b86dSChao Yu void f2fs_register_inmem_page(struct inode *inode, struct page *page);
35844d57b86dSChao Yu void f2fs_drop_inmem_pages_all(struct f2fs_sb_info *sbi, bool gc_failure);
35854d57b86dSChao Yu void f2fs_drop_inmem_pages(struct inode *inode);
35864d57b86dSChao Yu void f2fs_drop_inmem_page(struct inode *inode, struct page *page);
35874d57b86dSChao Yu int f2fs_commit_inmem_pages(struct inode *inode);
3588cac5a3d8SDongOh Shin void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
35897bcd0cfaSChao Yu void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi, bool from_bg);
359039d787beSChao Yu int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
35914d57b86dSChao Yu int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
35921228b482SChao Yu int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
35934d57b86dSChao Yu void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
35944d57b86dSChao Yu void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
35954d57b86dSChao Yu bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
35964d674904SFengnan Chang int f2fs_start_discard_thread(struct f2fs_sb_info *sbi);
35974d57b86dSChao Yu void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
35984d57b86dSChao Yu void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi);
359903f2c02dSJaegeuk Kim bool f2fs_issue_discard_timeout(struct f2fs_sb_info *sbi);
36004d57b86dSChao Yu void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
36014d57b86dSChao Yu 					struct cp_control *cpc);
36024354994fSDaniel Rosenberg void f2fs_dirty_to_prefree(struct f2fs_sb_info *sbi);
36034d3aed70SDaniel Rosenberg block_t f2fs_get_unusable_blocks(struct f2fs_sb_info *sbi);
36044d3aed70SDaniel Rosenberg int f2fs_disable_cp_again(struct f2fs_sb_info *sbi, block_t unusable);
36054d57b86dSChao Yu void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi);
36064d57b86dSChao Yu int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
360761461fc9SChao Yu bool f2fs_segment_has_free_slot(struct f2fs_sb_info *sbi, int segno);
3608093749e2SChao Yu void f2fs_init_inmem_curseg(struct f2fs_sb_info *sbi);
3609093749e2SChao Yu void f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi);
3610093749e2SChao Yu void f2fs_restore_inmem_curseg(struct f2fs_sb_info *sbi);
3611093749e2SChao Yu void f2fs_get_new_segment(struct f2fs_sb_info *sbi,
3612093749e2SChao Yu 			unsigned int *newseg, bool new_sec, int dir);
36130ef81833SChao Yu void f2fs_allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type,
361404f0b2eaSQiuyang Sun 					unsigned int start, unsigned int end);
3615509f1010SChao Yu void f2fs_allocate_new_section(struct f2fs_sb_info *sbi, int type, bool force);
3616901d745fSChao Yu void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi);
3617cac5a3d8SDongOh Shin int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
36184d57b86dSChao Yu bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
36194d57b86dSChao Yu 					struct cp_control *cpc);
36204d57b86dSChao Yu struct page *f2fs_get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
36214d57b86dSChao Yu void f2fs_update_meta_page(struct f2fs_sb_info *sbi, void *src,
36224d57b86dSChao Yu 					block_t blk_addr);
36234d57b86dSChao Yu void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
3624b0af6d49SChao Yu 						enum iostat_type io_type);
36254d57b86dSChao Yu void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio);
36264d57b86dSChao Yu void f2fs_outplace_write_data(struct dnode_of_data *dn,
36274d57b86dSChao Yu 			struct f2fs_io_info *fio);
36284d57b86dSChao Yu int f2fs_inplace_write_data(struct f2fs_io_info *fio);
36294d57b86dSChao Yu void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
3630cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t new_blkaddr,
3631c5d02785SChao Yu 			bool recover_curseg, bool recover_newaddr,
3632c5d02785SChao Yu 			bool from_gc);
3633cac5a3d8SDongOh Shin void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
3634cac5a3d8SDongOh Shin 			block_t old_addr, block_t new_addr,
3635cac5a3d8SDongOh Shin 			unsigned char version, bool recover_curseg,
3636cac5a3d8SDongOh Shin 			bool recover_newaddr);
36374d57b86dSChao Yu void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
3638cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t *new_blkaddr,
3639fb830fc5SChao Yu 			struct f2fs_summary *sum, int type,
3640f608c38cSChao Yu 			struct f2fs_io_info *fio);
364171f2c820SChao Yu void f2fs_update_device_state(struct f2fs_sb_info *sbi, nid_t ino,
364271f2c820SChao Yu 					block_t blkaddr, unsigned int blkcnt);
3643cac5a3d8SDongOh Shin void f2fs_wait_on_page_writeback(struct page *page,
3644bae0ee7aSChao Yu 			enum page_type type, bool ordered, bool locked);
36450ded69f6SJaegeuk Kim void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr);
36461e78e8bdSSahitya Tummala void f2fs_wait_on_block_writeback_range(struct inode *inode, block_t blkaddr,
36471e78e8bdSSahitya Tummala 								block_t len);
36484d57b86dSChao Yu void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
36494d57b86dSChao Yu void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
36504d57b86dSChao Yu int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
3651cac5a3d8SDongOh Shin 			unsigned int val, int alloc);
36524d57b86dSChao Yu void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
3653c426d991SShin'ichiro Kawasaki int f2fs_fix_curseg_write_pointer(struct f2fs_sb_info *sbi);
3654d508c94eSShin'ichiro Kawasaki int f2fs_check_write_pointer(struct f2fs_sb_info *sbi);
36554d57b86dSChao Yu int f2fs_build_segment_manager(struct f2fs_sb_info *sbi);
36564d57b86dSChao Yu void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi);
36574d57b86dSChao Yu int __init f2fs_create_segment_manager_caches(void);
36584d57b86dSChao Yu void f2fs_destroy_segment_manager_caches(void);
36594d57b86dSChao Yu int f2fs_rw_hint_to_seg_type(enum rw_hint hint);
36604d57b86dSChao Yu enum rw_hint f2fs_io_type_to_rw_hint(struct f2fs_sb_info *sbi,
36614d57b86dSChao Yu 			enum page_type type, enum temp_type temp);
3662de881df9SAravind Ramesh unsigned int f2fs_usable_segs_in_sec(struct f2fs_sb_info *sbi,
3663de881df9SAravind Ramesh 			unsigned int segno);
3664de881df9SAravind Ramesh unsigned int f2fs_usable_blks_in_seg(struct f2fs_sb_info *sbi,
3665de881df9SAravind Ramesh 			unsigned int segno);
366639a53e0cSJaegeuk Kim 
36676691d940SDaeho Jeong #define DEF_FRAGMENT_SIZE	4
36686691d940SDaeho Jeong #define MIN_FRAGMENT_SIZE	1
36696691d940SDaeho Jeong #define MAX_FRAGMENT_SIZE	512
36706691d940SDaeho Jeong 
36716691d940SDaeho Jeong static inline bool f2fs_need_rand_seg(struct f2fs_sb_info *sbi)
36726691d940SDaeho Jeong {
36736691d940SDaeho Jeong 	return F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_SEG ||
36746691d940SDaeho Jeong 		F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK;
36756691d940SDaeho Jeong }
36766691d940SDaeho Jeong 
367739a53e0cSJaegeuk Kim /*
367839a53e0cSJaegeuk Kim  * checkpoint.c
367939a53e0cSJaegeuk Kim  */
3680cac5a3d8SDongOh Shin void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io);
36814d57b86dSChao Yu struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
36824d57b86dSChao Yu struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
368386f33603SJaegeuk Kim struct page *f2fs_get_meta_page_retry(struct f2fs_sb_info *sbi, pgoff_t index);
36844d57b86dSChao Yu struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
3685e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
36864d57b86dSChao Yu 					block_t blkaddr, int type);
36874d57b86dSChao Yu int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
3688cac5a3d8SDongOh Shin 			int type, bool sync);
3689430f163bSChao Yu void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index,
3690430f163bSChao Yu 							unsigned int ra_blocks);
36914d57b86dSChao Yu long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
3692b0af6d49SChao Yu 			long nr_to_write, enum iostat_type io_type);
36934d57b86dSChao Yu void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
36944d57b86dSChao Yu void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
36954d57b86dSChao Yu void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all);
36964d57b86dSChao Yu bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
36974d57b86dSChao Yu void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
369839d787beSChao Yu 					unsigned int devidx, int type);
36994d57b86dSChao Yu bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
370039d787beSChao Yu 					unsigned int devidx, int type);
3701cac5a3d8SDongOh Shin int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
37024d57b86dSChao Yu int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi);
37034d57b86dSChao Yu void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi);
37044d57b86dSChao Yu void f2fs_add_orphan_inode(struct inode *inode);
37054d57b86dSChao Yu void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
37064d57b86dSChao Yu int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi);
37074d57b86dSChao Yu int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi);
37084d57b86dSChao Yu void f2fs_update_dirty_page(struct inode *inode, struct page *page);
37094d57b86dSChao Yu void f2fs_remove_dirty_inode(struct inode *inode);
37104d57b86dSChao Yu int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
3711bf22c3ccSSahitya Tummala void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type);
37123a0a9cbcSChao Yu u64 f2fs_get_sectors_written(struct f2fs_sb_info *sbi);
37134d57b86dSChao Yu int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
37144d57b86dSChao Yu void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi);
37154d57b86dSChao Yu int __init f2fs_create_checkpoint_caches(void);
37164d57b86dSChao Yu void f2fs_destroy_checkpoint_caches(void);
3717261eeb9cSDaeho Jeong int f2fs_issue_checkpoint(struct f2fs_sb_info *sbi);
3718261eeb9cSDaeho Jeong int f2fs_start_ckpt_thread(struct f2fs_sb_info *sbi);
3719261eeb9cSDaeho Jeong void f2fs_stop_ckpt_thread(struct f2fs_sb_info *sbi);
3720261eeb9cSDaeho Jeong void f2fs_init_ckpt_req_control(struct f2fs_sb_info *sbi);
372139a53e0cSJaegeuk Kim 
372239a53e0cSJaegeuk Kim /*
372339a53e0cSJaegeuk Kim  * data.c
372439a53e0cSJaegeuk Kim  */
3725f543805fSChao Yu int __init f2fs_init_bioset(void);
3726f543805fSChao Yu void f2fs_destroy_bioset(void);
37270b20fcecSChao Yu int f2fs_init_bio_entry_cache(void);
37280b20fcecSChao Yu void f2fs_destroy_bio_entry_cache(void);
37294c8ff709SChao Yu void f2fs_submit_bio(struct f2fs_sb_info *sbi,
37304c8ff709SChao Yu 				struct bio *bio, enum page_type type);
3731b9109b0eSJaegeuk Kim void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
3732b9109b0eSJaegeuk Kim void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
3733bab475c5SChao Yu 				struct inode *inode, struct page *page,
3734bab475c5SChao Yu 				nid_t ino, enum page_type type);
37350b20fcecSChao Yu void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
37360b20fcecSChao Yu 					struct bio **bio, struct page *page);
3737b9109b0eSJaegeuk Kim void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
3738cac5a3d8SDongOh Shin int f2fs_submit_page_bio(struct f2fs_io_info *fio);
37398648de2cSChao Yu int f2fs_merge_page_bio(struct f2fs_io_info *fio);
3740fe16efe6SChao Yu void f2fs_submit_page_write(struct f2fs_io_info *fio);
3741cac5a3d8SDongOh Shin struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
3742cac5a3d8SDongOh Shin 			block_t blk_addr, struct bio *bio);
3743cac5a3d8SDongOh Shin int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
37444d57b86dSChao Yu void f2fs_set_data_blkaddr(struct dnode_of_data *dn);
3745cac5a3d8SDongOh Shin void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
37464d57b86dSChao Yu int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
37474d57b86dSChao Yu int f2fs_reserve_new_block(struct dnode_of_data *dn);
3748cac5a3d8SDongOh Shin int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
3749cac5a3d8SDongOh Shin int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
37504d57b86dSChao Yu struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
3751cac5a3d8SDongOh Shin 			int op_flags, bool for_write);
37524d57b86dSChao Yu struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index);
37534d57b86dSChao Yu struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
3754cac5a3d8SDongOh Shin 			bool for_write);
37554d57b86dSChao Yu struct page *f2fs_get_new_data_page(struct inode *inode,
3756cac5a3d8SDongOh Shin 			struct page *ipage, pgoff_t index, bool new_i_size);
37574d57b86dSChao Yu int f2fs_do_write_data_page(struct f2fs_io_info *fio);
37580ef81833SChao Yu void f2fs_do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock);
3759cac5a3d8SDongOh Shin int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
3760cac5a3d8SDongOh Shin 			int create, int flag);
3761cac5a3d8SDongOh Shin int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3762cac5a3d8SDongOh Shin 			u64 start, u64 len);
37634c8ff709SChao Yu int f2fs_encrypt_one_page(struct f2fs_io_info *fio);
37644d57b86dSChao Yu bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio);
37654d57b86dSChao Yu bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio);
37664c8ff709SChao Yu int f2fs_write_single_data_page(struct page *page, int *submitted,
37674c8ff709SChao Yu 				struct bio **bio, sector_t *last_block,
37684c8ff709SChao Yu 				struct writeback_control *wbc,
37694c8ff709SChao Yu 				enum iostat_type io_type,
37703afae09fSChao Yu 				int compr_blocks, bool allow_balance);
3771a1e09b03SEric Biggers void f2fs_write_failed(struct inode *inode, loff_t to);
3772cac5a3d8SDongOh Shin void f2fs_invalidate_page(struct page *page, unsigned int offset,
3773cac5a3d8SDongOh Shin 			unsigned int length);
3774cac5a3d8SDongOh Shin int f2fs_release_page(struct page *page, gfp_t wait);
37755b7a487cSWeichao Guo #ifdef CONFIG_MIGRATION
3776cac5a3d8SDongOh Shin int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,
3777cac5a3d8SDongOh Shin 			struct page *page, enum migrate_mode mode);
37785b7a487cSWeichao Guo #endif
3779b91050a8SHyunchul Lee bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len);
37805ec2d99dSMatthew Wilcox void f2fs_clear_page_cache_dirty_tag(struct page *page);
37814c8ff709SChao Yu int f2fs_init_post_read_processing(void);
37824c8ff709SChao Yu void f2fs_destroy_post_read_processing(void);
37834c8ff709SChao Yu int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi);
37844c8ff709SChao Yu void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi);
37851517c1a7SEric Biggers extern const struct iomap_ops f2fs_iomap_ops;
378639a53e0cSJaegeuk Kim 
378739a53e0cSJaegeuk Kim /*
378839a53e0cSJaegeuk Kim  * gc.c
378939a53e0cSJaegeuk Kim  */
37904d57b86dSChao Yu int f2fs_start_gc_thread(struct f2fs_sb_info *sbi);
37914d57b86dSChao Yu void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi);
37924d57b86dSChao Yu block_t f2fs_start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
37937dede886SChao Yu int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background, bool force,
3794e066b83cSJaegeuk Kim 			unsigned int segno);
37954d57b86dSChao Yu void f2fs_build_gc_manager(struct f2fs_sb_info *sbi);
379604f0b2eaSQiuyang Sun int f2fs_resize_fs(struct f2fs_sb_info *sbi, __u64 block_count);
3797093749e2SChao Yu int __init f2fs_create_garbage_collection_cache(void);
3798093749e2SChao Yu void f2fs_destroy_garbage_collection_cache(void);
379939a53e0cSJaegeuk Kim 
380039a53e0cSJaegeuk Kim /*
380139a53e0cSJaegeuk Kim  * recovery.c
380239a53e0cSJaegeuk Kim  */
38034d57b86dSChao Yu int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
38044d57b86dSChao Yu bool f2fs_space_for_roll_forward(struct f2fs_sb_info *sbi);
3805cad83c96SChao Yu int __init f2fs_create_recovery_cache(void);
3806cad83c96SChao Yu void f2fs_destroy_recovery_cache(void);
380739a53e0cSJaegeuk Kim 
380839a53e0cSJaegeuk Kim /*
380939a53e0cSJaegeuk Kim  * debug.c
381039a53e0cSJaegeuk Kim  */
381139a53e0cSJaegeuk Kim #ifdef CONFIG_F2FS_STAT_FS
381239a53e0cSJaegeuk Kim struct f2fs_stat_info {
381339a53e0cSJaegeuk Kim 	struct list_head stat_list;
381439a53e0cSJaegeuk Kim 	struct f2fs_sb_info *sbi;
381539a53e0cSJaegeuk Kim 	int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
381639a53e0cSJaegeuk Kim 	int main_area_segs, main_area_sections, main_area_zones;
38175b7ee374SChao Yu 	unsigned long long hit_largest, hit_cached, hit_rbtree;
38185b7ee374SChao Yu 	unsigned long long hit_total, total_ext;
3819c00ba554SJaegeuk Kim 	int ext_tree, zombie_tree, ext_node;
38202c8a4a28SJaegeuk Kim 	int ndirty_node, ndirty_dent, ndirty_meta, ndirty_imeta;
38212c8a4a28SJaegeuk Kim 	int ndirty_data, ndirty_qdata;
382235782b23SJaegeuk Kim 	int inmem_pages;
38232c8a4a28SJaegeuk Kim 	unsigned int ndirty_dirs, ndirty_files, nquota_files, ndirty_all;
38245b0ef73cSJaegeuk Kim 	int nats, dirty_nats, sits, dirty_sits;
38255b0ef73cSJaegeuk Kim 	int free_nids, avail_nids, alloc_nids;
382639a53e0cSJaegeuk Kim 	int total_count, utilization;
38278b8dd65fSChao Yu 	int bg_gc, nr_wb_cp_data, nr_wb_data;
38285f9abab4SJaegeuk Kim 	int nr_rd_data, nr_rd_node, nr_rd_meta;
382902b16d0aSChao Yu 	int nr_dio_read, nr_dio_write;
3830274bd9baSChao Yu 	unsigned int io_skip_bggc, other_skip_bggc;
383114d8d5f7SChao Yu 	int nr_flushing, nr_flushed, flush_list_empty;
383214d8d5f7SChao Yu 	int nr_discarding, nr_discarded;
38335f32366aSChao Yu 	int nr_discard_cmd;
3834d84d1cbdSChao Yu 	unsigned int undiscard_blks;
3835261eeb9cSDaeho Jeong 	int nr_issued_ckpt, nr_total_ckpt, nr_queued_ckpt;
3836261eeb9cSDaeho Jeong 	unsigned int cur_ckpt_time, peak_ckpt_time;
3837a00861dbSJaegeuk Kim 	int inline_xattr, inline_inode, inline_dir, append, update, orphans;
3838ae999bb9SDaeho Jeong 	int compr_inode;
3839ae999bb9SDaeho Jeong 	unsigned long long compr_blocks;
3840648d50baSChao Yu 	int aw_cnt, max_aw_cnt, vw_cnt, max_vw_cnt;
3841f83a2584SYunlei He 	unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
384239a53e0cSJaegeuk Kim 	unsigned int bimodal, avg_vblocks;
384339a53e0cSJaegeuk Kim 	int util_free, util_valid, util_invalid;
384439a53e0cSJaegeuk Kim 	int rsvd_segs, overp_segs;
38456ce19affSChao Yu 	int dirty_count, node_pages, meta_pages, compress_pages;
38466ce19affSChao Yu 	int compress_page_hit;
384742190d2aSJaegeuk Kim 	int prefree_count, call_count, cp_count, bg_cp_count;
384839a53e0cSJaegeuk Kim 	int tot_segs, node_segs, data_segs, free_segs, free_secs;
3849e1235983SChangman Lee 	int bg_node_segs, bg_data_segs;
385039a53e0cSJaegeuk Kim 	int tot_blks, data_blks, node_blks;
3851e1235983SChangman Lee 	int bg_data_blks, bg_node_blks;
38522ef79ecbSChao Yu 	unsigned long long skipped_atomic_files[2];
385339a53e0cSJaegeuk Kim 	int curseg[NR_CURSEG_TYPE];
385439a53e0cSJaegeuk Kim 	int cursec[NR_CURSEG_TYPE];
385539a53e0cSJaegeuk Kim 	int curzone[NR_CURSEG_TYPE];
38560759e2c1SChao Yu 	unsigned int dirty_seg[NR_CURSEG_TYPE];
38570759e2c1SChao Yu 	unsigned int full_seg[NR_CURSEG_TYPE];
38580759e2c1SChao Yu 	unsigned int valid_blks[NR_CURSEG_TYPE];
385939a53e0cSJaegeuk Kim 
3860b63e7be5SChao Yu 	unsigned int meta_count[META_MAX];
386139a53e0cSJaegeuk Kim 	unsigned int segment_count[2];
386239a53e0cSJaegeuk Kim 	unsigned int block_count[2];
3863b9a2c252SChangman Lee 	unsigned int inplace_count;
38649edcdabfSChao Yu 	unsigned long long base_mem, cache_mem, page_mem;
386539a53e0cSJaegeuk Kim };
386639a53e0cSJaegeuk Kim 
3867963d4f7dSGu Zheng static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
3868963d4f7dSGu Zheng {
3869963d4f7dSGu Zheng 	return (struct f2fs_stat_info *)sbi->stat_info;
3870963d4f7dSGu Zheng }
3871963d4f7dSGu Zheng 
3872942e0be6SChangman Lee #define stat_inc_cp_count(si)		((si)->cp_count++)
387342190d2aSJaegeuk Kim #define stat_inc_bg_cp_count(si)	((si)->bg_cp_count++)
387439a53e0cSJaegeuk Kim #define stat_inc_call_count(si)		((si)->call_count++)
3875fc7100eaSHridya Valsaraju #define stat_inc_bggc_count(si)		((si)->bg_gc++)
3876274bd9baSChao Yu #define stat_io_skip_bggc_count(sbi)	((sbi)->io_skip_bggc++)
3877274bd9baSChao Yu #define stat_other_skip_bggc_count(sbi)	((sbi)->other_skip_bggc++)
387833fbd510SChao Yu #define stat_inc_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]++)
387933fbd510SChao Yu #define stat_dec_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]--)
38805b7ee374SChao Yu #define stat_inc_total_hit(sbi)		(atomic64_inc(&(sbi)->total_hit_ext))
38815b7ee374SChao Yu #define stat_inc_rbtree_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_rbtree))
38825b7ee374SChao Yu #define stat_inc_largest_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_largest))
38835b7ee374SChao Yu #define stat_inc_cached_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_cached))
3884d5e8f6c9SChao Yu #define stat_inc_inline_xattr(inode)					\
3885d5e8f6c9SChao Yu 	do {								\
3886d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3887d5e8f6c9SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_xattr));	\
3888d5e8f6c9SChao Yu 	} while (0)
3889d5e8f6c9SChao Yu #define stat_dec_inline_xattr(inode)					\
3890d5e8f6c9SChao Yu 	do {								\
3891d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3892d5e8f6c9SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_xattr));	\
3893d5e8f6c9SChao Yu 	} while (0)
38940dbdc2aeSJaegeuk Kim #define stat_inc_inline_inode(inode)					\
38950dbdc2aeSJaegeuk Kim 	do {								\
38960dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
389703e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_inode));	\
38980dbdc2aeSJaegeuk Kim 	} while (0)
38990dbdc2aeSJaegeuk Kim #define stat_dec_inline_inode(inode)					\
39000dbdc2aeSJaegeuk Kim 	do {								\
39010dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
390203e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_inode));	\
39030dbdc2aeSJaegeuk Kim 	} while (0)
39043289c061SJaegeuk Kim #define stat_inc_inline_dir(inode)					\
39053289c061SJaegeuk Kim 	do {								\
39063289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
390703e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_dir));	\
39083289c061SJaegeuk Kim 	} while (0)
39093289c061SJaegeuk Kim #define stat_dec_inline_dir(inode)					\
39103289c061SJaegeuk Kim 	do {								\
39113289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
391203e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_dir));	\
39133289c061SJaegeuk Kim 	} while (0)
39144c8ff709SChao Yu #define stat_inc_compr_inode(inode)					\
39154c8ff709SChao Yu 	do {								\
39164c8ff709SChao Yu 		if (f2fs_compressed_file(inode))			\
39174c8ff709SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->compr_inode));	\
39184c8ff709SChao Yu 	} while (0)
39194c8ff709SChao Yu #define stat_dec_compr_inode(inode)					\
39204c8ff709SChao Yu 	do {								\
39214c8ff709SChao Yu 		if (f2fs_compressed_file(inode))			\
39224c8ff709SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->compr_inode));	\
39234c8ff709SChao Yu 	} while (0)
39244c8ff709SChao Yu #define stat_add_compr_blocks(inode, blocks)				\
3925ae999bb9SDaeho Jeong 		(atomic64_add(blocks, &F2FS_I_SB(inode)->compr_blocks))
39264c8ff709SChao Yu #define stat_sub_compr_blocks(inode, blocks)				\
3927ae999bb9SDaeho Jeong 		(atomic64_sub(blocks, &F2FS_I_SB(inode)->compr_blocks))
3928b63e7be5SChao Yu #define stat_inc_meta_count(sbi, blkaddr)				\
3929b63e7be5SChao Yu 	do {								\
3930b63e7be5SChao Yu 		if (blkaddr < SIT_I(sbi)->sit_base_addr)		\
3931b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_CP]);	\
3932b63e7be5SChao Yu 		else if (blkaddr < NM_I(sbi)->nat_blkaddr)		\
3933b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_SIT]);	\
3934b63e7be5SChao Yu 		else if (blkaddr < SM_I(sbi)->ssa_blkaddr)		\
3935b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_NAT]);	\
3936b63e7be5SChao Yu 		else if (blkaddr < SM_I(sbi)->main_blkaddr)		\
3937b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_SSA]);	\
3938b63e7be5SChao Yu 	} while (0)
3939dcdfff65SJaegeuk Kim #define stat_inc_seg_type(sbi, curseg)					\
3940dcdfff65SJaegeuk Kim 		((sbi)->segment_count[(curseg)->alloc_type]++)
3941dcdfff65SJaegeuk Kim #define stat_inc_block_count(sbi, curseg)				\
3942dcdfff65SJaegeuk Kim 		((sbi)->block_count[(curseg)->alloc_type]++)
3943b9a2c252SChangman Lee #define stat_inc_inplace_blocks(sbi)					\
3944b9a2c252SChangman Lee 		(atomic_inc(&(sbi)->inplace_count))
394526a28a0cSJaegeuk Kim #define stat_update_max_atomic_write(inode)				\
394626a28a0cSJaegeuk Kim 	do {								\
39470e6d0164SSahitya Tummala 		int cur = F2FS_I_SB(inode)->atomic_files;	\
394826a28a0cSJaegeuk Kim 		int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt);	\
394926a28a0cSJaegeuk Kim 		if (cur > max)						\
395026a28a0cSJaegeuk Kim 			atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur);	\
395126a28a0cSJaegeuk Kim 	} while (0)
3952648d50baSChao Yu #define stat_inc_volatile_write(inode)					\
3953648d50baSChao Yu 		(atomic_inc(&F2FS_I_SB(inode)->vw_cnt))
3954648d50baSChao Yu #define stat_dec_volatile_write(inode)					\
3955648d50baSChao Yu 		(atomic_dec(&F2FS_I_SB(inode)->vw_cnt))
3956648d50baSChao Yu #define stat_update_max_volatile_write(inode)				\
3957648d50baSChao Yu 	do {								\
3958648d50baSChao Yu 		int cur = atomic_read(&F2FS_I_SB(inode)->vw_cnt);	\
3959648d50baSChao Yu 		int max = atomic_read(&F2FS_I_SB(inode)->max_vw_cnt);	\
3960648d50baSChao Yu 		if (cur > max)						\
3961648d50baSChao Yu 			atomic_set(&F2FS_I_SB(inode)->max_vw_cnt, cur);	\
3962648d50baSChao Yu 	} while (0)
3963e1235983SChangman Lee #define stat_inc_seg_count(sbi, type, gc_type)				\
396439a53e0cSJaegeuk Kim 	do {								\
3965963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
396668afcf2dSTomohiro Kusumi 		si->tot_segs++;						\
396768afcf2dSTomohiro Kusumi 		if ((type) == SUM_TYPE_DATA) {				\
396839a53e0cSJaegeuk Kim 			si->data_segs++;				\
3969e1235983SChangman Lee 			si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0;	\
3970e1235983SChangman Lee 		} else {						\
397139a53e0cSJaegeuk Kim 			si->node_segs++;				\
3972e1235983SChangman Lee 			si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0;	\
3973e1235983SChangman Lee 		}							\
397439a53e0cSJaegeuk Kim 	} while (0)
397539a53e0cSJaegeuk Kim 
397639a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks)				\
397768afcf2dSTomohiro Kusumi 	((si)->tot_blks += (blks))
397839a53e0cSJaegeuk Kim 
3979e1235983SChangman Lee #define stat_inc_data_blk_count(sbi, blks, gc_type)			\
398039a53e0cSJaegeuk Kim 	do {								\
3981963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
398239a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
398339a53e0cSJaegeuk Kim 		si->data_blks += (blks);				\
398468afcf2dSTomohiro Kusumi 		si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
398539a53e0cSJaegeuk Kim 	} while (0)
398639a53e0cSJaegeuk Kim 
3987e1235983SChangman Lee #define stat_inc_node_blk_count(sbi, blks, gc_type)			\
398839a53e0cSJaegeuk Kim 	do {								\
3989963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
399039a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
399139a53e0cSJaegeuk Kim 		si->node_blks += (blks);				\
399268afcf2dSTomohiro Kusumi 		si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
399339a53e0cSJaegeuk Kim 	} while (0)
399439a53e0cSJaegeuk Kim 
3995cac5a3d8SDongOh Shin int f2fs_build_stats(struct f2fs_sb_info *sbi);
3996cac5a3d8SDongOh Shin void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
399721acc07dSGreg Kroah-Hartman void __init f2fs_create_root_stats(void);
39984589d25dSNamjae Jeon void f2fs_destroy_root_stats(void);
3999fc7100eaSHridya Valsaraju void f2fs_update_sit_info(struct f2fs_sb_info *sbi);
400039a53e0cSJaegeuk Kim #else
4001d66450e7SArnd Bergmann #define stat_inc_cp_count(si)				do { } while (0)
4002d66450e7SArnd Bergmann #define stat_inc_bg_cp_count(si)			do { } while (0)
4003d66450e7SArnd Bergmann #define stat_inc_call_count(si)				do { } while (0)
4004d66450e7SArnd Bergmann #define stat_inc_bggc_count(si)				do { } while (0)
4005274bd9baSChao Yu #define stat_io_skip_bggc_count(sbi)			do { } while (0)
4006274bd9baSChao Yu #define stat_other_skip_bggc_count(sbi)			do { } while (0)
4007d66450e7SArnd Bergmann #define stat_inc_dirty_inode(sbi, type)			do { } while (0)
4008d66450e7SArnd Bergmann #define stat_dec_dirty_inode(sbi, type)			do { } while (0)
4009fc7100eaSHridya Valsaraju #define stat_inc_total_hit(sbi)				do { } while (0)
4010fc7100eaSHridya Valsaraju #define stat_inc_rbtree_node_hit(sbi)			do { } while (0)
4011d66450e7SArnd Bergmann #define stat_inc_largest_node_hit(sbi)			do { } while (0)
4012d66450e7SArnd Bergmann #define stat_inc_cached_node_hit(sbi)			do { } while (0)
4013d66450e7SArnd Bergmann #define stat_inc_inline_xattr(inode)			do { } while (0)
4014d66450e7SArnd Bergmann #define stat_dec_inline_xattr(inode)			do { } while (0)
4015d66450e7SArnd Bergmann #define stat_inc_inline_inode(inode)			do { } while (0)
4016d66450e7SArnd Bergmann #define stat_dec_inline_inode(inode)			do { } while (0)
4017d66450e7SArnd Bergmann #define stat_inc_inline_dir(inode)			do { } while (0)
4018d66450e7SArnd Bergmann #define stat_dec_inline_dir(inode)			do { } while (0)
40194c8ff709SChao Yu #define stat_inc_compr_inode(inode)			do { } while (0)
40204c8ff709SChao Yu #define stat_dec_compr_inode(inode)			do { } while (0)
40214c8ff709SChao Yu #define stat_add_compr_blocks(inode, blocks)		do { } while (0)
40224c8ff709SChao Yu #define stat_sub_compr_blocks(inode, blocks)		do { } while (0)
4023d66450e7SArnd Bergmann #define stat_update_max_atomic_write(inode)		do { } while (0)
4024d66450e7SArnd Bergmann #define stat_inc_volatile_write(inode)			do { } while (0)
4025d66450e7SArnd Bergmann #define stat_dec_volatile_write(inode)			do { } while (0)
4026d66450e7SArnd Bergmann #define stat_update_max_volatile_write(inode)		do { } while (0)
4027b63e7be5SChao Yu #define stat_inc_meta_count(sbi, blkaddr)		do { } while (0)
4028d66450e7SArnd Bergmann #define stat_inc_seg_type(sbi, curseg)			do { } while (0)
4029d66450e7SArnd Bergmann #define stat_inc_block_count(sbi, curseg)		do { } while (0)
4030d66450e7SArnd Bergmann #define stat_inc_inplace_blocks(sbi)			do { } while (0)
4031d66450e7SArnd Bergmann #define stat_inc_seg_count(sbi, type, gc_type)		do { } while (0)
4032d66450e7SArnd Bergmann #define stat_inc_tot_blk_count(si, blks)		do { } while (0)
4033d66450e7SArnd Bergmann #define stat_inc_data_blk_count(sbi, blks, gc_type)	do { } while (0)
4034d66450e7SArnd Bergmann #define stat_inc_node_blk_count(sbi, blks, gc_type)	do { } while (0)
403539a53e0cSJaegeuk Kim 
403639a53e0cSJaegeuk Kim static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
403739a53e0cSJaegeuk Kim static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
403821acc07dSGreg Kroah-Hartman static inline void __init f2fs_create_root_stats(void) { }
40394589d25dSNamjae Jeon static inline void f2fs_destroy_root_stats(void) { }
404048abe91aSYueHaibing static inline void f2fs_update_sit_info(struct f2fs_sb_info *sbi) {}
404139a53e0cSJaegeuk Kim #endif
404239a53e0cSJaegeuk Kim 
404339a53e0cSJaegeuk Kim extern const struct file_operations f2fs_dir_operations;
404439a53e0cSJaegeuk Kim extern const struct file_operations f2fs_file_operations;
404539a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_file_inode_operations;
404639a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_dblock_aops;
404739a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_node_aops;
404839a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_meta_aops;
404939a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_dir_inode_operations;
405039a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_symlink_inode_operations;
4051cbaf042aSJaegeuk Kim extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
405239a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_special_inode_operations;
40534d57b86dSChao Yu extern struct kmem_cache *f2fs_inode_entry_slab;
40541001b347SHuajun Li 
4055e18c65b2SHuajun Li /*
4056e18c65b2SHuajun Li  * inline.c
4057e18c65b2SHuajun Li  */
4058cac5a3d8SDongOh Shin bool f2fs_may_inline_data(struct inode *inode);
4059cac5a3d8SDongOh Shin bool f2fs_may_inline_dentry(struct inode *inode);
40604d57b86dSChao Yu void f2fs_do_read_inline_data(struct page *page, struct page *ipage);
40614d57b86dSChao Yu void f2fs_truncate_inline_inode(struct inode *inode,
40624d57b86dSChao Yu 						struct page *ipage, u64 from);
4063cac5a3d8SDongOh Shin int f2fs_read_inline_data(struct inode *inode, struct page *page);
4064cac5a3d8SDongOh Shin int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
4065cac5a3d8SDongOh Shin int f2fs_convert_inline_inode(struct inode *inode);
4066b06af2afSJaegeuk Kim int f2fs_try_convert_inline_dir(struct inode *dir, struct dentry *dentry);
4067cac5a3d8SDongOh Shin int f2fs_write_inline_data(struct inode *inode, struct page *page);
40689627a7b3SChao Yu int f2fs_recover_inline_data(struct inode *inode, struct page *npage);
40694d57b86dSChao Yu struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
407043c780baSEric Biggers 					const struct f2fs_filename *fname,
407143c780baSEric Biggers 					struct page **res_page);
40724d57b86dSChao Yu int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent,
4073cac5a3d8SDongOh Shin 			struct page *ipage);
407443c780baSEric Biggers int f2fs_add_inline_entry(struct inode *dir, const struct f2fs_filename *fname,
4075cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
40764d57b86dSChao Yu void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry,
40774d57b86dSChao Yu 				struct page *page, struct inode *dir,
40784d57b86dSChao Yu 				struct inode *inode);
4079cac5a3d8SDongOh Shin bool f2fs_empty_inline_dir(struct inode *dir);
4080cac5a3d8SDongOh Shin int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
4081cac5a3d8SDongOh Shin 			struct fscrypt_str *fstr);
4082cac5a3d8SDongOh Shin int f2fs_inline_data_fiemap(struct inode *inode,
4083cac5a3d8SDongOh Shin 			struct fiemap_extent_info *fieinfo,
4084cac5a3d8SDongOh Shin 			__u64 start, __u64 len);
4085cde4de12SJaegeuk Kim 
4086cde4de12SJaegeuk Kim /*
40872658e50dSJaegeuk Kim  * shrinker.c
40882658e50dSJaegeuk Kim  */
4089cac5a3d8SDongOh Shin unsigned long f2fs_shrink_count(struct shrinker *shrink,
4090cac5a3d8SDongOh Shin 			struct shrink_control *sc);
4091cac5a3d8SDongOh Shin unsigned long f2fs_shrink_scan(struct shrinker *shrink,
4092cac5a3d8SDongOh Shin 			struct shrink_control *sc);
4093cac5a3d8SDongOh Shin void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
4094cac5a3d8SDongOh Shin void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
40952658e50dSJaegeuk Kim 
40962658e50dSJaegeuk Kim /*
4097a28ef1f5SChao Yu  * extent_cache.c
4098a28ef1f5SChao Yu  */
40994dada3fdSChao Yu struct rb_entry *f2fs_lookup_rb_tree(struct rb_root_cached *root,
4100004b6862SChao Yu 				struct rb_entry *cached_re, unsigned int ofs);
41012e9b2bb2SChao Yu struct rb_node **f2fs_lookup_rb_tree_ext(struct f2fs_sb_info *sbi,
41022e9b2bb2SChao Yu 				struct rb_root_cached *root,
41032e9b2bb2SChao Yu 				struct rb_node **parent,
41042e9b2bb2SChao Yu 				unsigned long long key, bool *left_most);
41054d57b86dSChao Yu struct rb_node **f2fs_lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
41064dada3fdSChao Yu 				struct rb_root_cached *root,
41074dada3fdSChao Yu 				struct rb_node **parent,
41084dada3fdSChao Yu 				unsigned int ofs, bool *leftmost);
41094dada3fdSChao Yu struct rb_entry *f2fs_lookup_rb_tree_ret(struct rb_root_cached *root,
4110004b6862SChao Yu 		struct rb_entry *cached_re, unsigned int ofs,
4111004b6862SChao Yu 		struct rb_entry **prev_entry, struct rb_entry **next_entry,
4112004b6862SChao Yu 		struct rb_node ***insert_p, struct rb_node **insert_parent,
41134dada3fdSChao Yu 		bool force, bool *leftmost);
41144d57b86dSChao Yu bool f2fs_check_rb_tree_consistence(struct f2fs_sb_info *sbi,
41152e9b2bb2SChao Yu 				struct rb_root_cached *root, bool check_key);
4116cac5a3d8SDongOh Shin unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
4117a6d601f3SChao Yu void f2fs_init_extent_tree(struct inode *inode, struct page *ipage);
4118cac5a3d8SDongOh Shin void f2fs_drop_extent_tree(struct inode *inode);
4119cac5a3d8SDongOh Shin unsigned int f2fs_destroy_extent_node(struct inode *inode);
4120cac5a3d8SDongOh Shin void f2fs_destroy_extent_tree(struct inode *inode);
4121cac5a3d8SDongOh Shin bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
4122cac5a3d8SDongOh Shin 			struct extent_info *ei);
4123cac5a3d8SDongOh Shin void f2fs_update_extent_cache(struct dnode_of_data *dn);
412419b2c30dSChao Yu void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
4125cac5a3d8SDongOh Shin 			pgoff_t fofs, block_t blkaddr, unsigned int len);
41264d57b86dSChao Yu void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi);
41274d57b86dSChao Yu int __init f2fs_create_extent_cache(void);
41284d57b86dSChao Yu void f2fs_destroy_extent_cache(void);
4129a28ef1f5SChao Yu 
4130a28ef1f5SChao Yu /*
41318ceffcb2SChao Yu  * sysfs.c
41328ceffcb2SChao Yu  */
41330f6b56ecSDaeho Jeong #define MIN_RA_MUL	2
41340f6b56ecSDaeho Jeong #define MAX_RA_MUL	256
41350f6b56ecSDaeho Jeong 
4136dc6b2055SJaegeuk Kim int __init f2fs_init_sysfs(void);
4137dc6b2055SJaegeuk Kim void f2fs_exit_sysfs(void);
4138dc6b2055SJaegeuk Kim int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
4139dc6b2055SJaegeuk Kim void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
41408ceffcb2SChao Yu 
414195ae251fSEric Biggers /* verity.c */
414295ae251fSEric Biggers extern const struct fsverity_operations f2fs_verityops;
414395ae251fSEric Biggers 
41448ceffcb2SChao Yu /*
4145cde4de12SJaegeuk Kim  * crypto support
4146cde4de12SJaegeuk Kim  */
41471958593eSJaegeuk Kim static inline bool f2fs_encrypted_file(struct inode *inode)
41481958593eSJaegeuk Kim {
414962230e0dSChandan Rajendra 	return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
41501958593eSJaegeuk Kim }
41511958593eSJaegeuk Kim 
4152cde4de12SJaegeuk Kim static inline void f2fs_set_encrypted_inode(struct inode *inode)
4153cde4de12SJaegeuk Kim {
4154643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
4155cde4de12SJaegeuk Kim 	file_set_encrypt(inode);
41569149a5ebSChao Yu 	f2fs_set_inode_flags(inode);
4157cde4de12SJaegeuk Kim #endif
4158cde4de12SJaegeuk Kim }
4159cde4de12SJaegeuk Kim 
41606dbb1796SEric Biggers /*
41616dbb1796SEric Biggers  * Returns true if the reads of the inode's data need to undergo some
41626dbb1796SEric Biggers  * postprocessing step, like decryption or authenticity verification.
41636dbb1796SEric Biggers  */
41646dbb1796SEric Biggers static inline bool f2fs_post_read_required(struct inode *inode)
4165cde4de12SJaegeuk Kim {
41664c8ff709SChao Yu 	return f2fs_encrypted_file(inode) || fsverity_active(inode) ||
41674c8ff709SChao Yu 		f2fs_compressed_file(inode);
41684c8ff709SChao Yu }
41694c8ff709SChao Yu 
41704c8ff709SChao Yu /*
41714c8ff709SChao Yu  * compress.c
41724c8ff709SChao Yu  */
41734c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
41744c8ff709SChao Yu bool f2fs_is_compressed_page(struct page *page);
41754c8ff709SChao Yu struct page *f2fs_compress_control_page(struct page *page);
41764c8ff709SChao Yu int f2fs_prepare_compress_overwrite(struct inode *inode,
41774c8ff709SChao Yu 			struct page **pagep, pgoff_t index, void **fsdata);
41784c8ff709SChao Yu bool f2fs_compress_write_end(struct inode *inode, void *fsdata,
41794c8ff709SChao Yu 					pgoff_t index, unsigned copied);
41803265d3dbSChao Yu int f2fs_truncate_partial_cluster(struct inode *inode, u64 from, bool lock);
41814c8ff709SChao Yu void f2fs_compress_write_end_io(struct bio *bio, struct page *page);
41824c8ff709SChao Yu bool f2fs_is_compress_backend_ready(struct inode *inode);
41835e6bbde9SChao Yu int f2fs_init_compress_mempool(void);
41845e6bbde9SChao Yu void f2fs_destroy_compress_mempool(void);
41856ce19affSChao Yu void f2fs_decompress_cluster(struct decompress_io_ctx *dic);
41866ce19affSChao Yu void f2fs_end_read_compressed_page(struct page *page, bool failed,
41876ce19affSChao Yu 							block_t blkaddr);
41884c8ff709SChao Yu bool f2fs_cluster_is_empty(struct compress_ctx *cc);
41894c8ff709SChao Yu bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index);
4190b368cc5eSFengnan Chang bool f2fs_all_cluster_page_loaded(struct compress_ctx *cc, struct pagevec *pvec,
4191b368cc5eSFengnan Chang 				int index, int nr_pages);
4192bbe1da7eSChao Yu bool f2fs_sanity_check_cluster(struct dnode_of_data *dn);
41934c8ff709SChao Yu void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct page *page);
41944c8ff709SChao Yu int f2fs_write_multi_pages(struct compress_ctx *cc,
41954c8ff709SChao Yu 						int *submitted,
41964c8ff709SChao Yu 						struct writeback_control *wbc,
41974c8ff709SChao Yu 						enum iostat_type io_type);
41984c8ff709SChao Yu int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index);
419994afd6d6SChao Yu void f2fs_update_extent_tree_range_compressed(struct inode *inode,
420094afd6d6SChao Yu 				pgoff_t fofs, block_t blkaddr, unsigned int llen,
420194afd6d6SChao Yu 				unsigned int c_len);
42024c8ff709SChao Yu int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
42034c8ff709SChao Yu 				unsigned nr_pages, sector_t *last_block_in_bio,
42040683728aSChao Yu 				bool is_readahead, bool for_write);
42054c8ff709SChao Yu struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc);
42067f59b277SEric Biggers void f2fs_decompress_end_io(struct decompress_io_ctx *dic, bool failed);
42077f59b277SEric Biggers void f2fs_put_page_dic(struct page *page);
420894afd6d6SChao Yu unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn);
42094c8ff709SChao Yu int f2fs_init_compress_ctx(struct compress_ctx *cc);
42108bfbfb0dSChao Yu void f2fs_destroy_compress_ctx(struct compress_ctx *cc, bool reuse);
42114c8ff709SChao Yu void f2fs_init_compress_info(struct f2fs_sb_info *sbi);
42126ce19affSChao Yu int f2fs_init_compress_inode(struct f2fs_sb_info *sbi);
42136ce19affSChao Yu void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi);
421431083031SChao Yu int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi);
421531083031SChao Yu void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi);
4216c68d6c88SChao Yu int __init f2fs_init_compress_cache(void);
4217c68d6c88SChao Yu void f2fs_destroy_compress_cache(void);
42186ce19affSChao Yu struct address_space *COMPRESS_MAPPING(struct f2fs_sb_info *sbi);
42196ce19affSChao Yu void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi, block_t blkaddr);
42206ce19affSChao Yu void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
42216ce19affSChao Yu 						nid_t ino, block_t blkaddr);
42226ce19affSChao Yu bool f2fs_load_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
42236ce19affSChao Yu 								block_t blkaddr);
42246ce19affSChao Yu void f2fs_invalidate_compress_pages(struct f2fs_sb_info *sbi, nid_t ino);
42255ac443e2SDaeho Jeong #define inc_compr_inode_stat(inode)					\
42265ac443e2SDaeho Jeong 	do {								\
42275ac443e2SDaeho Jeong 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);		\
42285ac443e2SDaeho Jeong 		sbi->compr_new_inode++;					\
42295ac443e2SDaeho Jeong 	} while (0)
42305ac443e2SDaeho Jeong #define add_compr_block_stat(inode, blocks)				\
42315ac443e2SDaeho Jeong 	do {								\
42325ac443e2SDaeho Jeong 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);		\
42335ac443e2SDaeho Jeong 		int diff = F2FS_I(inode)->i_cluster_size - blocks;	\
42345ac443e2SDaeho Jeong 		sbi->compr_written_block += blocks;			\
42355ac443e2SDaeho Jeong 		sbi->compr_saved_block += diff;				\
42365ac443e2SDaeho Jeong 	} while (0)
42374c8ff709SChao Yu #else
42384c8ff709SChao Yu static inline bool f2fs_is_compressed_page(struct page *page) { return false; }
42394c8ff709SChao Yu static inline bool f2fs_is_compress_backend_ready(struct inode *inode)
42404c8ff709SChao Yu {
42414c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
42424c8ff709SChao Yu 		return true;
42434c8ff709SChao Yu 	/* not support compression */
42444c8ff709SChao Yu 	return false;
42454c8ff709SChao Yu }
42464c8ff709SChao Yu static inline struct page *f2fs_compress_control_page(struct page *page)
42474c8ff709SChao Yu {
42484c8ff709SChao Yu 	WARN_ON_ONCE(1);
42494c8ff709SChao Yu 	return ERR_PTR(-EINVAL);
42504c8ff709SChao Yu }
42515e6bbde9SChao Yu static inline int f2fs_init_compress_mempool(void) { return 0; }
42525e6bbde9SChao Yu static inline void f2fs_destroy_compress_mempool(void) { }
42536ce19affSChao Yu static inline void f2fs_decompress_cluster(struct decompress_io_ctx *dic) { }
42546ce19affSChao Yu static inline void f2fs_end_read_compressed_page(struct page *page,
42556ce19affSChao Yu 						bool failed, block_t blkaddr)
42567f59b277SEric Biggers {
42577f59b277SEric Biggers 	WARN_ON_ONCE(1);
42587f59b277SEric Biggers }
42597f59b277SEric Biggers static inline void f2fs_put_page_dic(struct page *page)
42607f59b277SEric Biggers {
42617f59b277SEric Biggers 	WARN_ON_ONCE(1);
42627f59b277SEric Biggers }
426394afd6d6SChao Yu static inline unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn) { return 0; }
4264bbe1da7eSChao Yu static inline bool f2fs_sanity_check_cluster(struct dnode_of_data *dn) { return false; }
42656ce19affSChao Yu static inline int f2fs_init_compress_inode(struct f2fs_sb_info *sbi) { return 0; }
42666ce19affSChao Yu static inline void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi) { }
426731083031SChao Yu static inline int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi) { return 0; }
426831083031SChao Yu static inline void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi) { }
4269c68d6c88SChao Yu static inline int __init f2fs_init_compress_cache(void) { return 0; }
4270c68d6c88SChao Yu static inline void f2fs_destroy_compress_cache(void) { }
42716ce19affSChao Yu static inline void f2fs_invalidate_compress_page(struct f2fs_sb_info *sbi,
42726ce19affSChao Yu 				block_t blkaddr) { }
42736ce19affSChao Yu static inline void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi,
42746ce19affSChao Yu 				struct page *page, nid_t ino, block_t blkaddr) { }
42756ce19affSChao Yu static inline bool f2fs_load_compressed_page(struct f2fs_sb_info *sbi,
42766ce19affSChao Yu 				struct page *page, block_t blkaddr) { return false; }
42776ce19affSChao Yu static inline void f2fs_invalidate_compress_pages(struct f2fs_sb_info *sbi,
42786ce19affSChao Yu 							nid_t ino) { }
42795ac443e2SDaeho Jeong #define inc_compr_inode_stat(inode)		do { } while (0)
428094afd6d6SChao Yu static inline void f2fs_update_extent_tree_range_compressed(struct inode *inode,
428194afd6d6SChao Yu 				pgoff_t fofs, block_t blkaddr, unsigned int llen,
428294afd6d6SChao Yu 				unsigned int c_len) { }
42834c8ff709SChao Yu #endif
42844c8ff709SChao Yu 
42854c8ff709SChao Yu static inline void set_compress_context(struct inode *inode)
42864c8ff709SChao Yu {
42874c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
42884c8ff709SChao Yu 
42894c8ff709SChao Yu 	F2FS_I(inode)->i_compress_algorithm =
42904c8ff709SChao Yu 			F2FS_OPTION(sbi).compress_algorithm;
42914c8ff709SChao Yu 	F2FS_I(inode)->i_log_cluster_size =
42924c8ff709SChao Yu 			F2FS_OPTION(sbi).compress_log_size;
4293b28f047bSChao Yu 	F2FS_I(inode)->i_compress_flag =
4294b28f047bSChao Yu 			F2FS_OPTION(sbi).compress_chksum ?
4295b28f047bSChao Yu 				1 << COMPRESS_CHKSUM : 0;
42964c8ff709SChao Yu 	F2FS_I(inode)->i_cluster_size =
42974c8ff709SChao Yu 			1 << F2FS_I(inode)->i_log_cluster_size;
429801f6afd0SChao Yu 	if ((F2FS_I(inode)->i_compress_algorithm == COMPRESS_LZ4 ||
429901f6afd0SChao Yu 		F2FS_I(inode)->i_compress_algorithm == COMPRESS_ZSTD) &&
43003fde13f8SChao Yu 			F2FS_OPTION(sbi).compress_level)
43013fde13f8SChao Yu 		F2FS_I(inode)->i_compress_flag |=
43023fde13f8SChao Yu 				F2FS_OPTION(sbi).compress_level <<
43033fde13f8SChao Yu 				COMPRESS_LEVEL_OFFSET;
43044c8ff709SChao Yu 	F2FS_I(inode)->i_flags |= F2FS_COMPR_FL;
43054c8ff709SChao Yu 	set_inode_flag(inode, FI_COMPRESSED_FILE);
43064c8ff709SChao Yu 	stat_inc_compr_inode(inode);
43075ac443e2SDaeho Jeong 	inc_compr_inode_stat(inode);
4308530e0704SChao Yu 	f2fs_mark_inode_dirty_sync(inode, true);
43094c8ff709SChao Yu }
43104c8ff709SChao Yu 
431178134d03SDaeho Jeong static inline bool f2fs_disable_compressed_file(struct inode *inode)
43124c8ff709SChao Yu {
43134c8ff709SChao Yu 	struct f2fs_inode_info *fi = F2FS_I(inode);
43144c8ff709SChao Yu 
43154c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
431678134d03SDaeho Jeong 		return true;
431702d58cd2SHyeong-Jun Kim 	if (S_ISREG(inode->i_mode) && F2FS_HAS_BLOCKS(inode))
431878134d03SDaeho Jeong 		return false;
43194c8ff709SChao Yu 
43204c8ff709SChao Yu 	fi->i_flags &= ~F2FS_COMPR_FL;
43214c8ff709SChao Yu 	stat_dec_compr_inode(inode);
432296f5b4faSChao Yu 	clear_inode_flag(inode, FI_COMPRESSED_FILE);
4323530e0704SChao Yu 	f2fs_mark_inode_dirty_sync(inode, true);
432478134d03SDaeho Jeong 	return true;
4325cde4de12SJaegeuk Kim }
4326cde4de12SJaegeuk Kim 
4327ccd31cb2SSheng Yong #define F2FS_FEATURE_FUNCS(name, flagname) \
43287beb01f7SChao Yu static inline int f2fs_sb_has_##name(struct f2fs_sb_info *sbi) \
4329ccd31cb2SSheng Yong { \
43307beb01f7SChao Yu 	return F2FS_HAS_FEATURE(sbi, F2FS_FEATURE_##flagname); \
4331cde4de12SJaegeuk Kim }
4332f424f664SJaegeuk Kim 
4333ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(encrypt, ENCRYPT);
4334ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(blkzoned, BLKZONED);
4335ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(extra_attr, EXTRA_ATTR);
4336ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(project_quota, PRJQUOTA);
4337ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_chksum, INODE_CHKSUM);
4338ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
4339ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
4340ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
4341b7c409deSSheng Yong F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
434295ae251fSEric Biggers F2FS_FEATURE_FUNCS(verity, VERITY);
4343d440c52dSJunling Zheng F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
43445aba5430SDaniel Rosenberg F2FS_FEATURE_FUNCS(casefold, CASEFOLD);
43454c8ff709SChao Yu F2FS_FEATURE_FUNCS(compression, COMPRESSION);
4346a7d9fe3cSJaegeuk Kim F2FS_FEATURE_FUNCS(readonly, RO);
43471c1d35dfSChao Yu 
43484215d054SDaeho Jeong static inline bool f2fs_may_extent_tree(struct inode *inode)
43494215d054SDaeho Jeong {
43504215d054SDaeho Jeong 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
43514215d054SDaeho Jeong 
43524215d054SDaeho Jeong 	if (!test_opt(sbi, EXTENT_CACHE) ||
43534215d054SDaeho Jeong 			is_inode_flag_set(inode, FI_NO_EXTENT) ||
43544215d054SDaeho Jeong 			(is_inode_flag_set(inode, FI_COMPRESSED_FILE) &&
43554215d054SDaeho Jeong 			 !f2fs_sb_has_readonly(sbi)))
43564215d054SDaeho Jeong 		return false;
43574215d054SDaeho Jeong 
43584215d054SDaeho Jeong 	/*
43594215d054SDaeho Jeong 	 * for recovered files during mount do not create extents
43604215d054SDaeho Jeong 	 * if shrinker is not registered.
43614215d054SDaeho Jeong 	 */
43624215d054SDaeho Jeong 	if (list_empty(&sbi->s_list))
43634215d054SDaeho Jeong 		return false;
43644215d054SDaeho Jeong 
43654215d054SDaeho Jeong 	return S_ISREG(inode->i_mode);
43664215d054SDaeho Jeong }
43674215d054SDaeho Jeong 
4368178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
436995175dafSDamien Le Moal static inline bool f2fs_blkz_is_seq(struct f2fs_sb_info *sbi, int devi,
437095175dafSDamien Le Moal 				    block_t blkaddr)
4371178053e2SDamien Le Moal {
4372178053e2SDamien Le Moal 	unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
4373178053e2SDamien Le Moal 
437495175dafSDamien Le Moal 	return test_bit(zno, FDEV(devi).blkz_seq);
4375178053e2SDamien Le Moal }
4376178053e2SDamien Le Moal #endif
4377178053e2SDamien Le Moal 
43787d20c8abSChao Yu static inline bool f2fs_hw_should_discard(struct f2fs_sb_info *sbi)
437996ba2decSDamien Le Moal {
43807beb01f7SChao Yu 	return f2fs_sb_has_blkzoned(sbi);
43817d20c8abSChao Yu }
438296ba2decSDamien Le Moal 
43837f3d7719SDamien Le Moal static inline bool f2fs_bdev_support_discard(struct block_device *bdev)
43847f3d7719SDamien Le Moal {
43857f3d7719SDamien Le Moal 	return blk_queue_discard(bdev_get_queue(bdev)) ||
43867f3d7719SDamien Le Moal 	       bdev_is_zoned(bdev);
43877f3d7719SDamien Le Moal }
43887f3d7719SDamien Le Moal 
43897d20c8abSChao Yu static inline bool f2fs_hw_support_discard(struct f2fs_sb_info *sbi)
43907d20c8abSChao Yu {
43917f3d7719SDamien Le Moal 	int i;
43927f3d7719SDamien Le Moal 
43937f3d7719SDamien Le Moal 	if (!f2fs_is_multi_device(sbi))
43947f3d7719SDamien Le Moal 		return f2fs_bdev_support_discard(sbi->sb->s_bdev);
43957f3d7719SDamien Le Moal 
43967f3d7719SDamien Le Moal 	for (i = 0; i < sbi->s_ndevs; i++)
43977f3d7719SDamien Le Moal 		if (f2fs_bdev_support_discard(FDEV(i).bdev))
43987f3d7719SDamien Le Moal 			return true;
43997f3d7719SDamien Le Moal 	return false;
44007d20c8abSChao Yu }
44017d20c8abSChao Yu 
44027d20c8abSChao Yu static inline bool f2fs_realtime_discard_enable(struct f2fs_sb_info *sbi)
44037d20c8abSChao Yu {
44047d20c8abSChao Yu 	return (test_opt(sbi, DISCARD) && f2fs_hw_support_discard(sbi)) ||
44057d20c8abSChao Yu 					f2fs_hw_should_discard(sbi);
440652763a4bSJaegeuk Kim }
440752763a4bSJaegeuk Kim 
4408f824deb5SChao Yu static inline bool f2fs_hw_is_readonly(struct f2fs_sb_info *sbi)
4409f824deb5SChao Yu {
4410f824deb5SChao Yu 	int i;
4411f824deb5SChao Yu 
4412f824deb5SChao Yu 	if (!f2fs_is_multi_device(sbi))
4413f824deb5SChao Yu 		return bdev_read_only(sbi->sb->s_bdev);
4414f824deb5SChao Yu 
4415f824deb5SChao Yu 	for (i = 0; i < sbi->s_ndevs; i++)
4416f824deb5SChao Yu 		if (bdev_read_only(FDEV(i).bdev))
4417f824deb5SChao Yu 			return true;
4418f824deb5SChao Yu 	return false;
4419f824deb5SChao Yu }
4420f824deb5SChao Yu 
4421b0332a0fSChao Yu static inline bool f2fs_lfs_mode(struct f2fs_sb_info *sbi)
442252763a4bSJaegeuk Kim {
4423b0332a0fSChao Yu 	return F2FS_OPTION(sbi).fs_mode == FS_MODE_LFS;
442452763a4bSJaegeuk Kim }
442552763a4bSJaegeuk Kim 
44264c8ff709SChao Yu static inline bool f2fs_may_compress(struct inode *inode)
44274c8ff709SChao Yu {
44284c8ff709SChao Yu 	if (IS_SWAPFILE(inode) || f2fs_is_pinned_file(inode) ||
44294c8ff709SChao Yu 				f2fs_is_atomic_file(inode) ||
44304c8ff709SChao Yu 				f2fs_is_volatile_file(inode))
44314c8ff709SChao Yu 		return false;
44324c8ff709SChao Yu 	return S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode);
44334c8ff709SChao Yu }
44344c8ff709SChao Yu 
44354c8ff709SChao Yu static inline void f2fs_i_compr_blocks_update(struct inode *inode,
44364c8ff709SChao Yu 						u64 blocks, bool add)
44374c8ff709SChao Yu {
44384c8ff709SChao Yu 	int diff = F2FS_I(inode)->i_cluster_size - blocks;
4439c2759ebaSDaeho Jeong 	struct f2fs_inode_info *fi = F2FS_I(inode);
44404c8ff709SChao Yu 
4441ef8d563fSChao Yu 	/* don't update i_compr_blocks if saved blocks were released */
4442c2759ebaSDaeho Jeong 	if (!add && !atomic_read(&fi->i_compr_blocks))
4443ef8d563fSChao Yu 		return;
4444ef8d563fSChao Yu 
44454c8ff709SChao Yu 	if (add) {
4446c2759ebaSDaeho Jeong 		atomic_add(diff, &fi->i_compr_blocks);
44474c8ff709SChao Yu 		stat_add_compr_blocks(inode, diff);
44484c8ff709SChao Yu 	} else {
4449c2759ebaSDaeho Jeong 		atomic_sub(diff, &fi->i_compr_blocks);
44504c8ff709SChao Yu 		stat_sub_compr_blocks(inode, diff);
44514c8ff709SChao Yu 	}
44524c8ff709SChao Yu 	f2fs_mark_inode_dirty_sync(inode, true);
44534c8ff709SChao Yu }
44544c8ff709SChao Yu 
4455f847c699SChao Yu static inline int block_unaligned_IO(struct inode *inode,
4456f847c699SChao Yu 				struct kiocb *iocb, struct iov_iter *iter)
4457b91050a8SHyunchul Lee {
4458f847c699SChao Yu 	unsigned int i_blkbits = READ_ONCE(inode->i_blkbits);
4459f847c699SChao Yu 	unsigned int blocksize_mask = (1 << i_blkbits) - 1;
4460f847c699SChao Yu 	loff_t offset = iocb->ki_pos;
4461f847c699SChao Yu 	unsigned long align = offset | iov_iter_alignment(iter);
4462f847c699SChao Yu 
4463f847c699SChao Yu 	return align & blocksize_mask;
4464f847c699SChao Yu }
4465f847c699SChao Yu 
446671f2c820SChao Yu static inline bool f2fs_allow_multi_device_dio(struct f2fs_sb_info *sbi,
446771f2c820SChao Yu 								int flag)
446871f2c820SChao Yu {
446971f2c820SChao Yu 	if (!f2fs_is_multi_device(sbi))
447071f2c820SChao Yu 		return false;
447171f2c820SChao Yu 	if (flag != F2FS_GET_BLOCK_DIO)
447271f2c820SChao Yu 		return false;
447371f2c820SChao Yu 	return sbi->aligned_blksize;
447471f2c820SChao Yu }
447571f2c820SChao Yu 
4476f847c699SChao Yu static inline bool f2fs_force_buffered_io(struct inode *inode,
4477f847c699SChao Yu 				struct kiocb *iocb, struct iov_iter *iter)
4478f847c699SChao Yu {
4479f847c699SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
4480f847c699SChao Yu 	int rw = iov_iter_rw(iter);
4481f847c699SChao Yu 
4482f847c699SChao Yu 	if (f2fs_post_read_required(inode))
4483f847c699SChao Yu 		return true;
448471f2c820SChao Yu 
448571f2c820SChao Yu 	/* disallow direct IO if any of devices has unaligned blksize */
448671f2c820SChao Yu 	if (f2fs_is_multi_device(sbi) && !sbi->aligned_blksize)
4487f847c699SChao Yu 		return true;
4488f847c699SChao Yu 	/*
4489f847c699SChao Yu 	 * for blkzoned device, fallback direct IO to buffered IO, so
4490f847c699SChao Yu 	 * all IOs can be serialized by log-structured write.
4491f847c699SChao Yu 	 */
44927beb01f7SChao Yu 	if (f2fs_sb_has_blkzoned(sbi))
4493f847c699SChao Yu 		return true;
4494b0332a0fSChao Yu 	if (f2fs_lfs_mode(sbi) && (rw == WRITE)) {
44959720ee80SChao Yu 		if (block_unaligned_IO(inode, iocb, iter))
4496f847c699SChao Yu 			return true;
44979720ee80SChao Yu 		if (F2FS_IO_ALIGNED(sbi))
44989720ee80SChao Yu 			return true;
44999720ee80SChao Yu 	}
4500ebc29b62Shuangjianan@oppo.com 	if (is_sbi_flag_set(F2FS_I_SB(inode), SBI_CP_DISABLED))
45014354994fSDaniel Rosenberg 		return true;
45024354994fSDaniel Rosenberg 
4503f847c699SChao Yu 	return false;
4504b91050a8SHyunchul Lee }
4505b91050a8SHyunchul Lee 
45067f59b277SEric Biggers static inline bool f2fs_need_verity(const struct inode *inode, pgoff_t idx)
45077f59b277SEric Biggers {
45087f59b277SEric Biggers 	return fsverity_active(inode) &&
45097f59b277SEric Biggers 	       idx < DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
45107f59b277SEric Biggers }
45117f59b277SEric Biggers 
451283a3bfdbSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
4513d494500aSChao Yu extern void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate,
4514d494500aSChao Yu 							unsigned int type);
451583a3bfdbSJaegeuk Kim #else
4516d494500aSChao Yu #define f2fs_build_fault_attr(sbi, rate, type)		do { } while (0)
451783a3bfdbSJaegeuk Kim #endif
451883a3bfdbSJaegeuk Kim 
4519af033b2aSChao Yu static inline bool is_journalled_quota(struct f2fs_sb_info *sbi)
4520af033b2aSChao Yu {
4521af033b2aSChao Yu #ifdef CONFIG_QUOTA
45227beb01f7SChao Yu 	if (f2fs_sb_has_quota_ino(sbi))
4523af033b2aSChao Yu 		return true;
4524af033b2aSChao Yu 	if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
4525af033b2aSChao Yu 		F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
4526af033b2aSChao Yu 		F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
4527af033b2aSChao Yu 		return true;
4528af033b2aSChao Yu #endif
4529af033b2aSChao Yu 	return false;
4530af033b2aSChao Yu }
45310af725fcSJaegeuk Kim 
45324f993264SChao Yu static inline bool f2fs_block_unit_discard(struct f2fs_sb_info *sbi)
45334f993264SChao Yu {
45344f993264SChao Yu 	return F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_BLOCK;
45354f993264SChao Yu }
45364f993264SChao Yu 
453710f966bbSChao Yu #define EFSBADCRC	EBADMSG		/* Bad CRC detected */
453810f966bbSChao Yu #define EFSCORRUPTED	EUCLEAN		/* Filesystem is corrupted */
453910f966bbSChao Yu 
4540e1074d4bSChao Yu #endif /* _LINUX_F2FS_H */
4541