xref: /openbmc/linux/fs/f2fs/f2fs.h (revision af033b2aa8a874fd5737fafe90d159136527b5b4)
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 #define __FS_HAS_ENCRYPTION IS_ENABLED(CONFIG_F2FS_FS_ENCRYPTION)
28734f0d24SDave Chinner #include <linux/fscrypt.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 
702d3a5856SGao Xiang extern 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_BG_GC		0x00000001
7839a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_ROLL_FORWARD	0x00000002
7939a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISCARD		0x00000004
8039a53e0cSJaegeuk Kim #define F2FS_MOUNT_NOHEAP		0x00000008
8139a53e0cSJaegeuk Kim #define F2FS_MOUNT_XATTR_USER		0x00000010
8239a53e0cSJaegeuk Kim #define F2FS_MOUNT_POSIX_ACL		0x00000020
8339a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_EXT_IDENTIFY	0x00000040
84444c580fSJaegeuk Kim #define F2FS_MOUNT_INLINE_XATTR		0x00000080
851001b347SHuajun Li #define F2FS_MOUNT_INLINE_DATA		0x00000100
8634d67debSChao Yu #define F2FS_MOUNT_INLINE_DENTRY	0x00000200
8734d67debSChao Yu #define F2FS_MOUNT_FLUSH_MERGE		0x00000400
8834d67debSChao Yu #define F2FS_MOUNT_NOBARRIER		0x00000800
89d5053a34SJaegeuk Kim #define F2FS_MOUNT_FASTBOOT		0x00001000
9089672159SChao Yu #define F2FS_MOUNT_EXTENT_CACHE		0x00002000
916aefd93bSJaegeuk Kim #define F2FS_MOUNT_FORCE_FG_GC		0x00004000
92343f40f0SChao Yu #define F2FS_MOUNT_DATA_FLUSH		0x00008000
9373faec4dSJaegeuk Kim #define F2FS_MOUNT_FAULT_INJECTION	0x00010000
9436abef4eSJaegeuk Kim #define F2FS_MOUNT_ADAPTIVE		0x00020000
9536abef4eSJaegeuk Kim #define F2FS_MOUNT_LFS			0x00040000
960abd675eSChao Yu #define F2FS_MOUNT_USRQUOTA		0x00080000
970abd675eSChao Yu #define F2FS_MOUNT_GRPQUOTA		0x00100000
985c57132eSChao Yu #define F2FS_MOUNT_PRJQUOTA		0x00200000
994b2414d0SChao Yu #define F2FS_MOUNT_QUOTA		0x00400000
1006afc662eSChao Yu #define F2FS_MOUNT_INLINE_XATTR_SIZE	0x00800000
1017e65be49SJaegeuk Kim #define F2FS_MOUNT_RESERVE_ROOT		0x01000000
1024354994fSDaniel Rosenberg #define F2FS_MOUNT_DISABLE_CHECKPOINT	0x02000000
10339a53e0cSJaegeuk Kim 
10463189b78SChao Yu #define F2FS_OPTION(sbi)	((sbi)->mount_opt)
10563189b78SChao Yu #define clear_opt(sbi, option)	(F2FS_OPTION(sbi).opt &= ~F2FS_MOUNT_##option)
10663189b78SChao Yu #define set_opt(sbi, option)	(F2FS_OPTION(sbi).opt |= F2FS_MOUNT_##option)
10763189b78SChao Yu #define test_opt(sbi, option)	(F2FS_OPTION(sbi).opt & F2FS_MOUNT_##option)
10839a53e0cSJaegeuk Kim 
10939a53e0cSJaegeuk Kim #define ver_after(a, b)	(typecheck(unsigned long long, a) &&		\
11039a53e0cSJaegeuk Kim 		typecheck(unsigned long long, b) &&			\
11139a53e0cSJaegeuk Kim 		((long long)((a) - (b)) > 0))
11239a53e0cSJaegeuk Kim 
113a9841c4dSJaegeuk Kim typedef u32 block_t;	/*
114a9841c4dSJaegeuk Kim 			 * should not change u32, since it is the on-disk block
115a9841c4dSJaegeuk Kim 			 * address format, __le32.
116a9841c4dSJaegeuk Kim 			 */
11739a53e0cSJaegeuk Kim typedef u32 nid_t;
11839a53e0cSJaegeuk Kim 
11939a53e0cSJaegeuk Kim struct f2fs_mount_info {
12039a53e0cSJaegeuk Kim 	unsigned int opt;
12163189b78SChao Yu 	int write_io_size_bits;		/* Write IO size bits */
12263189b78SChao Yu 	block_t root_reserved_blocks;	/* root reserved blocks */
12363189b78SChao Yu 	kuid_t s_resuid;		/* reserved blocks for uid */
12463189b78SChao Yu 	kgid_t s_resgid;		/* reserved blocks for gid */
12563189b78SChao Yu 	int active_logs;		/* # of active logs */
12663189b78SChao Yu 	int inline_xattr_size;		/* inline xattr size */
12763189b78SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
12863189b78SChao Yu 	struct f2fs_fault_info fault_info;	/* For fault injection */
12963189b78SChao Yu #endif
13063189b78SChao Yu #ifdef CONFIG_QUOTA
13163189b78SChao Yu 	/* Names of quota files with journalled quota */
13263189b78SChao Yu 	char *s_qf_names[MAXQUOTAS];
13363189b78SChao Yu 	int s_jquota_fmt;			/* Format of quota to use */
13463189b78SChao Yu #endif
13563189b78SChao Yu 	/* For which write hints are passed down to block layer */
13663189b78SChao Yu 	int whint_mode;
13763189b78SChao Yu 	int alloc_mode;			/* segment allocation policy */
13863189b78SChao Yu 	int fsync_mode;			/* fsync policy */
139ff62af20SSheng Yong 	bool test_dummy_encryption;	/* test dummy encryption */
14039a53e0cSJaegeuk Kim };
14139a53e0cSJaegeuk Kim 
142cde4de12SJaegeuk Kim #define F2FS_FEATURE_ENCRYPT		0x0001
1430bfd7a09SDamien Le Moal #define F2FS_FEATURE_BLKZONED		0x0002
144e65ef207SJaegeuk Kim #define F2FS_FEATURE_ATOMIC_WRITE	0x0004
1457a2af766SChao Yu #define F2FS_FEATURE_EXTRA_ATTR		0x0008
1465c57132eSChao Yu #define F2FS_FEATURE_PRJQUOTA		0x0010
147704956ecSChao Yu #define F2FS_FEATURE_INODE_CHKSUM	0x0020
1486afc662eSChao Yu #define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR	0x0040
149234a9689SJaegeuk Kim #define F2FS_FEATURE_QUOTA_INO		0x0080
1501c1d35dfSChao Yu #define F2FS_FEATURE_INODE_CRTIME	0x0100
151b7c409deSSheng Yong #define F2FS_FEATURE_LOST_FOUND		0x0200
15253fedcc0SEric Biggers #define F2FS_FEATURE_VERITY		0x0400	/* reserved */
153d440c52dSJunling Zheng #define F2FS_FEATURE_SB_CHKSUM		0x0800
154cde4de12SJaegeuk Kim 
15576f105a2SJaegeuk Kim #define F2FS_HAS_FEATURE(sb, mask)					\
15676f105a2SJaegeuk Kim 	((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
15776f105a2SJaegeuk Kim #define F2FS_SET_FEATURE(sb, mask)					\
158c64ab12eSDongOh Shin 	(F2FS_SB(sb)->raw_super->feature |= cpu_to_le32(mask))
15976f105a2SJaegeuk Kim #define F2FS_CLEAR_FEATURE(sb, mask)					\
160c64ab12eSDongOh Shin 	(F2FS_SB(sb)->raw_super->feature &= ~cpu_to_le32(mask))
16176f105a2SJaegeuk Kim 
16239a53e0cSJaegeuk Kim /*
1637c2e5963SJaegeuk Kim  * Default values for user and/or group using reserved blocks
1647c2e5963SJaegeuk Kim  */
1657c2e5963SJaegeuk Kim #define	F2FS_DEF_RESUID		0
1667c2e5963SJaegeuk Kim #define	F2FS_DEF_RESGID		0
1677c2e5963SJaegeuk Kim 
1687c2e5963SJaegeuk Kim /*
16939a53e0cSJaegeuk Kim  * For checkpoint manager
17039a53e0cSJaegeuk Kim  */
17139a53e0cSJaegeuk Kim enum {
17239a53e0cSJaegeuk Kim 	NAT_BITMAP,
17339a53e0cSJaegeuk Kim 	SIT_BITMAP
17439a53e0cSJaegeuk Kim };
17539a53e0cSJaegeuk Kim 
176c473f1a9SChao Yu #define	CP_UMOUNT	0x00000001
177c473f1a9SChao Yu #define	CP_FASTBOOT	0x00000002
178c473f1a9SChao Yu #define	CP_SYNC		0x00000004
179c473f1a9SChao Yu #define	CP_RECOVERY	0x00000008
180c473f1a9SChao Yu #define	CP_DISCARD	0x00000010
1811f43e2adSChao Yu #define CP_TRIMMED	0x00000020
1824354994fSDaniel Rosenberg #define CP_PAUSE	0x00000040
18375ab4cb8SJaegeuk Kim 
1844ddb1a4dSJaegeuk Kim #define MAX_DISCARD_BLOCKS(sbi)		BLKS_PER_SEC(sbi)
185ecc9aa00SChao Yu #define DEF_MAX_DISCARD_REQUEST		8	/* issue 8 discards per round */
186969d1b18SChao Yu #define DEF_MIN_DISCARD_ISSUE_TIME	50	/* 50 ms, if exists */
187f9d1dcedSYunlei He #define DEF_MID_DISCARD_ISSUE_TIME	500	/* 500 ms, if device busy */
188969d1b18SChao Yu #define DEF_MAX_DISCARD_ISSUE_TIME	60000	/* 60 s, if no candidates */
1898bb4f253SJaegeuk Kim #define DEF_DISCARD_URGENT_UTIL		80	/* do more discard over 80% */
19060b99b48SJaegeuk Kim #define DEF_CP_INTERVAL			60	/* 60 secs */
191dcf25fe8SChao Yu #define DEF_IDLE_INTERVAL		5	/* 5 secs */
1924354994fSDaniel Rosenberg #define DEF_DISABLE_INTERVAL		5	/* 5 secs */
193bba681cbSJaegeuk Kim 
19475ab4cb8SJaegeuk Kim struct cp_control {
19575ab4cb8SJaegeuk Kim 	int reason;
1964b2fecc8SJaegeuk Kim 	__u64 trim_start;
1974b2fecc8SJaegeuk Kim 	__u64 trim_end;
1984b2fecc8SJaegeuk Kim 	__u64 trim_minlen;
19975ab4cb8SJaegeuk Kim };
20075ab4cb8SJaegeuk Kim 
201662befdaSChao Yu /*
202e1da7872SChao Yu  * indicate meta/data type
203662befdaSChao Yu  */
204662befdaSChao Yu enum {
205662befdaSChao Yu 	META_CP,
206662befdaSChao Yu 	META_NAT,
20781c1a0f1SChao Yu 	META_SIT,
2084c521f49SJaegeuk Kim 	META_SSA,
209b63e7be5SChao Yu 	META_MAX,
2104c521f49SJaegeuk Kim 	META_POR,
211e1da7872SChao Yu 	DATA_GENERIC,
212e1da7872SChao Yu 	META_GENERIC,
213662befdaSChao Yu };
214662befdaSChao Yu 
2156451e041SJaegeuk Kim /* for the list of ino */
2166451e041SJaegeuk Kim enum {
2176451e041SJaegeuk Kim 	ORPHAN_INO,		/* for orphan ino list */
218fff04f90SJaegeuk Kim 	APPEND_INO,		/* for append ino list */
219fff04f90SJaegeuk Kim 	UPDATE_INO,		/* for update ino list */
2200a007b97SJaegeuk Kim 	TRANS_DIR_INO,		/* for trasactions dir ino list */
22139d787beSChao Yu 	FLUSH_INO,		/* for multiple device flushing */
2226451e041SJaegeuk Kim 	MAX_INO_ENTRY,		/* max. list */
2236451e041SJaegeuk Kim };
2246451e041SJaegeuk Kim 
2256451e041SJaegeuk Kim struct ino_entry {
22639a53e0cSJaegeuk Kim 	struct list_head list;		/* list head */
22739a53e0cSJaegeuk Kim 	nid_t ino;			/* inode number */
22839d787beSChao Yu 	unsigned int dirty_device;	/* dirty device bitmap */
22939a53e0cSJaegeuk Kim };
23039a53e0cSJaegeuk Kim 
2312710fd7eSChao Yu /* for the list of inodes to be GCed */
23206292073SChao Yu struct inode_entry {
23339a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
23439a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
23539a53e0cSJaegeuk Kim };
23639a53e0cSJaegeuk Kim 
23750fa53ecSChao Yu struct fsync_node_entry {
23850fa53ecSChao Yu 	struct list_head list;	/* list head */
23950fa53ecSChao Yu 	struct page *page;	/* warm node page pointer */
24050fa53ecSChao Yu 	unsigned int seq_id;	/* sequence id */
24150fa53ecSChao Yu };
24250fa53ecSChao Yu 
243a7eeb823SChao Yu /* for the bitmap indicate blocks to be discarded */
2447fd9e544SJaegeuk Kim struct discard_entry {
2457fd9e544SJaegeuk Kim 	struct list_head list;	/* list head */
246a7eeb823SChao Yu 	block_t start_blkaddr;	/* start blockaddr of current segment */
247a7eeb823SChao Yu 	unsigned char discard_map[SIT_VBLOCK_MAP_SIZE];	/* segment discard bitmap */
2487fd9e544SJaegeuk Kim };
2497fd9e544SJaegeuk Kim 
250969d1b18SChao Yu /* default discard granularity of inner discard thread, unit: block count */
251969d1b18SChao Yu #define DEFAULT_DISCARD_GRANULARITY		16
252969d1b18SChao Yu 
253ba48a33eSChao Yu /* max discard pend list number */
254ba48a33eSChao Yu #define MAX_PLIST_NUM		512
255ba48a33eSChao Yu #define plist_idx(blk_num)	((blk_num) >= MAX_PLIST_NUM ?		\
256ba48a33eSChao Yu 					(MAX_PLIST_NUM - 1) : (blk_num - 1))
257ba48a33eSChao Yu 
25815469963SJaegeuk Kim enum {
25935ec7d57SChao Yu 	D_PREP,			/* initial */
26035ec7d57SChao Yu 	D_PARTIAL,		/* partially submitted */
26135ec7d57SChao Yu 	D_SUBMIT,		/* all submitted */
26235ec7d57SChao Yu 	D_DONE,			/* finished */
26315469963SJaegeuk Kim };
26415469963SJaegeuk Kim 
265004b6862SChao Yu struct discard_info {
266b01a9201SJaegeuk Kim 	block_t lstart;			/* logical start address */
267b01a9201SJaegeuk Kim 	block_t len;			/* length */
268004b6862SChao Yu 	block_t start;			/* actual start address in dev */
269004b6862SChao Yu };
270004b6862SChao Yu 
271b01a9201SJaegeuk Kim struct discard_cmd {
272004b6862SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
273004b6862SChao Yu 	union {
274004b6862SChao Yu 		struct {
275004b6862SChao Yu 			block_t lstart;	/* logical start address */
276004b6862SChao Yu 			block_t len;	/* length */
277004b6862SChao Yu 			block_t start;	/* actual start address in dev */
278004b6862SChao Yu 		};
279004b6862SChao Yu 		struct discard_info di;	/* discard info */
280004b6862SChao Yu 
281004b6862SChao Yu 	};
282b01a9201SJaegeuk Kim 	struct list_head list;		/* command list */
283b01a9201SJaegeuk Kim 	struct completion wait;		/* compleation */
284c81abe34SJaegeuk Kim 	struct block_device *bdev;	/* bdev */
285ec9895adSChao Yu 	unsigned short ref;		/* reference count */
2869a744b92SChao Yu 	unsigned char state;		/* state */
28735ec7d57SChao Yu 	unsigned char issuing;		/* issuing discard */
288c81abe34SJaegeuk Kim 	int error;			/* bio error */
28935ec7d57SChao Yu 	spinlock_t lock;		/* for state/bio_ref updating */
29035ec7d57SChao Yu 	unsigned short bio_ref;		/* bio reference count */
291275b66b0SChao Yu };
292275b66b0SChao Yu 
29378997b56SChao Yu enum {
29478997b56SChao Yu 	DPOLICY_BG,
29578997b56SChao Yu 	DPOLICY_FORCE,
29678997b56SChao Yu 	DPOLICY_FSTRIM,
29778997b56SChao Yu 	DPOLICY_UMOUNT,
29878997b56SChao Yu 	MAX_DPOLICY,
29978997b56SChao Yu };
30078997b56SChao Yu 
301ecc9aa00SChao Yu struct discard_policy {
30278997b56SChao Yu 	int type;			/* type of discard */
303ecc9aa00SChao Yu 	unsigned int min_interval;	/* used for candidates exist */
304f9d1dcedSYunlei He 	unsigned int mid_interval;	/* used for device busy */
305ecc9aa00SChao Yu 	unsigned int max_interval;	/* used for candidates not exist */
306ecc9aa00SChao Yu 	unsigned int max_requests;	/* # of discards issued per round */
307ecc9aa00SChao Yu 	unsigned int io_aware_gran;	/* minimum granularity discard not be aware of I/O */
308ecc9aa00SChao Yu 	bool io_aware;			/* issue discard in idle time */
309ecc9aa00SChao Yu 	bool sync;			/* submit discard with REQ_SYNC flag */
31020ee4382SChao Yu 	bool ordered;			/* issue discard by lba order */
31178997b56SChao Yu 	unsigned int granularity;	/* discard granularity */
312ecc9aa00SChao Yu };
313ecc9aa00SChao Yu 
3140b54fb84SJaegeuk Kim struct discard_cmd_control {
31515469963SJaegeuk Kim 	struct task_struct *f2fs_issue_discard;	/* discard thread */
31646f84c2cSChao Yu 	struct list_head entry_list;		/* 4KB discard entry list */
317ba48a33eSChao Yu 	struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
31846f84c2cSChao Yu 	struct list_head wait_list;		/* store on-flushing entries */
3198412663dSChao Yu 	struct list_head fstrim_list;		/* in-flight discard from fstrim */
32015469963SJaegeuk Kim 	wait_queue_head_t discard_wait_queue;	/* waiting queue for wake-up */
321969d1b18SChao Yu 	unsigned int discard_wake;		/* to wake up discard thread */
32215469963SJaegeuk Kim 	struct mutex cmd_lock;
323d618ebafSChao Yu 	unsigned int nr_discards;		/* # of discards in the list */
324d618ebafSChao Yu 	unsigned int max_discards;		/* max. discards to be issued */
325969d1b18SChao Yu 	unsigned int discard_granularity;	/* discard granularity */
326d84d1cbdSChao Yu 	unsigned int undiscard_blks;		/* # of undiscard blocks */
32720ee4382SChao Yu 	unsigned int next_pos;			/* next discard position */
3288b8dd65fSChao Yu 	atomic_t issued_discard;		/* # of issued discard */
3298b8dd65fSChao Yu 	atomic_t issing_discard;		/* # of issing discard */
3305f32366aSChao Yu 	atomic_t discard_cmd_cnt;		/* # of cached cmd count */
3314dada3fdSChao Yu 	struct rb_root_cached root;		/* root of discard rb-tree */
33267fce70bSChao Yu 	bool rbtree_check;			/* config for consistence check */
33339a53e0cSJaegeuk Kim };
33439a53e0cSJaegeuk Kim 
33539a53e0cSJaegeuk Kim /* for the list of fsync inodes, used only during recovery */
33639a53e0cSJaegeuk Kim struct fsync_inode_entry {
33739a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
33839a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
339c52e1b10SJaegeuk Kim 	block_t blkaddr;	/* block address locating the last fsync */
340c52e1b10SJaegeuk Kim 	block_t last_dentry;	/* block address locating the last dentry */
34139a53e0cSJaegeuk Kim };
34239a53e0cSJaegeuk Kim 
34368afcf2dSTomohiro Kusumi #define nats_in_cursum(jnl)		(le16_to_cpu((jnl)->n_nats))
34468afcf2dSTomohiro Kusumi #define sits_in_cursum(jnl)		(le16_to_cpu((jnl)->n_sits))
34539a53e0cSJaegeuk Kim 
34668afcf2dSTomohiro Kusumi #define nat_in_journal(jnl, i)		((jnl)->nat_j.entries[i].ne)
34768afcf2dSTomohiro Kusumi #define nid_in_journal(jnl, i)		((jnl)->nat_j.entries[i].nid)
34868afcf2dSTomohiro Kusumi #define sit_in_journal(jnl, i)		((jnl)->sit_j.entries[i].se)
34968afcf2dSTomohiro Kusumi #define segno_in_journal(jnl, i)	((jnl)->sit_j.entries[i].segno)
35039a53e0cSJaegeuk Kim 
351dfc08a12SChao Yu #define MAX_NAT_JENTRIES(jnl)	(NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
352dfc08a12SChao Yu #define MAX_SIT_JENTRIES(jnl)	(SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
353309cc2b6SJaegeuk Kim 
354dfc08a12SChao Yu static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
35539a53e0cSJaegeuk Kim {
356dfc08a12SChao Yu 	int before = nats_in_cursum(journal);
357cac5a3d8SDongOh Shin 
358dfc08a12SChao Yu 	journal->n_nats = cpu_to_le16(before + i);
35939a53e0cSJaegeuk Kim 	return before;
36039a53e0cSJaegeuk Kim }
36139a53e0cSJaegeuk Kim 
362dfc08a12SChao Yu static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
36339a53e0cSJaegeuk Kim {
364dfc08a12SChao Yu 	int before = sits_in_cursum(journal);
365cac5a3d8SDongOh Shin 
366dfc08a12SChao Yu 	journal->n_sits = cpu_to_le16(before + i);
36739a53e0cSJaegeuk Kim 	return before;
36839a53e0cSJaegeuk Kim }
36939a53e0cSJaegeuk Kim 
370dfc08a12SChao Yu static inline bool __has_cursum_space(struct f2fs_journal *journal,
371dfc08a12SChao Yu 							int size, int type)
372184a5cd2SChao Yu {
373184a5cd2SChao Yu 	if (type == NAT_JOURNAL)
374dfc08a12SChao Yu 		return size <= MAX_NAT_JENTRIES(journal);
375dfc08a12SChao Yu 	return size <= MAX_SIT_JENTRIES(journal);
376184a5cd2SChao Yu }
377184a5cd2SChao Yu 
37839a53e0cSJaegeuk Kim /*
379e9750824SNamjae Jeon  * ioctl commands
380e9750824SNamjae Jeon  */
381e9750824SNamjae Jeon #define F2FS_IOC_GETFLAGS		FS_IOC_GETFLAGS
382e9750824SNamjae Jeon #define F2FS_IOC_SETFLAGS		FS_IOC_SETFLAGS
383d49f3e89SChao Yu #define F2FS_IOC_GETVERSION		FS_IOC_GETVERSION
384e9750824SNamjae Jeon 
38588b88a66SJaegeuk Kim #define F2FS_IOCTL_MAGIC		0xf5
38688b88a66SJaegeuk Kim #define F2FS_IOC_START_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 1)
38788b88a66SJaegeuk Kim #define F2FS_IOC_COMMIT_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 2)
38802a1335fSJaegeuk Kim #define F2FS_IOC_START_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 3)
3891e84371fSJaegeuk Kim #define F2FS_IOC_RELEASE_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 4)
3901e84371fSJaegeuk Kim #define F2FS_IOC_ABORT_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 5)
391d07efb50SJaegeuk Kim #define F2FS_IOC_GARBAGE_COLLECT	_IOW(F2FS_IOCTL_MAGIC, 6, __u32)
392456b88e4SChao Yu #define F2FS_IOC_WRITE_CHECKPOINT	_IO(F2FS_IOCTL_MAGIC, 7)
393d07efb50SJaegeuk Kim #define F2FS_IOC_DEFRAGMENT		_IOWR(F2FS_IOCTL_MAGIC, 8,	\
394d07efb50SJaegeuk Kim 						struct f2fs_defragment)
3954dd6f977SJaegeuk Kim #define F2FS_IOC_MOVE_RANGE		_IOWR(F2FS_IOCTL_MAGIC, 9,	\
3964dd6f977SJaegeuk Kim 						struct f2fs_move_range)
397e066b83cSJaegeuk Kim #define F2FS_IOC_FLUSH_DEVICE		_IOW(F2FS_IOCTL_MAGIC, 10,	\
398e066b83cSJaegeuk Kim 						struct f2fs_flush_device)
39934dc77adSJaegeuk Kim #define F2FS_IOC_GARBAGE_COLLECT_RANGE	_IOW(F2FS_IOCTL_MAGIC, 11,	\
40034dc77adSJaegeuk Kim 						struct f2fs_gc_range)
401e65ef207SJaegeuk Kim #define F2FS_IOC_GET_FEATURES		_IOR(F2FS_IOCTL_MAGIC, 12, __u32)
4021ad71a27SJaegeuk Kim #define F2FS_IOC_SET_PIN_FILE		_IOW(F2FS_IOCTL_MAGIC, 13, __u32)
4031ad71a27SJaegeuk Kim #define F2FS_IOC_GET_PIN_FILE		_IOR(F2FS_IOCTL_MAGIC, 14, __u32)
404c4020b2dSChao Yu #define F2FS_IOC_PRECACHE_EXTENTS	_IO(F2FS_IOCTL_MAGIC, 15)
40588b88a66SJaegeuk Kim 
4060b81d077SJaegeuk Kim #define F2FS_IOC_SET_ENCRYPTION_POLICY	FS_IOC_SET_ENCRYPTION_POLICY
4070b81d077SJaegeuk Kim #define F2FS_IOC_GET_ENCRYPTION_POLICY	FS_IOC_GET_ENCRYPTION_POLICY
4080b81d077SJaegeuk Kim #define F2FS_IOC_GET_ENCRYPTION_PWSALT	FS_IOC_GET_ENCRYPTION_PWSALT
409f424f664SJaegeuk Kim 
4101abff93dSJaegeuk Kim /*
4111abff93dSJaegeuk Kim  * should be same as XFS_IOC_GOINGDOWN.
4121abff93dSJaegeuk Kim  * Flags for going down operation used by FS_IOC_GOINGDOWN
4131abff93dSJaegeuk Kim  */
4141abff93dSJaegeuk Kim #define F2FS_IOC_SHUTDOWN	_IOR('X', 125, __u32)	/* Shutdown */
4151abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_FULLSYNC	0x0	/* going down with full sync */
4161abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_METASYNC	0x1	/* going down with metadata */
4171abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_NOSYNC		0x2	/* going down */
418c912a829SJaegeuk Kim #define F2FS_GOING_DOWN_METAFLUSH	0x3	/* going down with meta flush */
4191abff93dSJaegeuk Kim 
420e9750824SNamjae Jeon #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
421e9750824SNamjae Jeon /*
422e9750824SNamjae Jeon  * ioctl commands in 32 bit emulation
423e9750824SNamjae Jeon  */
424e9750824SNamjae Jeon #define F2FS_IOC32_GETFLAGS		FS_IOC32_GETFLAGS
425e9750824SNamjae Jeon #define F2FS_IOC32_SETFLAGS		FS_IOC32_SETFLAGS
42604ef4b62SChao Yu #define F2FS_IOC32_GETVERSION		FS_IOC32_GETVERSION
427e9750824SNamjae Jeon #endif
428e9750824SNamjae Jeon 
4292c1d0305SChao Yu #define F2FS_IOC_FSGETXATTR		FS_IOC_FSGETXATTR
4302c1d0305SChao Yu #define F2FS_IOC_FSSETXATTR		FS_IOC_FSSETXATTR
4312c1d0305SChao Yu 
43234dc77adSJaegeuk Kim struct f2fs_gc_range {
43334dc77adSJaegeuk Kim 	u32 sync;
43434dc77adSJaegeuk Kim 	u64 start;
43534dc77adSJaegeuk Kim 	u64 len;
43634dc77adSJaegeuk Kim };
43734dc77adSJaegeuk Kim 
438d323d005SChao Yu struct f2fs_defragment {
439d323d005SChao Yu 	u64 start;
440d323d005SChao Yu 	u64 len;
441d323d005SChao Yu };
442d323d005SChao Yu 
4434dd6f977SJaegeuk Kim struct f2fs_move_range {
4444dd6f977SJaegeuk Kim 	u32 dst_fd;		/* destination fd */
4454dd6f977SJaegeuk Kim 	u64 pos_in;		/* start position in src_fd */
4464dd6f977SJaegeuk Kim 	u64 pos_out;		/* start position in dst_fd */
4474dd6f977SJaegeuk Kim 	u64 len;		/* size to move */
4484dd6f977SJaegeuk Kim };
4494dd6f977SJaegeuk Kim 
450e066b83cSJaegeuk Kim struct f2fs_flush_device {
451e066b83cSJaegeuk Kim 	u32 dev_num;		/* device number to flush */
452e066b83cSJaegeuk Kim 	u32 segments;		/* # of segments to flush */
453e066b83cSJaegeuk Kim };
454e066b83cSJaegeuk Kim 
455f2470371SChao Yu /* for inline stuff */
456f2470371SChao Yu #define DEF_INLINE_RESERVED_SIZE	1
4576afc662eSChao Yu #define DEF_MIN_INLINE_SIZE		1
4587a2af766SChao Yu static inline int get_extra_isize(struct inode *inode);
4596afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode);
4607a2af766SChao Yu #define MAX_INLINE_DATA(inode)	(sizeof(__le32) *			\
4617a2af766SChao Yu 				(CUR_ADDRS_PER_INODE(inode) -		\
462b323fd28SChao Yu 				get_inline_xattr_addrs(inode) -	\
4636afc662eSChao Yu 				DEF_INLINE_RESERVED_SIZE))
464f2470371SChao Yu 
465f2470371SChao Yu /* for inline dir */
466f2470371SChao Yu #define NR_INLINE_DENTRY(inode)	(MAX_INLINE_DATA(inode) * BITS_PER_BYTE / \
467f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
468f2470371SChao Yu 				BITS_PER_BYTE + 1))
469f2470371SChao Yu #define INLINE_DENTRY_BITMAP_SIZE(inode)	((NR_INLINE_DENTRY(inode) + \
470f2470371SChao Yu 					BITS_PER_BYTE - 1) / BITS_PER_BYTE)
471f2470371SChao Yu #define INLINE_RESERVED_SIZE(inode)	(MAX_INLINE_DATA(inode) - \
472f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
473f2470371SChao Yu 				NR_INLINE_DENTRY(inode) + \
474f2470371SChao Yu 				INLINE_DENTRY_BITMAP_SIZE(inode)))
475f2470371SChao Yu 
476e9750824SNamjae Jeon /*
47739a53e0cSJaegeuk Kim  * For INODE and NODE manager
47839a53e0cSJaegeuk Kim  */
4797b3cd7d6SJaegeuk Kim /* for directory operations */
4807b3cd7d6SJaegeuk Kim struct f2fs_dentry_ptr {
481d8c6822aSJaegeuk Kim 	struct inode *inode;
48276a9dd85SChao Yu 	void *bitmap;
4837b3cd7d6SJaegeuk Kim 	struct f2fs_dir_entry *dentry;
4847b3cd7d6SJaegeuk Kim 	__u8 (*filename)[F2FS_SLOT_LEN];
4857b3cd7d6SJaegeuk Kim 	int max;
48676a9dd85SChao Yu 	int nr_bitmap;
4877b3cd7d6SJaegeuk Kim };
4887b3cd7d6SJaegeuk Kim 
48964c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_block(struct inode *inode,
49064c24ecbSTomohiro Kusumi 		struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
4917b3cd7d6SJaegeuk Kim {
492d8c6822aSJaegeuk Kim 	d->inode = inode;
4937b3cd7d6SJaegeuk Kim 	d->max = NR_DENTRY_IN_BLOCK;
49476a9dd85SChao Yu 	d->nr_bitmap = SIZE_OF_DENTRY_BITMAP;
495c79d1520SYunlong Song 	d->bitmap = t->dentry_bitmap;
4967b3cd7d6SJaegeuk Kim 	d->dentry = t->dentry;
4977b3cd7d6SJaegeuk Kim 	d->filename = t->filename;
49864c24ecbSTomohiro Kusumi }
499cac5a3d8SDongOh Shin 
50064c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_inline(struct inode *inode,
501f2470371SChao Yu 					struct f2fs_dentry_ptr *d, void *t)
50264c24ecbSTomohiro Kusumi {
503f2470371SChao Yu 	int entry_cnt = NR_INLINE_DENTRY(inode);
504f2470371SChao Yu 	int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(inode);
505f2470371SChao Yu 	int reserved_size = INLINE_RESERVED_SIZE(inode);
506f2470371SChao Yu 
50764c24ecbSTomohiro Kusumi 	d->inode = inode;
508f2470371SChao Yu 	d->max = entry_cnt;
509f2470371SChao Yu 	d->nr_bitmap = bitmap_size;
510f2470371SChao Yu 	d->bitmap = t;
511f2470371SChao Yu 	d->dentry = t + bitmap_size + reserved_size;
512f2470371SChao Yu 	d->filename = t + bitmap_size + reserved_size +
513f2470371SChao Yu 					SIZE_OF_DIR_ENTRY * entry_cnt;
5147b3cd7d6SJaegeuk Kim }
5157b3cd7d6SJaegeuk Kim 
516dbe6a5ffSJaegeuk Kim /*
517dbe6a5ffSJaegeuk Kim  * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
518dbe6a5ffSJaegeuk Kim  * as its node offset to distinguish from index node blocks.
519dbe6a5ffSJaegeuk Kim  * But some bits are used to mark the node block.
52039a53e0cSJaegeuk Kim  */
521dbe6a5ffSJaegeuk Kim #define XATTR_NODE_OFFSET	((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
522dbe6a5ffSJaegeuk Kim 				>> OFFSET_BIT_SHIFT)
523266e97a8SJaegeuk Kim enum {
524266e97a8SJaegeuk Kim 	ALLOC_NODE,			/* allocate a new node page if needed */
525266e97a8SJaegeuk Kim 	LOOKUP_NODE,			/* look up a node without readahead */
526266e97a8SJaegeuk Kim 	LOOKUP_NODE_RA,			/*
527266e97a8SJaegeuk Kim 					 * look up a node with readahead called
5284f4124d0SChao Yu 					 * by get_data_block.
52939a53e0cSJaegeuk Kim 					 */
530266e97a8SJaegeuk Kim };
531266e97a8SJaegeuk Kim 
5327735730dSChao Yu #define DEFAULT_RETRY_IO_COUNT	8	/* maximum retry read IO count */
5337735730dSChao Yu 
534*af033b2aSChao Yu /* maximum retry quota flush count */
535*af033b2aSChao Yu #define DEFAULT_RETRY_QUOTA_FLUSH_COUNT		8
536*af033b2aSChao Yu 
537a6db67f0SJaegeuk Kim #define F2FS_LINK_MAX	0xffffffff	/* maximum link count per file */
53839a53e0cSJaegeuk Kim 
539817202d9SChao Yu #define MAX_DIR_RA_PAGES	4	/* maximum ra pages of dir */
540817202d9SChao Yu 
54139a53e0cSJaegeuk Kim /* for in-memory extent cache entry */
54213054c54SChao Yu #define F2FS_MIN_EXTENT_LEN	64	/* minimum extent length */
54313054c54SChao Yu 
54413054c54SChao Yu /* number of extent info in extent cache we try to shrink */
54513054c54SChao Yu #define EXTENT_CACHE_SHRINK_NUMBER	128
546c11abd1aSJaegeuk Kim 
54754c2258cSChao Yu struct rb_entry {
54854c2258cSChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
54954c2258cSChao Yu 	unsigned int ofs;		/* start offset of the entry */
55054c2258cSChao Yu 	unsigned int len;		/* length of the entry */
55154c2258cSChao Yu };
55254c2258cSChao Yu 
55339a53e0cSJaegeuk Kim struct extent_info {
55439a53e0cSJaegeuk Kim 	unsigned int fofs;		/* start offset in a file */
555111d2495SMasanari Iida 	unsigned int len;		/* length of the extent */
55654c2258cSChao Yu 	u32 blk;			/* start block address of the extent */
55739a53e0cSJaegeuk Kim };
55839a53e0cSJaegeuk Kim 
55913054c54SChao Yu struct extent_node {
56054c2258cSChao Yu 	struct rb_node rb_node;
56154c2258cSChao Yu 	union {
56254c2258cSChao Yu 		struct {
56354c2258cSChao Yu 			unsigned int fofs;
56454c2258cSChao Yu 			unsigned int len;
56554c2258cSChao Yu 			u32 blk;
56654c2258cSChao Yu 		};
56713054c54SChao Yu 		struct extent_info ei;	/* extent info */
56854c2258cSChao Yu 
56954c2258cSChao Yu 	};
57054c2258cSChao Yu 	struct list_head list;		/* node in global extent list of sbi */
571201ef5e0SHou Pengyang 	struct extent_tree *et;		/* extent tree pointer */
57213054c54SChao Yu };
57313054c54SChao Yu 
57413054c54SChao Yu struct extent_tree {
57513054c54SChao Yu 	nid_t ino;			/* inode number */
5764dada3fdSChao Yu 	struct rb_root_cached root;	/* root of extent info rb-tree */
57762c8af65SChao Yu 	struct extent_node *cached_en;	/* recently accessed extent node */
5783e72f721SJaegeuk Kim 	struct extent_info largest;	/* largested extent info */
579137d09f0SJaegeuk Kim 	struct list_head list;		/* to be used by sbi->zombie_list */
58013054c54SChao Yu 	rwlock_t lock;			/* protect extent info rb-tree */
58168e35385SChao Yu 	atomic_t node_cnt;		/* # of extent node in rb-tree*/
582b430f726SZhikang Zhang 	bool largest_updated;		/* largest extent updated */
58313054c54SChao Yu };
58413054c54SChao Yu 
58539a53e0cSJaegeuk Kim /*
586003a3e1dSJaegeuk Kim  * This structure is taken from ext4_map_blocks.
587003a3e1dSJaegeuk Kim  *
588003a3e1dSJaegeuk Kim  * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
589003a3e1dSJaegeuk Kim  */
590003a3e1dSJaegeuk Kim #define F2FS_MAP_NEW		(1 << BH_New)
591003a3e1dSJaegeuk Kim #define F2FS_MAP_MAPPED		(1 << BH_Mapped)
5927f63eb77SJaegeuk Kim #define F2FS_MAP_UNWRITTEN	(1 << BH_Unwritten)
5937f63eb77SJaegeuk Kim #define F2FS_MAP_FLAGS		(F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
5947f63eb77SJaegeuk Kim 				F2FS_MAP_UNWRITTEN)
595003a3e1dSJaegeuk Kim 
596003a3e1dSJaegeuk Kim struct f2fs_map_blocks {
597003a3e1dSJaegeuk Kim 	block_t m_pblk;
598003a3e1dSJaegeuk Kim 	block_t m_lblk;
599003a3e1dSJaegeuk Kim 	unsigned int m_len;
600003a3e1dSJaegeuk Kim 	unsigned int m_flags;
601da85985cSChao Yu 	pgoff_t *m_next_pgofs;		/* point next possible non-hole pgofs */
602c4020b2dSChao Yu 	pgoff_t *m_next_extent;		/* point to next possible extent */
603d5097be5SHyunchul Lee 	int m_seg_type;
604003a3e1dSJaegeuk Kim };
605003a3e1dSJaegeuk Kim 
606e2b4e2bcSChao Yu /* for flag in get_data_block */
607f2220c7fSQiuyang Sun enum {
608f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_DEFAULT,
609f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_FIEMAP,
610f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_BMAP,
6110a4daae5SJaegeuk Kim 	F2FS_GET_BLOCK_DIO,
612f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_DIO,
613f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_AIO,
614c4020b2dSChao Yu 	F2FS_GET_BLOCK_PRECACHE,
615f2220c7fSQiuyang Sun };
616e2b4e2bcSChao Yu 
617003a3e1dSJaegeuk Kim /*
61839a53e0cSJaegeuk Kim  * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
61939a53e0cSJaegeuk Kim  */
62039a53e0cSJaegeuk Kim #define FADVISE_COLD_BIT	0x01
621354a3399SJaegeuk Kim #define FADVISE_LOST_PINO_BIT	0x02
622cde4de12SJaegeuk Kim #define FADVISE_ENCRYPT_BIT	0x04
623e7d55452SJaegeuk Kim #define FADVISE_ENC_NAME_BIT	0x08
62426787236SJaegeuk Kim #define FADVISE_KEEP_SIZE_BIT	0x10
625b6a06cbbSChao Yu #define FADVISE_HOT_BIT		0x20
62653fedcc0SEric Biggers #define FADVISE_VERITY_BIT	0x40	/* reserved */
62739a53e0cSJaegeuk Kim 
628797c1cb5SChao Yu #define FADVISE_MODIFIABLE_BITS	(FADVISE_COLD_BIT | FADVISE_HOT_BIT)
629797c1cb5SChao Yu 
630b5492af7SJaegeuk Kim #define file_is_cold(inode)	is_file(inode, FADVISE_COLD_BIT)
631b5492af7SJaegeuk Kim #define file_wrong_pino(inode)	is_file(inode, FADVISE_LOST_PINO_BIT)
632b5492af7SJaegeuk Kim #define file_set_cold(inode)	set_file(inode, FADVISE_COLD_BIT)
633b5492af7SJaegeuk Kim #define file_lost_pino(inode)	set_file(inode, FADVISE_LOST_PINO_BIT)
634b5492af7SJaegeuk Kim #define file_clear_cold(inode)	clear_file(inode, FADVISE_COLD_BIT)
635b5492af7SJaegeuk Kim #define file_got_pino(inode)	clear_file(inode, FADVISE_LOST_PINO_BIT)
636cde4de12SJaegeuk Kim #define file_is_encrypt(inode)	is_file(inode, FADVISE_ENCRYPT_BIT)
637cde4de12SJaegeuk Kim #define file_set_encrypt(inode)	set_file(inode, FADVISE_ENCRYPT_BIT)
638cde4de12SJaegeuk Kim #define file_clear_encrypt(inode) clear_file(inode, FADVISE_ENCRYPT_BIT)
639e7d55452SJaegeuk Kim #define file_enc_name(inode)	is_file(inode, FADVISE_ENC_NAME_BIT)
640e7d55452SJaegeuk Kim #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
64126787236SJaegeuk Kim #define file_keep_isize(inode)	is_file(inode, FADVISE_KEEP_SIZE_BIT)
64226787236SJaegeuk Kim #define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
643b6a06cbbSChao Yu #define file_is_hot(inode)	is_file(inode, FADVISE_HOT_BIT)
644b6a06cbbSChao Yu #define file_set_hot(inode)	set_file(inode, FADVISE_HOT_BIT)
645b6a06cbbSChao Yu #define file_clear_hot(inode)	clear_file(inode, FADVISE_HOT_BIT)
646cde4de12SJaegeuk Kim 
647ab9fa662SJaegeuk Kim #define DEF_DIR_LEVEL		0
648ab9fa662SJaegeuk Kim 
6492ef79ecbSChao Yu enum {
6502ef79ecbSChao Yu 	GC_FAILURE_PIN,
6512ef79ecbSChao Yu 	GC_FAILURE_ATOMIC,
6522ef79ecbSChao Yu 	MAX_GC_FAILURE
6532ef79ecbSChao Yu };
6542ef79ecbSChao Yu 
65539a53e0cSJaegeuk Kim struct f2fs_inode_info {
65639a53e0cSJaegeuk Kim 	struct inode vfs_inode;		/* serve a vfs inode */
65739a53e0cSJaegeuk Kim 	unsigned long i_flags;		/* keep an inode flags for ioctl */
65839a53e0cSJaegeuk Kim 	unsigned char i_advise;		/* use to give file attribute hints */
65938431545SJaegeuk Kim 	unsigned char i_dir_level;	/* use for dentry level for large dir */
6601ad71a27SJaegeuk Kim 	unsigned int i_current_depth;	/* only for directory depth */
6612ef79ecbSChao Yu 	/* for gc failure statistic */
6622ef79ecbSChao Yu 	unsigned int i_gc_failures[MAX_GC_FAILURE];
6636666e6aaSJaegeuk Kim 	unsigned int i_pino;		/* parent inode number */
66439a53e0cSJaegeuk Kim 	umode_t i_acl_mode;		/* keep file acl mode temporarily */
66539a53e0cSJaegeuk Kim 
66639a53e0cSJaegeuk Kim 	/* Use below internally in f2fs*/
66739a53e0cSJaegeuk Kim 	unsigned long flags;		/* use to pass per-file flags */
668d928bfbfSJaegeuk Kim 	struct rw_semaphore i_sem;	/* protect fi info */
669204706c7SJaegeuk Kim 	atomic_t dirty_pages;		/* # of dirty pages */
67039a53e0cSJaegeuk Kim 	f2fs_hash_t chash;		/* hash value of given file name */
67139a53e0cSJaegeuk Kim 	unsigned int clevel;		/* maximum level of given file name */
67288c5c13aSJaegeuk Kim 	struct task_struct *task;	/* lookup and create consistency */
673b0af6d49SChao Yu 	struct task_struct *cp_task;	/* separate cp/wb IO stats*/
67439a53e0cSJaegeuk Kim 	nid_t i_xattr_nid;		/* node id that contains xattrs */
67526de9b11SJaegeuk Kim 	loff_t	last_disk_size;		/* lastly written file size */
67688b88a66SJaegeuk Kim 
6770abd675eSChao Yu #ifdef CONFIG_QUOTA
6780abd675eSChao Yu 	struct dquot *i_dquot[MAXQUOTAS];
6790abd675eSChao Yu 
6800abd675eSChao Yu 	/* quota space reservation, managed internally by quota code */
6810abd675eSChao Yu 	qsize_t i_reserved_quota;
6820abd675eSChao Yu #endif
6830f18b462SJaegeuk Kim 	struct list_head dirty_list;	/* dirty list for dirs and files */
6840f18b462SJaegeuk Kim 	struct list_head gdirty_list;	/* linked in global dirty list */
68557864ae5SJaegeuk Kim 	struct list_head inmem_ilist;	/* list for inmem inodes */
68688b88a66SJaegeuk Kim 	struct list_head inmem_pages;	/* inmemory pages managed by f2fs */
6877a10f017SJaegeuk Kim 	struct task_struct *inmem_task;	/* store inmemory task */
68888b88a66SJaegeuk Kim 	struct mutex inmem_lock;	/* lock for inmemory pages */
6893e72f721SJaegeuk Kim 	struct extent_tree *extent_tree;	/* cached extent_tree entry */
690b2532c69SChao Yu 
691b2532c69SChao Yu 	/* avoid racing between foreground op and gc */
692b2532c69SChao Yu 	struct rw_semaphore i_gc_rwsem[2];
6935a3a2d83SQiuyang Sun 	struct rw_semaphore i_mmap_sem;
69427161f13SYunlei He 	struct rw_semaphore i_xattr_sem; /* avoid racing between reading and changing EAs */
695f2470371SChao Yu 
6967a2af766SChao Yu 	int i_extra_isize;		/* size of extra space located in i_addr */
6975c57132eSChao Yu 	kprojid_t i_projid;		/* id for project quota */
6986afc662eSChao Yu 	int i_inline_xattr_size;	/* inline xattr size */
69924b81dfcSArnd Bergmann 	struct timespec64 i_crtime;	/* inode creation time */
70024b81dfcSArnd Bergmann 	struct timespec64 i_disk_time[4];/* inode disk times */
70139a53e0cSJaegeuk Kim };
70239a53e0cSJaegeuk Kim 
70339a53e0cSJaegeuk Kim static inline void get_extent_info(struct extent_info *ext,
704bd933d4fSChao Yu 					struct f2fs_extent *i_ext)
70539a53e0cSJaegeuk Kim {
706bd933d4fSChao Yu 	ext->fofs = le32_to_cpu(i_ext->fofs);
707bd933d4fSChao Yu 	ext->blk = le32_to_cpu(i_ext->blk);
708bd933d4fSChao Yu 	ext->len = le32_to_cpu(i_ext->len);
70939a53e0cSJaegeuk Kim }
71039a53e0cSJaegeuk Kim 
71139a53e0cSJaegeuk Kim static inline void set_raw_extent(struct extent_info *ext,
71239a53e0cSJaegeuk Kim 					struct f2fs_extent *i_ext)
71339a53e0cSJaegeuk Kim {
71439a53e0cSJaegeuk Kim 	i_ext->fofs = cpu_to_le32(ext->fofs);
7154d0b0bd4SChao Yu 	i_ext->blk = cpu_to_le32(ext->blk);
71639a53e0cSJaegeuk Kim 	i_ext->len = cpu_to_le32(ext->len);
71739a53e0cSJaegeuk Kim }
71839a53e0cSJaegeuk Kim 
719429511cdSChao Yu static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
720429511cdSChao Yu 						u32 blk, unsigned int len)
721429511cdSChao Yu {
722429511cdSChao Yu 	ei->fofs = fofs;
723429511cdSChao Yu 	ei->blk = blk;
724429511cdSChao Yu 	ei->len = len;
725429511cdSChao Yu }
726429511cdSChao Yu 
727004b6862SChao Yu static inline bool __is_discard_mergeable(struct discard_info *back,
72835ec7d57SChao Yu 			struct discard_info *front, unsigned int max_len)
729004b6862SChao Yu {
7309a997188SJaegeuk Kim 	return (back->lstart + back->len == front->lstart) &&
73135ec7d57SChao Yu 		(back->len + front->len <= max_len);
732004b6862SChao Yu }
733004b6862SChao Yu 
734004b6862SChao Yu static inline bool __is_discard_back_mergeable(struct discard_info *cur,
73535ec7d57SChao Yu 			struct discard_info *back, unsigned int max_len)
736004b6862SChao Yu {
73735ec7d57SChao Yu 	return __is_discard_mergeable(back, cur, max_len);
738004b6862SChao Yu }
739004b6862SChao Yu 
740004b6862SChao Yu static inline bool __is_discard_front_mergeable(struct discard_info *cur,
74135ec7d57SChao Yu 			struct discard_info *front, unsigned int max_len)
742004b6862SChao Yu {
74335ec7d57SChao Yu 	return __is_discard_mergeable(cur, front, max_len);
744004b6862SChao Yu }
745004b6862SChao Yu 
746429511cdSChao Yu static inline bool __is_extent_mergeable(struct extent_info *back,
747429511cdSChao Yu 						struct extent_info *front)
748429511cdSChao Yu {
749429511cdSChao Yu 	return (back->fofs + back->len == front->fofs &&
750429511cdSChao Yu 			back->blk + back->len == front->blk);
751429511cdSChao Yu }
752429511cdSChao Yu 
753429511cdSChao Yu static inline bool __is_back_mergeable(struct extent_info *cur,
754429511cdSChao Yu 						struct extent_info *back)
755429511cdSChao Yu {
756429511cdSChao Yu 	return __is_extent_mergeable(back, cur);
757429511cdSChao Yu }
758429511cdSChao Yu 
759429511cdSChao Yu static inline bool __is_front_mergeable(struct extent_info *cur,
760429511cdSChao Yu 						struct extent_info *front)
761429511cdSChao Yu {
762429511cdSChao Yu 	return __is_extent_mergeable(cur, front);
763429511cdSChao Yu }
764429511cdSChao Yu 
765cac5a3d8SDongOh Shin extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
766b430f726SZhikang Zhang static inline void __try_update_largest_extent(struct extent_tree *et,
767b430f726SZhikang Zhang 						struct extent_node *en)
7684abd3f5aSChao Yu {
769205b9822SJaegeuk Kim 	if (en->ei.len > et->largest.len) {
7704abd3f5aSChao Yu 		et->largest = en->ei;
771b430f726SZhikang Zhang 		et->largest_updated = true;
772205b9822SJaegeuk Kim 	}
7734abd3f5aSChao Yu }
7744abd3f5aSChao Yu 
7759a4ffdf5SChao Yu /*
7769a4ffdf5SChao Yu  * For free nid management
7779a4ffdf5SChao Yu  */
7789a4ffdf5SChao Yu enum nid_state {
7799a4ffdf5SChao Yu 	FREE_NID,		/* newly added to free nid list */
7809a4ffdf5SChao Yu 	PREALLOC_NID,		/* it is preallocated */
7819a4ffdf5SChao Yu 	MAX_NID_STATE,
782b8559dc2SChao Yu };
783b8559dc2SChao Yu 
78439a53e0cSJaegeuk Kim struct f2fs_nm_info {
78539a53e0cSJaegeuk Kim 	block_t nat_blkaddr;		/* base disk address of NAT */
78639a53e0cSJaegeuk Kim 	nid_t max_nid;			/* maximum possible node ids */
78704d47e67SChao Yu 	nid_t available_nids;		/* # of available node ids */
78839a53e0cSJaegeuk Kim 	nid_t next_scan_nid;		/* the next nid to be scanned */
789cdfc41c1SJaegeuk Kim 	unsigned int ram_thresh;	/* control the memory footprint */
790ea1a29a0SChao Yu 	unsigned int ra_nid_pages;	/* # of nid pages to be readaheaded */
7912304cb0cSChao Yu 	unsigned int dirty_nats_ratio;	/* control dirty nats ratio threshold */
79239a53e0cSJaegeuk Kim 
79339a53e0cSJaegeuk Kim 	/* NAT cache management */
79439a53e0cSJaegeuk Kim 	struct radix_tree_root nat_root;/* root of the nat entry cache */
795309cc2b6SJaegeuk Kim 	struct radix_tree_root nat_set_root;/* root of the nat set cache */
796b873b798SJaegeuk Kim 	struct rw_semaphore nat_tree_lock;	/* protect nat_tree_lock */
79739a53e0cSJaegeuk Kim 	struct list_head nat_entries;	/* cached nat entry list (clean) */
79822969158SChao Yu 	spinlock_t nat_list_lock;	/* protect clean nat entry list */
799309cc2b6SJaegeuk Kim 	unsigned int nat_cnt;		/* the # of cached nat entries */
800aec71382SChao Yu 	unsigned int dirty_nat_cnt;	/* total num of nat entries in set */
80122ad0b6aSJaegeuk Kim 	unsigned int nat_blocks;	/* # of nat blocks */
80239a53e0cSJaegeuk Kim 
80339a53e0cSJaegeuk Kim 	/* free node ids management */
8048a7ed66aSJaegeuk Kim 	struct radix_tree_root free_nid_root;/* root of the free_nid cache */
8059a4ffdf5SChao Yu 	struct list_head free_nid_list;		/* list for free nids excluding preallocated nids */
8069a4ffdf5SChao Yu 	unsigned int nid_cnt[MAX_NID_STATE];	/* the number of free node id */
807b8559dc2SChao Yu 	spinlock_t nid_list_lock;	/* protect nid lists ops */
80839a53e0cSJaegeuk Kim 	struct mutex build_lock;	/* lock for build free nids */
809bb1105e4SJaegeuk Kim 	unsigned char **free_nid_bitmap;
8104ac91242SChao Yu 	unsigned char *nat_block_bitmap;
811586d1492SChao Yu 	unsigned short *free_nid_count;	/* free nid count of NAT block */
81239a53e0cSJaegeuk Kim 
81339a53e0cSJaegeuk Kim 	/* for checkpoint */
81439a53e0cSJaegeuk Kim 	char *nat_bitmap;		/* NAT bitmap pointer */
81522ad0b6aSJaegeuk Kim 
81622ad0b6aSJaegeuk Kim 	unsigned int nat_bits_blocks;	/* # of nat bits blocks */
81722ad0b6aSJaegeuk Kim 	unsigned char *nat_bits;	/* NAT bits blocks */
81822ad0b6aSJaegeuk Kim 	unsigned char *full_nat_bits;	/* full NAT pages */
81922ad0b6aSJaegeuk Kim 	unsigned char *empty_nat_bits;	/* empty NAT pages */
820599a09b2SChao Yu #ifdef CONFIG_F2FS_CHECK_FS
821599a09b2SChao Yu 	char *nat_bitmap_mir;		/* NAT bitmap mirror */
822599a09b2SChao Yu #endif
82339a53e0cSJaegeuk Kim 	int bitmap_size;		/* bitmap size */
82439a53e0cSJaegeuk Kim };
82539a53e0cSJaegeuk Kim 
82639a53e0cSJaegeuk Kim /*
82739a53e0cSJaegeuk Kim  * this structure is used as one of function parameters.
82839a53e0cSJaegeuk Kim  * all the information are dedicated to a given direct node block determined
82939a53e0cSJaegeuk Kim  * by the data offset in a file.
83039a53e0cSJaegeuk Kim  */
83139a53e0cSJaegeuk Kim struct dnode_of_data {
83239a53e0cSJaegeuk Kim 	struct inode *inode;		/* vfs inode pointer */
83339a53e0cSJaegeuk Kim 	struct page *inode_page;	/* its inode page, NULL is possible */
83439a53e0cSJaegeuk Kim 	struct page *node_page;		/* cached direct node page */
83539a53e0cSJaegeuk Kim 	nid_t nid;			/* node id of the direct node block */
83639a53e0cSJaegeuk Kim 	unsigned int ofs_in_node;	/* data offset in the node page */
83739a53e0cSJaegeuk Kim 	bool inode_page_locked;		/* inode page is locked or not */
83893bae099SJaegeuk Kim 	bool node_changed;		/* is node block changed */
8393cf45747SChao Yu 	char cur_level;			/* level of hole node page */
8403cf45747SChao Yu 	char max_level;			/* level of current page located */
84139a53e0cSJaegeuk Kim 	block_t	data_blkaddr;		/* block address of the node block */
84239a53e0cSJaegeuk Kim };
84339a53e0cSJaegeuk Kim 
84439a53e0cSJaegeuk Kim static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
84539a53e0cSJaegeuk Kim 		struct page *ipage, struct page *npage, nid_t nid)
84639a53e0cSJaegeuk Kim {
847d66d1f76SJaegeuk Kim 	memset(dn, 0, sizeof(*dn));
84839a53e0cSJaegeuk Kim 	dn->inode = inode;
84939a53e0cSJaegeuk Kim 	dn->inode_page = ipage;
85039a53e0cSJaegeuk Kim 	dn->node_page = npage;
85139a53e0cSJaegeuk Kim 	dn->nid = nid;
85239a53e0cSJaegeuk Kim }
85339a53e0cSJaegeuk Kim 
85439a53e0cSJaegeuk Kim /*
85539a53e0cSJaegeuk Kim  * For SIT manager
85639a53e0cSJaegeuk Kim  *
85739a53e0cSJaegeuk Kim  * By default, there are 6 active log areas across the whole main area.
85839a53e0cSJaegeuk Kim  * When considering hot and cold data separation to reduce cleaning overhead,
85939a53e0cSJaegeuk Kim  * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
86039a53e0cSJaegeuk Kim  * respectively.
86139a53e0cSJaegeuk Kim  * In the current design, you should not change the numbers intentionally.
86239a53e0cSJaegeuk Kim  * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
86339a53e0cSJaegeuk Kim  * logs individually according to the underlying devices. (default: 6)
86439a53e0cSJaegeuk Kim  * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
86539a53e0cSJaegeuk Kim  * data and 8 for node logs.
86639a53e0cSJaegeuk Kim  */
86739a53e0cSJaegeuk Kim #define	NR_CURSEG_DATA_TYPE	(3)
86839a53e0cSJaegeuk Kim #define NR_CURSEG_NODE_TYPE	(3)
86939a53e0cSJaegeuk Kim #define NR_CURSEG_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
87039a53e0cSJaegeuk Kim 
87139a53e0cSJaegeuk Kim enum {
87239a53e0cSJaegeuk Kim 	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
87339a53e0cSJaegeuk Kim 	CURSEG_WARM_DATA,	/* data blocks */
87439a53e0cSJaegeuk Kim 	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
87539a53e0cSJaegeuk Kim 	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
87639a53e0cSJaegeuk Kim 	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
87739a53e0cSJaegeuk Kim 	CURSEG_COLD_NODE,	/* indirect node blocks */
87838aa0889SJaegeuk Kim 	NO_CHECK_TYPE,
87939a53e0cSJaegeuk Kim };
88039a53e0cSJaegeuk Kim 
8816b4afdd7SJaegeuk Kim struct flush_cmd {
8826b4afdd7SJaegeuk Kim 	struct completion wait;
883721bd4d5SGu Zheng 	struct llist_node llnode;
88439d787beSChao Yu 	nid_t ino;
8856b4afdd7SJaegeuk Kim 	int ret;
8866b4afdd7SJaegeuk Kim };
8876b4afdd7SJaegeuk Kim 
888a688b9d9SGu Zheng struct flush_cmd_control {
889a688b9d9SGu Zheng 	struct task_struct *f2fs_issue_flush;	/* flush thread */
890a688b9d9SGu Zheng 	wait_queue_head_t flush_wait_queue;	/* waiting queue for wake-up */
8918b8dd65fSChao Yu 	atomic_t issued_flush;			/* # of issued flushes */
8928b8dd65fSChao Yu 	atomic_t issing_flush;			/* # of issing flushes */
893721bd4d5SGu Zheng 	struct llist_head issue_list;		/* list for command issue */
894721bd4d5SGu Zheng 	struct llist_node *dispatch_list;	/* list for command dispatch */
895a688b9d9SGu Zheng };
896a688b9d9SGu Zheng 
89739a53e0cSJaegeuk Kim struct f2fs_sm_info {
89839a53e0cSJaegeuk Kim 	struct sit_info *sit_info;		/* whole segment information */
89939a53e0cSJaegeuk Kim 	struct free_segmap_info *free_info;	/* free segment information */
90039a53e0cSJaegeuk Kim 	struct dirty_seglist_info *dirty_info;	/* dirty segment information */
90139a53e0cSJaegeuk Kim 	struct curseg_info *curseg_array;	/* active segment information */
90239a53e0cSJaegeuk Kim 
9032b60311dSChao Yu 	struct rw_semaphore curseg_lock;	/* for preventing curseg change */
9042b60311dSChao Yu 
90539a53e0cSJaegeuk Kim 	block_t seg0_blkaddr;		/* block address of 0'th segment */
90639a53e0cSJaegeuk Kim 	block_t main_blkaddr;		/* start block address of main area */
90739a53e0cSJaegeuk Kim 	block_t ssa_blkaddr;		/* start block address of SSA area */
90839a53e0cSJaegeuk Kim 
90939a53e0cSJaegeuk Kim 	unsigned int segment_count;	/* total # of segments */
91039a53e0cSJaegeuk Kim 	unsigned int main_segments;	/* # of segments in main area */
91139a53e0cSJaegeuk Kim 	unsigned int reserved_segments;	/* # of reserved segments */
91239a53e0cSJaegeuk Kim 	unsigned int ovp_segments;	/* # of overprovision segments */
91381eb8d6eSJaegeuk Kim 
91481eb8d6eSJaegeuk Kim 	/* a threshold to reclaim prefree segments */
91581eb8d6eSJaegeuk Kim 	unsigned int rec_prefree_segments;
9167fd9e544SJaegeuk Kim 
917bba681cbSJaegeuk Kim 	/* for batched trimming */
918bba681cbSJaegeuk Kim 	unsigned int trim_sections;		/* # of sections to trim */
919bba681cbSJaegeuk Kim 
920184a5cd2SChao Yu 	struct list_head sit_entry_set;	/* sit entry set list */
921184a5cd2SChao Yu 
922216fbd64SJaegeuk Kim 	unsigned int ipu_policy;	/* in-place-update policy */
923216fbd64SJaegeuk Kim 	unsigned int min_ipu_util;	/* in-place-update threshold */
924c1ce1b02SJaegeuk Kim 	unsigned int min_fsync_blocks;	/* threshold for fsync */
925853137ceSJaegeuk Kim 	unsigned int min_seq_blocks;	/* threshold for sequential blocks */
926ef095d19SJaegeuk Kim 	unsigned int min_hot_blocks;	/* threshold for hot block allocation */
927a2a12b67SChao Yu 	unsigned int min_ssr_sections;	/* threshold to trigger SSR allocation */
9286b4afdd7SJaegeuk Kim 
9296b4afdd7SJaegeuk Kim 	/* for flush command control */
930b01a9201SJaegeuk Kim 	struct flush_cmd_control *fcc_info;
931a688b9d9SGu Zheng 
9320b54fb84SJaegeuk Kim 	/* for discard command control */
9330b54fb84SJaegeuk Kim 	struct discard_cmd_control *dcc_info;
93439a53e0cSJaegeuk Kim };
93539a53e0cSJaegeuk Kim 
93639a53e0cSJaegeuk Kim /*
93739a53e0cSJaegeuk Kim  * For superblock
93839a53e0cSJaegeuk Kim  */
93939a53e0cSJaegeuk Kim /*
94039a53e0cSJaegeuk Kim  * COUNT_TYPE for monitoring
94139a53e0cSJaegeuk Kim  *
94239a53e0cSJaegeuk Kim  * f2fs monitors the number of several block types such as on-writeback,
94339a53e0cSJaegeuk Kim  * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
94439a53e0cSJaegeuk Kim  */
94536951b38SChao Yu #define WB_DATA_TYPE(p)	(__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
94639a53e0cSJaegeuk Kim enum count_type {
94739a53e0cSJaegeuk Kim 	F2FS_DIRTY_DENTS,
948c227f912SChao Yu 	F2FS_DIRTY_DATA,
9492c8a4a28SJaegeuk Kim 	F2FS_DIRTY_QDATA,
95039a53e0cSJaegeuk Kim 	F2FS_DIRTY_NODES,
95139a53e0cSJaegeuk Kim 	F2FS_DIRTY_META,
9528dcf2ff7SJaegeuk Kim 	F2FS_INMEM_PAGES,
9530f18b462SJaegeuk Kim 	F2FS_DIRTY_IMETA,
95436951b38SChao Yu 	F2FS_WB_CP_DATA,
95536951b38SChao Yu 	F2FS_WB_DATA,
9565f9abab4SJaegeuk Kim 	F2FS_RD_DATA,
9575f9abab4SJaegeuk Kim 	F2FS_RD_NODE,
9585f9abab4SJaegeuk Kim 	F2FS_RD_META,
95939a53e0cSJaegeuk Kim 	NR_COUNT_TYPE,
96039a53e0cSJaegeuk Kim };
96139a53e0cSJaegeuk Kim 
96239a53e0cSJaegeuk Kim /*
963e1c42045Sarter97  * The below are the page types of bios used in submit_bio().
96439a53e0cSJaegeuk Kim  * The available types are:
96539a53e0cSJaegeuk Kim  * DATA			User data pages. It operates as async mode.
96639a53e0cSJaegeuk Kim  * NODE			Node pages. It operates as async mode.
96739a53e0cSJaegeuk Kim  * META			FS metadata pages such as SIT, NAT, CP.
96839a53e0cSJaegeuk Kim  * NR_PAGE_TYPE		The number of page types.
96939a53e0cSJaegeuk Kim  * META_FLUSH		Make sure the previous pages are written
97039a53e0cSJaegeuk Kim  *			with waiting the bio's completion
97139a53e0cSJaegeuk Kim  * ...			Only can be used with META.
97239a53e0cSJaegeuk Kim  */
9737d5e5109SJaegeuk Kim #define PAGE_TYPE_OF_BIO(type)	((type) > META ? META : (type))
97439a53e0cSJaegeuk Kim enum page_type {
97539a53e0cSJaegeuk Kim 	DATA,
97639a53e0cSJaegeuk Kim 	NODE,
97739a53e0cSJaegeuk Kim 	META,
97839a53e0cSJaegeuk Kim 	NR_PAGE_TYPE,
97939a53e0cSJaegeuk Kim 	META_FLUSH,
9808ce67cb0SJaegeuk Kim 	INMEM,		/* the below types are used by tracepoints only. */
9818ce67cb0SJaegeuk Kim 	INMEM_DROP,
9828c242db9SJaegeuk Kim 	INMEM_INVALIDATE,
98328bc106bSChao Yu 	INMEM_REVOKE,
9848ce67cb0SJaegeuk Kim 	IPU,
9858ce67cb0SJaegeuk Kim 	OPU,
98639a53e0cSJaegeuk Kim };
98739a53e0cSJaegeuk Kim 
988a912b54dSJaegeuk Kim enum temp_type {
989a912b54dSJaegeuk Kim 	HOT = 0,	/* must be zero for meta bio */
990a912b54dSJaegeuk Kim 	WARM,
991a912b54dSJaegeuk Kim 	COLD,
992a912b54dSJaegeuk Kim 	NR_TEMP_TYPE,
993a912b54dSJaegeuk Kim };
994a912b54dSJaegeuk Kim 
995cc15620bSJaegeuk Kim enum need_lock_type {
996cc15620bSJaegeuk Kim 	LOCK_REQ = 0,
997cc15620bSJaegeuk Kim 	LOCK_DONE,
998cc15620bSJaegeuk Kim 	LOCK_RETRY,
999cc15620bSJaegeuk Kim };
1000cc15620bSJaegeuk Kim 
1001a5fd5050SChao Yu enum cp_reason_type {
1002a5fd5050SChao Yu 	CP_NO_NEEDED,
1003a5fd5050SChao Yu 	CP_NON_REGULAR,
1004a5fd5050SChao Yu 	CP_HARDLINK,
1005a5fd5050SChao Yu 	CP_SB_NEED_CP,
1006a5fd5050SChao Yu 	CP_WRONG_PINO,
1007a5fd5050SChao Yu 	CP_NO_SPC_ROLL,
1008a5fd5050SChao Yu 	CP_NODE_NEED_CP,
1009a5fd5050SChao Yu 	CP_FASTBOOT_MODE,
1010a5fd5050SChao Yu 	CP_SPEC_LOG_NUM,
10110a007b97SJaegeuk Kim 	CP_RECOVER_DIR,
1012a5fd5050SChao Yu };
1013a5fd5050SChao Yu 
1014b0af6d49SChao Yu enum iostat_type {
1015b0af6d49SChao Yu 	APP_DIRECT_IO,			/* app direct IOs */
1016b0af6d49SChao Yu 	APP_BUFFERED_IO,		/* app buffered IOs */
1017b0af6d49SChao Yu 	APP_WRITE_IO,			/* app write IOs */
1018b0af6d49SChao Yu 	APP_MAPPED_IO,			/* app mapped IOs */
1019b0af6d49SChao Yu 	FS_DATA_IO,			/* data IOs from kworker/fsync/reclaimer */
1020b0af6d49SChao Yu 	FS_NODE_IO,			/* node IOs from kworker/fsync/reclaimer */
1021b0af6d49SChao Yu 	FS_META_IO,			/* meta IOs from kworker/reclaimer */
1022b0af6d49SChao Yu 	FS_GC_DATA_IO,			/* data IOs from forground gc */
1023b0af6d49SChao Yu 	FS_GC_NODE_IO,			/* node IOs from forground gc */
1024b0af6d49SChao Yu 	FS_CP_DATA_IO,			/* data IOs from checkpoint */
1025b0af6d49SChao Yu 	FS_CP_NODE_IO,			/* node IOs from checkpoint */
1026b0af6d49SChao Yu 	FS_CP_META_IO,			/* meta IOs from checkpoint */
1027b0af6d49SChao Yu 	FS_DISCARD,			/* discard */
1028b0af6d49SChao Yu 	NR_IO_TYPE,
1029b0af6d49SChao Yu };
1030b0af6d49SChao Yu 
1031458e6197SJaegeuk Kim struct f2fs_io_info {
103205ca3632SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs_sb_info pointer */
103339d787beSChao Yu 	nid_t ino;		/* inode number */
1034458e6197SJaegeuk Kim 	enum page_type type;	/* contains DATA/NODE/META/META_FLUSH */
1035a912b54dSJaegeuk Kim 	enum temp_type temp;	/* contains HOT/WARM/COLD */
103604d328deSMike Christie 	int op;			/* contains REQ_OP_ */
1037ef295ecfSChristoph Hellwig 	int op_flags;		/* req_flag_bits */
10387a9d7548SChao Yu 	block_t new_blkaddr;	/* new block address to be written */
103928bc106bSChao Yu 	block_t old_blkaddr;	/* old block address before Cow */
104005ca3632SJaegeuk Kim 	struct page *page;	/* page to be written */
10414375a336SJaegeuk Kim 	struct page *encrypted_page;	/* encrypted page */
1042fb830fc5SChao Yu 	struct list_head list;		/* serialize IOs */
1043d68f735bSJaegeuk Kim 	bool submitted;		/* indicate IO submission */
1044cc15620bSJaegeuk Kim 	int need_lock;		/* indicate we need to lock cp_rwsem */
1045fb830fc5SChao Yu 	bool in_list;		/* indicate fio is in io_list */
10460833721eSYunlei He 	bool is_meta;		/* indicate borrow meta inode mapping or not */
1047fe16efe6SChao Yu 	bool retry;		/* need to reallocate block address */
1048b0af6d49SChao Yu 	enum iostat_type io_type;	/* io type */
1049578c6478SYufen Yu 	struct writeback_control *io_wbc; /* writeback control */
10507735730dSChao Yu 	unsigned char version;		/* version of the node */
1051458e6197SJaegeuk Kim };
1052458e6197SJaegeuk Kim 
105368afcf2dSTomohiro Kusumi #define is_read_io(rw) ((rw) == READ)
10541ff7bd3bSJaegeuk Kim struct f2fs_bio_info {
1055458e6197SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs superblock */
10561ff7bd3bSJaegeuk Kim 	struct bio *bio;		/* bios to merge */
10571ff7bd3bSJaegeuk Kim 	sector_t last_block_in_bio;	/* last block number */
1058458e6197SJaegeuk Kim 	struct f2fs_io_info fio;	/* store buffered io info. */
1059df0f8dc0SChao Yu 	struct rw_semaphore io_rwsem;	/* blocking op for bio */
1060fb830fc5SChao Yu 	spinlock_t io_lock;		/* serialize DATA/NODE IOs */
1061fb830fc5SChao Yu 	struct list_head io_list;	/* track fios */
10621ff7bd3bSJaegeuk Kim };
10631ff7bd3bSJaegeuk Kim 
10643c62be17SJaegeuk Kim #define FDEV(i)				(sbi->devs[i])
10653c62be17SJaegeuk Kim #define RDEV(i)				(raw_super->devs[i])
10663c62be17SJaegeuk Kim struct f2fs_dev_info {
10673c62be17SJaegeuk Kim 	struct block_device *bdev;
10683c62be17SJaegeuk Kim 	char path[MAX_PATH_LEN];
10693c62be17SJaegeuk Kim 	unsigned int total_segments;
10703c62be17SJaegeuk Kim 	block_t start_blk;
10713c62be17SJaegeuk Kim 	block_t end_blk;
10723c62be17SJaegeuk Kim #ifdef CONFIG_BLK_DEV_ZONED
10733c62be17SJaegeuk Kim 	unsigned int nr_blkz;			/* Total number of zones */
10743c62be17SJaegeuk Kim 	u8 *blkz_type;				/* Array of zones type */
10753c62be17SJaegeuk Kim #endif
10763c62be17SJaegeuk Kim };
10773c62be17SJaegeuk Kim 
1078c227f912SChao Yu enum inode_type {
1079c227f912SChao Yu 	DIR_INODE,			/* for dirty dir inode */
1080c227f912SChao Yu 	FILE_INODE,			/* for dirty regular/symlink inode */
10810f18b462SJaegeuk Kim 	DIRTY_META,			/* for all dirtied inode metadata */
108257864ae5SJaegeuk Kim 	ATOMIC_FILE,			/* for all atomic files */
1083c227f912SChao Yu 	NR_INODE_TYPE,
1084c227f912SChao Yu };
1085c227f912SChao Yu 
108667298804SChao Yu /* for inner inode cache management */
108767298804SChao Yu struct inode_management {
108867298804SChao Yu 	struct radix_tree_root ino_root;	/* ino entry array */
108967298804SChao Yu 	spinlock_t ino_lock;			/* for ino entry lock */
109067298804SChao Yu 	struct list_head ino_list;		/* inode list head */
109167298804SChao Yu 	unsigned long ino_num;			/* number of entries */
109267298804SChao Yu };
109367298804SChao Yu 
1094caf0047eSChao Yu /* For s_flag in struct f2fs_sb_info */
1095caf0047eSChao Yu enum {
1096caf0047eSChao Yu 	SBI_IS_DIRTY,				/* dirty flag for checkpoint */
1097caf0047eSChao Yu 	SBI_IS_CLOSE,				/* specify unmounting */
1098caf0047eSChao Yu 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
1099caf0047eSChao Yu 	SBI_POR_DOING,				/* recovery is doing or not */
1100df728b0fSJaegeuk Kim 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
1101bbf156f7SJaegeuk Kim 	SBI_NEED_CP,				/* need to checkpoint */
110283a3bfdbSJaegeuk Kim 	SBI_IS_SHUTDOWN,			/* shutdown by ioctl */
11031378752bSChao Yu 	SBI_IS_RECOVERED,			/* recovered orphan/data */
11044354994fSDaniel Rosenberg 	SBI_CP_DISABLED,			/* CP was disabled last mount */
1105*af033b2aSChao Yu 	SBI_QUOTA_NEED_FLUSH,			/* need to flush quota info in CP */
1106*af033b2aSChao Yu 	SBI_QUOTA_SKIP_FLUSH,			/* skip flushing quota in current CP */
1107*af033b2aSChao Yu 	SBI_QUOTA_NEED_REPAIR,			/* quota file may be corrupted */
1108caf0047eSChao Yu };
1109caf0047eSChao Yu 
11106beceb54SJaegeuk Kim enum {
11116beceb54SJaegeuk Kim 	CP_TIME,
1112d0239e1bSJaegeuk Kim 	REQ_TIME,
1113a7d10cf3SSahitya Tummala 	DISCARD_TIME,
1114a7d10cf3SSahitya Tummala 	GC_TIME,
11154354994fSDaniel Rosenberg 	DISABLE_TIME,
11166beceb54SJaegeuk Kim 	MAX_TIME,
11176beceb54SJaegeuk Kim };
11186beceb54SJaegeuk Kim 
11190cdd3195SHyunchul Lee enum {
11205b0e9539SJaegeuk Kim 	GC_NORMAL,
11215b0e9539SJaegeuk Kim 	GC_IDLE_CB,
11225b0e9539SJaegeuk Kim 	GC_IDLE_GREEDY,
11235b0e9539SJaegeuk Kim 	GC_URGENT,
11245b0e9539SJaegeuk Kim };
11255b0e9539SJaegeuk Kim 
11265b0e9539SJaegeuk Kim enum {
11270cdd3195SHyunchul Lee 	WHINT_MODE_OFF,		/* not pass down write hints */
11280cdd3195SHyunchul Lee 	WHINT_MODE_USER,	/* try to pass down hints given by users */
1129f2e703f9SHyunchul Lee 	WHINT_MODE_FS,		/* pass down hints with F2FS policy */
11300cdd3195SHyunchul Lee };
11310cdd3195SHyunchul Lee 
113207939627SJaegeuk Kim enum {
113307939627SJaegeuk Kim 	ALLOC_MODE_DEFAULT,	/* stay default */
113407939627SJaegeuk Kim 	ALLOC_MODE_REUSE,	/* reuse segments as much as possible */
113507939627SJaegeuk Kim };
113607939627SJaegeuk Kim 
113793cf93f1SJunling Zheng enum fsync_mode {
113893cf93f1SJunling Zheng 	FSYNC_MODE_POSIX,	/* fsync follows posix semantics */
113993cf93f1SJunling Zheng 	FSYNC_MODE_STRICT,	/* fsync behaves in line with ext4 */
1140d6290814SJaegeuk Kim 	FSYNC_MODE_NOBARRIER,	/* fsync behaves nobarrier based on posix */
114193cf93f1SJunling Zheng };
114293cf93f1SJunling Zheng 
1143ff62af20SSheng Yong #ifdef CONFIG_F2FS_FS_ENCRYPTION
1144ff62af20SSheng Yong #define DUMMY_ENCRYPTION_ENABLED(sbi) \
1145ff62af20SSheng Yong 			(unlikely(F2FS_OPTION(sbi).test_dummy_encryption))
1146ff62af20SSheng Yong #else
1147ff62af20SSheng Yong #define DUMMY_ENCRYPTION_ENABLED(sbi) (0)
1148ff62af20SSheng Yong #endif
1149ff62af20SSheng Yong 
115039a53e0cSJaegeuk Kim struct f2fs_sb_info {
115139a53e0cSJaegeuk Kim 	struct super_block *sb;			/* pointer to VFS super block */
11525e176d54SJaegeuk Kim 	struct proc_dir_entry *s_proc;		/* proc entry */
115339a53e0cSJaegeuk Kim 	struct f2fs_super_block *raw_super;	/* raw super block pointer */
1154846ae671SChao Yu 	struct rw_semaphore sb_lock;		/* lock for raw super block */
1155e8240f65SChao Yu 	int valid_super_block;			/* valid super block no */
1156fadb2fb8SChao Yu 	unsigned long s_flag;				/* flags for sbi */
1157853137ceSJaegeuk Kim 	struct mutex writepages;		/* mutex for writepages() */
115839a53e0cSJaegeuk Kim 
1159178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
1160178053e2SDamien Le Moal 	unsigned int blocks_per_blkz;		/* F2FS blocks per zone */
1161178053e2SDamien Le Moal 	unsigned int log_blocks_per_blkz;	/* log2 F2FS blocks per zone */
1162178053e2SDamien Le Moal #endif
1163178053e2SDamien Le Moal 
116439a53e0cSJaegeuk Kim 	/* for node-related operations */
116539a53e0cSJaegeuk Kim 	struct f2fs_nm_info *nm_info;		/* node manager */
116639a53e0cSJaegeuk Kim 	struct inode *node_inode;		/* cache node blocks */
116739a53e0cSJaegeuk Kim 
116839a53e0cSJaegeuk Kim 	/* for segment-related operations */
116939a53e0cSJaegeuk Kim 	struct f2fs_sm_info *sm_info;		/* segment manager */
11701ff7bd3bSJaegeuk Kim 
11711ff7bd3bSJaegeuk Kim 	/* for bio operations */
1172a912b54dSJaegeuk Kim 	struct f2fs_bio_info *write_io[NR_PAGE_TYPE];	/* for write bios */
1173e41e6d75SChao Yu 	struct mutex wio_mutex[NR_PAGE_TYPE - 1][NR_TEMP_TYPE];
1174e41e6d75SChao Yu 						/* bio ordering for NODE/DATA */
1175107a805dSChao Yu 	/* keep migration IO order for LFS mode */
1176107a805dSChao Yu 	struct rw_semaphore io_order_lock;
11770a595ebaSJaegeuk Kim 	mempool_t *write_io_dummy;		/* Dummy pages */
117839a53e0cSJaegeuk Kim 
117939a53e0cSJaegeuk Kim 	/* for checkpoint */
118039a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt;		/* raw checkpoint pointer */
11818508e44aSJaegeuk Kim 	int cur_cp_pack;			/* remain current cp pack */
1182aaec2b1dSChao Yu 	spinlock_t cp_lock;			/* for flag in ckpt */
118339a53e0cSJaegeuk Kim 	struct inode *meta_inode;		/* cache meta blocks */
118439936837SJaegeuk Kim 	struct mutex cp_mutex;			/* checkpoint procedure lock */
1185b873b798SJaegeuk Kim 	struct rw_semaphore cp_rwsem;		/* blocking FS operations */
1186b3582c68SChao Yu 	struct rw_semaphore node_write;		/* locking node writes */
118759c9081bSYunlei He 	struct rw_semaphore node_change;	/* locking node change */
1188fb51b5efSChangman Lee 	wait_queue_head_t cp_wait;
11896beceb54SJaegeuk Kim 	unsigned long last_time[MAX_TIME];	/* to store time in jiffies */
11906beceb54SJaegeuk Kim 	long interval_time[MAX_TIME];		/* to store thresholds */
119139a53e0cSJaegeuk Kim 
119267298804SChao Yu 	struct inode_management im[MAX_INO_ENTRY];      /* manage inode cache */
11936451e041SJaegeuk Kim 
119450fa53ecSChao Yu 	spinlock_t fsync_node_lock;		/* for node entry lock */
119550fa53ecSChao Yu 	struct list_head fsync_node_list;	/* node list head */
119650fa53ecSChao Yu 	unsigned int fsync_seg_id;		/* sequence id */
119750fa53ecSChao Yu 	unsigned int fsync_node_num;		/* number of node entries */
119850fa53ecSChao Yu 
11996451e041SJaegeuk Kim 	/* for orphan inode, use 0'th array */
12000d47c1adSGu Zheng 	unsigned int max_orphans;		/* max orphan inodes */
120139a53e0cSJaegeuk Kim 
1202c227f912SChao Yu 	/* for inode management */
1203c227f912SChao Yu 	struct list_head inode_list[NR_INODE_TYPE];	/* dirty inode list */
1204c227f912SChao Yu 	spinlock_t inode_lock[NR_INODE_TYPE];	/* for dirty inode list lock */
120539a53e0cSJaegeuk Kim 
120613054c54SChao Yu 	/* for extent tree cache */
120713054c54SChao Yu 	struct radix_tree_root extent_tree_root;/* cache extent cache entries */
12085e8256acSYunlei He 	struct mutex extent_tree_lock;	/* locking extent radix tree */
120913054c54SChao Yu 	struct list_head extent_list;		/* lru list for shrinker */
121013054c54SChao Yu 	spinlock_t extent_lock;			/* locking extent lru list */
12117441ccefSJaegeuk Kim 	atomic_t total_ext_tree;		/* extent tree count */
1212137d09f0SJaegeuk Kim 	struct list_head zombie_list;		/* extent zombie tree list */
121374fd8d99SJaegeuk Kim 	atomic_t total_zombie_tree;		/* extent zombie tree count */
121413054c54SChao Yu 	atomic_t total_ext_node;		/* extent info count */
121513054c54SChao Yu 
121639a53e0cSJaegeuk Kim 	/* basic filesystem units */
121739a53e0cSJaegeuk Kim 	unsigned int log_sectors_per_block;	/* log2 sectors per block */
121839a53e0cSJaegeuk Kim 	unsigned int log_blocksize;		/* log2 block size */
121939a53e0cSJaegeuk Kim 	unsigned int blocksize;			/* block size */
122039a53e0cSJaegeuk Kim 	unsigned int root_ino_num;		/* root inode number*/
122139a53e0cSJaegeuk Kim 	unsigned int node_ino_num;		/* node inode number*/
122239a53e0cSJaegeuk Kim 	unsigned int meta_ino_num;		/* meta inode number*/
122339a53e0cSJaegeuk Kim 	unsigned int log_blocks_per_seg;	/* log2 blocks per segment */
122439a53e0cSJaegeuk Kim 	unsigned int blocks_per_seg;		/* blocks per segment */
122539a53e0cSJaegeuk Kim 	unsigned int segs_per_sec;		/* segments per section */
122639a53e0cSJaegeuk Kim 	unsigned int secs_per_zone;		/* sections per zone */
122739a53e0cSJaegeuk Kim 	unsigned int total_sections;		/* total section count */
122839a53e0cSJaegeuk Kim 	unsigned int total_node_count;		/* total node block count */
122939a53e0cSJaegeuk Kim 	unsigned int total_valid_node_count;	/* valid node block count */
1230e0afc4d6SChao Yu 	loff_t max_file_blocks;			/* max block index of file */
1231ab9fa662SJaegeuk Kim 	int dir_level;				/* directory level */
1232f6df8f23SSheng Yong 	int readdir_ra;				/* readahead inode in readdir */
123339a53e0cSJaegeuk Kim 
123439a53e0cSJaegeuk Kim 	block_t user_block_count;		/* # of user blocks */
123539a53e0cSJaegeuk Kim 	block_t total_valid_block_count;	/* # of valid blocks */
1236a66cdd98SJaegeuk Kim 	block_t discard_blks;			/* discard command candidats */
123739a53e0cSJaegeuk Kim 	block_t last_valid_block_count;		/* for recovery */
1238daeb433eSChao Yu 	block_t reserved_blocks;		/* configurable reserved blocks */
123980d42145SYunlong Song 	block_t current_reserved_blocks;	/* current reserved blocks */
1240daeb433eSChao Yu 
12414354994fSDaniel Rosenberg 	/* Additional tracking for no checkpoint mode */
12424354994fSDaniel Rosenberg 	block_t unusable_block_count;		/* # of blocks saved by last cp */
12434354994fSDaniel Rosenberg 
1244292c196aSChao Yu 	unsigned int nquota_files;		/* # of quota sysfile */
1245292c196aSChao Yu 
124639a53e0cSJaegeuk Kim 	u32 s_next_generation;			/* for NFS support */
1247523be8a6SJaegeuk Kim 
1248523be8a6SJaegeuk Kim 	/* # of pages, see count_type */
124935782b23SJaegeuk Kim 	atomic_t nr_pages[NR_COUNT_TYPE];
125041382ec4SJaegeuk Kim 	/* # of allocated blocks */
125141382ec4SJaegeuk Kim 	struct percpu_counter alloc_valid_block_count;
125239a53e0cSJaegeuk Kim 
1253687de7f1SJaegeuk Kim 	/* writeback control */
1254c29fd0c0SChao Yu 	atomic_t wb_sync_req[META];	/* count # of WB_SYNC threads */
1255687de7f1SJaegeuk Kim 
1256513c5f37SJaegeuk Kim 	/* valid inode count */
1257513c5f37SJaegeuk Kim 	struct percpu_counter total_valid_inode_count;
1258513c5f37SJaegeuk Kim 
125939a53e0cSJaegeuk Kim 	struct f2fs_mount_info mount_opt;	/* mount options */
126039a53e0cSJaegeuk Kim 
126139a53e0cSJaegeuk Kim 	/* for cleaning operations */
126239a53e0cSJaegeuk Kim 	struct mutex gc_mutex;			/* mutex for GC */
126339a53e0cSJaegeuk Kim 	struct f2fs_gc_kthread	*gc_thread;	/* GC thread */
12645ec4e49fSJaegeuk Kim 	unsigned int cur_victim_sec;		/* current victim section num */
12655b0e9539SJaegeuk Kim 	unsigned int gc_mode;			/* current GC state */
12662ef79ecbSChao Yu 	/* for skip statistic */
12672ef79ecbSChao Yu 	unsigned long long skipped_atomic_files[2];	/* FG_GC and BG_GC */
12686f8d4455SJaegeuk Kim 	unsigned long long skipped_gc_rwsem;		/* FG_GC only */
126939a53e0cSJaegeuk Kim 
12701ad71a27SJaegeuk Kim 	/* threshold for gc trials on pinned files */
12711ad71a27SJaegeuk Kim 	u64 gc_pin_file_threshold;
12721ad71a27SJaegeuk Kim 
1273b1c57c1cSJaegeuk Kim 	/* maximum # of trials to find a victim segment for SSR and GC */
1274b1c57c1cSJaegeuk Kim 	unsigned int max_victim_search;
1275b1c57c1cSJaegeuk Kim 
127639a53e0cSJaegeuk Kim 	/*
127739a53e0cSJaegeuk Kim 	 * for stat information.
127839a53e0cSJaegeuk Kim 	 * one is for the LFS mode, and the other is for the SSR mode.
127939a53e0cSJaegeuk Kim 	 */
128035b09d82SNamjae Jeon #ifdef CONFIG_F2FS_STAT_FS
128139a53e0cSJaegeuk Kim 	struct f2fs_stat_info *stat_info;	/* FS status information */
1282b63e7be5SChao Yu 	atomic_t meta_count[META_MAX];		/* # of meta blocks */
128339a53e0cSJaegeuk Kim 	unsigned int segment_count[2];		/* # of allocated segments */
128439a53e0cSJaegeuk Kim 	unsigned int block_count[2];		/* # of allocated blocks */
1285b9a2c252SChangman Lee 	atomic_t inplace_count;		/* # of inplace update */
12865b7ee374SChao Yu 	atomic64_t total_hit_ext;		/* # of lookup extent cache */
12875b7ee374SChao Yu 	atomic64_t read_hit_rbtree;		/* # of hit rbtree extent node */
12885b7ee374SChao Yu 	atomic64_t read_hit_largest;		/* # of hit largest extent node */
12895b7ee374SChao Yu 	atomic64_t read_hit_cached;		/* # of hit cached extent node */
1290d5e8f6c9SChao Yu 	atomic_t inline_xattr;			/* # of inline_xattr inodes */
129103e14d52SChao Yu 	atomic_t inline_inode;			/* # of inline_data inodes */
129203e14d52SChao Yu 	atomic_t inline_dir;			/* # of inline_dentry inodes */
129326a28a0cSJaegeuk Kim 	atomic_t aw_cnt;			/* # of atomic writes */
1294648d50baSChao Yu 	atomic_t vw_cnt;			/* # of volatile writes */
129526a28a0cSJaegeuk Kim 	atomic_t max_aw_cnt;			/* max # of atomic writes */
1296648d50baSChao Yu 	atomic_t max_vw_cnt;			/* max # of volatile writes */
129739a53e0cSJaegeuk Kim 	int bg_gc;				/* background gc calls */
1298274bd9baSChao Yu 	unsigned int io_skip_bggc;		/* skip background gc for in-flight IO */
1299274bd9baSChao Yu 	unsigned int other_skip_bggc;		/* skip background gc for other reasons */
130033fbd510SChao Yu 	unsigned int ndirty_inode[NR_INODE_TYPE];	/* # of dirty inodes */
130135b09d82SNamjae Jeon #endif
130239a53e0cSJaegeuk Kim 	spinlock_t stat_lock;			/* lock for stat operations */
1303b59d0baeSNamjae Jeon 
1304b0af6d49SChao Yu 	/* For app/fs IO statistics */
1305b0af6d49SChao Yu 	spinlock_t iostat_lock;
1306b0af6d49SChao Yu 	unsigned long long write_iostat[NR_IO_TYPE];
1307b0af6d49SChao Yu 	bool iostat_enable;
1308b0af6d49SChao Yu 
1309b59d0baeSNamjae Jeon 	/* For sysfs suppport */
1310b59d0baeSNamjae Jeon 	struct kobject s_kobj;
1311b59d0baeSNamjae Jeon 	struct completion s_kobj_unregister;
13122658e50dSJaegeuk Kim 
13132658e50dSJaegeuk Kim 	/* For shrinker support */
13142658e50dSJaegeuk Kim 	struct list_head s_list;
13153c62be17SJaegeuk Kim 	int s_ndevs;				/* number of devices */
13163c62be17SJaegeuk Kim 	struct f2fs_dev_info *devs;		/* for device list */
13171228b482SChao Yu 	unsigned int dirty_device;		/* for checkpoint data flush */
13181228b482SChao Yu 	spinlock_t dev_lock;			/* protect dirty_device */
13192658e50dSJaegeuk Kim 	struct mutex umount_mutex;
13202658e50dSJaegeuk Kim 	unsigned int shrinker_run_no;
13218f1dbbbbSShuoran Liu 
13228f1dbbbbSShuoran Liu 	/* For write statistics */
13238f1dbbbbSShuoran Liu 	u64 sectors_written_start;
13248f1dbbbbSShuoran Liu 	u64 kbytes_written;
132543b6573bSKeith Mok 
132643b6573bSKeith Mok 	/* Reference to checksum algorithm driver via cryptoapi */
132743b6573bSKeith Mok 	struct crypto_shash *s_chksum_driver;
13281ecc0c5cSChao Yu 
1329704956ecSChao Yu 	/* Precomputed FS UUID checksum for seeding other checksums */
1330704956ecSChao Yu 	__u32 s_chksum_seed;
133139a53e0cSJaegeuk Kim };
133239a53e0cSJaegeuk Kim 
13331ecc0c5cSChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
133455523519SChao Yu #define f2fs_show_injection_info(type)					\
133522d7ea13SChao Yu 	printk_ratelimited("%sF2FS-fs : inject %s in %s of %pF\n",	\
13362d3a5856SGao Xiang 		KERN_INFO, f2fs_fault_name[type],			\
133755523519SChao Yu 		__func__, __builtin_return_address(0))
13381ecc0c5cSChao Yu static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
13391ecc0c5cSChao Yu {
134063189b78SChao Yu 	struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
13411ecc0c5cSChao Yu 
13421ecc0c5cSChao Yu 	if (!ffi->inject_rate)
13431ecc0c5cSChao Yu 		return false;
13441ecc0c5cSChao Yu 
13451ecc0c5cSChao Yu 	if (!IS_FAULT_SET(ffi, type))
13461ecc0c5cSChao Yu 		return false;
13471ecc0c5cSChao Yu 
13481ecc0c5cSChao Yu 	atomic_inc(&ffi->inject_ops);
13491ecc0c5cSChao Yu 	if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
13501ecc0c5cSChao Yu 		atomic_set(&ffi->inject_ops, 0);
13511ecc0c5cSChao Yu 		return true;
13521ecc0c5cSChao Yu 	}
13531ecc0c5cSChao Yu 	return false;
13541ecc0c5cSChao Yu }
13557fa750a1SArnd Bergmann #else
13567fa750a1SArnd Bergmann #define f2fs_show_injection_info(type) do { } while (0)
13577fa750a1SArnd Bergmann static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
13587fa750a1SArnd Bergmann {
13597fa750a1SArnd Bergmann 	return false;
13607fa750a1SArnd Bergmann }
13611ecc0c5cSChao Yu #endif
13621ecc0c5cSChao Yu 
13638f1dbbbbSShuoran Liu /* For write statistics. Suppose sector size is 512 bytes,
13648f1dbbbbSShuoran Liu  * and the return value is in kbytes. s is of struct f2fs_sb_info.
13658f1dbbbbSShuoran Liu  */
13668f1dbbbbSShuoran Liu #define BD_PART_WRITTEN(s)						 \
1367dbae2c55SMichael Callahan (((u64)part_stat_read((s)->sb->s_bdev->bd_part, sectors[STAT_WRITE]) -   \
136868afcf2dSTomohiro Kusumi 		(s)->sectors_written_start) >> 1)
13698f1dbbbbSShuoran Liu 
13706beceb54SJaegeuk Kim static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
13716beceb54SJaegeuk Kim {
1372a7d10cf3SSahitya Tummala 	unsigned long now = jiffies;
1373a7d10cf3SSahitya Tummala 
1374a7d10cf3SSahitya Tummala 	sbi->last_time[type] = now;
1375a7d10cf3SSahitya Tummala 
1376a7d10cf3SSahitya Tummala 	/* DISCARD_TIME and GC_TIME are based on REQ_TIME */
1377a7d10cf3SSahitya Tummala 	if (type == REQ_TIME) {
1378a7d10cf3SSahitya Tummala 		sbi->last_time[DISCARD_TIME] = now;
1379a7d10cf3SSahitya Tummala 		sbi->last_time[GC_TIME] = now;
1380a7d10cf3SSahitya Tummala 	}
13816beceb54SJaegeuk Kim }
13826beceb54SJaegeuk Kim 
13836beceb54SJaegeuk Kim static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
13846beceb54SJaegeuk Kim {
13856bccfa19SArnd Bergmann 	unsigned long interval = sbi->interval_time[type] * HZ;
13866beceb54SJaegeuk Kim 
13876beceb54SJaegeuk Kim 	return time_after(jiffies, sbi->last_time[type] + interval);
13886beceb54SJaegeuk Kim }
13896beceb54SJaegeuk Kim 
1390a7d10cf3SSahitya Tummala static inline unsigned int f2fs_time_to_wait(struct f2fs_sb_info *sbi,
1391a7d10cf3SSahitya Tummala 						int type)
1392a7d10cf3SSahitya Tummala {
1393a7d10cf3SSahitya Tummala 	unsigned long interval = sbi->interval_time[type] * HZ;
1394a7d10cf3SSahitya Tummala 	unsigned int wait_ms = 0;
1395a7d10cf3SSahitya Tummala 	long delta;
1396a7d10cf3SSahitya Tummala 
1397a7d10cf3SSahitya Tummala 	delta = (sbi->last_time[type] + interval) - jiffies;
1398a7d10cf3SSahitya Tummala 	if (delta > 0)
1399a7d10cf3SSahitya Tummala 		wait_ms = jiffies_to_msecs(delta);
1400a7d10cf3SSahitya Tummala 
1401a7d10cf3SSahitya Tummala 	return wait_ms;
1402a7d10cf3SSahitya Tummala }
1403a7d10cf3SSahitya Tummala 
140439a53e0cSJaegeuk Kim /*
140539a53e0cSJaegeuk Kim  * Inline functions
140639a53e0cSJaegeuk Kim  */
1407416d2dbbSChao Yu static inline u32 __f2fs_crc32(struct f2fs_sb_info *sbi, u32 crc,
1408704956ecSChao Yu 			      const void *address, unsigned int length)
1409704956ecSChao Yu {
1410704956ecSChao Yu 	struct {
1411704956ecSChao Yu 		struct shash_desc shash;
1412704956ecSChao Yu 		char ctx[4];
1413704956ecSChao Yu 	} desc;
1414704956ecSChao Yu 	int err;
1415704956ecSChao Yu 
1416704956ecSChao Yu 	BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver) != sizeof(desc.ctx));
1417704956ecSChao Yu 
1418704956ecSChao Yu 	desc.shash.tfm = sbi->s_chksum_driver;
1419704956ecSChao Yu 	desc.shash.flags = 0;
1420704956ecSChao Yu 	*(u32 *)desc.ctx = crc;
1421704956ecSChao Yu 
1422704956ecSChao Yu 	err = crypto_shash_update(&desc.shash, address, length);
1423704956ecSChao Yu 	BUG_ON(err);
1424704956ecSChao Yu 
1425704956ecSChao Yu 	return *(u32 *)desc.ctx;
1426704956ecSChao Yu }
1427704956ecSChao Yu 
1428416d2dbbSChao Yu static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
1429416d2dbbSChao Yu 			   unsigned int length)
1430416d2dbbSChao Yu {
1431416d2dbbSChao Yu 	return __f2fs_crc32(sbi, F2FS_SUPER_MAGIC, address, length);
1432416d2dbbSChao Yu }
1433416d2dbbSChao Yu 
1434416d2dbbSChao Yu static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
1435416d2dbbSChao Yu 				  void *buf, size_t buf_size)
1436416d2dbbSChao Yu {
1437416d2dbbSChao Yu 	return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
1438416d2dbbSChao Yu }
1439416d2dbbSChao Yu 
1440416d2dbbSChao Yu static inline u32 f2fs_chksum(struct f2fs_sb_info *sbi, u32 crc,
1441416d2dbbSChao Yu 			      const void *address, unsigned int length)
1442416d2dbbSChao Yu {
1443416d2dbbSChao Yu 	return __f2fs_crc32(sbi, crc, address, length);
1444416d2dbbSChao Yu }
1445416d2dbbSChao Yu 
144639a53e0cSJaegeuk Kim static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
144739a53e0cSJaegeuk Kim {
144839a53e0cSJaegeuk Kim 	return container_of(inode, struct f2fs_inode_info, vfs_inode);
144939a53e0cSJaegeuk Kim }
145039a53e0cSJaegeuk Kim 
145139a53e0cSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
145239a53e0cSJaegeuk Kim {
145339a53e0cSJaegeuk Kim 	return sb->s_fs_info;
145439a53e0cSJaegeuk Kim }
145539a53e0cSJaegeuk Kim 
14564081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
14574081363fSJaegeuk Kim {
14584081363fSJaegeuk Kim 	return F2FS_SB(inode->i_sb);
14594081363fSJaegeuk Kim }
14604081363fSJaegeuk Kim 
14614081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
14624081363fSJaegeuk Kim {
14634081363fSJaegeuk Kim 	return F2FS_I_SB(mapping->host);
14644081363fSJaegeuk Kim }
14654081363fSJaegeuk Kim 
14664081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
14674081363fSJaegeuk Kim {
14684081363fSJaegeuk Kim 	return F2FS_M_SB(page->mapping);
14694081363fSJaegeuk Kim }
14704081363fSJaegeuk Kim 
147139a53e0cSJaegeuk Kim static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
147239a53e0cSJaegeuk Kim {
147339a53e0cSJaegeuk Kim 	return (struct f2fs_super_block *)(sbi->raw_super);
147439a53e0cSJaegeuk Kim }
147539a53e0cSJaegeuk Kim 
147639a53e0cSJaegeuk Kim static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
147739a53e0cSJaegeuk Kim {
147839a53e0cSJaegeuk Kim 	return (struct f2fs_checkpoint *)(sbi->ckpt);
147939a53e0cSJaegeuk Kim }
148039a53e0cSJaegeuk Kim 
148145590710SGu Zheng static inline struct f2fs_node *F2FS_NODE(struct page *page)
148245590710SGu Zheng {
148345590710SGu Zheng 	return (struct f2fs_node *)page_address(page);
148445590710SGu Zheng }
148545590710SGu Zheng 
148658bfaf44SJaegeuk Kim static inline struct f2fs_inode *F2FS_INODE(struct page *page)
148758bfaf44SJaegeuk Kim {
148858bfaf44SJaegeuk Kim 	return &((struct f2fs_node *)page_address(page))->i;
148958bfaf44SJaegeuk Kim }
149058bfaf44SJaegeuk Kim 
149139a53e0cSJaegeuk Kim static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
149239a53e0cSJaegeuk Kim {
149339a53e0cSJaegeuk Kim 	return (struct f2fs_nm_info *)(sbi->nm_info);
149439a53e0cSJaegeuk Kim }
149539a53e0cSJaegeuk Kim 
149639a53e0cSJaegeuk Kim static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
149739a53e0cSJaegeuk Kim {
149839a53e0cSJaegeuk Kim 	return (struct f2fs_sm_info *)(sbi->sm_info);
149939a53e0cSJaegeuk Kim }
150039a53e0cSJaegeuk Kim 
150139a53e0cSJaegeuk Kim static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
150239a53e0cSJaegeuk Kim {
150339a53e0cSJaegeuk Kim 	return (struct sit_info *)(SM_I(sbi)->sit_info);
150439a53e0cSJaegeuk Kim }
150539a53e0cSJaegeuk Kim 
150639a53e0cSJaegeuk Kim static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
150739a53e0cSJaegeuk Kim {
150839a53e0cSJaegeuk Kim 	return (struct free_segmap_info *)(SM_I(sbi)->free_info);
150939a53e0cSJaegeuk Kim }
151039a53e0cSJaegeuk Kim 
151139a53e0cSJaegeuk Kim static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
151239a53e0cSJaegeuk Kim {
151339a53e0cSJaegeuk Kim 	return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
151439a53e0cSJaegeuk Kim }
151539a53e0cSJaegeuk Kim 
15169df27d98SGu Zheng static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
15179df27d98SGu Zheng {
15189df27d98SGu Zheng 	return sbi->meta_inode->i_mapping;
15199df27d98SGu Zheng }
15209df27d98SGu Zheng 
15214ef51a8fSJaegeuk Kim static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
15224ef51a8fSJaegeuk Kim {
15234ef51a8fSJaegeuk Kim 	return sbi->node_inode->i_mapping;
15244ef51a8fSJaegeuk Kim }
15254ef51a8fSJaegeuk Kim 
1526caf0047eSChao Yu static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
152739a53e0cSJaegeuk Kim {
1528fadb2fb8SChao Yu 	return test_bit(type, &sbi->s_flag);
152939a53e0cSJaegeuk Kim }
153039a53e0cSJaegeuk Kim 
1531caf0047eSChao Yu static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
153239a53e0cSJaegeuk Kim {
1533fadb2fb8SChao Yu 	set_bit(type, &sbi->s_flag);
1534caf0047eSChao Yu }
1535caf0047eSChao Yu 
1536caf0047eSChao Yu static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
1537caf0047eSChao Yu {
1538fadb2fb8SChao Yu 	clear_bit(type, &sbi->s_flag);
153939a53e0cSJaegeuk Kim }
154039a53e0cSJaegeuk Kim 
1541d71b5564SJaegeuk Kim static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
1542d71b5564SJaegeuk Kim {
1543d71b5564SJaegeuk Kim 	return le64_to_cpu(cp->checkpoint_ver);
1544d71b5564SJaegeuk Kim }
1545d71b5564SJaegeuk Kim 
1546ea676733SJaegeuk Kim static inline unsigned long f2fs_qf_ino(struct super_block *sb, int type)
1547ea676733SJaegeuk Kim {
1548ea676733SJaegeuk Kim 	if (type < F2FS_MAX_QUOTAS)
1549ea676733SJaegeuk Kim 		return le32_to_cpu(F2FS_SB(sb)->raw_super->qf_ino[type]);
1550ea676733SJaegeuk Kim 	return 0;
1551ea676733SJaegeuk Kim }
1552ea676733SJaegeuk Kim 
1553ced2c7eaSKinglong Mee static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
1554ced2c7eaSKinglong Mee {
1555ced2c7eaSKinglong Mee 	size_t crc_offset = le32_to_cpu(cp->checksum_offset);
1556ced2c7eaSKinglong Mee 	return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
1557ced2c7eaSKinglong Mee }
1558ced2c7eaSKinglong Mee 
1559aaec2b1dSChao Yu static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
156025ca923bSJaegeuk Kim {
156125ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
1562aaec2b1dSChao Yu 
156325ca923bSJaegeuk Kim 	return ckpt_flags & f;
156425ca923bSJaegeuk Kim }
156525ca923bSJaegeuk Kim 
1566aaec2b1dSChao Yu static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
156725ca923bSJaegeuk Kim {
1568aaec2b1dSChao Yu 	return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
1569aaec2b1dSChao Yu }
1570aaec2b1dSChao Yu 
1571aaec2b1dSChao Yu static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1572aaec2b1dSChao Yu {
1573aaec2b1dSChao Yu 	unsigned int ckpt_flags;
1574aaec2b1dSChao Yu 
1575aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
157625ca923bSJaegeuk Kim 	ckpt_flags |= f;
157725ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
157825ca923bSJaegeuk Kim }
157925ca923bSJaegeuk Kim 
1580aaec2b1dSChao Yu static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
158125ca923bSJaegeuk Kim {
1582d1aa2453SChao Yu 	unsigned long flags;
1583d1aa2453SChao Yu 
1584d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1585aaec2b1dSChao Yu 	__set_ckpt_flags(F2FS_CKPT(sbi), f);
1586d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1587aaec2b1dSChao Yu }
1588aaec2b1dSChao Yu 
1589aaec2b1dSChao Yu static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1590aaec2b1dSChao Yu {
1591aaec2b1dSChao Yu 	unsigned int ckpt_flags;
1592aaec2b1dSChao Yu 
1593aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
159425ca923bSJaegeuk Kim 	ckpt_flags &= (~f);
159525ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
159625ca923bSJaegeuk Kim }
159725ca923bSJaegeuk Kim 
1598aaec2b1dSChao Yu static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
1599aaec2b1dSChao Yu {
1600d1aa2453SChao Yu 	unsigned long flags;
1601d1aa2453SChao Yu 
1602d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1603aaec2b1dSChao Yu 	__clear_ckpt_flags(F2FS_CKPT(sbi), f);
1604d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1605aaec2b1dSChao Yu }
1606aaec2b1dSChao Yu 
160722ad0b6aSJaegeuk Kim static inline void disable_nat_bits(struct f2fs_sb_info *sbi, bool lock)
160822ad0b6aSJaegeuk Kim {
1609d1aa2453SChao Yu 	unsigned long flags;
1610d1aa2453SChao Yu 
161122ad0b6aSJaegeuk Kim 	set_sbi_flag(sbi, SBI_NEED_FSCK);
161222ad0b6aSJaegeuk Kim 
161322ad0b6aSJaegeuk Kim 	if (lock)
1614d1aa2453SChao Yu 		spin_lock_irqsave(&sbi->cp_lock, flags);
161522ad0b6aSJaegeuk Kim 	__clear_ckpt_flags(F2FS_CKPT(sbi), CP_NAT_BITS_FLAG);
161622ad0b6aSJaegeuk Kim 	kfree(NM_I(sbi)->nat_bits);
161722ad0b6aSJaegeuk Kim 	NM_I(sbi)->nat_bits = NULL;
161822ad0b6aSJaegeuk Kim 	if (lock)
1619d1aa2453SChao Yu 		spin_unlock_irqrestore(&sbi->cp_lock, flags);
162022ad0b6aSJaegeuk Kim }
162122ad0b6aSJaegeuk Kim 
162222ad0b6aSJaegeuk Kim static inline bool enabled_nat_bits(struct f2fs_sb_info *sbi,
162322ad0b6aSJaegeuk Kim 					struct cp_control *cpc)
162422ad0b6aSJaegeuk Kim {
162522ad0b6aSJaegeuk Kim 	bool set = is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
162622ad0b6aSJaegeuk Kim 
1627c473f1a9SChao Yu 	return (cpc) ? (cpc->reason & CP_UMOUNT) && set : set;
162822ad0b6aSJaegeuk Kim }
162922ad0b6aSJaegeuk Kim 
1630e479556bSGu Zheng static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
163139a53e0cSJaegeuk Kim {
1632b873b798SJaegeuk Kim 	down_read(&sbi->cp_rwsem);
163339a53e0cSJaegeuk Kim }
163439a53e0cSJaegeuk Kim 
1635cc15620bSJaegeuk Kim static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi)
1636cc15620bSJaegeuk Kim {
1637cc15620bSJaegeuk Kim 	return down_read_trylock(&sbi->cp_rwsem);
1638cc15620bSJaegeuk Kim }
1639cc15620bSJaegeuk Kim 
1640e479556bSGu Zheng static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
164139a53e0cSJaegeuk Kim {
1642b873b798SJaegeuk Kim 	up_read(&sbi->cp_rwsem);
164339936837SJaegeuk Kim }
164439936837SJaegeuk Kim 
1645e479556bSGu Zheng static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
164639936837SJaegeuk Kim {
1647b873b798SJaegeuk Kim 	down_write(&sbi->cp_rwsem);
164839936837SJaegeuk Kim }
164939936837SJaegeuk Kim 
1650e479556bSGu Zheng static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
165139936837SJaegeuk Kim {
1652b873b798SJaegeuk Kim 	up_write(&sbi->cp_rwsem);
165339a53e0cSJaegeuk Kim }
165439a53e0cSJaegeuk Kim 
1655119ee914SJaegeuk Kim static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
1656119ee914SJaegeuk Kim {
1657119ee914SJaegeuk Kim 	int reason = CP_SYNC;
1658119ee914SJaegeuk Kim 
1659119ee914SJaegeuk Kim 	if (test_opt(sbi, FASTBOOT))
1660119ee914SJaegeuk Kim 		reason = CP_FASTBOOT;
1661119ee914SJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
1662119ee914SJaegeuk Kim 		reason = CP_UMOUNT;
1663119ee914SJaegeuk Kim 	return reason;
1664119ee914SJaegeuk Kim }
1665119ee914SJaegeuk Kim 
1666119ee914SJaegeuk Kim static inline bool __remain_node_summaries(int reason)
1667119ee914SJaegeuk Kim {
1668c473f1a9SChao Yu 	return (reason & (CP_UMOUNT | CP_FASTBOOT));
1669119ee914SJaegeuk Kim }
1670119ee914SJaegeuk Kim 
1671119ee914SJaegeuk Kim static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
1672119ee914SJaegeuk Kim {
1673aaec2b1dSChao Yu 	return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
1674aaec2b1dSChao Yu 			is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
1675119ee914SJaegeuk Kim }
1676119ee914SJaegeuk Kim 
167739a53e0cSJaegeuk Kim /*
167839a53e0cSJaegeuk Kim  * Check whether the inode has blocks or not
167939a53e0cSJaegeuk Kim  */
168039a53e0cSJaegeuk Kim static inline int F2FS_HAS_BLOCKS(struct inode *inode)
168139a53e0cSJaegeuk Kim {
16820eb0adadSChao Yu 	block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
16830eb0adadSChao Yu 
1684000519f2SChao Yu 	return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block;
168539a53e0cSJaegeuk Kim }
168639a53e0cSJaegeuk Kim 
16874bc8e9bcSChao Yu static inline bool f2fs_has_xattr_block(unsigned int ofs)
16884bc8e9bcSChao Yu {
16894bc8e9bcSChao Yu 	return ofs == XATTR_NODE_OFFSET;
16904bc8e9bcSChao Yu }
16914bc8e9bcSChao Yu 
1692d8a9a229SJaegeuk Kim static inline bool __allow_reserved_blocks(struct f2fs_sb_info *sbi,
1693a90a0884SJaegeuk Kim 					struct inode *inode, bool cap)
16947c2e5963SJaegeuk Kim {
1695d8a9a229SJaegeuk Kim 	if (!inode)
1696d8a9a229SJaegeuk Kim 		return true;
16977c2e5963SJaegeuk Kim 	if (!test_opt(sbi, RESERVE_ROOT))
16987c2e5963SJaegeuk Kim 		return false;
1699d8a9a229SJaegeuk Kim 	if (IS_NOQUOTA(inode))
1700d8a9a229SJaegeuk Kim 		return true;
170163189b78SChao Yu 	if (uid_eq(F2FS_OPTION(sbi).s_resuid, current_fsuid()))
17027c2e5963SJaegeuk Kim 		return true;
170363189b78SChao Yu 	if (!gid_eq(F2FS_OPTION(sbi).s_resgid, GLOBAL_ROOT_GID) &&
170463189b78SChao Yu 					in_group_p(F2FS_OPTION(sbi).s_resgid))
17057c2e5963SJaegeuk Kim 		return true;
1706a90a0884SJaegeuk Kim 	if (cap && capable(CAP_SYS_RESOURCE))
1707162b27aeSJaegeuk Kim 		return true;
17087c2e5963SJaegeuk Kim 	return false;
17097c2e5963SJaegeuk Kim }
17107c2e5963SJaegeuk Kim 
17110abd675eSChao Yu static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool);
17120abd675eSChao Yu static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
171346008c6dSChao Yu 				 struct inode *inode, blkcnt_t *count)
171439a53e0cSJaegeuk Kim {
17150abd675eSChao Yu 	blkcnt_t diff = 0, release = 0;
1716daeb433eSChao Yu 	block_t avail_user_block_count;
17170abd675eSChao Yu 	int ret;
17180abd675eSChao Yu 
17190abd675eSChao Yu 	ret = dquot_reserve_block(inode, *count);
17200abd675eSChao Yu 	if (ret)
17210abd675eSChao Yu 		return ret;
1722dd11a5dfSJaegeuk Kim 
172355523519SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
172455523519SChao Yu 		f2fs_show_injection_info(FAULT_BLOCK);
17250abd675eSChao Yu 		release = *count;
17260abd675eSChao Yu 		goto enospc;
172755523519SChao Yu 	}
17287fa750a1SArnd Bergmann 
1729dd11a5dfSJaegeuk Kim 	/*
1730dd11a5dfSJaegeuk Kim 	 * let's increase this in prior to actual block count change in order
1731dd11a5dfSJaegeuk Kim 	 * for f2fs_sync_file to avoid data races when deciding checkpoint.
1732dd11a5dfSJaegeuk Kim 	 */
1733dd11a5dfSJaegeuk Kim 	percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
173439a53e0cSJaegeuk Kim 
173539a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
17362555a2d5SJaegeuk Kim 	sbi->total_valid_block_count += (block_t)(*count);
173780d42145SYunlong Song 	avail_user_block_count = sbi->user_block_count -
173880d42145SYunlong Song 					sbi->current_reserved_blocks;
17397e65be49SJaegeuk Kim 
1740a90a0884SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode, true))
174163189b78SChao Yu 		avail_user_block_count -= F2FS_OPTION(sbi).root_reserved_blocks;
17424354994fSDaniel Rosenberg 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
17434354994fSDaniel Rosenberg 		avail_user_block_count -= sbi->unusable_block_count;
1744daeb433eSChao Yu 	if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
1745daeb433eSChao Yu 		diff = sbi->total_valid_block_count - avail_user_block_count;
17467e65be49SJaegeuk Kim 		if (diff > *count)
17477e65be49SJaegeuk Kim 			diff = *count;
1748dd11a5dfSJaegeuk Kim 		*count -= diff;
17490abd675eSChao Yu 		release = diff;
17507e65be49SJaegeuk Kim 		sbi->total_valid_block_count -= diff;
175146008c6dSChao Yu 		if (!*count) {
175239a53e0cSJaegeuk Kim 			spin_unlock(&sbi->stat_lock);
17530abd675eSChao Yu 			goto enospc;
175439a53e0cSJaegeuk Kim 		}
175546008c6dSChao Yu 	}
175639a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
175741382ec4SJaegeuk Kim 
175836b877afSDaniel Rosenberg 	if (unlikely(release)) {
175936b877afSDaniel Rosenberg 		percpu_counter_sub(&sbi->alloc_valid_block_count, release);
17600abd675eSChao Yu 		dquot_release_reservation_block(inode, release);
176136b877afSDaniel Rosenberg 	}
17620abd675eSChao Yu 	f2fs_i_blocks_write(inode, *count, true, true);
17630abd675eSChao Yu 	return 0;
17640abd675eSChao Yu 
17650abd675eSChao Yu enospc:
176636b877afSDaniel Rosenberg 	percpu_counter_sub(&sbi->alloc_valid_block_count, release);
17670abd675eSChao Yu 	dquot_release_reservation_block(inode, release);
17680abd675eSChao Yu 	return -ENOSPC;
176939a53e0cSJaegeuk Kim }
177039a53e0cSJaegeuk Kim 
1771da19b0dcSGu Zheng static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
177239a53e0cSJaegeuk Kim 						struct inode *inode,
17730eb0adadSChao Yu 						block_t count)
177439a53e0cSJaegeuk Kim {
17750eb0adadSChao Yu 	blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK;
17760eb0adadSChao Yu 
177739a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
17789850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
17790eb0adadSChao Yu 	f2fs_bug_on(sbi, inode->i_blocks < sectors);
178039a53e0cSJaegeuk Kim 	sbi->total_valid_block_count -= (block_t)count;
178180d42145SYunlong Song 	if (sbi->reserved_blocks &&
178280d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
178380d42145SYunlong Song 		sbi->current_reserved_blocks = min(sbi->reserved_blocks,
178480d42145SYunlong Song 					sbi->current_reserved_blocks + count);
178539a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
17860abd675eSChao Yu 	f2fs_i_blocks_write(inode, count, false, true);
178739a53e0cSJaegeuk Kim }
178839a53e0cSJaegeuk Kim 
178939a53e0cSJaegeuk Kim static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
179039a53e0cSJaegeuk Kim {
179135782b23SJaegeuk Kim 	atomic_inc(&sbi->nr_pages[count_type]);
17927c4abcbeSChao Yu 
179336951b38SChao Yu 	if (count_type == F2FS_DIRTY_DATA || count_type == F2FS_INMEM_PAGES ||
17945f9abab4SJaegeuk Kim 		count_type == F2FS_WB_CP_DATA || count_type == F2FS_WB_DATA ||
17955f9abab4SJaegeuk Kim 		count_type == F2FS_RD_DATA || count_type == F2FS_RD_NODE ||
17965f9abab4SJaegeuk Kim 		count_type == F2FS_RD_META)
17977c4abcbeSChao Yu 		return;
17987c4abcbeSChao Yu 
1799caf0047eSChao Yu 	set_sbi_flag(sbi, SBI_IS_DIRTY);
180039a53e0cSJaegeuk Kim }
180139a53e0cSJaegeuk Kim 
1802a7ffdbe2SJaegeuk Kim static inline void inode_inc_dirty_pages(struct inode *inode)
180339a53e0cSJaegeuk Kim {
1804204706c7SJaegeuk Kim 	atomic_inc(&F2FS_I(inode)->dirty_pages);
1805c227f912SChao Yu 	inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1806c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
18072c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
18082c8a4a28SJaegeuk Kim 		inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
180939a53e0cSJaegeuk Kim }
181039a53e0cSJaegeuk Kim 
181139a53e0cSJaegeuk Kim static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
181239a53e0cSJaegeuk Kim {
181335782b23SJaegeuk Kim 	atomic_dec(&sbi->nr_pages[count_type]);
181439a53e0cSJaegeuk Kim }
181539a53e0cSJaegeuk Kim 
1816a7ffdbe2SJaegeuk Kim static inline void inode_dec_dirty_pages(struct inode *inode)
181739a53e0cSJaegeuk Kim {
18185ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
18195ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
18201fe54f9dSJaegeuk Kim 		return;
18211fe54f9dSJaegeuk Kim 
1822204706c7SJaegeuk Kim 	atomic_dec(&F2FS_I(inode)->dirty_pages);
1823c227f912SChao Yu 	dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1824c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
18252c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
18262c8a4a28SJaegeuk Kim 		dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
182739a53e0cSJaegeuk Kim }
182839a53e0cSJaegeuk Kim 
1829523be8a6SJaegeuk Kim static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
183039a53e0cSJaegeuk Kim {
183135782b23SJaegeuk Kim 	return atomic_read(&sbi->nr_pages[count_type]);
183239a53e0cSJaegeuk Kim }
183339a53e0cSJaegeuk Kim 
1834204706c7SJaegeuk Kim static inline int get_dirty_pages(struct inode *inode)
1835f8b2c1f9SJaegeuk Kim {
1836204706c7SJaegeuk Kim 	return atomic_read(&F2FS_I(inode)->dirty_pages);
1837f8b2c1f9SJaegeuk Kim }
1838f8b2c1f9SJaegeuk Kim 
18395ac206cfSNamjae Jeon static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
18405ac206cfSNamjae Jeon {
18413519e3f9SChao Yu 	unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
1842523be8a6SJaegeuk Kim 	unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
1843523be8a6SJaegeuk Kim 						sbi->log_blocks_per_seg;
1844523be8a6SJaegeuk Kim 
1845523be8a6SJaegeuk Kim 	return segs / sbi->segs_per_sec;
18465ac206cfSNamjae Jeon }
18475ac206cfSNamjae Jeon 
184839a53e0cSJaegeuk Kim static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
184939a53e0cSJaegeuk Kim {
18508b8343faSJaegeuk Kim 	return sbi->total_valid_block_count;
185139a53e0cSJaegeuk Kim }
185239a53e0cSJaegeuk Kim 
1853f83a2584SYunlei He static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
1854f83a2584SYunlei He {
1855f83a2584SYunlei He 	return sbi->discard_blks;
1856f83a2584SYunlei He }
1857f83a2584SYunlei He 
185839a53e0cSJaegeuk Kim static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
185939a53e0cSJaegeuk Kim {
186039a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
186139a53e0cSJaegeuk Kim 
186239a53e0cSJaegeuk Kim 	/* return NAT or SIT bitmap */
186339a53e0cSJaegeuk Kim 	if (flag == NAT_BITMAP)
186439a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
186539a53e0cSJaegeuk Kim 	else if (flag == SIT_BITMAP)
186639a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
186739a53e0cSJaegeuk Kim 
186839a53e0cSJaegeuk Kim 	return 0;
186939a53e0cSJaegeuk Kim }
187039a53e0cSJaegeuk Kim 
187155141486SWanpeng Li static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
187255141486SWanpeng Li {
187355141486SWanpeng Li 	return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
187455141486SWanpeng Li }
187555141486SWanpeng Li 
187639a53e0cSJaegeuk Kim static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
187739a53e0cSJaegeuk Kim {
187839a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
18791dbe4152SChangman Lee 	int offset;
18801dbe4152SChangman Lee 
1881199bc3feSChao Yu 	if (is_set_ckpt_flags(sbi, CP_LARGE_NAT_BITMAP_FLAG)) {
1882199bc3feSChao Yu 		offset = (flag == SIT_BITMAP) ?
1883199bc3feSChao Yu 			le32_to_cpu(ckpt->nat_ver_bitmap_bytesize) : 0;
1884199bc3feSChao Yu 		return &ckpt->sit_nat_version_bitmap + offset;
1885199bc3feSChao Yu 	}
1886199bc3feSChao Yu 
188755141486SWanpeng Li 	if (__cp_payload(sbi) > 0) {
18881dbe4152SChangman Lee 		if (flag == NAT_BITMAP)
18891dbe4152SChangman Lee 			return &ckpt->sit_nat_version_bitmap;
18901dbe4152SChangman Lee 		else
189165b85cccSJaegeuk Kim 			return (unsigned char *)ckpt + F2FS_BLKSIZE;
18921dbe4152SChangman Lee 	} else {
18931dbe4152SChangman Lee 		offset = (flag == NAT_BITMAP) ?
189425ca923bSJaegeuk Kim 			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
189539a53e0cSJaegeuk Kim 		return &ckpt->sit_nat_version_bitmap + offset;
189639a53e0cSJaegeuk Kim 	}
18971dbe4152SChangman Lee }
189839a53e0cSJaegeuk Kim 
189939a53e0cSJaegeuk Kim static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
190039a53e0cSJaegeuk Kim {
19018508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
190239a53e0cSJaegeuk Kim 
19038508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 2)
190439a53e0cSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
190539a53e0cSJaegeuk Kim 	return start_addr;
190639a53e0cSJaegeuk Kim }
190739a53e0cSJaegeuk Kim 
19088508e44aSJaegeuk Kim static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
19098508e44aSJaegeuk Kim {
19108508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
19118508e44aSJaegeuk Kim 
19128508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 1)
19138508e44aSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
19148508e44aSJaegeuk Kim 	return start_addr;
19158508e44aSJaegeuk Kim }
19168508e44aSJaegeuk Kim 
19178508e44aSJaegeuk Kim static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
19188508e44aSJaegeuk Kim {
19198508e44aSJaegeuk Kim 	sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
19208508e44aSJaegeuk Kim }
19218508e44aSJaegeuk Kim 
192239a53e0cSJaegeuk Kim static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
192339a53e0cSJaegeuk Kim {
192439a53e0cSJaegeuk Kim 	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
192539a53e0cSJaegeuk Kim }
192639a53e0cSJaegeuk Kim 
19270abd675eSChao Yu static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
1928000519f2SChao Yu 					struct inode *inode, bool is_inode)
192939a53e0cSJaegeuk Kim {
193039a53e0cSJaegeuk Kim 	block_t	valid_block_count;
193139a53e0cSJaegeuk Kim 	unsigned int valid_node_count;
1932*af033b2aSChao Yu 	int err;
19330abd675eSChao Yu 
1934*af033b2aSChao Yu 	if (is_inode) {
1935*af033b2aSChao Yu 		if (inode) {
1936*af033b2aSChao Yu 			err = dquot_alloc_inode(inode);
1937*af033b2aSChao Yu 			if (err)
1938*af033b2aSChao Yu 				return err;
1939*af033b2aSChao Yu 		}
1940*af033b2aSChao Yu 	} else {
1941*af033b2aSChao Yu 		err = dquot_reserve_block(inode, 1);
1942*af033b2aSChao Yu 		if (err)
1943*af033b2aSChao Yu 			return err;
19440abd675eSChao Yu 	}
194539a53e0cSJaegeuk Kim 
1946812c6056SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
1947812c6056SChao Yu 		f2fs_show_injection_info(FAULT_BLOCK);
1948812c6056SChao Yu 		goto enospc;
1949812c6056SChao Yu 	}
1950812c6056SChao Yu 
195139a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
195239a53e0cSJaegeuk Kim 
19537e65be49SJaegeuk Kim 	valid_block_count = sbi->total_valid_block_count +
19547e65be49SJaegeuk Kim 					sbi->current_reserved_blocks + 1;
19557e65be49SJaegeuk Kim 
1956a90a0884SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode, false))
195763189b78SChao Yu 		valid_block_count += F2FS_OPTION(sbi).root_reserved_blocks;
19584354994fSDaniel Rosenberg 	if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
19594354994fSDaniel Rosenberg 		valid_block_count += sbi->unusable_block_count;
19607e65be49SJaegeuk Kim 
19617e65be49SJaegeuk Kim 	if (unlikely(valid_block_count > sbi->user_block_count)) {
196239a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
19630abd675eSChao Yu 		goto enospc;
196439a53e0cSJaegeuk Kim 	}
196539a53e0cSJaegeuk Kim 
1966ef86d709SGu Zheng 	valid_node_count = sbi->total_valid_node_count + 1;
1967cfb271d4SChao Yu 	if (unlikely(valid_node_count > sbi->total_node_count)) {
196839a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
19690abd675eSChao Yu 		goto enospc;
197039a53e0cSJaegeuk Kim 	}
197139a53e0cSJaegeuk Kim 
1972ef86d709SGu Zheng 	sbi->total_valid_node_count++;
1973ef86d709SGu Zheng 	sbi->total_valid_block_count++;
197439a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
197539a53e0cSJaegeuk Kim 
19760abd675eSChao Yu 	if (inode) {
19770abd675eSChao Yu 		if (is_inode)
19780abd675eSChao Yu 			f2fs_mark_inode_dirty_sync(inode, true);
19790abd675eSChao Yu 		else
19800abd675eSChao Yu 			f2fs_i_blocks_write(inode, 1, true, true);
19810abd675eSChao Yu 	}
19820abd675eSChao Yu 
198341382ec4SJaegeuk Kim 	percpu_counter_inc(&sbi->alloc_valid_block_count);
19840abd675eSChao Yu 	return 0;
19850abd675eSChao Yu 
19860abd675eSChao Yu enospc:
1987*af033b2aSChao Yu 	if (is_inode) {
1988*af033b2aSChao Yu 		if (inode)
1989*af033b2aSChao Yu 			dquot_free_inode(inode);
1990*af033b2aSChao Yu 	} else {
19910abd675eSChao Yu 		dquot_release_reservation_block(inode, 1);
1992*af033b2aSChao Yu 	}
19930abd675eSChao Yu 	return -ENOSPC;
199439a53e0cSJaegeuk Kim }
199539a53e0cSJaegeuk Kim 
199639a53e0cSJaegeuk Kim static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
1997000519f2SChao Yu 					struct inode *inode, bool is_inode)
199839a53e0cSJaegeuk Kim {
199939a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
200039a53e0cSJaegeuk Kim 
20019850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_block_count);
20029850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_node_count);
2003000519f2SChao Yu 	f2fs_bug_on(sbi, !is_inode && !inode->i_blocks);
200439a53e0cSJaegeuk Kim 
2005ef86d709SGu Zheng 	sbi->total_valid_node_count--;
2006ef86d709SGu Zheng 	sbi->total_valid_block_count--;
200780d42145SYunlong Song 	if (sbi->reserved_blocks &&
200880d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
200980d42145SYunlong Song 		sbi->current_reserved_blocks++;
201039a53e0cSJaegeuk Kim 
201139a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
20120abd675eSChao Yu 
2013*af033b2aSChao Yu 	if (is_inode)
2014*af033b2aSChao Yu 		dquot_free_inode(inode);
2015*af033b2aSChao Yu 	else
20160abd675eSChao Yu 		f2fs_i_blocks_write(inode, 1, false, true);
201739a53e0cSJaegeuk Kim }
201839a53e0cSJaegeuk Kim 
201939a53e0cSJaegeuk Kim static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
202039a53e0cSJaegeuk Kim {
20218b8343faSJaegeuk Kim 	return sbi->total_valid_node_count;
202239a53e0cSJaegeuk Kim }
202339a53e0cSJaegeuk Kim 
202439a53e0cSJaegeuk Kim static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
202539a53e0cSJaegeuk Kim {
2026513c5f37SJaegeuk Kim 	percpu_counter_inc(&sbi->total_valid_inode_count);
202739a53e0cSJaegeuk Kim }
202839a53e0cSJaegeuk Kim 
20290e80220aSJaegeuk Kim static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
203039a53e0cSJaegeuk Kim {
2031513c5f37SJaegeuk Kim 	percpu_counter_dec(&sbi->total_valid_inode_count);
203239a53e0cSJaegeuk Kim }
203339a53e0cSJaegeuk Kim 
2034513c5f37SJaegeuk Kim static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
203539a53e0cSJaegeuk Kim {
2036513c5f37SJaegeuk Kim 	return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
203739a53e0cSJaegeuk Kim }
203839a53e0cSJaegeuk Kim 
2039a56c7c6fSJaegeuk Kim static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
2040a56c7c6fSJaegeuk Kim 						pgoff_t index, bool for_write)
2041a56c7c6fSJaegeuk Kim {
204282cf4f13SChao Yu 	struct page *page;
2043cac5a3d8SDongOh Shin 
20447fa750a1SArnd Bergmann 	if (IS_ENABLED(CONFIG_F2FS_FAULT_INJECTION)) {
204582cf4f13SChao Yu 		if (!for_write)
204682cf4f13SChao Yu 			page = find_get_page_flags(mapping, index,
204782cf4f13SChao Yu 							FGP_LOCK | FGP_ACCESSED);
204882cf4f13SChao Yu 		else
204982cf4f13SChao Yu 			page = find_lock_page(mapping, index);
2050c41f3cc3SJaegeuk Kim 		if (page)
2051c41f3cc3SJaegeuk Kim 			return page;
2052c41f3cc3SJaegeuk Kim 
205355523519SChao Yu 		if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) {
205455523519SChao Yu 			f2fs_show_injection_info(FAULT_PAGE_ALLOC);
2055c41f3cc3SJaegeuk Kim 			return NULL;
205655523519SChao Yu 		}
20577fa750a1SArnd Bergmann 	}
20587fa750a1SArnd Bergmann 
2059a56c7c6fSJaegeuk Kim 	if (!for_write)
2060a56c7c6fSJaegeuk Kim 		return grab_cache_page(mapping, index);
2061a56c7c6fSJaegeuk Kim 	return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
2062a56c7c6fSJaegeuk Kim }
2063a56c7c6fSJaegeuk Kim 
206401eccef7SChao Yu static inline struct page *f2fs_pagecache_get_page(
206501eccef7SChao Yu 				struct address_space *mapping, pgoff_t index,
206601eccef7SChao Yu 				int fgp_flags, gfp_t gfp_mask)
206701eccef7SChao Yu {
206801eccef7SChao Yu 	if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_GET)) {
206901eccef7SChao Yu 		f2fs_show_injection_info(FAULT_PAGE_GET);
207001eccef7SChao Yu 		return NULL;
207101eccef7SChao Yu 	}
20727fa750a1SArnd Bergmann 
207301eccef7SChao Yu 	return pagecache_get_page(mapping, index, fgp_flags, gfp_mask);
207401eccef7SChao Yu }
207501eccef7SChao Yu 
20766e2c64adSJaegeuk Kim static inline void f2fs_copy_page(struct page *src, struct page *dst)
20776e2c64adSJaegeuk Kim {
20786e2c64adSJaegeuk Kim 	char *src_kaddr = kmap(src);
20796e2c64adSJaegeuk Kim 	char *dst_kaddr = kmap(dst);
20806e2c64adSJaegeuk Kim 
20816e2c64adSJaegeuk Kim 	memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
20826e2c64adSJaegeuk Kim 	kunmap(dst);
20836e2c64adSJaegeuk Kim 	kunmap(src);
20846e2c64adSJaegeuk Kim }
20856e2c64adSJaegeuk Kim 
208639a53e0cSJaegeuk Kim static inline void f2fs_put_page(struct page *page, int unlock)
208739a53e0cSJaegeuk Kim {
2088031fa8ccSJaegeuk Kim 	if (!page)
208939a53e0cSJaegeuk Kim 		return;
209039a53e0cSJaegeuk Kim 
209139a53e0cSJaegeuk Kim 	if (unlock) {
20929850cf4aSJaegeuk Kim 		f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
209339a53e0cSJaegeuk Kim 		unlock_page(page);
209439a53e0cSJaegeuk Kim 	}
209509cbfeafSKirill A. Shutemov 	put_page(page);
209639a53e0cSJaegeuk Kim }
209739a53e0cSJaegeuk Kim 
209839a53e0cSJaegeuk Kim static inline void f2fs_put_dnode(struct dnode_of_data *dn)
209939a53e0cSJaegeuk Kim {
210039a53e0cSJaegeuk Kim 	if (dn->node_page)
210139a53e0cSJaegeuk Kim 		f2fs_put_page(dn->node_page, 1);
210239a53e0cSJaegeuk Kim 	if (dn->inode_page && dn->node_page != dn->inode_page)
210339a53e0cSJaegeuk Kim 		f2fs_put_page(dn->inode_page, 0);
210439a53e0cSJaegeuk Kim 	dn->node_page = NULL;
210539a53e0cSJaegeuk Kim 	dn->inode_page = NULL;
210639a53e0cSJaegeuk Kim }
210739a53e0cSJaegeuk Kim 
210839a53e0cSJaegeuk Kim static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
2109e8512d2eSGu Zheng 					size_t size)
211039a53e0cSJaegeuk Kim {
2111e8512d2eSGu Zheng 	return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
211239a53e0cSJaegeuk Kim }
211339a53e0cSJaegeuk Kim 
21147bd59381SGu Zheng static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
21157bd59381SGu Zheng 						gfp_t flags)
21167bd59381SGu Zheng {
21177bd59381SGu Zheng 	void *entry;
21187bd59381SGu Zheng 
211980c54505SJaegeuk Kim 	entry = kmem_cache_alloc(cachep, flags);
212080c54505SJaegeuk Kim 	if (!entry)
212180c54505SJaegeuk Kim 		entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
21227bd59381SGu Zheng 	return entry;
21237bd59381SGu Zheng }
21247bd59381SGu Zheng 
2125d62fe971SChao Yu static inline struct bio *f2fs_bio_alloc(struct f2fs_sb_info *sbi,
2126d62fe971SChao Yu 						int npages, bool no_fail)
2127740432f8SJaegeuk Kim {
2128740432f8SJaegeuk Kim 	struct bio *bio;
2129740432f8SJaegeuk Kim 
2130d62fe971SChao Yu 	if (no_fail) {
2131740432f8SJaegeuk Kim 		/* No failure on bio allocation */
2132740432f8SJaegeuk Kim 		bio = bio_alloc(GFP_NOIO, npages);
213380c54505SJaegeuk Kim 		if (!bio)
213480c54505SJaegeuk Kim 			bio = bio_alloc(GFP_NOIO | __GFP_NOFAIL, npages);
2135740432f8SJaegeuk Kim 		return bio;
2136740432f8SJaegeuk Kim 	}
2137d62fe971SChao Yu 	if (time_to_inject(sbi, FAULT_ALLOC_BIO)) {
2138d62fe971SChao Yu 		f2fs_show_injection_info(FAULT_ALLOC_BIO);
2139d62fe971SChao Yu 		return NULL;
2140d62fe971SChao Yu 	}
21417fa750a1SArnd Bergmann 
2142d62fe971SChao Yu 	return bio_alloc(GFP_KERNEL, npages);
2143d62fe971SChao Yu }
2144740432f8SJaegeuk Kim 
21455f9abab4SJaegeuk Kim static inline bool is_idle(struct f2fs_sb_info *sbi, int type)
21465f9abab4SJaegeuk Kim {
21475f9abab4SJaegeuk Kim 	if (get_pages(sbi, F2FS_RD_DATA) || get_pages(sbi, F2FS_RD_NODE) ||
21485f9abab4SJaegeuk Kim 		get_pages(sbi, F2FS_RD_META) || get_pages(sbi, F2FS_WB_DATA) ||
21495f9abab4SJaegeuk Kim 		get_pages(sbi, F2FS_WB_CP_DATA))
21505f9abab4SJaegeuk Kim 		return false;
21515f9abab4SJaegeuk Kim 	return f2fs_time_over(sbi, type);
21525f9abab4SJaegeuk Kim }
21535f9abab4SJaegeuk Kim 
21549be32d72SJaegeuk Kim static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
21559be32d72SJaegeuk Kim 				unsigned long index, void *item)
21569be32d72SJaegeuk Kim {
21579be32d72SJaegeuk Kim 	while (radix_tree_insert(root, index, item))
21589be32d72SJaegeuk Kim 		cond_resched();
21599be32d72SJaegeuk Kim }
21609be32d72SJaegeuk Kim 
216139a53e0cSJaegeuk Kim #define RAW_IS_INODE(p)	((p)->footer.nid == (p)->footer.ino)
216239a53e0cSJaegeuk Kim 
216339a53e0cSJaegeuk Kim static inline bool IS_INODE(struct page *page)
216439a53e0cSJaegeuk Kim {
216545590710SGu Zheng 	struct f2fs_node *p = F2FS_NODE(page);
2166cac5a3d8SDongOh Shin 
216739a53e0cSJaegeuk Kim 	return RAW_IS_INODE(p);
216839a53e0cSJaegeuk Kim }
216939a53e0cSJaegeuk Kim 
21707a2af766SChao Yu static inline int offset_in_addr(struct f2fs_inode *i)
21717a2af766SChao Yu {
21727a2af766SChao Yu 	return (i->i_inline & F2FS_EXTRA_ATTR) ?
21737a2af766SChao Yu 			(le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0;
21747a2af766SChao Yu }
21757a2af766SChao Yu 
217639a53e0cSJaegeuk Kim static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
217739a53e0cSJaegeuk Kim {
217839a53e0cSJaegeuk Kim 	return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
217939a53e0cSJaegeuk Kim }
218039a53e0cSJaegeuk Kim 
21817a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode);
21827a2af766SChao Yu static inline block_t datablock_addr(struct inode *inode,
21837a2af766SChao Yu 			struct page *node_page, unsigned int offset)
218439a53e0cSJaegeuk Kim {
218539a53e0cSJaegeuk Kim 	struct f2fs_node *raw_node;
218639a53e0cSJaegeuk Kim 	__le32 *addr_array;
21877a2af766SChao Yu 	int base = 0;
21887a2af766SChao Yu 	bool is_inode = IS_INODE(node_page);
2189cac5a3d8SDongOh Shin 
219045590710SGu Zheng 	raw_node = F2FS_NODE(node_page);
21917a2af766SChao Yu 
21927a2af766SChao Yu 	/* from GC path only */
2193979f492fSLiFan 	if (is_inode) {
2194979f492fSLiFan 		if (!inode)
21957a2af766SChao Yu 			base = offset_in_addr(&raw_node->i);
2196979f492fSLiFan 		else if (f2fs_has_extra_attr(inode))
21977a2af766SChao Yu 			base = get_extra_isize(inode);
21987a2af766SChao Yu 	}
21997a2af766SChao Yu 
220039a53e0cSJaegeuk Kim 	addr_array = blkaddr_in_node(raw_node);
22017a2af766SChao Yu 	return le32_to_cpu(addr_array[base + offset]);
220239a53e0cSJaegeuk Kim }
220339a53e0cSJaegeuk Kim 
220439a53e0cSJaegeuk Kim static inline int f2fs_test_bit(unsigned int nr, char *addr)
220539a53e0cSJaegeuk Kim {
220639a53e0cSJaegeuk Kim 	int mask;
220739a53e0cSJaegeuk Kim 
220839a53e0cSJaegeuk Kim 	addr += (nr >> 3);
220939a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
221039a53e0cSJaegeuk Kim 	return mask & *addr;
221139a53e0cSJaegeuk Kim }
221239a53e0cSJaegeuk Kim 
2213a66cdd98SJaegeuk Kim static inline void f2fs_set_bit(unsigned int nr, char *addr)
2214a66cdd98SJaegeuk Kim {
2215a66cdd98SJaegeuk Kim 	int mask;
2216a66cdd98SJaegeuk Kim 
2217a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2218a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2219a66cdd98SJaegeuk Kim 	*addr |= mask;
2220a66cdd98SJaegeuk Kim }
2221a66cdd98SJaegeuk Kim 
2222a66cdd98SJaegeuk Kim static inline void f2fs_clear_bit(unsigned int nr, char *addr)
2223a66cdd98SJaegeuk Kim {
2224a66cdd98SJaegeuk Kim 	int mask;
2225a66cdd98SJaegeuk Kim 
2226a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2227a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2228a66cdd98SJaegeuk Kim 	*addr &= ~mask;
2229a66cdd98SJaegeuk Kim }
2230a66cdd98SJaegeuk Kim 
223152aca074SGu Zheng static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
223239a53e0cSJaegeuk Kim {
223339a53e0cSJaegeuk Kim 	int mask;
223439a53e0cSJaegeuk Kim 	int ret;
223539a53e0cSJaegeuk Kim 
223639a53e0cSJaegeuk Kim 	addr += (nr >> 3);
223739a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
223839a53e0cSJaegeuk Kim 	ret = mask & *addr;
223939a53e0cSJaegeuk Kim 	*addr |= mask;
224039a53e0cSJaegeuk Kim 	return ret;
224139a53e0cSJaegeuk Kim }
224239a53e0cSJaegeuk Kim 
224352aca074SGu Zheng static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
224439a53e0cSJaegeuk Kim {
224539a53e0cSJaegeuk Kim 	int mask;
224639a53e0cSJaegeuk Kim 	int ret;
224739a53e0cSJaegeuk Kim 
224839a53e0cSJaegeuk Kim 	addr += (nr >> 3);
224939a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
225039a53e0cSJaegeuk Kim 	ret = mask & *addr;
225139a53e0cSJaegeuk Kim 	*addr &= ~mask;
225239a53e0cSJaegeuk Kim 	return ret;
225339a53e0cSJaegeuk Kim }
225439a53e0cSJaegeuk Kim 
2255c6ac4c0eSGu Zheng static inline void f2fs_change_bit(unsigned int nr, char *addr)
2256c6ac4c0eSGu Zheng {
2257c6ac4c0eSGu Zheng 	int mask;
2258c6ac4c0eSGu Zheng 
2259c6ac4c0eSGu Zheng 	addr += (nr >> 3);
2260c6ac4c0eSGu Zheng 	mask = 1 << (7 - (nr & 0x07));
2261c6ac4c0eSGu Zheng 	*addr ^= mask;
2262c6ac4c0eSGu Zheng }
2263c6ac4c0eSGu Zheng 
226459c84408SChao Yu /*
226559c84408SChao Yu  * Inode flags
226659c84408SChao Yu  */
226759c84408SChao Yu #define F2FS_SECRM_FL			0x00000001 /* Secure deletion */
226859c84408SChao Yu #define F2FS_UNRM_FL			0x00000002 /* Undelete */
226959c84408SChao Yu #define F2FS_COMPR_FL			0x00000004 /* Compress file */
227059c84408SChao Yu #define F2FS_SYNC_FL			0x00000008 /* Synchronous updates */
227159c84408SChao Yu #define F2FS_IMMUTABLE_FL		0x00000010 /* Immutable file */
227259c84408SChao Yu #define F2FS_APPEND_FL			0x00000020 /* writes to file may only append */
227359c84408SChao Yu #define F2FS_NODUMP_FL			0x00000040 /* do not dump file */
227459c84408SChao Yu #define F2FS_NOATIME_FL			0x00000080 /* do not update atime */
227559c84408SChao Yu /* Reserved for compression usage... */
227659c84408SChao Yu #define F2FS_DIRTY_FL			0x00000100
227759c84408SChao Yu #define F2FS_COMPRBLK_FL		0x00000200 /* One or more compressed clusters */
227859c84408SChao Yu #define F2FS_NOCOMPR_FL			0x00000400 /* Don't compress */
227959c84408SChao Yu #define F2FS_ENCRYPT_FL			0x00000800 /* encrypted file */
228059c84408SChao Yu /* End compression flags --- maybe not all used */
228159c84408SChao Yu #define F2FS_INDEX_FL			0x00001000 /* hash-indexed directory */
228259c84408SChao Yu #define F2FS_IMAGIC_FL			0x00002000 /* AFS directory */
228359c84408SChao Yu #define F2FS_JOURNAL_DATA_FL		0x00004000 /* file data should be journaled */
228459c84408SChao Yu #define F2FS_NOTAIL_FL			0x00008000 /* file tail should not be merged */
228559c84408SChao Yu #define F2FS_DIRSYNC_FL			0x00010000 /* dirsync behaviour (directories only) */
228659c84408SChao Yu #define F2FS_TOPDIR_FL			0x00020000 /* Top of directory hierarchies*/
228759c84408SChao Yu #define F2FS_HUGE_FILE_FL               0x00040000 /* Set to each huge file */
228859c84408SChao Yu #define F2FS_EXTENTS_FL			0x00080000 /* Inode uses extents */
228959c84408SChao Yu #define F2FS_EA_INODE_FL	        0x00200000 /* Inode used for large EA */
229059c84408SChao Yu #define F2FS_EOFBLOCKS_FL		0x00400000 /* Blocks allocated beyond EOF */
229159c84408SChao Yu #define F2FS_INLINE_DATA_FL		0x10000000 /* Inode has inline data. */
229259c84408SChao Yu #define F2FS_PROJINHERIT_FL		0x20000000 /* Create with parents projid */
229359c84408SChao Yu #define F2FS_RESERVED_FL		0x80000000 /* reserved for ext4 lib */
229459c84408SChao Yu 
229559c84408SChao Yu #define F2FS_FL_USER_VISIBLE		0x304BDFFF /* User visible flags */
229659c84408SChao Yu #define F2FS_FL_USER_MODIFIABLE		0x204BC0FF /* User modifiable flags */
229759c84408SChao Yu 
229859c84408SChao Yu /* Flags we can manipulate with through F2FS_IOC_FSSETXATTR */
229959c84408SChao Yu #define F2FS_FL_XFLAG_VISIBLE		(F2FS_SYNC_FL | \
230059c84408SChao Yu 					 F2FS_IMMUTABLE_FL | \
230159c84408SChao Yu 					 F2FS_APPEND_FL | \
230259c84408SChao Yu 					 F2FS_NODUMP_FL | \
230359c84408SChao Yu 					 F2FS_NOATIME_FL | \
230459c84408SChao Yu 					 F2FS_PROJINHERIT_FL)
230559c84408SChao Yu 
230659c84408SChao Yu /* Flags that should be inherited by new inodes from their parent. */
230759c84408SChao Yu #define F2FS_FL_INHERITED (F2FS_SECRM_FL | F2FS_UNRM_FL | F2FS_COMPR_FL |\
230859c84408SChao Yu 			   F2FS_SYNC_FL | F2FS_NODUMP_FL | F2FS_NOATIME_FL |\
230959c84408SChao Yu 			   F2FS_NOCOMPR_FL | F2FS_JOURNAL_DATA_FL |\
231059c84408SChao Yu 			   F2FS_NOTAIL_FL | F2FS_DIRSYNC_FL |\
231159c84408SChao Yu 			   F2FS_PROJINHERIT_FL)
231259c84408SChao Yu 
231359c84408SChao Yu /* Flags that are appropriate for regular files (all but dir-specific ones). */
231459c84408SChao Yu #define F2FS_REG_FLMASK		(~(F2FS_DIRSYNC_FL | F2FS_TOPDIR_FL))
231559c84408SChao Yu 
231659c84408SChao Yu /* Flags that are appropriate for non-directories/regular files. */
231759c84408SChao Yu #define F2FS_OTHER_FLMASK	(F2FS_NODUMP_FL | F2FS_NOATIME_FL)
23185c57132eSChao Yu 
23195c57132eSChao Yu static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
23205c57132eSChao Yu {
23215c57132eSChao Yu 	if (S_ISDIR(mode))
23225c57132eSChao Yu 		return flags;
23235c57132eSChao Yu 	else if (S_ISREG(mode))
23245c57132eSChao Yu 		return flags & F2FS_REG_FLMASK;
23255c57132eSChao Yu 	else
23265c57132eSChao Yu 		return flags & F2FS_OTHER_FLMASK;
23275c57132eSChao Yu }
23285c57132eSChao Yu 
232939a53e0cSJaegeuk Kim /* used for f2fs_inode_info->flags */
233039a53e0cSJaegeuk Kim enum {
233139a53e0cSJaegeuk Kim 	FI_NEW_INODE,		/* indicate newly allocated inode */
2332b3783873SJaegeuk Kim 	FI_DIRTY_INODE,		/* indicate inode is dirty or not */
233326de9b11SJaegeuk Kim 	FI_AUTO_RECOVER,	/* indicate inode is recoverable */
2334ed57c27fSJaegeuk Kim 	FI_DIRTY_DIR,		/* indicate directory has dirty pages */
233539a53e0cSJaegeuk Kim 	FI_INC_LINK,		/* need to increment i_nlink */
233639a53e0cSJaegeuk Kim 	FI_ACL_MODE,		/* indicate acl mode */
233739a53e0cSJaegeuk Kim 	FI_NO_ALLOC,		/* should not allocate any blocks */
2338c9b63bd0SJaegeuk Kim 	FI_FREE_NID,		/* free allocated nide */
2339c11abd1aSJaegeuk Kim 	FI_NO_EXTENT,		/* not to use the extent cache */
2340444c580fSJaegeuk Kim 	FI_INLINE_XATTR,	/* used for inline xattr */
23411001b347SHuajun Li 	FI_INLINE_DATA,		/* used for inline data*/
234234d67debSChao Yu 	FI_INLINE_DENTRY,	/* used for inline dentry */
2343fff04f90SJaegeuk Kim 	FI_APPEND_WRITE,	/* inode has appended data */
2344fff04f90SJaegeuk Kim 	FI_UPDATE_WRITE,	/* inode has in-place-update data */
234588b88a66SJaegeuk Kim 	FI_NEED_IPU,		/* used for ipu per file */
234688b88a66SJaegeuk Kim 	FI_ATOMIC_FILE,		/* indicate atomic file */
23475fe45743SChao Yu 	FI_ATOMIC_COMMIT,	/* indicate the state of atomical committing */
234802a1335fSJaegeuk Kim 	FI_VOLATILE_FILE,	/* indicate volatile file */
23493c6c2bebSJaegeuk Kim 	FI_FIRST_BLOCK_WRITTEN,	/* indicate #0 data block was written */
23501e84371fSJaegeuk Kim 	FI_DROP_CACHE,		/* drop dirty page cache */
2351b3d208f9SJaegeuk Kim 	FI_DATA_EXIST,		/* indicate data exists */
2352510022a8SJaegeuk Kim 	FI_INLINE_DOTS,		/* indicate inline dot dentries */
2353d323d005SChao Yu 	FI_DO_DEFRAG,		/* indicate defragment is running */
2354c227f912SChao Yu 	FI_DIRTY_FILE,		/* indicate regular/symlink has dirty pages */
2355dc91de78SJaegeuk Kim 	FI_NO_PREALLOC,		/* indicate skipped preallocated blocks */
2356ef095d19SJaegeuk Kim 	FI_HOT_DATA,		/* indicate file is hot */
23577a2af766SChao Yu 	FI_EXTRA_ATTR,		/* indicate file has extra attribute */
23585c57132eSChao Yu 	FI_PROJ_INHERIT,	/* indicate file inherits projectid */
23591ad71a27SJaegeuk Kim 	FI_PIN_FILE,		/* indicate file should not be gced */
23602ef79ecbSChao Yu 	FI_ATOMIC_REVOKE_REQUEST, /* request to drop atomic data */
236139a53e0cSJaegeuk Kim };
236239a53e0cSJaegeuk Kim 
2363205b9822SJaegeuk Kim static inline void __mark_inode_dirty_flag(struct inode *inode,
2364205b9822SJaegeuk Kim 						int flag, bool set)
236539a53e0cSJaegeuk Kim {
2366205b9822SJaegeuk Kim 	switch (flag) {
2367205b9822SJaegeuk Kim 	case FI_INLINE_XATTR:
2368205b9822SJaegeuk Kim 	case FI_INLINE_DATA:
2369205b9822SJaegeuk Kim 	case FI_INLINE_DENTRY:
23709ac1e2d8SDaeho Jeong 	case FI_NEW_INODE:
2371205b9822SJaegeuk Kim 		if (set)
2372205b9822SJaegeuk Kim 			return;
2373205b9822SJaegeuk Kim 	case FI_DATA_EXIST:
2374205b9822SJaegeuk Kim 	case FI_INLINE_DOTS:
23751ad71a27SJaegeuk Kim 	case FI_PIN_FILE:
23767c45729aSJaegeuk Kim 		f2fs_mark_inode_dirty_sync(inode, true);
2377205b9822SJaegeuk Kim 	}
237839a53e0cSJaegeuk Kim }
237939a53e0cSJaegeuk Kim 
238091942321SJaegeuk Kim static inline void set_inode_flag(struct inode *inode, int flag)
238139a53e0cSJaegeuk Kim {
238291942321SJaegeuk Kim 	if (!test_bit(flag, &F2FS_I(inode)->flags))
238391942321SJaegeuk Kim 		set_bit(flag, &F2FS_I(inode)->flags);
2384205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, true);
238539a53e0cSJaegeuk Kim }
238639a53e0cSJaegeuk Kim 
238791942321SJaegeuk Kim static inline int is_inode_flag_set(struct inode *inode, int flag)
238839a53e0cSJaegeuk Kim {
238991942321SJaegeuk Kim 	return test_bit(flag, &F2FS_I(inode)->flags);
239039a53e0cSJaegeuk Kim }
239139a53e0cSJaegeuk Kim 
239291942321SJaegeuk Kim static inline void clear_inode_flag(struct inode *inode, int flag)
239339a53e0cSJaegeuk Kim {
239491942321SJaegeuk Kim 	if (test_bit(flag, &F2FS_I(inode)->flags))
239591942321SJaegeuk Kim 		clear_bit(flag, &F2FS_I(inode)->flags);
2396205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, false);
239739a53e0cSJaegeuk Kim }
239839a53e0cSJaegeuk Kim 
239991942321SJaegeuk Kim static inline void set_acl_inode(struct inode *inode, umode_t mode)
2400444c580fSJaegeuk Kim {
240191942321SJaegeuk Kim 	F2FS_I(inode)->i_acl_mode = mode;
240291942321SJaegeuk Kim 	set_inode_flag(inode, FI_ACL_MODE);
24037c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, false);
240439a53e0cSJaegeuk Kim }
240539a53e0cSJaegeuk Kim 
2406a1961246SJaegeuk Kim static inline void f2fs_i_links_write(struct inode *inode, bool inc)
2407a1961246SJaegeuk Kim {
2408a1961246SJaegeuk Kim 	if (inc)
2409a1961246SJaegeuk Kim 		inc_nlink(inode);
2410a1961246SJaegeuk Kim 	else
2411a1961246SJaegeuk Kim 		drop_nlink(inode);
24127c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2413a1961246SJaegeuk Kim }
2414a1961246SJaegeuk Kim 
24158edd03c8SJaegeuk Kim static inline void f2fs_i_blocks_write(struct inode *inode,
24160abd675eSChao Yu 					block_t diff, bool add, bool claim)
24178edd03c8SJaegeuk Kim {
241826de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
241926de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
242026de9b11SJaegeuk Kim 
24210abd675eSChao Yu 	/* add = 1, claim = 1 should be dquot_reserve_block in pair */
24220abd675eSChao Yu 	if (add) {
24230abd675eSChao Yu 		if (claim)
24240abd675eSChao Yu 			dquot_claim_block(inode, diff);
24250abd675eSChao Yu 		else
24260abd675eSChao Yu 			dquot_alloc_block_nofail(inode, diff);
24270abd675eSChao Yu 	} else {
24280abd675eSChao Yu 		dquot_free_block(inode, diff);
24290abd675eSChao Yu 	}
24300abd675eSChao Yu 
24317c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
243226de9b11SJaegeuk Kim 	if (clean || recover)
243326de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
24348edd03c8SJaegeuk Kim }
24358edd03c8SJaegeuk Kim 
2436fc9581c8SJaegeuk Kim static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
2437fc9581c8SJaegeuk Kim {
243826de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
243926de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
244026de9b11SJaegeuk Kim 
2441fc9581c8SJaegeuk Kim 	if (i_size_read(inode) == i_size)
2442fc9581c8SJaegeuk Kim 		return;
2443fc9581c8SJaegeuk Kim 
2444fc9581c8SJaegeuk Kim 	i_size_write(inode, i_size);
24457c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
244626de9b11SJaegeuk Kim 	if (clean || recover)
244726de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
244826de9b11SJaegeuk Kim }
244926de9b11SJaegeuk Kim 
2450205b9822SJaegeuk Kim static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
2451205b9822SJaegeuk Kim {
2452205b9822SJaegeuk Kim 	F2FS_I(inode)->i_current_depth = depth;
24537c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2454205b9822SJaegeuk Kim }
2455205b9822SJaegeuk Kim 
24561ad71a27SJaegeuk Kim static inline void f2fs_i_gc_failures_write(struct inode *inode,
24571ad71a27SJaegeuk Kim 					unsigned int count)
24581ad71a27SJaegeuk Kim {
24592ef79ecbSChao Yu 	F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN] = count;
24601ad71a27SJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
24611ad71a27SJaegeuk Kim }
24621ad71a27SJaegeuk Kim 
2463205b9822SJaegeuk Kim static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
2464205b9822SJaegeuk Kim {
2465205b9822SJaegeuk Kim 	F2FS_I(inode)->i_xattr_nid = xnid;
24667c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2467205b9822SJaegeuk Kim }
2468205b9822SJaegeuk Kim 
2469205b9822SJaegeuk Kim static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
2470205b9822SJaegeuk Kim {
2471205b9822SJaegeuk Kim 	F2FS_I(inode)->i_pino = pino;
24727c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2473205b9822SJaegeuk Kim }
2474205b9822SJaegeuk Kim 
247591942321SJaegeuk Kim static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
2476444c580fSJaegeuk Kim {
2477205b9822SJaegeuk Kim 	struct f2fs_inode_info *fi = F2FS_I(inode);
2478205b9822SJaegeuk Kim 
2479444c580fSJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_XATTR)
2480205b9822SJaegeuk Kim 		set_bit(FI_INLINE_XATTR, &fi->flags);
24811001b347SHuajun Li 	if (ri->i_inline & F2FS_INLINE_DATA)
2482205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DATA, &fi->flags);
248334d67debSChao Yu 	if (ri->i_inline & F2FS_INLINE_DENTRY)
2484205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DENTRY, &fi->flags);
2485b3d208f9SJaegeuk Kim 	if (ri->i_inline & F2FS_DATA_EXIST)
2486205b9822SJaegeuk Kim 		set_bit(FI_DATA_EXIST, &fi->flags);
2487510022a8SJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_DOTS)
2488205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DOTS, &fi->flags);
24897a2af766SChao Yu 	if (ri->i_inline & F2FS_EXTRA_ATTR)
24907a2af766SChao Yu 		set_bit(FI_EXTRA_ATTR, &fi->flags);
24911ad71a27SJaegeuk Kim 	if (ri->i_inline & F2FS_PIN_FILE)
24921ad71a27SJaegeuk Kim 		set_bit(FI_PIN_FILE, &fi->flags);
2493444c580fSJaegeuk Kim }
2494444c580fSJaegeuk Kim 
249591942321SJaegeuk Kim static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
2496444c580fSJaegeuk Kim {
2497444c580fSJaegeuk Kim 	ri->i_inline = 0;
2498444c580fSJaegeuk Kim 
249991942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_XATTR))
2500444c580fSJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_XATTR;
250191942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DATA))
25021001b347SHuajun Li 		ri->i_inline |= F2FS_INLINE_DATA;
250391942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
250434d67debSChao Yu 		ri->i_inline |= F2FS_INLINE_DENTRY;
250591942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_DATA_EXIST))
2506b3d208f9SJaegeuk Kim 		ri->i_inline |= F2FS_DATA_EXIST;
250791942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DOTS))
2508510022a8SJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_DOTS;
25097a2af766SChao Yu 	if (is_inode_flag_set(inode, FI_EXTRA_ATTR))
25107a2af766SChao Yu 		ri->i_inline |= F2FS_EXTRA_ATTR;
25111ad71a27SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_PIN_FILE))
25121ad71a27SJaegeuk Kim 		ri->i_inline |= F2FS_PIN_FILE;
25137a2af766SChao Yu }
25147a2af766SChao Yu 
25157a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode)
25167a2af766SChao Yu {
25177a2af766SChao Yu 	return is_inode_flag_set(inode, FI_EXTRA_ATTR);
2518444c580fSJaegeuk Kim }
2519444c580fSJaegeuk Kim 
2520987c7c31SChao Yu static inline int f2fs_has_inline_xattr(struct inode *inode)
2521987c7c31SChao Yu {
252291942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_XATTR);
2523987c7c31SChao Yu }
2524987c7c31SChao Yu 
252581ca7350SChao Yu static inline unsigned int addrs_per_inode(struct inode *inode)
2526de93653fSJaegeuk Kim {
2527b323fd28SChao Yu 	return CUR_ADDRS_PER_INODE(inode) - get_inline_xattr_addrs(inode);
2528de93653fSJaegeuk Kim }
2529de93653fSJaegeuk Kim 
25306afc662eSChao Yu static inline void *inline_xattr_addr(struct inode *inode, struct page *page)
253165985d93SJaegeuk Kim {
2532695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
2533cac5a3d8SDongOh Shin 
253465985d93SJaegeuk Kim 	return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
2535b323fd28SChao Yu 					get_inline_xattr_addrs(inode)]);
253665985d93SJaegeuk Kim }
253765985d93SJaegeuk Kim 
253865985d93SJaegeuk Kim static inline int inline_xattr_size(struct inode *inode)
253965985d93SJaegeuk Kim {
25406afc662eSChao Yu 	return get_inline_xattr_addrs(inode) * sizeof(__le32);
254165985d93SJaegeuk Kim }
254265985d93SJaegeuk Kim 
25430dbdc2aeSJaegeuk Kim static inline int f2fs_has_inline_data(struct inode *inode)
25440dbdc2aeSJaegeuk Kim {
254591942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DATA);
25460dbdc2aeSJaegeuk Kim }
25470dbdc2aeSJaegeuk Kim 
2548b3d208f9SJaegeuk Kim static inline int f2fs_exist_data(struct inode *inode)
2549b3d208f9SJaegeuk Kim {
255091942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DATA_EXIST);
2551b3d208f9SJaegeuk Kim }
2552b3d208f9SJaegeuk Kim 
2553510022a8SJaegeuk Kim static inline int f2fs_has_inline_dots(struct inode *inode)
2554510022a8SJaegeuk Kim {
255591942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DOTS);
2556510022a8SJaegeuk Kim }
2557510022a8SJaegeuk Kim 
25581ad71a27SJaegeuk Kim static inline bool f2fs_is_pinned_file(struct inode *inode)
25591ad71a27SJaegeuk Kim {
25601ad71a27SJaegeuk Kim 	return is_inode_flag_set(inode, FI_PIN_FILE);
25611ad71a27SJaegeuk Kim }
25621ad71a27SJaegeuk Kim 
256388b88a66SJaegeuk Kim static inline bool f2fs_is_atomic_file(struct inode *inode)
256488b88a66SJaegeuk Kim {
256591942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_ATOMIC_FILE);
256688b88a66SJaegeuk Kim }
256788b88a66SJaegeuk Kim 
25685fe45743SChao Yu static inline bool f2fs_is_commit_atomic_write(struct inode *inode)
25695fe45743SChao Yu {
25705fe45743SChao Yu 	return is_inode_flag_set(inode, FI_ATOMIC_COMMIT);
25715fe45743SChao Yu }
25725fe45743SChao Yu 
257302a1335fSJaegeuk Kim static inline bool f2fs_is_volatile_file(struct inode *inode)
257402a1335fSJaegeuk Kim {
257591942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_VOLATILE_FILE);
257602a1335fSJaegeuk Kim }
257702a1335fSJaegeuk Kim 
25783c6c2bebSJaegeuk Kim static inline bool f2fs_is_first_block_written(struct inode *inode)
25793c6c2bebSJaegeuk Kim {
258091942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
25813c6c2bebSJaegeuk Kim }
25823c6c2bebSJaegeuk Kim 
25831e84371fSJaegeuk Kim static inline bool f2fs_is_drop_cache(struct inode *inode)
25841e84371fSJaegeuk Kim {
258591942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DROP_CACHE);
25861e84371fSJaegeuk Kim }
25871e84371fSJaegeuk Kim 
2588f2470371SChao Yu static inline void *inline_data_addr(struct inode *inode, struct page *page)
25891001b347SHuajun Li {
2590695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
25917a2af766SChao Yu 	int extra_size = get_extra_isize(inode);
2592cac5a3d8SDongOh Shin 
25937a2af766SChao Yu 	return (void *)&(ri->i_addr[extra_size + DEF_INLINE_RESERVED_SIZE]);
25941001b347SHuajun Li }
25951001b347SHuajun Li 
259634d67debSChao Yu static inline int f2fs_has_inline_dentry(struct inode *inode)
259734d67debSChao Yu {
259891942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DENTRY);
259934d67debSChao Yu }
260034d67debSChao Yu 
2601b5492af7SJaegeuk Kim static inline int is_file(struct inode *inode, int type)
2602b5492af7SJaegeuk Kim {
2603b5492af7SJaegeuk Kim 	return F2FS_I(inode)->i_advise & type;
2604b5492af7SJaegeuk Kim }
2605b5492af7SJaegeuk Kim 
2606b5492af7SJaegeuk Kim static inline void set_file(struct inode *inode, int type)
2607b5492af7SJaegeuk Kim {
2608b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise |= type;
26097c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2610b5492af7SJaegeuk Kim }
2611b5492af7SJaegeuk Kim 
2612b5492af7SJaegeuk Kim static inline void clear_file(struct inode *inode, int type)
2613b5492af7SJaegeuk Kim {
2614b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise &= ~type;
26157c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2616b5492af7SJaegeuk Kim }
2617b5492af7SJaegeuk Kim 
261826787236SJaegeuk Kim static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
261926787236SJaegeuk Kim {
2620a0d00fadSChao Yu 	bool ret;
2621a0d00fadSChao Yu 
262226787236SJaegeuk Kim 	if (dsync) {
262326787236SJaegeuk Kim 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
262426787236SJaegeuk Kim 
262526787236SJaegeuk Kim 		spin_lock(&sbi->inode_lock[DIRTY_META]);
262626787236SJaegeuk Kim 		ret = list_empty(&F2FS_I(inode)->gdirty_list);
262726787236SJaegeuk Kim 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
262826787236SJaegeuk Kim 		return ret;
262926787236SJaegeuk Kim 	}
263026787236SJaegeuk Kim 	if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
263126787236SJaegeuk Kim 			file_keep_isize(inode) ||
2632235831d7SJunling Zheng 			i_size_read(inode) & ~PAGE_MASK)
263326787236SJaegeuk Kim 		return false;
2634a0d00fadSChao Yu 
263524b81dfcSArnd Bergmann 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time, &inode->i_atime))
2636214c2461SJaegeuk Kim 		return false;
263724b81dfcSArnd Bergmann 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 1, &inode->i_ctime))
2638214c2461SJaegeuk Kim 		return false;
263924b81dfcSArnd Bergmann 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 2, &inode->i_mtime))
2640214c2461SJaegeuk Kim 		return false;
264124b81dfcSArnd Bergmann 	if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 3,
2642214c2461SJaegeuk Kim 						&F2FS_I(inode)->i_crtime))
2643214c2461SJaegeuk Kim 		return false;
2644214c2461SJaegeuk Kim 
2645a0d00fadSChao Yu 	down_read(&F2FS_I(inode)->i_sem);
2646a0d00fadSChao Yu 	ret = F2FS_I(inode)->last_disk_size == i_size_read(inode);
2647a0d00fadSChao Yu 	up_read(&F2FS_I(inode)->i_sem);
2648a0d00fadSChao Yu 
2649a0d00fadSChao Yu 	return ret;
2650267378d4SChao Yu }
2651267378d4SChao Yu 
2652deeedd71SChao Yu static inline bool f2fs_readonly(struct super_block *sb)
265377888c1eSJaegeuk Kim {
2654deeedd71SChao Yu 	return sb_rdonly(sb);
265577888c1eSJaegeuk Kim }
265677888c1eSJaegeuk Kim 
2657744602cfSJaegeuk Kim static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
2658744602cfSJaegeuk Kim {
2659aaec2b1dSChao Yu 	return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
2660744602cfSJaegeuk Kim }
2661744602cfSJaegeuk Kim 
2662eaa693f4SJaegeuk Kim static inline bool is_dot_dotdot(const struct qstr *str)
2663eaa693f4SJaegeuk Kim {
2664eaa693f4SJaegeuk Kim 	if (str->len == 1 && str->name[0] == '.')
2665eaa693f4SJaegeuk Kim 		return true;
2666eaa693f4SJaegeuk Kim 
2667eaa693f4SJaegeuk Kim 	if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
2668eaa693f4SJaegeuk Kim 		return true;
2669eaa693f4SJaegeuk Kim 
2670eaa693f4SJaegeuk Kim 	return false;
2671eaa693f4SJaegeuk Kim }
2672eaa693f4SJaegeuk Kim 
26733e72f721SJaegeuk Kim static inline bool f2fs_may_extent_tree(struct inode *inode)
26743e72f721SJaegeuk Kim {
26753e72f721SJaegeuk Kim 	if (!test_opt(F2FS_I_SB(inode), EXTENT_CACHE) ||
267691942321SJaegeuk Kim 			is_inode_flag_set(inode, FI_NO_EXTENT))
26773e72f721SJaegeuk Kim 		return false;
26783e72f721SJaegeuk Kim 
2679886f56f9SAl Viro 	return S_ISREG(inode->i_mode);
26803e72f721SJaegeuk Kim }
26813e72f721SJaegeuk Kim 
26821ecc0c5cSChao Yu static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
26831ecc0c5cSChao Yu 					size_t size, gfp_t flags)
26840414b004SJaegeuk Kim {
268555523519SChao Yu 	if (time_to_inject(sbi, FAULT_KMALLOC)) {
268655523519SChao Yu 		f2fs_show_injection_info(FAULT_KMALLOC);
26872c63feadSJaegeuk Kim 		return NULL;
268855523519SChao Yu 	}
26897fa750a1SArnd Bergmann 
26900414b004SJaegeuk Kim 	return kmalloc(size, flags);
26910414b004SJaegeuk Kim }
26920414b004SJaegeuk Kim 
2693acbf054dSChao Yu static inline void *f2fs_kzalloc(struct f2fs_sb_info *sbi,
2694acbf054dSChao Yu 					size_t size, gfp_t flags)
2695acbf054dSChao Yu {
2696acbf054dSChao Yu 	return f2fs_kmalloc(sbi, size, flags | __GFP_ZERO);
2697acbf054dSChao Yu }
2698acbf054dSChao Yu 
2699628b3d14SChao Yu static inline void *f2fs_kvmalloc(struct f2fs_sb_info *sbi,
2700628b3d14SChao Yu 					size_t size, gfp_t flags)
2701628b3d14SChao Yu {
2702628b3d14SChao Yu 	if (time_to_inject(sbi, FAULT_KVMALLOC)) {
2703628b3d14SChao Yu 		f2fs_show_injection_info(FAULT_KVMALLOC);
2704628b3d14SChao Yu 		return NULL;
2705628b3d14SChao Yu 	}
27067fa750a1SArnd Bergmann 
2707628b3d14SChao Yu 	return kvmalloc(size, flags);
2708628b3d14SChao Yu }
2709628b3d14SChao Yu 
2710628b3d14SChao Yu static inline void *f2fs_kvzalloc(struct f2fs_sb_info *sbi,
2711628b3d14SChao Yu 					size_t size, gfp_t flags)
2712628b3d14SChao Yu {
2713628b3d14SChao Yu 	return f2fs_kvmalloc(sbi, size, flags | __GFP_ZERO);
2714628b3d14SChao Yu }
2715628b3d14SChao Yu 
27167a2af766SChao Yu static inline int get_extra_isize(struct inode *inode)
2717f2470371SChao Yu {
27187a2af766SChao Yu 	return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
2719f2470371SChao Yu }
2720f2470371SChao Yu 
27216afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode)
27226afc662eSChao Yu {
27236afc662eSChao Yu 	return F2FS_I(inode)->i_inline_xattr_size;
27246afc662eSChao Yu }
27256afc662eSChao Yu 
27264d57b86dSChao Yu #define f2fs_get_inode_mode(i) \
272791942321SJaegeuk Kim 	((is_inode_flag_set(i, FI_ACL_MODE)) ? \
2728a6dda0e6SChristoph Hellwig 	 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
2729a6dda0e6SChristoph Hellwig 
27307a2af766SChao Yu #define F2FS_TOTAL_EXTRA_ATTR_SIZE			\
27317a2af766SChao Yu 	(offsetof(struct f2fs_inode, i_extra_end) -	\
27327a2af766SChao Yu 	offsetof(struct f2fs_inode, i_extra_isize))	\
27337a2af766SChao Yu 
27345c57132eSChao Yu #define F2FS_OLD_ATTRIBUTE_SIZE	(offsetof(struct f2fs_inode, i_addr))
27355c57132eSChao Yu #define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field)		\
27365c57132eSChao Yu 		((offsetof(typeof(*f2fs_inode), field) +	\
27375c57132eSChao Yu 		sizeof((f2fs_inode)->field))			\
27385c57132eSChao Yu 		<= (F2FS_OLD_ATTRIBUTE_SIZE + extra_isize))	\
27395c57132eSChao Yu 
2740b0af6d49SChao Yu static inline void f2fs_reset_iostat(struct f2fs_sb_info *sbi)
2741b0af6d49SChao Yu {
2742b0af6d49SChao Yu 	int i;
2743b0af6d49SChao Yu 
2744b0af6d49SChao Yu 	spin_lock(&sbi->iostat_lock);
2745b0af6d49SChao Yu 	for (i = 0; i < NR_IO_TYPE; i++)
2746b0af6d49SChao Yu 		sbi->write_iostat[i] = 0;
2747b0af6d49SChao Yu 	spin_unlock(&sbi->iostat_lock);
2748b0af6d49SChao Yu }
2749b0af6d49SChao Yu 
2750b0af6d49SChao Yu static inline void f2fs_update_iostat(struct f2fs_sb_info *sbi,
2751b0af6d49SChao Yu 			enum iostat_type type, unsigned long long io_bytes)
2752b0af6d49SChao Yu {
2753b0af6d49SChao Yu 	if (!sbi->iostat_enable)
2754b0af6d49SChao Yu 		return;
2755b0af6d49SChao Yu 	spin_lock(&sbi->iostat_lock);
2756b0af6d49SChao Yu 	sbi->write_iostat[type] += io_bytes;
2757b0af6d49SChao Yu 
2758b0af6d49SChao Yu 	if (type == APP_WRITE_IO || type == APP_DIRECT_IO)
2759b0af6d49SChao Yu 		sbi->write_iostat[APP_BUFFERED_IO] =
2760b0af6d49SChao Yu 			sbi->write_iostat[APP_WRITE_IO] -
2761b0af6d49SChao Yu 			sbi->write_iostat[APP_DIRECT_IO];
2762b0af6d49SChao Yu 	spin_unlock(&sbi->iostat_lock);
2763b0af6d49SChao Yu }
2764b0af6d49SChao Yu 
2765c9b60788SChao Yu #define __is_meta_io(fio) (PAGE_TYPE_OF_BIO(fio->type) == META &&	\
2766c9b60788SChao Yu 				(!is_read_io(fio->op) || fio->is_meta))
2767c9b60788SChao Yu 
2768e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
2769e1da7872SChao Yu 					block_t blkaddr, int type);
2770e1da7872SChao Yu void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...);
2771e1da7872SChao Yu static inline void verify_blkaddr(struct f2fs_sb_info *sbi,
2772e1da7872SChao Yu 					block_t blkaddr, int type)
2773e1da7872SChao Yu {
2774e1da7872SChao Yu 	if (!f2fs_is_valid_blkaddr(sbi, blkaddr, type)) {
2775e1da7872SChao Yu 		f2fs_msg(sbi->sb, KERN_ERR,
2776e1da7872SChao Yu 			"invalid blkaddr: %u, type: %d, run fsck to fix.",
2777e1da7872SChao Yu 			blkaddr, type);
2778e1da7872SChao Yu 		f2fs_bug_on(sbi, 1);
2779e1da7872SChao Yu 	}
2780e1da7872SChao Yu }
2781e1da7872SChao Yu 
2782e1da7872SChao Yu static inline bool __is_valid_data_blkaddr(block_t blkaddr)
27837b525dd0SChao Yu {
27847b525dd0SChao Yu 	if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
27857b525dd0SChao Yu 		return false;
27867b525dd0SChao Yu 	return true;
27877b525dd0SChao Yu }
27887b525dd0SChao Yu 
2789e1da7872SChao Yu static inline bool is_valid_data_blkaddr(struct f2fs_sb_info *sbi,
2790e1da7872SChao Yu 						block_t blkaddr)
2791e1da7872SChao Yu {
2792e1da7872SChao Yu 	if (!__is_valid_data_blkaddr(blkaddr))
2793e1da7872SChao Yu 		return false;
2794e1da7872SChao Yu 	verify_blkaddr(sbi, blkaddr, DATA_GENERIC);
2795e1da7872SChao Yu 	return true;
2796e1da7872SChao Yu }
2797e1da7872SChao Yu 
279839a53e0cSJaegeuk Kim /*
279939a53e0cSJaegeuk Kim  * file.c
280039a53e0cSJaegeuk Kim  */
2801cac5a3d8SDongOh Shin int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
28024d57b86dSChao Yu void f2fs_truncate_data_blocks(struct dnode_of_data *dn);
2803*af033b2aSChao Yu int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock,
2804*af033b2aSChao Yu 							bool buf_write);
2805cac5a3d8SDongOh Shin int f2fs_truncate(struct inode *inode);
2806a528d35eSDavid Howells int f2fs_getattr(const struct path *path, struct kstat *stat,
2807a528d35eSDavid Howells 			u32 request_mask, unsigned int flags);
2808cac5a3d8SDongOh Shin int f2fs_setattr(struct dentry *dentry, struct iattr *attr);
28094d57b86dSChao Yu int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
28104d57b86dSChao Yu void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count);
2811c4020b2dSChao Yu int f2fs_precache_extents(struct inode *inode);
2812cac5a3d8SDongOh Shin long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
2813cac5a3d8SDongOh Shin long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
28141ad71a27SJaegeuk Kim int f2fs_pin_file_control(struct inode *inode, bool inc);
281539a53e0cSJaegeuk Kim 
281639a53e0cSJaegeuk Kim /*
281739a53e0cSJaegeuk Kim  * inode.c
281839a53e0cSJaegeuk Kim  */
2819cac5a3d8SDongOh Shin void f2fs_set_inode_flags(struct inode *inode);
2820704956ecSChao Yu bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page);
2821704956ecSChao Yu void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page);
2822cac5a3d8SDongOh Shin struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
2823cac5a3d8SDongOh Shin struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
28244d57b86dSChao Yu int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
28254d57b86dSChao Yu void f2fs_update_inode(struct inode *inode, struct page *node_page);
28264d57b86dSChao Yu void f2fs_update_inode_page(struct inode *inode);
2827cac5a3d8SDongOh Shin int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
2828cac5a3d8SDongOh Shin void f2fs_evict_inode(struct inode *inode);
28294d57b86dSChao Yu void f2fs_handle_failed_inode(struct inode *inode);
283039a53e0cSJaegeuk Kim 
283139a53e0cSJaegeuk Kim /*
283239a53e0cSJaegeuk Kim  * namei.c
283339a53e0cSJaegeuk Kim  */
28344d57b86dSChao Yu int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
2835b6a06cbbSChao Yu 							bool hot, bool set);
283639a53e0cSJaegeuk Kim struct dentry *f2fs_get_parent(struct dentry *child);
283739a53e0cSJaegeuk Kim 
283839a53e0cSJaegeuk Kim /*
283939a53e0cSJaegeuk Kim  * dir.c
284039a53e0cSJaegeuk Kim  */
28414d57b86dSChao Yu unsigned char f2fs_get_de_type(struct f2fs_dir_entry *de);
28424d57b86dSChao Yu struct f2fs_dir_entry *f2fs_find_target_dentry(struct fscrypt_name *fname,
2843cac5a3d8SDongOh Shin 			f2fs_hash_t namehash, int *max_slots,
2844cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
2845cac5a3d8SDongOh Shin int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
2846cac5a3d8SDongOh Shin 			unsigned int start_pos, struct fscrypt_str *fstr);
28474d57b86dSChao Yu void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
2848cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
28494d57b86dSChao Yu struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
2850cac5a3d8SDongOh Shin 			const struct qstr *new_name,
2851cac5a3d8SDongOh Shin 			const struct qstr *orig_name, struct page *dpage);
28524d57b86dSChao Yu void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
2853cac5a3d8SDongOh Shin 			unsigned int current_depth);
28544d57b86dSChao Yu int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots);
2855cac5a3d8SDongOh Shin void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
2856cac5a3d8SDongOh Shin struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
2857cac5a3d8SDongOh Shin 			struct fscrypt_name *fname, struct page **res_page);
2858cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
2859cac5a3d8SDongOh Shin 			const struct qstr *child, struct page **res_page);
2860cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
2861cac5a3d8SDongOh Shin ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
2862cac5a3d8SDongOh Shin 			struct page **page);
2863cac5a3d8SDongOh Shin void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
2864cac5a3d8SDongOh Shin 			struct page *page, struct inode *inode);
2865cac5a3d8SDongOh Shin void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
2866cac5a3d8SDongOh Shin 			const struct qstr *name, f2fs_hash_t name_hash,
2867cac5a3d8SDongOh Shin 			unsigned int bit_pos);
2868cac5a3d8SDongOh Shin int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
2869cac5a3d8SDongOh Shin 			const struct qstr *orig_name,
2870cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
28714d57b86dSChao Yu int f2fs_add_dentry(struct inode *dir, struct fscrypt_name *fname,
2872cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
28734d57b86dSChao Yu int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
2874cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
2875cac5a3d8SDongOh Shin void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
2876cac5a3d8SDongOh Shin 			struct inode *dir, struct inode *inode);
2877cac5a3d8SDongOh Shin int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
2878cac5a3d8SDongOh Shin bool f2fs_empty_dir(struct inode *dir);
287939a53e0cSJaegeuk Kim 
2880b7f7a5e0SAl Viro static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
2881b7f7a5e0SAl Viro {
28824d57b86dSChao Yu 	return f2fs_do_add_link(d_inode(dentry->d_parent), &dentry->d_name,
2883510022a8SJaegeuk Kim 				inode, inode->i_ino, inode->i_mode);
2884b7f7a5e0SAl Viro }
2885b7f7a5e0SAl Viro 
288639a53e0cSJaegeuk Kim /*
288739a53e0cSJaegeuk Kim  * super.c
288839a53e0cSJaegeuk Kim  */
2889cac5a3d8SDongOh Shin int f2fs_inode_dirtied(struct inode *inode, bool sync);
2890cac5a3d8SDongOh Shin void f2fs_inode_synced(struct inode *inode);
2891ea676733SJaegeuk Kim int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly);
2892*af033b2aSChao Yu int f2fs_quota_sync(struct super_block *sb, int type);
28934b2414d0SChao Yu void f2fs_quota_off_umount(struct super_block *sb);
2894cac5a3d8SDongOh Shin int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
2895cac5a3d8SDongOh Shin int f2fs_sync_fs(struct super_block *sb, int sync);
2896a07ef784SNamjae Jeon extern __printf(3, 4)
2897cac5a3d8SDongOh Shin void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...);
28984d57b86dSChao Yu int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi);
289939a53e0cSJaegeuk Kim 
290039a53e0cSJaegeuk Kim /*
290139a53e0cSJaegeuk Kim  * hash.c
290239a53e0cSJaegeuk Kim  */
29036332cd32SJaegeuk Kim f2fs_hash_t f2fs_dentry_hash(const struct qstr *name_info,
29046332cd32SJaegeuk Kim 				struct fscrypt_name *fname);
290539a53e0cSJaegeuk Kim 
290639a53e0cSJaegeuk Kim /*
290739a53e0cSJaegeuk Kim  * node.c
290839a53e0cSJaegeuk Kim  */
290939a53e0cSJaegeuk Kim struct dnode_of_data;
291039a53e0cSJaegeuk Kim struct node_info;
291139a53e0cSJaegeuk Kim 
29124d57b86dSChao Yu int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid);
29134d57b86dSChao Yu bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type);
291450fa53ecSChao Yu bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct page *page);
291550fa53ecSChao Yu void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi);
291650fa53ecSChao Yu void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct page *page);
291750fa53ecSChao Yu void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi);
29184d57b86dSChao Yu int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
29194d57b86dSChao Yu bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
29204d57b86dSChao Yu bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
29217735730dSChao Yu int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
29224d57b86dSChao Yu 						struct node_info *ni);
29234d57b86dSChao Yu pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
29244d57b86dSChao Yu int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
29254d57b86dSChao Yu int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from);
29264d57b86dSChao Yu int f2fs_truncate_xattr_node(struct inode *inode);
292750fa53ecSChao Yu int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
292850fa53ecSChao Yu 					unsigned int seq_id);
29294d57b86dSChao Yu int f2fs_remove_inode_page(struct inode *inode);
29304d57b86dSChao Yu struct page *f2fs_new_inode_page(struct inode *inode);
29314d57b86dSChao Yu struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs);
29324d57b86dSChao Yu void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
29334d57b86dSChao Yu struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
29344d57b86dSChao Yu struct page *f2fs_get_node_page_ra(struct page *parent, int start);
293548018b4cSChao Yu int f2fs_move_node_page(struct page *node_page, int gc_type);
29364d57b86dSChao Yu int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
293750fa53ecSChao Yu 			struct writeback_control *wbc, bool atomic,
293850fa53ecSChao Yu 			unsigned int *seq_id);
29394d57b86dSChao Yu int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
29404d57b86dSChao Yu 			struct writeback_control *wbc,
2941b0af6d49SChao Yu 			bool do_balance, enum iostat_type io_type);
2942e2374015SChao Yu int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
29434d57b86dSChao Yu bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
29444d57b86dSChao Yu void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
29454d57b86dSChao Yu void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
29464d57b86dSChao Yu int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
29474d57b86dSChao Yu void f2fs_recover_inline_xattr(struct inode *inode, struct page *page);
29484d57b86dSChao Yu int f2fs_recover_xattr_data(struct inode *inode, struct page *page);
29494d57b86dSChao Yu int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
29507735730dSChao Yu int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
2951cac5a3d8SDongOh Shin 			unsigned int segno, struct f2fs_summary_block *sum);
2952edc55aafSJaegeuk Kim int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
29534d57b86dSChao Yu int f2fs_build_node_manager(struct f2fs_sb_info *sbi);
29544d57b86dSChao Yu void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi);
29554d57b86dSChao Yu int __init f2fs_create_node_manager_caches(void);
29564d57b86dSChao Yu void f2fs_destroy_node_manager_caches(void);
295739a53e0cSJaegeuk Kim 
295839a53e0cSJaegeuk Kim /*
295939a53e0cSJaegeuk Kim  * segment.c
296039a53e0cSJaegeuk Kim  */
29614d57b86dSChao Yu bool f2fs_need_SSR(struct f2fs_sb_info *sbi);
29624d57b86dSChao Yu void f2fs_register_inmem_page(struct inode *inode, struct page *page);
29634d57b86dSChao Yu void f2fs_drop_inmem_pages_all(struct f2fs_sb_info *sbi, bool gc_failure);
29644d57b86dSChao Yu void f2fs_drop_inmem_pages(struct inode *inode);
29654d57b86dSChao Yu void f2fs_drop_inmem_page(struct inode *inode, struct page *page);
29664d57b86dSChao Yu int f2fs_commit_inmem_pages(struct inode *inode);
2967cac5a3d8SDongOh Shin void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
2968cac5a3d8SDongOh Shin void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi);
296939d787beSChao Yu int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
29704d57b86dSChao Yu int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
29711228b482SChao Yu int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
29724d57b86dSChao Yu void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
29734d57b86dSChao Yu void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
29744d57b86dSChao Yu bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
29754d57b86dSChao Yu void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
29764d57b86dSChao Yu void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi);
2977cf5c759fSChao Yu bool f2fs_wait_discard_bios(struct f2fs_sb_info *sbi);
29784d57b86dSChao Yu void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
29794d57b86dSChao Yu 					struct cp_control *cpc);
29804354994fSDaniel Rosenberg void f2fs_dirty_to_prefree(struct f2fs_sb_info *sbi);
29814354994fSDaniel Rosenberg int f2fs_disable_cp_again(struct f2fs_sb_info *sbi);
29824d57b86dSChao Yu void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi);
29834d57b86dSChao Yu int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
29844d57b86dSChao Yu void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi);
2985cac5a3d8SDongOh Shin int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
29864d57b86dSChao Yu bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
29874d57b86dSChao Yu 					struct cp_control *cpc);
29884d57b86dSChao Yu struct page *f2fs_get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
29894d57b86dSChao Yu void f2fs_update_meta_page(struct f2fs_sb_info *sbi, void *src,
29904d57b86dSChao Yu 					block_t blk_addr);
29914d57b86dSChao Yu void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
2992b0af6d49SChao Yu 						enum iostat_type io_type);
29934d57b86dSChao Yu void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio);
29944d57b86dSChao Yu void f2fs_outplace_write_data(struct dnode_of_data *dn,
29954d57b86dSChao Yu 			struct f2fs_io_info *fio);
29964d57b86dSChao Yu int f2fs_inplace_write_data(struct f2fs_io_info *fio);
29974d57b86dSChao Yu void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
2998cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t new_blkaddr,
2999cac5a3d8SDongOh Shin 			bool recover_curseg, bool recover_newaddr);
3000cac5a3d8SDongOh Shin void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
3001cac5a3d8SDongOh Shin 			block_t old_addr, block_t new_addr,
3002cac5a3d8SDongOh Shin 			unsigned char version, bool recover_curseg,
3003cac5a3d8SDongOh Shin 			bool recover_newaddr);
30044d57b86dSChao Yu void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
3005cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t *new_blkaddr,
3006fb830fc5SChao Yu 			struct f2fs_summary *sum, int type,
3007fb830fc5SChao Yu 			struct f2fs_io_info *fio, bool add_list);
3008cac5a3d8SDongOh Shin void f2fs_wait_on_page_writeback(struct page *page,
3009cac5a3d8SDongOh Shin 			enum page_type type, bool ordered);
30100ded69f6SJaegeuk Kim void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr);
30111e78e8bdSSahitya Tummala void f2fs_wait_on_block_writeback_range(struct inode *inode, block_t blkaddr,
30121e78e8bdSSahitya Tummala 								block_t len);
30134d57b86dSChao Yu void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
30144d57b86dSChao Yu void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
30154d57b86dSChao Yu int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
3016cac5a3d8SDongOh Shin 			unsigned int val, int alloc);
30174d57b86dSChao Yu void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
30184d57b86dSChao Yu int f2fs_build_segment_manager(struct f2fs_sb_info *sbi);
30194d57b86dSChao Yu void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi);
30204d57b86dSChao Yu int __init f2fs_create_segment_manager_caches(void);
30214d57b86dSChao Yu void f2fs_destroy_segment_manager_caches(void);
30224d57b86dSChao Yu int f2fs_rw_hint_to_seg_type(enum rw_hint hint);
30234d57b86dSChao Yu enum rw_hint f2fs_io_type_to_rw_hint(struct f2fs_sb_info *sbi,
30244d57b86dSChao Yu 			enum page_type type, enum temp_type temp);
302539a53e0cSJaegeuk Kim 
302639a53e0cSJaegeuk Kim /*
302739a53e0cSJaegeuk Kim  * checkpoint.c
302839a53e0cSJaegeuk Kim  */
3029cac5a3d8SDongOh Shin void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io);
30304d57b86dSChao Yu struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
30314d57b86dSChao Yu struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
30327735730dSChao Yu struct page *f2fs_get_meta_page_nofail(struct f2fs_sb_info *sbi, pgoff_t index);
30334d57b86dSChao Yu struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
3034e1da7872SChao Yu bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
30354d57b86dSChao Yu 					block_t blkaddr, int type);
30364d57b86dSChao Yu int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
3037cac5a3d8SDongOh Shin 			int type, bool sync);
30384d57b86dSChao Yu void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index);
30394d57b86dSChao Yu long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
3040b0af6d49SChao Yu 			long nr_to_write, enum iostat_type io_type);
30414d57b86dSChao Yu void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
30424d57b86dSChao Yu void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
30434d57b86dSChao Yu void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all);
30444d57b86dSChao Yu bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
30454d57b86dSChao Yu void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
304639d787beSChao Yu 					unsigned int devidx, int type);
30474d57b86dSChao Yu bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
304839d787beSChao Yu 					unsigned int devidx, int type);
3049cac5a3d8SDongOh Shin int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
30504d57b86dSChao Yu int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi);
30514d57b86dSChao Yu void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi);
30524d57b86dSChao Yu void f2fs_add_orphan_inode(struct inode *inode);
30534d57b86dSChao Yu void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
30544d57b86dSChao Yu int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi);
30554d57b86dSChao Yu int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi);
30564d57b86dSChao Yu void f2fs_update_dirty_page(struct inode *inode, struct page *page);
30574d57b86dSChao Yu void f2fs_remove_dirty_inode(struct inode *inode);
30584d57b86dSChao Yu int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
305950fa53ecSChao Yu void f2fs_wait_on_all_pages_writeback(struct f2fs_sb_info *sbi);
30604d57b86dSChao Yu int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
30614d57b86dSChao Yu void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi);
30624d57b86dSChao Yu int __init f2fs_create_checkpoint_caches(void);
30634d57b86dSChao Yu void f2fs_destroy_checkpoint_caches(void);
306439a53e0cSJaegeuk Kim 
306539a53e0cSJaegeuk Kim /*
306639a53e0cSJaegeuk Kim  * data.c
306739a53e0cSJaegeuk Kim  */
30686dbb1796SEric Biggers int f2fs_init_post_read_processing(void);
30696dbb1796SEric Biggers void f2fs_destroy_post_read_processing(void);
3070b9109b0eSJaegeuk Kim void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
3071b9109b0eSJaegeuk Kim void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
3072bab475c5SChao Yu 				struct inode *inode, struct page *page,
3073bab475c5SChao Yu 				nid_t ino, enum page_type type);
3074b9109b0eSJaegeuk Kim void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
3075cac5a3d8SDongOh Shin int f2fs_submit_page_bio(struct f2fs_io_info *fio);
3076fe16efe6SChao Yu void f2fs_submit_page_write(struct f2fs_io_info *fio);
3077cac5a3d8SDongOh Shin struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
3078cac5a3d8SDongOh Shin 			block_t blk_addr, struct bio *bio);
3079cac5a3d8SDongOh Shin int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
30804d57b86dSChao Yu void f2fs_set_data_blkaddr(struct dnode_of_data *dn);
3081cac5a3d8SDongOh Shin void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
30824d57b86dSChao Yu int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
30834d57b86dSChao Yu int f2fs_reserve_new_block(struct dnode_of_data *dn);
3084cac5a3d8SDongOh Shin int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
3085cac5a3d8SDongOh Shin int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from);
3086cac5a3d8SDongOh Shin int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
30874d57b86dSChao Yu struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
3088cac5a3d8SDongOh Shin 			int op_flags, bool for_write);
30894d57b86dSChao Yu struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index);
30904d57b86dSChao Yu struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
3091cac5a3d8SDongOh Shin 			bool for_write);
30924d57b86dSChao Yu struct page *f2fs_get_new_data_page(struct inode *inode,
3093cac5a3d8SDongOh Shin 			struct page *ipage, pgoff_t index, bool new_i_size);
30944d57b86dSChao Yu int f2fs_do_write_data_page(struct f2fs_io_info *fio);
309539a86958SChao Yu void __do_map_lock(struct f2fs_sb_info *sbi, int flag, bool lock);
3096cac5a3d8SDongOh Shin int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
3097cac5a3d8SDongOh Shin 			int create, int flag);
3098cac5a3d8SDongOh Shin int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3099cac5a3d8SDongOh Shin 			u64 start, u64 len);
31004d57b86dSChao Yu bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio);
31014d57b86dSChao Yu bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio);
3102cac5a3d8SDongOh Shin void f2fs_invalidate_page(struct page *page, unsigned int offset,
3103cac5a3d8SDongOh Shin 			unsigned int length);
3104cac5a3d8SDongOh Shin int f2fs_release_page(struct page *page, gfp_t wait);
31055b7a487cSWeichao Guo #ifdef CONFIG_MIGRATION
3106cac5a3d8SDongOh Shin int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,
3107cac5a3d8SDongOh Shin 			struct page *page, enum migrate_mode mode);
31085b7a487cSWeichao Guo #endif
3109b91050a8SHyunchul Lee bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len);
31104d57b86dSChao Yu void f2fs_clear_radix_tree_dirty_tag(struct page *page);
311139a53e0cSJaegeuk Kim 
311239a53e0cSJaegeuk Kim /*
311339a53e0cSJaegeuk Kim  * gc.c
311439a53e0cSJaegeuk Kim  */
31154d57b86dSChao Yu int f2fs_start_gc_thread(struct f2fs_sb_info *sbi);
31164d57b86dSChao Yu void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi);
31174d57b86dSChao Yu block_t f2fs_start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
3118e066b83cSJaegeuk Kim int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background,
3119e066b83cSJaegeuk Kim 			unsigned int segno);
31204d57b86dSChao Yu void f2fs_build_gc_manager(struct f2fs_sb_info *sbi);
312139a53e0cSJaegeuk Kim 
312239a53e0cSJaegeuk Kim /*
312339a53e0cSJaegeuk Kim  * recovery.c
312439a53e0cSJaegeuk Kim  */
31254d57b86dSChao Yu int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
31264d57b86dSChao Yu bool f2fs_space_for_roll_forward(struct f2fs_sb_info *sbi);
312739a53e0cSJaegeuk Kim 
312839a53e0cSJaegeuk Kim /*
312939a53e0cSJaegeuk Kim  * debug.c
313039a53e0cSJaegeuk Kim  */
313139a53e0cSJaegeuk Kim #ifdef CONFIG_F2FS_STAT_FS
313239a53e0cSJaegeuk Kim struct f2fs_stat_info {
313339a53e0cSJaegeuk Kim 	struct list_head stat_list;
313439a53e0cSJaegeuk Kim 	struct f2fs_sb_info *sbi;
313539a53e0cSJaegeuk Kim 	int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
313639a53e0cSJaegeuk Kim 	int main_area_segs, main_area_sections, main_area_zones;
31375b7ee374SChao Yu 	unsigned long long hit_largest, hit_cached, hit_rbtree;
31385b7ee374SChao Yu 	unsigned long long hit_total, total_ext;
3139c00ba554SJaegeuk Kim 	int ext_tree, zombie_tree, ext_node;
31402c8a4a28SJaegeuk Kim 	int ndirty_node, ndirty_dent, ndirty_meta, ndirty_imeta;
31412c8a4a28SJaegeuk Kim 	int ndirty_data, ndirty_qdata;
314235782b23SJaegeuk Kim 	int inmem_pages;
31432c8a4a28SJaegeuk Kim 	unsigned int ndirty_dirs, ndirty_files, nquota_files, ndirty_all;
31445b0ef73cSJaegeuk Kim 	int nats, dirty_nats, sits, dirty_sits;
31455b0ef73cSJaegeuk Kim 	int free_nids, avail_nids, alloc_nids;
314639a53e0cSJaegeuk Kim 	int total_count, utilization;
31478b8dd65fSChao Yu 	int bg_gc, nr_wb_cp_data, nr_wb_data;
31485f9abab4SJaegeuk Kim 	int nr_rd_data, nr_rd_node, nr_rd_meta;
3149274bd9baSChao Yu 	unsigned int io_skip_bggc, other_skip_bggc;
315014d8d5f7SChao Yu 	int nr_flushing, nr_flushed, flush_list_empty;
315114d8d5f7SChao Yu 	int nr_discarding, nr_discarded;
31525f32366aSChao Yu 	int nr_discard_cmd;
3153d84d1cbdSChao Yu 	unsigned int undiscard_blks;
3154a00861dbSJaegeuk Kim 	int inline_xattr, inline_inode, inline_dir, append, update, orphans;
3155648d50baSChao Yu 	int aw_cnt, max_aw_cnt, vw_cnt, max_vw_cnt;
3156f83a2584SYunlei He 	unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
315739a53e0cSJaegeuk Kim 	unsigned int bimodal, avg_vblocks;
315839a53e0cSJaegeuk Kim 	int util_free, util_valid, util_invalid;
315939a53e0cSJaegeuk Kim 	int rsvd_segs, overp_segs;
316039a53e0cSJaegeuk Kim 	int dirty_count, node_pages, meta_pages;
316142190d2aSJaegeuk Kim 	int prefree_count, call_count, cp_count, bg_cp_count;
316239a53e0cSJaegeuk Kim 	int tot_segs, node_segs, data_segs, free_segs, free_secs;
3163e1235983SChangman Lee 	int bg_node_segs, bg_data_segs;
316439a53e0cSJaegeuk Kim 	int tot_blks, data_blks, node_blks;
3165e1235983SChangman Lee 	int bg_data_blks, bg_node_blks;
31662ef79ecbSChao Yu 	unsigned long long skipped_atomic_files[2];
316739a53e0cSJaegeuk Kim 	int curseg[NR_CURSEG_TYPE];
316839a53e0cSJaegeuk Kim 	int cursec[NR_CURSEG_TYPE];
316939a53e0cSJaegeuk Kim 	int curzone[NR_CURSEG_TYPE];
317039a53e0cSJaegeuk Kim 
3171b63e7be5SChao Yu 	unsigned int meta_count[META_MAX];
317239a53e0cSJaegeuk Kim 	unsigned int segment_count[2];
317339a53e0cSJaegeuk Kim 	unsigned int block_count[2];
3174b9a2c252SChangman Lee 	unsigned int inplace_count;
31759edcdabfSChao Yu 	unsigned long long base_mem, cache_mem, page_mem;
317639a53e0cSJaegeuk Kim };
317739a53e0cSJaegeuk Kim 
3178963d4f7dSGu Zheng static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
3179963d4f7dSGu Zheng {
3180963d4f7dSGu Zheng 	return (struct f2fs_stat_info *)sbi->stat_info;
3181963d4f7dSGu Zheng }
3182963d4f7dSGu Zheng 
3183942e0be6SChangman Lee #define stat_inc_cp_count(si)		((si)->cp_count++)
318442190d2aSJaegeuk Kim #define stat_inc_bg_cp_count(si)	((si)->bg_cp_count++)
318539a53e0cSJaegeuk Kim #define stat_inc_call_count(si)		((si)->call_count++)
3186dcdfff65SJaegeuk Kim #define stat_inc_bggc_count(sbi)	((sbi)->bg_gc++)
3187274bd9baSChao Yu #define stat_io_skip_bggc_count(sbi)	((sbi)->io_skip_bggc++)
3188274bd9baSChao Yu #define stat_other_skip_bggc_count(sbi)	((sbi)->other_skip_bggc++)
318933fbd510SChao Yu #define stat_inc_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]++)
319033fbd510SChao Yu #define stat_dec_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]--)
31915b7ee374SChao Yu #define stat_inc_total_hit(sbi)		(atomic64_inc(&(sbi)->total_hit_ext))
31925b7ee374SChao Yu #define stat_inc_rbtree_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_rbtree))
31935b7ee374SChao Yu #define stat_inc_largest_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_largest))
31945b7ee374SChao Yu #define stat_inc_cached_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_cached))
3195d5e8f6c9SChao Yu #define stat_inc_inline_xattr(inode)					\
3196d5e8f6c9SChao Yu 	do {								\
3197d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3198d5e8f6c9SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_xattr));	\
3199d5e8f6c9SChao Yu 	} while (0)
3200d5e8f6c9SChao Yu #define stat_dec_inline_xattr(inode)					\
3201d5e8f6c9SChao Yu 	do {								\
3202d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3203d5e8f6c9SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_xattr));	\
3204d5e8f6c9SChao Yu 	} while (0)
32050dbdc2aeSJaegeuk Kim #define stat_inc_inline_inode(inode)					\
32060dbdc2aeSJaegeuk Kim 	do {								\
32070dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
320803e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_inode));	\
32090dbdc2aeSJaegeuk Kim 	} while (0)
32100dbdc2aeSJaegeuk Kim #define stat_dec_inline_inode(inode)					\
32110dbdc2aeSJaegeuk Kim 	do {								\
32120dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
321303e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_inode));	\
32140dbdc2aeSJaegeuk Kim 	} while (0)
32153289c061SJaegeuk Kim #define stat_inc_inline_dir(inode)					\
32163289c061SJaegeuk Kim 	do {								\
32173289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
321803e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_dir));	\
32193289c061SJaegeuk Kim 	} while (0)
32203289c061SJaegeuk Kim #define stat_dec_inline_dir(inode)					\
32213289c061SJaegeuk Kim 	do {								\
32223289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
322303e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_dir));	\
32243289c061SJaegeuk Kim 	} while (0)
3225b63e7be5SChao Yu #define stat_inc_meta_count(sbi, blkaddr)				\
3226b63e7be5SChao Yu 	do {								\
3227b63e7be5SChao Yu 		if (blkaddr < SIT_I(sbi)->sit_base_addr)		\
3228b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_CP]);	\
3229b63e7be5SChao Yu 		else if (blkaddr < NM_I(sbi)->nat_blkaddr)		\
3230b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_SIT]);	\
3231b63e7be5SChao Yu 		else if (blkaddr < SM_I(sbi)->ssa_blkaddr)		\
3232b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_NAT]);	\
3233b63e7be5SChao Yu 		else if (blkaddr < SM_I(sbi)->main_blkaddr)		\
3234b63e7be5SChao Yu 			atomic_inc(&(sbi)->meta_count[META_SSA]);	\
3235b63e7be5SChao Yu 	} while (0)
3236dcdfff65SJaegeuk Kim #define stat_inc_seg_type(sbi, curseg)					\
3237dcdfff65SJaegeuk Kim 		((sbi)->segment_count[(curseg)->alloc_type]++)
3238dcdfff65SJaegeuk Kim #define stat_inc_block_count(sbi, curseg)				\
3239dcdfff65SJaegeuk Kim 		((sbi)->block_count[(curseg)->alloc_type]++)
3240b9a2c252SChangman Lee #define stat_inc_inplace_blocks(sbi)					\
3241b9a2c252SChangman Lee 		(atomic_inc(&(sbi)->inplace_count))
324226a28a0cSJaegeuk Kim #define stat_inc_atomic_write(inode)					\
3243cac5a3d8SDongOh Shin 		(atomic_inc(&F2FS_I_SB(inode)->aw_cnt))
324426a28a0cSJaegeuk Kim #define stat_dec_atomic_write(inode)					\
3245cac5a3d8SDongOh Shin 		(atomic_dec(&F2FS_I_SB(inode)->aw_cnt))
324626a28a0cSJaegeuk Kim #define stat_update_max_atomic_write(inode)				\
324726a28a0cSJaegeuk Kim 	do {								\
324826a28a0cSJaegeuk Kim 		int cur = atomic_read(&F2FS_I_SB(inode)->aw_cnt);	\
324926a28a0cSJaegeuk Kim 		int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt);	\
325026a28a0cSJaegeuk Kim 		if (cur > max)						\
325126a28a0cSJaegeuk Kim 			atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur);	\
325226a28a0cSJaegeuk Kim 	} while (0)
3253648d50baSChao Yu #define stat_inc_volatile_write(inode)					\
3254648d50baSChao Yu 		(atomic_inc(&F2FS_I_SB(inode)->vw_cnt))
3255648d50baSChao Yu #define stat_dec_volatile_write(inode)					\
3256648d50baSChao Yu 		(atomic_dec(&F2FS_I_SB(inode)->vw_cnt))
3257648d50baSChao Yu #define stat_update_max_volatile_write(inode)				\
3258648d50baSChao Yu 	do {								\
3259648d50baSChao Yu 		int cur = atomic_read(&F2FS_I_SB(inode)->vw_cnt);	\
3260648d50baSChao Yu 		int max = atomic_read(&F2FS_I_SB(inode)->max_vw_cnt);	\
3261648d50baSChao Yu 		if (cur > max)						\
3262648d50baSChao Yu 			atomic_set(&F2FS_I_SB(inode)->max_vw_cnt, cur);	\
3263648d50baSChao Yu 	} while (0)
3264e1235983SChangman Lee #define stat_inc_seg_count(sbi, type, gc_type)				\
326539a53e0cSJaegeuk Kim 	do {								\
3266963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
326768afcf2dSTomohiro Kusumi 		si->tot_segs++;						\
326868afcf2dSTomohiro Kusumi 		if ((type) == SUM_TYPE_DATA) {				\
326939a53e0cSJaegeuk Kim 			si->data_segs++;				\
3270e1235983SChangman Lee 			si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0;	\
3271e1235983SChangman Lee 		} else {						\
327239a53e0cSJaegeuk Kim 			si->node_segs++;				\
3273e1235983SChangman Lee 			si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0;	\
3274e1235983SChangman Lee 		}							\
327539a53e0cSJaegeuk Kim 	} while (0)
327639a53e0cSJaegeuk Kim 
327739a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks)				\
327868afcf2dSTomohiro Kusumi 	((si)->tot_blks += (blks))
327939a53e0cSJaegeuk Kim 
3280e1235983SChangman Lee #define stat_inc_data_blk_count(sbi, blks, gc_type)			\
328139a53e0cSJaegeuk Kim 	do {								\
3282963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
328339a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
328439a53e0cSJaegeuk Kim 		si->data_blks += (blks);				\
328568afcf2dSTomohiro Kusumi 		si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
328639a53e0cSJaegeuk Kim 	} while (0)
328739a53e0cSJaegeuk Kim 
3288e1235983SChangman Lee #define stat_inc_node_blk_count(sbi, blks, gc_type)			\
328939a53e0cSJaegeuk Kim 	do {								\
3290963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
329139a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
329239a53e0cSJaegeuk Kim 		si->node_blks += (blks);				\
329368afcf2dSTomohiro Kusumi 		si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
329439a53e0cSJaegeuk Kim 	} while (0)
329539a53e0cSJaegeuk Kim 
3296cac5a3d8SDongOh Shin int f2fs_build_stats(struct f2fs_sb_info *sbi);
3297cac5a3d8SDongOh Shin void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
3298787c7b8cSChao Yu int __init f2fs_create_root_stats(void);
32994589d25dSNamjae Jeon void f2fs_destroy_root_stats(void);
330039a53e0cSJaegeuk Kim #else
3301d66450e7SArnd Bergmann #define stat_inc_cp_count(si)				do { } while (0)
3302d66450e7SArnd Bergmann #define stat_inc_bg_cp_count(si)			do { } while (0)
3303d66450e7SArnd Bergmann #define stat_inc_call_count(si)				do { } while (0)
3304d66450e7SArnd Bergmann #define stat_inc_bggc_count(si)				do { } while (0)
3305274bd9baSChao Yu #define stat_io_skip_bggc_count(sbi)			do { } while (0)
3306274bd9baSChao Yu #define stat_other_skip_bggc_count(sbi)			do { } while (0)
3307d66450e7SArnd Bergmann #define stat_inc_dirty_inode(sbi, type)			do { } while (0)
3308d66450e7SArnd Bergmann #define stat_dec_dirty_inode(sbi, type)			do { } while (0)
3309d66450e7SArnd Bergmann #define stat_inc_total_hit(sb)				do { } while (0)
3310d66450e7SArnd Bergmann #define stat_inc_rbtree_node_hit(sb)			do { } while (0)
3311d66450e7SArnd Bergmann #define stat_inc_largest_node_hit(sbi)			do { } while (0)
3312d66450e7SArnd Bergmann #define stat_inc_cached_node_hit(sbi)			do { } while (0)
3313d66450e7SArnd Bergmann #define stat_inc_inline_xattr(inode)			do { } while (0)
3314d66450e7SArnd Bergmann #define stat_dec_inline_xattr(inode)			do { } while (0)
3315d66450e7SArnd Bergmann #define stat_inc_inline_inode(inode)			do { } while (0)
3316d66450e7SArnd Bergmann #define stat_dec_inline_inode(inode)			do { } while (0)
3317d66450e7SArnd Bergmann #define stat_inc_inline_dir(inode)			do { } while (0)
3318d66450e7SArnd Bergmann #define stat_dec_inline_dir(inode)			do { } while (0)
3319d66450e7SArnd Bergmann #define stat_inc_atomic_write(inode)			do { } while (0)
3320d66450e7SArnd Bergmann #define stat_dec_atomic_write(inode)			do { } while (0)
3321d66450e7SArnd Bergmann #define stat_update_max_atomic_write(inode)		do { } while (0)
3322d66450e7SArnd Bergmann #define stat_inc_volatile_write(inode)			do { } while (0)
3323d66450e7SArnd Bergmann #define stat_dec_volatile_write(inode)			do { } while (0)
3324d66450e7SArnd Bergmann #define stat_update_max_volatile_write(inode)		do { } while (0)
3325b63e7be5SChao Yu #define stat_inc_meta_count(sbi, blkaddr)		do { } while (0)
3326d66450e7SArnd Bergmann #define stat_inc_seg_type(sbi, curseg)			do { } while (0)
3327d66450e7SArnd Bergmann #define stat_inc_block_count(sbi, curseg)		do { } while (0)
3328d66450e7SArnd Bergmann #define stat_inc_inplace_blocks(sbi)			do { } while (0)
3329d66450e7SArnd Bergmann #define stat_inc_seg_count(sbi, type, gc_type)		do { } while (0)
3330d66450e7SArnd Bergmann #define stat_inc_tot_blk_count(si, blks)		do { } while (0)
3331d66450e7SArnd Bergmann #define stat_inc_data_blk_count(sbi, blks, gc_type)	do { } while (0)
3332d66450e7SArnd Bergmann #define stat_inc_node_blk_count(sbi, blks, gc_type)	do { } while (0)
333339a53e0cSJaegeuk Kim 
333439a53e0cSJaegeuk Kim static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
333539a53e0cSJaegeuk Kim static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
3336787c7b8cSChao Yu static inline int __init f2fs_create_root_stats(void) { return 0; }
33374589d25dSNamjae Jeon static inline void f2fs_destroy_root_stats(void) { }
333839a53e0cSJaegeuk Kim #endif
333939a53e0cSJaegeuk Kim 
334039a53e0cSJaegeuk Kim extern const struct file_operations f2fs_dir_operations;
334139a53e0cSJaegeuk Kim extern const struct file_operations f2fs_file_operations;
334239a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_file_inode_operations;
334339a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_dblock_aops;
334439a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_node_aops;
334539a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_meta_aops;
334639a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_dir_inode_operations;
334739a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_symlink_inode_operations;
3348cbaf042aSJaegeuk Kim extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
334939a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_special_inode_operations;
33504d57b86dSChao Yu extern struct kmem_cache *f2fs_inode_entry_slab;
33511001b347SHuajun Li 
3352e18c65b2SHuajun Li /*
3353e18c65b2SHuajun Li  * inline.c
3354e18c65b2SHuajun Li  */
3355cac5a3d8SDongOh Shin bool f2fs_may_inline_data(struct inode *inode);
3356cac5a3d8SDongOh Shin bool f2fs_may_inline_dentry(struct inode *inode);
33574d57b86dSChao Yu void f2fs_do_read_inline_data(struct page *page, struct page *ipage);
33584d57b86dSChao Yu void f2fs_truncate_inline_inode(struct inode *inode,
33594d57b86dSChao Yu 						struct page *ipage, u64 from);
3360cac5a3d8SDongOh Shin int f2fs_read_inline_data(struct inode *inode, struct page *page);
3361cac5a3d8SDongOh Shin int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
3362cac5a3d8SDongOh Shin int f2fs_convert_inline_inode(struct inode *inode);
3363cac5a3d8SDongOh Shin int f2fs_write_inline_data(struct inode *inode, struct page *page);
33644d57b86dSChao Yu bool f2fs_recover_inline_data(struct inode *inode, struct page *npage);
33654d57b86dSChao Yu struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
3366cac5a3d8SDongOh Shin 			struct fscrypt_name *fname, struct page **res_page);
33674d57b86dSChao Yu int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent,
3368cac5a3d8SDongOh Shin 			struct page *ipage);
3369cac5a3d8SDongOh Shin int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
3370cac5a3d8SDongOh Shin 			const struct qstr *orig_name,
3371cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
33724d57b86dSChao Yu void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry,
33734d57b86dSChao Yu 				struct page *page, struct inode *dir,
33744d57b86dSChao Yu 				struct inode *inode);
3375cac5a3d8SDongOh Shin bool f2fs_empty_inline_dir(struct inode *dir);
3376cac5a3d8SDongOh Shin int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
3377cac5a3d8SDongOh Shin 			struct fscrypt_str *fstr);
3378cac5a3d8SDongOh Shin int f2fs_inline_data_fiemap(struct inode *inode,
3379cac5a3d8SDongOh Shin 			struct fiemap_extent_info *fieinfo,
3380cac5a3d8SDongOh Shin 			__u64 start, __u64 len);
3381cde4de12SJaegeuk Kim 
3382cde4de12SJaegeuk Kim /*
33832658e50dSJaegeuk Kim  * shrinker.c
33842658e50dSJaegeuk Kim  */
3385cac5a3d8SDongOh Shin unsigned long f2fs_shrink_count(struct shrinker *shrink,
3386cac5a3d8SDongOh Shin 			struct shrink_control *sc);
3387cac5a3d8SDongOh Shin unsigned long f2fs_shrink_scan(struct shrinker *shrink,
3388cac5a3d8SDongOh Shin 			struct shrink_control *sc);
3389cac5a3d8SDongOh Shin void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
3390cac5a3d8SDongOh Shin void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
33912658e50dSJaegeuk Kim 
33922658e50dSJaegeuk Kim /*
3393a28ef1f5SChao Yu  * extent_cache.c
3394a28ef1f5SChao Yu  */
33954dada3fdSChao Yu struct rb_entry *f2fs_lookup_rb_tree(struct rb_root_cached *root,
3396004b6862SChao Yu 				struct rb_entry *cached_re, unsigned int ofs);
33974d57b86dSChao Yu struct rb_node **f2fs_lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
33984dada3fdSChao Yu 				struct rb_root_cached *root,
33994dada3fdSChao Yu 				struct rb_node **parent,
34004dada3fdSChao Yu 				unsigned int ofs, bool *leftmost);
34014dada3fdSChao Yu struct rb_entry *f2fs_lookup_rb_tree_ret(struct rb_root_cached *root,
3402004b6862SChao Yu 		struct rb_entry *cached_re, unsigned int ofs,
3403004b6862SChao Yu 		struct rb_entry **prev_entry, struct rb_entry **next_entry,
3404004b6862SChao Yu 		struct rb_node ***insert_p, struct rb_node **insert_parent,
34054dada3fdSChao Yu 		bool force, bool *leftmost);
34064d57b86dSChao Yu bool f2fs_check_rb_tree_consistence(struct f2fs_sb_info *sbi,
34074dada3fdSChao Yu 						struct rb_root_cached *root);
3408cac5a3d8SDongOh Shin unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
3409cac5a3d8SDongOh Shin bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext);
3410cac5a3d8SDongOh Shin void f2fs_drop_extent_tree(struct inode *inode);
3411cac5a3d8SDongOh Shin unsigned int f2fs_destroy_extent_node(struct inode *inode);
3412cac5a3d8SDongOh Shin void f2fs_destroy_extent_tree(struct inode *inode);
3413cac5a3d8SDongOh Shin bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
3414cac5a3d8SDongOh Shin 			struct extent_info *ei);
3415cac5a3d8SDongOh Shin void f2fs_update_extent_cache(struct dnode_of_data *dn);
341619b2c30dSChao Yu void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
3417cac5a3d8SDongOh Shin 			pgoff_t fofs, block_t blkaddr, unsigned int len);
34184d57b86dSChao Yu void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi);
34194d57b86dSChao Yu int __init f2fs_create_extent_cache(void);
34204d57b86dSChao Yu void f2fs_destroy_extent_cache(void);
3421a28ef1f5SChao Yu 
3422a28ef1f5SChao Yu /*
34238ceffcb2SChao Yu  * sysfs.c
34248ceffcb2SChao Yu  */
3425dc6b2055SJaegeuk Kim int __init f2fs_init_sysfs(void);
3426dc6b2055SJaegeuk Kim void f2fs_exit_sysfs(void);
3427dc6b2055SJaegeuk Kim int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
3428dc6b2055SJaegeuk Kim void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
34298ceffcb2SChao Yu 
34308ceffcb2SChao Yu /*
3431cde4de12SJaegeuk Kim  * crypto support
3432cde4de12SJaegeuk Kim  */
34330b81d077SJaegeuk Kim static inline bool f2fs_encrypted_inode(struct inode *inode)
3434cde4de12SJaegeuk Kim {
3435cde4de12SJaegeuk Kim 	return file_is_encrypt(inode);
3436cde4de12SJaegeuk Kim }
3437cde4de12SJaegeuk Kim 
34381958593eSJaegeuk Kim static inline bool f2fs_encrypted_file(struct inode *inode)
34391958593eSJaegeuk Kim {
34401958593eSJaegeuk Kim 	return f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode);
34411958593eSJaegeuk Kim }
34421958593eSJaegeuk Kim 
3443cde4de12SJaegeuk Kim static inline void f2fs_set_encrypted_inode(struct inode *inode)
3444cde4de12SJaegeuk Kim {
3445cde4de12SJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
3446cde4de12SJaegeuk Kim 	file_set_encrypt(inode);
34479149a5ebSChao Yu 	f2fs_set_inode_flags(inode);
3448cde4de12SJaegeuk Kim #endif
3449cde4de12SJaegeuk Kim }
3450cde4de12SJaegeuk Kim 
34516dbb1796SEric Biggers /*
34526dbb1796SEric Biggers  * Returns true if the reads of the inode's data need to undergo some
34536dbb1796SEric Biggers  * postprocessing step, like decryption or authenticity verification.
34546dbb1796SEric Biggers  */
34556dbb1796SEric Biggers static inline bool f2fs_post_read_required(struct inode *inode)
3456cde4de12SJaegeuk Kim {
34576dbb1796SEric Biggers 	return f2fs_encrypted_file(inode);
3458cde4de12SJaegeuk Kim }
3459cde4de12SJaegeuk Kim 
3460ccd31cb2SSheng Yong #define F2FS_FEATURE_FUNCS(name, flagname) \
3461ccd31cb2SSheng Yong static inline int f2fs_sb_has_##name(struct super_block *sb) \
3462ccd31cb2SSheng Yong { \
3463ccd31cb2SSheng Yong 	return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_##flagname); \
3464cde4de12SJaegeuk Kim }
3465f424f664SJaegeuk Kim 
3466ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(encrypt, ENCRYPT);
3467ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(blkzoned, BLKZONED);
3468ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(extra_attr, EXTRA_ATTR);
3469ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(project_quota, PRJQUOTA);
3470ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_chksum, INODE_CHKSUM);
3471ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
3472ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
3473ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
3474b7c409deSSheng Yong F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
3475d440c52dSJunling Zheng F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
34761c1d35dfSChao Yu 
3477178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
3478178053e2SDamien Le Moal static inline int get_blkz_type(struct f2fs_sb_info *sbi,
34793c62be17SJaegeuk Kim 			struct block_device *bdev, block_t blkaddr)
3480178053e2SDamien Le Moal {
3481178053e2SDamien Le Moal 	unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
34823c62be17SJaegeuk Kim 	int i;
3483178053e2SDamien Le Moal 
34843c62be17SJaegeuk Kim 	for (i = 0; i < sbi->s_ndevs; i++)
34853c62be17SJaegeuk Kim 		if (FDEV(i).bdev == bdev)
34863c62be17SJaegeuk Kim 			return FDEV(i).blkz_type[zno];
34873c62be17SJaegeuk Kim 	return -EINVAL;
3488178053e2SDamien Le Moal }
3489178053e2SDamien Le Moal #endif
3490178053e2SDamien Le Moal 
34917d20c8abSChao Yu static inline bool f2fs_hw_should_discard(struct f2fs_sb_info *sbi)
349296ba2decSDamien Le Moal {
34937d20c8abSChao Yu 	return f2fs_sb_has_blkzoned(sbi->sb);
34947d20c8abSChao Yu }
349596ba2decSDamien Le Moal 
34967d20c8abSChao Yu static inline bool f2fs_hw_support_discard(struct f2fs_sb_info *sbi)
34977d20c8abSChao Yu {
34987d20c8abSChao Yu 	return blk_queue_discard(bdev_get_queue(sbi->sb->s_bdev));
34997d20c8abSChao Yu }
35007d20c8abSChao Yu 
35017d20c8abSChao Yu static inline bool f2fs_realtime_discard_enable(struct f2fs_sb_info *sbi)
35027d20c8abSChao Yu {
35037d20c8abSChao Yu 	return (test_opt(sbi, DISCARD) && f2fs_hw_support_discard(sbi)) ||
35047d20c8abSChao Yu 					f2fs_hw_should_discard(sbi);
350552763a4bSJaegeuk Kim }
350652763a4bSJaegeuk Kim 
350752763a4bSJaegeuk Kim static inline void set_opt_mode(struct f2fs_sb_info *sbi, unsigned int mt)
350852763a4bSJaegeuk Kim {
350952763a4bSJaegeuk Kim 	clear_opt(sbi, ADAPTIVE);
351052763a4bSJaegeuk Kim 	clear_opt(sbi, LFS);
351152763a4bSJaegeuk Kim 
351252763a4bSJaegeuk Kim 	switch (mt) {
351352763a4bSJaegeuk Kim 	case F2FS_MOUNT_ADAPTIVE:
351452763a4bSJaegeuk Kim 		set_opt(sbi, ADAPTIVE);
351552763a4bSJaegeuk Kim 		break;
351652763a4bSJaegeuk Kim 	case F2FS_MOUNT_LFS:
351752763a4bSJaegeuk Kim 		set_opt(sbi, LFS);
351852763a4bSJaegeuk Kim 		break;
351952763a4bSJaegeuk Kim 	}
352052763a4bSJaegeuk Kim }
352152763a4bSJaegeuk Kim 
3522fcc85a4dSJaegeuk Kim static inline bool f2fs_may_encrypt(struct inode *inode)
3523fcc85a4dSJaegeuk Kim {
3524fcc85a4dSJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
3525886f56f9SAl Viro 	umode_t mode = inode->i_mode;
3526fcc85a4dSJaegeuk Kim 
3527fcc85a4dSJaegeuk Kim 	return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
3528fcc85a4dSJaegeuk Kim #else
35296e45f2a5SGustavo A. R. Silva 	return false;
3530fcc85a4dSJaegeuk Kim #endif
3531fcc85a4dSJaegeuk Kim }
3532fcc85a4dSJaegeuk Kim 
3533f847c699SChao Yu static inline int block_unaligned_IO(struct inode *inode,
3534f847c699SChao Yu 				struct kiocb *iocb, struct iov_iter *iter)
3535b91050a8SHyunchul Lee {
3536f847c699SChao Yu 	unsigned int i_blkbits = READ_ONCE(inode->i_blkbits);
3537f847c699SChao Yu 	unsigned int blocksize_mask = (1 << i_blkbits) - 1;
3538f847c699SChao Yu 	loff_t offset = iocb->ki_pos;
3539f847c699SChao Yu 	unsigned long align = offset | iov_iter_alignment(iter);
3540f847c699SChao Yu 
3541f847c699SChao Yu 	return align & blocksize_mask;
3542f847c699SChao Yu }
3543f847c699SChao Yu 
3544f847c699SChao Yu static inline int allow_outplace_dio(struct inode *inode,
3545f847c699SChao Yu 				struct kiocb *iocb, struct iov_iter *iter)
3546f847c699SChao Yu {
3547f847c699SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3548f847c699SChao Yu 	int rw = iov_iter_rw(iter);
3549f847c699SChao Yu 
3550f847c699SChao Yu 	return (test_opt(sbi, LFS) && (rw == WRITE) &&
3551f847c699SChao Yu 				!block_unaligned_IO(inode, iocb, iter));
3552f847c699SChao Yu }
3553f847c699SChao Yu 
3554f847c699SChao Yu static inline bool f2fs_force_buffered_io(struct inode *inode,
3555f847c699SChao Yu 				struct kiocb *iocb, struct iov_iter *iter)
3556f847c699SChao Yu {
3557f847c699SChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3558f847c699SChao Yu 	int rw = iov_iter_rw(iter);
3559f847c699SChao Yu 
3560f847c699SChao Yu 	if (f2fs_post_read_required(inode))
3561f847c699SChao Yu 		return true;
3562f847c699SChao Yu 	if (sbi->s_ndevs)
3563f847c699SChao Yu 		return true;
3564f847c699SChao Yu 	/*
3565f847c699SChao Yu 	 * for blkzoned device, fallback direct IO to buffered IO, so
3566f847c699SChao Yu 	 * all IOs can be serialized by log-structured write.
3567f847c699SChao Yu 	 */
3568f847c699SChao Yu 	if (f2fs_sb_has_blkzoned(sbi->sb))
3569f847c699SChao Yu 		return true;
3570f847c699SChao Yu 	if (test_opt(sbi, LFS) && (rw == WRITE) &&
3571f847c699SChao Yu 				block_unaligned_IO(inode, iocb, iter))
3572f847c699SChao Yu 		return true;
35734354994fSDaniel Rosenberg 	if (is_sbi_flag_set(F2FS_I_SB(inode), SBI_CP_DISABLED))
35744354994fSDaniel Rosenberg 		return true;
35754354994fSDaniel Rosenberg 
3576f847c699SChao Yu 	return false;
3577b91050a8SHyunchul Lee }
3578b91050a8SHyunchul Lee 
357983a3bfdbSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
3580d494500aSChao Yu extern void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate,
3581d494500aSChao Yu 							unsigned int type);
358283a3bfdbSJaegeuk Kim #else
3583d494500aSChao Yu #define f2fs_build_fault_attr(sbi, rate, type)		do { } while (0)
358483a3bfdbSJaegeuk Kim #endif
358583a3bfdbSJaegeuk Kim 
358639a53e0cSJaegeuk Kim #endif
3587*af033b2aSChao Yu 
3588*af033b2aSChao Yu static inline bool is_journalled_quota(struct f2fs_sb_info *sbi)
3589*af033b2aSChao Yu {
3590*af033b2aSChao Yu #ifdef CONFIG_QUOTA
3591*af033b2aSChao Yu 	if (f2fs_sb_has_quota_ino(sbi->sb))
3592*af033b2aSChao Yu 		return true;
3593*af033b2aSChao Yu 	if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
3594*af033b2aSChao Yu 		F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
3595*af033b2aSChao Yu 		F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
3596*af033b2aSChao Yu 		return true;
3597*af033b2aSChao Yu #endif
3598*af033b2aSChao Yu 	return false;
3599*af033b2aSChao Yu }
3600