xref: /openbmc/linux/fs/f2fs/f2fs.h (revision 0683728adab251d7747ce61279e249944effa78d)
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 
11f847c699SChao Yu #include <linux/uio.h>
1239a53e0cSJaegeuk Kim #include <linux/types.h>
1339a53e0cSJaegeuk Kim #include <linux/page-flags.h>
1439a53e0cSJaegeuk Kim #include <linux/buffer_head.h>
1539a53e0cSJaegeuk Kim #include <linux/slab.h>
1639a53e0cSJaegeuk Kim #include <linux/crc32.h>
1739a53e0cSJaegeuk Kim #include <linux/magic.h>
18c2d715d1SJaegeuk Kim #include <linux/kobject.h>
197bd59381SGu Zheng #include <linux/sched.h>
207c2e5963SJaegeuk Kim #include <linux/cred.h>
2139307a8eSJaegeuk Kim #include <linux/vmalloc.h>
22740432f8SJaegeuk Kim #include <linux/bio.h>
23d0239e1bSJaegeuk Kim #include <linux/blkdev.h>
240abd675eSChao Yu #include <linux/quotaops.h>
2543b6573bSKeith Mok #include <crypto/hash.h>
2639a53e0cSJaegeuk Kim 
27734f0d24SDave Chinner #include <linux/fscrypt.h>
2895ae251fSEric Biggers #include <linux/fsverity.h>
29734f0d24SDave Chinner 
305d56b671SJaegeuk Kim #ifdef CONFIG_F2FS_CHECK_FS
319850cf4aSJaegeuk Kim #define f2fs_bug_on(sbi, condition)	BUG_ON(condition)
325d56b671SJaegeuk Kim #else
339850cf4aSJaegeuk Kim #define f2fs_bug_on(sbi, condition)					\
349850cf4aSJaegeuk Kim 	do {								\
359850cf4aSJaegeuk Kim 		if (unlikely(condition)) {				\
369850cf4aSJaegeuk Kim 			WARN_ON(1);					\
37caf0047eSChao Yu 			set_sbi_flag(sbi, SBI_NEED_FSCK);		\
389850cf4aSJaegeuk Kim 		}							\
399850cf4aSJaegeuk Kim 	} while (0)
405d56b671SJaegeuk Kim #endif
415d56b671SJaegeuk Kim 
422c63feadSJaegeuk Kim enum {
432c63feadSJaegeuk Kim 	FAULT_KMALLOC,
44628b3d14SChao Yu 	FAULT_KVMALLOC,
45c41f3cc3SJaegeuk Kim 	FAULT_PAGE_ALLOC,
4601eccef7SChao Yu 	FAULT_PAGE_GET,
47d62fe971SChao Yu 	FAULT_ALLOC_BIO,
48cb78942bSJaegeuk Kim 	FAULT_ALLOC_NID,
49cb78942bSJaegeuk Kim 	FAULT_ORPHAN,
50cb78942bSJaegeuk Kim 	FAULT_BLOCK,
51cb78942bSJaegeuk Kim 	FAULT_DIR_DEPTH,
5253aa6bbfSJaegeuk Kim 	FAULT_EVICT_INODE,
5314b44d23SJaegeuk Kim 	FAULT_TRUNCATE,
546f5c2ed0SChao Yu 	FAULT_READ_IO,
550f348028SChao Yu 	FAULT_CHECKPOINT,
56b83dcfe6SChao Yu 	FAULT_DISCARD,
576f5c2ed0SChao Yu 	FAULT_WRITE_IO,
582c63feadSJaegeuk Kim 	FAULT_MAX,
592c63feadSJaegeuk Kim };
602c63feadSJaegeuk Kim 
617fa750a1SArnd Bergmann #ifdef CONFIG_F2FS_FAULT_INJECTION
62d494500aSChao Yu #define F2FS_ALL_FAULT_TYPE		((1 << FAULT_MAX) - 1)
63d494500aSChao Yu 
6408796897SSheng Yong struct f2fs_fault_info {
6508796897SSheng Yong 	atomic_t inject_ops;
6608796897SSheng Yong 	unsigned int inject_rate;
6708796897SSheng Yong 	unsigned int inject_type;
6808796897SSheng Yong };
6908796897SSheng Yong 
7019880e6eSAlexey Dobriyan extern const char *f2fs_fault_name[FAULT_MAX];
7168afcf2dSTomohiro Kusumi #define IS_FAULT_SET(fi, type) ((fi)->inject_type & (1 << (type)))
722c63feadSJaegeuk Kim #endif
732c63feadSJaegeuk Kim 
7439a53e0cSJaegeuk Kim /*
7539a53e0cSJaegeuk Kim  * For mount options
7639a53e0cSJaegeuk Kim  */
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
90343f40f0SChao Yu #define F2FS_MOUNT_DATA_FLUSH		0x00008000
9173faec4dSJaegeuk Kim #define F2FS_MOUNT_FAULT_INJECTION	0x00010000
920abd675eSChao Yu #define F2FS_MOUNT_USRQUOTA		0x00080000
930abd675eSChao Yu #define F2FS_MOUNT_GRPQUOTA		0x00100000
945c57132eSChao Yu #define F2FS_MOUNT_PRJQUOTA		0x00200000
954b2414d0SChao Yu #define F2FS_MOUNT_QUOTA		0x00400000
966afc662eSChao Yu #define F2FS_MOUNT_INLINE_XATTR_SIZE	0x00800000
977e65be49SJaegeuk Kim #define F2FS_MOUNT_RESERVE_ROOT		0x01000000
984354994fSDaniel Rosenberg #define F2FS_MOUNT_DISABLE_CHECKPOINT	0x02000000
99a9117ecaSChao Yu #define F2FS_MOUNT_NORECOVERY		0x04000000
10039a53e0cSJaegeuk Kim 
10163189b78SChao Yu #define F2FS_OPTION(sbi)	((sbi)->mount_opt)
10263189b78SChao Yu #define clear_opt(sbi, option)	(F2FS_OPTION(sbi).opt &= ~F2FS_MOUNT_##option)
10363189b78SChao Yu #define set_opt(sbi, option)	(F2FS_OPTION(sbi).opt |= F2FS_MOUNT_##option)
10463189b78SChao Yu #define test_opt(sbi, option)	(F2FS_OPTION(sbi).opt & F2FS_MOUNT_##option)
10539a53e0cSJaegeuk Kim 
10639a53e0cSJaegeuk Kim #define ver_after(a, b)	(typecheck(unsigned long long, a) &&		\
10739a53e0cSJaegeuk Kim 		typecheck(unsigned long long, b) &&			\
10839a53e0cSJaegeuk Kim 		((long long)((a) - (b)) > 0))
10939a53e0cSJaegeuk Kim 
110a9841c4dSJaegeuk Kim typedef u32 block_t;	/*
111a9841c4dSJaegeuk Kim 			 * should not change u32, since it is the on-disk block
112a9841c4dSJaegeuk Kim 			 * address format, __le32.
113a9841c4dSJaegeuk Kim 			 */
11439a53e0cSJaegeuk Kim typedef u32 nid_t;
11539a53e0cSJaegeuk Kim 
1164c8ff709SChao Yu #define COMPRESS_EXT_NUM		16
1174c8ff709SChao Yu 
11839a53e0cSJaegeuk Kim struct f2fs_mount_info {
11939a53e0cSJaegeuk Kim 	unsigned int opt;
12063189b78SChao Yu 	int write_io_size_bits;		/* Write IO size bits */
12163189b78SChao Yu 	block_t root_reserved_blocks;	/* root reserved blocks */
12263189b78SChao Yu 	kuid_t s_resuid;		/* reserved blocks for uid */
12363189b78SChao Yu 	kgid_t s_resgid;		/* reserved blocks for gid */
12463189b78SChao Yu 	int active_logs;		/* # of active logs */
12563189b78SChao Yu 	int inline_xattr_size;		/* inline xattr size */
12663189b78SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
12763189b78SChao Yu 	struct f2fs_fault_info fault_info;	/* For fault injection */
12863189b78SChao Yu #endif
12963189b78SChao Yu #ifdef CONFIG_QUOTA
13063189b78SChao Yu 	/* Names of quota files with journalled quota */
13163189b78SChao Yu 	char *s_qf_names[MAXQUOTAS];
13263189b78SChao Yu 	int s_jquota_fmt;			/* Format of quota to use */
13363189b78SChao Yu #endif
13463189b78SChao Yu 	/* For which write hints are passed down to block layer */
13563189b78SChao Yu 	int whint_mode;
13663189b78SChao Yu 	int alloc_mode;			/* segment allocation policy */
13763189b78SChao Yu 	int fsync_mode;			/* fsync policy */
138b0332a0fSChao Yu 	int fs_mode;			/* fs mode: LFS or ADAPTIVE */
139bbbc34fdSChao Yu 	int bggc_mode;			/* bggc mode: off, on or sync */
140ff62af20SSheng Yong 	bool test_dummy_encryption;	/* test dummy encryption */
1414d3aed70SDaniel Rosenberg 	block_t unusable_cap;		/* Amount of space allowed to be
1424d3aed70SDaniel Rosenberg 					 * unusable when disabling checkpoint
1434d3aed70SDaniel Rosenberg 					 */
1444c8ff709SChao Yu 
1454c8ff709SChao Yu 	/* For compression */
1464c8ff709SChao Yu 	unsigned char compress_algorithm;	/* algorithm type */
1474c8ff709SChao Yu 	unsigned compress_log_size;		/* cluster log size */
1484c8ff709SChao Yu 	unsigned char compress_ext_cnt;		/* extension count */
1494c8ff709SChao Yu 	unsigned char extensions[COMPRESS_EXT_NUM][F2FS_EXTENSION_LEN];	/* extensions */
15039a53e0cSJaegeuk Kim };
15139a53e0cSJaegeuk Kim 
152cde4de12SJaegeuk Kim #define F2FS_FEATURE_ENCRYPT		0x0001
1530bfd7a09SDamien Le Moal #define F2FS_FEATURE_BLKZONED		0x0002
154e65ef207SJaegeuk Kim #define F2FS_FEATURE_ATOMIC_WRITE	0x0004
1557a2af766SChao Yu #define F2FS_FEATURE_EXTRA_ATTR		0x0008
1565c57132eSChao Yu #define F2FS_FEATURE_PRJQUOTA		0x0010
157704956ecSChao Yu #define F2FS_FEATURE_INODE_CHKSUM	0x0020
1586afc662eSChao Yu #define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR	0x0040
159234a9689SJaegeuk Kim #define F2FS_FEATURE_QUOTA_INO		0x0080
1601c1d35dfSChao Yu #define F2FS_FEATURE_INODE_CRTIME	0x0100
161b7c409deSSheng Yong #define F2FS_FEATURE_LOST_FOUND		0x0200
16295ae251fSEric Biggers #define F2FS_FEATURE_VERITY		0x0400
163d440c52dSJunling Zheng #define F2FS_FEATURE_SB_CHKSUM		0x0800
1645aba5430SDaniel Rosenberg #define F2FS_FEATURE_CASEFOLD		0x1000
1654c8ff709SChao Yu #define F2FS_FEATURE_COMPRESSION	0x2000
166cde4de12SJaegeuk Kim 
1677beb01f7SChao Yu #define __F2FS_HAS_FEATURE(raw_super, mask)				\
1687beb01f7SChao Yu 	((raw_super->feature & cpu_to_le32(mask)) != 0)
1697beb01f7SChao Yu #define F2FS_HAS_FEATURE(sbi, mask)	__F2FS_HAS_FEATURE(sbi->raw_super, mask)
1707beb01f7SChao Yu #define F2FS_SET_FEATURE(sbi, mask)					\
1717beb01f7SChao Yu 	(sbi->raw_super->feature |= cpu_to_le32(mask))
1727beb01f7SChao Yu #define F2FS_CLEAR_FEATURE(sbi, mask)					\
1737beb01f7SChao Yu 	(sbi->raw_super->feature &= ~cpu_to_le32(mask))
17476f105a2SJaegeuk Kim 
17539a53e0cSJaegeuk Kim /*
1767c2e5963SJaegeuk Kim  * Default values for user and/or group using reserved blocks
1777c2e5963SJaegeuk Kim  */
1787c2e5963SJaegeuk Kim #define	F2FS_DEF_RESUID		0
1797c2e5963SJaegeuk Kim #define	F2FS_DEF_RESGID		0
1807c2e5963SJaegeuk Kim 
1817c2e5963SJaegeuk Kim /*
18239a53e0cSJaegeuk Kim  * For checkpoint manager
18339a53e0cSJaegeuk Kim  */
18439a53e0cSJaegeuk Kim enum {
18539a53e0cSJaegeuk Kim 	NAT_BITMAP,
18639a53e0cSJaegeuk Kim 	SIT_BITMAP
18739a53e0cSJaegeuk Kim };
18839a53e0cSJaegeuk Kim 
189c473f1a9SChao Yu #define	CP_UMOUNT	0x00000001
190c473f1a9SChao Yu #define	CP_FASTBOOT	0x00000002
191c473f1a9SChao Yu #define	CP_SYNC		0x00000004
192c473f1a9SChao Yu #define	CP_RECOVERY	0x00000008
193c473f1a9SChao Yu #define	CP_DISCARD	0x00000010
1941f43e2adSChao Yu #define CP_TRIMMED	0x00000020
1954354994fSDaniel Rosenberg #define CP_PAUSE	0x00000040
19675ab4cb8SJaegeuk Kim 
1974ddb1a4dSJaegeuk Kim #define MAX_DISCARD_BLOCKS(sbi)		BLKS_PER_SEC(sbi)
198ecc9aa00SChao Yu #define DEF_MAX_DISCARD_REQUEST		8	/* issue 8 discards per round */
199969d1b18SChao Yu #define DEF_MIN_DISCARD_ISSUE_TIME	50	/* 50 ms, if exists */
200f9d1dcedSYunlei He #define DEF_MID_DISCARD_ISSUE_TIME	500	/* 500 ms, if device busy */
201969d1b18SChao Yu #define DEF_MAX_DISCARD_ISSUE_TIME	60000	/* 60 s, if no candidates */
2028bb4f253SJaegeuk Kim #define DEF_DISCARD_URGENT_UTIL		80	/* do more discard over 80% */
20360b99b48SJaegeuk Kim #define DEF_CP_INTERVAL			60	/* 60 secs */
204dcf25fe8SChao Yu #define DEF_IDLE_INTERVAL		5	/* 5 secs */
2054354994fSDaniel Rosenberg #define DEF_DISABLE_INTERVAL		5	/* 5 secs */
206db610a64SJaegeuk Kim #define DEF_DISABLE_QUICK_INTERVAL	1	/* 1 secs */
20703f2c02dSJaegeuk Kim #define DEF_UMOUNT_DISCARD_TIMEOUT	5	/* 5 secs */
208bba681cbSJaegeuk Kim 
20975ab4cb8SJaegeuk Kim struct cp_control {
21075ab4cb8SJaegeuk Kim 	int reason;
2114b2fecc8SJaegeuk Kim 	__u64 trim_start;
2124b2fecc8SJaegeuk Kim 	__u64 trim_end;
2134b2fecc8SJaegeuk Kim 	__u64 trim_minlen;
21475ab4cb8SJaegeuk Kim };
21575ab4cb8SJaegeuk Kim 
216662befdaSChao Yu /*
217e1da7872SChao Yu  * indicate meta/data type
218662befdaSChao Yu  */
219662befdaSChao Yu enum {
220662befdaSChao Yu 	META_CP,
221662befdaSChao Yu 	META_NAT,
22281c1a0f1SChao Yu 	META_SIT,
2234c521f49SJaegeuk Kim 	META_SSA,
224b63e7be5SChao Yu 	META_MAX,
2254c521f49SJaegeuk Kim 	META_POR,
22693770ab7SChao Yu 	DATA_GENERIC,		/* check range only */
22793770ab7SChao Yu 	DATA_GENERIC_ENHANCE,	/* strong check on range and segment bitmap */
22893770ab7SChao Yu 	DATA_GENERIC_ENHANCE_READ,	/*
22993770ab7SChao Yu 					 * strong check on range and segment
23093770ab7SChao Yu 					 * bitmap but no warning due to race
23193770ab7SChao Yu 					 * condition of read on truncated area
23293770ab7SChao Yu 					 * by extent_cache
23393770ab7SChao Yu 					 */
234e1da7872SChao Yu 	META_GENERIC,
235662befdaSChao Yu };
236662befdaSChao Yu 
2376451e041SJaegeuk Kim /* for the list of ino */
2386451e041SJaegeuk Kim enum {
2396451e041SJaegeuk Kim 	ORPHAN_INO,		/* for orphan ino list */
240fff04f90SJaegeuk Kim 	APPEND_INO,		/* for append ino list */
241fff04f90SJaegeuk Kim 	UPDATE_INO,		/* for update ino list */
2420a007b97SJaegeuk Kim 	TRANS_DIR_INO,		/* for trasactions dir ino list */
24339d787beSChao Yu 	FLUSH_INO,		/* for multiple device flushing */
2446451e041SJaegeuk Kim 	MAX_INO_ENTRY,		/* max. list */
2456451e041SJaegeuk Kim };
2466451e041SJaegeuk Kim 
2476451e041SJaegeuk Kim struct ino_entry {
24839a53e0cSJaegeuk Kim 	struct list_head list;		/* list head */
24939a53e0cSJaegeuk Kim 	nid_t ino;			/* inode number */
25039d787beSChao Yu 	unsigned int dirty_device;	/* dirty device bitmap */
25139a53e0cSJaegeuk Kim };
25239a53e0cSJaegeuk Kim 
2532710fd7eSChao Yu /* for the list of inodes to be GCed */
25406292073SChao Yu struct inode_entry {
25539a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
25639a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
25739a53e0cSJaegeuk Kim };
25839a53e0cSJaegeuk Kim 
25950fa53ecSChao Yu struct fsync_node_entry {
26050fa53ecSChao Yu 	struct list_head list;	/* list head */
26150fa53ecSChao Yu 	struct page *page;	/* warm node page pointer */
26250fa53ecSChao Yu 	unsigned int seq_id;	/* sequence id */
26350fa53ecSChao Yu };
26450fa53ecSChao Yu 
265a7eeb823SChao Yu /* for the bitmap indicate blocks to be discarded */
2667fd9e544SJaegeuk Kim struct discard_entry {
2677fd9e544SJaegeuk Kim 	struct list_head list;	/* list head */
268a7eeb823SChao Yu 	block_t start_blkaddr;	/* start blockaddr of current segment */
269a7eeb823SChao Yu 	unsigned char discard_map[SIT_VBLOCK_MAP_SIZE];	/* segment discard bitmap */
2707fd9e544SJaegeuk Kim };
2717fd9e544SJaegeuk Kim 
272969d1b18SChao Yu /* default discard granularity of inner discard thread, unit: block count */
273969d1b18SChao Yu #define DEFAULT_DISCARD_GRANULARITY		16
274969d1b18SChao Yu 
275ba48a33eSChao Yu /* max discard pend list number */
276ba48a33eSChao Yu #define MAX_PLIST_NUM		512
277ba48a33eSChao Yu #define plist_idx(blk_num)	((blk_num) >= MAX_PLIST_NUM ?		\
2782f84babfSSheng Yong 					(MAX_PLIST_NUM - 1) : ((blk_num) - 1))
279ba48a33eSChao Yu 
28015469963SJaegeuk Kim enum {
28135ec7d57SChao Yu 	D_PREP,			/* initial */
28235ec7d57SChao Yu 	D_PARTIAL,		/* partially submitted */
28335ec7d57SChao Yu 	D_SUBMIT,		/* all submitted */
28435ec7d57SChao Yu 	D_DONE,			/* finished */
28515469963SJaegeuk Kim };
28615469963SJaegeuk Kim 
287004b6862SChao Yu struct discard_info {
288b01a9201SJaegeuk Kim 	block_t lstart;			/* logical start address */
289b01a9201SJaegeuk Kim 	block_t len;			/* length */
290004b6862SChao Yu 	block_t start;			/* actual start address in dev */
291004b6862SChao Yu };
292004b6862SChao Yu 
293b01a9201SJaegeuk Kim struct discard_cmd {
294004b6862SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
295004b6862SChao Yu 	union {
296004b6862SChao Yu 		struct {
297004b6862SChao Yu 			block_t lstart;	/* logical start address */
298004b6862SChao Yu 			block_t len;	/* length */
299004b6862SChao Yu 			block_t start;	/* actual start address in dev */
300004b6862SChao Yu 		};
301004b6862SChao Yu 		struct discard_info di;	/* discard info */
302004b6862SChao Yu 
303004b6862SChao Yu 	};
304b01a9201SJaegeuk Kim 	struct list_head list;		/* command list */
305b01a9201SJaegeuk Kim 	struct completion wait;		/* compleation */
306c81abe34SJaegeuk Kim 	struct block_device *bdev;	/* bdev */
307ec9895adSChao Yu 	unsigned short ref;		/* reference count */
3089a744b92SChao Yu 	unsigned char state;		/* state */
30972691af6SJaegeuk Kim 	unsigned char queued;		/* queued discard */
310c81abe34SJaegeuk Kim 	int error;			/* bio error */
31135ec7d57SChao Yu 	spinlock_t lock;		/* for state/bio_ref updating */
31235ec7d57SChao Yu 	unsigned short bio_ref;		/* bio reference count */
313275b66b0SChao Yu };
314275b66b0SChao Yu 
31578997b56SChao Yu enum {
31678997b56SChao Yu 	DPOLICY_BG,
31778997b56SChao Yu 	DPOLICY_FORCE,
31878997b56SChao Yu 	DPOLICY_FSTRIM,
31978997b56SChao Yu 	DPOLICY_UMOUNT,
32078997b56SChao Yu 	MAX_DPOLICY,
32178997b56SChao Yu };
32278997b56SChao Yu 
323ecc9aa00SChao Yu struct discard_policy {
32478997b56SChao Yu 	int type;			/* type of discard */
325ecc9aa00SChao Yu 	unsigned int min_interval;	/* used for candidates exist */
326f9d1dcedSYunlei He 	unsigned int mid_interval;	/* used for device busy */
327ecc9aa00SChao Yu 	unsigned int max_interval;	/* used for candidates not exist */
328ecc9aa00SChao Yu 	unsigned int max_requests;	/* # of discards issued per round */
329ecc9aa00SChao Yu 	unsigned int io_aware_gran;	/* minimum granularity discard not be aware of I/O */
330ecc9aa00SChao Yu 	bool io_aware;			/* issue discard in idle time */
331ecc9aa00SChao Yu 	bool sync;			/* submit discard with REQ_SYNC flag */
33220ee4382SChao Yu 	bool ordered;			/* issue discard by lba order */
33378997b56SChao Yu 	unsigned int granularity;	/* discard granularity */
33403f2c02dSJaegeuk Kim 	int timeout;			/* discard timeout for put_super */
335ecc9aa00SChao Yu };
336ecc9aa00SChao Yu 
3370b54fb84SJaegeuk Kim struct discard_cmd_control {
33815469963SJaegeuk Kim 	struct task_struct *f2fs_issue_discard;	/* discard thread */
33946f84c2cSChao Yu 	struct list_head entry_list;		/* 4KB discard entry list */
340ba48a33eSChao Yu 	struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
34146f84c2cSChao Yu 	struct list_head wait_list;		/* store on-flushing entries */
3428412663dSChao Yu 	struct list_head fstrim_list;		/* in-flight discard from fstrim */
34315469963SJaegeuk Kim 	wait_queue_head_t discard_wait_queue;	/* waiting queue for wake-up */
344969d1b18SChao Yu 	unsigned int discard_wake;		/* to wake up discard thread */
34515469963SJaegeuk Kim 	struct mutex cmd_lock;
346d618ebafSChao Yu 	unsigned int nr_discards;		/* # of discards in the list */
347d618ebafSChao Yu 	unsigned int max_discards;		/* max. discards to be issued */
348969d1b18SChao Yu 	unsigned int discard_granularity;	/* discard granularity */
349d84d1cbdSChao Yu 	unsigned int undiscard_blks;		/* # of undiscard blocks */
35020ee4382SChao Yu 	unsigned int next_pos;			/* next discard position */
3518b8dd65fSChao Yu 	atomic_t issued_discard;		/* # of issued discard */
35272691af6SJaegeuk Kim 	atomic_t queued_discard;		/* # of queued discard */
3535f32366aSChao Yu 	atomic_t discard_cmd_cnt;		/* # of cached cmd count */
3544dada3fdSChao Yu 	struct rb_root_cached root;		/* root of discard rb-tree */
35567fce70bSChao Yu 	bool rbtree_check;			/* config for consistence check */
35639a53e0cSJaegeuk Kim };
35739a53e0cSJaegeuk Kim 
35839a53e0cSJaegeuk Kim /* for the list of fsync inodes, used only during recovery */
35939a53e0cSJaegeuk Kim struct fsync_inode_entry {
36039a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
36139a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
362c52e1b10SJaegeuk Kim 	block_t blkaddr;	/* block address locating the last fsync */
363c52e1b10SJaegeuk Kim 	block_t last_dentry;	/* block address locating the last dentry */
36439a53e0cSJaegeuk Kim };
36539a53e0cSJaegeuk Kim 
36668afcf2dSTomohiro Kusumi #define nats_in_cursum(jnl)		(le16_to_cpu((jnl)->n_nats))
36768afcf2dSTomohiro Kusumi #define sits_in_cursum(jnl)		(le16_to_cpu((jnl)->n_sits))
36839a53e0cSJaegeuk Kim 
36968afcf2dSTomohiro Kusumi #define nat_in_journal(jnl, i)		((jnl)->nat_j.entries[i].ne)
37068afcf2dSTomohiro Kusumi #define nid_in_journal(jnl, i)		((jnl)->nat_j.entries[i].nid)
37168afcf2dSTomohiro Kusumi #define sit_in_journal(jnl, i)		((jnl)->sit_j.entries[i].se)
37268afcf2dSTomohiro Kusumi #define segno_in_journal(jnl, i)	((jnl)->sit_j.entries[i].segno)
37339a53e0cSJaegeuk Kim 
374dfc08a12SChao Yu #define MAX_NAT_JENTRIES(jnl)	(NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
375dfc08a12SChao Yu #define MAX_SIT_JENTRIES(jnl)	(SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
376309cc2b6SJaegeuk Kim 
377dfc08a12SChao Yu static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
37839a53e0cSJaegeuk Kim {
379dfc08a12SChao Yu 	int before = nats_in_cursum(journal);
380cac5a3d8SDongOh Shin 
381dfc08a12SChao Yu 	journal->n_nats = cpu_to_le16(before + i);
38239a53e0cSJaegeuk Kim 	return before;
38339a53e0cSJaegeuk Kim }
38439a53e0cSJaegeuk Kim 
385dfc08a12SChao Yu static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
38639a53e0cSJaegeuk Kim {
387dfc08a12SChao Yu 	int before = sits_in_cursum(journal);
388cac5a3d8SDongOh Shin 
389dfc08a12SChao Yu 	journal->n_sits = cpu_to_le16(before + i);
39039a53e0cSJaegeuk Kim 	return before;
39139a53e0cSJaegeuk Kim }
39239a53e0cSJaegeuk Kim 
393dfc08a12SChao Yu static inline bool __has_cursum_space(struct f2fs_journal *journal,
394dfc08a12SChao Yu 							int size, int type)
395184a5cd2SChao Yu {
396184a5cd2SChao Yu 	if (type == NAT_JOURNAL)
397dfc08a12SChao Yu 		return size <= MAX_NAT_JENTRIES(journal);
398dfc08a12SChao Yu 	return size <= MAX_SIT_JENTRIES(journal);
399184a5cd2SChao Yu }
400184a5cd2SChao Yu 
40139a53e0cSJaegeuk Kim /*
402e9750824SNamjae Jeon  * ioctl commands
403e9750824SNamjae Jeon  */
404e9750824SNamjae Jeon #define F2FS_IOC_GETFLAGS		FS_IOC_GETFLAGS
405e9750824SNamjae Jeon #define F2FS_IOC_SETFLAGS		FS_IOC_SETFLAGS
406d49f3e89SChao Yu #define F2FS_IOC_GETVERSION		FS_IOC_GETVERSION
407e9750824SNamjae Jeon 
40888b88a66SJaegeuk Kim #define F2FS_IOCTL_MAGIC		0xf5
40988b88a66SJaegeuk Kim #define F2FS_IOC_START_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 1)
41088b88a66SJaegeuk Kim #define F2FS_IOC_COMMIT_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 2)
41102a1335fSJaegeuk Kim #define F2FS_IOC_START_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 3)
4121e84371fSJaegeuk Kim #define F2FS_IOC_RELEASE_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 4)
4131e84371fSJaegeuk Kim #define F2FS_IOC_ABORT_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 5)
414d07efb50SJaegeuk Kim #define F2FS_IOC_GARBAGE_COLLECT	_IOW(F2FS_IOCTL_MAGIC, 6, __u32)
415456b88e4SChao Yu #define F2FS_IOC_WRITE_CHECKPOINT	_IO(F2FS_IOCTL_MAGIC, 7)
416d07efb50SJaegeuk Kim #define F2FS_IOC_DEFRAGMENT		_IOWR(F2FS_IOCTL_MAGIC, 8,	\
417d07efb50SJaegeuk Kim 						struct f2fs_defragment)
4184dd6f977SJaegeuk Kim #define F2FS_IOC_MOVE_RANGE		_IOWR(F2FS_IOCTL_MAGIC, 9,	\
4194dd6f977SJaegeuk Kim 						struct f2fs_move_range)
420e066b83cSJaegeuk Kim #define F2FS_IOC_FLUSH_DEVICE		_IOW(F2FS_IOCTL_MAGIC, 10,	\
421e066b83cSJaegeuk Kim 						struct f2fs_flush_device)
42234dc77adSJaegeuk Kim #define F2FS_IOC_GARBAGE_COLLECT_RANGE	_IOW(F2FS_IOCTL_MAGIC, 11,	\
42334dc77adSJaegeuk Kim 						struct f2fs_gc_range)
424e65ef207SJaegeuk Kim #define F2FS_IOC_GET_FEATURES		_IOR(F2FS_IOCTL_MAGIC, 12, __u32)
4251ad71a27SJaegeuk Kim #define F2FS_IOC_SET_PIN_FILE		_IOW(F2FS_IOCTL_MAGIC, 13, __u32)
4261ad71a27SJaegeuk Kim #define F2FS_IOC_GET_PIN_FILE		_IOR(F2FS_IOCTL_MAGIC, 14, __u32)
427c4020b2dSChao Yu #define F2FS_IOC_PRECACHE_EXTENTS	_IO(F2FS_IOCTL_MAGIC, 15)
42804f0b2eaSQiuyang Sun #define F2FS_IOC_RESIZE_FS		_IOW(F2FS_IOCTL_MAGIC, 16, __u64)
42988b88a66SJaegeuk Kim 
4304507847cSChao Yu #define F2FS_IOC_GET_VOLUME_NAME	FS_IOC_GETFSLABEL
4314507847cSChao Yu #define F2FS_IOC_SET_VOLUME_NAME	FS_IOC_SETFSLABEL
4324507847cSChao Yu 
4330b81d077SJaegeuk Kim #define F2FS_IOC_SET_ENCRYPTION_POLICY	FS_IOC_SET_ENCRYPTION_POLICY
4340b81d077SJaegeuk Kim #define F2FS_IOC_GET_ENCRYPTION_POLICY	FS_IOC_GET_ENCRYPTION_POLICY
4350b81d077SJaegeuk Kim #define F2FS_IOC_GET_ENCRYPTION_PWSALT	FS_IOC_GET_ENCRYPTION_PWSALT
436f424f664SJaegeuk Kim 
4371abff93dSJaegeuk Kim /*
4381abff93dSJaegeuk Kim  * should be same as XFS_IOC_GOINGDOWN.
4391abff93dSJaegeuk Kim  * Flags for going down operation used by FS_IOC_GOINGDOWN
4401abff93dSJaegeuk Kim  */
4411abff93dSJaegeuk Kim #define F2FS_IOC_SHUTDOWN	_IOR('X', 125, __u32)	/* Shutdown */
4421abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_FULLSYNC	0x0	/* going down with full sync */
4431abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_METASYNC	0x1	/* going down with metadata */
4441abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_NOSYNC		0x2	/* going down */
445c912a829SJaegeuk Kim #define F2FS_GOING_DOWN_METAFLUSH	0x3	/* going down with meta flush */
4460cd6d9b0SJaegeuk Kim #define F2FS_GOING_DOWN_NEED_FSCK	0x4	/* going down to trigger fsck */
4471abff93dSJaegeuk Kim 
448e9750824SNamjae Jeon #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
449e9750824SNamjae Jeon /*
450e9750824SNamjae Jeon  * ioctl commands in 32 bit emulation
451e9750824SNamjae Jeon  */
452e9750824SNamjae Jeon #define F2FS_IOC32_GETFLAGS		FS_IOC32_GETFLAGS
453e9750824SNamjae Jeon #define F2FS_IOC32_SETFLAGS		FS_IOC32_SETFLAGS
45404ef4b62SChao Yu #define F2FS_IOC32_GETVERSION		FS_IOC32_GETVERSION
455e9750824SNamjae Jeon #endif
456e9750824SNamjae Jeon 
4572c1d0305SChao Yu #define F2FS_IOC_FSGETXATTR		FS_IOC_FSGETXATTR
4582c1d0305SChao Yu #define F2FS_IOC_FSSETXATTR		FS_IOC_FSSETXATTR
4592c1d0305SChao Yu 
46034dc77adSJaegeuk Kim struct f2fs_gc_range {
46134dc77adSJaegeuk Kim 	u32 sync;
46234dc77adSJaegeuk Kim 	u64 start;
46334dc77adSJaegeuk Kim 	u64 len;
46434dc77adSJaegeuk Kim };
46534dc77adSJaegeuk Kim 
466d323d005SChao Yu struct f2fs_defragment {
467d323d005SChao Yu 	u64 start;
468d323d005SChao Yu 	u64 len;
469d323d005SChao Yu };
470d323d005SChao Yu 
4714dd6f977SJaegeuk Kim struct f2fs_move_range {
4724dd6f977SJaegeuk Kim 	u32 dst_fd;		/* destination fd */
4734dd6f977SJaegeuk Kim 	u64 pos_in;		/* start position in src_fd */
4744dd6f977SJaegeuk Kim 	u64 pos_out;		/* start position in dst_fd */
4754dd6f977SJaegeuk Kim 	u64 len;		/* size to move */
4764dd6f977SJaegeuk Kim };
4774dd6f977SJaegeuk Kim 
478e066b83cSJaegeuk Kim struct f2fs_flush_device {
479e066b83cSJaegeuk Kim 	u32 dev_num;		/* device number to flush */
480e066b83cSJaegeuk Kim 	u32 segments;		/* # of segments to flush */
481e066b83cSJaegeuk Kim };
482e066b83cSJaegeuk Kim 
483f2470371SChao Yu /* for inline stuff */
484f2470371SChao Yu #define DEF_INLINE_RESERVED_SIZE	1
4857a2af766SChao Yu static inline int get_extra_isize(struct inode *inode);
4866afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode);
4877a2af766SChao Yu #define MAX_INLINE_DATA(inode)	(sizeof(__le32) *			\
4887a2af766SChao Yu 				(CUR_ADDRS_PER_INODE(inode) -		\
489b323fd28SChao Yu 				get_inline_xattr_addrs(inode) -	\
4906afc662eSChao Yu 				DEF_INLINE_RESERVED_SIZE))
491f2470371SChao Yu 
492f2470371SChao Yu /* for inline dir */
493f2470371SChao Yu #define NR_INLINE_DENTRY(inode)	(MAX_INLINE_DATA(inode) * BITS_PER_BYTE / \
494f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
495f2470371SChao Yu 				BITS_PER_BYTE + 1))
496f91108b8SGeert Uytterhoeven #define INLINE_DENTRY_BITMAP_SIZE(inode) \
497f91108b8SGeert Uytterhoeven 	DIV_ROUND_UP(NR_INLINE_DENTRY(inode), BITS_PER_BYTE)
498f2470371SChao Yu #define INLINE_RESERVED_SIZE(inode)	(MAX_INLINE_DATA(inode) - \
499f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
500f2470371SChao Yu 				NR_INLINE_DENTRY(inode) + \
501f2470371SChao Yu 				INLINE_DENTRY_BITMAP_SIZE(inode)))
502f2470371SChao Yu 
503e9750824SNamjae Jeon /*
50439a53e0cSJaegeuk Kim  * For INODE and NODE manager
50539a53e0cSJaegeuk Kim  */
5067b3cd7d6SJaegeuk Kim /* for directory operations */
5077b3cd7d6SJaegeuk Kim struct f2fs_dentry_ptr {
508d8c6822aSJaegeuk Kim 	struct inode *inode;
50976a9dd85SChao Yu 	void *bitmap;
5107b3cd7d6SJaegeuk Kim 	struct f2fs_dir_entry *dentry;
5117b3cd7d6SJaegeuk Kim 	__u8 (*filename)[F2FS_SLOT_LEN];
5127b3cd7d6SJaegeuk Kim 	int max;
51376a9dd85SChao Yu 	int nr_bitmap;
5147b3cd7d6SJaegeuk Kim };
5157b3cd7d6SJaegeuk Kim 
51664c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_block(struct inode *inode,
51764c24ecbSTomohiro Kusumi 		struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
5187b3cd7d6SJaegeuk Kim {
519d8c6822aSJaegeuk Kim 	d->inode = inode;
5207b3cd7d6SJaegeuk Kim 	d->max = NR_DENTRY_IN_BLOCK;
52176a9dd85SChao Yu 	d->nr_bitmap = SIZE_OF_DENTRY_BITMAP;
522c79d1520SYunlong Song 	d->bitmap = t->dentry_bitmap;
5237b3cd7d6SJaegeuk Kim 	d->dentry = t->dentry;
5247b3cd7d6SJaegeuk Kim 	d->filename = t->filename;
52564c24ecbSTomohiro Kusumi }
526cac5a3d8SDongOh Shin 
52764c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_inline(struct inode *inode,
528f2470371SChao Yu 					struct f2fs_dentry_ptr *d, void *t)
52964c24ecbSTomohiro Kusumi {
530f2470371SChao Yu 	int entry_cnt = NR_INLINE_DENTRY(inode);
531f2470371SChao Yu 	int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(inode);
532f2470371SChao Yu 	int reserved_size = INLINE_RESERVED_SIZE(inode);
533f2470371SChao Yu 
53464c24ecbSTomohiro Kusumi 	d->inode = inode;
535f2470371SChao Yu 	d->max = entry_cnt;
536f2470371SChao Yu 	d->nr_bitmap = bitmap_size;
537f2470371SChao Yu 	d->bitmap = t;
538f2470371SChao Yu 	d->dentry = t + bitmap_size + reserved_size;
539f2470371SChao Yu 	d->filename = t + bitmap_size + reserved_size +
540f2470371SChao Yu 					SIZE_OF_DIR_ENTRY * entry_cnt;
5417b3cd7d6SJaegeuk Kim }
5427b3cd7d6SJaegeuk Kim 
543dbe6a5ffSJaegeuk Kim /*
544dbe6a5ffSJaegeuk Kim  * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
545dbe6a5ffSJaegeuk Kim  * as its node offset to distinguish from index node blocks.
546dbe6a5ffSJaegeuk Kim  * But some bits are used to mark the node block.
54739a53e0cSJaegeuk Kim  */
548dbe6a5ffSJaegeuk Kim #define XATTR_NODE_OFFSET	((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
549dbe6a5ffSJaegeuk Kim 				>> OFFSET_BIT_SHIFT)
550266e97a8SJaegeuk Kim enum {
551266e97a8SJaegeuk Kim 	ALLOC_NODE,			/* allocate a new node page if needed */
552266e97a8SJaegeuk Kim 	LOOKUP_NODE,			/* look up a node without readahead */
553266e97a8SJaegeuk Kim 	LOOKUP_NODE_RA,			/*
554266e97a8SJaegeuk Kim 					 * look up a node with readahead called
5554f4124d0SChao Yu 					 * by get_data_block.
55639a53e0cSJaegeuk Kim 					 */
557266e97a8SJaegeuk Kim };
558266e97a8SJaegeuk Kim 
5597735730dSChao Yu #define DEFAULT_RETRY_IO_COUNT	8	/* maximum retry read IO count */
5607735730dSChao Yu 
5615df7731fSChao Yu /* congestion wait timeout value, default: 20ms */
5625df7731fSChao Yu #define	DEFAULT_IO_TIMEOUT	(msecs_to_jiffies(20))
5635df7731fSChao Yu 
564af033b2aSChao Yu /* maximum retry quota flush count */
565af033b2aSChao Yu #define DEFAULT_RETRY_QUOTA_FLUSH_COUNT		8
566af033b2aSChao Yu 
567a6db67f0SJaegeuk Kim #define F2FS_LINK_MAX	0xffffffff	/* maximum link count per file */
56839a53e0cSJaegeuk Kim 
569817202d9SChao Yu #define MAX_DIR_RA_PAGES	4	/* maximum ra pages of dir */
570817202d9SChao Yu 
57139a53e0cSJaegeuk Kim /* for in-memory extent cache entry */
57213054c54SChao Yu #define F2FS_MIN_EXTENT_LEN	64	/* minimum extent length */
57313054c54SChao Yu 
57413054c54SChao Yu /* number of extent info in extent cache we try to shrink */
57513054c54SChao Yu #define EXTENT_CACHE_SHRINK_NUMBER	128
576c11abd1aSJaegeuk Kim 
57754c2258cSChao Yu struct rb_entry {
57854c2258cSChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
57954c2258cSChao Yu 	unsigned int ofs;		/* start offset of the entry */
58054c2258cSChao Yu 	unsigned int len;		/* length of the entry */
58154c2258cSChao Yu };
58254c2258cSChao Yu 
58339a53e0cSJaegeuk Kim struct extent_info {
58439a53e0cSJaegeuk Kim 	unsigned int fofs;		/* start offset in a file */
585111d2495SMasanari Iida 	unsigned int len;		/* length of the extent */
58654c2258cSChao Yu 	u32 blk;			/* start block address of the extent */
58739a53e0cSJaegeuk Kim };
58839a53e0cSJaegeuk Kim 
58913054c54SChao Yu struct extent_node {
590c0362117SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
59113054c54SChao Yu 	struct extent_info ei;		/* extent info */
59254c2258cSChao Yu 	struct list_head list;		/* node in global extent list of sbi */
593201ef5e0SHou Pengyang 	struct extent_tree *et;		/* extent tree pointer */
59413054c54SChao Yu };
59513054c54SChao Yu 
59613054c54SChao Yu struct extent_tree {
59713054c54SChao Yu 	nid_t ino;			/* inode number */
5984dada3fdSChao Yu 	struct rb_root_cached root;	/* root of extent info rb-tree */
59962c8af65SChao Yu 	struct extent_node *cached_en;	/* recently accessed extent node */
6003e72f721SJaegeuk Kim 	struct extent_info largest;	/* largested extent info */
601137d09f0SJaegeuk Kim 	struct list_head list;		/* to be used by sbi->zombie_list */
60213054c54SChao Yu 	rwlock_t lock;			/* protect extent info rb-tree */
60368e35385SChao Yu 	atomic_t node_cnt;		/* # of extent node in rb-tree*/
604b430f726SZhikang Zhang 	bool largest_updated;		/* largest extent updated */
60513054c54SChao Yu };
60613054c54SChao Yu 
60739a53e0cSJaegeuk Kim /*
608003a3e1dSJaegeuk Kim  * This structure is taken from ext4_map_blocks.
609003a3e1dSJaegeuk Kim  *
610003a3e1dSJaegeuk Kim  * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
611003a3e1dSJaegeuk Kim  */
612003a3e1dSJaegeuk Kim #define F2FS_MAP_NEW		(1 << BH_New)
613003a3e1dSJaegeuk Kim #define F2FS_MAP_MAPPED		(1 << BH_Mapped)
6147f63eb77SJaegeuk Kim #define F2FS_MAP_UNWRITTEN	(1 << BH_Unwritten)
6157f63eb77SJaegeuk Kim #define F2FS_MAP_FLAGS		(F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
6167f63eb77SJaegeuk Kim 				F2FS_MAP_UNWRITTEN)
617003a3e1dSJaegeuk Kim 
618003a3e1dSJaegeuk Kim struct f2fs_map_blocks {
619003a3e1dSJaegeuk Kim 	block_t m_pblk;
620003a3e1dSJaegeuk Kim 	block_t m_lblk;
621003a3e1dSJaegeuk Kim 	unsigned int m_len;
622003a3e1dSJaegeuk Kim 	unsigned int m_flags;
623da85985cSChao Yu 	pgoff_t *m_next_pgofs;		/* point next possible non-hole pgofs */
624c4020b2dSChao Yu 	pgoff_t *m_next_extent;		/* point to next possible extent */
625d5097be5SHyunchul Lee 	int m_seg_type;
626f9d6d059SChao Yu 	bool m_may_create;		/* indicate it is from write path */
627003a3e1dSJaegeuk Kim };
628003a3e1dSJaegeuk Kim 
629e2b4e2bcSChao Yu /* for flag in get_data_block */
630f2220c7fSQiuyang Sun enum {
631f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_DEFAULT,
632f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_FIEMAP,
633f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_BMAP,
6340a4daae5SJaegeuk Kim 	F2FS_GET_BLOCK_DIO,
635f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_DIO,
636f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_AIO,
637c4020b2dSChao Yu 	F2FS_GET_BLOCK_PRECACHE,
638f2220c7fSQiuyang Sun };
639e2b4e2bcSChao Yu 
640003a3e1dSJaegeuk Kim /*
64139a53e0cSJaegeuk Kim  * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
64239a53e0cSJaegeuk Kim  */
64339a53e0cSJaegeuk Kim #define FADVISE_COLD_BIT	0x01
644354a3399SJaegeuk Kim #define FADVISE_LOST_PINO_BIT	0x02
645cde4de12SJaegeuk Kim #define FADVISE_ENCRYPT_BIT	0x04
646e7d55452SJaegeuk Kim #define FADVISE_ENC_NAME_BIT	0x08
64726787236SJaegeuk Kim #define FADVISE_KEEP_SIZE_BIT	0x10
648b6a06cbbSChao Yu #define FADVISE_HOT_BIT		0x20
64995ae251fSEric Biggers #define FADVISE_VERITY_BIT	0x40
65039a53e0cSJaegeuk Kim 
651797c1cb5SChao Yu #define FADVISE_MODIFIABLE_BITS	(FADVISE_COLD_BIT | FADVISE_HOT_BIT)
652797c1cb5SChao Yu 
653b5492af7SJaegeuk Kim #define file_is_cold(inode)	is_file(inode, FADVISE_COLD_BIT)
654b5492af7SJaegeuk Kim #define file_wrong_pino(inode)	is_file(inode, FADVISE_LOST_PINO_BIT)
655b5492af7SJaegeuk Kim #define file_set_cold(inode)	set_file(inode, FADVISE_COLD_BIT)
656b5492af7SJaegeuk Kim #define file_lost_pino(inode)	set_file(inode, FADVISE_LOST_PINO_BIT)
657b5492af7SJaegeuk Kim #define file_clear_cold(inode)	clear_file(inode, FADVISE_COLD_BIT)
658b5492af7SJaegeuk Kim #define file_got_pino(inode)	clear_file(inode, FADVISE_LOST_PINO_BIT)
659cde4de12SJaegeuk Kim #define file_is_encrypt(inode)	is_file(inode, FADVISE_ENCRYPT_BIT)
660cde4de12SJaegeuk Kim #define file_set_encrypt(inode)	set_file(inode, FADVISE_ENCRYPT_BIT)
661cde4de12SJaegeuk Kim #define file_clear_encrypt(inode) clear_file(inode, FADVISE_ENCRYPT_BIT)
662e7d55452SJaegeuk Kim #define file_enc_name(inode)	is_file(inode, FADVISE_ENC_NAME_BIT)
663e7d55452SJaegeuk Kim #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
66426787236SJaegeuk Kim #define file_keep_isize(inode)	is_file(inode, FADVISE_KEEP_SIZE_BIT)
66526787236SJaegeuk Kim #define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
666b6a06cbbSChao Yu #define file_is_hot(inode)	is_file(inode, FADVISE_HOT_BIT)
667b6a06cbbSChao Yu #define file_set_hot(inode)	set_file(inode, FADVISE_HOT_BIT)
668b6a06cbbSChao Yu #define file_clear_hot(inode)	clear_file(inode, FADVISE_HOT_BIT)
66995ae251fSEric Biggers #define file_is_verity(inode)	is_file(inode, FADVISE_VERITY_BIT)
67095ae251fSEric Biggers #define file_set_verity(inode)	set_file(inode, FADVISE_VERITY_BIT)
671cde4de12SJaegeuk Kim 
672ab9fa662SJaegeuk Kim #define DEF_DIR_LEVEL		0
673ab9fa662SJaegeuk Kim 
6742ef79ecbSChao Yu enum {
6752ef79ecbSChao Yu 	GC_FAILURE_PIN,
6762ef79ecbSChao Yu 	GC_FAILURE_ATOMIC,
6772ef79ecbSChao Yu 	MAX_GC_FAILURE
6782ef79ecbSChao Yu };
6792ef79ecbSChao Yu 
68039a53e0cSJaegeuk Kim struct f2fs_inode_info {
68139a53e0cSJaegeuk Kim 	struct inode vfs_inode;		/* serve a vfs inode */
68239a53e0cSJaegeuk Kim 	unsigned long i_flags;		/* keep an inode flags for ioctl */
68339a53e0cSJaegeuk Kim 	unsigned char i_advise;		/* use to give file attribute hints */
68438431545SJaegeuk Kim 	unsigned char i_dir_level;	/* use for dentry level for large dir */
6851ad71a27SJaegeuk Kim 	unsigned int i_current_depth;	/* only for directory depth */
6862ef79ecbSChao Yu 	/* for gc failure statistic */
6872ef79ecbSChao Yu 	unsigned int i_gc_failures[MAX_GC_FAILURE];
6886666e6aaSJaegeuk Kim 	unsigned int i_pino;		/* parent inode number */
68939a53e0cSJaegeuk Kim 	umode_t i_acl_mode;		/* keep file acl mode temporarily */
69039a53e0cSJaegeuk Kim 
69139a53e0cSJaegeuk Kim 	/* Use below internally in f2fs*/
69239a53e0cSJaegeuk Kim 	unsigned long flags;		/* use to pass per-file flags */
693d928bfbfSJaegeuk Kim 	struct rw_semaphore i_sem;	/* protect fi info */
694204706c7SJaegeuk Kim 	atomic_t dirty_pages;		/* # of dirty pages */
69539a53e0cSJaegeuk Kim 	f2fs_hash_t chash;		/* hash value of given file name */
69639a53e0cSJaegeuk Kim 	unsigned int clevel;		/* maximum level of given file name */
69788c5c13aSJaegeuk Kim 	struct task_struct *task;	/* lookup and create consistency */
698b0af6d49SChao Yu 	struct task_struct *cp_task;	/* separate cp/wb IO stats*/
69939a53e0cSJaegeuk Kim 	nid_t i_xattr_nid;		/* node id that contains xattrs */
70026de9b11SJaegeuk Kim 	loff_t	last_disk_size;		/* lastly written file size */
701c10c9820SChao Yu 	spinlock_t i_size_lock;		/* protect last_disk_size */
70288b88a66SJaegeuk Kim 
7030abd675eSChao Yu #ifdef CONFIG_QUOTA
7040abd675eSChao Yu 	struct dquot *i_dquot[MAXQUOTAS];
7050abd675eSChao Yu 
7060abd675eSChao Yu 	/* quota space reservation, managed internally by quota code */
7070abd675eSChao Yu 	qsize_t i_reserved_quota;
7080abd675eSChao Yu #endif
7090f18b462SJaegeuk Kim 	struct list_head dirty_list;	/* dirty list for dirs and files */
7100f18b462SJaegeuk Kim 	struct list_head gdirty_list;	/* linked in global dirty list */
71157864ae5SJaegeuk Kim 	struct list_head inmem_ilist;	/* list for inmem inodes */
71288b88a66SJaegeuk Kim 	struct list_head inmem_pages;	/* inmemory pages managed by f2fs */
7137a10f017SJaegeuk Kim 	struct task_struct *inmem_task;	/* store inmemory task */
71488b88a66SJaegeuk Kim 	struct mutex inmem_lock;	/* lock for inmemory pages */
7153e72f721SJaegeuk Kim 	struct extent_tree *extent_tree;	/* cached extent_tree entry */
716b2532c69SChao Yu 
717b2532c69SChao Yu 	/* avoid racing between foreground op and gc */
718b2532c69SChao Yu 	struct rw_semaphore i_gc_rwsem[2];
7195a3a2d83SQiuyang Sun 	struct rw_semaphore i_mmap_sem;
72027161f13SYunlei He 	struct rw_semaphore i_xattr_sem; /* avoid racing between reading and changing EAs */
721f2470371SChao Yu 
7227a2af766SChao Yu 	int i_extra_isize;		/* size of extra space located in i_addr */
7235c57132eSChao Yu 	kprojid_t i_projid;		/* id for project quota */
7246afc662eSChao Yu 	int i_inline_xattr_size;	/* inline xattr size */
72524b81dfcSArnd Bergmann 	struct timespec64 i_crtime;	/* inode creation time */
72624b81dfcSArnd Bergmann 	struct timespec64 i_disk_time[4];/* inode disk times */
7274c8ff709SChao Yu 
7284c8ff709SChao Yu 	/* for file compress */
7294c8ff709SChao Yu 	u64 i_compr_blocks;			/* # of compressed blocks */
7304c8ff709SChao Yu 	unsigned char i_compress_algorithm;	/* algorithm type */
7314c8ff709SChao Yu 	unsigned char i_log_cluster_size;	/* log of cluster size */
7324c8ff709SChao Yu 	unsigned int i_cluster_size;		/* cluster size */
73339a53e0cSJaegeuk Kim };
73439a53e0cSJaegeuk Kim 
73539a53e0cSJaegeuk Kim static inline void get_extent_info(struct extent_info *ext,
736bd933d4fSChao Yu 					struct f2fs_extent *i_ext)
73739a53e0cSJaegeuk Kim {
738bd933d4fSChao Yu 	ext->fofs = le32_to_cpu(i_ext->fofs);
739bd933d4fSChao Yu 	ext->blk = le32_to_cpu(i_ext->blk);
740bd933d4fSChao Yu 	ext->len = le32_to_cpu(i_ext->len);
74139a53e0cSJaegeuk Kim }
74239a53e0cSJaegeuk Kim 
74339a53e0cSJaegeuk Kim static inline void set_raw_extent(struct extent_info *ext,
74439a53e0cSJaegeuk Kim 					struct f2fs_extent *i_ext)
74539a53e0cSJaegeuk Kim {
74639a53e0cSJaegeuk Kim 	i_ext->fofs = cpu_to_le32(ext->fofs);
7474d0b0bd4SChao Yu 	i_ext->blk = cpu_to_le32(ext->blk);
74839a53e0cSJaegeuk Kim 	i_ext->len = cpu_to_le32(ext->len);
74939a53e0cSJaegeuk Kim }
75039a53e0cSJaegeuk Kim 
751429511cdSChao Yu static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
752429511cdSChao Yu 						u32 blk, unsigned int len)
753429511cdSChao Yu {
754429511cdSChao Yu 	ei->fofs = fofs;
755429511cdSChao Yu 	ei->blk = blk;
756429511cdSChao Yu 	ei->len = len;
757429511cdSChao Yu }
758429511cdSChao Yu 
759004b6862SChao Yu static inline bool __is_discard_mergeable(struct discard_info *back,
76035ec7d57SChao Yu 			struct discard_info *front, unsigned int max_len)
761004b6862SChao Yu {
7629a997188SJaegeuk Kim 	return (back->lstart + back->len == front->lstart) &&
76335ec7d57SChao Yu 		(back->len + front->len <= max_len);
764004b6862SChao Yu }
765004b6862SChao Yu 
766004b6862SChao Yu static inline bool __is_discard_back_mergeable(struct discard_info *cur,
76735ec7d57SChao Yu 			struct discard_info *back, unsigned int max_len)
768004b6862SChao Yu {
76935ec7d57SChao Yu 	return __is_discard_mergeable(back, cur, max_len);
770004b6862SChao Yu }
771004b6862SChao Yu 
772004b6862SChao Yu static inline bool __is_discard_front_mergeable(struct discard_info *cur,
77335ec7d57SChao Yu 			struct discard_info *front, unsigned int max_len)
774004b6862SChao Yu {
77535ec7d57SChao Yu 	return __is_discard_mergeable(cur, front, max_len);
776004b6862SChao Yu }
777004b6862SChao Yu 
778429511cdSChao Yu static inline bool __is_extent_mergeable(struct extent_info *back,
779429511cdSChao Yu 						struct extent_info *front)
780429511cdSChao Yu {
781429511cdSChao Yu 	return (back->fofs + back->len == front->fofs &&
782429511cdSChao Yu 			back->blk + back->len == front->blk);
783429511cdSChao Yu }
784429511cdSChao Yu 
785429511cdSChao Yu static inline bool __is_back_mergeable(struct extent_info *cur,
786429511cdSChao Yu 						struct extent_info *back)
787429511cdSChao Yu {
788429511cdSChao Yu 	return __is_extent_mergeable(back, cur);
789429511cdSChao Yu }
790429511cdSChao Yu 
791429511cdSChao Yu static inline bool __is_front_mergeable(struct extent_info *cur,
792429511cdSChao Yu 						struct extent_info *front)
793429511cdSChao Yu {
794429511cdSChao Yu 	return __is_extent_mergeable(cur, front);
795429511cdSChao Yu }
796429511cdSChao Yu 
797cac5a3d8SDongOh Shin extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
798b430f726SZhikang Zhang static inline void __try_update_largest_extent(struct extent_tree *et,
799b430f726SZhikang Zhang 						struct extent_node *en)
8004abd3f5aSChao Yu {
801205b9822SJaegeuk Kim 	if (en->ei.len > et->largest.len) {
8024abd3f5aSChao Yu 		et->largest = en->ei;
803b430f726SZhikang Zhang 		et->largest_updated = true;
804205b9822SJaegeuk Kim 	}
8054abd3f5aSChao Yu }
8064abd3f5aSChao Yu 
8079a4ffdf5SChao Yu /*
8089a4ffdf5SChao Yu  * For free nid management
8099a4ffdf5SChao Yu  */
8109a4ffdf5SChao Yu enum nid_state {
8119a4ffdf5SChao Yu 	FREE_NID,		/* newly added to free nid list */
8129a4ffdf5SChao Yu 	PREALLOC_NID,		/* it is preallocated */
8139a4ffdf5SChao Yu 	MAX_NID_STATE,
814b8559dc2SChao Yu };
815b8559dc2SChao Yu 
81639a53e0cSJaegeuk Kim struct f2fs_nm_info {
81739a53e0cSJaegeuk Kim 	block_t nat_blkaddr;		/* base disk address of NAT */
81839a53e0cSJaegeuk Kim 	nid_t max_nid;			/* maximum possible node ids */
81904d47e67SChao Yu 	nid_t available_nids;		/* # of available node ids */
82039a53e0cSJaegeuk Kim 	nid_t next_scan_nid;		/* the next nid to be scanned */
821cdfc41c1SJaegeuk Kim 	unsigned int ram_thresh;	/* control the memory footprint */
822ea1a29a0SChao Yu 	unsigned int ra_nid_pages;	/* # of nid pages to be readaheaded */
8232304cb0cSChao Yu 	unsigned int dirty_nats_ratio;	/* control dirty nats ratio threshold */
82439a53e0cSJaegeuk Kim 
82539a53e0cSJaegeuk Kim 	/* NAT cache management */
82639a53e0cSJaegeuk Kim 	struct radix_tree_root nat_root;/* root of the nat entry cache */
827309cc2b6SJaegeuk Kim 	struct radix_tree_root nat_set_root;/* root of the nat set cache */
828b873b798SJaegeuk Kim 	struct rw_semaphore nat_tree_lock;	/* protect nat_tree_lock */
82939a53e0cSJaegeuk Kim 	struct list_head nat_entries;	/* cached nat entry list (clean) */
83022969158SChao Yu 	spinlock_t nat_list_lock;	/* protect clean nat entry list */
831309cc2b6SJaegeuk Kim 	unsigned int nat_cnt;		/* the # of cached nat entries */
832aec71382SChao Yu 	unsigned int dirty_nat_cnt;	/* total num of nat entries in set */
83322ad0b6aSJaegeuk Kim 	unsigned int nat_blocks;	/* # of nat blocks */
83439a53e0cSJaegeuk Kim 
83539a53e0cSJaegeuk Kim 	/* free node ids management */
8368a7ed66aSJaegeuk Kim 	struct radix_tree_root free_nid_root;/* root of the free_nid cache */
8379a4ffdf5SChao Yu 	struct list_head free_nid_list;		/* list for free nids excluding preallocated nids */
8389a4ffdf5SChao Yu 	unsigned int nid_cnt[MAX_NID_STATE];	/* the number of free node id */
839b8559dc2SChao Yu 	spinlock_t nid_list_lock;	/* protect nid lists ops */
84039a53e0cSJaegeuk Kim 	struct mutex build_lock;	/* lock for build free nids */
841bb1105e4SJaegeuk Kim 	unsigned char **free_nid_bitmap;
8424ac91242SChao Yu 	unsigned char *nat_block_bitmap;
843586d1492SChao Yu 	unsigned short *free_nid_count;	/* free nid count of NAT block */
84439a53e0cSJaegeuk Kim 
84539a53e0cSJaegeuk Kim 	/* for checkpoint */
84639a53e0cSJaegeuk Kim 	char *nat_bitmap;		/* NAT bitmap pointer */
84722ad0b6aSJaegeuk Kim 
84822ad0b6aSJaegeuk Kim 	unsigned int nat_bits_blocks;	/* # of nat bits blocks */
84922ad0b6aSJaegeuk Kim 	unsigned char *nat_bits;	/* NAT bits blocks */
85022ad0b6aSJaegeuk Kim 	unsigned char *full_nat_bits;	/* full NAT pages */
85122ad0b6aSJaegeuk Kim 	unsigned char *empty_nat_bits;	/* empty NAT pages */
852599a09b2SChao Yu #ifdef CONFIG_F2FS_CHECK_FS
853599a09b2SChao Yu 	char *nat_bitmap_mir;		/* NAT bitmap mirror */
854599a09b2SChao Yu #endif
85539a53e0cSJaegeuk Kim 	int bitmap_size;		/* bitmap size */
85639a53e0cSJaegeuk Kim };
85739a53e0cSJaegeuk Kim 
85839a53e0cSJaegeuk Kim /*
85939a53e0cSJaegeuk Kim  * this structure is used as one of function parameters.
86039a53e0cSJaegeuk Kim  * all the information are dedicated to a given direct node block determined
86139a53e0cSJaegeuk Kim  * by the data offset in a file.
86239a53e0cSJaegeuk Kim  */
86339a53e0cSJaegeuk Kim struct dnode_of_data {
86439a53e0cSJaegeuk Kim 	struct inode *inode;		/* vfs inode pointer */
86539a53e0cSJaegeuk Kim 	struct page *inode_page;	/* its inode page, NULL is possible */
86639a53e0cSJaegeuk Kim 	struct page *node_page;		/* cached direct node page */
86739a53e0cSJaegeuk Kim 	nid_t nid;			/* node id of the direct node block */
86839a53e0cSJaegeuk Kim 	unsigned int ofs_in_node;	/* data offset in the node page */
86939a53e0cSJaegeuk Kim 	bool inode_page_locked;		/* inode page is locked or not */
87093bae099SJaegeuk Kim 	bool node_changed;		/* is node block changed */
8713cf45747SChao Yu 	char cur_level;			/* level of hole node page */
8723cf45747SChao Yu 	char max_level;			/* level of current page located */
87339a53e0cSJaegeuk Kim 	block_t	data_blkaddr;		/* block address of the node block */
87439a53e0cSJaegeuk Kim };
87539a53e0cSJaegeuk Kim 
87639a53e0cSJaegeuk Kim static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
87739a53e0cSJaegeuk Kim 		struct page *ipage, struct page *npage, nid_t nid)
87839a53e0cSJaegeuk Kim {
879d66d1f76SJaegeuk Kim 	memset(dn, 0, sizeof(*dn));
88039a53e0cSJaegeuk Kim 	dn->inode = inode;
88139a53e0cSJaegeuk Kim 	dn->inode_page = ipage;
88239a53e0cSJaegeuk Kim 	dn->node_page = npage;
88339a53e0cSJaegeuk Kim 	dn->nid = nid;
88439a53e0cSJaegeuk Kim }
88539a53e0cSJaegeuk Kim 
88639a53e0cSJaegeuk Kim /*
88739a53e0cSJaegeuk Kim  * For SIT manager
88839a53e0cSJaegeuk Kim  *
88939a53e0cSJaegeuk Kim  * By default, there are 6 active log areas across the whole main area.
89039a53e0cSJaegeuk Kim  * When considering hot and cold data separation to reduce cleaning overhead,
89139a53e0cSJaegeuk Kim  * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
89239a53e0cSJaegeuk Kim  * respectively.
89339a53e0cSJaegeuk Kim  * In the current design, you should not change the numbers intentionally.
89439a53e0cSJaegeuk Kim  * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
89539a53e0cSJaegeuk Kim  * logs individually according to the underlying devices. (default: 6)
89639a53e0cSJaegeuk Kim  * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
89739a53e0cSJaegeuk Kim  * data and 8 for node logs.
89839a53e0cSJaegeuk Kim  */
89939a53e0cSJaegeuk Kim #define	NR_CURSEG_DATA_TYPE	(3)
90039a53e0cSJaegeuk Kim #define NR_CURSEG_NODE_TYPE	(3)
90139a53e0cSJaegeuk Kim #define NR_CURSEG_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
90239a53e0cSJaegeuk Kim 
90339a53e0cSJaegeuk Kim enum {
90439a53e0cSJaegeuk Kim 	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
90539a53e0cSJaegeuk Kim 	CURSEG_WARM_DATA,	/* data blocks */
90639a53e0cSJaegeuk Kim 	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
90739a53e0cSJaegeuk Kim 	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
90839a53e0cSJaegeuk Kim 	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
90939a53e0cSJaegeuk Kim 	CURSEG_COLD_NODE,	/* indirect node blocks */
91038aa0889SJaegeuk Kim 	NO_CHECK_TYPE,
911f5a53edcSJaegeuk Kim 	CURSEG_COLD_DATA_PINNED,/* cold data for pinned file */
91239a53e0cSJaegeuk Kim };
91339a53e0cSJaegeuk Kim 
9146b4afdd7SJaegeuk Kim struct flush_cmd {
9156b4afdd7SJaegeuk Kim 	struct completion wait;
916721bd4d5SGu Zheng 	struct llist_node llnode;
91739d787beSChao Yu 	nid_t ino;
9186b4afdd7SJaegeuk Kim 	int ret;
9196b4afdd7SJaegeuk Kim };
9206b4afdd7SJaegeuk Kim 
921a688b9d9SGu Zheng struct flush_cmd_control {
922a688b9d9SGu Zheng 	struct task_struct *f2fs_issue_flush;	/* flush thread */
923a688b9d9SGu Zheng 	wait_queue_head_t flush_wait_queue;	/* waiting queue for wake-up */
9248b8dd65fSChao Yu 	atomic_t issued_flush;			/* # of issued flushes */
92572691af6SJaegeuk Kim 	atomic_t queued_flush;			/* # of queued flushes */
926721bd4d5SGu Zheng 	struct llist_head issue_list;		/* list for command issue */
927721bd4d5SGu Zheng 	struct llist_node *dispatch_list;	/* list for command dispatch */
928a688b9d9SGu Zheng };
929a688b9d9SGu Zheng 
93039a53e0cSJaegeuk Kim struct f2fs_sm_info {
93139a53e0cSJaegeuk Kim 	struct sit_info *sit_info;		/* whole segment information */
93239a53e0cSJaegeuk Kim 	struct free_segmap_info *free_info;	/* free segment information */
93339a53e0cSJaegeuk Kim 	struct dirty_seglist_info *dirty_info;	/* dirty segment information */
93439a53e0cSJaegeuk Kim 	struct curseg_info *curseg_array;	/* active segment information */
93539a53e0cSJaegeuk Kim 
9362b60311dSChao Yu 	struct rw_semaphore curseg_lock;	/* for preventing curseg change */
9372b60311dSChao Yu 
93839a53e0cSJaegeuk Kim 	block_t seg0_blkaddr;		/* block address of 0'th segment */
93939a53e0cSJaegeuk Kim 	block_t main_blkaddr;		/* start block address of main area */
94039a53e0cSJaegeuk Kim 	block_t ssa_blkaddr;		/* start block address of SSA area */
94139a53e0cSJaegeuk Kim 
94239a53e0cSJaegeuk Kim 	unsigned int segment_count;	/* total # of segments */
94339a53e0cSJaegeuk Kim 	unsigned int main_segments;	/* # of segments in main area */
94439a53e0cSJaegeuk Kim 	unsigned int reserved_segments;	/* # of reserved segments */
94539a53e0cSJaegeuk Kim 	unsigned int ovp_segments;	/* # of overprovision segments */
94681eb8d6eSJaegeuk Kim 
94781eb8d6eSJaegeuk Kim 	/* a threshold to reclaim prefree segments */
94881eb8d6eSJaegeuk Kim 	unsigned int rec_prefree_segments;
9497fd9e544SJaegeuk Kim 
950bba681cbSJaegeuk Kim 	/* for batched trimming */
951bba681cbSJaegeuk Kim 	unsigned int trim_sections;		/* # of sections to trim */
952bba681cbSJaegeuk Kim 
953184a5cd2SChao Yu 	struct list_head sit_entry_set;	/* sit entry set list */
954184a5cd2SChao Yu 
955216fbd64SJaegeuk Kim 	unsigned int ipu_policy;	/* in-place-update policy */
956216fbd64SJaegeuk Kim 	unsigned int min_ipu_util;	/* in-place-update threshold */
957c1ce1b02SJaegeuk Kim 	unsigned int min_fsync_blocks;	/* threshold for fsync */
958853137ceSJaegeuk Kim 	unsigned int min_seq_blocks;	/* threshold for sequential blocks */
959ef095d19SJaegeuk Kim 	unsigned int min_hot_blocks;	/* threshold for hot block allocation */
960a2a12b67SChao Yu 	unsigned int min_ssr_sections;	/* threshold to trigger SSR allocation */
9616b4afdd7SJaegeuk Kim 
9626b4afdd7SJaegeuk Kim 	/* for flush command control */
963b01a9201SJaegeuk Kim 	struct flush_cmd_control *fcc_info;
964a688b9d9SGu Zheng 
9650b54fb84SJaegeuk Kim 	/* for discard command control */
9660b54fb84SJaegeuk Kim 	struct discard_cmd_control *dcc_info;
96739a53e0cSJaegeuk Kim };
96839a53e0cSJaegeuk Kim 
96939a53e0cSJaegeuk Kim /*
97039a53e0cSJaegeuk Kim  * For superblock
97139a53e0cSJaegeuk Kim  */
97239a53e0cSJaegeuk Kim /*
97339a53e0cSJaegeuk Kim  * COUNT_TYPE for monitoring
97439a53e0cSJaegeuk Kim  *
97539a53e0cSJaegeuk Kim  * f2fs monitors the number of several block types such as on-writeback,
97639a53e0cSJaegeuk Kim  * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
97739a53e0cSJaegeuk Kim  */
97836951b38SChao Yu #define WB_DATA_TYPE(p)	(__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
97939a53e0cSJaegeuk Kim enum count_type {
98039a53e0cSJaegeuk Kim 	F2FS_DIRTY_DENTS,
981c227f912SChao Yu 	F2FS_DIRTY_DATA,
9822c8a4a28SJaegeuk Kim 	F2FS_DIRTY_QDATA,
98339a53e0cSJaegeuk Kim 	F2FS_DIRTY_NODES,
98439a53e0cSJaegeuk Kim 	F2FS_DIRTY_META,
9858dcf2ff7SJaegeuk Kim 	F2FS_INMEM_PAGES,
9860f18b462SJaegeuk Kim 	F2FS_DIRTY_IMETA,
98736951b38SChao Yu 	F2FS_WB_CP_DATA,
98836951b38SChao Yu 	F2FS_WB_DATA,
9895f9abab4SJaegeuk Kim 	F2FS_RD_DATA,
9905f9abab4SJaegeuk Kim 	F2FS_RD_NODE,
9915f9abab4SJaegeuk Kim 	F2FS_RD_META,
99202b16d0aSChao Yu 	F2FS_DIO_WRITE,
99302b16d0aSChao Yu 	F2FS_DIO_READ,
99439a53e0cSJaegeuk Kim 	NR_COUNT_TYPE,
99539a53e0cSJaegeuk Kim };
99639a53e0cSJaegeuk Kim 
99739a53e0cSJaegeuk Kim /*
998e1c42045Sarter97  * The below are the page types of bios used in submit_bio().
99939a53e0cSJaegeuk Kim  * The available types are:
100039a53e0cSJaegeuk Kim  * DATA			User data pages. It operates as async mode.
100139a53e0cSJaegeuk Kim  * NODE			Node pages. It operates as async mode.
100239a53e0cSJaegeuk Kim  * META			FS metadata pages such as SIT, NAT, CP.
100339a53e0cSJaegeuk Kim  * NR_PAGE_TYPE		The number of page types.
100439a53e0cSJaegeuk Kim  * META_FLUSH		Make sure the previous pages are written
100539a53e0cSJaegeuk Kim  *			with waiting the bio's completion
100639a53e0cSJaegeuk Kim  * ...			Only can be used with META.
100739a53e0cSJaegeuk Kim  */
10087d5e5109SJaegeuk Kim #define PAGE_TYPE_OF_BIO(type)	((type) > META ? META : (type))
100939a53e0cSJaegeuk Kim enum page_type {
101039a53e0cSJaegeuk Kim 	DATA,
101139a53e0cSJaegeuk Kim 	NODE,
101239a53e0cSJaegeuk Kim 	META,
101339a53e0cSJaegeuk Kim 	NR_PAGE_TYPE,
101439a53e0cSJaegeuk Kim 	META_FLUSH,
10158ce67cb0SJaegeuk Kim 	INMEM,		/* the below types are used by tracepoints only. */
10168ce67cb0SJaegeuk Kim 	INMEM_DROP,
10178c242db9SJaegeuk Kim 	INMEM_INVALIDATE,
101828bc106bSChao Yu 	INMEM_REVOKE,
10198ce67cb0SJaegeuk Kim 	IPU,
10208ce67cb0SJaegeuk Kim 	OPU,
102139a53e0cSJaegeuk Kim };
102239a53e0cSJaegeuk Kim 
1023a912b54dSJaegeuk Kim enum temp_type {
1024a912b54dSJaegeuk Kim 	HOT = 0,	/* must be zero for meta bio */
1025a912b54dSJaegeuk Kim 	WARM,
1026a912b54dSJaegeuk Kim 	COLD,
1027a912b54dSJaegeuk Kim 	NR_TEMP_TYPE,
1028a912b54dSJaegeuk Kim };
1029a912b54dSJaegeuk Kim 
1030cc15620bSJaegeuk Kim enum need_lock_type {
1031cc15620bSJaegeuk Kim 	LOCK_REQ = 0,
1032cc15620bSJaegeuk Kim 	LOCK_DONE,
1033cc15620bSJaegeuk Kim 	LOCK_RETRY,
1034cc15620bSJaegeuk Kim };
1035cc15620bSJaegeuk Kim 
1036a5fd5050SChao Yu enum cp_reason_type {
1037a5fd5050SChao Yu 	CP_NO_NEEDED,
1038a5fd5050SChao Yu 	CP_NON_REGULAR,
10394c8ff709SChao Yu 	CP_COMPRESSED,
1040a5fd5050SChao Yu 	CP_HARDLINK,
1041a5fd5050SChao Yu 	CP_SB_NEED_CP,
1042a5fd5050SChao Yu 	CP_WRONG_PINO,
1043a5fd5050SChao Yu 	CP_NO_SPC_ROLL,
1044a5fd5050SChao Yu 	CP_NODE_NEED_CP,
1045a5fd5050SChao Yu 	CP_FASTBOOT_MODE,
1046a5fd5050SChao Yu 	CP_SPEC_LOG_NUM,
10470a007b97SJaegeuk Kim 	CP_RECOVER_DIR,
1048a5fd5050SChao Yu };
1049a5fd5050SChao Yu 
1050b0af6d49SChao Yu enum iostat_type {
1051b0af6d49SChao Yu 	APP_DIRECT_IO,			/* app direct IOs */
1052b0af6d49SChao Yu 	APP_BUFFERED_IO,		/* app buffered IOs */
1053b0af6d49SChao Yu 	APP_WRITE_IO,			/* app write IOs */
1054b0af6d49SChao Yu 	APP_MAPPED_IO,			/* app mapped IOs */
1055b0af6d49SChao Yu 	FS_DATA_IO,			/* data IOs from kworker/fsync/reclaimer */
1056b0af6d49SChao Yu 	FS_NODE_IO,			/* node IOs from kworker/fsync/reclaimer */
1057b0af6d49SChao Yu 	FS_META_IO,			/* meta IOs from kworker/reclaimer */
1058b0af6d49SChao Yu 	FS_GC_DATA_IO,			/* data IOs from forground gc */
1059b0af6d49SChao Yu 	FS_GC_NODE_IO,			/* node IOs from forground gc */
1060b0af6d49SChao Yu 	FS_CP_DATA_IO,			/* data IOs from checkpoint */
1061b0af6d49SChao Yu 	FS_CP_NODE_IO,			/* node IOs from checkpoint */
1062b0af6d49SChao Yu 	FS_CP_META_IO,			/* meta IOs from checkpoint */
1063b0af6d49SChao Yu 	FS_DISCARD,			/* discard */
1064b0af6d49SChao Yu 	NR_IO_TYPE,
1065b0af6d49SChao Yu };
1066b0af6d49SChao Yu 
1067458e6197SJaegeuk Kim struct f2fs_io_info {
106805ca3632SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs_sb_info pointer */
106939d787beSChao Yu 	nid_t ino;		/* inode number */
1070458e6197SJaegeuk Kim 	enum page_type type;	/* contains DATA/NODE/META/META_FLUSH */
1071a912b54dSJaegeuk Kim 	enum temp_type temp;	/* contains HOT/WARM/COLD */
107204d328deSMike Christie 	int op;			/* contains REQ_OP_ */
1073ef295ecfSChristoph Hellwig 	int op_flags;		/* req_flag_bits */
10747a9d7548SChao Yu 	block_t new_blkaddr;	/* new block address to be written */
107528bc106bSChao Yu 	block_t old_blkaddr;	/* old block address before Cow */
107605ca3632SJaegeuk Kim 	struct page *page;	/* page to be written */
10774375a336SJaegeuk Kim 	struct page *encrypted_page;	/* encrypted page */
10784c8ff709SChao Yu 	struct page *compressed_page;	/* compressed page */
1079fb830fc5SChao Yu 	struct list_head list;		/* serialize IOs */
1080d68f735bSJaegeuk Kim 	bool submitted;		/* indicate IO submission */
1081cc15620bSJaegeuk Kim 	int need_lock;		/* indicate we need to lock cp_rwsem */
1082fb830fc5SChao Yu 	bool in_list;		/* indicate fio is in io_list */
10836dc3a126SChao Yu 	bool is_por;		/* indicate IO is from recovery or not */
1084fe16efe6SChao Yu 	bool retry;		/* need to reallocate block address */
10854c8ff709SChao Yu 	int compr_blocks;	/* # of compressed block addresses */
10864c8ff709SChao Yu 	bool encrypted;		/* indicate file is encrypted */
1087b0af6d49SChao Yu 	enum iostat_type io_type;	/* io type */
1088578c6478SYufen Yu 	struct writeback_control *io_wbc; /* writeback control */
10898648de2cSChao Yu 	struct bio **bio;		/* bio for ipu */
10908648de2cSChao Yu 	sector_t *last_block;		/* last block number in bio */
10917735730dSChao Yu 	unsigned char version;		/* version of the node */
1092458e6197SJaegeuk Kim };
1093458e6197SJaegeuk Kim 
10940b20fcecSChao Yu struct bio_entry {
10950b20fcecSChao Yu 	struct bio *bio;
10960b20fcecSChao Yu 	struct list_head list;
10970b20fcecSChao Yu };
10980b20fcecSChao Yu 
109968afcf2dSTomohiro Kusumi #define is_read_io(rw) ((rw) == READ)
11001ff7bd3bSJaegeuk Kim struct f2fs_bio_info {
1101458e6197SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs superblock */
11021ff7bd3bSJaegeuk Kim 	struct bio *bio;		/* bios to merge */
11031ff7bd3bSJaegeuk Kim 	sector_t last_block_in_bio;	/* last block number */
1104458e6197SJaegeuk Kim 	struct f2fs_io_info fio;	/* store buffered io info. */
1105df0f8dc0SChao Yu 	struct rw_semaphore io_rwsem;	/* blocking op for bio */
1106fb830fc5SChao Yu 	spinlock_t io_lock;		/* serialize DATA/NODE IOs */
1107fb830fc5SChao Yu 	struct list_head io_list;	/* track fios */
11080b20fcecSChao Yu 	struct list_head bio_list;	/* bio entry list head */
11090b20fcecSChao Yu 	struct rw_semaphore bio_list_lock;	/* lock to protect bio entry list */
11101ff7bd3bSJaegeuk Kim };
11111ff7bd3bSJaegeuk Kim 
11123c62be17SJaegeuk Kim #define FDEV(i)				(sbi->devs[i])
11133c62be17SJaegeuk Kim #define RDEV(i)				(raw_super->devs[i])
11143c62be17SJaegeuk Kim struct f2fs_dev_info {
11153c62be17SJaegeuk Kim 	struct block_device *bdev;
11163c62be17SJaegeuk Kim 	char path[MAX_PATH_LEN];
11173c62be17SJaegeuk Kim 	unsigned int total_segments;
11183c62be17SJaegeuk Kim 	block_t start_blk;
11193c62be17SJaegeuk Kim 	block_t end_blk;
11203c62be17SJaegeuk Kim #ifdef CONFIG_BLK_DEV_ZONED
11213c62be17SJaegeuk Kim 	unsigned int nr_blkz;		/* Total number of zones */
112295175dafSDamien Le Moal 	unsigned long *blkz_seq;	/* Bitmap indicating sequential zones */
11233c62be17SJaegeuk Kim #endif
11243c62be17SJaegeuk Kim };
11253c62be17SJaegeuk Kim 
1126c227f912SChao Yu enum inode_type {
1127c227f912SChao Yu 	DIR_INODE,			/* for dirty dir inode */
1128c227f912SChao Yu 	FILE_INODE,			/* for dirty regular/symlink inode */
11290f18b462SJaegeuk Kim 	DIRTY_META,			/* for all dirtied inode metadata */
113057864ae5SJaegeuk Kim 	ATOMIC_FILE,			/* for all atomic files */
1131c227f912SChao Yu 	NR_INODE_TYPE,
1132c227f912SChao Yu };
1133c227f912SChao Yu 
113467298804SChao Yu /* for inner inode cache management */
113567298804SChao Yu struct inode_management {
113667298804SChao Yu 	struct radix_tree_root ino_root;	/* ino entry array */
113767298804SChao Yu 	spinlock_t ino_lock;			/* for ino entry lock */
113867298804SChao Yu 	struct list_head ino_list;		/* inode list head */
113967298804SChao Yu 	unsigned long ino_num;			/* number of entries */
114067298804SChao Yu };
114167298804SChao Yu 
1142caf0047eSChao Yu /* For s_flag in struct f2fs_sb_info */
1143caf0047eSChao Yu enum {
1144caf0047eSChao Yu 	SBI_IS_DIRTY,				/* dirty flag for checkpoint */
1145caf0047eSChao Yu 	SBI_IS_CLOSE,				/* specify unmounting */
1146caf0047eSChao Yu 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
1147caf0047eSChao Yu 	SBI_POR_DOING,				/* recovery is doing or not */
1148df728b0fSJaegeuk Kim 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
1149bbf156f7SJaegeuk Kim 	SBI_NEED_CP,				/* need to checkpoint */
115083a3bfdbSJaegeuk Kim 	SBI_IS_SHUTDOWN,			/* shutdown by ioctl */
11511378752bSChao Yu 	SBI_IS_RECOVERED,			/* recovered orphan/data */
11524354994fSDaniel Rosenberg 	SBI_CP_DISABLED,			/* CP was disabled last mount */
1153db610a64SJaegeuk Kim 	SBI_CP_DISABLED_QUICK,			/* CP was disabled quickly */
1154af033b2aSChao Yu 	SBI_QUOTA_NEED_FLUSH,			/* need to flush quota info in CP */
1155af033b2aSChao Yu 	SBI_QUOTA_SKIP_FLUSH,			/* skip flushing quota in current CP */
1156af033b2aSChao Yu 	SBI_QUOTA_NEED_REPAIR,			/* quota file may be corrupted */
115704f0b2eaSQiuyang Sun 	SBI_IS_RESIZEFS,			/* resizefs is in process */
1158caf0047eSChao Yu };
1159caf0047eSChao Yu 
11606beceb54SJaegeuk Kim enum {
11616beceb54SJaegeuk Kim 	CP_TIME,
1162d0239e1bSJaegeuk Kim 	REQ_TIME,
1163a7d10cf3SSahitya Tummala 	DISCARD_TIME,
1164a7d10cf3SSahitya Tummala 	GC_TIME,
11654354994fSDaniel Rosenberg 	DISABLE_TIME,
116603f2c02dSJaegeuk Kim 	UMOUNT_DISCARD_TIMEOUT,
11676beceb54SJaegeuk Kim 	MAX_TIME,
11686beceb54SJaegeuk Kim };
11696beceb54SJaegeuk Kim 
11700cdd3195SHyunchul Lee enum {
11715b0e9539SJaegeuk Kim 	GC_NORMAL,
11725b0e9539SJaegeuk Kim 	GC_IDLE_CB,
11735b0e9539SJaegeuk Kim 	GC_IDLE_GREEDY,
11745b0e9539SJaegeuk Kim 	GC_URGENT,
11755b0e9539SJaegeuk Kim };
11765b0e9539SJaegeuk Kim 
11775b0e9539SJaegeuk Kim enum {
1178bbbc34fdSChao Yu 	BGGC_MODE_ON,		/* background gc is on */
1179bbbc34fdSChao Yu 	BGGC_MODE_OFF,		/* background gc is off */
1180bbbc34fdSChao Yu 	BGGC_MODE_SYNC,		/*
1181bbbc34fdSChao Yu 				 * background gc is on, migrating blocks
1182bbbc34fdSChao Yu 				 * like foreground gc
1183bbbc34fdSChao Yu 				 */
1184bbbc34fdSChao Yu };
1185bbbc34fdSChao Yu 
1186bbbc34fdSChao Yu enum {
1187b0332a0fSChao Yu 	FS_MODE_ADAPTIVE,	/* use both lfs/ssr allocation */
1188b0332a0fSChao Yu 	FS_MODE_LFS,		/* use lfs allocation only */
1189b0332a0fSChao Yu };
1190b0332a0fSChao Yu 
1191b0332a0fSChao Yu enum {
11920cdd3195SHyunchul Lee 	WHINT_MODE_OFF,		/* not pass down write hints */
11930cdd3195SHyunchul Lee 	WHINT_MODE_USER,	/* try to pass down hints given by users */
1194f2e703f9SHyunchul Lee 	WHINT_MODE_FS,		/* pass down hints with F2FS policy */
11950cdd3195SHyunchul Lee };
11960cdd3195SHyunchul Lee 
119707939627SJaegeuk Kim enum {
119807939627SJaegeuk Kim 	ALLOC_MODE_DEFAULT,	/* stay default */
119907939627SJaegeuk Kim 	ALLOC_MODE_REUSE,	/* reuse segments as much as possible */
120007939627SJaegeuk Kim };
120107939627SJaegeuk Kim 
120293cf93f1SJunling Zheng enum fsync_mode {
120393cf93f1SJunling Zheng 	FSYNC_MODE_POSIX,	/* fsync follows posix semantics */
120493cf93f1SJunling Zheng 	FSYNC_MODE_STRICT,	/* fsync behaves in line with ext4 */
1205d6290814SJaegeuk Kim 	FSYNC_MODE_NOBARRIER,	/* fsync behaves nobarrier based on posix */
120693cf93f1SJunling Zheng };
120793cf93f1SJunling Zheng 
12084c8ff709SChao Yu /*
12094c8ff709SChao Yu  * this value is set in page as a private data which indicate that
12104c8ff709SChao Yu  * the page is atomically written, and it is in inmem_pages list.
12114c8ff709SChao Yu  */
12124c8ff709SChao Yu #define ATOMIC_WRITTEN_PAGE		((unsigned long)-1)
12134c8ff709SChao Yu #define DUMMY_WRITTEN_PAGE		((unsigned long)-2)
12144c8ff709SChao Yu 
12154c8ff709SChao Yu #define IS_ATOMIC_WRITTEN_PAGE(page)			\
12164c8ff709SChao Yu 		(page_private(page) == (unsigned long)ATOMIC_WRITTEN_PAGE)
12174c8ff709SChao Yu #define IS_DUMMY_WRITTEN_PAGE(page)			\
12184c8ff709SChao Yu 		(page_private(page) == (unsigned long)DUMMY_WRITTEN_PAGE)
12194c8ff709SChao Yu 
1220643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
1221ff62af20SSheng Yong #define DUMMY_ENCRYPTION_ENABLED(sbi) \
1222ff62af20SSheng Yong 			(unlikely(F2FS_OPTION(sbi).test_dummy_encryption))
1223ff62af20SSheng Yong #else
1224ff62af20SSheng Yong #define DUMMY_ENCRYPTION_ENABLED(sbi) (0)
1225ff62af20SSheng Yong #endif
1226ff62af20SSheng Yong 
12274c8ff709SChao Yu /* For compression */
12284c8ff709SChao Yu enum compress_algorithm_type {
12294c8ff709SChao Yu 	COMPRESS_LZO,
12304c8ff709SChao Yu 	COMPRESS_LZ4,
12314c8ff709SChao Yu 	COMPRESS_MAX,
12324c8ff709SChao Yu };
12334c8ff709SChao Yu 
12340b32dc18SChao Yu #define COMPRESS_DATA_RESERVED_SIZE		5
12354c8ff709SChao Yu struct compress_data {
12364c8ff709SChao Yu 	__le32 clen;			/* compressed data size */
12374c8ff709SChao Yu 	__le32 reserved[COMPRESS_DATA_RESERVED_SIZE];	/* reserved */
12384c8ff709SChao Yu 	u8 cdata[];			/* compressed data */
12394c8ff709SChao Yu };
12404c8ff709SChao Yu 
12414c8ff709SChao Yu #define COMPRESS_HEADER_SIZE	(sizeof(struct compress_data))
12424c8ff709SChao Yu 
12434c8ff709SChao Yu #define F2FS_COMPRESSED_PAGE_MAGIC	0xF5F2C000
12444c8ff709SChao Yu 
12454c8ff709SChao Yu /* compress context */
12464c8ff709SChao Yu struct compress_ctx {
12474c8ff709SChao Yu 	struct inode *inode;		/* inode the context belong to */
12484c8ff709SChao Yu 	pgoff_t cluster_idx;		/* cluster index number */
12494c8ff709SChao Yu 	unsigned int cluster_size;	/* page count in cluster */
12504c8ff709SChao Yu 	unsigned int log_cluster_size;	/* log of cluster size */
12514c8ff709SChao Yu 	struct page **rpages;		/* pages store raw data in cluster */
12524c8ff709SChao Yu 	unsigned int nr_rpages;		/* total page number in rpages */
12534c8ff709SChao Yu 	struct page **cpages;		/* pages store compressed data in cluster */
12544c8ff709SChao Yu 	unsigned int nr_cpages;		/* total page number in cpages */
12554c8ff709SChao Yu 	void *rbuf;			/* virtual mapped address on rpages */
12564c8ff709SChao Yu 	struct compress_data *cbuf;	/* virtual mapped address on cpages */
12574c8ff709SChao Yu 	size_t rlen;			/* valid data length in rbuf */
12584c8ff709SChao Yu 	size_t clen;			/* valid data length in cbuf */
12594c8ff709SChao Yu 	void *private;			/* payload buffer for specified compression algorithm */
12604c8ff709SChao Yu };
12614c8ff709SChao Yu 
12624c8ff709SChao Yu /* compress context for write IO path */
12634c8ff709SChao Yu struct compress_io_ctx {
12644c8ff709SChao Yu 	u32 magic;			/* magic number to indicate page is compressed */
12654c8ff709SChao Yu 	struct inode *inode;		/* inode the context belong to */
12664c8ff709SChao Yu 	struct page **rpages;		/* pages store raw data in cluster */
12674c8ff709SChao Yu 	unsigned int nr_rpages;		/* total page number in rpages */
12684c8ff709SChao Yu 	refcount_t ref;			/* referrence count of raw page */
12694c8ff709SChao Yu };
12704c8ff709SChao Yu 
12714c8ff709SChao Yu /* decompress io context for read IO path */
12724c8ff709SChao Yu struct decompress_io_ctx {
12734c8ff709SChao Yu 	u32 magic;			/* magic number to indicate page is compressed */
12744c8ff709SChao Yu 	struct inode *inode;		/* inode the context belong to */
12754c8ff709SChao Yu 	pgoff_t cluster_idx;		/* cluster index number */
12764c8ff709SChao Yu 	unsigned int cluster_size;	/* page count in cluster */
12774c8ff709SChao Yu 	unsigned int log_cluster_size;	/* log of cluster size */
12784c8ff709SChao Yu 	struct page **rpages;		/* pages store raw data in cluster */
12794c8ff709SChao Yu 	unsigned int nr_rpages;		/* total page number in rpages */
12804c8ff709SChao Yu 	struct page **cpages;		/* pages store compressed data in cluster */
12814c8ff709SChao Yu 	unsigned int nr_cpages;		/* total page number in cpages */
12824c8ff709SChao Yu 	struct page **tpages;		/* temp pages to pad holes in cluster */
12834c8ff709SChao Yu 	void *rbuf;			/* virtual mapped address on rpages */
12844c8ff709SChao Yu 	struct compress_data *cbuf;	/* virtual mapped address on cpages */
12854c8ff709SChao Yu 	size_t rlen;			/* valid data length in rbuf */
12864c8ff709SChao Yu 	size_t clen;			/* valid data length in cbuf */
12874c8ff709SChao Yu 	refcount_t ref;			/* referrence count of compressed page */
12884c8ff709SChao Yu 	bool failed;			/* indicate IO error during decompression */
12894c8ff709SChao Yu };
12904c8ff709SChao Yu 
12914c8ff709SChao Yu #define NULL_CLUSTER			((unsigned int)(~0))
12924c8ff709SChao Yu #define MIN_COMPRESS_LOG_SIZE		2
12934c8ff709SChao Yu #define MAX_COMPRESS_LOG_SIZE		8
12944c8ff709SChao Yu 
129539a53e0cSJaegeuk Kim struct f2fs_sb_info {
129639a53e0cSJaegeuk Kim 	struct super_block *sb;			/* pointer to VFS super block */
12975e176d54SJaegeuk Kim 	struct proc_dir_entry *s_proc;		/* proc entry */
129839a53e0cSJaegeuk Kim 	struct f2fs_super_block *raw_super;	/* raw super block pointer */
1299846ae671SChao Yu 	struct rw_semaphore sb_lock;		/* lock for raw super block */
1300e8240f65SChao Yu 	int valid_super_block;			/* valid super block no */
1301fadb2fb8SChao Yu 	unsigned long s_flag;				/* flags for sbi */
1302853137ceSJaegeuk Kim 	struct mutex writepages;		/* mutex for writepages() */
13035aba5430SDaniel Rosenberg #ifdef CONFIG_UNICODE
13045aba5430SDaniel Rosenberg 	struct unicode_map *s_encoding;
13055aba5430SDaniel Rosenberg 	__u16 s_encoding_flags;
13065aba5430SDaniel Rosenberg #endif
130739a53e0cSJaegeuk Kim 
1308178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
1309178053e2SDamien Le Moal 	unsigned int blocks_per_blkz;		/* F2FS blocks per zone */
1310178053e2SDamien Le Moal 	unsigned int log_blocks_per_blkz;	/* log2 F2FS blocks per zone */
1311178053e2SDamien Le Moal #endif
1312178053e2SDamien Le Moal 
131339a53e0cSJaegeuk Kim 	/* for node-related operations */
131439a53e0cSJaegeuk Kim 	struct f2fs_nm_info *nm_info;		/* node manager */
131539a53e0cSJaegeuk Kim 	struct inode *node_inode;		/* cache node blocks */
131639a53e0cSJaegeuk Kim 
131739a53e0cSJaegeuk Kim 	/* for segment-related operations */
131839a53e0cSJaegeuk Kim 	struct f2fs_sm_info *sm_info;		/* segment manager */
13191ff7bd3bSJaegeuk Kim 
13201ff7bd3bSJaegeuk Kim 	/* for bio operations */
1321a912b54dSJaegeuk Kim 	struct f2fs_bio_info *write_io[NR_PAGE_TYPE];	/* for write bios */
1322107a805dSChao Yu 	/* keep migration IO order for LFS mode */
1323107a805dSChao Yu 	struct rw_semaphore io_order_lock;
13240a595ebaSJaegeuk Kim 	mempool_t *write_io_dummy;		/* Dummy pages */
132539a53e0cSJaegeuk Kim 
132639a53e0cSJaegeuk Kim 	/* for checkpoint */
132739a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt;		/* raw checkpoint pointer */
13288508e44aSJaegeuk Kim 	int cur_cp_pack;			/* remain current cp pack */
1329aaec2b1dSChao Yu 	spinlock_t cp_lock;			/* for flag in ckpt */
133039a53e0cSJaegeuk Kim 	struct inode *meta_inode;		/* cache meta blocks */
133139936837SJaegeuk Kim 	struct mutex cp_mutex;			/* checkpoint procedure lock */
1332b873b798SJaegeuk Kim 	struct rw_semaphore cp_rwsem;		/* blocking FS operations */
1333b3582c68SChao Yu 	struct rw_semaphore node_write;		/* locking node writes */
133459c9081bSYunlei He 	struct rw_semaphore node_change;	/* locking node change */
1335fb51b5efSChangman Lee 	wait_queue_head_t cp_wait;
13366beceb54SJaegeuk Kim 	unsigned long last_time[MAX_TIME];	/* to store time in jiffies */
13376beceb54SJaegeuk Kim 	long interval_time[MAX_TIME];		/* to store thresholds */
133839a53e0cSJaegeuk Kim 
133967298804SChao Yu 	struct inode_management im[MAX_INO_ENTRY];      /* manage inode cache */
13406451e041SJaegeuk Kim 
134150fa53ecSChao Yu 	spinlock_t fsync_node_lock;		/* for node entry lock */
134250fa53ecSChao Yu 	struct list_head fsync_node_list;	/* node list head */
134350fa53ecSChao Yu 	unsigned int fsync_seg_id;		/* sequence id */
134450fa53ecSChao Yu 	unsigned int fsync_node_num;		/* number of node entries */
134550fa53ecSChao Yu 
13466451e041SJaegeuk Kim 	/* for orphan inode, use 0'th array */
13470d47c1adSGu Zheng 	unsigned int max_orphans;		/* max orphan inodes */
134839a53e0cSJaegeuk Kim 
1349c227f912SChao Yu 	/* for inode management */
1350c227f912SChao Yu 	struct list_head inode_list[NR_INODE_TYPE];	/* dirty inode list */
1351c227f912SChao Yu 	spinlock_t inode_lock[NR_INODE_TYPE];	/* for dirty inode list lock */
1352040d2bb3SChao Yu 	struct mutex flush_lock;		/* for flush exclusion */
135339a53e0cSJaegeuk Kim 
135413054c54SChao Yu 	/* for extent tree cache */
135513054c54SChao Yu 	struct radix_tree_root extent_tree_root;/* cache extent cache entries */
13565e8256acSYunlei He 	struct mutex extent_tree_lock;	/* locking extent radix tree */
135713054c54SChao Yu 	struct list_head extent_list;		/* lru list for shrinker */
135813054c54SChao Yu 	spinlock_t extent_lock;			/* locking extent lru list */
13597441ccefSJaegeuk Kim 	atomic_t total_ext_tree;		/* extent tree count */
1360137d09f0SJaegeuk Kim 	struct list_head zombie_list;		/* extent zombie tree list */
136174fd8d99SJaegeuk Kim 	atomic_t total_zombie_tree;		/* extent zombie tree count */
136213054c54SChao Yu 	atomic_t total_ext_node;		/* extent info count */
136313054c54SChao Yu 
136439a53e0cSJaegeuk Kim 	/* basic filesystem units */
136539a53e0cSJaegeuk Kim 	unsigned int log_sectors_per_block;	/* log2 sectors per block */
136639a53e0cSJaegeuk Kim 	unsigned int log_blocksize;		/* log2 block size */
136739a53e0cSJaegeuk Kim 	unsigned int blocksize;			/* block size */
136839a53e0cSJaegeuk Kim 	unsigned int root_ino_num;		/* root inode number*/
136939a53e0cSJaegeuk Kim 	unsigned int node_ino_num;		/* node inode number*/
137039a53e0cSJaegeuk Kim 	unsigned int meta_ino_num;		/* meta inode number*/
137139a53e0cSJaegeuk Kim 	unsigned int log_blocks_per_seg;	/* log2 blocks per segment */
137239a53e0cSJaegeuk Kim 	unsigned int blocks_per_seg;		/* blocks per segment */
137339a53e0cSJaegeuk Kim 	unsigned int segs_per_sec;		/* segments per section */
137439a53e0cSJaegeuk Kim 	unsigned int secs_per_zone;		/* sections per zone */
137539a53e0cSJaegeuk Kim 	unsigned int total_sections;		/* total section count */
137604f0b2eaSQiuyang Sun 	struct mutex resize_mutex;		/* for resize exclusion */
137739a53e0cSJaegeuk Kim 	unsigned int total_node_count;		/* total node block count */
137839a53e0cSJaegeuk Kim 	unsigned int total_valid_node_count;	/* valid node block count */
1379e0afc4d6SChao Yu 	loff_t max_file_blocks;			/* max block index of file */
1380ab9fa662SJaegeuk Kim 	int dir_level;				/* directory level */
1381f6df8f23SSheng Yong 	int readdir_ra;				/* readahead inode in readdir */
138239a53e0cSJaegeuk Kim 
138339a53e0cSJaegeuk Kim 	block_t user_block_count;		/* # of user blocks */
138439a53e0cSJaegeuk Kim 	block_t total_valid_block_count;	/* # of valid blocks */
1385a66cdd98SJaegeuk Kim 	block_t discard_blks;			/* discard command candidats */
138639a53e0cSJaegeuk Kim 	block_t last_valid_block_count;		/* for recovery */
1387daeb433eSChao Yu 	block_t reserved_blocks;		/* configurable reserved blocks */
138880d42145SYunlong Song 	block_t current_reserved_blocks;	/* current reserved blocks */
1389daeb433eSChao Yu 
13904354994fSDaniel Rosenberg 	/* Additional tracking for no checkpoint mode */
13914354994fSDaniel Rosenberg 	block_t unusable_block_count;		/* # of blocks saved by last cp */
13924354994fSDaniel Rosenberg 
1393292c196aSChao Yu 	unsigned int nquota_files;		/* # of quota sysfile */
1394db6ec53bSJaegeuk Kim 	struct rw_semaphore quota_sem;		/* blocking cp for flags */
1395292c196aSChao Yu 
1396523be8a6SJaegeuk Kim 	/* # of pages, see count_type */
139735782b23SJaegeuk Kim 	atomic_t nr_pages[NR_COUNT_TYPE];
139841382ec4SJaegeuk Kim 	/* # of allocated blocks */
139941382ec4SJaegeuk Kim 	struct percpu_counter alloc_valid_block_count;
140039a53e0cSJaegeuk Kim 
1401687de7f1SJaegeuk Kim 	/* writeback control */
1402c29fd0c0SChao Yu 	atomic_t wb_sync_req[META];	/* count # of WB_SYNC threads */
1403687de7f1SJaegeuk Kim 
1404513c5f37SJaegeuk Kim 	/* valid inode count */
1405513c5f37SJaegeuk Kim 	struct percpu_counter total_valid_inode_count;
1406513c5f37SJaegeuk Kim 
140739a53e0cSJaegeuk Kim 	struct f2fs_mount_info mount_opt;	/* mount options */
140839a53e0cSJaegeuk Kim 
140939a53e0cSJaegeuk Kim 	/* for cleaning operations */
1410fb24fea7SChao Yu 	struct rw_semaphore gc_lock;		/*
1411fb24fea7SChao Yu 						 * semaphore for GC, avoid
1412fb24fea7SChao Yu 						 * race between GC and GC or CP
1413fb24fea7SChao Yu 						 */
141439a53e0cSJaegeuk Kim 	struct f2fs_gc_kthread	*gc_thread;	/* GC thread */
14155ec4e49fSJaegeuk Kim 	unsigned int cur_victim_sec;		/* current victim section num */
14165b0e9539SJaegeuk Kim 	unsigned int gc_mode;			/* current GC state */
1417e3080b01SChao Yu 	unsigned int next_victim_seg[2];	/* next segment in victim section */
14182ef79ecbSChao Yu 	/* for skip statistic */
1419677017d1SSahitya Tummala 	unsigned int atomic_files;              /* # of opened atomic file */
14202ef79ecbSChao Yu 	unsigned long long skipped_atomic_files[2];	/* FG_GC and BG_GC */
14216f8d4455SJaegeuk Kim 	unsigned long long skipped_gc_rwsem;		/* FG_GC only */
142239a53e0cSJaegeuk Kim 
14231ad71a27SJaegeuk Kim 	/* threshold for gc trials on pinned files */
14241ad71a27SJaegeuk Kim 	u64 gc_pin_file_threshold;
1425f5a53edcSJaegeuk Kim 	struct rw_semaphore pin_sem;
14261ad71a27SJaegeuk Kim 
1427b1c57c1cSJaegeuk Kim 	/* maximum # of trials to find a victim segment for SSR and GC */
1428b1c57c1cSJaegeuk Kim 	unsigned int max_victim_search;
1429e3080b01SChao Yu 	/* migration granularity of garbage collection, unit: segment */
1430e3080b01SChao Yu 	unsigned int migration_granularity;
1431b1c57c1cSJaegeuk Kim 
143239a53e0cSJaegeuk Kim 	/*
143339a53e0cSJaegeuk Kim 	 * for stat information.
143439a53e0cSJaegeuk Kim 	 * one is for the LFS mode, and the other is for the SSR mode.
143539a53e0cSJaegeuk Kim 	 */
143635b09d82SNamjae Jeon #ifdef CONFIG_F2FS_STAT_FS
143739a53e0cSJaegeuk Kim 	struct f2fs_stat_info *stat_info;	/* FS status information */
1438b63e7be5SChao Yu 	atomic_t meta_count[META_MAX];		/* # of meta blocks */
143939a53e0cSJaegeuk Kim 	unsigned int segment_count[2];		/* # of allocated segments */
144039a53e0cSJaegeuk Kim 	unsigned int block_count[2];		/* # of allocated blocks */
1441b9a2c252SChangman Lee 	atomic_t inplace_count;		/* # of inplace update */
14425b7ee374SChao Yu 	atomic64_t total_hit_ext;		/* # of lookup extent cache */
14435b7ee374SChao Yu 	atomic64_t read_hit_rbtree;		/* # of hit rbtree extent node */
14445b7ee374SChao Yu 	atomic64_t read_hit_largest;		/* # of hit largest extent node */
14455b7ee374SChao Yu 	atomic64_t read_hit_cached;		/* # of hit cached extent node */
1446d5e8f6c9SChao Yu 	atomic_t inline_xattr;			/* # of inline_xattr inodes */
144703e14d52SChao Yu 	atomic_t inline_inode;			/* # of inline_data inodes */
144803e14d52SChao Yu 	atomic_t inline_dir;			/* # of inline_dentry inodes */
14494c8ff709SChao Yu 	atomic_t compr_inode;			/* # of compressed inodes */
14504c8ff709SChao Yu 	atomic_t compr_blocks;			/* # of compressed blocks */
1451648d50baSChao Yu 	atomic_t vw_cnt;			/* # of volatile writes */
145226a28a0cSJaegeuk Kim 	atomic_t max_aw_cnt;			/* max # of atomic writes */
1453648d50baSChao Yu 	atomic_t max_vw_cnt;			/* max # of volatile writes */
1454274bd9baSChao Yu 	unsigned int io_skip_bggc;		/* skip background gc for in-flight IO */
1455274bd9baSChao Yu 	unsigned int other_skip_bggc;		/* skip background gc for other reasons */
145633fbd510SChao Yu 	unsigned int ndirty_inode[NR_INODE_TYPE];	/* # of dirty inodes */
145735b09d82SNamjae Jeon #endif
145839a53e0cSJaegeuk Kim 	spinlock_t stat_lock;			/* lock for stat operations */
1459b59d0baeSNamjae Jeon 
1460b0af6d49SChao Yu 	/* For app/fs IO statistics */
1461b0af6d49SChao Yu 	spinlock_t iostat_lock;
1462b0af6d49SChao Yu 	unsigned long long write_iostat[NR_IO_TYPE];
1463b0af6d49SChao Yu 	bool iostat_enable;
1464b0af6d49SChao Yu 
1465b59d0baeSNamjae Jeon 	/* For sysfs suppport */
1466b59d0baeSNamjae Jeon 	struct kobject s_kobj;
1467b59d0baeSNamjae Jeon 	struct completion s_kobj_unregister;
14682658e50dSJaegeuk Kim 
14692658e50dSJaegeuk Kim 	/* For shrinker support */
14702658e50dSJaegeuk Kim 	struct list_head s_list;
14713c62be17SJaegeuk Kim 	int s_ndevs;				/* number of devices */
14723c62be17SJaegeuk Kim 	struct f2fs_dev_info *devs;		/* for device list */
14731228b482SChao Yu 	unsigned int dirty_device;		/* for checkpoint data flush */
14741228b482SChao Yu 	spinlock_t dev_lock;			/* protect dirty_device */
14752658e50dSJaegeuk Kim 	struct mutex umount_mutex;
14762658e50dSJaegeuk Kim 	unsigned int shrinker_run_no;
14778f1dbbbbSShuoran Liu 
14788f1dbbbbSShuoran Liu 	/* For write statistics */
14798f1dbbbbSShuoran Liu 	u64 sectors_written_start;
14808f1dbbbbSShuoran Liu 	u64 kbytes_written;
148143b6573bSKeith Mok 
148243b6573bSKeith Mok 	/* Reference to checksum algorithm driver via cryptoapi */
148343b6573bSKeith Mok 	struct crypto_shash *s_chksum_driver;
14841ecc0c5cSChao Yu 
1485704956ecSChao Yu 	/* Precomputed FS UUID checksum for seeding other checksums */
1486704956ecSChao Yu 	__u32 s_chksum_seed;
14874c8ff709SChao Yu 
14884c8ff709SChao Yu 	struct workqueue_struct *post_read_wq;	/* post read workqueue */
148939a53e0cSJaegeuk Kim };
149039a53e0cSJaegeuk Kim 
149102b16d0aSChao Yu struct f2fs_private_dio {
149202b16d0aSChao Yu 	struct inode *inode;
149302b16d0aSChao Yu 	void *orig_private;
149402b16d0aSChao Yu 	bio_end_io_t *orig_end_io;
149502b16d0aSChao Yu 	bool write;
149602b16d0aSChao Yu };
149702b16d0aSChao Yu 
14981ecc0c5cSChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
1499c45d6002SChao Yu #define f2fs_show_injection_info(sbi, type)					\
1500c45d6002SChao Yu 	printk_ratelimited("%sF2FS-fs (%s) : inject %s in %s of %pS\n",	\
1501c45d6002SChao Yu 		KERN_INFO, sbi->sb->s_id,				\
1502c45d6002SChao Yu 		f2fs_fault_name[type],					\
150355523519SChao Yu 		__func__, __builtin_return_address(0))
15041ecc0c5cSChao Yu static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
15051ecc0c5cSChao Yu {
150663189b78SChao Yu 	struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
15071ecc0c5cSChao Yu 
15081ecc0c5cSChao Yu 	if (!ffi->inject_rate)
15091ecc0c5cSChao Yu 		return false;
15101ecc0c5cSChao Yu 
15111ecc0c5cSChao Yu 	if (!IS_FAULT_SET(ffi, type))
15121ecc0c5cSChao Yu 		return false;
15131ecc0c5cSChao Yu 
15141ecc0c5cSChao Yu 	atomic_inc(&ffi->inject_ops);
15151ecc0c5cSChao Yu 	if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
15161ecc0c5cSChao Yu 		atomic_set(&ffi->inject_ops, 0);
15171ecc0c5cSChao Yu 		return true;
15181ecc0c5cSChao Yu 	}
15191ecc0c5cSChao Yu 	return false;
15201ecc0c5cSChao Yu }
15217fa750a1SArnd Bergmann #else
1522c45d6002SChao Yu #define f2fs_show_injection_info(sbi, type) do { } while (0)
15237fa750a1SArnd Bergmann static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
15247fa750a1SArnd Bergmann {
15257fa750a1SArnd Bergmann 	return false;
15267fa750a1SArnd Bergmann }
15271ecc0c5cSChao Yu #endif
15281ecc0c5cSChao Yu 
15290916878dSDamien Le Moal /*
15300916878dSDamien Le Moal  * Test if the mounted volume is a multi-device volume.
15310916878dSDamien Le Moal  *   - For a single regular disk volume, sbi->s_ndevs is 0.
15320916878dSDamien Le Moal  *   - For a single zoned disk volume, sbi->s_ndevs is 1.
15330916878dSDamien Le Moal  *   - For a multi-device volume, sbi->s_ndevs is always 2 or more.
15340916878dSDamien Le Moal  */
15350916878dSDamien Le Moal static inline bool f2fs_is_multi_device(struct f2fs_sb_info *sbi)
15360916878dSDamien Le Moal {
15370916878dSDamien Le Moal 	return sbi->s_ndevs > 1;
15380916878dSDamien Le Moal }
15390916878dSDamien Le Moal 
15408f1dbbbbSShuoran Liu /* For write statistics. Suppose sector size is 512 bytes,
15418f1dbbbbSShuoran Liu  * and the return value is in kbytes. s is of struct f2fs_sb_info.
15428f1dbbbbSShuoran Liu  */
15438f1dbbbbSShuoran Liu #define BD_PART_WRITTEN(s)						 \
1544dbae2c55SMichael Callahan (((u64)part_stat_read((s)->sb->s_bdev->bd_part, sectors[STAT_WRITE]) -   \
154568afcf2dSTomohiro Kusumi 		(s)->sectors_written_start) >> 1)
15468f1dbbbbSShuoran Liu 
15476beceb54SJaegeuk Kim static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
15486beceb54SJaegeuk Kim {
1549a7d10cf3SSahitya Tummala 	unsigned long now = jiffies;
1550a7d10cf3SSahitya Tummala 
1551a7d10cf3SSahitya Tummala 	sbi->last_time[type] = now;
1552a7d10cf3SSahitya Tummala 
1553a7d10cf3SSahitya Tummala 	/* DISCARD_TIME and GC_TIME are based on REQ_TIME */
1554a7d10cf3SSahitya Tummala 	if (type == REQ_TIME) {
1555a7d10cf3SSahitya Tummala 		sbi->last_time[DISCARD_TIME] = now;
1556a7d10cf3SSahitya Tummala 		sbi->last_time[GC_TIME] = now;
1557a7d10cf3SSahitya Tummala 	}
15586beceb54SJaegeuk Kim }
15596beceb54SJaegeuk Kim 
15606beceb54SJaegeuk Kim static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
15616beceb54SJaegeuk Kim {
15626bccfa19SArnd Bergmann 	unsigned long interval = sbi->interval_time[type] * HZ;
15636beceb54SJaegeuk Kim 
15646beceb54SJaegeuk Kim 	return time_after(jiffies, sbi->last_time[type] + interval);
15656beceb54SJaegeuk Kim }
15666beceb54SJaegeuk Kim 
1567a7d10cf3SSahitya Tummala static inline unsigned int f2fs_time_to_wait(struct f2fs_sb_info *sbi,
1568a7d10cf3SSahitya Tummala 						int type)
1569a7d10cf3SSahitya Tummala {
1570a7d10cf3SSahitya Tummala 	unsigned long interval = sbi->interval_time[type] * HZ;
1571a7d10cf3SSahitya Tummala 	unsigned int wait_ms = 0;
1572a7d10cf3SSahitya Tummala 	long delta;
1573a7d10cf3SSahitya Tummala 
1574a7d10cf3SSahitya Tummala 	delta = (sbi->last_time[type] + interval) - jiffies;
1575a7d10cf3SSahitya Tummala 	if (delta > 0)
1576a7d10cf3SSahitya Tummala 		wait_ms = jiffies_to_msecs(delta);
1577a7d10cf3SSahitya Tummala 
1578a7d10cf3SSahitya Tummala 	return wait_ms;
1579a7d10cf3SSahitya Tummala }
1580a7d10cf3SSahitya Tummala 
158139a53e0cSJaegeuk Kim /*
158239a53e0cSJaegeuk Kim  * Inline functions
158339a53e0cSJaegeuk Kim  */
1584416d2dbbSChao Yu static inline u32 __f2fs_crc32(struct f2fs_sb_info *sbi, u32 crc,
1585704956ecSChao Yu 			      const void *address, unsigned int length)
1586704956ecSChao Yu {
1587704956ecSChao Yu 	struct {
1588704956ecSChao Yu 		struct shash_desc shash;
1589704956ecSChao Yu 		char ctx[4];
1590704956ecSChao Yu 	} desc;
1591704956ecSChao Yu 	int err;
1592704956ecSChao Yu 
1593704956ecSChao Yu 	BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver) != sizeof(desc.ctx));
1594704956ecSChao Yu 
1595704956ecSChao Yu 	desc.shash.tfm = sbi->s_chksum_driver;
1596704956ecSChao Yu 	*(u32 *)desc.ctx = crc;
1597704956ecSChao Yu 
1598704956ecSChao Yu 	err = crypto_shash_update(&desc.shash, address, length);
1599704956ecSChao Yu 	BUG_ON(err);
1600704956ecSChao Yu 
1601704956ecSChao Yu 	return *(u32 *)desc.ctx;
1602704956ecSChao Yu }
1603704956ecSChao Yu 
1604416d2dbbSChao Yu static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
1605416d2dbbSChao Yu 			   unsigned int length)
1606416d2dbbSChao Yu {
1607416d2dbbSChao Yu 	return __f2fs_crc32(sbi, F2FS_SUPER_MAGIC, address, length);
1608416d2dbbSChao Yu }
1609416d2dbbSChao Yu 
1610416d2dbbSChao Yu static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
1611416d2dbbSChao Yu 				  void *buf, size_t buf_size)
1612416d2dbbSChao Yu {
1613416d2dbbSChao Yu 	return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
1614416d2dbbSChao Yu }
1615416d2dbbSChao Yu 
1616416d2dbbSChao Yu static inline u32 f2fs_chksum(struct f2fs_sb_info *sbi, u32 crc,
1617416d2dbbSChao Yu 			      const void *address, unsigned int length)
1618416d2dbbSChao Yu {
1619416d2dbbSChao Yu 	return __f2fs_crc32(sbi, crc, address, length);
1620416d2dbbSChao Yu }
1621416d2dbbSChao Yu 
162239a53e0cSJaegeuk Kim static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
162339a53e0cSJaegeuk Kim {
162439a53e0cSJaegeuk Kim 	return container_of(inode, struct f2fs_inode_info, vfs_inode);
162539a53e0cSJaegeuk Kim }
162639a53e0cSJaegeuk Kim 
162739a53e0cSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
162839a53e0cSJaegeuk Kim {
162939a53e0cSJaegeuk Kim 	return sb->s_fs_info;
163039a53e0cSJaegeuk Kim }
163139a53e0cSJaegeuk Kim 
16324081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
16334081363fSJaegeuk Kim {
16344081363fSJaegeuk Kim 	return F2FS_SB(inode->i_sb);
16354081363fSJaegeuk Kim }
16364081363fSJaegeuk Kim 
16374081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
16384081363fSJaegeuk Kim {
16394081363fSJaegeuk Kim 	return F2FS_I_SB(mapping->host);
16404081363fSJaegeuk Kim }
16414081363fSJaegeuk Kim 
16424081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
16434081363fSJaegeuk Kim {
16444969c06aSJaegeuk Kim 	return F2FS_M_SB(page_file_mapping(page));
16454081363fSJaegeuk Kim }
16464081363fSJaegeuk Kim 
164739a53e0cSJaegeuk Kim static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
164839a53e0cSJaegeuk Kim {
164939a53e0cSJaegeuk Kim 	return (struct f2fs_super_block *)(sbi->raw_super);
165039a53e0cSJaegeuk Kim }
165139a53e0cSJaegeuk Kim 
165239a53e0cSJaegeuk Kim static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
165339a53e0cSJaegeuk Kim {
165439a53e0cSJaegeuk Kim 	return (struct f2fs_checkpoint *)(sbi->ckpt);
165539a53e0cSJaegeuk Kim }
165639a53e0cSJaegeuk Kim 
165745590710SGu Zheng static inline struct f2fs_node *F2FS_NODE(struct page *page)
165845590710SGu Zheng {
165945590710SGu Zheng 	return (struct f2fs_node *)page_address(page);
166045590710SGu Zheng }
166145590710SGu Zheng 
166258bfaf44SJaegeuk Kim static inline struct f2fs_inode *F2FS_INODE(struct page *page)
166358bfaf44SJaegeuk Kim {
166458bfaf44SJaegeuk Kim 	return &((struct f2fs_node *)page_address(page))->i;
166558bfaf44SJaegeuk Kim }
166658bfaf44SJaegeuk Kim 
166739a53e0cSJaegeuk Kim static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
166839a53e0cSJaegeuk Kim {
166939a53e0cSJaegeuk Kim 	return (struct f2fs_nm_info *)(sbi->nm_info);
167039a53e0cSJaegeuk Kim }
167139a53e0cSJaegeuk Kim 
167239a53e0cSJaegeuk Kim static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
167339a53e0cSJaegeuk Kim {
167439a53e0cSJaegeuk Kim 	return (struct f2fs_sm_info *)(sbi->sm_info);
167539a53e0cSJaegeuk Kim }
167639a53e0cSJaegeuk Kim 
167739a53e0cSJaegeuk Kim static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
167839a53e0cSJaegeuk Kim {
167939a53e0cSJaegeuk Kim 	return (struct sit_info *)(SM_I(sbi)->sit_info);
168039a53e0cSJaegeuk Kim }
168139a53e0cSJaegeuk Kim 
168239a53e0cSJaegeuk Kim static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
168339a53e0cSJaegeuk Kim {
168439a53e0cSJaegeuk Kim 	return (struct free_segmap_info *)(SM_I(sbi)->free_info);
168539a53e0cSJaegeuk Kim }
168639a53e0cSJaegeuk Kim 
168739a53e0cSJaegeuk Kim static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
168839a53e0cSJaegeuk Kim {
168939a53e0cSJaegeuk Kim 	return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
169039a53e0cSJaegeuk Kim }
169139a53e0cSJaegeuk Kim 
16929df27d98SGu Zheng static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
16939df27d98SGu Zheng {
16949df27d98SGu Zheng 	return sbi->meta_inode->i_mapping;
16959df27d98SGu Zheng }
16969df27d98SGu Zheng 
16974ef51a8fSJaegeuk Kim static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
16984ef51a8fSJaegeuk Kim {
16994ef51a8fSJaegeuk Kim 	return sbi->node_inode->i_mapping;
17004ef51a8fSJaegeuk Kim }
17014ef51a8fSJaegeuk Kim 
1702caf0047eSChao Yu static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
170339a53e0cSJaegeuk Kim {
1704fadb2fb8SChao Yu 	return test_bit(type, &sbi->s_flag);
170539a53e0cSJaegeuk Kim }
170639a53e0cSJaegeuk Kim 
1707caf0047eSChao Yu static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
170839a53e0cSJaegeuk Kim {
1709fadb2fb8SChao Yu 	set_bit(type, &sbi->s_flag);
1710caf0047eSChao Yu }
1711caf0047eSChao Yu 
1712caf0047eSChao Yu static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
1713caf0047eSChao Yu {
1714fadb2fb8SChao Yu 	clear_bit(type, &sbi->s_flag);
171539a53e0cSJaegeuk Kim }
171639a53e0cSJaegeuk Kim 
1717d71b5564SJaegeuk Kim static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
1718d71b5564SJaegeuk Kim {
1719d71b5564SJaegeuk Kim 	return le64_to_cpu(cp->checkpoint_ver);
1720d71b5564SJaegeuk Kim }
1721d71b5564SJaegeuk Kim 
1722ea676733SJaegeuk Kim static inline unsigned long f2fs_qf_ino(struct super_block *sb, int type)
1723ea676733SJaegeuk Kim {
1724ea676733SJaegeuk Kim 	if (type < F2FS_MAX_QUOTAS)
1725ea676733SJaegeuk Kim 		return le32_to_cpu(F2FS_SB(sb)->raw_super->qf_ino[type]);
1726ea676733SJaegeuk Kim 	return 0;
1727ea676733SJaegeuk Kim }
1728ea676733SJaegeuk Kim 
1729ced2c7eaSKinglong Mee static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
1730ced2c7eaSKinglong Mee {
1731ced2c7eaSKinglong Mee 	size_t crc_offset = le32_to_cpu(cp->checksum_offset);
1732ced2c7eaSKinglong Mee 	return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
1733ced2c7eaSKinglong Mee }
1734ced2c7eaSKinglong Mee 
1735aaec2b1dSChao Yu static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
173625ca923bSJaegeuk Kim {
173725ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
1738aaec2b1dSChao Yu 
173925ca923bSJaegeuk Kim 	return ckpt_flags & f;
174025ca923bSJaegeuk Kim }
174125ca923bSJaegeuk Kim 
1742aaec2b1dSChao Yu static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
174325ca923bSJaegeuk Kim {
1744aaec2b1dSChao Yu 	return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
1745aaec2b1dSChao Yu }
1746aaec2b1dSChao Yu 
1747aaec2b1dSChao Yu static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1748aaec2b1dSChao Yu {
1749aaec2b1dSChao Yu 	unsigned int ckpt_flags;
1750aaec2b1dSChao Yu 
1751aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
175225ca923bSJaegeuk Kim 	ckpt_flags |= f;
175325ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
175425ca923bSJaegeuk Kim }
175525ca923bSJaegeuk Kim 
1756aaec2b1dSChao Yu static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
175725ca923bSJaegeuk Kim {
1758d1aa2453SChao Yu 	unsigned long flags;
1759d1aa2453SChao Yu 
1760d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1761aaec2b1dSChao Yu 	__set_ckpt_flags(F2FS_CKPT(sbi), f);
1762d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1763aaec2b1dSChao Yu }
1764aaec2b1dSChao Yu 
1765aaec2b1dSChao Yu static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1766aaec2b1dSChao Yu {
1767aaec2b1dSChao Yu 	unsigned int ckpt_flags;
1768aaec2b1dSChao Yu 
1769aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
177025ca923bSJaegeuk Kim 	ckpt_flags &= (~f);
177125ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
177225ca923bSJaegeuk Kim }
177325ca923bSJaegeuk Kim 
1774aaec2b1dSChao Yu static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
1775aaec2b1dSChao Yu {
1776d1aa2453SChao Yu 	unsigned long flags;
1777d1aa2453SChao Yu 
1778d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1779aaec2b1dSChao Yu 	__clear_ckpt_flags(F2FS_CKPT(sbi), f);
1780d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1781aaec2b1dSChao Yu }
1782aaec2b1dSChao Yu 
178322ad0b6aSJaegeuk Kim static inline void disable_nat_bits(struct f2fs_sb_info *sbi, bool lock)
178422ad0b6aSJaegeuk Kim {
1785d1aa2453SChao Yu 	unsigned long flags;
17860921835cSChao Yu 	unsigned char *nat_bits;
1787d1aa2453SChao Yu 
1788a742fd41SJaegeuk Kim 	/*
1789a742fd41SJaegeuk Kim 	 * In order to re-enable nat_bits we need to call fsck.f2fs by
1790a742fd41SJaegeuk Kim 	 * set_sbi_flag(sbi, SBI_NEED_FSCK). But it may give huge cost,
1791a742fd41SJaegeuk Kim 	 * so let's rely on regular fsck or unclean shutdown.
1792a742fd41SJaegeuk Kim 	 */
179322ad0b6aSJaegeuk Kim 
179422ad0b6aSJaegeuk Kim 	if (lock)
1795d1aa2453SChao Yu 		spin_lock_irqsave(&sbi->cp_lock, flags);
179622ad0b6aSJaegeuk Kim 	__clear_ckpt_flags(F2FS_CKPT(sbi), CP_NAT_BITS_FLAG);
17970921835cSChao Yu 	nat_bits = NM_I(sbi)->nat_bits;
179822ad0b6aSJaegeuk Kim 	NM_I(sbi)->nat_bits = NULL;
179922ad0b6aSJaegeuk Kim 	if (lock)
1800d1aa2453SChao Yu 		spin_unlock_irqrestore(&sbi->cp_lock, flags);
18010921835cSChao Yu 
18020921835cSChao Yu 	kvfree(nat_bits);
180322ad0b6aSJaegeuk Kim }
180422ad0b6aSJaegeuk Kim 
180522ad0b6aSJaegeuk Kim static inline bool enabled_nat_bits(struct f2fs_sb_info *sbi,
180622ad0b6aSJaegeuk Kim 					struct cp_control *cpc)
180722ad0b6aSJaegeuk Kim {
180822ad0b6aSJaegeuk Kim 	bool set = is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
180922ad0b6aSJaegeuk Kim 
1810c473f1a9SChao Yu 	return (cpc) ? (cpc->reason & CP_UMOUNT) && set : set;
181122ad0b6aSJaegeuk Kim }
181222ad0b6aSJaegeuk Kim 
1813e479556bSGu Zheng static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
181439a53e0cSJaegeuk Kim {
1815b873b798SJaegeuk Kim 	down_read(&sbi->cp_rwsem);
181639a53e0cSJaegeuk Kim }
181739a53e0cSJaegeuk Kim 
1818cc15620bSJaegeuk Kim static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi)
1819cc15620bSJaegeuk Kim {
1820cc15620bSJaegeuk Kim 	return down_read_trylock(&sbi->cp_rwsem);
1821cc15620bSJaegeuk Kim }
1822cc15620bSJaegeuk Kim 
1823e479556bSGu Zheng static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
182439a53e0cSJaegeuk Kim {
1825b873b798SJaegeuk Kim 	up_read(&sbi->cp_rwsem);
182639936837SJaegeuk Kim }
182739936837SJaegeuk Kim 
1828e479556bSGu Zheng static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
182939936837SJaegeuk Kim {
1830b873b798SJaegeuk Kim 	down_write(&sbi->cp_rwsem);
183139936837SJaegeuk Kim }
183239936837SJaegeuk Kim 
1833e479556bSGu Zheng static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
183439936837SJaegeuk Kim {
1835b873b798SJaegeuk Kim 	up_write(&sbi->cp_rwsem);
183639a53e0cSJaegeuk Kim }
183739a53e0cSJaegeuk Kim 
1838119ee914SJaegeuk Kim static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
1839119ee914SJaegeuk Kim {
1840119ee914SJaegeuk Kim 	int reason = CP_SYNC;
1841119ee914SJaegeuk Kim 
1842119ee914SJaegeuk Kim 	if (test_opt(sbi, FASTBOOT))
1843119ee914SJaegeuk Kim 		reason = CP_FASTBOOT;
1844119ee914SJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
1845119ee914SJaegeuk Kim 		reason = CP_UMOUNT;
1846119ee914SJaegeuk Kim 	return reason;
1847119ee914SJaegeuk Kim }
1848119ee914SJaegeuk Kim 
1849119ee914SJaegeuk Kim static inline bool __remain_node_summaries(int reason)
1850119ee914SJaegeuk Kim {
1851c473f1a9SChao Yu 	return (reason & (CP_UMOUNT | CP_FASTBOOT));
1852119ee914SJaegeuk Kim }
1853119ee914SJaegeuk Kim 
1854119ee914SJaegeuk Kim static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
1855119ee914SJaegeuk Kim {
1856aaec2b1dSChao Yu 	return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
1857aaec2b1dSChao Yu 			is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
1858119ee914SJaegeuk Kim }
1859119ee914SJaegeuk Kim 
186039a53e0cSJaegeuk Kim /*
186139a53e0cSJaegeuk Kim  * Check whether the inode has blocks or not
186239a53e0cSJaegeuk Kim  */
186339a53e0cSJaegeuk Kim static inline int F2FS_HAS_BLOCKS(struct inode *inode)
186439a53e0cSJaegeuk Kim {
18650eb0adadSChao Yu 	block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
18660eb0adadSChao Yu 
1867000519f2SChao Yu 	return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block;
186839a53e0cSJaegeuk Kim }
186939a53e0cSJaegeuk Kim 
18704bc8e9bcSChao Yu static inline bool f2fs_has_xattr_block(unsigned int ofs)
18714bc8e9bcSChao Yu {
18724bc8e9bcSChao Yu 	return ofs == XATTR_NODE_OFFSET;
18734bc8e9bcSChao Yu }
18744bc8e9bcSChao Yu 
1875d8a9a229SJaegeuk Kim static inline bool __allow_reserved_blocks(struct f2fs_sb_info *sbi,
1876a90a0884SJaegeuk Kim 					struct inode *inode, bool cap)
18777c2e5963SJaegeuk Kim {
1878d8a9a229SJaegeuk Kim 	if (!inode)
1879d8a9a229SJaegeuk Kim 		return true;
18807c2e5963SJaegeuk Kim 	if (!test_opt(sbi, RESERVE_ROOT))
18817c2e5963SJaegeuk Kim 		return false;
1882d8a9a229SJaegeuk Kim 	if (IS_NOQUOTA(inode))
1883d8a9a229SJaegeuk Kim 		return true;
188463189b78SChao Yu 	if (uid_eq(F2FS_OPTION(sbi).s_resuid, current_fsuid()))
18857c2e5963SJaegeuk Kim 		return true;
188663189b78SChao Yu 	if (!gid_eq(F2FS_OPTION(sbi).s_resgid, GLOBAL_ROOT_GID) &&
188763189b78SChao Yu 					in_group_p(F2FS_OPTION(sbi).s_resgid))
18887c2e5963SJaegeuk Kim 		return true;
1889a90a0884SJaegeuk Kim 	if (cap && capable(CAP_SYS_RESOURCE))
1890162b27aeSJaegeuk Kim 		return true;
18917c2e5963SJaegeuk Kim 	return false;
18927c2e5963SJaegeuk Kim }
18937c2e5963SJaegeuk Kim 
18940abd675eSChao Yu static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool);
18950abd675eSChao Yu static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
189646008c6dSChao Yu 				 struct inode *inode, blkcnt_t *count)
189739a53e0cSJaegeuk Kim {
18980abd675eSChao Yu 	blkcnt_t diff = 0, release = 0;
1899daeb433eSChao Yu 	block_t avail_user_block_count;
19000abd675eSChao Yu 	int ret;
19010abd675eSChao Yu 
19020abd675eSChao Yu 	ret = dquot_reserve_block(inode, *count);
19030abd675eSChao Yu 	if (ret)
19040abd675eSChao Yu 		return ret;
1905dd11a5dfSJaegeuk Kim 
190655523519SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
1907c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_BLOCK);
19080abd675eSChao Yu 		release = *count;
19099ea2f0beSChao Yu 		goto release_quota;
191055523519SChao Yu 	}
19117fa750a1SArnd Bergmann 
1912dd11a5dfSJaegeuk Kim 	/*
1913dd11a5dfSJaegeuk Kim 	 * let's increase this in prior to actual block count change in order
1914dd11a5dfSJaegeuk Kim 	 * for f2fs_sync_file to avoid data races when deciding checkpoint.
1915dd11a5dfSJaegeuk Kim 	 */
1916dd11a5dfSJaegeuk Kim 	percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
191739a53e0cSJaegeuk Kim 
191839a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
19192555a2d5SJaegeuk Kim 	sbi->total_valid_block_count += (block_t)(*count);
192080d42145SYunlong Song 	avail_user_block_count = sbi->user_block_count -
192180d42145SYunlong Song 					sbi->current_reserved_blocks;
19227e65be49SJaegeuk Kim 
1923a90a0884SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode, true))
192463189b78SChao Yu 		avail_user_block_count -= F2FS_OPTION(sbi).root_reserved_blocks;
1925a4c3ecaaSDaniel Rosenberg 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
1926a4c3ecaaSDaniel Rosenberg 		if (avail_user_block_count > sbi->unusable_block_count)
19274354994fSDaniel Rosenberg 			avail_user_block_count -= sbi->unusable_block_count;
1928a4c3ecaaSDaniel Rosenberg 		else
1929a4c3ecaaSDaniel Rosenberg 			avail_user_block_count = 0;
1930a4c3ecaaSDaniel Rosenberg 	}
1931daeb433eSChao Yu 	if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
1932daeb433eSChao Yu 		diff = sbi->total_valid_block_count - avail_user_block_count;
19337e65be49SJaegeuk Kim 		if (diff > *count)
19347e65be49SJaegeuk Kim 			diff = *count;
1935dd11a5dfSJaegeuk Kim 		*count -= diff;
19360abd675eSChao Yu 		release = diff;
19377e65be49SJaegeuk Kim 		sbi->total_valid_block_count -= diff;
193846008c6dSChao Yu 		if (!*count) {
193939a53e0cSJaegeuk Kim 			spin_unlock(&sbi->stat_lock);
19400abd675eSChao Yu 			goto enospc;
194139a53e0cSJaegeuk Kim 		}
194246008c6dSChao Yu 	}
194339a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
194441382ec4SJaegeuk Kim 
194536b877afSDaniel Rosenberg 	if (unlikely(release)) {
194636b877afSDaniel Rosenberg 		percpu_counter_sub(&sbi->alloc_valid_block_count, release);
19470abd675eSChao Yu 		dquot_release_reservation_block(inode, release);
194836b877afSDaniel Rosenberg 	}
19490abd675eSChao Yu 	f2fs_i_blocks_write(inode, *count, true, true);
19500abd675eSChao Yu 	return 0;
19510abd675eSChao Yu 
19520abd675eSChao Yu enospc:
195336b877afSDaniel Rosenberg 	percpu_counter_sub(&sbi->alloc_valid_block_count, release);
19549ea2f0beSChao Yu release_quota:
19550abd675eSChao Yu 	dquot_release_reservation_block(inode, release);
19560abd675eSChao Yu 	return -ENOSPC;
195739a53e0cSJaegeuk Kim }
195839a53e0cSJaegeuk Kim 
1959dcbb4c10SJoe Perches __printf(2, 3)
1960dcbb4c10SJoe Perches void f2fs_printk(struct f2fs_sb_info *sbi, const char *fmt, ...);
1961dcbb4c10SJoe Perches 
1962dcbb4c10SJoe Perches #define f2fs_err(sbi, fmt, ...)						\
1963dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_ERR fmt, ##__VA_ARGS__)
1964dcbb4c10SJoe Perches #define f2fs_warn(sbi, fmt, ...)					\
1965dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_WARNING fmt, ##__VA_ARGS__)
1966dcbb4c10SJoe Perches #define f2fs_notice(sbi, fmt, ...)					\
1967dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_NOTICE fmt, ##__VA_ARGS__)
1968dcbb4c10SJoe Perches #define f2fs_info(sbi, fmt, ...)					\
1969dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_INFO fmt, ##__VA_ARGS__)
1970dcbb4c10SJoe Perches #define f2fs_debug(sbi, fmt, ...)					\
1971dcbb4c10SJoe Perches 	f2fs_printk(sbi, KERN_DEBUG fmt, ##__VA_ARGS__)
1972dcbb4c10SJoe Perches 
1973da19b0dcSGu Zheng static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
197439a53e0cSJaegeuk Kim 						struct inode *inode,
19750eb0adadSChao Yu 						block_t count)
197639a53e0cSJaegeuk Kim {
19770eb0adadSChao Yu 	blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK;
19780eb0adadSChao Yu 
197939a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
19809850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
198139a53e0cSJaegeuk Kim 	sbi->total_valid_block_count -= (block_t)count;
198280d42145SYunlong Song 	if (sbi->reserved_blocks &&
198380d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
198480d42145SYunlong Song 		sbi->current_reserved_blocks = min(sbi->reserved_blocks,
198580d42145SYunlong Song 					sbi->current_reserved_blocks + count);
198639a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
19875e159cd3SChao Yu 	if (unlikely(inode->i_blocks < sectors)) {
1988dcbb4c10SJoe Perches 		f2fs_warn(sbi, "Inconsistent i_blocks, ino:%lu, iblocks:%llu, sectors:%llu",
19895e159cd3SChao Yu 			  inode->i_ino,
19905e159cd3SChao Yu 			  (unsigned long long)inode->i_blocks,
19915e159cd3SChao Yu 			  (unsigned long long)sectors);
19925e159cd3SChao Yu 		set_sbi_flag(sbi, SBI_NEED_FSCK);
19935e159cd3SChao Yu 		return;
19945e159cd3SChao Yu 	}
19950abd675eSChao Yu 	f2fs_i_blocks_write(inode, count, false, true);
199639a53e0cSJaegeuk Kim }
199739a53e0cSJaegeuk Kim 
199839a53e0cSJaegeuk Kim static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
199939a53e0cSJaegeuk Kim {
200035782b23SJaegeuk Kim 	atomic_inc(&sbi->nr_pages[count_type]);
20017c4abcbeSChao Yu 
200220109873SChao Yu 	if (count_type == F2FS_DIRTY_DENTS ||
200320109873SChao Yu 			count_type == F2FS_DIRTY_NODES ||
200420109873SChao Yu 			count_type == F2FS_DIRTY_META ||
200520109873SChao Yu 			count_type == F2FS_DIRTY_QDATA ||
200620109873SChao Yu 			count_type == F2FS_DIRTY_IMETA)
2007caf0047eSChao Yu 		set_sbi_flag(sbi, SBI_IS_DIRTY);
200839a53e0cSJaegeuk Kim }
200939a53e0cSJaegeuk Kim 
2010a7ffdbe2SJaegeuk Kim static inline void inode_inc_dirty_pages(struct inode *inode)
201139a53e0cSJaegeuk Kim {
2012204706c7SJaegeuk Kim 	atomic_inc(&F2FS_I(inode)->dirty_pages);
2013c227f912SChao Yu 	inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
2014c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
20152c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
20162c8a4a28SJaegeuk Kim 		inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
201739a53e0cSJaegeuk Kim }
201839a53e0cSJaegeuk Kim 
201939a53e0cSJaegeuk Kim static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
202039a53e0cSJaegeuk Kim {
202135782b23SJaegeuk Kim 	atomic_dec(&sbi->nr_pages[count_type]);
202239a53e0cSJaegeuk Kim }
202339a53e0cSJaegeuk Kim 
2024a7ffdbe2SJaegeuk Kim static inline void inode_dec_dirty_pages(struct inode *inode)
202539a53e0cSJaegeuk Kim {
20265ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
20275ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
20281fe54f9dSJaegeuk Kim 		return;
20291fe54f9dSJaegeuk Kim 
2030204706c7SJaegeuk Kim 	atomic_dec(&F2FS_I(inode)->dirty_pages);
2031c227f912SChao Yu 	dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
2032c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
20332c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
20342c8a4a28SJaegeuk Kim 		dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
203539a53e0cSJaegeuk Kim }
203639a53e0cSJaegeuk Kim 
2037523be8a6SJaegeuk Kim static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
203839a53e0cSJaegeuk Kim {
203935782b23SJaegeuk Kim 	return atomic_read(&sbi->nr_pages[count_type]);
204039a53e0cSJaegeuk Kim }
204139a53e0cSJaegeuk Kim 
2042204706c7SJaegeuk Kim static inline int get_dirty_pages(struct inode *inode)
2043f8b2c1f9SJaegeuk Kim {
2044204706c7SJaegeuk Kim 	return atomic_read(&F2FS_I(inode)->dirty_pages);
2045f8b2c1f9SJaegeuk Kim }
2046f8b2c1f9SJaegeuk Kim 
20475ac206cfSNamjae Jeon static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
20485ac206cfSNamjae Jeon {
20493519e3f9SChao Yu 	unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
2050523be8a6SJaegeuk Kim 	unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
2051523be8a6SJaegeuk Kim 						sbi->log_blocks_per_seg;
2052523be8a6SJaegeuk Kim 
2053523be8a6SJaegeuk Kim 	return segs / sbi->segs_per_sec;
20545ac206cfSNamjae Jeon }
20555ac206cfSNamjae Jeon 
205639a53e0cSJaegeuk Kim static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
205739a53e0cSJaegeuk Kim {
20588b8343faSJaegeuk Kim 	return sbi->total_valid_block_count;
205939a53e0cSJaegeuk Kim }
206039a53e0cSJaegeuk Kim 
2061f83a2584SYunlei He static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
2062f83a2584SYunlei He {
2063f83a2584SYunlei He 	return sbi->discard_blks;
2064f83a2584SYunlei He }
2065f83a2584SYunlei He 
206639a53e0cSJaegeuk Kim static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
206739a53e0cSJaegeuk Kim {
206839a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
206939a53e0cSJaegeuk Kim 
207039a53e0cSJaegeuk Kim 	/* return NAT or SIT bitmap */
207139a53e0cSJaegeuk Kim 	if (flag == NAT_BITMAP)
207239a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
207339a53e0cSJaegeuk Kim 	else if (flag == SIT_BITMAP)
207439a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
207539a53e0cSJaegeuk Kim 
207639a53e0cSJaegeuk Kim 	return 0;
207739a53e0cSJaegeuk Kim }
207839a53e0cSJaegeuk Kim 
207955141486SWanpeng Li static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
208055141486SWanpeng Li {
208155141486SWanpeng Li 	return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
208255141486SWanpeng Li }
208355141486SWanpeng Li 
208439a53e0cSJaegeuk Kim static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
208539a53e0cSJaegeuk Kim {
208639a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
20871dbe4152SChangman Lee 	int offset;
20881dbe4152SChangman Lee 
2089199bc3feSChao Yu 	if (is_set_ckpt_flags(sbi, CP_LARGE_NAT_BITMAP_FLAG)) {
2090199bc3feSChao Yu 		offset = (flag == SIT_BITMAP) ?
2091199bc3feSChao Yu 			le32_to_cpu(ckpt->nat_ver_bitmap_bytesize) : 0;
2092b471eb99SChao Yu 		/*
2093b471eb99SChao Yu 		 * if large_nat_bitmap feature is enabled, leave checksum
2094b471eb99SChao Yu 		 * protection for all nat/sit bitmaps.
2095b471eb99SChao Yu 		 */
2096b471eb99SChao Yu 		return &ckpt->sit_nat_version_bitmap + offset + sizeof(__le32);
2097199bc3feSChao Yu 	}
2098199bc3feSChao Yu 
209955141486SWanpeng Li 	if (__cp_payload(sbi) > 0) {
21001dbe4152SChangman Lee 		if (flag == NAT_BITMAP)
21011dbe4152SChangman Lee 			return &ckpt->sit_nat_version_bitmap;
21021dbe4152SChangman Lee 		else
210365b85cccSJaegeuk Kim 			return (unsigned char *)ckpt + F2FS_BLKSIZE;
21041dbe4152SChangman Lee 	} else {
21051dbe4152SChangman Lee 		offset = (flag == NAT_BITMAP) ?
210625ca923bSJaegeuk Kim 			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
210739a53e0cSJaegeuk Kim 		return &ckpt->sit_nat_version_bitmap + offset;
210839a53e0cSJaegeuk Kim 	}
21091dbe4152SChangman Lee }
211039a53e0cSJaegeuk Kim 
211139a53e0cSJaegeuk Kim static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
211239a53e0cSJaegeuk Kim {
21138508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
211439a53e0cSJaegeuk Kim 
21158508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 2)
211639a53e0cSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
211739a53e0cSJaegeuk Kim 	return start_addr;
211839a53e0cSJaegeuk Kim }
211939a53e0cSJaegeuk Kim 
21208508e44aSJaegeuk Kim static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
21218508e44aSJaegeuk Kim {
21228508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
21238508e44aSJaegeuk Kim 
21248508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 1)
21258508e44aSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
21268508e44aSJaegeuk Kim 	return start_addr;
21278508e44aSJaegeuk Kim }
21288508e44aSJaegeuk Kim 
21298508e44aSJaegeuk Kim static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
21308508e44aSJaegeuk Kim {
21318508e44aSJaegeuk Kim 	sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
21328508e44aSJaegeuk Kim }
21338508e44aSJaegeuk Kim 
213439a53e0cSJaegeuk Kim static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
213539a53e0cSJaegeuk Kim {
213639a53e0cSJaegeuk Kim 	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
213739a53e0cSJaegeuk Kim }
213839a53e0cSJaegeuk Kim 
21390abd675eSChao Yu static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
2140000519f2SChao Yu 					struct inode *inode, bool is_inode)
214139a53e0cSJaegeuk Kim {
214239a53e0cSJaegeuk Kim 	block_t	valid_block_count;
2143a4c3ecaaSDaniel Rosenberg 	unsigned int valid_node_count, user_block_count;
2144af033b2aSChao Yu 	int err;
21450abd675eSChao Yu 
2146af033b2aSChao Yu 	if (is_inode) {
2147af033b2aSChao Yu 		if (inode) {
2148af033b2aSChao Yu 			err = dquot_alloc_inode(inode);
2149af033b2aSChao Yu 			if (err)
2150af033b2aSChao Yu 				return err;
2151af033b2aSChao Yu 		}
2152af033b2aSChao Yu 	} else {
2153af033b2aSChao Yu 		err = dquot_reserve_block(inode, 1);
2154af033b2aSChao Yu 		if (err)
2155af033b2aSChao Yu 			return err;
21560abd675eSChao Yu 	}
215739a53e0cSJaegeuk Kim 
2158812c6056SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
2159c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_BLOCK);
2160812c6056SChao Yu 		goto enospc;
2161812c6056SChao Yu 	}
2162812c6056SChao Yu 
216339a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
216439a53e0cSJaegeuk Kim 
21657e65be49SJaegeuk Kim 	valid_block_count = sbi->total_valid_block_count +
21667e65be49SJaegeuk Kim 					sbi->current_reserved_blocks + 1;
21677e65be49SJaegeuk Kim 
2168a90a0884SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode, false))
216963189b78SChao Yu 		valid_block_count += F2FS_OPTION(sbi).root_reserved_blocks;
2170a4c3ecaaSDaniel Rosenberg 	user_block_count = sbi->user_block_count;
21714354994fSDaniel Rosenberg 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2172a4c3ecaaSDaniel Rosenberg 		user_block_count -= sbi->unusable_block_count;
21737e65be49SJaegeuk Kim 
2174a4c3ecaaSDaniel Rosenberg 	if (unlikely(valid_block_count > user_block_count)) {
217539a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
21760abd675eSChao Yu 		goto enospc;
217739a53e0cSJaegeuk Kim 	}
217839a53e0cSJaegeuk Kim 
2179ef86d709SGu Zheng 	valid_node_count = sbi->total_valid_node_count + 1;
2180cfb271d4SChao Yu 	if (unlikely(valid_node_count > sbi->total_node_count)) {
218139a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
21820abd675eSChao Yu 		goto enospc;
218339a53e0cSJaegeuk Kim 	}
218439a53e0cSJaegeuk Kim 
2185ef86d709SGu Zheng 	sbi->total_valid_node_count++;
2186ef86d709SGu Zheng 	sbi->total_valid_block_count++;
218739a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
218839a53e0cSJaegeuk Kim 
21890abd675eSChao Yu 	if (inode) {
21900abd675eSChao Yu 		if (is_inode)
21910abd675eSChao Yu 			f2fs_mark_inode_dirty_sync(inode, true);
21920abd675eSChao Yu 		else
21930abd675eSChao Yu 			f2fs_i_blocks_write(inode, 1, true, true);
21940abd675eSChao Yu 	}
21950abd675eSChao Yu 
219641382ec4SJaegeuk Kim 	percpu_counter_inc(&sbi->alloc_valid_block_count);
21970abd675eSChao Yu 	return 0;
21980abd675eSChao Yu 
21990abd675eSChao Yu enospc:
2200af033b2aSChao Yu 	if (is_inode) {
2201af033b2aSChao Yu 		if (inode)
2202af033b2aSChao Yu 			dquot_free_inode(inode);
2203af033b2aSChao Yu 	} else {
22040abd675eSChao Yu 		dquot_release_reservation_block(inode, 1);
2205af033b2aSChao Yu 	}
22060abd675eSChao Yu 	return -ENOSPC;
220739a53e0cSJaegeuk Kim }
220839a53e0cSJaegeuk Kim 
220939a53e0cSJaegeuk Kim static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
2210000519f2SChao Yu 					struct inode *inode, bool is_inode)
221139a53e0cSJaegeuk Kim {
221239a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
221339a53e0cSJaegeuk Kim 
22149850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_block_count);
22159850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_node_count);
221639a53e0cSJaegeuk Kim 
2217ef86d709SGu Zheng 	sbi->total_valid_node_count--;
2218ef86d709SGu Zheng 	sbi->total_valid_block_count--;
221980d42145SYunlong Song 	if (sbi->reserved_blocks &&
222080d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
222180d42145SYunlong Song 		sbi->current_reserved_blocks++;
222239a53e0cSJaegeuk Kim 
222339a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
22240abd675eSChao Yu 
2225ea6d7e72SChao Yu 	if (is_inode) {
2226af033b2aSChao Yu 		dquot_free_inode(inode);
2227ea6d7e72SChao Yu 	} else {
2228ea6d7e72SChao Yu 		if (unlikely(inode->i_blocks == 0)) {
2229097a7686SChao Yu 			f2fs_warn(sbi, "dec_valid_node_count: inconsistent i_blocks, ino:%lu, iblocks:%llu",
2230ea6d7e72SChao Yu 				  inode->i_ino,
2231ea6d7e72SChao Yu 				  (unsigned long long)inode->i_blocks);
2232ea6d7e72SChao Yu 			set_sbi_flag(sbi, SBI_NEED_FSCK);
2233ea6d7e72SChao Yu 			return;
2234ea6d7e72SChao Yu 		}
22350abd675eSChao Yu 		f2fs_i_blocks_write(inode, 1, false, true);
223639a53e0cSJaegeuk Kim 	}
2237ea6d7e72SChao Yu }
223839a53e0cSJaegeuk Kim 
223939a53e0cSJaegeuk Kim static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
224039a53e0cSJaegeuk Kim {
22418b8343faSJaegeuk Kim 	return sbi->total_valid_node_count;
224239a53e0cSJaegeuk Kim }
224339a53e0cSJaegeuk Kim 
224439a53e0cSJaegeuk Kim static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
224539a53e0cSJaegeuk Kim {
2246513c5f37SJaegeuk Kim 	percpu_counter_inc(&sbi->total_valid_inode_count);
224739a53e0cSJaegeuk Kim }
224839a53e0cSJaegeuk Kim 
22490e80220aSJaegeuk Kim static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
225039a53e0cSJaegeuk Kim {
2251513c5f37SJaegeuk Kim 	percpu_counter_dec(&sbi->total_valid_inode_count);
225239a53e0cSJaegeuk Kim }
225339a53e0cSJaegeuk Kim 
2254513c5f37SJaegeuk Kim static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
225539a53e0cSJaegeuk Kim {
2256513c5f37SJaegeuk Kim 	return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
225739a53e0cSJaegeuk Kim }
225839a53e0cSJaegeuk Kim 
2259a56c7c6fSJaegeuk Kim static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
2260a56c7c6fSJaegeuk Kim 						pgoff_t index, bool for_write)
2261a56c7c6fSJaegeuk Kim {
226282cf4f13SChao Yu 	struct page *page;
2263cac5a3d8SDongOh Shin 
22647fa750a1SArnd Bergmann 	if (IS_ENABLED(CONFIG_F2FS_FAULT_INJECTION)) {
226582cf4f13SChao Yu 		if (!for_write)
226682cf4f13SChao Yu 			page = find_get_page_flags(mapping, index,
226782cf4f13SChao Yu 							FGP_LOCK | FGP_ACCESSED);
226882cf4f13SChao Yu 		else
226982cf4f13SChao Yu 			page = find_lock_page(mapping, index);
2270c41f3cc3SJaegeuk Kim 		if (page)
2271c41f3cc3SJaegeuk Kim 			return page;
2272c41f3cc3SJaegeuk Kim 
227355523519SChao Yu 		if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) {
2274c45d6002SChao Yu 			f2fs_show_injection_info(F2FS_M_SB(mapping),
2275c45d6002SChao Yu 							FAULT_PAGE_ALLOC);
2276c41f3cc3SJaegeuk Kim 			return NULL;
227755523519SChao Yu 		}
22787fa750a1SArnd Bergmann 	}
22797fa750a1SArnd Bergmann 
2280a56c7c6fSJaegeuk Kim 	if (!for_write)
2281a56c7c6fSJaegeuk Kim 		return grab_cache_page(mapping, index);
2282a56c7c6fSJaegeuk Kim 	return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
2283a56c7c6fSJaegeuk Kim }
2284a56c7c6fSJaegeuk Kim 
228501eccef7SChao Yu static inline struct page *f2fs_pagecache_get_page(
228601eccef7SChao Yu 				struct address_space *mapping, pgoff_t index,
228701eccef7SChao Yu 				int fgp_flags, gfp_t gfp_mask)
228801eccef7SChao Yu {
228901eccef7SChao Yu 	if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_GET)) {
2290c45d6002SChao Yu 		f2fs_show_injection_info(F2FS_M_SB(mapping), FAULT_PAGE_GET);
229101eccef7SChao Yu 		return NULL;
229201eccef7SChao Yu 	}
22937fa750a1SArnd Bergmann 
229401eccef7SChao Yu 	return pagecache_get_page(mapping, index, fgp_flags, gfp_mask);
229501eccef7SChao Yu }
229601eccef7SChao Yu 
22976e2c64adSJaegeuk Kim static inline void f2fs_copy_page(struct page *src, struct page *dst)
22986e2c64adSJaegeuk Kim {
22996e2c64adSJaegeuk Kim 	char *src_kaddr = kmap(src);
23006e2c64adSJaegeuk Kim 	char *dst_kaddr = kmap(dst);
23016e2c64adSJaegeuk Kim 
23026e2c64adSJaegeuk Kim 	memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
23036e2c64adSJaegeuk Kim 	kunmap(dst);
23046e2c64adSJaegeuk Kim 	kunmap(src);
23056e2c64adSJaegeuk Kim }
23066e2c64adSJaegeuk Kim 
230739a53e0cSJaegeuk Kim static inline void f2fs_put_page(struct page *page, int unlock)
230839a53e0cSJaegeuk Kim {
2309031fa8ccSJaegeuk Kim 	if (!page)
231039a53e0cSJaegeuk Kim 		return;
231139a53e0cSJaegeuk Kim 
231239a53e0cSJaegeuk Kim 	if (unlock) {
23139850cf4aSJaegeuk Kim 		f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
231439a53e0cSJaegeuk Kim 		unlock_page(page);
231539a53e0cSJaegeuk Kim 	}
231609cbfeafSKirill A. Shutemov 	put_page(page);
231739a53e0cSJaegeuk Kim }
231839a53e0cSJaegeuk Kim 
231939a53e0cSJaegeuk Kim static inline void f2fs_put_dnode(struct dnode_of_data *dn)
232039a53e0cSJaegeuk Kim {
232139a53e0cSJaegeuk Kim 	if (dn->node_page)
232239a53e0cSJaegeuk Kim 		f2fs_put_page(dn->node_page, 1);
232339a53e0cSJaegeuk Kim 	if (dn->inode_page && dn->node_page != dn->inode_page)
232439a53e0cSJaegeuk Kim 		f2fs_put_page(dn->inode_page, 0);
232539a53e0cSJaegeuk Kim 	dn->node_page = NULL;
232639a53e0cSJaegeuk Kim 	dn->inode_page = NULL;
232739a53e0cSJaegeuk Kim }
232839a53e0cSJaegeuk Kim 
232939a53e0cSJaegeuk Kim static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
2330e8512d2eSGu Zheng 					size_t size)
233139a53e0cSJaegeuk Kim {
2332e8512d2eSGu Zheng 	return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
233339a53e0cSJaegeuk Kim }
233439a53e0cSJaegeuk Kim 
23357bd59381SGu Zheng static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
23367bd59381SGu Zheng 						gfp_t flags)
23377bd59381SGu Zheng {
23387bd59381SGu Zheng 	void *entry;
23397bd59381SGu Zheng 
234080c54505SJaegeuk Kim 	entry = kmem_cache_alloc(cachep, flags);
234180c54505SJaegeuk Kim 	if (!entry)
234280c54505SJaegeuk Kim 		entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
23437bd59381SGu Zheng 	return entry;
23447bd59381SGu Zheng }
23457bd59381SGu Zheng 
23465f9abab4SJaegeuk Kim static inline bool is_idle(struct f2fs_sb_info *sbi, int type)
23475f9abab4SJaegeuk Kim {
2348f7dfd9f3SPark Ju Hyung 	if (sbi->gc_mode == GC_URGENT)
2349f7dfd9f3SPark Ju Hyung 		return true;
2350f7dfd9f3SPark Ju Hyung 
23515f9abab4SJaegeuk Kim 	if (get_pages(sbi, F2FS_RD_DATA) || get_pages(sbi, F2FS_RD_NODE) ||
23525f9abab4SJaegeuk Kim 		get_pages(sbi, F2FS_RD_META) || get_pages(sbi, F2FS_WB_DATA) ||
2353fef4129eSChao Yu 		get_pages(sbi, F2FS_WB_CP_DATA) ||
2354fef4129eSChao Yu 		get_pages(sbi, F2FS_DIO_READ) ||
235511ac8ef8SJaegeuk Kim 		get_pages(sbi, F2FS_DIO_WRITE))
235611ac8ef8SJaegeuk Kim 		return false;
235711ac8ef8SJaegeuk Kim 
235856659ce8SSahitya Tummala 	if (type != DISCARD_TIME && SM_I(sbi) && SM_I(sbi)->dcc_info &&
235911ac8ef8SJaegeuk Kim 			atomic_read(&SM_I(sbi)->dcc_info->queued_discard))
236011ac8ef8SJaegeuk Kim 		return false;
236111ac8ef8SJaegeuk Kim 
236211ac8ef8SJaegeuk Kim 	if (SM_I(sbi) && SM_I(sbi)->fcc_info &&
236372691af6SJaegeuk Kim 			atomic_read(&SM_I(sbi)->fcc_info->queued_flush))
23645f9abab4SJaegeuk Kim 		return false;
236511ac8ef8SJaegeuk Kim 
23665f9abab4SJaegeuk Kim 	return f2fs_time_over(sbi, type);
23675f9abab4SJaegeuk Kim }
23685f9abab4SJaegeuk Kim 
23699be32d72SJaegeuk Kim static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
23709be32d72SJaegeuk Kim 				unsigned long index, void *item)
23719be32d72SJaegeuk Kim {
23729be32d72SJaegeuk Kim 	while (radix_tree_insert(root, index, item))
23739be32d72SJaegeuk Kim 		cond_resched();
23749be32d72SJaegeuk Kim }
23759be32d72SJaegeuk Kim 
237639a53e0cSJaegeuk Kim #define RAW_IS_INODE(p)	((p)->footer.nid == (p)->footer.ino)
237739a53e0cSJaegeuk Kim 
237839a53e0cSJaegeuk Kim static inline bool IS_INODE(struct page *page)
237939a53e0cSJaegeuk Kim {
238045590710SGu Zheng 	struct f2fs_node *p = F2FS_NODE(page);
2381cac5a3d8SDongOh Shin 
238239a53e0cSJaegeuk Kim 	return RAW_IS_INODE(p);
238339a53e0cSJaegeuk Kim }
238439a53e0cSJaegeuk Kim 
23857a2af766SChao Yu static inline int offset_in_addr(struct f2fs_inode *i)
23867a2af766SChao Yu {
23877a2af766SChao Yu 	return (i->i_inline & F2FS_EXTRA_ATTR) ?
23887a2af766SChao Yu 			(le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0;
23897a2af766SChao Yu }
23907a2af766SChao Yu 
239139a53e0cSJaegeuk Kim static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
239239a53e0cSJaegeuk Kim {
239339a53e0cSJaegeuk Kim 	return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
239439a53e0cSJaegeuk Kim }
239539a53e0cSJaegeuk Kim 
23967a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode);
2397a2ced1ceSChao Yu static inline block_t data_blkaddr(struct inode *inode,
23987a2af766SChao Yu 			struct page *node_page, unsigned int offset)
239939a53e0cSJaegeuk Kim {
240039a53e0cSJaegeuk Kim 	struct f2fs_node *raw_node;
240139a53e0cSJaegeuk Kim 	__le32 *addr_array;
24027a2af766SChao Yu 	int base = 0;
24037a2af766SChao Yu 	bool is_inode = IS_INODE(node_page);
2404cac5a3d8SDongOh Shin 
240545590710SGu Zheng 	raw_node = F2FS_NODE(node_page);
24067a2af766SChao Yu 
2407979f492fSLiFan 	if (is_inode) {
2408979f492fSLiFan 		if (!inode)
24097a88ddb5SChao Yu 			/* from GC path only */
24107a2af766SChao Yu 			base = offset_in_addr(&raw_node->i);
2411979f492fSLiFan 		else if (f2fs_has_extra_attr(inode))
24127a2af766SChao Yu 			base = get_extra_isize(inode);
24137a2af766SChao Yu 	}
24147a2af766SChao Yu 
241539a53e0cSJaegeuk Kim 	addr_array = blkaddr_in_node(raw_node);
24167a2af766SChao Yu 	return le32_to_cpu(addr_array[base + offset]);
241739a53e0cSJaegeuk Kim }
241839a53e0cSJaegeuk Kim 
2419a2ced1ceSChao Yu static inline block_t f2fs_data_blkaddr(struct dnode_of_data *dn)
2420a2ced1ceSChao Yu {
2421a2ced1ceSChao Yu 	return data_blkaddr(dn->inode, dn->node_page, dn->ofs_in_node);
2422a2ced1ceSChao Yu }
2423a2ced1ceSChao Yu 
242439a53e0cSJaegeuk Kim static inline int f2fs_test_bit(unsigned int nr, char *addr)
242539a53e0cSJaegeuk Kim {
242639a53e0cSJaegeuk Kim 	int mask;
242739a53e0cSJaegeuk Kim 
242839a53e0cSJaegeuk Kim 	addr += (nr >> 3);
242939a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
243039a53e0cSJaegeuk Kim 	return mask & *addr;
243139a53e0cSJaegeuk Kim }
243239a53e0cSJaegeuk Kim 
2433a66cdd98SJaegeuk Kim static inline void f2fs_set_bit(unsigned int nr, char *addr)
2434a66cdd98SJaegeuk Kim {
2435a66cdd98SJaegeuk Kim 	int mask;
2436a66cdd98SJaegeuk Kim 
2437a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2438a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2439a66cdd98SJaegeuk Kim 	*addr |= mask;
2440a66cdd98SJaegeuk Kim }
2441a66cdd98SJaegeuk Kim 
2442a66cdd98SJaegeuk Kim static inline void f2fs_clear_bit(unsigned int nr, char *addr)
2443a66cdd98SJaegeuk Kim {
2444a66cdd98SJaegeuk Kim 	int mask;
2445a66cdd98SJaegeuk Kim 
2446a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2447a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2448a66cdd98SJaegeuk Kim 	*addr &= ~mask;
2449a66cdd98SJaegeuk Kim }
2450a66cdd98SJaegeuk Kim 
245152aca074SGu Zheng static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
245239a53e0cSJaegeuk Kim {
245339a53e0cSJaegeuk Kim 	int mask;
245439a53e0cSJaegeuk Kim 	int ret;
245539a53e0cSJaegeuk Kim 
245639a53e0cSJaegeuk Kim 	addr += (nr >> 3);
245739a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
245839a53e0cSJaegeuk Kim 	ret = mask & *addr;
245939a53e0cSJaegeuk Kim 	*addr |= mask;
246039a53e0cSJaegeuk Kim 	return ret;
246139a53e0cSJaegeuk Kim }
246239a53e0cSJaegeuk Kim 
246352aca074SGu Zheng static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
246439a53e0cSJaegeuk Kim {
246539a53e0cSJaegeuk Kim 	int mask;
246639a53e0cSJaegeuk Kim 	int ret;
246739a53e0cSJaegeuk Kim 
246839a53e0cSJaegeuk Kim 	addr += (nr >> 3);
246939a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
247039a53e0cSJaegeuk Kim 	ret = mask & *addr;
247139a53e0cSJaegeuk Kim 	*addr &= ~mask;
247239a53e0cSJaegeuk Kim 	return ret;
247339a53e0cSJaegeuk Kim }
247439a53e0cSJaegeuk Kim 
2475c6ac4c0eSGu Zheng static inline void f2fs_change_bit(unsigned int nr, char *addr)
2476c6ac4c0eSGu Zheng {
2477c6ac4c0eSGu Zheng 	int mask;
2478c6ac4c0eSGu Zheng 
2479c6ac4c0eSGu Zheng 	addr += (nr >> 3);
2480c6ac4c0eSGu Zheng 	mask = 1 << (7 - (nr & 0x07));
2481c6ac4c0eSGu Zheng 	*addr ^= mask;
2482c6ac4c0eSGu Zheng }
2483c6ac4c0eSGu Zheng 
248459c84408SChao Yu /*
248536098557SEric Biggers  * On-disk inode flags (f2fs_inode::i_flags)
248659c84408SChao Yu  */
24874c8ff709SChao Yu #define F2FS_COMPR_FL			0x00000004 /* Compress file */
248859c84408SChao Yu #define F2FS_SYNC_FL			0x00000008 /* Synchronous updates */
248959c84408SChao Yu #define F2FS_IMMUTABLE_FL		0x00000010 /* Immutable file */
249059c84408SChao Yu #define F2FS_APPEND_FL			0x00000020 /* writes to file may only append */
249159c84408SChao Yu #define F2FS_NODUMP_FL			0x00000040 /* do not dump file */
249259c84408SChao Yu #define F2FS_NOATIME_FL			0x00000080 /* do not update atime */
24934c8ff709SChao Yu #define F2FS_NOCOMP_FL			0x00000400 /* Don't compress */
249459c84408SChao Yu #define F2FS_INDEX_FL			0x00001000 /* hash-indexed directory */
249559c84408SChao Yu #define F2FS_DIRSYNC_FL			0x00010000 /* dirsync behaviour (directories only) */
249659c84408SChao Yu #define F2FS_PROJINHERIT_FL		0x20000000 /* Create with parents projid */
24972c2eb7a3SDaniel Rosenberg #define F2FS_CASEFOLD_FL		0x40000000 /* Casefolded file */
249859c84408SChao Yu 
249959c84408SChao Yu /* Flags that should be inherited by new inodes from their parent. */
250036098557SEric Biggers #define F2FS_FL_INHERITED (F2FS_SYNC_FL | F2FS_NODUMP_FL | F2FS_NOATIME_FL | \
25012c2eb7a3SDaniel Rosenberg 			   F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL | \
25024c8ff709SChao Yu 			   F2FS_CASEFOLD_FL | F2FS_COMPR_FL | F2FS_NOCOMP_FL)
250359c84408SChao Yu 
250459c84408SChao Yu /* Flags that are appropriate for regular files (all but dir-specific ones). */
25052c2eb7a3SDaniel Rosenberg #define F2FS_REG_FLMASK		(~(F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL | \
25062c2eb7a3SDaniel Rosenberg 				F2FS_CASEFOLD_FL))
250759c84408SChao Yu 
250859c84408SChao Yu /* Flags that are appropriate for non-directories/regular files. */
250959c84408SChao Yu #define F2FS_OTHER_FLMASK	(F2FS_NODUMP_FL | F2FS_NOATIME_FL)
25105c57132eSChao Yu 
25115c57132eSChao Yu static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
25125c57132eSChao Yu {
25135c57132eSChao Yu 	if (S_ISDIR(mode))
25145c57132eSChao Yu 		return flags;
25155c57132eSChao Yu 	else if (S_ISREG(mode))
25165c57132eSChao Yu 		return flags & F2FS_REG_FLMASK;
25175c57132eSChao Yu 	else
25185c57132eSChao Yu 		return flags & F2FS_OTHER_FLMASK;
25195c57132eSChao Yu }
25205c57132eSChao Yu 
252139a53e0cSJaegeuk Kim /* used for f2fs_inode_info->flags */
252239a53e0cSJaegeuk Kim enum {
252339a53e0cSJaegeuk Kim 	FI_NEW_INODE,		/* indicate newly allocated inode */
2524b3783873SJaegeuk Kim 	FI_DIRTY_INODE,		/* indicate inode is dirty or not */
252526de9b11SJaegeuk Kim 	FI_AUTO_RECOVER,	/* indicate inode is recoverable */
2526ed57c27fSJaegeuk Kim 	FI_DIRTY_DIR,		/* indicate directory has dirty pages */
252739a53e0cSJaegeuk Kim 	FI_INC_LINK,		/* need to increment i_nlink */
252839a53e0cSJaegeuk Kim 	FI_ACL_MODE,		/* indicate acl mode */
252939a53e0cSJaegeuk Kim 	FI_NO_ALLOC,		/* should not allocate any blocks */
2530c9b63bd0SJaegeuk Kim 	FI_FREE_NID,		/* free allocated nide */
2531c11abd1aSJaegeuk Kim 	FI_NO_EXTENT,		/* not to use the extent cache */
2532444c580fSJaegeuk Kim 	FI_INLINE_XATTR,	/* used for inline xattr */
25331001b347SHuajun Li 	FI_INLINE_DATA,		/* used for inline data*/
253434d67debSChao Yu 	FI_INLINE_DENTRY,	/* used for inline dentry */
2535fff04f90SJaegeuk Kim 	FI_APPEND_WRITE,	/* inode has appended data */
2536fff04f90SJaegeuk Kim 	FI_UPDATE_WRITE,	/* inode has in-place-update data */
253788b88a66SJaegeuk Kim 	FI_NEED_IPU,		/* used for ipu per file */
253888b88a66SJaegeuk Kim 	FI_ATOMIC_FILE,		/* indicate atomic file */
25395fe45743SChao Yu 	FI_ATOMIC_COMMIT,	/* indicate the state of atomical committing */
254002a1335fSJaegeuk Kim 	FI_VOLATILE_FILE,	/* indicate volatile file */
25413c6c2bebSJaegeuk Kim 	FI_FIRST_BLOCK_WRITTEN,	/* indicate #0 data block was written */
25421e84371fSJaegeuk Kim 	FI_DROP_CACHE,		/* drop dirty page cache */
2543b3d208f9SJaegeuk Kim 	FI_DATA_EXIST,		/* indicate data exists */
2544510022a8SJaegeuk Kim 	FI_INLINE_DOTS,		/* indicate inline dot dentries */
2545d323d005SChao Yu 	FI_DO_DEFRAG,		/* indicate defragment is running */
2546c227f912SChao Yu 	FI_DIRTY_FILE,		/* indicate regular/symlink has dirty pages */
2547dc91de78SJaegeuk Kim 	FI_NO_PREALLOC,		/* indicate skipped preallocated blocks */
2548ef095d19SJaegeuk Kim 	FI_HOT_DATA,		/* indicate file is hot */
25497a2af766SChao Yu 	FI_EXTRA_ATTR,		/* indicate file has extra attribute */
25505c57132eSChao Yu 	FI_PROJ_INHERIT,	/* indicate file inherits projectid */
25511ad71a27SJaegeuk Kim 	FI_PIN_FILE,		/* indicate file should not be gced */
25522ef79ecbSChao Yu 	FI_ATOMIC_REVOKE_REQUEST, /* request to drop atomic data */
255395ae251fSEric Biggers 	FI_VERITY_IN_PROGRESS,	/* building fs-verity Merkle tree */
25544c8ff709SChao Yu 	FI_COMPRESSED_FILE,	/* indicate file's data can be compressed */
25554c8ff709SChao Yu 	FI_MMAP_FILE,		/* indicate file was mmapped */
255639a53e0cSJaegeuk Kim };
255739a53e0cSJaegeuk Kim 
2558205b9822SJaegeuk Kim static inline void __mark_inode_dirty_flag(struct inode *inode,
2559205b9822SJaegeuk Kim 						int flag, bool set)
256039a53e0cSJaegeuk Kim {
2561205b9822SJaegeuk Kim 	switch (flag) {
2562205b9822SJaegeuk Kim 	case FI_INLINE_XATTR:
2563205b9822SJaegeuk Kim 	case FI_INLINE_DATA:
2564205b9822SJaegeuk Kim 	case FI_INLINE_DENTRY:
25659ac1e2d8SDaeho Jeong 	case FI_NEW_INODE:
2566205b9822SJaegeuk Kim 		if (set)
2567205b9822SJaegeuk Kim 			return;
2568f5d5510eSJaegeuk Kim 		/* fall through */
2569205b9822SJaegeuk Kim 	case FI_DATA_EXIST:
2570205b9822SJaegeuk Kim 	case FI_INLINE_DOTS:
25711ad71a27SJaegeuk Kim 	case FI_PIN_FILE:
25724c8ff709SChao Yu 	case FI_COMPRESSED_FILE:
25737c45729aSJaegeuk Kim 		f2fs_mark_inode_dirty_sync(inode, true);
2574205b9822SJaegeuk Kim 	}
257539a53e0cSJaegeuk Kim }
257639a53e0cSJaegeuk Kim 
257791942321SJaegeuk Kim static inline void set_inode_flag(struct inode *inode, int flag)
257839a53e0cSJaegeuk Kim {
257991942321SJaegeuk Kim 	if (!test_bit(flag, &F2FS_I(inode)->flags))
258091942321SJaegeuk Kim 		set_bit(flag, &F2FS_I(inode)->flags);
2581205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, true);
258239a53e0cSJaegeuk Kim }
258339a53e0cSJaegeuk Kim 
258491942321SJaegeuk Kim static inline int is_inode_flag_set(struct inode *inode, int flag)
258539a53e0cSJaegeuk Kim {
258691942321SJaegeuk Kim 	return test_bit(flag, &F2FS_I(inode)->flags);
258739a53e0cSJaegeuk Kim }
258839a53e0cSJaegeuk Kim 
258991942321SJaegeuk Kim static inline void clear_inode_flag(struct inode *inode, int flag)
259039a53e0cSJaegeuk Kim {
259191942321SJaegeuk Kim 	if (test_bit(flag, &F2FS_I(inode)->flags))
259291942321SJaegeuk Kim 		clear_bit(flag, &F2FS_I(inode)->flags);
2593205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, false);
259439a53e0cSJaegeuk Kim }
259539a53e0cSJaegeuk Kim 
259695ae251fSEric Biggers static inline bool f2fs_verity_in_progress(struct inode *inode)
259795ae251fSEric Biggers {
259895ae251fSEric Biggers 	return IS_ENABLED(CONFIG_FS_VERITY) &&
259995ae251fSEric Biggers 	       is_inode_flag_set(inode, FI_VERITY_IN_PROGRESS);
260095ae251fSEric Biggers }
260195ae251fSEric Biggers 
260291942321SJaegeuk Kim static inline void set_acl_inode(struct inode *inode, umode_t mode)
2603444c580fSJaegeuk Kim {
260491942321SJaegeuk Kim 	F2FS_I(inode)->i_acl_mode = mode;
260591942321SJaegeuk Kim 	set_inode_flag(inode, FI_ACL_MODE);
26067c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, false);
260739a53e0cSJaegeuk Kim }
260839a53e0cSJaegeuk Kim 
2609a1961246SJaegeuk Kim static inline void f2fs_i_links_write(struct inode *inode, bool inc)
2610a1961246SJaegeuk Kim {
2611a1961246SJaegeuk Kim 	if (inc)
2612a1961246SJaegeuk Kim 		inc_nlink(inode);
2613a1961246SJaegeuk Kim 	else
2614a1961246SJaegeuk Kim 		drop_nlink(inode);
26157c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2616a1961246SJaegeuk Kim }
2617a1961246SJaegeuk Kim 
26188edd03c8SJaegeuk Kim static inline void f2fs_i_blocks_write(struct inode *inode,
26190abd675eSChao Yu 					block_t diff, bool add, bool claim)
26208edd03c8SJaegeuk Kim {
262126de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
262226de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
262326de9b11SJaegeuk Kim 
26240abd675eSChao Yu 	/* add = 1, claim = 1 should be dquot_reserve_block in pair */
26250abd675eSChao Yu 	if (add) {
26260abd675eSChao Yu 		if (claim)
26270abd675eSChao Yu 			dquot_claim_block(inode, diff);
26280abd675eSChao Yu 		else
26290abd675eSChao Yu 			dquot_alloc_block_nofail(inode, diff);
26300abd675eSChao Yu 	} else {
26310abd675eSChao Yu 		dquot_free_block(inode, diff);
26320abd675eSChao Yu 	}
26330abd675eSChao Yu 
26347c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
263526de9b11SJaegeuk Kim 	if (clean || recover)
263626de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
26378edd03c8SJaegeuk Kim }
26388edd03c8SJaegeuk Kim 
2639fc9581c8SJaegeuk Kim static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
2640fc9581c8SJaegeuk Kim {
264126de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
264226de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
264326de9b11SJaegeuk Kim 
2644fc9581c8SJaegeuk Kim 	if (i_size_read(inode) == i_size)
2645fc9581c8SJaegeuk Kim 		return;
2646fc9581c8SJaegeuk Kim 
2647fc9581c8SJaegeuk Kim 	i_size_write(inode, i_size);
26487c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
264926de9b11SJaegeuk Kim 	if (clean || recover)
265026de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
265126de9b11SJaegeuk Kim }
265226de9b11SJaegeuk Kim 
2653205b9822SJaegeuk Kim static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
2654205b9822SJaegeuk Kim {
2655205b9822SJaegeuk Kim 	F2FS_I(inode)->i_current_depth = depth;
26567c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2657205b9822SJaegeuk Kim }
2658205b9822SJaegeuk Kim 
26591ad71a27SJaegeuk Kim static inline void f2fs_i_gc_failures_write(struct inode *inode,
26601ad71a27SJaegeuk Kim 					unsigned int count)
26611ad71a27SJaegeuk Kim {
26622ef79ecbSChao Yu 	F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN] = count;
26631ad71a27SJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
26641ad71a27SJaegeuk Kim }
26651ad71a27SJaegeuk Kim 
2666205b9822SJaegeuk Kim static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
2667205b9822SJaegeuk Kim {
2668205b9822SJaegeuk Kim 	F2FS_I(inode)->i_xattr_nid = xnid;
26697c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2670205b9822SJaegeuk Kim }
2671205b9822SJaegeuk Kim 
2672205b9822SJaegeuk Kim static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
2673205b9822SJaegeuk Kim {
2674205b9822SJaegeuk Kim 	F2FS_I(inode)->i_pino = pino;
26757c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2676205b9822SJaegeuk Kim }
2677205b9822SJaegeuk Kim 
267891942321SJaegeuk Kim static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
2679444c580fSJaegeuk Kim {
2680205b9822SJaegeuk Kim 	struct f2fs_inode_info *fi = F2FS_I(inode);
2681205b9822SJaegeuk Kim 
2682444c580fSJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_XATTR)
2683205b9822SJaegeuk Kim 		set_bit(FI_INLINE_XATTR, &fi->flags);
26841001b347SHuajun Li 	if (ri->i_inline & F2FS_INLINE_DATA)
2685205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DATA, &fi->flags);
268634d67debSChao Yu 	if (ri->i_inline & F2FS_INLINE_DENTRY)
2687205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DENTRY, &fi->flags);
2688b3d208f9SJaegeuk Kim 	if (ri->i_inline & F2FS_DATA_EXIST)
2689205b9822SJaegeuk Kim 		set_bit(FI_DATA_EXIST, &fi->flags);
2690510022a8SJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_DOTS)
2691205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DOTS, &fi->flags);
26927a2af766SChao Yu 	if (ri->i_inline & F2FS_EXTRA_ATTR)
26937a2af766SChao Yu 		set_bit(FI_EXTRA_ATTR, &fi->flags);
26941ad71a27SJaegeuk Kim 	if (ri->i_inline & F2FS_PIN_FILE)
26951ad71a27SJaegeuk Kim 		set_bit(FI_PIN_FILE, &fi->flags);
2696444c580fSJaegeuk Kim }
2697444c580fSJaegeuk Kim 
269891942321SJaegeuk Kim static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
2699444c580fSJaegeuk Kim {
2700444c580fSJaegeuk Kim 	ri->i_inline = 0;
2701444c580fSJaegeuk Kim 
270291942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_XATTR))
2703444c580fSJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_XATTR;
270491942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DATA))
27051001b347SHuajun Li 		ri->i_inline |= F2FS_INLINE_DATA;
270691942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
270734d67debSChao Yu 		ri->i_inline |= F2FS_INLINE_DENTRY;
270891942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_DATA_EXIST))
2709b3d208f9SJaegeuk Kim 		ri->i_inline |= F2FS_DATA_EXIST;
271091942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DOTS))
2711510022a8SJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_DOTS;
27127a2af766SChao Yu 	if (is_inode_flag_set(inode, FI_EXTRA_ATTR))
27137a2af766SChao Yu 		ri->i_inline |= F2FS_EXTRA_ATTR;
27141ad71a27SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_PIN_FILE))
27151ad71a27SJaegeuk Kim 		ri->i_inline |= F2FS_PIN_FILE;
27167a2af766SChao Yu }
27177a2af766SChao Yu 
27187a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode)
27197a2af766SChao Yu {
27207a2af766SChao Yu 	return is_inode_flag_set(inode, FI_EXTRA_ATTR);
2721444c580fSJaegeuk Kim }
2722444c580fSJaegeuk Kim 
2723987c7c31SChao Yu static inline int f2fs_has_inline_xattr(struct inode *inode)
2724987c7c31SChao Yu {
272591942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_XATTR);
2726987c7c31SChao Yu }
2727987c7c31SChao Yu 
27284c8ff709SChao Yu static inline int f2fs_compressed_file(struct inode *inode)
27294c8ff709SChao Yu {
27304c8ff709SChao Yu 	return S_ISREG(inode->i_mode) &&
27314c8ff709SChao Yu 		is_inode_flag_set(inode, FI_COMPRESSED_FILE);
27324c8ff709SChao Yu }
27334c8ff709SChao Yu 
273481ca7350SChao Yu static inline unsigned int addrs_per_inode(struct inode *inode)
2735de93653fSJaegeuk Kim {
2736d02a6e61SChao Yu 	unsigned int addrs = CUR_ADDRS_PER_INODE(inode) -
2737d02a6e61SChao Yu 				get_inline_xattr_addrs(inode);
27384c8ff709SChao Yu 
27394c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
27404c8ff709SChao Yu 		return addrs;
27414c8ff709SChao Yu 	return ALIGN_DOWN(addrs, F2FS_I(inode)->i_cluster_size);
2742d02a6e61SChao Yu }
2743d02a6e61SChao Yu 
2744d02a6e61SChao Yu static inline unsigned int addrs_per_block(struct inode *inode)
2745d02a6e61SChao Yu {
27464c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
27474c8ff709SChao Yu 		return DEF_ADDRS_PER_BLOCK;
27484c8ff709SChao Yu 	return ALIGN_DOWN(DEF_ADDRS_PER_BLOCK, F2FS_I(inode)->i_cluster_size);
2749de93653fSJaegeuk Kim }
2750de93653fSJaegeuk Kim 
27516afc662eSChao Yu static inline void *inline_xattr_addr(struct inode *inode, struct page *page)
275265985d93SJaegeuk Kim {
2753695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
2754cac5a3d8SDongOh Shin 
275565985d93SJaegeuk Kim 	return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
2756b323fd28SChao Yu 					get_inline_xattr_addrs(inode)]);
275765985d93SJaegeuk Kim }
275865985d93SJaegeuk Kim 
275965985d93SJaegeuk Kim static inline int inline_xattr_size(struct inode *inode)
276065985d93SJaegeuk Kim {
2761622927f3SChao Yu 	if (f2fs_has_inline_xattr(inode))
27626afc662eSChao Yu 		return get_inline_xattr_addrs(inode) * sizeof(__le32);
2763622927f3SChao Yu 	return 0;
276465985d93SJaegeuk Kim }
276565985d93SJaegeuk Kim 
27660dbdc2aeSJaegeuk Kim static inline int f2fs_has_inline_data(struct inode *inode)
27670dbdc2aeSJaegeuk Kim {
276891942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DATA);
27690dbdc2aeSJaegeuk Kim }
27700dbdc2aeSJaegeuk Kim 
2771b3d208f9SJaegeuk Kim static inline int f2fs_exist_data(struct inode *inode)
2772b3d208f9SJaegeuk Kim {
277391942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DATA_EXIST);
2774b3d208f9SJaegeuk Kim }
2775b3d208f9SJaegeuk Kim 
2776510022a8SJaegeuk Kim static inline int f2fs_has_inline_dots(struct inode *inode)
2777510022a8SJaegeuk Kim {
277891942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DOTS);
2779510022a8SJaegeuk Kim }
2780510022a8SJaegeuk Kim 
27814c8ff709SChao Yu static inline int f2fs_is_mmap_file(struct inode *inode)
27824c8ff709SChao Yu {
27834c8ff709SChao Yu 	return is_inode_flag_set(inode, FI_MMAP_FILE);
27844c8ff709SChao Yu }
27854c8ff709SChao Yu 
27861ad71a27SJaegeuk Kim static inline bool f2fs_is_pinned_file(struct inode *inode)
27871ad71a27SJaegeuk Kim {
27881ad71a27SJaegeuk Kim 	return is_inode_flag_set(inode, FI_PIN_FILE);
27891ad71a27SJaegeuk Kim }
27901ad71a27SJaegeuk Kim 
279188b88a66SJaegeuk Kim static inline bool f2fs_is_atomic_file(struct inode *inode)
279288b88a66SJaegeuk Kim {
279391942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_ATOMIC_FILE);
279488b88a66SJaegeuk Kim }
279588b88a66SJaegeuk Kim 
27965fe45743SChao Yu static inline bool f2fs_is_commit_atomic_write(struct inode *inode)
27975fe45743SChao Yu {
27985fe45743SChao Yu 	return is_inode_flag_set(inode, FI_ATOMIC_COMMIT);
27995fe45743SChao Yu }
28005fe45743SChao Yu 
280102a1335fSJaegeuk Kim static inline bool f2fs_is_volatile_file(struct inode *inode)
280202a1335fSJaegeuk Kim {
280391942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_VOLATILE_FILE);
280402a1335fSJaegeuk Kim }
280502a1335fSJaegeuk Kim 
28063c6c2bebSJaegeuk Kim static inline bool f2fs_is_first_block_written(struct inode *inode)
28073c6c2bebSJaegeuk Kim {
280891942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
28093c6c2bebSJaegeuk Kim }
28103c6c2bebSJaegeuk Kim 
28111e84371fSJaegeuk Kim static inline bool f2fs_is_drop_cache(struct inode *inode)
28121e84371fSJaegeuk Kim {
281391942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DROP_CACHE);
28141e84371fSJaegeuk Kim }
28151e84371fSJaegeuk Kim 
2816f2470371SChao Yu static inline void *inline_data_addr(struct inode *inode, struct page *page)
28171001b347SHuajun Li {
2818695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
28197a2af766SChao Yu 	int extra_size = get_extra_isize(inode);
2820cac5a3d8SDongOh Shin 
28217a2af766SChao Yu 	return (void *)&(ri->i_addr[extra_size + DEF_INLINE_RESERVED_SIZE]);
28221001b347SHuajun Li }
28231001b347SHuajun Li 
282434d67debSChao Yu static inline int f2fs_has_inline_dentry(struct inode *inode)
282534d67debSChao Yu {
282691942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DENTRY);
282734d67debSChao Yu }
282834d67debSChao Yu 
2829b5492af7SJaegeuk Kim static inline int is_file(struct inode *inode, int type)
2830b5492af7SJaegeuk Kim {
2831b5492af7SJaegeuk Kim 	return F2FS_I(inode)->i_advise & type;
2832b5492af7SJaegeuk Kim }
2833b5492af7SJaegeuk Kim 
2834b5492af7SJaegeuk Kim static inline void set_file(struct inode *inode, int type)
2835b5492af7SJaegeuk Kim {
2836b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise |= type;
28377c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2838b5492af7SJaegeuk Kim }
2839b5492af7SJaegeuk Kim 
2840b5492af7SJaegeuk Kim static inline void clear_file(struct inode *inode, int type)
2841b5492af7SJaegeuk Kim {
2842b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise &= ~type;
28437c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2844b5492af7SJaegeuk Kim }
2845b5492af7SJaegeuk Kim 
2846fe1897eaSChao Yu static inline bool f2fs_is_time_consistent(struct inode *inode)
2847fe1897eaSChao Yu {
2848fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time, &inode->i_atime))
2849fe1897eaSChao Yu 		return false;
2850fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 1, &inode->i_ctime))
2851fe1897eaSChao Yu 		return false;
2852fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 2, &inode->i_mtime))
2853fe1897eaSChao Yu 		return false;
2854fe1897eaSChao Yu 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 3,
2855fe1897eaSChao Yu 						&F2FS_I(inode)->i_crtime))
2856fe1897eaSChao Yu 		return false;
2857fe1897eaSChao Yu 	return true;
2858fe1897eaSChao Yu }
2859fe1897eaSChao Yu 
286026787236SJaegeuk Kim static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
286126787236SJaegeuk Kim {
2862a0d00fadSChao Yu 	bool ret;
2863a0d00fadSChao Yu 
286426787236SJaegeuk Kim 	if (dsync) {
286526787236SJaegeuk Kim 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
286626787236SJaegeuk Kim 
286726787236SJaegeuk Kim 		spin_lock(&sbi->inode_lock[DIRTY_META]);
286826787236SJaegeuk Kim 		ret = list_empty(&F2FS_I(inode)->gdirty_list);
286926787236SJaegeuk Kim 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
287026787236SJaegeuk Kim 		return ret;
287126787236SJaegeuk Kim 	}
287226787236SJaegeuk Kim 	if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
287326787236SJaegeuk Kim 			file_keep_isize(inode) ||
2874235831d7SJunling Zheng 			i_size_read(inode) & ~PAGE_MASK)
287526787236SJaegeuk Kim 		return false;
2876a0d00fadSChao Yu 
2877fe1897eaSChao Yu 	if (!f2fs_is_time_consistent(inode))
2878214c2461SJaegeuk Kim 		return false;
2879214c2461SJaegeuk Kim 
2880c10c9820SChao Yu 	spin_lock(&F2FS_I(inode)->i_size_lock);
2881a0d00fadSChao Yu 	ret = F2FS_I(inode)->last_disk_size == i_size_read(inode);
2882c10c9820SChao Yu 	spin_unlock(&F2FS_I(inode)->i_size_lock);
2883a0d00fadSChao Yu 
2884a0d00fadSChao Yu 	return ret;
2885267378d4SChao Yu }
2886267378d4SChao Yu 
2887deeedd71SChao Yu static inline bool f2fs_readonly(struct super_block *sb)
288877888c1eSJaegeuk Kim {
2889deeedd71SChao Yu 	return sb_rdonly(sb);
289077888c1eSJaegeuk Kim }
289177888c1eSJaegeuk Kim 
2892744602cfSJaegeuk Kim static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
2893744602cfSJaegeuk Kim {
2894aaec2b1dSChao Yu 	return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
2895744602cfSJaegeuk Kim }
2896744602cfSJaegeuk Kim 
2897eaa693f4SJaegeuk Kim static inline bool is_dot_dotdot(const struct qstr *str)
2898eaa693f4SJaegeuk Kim {
2899eaa693f4SJaegeuk Kim 	if (str->len == 1 && str->name[0] == '.')
2900eaa693f4SJaegeuk Kim 		return true;
2901eaa693f4SJaegeuk Kim 
2902eaa693f4SJaegeuk Kim 	if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
2903eaa693f4SJaegeuk Kim 		return true;
2904eaa693f4SJaegeuk Kim 
2905eaa693f4SJaegeuk Kim 	return false;
2906eaa693f4SJaegeuk Kim }
2907eaa693f4SJaegeuk Kim 
29083e72f721SJaegeuk Kim static inline bool f2fs_may_extent_tree(struct inode *inode)
29093e72f721SJaegeuk Kim {
2910e4589fa5SSahitya Tummala 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2911e4589fa5SSahitya Tummala 
2912e4589fa5SSahitya Tummala 	if (!test_opt(sbi, EXTENT_CACHE) ||
29134c8ff709SChao Yu 			is_inode_flag_set(inode, FI_NO_EXTENT) ||
29144c8ff709SChao Yu 			is_inode_flag_set(inode, FI_COMPRESSED_FILE))
29153e72f721SJaegeuk Kim 		return false;
29163e72f721SJaegeuk Kim 
2917e4589fa5SSahitya Tummala 	/*
2918e4589fa5SSahitya Tummala 	 * for recovered files during mount do not create extents
2919e4589fa5SSahitya Tummala 	 * if shrinker is not registered.
2920e4589fa5SSahitya Tummala 	 */
2921e4589fa5SSahitya Tummala 	if (list_empty(&sbi->s_list))
2922e4589fa5SSahitya Tummala 		return false;
2923e4589fa5SSahitya Tummala 
2924886f56f9SAl Viro 	return S_ISREG(inode->i_mode);
29253e72f721SJaegeuk Kim }
29263e72f721SJaegeuk Kim 
29271ecc0c5cSChao Yu static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
29281ecc0c5cSChao Yu 					size_t size, gfp_t flags)
29290414b004SJaegeuk Kim {
29305222595dSJaegeuk Kim 	void *ret;
29315222595dSJaegeuk Kim 
293255523519SChao Yu 	if (time_to_inject(sbi, FAULT_KMALLOC)) {
2933c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_KMALLOC);
29342c63feadSJaegeuk Kim 		return NULL;
293555523519SChao Yu 	}
29367fa750a1SArnd Bergmann 
29375222595dSJaegeuk Kim 	ret = kmalloc(size, flags);
29385222595dSJaegeuk Kim 	if (ret)
29395222595dSJaegeuk Kim 		return ret;
29405222595dSJaegeuk Kim 
29415222595dSJaegeuk Kim 	return kvmalloc(size, flags);
29420414b004SJaegeuk Kim }
29430414b004SJaegeuk Kim 
2944acbf054dSChao Yu static inline void *f2fs_kzalloc(struct f2fs_sb_info *sbi,
2945acbf054dSChao Yu 					size_t size, gfp_t flags)
2946acbf054dSChao Yu {
2947acbf054dSChao Yu 	return f2fs_kmalloc(sbi, size, flags | __GFP_ZERO);
2948acbf054dSChao Yu }
2949acbf054dSChao Yu 
2950628b3d14SChao Yu static inline void *f2fs_kvmalloc(struct f2fs_sb_info *sbi,
2951628b3d14SChao Yu 					size_t size, gfp_t flags)
2952628b3d14SChao Yu {
2953628b3d14SChao Yu 	if (time_to_inject(sbi, FAULT_KVMALLOC)) {
2954c45d6002SChao Yu 		f2fs_show_injection_info(sbi, FAULT_KVMALLOC);
2955628b3d14SChao Yu 		return NULL;
2956628b3d14SChao Yu 	}
29577fa750a1SArnd Bergmann 
2958628b3d14SChao Yu 	return kvmalloc(size, flags);
2959628b3d14SChao Yu }
2960628b3d14SChao Yu 
2961628b3d14SChao Yu static inline void *f2fs_kvzalloc(struct f2fs_sb_info *sbi,
2962628b3d14SChao Yu 					size_t size, gfp_t flags)
2963628b3d14SChao Yu {
2964628b3d14SChao Yu 	return f2fs_kvmalloc(sbi, size, flags | __GFP_ZERO);
2965628b3d14SChao Yu }
2966628b3d14SChao Yu 
29677a2af766SChao Yu static inline int get_extra_isize(struct inode *inode)
2968f2470371SChao Yu {
29697a2af766SChao Yu 	return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
2970f2470371SChao Yu }
2971f2470371SChao Yu 
29726afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode)
29736afc662eSChao Yu {
29746afc662eSChao Yu 	return F2FS_I(inode)->i_inline_xattr_size;
29756afc662eSChao Yu }
29766afc662eSChao Yu 
29774d57b86dSChao Yu #define f2fs_get_inode_mode(i) \
297891942321SJaegeuk Kim 	((is_inode_flag_set(i, FI_ACL_MODE)) ? \
2979a6dda0e6SChristoph Hellwig 	 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
2980a6dda0e6SChristoph Hellwig 
29817a2af766SChao Yu #define F2FS_TOTAL_EXTRA_ATTR_SIZE			\
29827a2af766SChao Yu 	(offsetof(struct f2fs_inode, i_extra_end) -	\
29837a2af766SChao Yu 	offsetof(struct f2fs_inode, i_extra_isize))	\
29847a2af766SChao Yu 
29855c57132eSChao Yu #define F2FS_OLD_ATTRIBUTE_SIZE	(offsetof(struct f2fs_inode, i_addr))
29865c57132eSChao Yu #define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field)		\
29872f84babfSSheng Yong 		((offsetof(typeof(*(f2fs_inode)), field) +	\
29885c57132eSChao Yu 		sizeof((f2fs_inode)->field))			\
29892f84babfSSheng Yong 		<= (F2FS_OLD_ATTRIBUTE_SIZE + (extra_isize)))	\
29905c57132eSChao Yu 
2991b0af6d49SChao Yu static inline void f2fs_reset_iostat(struct f2fs_sb_info *sbi)
2992b0af6d49SChao Yu {
2993b0af6d49SChao Yu 	int i;
2994b0af6d49SChao Yu 
2995b0af6d49SChao Yu 	spin_lock(&sbi->iostat_lock);
2996b0af6d49SChao Yu 	for (i = 0; i < NR_IO_TYPE; i++)
2997b0af6d49SChao Yu 		sbi->write_iostat[i] = 0;
2998b0af6d49SChao Yu 	spin_unlock(&sbi->iostat_lock);
2999b0af6d49SChao Yu }
3000b0af6d49SChao Yu 
3001b0af6d49SChao Yu static inline void f2fs_update_iostat(struct f2fs_sb_info *sbi,
3002b0af6d49SChao Yu 			enum iostat_type type, unsigned long long io_bytes)
3003b0af6d49SChao Yu {
3004b0af6d49SChao Yu 	if (!sbi->iostat_enable)
3005b0af6d49SChao Yu 		return;
3006b0af6d49SChao Yu 	spin_lock(&sbi->iostat_lock);
3007b0af6d49SChao Yu 	sbi->write_iostat[type] += io_bytes;
3008b0af6d49SChao Yu 
3009b0af6d49SChao Yu 	if (type == APP_WRITE_IO || type == APP_DIRECT_IO)
3010b0af6d49SChao Yu 		sbi->write_iostat[APP_BUFFERED_IO] =
3011b0af6d49SChao Yu 			sbi->write_iostat[APP_WRITE_IO] -
3012b0af6d49SChao Yu 			sbi->write_iostat[APP_DIRECT_IO];
3013b0af6d49SChao Yu 	spin_unlock(&sbi->iostat_lock);
3014b0af6d49SChao Yu }
3015b0af6d49SChao Yu 
30162c70c5e3SChao Yu #define __is_large_section(sbi)		((sbi)->segs_per_sec > 1)
30172c70c5e3SChao Yu 
30186dc3a126SChao Yu #define __is_meta_io(fio) (PAGE_TYPE_OF_BIO((fio)->type) == META)
3019c9b60788SChao Yu 
3020e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
3021e1da7872SChao Yu 					block_t blkaddr, int type);
3022e1da7872SChao Yu static inline void verify_blkaddr(struct f2fs_sb_info *sbi,
3023e1da7872SChao Yu 					block_t blkaddr, int type)
3024e1da7872SChao Yu {
3025e1da7872SChao Yu 	if (!f2fs_is_valid_blkaddr(sbi, blkaddr, type)) {
3026dcbb4c10SJoe Perches 		f2fs_err(sbi, "invalid blkaddr: %u, type: %d, run fsck to fix.",
3027e1da7872SChao Yu 			 blkaddr, type);
3028e1da7872SChao Yu 		f2fs_bug_on(sbi, 1);
3029e1da7872SChao Yu 	}
3030e1da7872SChao Yu }
3031e1da7872SChao Yu 
3032e1da7872SChao Yu static inline bool __is_valid_data_blkaddr(block_t blkaddr)
30337b525dd0SChao Yu {
30344c8ff709SChao Yu 	if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR ||
30354c8ff709SChao Yu 			blkaddr == COMPRESS_ADDR)
30367b525dd0SChao Yu 		return false;
30377b525dd0SChao Yu 	return true;
30387b525dd0SChao Yu }
30397b525dd0SChao Yu 
3040240a5915SChao Yu static inline void f2fs_set_page_private(struct page *page,
3041240a5915SChao Yu 						unsigned long data)
3042240a5915SChao Yu {
3043240a5915SChao Yu 	if (PagePrivate(page))
3044240a5915SChao Yu 		return;
3045240a5915SChao Yu 
3046240a5915SChao Yu 	get_page(page);
3047240a5915SChao Yu 	SetPagePrivate(page);
3048240a5915SChao Yu 	set_page_private(page, data);
3049240a5915SChao Yu }
3050240a5915SChao Yu 
3051240a5915SChao Yu static inline void f2fs_clear_page_private(struct page *page)
3052240a5915SChao Yu {
3053240a5915SChao Yu 	if (!PagePrivate(page))
3054240a5915SChao Yu 		return;
3055240a5915SChao Yu 
3056240a5915SChao Yu 	set_page_private(page, 0);
3057240a5915SChao Yu 	ClearPagePrivate(page);
3058240a5915SChao Yu 	f2fs_put_page(page, 0);
3059240a5915SChao Yu }
3060240a5915SChao Yu 
306139a53e0cSJaegeuk Kim /*
306239a53e0cSJaegeuk Kim  * file.c
306339a53e0cSJaegeuk Kim  */
3064cac5a3d8SDongOh Shin int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
30654d57b86dSChao Yu void f2fs_truncate_data_blocks(struct dnode_of_data *dn);
3066c42d28ceSChao Yu int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock);
3067cac5a3d8SDongOh Shin int f2fs_truncate(struct inode *inode);
3068a528d35eSDavid Howells int f2fs_getattr(const struct path *path, struct kstat *stat,
3069a528d35eSDavid Howells 			u32 request_mask, unsigned int flags);
3070cac5a3d8SDongOh Shin int f2fs_setattr(struct dentry *dentry, struct iattr *attr);
30714d57b86dSChao Yu int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
30724d57b86dSChao Yu void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count);
3073c4020b2dSChao Yu int f2fs_precache_extents(struct inode *inode);
3074cac5a3d8SDongOh Shin long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
3075cac5a3d8SDongOh Shin long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
307678130819SChao Yu int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid);
30771ad71a27SJaegeuk Kim int f2fs_pin_file_control(struct inode *inode, bool inc);
307839a53e0cSJaegeuk Kim 
307939a53e0cSJaegeuk Kim /*
308039a53e0cSJaegeuk Kim  * inode.c
308139a53e0cSJaegeuk Kim  */
3082cac5a3d8SDongOh Shin void f2fs_set_inode_flags(struct inode *inode);
3083704956ecSChao Yu bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page);
3084704956ecSChao Yu void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page);
3085cac5a3d8SDongOh Shin struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
3086cac5a3d8SDongOh Shin struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
30874d57b86dSChao Yu int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
30884d57b86dSChao Yu void f2fs_update_inode(struct inode *inode, struct page *node_page);
30894d57b86dSChao Yu void f2fs_update_inode_page(struct inode *inode);
3090cac5a3d8SDongOh Shin int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
3091cac5a3d8SDongOh Shin void f2fs_evict_inode(struct inode *inode);
30924d57b86dSChao Yu void f2fs_handle_failed_inode(struct inode *inode);
309339a53e0cSJaegeuk Kim 
309439a53e0cSJaegeuk Kim /*
309539a53e0cSJaegeuk Kim  * namei.c
309639a53e0cSJaegeuk Kim  */
30974d57b86dSChao Yu int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
3098b6a06cbbSChao Yu 							bool hot, bool set);
309939a53e0cSJaegeuk Kim struct dentry *f2fs_get_parent(struct dentry *child);
310039a53e0cSJaegeuk Kim 
31012c2eb7a3SDaniel Rosenberg extern int f2fs_ci_compare(const struct inode *parent,
31022c2eb7a3SDaniel Rosenberg 			   const struct qstr *name,
3103950d47f2SChao Yu 			   const struct qstr *entry,
3104950d47f2SChao Yu 			   bool quick);
31052c2eb7a3SDaniel Rosenberg 
310639a53e0cSJaegeuk Kim /*
310739a53e0cSJaegeuk Kim  * dir.c
310839a53e0cSJaegeuk Kim  */
31094d57b86dSChao Yu unsigned char f2fs_get_de_type(struct f2fs_dir_entry *de);
31104d57b86dSChao Yu struct f2fs_dir_entry *f2fs_find_target_dentry(struct fscrypt_name *fname,
3111cac5a3d8SDongOh Shin 			f2fs_hash_t namehash, int *max_slots,
3112cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
3113cac5a3d8SDongOh Shin int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
3114cac5a3d8SDongOh Shin 			unsigned int start_pos, struct fscrypt_str *fstr);
31154d57b86dSChao Yu void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
3116cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
31174d57b86dSChao Yu struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
3118cac5a3d8SDongOh Shin 			const struct qstr *new_name,
3119cac5a3d8SDongOh Shin 			const struct qstr *orig_name, struct page *dpage);
31204d57b86dSChao Yu void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
3121cac5a3d8SDongOh Shin 			unsigned int current_depth);
31224d57b86dSChao Yu int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots);
3123cac5a3d8SDongOh Shin void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
3124cac5a3d8SDongOh Shin struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
3125cac5a3d8SDongOh Shin 			struct fscrypt_name *fname, struct page **res_page);
3126cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
3127cac5a3d8SDongOh Shin 			const struct qstr *child, struct page **res_page);
3128cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
3129cac5a3d8SDongOh Shin ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
3130cac5a3d8SDongOh Shin 			struct page **page);
3131cac5a3d8SDongOh Shin void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
3132cac5a3d8SDongOh Shin 			struct page *page, struct inode *inode);
3133b06af2afSJaegeuk Kim bool f2fs_has_enough_room(struct inode *dir, struct page *ipage,
3134b06af2afSJaegeuk Kim 			struct fscrypt_name *fname);
3135cac5a3d8SDongOh Shin void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
3136cac5a3d8SDongOh Shin 			const struct qstr *name, f2fs_hash_t name_hash,
3137cac5a3d8SDongOh Shin 			unsigned int bit_pos);
3138cac5a3d8SDongOh Shin int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
3139cac5a3d8SDongOh Shin 			const struct qstr *orig_name,
3140cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
31414d57b86dSChao Yu int f2fs_add_dentry(struct inode *dir, struct fscrypt_name *fname,
3142cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
31434d57b86dSChao Yu int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
3144cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
3145cac5a3d8SDongOh Shin void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
3146cac5a3d8SDongOh Shin 			struct inode *dir, struct inode *inode);
3147cac5a3d8SDongOh Shin int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
3148cac5a3d8SDongOh Shin bool f2fs_empty_dir(struct inode *dir);
314939a53e0cSJaegeuk Kim 
3150b7f7a5e0SAl Viro static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
3151b7f7a5e0SAl Viro {
31524d57b86dSChao Yu 	return f2fs_do_add_link(d_inode(dentry->d_parent), &dentry->d_name,
3153510022a8SJaegeuk Kim 				inode, inode->i_ino, inode->i_mode);
3154b7f7a5e0SAl Viro }
3155b7f7a5e0SAl Viro 
315639a53e0cSJaegeuk Kim /*
315739a53e0cSJaegeuk Kim  * super.c
315839a53e0cSJaegeuk Kim  */
3159cac5a3d8SDongOh Shin int f2fs_inode_dirtied(struct inode *inode, bool sync);
3160cac5a3d8SDongOh Shin void f2fs_inode_synced(struct inode *inode);
3161ea676733SJaegeuk Kim int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly);
3162af033b2aSChao Yu int f2fs_quota_sync(struct super_block *sb, int type);
31634b2414d0SChao Yu void f2fs_quota_off_umount(struct super_block *sb);
3164cac5a3d8SDongOh Shin int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
3165cac5a3d8SDongOh Shin int f2fs_sync_fs(struct super_block *sb, int sync);
31664d57b86dSChao Yu int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi);
316739a53e0cSJaegeuk Kim 
316839a53e0cSJaegeuk Kim /*
316939a53e0cSJaegeuk Kim  * hash.c
317039a53e0cSJaegeuk Kim  */
31712c2eb7a3SDaniel Rosenberg f2fs_hash_t f2fs_dentry_hash(const struct inode *dir,
31722c2eb7a3SDaniel Rosenberg 		const struct qstr *name_info, struct fscrypt_name *fname);
317339a53e0cSJaegeuk Kim 
317439a53e0cSJaegeuk Kim /*
317539a53e0cSJaegeuk Kim  * node.c
317639a53e0cSJaegeuk Kim  */
317739a53e0cSJaegeuk Kim struct dnode_of_data;
317839a53e0cSJaegeuk Kim struct node_info;
317939a53e0cSJaegeuk Kim 
31804d57b86dSChao Yu int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid);
31814d57b86dSChao Yu bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type);
318250fa53ecSChao Yu bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct page *page);
318350fa53ecSChao Yu void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi);
318450fa53ecSChao Yu void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct page *page);
318550fa53ecSChao Yu void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi);
31864d57b86dSChao Yu int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
31874d57b86dSChao Yu bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
31884d57b86dSChao Yu bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
31897735730dSChao Yu int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
31904d57b86dSChao Yu 						struct node_info *ni);
31914d57b86dSChao Yu pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
31924d57b86dSChao Yu int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
31934d57b86dSChao Yu int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from);
31944d57b86dSChao Yu int f2fs_truncate_xattr_node(struct inode *inode);
319550fa53ecSChao Yu int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
319650fa53ecSChao Yu 					unsigned int seq_id);
31974d57b86dSChao Yu int f2fs_remove_inode_page(struct inode *inode);
31984d57b86dSChao Yu struct page *f2fs_new_inode_page(struct inode *inode);
31994d57b86dSChao Yu struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs);
32004d57b86dSChao Yu void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
32014d57b86dSChao Yu struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
32024d57b86dSChao Yu struct page *f2fs_get_node_page_ra(struct page *parent, int start);
320348018b4cSChao Yu int f2fs_move_node_page(struct page *node_page, int gc_type);
32044d57b86dSChao Yu int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
320550fa53ecSChao Yu 			struct writeback_control *wbc, bool atomic,
320650fa53ecSChao Yu 			unsigned int *seq_id);
32074d57b86dSChao Yu int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
32084d57b86dSChao Yu 			struct writeback_control *wbc,
3209b0af6d49SChao Yu 			bool do_balance, enum iostat_type io_type);
3210e2374015SChao Yu int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
32114d57b86dSChao Yu bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
32124d57b86dSChao Yu void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
32134d57b86dSChao Yu void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
32144d57b86dSChao Yu int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
32154d57b86dSChao Yu void f2fs_recover_inline_xattr(struct inode *inode, struct page *page);
32164d57b86dSChao Yu int f2fs_recover_xattr_data(struct inode *inode, struct page *page);
32174d57b86dSChao Yu int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
32187735730dSChao Yu int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
3219cac5a3d8SDongOh Shin 			unsigned int segno, struct f2fs_summary_block *sum);
3220edc55aafSJaegeuk Kim int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
32214d57b86dSChao Yu int f2fs_build_node_manager(struct f2fs_sb_info *sbi);
32224d57b86dSChao Yu void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi);
32234d57b86dSChao Yu int __init f2fs_create_node_manager_caches(void);
32244d57b86dSChao Yu void f2fs_destroy_node_manager_caches(void);
322539a53e0cSJaegeuk Kim 
322639a53e0cSJaegeuk Kim /*
322739a53e0cSJaegeuk Kim  * segment.c
322839a53e0cSJaegeuk Kim  */
32294d57b86dSChao Yu bool f2fs_need_SSR(struct f2fs_sb_info *sbi);
32304d57b86dSChao Yu void f2fs_register_inmem_page(struct inode *inode, struct page *page);
32314d57b86dSChao Yu void f2fs_drop_inmem_pages_all(struct f2fs_sb_info *sbi, bool gc_failure);
32324d57b86dSChao Yu void f2fs_drop_inmem_pages(struct inode *inode);
32334d57b86dSChao Yu void f2fs_drop_inmem_page(struct inode *inode, struct page *page);
32344d57b86dSChao Yu int f2fs_commit_inmem_pages(struct inode *inode);
3235cac5a3d8SDongOh Shin void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
3236cac5a3d8SDongOh Shin void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi);
323739d787beSChao Yu int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
32384d57b86dSChao Yu int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
32391228b482SChao Yu int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
32404d57b86dSChao Yu void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
32414d57b86dSChao Yu void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
32424d57b86dSChao Yu bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
32434d57b86dSChao Yu void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
32444d57b86dSChao Yu void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi);
324503f2c02dSJaegeuk Kim bool f2fs_issue_discard_timeout(struct f2fs_sb_info *sbi);
32464d57b86dSChao Yu void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
32474d57b86dSChao Yu 					struct cp_control *cpc);
32484354994fSDaniel Rosenberg void f2fs_dirty_to_prefree(struct f2fs_sb_info *sbi);
32494d3aed70SDaniel Rosenberg block_t f2fs_get_unusable_blocks(struct f2fs_sb_info *sbi);
32504d3aed70SDaniel Rosenberg int f2fs_disable_cp_again(struct f2fs_sb_info *sbi, block_t unusable);
32514d57b86dSChao Yu void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi);
32524d57b86dSChao Yu int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
325304f0b2eaSQiuyang Sun void allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type,
325404f0b2eaSQiuyang Sun 					unsigned int start, unsigned int end);
3255f5a53edcSJaegeuk Kim void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi, int type);
3256cac5a3d8SDongOh Shin int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
32574d57b86dSChao Yu bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
32584d57b86dSChao Yu 					struct cp_control *cpc);
32594d57b86dSChao Yu struct page *f2fs_get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
32604d57b86dSChao Yu void f2fs_update_meta_page(struct f2fs_sb_info *sbi, void *src,
32614d57b86dSChao Yu 					block_t blk_addr);
32624d57b86dSChao Yu void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
3263b0af6d49SChao Yu 						enum iostat_type io_type);
32644d57b86dSChao Yu void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio);
32654d57b86dSChao Yu void f2fs_outplace_write_data(struct dnode_of_data *dn,
32664d57b86dSChao Yu 			struct f2fs_io_info *fio);
32674d57b86dSChao Yu int f2fs_inplace_write_data(struct f2fs_io_info *fio);
32684d57b86dSChao Yu void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
3269cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t new_blkaddr,
3270cac5a3d8SDongOh Shin 			bool recover_curseg, bool recover_newaddr);
3271cac5a3d8SDongOh Shin void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
3272cac5a3d8SDongOh Shin 			block_t old_addr, block_t new_addr,
3273cac5a3d8SDongOh Shin 			unsigned char version, bool recover_curseg,
3274cac5a3d8SDongOh Shin 			bool recover_newaddr);
32754d57b86dSChao Yu void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
3276cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t *new_blkaddr,
3277fb830fc5SChao Yu 			struct f2fs_summary *sum, int type,
3278fb830fc5SChao Yu 			struct f2fs_io_info *fio, bool add_list);
3279cac5a3d8SDongOh Shin void f2fs_wait_on_page_writeback(struct page *page,
3280bae0ee7aSChao Yu 			enum page_type type, bool ordered, bool locked);
32810ded69f6SJaegeuk Kim void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr);
32821e78e8bdSSahitya Tummala void f2fs_wait_on_block_writeback_range(struct inode *inode, block_t blkaddr,
32831e78e8bdSSahitya Tummala 								block_t len);
32844d57b86dSChao Yu void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
32854d57b86dSChao Yu void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
32864d57b86dSChao Yu int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
3287cac5a3d8SDongOh Shin 			unsigned int val, int alloc);
32884d57b86dSChao Yu void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
3289c426d991SShin'ichiro Kawasaki int f2fs_fix_curseg_write_pointer(struct f2fs_sb_info *sbi);
3290d508c94eSShin'ichiro Kawasaki int f2fs_check_write_pointer(struct f2fs_sb_info *sbi);
32914d57b86dSChao Yu int f2fs_build_segment_manager(struct f2fs_sb_info *sbi);
32924d57b86dSChao Yu void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi);
32934d57b86dSChao Yu int __init f2fs_create_segment_manager_caches(void);
32944d57b86dSChao Yu void f2fs_destroy_segment_manager_caches(void);
32954d57b86dSChao Yu int f2fs_rw_hint_to_seg_type(enum rw_hint hint);
32964d57b86dSChao Yu enum rw_hint f2fs_io_type_to_rw_hint(struct f2fs_sb_info *sbi,
32974d57b86dSChao Yu 			enum page_type type, enum temp_type temp);
329839a53e0cSJaegeuk Kim 
329939a53e0cSJaegeuk Kim /*
330039a53e0cSJaegeuk Kim  * checkpoint.c
330139a53e0cSJaegeuk Kim  */
3302cac5a3d8SDongOh Shin void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io);
33034d57b86dSChao Yu struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
33044d57b86dSChao Yu struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
33057735730dSChao Yu struct page *f2fs_get_meta_page_nofail(struct f2fs_sb_info *sbi, pgoff_t index);
33064d57b86dSChao Yu struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
3307e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
33084d57b86dSChao Yu 					block_t blkaddr, int type);
33094d57b86dSChao Yu int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
3310cac5a3d8SDongOh Shin 			int type, bool sync);
33114d57b86dSChao Yu void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index);
33124d57b86dSChao Yu long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
3313b0af6d49SChao Yu 			long nr_to_write, enum iostat_type io_type);
33144d57b86dSChao Yu void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
33154d57b86dSChao Yu void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
33164d57b86dSChao Yu void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all);
33174d57b86dSChao Yu bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
33184d57b86dSChao Yu void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
331939d787beSChao Yu 					unsigned int devidx, int type);
33204d57b86dSChao Yu bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
332139d787beSChao Yu 					unsigned int devidx, int type);
3322cac5a3d8SDongOh Shin int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
33234d57b86dSChao Yu int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi);
33244d57b86dSChao Yu void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi);
33254d57b86dSChao Yu void f2fs_add_orphan_inode(struct inode *inode);
33264d57b86dSChao Yu void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
33274d57b86dSChao Yu int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi);
33284d57b86dSChao Yu int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi);
33294d57b86dSChao Yu void f2fs_update_dirty_page(struct inode *inode, struct page *page);
33304d57b86dSChao Yu void f2fs_remove_dirty_inode(struct inode *inode);
33314d57b86dSChao Yu int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
3332bf22c3ccSSahitya Tummala void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type);
33334d57b86dSChao Yu int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
33344d57b86dSChao Yu void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi);
33354d57b86dSChao Yu int __init f2fs_create_checkpoint_caches(void);
33364d57b86dSChao Yu void f2fs_destroy_checkpoint_caches(void);
333739a53e0cSJaegeuk Kim 
333839a53e0cSJaegeuk Kim /*
333939a53e0cSJaegeuk Kim  * data.c
334039a53e0cSJaegeuk Kim  */
3341f543805fSChao Yu int __init f2fs_init_bioset(void);
3342f543805fSChao Yu void f2fs_destroy_bioset(void);
3343f543805fSChao Yu struct bio *f2fs_bio_alloc(struct f2fs_sb_info *sbi, int npages, bool no_fail);
33440b20fcecSChao Yu int f2fs_init_bio_entry_cache(void);
33450b20fcecSChao Yu void f2fs_destroy_bio_entry_cache(void);
33464c8ff709SChao Yu void f2fs_submit_bio(struct f2fs_sb_info *sbi,
33474c8ff709SChao Yu 				struct bio *bio, enum page_type type);
3348b9109b0eSJaegeuk Kim void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
3349b9109b0eSJaegeuk Kim void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
3350bab475c5SChao Yu 				struct inode *inode, struct page *page,
3351bab475c5SChao Yu 				nid_t ino, enum page_type type);
33520b20fcecSChao Yu void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
33530b20fcecSChao Yu 					struct bio **bio, struct page *page);
3354b9109b0eSJaegeuk Kim void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
3355cac5a3d8SDongOh Shin int f2fs_submit_page_bio(struct f2fs_io_info *fio);
33568648de2cSChao Yu int f2fs_merge_page_bio(struct f2fs_io_info *fio);
3357fe16efe6SChao Yu void f2fs_submit_page_write(struct f2fs_io_info *fio);
3358cac5a3d8SDongOh Shin struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
3359cac5a3d8SDongOh Shin 			block_t blk_addr, struct bio *bio);
3360cac5a3d8SDongOh Shin int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
33614d57b86dSChao Yu void f2fs_set_data_blkaddr(struct dnode_of_data *dn);
3362cac5a3d8SDongOh Shin void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
33634d57b86dSChao Yu int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
33644d57b86dSChao Yu int f2fs_reserve_new_block(struct dnode_of_data *dn);
3365cac5a3d8SDongOh Shin int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
3366cac5a3d8SDongOh Shin int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from);
3367cac5a3d8SDongOh Shin int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
33684c8ff709SChao Yu int f2fs_mpage_readpages(struct address_space *mapping,
33694c8ff709SChao Yu 			struct list_head *pages, struct page *page,
33704c8ff709SChao Yu 			unsigned nr_pages, bool is_readahead);
33714d57b86dSChao Yu struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
3372cac5a3d8SDongOh Shin 			int op_flags, bool for_write);
33734d57b86dSChao Yu struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index);
33744d57b86dSChao Yu struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
3375cac5a3d8SDongOh Shin 			bool for_write);
33764d57b86dSChao Yu struct page *f2fs_get_new_data_page(struct inode *inode,
3377cac5a3d8SDongOh Shin 			struct page *ipage, pgoff_t index, bool new_i_size);
33784d57b86dSChao Yu int f2fs_do_write_data_page(struct f2fs_io_info *fio);
337939a86958SChao Yu void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock);
3380cac5a3d8SDongOh Shin int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
3381cac5a3d8SDongOh Shin 			int create, int flag);
3382cac5a3d8SDongOh Shin int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3383cac5a3d8SDongOh Shin 			u64 start, u64 len);
33844c8ff709SChao Yu int f2fs_encrypt_one_page(struct f2fs_io_info *fio);
33854d57b86dSChao Yu bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio);
33864d57b86dSChao Yu bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio);
33874c8ff709SChao Yu int f2fs_write_single_data_page(struct page *page, int *submitted,
33884c8ff709SChao Yu 				struct bio **bio, sector_t *last_block,
33894c8ff709SChao Yu 				struct writeback_control *wbc,
33904c8ff709SChao Yu 				enum iostat_type io_type,
33914c8ff709SChao Yu 				int compr_blocks);
3392cac5a3d8SDongOh Shin void f2fs_invalidate_page(struct page *page, unsigned int offset,
3393cac5a3d8SDongOh Shin 			unsigned int length);
3394cac5a3d8SDongOh Shin int f2fs_release_page(struct page *page, gfp_t wait);
33955b7a487cSWeichao Guo #ifdef CONFIG_MIGRATION
3396cac5a3d8SDongOh Shin int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,
3397cac5a3d8SDongOh Shin 			struct page *page, enum migrate_mode mode);
33985b7a487cSWeichao Guo #endif
3399b91050a8SHyunchul Lee bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len);
34005ec2d99dSMatthew Wilcox void f2fs_clear_page_cache_dirty_tag(struct page *page);
34014c8ff709SChao Yu int f2fs_init_post_read_processing(void);
34024c8ff709SChao Yu void f2fs_destroy_post_read_processing(void);
34034c8ff709SChao Yu int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi);
34044c8ff709SChao Yu void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi);
340539a53e0cSJaegeuk Kim 
340639a53e0cSJaegeuk Kim /*
340739a53e0cSJaegeuk Kim  * gc.c
340839a53e0cSJaegeuk Kim  */
34094d57b86dSChao Yu int f2fs_start_gc_thread(struct f2fs_sb_info *sbi);
34104d57b86dSChao Yu void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi);
34114d57b86dSChao Yu block_t f2fs_start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
3412e066b83cSJaegeuk Kim int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background,
3413e066b83cSJaegeuk Kim 			unsigned int segno);
34144d57b86dSChao Yu void f2fs_build_gc_manager(struct f2fs_sb_info *sbi);
341504f0b2eaSQiuyang Sun int f2fs_resize_fs(struct f2fs_sb_info *sbi, __u64 block_count);
341639a53e0cSJaegeuk Kim 
341739a53e0cSJaegeuk Kim /*
341839a53e0cSJaegeuk Kim  * recovery.c
341939a53e0cSJaegeuk Kim  */
34204d57b86dSChao Yu int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
34214d57b86dSChao Yu bool f2fs_space_for_roll_forward(struct f2fs_sb_info *sbi);
342239a53e0cSJaegeuk Kim 
342339a53e0cSJaegeuk Kim /*
342439a53e0cSJaegeuk Kim  * debug.c
342539a53e0cSJaegeuk Kim  */
342639a53e0cSJaegeuk Kim #ifdef CONFIG_F2FS_STAT_FS
342739a53e0cSJaegeuk Kim struct f2fs_stat_info {
342839a53e0cSJaegeuk Kim 	struct list_head stat_list;
342939a53e0cSJaegeuk Kim 	struct f2fs_sb_info *sbi;
343039a53e0cSJaegeuk Kim 	int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
343139a53e0cSJaegeuk Kim 	int main_area_segs, main_area_sections, main_area_zones;
34325b7ee374SChao Yu 	unsigned long long hit_largest, hit_cached, hit_rbtree;
34335b7ee374SChao Yu 	unsigned long long hit_total, total_ext;
3434c00ba554SJaegeuk Kim 	int ext_tree, zombie_tree, ext_node;
34352c8a4a28SJaegeuk Kim 	int ndirty_node, ndirty_dent, ndirty_meta, ndirty_imeta;
34362c8a4a28SJaegeuk Kim 	int ndirty_data, ndirty_qdata;
343735782b23SJaegeuk Kim 	int inmem_pages;
34382c8a4a28SJaegeuk Kim 	unsigned int ndirty_dirs, ndirty_files, nquota_files, ndirty_all;
34395b0ef73cSJaegeuk Kim 	int nats, dirty_nats, sits, dirty_sits;
34405b0ef73cSJaegeuk Kim 	int free_nids, avail_nids, alloc_nids;
344139a53e0cSJaegeuk Kim 	int total_count, utilization;
34428b8dd65fSChao Yu 	int bg_gc, nr_wb_cp_data, nr_wb_data;
34435f9abab4SJaegeuk Kim 	int nr_rd_data, nr_rd_node, nr_rd_meta;
344402b16d0aSChao Yu 	int nr_dio_read, nr_dio_write;
3445274bd9baSChao Yu 	unsigned int io_skip_bggc, other_skip_bggc;
344614d8d5f7SChao Yu 	int nr_flushing, nr_flushed, flush_list_empty;
344714d8d5f7SChao Yu 	int nr_discarding, nr_discarded;
34485f32366aSChao Yu 	int nr_discard_cmd;
3449d84d1cbdSChao Yu 	unsigned int undiscard_blks;
3450a00861dbSJaegeuk Kim 	int inline_xattr, inline_inode, inline_dir, append, update, orphans;
34514c8ff709SChao Yu 	int compr_inode, compr_blocks;
3452648d50baSChao Yu 	int aw_cnt, max_aw_cnt, vw_cnt, max_vw_cnt;
3453f83a2584SYunlei He 	unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
345439a53e0cSJaegeuk Kim 	unsigned int bimodal, avg_vblocks;
345539a53e0cSJaegeuk Kim 	int util_free, util_valid, util_invalid;
345639a53e0cSJaegeuk Kim 	int rsvd_segs, overp_segs;
345739a53e0cSJaegeuk Kim 	int dirty_count, node_pages, meta_pages;
345842190d2aSJaegeuk Kim 	int prefree_count, call_count, cp_count, bg_cp_count;
345939a53e0cSJaegeuk Kim 	int tot_segs, node_segs, data_segs, free_segs, free_secs;
3460e1235983SChangman Lee 	int bg_node_segs, bg_data_segs;
346139a53e0cSJaegeuk Kim 	int tot_blks, data_blks, node_blks;
3462e1235983SChangman Lee 	int bg_data_blks, bg_node_blks;
34632ef79ecbSChao Yu 	unsigned long long skipped_atomic_files[2];
346439a53e0cSJaegeuk Kim 	int curseg[NR_CURSEG_TYPE];
346539a53e0cSJaegeuk Kim 	int cursec[NR_CURSEG_TYPE];
346639a53e0cSJaegeuk Kim 	int curzone[NR_CURSEG_TYPE];
346739a53e0cSJaegeuk Kim 
3468b63e7be5SChao Yu 	unsigned int meta_count[META_MAX];
346939a53e0cSJaegeuk Kim 	unsigned int segment_count[2];
347039a53e0cSJaegeuk Kim 	unsigned int block_count[2];
3471b9a2c252SChangman Lee 	unsigned int inplace_count;
34729edcdabfSChao Yu 	unsigned long long base_mem, cache_mem, page_mem;
347339a53e0cSJaegeuk Kim };
347439a53e0cSJaegeuk Kim 
3475963d4f7dSGu Zheng static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
3476963d4f7dSGu Zheng {
3477963d4f7dSGu Zheng 	return (struct f2fs_stat_info *)sbi->stat_info;
3478963d4f7dSGu Zheng }
3479963d4f7dSGu Zheng 
3480942e0be6SChangman Lee #define stat_inc_cp_count(si)		((si)->cp_count++)
348142190d2aSJaegeuk Kim #define stat_inc_bg_cp_count(si)	((si)->bg_cp_count++)
348239a53e0cSJaegeuk Kim #define stat_inc_call_count(si)		((si)->call_count++)
3483fc7100eaSHridya Valsaraju #define stat_inc_bggc_count(si)		((si)->bg_gc++)
3484274bd9baSChao Yu #define stat_io_skip_bggc_count(sbi)	((sbi)->io_skip_bggc++)
3485274bd9baSChao Yu #define stat_other_skip_bggc_count(sbi)	((sbi)->other_skip_bggc++)
348633fbd510SChao Yu #define stat_inc_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]++)
348733fbd510SChao Yu #define stat_dec_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]--)
34885b7ee374SChao Yu #define stat_inc_total_hit(sbi)		(atomic64_inc(&(sbi)->total_hit_ext))
34895b7ee374SChao Yu #define stat_inc_rbtree_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_rbtree))
34905b7ee374SChao Yu #define stat_inc_largest_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_largest))
34915b7ee374SChao Yu #define stat_inc_cached_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_cached))
3492d5e8f6c9SChao Yu #define stat_inc_inline_xattr(inode)					\
3493d5e8f6c9SChao Yu 	do {								\
3494d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3495d5e8f6c9SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_xattr));	\
3496d5e8f6c9SChao Yu 	} while (0)
3497d5e8f6c9SChao Yu #define stat_dec_inline_xattr(inode)					\
3498d5e8f6c9SChao Yu 	do {								\
3499d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3500d5e8f6c9SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_xattr));	\
3501d5e8f6c9SChao Yu 	} while (0)
35020dbdc2aeSJaegeuk Kim #define stat_inc_inline_inode(inode)					\
35030dbdc2aeSJaegeuk Kim 	do {								\
35040dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
350503e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_inode));	\
35060dbdc2aeSJaegeuk Kim 	} while (0)
35070dbdc2aeSJaegeuk Kim #define stat_dec_inline_inode(inode)					\
35080dbdc2aeSJaegeuk Kim 	do {								\
35090dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
351003e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_inode));	\
35110dbdc2aeSJaegeuk Kim 	} while (0)
35123289c061SJaegeuk Kim #define stat_inc_inline_dir(inode)					\
35133289c061SJaegeuk Kim 	do {								\
35143289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
351503e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_dir));	\
35163289c061SJaegeuk Kim 	} while (0)
35173289c061SJaegeuk Kim #define stat_dec_inline_dir(inode)					\
35183289c061SJaegeuk Kim 	do {								\
35193289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
352003e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_dir));	\
35213289c061SJaegeuk Kim 	} while (0)
35224c8ff709SChao Yu #define stat_inc_compr_inode(inode)					\
35234c8ff709SChao Yu 	do {								\
35244c8ff709SChao Yu 		if (f2fs_compressed_file(inode))			\
35254c8ff709SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->compr_inode));	\
35264c8ff709SChao Yu 	} while (0)
35274c8ff709SChao Yu #define stat_dec_compr_inode(inode)					\
35284c8ff709SChao Yu 	do {								\
35294c8ff709SChao Yu 		if (f2fs_compressed_file(inode))			\
35304c8ff709SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->compr_inode));	\
35314c8ff709SChao Yu 	} while (0)
35324c8ff709SChao Yu #define stat_add_compr_blocks(inode, blocks)				\
35334c8ff709SChao Yu 		(atomic_add(blocks, &F2FS_I_SB(inode)->compr_blocks))
35344c8ff709SChao Yu #define stat_sub_compr_blocks(inode, blocks)				\
35354c8ff709SChao Yu 		(atomic_sub(blocks, &F2FS_I_SB(inode)->compr_blocks))
3536b63e7be5SChao Yu #define stat_inc_meta_count(sbi, blkaddr)				\
3537b63e7be5SChao Yu 	do {								\
3538b63e7be5SChao Yu 		if (blkaddr < SIT_I(sbi)->sit_base_addr)		\
3539b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_CP]);	\
3540b63e7be5SChao Yu 		else if (blkaddr < NM_I(sbi)->nat_blkaddr)		\
3541b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_SIT]);	\
3542b63e7be5SChao Yu 		else if (blkaddr < SM_I(sbi)->ssa_blkaddr)		\
3543b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_NAT]);	\
3544b63e7be5SChao Yu 		else if (blkaddr < SM_I(sbi)->main_blkaddr)		\
3545b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_SSA]);	\
3546b63e7be5SChao Yu 	} while (0)
3547dcdfff65SJaegeuk Kim #define stat_inc_seg_type(sbi, curseg)					\
3548dcdfff65SJaegeuk Kim 		((sbi)->segment_count[(curseg)->alloc_type]++)
3549dcdfff65SJaegeuk Kim #define stat_inc_block_count(sbi, curseg)				\
3550dcdfff65SJaegeuk Kim 		((sbi)->block_count[(curseg)->alloc_type]++)
3551b9a2c252SChangman Lee #define stat_inc_inplace_blocks(sbi)					\
3552b9a2c252SChangman Lee 		(atomic_inc(&(sbi)->inplace_count))
355326a28a0cSJaegeuk Kim #define stat_update_max_atomic_write(inode)				\
355426a28a0cSJaegeuk Kim 	do {								\
35550e6d0164SSahitya Tummala 		int cur = F2FS_I_SB(inode)->atomic_files;	\
355626a28a0cSJaegeuk Kim 		int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt);	\
355726a28a0cSJaegeuk Kim 		if (cur > max)						\
355826a28a0cSJaegeuk Kim 			atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur);	\
355926a28a0cSJaegeuk Kim 	} while (0)
3560648d50baSChao Yu #define stat_inc_volatile_write(inode)					\
3561648d50baSChao Yu 		(atomic_inc(&F2FS_I_SB(inode)->vw_cnt))
3562648d50baSChao Yu #define stat_dec_volatile_write(inode)					\
3563648d50baSChao Yu 		(atomic_dec(&F2FS_I_SB(inode)->vw_cnt))
3564648d50baSChao Yu #define stat_update_max_volatile_write(inode)				\
3565648d50baSChao Yu 	do {								\
3566648d50baSChao Yu 		int cur = atomic_read(&F2FS_I_SB(inode)->vw_cnt);	\
3567648d50baSChao Yu 		int max = atomic_read(&F2FS_I_SB(inode)->max_vw_cnt);	\
3568648d50baSChao Yu 		if (cur > max)						\
3569648d50baSChao Yu 			atomic_set(&F2FS_I_SB(inode)->max_vw_cnt, cur);	\
3570648d50baSChao Yu 	} while (0)
3571e1235983SChangman Lee #define stat_inc_seg_count(sbi, type, gc_type)				\
357239a53e0cSJaegeuk Kim 	do {								\
3573963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
357468afcf2dSTomohiro Kusumi 		si->tot_segs++;						\
357568afcf2dSTomohiro Kusumi 		if ((type) == SUM_TYPE_DATA) {				\
357639a53e0cSJaegeuk Kim 			si->data_segs++;				\
3577e1235983SChangman Lee 			si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0;	\
3578e1235983SChangman Lee 		} else {						\
357939a53e0cSJaegeuk Kim 			si->node_segs++;				\
3580e1235983SChangman Lee 			si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0;	\
3581e1235983SChangman Lee 		}							\
358239a53e0cSJaegeuk Kim 	} while (0)
358339a53e0cSJaegeuk Kim 
358439a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks)				\
358568afcf2dSTomohiro Kusumi 	((si)->tot_blks += (blks))
358639a53e0cSJaegeuk Kim 
3587e1235983SChangman Lee #define stat_inc_data_blk_count(sbi, blks, gc_type)			\
358839a53e0cSJaegeuk Kim 	do {								\
3589963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
359039a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
359139a53e0cSJaegeuk Kim 		si->data_blks += (blks);				\
359268afcf2dSTomohiro Kusumi 		si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
359339a53e0cSJaegeuk Kim 	} while (0)
359439a53e0cSJaegeuk Kim 
3595e1235983SChangman Lee #define stat_inc_node_blk_count(sbi, blks, gc_type)			\
359639a53e0cSJaegeuk Kim 	do {								\
3597963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
359839a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
359939a53e0cSJaegeuk Kim 		si->node_blks += (blks);				\
360068afcf2dSTomohiro Kusumi 		si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
360139a53e0cSJaegeuk Kim 	} while (0)
360239a53e0cSJaegeuk Kim 
3603cac5a3d8SDongOh Shin int f2fs_build_stats(struct f2fs_sb_info *sbi);
3604cac5a3d8SDongOh Shin void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
360521acc07dSGreg Kroah-Hartman void __init f2fs_create_root_stats(void);
36064589d25dSNamjae Jeon void f2fs_destroy_root_stats(void);
3607fc7100eaSHridya Valsaraju void f2fs_update_sit_info(struct f2fs_sb_info *sbi);
360839a53e0cSJaegeuk Kim #else
3609d66450e7SArnd Bergmann #define stat_inc_cp_count(si)				do { } while (0)
3610d66450e7SArnd Bergmann #define stat_inc_bg_cp_count(si)			do { } while (0)
3611d66450e7SArnd Bergmann #define stat_inc_call_count(si)				do { } while (0)
3612d66450e7SArnd Bergmann #define stat_inc_bggc_count(si)				do { } while (0)
3613274bd9baSChao Yu #define stat_io_skip_bggc_count(sbi)			do { } while (0)
3614274bd9baSChao Yu #define stat_other_skip_bggc_count(sbi)			do { } while (0)
3615d66450e7SArnd Bergmann #define stat_inc_dirty_inode(sbi, type)			do { } while (0)
3616d66450e7SArnd Bergmann #define stat_dec_dirty_inode(sbi, type)			do { } while (0)
3617fc7100eaSHridya Valsaraju #define stat_inc_total_hit(sbi)				do { } while (0)
3618fc7100eaSHridya Valsaraju #define stat_inc_rbtree_node_hit(sbi)			do { } while (0)
3619d66450e7SArnd Bergmann #define stat_inc_largest_node_hit(sbi)			do { } while (0)
3620d66450e7SArnd Bergmann #define stat_inc_cached_node_hit(sbi)			do { } while (0)
3621d66450e7SArnd Bergmann #define stat_inc_inline_xattr(inode)			do { } while (0)
3622d66450e7SArnd Bergmann #define stat_dec_inline_xattr(inode)			do { } while (0)
3623d66450e7SArnd Bergmann #define stat_inc_inline_inode(inode)			do { } while (0)
3624d66450e7SArnd Bergmann #define stat_dec_inline_inode(inode)			do { } while (0)
3625d66450e7SArnd Bergmann #define stat_inc_inline_dir(inode)			do { } while (0)
3626d66450e7SArnd Bergmann #define stat_dec_inline_dir(inode)			do { } while (0)
36274c8ff709SChao Yu #define stat_inc_compr_inode(inode)			do { } while (0)
36284c8ff709SChao Yu #define stat_dec_compr_inode(inode)			do { } while (0)
36294c8ff709SChao Yu #define stat_add_compr_blocks(inode, blocks)		do { } while (0)
36304c8ff709SChao Yu #define stat_sub_compr_blocks(inode, blocks)		do { } while (0)
3631d66450e7SArnd Bergmann #define stat_inc_atomic_write(inode)			do { } while (0)
3632d66450e7SArnd Bergmann #define stat_dec_atomic_write(inode)			do { } while (0)
3633d66450e7SArnd Bergmann #define stat_update_max_atomic_write(inode)		do { } while (0)
3634d66450e7SArnd Bergmann #define stat_inc_volatile_write(inode)			do { } while (0)
3635d66450e7SArnd Bergmann #define stat_dec_volatile_write(inode)			do { } while (0)
3636d66450e7SArnd Bergmann #define stat_update_max_volatile_write(inode)		do { } while (0)
3637b63e7be5SChao Yu #define stat_inc_meta_count(sbi, blkaddr)		do { } while (0)
3638d66450e7SArnd Bergmann #define stat_inc_seg_type(sbi, curseg)			do { } while (0)
3639d66450e7SArnd Bergmann #define stat_inc_block_count(sbi, curseg)		do { } while (0)
3640d66450e7SArnd Bergmann #define stat_inc_inplace_blocks(sbi)			do { } while (0)
3641d66450e7SArnd Bergmann #define stat_inc_seg_count(sbi, type, gc_type)		do { } while (0)
3642d66450e7SArnd Bergmann #define stat_inc_tot_blk_count(si, blks)		do { } while (0)
3643d66450e7SArnd Bergmann #define stat_inc_data_blk_count(sbi, blks, gc_type)	do { } while (0)
3644d66450e7SArnd Bergmann #define stat_inc_node_blk_count(sbi, blks, gc_type)	do { } while (0)
364539a53e0cSJaegeuk Kim 
364639a53e0cSJaegeuk Kim static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
364739a53e0cSJaegeuk Kim static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
364821acc07dSGreg Kroah-Hartman static inline void __init f2fs_create_root_stats(void) { }
36494589d25dSNamjae Jeon static inline void f2fs_destroy_root_stats(void) { }
3650fc7100eaSHridya Valsaraju static inline void update_sit_info(struct f2fs_sb_info *sbi) {}
365139a53e0cSJaegeuk Kim #endif
365239a53e0cSJaegeuk Kim 
365339a53e0cSJaegeuk Kim extern const struct file_operations f2fs_dir_operations;
36542c2eb7a3SDaniel Rosenberg #ifdef CONFIG_UNICODE
36552c2eb7a3SDaniel Rosenberg extern const struct dentry_operations f2fs_dentry_ops;
36562c2eb7a3SDaniel Rosenberg #endif
365739a53e0cSJaegeuk Kim extern const struct file_operations f2fs_file_operations;
365839a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_file_inode_operations;
365939a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_dblock_aops;
366039a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_node_aops;
366139a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_meta_aops;
366239a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_dir_inode_operations;
366339a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_symlink_inode_operations;
3664cbaf042aSJaegeuk Kim extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
366539a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_special_inode_operations;
36664d57b86dSChao Yu extern struct kmem_cache *f2fs_inode_entry_slab;
36671001b347SHuajun Li 
3668e18c65b2SHuajun Li /*
3669e18c65b2SHuajun Li  * inline.c
3670e18c65b2SHuajun Li  */
3671cac5a3d8SDongOh Shin bool f2fs_may_inline_data(struct inode *inode);
3672cac5a3d8SDongOh Shin bool f2fs_may_inline_dentry(struct inode *inode);
36734d57b86dSChao Yu void f2fs_do_read_inline_data(struct page *page, struct page *ipage);
36744d57b86dSChao Yu void f2fs_truncate_inline_inode(struct inode *inode,
36754d57b86dSChao Yu 						struct page *ipage, u64 from);
3676cac5a3d8SDongOh Shin int f2fs_read_inline_data(struct inode *inode, struct page *page);
3677cac5a3d8SDongOh Shin int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
3678cac5a3d8SDongOh Shin int f2fs_convert_inline_inode(struct inode *inode);
3679b06af2afSJaegeuk Kim int f2fs_try_convert_inline_dir(struct inode *dir, struct dentry *dentry);
3680cac5a3d8SDongOh Shin int f2fs_write_inline_data(struct inode *inode, struct page *page);
36814d57b86dSChao Yu bool f2fs_recover_inline_data(struct inode *inode, struct page *npage);
36824d57b86dSChao Yu struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
3683cac5a3d8SDongOh Shin 			struct fscrypt_name *fname, struct page **res_page);
36844d57b86dSChao Yu int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent,
3685cac5a3d8SDongOh Shin 			struct page *ipage);
3686cac5a3d8SDongOh Shin int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
3687cac5a3d8SDongOh Shin 			const struct qstr *orig_name,
3688cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
36894d57b86dSChao Yu void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry,
36904d57b86dSChao Yu 				struct page *page, struct inode *dir,
36914d57b86dSChao Yu 				struct inode *inode);
3692cac5a3d8SDongOh Shin bool f2fs_empty_inline_dir(struct inode *dir);
3693cac5a3d8SDongOh Shin int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
3694cac5a3d8SDongOh Shin 			struct fscrypt_str *fstr);
3695cac5a3d8SDongOh Shin int f2fs_inline_data_fiemap(struct inode *inode,
3696cac5a3d8SDongOh Shin 			struct fiemap_extent_info *fieinfo,
3697cac5a3d8SDongOh Shin 			__u64 start, __u64 len);
3698cde4de12SJaegeuk Kim 
3699cde4de12SJaegeuk Kim /*
37002658e50dSJaegeuk Kim  * shrinker.c
37012658e50dSJaegeuk Kim  */
3702cac5a3d8SDongOh Shin unsigned long f2fs_shrink_count(struct shrinker *shrink,
3703cac5a3d8SDongOh Shin 			struct shrink_control *sc);
3704cac5a3d8SDongOh Shin unsigned long f2fs_shrink_scan(struct shrinker *shrink,
3705cac5a3d8SDongOh Shin 			struct shrink_control *sc);
3706cac5a3d8SDongOh Shin void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
3707cac5a3d8SDongOh Shin void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
37082658e50dSJaegeuk Kim 
37092658e50dSJaegeuk Kim /*
3710a28ef1f5SChao Yu  * extent_cache.c
3711a28ef1f5SChao Yu  */
37124dada3fdSChao Yu struct rb_entry *f2fs_lookup_rb_tree(struct rb_root_cached *root,
3713004b6862SChao Yu 				struct rb_entry *cached_re, unsigned int ofs);
37144d57b86dSChao Yu struct rb_node **f2fs_lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
37154dada3fdSChao Yu 				struct rb_root_cached *root,
37164dada3fdSChao Yu 				struct rb_node **parent,
37174dada3fdSChao Yu 				unsigned int ofs, bool *leftmost);
37184dada3fdSChao Yu struct rb_entry *f2fs_lookup_rb_tree_ret(struct rb_root_cached *root,
3719004b6862SChao Yu 		struct rb_entry *cached_re, unsigned int ofs,
3720004b6862SChao Yu 		struct rb_entry **prev_entry, struct rb_entry **next_entry,
3721004b6862SChao Yu 		struct rb_node ***insert_p, struct rb_node **insert_parent,
37224dada3fdSChao Yu 		bool force, bool *leftmost);
37234d57b86dSChao Yu bool f2fs_check_rb_tree_consistence(struct f2fs_sb_info *sbi,
37244dada3fdSChao Yu 						struct rb_root_cached *root);
3725cac5a3d8SDongOh Shin unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
3726cac5a3d8SDongOh Shin bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext);
3727cac5a3d8SDongOh Shin void f2fs_drop_extent_tree(struct inode *inode);
3728cac5a3d8SDongOh Shin unsigned int f2fs_destroy_extent_node(struct inode *inode);
3729cac5a3d8SDongOh Shin void f2fs_destroy_extent_tree(struct inode *inode);
3730cac5a3d8SDongOh Shin bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
3731cac5a3d8SDongOh Shin 			struct extent_info *ei);
3732cac5a3d8SDongOh Shin void f2fs_update_extent_cache(struct dnode_of_data *dn);
373319b2c30dSChao Yu void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
3734cac5a3d8SDongOh Shin 			pgoff_t fofs, block_t blkaddr, unsigned int len);
37354d57b86dSChao Yu void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi);
37364d57b86dSChao Yu int __init f2fs_create_extent_cache(void);
37374d57b86dSChao Yu void f2fs_destroy_extent_cache(void);
3738a28ef1f5SChao Yu 
3739a28ef1f5SChao Yu /*
37408ceffcb2SChao Yu  * sysfs.c
37418ceffcb2SChao Yu  */
3742dc6b2055SJaegeuk Kim int __init f2fs_init_sysfs(void);
3743dc6b2055SJaegeuk Kim void f2fs_exit_sysfs(void);
3744dc6b2055SJaegeuk Kim int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
3745dc6b2055SJaegeuk Kim void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
37468ceffcb2SChao Yu 
374795ae251fSEric Biggers /* verity.c */
374895ae251fSEric Biggers extern const struct fsverity_operations f2fs_verityops;
374995ae251fSEric Biggers 
37508ceffcb2SChao Yu /*
3751cde4de12SJaegeuk Kim  * crypto support
3752cde4de12SJaegeuk Kim  */
37531958593eSJaegeuk Kim static inline bool f2fs_encrypted_file(struct inode *inode)
37541958593eSJaegeuk Kim {
375562230e0dSChandan Rajendra 	return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
37561958593eSJaegeuk Kim }
37571958593eSJaegeuk Kim 
3758cde4de12SJaegeuk Kim static inline void f2fs_set_encrypted_inode(struct inode *inode)
3759cde4de12SJaegeuk Kim {
3760643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
3761cde4de12SJaegeuk Kim 	file_set_encrypt(inode);
37629149a5ebSChao Yu 	f2fs_set_inode_flags(inode);
3763cde4de12SJaegeuk Kim #endif
3764cde4de12SJaegeuk Kim }
3765cde4de12SJaegeuk Kim 
37666dbb1796SEric Biggers /*
37676dbb1796SEric Biggers  * Returns true if the reads of the inode's data need to undergo some
37686dbb1796SEric Biggers  * postprocessing step, like decryption or authenticity verification.
37696dbb1796SEric Biggers  */
37706dbb1796SEric Biggers static inline bool f2fs_post_read_required(struct inode *inode)
3771cde4de12SJaegeuk Kim {
37724c8ff709SChao Yu 	return f2fs_encrypted_file(inode) || fsverity_active(inode) ||
37734c8ff709SChao Yu 		f2fs_compressed_file(inode);
37744c8ff709SChao Yu }
37754c8ff709SChao Yu 
37764c8ff709SChao Yu /*
37774c8ff709SChao Yu  * compress.c
37784c8ff709SChao Yu  */
37794c8ff709SChao Yu #ifdef CONFIG_F2FS_FS_COMPRESSION
37804c8ff709SChao Yu bool f2fs_is_compressed_page(struct page *page);
37814c8ff709SChao Yu struct page *f2fs_compress_control_page(struct page *page);
37824c8ff709SChao Yu int f2fs_prepare_compress_overwrite(struct inode *inode,
37834c8ff709SChao Yu 			struct page **pagep, pgoff_t index, void **fsdata);
37844c8ff709SChao Yu bool f2fs_compress_write_end(struct inode *inode, void *fsdata,
37854c8ff709SChao Yu 					pgoff_t index, unsigned copied);
37864c8ff709SChao Yu void f2fs_compress_write_end_io(struct bio *bio, struct page *page);
37874c8ff709SChao Yu bool f2fs_is_compress_backend_ready(struct inode *inode);
37884c8ff709SChao Yu void f2fs_decompress_pages(struct bio *bio, struct page *page, bool verity);
37894c8ff709SChao Yu bool f2fs_cluster_is_empty(struct compress_ctx *cc);
37904c8ff709SChao Yu bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index);
37914c8ff709SChao Yu void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct page *page);
37924c8ff709SChao Yu int f2fs_write_multi_pages(struct compress_ctx *cc,
37934c8ff709SChao Yu 						int *submitted,
37944c8ff709SChao Yu 						struct writeback_control *wbc,
37954c8ff709SChao Yu 						enum iostat_type io_type);
37964c8ff709SChao Yu int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index);
37974c8ff709SChao Yu int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
37984c8ff709SChao Yu 				unsigned nr_pages, sector_t *last_block_in_bio,
3799*0683728aSChao Yu 				bool is_readahead, bool for_write);
38004c8ff709SChao Yu struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc);
38014c8ff709SChao Yu void f2fs_free_dic(struct decompress_io_ctx *dic);
38024c8ff709SChao Yu void f2fs_decompress_end_io(struct page **rpages,
38034c8ff709SChao Yu 			unsigned int cluster_size, bool err, bool verity);
38044c8ff709SChao Yu int f2fs_init_compress_ctx(struct compress_ctx *cc);
38054c8ff709SChao Yu void f2fs_destroy_compress_ctx(struct compress_ctx *cc);
38064c8ff709SChao Yu void f2fs_init_compress_info(struct f2fs_sb_info *sbi);
38074c8ff709SChao Yu #else
38084c8ff709SChao Yu static inline bool f2fs_is_compressed_page(struct page *page) { return false; }
38094c8ff709SChao Yu static inline bool f2fs_is_compress_backend_ready(struct inode *inode)
38104c8ff709SChao Yu {
38114c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
38124c8ff709SChao Yu 		return true;
38134c8ff709SChao Yu 	/* not support compression */
38144c8ff709SChao Yu 	return false;
38154c8ff709SChao Yu }
38164c8ff709SChao Yu static inline struct page *f2fs_compress_control_page(struct page *page)
38174c8ff709SChao Yu {
38184c8ff709SChao Yu 	WARN_ON_ONCE(1);
38194c8ff709SChao Yu 	return ERR_PTR(-EINVAL);
38204c8ff709SChao Yu }
38214c8ff709SChao Yu #endif
38224c8ff709SChao Yu 
38234c8ff709SChao Yu static inline void set_compress_context(struct inode *inode)
38244c8ff709SChao Yu {
38254c8ff709SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
38264c8ff709SChao Yu 
38274c8ff709SChao Yu 	F2FS_I(inode)->i_compress_algorithm =
38284c8ff709SChao Yu 			F2FS_OPTION(sbi).compress_algorithm;
38294c8ff709SChao Yu 	F2FS_I(inode)->i_log_cluster_size =
38304c8ff709SChao Yu 			F2FS_OPTION(sbi).compress_log_size;
38314c8ff709SChao Yu 	F2FS_I(inode)->i_cluster_size =
38324c8ff709SChao Yu 			1 << F2FS_I(inode)->i_log_cluster_size;
38334c8ff709SChao Yu 	F2FS_I(inode)->i_flags |= F2FS_COMPR_FL;
38344c8ff709SChao Yu 	set_inode_flag(inode, FI_COMPRESSED_FILE);
38354c8ff709SChao Yu 	stat_inc_compr_inode(inode);
38364c8ff709SChao Yu }
38374c8ff709SChao Yu 
38384c8ff709SChao Yu static inline u64 f2fs_disable_compressed_file(struct inode *inode)
38394c8ff709SChao Yu {
38404c8ff709SChao Yu 	struct f2fs_inode_info *fi = F2FS_I(inode);
38414c8ff709SChao Yu 
38424c8ff709SChao Yu 	if (!f2fs_compressed_file(inode))
38434c8ff709SChao Yu 		return 0;
38446cfdf15fSChao Yu 	if (get_dirty_pages(inode))
38456cfdf15fSChao Yu 		return 1;
38464c8ff709SChao Yu 	if (fi->i_compr_blocks)
38474c8ff709SChao Yu 		return fi->i_compr_blocks;
38484c8ff709SChao Yu 
38494c8ff709SChao Yu 	fi->i_flags &= ~F2FS_COMPR_FL;
38504c8ff709SChao Yu 	stat_dec_compr_inode(inode);
385196f5b4faSChao Yu 	clear_inode_flag(inode, FI_COMPRESSED_FILE);
38524c8ff709SChao Yu 	return 0;
3853cde4de12SJaegeuk Kim }
3854cde4de12SJaegeuk Kim 
3855ccd31cb2SSheng Yong #define F2FS_FEATURE_FUNCS(name, flagname) \
38567beb01f7SChao Yu static inline int f2fs_sb_has_##name(struct f2fs_sb_info *sbi) \
3857ccd31cb2SSheng Yong { \
38587beb01f7SChao Yu 	return F2FS_HAS_FEATURE(sbi, F2FS_FEATURE_##flagname); \
3859cde4de12SJaegeuk Kim }
3860f424f664SJaegeuk Kim 
3861ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(encrypt, ENCRYPT);
3862ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(blkzoned, BLKZONED);
3863ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(extra_attr, EXTRA_ATTR);
3864ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(project_quota, PRJQUOTA);
3865ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_chksum, INODE_CHKSUM);
3866ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
3867ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
3868ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
3869b7c409deSSheng Yong F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
387095ae251fSEric Biggers F2FS_FEATURE_FUNCS(verity, VERITY);
3871d440c52dSJunling Zheng F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
38725aba5430SDaniel Rosenberg F2FS_FEATURE_FUNCS(casefold, CASEFOLD);
38734c8ff709SChao Yu F2FS_FEATURE_FUNCS(compression, COMPRESSION);
38741c1d35dfSChao Yu 
3875178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
387695175dafSDamien Le Moal static inline bool f2fs_blkz_is_seq(struct f2fs_sb_info *sbi, int devi,
387795175dafSDamien Le Moal 				    block_t blkaddr)
3878178053e2SDamien Le Moal {
3879178053e2SDamien Le Moal 	unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
3880178053e2SDamien Le Moal 
388195175dafSDamien Le Moal 	return test_bit(zno, FDEV(devi).blkz_seq);
3882178053e2SDamien Le Moal }
3883178053e2SDamien Le Moal #endif
3884178053e2SDamien Le Moal 
38857d20c8abSChao Yu static inline bool f2fs_hw_should_discard(struct f2fs_sb_info *sbi)
388696ba2decSDamien Le Moal {
38877beb01f7SChao Yu 	return f2fs_sb_has_blkzoned(sbi);
38887d20c8abSChao Yu }
388996ba2decSDamien Le Moal 
38907f3d7719SDamien Le Moal static inline bool f2fs_bdev_support_discard(struct block_device *bdev)
38917f3d7719SDamien Le Moal {
38927f3d7719SDamien Le Moal 	return blk_queue_discard(bdev_get_queue(bdev)) ||
38937f3d7719SDamien Le Moal 	       bdev_is_zoned(bdev);
38947f3d7719SDamien Le Moal }
38957f3d7719SDamien Le Moal 
38967d20c8abSChao Yu static inline bool f2fs_hw_support_discard(struct f2fs_sb_info *sbi)
38977d20c8abSChao Yu {
38987f3d7719SDamien Le Moal 	int i;
38997f3d7719SDamien Le Moal 
39007f3d7719SDamien Le Moal 	if (!f2fs_is_multi_device(sbi))
39017f3d7719SDamien Le Moal 		return f2fs_bdev_support_discard(sbi->sb->s_bdev);
39027f3d7719SDamien Le Moal 
39037f3d7719SDamien Le Moal 	for (i = 0; i < sbi->s_ndevs; i++)
39047f3d7719SDamien Le Moal 		if (f2fs_bdev_support_discard(FDEV(i).bdev))
39057f3d7719SDamien Le Moal 			return true;
39067f3d7719SDamien Le Moal 	return false;
39077d20c8abSChao Yu }
39087d20c8abSChao Yu 
39097d20c8abSChao Yu static inline bool f2fs_realtime_discard_enable(struct f2fs_sb_info *sbi)
39107d20c8abSChao Yu {
39117d20c8abSChao Yu 	return (test_opt(sbi, DISCARD) && f2fs_hw_support_discard(sbi)) ||
39127d20c8abSChao Yu 					f2fs_hw_should_discard(sbi);
391352763a4bSJaegeuk Kim }
391452763a4bSJaegeuk Kim 
3915f824deb5SChao Yu static inline bool f2fs_hw_is_readonly(struct f2fs_sb_info *sbi)
3916f824deb5SChao Yu {
3917f824deb5SChao Yu 	int i;
3918f824deb5SChao Yu 
3919f824deb5SChao Yu 	if (!f2fs_is_multi_device(sbi))
3920f824deb5SChao Yu 		return bdev_read_only(sbi->sb->s_bdev);
3921f824deb5SChao Yu 
3922f824deb5SChao Yu 	for (i = 0; i < sbi->s_ndevs; i++)
3923f824deb5SChao Yu 		if (bdev_read_only(FDEV(i).bdev))
3924f824deb5SChao Yu 			return true;
3925f824deb5SChao Yu 	return false;
3926f824deb5SChao Yu }
3927f824deb5SChao Yu 
3928b0332a0fSChao Yu static inline bool f2fs_lfs_mode(struct f2fs_sb_info *sbi)
392952763a4bSJaegeuk Kim {
3930b0332a0fSChao Yu 	return F2FS_OPTION(sbi).fs_mode == FS_MODE_LFS;
393152763a4bSJaegeuk Kim }
393252763a4bSJaegeuk Kim 
3933fcc85a4dSJaegeuk Kim static inline bool f2fs_may_encrypt(struct inode *inode)
3934fcc85a4dSJaegeuk Kim {
3935643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
3936886f56f9SAl Viro 	umode_t mode = inode->i_mode;
3937fcc85a4dSJaegeuk Kim 
3938fcc85a4dSJaegeuk Kim 	return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
3939fcc85a4dSJaegeuk Kim #else
39406e45f2a5SGustavo A. R. Silva 	return false;
3941fcc85a4dSJaegeuk Kim #endif
3942fcc85a4dSJaegeuk Kim }
3943fcc85a4dSJaegeuk Kim 
39444c8ff709SChao Yu static inline bool f2fs_may_compress(struct inode *inode)
39454c8ff709SChao Yu {
39464c8ff709SChao Yu 	if (IS_SWAPFILE(inode) || f2fs_is_pinned_file(inode) ||
39474c8ff709SChao Yu 				f2fs_is_atomic_file(inode) ||
39484c8ff709SChao Yu 				f2fs_is_volatile_file(inode))
39494c8ff709SChao Yu 		return false;
39504c8ff709SChao Yu 	return S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode);
39514c8ff709SChao Yu }
39524c8ff709SChao Yu 
39534c8ff709SChao Yu static inline void f2fs_i_compr_blocks_update(struct inode *inode,
39544c8ff709SChao Yu 						u64 blocks, bool add)
39554c8ff709SChao Yu {
39564c8ff709SChao Yu 	int diff = F2FS_I(inode)->i_cluster_size - blocks;
39574c8ff709SChao Yu 
39584c8ff709SChao Yu 	if (add) {
39594c8ff709SChao Yu 		F2FS_I(inode)->i_compr_blocks += diff;
39604c8ff709SChao Yu 		stat_add_compr_blocks(inode, diff);
39614c8ff709SChao Yu 	} else {
39624c8ff709SChao Yu 		F2FS_I(inode)->i_compr_blocks -= diff;
39634c8ff709SChao Yu 		stat_sub_compr_blocks(inode, diff);
39644c8ff709SChao Yu 	}
39654c8ff709SChao Yu 	f2fs_mark_inode_dirty_sync(inode, true);
39664c8ff709SChao Yu }
39674c8ff709SChao Yu 
3968f847c699SChao Yu static inline int block_unaligned_IO(struct inode *inode,
3969f847c699SChao Yu 				struct kiocb *iocb, struct iov_iter *iter)
3970b91050a8SHyunchul Lee {
3971f847c699SChao Yu 	unsigned int i_blkbits = READ_ONCE(inode->i_blkbits);
3972f847c699SChao Yu 	unsigned int blocksize_mask = (1 << i_blkbits) - 1;
3973f847c699SChao Yu 	loff_t offset = iocb->ki_pos;
3974f847c699SChao Yu 	unsigned long align = offset | iov_iter_alignment(iter);
3975f847c699SChao Yu 
3976f847c699SChao Yu 	return align & blocksize_mask;
3977f847c699SChao Yu }
3978f847c699SChao Yu 
3979f847c699SChao Yu static inline int allow_outplace_dio(struct inode *inode,
3980f847c699SChao Yu 				struct kiocb *iocb, struct iov_iter *iter)
3981f847c699SChao Yu {
3982f847c699SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3983f847c699SChao Yu 	int rw = iov_iter_rw(iter);
3984f847c699SChao Yu 
3985b0332a0fSChao Yu 	return (f2fs_lfs_mode(sbi) && (rw == WRITE) &&
3986f847c699SChao Yu 				!block_unaligned_IO(inode, iocb, iter));
3987f847c699SChao Yu }
3988f847c699SChao Yu 
3989f847c699SChao Yu static inline bool f2fs_force_buffered_io(struct inode *inode,
3990f847c699SChao Yu 				struct kiocb *iocb, struct iov_iter *iter)
3991f847c699SChao Yu {
3992f847c699SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3993f847c699SChao Yu 	int rw = iov_iter_rw(iter);
3994f847c699SChao Yu 
3995f847c699SChao Yu 	if (f2fs_post_read_required(inode))
3996f847c699SChao Yu 		return true;
39970916878dSDamien Le Moal 	if (f2fs_is_multi_device(sbi))
3998f847c699SChao Yu 		return true;
39994c8ff709SChao Yu 	if (f2fs_compressed_file(inode))
40004c8ff709SChao Yu 		return true;
4001f847c699SChao Yu 	/*
4002f847c699SChao Yu 	 * for blkzoned device, fallback direct IO to buffered IO, so
4003f847c699SChao Yu 	 * all IOs can be serialized by log-structured write.
4004f847c699SChao Yu 	 */
40057beb01f7SChao Yu 	if (f2fs_sb_has_blkzoned(sbi))
4006f847c699SChao Yu 		return true;
4007b0332a0fSChao Yu 	if (f2fs_lfs_mode(sbi) && (rw == WRITE)) {
40089720ee80SChao Yu 		if (block_unaligned_IO(inode, iocb, iter))
4009f847c699SChao Yu 			return true;
40109720ee80SChao Yu 		if (F2FS_IO_ALIGNED(sbi))
40119720ee80SChao Yu 			return true;
40129720ee80SChao Yu 	}
40134969c06aSJaegeuk Kim 	if (is_sbi_flag_set(F2FS_I_SB(inode), SBI_CP_DISABLED) &&
40143ee0c5d3SChao Yu 					!IS_SWAPFILE(inode))
40154354994fSDaniel Rosenberg 		return true;
40164354994fSDaniel Rosenberg 
4017f847c699SChao Yu 	return false;
4018b91050a8SHyunchul Lee }
4019b91050a8SHyunchul Lee 
402083a3bfdbSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
4021d494500aSChao Yu extern void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate,
4022d494500aSChao Yu 							unsigned int type);
402383a3bfdbSJaegeuk Kim #else
4024d494500aSChao Yu #define f2fs_build_fault_attr(sbi, rate, type)		do { } while (0)
402583a3bfdbSJaegeuk Kim #endif
402683a3bfdbSJaegeuk Kim 
4027af033b2aSChao Yu static inline bool is_journalled_quota(struct f2fs_sb_info *sbi)
4028af033b2aSChao Yu {
4029af033b2aSChao Yu #ifdef CONFIG_QUOTA
40307beb01f7SChao Yu 	if (f2fs_sb_has_quota_ino(sbi))
4031af033b2aSChao Yu 		return true;
4032af033b2aSChao Yu 	if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
4033af033b2aSChao Yu 		F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
4034af033b2aSChao Yu 		F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
4035af033b2aSChao Yu 		return true;
4036af033b2aSChao Yu #endif
4037af033b2aSChao Yu 	return false;
4038af033b2aSChao Yu }
40390af725fcSJaegeuk Kim 
404010f966bbSChao Yu #define EFSBADCRC	EBADMSG		/* Bad CRC detected */
404110f966bbSChao Yu #define EFSCORRUPTED	EUCLEAN		/* Filesystem is corrupted */
404210f966bbSChao Yu 
4043e1074d4bSChao Yu #endif /* _LINUX_F2FS_H */
4044