xref: /openbmc/linux/fs/f2fs/f2fs.h (revision dbae2c551377b6533a00c11fc7ede370100ab404)
10a8165d7SJaegeuk Kim /*
239a53e0cSJaegeuk Kim  * fs/f2fs/f2fs.h
339a53e0cSJaegeuk Kim  *
439a53e0cSJaegeuk Kim  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
539a53e0cSJaegeuk Kim  *             http://www.samsung.com/
639a53e0cSJaegeuk Kim  *
739a53e0cSJaegeuk Kim  * This program is free software; you can redistribute it and/or modify
839a53e0cSJaegeuk Kim  * it under the terms of the GNU General Public License version 2 as
939a53e0cSJaegeuk Kim  * published by the Free Software Foundation.
1039a53e0cSJaegeuk Kim  */
1139a53e0cSJaegeuk Kim #ifndef _LINUX_F2FS_H
1239a53e0cSJaegeuk Kim #define _LINUX_F2FS_H
1339a53e0cSJaegeuk Kim 
1439a53e0cSJaegeuk Kim #include <linux/types.h>
1539a53e0cSJaegeuk Kim #include <linux/page-flags.h>
1639a53e0cSJaegeuk Kim #include <linux/buffer_head.h>
1739a53e0cSJaegeuk Kim #include <linux/slab.h>
1839a53e0cSJaegeuk Kim #include <linux/crc32.h>
1939a53e0cSJaegeuk Kim #include <linux/magic.h>
20c2d715d1SJaegeuk Kim #include <linux/kobject.h>
217bd59381SGu Zheng #include <linux/sched.h>
227c2e5963SJaegeuk Kim #include <linux/cred.h>
2339307a8eSJaegeuk Kim #include <linux/vmalloc.h>
24740432f8SJaegeuk Kim #include <linux/bio.h>
25d0239e1bSJaegeuk Kim #include <linux/blkdev.h>
260abd675eSChao Yu #include <linux/quotaops.h>
2743b6573bSKeith Mok #include <crypto/hash.h>
2839a53e0cSJaegeuk Kim 
29734f0d24SDave Chinner #define __FS_HAS_ENCRYPTION IS_ENABLED(CONFIG_F2FS_FS_ENCRYPTION)
30734f0d24SDave Chinner #include <linux/fscrypt.h>
31734f0d24SDave Chinner 
325d56b671SJaegeuk Kim #ifdef CONFIG_F2FS_CHECK_FS
339850cf4aSJaegeuk Kim #define f2fs_bug_on(sbi, condition)	BUG_ON(condition)
345d56b671SJaegeuk Kim #else
359850cf4aSJaegeuk Kim #define f2fs_bug_on(sbi, condition)					\
369850cf4aSJaegeuk Kim 	do {								\
379850cf4aSJaegeuk Kim 		if (unlikely(condition)) {				\
389850cf4aSJaegeuk Kim 			WARN_ON(1);					\
39caf0047eSChao Yu 			set_sbi_flag(sbi, SBI_NEED_FSCK);		\
409850cf4aSJaegeuk Kim 		}							\
419850cf4aSJaegeuk Kim 	} while (0)
425d56b671SJaegeuk Kim #endif
435d56b671SJaegeuk Kim 
442c63feadSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
452c63feadSJaegeuk Kim enum {
462c63feadSJaegeuk Kim 	FAULT_KMALLOC,
47628b3d14SChao Yu 	FAULT_KVMALLOC,
48c41f3cc3SJaegeuk Kim 	FAULT_PAGE_ALLOC,
4901eccef7SChao Yu 	FAULT_PAGE_GET,
50d62fe971SChao Yu 	FAULT_ALLOC_BIO,
51cb78942bSJaegeuk Kim 	FAULT_ALLOC_NID,
52cb78942bSJaegeuk Kim 	FAULT_ORPHAN,
53cb78942bSJaegeuk Kim 	FAULT_BLOCK,
54cb78942bSJaegeuk Kim 	FAULT_DIR_DEPTH,
5553aa6bbfSJaegeuk Kim 	FAULT_EVICT_INODE,
5614b44d23SJaegeuk Kim 	FAULT_TRUNCATE,
578b038c70SChao Yu 	FAULT_IO,
580f348028SChao Yu 	FAULT_CHECKPOINT,
592c63feadSJaegeuk Kim 	FAULT_MAX,
602c63feadSJaegeuk Kim };
612c63feadSJaegeuk Kim 
6208796897SSheng Yong struct f2fs_fault_info {
6308796897SSheng Yong 	atomic_t inject_ops;
6408796897SSheng Yong 	unsigned int inject_rate;
6508796897SSheng Yong 	unsigned int inject_type;
6608796897SSheng Yong };
6708796897SSheng Yong 
682c63feadSJaegeuk Kim extern char *fault_name[FAULT_MAX];
6968afcf2dSTomohiro Kusumi #define IS_FAULT_SET(fi, type) ((fi)->inject_type & (1 << (type)))
702c63feadSJaegeuk Kim #endif
712c63feadSJaegeuk Kim 
7239a53e0cSJaegeuk Kim /*
7339a53e0cSJaegeuk Kim  * For mount options
7439a53e0cSJaegeuk Kim  */
7539a53e0cSJaegeuk Kim #define F2FS_MOUNT_BG_GC		0x00000001
7639a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_ROLL_FORWARD	0x00000002
7739a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISCARD		0x00000004
7839a53e0cSJaegeuk Kim #define F2FS_MOUNT_NOHEAP		0x00000008
7939a53e0cSJaegeuk Kim #define F2FS_MOUNT_XATTR_USER		0x00000010
8039a53e0cSJaegeuk Kim #define F2FS_MOUNT_POSIX_ACL		0x00000020
8139a53e0cSJaegeuk Kim #define F2FS_MOUNT_DISABLE_EXT_IDENTIFY	0x00000040
82444c580fSJaegeuk Kim #define F2FS_MOUNT_INLINE_XATTR		0x00000080
831001b347SHuajun Li #define F2FS_MOUNT_INLINE_DATA		0x00000100
8434d67debSChao Yu #define F2FS_MOUNT_INLINE_DENTRY	0x00000200
8534d67debSChao Yu #define F2FS_MOUNT_FLUSH_MERGE		0x00000400
8634d67debSChao Yu #define F2FS_MOUNT_NOBARRIER		0x00000800
87d5053a34SJaegeuk Kim #define F2FS_MOUNT_FASTBOOT		0x00001000
8889672159SChao Yu #define F2FS_MOUNT_EXTENT_CACHE		0x00002000
896aefd93bSJaegeuk Kim #define F2FS_MOUNT_FORCE_FG_GC		0x00004000
90343f40f0SChao Yu #define F2FS_MOUNT_DATA_FLUSH		0x00008000
9173faec4dSJaegeuk Kim #define F2FS_MOUNT_FAULT_INJECTION	0x00010000
9236abef4eSJaegeuk Kim #define F2FS_MOUNT_ADAPTIVE		0x00020000
9336abef4eSJaegeuk Kim #define F2FS_MOUNT_LFS			0x00040000
940abd675eSChao Yu #define F2FS_MOUNT_USRQUOTA		0x00080000
950abd675eSChao Yu #define F2FS_MOUNT_GRPQUOTA		0x00100000
965c57132eSChao Yu #define F2FS_MOUNT_PRJQUOTA		0x00200000
974b2414d0SChao Yu #define F2FS_MOUNT_QUOTA		0x00400000
986afc662eSChao Yu #define F2FS_MOUNT_INLINE_XATTR_SIZE	0x00800000
997e65be49SJaegeuk Kim #define F2FS_MOUNT_RESERVE_ROOT		0x01000000
10039a53e0cSJaegeuk Kim 
10163189b78SChao Yu #define F2FS_OPTION(sbi)	((sbi)->mount_opt)
10263189b78SChao Yu #define clear_opt(sbi, option)	(F2FS_OPTION(sbi).opt &= ~F2FS_MOUNT_##option)
10363189b78SChao Yu #define set_opt(sbi, option)	(F2FS_OPTION(sbi).opt |= F2FS_MOUNT_##option)
10463189b78SChao Yu #define test_opt(sbi, option)	(F2FS_OPTION(sbi).opt & F2FS_MOUNT_##option)
10539a53e0cSJaegeuk Kim 
10639a53e0cSJaegeuk Kim #define ver_after(a, b)	(typecheck(unsigned long long, a) &&		\
10739a53e0cSJaegeuk Kim 		typecheck(unsigned long long, b) &&			\
10839a53e0cSJaegeuk Kim 		((long long)((a) - (b)) > 0))
10939a53e0cSJaegeuk Kim 
110a9841c4dSJaegeuk Kim typedef u32 block_t;	/*
111a9841c4dSJaegeuk Kim 			 * should not change u32, since it is the on-disk block
112a9841c4dSJaegeuk Kim 			 * address format, __le32.
113a9841c4dSJaegeuk Kim 			 */
11439a53e0cSJaegeuk Kim typedef u32 nid_t;
11539a53e0cSJaegeuk Kim 
11639a53e0cSJaegeuk Kim struct f2fs_mount_info {
11739a53e0cSJaegeuk Kim 	unsigned int opt;
11863189b78SChao Yu 	int write_io_size_bits;		/* Write IO size bits */
11963189b78SChao Yu 	block_t root_reserved_blocks;	/* root reserved blocks */
12063189b78SChao Yu 	kuid_t s_resuid;		/* reserved blocks for uid */
12163189b78SChao Yu 	kgid_t s_resgid;		/* reserved blocks for gid */
12263189b78SChao Yu 	int active_logs;		/* # of active logs */
12363189b78SChao Yu 	int inline_xattr_size;		/* inline xattr size */
12463189b78SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
12563189b78SChao Yu 	struct f2fs_fault_info fault_info;	/* For fault injection */
12663189b78SChao Yu #endif
12763189b78SChao Yu #ifdef CONFIG_QUOTA
12863189b78SChao Yu 	/* Names of quota files with journalled quota */
12963189b78SChao Yu 	char *s_qf_names[MAXQUOTAS];
13063189b78SChao Yu 	int s_jquota_fmt;			/* Format of quota to use */
13163189b78SChao Yu #endif
13263189b78SChao Yu 	/* For which write hints are passed down to block layer */
13363189b78SChao Yu 	int whint_mode;
13463189b78SChao Yu 	int alloc_mode;			/* segment allocation policy */
13563189b78SChao Yu 	int fsync_mode;			/* fsync policy */
136ff62af20SSheng Yong 	bool test_dummy_encryption;	/* test dummy encryption */
13739a53e0cSJaegeuk Kim };
13839a53e0cSJaegeuk Kim 
139cde4de12SJaegeuk Kim #define F2FS_FEATURE_ENCRYPT		0x0001
1400bfd7a09SDamien Le Moal #define F2FS_FEATURE_BLKZONED		0x0002
141e65ef207SJaegeuk Kim #define F2FS_FEATURE_ATOMIC_WRITE	0x0004
1427a2af766SChao Yu #define F2FS_FEATURE_EXTRA_ATTR		0x0008
1435c57132eSChao Yu #define F2FS_FEATURE_PRJQUOTA		0x0010
144704956ecSChao Yu #define F2FS_FEATURE_INODE_CHKSUM	0x0020
1456afc662eSChao Yu #define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR	0x0040
146234a9689SJaegeuk Kim #define F2FS_FEATURE_QUOTA_INO		0x0080
1471c1d35dfSChao Yu #define F2FS_FEATURE_INODE_CRTIME	0x0100
148b7c409deSSheng Yong #define F2FS_FEATURE_LOST_FOUND		0x0200
14953fedcc0SEric Biggers #define F2FS_FEATURE_VERITY		0x0400	/* reserved */
150cde4de12SJaegeuk Kim 
15176f105a2SJaegeuk Kim #define F2FS_HAS_FEATURE(sb, mask)					\
15276f105a2SJaegeuk Kim 	((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
15376f105a2SJaegeuk Kim #define F2FS_SET_FEATURE(sb, mask)					\
154c64ab12eSDongOh Shin 	(F2FS_SB(sb)->raw_super->feature |= cpu_to_le32(mask))
15576f105a2SJaegeuk Kim #define F2FS_CLEAR_FEATURE(sb, mask)					\
156c64ab12eSDongOh Shin 	(F2FS_SB(sb)->raw_super->feature &= ~cpu_to_le32(mask))
15776f105a2SJaegeuk Kim 
15839a53e0cSJaegeuk Kim /*
1597c2e5963SJaegeuk Kim  * Default values for user and/or group using reserved blocks
1607c2e5963SJaegeuk Kim  */
1617c2e5963SJaegeuk Kim #define	F2FS_DEF_RESUID		0
1627c2e5963SJaegeuk Kim #define	F2FS_DEF_RESGID		0
1637c2e5963SJaegeuk Kim 
1647c2e5963SJaegeuk Kim /*
16539a53e0cSJaegeuk Kim  * For checkpoint manager
16639a53e0cSJaegeuk Kim  */
16739a53e0cSJaegeuk Kim enum {
16839a53e0cSJaegeuk Kim 	NAT_BITMAP,
16939a53e0cSJaegeuk Kim 	SIT_BITMAP
17039a53e0cSJaegeuk Kim };
17139a53e0cSJaegeuk Kim 
172c473f1a9SChao Yu #define	CP_UMOUNT	0x00000001
173c473f1a9SChao Yu #define	CP_FASTBOOT	0x00000002
174c473f1a9SChao Yu #define	CP_SYNC		0x00000004
175c473f1a9SChao Yu #define	CP_RECOVERY	0x00000008
176c473f1a9SChao Yu #define	CP_DISCARD	0x00000010
1771f43e2adSChao Yu #define CP_TRIMMED	0x00000020
17875ab4cb8SJaegeuk Kim 
1794ddb1a4dSJaegeuk Kim #define MAX_DISCARD_BLOCKS(sbi)		BLKS_PER_SEC(sbi)
180ecc9aa00SChao Yu #define DEF_MAX_DISCARD_REQUEST		8	/* issue 8 discards per round */
1819a997188SJaegeuk Kim #define DEF_MAX_DISCARD_LEN		512	/* Max. 2MB per discard */
182969d1b18SChao Yu #define DEF_MIN_DISCARD_ISSUE_TIME	50	/* 50 ms, if exists */
183f9d1dcedSYunlei He #define DEF_MID_DISCARD_ISSUE_TIME	500	/* 500 ms, if device busy */
184969d1b18SChao Yu #define DEF_MAX_DISCARD_ISSUE_TIME	60000	/* 60 s, if no candidates */
1858bb4f253SJaegeuk Kim #define DEF_DISCARD_URGENT_UTIL		80	/* do more discard over 80% */
18660b99b48SJaegeuk Kim #define DEF_CP_INTERVAL			60	/* 60 secs */
187dcf25fe8SChao Yu #define DEF_IDLE_INTERVAL		5	/* 5 secs */
188bba681cbSJaegeuk Kim 
18975ab4cb8SJaegeuk Kim struct cp_control {
19075ab4cb8SJaegeuk Kim 	int reason;
1914b2fecc8SJaegeuk Kim 	__u64 trim_start;
1924b2fecc8SJaegeuk Kim 	__u64 trim_end;
1934b2fecc8SJaegeuk Kim 	__u64 trim_minlen;
19475ab4cb8SJaegeuk Kim };
19575ab4cb8SJaegeuk Kim 
196662befdaSChao Yu /*
19781c1a0f1SChao Yu  * For CP/NAT/SIT/SSA readahead
198662befdaSChao Yu  */
199662befdaSChao Yu enum {
200662befdaSChao Yu 	META_CP,
201662befdaSChao Yu 	META_NAT,
20281c1a0f1SChao Yu 	META_SIT,
2034c521f49SJaegeuk Kim 	META_SSA,
2044c521f49SJaegeuk Kim 	META_POR,
205662befdaSChao Yu };
206662befdaSChao Yu 
2076451e041SJaegeuk Kim /* for the list of ino */
2086451e041SJaegeuk Kim enum {
2096451e041SJaegeuk Kim 	ORPHAN_INO,		/* for orphan ino list */
210fff04f90SJaegeuk Kim 	APPEND_INO,		/* for append ino list */
211fff04f90SJaegeuk Kim 	UPDATE_INO,		/* for update ino list */
2120a007b97SJaegeuk Kim 	TRANS_DIR_INO,		/* for trasactions dir ino list */
21339d787beSChao Yu 	FLUSH_INO,		/* for multiple device flushing */
2146451e041SJaegeuk Kim 	MAX_INO_ENTRY,		/* max. list */
2156451e041SJaegeuk Kim };
2166451e041SJaegeuk Kim 
2176451e041SJaegeuk Kim struct ino_entry {
21839a53e0cSJaegeuk Kim 	struct list_head list;		/* list head */
21939a53e0cSJaegeuk Kim 	nid_t ino;			/* inode number */
22039d787beSChao Yu 	unsigned int dirty_device;	/* dirty device bitmap */
22139a53e0cSJaegeuk Kim };
22239a53e0cSJaegeuk Kim 
2232710fd7eSChao Yu /* for the list of inodes to be GCed */
22406292073SChao Yu struct inode_entry {
22539a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
22639a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
22739a53e0cSJaegeuk Kim };
22839a53e0cSJaegeuk Kim 
229a7eeb823SChao Yu /* for the bitmap indicate blocks to be discarded */
2307fd9e544SJaegeuk Kim struct discard_entry {
2317fd9e544SJaegeuk Kim 	struct list_head list;	/* list head */
232a7eeb823SChao Yu 	block_t start_blkaddr;	/* start blockaddr of current segment */
233a7eeb823SChao Yu 	unsigned char discard_map[SIT_VBLOCK_MAP_SIZE];	/* segment discard bitmap */
2347fd9e544SJaegeuk Kim };
2357fd9e544SJaegeuk Kim 
236969d1b18SChao Yu /* default discard granularity of inner discard thread, unit: block count */
237969d1b18SChao Yu #define DEFAULT_DISCARD_GRANULARITY		16
238969d1b18SChao Yu 
239ba48a33eSChao Yu /* max discard pend list number */
240ba48a33eSChao Yu #define MAX_PLIST_NUM		512
241ba48a33eSChao Yu #define plist_idx(blk_num)	((blk_num) >= MAX_PLIST_NUM ?		\
242ba48a33eSChao Yu 					(MAX_PLIST_NUM - 1) : (blk_num - 1))
243ba48a33eSChao Yu 
24415469963SJaegeuk Kim enum {
24515469963SJaegeuk Kim 	D_PREP,
24615469963SJaegeuk Kim 	D_SUBMIT,
24715469963SJaegeuk Kim 	D_DONE,
24815469963SJaegeuk Kim };
24915469963SJaegeuk Kim 
250004b6862SChao Yu struct discard_info {
251b01a9201SJaegeuk Kim 	block_t lstart;			/* logical start address */
252b01a9201SJaegeuk Kim 	block_t len;			/* length */
253004b6862SChao Yu 	block_t start;			/* actual start address in dev */
254004b6862SChao Yu };
255004b6862SChao Yu 
256b01a9201SJaegeuk Kim struct discard_cmd {
257004b6862SChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
258004b6862SChao Yu 	union {
259004b6862SChao Yu 		struct {
260004b6862SChao Yu 			block_t lstart;	/* logical start address */
261004b6862SChao Yu 			block_t len;	/* length */
262004b6862SChao Yu 			block_t start;	/* actual start address in dev */
263004b6862SChao Yu 		};
264004b6862SChao Yu 		struct discard_info di;	/* discard info */
265004b6862SChao Yu 
266004b6862SChao Yu 	};
267b01a9201SJaegeuk Kim 	struct list_head list;		/* command list */
268b01a9201SJaegeuk Kim 	struct completion wait;		/* compleation */
269c81abe34SJaegeuk Kim 	struct block_device *bdev;	/* bdev */
270ec9895adSChao Yu 	unsigned short ref;		/* reference count */
2719a744b92SChao Yu 	unsigned char state;		/* state */
272c81abe34SJaegeuk Kim 	int error;			/* bio error */
273275b66b0SChao Yu };
274275b66b0SChao Yu 
27578997b56SChao Yu enum {
27678997b56SChao Yu 	DPOLICY_BG,
27778997b56SChao Yu 	DPOLICY_FORCE,
27878997b56SChao Yu 	DPOLICY_FSTRIM,
27978997b56SChao Yu 	DPOLICY_UMOUNT,
28078997b56SChao Yu 	MAX_DPOLICY,
28178997b56SChao Yu };
28278997b56SChao Yu 
283ecc9aa00SChao Yu struct discard_policy {
28478997b56SChao Yu 	int type;			/* type of discard */
285ecc9aa00SChao Yu 	unsigned int min_interval;	/* used for candidates exist */
286f9d1dcedSYunlei He 	unsigned int mid_interval;	/* used for device busy */
287ecc9aa00SChao Yu 	unsigned int max_interval;	/* used for candidates not exist */
288ecc9aa00SChao Yu 	unsigned int max_requests;	/* # of discards issued per round */
289ecc9aa00SChao Yu 	unsigned int io_aware_gran;	/* minimum granularity discard not be aware of I/O */
290ecc9aa00SChao Yu 	bool io_aware;			/* issue discard in idle time */
291ecc9aa00SChao Yu 	bool sync;			/* submit discard with REQ_SYNC flag */
29278997b56SChao Yu 	unsigned int granularity;	/* discard granularity */
293ecc9aa00SChao Yu };
294ecc9aa00SChao Yu 
2950b54fb84SJaegeuk Kim struct discard_cmd_control {
29615469963SJaegeuk Kim 	struct task_struct *f2fs_issue_discard;	/* discard thread */
29746f84c2cSChao Yu 	struct list_head entry_list;		/* 4KB discard entry list */
298ba48a33eSChao Yu 	struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
29946f84c2cSChao Yu 	struct list_head wait_list;		/* store on-flushing entries */
3008412663dSChao Yu 	struct list_head fstrim_list;		/* in-flight discard from fstrim */
30115469963SJaegeuk Kim 	wait_queue_head_t discard_wait_queue;	/* waiting queue for wake-up */
302969d1b18SChao Yu 	unsigned int discard_wake;		/* to wake up discard thread */
30315469963SJaegeuk Kim 	struct mutex cmd_lock;
304d618ebafSChao Yu 	unsigned int nr_discards;		/* # of discards in the list */
305d618ebafSChao Yu 	unsigned int max_discards;		/* max. discards to be issued */
306969d1b18SChao Yu 	unsigned int discard_granularity;	/* discard granularity */
307d84d1cbdSChao Yu 	unsigned int undiscard_blks;		/* # of undiscard blocks */
3088b8dd65fSChao Yu 	atomic_t issued_discard;		/* # of issued discard */
3098b8dd65fSChao Yu 	atomic_t issing_discard;		/* # of issing discard */
3105f32366aSChao Yu 	atomic_t discard_cmd_cnt;		/* # of cached cmd count */
311004b6862SChao Yu 	struct rb_root root;			/* root of discard rb-tree */
31239a53e0cSJaegeuk Kim };
31339a53e0cSJaegeuk Kim 
31439a53e0cSJaegeuk Kim /* for the list of fsync inodes, used only during recovery */
31539a53e0cSJaegeuk Kim struct fsync_inode_entry {
31639a53e0cSJaegeuk Kim 	struct list_head list;	/* list head */
31739a53e0cSJaegeuk Kim 	struct inode *inode;	/* vfs inode pointer */
318c52e1b10SJaegeuk Kim 	block_t blkaddr;	/* block address locating the last fsync */
319c52e1b10SJaegeuk Kim 	block_t last_dentry;	/* block address locating the last dentry */
32039a53e0cSJaegeuk Kim };
32139a53e0cSJaegeuk Kim 
32268afcf2dSTomohiro Kusumi #define nats_in_cursum(jnl)		(le16_to_cpu((jnl)->n_nats))
32368afcf2dSTomohiro Kusumi #define sits_in_cursum(jnl)		(le16_to_cpu((jnl)->n_sits))
32439a53e0cSJaegeuk Kim 
32568afcf2dSTomohiro Kusumi #define nat_in_journal(jnl, i)		((jnl)->nat_j.entries[i].ne)
32668afcf2dSTomohiro Kusumi #define nid_in_journal(jnl, i)		((jnl)->nat_j.entries[i].nid)
32768afcf2dSTomohiro Kusumi #define sit_in_journal(jnl, i)		((jnl)->sit_j.entries[i].se)
32868afcf2dSTomohiro Kusumi #define segno_in_journal(jnl, i)	((jnl)->sit_j.entries[i].segno)
32939a53e0cSJaegeuk Kim 
330dfc08a12SChao Yu #define MAX_NAT_JENTRIES(jnl)	(NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
331dfc08a12SChao Yu #define MAX_SIT_JENTRIES(jnl)	(SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
332309cc2b6SJaegeuk Kim 
333dfc08a12SChao Yu static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
33439a53e0cSJaegeuk Kim {
335dfc08a12SChao Yu 	int before = nats_in_cursum(journal);
336cac5a3d8SDongOh Shin 
337dfc08a12SChao Yu 	journal->n_nats = cpu_to_le16(before + i);
33839a53e0cSJaegeuk Kim 	return before;
33939a53e0cSJaegeuk Kim }
34039a53e0cSJaegeuk Kim 
341dfc08a12SChao Yu static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
34239a53e0cSJaegeuk Kim {
343dfc08a12SChao Yu 	int before = sits_in_cursum(journal);
344cac5a3d8SDongOh Shin 
345dfc08a12SChao Yu 	journal->n_sits = cpu_to_le16(before + i);
34639a53e0cSJaegeuk Kim 	return before;
34739a53e0cSJaegeuk Kim }
34839a53e0cSJaegeuk Kim 
349dfc08a12SChao Yu static inline bool __has_cursum_space(struct f2fs_journal *journal,
350dfc08a12SChao Yu 							int size, int type)
351184a5cd2SChao Yu {
352184a5cd2SChao Yu 	if (type == NAT_JOURNAL)
353dfc08a12SChao Yu 		return size <= MAX_NAT_JENTRIES(journal);
354dfc08a12SChao Yu 	return size <= MAX_SIT_JENTRIES(journal);
355184a5cd2SChao Yu }
356184a5cd2SChao Yu 
35739a53e0cSJaegeuk Kim /*
358e9750824SNamjae Jeon  * ioctl commands
359e9750824SNamjae Jeon  */
360e9750824SNamjae Jeon #define F2FS_IOC_GETFLAGS		FS_IOC_GETFLAGS
361e9750824SNamjae Jeon #define F2FS_IOC_SETFLAGS		FS_IOC_SETFLAGS
362d49f3e89SChao Yu #define F2FS_IOC_GETVERSION		FS_IOC_GETVERSION
363e9750824SNamjae Jeon 
36488b88a66SJaegeuk Kim #define F2FS_IOCTL_MAGIC		0xf5
36588b88a66SJaegeuk Kim #define F2FS_IOC_START_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 1)
36688b88a66SJaegeuk Kim #define F2FS_IOC_COMMIT_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 2)
36702a1335fSJaegeuk Kim #define F2FS_IOC_START_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 3)
3681e84371fSJaegeuk Kim #define F2FS_IOC_RELEASE_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 4)
3691e84371fSJaegeuk Kim #define F2FS_IOC_ABORT_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 5)
370d07efb50SJaegeuk Kim #define F2FS_IOC_GARBAGE_COLLECT	_IOW(F2FS_IOCTL_MAGIC, 6, __u32)
371456b88e4SChao Yu #define F2FS_IOC_WRITE_CHECKPOINT	_IO(F2FS_IOCTL_MAGIC, 7)
372d07efb50SJaegeuk Kim #define F2FS_IOC_DEFRAGMENT		_IOWR(F2FS_IOCTL_MAGIC, 8,	\
373d07efb50SJaegeuk Kim 						struct f2fs_defragment)
3744dd6f977SJaegeuk Kim #define F2FS_IOC_MOVE_RANGE		_IOWR(F2FS_IOCTL_MAGIC, 9,	\
3754dd6f977SJaegeuk Kim 						struct f2fs_move_range)
376e066b83cSJaegeuk Kim #define F2FS_IOC_FLUSH_DEVICE		_IOW(F2FS_IOCTL_MAGIC, 10,	\
377e066b83cSJaegeuk Kim 						struct f2fs_flush_device)
37834dc77adSJaegeuk Kim #define F2FS_IOC_GARBAGE_COLLECT_RANGE	_IOW(F2FS_IOCTL_MAGIC, 11,	\
37934dc77adSJaegeuk Kim 						struct f2fs_gc_range)
380e65ef207SJaegeuk Kim #define F2FS_IOC_GET_FEATURES		_IOR(F2FS_IOCTL_MAGIC, 12, __u32)
3811ad71a27SJaegeuk Kim #define F2FS_IOC_SET_PIN_FILE		_IOW(F2FS_IOCTL_MAGIC, 13, __u32)
3821ad71a27SJaegeuk Kim #define F2FS_IOC_GET_PIN_FILE		_IOR(F2FS_IOCTL_MAGIC, 14, __u32)
383c4020b2dSChao Yu #define F2FS_IOC_PRECACHE_EXTENTS	_IO(F2FS_IOCTL_MAGIC, 15)
38488b88a66SJaegeuk Kim 
3850b81d077SJaegeuk Kim #define F2FS_IOC_SET_ENCRYPTION_POLICY	FS_IOC_SET_ENCRYPTION_POLICY
3860b81d077SJaegeuk Kim #define F2FS_IOC_GET_ENCRYPTION_POLICY	FS_IOC_GET_ENCRYPTION_POLICY
3870b81d077SJaegeuk Kim #define F2FS_IOC_GET_ENCRYPTION_PWSALT	FS_IOC_GET_ENCRYPTION_PWSALT
388f424f664SJaegeuk Kim 
3891abff93dSJaegeuk Kim /*
3901abff93dSJaegeuk Kim  * should be same as XFS_IOC_GOINGDOWN.
3911abff93dSJaegeuk Kim  * Flags for going down operation used by FS_IOC_GOINGDOWN
3921abff93dSJaegeuk Kim  */
3931abff93dSJaegeuk Kim #define F2FS_IOC_SHUTDOWN	_IOR('X', 125, __u32)	/* Shutdown */
3941abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_FULLSYNC	0x0	/* going down with full sync */
3951abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_METASYNC	0x1	/* going down with metadata */
3961abff93dSJaegeuk Kim #define F2FS_GOING_DOWN_NOSYNC		0x2	/* going down */
397c912a829SJaegeuk Kim #define F2FS_GOING_DOWN_METAFLUSH	0x3	/* going down with meta flush */
3981abff93dSJaegeuk Kim 
399e9750824SNamjae Jeon #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
400e9750824SNamjae Jeon /*
401e9750824SNamjae Jeon  * ioctl commands in 32 bit emulation
402e9750824SNamjae Jeon  */
403e9750824SNamjae Jeon #define F2FS_IOC32_GETFLAGS		FS_IOC32_GETFLAGS
404e9750824SNamjae Jeon #define F2FS_IOC32_SETFLAGS		FS_IOC32_SETFLAGS
40504ef4b62SChao Yu #define F2FS_IOC32_GETVERSION		FS_IOC32_GETVERSION
406e9750824SNamjae Jeon #endif
407e9750824SNamjae Jeon 
4082c1d0305SChao Yu #define F2FS_IOC_FSGETXATTR		FS_IOC_FSGETXATTR
4092c1d0305SChao Yu #define F2FS_IOC_FSSETXATTR		FS_IOC_FSSETXATTR
4102c1d0305SChao Yu 
41134dc77adSJaegeuk Kim struct f2fs_gc_range {
41234dc77adSJaegeuk Kim 	u32 sync;
41334dc77adSJaegeuk Kim 	u64 start;
41434dc77adSJaegeuk Kim 	u64 len;
41534dc77adSJaegeuk Kim };
41634dc77adSJaegeuk Kim 
417d323d005SChao Yu struct f2fs_defragment {
418d323d005SChao Yu 	u64 start;
419d323d005SChao Yu 	u64 len;
420d323d005SChao Yu };
421d323d005SChao Yu 
4224dd6f977SJaegeuk Kim struct f2fs_move_range {
4234dd6f977SJaegeuk Kim 	u32 dst_fd;		/* destination fd */
4244dd6f977SJaegeuk Kim 	u64 pos_in;		/* start position in src_fd */
4254dd6f977SJaegeuk Kim 	u64 pos_out;		/* start position in dst_fd */
4264dd6f977SJaegeuk Kim 	u64 len;		/* size to move */
4274dd6f977SJaegeuk Kim };
4284dd6f977SJaegeuk Kim 
429e066b83cSJaegeuk Kim struct f2fs_flush_device {
430e066b83cSJaegeuk Kim 	u32 dev_num;		/* device number to flush */
431e066b83cSJaegeuk Kim 	u32 segments;		/* # of segments to flush */
432e066b83cSJaegeuk Kim };
433e066b83cSJaegeuk Kim 
434f2470371SChao Yu /* for inline stuff */
435f2470371SChao Yu #define DEF_INLINE_RESERVED_SIZE	1
4366afc662eSChao Yu #define DEF_MIN_INLINE_SIZE		1
4377a2af766SChao Yu static inline int get_extra_isize(struct inode *inode);
4386afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode);
4397a2af766SChao Yu #define MAX_INLINE_DATA(inode)	(sizeof(__le32) *			\
4407a2af766SChao Yu 				(CUR_ADDRS_PER_INODE(inode) -		\
441b323fd28SChao Yu 				get_inline_xattr_addrs(inode) -	\
4426afc662eSChao Yu 				DEF_INLINE_RESERVED_SIZE))
443f2470371SChao Yu 
444f2470371SChao Yu /* for inline dir */
445f2470371SChao Yu #define NR_INLINE_DENTRY(inode)	(MAX_INLINE_DATA(inode) * BITS_PER_BYTE / \
446f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
447f2470371SChao Yu 				BITS_PER_BYTE + 1))
448f2470371SChao Yu #define INLINE_DENTRY_BITMAP_SIZE(inode)	((NR_INLINE_DENTRY(inode) + \
449f2470371SChao Yu 					BITS_PER_BYTE - 1) / BITS_PER_BYTE)
450f2470371SChao Yu #define INLINE_RESERVED_SIZE(inode)	(MAX_INLINE_DATA(inode) - \
451f2470371SChao Yu 				((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
452f2470371SChao Yu 				NR_INLINE_DENTRY(inode) + \
453f2470371SChao Yu 				INLINE_DENTRY_BITMAP_SIZE(inode)))
454f2470371SChao Yu 
455e9750824SNamjae Jeon /*
45639a53e0cSJaegeuk Kim  * For INODE and NODE manager
45739a53e0cSJaegeuk Kim  */
4587b3cd7d6SJaegeuk Kim /* for directory operations */
4597b3cd7d6SJaegeuk Kim struct f2fs_dentry_ptr {
460d8c6822aSJaegeuk Kim 	struct inode *inode;
46176a9dd85SChao Yu 	void *bitmap;
4627b3cd7d6SJaegeuk Kim 	struct f2fs_dir_entry *dentry;
4637b3cd7d6SJaegeuk Kim 	__u8 (*filename)[F2FS_SLOT_LEN];
4647b3cd7d6SJaegeuk Kim 	int max;
46576a9dd85SChao Yu 	int nr_bitmap;
4667b3cd7d6SJaegeuk Kim };
4677b3cd7d6SJaegeuk Kim 
46864c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_block(struct inode *inode,
46964c24ecbSTomohiro Kusumi 		struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
4707b3cd7d6SJaegeuk Kim {
471d8c6822aSJaegeuk Kim 	d->inode = inode;
4727b3cd7d6SJaegeuk Kim 	d->max = NR_DENTRY_IN_BLOCK;
47376a9dd85SChao Yu 	d->nr_bitmap = SIZE_OF_DENTRY_BITMAP;
474c79d1520SYunlong Song 	d->bitmap = t->dentry_bitmap;
4757b3cd7d6SJaegeuk Kim 	d->dentry = t->dentry;
4767b3cd7d6SJaegeuk Kim 	d->filename = t->filename;
47764c24ecbSTomohiro Kusumi }
478cac5a3d8SDongOh Shin 
47964c24ecbSTomohiro Kusumi static inline void make_dentry_ptr_inline(struct inode *inode,
480f2470371SChao Yu 					struct f2fs_dentry_ptr *d, void *t)
48164c24ecbSTomohiro Kusumi {
482f2470371SChao Yu 	int entry_cnt = NR_INLINE_DENTRY(inode);
483f2470371SChao Yu 	int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(inode);
484f2470371SChao Yu 	int reserved_size = INLINE_RESERVED_SIZE(inode);
485f2470371SChao Yu 
48664c24ecbSTomohiro Kusumi 	d->inode = inode;
487f2470371SChao Yu 	d->max = entry_cnt;
488f2470371SChao Yu 	d->nr_bitmap = bitmap_size;
489f2470371SChao Yu 	d->bitmap = t;
490f2470371SChao Yu 	d->dentry = t + bitmap_size + reserved_size;
491f2470371SChao Yu 	d->filename = t + bitmap_size + reserved_size +
492f2470371SChao Yu 					SIZE_OF_DIR_ENTRY * entry_cnt;
4937b3cd7d6SJaegeuk Kim }
4947b3cd7d6SJaegeuk Kim 
495dbe6a5ffSJaegeuk Kim /*
496dbe6a5ffSJaegeuk Kim  * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
497dbe6a5ffSJaegeuk Kim  * as its node offset to distinguish from index node blocks.
498dbe6a5ffSJaegeuk Kim  * But some bits are used to mark the node block.
49939a53e0cSJaegeuk Kim  */
500dbe6a5ffSJaegeuk Kim #define XATTR_NODE_OFFSET	((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
501dbe6a5ffSJaegeuk Kim 				>> OFFSET_BIT_SHIFT)
502266e97a8SJaegeuk Kim enum {
503266e97a8SJaegeuk Kim 	ALLOC_NODE,			/* allocate a new node page if needed */
504266e97a8SJaegeuk Kim 	LOOKUP_NODE,			/* look up a node without readahead */
505266e97a8SJaegeuk Kim 	LOOKUP_NODE_RA,			/*
506266e97a8SJaegeuk Kim 					 * look up a node with readahead called
5074f4124d0SChao Yu 					 * by get_data_block.
50839a53e0cSJaegeuk Kim 					 */
509266e97a8SJaegeuk Kim };
510266e97a8SJaegeuk Kim 
511a6db67f0SJaegeuk Kim #define F2FS_LINK_MAX	0xffffffff	/* maximum link count per file */
51239a53e0cSJaegeuk Kim 
513817202d9SChao Yu #define MAX_DIR_RA_PAGES	4	/* maximum ra pages of dir */
514817202d9SChao Yu 
51513054c54SChao Yu /* vector size for gang look-up from extent cache that consists of radix tree */
51613054c54SChao Yu #define EXT_TREE_VEC_SIZE	64
51713054c54SChao Yu 
51839a53e0cSJaegeuk Kim /* for in-memory extent cache entry */
51913054c54SChao Yu #define F2FS_MIN_EXTENT_LEN	64	/* minimum extent length */
52013054c54SChao Yu 
52113054c54SChao Yu /* number of extent info in extent cache we try to shrink */
52213054c54SChao Yu #define EXTENT_CACHE_SHRINK_NUMBER	128
523c11abd1aSJaegeuk Kim 
52454c2258cSChao Yu struct rb_entry {
52554c2258cSChao Yu 	struct rb_node rb_node;		/* rb node located in rb-tree */
52654c2258cSChao Yu 	unsigned int ofs;		/* start offset of the entry */
52754c2258cSChao Yu 	unsigned int len;		/* length of the entry */
52854c2258cSChao Yu };
52954c2258cSChao Yu 
53039a53e0cSJaegeuk Kim struct extent_info {
53139a53e0cSJaegeuk Kim 	unsigned int fofs;		/* start offset in a file */
532111d2495SMasanari Iida 	unsigned int len;		/* length of the extent */
53354c2258cSChao Yu 	u32 blk;			/* start block address of the extent */
53439a53e0cSJaegeuk Kim };
53539a53e0cSJaegeuk Kim 
53613054c54SChao Yu struct extent_node {
53754c2258cSChao Yu 	struct rb_node rb_node;
53854c2258cSChao Yu 	union {
53954c2258cSChao Yu 		struct {
54054c2258cSChao Yu 			unsigned int fofs;
54154c2258cSChao Yu 			unsigned int len;
54254c2258cSChao Yu 			u32 blk;
54354c2258cSChao Yu 		};
54413054c54SChao Yu 		struct extent_info ei;	/* extent info */
54554c2258cSChao Yu 
54654c2258cSChao Yu 	};
54754c2258cSChao Yu 	struct list_head list;		/* node in global extent list of sbi */
548201ef5e0SHou Pengyang 	struct extent_tree *et;		/* extent tree pointer */
54913054c54SChao Yu };
55013054c54SChao Yu 
55113054c54SChao Yu struct extent_tree {
55213054c54SChao Yu 	nid_t ino;			/* inode number */
55313054c54SChao Yu 	struct rb_root root;		/* root of extent info rb-tree */
55462c8af65SChao Yu 	struct extent_node *cached_en;	/* recently accessed extent node */
5553e72f721SJaegeuk Kim 	struct extent_info largest;	/* largested extent info */
556137d09f0SJaegeuk Kim 	struct list_head list;		/* to be used by sbi->zombie_list */
55713054c54SChao Yu 	rwlock_t lock;			/* protect extent info rb-tree */
55868e35385SChao Yu 	atomic_t node_cnt;		/* # of extent node in rb-tree*/
55913054c54SChao Yu };
56013054c54SChao Yu 
56139a53e0cSJaegeuk Kim /*
562003a3e1dSJaegeuk Kim  * This structure is taken from ext4_map_blocks.
563003a3e1dSJaegeuk Kim  *
564003a3e1dSJaegeuk Kim  * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
565003a3e1dSJaegeuk Kim  */
566003a3e1dSJaegeuk Kim #define F2FS_MAP_NEW		(1 << BH_New)
567003a3e1dSJaegeuk Kim #define F2FS_MAP_MAPPED		(1 << BH_Mapped)
5687f63eb77SJaegeuk Kim #define F2FS_MAP_UNWRITTEN	(1 << BH_Unwritten)
5697f63eb77SJaegeuk Kim #define F2FS_MAP_FLAGS		(F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
5707f63eb77SJaegeuk Kim 				F2FS_MAP_UNWRITTEN)
571003a3e1dSJaegeuk Kim 
572003a3e1dSJaegeuk Kim struct f2fs_map_blocks {
573003a3e1dSJaegeuk Kim 	block_t m_pblk;
574003a3e1dSJaegeuk Kim 	block_t m_lblk;
575003a3e1dSJaegeuk Kim 	unsigned int m_len;
576003a3e1dSJaegeuk Kim 	unsigned int m_flags;
577da85985cSChao Yu 	pgoff_t *m_next_pgofs;		/* point next possible non-hole pgofs */
578c4020b2dSChao Yu 	pgoff_t *m_next_extent;		/* point to next possible extent */
579d5097be5SHyunchul Lee 	int m_seg_type;
580003a3e1dSJaegeuk Kim };
581003a3e1dSJaegeuk Kim 
582e2b4e2bcSChao Yu /* for flag in get_data_block */
583f2220c7fSQiuyang Sun enum {
584f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_DEFAULT,
585f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_FIEMAP,
586f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_BMAP,
587f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_DIO,
588f2220c7fSQiuyang Sun 	F2FS_GET_BLOCK_PRE_AIO,
589c4020b2dSChao Yu 	F2FS_GET_BLOCK_PRECACHE,
590f2220c7fSQiuyang Sun };
591e2b4e2bcSChao Yu 
592003a3e1dSJaegeuk Kim /*
59339a53e0cSJaegeuk Kim  * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
59439a53e0cSJaegeuk Kim  */
59539a53e0cSJaegeuk Kim #define FADVISE_COLD_BIT	0x01
596354a3399SJaegeuk Kim #define FADVISE_LOST_PINO_BIT	0x02
597cde4de12SJaegeuk Kim #define FADVISE_ENCRYPT_BIT	0x04
598e7d55452SJaegeuk Kim #define FADVISE_ENC_NAME_BIT	0x08
59926787236SJaegeuk Kim #define FADVISE_KEEP_SIZE_BIT	0x10
600b6a06cbbSChao Yu #define FADVISE_HOT_BIT		0x20
60153fedcc0SEric Biggers #define FADVISE_VERITY_BIT	0x40	/* reserved */
60239a53e0cSJaegeuk Kim 
603b5492af7SJaegeuk Kim #define file_is_cold(inode)	is_file(inode, FADVISE_COLD_BIT)
604b5492af7SJaegeuk Kim #define file_wrong_pino(inode)	is_file(inode, FADVISE_LOST_PINO_BIT)
605b5492af7SJaegeuk Kim #define file_set_cold(inode)	set_file(inode, FADVISE_COLD_BIT)
606b5492af7SJaegeuk Kim #define file_lost_pino(inode)	set_file(inode, FADVISE_LOST_PINO_BIT)
607b5492af7SJaegeuk Kim #define file_clear_cold(inode)	clear_file(inode, FADVISE_COLD_BIT)
608b5492af7SJaegeuk Kim #define file_got_pino(inode)	clear_file(inode, FADVISE_LOST_PINO_BIT)
609cde4de12SJaegeuk Kim #define file_is_encrypt(inode)	is_file(inode, FADVISE_ENCRYPT_BIT)
610cde4de12SJaegeuk Kim #define file_set_encrypt(inode)	set_file(inode, FADVISE_ENCRYPT_BIT)
611cde4de12SJaegeuk Kim #define file_clear_encrypt(inode) clear_file(inode, FADVISE_ENCRYPT_BIT)
612e7d55452SJaegeuk Kim #define file_enc_name(inode)	is_file(inode, FADVISE_ENC_NAME_BIT)
613e7d55452SJaegeuk Kim #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
61426787236SJaegeuk Kim #define file_keep_isize(inode)	is_file(inode, FADVISE_KEEP_SIZE_BIT)
61526787236SJaegeuk Kim #define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
616b6a06cbbSChao Yu #define file_is_hot(inode)	is_file(inode, FADVISE_HOT_BIT)
617b6a06cbbSChao Yu #define file_set_hot(inode)	set_file(inode, FADVISE_HOT_BIT)
618b6a06cbbSChao Yu #define file_clear_hot(inode)	clear_file(inode, FADVISE_HOT_BIT)
619cde4de12SJaegeuk Kim 
620ab9fa662SJaegeuk Kim #define DEF_DIR_LEVEL		0
621ab9fa662SJaegeuk Kim 
6222ef79ecbSChao Yu enum {
6232ef79ecbSChao Yu 	GC_FAILURE_PIN,
6242ef79ecbSChao Yu 	GC_FAILURE_ATOMIC,
6252ef79ecbSChao Yu 	MAX_GC_FAILURE
6262ef79ecbSChao Yu };
6272ef79ecbSChao Yu 
62839a53e0cSJaegeuk Kim struct f2fs_inode_info {
62939a53e0cSJaegeuk Kim 	struct inode vfs_inode;		/* serve a vfs inode */
63039a53e0cSJaegeuk Kim 	unsigned long i_flags;		/* keep an inode flags for ioctl */
63139a53e0cSJaegeuk Kim 	unsigned char i_advise;		/* use to give file attribute hints */
63238431545SJaegeuk Kim 	unsigned char i_dir_level;	/* use for dentry level for large dir */
6331ad71a27SJaegeuk Kim 	unsigned int i_current_depth;	/* only for directory depth */
6342ef79ecbSChao Yu 	/* for gc failure statistic */
6352ef79ecbSChao Yu 	unsigned int i_gc_failures[MAX_GC_FAILURE];
6366666e6aaSJaegeuk Kim 	unsigned int i_pino;		/* parent inode number */
63739a53e0cSJaegeuk Kim 	umode_t i_acl_mode;		/* keep file acl mode temporarily */
63839a53e0cSJaegeuk Kim 
63939a53e0cSJaegeuk Kim 	/* Use below internally in f2fs*/
64039a53e0cSJaegeuk Kim 	unsigned long flags;		/* use to pass per-file flags */
641d928bfbfSJaegeuk Kim 	struct rw_semaphore i_sem;	/* protect fi info */
642204706c7SJaegeuk Kim 	atomic_t dirty_pages;		/* # of dirty pages */
64339a53e0cSJaegeuk Kim 	f2fs_hash_t chash;		/* hash value of given file name */
64439a53e0cSJaegeuk Kim 	unsigned int clevel;		/* maximum level of given file name */
64588c5c13aSJaegeuk Kim 	struct task_struct *task;	/* lookup and create consistency */
646b0af6d49SChao Yu 	struct task_struct *cp_task;	/* separate cp/wb IO stats*/
64739a53e0cSJaegeuk Kim 	nid_t i_xattr_nid;		/* node id that contains xattrs */
64826de9b11SJaegeuk Kim 	loff_t	last_disk_size;		/* lastly written file size */
64988b88a66SJaegeuk Kim 
6500abd675eSChao Yu #ifdef CONFIG_QUOTA
6510abd675eSChao Yu 	struct dquot *i_dquot[MAXQUOTAS];
6520abd675eSChao Yu 
6530abd675eSChao Yu 	/* quota space reservation, managed internally by quota code */
6540abd675eSChao Yu 	qsize_t i_reserved_quota;
6550abd675eSChao Yu #endif
6560f18b462SJaegeuk Kim 	struct list_head dirty_list;	/* dirty list for dirs and files */
6570f18b462SJaegeuk Kim 	struct list_head gdirty_list;	/* linked in global dirty list */
65857864ae5SJaegeuk Kim 	struct list_head inmem_ilist;	/* list for inmem inodes */
65988b88a66SJaegeuk Kim 	struct list_head inmem_pages;	/* inmemory pages managed by f2fs */
6607a10f017SJaegeuk Kim 	struct task_struct *inmem_task;	/* store inmemory task */
66188b88a66SJaegeuk Kim 	struct mutex inmem_lock;	/* lock for inmemory pages */
6623e72f721SJaegeuk Kim 	struct extent_tree *extent_tree;	/* cached extent_tree entry */
663b2532c69SChao Yu 
664b2532c69SChao Yu 	/* avoid racing between foreground op and gc */
665b2532c69SChao Yu 	struct rw_semaphore i_gc_rwsem[2];
6665a3a2d83SQiuyang Sun 	struct rw_semaphore i_mmap_sem;
66727161f13SYunlei He 	struct rw_semaphore i_xattr_sem; /* avoid racing between reading and changing EAs */
668f2470371SChao Yu 
6697a2af766SChao Yu 	int i_extra_isize;		/* size of extra space located in i_addr */
6705c57132eSChao Yu 	kprojid_t i_projid;		/* id for project quota */
6716afc662eSChao Yu 	int i_inline_xattr_size;	/* inline xattr size */
6721c1d35dfSChao Yu 	struct timespec i_crtime;	/* inode creation time */
673214c2461SJaegeuk Kim 	struct timespec i_disk_time[4];	/* inode disk times */
67439a53e0cSJaegeuk Kim };
67539a53e0cSJaegeuk Kim 
67639a53e0cSJaegeuk Kim static inline void get_extent_info(struct extent_info *ext,
677bd933d4fSChao Yu 					struct f2fs_extent *i_ext)
67839a53e0cSJaegeuk Kim {
679bd933d4fSChao Yu 	ext->fofs = le32_to_cpu(i_ext->fofs);
680bd933d4fSChao Yu 	ext->blk = le32_to_cpu(i_ext->blk);
681bd933d4fSChao Yu 	ext->len = le32_to_cpu(i_ext->len);
68239a53e0cSJaegeuk Kim }
68339a53e0cSJaegeuk Kim 
68439a53e0cSJaegeuk Kim static inline void set_raw_extent(struct extent_info *ext,
68539a53e0cSJaegeuk Kim 					struct f2fs_extent *i_ext)
68639a53e0cSJaegeuk Kim {
68739a53e0cSJaegeuk Kim 	i_ext->fofs = cpu_to_le32(ext->fofs);
6884d0b0bd4SChao Yu 	i_ext->blk = cpu_to_le32(ext->blk);
68939a53e0cSJaegeuk Kim 	i_ext->len = cpu_to_le32(ext->len);
69039a53e0cSJaegeuk Kim }
69139a53e0cSJaegeuk Kim 
692429511cdSChao Yu static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
693429511cdSChao Yu 						u32 blk, unsigned int len)
694429511cdSChao Yu {
695429511cdSChao Yu 	ei->fofs = fofs;
696429511cdSChao Yu 	ei->blk = blk;
697429511cdSChao Yu 	ei->len = len;
698429511cdSChao Yu }
699429511cdSChao Yu 
700004b6862SChao Yu static inline bool __is_discard_mergeable(struct discard_info *back,
701004b6862SChao Yu 						struct discard_info *front)
702004b6862SChao Yu {
7039a997188SJaegeuk Kim 	return (back->lstart + back->len == front->lstart) &&
7049a997188SJaegeuk Kim 		(back->len + front->len < DEF_MAX_DISCARD_LEN);
705004b6862SChao Yu }
706004b6862SChao Yu 
707004b6862SChao Yu static inline bool __is_discard_back_mergeable(struct discard_info *cur,
708004b6862SChao Yu 						struct discard_info *back)
709004b6862SChao Yu {
710004b6862SChao Yu 	return __is_discard_mergeable(back, cur);
711004b6862SChao Yu }
712004b6862SChao Yu 
713004b6862SChao Yu static inline bool __is_discard_front_mergeable(struct discard_info *cur,
714004b6862SChao Yu 						struct discard_info *front)
715004b6862SChao Yu {
716004b6862SChao Yu 	return __is_discard_mergeable(cur, front);
717004b6862SChao Yu }
718004b6862SChao Yu 
719429511cdSChao Yu static inline bool __is_extent_mergeable(struct extent_info *back,
720429511cdSChao Yu 						struct extent_info *front)
721429511cdSChao Yu {
722429511cdSChao Yu 	return (back->fofs + back->len == front->fofs &&
723429511cdSChao Yu 			back->blk + back->len == front->blk);
724429511cdSChao Yu }
725429511cdSChao Yu 
726429511cdSChao Yu static inline bool __is_back_mergeable(struct extent_info *cur,
727429511cdSChao Yu 						struct extent_info *back)
728429511cdSChao Yu {
729429511cdSChao Yu 	return __is_extent_mergeable(back, cur);
730429511cdSChao Yu }
731429511cdSChao Yu 
732429511cdSChao Yu static inline bool __is_front_mergeable(struct extent_info *cur,
733429511cdSChao Yu 						struct extent_info *front)
734429511cdSChao Yu {
735429511cdSChao Yu 	return __is_extent_mergeable(cur, front);
736429511cdSChao Yu }
737429511cdSChao Yu 
738cac5a3d8SDongOh Shin extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
739205b9822SJaegeuk Kim static inline void __try_update_largest_extent(struct inode *inode,
740205b9822SJaegeuk Kim 			struct extent_tree *et, struct extent_node *en)
7414abd3f5aSChao Yu {
742205b9822SJaegeuk Kim 	if (en->ei.len > et->largest.len) {
7434abd3f5aSChao Yu 		et->largest = en->ei;
7447c45729aSJaegeuk Kim 		f2fs_mark_inode_dirty_sync(inode, true);
745205b9822SJaegeuk Kim 	}
7464abd3f5aSChao Yu }
7474abd3f5aSChao Yu 
7489a4ffdf5SChao Yu /*
7499a4ffdf5SChao Yu  * For free nid management
7509a4ffdf5SChao Yu  */
7519a4ffdf5SChao Yu enum nid_state {
7529a4ffdf5SChao Yu 	FREE_NID,		/* newly added to free nid list */
7539a4ffdf5SChao Yu 	PREALLOC_NID,		/* it is preallocated */
7549a4ffdf5SChao Yu 	MAX_NID_STATE,
755b8559dc2SChao Yu };
756b8559dc2SChao Yu 
75739a53e0cSJaegeuk Kim struct f2fs_nm_info {
75839a53e0cSJaegeuk Kim 	block_t nat_blkaddr;		/* base disk address of NAT */
75939a53e0cSJaegeuk Kim 	nid_t max_nid;			/* maximum possible node ids */
76004d47e67SChao Yu 	nid_t available_nids;		/* # of available node ids */
76139a53e0cSJaegeuk Kim 	nid_t next_scan_nid;		/* the next nid to be scanned */
762cdfc41c1SJaegeuk Kim 	unsigned int ram_thresh;	/* control the memory footprint */
763ea1a29a0SChao Yu 	unsigned int ra_nid_pages;	/* # of nid pages to be readaheaded */
7642304cb0cSChao Yu 	unsigned int dirty_nats_ratio;	/* control dirty nats ratio threshold */
76539a53e0cSJaegeuk Kim 
76639a53e0cSJaegeuk Kim 	/* NAT cache management */
76739a53e0cSJaegeuk Kim 	struct radix_tree_root nat_root;/* root of the nat entry cache */
768309cc2b6SJaegeuk Kim 	struct radix_tree_root nat_set_root;/* root of the nat set cache */
769b873b798SJaegeuk Kim 	struct rw_semaphore nat_tree_lock;	/* protect nat_tree_lock */
77039a53e0cSJaegeuk Kim 	struct list_head nat_entries;	/* cached nat entry list (clean) */
771309cc2b6SJaegeuk Kim 	unsigned int nat_cnt;		/* the # of cached nat entries */
772aec71382SChao Yu 	unsigned int dirty_nat_cnt;	/* total num of nat entries in set */
77322ad0b6aSJaegeuk Kim 	unsigned int nat_blocks;	/* # of nat blocks */
77439a53e0cSJaegeuk Kim 
77539a53e0cSJaegeuk Kim 	/* free node ids management */
7768a7ed66aSJaegeuk Kim 	struct radix_tree_root free_nid_root;/* root of the free_nid cache */
7779a4ffdf5SChao Yu 	struct list_head free_nid_list;		/* list for free nids excluding preallocated nids */
7789a4ffdf5SChao Yu 	unsigned int nid_cnt[MAX_NID_STATE];	/* the number of free node id */
779b8559dc2SChao Yu 	spinlock_t nid_list_lock;	/* protect nid lists ops */
78039a53e0cSJaegeuk Kim 	struct mutex build_lock;	/* lock for build free nids */
781bb1105e4SJaegeuk Kim 	unsigned char **free_nid_bitmap;
7824ac91242SChao Yu 	unsigned char *nat_block_bitmap;
783586d1492SChao Yu 	unsigned short *free_nid_count;	/* free nid count of NAT block */
78439a53e0cSJaegeuk Kim 
78539a53e0cSJaegeuk Kim 	/* for checkpoint */
78639a53e0cSJaegeuk Kim 	char *nat_bitmap;		/* NAT bitmap pointer */
78722ad0b6aSJaegeuk Kim 
78822ad0b6aSJaegeuk Kim 	unsigned int nat_bits_blocks;	/* # of nat bits blocks */
78922ad0b6aSJaegeuk Kim 	unsigned char *nat_bits;	/* NAT bits blocks */
79022ad0b6aSJaegeuk Kim 	unsigned char *full_nat_bits;	/* full NAT pages */
79122ad0b6aSJaegeuk Kim 	unsigned char *empty_nat_bits;	/* empty NAT pages */
792599a09b2SChao Yu #ifdef CONFIG_F2FS_CHECK_FS
793599a09b2SChao Yu 	char *nat_bitmap_mir;		/* NAT bitmap mirror */
794599a09b2SChao Yu #endif
79539a53e0cSJaegeuk Kim 	int bitmap_size;		/* bitmap size */
79639a53e0cSJaegeuk Kim };
79739a53e0cSJaegeuk Kim 
79839a53e0cSJaegeuk Kim /*
79939a53e0cSJaegeuk Kim  * this structure is used as one of function parameters.
80039a53e0cSJaegeuk Kim  * all the information are dedicated to a given direct node block determined
80139a53e0cSJaegeuk Kim  * by the data offset in a file.
80239a53e0cSJaegeuk Kim  */
80339a53e0cSJaegeuk Kim struct dnode_of_data {
80439a53e0cSJaegeuk Kim 	struct inode *inode;		/* vfs inode pointer */
80539a53e0cSJaegeuk Kim 	struct page *inode_page;	/* its inode page, NULL is possible */
80639a53e0cSJaegeuk Kim 	struct page *node_page;		/* cached direct node page */
80739a53e0cSJaegeuk Kim 	nid_t nid;			/* node id of the direct node block */
80839a53e0cSJaegeuk Kim 	unsigned int ofs_in_node;	/* data offset in the node page */
80939a53e0cSJaegeuk Kim 	bool inode_page_locked;		/* inode page is locked or not */
81093bae099SJaegeuk Kim 	bool node_changed;		/* is node block changed */
8113cf45747SChao Yu 	char cur_level;			/* level of hole node page */
8123cf45747SChao Yu 	char max_level;			/* level of current page located */
81339a53e0cSJaegeuk Kim 	block_t	data_blkaddr;		/* block address of the node block */
81439a53e0cSJaegeuk Kim };
81539a53e0cSJaegeuk Kim 
81639a53e0cSJaegeuk Kim static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
81739a53e0cSJaegeuk Kim 		struct page *ipage, struct page *npage, nid_t nid)
81839a53e0cSJaegeuk Kim {
819d66d1f76SJaegeuk Kim 	memset(dn, 0, sizeof(*dn));
82039a53e0cSJaegeuk Kim 	dn->inode = inode;
82139a53e0cSJaegeuk Kim 	dn->inode_page = ipage;
82239a53e0cSJaegeuk Kim 	dn->node_page = npage;
82339a53e0cSJaegeuk Kim 	dn->nid = nid;
82439a53e0cSJaegeuk Kim }
82539a53e0cSJaegeuk Kim 
82639a53e0cSJaegeuk Kim /*
82739a53e0cSJaegeuk Kim  * For SIT manager
82839a53e0cSJaegeuk Kim  *
82939a53e0cSJaegeuk Kim  * By default, there are 6 active log areas across the whole main area.
83039a53e0cSJaegeuk Kim  * When considering hot and cold data separation to reduce cleaning overhead,
83139a53e0cSJaegeuk Kim  * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
83239a53e0cSJaegeuk Kim  * respectively.
83339a53e0cSJaegeuk Kim  * In the current design, you should not change the numbers intentionally.
83439a53e0cSJaegeuk Kim  * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
83539a53e0cSJaegeuk Kim  * logs individually according to the underlying devices. (default: 6)
83639a53e0cSJaegeuk Kim  * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
83739a53e0cSJaegeuk Kim  * data and 8 for node logs.
83839a53e0cSJaegeuk Kim  */
83939a53e0cSJaegeuk Kim #define	NR_CURSEG_DATA_TYPE	(3)
84039a53e0cSJaegeuk Kim #define NR_CURSEG_NODE_TYPE	(3)
84139a53e0cSJaegeuk Kim #define NR_CURSEG_TYPE	(NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
84239a53e0cSJaegeuk Kim 
84339a53e0cSJaegeuk Kim enum {
84439a53e0cSJaegeuk Kim 	CURSEG_HOT_DATA	= 0,	/* directory entry blocks */
84539a53e0cSJaegeuk Kim 	CURSEG_WARM_DATA,	/* data blocks */
84639a53e0cSJaegeuk Kim 	CURSEG_COLD_DATA,	/* multimedia or GCed data blocks */
84739a53e0cSJaegeuk Kim 	CURSEG_HOT_NODE,	/* direct node blocks of directory files */
84839a53e0cSJaegeuk Kim 	CURSEG_WARM_NODE,	/* direct node blocks of normal files */
84939a53e0cSJaegeuk Kim 	CURSEG_COLD_NODE,	/* indirect node blocks */
85038aa0889SJaegeuk Kim 	NO_CHECK_TYPE,
85139a53e0cSJaegeuk Kim };
85239a53e0cSJaegeuk Kim 
8536b4afdd7SJaegeuk Kim struct flush_cmd {
8546b4afdd7SJaegeuk Kim 	struct completion wait;
855721bd4d5SGu Zheng 	struct llist_node llnode;
85639d787beSChao Yu 	nid_t ino;
8576b4afdd7SJaegeuk Kim 	int ret;
8586b4afdd7SJaegeuk Kim };
8596b4afdd7SJaegeuk Kim 
860a688b9d9SGu Zheng struct flush_cmd_control {
861a688b9d9SGu Zheng 	struct task_struct *f2fs_issue_flush;	/* flush thread */
862a688b9d9SGu Zheng 	wait_queue_head_t flush_wait_queue;	/* waiting queue for wake-up */
8638b8dd65fSChao Yu 	atomic_t issued_flush;			/* # of issued flushes */
8648b8dd65fSChao Yu 	atomic_t issing_flush;			/* # of issing flushes */
865721bd4d5SGu Zheng 	struct llist_head issue_list;		/* list for command issue */
866721bd4d5SGu Zheng 	struct llist_node *dispatch_list;	/* list for command dispatch */
867a688b9d9SGu Zheng };
868a688b9d9SGu Zheng 
86939a53e0cSJaegeuk Kim struct f2fs_sm_info {
87039a53e0cSJaegeuk Kim 	struct sit_info *sit_info;		/* whole segment information */
87139a53e0cSJaegeuk Kim 	struct free_segmap_info *free_info;	/* free segment information */
87239a53e0cSJaegeuk Kim 	struct dirty_seglist_info *dirty_info;	/* dirty segment information */
87339a53e0cSJaegeuk Kim 	struct curseg_info *curseg_array;	/* active segment information */
87439a53e0cSJaegeuk Kim 
8752b60311dSChao Yu 	struct rw_semaphore curseg_lock;	/* for preventing curseg change */
8762b60311dSChao Yu 
87739a53e0cSJaegeuk Kim 	block_t seg0_blkaddr;		/* block address of 0'th segment */
87839a53e0cSJaegeuk Kim 	block_t main_blkaddr;		/* start block address of main area */
87939a53e0cSJaegeuk Kim 	block_t ssa_blkaddr;		/* start block address of SSA area */
88039a53e0cSJaegeuk Kim 
88139a53e0cSJaegeuk Kim 	unsigned int segment_count;	/* total # of segments */
88239a53e0cSJaegeuk Kim 	unsigned int main_segments;	/* # of segments in main area */
88339a53e0cSJaegeuk Kim 	unsigned int reserved_segments;	/* # of reserved segments */
88439a53e0cSJaegeuk Kim 	unsigned int ovp_segments;	/* # of overprovision segments */
88581eb8d6eSJaegeuk Kim 
88681eb8d6eSJaegeuk Kim 	/* a threshold to reclaim prefree segments */
88781eb8d6eSJaegeuk Kim 	unsigned int rec_prefree_segments;
8887fd9e544SJaegeuk Kim 
889bba681cbSJaegeuk Kim 	/* for batched trimming */
890bba681cbSJaegeuk Kim 	unsigned int trim_sections;		/* # of sections to trim */
891bba681cbSJaegeuk Kim 
892184a5cd2SChao Yu 	struct list_head sit_entry_set;	/* sit entry set list */
893184a5cd2SChao Yu 
894216fbd64SJaegeuk Kim 	unsigned int ipu_policy;	/* in-place-update policy */
895216fbd64SJaegeuk Kim 	unsigned int min_ipu_util;	/* in-place-update threshold */
896c1ce1b02SJaegeuk Kim 	unsigned int min_fsync_blocks;	/* threshold for fsync */
897ef095d19SJaegeuk Kim 	unsigned int min_hot_blocks;	/* threshold for hot block allocation */
898a2a12b67SChao Yu 	unsigned int min_ssr_sections;	/* threshold to trigger SSR allocation */
8996b4afdd7SJaegeuk Kim 
9006b4afdd7SJaegeuk Kim 	/* for flush command control */
901b01a9201SJaegeuk Kim 	struct flush_cmd_control *fcc_info;
902a688b9d9SGu Zheng 
9030b54fb84SJaegeuk Kim 	/* for discard command control */
9040b54fb84SJaegeuk Kim 	struct discard_cmd_control *dcc_info;
90539a53e0cSJaegeuk Kim };
90639a53e0cSJaegeuk Kim 
90739a53e0cSJaegeuk Kim /*
90839a53e0cSJaegeuk Kim  * For superblock
90939a53e0cSJaegeuk Kim  */
91039a53e0cSJaegeuk Kim /*
91139a53e0cSJaegeuk Kim  * COUNT_TYPE for monitoring
91239a53e0cSJaegeuk Kim  *
91339a53e0cSJaegeuk Kim  * f2fs monitors the number of several block types such as on-writeback,
91439a53e0cSJaegeuk Kim  * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
91539a53e0cSJaegeuk Kim  */
91636951b38SChao Yu #define WB_DATA_TYPE(p)	(__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
91739a53e0cSJaegeuk Kim enum count_type {
91839a53e0cSJaegeuk Kim 	F2FS_DIRTY_DENTS,
919c227f912SChao Yu 	F2FS_DIRTY_DATA,
9202c8a4a28SJaegeuk Kim 	F2FS_DIRTY_QDATA,
92139a53e0cSJaegeuk Kim 	F2FS_DIRTY_NODES,
92239a53e0cSJaegeuk Kim 	F2FS_DIRTY_META,
9238dcf2ff7SJaegeuk Kim 	F2FS_INMEM_PAGES,
9240f18b462SJaegeuk Kim 	F2FS_DIRTY_IMETA,
92536951b38SChao Yu 	F2FS_WB_CP_DATA,
92636951b38SChao Yu 	F2FS_WB_DATA,
92739a53e0cSJaegeuk Kim 	NR_COUNT_TYPE,
92839a53e0cSJaegeuk Kim };
92939a53e0cSJaegeuk Kim 
93039a53e0cSJaegeuk Kim /*
931e1c42045Sarter97  * The below are the page types of bios used in submit_bio().
93239a53e0cSJaegeuk Kim  * The available types are:
93339a53e0cSJaegeuk Kim  * DATA			User data pages. It operates as async mode.
93439a53e0cSJaegeuk Kim  * NODE			Node pages. It operates as async mode.
93539a53e0cSJaegeuk Kim  * META			FS metadata pages such as SIT, NAT, CP.
93639a53e0cSJaegeuk Kim  * NR_PAGE_TYPE		The number of page types.
93739a53e0cSJaegeuk Kim  * META_FLUSH		Make sure the previous pages are written
93839a53e0cSJaegeuk Kim  *			with waiting the bio's completion
93939a53e0cSJaegeuk Kim  * ...			Only can be used with META.
94039a53e0cSJaegeuk Kim  */
9417d5e5109SJaegeuk Kim #define PAGE_TYPE_OF_BIO(type)	((type) > META ? META : (type))
94239a53e0cSJaegeuk Kim enum page_type {
94339a53e0cSJaegeuk Kim 	DATA,
94439a53e0cSJaegeuk Kim 	NODE,
94539a53e0cSJaegeuk Kim 	META,
94639a53e0cSJaegeuk Kim 	NR_PAGE_TYPE,
94739a53e0cSJaegeuk Kim 	META_FLUSH,
9488ce67cb0SJaegeuk Kim 	INMEM,		/* the below types are used by tracepoints only. */
9498ce67cb0SJaegeuk Kim 	INMEM_DROP,
9508c242db9SJaegeuk Kim 	INMEM_INVALIDATE,
95128bc106bSChao Yu 	INMEM_REVOKE,
9528ce67cb0SJaegeuk Kim 	IPU,
9538ce67cb0SJaegeuk Kim 	OPU,
95439a53e0cSJaegeuk Kim };
95539a53e0cSJaegeuk Kim 
956a912b54dSJaegeuk Kim enum temp_type {
957a912b54dSJaegeuk Kim 	HOT = 0,	/* must be zero for meta bio */
958a912b54dSJaegeuk Kim 	WARM,
959a912b54dSJaegeuk Kim 	COLD,
960a912b54dSJaegeuk Kim 	NR_TEMP_TYPE,
961a912b54dSJaegeuk Kim };
962a912b54dSJaegeuk Kim 
963cc15620bSJaegeuk Kim enum need_lock_type {
964cc15620bSJaegeuk Kim 	LOCK_REQ = 0,
965cc15620bSJaegeuk Kim 	LOCK_DONE,
966cc15620bSJaegeuk Kim 	LOCK_RETRY,
967cc15620bSJaegeuk Kim };
968cc15620bSJaegeuk Kim 
969a5fd5050SChao Yu enum cp_reason_type {
970a5fd5050SChao Yu 	CP_NO_NEEDED,
971a5fd5050SChao Yu 	CP_NON_REGULAR,
972a5fd5050SChao Yu 	CP_HARDLINK,
973a5fd5050SChao Yu 	CP_SB_NEED_CP,
974a5fd5050SChao Yu 	CP_WRONG_PINO,
975a5fd5050SChao Yu 	CP_NO_SPC_ROLL,
976a5fd5050SChao Yu 	CP_NODE_NEED_CP,
977a5fd5050SChao Yu 	CP_FASTBOOT_MODE,
978a5fd5050SChao Yu 	CP_SPEC_LOG_NUM,
9790a007b97SJaegeuk Kim 	CP_RECOVER_DIR,
980a5fd5050SChao Yu };
981a5fd5050SChao Yu 
982b0af6d49SChao Yu enum iostat_type {
983b0af6d49SChao Yu 	APP_DIRECT_IO,			/* app direct IOs */
984b0af6d49SChao Yu 	APP_BUFFERED_IO,		/* app buffered IOs */
985b0af6d49SChao Yu 	APP_WRITE_IO,			/* app write IOs */
986b0af6d49SChao Yu 	APP_MAPPED_IO,			/* app mapped IOs */
987b0af6d49SChao Yu 	FS_DATA_IO,			/* data IOs from kworker/fsync/reclaimer */
988b0af6d49SChao Yu 	FS_NODE_IO,			/* node IOs from kworker/fsync/reclaimer */
989b0af6d49SChao Yu 	FS_META_IO,			/* meta IOs from kworker/reclaimer */
990b0af6d49SChao Yu 	FS_GC_DATA_IO,			/* data IOs from forground gc */
991b0af6d49SChao Yu 	FS_GC_NODE_IO,			/* node IOs from forground gc */
992b0af6d49SChao Yu 	FS_CP_DATA_IO,			/* data IOs from checkpoint */
993b0af6d49SChao Yu 	FS_CP_NODE_IO,			/* node IOs from checkpoint */
994b0af6d49SChao Yu 	FS_CP_META_IO,			/* meta IOs from checkpoint */
995b0af6d49SChao Yu 	FS_DISCARD,			/* discard */
996b0af6d49SChao Yu 	NR_IO_TYPE,
997b0af6d49SChao Yu };
998b0af6d49SChao Yu 
999458e6197SJaegeuk Kim struct f2fs_io_info {
100005ca3632SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs_sb_info pointer */
100139d787beSChao Yu 	nid_t ino;		/* inode number */
1002458e6197SJaegeuk Kim 	enum page_type type;	/* contains DATA/NODE/META/META_FLUSH */
1003a912b54dSJaegeuk Kim 	enum temp_type temp;	/* contains HOT/WARM/COLD */
100404d328deSMike Christie 	int op;			/* contains REQ_OP_ */
1005ef295ecfSChristoph Hellwig 	int op_flags;		/* req_flag_bits */
10067a9d7548SChao Yu 	block_t new_blkaddr;	/* new block address to be written */
100728bc106bSChao Yu 	block_t old_blkaddr;	/* old block address before Cow */
100805ca3632SJaegeuk Kim 	struct page *page;	/* page to be written */
10094375a336SJaegeuk Kim 	struct page *encrypted_page;	/* encrypted page */
1010fb830fc5SChao Yu 	struct list_head list;		/* serialize IOs */
1011d68f735bSJaegeuk Kim 	bool submitted;		/* indicate IO submission */
1012cc15620bSJaegeuk Kim 	int need_lock;		/* indicate we need to lock cp_rwsem */
1013fb830fc5SChao Yu 	bool in_list;		/* indicate fio is in io_list */
10140833721eSYunlei He 	bool is_meta;		/* indicate borrow meta inode mapping or not */
1015fe16efe6SChao Yu 	bool retry;		/* need to reallocate block address */
1016b0af6d49SChao Yu 	enum iostat_type io_type;	/* io type */
1017578c6478SYufen Yu 	struct writeback_control *io_wbc; /* writeback control */
1018458e6197SJaegeuk Kim };
1019458e6197SJaegeuk Kim 
102068afcf2dSTomohiro Kusumi #define is_read_io(rw) ((rw) == READ)
10211ff7bd3bSJaegeuk Kim struct f2fs_bio_info {
1022458e6197SJaegeuk Kim 	struct f2fs_sb_info *sbi;	/* f2fs superblock */
10231ff7bd3bSJaegeuk Kim 	struct bio *bio;		/* bios to merge */
10241ff7bd3bSJaegeuk Kim 	sector_t last_block_in_bio;	/* last block number */
1025458e6197SJaegeuk Kim 	struct f2fs_io_info fio;	/* store buffered io info. */
1026df0f8dc0SChao Yu 	struct rw_semaphore io_rwsem;	/* blocking op for bio */
1027fb830fc5SChao Yu 	spinlock_t io_lock;		/* serialize DATA/NODE IOs */
1028fb830fc5SChao Yu 	struct list_head io_list;	/* track fios */
10291ff7bd3bSJaegeuk Kim };
10301ff7bd3bSJaegeuk Kim 
10313c62be17SJaegeuk Kim #define FDEV(i)				(sbi->devs[i])
10323c62be17SJaegeuk Kim #define RDEV(i)				(raw_super->devs[i])
10333c62be17SJaegeuk Kim struct f2fs_dev_info {
10343c62be17SJaegeuk Kim 	struct block_device *bdev;
10353c62be17SJaegeuk Kim 	char path[MAX_PATH_LEN];
10363c62be17SJaegeuk Kim 	unsigned int total_segments;
10373c62be17SJaegeuk Kim 	block_t start_blk;
10383c62be17SJaegeuk Kim 	block_t end_blk;
10393c62be17SJaegeuk Kim #ifdef CONFIG_BLK_DEV_ZONED
10403c62be17SJaegeuk Kim 	unsigned int nr_blkz;			/* Total number of zones */
10413c62be17SJaegeuk Kim 	u8 *blkz_type;				/* Array of zones type */
10423c62be17SJaegeuk Kim #endif
10433c62be17SJaegeuk Kim };
10443c62be17SJaegeuk Kim 
1045c227f912SChao Yu enum inode_type {
1046c227f912SChao Yu 	DIR_INODE,			/* for dirty dir inode */
1047c227f912SChao Yu 	FILE_INODE,			/* for dirty regular/symlink inode */
10480f18b462SJaegeuk Kim 	DIRTY_META,			/* for all dirtied inode metadata */
104957864ae5SJaegeuk Kim 	ATOMIC_FILE,			/* for all atomic files */
1050c227f912SChao Yu 	NR_INODE_TYPE,
1051c227f912SChao Yu };
1052c227f912SChao Yu 
105367298804SChao Yu /* for inner inode cache management */
105467298804SChao Yu struct inode_management {
105567298804SChao Yu 	struct radix_tree_root ino_root;	/* ino entry array */
105667298804SChao Yu 	spinlock_t ino_lock;			/* for ino entry lock */
105767298804SChao Yu 	struct list_head ino_list;		/* inode list head */
105867298804SChao Yu 	unsigned long ino_num;			/* number of entries */
105967298804SChao Yu };
106067298804SChao Yu 
1061caf0047eSChao Yu /* For s_flag in struct f2fs_sb_info */
1062caf0047eSChao Yu enum {
1063caf0047eSChao Yu 	SBI_IS_DIRTY,				/* dirty flag for checkpoint */
1064caf0047eSChao Yu 	SBI_IS_CLOSE,				/* specify unmounting */
1065caf0047eSChao Yu 	SBI_NEED_FSCK,				/* need fsck.f2fs to fix */
1066caf0047eSChao Yu 	SBI_POR_DOING,				/* recovery is doing or not */
1067df728b0fSJaegeuk Kim 	SBI_NEED_SB_WRITE,			/* need to recover superblock */
1068bbf156f7SJaegeuk Kim 	SBI_NEED_CP,				/* need to checkpoint */
1069caf0047eSChao Yu };
1070caf0047eSChao Yu 
10716beceb54SJaegeuk Kim enum {
10726beceb54SJaegeuk Kim 	CP_TIME,
1073d0239e1bSJaegeuk Kim 	REQ_TIME,
10746beceb54SJaegeuk Kim 	MAX_TIME,
10756beceb54SJaegeuk Kim };
10766beceb54SJaegeuk Kim 
10770cdd3195SHyunchul Lee enum {
10785b0e9539SJaegeuk Kim 	GC_NORMAL,
10795b0e9539SJaegeuk Kim 	GC_IDLE_CB,
10805b0e9539SJaegeuk Kim 	GC_IDLE_GREEDY,
10815b0e9539SJaegeuk Kim 	GC_URGENT,
10825b0e9539SJaegeuk Kim };
10835b0e9539SJaegeuk Kim 
10845b0e9539SJaegeuk Kim enum {
10850cdd3195SHyunchul Lee 	WHINT_MODE_OFF,		/* not pass down write hints */
10860cdd3195SHyunchul Lee 	WHINT_MODE_USER,	/* try to pass down hints given by users */
1087f2e703f9SHyunchul Lee 	WHINT_MODE_FS,		/* pass down hints with F2FS policy */
10880cdd3195SHyunchul Lee };
10890cdd3195SHyunchul Lee 
109007939627SJaegeuk Kim enum {
109107939627SJaegeuk Kim 	ALLOC_MODE_DEFAULT,	/* stay default */
109207939627SJaegeuk Kim 	ALLOC_MODE_REUSE,	/* reuse segments as much as possible */
109307939627SJaegeuk Kim };
109407939627SJaegeuk Kim 
109593cf93f1SJunling Zheng enum fsync_mode {
109693cf93f1SJunling Zheng 	FSYNC_MODE_POSIX,	/* fsync follows posix semantics */
109793cf93f1SJunling Zheng 	FSYNC_MODE_STRICT,	/* fsync behaves in line with ext4 */
1098d6290814SJaegeuk Kim 	FSYNC_MODE_NOBARRIER,	/* fsync behaves nobarrier based on posix */
109993cf93f1SJunling Zheng };
110093cf93f1SJunling Zheng 
1101ff62af20SSheng Yong #ifdef CONFIG_F2FS_FS_ENCRYPTION
1102ff62af20SSheng Yong #define DUMMY_ENCRYPTION_ENABLED(sbi) \
1103ff62af20SSheng Yong 			(unlikely(F2FS_OPTION(sbi).test_dummy_encryption))
1104ff62af20SSheng Yong #else
1105ff62af20SSheng Yong #define DUMMY_ENCRYPTION_ENABLED(sbi) (0)
1106ff62af20SSheng Yong #endif
1107ff62af20SSheng Yong 
110839a53e0cSJaegeuk Kim struct f2fs_sb_info {
110939a53e0cSJaegeuk Kim 	struct super_block *sb;			/* pointer to VFS super block */
11105e176d54SJaegeuk Kim 	struct proc_dir_entry *s_proc;		/* proc entry */
111139a53e0cSJaegeuk Kim 	struct f2fs_super_block *raw_super;	/* raw super block pointer */
1112846ae671SChao Yu 	struct rw_semaphore sb_lock;		/* lock for raw super block */
1113e8240f65SChao Yu 	int valid_super_block;			/* valid super block no */
1114fadb2fb8SChao Yu 	unsigned long s_flag;				/* flags for sbi */
111539a53e0cSJaegeuk Kim 
1116178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
1117178053e2SDamien Le Moal 	unsigned int blocks_per_blkz;		/* F2FS blocks per zone */
1118178053e2SDamien Le Moal 	unsigned int log_blocks_per_blkz;	/* log2 F2FS blocks per zone */
1119178053e2SDamien Le Moal #endif
1120178053e2SDamien Le Moal 
112139a53e0cSJaegeuk Kim 	/* for node-related operations */
112239a53e0cSJaegeuk Kim 	struct f2fs_nm_info *nm_info;		/* node manager */
112339a53e0cSJaegeuk Kim 	struct inode *node_inode;		/* cache node blocks */
112439a53e0cSJaegeuk Kim 
112539a53e0cSJaegeuk Kim 	/* for segment-related operations */
112639a53e0cSJaegeuk Kim 	struct f2fs_sm_info *sm_info;		/* segment manager */
11271ff7bd3bSJaegeuk Kim 
11281ff7bd3bSJaegeuk Kim 	/* for bio operations */
1129a912b54dSJaegeuk Kim 	struct f2fs_bio_info *write_io[NR_PAGE_TYPE];	/* for write bios */
1130e41e6d75SChao Yu 	struct mutex wio_mutex[NR_PAGE_TYPE - 1][NR_TEMP_TYPE];
1131e41e6d75SChao Yu 						/* bio ordering for NODE/DATA */
1132107a805dSChao Yu 	/* keep migration IO order for LFS mode */
1133107a805dSChao Yu 	struct rw_semaphore io_order_lock;
11340a595ebaSJaegeuk Kim 	mempool_t *write_io_dummy;		/* Dummy pages */
113539a53e0cSJaegeuk Kim 
113639a53e0cSJaegeuk Kim 	/* for checkpoint */
113739a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt;		/* raw checkpoint pointer */
11388508e44aSJaegeuk Kim 	int cur_cp_pack;			/* remain current cp pack */
1139aaec2b1dSChao Yu 	spinlock_t cp_lock;			/* for flag in ckpt */
114039a53e0cSJaegeuk Kim 	struct inode *meta_inode;		/* cache meta blocks */
114139936837SJaegeuk Kim 	struct mutex cp_mutex;			/* checkpoint procedure lock */
1142b873b798SJaegeuk Kim 	struct rw_semaphore cp_rwsem;		/* blocking FS operations */
1143b3582c68SChao Yu 	struct rw_semaphore node_write;		/* locking node writes */
114459c9081bSYunlei He 	struct rw_semaphore node_change;	/* locking node change */
1145fb51b5efSChangman Lee 	wait_queue_head_t cp_wait;
11466beceb54SJaegeuk Kim 	unsigned long last_time[MAX_TIME];	/* to store time in jiffies */
11476beceb54SJaegeuk Kim 	long interval_time[MAX_TIME];		/* to store thresholds */
114839a53e0cSJaegeuk Kim 
114967298804SChao Yu 	struct inode_management im[MAX_INO_ENTRY];      /* manage inode cache */
11506451e041SJaegeuk Kim 
11516451e041SJaegeuk Kim 	/* for orphan inode, use 0'th array */
11520d47c1adSGu Zheng 	unsigned int max_orphans;		/* max orphan inodes */
115339a53e0cSJaegeuk Kim 
1154c227f912SChao Yu 	/* for inode management */
1155c227f912SChao Yu 	struct list_head inode_list[NR_INODE_TYPE];	/* dirty inode list */
1156c227f912SChao Yu 	spinlock_t inode_lock[NR_INODE_TYPE];	/* for dirty inode list lock */
115739a53e0cSJaegeuk Kim 
115813054c54SChao Yu 	/* for extent tree cache */
115913054c54SChao Yu 	struct radix_tree_root extent_tree_root;/* cache extent cache entries */
11605e8256acSYunlei He 	struct mutex extent_tree_lock;	/* locking extent radix tree */
116113054c54SChao Yu 	struct list_head extent_list;		/* lru list for shrinker */
116213054c54SChao Yu 	spinlock_t extent_lock;			/* locking extent lru list */
11637441ccefSJaegeuk Kim 	atomic_t total_ext_tree;		/* extent tree count */
1164137d09f0SJaegeuk Kim 	struct list_head zombie_list;		/* extent zombie tree list */
116574fd8d99SJaegeuk Kim 	atomic_t total_zombie_tree;		/* extent zombie tree count */
116613054c54SChao Yu 	atomic_t total_ext_node;		/* extent info count */
116713054c54SChao Yu 
116839a53e0cSJaegeuk Kim 	/* basic filesystem units */
116939a53e0cSJaegeuk Kim 	unsigned int log_sectors_per_block;	/* log2 sectors per block */
117039a53e0cSJaegeuk Kim 	unsigned int log_blocksize;		/* log2 block size */
117139a53e0cSJaegeuk Kim 	unsigned int blocksize;			/* block size */
117239a53e0cSJaegeuk Kim 	unsigned int root_ino_num;		/* root inode number*/
117339a53e0cSJaegeuk Kim 	unsigned int node_ino_num;		/* node inode number*/
117439a53e0cSJaegeuk Kim 	unsigned int meta_ino_num;		/* meta inode number*/
117539a53e0cSJaegeuk Kim 	unsigned int log_blocks_per_seg;	/* log2 blocks per segment */
117639a53e0cSJaegeuk Kim 	unsigned int blocks_per_seg;		/* blocks per segment */
117739a53e0cSJaegeuk Kim 	unsigned int segs_per_sec;		/* segments per section */
117839a53e0cSJaegeuk Kim 	unsigned int secs_per_zone;		/* sections per zone */
117939a53e0cSJaegeuk Kim 	unsigned int total_sections;		/* total section count */
118039a53e0cSJaegeuk Kim 	unsigned int total_node_count;		/* total node block count */
118139a53e0cSJaegeuk Kim 	unsigned int total_valid_node_count;	/* valid node block count */
1182e0afc4d6SChao Yu 	loff_t max_file_blocks;			/* max block index of file */
1183ab9fa662SJaegeuk Kim 	int dir_level;				/* directory level */
1184a2a12b67SChao Yu 	unsigned int trigger_ssr_threshold;	/* threshold to trigger ssr */
1185f6df8f23SSheng Yong 	int readdir_ra;				/* readahead inode in readdir */
118639a53e0cSJaegeuk Kim 
118739a53e0cSJaegeuk Kim 	block_t user_block_count;		/* # of user blocks */
118839a53e0cSJaegeuk Kim 	block_t total_valid_block_count;	/* # of valid blocks */
1189a66cdd98SJaegeuk Kim 	block_t discard_blks;			/* discard command candidats */
119039a53e0cSJaegeuk Kim 	block_t last_valid_block_count;		/* for recovery */
1191daeb433eSChao Yu 	block_t reserved_blocks;		/* configurable reserved blocks */
119280d42145SYunlong Song 	block_t current_reserved_blocks;	/* current reserved blocks */
1193daeb433eSChao Yu 
1194292c196aSChao Yu 	unsigned int nquota_files;		/* # of quota sysfile */
1195292c196aSChao Yu 
119639a53e0cSJaegeuk Kim 	u32 s_next_generation;			/* for NFS support */
1197523be8a6SJaegeuk Kim 
1198523be8a6SJaegeuk Kim 	/* # of pages, see count_type */
119935782b23SJaegeuk Kim 	atomic_t nr_pages[NR_COUNT_TYPE];
120041382ec4SJaegeuk Kim 	/* # of allocated blocks */
120141382ec4SJaegeuk Kim 	struct percpu_counter alloc_valid_block_count;
120239a53e0cSJaegeuk Kim 
1203687de7f1SJaegeuk Kim 	/* writeback control */
1204c29fd0c0SChao Yu 	atomic_t wb_sync_req[META];	/* count # of WB_SYNC threads */
1205687de7f1SJaegeuk Kim 
1206513c5f37SJaegeuk Kim 	/* valid inode count */
1207513c5f37SJaegeuk Kim 	struct percpu_counter total_valid_inode_count;
1208513c5f37SJaegeuk Kim 
120939a53e0cSJaegeuk Kim 	struct f2fs_mount_info mount_opt;	/* mount options */
121039a53e0cSJaegeuk Kim 
121139a53e0cSJaegeuk Kim 	/* for cleaning operations */
121239a53e0cSJaegeuk Kim 	struct mutex gc_mutex;			/* mutex for GC */
121339a53e0cSJaegeuk Kim 	struct f2fs_gc_kthread	*gc_thread;	/* GC thread */
12145ec4e49fSJaegeuk Kim 	unsigned int cur_victim_sec;		/* current victim section num */
12155b0e9539SJaegeuk Kim 	unsigned int gc_mode;			/* current GC state */
12162ef79ecbSChao Yu 	/* for skip statistic */
12172ef79ecbSChao Yu 	unsigned long long skipped_atomic_files[2];	/* FG_GC and BG_GC */
121839a53e0cSJaegeuk Kim 
12191ad71a27SJaegeuk Kim 	/* threshold for gc trials on pinned files */
12201ad71a27SJaegeuk Kim 	u64 gc_pin_file_threshold;
12211ad71a27SJaegeuk Kim 
1222b1c57c1cSJaegeuk Kim 	/* maximum # of trials to find a victim segment for SSR and GC */
1223b1c57c1cSJaegeuk Kim 	unsigned int max_victim_search;
1224b1c57c1cSJaegeuk Kim 
122539a53e0cSJaegeuk Kim 	/*
122639a53e0cSJaegeuk Kim 	 * for stat information.
122739a53e0cSJaegeuk Kim 	 * one is for the LFS mode, and the other is for the SSR mode.
122839a53e0cSJaegeuk Kim 	 */
122935b09d82SNamjae Jeon #ifdef CONFIG_F2FS_STAT_FS
123039a53e0cSJaegeuk Kim 	struct f2fs_stat_info *stat_info;	/* FS status information */
123139a53e0cSJaegeuk Kim 	unsigned int segment_count[2];		/* # of allocated segments */
123239a53e0cSJaegeuk Kim 	unsigned int block_count[2];		/* # of allocated blocks */
1233b9a2c252SChangman Lee 	atomic_t inplace_count;		/* # of inplace update */
12345b7ee374SChao Yu 	atomic64_t total_hit_ext;		/* # of lookup extent cache */
12355b7ee374SChao Yu 	atomic64_t read_hit_rbtree;		/* # of hit rbtree extent node */
12365b7ee374SChao Yu 	atomic64_t read_hit_largest;		/* # of hit largest extent node */
12375b7ee374SChao Yu 	atomic64_t read_hit_cached;		/* # of hit cached extent node */
1238d5e8f6c9SChao Yu 	atomic_t inline_xattr;			/* # of inline_xattr inodes */
123903e14d52SChao Yu 	atomic_t inline_inode;			/* # of inline_data inodes */
124003e14d52SChao Yu 	atomic_t inline_dir;			/* # of inline_dentry inodes */
124126a28a0cSJaegeuk Kim 	atomic_t aw_cnt;			/* # of atomic writes */
1242648d50baSChao Yu 	atomic_t vw_cnt;			/* # of volatile writes */
124326a28a0cSJaegeuk Kim 	atomic_t max_aw_cnt;			/* max # of atomic writes */
1244648d50baSChao Yu 	atomic_t max_vw_cnt;			/* max # of volatile writes */
124539a53e0cSJaegeuk Kim 	int bg_gc;				/* background gc calls */
124633fbd510SChao Yu 	unsigned int ndirty_inode[NR_INODE_TYPE];	/* # of dirty inodes */
124735b09d82SNamjae Jeon #endif
124839a53e0cSJaegeuk Kim 	spinlock_t stat_lock;			/* lock for stat operations */
1249b59d0baeSNamjae Jeon 
1250b0af6d49SChao Yu 	/* For app/fs IO statistics */
1251b0af6d49SChao Yu 	spinlock_t iostat_lock;
1252b0af6d49SChao Yu 	unsigned long long write_iostat[NR_IO_TYPE];
1253b0af6d49SChao Yu 	bool iostat_enable;
1254b0af6d49SChao Yu 
1255b59d0baeSNamjae Jeon 	/* For sysfs suppport */
1256b59d0baeSNamjae Jeon 	struct kobject s_kobj;
1257b59d0baeSNamjae Jeon 	struct completion s_kobj_unregister;
12582658e50dSJaegeuk Kim 
12592658e50dSJaegeuk Kim 	/* For shrinker support */
12602658e50dSJaegeuk Kim 	struct list_head s_list;
12613c62be17SJaegeuk Kim 	int s_ndevs;				/* number of devices */
12623c62be17SJaegeuk Kim 	struct f2fs_dev_info *devs;		/* for device list */
12631228b482SChao Yu 	unsigned int dirty_device;		/* for checkpoint data flush */
12641228b482SChao Yu 	spinlock_t dev_lock;			/* protect dirty_device */
12652658e50dSJaegeuk Kim 	struct mutex umount_mutex;
12662658e50dSJaegeuk Kim 	unsigned int shrinker_run_no;
12678f1dbbbbSShuoran Liu 
12688f1dbbbbSShuoran Liu 	/* For write statistics */
12698f1dbbbbSShuoran Liu 	u64 sectors_written_start;
12708f1dbbbbSShuoran Liu 	u64 kbytes_written;
127143b6573bSKeith Mok 
127243b6573bSKeith Mok 	/* Reference to checksum algorithm driver via cryptoapi */
127343b6573bSKeith Mok 	struct crypto_shash *s_chksum_driver;
12741ecc0c5cSChao Yu 
1275704956ecSChao Yu 	/* Precomputed FS UUID checksum for seeding other checksums */
1276704956ecSChao Yu 	__u32 s_chksum_seed;
127739a53e0cSJaegeuk Kim };
127839a53e0cSJaegeuk Kim 
12791ecc0c5cSChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
128055523519SChao Yu #define f2fs_show_injection_info(type)				\
128155523519SChao Yu 	printk("%sF2FS-fs : inject %s in %s of %pF\n",		\
128255523519SChao Yu 		KERN_INFO, fault_name[type],			\
128355523519SChao Yu 		__func__, __builtin_return_address(0))
12841ecc0c5cSChao Yu static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
12851ecc0c5cSChao Yu {
128663189b78SChao Yu 	struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
12871ecc0c5cSChao Yu 
12881ecc0c5cSChao Yu 	if (!ffi->inject_rate)
12891ecc0c5cSChao Yu 		return false;
12901ecc0c5cSChao Yu 
12911ecc0c5cSChao Yu 	if (!IS_FAULT_SET(ffi, type))
12921ecc0c5cSChao Yu 		return false;
12931ecc0c5cSChao Yu 
12941ecc0c5cSChao Yu 	atomic_inc(&ffi->inject_ops);
12951ecc0c5cSChao Yu 	if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
12961ecc0c5cSChao Yu 		atomic_set(&ffi->inject_ops, 0);
12971ecc0c5cSChao Yu 		return true;
12981ecc0c5cSChao Yu 	}
12991ecc0c5cSChao Yu 	return false;
13001ecc0c5cSChao Yu }
13011ecc0c5cSChao Yu #endif
13021ecc0c5cSChao Yu 
13038f1dbbbbSShuoran Liu /* For write statistics. Suppose sector size is 512 bytes,
13048f1dbbbbSShuoran Liu  * and the return value is in kbytes. s is of struct f2fs_sb_info.
13058f1dbbbbSShuoran Liu  */
13068f1dbbbbSShuoran Liu #define BD_PART_WRITTEN(s)						 \
1307*dbae2c55SMichael Callahan (((u64)part_stat_read((s)->sb->s_bdev->bd_part, sectors[STAT_WRITE]) -   \
130868afcf2dSTomohiro Kusumi 		(s)->sectors_written_start) >> 1)
13098f1dbbbbSShuoran Liu 
13106beceb54SJaegeuk Kim static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
13116beceb54SJaegeuk Kim {
13126beceb54SJaegeuk Kim 	sbi->last_time[type] = jiffies;
13136beceb54SJaegeuk Kim }
13146beceb54SJaegeuk Kim 
13156beceb54SJaegeuk Kim static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
13166beceb54SJaegeuk Kim {
13176bccfa19SArnd Bergmann 	unsigned long interval = sbi->interval_time[type] * HZ;
13186beceb54SJaegeuk Kim 
13196beceb54SJaegeuk Kim 	return time_after(jiffies, sbi->last_time[type] + interval);
13206beceb54SJaegeuk Kim }
13216beceb54SJaegeuk Kim 
1322d0239e1bSJaegeuk Kim static inline bool is_idle(struct f2fs_sb_info *sbi)
1323d0239e1bSJaegeuk Kim {
1324d0239e1bSJaegeuk Kim 	struct block_device *bdev = sbi->sb->s_bdev;
1325d0239e1bSJaegeuk Kim 	struct request_queue *q = bdev_get_queue(bdev);
1326d0239e1bSJaegeuk Kim 	struct request_list *rl = &q->root_rl;
1327d0239e1bSJaegeuk Kim 
1328d0239e1bSJaegeuk Kim 	if (rl->count[BLK_RW_SYNC] || rl->count[BLK_RW_ASYNC])
1329d0239e1bSJaegeuk Kim 		return 0;
1330d0239e1bSJaegeuk Kim 
1331d0239e1bSJaegeuk Kim 	return f2fs_time_over(sbi, REQ_TIME);
1332d0239e1bSJaegeuk Kim }
1333d0239e1bSJaegeuk Kim 
133439a53e0cSJaegeuk Kim /*
133539a53e0cSJaegeuk Kim  * Inline functions
133639a53e0cSJaegeuk Kim  */
1337416d2dbbSChao Yu static inline u32 __f2fs_crc32(struct f2fs_sb_info *sbi, u32 crc,
1338704956ecSChao Yu 			      const void *address, unsigned int length)
1339704956ecSChao Yu {
1340704956ecSChao Yu 	struct {
1341704956ecSChao Yu 		struct shash_desc shash;
1342704956ecSChao Yu 		char ctx[4];
1343704956ecSChao Yu 	} desc;
1344704956ecSChao Yu 	int err;
1345704956ecSChao Yu 
1346704956ecSChao Yu 	BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver) != sizeof(desc.ctx));
1347704956ecSChao Yu 
1348704956ecSChao Yu 	desc.shash.tfm = sbi->s_chksum_driver;
1349704956ecSChao Yu 	desc.shash.flags = 0;
1350704956ecSChao Yu 	*(u32 *)desc.ctx = crc;
1351704956ecSChao Yu 
1352704956ecSChao Yu 	err = crypto_shash_update(&desc.shash, address, length);
1353704956ecSChao Yu 	BUG_ON(err);
1354704956ecSChao Yu 
1355704956ecSChao Yu 	return *(u32 *)desc.ctx;
1356704956ecSChao Yu }
1357704956ecSChao Yu 
1358416d2dbbSChao Yu static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
1359416d2dbbSChao Yu 			   unsigned int length)
1360416d2dbbSChao Yu {
1361416d2dbbSChao Yu 	return __f2fs_crc32(sbi, F2FS_SUPER_MAGIC, address, length);
1362416d2dbbSChao Yu }
1363416d2dbbSChao Yu 
1364416d2dbbSChao Yu static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
1365416d2dbbSChao Yu 				  void *buf, size_t buf_size)
1366416d2dbbSChao Yu {
1367416d2dbbSChao Yu 	return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
1368416d2dbbSChao Yu }
1369416d2dbbSChao Yu 
1370416d2dbbSChao Yu static inline u32 f2fs_chksum(struct f2fs_sb_info *sbi, u32 crc,
1371416d2dbbSChao Yu 			      const void *address, unsigned int length)
1372416d2dbbSChao Yu {
1373416d2dbbSChao Yu 	return __f2fs_crc32(sbi, crc, address, length);
1374416d2dbbSChao Yu }
1375416d2dbbSChao Yu 
137639a53e0cSJaegeuk Kim static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
137739a53e0cSJaegeuk Kim {
137839a53e0cSJaegeuk Kim 	return container_of(inode, struct f2fs_inode_info, vfs_inode);
137939a53e0cSJaegeuk Kim }
138039a53e0cSJaegeuk Kim 
138139a53e0cSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
138239a53e0cSJaegeuk Kim {
138339a53e0cSJaegeuk Kim 	return sb->s_fs_info;
138439a53e0cSJaegeuk Kim }
138539a53e0cSJaegeuk Kim 
13864081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
13874081363fSJaegeuk Kim {
13884081363fSJaegeuk Kim 	return F2FS_SB(inode->i_sb);
13894081363fSJaegeuk Kim }
13904081363fSJaegeuk Kim 
13914081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
13924081363fSJaegeuk Kim {
13934081363fSJaegeuk Kim 	return F2FS_I_SB(mapping->host);
13944081363fSJaegeuk Kim }
13954081363fSJaegeuk Kim 
13964081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
13974081363fSJaegeuk Kim {
13984081363fSJaegeuk Kim 	return F2FS_M_SB(page->mapping);
13994081363fSJaegeuk Kim }
14004081363fSJaegeuk Kim 
140139a53e0cSJaegeuk Kim static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
140239a53e0cSJaegeuk Kim {
140339a53e0cSJaegeuk Kim 	return (struct f2fs_super_block *)(sbi->raw_super);
140439a53e0cSJaegeuk Kim }
140539a53e0cSJaegeuk Kim 
140639a53e0cSJaegeuk Kim static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
140739a53e0cSJaegeuk Kim {
140839a53e0cSJaegeuk Kim 	return (struct f2fs_checkpoint *)(sbi->ckpt);
140939a53e0cSJaegeuk Kim }
141039a53e0cSJaegeuk Kim 
141145590710SGu Zheng static inline struct f2fs_node *F2FS_NODE(struct page *page)
141245590710SGu Zheng {
141345590710SGu Zheng 	return (struct f2fs_node *)page_address(page);
141445590710SGu Zheng }
141545590710SGu Zheng 
141658bfaf44SJaegeuk Kim static inline struct f2fs_inode *F2FS_INODE(struct page *page)
141758bfaf44SJaegeuk Kim {
141858bfaf44SJaegeuk Kim 	return &((struct f2fs_node *)page_address(page))->i;
141958bfaf44SJaegeuk Kim }
142058bfaf44SJaegeuk Kim 
142139a53e0cSJaegeuk Kim static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
142239a53e0cSJaegeuk Kim {
142339a53e0cSJaegeuk Kim 	return (struct f2fs_nm_info *)(sbi->nm_info);
142439a53e0cSJaegeuk Kim }
142539a53e0cSJaegeuk Kim 
142639a53e0cSJaegeuk Kim static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
142739a53e0cSJaegeuk Kim {
142839a53e0cSJaegeuk Kim 	return (struct f2fs_sm_info *)(sbi->sm_info);
142939a53e0cSJaegeuk Kim }
143039a53e0cSJaegeuk Kim 
143139a53e0cSJaegeuk Kim static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
143239a53e0cSJaegeuk Kim {
143339a53e0cSJaegeuk Kim 	return (struct sit_info *)(SM_I(sbi)->sit_info);
143439a53e0cSJaegeuk Kim }
143539a53e0cSJaegeuk Kim 
143639a53e0cSJaegeuk Kim static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
143739a53e0cSJaegeuk Kim {
143839a53e0cSJaegeuk Kim 	return (struct free_segmap_info *)(SM_I(sbi)->free_info);
143939a53e0cSJaegeuk Kim }
144039a53e0cSJaegeuk Kim 
144139a53e0cSJaegeuk Kim static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
144239a53e0cSJaegeuk Kim {
144339a53e0cSJaegeuk Kim 	return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
144439a53e0cSJaegeuk Kim }
144539a53e0cSJaegeuk Kim 
14469df27d98SGu Zheng static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
14479df27d98SGu Zheng {
14489df27d98SGu Zheng 	return sbi->meta_inode->i_mapping;
14499df27d98SGu Zheng }
14509df27d98SGu Zheng 
14514ef51a8fSJaegeuk Kim static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
14524ef51a8fSJaegeuk Kim {
14534ef51a8fSJaegeuk Kim 	return sbi->node_inode->i_mapping;
14544ef51a8fSJaegeuk Kim }
14554ef51a8fSJaegeuk Kim 
1456caf0047eSChao Yu static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
145739a53e0cSJaegeuk Kim {
1458fadb2fb8SChao Yu 	return test_bit(type, &sbi->s_flag);
145939a53e0cSJaegeuk Kim }
146039a53e0cSJaegeuk Kim 
1461caf0047eSChao Yu static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
146239a53e0cSJaegeuk Kim {
1463fadb2fb8SChao Yu 	set_bit(type, &sbi->s_flag);
1464caf0047eSChao Yu }
1465caf0047eSChao Yu 
1466caf0047eSChao Yu static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
1467caf0047eSChao Yu {
1468fadb2fb8SChao Yu 	clear_bit(type, &sbi->s_flag);
146939a53e0cSJaegeuk Kim }
147039a53e0cSJaegeuk Kim 
1471d71b5564SJaegeuk Kim static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
1472d71b5564SJaegeuk Kim {
1473d71b5564SJaegeuk Kim 	return le64_to_cpu(cp->checkpoint_ver);
1474d71b5564SJaegeuk Kim }
1475d71b5564SJaegeuk Kim 
1476ea676733SJaegeuk Kim static inline unsigned long f2fs_qf_ino(struct super_block *sb, int type)
1477ea676733SJaegeuk Kim {
1478ea676733SJaegeuk Kim 	if (type < F2FS_MAX_QUOTAS)
1479ea676733SJaegeuk Kim 		return le32_to_cpu(F2FS_SB(sb)->raw_super->qf_ino[type]);
1480ea676733SJaegeuk Kim 	return 0;
1481ea676733SJaegeuk Kim }
1482ea676733SJaegeuk Kim 
1483ced2c7eaSKinglong Mee static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
1484ced2c7eaSKinglong Mee {
1485ced2c7eaSKinglong Mee 	size_t crc_offset = le32_to_cpu(cp->checksum_offset);
1486ced2c7eaSKinglong Mee 	return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
1487ced2c7eaSKinglong Mee }
1488ced2c7eaSKinglong Mee 
1489aaec2b1dSChao Yu static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
149025ca923bSJaegeuk Kim {
149125ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
1492aaec2b1dSChao Yu 
149325ca923bSJaegeuk Kim 	return ckpt_flags & f;
149425ca923bSJaegeuk Kim }
149525ca923bSJaegeuk Kim 
1496aaec2b1dSChao Yu static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
149725ca923bSJaegeuk Kim {
1498aaec2b1dSChao Yu 	return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
1499aaec2b1dSChao Yu }
1500aaec2b1dSChao Yu 
1501aaec2b1dSChao Yu static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1502aaec2b1dSChao Yu {
1503aaec2b1dSChao Yu 	unsigned int ckpt_flags;
1504aaec2b1dSChao Yu 
1505aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
150625ca923bSJaegeuk Kim 	ckpt_flags |= f;
150725ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
150825ca923bSJaegeuk Kim }
150925ca923bSJaegeuk Kim 
1510aaec2b1dSChao Yu static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
151125ca923bSJaegeuk Kim {
1512d1aa2453SChao Yu 	unsigned long flags;
1513d1aa2453SChao Yu 
1514d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1515aaec2b1dSChao Yu 	__set_ckpt_flags(F2FS_CKPT(sbi), f);
1516d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1517aaec2b1dSChao Yu }
1518aaec2b1dSChao Yu 
1519aaec2b1dSChao Yu static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1520aaec2b1dSChao Yu {
1521aaec2b1dSChao Yu 	unsigned int ckpt_flags;
1522aaec2b1dSChao Yu 
1523aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
152425ca923bSJaegeuk Kim 	ckpt_flags &= (~f);
152525ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
152625ca923bSJaegeuk Kim }
152725ca923bSJaegeuk Kim 
1528aaec2b1dSChao Yu static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
1529aaec2b1dSChao Yu {
1530d1aa2453SChao Yu 	unsigned long flags;
1531d1aa2453SChao Yu 
1532d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1533aaec2b1dSChao Yu 	__clear_ckpt_flags(F2FS_CKPT(sbi), f);
1534d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1535aaec2b1dSChao Yu }
1536aaec2b1dSChao Yu 
153722ad0b6aSJaegeuk Kim static inline void disable_nat_bits(struct f2fs_sb_info *sbi, bool lock)
153822ad0b6aSJaegeuk Kim {
1539d1aa2453SChao Yu 	unsigned long flags;
1540d1aa2453SChao Yu 
154122ad0b6aSJaegeuk Kim 	set_sbi_flag(sbi, SBI_NEED_FSCK);
154222ad0b6aSJaegeuk Kim 
154322ad0b6aSJaegeuk Kim 	if (lock)
1544d1aa2453SChao Yu 		spin_lock_irqsave(&sbi->cp_lock, flags);
154522ad0b6aSJaegeuk Kim 	__clear_ckpt_flags(F2FS_CKPT(sbi), CP_NAT_BITS_FLAG);
154622ad0b6aSJaegeuk Kim 	kfree(NM_I(sbi)->nat_bits);
154722ad0b6aSJaegeuk Kim 	NM_I(sbi)->nat_bits = NULL;
154822ad0b6aSJaegeuk Kim 	if (lock)
1549d1aa2453SChao Yu 		spin_unlock_irqrestore(&sbi->cp_lock, flags);
155022ad0b6aSJaegeuk Kim }
155122ad0b6aSJaegeuk Kim 
155222ad0b6aSJaegeuk Kim static inline bool enabled_nat_bits(struct f2fs_sb_info *sbi,
155322ad0b6aSJaegeuk Kim 					struct cp_control *cpc)
155422ad0b6aSJaegeuk Kim {
155522ad0b6aSJaegeuk Kim 	bool set = is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
155622ad0b6aSJaegeuk Kim 
1557c473f1a9SChao Yu 	return (cpc) ? (cpc->reason & CP_UMOUNT) && set : set;
155822ad0b6aSJaegeuk Kim }
155922ad0b6aSJaegeuk Kim 
1560e479556bSGu Zheng static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
156139a53e0cSJaegeuk Kim {
1562b873b798SJaegeuk Kim 	down_read(&sbi->cp_rwsem);
156339a53e0cSJaegeuk Kim }
156439a53e0cSJaegeuk Kim 
1565cc15620bSJaegeuk Kim static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi)
1566cc15620bSJaegeuk Kim {
1567cc15620bSJaegeuk Kim 	return down_read_trylock(&sbi->cp_rwsem);
1568cc15620bSJaegeuk Kim }
1569cc15620bSJaegeuk Kim 
1570e479556bSGu Zheng static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
157139a53e0cSJaegeuk Kim {
1572b873b798SJaegeuk Kim 	up_read(&sbi->cp_rwsem);
157339936837SJaegeuk Kim }
157439936837SJaegeuk Kim 
1575e479556bSGu Zheng static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
157639936837SJaegeuk Kim {
1577b873b798SJaegeuk Kim 	down_write(&sbi->cp_rwsem);
157839936837SJaegeuk Kim }
157939936837SJaegeuk Kim 
1580e479556bSGu Zheng static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
158139936837SJaegeuk Kim {
1582b873b798SJaegeuk Kim 	up_write(&sbi->cp_rwsem);
158339a53e0cSJaegeuk Kim }
158439a53e0cSJaegeuk Kim 
1585119ee914SJaegeuk Kim static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
1586119ee914SJaegeuk Kim {
1587119ee914SJaegeuk Kim 	int reason = CP_SYNC;
1588119ee914SJaegeuk Kim 
1589119ee914SJaegeuk Kim 	if (test_opt(sbi, FASTBOOT))
1590119ee914SJaegeuk Kim 		reason = CP_FASTBOOT;
1591119ee914SJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
1592119ee914SJaegeuk Kim 		reason = CP_UMOUNT;
1593119ee914SJaegeuk Kim 	return reason;
1594119ee914SJaegeuk Kim }
1595119ee914SJaegeuk Kim 
1596119ee914SJaegeuk Kim static inline bool __remain_node_summaries(int reason)
1597119ee914SJaegeuk Kim {
1598c473f1a9SChao Yu 	return (reason & (CP_UMOUNT | CP_FASTBOOT));
1599119ee914SJaegeuk Kim }
1600119ee914SJaegeuk Kim 
1601119ee914SJaegeuk Kim static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
1602119ee914SJaegeuk Kim {
1603aaec2b1dSChao Yu 	return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
1604aaec2b1dSChao Yu 			is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
1605119ee914SJaegeuk Kim }
1606119ee914SJaegeuk Kim 
160739a53e0cSJaegeuk Kim /*
160839a53e0cSJaegeuk Kim  * Check whether the inode has blocks or not
160939a53e0cSJaegeuk Kim  */
161039a53e0cSJaegeuk Kim static inline int F2FS_HAS_BLOCKS(struct inode *inode)
161139a53e0cSJaegeuk Kim {
16120eb0adadSChao Yu 	block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
16130eb0adadSChao Yu 
1614000519f2SChao Yu 	return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block;
161539a53e0cSJaegeuk Kim }
161639a53e0cSJaegeuk Kim 
16174bc8e9bcSChao Yu static inline bool f2fs_has_xattr_block(unsigned int ofs)
16184bc8e9bcSChao Yu {
16194bc8e9bcSChao Yu 	return ofs == XATTR_NODE_OFFSET;
16204bc8e9bcSChao Yu }
16214bc8e9bcSChao Yu 
1622d8a9a229SJaegeuk Kim static inline bool __allow_reserved_blocks(struct f2fs_sb_info *sbi,
1623a90a0884SJaegeuk Kim 					struct inode *inode, bool cap)
16247c2e5963SJaegeuk Kim {
1625d8a9a229SJaegeuk Kim 	if (!inode)
1626d8a9a229SJaegeuk Kim 		return true;
16277c2e5963SJaegeuk Kim 	if (!test_opt(sbi, RESERVE_ROOT))
16287c2e5963SJaegeuk Kim 		return false;
1629d8a9a229SJaegeuk Kim 	if (IS_NOQUOTA(inode))
1630d8a9a229SJaegeuk Kim 		return true;
163163189b78SChao Yu 	if (uid_eq(F2FS_OPTION(sbi).s_resuid, current_fsuid()))
16327c2e5963SJaegeuk Kim 		return true;
163363189b78SChao Yu 	if (!gid_eq(F2FS_OPTION(sbi).s_resgid, GLOBAL_ROOT_GID) &&
163463189b78SChao Yu 					in_group_p(F2FS_OPTION(sbi).s_resgid))
16357c2e5963SJaegeuk Kim 		return true;
1636a90a0884SJaegeuk Kim 	if (cap && capable(CAP_SYS_RESOURCE))
1637162b27aeSJaegeuk Kim 		return true;
16387c2e5963SJaegeuk Kim 	return false;
16397c2e5963SJaegeuk Kim }
16407c2e5963SJaegeuk Kim 
16410abd675eSChao Yu static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool);
16420abd675eSChao Yu static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
164346008c6dSChao Yu 				 struct inode *inode, blkcnt_t *count)
164439a53e0cSJaegeuk Kim {
16450abd675eSChao Yu 	blkcnt_t diff = 0, release = 0;
1646daeb433eSChao Yu 	block_t avail_user_block_count;
16470abd675eSChao Yu 	int ret;
16480abd675eSChao Yu 
16490abd675eSChao Yu 	ret = dquot_reserve_block(inode, *count);
16500abd675eSChao Yu 	if (ret)
16510abd675eSChao Yu 		return ret;
1652dd11a5dfSJaegeuk Kim 
1653cb78942bSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
165455523519SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
165555523519SChao Yu 		f2fs_show_injection_info(FAULT_BLOCK);
16560abd675eSChao Yu 		release = *count;
16570abd675eSChao Yu 		goto enospc;
165855523519SChao Yu 	}
1659cb78942bSJaegeuk Kim #endif
1660dd11a5dfSJaegeuk Kim 	/*
1661dd11a5dfSJaegeuk Kim 	 * let's increase this in prior to actual block count change in order
1662dd11a5dfSJaegeuk Kim 	 * for f2fs_sync_file to avoid data races when deciding checkpoint.
1663dd11a5dfSJaegeuk Kim 	 */
1664dd11a5dfSJaegeuk Kim 	percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
1665cfb271d4SChao Yu 
166639a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
16672555a2d5SJaegeuk Kim 	sbi->total_valid_block_count += (block_t)(*count);
166880d42145SYunlong Song 	avail_user_block_count = sbi->user_block_count -
166980d42145SYunlong Song 					sbi->current_reserved_blocks;
16707e65be49SJaegeuk Kim 
1671a90a0884SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode, true))
167263189b78SChao Yu 		avail_user_block_count -= F2FS_OPTION(sbi).root_reserved_blocks;
16737e65be49SJaegeuk Kim 
1674daeb433eSChao Yu 	if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
1675daeb433eSChao Yu 		diff = sbi->total_valid_block_count - avail_user_block_count;
16767e65be49SJaegeuk Kim 		if (diff > *count)
16777e65be49SJaegeuk Kim 			diff = *count;
1678dd11a5dfSJaegeuk Kim 		*count -= diff;
16790abd675eSChao Yu 		release = diff;
16807e65be49SJaegeuk Kim 		sbi->total_valid_block_count -= diff;
168146008c6dSChao Yu 		if (!*count) {
168239a53e0cSJaegeuk Kim 			spin_unlock(&sbi->stat_lock);
1683dd11a5dfSJaegeuk Kim 			percpu_counter_sub(&sbi->alloc_valid_block_count, diff);
16840abd675eSChao Yu 			goto enospc;
168539a53e0cSJaegeuk Kim 		}
168646008c6dSChao Yu 	}
168739a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
168841382ec4SJaegeuk Kim 
1689fab2adeeSLiFan 	if (unlikely(release))
16900abd675eSChao Yu 		dquot_release_reservation_block(inode, release);
16910abd675eSChao Yu 	f2fs_i_blocks_write(inode, *count, true, true);
16920abd675eSChao Yu 	return 0;
16930abd675eSChao Yu 
16940abd675eSChao Yu enospc:
16950abd675eSChao Yu 	dquot_release_reservation_block(inode, release);
16960abd675eSChao Yu 	return -ENOSPC;
169739a53e0cSJaegeuk Kim }
169839a53e0cSJaegeuk Kim 
1699da19b0dcSGu Zheng static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
170039a53e0cSJaegeuk Kim 						struct inode *inode,
17010eb0adadSChao Yu 						block_t count)
170239a53e0cSJaegeuk Kim {
17030eb0adadSChao Yu 	blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK;
17040eb0adadSChao Yu 
170539a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
17069850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
17070eb0adadSChao Yu 	f2fs_bug_on(sbi, inode->i_blocks < sectors);
170839a53e0cSJaegeuk Kim 	sbi->total_valid_block_count -= (block_t)count;
170980d42145SYunlong Song 	if (sbi->reserved_blocks &&
171080d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
171180d42145SYunlong Song 		sbi->current_reserved_blocks = min(sbi->reserved_blocks,
171280d42145SYunlong Song 					sbi->current_reserved_blocks + count);
171339a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
17140abd675eSChao Yu 	f2fs_i_blocks_write(inode, count, false, true);
171539a53e0cSJaegeuk Kim }
171639a53e0cSJaegeuk Kim 
171739a53e0cSJaegeuk Kim static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
171839a53e0cSJaegeuk Kim {
171935782b23SJaegeuk Kim 	atomic_inc(&sbi->nr_pages[count_type]);
17207c4abcbeSChao Yu 
172136951b38SChao Yu 	if (count_type == F2FS_DIRTY_DATA || count_type == F2FS_INMEM_PAGES ||
172236951b38SChao Yu 		count_type == F2FS_WB_CP_DATA || count_type == F2FS_WB_DATA)
17237c4abcbeSChao Yu 		return;
17247c4abcbeSChao Yu 
1725caf0047eSChao Yu 	set_sbi_flag(sbi, SBI_IS_DIRTY);
172639a53e0cSJaegeuk Kim }
172739a53e0cSJaegeuk Kim 
1728a7ffdbe2SJaegeuk Kim static inline void inode_inc_dirty_pages(struct inode *inode)
172939a53e0cSJaegeuk Kim {
1730204706c7SJaegeuk Kim 	atomic_inc(&F2FS_I(inode)->dirty_pages);
1731c227f912SChao Yu 	inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1732c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
17332c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
17342c8a4a28SJaegeuk Kim 		inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
173539a53e0cSJaegeuk Kim }
173639a53e0cSJaegeuk Kim 
173739a53e0cSJaegeuk Kim static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
173839a53e0cSJaegeuk Kim {
173935782b23SJaegeuk Kim 	atomic_dec(&sbi->nr_pages[count_type]);
174039a53e0cSJaegeuk Kim }
174139a53e0cSJaegeuk Kim 
1742a7ffdbe2SJaegeuk Kim static inline void inode_dec_dirty_pages(struct inode *inode)
174339a53e0cSJaegeuk Kim {
17445ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
17455ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
17461fe54f9dSJaegeuk Kim 		return;
17471fe54f9dSJaegeuk Kim 
1748204706c7SJaegeuk Kim 	atomic_dec(&F2FS_I(inode)->dirty_pages);
1749c227f912SChao Yu 	dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1750c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
17512c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
17522c8a4a28SJaegeuk Kim 		dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
175339a53e0cSJaegeuk Kim }
175439a53e0cSJaegeuk Kim 
1755523be8a6SJaegeuk Kim static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
175639a53e0cSJaegeuk Kim {
175735782b23SJaegeuk Kim 	return atomic_read(&sbi->nr_pages[count_type]);
175839a53e0cSJaegeuk Kim }
175939a53e0cSJaegeuk Kim 
1760204706c7SJaegeuk Kim static inline int get_dirty_pages(struct inode *inode)
1761f8b2c1f9SJaegeuk Kim {
1762204706c7SJaegeuk Kim 	return atomic_read(&F2FS_I(inode)->dirty_pages);
1763f8b2c1f9SJaegeuk Kim }
1764f8b2c1f9SJaegeuk Kim 
17655ac206cfSNamjae Jeon static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
17665ac206cfSNamjae Jeon {
17673519e3f9SChao Yu 	unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
1768523be8a6SJaegeuk Kim 	unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
1769523be8a6SJaegeuk Kim 						sbi->log_blocks_per_seg;
1770523be8a6SJaegeuk Kim 
1771523be8a6SJaegeuk Kim 	return segs / sbi->segs_per_sec;
17725ac206cfSNamjae Jeon }
17735ac206cfSNamjae Jeon 
177439a53e0cSJaegeuk Kim static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
177539a53e0cSJaegeuk Kim {
17768b8343faSJaegeuk Kim 	return sbi->total_valid_block_count;
177739a53e0cSJaegeuk Kim }
177839a53e0cSJaegeuk Kim 
1779f83a2584SYunlei He static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
1780f83a2584SYunlei He {
1781f83a2584SYunlei He 	return sbi->discard_blks;
1782f83a2584SYunlei He }
1783f83a2584SYunlei He 
178439a53e0cSJaegeuk Kim static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
178539a53e0cSJaegeuk Kim {
178639a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
178739a53e0cSJaegeuk Kim 
178839a53e0cSJaegeuk Kim 	/* return NAT or SIT bitmap */
178939a53e0cSJaegeuk Kim 	if (flag == NAT_BITMAP)
179039a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
179139a53e0cSJaegeuk Kim 	else if (flag == SIT_BITMAP)
179239a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
179339a53e0cSJaegeuk Kim 
179439a53e0cSJaegeuk Kim 	return 0;
179539a53e0cSJaegeuk Kim }
179639a53e0cSJaegeuk Kim 
179755141486SWanpeng Li static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
179855141486SWanpeng Li {
179955141486SWanpeng Li 	return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
180055141486SWanpeng Li }
180155141486SWanpeng Li 
180239a53e0cSJaegeuk Kim static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
180339a53e0cSJaegeuk Kim {
180439a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
18051dbe4152SChangman Lee 	int offset;
18061dbe4152SChangman Lee 
1807199bc3feSChao Yu 	if (is_set_ckpt_flags(sbi, CP_LARGE_NAT_BITMAP_FLAG)) {
1808199bc3feSChao Yu 		offset = (flag == SIT_BITMAP) ?
1809199bc3feSChao Yu 			le32_to_cpu(ckpt->nat_ver_bitmap_bytesize) : 0;
1810199bc3feSChao Yu 		return &ckpt->sit_nat_version_bitmap + offset;
1811199bc3feSChao Yu 	}
1812199bc3feSChao Yu 
181355141486SWanpeng Li 	if (__cp_payload(sbi) > 0) {
18141dbe4152SChangman Lee 		if (flag == NAT_BITMAP)
18151dbe4152SChangman Lee 			return &ckpt->sit_nat_version_bitmap;
18161dbe4152SChangman Lee 		else
181765b85cccSJaegeuk Kim 			return (unsigned char *)ckpt + F2FS_BLKSIZE;
18181dbe4152SChangman Lee 	} else {
18191dbe4152SChangman Lee 		offset = (flag == NAT_BITMAP) ?
182025ca923bSJaegeuk Kim 			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
182139a53e0cSJaegeuk Kim 		return &ckpt->sit_nat_version_bitmap + offset;
182239a53e0cSJaegeuk Kim 	}
18231dbe4152SChangman Lee }
182439a53e0cSJaegeuk Kim 
182539a53e0cSJaegeuk Kim static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
182639a53e0cSJaegeuk Kim {
18278508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
182839a53e0cSJaegeuk Kim 
18298508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 2)
183039a53e0cSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
183139a53e0cSJaegeuk Kim 	return start_addr;
183239a53e0cSJaegeuk Kim }
183339a53e0cSJaegeuk Kim 
18348508e44aSJaegeuk Kim static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
18358508e44aSJaegeuk Kim {
18368508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
18378508e44aSJaegeuk Kim 
18388508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 1)
18398508e44aSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
18408508e44aSJaegeuk Kim 	return start_addr;
18418508e44aSJaegeuk Kim }
18428508e44aSJaegeuk Kim 
18438508e44aSJaegeuk Kim static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
18448508e44aSJaegeuk Kim {
18458508e44aSJaegeuk Kim 	sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
18468508e44aSJaegeuk Kim }
18478508e44aSJaegeuk Kim 
184839a53e0cSJaegeuk Kim static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
184939a53e0cSJaegeuk Kim {
185039a53e0cSJaegeuk Kim 	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
185139a53e0cSJaegeuk Kim }
185239a53e0cSJaegeuk Kim 
18530abd675eSChao Yu static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
1854000519f2SChao Yu 					struct inode *inode, bool is_inode)
185539a53e0cSJaegeuk Kim {
185639a53e0cSJaegeuk Kim 	block_t	valid_block_count;
185739a53e0cSJaegeuk Kim 	unsigned int valid_node_count;
18580abd675eSChao Yu 	bool quota = inode && !is_inode;
18590abd675eSChao Yu 
18600abd675eSChao Yu 	if (quota) {
18610abd675eSChao Yu 		int ret = dquot_reserve_block(inode, 1);
18620abd675eSChao Yu 		if (ret)
18630abd675eSChao Yu 			return ret;
18640abd675eSChao Yu 	}
186539a53e0cSJaegeuk Kim 
1866812c6056SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
1867812c6056SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
1868812c6056SChao Yu 		f2fs_show_injection_info(FAULT_BLOCK);
1869812c6056SChao Yu 		goto enospc;
1870812c6056SChao Yu 	}
1871812c6056SChao Yu #endif
1872812c6056SChao Yu 
187339a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
187439a53e0cSJaegeuk Kim 
18757e65be49SJaegeuk Kim 	valid_block_count = sbi->total_valid_block_count +
18767e65be49SJaegeuk Kim 					sbi->current_reserved_blocks + 1;
18777e65be49SJaegeuk Kim 
1878a90a0884SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode, false))
187963189b78SChao Yu 		valid_block_count += F2FS_OPTION(sbi).root_reserved_blocks;
18807e65be49SJaegeuk Kim 
18817e65be49SJaegeuk Kim 	if (unlikely(valid_block_count > sbi->user_block_count)) {
188239a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
18830abd675eSChao Yu 		goto enospc;
188439a53e0cSJaegeuk Kim 	}
188539a53e0cSJaegeuk Kim 
1886ef86d709SGu Zheng 	valid_node_count = sbi->total_valid_node_count + 1;
1887cfb271d4SChao Yu 	if (unlikely(valid_node_count > sbi->total_node_count)) {
188839a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
18890abd675eSChao Yu 		goto enospc;
189039a53e0cSJaegeuk Kim 	}
189139a53e0cSJaegeuk Kim 
1892ef86d709SGu Zheng 	sbi->total_valid_node_count++;
1893ef86d709SGu Zheng 	sbi->total_valid_block_count++;
189439a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
189539a53e0cSJaegeuk Kim 
18960abd675eSChao Yu 	if (inode) {
18970abd675eSChao Yu 		if (is_inode)
18980abd675eSChao Yu 			f2fs_mark_inode_dirty_sync(inode, true);
18990abd675eSChao Yu 		else
19000abd675eSChao Yu 			f2fs_i_blocks_write(inode, 1, true, true);
19010abd675eSChao Yu 	}
19020abd675eSChao Yu 
190341382ec4SJaegeuk Kim 	percpu_counter_inc(&sbi->alloc_valid_block_count);
19040abd675eSChao Yu 	return 0;
19050abd675eSChao Yu 
19060abd675eSChao Yu enospc:
19070abd675eSChao Yu 	if (quota)
19080abd675eSChao Yu 		dquot_release_reservation_block(inode, 1);
19090abd675eSChao Yu 	return -ENOSPC;
191039a53e0cSJaegeuk Kim }
191139a53e0cSJaegeuk Kim 
191239a53e0cSJaegeuk Kim static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
1913000519f2SChao Yu 					struct inode *inode, bool is_inode)
191439a53e0cSJaegeuk Kim {
191539a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
191639a53e0cSJaegeuk Kim 
19179850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_block_count);
19189850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_node_count);
1919000519f2SChao Yu 	f2fs_bug_on(sbi, !is_inode && !inode->i_blocks);
192039a53e0cSJaegeuk Kim 
1921ef86d709SGu Zheng 	sbi->total_valid_node_count--;
1922ef86d709SGu Zheng 	sbi->total_valid_block_count--;
192380d42145SYunlong Song 	if (sbi->reserved_blocks &&
192480d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
192580d42145SYunlong Song 		sbi->current_reserved_blocks++;
192639a53e0cSJaegeuk Kim 
192739a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
19280abd675eSChao Yu 
19290abd675eSChao Yu 	if (!is_inode)
19300abd675eSChao Yu 		f2fs_i_blocks_write(inode, 1, false, true);
193139a53e0cSJaegeuk Kim }
193239a53e0cSJaegeuk Kim 
193339a53e0cSJaegeuk Kim static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
193439a53e0cSJaegeuk Kim {
19358b8343faSJaegeuk Kim 	return sbi->total_valid_node_count;
193639a53e0cSJaegeuk Kim }
193739a53e0cSJaegeuk Kim 
193839a53e0cSJaegeuk Kim static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
193939a53e0cSJaegeuk Kim {
1940513c5f37SJaegeuk Kim 	percpu_counter_inc(&sbi->total_valid_inode_count);
194139a53e0cSJaegeuk Kim }
194239a53e0cSJaegeuk Kim 
19430e80220aSJaegeuk Kim static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
194439a53e0cSJaegeuk Kim {
1945513c5f37SJaegeuk Kim 	percpu_counter_dec(&sbi->total_valid_inode_count);
194639a53e0cSJaegeuk Kim }
194739a53e0cSJaegeuk Kim 
1948513c5f37SJaegeuk Kim static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
194939a53e0cSJaegeuk Kim {
1950513c5f37SJaegeuk Kim 	return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
195139a53e0cSJaegeuk Kim }
195239a53e0cSJaegeuk Kim 
1953a56c7c6fSJaegeuk Kim static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
1954a56c7c6fSJaegeuk Kim 						pgoff_t index, bool for_write)
1955a56c7c6fSJaegeuk Kim {
1956c41f3cc3SJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
1957c41f3cc3SJaegeuk Kim 	struct page *page = find_lock_page(mapping, index);
1958cac5a3d8SDongOh Shin 
1959c41f3cc3SJaegeuk Kim 	if (page)
1960c41f3cc3SJaegeuk Kim 		return page;
1961c41f3cc3SJaegeuk Kim 
196255523519SChao Yu 	if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) {
196355523519SChao Yu 		f2fs_show_injection_info(FAULT_PAGE_ALLOC);
1964c41f3cc3SJaegeuk Kim 		return NULL;
196555523519SChao Yu 	}
1966c41f3cc3SJaegeuk Kim #endif
1967a56c7c6fSJaegeuk Kim 	if (!for_write)
1968a56c7c6fSJaegeuk Kim 		return grab_cache_page(mapping, index);
1969a56c7c6fSJaegeuk Kim 	return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
1970a56c7c6fSJaegeuk Kim }
1971a56c7c6fSJaegeuk Kim 
197201eccef7SChao Yu static inline struct page *f2fs_pagecache_get_page(
197301eccef7SChao Yu 				struct address_space *mapping, pgoff_t index,
197401eccef7SChao Yu 				int fgp_flags, gfp_t gfp_mask)
197501eccef7SChao Yu {
197601eccef7SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
197701eccef7SChao Yu 	if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_GET)) {
197801eccef7SChao Yu 		f2fs_show_injection_info(FAULT_PAGE_GET);
197901eccef7SChao Yu 		return NULL;
198001eccef7SChao Yu 	}
198101eccef7SChao Yu #endif
198201eccef7SChao Yu 	return pagecache_get_page(mapping, index, fgp_flags, gfp_mask);
198301eccef7SChao Yu }
198401eccef7SChao Yu 
19856e2c64adSJaegeuk Kim static inline void f2fs_copy_page(struct page *src, struct page *dst)
19866e2c64adSJaegeuk Kim {
19876e2c64adSJaegeuk Kim 	char *src_kaddr = kmap(src);
19886e2c64adSJaegeuk Kim 	char *dst_kaddr = kmap(dst);
19896e2c64adSJaegeuk Kim 
19906e2c64adSJaegeuk Kim 	memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
19916e2c64adSJaegeuk Kim 	kunmap(dst);
19926e2c64adSJaegeuk Kim 	kunmap(src);
19936e2c64adSJaegeuk Kim }
19946e2c64adSJaegeuk Kim 
199539a53e0cSJaegeuk Kim static inline void f2fs_put_page(struct page *page, int unlock)
199639a53e0cSJaegeuk Kim {
1997031fa8ccSJaegeuk Kim 	if (!page)
199839a53e0cSJaegeuk Kim 		return;
199939a53e0cSJaegeuk Kim 
200039a53e0cSJaegeuk Kim 	if (unlock) {
20019850cf4aSJaegeuk Kim 		f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
200239a53e0cSJaegeuk Kim 		unlock_page(page);
200339a53e0cSJaegeuk Kim 	}
200409cbfeafSKirill A. Shutemov 	put_page(page);
200539a53e0cSJaegeuk Kim }
200639a53e0cSJaegeuk Kim 
200739a53e0cSJaegeuk Kim static inline void f2fs_put_dnode(struct dnode_of_data *dn)
200839a53e0cSJaegeuk Kim {
200939a53e0cSJaegeuk Kim 	if (dn->node_page)
201039a53e0cSJaegeuk Kim 		f2fs_put_page(dn->node_page, 1);
201139a53e0cSJaegeuk Kim 	if (dn->inode_page && dn->node_page != dn->inode_page)
201239a53e0cSJaegeuk Kim 		f2fs_put_page(dn->inode_page, 0);
201339a53e0cSJaegeuk Kim 	dn->node_page = NULL;
201439a53e0cSJaegeuk Kim 	dn->inode_page = NULL;
201539a53e0cSJaegeuk Kim }
201639a53e0cSJaegeuk Kim 
201739a53e0cSJaegeuk Kim static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
2018e8512d2eSGu Zheng 					size_t size)
201939a53e0cSJaegeuk Kim {
2020e8512d2eSGu Zheng 	return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
202139a53e0cSJaegeuk Kim }
202239a53e0cSJaegeuk Kim 
20237bd59381SGu Zheng static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
20247bd59381SGu Zheng 						gfp_t flags)
20257bd59381SGu Zheng {
20267bd59381SGu Zheng 	void *entry;
20277bd59381SGu Zheng 
202880c54505SJaegeuk Kim 	entry = kmem_cache_alloc(cachep, flags);
202980c54505SJaegeuk Kim 	if (!entry)
203080c54505SJaegeuk Kim 		entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
20317bd59381SGu Zheng 	return entry;
20327bd59381SGu Zheng }
20337bd59381SGu Zheng 
2034d62fe971SChao Yu static inline struct bio *f2fs_bio_alloc(struct f2fs_sb_info *sbi,
2035d62fe971SChao Yu 						int npages, bool no_fail)
2036740432f8SJaegeuk Kim {
2037740432f8SJaegeuk Kim 	struct bio *bio;
2038740432f8SJaegeuk Kim 
2039d62fe971SChao Yu 	if (no_fail) {
2040740432f8SJaegeuk Kim 		/* No failure on bio allocation */
2041740432f8SJaegeuk Kim 		bio = bio_alloc(GFP_NOIO, npages);
204280c54505SJaegeuk Kim 		if (!bio)
204380c54505SJaegeuk Kim 			bio = bio_alloc(GFP_NOIO | __GFP_NOFAIL, npages);
2044740432f8SJaegeuk Kim 		return bio;
2045740432f8SJaegeuk Kim 	}
2046d62fe971SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
2047d62fe971SChao Yu 	if (time_to_inject(sbi, FAULT_ALLOC_BIO)) {
2048d62fe971SChao Yu 		f2fs_show_injection_info(FAULT_ALLOC_BIO);
2049d62fe971SChao Yu 		return NULL;
2050d62fe971SChao Yu 	}
2051d62fe971SChao Yu #endif
2052d62fe971SChao Yu 	return bio_alloc(GFP_KERNEL, npages);
2053d62fe971SChao Yu }
2054740432f8SJaegeuk Kim 
20559be32d72SJaegeuk Kim static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
20569be32d72SJaegeuk Kim 				unsigned long index, void *item)
20579be32d72SJaegeuk Kim {
20589be32d72SJaegeuk Kim 	while (radix_tree_insert(root, index, item))
20599be32d72SJaegeuk Kim 		cond_resched();
20609be32d72SJaegeuk Kim }
20619be32d72SJaegeuk Kim 
206239a53e0cSJaegeuk Kim #define RAW_IS_INODE(p)	((p)->footer.nid == (p)->footer.ino)
206339a53e0cSJaegeuk Kim 
206439a53e0cSJaegeuk Kim static inline bool IS_INODE(struct page *page)
206539a53e0cSJaegeuk Kim {
206645590710SGu Zheng 	struct f2fs_node *p = F2FS_NODE(page);
2067cac5a3d8SDongOh Shin 
206839a53e0cSJaegeuk Kim 	return RAW_IS_INODE(p);
206939a53e0cSJaegeuk Kim }
207039a53e0cSJaegeuk Kim 
20717a2af766SChao Yu static inline int offset_in_addr(struct f2fs_inode *i)
20727a2af766SChao Yu {
20737a2af766SChao Yu 	return (i->i_inline & F2FS_EXTRA_ATTR) ?
20747a2af766SChao Yu 			(le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0;
20757a2af766SChao Yu }
20767a2af766SChao Yu 
207739a53e0cSJaegeuk Kim static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
207839a53e0cSJaegeuk Kim {
207939a53e0cSJaegeuk Kim 	return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
208039a53e0cSJaegeuk Kim }
208139a53e0cSJaegeuk Kim 
20827a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode);
20837a2af766SChao Yu static inline block_t datablock_addr(struct inode *inode,
20847a2af766SChao Yu 			struct page *node_page, unsigned int offset)
208539a53e0cSJaegeuk Kim {
208639a53e0cSJaegeuk Kim 	struct f2fs_node *raw_node;
208739a53e0cSJaegeuk Kim 	__le32 *addr_array;
20887a2af766SChao Yu 	int base = 0;
20897a2af766SChao Yu 	bool is_inode = IS_INODE(node_page);
2090cac5a3d8SDongOh Shin 
209145590710SGu Zheng 	raw_node = F2FS_NODE(node_page);
20927a2af766SChao Yu 
20937a2af766SChao Yu 	/* from GC path only */
2094979f492fSLiFan 	if (is_inode) {
2095979f492fSLiFan 		if (!inode)
20967a2af766SChao Yu 			base = offset_in_addr(&raw_node->i);
2097979f492fSLiFan 		else if (f2fs_has_extra_attr(inode))
20987a2af766SChao Yu 			base = get_extra_isize(inode);
20997a2af766SChao Yu 	}
21007a2af766SChao Yu 
210139a53e0cSJaegeuk Kim 	addr_array = blkaddr_in_node(raw_node);
21027a2af766SChao Yu 	return le32_to_cpu(addr_array[base + offset]);
210339a53e0cSJaegeuk Kim }
210439a53e0cSJaegeuk Kim 
210539a53e0cSJaegeuk Kim static inline int f2fs_test_bit(unsigned int nr, char *addr)
210639a53e0cSJaegeuk Kim {
210739a53e0cSJaegeuk Kim 	int mask;
210839a53e0cSJaegeuk Kim 
210939a53e0cSJaegeuk Kim 	addr += (nr >> 3);
211039a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
211139a53e0cSJaegeuk Kim 	return mask & *addr;
211239a53e0cSJaegeuk Kim }
211339a53e0cSJaegeuk Kim 
2114a66cdd98SJaegeuk Kim static inline void f2fs_set_bit(unsigned int nr, char *addr)
2115a66cdd98SJaegeuk Kim {
2116a66cdd98SJaegeuk Kim 	int mask;
2117a66cdd98SJaegeuk Kim 
2118a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2119a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2120a66cdd98SJaegeuk Kim 	*addr |= mask;
2121a66cdd98SJaegeuk Kim }
2122a66cdd98SJaegeuk Kim 
2123a66cdd98SJaegeuk Kim static inline void f2fs_clear_bit(unsigned int nr, char *addr)
2124a66cdd98SJaegeuk Kim {
2125a66cdd98SJaegeuk Kim 	int mask;
2126a66cdd98SJaegeuk Kim 
2127a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2128a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2129a66cdd98SJaegeuk Kim 	*addr &= ~mask;
2130a66cdd98SJaegeuk Kim }
2131a66cdd98SJaegeuk Kim 
213252aca074SGu Zheng static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
213339a53e0cSJaegeuk Kim {
213439a53e0cSJaegeuk Kim 	int mask;
213539a53e0cSJaegeuk Kim 	int ret;
213639a53e0cSJaegeuk Kim 
213739a53e0cSJaegeuk Kim 	addr += (nr >> 3);
213839a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
213939a53e0cSJaegeuk Kim 	ret = mask & *addr;
214039a53e0cSJaegeuk Kim 	*addr |= mask;
214139a53e0cSJaegeuk Kim 	return ret;
214239a53e0cSJaegeuk Kim }
214339a53e0cSJaegeuk Kim 
214452aca074SGu Zheng static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
214539a53e0cSJaegeuk Kim {
214639a53e0cSJaegeuk Kim 	int mask;
214739a53e0cSJaegeuk Kim 	int ret;
214839a53e0cSJaegeuk Kim 
214939a53e0cSJaegeuk Kim 	addr += (nr >> 3);
215039a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
215139a53e0cSJaegeuk Kim 	ret = mask & *addr;
215239a53e0cSJaegeuk Kim 	*addr &= ~mask;
215339a53e0cSJaegeuk Kim 	return ret;
215439a53e0cSJaegeuk Kim }
215539a53e0cSJaegeuk Kim 
2156c6ac4c0eSGu Zheng static inline void f2fs_change_bit(unsigned int nr, char *addr)
2157c6ac4c0eSGu Zheng {
2158c6ac4c0eSGu Zheng 	int mask;
2159c6ac4c0eSGu Zheng 
2160c6ac4c0eSGu Zheng 	addr += (nr >> 3);
2161c6ac4c0eSGu Zheng 	mask = 1 << (7 - (nr & 0x07));
2162c6ac4c0eSGu Zheng 	*addr ^= mask;
2163c6ac4c0eSGu Zheng }
2164c6ac4c0eSGu Zheng 
216559c84408SChao Yu /*
216659c84408SChao Yu  * Inode flags
216759c84408SChao Yu  */
216859c84408SChao Yu #define F2FS_SECRM_FL			0x00000001 /* Secure deletion */
216959c84408SChao Yu #define F2FS_UNRM_FL			0x00000002 /* Undelete */
217059c84408SChao Yu #define F2FS_COMPR_FL			0x00000004 /* Compress file */
217159c84408SChao Yu #define F2FS_SYNC_FL			0x00000008 /* Synchronous updates */
217259c84408SChao Yu #define F2FS_IMMUTABLE_FL		0x00000010 /* Immutable file */
217359c84408SChao Yu #define F2FS_APPEND_FL			0x00000020 /* writes to file may only append */
217459c84408SChao Yu #define F2FS_NODUMP_FL			0x00000040 /* do not dump file */
217559c84408SChao Yu #define F2FS_NOATIME_FL			0x00000080 /* do not update atime */
217659c84408SChao Yu /* Reserved for compression usage... */
217759c84408SChao Yu #define F2FS_DIRTY_FL			0x00000100
217859c84408SChao Yu #define F2FS_COMPRBLK_FL		0x00000200 /* One or more compressed clusters */
217959c84408SChao Yu #define F2FS_NOCOMPR_FL			0x00000400 /* Don't compress */
218059c84408SChao Yu #define F2FS_ENCRYPT_FL			0x00000800 /* encrypted file */
218159c84408SChao Yu /* End compression flags --- maybe not all used */
218259c84408SChao Yu #define F2FS_INDEX_FL			0x00001000 /* hash-indexed directory */
218359c84408SChao Yu #define F2FS_IMAGIC_FL			0x00002000 /* AFS directory */
218459c84408SChao Yu #define F2FS_JOURNAL_DATA_FL		0x00004000 /* file data should be journaled */
218559c84408SChao Yu #define F2FS_NOTAIL_FL			0x00008000 /* file tail should not be merged */
218659c84408SChao Yu #define F2FS_DIRSYNC_FL			0x00010000 /* dirsync behaviour (directories only) */
218759c84408SChao Yu #define F2FS_TOPDIR_FL			0x00020000 /* Top of directory hierarchies*/
218859c84408SChao Yu #define F2FS_HUGE_FILE_FL               0x00040000 /* Set to each huge file */
218959c84408SChao Yu #define F2FS_EXTENTS_FL			0x00080000 /* Inode uses extents */
219059c84408SChao Yu #define F2FS_EA_INODE_FL	        0x00200000 /* Inode used for large EA */
219159c84408SChao Yu #define F2FS_EOFBLOCKS_FL		0x00400000 /* Blocks allocated beyond EOF */
219259c84408SChao Yu #define F2FS_INLINE_DATA_FL		0x10000000 /* Inode has inline data. */
219359c84408SChao Yu #define F2FS_PROJINHERIT_FL		0x20000000 /* Create with parents projid */
219459c84408SChao Yu #define F2FS_RESERVED_FL		0x80000000 /* reserved for ext4 lib */
219559c84408SChao Yu 
219659c84408SChao Yu #define F2FS_FL_USER_VISIBLE		0x304BDFFF /* User visible flags */
219759c84408SChao Yu #define F2FS_FL_USER_MODIFIABLE		0x204BC0FF /* User modifiable flags */
219859c84408SChao Yu 
219959c84408SChao Yu /* Flags we can manipulate with through F2FS_IOC_FSSETXATTR */
220059c84408SChao Yu #define F2FS_FL_XFLAG_VISIBLE		(F2FS_SYNC_FL | \
220159c84408SChao Yu 					 F2FS_IMMUTABLE_FL | \
220259c84408SChao Yu 					 F2FS_APPEND_FL | \
220359c84408SChao Yu 					 F2FS_NODUMP_FL | \
220459c84408SChao Yu 					 F2FS_NOATIME_FL | \
220559c84408SChao Yu 					 F2FS_PROJINHERIT_FL)
220659c84408SChao Yu 
220759c84408SChao Yu /* Flags that should be inherited by new inodes from their parent. */
220859c84408SChao Yu #define F2FS_FL_INHERITED (F2FS_SECRM_FL | F2FS_UNRM_FL | F2FS_COMPR_FL |\
220959c84408SChao Yu 			   F2FS_SYNC_FL | F2FS_NODUMP_FL | F2FS_NOATIME_FL |\
221059c84408SChao Yu 			   F2FS_NOCOMPR_FL | F2FS_JOURNAL_DATA_FL |\
221159c84408SChao Yu 			   F2FS_NOTAIL_FL | F2FS_DIRSYNC_FL |\
221259c84408SChao Yu 			   F2FS_PROJINHERIT_FL)
221359c84408SChao Yu 
221459c84408SChao Yu /* Flags that are appropriate for regular files (all but dir-specific ones). */
221559c84408SChao Yu #define F2FS_REG_FLMASK		(~(F2FS_DIRSYNC_FL | F2FS_TOPDIR_FL))
221659c84408SChao Yu 
221759c84408SChao Yu /* Flags that are appropriate for non-directories/regular files. */
221859c84408SChao Yu #define F2FS_OTHER_FLMASK	(F2FS_NODUMP_FL | F2FS_NOATIME_FL)
22195c57132eSChao Yu 
22205c57132eSChao Yu static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
22215c57132eSChao Yu {
22225c57132eSChao Yu 	if (S_ISDIR(mode))
22235c57132eSChao Yu 		return flags;
22245c57132eSChao Yu 	else if (S_ISREG(mode))
22255c57132eSChao Yu 		return flags & F2FS_REG_FLMASK;
22265c57132eSChao Yu 	else
22275c57132eSChao Yu 		return flags & F2FS_OTHER_FLMASK;
22285c57132eSChao Yu }
22295c57132eSChao Yu 
223039a53e0cSJaegeuk Kim /* used for f2fs_inode_info->flags */
223139a53e0cSJaegeuk Kim enum {
223239a53e0cSJaegeuk Kim 	FI_NEW_INODE,		/* indicate newly allocated inode */
2233b3783873SJaegeuk Kim 	FI_DIRTY_INODE,		/* indicate inode is dirty or not */
223426de9b11SJaegeuk Kim 	FI_AUTO_RECOVER,	/* indicate inode is recoverable */
2235ed57c27fSJaegeuk Kim 	FI_DIRTY_DIR,		/* indicate directory has dirty pages */
223639a53e0cSJaegeuk Kim 	FI_INC_LINK,		/* need to increment i_nlink */
223739a53e0cSJaegeuk Kim 	FI_ACL_MODE,		/* indicate acl mode */
223839a53e0cSJaegeuk Kim 	FI_NO_ALLOC,		/* should not allocate any blocks */
2239c9b63bd0SJaegeuk Kim 	FI_FREE_NID,		/* free allocated nide */
2240c11abd1aSJaegeuk Kim 	FI_NO_EXTENT,		/* not to use the extent cache */
2241444c580fSJaegeuk Kim 	FI_INLINE_XATTR,	/* used for inline xattr */
22421001b347SHuajun Li 	FI_INLINE_DATA,		/* used for inline data*/
224334d67debSChao Yu 	FI_INLINE_DENTRY,	/* used for inline dentry */
2244fff04f90SJaegeuk Kim 	FI_APPEND_WRITE,	/* inode has appended data */
2245fff04f90SJaegeuk Kim 	FI_UPDATE_WRITE,	/* inode has in-place-update data */
224688b88a66SJaegeuk Kim 	FI_NEED_IPU,		/* used for ipu per file */
224788b88a66SJaegeuk Kim 	FI_ATOMIC_FILE,		/* indicate atomic file */
22485fe45743SChao Yu 	FI_ATOMIC_COMMIT,	/* indicate the state of atomical committing */
224902a1335fSJaegeuk Kim 	FI_VOLATILE_FILE,	/* indicate volatile file */
22503c6c2bebSJaegeuk Kim 	FI_FIRST_BLOCK_WRITTEN,	/* indicate #0 data block was written */
22511e84371fSJaegeuk Kim 	FI_DROP_CACHE,		/* drop dirty page cache */
2252b3d208f9SJaegeuk Kim 	FI_DATA_EXIST,		/* indicate data exists */
2253510022a8SJaegeuk Kim 	FI_INLINE_DOTS,		/* indicate inline dot dentries */
2254d323d005SChao Yu 	FI_DO_DEFRAG,		/* indicate defragment is running */
2255c227f912SChao Yu 	FI_DIRTY_FILE,		/* indicate regular/symlink has dirty pages */
2256dc91de78SJaegeuk Kim 	FI_NO_PREALLOC,		/* indicate skipped preallocated blocks */
2257ef095d19SJaegeuk Kim 	FI_HOT_DATA,		/* indicate file is hot */
22587a2af766SChao Yu 	FI_EXTRA_ATTR,		/* indicate file has extra attribute */
22595c57132eSChao Yu 	FI_PROJ_INHERIT,	/* indicate file inherits projectid */
22601ad71a27SJaegeuk Kim 	FI_PIN_FILE,		/* indicate file should not be gced */
22612ef79ecbSChao Yu 	FI_ATOMIC_REVOKE_REQUEST, /* request to drop atomic data */
226239a53e0cSJaegeuk Kim };
226339a53e0cSJaegeuk Kim 
2264205b9822SJaegeuk Kim static inline void __mark_inode_dirty_flag(struct inode *inode,
2265205b9822SJaegeuk Kim 						int flag, bool set)
226639a53e0cSJaegeuk Kim {
2267205b9822SJaegeuk Kim 	switch (flag) {
2268205b9822SJaegeuk Kim 	case FI_INLINE_XATTR:
2269205b9822SJaegeuk Kim 	case FI_INLINE_DATA:
2270205b9822SJaegeuk Kim 	case FI_INLINE_DENTRY:
22719ac1e2d8SDaeho Jeong 	case FI_NEW_INODE:
2272205b9822SJaegeuk Kim 		if (set)
2273205b9822SJaegeuk Kim 			return;
2274205b9822SJaegeuk Kim 	case FI_DATA_EXIST:
2275205b9822SJaegeuk Kim 	case FI_INLINE_DOTS:
22761ad71a27SJaegeuk Kim 	case FI_PIN_FILE:
22777c45729aSJaegeuk Kim 		f2fs_mark_inode_dirty_sync(inode, true);
2278205b9822SJaegeuk Kim 	}
227939a53e0cSJaegeuk Kim }
228039a53e0cSJaegeuk Kim 
228191942321SJaegeuk Kim static inline void set_inode_flag(struct inode *inode, int flag)
228239a53e0cSJaegeuk Kim {
228391942321SJaegeuk Kim 	if (!test_bit(flag, &F2FS_I(inode)->flags))
228491942321SJaegeuk Kim 		set_bit(flag, &F2FS_I(inode)->flags);
2285205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, true);
228639a53e0cSJaegeuk Kim }
228739a53e0cSJaegeuk Kim 
228891942321SJaegeuk Kim static inline int is_inode_flag_set(struct inode *inode, int flag)
228939a53e0cSJaegeuk Kim {
229091942321SJaegeuk Kim 	return test_bit(flag, &F2FS_I(inode)->flags);
229139a53e0cSJaegeuk Kim }
229239a53e0cSJaegeuk Kim 
229391942321SJaegeuk Kim static inline void clear_inode_flag(struct inode *inode, int flag)
229439a53e0cSJaegeuk Kim {
229591942321SJaegeuk Kim 	if (test_bit(flag, &F2FS_I(inode)->flags))
229691942321SJaegeuk Kim 		clear_bit(flag, &F2FS_I(inode)->flags);
2297205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, false);
229839a53e0cSJaegeuk Kim }
229939a53e0cSJaegeuk Kim 
230091942321SJaegeuk Kim static inline void set_acl_inode(struct inode *inode, umode_t mode)
2301444c580fSJaegeuk Kim {
230291942321SJaegeuk Kim 	F2FS_I(inode)->i_acl_mode = mode;
230391942321SJaegeuk Kim 	set_inode_flag(inode, FI_ACL_MODE);
23047c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, false);
230539a53e0cSJaegeuk Kim }
230639a53e0cSJaegeuk Kim 
2307a1961246SJaegeuk Kim static inline void f2fs_i_links_write(struct inode *inode, bool inc)
2308a1961246SJaegeuk Kim {
2309a1961246SJaegeuk Kim 	if (inc)
2310a1961246SJaegeuk Kim 		inc_nlink(inode);
2311a1961246SJaegeuk Kim 	else
2312a1961246SJaegeuk Kim 		drop_nlink(inode);
23137c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2314a1961246SJaegeuk Kim }
2315a1961246SJaegeuk Kim 
23168edd03c8SJaegeuk Kim static inline void f2fs_i_blocks_write(struct inode *inode,
23170abd675eSChao Yu 					block_t diff, bool add, bool claim)
23188edd03c8SJaegeuk Kim {
231926de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
232026de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
232126de9b11SJaegeuk Kim 
23220abd675eSChao Yu 	/* add = 1, claim = 1 should be dquot_reserve_block in pair */
23230abd675eSChao Yu 	if (add) {
23240abd675eSChao Yu 		if (claim)
23250abd675eSChao Yu 			dquot_claim_block(inode, diff);
23260abd675eSChao Yu 		else
23270abd675eSChao Yu 			dquot_alloc_block_nofail(inode, diff);
23280abd675eSChao Yu 	} else {
23290abd675eSChao Yu 		dquot_free_block(inode, diff);
23300abd675eSChao Yu 	}
23310abd675eSChao Yu 
23327c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
233326de9b11SJaegeuk Kim 	if (clean || recover)
233426de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
23358edd03c8SJaegeuk Kim }
23368edd03c8SJaegeuk Kim 
2337fc9581c8SJaegeuk Kim static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
2338fc9581c8SJaegeuk Kim {
233926de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
234026de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
234126de9b11SJaegeuk Kim 
2342fc9581c8SJaegeuk Kim 	if (i_size_read(inode) == i_size)
2343fc9581c8SJaegeuk Kim 		return;
2344fc9581c8SJaegeuk Kim 
2345fc9581c8SJaegeuk Kim 	i_size_write(inode, i_size);
23467c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
234726de9b11SJaegeuk Kim 	if (clean || recover)
234826de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
234926de9b11SJaegeuk Kim }
235026de9b11SJaegeuk Kim 
2351205b9822SJaegeuk Kim static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
2352205b9822SJaegeuk Kim {
2353205b9822SJaegeuk Kim 	F2FS_I(inode)->i_current_depth = depth;
23547c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2355205b9822SJaegeuk Kim }
2356205b9822SJaegeuk Kim 
23571ad71a27SJaegeuk Kim static inline void f2fs_i_gc_failures_write(struct inode *inode,
23581ad71a27SJaegeuk Kim 					unsigned int count)
23591ad71a27SJaegeuk Kim {
23602ef79ecbSChao Yu 	F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN] = count;
23611ad71a27SJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
23621ad71a27SJaegeuk Kim }
23631ad71a27SJaegeuk Kim 
2364205b9822SJaegeuk Kim static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
2365205b9822SJaegeuk Kim {
2366205b9822SJaegeuk Kim 	F2FS_I(inode)->i_xattr_nid = xnid;
23677c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2368205b9822SJaegeuk Kim }
2369205b9822SJaegeuk Kim 
2370205b9822SJaegeuk Kim static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
2371205b9822SJaegeuk Kim {
2372205b9822SJaegeuk Kim 	F2FS_I(inode)->i_pino = pino;
23737c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2374205b9822SJaegeuk Kim }
2375205b9822SJaegeuk Kim 
237691942321SJaegeuk Kim static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
2377444c580fSJaegeuk Kim {
2378205b9822SJaegeuk Kim 	struct f2fs_inode_info *fi = F2FS_I(inode);
2379205b9822SJaegeuk Kim 
2380444c580fSJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_XATTR)
2381205b9822SJaegeuk Kim 		set_bit(FI_INLINE_XATTR, &fi->flags);
23821001b347SHuajun Li 	if (ri->i_inline & F2FS_INLINE_DATA)
2383205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DATA, &fi->flags);
238434d67debSChao Yu 	if (ri->i_inline & F2FS_INLINE_DENTRY)
2385205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DENTRY, &fi->flags);
2386b3d208f9SJaegeuk Kim 	if (ri->i_inline & F2FS_DATA_EXIST)
2387205b9822SJaegeuk Kim 		set_bit(FI_DATA_EXIST, &fi->flags);
2388510022a8SJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_DOTS)
2389205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DOTS, &fi->flags);
23907a2af766SChao Yu 	if (ri->i_inline & F2FS_EXTRA_ATTR)
23917a2af766SChao Yu 		set_bit(FI_EXTRA_ATTR, &fi->flags);
23921ad71a27SJaegeuk Kim 	if (ri->i_inline & F2FS_PIN_FILE)
23931ad71a27SJaegeuk Kim 		set_bit(FI_PIN_FILE, &fi->flags);
2394444c580fSJaegeuk Kim }
2395444c580fSJaegeuk Kim 
239691942321SJaegeuk Kim static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
2397444c580fSJaegeuk Kim {
2398444c580fSJaegeuk Kim 	ri->i_inline = 0;
2399444c580fSJaegeuk Kim 
240091942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_XATTR))
2401444c580fSJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_XATTR;
240291942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DATA))
24031001b347SHuajun Li 		ri->i_inline |= F2FS_INLINE_DATA;
240491942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
240534d67debSChao Yu 		ri->i_inline |= F2FS_INLINE_DENTRY;
240691942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_DATA_EXIST))
2407b3d208f9SJaegeuk Kim 		ri->i_inline |= F2FS_DATA_EXIST;
240891942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DOTS))
2409510022a8SJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_DOTS;
24107a2af766SChao Yu 	if (is_inode_flag_set(inode, FI_EXTRA_ATTR))
24117a2af766SChao Yu 		ri->i_inline |= F2FS_EXTRA_ATTR;
24121ad71a27SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_PIN_FILE))
24131ad71a27SJaegeuk Kim 		ri->i_inline |= F2FS_PIN_FILE;
24147a2af766SChao Yu }
24157a2af766SChao Yu 
24167a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode)
24177a2af766SChao Yu {
24187a2af766SChao Yu 	return is_inode_flag_set(inode, FI_EXTRA_ATTR);
2419444c580fSJaegeuk Kim }
2420444c580fSJaegeuk Kim 
2421987c7c31SChao Yu static inline int f2fs_has_inline_xattr(struct inode *inode)
2422987c7c31SChao Yu {
242391942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_XATTR);
2424987c7c31SChao Yu }
2425987c7c31SChao Yu 
242681ca7350SChao Yu static inline unsigned int addrs_per_inode(struct inode *inode)
2427de93653fSJaegeuk Kim {
2428b323fd28SChao Yu 	return CUR_ADDRS_PER_INODE(inode) - get_inline_xattr_addrs(inode);
2429de93653fSJaegeuk Kim }
2430de93653fSJaegeuk Kim 
24316afc662eSChao Yu static inline void *inline_xattr_addr(struct inode *inode, struct page *page)
243265985d93SJaegeuk Kim {
2433695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
2434cac5a3d8SDongOh Shin 
243565985d93SJaegeuk Kim 	return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
2436b323fd28SChao Yu 					get_inline_xattr_addrs(inode)]);
243765985d93SJaegeuk Kim }
243865985d93SJaegeuk Kim 
243965985d93SJaegeuk Kim static inline int inline_xattr_size(struct inode *inode)
244065985d93SJaegeuk Kim {
24416afc662eSChao Yu 	return get_inline_xattr_addrs(inode) * sizeof(__le32);
244265985d93SJaegeuk Kim }
244365985d93SJaegeuk Kim 
24440dbdc2aeSJaegeuk Kim static inline int f2fs_has_inline_data(struct inode *inode)
24450dbdc2aeSJaegeuk Kim {
244691942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DATA);
24470dbdc2aeSJaegeuk Kim }
24480dbdc2aeSJaegeuk Kim 
2449b3d208f9SJaegeuk Kim static inline int f2fs_exist_data(struct inode *inode)
2450b3d208f9SJaegeuk Kim {
245191942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DATA_EXIST);
2452b3d208f9SJaegeuk Kim }
2453b3d208f9SJaegeuk Kim 
2454510022a8SJaegeuk Kim static inline int f2fs_has_inline_dots(struct inode *inode)
2455510022a8SJaegeuk Kim {
245691942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DOTS);
2457510022a8SJaegeuk Kim }
2458510022a8SJaegeuk Kim 
24591ad71a27SJaegeuk Kim static inline bool f2fs_is_pinned_file(struct inode *inode)
24601ad71a27SJaegeuk Kim {
24611ad71a27SJaegeuk Kim 	return is_inode_flag_set(inode, FI_PIN_FILE);
24621ad71a27SJaegeuk Kim }
24631ad71a27SJaegeuk Kim 
246488b88a66SJaegeuk Kim static inline bool f2fs_is_atomic_file(struct inode *inode)
246588b88a66SJaegeuk Kim {
246691942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_ATOMIC_FILE);
246788b88a66SJaegeuk Kim }
246888b88a66SJaegeuk Kim 
24695fe45743SChao Yu static inline bool f2fs_is_commit_atomic_write(struct inode *inode)
24705fe45743SChao Yu {
24715fe45743SChao Yu 	return is_inode_flag_set(inode, FI_ATOMIC_COMMIT);
24725fe45743SChao Yu }
24735fe45743SChao Yu 
247402a1335fSJaegeuk Kim static inline bool f2fs_is_volatile_file(struct inode *inode)
247502a1335fSJaegeuk Kim {
247691942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_VOLATILE_FILE);
247702a1335fSJaegeuk Kim }
247802a1335fSJaegeuk Kim 
24793c6c2bebSJaegeuk Kim static inline bool f2fs_is_first_block_written(struct inode *inode)
24803c6c2bebSJaegeuk Kim {
248191942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
24823c6c2bebSJaegeuk Kim }
24833c6c2bebSJaegeuk Kim 
24841e84371fSJaegeuk Kim static inline bool f2fs_is_drop_cache(struct inode *inode)
24851e84371fSJaegeuk Kim {
248691942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DROP_CACHE);
24871e84371fSJaegeuk Kim }
24881e84371fSJaegeuk Kim 
2489f2470371SChao Yu static inline void *inline_data_addr(struct inode *inode, struct page *page)
24901001b347SHuajun Li {
2491695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
24927a2af766SChao Yu 	int extra_size = get_extra_isize(inode);
2493cac5a3d8SDongOh Shin 
24947a2af766SChao Yu 	return (void *)&(ri->i_addr[extra_size + DEF_INLINE_RESERVED_SIZE]);
24951001b347SHuajun Li }
24961001b347SHuajun Li 
249734d67debSChao Yu static inline int f2fs_has_inline_dentry(struct inode *inode)
249834d67debSChao Yu {
249991942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DENTRY);
250034d67debSChao Yu }
250134d67debSChao Yu 
2502b5492af7SJaegeuk Kim static inline int is_file(struct inode *inode, int type)
2503b5492af7SJaegeuk Kim {
2504b5492af7SJaegeuk Kim 	return F2FS_I(inode)->i_advise & type;
2505b5492af7SJaegeuk Kim }
2506b5492af7SJaegeuk Kim 
2507b5492af7SJaegeuk Kim static inline void set_file(struct inode *inode, int type)
2508b5492af7SJaegeuk Kim {
2509b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise |= type;
25107c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2511b5492af7SJaegeuk Kim }
2512b5492af7SJaegeuk Kim 
2513b5492af7SJaegeuk Kim static inline void clear_file(struct inode *inode, int type)
2514b5492af7SJaegeuk Kim {
2515b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise &= ~type;
25167c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2517b5492af7SJaegeuk Kim }
2518b5492af7SJaegeuk Kim 
251926787236SJaegeuk Kim static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
252026787236SJaegeuk Kim {
252195582b00SDeepa Dinamani 	struct timespec ts;
2522a0d00fadSChao Yu 	bool ret;
2523a0d00fadSChao Yu 
252426787236SJaegeuk Kim 	if (dsync) {
252526787236SJaegeuk Kim 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
252626787236SJaegeuk Kim 
252726787236SJaegeuk Kim 		spin_lock(&sbi->inode_lock[DIRTY_META]);
252826787236SJaegeuk Kim 		ret = list_empty(&F2FS_I(inode)->gdirty_list);
252926787236SJaegeuk Kim 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
253026787236SJaegeuk Kim 		return ret;
253126787236SJaegeuk Kim 	}
253226787236SJaegeuk Kim 	if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
253326787236SJaegeuk Kim 			file_keep_isize(inode) ||
2534235831d7SJunling Zheng 			i_size_read(inode) & ~PAGE_MASK)
253526787236SJaegeuk Kim 		return false;
2536a0d00fadSChao Yu 
253795582b00SDeepa Dinamani 	ts = timespec64_to_timespec(inode->i_atime);
253895582b00SDeepa Dinamani 	if (!timespec_equal(F2FS_I(inode)->i_disk_time, &ts))
2539214c2461SJaegeuk Kim 		return false;
254095582b00SDeepa Dinamani 	ts = timespec64_to_timespec(inode->i_ctime);
254195582b00SDeepa Dinamani 	if (!timespec_equal(F2FS_I(inode)->i_disk_time + 1, &ts))
2542214c2461SJaegeuk Kim 		return false;
254395582b00SDeepa Dinamani 	ts = timespec64_to_timespec(inode->i_mtime);
254495582b00SDeepa Dinamani 	if (!timespec_equal(F2FS_I(inode)->i_disk_time + 2, &ts))
2545214c2461SJaegeuk Kim 		return false;
2546214c2461SJaegeuk Kim 	if (!timespec_equal(F2FS_I(inode)->i_disk_time + 3,
2547214c2461SJaegeuk Kim 						&F2FS_I(inode)->i_crtime))
2548214c2461SJaegeuk Kim 		return false;
2549214c2461SJaegeuk Kim 
2550a0d00fadSChao Yu 	down_read(&F2FS_I(inode)->i_sem);
2551a0d00fadSChao Yu 	ret = F2FS_I(inode)->last_disk_size == i_size_read(inode);
2552a0d00fadSChao Yu 	up_read(&F2FS_I(inode)->i_sem);
2553a0d00fadSChao Yu 
2554a0d00fadSChao Yu 	return ret;
2555267378d4SChao Yu }
2556267378d4SChao Yu 
2557deeedd71SChao Yu static inline bool f2fs_readonly(struct super_block *sb)
255877888c1eSJaegeuk Kim {
2559deeedd71SChao Yu 	return sb_rdonly(sb);
256077888c1eSJaegeuk Kim }
256177888c1eSJaegeuk Kim 
2562744602cfSJaegeuk Kim static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
2563744602cfSJaegeuk Kim {
2564aaec2b1dSChao Yu 	return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
2565744602cfSJaegeuk Kim }
2566744602cfSJaegeuk Kim 
2567eaa693f4SJaegeuk Kim static inline bool is_dot_dotdot(const struct qstr *str)
2568eaa693f4SJaegeuk Kim {
2569eaa693f4SJaegeuk Kim 	if (str->len == 1 && str->name[0] == '.')
2570eaa693f4SJaegeuk Kim 		return true;
2571eaa693f4SJaegeuk Kim 
2572eaa693f4SJaegeuk Kim 	if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
2573eaa693f4SJaegeuk Kim 		return true;
2574eaa693f4SJaegeuk Kim 
2575eaa693f4SJaegeuk Kim 	return false;
2576eaa693f4SJaegeuk Kim }
2577eaa693f4SJaegeuk Kim 
25783e72f721SJaegeuk Kim static inline bool f2fs_may_extent_tree(struct inode *inode)
25793e72f721SJaegeuk Kim {
25803e72f721SJaegeuk Kim 	if (!test_opt(F2FS_I_SB(inode), EXTENT_CACHE) ||
258191942321SJaegeuk Kim 			is_inode_flag_set(inode, FI_NO_EXTENT))
25823e72f721SJaegeuk Kim 		return false;
25833e72f721SJaegeuk Kim 
2584886f56f9SAl Viro 	return S_ISREG(inode->i_mode);
25853e72f721SJaegeuk Kim }
25863e72f721SJaegeuk Kim 
25871ecc0c5cSChao Yu static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
25881ecc0c5cSChao Yu 					size_t size, gfp_t flags)
25890414b004SJaegeuk Kim {
25902c63feadSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
259155523519SChao Yu 	if (time_to_inject(sbi, FAULT_KMALLOC)) {
259255523519SChao Yu 		f2fs_show_injection_info(FAULT_KMALLOC);
25932c63feadSJaegeuk Kim 		return NULL;
259455523519SChao Yu 	}
25952c63feadSJaegeuk Kim #endif
25960414b004SJaegeuk Kim 	return kmalloc(size, flags);
25970414b004SJaegeuk Kim }
25980414b004SJaegeuk Kim 
2599acbf054dSChao Yu static inline void *f2fs_kzalloc(struct f2fs_sb_info *sbi,
2600acbf054dSChao Yu 					size_t size, gfp_t flags)
2601acbf054dSChao Yu {
2602acbf054dSChao Yu 	return f2fs_kmalloc(sbi, size, flags | __GFP_ZERO);
2603acbf054dSChao Yu }
2604acbf054dSChao Yu 
2605628b3d14SChao Yu static inline void *f2fs_kvmalloc(struct f2fs_sb_info *sbi,
2606628b3d14SChao Yu 					size_t size, gfp_t flags)
2607628b3d14SChao Yu {
2608628b3d14SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
2609628b3d14SChao Yu 	if (time_to_inject(sbi, FAULT_KVMALLOC)) {
2610628b3d14SChao Yu 		f2fs_show_injection_info(FAULT_KVMALLOC);
2611628b3d14SChao Yu 		return NULL;
2612628b3d14SChao Yu 	}
2613628b3d14SChao Yu #endif
2614628b3d14SChao Yu 	return kvmalloc(size, flags);
2615628b3d14SChao Yu }
2616628b3d14SChao Yu 
2617628b3d14SChao Yu static inline void *f2fs_kvzalloc(struct f2fs_sb_info *sbi,
2618628b3d14SChao Yu 					size_t size, gfp_t flags)
2619628b3d14SChao Yu {
2620628b3d14SChao Yu 	return f2fs_kvmalloc(sbi, size, flags | __GFP_ZERO);
2621628b3d14SChao Yu }
2622628b3d14SChao Yu 
26237a2af766SChao Yu static inline int get_extra_isize(struct inode *inode)
2624f2470371SChao Yu {
26257a2af766SChao Yu 	return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
2626f2470371SChao Yu }
2627f2470371SChao Yu 
26286afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode)
26296afc662eSChao Yu {
26306afc662eSChao Yu 	return F2FS_I(inode)->i_inline_xattr_size;
26316afc662eSChao Yu }
26326afc662eSChao Yu 
26334d57b86dSChao Yu #define f2fs_get_inode_mode(i) \
263491942321SJaegeuk Kim 	((is_inode_flag_set(i, FI_ACL_MODE)) ? \
2635a6dda0e6SChristoph Hellwig 	 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
2636a6dda0e6SChristoph Hellwig 
26377a2af766SChao Yu #define F2FS_TOTAL_EXTRA_ATTR_SIZE			\
26387a2af766SChao Yu 	(offsetof(struct f2fs_inode, i_extra_end) -	\
26397a2af766SChao Yu 	offsetof(struct f2fs_inode, i_extra_isize))	\
26407a2af766SChao Yu 
26415c57132eSChao Yu #define F2FS_OLD_ATTRIBUTE_SIZE	(offsetof(struct f2fs_inode, i_addr))
26425c57132eSChao Yu #define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field)		\
26435c57132eSChao Yu 		((offsetof(typeof(*f2fs_inode), field) +	\
26445c57132eSChao Yu 		sizeof((f2fs_inode)->field))			\
26455c57132eSChao Yu 		<= (F2FS_OLD_ATTRIBUTE_SIZE + extra_isize))	\
26465c57132eSChao Yu 
2647b0af6d49SChao Yu static inline void f2fs_reset_iostat(struct f2fs_sb_info *sbi)
2648b0af6d49SChao Yu {
2649b0af6d49SChao Yu 	int i;
2650b0af6d49SChao Yu 
2651b0af6d49SChao Yu 	spin_lock(&sbi->iostat_lock);
2652b0af6d49SChao Yu 	for (i = 0; i < NR_IO_TYPE; i++)
2653b0af6d49SChao Yu 		sbi->write_iostat[i] = 0;
2654b0af6d49SChao Yu 	spin_unlock(&sbi->iostat_lock);
2655b0af6d49SChao Yu }
2656b0af6d49SChao Yu 
2657b0af6d49SChao Yu static inline void f2fs_update_iostat(struct f2fs_sb_info *sbi,
2658b0af6d49SChao Yu 			enum iostat_type type, unsigned long long io_bytes)
2659b0af6d49SChao Yu {
2660b0af6d49SChao Yu 	if (!sbi->iostat_enable)
2661b0af6d49SChao Yu 		return;
2662b0af6d49SChao Yu 	spin_lock(&sbi->iostat_lock);
2663b0af6d49SChao Yu 	sbi->write_iostat[type] += io_bytes;
2664b0af6d49SChao Yu 
2665b0af6d49SChao Yu 	if (type == APP_WRITE_IO || type == APP_DIRECT_IO)
2666b0af6d49SChao Yu 		sbi->write_iostat[APP_BUFFERED_IO] =
2667b0af6d49SChao Yu 			sbi->write_iostat[APP_WRITE_IO] -
2668b0af6d49SChao Yu 			sbi->write_iostat[APP_DIRECT_IO];
2669b0af6d49SChao Yu 	spin_unlock(&sbi->iostat_lock);
2670b0af6d49SChao Yu }
2671b0af6d49SChao Yu 
26727b525dd0SChao Yu static inline bool is_valid_blkaddr(block_t blkaddr)
26737b525dd0SChao Yu {
26747b525dd0SChao Yu 	if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
26757b525dd0SChao Yu 		return false;
26767b525dd0SChao Yu 	return true;
26777b525dd0SChao Yu }
26787b525dd0SChao Yu 
267939a53e0cSJaegeuk Kim /*
268039a53e0cSJaegeuk Kim  * file.c
268139a53e0cSJaegeuk Kim  */
2682cac5a3d8SDongOh Shin int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
26834d57b86dSChao Yu void f2fs_truncate_data_blocks(struct dnode_of_data *dn);
26844d57b86dSChao Yu int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock);
2685cac5a3d8SDongOh Shin int f2fs_truncate(struct inode *inode);
2686a528d35eSDavid Howells int f2fs_getattr(const struct path *path, struct kstat *stat,
2687a528d35eSDavid Howells 			u32 request_mask, unsigned int flags);
2688cac5a3d8SDongOh Shin int f2fs_setattr(struct dentry *dentry, struct iattr *attr);
26894d57b86dSChao Yu int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
26904d57b86dSChao Yu void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count);
2691c4020b2dSChao Yu int f2fs_precache_extents(struct inode *inode);
2692cac5a3d8SDongOh Shin long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
2693cac5a3d8SDongOh Shin long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
26941ad71a27SJaegeuk Kim int f2fs_pin_file_control(struct inode *inode, bool inc);
269539a53e0cSJaegeuk Kim 
269639a53e0cSJaegeuk Kim /*
269739a53e0cSJaegeuk Kim  * inode.c
269839a53e0cSJaegeuk Kim  */
2699cac5a3d8SDongOh Shin void f2fs_set_inode_flags(struct inode *inode);
2700704956ecSChao Yu bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page);
2701704956ecSChao Yu void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page);
2702cac5a3d8SDongOh Shin struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
2703cac5a3d8SDongOh Shin struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
27044d57b86dSChao Yu int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
27054d57b86dSChao Yu void f2fs_update_inode(struct inode *inode, struct page *node_page);
27064d57b86dSChao Yu void f2fs_update_inode_page(struct inode *inode);
2707cac5a3d8SDongOh Shin int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
2708cac5a3d8SDongOh Shin void f2fs_evict_inode(struct inode *inode);
27094d57b86dSChao Yu void f2fs_handle_failed_inode(struct inode *inode);
271039a53e0cSJaegeuk Kim 
271139a53e0cSJaegeuk Kim /*
271239a53e0cSJaegeuk Kim  * namei.c
271339a53e0cSJaegeuk Kim  */
27144d57b86dSChao Yu int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
2715b6a06cbbSChao Yu 							bool hot, bool set);
271639a53e0cSJaegeuk Kim struct dentry *f2fs_get_parent(struct dentry *child);
271739a53e0cSJaegeuk Kim 
271839a53e0cSJaegeuk Kim /*
271939a53e0cSJaegeuk Kim  * dir.c
272039a53e0cSJaegeuk Kim  */
27214d57b86dSChao Yu unsigned char f2fs_get_de_type(struct f2fs_dir_entry *de);
27224d57b86dSChao Yu struct f2fs_dir_entry *f2fs_find_target_dentry(struct fscrypt_name *fname,
2723cac5a3d8SDongOh Shin 			f2fs_hash_t namehash, int *max_slots,
2724cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
2725cac5a3d8SDongOh Shin int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
2726cac5a3d8SDongOh Shin 			unsigned int start_pos, struct fscrypt_str *fstr);
27274d57b86dSChao Yu void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
2728cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
27294d57b86dSChao Yu struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
2730cac5a3d8SDongOh Shin 			const struct qstr *new_name,
2731cac5a3d8SDongOh Shin 			const struct qstr *orig_name, struct page *dpage);
27324d57b86dSChao Yu void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
2733cac5a3d8SDongOh Shin 			unsigned int current_depth);
27344d57b86dSChao Yu int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots);
2735cac5a3d8SDongOh Shin void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
2736cac5a3d8SDongOh Shin struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
2737cac5a3d8SDongOh Shin 			struct fscrypt_name *fname, struct page **res_page);
2738cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
2739cac5a3d8SDongOh Shin 			const struct qstr *child, struct page **res_page);
2740cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
2741cac5a3d8SDongOh Shin ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
2742cac5a3d8SDongOh Shin 			struct page **page);
2743cac5a3d8SDongOh Shin void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
2744cac5a3d8SDongOh Shin 			struct page *page, struct inode *inode);
2745cac5a3d8SDongOh Shin void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
2746cac5a3d8SDongOh Shin 			const struct qstr *name, f2fs_hash_t name_hash,
2747cac5a3d8SDongOh Shin 			unsigned int bit_pos);
2748cac5a3d8SDongOh Shin int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
2749cac5a3d8SDongOh Shin 			const struct qstr *orig_name,
2750cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
27514d57b86dSChao Yu int f2fs_add_dentry(struct inode *dir, struct fscrypt_name *fname,
2752cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
27534d57b86dSChao Yu int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
2754cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
2755cac5a3d8SDongOh Shin void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
2756cac5a3d8SDongOh Shin 			struct inode *dir, struct inode *inode);
2757cac5a3d8SDongOh Shin int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
2758cac5a3d8SDongOh Shin bool f2fs_empty_dir(struct inode *dir);
275939a53e0cSJaegeuk Kim 
2760b7f7a5e0SAl Viro static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
2761b7f7a5e0SAl Viro {
27624d57b86dSChao Yu 	return f2fs_do_add_link(d_inode(dentry->d_parent), &dentry->d_name,
2763510022a8SJaegeuk Kim 				inode, inode->i_ino, inode->i_mode);
2764b7f7a5e0SAl Viro }
2765b7f7a5e0SAl Viro 
276639a53e0cSJaegeuk Kim /*
276739a53e0cSJaegeuk Kim  * super.c
276839a53e0cSJaegeuk Kim  */
2769cac5a3d8SDongOh Shin int f2fs_inode_dirtied(struct inode *inode, bool sync);
2770cac5a3d8SDongOh Shin void f2fs_inode_synced(struct inode *inode);
2771ea676733SJaegeuk Kim int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly);
27724b2414d0SChao Yu void f2fs_quota_off_umount(struct super_block *sb);
2773cac5a3d8SDongOh Shin int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
2774cac5a3d8SDongOh Shin int f2fs_sync_fs(struct super_block *sb, int sync);
2775a07ef784SNamjae Jeon extern __printf(3, 4)
2776cac5a3d8SDongOh Shin void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...);
27774d57b86dSChao Yu int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi);
277839a53e0cSJaegeuk Kim 
277939a53e0cSJaegeuk Kim /*
278039a53e0cSJaegeuk Kim  * hash.c
278139a53e0cSJaegeuk Kim  */
27826332cd32SJaegeuk Kim f2fs_hash_t f2fs_dentry_hash(const struct qstr *name_info,
27836332cd32SJaegeuk Kim 				struct fscrypt_name *fname);
278439a53e0cSJaegeuk Kim 
278539a53e0cSJaegeuk Kim /*
278639a53e0cSJaegeuk Kim  * node.c
278739a53e0cSJaegeuk Kim  */
278839a53e0cSJaegeuk Kim struct dnode_of_data;
278939a53e0cSJaegeuk Kim struct node_info;
279039a53e0cSJaegeuk Kim 
27914d57b86dSChao Yu int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid);
27924d57b86dSChao Yu bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type);
27934d57b86dSChao Yu int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
27944d57b86dSChao Yu bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
27954d57b86dSChao Yu bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
27964d57b86dSChao Yu void f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
27974d57b86dSChao Yu 						struct node_info *ni);
27984d57b86dSChao Yu pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
27994d57b86dSChao Yu int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
28004d57b86dSChao Yu int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from);
28014d57b86dSChao Yu int f2fs_truncate_xattr_node(struct inode *inode);
28024d57b86dSChao Yu int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino);
28034d57b86dSChao Yu int f2fs_remove_inode_page(struct inode *inode);
28044d57b86dSChao Yu struct page *f2fs_new_inode_page(struct inode *inode);
28054d57b86dSChao Yu struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs);
28064d57b86dSChao Yu void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
28074d57b86dSChao Yu struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
28084d57b86dSChao Yu struct page *f2fs_get_node_page_ra(struct page *parent, int start);
28094d57b86dSChao Yu void f2fs_move_node_page(struct page *node_page, int gc_type);
28104d57b86dSChao Yu int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
2811cac5a3d8SDongOh Shin 			struct writeback_control *wbc, bool atomic);
28124d57b86dSChao Yu int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
28134d57b86dSChao Yu 			struct writeback_control *wbc,
2814b0af6d49SChao Yu 			bool do_balance, enum iostat_type io_type);
28154d57b86dSChao Yu void f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
28164d57b86dSChao Yu bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
28174d57b86dSChao Yu void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
28184d57b86dSChao Yu void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
28194d57b86dSChao Yu int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
28204d57b86dSChao Yu void f2fs_recover_inline_xattr(struct inode *inode, struct page *page);
28214d57b86dSChao Yu int f2fs_recover_xattr_data(struct inode *inode, struct page *page);
28224d57b86dSChao Yu int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
28234d57b86dSChao Yu void f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
2824cac5a3d8SDongOh Shin 			unsigned int segno, struct f2fs_summary_block *sum);
28254d57b86dSChao Yu void f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
28264d57b86dSChao Yu int f2fs_build_node_manager(struct f2fs_sb_info *sbi);
28274d57b86dSChao Yu void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi);
28284d57b86dSChao Yu int __init f2fs_create_node_manager_caches(void);
28294d57b86dSChao Yu void f2fs_destroy_node_manager_caches(void);
283039a53e0cSJaegeuk Kim 
283139a53e0cSJaegeuk Kim /*
283239a53e0cSJaegeuk Kim  * segment.c
283339a53e0cSJaegeuk Kim  */
28344d57b86dSChao Yu bool f2fs_need_SSR(struct f2fs_sb_info *sbi);
28354d57b86dSChao Yu void f2fs_register_inmem_page(struct inode *inode, struct page *page);
28364d57b86dSChao Yu void f2fs_drop_inmem_pages_all(struct f2fs_sb_info *sbi, bool gc_failure);
28374d57b86dSChao Yu void f2fs_drop_inmem_pages(struct inode *inode);
28384d57b86dSChao Yu void f2fs_drop_inmem_page(struct inode *inode, struct page *page);
28394d57b86dSChao Yu int f2fs_commit_inmem_pages(struct inode *inode);
2840cac5a3d8SDongOh Shin void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
2841cac5a3d8SDongOh Shin void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi);
284239d787beSChao Yu int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
28434d57b86dSChao Yu int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
28441228b482SChao Yu int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
28454d57b86dSChao Yu void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
28464d57b86dSChao Yu void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
28474d57b86dSChao Yu bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
28484d57b86dSChao Yu void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
28494d57b86dSChao Yu void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi);
2850cf5c759fSChao Yu bool f2fs_wait_discard_bios(struct f2fs_sb_info *sbi);
28514d57b86dSChao Yu void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
28524d57b86dSChao Yu 					struct cp_control *cpc);
28534d57b86dSChao Yu void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi);
28544d57b86dSChao Yu int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
28554d57b86dSChao Yu void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi);
2856cac5a3d8SDongOh Shin int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
28574d57b86dSChao Yu bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
28584d57b86dSChao Yu 					struct cp_control *cpc);
28594d57b86dSChao Yu struct page *f2fs_get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
28604d57b86dSChao Yu void f2fs_update_meta_page(struct f2fs_sb_info *sbi, void *src,
28614d57b86dSChao Yu 					block_t blk_addr);
28624d57b86dSChao Yu void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
2863b0af6d49SChao Yu 						enum iostat_type io_type);
28644d57b86dSChao Yu void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio);
28654d57b86dSChao Yu void f2fs_outplace_write_data(struct dnode_of_data *dn,
28664d57b86dSChao Yu 			struct f2fs_io_info *fio);
28674d57b86dSChao Yu int f2fs_inplace_write_data(struct f2fs_io_info *fio);
28684d57b86dSChao Yu void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
2869cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t new_blkaddr,
2870cac5a3d8SDongOh Shin 			bool recover_curseg, bool recover_newaddr);
2871cac5a3d8SDongOh Shin void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
2872cac5a3d8SDongOh Shin 			block_t old_addr, block_t new_addr,
2873cac5a3d8SDongOh Shin 			unsigned char version, bool recover_curseg,
2874cac5a3d8SDongOh Shin 			bool recover_newaddr);
28754d57b86dSChao Yu void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
2876cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t *new_blkaddr,
2877fb830fc5SChao Yu 			struct f2fs_summary *sum, int type,
2878fb830fc5SChao Yu 			struct f2fs_io_info *fio, bool add_list);
2879cac5a3d8SDongOh Shin void f2fs_wait_on_page_writeback(struct page *page,
2880cac5a3d8SDongOh Shin 			enum page_type type, bool ordered);
2881d4c759eeSJaegeuk Kim void f2fs_wait_on_block_writeback(struct f2fs_sb_info *sbi, block_t blkaddr);
28824d57b86dSChao Yu void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
28834d57b86dSChao Yu void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
28844d57b86dSChao Yu int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
2885cac5a3d8SDongOh Shin 			unsigned int val, int alloc);
28864d57b86dSChao Yu void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
28874d57b86dSChao Yu int f2fs_build_segment_manager(struct f2fs_sb_info *sbi);
28884d57b86dSChao Yu void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi);
28894d57b86dSChao Yu int __init f2fs_create_segment_manager_caches(void);
28904d57b86dSChao Yu void f2fs_destroy_segment_manager_caches(void);
28914d57b86dSChao Yu int f2fs_rw_hint_to_seg_type(enum rw_hint hint);
28924d57b86dSChao Yu enum rw_hint f2fs_io_type_to_rw_hint(struct f2fs_sb_info *sbi,
28934d57b86dSChao Yu 			enum page_type type, enum temp_type temp);
289439a53e0cSJaegeuk Kim 
289539a53e0cSJaegeuk Kim /*
289639a53e0cSJaegeuk Kim  * checkpoint.c
289739a53e0cSJaegeuk Kim  */
2898cac5a3d8SDongOh Shin void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io);
28994d57b86dSChao Yu struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
29004d57b86dSChao Yu struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
29014d57b86dSChao Yu struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
29024d57b86dSChao Yu bool f2fs_is_valid_meta_blkaddr(struct f2fs_sb_info *sbi,
29034d57b86dSChao Yu 			block_t blkaddr, int type);
29044d57b86dSChao Yu int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
2905cac5a3d8SDongOh Shin 			int type, bool sync);
29064d57b86dSChao Yu void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index);
29074d57b86dSChao Yu long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
2908b0af6d49SChao Yu 			long nr_to_write, enum iostat_type io_type);
29094d57b86dSChao Yu void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
29104d57b86dSChao Yu void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
29114d57b86dSChao Yu void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all);
29124d57b86dSChao Yu bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
29134d57b86dSChao Yu void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
291439d787beSChao Yu 					unsigned int devidx, int type);
29154d57b86dSChao Yu bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
291639d787beSChao Yu 					unsigned int devidx, int type);
2917cac5a3d8SDongOh Shin int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
29184d57b86dSChao Yu int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi);
29194d57b86dSChao Yu void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi);
29204d57b86dSChao Yu void f2fs_add_orphan_inode(struct inode *inode);
29214d57b86dSChao Yu void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
29224d57b86dSChao Yu int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi);
29234d57b86dSChao Yu int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi);
29244d57b86dSChao Yu void f2fs_update_dirty_page(struct inode *inode, struct page *page);
29254d57b86dSChao Yu void f2fs_remove_dirty_inode(struct inode *inode);
29264d57b86dSChao Yu int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
29274d57b86dSChao Yu int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
29284d57b86dSChao Yu void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi);
29294d57b86dSChao Yu int __init f2fs_create_checkpoint_caches(void);
29304d57b86dSChao Yu void f2fs_destroy_checkpoint_caches(void);
293139a53e0cSJaegeuk Kim 
293239a53e0cSJaegeuk Kim /*
293339a53e0cSJaegeuk Kim  * data.c
293439a53e0cSJaegeuk Kim  */
29356dbb1796SEric Biggers int f2fs_init_post_read_processing(void);
29366dbb1796SEric Biggers void f2fs_destroy_post_read_processing(void);
2937b9109b0eSJaegeuk Kim void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
2938b9109b0eSJaegeuk Kim void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
2939942fd319SJaegeuk Kim 				struct inode *inode, nid_t ino, pgoff_t idx,
2940b9109b0eSJaegeuk Kim 				enum page_type type);
2941b9109b0eSJaegeuk Kim void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
2942cac5a3d8SDongOh Shin int f2fs_submit_page_bio(struct f2fs_io_info *fio);
2943fe16efe6SChao Yu void f2fs_submit_page_write(struct f2fs_io_info *fio);
2944cac5a3d8SDongOh Shin struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
2945cac5a3d8SDongOh Shin 			block_t blk_addr, struct bio *bio);
2946cac5a3d8SDongOh Shin int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
29474d57b86dSChao Yu void f2fs_set_data_blkaddr(struct dnode_of_data *dn);
2948cac5a3d8SDongOh Shin void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
29494d57b86dSChao Yu int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
29504d57b86dSChao Yu int f2fs_reserve_new_block(struct dnode_of_data *dn);
2951cac5a3d8SDongOh Shin int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
2952cac5a3d8SDongOh Shin int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from);
2953cac5a3d8SDongOh Shin int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
29544d57b86dSChao Yu struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
2955cac5a3d8SDongOh Shin 			int op_flags, bool for_write);
29564d57b86dSChao Yu struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index);
29574d57b86dSChao Yu struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
2958cac5a3d8SDongOh Shin 			bool for_write);
29594d57b86dSChao Yu struct page *f2fs_get_new_data_page(struct inode *inode,
2960cac5a3d8SDongOh Shin 			struct page *ipage, pgoff_t index, bool new_i_size);
29614d57b86dSChao Yu int f2fs_do_write_data_page(struct f2fs_io_info *fio);
2962cac5a3d8SDongOh Shin int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
2963cac5a3d8SDongOh Shin 			int create, int flag);
2964cac5a3d8SDongOh Shin int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2965cac5a3d8SDongOh Shin 			u64 start, u64 len);
29664d57b86dSChao Yu bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio);
29674d57b86dSChao Yu bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio);
2968cac5a3d8SDongOh Shin void f2fs_invalidate_page(struct page *page, unsigned int offset,
2969cac5a3d8SDongOh Shin 			unsigned int length);
2970cac5a3d8SDongOh Shin int f2fs_release_page(struct page *page, gfp_t wait);
29715b7a487cSWeichao Guo #ifdef CONFIG_MIGRATION
2972cac5a3d8SDongOh Shin int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,
2973cac5a3d8SDongOh Shin 			struct page *page, enum migrate_mode mode);
29745b7a487cSWeichao Guo #endif
2975b91050a8SHyunchul Lee bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len);
29764d57b86dSChao Yu void f2fs_clear_radix_tree_dirty_tag(struct page *page);
297739a53e0cSJaegeuk Kim 
297839a53e0cSJaegeuk Kim /*
297939a53e0cSJaegeuk Kim  * gc.c
298039a53e0cSJaegeuk Kim  */
29814d57b86dSChao Yu int f2fs_start_gc_thread(struct f2fs_sb_info *sbi);
29824d57b86dSChao Yu void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi);
29834d57b86dSChao Yu block_t f2fs_start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
2984e066b83cSJaegeuk Kim int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background,
2985e066b83cSJaegeuk Kim 			unsigned int segno);
29864d57b86dSChao Yu void f2fs_build_gc_manager(struct f2fs_sb_info *sbi);
298739a53e0cSJaegeuk Kim 
298839a53e0cSJaegeuk Kim /*
298939a53e0cSJaegeuk Kim  * recovery.c
299039a53e0cSJaegeuk Kim  */
29914d57b86dSChao Yu int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
29924d57b86dSChao Yu bool f2fs_space_for_roll_forward(struct f2fs_sb_info *sbi);
299339a53e0cSJaegeuk Kim 
299439a53e0cSJaegeuk Kim /*
299539a53e0cSJaegeuk Kim  * debug.c
299639a53e0cSJaegeuk Kim  */
299739a53e0cSJaegeuk Kim #ifdef CONFIG_F2FS_STAT_FS
299839a53e0cSJaegeuk Kim struct f2fs_stat_info {
299939a53e0cSJaegeuk Kim 	struct list_head stat_list;
300039a53e0cSJaegeuk Kim 	struct f2fs_sb_info *sbi;
300139a53e0cSJaegeuk Kim 	int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
300239a53e0cSJaegeuk Kim 	int main_area_segs, main_area_sections, main_area_zones;
30035b7ee374SChao Yu 	unsigned long long hit_largest, hit_cached, hit_rbtree;
30045b7ee374SChao Yu 	unsigned long long hit_total, total_ext;
3005c00ba554SJaegeuk Kim 	int ext_tree, zombie_tree, ext_node;
30062c8a4a28SJaegeuk Kim 	int ndirty_node, ndirty_dent, ndirty_meta, ndirty_imeta;
30072c8a4a28SJaegeuk Kim 	int ndirty_data, ndirty_qdata;
300835782b23SJaegeuk Kim 	int inmem_pages;
30092c8a4a28SJaegeuk Kim 	unsigned int ndirty_dirs, ndirty_files, nquota_files, ndirty_all;
30105b0ef73cSJaegeuk Kim 	int nats, dirty_nats, sits, dirty_sits;
30115b0ef73cSJaegeuk Kim 	int free_nids, avail_nids, alloc_nids;
301239a53e0cSJaegeuk Kim 	int total_count, utilization;
30138b8dd65fSChao Yu 	int bg_gc, nr_wb_cp_data, nr_wb_data;
301414d8d5f7SChao Yu 	int nr_flushing, nr_flushed, flush_list_empty;
301514d8d5f7SChao Yu 	int nr_discarding, nr_discarded;
30165f32366aSChao Yu 	int nr_discard_cmd;
3017d84d1cbdSChao Yu 	unsigned int undiscard_blks;
3018a00861dbSJaegeuk Kim 	int inline_xattr, inline_inode, inline_dir, append, update, orphans;
3019648d50baSChao Yu 	int aw_cnt, max_aw_cnt, vw_cnt, max_vw_cnt;
3020f83a2584SYunlei He 	unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
302139a53e0cSJaegeuk Kim 	unsigned int bimodal, avg_vblocks;
302239a53e0cSJaegeuk Kim 	int util_free, util_valid, util_invalid;
302339a53e0cSJaegeuk Kim 	int rsvd_segs, overp_segs;
302439a53e0cSJaegeuk Kim 	int dirty_count, node_pages, meta_pages;
302542190d2aSJaegeuk Kim 	int prefree_count, call_count, cp_count, bg_cp_count;
302639a53e0cSJaegeuk Kim 	int tot_segs, node_segs, data_segs, free_segs, free_secs;
3027e1235983SChangman Lee 	int bg_node_segs, bg_data_segs;
302839a53e0cSJaegeuk Kim 	int tot_blks, data_blks, node_blks;
3029e1235983SChangman Lee 	int bg_data_blks, bg_node_blks;
30302ef79ecbSChao Yu 	unsigned long long skipped_atomic_files[2];
303139a53e0cSJaegeuk Kim 	int curseg[NR_CURSEG_TYPE];
303239a53e0cSJaegeuk Kim 	int cursec[NR_CURSEG_TYPE];
303339a53e0cSJaegeuk Kim 	int curzone[NR_CURSEG_TYPE];
303439a53e0cSJaegeuk Kim 
303539a53e0cSJaegeuk Kim 	unsigned int segment_count[2];
303639a53e0cSJaegeuk Kim 	unsigned int block_count[2];
3037b9a2c252SChangman Lee 	unsigned int inplace_count;
30389edcdabfSChao Yu 	unsigned long long base_mem, cache_mem, page_mem;
303939a53e0cSJaegeuk Kim };
304039a53e0cSJaegeuk Kim 
3041963d4f7dSGu Zheng static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
3042963d4f7dSGu Zheng {
3043963d4f7dSGu Zheng 	return (struct f2fs_stat_info *)sbi->stat_info;
3044963d4f7dSGu Zheng }
3045963d4f7dSGu Zheng 
3046942e0be6SChangman Lee #define stat_inc_cp_count(si)		((si)->cp_count++)
304742190d2aSJaegeuk Kim #define stat_inc_bg_cp_count(si)	((si)->bg_cp_count++)
304839a53e0cSJaegeuk Kim #define stat_inc_call_count(si)		((si)->call_count++)
3049dcdfff65SJaegeuk Kim #define stat_inc_bggc_count(sbi)	((sbi)->bg_gc++)
305033fbd510SChao Yu #define stat_inc_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]++)
305133fbd510SChao Yu #define stat_dec_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]--)
30525b7ee374SChao Yu #define stat_inc_total_hit(sbi)		(atomic64_inc(&(sbi)->total_hit_ext))
30535b7ee374SChao Yu #define stat_inc_rbtree_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_rbtree))
30545b7ee374SChao Yu #define stat_inc_largest_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_largest))
30555b7ee374SChao Yu #define stat_inc_cached_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_cached))
3056d5e8f6c9SChao Yu #define stat_inc_inline_xattr(inode)					\
3057d5e8f6c9SChao Yu 	do {								\
3058d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3059d5e8f6c9SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_xattr));	\
3060d5e8f6c9SChao Yu 	} while (0)
3061d5e8f6c9SChao Yu #define stat_dec_inline_xattr(inode)					\
3062d5e8f6c9SChao Yu 	do {								\
3063d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3064d5e8f6c9SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_xattr));	\
3065d5e8f6c9SChao Yu 	} while (0)
30660dbdc2aeSJaegeuk Kim #define stat_inc_inline_inode(inode)					\
30670dbdc2aeSJaegeuk Kim 	do {								\
30680dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
306903e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_inode));	\
30700dbdc2aeSJaegeuk Kim 	} while (0)
30710dbdc2aeSJaegeuk Kim #define stat_dec_inline_inode(inode)					\
30720dbdc2aeSJaegeuk Kim 	do {								\
30730dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
307403e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_inode));	\
30750dbdc2aeSJaegeuk Kim 	} while (0)
30763289c061SJaegeuk Kim #define stat_inc_inline_dir(inode)					\
30773289c061SJaegeuk Kim 	do {								\
30783289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
307903e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_dir));	\
30803289c061SJaegeuk Kim 	} while (0)
30813289c061SJaegeuk Kim #define stat_dec_inline_dir(inode)					\
30823289c061SJaegeuk Kim 	do {								\
30833289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
308403e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_dir));	\
30853289c061SJaegeuk Kim 	} while (0)
3086dcdfff65SJaegeuk Kim #define stat_inc_seg_type(sbi, curseg)					\
3087dcdfff65SJaegeuk Kim 		((sbi)->segment_count[(curseg)->alloc_type]++)
3088dcdfff65SJaegeuk Kim #define stat_inc_block_count(sbi, curseg)				\
3089dcdfff65SJaegeuk Kim 		((sbi)->block_count[(curseg)->alloc_type]++)
3090b9a2c252SChangman Lee #define stat_inc_inplace_blocks(sbi)					\
3091b9a2c252SChangman Lee 		(atomic_inc(&(sbi)->inplace_count))
309226a28a0cSJaegeuk Kim #define stat_inc_atomic_write(inode)					\
3093cac5a3d8SDongOh Shin 		(atomic_inc(&F2FS_I_SB(inode)->aw_cnt))
309426a28a0cSJaegeuk Kim #define stat_dec_atomic_write(inode)					\
3095cac5a3d8SDongOh Shin 		(atomic_dec(&F2FS_I_SB(inode)->aw_cnt))
309626a28a0cSJaegeuk Kim #define stat_update_max_atomic_write(inode)				\
309726a28a0cSJaegeuk Kim 	do {								\
309826a28a0cSJaegeuk Kim 		int cur = atomic_read(&F2FS_I_SB(inode)->aw_cnt);	\
309926a28a0cSJaegeuk Kim 		int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt);	\
310026a28a0cSJaegeuk Kim 		if (cur > max)						\
310126a28a0cSJaegeuk Kim 			atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur);	\
310226a28a0cSJaegeuk Kim 	} while (0)
3103648d50baSChao Yu #define stat_inc_volatile_write(inode)					\
3104648d50baSChao Yu 		(atomic_inc(&F2FS_I_SB(inode)->vw_cnt))
3105648d50baSChao Yu #define stat_dec_volatile_write(inode)					\
3106648d50baSChao Yu 		(atomic_dec(&F2FS_I_SB(inode)->vw_cnt))
3107648d50baSChao Yu #define stat_update_max_volatile_write(inode)				\
3108648d50baSChao Yu 	do {								\
3109648d50baSChao Yu 		int cur = atomic_read(&F2FS_I_SB(inode)->vw_cnt);	\
3110648d50baSChao Yu 		int max = atomic_read(&F2FS_I_SB(inode)->max_vw_cnt);	\
3111648d50baSChao Yu 		if (cur > max)						\
3112648d50baSChao Yu 			atomic_set(&F2FS_I_SB(inode)->max_vw_cnt, cur);	\
3113648d50baSChao Yu 	} while (0)
3114e1235983SChangman Lee #define stat_inc_seg_count(sbi, type, gc_type)				\
311539a53e0cSJaegeuk Kim 	do {								\
3116963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
311768afcf2dSTomohiro Kusumi 		si->tot_segs++;						\
311868afcf2dSTomohiro Kusumi 		if ((type) == SUM_TYPE_DATA) {				\
311939a53e0cSJaegeuk Kim 			si->data_segs++;				\
3120e1235983SChangman Lee 			si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0;	\
3121e1235983SChangman Lee 		} else {						\
312239a53e0cSJaegeuk Kim 			si->node_segs++;				\
3123e1235983SChangman Lee 			si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0;	\
3124e1235983SChangman Lee 		}							\
312539a53e0cSJaegeuk Kim 	} while (0)
312639a53e0cSJaegeuk Kim 
312739a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks)				\
312868afcf2dSTomohiro Kusumi 	((si)->tot_blks += (blks))
312939a53e0cSJaegeuk Kim 
3130e1235983SChangman Lee #define stat_inc_data_blk_count(sbi, blks, gc_type)			\
313139a53e0cSJaegeuk Kim 	do {								\
3132963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
313339a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
313439a53e0cSJaegeuk Kim 		si->data_blks += (blks);				\
313568afcf2dSTomohiro Kusumi 		si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
313639a53e0cSJaegeuk Kim 	} while (0)
313739a53e0cSJaegeuk Kim 
3138e1235983SChangman Lee #define stat_inc_node_blk_count(sbi, blks, gc_type)			\
313939a53e0cSJaegeuk Kim 	do {								\
3140963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
314139a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
314239a53e0cSJaegeuk Kim 		si->node_blks += (blks);				\
314368afcf2dSTomohiro Kusumi 		si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
314439a53e0cSJaegeuk Kim 	} while (0)
314539a53e0cSJaegeuk Kim 
3146cac5a3d8SDongOh Shin int f2fs_build_stats(struct f2fs_sb_info *sbi);
3147cac5a3d8SDongOh Shin void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
3148787c7b8cSChao Yu int __init f2fs_create_root_stats(void);
31494589d25dSNamjae Jeon void f2fs_destroy_root_stats(void);
315039a53e0cSJaegeuk Kim #else
3151d66450e7SArnd Bergmann #define stat_inc_cp_count(si)				do { } while (0)
3152d66450e7SArnd Bergmann #define stat_inc_bg_cp_count(si)			do { } while (0)
3153d66450e7SArnd Bergmann #define stat_inc_call_count(si)				do { } while (0)
3154d66450e7SArnd Bergmann #define stat_inc_bggc_count(si)				do { } while (0)
3155d66450e7SArnd Bergmann #define stat_inc_dirty_inode(sbi, type)			do { } while (0)
3156d66450e7SArnd Bergmann #define stat_dec_dirty_inode(sbi, type)			do { } while (0)
3157d66450e7SArnd Bergmann #define stat_inc_total_hit(sb)				do { } while (0)
3158d66450e7SArnd Bergmann #define stat_inc_rbtree_node_hit(sb)			do { } while (0)
3159d66450e7SArnd Bergmann #define stat_inc_largest_node_hit(sbi)			do { } while (0)
3160d66450e7SArnd Bergmann #define stat_inc_cached_node_hit(sbi)			do { } while (0)
3161d66450e7SArnd Bergmann #define stat_inc_inline_xattr(inode)			do { } while (0)
3162d66450e7SArnd Bergmann #define stat_dec_inline_xattr(inode)			do { } while (0)
3163d66450e7SArnd Bergmann #define stat_inc_inline_inode(inode)			do { } while (0)
3164d66450e7SArnd Bergmann #define stat_dec_inline_inode(inode)			do { } while (0)
3165d66450e7SArnd Bergmann #define stat_inc_inline_dir(inode)			do { } while (0)
3166d66450e7SArnd Bergmann #define stat_dec_inline_dir(inode)			do { } while (0)
3167d66450e7SArnd Bergmann #define stat_inc_atomic_write(inode)			do { } while (0)
3168d66450e7SArnd Bergmann #define stat_dec_atomic_write(inode)			do { } while (0)
3169d66450e7SArnd Bergmann #define stat_update_max_atomic_write(inode)		do { } while (0)
3170d66450e7SArnd Bergmann #define stat_inc_volatile_write(inode)			do { } while (0)
3171d66450e7SArnd Bergmann #define stat_dec_volatile_write(inode)			do { } while (0)
3172d66450e7SArnd Bergmann #define stat_update_max_volatile_write(inode)		do { } while (0)
3173d66450e7SArnd Bergmann #define stat_inc_seg_type(sbi, curseg)			do { } while (0)
3174d66450e7SArnd Bergmann #define stat_inc_block_count(sbi, curseg)		do { } while (0)
3175d66450e7SArnd Bergmann #define stat_inc_inplace_blocks(sbi)			do { } while (0)
3176d66450e7SArnd Bergmann #define stat_inc_seg_count(sbi, type, gc_type)		do { } while (0)
3177d66450e7SArnd Bergmann #define stat_inc_tot_blk_count(si, blks)		do { } while (0)
3178d66450e7SArnd Bergmann #define stat_inc_data_blk_count(sbi, blks, gc_type)	do { } while (0)
3179d66450e7SArnd Bergmann #define stat_inc_node_blk_count(sbi, blks, gc_type)	do { } while (0)
318039a53e0cSJaegeuk Kim 
318139a53e0cSJaegeuk Kim static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
318239a53e0cSJaegeuk Kim static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
3183787c7b8cSChao Yu static inline int __init f2fs_create_root_stats(void) { return 0; }
31844589d25dSNamjae Jeon static inline void f2fs_destroy_root_stats(void) { }
318539a53e0cSJaegeuk Kim #endif
318639a53e0cSJaegeuk Kim 
318739a53e0cSJaegeuk Kim extern const struct file_operations f2fs_dir_operations;
318839a53e0cSJaegeuk Kim extern const struct file_operations f2fs_file_operations;
318939a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_file_inode_operations;
319039a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_dblock_aops;
319139a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_node_aops;
319239a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_meta_aops;
319339a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_dir_inode_operations;
319439a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_symlink_inode_operations;
3195cbaf042aSJaegeuk Kim extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
319639a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_special_inode_operations;
31974d57b86dSChao Yu extern struct kmem_cache *f2fs_inode_entry_slab;
31981001b347SHuajun Li 
3199e18c65b2SHuajun Li /*
3200e18c65b2SHuajun Li  * inline.c
3201e18c65b2SHuajun Li  */
3202cac5a3d8SDongOh Shin bool f2fs_may_inline_data(struct inode *inode);
3203cac5a3d8SDongOh Shin bool f2fs_may_inline_dentry(struct inode *inode);
32044d57b86dSChao Yu void f2fs_do_read_inline_data(struct page *page, struct page *ipage);
32054d57b86dSChao Yu void f2fs_truncate_inline_inode(struct inode *inode,
32064d57b86dSChao Yu 						struct page *ipage, u64 from);
3207cac5a3d8SDongOh Shin int f2fs_read_inline_data(struct inode *inode, struct page *page);
3208cac5a3d8SDongOh Shin int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
3209cac5a3d8SDongOh Shin int f2fs_convert_inline_inode(struct inode *inode);
3210cac5a3d8SDongOh Shin int f2fs_write_inline_data(struct inode *inode, struct page *page);
32114d57b86dSChao Yu bool f2fs_recover_inline_data(struct inode *inode, struct page *npage);
32124d57b86dSChao Yu struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
3213cac5a3d8SDongOh Shin 			struct fscrypt_name *fname, struct page **res_page);
32144d57b86dSChao Yu int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent,
3215cac5a3d8SDongOh Shin 			struct page *ipage);
3216cac5a3d8SDongOh Shin int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
3217cac5a3d8SDongOh Shin 			const struct qstr *orig_name,
3218cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
32194d57b86dSChao Yu void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry,
32204d57b86dSChao Yu 				struct page *page, struct inode *dir,
32214d57b86dSChao Yu 				struct inode *inode);
3222cac5a3d8SDongOh Shin bool f2fs_empty_inline_dir(struct inode *dir);
3223cac5a3d8SDongOh Shin int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
3224cac5a3d8SDongOh Shin 			struct fscrypt_str *fstr);
3225cac5a3d8SDongOh Shin int f2fs_inline_data_fiemap(struct inode *inode,
3226cac5a3d8SDongOh Shin 			struct fiemap_extent_info *fieinfo,
3227cac5a3d8SDongOh Shin 			__u64 start, __u64 len);
3228cde4de12SJaegeuk Kim 
3229cde4de12SJaegeuk Kim /*
32302658e50dSJaegeuk Kim  * shrinker.c
32312658e50dSJaegeuk Kim  */
3232cac5a3d8SDongOh Shin unsigned long f2fs_shrink_count(struct shrinker *shrink,
3233cac5a3d8SDongOh Shin 			struct shrink_control *sc);
3234cac5a3d8SDongOh Shin unsigned long f2fs_shrink_scan(struct shrinker *shrink,
3235cac5a3d8SDongOh Shin 			struct shrink_control *sc);
3236cac5a3d8SDongOh Shin void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
3237cac5a3d8SDongOh Shin void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
32382658e50dSJaegeuk Kim 
32392658e50dSJaegeuk Kim /*
3240a28ef1f5SChao Yu  * extent_cache.c
3241a28ef1f5SChao Yu  */
32424d57b86dSChao Yu struct rb_entry *f2fs_lookup_rb_tree(struct rb_root *root,
3243004b6862SChao Yu 				struct rb_entry *cached_re, unsigned int ofs);
32444d57b86dSChao Yu struct rb_node **f2fs_lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
3245004b6862SChao Yu 				struct rb_root *root, struct rb_node **parent,
3246004b6862SChao Yu 				unsigned int ofs);
32474d57b86dSChao Yu struct rb_entry *f2fs_lookup_rb_tree_ret(struct rb_root *root,
3248004b6862SChao Yu 		struct rb_entry *cached_re, unsigned int ofs,
3249004b6862SChao Yu 		struct rb_entry **prev_entry, struct rb_entry **next_entry,
3250004b6862SChao Yu 		struct rb_node ***insert_p, struct rb_node **insert_parent,
3251004b6862SChao Yu 		bool force);
32524d57b86dSChao Yu bool f2fs_check_rb_tree_consistence(struct f2fs_sb_info *sbi,
3253df0f6b44SChao Yu 						struct rb_root *root);
3254cac5a3d8SDongOh Shin unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
3255cac5a3d8SDongOh Shin bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext);
3256cac5a3d8SDongOh Shin void f2fs_drop_extent_tree(struct inode *inode);
3257cac5a3d8SDongOh Shin unsigned int f2fs_destroy_extent_node(struct inode *inode);
3258cac5a3d8SDongOh Shin void f2fs_destroy_extent_tree(struct inode *inode);
3259cac5a3d8SDongOh Shin bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
3260cac5a3d8SDongOh Shin 			struct extent_info *ei);
3261cac5a3d8SDongOh Shin void f2fs_update_extent_cache(struct dnode_of_data *dn);
326219b2c30dSChao Yu void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
3263cac5a3d8SDongOh Shin 			pgoff_t fofs, block_t blkaddr, unsigned int len);
32644d57b86dSChao Yu void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi);
32654d57b86dSChao Yu int __init f2fs_create_extent_cache(void);
32664d57b86dSChao Yu void f2fs_destroy_extent_cache(void);
3267a28ef1f5SChao Yu 
3268a28ef1f5SChao Yu /*
32698ceffcb2SChao Yu  * sysfs.c
32708ceffcb2SChao Yu  */
3271dc6b2055SJaegeuk Kim int __init f2fs_init_sysfs(void);
3272dc6b2055SJaegeuk Kim void f2fs_exit_sysfs(void);
3273dc6b2055SJaegeuk Kim int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
3274dc6b2055SJaegeuk Kim void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
32758ceffcb2SChao Yu 
32768ceffcb2SChao Yu /*
3277cde4de12SJaegeuk Kim  * crypto support
3278cde4de12SJaegeuk Kim  */
32790b81d077SJaegeuk Kim static inline bool f2fs_encrypted_inode(struct inode *inode)
3280cde4de12SJaegeuk Kim {
3281cde4de12SJaegeuk Kim 	return file_is_encrypt(inode);
3282cde4de12SJaegeuk Kim }
3283cde4de12SJaegeuk Kim 
32841958593eSJaegeuk Kim static inline bool f2fs_encrypted_file(struct inode *inode)
32851958593eSJaegeuk Kim {
32861958593eSJaegeuk Kim 	return f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode);
32871958593eSJaegeuk Kim }
32881958593eSJaegeuk Kim 
3289cde4de12SJaegeuk Kim static inline void f2fs_set_encrypted_inode(struct inode *inode)
3290cde4de12SJaegeuk Kim {
3291cde4de12SJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
3292cde4de12SJaegeuk Kim 	file_set_encrypt(inode);
32932ee6a576SEric Biggers 	inode->i_flags |= S_ENCRYPTED;
3294cde4de12SJaegeuk Kim #endif
3295cde4de12SJaegeuk Kim }
3296cde4de12SJaegeuk Kim 
32976dbb1796SEric Biggers /*
32986dbb1796SEric Biggers  * Returns true if the reads of the inode's data need to undergo some
32996dbb1796SEric Biggers  * postprocessing step, like decryption or authenticity verification.
33006dbb1796SEric Biggers  */
33016dbb1796SEric Biggers static inline bool f2fs_post_read_required(struct inode *inode)
3302cde4de12SJaegeuk Kim {
33036dbb1796SEric Biggers 	return f2fs_encrypted_file(inode);
3304cde4de12SJaegeuk Kim }
3305cde4de12SJaegeuk Kim 
3306ccd31cb2SSheng Yong #define F2FS_FEATURE_FUNCS(name, flagname) \
3307ccd31cb2SSheng Yong static inline int f2fs_sb_has_##name(struct super_block *sb) \
3308ccd31cb2SSheng Yong { \
3309ccd31cb2SSheng Yong 	return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_##flagname); \
3310cde4de12SJaegeuk Kim }
3311f424f664SJaegeuk Kim 
3312ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(encrypt, ENCRYPT);
3313ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(blkzoned, BLKZONED);
3314ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(extra_attr, EXTRA_ATTR);
3315ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(project_quota, PRJQUOTA);
3316ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_chksum, INODE_CHKSUM);
3317ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
3318ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
3319ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
3320b7c409deSSheng Yong F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
33211c1d35dfSChao Yu 
3322178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
3323178053e2SDamien Le Moal static inline int get_blkz_type(struct f2fs_sb_info *sbi,
33243c62be17SJaegeuk Kim 			struct block_device *bdev, block_t blkaddr)
3325178053e2SDamien Le Moal {
3326178053e2SDamien Le Moal 	unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
33273c62be17SJaegeuk Kim 	int i;
3328178053e2SDamien Le Moal 
33293c62be17SJaegeuk Kim 	for (i = 0; i < sbi->s_ndevs; i++)
33303c62be17SJaegeuk Kim 		if (FDEV(i).bdev == bdev)
33313c62be17SJaegeuk Kim 			return FDEV(i).blkz_type[zno];
33323c62be17SJaegeuk Kim 	return -EINVAL;
3333178053e2SDamien Le Moal }
3334178053e2SDamien Le Moal #endif
3335178053e2SDamien Le Moal 
333696ba2decSDamien Le Moal static inline bool f2fs_discard_en(struct f2fs_sb_info *sbi)
333796ba2decSDamien Le Moal {
333896ba2decSDamien Le Moal 	struct request_queue *q = bdev_get_queue(sbi->sb->s_bdev);
333996ba2decSDamien Le Moal 
3340ccd31cb2SSheng Yong 	return blk_queue_discard(q) || f2fs_sb_has_blkzoned(sbi->sb);
334152763a4bSJaegeuk Kim }
334252763a4bSJaegeuk Kim 
334352763a4bSJaegeuk Kim static inline void set_opt_mode(struct f2fs_sb_info *sbi, unsigned int mt)
334452763a4bSJaegeuk Kim {
334552763a4bSJaegeuk Kim 	clear_opt(sbi, ADAPTIVE);
334652763a4bSJaegeuk Kim 	clear_opt(sbi, LFS);
334752763a4bSJaegeuk Kim 
334852763a4bSJaegeuk Kim 	switch (mt) {
334952763a4bSJaegeuk Kim 	case F2FS_MOUNT_ADAPTIVE:
335052763a4bSJaegeuk Kim 		set_opt(sbi, ADAPTIVE);
335152763a4bSJaegeuk Kim 		break;
335252763a4bSJaegeuk Kim 	case F2FS_MOUNT_LFS:
335352763a4bSJaegeuk Kim 		set_opt(sbi, LFS);
335452763a4bSJaegeuk Kim 		break;
335552763a4bSJaegeuk Kim 	}
335652763a4bSJaegeuk Kim }
335752763a4bSJaegeuk Kim 
3358fcc85a4dSJaegeuk Kim static inline bool f2fs_may_encrypt(struct inode *inode)
3359fcc85a4dSJaegeuk Kim {
3360fcc85a4dSJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
3361886f56f9SAl Viro 	umode_t mode = inode->i_mode;
3362fcc85a4dSJaegeuk Kim 
3363fcc85a4dSJaegeuk Kim 	return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
3364fcc85a4dSJaegeuk Kim #else
3365fcc85a4dSJaegeuk Kim 	return 0;
3366fcc85a4dSJaegeuk Kim #endif
3367fcc85a4dSJaegeuk Kim }
3368fcc85a4dSJaegeuk Kim 
3369b91050a8SHyunchul Lee static inline bool f2fs_force_buffered_io(struct inode *inode, int rw)
3370b91050a8SHyunchul Lee {
33716dbb1796SEric Biggers 	return (f2fs_post_read_required(inode) ||
3372b91050a8SHyunchul Lee 			(rw == WRITE && test_opt(F2FS_I_SB(inode), LFS)) ||
3373b91050a8SHyunchul Lee 			F2FS_I_SB(inode)->s_ndevs);
3374b91050a8SHyunchul Lee }
3375b91050a8SHyunchul Lee 
337639a53e0cSJaegeuk Kim #endif
3377