xref: /openbmc/linux/fs/f2fs/f2fs.h (revision bab475c5414e8d1fa182fd17ae966864e9c85741)
17c1a000dSChao Yu // 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 
1139a53e0cSJaegeuk Kim #include <linux/types.h>
1239a53e0cSJaegeuk Kim #include <linux/page-flags.h>
1339a53e0cSJaegeuk Kim #include <linux/buffer_head.h>
1439a53e0cSJaegeuk Kim #include <linux/slab.h>
1539a53e0cSJaegeuk Kim #include <linux/crc32.h>
1639a53e0cSJaegeuk Kim #include <linux/magic.h>
17c2d715d1SJaegeuk Kim #include <linux/kobject.h>
187bd59381SGu Zheng #include <linux/sched.h>
197c2e5963SJaegeuk Kim #include <linux/cred.h>
2039307a8eSJaegeuk Kim #include <linux/vmalloc.h>
21740432f8SJaegeuk Kim #include <linux/bio.h>
22d0239e1bSJaegeuk Kim #include <linux/blkdev.h>
230abd675eSChao Yu #include <linux/quotaops.h>
2443b6573bSKeith Mok #include <crypto/hash.h>
2539a53e0cSJaegeuk Kim 
26734f0d24SDave Chinner #define __FS_HAS_ENCRYPTION IS_ENABLED(CONFIG_F2FS_FS_ENCRYPTION)
27734f0d24SDave Chinner #include <linux/fscrypt.h>
28734f0d24SDave Chinner 
295d56b671SJaegeuk Kim #ifdef CONFIG_F2FS_CHECK_FS
309850cf4aSJaegeuk Kim #define f2fs_bug_on(sbi, condition)	BUG_ON(condition)
315d56b671SJaegeuk Kim #else
329850cf4aSJaegeuk Kim #define f2fs_bug_on(sbi, condition)					\
339850cf4aSJaegeuk Kim 	do {								\
349850cf4aSJaegeuk Kim 		if (unlikely(condition)) {				\
359850cf4aSJaegeuk Kim 			WARN_ON(1);					\
36caf0047eSChao Yu 			set_sbi_flag(sbi, SBI_NEED_FSCK);		\
379850cf4aSJaegeuk Kim 		}							\
389850cf4aSJaegeuk Kim 	} while (0)
395d56b671SJaegeuk Kim #endif
405d56b671SJaegeuk Kim 
412c63feadSJaegeuk Kim enum {
422c63feadSJaegeuk Kim 	FAULT_KMALLOC,
43628b3d14SChao Yu 	FAULT_KVMALLOC,
44c41f3cc3SJaegeuk Kim 	FAULT_PAGE_ALLOC,
4501eccef7SChao Yu 	FAULT_PAGE_GET,
46d62fe971SChao Yu 	FAULT_ALLOC_BIO,
47cb78942bSJaegeuk Kim 	FAULT_ALLOC_NID,
48cb78942bSJaegeuk Kim 	FAULT_ORPHAN,
49cb78942bSJaegeuk Kim 	FAULT_BLOCK,
50cb78942bSJaegeuk Kim 	FAULT_DIR_DEPTH,
5153aa6bbfSJaegeuk Kim 	FAULT_EVICT_INODE,
5214b44d23SJaegeuk Kim 	FAULT_TRUNCATE,
536f5c2ed0SChao Yu 	FAULT_READ_IO,
540f348028SChao Yu 	FAULT_CHECKPOINT,
55b83dcfe6SChao Yu 	FAULT_DISCARD,
566f5c2ed0SChao Yu 	FAULT_WRITE_IO,
572c63feadSJaegeuk Kim 	FAULT_MAX,
582c63feadSJaegeuk Kim };
592c63feadSJaegeuk Kim 
607fa750a1SArnd Bergmann #ifdef CONFIG_F2FS_FAULT_INJECTION
61d494500aSChao Yu #define F2FS_ALL_FAULT_TYPE		((1 << FAULT_MAX) - 1)
62d494500aSChao Yu 
6308796897SSheng Yong struct f2fs_fault_info {
6408796897SSheng Yong 	atomic_t inject_ops;
6508796897SSheng Yong 	unsigned int inject_rate;
6608796897SSheng Yong 	unsigned int inject_type;
6708796897SSheng Yong };
6808796897SSheng Yong 
692d3a5856SGao Xiang extern char *f2fs_fault_name[FAULT_MAX];
7068afcf2dSTomohiro Kusumi #define IS_FAULT_SET(fi, type) ((fi)->inject_type & (1 << (type)))
712c63feadSJaegeuk Kim #endif
722c63feadSJaegeuk Kim 
7339a53e0cSJaegeuk Kim /*
7439a53e0cSJaegeuk Kim  * For mount options
7539a53e0cSJaegeuk Kim  */
7639a53e0cSJaegeuk Kim #define F2FS_MOUNT_BG_GC		0x00000001
7739a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_ROLL_FORWARD	0x00000002
7839a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISCARD		0x00000004
7939a53e0cSJaegeuk Kim #define F2FS_MOUNT_NOHEAP		0x00000008
8039a53e0cSJaegeuk Kim #define F2FS_MOUNT_XATTR_USER		0x00000010
8139a53e0cSJaegeuk Kim #define F2FS_MOUNT_POSIX_ACL		0x00000020
8239a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_EXT_IDENTIFY	0x00000040
83444c580fSJaegeuk Kim #define F2FS_MOUNT_INLINE_XATTR		0x00000080
841001b347SHuajun Li #define F2FS_MOUNT_INLINE_DATA		0x00000100
8534d67debSChao Yu #define F2FS_MOUNT_INLINE_DENTRY	0x00000200
8634d67debSChao Yu #define F2FS_MOUNT_FLUSH_MERGE		0x00000400
8734d67debSChao Yu #define F2FS_MOUNT_NOBARRIER		0x00000800
88d5053a34SJaegeuk Kim #define F2FS_MOUNT_FASTBOOT		0x00001000
8989672159SChao Yu #define F2FS_MOUNT_EXTENT_CACHE		0x00002000
906aefd93bSJaegeuk Kim #define F2FS_MOUNT_FORCE_FG_GC		0x00004000
91343f40f0SChao Yu #define F2FS_MOUNT_DATA_FLUSH		0x00008000
9273faec4dSJaegeuk Kim #define F2FS_MOUNT_FAULT_INJECTION	0x00010000
9336abef4eSJaegeuk Kim #define F2FS_MOUNT_ADAPTIVE		0x00020000
9436abef4eSJaegeuk Kim #define F2FS_MOUNT_LFS			0x00040000
950abd675eSChao Yu #define F2FS_MOUNT_USRQUOTA		0x00080000
960abd675eSChao Yu #define F2FS_MOUNT_GRPQUOTA		0x00100000
975c57132eSChao Yu #define F2FS_MOUNT_PRJQUOTA		0x00200000
984b2414d0SChao Yu #define F2FS_MOUNT_QUOTA		0x00400000
996afc662eSChao Yu #define F2FS_MOUNT_INLINE_XATTR_SIZE	0x00800000
1007e65be49SJaegeuk Kim #define F2FS_MOUNT_RESERVE_ROOT		0x01000000
10139a53e0cSJaegeuk Kim 
10263189b78SChao Yu #define F2FS_OPTION(sbi)	((sbi)->mount_opt)
10363189b78SChao Yu #define clear_opt(sbi, option)	(F2FS_OPTION(sbi).opt &= ~F2FS_MOUNT_##option)
10463189b78SChao Yu #define set_opt(sbi, option)	(F2FS_OPTION(sbi).opt |= F2FS_MOUNT_##option)
10563189b78SChao Yu #define test_opt(sbi, option)	(F2FS_OPTION(sbi).opt & F2FS_MOUNT_##option)
10639a53e0cSJaegeuk Kim 
10739a53e0cSJaegeuk Kim #define ver_after(a, b)	(typecheck(unsigned long long, a) &&		\
10839a53e0cSJaegeuk Kim 		typecheck(unsigned long long, b) &&			\
10939a53e0cSJaegeuk Kim 		((long long)((a) - (b)) > 0))
11039a53e0cSJaegeuk Kim 
111a9841c4dSJaegeuk Kim typedef u32 block_t;	/*
112a9841c4dSJaegeuk Kim 			 * should not change u32, since it is the on-disk block
113a9841c4dSJaegeuk Kim 			 * address format, __le32.
114a9841c4dSJaegeuk Kim 			 */
11539a53e0cSJaegeuk Kim typedef u32 nid_t;
11639a53e0cSJaegeuk Kim 
11739a53e0cSJaegeuk Kim struct f2fs_mount_info {
11839a53e0cSJaegeuk Kim 	unsigned int opt;
11963189b78SChao Yu 	int write_io_size_bits;		/* Write IO size bits */
12063189b78SChao Yu 	block_t root_reserved_blocks;	/* root reserved blocks */
12163189b78SChao Yu 	kuid_t s_resuid;		/* reserved blocks for uid */
12263189b78SChao Yu 	kgid_t s_resgid;		/* reserved blocks for gid */
12363189b78SChao Yu 	int active_logs;		/* # of active logs */
12463189b78SChao Yu 	int inline_xattr_size;		/* inline xattr size */
12563189b78SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
12663189b78SChao Yu 	struct f2fs_fault_info fault_info;	/* For fault injection */
12763189b78SChao Yu #endif
12863189b78SChao Yu #ifdef CONFIG_QUOTA
12963189b78SChao Yu 	/* Names of quota files with journalled quota */
13063189b78SChao Yu 	char *s_qf_names[MAXQUOTAS];
13163189b78SChao Yu 	int s_jquota_fmt;			/* Format of quota to use */
13263189b78SChao Yu #endif
13363189b78SChao Yu 	/* For which write hints are passed down to block layer */
13463189b78SChao Yu 	int whint_mode;
13563189b78SChao Yu 	int alloc_mode;			/* segment allocation policy */
13663189b78SChao Yu 	int fsync_mode;			/* fsync policy */
137ff62af20SSheng Yong 	bool test_dummy_encryption;	/* test dummy encryption */
13839a53e0cSJaegeuk Kim };
13939a53e0cSJaegeuk Kim 
140cde4de12SJaegeuk Kim #define F2FS_FEATURE_ENCRYPT		0x0001
1410bfd7a09SDamien Le Moal #define F2FS_FEATURE_BLKZONED		0x0002
142e65ef207SJaegeuk Kim #define F2FS_FEATURE_ATOMIC_WRITE	0x0004
1437a2af766SChao Yu #define F2FS_FEATURE_EXTRA_ATTR		0x0008
1445c57132eSChao Yu #define F2FS_FEATURE_PRJQUOTA		0x0010
145704956ecSChao Yu #define F2FS_FEATURE_INODE_CHKSUM	0x0020
1466afc662eSChao Yu #define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR	0x0040
147234a9689SJaegeuk Kim #define F2FS_FEATURE_QUOTA_INO		0x0080
1481c1d35dfSChao Yu #define F2FS_FEATURE_INODE_CRTIME	0x0100
149b7c409deSSheng Yong #define F2FS_FEATURE_LOST_FOUND		0x0200
15053fedcc0SEric Biggers #define F2FS_FEATURE_VERITY		0x0400	/* reserved */
151d440c52dSJunling Zheng #define F2FS_FEATURE_SB_CHKSUM		0x0800
152cde4de12SJaegeuk Kim 
15376f105a2SJaegeuk Kim #define F2FS_HAS_FEATURE(sb, mask)					\
15476f105a2SJaegeuk Kim 	((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
15576f105a2SJaegeuk Kim #define F2FS_SET_FEATURE(sb, mask)					\
156c64ab12eSDongOh Shin 	(F2FS_SB(sb)->raw_super->feature |= cpu_to_le32(mask))
15776f105a2SJaegeuk Kim #define F2FS_CLEAR_FEATURE(sb, mask)					\
158c64ab12eSDongOh Shin 	(F2FS_SB(sb)->raw_super->feature &= ~cpu_to_le32(mask))
15976f105a2SJaegeuk Kim 
16039a53e0cSJaegeuk Kim /*
1617c2e5963SJaegeuk Kim  * Default values for user and/or group using reserved blocks
1627c2e5963SJaegeuk Kim  */
1637c2e5963SJaegeuk Kim #define	F2FS_DEF_RESUID		0
1647c2e5963SJaegeuk Kim #define	F2FS_DEF_RESGID		0
1657c2e5963SJaegeuk Kim 
1667c2e5963SJaegeuk Kim /*
16739a53e0cSJaegeuk Kim  * For checkpoint manager
16839a53e0cSJaegeuk Kim  */
16939a53e0cSJaegeuk Kim enum {
17039a53e0cSJaegeuk Kim 	NAT_BITMAP,
17139a53e0cSJaegeuk Kim 	SIT_BITMAP
17239a53e0cSJaegeuk Kim };
17339a53e0cSJaegeuk Kim 
174c473f1a9SChao Yu #define	CP_UMOUNT	0x00000001
175c473f1a9SChao Yu #define	CP_FASTBOOT	0x00000002
176c473f1a9SChao Yu #define	CP_SYNC		0x00000004
177c473f1a9SChao Yu #define	CP_RECOVERY	0x00000008
178c473f1a9SChao Yu #define	CP_DISCARD	0x00000010
1791f43e2adSChao Yu #define CP_TRIMMED	0x00000020
18075ab4cb8SJaegeuk Kim 
1814ddb1a4dSJaegeuk Kim #define MAX_DISCARD_BLOCKS(sbi)		BLKS_PER_SEC(sbi)
182ecc9aa00SChao Yu #define DEF_MAX_DISCARD_REQUEST		8	/* issue 8 discards per round */
183969d1b18SChao Yu #define DEF_MIN_DISCARD_ISSUE_TIME	50	/* 50 ms, if exists */
184f9d1dcedSYunlei He #define DEF_MID_DISCARD_ISSUE_TIME	500	/* 500 ms, if device busy */
185969d1b18SChao Yu #define DEF_MAX_DISCARD_ISSUE_TIME	60000	/* 60 s, if no candidates */
1868bb4f253SJaegeuk Kim #define DEF_DISCARD_URGENT_UTIL		80	/* do more discard over 80% */
18760b99b48SJaegeuk Kim #define DEF_CP_INTERVAL			60	/* 60 secs */
188dcf25fe8SChao Yu #define DEF_IDLE_INTERVAL		5	/* 5 secs */
189bba681cbSJaegeuk Kim 
19075ab4cb8SJaegeuk Kim struct cp_control {
19175ab4cb8SJaegeuk Kim 	int reason;
1924b2fecc8SJaegeuk Kim 	__u64 trim_start;
1934b2fecc8SJaegeuk Kim 	__u64 trim_end;
1944b2fecc8SJaegeuk Kim 	__u64 trim_minlen;
19575ab4cb8SJaegeuk Kim };
19675ab4cb8SJaegeuk Kim 
197662befdaSChao Yu /*
198e1da7872SChao Yu  * indicate meta/data type
199662befdaSChao Yu  */
200662befdaSChao Yu enum {
201662befdaSChao Yu 	META_CP,
202662befdaSChao Yu 	META_NAT,
20381c1a0f1SChao Yu 	META_SIT,
2044c521f49SJaegeuk Kim 	META_SSA,
205b63e7be5SChao Yu 	META_MAX,
2064c521f49SJaegeuk Kim 	META_POR,
207e1da7872SChao Yu 	DATA_GENERIC,
208e1da7872SChao Yu 	META_GENERIC,
209662befdaSChao Yu };
210662befdaSChao Yu 
2116451e041SJaegeuk Kim /* for the list of ino */
2126451e041SJaegeuk Kim enum {
2136451e041SJaegeuk Kim 	ORPHAN_INO,		/* for orphan ino list */
214fff04f90SJaegeuk Kim 	APPEND_INO,		/* for append ino list */
215fff04f90SJaegeuk Kim 	UPDATE_INO,		/* for update ino list */
2160a007b97SJaegeuk Kim 	TRANS_DIR_INO,		/* for trasactions dir ino list */
21739d787beSChao Yu 	FLUSH_INO,		/* for multiple device flushing */
2186451e041SJaegeuk Kim 	MAX_INO_ENTRY,		/* max. list */
2196451e041SJaegeuk Kim };
2206451e041SJaegeuk Kim 
2216451e041SJaegeuk Kim struct ino_entry {
22239a53e0cSJaegeuk Kim 	struct list_head list;		/* list head */
22339a53e0cSJaegeuk Kim 	nid_t ino;			/* inode number */
22439d787beSChao Yu 	unsigned int dirty_device;	/* dirty device bitmap */
22539a53e0cSJaegeuk Kim };
22639a53e0cSJaegeuk Kim 
2272710fd7eSChao Yu /* for the list of inodes to be GCed */
22806292073SChao Yu struct inode_entry {
22939a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
23039a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
23139a53e0cSJaegeuk Kim };
23239a53e0cSJaegeuk Kim 
23350fa53ecSChao Yu struct fsync_node_entry {
23450fa53ecSChao Yu 	struct list_head list;	/* list head */
23550fa53ecSChao Yu 	struct page *page;	/* warm node page pointer */
23650fa53ecSChao Yu 	unsigned int seq_id;	/* sequence id */
23750fa53ecSChao Yu };
23850fa53ecSChao Yu 
239a7eeb823SChao Yu /* for the bitmap indicate blocks to be discarded */
2407fd9e544SJaegeuk Kim struct discard_entry {
2417fd9e544SJaegeuk Kim 	struct list_head list;	/* list head */
242a7eeb823SChao Yu 	block_t start_blkaddr;	/* start blockaddr of current segment */
243a7eeb823SChao Yu 	unsigned char discard_map[SIT_VBLOCK_MAP_SIZE];	/* segment discard bitmap */
2447fd9e544SJaegeuk Kim };
2457fd9e544SJaegeuk Kim 
246969d1b18SChao Yu /* default discard granularity of inner discard thread, unit: block count */
247969d1b18SChao Yu #define DEFAULT_DISCARD_GRANULARITY		16
248969d1b18SChao Yu 
249ba48a33eSChao Yu /* max discard pend list number */
250ba48a33eSChao Yu #define MAX_PLIST_NUM		512
251ba48a33eSChao Yu #define plist_idx(blk_num)	((blk_num) >= MAX_PLIST_NUM ?		\
252ba48a33eSChao Yu 					(MAX_PLIST_NUM - 1) : (blk_num - 1))
253ba48a33eSChao Yu 
25415469963SJaegeuk Kim enum {
25535ec7d57SChao Yu 	D_PREP,			/* initial */
25635ec7d57SChao Yu 	D_PARTIAL,		/* partially submitted */
25735ec7d57SChao Yu 	D_SUBMIT,		/* all submitted */
25835ec7d57SChao Yu 	D_DONE,			/* finished */
25915469963SJaegeuk Kim };
26015469963SJaegeuk Kim 
261004b6862SChao Yu struct discard_info {
262b01a9201SJaegeuk Kim 	block_t lstart;			/* logical start address */
263b01a9201SJaegeuk Kim 	block_t len;			/* length */
264004b6862SChao Yu 	block_t start;			/* actual start address in dev */
265004b6862SChao Yu };
266004b6862SChao Yu 
267b01a9201SJaegeuk Kim struct discard_cmd {
268004b6862SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
269004b6862SChao Yu 	union {
270004b6862SChao Yu 		struct {
271004b6862SChao Yu 			block_t lstart;	/* logical start address */
272004b6862SChao Yu 			block_t len;	/* length */
273004b6862SChao Yu 			block_t start;	/* actual start address in dev */
274004b6862SChao Yu 		};
275004b6862SChao Yu 		struct discard_info di;	/* discard info */
276004b6862SChao Yu 
277004b6862SChao Yu 	};
278b01a9201SJaegeuk Kim 	struct list_head list;		/* command list */
279b01a9201SJaegeuk Kim 	struct completion wait;		/* compleation */
280c81abe34SJaegeuk Kim 	struct block_device *bdev;	/* bdev */
281ec9895adSChao Yu 	unsigned short ref;		/* reference count */
2829a744b92SChao Yu 	unsigned char state;		/* state */
28335ec7d57SChao Yu 	unsigned char issuing;		/* issuing discard */
284c81abe34SJaegeuk Kim 	int error;			/* bio error */
28535ec7d57SChao Yu 	spinlock_t lock;		/* for state/bio_ref updating */
28635ec7d57SChao Yu 	unsigned short bio_ref;		/* bio reference count */
287275b66b0SChao Yu };
288275b66b0SChao Yu 
28978997b56SChao Yu enum {
29078997b56SChao Yu 	DPOLICY_BG,
29178997b56SChao Yu 	DPOLICY_FORCE,
29278997b56SChao Yu 	DPOLICY_FSTRIM,
29378997b56SChao Yu 	DPOLICY_UMOUNT,
29478997b56SChao Yu 	MAX_DPOLICY,
29578997b56SChao Yu };
29678997b56SChao Yu 
297ecc9aa00SChao Yu struct discard_policy {
29878997b56SChao Yu 	int type;			/* type of discard */
299ecc9aa00SChao Yu 	unsigned int min_interval;	/* used for candidates exist */
300f9d1dcedSYunlei He 	unsigned int mid_interval;	/* used for device busy */
301ecc9aa00SChao Yu 	unsigned int max_interval;	/* used for candidates not exist */
302ecc9aa00SChao Yu 	unsigned int max_requests;	/* # of discards issued per round */
303ecc9aa00SChao Yu 	unsigned int io_aware_gran;	/* minimum granularity discard not be aware of I/O */
304ecc9aa00SChao Yu 	bool io_aware;			/* issue discard in idle time */
305ecc9aa00SChao Yu 	bool sync;			/* submit discard with REQ_SYNC flag */
30620ee4382SChao Yu 	bool ordered;			/* issue discard by lba order */
30778997b56SChao Yu 	unsigned int granularity;	/* discard granularity */
308ecc9aa00SChao Yu };
309ecc9aa00SChao Yu 
3100b54fb84SJaegeuk Kim struct discard_cmd_control {
31115469963SJaegeuk Kim 	struct task_struct *f2fs_issue_discard;	/* discard thread */
31246f84c2cSChao Yu 	struct list_head entry_list;		/* 4KB discard entry list */
313ba48a33eSChao Yu 	struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
31446f84c2cSChao Yu 	struct list_head wait_list;		/* store on-flushing entries */
3158412663dSChao Yu 	struct list_head fstrim_list;		/* in-flight discard from fstrim */
31615469963SJaegeuk Kim 	wait_queue_head_t discard_wait_queue;	/* waiting queue for wake-up */
317969d1b18SChao Yu 	unsigned int discard_wake;		/* to wake up discard thread */
31815469963SJaegeuk Kim 	struct mutex cmd_lock;
319d618ebafSChao Yu 	unsigned int nr_discards;		/* # of discards in the list */
320d618ebafSChao Yu 	unsigned int max_discards;		/* max. discards to be issued */
321969d1b18SChao Yu 	unsigned int discard_granularity;	/* discard granularity */
322d84d1cbdSChao Yu 	unsigned int undiscard_blks;		/* # of undiscard blocks */
32320ee4382SChao Yu 	unsigned int next_pos;			/* next discard position */
3248b8dd65fSChao Yu 	atomic_t issued_discard;		/* # of issued discard */
3258b8dd65fSChao Yu 	atomic_t issing_discard;		/* # of issing discard */
3265f32366aSChao Yu 	atomic_t discard_cmd_cnt;		/* # of cached cmd count */
327004b6862SChao Yu 	struct rb_root root;			/* root of discard rb-tree */
32867fce70bSChao Yu 	bool rbtree_check;			/* config for consistence check */
32939a53e0cSJaegeuk Kim };
33039a53e0cSJaegeuk Kim 
33139a53e0cSJaegeuk Kim /* for the list of fsync inodes, used only during recovery */
33239a53e0cSJaegeuk Kim struct fsync_inode_entry {
33339a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
33439a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
335c52e1b10SJaegeuk Kim 	block_t blkaddr;	/* block address locating the last fsync */
336c52e1b10SJaegeuk Kim 	block_t last_dentry;	/* block address locating the last dentry */
33739a53e0cSJaegeuk Kim };
33839a53e0cSJaegeuk Kim 
33968afcf2dSTomohiro Kusumi #define nats_in_cursum(jnl)		(le16_to_cpu((jnl)->n_nats))
34068afcf2dSTomohiro Kusumi #define sits_in_cursum(jnl)		(le16_to_cpu((jnl)->n_sits))
34139a53e0cSJaegeuk Kim 
34268afcf2dSTomohiro Kusumi #define nat_in_journal(jnl, i)		((jnl)->nat_j.entries[i].ne)
34368afcf2dSTomohiro Kusumi #define nid_in_journal(jnl, i)		((jnl)->nat_j.entries[i].nid)
34468afcf2dSTomohiro Kusumi #define sit_in_journal(jnl, i)		((jnl)->sit_j.entries[i].se)
34568afcf2dSTomohiro Kusumi #define segno_in_journal(jnl, i)	((jnl)->sit_j.entries[i].segno)
34639a53e0cSJaegeuk Kim 
347dfc08a12SChao Yu #define MAX_NAT_JENTRIES(jnl)	(NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
348dfc08a12SChao Yu #define MAX_SIT_JENTRIES(jnl)	(SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
349309cc2b6SJaegeuk Kim 
350dfc08a12SChao Yu static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
35139a53e0cSJaegeuk Kim {
352dfc08a12SChao Yu 	int before = nats_in_cursum(journal);
353cac5a3d8SDongOh Shin 
354dfc08a12SChao Yu 	journal->n_nats = cpu_to_le16(before + i);
35539a53e0cSJaegeuk Kim 	return before;
35639a53e0cSJaegeuk Kim }
35739a53e0cSJaegeuk Kim 
358dfc08a12SChao Yu static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
35939a53e0cSJaegeuk Kim {
360dfc08a12SChao Yu 	int before = sits_in_cursum(journal);
361cac5a3d8SDongOh Shin 
362dfc08a12SChao Yu 	journal->n_sits = cpu_to_le16(before + i);
36339a53e0cSJaegeuk Kim 	return before;
36439a53e0cSJaegeuk Kim }
36539a53e0cSJaegeuk Kim 
366dfc08a12SChao Yu static inline bool __has_cursum_space(struct f2fs_journal *journal,
367dfc08a12SChao Yu 							int size, int type)
368184a5cd2SChao Yu {
369184a5cd2SChao Yu 	if (type == NAT_JOURNAL)
370dfc08a12SChao Yu 		return size <= MAX_NAT_JENTRIES(journal);
371dfc08a12SChao Yu 	return size <= MAX_SIT_JENTRIES(journal);
372184a5cd2SChao Yu }
373184a5cd2SChao Yu 
37439a53e0cSJaegeuk Kim /*
375e9750824SNamjae Jeon  * ioctl commands
376e9750824SNamjae Jeon  */
377e9750824SNamjae Jeon #define F2FS_IOC_GETFLAGS		FS_IOC_GETFLAGS
378e9750824SNamjae Jeon #define F2FS_IOC_SETFLAGS		FS_IOC_SETFLAGS
379d49f3e89SChao Yu #define F2FS_IOC_GETVERSION		FS_IOC_GETVERSION
380e9750824SNamjae Jeon 
38188b88a66SJaegeuk Kim #define F2FS_IOCTL_MAGIC		0xf5
38288b88a66SJaegeuk Kim #define F2FS_IOC_START_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 1)
38388b88a66SJaegeuk Kim #define F2FS_IOC_COMMIT_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 2)
38402a1335fSJaegeuk Kim #define F2FS_IOC_START_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 3)
3851e84371fSJaegeuk Kim #define F2FS_IOC_RELEASE_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 4)
3861e84371fSJaegeuk Kim #define F2FS_IOC_ABORT_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 5)
387d07efb50SJaegeuk Kim #define F2FS_IOC_GARBAGE_COLLECT	_IOW(F2FS_IOCTL_MAGIC, 6, __u32)
388456b88e4SChao Yu #define F2FS_IOC_WRITE_CHECKPOINT	_IO(F2FS_IOCTL_MAGIC, 7)
389d07efb50SJaegeuk Kim #define F2FS_IOC_DEFRAGMENT		_IOWR(F2FS_IOCTL_MAGIC, 8,	\
390d07efb50SJaegeuk Kim 						struct f2fs_defragment)
3914dd6f977SJaegeuk Kim #define F2FS_IOC_MOVE_RANGE		_IOWR(F2FS_IOCTL_MAGIC, 9,	\
3924dd6f977SJaegeuk Kim 						struct f2fs_move_range)
393e066b83cSJaegeuk Kim #define F2FS_IOC_FLUSH_DEVICE		_IOW(F2FS_IOCTL_MAGIC, 10,	\
394e066b83cSJaegeuk Kim 						struct f2fs_flush_device)
39534dc77adSJaegeuk Kim #define F2FS_IOC_GARBAGE_COLLECT_RANGE	_IOW(F2FS_IOCTL_MAGIC, 11,	\
39634dc77adSJaegeuk Kim 						struct f2fs_gc_range)
397e65ef207SJaegeuk Kim #define F2FS_IOC_GET_FEATURES		_IOR(F2FS_IOCTL_MAGIC, 12, __u32)
3981ad71a27SJaegeuk Kim #define F2FS_IOC_SET_PIN_FILE		_IOW(F2FS_IOCTL_MAGIC, 13, __u32)
3991ad71a27SJaegeuk Kim #define F2FS_IOC_GET_PIN_FILE		_IOR(F2FS_IOCTL_MAGIC, 14, __u32)
400c4020b2dSChao Yu #define F2FS_IOC_PRECACHE_EXTENTS	_IO(F2FS_IOCTL_MAGIC, 15)
40188b88a66SJaegeuk Kim 
4020b81d077SJaegeuk Kim #define F2FS_IOC_SET_ENCRYPTION_POLICY	FS_IOC_SET_ENCRYPTION_POLICY
4030b81d077SJaegeuk Kim #define F2FS_IOC_GET_ENCRYPTION_POLICY	FS_IOC_GET_ENCRYPTION_POLICY
4040b81d077SJaegeuk Kim #define F2FS_IOC_GET_ENCRYPTION_PWSALT	FS_IOC_GET_ENCRYPTION_PWSALT
405f424f664SJaegeuk Kim 
4061abff93dSJaegeuk Kim /*
4071abff93dSJaegeuk Kim  * should be same as XFS_IOC_GOINGDOWN.
4081abff93dSJaegeuk Kim  * Flags for going down operation used by FS_IOC_GOINGDOWN
4091abff93dSJaegeuk Kim  */
4101abff93dSJaegeuk Kim #define F2FS_IOC_SHUTDOWN	_IOR('X', 125, __u32)	/* Shutdown */
4111abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_FULLSYNC	0x0	/* going down with full sync */
4121abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_METASYNC	0x1	/* going down with metadata */
4131abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_NOSYNC		0x2	/* going down */
414c912a829SJaegeuk Kim #define F2FS_GOING_DOWN_METAFLUSH	0x3	/* going down with meta flush */
4151abff93dSJaegeuk Kim 
416e9750824SNamjae Jeon #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
417e9750824SNamjae Jeon /*
418e9750824SNamjae Jeon  * ioctl commands in 32 bit emulation
419e9750824SNamjae Jeon  */
420e9750824SNamjae Jeon #define F2FS_IOC32_GETFLAGS		FS_IOC32_GETFLAGS
421e9750824SNamjae Jeon #define F2FS_IOC32_SETFLAGS		FS_IOC32_SETFLAGS
42204ef4b62SChao Yu #define F2FS_IOC32_GETVERSION		FS_IOC32_GETVERSION
423e9750824SNamjae Jeon #endif
424e9750824SNamjae Jeon 
4252c1d0305SChao Yu #define F2FS_IOC_FSGETXATTR		FS_IOC_FSGETXATTR
4262c1d0305SChao Yu #define F2FS_IOC_FSSETXATTR		FS_IOC_FSSETXATTR
4272c1d0305SChao Yu 
42834dc77adSJaegeuk Kim struct f2fs_gc_range {
42934dc77adSJaegeuk Kim 	u32 sync;
43034dc77adSJaegeuk Kim 	u64 start;
43134dc77adSJaegeuk Kim 	u64 len;
43234dc77adSJaegeuk Kim };
43334dc77adSJaegeuk Kim 
434d323d005SChao Yu struct f2fs_defragment {
435d323d005SChao Yu 	u64 start;
436d323d005SChao Yu 	u64 len;
437d323d005SChao Yu };
438d323d005SChao Yu 
4394dd6f977SJaegeuk Kim struct f2fs_move_range {
4404dd6f977SJaegeuk Kim 	u32 dst_fd;		/* destination fd */
4414dd6f977SJaegeuk Kim 	u64 pos_in;		/* start position in src_fd */
4424dd6f977SJaegeuk Kim 	u64 pos_out;		/* start position in dst_fd */
4434dd6f977SJaegeuk Kim 	u64 len;		/* size to move */
4444dd6f977SJaegeuk Kim };
4454dd6f977SJaegeuk Kim 
446e066b83cSJaegeuk Kim struct f2fs_flush_device {
447e066b83cSJaegeuk Kim 	u32 dev_num;		/* device number to flush */
448e066b83cSJaegeuk Kim 	u32 segments;		/* # of segments to flush */
449e066b83cSJaegeuk Kim };
450e066b83cSJaegeuk Kim 
451f2470371SChao Yu /* for inline stuff */
452f2470371SChao Yu #define DEF_INLINE_RESERVED_SIZE	1
4536afc662eSChao Yu #define DEF_MIN_INLINE_SIZE		1
4547a2af766SChao Yu static inline int get_extra_isize(struct inode *inode);
4556afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode);
4567a2af766SChao Yu #define MAX_INLINE_DATA(inode)	(sizeof(__le32) *			\
4577a2af766SChao Yu 				(CUR_ADDRS_PER_INODE(inode) -		\
458b323fd28SChao Yu 				get_inline_xattr_addrs(inode) -	\
4596afc662eSChao Yu 				DEF_INLINE_RESERVED_SIZE))
460f2470371SChao Yu 
461f2470371SChao Yu /* for inline dir */
462f2470371SChao Yu #define NR_INLINE_DENTRY(inode)	(MAX_INLINE_DATA(inode) * BITS_PER_BYTE / \
463f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
464f2470371SChao Yu 				BITS_PER_BYTE + 1))
465f2470371SChao Yu #define INLINE_DENTRY_BITMAP_SIZE(inode)	((NR_INLINE_DENTRY(inode) + \
466f2470371SChao Yu 					BITS_PER_BYTE - 1) / BITS_PER_BYTE)
467f2470371SChao Yu #define INLINE_RESERVED_SIZE(inode)	(MAX_INLINE_DATA(inode) - \
468f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
469f2470371SChao Yu 				NR_INLINE_DENTRY(inode) + \
470f2470371SChao Yu 				INLINE_DENTRY_BITMAP_SIZE(inode)))
471f2470371SChao Yu 
472e9750824SNamjae Jeon /*
47339a53e0cSJaegeuk Kim  * For INODE and NODE manager
47439a53e0cSJaegeuk Kim  */
4757b3cd7d6SJaegeuk Kim /* for directory operations */
4767b3cd7d6SJaegeuk Kim struct f2fs_dentry_ptr {
477d8c6822aSJaegeuk Kim 	struct inode *inode;
47876a9dd85SChao Yu 	void *bitmap;
4797b3cd7d6SJaegeuk Kim 	struct f2fs_dir_entry *dentry;
4807b3cd7d6SJaegeuk Kim 	__u8 (*filename)[F2FS_SLOT_LEN];
4817b3cd7d6SJaegeuk Kim 	int max;
48276a9dd85SChao Yu 	int nr_bitmap;
4837b3cd7d6SJaegeuk Kim };
4847b3cd7d6SJaegeuk Kim 
48564c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_block(struct inode *inode,
48664c24ecbSTomohiro Kusumi 		struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
4877b3cd7d6SJaegeuk Kim {
488d8c6822aSJaegeuk Kim 	d->inode = inode;
4897b3cd7d6SJaegeuk Kim 	d->max = NR_DENTRY_IN_BLOCK;
49076a9dd85SChao Yu 	d->nr_bitmap = SIZE_OF_DENTRY_BITMAP;
491c79d1520SYunlong Song 	d->bitmap = t->dentry_bitmap;
4927b3cd7d6SJaegeuk Kim 	d->dentry = t->dentry;
4937b3cd7d6SJaegeuk Kim 	d->filename = t->filename;
49464c24ecbSTomohiro Kusumi }
495cac5a3d8SDongOh Shin 
49664c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_inline(struct inode *inode,
497f2470371SChao Yu 					struct f2fs_dentry_ptr *d, void *t)
49864c24ecbSTomohiro Kusumi {
499f2470371SChao Yu 	int entry_cnt = NR_INLINE_DENTRY(inode);
500f2470371SChao Yu 	int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(inode);
501f2470371SChao Yu 	int reserved_size = INLINE_RESERVED_SIZE(inode);
502f2470371SChao Yu 
50364c24ecbSTomohiro Kusumi 	d->inode = inode;
504f2470371SChao Yu 	d->max = entry_cnt;
505f2470371SChao Yu 	d->nr_bitmap = bitmap_size;
506f2470371SChao Yu 	d->bitmap = t;
507f2470371SChao Yu 	d->dentry = t + bitmap_size + reserved_size;
508f2470371SChao Yu 	d->filename = t + bitmap_size + reserved_size +
509f2470371SChao Yu 					SIZE_OF_DIR_ENTRY * entry_cnt;
5107b3cd7d6SJaegeuk Kim }
5117b3cd7d6SJaegeuk Kim 
512dbe6a5ffSJaegeuk Kim /*
513dbe6a5ffSJaegeuk Kim  * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
514dbe6a5ffSJaegeuk Kim  * as its node offset to distinguish from index node blocks.
515dbe6a5ffSJaegeuk Kim  * But some bits are used to mark the node block.
51639a53e0cSJaegeuk Kim  */
517dbe6a5ffSJaegeuk Kim #define XATTR_NODE_OFFSET	((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
518dbe6a5ffSJaegeuk Kim 				>> OFFSET_BIT_SHIFT)
519266e97a8SJaegeuk Kim enum {
520266e97a8SJaegeuk Kim 	ALLOC_NODE,			/* allocate a new node page if needed */
521266e97a8SJaegeuk Kim 	LOOKUP_NODE,			/* look up a node without readahead */
522266e97a8SJaegeuk Kim 	LOOKUP_NODE_RA,			/*
523266e97a8SJaegeuk Kim 					 * look up a node with readahead called
5244f4124d0SChao Yu 					 * by get_data_block.
52539a53e0cSJaegeuk Kim 					 */
526266e97a8SJaegeuk Kim };
527266e97a8SJaegeuk Kim 
5287735730dSChao Yu #define DEFAULT_RETRY_IO_COUNT	8	/* maximum retry read IO count */
5297735730dSChao Yu 
530a6db67f0SJaegeuk Kim #define F2FS_LINK_MAX	0xffffffff	/* maximum link count per file */
53139a53e0cSJaegeuk Kim 
532817202d9SChao Yu #define MAX_DIR_RA_PAGES	4	/* maximum ra pages of dir */
533817202d9SChao Yu 
53439a53e0cSJaegeuk Kim /* for in-memory extent cache entry */
53513054c54SChao Yu #define F2FS_MIN_EXTENT_LEN	64	/* minimum extent length */
53613054c54SChao Yu 
53713054c54SChao Yu /* number of extent info in extent cache we try to shrink */
53813054c54SChao Yu #define EXTENT_CACHE_SHRINK_NUMBER	128
539c11abd1aSJaegeuk Kim 
54054c2258cSChao Yu struct rb_entry {
54154c2258cSChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
54254c2258cSChao Yu 	unsigned int ofs;		/* start offset of the entry */
54354c2258cSChao Yu 	unsigned int len;		/* length of the entry */
54454c2258cSChao Yu };
54554c2258cSChao Yu 
54639a53e0cSJaegeuk Kim struct extent_info {
54739a53e0cSJaegeuk Kim 	unsigned int fofs;		/* start offset in a file */
548111d2495SMasanari Iida 	unsigned int len;		/* length of the extent */
54954c2258cSChao Yu 	u32 blk;			/* start block address of the extent */
55039a53e0cSJaegeuk Kim };
55139a53e0cSJaegeuk Kim 
55213054c54SChao Yu struct extent_node {
55354c2258cSChao Yu 	struct rb_node rb_node;
55454c2258cSChao Yu 	union {
55554c2258cSChao Yu 		struct {
55654c2258cSChao Yu 			unsigned int fofs;
55754c2258cSChao Yu 			unsigned int len;
55854c2258cSChao Yu 			u32 blk;
55954c2258cSChao Yu 		};
56013054c54SChao Yu 		struct extent_info ei;	/* extent info */
56154c2258cSChao Yu 
56254c2258cSChao Yu 	};
56354c2258cSChao Yu 	struct list_head list;		/* node in global extent list of sbi */
564201ef5e0SHou Pengyang 	struct extent_tree *et;		/* extent tree pointer */
56513054c54SChao Yu };
56613054c54SChao Yu 
56713054c54SChao Yu struct extent_tree {
56813054c54SChao Yu 	nid_t ino;			/* inode number */
56913054c54SChao Yu 	struct rb_root root;		/* root of extent info rb-tree */
57062c8af65SChao Yu 	struct extent_node *cached_en;	/* recently accessed extent node */
5713e72f721SJaegeuk Kim 	struct extent_info largest;	/* largested extent info */
572137d09f0SJaegeuk Kim 	struct list_head list;		/* to be used by sbi->zombie_list */
57313054c54SChao Yu 	rwlock_t lock;			/* protect extent info rb-tree */
57468e35385SChao Yu 	atomic_t node_cnt;		/* # of extent node in rb-tree*/
575b430f726SZhikang Zhang 	bool largest_updated;		/* largest extent updated */
57613054c54SChao Yu };
57713054c54SChao Yu 
57839a53e0cSJaegeuk Kim /*
579003a3e1dSJaegeuk Kim  * This structure is taken from ext4_map_blocks.
580003a3e1dSJaegeuk Kim  *
581003a3e1dSJaegeuk Kim  * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
582003a3e1dSJaegeuk Kim  */
583003a3e1dSJaegeuk Kim #define F2FS_MAP_NEW		(1 << BH_New)
584003a3e1dSJaegeuk Kim #define F2FS_MAP_MAPPED		(1 << BH_Mapped)
5857f63eb77SJaegeuk Kim #define F2FS_MAP_UNWRITTEN	(1 << BH_Unwritten)
5867f63eb77SJaegeuk Kim #define F2FS_MAP_FLAGS		(F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
5877f63eb77SJaegeuk Kim 				F2FS_MAP_UNWRITTEN)
588003a3e1dSJaegeuk Kim 
589003a3e1dSJaegeuk Kim struct f2fs_map_blocks {
590003a3e1dSJaegeuk Kim 	block_t m_pblk;
591003a3e1dSJaegeuk Kim 	block_t m_lblk;
592003a3e1dSJaegeuk Kim 	unsigned int m_len;
593003a3e1dSJaegeuk Kim 	unsigned int m_flags;
594da85985cSChao Yu 	pgoff_t *m_next_pgofs;		/* point next possible non-hole pgofs */
595c4020b2dSChao Yu 	pgoff_t *m_next_extent;		/* point to next possible extent */
596d5097be5SHyunchul Lee 	int m_seg_type;
597003a3e1dSJaegeuk Kim };
598003a3e1dSJaegeuk Kim 
599e2b4e2bcSChao Yu /* for flag in get_data_block */
600f2220c7fSQiuyang Sun enum {
601f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_DEFAULT,
602f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_FIEMAP,
603f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_BMAP,
6040a4daae5SJaegeuk Kim 	F2FS_GET_BLOCK_DIO,
605f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_DIO,
606f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_AIO,
607c4020b2dSChao Yu 	F2FS_GET_BLOCK_PRECACHE,
608f2220c7fSQiuyang Sun };
609e2b4e2bcSChao Yu 
610003a3e1dSJaegeuk Kim /*
61139a53e0cSJaegeuk Kim  * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
61239a53e0cSJaegeuk Kim  */
61339a53e0cSJaegeuk Kim #define FADVISE_COLD_BIT	0x01
614354a3399SJaegeuk Kim #define FADVISE_LOST_PINO_BIT	0x02
615cde4de12SJaegeuk Kim #define FADVISE_ENCRYPT_BIT	0x04
616e7d55452SJaegeuk Kim #define FADVISE_ENC_NAME_BIT	0x08
61726787236SJaegeuk Kim #define FADVISE_KEEP_SIZE_BIT	0x10
618b6a06cbbSChao Yu #define FADVISE_HOT_BIT		0x20
61953fedcc0SEric Biggers #define FADVISE_VERITY_BIT	0x40	/* reserved */
62039a53e0cSJaegeuk Kim 
621797c1cb5SChao Yu #define FADVISE_MODIFIABLE_BITS	(FADVISE_COLD_BIT | FADVISE_HOT_BIT)
622797c1cb5SChao Yu 
623b5492af7SJaegeuk Kim #define file_is_cold(inode)	is_file(inode, FADVISE_COLD_BIT)
624b5492af7SJaegeuk Kim #define file_wrong_pino(inode)	is_file(inode, FADVISE_LOST_PINO_BIT)
625b5492af7SJaegeuk Kim #define file_set_cold(inode)	set_file(inode, FADVISE_COLD_BIT)
626b5492af7SJaegeuk Kim #define file_lost_pino(inode)	set_file(inode, FADVISE_LOST_PINO_BIT)
627b5492af7SJaegeuk Kim #define file_clear_cold(inode)	clear_file(inode, FADVISE_COLD_BIT)
628b5492af7SJaegeuk Kim #define file_got_pino(inode)	clear_file(inode, FADVISE_LOST_PINO_BIT)
629cde4de12SJaegeuk Kim #define file_is_encrypt(inode)	is_file(inode, FADVISE_ENCRYPT_BIT)
630cde4de12SJaegeuk Kim #define file_set_encrypt(inode)	set_file(inode, FADVISE_ENCRYPT_BIT)
631cde4de12SJaegeuk Kim #define file_clear_encrypt(inode) clear_file(inode, FADVISE_ENCRYPT_BIT)
632e7d55452SJaegeuk Kim #define file_enc_name(inode)	is_file(inode, FADVISE_ENC_NAME_BIT)
633e7d55452SJaegeuk Kim #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
63426787236SJaegeuk Kim #define file_keep_isize(inode)	is_file(inode, FADVISE_KEEP_SIZE_BIT)
63526787236SJaegeuk Kim #define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
636b6a06cbbSChao Yu #define file_is_hot(inode)	is_file(inode, FADVISE_HOT_BIT)
637b6a06cbbSChao Yu #define file_set_hot(inode)	set_file(inode, FADVISE_HOT_BIT)
638b6a06cbbSChao Yu #define file_clear_hot(inode)	clear_file(inode, FADVISE_HOT_BIT)
639cde4de12SJaegeuk Kim 
640ab9fa662SJaegeuk Kim #define DEF_DIR_LEVEL		0
641ab9fa662SJaegeuk Kim 
6422ef79ecbSChao Yu enum {
6432ef79ecbSChao Yu 	GC_FAILURE_PIN,
6442ef79ecbSChao Yu 	GC_FAILURE_ATOMIC,
6452ef79ecbSChao Yu 	MAX_GC_FAILURE
6462ef79ecbSChao Yu };
6472ef79ecbSChao Yu 
64839a53e0cSJaegeuk Kim struct f2fs_inode_info {
64939a53e0cSJaegeuk Kim 	struct inode vfs_inode;		/* serve a vfs inode */
65039a53e0cSJaegeuk Kim 	unsigned long i_flags;		/* keep an inode flags for ioctl */
65139a53e0cSJaegeuk Kim 	unsigned char i_advise;		/* use to give file attribute hints */
65238431545SJaegeuk Kim 	unsigned char i_dir_level;	/* use for dentry level for large dir */
6531ad71a27SJaegeuk Kim 	unsigned int i_current_depth;	/* only for directory depth */
6542ef79ecbSChao Yu 	/* for gc failure statistic */
6552ef79ecbSChao Yu 	unsigned int i_gc_failures[MAX_GC_FAILURE];
6566666e6aaSJaegeuk Kim 	unsigned int i_pino;		/* parent inode number */
65739a53e0cSJaegeuk Kim 	umode_t i_acl_mode;		/* keep file acl mode temporarily */
65839a53e0cSJaegeuk Kim 
65939a53e0cSJaegeuk Kim 	/* Use below internally in f2fs*/
66039a53e0cSJaegeuk Kim 	unsigned long flags;		/* use to pass per-file flags */
661d928bfbfSJaegeuk Kim 	struct rw_semaphore i_sem;	/* protect fi info */
662204706c7SJaegeuk Kim 	atomic_t dirty_pages;		/* # of dirty pages */
66339a53e0cSJaegeuk Kim 	f2fs_hash_t chash;		/* hash value of given file name */
66439a53e0cSJaegeuk Kim 	unsigned int clevel;		/* maximum level of given file name */
66588c5c13aSJaegeuk Kim 	struct task_struct *task;	/* lookup and create consistency */
666b0af6d49SChao Yu 	struct task_struct *cp_task;	/* separate cp/wb IO stats*/
66739a53e0cSJaegeuk Kim 	nid_t i_xattr_nid;		/* node id that contains xattrs */
66826de9b11SJaegeuk Kim 	loff_t	last_disk_size;		/* lastly written file size */
66988b88a66SJaegeuk Kim 
6700abd675eSChao Yu #ifdef CONFIG_QUOTA
6710abd675eSChao Yu 	struct dquot *i_dquot[MAXQUOTAS];
6720abd675eSChao Yu 
6730abd675eSChao Yu 	/* quota space reservation, managed internally by quota code */
6740abd675eSChao Yu 	qsize_t i_reserved_quota;
6750abd675eSChao Yu #endif
6760f18b462SJaegeuk Kim 	struct list_head dirty_list;	/* dirty list for dirs and files */
6770f18b462SJaegeuk Kim 	struct list_head gdirty_list;	/* linked in global dirty list */
67857864ae5SJaegeuk Kim 	struct list_head inmem_ilist;	/* list for inmem inodes */
67988b88a66SJaegeuk Kim 	struct list_head inmem_pages;	/* inmemory pages managed by f2fs */
6807a10f017SJaegeuk Kim 	struct task_struct *inmem_task;	/* store inmemory task */
68188b88a66SJaegeuk Kim 	struct mutex inmem_lock;	/* lock for inmemory pages */
6823e72f721SJaegeuk Kim 	struct extent_tree *extent_tree;	/* cached extent_tree entry */
683b2532c69SChao Yu 
684b2532c69SChao Yu 	/* avoid racing between foreground op and gc */
685b2532c69SChao Yu 	struct rw_semaphore i_gc_rwsem[2];
6865a3a2d83SQiuyang Sun 	struct rw_semaphore i_mmap_sem;
68727161f13SYunlei He 	struct rw_semaphore i_xattr_sem; /* avoid racing between reading and changing EAs */
688f2470371SChao Yu 
6897a2af766SChao Yu 	int i_extra_isize;		/* size of extra space located in i_addr */
6905c57132eSChao Yu 	kprojid_t i_projid;		/* id for project quota */
6916afc662eSChao Yu 	int i_inline_xattr_size;	/* inline xattr size */
69224b81dfcSArnd Bergmann 	struct timespec64 i_crtime;	/* inode creation time */
69324b81dfcSArnd Bergmann 	struct timespec64 i_disk_time[4];/* inode disk times */
69439a53e0cSJaegeuk Kim };
69539a53e0cSJaegeuk Kim 
69639a53e0cSJaegeuk Kim static inline void get_extent_info(struct extent_info *ext,
697bd933d4fSChao Yu 					struct f2fs_extent *i_ext)
69839a53e0cSJaegeuk Kim {
699bd933d4fSChao Yu 	ext->fofs = le32_to_cpu(i_ext->fofs);
700bd933d4fSChao Yu 	ext->blk = le32_to_cpu(i_ext->blk);
701bd933d4fSChao Yu 	ext->len = le32_to_cpu(i_ext->len);
70239a53e0cSJaegeuk Kim }
70339a53e0cSJaegeuk Kim 
70439a53e0cSJaegeuk Kim static inline void set_raw_extent(struct extent_info *ext,
70539a53e0cSJaegeuk Kim 					struct f2fs_extent *i_ext)
70639a53e0cSJaegeuk Kim {
70739a53e0cSJaegeuk Kim 	i_ext->fofs = cpu_to_le32(ext->fofs);
7084d0b0bd4SChao Yu 	i_ext->blk = cpu_to_le32(ext->blk);
70939a53e0cSJaegeuk Kim 	i_ext->len = cpu_to_le32(ext->len);
71039a53e0cSJaegeuk Kim }
71139a53e0cSJaegeuk Kim 
712429511cdSChao Yu static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
713429511cdSChao Yu 						u32 blk, unsigned int len)
714429511cdSChao Yu {
715429511cdSChao Yu 	ei->fofs = fofs;
716429511cdSChao Yu 	ei->blk = blk;
717429511cdSChao Yu 	ei->len = len;
718429511cdSChao Yu }
719429511cdSChao Yu 
720004b6862SChao Yu static inline bool __is_discard_mergeable(struct discard_info *back,
72135ec7d57SChao Yu 			struct discard_info *front, unsigned int max_len)
722004b6862SChao Yu {
7239a997188SJaegeuk Kim 	return (back->lstart + back->len == front->lstart) &&
72435ec7d57SChao Yu 		(back->len + front->len <= max_len);
725004b6862SChao Yu }
726004b6862SChao Yu 
727004b6862SChao Yu static inline bool __is_discard_back_mergeable(struct discard_info *cur,
72835ec7d57SChao Yu 			struct discard_info *back, unsigned int max_len)
729004b6862SChao Yu {
73035ec7d57SChao Yu 	return __is_discard_mergeable(back, cur, max_len);
731004b6862SChao Yu }
732004b6862SChao Yu 
733004b6862SChao Yu static inline bool __is_discard_front_mergeable(struct discard_info *cur,
73435ec7d57SChao Yu 			struct discard_info *front, unsigned int max_len)
735004b6862SChao Yu {
73635ec7d57SChao Yu 	return __is_discard_mergeable(cur, front, max_len);
737004b6862SChao Yu }
738004b6862SChao Yu 
739429511cdSChao Yu static inline bool __is_extent_mergeable(struct extent_info *back,
740429511cdSChao Yu 						struct extent_info *front)
741429511cdSChao Yu {
742429511cdSChao Yu 	return (back->fofs + back->len == front->fofs &&
743429511cdSChao Yu 			back->blk + back->len == front->blk);
744429511cdSChao Yu }
745429511cdSChao Yu 
746429511cdSChao Yu static inline bool __is_back_mergeable(struct extent_info *cur,
747429511cdSChao Yu 						struct extent_info *back)
748429511cdSChao Yu {
749429511cdSChao Yu 	return __is_extent_mergeable(back, cur);
750429511cdSChao Yu }
751429511cdSChao Yu 
752429511cdSChao Yu static inline bool __is_front_mergeable(struct extent_info *cur,
753429511cdSChao Yu 						struct extent_info *front)
754429511cdSChao Yu {
755429511cdSChao Yu 	return __is_extent_mergeable(cur, front);
756429511cdSChao Yu }
757429511cdSChao Yu 
758cac5a3d8SDongOh Shin extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
759b430f726SZhikang Zhang static inline void __try_update_largest_extent(struct extent_tree *et,
760b430f726SZhikang Zhang 						struct extent_node *en)
7614abd3f5aSChao Yu {
762205b9822SJaegeuk Kim 	if (en->ei.len > et->largest.len) {
7634abd3f5aSChao Yu 		et->largest = en->ei;
764b430f726SZhikang Zhang 		et->largest_updated = true;
765205b9822SJaegeuk Kim 	}
7664abd3f5aSChao Yu }
7674abd3f5aSChao Yu 
7689a4ffdf5SChao Yu /*
7699a4ffdf5SChao Yu  * For free nid management
7709a4ffdf5SChao Yu  */
7719a4ffdf5SChao Yu enum nid_state {
7729a4ffdf5SChao Yu 	FREE_NID,		/* newly added to free nid list */
7739a4ffdf5SChao Yu 	PREALLOC_NID,		/* it is preallocated */
7749a4ffdf5SChao Yu 	MAX_NID_STATE,
775b8559dc2SChao Yu };
776b8559dc2SChao Yu 
77739a53e0cSJaegeuk Kim struct f2fs_nm_info {
77839a53e0cSJaegeuk Kim 	block_t nat_blkaddr;		/* base disk address of NAT */
77939a53e0cSJaegeuk Kim 	nid_t max_nid;			/* maximum possible node ids */
78004d47e67SChao Yu 	nid_t available_nids;		/* # of available node ids */
78139a53e0cSJaegeuk Kim 	nid_t next_scan_nid;		/* the next nid to be scanned */
782cdfc41c1SJaegeuk Kim 	unsigned int ram_thresh;	/* control the memory footprint */
783ea1a29a0SChao Yu 	unsigned int ra_nid_pages;	/* # of nid pages to be readaheaded */
7842304cb0cSChao Yu 	unsigned int dirty_nats_ratio;	/* control dirty nats ratio threshold */
78539a53e0cSJaegeuk Kim 
78639a53e0cSJaegeuk Kim 	/* NAT cache management */
78739a53e0cSJaegeuk Kim 	struct radix_tree_root nat_root;/* root of the nat entry cache */
788309cc2b6SJaegeuk Kim 	struct radix_tree_root nat_set_root;/* root of the nat set cache */
789b873b798SJaegeuk Kim 	struct rw_semaphore nat_tree_lock;	/* protect nat_tree_lock */
79039a53e0cSJaegeuk Kim 	struct list_head nat_entries;	/* cached nat entry list (clean) */
79122969158SChao Yu 	spinlock_t nat_list_lock;	/* protect clean nat entry list */
792309cc2b6SJaegeuk Kim 	unsigned int nat_cnt;		/* the # of cached nat entries */
793aec71382SChao Yu 	unsigned int dirty_nat_cnt;	/* total num of nat entries in set */
79422ad0b6aSJaegeuk Kim 	unsigned int nat_blocks;	/* # of nat blocks */
79539a53e0cSJaegeuk Kim 
79639a53e0cSJaegeuk Kim 	/* free node ids management */
7978a7ed66aSJaegeuk Kim 	struct radix_tree_root free_nid_root;/* root of the free_nid cache */
7989a4ffdf5SChao Yu 	struct list_head free_nid_list;		/* list for free nids excluding preallocated nids */
7999a4ffdf5SChao Yu 	unsigned int nid_cnt[MAX_NID_STATE];	/* the number of free node id */
800b8559dc2SChao Yu 	spinlock_t nid_list_lock;	/* protect nid lists ops */
80139a53e0cSJaegeuk Kim 	struct mutex build_lock;	/* lock for build free nids */
802bb1105e4SJaegeuk Kim 	unsigned char **free_nid_bitmap;
8034ac91242SChao Yu 	unsigned char *nat_block_bitmap;
804586d1492SChao Yu 	unsigned short *free_nid_count;	/* free nid count of NAT block */
80539a53e0cSJaegeuk Kim 
80639a53e0cSJaegeuk Kim 	/* for checkpoint */
80739a53e0cSJaegeuk Kim 	char *nat_bitmap;		/* NAT bitmap pointer */
80822ad0b6aSJaegeuk Kim 
80922ad0b6aSJaegeuk Kim 	unsigned int nat_bits_blocks;	/* # of nat bits blocks */
81022ad0b6aSJaegeuk Kim 	unsigned char *nat_bits;	/* NAT bits blocks */
81122ad0b6aSJaegeuk Kim 	unsigned char *full_nat_bits;	/* full NAT pages */
81222ad0b6aSJaegeuk Kim 	unsigned char *empty_nat_bits;	/* empty NAT pages */
813599a09b2SChao Yu #ifdef CONFIG_F2FS_CHECK_FS
814599a09b2SChao Yu 	char *nat_bitmap_mir;		/* NAT bitmap mirror */
815599a09b2SChao Yu #endif
81639a53e0cSJaegeuk Kim 	int bitmap_size;		/* bitmap size */
81739a53e0cSJaegeuk Kim };
81839a53e0cSJaegeuk Kim 
81939a53e0cSJaegeuk Kim /*
82039a53e0cSJaegeuk Kim  * this structure is used as one of function parameters.
82139a53e0cSJaegeuk Kim  * all the information are dedicated to a given direct node block determined
82239a53e0cSJaegeuk Kim  * by the data offset in a file.
82339a53e0cSJaegeuk Kim  */
82439a53e0cSJaegeuk Kim struct dnode_of_data {
82539a53e0cSJaegeuk Kim 	struct inode *inode;		/* vfs inode pointer */
82639a53e0cSJaegeuk Kim 	struct page *inode_page;	/* its inode page, NULL is possible */
82739a53e0cSJaegeuk Kim 	struct page *node_page;		/* cached direct node page */
82839a53e0cSJaegeuk Kim 	nid_t nid;			/* node id of the direct node block */
82939a53e0cSJaegeuk Kim 	unsigned int ofs_in_node;	/* data offset in the node page */
83039a53e0cSJaegeuk Kim 	bool inode_page_locked;		/* inode page is locked or not */
83193bae099SJaegeuk Kim 	bool node_changed;		/* is node block changed */
8323cf45747SChao Yu 	char cur_level;			/* level of hole node page */
8333cf45747SChao Yu 	char max_level;			/* level of current page located */
83439a53e0cSJaegeuk Kim 	block_t	data_blkaddr;		/* block address of the node block */
83539a53e0cSJaegeuk Kim };
83639a53e0cSJaegeuk Kim 
83739a53e0cSJaegeuk Kim static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
83839a53e0cSJaegeuk Kim 		struct page *ipage, struct page *npage, nid_t nid)
83939a53e0cSJaegeuk Kim {
840d66d1f76SJaegeuk Kim 	memset(dn, 0, sizeof(*dn));
84139a53e0cSJaegeuk Kim 	dn->inode = inode;
84239a53e0cSJaegeuk Kim 	dn->inode_page = ipage;
84339a53e0cSJaegeuk Kim 	dn->node_page = npage;
84439a53e0cSJaegeuk Kim 	dn->nid = nid;
84539a53e0cSJaegeuk Kim }
84639a53e0cSJaegeuk Kim 
84739a53e0cSJaegeuk Kim /*
84839a53e0cSJaegeuk Kim  * For SIT manager
84939a53e0cSJaegeuk Kim  *
85039a53e0cSJaegeuk Kim  * By default, there are 6 active log areas across the whole main area.
85139a53e0cSJaegeuk Kim  * When considering hot and cold data separation to reduce cleaning overhead,
85239a53e0cSJaegeuk Kim  * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
85339a53e0cSJaegeuk Kim  * respectively.
85439a53e0cSJaegeuk Kim  * In the current design, you should not change the numbers intentionally.
85539a53e0cSJaegeuk Kim  * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
85639a53e0cSJaegeuk Kim  * logs individually according to the underlying devices. (default: 6)
85739a53e0cSJaegeuk Kim  * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
85839a53e0cSJaegeuk Kim  * data and 8 for node logs.
85939a53e0cSJaegeuk Kim  */
86039a53e0cSJaegeuk Kim #define	NR_CURSEG_DATA_TYPE	(3)
86139a53e0cSJaegeuk Kim #define NR_CURSEG_NODE_TYPE	(3)
86239a53e0cSJaegeuk Kim #define NR_CURSEG_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
86339a53e0cSJaegeuk Kim 
86439a53e0cSJaegeuk Kim enum {
86539a53e0cSJaegeuk Kim 	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
86639a53e0cSJaegeuk Kim 	CURSEG_WARM_DATA,	/* data blocks */
86739a53e0cSJaegeuk Kim 	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
86839a53e0cSJaegeuk Kim 	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
86939a53e0cSJaegeuk Kim 	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
87039a53e0cSJaegeuk Kim 	CURSEG_COLD_NODE,	/* indirect node blocks */
87138aa0889SJaegeuk Kim 	NO_CHECK_TYPE,
87239a53e0cSJaegeuk Kim };
87339a53e0cSJaegeuk Kim 
8746b4afdd7SJaegeuk Kim struct flush_cmd {
8756b4afdd7SJaegeuk Kim 	struct completion wait;
876721bd4d5SGu Zheng 	struct llist_node llnode;
87739d787beSChao Yu 	nid_t ino;
8786b4afdd7SJaegeuk Kim 	int ret;
8796b4afdd7SJaegeuk Kim };
8806b4afdd7SJaegeuk Kim 
881a688b9d9SGu Zheng struct flush_cmd_control {
882a688b9d9SGu Zheng 	struct task_struct *f2fs_issue_flush;	/* flush thread */
883a688b9d9SGu Zheng 	wait_queue_head_t flush_wait_queue;	/* waiting queue for wake-up */
8848b8dd65fSChao Yu 	atomic_t issued_flush;			/* # of issued flushes */
8858b8dd65fSChao Yu 	atomic_t issing_flush;			/* # of issing flushes */
886721bd4d5SGu Zheng 	struct llist_head issue_list;		/* list for command issue */
887721bd4d5SGu Zheng 	struct llist_node *dispatch_list;	/* list for command dispatch */
888a688b9d9SGu Zheng };
889a688b9d9SGu Zheng 
89039a53e0cSJaegeuk Kim struct f2fs_sm_info {
89139a53e0cSJaegeuk Kim 	struct sit_info *sit_info;		/* whole segment information */
89239a53e0cSJaegeuk Kim 	struct free_segmap_info *free_info;	/* free segment information */
89339a53e0cSJaegeuk Kim 	struct dirty_seglist_info *dirty_info;	/* dirty segment information */
89439a53e0cSJaegeuk Kim 	struct curseg_info *curseg_array;	/* active segment information */
89539a53e0cSJaegeuk Kim 
8962b60311dSChao Yu 	struct rw_semaphore curseg_lock;	/* for preventing curseg change */
8972b60311dSChao Yu 
89839a53e0cSJaegeuk Kim 	block_t seg0_blkaddr;		/* block address of 0'th segment */
89939a53e0cSJaegeuk Kim 	block_t main_blkaddr;		/* start block address of main area */
90039a53e0cSJaegeuk Kim 	block_t ssa_blkaddr;		/* start block address of SSA area */
90139a53e0cSJaegeuk Kim 
90239a53e0cSJaegeuk Kim 	unsigned int segment_count;	/* total # of segments */
90339a53e0cSJaegeuk Kim 	unsigned int main_segments;	/* # of segments in main area */
90439a53e0cSJaegeuk Kim 	unsigned int reserved_segments;	/* # of reserved segments */
90539a53e0cSJaegeuk Kim 	unsigned int ovp_segments;	/* # of overprovision segments */
90681eb8d6eSJaegeuk Kim 
90781eb8d6eSJaegeuk Kim 	/* a threshold to reclaim prefree segments */
90881eb8d6eSJaegeuk Kim 	unsigned int rec_prefree_segments;
9097fd9e544SJaegeuk Kim 
910bba681cbSJaegeuk Kim 	/* for batched trimming */
911bba681cbSJaegeuk Kim 	unsigned int trim_sections;		/* # of sections to trim */
912bba681cbSJaegeuk Kim 
913184a5cd2SChao Yu 	struct list_head sit_entry_set;	/* sit entry set list */
914184a5cd2SChao Yu 
915216fbd64SJaegeuk Kim 	unsigned int ipu_policy;	/* in-place-update policy */
916216fbd64SJaegeuk Kim 	unsigned int min_ipu_util;	/* in-place-update threshold */
917c1ce1b02SJaegeuk Kim 	unsigned int min_fsync_blocks;	/* threshold for fsync */
918853137ceSJaegeuk Kim 	unsigned int min_seq_blocks;	/* threshold for sequential blocks */
919ef095d19SJaegeuk Kim 	unsigned int min_hot_blocks;	/* threshold for hot block allocation */
920a2a12b67SChao Yu 	unsigned int min_ssr_sections;	/* threshold to trigger SSR allocation */
9216b4afdd7SJaegeuk Kim 
9226b4afdd7SJaegeuk Kim 	/* for flush command control */
923b01a9201SJaegeuk Kim 	struct flush_cmd_control *fcc_info;
924a688b9d9SGu Zheng 
9250b54fb84SJaegeuk Kim 	/* for discard command control */
9260b54fb84SJaegeuk Kim 	struct discard_cmd_control *dcc_info;
92739a53e0cSJaegeuk Kim };
92839a53e0cSJaegeuk Kim 
92939a53e0cSJaegeuk Kim /*
93039a53e0cSJaegeuk Kim  * For superblock
93139a53e0cSJaegeuk Kim  */
93239a53e0cSJaegeuk Kim /*
93339a53e0cSJaegeuk Kim  * COUNT_TYPE for monitoring
93439a53e0cSJaegeuk Kim  *
93539a53e0cSJaegeuk Kim  * f2fs monitors the number of several block types such as on-writeback,
93639a53e0cSJaegeuk Kim  * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
93739a53e0cSJaegeuk Kim  */
93836951b38SChao Yu #define WB_DATA_TYPE(p)	(__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
93939a53e0cSJaegeuk Kim enum count_type {
94039a53e0cSJaegeuk Kim 	F2FS_DIRTY_DENTS,
941c227f912SChao Yu 	F2FS_DIRTY_DATA,
9422c8a4a28SJaegeuk Kim 	F2FS_DIRTY_QDATA,
94339a53e0cSJaegeuk Kim 	F2FS_DIRTY_NODES,
94439a53e0cSJaegeuk Kim 	F2FS_DIRTY_META,
9458dcf2ff7SJaegeuk Kim 	F2FS_INMEM_PAGES,
9460f18b462SJaegeuk Kim 	F2FS_DIRTY_IMETA,
94736951b38SChao Yu 	F2FS_WB_CP_DATA,
94836951b38SChao Yu 	F2FS_WB_DATA,
94939a53e0cSJaegeuk Kim 	NR_COUNT_TYPE,
95039a53e0cSJaegeuk Kim };
95139a53e0cSJaegeuk Kim 
95239a53e0cSJaegeuk Kim /*
953e1c42045Sarter97  * The below are the page types of bios used in submit_bio().
95439a53e0cSJaegeuk Kim  * The available types are:
95539a53e0cSJaegeuk Kim  * DATA			User data pages. It operates as async mode.
95639a53e0cSJaegeuk Kim  * NODE			Node pages. It operates as async mode.
95739a53e0cSJaegeuk Kim  * META			FS metadata pages such as SIT, NAT, CP.
95839a53e0cSJaegeuk Kim  * NR_PAGE_TYPE		The number of page types.
95939a53e0cSJaegeuk Kim  * META_FLUSH		Make sure the previous pages are written
96039a53e0cSJaegeuk Kim  *			with waiting the bio's completion
96139a53e0cSJaegeuk Kim  * ...			Only can be used with META.
96239a53e0cSJaegeuk Kim  */
9637d5e5109SJaegeuk Kim #define PAGE_TYPE_OF_BIO(type)	((type) > META ? META : (type))
96439a53e0cSJaegeuk Kim enum page_type {
96539a53e0cSJaegeuk Kim 	DATA,
96639a53e0cSJaegeuk Kim 	NODE,
96739a53e0cSJaegeuk Kim 	META,
96839a53e0cSJaegeuk Kim 	NR_PAGE_TYPE,
96939a53e0cSJaegeuk Kim 	META_FLUSH,
9708ce67cb0SJaegeuk Kim 	INMEM,		/* the below types are used by tracepoints only. */
9718ce67cb0SJaegeuk Kim 	INMEM_DROP,
9728c242db9SJaegeuk Kim 	INMEM_INVALIDATE,
97328bc106bSChao Yu 	INMEM_REVOKE,
9748ce67cb0SJaegeuk Kim 	IPU,
9758ce67cb0SJaegeuk Kim 	OPU,
97639a53e0cSJaegeuk Kim };
97739a53e0cSJaegeuk Kim 
978a912b54dSJaegeuk Kim enum temp_type {
979a912b54dSJaegeuk Kim 	HOT = 0,	/* must be zero for meta bio */
980a912b54dSJaegeuk Kim 	WARM,
981a912b54dSJaegeuk Kim 	COLD,
982a912b54dSJaegeuk Kim 	NR_TEMP_TYPE,
983a912b54dSJaegeuk Kim };
984a912b54dSJaegeuk Kim 
985cc15620bSJaegeuk Kim enum need_lock_type {
986cc15620bSJaegeuk Kim 	LOCK_REQ = 0,
987cc15620bSJaegeuk Kim 	LOCK_DONE,
988cc15620bSJaegeuk Kim 	LOCK_RETRY,
989cc15620bSJaegeuk Kim };
990cc15620bSJaegeuk Kim 
991a5fd5050SChao Yu enum cp_reason_type {
992a5fd5050SChao Yu 	CP_NO_NEEDED,
993a5fd5050SChao Yu 	CP_NON_REGULAR,
994a5fd5050SChao Yu 	CP_HARDLINK,
995a5fd5050SChao Yu 	CP_SB_NEED_CP,
996a5fd5050SChao Yu 	CP_WRONG_PINO,
997a5fd5050SChao Yu 	CP_NO_SPC_ROLL,
998a5fd5050SChao Yu 	CP_NODE_NEED_CP,
999a5fd5050SChao Yu 	CP_FASTBOOT_MODE,
1000a5fd5050SChao Yu 	CP_SPEC_LOG_NUM,
10010a007b97SJaegeuk Kim 	CP_RECOVER_DIR,
1002a5fd5050SChao Yu };
1003a5fd5050SChao Yu 
1004b0af6d49SChao Yu enum iostat_type {
1005b0af6d49SChao Yu 	APP_DIRECT_IO,			/* app direct IOs */
1006b0af6d49SChao Yu 	APP_BUFFERED_IO,		/* app buffered IOs */
1007b0af6d49SChao Yu 	APP_WRITE_IO,			/* app write IOs */
1008b0af6d49SChao Yu 	APP_MAPPED_IO,			/* app mapped IOs */
1009b0af6d49SChao Yu 	FS_DATA_IO,			/* data IOs from kworker/fsync/reclaimer */
1010b0af6d49SChao Yu 	FS_NODE_IO,			/* node IOs from kworker/fsync/reclaimer */
1011b0af6d49SChao Yu 	FS_META_IO,			/* meta IOs from kworker/reclaimer */
1012b0af6d49SChao Yu 	FS_GC_DATA_IO,			/* data IOs from forground gc */
1013b0af6d49SChao Yu 	FS_GC_NODE_IO,			/* node IOs from forground gc */
1014b0af6d49SChao Yu 	FS_CP_DATA_IO,			/* data IOs from checkpoint */
1015b0af6d49SChao Yu 	FS_CP_NODE_IO,			/* node IOs from checkpoint */
1016b0af6d49SChao Yu 	FS_CP_META_IO,			/* meta IOs from checkpoint */
1017b0af6d49SChao Yu 	FS_DISCARD,			/* discard */
1018b0af6d49SChao Yu 	NR_IO_TYPE,
1019b0af6d49SChao Yu };
1020b0af6d49SChao Yu 
1021458e6197SJaegeuk Kim struct f2fs_io_info {
102205ca3632SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs_sb_info pointer */
102339d787beSChao Yu 	nid_t ino;		/* inode number */
1024458e6197SJaegeuk Kim 	enum page_type type;	/* contains DATA/NODE/META/META_FLUSH */
1025a912b54dSJaegeuk Kim 	enum temp_type temp;	/* contains HOT/WARM/COLD */
102604d328deSMike Christie 	int op;			/* contains REQ_OP_ */
1027ef295ecfSChristoph Hellwig 	int op_flags;		/* req_flag_bits */
10287a9d7548SChao Yu 	block_t new_blkaddr;	/* new block address to be written */
102928bc106bSChao Yu 	block_t old_blkaddr;	/* old block address before Cow */
103005ca3632SJaegeuk Kim 	struct page *page;	/* page to be written */
10314375a336SJaegeuk Kim 	struct page *encrypted_page;	/* encrypted page */
1032fb830fc5SChao Yu 	struct list_head list;		/* serialize IOs */
1033d68f735bSJaegeuk Kim 	bool submitted;		/* indicate IO submission */
1034cc15620bSJaegeuk Kim 	int need_lock;		/* indicate we need to lock cp_rwsem */
1035fb830fc5SChao Yu 	bool in_list;		/* indicate fio is in io_list */
10360833721eSYunlei He 	bool is_meta;		/* indicate borrow meta inode mapping or not */
1037fe16efe6SChao Yu 	bool retry;		/* need to reallocate block address */
1038b0af6d49SChao Yu 	enum iostat_type io_type;	/* io type */
1039578c6478SYufen Yu 	struct writeback_control *io_wbc; /* writeback control */
10407735730dSChao Yu 	unsigned char version;		/* version of the node */
1041458e6197SJaegeuk Kim };
1042458e6197SJaegeuk Kim 
104368afcf2dSTomohiro Kusumi #define is_read_io(rw) ((rw) == READ)
10441ff7bd3bSJaegeuk Kim struct f2fs_bio_info {
1045458e6197SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs superblock */
10461ff7bd3bSJaegeuk Kim 	struct bio *bio;		/* bios to merge */
10471ff7bd3bSJaegeuk Kim 	sector_t last_block_in_bio;	/* last block number */
1048458e6197SJaegeuk Kim 	struct f2fs_io_info fio;	/* store buffered io info. */
1049df0f8dc0SChao Yu 	struct rw_semaphore io_rwsem;	/* blocking op for bio */
1050fb830fc5SChao Yu 	spinlock_t io_lock;		/* serialize DATA/NODE IOs */
1051fb830fc5SChao Yu 	struct list_head io_list;	/* track fios */
10521ff7bd3bSJaegeuk Kim };
10531ff7bd3bSJaegeuk Kim 
10543c62be17SJaegeuk Kim #define FDEV(i)				(sbi->devs[i])
10553c62be17SJaegeuk Kim #define RDEV(i)				(raw_super->devs[i])
10563c62be17SJaegeuk Kim struct f2fs_dev_info {
10573c62be17SJaegeuk Kim 	struct block_device *bdev;
10583c62be17SJaegeuk Kim 	char path[MAX_PATH_LEN];
10593c62be17SJaegeuk Kim 	unsigned int total_segments;
10603c62be17SJaegeuk Kim 	block_t start_blk;
10613c62be17SJaegeuk Kim 	block_t end_blk;
10623c62be17SJaegeuk Kim #ifdef CONFIG_BLK_DEV_ZONED
10633c62be17SJaegeuk Kim 	unsigned int nr_blkz;			/* Total number of zones */
10643c62be17SJaegeuk Kim 	u8 *blkz_type;				/* Array of zones type */
10653c62be17SJaegeuk Kim #endif
10663c62be17SJaegeuk Kim };
10673c62be17SJaegeuk Kim 
1068c227f912SChao Yu enum inode_type {
1069c227f912SChao Yu 	DIR_INODE,			/* for dirty dir inode */
1070c227f912SChao Yu 	FILE_INODE,			/* for dirty regular/symlink inode */
10710f18b462SJaegeuk Kim 	DIRTY_META,			/* for all dirtied inode metadata */
107257864ae5SJaegeuk Kim 	ATOMIC_FILE,			/* for all atomic files */
1073c227f912SChao Yu 	NR_INODE_TYPE,
1074c227f912SChao Yu };
1075c227f912SChao Yu 
107667298804SChao Yu /* for inner inode cache management */
107767298804SChao Yu struct inode_management {
107867298804SChao Yu 	struct radix_tree_root ino_root;	/* ino entry array */
107967298804SChao Yu 	spinlock_t ino_lock;			/* for ino entry lock */
108067298804SChao Yu 	struct list_head ino_list;		/* inode list head */
108167298804SChao Yu 	unsigned long ino_num;			/* number of entries */
108267298804SChao Yu };
108367298804SChao Yu 
1084caf0047eSChao Yu /* For s_flag in struct f2fs_sb_info */
1085caf0047eSChao Yu enum {
1086caf0047eSChao Yu 	SBI_IS_DIRTY,				/* dirty flag for checkpoint */
1087caf0047eSChao Yu 	SBI_IS_CLOSE,				/* specify unmounting */
1088caf0047eSChao Yu 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
1089caf0047eSChao Yu 	SBI_POR_DOING,				/* recovery is doing or not */
1090df728b0fSJaegeuk Kim 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
1091bbf156f7SJaegeuk Kim 	SBI_NEED_CP,				/* need to checkpoint */
109283a3bfdbSJaegeuk Kim 	SBI_IS_SHUTDOWN,			/* shutdown by ioctl */
10931378752bSChao Yu 	SBI_IS_RECOVERED,			/* recovered orphan/data */
1094caf0047eSChao Yu };
1095caf0047eSChao Yu 
10966beceb54SJaegeuk Kim enum {
10976beceb54SJaegeuk Kim 	CP_TIME,
1098d0239e1bSJaegeuk Kim 	REQ_TIME,
1099a7d10cf3SSahitya Tummala 	DISCARD_TIME,
1100a7d10cf3SSahitya Tummala 	GC_TIME,
11016beceb54SJaegeuk Kim 	MAX_TIME,
11026beceb54SJaegeuk Kim };
11036beceb54SJaegeuk Kim 
11040cdd3195SHyunchul Lee enum {
11055b0e9539SJaegeuk Kim 	GC_NORMAL,
11065b0e9539SJaegeuk Kim 	GC_IDLE_CB,
11075b0e9539SJaegeuk Kim 	GC_IDLE_GREEDY,
11085b0e9539SJaegeuk Kim 	GC_URGENT,
11095b0e9539SJaegeuk Kim };
11105b0e9539SJaegeuk Kim 
11115b0e9539SJaegeuk Kim enum {
11120cdd3195SHyunchul Lee 	WHINT_MODE_OFF,		/* not pass down write hints */
11130cdd3195SHyunchul Lee 	WHINT_MODE_USER,	/* try to pass down hints given by users */
1114f2e703f9SHyunchul Lee 	WHINT_MODE_FS,		/* pass down hints with F2FS policy */
11150cdd3195SHyunchul Lee };
11160cdd3195SHyunchul Lee 
111707939627SJaegeuk Kim enum {
111807939627SJaegeuk Kim 	ALLOC_MODE_DEFAULT,	/* stay default */
111907939627SJaegeuk Kim 	ALLOC_MODE_REUSE,	/* reuse segments as much as possible */
112007939627SJaegeuk Kim };
112107939627SJaegeuk Kim 
112293cf93f1SJunling Zheng enum fsync_mode {
112393cf93f1SJunling Zheng 	FSYNC_MODE_POSIX,	/* fsync follows posix semantics */
112493cf93f1SJunling Zheng 	FSYNC_MODE_STRICT,	/* fsync behaves in line with ext4 */
1125d6290814SJaegeuk Kim 	FSYNC_MODE_NOBARRIER,	/* fsync behaves nobarrier based on posix */
112693cf93f1SJunling Zheng };
112793cf93f1SJunling Zheng 
1128ff62af20SSheng Yong #ifdef CONFIG_F2FS_FS_ENCRYPTION
1129ff62af20SSheng Yong #define DUMMY_ENCRYPTION_ENABLED(sbi) \
1130ff62af20SSheng Yong 			(unlikely(F2FS_OPTION(sbi).test_dummy_encryption))
1131ff62af20SSheng Yong #else
1132ff62af20SSheng Yong #define DUMMY_ENCRYPTION_ENABLED(sbi) (0)
1133ff62af20SSheng Yong #endif
1134ff62af20SSheng Yong 
113539a53e0cSJaegeuk Kim struct f2fs_sb_info {
113639a53e0cSJaegeuk Kim 	struct super_block *sb;			/* pointer to VFS super block */
11375e176d54SJaegeuk Kim 	struct proc_dir_entry *s_proc;		/* proc entry */
113839a53e0cSJaegeuk Kim 	struct f2fs_super_block *raw_super;	/* raw super block pointer */
1139846ae671SChao Yu 	struct rw_semaphore sb_lock;		/* lock for raw super block */
1140e8240f65SChao Yu 	int valid_super_block;			/* valid super block no */
1141fadb2fb8SChao Yu 	unsigned long s_flag;				/* flags for sbi */
1142853137ceSJaegeuk Kim 	struct mutex writepages;		/* mutex for writepages() */
114339a53e0cSJaegeuk Kim 
1144178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
1145178053e2SDamien Le Moal 	unsigned int blocks_per_blkz;		/* F2FS blocks per zone */
1146178053e2SDamien Le Moal 	unsigned int log_blocks_per_blkz;	/* log2 F2FS blocks per zone */
1147178053e2SDamien Le Moal #endif
1148178053e2SDamien Le Moal 
114939a53e0cSJaegeuk Kim 	/* for node-related operations */
115039a53e0cSJaegeuk Kim 	struct f2fs_nm_info *nm_info;		/* node manager */
115139a53e0cSJaegeuk Kim 	struct inode *node_inode;		/* cache node blocks */
115239a53e0cSJaegeuk Kim 
115339a53e0cSJaegeuk Kim 	/* for segment-related operations */
115439a53e0cSJaegeuk Kim 	struct f2fs_sm_info *sm_info;		/* segment manager */
11551ff7bd3bSJaegeuk Kim 
11561ff7bd3bSJaegeuk Kim 	/* for bio operations */
1157a912b54dSJaegeuk Kim 	struct f2fs_bio_info *write_io[NR_PAGE_TYPE];	/* for write bios */
1158e41e6d75SChao Yu 	struct mutex wio_mutex[NR_PAGE_TYPE - 1][NR_TEMP_TYPE];
1159e41e6d75SChao Yu 						/* bio ordering for NODE/DATA */
1160107a805dSChao Yu 	/* keep migration IO order for LFS mode */
1161107a805dSChao Yu 	struct rw_semaphore io_order_lock;
11620a595ebaSJaegeuk Kim 	mempool_t *write_io_dummy;		/* Dummy pages */
116339a53e0cSJaegeuk Kim 
116439a53e0cSJaegeuk Kim 	/* for checkpoint */
116539a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt;		/* raw checkpoint pointer */
11668508e44aSJaegeuk Kim 	int cur_cp_pack;			/* remain current cp pack */
1167aaec2b1dSChao Yu 	spinlock_t cp_lock;			/* for flag in ckpt */
116839a53e0cSJaegeuk Kim 	struct inode *meta_inode;		/* cache meta blocks */
116939936837SJaegeuk Kim 	struct mutex cp_mutex;			/* checkpoint procedure lock */
1170b873b798SJaegeuk Kim 	struct rw_semaphore cp_rwsem;		/* blocking FS operations */
1171b3582c68SChao Yu 	struct rw_semaphore node_write;		/* locking node writes */
117259c9081bSYunlei He 	struct rw_semaphore node_change;	/* locking node change */
1173fb51b5efSChangman Lee 	wait_queue_head_t cp_wait;
11746beceb54SJaegeuk Kim 	unsigned long last_time[MAX_TIME];	/* to store time in jiffies */
11756beceb54SJaegeuk Kim 	long interval_time[MAX_TIME];		/* to store thresholds */
117639a53e0cSJaegeuk Kim 
117767298804SChao Yu 	struct inode_management im[MAX_INO_ENTRY];      /* manage inode cache */
11786451e041SJaegeuk Kim 
117950fa53ecSChao Yu 	spinlock_t fsync_node_lock;		/* for node entry lock */
118050fa53ecSChao Yu 	struct list_head fsync_node_list;	/* node list head */
118150fa53ecSChao Yu 	unsigned int fsync_seg_id;		/* sequence id */
118250fa53ecSChao Yu 	unsigned int fsync_node_num;		/* number of node entries */
118350fa53ecSChao Yu 
11846451e041SJaegeuk Kim 	/* for orphan inode, use 0'th array */
11850d47c1adSGu Zheng 	unsigned int max_orphans;		/* max orphan inodes */
118639a53e0cSJaegeuk Kim 
1187c227f912SChao Yu 	/* for inode management */
1188c227f912SChao Yu 	struct list_head inode_list[NR_INODE_TYPE];	/* dirty inode list */
1189c227f912SChao Yu 	spinlock_t inode_lock[NR_INODE_TYPE];	/* for dirty inode list lock */
119039a53e0cSJaegeuk Kim 
119113054c54SChao Yu 	/* for extent tree cache */
119213054c54SChao Yu 	struct radix_tree_root extent_tree_root;/* cache extent cache entries */
11935e8256acSYunlei He 	struct mutex extent_tree_lock;	/* locking extent radix tree */
119413054c54SChao Yu 	struct list_head extent_list;		/* lru list for shrinker */
119513054c54SChao Yu 	spinlock_t extent_lock;			/* locking extent lru list */
11967441ccefSJaegeuk Kim 	atomic_t total_ext_tree;		/* extent tree count */
1197137d09f0SJaegeuk Kim 	struct list_head zombie_list;		/* extent zombie tree list */
119874fd8d99SJaegeuk Kim 	atomic_t total_zombie_tree;		/* extent zombie tree count */
119913054c54SChao Yu 	atomic_t total_ext_node;		/* extent info count */
120013054c54SChao Yu 
120139a53e0cSJaegeuk Kim 	/* basic filesystem units */
120239a53e0cSJaegeuk Kim 	unsigned int log_sectors_per_block;	/* log2 sectors per block */
120339a53e0cSJaegeuk Kim 	unsigned int log_blocksize;		/* log2 block size */
120439a53e0cSJaegeuk Kim 	unsigned int blocksize;			/* block size */
120539a53e0cSJaegeuk Kim 	unsigned int root_ino_num;		/* root inode number*/
120639a53e0cSJaegeuk Kim 	unsigned int node_ino_num;		/* node inode number*/
120739a53e0cSJaegeuk Kim 	unsigned int meta_ino_num;		/* meta inode number*/
120839a53e0cSJaegeuk Kim 	unsigned int log_blocks_per_seg;	/* log2 blocks per segment */
120939a53e0cSJaegeuk Kim 	unsigned int blocks_per_seg;		/* blocks per segment */
121039a53e0cSJaegeuk Kim 	unsigned int segs_per_sec;		/* segments per section */
121139a53e0cSJaegeuk Kim 	unsigned int secs_per_zone;		/* sections per zone */
121239a53e0cSJaegeuk Kim 	unsigned int total_sections;		/* total section count */
121339a53e0cSJaegeuk Kim 	unsigned int total_node_count;		/* total node block count */
121439a53e0cSJaegeuk Kim 	unsigned int total_valid_node_count;	/* valid node block count */
1215e0afc4d6SChao Yu 	loff_t max_file_blocks;			/* max block index of file */
1216ab9fa662SJaegeuk Kim 	int dir_level;				/* directory level */
1217a2a12b67SChao Yu 	unsigned int trigger_ssr_threshold;	/* threshold to trigger ssr */
1218f6df8f23SSheng Yong 	int readdir_ra;				/* readahead inode in readdir */
121939a53e0cSJaegeuk Kim 
122039a53e0cSJaegeuk Kim 	block_t user_block_count;		/* # of user blocks */
122139a53e0cSJaegeuk Kim 	block_t total_valid_block_count;	/* # of valid blocks */
1222a66cdd98SJaegeuk Kim 	block_t discard_blks;			/* discard command candidats */
122339a53e0cSJaegeuk Kim 	block_t last_valid_block_count;		/* for recovery */
1224daeb433eSChao Yu 	block_t reserved_blocks;		/* configurable reserved blocks */
122580d42145SYunlong Song 	block_t current_reserved_blocks;	/* current reserved blocks */
1226daeb433eSChao Yu 
1227292c196aSChao Yu 	unsigned int nquota_files;		/* # of quota sysfile */
1228292c196aSChao Yu 
122939a53e0cSJaegeuk Kim 	u32 s_next_generation;			/* for NFS support */
1230523be8a6SJaegeuk Kim 
1231523be8a6SJaegeuk Kim 	/* # of pages, see count_type */
123235782b23SJaegeuk Kim 	atomic_t nr_pages[NR_COUNT_TYPE];
123341382ec4SJaegeuk Kim 	/* # of allocated blocks */
123441382ec4SJaegeuk Kim 	struct percpu_counter alloc_valid_block_count;
123539a53e0cSJaegeuk Kim 
1236687de7f1SJaegeuk Kim 	/* writeback control */
1237c29fd0c0SChao Yu 	atomic_t wb_sync_req[META];	/* count # of WB_SYNC threads */
1238687de7f1SJaegeuk Kim 
1239513c5f37SJaegeuk Kim 	/* valid inode count */
1240513c5f37SJaegeuk Kim 	struct percpu_counter total_valid_inode_count;
1241513c5f37SJaegeuk Kim 
124239a53e0cSJaegeuk Kim 	struct f2fs_mount_info mount_opt;	/* mount options */
124339a53e0cSJaegeuk Kim 
124439a53e0cSJaegeuk Kim 	/* for cleaning operations */
124539a53e0cSJaegeuk Kim 	struct mutex gc_mutex;			/* mutex for GC */
124639a53e0cSJaegeuk Kim 	struct f2fs_gc_kthread	*gc_thread;	/* GC thread */
12475ec4e49fSJaegeuk Kim 	unsigned int cur_victim_sec;		/* current victim section num */
12485b0e9539SJaegeuk Kim 	unsigned int gc_mode;			/* current GC state */
12492ef79ecbSChao Yu 	/* for skip statistic */
12502ef79ecbSChao Yu 	unsigned long long skipped_atomic_files[2];	/* FG_GC and BG_GC */
12516f8d4455SJaegeuk Kim 	unsigned long long skipped_gc_rwsem;		/* FG_GC only */
125239a53e0cSJaegeuk Kim 
12531ad71a27SJaegeuk Kim 	/* threshold for gc trials on pinned files */
12541ad71a27SJaegeuk Kim 	u64 gc_pin_file_threshold;
12551ad71a27SJaegeuk Kim 
1256b1c57c1cSJaegeuk Kim 	/* maximum # of trials to find a victim segment for SSR and GC */
1257b1c57c1cSJaegeuk Kim 	unsigned int max_victim_search;
1258b1c57c1cSJaegeuk Kim 
125939a53e0cSJaegeuk Kim 	/*
126039a53e0cSJaegeuk Kim 	 * for stat information.
126139a53e0cSJaegeuk Kim 	 * one is for the LFS mode, and the other is for the SSR mode.
126239a53e0cSJaegeuk Kim 	 */
126335b09d82SNamjae Jeon #ifdef CONFIG_F2FS_STAT_FS
126439a53e0cSJaegeuk Kim 	struct f2fs_stat_info *stat_info;	/* FS status information */
1265b63e7be5SChao Yu 	atomic_t meta_count[META_MAX];		/* # of meta blocks */
126639a53e0cSJaegeuk Kim 	unsigned int segment_count[2];		/* # of allocated segments */
126739a53e0cSJaegeuk Kim 	unsigned int block_count[2];		/* # of allocated blocks */
1268b9a2c252SChangman Lee 	atomic_t inplace_count;		/* # of inplace update */
12695b7ee374SChao Yu 	atomic64_t total_hit_ext;		/* # of lookup extent cache */
12705b7ee374SChao Yu 	atomic64_t read_hit_rbtree;		/* # of hit rbtree extent node */
12715b7ee374SChao Yu 	atomic64_t read_hit_largest;		/* # of hit largest extent node */
12725b7ee374SChao Yu 	atomic64_t read_hit_cached;		/* # of hit cached extent node */
1273d5e8f6c9SChao Yu 	atomic_t inline_xattr;			/* # of inline_xattr inodes */
127403e14d52SChao Yu 	atomic_t inline_inode;			/* # of inline_data inodes */
127503e14d52SChao Yu 	atomic_t inline_dir;			/* # of inline_dentry inodes */
127626a28a0cSJaegeuk Kim 	atomic_t aw_cnt;			/* # of atomic writes */
1277648d50baSChao Yu 	atomic_t vw_cnt;			/* # of volatile writes */
127826a28a0cSJaegeuk Kim 	atomic_t max_aw_cnt;			/* max # of atomic writes */
1279648d50baSChao Yu 	atomic_t max_vw_cnt;			/* max # of volatile writes */
128039a53e0cSJaegeuk Kim 	int bg_gc;				/* background gc calls */
1281274bd9baSChao Yu 	unsigned int io_skip_bggc;		/* skip background gc for in-flight IO */
1282274bd9baSChao Yu 	unsigned int other_skip_bggc;		/* skip background gc for other reasons */
128333fbd510SChao Yu 	unsigned int ndirty_inode[NR_INODE_TYPE];	/* # of dirty inodes */
128435b09d82SNamjae Jeon #endif
128539a53e0cSJaegeuk Kim 	spinlock_t stat_lock;			/* lock for stat operations */
1286b59d0baeSNamjae Jeon 
1287b0af6d49SChao Yu 	/* For app/fs IO statistics */
1288b0af6d49SChao Yu 	spinlock_t iostat_lock;
1289b0af6d49SChao Yu 	unsigned long long write_iostat[NR_IO_TYPE];
1290b0af6d49SChao Yu 	bool iostat_enable;
1291b0af6d49SChao Yu 
1292b59d0baeSNamjae Jeon 	/* For sysfs suppport */
1293b59d0baeSNamjae Jeon 	struct kobject s_kobj;
1294b59d0baeSNamjae Jeon 	struct completion s_kobj_unregister;
12952658e50dSJaegeuk Kim 
12962658e50dSJaegeuk Kim 	/* For shrinker support */
12972658e50dSJaegeuk Kim 	struct list_head s_list;
12983c62be17SJaegeuk Kim 	int s_ndevs;				/* number of devices */
12993c62be17SJaegeuk Kim 	struct f2fs_dev_info *devs;		/* for device list */
13001228b482SChao Yu 	unsigned int dirty_device;		/* for checkpoint data flush */
13011228b482SChao Yu 	spinlock_t dev_lock;			/* protect dirty_device */
13022658e50dSJaegeuk Kim 	struct mutex umount_mutex;
13032658e50dSJaegeuk Kim 	unsigned int shrinker_run_no;
13048f1dbbbbSShuoran Liu 
13058f1dbbbbSShuoran Liu 	/* For write statistics */
13068f1dbbbbSShuoran Liu 	u64 sectors_written_start;
13078f1dbbbbSShuoran Liu 	u64 kbytes_written;
130843b6573bSKeith Mok 
130943b6573bSKeith Mok 	/* Reference to checksum algorithm driver via cryptoapi */
131043b6573bSKeith Mok 	struct crypto_shash *s_chksum_driver;
13111ecc0c5cSChao Yu 
1312704956ecSChao Yu 	/* Precomputed FS UUID checksum for seeding other checksums */
1313704956ecSChao Yu 	__u32 s_chksum_seed;
131439a53e0cSJaegeuk Kim };
131539a53e0cSJaegeuk Kim 
13161ecc0c5cSChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
131755523519SChao Yu #define f2fs_show_injection_info(type)					\
131822d7ea13SChao Yu 	printk_ratelimited("%sF2FS-fs : inject %s in %s of %pF\n",	\
13192d3a5856SGao Xiang 		KERN_INFO, f2fs_fault_name[type],			\
132055523519SChao Yu 		__func__, __builtin_return_address(0))
13211ecc0c5cSChao Yu static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
13221ecc0c5cSChao Yu {
132363189b78SChao Yu 	struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
13241ecc0c5cSChao Yu 
13251ecc0c5cSChao Yu 	if (!ffi->inject_rate)
13261ecc0c5cSChao Yu 		return false;
13271ecc0c5cSChao Yu 
13281ecc0c5cSChao Yu 	if (!IS_FAULT_SET(ffi, type))
13291ecc0c5cSChao Yu 		return false;
13301ecc0c5cSChao Yu 
13311ecc0c5cSChao Yu 	atomic_inc(&ffi->inject_ops);
13321ecc0c5cSChao Yu 	if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
13331ecc0c5cSChao Yu 		atomic_set(&ffi->inject_ops, 0);
13341ecc0c5cSChao Yu 		return true;
13351ecc0c5cSChao Yu 	}
13361ecc0c5cSChao Yu 	return false;
13371ecc0c5cSChao Yu }
13387fa750a1SArnd Bergmann #else
13397fa750a1SArnd Bergmann #define f2fs_show_injection_info(type) do { } while (0)
13407fa750a1SArnd Bergmann static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
13417fa750a1SArnd Bergmann {
13427fa750a1SArnd Bergmann 	return false;
13437fa750a1SArnd Bergmann }
13441ecc0c5cSChao Yu #endif
13451ecc0c5cSChao Yu 
13468f1dbbbbSShuoran Liu /* For write statistics. Suppose sector size is 512 bytes,
13478f1dbbbbSShuoran Liu  * and the return value is in kbytes. s is of struct f2fs_sb_info.
13488f1dbbbbSShuoran Liu  */
13498f1dbbbbSShuoran Liu #define BD_PART_WRITTEN(s)						 \
1350dbae2c55SMichael Callahan (((u64)part_stat_read((s)->sb->s_bdev->bd_part, sectors[STAT_WRITE]) -   \
135168afcf2dSTomohiro Kusumi 		(s)->sectors_written_start) >> 1)
13528f1dbbbbSShuoran Liu 
13536beceb54SJaegeuk Kim static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
13546beceb54SJaegeuk Kim {
1355a7d10cf3SSahitya Tummala 	unsigned long now = jiffies;
1356a7d10cf3SSahitya Tummala 
1357a7d10cf3SSahitya Tummala 	sbi->last_time[type] = now;
1358a7d10cf3SSahitya Tummala 
1359a7d10cf3SSahitya Tummala 	/* DISCARD_TIME and GC_TIME are based on REQ_TIME */
1360a7d10cf3SSahitya Tummala 	if (type == REQ_TIME) {
1361a7d10cf3SSahitya Tummala 		sbi->last_time[DISCARD_TIME] = now;
1362a7d10cf3SSahitya Tummala 		sbi->last_time[GC_TIME] = now;
1363a7d10cf3SSahitya Tummala 	}
13646beceb54SJaegeuk Kim }
13656beceb54SJaegeuk Kim 
13666beceb54SJaegeuk Kim static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
13676beceb54SJaegeuk Kim {
13686bccfa19SArnd Bergmann 	unsigned long interval = sbi->interval_time[type] * HZ;
13696beceb54SJaegeuk Kim 
13706beceb54SJaegeuk Kim 	return time_after(jiffies, sbi->last_time[type] + interval);
13716beceb54SJaegeuk Kim }
13726beceb54SJaegeuk Kim 
1373a7d10cf3SSahitya Tummala static inline unsigned int f2fs_time_to_wait(struct f2fs_sb_info *sbi,
1374a7d10cf3SSahitya Tummala 						int type)
1375a7d10cf3SSahitya Tummala {
1376a7d10cf3SSahitya Tummala 	unsigned long interval = sbi->interval_time[type] * HZ;
1377a7d10cf3SSahitya Tummala 	unsigned int wait_ms = 0;
1378a7d10cf3SSahitya Tummala 	long delta;
1379a7d10cf3SSahitya Tummala 
1380a7d10cf3SSahitya Tummala 	delta = (sbi->last_time[type] + interval) - jiffies;
1381a7d10cf3SSahitya Tummala 	if (delta > 0)
1382a7d10cf3SSahitya Tummala 		wait_ms = jiffies_to_msecs(delta);
1383a7d10cf3SSahitya Tummala 
1384a7d10cf3SSahitya Tummala 	return wait_ms;
1385a7d10cf3SSahitya Tummala }
1386a7d10cf3SSahitya Tummala 
1387a7d10cf3SSahitya Tummala static inline bool is_idle(struct f2fs_sb_info *sbi, int type)
1388d0239e1bSJaegeuk Kim {
1389d0239e1bSJaegeuk Kim 	struct block_device *bdev = sbi->sb->s_bdev;
1390d0239e1bSJaegeuk Kim 	struct request_queue *q = bdev_get_queue(bdev);
1391d0239e1bSJaegeuk Kim 	struct request_list *rl = &q->root_rl;
1392d0239e1bSJaegeuk Kim 
1393d0239e1bSJaegeuk Kim 	if (rl->count[BLK_RW_SYNC] || rl->count[BLK_RW_ASYNC])
13946e45f2a5SGustavo A. R. Silva 		return false;
1395d0239e1bSJaegeuk Kim 
1396a7d10cf3SSahitya Tummala 	return f2fs_time_over(sbi, type);
1397d0239e1bSJaegeuk Kim }
1398d0239e1bSJaegeuk Kim 
139939a53e0cSJaegeuk Kim /*
140039a53e0cSJaegeuk Kim  * Inline functions
140139a53e0cSJaegeuk Kim  */
1402416d2dbbSChao Yu static inline u32 __f2fs_crc32(struct f2fs_sb_info *sbi, u32 crc,
1403704956ecSChao Yu 			      const void *address, unsigned int length)
1404704956ecSChao Yu {
1405704956ecSChao Yu 	struct {
1406704956ecSChao Yu 		struct shash_desc shash;
1407704956ecSChao Yu 		char ctx[4];
1408704956ecSChao Yu 	} desc;
1409704956ecSChao Yu 	int err;
1410704956ecSChao Yu 
1411704956ecSChao Yu 	BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver) != sizeof(desc.ctx));
1412704956ecSChao Yu 
1413704956ecSChao Yu 	desc.shash.tfm = sbi->s_chksum_driver;
1414704956ecSChao Yu 	desc.shash.flags = 0;
1415704956ecSChao Yu 	*(u32 *)desc.ctx = crc;
1416704956ecSChao Yu 
1417704956ecSChao Yu 	err = crypto_shash_update(&desc.shash, address, length);
1418704956ecSChao Yu 	BUG_ON(err);
1419704956ecSChao Yu 
1420704956ecSChao Yu 	return *(u32 *)desc.ctx;
1421704956ecSChao Yu }
1422704956ecSChao Yu 
1423416d2dbbSChao Yu static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
1424416d2dbbSChao Yu 			   unsigned int length)
1425416d2dbbSChao Yu {
1426416d2dbbSChao Yu 	return __f2fs_crc32(sbi, F2FS_SUPER_MAGIC, address, length);
1427416d2dbbSChao Yu }
1428416d2dbbSChao Yu 
1429416d2dbbSChao Yu static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
1430416d2dbbSChao Yu 				  void *buf, size_t buf_size)
1431416d2dbbSChao Yu {
1432416d2dbbSChao Yu 	return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
1433416d2dbbSChao Yu }
1434416d2dbbSChao Yu 
1435416d2dbbSChao Yu static inline u32 f2fs_chksum(struct f2fs_sb_info *sbi, u32 crc,
1436416d2dbbSChao Yu 			      const void *address, unsigned int length)
1437416d2dbbSChao Yu {
1438416d2dbbSChao Yu 	return __f2fs_crc32(sbi, crc, address, length);
1439416d2dbbSChao Yu }
1440416d2dbbSChao Yu 
144139a53e0cSJaegeuk Kim static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
144239a53e0cSJaegeuk Kim {
144339a53e0cSJaegeuk Kim 	return container_of(inode, struct f2fs_inode_info, vfs_inode);
144439a53e0cSJaegeuk Kim }
144539a53e0cSJaegeuk Kim 
144639a53e0cSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
144739a53e0cSJaegeuk Kim {
144839a53e0cSJaegeuk Kim 	return sb->s_fs_info;
144939a53e0cSJaegeuk Kim }
145039a53e0cSJaegeuk Kim 
14514081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
14524081363fSJaegeuk Kim {
14534081363fSJaegeuk Kim 	return F2FS_SB(inode->i_sb);
14544081363fSJaegeuk Kim }
14554081363fSJaegeuk Kim 
14564081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
14574081363fSJaegeuk Kim {
14584081363fSJaegeuk Kim 	return F2FS_I_SB(mapping->host);
14594081363fSJaegeuk Kim }
14604081363fSJaegeuk Kim 
14614081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
14624081363fSJaegeuk Kim {
14634081363fSJaegeuk Kim 	return F2FS_M_SB(page->mapping);
14644081363fSJaegeuk Kim }
14654081363fSJaegeuk Kim 
146639a53e0cSJaegeuk Kim static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
146739a53e0cSJaegeuk Kim {
146839a53e0cSJaegeuk Kim 	return (struct f2fs_super_block *)(sbi->raw_super);
146939a53e0cSJaegeuk Kim }
147039a53e0cSJaegeuk Kim 
147139a53e0cSJaegeuk Kim static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
147239a53e0cSJaegeuk Kim {
147339a53e0cSJaegeuk Kim 	return (struct f2fs_checkpoint *)(sbi->ckpt);
147439a53e0cSJaegeuk Kim }
147539a53e0cSJaegeuk Kim 
147645590710SGu Zheng static inline struct f2fs_node *F2FS_NODE(struct page *page)
147745590710SGu Zheng {
147845590710SGu Zheng 	return (struct f2fs_node *)page_address(page);
147945590710SGu Zheng }
148045590710SGu Zheng 
148158bfaf44SJaegeuk Kim static inline struct f2fs_inode *F2FS_INODE(struct page *page)
148258bfaf44SJaegeuk Kim {
148358bfaf44SJaegeuk Kim 	return &((struct f2fs_node *)page_address(page))->i;
148458bfaf44SJaegeuk Kim }
148558bfaf44SJaegeuk Kim 
148639a53e0cSJaegeuk Kim static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
148739a53e0cSJaegeuk Kim {
148839a53e0cSJaegeuk Kim 	return (struct f2fs_nm_info *)(sbi->nm_info);
148939a53e0cSJaegeuk Kim }
149039a53e0cSJaegeuk Kim 
149139a53e0cSJaegeuk Kim static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
149239a53e0cSJaegeuk Kim {
149339a53e0cSJaegeuk Kim 	return (struct f2fs_sm_info *)(sbi->sm_info);
149439a53e0cSJaegeuk Kim }
149539a53e0cSJaegeuk Kim 
149639a53e0cSJaegeuk Kim static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
149739a53e0cSJaegeuk Kim {
149839a53e0cSJaegeuk Kim 	return (struct sit_info *)(SM_I(sbi)->sit_info);
149939a53e0cSJaegeuk Kim }
150039a53e0cSJaegeuk Kim 
150139a53e0cSJaegeuk Kim static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
150239a53e0cSJaegeuk Kim {
150339a53e0cSJaegeuk Kim 	return (struct free_segmap_info *)(SM_I(sbi)->free_info);
150439a53e0cSJaegeuk Kim }
150539a53e0cSJaegeuk Kim 
150639a53e0cSJaegeuk Kim static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
150739a53e0cSJaegeuk Kim {
150839a53e0cSJaegeuk Kim 	return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
150939a53e0cSJaegeuk Kim }
151039a53e0cSJaegeuk Kim 
15119df27d98SGu Zheng static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
15129df27d98SGu Zheng {
15139df27d98SGu Zheng 	return sbi->meta_inode->i_mapping;
15149df27d98SGu Zheng }
15159df27d98SGu Zheng 
15164ef51a8fSJaegeuk Kim static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
15174ef51a8fSJaegeuk Kim {
15184ef51a8fSJaegeuk Kim 	return sbi->node_inode->i_mapping;
15194ef51a8fSJaegeuk Kim }
15204ef51a8fSJaegeuk Kim 
1521caf0047eSChao Yu static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
152239a53e0cSJaegeuk Kim {
1523fadb2fb8SChao Yu 	return test_bit(type, &sbi->s_flag);
152439a53e0cSJaegeuk Kim }
152539a53e0cSJaegeuk Kim 
1526caf0047eSChao Yu static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
152739a53e0cSJaegeuk Kim {
1528fadb2fb8SChao Yu 	set_bit(type, &sbi->s_flag);
1529caf0047eSChao Yu }
1530caf0047eSChao Yu 
1531caf0047eSChao Yu static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
1532caf0047eSChao Yu {
1533fadb2fb8SChao Yu 	clear_bit(type, &sbi->s_flag);
153439a53e0cSJaegeuk Kim }
153539a53e0cSJaegeuk Kim 
1536d71b5564SJaegeuk Kim static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
1537d71b5564SJaegeuk Kim {
1538d71b5564SJaegeuk Kim 	return le64_to_cpu(cp->checkpoint_ver);
1539d71b5564SJaegeuk Kim }
1540d71b5564SJaegeuk Kim 
1541ea676733SJaegeuk Kim static inline unsigned long f2fs_qf_ino(struct super_block *sb, int type)
1542ea676733SJaegeuk Kim {
1543ea676733SJaegeuk Kim 	if (type < F2FS_MAX_QUOTAS)
1544ea676733SJaegeuk Kim 		return le32_to_cpu(F2FS_SB(sb)->raw_super->qf_ino[type]);
1545ea676733SJaegeuk Kim 	return 0;
1546ea676733SJaegeuk Kim }
1547ea676733SJaegeuk Kim 
1548ced2c7eaSKinglong Mee static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
1549ced2c7eaSKinglong Mee {
1550ced2c7eaSKinglong Mee 	size_t crc_offset = le32_to_cpu(cp->checksum_offset);
1551ced2c7eaSKinglong Mee 	return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
1552ced2c7eaSKinglong Mee }
1553ced2c7eaSKinglong Mee 
1554aaec2b1dSChao Yu static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
155525ca923bSJaegeuk Kim {
155625ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
1557aaec2b1dSChao Yu 
155825ca923bSJaegeuk Kim 	return ckpt_flags & f;
155925ca923bSJaegeuk Kim }
156025ca923bSJaegeuk Kim 
1561aaec2b1dSChao Yu static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
156225ca923bSJaegeuk Kim {
1563aaec2b1dSChao Yu 	return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
1564aaec2b1dSChao Yu }
1565aaec2b1dSChao Yu 
1566aaec2b1dSChao Yu static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1567aaec2b1dSChao Yu {
1568aaec2b1dSChao Yu 	unsigned int ckpt_flags;
1569aaec2b1dSChao Yu 
1570aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
157125ca923bSJaegeuk Kim 	ckpt_flags |= f;
157225ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
157325ca923bSJaegeuk Kim }
157425ca923bSJaegeuk Kim 
1575aaec2b1dSChao Yu static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
157625ca923bSJaegeuk Kim {
1577d1aa2453SChao Yu 	unsigned long flags;
1578d1aa2453SChao Yu 
1579d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1580aaec2b1dSChao Yu 	__set_ckpt_flags(F2FS_CKPT(sbi), f);
1581d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1582aaec2b1dSChao Yu }
1583aaec2b1dSChao Yu 
1584aaec2b1dSChao Yu static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1585aaec2b1dSChao Yu {
1586aaec2b1dSChao Yu 	unsigned int ckpt_flags;
1587aaec2b1dSChao Yu 
1588aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
158925ca923bSJaegeuk Kim 	ckpt_flags &= (~f);
159025ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
159125ca923bSJaegeuk Kim }
159225ca923bSJaegeuk Kim 
1593aaec2b1dSChao Yu static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
1594aaec2b1dSChao Yu {
1595d1aa2453SChao Yu 	unsigned long flags;
1596d1aa2453SChao Yu 
1597d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1598aaec2b1dSChao Yu 	__clear_ckpt_flags(F2FS_CKPT(sbi), f);
1599d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1600aaec2b1dSChao Yu }
1601aaec2b1dSChao Yu 
160222ad0b6aSJaegeuk Kim static inline void disable_nat_bits(struct f2fs_sb_info *sbi, bool lock)
160322ad0b6aSJaegeuk Kim {
1604d1aa2453SChao Yu 	unsigned long flags;
1605d1aa2453SChao Yu 
160622ad0b6aSJaegeuk Kim 	set_sbi_flag(sbi, SBI_NEED_FSCK);
160722ad0b6aSJaegeuk Kim 
160822ad0b6aSJaegeuk Kim 	if (lock)
1609d1aa2453SChao Yu 		spin_lock_irqsave(&sbi->cp_lock, flags);
161022ad0b6aSJaegeuk Kim 	__clear_ckpt_flags(F2FS_CKPT(sbi), CP_NAT_BITS_FLAG);
161122ad0b6aSJaegeuk Kim 	kfree(NM_I(sbi)->nat_bits);
161222ad0b6aSJaegeuk Kim 	NM_I(sbi)->nat_bits = NULL;
161322ad0b6aSJaegeuk Kim 	if (lock)
1614d1aa2453SChao Yu 		spin_unlock_irqrestore(&sbi->cp_lock, flags);
161522ad0b6aSJaegeuk Kim }
161622ad0b6aSJaegeuk Kim 
161722ad0b6aSJaegeuk Kim static inline bool enabled_nat_bits(struct f2fs_sb_info *sbi,
161822ad0b6aSJaegeuk Kim 					struct cp_control *cpc)
161922ad0b6aSJaegeuk Kim {
162022ad0b6aSJaegeuk Kim 	bool set = is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
162122ad0b6aSJaegeuk Kim 
1622c473f1a9SChao Yu 	return (cpc) ? (cpc->reason & CP_UMOUNT) && set : set;
162322ad0b6aSJaegeuk Kim }
162422ad0b6aSJaegeuk Kim 
1625e479556bSGu Zheng static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
162639a53e0cSJaegeuk Kim {
1627b873b798SJaegeuk Kim 	down_read(&sbi->cp_rwsem);
162839a53e0cSJaegeuk Kim }
162939a53e0cSJaegeuk Kim 
1630cc15620bSJaegeuk Kim static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi)
1631cc15620bSJaegeuk Kim {
1632cc15620bSJaegeuk Kim 	return down_read_trylock(&sbi->cp_rwsem);
1633cc15620bSJaegeuk Kim }
1634cc15620bSJaegeuk Kim 
1635e479556bSGu Zheng static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
163639a53e0cSJaegeuk Kim {
1637b873b798SJaegeuk Kim 	up_read(&sbi->cp_rwsem);
163839936837SJaegeuk Kim }
163939936837SJaegeuk Kim 
1640e479556bSGu Zheng static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
164139936837SJaegeuk Kim {
1642b873b798SJaegeuk Kim 	down_write(&sbi->cp_rwsem);
164339936837SJaegeuk Kim }
164439936837SJaegeuk Kim 
1645e479556bSGu Zheng static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
164639936837SJaegeuk Kim {
1647b873b798SJaegeuk Kim 	up_write(&sbi->cp_rwsem);
164839a53e0cSJaegeuk Kim }
164939a53e0cSJaegeuk Kim 
1650119ee914SJaegeuk Kim static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
1651119ee914SJaegeuk Kim {
1652119ee914SJaegeuk Kim 	int reason = CP_SYNC;
1653119ee914SJaegeuk Kim 
1654119ee914SJaegeuk Kim 	if (test_opt(sbi, FASTBOOT))
1655119ee914SJaegeuk Kim 		reason = CP_FASTBOOT;
1656119ee914SJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
1657119ee914SJaegeuk Kim 		reason = CP_UMOUNT;
1658119ee914SJaegeuk Kim 	return reason;
1659119ee914SJaegeuk Kim }
1660119ee914SJaegeuk Kim 
1661119ee914SJaegeuk Kim static inline bool __remain_node_summaries(int reason)
1662119ee914SJaegeuk Kim {
1663c473f1a9SChao Yu 	return (reason & (CP_UMOUNT | CP_FASTBOOT));
1664119ee914SJaegeuk Kim }
1665119ee914SJaegeuk Kim 
1666119ee914SJaegeuk Kim static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
1667119ee914SJaegeuk Kim {
1668aaec2b1dSChao Yu 	return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
1669aaec2b1dSChao Yu 			is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
1670119ee914SJaegeuk Kim }
1671119ee914SJaegeuk Kim 
167239a53e0cSJaegeuk Kim /*
167339a53e0cSJaegeuk Kim  * Check whether the inode has blocks or not
167439a53e0cSJaegeuk Kim  */
167539a53e0cSJaegeuk Kim static inline int F2FS_HAS_BLOCKS(struct inode *inode)
167639a53e0cSJaegeuk Kim {
16770eb0adadSChao Yu 	block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
16780eb0adadSChao Yu 
1679000519f2SChao Yu 	return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block;
168039a53e0cSJaegeuk Kim }
168139a53e0cSJaegeuk Kim 
16824bc8e9bcSChao Yu static inline bool f2fs_has_xattr_block(unsigned int ofs)
16834bc8e9bcSChao Yu {
16844bc8e9bcSChao Yu 	return ofs == XATTR_NODE_OFFSET;
16854bc8e9bcSChao Yu }
16864bc8e9bcSChao Yu 
1687d8a9a229SJaegeuk Kim static inline bool __allow_reserved_blocks(struct f2fs_sb_info *sbi,
1688a90a0884SJaegeuk Kim 					struct inode *inode, bool cap)
16897c2e5963SJaegeuk Kim {
1690d8a9a229SJaegeuk Kim 	if (!inode)
1691d8a9a229SJaegeuk Kim 		return true;
16927c2e5963SJaegeuk Kim 	if (!test_opt(sbi, RESERVE_ROOT))
16937c2e5963SJaegeuk Kim 		return false;
1694d8a9a229SJaegeuk Kim 	if (IS_NOQUOTA(inode))
1695d8a9a229SJaegeuk Kim 		return true;
169663189b78SChao Yu 	if (uid_eq(F2FS_OPTION(sbi).s_resuid, current_fsuid()))
16977c2e5963SJaegeuk Kim 		return true;
169863189b78SChao Yu 	if (!gid_eq(F2FS_OPTION(sbi).s_resgid, GLOBAL_ROOT_GID) &&
169963189b78SChao Yu 					in_group_p(F2FS_OPTION(sbi).s_resgid))
17007c2e5963SJaegeuk Kim 		return true;
1701a90a0884SJaegeuk Kim 	if (cap && capable(CAP_SYS_RESOURCE))
1702162b27aeSJaegeuk Kim 		return true;
17037c2e5963SJaegeuk Kim 	return false;
17047c2e5963SJaegeuk Kim }
17057c2e5963SJaegeuk Kim 
17060abd675eSChao Yu static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool);
17070abd675eSChao Yu static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
170846008c6dSChao Yu 				 struct inode *inode, blkcnt_t *count)
170939a53e0cSJaegeuk Kim {
17100abd675eSChao Yu 	blkcnt_t diff = 0, release = 0;
1711daeb433eSChao Yu 	block_t avail_user_block_count;
17120abd675eSChao Yu 	int ret;
17130abd675eSChao Yu 
17140abd675eSChao Yu 	ret = dquot_reserve_block(inode, *count);
17150abd675eSChao Yu 	if (ret)
17160abd675eSChao Yu 		return ret;
1717dd11a5dfSJaegeuk Kim 
171855523519SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
171955523519SChao Yu 		f2fs_show_injection_info(FAULT_BLOCK);
17200abd675eSChao Yu 		release = *count;
17210abd675eSChao Yu 		goto enospc;
172255523519SChao Yu 	}
17237fa750a1SArnd Bergmann 
1724dd11a5dfSJaegeuk Kim 	/*
1725dd11a5dfSJaegeuk Kim 	 * let's increase this in prior to actual block count change in order
1726dd11a5dfSJaegeuk Kim 	 * for f2fs_sync_file to avoid data races when deciding checkpoint.
1727dd11a5dfSJaegeuk Kim 	 */
1728dd11a5dfSJaegeuk Kim 	percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
172939a53e0cSJaegeuk Kim 
173039a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
17312555a2d5SJaegeuk Kim 	sbi->total_valid_block_count += (block_t)(*count);
173280d42145SYunlong Song 	avail_user_block_count = sbi->user_block_count -
173380d42145SYunlong Song 					sbi->current_reserved_blocks;
17347e65be49SJaegeuk Kim 
1735a90a0884SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode, true))
173663189b78SChao Yu 		avail_user_block_count -= F2FS_OPTION(sbi).root_reserved_blocks;
17377e65be49SJaegeuk Kim 
1738daeb433eSChao Yu 	if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
1739daeb433eSChao Yu 		diff = sbi->total_valid_block_count - avail_user_block_count;
17407e65be49SJaegeuk Kim 		if (diff > *count)
17417e65be49SJaegeuk Kim 			diff = *count;
1742dd11a5dfSJaegeuk Kim 		*count -= diff;
17430abd675eSChao Yu 		release = diff;
17447e65be49SJaegeuk Kim 		sbi->total_valid_block_count -= diff;
174546008c6dSChao Yu 		if (!*count) {
174639a53e0cSJaegeuk Kim 			spin_unlock(&sbi->stat_lock);
17470abd675eSChao Yu 			goto enospc;
174839a53e0cSJaegeuk Kim 		}
174946008c6dSChao Yu 	}
175039a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
175141382ec4SJaegeuk Kim 
175236b877afSDaniel Rosenberg 	if (unlikely(release)) {
175336b877afSDaniel Rosenberg 		percpu_counter_sub(&sbi->alloc_valid_block_count, release);
17540abd675eSChao Yu 		dquot_release_reservation_block(inode, release);
175536b877afSDaniel Rosenberg 	}
17560abd675eSChao Yu 	f2fs_i_blocks_write(inode, *count, true, true);
17570abd675eSChao Yu 	return 0;
17580abd675eSChao Yu 
17590abd675eSChao Yu enospc:
176036b877afSDaniel Rosenberg 	percpu_counter_sub(&sbi->alloc_valid_block_count, release);
17610abd675eSChao Yu 	dquot_release_reservation_block(inode, release);
17620abd675eSChao Yu 	return -ENOSPC;
176339a53e0cSJaegeuk Kim }
176439a53e0cSJaegeuk Kim 
1765da19b0dcSGu Zheng static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
176639a53e0cSJaegeuk Kim 						struct inode *inode,
17670eb0adadSChao Yu 						block_t count)
176839a53e0cSJaegeuk Kim {
17690eb0adadSChao Yu 	blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK;
17700eb0adadSChao Yu 
177139a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
17729850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
17730eb0adadSChao Yu 	f2fs_bug_on(sbi, inode->i_blocks < sectors);
177439a53e0cSJaegeuk Kim 	sbi->total_valid_block_count -= (block_t)count;
177580d42145SYunlong Song 	if (sbi->reserved_blocks &&
177680d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
177780d42145SYunlong Song 		sbi->current_reserved_blocks = min(sbi->reserved_blocks,
177880d42145SYunlong Song 					sbi->current_reserved_blocks + count);
177939a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
17800abd675eSChao Yu 	f2fs_i_blocks_write(inode, count, false, true);
178139a53e0cSJaegeuk Kim }
178239a53e0cSJaegeuk Kim 
178339a53e0cSJaegeuk Kim static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
178439a53e0cSJaegeuk Kim {
178535782b23SJaegeuk Kim 	atomic_inc(&sbi->nr_pages[count_type]);
17867c4abcbeSChao Yu 
178736951b38SChao Yu 	if (count_type == F2FS_DIRTY_DATA || count_type == F2FS_INMEM_PAGES ||
178836951b38SChao Yu 		count_type == F2FS_WB_CP_DATA || count_type == F2FS_WB_DATA)
17897c4abcbeSChao Yu 		return;
17907c4abcbeSChao Yu 
1791caf0047eSChao Yu 	set_sbi_flag(sbi, SBI_IS_DIRTY);
179239a53e0cSJaegeuk Kim }
179339a53e0cSJaegeuk Kim 
1794a7ffdbe2SJaegeuk Kim static inline void inode_inc_dirty_pages(struct inode *inode)
179539a53e0cSJaegeuk Kim {
1796204706c7SJaegeuk Kim 	atomic_inc(&F2FS_I(inode)->dirty_pages);
1797c227f912SChao Yu 	inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1798c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
17992c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
18002c8a4a28SJaegeuk Kim 		inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
180139a53e0cSJaegeuk Kim }
180239a53e0cSJaegeuk Kim 
180339a53e0cSJaegeuk Kim static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
180439a53e0cSJaegeuk Kim {
180535782b23SJaegeuk Kim 	atomic_dec(&sbi->nr_pages[count_type]);
180639a53e0cSJaegeuk Kim }
180739a53e0cSJaegeuk Kim 
1808a7ffdbe2SJaegeuk Kim static inline void inode_dec_dirty_pages(struct inode *inode)
180939a53e0cSJaegeuk Kim {
18105ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
18115ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
18121fe54f9dSJaegeuk Kim 		return;
18131fe54f9dSJaegeuk Kim 
1814204706c7SJaegeuk Kim 	atomic_dec(&F2FS_I(inode)->dirty_pages);
1815c227f912SChao Yu 	dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1816c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
18172c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
18182c8a4a28SJaegeuk Kim 		dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
181939a53e0cSJaegeuk Kim }
182039a53e0cSJaegeuk Kim 
1821523be8a6SJaegeuk Kim static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
182239a53e0cSJaegeuk Kim {
182335782b23SJaegeuk Kim 	return atomic_read(&sbi->nr_pages[count_type]);
182439a53e0cSJaegeuk Kim }
182539a53e0cSJaegeuk Kim 
1826204706c7SJaegeuk Kim static inline int get_dirty_pages(struct inode *inode)
1827f8b2c1f9SJaegeuk Kim {
1828204706c7SJaegeuk Kim 	return atomic_read(&F2FS_I(inode)->dirty_pages);
1829f8b2c1f9SJaegeuk Kim }
1830f8b2c1f9SJaegeuk Kim 
18315ac206cfSNamjae Jeon static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
18325ac206cfSNamjae Jeon {
18333519e3f9SChao Yu 	unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
1834523be8a6SJaegeuk Kim 	unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
1835523be8a6SJaegeuk Kim 						sbi->log_blocks_per_seg;
1836523be8a6SJaegeuk Kim 
1837523be8a6SJaegeuk Kim 	return segs / sbi->segs_per_sec;
18385ac206cfSNamjae Jeon }
18395ac206cfSNamjae Jeon 
184039a53e0cSJaegeuk Kim static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
184139a53e0cSJaegeuk Kim {
18428b8343faSJaegeuk Kim 	return sbi->total_valid_block_count;
184339a53e0cSJaegeuk Kim }
184439a53e0cSJaegeuk Kim 
1845f83a2584SYunlei He static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
1846f83a2584SYunlei He {
1847f83a2584SYunlei He 	return sbi->discard_blks;
1848f83a2584SYunlei He }
1849f83a2584SYunlei He 
185039a53e0cSJaegeuk Kim static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
185139a53e0cSJaegeuk Kim {
185239a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
185339a53e0cSJaegeuk Kim 
185439a53e0cSJaegeuk Kim 	/* return NAT or SIT bitmap */
185539a53e0cSJaegeuk Kim 	if (flag == NAT_BITMAP)
185639a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
185739a53e0cSJaegeuk Kim 	else if (flag == SIT_BITMAP)
185839a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
185939a53e0cSJaegeuk Kim 
186039a53e0cSJaegeuk Kim 	return 0;
186139a53e0cSJaegeuk Kim }
186239a53e0cSJaegeuk Kim 
186355141486SWanpeng Li static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
186455141486SWanpeng Li {
186555141486SWanpeng Li 	return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
186655141486SWanpeng Li }
186755141486SWanpeng Li 
186839a53e0cSJaegeuk Kim static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
186939a53e0cSJaegeuk Kim {
187039a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
18711dbe4152SChangman Lee 	int offset;
18721dbe4152SChangman Lee 
1873199bc3feSChao Yu 	if (is_set_ckpt_flags(sbi, CP_LARGE_NAT_BITMAP_FLAG)) {
1874199bc3feSChao Yu 		offset = (flag == SIT_BITMAP) ?
1875199bc3feSChao Yu 			le32_to_cpu(ckpt->nat_ver_bitmap_bytesize) : 0;
1876199bc3feSChao Yu 		return &ckpt->sit_nat_version_bitmap + offset;
1877199bc3feSChao Yu 	}
1878199bc3feSChao Yu 
187955141486SWanpeng Li 	if (__cp_payload(sbi) > 0) {
18801dbe4152SChangman Lee 		if (flag == NAT_BITMAP)
18811dbe4152SChangman Lee 			return &ckpt->sit_nat_version_bitmap;
18821dbe4152SChangman Lee 		else
188365b85cccSJaegeuk Kim 			return (unsigned char *)ckpt + F2FS_BLKSIZE;
18841dbe4152SChangman Lee 	} else {
18851dbe4152SChangman Lee 		offset = (flag == NAT_BITMAP) ?
188625ca923bSJaegeuk Kim 			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
188739a53e0cSJaegeuk Kim 		return &ckpt->sit_nat_version_bitmap + offset;
188839a53e0cSJaegeuk Kim 	}
18891dbe4152SChangman Lee }
189039a53e0cSJaegeuk Kim 
189139a53e0cSJaegeuk Kim static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
189239a53e0cSJaegeuk Kim {
18938508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
189439a53e0cSJaegeuk Kim 
18958508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 2)
189639a53e0cSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
189739a53e0cSJaegeuk Kim 	return start_addr;
189839a53e0cSJaegeuk Kim }
189939a53e0cSJaegeuk Kim 
19008508e44aSJaegeuk Kim static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
19018508e44aSJaegeuk Kim {
19028508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
19038508e44aSJaegeuk Kim 
19048508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 1)
19058508e44aSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
19068508e44aSJaegeuk Kim 	return start_addr;
19078508e44aSJaegeuk Kim }
19088508e44aSJaegeuk Kim 
19098508e44aSJaegeuk Kim static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
19108508e44aSJaegeuk Kim {
19118508e44aSJaegeuk Kim 	sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
19128508e44aSJaegeuk Kim }
19138508e44aSJaegeuk Kim 
191439a53e0cSJaegeuk Kim static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
191539a53e0cSJaegeuk Kim {
191639a53e0cSJaegeuk Kim 	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
191739a53e0cSJaegeuk Kim }
191839a53e0cSJaegeuk Kim 
19190abd675eSChao Yu static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
1920000519f2SChao Yu 					struct inode *inode, bool is_inode)
192139a53e0cSJaegeuk Kim {
192239a53e0cSJaegeuk Kim 	block_t	valid_block_count;
192339a53e0cSJaegeuk Kim 	unsigned int valid_node_count;
19240abd675eSChao Yu 	bool quota = inode && !is_inode;
19250abd675eSChao Yu 
19260abd675eSChao Yu 	if (quota) {
19270abd675eSChao Yu 		int ret = dquot_reserve_block(inode, 1);
19280abd675eSChao Yu 		if (ret)
19290abd675eSChao Yu 			return ret;
19300abd675eSChao Yu 	}
193139a53e0cSJaegeuk Kim 
1932812c6056SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
1933812c6056SChao Yu 		f2fs_show_injection_info(FAULT_BLOCK);
1934812c6056SChao Yu 		goto enospc;
1935812c6056SChao Yu 	}
1936812c6056SChao Yu 
193739a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
193839a53e0cSJaegeuk Kim 
19397e65be49SJaegeuk Kim 	valid_block_count = sbi->total_valid_block_count +
19407e65be49SJaegeuk Kim 					sbi->current_reserved_blocks + 1;
19417e65be49SJaegeuk Kim 
1942a90a0884SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode, false))
194363189b78SChao Yu 		valid_block_count += F2FS_OPTION(sbi).root_reserved_blocks;
19447e65be49SJaegeuk Kim 
19457e65be49SJaegeuk Kim 	if (unlikely(valid_block_count > sbi->user_block_count)) {
194639a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
19470abd675eSChao Yu 		goto enospc;
194839a53e0cSJaegeuk Kim 	}
194939a53e0cSJaegeuk Kim 
1950ef86d709SGu Zheng 	valid_node_count = sbi->total_valid_node_count + 1;
1951cfb271d4SChao Yu 	if (unlikely(valid_node_count > sbi->total_node_count)) {
195239a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
19530abd675eSChao Yu 		goto enospc;
195439a53e0cSJaegeuk Kim 	}
195539a53e0cSJaegeuk Kim 
1956ef86d709SGu Zheng 	sbi->total_valid_node_count++;
1957ef86d709SGu Zheng 	sbi->total_valid_block_count++;
195839a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
195939a53e0cSJaegeuk Kim 
19600abd675eSChao Yu 	if (inode) {
19610abd675eSChao Yu 		if (is_inode)
19620abd675eSChao Yu 			f2fs_mark_inode_dirty_sync(inode, true);
19630abd675eSChao Yu 		else
19640abd675eSChao Yu 			f2fs_i_blocks_write(inode, 1, true, true);
19650abd675eSChao Yu 	}
19660abd675eSChao Yu 
196741382ec4SJaegeuk Kim 	percpu_counter_inc(&sbi->alloc_valid_block_count);
19680abd675eSChao Yu 	return 0;
19690abd675eSChao Yu 
19700abd675eSChao Yu enospc:
19710abd675eSChao Yu 	if (quota)
19720abd675eSChao Yu 		dquot_release_reservation_block(inode, 1);
19730abd675eSChao Yu 	return -ENOSPC;
197439a53e0cSJaegeuk Kim }
197539a53e0cSJaegeuk Kim 
197639a53e0cSJaegeuk Kim static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
1977000519f2SChao Yu 					struct inode *inode, bool is_inode)
197839a53e0cSJaegeuk Kim {
197939a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
198039a53e0cSJaegeuk Kim 
19819850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_block_count);
19829850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_node_count);
1983000519f2SChao Yu 	f2fs_bug_on(sbi, !is_inode && !inode->i_blocks);
198439a53e0cSJaegeuk Kim 
1985ef86d709SGu Zheng 	sbi->total_valid_node_count--;
1986ef86d709SGu Zheng 	sbi->total_valid_block_count--;
198780d42145SYunlong Song 	if (sbi->reserved_blocks &&
198880d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
198980d42145SYunlong Song 		sbi->current_reserved_blocks++;
199039a53e0cSJaegeuk Kim 
199139a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
19920abd675eSChao Yu 
19930abd675eSChao Yu 	if (!is_inode)
19940abd675eSChao Yu 		f2fs_i_blocks_write(inode, 1, false, true);
199539a53e0cSJaegeuk Kim }
199639a53e0cSJaegeuk Kim 
199739a53e0cSJaegeuk Kim static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
199839a53e0cSJaegeuk Kim {
19998b8343faSJaegeuk Kim 	return sbi->total_valid_node_count;
200039a53e0cSJaegeuk Kim }
200139a53e0cSJaegeuk Kim 
200239a53e0cSJaegeuk Kim static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
200339a53e0cSJaegeuk Kim {
2004513c5f37SJaegeuk Kim 	percpu_counter_inc(&sbi->total_valid_inode_count);
200539a53e0cSJaegeuk Kim }
200639a53e0cSJaegeuk Kim 
20070e80220aSJaegeuk Kim static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
200839a53e0cSJaegeuk Kim {
2009513c5f37SJaegeuk Kim 	percpu_counter_dec(&sbi->total_valid_inode_count);
201039a53e0cSJaegeuk Kim }
201139a53e0cSJaegeuk Kim 
2012513c5f37SJaegeuk Kim static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
201339a53e0cSJaegeuk Kim {
2014513c5f37SJaegeuk Kim 	return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
201539a53e0cSJaegeuk Kim }
201639a53e0cSJaegeuk Kim 
2017a56c7c6fSJaegeuk Kim static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
2018a56c7c6fSJaegeuk Kim 						pgoff_t index, bool for_write)
2019a56c7c6fSJaegeuk Kim {
202082cf4f13SChao Yu 	struct page *page;
2021cac5a3d8SDongOh Shin 
20227fa750a1SArnd Bergmann 	if (IS_ENABLED(CONFIG_F2FS_FAULT_INJECTION)) {
202382cf4f13SChao Yu 		if (!for_write)
202482cf4f13SChao Yu 			page = find_get_page_flags(mapping, index,
202582cf4f13SChao Yu 							FGP_LOCK | FGP_ACCESSED);
202682cf4f13SChao Yu 		else
202782cf4f13SChao Yu 			page = find_lock_page(mapping, index);
2028c41f3cc3SJaegeuk Kim 		if (page)
2029c41f3cc3SJaegeuk Kim 			return page;
2030c41f3cc3SJaegeuk Kim 
203155523519SChao Yu 		if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) {
203255523519SChao Yu 			f2fs_show_injection_info(FAULT_PAGE_ALLOC);
2033c41f3cc3SJaegeuk Kim 			return NULL;
203455523519SChao Yu 		}
20357fa750a1SArnd Bergmann 	}
20367fa750a1SArnd Bergmann 
2037a56c7c6fSJaegeuk Kim 	if (!for_write)
2038a56c7c6fSJaegeuk Kim 		return grab_cache_page(mapping, index);
2039a56c7c6fSJaegeuk Kim 	return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
2040a56c7c6fSJaegeuk Kim }
2041a56c7c6fSJaegeuk Kim 
204201eccef7SChao Yu static inline struct page *f2fs_pagecache_get_page(
204301eccef7SChao Yu 				struct address_space *mapping, pgoff_t index,
204401eccef7SChao Yu 				int fgp_flags, gfp_t gfp_mask)
204501eccef7SChao Yu {
204601eccef7SChao Yu 	if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_GET)) {
204701eccef7SChao Yu 		f2fs_show_injection_info(FAULT_PAGE_GET);
204801eccef7SChao Yu 		return NULL;
204901eccef7SChao Yu 	}
20507fa750a1SArnd Bergmann 
205101eccef7SChao Yu 	return pagecache_get_page(mapping, index, fgp_flags, gfp_mask);
205201eccef7SChao Yu }
205301eccef7SChao Yu 
20546e2c64adSJaegeuk Kim static inline void f2fs_copy_page(struct page *src, struct page *dst)
20556e2c64adSJaegeuk Kim {
20566e2c64adSJaegeuk Kim 	char *src_kaddr = kmap(src);
20576e2c64adSJaegeuk Kim 	char *dst_kaddr = kmap(dst);
20586e2c64adSJaegeuk Kim 
20596e2c64adSJaegeuk Kim 	memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
20606e2c64adSJaegeuk Kim 	kunmap(dst);
20616e2c64adSJaegeuk Kim 	kunmap(src);
20626e2c64adSJaegeuk Kim }
20636e2c64adSJaegeuk Kim 
206439a53e0cSJaegeuk Kim static inline void f2fs_put_page(struct page *page, int unlock)
206539a53e0cSJaegeuk Kim {
2066031fa8ccSJaegeuk Kim 	if (!page)
206739a53e0cSJaegeuk Kim 		return;
206839a53e0cSJaegeuk Kim 
206939a53e0cSJaegeuk Kim 	if (unlock) {
20709850cf4aSJaegeuk Kim 		f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
207139a53e0cSJaegeuk Kim 		unlock_page(page);
207239a53e0cSJaegeuk Kim 	}
207309cbfeafSKirill A. Shutemov 	put_page(page);
207439a53e0cSJaegeuk Kim }
207539a53e0cSJaegeuk Kim 
207639a53e0cSJaegeuk Kim static inline void f2fs_put_dnode(struct dnode_of_data *dn)
207739a53e0cSJaegeuk Kim {
207839a53e0cSJaegeuk Kim 	if (dn->node_page)
207939a53e0cSJaegeuk Kim 		f2fs_put_page(dn->node_page, 1);
208039a53e0cSJaegeuk Kim 	if (dn->inode_page && dn->node_page != dn->inode_page)
208139a53e0cSJaegeuk Kim 		f2fs_put_page(dn->inode_page, 0);
208239a53e0cSJaegeuk Kim 	dn->node_page = NULL;
208339a53e0cSJaegeuk Kim 	dn->inode_page = NULL;
208439a53e0cSJaegeuk Kim }
208539a53e0cSJaegeuk Kim 
208639a53e0cSJaegeuk Kim static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
2087e8512d2eSGu Zheng 					size_t size)
208839a53e0cSJaegeuk Kim {
2089e8512d2eSGu Zheng 	return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
209039a53e0cSJaegeuk Kim }
209139a53e0cSJaegeuk Kim 
20927bd59381SGu Zheng static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
20937bd59381SGu Zheng 						gfp_t flags)
20947bd59381SGu Zheng {
20957bd59381SGu Zheng 	void *entry;
20967bd59381SGu Zheng 
209780c54505SJaegeuk Kim 	entry = kmem_cache_alloc(cachep, flags);
209880c54505SJaegeuk Kim 	if (!entry)
209980c54505SJaegeuk Kim 		entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
21007bd59381SGu Zheng 	return entry;
21017bd59381SGu Zheng }
21027bd59381SGu Zheng 
2103d62fe971SChao Yu static inline struct bio *f2fs_bio_alloc(struct f2fs_sb_info *sbi,
2104d62fe971SChao Yu 						int npages, bool no_fail)
2105740432f8SJaegeuk Kim {
2106740432f8SJaegeuk Kim 	struct bio *bio;
2107740432f8SJaegeuk Kim 
2108d62fe971SChao Yu 	if (no_fail) {
2109740432f8SJaegeuk Kim 		/* No failure on bio allocation */
2110740432f8SJaegeuk Kim 		bio = bio_alloc(GFP_NOIO, npages);
211180c54505SJaegeuk Kim 		if (!bio)
211280c54505SJaegeuk Kim 			bio = bio_alloc(GFP_NOIO | __GFP_NOFAIL, npages);
2113740432f8SJaegeuk Kim 		return bio;
2114740432f8SJaegeuk Kim 	}
2115d62fe971SChao Yu 	if (time_to_inject(sbi, FAULT_ALLOC_BIO)) {
2116d62fe971SChao Yu 		f2fs_show_injection_info(FAULT_ALLOC_BIO);
2117d62fe971SChao Yu 		return NULL;
2118d62fe971SChao Yu 	}
21197fa750a1SArnd Bergmann 
2120d62fe971SChao Yu 	return bio_alloc(GFP_KERNEL, npages);
2121d62fe971SChao Yu }
2122740432f8SJaegeuk Kim 
21239be32d72SJaegeuk Kim static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
21249be32d72SJaegeuk Kim 				unsigned long index, void *item)
21259be32d72SJaegeuk Kim {
21269be32d72SJaegeuk Kim 	while (radix_tree_insert(root, index, item))
21279be32d72SJaegeuk Kim 		cond_resched();
21289be32d72SJaegeuk Kim }
21299be32d72SJaegeuk Kim 
213039a53e0cSJaegeuk Kim #define RAW_IS_INODE(p)	((p)->footer.nid == (p)->footer.ino)
213139a53e0cSJaegeuk Kim 
213239a53e0cSJaegeuk Kim static inline bool IS_INODE(struct page *page)
213339a53e0cSJaegeuk Kim {
213445590710SGu Zheng 	struct f2fs_node *p = F2FS_NODE(page);
2135cac5a3d8SDongOh Shin 
213639a53e0cSJaegeuk Kim 	return RAW_IS_INODE(p);
213739a53e0cSJaegeuk Kim }
213839a53e0cSJaegeuk Kim 
21397a2af766SChao Yu static inline int offset_in_addr(struct f2fs_inode *i)
21407a2af766SChao Yu {
21417a2af766SChao Yu 	return (i->i_inline & F2FS_EXTRA_ATTR) ?
21427a2af766SChao Yu 			(le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0;
21437a2af766SChao Yu }
21447a2af766SChao Yu 
214539a53e0cSJaegeuk Kim static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
214639a53e0cSJaegeuk Kim {
214739a53e0cSJaegeuk Kim 	return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
214839a53e0cSJaegeuk Kim }
214939a53e0cSJaegeuk Kim 
21507a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode);
21517a2af766SChao Yu static inline block_t datablock_addr(struct inode *inode,
21527a2af766SChao Yu 			struct page *node_page, unsigned int offset)
215339a53e0cSJaegeuk Kim {
215439a53e0cSJaegeuk Kim 	struct f2fs_node *raw_node;
215539a53e0cSJaegeuk Kim 	__le32 *addr_array;
21567a2af766SChao Yu 	int base = 0;
21577a2af766SChao Yu 	bool is_inode = IS_INODE(node_page);
2158cac5a3d8SDongOh Shin 
215945590710SGu Zheng 	raw_node = F2FS_NODE(node_page);
21607a2af766SChao Yu 
21617a2af766SChao Yu 	/* from GC path only */
2162979f492fSLiFan 	if (is_inode) {
2163979f492fSLiFan 		if (!inode)
21647a2af766SChao Yu 			base = offset_in_addr(&raw_node->i);
2165979f492fSLiFan 		else if (f2fs_has_extra_attr(inode))
21667a2af766SChao Yu 			base = get_extra_isize(inode);
21677a2af766SChao Yu 	}
21687a2af766SChao Yu 
216939a53e0cSJaegeuk Kim 	addr_array = blkaddr_in_node(raw_node);
21707a2af766SChao Yu 	return le32_to_cpu(addr_array[base + offset]);
217139a53e0cSJaegeuk Kim }
217239a53e0cSJaegeuk Kim 
217339a53e0cSJaegeuk Kim static inline int f2fs_test_bit(unsigned int nr, char *addr)
217439a53e0cSJaegeuk Kim {
217539a53e0cSJaegeuk Kim 	int mask;
217639a53e0cSJaegeuk Kim 
217739a53e0cSJaegeuk Kim 	addr += (nr >> 3);
217839a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
217939a53e0cSJaegeuk Kim 	return mask & *addr;
218039a53e0cSJaegeuk Kim }
218139a53e0cSJaegeuk Kim 
2182a66cdd98SJaegeuk Kim static inline void f2fs_set_bit(unsigned int nr, char *addr)
2183a66cdd98SJaegeuk Kim {
2184a66cdd98SJaegeuk Kim 	int mask;
2185a66cdd98SJaegeuk Kim 
2186a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2187a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2188a66cdd98SJaegeuk Kim 	*addr |= mask;
2189a66cdd98SJaegeuk Kim }
2190a66cdd98SJaegeuk Kim 
2191a66cdd98SJaegeuk Kim static inline void f2fs_clear_bit(unsigned int nr, char *addr)
2192a66cdd98SJaegeuk Kim {
2193a66cdd98SJaegeuk Kim 	int mask;
2194a66cdd98SJaegeuk Kim 
2195a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2196a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2197a66cdd98SJaegeuk Kim 	*addr &= ~mask;
2198a66cdd98SJaegeuk Kim }
2199a66cdd98SJaegeuk Kim 
220052aca074SGu Zheng static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
220139a53e0cSJaegeuk Kim {
220239a53e0cSJaegeuk Kim 	int mask;
220339a53e0cSJaegeuk Kim 	int ret;
220439a53e0cSJaegeuk Kim 
220539a53e0cSJaegeuk Kim 	addr += (nr >> 3);
220639a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
220739a53e0cSJaegeuk Kim 	ret = mask & *addr;
220839a53e0cSJaegeuk Kim 	*addr |= mask;
220939a53e0cSJaegeuk Kim 	return ret;
221039a53e0cSJaegeuk Kim }
221139a53e0cSJaegeuk Kim 
221252aca074SGu Zheng static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
221339a53e0cSJaegeuk Kim {
221439a53e0cSJaegeuk Kim 	int mask;
221539a53e0cSJaegeuk Kim 	int ret;
221639a53e0cSJaegeuk Kim 
221739a53e0cSJaegeuk Kim 	addr += (nr >> 3);
221839a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
221939a53e0cSJaegeuk Kim 	ret = mask & *addr;
222039a53e0cSJaegeuk Kim 	*addr &= ~mask;
222139a53e0cSJaegeuk Kim 	return ret;
222239a53e0cSJaegeuk Kim }
222339a53e0cSJaegeuk Kim 
2224c6ac4c0eSGu Zheng static inline void f2fs_change_bit(unsigned int nr, char *addr)
2225c6ac4c0eSGu Zheng {
2226c6ac4c0eSGu Zheng 	int mask;
2227c6ac4c0eSGu Zheng 
2228c6ac4c0eSGu Zheng 	addr += (nr >> 3);
2229c6ac4c0eSGu Zheng 	mask = 1 << (7 - (nr & 0x07));
2230c6ac4c0eSGu Zheng 	*addr ^= mask;
2231c6ac4c0eSGu Zheng }
2232c6ac4c0eSGu Zheng 
223359c84408SChao Yu /*
223459c84408SChao Yu  * Inode flags
223559c84408SChao Yu  */
223659c84408SChao Yu #define F2FS_SECRM_FL			0x00000001 /* Secure deletion */
223759c84408SChao Yu #define F2FS_UNRM_FL			0x00000002 /* Undelete */
223859c84408SChao Yu #define F2FS_COMPR_FL			0x00000004 /* Compress file */
223959c84408SChao Yu #define F2FS_SYNC_FL			0x00000008 /* Synchronous updates */
224059c84408SChao Yu #define F2FS_IMMUTABLE_FL		0x00000010 /* Immutable file */
224159c84408SChao Yu #define F2FS_APPEND_FL			0x00000020 /* writes to file may only append */
224259c84408SChao Yu #define F2FS_NODUMP_FL			0x00000040 /* do not dump file */
224359c84408SChao Yu #define F2FS_NOATIME_FL			0x00000080 /* do not update atime */
224459c84408SChao Yu /* Reserved for compression usage... */
224559c84408SChao Yu #define F2FS_DIRTY_FL			0x00000100
224659c84408SChao Yu #define F2FS_COMPRBLK_FL		0x00000200 /* One or more compressed clusters */
224759c84408SChao Yu #define F2FS_NOCOMPR_FL			0x00000400 /* Don't compress */
224859c84408SChao Yu #define F2FS_ENCRYPT_FL			0x00000800 /* encrypted file */
224959c84408SChao Yu /* End compression flags --- maybe not all used */
225059c84408SChao Yu #define F2FS_INDEX_FL			0x00001000 /* hash-indexed directory */
225159c84408SChao Yu #define F2FS_IMAGIC_FL			0x00002000 /* AFS directory */
225259c84408SChao Yu #define F2FS_JOURNAL_DATA_FL		0x00004000 /* file data should be journaled */
225359c84408SChao Yu #define F2FS_NOTAIL_FL			0x00008000 /* file tail should not be merged */
225459c84408SChao Yu #define F2FS_DIRSYNC_FL			0x00010000 /* dirsync behaviour (directories only) */
225559c84408SChao Yu #define F2FS_TOPDIR_FL			0x00020000 /* Top of directory hierarchies*/
225659c84408SChao Yu #define F2FS_HUGE_FILE_FL               0x00040000 /* Set to each huge file */
225759c84408SChao Yu #define F2FS_EXTENTS_FL			0x00080000 /* Inode uses extents */
225859c84408SChao Yu #define F2FS_EA_INODE_FL	        0x00200000 /* Inode used for large EA */
225959c84408SChao Yu #define F2FS_EOFBLOCKS_FL		0x00400000 /* Blocks allocated beyond EOF */
226059c84408SChao Yu #define F2FS_INLINE_DATA_FL		0x10000000 /* Inode has inline data. */
226159c84408SChao Yu #define F2FS_PROJINHERIT_FL		0x20000000 /* Create with parents projid */
226259c84408SChao Yu #define F2FS_RESERVED_FL		0x80000000 /* reserved for ext4 lib */
226359c84408SChao Yu 
226459c84408SChao Yu #define F2FS_FL_USER_VISIBLE		0x304BDFFF /* User visible flags */
226559c84408SChao Yu #define F2FS_FL_USER_MODIFIABLE		0x204BC0FF /* User modifiable flags */
226659c84408SChao Yu 
226759c84408SChao Yu /* Flags we can manipulate with through F2FS_IOC_FSSETXATTR */
226859c84408SChao Yu #define F2FS_FL_XFLAG_VISIBLE		(F2FS_SYNC_FL | \
226959c84408SChao Yu 					 F2FS_IMMUTABLE_FL | \
227059c84408SChao Yu 					 F2FS_APPEND_FL | \
227159c84408SChao Yu 					 F2FS_NODUMP_FL | \
227259c84408SChao Yu 					 F2FS_NOATIME_FL | \
227359c84408SChao Yu 					 F2FS_PROJINHERIT_FL)
227459c84408SChao Yu 
227559c84408SChao Yu /* Flags that should be inherited by new inodes from their parent. */
227659c84408SChao Yu #define F2FS_FL_INHERITED (F2FS_SECRM_FL | F2FS_UNRM_FL | F2FS_COMPR_FL |\
227759c84408SChao Yu 			   F2FS_SYNC_FL | F2FS_NODUMP_FL | F2FS_NOATIME_FL |\
227859c84408SChao Yu 			   F2FS_NOCOMPR_FL | F2FS_JOURNAL_DATA_FL |\
227959c84408SChao Yu 			   F2FS_NOTAIL_FL | F2FS_DIRSYNC_FL |\
228059c84408SChao Yu 			   F2FS_PROJINHERIT_FL)
228159c84408SChao Yu 
228259c84408SChao Yu /* Flags that are appropriate for regular files (all but dir-specific ones). */
228359c84408SChao Yu #define F2FS_REG_FLMASK		(~(F2FS_DIRSYNC_FL | F2FS_TOPDIR_FL))
228459c84408SChao Yu 
228559c84408SChao Yu /* Flags that are appropriate for non-directories/regular files. */
228659c84408SChao Yu #define F2FS_OTHER_FLMASK	(F2FS_NODUMP_FL | F2FS_NOATIME_FL)
22875c57132eSChao Yu 
22885c57132eSChao Yu static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
22895c57132eSChao Yu {
22905c57132eSChao Yu 	if (S_ISDIR(mode))
22915c57132eSChao Yu 		return flags;
22925c57132eSChao Yu 	else if (S_ISREG(mode))
22935c57132eSChao Yu 		return flags & F2FS_REG_FLMASK;
22945c57132eSChao Yu 	else
22955c57132eSChao Yu 		return flags & F2FS_OTHER_FLMASK;
22965c57132eSChao Yu }
22975c57132eSChao Yu 
229839a53e0cSJaegeuk Kim /* used for f2fs_inode_info->flags */
229939a53e0cSJaegeuk Kim enum {
230039a53e0cSJaegeuk Kim 	FI_NEW_INODE,		/* indicate newly allocated inode */
2301b3783873SJaegeuk Kim 	FI_DIRTY_INODE,		/* indicate inode is dirty or not */
230226de9b11SJaegeuk Kim 	FI_AUTO_RECOVER,	/* indicate inode is recoverable */
2303ed57c27fSJaegeuk Kim 	FI_DIRTY_DIR,		/* indicate directory has dirty pages */
230439a53e0cSJaegeuk Kim 	FI_INC_LINK,		/* need to increment i_nlink */
230539a53e0cSJaegeuk Kim 	FI_ACL_MODE,		/* indicate acl mode */
230639a53e0cSJaegeuk Kim 	FI_NO_ALLOC,		/* should not allocate any blocks */
2307c9b63bd0SJaegeuk Kim 	FI_FREE_NID,		/* free allocated nide */
2308c11abd1aSJaegeuk Kim 	FI_NO_EXTENT,		/* not to use the extent cache */
2309444c580fSJaegeuk Kim 	FI_INLINE_XATTR,	/* used for inline xattr */
23101001b347SHuajun Li 	FI_INLINE_DATA,		/* used for inline data*/
231134d67debSChao Yu 	FI_INLINE_DENTRY,	/* used for inline dentry */
2312fff04f90SJaegeuk Kim 	FI_APPEND_WRITE,	/* inode has appended data */
2313fff04f90SJaegeuk Kim 	FI_UPDATE_WRITE,	/* inode has in-place-update data */
231488b88a66SJaegeuk Kim 	FI_NEED_IPU,		/* used for ipu per file */
231588b88a66SJaegeuk Kim 	FI_ATOMIC_FILE,		/* indicate atomic file */
23165fe45743SChao Yu 	FI_ATOMIC_COMMIT,	/* indicate the state of atomical committing */
231702a1335fSJaegeuk Kim 	FI_VOLATILE_FILE,	/* indicate volatile file */
23183c6c2bebSJaegeuk Kim 	FI_FIRST_BLOCK_WRITTEN,	/* indicate #0 data block was written */
23191e84371fSJaegeuk Kim 	FI_DROP_CACHE,		/* drop dirty page cache */
2320b3d208f9SJaegeuk Kim 	FI_DATA_EXIST,		/* indicate data exists */
2321510022a8SJaegeuk Kim 	FI_INLINE_DOTS,		/* indicate inline dot dentries */
2322d323d005SChao Yu 	FI_DO_DEFRAG,		/* indicate defragment is running */
2323c227f912SChao Yu 	FI_DIRTY_FILE,		/* indicate regular/symlink has dirty pages */
2324dc91de78SJaegeuk Kim 	FI_NO_PREALLOC,		/* indicate skipped preallocated blocks */
2325ef095d19SJaegeuk Kim 	FI_HOT_DATA,		/* indicate file is hot */
23267a2af766SChao Yu 	FI_EXTRA_ATTR,		/* indicate file has extra attribute */
23275c57132eSChao Yu 	FI_PROJ_INHERIT,	/* indicate file inherits projectid */
23281ad71a27SJaegeuk Kim 	FI_PIN_FILE,		/* indicate file should not be gced */
23292ef79ecbSChao Yu 	FI_ATOMIC_REVOKE_REQUEST, /* request to drop atomic data */
233039a53e0cSJaegeuk Kim };
233139a53e0cSJaegeuk Kim 
2332205b9822SJaegeuk Kim static inline void __mark_inode_dirty_flag(struct inode *inode,
2333205b9822SJaegeuk Kim 						int flag, bool set)
233439a53e0cSJaegeuk Kim {
2335205b9822SJaegeuk Kim 	switch (flag) {
2336205b9822SJaegeuk Kim 	case FI_INLINE_XATTR:
2337205b9822SJaegeuk Kim 	case FI_INLINE_DATA:
2338205b9822SJaegeuk Kim 	case FI_INLINE_DENTRY:
23399ac1e2d8SDaeho Jeong 	case FI_NEW_INODE:
2340205b9822SJaegeuk Kim 		if (set)
2341205b9822SJaegeuk Kim 			return;
2342205b9822SJaegeuk Kim 	case FI_DATA_EXIST:
2343205b9822SJaegeuk Kim 	case FI_INLINE_DOTS:
23441ad71a27SJaegeuk Kim 	case FI_PIN_FILE:
23457c45729aSJaegeuk Kim 		f2fs_mark_inode_dirty_sync(inode, true);
2346205b9822SJaegeuk Kim 	}
234739a53e0cSJaegeuk Kim }
234839a53e0cSJaegeuk Kim 
234991942321SJaegeuk Kim static inline void set_inode_flag(struct inode *inode, int flag)
235039a53e0cSJaegeuk Kim {
235191942321SJaegeuk Kim 	if (!test_bit(flag, &F2FS_I(inode)->flags))
235291942321SJaegeuk Kim 		set_bit(flag, &F2FS_I(inode)->flags);
2353205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, true);
235439a53e0cSJaegeuk Kim }
235539a53e0cSJaegeuk Kim 
235691942321SJaegeuk Kim static inline int is_inode_flag_set(struct inode *inode, int flag)
235739a53e0cSJaegeuk Kim {
235891942321SJaegeuk Kim 	return test_bit(flag, &F2FS_I(inode)->flags);
235939a53e0cSJaegeuk Kim }
236039a53e0cSJaegeuk Kim 
236191942321SJaegeuk Kim static inline void clear_inode_flag(struct inode *inode, int flag)
236239a53e0cSJaegeuk Kim {
236391942321SJaegeuk Kim 	if (test_bit(flag, &F2FS_I(inode)->flags))
236491942321SJaegeuk Kim 		clear_bit(flag, &F2FS_I(inode)->flags);
2365205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, false);
236639a53e0cSJaegeuk Kim }
236739a53e0cSJaegeuk Kim 
236891942321SJaegeuk Kim static inline void set_acl_inode(struct inode *inode, umode_t mode)
2369444c580fSJaegeuk Kim {
237091942321SJaegeuk Kim 	F2FS_I(inode)->i_acl_mode = mode;
237191942321SJaegeuk Kim 	set_inode_flag(inode, FI_ACL_MODE);
23727c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, false);
237339a53e0cSJaegeuk Kim }
237439a53e0cSJaegeuk Kim 
2375a1961246SJaegeuk Kim static inline void f2fs_i_links_write(struct inode *inode, bool inc)
2376a1961246SJaegeuk Kim {
2377a1961246SJaegeuk Kim 	if (inc)
2378a1961246SJaegeuk Kim 		inc_nlink(inode);
2379a1961246SJaegeuk Kim 	else
2380a1961246SJaegeuk Kim 		drop_nlink(inode);
23817c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2382a1961246SJaegeuk Kim }
2383a1961246SJaegeuk Kim 
23848edd03c8SJaegeuk Kim static inline void f2fs_i_blocks_write(struct inode *inode,
23850abd675eSChao Yu 					block_t diff, bool add, bool claim)
23868edd03c8SJaegeuk Kim {
238726de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
238826de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
238926de9b11SJaegeuk Kim 
23900abd675eSChao Yu 	/* add = 1, claim = 1 should be dquot_reserve_block in pair */
23910abd675eSChao Yu 	if (add) {
23920abd675eSChao Yu 		if (claim)
23930abd675eSChao Yu 			dquot_claim_block(inode, diff);
23940abd675eSChao Yu 		else
23950abd675eSChao Yu 			dquot_alloc_block_nofail(inode, diff);
23960abd675eSChao Yu 	} else {
23970abd675eSChao Yu 		dquot_free_block(inode, diff);
23980abd675eSChao Yu 	}
23990abd675eSChao Yu 
24007c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
240126de9b11SJaegeuk Kim 	if (clean || recover)
240226de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
24038edd03c8SJaegeuk Kim }
24048edd03c8SJaegeuk Kim 
2405fc9581c8SJaegeuk Kim static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
2406fc9581c8SJaegeuk Kim {
240726de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
240826de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
240926de9b11SJaegeuk Kim 
2410fc9581c8SJaegeuk Kim 	if (i_size_read(inode) == i_size)
2411fc9581c8SJaegeuk Kim 		return;
2412fc9581c8SJaegeuk Kim 
2413fc9581c8SJaegeuk Kim 	i_size_write(inode, i_size);
24147c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
241526de9b11SJaegeuk Kim 	if (clean || recover)
241626de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
241726de9b11SJaegeuk Kim }
241826de9b11SJaegeuk Kim 
2419205b9822SJaegeuk Kim static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
2420205b9822SJaegeuk Kim {
2421205b9822SJaegeuk Kim 	F2FS_I(inode)->i_current_depth = depth;
24227c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2423205b9822SJaegeuk Kim }
2424205b9822SJaegeuk Kim 
24251ad71a27SJaegeuk Kim static inline void f2fs_i_gc_failures_write(struct inode *inode,
24261ad71a27SJaegeuk Kim 					unsigned int count)
24271ad71a27SJaegeuk Kim {
24282ef79ecbSChao Yu 	F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN] = count;
24291ad71a27SJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
24301ad71a27SJaegeuk Kim }
24311ad71a27SJaegeuk Kim 
2432205b9822SJaegeuk Kim static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
2433205b9822SJaegeuk Kim {
2434205b9822SJaegeuk Kim 	F2FS_I(inode)->i_xattr_nid = xnid;
24357c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2436205b9822SJaegeuk Kim }
2437205b9822SJaegeuk Kim 
2438205b9822SJaegeuk Kim static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
2439205b9822SJaegeuk Kim {
2440205b9822SJaegeuk Kim 	F2FS_I(inode)->i_pino = pino;
24417c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2442205b9822SJaegeuk Kim }
2443205b9822SJaegeuk Kim 
244491942321SJaegeuk Kim static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
2445444c580fSJaegeuk Kim {
2446205b9822SJaegeuk Kim 	struct f2fs_inode_info *fi = F2FS_I(inode);
2447205b9822SJaegeuk Kim 
2448444c580fSJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_XATTR)
2449205b9822SJaegeuk Kim 		set_bit(FI_INLINE_XATTR, &fi->flags);
24501001b347SHuajun Li 	if (ri->i_inline & F2FS_INLINE_DATA)
2451205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DATA, &fi->flags);
245234d67debSChao Yu 	if (ri->i_inline & F2FS_INLINE_DENTRY)
2453205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DENTRY, &fi->flags);
2454b3d208f9SJaegeuk Kim 	if (ri->i_inline & F2FS_DATA_EXIST)
2455205b9822SJaegeuk Kim 		set_bit(FI_DATA_EXIST, &fi->flags);
2456510022a8SJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_DOTS)
2457205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DOTS, &fi->flags);
24587a2af766SChao Yu 	if (ri->i_inline & F2FS_EXTRA_ATTR)
24597a2af766SChao Yu 		set_bit(FI_EXTRA_ATTR, &fi->flags);
24601ad71a27SJaegeuk Kim 	if (ri->i_inline & F2FS_PIN_FILE)
24611ad71a27SJaegeuk Kim 		set_bit(FI_PIN_FILE, &fi->flags);
2462444c580fSJaegeuk Kim }
2463444c580fSJaegeuk Kim 
246491942321SJaegeuk Kim static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
2465444c580fSJaegeuk Kim {
2466444c580fSJaegeuk Kim 	ri->i_inline = 0;
2467444c580fSJaegeuk Kim 
246891942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_XATTR))
2469444c580fSJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_XATTR;
247091942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DATA))
24711001b347SHuajun Li 		ri->i_inline |= F2FS_INLINE_DATA;
247291942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
247334d67debSChao Yu 		ri->i_inline |= F2FS_INLINE_DENTRY;
247491942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_DATA_EXIST))
2475b3d208f9SJaegeuk Kim 		ri->i_inline |= F2FS_DATA_EXIST;
247691942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DOTS))
2477510022a8SJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_DOTS;
24787a2af766SChao Yu 	if (is_inode_flag_set(inode, FI_EXTRA_ATTR))
24797a2af766SChao Yu 		ri->i_inline |= F2FS_EXTRA_ATTR;
24801ad71a27SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_PIN_FILE))
24811ad71a27SJaegeuk Kim 		ri->i_inline |= F2FS_PIN_FILE;
24827a2af766SChao Yu }
24837a2af766SChao Yu 
24847a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode)
24857a2af766SChao Yu {
24867a2af766SChao Yu 	return is_inode_flag_set(inode, FI_EXTRA_ATTR);
2487444c580fSJaegeuk Kim }
2488444c580fSJaegeuk Kim 
2489987c7c31SChao Yu static inline int f2fs_has_inline_xattr(struct inode *inode)
2490987c7c31SChao Yu {
249191942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_XATTR);
2492987c7c31SChao Yu }
2493987c7c31SChao Yu 
249481ca7350SChao Yu static inline unsigned int addrs_per_inode(struct inode *inode)
2495de93653fSJaegeuk Kim {
2496b323fd28SChao Yu 	return CUR_ADDRS_PER_INODE(inode) - get_inline_xattr_addrs(inode);
2497de93653fSJaegeuk Kim }
2498de93653fSJaegeuk Kim 
24996afc662eSChao Yu static inline void *inline_xattr_addr(struct inode *inode, struct page *page)
250065985d93SJaegeuk Kim {
2501695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
2502cac5a3d8SDongOh Shin 
250365985d93SJaegeuk Kim 	return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
2504b323fd28SChao Yu 					get_inline_xattr_addrs(inode)]);
250565985d93SJaegeuk Kim }
250665985d93SJaegeuk Kim 
250765985d93SJaegeuk Kim static inline int inline_xattr_size(struct inode *inode)
250865985d93SJaegeuk Kim {
25096afc662eSChao Yu 	return get_inline_xattr_addrs(inode) * sizeof(__le32);
251065985d93SJaegeuk Kim }
251165985d93SJaegeuk Kim 
25120dbdc2aeSJaegeuk Kim static inline int f2fs_has_inline_data(struct inode *inode)
25130dbdc2aeSJaegeuk Kim {
251491942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DATA);
25150dbdc2aeSJaegeuk Kim }
25160dbdc2aeSJaegeuk Kim 
2517b3d208f9SJaegeuk Kim static inline int f2fs_exist_data(struct inode *inode)
2518b3d208f9SJaegeuk Kim {
251991942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DATA_EXIST);
2520b3d208f9SJaegeuk Kim }
2521b3d208f9SJaegeuk Kim 
2522510022a8SJaegeuk Kim static inline int f2fs_has_inline_dots(struct inode *inode)
2523510022a8SJaegeuk Kim {
252491942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DOTS);
2525510022a8SJaegeuk Kim }
2526510022a8SJaegeuk Kim 
25271ad71a27SJaegeuk Kim static inline bool f2fs_is_pinned_file(struct inode *inode)
25281ad71a27SJaegeuk Kim {
25291ad71a27SJaegeuk Kim 	return is_inode_flag_set(inode, FI_PIN_FILE);
25301ad71a27SJaegeuk Kim }
25311ad71a27SJaegeuk Kim 
253288b88a66SJaegeuk Kim static inline bool f2fs_is_atomic_file(struct inode *inode)
253388b88a66SJaegeuk Kim {
253491942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_ATOMIC_FILE);
253588b88a66SJaegeuk Kim }
253688b88a66SJaegeuk Kim 
25375fe45743SChao Yu static inline bool f2fs_is_commit_atomic_write(struct inode *inode)
25385fe45743SChao Yu {
25395fe45743SChao Yu 	return is_inode_flag_set(inode, FI_ATOMIC_COMMIT);
25405fe45743SChao Yu }
25415fe45743SChao Yu 
254202a1335fSJaegeuk Kim static inline bool f2fs_is_volatile_file(struct inode *inode)
254302a1335fSJaegeuk Kim {
254491942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_VOLATILE_FILE);
254502a1335fSJaegeuk Kim }
254602a1335fSJaegeuk Kim 
25473c6c2bebSJaegeuk Kim static inline bool f2fs_is_first_block_written(struct inode *inode)
25483c6c2bebSJaegeuk Kim {
254991942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
25503c6c2bebSJaegeuk Kim }
25513c6c2bebSJaegeuk Kim 
25521e84371fSJaegeuk Kim static inline bool f2fs_is_drop_cache(struct inode *inode)
25531e84371fSJaegeuk Kim {
255491942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DROP_CACHE);
25551e84371fSJaegeuk Kim }
25561e84371fSJaegeuk Kim 
2557f2470371SChao Yu static inline void *inline_data_addr(struct inode *inode, struct page *page)
25581001b347SHuajun Li {
2559695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
25607a2af766SChao Yu 	int extra_size = get_extra_isize(inode);
2561cac5a3d8SDongOh Shin 
25627a2af766SChao Yu 	return (void *)&(ri->i_addr[extra_size + DEF_INLINE_RESERVED_SIZE]);
25631001b347SHuajun Li }
25641001b347SHuajun Li 
256534d67debSChao Yu static inline int f2fs_has_inline_dentry(struct inode *inode)
256634d67debSChao Yu {
256791942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DENTRY);
256834d67debSChao Yu }
256934d67debSChao Yu 
2570b5492af7SJaegeuk Kim static inline int is_file(struct inode *inode, int type)
2571b5492af7SJaegeuk Kim {
2572b5492af7SJaegeuk Kim 	return F2FS_I(inode)->i_advise & type;
2573b5492af7SJaegeuk Kim }
2574b5492af7SJaegeuk Kim 
2575b5492af7SJaegeuk Kim static inline void set_file(struct inode *inode, int type)
2576b5492af7SJaegeuk Kim {
2577b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise |= type;
25787c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2579b5492af7SJaegeuk Kim }
2580b5492af7SJaegeuk Kim 
2581b5492af7SJaegeuk Kim static inline void clear_file(struct inode *inode, int type)
2582b5492af7SJaegeuk Kim {
2583b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise &= ~type;
25847c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2585b5492af7SJaegeuk Kim }
2586b5492af7SJaegeuk Kim 
258726787236SJaegeuk Kim static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
258826787236SJaegeuk Kim {
2589a0d00fadSChao Yu 	bool ret;
2590a0d00fadSChao Yu 
259126787236SJaegeuk Kim 	if (dsync) {
259226787236SJaegeuk Kim 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
259326787236SJaegeuk Kim 
259426787236SJaegeuk Kim 		spin_lock(&sbi->inode_lock[DIRTY_META]);
259526787236SJaegeuk Kim 		ret = list_empty(&F2FS_I(inode)->gdirty_list);
259626787236SJaegeuk Kim 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
259726787236SJaegeuk Kim 		return ret;
259826787236SJaegeuk Kim 	}
259926787236SJaegeuk Kim 	if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
260026787236SJaegeuk Kim 			file_keep_isize(inode) ||
2601235831d7SJunling Zheng 			i_size_read(inode) & ~PAGE_MASK)
260226787236SJaegeuk Kim 		return false;
2603a0d00fadSChao Yu 
260424b81dfcSArnd Bergmann 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time, &inode->i_atime))
2605214c2461SJaegeuk Kim 		return false;
260624b81dfcSArnd Bergmann 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 1, &inode->i_ctime))
2607214c2461SJaegeuk Kim 		return false;
260824b81dfcSArnd Bergmann 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 2, &inode->i_mtime))
2609214c2461SJaegeuk Kim 		return false;
261024b81dfcSArnd Bergmann 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 3,
2611214c2461SJaegeuk Kim 						&F2FS_I(inode)->i_crtime))
2612214c2461SJaegeuk Kim 		return false;
2613214c2461SJaegeuk Kim 
2614a0d00fadSChao Yu 	down_read(&F2FS_I(inode)->i_sem);
2615a0d00fadSChao Yu 	ret = F2FS_I(inode)->last_disk_size == i_size_read(inode);
2616a0d00fadSChao Yu 	up_read(&F2FS_I(inode)->i_sem);
2617a0d00fadSChao Yu 
2618a0d00fadSChao Yu 	return ret;
2619267378d4SChao Yu }
2620267378d4SChao Yu 
2621deeedd71SChao Yu static inline bool f2fs_readonly(struct super_block *sb)
262277888c1eSJaegeuk Kim {
2623deeedd71SChao Yu 	return sb_rdonly(sb);
262477888c1eSJaegeuk Kim }
262577888c1eSJaegeuk Kim 
2626744602cfSJaegeuk Kim static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
2627744602cfSJaegeuk Kim {
2628aaec2b1dSChao Yu 	return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
2629744602cfSJaegeuk Kim }
2630744602cfSJaegeuk Kim 
2631eaa693f4SJaegeuk Kim static inline bool is_dot_dotdot(const struct qstr *str)
2632eaa693f4SJaegeuk Kim {
2633eaa693f4SJaegeuk Kim 	if (str->len == 1 && str->name[0] == '.')
2634eaa693f4SJaegeuk Kim 		return true;
2635eaa693f4SJaegeuk Kim 
2636eaa693f4SJaegeuk Kim 	if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
2637eaa693f4SJaegeuk Kim 		return true;
2638eaa693f4SJaegeuk Kim 
2639eaa693f4SJaegeuk Kim 	return false;
2640eaa693f4SJaegeuk Kim }
2641eaa693f4SJaegeuk Kim 
26423e72f721SJaegeuk Kim static inline bool f2fs_may_extent_tree(struct inode *inode)
26433e72f721SJaegeuk Kim {
26443e72f721SJaegeuk Kim 	if (!test_opt(F2FS_I_SB(inode), EXTENT_CACHE) ||
264591942321SJaegeuk Kim 			is_inode_flag_set(inode, FI_NO_EXTENT))
26463e72f721SJaegeuk Kim 		return false;
26473e72f721SJaegeuk Kim 
2648886f56f9SAl Viro 	return S_ISREG(inode->i_mode);
26493e72f721SJaegeuk Kim }
26503e72f721SJaegeuk Kim 
26511ecc0c5cSChao Yu static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
26521ecc0c5cSChao Yu 					size_t size, gfp_t flags)
26530414b004SJaegeuk Kim {
265455523519SChao Yu 	if (time_to_inject(sbi, FAULT_KMALLOC)) {
265555523519SChao Yu 		f2fs_show_injection_info(FAULT_KMALLOC);
26562c63feadSJaegeuk Kim 		return NULL;
265755523519SChao Yu 	}
26587fa750a1SArnd Bergmann 
26590414b004SJaegeuk Kim 	return kmalloc(size, flags);
26600414b004SJaegeuk Kim }
26610414b004SJaegeuk Kim 
2662acbf054dSChao Yu static inline void *f2fs_kzalloc(struct f2fs_sb_info *sbi,
2663acbf054dSChao Yu 					size_t size, gfp_t flags)
2664acbf054dSChao Yu {
2665acbf054dSChao Yu 	return f2fs_kmalloc(sbi, size, flags | __GFP_ZERO);
2666acbf054dSChao Yu }
2667acbf054dSChao Yu 
2668628b3d14SChao Yu static inline void *f2fs_kvmalloc(struct f2fs_sb_info *sbi,
2669628b3d14SChao Yu 					size_t size, gfp_t flags)
2670628b3d14SChao Yu {
2671628b3d14SChao Yu 	if (time_to_inject(sbi, FAULT_KVMALLOC)) {
2672628b3d14SChao Yu 		f2fs_show_injection_info(FAULT_KVMALLOC);
2673628b3d14SChao Yu 		return NULL;
2674628b3d14SChao Yu 	}
26757fa750a1SArnd Bergmann 
2676628b3d14SChao Yu 	return kvmalloc(size, flags);
2677628b3d14SChao Yu }
2678628b3d14SChao Yu 
2679628b3d14SChao Yu static inline void *f2fs_kvzalloc(struct f2fs_sb_info *sbi,
2680628b3d14SChao Yu 					size_t size, gfp_t flags)
2681628b3d14SChao Yu {
2682628b3d14SChao Yu 	return f2fs_kvmalloc(sbi, size, flags | __GFP_ZERO);
2683628b3d14SChao Yu }
2684628b3d14SChao Yu 
26857a2af766SChao Yu static inline int get_extra_isize(struct inode *inode)
2686f2470371SChao Yu {
26877a2af766SChao Yu 	return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
2688f2470371SChao Yu }
2689f2470371SChao Yu 
26906afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode)
26916afc662eSChao Yu {
26926afc662eSChao Yu 	return F2FS_I(inode)->i_inline_xattr_size;
26936afc662eSChao Yu }
26946afc662eSChao Yu 
26954d57b86dSChao Yu #define f2fs_get_inode_mode(i) \
269691942321SJaegeuk Kim 	((is_inode_flag_set(i, FI_ACL_MODE)) ? \
2697a6dda0e6SChristoph Hellwig 	 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
2698a6dda0e6SChristoph Hellwig 
26997a2af766SChao Yu #define F2FS_TOTAL_EXTRA_ATTR_SIZE			\
27007a2af766SChao Yu 	(offsetof(struct f2fs_inode, i_extra_end) -	\
27017a2af766SChao Yu 	offsetof(struct f2fs_inode, i_extra_isize))	\
27027a2af766SChao Yu 
27035c57132eSChao Yu #define F2FS_OLD_ATTRIBUTE_SIZE	(offsetof(struct f2fs_inode, i_addr))
27045c57132eSChao Yu #define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field)		\
27055c57132eSChao Yu 		((offsetof(typeof(*f2fs_inode), field) +	\
27065c57132eSChao Yu 		sizeof((f2fs_inode)->field))			\
27075c57132eSChao Yu 		<= (F2FS_OLD_ATTRIBUTE_SIZE + extra_isize))	\
27085c57132eSChao Yu 
2709b0af6d49SChao Yu static inline void f2fs_reset_iostat(struct f2fs_sb_info *sbi)
2710b0af6d49SChao Yu {
2711b0af6d49SChao Yu 	int i;
2712b0af6d49SChao Yu 
2713b0af6d49SChao Yu 	spin_lock(&sbi->iostat_lock);
2714b0af6d49SChao Yu 	for (i = 0; i < NR_IO_TYPE; i++)
2715b0af6d49SChao Yu 		sbi->write_iostat[i] = 0;
2716b0af6d49SChao Yu 	spin_unlock(&sbi->iostat_lock);
2717b0af6d49SChao Yu }
2718b0af6d49SChao Yu 
2719b0af6d49SChao Yu static inline void f2fs_update_iostat(struct f2fs_sb_info *sbi,
2720b0af6d49SChao Yu 			enum iostat_type type, unsigned long long io_bytes)
2721b0af6d49SChao Yu {
2722b0af6d49SChao Yu 	if (!sbi->iostat_enable)
2723b0af6d49SChao Yu 		return;
2724b0af6d49SChao Yu 	spin_lock(&sbi->iostat_lock);
2725b0af6d49SChao Yu 	sbi->write_iostat[type] += io_bytes;
2726b0af6d49SChao Yu 
2727b0af6d49SChao Yu 	if (type == APP_WRITE_IO || type == APP_DIRECT_IO)
2728b0af6d49SChao Yu 		sbi->write_iostat[APP_BUFFERED_IO] =
2729b0af6d49SChao Yu 			sbi->write_iostat[APP_WRITE_IO] -
2730b0af6d49SChao Yu 			sbi->write_iostat[APP_DIRECT_IO];
2731b0af6d49SChao Yu 	spin_unlock(&sbi->iostat_lock);
2732b0af6d49SChao Yu }
2733b0af6d49SChao Yu 
2734c9b60788SChao Yu #define __is_meta_io(fio) (PAGE_TYPE_OF_BIO(fio->type) == META &&	\
2735c9b60788SChao Yu 				(!is_read_io(fio->op) || fio->is_meta))
2736c9b60788SChao Yu 
2737e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
2738e1da7872SChao Yu 					block_t blkaddr, int type);
2739e1da7872SChao Yu void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...);
2740e1da7872SChao Yu static inline void verify_blkaddr(struct f2fs_sb_info *sbi,
2741e1da7872SChao Yu 					block_t blkaddr, int type)
2742e1da7872SChao Yu {
2743e1da7872SChao Yu 	if (!f2fs_is_valid_blkaddr(sbi, blkaddr, type)) {
2744e1da7872SChao Yu 		f2fs_msg(sbi->sb, KERN_ERR,
2745e1da7872SChao Yu 			"invalid blkaddr: %u, type: %d, run fsck to fix.",
2746e1da7872SChao Yu 			blkaddr, type);
2747e1da7872SChao Yu 		f2fs_bug_on(sbi, 1);
2748e1da7872SChao Yu 	}
2749e1da7872SChao Yu }
2750e1da7872SChao Yu 
2751e1da7872SChao Yu static inline bool __is_valid_data_blkaddr(block_t blkaddr)
27527b525dd0SChao Yu {
27537b525dd0SChao Yu 	if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
27547b525dd0SChao Yu 		return false;
27557b525dd0SChao Yu 	return true;
27567b525dd0SChao Yu }
27577b525dd0SChao Yu 
2758e1da7872SChao Yu static inline bool is_valid_data_blkaddr(struct f2fs_sb_info *sbi,
2759e1da7872SChao Yu 						block_t blkaddr)
2760e1da7872SChao Yu {
2761e1da7872SChao Yu 	if (!__is_valid_data_blkaddr(blkaddr))
2762e1da7872SChao Yu 		return false;
2763e1da7872SChao Yu 	verify_blkaddr(sbi, blkaddr, DATA_GENERIC);
2764e1da7872SChao Yu 	return true;
2765e1da7872SChao Yu }
2766e1da7872SChao Yu 
276739a53e0cSJaegeuk Kim /*
276839a53e0cSJaegeuk Kim  * file.c
276939a53e0cSJaegeuk Kim  */
2770cac5a3d8SDongOh Shin int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
27714d57b86dSChao Yu void f2fs_truncate_data_blocks(struct dnode_of_data *dn);
27724d57b86dSChao Yu int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock);
2773cac5a3d8SDongOh Shin int f2fs_truncate(struct inode *inode);
2774a528d35eSDavid Howells int f2fs_getattr(const struct path *path, struct kstat *stat,
2775a528d35eSDavid Howells 			u32 request_mask, unsigned int flags);
2776cac5a3d8SDongOh Shin int f2fs_setattr(struct dentry *dentry, struct iattr *attr);
27774d57b86dSChao Yu int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
27784d57b86dSChao Yu void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count);
2779c4020b2dSChao Yu int f2fs_precache_extents(struct inode *inode);
2780cac5a3d8SDongOh Shin long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
2781cac5a3d8SDongOh Shin long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
27821ad71a27SJaegeuk Kim int f2fs_pin_file_control(struct inode *inode, bool inc);
278339a53e0cSJaegeuk Kim 
278439a53e0cSJaegeuk Kim /*
278539a53e0cSJaegeuk Kim  * inode.c
278639a53e0cSJaegeuk Kim  */
2787cac5a3d8SDongOh Shin void f2fs_set_inode_flags(struct inode *inode);
2788704956ecSChao Yu bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page);
2789704956ecSChao Yu void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page);
2790cac5a3d8SDongOh Shin struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
2791cac5a3d8SDongOh Shin struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
27924d57b86dSChao Yu int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
27934d57b86dSChao Yu void f2fs_update_inode(struct inode *inode, struct page *node_page);
27944d57b86dSChao Yu void f2fs_update_inode_page(struct inode *inode);
2795cac5a3d8SDongOh Shin int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
2796cac5a3d8SDongOh Shin void f2fs_evict_inode(struct inode *inode);
27974d57b86dSChao Yu void f2fs_handle_failed_inode(struct inode *inode);
279839a53e0cSJaegeuk Kim 
279939a53e0cSJaegeuk Kim /*
280039a53e0cSJaegeuk Kim  * namei.c
280139a53e0cSJaegeuk Kim  */
28024d57b86dSChao Yu int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
2803b6a06cbbSChao Yu 							bool hot, bool set);
280439a53e0cSJaegeuk Kim struct dentry *f2fs_get_parent(struct dentry *child);
280539a53e0cSJaegeuk Kim 
280639a53e0cSJaegeuk Kim /*
280739a53e0cSJaegeuk Kim  * dir.c
280839a53e0cSJaegeuk Kim  */
28094d57b86dSChao Yu unsigned char f2fs_get_de_type(struct f2fs_dir_entry *de);
28104d57b86dSChao Yu struct f2fs_dir_entry *f2fs_find_target_dentry(struct fscrypt_name *fname,
2811cac5a3d8SDongOh Shin 			f2fs_hash_t namehash, int *max_slots,
2812cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
2813cac5a3d8SDongOh Shin int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
2814cac5a3d8SDongOh Shin 			unsigned int start_pos, struct fscrypt_str *fstr);
28154d57b86dSChao Yu void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
2816cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
28174d57b86dSChao Yu struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
2818cac5a3d8SDongOh Shin 			const struct qstr *new_name,
2819cac5a3d8SDongOh Shin 			const struct qstr *orig_name, struct page *dpage);
28204d57b86dSChao Yu void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
2821cac5a3d8SDongOh Shin 			unsigned int current_depth);
28224d57b86dSChao Yu int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots);
2823cac5a3d8SDongOh Shin void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
2824cac5a3d8SDongOh Shin struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
2825cac5a3d8SDongOh Shin 			struct fscrypt_name *fname, struct page **res_page);
2826cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
2827cac5a3d8SDongOh Shin 			const struct qstr *child, struct page **res_page);
2828cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
2829cac5a3d8SDongOh Shin ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
2830cac5a3d8SDongOh Shin 			struct page **page);
2831cac5a3d8SDongOh Shin void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
2832cac5a3d8SDongOh Shin 			struct page *page, struct inode *inode);
2833cac5a3d8SDongOh Shin void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
2834cac5a3d8SDongOh Shin 			const struct qstr *name, f2fs_hash_t name_hash,
2835cac5a3d8SDongOh Shin 			unsigned int bit_pos);
2836cac5a3d8SDongOh Shin int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
2837cac5a3d8SDongOh Shin 			const struct qstr *orig_name,
2838cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
28394d57b86dSChao Yu int f2fs_add_dentry(struct inode *dir, struct fscrypt_name *fname,
2840cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
28414d57b86dSChao Yu int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
2842cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
2843cac5a3d8SDongOh Shin void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
2844cac5a3d8SDongOh Shin 			struct inode *dir, struct inode *inode);
2845cac5a3d8SDongOh Shin int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
2846cac5a3d8SDongOh Shin bool f2fs_empty_dir(struct inode *dir);
284739a53e0cSJaegeuk Kim 
2848b7f7a5e0SAl Viro static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
2849b7f7a5e0SAl Viro {
28504d57b86dSChao Yu 	return f2fs_do_add_link(d_inode(dentry->d_parent), &dentry->d_name,
2851510022a8SJaegeuk Kim 				inode, inode->i_ino, inode->i_mode);
2852b7f7a5e0SAl Viro }
2853b7f7a5e0SAl Viro 
285439a53e0cSJaegeuk Kim /*
285539a53e0cSJaegeuk Kim  * super.c
285639a53e0cSJaegeuk Kim  */
2857cac5a3d8SDongOh Shin int f2fs_inode_dirtied(struct inode *inode, bool sync);
2858cac5a3d8SDongOh Shin void f2fs_inode_synced(struct inode *inode);
2859ea676733SJaegeuk Kim int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly);
28604b2414d0SChao Yu void f2fs_quota_off_umount(struct super_block *sb);
2861cac5a3d8SDongOh Shin int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
2862cac5a3d8SDongOh Shin int f2fs_sync_fs(struct super_block *sb, int sync);
2863a07ef784SNamjae Jeon extern __printf(3, 4)
2864cac5a3d8SDongOh Shin void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...);
28654d57b86dSChao Yu int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi);
286639a53e0cSJaegeuk Kim 
286739a53e0cSJaegeuk Kim /*
286839a53e0cSJaegeuk Kim  * hash.c
286939a53e0cSJaegeuk Kim  */
28706332cd32SJaegeuk Kim f2fs_hash_t f2fs_dentry_hash(const struct qstr *name_info,
28716332cd32SJaegeuk Kim 				struct fscrypt_name *fname);
287239a53e0cSJaegeuk Kim 
287339a53e0cSJaegeuk Kim /*
287439a53e0cSJaegeuk Kim  * node.c
287539a53e0cSJaegeuk Kim  */
287639a53e0cSJaegeuk Kim struct dnode_of_data;
287739a53e0cSJaegeuk Kim struct node_info;
287839a53e0cSJaegeuk Kim 
28794d57b86dSChao Yu int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid);
28804d57b86dSChao Yu bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type);
288150fa53ecSChao Yu bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct page *page);
288250fa53ecSChao Yu void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi);
288350fa53ecSChao Yu void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct page *page);
288450fa53ecSChao Yu void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi);
28854d57b86dSChao Yu int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
28864d57b86dSChao Yu bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
28874d57b86dSChao Yu bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
28887735730dSChao Yu int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
28894d57b86dSChao Yu 						struct node_info *ni);
28904d57b86dSChao Yu pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
28914d57b86dSChao Yu int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
28924d57b86dSChao Yu int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from);
28934d57b86dSChao Yu int f2fs_truncate_xattr_node(struct inode *inode);
289450fa53ecSChao Yu int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
289550fa53ecSChao Yu 					unsigned int seq_id);
28964d57b86dSChao Yu int f2fs_remove_inode_page(struct inode *inode);
28974d57b86dSChao Yu struct page *f2fs_new_inode_page(struct inode *inode);
28984d57b86dSChao Yu struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs);
28994d57b86dSChao Yu void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
29004d57b86dSChao Yu struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
29014d57b86dSChao Yu struct page *f2fs_get_node_page_ra(struct page *parent, int start);
29024d57b86dSChao Yu void f2fs_move_node_page(struct page *node_page, int gc_type);
29034d57b86dSChao Yu int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
290450fa53ecSChao Yu 			struct writeback_control *wbc, bool atomic,
290550fa53ecSChao Yu 			unsigned int *seq_id);
29064d57b86dSChao Yu int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
29074d57b86dSChao Yu 			struct writeback_control *wbc,
2908b0af6d49SChao Yu 			bool do_balance, enum iostat_type io_type);
2909e2374015SChao Yu int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
29104d57b86dSChao Yu bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
29114d57b86dSChao Yu void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
29124d57b86dSChao Yu void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
29134d57b86dSChao Yu int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
29144d57b86dSChao Yu void f2fs_recover_inline_xattr(struct inode *inode, struct page *page);
29154d57b86dSChao Yu int f2fs_recover_xattr_data(struct inode *inode, struct page *page);
29164d57b86dSChao Yu int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
29177735730dSChao Yu int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
2918cac5a3d8SDongOh Shin 			unsigned int segno, struct f2fs_summary_block *sum);
2919edc55aafSJaegeuk Kim int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
29204d57b86dSChao Yu int f2fs_build_node_manager(struct f2fs_sb_info *sbi);
29214d57b86dSChao Yu void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi);
29224d57b86dSChao Yu int __init f2fs_create_node_manager_caches(void);
29234d57b86dSChao Yu void f2fs_destroy_node_manager_caches(void);
292439a53e0cSJaegeuk Kim 
292539a53e0cSJaegeuk Kim /*
292639a53e0cSJaegeuk Kim  * segment.c
292739a53e0cSJaegeuk Kim  */
29284d57b86dSChao Yu bool f2fs_need_SSR(struct f2fs_sb_info *sbi);
29294d57b86dSChao Yu void f2fs_register_inmem_page(struct inode *inode, struct page *page);
29304d57b86dSChao Yu void f2fs_drop_inmem_pages_all(struct f2fs_sb_info *sbi, bool gc_failure);
29314d57b86dSChao Yu void f2fs_drop_inmem_pages(struct inode *inode);
29324d57b86dSChao Yu void f2fs_drop_inmem_page(struct inode *inode, struct page *page);
29334d57b86dSChao Yu int f2fs_commit_inmem_pages(struct inode *inode);
2934cac5a3d8SDongOh Shin void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
2935cac5a3d8SDongOh Shin void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi);
293639d787beSChao Yu int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
29374d57b86dSChao Yu int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
29381228b482SChao Yu int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
29394d57b86dSChao Yu void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
29404d57b86dSChao Yu void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
29414d57b86dSChao Yu bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
29424d57b86dSChao Yu void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
29434d57b86dSChao Yu void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi);
2944cf5c759fSChao Yu bool f2fs_wait_discard_bios(struct f2fs_sb_info *sbi);
29454d57b86dSChao Yu void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
29464d57b86dSChao Yu 					struct cp_control *cpc);
29474d57b86dSChao Yu void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi);
29484d57b86dSChao Yu int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
29494d57b86dSChao Yu void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi);
2950cac5a3d8SDongOh Shin int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
29514d57b86dSChao Yu bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
29524d57b86dSChao Yu 					struct cp_control *cpc);
29534d57b86dSChao Yu struct page *f2fs_get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
29544d57b86dSChao Yu void f2fs_update_meta_page(struct f2fs_sb_info *sbi, void *src,
29554d57b86dSChao Yu 					block_t blk_addr);
29564d57b86dSChao Yu void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
2957b0af6d49SChao Yu 						enum iostat_type io_type);
29584d57b86dSChao Yu void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio);
29594d57b86dSChao Yu void f2fs_outplace_write_data(struct dnode_of_data *dn,
29604d57b86dSChao Yu 			struct f2fs_io_info *fio);
29614d57b86dSChao Yu int f2fs_inplace_write_data(struct f2fs_io_info *fio);
29624d57b86dSChao Yu void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
2963cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t new_blkaddr,
2964cac5a3d8SDongOh Shin 			bool recover_curseg, bool recover_newaddr);
2965cac5a3d8SDongOh Shin void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
2966cac5a3d8SDongOh Shin 			block_t old_addr, block_t new_addr,
2967cac5a3d8SDongOh Shin 			unsigned char version, bool recover_curseg,
2968cac5a3d8SDongOh Shin 			bool recover_newaddr);
29694d57b86dSChao Yu void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
2970cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t *new_blkaddr,
2971fb830fc5SChao Yu 			struct f2fs_summary *sum, int type,
2972fb830fc5SChao Yu 			struct f2fs_io_info *fio, bool add_list);
2973cac5a3d8SDongOh Shin void f2fs_wait_on_page_writeback(struct page *page,
2974cac5a3d8SDongOh Shin 			enum page_type type, bool ordered);
29750ded69f6SJaegeuk Kim void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr);
29764d57b86dSChao Yu void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
29774d57b86dSChao Yu void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
29784d57b86dSChao Yu int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
2979cac5a3d8SDongOh Shin 			unsigned int val, int alloc);
29804d57b86dSChao Yu void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
29814d57b86dSChao Yu int f2fs_build_segment_manager(struct f2fs_sb_info *sbi);
29824d57b86dSChao Yu void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi);
29834d57b86dSChao Yu int __init f2fs_create_segment_manager_caches(void);
29844d57b86dSChao Yu void f2fs_destroy_segment_manager_caches(void);
29854d57b86dSChao Yu int f2fs_rw_hint_to_seg_type(enum rw_hint hint);
29864d57b86dSChao Yu enum rw_hint f2fs_io_type_to_rw_hint(struct f2fs_sb_info *sbi,
29874d57b86dSChao Yu 			enum page_type type, enum temp_type temp);
298839a53e0cSJaegeuk Kim 
298939a53e0cSJaegeuk Kim /*
299039a53e0cSJaegeuk Kim  * checkpoint.c
299139a53e0cSJaegeuk Kim  */
2992cac5a3d8SDongOh Shin void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io);
29934d57b86dSChao Yu struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
29944d57b86dSChao Yu struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
29957735730dSChao Yu struct page *f2fs_get_meta_page_nofail(struct f2fs_sb_info *sbi, pgoff_t index);
29964d57b86dSChao Yu struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
2997e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
29984d57b86dSChao Yu 					block_t blkaddr, int type);
29994d57b86dSChao Yu int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
3000cac5a3d8SDongOh Shin 			int type, bool sync);
30014d57b86dSChao Yu void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index);
30024d57b86dSChao Yu long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
3003b0af6d49SChao Yu 			long nr_to_write, enum iostat_type io_type);
30044d57b86dSChao Yu void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
30054d57b86dSChao Yu void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
30064d57b86dSChao Yu void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all);
30074d57b86dSChao Yu bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
30084d57b86dSChao Yu void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
300939d787beSChao Yu 					unsigned int devidx, int type);
30104d57b86dSChao Yu bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
301139d787beSChao Yu 					unsigned int devidx, int type);
3012cac5a3d8SDongOh Shin int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
30134d57b86dSChao Yu int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi);
30144d57b86dSChao Yu void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi);
30154d57b86dSChao Yu void f2fs_add_orphan_inode(struct inode *inode);
30164d57b86dSChao Yu void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
30174d57b86dSChao Yu int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi);
30184d57b86dSChao Yu int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi);
30194d57b86dSChao Yu void f2fs_update_dirty_page(struct inode *inode, struct page *page);
30204d57b86dSChao Yu void f2fs_remove_dirty_inode(struct inode *inode);
30214d57b86dSChao Yu int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
302250fa53ecSChao Yu void f2fs_wait_on_all_pages_writeback(struct f2fs_sb_info *sbi);
30234d57b86dSChao Yu int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
30244d57b86dSChao Yu void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi);
30254d57b86dSChao Yu int __init f2fs_create_checkpoint_caches(void);
30264d57b86dSChao Yu void f2fs_destroy_checkpoint_caches(void);
302739a53e0cSJaegeuk Kim 
302839a53e0cSJaegeuk Kim /*
302939a53e0cSJaegeuk Kim  * data.c
303039a53e0cSJaegeuk Kim  */
30316dbb1796SEric Biggers int f2fs_init_post_read_processing(void);
30326dbb1796SEric Biggers void f2fs_destroy_post_read_processing(void);
3033b9109b0eSJaegeuk Kim void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
3034b9109b0eSJaegeuk Kim void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
3035*bab475c5SChao Yu 				struct inode *inode, struct page *page,
3036*bab475c5SChao Yu 				nid_t ino, enum page_type type);
3037b9109b0eSJaegeuk Kim void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
3038cac5a3d8SDongOh Shin int f2fs_submit_page_bio(struct f2fs_io_info *fio);
3039fe16efe6SChao Yu void f2fs_submit_page_write(struct f2fs_io_info *fio);
3040cac5a3d8SDongOh Shin struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
3041cac5a3d8SDongOh Shin 			block_t blk_addr, struct bio *bio);
3042cac5a3d8SDongOh Shin int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
30434d57b86dSChao Yu void f2fs_set_data_blkaddr(struct dnode_of_data *dn);
3044cac5a3d8SDongOh Shin void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
30454d57b86dSChao Yu int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
30464d57b86dSChao Yu int f2fs_reserve_new_block(struct dnode_of_data *dn);
3047cac5a3d8SDongOh Shin int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
3048cac5a3d8SDongOh Shin int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from);
3049cac5a3d8SDongOh Shin int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
30504d57b86dSChao Yu struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
3051cac5a3d8SDongOh Shin 			int op_flags, bool for_write);
30524d57b86dSChao Yu struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index);
30534d57b86dSChao Yu struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
3054cac5a3d8SDongOh Shin 			bool for_write);
30554d57b86dSChao Yu struct page *f2fs_get_new_data_page(struct inode *inode,
3056cac5a3d8SDongOh Shin 			struct page *ipage, pgoff_t index, bool new_i_size);
30574d57b86dSChao Yu int f2fs_do_write_data_page(struct f2fs_io_info *fio);
3058cac5a3d8SDongOh Shin int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
3059cac5a3d8SDongOh Shin 			int create, int flag);
3060cac5a3d8SDongOh Shin int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3061cac5a3d8SDongOh Shin 			u64 start, u64 len);
30624d57b86dSChao Yu bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio);
30634d57b86dSChao Yu bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio);
3064cac5a3d8SDongOh Shin void f2fs_invalidate_page(struct page *page, unsigned int offset,
3065cac5a3d8SDongOh Shin 			unsigned int length);
3066cac5a3d8SDongOh Shin int f2fs_release_page(struct page *page, gfp_t wait);
30675b7a487cSWeichao Guo #ifdef CONFIG_MIGRATION
3068cac5a3d8SDongOh Shin int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,
3069cac5a3d8SDongOh Shin 			struct page *page, enum migrate_mode mode);
30705b7a487cSWeichao Guo #endif
3071b91050a8SHyunchul Lee bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len);
30724d57b86dSChao Yu void f2fs_clear_radix_tree_dirty_tag(struct page *page);
307339a53e0cSJaegeuk Kim 
307439a53e0cSJaegeuk Kim /*
307539a53e0cSJaegeuk Kim  * gc.c
307639a53e0cSJaegeuk Kim  */
30774d57b86dSChao Yu int f2fs_start_gc_thread(struct f2fs_sb_info *sbi);
30784d57b86dSChao Yu void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi);
30794d57b86dSChao Yu block_t f2fs_start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
3080e066b83cSJaegeuk Kim int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background,
3081e066b83cSJaegeuk Kim 			unsigned int segno);
30824d57b86dSChao Yu void f2fs_build_gc_manager(struct f2fs_sb_info *sbi);
308339a53e0cSJaegeuk Kim 
308439a53e0cSJaegeuk Kim /*
308539a53e0cSJaegeuk Kim  * recovery.c
308639a53e0cSJaegeuk Kim  */
30874d57b86dSChao Yu int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
30884d57b86dSChao Yu bool f2fs_space_for_roll_forward(struct f2fs_sb_info *sbi);
308939a53e0cSJaegeuk Kim 
309039a53e0cSJaegeuk Kim /*
309139a53e0cSJaegeuk Kim  * debug.c
309239a53e0cSJaegeuk Kim  */
309339a53e0cSJaegeuk Kim #ifdef CONFIG_F2FS_STAT_FS
309439a53e0cSJaegeuk Kim struct f2fs_stat_info {
309539a53e0cSJaegeuk Kim 	struct list_head stat_list;
309639a53e0cSJaegeuk Kim 	struct f2fs_sb_info *sbi;
309739a53e0cSJaegeuk Kim 	int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
309839a53e0cSJaegeuk Kim 	int main_area_segs, main_area_sections, main_area_zones;
30995b7ee374SChao Yu 	unsigned long long hit_largest, hit_cached, hit_rbtree;
31005b7ee374SChao Yu 	unsigned long long hit_total, total_ext;
3101c00ba554SJaegeuk Kim 	int ext_tree, zombie_tree, ext_node;
31022c8a4a28SJaegeuk Kim 	int ndirty_node, ndirty_dent, ndirty_meta, ndirty_imeta;
31032c8a4a28SJaegeuk Kim 	int ndirty_data, ndirty_qdata;
310435782b23SJaegeuk Kim 	int inmem_pages;
31052c8a4a28SJaegeuk Kim 	unsigned int ndirty_dirs, ndirty_files, nquota_files, ndirty_all;
31065b0ef73cSJaegeuk Kim 	int nats, dirty_nats, sits, dirty_sits;
31075b0ef73cSJaegeuk Kim 	int free_nids, avail_nids, alloc_nids;
310839a53e0cSJaegeuk Kim 	int total_count, utilization;
31098b8dd65fSChao Yu 	int bg_gc, nr_wb_cp_data, nr_wb_data;
3110274bd9baSChao Yu 	unsigned int io_skip_bggc, other_skip_bggc;
311114d8d5f7SChao Yu 	int nr_flushing, nr_flushed, flush_list_empty;
311214d8d5f7SChao Yu 	int nr_discarding, nr_discarded;
31135f32366aSChao Yu 	int nr_discard_cmd;
3114d84d1cbdSChao Yu 	unsigned int undiscard_blks;
3115a00861dbSJaegeuk Kim 	int inline_xattr, inline_inode, inline_dir, append, update, orphans;
3116648d50baSChao Yu 	int aw_cnt, max_aw_cnt, vw_cnt, max_vw_cnt;
3117f83a2584SYunlei He 	unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
311839a53e0cSJaegeuk Kim 	unsigned int bimodal, avg_vblocks;
311939a53e0cSJaegeuk Kim 	int util_free, util_valid, util_invalid;
312039a53e0cSJaegeuk Kim 	int rsvd_segs, overp_segs;
312139a53e0cSJaegeuk Kim 	int dirty_count, node_pages, meta_pages;
312242190d2aSJaegeuk Kim 	int prefree_count, call_count, cp_count, bg_cp_count;
312339a53e0cSJaegeuk Kim 	int tot_segs, node_segs, data_segs, free_segs, free_secs;
3124e1235983SChangman Lee 	int bg_node_segs, bg_data_segs;
312539a53e0cSJaegeuk Kim 	int tot_blks, data_blks, node_blks;
3126e1235983SChangman Lee 	int bg_data_blks, bg_node_blks;
31272ef79ecbSChao Yu 	unsigned long long skipped_atomic_files[2];
312839a53e0cSJaegeuk Kim 	int curseg[NR_CURSEG_TYPE];
312939a53e0cSJaegeuk Kim 	int cursec[NR_CURSEG_TYPE];
313039a53e0cSJaegeuk Kim 	int curzone[NR_CURSEG_TYPE];
313139a53e0cSJaegeuk Kim 
3132b63e7be5SChao Yu 	unsigned int meta_count[META_MAX];
313339a53e0cSJaegeuk Kim 	unsigned int segment_count[2];
313439a53e0cSJaegeuk Kim 	unsigned int block_count[2];
3135b9a2c252SChangman Lee 	unsigned int inplace_count;
31369edcdabfSChao Yu 	unsigned long long base_mem, cache_mem, page_mem;
313739a53e0cSJaegeuk Kim };
313839a53e0cSJaegeuk Kim 
3139963d4f7dSGu Zheng static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
3140963d4f7dSGu Zheng {
3141963d4f7dSGu Zheng 	return (struct f2fs_stat_info *)sbi->stat_info;
3142963d4f7dSGu Zheng }
3143963d4f7dSGu Zheng 
3144942e0be6SChangman Lee #define stat_inc_cp_count(si)		((si)->cp_count++)
314542190d2aSJaegeuk Kim #define stat_inc_bg_cp_count(si)	((si)->bg_cp_count++)
314639a53e0cSJaegeuk Kim #define stat_inc_call_count(si)		((si)->call_count++)
3147dcdfff65SJaegeuk Kim #define stat_inc_bggc_count(sbi)	((sbi)->bg_gc++)
3148274bd9baSChao Yu #define stat_io_skip_bggc_count(sbi)	((sbi)->io_skip_bggc++)
3149274bd9baSChao Yu #define stat_other_skip_bggc_count(sbi)	((sbi)->other_skip_bggc++)
315033fbd510SChao Yu #define stat_inc_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]++)
315133fbd510SChao Yu #define stat_dec_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]--)
31525b7ee374SChao Yu #define stat_inc_total_hit(sbi)		(atomic64_inc(&(sbi)->total_hit_ext))
31535b7ee374SChao Yu #define stat_inc_rbtree_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_rbtree))
31545b7ee374SChao Yu #define stat_inc_largest_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_largest))
31555b7ee374SChao Yu #define stat_inc_cached_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_cached))
3156d5e8f6c9SChao Yu #define stat_inc_inline_xattr(inode)					\
3157d5e8f6c9SChao Yu 	do {								\
3158d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3159d5e8f6c9SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_xattr));	\
3160d5e8f6c9SChao Yu 	} while (0)
3161d5e8f6c9SChao Yu #define stat_dec_inline_xattr(inode)					\
3162d5e8f6c9SChao Yu 	do {								\
3163d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3164d5e8f6c9SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_xattr));	\
3165d5e8f6c9SChao Yu 	} while (0)
31660dbdc2aeSJaegeuk Kim #define stat_inc_inline_inode(inode)					\
31670dbdc2aeSJaegeuk Kim 	do {								\
31680dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
316903e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_inode));	\
31700dbdc2aeSJaegeuk Kim 	} while (0)
31710dbdc2aeSJaegeuk Kim #define stat_dec_inline_inode(inode)					\
31720dbdc2aeSJaegeuk Kim 	do {								\
31730dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
317403e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_inode));	\
31750dbdc2aeSJaegeuk Kim 	} while (0)
31763289c061SJaegeuk Kim #define stat_inc_inline_dir(inode)					\
31773289c061SJaegeuk Kim 	do {								\
31783289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
317903e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_dir));	\
31803289c061SJaegeuk Kim 	} while (0)
31813289c061SJaegeuk Kim #define stat_dec_inline_dir(inode)					\
31823289c061SJaegeuk Kim 	do {								\
31833289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
318403e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_dir));	\
31853289c061SJaegeuk Kim 	} while (0)
3186b63e7be5SChao Yu #define stat_inc_meta_count(sbi, blkaddr)				\
3187b63e7be5SChao Yu 	do {								\
3188b63e7be5SChao Yu 		if (blkaddr < SIT_I(sbi)->sit_base_addr)		\
3189b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_CP]);	\
3190b63e7be5SChao Yu 		else if (blkaddr < NM_I(sbi)->nat_blkaddr)		\
3191b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_SIT]);	\
3192b63e7be5SChao Yu 		else if (blkaddr < SM_I(sbi)->ssa_blkaddr)		\
3193b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_NAT]);	\
3194b63e7be5SChao Yu 		else if (blkaddr < SM_I(sbi)->main_blkaddr)		\
3195b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_SSA]);	\
3196b63e7be5SChao Yu 	} while (0)
3197dcdfff65SJaegeuk Kim #define stat_inc_seg_type(sbi, curseg)					\
3198dcdfff65SJaegeuk Kim 		((sbi)->segment_count[(curseg)->alloc_type]++)
3199dcdfff65SJaegeuk Kim #define stat_inc_block_count(sbi, curseg)				\
3200dcdfff65SJaegeuk Kim 		((sbi)->block_count[(curseg)->alloc_type]++)
3201b9a2c252SChangman Lee #define stat_inc_inplace_blocks(sbi)					\
3202b9a2c252SChangman Lee 		(atomic_inc(&(sbi)->inplace_count))
320326a28a0cSJaegeuk Kim #define stat_inc_atomic_write(inode)					\
3204cac5a3d8SDongOh Shin 		(atomic_inc(&F2FS_I_SB(inode)->aw_cnt))
320526a28a0cSJaegeuk Kim #define stat_dec_atomic_write(inode)					\
3206cac5a3d8SDongOh Shin 		(atomic_dec(&F2FS_I_SB(inode)->aw_cnt))
320726a28a0cSJaegeuk Kim #define stat_update_max_atomic_write(inode)				\
320826a28a0cSJaegeuk Kim 	do {								\
320926a28a0cSJaegeuk Kim 		int cur = atomic_read(&F2FS_I_SB(inode)->aw_cnt);	\
321026a28a0cSJaegeuk Kim 		int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt);	\
321126a28a0cSJaegeuk Kim 		if (cur > max)						\
321226a28a0cSJaegeuk Kim 			atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur);	\
321326a28a0cSJaegeuk Kim 	} while (0)
3214648d50baSChao Yu #define stat_inc_volatile_write(inode)					\
3215648d50baSChao Yu 		(atomic_inc(&F2FS_I_SB(inode)->vw_cnt))
3216648d50baSChao Yu #define stat_dec_volatile_write(inode)					\
3217648d50baSChao Yu 		(atomic_dec(&F2FS_I_SB(inode)->vw_cnt))
3218648d50baSChao Yu #define stat_update_max_volatile_write(inode)				\
3219648d50baSChao Yu 	do {								\
3220648d50baSChao Yu 		int cur = atomic_read(&F2FS_I_SB(inode)->vw_cnt);	\
3221648d50baSChao Yu 		int max = atomic_read(&F2FS_I_SB(inode)->max_vw_cnt);	\
3222648d50baSChao Yu 		if (cur > max)						\
3223648d50baSChao Yu 			atomic_set(&F2FS_I_SB(inode)->max_vw_cnt, cur);	\
3224648d50baSChao Yu 	} while (0)
3225e1235983SChangman Lee #define stat_inc_seg_count(sbi, type, gc_type)				\
322639a53e0cSJaegeuk Kim 	do {								\
3227963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
322868afcf2dSTomohiro Kusumi 		si->tot_segs++;						\
322968afcf2dSTomohiro Kusumi 		if ((type) == SUM_TYPE_DATA) {				\
323039a53e0cSJaegeuk Kim 			si->data_segs++;				\
3231e1235983SChangman Lee 			si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0;	\
3232e1235983SChangman Lee 		} else {						\
323339a53e0cSJaegeuk Kim 			si->node_segs++;				\
3234e1235983SChangman Lee 			si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0;	\
3235e1235983SChangman Lee 		}							\
323639a53e0cSJaegeuk Kim 	} while (0)
323739a53e0cSJaegeuk Kim 
323839a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks)				\
323968afcf2dSTomohiro Kusumi 	((si)->tot_blks += (blks))
324039a53e0cSJaegeuk Kim 
3241e1235983SChangman Lee #define stat_inc_data_blk_count(sbi, blks, gc_type)			\
324239a53e0cSJaegeuk Kim 	do {								\
3243963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
324439a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
324539a53e0cSJaegeuk Kim 		si->data_blks += (blks);				\
324668afcf2dSTomohiro Kusumi 		si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
324739a53e0cSJaegeuk Kim 	} while (0)
324839a53e0cSJaegeuk Kim 
3249e1235983SChangman Lee #define stat_inc_node_blk_count(sbi, blks, gc_type)			\
325039a53e0cSJaegeuk Kim 	do {								\
3251963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
325239a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
325339a53e0cSJaegeuk Kim 		si->node_blks += (blks);				\
325468afcf2dSTomohiro Kusumi 		si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
325539a53e0cSJaegeuk Kim 	} while (0)
325639a53e0cSJaegeuk Kim 
3257cac5a3d8SDongOh Shin int f2fs_build_stats(struct f2fs_sb_info *sbi);
3258cac5a3d8SDongOh Shin void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
3259787c7b8cSChao Yu int __init f2fs_create_root_stats(void);
32604589d25dSNamjae Jeon void f2fs_destroy_root_stats(void);
326139a53e0cSJaegeuk Kim #else
3262d66450e7SArnd Bergmann #define stat_inc_cp_count(si)				do { } while (0)
3263d66450e7SArnd Bergmann #define stat_inc_bg_cp_count(si)			do { } while (0)
3264d66450e7SArnd Bergmann #define stat_inc_call_count(si)				do { } while (0)
3265d66450e7SArnd Bergmann #define stat_inc_bggc_count(si)				do { } while (0)
3266274bd9baSChao Yu #define stat_io_skip_bggc_count(sbi)			do { } while (0)
3267274bd9baSChao Yu #define stat_other_skip_bggc_count(sbi)			do { } while (0)
3268d66450e7SArnd Bergmann #define stat_inc_dirty_inode(sbi, type)			do { } while (0)
3269d66450e7SArnd Bergmann #define stat_dec_dirty_inode(sbi, type)			do { } while (0)
3270d66450e7SArnd Bergmann #define stat_inc_total_hit(sb)				do { } while (0)
3271d66450e7SArnd Bergmann #define stat_inc_rbtree_node_hit(sb)			do { } while (0)
3272d66450e7SArnd Bergmann #define stat_inc_largest_node_hit(sbi)			do { } while (0)
3273d66450e7SArnd Bergmann #define stat_inc_cached_node_hit(sbi)			do { } while (0)
3274d66450e7SArnd Bergmann #define stat_inc_inline_xattr(inode)			do { } while (0)
3275d66450e7SArnd Bergmann #define stat_dec_inline_xattr(inode)			do { } while (0)
3276d66450e7SArnd Bergmann #define stat_inc_inline_inode(inode)			do { } while (0)
3277d66450e7SArnd Bergmann #define stat_dec_inline_inode(inode)			do { } while (0)
3278d66450e7SArnd Bergmann #define stat_inc_inline_dir(inode)			do { } while (0)
3279d66450e7SArnd Bergmann #define stat_dec_inline_dir(inode)			do { } while (0)
3280d66450e7SArnd Bergmann #define stat_inc_atomic_write(inode)			do { } while (0)
3281d66450e7SArnd Bergmann #define stat_dec_atomic_write(inode)			do { } while (0)
3282d66450e7SArnd Bergmann #define stat_update_max_atomic_write(inode)		do { } while (0)
3283d66450e7SArnd Bergmann #define stat_inc_volatile_write(inode)			do { } while (0)
3284d66450e7SArnd Bergmann #define stat_dec_volatile_write(inode)			do { } while (0)
3285d66450e7SArnd Bergmann #define stat_update_max_volatile_write(inode)		do { } while (0)
3286b63e7be5SChao Yu #define stat_inc_meta_count(sbi, blkaddr)		do { } while (0)
3287d66450e7SArnd Bergmann #define stat_inc_seg_type(sbi, curseg)			do { } while (0)
3288d66450e7SArnd Bergmann #define stat_inc_block_count(sbi, curseg)		do { } while (0)
3289d66450e7SArnd Bergmann #define stat_inc_inplace_blocks(sbi)			do { } while (0)
3290d66450e7SArnd Bergmann #define stat_inc_seg_count(sbi, type, gc_type)		do { } while (0)
3291d66450e7SArnd Bergmann #define stat_inc_tot_blk_count(si, blks)		do { } while (0)
3292d66450e7SArnd Bergmann #define stat_inc_data_blk_count(sbi, blks, gc_type)	do { } while (0)
3293d66450e7SArnd Bergmann #define stat_inc_node_blk_count(sbi, blks, gc_type)	do { } while (0)
329439a53e0cSJaegeuk Kim 
329539a53e0cSJaegeuk Kim static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
329639a53e0cSJaegeuk Kim static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
3297787c7b8cSChao Yu static inline int __init f2fs_create_root_stats(void) { return 0; }
32984589d25dSNamjae Jeon static inline void f2fs_destroy_root_stats(void) { }
329939a53e0cSJaegeuk Kim #endif
330039a53e0cSJaegeuk Kim 
330139a53e0cSJaegeuk Kim extern const struct file_operations f2fs_dir_operations;
330239a53e0cSJaegeuk Kim extern const struct file_operations f2fs_file_operations;
330339a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_file_inode_operations;
330439a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_dblock_aops;
330539a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_node_aops;
330639a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_meta_aops;
330739a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_dir_inode_operations;
330839a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_symlink_inode_operations;
3309cbaf042aSJaegeuk Kim extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
331039a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_special_inode_operations;
33114d57b86dSChao Yu extern struct kmem_cache *f2fs_inode_entry_slab;
33121001b347SHuajun Li 
3313e18c65b2SHuajun Li /*
3314e18c65b2SHuajun Li  * inline.c
3315e18c65b2SHuajun Li  */
3316cac5a3d8SDongOh Shin bool f2fs_may_inline_data(struct inode *inode);
3317cac5a3d8SDongOh Shin bool f2fs_may_inline_dentry(struct inode *inode);
33184d57b86dSChao Yu void f2fs_do_read_inline_data(struct page *page, struct page *ipage);
33194d57b86dSChao Yu void f2fs_truncate_inline_inode(struct inode *inode,
33204d57b86dSChao Yu 						struct page *ipage, u64 from);
3321cac5a3d8SDongOh Shin int f2fs_read_inline_data(struct inode *inode, struct page *page);
3322cac5a3d8SDongOh Shin int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
3323cac5a3d8SDongOh Shin int f2fs_convert_inline_inode(struct inode *inode);
3324cac5a3d8SDongOh Shin int f2fs_write_inline_data(struct inode *inode, struct page *page);
33254d57b86dSChao Yu bool f2fs_recover_inline_data(struct inode *inode, struct page *npage);
33264d57b86dSChao Yu struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
3327cac5a3d8SDongOh Shin 			struct fscrypt_name *fname, struct page **res_page);
33284d57b86dSChao Yu int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent,
3329cac5a3d8SDongOh Shin 			struct page *ipage);
3330cac5a3d8SDongOh Shin int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
3331cac5a3d8SDongOh Shin 			const struct qstr *orig_name,
3332cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
33334d57b86dSChao Yu void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry,
33344d57b86dSChao Yu 				struct page *page, struct inode *dir,
33354d57b86dSChao Yu 				struct inode *inode);
3336cac5a3d8SDongOh Shin bool f2fs_empty_inline_dir(struct inode *dir);
3337cac5a3d8SDongOh Shin int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
3338cac5a3d8SDongOh Shin 			struct fscrypt_str *fstr);
3339cac5a3d8SDongOh Shin int f2fs_inline_data_fiemap(struct inode *inode,
3340cac5a3d8SDongOh Shin 			struct fiemap_extent_info *fieinfo,
3341cac5a3d8SDongOh Shin 			__u64 start, __u64 len);
3342cde4de12SJaegeuk Kim 
3343cde4de12SJaegeuk Kim /*
33442658e50dSJaegeuk Kim  * shrinker.c
33452658e50dSJaegeuk Kim  */
3346cac5a3d8SDongOh Shin unsigned long f2fs_shrink_count(struct shrinker *shrink,
3347cac5a3d8SDongOh Shin 			struct shrink_control *sc);
3348cac5a3d8SDongOh Shin unsigned long f2fs_shrink_scan(struct shrinker *shrink,
3349cac5a3d8SDongOh Shin 			struct shrink_control *sc);
3350cac5a3d8SDongOh Shin void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
3351cac5a3d8SDongOh Shin void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
33522658e50dSJaegeuk Kim 
33532658e50dSJaegeuk Kim /*
3354a28ef1f5SChao Yu  * extent_cache.c
3355a28ef1f5SChao Yu  */
33564d57b86dSChao Yu struct rb_entry *f2fs_lookup_rb_tree(struct rb_root *root,
3357004b6862SChao Yu 				struct rb_entry *cached_re, unsigned int ofs);
33584d57b86dSChao Yu struct rb_node **f2fs_lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
3359004b6862SChao Yu 				struct rb_root *root, struct rb_node **parent,
3360004b6862SChao Yu 				unsigned int ofs);
33614d57b86dSChao Yu struct rb_entry *f2fs_lookup_rb_tree_ret(struct rb_root *root,
3362004b6862SChao Yu 		struct rb_entry *cached_re, unsigned int ofs,
3363004b6862SChao Yu 		struct rb_entry **prev_entry, struct rb_entry **next_entry,
3364004b6862SChao Yu 		struct rb_node ***insert_p, struct rb_node **insert_parent,
3365004b6862SChao Yu 		bool force);
33664d57b86dSChao Yu bool f2fs_check_rb_tree_consistence(struct f2fs_sb_info *sbi,
3367df0f6b44SChao Yu 						struct rb_root *root);
3368cac5a3d8SDongOh Shin unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
3369cac5a3d8SDongOh Shin bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext);
3370cac5a3d8SDongOh Shin void f2fs_drop_extent_tree(struct inode *inode);
3371cac5a3d8SDongOh Shin unsigned int f2fs_destroy_extent_node(struct inode *inode);
3372cac5a3d8SDongOh Shin void f2fs_destroy_extent_tree(struct inode *inode);
3373cac5a3d8SDongOh Shin bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
3374cac5a3d8SDongOh Shin 			struct extent_info *ei);
3375cac5a3d8SDongOh Shin void f2fs_update_extent_cache(struct dnode_of_data *dn);
337619b2c30dSChao Yu void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
3377cac5a3d8SDongOh Shin 			pgoff_t fofs, block_t blkaddr, unsigned int len);
33784d57b86dSChao Yu void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi);
33794d57b86dSChao Yu int __init f2fs_create_extent_cache(void);
33804d57b86dSChao Yu void f2fs_destroy_extent_cache(void);
3381a28ef1f5SChao Yu 
3382a28ef1f5SChao Yu /*
33838ceffcb2SChao Yu  * sysfs.c
33848ceffcb2SChao Yu  */
3385dc6b2055SJaegeuk Kim int __init f2fs_init_sysfs(void);
3386dc6b2055SJaegeuk Kim void f2fs_exit_sysfs(void);
3387dc6b2055SJaegeuk Kim int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
3388dc6b2055SJaegeuk Kim void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
33898ceffcb2SChao Yu 
33908ceffcb2SChao Yu /*
3391cde4de12SJaegeuk Kim  * crypto support
3392cde4de12SJaegeuk Kim  */
33930b81d077SJaegeuk Kim static inline bool f2fs_encrypted_inode(struct inode *inode)
3394cde4de12SJaegeuk Kim {
3395cde4de12SJaegeuk Kim 	return file_is_encrypt(inode);
3396cde4de12SJaegeuk Kim }
3397cde4de12SJaegeuk Kim 
33981958593eSJaegeuk Kim static inline bool f2fs_encrypted_file(struct inode *inode)
33991958593eSJaegeuk Kim {
34001958593eSJaegeuk Kim 	return f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode);
34011958593eSJaegeuk Kim }
34021958593eSJaegeuk Kim 
3403cde4de12SJaegeuk Kim static inline void f2fs_set_encrypted_inode(struct inode *inode)
3404cde4de12SJaegeuk Kim {
3405cde4de12SJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
3406cde4de12SJaegeuk Kim 	file_set_encrypt(inode);
34072ee6a576SEric Biggers 	inode->i_flags |= S_ENCRYPTED;
3408cde4de12SJaegeuk Kim #endif
3409cde4de12SJaegeuk Kim }
3410cde4de12SJaegeuk Kim 
34116dbb1796SEric Biggers /*
34126dbb1796SEric Biggers  * Returns true if the reads of the inode's data need to undergo some
34136dbb1796SEric Biggers  * postprocessing step, like decryption or authenticity verification.
34146dbb1796SEric Biggers  */
34156dbb1796SEric Biggers static inline bool f2fs_post_read_required(struct inode *inode)
3416cde4de12SJaegeuk Kim {
34176dbb1796SEric Biggers 	return f2fs_encrypted_file(inode);
3418cde4de12SJaegeuk Kim }
3419cde4de12SJaegeuk Kim 
3420ccd31cb2SSheng Yong #define F2FS_FEATURE_FUNCS(name, flagname) \
3421ccd31cb2SSheng Yong static inline int f2fs_sb_has_##name(struct super_block *sb) \
3422ccd31cb2SSheng Yong { \
3423ccd31cb2SSheng Yong 	return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_##flagname); \
3424cde4de12SJaegeuk Kim }
3425f424f664SJaegeuk Kim 
3426ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(encrypt, ENCRYPT);
3427ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(blkzoned, BLKZONED);
3428ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(extra_attr, EXTRA_ATTR);
3429ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(project_quota, PRJQUOTA);
3430ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_chksum, INODE_CHKSUM);
3431ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
3432ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
3433ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
3434b7c409deSSheng Yong F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
3435d440c52dSJunling Zheng F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
34361c1d35dfSChao Yu 
3437178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
3438178053e2SDamien Le Moal static inline int get_blkz_type(struct f2fs_sb_info *sbi,
34393c62be17SJaegeuk Kim 			struct block_device *bdev, block_t blkaddr)
3440178053e2SDamien Le Moal {
3441178053e2SDamien Le Moal 	unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
34423c62be17SJaegeuk Kim 	int i;
3443178053e2SDamien Le Moal 
34443c62be17SJaegeuk Kim 	for (i = 0; i < sbi->s_ndevs; i++)
34453c62be17SJaegeuk Kim 		if (FDEV(i).bdev == bdev)
34463c62be17SJaegeuk Kim 			return FDEV(i).blkz_type[zno];
34473c62be17SJaegeuk Kim 	return -EINVAL;
3448178053e2SDamien Le Moal }
3449178053e2SDamien Le Moal #endif
3450178053e2SDamien Le Moal 
34517d20c8abSChao Yu static inline bool f2fs_hw_should_discard(struct f2fs_sb_info *sbi)
345296ba2decSDamien Le Moal {
34537d20c8abSChao Yu 	return f2fs_sb_has_blkzoned(sbi->sb);
34547d20c8abSChao Yu }
345596ba2decSDamien Le Moal 
34567d20c8abSChao Yu static inline bool f2fs_hw_support_discard(struct f2fs_sb_info *sbi)
34577d20c8abSChao Yu {
34587d20c8abSChao Yu 	return blk_queue_discard(bdev_get_queue(sbi->sb->s_bdev));
34597d20c8abSChao Yu }
34607d20c8abSChao Yu 
34617d20c8abSChao Yu static inline bool f2fs_realtime_discard_enable(struct f2fs_sb_info *sbi)
34627d20c8abSChao Yu {
34637d20c8abSChao Yu 	return (test_opt(sbi, DISCARD) && f2fs_hw_support_discard(sbi)) ||
34647d20c8abSChao Yu 					f2fs_hw_should_discard(sbi);
346552763a4bSJaegeuk Kim }
346652763a4bSJaegeuk Kim 
346752763a4bSJaegeuk Kim static inline void set_opt_mode(struct f2fs_sb_info *sbi, unsigned int mt)
346852763a4bSJaegeuk Kim {
346952763a4bSJaegeuk Kim 	clear_opt(sbi, ADAPTIVE);
347052763a4bSJaegeuk Kim 	clear_opt(sbi, LFS);
347152763a4bSJaegeuk Kim 
347252763a4bSJaegeuk Kim 	switch (mt) {
347352763a4bSJaegeuk Kim 	case F2FS_MOUNT_ADAPTIVE:
347452763a4bSJaegeuk Kim 		set_opt(sbi, ADAPTIVE);
347552763a4bSJaegeuk Kim 		break;
347652763a4bSJaegeuk Kim 	case F2FS_MOUNT_LFS:
347752763a4bSJaegeuk Kim 		set_opt(sbi, LFS);
347852763a4bSJaegeuk Kim 		break;
347952763a4bSJaegeuk Kim 	}
348052763a4bSJaegeuk Kim }
348152763a4bSJaegeuk Kim 
3482fcc85a4dSJaegeuk Kim static inline bool f2fs_may_encrypt(struct inode *inode)
3483fcc85a4dSJaegeuk Kim {
3484fcc85a4dSJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
3485886f56f9SAl Viro 	umode_t mode = inode->i_mode;
3486fcc85a4dSJaegeuk Kim 
3487fcc85a4dSJaegeuk Kim 	return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
3488fcc85a4dSJaegeuk Kim #else
34896e45f2a5SGustavo A. R. Silva 	return false;
3490fcc85a4dSJaegeuk Kim #endif
3491fcc85a4dSJaegeuk Kim }
3492fcc85a4dSJaegeuk Kim 
3493b91050a8SHyunchul Lee static inline bool f2fs_force_buffered_io(struct inode *inode, int rw)
3494b91050a8SHyunchul Lee {
34956dbb1796SEric Biggers 	return (f2fs_post_read_required(inode) ||
3496b91050a8SHyunchul Lee 			(rw == WRITE && test_opt(F2FS_I_SB(inode), LFS)) ||
3497b91050a8SHyunchul Lee 			F2FS_I_SB(inode)->s_ndevs);
3498b91050a8SHyunchul Lee }
3499b91050a8SHyunchul Lee 
350083a3bfdbSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
3501d494500aSChao Yu extern void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate,
3502d494500aSChao Yu 							unsigned int type);
350383a3bfdbSJaegeuk Kim #else
3504d494500aSChao Yu #define f2fs_build_fault_attr(sbi, rate, type)		do { } while (0)
350583a3bfdbSJaegeuk Kim #endif
350683a3bfdbSJaegeuk Kim 
350739a53e0cSJaegeuk Kim #endif
3508