xref: /openbmc/linux/fs/f2fs/f2fs.h (revision e2374015f27fe5ee5d5c37966e2faf396cdaaa65)
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 */
106983a3bfdbSJaegeuk Kim 	SBI_IS_SHUTDOWN,			/* shutdown by ioctl */
1070caf0047eSChao Yu };
1071caf0047eSChao Yu 
10726beceb54SJaegeuk Kim enum {
10736beceb54SJaegeuk Kim 	CP_TIME,
1074d0239e1bSJaegeuk Kim 	REQ_TIME,
10756beceb54SJaegeuk Kim 	MAX_TIME,
10766beceb54SJaegeuk Kim };
10776beceb54SJaegeuk Kim 
10780cdd3195SHyunchul Lee enum {
10795b0e9539SJaegeuk Kim 	GC_NORMAL,
10805b0e9539SJaegeuk Kim 	GC_IDLE_CB,
10815b0e9539SJaegeuk Kim 	GC_IDLE_GREEDY,
10825b0e9539SJaegeuk Kim 	GC_URGENT,
10835b0e9539SJaegeuk Kim };
10845b0e9539SJaegeuk Kim 
10855b0e9539SJaegeuk Kim enum {
10860cdd3195SHyunchul Lee 	WHINT_MODE_OFF,		/* not pass down write hints */
10870cdd3195SHyunchul Lee 	WHINT_MODE_USER,	/* try to pass down hints given by users */
1088f2e703f9SHyunchul Lee 	WHINT_MODE_FS,		/* pass down hints with F2FS policy */
10890cdd3195SHyunchul Lee };
10900cdd3195SHyunchul Lee 
109107939627SJaegeuk Kim enum {
109207939627SJaegeuk Kim 	ALLOC_MODE_DEFAULT,	/* stay default */
109307939627SJaegeuk Kim 	ALLOC_MODE_REUSE,	/* reuse segments as much as possible */
109407939627SJaegeuk Kim };
109507939627SJaegeuk Kim 
109693cf93f1SJunling Zheng enum fsync_mode {
109793cf93f1SJunling Zheng 	FSYNC_MODE_POSIX,	/* fsync follows posix semantics */
109893cf93f1SJunling Zheng 	FSYNC_MODE_STRICT,	/* fsync behaves in line with ext4 */
1099d6290814SJaegeuk Kim 	FSYNC_MODE_NOBARRIER,	/* fsync behaves nobarrier based on posix */
110093cf93f1SJunling Zheng };
110193cf93f1SJunling Zheng 
1102ff62af20SSheng Yong #ifdef CONFIG_F2FS_FS_ENCRYPTION
1103ff62af20SSheng Yong #define DUMMY_ENCRYPTION_ENABLED(sbi) \
1104ff62af20SSheng Yong 			(unlikely(F2FS_OPTION(sbi).test_dummy_encryption))
1105ff62af20SSheng Yong #else
1106ff62af20SSheng Yong #define DUMMY_ENCRYPTION_ENABLED(sbi) (0)
1107ff62af20SSheng Yong #endif
1108ff62af20SSheng Yong 
110939a53e0cSJaegeuk Kim struct f2fs_sb_info {
111039a53e0cSJaegeuk Kim 	struct super_block *sb;			/* pointer to VFS super block */
11115e176d54SJaegeuk Kim 	struct proc_dir_entry *s_proc;		/* proc entry */
111239a53e0cSJaegeuk Kim 	struct f2fs_super_block *raw_super;	/* raw super block pointer */
1113846ae671SChao Yu 	struct rw_semaphore sb_lock;		/* lock for raw super block */
1114e8240f65SChao Yu 	int valid_super_block;			/* valid super block no */
1115fadb2fb8SChao Yu 	unsigned long s_flag;				/* flags for sbi */
111639a53e0cSJaegeuk Kim 
1117178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
1118178053e2SDamien Le Moal 	unsigned int blocks_per_blkz;		/* F2FS blocks per zone */
1119178053e2SDamien Le Moal 	unsigned int log_blocks_per_blkz;	/* log2 F2FS blocks per zone */
1120178053e2SDamien Le Moal #endif
1121178053e2SDamien Le Moal 
112239a53e0cSJaegeuk Kim 	/* for node-related operations */
112339a53e0cSJaegeuk Kim 	struct f2fs_nm_info *nm_info;		/* node manager */
112439a53e0cSJaegeuk Kim 	struct inode *node_inode;		/* cache node blocks */
112539a53e0cSJaegeuk Kim 
112639a53e0cSJaegeuk Kim 	/* for segment-related operations */
112739a53e0cSJaegeuk Kim 	struct f2fs_sm_info *sm_info;		/* segment manager */
11281ff7bd3bSJaegeuk Kim 
11291ff7bd3bSJaegeuk Kim 	/* for bio operations */
1130a912b54dSJaegeuk Kim 	struct f2fs_bio_info *write_io[NR_PAGE_TYPE];	/* for write bios */
1131e41e6d75SChao Yu 	struct mutex wio_mutex[NR_PAGE_TYPE - 1][NR_TEMP_TYPE];
1132e41e6d75SChao Yu 						/* bio ordering for NODE/DATA */
1133107a805dSChao Yu 	/* keep migration IO order for LFS mode */
1134107a805dSChao Yu 	struct rw_semaphore io_order_lock;
11350a595ebaSJaegeuk Kim 	mempool_t *write_io_dummy;		/* Dummy pages */
113639a53e0cSJaegeuk Kim 
113739a53e0cSJaegeuk Kim 	/* for checkpoint */
113839a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt;		/* raw checkpoint pointer */
11398508e44aSJaegeuk Kim 	int cur_cp_pack;			/* remain current cp pack */
1140aaec2b1dSChao Yu 	spinlock_t cp_lock;			/* for flag in ckpt */
114139a53e0cSJaegeuk Kim 	struct inode *meta_inode;		/* cache meta blocks */
114239936837SJaegeuk Kim 	struct mutex cp_mutex;			/* checkpoint procedure lock */
1143b873b798SJaegeuk Kim 	struct rw_semaphore cp_rwsem;		/* blocking FS operations */
1144b3582c68SChao Yu 	struct rw_semaphore node_write;		/* locking node writes */
114559c9081bSYunlei He 	struct rw_semaphore node_change;	/* locking node change */
1146fb51b5efSChangman Lee 	wait_queue_head_t cp_wait;
11476beceb54SJaegeuk Kim 	unsigned long last_time[MAX_TIME];	/* to store time in jiffies */
11486beceb54SJaegeuk Kim 	long interval_time[MAX_TIME];		/* to store thresholds */
114939a53e0cSJaegeuk Kim 
115067298804SChao Yu 	struct inode_management im[MAX_INO_ENTRY];      /* manage inode cache */
11516451e041SJaegeuk Kim 
11526451e041SJaegeuk Kim 	/* for orphan inode, use 0'th array */
11530d47c1adSGu Zheng 	unsigned int max_orphans;		/* max orphan inodes */
115439a53e0cSJaegeuk Kim 
1155c227f912SChao Yu 	/* for inode management */
1156c227f912SChao Yu 	struct list_head inode_list[NR_INODE_TYPE];	/* dirty inode list */
1157c227f912SChao Yu 	spinlock_t inode_lock[NR_INODE_TYPE];	/* for dirty inode list lock */
115839a53e0cSJaegeuk Kim 
115913054c54SChao Yu 	/* for extent tree cache */
116013054c54SChao Yu 	struct radix_tree_root extent_tree_root;/* cache extent cache entries */
11615e8256acSYunlei He 	struct mutex extent_tree_lock;	/* locking extent radix tree */
116213054c54SChao Yu 	struct list_head extent_list;		/* lru list for shrinker */
116313054c54SChao Yu 	spinlock_t extent_lock;			/* locking extent lru list */
11647441ccefSJaegeuk Kim 	atomic_t total_ext_tree;		/* extent tree count */
1165137d09f0SJaegeuk Kim 	struct list_head zombie_list;		/* extent zombie tree list */
116674fd8d99SJaegeuk Kim 	atomic_t total_zombie_tree;		/* extent zombie tree count */
116713054c54SChao Yu 	atomic_t total_ext_node;		/* extent info count */
116813054c54SChao Yu 
116939a53e0cSJaegeuk Kim 	/* basic filesystem units */
117039a53e0cSJaegeuk Kim 	unsigned int log_sectors_per_block;	/* log2 sectors per block */
117139a53e0cSJaegeuk Kim 	unsigned int log_blocksize;		/* log2 block size */
117239a53e0cSJaegeuk Kim 	unsigned int blocksize;			/* block size */
117339a53e0cSJaegeuk Kim 	unsigned int root_ino_num;		/* root inode number*/
117439a53e0cSJaegeuk Kim 	unsigned int node_ino_num;		/* node inode number*/
117539a53e0cSJaegeuk Kim 	unsigned int meta_ino_num;		/* meta inode number*/
117639a53e0cSJaegeuk Kim 	unsigned int log_blocks_per_seg;	/* log2 blocks per segment */
117739a53e0cSJaegeuk Kim 	unsigned int blocks_per_seg;		/* blocks per segment */
117839a53e0cSJaegeuk Kim 	unsigned int segs_per_sec;		/* segments per section */
117939a53e0cSJaegeuk Kim 	unsigned int secs_per_zone;		/* sections per zone */
118039a53e0cSJaegeuk Kim 	unsigned int total_sections;		/* total section count */
118139a53e0cSJaegeuk Kim 	unsigned int total_node_count;		/* total node block count */
118239a53e0cSJaegeuk Kim 	unsigned int total_valid_node_count;	/* valid node block count */
1183e0afc4d6SChao Yu 	loff_t max_file_blocks;			/* max block index of file */
1184ab9fa662SJaegeuk Kim 	int dir_level;				/* directory level */
1185a2a12b67SChao Yu 	unsigned int trigger_ssr_threshold;	/* threshold to trigger ssr */
1186f6df8f23SSheng Yong 	int readdir_ra;				/* readahead inode in readdir */
118739a53e0cSJaegeuk Kim 
118839a53e0cSJaegeuk Kim 	block_t user_block_count;		/* # of user blocks */
118939a53e0cSJaegeuk Kim 	block_t total_valid_block_count;	/* # of valid blocks */
1190a66cdd98SJaegeuk Kim 	block_t discard_blks;			/* discard command candidats */
119139a53e0cSJaegeuk Kim 	block_t last_valid_block_count;		/* for recovery */
1192daeb433eSChao Yu 	block_t reserved_blocks;		/* configurable reserved blocks */
119380d42145SYunlong Song 	block_t current_reserved_blocks;	/* current reserved blocks */
1194daeb433eSChao Yu 
1195292c196aSChao Yu 	unsigned int nquota_files;		/* # of quota sysfile */
1196292c196aSChao Yu 
119739a53e0cSJaegeuk Kim 	u32 s_next_generation;			/* for NFS support */
1198523be8a6SJaegeuk Kim 
1199523be8a6SJaegeuk Kim 	/* # of pages, see count_type */
120035782b23SJaegeuk Kim 	atomic_t nr_pages[NR_COUNT_TYPE];
120141382ec4SJaegeuk Kim 	/* # of allocated blocks */
120241382ec4SJaegeuk Kim 	struct percpu_counter alloc_valid_block_count;
120339a53e0cSJaegeuk Kim 
1204687de7f1SJaegeuk Kim 	/* writeback control */
1205c29fd0c0SChao Yu 	atomic_t wb_sync_req[META];	/* count # of WB_SYNC threads */
1206687de7f1SJaegeuk Kim 
1207513c5f37SJaegeuk Kim 	/* valid inode count */
1208513c5f37SJaegeuk Kim 	struct percpu_counter total_valid_inode_count;
1209513c5f37SJaegeuk Kim 
121039a53e0cSJaegeuk Kim 	struct f2fs_mount_info mount_opt;	/* mount options */
121139a53e0cSJaegeuk Kim 
121239a53e0cSJaegeuk Kim 	/* for cleaning operations */
121339a53e0cSJaegeuk Kim 	struct mutex gc_mutex;			/* mutex for GC */
121439a53e0cSJaegeuk Kim 	struct f2fs_gc_kthread	*gc_thread;	/* GC thread */
12155ec4e49fSJaegeuk Kim 	unsigned int cur_victim_sec;		/* current victim section num */
12165b0e9539SJaegeuk Kim 	unsigned int gc_mode;			/* current GC state */
12172ef79ecbSChao Yu 	/* for skip statistic */
12182ef79ecbSChao Yu 	unsigned long long skipped_atomic_files[2];	/* FG_GC and BG_GC */
121939a53e0cSJaegeuk Kim 
12201ad71a27SJaegeuk Kim 	/* threshold for gc trials on pinned files */
12211ad71a27SJaegeuk Kim 	u64 gc_pin_file_threshold;
12221ad71a27SJaegeuk Kim 
1223b1c57c1cSJaegeuk Kim 	/* maximum # of trials to find a victim segment for SSR and GC */
1224b1c57c1cSJaegeuk Kim 	unsigned int max_victim_search;
1225b1c57c1cSJaegeuk Kim 
122639a53e0cSJaegeuk Kim 	/*
122739a53e0cSJaegeuk Kim 	 * for stat information.
122839a53e0cSJaegeuk Kim 	 * one is for the LFS mode, and the other is for the SSR mode.
122939a53e0cSJaegeuk Kim 	 */
123035b09d82SNamjae Jeon #ifdef CONFIG_F2FS_STAT_FS
123139a53e0cSJaegeuk Kim 	struct f2fs_stat_info *stat_info;	/* FS status information */
123239a53e0cSJaegeuk Kim 	unsigned int segment_count[2];		/* # of allocated segments */
123339a53e0cSJaegeuk Kim 	unsigned int block_count[2];		/* # of allocated blocks */
1234b9a2c252SChangman Lee 	atomic_t inplace_count;		/* # of inplace update */
12355b7ee374SChao Yu 	atomic64_t total_hit_ext;		/* # of lookup extent cache */
12365b7ee374SChao Yu 	atomic64_t read_hit_rbtree;		/* # of hit rbtree extent node */
12375b7ee374SChao Yu 	atomic64_t read_hit_largest;		/* # of hit largest extent node */
12385b7ee374SChao Yu 	atomic64_t read_hit_cached;		/* # of hit cached extent node */
1239d5e8f6c9SChao Yu 	atomic_t inline_xattr;			/* # of inline_xattr inodes */
124003e14d52SChao Yu 	atomic_t inline_inode;			/* # of inline_data inodes */
124103e14d52SChao Yu 	atomic_t inline_dir;			/* # of inline_dentry inodes */
124226a28a0cSJaegeuk Kim 	atomic_t aw_cnt;			/* # of atomic writes */
1243648d50baSChao Yu 	atomic_t vw_cnt;			/* # of volatile writes */
124426a28a0cSJaegeuk Kim 	atomic_t max_aw_cnt;			/* max # of atomic writes */
1245648d50baSChao Yu 	atomic_t max_vw_cnt;			/* max # of volatile writes */
124639a53e0cSJaegeuk Kim 	int bg_gc;				/* background gc calls */
124733fbd510SChao Yu 	unsigned int ndirty_inode[NR_INODE_TYPE];	/* # of dirty inodes */
124835b09d82SNamjae Jeon #endif
124939a53e0cSJaegeuk Kim 	spinlock_t stat_lock;			/* lock for stat operations */
1250b59d0baeSNamjae Jeon 
1251b0af6d49SChao Yu 	/* For app/fs IO statistics */
1252b0af6d49SChao Yu 	spinlock_t iostat_lock;
1253b0af6d49SChao Yu 	unsigned long long write_iostat[NR_IO_TYPE];
1254b0af6d49SChao Yu 	bool iostat_enable;
1255b0af6d49SChao Yu 
1256b59d0baeSNamjae Jeon 	/* For sysfs suppport */
1257b59d0baeSNamjae Jeon 	struct kobject s_kobj;
1258b59d0baeSNamjae Jeon 	struct completion s_kobj_unregister;
12592658e50dSJaegeuk Kim 
12602658e50dSJaegeuk Kim 	/* For shrinker support */
12612658e50dSJaegeuk Kim 	struct list_head s_list;
12623c62be17SJaegeuk Kim 	int s_ndevs;				/* number of devices */
12633c62be17SJaegeuk Kim 	struct f2fs_dev_info *devs;		/* for device list */
12641228b482SChao Yu 	unsigned int dirty_device;		/* for checkpoint data flush */
12651228b482SChao Yu 	spinlock_t dev_lock;			/* protect dirty_device */
12662658e50dSJaegeuk Kim 	struct mutex umount_mutex;
12672658e50dSJaegeuk Kim 	unsigned int shrinker_run_no;
12688f1dbbbbSShuoran Liu 
12698f1dbbbbSShuoran Liu 	/* For write statistics */
12708f1dbbbbSShuoran Liu 	u64 sectors_written_start;
12718f1dbbbbSShuoran Liu 	u64 kbytes_written;
127243b6573bSKeith Mok 
127343b6573bSKeith Mok 	/* Reference to checksum algorithm driver via cryptoapi */
127443b6573bSKeith Mok 	struct crypto_shash *s_chksum_driver;
12751ecc0c5cSChao Yu 
1276704956ecSChao Yu 	/* Precomputed FS UUID checksum for seeding other checksums */
1277704956ecSChao Yu 	__u32 s_chksum_seed;
127839a53e0cSJaegeuk Kim };
127939a53e0cSJaegeuk Kim 
12801ecc0c5cSChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
128155523519SChao Yu #define f2fs_show_injection_info(type)				\
128255523519SChao Yu 	printk("%sF2FS-fs : inject %s in %s of %pF\n",		\
128355523519SChao Yu 		KERN_INFO, fault_name[type],			\
128455523519SChao Yu 		__func__, __builtin_return_address(0))
12851ecc0c5cSChao Yu static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
12861ecc0c5cSChao Yu {
128763189b78SChao Yu 	struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
12881ecc0c5cSChao Yu 
12891ecc0c5cSChao Yu 	if (!ffi->inject_rate)
12901ecc0c5cSChao Yu 		return false;
12911ecc0c5cSChao Yu 
12921ecc0c5cSChao Yu 	if (!IS_FAULT_SET(ffi, type))
12931ecc0c5cSChao Yu 		return false;
12941ecc0c5cSChao Yu 
12951ecc0c5cSChao Yu 	atomic_inc(&ffi->inject_ops);
12961ecc0c5cSChao Yu 	if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
12971ecc0c5cSChao Yu 		atomic_set(&ffi->inject_ops, 0);
12981ecc0c5cSChao Yu 		return true;
12991ecc0c5cSChao Yu 	}
13001ecc0c5cSChao Yu 	return false;
13011ecc0c5cSChao Yu }
13021ecc0c5cSChao Yu #endif
13031ecc0c5cSChao Yu 
13048f1dbbbbSShuoran Liu /* For write statistics. Suppose sector size is 512 bytes,
13058f1dbbbbSShuoran Liu  * and the return value is in kbytes. s is of struct f2fs_sb_info.
13068f1dbbbbSShuoran Liu  */
13078f1dbbbbSShuoran Liu #define BD_PART_WRITTEN(s)						 \
130868afcf2dSTomohiro Kusumi (((u64)part_stat_read((s)->sb->s_bdev->bd_part, sectors[1]) -		 \
130968afcf2dSTomohiro Kusumi 		(s)->sectors_written_start) >> 1)
13108f1dbbbbSShuoran Liu 
13116beceb54SJaegeuk Kim static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
13126beceb54SJaegeuk Kim {
13136beceb54SJaegeuk Kim 	sbi->last_time[type] = jiffies;
13146beceb54SJaegeuk Kim }
13156beceb54SJaegeuk Kim 
13166beceb54SJaegeuk Kim static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
13176beceb54SJaegeuk Kim {
13186bccfa19SArnd Bergmann 	unsigned long interval = sbi->interval_time[type] * HZ;
13196beceb54SJaegeuk Kim 
13206beceb54SJaegeuk Kim 	return time_after(jiffies, sbi->last_time[type] + interval);
13216beceb54SJaegeuk Kim }
13226beceb54SJaegeuk Kim 
1323d0239e1bSJaegeuk Kim static inline bool is_idle(struct f2fs_sb_info *sbi)
1324d0239e1bSJaegeuk Kim {
1325d0239e1bSJaegeuk Kim 	struct block_device *bdev = sbi->sb->s_bdev;
1326d0239e1bSJaegeuk Kim 	struct request_queue *q = bdev_get_queue(bdev);
1327d0239e1bSJaegeuk Kim 	struct request_list *rl = &q->root_rl;
1328d0239e1bSJaegeuk Kim 
1329d0239e1bSJaegeuk Kim 	if (rl->count[BLK_RW_SYNC] || rl->count[BLK_RW_ASYNC])
1330d0239e1bSJaegeuk Kim 		return 0;
1331d0239e1bSJaegeuk Kim 
1332d0239e1bSJaegeuk Kim 	return f2fs_time_over(sbi, REQ_TIME);
1333d0239e1bSJaegeuk Kim }
1334d0239e1bSJaegeuk Kim 
133539a53e0cSJaegeuk Kim /*
133639a53e0cSJaegeuk Kim  * Inline functions
133739a53e0cSJaegeuk Kim  */
1338416d2dbbSChao Yu static inline u32 __f2fs_crc32(struct f2fs_sb_info *sbi, u32 crc,
1339704956ecSChao Yu 			      const void *address, unsigned int length)
1340704956ecSChao Yu {
1341704956ecSChao Yu 	struct {
1342704956ecSChao Yu 		struct shash_desc shash;
1343704956ecSChao Yu 		char ctx[4];
1344704956ecSChao Yu 	} desc;
1345704956ecSChao Yu 	int err;
1346704956ecSChao Yu 
1347704956ecSChao Yu 	BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver) != sizeof(desc.ctx));
1348704956ecSChao Yu 
1349704956ecSChao Yu 	desc.shash.tfm = sbi->s_chksum_driver;
1350704956ecSChao Yu 	desc.shash.flags = 0;
1351704956ecSChao Yu 	*(u32 *)desc.ctx = crc;
1352704956ecSChao Yu 
1353704956ecSChao Yu 	err = crypto_shash_update(&desc.shash, address, length);
1354704956ecSChao Yu 	BUG_ON(err);
1355704956ecSChao Yu 
1356704956ecSChao Yu 	return *(u32 *)desc.ctx;
1357704956ecSChao Yu }
1358704956ecSChao Yu 
1359416d2dbbSChao Yu static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
1360416d2dbbSChao Yu 			   unsigned int length)
1361416d2dbbSChao Yu {
1362416d2dbbSChao Yu 	return __f2fs_crc32(sbi, F2FS_SUPER_MAGIC, address, length);
1363416d2dbbSChao Yu }
1364416d2dbbSChao Yu 
1365416d2dbbSChao Yu static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
1366416d2dbbSChao Yu 				  void *buf, size_t buf_size)
1367416d2dbbSChao Yu {
1368416d2dbbSChao Yu 	return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
1369416d2dbbSChao Yu }
1370416d2dbbSChao Yu 
1371416d2dbbSChao Yu static inline u32 f2fs_chksum(struct f2fs_sb_info *sbi, u32 crc,
1372416d2dbbSChao Yu 			      const void *address, unsigned int length)
1373416d2dbbSChao Yu {
1374416d2dbbSChao Yu 	return __f2fs_crc32(sbi, crc, address, length);
1375416d2dbbSChao Yu }
1376416d2dbbSChao Yu 
137739a53e0cSJaegeuk Kim static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
137839a53e0cSJaegeuk Kim {
137939a53e0cSJaegeuk Kim 	return container_of(inode, struct f2fs_inode_info, vfs_inode);
138039a53e0cSJaegeuk Kim }
138139a53e0cSJaegeuk Kim 
138239a53e0cSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
138339a53e0cSJaegeuk Kim {
138439a53e0cSJaegeuk Kim 	return sb->s_fs_info;
138539a53e0cSJaegeuk Kim }
138639a53e0cSJaegeuk Kim 
13874081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
13884081363fSJaegeuk Kim {
13894081363fSJaegeuk Kim 	return F2FS_SB(inode->i_sb);
13904081363fSJaegeuk Kim }
13914081363fSJaegeuk Kim 
13924081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
13934081363fSJaegeuk Kim {
13944081363fSJaegeuk Kim 	return F2FS_I_SB(mapping->host);
13954081363fSJaegeuk Kim }
13964081363fSJaegeuk Kim 
13974081363fSJaegeuk Kim static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
13984081363fSJaegeuk Kim {
13994081363fSJaegeuk Kim 	return F2FS_M_SB(page->mapping);
14004081363fSJaegeuk Kim }
14014081363fSJaegeuk Kim 
140239a53e0cSJaegeuk Kim static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
140339a53e0cSJaegeuk Kim {
140439a53e0cSJaegeuk Kim 	return (struct f2fs_super_block *)(sbi->raw_super);
140539a53e0cSJaegeuk Kim }
140639a53e0cSJaegeuk Kim 
140739a53e0cSJaegeuk Kim static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
140839a53e0cSJaegeuk Kim {
140939a53e0cSJaegeuk Kim 	return (struct f2fs_checkpoint *)(sbi->ckpt);
141039a53e0cSJaegeuk Kim }
141139a53e0cSJaegeuk Kim 
141245590710SGu Zheng static inline struct f2fs_node *F2FS_NODE(struct page *page)
141345590710SGu Zheng {
141445590710SGu Zheng 	return (struct f2fs_node *)page_address(page);
141545590710SGu Zheng }
141645590710SGu Zheng 
141758bfaf44SJaegeuk Kim static inline struct f2fs_inode *F2FS_INODE(struct page *page)
141858bfaf44SJaegeuk Kim {
141958bfaf44SJaegeuk Kim 	return &((struct f2fs_node *)page_address(page))->i;
142058bfaf44SJaegeuk Kim }
142158bfaf44SJaegeuk Kim 
142239a53e0cSJaegeuk Kim static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
142339a53e0cSJaegeuk Kim {
142439a53e0cSJaegeuk Kim 	return (struct f2fs_nm_info *)(sbi->nm_info);
142539a53e0cSJaegeuk Kim }
142639a53e0cSJaegeuk Kim 
142739a53e0cSJaegeuk Kim static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
142839a53e0cSJaegeuk Kim {
142939a53e0cSJaegeuk Kim 	return (struct f2fs_sm_info *)(sbi->sm_info);
143039a53e0cSJaegeuk Kim }
143139a53e0cSJaegeuk Kim 
143239a53e0cSJaegeuk Kim static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
143339a53e0cSJaegeuk Kim {
143439a53e0cSJaegeuk Kim 	return (struct sit_info *)(SM_I(sbi)->sit_info);
143539a53e0cSJaegeuk Kim }
143639a53e0cSJaegeuk Kim 
143739a53e0cSJaegeuk Kim static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
143839a53e0cSJaegeuk Kim {
143939a53e0cSJaegeuk Kim 	return (struct free_segmap_info *)(SM_I(sbi)->free_info);
144039a53e0cSJaegeuk Kim }
144139a53e0cSJaegeuk Kim 
144239a53e0cSJaegeuk Kim static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
144339a53e0cSJaegeuk Kim {
144439a53e0cSJaegeuk Kim 	return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
144539a53e0cSJaegeuk Kim }
144639a53e0cSJaegeuk Kim 
14479df27d98SGu Zheng static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
14489df27d98SGu Zheng {
14499df27d98SGu Zheng 	return sbi->meta_inode->i_mapping;
14509df27d98SGu Zheng }
14519df27d98SGu Zheng 
14524ef51a8fSJaegeuk Kim static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
14534ef51a8fSJaegeuk Kim {
14544ef51a8fSJaegeuk Kim 	return sbi->node_inode->i_mapping;
14554ef51a8fSJaegeuk Kim }
14564ef51a8fSJaegeuk Kim 
1457caf0047eSChao Yu static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
145839a53e0cSJaegeuk Kim {
1459fadb2fb8SChao Yu 	return test_bit(type, &sbi->s_flag);
146039a53e0cSJaegeuk Kim }
146139a53e0cSJaegeuk Kim 
1462caf0047eSChao Yu static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
146339a53e0cSJaegeuk Kim {
1464fadb2fb8SChao Yu 	set_bit(type, &sbi->s_flag);
1465caf0047eSChao Yu }
1466caf0047eSChao Yu 
1467caf0047eSChao Yu static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
1468caf0047eSChao Yu {
1469fadb2fb8SChao Yu 	clear_bit(type, &sbi->s_flag);
147039a53e0cSJaegeuk Kim }
147139a53e0cSJaegeuk Kim 
1472d71b5564SJaegeuk Kim static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
1473d71b5564SJaegeuk Kim {
1474d71b5564SJaegeuk Kim 	return le64_to_cpu(cp->checkpoint_ver);
1475d71b5564SJaegeuk Kim }
1476d71b5564SJaegeuk Kim 
1477ea676733SJaegeuk Kim static inline unsigned long f2fs_qf_ino(struct super_block *sb, int type)
1478ea676733SJaegeuk Kim {
1479ea676733SJaegeuk Kim 	if (type < F2FS_MAX_QUOTAS)
1480ea676733SJaegeuk Kim 		return le32_to_cpu(F2FS_SB(sb)->raw_super->qf_ino[type]);
1481ea676733SJaegeuk Kim 	return 0;
1482ea676733SJaegeuk Kim }
1483ea676733SJaegeuk Kim 
1484ced2c7eaSKinglong Mee static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
1485ced2c7eaSKinglong Mee {
1486ced2c7eaSKinglong Mee 	size_t crc_offset = le32_to_cpu(cp->checksum_offset);
1487ced2c7eaSKinglong Mee 	return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
1488ced2c7eaSKinglong Mee }
1489ced2c7eaSKinglong Mee 
1490aaec2b1dSChao Yu static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
149125ca923bSJaegeuk Kim {
149225ca923bSJaegeuk Kim 	unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
1493aaec2b1dSChao Yu 
149425ca923bSJaegeuk Kim 	return ckpt_flags & f;
149525ca923bSJaegeuk Kim }
149625ca923bSJaegeuk Kim 
1497aaec2b1dSChao Yu static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
149825ca923bSJaegeuk Kim {
1499aaec2b1dSChao Yu 	return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
1500aaec2b1dSChao Yu }
1501aaec2b1dSChao Yu 
1502aaec2b1dSChao Yu static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1503aaec2b1dSChao Yu {
1504aaec2b1dSChao Yu 	unsigned int ckpt_flags;
1505aaec2b1dSChao Yu 
1506aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
150725ca923bSJaegeuk Kim 	ckpt_flags |= f;
150825ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
150925ca923bSJaegeuk Kim }
151025ca923bSJaegeuk Kim 
1511aaec2b1dSChao Yu static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
151225ca923bSJaegeuk Kim {
1513d1aa2453SChao Yu 	unsigned long flags;
1514d1aa2453SChao Yu 
1515d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1516aaec2b1dSChao Yu 	__set_ckpt_flags(F2FS_CKPT(sbi), f);
1517d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1518aaec2b1dSChao Yu }
1519aaec2b1dSChao Yu 
1520aaec2b1dSChao Yu static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
1521aaec2b1dSChao Yu {
1522aaec2b1dSChao Yu 	unsigned int ckpt_flags;
1523aaec2b1dSChao Yu 
1524aaec2b1dSChao Yu 	ckpt_flags = le32_to_cpu(cp->ckpt_flags);
152525ca923bSJaegeuk Kim 	ckpt_flags &= (~f);
152625ca923bSJaegeuk Kim 	cp->ckpt_flags = cpu_to_le32(ckpt_flags);
152725ca923bSJaegeuk Kim }
152825ca923bSJaegeuk Kim 
1529aaec2b1dSChao Yu static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
1530aaec2b1dSChao Yu {
1531d1aa2453SChao Yu 	unsigned long flags;
1532d1aa2453SChao Yu 
1533d1aa2453SChao Yu 	spin_lock_irqsave(&sbi->cp_lock, flags);
1534aaec2b1dSChao Yu 	__clear_ckpt_flags(F2FS_CKPT(sbi), f);
1535d1aa2453SChao Yu 	spin_unlock_irqrestore(&sbi->cp_lock, flags);
1536aaec2b1dSChao Yu }
1537aaec2b1dSChao Yu 
153822ad0b6aSJaegeuk Kim static inline void disable_nat_bits(struct f2fs_sb_info *sbi, bool lock)
153922ad0b6aSJaegeuk Kim {
1540d1aa2453SChao Yu 	unsigned long flags;
1541d1aa2453SChao Yu 
154222ad0b6aSJaegeuk Kim 	set_sbi_flag(sbi, SBI_NEED_FSCK);
154322ad0b6aSJaegeuk Kim 
154422ad0b6aSJaegeuk Kim 	if (lock)
1545d1aa2453SChao Yu 		spin_lock_irqsave(&sbi->cp_lock, flags);
154622ad0b6aSJaegeuk Kim 	__clear_ckpt_flags(F2FS_CKPT(sbi), CP_NAT_BITS_FLAG);
154722ad0b6aSJaegeuk Kim 	kfree(NM_I(sbi)->nat_bits);
154822ad0b6aSJaegeuk Kim 	NM_I(sbi)->nat_bits = NULL;
154922ad0b6aSJaegeuk Kim 	if (lock)
1550d1aa2453SChao Yu 		spin_unlock_irqrestore(&sbi->cp_lock, flags);
155122ad0b6aSJaegeuk Kim }
155222ad0b6aSJaegeuk Kim 
155322ad0b6aSJaegeuk Kim static inline bool enabled_nat_bits(struct f2fs_sb_info *sbi,
155422ad0b6aSJaegeuk Kim 					struct cp_control *cpc)
155522ad0b6aSJaegeuk Kim {
155622ad0b6aSJaegeuk Kim 	bool set = is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
155722ad0b6aSJaegeuk Kim 
1558c473f1a9SChao Yu 	return (cpc) ? (cpc->reason & CP_UMOUNT) && set : set;
155922ad0b6aSJaegeuk Kim }
156022ad0b6aSJaegeuk Kim 
1561e479556bSGu Zheng static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
156239a53e0cSJaegeuk Kim {
1563b873b798SJaegeuk Kim 	down_read(&sbi->cp_rwsem);
156439a53e0cSJaegeuk Kim }
156539a53e0cSJaegeuk Kim 
1566cc15620bSJaegeuk Kim static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi)
1567cc15620bSJaegeuk Kim {
1568cc15620bSJaegeuk Kim 	return down_read_trylock(&sbi->cp_rwsem);
1569cc15620bSJaegeuk Kim }
1570cc15620bSJaegeuk Kim 
1571e479556bSGu Zheng static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
157239a53e0cSJaegeuk Kim {
1573b873b798SJaegeuk Kim 	up_read(&sbi->cp_rwsem);
157439936837SJaegeuk Kim }
157539936837SJaegeuk Kim 
1576e479556bSGu Zheng static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
157739936837SJaegeuk Kim {
1578b873b798SJaegeuk Kim 	down_write(&sbi->cp_rwsem);
157939936837SJaegeuk Kim }
158039936837SJaegeuk Kim 
1581e479556bSGu Zheng static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
158239936837SJaegeuk Kim {
1583b873b798SJaegeuk Kim 	up_write(&sbi->cp_rwsem);
158439a53e0cSJaegeuk Kim }
158539a53e0cSJaegeuk Kim 
1586119ee914SJaegeuk Kim static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
1587119ee914SJaegeuk Kim {
1588119ee914SJaegeuk Kim 	int reason = CP_SYNC;
1589119ee914SJaegeuk Kim 
1590119ee914SJaegeuk Kim 	if (test_opt(sbi, FASTBOOT))
1591119ee914SJaegeuk Kim 		reason = CP_FASTBOOT;
1592119ee914SJaegeuk Kim 	if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
1593119ee914SJaegeuk Kim 		reason = CP_UMOUNT;
1594119ee914SJaegeuk Kim 	return reason;
1595119ee914SJaegeuk Kim }
1596119ee914SJaegeuk Kim 
1597119ee914SJaegeuk Kim static inline bool __remain_node_summaries(int reason)
1598119ee914SJaegeuk Kim {
1599c473f1a9SChao Yu 	return (reason & (CP_UMOUNT | CP_FASTBOOT));
1600119ee914SJaegeuk Kim }
1601119ee914SJaegeuk Kim 
1602119ee914SJaegeuk Kim static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
1603119ee914SJaegeuk Kim {
1604aaec2b1dSChao Yu 	return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
1605aaec2b1dSChao Yu 			is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
1606119ee914SJaegeuk Kim }
1607119ee914SJaegeuk Kim 
160839a53e0cSJaegeuk Kim /*
160939a53e0cSJaegeuk Kim  * Check whether the inode has blocks or not
161039a53e0cSJaegeuk Kim  */
161139a53e0cSJaegeuk Kim static inline int F2FS_HAS_BLOCKS(struct inode *inode)
161239a53e0cSJaegeuk Kim {
16130eb0adadSChao Yu 	block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
16140eb0adadSChao Yu 
1615000519f2SChao Yu 	return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block;
161639a53e0cSJaegeuk Kim }
161739a53e0cSJaegeuk Kim 
16184bc8e9bcSChao Yu static inline bool f2fs_has_xattr_block(unsigned int ofs)
16194bc8e9bcSChao Yu {
16204bc8e9bcSChao Yu 	return ofs == XATTR_NODE_OFFSET;
16214bc8e9bcSChao Yu }
16224bc8e9bcSChao Yu 
1623d8a9a229SJaegeuk Kim static inline bool __allow_reserved_blocks(struct f2fs_sb_info *sbi,
1624a90a0884SJaegeuk Kim 					struct inode *inode, bool cap)
16257c2e5963SJaegeuk Kim {
1626d8a9a229SJaegeuk Kim 	if (!inode)
1627d8a9a229SJaegeuk Kim 		return true;
16287c2e5963SJaegeuk Kim 	if (!test_opt(sbi, RESERVE_ROOT))
16297c2e5963SJaegeuk Kim 		return false;
1630d8a9a229SJaegeuk Kim 	if (IS_NOQUOTA(inode))
1631d8a9a229SJaegeuk Kim 		return true;
163263189b78SChao Yu 	if (uid_eq(F2FS_OPTION(sbi).s_resuid, current_fsuid()))
16337c2e5963SJaegeuk Kim 		return true;
163463189b78SChao Yu 	if (!gid_eq(F2FS_OPTION(sbi).s_resgid, GLOBAL_ROOT_GID) &&
163563189b78SChao Yu 					in_group_p(F2FS_OPTION(sbi).s_resgid))
16367c2e5963SJaegeuk Kim 		return true;
1637a90a0884SJaegeuk Kim 	if (cap && capable(CAP_SYS_RESOURCE))
1638162b27aeSJaegeuk Kim 		return true;
16397c2e5963SJaegeuk Kim 	return false;
16407c2e5963SJaegeuk Kim }
16417c2e5963SJaegeuk Kim 
16420abd675eSChao Yu static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool);
16430abd675eSChao Yu static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
164446008c6dSChao Yu 				 struct inode *inode, blkcnt_t *count)
164539a53e0cSJaegeuk Kim {
16460abd675eSChao Yu 	blkcnt_t diff = 0, release = 0;
1647daeb433eSChao Yu 	block_t avail_user_block_count;
16480abd675eSChao Yu 	int ret;
16490abd675eSChao Yu 
16500abd675eSChao Yu 	ret = dquot_reserve_block(inode, *count);
16510abd675eSChao Yu 	if (ret)
16520abd675eSChao Yu 		return ret;
1653dd11a5dfSJaegeuk Kim 
1654cb78942bSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
165555523519SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
165655523519SChao Yu 		f2fs_show_injection_info(FAULT_BLOCK);
16570abd675eSChao Yu 		release = *count;
16580abd675eSChao Yu 		goto enospc;
165955523519SChao Yu 	}
1660cb78942bSJaegeuk Kim #endif
1661dd11a5dfSJaegeuk Kim 	/*
1662dd11a5dfSJaegeuk Kim 	 * let's increase this in prior to actual block count change in order
1663dd11a5dfSJaegeuk Kim 	 * for f2fs_sync_file to avoid data races when deciding checkpoint.
1664dd11a5dfSJaegeuk Kim 	 */
1665dd11a5dfSJaegeuk Kim 	percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
1666cfb271d4SChao Yu 
166739a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
16682555a2d5SJaegeuk Kim 	sbi->total_valid_block_count += (block_t)(*count);
166980d42145SYunlong Song 	avail_user_block_count = sbi->user_block_count -
167080d42145SYunlong Song 					sbi->current_reserved_blocks;
16717e65be49SJaegeuk Kim 
1672a90a0884SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode, true))
167363189b78SChao Yu 		avail_user_block_count -= F2FS_OPTION(sbi).root_reserved_blocks;
16747e65be49SJaegeuk Kim 
1675daeb433eSChao Yu 	if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
1676daeb433eSChao Yu 		diff = sbi->total_valid_block_count - avail_user_block_count;
16777e65be49SJaegeuk Kim 		if (diff > *count)
16787e65be49SJaegeuk Kim 			diff = *count;
1679dd11a5dfSJaegeuk Kim 		*count -= diff;
16800abd675eSChao Yu 		release = diff;
16817e65be49SJaegeuk Kim 		sbi->total_valid_block_count -= diff;
168246008c6dSChao Yu 		if (!*count) {
168339a53e0cSJaegeuk Kim 			spin_unlock(&sbi->stat_lock);
1684dd11a5dfSJaegeuk Kim 			percpu_counter_sub(&sbi->alloc_valid_block_count, diff);
16850abd675eSChao Yu 			goto enospc;
168639a53e0cSJaegeuk Kim 		}
168746008c6dSChao Yu 	}
168839a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
168941382ec4SJaegeuk Kim 
1690fab2adeeSLiFan 	if (unlikely(release))
16910abd675eSChao Yu 		dquot_release_reservation_block(inode, release);
16920abd675eSChao Yu 	f2fs_i_blocks_write(inode, *count, true, true);
16930abd675eSChao Yu 	return 0;
16940abd675eSChao Yu 
16950abd675eSChao Yu enospc:
16960abd675eSChao Yu 	dquot_release_reservation_block(inode, release);
16970abd675eSChao Yu 	return -ENOSPC;
169839a53e0cSJaegeuk Kim }
169939a53e0cSJaegeuk Kim 
1700da19b0dcSGu Zheng static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
170139a53e0cSJaegeuk Kim 						struct inode *inode,
17020eb0adadSChao Yu 						block_t count)
170339a53e0cSJaegeuk Kim {
17040eb0adadSChao Yu 	blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK;
17050eb0adadSChao Yu 
170639a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
17079850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
17080eb0adadSChao Yu 	f2fs_bug_on(sbi, inode->i_blocks < sectors);
170939a53e0cSJaegeuk Kim 	sbi->total_valid_block_count -= (block_t)count;
171080d42145SYunlong Song 	if (sbi->reserved_blocks &&
171180d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
171280d42145SYunlong Song 		sbi->current_reserved_blocks = min(sbi->reserved_blocks,
171380d42145SYunlong Song 					sbi->current_reserved_blocks + count);
171439a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
17150abd675eSChao Yu 	f2fs_i_blocks_write(inode, count, false, true);
171639a53e0cSJaegeuk Kim }
171739a53e0cSJaegeuk Kim 
171839a53e0cSJaegeuk Kim static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
171939a53e0cSJaegeuk Kim {
172035782b23SJaegeuk Kim 	atomic_inc(&sbi->nr_pages[count_type]);
17217c4abcbeSChao Yu 
172236951b38SChao Yu 	if (count_type == F2FS_DIRTY_DATA || count_type == F2FS_INMEM_PAGES ||
172336951b38SChao Yu 		count_type == F2FS_WB_CP_DATA || count_type == F2FS_WB_DATA)
17247c4abcbeSChao Yu 		return;
17257c4abcbeSChao Yu 
1726caf0047eSChao Yu 	set_sbi_flag(sbi, SBI_IS_DIRTY);
172739a53e0cSJaegeuk Kim }
172839a53e0cSJaegeuk Kim 
1729a7ffdbe2SJaegeuk Kim static inline void inode_inc_dirty_pages(struct inode *inode)
173039a53e0cSJaegeuk Kim {
1731204706c7SJaegeuk Kim 	atomic_inc(&F2FS_I(inode)->dirty_pages);
1732c227f912SChao Yu 	inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1733c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
17342c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
17352c8a4a28SJaegeuk Kim 		inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
173639a53e0cSJaegeuk Kim }
173739a53e0cSJaegeuk Kim 
173839a53e0cSJaegeuk Kim static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
173939a53e0cSJaegeuk Kim {
174035782b23SJaegeuk Kim 	atomic_dec(&sbi->nr_pages[count_type]);
174139a53e0cSJaegeuk Kim }
174239a53e0cSJaegeuk Kim 
1743a7ffdbe2SJaegeuk Kim static inline void inode_dec_dirty_pages(struct inode *inode)
174439a53e0cSJaegeuk Kim {
17455ac9f36fSChao Yu 	if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
17465ac9f36fSChao Yu 			!S_ISLNK(inode->i_mode))
17471fe54f9dSJaegeuk Kim 		return;
17481fe54f9dSJaegeuk Kim 
1749204706c7SJaegeuk Kim 	atomic_dec(&F2FS_I(inode)->dirty_pages);
1750c227f912SChao Yu 	dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
1751c227f912SChao Yu 				F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
17522c8a4a28SJaegeuk Kim 	if (IS_NOQUOTA(inode))
17532c8a4a28SJaegeuk Kim 		dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
175439a53e0cSJaegeuk Kim }
175539a53e0cSJaegeuk Kim 
1756523be8a6SJaegeuk Kim static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
175739a53e0cSJaegeuk Kim {
175835782b23SJaegeuk Kim 	return atomic_read(&sbi->nr_pages[count_type]);
175939a53e0cSJaegeuk Kim }
176039a53e0cSJaegeuk Kim 
1761204706c7SJaegeuk Kim static inline int get_dirty_pages(struct inode *inode)
1762f8b2c1f9SJaegeuk Kim {
1763204706c7SJaegeuk Kim 	return atomic_read(&F2FS_I(inode)->dirty_pages);
1764f8b2c1f9SJaegeuk Kim }
1765f8b2c1f9SJaegeuk Kim 
17665ac206cfSNamjae Jeon static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
17675ac206cfSNamjae Jeon {
17683519e3f9SChao Yu 	unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
1769523be8a6SJaegeuk Kim 	unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
1770523be8a6SJaegeuk Kim 						sbi->log_blocks_per_seg;
1771523be8a6SJaegeuk Kim 
1772523be8a6SJaegeuk Kim 	return segs / sbi->segs_per_sec;
17735ac206cfSNamjae Jeon }
17745ac206cfSNamjae Jeon 
177539a53e0cSJaegeuk Kim static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
177639a53e0cSJaegeuk Kim {
17778b8343faSJaegeuk Kim 	return sbi->total_valid_block_count;
177839a53e0cSJaegeuk Kim }
177939a53e0cSJaegeuk Kim 
1780f83a2584SYunlei He static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
1781f83a2584SYunlei He {
1782f83a2584SYunlei He 	return sbi->discard_blks;
1783f83a2584SYunlei He }
1784f83a2584SYunlei He 
178539a53e0cSJaegeuk Kim static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
178639a53e0cSJaegeuk Kim {
178739a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
178839a53e0cSJaegeuk Kim 
178939a53e0cSJaegeuk Kim 	/* return NAT or SIT bitmap */
179039a53e0cSJaegeuk Kim 	if (flag == NAT_BITMAP)
179139a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
179239a53e0cSJaegeuk Kim 	else if (flag == SIT_BITMAP)
179339a53e0cSJaegeuk Kim 		return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
179439a53e0cSJaegeuk Kim 
179539a53e0cSJaegeuk Kim 	return 0;
179639a53e0cSJaegeuk Kim }
179739a53e0cSJaegeuk Kim 
179855141486SWanpeng Li static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
179955141486SWanpeng Li {
180055141486SWanpeng Li 	return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
180155141486SWanpeng Li }
180255141486SWanpeng Li 
180339a53e0cSJaegeuk Kim static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
180439a53e0cSJaegeuk Kim {
180539a53e0cSJaegeuk Kim 	struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
18061dbe4152SChangman Lee 	int offset;
18071dbe4152SChangman Lee 
1808199bc3feSChao Yu 	if (is_set_ckpt_flags(sbi, CP_LARGE_NAT_BITMAP_FLAG)) {
1809199bc3feSChao Yu 		offset = (flag == SIT_BITMAP) ?
1810199bc3feSChao Yu 			le32_to_cpu(ckpt->nat_ver_bitmap_bytesize) : 0;
1811199bc3feSChao Yu 		return &ckpt->sit_nat_version_bitmap + offset;
1812199bc3feSChao Yu 	}
1813199bc3feSChao Yu 
181455141486SWanpeng Li 	if (__cp_payload(sbi) > 0) {
18151dbe4152SChangman Lee 		if (flag == NAT_BITMAP)
18161dbe4152SChangman Lee 			return &ckpt->sit_nat_version_bitmap;
18171dbe4152SChangman Lee 		else
181865b85cccSJaegeuk Kim 			return (unsigned char *)ckpt + F2FS_BLKSIZE;
18191dbe4152SChangman Lee 	} else {
18201dbe4152SChangman Lee 		offset = (flag == NAT_BITMAP) ?
182125ca923bSJaegeuk Kim 			le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
182239a53e0cSJaegeuk Kim 		return &ckpt->sit_nat_version_bitmap + offset;
182339a53e0cSJaegeuk Kim 	}
18241dbe4152SChangman Lee }
182539a53e0cSJaegeuk Kim 
182639a53e0cSJaegeuk Kim static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
182739a53e0cSJaegeuk Kim {
18288508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
182939a53e0cSJaegeuk Kim 
18308508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 2)
183139a53e0cSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
183239a53e0cSJaegeuk Kim 	return start_addr;
183339a53e0cSJaegeuk Kim }
183439a53e0cSJaegeuk Kim 
18358508e44aSJaegeuk Kim static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
18368508e44aSJaegeuk Kim {
18378508e44aSJaegeuk Kim 	block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
18388508e44aSJaegeuk Kim 
18398508e44aSJaegeuk Kim 	if (sbi->cur_cp_pack == 1)
18408508e44aSJaegeuk Kim 		start_addr += sbi->blocks_per_seg;
18418508e44aSJaegeuk Kim 	return start_addr;
18428508e44aSJaegeuk Kim }
18438508e44aSJaegeuk Kim 
18448508e44aSJaegeuk Kim static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
18458508e44aSJaegeuk Kim {
18468508e44aSJaegeuk Kim 	sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
18478508e44aSJaegeuk Kim }
18488508e44aSJaegeuk Kim 
184939a53e0cSJaegeuk Kim static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
185039a53e0cSJaegeuk Kim {
185139a53e0cSJaegeuk Kim 	return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
185239a53e0cSJaegeuk Kim }
185339a53e0cSJaegeuk Kim 
18540abd675eSChao Yu static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
1855000519f2SChao Yu 					struct inode *inode, bool is_inode)
185639a53e0cSJaegeuk Kim {
185739a53e0cSJaegeuk Kim 	block_t	valid_block_count;
185839a53e0cSJaegeuk Kim 	unsigned int valid_node_count;
18590abd675eSChao Yu 	bool quota = inode && !is_inode;
18600abd675eSChao Yu 
18610abd675eSChao Yu 	if (quota) {
18620abd675eSChao Yu 		int ret = dquot_reserve_block(inode, 1);
18630abd675eSChao Yu 		if (ret)
18640abd675eSChao Yu 			return ret;
18650abd675eSChao Yu 	}
186639a53e0cSJaegeuk Kim 
1867812c6056SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
1868812c6056SChao Yu 	if (time_to_inject(sbi, FAULT_BLOCK)) {
1869812c6056SChao Yu 		f2fs_show_injection_info(FAULT_BLOCK);
1870812c6056SChao Yu 		goto enospc;
1871812c6056SChao Yu 	}
1872812c6056SChao Yu #endif
1873812c6056SChao Yu 
187439a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
187539a53e0cSJaegeuk Kim 
18767e65be49SJaegeuk Kim 	valid_block_count = sbi->total_valid_block_count +
18777e65be49SJaegeuk Kim 					sbi->current_reserved_blocks + 1;
18787e65be49SJaegeuk Kim 
1879a90a0884SJaegeuk Kim 	if (!__allow_reserved_blocks(sbi, inode, false))
188063189b78SChao Yu 		valid_block_count += F2FS_OPTION(sbi).root_reserved_blocks;
18817e65be49SJaegeuk Kim 
18827e65be49SJaegeuk Kim 	if (unlikely(valid_block_count > sbi->user_block_count)) {
188339a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
18840abd675eSChao Yu 		goto enospc;
188539a53e0cSJaegeuk Kim 	}
188639a53e0cSJaegeuk Kim 
1887ef86d709SGu Zheng 	valid_node_count = sbi->total_valid_node_count + 1;
1888cfb271d4SChao Yu 	if (unlikely(valid_node_count > sbi->total_node_count)) {
188939a53e0cSJaegeuk Kim 		spin_unlock(&sbi->stat_lock);
18900abd675eSChao Yu 		goto enospc;
189139a53e0cSJaegeuk Kim 	}
189239a53e0cSJaegeuk Kim 
1893ef86d709SGu Zheng 	sbi->total_valid_node_count++;
1894ef86d709SGu Zheng 	sbi->total_valid_block_count++;
189539a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
189639a53e0cSJaegeuk Kim 
18970abd675eSChao Yu 	if (inode) {
18980abd675eSChao Yu 		if (is_inode)
18990abd675eSChao Yu 			f2fs_mark_inode_dirty_sync(inode, true);
19000abd675eSChao Yu 		else
19010abd675eSChao Yu 			f2fs_i_blocks_write(inode, 1, true, true);
19020abd675eSChao Yu 	}
19030abd675eSChao Yu 
190441382ec4SJaegeuk Kim 	percpu_counter_inc(&sbi->alloc_valid_block_count);
19050abd675eSChao Yu 	return 0;
19060abd675eSChao Yu 
19070abd675eSChao Yu enospc:
19080abd675eSChao Yu 	if (quota)
19090abd675eSChao Yu 		dquot_release_reservation_block(inode, 1);
19100abd675eSChao Yu 	return -ENOSPC;
191139a53e0cSJaegeuk Kim }
191239a53e0cSJaegeuk Kim 
191339a53e0cSJaegeuk Kim static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
1914000519f2SChao Yu 					struct inode *inode, bool is_inode)
191539a53e0cSJaegeuk Kim {
191639a53e0cSJaegeuk Kim 	spin_lock(&sbi->stat_lock);
191739a53e0cSJaegeuk Kim 
19189850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_block_count);
19199850cf4aSJaegeuk Kim 	f2fs_bug_on(sbi, !sbi->total_valid_node_count);
1920000519f2SChao Yu 	f2fs_bug_on(sbi, !is_inode && !inode->i_blocks);
192139a53e0cSJaegeuk Kim 
1922ef86d709SGu Zheng 	sbi->total_valid_node_count--;
1923ef86d709SGu Zheng 	sbi->total_valid_block_count--;
192480d42145SYunlong Song 	if (sbi->reserved_blocks &&
192580d42145SYunlong Song 		sbi->current_reserved_blocks < sbi->reserved_blocks)
192680d42145SYunlong Song 		sbi->current_reserved_blocks++;
192739a53e0cSJaegeuk Kim 
192839a53e0cSJaegeuk Kim 	spin_unlock(&sbi->stat_lock);
19290abd675eSChao Yu 
19300abd675eSChao Yu 	if (!is_inode)
19310abd675eSChao Yu 		f2fs_i_blocks_write(inode, 1, false, true);
193239a53e0cSJaegeuk Kim }
193339a53e0cSJaegeuk Kim 
193439a53e0cSJaegeuk Kim static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
193539a53e0cSJaegeuk Kim {
19368b8343faSJaegeuk Kim 	return sbi->total_valid_node_count;
193739a53e0cSJaegeuk Kim }
193839a53e0cSJaegeuk Kim 
193939a53e0cSJaegeuk Kim static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
194039a53e0cSJaegeuk Kim {
1941513c5f37SJaegeuk Kim 	percpu_counter_inc(&sbi->total_valid_inode_count);
194239a53e0cSJaegeuk Kim }
194339a53e0cSJaegeuk Kim 
19440e80220aSJaegeuk Kim static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
194539a53e0cSJaegeuk Kim {
1946513c5f37SJaegeuk Kim 	percpu_counter_dec(&sbi->total_valid_inode_count);
194739a53e0cSJaegeuk Kim }
194839a53e0cSJaegeuk Kim 
1949513c5f37SJaegeuk Kim static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
195039a53e0cSJaegeuk Kim {
1951513c5f37SJaegeuk Kim 	return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
195239a53e0cSJaegeuk Kim }
195339a53e0cSJaegeuk Kim 
1954a56c7c6fSJaegeuk Kim static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
1955a56c7c6fSJaegeuk Kim 						pgoff_t index, bool for_write)
1956a56c7c6fSJaegeuk Kim {
1957c41f3cc3SJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
1958c41f3cc3SJaegeuk Kim 	struct page *page = find_lock_page(mapping, index);
1959cac5a3d8SDongOh Shin 
1960c41f3cc3SJaegeuk Kim 	if (page)
1961c41f3cc3SJaegeuk Kim 		return page;
1962c41f3cc3SJaegeuk Kim 
196355523519SChao Yu 	if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) {
196455523519SChao Yu 		f2fs_show_injection_info(FAULT_PAGE_ALLOC);
1965c41f3cc3SJaegeuk Kim 		return NULL;
196655523519SChao Yu 	}
1967c41f3cc3SJaegeuk Kim #endif
1968a56c7c6fSJaegeuk Kim 	if (!for_write)
1969a56c7c6fSJaegeuk Kim 		return grab_cache_page(mapping, index);
1970a56c7c6fSJaegeuk Kim 	return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
1971a56c7c6fSJaegeuk Kim }
1972a56c7c6fSJaegeuk Kim 
197301eccef7SChao Yu static inline struct page *f2fs_pagecache_get_page(
197401eccef7SChao Yu 				struct address_space *mapping, pgoff_t index,
197501eccef7SChao Yu 				int fgp_flags, gfp_t gfp_mask)
197601eccef7SChao Yu {
197701eccef7SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
197801eccef7SChao Yu 	if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_GET)) {
197901eccef7SChao Yu 		f2fs_show_injection_info(FAULT_PAGE_GET);
198001eccef7SChao Yu 		return NULL;
198101eccef7SChao Yu 	}
198201eccef7SChao Yu #endif
198301eccef7SChao Yu 	return pagecache_get_page(mapping, index, fgp_flags, gfp_mask);
198401eccef7SChao Yu }
198501eccef7SChao Yu 
19866e2c64adSJaegeuk Kim static inline void f2fs_copy_page(struct page *src, struct page *dst)
19876e2c64adSJaegeuk Kim {
19886e2c64adSJaegeuk Kim 	char *src_kaddr = kmap(src);
19896e2c64adSJaegeuk Kim 	char *dst_kaddr = kmap(dst);
19906e2c64adSJaegeuk Kim 
19916e2c64adSJaegeuk Kim 	memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
19926e2c64adSJaegeuk Kim 	kunmap(dst);
19936e2c64adSJaegeuk Kim 	kunmap(src);
19946e2c64adSJaegeuk Kim }
19956e2c64adSJaegeuk Kim 
199639a53e0cSJaegeuk Kim static inline void f2fs_put_page(struct page *page, int unlock)
199739a53e0cSJaegeuk Kim {
1998031fa8ccSJaegeuk Kim 	if (!page)
199939a53e0cSJaegeuk Kim 		return;
200039a53e0cSJaegeuk Kim 
200139a53e0cSJaegeuk Kim 	if (unlock) {
20029850cf4aSJaegeuk Kim 		f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
200339a53e0cSJaegeuk Kim 		unlock_page(page);
200439a53e0cSJaegeuk Kim 	}
200509cbfeafSKirill A. Shutemov 	put_page(page);
200639a53e0cSJaegeuk Kim }
200739a53e0cSJaegeuk Kim 
200839a53e0cSJaegeuk Kim static inline void f2fs_put_dnode(struct dnode_of_data *dn)
200939a53e0cSJaegeuk Kim {
201039a53e0cSJaegeuk Kim 	if (dn->node_page)
201139a53e0cSJaegeuk Kim 		f2fs_put_page(dn->node_page, 1);
201239a53e0cSJaegeuk Kim 	if (dn->inode_page && dn->node_page != dn->inode_page)
201339a53e0cSJaegeuk Kim 		f2fs_put_page(dn->inode_page, 0);
201439a53e0cSJaegeuk Kim 	dn->node_page = NULL;
201539a53e0cSJaegeuk Kim 	dn->inode_page = NULL;
201639a53e0cSJaegeuk Kim }
201739a53e0cSJaegeuk Kim 
201839a53e0cSJaegeuk Kim static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
2019e8512d2eSGu Zheng 					size_t size)
202039a53e0cSJaegeuk Kim {
2021e8512d2eSGu Zheng 	return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
202239a53e0cSJaegeuk Kim }
202339a53e0cSJaegeuk Kim 
20247bd59381SGu Zheng static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
20257bd59381SGu Zheng 						gfp_t flags)
20267bd59381SGu Zheng {
20277bd59381SGu Zheng 	void *entry;
20287bd59381SGu Zheng 
202980c54505SJaegeuk Kim 	entry = kmem_cache_alloc(cachep, flags);
203080c54505SJaegeuk Kim 	if (!entry)
203180c54505SJaegeuk Kim 		entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
20327bd59381SGu Zheng 	return entry;
20337bd59381SGu Zheng }
20347bd59381SGu Zheng 
2035d62fe971SChao Yu static inline struct bio *f2fs_bio_alloc(struct f2fs_sb_info *sbi,
2036d62fe971SChao Yu 						int npages, bool no_fail)
2037740432f8SJaegeuk Kim {
2038740432f8SJaegeuk Kim 	struct bio *bio;
2039740432f8SJaegeuk Kim 
2040d62fe971SChao Yu 	if (no_fail) {
2041740432f8SJaegeuk Kim 		/* No failure on bio allocation */
2042740432f8SJaegeuk Kim 		bio = bio_alloc(GFP_NOIO, npages);
204380c54505SJaegeuk Kim 		if (!bio)
204480c54505SJaegeuk Kim 			bio = bio_alloc(GFP_NOIO | __GFP_NOFAIL, npages);
2045740432f8SJaegeuk Kim 		return bio;
2046740432f8SJaegeuk Kim 	}
2047d62fe971SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
2048d62fe971SChao Yu 	if (time_to_inject(sbi, FAULT_ALLOC_BIO)) {
2049d62fe971SChao Yu 		f2fs_show_injection_info(FAULT_ALLOC_BIO);
2050d62fe971SChao Yu 		return NULL;
2051d62fe971SChao Yu 	}
2052d62fe971SChao Yu #endif
2053d62fe971SChao Yu 	return bio_alloc(GFP_KERNEL, npages);
2054d62fe971SChao Yu }
2055740432f8SJaegeuk Kim 
20569be32d72SJaegeuk Kim static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
20579be32d72SJaegeuk Kim 				unsigned long index, void *item)
20589be32d72SJaegeuk Kim {
20599be32d72SJaegeuk Kim 	while (radix_tree_insert(root, index, item))
20609be32d72SJaegeuk Kim 		cond_resched();
20619be32d72SJaegeuk Kim }
20629be32d72SJaegeuk Kim 
206339a53e0cSJaegeuk Kim #define RAW_IS_INODE(p)	((p)->footer.nid == (p)->footer.ino)
206439a53e0cSJaegeuk Kim 
206539a53e0cSJaegeuk Kim static inline bool IS_INODE(struct page *page)
206639a53e0cSJaegeuk Kim {
206745590710SGu Zheng 	struct f2fs_node *p = F2FS_NODE(page);
2068cac5a3d8SDongOh Shin 
206939a53e0cSJaegeuk Kim 	return RAW_IS_INODE(p);
207039a53e0cSJaegeuk Kim }
207139a53e0cSJaegeuk Kim 
20727a2af766SChao Yu static inline int offset_in_addr(struct f2fs_inode *i)
20737a2af766SChao Yu {
20747a2af766SChao Yu 	return (i->i_inline & F2FS_EXTRA_ATTR) ?
20757a2af766SChao Yu 			(le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0;
20767a2af766SChao Yu }
20777a2af766SChao Yu 
207839a53e0cSJaegeuk Kim static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
207939a53e0cSJaegeuk Kim {
208039a53e0cSJaegeuk Kim 	return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
208139a53e0cSJaegeuk Kim }
208239a53e0cSJaegeuk Kim 
20837a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode);
20847a2af766SChao Yu static inline block_t datablock_addr(struct inode *inode,
20857a2af766SChao Yu 			struct page *node_page, unsigned int offset)
208639a53e0cSJaegeuk Kim {
208739a53e0cSJaegeuk Kim 	struct f2fs_node *raw_node;
208839a53e0cSJaegeuk Kim 	__le32 *addr_array;
20897a2af766SChao Yu 	int base = 0;
20907a2af766SChao Yu 	bool is_inode = IS_INODE(node_page);
2091cac5a3d8SDongOh Shin 
209245590710SGu Zheng 	raw_node = F2FS_NODE(node_page);
20937a2af766SChao Yu 
20947a2af766SChao Yu 	/* from GC path only */
2095979f492fSLiFan 	if (is_inode) {
2096979f492fSLiFan 		if (!inode)
20977a2af766SChao Yu 			base = offset_in_addr(&raw_node->i);
2098979f492fSLiFan 		else if (f2fs_has_extra_attr(inode))
20997a2af766SChao Yu 			base = get_extra_isize(inode);
21007a2af766SChao Yu 	}
21017a2af766SChao Yu 
210239a53e0cSJaegeuk Kim 	addr_array = blkaddr_in_node(raw_node);
21037a2af766SChao Yu 	return le32_to_cpu(addr_array[base + offset]);
210439a53e0cSJaegeuk Kim }
210539a53e0cSJaegeuk Kim 
210639a53e0cSJaegeuk Kim static inline int f2fs_test_bit(unsigned int nr, char *addr)
210739a53e0cSJaegeuk Kim {
210839a53e0cSJaegeuk Kim 	int mask;
210939a53e0cSJaegeuk Kim 
211039a53e0cSJaegeuk Kim 	addr += (nr >> 3);
211139a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
211239a53e0cSJaegeuk Kim 	return mask & *addr;
211339a53e0cSJaegeuk Kim }
211439a53e0cSJaegeuk Kim 
2115a66cdd98SJaegeuk Kim static inline void f2fs_set_bit(unsigned int nr, char *addr)
2116a66cdd98SJaegeuk Kim {
2117a66cdd98SJaegeuk Kim 	int mask;
2118a66cdd98SJaegeuk Kim 
2119a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2120a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2121a66cdd98SJaegeuk Kim 	*addr |= mask;
2122a66cdd98SJaegeuk Kim }
2123a66cdd98SJaegeuk Kim 
2124a66cdd98SJaegeuk Kim static inline void f2fs_clear_bit(unsigned int nr, char *addr)
2125a66cdd98SJaegeuk Kim {
2126a66cdd98SJaegeuk Kim 	int mask;
2127a66cdd98SJaegeuk Kim 
2128a66cdd98SJaegeuk Kim 	addr += (nr >> 3);
2129a66cdd98SJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
2130a66cdd98SJaegeuk Kim 	*addr &= ~mask;
2131a66cdd98SJaegeuk Kim }
2132a66cdd98SJaegeuk Kim 
213352aca074SGu Zheng static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
213439a53e0cSJaegeuk Kim {
213539a53e0cSJaegeuk Kim 	int mask;
213639a53e0cSJaegeuk Kim 	int ret;
213739a53e0cSJaegeuk Kim 
213839a53e0cSJaegeuk Kim 	addr += (nr >> 3);
213939a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
214039a53e0cSJaegeuk Kim 	ret = mask & *addr;
214139a53e0cSJaegeuk Kim 	*addr |= mask;
214239a53e0cSJaegeuk Kim 	return ret;
214339a53e0cSJaegeuk Kim }
214439a53e0cSJaegeuk Kim 
214552aca074SGu Zheng static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
214639a53e0cSJaegeuk Kim {
214739a53e0cSJaegeuk Kim 	int mask;
214839a53e0cSJaegeuk Kim 	int ret;
214939a53e0cSJaegeuk Kim 
215039a53e0cSJaegeuk Kim 	addr += (nr >> 3);
215139a53e0cSJaegeuk Kim 	mask = 1 << (7 - (nr & 0x07));
215239a53e0cSJaegeuk Kim 	ret = mask & *addr;
215339a53e0cSJaegeuk Kim 	*addr &= ~mask;
215439a53e0cSJaegeuk Kim 	return ret;
215539a53e0cSJaegeuk Kim }
215639a53e0cSJaegeuk Kim 
2157c6ac4c0eSGu Zheng static inline void f2fs_change_bit(unsigned int nr, char *addr)
2158c6ac4c0eSGu Zheng {
2159c6ac4c0eSGu Zheng 	int mask;
2160c6ac4c0eSGu Zheng 
2161c6ac4c0eSGu Zheng 	addr += (nr >> 3);
2162c6ac4c0eSGu Zheng 	mask = 1 << (7 - (nr & 0x07));
2163c6ac4c0eSGu Zheng 	*addr ^= mask;
2164c6ac4c0eSGu Zheng }
2165c6ac4c0eSGu Zheng 
216659c84408SChao Yu /*
216759c84408SChao Yu  * Inode flags
216859c84408SChao Yu  */
216959c84408SChao Yu #define F2FS_SECRM_FL			0x00000001 /* Secure deletion */
217059c84408SChao Yu #define F2FS_UNRM_FL			0x00000002 /* Undelete */
217159c84408SChao Yu #define F2FS_COMPR_FL			0x00000004 /* Compress file */
217259c84408SChao Yu #define F2FS_SYNC_FL			0x00000008 /* Synchronous updates */
217359c84408SChao Yu #define F2FS_IMMUTABLE_FL		0x00000010 /* Immutable file */
217459c84408SChao Yu #define F2FS_APPEND_FL			0x00000020 /* writes to file may only append */
217559c84408SChao Yu #define F2FS_NODUMP_FL			0x00000040 /* do not dump file */
217659c84408SChao Yu #define F2FS_NOATIME_FL			0x00000080 /* do not update atime */
217759c84408SChao Yu /* Reserved for compression usage... */
217859c84408SChao Yu #define F2FS_DIRTY_FL			0x00000100
217959c84408SChao Yu #define F2FS_COMPRBLK_FL		0x00000200 /* One or more compressed clusters */
218059c84408SChao Yu #define F2FS_NOCOMPR_FL			0x00000400 /* Don't compress */
218159c84408SChao Yu #define F2FS_ENCRYPT_FL			0x00000800 /* encrypted file */
218259c84408SChao Yu /* End compression flags --- maybe not all used */
218359c84408SChao Yu #define F2FS_INDEX_FL			0x00001000 /* hash-indexed directory */
218459c84408SChao Yu #define F2FS_IMAGIC_FL			0x00002000 /* AFS directory */
218559c84408SChao Yu #define F2FS_JOURNAL_DATA_FL		0x00004000 /* file data should be journaled */
218659c84408SChao Yu #define F2FS_NOTAIL_FL			0x00008000 /* file tail should not be merged */
218759c84408SChao Yu #define F2FS_DIRSYNC_FL			0x00010000 /* dirsync behaviour (directories only) */
218859c84408SChao Yu #define F2FS_TOPDIR_FL			0x00020000 /* Top of directory hierarchies*/
218959c84408SChao Yu #define F2FS_HUGE_FILE_FL               0x00040000 /* Set to each huge file */
219059c84408SChao Yu #define F2FS_EXTENTS_FL			0x00080000 /* Inode uses extents */
219159c84408SChao Yu #define F2FS_EA_INODE_FL	        0x00200000 /* Inode used for large EA */
219259c84408SChao Yu #define F2FS_EOFBLOCKS_FL		0x00400000 /* Blocks allocated beyond EOF */
219359c84408SChao Yu #define F2FS_INLINE_DATA_FL		0x10000000 /* Inode has inline data. */
219459c84408SChao Yu #define F2FS_PROJINHERIT_FL		0x20000000 /* Create with parents projid */
219559c84408SChao Yu #define F2FS_RESERVED_FL		0x80000000 /* reserved for ext4 lib */
219659c84408SChao Yu 
219759c84408SChao Yu #define F2FS_FL_USER_VISIBLE		0x304BDFFF /* User visible flags */
219859c84408SChao Yu #define F2FS_FL_USER_MODIFIABLE		0x204BC0FF /* User modifiable flags */
219959c84408SChao Yu 
220059c84408SChao Yu /* Flags we can manipulate with through F2FS_IOC_FSSETXATTR */
220159c84408SChao Yu #define F2FS_FL_XFLAG_VISIBLE		(F2FS_SYNC_FL | \
220259c84408SChao Yu 					 F2FS_IMMUTABLE_FL | \
220359c84408SChao Yu 					 F2FS_APPEND_FL | \
220459c84408SChao Yu 					 F2FS_NODUMP_FL | \
220559c84408SChao Yu 					 F2FS_NOATIME_FL | \
220659c84408SChao Yu 					 F2FS_PROJINHERIT_FL)
220759c84408SChao Yu 
220859c84408SChao Yu /* Flags that should be inherited by new inodes from their parent. */
220959c84408SChao Yu #define F2FS_FL_INHERITED (F2FS_SECRM_FL | F2FS_UNRM_FL | F2FS_COMPR_FL |\
221059c84408SChao Yu 			   F2FS_SYNC_FL | F2FS_NODUMP_FL | F2FS_NOATIME_FL |\
221159c84408SChao Yu 			   F2FS_NOCOMPR_FL | F2FS_JOURNAL_DATA_FL |\
221259c84408SChao Yu 			   F2FS_NOTAIL_FL | F2FS_DIRSYNC_FL |\
221359c84408SChao Yu 			   F2FS_PROJINHERIT_FL)
221459c84408SChao Yu 
221559c84408SChao Yu /* Flags that are appropriate for regular files (all but dir-specific ones). */
221659c84408SChao Yu #define F2FS_REG_FLMASK		(~(F2FS_DIRSYNC_FL | F2FS_TOPDIR_FL))
221759c84408SChao Yu 
221859c84408SChao Yu /* Flags that are appropriate for non-directories/regular files. */
221959c84408SChao Yu #define F2FS_OTHER_FLMASK	(F2FS_NODUMP_FL | F2FS_NOATIME_FL)
22205c57132eSChao Yu 
22215c57132eSChao Yu static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
22225c57132eSChao Yu {
22235c57132eSChao Yu 	if (S_ISDIR(mode))
22245c57132eSChao Yu 		return flags;
22255c57132eSChao Yu 	else if (S_ISREG(mode))
22265c57132eSChao Yu 		return flags & F2FS_REG_FLMASK;
22275c57132eSChao Yu 	else
22285c57132eSChao Yu 		return flags & F2FS_OTHER_FLMASK;
22295c57132eSChao Yu }
22305c57132eSChao Yu 
223139a53e0cSJaegeuk Kim /* used for f2fs_inode_info->flags */
223239a53e0cSJaegeuk Kim enum {
223339a53e0cSJaegeuk Kim 	FI_NEW_INODE,		/* indicate newly allocated inode */
2234b3783873SJaegeuk Kim 	FI_DIRTY_INODE,		/* indicate inode is dirty or not */
223526de9b11SJaegeuk Kim 	FI_AUTO_RECOVER,	/* indicate inode is recoverable */
2236ed57c27fSJaegeuk Kim 	FI_DIRTY_DIR,		/* indicate directory has dirty pages */
223739a53e0cSJaegeuk Kim 	FI_INC_LINK,		/* need to increment i_nlink */
223839a53e0cSJaegeuk Kim 	FI_ACL_MODE,		/* indicate acl mode */
223939a53e0cSJaegeuk Kim 	FI_NO_ALLOC,		/* should not allocate any blocks */
2240c9b63bd0SJaegeuk Kim 	FI_FREE_NID,		/* free allocated nide */
2241c11abd1aSJaegeuk Kim 	FI_NO_EXTENT,		/* not to use the extent cache */
2242444c580fSJaegeuk Kim 	FI_INLINE_XATTR,	/* used for inline xattr */
22431001b347SHuajun Li 	FI_INLINE_DATA,		/* used for inline data*/
224434d67debSChao Yu 	FI_INLINE_DENTRY,	/* used for inline dentry */
2245fff04f90SJaegeuk Kim 	FI_APPEND_WRITE,	/* inode has appended data */
2246fff04f90SJaegeuk Kim 	FI_UPDATE_WRITE,	/* inode has in-place-update data */
224788b88a66SJaegeuk Kim 	FI_NEED_IPU,		/* used for ipu per file */
224888b88a66SJaegeuk Kim 	FI_ATOMIC_FILE,		/* indicate atomic file */
22495fe45743SChao Yu 	FI_ATOMIC_COMMIT,	/* indicate the state of atomical committing */
225002a1335fSJaegeuk Kim 	FI_VOLATILE_FILE,	/* indicate volatile file */
22513c6c2bebSJaegeuk Kim 	FI_FIRST_BLOCK_WRITTEN,	/* indicate #0 data block was written */
22521e84371fSJaegeuk Kim 	FI_DROP_CACHE,		/* drop dirty page cache */
2253b3d208f9SJaegeuk Kim 	FI_DATA_EXIST,		/* indicate data exists */
2254510022a8SJaegeuk Kim 	FI_INLINE_DOTS,		/* indicate inline dot dentries */
2255d323d005SChao Yu 	FI_DO_DEFRAG,		/* indicate defragment is running */
2256c227f912SChao Yu 	FI_DIRTY_FILE,		/* indicate regular/symlink has dirty pages */
2257dc91de78SJaegeuk Kim 	FI_NO_PREALLOC,		/* indicate skipped preallocated blocks */
2258ef095d19SJaegeuk Kim 	FI_HOT_DATA,		/* indicate file is hot */
22597a2af766SChao Yu 	FI_EXTRA_ATTR,		/* indicate file has extra attribute */
22605c57132eSChao Yu 	FI_PROJ_INHERIT,	/* indicate file inherits projectid */
22611ad71a27SJaegeuk Kim 	FI_PIN_FILE,		/* indicate file should not be gced */
22622ef79ecbSChao Yu 	FI_ATOMIC_REVOKE_REQUEST, /* request to drop atomic data */
226339a53e0cSJaegeuk Kim };
226439a53e0cSJaegeuk Kim 
2265205b9822SJaegeuk Kim static inline void __mark_inode_dirty_flag(struct inode *inode,
2266205b9822SJaegeuk Kim 						int flag, bool set)
226739a53e0cSJaegeuk Kim {
2268205b9822SJaegeuk Kim 	switch (flag) {
2269205b9822SJaegeuk Kim 	case FI_INLINE_XATTR:
2270205b9822SJaegeuk Kim 	case FI_INLINE_DATA:
2271205b9822SJaegeuk Kim 	case FI_INLINE_DENTRY:
22729ac1e2d8SDaeho Jeong 	case FI_NEW_INODE:
2273205b9822SJaegeuk Kim 		if (set)
2274205b9822SJaegeuk Kim 			return;
2275205b9822SJaegeuk Kim 	case FI_DATA_EXIST:
2276205b9822SJaegeuk Kim 	case FI_INLINE_DOTS:
22771ad71a27SJaegeuk Kim 	case FI_PIN_FILE:
22787c45729aSJaegeuk Kim 		f2fs_mark_inode_dirty_sync(inode, true);
2279205b9822SJaegeuk Kim 	}
228039a53e0cSJaegeuk Kim }
228139a53e0cSJaegeuk Kim 
228291942321SJaegeuk Kim static inline void set_inode_flag(struct inode *inode, int flag)
228339a53e0cSJaegeuk Kim {
228491942321SJaegeuk Kim 	if (!test_bit(flag, &F2FS_I(inode)->flags))
228591942321SJaegeuk Kim 		set_bit(flag, &F2FS_I(inode)->flags);
2286205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, true);
228739a53e0cSJaegeuk Kim }
228839a53e0cSJaegeuk Kim 
228991942321SJaegeuk Kim static inline int is_inode_flag_set(struct inode *inode, int flag)
229039a53e0cSJaegeuk Kim {
229191942321SJaegeuk Kim 	return test_bit(flag, &F2FS_I(inode)->flags);
229239a53e0cSJaegeuk Kim }
229339a53e0cSJaegeuk Kim 
229491942321SJaegeuk Kim static inline void clear_inode_flag(struct inode *inode, int flag)
229539a53e0cSJaegeuk Kim {
229691942321SJaegeuk Kim 	if (test_bit(flag, &F2FS_I(inode)->flags))
229791942321SJaegeuk Kim 		clear_bit(flag, &F2FS_I(inode)->flags);
2298205b9822SJaegeuk Kim 	__mark_inode_dirty_flag(inode, flag, false);
229939a53e0cSJaegeuk Kim }
230039a53e0cSJaegeuk Kim 
230191942321SJaegeuk Kim static inline void set_acl_inode(struct inode *inode, umode_t mode)
2302444c580fSJaegeuk Kim {
230391942321SJaegeuk Kim 	F2FS_I(inode)->i_acl_mode = mode;
230491942321SJaegeuk Kim 	set_inode_flag(inode, FI_ACL_MODE);
23057c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, false);
230639a53e0cSJaegeuk Kim }
230739a53e0cSJaegeuk Kim 
2308a1961246SJaegeuk Kim static inline void f2fs_i_links_write(struct inode *inode, bool inc)
2309a1961246SJaegeuk Kim {
2310a1961246SJaegeuk Kim 	if (inc)
2311a1961246SJaegeuk Kim 		inc_nlink(inode);
2312a1961246SJaegeuk Kim 	else
2313a1961246SJaegeuk Kim 		drop_nlink(inode);
23147c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2315a1961246SJaegeuk Kim }
2316a1961246SJaegeuk Kim 
23178edd03c8SJaegeuk Kim static inline void f2fs_i_blocks_write(struct inode *inode,
23180abd675eSChao Yu 					block_t diff, bool add, bool claim)
23198edd03c8SJaegeuk Kim {
232026de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
232126de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
232226de9b11SJaegeuk Kim 
23230abd675eSChao Yu 	/* add = 1, claim = 1 should be dquot_reserve_block in pair */
23240abd675eSChao Yu 	if (add) {
23250abd675eSChao Yu 		if (claim)
23260abd675eSChao Yu 			dquot_claim_block(inode, diff);
23270abd675eSChao Yu 		else
23280abd675eSChao Yu 			dquot_alloc_block_nofail(inode, diff);
23290abd675eSChao Yu 	} else {
23300abd675eSChao Yu 		dquot_free_block(inode, diff);
23310abd675eSChao Yu 	}
23320abd675eSChao Yu 
23337c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
233426de9b11SJaegeuk Kim 	if (clean || recover)
233526de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
23368edd03c8SJaegeuk Kim }
23378edd03c8SJaegeuk Kim 
2338fc9581c8SJaegeuk Kim static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
2339fc9581c8SJaegeuk Kim {
234026de9b11SJaegeuk Kim 	bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
234126de9b11SJaegeuk Kim 	bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
234226de9b11SJaegeuk Kim 
2343fc9581c8SJaegeuk Kim 	if (i_size_read(inode) == i_size)
2344fc9581c8SJaegeuk Kim 		return;
2345fc9581c8SJaegeuk Kim 
2346fc9581c8SJaegeuk Kim 	i_size_write(inode, i_size);
23477c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
234826de9b11SJaegeuk Kim 	if (clean || recover)
234926de9b11SJaegeuk Kim 		set_inode_flag(inode, FI_AUTO_RECOVER);
235026de9b11SJaegeuk Kim }
235126de9b11SJaegeuk Kim 
2352205b9822SJaegeuk Kim static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
2353205b9822SJaegeuk Kim {
2354205b9822SJaegeuk Kim 	F2FS_I(inode)->i_current_depth = depth;
23557c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2356205b9822SJaegeuk Kim }
2357205b9822SJaegeuk Kim 
23581ad71a27SJaegeuk Kim static inline void f2fs_i_gc_failures_write(struct inode *inode,
23591ad71a27SJaegeuk Kim 					unsigned int count)
23601ad71a27SJaegeuk Kim {
23612ef79ecbSChao Yu 	F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN] = count;
23621ad71a27SJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
23631ad71a27SJaegeuk Kim }
23641ad71a27SJaegeuk Kim 
2365205b9822SJaegeuk Kim static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
2366205b9822SJaegeuk Kim {
2367205b9822SJaegeuk Kim 	F2FS_I(inode)->i_xattr_nid = xnid;
23687c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2369205b9822SJaegeuk Kim }
2370205b9822SJaegeuk Kim 
2371205b9822SJaegeuk Kim static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
2372205b9822SJaegeuk Kim {
2373205b9822SJaegeuk Kim 	F2FS_I(inode)->i_pino = pino;
23747c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2375205b9822SJaegeuk Kim }
2376205b9822SJaegeuk Kim 
237791942321SJaegeuk Kim static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
2378444c580fSJaegeuk Kim {
2379205b9822SJaegeuk Kim 	struct f2fs_inode_info *fi = F2FS_I(inode);
2380205b9822SJaegeuk Kim 
2381444c580fSJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_XATTR)
2382205b9822SJaegeuk Kim 		set_bit(FI_INLINE_XATTR, &fi->flags);
23831001b347SHuajun Li 	if (ri->i_inline & F2FS_INLINE_DATA)
2384205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DATA, &fi->flags);
238534d67debSChao Yu 	if (ri->i_inline & F2FS_INLINE_DENTRY)
2386205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DENTRY, &fi->flags);
2387b3d208f9SJaegeuk Kim 	if (ri->i_inline & F2FS_DATA_EXIST)
2388205b9822SJaegeuk Kim 		set_bit(FI_DATA_EXIST, &fi->flags);
2389510022a8SJaegeuk Kim 	if (ri->i_inline & F2FS_INLINE_DOTS)
2390205b9822SJaegeuk Kim 		set_bit(FI_INLINE_DOTS, &fi->flags);
23917a2af766SChao Yu 	if (ri->i_inline & F2FS_EXTRA_ATTR)
23927a2af766SChao Yu 		set_bit(FI_EXTRA_ATTR, &fi->flags);
23931ad71a27SJaegeuk Kim 	if (ri->i_inline & F2FS_PIN_FILE)
23941ad71a27SJaegeuk Kim 		set_bit(FI_PIN_FILE, &fi->flags);
2395444c580fSJaegeuk Kim }
2396444c580fSJaegeuk Kim 
239791942321SJaegeuk Kim static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
2398444c580fSJaegeuk Kim {
2399444c580fSJaegeuk Kim 	ri->i_inline = 0;
2400444c580fSJaegeuk Kim 
240191942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_XATTR))
2402444c580fSJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_XATTR;
240391942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DATA))
24041001b347SHuajun Li 		ri->i_inline |= F2FS_INLINE_DATA;
240591942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
240634d67debSChao Yu 		ri->i_inline |= F2FS_INLINE_DENTRY;
240791942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_DATA_EXIST))
2408b3d208f9SJaegeuk Kim 		ri->i_inline |= F2FS_DATA_EXIST;
240991942321SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_INLINE_DOTS))
2410510022a8SJaegeuk Kim 		ri->i_inline |= F2FS_INLINE_DOTS;
24117a2af766SChao Yu 	if (is_inode_flag_set(inode, FI_EXTRA_ATTR))
24127a2af766SChao Yu 		ri->i_inline |= F2FS_EXTRA_ATTR;
24131ad71a27SJaegeuk Kim 	if (is_inode_flag_set(inode, FI_PIN_FILE))
24141ad71a27SJaegeuk Kim 		ri->i_inline |= F2FS_PIN_FILE;
24157a2af766SChao Yu }
24167a2af766SChao Yu 
24177a2af766SChao Yu static inline int f2fs_has_extra_attr(struct inode *inode)
24187a2af766SChao Yu {
24197a2af766SChao Yu 	return is_inode_flag_set(inode, FI_EXTRA_ATTR);
2420444c580fSJaegeuk Kim }
2421444c580fSJaegeuk Kim 
2422987c7c31SChao Yu static inline int f2fs_has_inline_xattr(struct inode *inode)
2423987c7c31SChao Yu {
242491942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_XATTR);
2425987c7c31SChao Yu }
2426987c7c31SChao Yu 
242781ca7350SChao Yu static inline unsigned int addrs_per_inode(struct inode *inode)
2428de93653fSJaegeuk Kim {
2429b323fd28SChao Yu 	return CUR_ADDRS_PER_INODE(inode) - get_inline_xattr_addrs(inode);
2430de93653fSJaegeuk Kim }
2431de93653fSJaegeuk Kim 
24326afc662eSChao Yu static inline void *inline_xattr_addr(struct inode *inode, struct page *page)
243365985d93SJaegeuk Kim {
2434695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
2435cac5a3d8SDongOh Shin 
243665985d93SJaegeuk Kim 	return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
2437b323fd28SChao Yu 					get_inline_xattr_addrs(inode)]);
243865985d93SJaegeuk Kim }
243965985d93SJaegeuk Kim 
244065985d93SJaegeuk Kim static inline int inline_xattr_size(struct inode *inode)
244165985d93SJaegeuk Kim {
24426afc662eSChao Yu 	return get_inline_xattr_addrs(inode) * sizeof(__le32);
244365985d93SJaegeuk Kim }
244465985d93SJaegeuk Kim 
24450dbdc2aeSJaegeuk Kim static inline int f2fs_has_inline_data(struct inode *inode)
24460dbdc2aeSJaegeuk Kim {
244791942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DATA);
24480dbdc2aeSJaegeuk Kim }
24490dbdc2aeSJaegeuk Kim 
2450b3d208f9SJaegeuk Kim static inline int f2fs_exist_data(struct inode *inode)
2451b3d208f9SJaegeuk Kim {
245291942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DATA_EXIST);
2453b3d208f9SJaegeuk Kim }
2454b3d208f9SJaegeuk Kim 
2455510022a8SJaegeuk Kim static inline int f2fs_has_inline_dots(struct inode *inode)
2456510022a8SJaegeuk Kim {
245791942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DOTS);
2458510022a8SJaegeuk Kim }
2459510022a8SJaegeuk Kim 
24601ad71a27SJaegeuk Kim static inline bool f2fs_is_pinned_file(struct inode *inode)
24611ad71a27SJaegeuk Kim {
24621ad71a27SJaegeuk Kim 	return is_inode_flag_set(inode, FI_PIN_FILE);
24631ad71a27SJaegeuk Kim }
24641ad71a27SJaegeuk Kim 
246588b88a66SJaegeuk Kim static inline bool f2fs_is_atomic_file(struct inode *inode)
246688b88a66SJaegeuk Kim {
246791942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_ATOMIC_FILE);
246888b88a66SJaegeuk Kim }
246988b88a66SJaegeuk Kim 
24705fe45743SChao Yu static inline bool f2fs_is_commit_atomic_write(struct inode *inode)
24715fe45743SChao Yu {
24725fe45743SChao Yu 	return is_inode_flag_set(inode, FI_ATOMIC_COMMIT);
24735fe45743SChao Yu }
24745fe45743SChao Yu 
247502a1335fSJaegeuk Kim static inline bool f2fs_is_volatile_file(struct inode *inode)
247602a1335fSJaegeuk Kim {
247791942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_VOLATILE_FILE);
247802a1335fSJaegeuk Kim }
247902a1335fSJaegeuk Kim 
24803c6c2bebSJaegeuk Kim static inline bool f2fs_is_first_block_written(struct inode *inode)
24813c6c2bebSJaegeuk Kim {
248291942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
24833c6c2bebSJaegeuk Kim }
24843c6c2bebSJaegeuk Kim 
24851e84371fSJaegeuk Kim static inline bool f2fs_is_drop_cache(struct inode *inode)
24861e84371fSJaegeuk Kim {
248791942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_DROP_CACHE);
24881e84371fSJaegeuk Kim }
24891e84371fSJaegeuk Kim 
2490f2470371SChao Yu static inline void *inline_data_addr(struct inode *inode, struct page *page)
24911001b347SHuajun Li {
2492695fd1edSChao Yu 	struct f2fs_inode *ri = F2FS_INODE(page);
24937a2af766SChao Yu 	int extra_size = get_extra_isize(inode);
2494cac5a3d8SDongOh Shin 
24957a2af766SChao Yu 	return (void *)&(ri->i_addr[extra_size + DEF_INLINE_RESERVED_SIZE]);
24961001b347SHuajun Li }
24971001b347SHuajun Li 
249834d67debSChao Yu static inline int f2fs_has_inline_dentry(struct inode *inode)
249934d67debSChao Yu {
250091942321SJaegeuk Kim 	return is_inode_flag_set(inode, FI_INLINE_DENTRY);
250134d67debSChao Yu }
250234d67debSChao Yu 
2503b5492af7SJaegeuk Kim static inline int is_file(struct inode *inode, int type)
2504b5492af7SJaegeuk Kim {
2505b5492af7SJaegeuk Kim 	return F2FS_I(inode)->i_advise & type;
2506b5492af7SJaegeuk Kim }
2507b5492af7SJaegeuk Kim 
2508b5492af7SJaegeuk Kim static inline void set_file(struct inode *inode, int type)
2509b5492af7SJaegeuk Kim {
2510b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise |= type;
25117c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2512b5492af7SJaegeuk Kim }
2513b5492af7SJaegeuk Kim 
2514b5492af7SJaegeuk Kim static inline void clear_file(struct inode *inode, int type)
2515b5492af7SJaegeuk Kim {
2516b5492af7SJaegeuk Kim 	F2FS_I(inode)->i_advise &= ~type;
25177c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(inode, true);
2518b5492af7SJaegeuk Kim }
2519b5492af7SJaegeuk Kim 
252026787236SJaegeuk Kim static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
252126787236SJaegeuk Kim {
252295582b00SDeepa Dinamani 	struct timespec ts;
2523a0d00fadSChao Yu 	bool ret;
2524a0d00fadSChao Yu 
252526787236SJaegeuk Kim 	if (dsync) {
252626787236SJaegeuk Kim 		struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
252726787236SJaegeuk Kim 
252826787236SJaegeuk Kim 		spin_lock(&sbi->inode_lock[DIRTY_META]);
252926787236SJaegeuk Kim 		ret = list_empty(&F2FS_I(inode)->gdirty_list);
253026787236SJaegeuk Kim 		spin_unlock(&sbi->inode_lock[DIRTY_META]);
253126787236SJaegeuk Kim 		return ret;
253226787236SJaegeuk Kim 	}
253326787236SJaegeuk Kim 	if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
253426787236SJaegeuk Kim 			file_keep_isize(inode) ||
2535235831d7SJunling Zheng 			i_size_read(inode) & ~PAGE_MASK)
253626787236SJaegeuk Kim 		return false;
2537a0d00fadSChao Yu 
253895582b00SDeepa Dinamani 	ts = timespec64_to_timespec(inode->i_atime);
253995582b00SDeepa Dinamani 	if (!timespec_equal(F2FS_I(inode)->i_disk_time, &ts))
2540214c2461SJaegeuk Kim 		return false;
254195582b00SDeepa Dinamani 	ts = timespec64_to_timespec(inode->i_ctime);
254295582b00SDeepa Dinamani 	if (!timespec_equal(F2FS_I(inode)->i_disk_time + 1, &ts))
2543214c2461SJaegeuk Kim 		return false;
254495582b00SDeepa Dinamani 	ts = timespec64_to_timespec(inode->i_mtime);
254595582b00SDeepa Dinamani 	if (!timespec_equal(F2FS_I(inode)->i_disk_time + 2, &ts))
2546214c2461SJaegeuk Kim 		return false;
2547214c2461SJaegeuk Kim 	if (!timespec_equal(F2FS_I(inode)->i_disk_time + 3,
2548214c2461SJaegeuk Kim 						&F2FS_I(inode)->i_crtime))
2549214c2461SJaegeuk Kim 		return false;
2550214c2461SJaegeuk Kim 
2551a0d00fadSChao Yu 	down_read(&F2FS_I(inode)->i_sem);
2552a0d00fadSChao Yu 	ret = F2FS_I(inode)->last_disk_size == i_size_read(inode);
2553a0d00fadSChao Yu 	up_read(&F2FS_I(inode)->i_sem);
2554a0d00fadSChao Yu 
2555a0d00fadSChao Yu 	return ret;
2556267378d4SChao Yu }
2557267378d4SChao Yu 
2558deeedd71SChao Yu static inline bool f2fs_readonly(struct super_block *sb)
255977888c1eSJaegeuk Kim {
2560deeedd71SChao Yu 	return sb_rdonly(sb);
256177888c1eSJaegeuk Kim }
256277888c1eSJaegeuk Kim 
2563744602cfSJaegeuk Kim static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
2564744602cfSJaegeuk Kim {
2565aaec2b1dSChao Yu 	return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
2566744602cfSJaegeuk Kim }
2567744602cfSJaegeuk Kim 
2568eaa693f4SJaegeuk Kim static inline bool is_dot_dotdot(const struct qstr *str)
2569eaa693f4SJaegeuk Kim {
2570eaa693f4SJaegeuk Kim 	if (str->len == 1 && str->name[0] == '.')
2571eaa693f4SJaegeuk Kim 		return true;
2572eaa693f4SJaegeuk Kim 
2573eaa693f4SJaegeuk Kim 	if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
2574eaa693f4SJaegeuk Kim 		return true;
2575eaa693f4SJaegeuk Kim 
2576eaa693f4SJaegeuk Kim 	return false;
2577eaa693f4SJaegeuk Kim }
2578eaa693f4SJaegeuk Kim 
25793e72f721SJaegeuk Kim static inline bool f2fs_may_extent_tree(struct inode *inode)
25803e72f721SJaegeuk Kim {
25813e72f721SJaegeuk Kim 	if (!test_opt(F2FS_I_SB(inode), EXTENT_CACHE) ||
258291942321SJaegeuk Kim 			is_inode_flag_set(inode, FI_NO_EXTENT))
25833e72f721SJaegeuk Kim 		return false;
25843e72f721SJaegeuk Kim 
2585886f56f9SAl Viro 	return S_ISREG(inode->i_mode);
25863e72f721SJaegeuk Kim }
25873e72f721SJaegeuk Kim 
25881ecc0c5cSChao Yu static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
25891ecc0c5cSChao Yu 					size_t size, gfp_t flags)
25900414b004SJaegeuk Kim {
25912c63feadSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
259255523519SChao Yu 	if (time_to_inject(sbi, FAULT_KMALLOC)) {
259355523519SChao Yu 		f2fs_show_injection_info(FAULT_KMALLOC);
25942c63feadSJaegeuk Kim 		return NULL;
259555523519SChao Yu 	}
25962c63feadSJaegeuk Kim #endif
25970414b004SJaegeuk Kim 	return kmalloc(size, flags);
25980414b004SJaegeuk Kim }
25990414b004SJaegeuk Kim 
2600acbf054dSChao Yu static inline void *f2fs_kzalloc(struct f2fs_sb_info *sbi,
2601acbf054dSChao Yu 					size_t size, gfp_t flags)
2602acbf054dSChao Yu {
2603acbf054dSChao Yu 	return f2fs_kmalloc(sbi, size, flags | __GFP_ZERO);
2604acbf054dSChao Yu }
2605acbf054dSChao Yu 
2606628b3d14SChao Yu static inline void *f2fs_kvmalloc(struct f2fs_sb_info *sbi,
2607628b3d14SChao Yu 					size_t size, gfp_t flags)
2608628b3d14SChao Yu {
2609628b3d14SChao Yu #ifdef CONFIG_F2FS_FAULT_INJECTION
2610628b3d14SChao Yu 	if (time_to_inject(sbi, FAULT_KVMALLOC)) {
2611628b3d14SChao Yu 		f2fs_show_injection_info(FAULT_KVMALLOC);
2612628b3d14SChao Yu 		return NULL;
2613628b3d14SChao Yu 	}
2614628b3d14SChao Yu #endif
2615628b3d14SChao Yu 	return kvmalloc(size, flags);
2616628b3d14SChao Yu }
2617628b3d14SChao Yu 
2618628b3d14SChao Yu static inline void *f2fs_kvzalloc(struct f2fs_sb_info *sbi,
2619628b3d14SChao Yu 					size_t size, gfp_t flags)
2620628b3d14SChao Yu {
2621628b3d14SChao Yu 	return f2fs_kvmalloc(sbi, size, flags | __GFP_ZERO);
2622628b3d14SChao Yu }
2623628b3d14SChao Yu 
26247a2af766SChao Yu static inline int get_extra_isize(struct inode *inode)
2625f2470371SChao Yu {
26267a2af766SChao Yu 	return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
2627f2470371SChao Yu }
2628f2470371SChao Yu 
26296afc662eSChao Yu static inline int get_inline_xattr_addrs(struct inode *inode)
26306afc662eSChao Yu {
26316afc662eSChao Yu 	return F2FS_I(inode)->i_inline_xattr_size;
26326afc662eSChao Yu }
26336afc662eSChao Yu 
26344d57b86dSChao Yu #define f2fs_get_inode_mode(i) \
263591942321SJaegeuk Kim 	((is_inode_flag_set(i, FI_ACL_MODE)) ? \
2636a6dda0e6SChristoph Hellwig 	 (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
2637a6dda0e6SChristoph Hellwig 
26387a2af766SChao Yu #define F2FS_TOTAL_EXTRA_ATTR_SIZE			\
26397a2af766SChao Yu 	(offsetof(struct f2fs_inode, i_extra_end) -	\
26407a2af766SChao Yu 	offsetof(struct f2fs_inode, i_extra_isize))	\
26417a2af766SChao Yu 
26425c57132eSChao Yu #define F2FS_OLD_ATTRIBUTE_SIZE	(offsetof(struct f2fs_inode, i_addr))
26435c57132eSChao Yu #define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field)		\
26445c57132eSChao Yu 		((offsetof(typeof(*f2fs_inode), field) +	\
26455c57132eSChao Yu 		sizeof((f2fs_inode)->field))			\
26465c57132eSChao Yu 		<= (F2FS_OLD_ATTRIBUTE_SIZE + extra_isize))	\
26475c57132eSChao Yu 
2648b0af6d49SChao Yu static inline void f2fs_reset_iostat(struct f2fs_sb_info *sbi)
2649b0af6d49SChao Yu {
2650b0af6d49SChao Yu 	int i;
2651b0af6d49SChao Yu 
2652b0af6d49SChao Yu 	spin_lock(&sbi->iostat_lock);
2653b0af6d49SChao Yu 	for (i = 0; i < NR_IO_TYPE; i++)
2654b0af6d49SChao Yu 		sbi->write_iostat[i] = 0;
2655b0af6d49SChao Yu 	spin_unlock(&sbi->iostat_lock);
2656b0af6d49SChao Yu }
2657b0af6d49SChao Yu 
2658b0af6d49SChao Yu static inline void f2fs_update_iostat(struct f2fs_sb_info *sbi,
2659b0af6d49SChao Yu 			enum iostat_type type, unsigned long long io_bytes)
2660b0af6d49SChao Yu {
2661b0af6d49SChao Yu 	if (!sbi->iostat_enable)
2662b0af6d49SChao Yu 		return;
2663b0af6d49SChao Yu 	spin_lock(&sbi->iostat_lock);
2664b0af6d49SChao Yu 	sbi->write_iostat[type] += io_bytes;
2665b0af6d49SChao Yu 
2666b0af6d49SChao Yu 	if (type == APP_WRITE_IO || type == APP_DIRECT_IO)
2667b0af6d49SChao Yu 		sbi->write_iostat[APP_BUFFERED_IO] =
2668b0af6d49SChao Yu 			sbi->write_iostat[APP_WRITE_IO] -
2669b0af6d49SChao Yu 			sbi->write_iostat[APP_DIRECT_IO];
2670b0af6d49SChao Yu 	spin_unlock(&sbi->iostat_lock);
2671b0af6d49SChao Yu }
2672b0af6d49SChao Yu 
26737b525dd0SChao Yu static inline bool is_valid_blkaddr(block_t blkaddr)
26747b525dd0SChao Yu {
26757b525dd0SChao Yu 	if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
26767b525dd0SChao Yu 		return false;
26777b525dd0SChao Yu 	return true;
26787b525dd0SChao Yu }
26797b525dd0SChao Yu 
268039a53e0cSJaegeuk Kim /*
268139a53e0cSJaegeuk Kim  * file.c
268239a53e0cSJaegeuk Kim  */
2683cac5a3d8SDongOh Shin int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
26844d57b86dSChao Yu void f2fs_truncate_data_blocks(struct dnode_of_data *dn);
26854d57b86dSChao Yu int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock);
2686cac5a3d8SDongOh Shin int f2fs_truncate(struct inode *inode);
2687a528d35eSDavid Howells int f2fs_getattr(const struct path *path, struct kstat *stat,
2688a528d35eSDavid Howells 			u32 request_mask, unsigned int flags);
2689cac5a3d8SDongOh Shin int f2fs_setattr(struct dentry *dentry, struct iattr *attr);
26904d57b86dSChao Yu int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
26914d57b86dSChao Yu void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count);
2692c4020b2dSChao Yu int f2fs_precache_extents(struct inode *inode);
2693cac5a3d8SDongOh Shin long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
2694cac5a3d8SDongOh Shin long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
26951ad71a27SJaegeuk Kim int f2fs_pin_file_control(struct inode *inode, bool inc);
269639a53e0cSJaegeuk Kim 
269739a53e0cSJaegeuk Kim /*
269839a53e0cSJaegeuk Kim  * inode.c
269939a53e0cSJaegeuk Kim  */
2700cac5a3d8SDongOh Shin void f2fs_set_inode_flags(struct inode *inode);
2701704956ecSChao Yu bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page);
2702704956ecSChao Yu void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page);
2703cac5a3d8SDongOh Shin struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
2704cac5a3d8SDongOh Shin struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
27054d57b86dSChao Yu int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
27064d57b86dSChao Yu void f2fs_update_inode(struct inode *inode, struct page *node_page);
27074d57b86dSChao Yu void f2fs_update_inode_page(struct inode *inode);
2708cac5a3d8SDongOh Shin int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
2709cac5a3d8SDongOh Shin void f2fs_evict_inode(struct inode *inode);
27104d57b86dSChao Yu void f2fs_handle_failed_inode(struct inode *inode);
271139a53e0cSJaegeuk Kim 
271239a53e0cSJaegeuk Kim /*
271339a53e0cSJaegeuk Kim  * namei.c
271439a53e0cSJaegeuk Kim  */
27154d57b86dSChao Yu int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
2716b6a06cbbSChao Yu 							bool hot, bool set);
271739a53e0cSJaegeuk Kim struct dentry *f2fs_get_parent(struct dentry *child);
271839a53e0cSJaegeuk Kim 
271939a53e0cSJaegeuk Kim /*
272039a53e0cSJaegeuk Kim  * dir.c
272139a53e0cSJaegeuk Kim  */
27224d57b86dSChao Yu unsigned char f2fs_get_de_type(struct f2fs_dir_entry *de);
27234d57b86dSChao Yu struct f2fs_dir_entry *f2fs_find_target_dentry(struct fscrypt_name *fname,
2724cac5a3d8SDongOh Shin 			f2fs_hash_t namehash, int *max_slots,
2725cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
2726cac5a3d8SDongOh Shin int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
2727cac5a3d8SDongOh Shin 			unsigned int start_pos, struct fscrypt_str *fstr);
27284d57b86dSChao Yu void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
2729cac5a3d8SDongOh Shin 			struct f2fs_dentry_ptr *d);
27304d57b86dSChao Yu struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
2731cac5a3d8SDongOh Shin 			const struct qstr *new_name,
2732cac5a3d8SDongOh Shin 			const struct qstr *orig_name, struct page *dpage);
27334d57b86dSChao Yu void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
2734cac5a3d8SDongOh Shin 			unsigned int current_depth);
27354d57b86dSChao Yu int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots);
2736cac5a3d8SDongOh Shin void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
2737cac5a3d8SDongOh Shin struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
2738cac5a3d8SDongOh Shin 			struct fscrypt_name *fname, struct page **res_page);
2739cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
2740cac5a3d8SDongOh Shin 			const struct qstr *child, struct page **res_page);
2741cac5a3d8SDongOh Shin struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
2742cac5a3d8SDongOh Shin ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
2743cac5a3d8SDongOh Shin 			struct page **page);
2744cac5a3d8SDongOh Shin void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
2745cac5a3d8SDongOh Shin 			struct page *page, struct inode *inode);
2746cac5a3d8SDongOh Shin void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
2747cac5a3d8SDongOh Shin 			const struct qstr *name, f2fs_hash_t name_hash,
2748cac5a3d8SDongOh Shin 			unsigned int bit_pos);
2749cac5a3d8SDongOh Shin int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
2750cac5a3d8SDongOh Shin 			const struct qstr *orig_name,
2751cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
27524d57b86dSChao Yu int f2fs_add_dentry(struct inode *dir, struct fscrypt_name *fname,
2753cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
27544d57b86dSChao Yu int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
2755cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
2756cac5a3d8SDongOh Shin void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
2757cac5a3d8SDongOh Shin 			struct inode *dir, struct inode *inode);
2758cac5a3d8SDongOh Shin int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
2759cac5a3d8SDongOh Shin bool f2fs_empty_dir(struct inode *dir);
276039a53e0cSJaegeuk Kim 
2761b7f7a5e0SAl Viro static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
2762b7f7a5e0SAl Viro {
27634d57b86dSChao Yu 	return f2fs_do_add_link(d_inode(dentry->d_parent), &dentry->d_name,
2764510022a8SJaegeuk Kim 				inode, inode->i_ino, inode->i_mode);
2765b7f7a5e0SAl Viro }
2766b7f7a5e0SAl Viro 
276739a53e0cSJaegeuk Kim /*
276839a53e0cSJaegeuk Kim  * super.c
276939a53e0cSJaegeuk Kim  */
2770cac5a3d8SDongOh Shin int f2fs_inode_dirtied(struct inode *inode, bool sync);
2771cac5a3d8SDongOh Shin void f2fs_inode_synced(struct inode *inode);
2772ea676733SJaegeuk Kim int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly);
27734b2414d0SChao Yu void f2fs_quota_off_umount(struct super_block *sb);
2774cac5a3d8SDongOh Shin int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
2775cac5a3d8SDongOh Shin int f2fs_sync_fs(struct super_block *sb, int sync);
2776a07ef784SNamjae Jeon extern __printf(3, 4)
2777cac5a3d8SDongOh Shin void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...);
27784d57b86dSChao Yu int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi);
277939a53e0cSJaegeuk Kim 
278039a53e0cSJaegeuk Kim /*
278139a53e0cSJaegeuk Kim  * hash.c
278239a53e0cSJaegeuk Kim  */
27836332cd32SJaegeuk Kim f2fs_hash_t f2fs_dentry_hash(const struct qstr *name_info,
27846332cd32SJaegeuk Kim 				struct fscrypt_name *fname);
278539a53e0cSJaegeuk Kim 
278639a53e0cSJaegeuk Kim /*
278739a53e0cSJaegeuk Kim  * node.c
278839a53e0cSJaegeuk Kim  */
278939a53e0cSJaegeuk Kim struct dnode_of_data;
279039a53e0cSJaegeuk Kim struct node_info;
279139a53e0cSJaegeuk Kim 
27924d57b86dSChao Yu int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid);
27934d57b86dSChao Yu bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type);
27944d57b86dSChao Yu int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
27954d57b86dSChao Yu bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
27964d57b86dSChao Yu bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
27974d57b86dSChao Yu void f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
27984d57b86dSChao Yu 						struct node_info *ni);
27994d57b86dSChao Yu pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
28004d57b86dSChao Yu int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
28014d57b86dSChao Yu int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from);
28024d57b86dSChao Yu int f2fs_truncate_xattr_node(struct inode *inode);
28034d57b86dSChao Yu int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino);
28044d57b86dSChao Yu int f2fs_remove_inode_page(struct inode *inode);
28054d57b86dSChao Yu struct page *f2fs_new_inode_page(struct inode *inode);
28064d57b86dSChao Yu struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs);
28074d57b86dSChao Yu void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
28084d57b86dSChao Yu struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
28094d57b86dSChao Yu struct page *f2fs_get_node_page_ra(struct page *parent, int start);
28104d57b86dSChao Yu void f2fs_move_node_page(struct page *node_page, int gc_type);
28114d57b86dSChao Yu int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
2812cac5a3d8SDongOh Shin 			struct writeback_control *wbc, bool atomic);
28134d57b86dSChao Yu int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
28144d57b86dSChao Yu 			struct writeback_control *wbc,
2815b0af6d49SChao Yu 			bool do_balance, enum iostat_type io_type);
2816*e2374015SChao Yu int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
28174d57b86dSChao Yu bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
28184d57b86dSChao Yu void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
28194d57b86dSChao Yu void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
28204d57b86dSChao Yu int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
28214d57b86dSChao Yu void f2fs_recover_inline_xattr(struct inode *inode, struct page *page);
28224d57b86dSChao Yu int f2fs_recover_xattr_data(struct inode *inode, struct page *page);
28234d57b86dSChao Yu int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
28244d57b86dSChao Yu void f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
2825cac5a3d8SDongOh Shin 			unsigned int segno, struct f2fs_summary_block *sum);
28264d57b86dSChao Yu void f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
28274d57b86dSChao Yu int f2fs_build_node_manager(struct f2fs_sb_info *sbi);
28284d57b86dSChao Yu void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi);
28294d57b86dSChao Yu int __init f2fs_create_node_manager_caches(void);
28304d57b86dSChao Yu void f2fs_destroy_node_manager_caches(void);
283139a53e0cSJaegeuk Kim 
283239a53e0cSJaegeuk Kim /*
283339a53e0cSJaegeuk Kim  * segment.c
283439a53e0cSJaegeuk Kim  */
28354d57b86dSChao Yu bool f2fs_need_SSR(struct f2fs_sb_info *sbi);
28364d57b86dSChao Yu void f2fs_register_inmem_page(struct inode *inode, struct page *page);
28374d57b86dSChao Yu void f2fs_drop_inmem_pages_all(struct f2fs_sb_info *sbi, bool gc_failure);
28384d57b86dSChao Yu void f2fs_drop_inmem_pages(struct inode *inode);
28394d57b86dSChao Yu void f2fs_drop_inmem_page(struct inode *inode, struct page *page);
28404d57b86dSChao Yu int f2fs_commit_inmem_pages(struct inode *inode);
2841cac5a3d8SDongOh Shin void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
2842cac5a3d8SDongOh Shin void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi);
284339d787beSChao Yu int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
28444d57b86dSChao Yu int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
28451228b482SChao Yu int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
28464d57b86dSChao Yu void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
28474d57b86dSChao Yu void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
28484d57b86dSChao Yu bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
28494d57b86dSChao Yu void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
28504d57b86dSChao Yu void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi);
2851cf5c759fSChao Yu bool f2fs_wait_discard_bios(struct f2fs_sb_info *sbi);
28524d57b86dSChao Yu void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
28534d57b86dSChao Yu 					struct cp_control *cpc);
28544d57b86dSChao Yu void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi);
28554d57b86dSChao Yu int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
28564d57b86dSChao Yu void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi);
2857cac5a3d8SDongOh Shin int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
28584d57b86dSChao Yu bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
28594d57b86dSChao Yu 					struct cp_control *cpc);
28604d57b86dSChao Yu struct page *f2fs_get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
28614d57b86dSChao Yu void f2fs_update_meta_page(struct f2fs_sb_info *sbi, void *src,
28624d57b86dSChao Yu 					block_t blk_addr);
28634d57b86dSChao Yu void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
2864b0af6d49SChao Yu 						enum iostat_type io_type);
28654d57b86dSChao Yu void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio);
28664d57b86dSChao Yu void f2fs_outplace_write_data(struct dnode_of_data *dn,
28674d57b86dSChao Yu 			struct f2fs_io_info *fio);
28684d57b86dSChao Yu int f2fs_inplace_write_data(struct f2fs_io_info *fio);
28694d57b86dSChao Yu void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
2870cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t new_blkaddr,
2871cac5a3d8SDongOh Shin 			bool recover_curseg, bool recover_newaddr);
2872cac5a3d8SDongOh Shin void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
2873cac5a3d8SDongOh Shin 			block_t old_addr, block_t new_addr,
2874cac5a3d8SDongOh Shin 			unsigned char version, bool recover_curseg,
2875cac5a3d8SDongOh Shin 			bool recover_newaddr);
28764d57b86dSChao Yu void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
2877cac5a3d8SDongOh Shin 			block_t old_blkaddr, block_t *new_blkaddr,
2878fb830fc5SChao Yu 			struct f2fs_summary *sum, int type,
2879fb830fc5SChao Yu 			struct f2fs_io_info *fio, bool add_list);
2880cac5a3d8SDongOh Shin void f2fs_wait_on_page_writeback(struct page *page,
2881cac5a3d8SDongOh Shin 			enum page_type type, bool ordered);
2882d4c759eeSJaegeuk Kim void f2fs_wait_on_block_writeback(struct f2fs_sb_info *sbi, block_t blkaddr);
28834d57b86dSChao Yu void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
28844d57b86dSChao Yu void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
28854d57b86dSChao Yu int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
2886cac5a3d8SDongOh Shin 			unsigned int val, int alloc);
28874d57b86dSChao Yu void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
28884d57b86dSChao Yu int f2fs_build_segment_manager(struct f2fs_sb_info *sbi);
28894d57b86dSChao Yu void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi);
28904d57b86dSChao Yu int __init f2fs_create_segment_manager_caches(void);
28914d57b86dSChao Yu void f2fs_destroy_segment_manager_caches(void);
28924d57b86dSChao Yu int f2fs_rw_hint_to_seg_type(enum rw_hint hint);
28934d57b86dSChao Yu enum rw_hint f2fs_io_type_to_rw_hint(struct f2fs_sb_info *sbi,
28944d57b86dSChao Yu 			enum page_type type, enum temp_type temp);
289539a53e0cSJaegeuk Kim 
289639a53e0cSJaegeuk Kim /*
289739a53e0cSJaegeuk Kim  * checkpoint.c
289839a53e0cSJaegeuk Kim  */
2899cac5a3d8SDongOh Shin void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io);
29004d57b86dSChao Yu struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
29014d57b86dSChao Yu struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
29024d57b86dSChao Yu struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
29034d57b86dSChao Yu bool f2fs_is_valid_meta_blkaddr(struct f2fs_sb_info *sbi,
29044d57b86dSChao Yu 			block_t blkaddr, int type);
29054d57b86dSChao Yu int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
2906cac5a3d8SDongOh Shin 			int type, bool sync);
29074d57b86dSChao Yu void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index);
29084d57b86dSChao Yu long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
2909b0af6d49SChao Yu 			long nr_to_write, enum iostat_type io_type);
29104d57b86dSChao Yu void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
29114d57b86dSChao Yu void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
29124d57b86dSChao Yu void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all);
29134d57b86dSChao Yu bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
29144d57b86dSChao Yu void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
291539d787beSChao Yu 					unsigned int devidx, int type);
29164d57b86dSChao Yu bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
291739d787beSChao Yu 					unsigned int devidx, int type);
2918cac5a3d8SDongOh Shin int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
29194d57b86dSChao Yu int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi);
29204d57b86dSChao Yu void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi);
29214d57b86dSChao Yu void f2fs_add_orphan_inode(struct inode *inode);
29224d57b86dSChao Yu void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
29234d57b86dSChao Yu int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi);
29244d57b86dSChao Yu int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi);
29254d57b86dSChao Yu void f2fs_update_dirty_page(struct inode *inode, struct page *page);
29264d57b86dSChao Yu void f2fs_remove_dirty_inode(struct inode *inode);
29274d57b86dSChao Yu int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
29284d57b86dSChao Yu int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
29294d57b86dSChao Yu void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi);
29304d57b86dSChao Yu int __init f2fs_create_checkpoint_caches(void);
29314d57b86dSChao Yu void f2fs_destroy_checkpoint_caches(void);
293239a53e0cSJaegeuk Kim 
293339a53e0cSJaegeuk Kim /*
293439a53e0cSJaegeuk Kim  * data.c
293539a53e0cSJaegeuk Kim  */
29366dbb1796SEric Biggers int f2fs_init_post_read_processing(void);
29376dbb1796SEric Biggers void f2fs_destroy_post_read_processing(void);
2938b9109b0eSJaegeuk Kim void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
2939b9109b0eSJaegeuk Kim void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
2940942fd319SJaegeuk Kim 				struct inode *inode, nid_t ino, pgoff_t idx,
2941b9109b0eSJaegeuk Kim 				enum page_type type);
2942b9109b0eSJaegeuk Kim void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
2943cac5a3d8SDongOh Shin int f2fs_submit_page_bio(struct f2fs_io_info *fio);
2944fe16efe6SChao Yu void f2fs_submit_page_write(struct f2fs_io_info *fio);
2945cac5a3d8SDongOh Shin struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
2946cac5a3d8SDongOh Shin 			block_t blk_addr, struct bio *bio);
2947cac5a3d8SDongOh Shin int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
29484d57b86dSChao Yu void f2fs_set_data_blkaddr(struct dnode_of_data *dn);
2949cac5a3d8SDongOh Shin void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
29504d57b86dSChao Yu int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
29514d57b86dSChao Yu int f2fs_reserve_new_block(struct dnode_of_data *dn);
2952cac5a3d8SDongOh Shin int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
2953cac5a3d8SDongOh Shin int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from);
2954cac5a3d8SDongOh Shin int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
29554d57b86dSChao Yu struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
2956cac5a3d8SDongOh Shin 			int op_flags, bool for_write);
29574d57b86dSChao Yu struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index);
29584d57b86dSChao Yu struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
2959cac5a3d8SDongOh Shin 			bool for_write);
29604d57b86dSChao Yu struct page *f2fs_get_new_data_page(struct inode *inode,
2961cac5a3d8SDongOh Shin 			struct page *ipage, pgoff_t index, bool new_i_size);
29624d57b86dSChao Yu int f2fs_do_write_data_page(struct f2fs_io_info *fio);
2963cac5a3d8SDongOh Shin int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
2964cac5a3d8SDongOh Shin 			int create, int flag);
2965cac5a3d8SDongOh Shin int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2966cac5a3d8SDongOh Shin 			u64 start, u64 len);
29674d57b86dSChao Yu bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio);
29684d57b86dSChao Yu bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio);
2969cac5a3d8SDongOh Shin void f2fs_invalidate_page(struct page *page, unsigned int offset,
2970cac5a3d8SDongOh Shin 			unsigned int length);
2971cac5a3d8SDongOh Shin int f2fs_release_page(struct page *page, gfp_t wait);
29725b7a487cSWeichao Guo #ifdef CONFIG_MIGRATION
2973cac5a3d8SDongOh Shin int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,
2974cac5a3d8SDongOh Shin 			struct page *page, enum migrate_mode mode);
29755b7a487cSWeichao Guo #endif
2976b91050a8SHyunchul Lee bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len);
29774d57b86dSChao Yu void f2fs_clear_radix_tree_dirty_tag(struct page *page);
297839a53e0cSJaegeuk Kim 
297939a53e0cSJaegeuk Kim /*
298039a53e0cSJaegeuk Kim  * gc.c
298139a53e0cSJaegeuk Kim  */
29824d57b86dSChao Yu int f2fs_start_gc_thread(struct f2fs_sb_info *sbi);
29834d57b86dSChao Yu void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi);
29844d57b86dSChao Yu block_t f2fs_start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
2985e066b83cSJaegeuk Kim int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background,
2986e066b83cSJaegeuk Kim 			unsigned int segno);
29874d57b86dSChao Yu void f2fs_build_gc_manager(struct f2fs_sb_info *sbi);
298839a53e0cSJaegeuk Kim 
298939a53e0cSJaegeuk Kim /*
299039a53e0cSJaegeuk Kim  * recovery.c
299139a53e0cSJaegeuk Kim  */
29924d57b86dSChao Yu int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
29934d57b86dSChao Yu bool f2fs_space_for_roll_forward(struct f2fs_sb_info *sbi);
299439a53e0cSJaegeuk Kim 
299539a53e0cSJaegeuk Kim /*
299639a53e0cSJaegeuk Kim  * debug.c
299739a53e0cSJaegeuk Kim  */
299839a53e0cSJaegeuk Kim #ifdef CONFIG_F2FS_STAT_FS
299939a53e0cSJaegeuk Kim struct f2fs_stat_info {
300039a53e0cSJaegeuk Kim 	struct list_head stat_list;
300139a53e0cSJaegeuk Kim 	struct f2fs_sb_info *sbi;
300239a53e0cSJaegeuk Kim 	int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
300339a53e0cSJaegeuk Kim 	int main_area_segs, main_area_sections, main_area_zones;
30045b7ee374SChao Yu 	unsigned long long hit_largest, hit_cached, hit_rbtree;
30055b7ee374SChao Yu 	unsigned long long hit_total, total_ext;
3006c00ba554SJaegeuk Kim 	int ext_tree, zombie_tree, ext_node;
30072c8a4a28SJaegeuk Kim 	int ndirty_node, ndirty_dent, ndirty_meta, ndirty_imeta;
30082c8a4a28SJaegeuk Kim 	int ndirty_data, ndirty_qdata;
300935782b23SJaegeuk Kim 	int inmem_pages;
30102c8a4a28SJaegeuk Kim 	unsigned int ndirty_dirs, ndirty_files, nquota_files, ndirty_all;
30115b0ef73cSJaegeuk Kim 	int nats, dirty_nats, sits, dirty_sits;
30125b0ef73cSJaegeuk Kim 	int free_nids, avail_nids, alloc_nids;
301339a53e0cSJaegeuk Kim 	int total_count, utilization;
30148b8dd65fSChao Yu 	int bg_gc, nr_wb_cp_data, nr_wb_data;
301514d8d5f7SChao Yu 	int nr_flushing, nr_flushed, flush_list_empty;
301614d8d5f7SChao Yu 	int nr_discarding, nr_discarded;
30175f32366aSChao Yu 	int nr_discard_cmd;
3018d84d1cbdSChao Yu 	unsigned int undiscard_blks;
3019a00861dbSJaegeuk Kim 	int inline_xattr, inline_inode, inline_dir, append, update, orphans;
3020648d50baSChao Yu 	int aw_cnt, max_aw_cnt, vw_cnt, max_vw_cnt;
3021f83a2584SYunlei He 	unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
302239a53e0cSJaegeuk Kim 	unsigned int bimodal, avg_vblocks;
302339a53e0cSJaegeuk Kim 	int util_free, util_valid, util_invalid;
302439a53e0cSJaegeuk Kim 	int rsvd_segs, overp_segs;
302539a53e0cSJaegeuk Kim 	int dirty_count, node_pages, meta_pages;
302642190d2aSJaegeuk Kim 	int prefree_count, call_count, cp_count, bg_cp_count;
302739a53e0cSJaegeuk Kim 	int tot_segs, node_segs, data_segs, free_segs, free_secs;
3028e1235983SChangman Lee 	int bg_node_segs, bg_data_segs;
302939a53e0cSJaegeuk Kim 	int tot_blks, data_blks, node_blks;
3030e1235983SChangman Lee 	int bg_data_blks, bg_node_blks;
30312ef79ecbSChao Yu 	unsigned long long skipped_atomic_files[2];
303239a53e0cSJaegeuk Kim 	int curseg[NR_CURSEG_TYPE];
303339a53e0cSJaegeuk Kim 	int cursec[NR_CURSEG_TYPE];
303439a53e0cSJaegeuk Kim 	int curzone[NR_CURSEG_TYPE];
303539a53e0cSJaegeuk Kim 
303639a53e0cSJaegeuk Kim 	unsigned int segment_count[2];
303739a53e0cSJaegeuk Kim 	unsigned int block_count[2];
3038b9a2c252SChangman Lee 	unsigned int inplace_count;
30399edcdabfSChao Yu 	unsigned long long base_mem, cache_mem, page_mem;
304039a53e0cSJaegeuk Kim };
304139a53e0cSJaegeuk Kim 
3042963d4f7dSGu Zheng static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
3043963d4f7dSGu Zheng {
3044963d4f7dSGu Zheng 	return (struct f2fs_stat_info *)sbi->stat_info;
3045963d4f7dSGu Zheng }
3046963d4f7dSGu Zheng 
3047942e0be6SChangman Lee #define stat_inc_cp_count(si)		((si)->cp_count++)
304842190d2aSJaegeuk Kim #define stat_inc_bg_cp_count(si)	((si)->bg_cp_count++)
304939a53e0cSJaegeuk Kim #define stat_inc_call_count(si)		((si)->call_count++)
3050dcdfff65SJaegeuk Kim #define stat_inc_bggc_count(sbi)	((sbi)->bg_gc++)
305133fbd510SChao Yu #define stat_inc_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]++)
305233fbd510SChao Yu #define stat_dec_dirty_inode(sbi, type)	((sbi)->ndirty_inode[type]--)
30535b7ee374SChao Yu #define stat_inc_total_hit(sbi)		(atomic64_inc(&(sbi)->total_hit_ext))
30545b7ee374SChao Yu #define stat_inc_rbtree_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_rbtree))
30555b7ee374SChao Yu #define stat_inc_largest_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_largest))
30565b7ee374SChao Yu #define stat_inc_cached_node_hit(sbi)	(atomic64_inc(&(sbi)->read_hit_cached))
3057d5e8f6c9SChao Yu #define stat_inc_inline_xattr(inode)					\
3058d5e8f6c9SChao Yu 	do {								\
3059d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3060d5e8f6c9SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_xattr));	\
3061d5e8f6c9SChao Yu 	} while (0)
3062d5e8f6c9SChao Yu #define stat_dec_inline_xattr(inode)					\
3063d5e8f6c9SChao Yu 	do {								\
3064d5e8f6c9SChao Yu 		if (f2fs_has_inline_xattr(inode))			\
3065d5e8f6c9SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_xattr));	\
3066d5e8f6c9SChao Yu 	} while (0)
30670dbdc2aeSJaegeuk Kim #define stat_inc_inline_inode(inode)					\
30680dbdc2aeSJaegeuk Kim 	do {								\
30690dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
307003e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_inode));	\
30710dbdc2aeSJaegeuk Kim 	} while (0)
30720dbdc2aeSJaegeuk Kim #define stat_dec_inline_inode(inode)					\
30730dbdc2aeSJaegeuk Kim 	do {								\
30740dbdc2aeSJaegeuk Kim 		if (f2fs_has_inline_data(inode))			\
307503e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_inode));	\
30760dbdc2aeSJaegeuk Kim 	} while (0)
30773289c061SJaegeuk Kim #define stat_inc_inline_dir(inode)					\
30783289c061SJaegeuk Kim 	do {								\
30793289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
308003e14d52SChao Yu 			(atomic_inc(&F2FS_I_SB(inode)->inline_dir));	\
30813289c061SJaegeuk Kim 	} while (0)
30823289c061SJaegeuk Kim #define stat_dec_inline_dir(inode)					\
30833289c061SJaegeuk Kim 	do {								\
30843289c061SJaegeuk Kim 		if (f2fs_has_inline_dentry(inode))			\
308503e14d52SChao Yu 			(atomic_dec(&F2FS_I_SB(inode)->inline_dir));	\
30863289c061SJaegeuk Kim 	} while (0)
3087dcdfff65SJaegeuk Kim #define stat_inc_seg_type(sbi, curseg)					\
3088dcdfff65SJaegeuk Kim 		((sbi)->segment_count[(curseg)->alloc_type]++)
3089dcdfff65SJaegeuk Kim #define stat_inc_block_count(sbi, curseg)				\
3090dcdfff65SJaegeuk Kim 		((sbi)->block_count[(curseg)->alloc_type]++)
3091b9a2c252SChangman Lee #define stat_inc_inplace_blocks(sbi)					\
3092b9a2c252SChangman Lee 		(atomic_inc(&(sbi)->inplace_count))
309326a28a0cSJaegeuk Kim #define stat_inc_atomic_write(inode)					\
3094cac5a3d8SDongOh Shin 		(atomic_inc(&F2FS_I_SB(inode)->aw_cnt))
309526a28a0cSJaegeuk Kim #define stat_dec_atomic_write(inode)					\
3096cac5a3d8SDongOh Shin 		(atomic_dec(&F2FS_I_SB(inode)->aw_cnt))
309726a28a0cSJaegeuk Kim #define stat_update_max_atomic_write(inode)				\
309826a28a0cSJaegeuk Kim 	do {								\
309926a28a0cSJaegeuk Kim 		int cur = atomic_read(&F2FS_I_SB(inode)->aw_cnt);	\
310026a28a0cSJaegeuk Kim 		int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt);	\
310126a28a0cSJaegeuk Kim 		if (cur > max)						\
310226a28a0cSJaegeuk Kim 			atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur);	\
310326a28a0cSJaegeuk Kim 	} while (0)
3104648d50baSChao Yu #define stat_inc_volatile_write(inode)					\
3105648d50baSChao Yu 		(atomic_inc(&F2FS_I_SB(inode)->vw_cnt))
3106648d50baSChao Yu #define stat_dec_volatile_write(inode)					\
3107648d50baSChao Yu 		(atomic_dec(&F2FS_I_SB(inode)->vw_cnt))
3108648d50baSChao Yu #define stat_update_max_volatile_write(inode)				\
3109648d50baSChao Yu 	do {								\
3110648d50baSChao Yu 		int cur = atomic_read(&F2FS_I_SB(inode)->vw_cnt);	\
3111648d50baSChao Yu 		int max = atomic_read(&F2FS_I_SB(inode)->max_vw_cnt);	\
3112648d50baSChao Yu 		if (cur > max)						\
3113648d50baSChao Yu 			atomic_set(&F2FS_I_SB(inode)->max_vw_cnt, cur);	\
3114648d50baSChao Yu 	} while (0)
3115e1235983SChangman Lee #define stat_inc_seg_count(sbi, type, gc_type)				\
311639a53e0cSJaegeuk Kim 	do {								\
3117963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
311868afcf2dSTomohiro Kusumi 		si->tot_segs++;						\
311968afcf2dSTomohiro Kusumi 		if ((type) == SUM_TYPE_DATA) {				\
312039a53e0cSJaegeuk Kim 			si->data_segs++;				\
3121e1235983SChangman Lee 			si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0;	\
3122e1235983SChangman Lee 		} else {						\
312339a53e0cSJaegeuk Kim 			si->node_segs++;				\
3124e1235983SChangman Lee 			si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0;	\
3125e1235983SChangman Lee 		}							\
312639a53e0cSJaegeuk Kim 	} while (0)
312739a53e0cSJaegeuk Kim 
312839a53e0cSJaegeuk Kim #define stat_inc_tot_blk_count(si, blks)				\
312968afcf2dSTomohiro Kusumi 	((si)->tot_blks += (blks))
313039a53e0cSJaegeuk Kim 
3131e1235983SChangman Lee #define stat_inc_data_blk_count(sbi, blks, gc_type)			\
313239a53e0cSJaegeuk Kim 	do {								\
3133963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
313439a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
313539a53e0cSJaegeuk Kim 		si->data_blks += (blks);				\
313668afcf2dSTomohiro Kusumi 		si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
313739a53e0cSJaegeuk Kim 	} while (0)
313839a53e0cSJaegeuk Kim 
3139e1235983SChangman Lee #define stat_inc_node_blk_count(sbi, blks, gc_type)			\
314039a53e0cSJaegeuk Kim 	do {								\
3141963d4f7dSGu Zheng 		struct f2fs_stat_info *si = F2FS_STAT(sbi);		\
314239a53e0cSJaegeuk Kim 		stat_inc_tot_blk_count(si, blks);			\
314339a53e0cSJaegeuk Kim 		si->node_blks += (blks);				\
314468afcf2dSTomohiro Kusumi 		si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0;	\
314539a53e0cSJaegeuk Kim 	} while (0)
314639a53e0cSJaegeuk Kim 
3147cac5a3d8SDongOh Shin int f2fs_build_stats(struct f2fs_sb_info *sbi);
3148cac5a3d8SDongOh Shin void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
3149787c7b8cSChao Yu int __init f2fs_create_root_stats(void);
31504589d25dSNamjae Jeon void f2fs_destroy_root_stats(void);
315139a53e0cSJaegeuk Kim #else
3152d66450e7SArnd Bergmann #define stat_inc_cp_count(si)				do { } while (0)
3153d66450e7SArnd Bergmann #define stat_inc_bg_cp_count(si)			do { } while (0)
3154d66450e7SArnd Bergmann #define stat_inc_call_count(si)				do { } while (0)
3155d66450e7SArnd Bergmann #define stat_inc_bggc_count(si)				do { } while (0)
3156d66450e7SArnd Bergmann #define stat_inc_dirty_inode(sbi, type)			do { } while (0)
3157d66450e7SArnd Bergmann #define stat_dec_dirty_inode(sbi, type)			do { } while (0)
3158d66450e7SArnd Bergmann #define stat_inc_total_hit(sb)				do { } while (0)
3159d66450e7SArnd Bergmann #define stat_inc_rbtree_node_hit(sb)			do { } while (0)
3160d66450e7SArnd Bergmann #define stat_inc_largest_node_hit(sbi)			do { } while (0)
3161d66450e7SArnd Bergmann #define stat_inc_cached_node_hit(sbi)			do { } while (0)
3162d66450e7SArnd Bergmann #define stat_inc_inline_xattr(inode)			do { } while (0)
3163d66450e7SArnd Bergmann #define stat_dec_inline_xattr(inode)			do { } while (0)
3164d66450e7SArnd Bergmann #define stat_inc_inline_inode(inode)			do { } while (0)
3165d66450e7SArnd Bergmann #define stat_dec_inline_inode(inode)			do { } while (0)
3166d66450e7SArnd Bergmann #define stat_inc_inline_dir(inode)			do { } while (0)
3167d66450e7SArnd Bergmann #define stat_dec_inline_dir(inode)			do { } while (0)
3168d66450e7SArnd Bergmann #define stat_inc_atomic_write(inode)			do { } while (0)
3169d66450e7SArnd Bergmann #define stat_dec_atomic_write(inode)			do { } while (0)
3170d66450e7SArnd Bergmann #define stat_update_max_atomic_write(inode)		do { } while (0)
3171d66450e7SArnd Bergmann #define stat_inc_volatile_write(inode)			do { } while (0)
3172d66450e7SArnd Bergmann #define stat_dec_volatile_write(inode)			do { } while (0)
3173d66450e7SArnd Bergmann #define stat_update_max_volatile_write(inode)		do { } while (0)
3174d66450e7SArnd Bergmann #define stat_inc_seg_type(sbi, curseg)			do { } while (0)
3175d66450e7SArnd Bergmann #define stat_inc_block_count(sbi, curseg)		do { } while (0)
3176d66450e7SArnd Bergmann #define stat_inc_inplace_blocks(sbi)			do { } while (0)
3177d66450e7SArnd Bergmann #define stat_inc_seg_count(sbi, type, gc_type)		do { } while (0)
3178d66450e7SArnd Bergmann #define stat_inc_tot_blk_count(si, blks)		do { } while (0)
3179d66450e7SArnd Bergmann #define stat_inc_data_blk_count(sbi, blks, gc_type)	do { } while (0)
3180d66450e7SArnd Bergmann #define stat_inc_node_blk_count(sbi, blks, gc_type)	do { } while (0)
318139a53e0cSJaegeuk Kim 
318239a53e0cSJaegeuk Kim static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
318339a53e0cSJaegeuk Kim static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
3184787c7b8cSChao Yu static inline int __init f2fs_create_root_stats(void) { return 0; }
31854589d25dSNamjae Jeon static inline void f2fs_destroy_root_stats(void) { }
318639a53e0cSJaegeuk Kim #endif
318739a53e0cSJaegeuk Kim 
318839a53e0cSJaegeuk Kim extern const struct file_operations f2fs_dir_operations;
318939a53e0cSJaegeuk Kim extern const struct file_operations f2fs_file_operations;
319039a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_file_inode_operations;
319139a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_dblock_aops;
319239a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_node_aops;
319339a53e0cSJaegeuk Kim extern const struct address_space_operations f2fs_meta_aops;
319439a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_dir_inode_operations;
319539a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_symlink_inode_operations;
3196cbaf042aSJaegeuk Kim extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
319739a53e0cSJaegeuk Kim extern const struct inode_operations f2fs_special_inode_operations;
31984d57b86dSChao Yu extern struct kmem_cache *f2fs_inode_entry_slab;
31991001b347SHuajun Li 
3200e18c65b2SHuajun Li /*
3201e18c65b2SHuajun Li  * inline.c
3202e18c65b2SHuajun Li  */
3203cac5a3d8SDongOh Shin bool f2fs_may_inline_data(struct inode *inode);
3204cac5a3d8SDongOh Shin bool f2fs_may_inline_dentry(struct inode *inode);
32054d57b86dSChao Yu void f2fs_do_read_inline_data(struct page *page, struct page *ipage);
32064d57b86dSChao Yu void f2fs_truncate_inline_inode(struct inode *inode,
32074d57b86dSChao Yu 						struct page *ipage, u64 from);
3208cac5a3d8SDongOh Shin int f2fs_read_inline_data(struct inode *inode, struct page *page);
3209cac5a3d8SDongOh Shin int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
3210cac5a3d8SDongOh Shin int f2fs_convert_inline_inode(struct inode *inode);
3211cac5a3d8SDongOh Shin int f2fs_write_inline_data(struct inode *inode, struct page *page);
32124d57b86dSChao Yu bool f2fs_recover_inline_data(struct inode *inode, struct page *npage);
32134d57b86dSChao Yu struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
3214cac5a3d8SDongOh Shin 			struct fscrypt_name *fname, struct page **res_page);
32154d57b86dSChao Yu int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent,
3216cac5a3d8SDongOh Shin 			struct page *ipage);
3217cac5a3d8SDongOh Shin int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
3218cac5a3d8SDongOh Shin 			const struct qstr *orig_name,
3219cac5a3d8SDongOh Shin 			struct inode *inode, nid_t ino, umode_t mode);
32204d57b86dSChao Yu void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry,
32214d57b86dSChao Yu 				struct page *page, struct inode *dir,
32224d57b86dSChao Yu 				struct inode *inode);
3223cac5a3d8SDongOh Shin bool f2fs_empty_inline_dir(struct inode *dir);
3224cac5a3d8SDongOh Shin int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
3225cac5a3d8SDongOh Shin 			struct fscrypt_str *fstr);
3226cac5a3d8SDongOh Shin int f2fs_inline_data_fiemap(struct inode *inode,
3227cac5a3d8SDongOh Shin 			struct fiemap_extent_info *fieinfo,
3228cac5a3d8SDongOh Shin 			__u64 start, __u64 len);
3229cde4de12SJaegeuk Kim 
3230cde4de12SJaegeuk Kim /*
32312658e50dSJaegeuk Kim  * shrinker.c
32322658e50dSJaegeuk Kim  */
3233cac5a3d8SDongOh Shin unsigned long f2fs_shrink_count(struct shrinker *shrink,
3234cac5a3d8SDongOh Shin 			struct shrink_control *sc);
3235cac5a3d8SDongOh Shin unsigned long f2fs_shrink_scan(struct shrinker *shrink,
3236cac5a3d8SDongOh Shin 			struct shrink_control *sc);
3237cac5a3d8SDongOh Shin void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
3238cac5a3d8SDongOh Shin void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
32392658e50dSJaegeuk Kim 
32402658e50dSJaegeuk Kim /*
3241a28ef1f5SChao Yu  * extent_cache.c
3242a28ef1f5SChao Yu  */
32434d57b86dSChao Yu struct rb_entry *f2fs_lookup_rb_tree(struct rb_root *root,
3244004b6862SChao Yu 				struct rb_entry *cached_re, unsigned int ofs);
32454d57b86dSChao Yu struct rb_node **f2fs_lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
3246004b6862SChao Yu 				struct rb_root *root, struct rb_node **parent,
3247004b6862SChao Yu 				unsigned int ofs);
32484d57b86dSChao Yu struct rb_entry *f2fs_lookup_rb_tree_ret(struct rb_root *root,
3249004b6862SChao Yu 		struct rb_entry *cached_re, unsigned int ofs,
3250004b6862SChao Yu 		struct rb_entry **prev_entry, struct rb_entry **next_entry,
3251004b6862SChao Yu 		struct rb_node ***insert_p, struct rb_node **insert_parent,
3252004b6862SChao Yu 		bool force);
32534d57b86dSChao Yu bool f2fs_check_rb_tree_consistence(struct f2fs_sb_info *sbi,
3254df0f6b44SChao Yu 						struct rb_root *root);
3255cac5a3d8SDongOh Shin unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
3256cac5a3d8SDongOh Shin bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext);
3257cac5a3d8SDongOh Shin void f2fs_drop_extent_tree(struct inode *inode);
3258cac5a3d8SDongOh Shin unsigned int f2fs_destroy_extent_node(struct inode *inode);
3259cac5a3d8SDongOh Shin void f2fs_destroy_extent_tree(struct inode *inode);
3260cac5a3d8SDongOh Shin bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
3261cac5a3d8SDongOh Shin 			struct extent_info *ei);
3262cac5a3d8SDongOh Shin void f2fs_update_extent_cache(struct dnode_of_data *dn);
326319b2c30dSChao Yu void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
3264cac5a3d8SDongOh Shin 			pgoff_t fofs, block_t blkaddr, unsigned int len);
32654d57b86dSChao Yu void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi);
32664d57b86dSChao Yu int __init f2fs_create_extent_cache(void);
32674d57b86dSChao Yu void f2fs_destroy_extent_cache(void);
3268a28ef1f5SChao Yu 
3269a28ef1f5SChao Yu /*
32708ceffcb2SChao Yu  * sysfs.c
32718ceffcb2SChao Yu  */
3272dc6b2055SJaegeuk Kim int __init f2fs_init_sysfs(void);
3273dc6b2055SJaegeuk Kim void f2fs_exit_sysfs(void);
3274dc6b2055SJaegeuk Kim int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
3275dc6b2055SJaegeuk Kim void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
32768ceffcb2SChao Yu 
32778ceffcb2SChao Yu /*
3278cde4de12SJaegeuk Kim  * crypto support
3279cde4de12SJaegeuk Kim  */
32800b81d077SJaegeuk Kim static inline bool f2fs_encrypted_inode(struct inode *inode)
3281cde4de12SJaegeuk Kim {
3282cde4de12SJaegeuk Kim 	return file_is_encrypt(inode);
3283cde4de12SJaegeuk Kim }
3284cde4de12SJaegeuk Kim 
32851958593eSJaegeuk Kim static inline bool f2fs_encrypted_file(struct inode *inode)
32861958593eSJaegeuk Kim {
32871958593eSJaegeuk Kim 	return f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode);
32881958593eSJaegeuk Kim }
32891958593eSJaegeuk Kim 
3290cde4de12SJaegeuk Kim static inline void f2fs_set_encrypted_inode(struct inode *inode)
3291cde4de12SJaegeuk Kim {
3292cde4de12SJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
3293cde4de12SJaegeuk Kim 	file_set_encrypt(inode);
32942ee6a576SEric Biggers 	inode->i_flags |= S_ENCRYPTED;
3295cde4de12SJaegeuk Kim #endif
3296cde4de12SJaegeuk Kim }
3297cde4de12SJaegeuk Kim 
32986dbb1796SEric Biggers /*
32996dbb1796SEric Biggers  * Returns true if the reads of the inode's data need to undergo some
33006dbb1796SEric Biggers  * postprocessing step, like decryption or authenticity verification.
33016dbb1796SEric Biggers  */
33026dbb1796SEric Biggers static inline bool f2fs_post_read_required(struct inode *inode)
3303cde4de12SJaegeuk Kim {
33046dbb1796SEric Biggers 	return f2fs_encrypted_file(inode);
3305cde4de12SJaegeuk Kim }
3306cde4de12SJaegeuk Kim 
3307ccd31cb2SSheng Yong #define F2FS_FEATURE_FUNCS(name, flagname) \
3308ccd31cb2SSheng Yong static inline int f2fs_sb_has_##name(struct super_block *sb) \
3309ccd31cb2SSheng Yong { \
3310ccd31cb2SSheng Yong 	return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_##flagname); \
3311cde4de12SJaegeuk Kim }
3312f424f664SJaegeuk Kim 
3313ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(encrypt, ENCRYPT);
3314ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(blkzoned, BLKZONED);
3315ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(extra_attr, EXTRA_ATTR);
3316ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(project_quota, PRJQUOTA);
3317ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_chksum, INODE_CHKSUM);
3318ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
3319ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
3320ccd31cb2SSheng Yong F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
3321b7c409deSSheng Yong F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
33221c1d35dfSChao Yu 
3323178053e2SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED
3324178053e2SDamien Le Moal static inline int get_blkz_type(struct f2fs_sb_info *sbi,
33253c62be17SJaegeuk Kim 			struct block_device *bdev, block_t blkaddr)
3326178053e2SDamien Le Moal {
3327178053e2SDamien Le Moal 	unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
33283c62be17SJaegeuk Kim 	int i;
3329178053e2SDamien Le Moal 
33303c62be17SJaegeuk Kim 	for (i = 0; i < sbi->s_ndevs; i++)
33313c62be17SJaegeuk Kim 		if (FDEV(i).bdev == bdev)
33323c62be17SJaegeuk Kim 			return FDEV(i).blkz_type[zno];
33333c62be17SJaegeuk Kim 	return -EINVAL;
3334178053e2SDamien Le Moal }
3335178053e2SDamien Le Moal #endif
3336178053e2SDamien Le Moal 
333796ba2decSDamien Le Moal static inline bool f2fs_discard_en(struct f2fs_sb_info *sbi)
333896ba2decSDamien Le Moal {
333996ba2decSDamien Le Moal 	struct request_queue *q = bdev_get_queue(sbi->sb->s_bdev);
334096ba2decSDamien Le Moal 
3341ccd31cb2SSheng Yong 	return blk_queue_discard(q) || f2fs_sb_has_blkzoned(sbi->sb);
334252763a4bSJaegeuk Kim }
334352763a4bSJaegeuk Kim 
334452763a4bSJaegeuk Kim static inline void set_opt_mode(struct f2fs_sb_info *sbi, unsigned int mt)
334552763a4bSJaegeuk Kim {
334652763a4bSJaegeuk Kim 	clear_opt(sbi, ADAPTIVE);
334752763a4bSJaegeuk Kim 	clear_opt(sbi, LFS);
334852763a4bSJaegeuk Kim 
334952763a4bSJaegeuk Kim 	switch (mt) {
335052763a4bSJaegeuk Kim 	case F2FS_MOUNT_ADAPTIVE:
335152763a4bSJaegeuk Kim 		set_opt(sbi, ADAPTIVE);
335252763a4bSJaegeuk Kim 		break;
335352763a4bSJaegeuk Kim 	case F2FS_MOUNT_LFS:
335452763a4bSJaegeuk Kim 		set_opt(sbi, LFS);
335552763a4bSJaegeuk Kim 		break;
335652763a4bSJaegeuk Kim 	}
335752763a4bSJaegeuk Kim }
335852763a4bSJaegeuk Kim 
3359fcc85a4dSJaegeuk Kim static inline bool f2fs_may_encrypt(struct inode *inode)
3360fcc85a4dSJaegeuk Kim {
3361fcc85a4dSJaegeuk Kim #ifdef CONFIG_F2FS_FS_ENCRYPTION
3362886f56f9SAl Viro 	umode_t mode = inode->i_mode;
3363fcc85a4dSJaegeuk Kim 
3364fcc85a4dSJaegeuk Kim 	return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
3365fcc85a4dSJaegeuk Kim #else
3366fcc85a4dSJaegeuk Kim 	return 0;
3367fcc85a4dSJaegeuk Kim #endif
3368fcc85a4dSJaegeuk Kim }
3369fcc85a4dSJaegeuk Kim 
3370b91050a8SHyunchul Lee static inline bool f2fs_force_buffered_io(struct inode *inode, int rw)
3371b91050a8SHyunchul Lee {
33726dbb1796SEric Biggers 	return (f2fs_post_read_required(inode) ||
3373b91050a8SHyunchul Lee 			(rw == WRITE && test_opt(F2FS_I_SB(inode), LFS)) ||
3374b91050a8SHyunchul Lee 			F2FS_I_SB(inode)->s_ndevs);
3375b91050a8SHyunchul Lee }
3376b91050a8SHyunchul Lee 
337783a3bfdbSJaegeuk Kim #ifdef CONFIG_F2FS_FAULT_INJECTION
337883a3bfdbSJaegeuk Kim extern void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate);
337983a3bfdbSJaegeuk Kim #else
338083a3bfdbSJaegeuk Kim #define f2fs_build_fault_attr(sbi, rate)		do { } while (0)
338183a3bfdbSJaegeuk Kim #endif
338283a3bfdbSJaegeuk Kim 
338339a53e0cSJaegeuk Kim #endif
3384